Merge branch 'vendor/LDNS'
[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.20 2008/09/24 14:26:39 sephe 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 <sys/thread2.h>
50 #include <sys/msgport2.h>
51 #include <sys/mplock2.h>
52
53 #include <net/if.h>
54 #include <net/route.h>
55 #include <net/netisr.h>
56
57 #include "ipx.h"
58 #include "spx.h"
59 #include "ipx_if.h"
60 #include "ipx_pcb.h"
61 #include "ipx_var.h"
62
63 int     ipxcksum = 0;
64 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, checksum, CTLFLAG_RW,
65            &ipxcksum, 0, "");
66
67 static int      ipxprintfs = 0;         /* printing forwarding information */
68 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxprintfs, CTLFLAG_RW,
69            &ipxprintfs, 0, "");
70
71 static int      ipxforwarding = 0;
72 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxforwarding, CTLFLAG_RW,
73             &ipxforwarding, 0, "");
74
75 static int      ipxnetbios = 0;
76 SYSCTL_INT(_net_ipx, OID_AUTO, ipxnetbios, CTLFLAG_RW,
77            &ipxnetbios, 0, "");
78
79 union   ipx_net ipx_zeronet;
80 union   ipx_host ipx_zerohost;
81
82 union   ipx_net ipx_broadnet;
83 union   ipx_host ipx_broadhost;
84
85 struct  ipxstat ipxstat;
86 struct  sockaddr_ipx ipx_netmask, ipx_hostmask;
87
88 static  u_short allones[] = {-1, -1, -1};
89
90 struct  ipxpcbhead ipxpcb_list;
91 struct  ipxpcbhead ipxrawpcb_list;
92
93 long    ipx_pexseq;
94
95 static  void ipxintr(netmsg_t msg);
96 static  int ipx_do_route(struct ipx_addr *src, struct route *ro);
97 static  void ipx_undo_route(struct route *ro);
98 static  void ipx_forward(struct mbuf *m);
99
100 /*
101  * IPX initialization.
102  */
103
104 void
105 ipx_init(void)
106 {
107         ipx_broadnet = *(union ipx_net *)allones;
108         ipx_broadhost = *(union ipx_host *)allones;
109
110         read_random(&ipx_pexseq, sizeof ipx_pexseq);
111         LIST_INIT(&ipxpcb_list);
112         LIST_INIT(&ipxrawpcb_list);
113
114         ipx_netmask.sipx_len = 6;
115         ipx_netmask.sipx_addr.x_net = ipx_broadnet;
116
117         ipx_hostmask.sipx_len = 12;
118         ipx_hostmask.sipx_addr.x_net = ipx_broadnet;
119         ipx_hostmask.sipx_addr.x_host = ipx_broadhost;
120
121         netisr_register(NETISR_IPX, ipxintr, NULL);
122 }
123
124 /*
125  * IPX input routine.  Pass to next level.
126  */
127 static void
128 ipxintr(netmsg_t msg)
129 {
130         struct mbuf *m = msg->packet.nm_packet;
131         struct ipx *ipx;
132         struct ipxpcb *ipxp;
133         struct ipx_ifaddr *ia;
134         int len;
135
136         get_mplock();
137
138         /*
139          * If no IPX addresses have been set yet but the interfaces
140          * are receiving, can't do anything with incoming packets yet.
141          */
142         if (ipx_ifaddr == NULL)
143                 goto bad;
144
145         ipxstat.ipxs_total++;
146
147         if ((m->m_flags & M_EXT || m->m_len < sizeof(struct ipx)) &&
148             (m = m_pullup(m, sizeof(struct ipx))) == 0) {
149                 ipxstat.ipxs_toosmall++;
150                 goto out;
151         }
152
153         /*
154          * Give any raw listeners a crack at the packet
155          */
156         LIST_FOREACH(ipxp, &ipxrawpcb_list, ipxp_list) {
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                         goto out;
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                                 goto out;
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                         goto out;
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                                 goto out;
263                         }
264                 ipx_input(m, ipxp);
265         } else
266                 goto bad;
267
268         goto out;
269
270 bad:
271         m_freem(m);
272 out:
273         rel_mplock();
274         /* msg was embedded in the mbuf, do not reply! */
275 }
276
277 /*
278  * Parameters:
279  *      arg_as_sa:      XXX should be swapped with dummy
280  */
281 void
282 ipx_ctlinput(netmsg_t msg)
283 {
284         int cmd = msg->ctlinput.nm_cmd;
285         struct sockaddr *arg_as_sa = msg->ctlinput.nm_arg;
286         caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
287         struct ipx_addr *ipx;
288         struct sockaddr_ipx *sipx;
289
290         if (cmd < 0 || cmd > PRC_NCMDS)
291                 goto out;
292
293         switch (cmd) {
294         case PRC_IFDOWN:
295         case PRC_HOSTDEAD:
296         case PRC_HOSTUNREACH:
297                 sipx = (struct sockaddr_ipx *)arg;
298                 if (sipx->sipx_family != AF_IPX)
299                         break;
300                 ipx = &sipx->sipx_addr;
301                 break;
302
303         default:
304                 if (ipxprintfs)
305                         kprintf("ipx_ctlinput: cmd %d.\n", cmd);
306                 break;
307         }
308 out:
309         lwkt_replymsg(&msg->lmsg, 0);
310 }
311
312 /*
313  * Forward a packet. If some error occurs drop the packet. IPX don't
314  * have a way to return errors to the sender.
315  */
316
317 static struct route ipx_droute;
318 static struct route ipx_sroute;
319
320 static void
321 ipx_forward(struct mbuf *m)
322 {
323         struct ipx *ipx = mtod(m, struct ipx *);
324         int error;
325         struct mbuf *mcopy = NULL;
326         int agedelta = 1;
327         int flags = IPX_FORWARDING;
328         int ok_there = 0;
329         int ok_back = 0;
330
331         if (ipxforwarding == 0) {
332                 /* can't tell difference between net and host */
333                 ipxstat.ipxs_cantforward++;
334                 m_freem(m);
335                 goto cleanup;
336         }
337         ipx->ipx_tc++;
338         if (ipx->ipx_tc > IPX_MAXHOPS) {
339                 ipxstat.ipxs_cantforward++;
340                 m_freem(m);
341                 goto cleanup;
342         }
343
344         if ((ok_there = ipx_do_route(&ipx->ipx_dna,&ipx_droute)) == 0) {
345                 ipxstat.ipxs_noroute++;
346                 m_freem(m);
347                 goto cleanup;
348         }
349         /*
350          * Here we think about  forwarding  broadcast packets,
351          * so we try to insure that it doesn't go back out
352          * on the interface it came in on.  Also, if we
353          * are going to physically broadcast this, let us
354          * age the packet so we can eat it safely the second time around.
355          */
356         if (ipx->ipx_dna.x_host.c_host[0] & 0x1) {
357                 struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
358                 struct ifnet *ifp;
359                 if (ia != NULL) {
360                         /* I'm gonna hafta eat this packet */
361                         agedelta += IPX_MAXHOPS - ipx->ipx_tc;
362                         ipx->ipx_tc = IPX_MAXHOPS;
363                 }
364                 if ((ok_back = ipx_do_route(&ipx->ipx_sna,&ipx_sroute)) == 0) {
365                         /* error = ENETUNREACH; He'll never get it! */
366                         ipxstat.ipxs_noroute++;
367                         m_freem(m);
368                         goto cleanup;
369                 }
370                 if (ipx_droute.ro_rt &&
371                     (ifp = ipx_droute.ro_rt->rt_ifp) &&
372                     ipx_sroute.ro_rt &&
373                     (ifp != ipx_sroute.ro_rt->rt_ifp)) {
374                         flags |= IPX_ALLOWBROADCAST;
375                 } else {
376                         ipxstat.ipxs_noroute++;
377                         m_freem(m);
378                         goto cleanup;
379                 }
380         }
381         /*
382          * We don't need to recompute checksum because ipx_tc field
383          * is ignored by checksum calculation routine, however
384          * it may be desirable to reset checksum if ipxcksum == 0
385          */
386 #if 0
387         if (!ipxcksum)
388                 ipx->ipx_sum = 0xffff;
389 #endif
390
391         error = ipx_outputfl(m, &ipx_droute, flags);
392         if (error == 0) {
393                 ipxstat.ipxs_forward++;
394
395                 if (ipxprintfs) {
396                         kprintf("forward: ");
397                         ipx_printhost(&ipx->ipx_sna);
398                         kprintf(" to ");
399                         ipx_printhost(&ipx->ipx_dna);
400                         kprintf(" hops %d\n", ipx->ipx_tc);
401                 }
402         } else if (mcopy != NULL) {
403                 ipx = mtod(mcopy, struct ipx *);
404                 switch (error) {
405
406                 case ENETUNREACH:
407                 case EHOSTDOWN:
408                 case EHOSTUNREACH:
409                 case ENETDOWN:
410                 case EPERM:
411                         ipxstat.ipxs_noroute++;
412                         break;
413
414                 case EMSGSIZE:
415                         ipxstat.ipxs_mtutoosmall++;
416                         break;
417
418                 case ENOBUFS:
419                         ipxstat.ipxs_odropped++;
420                         break;
421                 }
422                 mcopy = NULL;
423                 m_freem(m);
424         }
425 cleanup:
426         if (ok_there)
427                 ipx_undo_route(&ipx_droute);
428         if (ok_back)
429                 ipx_undo_route(&ipx_sroute);
430         if (mcopy != NULL)
431                 m_freem(mcopy);
432 }
433
434 static int
435 ipx_do_route(struct ipx_addr *src, struct route *ro)
436 {
437         struct sockaddr_ipx *dst;
438
439         bzero((caddr_t)ro, sizeof(*ro));
440         dst = (struct sockaddr_ipx *)&ro->ro_dst;
441
442         dst->sipx_len = sizeof(*dst);
443         dst->sipx_family = AF_IPX;
444         dst->sipx_addr = *src;
445         dst->sipx_addr.x_port = 0;
446         rtalloc(ro);
447         if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
448                 return (0);
449         }
450         ro->ro_rt->rt_use++;
451         return (1);
452 }
453
454 static void
455 ipx_undo_route(struct route *ro)
456 {
457         if (ro->ro_rt != NULL) {
458                 RTFREE(ro->ro_rt);
459         }
460 }
461
462 void
463 ipx_watch_output(struct mbuf *m, struct ifnet *ifp)
464 {
465         struct ipxpcb *ipxp;
466         struct ipx_ifaddr *ia;
467
468         /*
469          * Give any raw listeners a crack at the packet
470          */
471         LIST_FOREACH(ipxp, &ipxrawpcb_list, ipxp_list) {
472                 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
473                 if (m0 != NULL) {
474                         struct ipx *ipx;
475
476                         M_PREPEND(m0, sizeof(*ipx), MB_DONTWAIT);
477                         if (m0 == NULL)
478                                 continue;
479                         ipx = mtod(m0, struct ipx *);
480                         ipx->ipx_sna.x_net = ipx_zeronet;
481                         for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
482                                 if (ifp == ia->ia_ifp)
483                                         break;
484                         if (ia == NULL)
485                                 ipx->ipx_sna.x_host = ipx_zerohost;  
486                         else 
487                                 ipx->ipx_sna.x_host =
488                                     ia->ia_addr.sipx_addr.x_host;
489
490                         if (ifp != NULL && (ifp->if_flags & IFF_POINTOPOINT)) {
491                             struct ifaddr_container *ifac;
492
493                             TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
494                                           ifa_link) {
495                                 struct ifaddr *ifa = ifac->ifa;
496
497                                 if (ifa->ifa_addr->sa_family == AF_IPX) {
498                                     ipx->ipx_sna = IA_SIPX(ifa)->sipx_addr;
499                                     break;
500                                 }
501                             }
502                         }
503                         ipx->ipx_len = ntohl(m0->m_pkthdr.len);
504                         ipx_input(m0, ipxp);
505                 }
506         }
507 }