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
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
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.8 2004/03/06 01:58:56 hsu Exp $
40 #include <sys/param.h>
41 #include <sys/systm.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>
50 #include <net/route.h>
51 #include <net/netisr.h>
52 #include <net/intrq.h>
61 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, checksum, CTLFLAG_RW,
64 static int ipxprintfs = 0; /* printing forwarding information */
65 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxprintfs, CTLFLAG_RW,
68 static int ipxforwarding = 0;
69 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxforwarding, CTLFLAG_RW,
70 &ipxforwarding, 0, "");
72 static int ipxnetbios = 0;
73 SYSCTL_INT(_net_ipx, OID_AUTO, ipxnetbios, CTLFLAG_RW,
76 union ipx_net ipx_zeronet;
77 union ipx_host ipx_zerohost;
79 union ipx_net ipx_broadnet;
80 union ipx_host ipx_broadhost;
82 struct ipxstat ipxstat;
83 struct sockaddr_ipx ipx_netmask, ipx_hostmask;
85 static u_short allones[] = {-1, -1, -1};
88 struct ipxpcb ipxrawpcb;
92 static void ipxintr(struct netmsg *);
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);
104 ipx_broadnet = *(union ipx_net *)allones;
105 ipx_broadhost = *(union ipx_host *)allones;
107 read_random(&ipx_pexseq, sizeof ipx_pexseq);
108 ipxpcb.ipxp_next = ipxpcb.ipxp_prev = &ipxpcb;
109 ipxrawpcb.ipxp_next = ipxrawpcb.ipxp_prev = &ipxrawpcb;
111 ipx_netmask.sipx_len = 6;
112 ipx_netmask.sipx_addr.x_net = ipx_broadnet;
114 ipx_hostmask.sipx_len = 12;
115 ipx_hostmask.sipx_addr.x_net = ipx_broadnet;
116 ipx_hostmask.sipx_addr.x_host = ipx_broadhost;
118 netisr_register(NETISR_IPX, cpu0_portfn, ipxintr);
122 * IPX input routine. Pass to next level.
125 ipxintr(struct netmsg *msg)
127 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
130 struct ipx_ifaddr *ia;
134 * If no IPX addresses have been set yet but the interfaces
135 * are receiving, can't do anything with incoming packets yet.
137 if (ipx_ifaddr == NULL)
140 ipxstat.ipxs_total++;
142 if ((m->m_flags & M_EXT || m->m_len < sizeof(struct ipx)) &&
143 (m = m_pullup(m, sizeof(struct ipx))) == 0) {
144 ipxstat.ipxs_toosmall++;
149 * Give any raw listeners a crack at the packet
151 for (ipxp = ipxrawpcb.ipxp_next; ipxp != &ipxrawpcb;
152 ipxp = ipxp->ipxp_next) {
153 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
158 ipx = mtod(m, struct ipx *);
159 len = ntohs(ipx->ipx_len);
161 * Check that the amount of data in the buffers
162 * is as at least much as the IPX header would have us expect.
163 * Trim mbufs if longer than we expect.
164 * Drop packet if shorter than we expect.
166 if (m->m_pkthdr.len < len) {
167 ipxstat.ipxs_tooshort++;
170 if (m->m_pkthdr.len > len) {
171 if (m->m_len == m->m_pkthdr.len) {
173 m->m_pkthdr.len = len;
175 m_adj(m, len - m->m_pkthdr.len);
177 if (ipxcksum && ipx->ipx_sum != 0xffff) {
178 if (ipx->ipx_sum != ipx_cksum(m, len)) {
179 ipxstat.ipxs_badsum++;
185 * Propagated (Netbios) packets (type 20) has to be handled
188 if (ipx->ipx_pt == IPXPROTO_NETBIOS) {
190 ipx_output_type20(m);
197 * Is this a directed broadcast?
199 if (ipx_hosteqnh(ipx_broadhost,ipx->ipx_dna.x_host)) {
200 if ((!ipx_neteq(ipx->ipx_dna, ipx->ipx_sna)) &&
201 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_broadnet)) &&
202 (!ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet)) &&
203 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)) ) {
205 * If it is a broadcast to the net where it was
206 * received from, treat it as ours.
208 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
209 if((ia->ia_ifa.ifa_ifp == m->m_pkthdr.rcvif) &&
210 ipx_neteq(ia->ia_addr.sipx_addr,
215 * Look to see if I need to eat this packet.
216 * Algorithm is to forward all young packets
217 * and prematurely age any packets which will
218 * by physically broadcasted.
219 * Any very old packets eaten without forwarding
222 * Suggestion of Bill Nesheim, Cornell U.
224 if (ipx->ipx_tc < IPX_MAXHOPS) {
230 * Is this our packet? If not, forward.
233 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
234 if (ipx_hosteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) &&
235 (ipx_neteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) ||
236 ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)))
246 * Locate pcb for datagram.
248 ipxp = ipx_pcblookup(&ipx->ipx_sna, ipx->ipx_dna.x_port, IPX_WILDCARD);
250 * Switch out to protocol's input routine.
253 ipxstat.ipxs_delivered++;
254 if ((ipxp->ipxp_flags & IPXP_ALL_PACKETS) == 0)
255 switch (ipx->ipx_pt) {
271 ipx_ctlinput(cmd, arg_as_sa, dummy)
273 struct sockaddr *arg_as_sa; /* XXX should be swapped with dummy */
276 caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
277 struct ipx_addr *ipx;
279 if (cmd < 0 || cmd > PRC_NCMDS)
282 struct sockaddr_ipx *sipx;
286 case PRC_HOSTUNREACH:
287 sipx = (struct sockaddr_ipx *)arg;
288 if (sipx->sipx_family != AF_IPX)
290 ipx = &sipx->sipx_addr;
295 printf("ipx_ctlinput: cmd %d.\n", cmd);
301 * Forward a packet. If some error occurs drop the packet. IPX don't
302 * have a way to return errors to the sender.
305 static struct route ipx_droute;
306 static struct route ipx_sroute;
312 struct ipx *ipx = mtod(m, struct ipx *);
314 struct mbuf *mcopy = NULL;
316 int flags = IPX_FORWARDING;
320 if (ipxforwarding == 0) {
321 /* can't tell difference between net and host */
322 ipxstat.ipxs_cantforward++;
327 if (ipx->ipx_tc > IPX_MAXHOPS) {
328 ipxstat.ipxs_cantforward++;
333 if ((ok_there = ipx_do_route(&ipx->ipx_dna,&ipx_droute)) == 0) {
334 ipxstat.ipxs_noroute++;
339 * Here we think about forwarding broadcast packets,
340 * so we try to insure that it doesn't go back out
341 * on the interface it came in on. Also, if we
342 * are going to physically broadcast this, let us
343 * age the packet so we can eat it safely the second time around.
345 if (ipx->ipx_dna.x_host.c_host[0] & 0x1) {
346 struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
349 /* I'm gonna hafta eat this packet */
350 agedelta += IPX_MAXHOPS - ipx->ipx_tc;
351 ipx->ipx_tc = IPX_MAXHOPS;
353 if ((ok_back = ipx_do_route(&ipx->ipx_sna,&ipx_sroute)) == 0) {
354 /* error = ENETUNREACH; He'll never get it! */
355 ipxstat.ipxs_noroute++;
359 if (ipx_droute.ro_rt &&
360 (ifp = ipx_droute.ro_rt->rt_ifp) &&
362 (ifp != ipx_sroute.ro_rt->rt_ifp)) {
363 flags |= IPX_ALLOWBROADCAST;
365 ipxstat.ipxs_noroute++;
371 * We don't need to recompute checksum because ipx_tc field
372 * is ignored by checksum calculation routine, however
373 * it may be desirable to reset checksum if ipxcksum == 0
377 ipx->ipx_sum = 0xffff;
380 error = ipx_outputfl(m, &ipx_droute, flags);
382 ipxstat.ipxs_forward++;
386 ipx_printhost(&ipx->ipx_sna);
388 ipx_printhost(&ipx->ipx_dna);
389 printf(" hops %d\n", ipx->ipx_tc);
391 } else if (mcopy != NULL) {
392 ipx = mtod(mcopy, struct ipx *);
400 ipxstat.ipxs_noroute++;
404 ipxstat.ipxs_mtutoosmall++;
408 ipxstat.ipxs_odropped++;
416 ipx_undo_route(&ipx_droute);
418 ipx_undo_route(&ipx_sroute);
424 ipx_do_route(src, ro)
425 struct ipx_addr *src;
428 struct sockaddr_ipx *dst;
430 bzero((caddr_t)ro, sizeof(*ro));
431 dst = (struct sockaddr_ipx *)&ro->ro_dst;
433 dst->sipx_len = sizeof(*dst);
434 dst->sipx_family = AF_IPX;
435 dst->sipx_addr = *src;
436 dst->sipx_addr.x_port = 0;
438 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
449 if (ro->ro_rt != NULL) {
455 ipx_watch_output(m, ifp)
461 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);
471 M_PREPEND(m0, sizeof(*ipx), M_DONTWAIT);
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)
480 ipx->ipx_sna.x_host = ipx_zerohost;
482 ipx->ipx_sna.x_host =
483 ia->ia_addr.sipx_addr.x_host;
485 if (ifp != NULL && (ifp->if_flags & IFF_POINTOPOINT))
486 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
487 if (ifa->ifa_addr->sa_family == AF_IPX) {
488 ipx->ipx_sna = IA_SIPX(ifa)->sipx_addr;
492 ipx->ipx_len = ntohl(m0->m_pkthdr.len);