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

Cache object: 8e3c516993a0a1757a72092192fe44a9


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