MFC:
[dragonfly.git] / sys / netproto / ipx / ipx_input.c
bloba8699ce7259f745ffcd1e2c49bb0f1edb8e92131
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.
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.
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.
34 * @(#)ipx_input.c
36 * $FreeBSD: src/sys/netipx/ipx_input.c,v 1.22.2.2 2001/02/22 09:44:18 bp Exp $
37 * $DragonFly: src/sys/netproto/ipx/ipx_input.c,v 1.18 2008/03/07 11:34:21 sephe Exp $
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/kernel.h>
46 #include <sys/random.h>
47 #include <sys/sysctl.h>
49 #include <sys/thread2.h>
50 #include <sys/msgport2.h>
52 #include <net/if.h>
53 #include <net/route.h>
54 #include <net/netisr.h>
56 #include "ipx.h"
57 #include "spx.h"
58 #include "ipx_if.h"
59 #include "ipx_pcb.h"
60 #include "ipx_var.h"
62 int ipxcksum = 0;
63 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, checksum, CTLFLAG_RW,
64 &ipxcksum, 0, "");
66 static int ipxprintfs = 0; /* printing forwarding information */
67 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxprintfs, CTLFLAG_RW,
68 &ipxprintfs, 0, "");
70 static int ipxforwarding = 0;
71 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxforwarding, CTLFLAG_RW,
72 &ipxforwarding, 0, "");
74 static int ipxnetbios = 0;
75 SYSCTL_INT(_net_ipx, OID_AUTO, ipxnetbios, CTLFLAG_RW,
76 &ipxnetbios, 0, "");
78 union ipx_net ipx_zeronet;
79 union ipx_host ipx_zerohost;
81 union ipx_net ipx_broadnet;
82 union ipx_host ipx_broadhost;
84 struct ipxstat ipxstat;
85 struct sockaddr_ipx ipx_netmask, ipx_hostmask;
87 static u_short allones[] = {-1, -1, -1};
89 struct ipxpcb ipxpcb;
90 struct ipxpcb ipxrawpcb;
92 long ipx_pexseq;
94 static void ipxintr(struct netmsg *);
95 static int ipx_do_route(struct ipx_addr *src, struct route *ro);
96 static void ipx_undo_route(struct route *ro);
97 static void ipx_forward(struct mbuf *m);
100 * IPX initialization.
103 void
104 ipx_init(void)
106 ipx_broadnet = *(union ipx_net *)allones;
107 ipx_broadhost = *(union ipx_host *)allones;
109 read_random(&ipx_pexseq, sizeof ipx_pexseq);
110 ipxpcb.ipxp_next = ipxpcb.ipxp_prev = &ipxpcb;
111 ipxrawpcb.ipxp_next = ipxrawpcb.ipxp_prev = &ipxrawpcb;
113 ipx_netmask.sipx_len = 6;
114 ipx_netmask.sipx_addr.x_net = ipx_broadnet;
116 ipx_hostmask.sipx_len = 12;
117 ipx_hostmask.sipx_addr.x_net = ipx_broadnet;
118 ipx_hostmask.sipx_addr.x_host = ipx_broadhost;
120 netisr_register(NETISR_IPX, cpu0_portfn, ipxintr);
124 * IPX input routine. Pass to next level.
126 static void
127 ipxintr(struct netmsg *msg)
129 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
130 struct ipx *ipx;
131 struct ipxpcb *ipxp;
132 struct ipx_ifaddr *ia;
133 int len;
136 * If no IPX addresses have been set yet but the interfaces
137 * are receiving, can't do anything with incoming packets yet.
139 if (ipx_ifaddr == NULL)
140 goto bad;
142 ipxstat.ipxs_total++;
144 if ((m->m_flags & M_EXT || m->m_len < sizeof(struct ipx)) &&
145 (m = m_pullup(m, sizeof(struct ipx))) == 0) {
146 ipxstat.ipxs_toosmall++;
147 goto out;
151 * Give any raw listeners a crack at the packet
153 for (ipxp = ipxrawpcb.ipxp_next; ipxp != &ipxrawpcb;
154 ipxp = ipxp->ipxp_next) {
155 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
156 if (m1 != NULL)
157 ipx_input(m1, ipxp);
160 ipx = mtod(m, struct ipx *);
161 len = ntohs(ipx->ipx_len);
163 * Check that the amount of data in the buffers
164 * is as at least much as the IPX header would have us expect.
165 * Trim mbufs if longer than we expect.
166 * Drop packet if shorter than we expect.
168 if (m->m_pkthdr.len < len) {
169 ipxstat.ipxs_tooshort++;
170 goto bad;
172 if (m->m_pkthdr.len > len) {
173 if (m->m_len == m->m_pkthdr.len) {
174 m->m_len = len;
175 m->m_pkthdr.len = len;
176 } else
177 m_adj(m, len - m->m_pkthdr.len);
179 if (ipxcksum && ipx->ipx_sum != 0xffff) {
180 if (ipx->ipx_sum != ipx_cksum(m, len)) {
181 ipxstat.ipxs_badsum++;
182 goto bad;
187 * Propagated (Netbios) packets (type 20) has to be handled
188 * different. :-(
190 if (ipx->ipx_pt == IPXPROTO_NETBIOS) {
191 if (ipxnetbios) {
192 ipx_output_type20(m);
193 goto out;
194 } else
195 goto bad;
199 * Is this a directed broadcast?
201 if (ipx_hosteqnh(ipx_broadhost,ipx->ipx_dna.x_host)) {
202 if ((!ipx_neteq(ipx->ipx_dna, ipx->ipx_sna)) &&
203 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_broadnet)) &&
204 (!ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet)) &&
205 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)) ) {
207 * If it is a broadcast to the net where it was
208 * received from, treat it as ours.
210 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
211 if((ia->ia_ifa.ifa_ifp == m->m_pkthdr.rcvif) &&
212 ipx_neteq(ia->ia_addr.sipx_addr,
213 ipx->ipx_dna))
214 goto ours;
217 * Look to see if I need to eat this packet.
218 * Algorithm is to forward all young packets
219 * and prematurely age any packets which will
220 * by physically broadcasted.
221 * Any very old packets eaten without forwarding
222 * would die anyway.
224 * Suggestion of Bill Nesheim, Cornell U.
226 if (ipx->ipx_tc < IPX_MAXHOPS) {
227 ipx_forward(m);
228 goto out;
232 * Is this our packet? If not, forward.
234 } else {
235 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
236 if (ipx_hosteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) &&
237 (ipx_neteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) ||
238 ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)))
239 break;
241 if (ia == NULL) {
242 ipx_forward(m);
243 goto out;
246 ours:
248 * Locate pcb for datagram.
250 ipxp = ipx_pcblookup(&ipx->ipx_sna, ipx->ipx_dna.x_port, IPX_WILDCARD);
252 * Switch out to protocol's input routine.
254 if (ipxp != NULL) {
255 ipxstat.ipxs_delivered++;
256 if ((ipxp->ipxp_flags & IPXP_ALL_PACKETS) == 0)
257 switch (ipx->ipx_pt) {
258 case IPXPROTO_SPX:
259 spx_input(m, ipxp);
260 goto out;
262 ipx_input(m, ipxp);
263 } else
264 goto bad;
266 goto out;
268 bad:
269 m_freem(m);
270 out:
272 /* msg was embedded in the mbuf, do not reply! */
276 * Parameters:
277 * arg_as_sa: XXX should be swapped with dummy
279 void
280 ipx_ctlinput(int cmd, struct sockaddr *arg_as_sa, void *dummy)
282 caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
283 struct ipx_addr *ipx;
285 if (cmd < 0 || cmd > PRC_NCMDS)
286 return;
287 switch (cmd) {
288 struct sockaddr_ipx *sipx;
290 case PRC_IFDOWN:
291 case PRC_HOSTDEAD:
292 case PRC_HOSTUNREACH:
293 sipx = (struct sockaddr_ipx *)arg;
294 if (sipx->sipx_family != AF_IPX)
295 return;
296 ipx = &sipx->sipx_addr;
297 break;
299 default:
300 if (ipxprintfs)
301 kprintf("ipx_ctlinput: cmd %d.\n", cmd);
302 break;
307 * Forward a packet. If some error occurs drop the packet. IPX don't
308 * have a way to return errors to the sender.
311 static struct route ipx_droute;
312 static struct route ipx_sroute;
314 static void
315 ipx_forward(struct mbuf *m)
317 struct ipx *ipx = mtod(m, struct ipx *);
318 int error;
319 struct mbuf *mcopy = NULL;
320 int agedelta = 1;
321 int flags = IPX_FORWARDING;
322 int ok_there = 0;
323 int ok_back = 0;
325 if (ipxforwarding == 0) {
326 /* can't tell difference between net and host */
327 ipxstat.ipxs_cantforward++;
328 m_freem(m);
329 goto cleanup;
331 ipx->ipx_tc++;
332 if (ipx->ipx_tc > IPX_MAXHOPS) {
333 ipxstat.ipxs_cantforward++;
334 m_freem(m);
335 goto cleanup;
338 if ((ok_there = ipx_do_route(&ipx->ipx_dna,&ipx_droute)) == 0) {
339 ipxstat.ipxs_noroute++;
340 m_freem(m);
341 goto cleanup;
344 * Here we think about forwarding broadcast packets,
345 * so we try to insure that it doesn't go back out
346 * on the interface it came in on. Also, if we
347 * are going to physically broadcast this, let us
348 * age the packet so we can eat it safely the second time around.
350 if (ipx->ipx_dna.x_host.c_host[0] & 0x1) {
351 struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
352 struct ifnet *ifp;
353 if (ia != NULL) {
354 /* I'm gonna hafta eat this packet */
355 agedelta += IPX_MAXHOPS - ipx->ipx_tc;
356 ipx->ipx_tc = IPX_MAXHOPS;
358 if ((ok_back = ipx_do_route(&ipx->ipx_sna,&ipx_sroute)) == 0) {
359 /* error = ENETUNREACH; He'll never get it! */
360 ipxstat.ipxs_noroute++;
361 m_freem(m);
362 goto cleanup;
364 if (ipx_droute.ro_rt &&
365 (ifp = ipx_droute.ro_rt->rt_ifp) &&
366 ipx_sroute.ro_rt &&
367 (ifp != ipx_sroute.ro_rt->rt_ifp)) {
368 flags |= IPX_ALLOWBROADCAST;
369 } else {
370 ipxstat.ipxs_noroute++;
371 m_freem(m);
372 goto cleanup;
376 * We don't need to recompute checksum because ipx_tc field
377 * is ignored by checksum calculation routine, however
378 * it may be desirable to reset checksum if ipxcksum == 0
380 #if 0
381 if (!ipxcksum)
382 ipx->ipx_sum = 0xffff;
383 #endif
385 error = ipx_outputfl(m, &ipx_droute, flags);
386 if (error == 0) {
387 ipxstat.ipxs_forward++;
389 if (ipxprintfs) {
390 kprintf("forward: ");
391 ipx_printhost(&ipx->ipx_sna);
392 kprintf(" to ");
393 ipx_printhost(&ipx->ipx_dna);
394 kprintf(" hops %d\n", ipx->ipx_tc);
396 } else if (mcopy != NULL) {
397 ipx = mtod(mcopy, struct ipx *);
398 switch (error) {
400 case ENETUNREACH:
401 case EHOSTDOWN:
402 case EHOSTUNREACH:
403 case ENETDOWN:
404 case EPERM:
405 ipxstat.ipxs_noroute++;
406 break;
408 case EMSGSIZE:
409 ipxstat.ipxs_mtutoosmall++;
410 break;
412 case ENOBUFS:
413 ipxstat.ipxs_odropped++;
414 break;
416 mcopy = NULL;
417 m_freem(m);
419 cleanup:
420 if (ok_there)
421 ipx_undo_route(&ipx_droute);
422 if (ok_back)
423 ipx_undo_route(&ipx_sroute);
424 if (mcopy != NULL)
425 m_freem(mcopy);
428 static int
429 ipx_do_route(struct ipx_addr *src, struct route *ro)
431 struct sockaddr_ipx *dst;
433 bzero((caddr_t)ro, sizeof(*ro));
434 dst = (struct sockaddr_ipx *)&ro->ro_dst;
436 dst->sipx_len = sizeof(*dst);
437 dst->sipx_family = AF_IPX;
438 dst->sipx_addr = *src;
439 dst->sipx_addr.x_port = 0;
440 rtalloc(ro);
441 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
442 return (0);
444 ro->ro_rt->rt_use++;
445 return (1);
448 static void
449 ipx_undo_route(struct route *ro)
451 if (ro->ro_rt != NULL) {
452 RTFREE(ro->ro_rt);
456 void
457 ipx_watch_output(struct mbuf *m, struct ifnet *ifp)
459 struct ipxpcb *ipxp;
460 struct ipx_ifaddr *ia;
463 * Give any raw listeners a crack at the packet
465 for (ipxp = ipxrawpcb.ipxp_next; ipxp != &ipxrawpcb;
466 ipxp = ipxp->ipxp_next) {
467 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
468 if (m0 != NULL) {
469 struct ipx *ipx;
471 M_PREPEND(m0, sizeof(*ipx), MB_DONTWAIT);
472 if (m0 == NULL)
473 continue;
474 ipx = mtod(m0, struct ipx *);
475 ipx->ipx_sna.x_net = ipx_zeronet;
476 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
477 if (ifp == ia->ia_ifp)
478 break;
479 if (ia == NULL)
480 ipx->ipx_sna.x_host = ipx_zerohost;
481 else
482 ipx->ipx_sna.x_host =
483 ia->ia_addr.sipx_addr.x_host;
485 if (ifp != NULL && (ifp->if_flags & IFF_POINTOPOINT)) {
486 struct ifaddr_container *ifac;
488 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
489 ifa_link) {
490 struct ifaddr *ifa = ifac->ifa;
492 if (ifa->ifa_addr->sa_family == AF_IPX) {
493 ipx->ipx_sna = IA_SIPX(ifa)->sipx_addr;
494 break;
498 ipx->ipx_len = ntohl(m0->m_pkthdr.len);
499 ipx_input(m0, ipxp);