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/netinet6/in6.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) 1995, 1996, 1997, and 1998 WIDE Project.
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. Neither the name of the project nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $
   30  */
   31 
   32 /*-
   33  * Copyright (c) 1982, 1986, 1991, 1993
   34  *      The Regents of the University of California.  All rights reserved.
   35  *
   36  * Redistribution and use in source and binary forms, with or without
   37  * modification, are permitted provided that the following conditions
   38  * are met:
   39  * 1. Redistributions of source code must retain the above copyright
   40  *    notice, this list of conditions and the following disclaimer.
   41  * 2. Redistributions in binary form must reproduce the above copyright
   42  *    notice, this list of conditions and the following disclaimer in the
   43  *    documentation and/or other materials provided with the distribution.
   44  * 4. Neither the name of the University nor the names of its contributors
   45  *    may be used to endorse or promote products derived from this software
   46  *    without specific prior written permission.
   47  *
   48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   58  * SUCH DAMAGE.
   59  *
   60  *      @(#)in.c        8.2 (Berkeley) 11/15/93
   61  */
   62 
   63 #include <sys/cdefs.h>
   64 __FBSDID("$FreeBSD$");
   65 
   66 #include "opt_compat.h"
   67 #include "opt_inet.h"
   68 #include "opt_inet6.h"
   69 
   70 #include <sys/param.h>
   71 #include <sys/eventhandler.h>
   72 #include <sys/errno.h>
   73 #include <sys/jail.h>
   74 #include <sys/malloc.h>
   75 #include <sys/socket.h>
   76 #include <sys/socketvar.h>
   77 #include <sys/sockio.h>
   78 #include <sys/systm.h>
   79 #include <sys/priv.h>
   80 #include <sys/proc.h>
   81 #include <sys/time.h>
   82 #include <sys/kernel.h>
   83 #include <sys/lock.h>
   84 #include <sys/rmlock.h>
   85 #include <sys/syslog.h>
   86 
   87 #include <net/if.h>
   88 #include <net/if_var.h>
   89 #include <net/if_types.h>
   90 #include <net/route.h>
   91 #include <net/if_dl.h>
   92 #include <net/vnet.h>
   93 
   94 #include <netinet/in.h>
   95 #include <netinet/in_var.h>
   96 #include <net/if_llatbl.h>
   97 #include <netinet/if_ether.h>
   98 #include <netinet/in_systm.h>
   99 #include <netinet/ip.h>
  100 #include <netinet/in_pcb.h>
  101 #include <netinet/ip_carp.h>
  102 
  103 #include <netinet/ip6.h>
  104 #include <netinet6/ip6_var.h>
  105 #include <netinet6/nd6.h>
  106 #include <netinet6/mld6_var.h>
  107 #include <netinet6/ip6_mroute.h>
  108 #include <netinet6/in6_ifattach.h>
  109 #include <netinet6/scope6_var.h>
  110 #include <netinet6/in6_fib.h>
  111 #include <netinet6/in6_pcb.h>
  112 
  113 VNET_DECLARE(int, icmp6_nodeinfo_oldmcprefix);
  114 #define V_icmp6_nodeinfo_oldmcprefix    VNET(icmp6_nodeinfo_oldmcprefix)
  115 
  116 /*
  117  * Definitions of some costant IP6 addresses.
  118  */
  119 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
  120 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
  121 const struct in6_addr in6addr_nodelocal_allnodes =
  122         IN6ADDR_NODELOCAL_ALLNODES_INIT;
  123 const struct in6_addr in6addr_linklocal_allnodes =
  124         IN6ADDR_LINKLOCAL_ALLNODES_INIT;
  125 const struct in6_addr in6addr_linklocal_allrouters =
  126         IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
  127 const struct in6_addr in6addr_linklocal_allv2routers =
  128         IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
  129 
  130 const struct in6_addr in6mask0 = IN6MASK0;
  131 const struct in6_addr in6mask32 = IN6MASK32;
  132 const struct in6_addr in6mask64 = IN6MASK64;
  133 const struct in6_addr in6mask96 = IN6MASK96;
  134 const struct in6_addr in6mask128 = IN6MASK128;
  135 
  136 const struct sockaddr_in6 sa6_any =
  137         { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
  138 
  139 static int in6_notify_ifa(struct ifnet *, struct in6_ifaddr *,
  140         struct in6_aliasreq *, int);
  141 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
  142 
  143 static int in6_validate_ifra(struct ifnet *, struct in6_aliasreq *,
  144     struct in6_ifaddr *, int);
  145 static struct in6_ifaddr *in6_alloc_ifa(struct ifnet *,
  146     struct in6_aliasreq *, int flags);
  147 static int in6_update_ifa_internal(struct ifnet *, struct in6_aliasreq *,
  148     struct in6_ifaddr *, int, int);
  149 static int in6_broadcast_ifa(struct ifnet *, struct in6_aliasreq *,
  150     struct in6_ifaddr *, int);
  151 
  152 #define ifa2ia6(ifa)    ((struct in6_ifaddr *)(ifa))
  153 #define ia62ifa(ia6)    (&((ia6)->ia_ifa))
  154 
  155 
  156 void
  157 in6_newaddrmsg(struct in6_ifaddr *ia, int cmd)
  158 {
  159         struct sockaddr_dl gateway;
  160         struct sockaddr_in6 mask, addr;
  161         struct rtentry rt;
  162         int fibnum;
  163 
  164         /*
  165          * initialize for rtmsg generation
  166          */
  167         bzero(&gateway, sizeof(gateway));
  168         gateway.sdl_len = sizeof(gateway);
  169         gateway.sdl_family = AF_LINK;
  170 
  171         bzero(&rt, sizeof(rt));
  172         rt.rt_gateway = (struct sockaddr *)&gateway;
  173         memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
  174         memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
  175         rt_mask(&rt) = (struct sockaddr *)&mask;
  176         rt_key(&rt) = (struct sockaddr *)&addr;
  177         rt.rt_flags = RTF_HOST | RTF_STATIC;
  178         if (cmd == RTM_ADD)
  179                 rt.rt_flags |= RTF_UP;
  180         fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS : ia62ifa(ia)->ifa_ifp->if_fib;
  181         /* Announce arrival of local address to this FIB. */
  182         rt_newaddrmsg_fib(cmd, &ia->ia_ifa, 0, &rt, fibnum);
  183 }
  184 
  185 int
  186 in6_mask2len(struct in6_addr *mask, u_char *lim0)
  187 {
  188         int x = 0, y;
  189         u_char *lim = lim0, *p;
  190 
  191         /* ignore the scope_id part */
  192         if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
  193                 lim = (u_char *)mask + sizeof(*mask);
  194         for (p = (u_char *)mask; p < lim; x++, p++) {
  195                 if (*p != 0xff)
  196                         break;
  197         }
  198         y = 0;
  199         if (p < lim) {
  200                 for (y = 0; y < 8; y++) {
  201                         if ((*p & (0x80 >> y)) == 0)
  202                                 break;
  203                 }
  204         }
  205 
  206         /*
  207          * when the limit pointer is given, do a stricter check on the
  208          * remaining bits.
  209          */
  210         if (p < lim) {
  211                 if (y != 0 && (*p & (0x00ff >> y)) != 0)
  212                         return (-1);
  213                 for (p = p + 1; p < lim; p++)
  214                         if (*p != 0)
  215                                 return (-1);
  216         }
  217 
  218         return x * 8 + y;
  219 }
  220 
  221 #ifdef COMPAT_FREEBSD32
  222 struct in6_ndifreq32 {
  223         char ifname[IFNAMSIZ];
  224         uint32_t ifindex;
  225 };
  226 #define SIOCGDEFIFACE32_IN6     _IOWR('i', 86, struct in6_ndifreq32)
  227 #endif
  228 
  229 int
  230 in6_control(struct socket *so, u_long cmd, caddr_t data,
  231     struct ifnet *ifp, struct thread *td)
  232 {
  233         struct  in6_ifreq *ifr = (struct in6_ifreq *)data;
  234         struct  in6_ifaddr *ia = NULL;
  235         struct  in6_aliasreq *ifra = (struct in6_aliasreq *)data;
  236         struct sockaddr_in6 *sa6;
  237         int carp_attached = 0;
  238         int error;
  239         u_long ocmd = cmd;
  240 
  241         /*
  242          * Compat to make pre-10.x ifconfig(8) operable.
  243          */
  244         if (cmd == OSIOCAIFADDR_IN6)
  245                 cmd = SIOCAIFADDR_IN6;
  246 
  247         switch (cmd) {
  248         case SIOCGETSGCNT_IN6:
  249         case SIOCGETMIFCNT_IN6:
  250                 /*
  251                  * XXX mrt_ioctl has a 3rd, unused, FIB argument in route.c.
  252                  * We cannot see how that would be needed, so do not adjust the
  253                  * KPI blindly; more likely should clean up the IPv4 variant.
  254                  */
  255                 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
  256         }
  257 
  258         switch (cmd) {
  259         case SIOCAADDRCTL_POLICY:
  260         case SIOCDADDRCTL_POLICY:
  261                 if (td != NULL) {
  262                         error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
  263                         if (error)
  264                                 return (error);
  265                 }
  266                 return (in6_src_ioctl(cmd, data));
  267         }
  268 
  269         if (ifp == NULL)
  270                 return (EOPNOTSUPP);
  271 
  272         switch (cmd) {
  273         case SIOCSNDFLUSH_IN6:
  274         case SIOCSPFXFLUSH_IN6:
  275         case SIOCSRTRFLUSH_IN6:
  276         case SIOCSDEFIFACE_IN6:
  277         case SIOCSIFINFO_FLAGS:
  278         case SIOCSIFINFO_IN6:
  279                 if (td != NULL) {
  280                         error = priv_check(td, PRIV_NETINET_ND6);
  281                         if (error)
  282                                 return (error);
  283                 }
  284                 /* FALLTHROUGH */
  285         case OSIOCGIFINFO_IN6:
  286         case SIOCGIFINFO_IN6:
  287         case SIOCGNBRINFO_IN6:
  288         case SIOCGDEFIFACE_IN6:
  289                 return (nd6_ioctl(cmd, data, ifp));
  290 
  291 #ifdef COMPAT_FREEBSD32
  292         case SIOCGDEFIFACE32_IN6:
  293                 {
  294                         struct in6_ndifreq ndif;
  295                         struct in6_ndifreq32 *ndif32;
  296 
  297                         error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif,
  298                             ifp);
  299                         if (error)
  300                                 return (error);
  301                         ndif32 = (struct in6_ndifreq32 *)data;
  302                         ndif32->ifindex = ndif.ifindex;
  303                         return (0);
  304                 }
  305 #endif
  306         }
  307 
  308         switch (cmd) {
  309         case SIOCSIFPREFIX_IN6:
  310         case SIOCDIFPREFIX_IN6:
  311         case SIOCAIFPREFIX_IN6:
  312         case SIOCCIFPREFIX_IN6:
  313         case SIOCSGIFPREFIX_IN6:
  314         case SIOCGIFPREFIX_IN6:
  315                 log(LOG_NOTICE,
  316                     "prefix ioctls are now invalidated. "
  317                     "please use ifconfig.\n");
  318                 return (EOPNOTSUPP);
  319         }
  320 
  321         switch (cmd) {
  322         case SIOCSSCOPE6:
  323                 if (td != NULL) {
  324                         error = priv_check(td, PRIV_NETINET_SCOPE6);
  325                         if (error)
  326                                 return (error);
  327                 }
  328                 /* FALLTHROUGH */
  329         case SIOCGSCOPE6:
  330         case SIOCGSCOPE6DEF:
  331                 return (scope6_ioctl(cmd, data, ifp));
  332         }
  333 
  334         /*
  335          * Find address for this interface, if it exists.
  336          *
  337          * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
  338          * only, and used the first interface address as the target of other
  339          * operations (without checking ifra_addr).  This was because netinet
  340          * code/API assumed at most 1 interface address per interface.
  341          * Since IPv6 allows a node to assign multiple addresses
  342          * on a single interface, we almost always look and check the
  343          * presence of ifra_addr, and reject invalid ones here.
  344          * It also decreases duplicated code among SIOC*_IN6 operations.
  345          */
  346         switch (cmd) {
  347         case SIOCAIFADDR_IN6:
  348         case SIOCSIFPHYADDR_IN6:
  349                 sa6 = &ifra->ifra_addr;
  350                 break;
  351         case SIOCSIFADDR_IN6:
  352         case SIOCGIFADDR_IN6:
  353         case SIOCSIFDSTADDR_IN6:
  354         case SIOCSIFNETMASK_IN6:
  355         case SIOCGIFDSTADDR_IN6:
  356         case SIOCGIFNETMASK_IN6:
  357         case SIOCDIFADDR_IN6:
  358         case SIOCGIFPSRCADDR_IN6:
  359         case SIOCGIFPDSTADDR_IN6:
  360         case SIOCGIFAFLAG_IN6:
  361         case SIOCSNDFLUSH_IN6:
  362         case SIOCSPFXFLUSH_IN6:
  363         case SIOCSRTRFLUSH_IN6:
  364         case SIOCGIFALIFETIME_IN6:
  365         case SIOCGIFSTAT_IN6:
  366         case SIOCGIFSTAT_ICMP6:
  367                 sa6 = &ifr->ifr_addr;
  368                 break;
  369         case SIOCSIFADDR:
  370         case SIOCSIFBRDADDR:
  371         case SIOCSIFDSTADDR:
  372         case SIOCSIFNETMASK:
  373                 /*
  374                  * Although we should pass any non-INET6 ioctl requests
  375                  * down to driver, we filter some legacy INET requests.
  376                  * Drivers trust SIOCSIFADDR et al to come from an already
  377                  * privileged layer, and do not perform any credentials
  378                  * checks or input validation.
  379                  */
  380                 return (EINVAL);
  381         default:
  382                 sa6 = NULL;
  383                 break;
  384         }
  385         if (sa6 && sa6->sin6_family == AF_INET6) {
  386                 if (sa6->sin6_scope_id != 0)
  387                         error = sa6_embedscope(sa6, 0);
  388                 else
  389                         error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
  390                 if (error != 0)
  391                         return (error);
  392                 if (td != NULL && (error = prison_check_ip6(td->td_ucred,
  393                     &sa6->sin6_addr)) != 0)
  394                         return (error);
  395                 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
  396         } else
  397                 ia = NULL;
  398 
  399         switch (cmd) {
  400         case SIOCSIFADDR_IN6:
  401         case SIOCSIFDSTADDR_IN6:
  402         case SIOCSIFNETMASK_IN6:
  403                 /*
  404                  * Since IPv6 allows a node to assign multiple addresses
  405                  * on a single interface, SIOCSIFxxx ioctls are deprecated.
  406                  */
  407                 /* we decided to obsolete this command (20000704) */
  408                 error = EINVAL;
  409                 goto out;
  410 
  411         case SIOCDIFADDR_IN6:
  412                 /*
  413                  * for IPv4, we look for existing in_ifaddr here to allow
  414                  * "ifconfig if0 delete" to remove the first IPv4 address on
  415                  * the interface.  For IPv6, as the spec allows multiple
  416                  * interface address from the day one, we consider "remove the
  417                  * first one" semantics to be not preferable.
  418                  */
  419                 if (ia == NULL) {
  420                         error = EADDRNOTAVAIL;
  421                         goto out;
  422                 }
  423                 /* FALLTHROUGH */
  424         case SIOCAIFADDR_IN6:
  425                 /*
  426                  * We always require users to specify a valid IPv6 address for
  427                  * the corresponding operation.
  428                  */
  429                 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
  430                     ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
  431                         error = EAFNOSUPPORT;
  432                         goto out;
  433                 }
  434 
  435                 if (td != NULL) {
  436                         error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
  437                             PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
  438                         if (error)
  439                                 goto out;
  440                 }
  441                 /* FALLTHROUGH */
  442         case SIOCGIFSTAT_IN6:
  443         case SIOCGIFSTAT_ICMP6:
  444                 if (ifp->if_afdata[AF_INET6] == NULL) {
  445                         error = EPFNOSUPPORT;
  446                         goto out;
  447                 }
  448                 break;
  449 
  450         case SIOCGIFADDR_IN6:
  451                 /* This interface is basically deprecated. use SIOCGIFCONF. */
  452                 /* FALLTHROUGH */
  453         case SIOCGIFAFLAG_IN6:
  454         case SIOCGIFNETMASK_IN6:
  455         case SIOCGIFDSTADDR_IN6:
  456         case SIOCGIFALIFETIME_IN6:
  457                 /* must think again about its semantics */
  458                 if (ia == NULL) {
  459                         error = EADDRNOTAVAIL;
  460                         goto out;
  461                 }
  462                 break;
  463         }
  464 
  465         switch (cmd) {
  466         case SIOCGIFADDR_IN6:
  467                 ifr->ifr_addr = ia->ia_addr;
  468                 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
  469                         goto out;
  470                 break;
  471 
  472         case SIOCGIFDSTADDR_IN6:
  473                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
  474                         error = EINVAL;
  475                         goto out;
  476                 }
  477                 /*
  478                  * XXX: should we check if ifa_dstaddr is NULL and return
  479                  * an error?
  480                  */
  481                 ifr->ifr_dstaddr = ia->ia_dstaddr;
  482                 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
  483                         goto out;
  484                 break;
  485 
  486         case SIOCGIFNETMASK_IN6:
  487                 ifr->ifr_addr = ia->ia_prefixmask;
  488                 break;
  489 
  490         case SIOCGIFAFLAG_IN6:
  491                 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
  492                 break;
  493 
  494         case SIOCGIFSTAT_IN6:
  495                 COUNTER_ARRAY_COPY(((struct in6_ifextra *)
  496                     ifp->if_afdata[AF_INET6])->in6_ifstat,
  497                     &ifr->ifr_ifru.ifru_stat,
  498                     sizeof(struct in6_ifstat) / sizeof(uint64_t));
  499                 break;
  500 
  501         case SIOCGIFSTAT_ICMP6:
  502                 COUNTER_ARRAY_COPY(((struct in6_ifextra *)
  503                     ifp->if_afdata[AF_INET6])->icmp6_ifstat,
  504                     &ifr->ifr_ifru.ifru_icmp6stat,
  505                     sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
  506                 break;
  507 
  508         case SIOCGIFALIFETIME_IN6:
  509                 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
  510                 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
  511                         time_t maxexpire;
  512                         struct in6_addrlifetime *retlt =
  513                             &ifr->ifr_ifru.ifru_lifetime;
  514 
  515                         /*
  516                          * XXX: adjust expiration time assuming time_t is
  517                          * signed.
  518                          */
  519                         maxexpire = (-1) &
  520                             ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
  521                         if (ia->ia6_lifetime.ia6t_vltime <
  522                             maxexpire - ia->ia6_updatetime) {
  523                                 retlt->ia6t_expire = ia->ia6_updatetime +
  524                                     ia->ia6_lifetime.ia6t_vltime;
  525                         } else
  526                                 retlt->ia6t_expire = maxexpire;
  527                 }
  528                 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
  529                         time_t maxexpire;
  530                         struct in6_addrlifetime *retlt =
  531                             &ifr->ifr_ifru.ifru_lifetime;
  532 
  533                         /*
  534                          * XXX: adjust expiration time assuming time_t is
  535                          * signed.
  536                          */
  537                         maxexpire = (-1) &
  538                             ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
  539                         if (ia->ia6_lifetime.ia6t_pltime <
  540                             maxexpire - ia->ia6_updatetime) {
  541                                 retlt->ia6t_preferred = ia->ia6_updatetime +
  542                                     ia->ia6_lifetime.ia6t_pltime;
  543                         } else
  544                                 retlt->ia6t_preferred = maxexpire;
  545                 }
  546                 break;
  547 
  548         case SIOCAIFADDR_IN6:
  549         {
  550                 struct nd_prefixctl pr0;
  551                 struct nd_prefix *pr;
  552 
  553                 /*
  554                  * first, make or update the interface address structure,
  555                  * and link it to the list.
  556                  */
  557                 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
  558                         goto out;
  559                 if (ia != NULL) {
  560                         if (ia->ia_ifa.ifa_carp)
  561                                 (*carp_detach_p)(&ia->ia_ifa, true);
  562                         ifa_free(&ia->ia_ifa);
  563                 }
  564                 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
  565                     == NULL) {
  566                         /*
  567                          * this can happen when the user specify the 0 valid
  568                          * lifetime.
  569                          */
  570                         break;
  571                 }
  572 
  573                 if (cmd == ocmd && ifra->ifra_vhid > 0) {
  574                         if (carp_attach_p != NULL)
  575                                 error = (*carp_attach_p)(&ia->ia_ifa,
  576                                     ifra->ifra_vhid);
  577                         else
  578                                 error = EPROTONOSUPPORT;
  579                         if (error)
  580                                 goto out;
  581                         else
  582                                 carp_attached = 1;
  583                 }
  584 
  585                 /*
  586                  * then, make the prefix on-link on the interface.
  587                  * XXX: we'd rather create the prefix before the address, but
  588                  * we need at least one address to install the corresponding
  589                  * interface route, so we configure the address first.
  590                  */
  591 
  592                 /*
  593                  * convert mask to prefix length (prefixmask has already
  594                  * been validated in in6_update_ifa().
  595                  */
  596                 bzero(&pr0, sizeof(pr0));
  597                 pr0.ndpr_ifp = ifp;
  598                 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
  599                     NULL);
  600                 if (pr0.ndpr_plen == 128) {
  601                         /* we don't need to install a host route. */
  602                         goto aifaddr_out;
  603                 }
  604                 pr0.ndpr_prefix = ifra->ifra_addr;
  605                 /* apply the mask for safety. */
  606                 IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr,
  607                     &ifra->ifra_prefixmask.sin6_addr);
  608 
  609                 /*
  610                  * XXX: since we don't have an API to set prefix (not address)
  611                  * lifetimes, we just use the same lifetimes as addresses.
  612                  * The (temporarily) installed lifetimes can be overridden by
  613                  * later advertised RAs (when accept_rtadv is non 0), which is
  614                  * an intended behavior.
  615                  */
  616                 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
  617                 pr0.ndpr_raf_auto =
  618                     ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
  619                 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
  620                 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
  621 
  622                 /* add the prefix if not yet. */
  623                 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
  624                         /*
  625                          * nd6_prelist_add will install the corresponding
  626                          * interface route.
  627                          */
  628                         if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) {
  629                                 if (carp_attached)
  630                                         (*carp_detach_p)(&ia->ia_ifa, false);
  631                                 goto out;
  632                         }
  633                 }
  634 
  635                 /* relate the address to the prefix */
  636                 if (ia->ia6_ndpr == NULL) {
  637                         ia->ia6_ndpr = pr;
  638                         pr->ndpr_addrcnt++;
  639 
  640                         /*
  641                          * If this is the first autoconf address from the
  642                          * prefix, create a temporary address as well
  643                          * (when required).
  644                          */
  645                         if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
  646                             V_ip6_use_tempaddr && pr->ndpr_addrcnt == 1) {
  647                                 int e;
  648                                 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
  649                                         log(LOG_NOTICE, "in6_control: failed "
  650                                             "to create a temporary address, "
  651                                             "errno=%d\n", e);
  652                                 }
  653                         }
  654                 }
  655                 nd6_prefix_rele(pr);
  656 
  657                 /*
  658                  * this might affect the status of autoconfigured addresses,
  659                  * that is, this address might make other addresses detached.
  660                  */
  661                 pfxlist_onlink_check();
  662 
  663 aifaddr_out:
  664                 /*
  665                  * Try to clear the flag when a new IPv6 address is added
  666                  * onto an IFDISABLED interface and it succeeds.
  667                  */
  668                 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
  669                         struct in6_ndireq nd;
  670 
  671                         memset(&nd, 0, sizeof(nd));
  672                         nd.ndi.flags = ND_IFINFO(ifp)->flags;
  673                         nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
  674                         if (nd6_ioctl(SIOCSIFINFO_FLAGS, (caddr_t)&nd, ifp) < 0)
  675                                 log(LOG_NOTICE, "SIOCAIFADDR_IN6: "
  676                                     "SIOCSIFINFO_FLAGS for -ifdisabled "
  677                                     "failed.");
  678                         /*
  679                          * Ignore failure of clearing the flag intentionally.
  680                          * The failure means address duplication was detected.
  681                          */
  682                 }
  683                 break;
  684         }
  685 
  686         case SIOCDIFADDR_IN6:
  687         {
  688                 struct nd_prefix *pr;
  689 
  690                 /*
  691                  * If the address being deleted is the only one that owns
  692                  * the corresponding prefix, expire the prefix as well.
  693                  * XXX: theoretically, we don't have to worry about such
  694                  * relationship, since we separate the address management
  695                  * and the prefix management.  We do this, however, to provide
  696                  * as much backward compatibility as possible in terms of
  697                  * the ioctl operation.
  698                  * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
  699                  */
  700                 pr = ia->ia6_ndpr;
  701                 in6_purgeaddr(&ia->ia_ifa);
  702                 if (pr != NULL && pr->ndpr_addrcnt == 0) {
  703                         ND6_WLOCK();
  704                         nd6_prefix_unlink(pr, NULL);
  705                         ND6_WUNLOCK();
  706                         nd6_prefix_del(pr);
  707                 }
  708                 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
  709                 break;
  710         }
  711 
  712         default:
  713                 if (ifp->if_ioctl == NULL) {
  714                         error = EOPNOTSUPP;
  715                         goto out;
  716                 }
  717                 error = (*ifp->if_ioctl)(ifp, cmd, data);
  718                 goto out;
  719         }
  720 
  721         error = 0;
  722 out:
  723         if (ia != NULL)
  724                 ifa_free(&ia->ia_ifa);
  725         return (error);
  726 }
  727 
  728 
  729 /*
  730  * Join necessary multicast groups.  Factored out from in6_update_ifa().
  731  * This entire work should only be done once, for the default FIB.
  732  */
  733 static int
  734 in6_update_ifa_join_mc(struct ifnet *ifp, struct in6_aliasreq *ifra,
  735     struct in6_ifaddr *ia, int flags, struct in6_multi **in6m_sol)
  736 {
  737         char ip6buf[INET6_ADDRSTRLEN];
  738         struct in6_addr mltaddr;
  739         struct in6_multi_mship *imm;
  740         int delay, error;
  741 
  742         KASSERT(in6m_sol != NULL, ("%s: in6m_sol is NULL", __func__));
  743 
  744         /* Join solicited multicast addr for new host id. */
  745         bzero(&mltaddr, sizeof(struct in6_addr));
  746         mltaddr.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
  747         mltaddr.s6_addr32[2] = htonl(1);
  748         mltaddr.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
  749         mltaddr.s6_addr8[12] = 0xff;
  750         if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0) {
  751                 /* XXX: should not happen */
  752                 log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
  753                 goto cleanup;
  754         }
  755         delay = error = 0;
  756         if ((flags & IN6_IFAUPDATE_DADDELAY)) {
  757                 /*
  758                  * We need a random delay for DAD on the address being
  759                  * configured.  It also means delaying transmission of the
  760                  * corresponding MLD report to avoid report collision.
  761                  * [RFC 4861, Section 6.3.7]
  762                  */
  763                 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
  764         }
  765         imm = in6_joingroup(ifp, &mltaddr, &error, delay);
  766         if (imm == NULL) {
  767                 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
  768                     "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &mltaddr),
  769                     if_name(ifp), error));
  770                 goto cleanup;
  771         }
  772         LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
  773         *in6m_sol = imm->i6mm_maddr;
  774 
  775         /*
  776          * Join link-local all-nodes address.
  777          */
  778         mltaddr = in6addr_linklocal_allnodes;
  779         if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0)
  780                 goto cleanup; /* XXX: should not fail */
  781 
  782         imm = in6_joingroup(ifp, &mltaddr, &error, 0);
  783         if (imm == NULL) {
  784                 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
  785                     "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &mltaddr),
  786                     if_name(ifp), error));
  787                 goto cleanup;
  788         }
  789         LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
  790 
  791         /*
  792          * Join node information group address.
  793          */
  794         delay = 0;
  795         if ((flags & IN6_IFAUPDATE_DADDELAY)) {
  796                 /*
  797                  * The spec does not say anything about delay for this group,
  798                  * but the same logic should apply.
  799                  */
  800                 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
  801         }
  802         if (in6_nigroup(ifp, NULL, -1, &mltaddr) == 0) {
  803                 /* XXX jinmei */
  804                 imm = in6_joingroup(ifp, &mltaddr, &error, delay);
  805                 if (imm == NULL)
  806                         nd6log((LOG_WARNING,
  807                             "%s: in6_joingroup failed for %s on %s "
  808                             "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
  809                             &mltaddr), if_name(ifp), error));
  810                         /* XXX not very fatal, go on... */
  811                 else
  812                         LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
  813         }
  814         if (V_icmp6_nodeinfo_oldmcprefix &&
  815             in6_nigroup_oldmcprefix(ifp, NULL, -1, &mltaddr) == 0) {
  816                 imm = in6_joingroup(ifp, &mltaddr, &error, delay);
  817                 if (imm == NULL)
  818                         nd6log((LOG_WARNING,
  819                             "%s: in6_joingroup failed for %s on %s "
  820                             "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
  821                             &mltaddr), if_name(ifp), error));
  822                         /* XXX not very fatal, go on... */
  823                 else
  824                         LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
  825         }
  826 
  827         /*
  828          * Join interface-local all-nodes address.
  829          * (ff01::1%ifN, and ff01::%ifN/32)
  830          */
  831         mltaddr = in6addr_nodelocal_allnodes;
  832         if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0)
  833                 goto cleanup; /* XXX: should not fail */
  834 
  835         imm = in6_joingroup(ifp, &mltaddr, &error, 0);
  836         if (imm == NULL) {
  837                 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
  838                     "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
  839                     &mltaddr), if_name(ifp), error));
  840                 goto cleanup;
  841         }
  842         LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
  843 
  844 cleanup:
  845         return (error);
  846 }
  847 
  848 /*
  849  * Update parameters of an IPv6 interface address.
  850  * If necessary, a new entry is created and linked into address chains.
  851  * This function is separated from in6_control().
  852  */
  853 int
  854 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
  855     struct in6_ifaddr *ia, int flags)
  856 {
  857         int error, hostIsNew = 0;
  858 
  859         if ((error = in6_validate_ifra(ifp, ifra, ia, flags)) != 0)
  860                 return (error);
  861 
  862         if (ia == NULL) {
  863                 hostIsNew = 1;
  864                 if ((ia = in6_alloc_ifa(ifp, ifra, flags)) == NULL)
  865                         return (ENOBUFS);
  866         }
  867 
  868         error = in6_update_ifa_internal(ifp, ifra, ia, hostIsNew, flags);
  869         if (error != 0) {
  870                 if (hostIsNew != 0) {
  871                         in6_unlink_ifa(ia, ifp);
  872                         ifa_free(&ia->ia_ifa);
  873                 }
  874                 return (error);
  875         }
  876 
  877         if (hostIsNew)
  878                 error = in6_broadcast_ifa(ifp, ifra, ia, flags);
  879 
  880         return (error);
  881 }
  882 
  883 /*
  884  * Fill in basic IPv6 address request info.
  885  */
  886 void
  887 in6_prepare_ifra(struct in6_aliasreq *ifra, const struct in6_addr *addr,
  888     const struct in6_addr *mask)
  889 {
  890 
  891         memset(ifra, 0, sizeof(struct in6_aliasreq));
  892 
  893         ifra->ifra_addr.sin6_family = AF_INET6;
  894         ifra->ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
  895         if (addr != NULL)
  896                 ifra->ifra_addr.sin6_addr = *addr;
  897 
  898         ifra->ifra_prefixmask.sin6_family = AF_INET6;
  899         ifra->ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
  900         if (mask != NULL)
  901                 ifra->ifra_prefixmask.sin6_addr = *mask;
  902 }
  903 
  904 static int
  905 in6_validate_ifra(struct ifnet *ifp, struct in6_aliasreq *ifra,
  906     struct in6_ifaddr *ia, int flags)
  907 {
  908         int plen = -1;
  909         struct sockaddr_in6 dst6;
  910         struct in6_addrlifetime *lt;
  911         char ip6buf[INET6_ADDRSTRLEN];
  912 
  913         /* Validate parameters */
  914         if (ifp == NULL || ifra == NULL) /* this maybe redundant */
  915                 return (EINVAL);
  916 
  917         /*
  918          * The destination address for a p2p link must have a family
  919          * of AF_UNSPEC or AF_INET6.
  920          */
  921         if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
  922             ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
  923             ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
  924                 return (EAFNOSUPPORT);
  925 
  926         /*
  927          * Validate address
  928          */
  929         if (ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6) ||
  930             ifra->ifra_addr.sin6_family != AF_INET6)
  931                 return (EINVAL);
  932 
  933         /*
  934          * validate ifra_prefixmask.  don't check sin6_family, netmask
  935          * does not carry fields other than sin6_len.
  936          */
  937         if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
  938                 return (EINVAL);
  939         /*
  940          * Because the IPv6 address architecture is classless, we require
  941          * users to specify a (non 0) prefix length (mask) for a new address.
  942          * We also require the prefix (when specified) mask is valid, and thus
  943          * reject a non-consecutive mask.
  944          */
  945         if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
  946                 return (EINVAL);
  947         if (ifra->ifra_prefixmask.sin6_len != 0) {
  948                 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
  949                     (u_char *)&ifra->ifra_prefixmask +
  950                     ifra->ifra_prefixmask.sin6_len);
  951                 if (plen <= 0)
  952                         return (EINVAL);
  953         } else {
  954                 /*
  955                  * In this case, ia must not be NULL.  We just use its prefix
  956                  * length.
  957                  */
  958                 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
  959         }
  960         /*
  961          * If the destination address on a p2p interface is specified,
  962          * and the address is a scoped one, validate/set the scope
  963          * zone identifier.
  964          */
  965         dst6 = ifra->ifra_dstaddr;
  966         if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
  967             (dst6.sin6_family == AF_INET6)) {
  968                 struct in6_addr in6_tmp;
  969                 u_int32_t zoneid;
  970 
  971                 in6_tmp = dst6.sin6_addr;
  972                 if (in6_setscope(&in6_tmp, ifp, &zoneid))
  973                         return (EINVAL); /* XXX: should be impossible */
  974 
  975                 if (dst6.sin6_scope_id != 0) {
  976                         if (dst6.sin6_scope_id != zoneid)
  977                                 return (EINVAL);
  978                 } else          /* user omit to specify the ID. */
  979                         dst6.sin6_scope_id = zoneid;
  980 
  981                 /* convert into the internal form */
  982                 if (sa6_embedscope(&dst6, 0))
  983                         return (EINVAL); /* XXX: should be impossible */
  984         }
  985         /* Modify original ifra_dstaddr to reflect changes */
  986         ifra->ifra_dstaddr = dst6;
  987 
  988         /*
  989          * The destination address can be specified only for a p2p or a
  990          * loopback interface.  If specified, the corresponding prefix length
  991          * must be 128.
  992          */
  993         if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
  994                 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
  995                         /* XXX: noisy message */
  996                         nd6log((LOG_INFO, "in6_update_ifa: a destination can "
  997                             "be specified for a p2p or a loopback IF only\n"));
  998                         return (EINVAL);
  999                 }
 1000                 if (plen != 128) {
 1001                         nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
 1002                             "be 128 when dstaddr is specified\n"));
 1003                         return (EINVAL);
 1004                 }
 1005         }
 1006         /* lifetime consistency check */
 1007         lt = &ifra->ifra_lifetime;
 1008         if (lt->ia6t_pltime > lt->ia6t_vltime)
 1009                 return (EINVAL);
 1010         if (lt->ia6t_vltime == 0) {
 1011                 /*
 1012                  * the following log might be noisy, but this is a typical
 1013                  * configuration mistake or a tool's bug.
 1014                  */
 1015                 nd6log((LOG_INFO,
 1016                     "in6_update_ifa: valid lifetime is 0 for %s\n",
 1017                     ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
 1018 
 1019                 if (ia == NULL)
 1020                         return (0); /* there's nothing to do */
 1021         }
 1022 
 1023         /* Check prefix mask */
 1024         if (ia != NULL && ifra->ifra_prefixmask.sin6_len != 0) {
 1025                 /*
 1026                  * We prohibit changing the prefix length of an existing
 1027                  * address, because
 1028                  * + such an operation should be rare in IPv6, and
 1029                  * + the operation would confuse prefix management.
 1030                  */
 1031                 if (ia->ia_prefixmask.sin6_len != 0 &&
 1032                     in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
 1033                         nd6log((LOG_INFO, "in6_validate_ifa: the prefix length "
 1034                             "of an existing %s address should not be changed\n",
 1035                             ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
 1036 
 1037                         return (EINVAL);
 1038                 }
 1039         }
 1040 
 1041         return (0);
 1042 }
 1043 
 1044 
 1045 /*
 1046  * Allocate a new ifaddr and link it into chains.
 1047  */
 1048 static struct in6_ifaddr *
 1049 in6_alloc_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, int flags)
 1050 {
 1051         struct in6_ifaddr *ia;
 1052 
 1053         /*
 1054          * When in6_alloc_ifa() is called in a process of a received
 1055          * RA, it is called under an interrupt context.  So, we should
 1056          * call malloc with M_NOWAIT.
 1057          */
 1058         ia = (struct in6_ifaddr *)ifa_alloc(sizeof(*ia), M_NOWAIT);
 1059         if (ia == NULL)
 1060                 return (NULL);
 1061         LIST_INIT(&ia->ia6_memberships);
 1062         /* Initialize the address and masks, and put time stamp */
 1063         ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
 1064         ia->ia_addr.sin6_family = AF_INET6;
 1065         ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
 1066         /* XXX: Can we assign ,sin6_addr and skip the rest? */
 1067         ia->ia_addr = ifra->ifra_addr;
 1068         ia->ia6_createtime = time_uptime;
 1069         if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
 1070                 /*
 1071                  * Some functions expect that ifa_dstaddr is not
 1072                  * NULL for p2p interfaces.
 1073                  */
 1074                 ia->ia_ifa.ifa_dstaddr =
 1075                     (struct sockaddr *)&ia->ia_dstaddr;
 1076         } else {
 1077                 ia->ia_ifa.ifa_dstaddr = NULL;
 1078         }
 1079 
 1080         /* set prefix mask if any */
 1081         ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
 1082         if (ifra->ifra_prefixmask.sin6_len != 0) {
 1083                 ia->ia_prefixmask.sin6_family = AF_INET6;
 1084                 ia->ia_prefixmask.sin6_len = ifra->ifra_prefixmask.sin6_len;
 1085                 ia->ia_prefixmask.sin6_addr = ifra->ifra_prefixmask.sin6_addr;
 1086         }
 1087 
 1088         ia->ia_ifp = ifp;
 1089         ifa_ref(&ia->ia_ifa);                   /* if_addrhead */
 1090         IF_ADDR_WLOCK(ifp);
 1091         TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
 1092         IF_ADDR_WUNLOCK(ifp);
 1093 
 1094         ifa_ref(&ia->ia_ifa);                   /* in6_ifaddrhead */
 1095         IN6_IFADDR_WLOCK();
 1096         TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
 1097         LIST_INSERT_HEAD(IN6ADDR_HASH(&ia->ia_addr.sin6_addr), ia, ia6_hash);
 1098         IN6_IFADDR_WUNLOCK();
 1099 
 1100         return (ia);
 1101 }
 1102 
 1103 /*
 1104  * Update/configure interface address parameters:
 1105  *
 1106  * 1) Update lifetime
 1107  * 2) Update interface metric ad flags
 1108  * 3) Notify other subsystems
 1109  */
 1110 static int
 1111 in6_update_ifa_internal(struct ifnet *ifp, struct in6_aliasreq *ifra,
 1112     struct in6_ifaddr *ia, int hostIsNew, int flags)
 1113 {
 1114         int error;
 1115 
 1116         /* update timestamp */
 1117         ia->ia6_updatetime = time_uptime;
 1118 
 1119         /*
 1120          * Set lifetimes.  We do not refer to ia6t_expire and ia6t_preferred
 1121          * to see if the address is deprecated or invalidated, but initialize
 1122          * these members for applications.
 1123          */
 1124         ia->ia6_lifetime = ifra->ifra_lifetime;
 1125         if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
 1126                 ia->ia6_lifetime.ia6t_expire =
 1127                     time_uptime + ia->ia6_lifetime.ia6t_vltime;
 1128         } else
 1129                 ia->ia6_lifetime.ia6t_expire = 0;
 1130         if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
 1131                 ia->ia6_lifetime.ia6t_preferred =
 1132                     time_uptime + ia->ia6_lifetime.ia6t_pltime;
 1133         } else
 1134                 ia->ia6_lifetime.ia6t_preferred = 0;
 1135 
 1136         /*
 1137          * backward compatibility - if IN6_IFF_DEPRECATED is set from the
 1138          * userland, make it deprecated.
 1139          */
 1140         if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
 1141                 ia->ia6_lifetime.ia6t_pltime = 0;
 1142                 ia->ia6_lifetime.ia6t_preferred = time_uptime;
 1143         }
 1144 
 1145         /*
 1146          * configure address flags.
 1147          */
 1148         ia->ia6_flags = ifra->ifra_flags;
 1149 
 1150         /*
 1151          * Make the address tentative before joining multicast addresses,
 1152          * so that corresponding MLD responses would not have a tentative
 1153          * source address.
 1154          */
 1155         ia->ia6_flags &= ~IN6_IFF_DUPLICATED;   /* safety */
 1156 
 1157         /*
 1158          * DAD should be performed for an new address or addresses on
 1159          * an interface with ND6_IFF_IFDISABLED.
 1160          */
 1161         if (in6if_do_dad(ifp) &&
 1162             (hostIsNew || (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)))
 1163                 ia->ia6_flags |= IN6_IFF_TENTATIVE;
 1164 
 1165         /* notify other subsystems */
 1166         error = in6_notify_ifa(ifp, ia, ifra, hostIsNew);
 1167 
 1168         return (error);
 1169 }
 1170 
 1171 /*
 1172  * Do link-level ifa job:
 1173  * 1) Add lle entry for added address
 1174  * 2) Notifies routing socket users about new address
 1175  * 3) join appropriate multicast group
 1176  * 4) start DAD if enabled
 1177  */
 1178 static int
 1179 in6_broadcast_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
 1180     struct in6_ifaddr *ia, int flags)
 1181 {
 1182         struct in6_multi *in6m_sol;
 1183         int error = 0;
 1184 
 1185         /* Add local address to lltable, if necessary (ex. on p2p link). */
 1186         if ((error = nd6_add_ifa_lle(ia)) != 0) {
 1187                 in6_purgeaddr(&ia->ia_ifa);
 1188                 ifa_free(&ia->ia_ifa);
 1189                 return (error);
 1190         }
 1191 
 1192         /* Join necessary multicast groups. */
 1193         in6m_sol = NULL;
 1194         if ((ifp->if_flags & IFF_MULTICAST) != 0) {
 1195                 error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol);
 1196                 if (error != 0) {
 1197                         in6_purgeaddr(&ia->ia_ifa);
 1198                         ifa_free(&ia->ia_ifa);
 1199                         return (error);
 1200                 }
 1201         }
 1202 
 1203         /* Perform DAD, if the address is TENTATIVE. */
 1204         if ((ia->ia6_flags & IN6_IFF_TENTATIVE)) {
 1205                 int delay, mindelay, maxdelay;
 1206 
 1207                 delay = 0;
 1208                 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
 1209                         /*
 1210                          * We need to impose a delay before sending an NS
 1211                          * for DAD.  Check if we also needed a delay for the
 1212                          * corresponding MLD message.  If we did, the delay
 1213                          * should be larger than the MLD delay (this could be
 1214                          * relaxed a bit, but this simple logic is at least
 1215                          * safe).
 1216                          * XXX: Break data hiding guidelines and look at
 1217                          * state for the solicited multicast group.
 1218                          */
 1219                         mindelay = 0;
 1220                         if (in6m_sol != NULL &&
 1221                             in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
 1222                                 mindelay = in6m_sol->in6m_timer;
 1223                         }
 1224                         maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
 1225                         if (maxdelay - mindelay == 0)
 1226                                 delay = 0;
 1227                         else {
 1228                                 delay =
 1229                                     (arc4random() % (maxdelay - mindelay)) +
 1230                                     mindelay;
 1231                         }
 1232                 }
 1233                 nd6_dad_start((struct ifaddr *)ia, delay);
 1234         }
 1235 
 1236         in6_newaddrmsg(ia, RTM_ADD);
 1237         ifa_free(&ia->ia_ifa);
 1238         return (error);
 1239 }
 1240 
 1241 void
 1242 in6_purgeaddr(struct ifaddr *ifa)
 1243 {
 1244         struct ifnet *ifp = ifa->ifa_ifp;
 1245         struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
 1246         struct in6_multi_mship *imm;
 1247         int plen, error;
 1248 
 1249         if (ifa->ifa_carp)
 1250                 (*carp_detach_p)(ifa, false);
 1251 
 1252         /*
 1253          * Remove the loopback route to the interface address.
 1254          * The check for the current setting of "nd6_useloopback"
 1255          * is not needed.
 1256          */
 1257         if (ia->ia_flags & IFA_RTSELF) {
 1258                 error = ifa_del_loopback_route((struct ifaddr *)ia,
 1259                     (struct sockaddr *)&ia->ia_addr);
 1260                 if (error == 0)
 1261                         ia->ia_flags &= ~IFA_RTSELF;
 1262         }
 1263 
 1264         /* stop DAD processing */
 1265         nd6_dad_stop(ifa);
 1266 
 1267         /* Leave multicast groups. */
 1268         while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
 1269                 LIST_REMOVE(imm, i6mm_chain);
 1270                 in6_leavegroup(imm);
 1271         }
 1272         plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
 1273         if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
 1274                 error = rtinit(&(ia->ia_ifa), RTM_DELETE, ia->ia_flags |
 1275                     (ia->ia_dstaddr.sin6_family == AF_INET6 ? RTF_HOST : 0));
 1276                 if (error != 0)
 1277                         log(LOG_INFO, "%s: err=%d, destination address delete "
 1278                             "failed\n", __func__, error);
 1279                 ia->ia_flags &= ~IFA_ROUTE;
 1280         }
 1281 
 1282         in6_newaddrmsg(ia, RTM_DELETE);
 1283         in6_unlink_ifa(ia, ifp);
 1284 }
 1285 
 1286 static void
 1287 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
 1288 {
 1289         char ip6buf[INET6_ADDRSTRLEN];
 1290         int remove_lle;
 1291 
 1292         IF_ADDR_WLOCK(ifp);
 1293         TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
 1294         IF_ADDR_WUNLOCK(ifp);
 1295         ifa_free(&ia->ia_ifa);                  /* if_addrhead */
 1296 
 1297         /*
 1298          * Defer the release of what might be the last reference to the
 1299          * in6_ifaddr so that it can't be freed before the remainder of the
 1300          * cleanup.
 1301          */
 1302         IN6_IFADDR_WLOCK();
 1303         TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
 1304         LIST_REMOVE(ia, ia6_hash);
 1305         IN6_IFADDR_WUNLOCK();
 1306 
 1307         /*
 1308          * Release the reference to the base prefix.  There should be a
 1309          * positive reference.
 1310          */
 1311         remove_lle = 0;
 1312         if (ia->ia6_ndpr == NULL) {
 1313                 nd6log((LOG_NOTICE,
 1314                     "in6_unlink_ifa: autoconf'ed address "
 1315                     "%s has no prefix\n", ip6_sprintf(ip6buf, IA6_IN6(ia))));
 1316         } else {
 1317                 ia->ia6_ndpr->ndpr_addrcnt--;
 1318                 /* Do not delete lles within prefix if refcont != 0 */
 1319                 if (ia->ia6_ndpr->ndpr_addrcnt == 0)
 1320                         remove_lle = 1;
 1321                 ia->ia6_ndpr = NULL;
 1322         }
 1323 
 1324         nd6_rem_ifa_lle(ia, remove_lle);
 1325 
 1326         /*
 1327          * Also, if the address being removed is autoconf'ed, call
 1328          * pfxlist_onlink_check() since the release might affect the status of
 1329          * other (detached) addresses.
 1330          */
 1331         if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
 1332                 pfxlist_onlink_check();
 1333         }
 1334         ifa_free(&ia->ia_ifa);                  /* in6_ifaddrhead */
 1335 }
 1336 
 1337 /*
 1338  * Notifies other subsystems about address change/arrival:
 1339  * 1) Notifies device handler on the first IPv6 address assignment
 1340  * 2) Handle routing table changes for P2P links and route
 1341  * 3) Handle routing table changes for address host route
 1342  */
 1343 static int
 1344 in6_notify_ifa(struct ifnet *ifp, struct in6_ifaddr *ia,
 1345     struct in6_aliasreq *ifra, int hostIsNew)
 1346 {
 1347         int     error = 0, plen, ifacount = 0;
 1348         struct ifaddr *ifa;
 1349         struct sockaddr_in6 *pdst;
 1350         char ip6buf[INET6_ADDRSTRLEN];
 1351 
 1352         /*
 1353          * Give the interface a chance to initialize
 1354          * if this is its first address,
 1355          */
 1356         if (hostIsNew != 0) {
 1357                 IF_ADDR_RLOCK(ifp);
 1358                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1359                         if (ifa->ifa_addr->sa_family != AF_INET6)
 1360                                 continue;
 1361                         ifacount++;
 1362                 }
 1363                 IF_ADDR_RUNLOCK(ifp);
 1364         }
 1365 
 1366         if (ifacount <= 1 && ifp->if_ioctl) {
 1367                 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
 1368                 if (error)
 1369                         goto done;
 1370         }
 1371 
 1372         /*
 1373          * If a new destination address is specified, scrub the old one and
 1374          * install the new destination.  Note that the interface must be
 1375          * p2p or loopback.
 1376          */
 1377         pdst = &ifra->ifra_dstaddr;
 1378         if (pdst->sin6_family == AF_INET6 &&
 1379             !IN6_ARE_ADDR_EQUAL(&pdst->sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
 1380                 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
 1381                     (rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST) != 0)) {
 1382                         nd6log((LOG_ERR, "in6_update_ifa_internal: failed to "
 1383                             "remove a route to the old destination: %s\n",
 1384                             ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
 1385                         /* proceed anyway... */
 1386                 } else
 1387                         ia->ia_flags &= ~IFA_ROUTE;
 1388                 ia->ia_dstaddr = *pdst;
 1389         }
 1390 
 1391         /*
 1392          * If a new destination address is specified for a point-to-point
 1393          * interface, install a route to the destination as an interface
 1394          * direct route.
 1395          * XXX: the logic below rejects assigning multiple addresses on a p2p
 1396          * interface that share the same destination.
 1397          */
 1398         plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
 1399         if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
 1400             ia->ia_dstaddr.sin6_family == AF_INET6) {
 1401                 int rtflags = RTF_UP | RTF_HOST;
 1402                 /*
 1403                  * Handle the case for ::1 .
 1404                  */
 1405                 if (ifp->if_flags & IFF_LOOPBACK)
 1406                         ia->ia_flags |= IFA_RTSELF;
 1407                 error = rtinit(&ia->ia_ifa, RTM_ADD, ia->ia_flags | rtflags);
 1408                 if (error)
 1409                         goto done;
 1410                 ia->ia_flags |= IFA_ROUTE;
 1411         }
 1412 
 1413         /*
 1414          * add a loopback route to self if not exists
 1415          */
 1416         if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
 1417                 error = ifa_add_loopback_route((struct ifaddr *)ia,
 1418                     (struct sockaddr *)&ia->ia_addr);
 1419                 if (error == 0)
 1420                         ia->ia_flags |= IFA_RTSELF;
 1421         }
 1422 done:
 1423         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
 1424             "Invoking IPv6 network device address event may sleep");
 1425 
 1426         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
 1427 
 1428         return (error);
 1429 }
 1430 
 1431 /*
 1432  * Find an IPv6 interface link-local address specific to an interface.
 1433  * ifaddr is returned referenced.
 1434  */
 1435 struct in6_ifaddr *
 1436 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
 1437 {
 1438         struct ifaddr *ifa;
 1439 
 1440         IF_ADDR_RLOCK(ifp);
 1441         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1442                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1443                         continue;
 1444                 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
 1445                         if ((((struct in6_ifaddr *)ifa)->ia6_flags &
 1446                             ignoreflags) != 0)
 1447                                 continue;
 1448                         ifa_ref(ifa);
 1449                         break;
 1450                 }
 1451         }
 1452         IF_ADDR_RUNLOCK(ifp);
 1453 
 1454         return ((struct in6_ifaddr *)ifa);
 1455 }
 1456 
 1457 
 1458 /*
 1459  * find the interface address corresponding to a given IPv6 address.
 1460  * ifaddr is returned referenced.
 1461  */
 1462 struct in6_ifaddr *
 1463 in6ifa_ifwithaddr(const struct in6_addr *addr, uint32_t zoneid)
 1464 {
 1465         struct rm_priotracker in6_ifa_tracker;
 1466         struct in6_ifaddr *ia;
 1467 
 1468         IN6_IFADDR_RLOCK(&in6_ifa_tracker);
 1469         LIST_FOREACH(ia, IN6ADDR_HASH(addr), ia6_hash) {
 1470                 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), addr)) {
 1471                         if (zoneid != 0 &&
 1472                             zoneid != ia->ia_addr.sin6_scope_id)
 1473                                 continue;
 1474                         ifa_ref(&ia->ia_ifa);
 1475                         break;
 1476                 }
 1477         }
 1478         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
 1479         return (ia);
 1480 }
 1481 
 1482 /*
 1483  * find the internet address corresponding to a given interface and address.
 1484  * ifaddr is returned referenced.
 1485  */
 1486 struct in6_ifaddr *
 1487 in6ifa_ifpwithaddr(struct ifnet *ifp, const struct in6_addr *addr)
 1488 {
 1489         struct ifaddr *ifa;
 1490 
 1491         IF_ADDR_RLOCK(ifp);
 1492         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1493                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1494                         continue;
 1495                 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
 1496                         ifa_ref(ifa);
 1497                         break;
 1498                 }
 1499         }
 1500         IF_ADDR_RUNLOCK(ifp);
 1501 
 1502         return ((struct in6_ifaddr *)ifa);
 1503 }
 1504 
 1505 /*
 1506  * Find a link-local scoped address on ifp and return it if any.
 1507  */
 1508 struct in6_ifaddr *
 1509 in6ifa_llaonifp(struct ifnet *ifp)
 1510 {
 1511         struct sockaddr_in6 *sin6;
 1512         struct ifaddr *ifa;
 1513 
 1514         if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
 1515                 return (NULL);
 1516         IF_ADDR_RLOCK(ifp);
 1517         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1518                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1519                         continue;
 1520                 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
 1521                 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
 1522                     IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
 1523                     IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
 1524                         break;
 1525         }
 1526         IF_ADDR_RUNLOCK(ifp);
 1527 
 1528         return ((struct in6_ifaddr *)ifa);
 1529 }
 1530 
 1531 /*
 1532  * Convert IP6 address to printable (loggable) representation. Caller
 1533  * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
 1534  */
 1535 static char digits[] = "0123456789abcdef";
 1536 char *
 1537 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
 1538 {
 1539         int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
 1540         char *cp;
 1541         const u_int16_t *a = (const u_int16_t *)addr;
 1542         const u_int8_t *d;
 1543         int dcolon = 0, zero = 0;
 1544 
 1545         cp = ip6buf;
 1546 
 1547         for (i = 0; i < 8; i++) {
 1548                 if (*(a + i) == 0) {
 1549                         cnt++;
 1550                         if (cnt == 1)
 1551                                 idx = i;
 1552                 }
 1553                 else if (maxcnt < cnt) {
 1554                         maxcnt = cnt;
 1555                         index = idx;
 1556                         cnt = 0;
 1557                 }
 1558         }
 1559         if (maxcnt < cnt) {
 1560                 maxcnt = cnt;
 1561                 index = idx;
 1562         }
 1563 
 1564         for (i = 0; i < 8; i++) {
 1565                 if (dcolon == 1) {
 1566                         if (*a == 0) {
 1567                                 if (i == 7)
 1568                                         *cp++ = ':';
 1569                                 a++;
 1570                                 continue;
 1571                         } else
 1572                                 dcolon = 2;
 1573                 }
 1574                 if (*a == 0) {
 1575                         if (dcolon == 0 && *(a + 1) == 0 && i == index) {
 1576                                 if (i == 0)
 1577                                         *cp++ = ':';
 1578                                 *cp++ = ':';
 1579                                 dcolon = 1;
 1580                         } else {
 1581                                 *cp++ = '';
 1582                                 *cp++ = ':';
 1583                         }
 1584                         a++;
 1585                         continue;
 1586                 }
 1587                 d = (const u_char *)a;
 1588                 /* Try to eliminate leading zeros in printout like in :0001. */
 1589                 zero = 1;
 1590                 *cp = digits[*d >> 4];
 1591                 if (*cp != '') {
 1592                         zero = 0;
 1593                         cp++;
 1594                 }
 1595                 *cp = digits[*d++ & 0xf];
 1596                 if (zero == 0 || (*cp != '')) {
 1597                         zero = 0;
 1598                         cp++;
 1599                 }
 1600                 *cp = digits[*d >> 4];
 1601                 if (zero == 0 || (*cp != '')) {
 1602                         zero = 0;
 1603                         cp++;
 1604                 }
 1605                 *cp++ = digits[*d & 0xf];
 1606                 *cp++ = ':';
 1607                 a++;
 1608         }
 1609         *--cp = '\0';
 1610         return (ip6buf);
 1611 }
 1612 
 1613 int
 1614 in6_localaddr(struct in6_addr *in6)
 1615 {
 1616         struct rm_priotracker in6_ifa_tracker;
 1617         struct in6_ifaddr *ia;
 1618 
 1619         if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
 1620                 return 1;
 1621 
 1622         IN6_IFADDR_RLOCK(&in6_ifa_tracker);
 1623         TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
 1624                 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
 1625                     &ia->ia_prefixmask.sin6_addr)) {
 1626                         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
 1627                         return 1;
 1628                 }
 1629         }
 1630         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
 1631 
 1632         return (0);
 1633 }
 1634 
 1635 /*
 1636  * Return 1 if an internet address is for the local host and configured
 1637  * on one of its interfaces.
 1638  */
 1639 int
 1640 in6_localip(struct in6_addr *in6)
 1641 {
 1642         struct rm_priotracker in6_ifa_tracker;
 1643         struct in6_ifaddr *ia;
 1644 
 1645         IN6_IFADDR_RLOCK(&in6_ifa_tracker);
 1646         LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) {
 1647                 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) {
 1648                         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
 1649                         return (1);
 1650                 }
 1651         }
 1652         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
 1653         return (0);
 1654 }
 1655  
 1656 /*
 1657  * Return 1 if an internet address is configured on an interface.
 1658  */
 1659 int
 1660 in6_ifhasaddr(struct ifnet *ifp, struct in6_addr *addr)
 1661 {
 1662         struct in6_addr in6;
 1663         struct ifaddr *ifa;
 1664         struct in6_ifaddr *ia6;
 1665 
 1666         in6 = *addr;
 1667         if (in6_clearscope(&in6))
 1668                 return (0);
 1669         in6_setscope(&in6, ifp, NULL);
 1670 
 1671         IF_ADDR_RLOCK(ifp);
 1672         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1673                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1674                         continue;
 1675                 ia6 = (struct in6_ifaddr *)ifa;
 1676                 if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &in6)) {
 1677                         IF_ADDR_RUNLOCK(ifp);
 1678                         return (1);
 1679                 }
 1680         }
 1681         IF_ADDR_RUNLOCK(ifp);
 1682 
 1683         return (0);
 1684 }
 1685 
 1686 int
 1687 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
 1688 {
 1689         struct rm_priotracker in6_ifa_tracker;
 1690         struct in6_ifaddr *ia;
 1691 
 1692         IN6_IFADDR_RLOCK(&in6_ifa_tracker);
 1693         LIST_FOREACH(ia, IN6ADDR_HASH(&sa6->sin6_addr), ia6_hash) {
 1694                 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), &sa6->sin6_addr)) {
 1695                         if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
 1696                                 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
 1697                                 return (1); /* true */
 1698                         }
 1699                         break;
 1700                 }
 1701         }
 1702         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
 1703 
 1704         return (0);             /* false */
 1705 }
 1706 
 1707 /*
 1708  * return length of part which dst and src are equal
 1709  * hard coding...
 1710  */
 1711 int
 1712 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
 1713 {
 1714         int match = 0;
 1715         u_char *s = (u_char *)src, *d = (u_char *)dst;
 1716         u_char *lim = s + 16, r;
 1717 
 1718         while (s < lim)
 1719                 if ((r = (*d++ ^ *s++)) != 0) {
 1720                         while (r < 128) {
 1721                                 match++;
 1722                                 r <<= 1;
 1723                         }
 1724                         break;
 1725                 } else
 1726                         match += 8;
 1727         return match;
 1728 }
 1729 
 1730 /* XXX: to be scope conscious */
 1731 int
 1732 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
 1733 {
 1734         int bytelen, bitlen;
 1735 
 1736         /* sanity check */
 1737         if (0 > len || len > 128) {
 1738                 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
 1739                     len);
 1740                 return (0);
 1741         }
 1742 
 1743         bytelen = len / 8;
 1744         bitlen = len % 8;
 1745 
 1746         if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
 1747                 return (0);
 1748         if (bitlen != 0 &&
 1749             p1->s6_addr[bytelen] >> (8 - bitlen) !=
 1750             p2->s6_addr[bytelen] >> (8 - bitlen))
 1751                 return (0);
 1752 
 1753         return (1);
 1754 }
 1755 
 1756 void
 1757 in6_prefixlen2mask(struct in6_addr *maskp, int len)
 1758 {
 1759         u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
 1760         int bytelen, bitlen, i;
 1761 
 1762         /* sanity check */
 1763         if (0 > len || len > 128) {
 1764                 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
 1765                     len);
 1766                 return;
 1767         }
 1768 
 1769         bzero(maskp, sizeof(*maskp));
 1770         bytelen = len / 8;
 1771         bitlen = len % 8;
 1772         for (i = 0; i < bytelen; i++)
 1773                 maskp->s6_addr[i] = 0xff;
 1774         if (bitlen)
 1775                 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
 1776 }
 1777 
 1778 /*
 1779  * return the best address out of the same scope. if no address was
 1780  * found, return the first valid address from designated IF.
 1781  */
 1782 struct in6_ifaddr *
 1783 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
 1784 {
 1785         int dst_scope = in6_addrscope(dst), blen = -1, tlen;
 1786         struct ifaddr *ifa;
 1787         struct in6_ifaddr *besta = NULL;
 1788         struct in6_ifaddr *dep[2];      /* last-resort: deprecated */
 1789 
 1790         dep[0] = dep[1] = NULL;
 1791 
 1792         /*
 1793          * We first look for addresses in the same scope.
 1794          * If there is one, return it.
 1795          * If two or more, return one which matches the dst longest.
 1796          * If none, return one of global addresses assigned other ifs.
 1797          */
 1798         IF_ADDR_RLOCK(ifp);
 1799         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1800                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1801                         continue;
 1802                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
 1803                         continue; /* XXX: is there any case to allow anycast? */
 1804                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
 1805                         continue; /* don't use this interface */
 1806                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
 1807                         continue;
 1808                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
 1809                         if (V_ip6_use_deprecated)
 1810                                 dep[0] = (struct in6_ifaddr *)ifa;
 1811                         continue;
 1812                 }
 1813 
 1814                 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
 1815                         /*
 1816                          * call in6_matchlen() as few as possible
 1817                          */
 1818                         if (besta) {
 1819                                 if (blen == -1)
 1820                                         blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
 1821                                 tlen = in6_matchlen(IFA_IN6(ifa), dst);
 1822                                 if (tlen > blen) {
 1823                                         blen = tlen;
 1824                                         besta = (struct in6_ifaddr *)ifa;
 1825                                 }
 1826                         } else
 1827                                 besta = (struct in6_ifaddr *)ifa;
 1828                 }
 1829         }
 1830         if (besta) {
 1831                 ifa_ref(&besta->ia_ifa);
 1832                 IF_ADDR_RUNLOCK(ifp);
 1833                 return (besta);
 1834         }
 1835 
 1836         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1837                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1838                         continue;
 1839                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
 1840                         continue; /* XXX: is there any case to allow anycast? */
 1841                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
 1842                         continue; /* don't use this interface */
 1843                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
 1844                         continue;
 1845                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
 1846                         if (V_ip6_use_deprecated)
 1847                                 dep[1] = (struct in6_ifaddr *)ifa;
 1848                         continue;
 1849                 }
 1850 
 1851                 if (ifa != NULL)
 1852                         ifa_ref(ifa);
 1853                 IF_ADDR_RUNLOCK(ifp);
 1854                 return (struct in6_ifaddr *)ifa;
 1855         }
 1856 
 1857         /* use the last-resort values, that are, deprecated addresses */
 1858         if (dep[0]) {
 1859                 ifa_ref((struct ifaddr *)dep[0]);
 1860                 IF_ADDR_RUNLOCK(ifp);
 1861                 return dep[0];
 1862         }
 1863         if (dep[1]) {
 1864                 ifa_ref((struct ifaddr *)dep[1]);
 1865                 IF_ADDR_RUNLOCK(ifp);
 1866                 return dep[1];
 1867         }
 1868 
 1869         IF_ADDR_RUNLOCK(ifp);
 1870         return NULL;
 1871 }
 1872 
 1873 /*
 1874  * perform DAD when interface becomes IFF_UP.
 1875  */
 1876 void
 1877 in6_if_up(struct ifnet *ifp)
 1878 {
 1879         struct ifaddr *ifa;
 1880         struct in6_ifaddr *ia;
 1881 
 1882         IF_ADDR_RLOCK(ifp);
 1883         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1884                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1885                         continue;
 1886                 ia = (struct in6_ifaddr *)ifa;
 1887                 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
 1888                         /*
 1889                          * The TENTATIVE flag was likely set by hand
 1890                          * beforehand, implicitly indicating the need for DAD.
 1891                          * We may be able to skip the random delay in this
 1892                          * case, but we impose delays just in case.
 1893                          */
 1894                         nd6_dad_start(ifa,
 1895                             arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
 1896                 }
 1897         }
 1898         IF_ADDR_RUNLOCK(ifp);
 1899 
 1900         /*
 1901          * special cases, like 6to4, are handled in in6_ifattach
 1902          */
 1903         in6_ifattach(ifp, NULL);
 1904 }
 1905 
 1906 int
 1907 in6if_do_dad(struct ifnet *ifp)
 1908 {
 1909         if ((ifp->if_flags & IFF_LOOPBACK) != 0)
 1910                 return (0);
 1911 
 1912         if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) ||
 1913             (ND_IFINFO(ifp)->flags & ND6_IFF_NO_DAD))
 1914                 return (0);
 1915 
 1916         /*
 1917          * Our DAD routine requires the interface up and running.
 1918          * However, some interfaces can be up before the RUNNING
 1919          * status.  Additionally, users may try to assign addresses
 1920          * before the interface becomes up (or running).
 1921          * This function returns EAGAIN in that case.
 1922          * The caller should mark "tentative" on the address instead of
 1923          * performing DAD immediately.
 1924          */
 1925         if (!((ifp->if_flags & IFF_UP) &&
 1926             (ifp->if_drv_flags & IFF_DRV_RUNNING)))
 1927                 return (EAGAIN);
 1928 
 1929         return (1);
 1930 }
 1931 
 1932 /*
 1933  * Calculate max IPv6 MTU through all the interfaces and store it
 1934  * to in6_maxmtu.
 1935  */
 1936 void
 1937 in6_setmaxmtu(void)
 1938 {
 1939         unsigned long maxmtu = 0;
 1940         struct ifnet *ifp;
 1941 
 1942         IFNET_RLOCK_NOSLEEP();
 1943         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
 1944                 /* this function can be called during ifnet initialization */
 1945                 if (!ifp->if_afdata[AF_INET6])
 1946                         continue;
 1947                 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
 1948                     IN6_LINKMTU(ifp) > maxmtu)
 1949                         maxmtu = IN6_LINKMTU(ifp);
 1950         }
 1951         IFNET_RUNLOCK_NOSLEEP();
 1952         if (maxmtu)     /* update only when maxmtu is positive */
 1953                 V_in6_maxmtu = maxmtu;
 1954 }
 1955 
 1956 /*
 1957  * Provide the length of interface identifiers to be used for the link attached
 1958  * to the given interface.  The length should be defined in "IPv6 over
 1959  * xxx-link" document.  Note that address architecture might also define
 1960  * the length for a particular set of address prefixes, regardless of the
 1961  * link type.  As clarified in rfc2462bis, those two definitions should be
 1962  * consistent, and those really are as of August 2004.
 1963  */
 1964 int
 1965 in6_if2idlen(struct ifnet *ifp)
 1966 {
 1967         switch (ifp->if_type) {
 1968         case IFT_ETHER:         /* RFC2464 */
 1969         case IFT_PROPVIRTUAL:   /* XXX: no RFC. treat it as ether */
 1970         case IFT_L2VLAN:        /* ditto */
 1971         case IFT_IEEE80211:     /* ditto */
 1972         case IFT_BRIDGE:        /* bridge(4) only does Ethernet-like links */
 1973         case IFT_INFINIBAND:
 1974                 return (64);
 1975         case IFT_FDDI:          /* RFC2467 */
 1976                 return (64);
 1977         case IFT_ISO88025:      /* RFC2470 (IPv6 over Token Ring) */
 1978                 return (64);
 1979         case IFT_PPP:           /* RFC2472 */
 1980                 return (64);
 1981         case IFT_ARCNET:        /* RFC2497 */
 1982                 return (64);
 1983         case IFT_FRELAY:        /* RFC2590 */
 1984                 return (64);
 1985         case IFT_IEEE1394:      /* RFC3146 */
 1986                 return (64);
 1987         case IFT_GIF:
 1988                 return (64);    /* draft-ietf-v6ops-mech-v2-07 */
 1989         case IFT_LOOP:
 1990                 return (64);    /* XXX: is this really correct? */
 1991         default:
 1992                 /*
 1993                  * Unknown link type:
 1994                  * It might be controversial to use the today's common constant
 1995                  * of 64 for these cases unconditionally.  For full compliance,
 1996                  * we should return an error in this case.  On the other hand,
 1997                  * if we simply miss the standard for the link type or a new
 1998                  * standard is defined for a new link type, the IFID length
 1999                  * is very likely to be the common constant.  As a compromise,
 2000                  * we always use the constant, but make an explicit notice
 2001                  * indicating the "unknown" case.
 2002                  */
 2003                 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
 2004                 return (64);
 2005         }
 2006 }
 2007 
 2008 #include <sys/sysctl.h>
 2009 
 2010 struct in6_llentry {
 2011         struct llentry          base;
 2012 };
 2013 
 2014 #define IN6_LLTBL_DEFAULT_HSIZE 32
 2015 #define IN6_LLTBL_HASH(k, h) \
 2016         (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
 2017 
 2018 /*
 2019  * Do actual deallocation of @lle.
 2020  */
 2021 static void
 2022 in6_lltable_destroy_lle_unlocked(struct llentry *lle)
 2023 {
 2024 
 2025         LLE_LOCK_DESTROY(lle);
 2026         LLE_REQ_DESTROY(lle);
 2027         free(lle, M_LLTABLE);
 2028 }
 2029 
 2030 /*
 2031  * Called by LLE_FREE_LOCKED when number of references
 2032  * drops to zero.
 2033  */
 2034 static void
 2035 in6_lltable_destroy_lle(struct llentry *lle)
 2036 {
 2037 
 2038         LLE_WUNLOCK(lle);
 2039         in6_lltable_destroy_lle_unlocked(lle);
 2040 }
 2041 
 2042 static struct llentry *
 2043 in6_lltable_new(const struct in6_addr *addr6, u_int flags)
 2044 {
 2045         struct in6_llentry *lle;
 2046 
 2047         lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
 2048         if (lle == NULL)                /* NB: caller generates msg */
 2049                 return NULL;
 2050 
 2051         lle->base.r_l3addr.addr6 = *addr6;
 2052         lle->base.lle_refcnt = 1;
 2053         lle->base.lle_free = in6_lltable_destroy_lle;
 2054         LLE_LOCK_INIT(&lle->base);
 2055         LLE_REQ_INIT(&lle->base);
 2056         callout_init(&lle->base.lle_timer, 1);
 2057 
 2058         return (&lle->base);
 2059 }
 2060 
 2061 static int
 2062 in6_lltable_match_prefix(const struct sockaddr *saddr,
 2063     const struct sockaddr *smask, u_int flags, struct llentry *lle)
 2064 {
 2065         const struct in6_addr *addr, *mask, *lle_addr;
 2066 
 2067         addr = &((const struct sockaddr_in6 *)saddr)->sin6_addr;
 2068         mask = &((const struct sockaddr_in6 *)smask)->sin6_addr;
 2069         lle_addr = &lle->r_l3addr.addr6;
 2070 
 2071         if (IN6_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
 2072                 return (0);
 2073 
 2074         if (lle->la_flags & LLE_IFADDR) {
 2075 
 2076                 /*
 2077                  * Delete LLE_IFADDR records IFF address & flag matches.
 2078                  * Note that addr is the interface address within prefix
 2079                  * being matched.
 2080                  */
 2081                 if (IN6_ARE_ADDR_EQUAL(addr, lle_addr) &&
 2082                     (flags & LLE_STATIC) != 0)
 2083                         return (1);
 2084                 return (0);
 2085         }
 2086 
 2087         /* flags & LLE_STATIC means deleting both dynamic and static entries */
 2088         if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
 2089                 return (1);
 2090 
 2091         return (0);
 2092 }
 2093 
 2094 static void
 2095 in6_lltable_free_entry(struct lltable *llt, struct llentry *lle)
 2096 {
 2097         struct ifnet *ifp;
 2098 
 2099         LLE_WLOCK_ASSERT(lle);
 2100         KASSERT(llt != NULL, ("lltable is NULL"));
 2101 
 2102         /* Unlink entry from table */
 2103         if ((lle->la_flags & LLE_LINKED) != 0) {
 2104 
 2105                 ifp = llt->llt_ifp;
 2106                 IF_AFDATA_WLOCK_ASSERT(ifp);
 2107                 lltable_unlink_entry(llt, lle);
 2108         }
 2109 
 2110         llentry_free(lle);
 2111 }
 2112 
 2113 static int
 2114 in6_lltable_rtcheck(struct ifnet *ifp,
 2115                     u_int flags,
 2116                     const struct sockaddr *l3addr)
 2117 {
 2118         const struct sockaddr_in6 *sin6;
 2119         struct nhop6_basic nh6;
 2120         struct in6_addr dst;
 2121         uint32_t scopeid;
 2122         int error;
 2123         char ip6buf[INET6_ADDRSTRLEN];
 2124         int fibnum;
 2125 
 2126         KASSERT(l3addr->sa_family == AF_INET6,
 2127             ("sin_family %d", l3addr->sa_family));
 2128 
 2129         sin6 = (const struct sockaddr_in6 *)l3addr;
 2130         in6_splitscope(&sin6->sin6_addr, &dst, &scopeid);
 2131         fibnum = V_rt_add_addr_allfibs ? RT_DEFAULT_FIB : ifp->if_fib;
 2132         error = fib6_lookup_nh_basic(fibnum, &dst, scopeid, 0, 0, &nh6);
 2133         if (error != 0 || (nh6.nh_flags & NHF_GATEWAY) || nh6.nh_ifp != ifp) {
 2134                 struct ifaddr *ifa;
 2135                 /*
 2136                  * Create an ND6 cache for an IPv6 neighbor
 2137                  * that is not covered by our own prefix.
 2138                  */
 2139                 ifa = ifaof_ifpforaddr(l3addr, ifp);
 2140                 if (ifa != NULL) {
 2141                         ifa_free(ifa);
 2142                         return 0;
 2143                 }
 2144                 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
 2145                     ip6_sprintf(ip6buf, &sin6->sin6_addr));
 2146                 return EINVAL;
 2147         }
 2148         return 0;
 2149 }
 2150 
 2151 /*
 2152  * Called by the datapath to indicate that the entry was used.
 2153  */
 2154 static void
 2155 in6_lltable_mark_used(struct llentry *lle)
 2156 {
 2157 
 2158         LLE_REQ_LOCK(lle);
 2159         lle->r_skip_req = 0;
 2160 
 2161         /*
 2162          * Set the hit time so the callback function
 2163          * can determine the remaining time before
 2164          * transiting to the DELAY state.
 2165          */
 2166         lle->lle_hittime = time_uptime;
 2167         LLE_REQ_UNLOCK(lle);
 2168 }
 2169 
 2170 static inline uint32_t
 2171 in6_lltable_hash_dst(const struct in6_addr *dst, uint32_t hsize)
 2172 {
 2173 
 2174         return (IN6_LLTBL_HASH(dst->s6_addr32[3], hsize));
 2175 }
 2176 
 2177 static uint32_t
 2178 in6_lltable_hash(const struct llentry *lle, uint32_t hsize)
 2179 {
 2180 
 2181         return (in6_lltable_hash_dst(&lle->r_l3addr.addr6, hsize));
 2182 }
 2183 
 2184 static void
 2185 in6_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
 2186 {
 2187         struct sockaddr_in6 *sin6;
 2188 
 2189         sin6 = (struct sockaddr_in6 *)sa;
 2190         bzero(sin6, sizeof(*sin6));
 2191         sin6->sin6_family = AF_INET6;
 2192         sin6->sin6_len = sizeof(*sin6);
 2193         sin6->sin6_addr = lle->r_l3addr.addr6;
 2194 }
 2195 
 2196 static inline struct llentry *
 2197 in6_lltable_find_dst(struct lltable *llt, const struct in6_addr *dst)
 2198 {
 2199         struct llentry *lle;
 2200         struct llentries *lleh;
 2201         u_int hashidx;
 2202 
 2203         hashidx = in6_lltable_hash_dst(dst, llt->llt_hsize);
 2204         lleh = &llt->lle_head[hashidx];
 2205         LIST_FOREACH(lle, lleh, lle_next) {
 2206                 if (lle->la_flags & LLE_DELETED)
 2207                         continue;
 2208                 if (IN6_ARE_ADDR_EQUAL(&lle->r_l3addr.addr6, dst))
 2209                         break;
 2210         }
 2211 
 2212         return (lle);
 2213 }
 2214 
 2215 static void
 2216 in6_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
 2217 {
 2218 
 2219         lle->la_flags |= LLE_DELETED;
 2220         EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
 2221 #ifdef DIAGNOSTIC
 2222         log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
 2223 #endif
 2224         llentry_free(lle);
 2225 }
 2226 
 2227 static struct llentry *
 2228 in6_lltable_alloc(struct lltable *llt, u_int flags,
 2229         const struct sockaddr *l3addr)
 2230 {
 2231         const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
 2232         struct ifnet *ifp = llt->llt_ifp;
 2233         struct llentry *lle;
 2234         char linkhdr[LLE_MAX_LINKHDR];
 2235         size_t linkhdrsize;
 2236         int lladdr_off;
 2237 
 2238         KASSERT(l3addr->sa_family == AF_INET6,
 2239             ("sin_family %d", l3addr->sa_family));
 2240 
 2241         /*
 2242          * A route that covers the given address must have
 2243          * been installed 1st because we are doing a resolution,
 2244          * verify this.
 2245          */
 2246         if (!(flags & LLE_IFADDR) &&
 2247             in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
 2248                 return (NULL);
 2249 
 2250         lle = in6_lltable_new(&sin6->sin6_addr, flags);
 2251         if (lle == NULL) {
 2252                 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
 2253                 return (NULL);
 2254         }
 2255         lle->la_flags = flags;
 2256         if ((flags & LLE_IFADDR) == LLE_IFADDR) {
 2257                 linkhdrsize = LLE_MAX_LINKHDR;
 2258                 if (lltable_calc_llheader(ifp, AF_INET6, IF_LLADDR(ifp),
 2259                     linkhdr, &linkhdrsize, &lladdr_off) != 0) {
 2260                         in6_lltable_destroy_lle_unlocked(lle);
 2261                         return (NULL);
 2262                 }
 2263                 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
 2264                     lladdr_off);
 2265                 lle->la_flags |= LLE_STATIC;
 2266         }
 2267 
 2268         if ((lle->la_flags & LLE_STATIC) != 0)
 2269                 lle->ln_state = ND6_LLINFO_REACHABLE;
 2270 
 2271         return (lle);
 2272 }
 2273 
 2274 static struct llentry *
 2275 in6_lltable_lookup(struct lltable *llt, u_int flags,
 2276         const struct sockaddr *l3addr)
 2277 {
 2278         const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
 2279         struct llentry *lle;
 2280 
 2281         IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
 2282         KASSERT(l3addr->sa_family == AF_INET6,
 2283             ("sin_family %d", l3addr->sa_family));
 2284 
 2285         lle = in6_lltable_find_dst(llt, &sin6->sin6_addr);
 2286 
 2287         if (lle == NULL)
 2288                 return (NULL);
 2289 
 2290         KASSERT((flags & (LLE_UNLOCKED|LLE_EXCLUSIVE)) !=
 2291             (LLE_UNLOCKED|LLE_EXCLUSIVE),("wrong lle request flags: 0x%X",
 2292             flags));
 2293 
 2294         if (flags & LLE_UNLOCKED)
 2295                 return (lle);
 2296 
 2297         if (flags & LLE_EXCLUSIVE)
 2298                 LLE_WLOCK(lle);
 2299         else
 2300                 LLE_RLOCK(lle);
 2301         return (lle);
 2302 }
 2303 
 2304 static int
 2305 in6_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
 2306     struct sysctl_req *wr)
 2307 {
 2308         struct ifnet *ifp = llt->llt_ifp;
 2309         /* XXX stack use */
 2310         struct {
 2311                 struct rt_msghdr        rtm;
 2312                 struct sockaddr_in6     sin6;
 2313                 /*
 2314                  * ndp.c assumes that sdl is word aligned
 2315                  */
 2316 #ifdef __LP64__
 2317                 uint32_t                pad;
 2318 #endif
 2319                 struct sockaddr_dl      sdl;
 2320         } ndpc;
 2321         struct sockaddr_dl *sdl;
 2322         int error;
 2323 
 2324         bzero(&ndpc, sizeof(ndpc));
 2325                         /* skip deleted entries */
 2326                         if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
 2327                                 return (0);
 2328                         /* Skip if jailed and not a valid IP of the prison. */
 2329                         lltable_fill_sa_entry(lle,
 2330                             (struct sockaddr *)&ndpc.sin6);
 2331                         if (prison_if(wr->td->td_ucred,
 2332                             (struct sockaddr *)&ndpc.sin6) != 0)
 2333                                 return (0);
 2334                         /*
 2335                          * produce a msg made of:
 2336                          *  struct rt_msghdr;
 2337                          *  struct sockaddr_in6 (IPv6)
 2338                          *  struct sockaddr_dl;
 2339                          */
 2340                         ndpc.rtm.rtm_msglen = sizeof(ndpc);
 2341                         ndpc.rtm.rtm_version = RTM_VERSION;
 2342                         ndpc.rtm.rtm_type = RTM_GET;
 2343                         ndpc.rtm.rtm_flags = RTF_UP;
 2344                         ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
 2345                         if (V_deembed_scopeid)
 2346                                 sa6_recoverscope(&ndpc.sin6);
 2347 
 2348                         /* publish */
 2349                         if (lle->la_flags & LLE_PUB)
 2350                                 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
 2351 
 2352                         sdl = &ndpc.sdl;
 2353                         sdl->sdl_family = AF_LINK;
 2354                         sdl->sdl_len = sizeof(*sdl);
 2355                         sdl->sdl_index = ifp->if_index;
 2356                         sdl->sdl_type = ifp->if_type;
 2357                         if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
 2358                                 sdl->sdl_alen = ifp->if_addrlen;
 2359                                 bcopy(lle->ll_addr, LLADDR(sdl),
 2360                                     ifp->if_addrlen);
 2361                         } else {
 2362                                 sdl->sdl_alen = 0;
 2363                                 bzero(LLADDR(sdl), ifp->if_addrlen);
 2364                         }
 2365                         if (lle->la_expire != 0)
 2366                                 ndpc.rtm.rtm_rmx.rmx_expire = lle->la_expire +
 2367                                     lle->lle_remtime / hz +
 2368                                     time_second - time_uptime;
 2369                         ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
 2370                         if (lle->la_flags & LLE_STATIC)
 2371                                 ndpc.rtm.rtm_flags |= RTF_STATIC;
 2372                         if (lle->la_flags & LLE_IFADDR)
 2373                                 ndpc.rtm.rtm_flags |= RTF_PINNED;
 2374                         if (lle->ln_router != 0)
 2375                                 ndpc.rtm.rtm_flags |= RTF_GATEWAY;
 2376                         ndpc.rtm.rtm_rmx.rmx_pksent = lle->la_asked;
 2377                         /* Store state in rmx_weight value */
 2378                         ndpc.rtm.rtm_rmx.rmx_state = lle->ln_state;
 2379                         ndpc.rtm.rtm_index = ifp->if_index;
 2380                         error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
 2381 
 2382         return (error);
 2383 }
 2384 
 2385 static struct lltable *
 2386 in6_lltattach(struct ifnet *ifp)
 2387 {
 2388         struct lltable *llt;
 2389 
 2390         llt = lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE);
 2391         llt->llt_af = AF_INET6;
 2392         llt->llt_ifp = ifp;
 2393 
 2394         llt->llt_lookup = in6_lltable_lookup;
 2395         llt->llt_alloc_entry = in6_lltable_alloc;
 2396         llt->llt_delete_entry = in6_lltable_delete_entry;
 2397         llt->llt_dump_entry = in6_lltable_dump_entry;
 2398         llt->llt_hash = in6_lltable_hash;
 2399         llt->llt_fill_sa_entry = in6_lltable_fill_sa_entry;
 2400         llt->llt_free_entry = in6_lltable_free_entry;
 2401         llt->llt_match_prefix = in6_lltable_match_prefix;
 2402         llt->llt_mark_used = in6_lltable_mark_used;
 2403         lltable_link(llt);
 2404 
 2405         return (llt);
 2406 }
 2407 
 2408 void *
 2409 in6_domifattach(struct ifnet *ifp)
 2410 {
 2411         struct in6_ifextra *ext;
 2412 
 2413         /* There are not IPv6-capable interfaces. */
 2414         switch (ifp->if_type) {
 2415         case IFT_PFLOG:
 2416         case IFT_PFSYNC:
 2417         case IFT_USB:
 2418                 return (NULL);
 2419         }
 2420         ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
 2421         bzero(ext, sizeof(*ext));
 2422 
 2423         ext->in6_ifstat = malloc(sizeof(counter_u64_t) *
 2424             sizeof(struct in6_ifstat) / sizeof(uint64_t), M_IFADDR, M_WAITOK);
 2425         COUNTER_ARRAY_ALLOC(ext->in6_ifstat,
 2426             sizeof(struct in6_ifstat) / sizeof(uint64_t), M_WAITOK);
 2427 
 2428         ext->icmp6_ifstat = malloc(sizeof(counter_u64_t) *
 2429             sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_IFADDR,
 2430             M_WAITOK);
 2431         COUNTER_ARRAY_ALLOC(ext->icmp6_ifstat,
 2432             sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_WAITOK);
 2433 
 2434         ext->nd_ifinfo = nd6_ifattach(ifp);
 2435         ext->scope6_id = scope6_ifattach(ifp);
 2436         ext->lltable = in6_lltattach(ifp);
 2437 
 2438         ext->mld_ifinfo = mld_domifattach(ifp);
 2439 
 2440         return ext;
 2441 }
 2442 
 2443 int
 2444 in6_domifmtu(struct ifnet *ifp)
 2445 {
 2446         if (ifp->if_afdata[AF_INET6] == NULL)
 2447                 return ifp->if_mtu;
 2448 
 2449         return (IN6_LINKMTU(ifp));
 2450 }
 2451 
 2452 void
 2453 in6_domifdetach(struct ifnet *ifp, void *aux)
 2454 {
 2455         struct in6_ifextra *ext = (struct in6_ifextra *)aux;
 2456 
 2457         mld_domifdetach(ifp);
 2458         scope6_ifdetach(ext->scope6_id);
 2459         nd6_ifdetach(ifp, ext->nd_ifinfo);
 2460         lltable_free(ext->lltable);
 2461         COUNTER_ARRAY_FREE(ext->in6_ifstat,
 2462             sizeof(struct in6_ifstat) / sizeof(uint64_t));
 2463         free(ext->in6_ifstat, M_IFADDR);
 2464         COUNTER_ARRAY_FREE(ext->icmp6_ifstat,
 2465             sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
 2466         free(ext->icmp6_ifstat, M_IFADDR);
 2467         free(ext, M_IFADDR);
 2468 }
 2469 
 2470 /*
 2471  * Convert sockaddr_in6 to sockaddr_in.  Original sockaddr_in6 must be
 2472  * v4 mapped addr or v4 compat addr
 2473  */
 2474 void
 2475 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
 2476 {
 2477 
 2478         bzero(sin, sizeof(*sin));
 2479         sin->sin_len = sizeof(struct sockaddr_in);
 2480         sin->sin_family = AF_INET;
 2481         sin->sin_port = sin6->sin6_port;
 2482         sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
 2483 }
 2484 
 2485 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
 2486 void
 2487 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
 2488 {
 2489         bzero(sin6, sizeof(*sin6));
 2490         sin6->sin6_len = sizeof(struct sockaddr_in6);
 2491         sin6->sin6_family = AF_INET6;
 2492         sin6->sin6_port = sin->sin_port;
 2493         sin6->sin6_addr.s6_addr32[0] = 0;
 2494         sin6->sin6_addr.s6_addr32[1] = 0;
 2495         sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
 2496         sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
 2497 }
 2498 
 2499 /* Convert sockaddr_in6 into sockaddr_in. */
 2500 void
 2501 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
 2502 {
 2503         struct sockaddr_in *sin_p;
 2504         struct sockaddr_in6 sin6;
 2505 
 2506         /*
 2507          * Save original sockaddr_in6 addr and convert it
 2508          * to sockaddr_in.
 2509          */
 2510         sin6 = *(struct sockaddr_in6 *)nam;
 2511         sin_p = (struct sockaddr_in *)nam;
 2512         in6_sin6_2_sin(sin_p, &sin6);
 2513 }
 2514 
 2515 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
 2516 void
 2517 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
 2518 {
 2519         struct sockaddr_in *sin_p;
 2520         struct sockaddr_in6 *sin6_p;
 2521 
 2522         sin6_p = malloc(sizeof *sin6_p, M_SONAME, M_WAITOK);
 2523         sin_p = (struct sockaddr_in *)*nam;
 2524         in6_sin_2_v4mapsin6(sin_p, sin6_p);
 2525         free(*nam, M_SONAME);
 2526         *nam = (struct sockaddr *)sin6_p;
 2527 }

Cache object: b682fa84d77d64a1e09144da57b598f2


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