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/netns/ns_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 /*      $NetBSD: ns_ip.c,v 1.37 2003/11/14 15:04:48 itojun Exp $        */
    2 
    3 /*
    4  * Copyright (c) 1984, 1985, 1986, 1987, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. Neither the name of the University nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  *
   31  *      @(#)ns_ip.c     8.2 (Berkeley) 1/9/95
   32  */
   33 
   34 /*
   35  * Software interface driver for encapsulating ns in ip.
   36  */
   37 
   38 #include <sys/cdefs.h>
   39 __KERNEL_RCSID(0, "$NetBSD: ns_ip.c,v 1.37 2003/11/14 15:04:48 itojun Exp $");
   40 
   41 #include "opt_ns.h"             /* options NSIP, needed by ns_if.h */
   42 
   43 #include <sys/param.h>
   44 #include <sys/systm.h>
   45 #include <sys/malloc.h>
   46 #include <sys/mbuf.h>
   47 #include <sys/socket.h>
   48 #include <sys/socketvar.h>
   49 #include <sys/errno.h>
   50 #include <sys/ioctl.h>
   51 #include <sys/protosw.h>
   52 
   53 #include <machine/stdarg.h>
   54 #include <machine/cpu.h>        /* XXX for setsoftnet().  This must die. */
   55 
   56 #include <net/if.h>
   57 #include <net/netisr.h>
   58 #include <net/route.h>
   59 
   60 #include <netinet/in.h>
   61 #include <netinet/in_systm.h>
   62 #include <netinet/in_var.h>
   63 #include <netinet/ip.h>
   64 #include <netinet/ip_var.h>
   65 
   66 #include <netns/ns.h>
   67 #include <netns/ns_if.h>
   68 #include <netns/ns_var.h>
   69 #include <netns/idp.h>
   70 
   71 struct ifnet_en {
   72         struct ifnet ifen_ifnet;
   73         struct route ifen_route;
   74         struct in_addr ifen_src;
   75         struct in_addr ifen_dst;
   76         struct ifnet_en *ifen_next;
   77 };
   78 
   79 int     nsipoutput __P((struct ifnet *, struct mbuf *m, struct sockaddr *,
   80     struct rtentry *));
   81 int     nsipioctl __P((struct ifnet *, u_long, caddr_t));
   82 void    nsipstart __P((struct ifnet *));
   83 int     nsip_route __P((struct mbuf *));
   84 void    nsip_rtchange __P((struct in_addr *));
   85 #define LOMTU   (1024+512);
   86 
   87 int     nsipif_unit;                    /* XXX */
   88 struct ifnet nsipif;
   89 struct ifnet_en *nsip_list;             /* list of all hosts and gateways or
   90                                         broadcast addrs */
   91 
   92 struct ifnet_en *
   93 nsipattach()
   94 {
   95         struct ifnet_en *m;
   96         struct ifnet *ifp;
   97 
   98         if (nsipif.if_mtu == 0) {
   99                 ifp = &nsipif;
  100                 sprintf(ifp->if_xname, "nsip%d", nsipif_unit);
  101                 ifp->if_mtu = LOMTU;
  102                 ifp->if_ioctl = nsipioctl;
  103                 ifp->if_output = nsipoutput;
  104                 ifp->if_start = nsipstart;
  105                 ifp->if_flags = IFF_POINTOPOINT;
  106         }
  107 
  108         MALLOC((m), struct ifnet_en *, sizeof(*m), M_PCB, M_NOWAIT);
  109         if (m == NULL) return (NULL);
  110         m->ifen_next = nsip_list;
  111         nsip_list = m;
  112         ifp = &m->ifen_ifnet;
  113 
  114         sprintf(ifp->if_xname, "nsip%d", nsipif_unit++);
  115         ifp->if_mtu = LOMTU;
  116         ifp->if_ioctl = nsipioctl;
  117         ifp->if_output = nsipoutput;
  118         ifp->if_start = nsipstart;
  119         ifp->if_flags = IFF_POINTOPOINT;
  120         if_attach(ifp);
  121         if_alloc_sadl(ifp);
  122 
  123         /*
  124          * XXX Emulate the side effect of incrementing nsipif.if_unit
  125          * XXX in the days before if_xname.
  126          */
  127         bzero(nsipif.if_xname, sizeof(nsipif.if_xname));
  128         sprintf(nsipif.if_xname, "nsip%d", nsipif_unit);
  129 
  130         return (m);
  131 }
  132 
  133 
  134 /*
  135  * Process an ioctl request.
  136  */
  137 /* ARGSUSED */
  138 int
  139 nsipioctl(ifp, cmd, data)
  140         struct ifnet *ifp;
  141         u_long cmd;
  142         caddr_t data;
  143 {
  144         int error = 0;
  145         struct ifreq *ifr;
  146 
  147         switch (cmd) {
  148 
  149         case SIOCSIFADDR:
  150                 ifp->if_flags |= IFF_UP;
  151                 /* fall into: */
  152 
  153         case SIOCSIFDSTADDR:
  154                 /*
  155                  * Everything else is done at a higher level.
  156                  */
  157                 break;
  158 
  159         case SIOCSIFFLAGS:
  160                 ifr = (struct ifreq *)data;
  161                 if ((ifr->ifr_flags & IFF_UP) == 0)
  162                         error = nsip_free(ifp);
  163 
  164 
  165         default:
  166                 error = EINVAL;
  167         }
  168         return (error);
  169 }
  170 
  171 struct mbuf *nsip_badlen;
  172 struct mbuf *nsip_lastin;
  173 int nsip_hold_input;
  174 
  175 void
  176 #if __STDC__
  177 idpip_input(struct mbuf *m, ...)
  178 #else
  179 idpip_input(va_alist)
  180         va_dcl
  181 #endif
  182 {
  183         struct ifnet *ifp;
  184         struct ip *ip;
  185         struct idp *idp;
  186         struct ifqueue *ifq = &nsintrq;
  187         int len, s;
  188         va_list ap;
  189 #if __STDC__
  190         va_start(ap, m);
  191 #else
  192         struct mbuf *m;
  193 
  194         va_start(ap);
  195         m = va_arg(ap, struct mbuf *);
  196 #endif
  197         ifp = va_arg(ap, struct ifnet *);
  198         va_end(ap);
  199 
  200         if (nsip_hold_input) {
  201                 if (nsip_lastin) {
  202                         m_freem(nsip_lastin);
  203                 }
  204                 nsip_lastin = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT);
  205         }
  206         /*
  207          * Get IP and IDP header together in first mbuf.
  208          */
  209         nsipif.if_ipackets++;
  210         s = sizeof (struct ip) + sizeof (struct idp);
  211         if (((m->m_flags & M_EXT) || m->m_len < s) &&
  212             (m = m_pullup(m, s)) == 0) {
  213                 nsipif.if_ierrors++;
  214                 return;
  215         }
  216         ip = mtod(m, struct ip *);
  217         if (ip->ip_hl > (sizeof (struct ip) >> 2)) {
  218                 ip_stripoptions(m, (struct mbuf *)0);
  219                 if (m->m_len < s) {
  220                         if ((m = m_pullup(m, s)) == 0) {
  221                                 nsipif.if_ierrors++;
  222                                 return;
  223                         }
  224                         ip = mtod(m, struct ip *);
  225                 }
  226         }
  227 
  228         /*
  229          * Make mbuf data length reflect IDP length.
  230          * If not enough data to reflect IDP length, drop.
  231          */
  232         m->m_data += sizeof (struct ip);
  233         m->m_len -= sizeof (struct ip);
  234         m->m_pkthdr.len -= sizeof (struct ip);
  235         idp = mtod(m, struct idp *);
  236         len = ntohs(idp->idp_len);
  237         if (len & 1) len++;             /* Preserve Garbage Byte */
  238         if (ntohs(ip->ip_len) - (ip->ip_hl << 2) != len) {
  239                 if (len > ntohs(ip->ip_len) - (ip->ip_hl << 2)) {
  240                         nsipif.if_ierrors++;
  241                         if (nsip_badlen) m_freem(nsip_badlen);
  242                         nsip_badlen = m;
  243                         return;
  244                 }
  245                 /* Any extra will be trimmed off by the NS routines */
  246         }
  247 
  248         /*
  249          * Place interface pointer before the data
  250          * for the receiving protocol.
  251          */
  252         m->m_pkthdr.rcvif = ifp;
  253         /*
  254          * Deliver to NS
  255          */
  256         s = splnet();
  257         if (IF_QFULL(ifq)) {
  258                 IF_DROP(ifq);
  259                 m_freem(m);
  260                 splx(s);
  261                 return;
  262         }
  263         IF_ENQUEUE(ifq, m);
  264         schednetisr(NETISR_NS);
  265         splx(s);
  266         return;
  267 }
  268 
  269 /* ARGSUSED */
  270 int
  271 nsipoutput(ifp, m, dst, rt)
  272         struct ifnet *ifp;
  273         struct mbuf *m;
  274         struct sockaddr *dst;
  275         struct rtentry *rt;
  276 {
  277         struct ifnet_en *ifn = (struct ifnet_en *) ifp;
  278 
  279         struct ip *ip;
  280         struct route *ro = &(ifn->ifen_route);
  281         int len = 0;
  282         struct idp *idp = mtod(m, struct idp *);
  283         int error;
  284 
  285         ifn->ifen_ifnet.if_opackets++;
  286         nsipif.if_opackets++;
  287 
  288 
  289         /*
  290          * Calculate data length and make space
  291          * for IP header.
  292          */
  293         len = ntohs(idp->idp_len);
  294         if (len & 1) len++;             /* Preserve Garbage Byte */
  295         /* following clause not necessary on vax */
  296         if (3 & (int)m->m_data) {
  297                 /* force longword alignment of ip hdr */
  298                 struct mbuf *m0 = m_gethdr(M_DONTWAIT, MT_HEADER);
  299                 if (m0 == 0) {
  300                         m_freem(m);
  301                         return (ENOBUFS);
  302                 }
  303                 MH_ALIGN(m0, sizeof (struct ip));
  304                 m0->m_flags = m->m_flags & M_COPYFLAGS;
  305                 m0->m_next = m;
  306                 m0->m_len = sizeof (struct ip);
  307                 m0->m_pkthdr.len = m0->m_len + m->m_len;
  308                 m_tag_delete_chain(m, NULL);
  309                 m->m_flags &= ~M_PKTHDR;
  310         } else {
  311                 M_PREPEND(m, sizeof (struct ip), M_DONTWAIT);
  312                 if (m == 0)
  313                         return (ENOBUFS);
  314         }
  315         /*
  316          * Fill in IP header.
  317          */
  318         ip = mtod(m, struct ip *);
  319         *(int32_t *)ip = 0;
  320         ip->ip_p = IPPROTO_IDP;
  321         ip->ip_src = ifn->ifen_src;
  322         ip->ip_dst = ifn->ifen_dst;
  323         if (len + sizeof (struct ip) > IP_MAXPACKET) {
  324                 m_freem(m);
  325                 return (EMSGSIZE);
  326         }
  327         ip->ip_len = htons(len + sizeof (struct ip));
  328         ip->ip_ttl = MAXTTL;
  329 
  330         /*
  331          * Output final datagram.
  332          */
  333         error = ip_output(m, (struct mbuf *)0, ro, SO_BROADCAST,
  334             (struct ip_moptions *)NULL, (struct socket *)NULL);
  335         if (error) {
  336                 ifn->ifen_ifnet.if_oerrors++;
  337                 ifn->ifen_ifnet.if_ierrors = error;
  338         }
  339         return (error);
  340 }
  341 
  342 void
  343 nsipstart(ifp)
  344         struct ifnet *ifp;
  345 {
  346         panic("nsip_start called");
  347 }
  348 
  349 struct ifreq ifr = {"nsip0"};           /* XXX */
  350 
  351 int
  352 nsip_route(m)
  353         struct mbuf *m;
  354 {
  355         struct nsip_req *rq = mtod(m, struct nsip_req *);
  356         struct sockaddr_ns *ns_dst = satosns(&rq->rq_ns);
  357         struct sockaddr_in *ip_dst = satosin(&rq->rq_ip);
  358         struct route ro;
  359         struct ifnet_en *ifn;
  360         struct sockaddr_in *src;
  361 
  362         /*
  363          * First, make sure we already have an ns address:
  364          */
  365         if (ns_hosteqnh(ns_thishost, ns_zerohost))
  366                 return (EADDRNOTAVAIL);
  367         /*
  368          * Now, determine if we can get to the destination
  369          */
  370         bzero((caddr_t)&ro, sizeof (ro));
  371         ro.ro_dst = *sintosa(ip_dst);
  372         rtalloc(&ro);
  373         if (ro.ro_rt == 0 || ro.ro_rt->rt_ifp == 0) {
  374                 return (ENETUNREACH);
  375         }
  376 
  377         /*
  378          * And see how he's going to get back to us:
  379          * i.e., what return ip address do we use?
  380          */
  381         {
  382                 struct in_ifaddr *ia;
  383                 struct ifnet *ifp = ro.ro_rt->rt_ifp;
  384 
  385                 for (ia = in_ifaddrhead.tqh_first; ia != 0;
  386                     ia = ia->ia_list.tqe_next)
  387                         if (ia->ia_ifp == ifp)
  388                                 break;
  389                 if (ia == 0)
  390                         ia = in_ifaddrhead.tqh_first;
  391                 if (ia == 0) {
  392                         RTFREE(ro.ro_rt);
  393                         return (EADDRNOTAVAIL);
  394                 }
  395                 src = satosin(&ia->ia_addr);
  396         }
  397 
  398         /*
  399          * Is there a free (pseudo-)interface or space?
  400          */
  401         for (ifn = nsip_list; ifn; ifn = ifn->ifen_next) {
  402                 if ((ifn->ifen_ifnet.if_flags & IFF_UP) == 0)
  403                         break;
  404         }
  405         if (ifn == NULL)
  406                 ifn = nsipattach();
  407         if (ifn == NULL) {
  408                 RTFREE(ro.ro_rt);
  409                 return (ENOBUFS);
  410         }
  411         ifn->ifen_route = ro;
  412         ifn->ifen_dst =  ip_dst->sin_addr;
  413         ifn->ifen_src = src->sin_addr;
  414 
  415         /*
  416          * now configure this as a point to point link
  417          */
  418         bzero(ifr.ifr_name, sizeof(ifr.ifr_name));
  419         sprintf(ifr.ifr_name, "nsip%d", nsipif_unit - 1);
  420         ifr.ifr_dstaddr = *snstosa(ns_dst);
  421         (void)ns_control((struct socket *)0, SIOCSIFDSTADDR, (caddr_t)&ifr,
  422             (struct ifnet *)ifn, NULL);
  423         satons_addr(ifr.ifr_addr).x_host = ns_thishost;
  424         return (ns_control((struct socket *)0, SIOCSIFADDR, (caddr_t)&ifr,
  425             (struct ifnet *)ifn, NULL));
  426 }
  427 
  428 int
  429 nsip_free(ifp)
  430         struct ifnet *ifp;
  431 {
  432         struct ifnet_en *ifn = (struct ifnet_en *)ifp;
  433         struct route *ro = & ifn->ifen_route;
  434 
  435         if (ro->ro_rt) {
  436                 RTFREE(ro->ro_rt);
  437                 ro->ro_rt = 0;
  438         }
  439         ifp->if_flags &= ~IFF_UP;
  440         return (0);
  441 }
  442 
  443 void *
  444 nsip_ctlinput(cmd, sa, v)
  445         int cmd;
  446         struct sockaddr *sa;
  447         void *v;
  448 {
  449         struct sockaddr_in *sin;
  450 
  451         if ((unsigned)cmd >= PRC_NCMDS)
  452                 return NULL;
  453         if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
  454                 return NULL;
  455         sin = satosin(sa);
  456         if (sin->sin_addr.s_addr == INADDR_ANY)
  457                 return NULL;
  458 
  459         switch (cmd) {
  460 
  461         case PRC_ROUTEDEAD:
  462         case PRC_REDIRECT_NET:
  463         case PRC_REDIRECT_HOST:
  464         case PRC_REDIRECT_TOSNET:
  465         case PRC_REDIRECT_TOSHOST:
  466                 nsip_rtchange(&sin->sin_addr);
  467                 break;
  468         }
  469         return NULL;
  470 }
  471 
  472 void
  473 nsip_rtchange(dst)
  474         struct in_addr *dst;
  475 {
  476         struct ifnet_en *ifn;
  477 
  478         for (ifn = nsip_list; ifn; ifn = ifn->ifen_next) {
  479                 if (ifn->ifen_dst.s_addr == dst->s_addr &&
  480                         ifn->ifen_route.ro_rt) {
  481                                 RTFREE(ifn->ifen_route.ro_rt);
  482                                 ifn->ifen_route.ro_rt = 0;
  483                 }
  484         }
  485 }

Cache object: 3f53934de34d610f99beb26b162eea1a


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