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 $
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/kernel.h>
45 #include <sys/random.h>
46 #include <sys/sysctl.h>
48 #include <sys/thread2.h>
49 #include <sys/msgport2.h>
50 #include <sys/mplock2.h>
53 #include <net/route.h>
54 #include <net/netisr.h>
63 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, checksum, CTLFLAG_RW,
66 static int ipxprintfs = 0; /* printing forwarding information */
67 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxprintfs, CTLFLAG_RW,
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,
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 ipxpcbhead ipxpcb_list;
90 struct ipxpcbhead ipxrawpcb_list;
94 static void ipxintr(netmsg_t msg);
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.
106 ipx_broadnet = *(union ipx_net *)allones;
107 ipx_broadhost = *(union ipx_host *)allones;
109 read_random(&ipx_pexseq, sizeof ipx_pexseq);
110 LIST_INIT(&ipxpcb_list);
111 LIST_INIT(&ipxrawpcb_list);
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, ipxintr, NULL);
124 * IPX input routine. Pass to next level.
127 ipxintr(netmsg_t msg)
129 struct mbuf *m = msg->packet.nm_packet;
132 struct ipx_ifaddr *ia;
138 * If no IPX addresses have been set yet but the interfaces
139 * are receiving, can't do anything with incoming packets yet.
141 if (ipx_ifaddr == NULL)
144 ipxstat.ipxs_total++;
146 if ((m->m_flags & M_EXT || m->m_len < sizeof(struct ipx)) &&
147 (m = m_pullup(m, sizeof(struct ipx))) == NULL) {
148 ipxstat.ipxs_toosmall++;
153 * Give any raw listeners a crack at the packet
155 LIST_FOREACH(ipxp, &ipxrawpcb_list, ipxp_list) {
156 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
161 ipx = mtod(m, struct ipx *);
162 len = ntohs(ipx->ipx_len);
164 * Check that the amount of data in the buffers
165 * is as at least much as the IPX header would have us expect.
166 * Trim mbufs if longer than we expect.
167 * Drop packet if shorter than we expect.
169 if (m->m_pkthdr.len < len) {
170 ipxstat.ipxs_tooshort++;
173 if (m->m_pkthdr.len > len) {
174 if (m->m_len == m->m_pkthdr.len) {
176 m->m_pkthdr.len = len;
178 m_adj(m, len - m->m_pkthdr.len);
180 if (ipxcksum && ipx->ipx_sum != 0xffff) {
181 if (ipx->ipx_sum != ipx_cksum(m, len)) {
182 ipxstat.ipxs_badsum++;
188 * Propagated (Netbios) packets (type 20) has to be handled
191 if (ipx->ipx_pt == IPXPROTO_NETBIOS) {
193 ipx_output_type20(m);
200 * Is this a directed broadcast?
202 if (ipx_hosteqnh(ipx_broadhost,ipx->ipx_dna.x_host)) {
203 if ((!ipx_neteq(ipx->ipx_dna, ipx->ipx_sna)) &&
204 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_broadnet)) &&
205 (!ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet)) &&
206 (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)) ) {
208 * If it is a broadcast to the net where it was
209 * received from, treat it as ours.
211 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
212 if((ia->ia_ifa.ifa_ifp == m->m_pkthdr.rcvif) &&
213 ipx_neteq(ia->ia_addr.sipx_addr,
218 * Look to see if I need to eat this packet.
219 * Algorithm is to forward all young packets
220 * and prematurely age any packets which will
221 * by physically broadcasted.
222 * Any very old packets eaten without forwarding
225 * Suggestion of Bill Nesheim, Cornell U.
227 if (ipx->ipx_tc < IPX_MAXHOPS) {
233 * Is this our packet? If not, forward.
236 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
237 if (ipx_hosteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) &&
238 (ipx_neteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) ||
239 ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)))
249 * Locate pcb for datagram.
251 ipxp = ipx_pcblookup(&ipx->ipx_sna, ipx->ipx_dna.x_port, IPX_WILDCARD);
253 * Switch out to protocol's input routine.
256 ipxstat.ipxs_delivered++;
257 if ((ipxp->ipxp_flags & IPXP_ALL_PACKETS) == 0)
258 switch (ipx->ipx_pt) {
273 /* msg was embedded in the mbuf, do not reply! */
278 * arg_as_sa: XXX should be swapped with dummy
281 ipx_ctlinput(netmsg_t msg)
283 int cmd = msg->ctlinput.nm_cmd;
284 struct sockaddr *arg_as_sa = msg->ctlinput.nm_arg;
285 caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
286 struct ipx_addr *ipx;
287 struct sockaddr_ipx *sipx;
289 if (cmd < 0 || cmd > PRC_NCMDS)
295 case PRC_HOSTUNREACH:
296 sipx = (struct sockaddr_ipx *)arg;
297 if (sipx->sipx_family != AF_IPX)
299 ipx = &sipx->sipx_addr;
304 kprintf("ipx_ctlinput: cmd %d.\n", cmd);
308 lwkt_replymsg(&msg->lmsg, 0);
312 * Forward a packet. If some error occurs drop the packet. IPX don't
313 * have a way to return errors to the sender.
316 static struct route ipx_droute;
317 static struct route ipx_sroute;
320 ipx_forward(struct mbuf *m)
322 struct ipx *ipx = mtod(m, struct ipx *);
324 struct mbuf *mcopy = NULL;
326 int flags = IPX_FORWARDING;
330 if (ipxforwarding == 0) {
331 /* can't tell difference between net and host */
332 ipxstat.ipxs_cantforward++;
337 if (ipx->ipx_tc > IPX_MAXHOPS) {
338 ipxstat.ipxs_cantforward++;
343 if ((ok_there = ipx_do_route(&ipx->ipx_dna,&ipx_droute)) == 0) {
344 ipxstat.ipxs_noroute++;
349 * Here we think about forwarding broadcast packets,
350 * so we try to insure that it doesn't go back out
351 * on the interface it came in on. Also, if we
352 * are going to physically broadcast this, let us
353 * age the packet so we can eat it safely the second time around.
355 if (ipx->ipx_dna.x_host.c_host[0] & 0x1) {
356 struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
359 /* I'm gonna hafta eat this packet */
360 agedelta += IPX_MAXHOPS - ipx->ipx_tc;
361 ipx->ipx_tc = IPX_MAXHOPS;
363 if ((ok_back = ipx_do_route(&ipx->ipx_sna,&ipx_sroute)) == 0) {
364 /* error = ENETUNREACH; He'll never get it! */
365 ipxstat.ipxs_noroute++;
369 if (ipx_droute.ro_rt &&
370 (ifp = ipx_droute.ro_rt->rt_ifp) &&
372 (ifp != ipx_sroute.ro_rt->rt_ifp)) {
373 flags |= IPX_ALLOWBROADCAST;
375 ipxstat.ipxs_noroute++;
381 * We don't need to recompute checksum because ipx_tc field
382 * is ignored by checksum calculation routine, however
383 * it may be desirable to reset checksum if ipxcksum == 0
387 ipx->ipx_sum = 0xffff;
390 error = ipx_outputfl(m, &ipx_droute, flags);
392 ipxstat.ipxs_forward++;
395 kprintf("forward: ");
396 ipx_printhost(&ipx->ipx_sna);
398 ipx_printhost(&ipx->ipx_dna);
399 kprintf(" hops %d\n", ipx->ipx_tc);
401 } else if (mcopy != NULL) {
402 ipx = mtod(mcopy, struct ipx *);
410 ipxstat.ipxs_noroute++;
414 ipxstat.ipxs_mtutoosmall++;
418 ipxstat.ipxs_odropped++;
426 ipx_undo_route(&ipx_droute);
428 ipx_undo_route(&ipx_sroute);
434 ipx_do_route(struct ipx_addr *src, struct route *ro)
436 struct sockaddr_ipx *dst;
438 bzero((caddr_t)ro, sizeof(*ro));
439 dst = (struct sockaddr_ipx *)&ro->ro_dst;
441 dst->sipx_len = sizeof(*dst);
442 dst->sipx_family = AF_IPX;
443 dst->sipx_addr = *src;
444 dst->sipx_addr.x_port = 0;
446 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
454 ipx_undo_route(struct route *ro)
456 if (ro->ro_rt != NULL) {
462 ipx_watch_output(struct mbuf *m, struct ifnet *ifp)
465 struct ipx_ifaddr *ia;
468 * Give any raw listeners a crack at the packet
470 LIST_FOREACH(ipxp, &ipxrawpcb_list, ipxp_list) {
471 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
475 M_PREPEND(m0, sizeof(*ipx), MB_DONTWAIT);
478 ipx = mtod(m0, struct ipx *);
479 ipx->ipx_sna.x_net = ipx_zeronet;
480 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
481 if (ifp == ia->ia_ifp)
484 ipx->ipx_sna.x_host = ipx_zerohost;
486 ipx->ipx_sna.x_host =
487 ia->ia_addr.sipx_addr.x_host;
489 if (ifp != NULL && (ifp->if_flags & IFF_POINTOPOINT)) {
490 struct ifaddr_container *ifac;
492 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
494 struct ifaddr *ifa = ifac->ifa;
496 if (ifa->ifa_addr->sa_family == AF_IPX) {
497 ipx->ipx_sna = IA_SIPX(ifa)->sipx_addr;
502 ipx->ipx_len = ntohl(m0->m_pkthdr.len);