]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netipx/ipx_input.c
This commit was generated by cvs2svn to compensate for changes in r121947,
[FreeBSD/FreeBSD.git] / sys / netipx / 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
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
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
53 #include <netipx/ipx.h>
54 #include <netipx/spx.h>
55 #include <netipx/ipx_if.h>
56 #include <netipx/ipx_pcb.h>
57 #include <netipx/ipx_var.h>
58
59 int     ipxcksum = 0;
60 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, checksum, CTLFLAG_RW,
61            &ipxcksum, 0, "");
62
63 static int      ipxprintfs = 0;         /* printing forwarding information */
64 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxprintfs, CTLFLAG_RW,
65            &ipxprintfs, 0, "");
66
67 static int      ipxforwarding = 0;
68 SYSCTL_INT(_net_ipx_ipx, OID_AUTO, ipxforwarding, CTLFLAG_RW,
69             &ipxforwarding, 0, "");
70
71 static int      ipxnetbios = 0;
72 SYSCTL_INT(_net_ipx, OID_AUTO, ipxnetbios, CTLFLAG_RW,
73            &ipxnetbios, 0, "");
74
75 union   ipx_net ipx_zeronet;
76 union   ipx_host ipx_zerohost;
77
78 union   ipx_net ipx_broadnet;
79 union   ipx_host ipx_broadhost;
80
81 struct  ipxstat ipxstat;
82 struct  sockaddr_ipx ipx_netmask, ipx_hostmask;
83
84 static  u_short allones[] = {-1, -1, -1};
85
86 struct  ipxpcb ipxpcb;
87 struct  ipxpcb ipxrawpcb;
88
89 static int ipxqmaxlen = IFQ_MAXLEN;
90 static  struct ifqueue ipxintrq;
91
92 long    ipx_pexseq;
93
94 static  int ipx_do_route(struct ipx_addr *src, struct route *ro);
95 static  void ipx_undo_route(struct route *ro);
96 static  void ipx_forward(struct mbuf *m);
97 static  void ipxintr(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         ipxintrq.ifq_maxlen = ipxqmaxlen;
121         mtx_init(&ipxintrq.ifq_mtx, "ipx_inq", NULL, MTX_DEF);
122         netisr_register(NETISR_IPX, ipxintr, &ipxintrq);
123 }
124
125 /*
126  * IPX input routine.  Pass to next level.
127  */
128 static void
129 ipxintr(struct mbuf *m)
130 {
131         register struct ipx *ipx;
132         register 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                 return;
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                         return;
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                                 return;
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                         return;
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                                 return;
262                         }
263                 ipx_input(m, ipxp);
264         } else
265                 goto bad;
266
267         return;
268
269 bad:
270         m_freem(m);
271 }
272
273 void
274 ipx_ctlinput(cmd, arg_as_sa, dummy)
275         int cmd;
276         struct sockaddr *arg_as_sa;     /* XXX should be swapped with dummy */
277         void *dummy;
278 {
279         caddr_t arg = (/* XXX */ caddr_t)arg_as_sa;
280         struct ipx_addr *ipx;
281
282         if (cmd < 0 || cmd >= PRC_NCMDS)
283                 return;
284         switch (cmd) {
285                 struct sockaddr_ipx *sipx;
286
287         case PRC_IFDOWN:
288         case PRC_HOSTDEAD:
289         case PRC_HOSTUNREACH:
290                 sipx = (struct sockaddr_ipx *)arg;
291                 if (sipx->sipx_family != AF_IPX)
292                         return;
293                 ipx = &sipx->sipx_addr;
294                 break;
295
296         default:
297                 if (ipxprintfs)
298                         printf("ipx_ctlinput: cmd %d.\n", cmd);
299                 break;
300         }
301 }
302
303 /*
304  * Forward a packet. If some error occurs drop the packet. IPX don't
305  * have a way to return errors to the sender.
306  */
307
308 static struct route ipx_droute;
309 static struct route ipx_sroute;
310
311 static void
312 ipx_forward(m)
313 struct mbuf *m;
314 {
315         register struct ipx *ipx = mtod(m, struct ipx *);
316         register int error;
317         struct mbuf *mcopy = NULL;
318         int agedelta = 1;
319         int flags = IPX_FORWARDING;
320         int ok_there = 0;
321         int ok_back = 0;
322
323         if (ipxforwarding == 0) {
324                 /* can't tell difference between net and host */
325                 ipxstat.ipxs_cantforward++;
326                 m_freem(m);
327                 goto cleanup;
328         }
329         ipx->ipx_tc++;
330         if (ipx->ipx_tc > IPX_MAXHOPS) {
331                 ipxstat.ipxs_cantforward++;
332                 m_freem(m);
333                 goto cleanup;
334         }
335
336         if ((ok_there = ipx_do_route(&ipx->ipx_dna,&ipx_droute)) == 0) {
337                 ipxstat.ipxs_noroute++;
338                 m_freem(m);
339                 goto cleanup;
340         }
341         /*
342          * Here we think about  forwarding  broadcast packets,
343          * so we try to insure that it doesn't go back out
344          * on the interface it came in on.  Also, if we
345          * are going to physically broadcast this, let us
346          * age the packet so we can eat it safely the second time around.
347          */
348         if (ipx->ipx_dna.x_host.c_host[0] & 0x1) {
349                 struct ipx_ifaddr *ia = ipx_iaonnetof(&ipx->ipx_dna);
350                 struct ifnet *ifp;
351                 if (ia != NULL) {
352                         /* I'm gonna hafta eat this packet */
353                         agedelta += IPX_MAXHOPS - ipx->ipx_tc;
354                         ipx->ipx_tc = IPX_MAXHOPS;
355                 }
356                 if ((ok_back = ipx_do_route(&ipx->ipx_sna,&ipx_sroute)) == 0) {
357                         /* error = ENETUNREACH; He'll never get it! */
358                         ipxstat.ipxs_noroute++;
359                         m_freem(m);
360                         goto cleanup;
361                 }
362                 if (ipx_droute.ro_rt &&
363                     (ifp = ipx_droute.ro_rt->rt_ifp) &&
364                     ipx_sroute.ro_rt &&
365                     (ifp != ipx_sroute.ro_rt->rt_ifp)) {
366                         flags |= IPX_ALLOWBROADCAST;
367                 } else {
368                         ipxstat.ipxs_noroute++;
369                         m_freem(m);
370                         goto cleanup;
371                 }
372         }
373         /*
374          * We don't need to recompute checksum because ipx_tc field
375          * is ignored by checksum calculation routine, however
376          * it may be desirable to reset checksum if ipxcksum == 0
377          */
378 #if 0
379         if (!ipxcksum)
380                 ipx->ipx_sum = 0xffff;
381 #endif
382
383         error = ipx_outputfl(m, &ipx_droute, flags);
384         if (error == 0) {
385                 ipxstat.ipxs_forward++;
386
387                 if (ipxprintfs) {
388                         printf("forward: ");
389                         ipx_printhost(&ipx->ipx_sna);
390                         printf(" to ");
391                         ipx_printhost(&ipx->ipx_dna);
392                         printf(" hops %d\n", ipx->ipx_tc);
393                 }
394         } else if (mcopy != NULL) {
395                 ipx = mtod(mcopy, struct ipx *);
396                 switch (error) {
397
398                 case ENETUNREACH:
399                 case EHOSTDOWN:
400                 case EHOSTUNREACH:
401                 case ENETDOWN:
402                 case EPERM:
403                         ipxstat.ipxs_noroute++;
404                         break;
405
406                 case EMSGSIZE:
407                         ipxstat.ipxs_mtutoosmall++;
408                         break;
409
410                 case ENOBUFS:
411                         ipxstat.ipxs_odropped++;
412                         break;
413                 }
414                 mcopy = NULL;
415                 m_freem(m);
416         }
417 cleanup:
418         if (ok_there)
419                 ipx_undo_route(&ipx_droute);
420         if (ok_back)
421                 ipx_undo_route(&ipx_sroute);
422         if (mcopy != NULL)
423                 m_freem(mcopy);
424 }
425
426 static int
427 ipx_do_route(src, ro)
428 struct ipx_addr *src;
429 struct route *ro;
430 {
431         struct sockaddr_ipx *dst;
432
433         bzero((caddr_t)ro, sizeof(*ro));
434         dst = (struct sockaddr_ipx *)&ro->ro_dst;
435
436         dst->sipx_len = sizeof(*dst);
437         dst->sipx_family = AF_IPX;
438         dst->sipx_addr = *src;
439         dst->sipx_addr.x_port = 0;
440         rtalloc(ro);
441         if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
442                 return (0);
443         }
444         ro->ro_rt->rt_use++;
445         return (1);
446 }
447
448 static void
449 ipx_undo_route(ro)
450 register struct route *ro;
451 {
452         if (ro->ro_rt != NULL) {
453                 RTFREE(ro->ro_rt);
454         }
455 }
456
457 void
458 ipx_watch_output(m, ifp)
459 struct mbuf *m;
460 struct ifnet *ifp;
461 {
462         register struct ipxpcb *ipxp;
463         register struct ifaddr *ifa;
464         register struct ipx_ifaddr *ia;
465         /*
466          * Give any raw listeners a crack at the packet
467          */
468         for (ipxp = ipxrawpcb.ipxp_next; ipxp != &ipxrawpcb;
469              ipxp = ipxp->ipxp_next) {
470                 struct mbuf *m0 = m_copy(m, 0, (int)M_COPYALL);
471                 if (m0 != NULL) {
472                         register struct ipx *ipx;
473
474                         M_PREPEND(m0, sizeof(*ipx), M_DONTWAIT);
475                         if (m0 == NULL)
476                                 continue;
477                         ipx = mtod(m0, struct ipx *);
478                         ipx->ipx_sna.x_net = ipx_zeronet;
479                         for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
480                                 if (ifp == ia->ia_ifp)
481                                         break;
482                         if (ia == NULL)
483                                 ipx->ipx_sna.x_host = ipx_zerohost;  
484                         else 
485                                 ipx->ipx_sna.x_host =
486                                     ia->ia_addr.sipx_addr.x_host;
487
488                         if (ifp != NULL && (ifp->if_flags & IFF_POINTOPOINT))
489                             TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
490                                 if (ifa->ifa_addr->sa_family == AF_IPX) {
491                                     ipx->ipx_sna = IA_SIPX(ifa)->sipx_addr;
492                                     break;
493                                 }
494                             }
495                         ipx->ipx_len = ntohl(m0->m_pkthdr.len);
496                         ipx_input(m0, ipxp);
497                 }
498         }
499 }