Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[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. 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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  *      @(#)ipx_input.c
31  *
32  * $FreeBSD: src/sys/netipx/ipx_input.c,v 1.22.2.2 2001/02/22 09:44:18 bp Exp $
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/mbuf.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>
43
44 #include <sys/thread2.h>
45 #include <sys/msgport2.h>
46 #include <sys/mplock2.h>
47
48 #include <net/if.h>
49 #include <net/route.h>
50 #include <net/netisr.h>
51
52 #include "ipx.h"
53 #include "spx.h"
54 #include "ipx_if.h"
55 #include "ipx_pcb.h"
56 #include "ipx_var.h"
57
58 int     ipxcksum = 0;
59 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, checksum, CTLFLAG_RW,
60            &ipxcksum, 0, "");
61
62 static int      ipxprintfs = 0;         /* printing forwarding information */
63 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxprintfs, CTLFLAG_RW,
64            &ipxprintfs, 0, "");
65
66 static int      ipxforwarding = 0;
67 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxforwarding, CTLFLAG_RW,
68             &ipxforwarding, 0, "");
69
70 static int      ipxnetbios = 0;
71 SYSCTL_INT(_net_ipx, OID_AUTO, ipxnetbios, CTLFLAG_RW,
72            &ipxnetbios, 0, "");
73
74 union   ipx_net ipx_zeronet;
75 union   ipx_host ipx_zerohost;
76
77 union   ipx_net ipx_broadnet;
78 union   ipx_host ipx_broadhost;
79
80 struct  ipxstat ipxstat;
81 struct  sockaddr_ipx ipx_netmask, ipx_hostmask;
82
83 static  u_short allones[] = {-1, -1, -1};
84
85 struct  ipxpcbhead ipxpcb_list;
86 struct  ipxpcbhead ipxrawpcb_list;
87
88 long    ipx_pexseq;
89
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);
94
95 /*
96  * IPX initialization.
97  */
98
99 void
100 ipx_init(void)
101 {
102         ipx_broadnet = *(union ipx_net *)allones;
103         ipx_broadhost = *(union ipx_host *)allones;
104
105         read_random(&ipx_pexseq, sizeof ipx_pexseq);
106         LIST_INIT(&ipxpcb_list);
107         LIST_INIT(&ipxrawpcb_list);
108
109         ipx_netmask.sipx_len = 6;
110         ipx_netmask.sipx_addr.x_net = ipx_broadnet;
111
112         ipx_hostmask.sipx_len = 12;
113         ipx_hostmask.sipx_addr.x_net = ipx_broadnet;
114         ipx_hostmask.sipx_addr.x_host = ipx_broadhost;
115
116         netisr_register(NETISR_IPX, ipxintr, NULL);
117 }
118
119 /*
120  * IPX input routine.  Pass to next level.
121  */
122 static void
123 ipxintr(netmsg_t msg)
124 {
125         struct mbuf *m = msg->packet.nm_packet;
126         struct ipx *ipx;
127         struct ipxpcb *ipxp;
128         struct ipx_ifaddr *ia;
129         int len;
130
131         get_mplock();
132
133         /*
134          * If no IPX addresses have been set yet but the interfaces
135          * are receiving, can't do anything with incoming packets yet.
136          */
137         if (ipx_ifaddr == NULL)
138                 goto bad;
139
140         ipxstat.ipxs_total++;
141
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++;
145                 goto out;
146         }
147
148         /*
149          * Give any raw listeners a crack at the packet
150          */
151         LIST_FOREACH(ipxp, &ipxrawpcb_list, ipxp_list) {
152                 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
153                 if (m1 != NULL)
154                         ipx_input(m1, ipxp);
155         }
156
157         ipx = mtod(m, struct ipx *);
158         len = ntohs(ipx->ipx_len);
159         /*
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.
164          */
165         if (m->m_pkthdr.len < len) {
166                 ipxstat.ipxs_tooshort++;
167                 goto bad;
168         }
169         if (m->m_pkthdr.len > len) {
170                 if (m->m_len == m->m_pkthdr.len) {
171                         m->m_len = len;
172                         m->m_pkthdr.len = len;
173                 } else
174                         m_adj(m, len - m->m_pkthdr.len);
175         }
176         if (ipxcksum && ipx->ipx_sum != 0xffff) {
177                 if (ipx->ipx_sum != ipx_cksum(m, len)) {
178                         ipxstat.ipxs_badsum++;
179                         goto bad;
180                 }
181         }
182
183         /*
184          * Propagated (Netbios) packets (type 20) has to be handled
185          * different. :-(
186          */
187         if (ipx->ipx_pt == IPXPROTO_NETBIOS) {
188                 if (ipxnetbios) {
189                         ipx_output_type20(m);
190                         goto out;
191                 } else
192                         goto bad;
193         }
194
195         /*
196          * Is this a directed broadcast?
197          */
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)) ) {
203                         /*
204                          * If it is a broadcast to the net where it was
205                          * received from, treat it as ours.
206                          */
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, 
210                                              ipx->ipx_dna))
211                                         goto ours;
212
213                         /*
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
219                          * would die anyway.
220                          *
221                          * Suggestion of Bill Nesheim, Cornell U.
222                          */
223                         if (ipx->ipx_tc < IPX_MAXHOPS) {
224                                 ipx_forward(m);
225                                 goto out;
226                         }
227                 }
228         /*
229          * Is this our packet? If not, forward.
230          */
231         } else {
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)))
236                                 break;
237
238                 if (ia == NULL) {
239                         ipx_forward(m);
240                         goto out;
241                 }
242         }
243 ours:
244         /*
245          * Locate pcb for datagram.
246          */
247         ipxp = ipx_pcblookup(&ipx->ipx_sna, ipx->ipx_dna.x_port, IPX_WILDCARD);
248         /*
249          * Switch out to protocol's input routine.
250          */
251         if (ipxp != NULL) {
252                 ipxstat.ipxs_delivered++;
253                 if ((ipxp->ipxp_flags & IPXP_ALL_PACKETS) == 0)
254                         switch (ipx->ipx_pt) {
255                         case IPXPROTO_SPX:
256                                 spx_input(m, ipxp);
257                                 goto out;
258                         }
259                 ipx_input(m, ipxp);
260         } else
261                 goto bad;
262
263         goto out;
264
265 bad:
266         m_freem(m);
267 out:
268         rel_mplock();
269         /* msg was embedded in the mbuf, do not reply! */
270 }
271
272 /*
273  * Parameters:
274  *      arg_as_sa:      XXX should be swapped with dummy
275  */
276 void
277 ipx_ctlinput(netmsg_t msg)
278 {
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;
283
284         if (cmd < 0 || cmd > PRC_NCMDS)
285                 goto out;
286
287         switch (cmd) {
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                         break;
294                 break;
295
296         default:
297                 if (ipxprintfs)
298                         kprintf("ipx_ctlinput: cmd %d.\n", cmd);
299                 break;
300         }
301 out:
302         lwkt_replymsg(&msg->lmsg, 0);
303 }
304
305 /*
306  * Forward a packet. If some error occurs drop the packet. IPX don't
307  * have a way to return errors to the sender.
308  */
309
310 static struct route ipx_droute;
311 static struct route ipx_sroute;
312
313 static void
314 ipx_forward(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                         kprintf("forward: ");
390                         ipx_printhost(&ipx->ipx_sna);
391                         kprintf(" to ");
392                         ipx_printhost(&ipx->ipx_dna);
393                         kprintf(" 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(struct ipx_addr *src, struct route *ro)
429 {
430         struct sockaddr_ipx *dst;
431
432         bzero((caddr_t)ro, sizeof(*ro));
433         dst = (struct sockaddr_ipx *)&ro->ro_dst;
434
435         dst->sipx_len = sizeof(*dst);
436         dst->sipx_family = AF_IPX;
437         dst->sipx_addr = *src;
438         dst->sipx_addr.x_port = 0;
439         rtalloc(ro);
440         if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
441                 return (0);
442         }
443         ro->ro_rt->rt_use++;
444         return (1);
445 }
446
447 static void
448 ipx_undo_route(struct route *ro)
449 {
450         if (ro->ro_rt != NULL) {
451                 RTFREE(ro->ro_rt);
452         }
453 }
454
455 void
456 ipx_watch_output(struct mbuf *m, struct ifnet *ifp)
457 {
458         struct ipxpcb *ipxp;
459         struct ipx_ifaddr *ia;
460
461         /*
462          * Give any raw listeners a crack at the packet
463          */
464         LIST_FOREACH(ipxp, &ipxrawpcb_list, ipxp_list) {
465                 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
466                 if (m0 != NULL) {
467                         struct ipx *ipx;
468
469                         M_PREPEND(m0, sizeof(*ipx), MB_DONTWAIT);
470                         if (m0 == NULL)
471                                 continue;
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)
476                                         break;
477                         if (ia == NULL)
478                                 ipx->ipx_sna.x_host = ipx_zerohost;  
479                         else 
480                                 ipx->ipx_sna.x_host =
481                                     ia->ia_addr.sipx_addr.x_host;
482
483                         if (ifp != NULL && (ifp->if_flags & IFF_POINTOPOINT)) {
484                             struct ifaddr_container *ifac;
485
486                             TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
487                                           ifa_link) {
488                                 struct ifaddr *ifa = ifac->ifa;
489
490                                 if (ifa->ifa_addr->sa_family == AF_IPX) {
491                                     ipx->ipx_sna = IA_SIPX(ifa)->sipx_addr;
492                                     break;
493                                 }
494                             }
495                         }
496                         ipx->ipx_len = ntohl(m0->m_pkthdr.len);
497                         ipx_input(m0, ipxp);
498                 }
499         }
500 }