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