2 * Copyright (c) 1984, 1985, 1986, 1987, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2004-2009 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * 4. 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.
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
31 * Copyright (c) 1995, Mike Mitchell
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/kernel.h>
74 #include <sys/random.h>
76 #include <sys/rwlock.h>
77 #include <sys/sysctl.h>
80 #include <net/route.h>
81 #include <net/netisr.h>
83 #include <netipx/ipx.h>
84 #include <netipx/spx.h>
85 #include <netipx/ipx_if.h>
86 #include <netipx/ipx_pcb.h>
87 #include <netipx/ipx_var.h>
90 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, checksum, CTLFLAG_RW,
91 &ipxcksum, 0, "Compute ipx checksum");
93 static int ipxprintfs = 0; /* printing forwarding information */
94 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxprintfs, CTLFLAG_RW,
95 &ipxprintfs, 0, "Printing forwarding information");
97 static int ipxforwarding = 0;
98 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxforwarding, CTLFLAG_RW,
99 &ipxforwarding, 0, "Enable ipx forwarding");
101 static int ipxnetbios = 0;
102 SYSCTL_INT(_net_ipx, OID_AUTO, ipxnetbios, CTLFLAG_RW,
103 &ipxnetbios, 0, "Propagate netbios over ipx");
105 static int ipx_do_route(struct ipx_addr *src, struct route *ro);
106 static void ipx_undo_route(struct route *ro);
107 static void ipx_forward(struct mbuf *m);
108 static void ipxintr(struct mbuf *m);
110 const union ipx_net ipx_zeronet;
112 const union ipx_net ipx_broadnet = { .s_net[0] = 0xffff,
113 .s_net[1] = 0xffff };
114 const union ipx_host ipx_broadhost = { .s_host[0] = 0xffff,
116 .s_host[2] = 0xffff };
118 struct ipxstat ipxstat;
119 struct sockaddr_ipx ipx_netmask, ipx_hostmask;
122 * IPX protocol control block (pcb) lists.
124 struct mtx ipxpcb_list_mtx;
125 struct ipxpcbhead ipxpcb_list;
126 struct ipxpcbhead ipxrawpcb_list;
128 static struct netisr_handler ipx_nh = {
130 .nh_handler = ipxintr,
131 .nh_proto = NETISR_IPX,
132 .nh_policy = NETISR_POLICY_SOURCE,
135 long ipx_pexseq; /* Locked with ipxpcb_list_mtx. */
138 * IPX initialization.
145 read_random(&ipx_pexseq, sizeof ipx_pexseq);
147 LIST_INIT(&ipxpcb_list);
148 LIST_INIT(&ipxrawpcb_list);
149 TAILQ_INIT(&ipx_ifaddrhead);
151 IPX_LIST_LOCK_INIT();
152 IPX_IFADDR_LOCK_INIT();
154 ipx_netmask.sipx_len = 6;
155 ipx_netmask.sipx_addr.x_net = ipx_broadnet;
157 ipx_hostmask.sipx_len = 12;
158 ipx_hostmask.sipx_addr.x_net = ipx_broadnet;
159 ipx_hostmask.sipx_addr.x_host = ipx_broadhost;
161 netisr_register(&ipx_nh);
165 * IPX input routine. Pass to next level.
168 ipxintr(struct mbuf *m)
172 struct ipx_ifaddr *ia;
176 * If no IPX addresses have been set yet but the interfaces
177 * are receiving, can't do anything with incoming packets yet.
179 if (TAILQ_EMPTY(&ipx_ifaddrhead)) {
184 ipxstat.ipxs_total++;
186 if ((m->m_flags & M_EXT || m->m_len < sizeof(struct ipx)) &&
187 (m = m_pullup(m, sizeof(struct ipx))) == NULL) {
188 ipxstat.ipxs_toosmall++;
193 * Give any raw listeners a crack at the packet
196 LIST_FOREACH(ipxp, &ipxrawpcb_list, ipxp_list) {
197 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
206 ipx = mtod(m, struct ipx *);
207 len = ntohs(ipx->ipx_len);
209 * Check that the amount of data in the buffers
210 * is as at least much as the IPX header would have us expect.
211 * Trim mbufs if longer than we expect.
212 * Drop packet if shorter than we expect.
214 if (m->m_pkthdr.len < len) {
215 ipxstat.ipxs_tooshort++;
219 if (m->m_pkthdr.len > len) {
220 if (m->m_len == m->m_pkthdr.len) {
222 m->m_pkthdr.len = len;
224 m_adj(m, len - m->m_pkthdr.len);
226 if (ipxcksum && ipx->ipx_sum != 0xffff) {
227 if (ipx->ipx_sum != ipx_cksum(m, len)) {
228 ipxstat.ipxs_badsum++;
235 * Propagated (Netbios) packets (type 20) has to be handled
238 if (ipx->ipx_pt == IPXPROTO_NETBIOS) {
240 ipx_output_type20(m);
249 * Is this a directed broadcast?
251 if (ipx_hosteqnh(ipx_broadhost,ipx->ipx_dna.x_host)) {
252 if ((!ipx_neteq(ipx->ipx_dna, ipx->ipx_sna)) &&
253 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_broadnet)) &&
254 (!ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet)) &&
255 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)) ) {
257 * If it is a broadcast to the net where it was
258 * received from, treat it as ours.
261 TAILQ_FOREACH(ia, &ipx_ifaddrhead, ia_link) {
262 if ((ia->ia_ifa.ifa_ifp == m->m_pkthdr.rcvif)
263 && ipx_neteq(ia->ia_addr.sipx_addr,
265 IPX_IFADDR_RUNLOCK();
269 IPX_IFADDR_RUNLOCK();
272 * Look to see if I need to eat this packet.
273 * Algorithm is to forward all young packets
274 * and prematurely age any packets which will
275 * by physically broadcasted.
276 * Any very old packets eaten without forwarding
279 * Suggestion of Bill Nesheim, Cornell U.
281 if (ipx->ipx_tc < IPX_MAXHOPS) {
287 * Is this our packet? If not, forward.
291 TAILQ_FOREACH(ia, &ipx_ifaddrhead, ia_link) {
292 if (ipx_hosteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) &&
293 (ipx_neteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) ||
294 ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)))
297 IPX_IFADDR_RUNLOCK();
305 * Locate pcb for datagram.
308 ipxp = ipx_pcblookup(&ipx->ipx_sna, ipx->ipx_dna.x_port, IPX_WILDCARD);
310 * Switch out to protocol's input routine.
313 ipxstat.ipxs_delivered++;
314 if ((ipxp->ipxp_flags & IPXP_ALL_PACKETS) == 0)
315 switch (ipx->ipx_pt) {
318 /* Will release both locks. */
331 ipx_ctlinput(cmd, arg_as_sa, dummy)
333 struct sockaddr *arg_as_sa; /* XXX should be swapped with dummy */
337 /* Currently, nothing. */
341 * Forward a packet. If some error occurs drop the packet. IPX don't
342 * have a way to return errors to the sender.
345 static struct route ipx_droute;
346 static struct route ipx_sroute;
349 ipx_forward(struct mbuf *m)
351 struct ipx *ipx = mtod(m, struct ipx *);
354 int flags = IPX_FORWARDING;
358 if (ipxforwarding == 0) {
359 /* can't tell difference between net and host */
360 ipxstat.ipxs_cantforward++;
365 if (ipx->ipx_tc > IPX_MAXHOPS) {
366 ipxstat.ipxs_cantforward++;
371 if ((ok_there = ipx_do_route(&ipx->ipx_dna,&ipx_droute)) == 0) {
372 ipxstat.ipxs_noroute++;
377 * Here we think about forwarding broadcast packets,
378 * so we try to insure that it doesn't go back out
379 * on the interface it came in on. Also, if we
380 * are going to physically broadcast this, let us
381 * age the packet so we can eat it safely the second time around.
383 if (ipx->ipx_dna.x_host.c_host[0] & 0x1) {
384 struct ipx_ifaddr *ia;
388 ia = ipx_iaonnetof(&ipx->ipx_dna);
390 /* I'm gonna hafta eat this packet */
391 agedelta += IPX_MAXHOPS - ipx->ipx_tc;
392 ipx->ipx_tc = IPX_MAXHOPS;
394 IPX_IFADDR_RUNLOCK();
395 if ((ok_back = ipx_do_route(&ipx->ipx_sna,&ipx_sroute)) == 0) {
396 /* error = ENETUNREACH; He'll never get it! */
397 ipxstat.ipxs_noroute++;
401 if (ipx_droute.ro_rt &&
402 (ifp = ipx_droute.ro_rt->rt_ifp) &&
404 (ifp != ipx_sroute.ro_rt->rt_ifp)) {
405 flags |= IPX_ALLOWBROADCAST;
407 ipxstat.ipxs_noroute++;
413 * We don't need to recompute checksum because ipx_tc field
414 * is ignored by checksum calculation routine, however
415 * it may be desirable to reset checksum if ipxcksum == 0
419 ipx->ipx_sum = 0xffff;
422 error = ipx_outputfl(m, &ipx_droute, flags);
424 ipxstat.ipxs_forward++;
428 ipx_printhost(&ipx->ipx_sna);
430 ipx_printhost(&ipx->ipx_dna);
431 printf(" hops %d\n", ipx->ipx_tc);
436 ipx_undo_route(&ipx_droute);
438 ipx_undo_route(&ipx_sroute);
442 ipx_do_route(struct ipx_addr *src, struct route *ro)
444 struct sockaddr_ipx *dst;
446 bzero((caddr_t)ro, sizeof(*ro));
447 dst = (struct sockaddr_ipx *)&ro->ro_dst;
449 dst->sipx_len = sizeof(*dst);
450 dst->sipx_family = AF_IPX;
451 dst->sipx_addr = *src;
452 dst->sipx_addr.x_port = 0;
454 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
462 ipx_undo_route(struct route *ro)
465 if (ro->ro_rt != NULL) {