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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/sys/netipx/ipx_input.c,v 1.22.2.2 2001/02/22 09:44:18 bp Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/protosw.h>
39 #include <sys/socket.h>
40 #include <sys/kernel.h>
41 #include <sys/random.h>
42 #include <sys/sysctl.h>
44 #include <sys/thread2.h>
45 #include <sys/msgport2.h>
46 #include <sys/mplock2.h>
49 #include <net/route.h>
50 #include <net/netisr.h>
59 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, checksum, CTLFLAG_RW,
62 static int ipxprintfs = 0; /* printing forwarding information */
63 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxprintfs, CTLFLAG_RW,
66 static int ipxforwarding = 0;
67 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxforwarding, CTLFLAG_RW,
68 &ipxforwarding, 0, "");
70 static int ipxnetbios = 0;
71 SYSCTL_INT(_net_ipx, OID_AUTO, ipxnetbios, CTLFLAG_RW,
74 union ipx_net ipx_zeronet;
75 union ipx_host ipx_zerohost;
77 union ipx_net ipx_broadnet;
78 union ipx_host ipx_broadhost;
80 struct ipxstat ipxstat;
81 struct sockaddr_ipx ipx_netmask, ipx_hostmask;
83 static u_short allones[] = {-1, -1, -1};
85 struct ipxpcbhead ipxpcb_list;
86 struct ipxpcbhead ipxrawpcb_list;
90 static void ipxintr(netmsg_t msg);
91 static int ipx_do_route(struct ipx_addr *src, struct route *ro);
92 static void ipx_undo_route(struct route *ro);
93 static void ipx_forward(struct mbuf *m);
102 ipx_broadnet = *(union ipx_net *)allones;
103 ipx_broadhost = *(union ipx_host *)allones;
105 read_random(&ipx_pexseq, sizeof ipx_pexseq);
106 LIST_INIT(&ipxpcb_list);
107 LIST_INIT(&ipxrawpcb_list);
109 ipx_netmask.sipx_len = 6;
110 ipx_netmask.sipx_addr.x_net = ipx_broadnet;
112 ipx_hostmask.sipx_len = 12;
113 ipx_hostmask.sipx_addr.x_net = ipx_broadnet;
114 ipx_hostmask.sipx_addr.x_host = ipx_broadhost;
116 netisr_register(NETISR_IPX, ipxintr, NULL);
120 * IPX input routine. Pass to next level.
123 ipxintr(netmsg_t msg)
125 struct mbuf *m = msg->packet.nm_packet;
128 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))) == NULL) {
144 ipxstat.ipxs_toosmall++;
149 * Give any raw listeners a crack at the packet
151 LIST_FOREACH(ipxp, &ipxrawpcb_list, ipxp_list) {
152 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
157 ipx = mtod(m, struct ipx *);
158 len = ntohs(ipx->ipx_len);
160 * Check that the amount of data in the buffers
161 * is as at least much as the IPX header would have us expect.
162 * Trim mbufs if longer than we expect.
163 * Drop packet if shorter than we expect.
165 if (m->m_pkthdr.len < len) {
166 ipxstat.ipxs_tooshort++;
169 if (m->m_pkthdr.len > len) {
170 if (m->m_len == m->m_pkthdr.len) {
172 m->m_pkthdr.len = len;
174 m_adj(m, len - m->m_pkthdr.len);
176 if (ipxcksum && ipx->ipx_sum != 0xffff) {
177 if (ipx->ipx_sum != ipx_cksum(m, len)) {
178 ipxstat.ipxs_badsum++;
184 * Propagated (Netbios) packets (type 20) has to be handled
187 if (ipx->ipx_pt == IPXPROTO_NETBIOS) {
189 ipx_output_type20(m);
196 * Is this a directed broadcast?
198 if (ipx_hosteqnh(ipx_broadhost,ipx->ipx_dna.x_host)) {
199 if ((!ipx_neteq(ipx->ipx_dna, ipx->ipx_sna)) &&
200 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_broadnet)) &&
201 (!ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet)) &&
202 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)) ) {
204 * If it is a broadcast to the net where it was
205 * received from, treat it as ours.
207 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
208 if((ia->ia_ifa.ifa_ifp == m->m_pkthdr.rcvif) &&
209 ipx_neteq(ia->ia_addr.sipx_addr,
214 * Look to see if I need to eat this packet.
215 * Algorithm is to forward all young packets
216 * and prematurely age any packets which will
217 * by physically broadcasted.
218 * Any very old packets eaten without forwarding
221 * Suggestion of Bill Nesheim, Cornell U.
223 if (ipx->ipx_tc < IPX_MAXHOPS) {
229 * Is this our packet? If not, forward.
232 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
233 if (ipx_hosteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) &&
234 (ipx_neteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) ||
235 ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)))
245 * Locate pcb for datagram.
247 ipxp = ipx_pcblookup(&ipx->ipx_sna, ipx->ipx_dna.x_port, IPX_WILDCARD);
249 * Switch out to protocol's input routine.
252 ipxstat.ipxs_delivered++;
253 if ((ipxp->ipxp_flags & IPXP_ALL_PACKETS) == 0)
254 switch (ipx->ipx_pt) {
269 /* msg was embedded in the mbuf, do not reply! */
274 * arg_as_sa: XXX should be swapped with dummy
277 ipx_ctlinput(netmsg_t msg)
279 int cmd = msg->ctlinput.nm_cmd;
280 struct sockaddr *arg_as_sa = msg->ctlinput.nm_arg;
281 caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
282 struct sockaddr_ipx *sipx;
284 if (cmd < 0 || cmd > PRC_NCMDS)
290 case PRC_HOSTUNREACH:
291 sipx = (struct sockaddr_ipx *)arg;
292 if (sipx->sipx_family != AF_IPX)
298 kprintf("ipx_ctlinput: cmd %d.\n", cmd);
302 lwkt_replymsg(&msg->lmsg, 0);
306 * Forward a packet. If some error occurs drop the packet. IPX don't
307 * have a way to return errors to the sender.
310 static struct route ipx_droute;
311 static struct route ipx_sroute;
314 ipx_forward(struct mbuf *m)
316 struct ipx *ipx = mtod(m, struct ipx *);
318 struct mbuf *mcopy = NULL;
320 int flags = IPX_FORWARDING;
324 if (ipxforwarding == 0) {
325 /* can't tell difference between net and host */
326 ipxstat.ipxs_cantforward++;
331 if (ipx->ipx_tc > IPX_MAXHOPS) {
332 ipxstat.ipxs_cantforward++;
337 if ((ok_there = ipx_do_route(&ipx->ipx_dna,&ipx_droute)) == 0) {
338 ipxstat.ipxs_noroute++;
343 * Here we think about forwarding broadcast packets,
344 * so we try to insure that it doesn't go back out
345 * on the interface it came in on. Also, if we
346 * are going to physically broadcast this, let us
347 * age the packet so we can eat it safely the second time around.
349 if (ipx->ipx_dna.x_host.c_host[0] & 0x1) {
350 struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
353 /* I'm gonna hafta eat this packet */
354 agedelta += IPX_MAXHOPS - ipx->ipx_tc;
355 ipx->ipx_tc = IPX_MAXHOPS;
357 if ((ok_back = ipx_do_route(&ipx->ipx_sna,&ipx_sroute)) == 0) {
358 /* error = ENETUNREACH; He'll never get it! */
359 ipxstat.ipxs_noroute++;
363 if (ipx_droute.ro_rt &&
364 (ifp = ipx_droute.ro_rt->rt_ifp) &&
366 (ifp != ipx_sroute.ro_rt->rt_ifp)) {
367 flags |= IPX_ALLOWBROADCAST;
369 ipxstat.ipxs_noroute++;
375 * We don't need to recompute checksum because ipx_tc field
376 * is ignored by checksum calculation routine, however
377 * it may be desirable to reset checksum if ipxcksum == 0
381 ipx->ipx_sum = 0xffff;
384 error = ipx_outputfl(m, &ipx_droute, flags);
386 ipxstat.ipxs_forward++;
389 kprintf("forward: ");
390 ipx_printhost(&ipx->ipx_sna);
392 ipx_printhost(&ipx->ipx_dna);
393 kprintf(" hops %d\n", ipx->ipx_tc);
395 } else if (mcopy != NULL) {
396 ipx = mtod(mcopy, struct ipx *);
404 ipxstat.ipxs_noroute++;
408 ipxstat.ipxs_mtutoosmall++;
412 ipxstat.ipxs_odropped++;
420 ipx_undo_route(&ipx_droute);
422 ipx_undo_route(&ipx_sroute);
428 ipx_do_route(struct ipx_addr *src, struct route *ro)
430 struct sockaddr_ipx *dst;
432 bzero((caddr_t)ro, sizeof(*ro));
433 dst = (struct sockaddr_ipx *)&ro->ro_dst;
435 dst->sipx_len = sizeof(*dst);
436 dst->sipx_family = AF_IPX;
437 dst->sipx_addr = *src;
438 dst->sipx_addr.x_port = 0;
440 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
448 ipx_undo_route(struct route *ro)
450 if (ro->ro_rt != NULL) {
456 ipx_watch_output(struct mbuf *m, struct ifnet *ifp)
459 struct ipx_ifaddr *ia;
462 * Give any raw listeners a crack at the packet
464 LIST_FOREACH(ipxp, &ipxrawpcb_list, ipxp_list) {
465 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
469 M_PREPEND(m0, sizeof(*ipx), MB_DONTWAIT);
472 ipx = mtod(m0, struct ipx *);
473 ipx->ipx_sna.x_net = ipx_zeronet;
474 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
475 if (ifp == ia->ia_ifp)
478 ipx->ipx_sna.x_host = ipx_zerohost;
480 ipx->ipx_sna.x_host =
481 ia->ia_addr.sipx_addr.x_host;
483 if (ifp != NULL && (ifp->if_flags & IFF_POINTOPOINT)) {
484 struct ifaddr_container *ifac;
486 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
488 struct ifaddr *ifa = ifac->ifa;
490 if (ifa->ifa_addr->sa_family == AF_IPX) {
491 ipx->ipx_sna = IA_SIPX(ifa)->sipx_addr;
496 ipx->ipx_len = ntohl(m0->m_pkthdr.len);