The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/in6.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: b634ce3c5e040185bf4fcc598820da0d


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