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

Cache object: 092c8b027188dd2bbf809935bd804a50


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