[ 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  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  cheribsd  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1  -  FREEBSD-LIBC  -  FREEBSD8-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  3  -  10 

    1 /*      $FreeBSD: src/sys/netinet6/in6.c,v 1.51.2.11 2007/06/10 17:17:55 jinmei Exp $   */
    2 /*      $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $  */
    3 
    4 /*-
    5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of the project nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 /*-
   34  * Copyright (c) 1982, 1986, 1991, 1993
   35  *      The Regents of the University of California.  All rights reserved.
   36  *
   37  * Redistribution and use in source and binary forms, with or without
   38  * modification, are permitted provided that the following conditions
   39  * are met:
   40  * 1. Redistributions of source code must retain the above copyright
   41  *    notice, this list of conditions and the following disclaimer.
   42  * 2. Redistributions in binary form must reproduce the above copyright
   43  *    notice, this list of conditions and the following disclaimer in the
   44  *    documentation and/or other materials provided with the distribution.
   45  * 4. Neither the name of the University nor the names of its contributors
   46  *    may be used to endorse or promote products derived from this software
   47  *    without specific prior written permission.
   48  *
   49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   59  * SUCH DAMAGE.
   60  *
   61  *      @(#)in.c        8.2 (Berkeley) 11/15/93
   62  */
   63 
   64 #include "opt_inet.h"
   65 #include "opt_inet6.h"
   66 
   67 #include <sys/param.h>
   68 #include <sys/errno.h>
   69 #include <sys/malloc.h>
   70 #include <sys/socket.h>
   71 #include <sys/socketvar.h>
   72 #include <sys/sockio.h>
   73 #include <sys/systm.h>
   74 #include <sys/proc.h>
   75 #include <sys/time.h>
   76 #include <sys/kernel.h>
   77 #include <sys/syslog.h>
   78 
   79 #include <net/if.h>
   80 #include <net/if_types.h>
   81 #include <net/route.h>
   82 #include <net/if_dl.h>
   83 
   84 #include <netinet/in.h>
   85 #include <netinet/in_var.h>
   86 #include <netinet/if_ether.h>
   87 #include <netinet/in_systm.h>
   88 #include <netinet/ip.h>
   89 #include <netinet/in_pcb.h>
   90 
   91 #include <netinet/ip6.h>
   92 #include <netinet6/ip6_var.h>
   93 #include <netinet6/nd6.h>
   94 #include <netinet6/mld6_var.h>
   95 #include <netinet6/ip6_mroute.h>
   96 #include <netinet6/in6_ifattach.h>
   97 #include <netinet6/scope6_var.h>
   98 #include <netinet6/in6_pcb.h>
   99 
  100 #include <net/net_osdep.h>
  101 
  102 MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "internet multicast address");
  103 
  104 /*
  105  * Definitions of some costant IP6 addresses.
  106  */
  107 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
  108 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
  109 const struct in6_addr in6addr_nodelocal_allnodes =
  110         IN6ADDR_NODELOCAL_ALLNODES_INIT;
  111 const struct in6_addr in6addr_linklocal_allnodes =
  112         IN6ADDR_LINKLOCAL_ALLNODES_INIT;
  113 const struct in6_addr in6addr_linklocal_allrouters =
  114         IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
  115 
  116 const struct in6_addr in6mask0 = IN6MASK0;
  117 const struct in6_addr in6mask32 = IN6MASK32;
  118 const struct in6_addr in6mask64 = IN6MASK64;
  119 const struct in6_addr in6mask96 = IN6MASK96;
  120 const struct in6_addr in6mask128 = IN6MASK128;
  121 
  122 const struct sockaddr_in6 sa6_any =
  123         { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
  124 
  125 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
  126         struct ifnet *, struct thread *));
  127 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
  128         struct sockaddr_in6 *, int));
  129 static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
  130 
  131 struct in6_multihead in6_multihead;     /* XXX BSS initialization */
  132 int     (*faithprefix_p)(struct in6_addr *);
  133 
  134 /*
  135  * Subroutine for in6_ifaddloop() and in6_ifremloop().
  136  * This routine does actual work.
  137  */
  138 static void
  139 in6_ifloop_request(int cmd, struct ifaddr *ifa)
  140 {
  141         struct sockaddr_in6 all1_sa;
  142         struct rtentry *nrt = NULL;
  143         int e;
  144 
  145         bzero(&all1_sa, sizeof(all1_sa));
  146         all1_sa.sin6_family = AF_INET6;
  147         all1_sa.sin6_len = sizeof(struct sockaddr_in6);
  148         all1_sa.sin6_addr = in6mask128;
  149 
  150         /*
  151          * We specify the address itself as the gateway, and set the
  152          * RTF_LLINFO flag, so that the corresponding host route would have
  153          * the flag, and thus applications that assume traditional behavior
  154          * would be happy.  Note that we assume the caller of the function
  155          * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
  156          * which changes the outgoing interface to the loopback interface.
  157          */
  158         e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
  159             (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
  160         if (e != 0) {
  161                 /* XXX need more descriptive message */
  162                 log(LOG_ERR, "in6_ifloop_request: "
  163                     "%s operation failed for %s (errno=%d)\n",
  164                     cmd == RTM_ADD ? "ADD" : "DELETE",
  165                     ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
  166                     e);
  167         }
  168 
  169         /*
  170          * Report the addition/removal of the address to the routing socket.
  171          * XXX: since we called rtinit for a p2p interface with a destination,
  172          *      we end up reporting twice in such a case.  Should we rather
  173          *      omit the second report?
  174          */
  175         if (nrt) {
  176                 RT_LOCK(nrt);
  177                 /*
  178                  * Make sure rt_ifa be equal to IFA, the second argument of
  179                  * the function.  We need this because when we refer to
  180                  * rt_ifa->ia6_flags in ip6_input, we assume that the rt_ifa
  181                  * points to the address instead of the loopback address.
  182                  */
  183                 if (cmd == RTM_ADD && ifa != nrt->rt_ifa) {
  184                         IFAFREE(nrt->rt_ifa);
  185                         IFAREF(ifa);
  186                         nrt->rt_ifa = ifa;
  187                 }
  188 
  189                 rt_newaddrmsg(cmd, ifa, e, nrt);
  190                 if (cmd == RTM_DELETE) {
  191                         rtfree(nrt);
  192                 } else {
  193                         /* the cmd must be RTM_ADD here */
  194                         RT_REMREF(nrt);
  195                         RT_UNLOCK(nrt);
  196                 }
  197         }
  198 }
  199 
  200 /*
  201  * Add ownaddr as loopback rtentry.  We previously add the route only if
  202  * necessary (ex. on a p2p link).  However, since we now manage addresses
  203  * separately from prefixes, we should always add the route.  We can't
  204  * rely on the cloning mechanism from the corresponding interface route
  205  * any more.
  206  */
  207 void
  208 in6_ifaddloop(struct ifaddr *ifa)
  209 {
  210         struct rtentry *rt;
  211         int need_loop;
  212 
  213         /* If there is no loopback entry, allocate one. */
  214         rt = rtalloc1(ifa->ifa_addr, 0, 0);
  215         need_loop = (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
  216             (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0);
  217         if (rt)
  218                 rtfree(rt);
  219         if (need_loop)
  220                 in6_ifloop_request(RTM_ADD, ifa);
  221 }
  222 
  223 /*
  224  * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
  225  * if it exists.
  226  */
  227 void
  228 in6_ifremloop(struct ifaddr *ifa)
  229 {
  230         struct in6_ifaddr *ia;
  231         struct rtentry *rt;
  232         int ia_count = 0;
  233 
  234         /*
  235          * Some of BSD variants do not remove cloned routes
  236          * from an interface direct route, when removing the direct route
  237          * (see comments in net/net_osdep.h).  Even for variants that do remove
  238          * cloned routes, they could fail to remove the cloned routes when
  239          * we handle multple addresses that share a common prefix.
  240          * So, we should remove the route corresponding to the deleted address.
  241          */
  242 
  243         /*
  244          * Delete the entry only if exact one ifa exists.  More than one ifa
  245          * can exist if we assign a same single address to multiple
  246          * (probably p2p) interfaces.
  247          * XXX: we should avoid such a configuration in IPv6...
  248          */
  249         for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
  250                 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
  251                         ia_count++;
  252                         if (ia_count > 1)
  253                                 break;
  254                 }
  255         }
  256 
  257         if (ia_count == 1) {
  258                 /*
  259                  * Before deleting, check if a corresponding loopbacked host
  260                  * route surely exists.  With this check, we can avoid to
  261                  * delete an interface direct route whose destination is same
  262                  * as the address being removed.  This can happen when removing
  263                  * a subnet-router anycast address on an interface attahced
  264                  * to a shared medium.
  265                  */
  266                 rt = rtalloc1(ifa->ifa_addr, 0, 0);
  267                 if (rt != NULL) {
  268                         if ((rt->rt_flags & RTF_HOST) != 0 &&
  269                             (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
  270                                 rtfree(rt);
  271                                 in6_ifloop_request(RTM_DELETE, ifa);
  272                         } else
  273                                 RT_UNLOCK(rt);
  274                 }
  275         }
  276 }
  277 
  278 int
  279 in6_mask2len(mask, lim0)
  280         struct in6_addr *mask;
  281         u_char *lim0;
  282 {
  283         int x = 0, y;
  284         u_char *lim = lim0, *p;
  285 
  286         /* ignore the scope_id part */
  287         if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
  288                 lim = (u_char *)mask + sizeof(*mask);
  289         for (p = (u_char *)mask; p < lim; x++, p++) {
  290                 if (*p != 0xff)
  291                         break;
  292         }
  293         y = 0;
  294         if (p < lim) {
  295                 for (y = 0; y < 8; y++) {
  296                         if ((*p & (0x80 >> y)) == 0)
  297                                 break;
  298                 }
  299         }
  300 
  301         /*
  302          * when the limit pointer is given, do a stricter check on the
  303          * remaining bits.
  304          */
  305         if (p < lim) {
  306                 if (y != 0 && (*p & (0x00ff >> y)) != 0)
  307                         return (-1);
  308                 for (p = p + 1; p < lim; p++)
  309                         if (*p != 0)
  310                                 return (-1);
  311         }
  312 
  313         return x * 8 + y;
  314 }
  315 
  316 #define ifa2ia6(ifa)    ((struct in6_ifaddr *)(ifa))
  317 #define ia62ifa(ia6)    (&((ia6)->ia_ifa))
  318 
  319 int
  320 in6_control(so, cmd, data, ifp, td)
  321         struct  socket *so;
  322         u_long cmd;
  323         caddr_t data;
  324         struct ifnet *ifp;
  325         struct thread *td;
  326 {
  327         struct  in6_ifreq *ifr = (struct in6_ifreq *)data;
  328         struct  in6_ifaddr *ia = NULL;
  329         struct  in6_aliasreq *ifra = (struct in6_aliasreq *)data;
  330         int error, privileged;
  331         struct sockaddr_in6 *sa6;
  332 
  333         privileged = 0;
  334         if (td == NULL || !suser(td))
  335                 privileged++;
  336 
  337         switch (cmd) {
  338         case SIOCGETSGCNT_IN6:
  339         case SIOCGETMIFCNT_IN6:
  340                 return (mrt6_ioctl(cmd, data));
  341         }
  342 
  343         switch(cmd) {
  344         case SIOCAADDRCTL_POLICY:
  345         case SIOCDADDRCTL_POLICY:
  346                 if (!privileged)
  347                         return (EPERM);
  348                 return (in6_src_ioctl(cmd, data));
  349         }
  350 
  351         if (ifp == NULL)
  352                 return (EOPNOTSUPP);
  353 
  354         switch (cmd) {
  355         case SIOCSNDFLUSH_IN6:
  356         case SIOCSPFXFLUSH_IN6:
  357         case SIOCSRTRFLUSH_IN6:
  358         case SIOCSDEFIFACE_IN6:
  359         case SIOCSIFINFO_FLAGS:
  360                 if (!privileged)
  361                         return (EPERM);
  362                 /* FALLTHROUGH */
  363         case OSIOCGIFINFO_IN6:
  364         case SIOCGIFINFO_IN6:
  365         case SIOCSIFINFO_IN6:
  366         case SIOCGDRLST_IN6:
  367         case SIOCGPRLST_IN6:
  368         case SIOCGNBRINFO_IN6:
  369         case SIOCGDEFIFACE_IN6:
  370                 return (nd6_ioctl(cmd, data, ifp));
  371         }
  372 
  373         switch (cmd) {
  374         case SIOCSIFPREFIX_IN6:
  375         case SIOCDIFPREFIX_IN6:
  376         case SIOCAIFPREFIX_IN6:
  377         case SIOCCIFPREFIX_IN6:
  378         case SIOCSGIFPREFIX_IN6:
  379         case SIOCGIFPREFIX_IN6:
  380                 log(LOG_NOTICE,
  381                     "prefix ioctls are now invalidated. "
  382                     "please use ifconfig.\n");
  383                 return (EOPNOTSUPP);
  384         }
  385 
  386         switch (cmd) {
  387         case SIOCSSCOPE6:
  388                 if (!privileged)
  389                         return (EPERM);
  390                 return (scope6_set(ifp,
  391                     (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
  392         case SIOCGSCOPE6:
  393                 return (scope6_get(ifp,
  394                     (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
  395         case SIOCGSCOPE6DEF:
  396                 return (scope6_get_default((struct scope6_id *)
  397                     ifr->ifr_ifru.ifru_scope_id));
  398         }
  399 
  400         switch (cmd) {
  401         case SIOCALIFADDR:
  402         case SIOCDLIFADDR:
  403                 if (!privileged)
  404                         return (EPERM);
  405                 /* FALLTHROUGH */
  406         case SIOCGLIFADDR:
  407                 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
  408         }
  409 
  410         /*
  411          * Find address for this interface, if it exists.
  412          *
  413          * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
  414          * only, and used the first interface address as the target of other
  415          * operations (without checking ifra_addr).  This was because netinet
  416          * code/API assumed at most 1 interface address per interface.
  417          * Since IPv6 allows a node to assign multiple addresses
  418          * on a single interface, we almost always look and check the
  419          * presence of ifra_addr, and reject invalid ones here.
  420          * It also decreases duplicated code among SIOC*_IN6 operations.
  421          */
  422         switch (cmd) {
  423         case SIOCAIFADDR_IN6:
  424         case SIOCSIFPHYADDR_IN6:
  425                 sa6 = &ifra->ifra_addr;
  426                 break;
  427         case SIOCSIFADDR_IN6:
  428         case SIOCGIFADDR_IN6:
  429         case SIOCSIFDSTADDR_IN6:
  430         case SIOCSIFNETMASK_IN6:
  431         case SIOCGIFDSTADDR_IN6:
  432         case SIOCGIFNETMASK_IN6:
  433         case SIOCDIFADDR_IN6:
  434         case SIOCGIFPSRCADDR_IN6:
  435         case SIOCGIFPDSTADDR_IN6:
  436         case SIOCGIFAFLAG_IN6:
  437         case SIOCSNDFLUSH_IN6:
  438         case SIOCSPFXFLUSH_IN6:
  439         case SIOCSRTRFLUSH_IN6:
  440         case SIOCGIFALIFETIME_IN6:
  441         case SIOCSIFALIFETIME_IN6:
  442         case SIOCGIFSTAT_IN6:
  443         case SIOCGIFSTAT_ICMP6:
  444                 sa6 = &ifr->ifr_addr;
  445                 break;
  446         default:
  447                 sa6 = NULL;
  448                 break;
  449         }
  450         if (sa6 && sa6->sin6_family == AF_INET6) {
  451                 int error = 0;
  452 
  453                 if (sa6->sin6_scope_id != 0)
  454                         error = sa6_embedscope(sa6, 0);
  455                 else
  456                         error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
  457                 if (error != 0)
  458                         return (error);
  459                 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
  460         } else
  461                 ia = NULL;
  462 
  463         switch (cmd) {
  464         case SIOCSIFADDR_IN6:
  465         case SIOCSIFDSTADDR_IN6:
  466         case SIOCSIFNETMASK_IN6:
  467                 /*
  468                  * Since IPv6 allows a node to assign multiple addresses
  469                  * on a single interface, SIOCSIFxxx ioctls are deprecated.
  470                  */
  471                 /* we decided to obsolete this command (20000704) */
  472                 return (EINVAL);
  473 
  474         case SIOCDIFADDR_IN6:
  475                 /*
  476                  * for IPv4, we look for existing in_ifaddr here to allow
  477                  * "ifconfig if0 delete" to remove the first IPv4 address on
  478                  * the interface.  For IPv6, as the spec allows multiple
  479                  * interface address from the day one, we consider "remove the
  480                  * first one" semantics to be not preferable.
  481                  */
  482                 if (ia == NULL)
  483                         return (EADDRNOTAVAIL);
  484                 /* FALLTHROUGH */
  485         case SIOCAIFADDR_IN6:
  486                 /*
  487                  * We always require users to specify a valid IPv6 address for
  488                  * the corresponding operation.
  489                  */
  490                 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
  491                     ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
  492                         return (EAFNOSUPPORT);
  493                 if (!privileged)
  494                         return (EPERM);
  495 
  496                 break;
  497 
  498         case SIOCGIFADDR_IN6:
  499                 /* This interface is basically deprecated. use SIOCGIFCONF. */
  500                 /* FALLTHROUGH */
  501         case SIOCGIFAFLAG_IN6:
  502         case SIOCGIFNETMASK_IN6:
  503         case SIOCGIFDSTADDR_IN6:
  504         case SIOCGIFALIFETIME_IN6:
  505                 /* must think again about its semantics */
  506                 if (ia == NULL)
  507                         return (EADDRNOTAVAIL);
  508                 break;
  509         case SIOCSIFALIFETIME_IN6:
  510             {
  511                 struct in6_addrlifetime *lt;
  512 
  513                 if (!privileged)
  514                         return (EPERM);
  515                 if (ia == NULL)
  516                         return (EADDRNOTAVAIL);
  517                 /* sanity for overflow - beware unsigned */
  518                 lt = &ifr->ifr_ifru.ifru_lifetime;
  519                 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
  520                     lt->ia6t_vltime + time_second < time_second) {
  521                         return EINVAL;
  522                 }
  523                 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
  524                     lt->ia6t_pltime + time_second < time_second) {
  525                         return EINVAL;
  526                 }
  527                 break;
  528             }
  529         }
  530 
  531         switch (cmd) {
  532 
  533         case SIOCGIFADDR_IN6:
  534                 ifr->ifr_addr = ia->ia_addr;
  535                 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
  536                         return (error);
  537                 break;
  538 
  539         case SIOCGIFDSTADDR_IN6:
  540                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
  541                         return (EINVAL);
  542                 /*
  543                  * XXX: should we check if ifa_dstaddr is NULL and return
  544                  * an error?
  545                  */
  546                 ifr->ifr_dstaddr = ia->ia_dstaddr;
  547                 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
  548                         return (error);
  549                 break;
  550 
  551         case SIOCGIFNETMASK_IN6:
  552                 ifr->ifr_addr = ia->ia_prefixmask;
  553                 break;
  554 
  555         case SIOCGIFAFLAG_IN6:
  556                 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
  557                 break;
  558 
  559         case SIOCGIFSTAT_IN6:
  560                 if (ifp == NULL)
  561                         return EINVAL;
  562                 bzero(&ifr->ifr_ifru.ifru_stat,
  563                     sizeof(ifr->ifr_ifru.ifru_stat));
  564                 ifr->ifr_ifru.ifru_stat =
  565                     *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
  566                 break;
  567 
  568         case SIOCGIFSTAT_ICMP6:
  569                 if (ifp == NULL)
  570                         return EINVAL;
  571                 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
  572                     sizeof(ifr->ifr_ifru.ifru_icmp6stat));
  573                 ifr->ifr_ifru.ifru_icmp6stat =
  574                     *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
  575                 break;
  576 
  577         case SIOCGIFALIFETIME_IN6:
  578                 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
  579                 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
  580                         time_t maxexpire;
  581                         struct in6_addrlifetime *retlt =
  582                             &ifr->ifr_ifru.ifru_lifetime;
  583 
  584                         /*
  585                          * XXX: adjust expiration time assuming time_t is
  586                          * signed.
  587                          */
  588                         maxexpire = (-1) &
  589                             ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
  590                         if (ia->ia6_lifetime.ia6t_vltime <
  591                             maxexpire - ia->ia6_updatetime) {
  592                                 retlt->ia6t_expire = ia->ia6_updatetime +
  593                                     ia->ia6_lifetime.ia6t_vltime;
  594                         } else
  595                                 retlt->ia6t_expire = maxexpire;
  596                 }
  597                 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
  598                         time_t maxexpire;
  599                         struct in6_addrlifetime *retlt =
  600                             &ifr->ifr_ifru.ifru_lifetime;
  601 
  602                         /*
  603                          * XXX: adjust expiration time assuming time_t is
  604                          * signed.
  605                          */
  606                         maxexpire = (-1) &
  607                             ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
  608                         if (ia->ia6_lifetime.ia6t_pltime <
  609                             maxexpire - ia->ia6_updatetime) {
  610                                 retlt->ia6t_preferred = ia->ia6_updatetime +
  611                                     ia->ia6_lifetime.ia6t_pltime;
  612                         } else
  613                                 retlt->ia6t_preferred = maxexpire;
  614                 }
  615                 break;
  616 
  617         case SIOCSIFALIFETIME_IN6:
  618                 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
  619                 /* for sanity */
  620                 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
  621                         ia->ia6_lifetime.ia6t_expire =
  622                                 time_second + ia->ia6_lifetime.ia6t_vltime;
  623                 } else
  624                         ia->ia6_lifetime.ia6t_expire = 0;
  625                 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
  626                         ia->ia6_lifetime.ia6t_preferred =
  627                                 time_second + ia->ia6_lifetime.ia6t_pltime;
  628                 } else
  629                         ia->ia6_lifetime.ia6t_preferred = 0;
  630                 break;
  631 
  632         case SIOCAIFADDR_IN6:
  633         {
  634                 int i, error = 0;
  635                 struct nd_prefixctl pr0;
  636                 struct nd_prefix *pr;
  637 
  638                 /*
  639                  * first, make or update the interface address structure,
  640                  * and link it to the list.
  641                  */
  642                 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
  643                         return (error);
  644                 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
  645                     == NULL) {
  646                         /*
  647                          * this can happen when the user specify the 0 valid
  648                          * lifetime.
  649                          */
  650                         break;
  651                 }
  652 
  653                 /*
  654                  * then, make the prefix on-link on the interface.
  655                  * XXX: we'd rather create the prefix before the address, but
  656                  * we need at least one address to install the corresponding
  657                  * interface route, so we configure the address first.
  658                  */
  659 
  660                 /*
  661                  * convert mask to prefix length (prefixmask has already
  662                  * been validated in in6_update_ifa().
  663                  */
  664                 bzero(&pr0, sizeof(pr0));
  665                 pr0.ndpr_ifp = ifp;
  666                 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
  667                     NULL);
  668                 if (pr0.ndpr_plen == 128) {
  669                         break;  /* we don't need to install a host route. */
  670                 }
  671                 pr0.ndpr_prefix = ifra->ifra_addr;
  672                 /* apply the mask for safety. */
  673                 for (i = 0; i < 4; i++) {
  674                         pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
  675                             ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
  676                 }
  677                 /*
  678                  * XXX: since we don't have an API to set prefix (not address)
  679                  * lifetimes, we just use the same lifetimes as addresses.
  680                  * The (temporarily) installed lifetimes can be overridden by
  681                  * later advertised RAs (when accept_rtadv is non 0), which is
  682                  * an intended behavior.
  683                  */
  684                 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
  685                 pr0.ndpr_raf_auto =
  686                     ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
  687                 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
  688                 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
  689 
  690                 /* add the prefix if not yet. */
  691                 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
  692                         /*
  693                          * nd6_prelist_add will install the corresponding
  694                          * interface route.
  695                          */
  696                         if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
  697                                 return (error);
  698                         if (pr == NULL) {
  699                                 log(LOG_ERR, "nd6_prelist_add succeeded but "
  700                                     "no prefix\n");
  701                                 return (EINVAL); /* XXX panic here? */
  702                         }
  703                 }
  704 
  705                 /* relate the address to the prefix */
  706                 if (ia->ia6_ndpr == NULL) {
  707                         ia->ia6_ndpr = pr;
  708                         pr->ndpr_refcnt++;
  709 
  710                         /*
  711                          * If this is the first autoconf address from the
  712                          * prefix, create a temporary address as well
  713                          * (when required).
  714                          */
  715                         if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
  716                             ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
  717                                 int e;
  718                                 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
  719                                         log(LOG_NOTICE, "in6_control: failed "
  720                                             "to create a temporary address, "
  721                                             "errno=%d\n", e);
  722                                 }
  723                         }
  724                 }
  725 
  726                 /*
  727                  * this might affect the status of autoconfigured addresses,
  728                  * that is, this address might make other addresses detached.
  729                  */
  730                 pfxlist_onlink_check();
  731                 if (error == 0 && ia)
  732                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
  733                 break;
  734         }
  735 
  736         case SIOCDIFADDR_IN6:
  737         {
  738                 struct nd_prefix *pr;
  739 
  740                 /*
  741                  * If the address being deleted is the only one that owns
  742                  * the corresponding prefix, expire the prefix as well.
  743                  * XXX: theoretically, we don't have to worry about such
  744                  * relationship, since we separate the address management
  745                  * and the prefix management.  We do this, however, to provide
  746                  * as much backward compatibility as possible in terms of
  747                  * the ioctl operation.
  748                  * Note that in6_purgeaddr() will decrement ndpr_refcnt.
  749                  */
  750                 pr = ia->ia6_ndpr;
  751                 in6_purgeaddr(&ia->ia_ifa);
  752                 if (pr && pr->ndpr_refcnt == 0)
  753                         prelist_remove(pr);
  754                 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
  755                 break;
  756         }
  757 
  758         default:
  759                 if (ifp == NULL || ifp->if_ioctl == 0)
  760                         return (EOPNOTSUPP);
  761                 return ((*ifp->if_ioctl)(ifp, cmd, data));
  762         }
  763 
  764         return (0);
  765 }
  766 
  767 /*
  768  * Update parameters of an IPv6 interface address.
  769  * If necessary, a new entry is created and linked into address chains.
  770  * This function is separated from in6_control().
  771  * XXX: should this be performed under splnet()?
  772  */
  773 int
  774 in6_update_ifa(ifp, ifra, ia, flags)
  775         struct ifnet *ifp;
  776         struct in6_aliasreq *ifra;
  777         struct in6_ifaddr *ia;
  778         int flags;
  779 {
  780         int error = 0, hostIsNew = 0, plen = -1;
  781         struct in6_ifaddr *oia;
  782         struct sockaddr_in6 dst6;
  783         struct in6_addrlifetime *lt;
  784         struct in6_multi_mship *imm;
  785         struct in6_multi *in6m_sol;
  786         struct rtentry *rt;
  787         int delay;
  788 
  789         /* Validate parameters */
  790         if (ifp == NULL || ifra == NULL) /* this maybe redundant */
  791                 return (EINVAL);
  792 
  793         /*
  794          * The destination address for a p2p link must have a family
  795          * of AF_UNSPEC or AF_INET6.
  796          */
  797         if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
  798             ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
  799             ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
  800                 return (EAFNOSUPPORT);
  801         /*
  802          * validate ifra_prefixmask.  don't check sin6_family, netmask
  803          * does not carry fields other than sin6_len.
  804          */
  805         if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
  806                 return (EINVAL);
  807         /*
  808          * Because the IPv6 address architecture is classless, we require
  809          * users to specify a (non 0) prefix length (mask) for a new address.
  810          * We also require the prefix (when specified) mask is valid, and thus
  811          * reject a non-consecutive mask.
  812          */
  813         if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
  814                 return (EINVAL);
  815         if (ifra->ifra_prefixmask.sin6_len != 0) {
  816                 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
  817                     (u_char *)&ifra->ifra_prefixmask +
  818                     ifra->ifra_prefixmask.sin6_len);
  819                 if (plen <= 0)
  820                         return (EINVAL);
  821         } else {
  822                 /*
  823                  * In this case, ia must not be NULL.  We just use its prefix
  824                  * length.
  825                  */
  826                 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
  827         }
  828         /*
  829          * If the destination address on a p2p interface is specified,
  830          * and the address is a scoped one, validate/set the scope
  831          * zone identifier.
  832          */
  833         dst6 = ifra->ifra_dstaddr;
  834         if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
  835             (dst6.sin6_family == AF_INET6)) {
  836                 struct in6_addr in6_tmp;
  837                 u_int32_t zoneid;
  838 
  839                 in6_tmp = dst6.sin6_addr;
  840                 if (in6_setscope(&in6_tmp, ifp, &zoneid))
  841                         return (EINVAL); /* XXX: should be impossible */
  842 
  843                 if (dst6.sin6_scope_id != 0) {
  844                         if (dst6.sin6_scope_id != zoneid)
  845                                 return (EINVAL);
  846                 } else          /* user omit to specify the ID. */
  847                         dst6.sin6_scope_id = zoneid;
  848 
  849                 /* convert into the internal form */
  850                 if (sa6_embedscope(&dst6, 0))
  851                         return (EINVAL); /* XXX: should be impossible */
  852         }
  853         /*
  854          * The destination address can be specified only for a p2p or a
  855          * loopback interface.  If specified, the corresponding prefix length
  856          * must be 128.
  857          */
  858         if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
  859                 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
  860                         /* XXX: noisy message */
  861                         nd6log((LOG_INFO, "in6_update_ifa: a destination can "
  862                             "be specified for a p2p or a loopback IF only\n"));
  863                         return (EINVAL);
  864                 }
  865                 if (plen != 128) {
  866                         nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
  867                             "be 128 when dstaddr is specified\n"));
  868                         return (EINVAL);
  869                 }
  870         }
  871         /* lifetime consistency check */
  872         lt = &ifra->ifra_lifetime;
  873         if (lt->ia6t_pltime > lt->ia6t_vltime)
  874                 return (EINVAL);
  875         if (lt->ia6t_vltime == 0) {
  876                 /*
  877                  * the following log might be noisy, but this is a typical
  878                  * configuration mistake or a tool's bug.
  879                  */
  880                 nd6log((LOG_INFO,
  881                     "in6_update_ifa: valid lifetime is 0 for %s\n",
  882                     ip6_sprintf(&ifra->ifra_addr.sin6_addr)));
  883 
  884                 if (ia == NULL)
  885                         return (0); /* there's nothing to do */
  886         }
  887 
  888         /*
  889          * If this is a new address, allocate a new ifaddr and link it
  890          * into chains.
  891          */
  892         if (ia == NULL) {
  893                 hostIsNew = 1;
  894                 /*
  895                  * When in6_update_ifa() is called in a process of a received
  896                  * RA, it is called under an interrupt context.  So, we should
  897                  * call malloc with M_NOWAIT.
  898                  */
  899                 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
  900                     M_NOWAIT);
  901                 if (ia == NULL)
  902                         return (ENOBUFS);
  903                 bzero((caddr_t)ia, sizeof(*ia));
  904                 LIST_INIT(&ia->ia6_memberships);
  905                 /* Initialize the address and masks, and put time stamp */
  906                 IFA_LOCK_INIT(&ia->ia_ifa);
  907                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
  908                 ia->ia_addr.sin6_family = AF_INET6;
  909                 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
  910                 ia->ia6_createtime = time_second;
  911                 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
  912                         /*
  913                          * XXX: some functions expect that ifa_dstaddr is not
  914                          * NULL for p2p interfaces.
  915                          */
  916                         ia->ia_ifa.ifa_dstaddr =
  917                             (struct sockaddr *)&ia->ia_dstaddr;
  918                 } else {
  919                         ia->ia_ifa.ifa_dstaddr = NULL;
  920                 }
  921                 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
  922 
  923                 ia->ia_ifp = ifp;
  924                 if ((oia = in6_ifaddr) != NULL) {
  925                         for ( ; oia->ia_next; oia = oia->ia_next)
  926                                 continue;
  927                         oia->ia_next = ia;
  928                 } else
  929                         in6_ifaddr = ia;
  930 
  931                 ia->ia_ifa.ifa_refcnt = 1;
  932                 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
  933         }
  934 
  935         /* update timestamp */
  936         ia->ia6_updatetime = time_second;
  937 
  938         /* set prefix mask */
  939         if (ifra->ifra_prefixmask.sin6_len) {
  940                 /*
  941                  * We prohibit changing the prefix length of an existing
  942                  * address, because
  943                  * + such an operation should be rare in IPv6, and
  944                  * + the operation would confuse prefix management.
  945                  */
  946                 if (ia->ia_prefixmask.sin6_len &&
  947                     in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
  948                         nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
  949                             " existing (%s) address should not be changed\n",
  950                             ip6_sprintf(&ia->ia_addr.sin6_addr)));
  951                         error = EINVAL;
  952                         goto unlink;
  953                 }
  954                 ia->ia_prefixmask = ifra->ifra_prefixmask;
  955         }
  956 
  957         /*
  958          * If a new destination address is specified, scrub the old one and
  959          * install the new destination.  Note that the interface must be
  960          * p2p or loopback (see the check above.)
  961          */
  962         if (dst6.sin6_family == AF_INET6 &&
  963             !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
  964                 int e;
  965 
  966                 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
  967                     (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
  968                         nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
  969                             "a route to the old destination: %s\n",
  970                             ip6_sprintf(&ia->ia_addr.sin6_addr)));
  971                         /* proceed anyway... */
  972                 } else
  973                         ia->ia_flags &= ~IFA_ROUTE;
  974                 ia->ia_dstaddr = dst6;
  975         }
  976 
  977         /*
  978          * Set lifetimes.  We do not refer to ia6t_expire and ia6t_preferred
  979          * to see if the address is deprecated or invalidated, but initialize
  980          * these members for applications.
  981          */
  982         ia->ia6_lifetime = ifra->ifra_lifetime;
  983         if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
  984                 ia->ia6_lifetime.ia6t_expire =
  985                     time_second + ia->ia6_lifetime.ia6t_vltime;
  986         } else
  987                 ia->ia6_lifetime.ia6t_expire = 0;
  988         if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
  989                 ia->ia6_lifetime.ia6t_preferred =
  990                     time_second + ia->ia6_lifetime.ia6t_pltime;
  991         } else
  992                 ia->ia6_lifetime.ia6t_preferred = 0;
  993 
  994         /* reset the interface and routing table appropriately. */
  995         if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
  996                 goto unlink;
  997 
  998         /*
  999          * configure address flags.
 1000          */
 1001         ia->ia6_flags = ifra->ifra_flags;
 1002         /*
 1003          * backward compatibility - if IN6_IFF_DEPRECATED is set from the
 1004          * userland, make it deprecated.
 1005          */
 1006         if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
 1007                 ia->ia6_lifetime.ia6t_pltime = 0;
 1008                 ia->ia6_lifetime.ia6t_preferred = time_second;
 1009         }
 1010         /*
 1011          * Make the address tentative before joining multicast addresses,
 1012          * so that corresponding MLD responses would not have a tentative
 1013          * source address.
 1014          */
 1015         ia->ia6_flags &= ~IN6_IFF_DUPLICATED;   /* safety */
 1016         if (hostIsNew && in6if_do_dad(ifp))
 1017                 ia->ia6_flags |= IN6_IFF_TENTATIVE;
 1018 
 1019         /*
 1020          * We are done if we have simply modified an existing address.
 1021          */
 1022         if (!hostIsNew)
 1023                 return (error);
 1024 
 1025         /*
 1026          * Beyond this point, we should call in6_purgeaddr upon an error,
 1027          * not just go to unlink.
 1028          */
 1029 
 1030         /* Join necessary multicast groups */
 1031         in6m_sol = NULL;
 1032         if ((ifp->if_flags & IFF_MULTICAST) != 0) {
 1033                 struct sockaddr_in6 mltaddr, mltmask;
 1034                 struct in6_addr llsol;
 1035 
 1036                 /* join solicited multicast addr for new host id */
 1037                 bzero(&llsol, sizeof(struct in6_addr));
 1038                 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
 1039                 llsol.s6_addr32[1] = 0;
 1040                 llsol.s6_addr32[2] = htonl(1);
 1041                 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
 1042                 llsol.s6_addr8[12] = 0xff;
 1043                 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
 1044                         /* XXX: should not happen */
 1045                         log(LOG_ERR, "in6_update_ifa: "
 1046                             "in6_setscope failed\n");
 1047                         goto cleanup;
 1048                 }
 1049                 delay = 0;
 1050                 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
 1051                         /*
 1052                          * We need a random delay for DAD on the address
 1053                          * being configured.  It also means delaying
 1054                          * transmission of the corresponding MLD report to
 1055                          * avoid report collision.
 1056                          * [draft-ietf-ipv6-rfc2462bis-02.txt]
 1057                          */
 1058                         delay = arc4random() %
 1059                             (MAX_RTR_SOLICITATION_DELAY * hz);
 1060                 }
 1061                 imm = in6_joingroup(ifp, &llsol, &error, delay);
 1062                 if (imm == NULL) {
 1063                         nd6log((LOG_WARNING,
 1064                             "in6_update_ifa: addmulti failed for "
 1065                             "%s on %s (errno=%d)\n",
 1066                             ip6_sprintf(&llsol), if_name(ifp),
 1067                             error));
 1068                         in6_purgeaddr((struct ifaddr *)ia);
 1069                         return (error);
 1070                 }
 1071                 LIST_INSERT_HEAD(&ia->ia6_memberships,
 1072                     imm, i6mm_chain);
 1073                 in6m_sol = imm->i6mm_maddr;
 1074 
 1075                 bzero(&mltmask, sizeof(mltmask));
 1076                 mltmask.sin6_len = sizeof(struct sockaddr_in6);
 1077                 mltmask.sin6_family = AF_INET6;
 1078                 mltmask.sin6_addr = in6mask32;
 1079 #define MLTMASK_LEN  4  /* mltmask's masklen (=32bit=4octet) */
 1080 
 1081                 /*
 1082                  * join link-local all-nodes address
 1083                  */
 1084                 bzero(&mltaddr, sizeof(mltaddr));
 1085                 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
 1086                 mltaddr.sin6_family = AF_INET6;
 1087                 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
 1088                 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
 1089                     0)
 1090                         goto cleanup; /* XXX: should not fail */
 1091 
 1092                 /*
 1093                  * XXX: do we really need this automatic routes?
 1094                  * We should probably reconsider this stuff.  Most applications
 1095                  * actually do not need the routes, since they usually specify
 1096                  * the outgoing interface.
 1097                  */
 1098                 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
 1099                 if (rt) {
 1100                         if (memcmp(&mltaddr.sin6_addr,
 1101                             &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
 1102                             MLTMASK_LEN)) {
 1103                                 RTFREE_LOCKED(rt);
 1104                                 rt = NULL;
 1105                         }
 1106                 }
 1107                 if (!rt) {
 1108                         /* XXX: we need RTF_CLONING to fake nd6_rtrequest */
 1109                         error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
 1110                             (struct sockaddr *)&ia->ia_addr,
 1111                             (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
 1112                             (struct rtentry **)0);
 1113                         if (error)
 1114                                 goto cleanup;
 1115                 } else
 1116                         RTFREE_LOCKED(rt);
 1117 
 1118                 /*
 1119                  * XXX: do we really need this automatic routes?
 1120                  * We should probably reconsider this stuff.  Most applications
 1121                  * actually do not need the routes, since they usually specify
 1122                  * the outgoing interface.
 1123                  */
 1124                 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
 1125                 if (rt) {
 1126                         /* XXX: only works in !SCOPEDROUTING case. */
 1127                         if (memcmp(&mltaddr.sin6_addr,
 1128                             &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
 1129                             MLTMASK_LEN)) {
 1130                                 RTFREE_LOCKED(rt);
 1131                                 rt = NULL;
 1132                         }
 1133                 }
 1134                 if (!rt) {
 1135                         error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
 1136                             (struct sockaddr *)&ia->ia_addr,
 1137                             (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
 1138                             (struct rtentry **)0);
 1139                         if (error)
 1140                                 goto cleanup;
 1141                 } else {
 1142                         RTFREE_LOCKED(rt);
 1143                 }
 1144 
 1145                 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
 1146                 if (!imm) {
 1147                         nd6log((LOG_WARNING,
 1148                             "in6_update_ifa: addmulti failed for "
 1149                             "%s on %s (errno=%d)\n",
 1150                             ip6_sprintf(&mltaddr.sin6_addr),
 1151                             if_name(ifp), error));
 1152                         goto cleanup;
 1153                 }
 1154                 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
 1155 
 1156                 /*
 1157                  * join node information group address
 1158                  */
 1159 #define hostnamelen     strlen(hostname)
 1160                 delay = 0;
 1161                 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
 1162                         /*
 1163                          * The spec doesn't say anything about delay for this
 1164                          * group, but the same logic should apply.
 1165                          */
 1166                         delay = arc4random() %
 1167                             (MAX_RTR_SOLICITATION_DELAY * hz);
 1168                 }
 1169                 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
 1170                     == 0) {
 1171                         imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
 1172                             delay); /* XXX jinmei */
 1173                         if (!imm) {
 1174                                 nd6log((LOG_WARNING, "in6_update_ifa: "
 1175                                     "addmulti failed for %s on %s "
 1176                                     "(errno=%d)\n",
 1177                                     ip6_sprintf(&mltaddr.sin6_addr),
 1178                                     if_name(ifp), error));
 1179                                 /* XXX not very fatal, go on... */
 1180                         } else {
 1181                                 LIST_INSERT_HEAD(&ia->ia6_memberships,
 1182                                     imm, i6mm_chain);
 1183                         }
 1184                 }
 1185 #undef hostnamelen
 1186 
 1187                 /*
 1188                  * join interface-local all-nodes address.
 1189                  * (ff01::1%ifN, and ff01::%ifN/32)
 1190                  */
 1191                 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
 1192                 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL))
 1193                     != 0)
 1194                         goto cleanup; /* XXX: should not fail */
 1195                 /* XXX: again, do we really need the route? */
 1196                 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
 1197                 if (rt) {
 1198                         if (memcmp(&mltaddr.sin6_addr,
 1199                             &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
 1200                             MLTMASK_LEN)) {
 1201                                 RTFREE_LOCKED(rt);
 1202                                 rt = NULL;
 1203                         }
 1204                 }
 1205                 if (!rt) {
 1206                         error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
 1207                             (struct sockaddr *)&ia->ia_addr,
 1208                             (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
 1209                             (struct rtentry **)0);
 1210                         if (error)
 1211                                 goto cleanup;
 1212                 } else
 1213                         RTFREE_LOCKED(rt);
 1214 
 1215                 /* XXX: again, do we really need the route? */
 1216                 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
 1217                 if (rt) {
 1218                         if (memcmp(&mltaddr.sin6_addr,
 1219                             &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
 1220                             MLTMASK_LEN)) {
 1221                                 RTFREE_LOCKED(rt);
 1222                                 rt = NULL;
 1223                         }
 1224                 }
 1225                 if (!rt) {
 1226                         error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
 1227                             (struct sockaddr *)&ia->ia_addr,
 1228                             (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
 1229                             (struct rtentry **)0);
 1230                         if (error)
 1231                                 goto cleanup;
 1232                 } else {
 1233                         RTFREE_LOCKED(rt);
 1234                 }
 1235 
 1236                 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
 1237                 if (!imm) {
 1238                         nd6log((LOG_WARNING, "in6_update_ifa: "
 1239                             "addmulti failed for %s on %s "
 1240                             "(errno=%d)\n",
 1241                             ip6_sprintf(&mltaddr.sin6_addr),
 1242                             if_name(ifp), error));
 1243                         goto cleanup;
 1244                 }
 1245                 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
 1246 #undef  MLTMASK_LEN
 1247         }
 1248 
 1249         /*
 1250          * Perform DAD, if needed.
 1251          * XXX It may be of use, if we can administratively
 1252          * disable DAD.
 1253          */
 1254         if (hostIsNew && in6if_do_dad(ifp) &&
 1255             ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
 1256             (ia->ia6_flags & IN6_IFF_TENTATIVE))
 1257         {
 1258                 int mindelay, maxdelay;
 1259 
 1260                 delay = 0;
 1261                 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
 1262                         /*
 1263                          * We need to impose a delay before sending an NS
 1264                          * for DAD.  Check if we also needed a delay for the
 1265                          * corresponding MLD message.  If we did, the delay
 1266                          * should be larger than the MLD delay (this could be
 1267                          * relaxed a bit, but this simple logic is at least
 1268                          * safe).
 1269                          */
 1270                         mindelay = 0;
 1271                         if (in6m_sol != NULL &&
 1272                             in6m_sol->in6m_state == MLD_REPORTPENDING) {
 1273                                 mindelay = in6m_sol->in6m_timer;
 1274                         }
 1275                         maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
 1276                         if (maxdelay - mindelay == 0)
 1277                                 delay = 0;
 1278                         else {
 1279                                 delay =
 1280                                     (arc4random() % (maxdelay - mindelay)) +
 1281                                     mindelay;
 1282                         }
 1283                 }
 1284                 nd6_dad_start((struct ifaddr *)ia, delay);
 1285         }
 1286 
 1287         return (error);
 1288 
 1289   unlink:
 1290         /*
 1291          * XXX: if a change of an existing address failed, keep the entry
 1292          * anyway.
 1293          */
 1294         if (hostIsNew)
 1295                 in6_unlink_ifa(ia, ifp);
 1296         return (error);
 1297 
 1298   cleanup:
 1299         in6_purgeaddr(&ia->ia_ifa);
 1300         return error;
 1301 }
 1302 
 1303 void
 1304 in6_purgeaddr(ifa)
 1305         struct ifaddr *ifa;
 1306 {
 1307         struct ifnet *ifp = ifa->ifa_ifp;
 1308         struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
 1309         struct in6_multi_mship *imm;
 1310 
 1311         /* stop DAD processing */
 1312         nd6_dad_stop(ifa);
 1313 
 1314         /*
 1315          * delete route to the destination of the address being purged.
 1316          * The interface must be p2p or loopback in this case.
 1317          */
 1318         if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
 1319                 int e;
 1320 
 1321                 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
 1322                     != 0) {
 1323                         log(LOG_ERR, "in6_purgeaddr: failed to remove "
 1324                             "a route to the p2p destination: %s on %s, "
 1325                             "errno=%d\n",
 1326                             ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
 1327                             e);
 1328                         /* proceed anyway... */
 1329                 } else
 1330                         ia->ia_flags &= ~IFA_ROUTE;
 1331         }
 1332 
 1333         /* Remove ownaddr's loopback rtentry, if it exists. */
 1334         in6_ifremloop(&(ia->ia_ifa));
 1335 
 1336         /*
 1337          * leave from multicast groups we have joined for the interface
 1338          */
 1339         while ((imm = ia->ia6_memberships.lh_first) != NULL) {
 1340                 LIST_REMOVE(imm, i6mm_chain);
 1341                 in6_leavegroup(imm);
 1342         }
 1343 
 1344         in6_unlink_ifa(ia, ifp);
 1345 }
 1346 
 1347 static void
 1348 in6_unlink_ifa(ia, ifp)
 1349         struct in6_ifaddr *ia;
 1350         struct ifnet *ifp;
 1351 {
 1352         struct in6_ifaddr *oia;
 1353         int     s = splnet();
 1354 
 1355         TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
 1356 
 1357         oia = ia;
 1358         if (oia == (ia = in6_ifaddr))
 1359                 in6_ifaddr = ia->ia_next;
 1360         else {
 1361                 while (ia->ia_next && (ia->ia_next != oia))
 1362                         ia = ia->ia_next;
 1363                 if (ia->ia_next)
 1364                         ia->ia_next = oia->ia_next;
 1365                 else {
 1366                         /* search failed */
 1367                         printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
 1368                 }
 1369         }
 1370 
 1371         /*
 1372          * Release the reference to the base prefix.  There should be a
 1373          * positive reference.
 1374          */
 1375         if (oia->ia6_ndpr == NULL) {
 1376                 nd6log((LOG_NOTICE,
 1377                     "in6_unlink_ifa: autoconf'ed address "
 1378                     "%p has no prefix\n", oia));
 1379         } else {
 1380                 oia->ia6_ndpr->ndpr_refcnt--;
 1381                 oia->ia6_ndpr = NULL;
 1382         }
 1383 
 1384         /*
 1385          * Also, if the address being removed is autoconf'ed, call
 1386          * pfxlist_onlink_check() since the release might affect the status of
 1387          * other (detached) addresses. 
 1388          */
 1389         if ((oia->ia6_flags & IN6_IFF_AUTOCONF)) {
 1390                 pfxlist_onlink_check();
 1391         }
 1392 
 1393         /*
 1394          * release another refcnt for the link from in6_ifaddr.
 1395          * Note that we should decrement the refcnt at least once for all *BSD.
 1396          */
 1397         IFAFREE(&oia->ia_ifa);
 1398 
 1399         splx(s);
 1400 }
 1401 
 1402 void
 1403 in6_purgeif(ifp)
 1404         struct ifnet *ifp;
 1405 {
 1406         struct ifaddr *ifa, *nifa;
 1407 
 1408         for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
 1409                 nifa = TAILQ_NEXT(ifa, ifa_list);
 1410                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1411                         continue;
 1412                 in6_purgeaddr(ifa);
 1413         }
 1414 
 1415         in6_ifdetach(ifp);
 1416 }
 1417 
 1418 /*
 1419  * SIOC[GAD]LIFADDR.
 1420  *      SIOCGLIFADDR: get first address. (?)
 1421  *      SIOCGLIFADDR with IFLR_PREFIX:
 1422  *              get first address that matches the specified prefix.
 1423  *      SIOCALIFADDR: add the specified address.
 1424  *      SIOCALIFADDR with IFLR_PREFIX:
 1425  *              add the specified prefix, filling hostid part from
 1426  *              the first link-local address.  prefixlen must be <= 64.
 1427  *      SIOCDLIFADDR: delete the specified address.
 1428  *      SIOCDLIFADDR with IFLR_PREFIX:
 1429  *              delete the first address that matches the specified prefix.
 1430  * return values:
 1431  *      EINVAL on invalid parameters
 1432  *      EADDRNOTAVAIL on prefix match failed/specified address not found
 1433  *      other values may be returned from in6_ioctl()
 1434  *
 1435  * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
 1436  * this is to accomodate address naming scheme other than RFC2374,
 1437  * in the future.
 1438  * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
 1439  * address encoding scheme. (see figure on page 8)
 1440  */
 1441 static int
 1442 in6_lifaddr_ioctl(so, cmd, data, ifp, td)
 1443         struct socket *so;
 1444         u_long cmd;
 1445         caddr_t data;
 1446         struct ifnet *ifp;
 1447         struct thread *td;
 1448 {
 1449         struct if_laddrreq *iflr = (struct if_laddrreq *)data;
 1450         struct ifaddr *ifa;
 1451         struct sockaddr *sa;
 1452 
 1453         /* sanity checks */
 1454         if (!data || !ifp) {
 1455                 panic("invalid argument to in6_lifaddr_ioctl");
 1456                 /* NOTREACHED */
 1457         }
 1458 
 1459         switch (cmd) {
 1460         case SIOCGLIFADDR:
 1461                 /* address must be specified on GET with IFLR_PREFIX */
 1462                 if ((iflr->flags & IFLR_PREFIX) == 0)
 1463                         break;
 1464                 /* FALLTHROUGH */
 1465         case SIOCALIFADDR:
 1466         case SIOCDLIFADDR:
 1467                 /* address must be specified on ADD and DELETE */
 1468                 sa = (struct sockaddr *)&iflr->addr;
 1469                 if (sa->sa_family != AF_INET6)
 1470                         return EINVAL;
 1471                 if (sa->sa_len != sizeof(struct sockaddr_in6))
 1472                         return EINVAL;
 1473                 /* XXX need improvement */
 1474                 sa = (struct sockaddr *)&iflr->dstaddr;
 1475                 if (sa->sa_family && sa->sa_family != AF_INET6)
 1476                         return EINVAL;
 1477                 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
 1478                         return EINVAL;
 1479                 break;
 1480         default: /* shouldn't happen */
 1481 #if 0
 1482                 panic("invalid cmd to in6_lifaddr_ioctl");
 1483                 /* NOTREACHED */
 1484 #else
 1485                 return EOPNOTSUPP;
 1486 #endif
 1487         }
 1488         if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
 1489                 return EINVAL;
 1490 
 1491         switch (cmd) {
 1492         case SIOCALIFADDR:
 1493             {
 1494                 struct in6_aliasreq ifra;
 1495                 struct in6_addr *hostid = NULL;
 1496                 int prefixlen;
 1497 
 1498                 if ((iflr->flags & IFLR_PREFIX) != 0) {
 1499                         struct sockaddr_in6 *sin6;
 1500 
 1501                         /*
 1502                          * hostid is to fill in the hostid part of the
 1503                          * address.  hostid points to the first link-local
 1504                          * address attached to the interface.
 1505                          */
 1506                         ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
 1507                         if (!ifa)
 1508                                 return EADDRNOTAVAIL;
 1509                         hostid = IFA_IN6(ifa);
 1510 
 1511                         /* prefixlen must be <= 64. */
 1512                         if (64 < iflr->prefixlen)
 1513                                 return EINVAL;
 1514                         prefixlen = iflr->prefixlen;
 1515 
 1516                         /* hostid part must be zero. */
 1517                         sin6 = (struct sockaddr_in6 *)&iflr->addr;
 1518                         if (sin6->sin6_addr.s6_addr32[2] != 0 ||
 1519                             sin6->sin6_addr.s6_addr32[3] != 0) {
 1520                                 return EINVAL;
 1521                         }
 1522                 } else
 1523                         prefixlen = iflr->prefixlen;
 1524 
 1525                 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
 1526                 bzero(&ifra, sizeof(ifra));
 1527                 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
 1528 
 1529                 bcopy(&iflr->addr, &ifra.ifra_addr,
 1530                     ((struct sockaddr *)&iflr->addr)->sa_len);
 1531                 if (hostid) {
 1532                         /* fill in hostid part */
 1533                         ifra.ifra_addr.sin6_addr.s6_addr32[2] =
 1534                             hostid->s6_addr32[2];
 1535                         ifra.ifra_addr.sin6_addr.s6_addr32[3] =
 1536                             hostid->s6_addr32[3];
 1537                 }
 1538 
 1539                 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
 1540                         bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
 1541                             ((struct sockaddr *)&iflr->dstaddr)->sa_len);
 1542                         if (hostid) {
 1543                                 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
 1544                                     hostid->s6_addr32[2];
 1545                                 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
 1546                                     hostid->s6_addr32[3];
 1547                         }
 1548                 }
 1549 
 1550                 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
 1551                 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
 1552 
 1553                 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
 1554                 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
 1555             }
 1556         case SIOCGLIFADDR:
 1557         case SIOCDLIFADDR:
 1558             {
 1559                 struct in6_ifaddr *ia;
 1560                 struct in6_addr mask, candidate, match;
 1561                 struct sockaddr_in6 *sin6;
 1562                 int cmp;
 1563 
 1564                 bzero(&mask, sizeof(mask));
 1565                 if (iflr->flags & IFLR_PREFIX) {
 1566                         /* lookup a prefix rather than address. */
 1567                         in6_prefixlen2mask(&mask, iflr->prefixlen);
 1568 
 1569                         sin6 = (struct sockaddr_in6 *)&iflr->addr;
 1570                         bcopy(&sin6->sin6_addr, &match, sizeof(match));
 1571                         match.s6_addr32[0] &= mask.s6_addr32[0];
 1572                         match.s6_addr32[1] &= mask.s6_addr32[1];
 1573                         match.s6_addr32[2] &= mask.s6_addr32[2];
 1574                         match.s6_addr32[3] &= mask.s6_addr32[3];
 1575 
 1576                         /* if you set extra bits, that's wrong */
 1577                         if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
 1578                                 return EINVAL;
 1579 
 1580                         cmp = 1;
 1581                 } else {
 1582                         if (cmd == SIOCGLIFADDR) {
 1583                                 /* on getting an address, take the 1st match */
 1584                                 cmp = 0;        /* XXX */
 1585                         } else {
 1586                                 /* on deleting an address, do exact match */
 1587                                 in6_prefixlen2mask(&mask, 128);
 1588                                 sin6 = (struct sockaddr_in6 *)&iflr->addr;
 1589                                 bcopy(&sin6->sin6_addr, &match, sizeof(match));
 1590 
 1591                                 cmp = 1;
 1592                         }
 1593                 }
 1594 
 1595                 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
 1596                         if (ifa->ifa_addr->sa_family != AF_INET6)
 1597                                 continue;
 1598                         if (!cmp)
 1599                                 break;
 1600 
 1601                         /*
 1602                          * XXX: this is adhoc, but is necessary to allow
 1603                          * a user to specify fe80::/64 (not /10) for a
 1604                          * link-local address.
 1605                          */
 1606                         bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
 1607                         in6_clearscope(&candidate);
 1608                         candidate.s6_addr32[0] &= mask.s6_addr32[0];
 1609                         candidate.s6_addr32[1] &= mask.s6_addr32[1];
 1610                         candidate.s6_addr32[2] &= mask.s6_addr32[2];
 1611                         candidate.s6_addr32[3] &= mask.s6_addr32[3];
 1612                         if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
 1613                                 break;
 1614                 }
 1615                 if (!ifa)
 1616                         return EADDRNOTAVAIL;
 1617                 ia = ifa2ia6(ifa);
 1618 
 1619                 if (cmd == SIOCGLIFADDR) {
 1620                         int error;
 1621 
 1622                         /* fill in the if_laddrreq structure */
 1623                         bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
 1624                         error = sa6_recoverscope(
 1625                             (struct sockaddr_in6 *)&iflr->addr);
 1626                         if (error != 0)
 1627                                 return (error);
 1628 
 1629                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
 1630                                 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
 1631                                     ia->ia_dstaddr.sin6_len);
 1632                                 error = sa6_recoverscope(
 1633                                     (struct sockaddr_in6 *)&iflr->dstaddr);
 1634                                 if (error != 0)
 1635                                         return (error);
 1636                         } else
 1637                                 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
 1638 
 1639                         iflr->prefixlen =
 1640                             in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
 1641 
 1642                         iflr->flags = ia->ia6_flags;    /* XXX */
 1643 
 1644                         return 0;
 1645                 } else {
 1646                         struct in6_aliasreq ifra;
 1647 
 1648                         /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
 1649                         bzero(&ifra, sizeof(ifra));
 1650                         bcopy(iflr->iflr_name, ifra.ifra_name,
 1651                             sizeof(ifra.ifra_name));
 1652 
 1653                         bcopy(&ia->ia_addr, &ifra.ifra_addr,
 1654                             ia->ia_addr.sin6_len);
 1655                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
 1656                                 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
 1657                                     ia->ia_dstaddr.sin6_len);
 1658                         } else {
 1659                                 bzero(&ifra.ifra_dstaddr,
 1660                                     sizeof(ifra.ifra_dstaddr));
 1661                         }
 1662                         bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
 1663                             ia->ia_prefixmask.sin6_len);
 1664 
 1665                         ifra.ifra_flags = ia->ia6_flags;
 1666                         return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
 1667                             ifp, td);
 1668                 }
 1669             }
 1670         }
 1671 
 1672         return EOPNOTSUPP;      /* just for safety */
 1673 }
 1674 
 1675 /*
 1676  * Initialize an interface's intetnet6 address
 1677  * and routing table entry.
 1678  */
 1679 static int
 1680 in6_ifinit(ifp, ia, sin6, newhost)
 1681         struct ifnet *ifp;
 1682         struct in6_ifaddr *ia;
 1683         struct sockaddr_in6 *sin6;
 1684         int newhost;
 1685 {
 1686         int     error = 0, plen, ifacount = 0;
 1687         int     s = splimp();
 1688         struct ifaddr *ifa;
 1689 
 1690         /*
 1691          * Give the interface a chance to initialize
 1692          * if this is its first address,
 1693          * and to validate the address if necessary.
 1694          */
 1695         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
 1696                 if (ifa->ifa_addr == NULL)
 1697                         continue;       /* just for safety */
 1698                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1699                         continue;
 1700                 ifacount++;
 1701         }
 1702 
 1703         ia->ia_addr = *sin6;
 1704 
 1705         if (ifacount <= 1 && ifp->if_ioctl) {
 1706                 IFF_LOCKGIANT(ifp);
 1707                 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
 1708                 IFF_UNLOCKGIANT(ifp);
 1709                 if (error) {
 1710                         splx(s);
 1711                         return (error);
 1712                 }
 1713         }
 1714         splx(s);
 1715 
 1716         ia->ia_ifa.ifa_metric = ifp->if_metric;
 1717 
 1718         /* we could do in(6)_socktrim here, but just omit it at this moment. */
 1719 
 1720         if (newhost) {
 1721                 /*
 1722                  * set the rtrequest function to create llinfo.  It also
 1723                  * adjust outgoing interface of the route for the local
 1724                  * address when called via in6_ifaddloop() below.
 1725                  */
 1726                 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
 1727         }
 1728 
 1729         /*
 1730          * Special case:
 1731          * If a new destination address is specified for a point-to-point
 1732          * interface, install a route to the destination as an interface
 1733          * direct route.  In addition, if the link is expected to have neighbor
 1734          * cache entries, specify RTF_LLINFO so that a cache entry for the
 1735          * destination address will be created.
 1736          * created
 1737          * XXX: the logic below rejects assigning multiple addresses on a p2p
 1738          * interface that share the same destination.
 1739          */
 1740         plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
 1741         if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
 1742             ia->ia_dstaddr.sin6_family == AF_INET6) {
 1743                 int rtflags = RTF_UP | RTF_HOST;
 1744                 struct rtentry *rt = NULL, **rtp = NULL;
 1745 
 1746                 if (nd6_need_cache(ifp) != 0) {
 1747                         rtflags |= RTF_LLINFO;
 1748                         rtp = &rt;
 1749                 }
 1750 
 1751                 error = rtrequest(RTM_ADD, (struct sockaddr *)&ia->ia_dstaddr,
 1752                     (struct sockaddr *)&ia->ia_addr,
 1753                     (struct sockaddr *)&ia->ia_prefixmask,
 1754                     ia->ia_flags | rtflags, rtp);
 1755                 if (error != 0)
 1756                         return (error);
 1757                 if (rt != NULL) {
 1758                         struct llinfo_nd6 *ln;
 1759 
 1760                         RT_LOCK(rt);
 1761                         ln = (struct llinfo_nd6 *)rt->rt_llinfo;
 1762                         if (ln != NULL) {
 1763                                 /*
 1764                                  * Set the state to STALE because we don't
 1765                                  * have to perform address resolution on this
 1766                                  * link.
 1767                                  */
 1768                                 ln->ln_state = ND6_LLINFO_STALE;
 1769                         }
 1770                         RT_REMREF(rt);
 1771                         RT_UNLOCK(rt);
 1772                 }
 1773                 ia->ia_flags |= IFA_ROUTE;
 1774         }
 1775         if (plen < 128) {
 1776                 /*
 1777                  * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
 1778                  */
 1779                 ia->ia_ifa.ifa_flags |= RTF_CLONING;
 1780         }
 1781 
 1782         /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
 1783         if (newhost)
 1784                 in6_ifaddloop(&(ia->ia_ifa));
 1785 
 1786         return (error);
 1787 }
 1788 
 1789 struct in6_multi_mship *
 1790 in6_joingroup(ifp, addr, errorp, delay)
 1791         struct ifnet *ifp;
 1792         struct in6_addr *addr;
 1793         int *errorp;
 1794         int delay;
 1795 {
 1796         struct in6_multi_mship *imm;
 1797 
 1798         imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT);
 1799         if (!imm) {
 1800                 *errorp = ENOBUFS;
 1801                 return NULL;
 1802         }
 1803         imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, delay);
 1804         if (!imm->i6mm_maddr) {
 1805                 /* *errorp is alrady set */
 1806                 free(imm, M_IP6MADDR);
 1807                 return NULL;
 1808         }
 1809         return imm;
 1810 }
 1811 
 1812 int
 1813 in6_leavegroup(imm)
 1814         struct in6_multi_mship *imm;
 1815 {
 1816 
 1817         if (imm->i6mm_maddr)
 1818                 in6_delmulti(imm->i6mm_maddr);
 1819         free(imm,  M_IP6MADDR);
 1820         return 0;
 1821 }
 1822 
 1823 /*
 1824  * Find an IPv6 interface link-local address specific to an interface.
 1825  */
 1826 struct in6_ifaddr *
 1827 in6ifa_ifpforlinklocal(ifp, ignoreflags)
 1828         struct ifnet *ifp;
 1829         int ignoreflags;
 1830 {
 1831         struct ifaddr *ifa;
 1832 
 1833         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
 1834                 if (ifa->ifa_addr == NULL)
 1835                         continue;       /* just for safety */
 1836                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1837                         continue;
 1838                 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
 1839                         if ((((struct in6_ifaddr *)ifa)->ia6_flags &
 1840                              ignoreflags) != 0)
 1841                                 continue;
 1842                         break;
 1843                 }
 1844         }
 1845 
 1846         return ((struct in6_ifaddr *)ifa);
 1847 }
 1848 
 1849 
 1850 /*
 1851  * find the internet address corresponding to a given interface and address.
 1852  */
 1853 struct in6_ifaddr *
 1854 in6ifa_ifpwithaddr(ifp, addr)
 1855         struct ifnet *ifp;
 1856         struct in6_addr *addr;
 1857 {
 1858         struct ifaddr *ifa;
 1859 
 1860         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
 1861                 if (ifa->ifa_addr == NULL)
 1862                         continue;       /* just for safety */
 1863                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1864                         continue;
 1865                 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
 1866                         break;
 1867         }
 1868 
 1869         return ((struct in6_ifaddr *)ifa);
 1870 }
 1871 
 1872 /*
 1873  * Convert IP6 address to printable (loggable) representation.
 1874  */
 1875 static char digits[] = "0123456789abcdef";
 1876 static int ip6round = 0;
 1877 char *
 1878 ip6_sprintf(addr)
 1879         const struct in6_addr *addr;
 1880 {
 1881         static char ip6buf[8][48];
 1882         int i;
 1883         char *cp;
 1884         const u_int16_t *a = (const u_int16_t *)addr;
 1885         const u_int8_t *d;
 1886         int dcolon = 0;
 1887 
 1888         ip6round = (ip6round + 1) & 7;
 1889         cp = ip6buf[ip6round];
 1890 
 1891         for (i = 0; i < 8; i++) {
 1892                 if (dcolon == 1) {
 1893                         if (*a == 0) {
 1894                                 if (i == 7)
 1895                                         *cp++ = ':';
 1896                                 a++;
 1897                                 continue;
 1898                         } else
 1899                                 dcolon = 2;
 1900                 }
 1901                 if (*a == 0) {
 1902                         if (dcolon == 0 && *(a + 1) == 0) {
 1903                                 if (i == 0)
 1904                                         *cp++ = ':';
 1905                                 *cp++ = ':';
 1906                                 dcolon = 1;
 1907                         } else {
 1908                                 *cp++ = '';
 1909                                 *cp++ = ':';
 1910                         }
 1911                         a++;
 1912                         continue;
 1913                 }
 1914                 d = (const u_char *)a;
 1915                 *cp++ = digits[*d >> 4];
 1916                 *cp++ = digits[*d++ & 0xf];
 1917                 *cp++ = digits[*d >> 4];
 1918                 *cp++ = digits[*d & 0xf];
 1919                 *cp++ = ':';
 1920                 a++;
 1921         }
 1922         *--cp = 0;
 1923         return (ip6buf[ip6round]);
 1924 }
 1925 
 1926 int
 1927 in6_localaddr(in6)
 1928         struct in6_addr *in6;
 1929 {
 1930         struct in6_ifaddr *ia;
 1931 
 1932         if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
 1933                 return 1;
 1934 
 1935         for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
 1936                 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
 1937                     &ia->ia_prefixmask.sin6_addr)) {
 1938                         return 1;
 1939                 }
 1940         }
 1941 
 1942         return (0);
 1943 }
 1944 
 1945 int
 1946 in6_is_addr_deprecated(sa6)
 1947         struct sockaddr_in6 *sa6;
 1948 {
 1949         struct in6_ifaddr *ia;
 1950 
 1951         for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
 1952                 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
 1953                                        &sa6->sin6_addr) &&
 1954                     (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
 1955                         return (1); /* true */
 1956 
 1957                 /* XXX: do we still have to go thru the rest of the list? */
 1958         }
 1959 
 1960         return (0);             /* false */
 1961 }
 1962 
 1963 /*
 1964  * return length of part which dst and src are equal
 1965  * hard coding...
 1966  */
 1967 int
 1968 in6_matchlen(src, dst)
 1969 struct in6_addr *src, *dst;
 1970 {
 1971         int match = 0;
 1972         u_char *s = (u_char *)src, *d = (u_char *)dst;
 1973         u_char *lim = s + 16, r;
 1974 
 1975         while (s < lim)
 1976                 if ((r = (*d++ ^ *s++)) != 0) {
 1977                         while (r < 128) {
 1978                                 match++;
 1979                                 r <<= 1;
 1980                         }
 1981                         break;
 1982                 } else
 1983                         match += 8;
 1984         return match;
 1985 }
 1986 
 1987 /* XXX: to be scope conscious */
 1988 int
 1989 in6_are_prefix_equal(p1, p2, len)
 1990         struct in6_addr *p1, *p2;
 1991         int len;
 1992 {
 1993         int bytelen, bitlen;
 1994 
 1995         /* sanity check */
 1996         if (0 > len || len > 128) {
 1997                 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
 1998                     len);
 1999                 return (0);
 2000         }
 2001 
 2002         bytelen = len / 8;
 2003         bitlen = len % 8;
 2004 
 2005         if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
 2006                 return (0);
 2007         if (bitlen != 0 &&
 2008             p1->s6_addr[bytelen] >> (8 - bitlen) !=
 2009             p2->s6_addr[bytelen] >> (8 - bitlen))
 2010                 return (0);
 2011 
 2012         return (1);
 2013 }
 2014 
 2015 void
 2016 in6_prefixlen2mask(maskp, len)
 2017         struct in6_addr *maskp;
 2018         int len;
 2019 {
 2020         u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
 2021         int bytelen, bitlen, i;
 2022 
 2023         /* sanity check */
 2024         if (0 > len || len > 128) {
 2025                 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
 2026                     len);
 2027                 return;
 2028         }
 2029 
 2030         bzero(maskp, sizeof(*maskp));
 2031         bytelen = len / 8;
 2032         bitlen = len % 8;
 2033         for (i = 0; i < bytelen; i++)
 2034                 maskp->s6_addr[i] = 0xff;
 2035         if (bitlen)
 2036                 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
 2037 }
 2038 
 2039 /*
 2040  * return the best address out of the same scope. if no address was
 2041  * found, return the first valid address from designated IF.
 2042  */
 2043 struct in6_ifaddr *
 2044 in6_ifawithifp(ifp, dst)
 2045         struct ifnet *ifp;
 2046         struct in6_addr *dst;
 2047 {
 2048         int dst_scope = in6_addrscope(dst), blen = -1, tlen;
 2049         struct ifaddr *ifa;
 2050         struct in6_ifaddr *besta = 0;
 2051         struct in6_ifaddr *dep[2];      /* last-resort: deprecated */
 2052 
 2053         dep[0] = dep[1] = NULL;
 2054 
 2055         /*
 2056          * We first look for addresses in the same scope.
 2057          * If there is one, return it.
 2058          * If two or more, return one which matches the dst longest.
 2059          * If none, return one of global addresses assigned other ifs.
 2060          */
 2061         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
 2062                 if (ifa->ifa_addr->sa_family != AF_INET6)
 2063                         continue;
 2064                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
 2065                         continue; /* XXX: is there any case to allow anycast? */
 2066                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
 2067                         continue; /* don't use this interface */
 2068                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
 2069                         continue;
 2070                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
 2071                         if (ip6_use_deprecated)
 2072                                 dep[0] = (struct in6_ifaddr *)ifa;
 2073                         continue;
 2074                 }
 2075 
 2076                 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
 2077                         /*
 2078                          * call in6_matchlen() as few as possible
 2079                          */
 2080                         if (besta) {
 2081                                 if (blen == -1)
 2082                                         blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
 2083                                 tlen = in6_matchlen(IFA_IN6(ifa), dst);
 2084                                 if (tlen > blen) {
 2085                                         blen = tlen;
 2086                                         besta = (struct in6_ifaddr *)ifa;
 2087                                 }
 2088                         } else
 2089                                 besta = (struct in6_ifaddr *)ifa;
 2090                 }
 2091         }
 2092         if (besta)
 2093                 return (besta);
 2094 
 2095         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
 2096                 if (ifa->ifa_addr->sa_family != AF_INET6)
 2097                         continue;
 2098                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
 2099                         continue; /* XXX: is there any case to allow anycast? */
 2100                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
 2101                         continue; /* don't use this interface */
 2102                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
 2103                         continue;
 2104                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
 2105                         if (ip6_use_deprecated)
 2106                                 dep[1] = (struct in6_ifaddr *)ifa;
 2107                         continue;
 2108                 }
 2109 
 2110                 return (struct in6_ifaddr *)ifa;
 2111         }
 2112 
 2113         /* use the last-resort values, that are, deprecated addresses */
 2114         if (dep[0])
 2115                 return dep[0];
 2116         if (dep[1])
 2117                 return dep[1];
 2118 
 2119         return NULL;
 2120 }
 2121 
 2122 /*
 2123  * perform DAD when interface becomes IFF_UP.
 2124  */
 2125 void
 2126 in6_if_up(ifp)
 2127         struct ifnet *ifp;
 2128 {
 2129         struct ifaddr *ifa;
 2130         struct in6_ifaddr *ia;
 2131 
 2132         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
 2133                 if (ifa->ifa_addr->sa_family != AF_INET6)
 2134                         continue;
 2135                 ia = (struct in6_ifaddr *)ifa;
 2136                 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
 2137                         /*
 2138                          * The TENTATIVE flag was likely set by hand
 2139                          * beforehand, implicitly indicating the need for DAD.
 2140                          * We may be able to skip the random delay in this
 2141                          * case, but we impose delays just in case.
 2142                          */
 2143                         nd6_dad_start(ifa,
 2144                             arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
 2145                 }
 2146         }
 2147 
 2148         /*
 2149          * special cases, like 6to4, are handled in in6_ifattach
 2150          */
 2151         in6_ifattach(ifp, NULL);
 2152 }
 2153 
 2154 int
 2155 in6if_do_dad(ifp)
 2156         struct ifnet *ifp;
 2157 {
 2158         if ((ifp->if_flags & IFF_LOOPBACK) != 0)
 2159                 return (0);
 2160 
 2161         switch (ifp->if_type) {
 2162 #ifdef IFT_DUMMY
 2163         case IFT_DUMMY:
 2164 #endif
 2165         case IFT_FAITH:
 2166                 /*
 2167                  * These interfaces do not have the IFF_LOOPBACK flag,
 2168                  * but loop packets back.  We do not have to do DAD on such
 2169                  * interfaces.  We should even omit it, because loop-backed
 2170                  * NS would confuse the DAD procedure.
 2171                  */
 2172                 return (0);
 2173         default:
 2174                 /*
 2175                  * Our DAD routine requires the interface up and running.
 2176                  * However, some interfaces can be up before the RUNNING
 2177                  * status.  Additionaly, users may try to assign addresses
 2178                  * before the interface becomes up (or running).
 2179                  * We simply skip DAD in such a case as a work around.
 2180                  * XXX: we should rather mark "tentative" on such addresses,
 2181                  * and do DAD after the interface becomes ready.
 2182                  */
 2183                 if (!((ifp->if_flags & IFF_UP) &&
 2184                     (ifp->if_drv_flags & IFF_DRV_RUNNING)))
 2185                         return (0);
 2186 
 2187                 return (1);
 2188         }
 2189 }
 2190 
 2191 /*
 2192  * Calculate max IPv6 MTU through all the interfaces and store it
 2193  * to in6_maxmtu.
 2194  */
 2195 void
 2196 in6_setmaxmtu()
 2197 {
 2198         unsigned long maxmtu = 0;
 2199         struct ifnet *ifp;
 2200 
 2201         IFNET_RLOCK();
 2202         for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
 2203                 /* this function can be called during ifnet initialization */
 2204                 if (!ifp->if_afdata[AF_INET6])
 2205                         continue;
 2206                 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
 2207                     IN6_LINKMTU(ifp) > maxmtu)
 2208                         maxmtu = IN6_LINKMTU(ifp);
 2209         }
 2210         IFNET_RUNLOCK();
 2211         if (maxmtu)          /* update only when maxmtu is positive */
 2212                 in6_maxmtu = maxmtu;
 2213 }
 2214 
 2215 /*
 2216  * Provide the length of interface identifiers to be used for the link attached
 2217  * to the given interface.  The length should be defined in "IPv6 over
 2218  * xxx-link" document.  Note that address architecture might also define
 2219  * the length for a particular set of address prefixes, regardless of the
 2220  * link type.  As clarified in rfc2462bis, those two definitions should be
 2221  * consistent, and those really are as of August 2004.
 2222  */
 2223 int
 2224 in6_if2idlen(ifp)
 2225         struct ifnet *ifp;
 2226 {
 2227         switch (ifp->if_type) {
 2228         case IFT_ETHER:         /* RFC2464 */
 2229 #ifdef IFT_PROPVIRTUAL
 2230         case IFT_PROPVIRTUAL:   /* XXX: no RFC. treat it as ether */
 2231 #endif
 2232 #ifdef IFT_L2VLAN
 2233         case IFT_L2VLAN:        /* ditto */
 2234 #endif
 2235 #ifdef IFT_IEEE80211
 2236         case IFT_IEEE80211:     /* ditto */
 2237 #endif
 2238 #ifdef IFT_MIP
 2239         case IFT_MIP:   /* ditto */
 2240 #endif
 2241                 return (64);
 2242         case IFT_FDDI:          /* RFC2467 */
 2243                 return (64);
 2244         case IFT_ISO88025:      /* RFC2470 (IPv6 over Token Ring) */
 2245                 return (64);
 2246         case IFT_PPP:           /* RFC2472 */
 2247                 return (64);
 2248         case IFT_ARCNET:        /* RFC2497 */
 2249                 return (64);
 2250         case IFT_FRELAY:        /* RFC2590 */
 2251                 return (64);
 2252         case IFT_IEEE1394:      /* RFC3146 */
 2253                 return (64);
 2254         case IFT_GIF:
 2255                 return (64);    /* draft-ietf-v6ops-mech-v2-07 */
 2256         case IFT_LOOP:
 2257                 return (64);    /* XXX: is this really correct? */
 2258         default:
 2259                 /*
 2260                  * Unknown link type:
 2261                  * It might be controversial to use the today's common constant
 2262                  * of 64 for these cases unconditionally.  For full compliance,
 2263                  * we should return an error in this case.  On the other hand,
 2264                  * if we simply miss the standard for the link type or a new
 2265                  * standard is defined for a new link type, the IFID length
 2266                  * is very likely to be the common constant.  As a compromise,
 2267                  * we always use the constant, but make an explicit notice
 2268                  * indicating the "unknown" case.
 2269                  */
 2270                 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
 2271                 return (64);
 2272         }
 2273 }
 2274 
 2275 void *
 2276 in6_domifattach(ifp)
 2277         struct ifnet *ifp;
 2278 {
 2279         struct in6_ifextra *ext;
 2280 
 2281         ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
 2282         bzero(ext, sizeof(*ext));
 2283 
 2284         ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
 2285             M_IFADDR, M_WAITOK);
 2286         bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
 2287 
 2288         ext->icmp6_ifstat =
 2289             (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
 2290             M_IFADDR, M_WAITOK);
 2291         bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
 2292 
 2293         ext->nd_ifinfo = nd6_ifattach(ifp);
 2294         ext->scope6_id = scope6_ifattach(ifp);
 2295         return ext;
 2296 }
 2297 
 2298 void
 2299 in6_domifdetach(ifp, aux)
 2300         struct ifnet *ifp;
 2301         void *aux;
 2302 {
 2303         struct in6_ifextra *ext = (struct in6_ifextra *)aux;
 2304 
 2305         scope6_ifdetach(ext->scope6_id);
 2306         nd6_ifdetach(ext->nd_ifinfo);
 2307         free(ext->in6_ifstat, M_IFADDR);
 2308         free(ext->icmp6_ifstat, M_IFADDR);
 2309         free(ext, M_IFADDR);
 2310 }
 2311 
 2312 /*
 2313  * Convert sockaddr_in6 to sockaddr_in.  Original sockaddr_in6 must be
 2314  * v4 mapped addr or v4 compat addr
 2315  */
 2316 void
 2317 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
 2318 {
 2319         bzero(sin, sizeof(*sin));
 2320         sin->sin_len = sizeof(struct sockaddr_in);
 2321         sin->sin_family = AF_INET;
 2322         sin->sin_port = sin6->sin6_port;
 2323         sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
 2324 }
 2325 
 2326 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
 2327 void
 2328 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
 2329 {
 2330         bzero(sin6, sizeof(*sin6));
 2331         sin6->sin6_len = sizeof(struct sockaddr_in6);
 2332         sin6->sin6_family = AF_INET6;
 2333         sin6->sin6_port = sin->sin_port;
 2334         sin6->sin6_addr.s6_addr32[0] = 0;
 2335         sin6->sin6_addr.s6_addr32[1] = 0;
 2336         sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
 2337         sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
 2338 }
 2339 
 2340 /* Convert sockaddr_in6 into sockaddr_in. */
 2341 void
 2342 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
 2343 {
 2344         struct sockaddr_in *sin_p;
 2345         struct sockaddr_in6 sin6;
 2346 
 2347         /*
 2348          * Save original sockaddr_in6 addr and convert it
 2349          * to sockaddr_in.
 2350          */
 2351         sin6 = *(struct sockaddr_in6 *)nam;
 2352         sin_p = (struct sockaddr_in *)nam;
 2353         in6_sin6_2_sin(sin_p, &sin6);
 2354 }
 2355 
 2356 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
 2357 void
 2358 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
 2359 {
 2360         struct sockaddr_in *sin_p;
 2361         struct sockaddr_in6 *sin6_p;
 2362 
 2363         MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
 2364                M_WAITOK);
 2365         sin_p = (struct sockaddr_in *)*nam;
 2366         in6_sin_2_v4mapsin6(sin_p, sin6_p);
 2367         FREE(*nam, M_SONAME);
 2368         *nam = (struct sockaddr *)sin6_p;
 2369 }

Cache object: 88f40f8329ca6a3af422465cfd668239


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