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: releng/10.4/sys/netinet/in.c 309340 2016-11-30 22:20:23Z vangyzen $");
   35 
   36 #include "opt_mpath.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/jail.h>
   45 #include <sys/kernel.h>
   46 #include <sys/proc.h>
   47 #include <sys/sysctl.h>
   48 #include <sys/syslog.h>
   49 
   50 #include <net/if.h>
   51 #include <net/if_var.h>
   52 #include <net/if_arp.h>
   53 #include <net/if_dl.h>
   54 #include <net/if_llatbl.h>
   55 #include <net/if_types.h>
   56 #include <net/route.h>
   57 #include <net/vnet.h>
   58 
   59 #include <netinet/if_ether.h>
   60 #include <netinet/in.h>
   61 #include <netinet/in_var.h>
   62 #include <netinet/in_pcb.h>
   63 #include <netinet/ip_var.h>
   64 #include <netinet/ip_carp.h>
   65 #include <netinet/igmp_var.h>
   66 #include <netinet/udp.h>
   67 #include <netinet/udp_var.h>
   68 
   69 static int in_mask2len(struct in_addr *);
   70 static void in_len2mask(struct in_addr *, int);
   71 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
   72         struct ifnet *, struct thread *);
   73 
   74 static void     in_socktrim(struct sockaddr_in *);
   75 static int      in_ifinit(struct ifnet *, struct in_ifaddr *,
   76                     struct sockaddr_in *, int, int);
   77 static void     in_purgemaddrs(struct ifnet *);
   78 
   79 static VNET_DEFINE(int, nosameprefix);
   80 #define V_nosameprefix                  VNET(nosameprefix)
   81 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_RW,
   82         &VNET_NAME(nosameprefix), 0,
   83         "Refuse to create same prefixes on different interfaces");
   84 
   85 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
   86 #define V_ripcbinfo                     VNET(ripcbinfo)
   87 
   88 /*
   89  * Return 1 if an internet address is for a ``local'' host
   90  * (one to which we have a connection).
   91  */
   92 int
   93 in_localaddr(struct in_addr in)
   94 {
   95         register u_long i = ntohl(in.s_addr);
   96         register struct in_ifaddr *ia;
   97 
   98         IN_IFADDR_RLOCK();
   99         TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
  100                 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
  101                         IN_IFADDR_RUNLOCK();
  102                         return (1);
  103                 }
  104         }
  105         IN_IFADDR_RUNLOCK();
  106         return (0);
  107 }
  108 
  109 /*
  110  * Return 1 if an internet address is for the local host and configured
  111  * on one of its interfaces.
  112  */
  113 int
  114 in_localip(struct in_addr in)
  115 {
  116         struct in_ifaddr *ia;
  117 
  118         IN_IFADDR_RLOCK();
  119         LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
  120                 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
  121                         IN_IFADDR_RUNLOCK();
  122                         return (1);
  123                 }
  124         }
  125         IN_IFADDR_RUNLOCK();
  126         return (0);
  127 }
  128 
  129 /*
  130  * Determine whether an IP address is in a reserved set of addresses
  131  * that may not be forwarded, or whether datagrams to that destination
  132  * may be forwarded.
  133  */
  134 int
  135 in_canforward(struct in_addr in)
  136 {
  137         register u_long i = ntohl(in.s_addr);
  138         register u_long net;
  139 
  140         if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
  141                 return (0);
  142         if (IN_CLASSA(i)) {
  143                 net = i & IN_CLASSA_NET;
  144                 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
  145                         return (0);
  146         }
  147         return (1);
  148 }
  149 
  150 /*
  151  * Trim a mask in a sockaddr
  152  */
  153 static void
  154 in_socktrim(struct sockaddr_in *ap)
  155 {
  156     register char *cplim = (char *) &ap->sin_addr;
  157     register char *cp = (char *) (&ap->sin_addr + 1);
  158 
  159     ap->sin_len = 0;
  160     while (--cp >= cplim)
  161         if (*cp) {
  162             (ap)->sin_len = cp - (char *) (ap) + 1;
  163             break;
  164         }
  165 }
  166 
  167 static int
  168 in_mask2len(mask)
  169         struct in_addr *mask;
  170 {
  171         int x, y;
  172         u_char *p;
  173 
  174         p = (u_char *)mask;
  175         for (x = 0; x < sizeof(*mask); x++) {
  176                 if (p[x] != 0xff)
  177                         break;
  178         }
  179         y = 0;
  180         if (x < sizeof(*mask)) {
  181                 for (y = 0; y < 8; y++) {
  182                         if ((p[x] & (0x80 >> y)) == 0)
  183                                 break;
  184                 }
  185         }
  186         return (x * 8 + y);
  187 }
  188 
  189 static void
  190 in_len2mask(struct in_addr *mask, int len)
  191 {
  192         int i;
  193         u_char *p;
  194 
  195         p = (u_char *)mask;
  196         bzero(mask, sizeof(*mask));
  197         for (i = 0; i < len / 8; i++)
  198                 p[i] = 0xff;
  199         if (len % 8)
  200                 p[i] = (0xff00 >> (len % 8)) & 0xff;
  201 }
  202 
  203 /*
  204  * Generic internet control operations (ioctl's).
  205  *
  206  * ifp is NULL if not an interface-specific ioctl.
  207  */
  208 /* ARGSUSED */
  209 int
  210 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
  211     struct thread *td)
  212 {
  213         register struct ifreq *ifr = (struct ifreq *)data;
  214         register struct in_ifaddr *ia, *iap;
  215         register struct ifaddr *ifa;
  216         struct in_addr allhosts_addr;
  217         struct in_addr dst;
  218         struct in_ifinfo *ii;
  219         struct in_aliasreq *ifra = (struct in_aliasreq *)data;
  220         int error, hostIsNew, iaIsNew, maskIsNew;
  221         int iaIsFirst;
  222         u_long ocmd = cmd;
  223 
  224         /*
  225          * Pre-10.x compat: OSIOCAIFADDR passes a shorter
  226          * struct in_aliasreq, without ifra_vhid.
  227          */
  228         if (cmd == OSIOCAIFADDR)
  229                 cmd = SIOCAIFADDR;
  230 
  231         ia = NULL;
  232         iaIsFirst = 0;
  233         iaIsNew = 0;
  234         allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
  235 
  236         /*
  237          * Filter out ioctls we implement directly; forward the rest on to
  238          * in_lifaddr_ioctl() and ifp->if_ioctl().
  239          */
  240         switch (cmd) {
  241         case SIOCGIFADDR:
  242         case SIOCGIFBRDADDR:
  243         case SIOCGIFDSTADDR:
  244         case SIOCGIFNETMASK:
  245         case SIOCDIFADDR:
  246                 break;
  247         case SIOCAIFADDR:
  248                 /*
  249                  * ifra_addr must be present and be of INET family.
  250                  * ifra_broadaddr and ifra_mask are optional.
  251                  */
  252                 if (ifra->ifra_addr.sin_len != sizeof(struct sockaddr_in) ||
  253                     ifra->ifra_addr.sin_family != AF_INET)
  254                         return (EINVAL);
  255                 if (ifra->ifra_broadaddr.sin_len != 0 &&
  256                     (ifra->ifra_broadaddr.sin_len !=
  257                     sizeof(struct sockaddr_in) ||
  258                     ifra->ifra_broadaddr.sin_family != AF_INET))
  259                         return (EINVAL);
  260 #if 0
  261                 /*
  262                  * ifconfig(8) in pre-10.x doesn't set sin_family for the
  263                  * mask. The code is disabled for the 10.x timeline, to
  264                  * make SIOCAIFADDR compatible with 9.x ifconfig(8).
  265                  * The code should be enabled in 11.x
  266                  */
  267                 if (ifra->ifra_mask.sin_len != 0 &&
  268                     (ifra->ifra_mask.sin_len != sizeof(struct sockaddr_in) ||
  269                     ifra->ifra_mask.sin_family != AF_INET))
  270                         return (EINVAL);
  271 #endif
  272                 break;
  273         case SIOCSIFADDR:
  274         case SIOCSIFBRDADDR:
  275         case SIOCSIFDSTADDR:
  276         case SIOCSIFNETMASK:
  277                 /* We no longer support that old commands. */
  278                 return (EINVAL);
  279 
  280         case SIOCALIFADDR:
  281                 if (td != NULL) {
  282                         error = priv_check(td, PRIV_NET_ADDIFADDR);
  283                         if (error)
  284                                 return (error);
  285                 }
  286                 if (ifp == NULL)
  287                         return (EINVAL);
  288                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
  289 
  290         case SIOCDLIFADDR:
  291                 if (td != NULL) {
  292                         error = priv_check(td, PRIV_NET_DELIFADDR);
  293                         if (error)
  294                                 return (error);
  295                 }
  296                 if (ifp == NULL)
  297                         return (EINVAL);
  298                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
  299 
  300         case SIOCGLIFADDR:
  301                 if (ifp == NULL)
  302                         return (EINVAL);
  303                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
  304 
  305         default:
  306                 if (ifp == NULL || ifp->if_ioctl == NULL)
  307                         return (EOPNOTSUPP);
  308                 return ((*ifp->if_ioctl)(ifp, cmd, data));
  309         }
  310 
  311         if (ifp == NULL)
  312                 return (EADDRNOTAVAIL);
  313 
  314         /*
  315          * Security checks before we get involved in any work.
  316          */
  317         switch (cmd) {
  318         case SIOCAIFADDR:
  319                 if (td != NULL) {
  320                         error = priv_check(td, PRIV_NET_ADDIFADDR);
  321                         if (error)
  322                                 return (error);
  323                 }
  324                 break;
  325 
  326         case SIOCDIFADDR:
  327                 if (td != NULL) {
  328                         error = priv_check(td, PRIV_NET_DELIFADDR);
  329                         if (error)
  330                                 return (error);
  331                 }
  332                 break;
  333         }
  334 
  335         /*
  336          * Find address for this interface, if it exists.
  337          *
  338          * If an alias address was specified, find that one instead of the
  339          * first one on the interface, if possible.
  340          */
  341         dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
  342         IN_IFADDR_RLOCK();
  343         LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) {
  344                 if (iap->ia_ifp == ifp &&
  345                     iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
  346                         if (td == NULL || prison_check_ip4(td->td_ucred,
  347                             &dst) == 0)
  348                                 ia = iap;
  349                         break;
  350                 }
  351         }
  352         if (ia != NULL)
  353                 ifa_ref(&ia->ia_ifa);
  354         IN_IFADDR_RUNLOCK();
  355         if (ia == NULL) {
  356                 IF_ADDR_RLOCK(ifp);
  357                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  358                         iap = ifatoia(ifa);
  359                         if (iap->ia_addr.sin_family == AF_INET) {
  360                                 if (td != NULL &&
  361                                     prison_check_ip4(td->td_ucred,
  362                                     &iap->ia_addr.sin_addr) != 0)
  363                                         continue;
  364                                 ia = iap;
  365                                 break;
  366                         }
  367                 }
  368                 if (ia != NULL)
  369                         ifa_ref(&ia->ia_ifa);
  370                 IF_ADDR_RUNLOCK(ifp);
  371         }
  372         if (ia == NULL)
  373                 iaIsFirst = 1;
  374 
  375         error = 0;
  376         switch (cmd) {
  377         case SIOCAIFADDR:
  378         case SIOCDIFADDR:
  379                 if (ifra->ifra_addr.sin_family == AF_INET) {
  380                         struct in_ifaddr *oia;
  381 
  382                         IN_IFADDR_RLOCK();
  383                         for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
  384                                 if (ia->ia_ifp == ifp  &&
  385                                     ia->ia_addr.sin_addr.s_addr ==
  386                                     ifra->ifra_addr.sin_addr.s_addr)
  387                                         break;
  388                         }
  389                         if (ia != NULL && ia != oia)
  390                                 ifa_ref(&ia->ia_ifa);
  391                         if (oia != NULL && ia != oia)
  392                                 ifa_free(&oia->ia_ifa);
  393                         IN_IFADDR_RUNLOCK();
  394                         if ((ifp->if_flags & IFF_POINTOPOINT)
  395                             && (cmd == SIOCAIFADDR)
  396                             && (ifra->ifra_dstaddr.sin_addr.s_addr
  397                                 == INADDR_ANY)) {
  398                                 error = EDESTADDRREQ;
  399                                 goto out;
  400                         }
  401                 }
  402                 if (cmd == SIOCDIFADDR && ia == NULL) {
  403                         error = EADDRNOTAVAIL;
  404                         goto out;
  405                 }
  406                 if (ia == NULL) {
  407                         ia = (struct in_ifaddr *)
  408                                 malloc(sizeof *ia, M_IFADDR, M_NOWAIT |
  409                                     M_ZERO);
  410                         if (ia == NULL) {
  411                                 error = ENOBUFS;
  412                                 goto out;
  413                         }
  414 
  415                         ifa = &ia->ia_ifa;
  416                         ifa_init(ifa);
  417                         ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
  418                         ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
  419                         ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
  420                         callout_init_mtx(&ia->ia_garp_timer, &ifa->ifa_mtx,
  421                             CALLOUT_RETURNUNLOCKED);
  422 
  423                         ia->ia_sockmask.sin_len = 8;
  424                         ia->ia_sockmask.sin_family = AF_INET;
  425                         if (ifp->if_flags & IFF_BROADCAST) {
  426                                 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
  427                                 ia->ia_broadaddr.sin_family = AF_INET;
  428                         }
  429                         ia->ia_ifp = ifp;
  430 
  431                         ifa_ref(ifa);                   /* if_addrhead */
  432                         IF_ADDR_WLOCK(ifp);
  433                         TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
  434                         IF_ADDR_WUNLOCK(ifp);
  435                         ifa_ref(ifa);                   /* in_ifaddrhead */
  436                         IN_IFADDR_WLOCK();
  437                         TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
  438                         IN_IFADDR_WUNLOCK();
  439                         iaIsNew = 1;
  440                 }
  441                 break;
  442 
  443         case SIOCGIFADDR:
  444         case SIOCGIFNETMASK:
  445         case SIOCGIFDSTADDR:
  446         case SIOCGIFBRDADDR:
  447                 if (ia == NULL) {
  448                         error = EADDRNOTAVAIL;
  449                         goto out;
  450                 }
  451                 break;
  452         }
  453 
  454         /*
  455          * Most paths in this switch return directly or via out.  Only paths
  456          * that remove the address break in order to hit common removal code.
  457          */
  458         switch (cmd) {
  459         case SIOCGIFADDR:
  460                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
  461                 goto out;
  462 
  463         case SIOCGIFBRDADDR:
  464                 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
  465                         error = EINVAL;
  466                         goto out;
  467                 }
  468                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
  469                 goto out;
  470 
  471         case SIOCGIFDSTADDR:
  472                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
  473                         error = EINVAL;
  474                         goto out;
  475                 }
  476                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
  477                 goto out;
  478 
  479         case SIOCGIFNETMASK:
  480                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
  481                 goto out;
  482 
  483         case SIOCAIFADDR:
  484                 maskIsNew = 0;
  485                 hostIsNew = 1;
  486                 error = 0;
  487                 if (ifra->ifra_addr.sin_addr.s_addr ==
  488                             ia->ia_addr.sin_addr.s_addr)
  489                         hostIsNew = 0;
  490                 if (ifra->ifra_mask.sin_len) {
  491                         /*
  492                          * QL: XXX
  493                          * Need to scrub the prefix here in case
  494                          * the issued command is SIOCAIFADDR with
  495                          * the same address, but with a different
  496                          * prefix length. And if the prefix length
  497                          * is the same as before, then the call is
  498                          * un-necessarily executed here.
  499                          */
  500                         in_ifscrub(ifp, ia, LLE_STATIC);
  501                         ia->ia_sockmask = ifra->ifra_mask;
  502                         ia->ia_sockmask.sin_family = AF_INET;
  503                         ia->ia_subnetmask =
  504                             ntohl(ia->ia_sockmask.sin_addr.s_addr);
  505                         maskIsNew = 1;
  506                 }
  507                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
  508                     (ifra->ifra_dstaddr.sin_family == AF_INET)) {
  509                         in_ifscrub(ifp, ia, LLE_STATIC);
  510                         ia->ia_dstaddr = ifra->ifra_dstaddr;
  511                         maskIsNew  = 1; /* We lie; but the effect's the same */
  512                 }
  513                 if (hostIsNew || maskIsNew)
  514                         error = in_ifinit(ifp, ia, &ifra->ifra_addr, maskIsNew,
  515                             (ocmd == cmd ? ifra->ifra_vhid : 0));
  516                 if (error != 0 && iaIsNew)
  517                         break;
  518 
  519                 if ((ifp->if_flags & IFF_BROADCAST) &&
  520                     ifra->ifra_broadaddr.sin_len)
  521                         ia->ia_broadaddr = ifra->ifra_broadaddr;
  522                 if (error == 0) {
  523                         ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
  524                         if (iaIsFirst &&
  525                             (ifp->if_flags & IFF_MULTICAST) != 0) {
  526                                 error = in_joingroup(ifp, &allhosts_addr,
  527                                     NULL, &ii->ii_allhosts);
  528                         }
  529                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
  530                 }
  531                 goto out;
  532 
  533         case SIOCDIFADDR:
  534                 /*
  535                  * in_ifscrub kills the interface route.
  536                  */
  537                 in_ifscrub(ifp, ia, LLE_STATIC);
  538 
  539                 /*
  540                  * in_ifadown gets rid of all the rest of
  541                  * the routes.  This is not quite the right
  542                  * thing to do, but at least if we are running
  543                  * a routing process they will come back.
  544                  */
  545                 in_ifadown(&ia->ia_ifa, 1);
  546                 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
  547                 error = 0;
  548                 break;
  549 
  550         default:
  551                 panic("in_control: unsupported ioctl");
  552         }
  553 
  554         if (ia->ia_ifa.ifa_carp)
  555                 (*carp_detach_p)(&ia->ia_ifa);
  556 
  557         IF_ADDR_WLOCK(ifp);
  558         /* Re-check that ia is still part of the list. */
  559         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  560                 if (ifa == &ia->ia_ifa)
  561                         break;
  562         }
  563         if (ifa == NULL) {
  564                 /*
  565                  * If we lost the race with another thread, there is no need to
  566                  * try it again for the next loop as there is no other exit
  567                  * path between here and out.
  568                  */
  569                 IF_ADDR_WUNLOCK(ifp);
  570                 error = EADDRNOTAVAIL;
  571                 goto out;
  572         }
  573         TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
  574         IF_ADDR_WUNLOCK(ifp);
  575         ifa_free(&ia->ia_ifa);                /* if_addrhead */
  576 
  577         IN_IFADDR_WLOCK();
  578         TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
  579 
  580         LIST_REMOVE(ia, ia_hash);
  581         IN_IFADDR_WUNLOCK();
  582         /*
  583          * If this is the last IPv4 address configured on this
  584          * interface, leave the all-hosts group.
  585          * No state-change report need be transmitted.
  586          */
  587         IFP_TO_IA(ifp, iap);
  588         if (iap == NULL) {
  589                 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
  590                 IN_MULTI_LOCK();
  591                 if (ii->ii_allhosts) {
  592                         (void)in_leavegroup_locked(ii->ii_allhosts, NULL);
  593                         ii->ii_allhosts = NULL;
  594                 }
  595                 IN_MULTI_UNLOCK();
  596         } else
  597                 ifa_free(&iap->ia_ifa);
  598 
  599         IFA_LOCK(&ia->ia_ifa);
  600         if (callout_stop(&ia->ia_garp_timer))
  601                 ifa_free(&ia->ia_ifa);
  602         IFA_UNLOCK(&ia->ia_ifa);
  603         ifa_free(&ia->ia_ifa);                          /* in_ifaddrhead */
  604 out:
  605         if (ia != NULL)
  606                 ifa_free(&ia->ia_ifa);
  607         return (error);
  608 }
  609 
  610 /*
  611  * SIOC[GAD]LIFADDR.
  612  *      SIOCGLIFADDR: get first address. (?!?)
  613  *      SIOCGLIFADDR with IFLR_PREFIX:
  614  *              get first address that matches the specified prefix.
  615  *      SIOCALIFADDR: add the specified address.
  616  *      SIOCALIFADDR with IFLR_PREFIX:
  617  *              EINVAL since we can't deduce hostid part of the address.
  618  *      SIOCDLIFADDR: delete the specified address.
  619  *      SIOCDLIFADDR with IFLR_PREFIX:
  620  *              delete the first address that matches the specified prefix.
  621  * return values:
  622  *      EINVAL on invalid parameters
  623  *      EADDRNOTAVAIL on prefix match failed/specified address not found
  624  *      other values may be returned from in_ioctl()
  625  */
  626 static int
  627 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
  628     struct ifnet *ifp, struct thread *td)
  629 {
  630         struct if_laddrreq *iflr = (struct if_laddrreq *)data;
  631         struct ifaddr *ifa;
  632 
  633         /* sanity checks */
  634         if (data == NULL || ifp == NULL) {
  635                 panic("invalid argument to in_lifaddr_ioctl");
  636                 /*NOTRECHED*/
  637         }
  638 
  639         switch (cmd) {
  640         case SIOCGLIFADDR:
  641                 /* address must be specified on GET with IFLR_PREFIX */
  642                 if ((iflr->flags & IFLR_PREFIX) == 0)
  643                         break;
  644                 /*FALLTHROUGH*/
  645         case SIOCALIFADDR:
  646         case SIOCDLIFADDR:
  647                 /* address must be specified on ADD and DELETE */
  648                 if (iflr->addr.ss_family != AF_INET)
  649                         return (EINVAL);
  650                 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
  651                         return (EINVAL);
  652                 /* XXX need improvement */
  653                 if (iflr->dstaddr.ss_family
  654                  && iflr->dstaddr.ss_family != AF_INET)
  655                         return (EINVAL);
  656                 if (iflr->dstaddr.ss_family
  657                  && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
  658                         return (EINVAL);
  659                 break;
  660         default: /*shouldn't happen*/
  661                 return (EOPNOTSUPP);
  662         }
  663         if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
  664                 return (EINVAL);
  665 
  666         switch (cmd) {
  667         case SIOCALIFADDR:
  668             {
  669                 struct in_aliasreq ifra;
  670 
  671                 if (iflr->flags & IFLR_PREFIX)
  672                         return (EINVAL);
  673 
  674                 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
  675                 bzero(&ifra, sizeof(ifra));
  676                 bcopy(iflr->iflr_name, ifra.ifra_name,
  677                         sizeof(ifra.ifra_name));
  678 
  679                 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
  680 
  681                 if (iflr->dstaddr.ss_family) {  /*XXX*/
  682                         bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
  683                                 iflr->dstaddr.ss_len);
  684                 }
  685 
  686                 ifra.ifra_mask.sin_family = AF_INET;
  687                 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
  688                 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
  689 
  690                 return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td));
  691             }
  692         case SIOCGLIFADDR:
  693         case SIOCDLIFADDR:
  694             {
  695                 struct in_ifaddr *ia;
  696                 struct in_addr mask, candidate, match;
  697                 struct sockaddr_in *sin;
  698 
  699                 bzero(&mask, sizeof(mask));
  700                 bzero(&match, sizeof(match));
  701                 if (iflr->flags & IFLR_PREFIX) {
  702                         /* lookup a prefix rather than address. */
  703                         in_len2mask(&mask, iflr->prefixlen);
  704 
  705                         sin = (struct sockaddr_in *)&iflr->addr;
  706                         match.s_addr = sin->sin_addr.s_addr;
  707                         match.s_addr &= mask.s_addr;
  708 
  709                         /* if you set extra bits, that's wrong */
  710                         if (match.s_addr != sin->sin_addr.s_addr)
  711                                 return (EINVAL);
  712 
  713                 } else {
  714                         /* on getting an address, take the 1st match */
  715                         /* on deleting an address, do exact match */
  716                         if (cmd != SIOCGLIFADDR) {
  717                                 in_len2mask(&mask, 32);
  718                                 sin = (struct sockaddr_in *)&iflr->addr;
  719                                 match.s_addr = sin->sin_addr.s_addr;
  720                         }
  721                 }
  722 
  723                 IF_ADDR_RLOCK(ifp);
  724                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  725                         if (ifa->ifa_addr->sa_family != AF_INET)
  726                                 continue;
  727                         if (match.s_addr == 0)
  728                                 break;
  729                         sin = (struct sockaddr_in *)&ifa->ifa_addr;
  730                         candidate.s_addr = sin->sin_addr.s_addr;
  731                         candidate.s_addr &= mask.s_addr;
  732                         if (candidate.s_addr == match.s_addr)
  733                                 break;
  734                 }
  735                 if (ifa != NULL)
  736                         ifa_ref(ifa);
  737                 IF_ADDR_RUNLOCK(ifp);
  738                 if (ifa == NULL)
  739                         return (EADDRNOTAVAIL);
  740                 ia = (struct in_ifaddr *)ifa;
  741 
  742                 if (cmd == SIOCGLIFADDR) {
  743                         /* fill in the if_laddrreq structure */
  744                         bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
  745 
  746                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
  747                                 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
  748                                         ia->ia_dstaddr.sin_len);
  749                         } else
  750                                 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
  751 
  752                         iflr->prefixlen =
  753                                 in_mask2len(&ia->ia_sockmask.sin_addr);
  754 
  755                         iflr->flags = 0;        /*XXX*/
  756                         ifa_free(ifa);
  757 
  758                         return (0);
  759                 } else {
  760                         struct in_aliasreq ifra;
  761 
  762                         /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
  763                         bzero(&ifra, sizeof(ifra));
  764                         bcopy(iflr->iflr_name, ifra.ifra_name,
  765                                 sizeof(ifra.ifra_name));
  766 
  767                         bcopy(&ia->ia_addr, &ifra.ifra_addr,
  768                                 ia->ia_addr.sin_len);
  769                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
  770                                 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
  771                                         ia->ia_dstaddr.sin_len);
  772                         }
  773                         bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
  774                                 ia->ia_sockmask.sin_len);
  775                         ifa_free(ifa);
  776 
  777                         return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
  778                             ifp, td));
  779                 }
  780             }
  781         }
  782 
  783         return (EOPNOTSUPP);    /*just for safety*/
  784 }
  785 
  786 /*
  787  * Delete any existing route for an interface.
  788  */
  789 void
  790 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia, u_int flags)
  791 {
  792 
  793         in_scrubprefix(ia, flags);
  794 }
  795 
  796 /*
  797  * Initialize an interface's internet address
  798  * and routing table entry.
  799  */
  800 static int
  801 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
  802     int masksupplied, int vhid)
  803 {
  804         register u_long i = ntohl(sin->sin_addr.s_addr);
  805         int flags, error = 0;
  806 
  807         IN_IFADDR_WLOCK();
  808         if (ia->ia_addr.sin_family == AF_INET)
  809                 LIST_REMOVE(ia, ia_hash);
  810         ia->ia_addr = *sin;
  811         LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
  812             ia, ia_hash);
  813         IN_IFADDR_WUNLOCK();
  814 
  815         if (vhid > 0) {
  816                 if (carp_attach_p != NULL)
  817                         error = (*carp_attach_p)(&ia->ia_ifa, vhid);
  818                 else
  819                         error = EPROTONOSUPPORT;
  820         }
  821         if (error)
  822                 return (error);
  823 
  824         /*
  825          * Give the interface a chance to initialize
  826          * if this is its first address,
  827          * and to validate the address if necessary.
  828          */
  829         if (ifp->if_ioctl != NULL &&
  830             (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)) != 0)
  831                         /* LIST_REMOVE(ia, ia_hash) is done in in_control */
  832                         return (error);
  833 
  834         /*
  835          * Be compatible with network classes, if netmask isn't supplied,
  836          * guess it based on classes.
  837          */
  838         if (!masksupplied) {
  839                 if (IN_CLASSA(i))
  840                         ia->ia_subnetmask = IN_CLASSA_NET;
  841                 else if (IN_CLASSB(i))
  842                         ia->ia_subnetmask = IN_CLASSB_NET;
  843                 else
  844                         ia->ia_subnetmask = IN_CLASSC_NET;
  845                 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
  846         }
  847         ia->ia_subnet = i & ia->ia_subnetmask;
  848         in_socktrim(&ia->ia_sockmask);
  849 
  850         /*
  851          * Add route for the network.
  852          */
  853         flags = RTF_UP;
  854         ia->ia_ifa.ifa_metric = ifp->if_metric;
  855         if (ifp->if_flags & IFF_BROADCAST) {
  856                 if (ia->ia_subnetmask == IN_RFC3021_MASK)
  857                         ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
  858                 else
  859                         ia->ia_broadaddr.sin_addr.s_addr =
  860                             htonl(ia->ia_subnet | ~ia->ia_subnetmask);
  861         } else if (ifp->if_flags & IFF_LOOPBACK) {
  862                 ia->ia_dstaddr = ia->ia_addr;
  863                 flags |= RTF_HOST;
  864         } else if (ifp->if_flags & IFF_POINTOPOINT) {
  865                 if (ia->ia_dstaddr.sin_family != AF_INET)
  866                         return (0);
  867                 flags |= RTF_HOST;
  868         }
  869         if (!vhid && (error = in_addprefix(ia, flags)) != 0)
  870                 return (error);
  871 
  872         if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)
  873                 return (0);
  874 
  875         if (ifp->if_flags & IFF_POINTOPOINT &&
  876             ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
  877                         return (0);
  878 
  879         /*
  880          * add a loopback route to self
  881          */
  882         if (V_useloopback && !vhid && !(ifp->if_flags & IFF_LOOPBACK)) {
  883                 struct route ia_ro;
  884 
  885                 bzero(&ia_ro, sizeof(ia_ro));
  886                 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = ia->ia_addr;
  887                 rtalloc_ign_fib(&ia_ro, 0, RT_DEFAULT_FIB);
  888                 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
  889                     (ia_ro.ro_rt->rt_ifp == V_loif)) {
  890                         RT_LOCK(ia_ro.ro_rt);
  891                         RT_ADDREF(ia_ro.ro_rt);
  892                         RTFREE_LOCKED(ia_ro.ro_rt);
  893                 } else
  894                         error = ifa_add_loopback_route((struct ifaddr *)ia,
  895                             (struct sockaddr *)&ia->ia_addr);
  896                 if (error == 0)
  897                         ia->ia_flags |= IFA_RTSELF;
  898                 if (ia_ro.ro_rt != NULL)
  899                         RTFREE(ia_ro.ro_rt);
  900         }
  901 
  902         return (error);
  903 }
  904 
  905 #define rtinitflags(x) \
  906         ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
  907             ? RTF_HOST : 0)
  908 
  909 /*
  910  * Check if we have a route for the given prefix already or add one accordingly.
  911  */
  912 int
  913 in_addprefix(struct in_ifaddr *target, int flags)
  914 {
  915         struct in_ifaddr *ia;
  916         struct in_addr prefix, mask, p, m;
  917         int error;
  918 
  919         if ((flags & RTF_HOST) != 0) {
  920                 prefix = target->ia_dstaddr.sin_addr;
  921                 mask.s_addr = 0;
  922         } else {
  923                 prefix = target->ia_addr.sin_addr;
  924                 mask = target->ia_sockmask.sin_addr;
  925                 prefix.s_addr &= mask.s_addr;
  926         }
  927 
  928         IN_IFADDR_RLOCK();
  929         /* Look for an existing address with the same prefix, mask, and fib */
  930         TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
  931                 if (rtinitflags(ia)) {
  932                         p = ia->ia_dstaddr.sin_addr;
  933 
  934                         if (prefix.s_addr != p.s_addr)
  935                                 continue;
  936                 } else {
  937                         p = ia->ia_addr.sin_addr;
  938                         m = ia->ia_sockmask.sin_addr;
  939                         p.s_addr &= m.s_addr;
  940 
  941                         if (prefix.s_addr != p.s_addr ||
  942                             mask.s_addr != m.s_addr)
  943                                 continue;
  944                 }
  945                 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
  946                         continue;
  947 
  948                 /*
  949                  * If we got a matching prefix route inserted by other
  950                  * interface address, we are done here.
  951                  */
  952                 if (ia->ia_flags & IFA_ROUTE) {
  953 #ifdef RADIX_MPATH
  954                         if (ia->ia_addr.sin_addr.s_addr ==
  955                             target->ia_addr.sin_addr.s_addr) {
  956                                 IN_IFADDR_RUNLOCK();
  957                                 return (EEXIST);
  958                         } else
  959                                 break;
  960 #endif
  961                         if (V_nosameprefix) {
  962                                 IN_IFADDR_RUNLOCK();
  963                                 return (EEXIST);
  964                         } else {
  965                                 int fibnum;
  966 
  967                                 fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS :
  968                                         target->ia_ifp->if_fib;
  969                                 rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum);
  970                                 IN_IFADDR_RUNLOCK();
  971                                 return (0);
  972                         }
  973                 }
  974         }
  975         IN_IFADDR_RUNLOCK();
  976 
  977         /*
  978          * No-one seem to have this prefix route, so we try to insert it.
  979          */
  980         error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
  981         if (!error)
  982                 target->ia_flags |= IFA_ROUTE;
  983         return (error);
  984 }
  985 
  986 /*
  987  * If there is no other address in the system that can serve a route to the
  988  * same prefix, remove the route.  Hand over the route to the new address
  989  * otherwise.
  990  */
  991 int
  992 in_scrubprefix(struct in_ifaddr *target, u_int flags)
  993 {
  994         struct in_ifaddr *ia;
  995         struct in_addr prefix, mask, p, m;
  996         int error = 0;
  997         struct sockaddr_in prefix0, mask0;
  998 
  999         /*
 1000          * Remove the loopback route to the interface address.
 1001          * The "useloopback" setting is not consulted because if the
 1002          * user configures an interface address, turns off this
 1003          * setting, and then tries to delete that interface address,
 1004          * checking the current setting of "useloopback" would leave
 1005          * that interface address loopback route untouched, which
 1006          * would be wrong. Therefore the interface address loopback route
 1007          * deletion is unconditional.
 1008          */
 1009         if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
 1010             !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
 1011             (target->ia_flags & IFA_RTSELF)) {
 1012                 struct route ia_ro;
 1013                 int freeit = 0;
 1014                 int fib;
 1015 
 1016                 bzero(&ia_ro, sizeof(ia_ro));
 1017                 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = target->ia_addr;
 1018                 fib = target->ia_ifa.ifa_ifp->if_fib;
 1019                 rtalloc_ign_fib(&ia_ro, 0, fib);
 1020                 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
 1021                     (ia_ro.ro_rt->rt_ifp == V_loif)) {
 1022                         RT_LOCK(ia_ro.ro_rt);
 1023                         if (ia_ro.ro_rt->rt_refcnt <= 1)
 1024                                 freeit = 1;
 1025                         else if (flags & LLE_STATIC) {
 1026                                 RT_REMREF(ia_ro.ro_rt);
 1027                                 target->ia_flags &= ~IFA_RTSELF;
 1028                         }
 1029                         RTFREE_LOCKED(ia_ro.ro_rt);
 1030                 }
 1031                 if (freeit && (flags & LLE_STATIC)) {
 1032                         error = ifa_del_loopback_route((struct ifaddr *)target,
 1033                             (struct sockaddr *)&target->ia_addr);
 1034                         if (error == 0)
 1035                                 target->ia_flags &= ~IFA_RTSELF;
 1036                 }
 1037                 if ((flags & LLE_STATIC) &&
 1038                         !(target->ia_ifp->if_flags & IFF_NOARP))
 1039                         /* remove arp cache */
 1040                         arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr);
 1041         }
 1042 
 1043         if (rtinitflags(target)) {
 1044                 prefix = target->ia_dstaddr.sin_addr;
 1045                 mask.s_addr = 0;
 1046         } else {
 1047                 prefix = target->ia_addr.sin_addr;
 1048                 mask = target->ia_sockmask.sin_addr;
 1049                 prefix.s_addr &= mask.s_addr;
 1050         }
 1051 
 1052         if ((target->ia_flags & IFA_ROUTE) == 0) {
 1053                 int fibnum;
 1054                 
 1055                 fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS :
 1056                         target->ia_ifp->if_fib;
 1057                 rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum);
 1058                 return (0);
 1059         }
 1060 
 1061         IN_IFADDR_RLOCK();
 1062         TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
 1063                 if (rtinitflags(ia)) {
 1064                         p = ia->ia_dstaddr.sin_addr;
 1065 
 1066                         if (prefix.s_addr != p.s_addr)
 1067                                 continue;
 1068                 } else {
 1069                         p = ia->ia_addr.sin_addr;
 1070                         m = ia->ia_sockmask.sin_addr;
 1071                         p.s_addr &= m.s_addr;
 1072 
 1073                         if (prefix.s_addr != p.s_addr ||
 1074                             mask.s_addr != m.s_addr)
 1075                                 continue;
 1076                 }
 1077 
 1078                 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
 1079                         continue;
 1080 
 1081                 /*
 1082                  * If we got a matching prefix address, move IFA_ROUTE and
 1083                  * the route itself to it.  Make sure that routing daemons
 1084                  * get a heads-up.
 1085                  */
 1086                 if ((ia->ia_flags & IFA_ROUTE) == 0) {
 1087                         ifa_ref(&ia->ia_ifa);
 1088                         IN_IFADDR_RUNLOCK();
 1089                         error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
 1090                             rtinitflags(target));
 1091                         if (error == 0)
 1092                                 target->ia_flags &= ~IFA_ROUTE;
 1093                         else
 1094                                 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
 1095                                         error);
 1096                         error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
 1097                             rtinitflags(ia) | RTF_UP);
 1098                         if (error == 0)
 1099                                 ia->ia_flags |= IFA_ROUTE;
 1100                         else
 1101                                 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
 1102                                         error);
 1103                         ifa_free(&ia->ia_ifa);
 1104                         return (error);
 1105                 }
 1106         }
 1107         IN_IFADDR_RUNLOCK();
 1108 
 1109         /*
 1110          * remove all L2 entries on the given prefix
 1111          */
 1112         bzero(&prefix0, sizeof(prefix0));
 1113         prefix0.sin_len = sizeof(prefix0);
 1114         prefix0.sin_family = AF_INET;
 1115         prefix0.sin_addr.s_addr = target->ia_subnet;
 1116         bzero(&mask0, sizeof(mask0));
 1117         mask0.sin_len = sizeof(mask0);
 1118         mask0.sin_family = AF_INET;
 1119         mask0.sin_addr.s_addr = target->ia_subnetmask;
 1120         lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
 1121             (struct sockaddr *)&mask0, flags);
 1122 
 1123         /*
 1124          * As no-one seem to have this prefix, we can remove the route.
 1125          */
 1126         error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
 1127         if (error == 0)
 1128                 target->ia_flags &= ~IFA_ROUTE;
 1129         else
 1130                 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
 1131         return (error);
 1132 }
 1133 
 1134 #undef rtinitflags
 1135 
 1136 /*
 1137  * Return 1 if the address might be a local broadcast address.
 1138  */
 1139 int
 1140 in_broadcast(struct in_addr in, struct ifnet *ifp)
 1141 {
 1142         register struct ifaddr *ifa;
 1143         u_long t;
 1144 
 1145         if (in.s_addr == INADDR_BROADCAST ||
 1146             in.s_addr == INADDR_ANY)
 1147                 return (1);
 1148         if ((ifp->if_flags & IFF_BROADCAST) == 0)
 1149                 return (0);
 1150         t = ntohl(in.s_addr);
 1151         /*
 1152          * Look through the list of addresses for a match
 1153          * with a broadcast address.
 1154          */
 1155 #define ia ((struct in_ifaddr *)ifa)
 1156         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
 1157                 if (ifa->ifa_addr->sa_family == AF_INET &&
 1158                     (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
 1159                      /*
 1160                       * Check for old-style (host 0) broadcast, but
 1161                       * taking into account that RFC 3021 obsoletes it.
 1162                       */
 1163                     (ia->ia_subnetmask != IN_RFC3021_MASK &&
 1164                     t == ia->ia_subnet)) &&
 1165                      /*
 1166                       * Check for an all one subnetmask. These
 1167                       * only exist when an interface gets a secondary
 1168                       * address.
 1169                       */
 1170                     ia->ia_subnetmask != (u_long)0xffffffff)
 1171                             return (1);
 1172         return (0);
 1173 #undef ia
 1174 }
 1175 
 1176 /*
 1177  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
 1178  */
 1179 void
 1180 in_ifdetach(struct ifnet *ifp)
 1181 {
 1182 
 1183         in_pcbpurgeif0(&V_ripcbinfo, ifp);
 1184         in_pcbpurgeif0(&V_udbinfo, ifp);
 1185         in_pcbpurgeif0(&V_ulitecbinfo, ifp);
 1186         in_purgemaddrs(ifp);
 1187 }
 1188 
 1189 /*
 1190  * Delete all IPv4 multicast address records, and associated link-layer
 1191  * multicast address records, associated with ifp.
 1192  * XXX It looks like domifdetach runs AFTER the link layer cleanup.
 1193  * XXX This should not race with ifma_protospec being set during
 1194  * a new allocation, if it does, we have bigger problems.
 1195  */
 1196 static void
 1197 in_purgemaddrs(struct ifnet *ifp)
 1198 {
 1199         LIST_HEAD(,in_multi) purgeinms;
 1200         struct in_multi         *inm, *tinm;
 1201         struct ifmultiaddr      *ifma;
 1202 
 1203         LIST_INIT(&purgeinms);
 1204         IN_MULTI_LOCK();
 1205 
 1206         /*
 1207          * Extract list of in_multi associated with the detaching ifp
 1208          * which the PF_INET layer is about to release.
 1209          * We need to do this as IF_ADDR_LOCK() may be re-acquired
 1210          * by code further down.
 1211          */
 1212         IF_ADDR_RLOCK(ifp);
 1213         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 1214                 if (ifma->ifma_addr->sa_family != AF_INET ||
 1215                     ifma->ifma_protospec == NULL)
 1216                         continue;
 1217 #if 0
 1218                 KASSERT(ifma->ifma_protospec != NULL,
 1219                     ("%s: ifma_protospec is NULL", __func__));
 1220 #endif
 1221                 inm = (struct in_multi *)ifma->ifma_protospec;
 1222                 LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
 1223         }
 1224         IF_ADDR_RUNLOCK(ifp);
 1225 
 1226         LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
 1227                 LIST_REMOVE(inm, inm_link);
 1228                 inm_release_locked(inm);
 1229         }
 1230         igmp_ifdetach(ifp);
 1231 
 1232         IN_MULTI_UNLOCK();
 1233 }
 1234 
 1235 struct in_llentry {
 1236         struct llentry          base;
 1237         struct sockaddr_in      l3_addr4;
 1238 };
 1239 
 1240 /*
 1241  * Deletes an address from the address table.
 1242  * This function is called by the timer functions
 1243  * such as arptimer() and nd6_llinfo_timer(), and
 1244  * the caller does the locking.
 1245  */
 1246 static void
 1247 in_lltable_free(struct lltable *llt, struct llentry *lle)
 1248 {
 1249         LLE_WUNLOCK(lle);
 1250         LLE_LOCK_DESTROY(lle);
 1251         free(lle, M_LLTABLE);
 1252 }
 1253 
 1254 static struct llentry *
 1255 in_lltable_new(const struct sockaddr *l3addr, u_int flags)
 1256 {
 1257         struct in_llentry *lle;
 1258 
 1259         lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
 1260         if (lle == NULL)                /* NB: caller generates msg */
 1261                 return NULL;
 1262 
 1263         /*
 1264          * For IPv4 this will trigger "arpresolve" to generate
 1265          * an ARP request.
 1266          */
 1267         lle->base.la_expire = time_uptime; /* mark expired */
 1268         lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
 1269         lle->base.lle_refcnt = 1;
 1270         lle->base.lle_free = in_lltable_free;
 1271         LLE_LOCK_INIT(&lle->base);
 1272         callout_init(&lle->base.la_timer, 1);
 1273 
 1274         return (&lle->base);
 1275 }
 1276 
 1277 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)       (                       \
 1278             (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
 1279 
 1280 static void
 1281 in_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
 1282     const struct sockaddr *mask, u_int flags)
 1283 {
 1284         const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
 1285         const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
 1286         struct llentry *lle, *next;
 1287         int i;
 1288         size_t pkts_dropped;
 1289 
 1290         IF_AFDATA_WLOCK(llt->llt_ifp);
 1291         for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
 1292                 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
 1293                         /*
 1294                          * (flags & LLE_STATIC) means deleting all entries
 1295                          * including static ARP entries.
 1296                          */
 1297                         if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)),
 1298                             pfx, msk) && ((flags & LLE_STATIC) ||
 1299                             !(lle->la_flags & LLE_STATIC))) {
 1300                                 LLE_WLOCK(lle);
 1301                                 if (callout_stop(&lle->la_timer))
 1302                                         LLE_REMREF(lle);
 1303                                 pkts_dropped = llentry_free(lle);
 1304                                 ARPSTAT_ADD(dropped, pkts_dropped);
 1305                         }
 1306                 }
 1307         }
 1308         IF_AFDATA_WUNLOCK(llt->llt_ifp);
 1309 }
 1310 
 1311 
 1312 static int
 1313 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
 1314 {
 1315         struct rtentry *rt;
 1316 
 1317         KASSERT(l3addr->sa_family == AF_INET,
 1318             ("sin_family %d", l3addr->sa_family));
 1319 
 1320         /* XXX rtalloc1_fib should take a const param */
 1321         rt = rtalloc1_fib(__DECONST(struct sockaddr *, l3addr), 0, 0,
 1322             ifp->if_fib);
 1323 
 1324         if (rt == NULL)
 1325                 return (EINVAL);
 1326 
 1327         /*
 1328          * If the gateway for an existing host route matches the target L3
 1329          * address, which is a special route inserted by some implementation
 1330          * such as MANET, and the interface is of the correct type, then
 1331          * allow for ARP to proceed.
 1332          */
 1333         if (rt->rt_flags & RTF_GATEWAY) {
 1334                 if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
 1335                     rt->rt_ifp->if_type != IFT_ETHER ||
 1336                     (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
 1337                     memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
 1338                     sizeof(in_addr_t)) != 0) {
 1339                         RTFREE_LOCKED(rt);
 1340                         return (EINVAL);
 1341                 }
 1342         }
 1343 
 1344         /*
 1345          * Make sure that at least the destination address is covered
 1346          * by the route. This is for handling the case where 2 or more
 1347          * interfaces have the same prefix. An incoming packet arrives
 1348          * on one interface and the corresponding outgoing packet leaves
 1349          * another interface.
 1350          */
 1351         if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
 1352                 const char *sa, *mask, *addr, *lim;
 1353                 int len;
 1354 
 1355                 mask = (const char *)rt_mask(rt);
 1356                 /*
 1357                  * Just being extra cautious to avoid some custom
 1358                  * code getting into trouble.
 1359                  */
 1360                 if (mask == NULL) {
 1361                         RTFREE_LOCKED(rt);
 1362                         return (EINVAL);
 1363                 }
 1364 
 1365                 sa = (const char *)rt_key(rt);
 1366                 addr = (const char *)l3addr;
 1367                 len = ((const struct sockaddr_in *)l3addr)->sin_len;
 1368                 lim = addr + len;
 1369 
 1370                 for ( ; addr < lim; sa++, mask++, addr++) {
 1371                         if ((*sa ^ *addr) & *mask) {
 1372 #ifdef DIAGNOSTIC
 1373                                 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
 1374                                     inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
 1375 #endif
 1376                                 RTFREE_LOCKED(rt);
 1377                                 return (EINVAL);
 1378                         }
 1379                 }
 1380         }
 1381 
 1382         RTFREE_LOCKED(rt);
 1383         return (0);
 1384 }
 1385 
 1386 /*
 1387  * Return NULL if not found or marked for deletion.
 1388  * If found return lle read locked.
 1389  */
 1390 static struct llentry *
 1391 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
 1392 {
 1393         const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
 1394         struct ifnet *ifp = llt->llt_ifp;
 1395         struct llentry *lle;
 1396         struct llentries *lleh;
 1397         u_int hashkey;
 1398 
 1399         IF_AFDATA_LOCK_ASSERT(ifp);
 1400         KASSERT(l3addr->sa_family == AF_INET,
 1401             ("sin_family %d", l3addr->sa_family));
 1402 
 1403         hashkey = sin->sin_addr.s_addr;
 1404         lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
 1405         LIST_FOREACH(lle, lleh, lle_next) {
 1406                 struct sockaddr_in *sa2 = satosin(L3_ADDR(lle));
 1407                 if (lle->la_flags & LLE_DELETED)
 1408                         continue;
 1409                 if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
 1410                         break;
 1411         }
 1412         if (lle == NULL) {
 1413 #ifdef DIAGNOSTIC
 1414                 if (flags & LLE_DELETE)
 1415                         log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);
 1416 #endif
 1417                 if (!(flags & LLE_CREATE))
 1418                         return (NULL);
 1419                 IF_AFDATA_WLOCK_ASSERT(ifp);
 1420                 /*
 1421                  * A route that covers the given address must have
 1422                  * been installed 1st because we are doing a resolution,
 1423                  * verify this.
 1424                  */
 1425                 if (!(flags & LLE_IFADDR) &&
 1426                     in_lltable_rtcheck(ifp, flags, l3addr) != 0)
 1427                         goto done;
 1428 
 1429                 lle = in_lltable_new(l3addr, flags);
 1430                 if (lle == NULL) {
 1431                         log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
 1432                         goto done;
 1433                 }
 1434                 lle->la_flags = flags & ~LLE_CREATE;
 1435                 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
 1436                         bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
 1437                         lle->la_flags |= (LLE_VALID | LLE_STATIC);
 1438                 }
 1439 
 1440                 lle->lle_tbl  = llt;
 1441                 lle->lle_head = lleh;
 1442                 lle->la_flags |= LLE_LINKED;
 1443                 LIST_INSERT_HEAD(lleh, lle, lle_next);
 1444         } else if (flags & LLE_DELETE) {
 1445                 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
 1446                         LLE_WLOCK(lle);
 1447                         lle->la_flags |= LLE_DELETED;
 1448                         EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
 1449 #ifdef DIAGNOSTIC
 1450                         log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
 1451 #endif
 1452                         if ((lle->la_flags &
 1453                             (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
 1454                                 llentry_free(lle);
 1455                         else
 1456                                 LLE_WUNLOCK(lle);
 1457                 }
 1458                 lle = (void *)-1;
 1459 
 1460         }
 1461         if (LLE_IS_VALID(lle)) {
 1462                 if (flags & LLE_EXCLUSIVE)
 1463                         LLE_WLOCK(lle);
 1464                 else
 1465                         LLE_RLOCK(lle);
 1466         }
 1467 done:
 1468         return (lle);
 1469 }
 1470 
 1471 static int
 1472 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
 1473 {
 1474 #define SIN(lle)        ((struct sockaddr_in *) L3_ADDR(lle))
 1475         struct ifnet *ifp = llt->llt_ifp;
 1476         struct llentry *lle;
 1477         /* XXX stack use */
 1478         struct {
 1479                 struct rt_msghdr        rtm;
 1480                 struct sockaddr_in      sin;
 1481                 struct sockaddr_dl      sdl;
 1482         } arpc;
 1483         int error, i;
 1484 
 1485         LLTABLE_LOCK_ASSERT();
 1486 
 1487         error = 0;
 1488         for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
 1489                 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
 1490                         struct sockaddr_dl *sdl;
 1491 
 1492                         /* skip deleted entries */
 1493                         if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
 1494                                 continue;
 1495                         /* Skip if jailed and not a valid IP of the prison. */
 1496                         if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
 1497                                 continue;
 1498                         /*
 1499                          * produce a msg made of:
 1500                          *  struct rt_msghdr;
 1501                          *  struct sockaddr_in; (IPv4)
 1502                          *  struct sockaddr_dl;
 1503                          */
 1504                         bzero(&arpc, sizeof(arpc));
 1505                         arpc.rtm.rtm_msglen = sizeof(arpc);
 1506                         arpc.rtm.rtm_version = RTM_VERSION;
 1507                         arpc.rtm.rtm_type = RTM_GET;
 1508                         arpc.rtm.rtm_flags = RTF_UP;
 1509                         arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
 1510                         arpc.sin.sin_family = AF_INET;
 1511                         arpc.sin.sin_len = sizeof(arpc.sin);
 1512                         arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
 1513 
 1514                         /* publish */
 1515                         if (lle->la_flags & LLE_PUB)
 1516                                 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
 1517 
 1518                         sdl = &arpc.sdl;
 1519                         sdl->sdl_family = AF_LINK;
 1520                         sdl->sdl_len = sizeof(*sdl);
 1521                         sdl->sdl_index = ifp->if_index;
 1522                         sdl->sdl_type = ifp->if_type;
 1523                         if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
 1524                                 sdl->sdl_alen = ifp->if_addrlen;
 1525                                 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
 1526                         } else {
 1527                                 sdl->sdl_alen = 0;
 1528                                 bzero(LLADDR(sdl), ifp->if_addrlen);
 1529                         }
 1530 
 1531                         arpc.rtm.rtm_rmx.rmx_expire =
 1532                             lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
 1533                         arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
 1534                         if (lle->la_flags & LLE_STATIC)
 1535                                 arpc.rtm.rtm_flags |= RTF_STATIC;
 1536                         arpc.rtm.rtm_index = ifp->if_index;
 1537                         error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
 1538                         if (error)
 1539                                 break;
 1540                 }
 1541         }
 1542         return error;
 1543 #undef SIN
 1544 }
 1545 
 1546 void *
 1547 in_domifattach(struct ifnet *ifp)
 1548 {
 1549         struct in_ifinfo *ii;
 1550         struct lltable *llt;
 1551 
 1552         ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
 1553 
 1554         llt = lltable_init(ifp, AF_INET);
 1555         if (llt != NULL) {
 1556                 llt->llt_prefix_free = in_lltable_prefix_free;
 1557                 llt->llt_lookup = in_lltable_lookup;
 1558                 llt->llt_dump = in_lltable_dump;
 1559         }
 1560         ii->ii_llt = llt;
 1561 
 1562         ii->ii_igmp = igmp_domifattach(ifp);
 1563 
 1564         return ii;
 1565 }
 1566 
 1567 void
 1568 in_domifdetach(struct ifnet *ifp, void *aux)
 1569 {
 1570         struct in_ifinfo *ii = (struct in_ifinfo *)aux;
 1571 
 1572         igmp_domifdetach(ifp);
 1573         lltable_free(ii->ii_llt);
 1574         free(ii, M_IFADDR);
 1575 }

Cache object: 3fa716946f1fcf1e8fb7761cd33c6932


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