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$
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 #include <machine/random.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 struct ifqueue ipxintrq;
90 static int ipxqmaxlen = IFQ_MAXLEN;
91
92 long ipx_pexseq;
93
94 NETISR_SET(NETISR_IPX, ipxintr);
95
96 static int ipx_do_route(struct ipx_addr *src, struct route *ro);
97 static void ipx_undo_route(struct route *ro);
98 static void ipx_forward(struct mbuf *m);
99
100 /*
101 * IPX initialization.
102 */
103
104 void
105 ipx_init()
106 {
107 ipx_broadnet = *(union ipx_net *)allones;
108 ipx_broadhost = *(union ipx_host *)allones;
109
110 read_random(&ipx_pexseq, sizeof ipx_pexseq);
111 ipxintrq.ifq_maxlen = ipxqmaxlen;
112 ipxpcb.ipxp_next = ipxpcb.ipxp_prev = &ipxpcb;
113 ipxrawpcb.ipxp_next = ipxrawpcb.ipxp_prev = &ipxrawpcb;
114
115 ipx_netmask.sipx_len = 6;
116 ipx_netmask.sipx_addr.x_net = ipx_broadnet;
117
118 ipx_hostmask.sipx_len = 12;
119 ipx_hostmask.sipx_addr.x_net = ipx_broadnet;
120 ipx_hostmask.sipx_addr.x_host = ipx_broadhost;
121 }
122
123 /*
124 * IPX input routine. Pass to next level.
125 */
126 void
127 ipxintr()
128 {
129 register struct ipx *ipx;
130 register struct mbuf *m;
131 register struct ipxpcb *ipxp;
132 struct ipx_ifaddr *ia;
133 int len, s;
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 /*
173 * Check that the amount of data in the buffers
174 * is as at least much as the IPX header would have us expect.
175 * Trim mbufs if longer than we expect.
176 * Drop packet if shorter than we expect.
177 */
178 if (m->m_pkthdr.len < len) {
179 ipxstat.ipxs_tooshort++;
180 goto bad;
181 }
182 if (m->m_pkthdr.len > len) {
183 if (m->m_len == m->m_pkthdr.len) {
184 m->m_len = len;
185 m->m_pkthdr.len = len;
186 } else
187 m_adj(m, len - m->m_pkthdr.len);
188 }
189 if (ipxcksum && ipx->ipx_sum != 0xffff) {
190 if (ipx->ipx_sum != ipx_cksum(m, len)) {
191 ipxstat.ipxs_badsum++;
192 goto bad;
193 }
194 }
195
196 /*
197 * Propagated (Netbios) packets (type 20) has to be handled
198 * different. :-(
199 */
200 if (ipx->ipx_pt == IPXPROTO_NETBIOS) {
201 if (ipxnetbios) {
202 ipx_output_type20(m);
203 goto next;
204 } else
205 goto bad;
206 }
207
208 /*
209 * Is this a directed broadcast?
210 */
211 if (ipx_hosteqnh(ipx_broadhost,ipx->ipx_dna.x_host)) {
212 if ((!ipx_neteq(ipx->ipx_dna, ipx->ipx_sna)) &&
213 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_broadnet)) &&
214 (!ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet)) &&
215 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)) ) {
216 /*
217 * If it is a broadcast to the net where it was
218 * received from, treat it as ours.
219 */
220 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
221 if((ia->ia_ifa.ifa_ifp == m->m_pkthdr.rcvif) &&
222 ipx_neteq(ia->ia_addr.sipx_addr,
223 ipx->ipx_dna))
224 goto ours;
225
226 /*
227 * Look to see if I need to eat this packet.
228 * Algorithm is to forward all young packets
229 * and prematurely age any packets which will
230 * by physically broadcasted.
231 * Any very old packets eaten without forwarding
232 * would die anyway.
233 *
234 * Suggestion of Bill Nesheim, Cornell U.
235 */
236 if (ipx->ipx_tc < IPX_MAXHOPS) {
237 ipx_forward(m);
238 goto next;
239 }
240 }
241 /*
242 * Is this our packet? If not, forward.
243 */
244 } else {
245 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
246 if (ipx_hosteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) &&
247 (ipx_neteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) ||
248 ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)))
249 break;
250
251 if (ia == NULL) {
252 ipx_forward(m);
253 goto next;
254 }
255 }
256 ours:
257 /*
258 * Locate pcb for datagram.
259 */
260 ipxp = ipx_pcblookup(&ipx->ipx_sna, ipx->ipx_dna.x_port, IPX_WILDCARD);
261 /*
262 * Switch out to protocol's input routine.
263 */
264 if (ipxp != NULL) {
265 ipxstat.ipxs_delivered++;
266 if ((ipxp->ipxp_flags & IPXP_ALL_PACKETS) == 0)
267 switch (ipx->ipx_pt) {
268
269 case IPXPROTO_SPX:
270 spx_input(m, ipxp);
271 goto next;
272 }
273 ipx_input(m, ipxp);
274 } else
275 goto bad;
276
277 goto next;
278
279 bad:
280 m_freem(m);
281 goto next;
282 }
283
284 void
285 ipx_ctlinput(cmd, arg_as_sa, dummy)
286 int cmd;
287 struct sockaddr *arg_as_sa; /* XXX should be swapped with dummy */
288 void *dummy;
289 {
290 caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
291 struct ipx_addr *ipx;
292
293 if (cmd < 0 || cmd > PRC_NCMDS)
294 return;
295 switch (cmd) {
296 struct sockaddr_ipx *sipx;
297
298 case PRC_IFDOWN:
299 case PRC_HOSTDEAD:
300 case PRC_HOSTUNREACH:
301 sipx = (struct sockaddr_ipx *)arg;
302 if (sipx->sipx_family != AF_IPX)
303 return;
304 ipx = &sipx->sipx_addr;
305 break;
306
307 default:
308 if (ipxprintfs)
309 printf("ipx_ctlinput: cmd %d.\n", cmd);
310 break;
311 }
312 }
313
314 /*
315 * Forward a packet. If some error occurs drop the packet. IPX don't
316 * have a way to return errors to the sender.
317 */
318
319 static struct route ipx_droute;
320 static struct route ipx_sroute;
321
322 static void
323 ipx_forward(m)
324 struct mbuf *m;
325 {
326 register struct ipx *ipx = mtod(m, struct ipx *);
327 register int error;
328 struct mbuf *mcopy = NULL;
329 int agedelta = 1;
330 int flags = IPX_FORWARDING;
331 int ok_there = 0;
332 int ok_back = 0;
333
334 if (ipxforwarding == 0) {
335 /* can't tell difference between net and host */
336 ipxstat.ipxs_cantforward++;
337 m_freem(m);
338 goto cleanup;
339 }
340 ipx->ipx_tc++;
341 if (ipx->ipx_tc > IPX_MAXHOPS) {
342 ipxstat.ipxs_cantforward++;
343 m_freem(m);
344 goto cleanup;
345 }
346
347 if ((ok_there = ipx_do_route(&ipx->ipx_dna,&ipx_droute)) == 0) {
348 ipxstat.ipxs_noroute++;
349 m_freem(m);
350 goto cleanup;
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 (ipx->ipx_dna.x_host.c_host[0] & 0x1) {
360 struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
361 struct ifnet *ifp;
362 if (ia != NULL) {
363 /* I'm gonna hafta eat this packet */
364 agedelta += IPX_MAXHOPS - ipx->ipx_tc;
365 ipx->ipx_tc = IPX_MAXHOPS;
366 }
367 if ((ok_back = ipx_do_route(&ipx->ipx_sna,&ipx_sroute)) == 0) {
368 /* error = ENETUNREACH; He'll never get it! */
369 ipxstat.ipxs_noroute++;
370 m_freem(m);
371 goto cleanup;
372 }
373 if (ipx_droute.ro_rt &&
374 (ifp = ipx_droute.ro_rt->rt_ifp) &&
375 ipx_sroute.ro_rt &&
376 (ifp != ipx_sroute.ro_rt->rt_ifp)) {
377 flags |= IPX_ALLOWBROADCAST;
378 } else {
379 ipxstat.ipxs_noroute++;
380 m_freem(m);
381 goto cleanup;
382 }
383 }
384 /*
385 * We don't need to recompute checksum because ipx_tc field
386 * is ignored by checksum calculation routine, however
387 * it may be desirable to reset checksum if ipxcksum == 0
388 */
389 #if 0
390 if (!ipxcksum)
391 ipx->ipx_sum = 0xffff;
392 #endif
393
394 error = ipx_outputfl(m, &ipx_droute, flags);
395 if (error == 0) {
396 ipxstat.ipxs_forward++;
397
398 if (ipxprintfs) {
399 printf("forward: ");
400 ipx_printhost(&ipx->ipx_sna);
401 printf(" to ");
402 ipx_printhost(&ipx->ipx_dna);
403 printf(" hops %d\n", ipx->ipx_tc);
404 }
405 } else if (mcopy != NULL) {
406 ipx = mtod(mcopy, struct ipx *);
407 switch (error) {
408
409 case ENETUNREACH:
410 case EHOSTDOWN:
411 case EHOSTUNREACH:
412 case ENETDOWN:
413 case EPERM:
414 ipxstat.ipxs_noroute++;
415 break;
416
417 case EMSGSIZE:
418 ipxstat.ipxs_mtutoosmall++;
419 break;
420
421 case ENOBUFS:
422 ipxstat.ipxs_odropped++;
423 break;
424 }
425 mcopy = NULL;
426 m_freem(m);
427 }
428 cleanup:
429 if (ok_there)
430 ipx_undo_route(&ipx_droute);
431 if (ok_back)
432 ipx_undo_route(&ipx_sroute);
433 if (mcopy != NULL)
434 m_freem(mcopy);
435 }
436
437 static int
438 ipx_do_route(src, ro)
439 struct ipx_addr *src;
440 struct route *ro;
441 {
442 struct sockaddr_ipx *dst;
443
444 bzero((caddr_t)ro, sizeof(*ro));
445 dst = (struct sockaddr_ipx *)&ro->ro_dst;
446
447 dst->sipx_len = sizeof(*dst);
448 dst->sipx_family = AF_IPX;
449 dst->sipx_addr = *src;
450 dst->sipx_addr.x_port = 0;
451 rtalloc(ro);
452 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
453 return (0);
454 }
455 ro->ro_rt->rt_use++;
456 return (1);
457 }
458
459 static void
460 ipx_undo_route(ro)
461 register struct route *ro;
462 {
463 if (ro->ro_rt != NULL) {
464 RTFREE(ro->ro_rt);
465 }
466 }
467
468 void
469 ipx_watch_output(m, ifp)
470 struct mbuf *m;
471 struct ifnet *ifp;
472 {
473 register struct ipxpcb *ipxp;
474 register struct ifaddr *ifa;
475 register struct ipx_ifaddr *ia;
476 /*
477 * Give any raw listeners a crack at the packet
478 */
479 for (ipxp = ipxrawpcb.ipxp_next; ipxp != &ipxrawpcb;
480 ipxp = ipxp->ipxp_next) {
481 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
482 if (m0 != NULL) {
483 register struct ipx *ipx;
484
485 M_PREPEND(m0, sizeof(*ipx), M_DONTWAIT);
486 if (m0 == NULL)
487 continue;
488 ipx = mtod(m0, struct ipx *);
489 ipx->ipx_sna.x_net = ipx_zeronet;
490 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
491 if (ifp == ia->ia_ifp)
492 break;
493 if (ia == NULL)
494 ipx->ipx_sna.x_host = ipx_zerohost;
495 else
496 ipx->ipx_sna.x_host =
497 ia->ia_addr.sipx_addr.x_host;
498
499 if (ifp != NULL && (ifp->if_flags & IFF_POINTOPOINT))
500 for(ifa = ifp->if_addrhead.tqh_first; ifa != NULL;
501 ifa = ifa->ifa_link.tqe_next) {
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: 5e6bfd81e9278346c9c98afd50876719
|