1 /*
2 * Copyright (c) 1995, Mike Mitchell
3 * Copyright (c) 1984, 1985, 1986, 1987, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)ipx_input.c
35 *
36 * $FreeBSD: src/sys/netipx/ipx_input.c,v 1.9.2.3 1999/09/05 08:18:58 peter Exp $
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/mbuf.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/kernel.h>
45 #include <sys/sysctl.h>
46
47 #include <net/if.h>
48 #include <net/route.h>
49 #include <net/netisr.h>
50
51 #include <netipx/ipx.h>
52 #include <netipx/spx.h>
53 #include <netipx/ipx_if.h>
54 #include <netipx/ipx_pcb.h>
55 #include <netipx/ipx_var.h>
56
57 int ipxcksum = 0;
58 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, checksum, CTLFLAG_RW,
59 &ipxcksum, 0, "");
60
61 static int ipxprintfs = 0; /* printing forwarding information */
62 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxprintfs, CTLFLAG_RW,
63 &ipxprintfs, 0, "");
64
65 static int ipxforwarding = 0;
66 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxforwarding, CTLFLAG_RW,
67 &ipxforwarding, 0, "");
68
69 static int ipxnetbios = 0;
70 SYSCTL_INT(_net_ipx, OID_AUTO, ipxnetbios, CTLFLAG_RW,
71 &ipxnetbios, 0, "");
72
73 union ipx_net ipx_zeronet;
74 union ipx_host ipx_zerohost;
75
76 union ipx_net ipx_broadnet;
77 union ipx_host ipx_broadhost;
78
79 struct ipxstat ipxstat;
80 struct sockaddr_ipx ipx_netmask, ipx_hostmask;
81
82 static u_short allones[] = {-1, -1, -1};
83
84 struct ipxpcb ipxpcb;
85 struct ipxpcb ipxrawpcb;
86
87 struct ifqueue ipxintrq;
88 int ipxqmaxlen = IFQ_MAXLEN;
89
90 long ipx_pexseq;
91
92 NETISR_SET(NETISR_IPX, ipxintr);
93
94 static int ipx_do_route(struct ipx_addr *src, struct route *ro);
95 static void ipx_undo_route(struct route *ro);
96 static void ipx_forward(struct mbuf *m);
97
98 /*
99 * IPX initialization.
100 */
101
102 void
103 ipx_init()
104 {
105 ipx_broadnet = *(union ipx_net *)allones;
106 ipx_broadhost = *(union ipx_host *)allones;
107
108 ipx_pexseq = time.tv_usec;
109 ipxintrq.ifq_maxlen = ipxqmaxlen;
110 ipxpcb.ipxp_next = ipxpcb.ipxp_prev = &ipxpcb;
111 ipxrawpcb.ipxp_next = ipxrawpcb.ipxp_prev = &ipxrawpcb;
112
113 ipx_netmask.sipx_len = 6;
114 ipx_netmask.sipx_addr.x_net = ipx_broadnet;
115
116 ipx_hostmask.sipx_len = 12;
117 ipx_hostmask.sipx_addr.x_net = ipx_broadnet;
118 ipx_hostmask.sipx_addr.x_host = ipx_broadhost;
119 }
120
121 /*
122 * IPX input routine. Pass to next level.
123 */
124 void
125 ipxintr()
126 {
127 register struct ipx *ipx;
128 register struct mbuf *m;
129 register struct ipxpcb *ipxp;
130 struct ipx_ifaddr *ia;
131 register int i;
132 int len, s;
133 char oddshortpacket = 0;
134
135 next:
136 /*
137 * Get next datagram off input queue and get IPX header
138 * in first mbuf.
139 */
140 s = splimp();
141 IF_DEQUEUE(&ipxintrq, m);
142 splx(s);
143 if (m == NULL)
144 return;
145 /*
146 * If no IPX addresses have been set yet but the interfaces
147 * are receiving, can't do anything with incoming packets yet.
148 */
149 if (ipx_ifaddr == NULL)
150 goto bad;
151
152 ipxstat.ipxs_total++;
153
154 if ((m->m_flags & M_EXT || m->m_len < sizeof(struct ipx)) &&
155 (m = m_pullup(m, sizeof(struct ipx))) == 0) {
156 ipxstat.ipxs_toosmall++;
157 goto next;
158 }
159
160 /*
161 * Give any raw listeners a crack at the packet
162 */
163 for (ipxp = ipxrawpcb.ipxp_next; ipxp != &ipxrawpcb;
164 ipxp = ipxp->ipxp_next) {
165 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
166 if (m1 != NULL)
167 ipx_input(m1, ipxp);
168 }
169
170 ipx = mtod(m, struct ipx *);
171 len = ntohs(ipx->ipx_len);
172 if ((len < m->m_pkthdr.len) && (oddshortpacket = len & 1)) {
173 /*
174 * If this packet is of odd length, and the length
175 * inside the header is less than the received packet
176 * length, preserve garbage byte for possible checksum.
177 */
178 len++;
179 }
180
181 /*
182 * Check that the amount of data in the buffers
183 * is as at least much as the IPX header would have us expect.
184 * Trim mbufs if longer than we expect.
185 * Drop packet if shorter than we expect.
186 */
187 if (m->m_pkthdr.len < len) {
188 ipxstat.ipxs_tooshort++;
189 goto bad;
190 }
191 if (m->m_pkthdr.len > len) {
192 if (m->m_len == m->m_pkthdr.len) {
193 m->m_len = len;
194 m->m_pkthdr.len = len;
195 } else
196 m_adj(m, len - m->m_pkthdr.len);
197 }
198 if (ipxcksum && ((i = ipx->ipx_sum) != 0xffff)) {
199 ipx->ipx_sum = 0;
200 if (i != (ipx->ipx_sum = ipx_cksum(m, len))) {
201 ipxstat.ipxs_badsum++;
202 goto bad;
203 }
204 }
205
206 /*
207 * Propagated (Netbios) packets (type 20) has to be handled
208 * different. :-(
209 */
210 if (ipx->ipx_pt == IPXPROTO_NETBIOS) {
211 if (ipxnetbios) {
212 ipx_output_type20(m);
213 goto next;
214 } else
215 goto bad;
216 }
217
218 /*
219 * Is this a directed broadcast?
220 */
221 if (ipx_hosteqnh(ipx_broadhost,ipx->ipx_dna.x_host)) {
222 if ((!ipx_neteq(ipx->ipx_dna, ipx->ipx_sna)) &&
223 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_broadnet)) &&
224 (!ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet)) &&
225 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)) ) {
226 /*
227 * If it is a broadcast to the net where it was
228 * received from, treat it as ours.
229 */
230 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
231 if((ia->ia_ifa.ifa_ifp == m->m_pkthdr.rcvif) &&
232 ipx_neteq(ia->ia_addr.sipx_addr,
233 ipx->ipx_dna))
234 goto ours;
235
236 /*
237 * Look to see if I need to eat this packet.
238 * Algorithm is to forward all young packets
239 * and prematurely age any packets which will
240 * by physically broadcasted.
241 * Any very old packets eaten without forwarding
242 * would die anyway.
243 *
244 * Suggestion of Bill Nesheim, Cornell U.
245 */
246 if (ipx->ipx_tc < IPX_MAXHOPS) {
247 ipx_forward(m);
248 goto next;
249 }
250 }
251 /*
252 * Is this our packet? If not, forward.
253 */
254 } else {
255 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
256 if (ipx_hosteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) &&
257 (ipx_neteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) ||
258 ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)))
259 break;
260
261 if (ia == NULL) {
262 ipx_forward(m);
263 goto next;
264 }
265 }
266 ours:
267 /*
268 * Locate pcb for datagram.
269 */
270 ipxp = ipx_pcblookup(&ipx->ipx_sna, ipx->ipx_dna.x_port, IPX_WILDCARD);
271 /*
272 * Switch out to protocol's input routine.
273 */
274 if (ipxp != NULL) {
275 if (oddshortpacket) {
276 m_adj(m, -1);
277 }
278 ipxstat.ipxs_delivered++;
279 if ((ipxp->ipxp_flags & IPXP_ALL_PACKETS) == 0)
280 switch (ipx->ipx_pt) {
281
282 case IPXPROTO_SPX:
283 spx_input(m, ipxp);
284 goto next;
285 }
286 ipx_input(m, ipxp);
287 } else
288 goto bad;
289
290 goto next;
291
292 bad:
293 m_freem(m);
294 goto next;
295 }
296
297 void
298 ipx_ctlinput(cmd, arg_as_sa, dummy)
299 int cmd;
300 struct sockaddr *arg_as_sa; /* XXX should be swapped with dummy */
301 void *dummy;
302 {
303 caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
304 struct ipx_addr *ipx;
305
306 if (cmd < 0 || cmd > PRC_NCMDS)
307 return;
308 switch (cmd) {
309 struct sockaddr_ipx *sipx;
310
311 case PRC_IFDOWN:
312 case PRC_HOSTDEAD:
313 case PRC_HOSTUNREACH:
314 sipx = (struct sockaddr_ipx *)arg;
315 if (sipx->sipx_family != AF_IPX)
316 return;
317 ipx = &sipx->sipx_addr;
318 break;
319
320 default:
321 if (ipxprintfs)
322 printf("ipx_ctlinput: cmd %d.\n", cmd);
323 break;
324 }
325 }
326
327 /*
328 * Forward a packet. If some error occurs drop the packet. IPX don't
329 * have a way to return errors to the sender.
330 */
331
332 struct route ipx_droute;
333 struct route ipx_sroute;
334
335 static void
336 ipx_forward(m)
337 struct mbuf *m;
338 {
339 register struct ipx *ipx = mtod(m, struct ipx *);
340 register int error;
341 struct mbuf *mcopy = NULL;
342 int agedelta = 1;
343 int flags = IPX_FORWARDING;
344 int ok_there = 0;
345 int ok_back = 0;
346
347 if (ipxforwarding == 0) {
348 /* can't tell difference between net and host */
349 ipxstat.ipxs_cantforward++;
350 m_freem(m);
351 goto cleanup;
352 }
353 ipx->ipx_tc++;
354 if (ipx->ipx_tc > IPX_MAXHOPS) {
355 ipxstat.ipxs_cantforward++;
356 m_freem(m);
357 goto cleanup;
358 }
359
360 if ((ok_there = ipx_do_route(&ipx->ipx_dna,&ipx_droute)) == 0) {
361 ipxstat.ipxs_noroute++;
362 m_freem(m);
363 goto cleanup;
364 }
365 /*
366 * Here we think about forwarding broadcast packets,
367 * so we try to insure that it doesn't go back out
368 * on the interface it came in on. Also, if we
369 * are going to physically broadcast this, let us
370 * age the packet so we can eat it safely the second time around.
371 */
372 if (ipx->ipx_dna.x_host.c_host[0] & 0x1) {
373 struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
374 struct ifnet *ifp;
375 if (ia != NULL) {
376 /* I'm gonna hafta eat this packet */
377 agedelta += IPX_MAXHOPS - ipx->ipx_tc;
378 ipx->ipx_tc = IPX_MAXHOPS;
379 }
380 if ((ok_back = ipx_do_route(&ipx->ipx_sna,&ipx_sroute)) == 0) {
381 /* error = ENETUNREACH; He'll never get it! */
382 ipxstat.ipxs_noroute++;
383 m_freem(m);
384 goto cleanup;
385 }
386 if (ipx_droute.ro_rt &&
387 (ifp = ipx_droute.ro_rt->rt_ifp) &&
388 ipx_sroute.ro_rt &&
389 (ifp != ipx_sroute.ro_rt->rt_ifp)) {
390 flags |= IPX_ALLOWBROADCAST;
391 } else {
392 ipxstat.ipxs_noroute++;
393 m_freem(m);
394 goto cleanup;
395 }
396 }
397 /* XXX
398 * I think the checksum generation is bogus. According to the NLSP
399 * spec the ipx_tc (hop count) field and the ipx_sum should be
400 * zero'ed before generating the checksum, ie. it should not be
401 * necesary to recompute it in the forwarding function.
402 */
403 /* need to adjust checksum */
404 if (ipxcksum && ipx->ipx_sum != 0xffff) {
405 union bytes {
406 u_char c[4];
407 u_short s[2];
408 long l;
409 } x;
410 register int shift;
411 x.l = 0;
412 x.c[0] = agedelta;
413 shift = (((((int)ntohs(ipx->ipx_len)) + 1) >> 1) - 2) & 0xf;
414 x.l = ipx->ipx_sum + (x.s[0] << shift);
415 x.l = x.s[0] + x.s[1];
416 x.l = x.s[0] + x.s[1];
417 if (x.l == 0xffff)
418 ipx->ipx_sum = 0;
419 else
420 ipx->ipx_sum = x.l;
421 } else
422 ipx->ipx_sum = 0xffff;
423
424 error = ipx_outputfl(m, &ipx_droute, flags);
425 if (error == 0) {
426 ipxstat.ipxs_forward++;
427
428 if (ipxprintfs) {
429 printf("forward: ");
430 ipx_printhost(&ipx->ipx_sna);
431 printf(" to ");
432 ipx_printhost(&ipx->ipx_dna);
433 printf(" hops %d\n", ipx->ipx_tc);
434 }
435 } else if (mcopy != NULL) {
436 ipx = mtod(mcopy, struct ipx *);
437 switch (error) {
438
439 case ENETUNREACH:
440 case EHOSTDOWN:
441 case EHOSTUNREACH:
442 case ENETDOWN:
443 case EPERM:
444 ipxstat.ipxs_noroute++;
445 break;
446
447 case EMSGSIZE:
448 ipxstat.ipxs_mtutoosmall++;
449 break;
450
451 case ENOBUFS:
452 ipxstat.ipxs_odropped++;
453 break;
454 }
455 mcopy = NULL;
456 m_freem(m);
457 }
458 cleanup:
459 if (ok_there)
460 ipx_undo_route(&ipx_droute);
461 if (ok_back)
462 ipx_undo_route(&ipx_sroute);
463 if (mcopy != NULL)
464 m_freem(mcopy);
465 }
466
467 static int
468 ipx_do_route(src, ro)
469 struct ipx_addr *src;
470 struct route *ro;
471 {
472 struct sockaddr_ipx *dst;
473
474 bzero((caddr_t)ro, sizeof(*ro));
475 dst = (struct sockaddr_ipx *)&ro->ro_dst;
476
477 dst->sipx_len = sizeof(*dst);
478 dst->sipx_family = AF_IPX;
479 dst->sipx_addr = *src;
480 dst->sipx_addr.x_port = 0;
481 rtalloc(ro);
482 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
483 return (0);
484 }
485 ro->ro_rt->rt_use++;
486 return (1);
487 }
488
489 static void
490 ipx_undo_route(ro)
491 register struct route *ro;
492 {
493 if (ro->ro_rt != NULL) {
494 RTFREE(ro->ro_rt);
495 }
496 }
497
498 void
499 ipx_watch_output(m, ifp)
500 struct mbuf *m;
501 struct ifnet *ifp;
502 {
503 register struct ipxpcb *ipxp;
504 register struct ifaddr *ifa;
505 register struct ipx_ifaddr *ia;
506 /*
507 * Give any raw listeners a crack at the packet
508 */
509 for (ipxp = ipxrawpcb.ipxp_next; ipxp != &ipxrawpcb;
510 ipxp = ipxp->ipxp_next) {
511 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
512 if (m0 != NULL) {
513 register struct ipx *ipx;
514
515 M_PREPEND(m0, sizeof(*ipx), M_DONTWAIT);
516 if (m0 == NULL)
517 continue;
518 ipx = mtod(m0, struct ipx *);
519 ipx->ipx_sna.x_net = ipx_zeronet;
520 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
521 if (ifp == ia->ia_ifp)
522 break;
523 if (ia == NULL)
524 ipx->ipx_sna.x_host = ipx_zerohost;
525 else
526 ipx->ipx_sna.x_host =
527 ia->ia_addr.sipx_addr.x_host;
528
529 if (ifp != NULL && (ifp->if_flags & IFF_POINTOPOINT))
530 for(ifa = ifp->if_addrlist; ifa != NULL;
531 ifa = ifa->ifa_next) {
532 if (ifa->ifa_addr->sa_family == AF_IPX) {
533 ipx->ipx_sna = IA_SIPX(ifa)->sipx_addr;
534 break;
535 }
536 }
537 ipx->ipx_len = ntohl(m0->m_pkthdr.len);
538 ipx_input(m0, ipxp);
539 }
540 }
541 }
Cache object: acb71fd0f3490cbcc9dd1cfa319d2236
|