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