xref: /original-bsd/sys/netns/ns_input.c (revision 69360437)
1 /*
2  * Copyright (c) 1984, 1985, 1986, 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)ns_input.c	7.6 (Berkeley) 04/22/89
18  */
19 
20 #include "param.h"
21 #include "systm.h"
22 #include "malloc.h"
23 #include "mbuf.h"
24 #include "domain.h"
25 #include "protosw.h"
26 #include "socket.h"
27 #include "socketvar.h"
28 #include "errno.h"
29 #include "time.h"
30 #include "kernel.h"
31 
32 #include "../net/if.h"
33 #include "../net/route.h"
34 #include "../net/raw_cb.h"
35 
36 #include "ns.h"
37 #include "ns_if.h"
38 #include "ns_pcb.h"
39 #include "idp.h"
40 #include "idp_var.h"
41 #include "ns_error.h"
42 
43 /*
44  * NS initialization.
45  */
46 union ns_host	ns_thishost;
47 union ns_host	ns_zerohost;
48 union ns_host	ns_broadhost;
49 union ns_net	ns_zeronet;
50 union ns_net	ns_broadnet;
51 struct sockaddr_ns ns_netmask, ns_hostmask;
52 
53 static u_short allones[] = {-1, -1, -1};
54 
55 struct nspcb nspcb;
56 struct nspcb nsrawpcb;
57 
58 struct ifqueue	nsintrq;
59 int	nsqmaxlen = IFQ_MAXLEN;
60 
61 int	idpcksum = 1;
62 long	ns_pexseq;
63 
64 ns_init()
65 {
66 	extern struct timeval time;
67 
68 	ns_broadhost = * (union ns_host *) allones;
69 	ns_broadnet = * (union ns_net *) allones;
70 	nspcb.nsp_next = nspcb.nsp_prev = &nspcb;
71 	nsrawpcb.nsp_next = nsrawpcb.nsp_prev = &nsrawpcb;
72 	nsintrq.ifq_maxlen = nsqmaxlen;
73 	ns_pexseq = time.tv_usec;
74 	ns_netmask.sns_len = 6;
75 	ns_netmask.sns_addr.x_net = ns_broadnet;
76 	ns_hostmask.sns_len = 12;
77 	ns_hostmask.sns_addr.x_net = ns_broadnet;
78 	ns_hostmask.sns_addr.x_host = ns_broadhost;
79 }
80 
81 /*
82  * Idp input routine.  Pass to next level.
83  */
84 int nsintr_getpck = 0;
85 int nsintr_swtch = 0;
86 nsintr()
87 {
88 	register struct idp *idp;
89 	register struct mbuf *m;
90 	register struct nspcb *nsp;
91 	register int i;
92 	int len, s, error;
93 	char oddpacketp;
94 
95 next:
96 	/*
97 	 * Get next datagram off input queue and get IDP header
98 	 * in first mbuf.
99 	 */
100 	s = splimp();
101 	IF_DEQUEUE(&nsintrq, m);
102 	splx(s);
103 	nsintr_getpck++;
104 	if (m == 0)
105 		return;
106 	if ((m->m_flags & M_EXT || m->m_len < sizeof (struct idp)) &&
107 	    (m = m_pullup(m, sizeof (struct idp))) == 0) {
108 		idpstat.idps_toosmall++;
109 		goto next;
110 	}
111 
112 	/*
113 	 * Give any raw listeners a crack at the packet
114 	 */
115 	for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) {
116 		struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
117 		if (m1) idp_input(m1, nsp);
118 	}
119 
120 	idp = mtod(m, struct idp *);
121 	len = ntohs(idp->idp_len);
122 	if (oddpacketp = len & 1) {
123 		len++;		/* If this packet is of odd length,
124 				   preserve garbage byte for checksum */
125 	}
126 
127 	/*
128 	 * Check that the amount of data in the buffers
129 	 * is as at least much as the IDP header would have us expect.
130 	 * Trim mbufs if longer than we expect.
131 	 * Drop packet if shorter than we expect.
132 	 */
133 	if (m->m_pkthdr.len < len) {
134 		idpstat.idps_tooshort++;
135 		goto bad;
136 	}
137 	if (m->m_pkthdr.len > len) {
138 		if (m->m_len == m->m_pkthdr.len) {
139 			m->m_len = len;
140 			m->m_pkthdr.len = len;
141 		} else
142 			m_adj(m, len - m->m_pkthdr.len);
143 	}
144 	if (idpcksum && ((i = idp->idp_sum)!=0xffff)) {
145 		idp->idp_sum = 0;
146 		if (i != (idp->idp_sum = ns_cksum(m, len))) {
147 			idpstat.idps_badsum++;
148 			idp->idp_sum = i;
149 			if (ns_hosteqnh(ns_thishost, idp->idp_dna.x_host))
150 				error = NS_ERR_BADSUM;
151 			else
152 				error = NS_ERR_BADSUM_T;
153 			ns_error(m, error, 0);
154 			goto next;
155 		}
156 	}
157 	/*
158 	 * Is this a directed broadcast?
159 	 */
160 	if (ns_hosteqnh(ns_broadhost,idp->idp_dna.x_host)) {
161 		if ((!ns_neteq(idp->idp_dna, idp->idp_sna)) &&
162 		    (!ns_neteqnn(idp->idp_dna.x_net, ns_broadnet)) &&
163 		    (!ns_neteqnn(idp->idp_sna.x_net, ns_zeronet)) &&
164 		    (!ns_neteqnn(idp->idp_dna.x_net, ns_zeronet)) ) {
165 			/*
166 			 * Look to see if I need to eat this packet.
167 			 * Algorithm is to forward all young packets
168 			 * and prematurely age any packets which will
169 			 * by physically broadcasted.
170 			 * Any very old packets eaten without forwarding
171 			 * would die anyway.
172 			 *
173 			 * Suggestion of Bill Nesheim, Cornell U.
174 			 */
175 			if (idp->idp_tc < NS_MAXHOPS) {
176 				idp_forward(m);
177 				goto next;
178 			}
179 		}
180 	/*
181 	 * Is this our packet? If not, forward.
182 	 */
183 	} else if (!ns_hosteqnh(ns_thishost,idp->idp_dna.x_host)) {
184 		idp_forward(m);
185 		goto next;
186 	}
187 	/*
188 	 * Locate pcb for datagram.
189 	 */
190 	nsp = ns_pcblookup(&idp->idp_sna, idp->idp_dna.x_port, NS_WILDCARD);
191 	/*
192 	 * Switch out to protocol's input routine.
193 	 */
194 	nsintr_swtch++;
195 	if (nsp) {
196 		if (oddpacketp) {
197 			m_adj(m, -1);
198 		}
199 		if ((nsp->nsp_flags & NSP_ALL_PACKETS)==0)
200 			switch (idp->idp_pt) {
201 
202 			    case NSPROTO_SPP:
203 				    spp_input(m, nsp);
204 				    goto next;
205 
206 			    case NSPROTO_ERROR:
207 				    ns_err_input(m);
208 				    goto next;
209 			}
210 		idp_input(m, nsp);
211 	} else {
212 		ns_error(m, NS_ERR_NOSOCK, 0);
213 	}
214 	goto next;
215 
216 bad:
217 	m_freem(m);
218 	goto next;
219 }
220 
221 u_char nsctlerrmap[PRC_NCMDS] = {
222 	ECONNABORTED,	ECONNABORTED,	0,		0,
223 	0,		0,		EHOSTDOWN,	EHOSTUNREACH,
224 	ENETUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
225 	EMSGSIZE,	0,		0,		0,
226 	0,		0,		0,		0
227 };
228 
229 idp_donosocks = 1;
230 
231 idp_ctlinput(cmd, arg)
232 	int cmd;
233 	caddr_t arg;
234 {
235 	struct ns_addr *ns;
236 	struct nspcb *nsp;
237 	struct ns_errp *errp;
238 	int idp_abort();
239 	extern struct nspcb *idp_drop();
240 	int type;
241 
242 	if (cmd < 0 || cmd > PRC_NCMDS)
243 		return;
244 	if (nsctlerrmap[cmd] == 0)
245 		return;		/* XXX */
246 	type = NS_ERR_UNREACH_HOST;
247 	switch (cmd) {
248 		struct sockaddr_ns *sns;
249 
250 	case PRC_IFDOWN:
251 	case PRC_HOSTDEAD:
252 	case PRC_HOSTUNREACH:
253 		sns = (struct sockaddr_ns *)arg;
254 		if (sns->sns_family != AF_NS)
255 			return;
256 		ns = &sns->sns_addr;
257 		break;
258 
259 	default:
260 		errp = (struct ns_errp *)arg;
261 		ns = &errp->ns_err_idp.idp_dna;
262 		type = errp->ns_err_num;
263 		type = ntohs((u_short)type);
264 	}
265 	switch (type) {
266 
267 	case NS_ERR_UNREACH_HOST:
268 		ns_pcbnotify(ns, (int)nsctlerrmap[cmd], idp_abort, (long)0);
269 		break;
270 
271 	case NS_ERR_NOSOCK:
272 		nsp = ns_pcblookup(ns, errp->ns_err_idp.idp_sna.x_port,
273 			NS_WILDCARD);
274 		if(nsp && idp_donosocks && ! ns_nullhost(nsp->nsp_faddr))
275 			(void) idp_drop(nsp, (int)nsctlerrmap[cmd]);
276 	}
277 }
278 
279 int	idpprintfs = 0;
280 int	idpforwarding = 1;
281 /*
282  * Forward a packet.  If some error occurs return the sender
283  * an error packet.  Note we can't always generate a meaningful
284  * error message because the NS errors don't have a large enough repetoire
285  * of codes and types.
286  */
287 struct route idp_droute;
288 struct route idp_sroute;
289 
290 idp_forward(m)
291 struct mbuf *m;
292 {
293 	register struct idp *idp = mtod(m, struct idp *);
294 	register int error, type, code;
295 	struct mbuf *mcopy = NULL;
296 	int agedelta = 1;
297 	int flags = NS_FORWARDING;
298 	int ok_there = 0;
299 	int ok_back = 0;
300 
301 	if (idpprintfs) {
302 		printf("forward: src ");
303 		ns_printhost(&idp->idp_sna);
304 		printf(", dst ");
305 		ns_printhost(&idp->idp_dna);
306 		printf("hop count %d\n", idp->idp_tc);
307 	}
308 	if (idpforwarding == 0) {
309 		/* can't tell difference between net and host */
310 		type = NS_ERR_UNREACH_HOST, code = 0;
311 		goto senderror;
312 	}
313 	idp->idp_tc++;
314 	if (idp->idp_tc > NS_MAXHOPS) {
315 		type = NS_ERR_TOO_OLD, code = 0;
316 		goto senderror;
317 	}
318 	/*
319 	 * Save at most 42 bytes of the packet in case
320 	 * we need to generate an NS error message to the src.
321 	 */
322 	mcopy = m_copy(m, 0, imin((int)ntohs(idp->idp_len), 42));
323 
324 	if ((ok_there = idp_do_route(&idp->idp_dna,&idp_droute))==0) {
325 		type = NS_ERR_UNREACH_HOST, code = 0;
326 		goto senderror;
327 	}
328 	/*
329 	 * Here we think about  forwarding  broadcast packets,
330 	 * so we try to insure that it doesn't go back out
331 	 * on the interface it came in on.  Also, if we
332 	 * are going to physically broadcast this, let us
333 	 * age the packet so we can eat it safely the second time around.
334 	 */
335 	if (idp->idp_dna.x_host.c_host[0] & 0x1) {
336 		struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna);
337 		struct ifnet *ifp;
338 		if (ia) {
339 			/* I'm gonna hafta eat this packet */
340 			agedelta += NS_MAXHOPS - idp->idp_tc;
341 			idp->idp_tc = NS_MAXHOPS;
342 		}
343 		if ((ok_back = idp_do_route(&idp->idp_sna,&idp_sroute))==0) {
344 			/* error = ENETUNREACH; He'll never get it! */
345 			m_freem(m);
346 			goto cleanup;
347 		}
348 		if (idp_droute.ro_rt &&
349 		    (ifp=idp_droute.ro_rt->rt_ifp) &&
350 		    idp_sroute.ro_rt &&
351 		    (ifp!=idp_sroute.ro_rt->rt_ifp)) {
352 			flags |= NS_ALLOWBROADCAST;
353 		} else {
354 			type = NS_ERR_UNREACH_HOST, code = 0;
355 			goto senderror;
356 		}
357 	}
358 	/* need to adjust checksum */
359 	if (idp->idp_sum!=0xffff) {
360 		union bytes {
361 			u_char c[4];
362 			u_short s[2];
363 			long l;
364 		} x;
365 		register int shift;
366 		x.l = 0; x.c[0] = agedelta;
367 		shift = (((((int)ntohs(idp->idp_len))+1)>>1)-2) & 0xf;
368 		x.l = idp->idp_sum + (x.s[0] << shift);
369 		x.l = x.s[0] + x.s[1];
370 		x.l = x.s[0] + x.s[1];
371 		if (x.l==0xffff) idp->idp_sum = 0; else idp->idp_sum = x.l;
372 	}
373 	if ((error = ns_output(m, &idp_droute, flags)) &&
374 	    (mcopy!=NULL)) {
375 		idp = mtod(mcopy, struct idp *);
376 		type = NS_ERR_UNSPEC_T, code = 0;
377 		switch (error) {
378 
379 		case ENETUNREACH:
380 		case EHOSTDOWN:
381 		case EHOSTUNREACH:
382 		case ENETDOWN:
383 		case EPERM:
384 			type = NS_ERR_UNREACH_HOST;
385 			break;
386 
387 		case EMSGSIZE:
388 			type = NS_ERR_TOO_BIG;
389 			code = 576; /* too hard to figure out mtu here */
390 			break;
391 
392 		case ENOBUFS:
393 			type = NS_ERR_UNSPEC_T;
394 			break;
395 		}
396 		mcopy = NULL;
397 	senderror:
398 		ns_error(m, type, code);
399 	}
400 cleanup:
401 	if (ok_there)
402 		idp_undo_route(&idp_droute);
403 	if (ok_back)
404 		idp_undo_route(&idp_sroute);
405 	if (mcopy != NULL)
406 		m_freem(mcopy);
407 }
408 
409 idp_do_route(src, ro)
410 struct ns_addr *src;
411 struct route *ro;
412 {
413 
414 	struct sockaddr_ns *dst;
415 
416 	bzero((caddr_t)ro, sizeof (*ro));
417 	dst = (struct sockaddr_ns *)&ro->ro_dst;
418 
419 	dst->sns_len = sizeof(*dst);
420 	dst->sns_family = AF_NS;
421 	dst->sns_addr = *src;
422 	dst->sns_addr.x_port = 0;
423 	rtalloc(ro);
424 	if (ro->ro_rt == 0 || ro->ro_rt->rt_ifp == 0) {
425 		return (0);
426 	}
427 	ro->ro_rt->rt_use++;
428 	return (1);
429 }
430 
431 idp_undo_route(ro)
432 register struct route *ro;
433 {
434 	if (ro->ro_rt) {RTFREE(ro->ro_rt);}
435 }
436 static union ns_net
437 ns_zeronet;
438 
439 ns_watch_output(m, ifp)
440 struct mbuf *m;
441 struct ifnet *ifp;
442 {
443 	register struct nspcb *nsp;
444 	register struct ifaddr *ifa;
445 	/*
446 	 * Give any raw listeners a crack at the packet
447 	 */
448 	for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) {
449 		struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
450 		if (m0) {
451 			register struct idp *idp;
452 
453 			M_PREPEND(m0, sizeof (*idp), M_DONTWAIT);
454 			if (m0 == NULL)
455 				continue;
456 			idp = mtod(m0, struct idp *);
457 			idp->idp_sna.x_net = ns_zeronet;
458 			idp->idp_sna.x_host = ns_thishost;
459 			if (ifp && (ifp->if_flags & IFF_POINTOPOINT))
460 			    for(ifa = ifp->if_addrlist; ifa;
461 						ifa = ifa->ifa_next) {
462 				if (ifa->ifa_addr->sa_family==AF_NS) {
463 				    idp->idp_sna = IA_SNS(ifa)->sns_addr;
464 				    break;
465 				}
466 			    }
467 			idp->idp_len = ntohl(m0->m_pkthdr.len);
468 			idp_input(m0, nsp);
469 		}
470 	}
471 }
472