Remove not needed void casts.
[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.12 2004/06/02 14:43:02 eirikn 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
52 #include <net/if.h>
53 #include <net/route.h>
54 #include <net/netisr.h>
55 #include <net/intrq.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  ipxpcb ipxpcb;
91 struct  ipxpcb ipxrawpcb;
92
93 long    ipx_pexseq;
94
95 static  int ipxintr(struct netmsg *);
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()
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         ipxpcb.ipxp_next = ipxpcb.ipxp_prev = &ipxpcb;
112         ipxrawpcb.ipxp_next = ipxrawpcb.ipxp_prev = &ipxrawpcb;
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, cpu0_portfn, ipxintr);
122 }
123
124 /*
125  * IPX input routine.  Pass to next level.
126  */
127 static int
128 ipxintr(struct netmsg *msg)
129 {
130         struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
131         struct ipx *ipx;
132         struct ipxpcb *ipxp;
133         struct ipx_ifaddr *ia;
134         int len;
135
136         /*
137          * If no IPX addresses have been set yet but the interfaces
138          * are receiving, can't do anything with incoming packets yet.
139          */
140         if (ipx_ifaddr == NULL)
141                 goto bad;
142
143         ipxstat.ipxs_total++;
144
145         if ((m->m_flags & M_EXT || m->m_len < sizeof(struct ipx)) &&
146             (m = m_pullup(m, sizeof(struct ipx))) == 0) {
147                 ipxstat.ipxs_toosmall++;
148                 goto out;
149         }
150
151         /*
152          * Give any raw listeners a crack at the packet
153          */
154         for (ipxp = ipxrawpcb.ipxp_next; ipxp != &ipxrawpcb;
155              ipxp = ipxp->ipxp_next) {
156                 struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL);
157                 if (m1 != NULL)
158                         ipx_input(m1, ipxp);
159         }
160
161         ipx = mtod(m, struct ipx *);
162         len = ntohs(ipx->ipx_len);
163         /*
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.
168          */
169         if (m->m_pkthdr.len < len) {
170                 ipxstat.ipxs_tooshort++;
171                 goto bad;
172         }
173         if (m->m_pkthdr.len > len) {
174                 if (m->m_len == m->m_pkthdr.len) {
175                         m->m_len = len;
176                         m->m_pkthdr.len = len;
177                 } else
178                         m_adj(m, len - m->m_pkthdr.len);
179         }
180         if (ipxcksum && ipx->ipx_sum != 0xffff) {
181                 if (ipx->ipx_sum != ipx_cksum(m, len)) {
182                         ipxstat.ipxs_badsum++;
183                         goto bad;
184                 }
185         }
186
187         /*
188          * Propagated (Netbios) packets (type 20) has to be handled
189          * different. :-(
190          */
191         if (ipx->ipx_pt == IPXPROTO_NETBIOS) {
192                 if (ipxnetbios) {
193                         ipx_output_type20(m);
194                         goto out;
195                 } else
196                         goto bad;
197         }
198
199         /*
200          * Is this a directed broadcast?
201          */
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)) ) {
207                         /*
208                          * If it is a broadcast to the net where it was
209                          * received from, treat it as ours.
210                          */
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, 
214                                              ipx->ipx_dna))
215                                         goto ours;
216
217                         /*
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
223                          * would die anyway.
224                          *
225                          * Suggestion of Bill Nesheim, Cornell U.
226                          */
227                         if (ipx->ipx_tc < IPX_MAXHOPS) {
228                                 ipx_forward(m);
229                                 goto out;
230                         }
231                 }
232         /*
233          * Is this our packet? If not, forward.
234          */
235         } else {
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)))
240                                 break;
241
242                 if (ia == NULL) {
243                         ipx_forward(m);
244                         goto out;
245                 }
246         }
247 ours:
248         /*
249          * Locate pcb for datagram.
250          */
251         ipxp = ipx_pcblookup(&ipx->ipx_sna, ipx->ipx_dna.x_port, IPX_WILDCARD);
252         /*
253          * Switch out to protocol's input routine.
254          */
255         if (ipxp != NULL) {
256                 ipxstat.ipxs_delivered++;
257                 if ((ipxp->ipxp_flags & IPXP_ALL_PACKETS) == 0)
258                         switch (ipx->ipx_pt) {
259                         case IPXPROTO_SPX:
260                                 spx_input(m, ipxp);
261                                 goto out;
262                         }
263                 ipx_input(m, ipxp);
264         } else
265                 goto bad;
266
267         goto out;
268
269 bad:
270         m_freem(m);
271 out:
272         lwkt_replymsg(&msg->nm_lmsg, 0);
273         return(EASYNC);
274 }
275
276 void
277 ipx_ctlinput(cmd, arg_as_sa, dummy)
278         int cmd;
279         struct sockaddr *arg_as_sa;     /* XXX should be swapped with dummy */
280         void *dummy;
281 {
282         caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
283         struct ipx_addr *ipx;
284
285         if (cmd < 0 || cmd > PRC_NCMDS)
286                 return;
287         switch (cmd) {
288                 struct sockaddr_ipx *sipx;
289
290         case PRC_IFDOWN:
291         case PRC_HOSTDEAD:
292         case PRC_HOSTUNREACH:
293                 sipx = (struct sockaddr_ipx *)arg;
294                 if (sipx->sipx_family != AF_IPX)
295                         return;
296                 ipx = &sipx->sipx_addr;
297                 break;
298
299         default:
300                 if (ipxprintfs)
301                         printf("ipx_ctlinput: cmd %d.\n", cmd);
302                 break;
303         }
304 }
305
306 /*
307  * Forward a packet. If some error occurs drop the packet. IPX don't
308  * have a way to return errors to the sender.
309  */
310
311 static struct route ipx_droute;
312 static struct route ipx_sroute;
313
314 static void
315 ipx_forward(m)
316 struct mbuf *m;
317 {
318         struct ipx *ipx = mtod(m, struct ipx *);
319         int error;
320         struct mbuf *mcopy = NULL;
321         int agedelta = 1;
322         int flags = IPX_FORWARDING;
323         int ok_there = 0;
324         int ok_back = 0;
325
326         if (ipxforwarding == 0) {
327                 /* can't tell difference between net and host */
328                 ipxstat.ipxs_cantforward++;
329                 m_freem(m);
330                 goto cleanup;
331         }
332         ipx->ipx_tc++;
333         if (ipx->ipx_tc > IPX_MAXHOPS) {
334                 ipxstat.ipxs_cantforward++;
335                 m_freem(m);
336                 goto cleanup;
337         }
338
339         if ((ok_there = ipx_do_route(&ipx->ipx_dna,&ipx_droute)) == 0) {
340                 ipxstat.ipxs_noroute++;
341                 m_freem(m);
342                 goto cleanup;
343         }
344         /*
345          * Here we think about  forwarding  broadcast packets,
346          * so we try to insure that it doesn't go back out
347          * on the interface it came in on.  Also, if we
348          * are going to physically broadcast this, let us
349          * age the packet so we can eat it safely the second time around.
350          */
351         if (ipx->ipx_dna.x_host.c_host[0] & 0x1) {
352                 struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
353                 struct ifnet *ifp;
354                 if (ia != NULL) {
355                         /* I'm gonna hafta eat this packet */
356                         agedelta += IPX_MAXHOPS - ipx->ipx_tc;
357                         ipx->ipx_tc = IPX_MAXHOPS;
358                 }
359                 if ((ok_back = ipx_do_route(&ipx->ipx_sna,&ipx_sroute)) == 0) {
360                         /* error = ENETUNREACH; He'll never get it! */
361                         ipxstat.ipxs_noroute++;
362                         m_freem(m);
363                         goto cleanup;
364                 }
365                 if (ipx_droute.ro_rt &&
366                     (ifp = ipx_droute.ro_rt->rt_ifp) &&
367                     ipx_sroute.ro_rt &&
368                     (ifp != ipx_sroute.ro_rt->rt_ifp)) {
369                         flags |= IPX_ALLOWBROADCAST;
370                 } else {
371                         ipxstat.ipxs_noroute++;
372                         m_freem(m);
373                         goto cleanup;
374                 }
375         }
376         /*
377          * We don't need to recompute checksum because ipx_tc field
378          * is ignored by checksum calculation routine, however
379          * it may be desirable to reset checksum if ipxcksum == 0
380          */
381 #if 0
382         if (!ipxcksum)
383                 ipx->ipx_sum = 0xffff;
384 #endif
385
386         error = ipx_outputfl(m, &ipx_droute, flags);
387         if (error == 0) {
388                 ipxstat.ipxs_forward++;
389
390                 if (ipxprintfs) {
391                         printf("forward: ");
392                         ipx_printhost(&ipx->ipx_sna);
393                         printf(" to ");
394                         ipx_printhost(&ipx->ipx_dna);
395                         printf(" hops %d\n", ipx->ipx_tc);
396                 }
397         } else if (mcopy != NULL) {
398                 ipx = mtod(mcopy, struct ipx *);
399                 switch (error) {
400
401                 case ENETUNREACH:
402                 case EHOSTDOWN:
403                 case EHOSTUNREACH:
404                 case ENETDOWN:
405                 case EPERM:
406                         ipxstat.ipxs_noroute++;
407                         break;
408
409                 case EMSGSIZE:
410                         ipxstat.ipxs_mtutoosmall++;
411                         break;
412
413                 case ENOBUFS:
414                         ipxstat.ipxs_odropped++;
415                         break;
416                 }
417                 mcopy = NULL;
418                 m_freem(m);
419         }
420 cleanup:
421         if (ok_there)
422                 ipx_undo_route(&ipx_droute);
423         if (ok_back)
424                 ipx_undo_route(&ipx_sroute);
425         if (mcopy != NULL)
426                 m_freem(mcopy);
427 }
428
429 static int
430 ipx_do_route(src, ro)
431 struct ipx_addr *src;
432 struct route *ro;
433 {
434         struct sockaddr_ipx *dst;
435
436         bzero((caddr_t)ro, sizeof(*ro));
437         dst = (struct sockaddr_ipx *)&ro->ro_dst;
438
439         dst->sipx_len = sizeof(*dst);
440         dst->sipx_family = AF_IPX;
441         dst->sipx_addr = *src;
442         dst->sipx_addr.x_port = 0;
443         rtalloc(ro);
444         if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
445                 return (0);
446         }
447         ro->ro_rt->rt_use++;
448         return (1);
449 }
450
451 static void
452 ipx_undo_route(ro)
453 struct route *ro;
454 {
455         if (ro->ro_rt != NULL) {
456                 RTFREE(ro->ro_rt);
457         }
458 }
459
460 void
461 ipx_watch_output(m, ifp)
462 struct mbuf *m;
463 struct ifnet *ifp;
464 {
465         struct ipxpcb *ipxp;
466         struct ifaddr *ifa;
467         struct ipx_ifaddr *ia;
468         /*
469          * Give any raw listeners a crack at the packet
470          */
471         for (ipxp = ipxrawpcb.ipxp_next; ipxp != &ipxrawpcb;
472              ipxp = ipxp->ipxp_next) {
473                 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
474                 if (m0 != NULL) {
475                         struct ipx *ipx;
476
477                         M_PREPEND(m0, sizeof(*ipx), MB_DONTWAIT);
478                         if (m0 == NULL)
479                                 continue;
480                         ipx = mtod(m0, struct ipx *);
481                         ipx->ipx_sna.x_net = ipx_zeronet;
482                         for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
483                                 if (ifp == ia->ia_ifp)
484                                         break;
485                         if (ia == NULL)
486                                 ipx->ipx_sna.x_host = ipx_zerohost;  
487                         else 
488                                 ipx->ipx_sna.x_host =
489                                     ia->ia_addr.sipx_addr.x_host;
490
491                         if (ifp != NULL && (ifp->if_flags & IFF_POINTOPOINT))
492                             TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
493                                 if (ifa->ifa_addr->sa_family == AF_IPX) {
494                                     ipx->ipx_sna = IA_SIPX(ifa)->sipx_addr;
495                                     break;
496                                 }
497                             }
498                         ipx->ipx_len = ntohl(m0->m_pkthdr.len);
499                         ipx_input(m0, ipxp);
500                 }
501         }
502 }