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: releng/5.0/sys/netipx/ipx_input.c 93818 2002-04-04 21:03:38Z jhb $
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/random.h>
46 #include <sys/sysctl.h>
47
48 #include <net/if.h>
49 #include <net/route.h>
50 #include <net/netisr.h>
51 #include <net/intrq.h>
52
53 #include <netipx/ipx.h>
54 #include <netipx/spx.h>
55 #include <netipx/ipx_if.h>
56 #include <netipx/ipx_pcb.h>
57 #include <netipx/ipx_var.h>
58
59 int ipxcksum = 0;
60 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, checksum, CTLFLAG_RW,
61 &ipxcksum, 0, "");
62
63 static int ipxprintfs = 0; /* printing forwarding information */
64 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxprintfs, CTLFLAG_RW,
65 &ipxprintfs, 0, "");
66
67 static int ipxforwarding = 0;
68 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxforwarding, CTLFLAG_RW,
69 &ipxforwarding, 0, "");
70
71 static int ipxnetbios = 0;
72 SYSCTL_INT(_net_ipx, OID_AUTO, ipxnetbios, CTLFLAG_RW,
73 &ipxnetbios, 0, "");
74
75 union ipx_net ipx_zeronet;
76 union ipx_host ipx_zerohost;
77
78 union ipx_net ipx_broadnet;
79 union ipx_host ipx_broadhost;
80
81 struct ipxstat ipxstat;
82 struct sockaddr_ipx ipx_netmask, ipx_hostmask;
83
84 static u_short allones[] = {-1, -1, -1};
85
86 struct ipxpcb ipxpcb;
87 struct ipxpcb ipxrawpcb;
88
89 static int ipxqmaxlen = IFQ_MAXLEN;
90
91 long ipx_pexseq;
92
93 static int ipx_do_route(struct ipx_addr *src, struct route *ro);
94 static void ipx_undo_route(struct route *ro);
95 static void ipx_forward(struct mbuf *m);
96
97 /*
98 * IPX initialization.
99 */
100
101 void
102 ipx_init()
103 {
104 ipx_broadnet = *(union ipx_net *)allones;
105 ipx_broadhost = *(union ipx_host *)allones;
106
107 read_random(&ipx_pexseq, sizeof ipx_pexseq);
108 ipxintrq.ifq_maxlen = ipxqmaxlen;
109 mtx_init(&ipxintrq.ifq_mtx, "ipx_inq", NULL, MTX_DEF);
110 ipxintrq_present = 1;
111 ipxpcb.ipxp_next = ipxpcb.ipxp_prev = &ipxpcb;
112 ipxrawpcb.ipxp_next = ipxrawpcb.ipxp_prev = &ipxrawpcb;
113
114 ipx_netmask.sipx_len = 6;
115 ipx_netmask.sipx_addr.x_net = ipx_broadnet;
116
117 ipx_hostmask.sipx_len = 12;
118 ipx_hostmask.sipx_addr.x_net = ipx_broadnet;
119 ipx_hostmask.sipx_addr.x_host = ipx_broadhost;
120
121 register_netisr(NETISR_IPX, ipxintr);
122 }
123
124 /*
125 * IPX input routine. Pass to next level.
126 */
127 void
128 ipxintr()
129 {
130 register struct ipx *ipx;
131 register struct mbuf *m;
132 register struct ipxpcb *ipxp;
133 struct ipx_ifaddr *ia;
134 int len, s;
135
136 next:
137 /*
138 * Get next datagram off input queue and get IPX header
139 * in first mbuf.
140 */
141 s = splimp();
142 IF_DEQUEUE(&ipxintrq, m);
143 splx(s);
144 if (m == NULL)
145 return;
146 /*
147 * If no IPX addresses have been set yet but the interfaces
148 * are receiving, can't do anything with incoming packets yet.
149 */
150 if (ipx_ifaddr == NULL)
151 goto bad;
152
153 ipxstat.ipxs_total++;
154
155 if ((m->m_flags & M_EXT || m->m_len < sizeof(struct ipx)) &&
156 (m = m_pullup(m, sizeof(struct ipx))) == 0) {
157 ipxstat.ipxs_toosmall++;
158 goto next;
159 }
160
161 /*
162 * Give any raw listeners a crack at the packet
163 */
164 for (ipxp = ipxrawpcb.ipxp_next; ipxp != &ipxrawpcb;
165 ipxp = ipxp->ipxp_next) {
166 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
167 if (m1 != NULL)
168 ipx_input(m1, ipxp);
169 }
170
171 ipx = mtod(m, struct ipx *);
172 len = ntohs(ipx->ipx_len);
173 /*
174 * Check that the amount of data in the buffers
175 * is as at least much as the IPX header would have us expect.
176 * Trim mbufs if longer than we expect.
177 * Drop packet if shorter than we expect.
178 */
179 if (m->m_pkthdr.len < len) {
180 ipxstat.ipxs_tooshort++;
181 goto bad;
182 }
183 if (m->m_pkthdr.len > len) {
184 if (m->m_len == m->m_pkthdr.len) {
185 m->m_len = len;
186 m->m_pkthdr.len = len;
187 } else
188 m_adj(m, len - m->m_pkthdr.len);
189 }
190 if (ipxcksum && ipx->ipx_sum != 0xffff) {
191 if (ipx->ipx_sum != ipx_cksum(m, len)) {
192 ipxstat.ipxs_badsum++;
193 goto bad;
194 }
195 }
196
197 /*
198 * Propagated (Netbios) packets (type 20) has to be handled
199 * different. :-(
200 */
201 if (ipx->ipx_pt == IPXPROTO_NETBIOS) {
202 if (ipxnetbios) {
203 ipx_output_type20(m);
204 goto next;
205 } else
206 goto bad;
207 }
208
209 /*
210 * Is this a directed broadcast?
211 */
212 if (ipx_hosteqnh(ipx_broadhost,ipx->ipx_dna.x_host)) {
213 if ((!ipx_neteq(ipx->ipx_dna, ipx->ipx_sna)) &&
214 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_broadnet)) &&
215 (!ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet)) &&
216 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)) ) {
217 /*
218 * If it is a broadcast to the net where it was
219 * received from, treat it as ours.
220 */
221 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
222 if((ia->ia_ifa.ifa_ifp == m->m_pkthdr.rcvif) &&
223 ipx_neteq(ia->ia_addr.sipx_addr,
224 ipx->ipx_dna))
225 goto ours;
226
227 /*
228 * Look to see if I need to eat this packet.
229 * Algorithm is to forward all young packets
230 * and prematurely age any packets which will
231 * by physically broadcasted.
232 * Any very old packets eaten without forwarding
233 * would die anyway.
234 *
235 * Suggestion of Bill Nesheim, Cornell U.
236 */
237 if (ipx->ipx_tc < IPX_MAXHOPS) {
238 ipx_forward(m);
239 goto next;
240 }
241 }
242 /*
243 * Is this our packet? If not, forward.
244 */
245 } else {
246 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
247 if (ipx_hosteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) &&
248 (ipx_neteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) ||
249 ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)))
250 break;
251
252 if (ia == NULL) {
253 ipx_forward(m);
254 goto next;
255 }
256 }
257 ours:
258 /*
259 * Locate pcb for datagram.
260 */
261 ipxp = ipx_pcblookup(&ipx->ipx_sna, ipx->ipx_dna.x_port, IPX_WILDCARD);
262 /*
263 * Switch out to protocol's input routine.
264 */
265 if (ipxp != NULL) {
266 ipxstat.ipxs_delivered++;
267 if ((ipxp->ipxp_flags & IPXP_ALL_PACKETS) == 0)
268 switch (ipx->ipx_pt) {
269
270 case IPXPROTO_SPX:
271 spx_input(m, ipxp);
272 goto next;
273 }
274 ipx_input(m, ipxp);
275 } else
276 goto bad;
277
278 goto next;
279
280 bad:
281 m_freem(m);
282 goto next;
283 }
284
285 void
286 ipx_ctlinput(cmd, arg_as_sa, dummy)
287 int cmd;
288 struct sockaddr *arg_as_sa; /* XXX should be swapped with dummy */
289 void *dummy;
290 {
291 caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
292 struct ipx_addr *ipx;
293
294 if (cmd < 0 || cmd > PRC_NCMDS)
295 return;
296 switch (cmd) {
297 struct sockaddr_ipx *sipx;
298
299 case PRC_IFDOWN:
300 case PRC_HOSTDEAD:
301 case PRC_HOSTUNREACH:
302 sipx = (struct sockaddr_ipx *)arg;
303 if (sipx->sipx_family != AF_IPX)
304 return;
305 ipx = &sipx->sipx_addr;
306 break;
307
308 default:
309 if (ipxprintfs)
310 printf("ipx_ctlinput: cmd %d.\n", cmd);
311 break;
312 }
313 }
314
315 /*
316 * Forward a packet. If some error occurs drop the packet. IPX don't
317 * have a way to return errors to the sender.
318 */
319
320 static struct route ipx_droute;
321 static struct route ipx_sroute;
322
323 static void
324 ipx_forward(m)
325 struct mbuf *m;
326 {
327 register struct ipx *ipx = mtod(m, struct ipx *);
328 register int error;
329 struct mbuf *mcopy = NULL;
330 int agedelta = 1;
331 int flags = IPX_FORWARDING;
332 int ok_there = 0;
333 int ok_back = 0;
334
335 if (ipxforwarding == 0) {
336 /* can't tell difference between net and host */
337 ipxstat.ipxs_cantforward++;
338 m_freem(m);
339 goto cleanup;
340 }
341 ipx->ipx_tc++;
342 if (ipx->ipx_tc > IPX_MAXHOPS) {
343 ipxstat.ipxs_cantforward++;
344 m_freem(m);
345 goto cleanup;
346 }
347
348 if ((ok_there = ipx_do_route(&ipx->ipx_dna,&ipx_droute)) == 0) {
349 ipxstat.ipxs_noroute++;
350 m_freem(m);
351 goto cleanup;
352 }
353 /*
354 * Here we think about forwarding broadcast packets,
355 * so we try to insure that it doesn't go back out
356 * on the interface it came in on. Also, if we
357 * are going to physically broadcast this, let us
358 * age the packet so we can eat it safely the second time around.
359 */
360 if (ipx->ipx_dna.x_host.c_host[0] & 0x1) {
361 struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
362 struct ifnet *ifp;
363 if (ia != NULL) {
364 /* I'm gonna hafta eat this packet */
365 agedelta += IPX_MAXHOPS - ipx->ipx_tc;
366 ipx->ipx_tc = IPX_MAXHOPS;
367 }
368 if ((ok_back = ipx_do_route(&ipx->ipx_sna,&ipx_sroute)) == 0) {
369 /* error = ENETUNREACH; He'll never get it! */
370 ipxstat.ipxs_noroute++;
371 m_freem(m);
372 goto cleanup;
373 }
374 if (ipx_droute.ro_rt &&
375 (ifp = ipx_droute.ro_rt->rt_ifp) &&
376 ipx_sroute.ro_rt &&
377 (ifp != ipx_sroute.ro_rt->rt_ifp)) {
378 flags |= IPX_ALLOWBROADCAST;
379 } else {
380 ipxstat.ipxs_noroute++;
381 m_freem(m);
382 goto cleanup;
383 }
384 }
385 /*
386 * We don't need to recompute checksum because ipx_tc field
387 * is ignored by checksum calculation routine, however
388 * it may be desirable to reset checksum if ipxcksum == 0
389 */
390 #if 0
391 if (!ipxcksum)
392 ipx->ipx_sum = 0xffff;
393 #endif
394
395 error = ipx_outputfl(m, &ipx_droute, flags);
396 if (error == 0) {
397 ipxstat.ipxs_forward++;
398
399 if (ipxprintfs) {
400 printf("forward: ");
401 ipx_printhost(&ipx->ipx_sna);
402 printf(" to ");
403 ipx_printhost(&ipx->ipx_dna);
404 printf(" hops %d\n", ipx->ipx_tc);
405 }
406 } else if (mcopy != NULL) {
407 ipx = mtod(mcopy, struct ipx *);
408 switch (error) {
409
410 case ENETUNREACH:
411 case EHOSTDOWN:
412 case EHOSTUNREACH:
413 case ENETDOWN:
414 case EPERM:
415 ipxstat.ipxs_noroute++;
416 break;
417
418 case EMSGSIZE:
419 ipxstat.ipxs_mtutoosmall++;
420 break;
421
422 case ENOBUFS:
423 ipxstat.ipxs_odropped++;
424 break;
425 }
426 mcopy = NULL;
427 m_freem(m);
428 }
429 cleanup:
430 if (ok_there)
431 ipx_undo_route(&ipx_droute);
432 if (ok_back)
433 ipx_undo_route(&ipx_sroute);
434 if (mcopy != NULL)
435 m_freem(mcopy);
436 }
437
438 static int
439 ipx_do_route(src, ro)
440 struct ipx_addr *src;
441 struct route *ro;
442 {
443 struct sockaddr_ipx *dst;
444
445 bzero((caddr_t)ro, sizeof(*ro));
446 dst = (struct sockaddr_ipx *)&ro->ro_dst;
447
448 dst->sipx_len = sizeof(*dst);
449 dst->sipx_family = AF_IPX;
450 dst->sipx_addr = *src;
451 dst->sipx_addr.x_port = 0;
452 rtalloc(ro);
453 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
454 return (0);
455 }
456 ro->ro_rt->rt_use++;
457 return (1);
458 }
459
460 static void
461 ipx_undo_route(ro)
462 register struct route *ro;
463 {
464 if (ro->ro_rt != NULL) {
465 RTFREE(ro->ro_rt);
466 }
467 }
468
469 void
470 ipx_watch_output(m, ifp)
471 struct mbuf *m;
472 struct ifnet *ifp;
473 {
474 register struct ipxpcb *ipxp;
475 register struct ifaddr *ifa;
476 register struct ipx_ifaddr *ia;
477 /*
478 * Give any raw listeners a crack at the packet
479 */
480 for (ipxp = ipxrawpcb.ipxp_next; ipxp != &ipxrawpcb;
481 ipxp = ipxp->ipxp_next) {
482 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
483 if (m0 != NULL) {
484 register struct ipx *ipx;
485
486 M_PREPEND(m0, sizeof(*ipx), M_DONTWAIT);
487 if (m0 == NULL)
488 continue;
489 ipx = mtod(m0, struct ipx *);
490 ipx->ipx_sna.x_net = ipx_zeronet;
491 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
492 if (ifp == ia->ia_ifp)
493 break;
494 if (ia == NULL)
495 ipx->ipx_sna.x_host = ipx_zerohost;
496 else
497 ipx->ipx_sna.x_host =
498 ia->ia_addr.sipx_addr.x_host;
499
500 if (ifp != NULL && (ifp->if_flags & IFF_POINTOPOINT))
501 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
502 if (ifa->ifa_addr->sa_family == AF_IPX) {
503 ipx->ipx_sna = IA_SIPX(ifa)->sipx_addr;
504 break;
505 }
506 }
507 ipx->ipx_len = ntohl(m0->m_pkthdr.len);
508 ipx_input(m0, ipxp);
509 }
510 }
511 }
Cache object: 0d2dae3c8ee814ceae06e0e971fc76d9
|