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