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

Cache object: fad098e7529075e903e6ff49d5fdaf3d


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