The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netipx/ipx_input.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 3ab747ccc9a15744b1e55de5700729e1


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.