f0e3ea7bf69ac8f41b848a4c96ec18c26bbae24d
[dragonfly.git] / sys / netproto / ipx / ipx_input.c
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.
5  *
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.
21  *
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.
33  *
34  *      @(#)ipx_input.c
35  *
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.5 2003/09/15 23:38:15 hsu Exp $
38  */
39
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>
48
49 #include <net/if.h>
50 #include <net/route.h>
51 #include <net/netisr.h>
52 #include <net/intrq.h>
53
54 #include "ipx.h"
55 #include "spx.h"
56 #include "ipx_if.h"
57 #include "ipx_pcb.h"
58 #include "ipx_var.h"
59
60 int     ipxcksum = 0;
61 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, checksum, CTLFLAG_RW,
62            &ipxcksum, 0, "");
63
64 static int      ipxprintfs = 0;         /* printing forwarding information */
65 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxprintfs, CTLFLAG_RW,
66            &ipxprintfs, 0, "");
67
68 static int      ipxforwarding = 0;
69 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxforwarding, CTLFLAG_RW,
70             &ipxforwarding, 0, "");
71
72 static int      ipxnetbios = 0;
73 SYSCTL_INT(_net_ipx, OID_AUTO, ipxnetbios, CTLFLAG_RW,
74            &ipxnetbios, 0, "");
75
76 union   ipx_net ipx_zeronet;
77 union   ipx_host ipx_zerohost;
78
79 union   ipx_net ipx_broadnet;
80 union   ipx_host ipx_broadhost;
81
82 struct  ipxstat ipxstat;
83 struct  sockaddr_ipx ipx_netmask, ipx_hostmask;
84
85 static  u_short allones[] = {-1, -1, -1};
86
87 struct  ipxpcb ipxpcb;
88 struct  ipxpcb ipxrawpcb;
89
90 static int ipxqmaxlen = IFQ_MAXLEN;
91 static struct ifqueue ipxintrq;
92
93 long    ipx_pexseq;
94 const int ipxintrq_present = 1;
95
96 static  void ipxintr(struct mbuf *);
97 static  int ipx_do_route(struct ipx_addr *src, struct route *ro);
98 static  void ipx_undo_route(struct route *ro);
99 static  void ipx_forward(struct mbuf *m);
100
101 /*
102  * IPX initialization.
103  */
104
105 void
106 ipx_init()
107 {
108         ipx_broadnet = *(union ipx_net *)allones;
109         ipx_broadhost = *(union ipx_host *)allones;
110
111         read_random(&ipx_pexseq, sizeof ipx_pexseq);
112         ipxintrq.ifq_maxlen = ipxqmaxlen;
113         ipxpcb.ipxp_next = ipxpcb.ipxp_prev = &ipxpcb;
114         ipxrawpcb.ipxp_next = ipxrawpcb.ipxp_prev = &ipxrawpcb;
115
116         ipx_netmask.sipx_len = 6;
117         ipx_netmask.sipx_addr.x_net = ipx_broadnet;
118
119         ipx_hostmask.sipx_len = 12;
120         ipx_hostmask.sipx_addr.x_net = ipx_broadnet;
121         ipx_hostmask.sipx_addr.x_host = ipx_broadhost;
122
123         netisr_register(NETISR_IPX, ipxintr, &ipxintrq);
124 }
125
126 /*
127  * IPX input routine.  Pass to next level.
128  */
129 static void
130 ipxintr(struct mbuf *m)
131 {
132         struct ipx *ipx;
133         struct ipxpcb *ipxp;
134         struct ipx_ifaddr *ia;
135         int len;
136
137         /*
138          * If no IPX addresses have been set yet but the interfaces
139          * are receiving, can't do anything with incoming packets yet.
140          */
141         if (ipx_ifaddr == NULL)
142                 goto bad;
143
144         ipxstat.ipxs_total++;
145
146         if ((m->m_flags & M_EXT || m->m_len < sizeof(struct ipx)) &&
147             (m = m_pullup(m, sizeof(struct ipx))) == 0) {
148                 ipxstat.ipxs_toosmall++;
149                 return;
150         }
151
152         /*
153          * Give any raw listeners a crack at the packet
154          */
155         for (ipxp = ipxrawpcb.ipxp_next; ipxp != &ipxrawpcb;
156              ipxp = ipxp->ipxp_next) {
157                 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
158                 if (m1 != NULL)
159                         ipx_input(m1, ipxp);
160         }
161
162         ipx = mtod(m, struct ipx *);
163         len = ntohs(ipx->ipx_len);
164         /*
165          * Check that the amount of data in the buffers
166          * is as at least much as the IPX header would have us expect.
167          * Trim mbufs if longer than we expect.
168          * Drop packet if shorter than we expect.
169          */
170         if (m->m_pkthdr.len < len) {
171                 ipxstat.ipxs_tooshort++;
172                 goto bad;
173         }
174         if (m->m_pkthdr.len > len) {
175                 if (m->m_len == m->m_pkthdr.len) {
176                         m->m_len = len;
177                         m->m_pkthdr.len = len;
178                 } else
179                         m_adj(m, len - m->m_pkthdr.len);
180         }
181         if (ipxcksum && ipx->ipx_sum != 0xffff) {
182                 if (ipx->ipx_sum != ipx_cksum(m, len)) {
183                         ipxstat.ipxs_badsum++;
184                         goto bad;
185                 }
186         }
187
188         /*
189          * Propagated (Netbios) packets (type 20) has to be handled
190          * different. :-(
191          */
192         if (ipx->ipx_pt == IPXPROTO_NETBIOS) {
193                 if (ipxnetbios) {
194                         ipx_output_type20(m);
195                         return;
196                 } else
197                         goto bad;
198         }
199
200         /*
201          * Is this a directed broadcast?
202          */
203         if (ipx_hosteqnh(ipx_broadhost,ipx->ipx_dna.x_host)) {
204                 if ((!ipx_neteq(ipx->ipx_dna, ipx->ipx_sna)) &&
205                     (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_broadnet)) &&
206                     (!ipx_neteqnn(ipx->ipx_sna.x_net, ipx_zeronet)) &&
207                     (!ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)) ) {
208                         /*
209                          * If it is a broadcast to the net where it was
210                          * received from, treat it as ours.
211                          */
212                         for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
213                                 if((ia->ia_ifa.ifa_ifp == m->m_pkthdr.rcvif) &&
214                                    ipx_neteq(ia->ia_addr.sipx_addr, 
215                                              ipx->ipx_dna))
216                                         goto ours;
217
218                         /*
219                          * Look to see if I need to eat this packet.
220                          * Algorithm is to forward all young packets
221                          * and prematurely age any packets which will
222                          * by physically broadcasted.
223                          * Any very old packets eaten without forwarding
224                          * would die anyway.
225                          *
226                          * Suggestion of Bill Nesheim, Cornell U.
227                          */
228                         if (ipx->ipx_tc < IPX_MAXHOPS) {
229                                 ipx_forward(m);
230                                 return;
231                         }
232                 }
233         /*
234          * Is this our packet? If not, forward.
235          */
236         } else {
237                 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
238                         if (ipx_hosteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) &&
239                             (ipx_neteq(ipx->ipx_dna, ia->ia_addr.sipx_addr) ||
240                              ipx_neteqnn(ipx->ipx_dna.x_net, ipx_zeronet)))
241                                 break;
242
243                 if (ia == NULL) {
244                         ipx_forward(m);
245                         return;
246                 }
247         }
248 ours:
249         /*
250          * Locate pcb for datagram.
251          */
252         ipxp = ipx_pcblookup(&ipx->ipx_sna, ipx->ipx_dna.x_port, IPX_WILDCARD);
253         /*
254          * Switch out to protocol's input routine.
255          */
256         if (ipxp != NULL) {
257                 ipxstat.ipxs_delivered++;
258                 if ((ipxp->ipxp_flags & IPXP_ALL_PACKETS) == 0)
259                         switch (ipx->ipx_pt) {
260                         case IPXPROTO_SPX:
261                                 spx_input(m, ipxp);
262                                 return;
263                         }
264                 ipx_input(m, ipxp);
265         } else
266                 goto bad;
267
268         return;
269
270 bad:
271         m_freem(m);
272 }
273
274 void
275 ipx_ctlinput(cmd, arg_as_sa, dummy)
276         int cmd;
277         struct sockaddr *arg_as_sa;     /* XXX should be swapped with dummy */
278         void *dummy;
279 {
280         caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
281         struct ipx_addr *ipx;
282
283         if (cmd < 0 || cmd > PRC_NCMDS)
284                 return;
285         switch (cmd) {
286                 struct sockaddr_ipx *sipx;
287
288         case PRC_IFDOWN:
289         case PRC_HOSTDEAD:
290         case PRC_HOSTUNREACH:
291                 sipx = (struct sockaddr_ipx *)arg;
292                 if (sipx->sipx_family != AF_IPX)
293                         return;
294                 ipx = &sipx->sipx_addr;
295                 break;
296
297         default:
298                 if (ipxprintfs)
299                         printf("ipx_ctlinput: cmd %d.\n", cmd);
300                 break;
301         }
302 }
303
304 /*
305  * Forward a packet. If some error occurs drop the packet. IPX don't
306  * have a way to return errors to the sender.
307  */
308
309 static struct route ipx_droute;
310 static struct route ipx_sroute;
311
312 static void
313 ipx_forward(m)
314 struct mbuf *m;
315 {
316         struct ipx *ipx = mtod(m, struct ipx *);
317         int error;
318         struct mbuf *mcopy = NULL;
319         int agedelta = 1;
320         int flags = IPX_FORWARDING;
321         int ok_there = 0;
322         int ok_back = 0;
323
324         if (ipxforwarding == 0) {
325                 /* can't tell difference between net and host */
326                 ipxstat.ipxs_cantforward++;
327                 m_freem(m);
328                 goto cleanup;
329         }
330         ipx->ipx_tc++;
331         if (ipx->ipx_tc > IPX_MAXHOPS) {
332                 ipxstat.ipxs_cantforward++;
333                 m_freem(m);
334                 goto cleanup;
335         }
336
337         if ((ok_there = ipx_do_route(&ipx->ipx_dna,&ipx_droute)) == 0) {
338                 ipxstat.ipxs_noroute++;
339                 m_freem(m);
340                 goto cleanup;
341         }
342         /*
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.
348          */
349         if (ipx->ipx_dna.x_host.c_host[0] & 0x1) {
350                 struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
351                 struct ifnet *ifp;
352                 if (ia != NULL) {
353                         /* I'm gonna hafta eat this packet */
354                         agedelta += IPX_MAXHOPS - ipx->ipx_tc;
355                         ipx->ipx_tc = IPX_MAXHOPS;
356                 }
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++;
360                         m_freem(m);
361                         goto cleanup;
362                 }
363                 if (ipx_droute.ro_rt &&
364                     (ifp = ipx_droute.ro_rt->rt_ifp) &&
365                     ipx_sroute.ro_rt &&
366                     (ifp != ipx_sroute.ro_rt->rt_ifp)) {
367                         flags |= IPX_ALLOWBROADCAST;
368                 } else {
369                         ipxstat.ipxs_noroute++;
370                         m_freem(m);
371                         goto cleanup;
372                 }
373         }
374         /*
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
378          */
379 #if 0
380         if (!ipxcksum)
381                 ipx->ipx_sum = 0xffff;
382 #endif
383
384         error = ipx_outputfl(m, &ipx_droute, flags);
385         if (error == 0) {
386                 ipxstat.ipxs_forward++;
387
388                 if (ipxprintfs) {
389                         printf("forward: ");
390                         ipx_printhost(&ipx->ipx_sna);
391                         printf(" to ");
392                         ipx_printhost(&ipx->ipx_dna);
393                         printf(" hops %d\n", ipx->ipx_tc);
394                 }
395         } else if (mcopy != NULL) {
396                 ipx = mtod(mcopy, struct ipx *);
397                 switch (error) {
398
399                 case ENETUNREACH:
400                 case EHOSTDOWN:
401                 case EHOSTUNREACH:
402                 case ENETDOWN:
403                 case EPERM:
404                         ipxstat.ipxs_noroute++;
405                         break;
406
407                 case EMSGSIZE:
408                         ipxstat.ipxs_mtutoosmall++;
409                         break;
410
411                 case ENOBUFS:
412                         ipxstat.ipxs_odropped++;
413                         break;
414                 }
415                 mcopy = NULL;
416                 m_freem(m);
417         }
418 cleanup:
419         if (ok_there)
420                 ipx_undo_route(&ipx_droute);
421         if (ok_back)
422                 ipx_undo_route(&ipx_sroute);
423         if (mcopy != NULL)
424                 m_freem(mcopy);
425 }
426
427 static int
428 ipx_do_route(src, ro)
429 struct ipx_addr *src;
430 struct route *ro;
431 {
432         struct sockaddr_ipx *dst;
433
434         bzero((caddr_t)ro, sizeof(*ro));
435         dst = (struct sockaddr_ipx *)&ro->ro_dst;
436
437         dst->sipx_len = sizeof(*dst);
438         dst->sipx_family = AF_IPX;
439         dst->sipx_addr = *src;
440         dst->sipx_addr.x_port = 0;
441         rtalloc(ro);
442         if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
443                 return (0);
444         }
445         ro->ro_rt->rt_use++;
446         return (1);
447 }
448
449 static void
450 ipx_undo_route(ro)
451 struct route *ro;
452 {
453         if (ro->ro_rt != NULL) {
454                 RTFREE(ro->ro_rt);
455         }
456 }
457
458 void
459 ipx_watch_output(m, ifp)
460 struct mbuf *m;
461 struct ifnet *ifp;
462 {
463         struct ipxpcb *ipxp;
464         struct ifaddr *ifa;
465         struct ipx_ifaddr *ia;
466         /*
467          * Give any raw listeners a crack at the packet
468          */
469         for (ipxp = ipxrawpcb.ipxp_next; ipxp != &ipxrawpcb;
470              ipxp = ipxp->ipxp_next) {
471                 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
472                 if (m0 != NULL) {
473                         struct ipx *ipx;
474
475                         M_PREPEND(m0, sizeof(*ipx), M_DONTWAIT);
476                         if (m0 == NULL)
477                                 continue;
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)
482                                         break;
483                         if (ia == NULL)
484                                 ipx->ipx_sna.x_host = ipx_zerohost;  
485                         else 
486                                 ipx->ipx_sna.x_host =
487                                     ia->ia_addr.sipx_addr.x_host;
488
489                         if (ifp != NULL && (ifp->if_flags & IFF_POINTOPOINT))
490                             TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
491                                 if (ifa->ifa_addr->sa_family == AF_IPX) {
492                                     ipx->ipx_sna = IA_SIPX(ifa)->sipx_addr;
493                                     break;
494                                 }
495                             }
496                         ipx->ipx_len = ntohl(m0->m_pkthdr.len);
497                         ipx_input(m0, ipxp);
498                 }
499         }
500 }