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.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.c,v 1.27 2005/02/26 22:39:50 perry 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.c        8.5 (Berkeley) 2/9/95
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __KERNEL_RCSID(0, "$NetBSD: ns.c,v 1.27 2005/02/26 22:39:50 perry Exp $");
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/mbuf.h>
   40 #include <sys/ioctl.h>
   41 #include <sys/protosw.h>
   42 #include <sys/errno.h>
   43 #include <sys/socket.h>
   44 #include <sys/socketvar.h>
   45 #include <sys/proc.h>
   46 
   47 #include <net/if.h>
   48 #include <net/route.h>
   49 
   50 #include <netns/ns.h>
   51 #include <netns/ns_if.h>
   52 #include <netns/ns_var.h>
   53 
   54 struct ns_ifaddrhead ns_ifaddr;
   55 int ns_interfaces;
   56 
   57 /*
   58  * Generic internet control operations (ioctl's).
   59  */
   60 /* ARGSUSED */
   61 int
   62 ns_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
   63         struct proc *p)
   64 {
   65         struct ifreq *ifr = (struct ifreq *)data;
   66         struct ns_ifaddr *ia = 0;
   67         struct ns_aliasreq *ifra = (struct ns_aliasreq *)data;
   68         struct sockaddr_ns oldaddr;
   69         int error = 0, dstIsNew, hostIsNew;
   70 
   71         /*
   72          * Find address for this interface, if it exists.
   73          */
   74         if (ifp)
   75                 for (ia = ns_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
   76                         if (ia->ia_ifp == ifp)
   77                                 break;
   78 
   79         switch (cmd) {
   80 
   81         case SIOCAIFADDR:
   82         case SIOCDIFADDR:
   83                 if (ifra->ifra_addr.sns_family == AF_NS)
   84                         for (; ia; ia = ia->ia_list.tqe_next) {
   85                                 if (ia->ia_ifp == ifp  &&
   86                                     ns_neteq(ia->ia_addr.sns_addr, ifra->ifra_addr.sns_addr))
   87                                         break;
   88                         }
   89                 if (cmd == SIOCDIFADDR && ia == 0)
   90                         return (EADDRNOTAVAIL);
   91                 /* FALLTHROUGH */
   92         case SIOCSIFADDR:
   93         case SIOCSIFDSTADDR:
   94                 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
   95                         return (EPERM);
   96 
   97                 if (ifp == 0)
   98                         panic("ns_control");
   99                 if (ia == 0) {
  100                         MALLOC(ia, struct ns_ifaddr *, sizeof(*ia),
  101                                M_IFADDR, M_WAITOK|M_ZERO);
  102                         if (ia == 0)
  103                                 return (ENOBUFS);
  104                         TAILQ_INSERT_TAIL(&ns_ifaddr, ia, ia_list);
  105                         IFAREF((struct ifaddr *)ia);
  106                         TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia,
  107                             ifa_list);
  108                         IFAREF((struct ifaddr *)ia);
  109                         ia->ia_ifa.ifa_addr = snstosa(&ia->ia_addr);
  110                         ia->ia_ifa.ifa_netmask = snstosa(&ns_netmask);
  111                         ia->ia_ifa.ifa_dstaddr = snstosa(&ia->ia_dstaddr);
  112                         if (ifp->if_flags & IFF_BROADCAST) {
  113                                 ia->ia_broadaddr.sns_len = sizeof(ia->ia_addr);
  114                                 ia->ia_broadaddr.sns_family = AF_NS;
  115                                 ia->ia_broadaddr.sns_addr.x_host = ns_broadhost;
  116                         }
  117                         ia->ia_ifp = ifp;
  118                         if ((ifp->if_flags & IFF_LOOPBACK) == 0)
  119                                 ns_interfaces++;
  120                 }
  121                 break;
  122 
  123         case SIOCGIFADDR:
  124         case SIOCGIFDSTADDR:
  125         case SIOCGIFBRDADDR:
  126                 if (ia == 0)
  127                         return (EADDRNOTAVAIL);
  128                 break;
  129         }
  130         switch (cmd) {
  131 
  132         case SIOCGIFADDR:
  133                 *satosns(&ifr->ifr_addr) = ia->ia_addr;
  134                 break;
  135 
  136         case SIOCGIFBRDADDR:
  137                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
  138                         return (EINVAL);
  139                 *satosns(&ifr->ifr_dstaddr) = ia->ia_broadaddr;
  140                 break;
  141 
  142         case SIOCGIFDSTADDR:
  143                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
  144                         return (EINVAL);
  145                 *satosns(&ifr->ifr_dstaddr) = ia->ia_dstaddr;
  146                 break;
  147 
  148         case SIOCSIFDSTADDR:
  149                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
  150                         return (EINVAL);
  151                 oldaddr = ia->ia_dstaddr;
  152                 ia->ia_dstaddr = *satosns(&ifr->ifr_dstaddr);
  153                 if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
  154                                         (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
  155                         ia->ia_dstaddr = oldaddr;
  156                         return (error);
  157                 }
  158                 if (ia->ia_flags & IFA_ROUTE) {
  159                         ia->ia_ifa.ifa_dstaddr = snstosa(&oldaddr);
  160                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
  161                         ia->ia_ifa.ifa_dstaddr = snstosa(&ia->ia_dstaddr);
  162                         rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
  163                 }
  164                 break;
  165 
  166         case SIOCSIFADDR:
  167                 return (ns_ifinit(ifp, ia, satosns(&ifr->ifr_addr), 1));
  168 
  169         case SIOCAIFADDR:
  170                 dstIsNew = 0;
  171                 hostIsNew = 1;
  172                 if (ia->ia_addr.sns_family == AF_NS) {
  173                         if (ifra->ifra_addr.sns_len == 0) {
  174                                 ifra->ifra_addr = ia->ia_addr;
  175                                 hostIsNew = 0;
  176                         } else if (ns_neteq(ifra->ifra_addr.sns_addr, ia->ia_addr.sns_addr))
  177                                 hostIsNew = 0;
  178                 }
  179                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
  180                     (ifra->ifra_dstaddr.sns_family == AF_NS)) {
  181                         ns_ifscrub(ifp, ia);
  182                         ia->ia_dstaddr = ifra->ifra_dstaddr;
  183                         dstIsNew  = 1;
  184                 }
  185                 if (ifra->ifra_addr.sns_family == AF_NS &&
  186                     (hostIsNew || dstIsNew))
  187                         error = ns_ifinit(ifp, ia, &ifra->ifra_addr, 0);
  188                 return (error);
  189 
  190         case SIOCDIFADDR:
  191                 ns_purgeaddr(&ia->ia_ifa, ifp);
  192                 break;
  193 
  194         default:
  195                 if (ifp == 0 || ifp->if_ioctl == 0)
  196                         return (EOPNOTSUPP);
  197                 return ((*ifp->if_ioctl)(ifp, cmd, data));
  198         }
  199         return (0);
  200 }
  201 
  202 void
  203 ns_purgeaddr(struct ifaddr *ifa, struct ifnet *ifp)
  204 {
  205         struct ns_ifaddr *ia = (void *) ifa;
  206 
  207         ns_ifscrub(ifp, ia);
  208         TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
  209         IFAFREE(&ia->ia_ifa);
  210         TAILQ_REMOVE(&ns_ifaddr, ia, ia_list);
  211         IFAFREE((&ia->ia_ifa));
  212         if (0 == --ns_interfaces) {
  213                 /*
  214                  * We reset to virginity and start all over again
  215                  */
  216                 ns_thishost = ns_zerohost;
  217         }
  218 }
  219 
  220 void
  221 ns_purgeif(struct ifnet *ifp)
  222 {
  223         struct ifaddr *ifa, *nifa;
  224 
  225         for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
  226                 nifa = TAILQ_NEXT(ifa, ifa_list);
  227                 if (ifa->ifa_addr->sa_family != AF_NS)
  228                         continue;
  229                 ns_purgeaddr(ifa, ifp);
  230         }
  231 }
  232 
  233 /*
  234  * Delete any previous route for an old address.
  235  */
  236 void
  237 ns_ifscrub(struct ifnet *ifp, struct ns_ifaddr *ia)
  238 {
  239 
  240         if ((ia->ia_flags & IFA_ROUTE) == 0)
  241                 return;
  242         if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
  243                 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
  244         else
  245                 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
  246         ia->ia_flags &= ~IFA_ROUTE;
  247 }
  248 /*
  249  * Initialize an interface's internet address
  250  * and routing table entry.
  251  */
  252 int
  253 ns_ifinit(struct ifnet *ifp, struct ns_ifaddr *ia, struct sockaddr_ns *sns,
  254         int scrub)
  255 {
  256         struct sockaddr_ns oldaddr;
  257         union ns_host *h = &ia->ia_addr.sns_addr.x_host;
  258         int s = splnet(), error;
  259 
  260         /*
  261          * Set up new addresses.
  262          */
  263         oldaddr = ia->ia_addr;
  264         ia->ia_addr = *sns;
  265         /*
  266          * The convention we shall adopt for naming is that
  267          * a supplied address of zero means that "we don't care".
  268          * if there is a single interface, use the address of that
  269          * interface as our 6 byte host address.
  270          * if there are multiple interfaces, use any address already
  271          * used.
  272          *
  273          * Give the interface a chance to initialize
  274          * if this is its first address,
  275          * and to validate the address if necessary.
  276          */
  277         if (ns_hosteqnh(ns_thishost, ns_zerohost)) {
  278                 if (ifp->if_ioctl &&
  279                      (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR,
  280                     (caddr_t)ia)))
  281                         goto bad;
  282                 ns_thishost = *h;
  283         } else if (ns_hosteqnh(sns->sns_addr.x_host, ns_zerohost)
  284             || ns_hosteqnh(sns->sns_addr.x_host, ns_thishost)) {
  285                 *h = ns_thishost;
  286                 if (ifp->if_ioctl &&
  287                      (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR,
  288                     (caddr_t)ia)))
  289                         goto bad;
  290                 if (!ns_hosteqnh(ns_thishost, *h)) {
  291                         error = EINVAL;
  292                         goto bad;
  293                 }
  294         } else {
  295                 error = EINVAL;
  296                 goto bad;
  297         }
  298         ia->ia_ifa.ifa_metric = ifp->if_metric;
  299         /*
  300          * Add route for the network.
  301          */
  302         if (scrub) {
  303                 ia->ia_ifa.ifa_addr = snstosa(&oldaddr);
  304                 ns_ifscrub(ifp, ia);
  305                 ia->ia_ifa.ifa_addr = snstosa(&ia->ia_addr);
  306         }
  307         if (ifp->if_flags & IFF_POINTOPOINT)
  308                 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
  309         else {
  310                 ia->ia_broadaddr.sns_addr.x_net = ia->ia_addr.sns_addr.x_net;
  311                 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP);
  312         }
  313         ia->ia_flags |= IFA_ROUTE;
  314         splx(s);
  315         return (0);
  316 bad:
  317         ia->ia_addr = oldaddr;
  318         splx(s);
  319         return (error);
  320 }
  321 
  322 /*
  323  * Return address info for specified internet network.
  324  */
  325 struct ns_ifaddr *
  326 ns_iaonnetof(struct ns_addr *dst)
  327 {
  328         struct ns_ifaddr *ia;
  329         struct ns_addr *compare;
  330         struct ifnet *ifp;
  331         struct ns_ifaddr *ia_maybe = 0;
  332         union ns_net net = dst->x_net;
  333 
  334         for (ia = ns_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next) {
  335                 if ((ifp = ia->ia_ifp) != NULL) {
  336                         if (ifp->if_flags & IFF_POINTOPOINT) {
  337                                 compare = &satons_addr(ia->ia_dstaddr);
  338                                 if (ns_hosteq(*dst, *compare))
  339                                         return (ia);
  340                                 if (ns_neteqnn(net, ia->ia_addr.sns_addr.x_net))
  341                                         ia_maybe = ia;
  342                         } else {
  343                                 if (ns_neteqnn(net, ia->ia_addr.sns_addr.x_net))
  344                                         return (ia);
  345                         }
  346                 }
  347         }
  348         return (ia_maybe);
  349 }

Cache object: 8818f30736ca746217806683cec71f77


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