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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (c) 1982, 1986, 1991, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  * Copyright (C) 2001 WIDE Project.  All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  *      @(#)in.c        8.4 (Berkeley) 1/9/95
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD: head/sys/netinet/in.c 345888 2019-04-04 19:01:13Z rgrimes $");
   37 
   38 #include "opt_mpath.h"
   39 
   40 #include <sys/param.h>
   41 #include <sys/eventhandler.h>
   42 #include <sys/systm.h>
   43 #include <sys/sockio.h>
   44 #include <sys/malloc.h>
   45 #include <sys/priv.h>
   46 #include <sys/socket.h>
   47 #include <sys/jail.h>
   48 #include <sys/kernel.h>
   49 #include <sys/lock.h>
   50 #include <sys/proc.h>
   51 #include <sys/rmlock.h>
   52 #include <sys/sysctl.h>
   53 #include <sys/syslog.h>
   54 #include <sys/sx.h>
   55 
   56 #include <net/if.h>
   57 #include <net/if_var.h>
   58 #include <net/if_arp.h>
   59 #include <net/if_dl.h>
   60 #include <net/if_llatbl.h>
   61 #include <net/if_types.h>
   62 #include <net/route.h>
   63 #include <net/vnet.h>
   64 
   65 #include <netinet/if_ether.h>
   66 #include <netinet/in.h>
   67 #include <netinet/in_var.h>
   68 #include <netinet/in_pcb.h>
   69 #include <netinet/ip_var.h>
   70 #include <netinet/ip_carp.h>
   71 #include <netinet/igmp_var.h>
   72 #include <netinet/udp.h>
   73 #include <netinet/udp_var.h>
   74 
   75 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
   76 static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
   77 
   78 static void     in_socktrim(struct sockaddr_in *);
   79 static void     in_purgemaddrs(struct ifnet *);
   80 
   81 VNET_DEFINE_STATIC(int, nosameprefix);
   82 #define V_nosameprefix                  VNET(nosameprefix)
   83 SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW,
   84         &VNET_NAME(nosameprefix), 0,
   85         "Refuse to create same prefixes on different interfaces");
   86 
   87 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
   88 #define V_ripcbinfo                     VNET(ripcbinfo)
   89 
   90 static struct sx in_control_sx;
   91 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
   92 
   93 /*
   94  * Return 1 if an internet address is for a ``local'' host
   95  * (one to which we have a connection).
   96  */
   97 int
   98 in_localaddr(struct in_addr in)
   99 {
  100         struct rm_priotracker in_ifa_tracker;
  101         u_long i = ntohl(in.s_addr);
  102         struct in_ifaddr *ia;
  103 
  104         IN_IFADDR_RLOCK(&in_ifa_tracker);
  105         CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
  106                 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
  107                         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
  108                         return (1);
  109                 }
  110         }
  111         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
  112         return (0);
  113 }
  114 
  115 /*
  116  * Return 1 if an internet address is for the local host and configured
  117  * on one of its interfaces.
  118  */
  119 int
  120 in_localip(struct in_addr in)
  121 {
  122         struct rm_priotracker in_ifa_tracker;
  123         struct in_ifaddr *ia;
  124 
  125         IN_IFADDR_RLOCK(&in_ifa_tracker);
  126         LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
  127                 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
  128                         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
  129                         return (1);
  130                 }
  131         }
  132         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
  133         return (0);
  134 }
  135 
  136 /*
  137  * Return 1 if an internet address is configured on an interface.
  138  */
  139 int
  140 in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
  141 {
  142         struct epoch_tracker et;
  143         struct ifaddr *ifa;
  144         struct in_ifaddr *ia;
  145 
  146         NET_EPOCH_ENTER(et);
  147         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  148                 if (ifa->ifa_addr->sa_family != AF_INET)
  149                         continue;
  150                 ia = (struct in_ifaddr *)ifa;
  151                 if (ia->ia_addr.sin_addr.s_addr == in.s_addr) {
  152                         NET_EPOCH_EXIT(et);
  153                         return (1);
  154                 }
  155         }
  156         NET_EPOCH_EXIT(et);
  157 
  158         return (0);
  159 }
  160 
  161 /*
  162  * Return a reference to the interface address which is different to
  163  * the supplied one but with same IP address value.
  164  */
  165 static struct in_ifaddr *
  166 in_localip_more(struct in_ifaddr *ia)
  167 {
  168         struct rm_priotracker in_ifa_tracker;
  169         in_addr_t in = IA_SIN(ia)->sin_addr.s_addr;
  170         struct in_ifaddr *it;
  171 
  172         IN_IFADDR_RLOCK(&in_ifa_tracker);
  173         LIST_FOREACH(it, INADDR_HASH(in), ia_hash) {
  174                 if (it != ia && IA_SIN(it)->sin_addr.s_addr == in) {
  175                         ifa_ref(&it->ia_ifa);
  176                         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
  177                         return (it);
  178                 }
  179         }
  180         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
  181 
  182         return (NULL);
  183 }
  184 
  185 /*
  186  * Determine whether an IP address is in a reserved set of addresses
  187  * that may not be forwarded, or whether datagrams to that destination
  188  * may be forwarded.
  189  */
  190 int
  191 in_canforward(struct in_addr in)
  192 {
  193         u_long i = ntohl(in.s_addr);
  194 
  195         if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i) ||
  196             IN_ZERONET(i) || IN_LOOPBACK(i))
  197                 return (0);
  198         return (1);
  199 }
  200 
  201 /*
  202  * Trim a mask in a sockaddr
  203  */
  204 static void
  205 in_socktrim(struct sockaddr_in *ap)
  206 {
  207     char *cplim = (char *) &ap->sin_addr;
  208     char *cp = (char *) (&ap->sin_addr + 1);
  209 
  210     ap->sin_len = 0;
  211     while (--cp >= cplim)
  212         if (*cp) {
  213             (ap)->sin_len = cp - (char *) (ap) + 1;
  214             break;
  215         }
  216 }
  217 
  218 /*
  219  * Generic internet control operations (ioctl's).
  220  */
  221 int
  222 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
  223     struct thread *td)
  224 {
  225         struct ifreq *ifr = (struct ifreq *)data;
  226         struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
  227         struct epoch_tracker et;
  228         struct ifaddr *ifa;
  229         struct in_ifaddr *ia;
  230         int error;
  231 
  232         if (ifp == NULL)
  233                 return (EADDRNOTAVAIL);
  234 
  235         /*
  236          * Filter out 4 ioctls we implement directly.  Forward the rest
  237          * to specific functions and ifp->if_ioctl().
  238          */
  239         switch (cmd) {
  240         case SIOCGIFADDR:
  241         case SIOCGIFBRDADDR:
  242         case SIOCGIFDSTADDR:
  243         case SIOCGIFNETMASK:
  244                 break;
  245         case SIOCDIFADDR:
  246                 sx_xlock(&in_control_sx);
  247                 error = in_difaddr_ioctl(cmd, data, ifp, td);
  248                 sx_xunlock(&in_control_sx);
  249                 return (error);
  250         case OSIOCAIFADDR:      /* 9.x compat */
  251         case SIOCAIFADDR:
  252                 sx_xlock(&in_control_sx);
  253                 error = in_aifaddr_ioctl(cmd, data, ifp, td);
  254                 sx_xunlock(&in_control_sx);
  255                 return (error);
  256         case SIOCSIFADDR:
  257         case SIOCSIFBRDADDR:
  258         case SIOCSIFDSTADDR:
  259         case SIOCSIFNETMASK:
  260                 /* We no longer support that old commands. */
  261                 return (EINVAL);
  262         default:
  263                 if (ifp->if_ioctl == NULL)
  264                         return (EOPNOTSUPP);
  265                 return ((*ifp->if_ioctl)(ifp, cmd, data));
  266         }
  267 
  268         if (addr->sin_addr.s_addr != INADDR_ANY &&
  269             prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
  270                 return (EADDRNOTAVAIL);
  271 
  272         /*
  273          * Find address for this interface, if it exists.  If an
  274          * address was specified, find that one instead of the
  275          * first one on the interface, if possible.
  276          */
  277         NET_EPOCH_ENTER(et);
  278         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  279                 if (ifa->ifa_addr->sa_family != AF_INET)
  280                         continue;
  281                 ia = (struct in_ifaddr *)ifa;
  282                 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
  283                         break;
  284         }
  285         if (ifa == NULL)
  286                 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
  287                         if (ifa->ifa_addr->sa_family == AF_INET) {
  288                                 ia = (struct in_ifaddr *)ifa;
  289                                 if (prison_check_ip4(td->td_ucred,
  290                                     &ia->ia_addr.sin_addr) == 0)
  291                                         break;
  292                         }
  293 
  294         if (ifa == NULL) {
  295                 NET_EPOCH_EXIT(et);
  296                 return (EADDRNOTAVAIL);
  297         }
  298 
  299         error = 0;
  300         switch (cmd) {
  301         case SIOCGIFADDR:
  302                 *addr = ia->ia_addr;
  303                 break;
  304 
  305         case SIOCGIFBRDADDR:
  306                 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
  307                         error = EINVAL;
  308                         break;
  309                 }
  310                 *addr = ia->ia_broadaddr;
  311                 break;
  312 
  313         case SIOCGIFDSTADDR:
  314                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
  315                         error = EINVAL;
  316                         break;
  317                 }
  318                 *addr = ia->ia_dstaddr;
  319                 break;
  320 
  321         case SIOCGIFNETMASK:
  322                 *addr = ia->ia_sockmask;
  323                 break;
  324         }
  325 
  326         NET_EPOCH_EXIT(et);
  327 
  328         return (error);
  329 }
  330 
  331 static int
  332 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
  333 {
  334         const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
  335         const struct sockaddr_in *addr = &ifra->ifra_addr;
  336         const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
  337         const struct sockaddr_in *mask = &ifra->ifra_mask;
  338         const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
  339         const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
  340         struct epoch_tracker et;
  341         struct ifaddr *ifa;
  342         struct in_ifaddr *ia;
  343         bool iaIsFirst;
  344         int error = 0;
  345 
  346         error = priv_check(td, PRIV_NET_ADDIFADDR);
  347         if (error)
  348                 return (error);
  349 
  350         /*
  351          * ifra_addr must be present and be of INET family.
  352          * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
  353          */
  354         if (addr->sin_len != sizeof(struct sockaddr_in) ||
  355             addr->sin_family != AF_INET)
  356                 return (EINVAL);
  357         if (broadaddr->sin_len != 0 &&
  358             (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
  359             broadaddr->sin_family != AF_INET))
  360                 return (EINVAL);
  361         if (mask->sin_len != 0 &&
  362             (mask->sin_len != sizeof(struct sockaddr_in) ||
  363             mask->sin_family != AF_INET))
  364                 return (EINVAL);
  365         if ((ifp->if_flags & IFF_POINTOPOINT) &&
  366             (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
  367              dstaddr->sin_addr.s_addr == INADDR_ANY))
  368                 return (EDESTADDRREQ);
  369         if (vhid > 0 && carp_attach_p == NULL)
  370                 return (EPROTONOSUPPORT);
  371 
  372         /*
  373          * See whether address already exist.
  374          */
  375         iaIsFirst = true;
  376         ia = NULL;
  377         NET_EPOCH_ENTER(et);
  378         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  379                 struct in_ifaddr *it;
  380 
  381                 if (ifa->ifa_addr->sa_family != AF_INET)
  382                         continue;
  383 
  384                 it = (struct in_ifaddr *)ifa;
  385                 iaIsFirst = false;
  386                 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
  387                     prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
  388                         ia = it;
  389         }
  390         NET_EPOCH_EXIT(et);
  391 
  392         if (ia != NULL)
  393                 (void )in_difaddr_ioctl(cmd, data, ifp, td);
  394 
  395         ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
  396         ia = (struct in_ifaddr *)ifa;
  397         ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
  398         ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
  399         ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
  400         callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
  401             CALLOUT_RETURNUNLOCKED);
  402 
  403         ia->ia_ifp = ifp;
  404         ia->ia_addr = *addr;
  405         if (mask->sin_len != 0) {
  406                 ia->ia_sockmask = *mask;
  407                 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
  408         } else {
  409                 in_addr_t i = ntohl(addr->sin_addr.s_addr);
  410 
  411                 /*
  412                  * Be compatible with network classes, if netmask isn't
  413                  * supplied, guess it based on classes.
  414                  */
  415                 if (IN_CLASSA(i))
  416                         ia->ia_subnetmask = IN_CLASSA_NET;
  417                 else if (IN_CLASSB(i))
  418                         ia->ia_subnetmask = IN_CLASSB_NET;
  419                 else
  420                         ia->ia_subnetmask = IN_CLASSC_NET;
  421                 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
  422         }
  423         ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
  424         in_socktrim(&ia->ia_sockmask);
  425 
  426         if (ifp->if_flags & IFF_BROADCAST) {
  427                 if (broadaddr->sin_len != 0) {
  428                         ia->ia_broadaddr = *broadaddr;
  429                 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
  430                         ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
  431                         ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
  432                         ia->ia_broadaddr.sin_family = AF_INET;
  433                 } else {
  434                         ia->ia_broadaddr.sin_addr.s_addr =
  435                             htonl(ia->ia_subnet | ~ia->ia_subnetmask);
  436                         ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
  437                         ia->ia_broadaddr.sin_family = AF_INET;
  438                 }
  439         }
  440 
  441         if (ifp->if_flags & IFF_POINTOPOINT)
  442                 ia->ia_dstaddr = *dstaddr;
  443 
  444         /* XXXGL: rtinit() needs this strange assignment. */
  445         if (ifp->if_flags & IFF_LOOPBACK)
  446                 ia->ia_dstaddr = ia->ia_addr;
  447 
  448         if (vhid != 0) {
  449                 error = (*carp_attach_p)(&ia->ia_ifa, vhid);
  450                 if (error)
  451                         return (error);
  452         }
  453 
  454         /* if_addrhead is already referenced by ifa_alloc() */
  455         IF_ADDR_WLOCK(ifp);
  456         CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
  457         IF_ADDR_WUNLOCK(ifp);
  458 
  459         ifa_ref(ifa);                   /* in_ifaddrhead */
  460         IN_IFADDR_WLOCK();
  461         CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
  462         LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
  463         IN_IFADDR_WUNLOCK();
  464 
  465         /*
  466          * Give the interface a chance to initialize
  467          * if this is its first address,
  468          * and to validate the address if necessary.
  469          */
  470         if (ifp->if_ioctl != NULL) {
  471                 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
  472                 if (error)
  473                         goto fail1;
  474         }
  475 
  476         /*
  477          * Add route for the network.
  478          */
  479         if (vhid == 0) {
  480                 int flags = RTF_UP;
  481 
  482                 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
  483                         flags |= RTF_HOST;
  484 
  485                 error = in_addprefix(ia, flags);
  486                 if (error)
  487                         goto fail1;
  488         }
  489 
  490         /*
  491          * Add a loopback route to self.
  492          */
  493         if (vhid == 0 && (ifp->if_flags & IFF_LOOPBACK) == 0 &&
  494             ia->ia_addr.sin_addr.s_addr != INADDR_ANY &&
  495             !((ifp->if_flags & IFF_POINTOPOINT) &&
  496              ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)) {
  497                 struct in_ifaddr *eia;
  498 
  499                 eia = in_localip_more(ia);
  500 
  501                 if (eia == NULL) {
  502                         error = ifa_add_loopback_route((struct ifaddr *)ia,
  503                             (struct sockaddr *)&ia->ia_addr);
  504                         if (error)
  505                                 goto fail2;
  506                 } else
  507                         ifa_free(&eia->ia_ifa);
  508         }
  509 
  510         if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
  511                 struct in_addr allhosts_addr;
  512                 struct in_ifinfo *ii;
  513 
  514                 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
  515                 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
  516 
  517                 error = in_joingroup(ifp, &allhosts_addr, NULL,
  518                         &ii->ii_allhosts);
  519         }
  520 
  521         /*
  522          * Note: we don't need extra reference for ifa, since we called
  523          * with sx lock held, and ifaddr can not be deleted in concurrent
  524          * thread.
  525          */
  526         EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
  527 
  528         return (error);
  529 
  530 fail2:
  531         if (vhid == 0)
  532                 (void )in_scrubprefix(ia, LLE_STATIC);
  533 
  534 fail1:
  535         if (ia->ia_ifa.ifa_carp)
  536                 (*carp_detach_p)(&ia->ia_ifa, false);
  537 
  538         IF_ADDR_WLOCK(ifp);
  539         CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
  540         IF_ADDR_WUNLOCK(ifp);
  541         ifa_free(&ia->ia_ifa);          /* if_addrhead */
  542 
  543         IN_IFADDR_WLOCK();
  544         CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
  545         LIST_REMOVE(ia, ia_hash);
  546         IN_IFADDR_WUNLOCK();
  547         ifa_free(&ia->ia_ifa);          /* in_ifaddrhead */
  548 
  549         return (error);
  550 }
  551 
  552 static int
  553 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
  554 {
  555         const struct ifreq *ifr = (struct ifreq *)data;
  556         const struct sockaddr_in *addr = (const struct sockaddr_in *)
  557             &ifr->ifr_addr;
  558         struct ifaddr *ifa;
  559         struct in_ifaddr *ia;
  560         bool deleteAny, iaIsLast;
  561         int error;
  562 
  563         if (td != NULL) {
  564                 error = priv_check(td, PRIV_NET_DELIFADDR);
  565                 if (error)
  566                         return (error);
  567         }
  568 
  569         if (addr->sin_len != sizeof(struct sockaddr_in) ||
  570             addr->sin_family != AF_INET)
  571                 deleteAny = true;
  572         else
  573                 deleteAny = false;
  574 
  575         iaIsLast = true;
  576         ia = NULL;
  577         IF_ADDR_WLOCK(ifp);
  578         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  579                 struct in_ifaddr *it;
  580 
  581                 if (ifa->ifa_addr->sa_family != AF_INET)
  582                         continue;
  583 
  584                 it = (struct in_ifaddr *)ifa;
  585                 if (deleteAny && ia == NULL && (td == NULL ||
  586                     prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
  587                         ia = it;
  588 
  589                 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
  590                     (td == NULL || prison_check_ip4(td->td_ucred,
  591                     &addr->sin_addr) == 0))
  592                         ia = it;
  593 
  594                 if (it != ia)
  595                         iaIsLast = false;
  596         }
  597 
  598         if (ia == NULL) {
  599                 IF_ADDR_WUNLOCK(ifp);
  600                 return (EADDRNOTAVAIL);
  601         }
  602 
  603         CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
  604         IF_ADDR_WUNLOCK(ifp);
  605         ifa_free(&ia->ia_ifa);          /* if_addrhead */
  606 
  607         IN_IFADDR_WLOCK();
  608         CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
  609         LIST_REMOVE(ia, ia_hash);
  610         IN_IFADDR_WUNLOCK();
  611 
  612         /*
  613          * in_scrubprefix() kills the interface route.
  614          */
  615         in_scrubprefix(ia, LLE_STATIC);
  616 
  617         /*
  618          * in_ifadown gets rid of all the rest of
  619          * the routes.  This is not quite the right
  620          * thing to do, but at least if we are running
  621          * a routing process they will come back.
  622          */
  623         in_ifadown(&ia->ia_ifa, 1);
  624 
  625         if (ia->ia_ifa.ifa_carp)
  626                 (*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
  627 
  628         /*
  629          * If this is the last IPv4 address configured on this
  630          * interface, leave the all-hosts group.
  631          * No state-change report need be transmitted.
  632          */
  633         if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
  634                 struct in_ifinfo *ii;
  635 
  636                 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
  637                 if (ii->ii_allhosts) {
  638                         (void)in_leavegroup(ii->ii_allhosts, NULL);
  639                         ii->ii_allhosts = NULL;
  640                 }
  641         }
  642 
  643         IF_ADDR_WLOCK(ifp);
  644         if (callout_stop(&ia->ia_garp_timer) == 1) {
  645                 ifa_free(&ia->ia_ifa);
  646         }
  647         IF_ADDR_WUNLOCK(ifp);
  648 
  649         EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
  650             IFADDR_EVENT_DEL);
  651         ifa_free(&ia->ia_ifa);          /* in_ifaddrhead */
  652 
  653         return (0);
  654 }
  655 
  656 #define rtinitflags(x) \
  657         ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
  658             ? RTF_HOST : 0)
  659 
  660 /*
  661  * Check if we have a route for the given prefix already or add one accordingly.
  662  */
  663 int
  664 in_addprefix(struct in_ifaddr *target, int flags)
  665 {
  666         struct rm_priotracker in_ifa_tracker;
  667         struct in_ifaddr *ia;
  668         struct in_addr prefix, mask, p, m;
  669         int error;
  670 
  671         if ((flags & RTF_HOST) != 0) {
  672                 prefix = target->ia_dstaddr.sin_addr;
  673                 mask.s_addr = 0;
  674         } else {
  675                 prefix = target->ia_addr.sin_addr;
  676                 mask = target->ia_sockmask.sin_addr;
  677                 prefix.s_addr &= mask.s_addr;
  678         }
  679 
  680         IN_IFADDR_RLOCK(&in_ifa_tracker);
  681         /* Look for an existing address with the same prefix, mask, and fib */
  682         CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
  683                 if (rtinitflags(ia)) {
  684                         p = ia->ia_dstaddr.sin_addr;
  685 
  686                         if (prefix.s_addr != p.s_addr)
  687                                 continue;
  688                 } else {
  689                         p = ia->ia_addr.sin_addr;
  690                         m = ia->ia_sockmask.sin_addr;
  691                         p.s_addr &= m.s_addr;
  692 
  693                         if (prefix.s_addr != p.s_addr ||
  694                             mask.s_addr != m.s_addr)
  695                                 continue;
  696                 }
  697                 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
  698                         continue;
  699 
  700                 /*
  701                  * If we got a matching prefix route inserted by other
  702                  * interface address, we are done here.
  703                  */
  704                 if (ia->ia_flags & IFA_ROUTE) {
  705 #ifdef RADIX_MPATH
  706                         if (ia->ia_addr.sin_addr.s_addr ==
  707                             target->ia_addr.sin_addr.s_addr) {
  708                                 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
  709                                 return (EEXIST);
  710                         } else
  711                                 break;
  712 #endif
  713                         if (V_nosameprefix) {
  714                                 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
  715                                 return (EEXIST);
  716                         } else {
  717                                 int fibnum;
  718 
  719                                 fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS :
  720                                         target->ia_ifp->if_fib;
  721                                 rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum);
  722                                 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
  723                                 return (0);
  724                         }
  725                 }
  726         }
  727         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
  728 
  729         /*
  730          * No-one seem to have this prefix route, so we try to insert it.
  731          */
  732         error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
  733         if (!error)
  734                 target->ia_flags |= IFA_ROUTE;
  735         return (error);
  736 }
  737 
  738 /*
  739  * Removes either all lle entries for given @ia, or lle
  740  * corresponding to @ia address.
  741  */
  742 static void
  743 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
  744 {
  745         struct sockaddr_in addr, mask;
  746         struct sockaddr *saddr, *smask;
  747         struct ifnet *ifp;
  748 
  749         saddr = (struct sockaddr *)&addr;
  750         bzero(&addr, sizeof(addr));
  751         addr.sin_len = sizeof(addr);
  752         addr.sin_family = AF_INET;
  753         smask = (struct sockaddr *)&mask;
  754         bzero(&mask, sizeof(mask));
  755         mask.sin_len = sizeof(mask);
  756         mask.sin_family = AF_INET;
  757         mask.sin_addr.s_addr = ia->ia_subnetmask;
  758         ifp = ia->ia_ifp;
  759 
  760         if (all) {
  761 
  762                 /*
  763                  * Remove all L2 entries matching given prefix.
  764                  * Convert address to host representation to avoid
  765                  * doing this on every callback. ia_subnetmask is already
  766                  * stored in host representation.
  767                  */
  768                 addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
  769                 lltable_prefix_free(AF_INET, saddr, smask, flags);
  770         } else {
  771                 /* Remove interface address only */
  772                 addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
  773                 lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
  774         }
  775 }
  776 
  777 /*
  778  * If there is no other address in the system that can serve a route to the
  779  * same prefix, remove the route.  Hand over the route to the new address
  780  * otherwise.
  781  */
  782 int
  783 in_scrubprefix(struct in_ifaddr *target, u_int flags)
  784 {
  785         struct rm_priotracker in_ifa_tracker;
  786         struct in_ifaddr *ia;
  787         struct in_addr prefix, mask, p, m;
  788         int error = 0;
  789 
  790         /*
  791          * Remove the loopback route to the interface address.
  792          */
  793         if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
  794             !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
  795             (flags & LLE_STATIC)) {
  796                 struct in_ifaddr *eia;
  797 
  798                 /*
  799                  * XXXME: add fib-aware in_localip.
  800                  * We definitely don't want to switch between
  801                  * prefixes in different fibs.
  802                  */
  803                 eia = in_localip_more(target);
  804 
  805                 if (eia != NULL) {
  806                         error = ifa_switch_loopback_route((struct ifaddr *)eia,
  807                             (struct sockaddr *)&target->ia_addr);
  808                         ifa_free(&eia->ia_ifa);
  809                 } else {
  810                         error = ifa_del_loopback_route((struct ifaddr *)target,
  811                             (struct sockaddr *)&target->ia_addr);
  812                 }
  813         }
  814 
  815         if (rtinitflags(target)) {
  816                 prefix = target->ia_dstaddr.sin_addr;
  817                 mask.s_addr = 0;
  818         } else {
  819                 prefix = target->ia_addr.sin_addr;
  820                 mask = target->ia_sockmask.sin_addr;
  821                 prefix.s_addr &= mask.s_addr;
  822         }
  823 
  824         if ((target->ia_flags & IFA_ROUTE) == 0) {
  825                 int fibnum;
  826                 
  827                 fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS :
  828                         target->ia_ifp->if_fib;
  829                 rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum);
  830         
  831                 /*
  832                  * Removing address from !IFF_UP interface or
  833                  * prefix which exists on other interface (along with route).
  834                  * No entries should exist here except target addr.
  835                  * Given that, delete this entry only.
  836                  */
  837                 in_scrubprefixlle(target, 0, flags);
  838                 return (0);
  839         }
  840 
  841         IN_IFADDR_RLOCK(&in_ifa_tracker);
  842         CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
  843                 if (rtinitflags(ia)) {
  844                         p = ia->ia_dstaddr.sin_addr;
  845 
  846                         if (prefix.s_addr != p.s_addr)
  847                                 continue;
  848                 } else {
  849                         p = ia->ia_addr.sin_addr;
  850                         m = ia->ia_sockmask.sin_addr;
  851                         p.s_addr &= m.s_addr;
  852 
  853                         if (prefix.s_addr != p.s_addr ||
  854                             mask.s_addr != m.s_addr)
  855                                 continue;
  856                 }
  857 
  858                 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
  859                         continue;
  860 
  861                 /*
  862                  * If we got a matching prefix address, move IFA_ROUTE and
  863                  * the route itself to it.  Make sure that routing daemons
  864                  * get a heads-up.
  865                  */
  866                 if ((ia->ia_flags & IFA_ROUTE) == 0) {
  867                         ifa_ref(&ia->ia_ifa);
  868                         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
  869                         error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
  870                             rtinitflags(target));
  871                         if (error == 0)
  872                                 target->ia_flags &= ~IFA_ROUTE;
  873                         else
  874                                 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
  875                                         error);
  876                         /* Scrub all entries IFF interface is different */
  877                         in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
  878                             flags);
  879                         error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
  880                             rtinitflags(ia) | RTF_UP);
  881                         if (error == 0)
  882                                 ia->ia_flags |= IFA_ROUTE;
  883                         else
  884                                 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
  885                                         error);
  886                         ifa_free(&ia->ia_ifa);
  887                         return (error);
  888                 }
  889         }
  890         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
  891 
  892         /*
  893          * remove all L2 entries on the given prefix
  894          */
  895         in_scrubprefixlle(target, 1, flags);
  896 
  897         /*
  898          * As no-one seem to have this prefix, we can remove the route.
  899          */
  900         error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
  901         if (error == 0)
  902                 target->ia_flags &= ~IFA_ROUTE;
  903         else
  904                 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
  905         return (error);
  906 }
  907 
  908 #undef rtinitflags
  909 
  910 void
  911 in_ifscrub_all(void)
  912 {
  913         struct ifnet *ifp;
  914         struct ifaddr *ifa, *nifa;
  915         struct ifaliasreq ifr;
  916 
  917         IFNET_RLOCK();
  918         CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
  919                 /* Cannot lock here - lock recursion. */
  920                 /* NET_EPOCH_ENTER(et); */
  921                 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
  922                         if (ifa->ifa_addr->sa_family != AF_INET)
  923                                 continue;
  924 
  925                         /*
  926                          * This is ugly but the only way for legacy IP to
  927                          * cleanly remove addresses and everything attached.
  928                          */
  929                         bzero(&ifr, sizeof(ifr));
  930                         ifr.ifra_addr = *ifa->ifa_addr;
  931                         if (ifa->ifa_dstaddr)
  932                         ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
  933                         (void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
  934                             ifp, NULL);
  935                 }
  936                 /* NET_EPOCH_EXIT(et); */
  937                 in_purgemaddrs(ifp);
  938                 igmp_domifdetach(ifp);
  939         }
  940         IFNET_RUNLOCK();
  941 }
  942 
  943 int
  944 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
  945 {
  946 
  947         return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
  948              /*
  949               * Check for old-style (host 0) broadcast, but
  950               * taking into account that RFC 3021 obsoletes it.
  951               */
  952             (ia->ia_subnetmask != IN_RFC3021_MASK &&
  953             ntohl(in.s_addr) == ia->ia_subnet)) &&
  954              /*
  955               * Check for an all one subnetmask. These
  956               * only exist when an interface gets a secondary
  957               * address.
  958               */
  959             ia->ia_subnetmask != (u_long)0xffffffff);
  960 }
  961 
  962 /*
  963  * Return 1 if the address might be a local broadcast address.
  964  */
  965 int
  966 in_broadcast(struct in_addr in, struct ifnet *ifp)
  967 {
  968         struct epoch_tracker et;
  969         struct ifaddr *ifa;
  970         int found;
  971 
  972         if (in.s_addr == INADDR_BROADCAST ||
  973             in.s_addr == INADDR_ANY)
  974                 return (1);
  975         if ((ifp->if_flags & IFF_BROADCAST) == 0)
  976                 return (0);
  977         found = 0;
  978         /*
  979          * Look through the list of addresses for a match
  980          * with a broadcast address.
  981          */
  982         NET_EPOCH_ENTER(et);
  983         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
  984                 if (ifa->ifa_addr->sa_family == AF_INET &&
  985                     in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) {
  986                         found = 1;
  987                         break;
  988                 }
  989         NET_EPOCH_EXIT(et);
  990         return (found);
  991 }
  992 
  993 /*
  994  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
  995  */
  996 void
  997 in_ifdetach(struct ifnet *ifp)
  998 {
  999         IN_MULTI_LOCK();
 1000         in_pcbpurgeif0(&V_ripcbinfo, ifp);
 1001         in_pcbpurgeif0(&V_udbinfo, ifp);
 1002         in_pcbpurgeif0(&V_ulitecbinfo, ifp);
 1003         in_purgemaddrs(ifp);
 1004         IN_MULTI_UNLOCK();
 1005 }
 1006 
 1007 /*
 1008  * Delete all IPv4 multicast address records, and associated link-layer
 1009  * multicast address records, associated with ifp.
 1010  * XXX It looks like domifdetach runs AFTER the link layer cleanup.
 1011  * XXX This should not race with ifma_protospec being set during
 1012  * a new allocation, if it does, we have bigger problems.
 1013  */
 1014 static void
 1015 in_purgemaddrs(struct ifnet *ifp)
 1016 {
 1017         struct in_multi_head purgeinms;
 1018         struct in_multi         *inm;
 1019         struct ifmultiaddr      *ifma, *next;
 1020 
 1021         SLIST_INIT(&purgeinms);
 1022         IN_MULTI_LIST_LOCK();
 1023 
 1024         /*
 1025          * Extract list of in_multi associated with the detaching ifp
 1026          * which the PF_INET layer is about to release.
 1027          * We need to do this as IF_ADDR_LOCK() may be re-acquired
 1028          * by code further down.
 1029          */
 1030         IF_ADDR_WLOCK(ifp);
 1031  restart:
 1032         CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) {
 1033                 if (ifma->ifma_addr->sa_family != AF_INET ||
 1034                     ifma->ifma_protospec == NULL)
 1035                         continue;
 1036                 inm = (struct in_multi *)ifma->ifma_protospec;
 1037                 inm_rele_locked(&purgeinms, inm);
 1038                 if (__predict_false(ifma_restart)) {
 1039                         ifma_restart = true;
 1040                         goto restart;
 1041                 }
 1042         }
 1043         IF_ADDR_WUNLOCK(ifp);
 1044 
 1045         inm_release_list_deferred(&purgeinms);
 1046         igmp_ifdetach(ifp);
 1047         IN_MULTI_LIST_UNLOCK();
 1048 }
 1049 
 1050 struct in_llentry {
 1051         struct llentry          base;
 1052 };
 1053 
 1054 #define IN_LLTBL_DEFAULT_HSIZE  32
 1055 #define IN_LLTBL_HASH(k, h) \
 1056         (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
 1057 
 1058 /*
 1059  * Do actual deallocation of @lle.
 1060  */
 1061 static void
 1062 in_lltable_destroy_lle_unlocked(epoch_context_t ctx)
 1063 {
 1064         struct llentry *lle;
 1065 
 1066         lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
 1067         LLE_LOCK_DESTROY(lle);
 1068         LLE_REQ_DESTROY(lle);
 1069         free(lle, M_LLTABLE);
 1070 }
 1071 
 1072 /*
 1073  * Called by the datapath to indicate that
 1074  * the entry was used.
 1075  */
 1076 static void
 1077 in_lltable_mark_used(struct llentry *lle)
 1078 {
 1079 
 1080         LLE_REQ_LOCK(lle);
 1081         lle->r_skip_req = 0;
 1082         LLE_REQ_UNLOCK(lle);
 1083 }
 1084 
 1085 /*
 1086  * Called by LLE_FREE_LOCKED when number of references
 1087  * drops to zero.
 1088  */
 1089 static void
 1090 in_lltable_destroy_lle(struct llentry *lle)
 1091 {
 1092 
 1093         LLE_WUNLOCK(lle);
 1094         epoch_call(net_epoch_preempt,  &lle->lle_epoch_ctx, in_lltable_destroy_lle_unlocked);
 1095 }
 1096 
 1097 static struct llentry *
 1098 in_lltable_new(struct in_addr addr4, u_int flags)
 1099 {
 1100         struct in_llentry *lle;
 1101 
 1102         lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
 1103         if (lle == NULL)                /* NB: caller generates msg */
 1104                 return NULL;
 1105 
 1106         /*
 1107          * For IPv4 this will trigger "arpresolve" to generate
 1108          * an ARP request.
 1109          */
 1110         lle->base.la_expire = time_uptime; /* mark expired */
 1111         lle->base.r_l3addr.addr4 = addr4;
 1112         lle->base.lle_refcnt = 1;
 1113         lle->base.lle_free = in_lltable_destroy_lle;
 1114         LLE_LOCK_INIT(&lle->base);
 1115         LLE_REQ_INIT(&lle->base);
 1116         callout_init(&lle->base.lle_timer, 1);
 1117 
 1118         return (&lle->base);
 1119 }
 1120 
 1121 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)       (               \
 1122         ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
 1123 
 1124 static int
 1125 in_lltable_match_prefix(const struct sockaddr *saddr,
 1126     const struct sockaddr *smask, u_int flags, struct llentry *lle)
 1127 {
 1128         struct in_addr addr, mask, lle_addr;
 1129 
 1130         addr = ((const struct sockaddr_in *)saddr)->sin_addr;
 1131         mask = ((const struct sockaddr_in *)smask)->sin_addr;
 1132         lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
 1133 
 1134         if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
 1135                 return (0);
 1136 
 1137         if (lle->la_flags & LLE_IFADDR) {
 1138 
 1139                 /*
 1140                  * Delete LLE_IFADDR records IFF address & flag matches.
 1141                  * Note that addr is the interface address within prefix
 1142                  * being matched.
 1143                  * Note also we should handle 'ifdown' cases without removing
 1144                  * ifaddr macs.
 1145                  */
 1146                 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
 1147                         return (1);
 1148                 return (0);
 1149         }
 1150 
 1151         /* flags & LLE_STATIC means deleting both dynamic and static entries */
 1152         if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
 1153                 return (1);
 1154 
 1155         return (0);
 1156 }
 1157 
 1158 static void
 1159 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
 1160 {
 1161         size_t pkts_dropped;
 1162 
 1163         LLE_WLOCK_ASSERT(lle);
 1164         KASSERT(llt != NULL, ("lltable is NULL"));
 1165 
 1166         /* Unlink entry from table if not already */
 1167         if ((lle->la_flags & LLE_LINKED) != 0) {
 1168                 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
 1169                 lltable_unlink_entry(llt, lle);
 1170         }
 1171 
 1172         /* Drop hold queue */
 1173         pkts_dropped = llentry_free(lle);
 1174         ARPSTAT_ADD(dropped, pkts_dropped);
 1175 }
 1176 
 1177 static int
 1178 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
 1179 {
 1180         struct rt_addrinfo info;
 1181         struct sockaddr_in rt_key, rt_mask;
 1182         struct sockaddr rt_gateway;
 1183         int rt_flags;
 1184 
 1185         KASSERT(l3addr->sa_family == AF_INET,
 1186             ("sin_family %d", l3addr->sa_family));
 1187 
 1188         bzero(&rt_key, sizeof(rt_key));
 1189         rt_key.sin_len = sizeof(rt_key);
 1190         bzero(&rt_mask, sizeof(rt_mask));
 1191         rt_mask.sin_len = sizeof(rt_mask);
 1192         bzero(&rt_gateway, sizeof(rt_gateway));
 1193         rt_gateway.sa_len = sizeof(rt_gateway);
 1194 
 1195         bzero(&info, sizeof(info));
 1196         info.rti_info[RTAX_DST] = (struct sockaddr *)&rt_key;
 1197         info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&rt_mask;
 1198         info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&rt_gateway;
 1199 
 1200         if (rib_lookup_info(ifp->if_fib, l3addr, NHR_REF, 0, &info) != 0)
 1201                 return (EINVAL);
 1202 
 1203         rt_flags = info.rti_flags;
 1204 
 1205         /*
 1206          * If the gateway for an existing host route matches the target L3
 1207          * address, which is a special route inserted by some implementation
 1208          * such as MANET, and the interface is of the correct type, then
 1209          * allow for ARP to proceed.
 1210          */
 1211         if (rt_flags & RTF_GATEWAY) {
 1212                 if (!(rt_flags & RTF_HOST) || !info.rti_ifp ||
 1213                     info.rti_ifp->if_type != IFT_ETHER ||
 1214                     (info.rti_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
 1215                     memcmp(rt_gateway.sa_data, l3addr->sa_data,
 1216                     sizeof(in_addr_t)) != 0) {
 1217                         rib_free_info(&info);
 1218                         return (EINVAL);
 1219                 }
 1220         }
 1221         rib_free_info(&info);
 1222 
 1223         /*
 1224          * Make sure that at least the destination address is covered
 1225          * by the route. This is for handling the case where 2 or more
 1226          * interfaces have the same prefix. An incoming packet arrives
 1227          * on one interface and the corresponding outgoing packet leaves
 1228          * another interface.
 1229          */
 1230         if (!(rt_flags & RTF_HOST) && info.rti_ifp != ifp) {
 1231                 const char *sa, *mask, *addr, *lim;
 1232                 const struct sockaddr_in *l3sin;
 1233 
 1234                 mask = (const char *)&rt_mask;
 1235                 /*
 1236                  * Just being extra cautious to avoid some custom
 1237                  * code getting into trouble.
 1238                  */
 1239                 if ((info.rti_addrs & RTA_NETMASK) == 0)
 1240                         return (EINVAL);
 1241 
 1242                 sa = (const char *)&rt_key;
 1243                 addr = (const char *)l3addr;
 1244                 l3sin = (const struct sockaddr_in *)l3addr;
 1245                 lim = addr + l3sin->sin_len;
 1246 
 1247                 for ( ; addr < lim; sa++, mask++, addr++) {
 1248                         if ((*sa ^ *addr) & *mask) {
 1249 #ifdef DIAGNOSTIC
 1250                                 char addrbuf[INET_ADDRSTRLEN];
 1251 
 1252                                 log(LOG_INFO, "IPv4 address: \"%s\" "
 1253                                     "is not on the network\n",
 1254                                     inet_ntoa_r(l3sin->sin_addr, addrbuf));
 1255 #endif
 1256                                 return (EINVAL);
 1257                         }
 1258                 }
 1259         }
 1260 
 1261         return (0);
 1262 }
 1263 
 1264 static inline uint32_t
 1265 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
 1266 {
 1267 
 1268         return (IN_LLTBL_HASH(dst.s_addr, hsize));
 1269 }
 1270 
 1271 static uint32_t
 1272 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
 1273 {
 1274 
 1275         return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
 1276 }
 1277 
 1278 static void
 1279 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
 1280 {
 1281         struct sockaddr_in *sin;
 1282 
 1283         sin = (struct sockaddr_in *)sa;
 1284         bzero(sin, sizeof(*sin));
 1285         sin->sin_family = AF_INET;
 1286         sin->sin_len = sizeof(*sin);
 1287         sin->sin_addr = lle->r_l3addr.addr4;
 1288 }
 1289 
 1290 static inline struct llentry *
 1291 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
 1292 {
 1293         struct llentry *lle;
 1294         struct llentries *lleh;
 1295         u_int hashidx;
 1296 
 1297         hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
 1298         lleh = &llt->lle_head[hashidx];
 1299         CK_LIST_FOREACH(lle, lleh, lle_next) {
 1300                 if (lle->la_flags & LLE_DELETED)
 1301                         continue;
 1302                 if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
 1303                         break;
 1304         }
 1305 
 1306         return (lle);
 1307 }
 1308 
 1309 static void
 1310 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
 1311 {
 1312 
 1313         lle->la_flags |= LLE_DELETED;
 1314         EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
 1315 #ifdef DIAGNOSTIC
 1316         log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
 1317 #endif
 1318         llentry_free(lle);
 1319 }
 1320 
 1321 static struct llentry *
 1322 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
 1323 {
 1324         const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
 1325         struct ifnet *ifp = llt->llt_ifp;
 1326         struct llentry *lle;
 1327         char linkhdr[LLE_MAX_LINKHDR];
 1328         size_t linkhdrsize;
 1329         int lladdr_off;
 1330 
 1331         KASSERT(l3addr->sa_family == AF_INET,
 1332             ("sin_family %d", l3addr->sa_family));
 1333 
 1334         /*
 1335          * A route that covers the given address must have
 1336          * been installed 1st because we are doing a resolution,
 1337          * verify this.
 1338          */
 1339         if (!(flags & LLE_IFADDR) &&
 1340             in_lltable_rtcheck(ifp, flags, l3addr) != 0)
 1341                 return (NULL);
 1342 
 1343         lle = in_lltable_new(sin->sin_addr, flags);
 1344         if (lle == NULL) {
 1345                 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
 1346                 return (NULL);
 1347         }
 1348         lle->la_flags = flags;
 1349         if (flags & LLE_STATIC)
 1350                 lle->r_flags |= RLLE_VALID;
 1351         if ((flags & LLE_IFADDR) == LLE_IFADDR) {
 1352                 linkhdrsize = LLE_MAX_LINKHDR;
 1353                 if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
 1354                     linkhdr, &linkhdrsize, &lladdr_off) != 0) {
 1355                         epoch_call(net_epoch_preempt,  &lle->lle_epoch_ctx, in_lltable_destroy_lle_unlocked);
 1356                         return (NULL);
 1357                 }
 1358                 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
 1359                     lladdr_off);
 1360                 lle->la_flags |= LLE_STATIC;
 1361                 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
 1362         }
 1363 
 1364         return (lle);
 1365 }
 1366 
 1367 /*
 1368  * Return NULL if not found or marked for deletion.
 1369  * If found return lle read locked.
 1370  */
 1371 static struct llentry *
 1372 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
 1373 {
 1374         const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
 1375         struct llentry *lle;
 1376 
 1377         IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
 1378         KASSERT(l3addr->sa_family == AF_INET,
 1379             ("sin_family %d", l3addr->sa_family));
 1380         KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
 1381             (LLE_UNLOCKED | LLE_EXCLUSIVE),
 1382             ("wrong lle request flags: %#x", flags));
 1383 
 1384         lle = in_lltable_find_dst(llt, sin->sin_addr);
 1385         if (lle == NULL)
 1386                 return (NULL);
 1387         if (flags & LLE_UNLOCKED)
 1388                 return (lle);
 1389 
 1390         if (flags & LLE_EXCLUSIVE)
 1391                 LLE_WLOCK(lle);
 1392         else
 1393                 LLE_RLOCK(lle);
 1394 
 1395         /*
 1396          * If the afdata lock is not held, the LLE may have been unlinked while
 1397          * we were blocked on the LLE lock.  Check for this case.
 1398          */
 1399         if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
 1400                 if (flags & LLE_EXCLUSIVE)
 1401                         LLE_WUNLOCK(lle);
 1402                 else
 1403                         LLE_RUNLOCK(lle);
 1404                 return (NULL);
 1405         }
 1406         return (lle);
 1407 }
 1408 
 1409 static int
 1410 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
 1411     struct sysctl_req *wr)
 1412 {
 1413         struct ifnet *ifp = llt->llt_ifp;
 1414         /* XXX stack use */
 1415         struct {
 1416                 struct rt_msghdr        rtm;
 1417                 struct sockaddr_in      sin;
 1418                 struct sockaddr_dl      sdl;
 1419         } arpc;
 1420         struct sockaddr_dl *sdl;
 1421         int error;
 1422 
 1423         bzero(&arpc, sizeof(arpc));
 1424         /* skip deleted entries */
 1425         if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
 1426                 return (0);
 1427         /* Skip if jailed and not a valid IP of the prison. */
 1428         lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
 1429         if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0)
 1430                 return (0);
 1431         /*
 1432          * produce a msg made of:
 1433          *  struct rt_msghdr;
 1434          *  struct sockaddr_in; (IPv4)
 1435          *  struct sockaddr_dl;
 1436          */
 1437         arpc.rtm.rtm_msglen = sizeof(arpc);
 1438         arpc.rtm.rtm_version = RTM_VERSION;
 1439         arpc.rtm.rtm_type = RTM_GET;
 1440         arpc.rtm.rtm_flags = RTF_UP;
 1441         arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
 1442 
 1443         /* publish */
 1444         if (lle->la_flags & LLE_PUB)
 1445                 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
 1446 
 1447         sdl = &arpc.sdl;
 1448         sdl->sdl_family = AF_LINK;
 1449         sdl->sdl_len = sizeof(*sdl);
 1450         sdl->sdl_index = ifp->if_index;
 1451         sdl->sdl_type = ifp->if_type;
 1452         if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
 1453                 sdl->sdl_alen = ifp->if_addrlen;
 1454                 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
 1455         } else {
 1456                 sdl->sdl_alen = 0;
 1457                 bzero(LLADDR(sdl), ifp->if_addrlen);
 1458         }
 1459 
 1460         arpc.rtm.rtm_rmx.rmx_expire =
 1461             lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
 1462         arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
 1463         if (lle->la_flags & LLE_STATIC)
 1464                 arpc.rtm.rtm_flags |= RTF_STATIC;
 1465         if (lle->la_flags & LLE_IFADDR)
 1466                 arpc.rtm.rtm_flags |= RTF_PINNED;
 1467         arpc.rtm.rtm_index = ifp->if_index;
 1468         error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
 1469 
 1470         return (error);
 1471 }
 1472 
 1473 static struct lltable *
 1474 in_lltattach(struct ifnet *ifp)
 1475 {
 1476         struct lltable *llt;
 1477 
 1478         llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
 1479         llt->llt_af = AF_INET;
 1480         llt->llt_ifp = ifp;
 1481 
 1482         llt->llt_lookup = in_lltable_lookup;
 1483         llt->llt_alloc_entry = in_lltable_alloc;
 1484         llt->llt_delete_entry = in_lltable_delete_entry;
 1485         llt->llt_dump_entry = in_lltable_dump_entry;
 1486         llt->llt_hash = in_lltable_hash;
 1487         llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
 1488         llt->llt_free_entry = in_lltable_free_entry;
 1489         llt->llt_match_prefix = in_lltable_match_prefix;
 1490         llt->llt_mark_used = in_lltable_mark_used;
 1491         lltable_link(llt);
 1492 
 1493         return (llt);
 1494 }
 1495 
 1496 void *
 1497 in_domifattach(struct ifnet *ifp)
 1498 {
 1499         struct in_ifinfo *ii;
 1500 
 1501         ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
 1502 
 1503         ii->ii_llt = in_lltattach(ifp);
 1504         ii->ii_igmp = igmp_domifattach(ifp);
 1505 
 1506         return (ii);
 1507 }
 1508 
 1509 void
 1510 in_domifdetach(struct ifnet *ifp, void *aux)
 1511 {
 1512         struct in_ifinfo *ii = (struct in_ifinfo *)aux;
 1513 
 1514         igmp_domifdetach(ifp);
 1515         lltable_free(ii->ii_llt);
 1516         free(ii, M_IFADDR);
 1517 }

Cache object: b5f9265e232b857b8a15de7c0a916fe5


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