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_ip.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_ip.c
   35  */
   36 
   37 #include <sys/cdefs.h>
   38 __FBSDID("$FreeBSD: src/sys/netipx/ipx_ip.c,v 1.36.4.3 2005/02/25 13:26:39 rwatson Exp $");
   39 
   40 /*
   41  * Software interface driver for encapsulating IPX in IP.
   42  */
   43 
   44 #include "opt_inet.h"
   45 #include "opt_ipx.h"
   46 
   47 #ifdef IPXIP
   48 #ifndef INET
   49 #error The option IPXIP requires option INET.
   50 #endif
   51 
   52 #include <sys/param.h>
   53 #include <sys/systm.h>
   54 #include <sys/malloc.h>
   55 #include <sys/mbuf.h>
   56 #include <sys/protosw.h>
   57 #include <sys/socket.h>
   58 #include <sys/socketvar.h>
   59 #include <sys/sockio.h>
   60 
   61 #include <net/if.h>
   62 #include <net/netisr.h>
   63 #include <net/route.h>
   64 
   65 #include <netinet/in.h>
   66 #include <netinet/in_systm.h>
   67 #include <netinet/in_var.h>
   68 #include <netinet/ip.h>
   69 #include <netinet/ip_var.h>
   70 
   71 #include <netipx/ipx.h>
   72 #include <netipx/ipx_if.h>
   73 #include <netipx/ipx_ip.h>
   74 #include <netipx/ipx_var.h>
   75 
   76 static struct   ifnet ipxipif;
   77 static int      ipxipif_units;
   78 
   79 /* list of all hosts and gateways or broadcast addrs */
   80 static struct   ifnet_en *ipxip_list;
   81 
   82 static  struct ifnet_en *ipxipattach(void);
   83 static  int ipxip_free(struct ifnet *ifp);
   84 static  int ipxipioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
   85 static  int ipxipoutput(struct ifnet *ifp, struct mbuf *m,
   86                         struct sockaddr *dst, struct rtentry *rt);
   87 static  void ipxip_rtchange(struct in_addr *dst);
   88 static  void ipxipstart(struct ifnet *ifp);
   89 
   90 static struct ifnet_en *
   91 ipxipattach()
   92 {
   93         register struct ifnet_en *m;
   94         register struct ifnet *ifp;
   95 
   96         if (ipxipif.if_mtu == 0) {
   97                 ifp = &ipxipif;
   98                 if_initname(ifp, "ipxip", ipxipif_units);
   99                 ifp->if_mtu = LOMTU;
  100                 ifp->if_ioctl = ipxipioctl;
  101                 ifp->if_output = ipxipoutput;
  102                 ifp->if_start = ipxipstart;
  103                 ifp->if_flags = IFF_POINTOPOINT;
  104         }
  105 
  106         MALLOC((m), struct ifnet_en *, sizeof(*m), M_PCB, M_NOWAIT | M_ZERO);
  107         if (m == NULL)
  108                 return (NULL);
  109         m->ifen_next = ipxip_list;
  110         ipxip_list = m;
  111         ifp = &m->ifen_ifnet;
  112 
  113         if_initname(ifp, "ipxip", ipxipif_units++);
  114         ifp->if_mtu = LOMTU;
  115         ifp->if_ioctl = ipxipioctl;
  116         ifp->if_output = ipxipoutput;
  117         ifp->if_start = ipxipstart;
  118         ifp->if_flags = IFF_POINTOPOINT;
  119         if_attach(ifp);
  120 
  121         return (m);
  122 }
  123 
  124 
  125 /*
  126  * Process an ioctl request.
  127  */
  128 static int
  129 ipxipioctl(ifp, cmd, data)
  130         register struct ifnet *ifp;
  131         u_long cmd;
  132         caddr_t data;
  133 {
  134         int error = 0;
  135         struct ifreq *ifr;
  136 
  137         switch (cmd) {
  138 
  139         case SIOCSIFADDR:
  140                 ifp->if_flags |= IFF_UP;
  141                 /* FALLTHROUGH */
  142 
  143         case SIOCSIFDSTADDR:
  144                 /*
  145                  * Everything else is done at a higher level.
  146                  */
  147                 break;
  148 
  149         case SIOCSIFFLAGS:
  150                 ifr = (struct ifreq *)data;
  151                 if ((ifr->ifr_flags & IFF_UP) == 0)
  152                         error = ipxip_free(ifp);
  153 
  154 
  155         default:
  156                 error = EINVAL;
  157         }
  158         return (error);
  159 }
  160 
  161 static struct mbuf *ipxip_badlen;
  162 static struct mbuf *ipxip_lastin;
  163 static int ipxip_hold_input;
  164 
  165 void
  166 ipxip_input(m, hlen)
  167         register struct mbuf *m;
  168         int hlen;
  169 {
  170         register struct ip *ip;
  171         register struct ipx *ipx;
  172         int len, s;
  173 
  174         if (ipxip_hold_input) {
  175                 if (ipxip_lastin != NULL) {
  176                         m_freem(ipxip_lastin);
  177                 }
  178                 ipxip_lastin = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT);
  179         }
  180         /*
  181          * Get IP and IPX header together in first mbuf.
  182          */
  183         ipxipif.if_ipackets++;
  184         s = sizeof(struct ip) + sizeof(struct ipx);
  185         if (((m->m_flags & M_EXT) || m->m_len < s) &&
  186             (m = m_pullup(m, s)) == NULL) {
  187                 ipxipif.if_ierrors++;
  188                 return;
  189         }
  190         ip = mtod(m, struct ip *);
  191         if (ip->ip_hl > (sizeof(struct ip) >> 2)) {
  192                 ip_stripoptions(m, (struct mbuf *)NULL);
  193                 if (m->m_len < s) {
  194                         if ((m = m_pullup(m, s)) == NULL) {
  195                                 ipxipif.if_ierrors++;
  196                                 return;
  197                         }
  198                         ip = mtod(m, struct ip *);
  199                 }
  200         }
  201 
  202         /*
  203          * Make mbuf data length reflect IPX length.
  204          * If not enough data to reflect IPX length, drop.
  205          */
  206         m->m_data += sizeof(struct ip);
  207         m->m_len -= sizeof(struct ip);
  208         m->m_pkthdr.len -= sizeof(struct ip);
  209         ipx = mtod(m, struct ipx *);
  210         len = ntohs(ipx->ipx_len);
  211         if (len & 1)
  212                 len++;          /* Preserve Garbage Byte */
  213         if (ip->ip_len != len) {
  214                 if (len > ip->ip_len) {
  215                         ipxipif.if_ierrors++;
  216                         if (ipxip_badlen)
  217                                 m_freem(ipxip_badlen);
  218                         ipxip_badlen = m;
  219                         return;
  220                 }
  221                 /* Any extra will be trimmed off by the IPX routines */
  222         }
  223 
  224         /*
  225          * Deliver to IPX
  226          */
  227         netisr_dispatch(NETISR_IPX, m);
  228 }
  229 
  230 static int
  231 ipxipoutput(ifp, m, dst, rt)
  232         struct ifnet *ifp;
  233         struct mbuf *m;
  234         struct sockaddr *dst;
  235         struct rtentry *rt;
  236 {
  237         register struct ifnet_en *ifn = (struct ifnet_en *)ifp;
  238         register struct ip *ip;
  239         register struct route *ro = &(ifn->ifen_route);
  240         register int len = 0;
  241         register struct ipx *ipx = mtod(m, struct ipx *);
  242         int error;
  243 
  244         ifn->ifen_ifnet.if_opackets++;
  245         ipxipif.if_opackets++;
  246 
  247         /*
  248          * Calculate data length and make space
  249          * for IP header.
  250          */
  251         len =  ntohs(ipx->ipx_len);
  252         if (len & 1)
  253                 len++;          /* Preserve Garbage Byte */
  254         /* following clause not necessary on vax */
  255         if (3 & (intptr_t)m->m_data) {
  256                 /* force longword alignment of ip hdr */
  257                 struct mbuf *m0 = m_gethdr(MT_HEADER, M_DONTWAIT);
  258                 if (m0 == NULL) {
  259                         m_freem(m);
  260                         return (ENOBUFS);
  261                 }
  262                 MH_ALIGN(m0, sizeof(struct ip));
  263                 m0->m_flags = m->m_flags & M_COPYFLAGS;
  264                 m0->m_next = m;
  265                 m0->m_len = sizeof(struct ip);
  266                 m0->m_pkthdr.len = m0->m_len + m->m_len;
  267                 m->m_flags &= ~M_PKTHDR;
  268                 m = m0;
  269         } else {
  270                 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
  271                 if (m == NULL)
  272                         return (ENOBUFS);
  273         }
  274         /*
  275          * Fill in IP header.
  276          */
  277         ip = mtod(m, struct ip *);
  278         *(long *)ip = 0;
  279         ip->ip_p = IPPROTO_IDP;
  280         ip->ip_src = ifn->ifen_src;
  281         ip->ip_dst = ifn->ifen_dst;
  282         ip->ip_len = (u_short)len + sizeof(struct ip);
  283         ip->ip_ttl = MAXTTL;
  284 
  285         /*
  286          * Output final datagram.
  287          */
  288         error =  (ip_output(m, (struct mbuf *)NULL, ro, SO_BROADCAST, NULL, NULL));
  289         if (error) {
  290                 ifn->ifen_ifnet.if_oerrors++;
  291                 ifn->ifen_ifnet.if_ierrors = error;
  292         }
  293         return (error);
  294         m_freem(m);
  295         return (ENETUNREACH);
  296 }
  297 
  298 static void
  299 ipxipstart(ifp)
  300 struct ifnet *ifp;
  301 {
  302         panic("ipxip_start called\n");
  303 }
  304 
  305 static struct ifreq ifr_ipxip = {"ipxip0"};
  306 
  307 int
  308 ipxip_route(so, sopt)
  309         struct socket *so;
  310         struct sockopt *sopt;
  311 {
  312         int error;
  313         struct ifnet_en *ifn;
  314         struct sockaddr_in *src;
  315         struct ipxip_req rq;
  316         struct sockaddr_ipx *ipx_dst;
  317         struct sockaddr_in *ip_dst;
  318         struct route ro;
  319 
  320         error = sooptcopyin(sopt, &rq, sizeof rq, sizeof rq);
  321         if (error)
  322                 return (error);
  323         ipx_dst = (struct sockaddr_ipx *)&rq.rq_ipx;
  324         ip_dst = (struct sockaddr_in *)&rq.rq_ip;
  325 
  326         /*
  327          * First, make sure we already have an IPX address:
  328          */
  329         if (ipx_ifaddr == NULL)
  330                 return (EADDRNOTAVAIL);
  331         /*
  332          * Now, determine if we can get to the destination
  333          */
  334         bzero((caddr_t)&ro, sizeof(ro));
  335         ro.ro_dst = *(struct sockaddr *)ip_dst;
  336         rtalloc_ign(&ro, 0);
  337         if (ro.ro_rt == NULL || ro.ro_rt->rt_ifp == NULL) {
  338                 return (ENETUNREACH);
  339         }
  340 
  341         /*
  342          * And see how he's going to get back to us:
  343          * i.e., what return ip address do we use?
  344          */
  345         {
  346                 register struct in_ifaddr *ia;
  347                 struct ifnet *ifp = ro.ro_rt->rt_ifp;
  348 
  349                 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia != NULL;
  350                      ia = TAILQ_NEXT(ia, ia_link))
  351                         if (ia->ia_ifp == ifp)
  352                                 break;
  353                 if (ia == NULL)
  354                         ia = TAILQ_FIRST(&in_ifaddrhead);
  355                 if (ia == NULL) {
  356                         RTFREE(ro.ro_rt);
  357                         return (EADDRNOTAVAIL);
  358                 }
  359                 src = (struct sockaddr_in *)&ia->ia_addr;
  360         }
  361 
  362         /*
  363          * Is there a free (pseudo-)interface or space?
  364          */
  365         for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
  366                 if ((ifn->ifen_ifnet.if_flags & IFF_UP) == 0)
  367                         break;
  368         }
  369         if (ifn == NULL)
  370                 ifn = ipxipattach();
  371         if (ifn == NULL) {
  372                 RTFREE(ro.ro_rt);
  373                 return (ENOBUFS);
  374         }
  375         ifn->ifen_route = ro;
  376         ifn->ifen_dst =  ip_dst->sin_addr;
  377         ifn->ifen_src = src->sin_addr;
  378 
  379         /*
  380          * now configure this as a point to point link
  381          */
  382         ifr_ipxip.ifr_name[4] = '' + ipxipif_units - 1;
  383         ifr_ipxip.ifr_dstaddr = *(struct sockaddr *)ipx_dst;
  384         ipx_control(so, (int)SIOCSIFDSTADDR, (caddr_t)&ifr_ipxip,
  385                         (struct ifnet *)ifn, sopt->sopt_td);
  386 
  387         /* use any of our addresses */
  388         satoipx_addr(ifr_ipxip.ifr_addr).x_host =
  389                         ipx_ifaddr->ia_addr.sipx_addr.x_host;
  390 
  391         return (ipx_control(so, (int)SIOCSIFADDR, (caddr_t)&ifr_ipxip,
  392                         (struct ifnet *)ifn, sopt->sopt_td));
  393 }
  394 
  395 static int
  396 ipxip_free(ifp)
  397 struct ifnet *ifp;
  398 {
  399         register struct ifnet_en *ifn = (struct ifnet_en *)ifp;
  400         struct route *ro = & ifn->ifen_route;
  401 
  402         if (ro->ro_rt != NULL) {
  403                 RTFREE(ro->ro_rt);
  404                 ro->ro_rt = NULL;
  405         }
  406         ifp->if_flags &= ~IFF_UP;
  407         return (0);
  408 }
  409 
  410 void
  411 ipxip_ctlinput(cmd, sa, dummy)
  412         int cmd;
  413         struct sockaddr *sa;
  414         void *dummy;
  415 {
  416         struct sockaddr_in *sin;
  417 
  418         if ((unsigned)cmd >= PRC_NCMDS)
  419                 return;
  420         if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
  421                 return;
  422         sin = (struct sockaddr_in *)sa;
  423         if (sin->sin_addr.s_addr == INADDR_ANY)
  424                 return;
  425 
  426         switch (cmd) {
  427 
  428         case PRC_ROUTEDEAD:
  429         case PRC_REDIRECT_NET:
  430         case PRC_REDIRECT_HOST:
  431         case PRC_REDIRECT_TOSNET:
  432         case PRC_REDIRECT_TOSHOST:
  433                 ipxip_rtchange(&sin->sin_addr);
  434                 break;
  435         }
  436 }
  437 
  438 static void
  439 ipxip_rtchange(dst)
  440         register struct in_addr *dst;
  441 {
  442         register struct ifnet_en *ifn;
  443 
  444         for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
  445                 if (ifn->ifen_dst.s_addr == dst->s_addr &&
  446                         ifn->ifen_route.ro_rt != NULL) {
  447                                 RTFREE(ifn->ifen_route.ro_rt);
  448                                 ifn->ifen_route.ro_rt = NULL;
  449                 }
  450         }
  451 }
  452 #endif /* IPXIP */

Cache object: f22acddcbb4be838a4dee42e0af9d932


[ 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.