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