kernel: Use NULL for pointers.
[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  */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/mbuf.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>
47
48 #include <sys/thread2.h>
49 #include <sys/msgport2.h>
50 #include <sys/mplock2.h>
51
52 #include <net/if.h>
53 #include <net/route.h>
54 #include <net/netisr.h>
55
56 #include "ipx.h"
57 #include "spx.h"
58 #include "ipx_if.h"
59 #include "ipx_pcb.h"
60 #include "ipx_var.h"
61
62 int     ipxcksum = 0;
63 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, checksum, CTLFLAG_RW,
64            &ipxcksum, 0, "");
65
66 static int      ipxprintfs = 0;         /* printing forwarding information */
67 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxprintfs, CTLFLAG_RW,
68            &ipxprintfs, 0, "");
69
70 static int      ipxforwarding = 0;
71 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxforwarding, CTLFLAG_RW,
72             &ipxforwarding, 0, "");
73
74 static int      ipxnetbios = 0;
75 SYSCTL_INT(_net_ipx, OID_AUTO, ipxnetbios, CTLFLAG_RW,
76            &ipxnetbios, 0, "");
77
78 union   ipx_net ipx_zeronet;
79 union   ipx_host ipx_zerohost;
80
81 union   ipx_net ipx_broadnet;
82 union   ipx_host ipx_broadhost;
83
84 struct  ipxstat ipxstat;
85 struct  sockaddr_ipx ipx_netmask, ipx_hostmask;
86
87 static  u_short allones[] = {-1, -1, -1};
88
89 struct  ipxpcbhead ipxpcb_list;
90 struct  ipxpcbhead ipxrawpcb_list;
91
92 long    ipx_pexseq;
93
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);
98
99 /*
100  * IPX initialization.
101  */
102
103 void
104 ipx_init(void)
105 {
106         ipx_broadnet = *(union ipx_net *)allones;
107         ipx_broadhost = *(union ipx_host *)allones;
108
109         read_random(&ipx_pexseq, sizeof ipx_pexseq);
110         LIST_INIT(&ipxpcb_list);
111         LIST_INIT(&ipxrawpcb_list);
112
113         ipx_netmask.sipx_len = 6;
114         ipx_netmask.sipx_addr.x_net = ipx_broadnet;
115
116         ipx_hostmask.sipx_len = 12;
117         ipx_hostmask.sipx_addr.x_net = ipx_broadnet;
118         ipx_hostmask.sipx_addr.x_host = ipx_broadhost;
119
120         netisr_register(NETISR_IPX, ipxintr, NULL);
121 }
122
123 /*
124  * IPX input routine.  Pass to next level.
125  */
126 static void
127 ipxintr(netmsg_t msg)
128 {
129         struct mbuf *m = msg->packet.nm_packet;
130         struct ipx *ipx;
131         struct ipxpcb *ipxp;
132         struct ipx_ifaddr *ia;
133         int len;
134
135         get_mplock();
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))) == NULL) {
148                 ipxstat.ipxs_toosmall++;
149                 goto out;
150         }
151
152         /*
153          * Give any raw listeners a crack at the packet
154          */
155         LIST_FOREACH(ipxp, &ipxrawpcb_list, ipxp_list) {
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         rel_mplock();
273         /* msg was embedded in the mbuf, do not reply! */
274 }
275
276 /*
277  * Parameters:
278  *      arg_as_sa:      XXX should be swapped with dummy
279  */
280 void
281 ipx_ctlinput(netmsg_t msg)
282 {
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;
288
289         if (cmd < 0 || cmd > PRC_NCMDS)
290                 goto out;
291
292         switch (cmd) {
293         case PRC_IFDOWN:
294         case PRC_HOSTDEAD:
295         case PRC_HOSTUNREACH:
296                 sipx = (struct sockaddr_ipx *)arg;
297                 if (sipx->sipx_family != AF_IPX)
298                         break;
299                 ipx = &sipx->sipx_addr;
300                 break;
301
302         default:
303                 if (ipxprintfs)
304                         kprintf("ipx_ctlinput: cmd %d.\n", cmd);
305                 break;
306         }
307 out:
308         lwkt_replymsg(&msg->lmsg, 0);
309 }
310
311 /*
312  * Forward a packet. If some error occurs drop the packet. IPX don't
313  * have a way to return errors to the sender.
314  */
315
316 static struct route ipx_droute;
317 static struct route ipx_sroute;
318
319 static void
320 ipx_forward(struct mbuf *m)
321 {
322         struct ipx *ipx = mtod(m, struct ipx *);
323         int error;
324         struct mbuf *mcopy = NULL;
325         int agedelta = 1;
326         int flags = IPX_FORWARDING;
327         int ok_there = 0;
328         int ok_back = 0;
329
330         if (ipxforwarding == 0) {
331                 /* can't tell difference between net and host */
332                 ipxstat.ipxs_cantforward++;
333                 m_freem(m);
334                 goto cleanup;
335         }
336         ipx->ipx_tc++;
337         if (ipx->ipx_tc > IPX_MAXHOPS) {
338                 ipxstat.ipxs_cantforward++;
339                 m_freem(m);
340                 goto cleanup;
341         }
342
343         if ((ok_there = ipx_do_route(&ipx->ipx_dna,&ipx_droute)) == 0) {
344                 ipxstat.ipxs_noroute++;
345                 m_freem(m);
346                 goto cleanup;
347         }
348         /*
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.
354          */
355         if (ipx->ipx_dna.x_host.c_host[0] & 0x1) {
356                 struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
357                 struct ifnet *ifp;
358                 if (ia != NULL) {
359                         /* I'm gonna hafta eat this packet */
360                         agedelta += IPX_MAXHOPS - ipx->ipx_tc;
361                         ipx->ipx_tc = IPX_MAXHOPS;
362                 }
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++;
366                         m_freem(m);
367                         goto cleanup;
368                 }
369                 if (ipx_droute.ro_rt &&
370                     (ifp = ipx_droute.ro_rt->rt_ifp) &&
371                     ipx_sroute.ro_rt &&
372                     (ifp != ipx_sroute.ro_rt->rt_ifp)) {
373                         flags |= IPX_ALLOWBROADCAST;
374                 } else {
375                         ipxstat.ipxs_noroute++;
376                         m_freem(m);
377                         goto cleanup;
378                 }
379         }
380         /*
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
384          */
385 #if 0
386         if (!ipxcksum)
387                 ipx->ipx_sum = 0xffff;
388 #endif
389
390         error = ipx_outputfl(m, &ipx_droute, flags);
391         if (error == 0) {
392                 ipxstat.ipxs_forward++;
393
394                 if (ipxprintfs) {
395                         kprintf("forward: ");
396                         ipx_printhost(&ipx->ipx_sna);
397                         kprintf(" to ");
398                         ipx_printhost(&ipx->ipx_dna);
399                         kprintf(" hops %d\n", ipx->ipx_tc);
400                 }
401         } else if (mcopy != NULL) {
402                 ipx = mtod(mcopy, struct ipx *);
403                 switch (error) {
404
405                 case ENETUNREACH:
406                 case EHOSTDOWN:
407                 case EHOSTUNREACH:
408                 case ENETDOWN:
409                 case EPERM:
410                         ipxstat.ipxs_noroute++;
411                         break;
412
413                 case EMSGSIZE:
414                         ipxstat.ipxs_mtutoosmall++;
415                         break;
416
417                 case ENOBUFS:
418                         ipxstat.ipxs_odropped++;
419                         break;
420                 }
421                 mcopy = NULL;
422                 m_freem(m);
423         }
424 cleanup:
425         if (ok_there)
426                 ipx_undo_route(&ipx_droute);
427         if (ok_back)
428                 ipx_undo_route(&ipx_sroute);
429         if (mcopy != NULL)
430                 m_freem(mcopy);
431 }
432
433 static int
434 ipx_do_route(struct ipx_addr *src, struct route *ro)
435 {
436         struct sockaddr_ipx *dst;
437
438         bzero((caddr_t)ro, sizeof(*ro));
439         dst = (struct sockaddr_ipx *)&ro->ro_dst;
440
441         dst->sipx_len = sizeof(*dst);
442         dst->sipx_family = AF_IPX;
443         dst->sipx_addr = *src;
444         dst->sipx_addr.x_port = 0;
445         rtalloc(ro);
446         if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
447                 return (0);
448         }
449         ro->ro_rt->rt_use++;
450         return (1);
451 }
452
453 static void
454 ipx_undo_route(struct route *ro)
455 {
456         if (ro->ro_rt != NULL) {
457                 RTFREE(ro->ro_rt);
458         }
459 }
460
461 void
462 ipx_watch_output(struct mbuf *m, struct ifnet *ifp)
463 {
464         struct ipxpcb *ipxp;
465         struct ipx_ifaddr *ia;
466
467         /*
468          * Give any raw listeners a crack at the packet
469          */
470         LIST_FOREACH(ipxp, &ipxrawpcb_list, ipxp_list) {
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), MB_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                             struct ifaddr_container *ifac;
491
492                             TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
493                                           ifa_link) {
494                                 struct ifaddr *ifa = ifac->ifa;
495
496                                 if (ifa->ifa_addr->sa_family == AF_IPX) {
497                                     ipx->ipx_sna = IA_SIPX(ifa)->sipx_addr;
498                                     break;
499                                 }
500                             }
501                         }
502                         ipx->ipx_len = ntohl(m0->m_pkthdr.len);
503                         ipx_input(m0, ipxp);
504                 }
505         }
506 }