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/netinet/in.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) 1982, 1986, 1991, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  * Copyright (C) 2001 WIDE Project.  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  * 4. Neither the name of the University nor the names of its contributors
   15  *    may be used to endorse or promote products derived from this software
   16  *    without specific prior written permission.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   28  * SUCH DAMAGE.
   29  *
   30  *      @(#)in.c        8.4 (Berkeley) 1/9/95
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD$");
   35 
   36 #include "opt_carp.h"
   37 
   38 #include <sys/param.h>
   39 #include <sys/systm.h>
   40 #include <sys/sockio.h>
   41 #include <sys/malloc.h>
   42 #include <sys/priv.h>
   43 #include <sys/socket.h>
   44 #include <sys/kernel.h>
   45 #include <sys/sysctl.h>
   46 
   47 #include <net/if.h>
   48 #include <net/if_types.h>
   49 #include <net/route.h>
   50 
   51 #include <netinet/in.h>
   52 #include <netinet/in_var.h>
   53 #include <netinet/in_pcb.h>
   54 #include <netinet/ip_var.h>
   55 
   56 static int in_mask2len(struct in_addr *);
   57 static void in_len2mask(struct in_addr *, int);
   58 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
   59         struct ifnet *, struct thread *);
   60 
   61 static int      in_addprefix(struct in_ifaddr *, int);
   62 static int      in_scrubprefix(struct in_ifaddr *);
   63 static void     in_socktrim(struct sockaddr_in *);
   64 static int      in_ifinit(struct ifnet *,
   65             struct in_ifaddr *, struct sockaddr_in *, int);
   66 static void     in_purgemaddrs(struct ifnet *);
   67 
   68 static int subnetsarelocal = 0;
   69 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
   70         &subnetsarelocal, 0, "Treat all subnets as directly connected");
   71 static int sameprefixcarponly = 0;
   72 SYSCTL_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
   73         &sameprefixcarponly, 0,
   74         "Refuse to create same prefixes on different interfaces");
   75 
   76 extern struct inpcbinfo ripcbinfo;
   77 extern struct inpcbinfo udbinfo;
   78 
   79 /*
   80  * Return 1 if an internet address is for a ``local'' host
   81  * (one to which we have a connection).  If subnetsarelocal
   82  * is true, this includes other subnets of the local net.
   83  * Otherwise, it includes only the directly-connected (sub)nets.
   84  */
   85 int
   86 in_localaddr(struct in_addr in)
   87 {
   88         register u_long i = ntohl(in.s_addr);
   89         register struct in_ifaddr *ia;
   90 
   91         if (subnetsarelocal) {
   92                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
   93                         if ((i & ia->ia_netmask) == ia->ia_net)
   94                                 return (1);
   95         } else {
   96                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
   97                         if ((i & ia->ia_subnetmask) == ia->ia_subnet)
   98                                 return (1);
   99         }
  100         return (0);
  101 }
  102 
  103 /*
  104  * Return 1 if an internet address is for the local host and configured
  105  * on one of its interfaces.
  106  */
  107 int
  108 in_localip(struct in_addr in)
  109 {
  110         struct in_ifaddr *ia;
  111 
  112         LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
  113                 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
  114                         return 1;
  115         }
  116         return 0;
  117 }
  118 
  119 /*
  120  * Determine whether an IP address is in a reserved set of addresses
  121  * that may not be forwarded, or whether datagrams to that destination
  122  * may be forwarded.
  123  */
  124 int
  125 in_canforward(struct in_addr in)
  126 {
  127         register u_long i = ntohl(in.s_addr);
  128         register u_long net;
  129 
  130         if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
  131                 return (0);
  132         if (IN_CLASSA(i)) {
  133                 net = i & IN_CLASSA_NET;
  134                 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
  135                         return (0);
  136         }
  137         return (1);
  138 }
  139 
  140 /*
  141  * Trim a mask in a sockaddr
  142  */
  143 static void
  144 in_socktrim(struct sockaddr_in *ap)
  145 {
  146     register char *cplim = (char *) &ap->sin_addr;
  147     register char *cp = (char *) (&ap->sin_addr + 1);
  148 
  149     ap->sin_len = 0;
  150     while (--cp >= cplim)
  151         if (*cp) {
  152             (ap)->sin_len = cp - (char *) (ap) + 1;
  153             break;
  154         }
  155 }
  156 
  157 static int
  158 in_mask2len(mask)
  159         struct in_addr *mask;
  160 {
  161         int x, y;
  162         u_char *p;
  163 
  164         p = (u_char *)mask;
  165         for (x = 0; x < sizeof(*mask); x++) {
  166                 if (p[x] != 0xff)
  167                         break;
  168         }
  169         y = 0;
  170         if (x < sizeof(*mask)) {
  171                 for (y = 0; y < 8; y++) {
  172                         if ((p[x] & (0x80 >> y)) == 0)
  173                                 break;
  174                 }
  175         }
  176         return x * 8 + y;
  177 }
  178 
  179 static void
  180 in_len2mask(struct in_addr *mask, int len)
  181 {
  182         int i;
  183         u_char *p;
  184 
  185         p = (u_char *)mask;
  186         bzero(mask, sizeof(*mask));
  187         for (i = 0; i < len / 8; i++)
  188                 p[i] = 0xff;
  189         if (len % 8)
  190                 p[i] = (0xff00 >> (len % 8)) & 0xff;
  191 }
  192 
  193 /*
  194  * Generic internet control operations (ioctl's).
  195  * Ifp is 0 if not an interface-specific ioctl.
  196  */
  197 /* ARGSUSED */
  198 int
  199 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
  200     struct thread *td)
  201 {
  202         register struct ifreq *ifr = (struct ifreq *)data;
  203         register struct in_ifaddr *ia = 0, *iap;
  204         register struct ifaddr *ifa;
  205         struct in_addr allhosts_addr;
  206         struct in_addr dst;
  207         struct in_ifaddr *oia;
  208         struct in_aliasreq *ifra = (struct in_aliasreq *)data;
  209         struct sockaddr_in oldaddr;
  210         int error, hostIsNew, iaIsNew, maskIsNew, s;
  211         int iaIsFirst;
  212 
  213         iaIsFirst = 0;
  214         iaIsNew = 0;
  215         allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
  216 
  217         switch (cmd) {
  218         case SIOCALIFADDR:
  219                 if (td != NULL) {
  220                         error = priv_check(td, PRIV_NET_ADDIFADDR);
  221                         if (error)
  222                                 return (error);
  223                 }
  224                 if (!ifp)
  225                         return EINVAL;
  226                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
  227 
  228         case SIOCDLIFADDR:
  229                 if (td != NULL) {
  230                         error = priv_check(td, PRIV_NET_DELIFADDR);
  231                         if (error)
  232                                 return (error);
  233                 }
  234                 if (!ifp)
  235                         return EINVAL;
  236                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
  237 
  238         case SIOCGLIFADDR:
  239                 if (!ifp)
  240                         return EINVAL;
  241                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
  242         }
  243 
  244         /*
  245          * Find address for this interface, if it exists.
  246          *
  247          * If an alias address was specified, find that one instead of
  248          * the first one on the interface, if possible.
  249          */
  250         if (ifp) {
  251                 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
  252                 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
  253                         if (iap->ia_ifp == ifp &&
  254                             iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
  255                                 ia = iap;
  256                                 break;
  257                         }
  258                 if (ia == NULL)
  259                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  260                                 iap = ifatoia(ifa);
  261                                 if (iap->ia_addr.sin_family == AF_INET) {
  262                                         ia = iap;
  263                                         break;
  264                                 }
  265                         }
  266                 if (ia == NULL)
  267                         iaIsFirst = 1;
  268         }
  269 
  270         switch (cmd) {
  271 
  272         case SIOCAIFADDR:
  273         case SIOCDIFADDR:
  274                 if (ifp == 0)
  275                         return (EADDRNOTAVAIL);
  276                 if (ifra->ifra_addr.sin_family == AF_INET) {
  277                         for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
  278                                 if (ia->ia_ifp == ifp  &&
  279                                     ia->ia_addr.sin_addr.s_addr ==
  280                                     ifra->ifra_addr.sin_addr.s_addr)
  281                                         break;
  282                         }
  283                         if ((ifp->if_flags & IFF_POINTOPOINT)
  284                             && (cmd == SIOCAIFADDR)
  285                             && (ifra->ifra_dstaddr.sin_addr.s_addr
  286                                 == INADDR_ANY)) {
  287                                 return EDESTADDRREQ;
  288                         }
  289                 }
  290                 if (cmd == SIOCDIFADDR && ia == 0)
  291                         return (EADDRNOTAVAIL);
  292                 /* FALLTHROUGH */
  293         case SIOCSIFADDR:
  294         case SIOCSIFNETMASK:
  295         case SIOCSIFDSTADDR:
  296                 if (td != NULL) {
  297                         error = priv_check(td, (cmd == SIOCDIFADDR) ? 
  298                             PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
  299                         if (error)
  300                                 return (error);
  301                 }
  302 
  303                 if (ifp == 0)
  304                         return (EADDRNOTAVAIL);
  305                 if (ia == (struct in_ifaddr *)0) {
  306                         ia = (struct in_ifaddr *)
  307                                 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
  308                         if (ia == (struct in_ifaddr *)NULL)
  309                                 return (ENOBUFS);
  310                         /*
  311                          * Protect from ipintr() traversing address list
  312                          * while we're modifying it.
  313                          */
  314                         s = splnet();
  315                         ifa = &ia->ia_ifa;
  316                         IFA_LOCK_INIT(ifa);
  317                         ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
  318                         ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
  319                         ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
  320                         ifa->ifa_refcnt = 1;
  321                         TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
  322 
  323                         ia->ia_sockmask.sin_len = 8;
  324                         ia->ia_sockmask.sin_family = AF_INET;
  325                         if (ifp->if_flags & IFF_BROADCAST) {
  326                                 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
  327                                 ia->ia_broadaddr.sin_family = AF_INET;
  328                         }
  329                         ia->ia_ifp = ifp;
  330 
  331                         TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
  332                         splx(s);
  333                         iaIsNew = 1;
  334                 }
  335                 break;
  336 
  337         case SIOCSIFBRDADDR:
  338                 if (td != NULL) {
  339                         error = priv_check(td, PRIV_NET_ADDIFADDR);
  340                         if (error)
  341                                 return (error);
  342                 }
  343                 /* FALLTHROUGH */
  344 
  345         case SIOCGIFADDR:
  346         case SIOCGIFNETMASK:
  347         case SIOCGIFDSTADDR:
  348         case SIOCGIFBRDADDR:
  349                 if (ia == (struct in_ifaddr *)0)
  350                         return (EADDRNOTAVAIL);
  351                 break;
  352         }
  353         switch (cmd) {
  354 
  355         case SIOCGIFADDR:
  356                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
  357                 return (0);
  358 
  359         case SIOCGIFBRDADDR:
  360                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
  361                         return (EINVAL);
  362                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
  363                 return (0);
  364 
  365         case SIOCGIFDSTADDR:
  366                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
  367                         return (EINVAL);
  368                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
  369                 return (0);
  370 
  371         case SIOCGIFNETMASK:
  372                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
  373                 return (0);
  374 
  375         case SIOCSIFDSTADDR:
  376                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
  377                         return (EINVAL);
  378                 oldaddr = ia->ia_dstaddr;
  379                 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
  380                 if (ifp->if_ioctl) {
  381                         IFF_LOCKGIANT(ifp);
  382                         error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
  383                             (caddr_t)ia);
  384                         IFF_UNLOCKGIANT(ifp);
  385                         if (error) {
  386                                 ia->ia_dstaddr = oldaddr;
  387                                 return (error);
  388                         }
  389                 }
  390                 if (ia->ia_flags & IFA_ROUTE) {
  391                         ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
  392                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
  393                         ia->ia_ifa.ifa_dstaddr =
  394                                         (struct sockaddr *)&ia->ia_dstaddr;
  395                         rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
  396                 }
  397                 return (0);
  398 
  399         case SIOCSIFBRDADDR:
  400                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
  401                         return (EINVAL);
  402                 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
  403                 return (0);
  404 
  405         case SIOCSIFADDR:
  406                 error = in_ifinit(ifp, ia,
  407                     (struct sockaddr_in *) &ifr->ifr_addr, 1);
  408                 if (error != 0 && iaIsNew)
  409                         break;
  410                 if (error == 0) {
  411                         if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
  412                                 in_addmulti(&allhosts_addr, ifp);
  413                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
  414                 }
  415                 return (0);
  416 
  417         case SIOCSIFNETMASK:
  418                 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
  419                 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
  420                 return (0);
  421 
  422         case SIOCAIFADDR:
  423                 maskIsNew = 0;
  424                 hostIsNew = 1;
  425                 error = 0;
  426                 if (ia->ia_addr.sin_family == AF_INET) {
  427                         if (ifra->ifra_addr.sin_len == 0) {
  428                                 ifra->ifra_addr = ia->ia_addr;
  429                                 hostIsNew = 0;
  430                         } else if (ifra->ifra_addr.sin_addr.s_addr ==
  431                                                ia->ia_addr.sin_addr.s_addr)
  432                                 hostIsNew = 0;
  433                 }
  434                 if (ifra->ifra_mask.sin_len) {
  435                         in_ifscrub(ifp, ia);
  436                         ia->ia_sockmask = ifra->ifra_mask;
  437                         ia->ia_sockmask.sin_family = AF_INET;
  438                         ia->ia_subnetmask =
  439                              ntohl(ia->ia_sockmask.sin_addr.s_addr);
  440                         maskIsNew = 1;
  441                 }
  442                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
  443                     (ifra->ifra_dstaddr.sin_family == AF_INET)) {
  444                         in_ifscrub(ifp, ia);
  445                         ia->ia_dstaddr = ifra->ifra_dstaddr;
  446                         maskIsNew  = 1; /* We lie; but the effect's the same */
  447                 }
  448                 if (ifra->ifra_addr.sin_family == AF_INET &&
  449                     (hostIsNew || maskIsNew))
  450                         error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
  451                 if (error != 0 && iaIsNew)
  452                         break;
  453 
  454                 if ((ifp->if_flags & IFF_BROADCAST) &&
  455                     (ifra->ifra_broadaddr.sin_family == AF_INET))
  456                         ia->ia_broadaddr = ifra->ifra_broadaddr;
  457                 if (error == 0) {
  458                         if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
  459                                 in_addmulti(&allhosts_addr, ifp);
  460                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
  461                 }
  462                 return (error);
  463 
  464         case SIOCDIFADDR:
  465                 /*
  466                  * in_ifscrub kills the interface route.
  467                  */
  468                 in_ifscrub(ifp, ia);
  469                 /*
  470                  * in_ifadown gets rid of all the rest of
  471                  * the routes.  This is not quite the right
  472                  * thing to do, but at least if we are running
  473                  * a routing process they will come back.
  474                  */
  475                 in_ifadown(&ia->ia_ifa, 1);
  476                 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
  477                 error = 0;
  478                 break;
  479 
  480         default:
  481                 if (ifp == 0 || ifp->if_ioctl == 0)
  482                         return (EOPNOTSUPP);
  483                 IFF_LOCKGIANT(ifp);
  484                 error = (*ifp->if_ioctl)(ifp, cmd, data);
  485                 IFF_UNLOCKGIANT(ifp);
  486                 return (error);
  487         }
  488 
  489         /*
  490          * Protect from ipintr() traversing address list while we're modifying
  491          * it.
  492          */
  493         s = splnet();
  494         TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
  495         TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
  496         if (ia->ia_addr.sin_family == AF_INET) {
  497                 LIST_REMOVE(ia, ia_hash);
  498                 /*
  499                  * If this is the last IPv4 address configured on this
  500                  * interface, leave the all-hosts group.
  501                  * XXX: This is quite ugly because of locking and structure.
  502                  */
  503                 oia = NULL;
  504                 IFP_TO_IA(ifp, oia);
  505                 if (oia == NULL) {
  506                         struct in_multi *inm;
  507 
  508                         IFF_LOCKGIANT(ifp);
  509                         IN_MULTI_LOCK();
  510                         IN_LOOKUP_MULTI(allhosts_addr, ifp, inm);
  511                         if (inm != NULL)
  512                                 in_delmulti_locked(inm);
  513                         IN_MULTI_UNLOCK();
  514                         IFF_UNLOCKGIANT(ifp);
  515                 }
  516         }
  517         IFAFREE(&ia->ia_ifa);
  518         splx(s);
  519 
  520         return (error);
  521 }
  522 
  523 /*
  524  * SIOC[GAD]LIFADDR.
  525  *      SIOCGLIFADDR: get first address. (?!?)
  526  *      SIOCGLIFADDR with IFLR_PREFIX:
  527  *              get first address that matches the specified prefix.
  528  *      SIOCALIFADDR: add the specified address.
  529  *      SIOCALIFADDR with IFLR_PREFIX:
  530  *              EINVAL since we can't deduce hostid part of the address.
  531  *      SIOCDLIFADDR: delete the specified address.
  532  *      SIOCDLIFADDR with IFLR_PREFIX:
  533  *              delete the first address that matches the specified prefix.
  534  * return values:
  535  *      EINVAL on invalid parameters
  536  *      EADDRNOTAVAIL on prefix match failed/specified address not found
  537  *      other values may be returned from in_ioctl()
  538  */
  539 static int
  540 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
  541     struct ifnet *ifp, struct thread *td)
  542 {
  543         struct if_laddrreq *iflr = (struct if_laddrreq *)data;
  544         struct ifaddr *ifa;
  545 
  546         /* sanity checks */
  547         if (!data || !ifp) {
  548                 panic("invalid argument to in_lifaddr_ioctl");
  549                 /*NOTRECHED*/
  550         }
  551 
  552         switch (cmd) {
  553         case SIOCGLIFADDR:
  554                 /* address must be specified on GET with IFLR_PREFIX */
  555                 if ((iflr->flags & IFLR_PREFIX) == 0)
  556                         break;
  557                 /*FALLTHROUGH*/
  558         case SIOCALIFADDR:
  559         case SIOCDLIFADDR:
  560                 /* address must be specified on ADD and DELETE */
  561                 if (iflr->addr.ss_family != AF_INET)
  562                         return EINVAL;
  563                 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
  564                         return EINVAL;
  565                 /* XXX need improvement */
  566                 if (iflr->dstaddr.ss_family
  567                  && iflr->dstaddr.ss_family != AF_INET)
  568                         return EINVAL;
  569                 if (iflr->dstaddr.ss_family
  570                  && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
  571                         return EINVAL;
  572                 break;
  573         default: /*shouldn't happen*/
  574                 return EOPNOTSUPP;
  575         }
  576         if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
  577                 return EINVAL;
  578 
  579         switch (cmd) {
  580         case SIOCALIFADDR:
  581             {
  582                 struct in_aliasreq ifra;
  583 
  584                 if (iflr->flags & IFLR_PREFIX)
  585                         return EINVAL;
  586 
  587                 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
  588                 bzero(&ifra, sizeof(ifra));
  589                 bcopy(iflr->iflr_name, ifra.ifra_name,
  590                         sizeof(ifra.ifra_name));
  591 
  592                 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
  593 
  594                 if (iflr->dstaddr.ss_family) {  /*XXX*/
  595                         bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
  596                                 iflr->dstaddr.ss_len);
  597                 }
  598 
  599                 ifra.ifra_mask.sin_family = AF_INET;
  600                 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
  601                 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
  602 
  603                 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
  604             }
  605         case SIOCGLIFADDR:
  606         case SIOCDLIFADDR:
  607             {
  608                 struct in_ifaddr *ia;
  609                 struct in_addr mask, candidate, match;
  610                 struct sockaddr_in *sin;
  611 
  612                 bzero(&mask, sizeof(mask));
  613                 bzero(&match, sizeof(match));
  614                 if (iflr->flags & IFLR_PREFIX) {
  615                         /* lookup a prefix rather than address. */
  616                         in_len2mask(&mask, iflr->prefixlen);
  617 
  618                         sin = (struct sockaddr_in *)&iflr->addr;
  619                         match.s_addr = sin->sin_addr.s_addr;
  620                         match.s_addr &= mask.s_addr;
  621 
  622                         /* if you set extra bits, that's wrong */
  623                         if (match.s_addr != sin->sin_addr.s_addr)
  624                                 return EINVAL;
  625 
  626                 } else {
  627                         /* on getting an address, take the 1st match */
  628                         /* on deleting an address, do exact match */
  629                         if (cmd != SIOCGLIFADDR) {
  630                                 in_len2mask(&mask, 32);
  631                                 sin = (struct sockaddr_in *)&iflr->addr;
  632                                 match.s_addr = sin->sin_addr.s_addr;
  633                         }
  634                 }
  635 
  636                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  637                         if (ifa->ifa_addr->sa_family != AF_INET6)
  638                                 continue;
  639                         if (match.s_addr == 0)
  640                                 break;
  641                         candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
  642                         candidate.s_addr &= mask.s_addr;
  643                         if (candidate.s_addr == match.s_addr)
  644                                 break;
  645                 }
  646                 if (!ifa)
  647                         return EADDRNOTAVAIL;
  648                 ia = (struct in_ifaddr *)ifa;
  649 
  650                 if (cmd == SIOCGLIFADDR) {
  651                         /* fill in the if_laddrreq structure */
  652                         bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
  653 
  654                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
  655                                 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
  656                                         ia->ia_dstaddr.sin_len);
  657                         } else
  658                                 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
  659 
  660                         iflr->prefixlen =
  661                                 in_mask2len(&ia->ia_sockmask.sin_addr);
  662 
  663                         iflr->flags = 0;        /*XXX*/
  664 
  665                         return 0;
  666                 } else {
  667                         struct in_aliasreq ifra;
  668 
  669                         /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
  670                         bzero(&ifra, sizeof(ifra));
  671                         bcopy(iflr->iflr_name, ifra.ifra_name,
  672                                 sizeof(ifra.ifra_name));
  673 
  674                         bcopy(&ia->ia_addr, &ifra.ifra_addr,
  675                                 ia->ia_addr.sin_len);
  676                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
  677                                 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
  678                                         ia->ia_dstaddr.sin_len);
  679                         }
  680                         bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
  681                                 ia->ia_sockmask.sin_len);
  682 
  683                         return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
  684                                           ifp, td);
  685                 }
  686             }
  687         }
  688 
  689         return EOPNOTSUPP;      /*just for safety*/
  690 }
  691 
  692 /*
  693  * Delete any existing route for an interface.
  694  */
  695 void
  696 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
  697 {
  698 
  699         in_scrubprefix(ia);
  700 }
  701 
  702 /*
  703  * Initialize an interface's internet address
  704  * and routing table entry.
  705  */
  706 static int
  707 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
  708     int scrub)
  709 {
  710         register u_long i = ntohl(sin->sin_addr.s_addr);
  711         struct sockaddr_in oldaddr;
  712         int s = splimp(), flags = RTF_UP, error = 0;
  713 
  714         oldaddr = ia->ia_addr;
  715         if (oldaddr.sin_family == AF_INET)
  716                 LIST_REMOVE(ia, ia_hash);
  717         ia->ia_addr = *sin;
  718         if (ia->ia_addr.sin_family == AF_INET)
  719                 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
  720                     ia, ia_hash);
  721         /*
  722          * Give the interface a chance to initialize
  723          * if this is its first address,
  724          * and to validate the address if necessary.
  725          */
  726         if (ifp->if_ioctl) {
  727                 IFF_LOCKGIANT(ifp);
  728                 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
  729                 IFF_UNLOCKGIANT(ifp);
  730                 if (error) {
  731                         splx(s);
  732                         /* LIST_REMOVE(ia, ia_hash) is done in in_control */
  733                         ia->ia_addr = oldaddr;
  734                         if (ia->ia_addr.sin_family == AF_INET)
  735                                 LIST_INSERT_HEAD(INADDR_HASH(
  736                                     ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
  737                         else 
  738                                 /* 
  739                                  * If oldaddr family is not AF_INET (e.g. 
  740                                  * interface has been just created) in_control 
  741                                  * does not call LIST_REMOVE, and we end up 
  742                                  * with bogus ia entries in hash
  743                                  */
  744                                 LIST_REMOVE(ia, ia_hash);
  745                         return (error);
  746                 }
  747         }
  748         splx(s);
  749         if (scrub) {
  750                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
  751                 in_ifscrub(ifp, ia);
  752                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
  753         }
  754         if (IN_CLASSA(i))
  755                 ia->ia_netmask = IN_CLASSA_NET;
  756         else if (IN_CLASSB(i))
  757                 ia->ia_netmask = IN_CLASSB_NET;
  758         else
  759                 ia->ia_netmask = IN_CLASSC_NET;
  760         /*
  761          * The subnet mask usually includes at least the standard network part,
  762          * but may may be smaller in the case of supernetting.
  763          * If it is set, we believe it.
  764          */
  765         if (ia->ia_subnetmask == 0) {
  766                 ia->ia_subnetmask = ia->ia_netmask;
  767                 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
  768         } else
  769                 ia->ia_netmask &= ia->ia_subnetmask;
  770         ia->ia_net = i & ia->ia_netmask;
  771         ia->ia_subnet = i & ia->ia_subnetmask;
  772         in_socktrim(&ia->ia_sockmask);
  773 #ifdef DEV_CARP
  774         /*
  775          * XXX: carp(4) does not have interface route
  776          */
  777         if (ifp->if_type == IFT_CARP)
  778                 return (0);
  779 #endif
  780         /*
  781          * Add route for the network.
  782          */
  783         ia->ia_ifa.ifa_metric = ifp->if_metric;
  784         if (ifp->if_flags & IFF_BROADCAST) {
  785                 ia->ia_broadaddr.sin_addr.s_addr =
  786                         htonl(ia->ia_subnet | ~ia->ia_subnetmask);
  787                 ia->ia_netbroadcast.s_addr =
  788                         htonl(ia->ia_net | ~ ia->ia_netmask);
  789         } else if (ifp->if_flags & IFF_LOOPBACK) {
  790                 ia->ia_dstaddr = ia->ia_addr;
  791                 flags |= RTF_HOST;
  792         } else if (ifp->if_flags & IFF_POINTOPOINT) {
  793                 if (ia->ia_dstaddr.sin_family != AF_INET)
  794                         return (0);
  795                 flags |= RTF_HOST;
  796         }
  797         if ((error = in_addprefix(ia, flags)) != 0)
  798                 return (error);
  799 
  800         return (error);
  801 }
  802 
  803 #define rtinitflags(x) \
  804         ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
  805             ? RTF_HOST : 0)
  806 /*
  807  * Check if we have a route for the given prefix already or add one accordingly.
  808  */
  809 static int
  810 in_addprefix(struct in_ifaddr *target, int flags)
  811 {
  812         struct in_ifaddr *ia;
  813         struct in_addr prefix, mask, p, m;
  814         int error;
  815 
  816         if ((flags & RTF_HOST) != 0) {
  817                 prefix = target->ia_dstaddr.sin_addr;
  818                 mask.s_addr = 0;
  819         } else {
  820                 prefix = target->ia_addr.sin_addr;
  821                 mask = target->ia_sockmask.sin_addr;
  822                 prefix.s_addr &= mask.s_addr;
  823         }
  824 
  825         TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
  826                 if (rtinitflags(ia)) {
  827                         p = ia->ia_addr.sin_addr;
  828 
  829                         if (prefix.s_addr != p.s_addr)
  830                                 continue;
  831                 } else {
  832                         p = ia->ia_addr.sin_addr;
  833                         m = ia->ia_sockmask.sin_addr;
  834                         p.s_addr &= m.s_addr;
  835 
  836                         if (prefix.s_addr != p.s_addr ||
  837                             mask.s_addr != m.s_addr)
  838                                 continue;
  839                 }
  840 
  841                 /*
  842                  * If we got a matching prefix route inserted by other
  843                  * interface address, we are done here.
  844                  */
  845                 if (ia->ia_flags & IFA_ROUTE) {
  846                         if (sameprefixcarponly &&
  847                             target->ia_ifp->if_type != IFT_CARP &&
  848                             ia->ia_ifp->if_type != IFT_CARP)
  849                                 return (EEXIST);
  850                         else
  851                                 return (0);
  852                 }
  853         }
  854 
  855         /*
  856          * No-one seem to have this prefix route, so we try to insert it.
  857          */
  858         error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
  859         if (!error)
  860                 target->ia_flags |= IFA_ROUTE;
  861         return error;
  862 }
  863 
  864 /*
  865  * If there is no other address in the system that can serve a route to the
  866  * same prefix, remove the route.  Hand over the route to the new address
  867  * otherwise.
  868  */
  869 static int
  870 in_scrubprefix(struct in_ifaddr *target)
  871 {
  872         struct in_ifaddr *ia;
  873         struct in_addr prefix, mask, p;
  874         int error;
  875 
  876         if ((target->ia_flags & IFA_ROUTE) == 0)
  877                 return 0;
  878 
  879         if (rtinitflags(target))
  880                 prefix = target->ia_dstaddr.sin_addr;
  881         else {
  882                 prefix = target->ia_addr.sin_addr;
  883                 mask = target->ia_sockmask.sin_addr;
  884                 prefix.s_addr &= mask.s_addr;
  885         }
  886 
  887         TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
  888                 if (rtinitflags(ia))
  889                         p = ia->ia_dstaddr.sin_addr;
  890                 else {
  891                         p = ia->ia_addr.sin_addr;
  892                         p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
  893                 }
  894 
  895                 if (prefix.s_addr != p.s_addr)
  896                         continue;
  897 
  898                 /*
  899                  * If we got a matching prefix address, move IFA_ROUTE and
  900                  * the route itself to it.  Make sure that routing daemons
  901                  * get a heads-up.
  902                  *
  903                  * XXX: a special case for carp(4) interface
  904                  */
  905                 if ((ia->ia_flags & IFA_ROUTE) == 0
  906 #ifdef DEV_CARP
  907                     && (ia->ia_ifp->if_type != IFT_CARP)
  908 #endif
  909                                                         ) {
  910                         rtinit(&(target->ia_ifa), (int)RTM_DELETE,
  911                             rtinitflags(target));
  912                         target->ia_flags &= ~IFA_ROUTE;
  913 
  914                         error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
  915                             rtinitflags(ia) | RTF_UP);
  916                         if (error == 0)
  917                                 ia->ia_flags |= IFA_ROUTE;
  918                         return error;
  919                 }
  920         }
  921 
  922         /*
  923          * As no-one seem to have this prefix, we can remove the route.
  924          */
  925         rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
  926         target->ia_flags &= ~IFA_ROUTE;
  927         return 0;
  928 }
  929 
  930 #undef rtinitflags
  931 
  932 /*
  933  * Return 1 if the address might be a local broadcast address.
  934  */
  935 int
  936 in_broadcast(struct in_addr in, struct ifnet *ifp)
  937 {
  938         register struct ifaddr *ifa;
  939         u_long t;
  940 
  941         if (in.s_addr == INADDR_BROADCAST ||
  942             in.s_addr == INADDR_ANY)
  943                 return 1;
  944         if ((ifp->if_flags & IFF_BROADCAST) == 0)
  945                 return 0;
  946         t = ntohl(in.s_addr);
  947         /*
  948          * Look through the list of addresses for a match
  949          * with a broadcast address.
  950          */
  951 #define ia ((struct in_ifaddr *)ifa)
  952         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
  953                 if (ifa->ifa_addr->sa_family == AF_INET &&
  954                     (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
  955                      in.s_addr == ia->ia_netbroadcast.s_addr ||
  956                      /*
  957                       * Check for old-style (host 0) broadcast.
  958                       */
  959                      t == ia->ia_subnet || t == ia->ia_net) &&
  960                      /*
  961                       * Check for an all one subnetmask. These
  962                       * only exist when an interface gets a secondary
  963                       * address.
  964                       */
  965                      ia->ia_subnetmask != (u_long)0xffffffff)
  966                             return 1;
  967         return (0);
  968 #undef ia
  969 }
  970 
  971 /*
  972  * Delete all IPv4 multicast address records, and associated link-layer
  973  * multicast address records, associated with ifp.
  974  */
  975 static void
  976 in_purgemaddrs(struct ifnet *ifp)
  977 {
  978         struct in_multi *inm;
  979         struct in_multi *oinm;
  980 
  981 #ifdef DIAGNOSTIC
  982         printf("%s: purging ifp %p\n", __func__, ifp);
  983 #endif
  984         IFF_LOCKGIANT(ifp);
  985         IN_MULTI_LOCK();
  986         LIST_FOREACH_SAFE(inm, &in_multihead, inm_link, oinm) {
  987                 if (inm->inm_ifp == ifp)
  988                         in_delmulti_locked(inm);
  989         }
  990         IN_MULTI_UNLOCK();
  991         IFF_UNLOCKGIANT(ifp);
  992 }
  993 
  994 /*
  995  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
  996  */
  997 void
  998 in_ifdetach(struct ifnet *ifp)
  999 {
 1000 
 1001         in_pcbpurgeif0(&ripcbinfo, ifp);
 1002         in_pcbpurgeif0(&udbinfo, ifp);
 1003         in_purgemaddrs(ifp);
 1004 }

Cache object: 0d79ba85efc3b90c44711eeabc9b90fa


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