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


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

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

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

    1 /*      $FreeBSD$       */
    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_IPMADDR, "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         if (nrt) {
  170                 RT_LOCK(nrt);
  171                 /*
  172                  * Make sure rt_ifa be equal to IFA, the second argument of
  173                  * the function.  We need this because when we refer to
  174                  * rt_ifa->ia6_flags in ip6_input, we assume that the rt_ifa
  175                  * points to the address instead of the loopback address.
  176                  */
  177                 if (cmd == RTM_ADD && ifa != nrt->rt_ifa) {
  178                         IFAFREE(nrt->rt_ifa);
  179                         IFAREF(ifa);
  180                         nrt->rt_ifa = ifa;
  181                 }
  182 
  183                 /*
  184                  * Report the addition/removal of the address to the routing
  185                  * socket.
  186                  *
  187                  * XXX: since we called rtinit for a p2p interface with a
  188                  *      destination, we end up reporting twice in such a case.
  189                  *      Should we rather omit the second report?
  190                  */
  191                 rt_newaddrmsg(cmd, ifa, e, nrt);
  192                 if (cmd == RTM_DELETE) {
  193                         rtfree(nrt);
  194                 } else {
  195                         /* the cmd must be RTM_ADD here */
  196                         RT_REMREF(nrt);
  197                         RT_UNLOCK(nrt);
  198                 }
  199         }
  200 }
  201 
  202 /*
  203  * Add ownaddr as loopback rtentry.  We previously add the route only if
  204  * necessary (ex. on a p2p link).  However, since we now manage addresses
  205  * separately from prefixes, we should always add the route.  We can't
  206  * rely on the cloning mechanism from the corresponding interface route
  207  * any more.
  208  */
  209 void
  210 in6_ifaddloop(struct ifaddr *ifa)
  211 {
  212         struct rtentry *rt;
  213         int need_loop;
  214 
  215         /* If there is no loopback entry, allocate one. */
  216         rt = rtalloc1(ifa->ifa_addr, 0, 0);
  217         need_loop = (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
  218             (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0);
  219         if (rt)
  220                 rtfree(rt);
  221         if (need_loop)
  222                 in6_ifloop_request(RTM_ADD, ifa);
  223 }
  224 
  225 /*
  226  * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
  227  * if it exists.
  228  */
  229 void
  230 in6_ifremloop(struct ifaddr *ifa)
  231 {
  232         struct in6_ifaddr *ia;
  233         struct rtentry *rt;
  234         int ia_count = 0;
  235 
  236         /*
  237          * Some of BSD variants do not remove cloned routes
  238          * from an interface direct route, when removing the direct route
  239          * (see comments in net/net_osdep.h).  Even for variants that do remove
  240          * cloned routes, they could fail to remove the cloned routes when
  241          * we handle multple addresses that share a common prefix.
  242          * So, we should remove the route corresponding to the deleted address
  243          * regardless of the result of in6_is_ifloop_auto().
  244          */
  245 
  246         /*
  247          * Delete the entry only if exact one ifa exists.  More than one ifa
  248          * can exist if we assign a same single address to multiple
  249          * (probably p2p) interfaces.
  250          * XXX: we should avoid such a configuration in IPv6...
  251          */
  252         for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
  253                 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
  254                         ia_count++;
  255                         if (ia_count > 1)
  256                                 break;
  257                 }
  258         }
  259 
  260         if (ia_count == 1) {
  261                 /*
  262                  * Before deleting, check if a corresponding loopbacked host
  263                  * route surely exists.  With this check, we can avoid to
  264                  * delete an interface direct route whose destination is same
  265                  * as the address being removed.  This can happen when removing
  266                  * a subnet-router anycast address on an interface attahced
  267                  * to a shared medium.
  268                  */
  269                 rt = rtalloc1(ifa->ifa_addr, 0, 0);
  270                 if (rt != NULL) {
  271                         if ((rt->rt_flags & RTF_HOST) != 0 &&
  272                             (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
  273                                 rtfree(rt);
  274                                 in6_ifloop_request(RTM_DELETE, ifa);
  275                         } else
  276                                 RT_UNLOCK(rt);
  277                 }
  278         }
  279 }
  280 
  281 int
  282 in6_mask2len(mask, lim0)
  283         struct in6_addr *mask;
  284         u_char *lim0;
  285 {
  286         int x = 0, y;
  287         u_char *lim = lim0, *p;
  288 
  289         /* ignore the scope_id part */
  290         if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
  291                 lim = (u_char *)mask + sizeof(*mask);
  292         for (p = (u_char *)mask; p < lim; x++, p++) {
  293                 if (*p != 0xff)
  294                         break;
  295         }
  296         y = 0;
  297         if (p < lim) {
  298                 for (y = 0; y < 8; y++) {
  299                         if ((*p & (0x80 >> y)) == 0)
  300                                 break;
  301                 }
  302         }
  303 
  304         /*
  305          * when the limit pointer is given, do a stricter check on the
  306          * remaining bits.
  307          */
  308         if (p < lim) {
  309                 if (y != 0 && (*p & (0x00ff >> y)) != 0)
  310                         return (-1);
  311                 for (p = p + 1; p < lim; p++)
  312                         if (*p != 0)
  313                                 return (-1);
  314         }
  315 
  316         return x * 8 + y;
  317 }
  318 
  319 #define ifa2ia6(ifa)    ((struct in6_ifaddr *)(ifa))
  320 #define ia62ifa(ia6)    (&((ia6)->ia_ifa))
  321 
  322 int
  323 in6_control(so, cmd, data, ifp, td)
  324         struct  socket *so;
  325         u_long cmd;
  326         caddr_t data;
  327         struct ifnet *ifp;
  328         struct thread *td;
  329 {
  330         struct  in6_ifreq *ifr = (struct in6_ifreq *)data;
  331         struct  in6_ifaddr *ia = NULL;
  332         struct  in6_aliasreq *ifra = (struct in6_aliasreq *)data;
  333         int privileged;
  334 
  335         privileged = 0;
  336         if (td == NULL || !suser(td))
  337                 privileged++;
  338 
  339         switch (cmd) {
  340         case SIOCGETSGCNT_IN6:
  341         case SIOCGETMIFCNT_IN6:
  342                 return (mrt6_ioctl(cmd, data));
  343         }
  344 
  345         switch(cmd) {
  346         case SIOCAADDRCTL_POLICY:
  347         case SIOCDADDRCTL_POLICY:
  348                 if (!privileged)
  349                         return (EPERM);
  350                 return (in6_src_ioctl(cmd, data));
  351         }
  352 
  353         if (ifp == NULL)
  354                 return (EOPNOTSUPP);
  355 
  356         switch (cmd) {
  357         case SIOCSNDFLUSH_IN6:
  358         case SIOCSPFXFLUSH_IN6:
  359         case SIOCSRTRFLUSH_IN6:
  360         case SIOCSDEFIFACE_IN6:
  361         case SIOCSIFINFO_FLAGS:
  362                 if (!privileged)
  363                         return (EPERM);
  364                 /* FALLTHROUGH */
  365         case OSIOCGIFINFO_IN6:
  366         case SIOCGIFINFO_IN6:
  367         case SIOCGDRLST_IN6:
  368         case SIOCGPRLST_IN6:
  369         case SIOCGNBRINFO_IN6:
  370         case SIOCGDEFIFACE_IN6:
  371                 return (nd6_ioctl(cmd, data, ifp));
  372         }
  373 
  374         switch (cmd) {
  375         case SIOCSIFPREFIX_IN6:
  376         case SIOCDIFPREFIX_IN6:
  377         case SIOCAIFPREFIX_IN6:
  378         case SIOCCIFPREFIX_IN6:
  379         case SIOCSGIFPREFIX_IN6:
  380         case SIOCGIFPREFIX_IN6:
  381                 log(LOG_NOTICE,
  382                     "prefix ioctls are now invalidated. "
  383                     "please use ifconfig.\n");
  384                 return (EOPNOTSUPP);
  385         }
  386 
  387         switch (cmd) {
  388         case SIOCSSCOPE6:
  389                 if (!privileged)
  390                         return (EPERM);
  391                 return (scope6_set(ifp,
  392                     (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
  393         case SIOCGSCOPE6:
  394                 return (scope6_get(ifp,
  395                     (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
  396         case SIOCGSCOPE6DEF:
  397                 return (scope6_get_default((struct scope6_id *)
  398                     ifr->ifr_ifru.ifru_scope_id));
  399         }
  400 
  401         switch (cmd) {
  402         case SIOCALIFADDR:
  403         case SIOCDLIFADDR:
  404                 if (!privileged)
  405                         return (EPERM);
  406                 /* FALLTHROUGH */
  407         case SIOCGLIFADDR:
  408                 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
  409         }
  410 
  411         /*
  412          * Find address for this interface, if it exists.
  413          */
  414         if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
  415                 struct sockaddr_in6 *sa6 =
  416                         (struct sockaddr_in6 *)&ifra->ifra_addr;
  417 
  418                 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
  419                         if (sa6->sin6_addr.s6_addr16[1] == 0) {
  420                                 /* link ID is not embedded by the user */
  421                                 sa6->sin6_addr.s6_addr16[1] =
  422                                     htons(ifp->if_index);
  423                         } else if (sa6->sin6_addr.s6_addr16[1] !=
  424                             htons(ifp->if_index)) {
  425                                 return (EINVAL);        /* link ID contradicts */
  426                         }
  427                         if (sa6->sin6_scope_id) {
  428                                 if (sa6->sin6_scope_id !=
  429                                     (u_int32_t)ifp->if_index)
  430                                         return (EINVAL);
  431                                 sa6->sin6_scope_id = 0; /* XXX: good way? */
  432                         }
  433                 }
  434                 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
  435         }
  436 
  437         switch (cmd) {
  438         case SIOCSIFADDR_IN6:
  439         case SIOCSIFDSTADDR_IN6:
  440         case SIOCSIFNETMASK_IN6:
  441                 /*
  442                  * Since IPv6 allows a node to assign multiple addresses
  443                  * on a single interface, SIOCSIFxxx ioctls are not suitable
  444                  * and should be unused.
  445                  */
  446                 /* we decided to obsolete this command (20000704) */
  447                 return (EINVAL);
  448 
  449         case SIOCDIFADDR_IN6:
  450                 /*
  451                  * for IPv4, we look for existing in_ifaddr here to allow
  452                  * "ifconfig if0 delete" to remove first IPv4 address on the
  453                  * interface.  For IPv6, as the spec allow multiple interface
  454                  * address from the day one, we consider "remove the first one"
  455                  * semantics to be not preferable.
  456                  */
  457                 if (ia == NULL)
  458                         return (EADDRNOTAVAIL);
  459                 /* FALLTHROUGH */
  460         case SIOCAIFADDR_IN6:
  461                 /*
  462                  * We always require users to specify a valid IPv6 address for
  463                  * the corresponding operation.
  464                  */
  465                 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
  466                     ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
  467                         return (EAFNOSUPPORT);
  468                 if (!privileged)
  469                         return (EPERM);
  470 
  471                 break;
  472 
  473         case SIOCGIFADDR_IN6:
  474                 /* This interface is basically deprecated. use SIOCGIFCONF. */
  475                 /* FALLTHROUGH */
  476         case SIOCGIFAFLAG_IN6:
  477         case SIOCGIFNETMASK_IN6:
  478         case SIOCGIFDSTADDR_IN6:
  479         case SIOCGIFALIFETIME_IN6:
  480                 /* must think again about its semantics */
  481                 if (ia == NULL)
  482                         return (EADDRNOTAVAIL);
  483                 break;
  484         case SIOCSIFALIFETIME_IN6:
  485             {
  486                 struct in6_addrlifetime *lt;
  487 
  488                 if (!privileged)
  489                         return (EPERM);
  490                 if (ia == NULL)
  491                         return (EADDRNOTAVAIL);
  492                 /* sanity for overflow - beware unsigned */
  493                 lt = &ifr->ifr_ifru.ifru_lifetime;
  494                 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
  495                     lt->ia6t_vltime + time_second < time_second) {
  496                         return EINVAL;
  497                 }
  498                 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
  499                     lt->ia6t_pltime + time_second < time_second) {
  500                         return EINVAL;
  501                 }
  502                 break;
  503             }
  504         }
  505 
  506         switch (cmd) {
  507 
  508         case SIOCGIFADDR_IN6:
  509                 ifr->ifr_addr = ia->ia_addr;
  510                 break;
  511 
  512         case SIOCGIFDSTADDR_IN6:
  513                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
  514                         return (EINVAL);
  515                 /*
  516                  * XXX: should we check if ifa_dstaddr is NULL and return
  517                  * an error?
  518                  */
  519                 ifr->ifr_dstaddr = ia->ia_dstaddr;
  520                 break;
  521 
  522         case SIOCGIFNETMASK_IN6:
  523                 ifr->ifr_addr = ia->ia_prefixmask;
  524                 break;
  525 
  526         case SIOCGIFAFLAG_IN6:
  527                 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
  528                 break;
  529 
  530         case SIOCGIFSTAT_IN6:
  531                 if (ifp == NULL)
  532                         return EINVAL;
  533                 bzero(&ifr->ifr_ifru.ifru_stat,
  534                     sizeof(ifr->ifr_ifru.ifru_stat));
  535                 ifr->ifr_ifru.ifru_stat =
  536                     *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
  537                 break;
  538 
  539         case SIOCGIFSTAT_ICMP6:
  540                 if (ifp == NULL)
  541                         return EINVAL;
  542                 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
  543                     sizeof(ifr->ifr_ifru.ifru_icmp6stat));
  544                 ifr->ifr_ifru.ifru_icmp6stat =
  545                     *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
  546                 break;
  547 
  548         case SIOCGIFALIFETIME_IN6:
  549                 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
  550                 break;
  551 
  552         case SIOCSIFALIFETIME_IN6:
  553                 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
  554                 /* for sanity */
  555                 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
  556                         ia->ia6_lifetime.ia6t_expire =
  557                                 time_second + ia->ia6_lifetime.ia6t_vltime;
  558                 } else
  559                         ia->ia6_lifetime.ia6t_expire = 0;
  560                 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
  561                         ia->ia6_lifetime.ia6t_preferred =
  562                                 time_second + ia->ia6_lifetime.ia6t_pltime;
  563                 } else
  564                         ia->ia6_lifetime.ia6t_preferred = 0;
  565                 break;
  566 
  567         case SIOCAIFADDR_IN6:
  568         {
  569                 int i, error = 0;
  570                 struct nd_prefix pr0, *pr;
  571 
  572                 /*
  573                  * first, make or update the interface address structure,
  574                  * and link it to the list.
  575                  */
  576                 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
  577                         return (error);
  578 
  579                 /*
  580                  * then, make the prefix on-link on the interface.
  581                  * XXX: we'd rather create the prefix before the address, but
  582                  * we need at least one address to install the corresponding
  583                  * interface route, so we configure the address first.
  584                  */
  585 
  586                 /*
  587                  * convert mask to prefix length (prefixmask has already
  588                  * been validated in in6_update_ifa().
  589                  */
  590                 bzero(&pr0, sizeof(pr0));
  591                 pr0.ndpr_ifp = ifp;
  592                 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
  593                     NULL);
  594                 if (pr0.ndpr_plen == 128) {
  595                         break;  /* we don't need to install a host route. */
  596                 }
  597                 pr0.ndpr_prefix = ifra->ifra_addr;
  598                 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
  599                 /* apply the mask for safety. */
  600                 for (i = 0; i < 4; i++) {
  601                         pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
  602                             ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
  603                 }
  604                 /*
  605                  * XXX: since we don't have an API to set prefix (not address)
  606                  * lifetimes, we just use the same lifetimes as addresses.
  607                  * The (temporarily) installed lifetimes can be overridden by
  608                  * later advertised RAs (when accept_rtadv is non 0), which is
  609                  * an intended behavior.
  610                  */
  611                 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
  612                 pr0.ndpr_raf_auto =
  613                     ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
  614                 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
  615                 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
  616 
  617                 /* add the prefix if not yet. */
  618                 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
  619                         /*
  620                          * nd6_prelist_add will install the corresponding
  621                          * interface route.
  622                          */
  623                         if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
  624                                 return (error);
  625                         if (pr == NULL) {
  626                                 log(LOG_ERR, "nd6_prelist_add succeeded but "
  627                                     "no prefix\n");
  628                                 return (EINVAL); /* XXX panic here? */
  629                         }
  630                 }
  631                 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
  632                     == NULL) {
  633                         /* XXX: this should not happen! */
  634                         log(LOG_ERR, "in6_control: addition succeeded, but"
  635                             " no ifaddr\n");
  636                 } else {
  637                         if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
  638                             ia->ia6_ndpr == NULL) { /* new autoconfed addr */
  639                                 ia->ia6_ndpr = pr;
  640                                 pr->ndpr_refcnt++;
  641 
  642                                 /*
  643                                  * If this is the first autoconf address from
  644                                  * the prefix, create a temporary address
  645                                  * as well (when specified).
  646                                  */
  647                                 if (ip6_use_tempaddr &&
  648                                     pr->ndpr_refcnt == 1) {
  649                                         int e;
  650                                         if ((e = in6_tmpifadd(ia, 1)) != 0) {
  651                                                 log(LOG_NOTICE, "in6_control: "
  652                                                     "failed to create a "
  653                                                     "temporary address, "
  654                                                     "errno=%d\n", e);
  655                                         }
  656                                 }
  657                         }
  658 
  659                         /*
  660                          * this might affect the status of autoconfigured
  661                          * addresses, that is, this address might make
  662                          * other addresses detached.
  663                          */
  664                         pfxlist_onlink_check();
  665                 }
  666                 if (error == 0 && ia)
  667                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
  668                 break;
  669         }
  670 
  671         case SIOCDIFADDR_IN6:
  672         {
  673                 int i = 0;
  674                 struct nd_prefix pr0, *pr;
  675 
  676                 /*
  677                  * If the address being deleted is the only one that owns
  678                  * the corresponding prefix, expire the prefix as well.
  679                  * XXX: theoretically, we don't have to worry about such
  680                  * relationship, since we separate the address management
  681                  * and the prefix management.  We do this, however, to provide
  682                  * as much backward compatibility as possible in terms of
  683                  * the ioctl operation.
  684                  */
  685                 bzero(&pr0, sizeof(pr0));
  686                 pr0.ndpr_ifp = ifp;
  687                 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
  688                                              NULL);
  689                 if (pr0.ndpr_plen == 128)
  690                         goto purgeaddr;
  691                 pr0.ndpr_prefix = ia->ia_addr;
  692                 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
  693                 for (i = 0; i < 4; i++) {
  694                         pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
  695                                 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
  696                 }
  697                 /*
  698                  * The logic of the following condition is a bit complicated.
  699                  * We expire the prefix when
  700                  * 1. the address obeys autoconfiguration and it is the
  701                  *    only owner of the associated prefix, or
  702                  * 2. the address does not obey autoconf and there is no
  703                  *    other owner of the prefix.
  704                  */
  705                 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
  706                     (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
  707                       pr->ndpr_refcnt == 1) ||
  708                      ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
  709                       pr->ndpr_refcnt == 0))) {
  710                         pr->ndpr_expire = 1; /* XXX: just for expiration */
  711                 }
  712 
  713           purgeaddr:
  714                 in6_purgeaddr(&ia->ia_ifa);
  715                 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
  716                 break;
  717         }
  718 
  719         default:
  720                 if (ifp == NULL || ifp->if_ioctl == 0)
  721                         return (EOPNOTSUPP);
  722                 return ((*ifp->if_ioctl)(ifp, cmd, data));
  723         }
  724 
  725         return (0);
  726 }
  727 
  728 /*
  729  * Update parameters of an IPv6 interface address.
  730  * If necessary, a new entry is created and linked into address chains.
  731  * This function is separated from in6_control().
  732  * XXX: should this be performed under splnet()?
  733  */
  734 int
  735 in6_update_ifa(ifp, ifra, ia)
  736         struct ifnet *ifp;
  737         struct in6_aliasreq *ifra;
  738         struct in6_ifaddr *ia;
  739 {
  740         int error = 0, hostIsNew = 0, plen = -1;
  741         struct in6_ifaddr *oia;
  742         struct sockaddr_in6 dst6;
  743         struct in6_addrlifetime *lt;
  744         struct in6_multi_mship *imm;
  745 
  746         /* Validate parameters */
  747         if (ifp == NULL || ifra == NULL) /* this maybe redundant */
  748                 return (EINVAL);
  749 
  750         /*
  751          * The destination address for a p2p link must have a family
  752          * of AF_UNSPEC or AF_INET6.
  753          */
  754         if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
  755             ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
  756             ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
  757                 return (EAFNOSUPPORT);
  758         /*
  759          * validate ifra_prefixmask.  don't check sin6_family, netmask
  760          * does not carry fields other than sin6_len.
  761          */
  762         if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
  763                 return (EINVAL);
  764         /*
  765          * Because the IPv6 address architecture is classless, we require
  766          * users to specify a (non 0) prefix length (mask) for a new address.
  767          * We also require the prefix (when specified) mask is valid, and thus
  768          * reject a non-consecutive mask.
  769          */
  770         if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
  771                 return (EINVAL);
  772         if (ifra->ifra_prefixmask.sin6_len != 0) {
  773                 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
  774                     (u_char *)&ifra->ifra_prefixmask +
  775                     ifra->ifra_prefixmask.sin6_len);
  776                 if (plen <= 0)
  777                         return (EINVAL);
  778         } else {
  779                 /*
  780                  * In this case, ia must not be NULL.  We just use its prefix
  781                  * length.
  782                  */
  783                 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
  784         }
  785         /*
  786          * If the destination address on a p2p interface is specified,
  787          * and the address is a scoped one, validate/set the scope
  788          * zone identifier.
  789          */
  790         dst6 = ifra->ifra_dstaddr;
  791         if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
  792             (dst6.sin6_family == AF_INET6)) {
  793                 u_int32_t zoneid;
  794 
  795                 if ((error = in6_recoverscope(&dst6,
  796                     &ifra->ifra_dstaddr.sin6_addr, ifp)) != 0)
  797                         return (error);
  798                 if (in6_addr2zoneid(ifp, &dst6.sin6_addr, &zoneid))
  799                         return (EINVAL);
  800                 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
  801                         dst6.sin6_scope_id = zoneid;
  802                 else if (dst6.sin6_scope_id != zoneid)
  803                         return (EINVAL); /* scope ID mismatch. */
  804                 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
  805                     != 0)
  806                         return (error);
  807                 dst6.sin6_scope_id = 0; /* XXX */
  808         }
  809         /*
  810          * The destination address can be specified only for a p2p or a
  811          * loopback interface.  If specified, the corresponding prefix length
  812          * must be 128.
  813          */
  814         if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
  815                 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
  816                         /* XXX: noisy message */
  817                         nd6log((LOG_INFO, "in6_update_ifa: a destination can "
  818                             "be specified for a p2p or a loopback IF only\n"));
  819                         return (EINVAL);
  820                 }
  821                 if (plen != 128) {
  822                         nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
  823                             "be 128 when dstaddr is specified\n"));
  824                         return (EINVAL);
  825                 }
  826         }
  827         /* lifetime consistency check */
  828         lt = &ifra->ifra_lifetime;
  829         if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
  830             && lt->ia6t_vltime + time_second < time_second) {
  831                 return EINVAL;
  832         }
  833         if (lt->ia6t_vltime == 0) {
  834                 /*
  835                  * the following log might be noisy, but this is a typical
  836                  * configuration mistake or a tool's bug.
  837                  */
  838                 nd6log((LOG_INFO,
  839                     "in6_update_ifa: valid lifetime is 0 for %s\n",
  840                     ip6_sprintf(&ifra->ifra_addr.sin6_addr)));
  841         }
  842         if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
  843             && lt->ia6t_pltime + time_second < time_second) {
  844                 return EINVAL;
  845         }
  846 
  847         /*
  848          * If this is a new address, allocate a new ifaddr and link it
  849          * into chains.
  850          */
  851         if (ia == NULL) {
  852                 hostIsNew = 1;
  853                 /*
  854                  * When in6_update_ifa() is called in a process of a received
  855                  * RA, it is called under an interrupt context.  So, we should
  856                  * call malloc with M_NOWAIT.
  857                  */
  858                 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
  859                     M_NOWAIT);
  860                 if (ia == NULL)
  861                         return (ENOBUFS);
  862                 bzero((caddr_t)ia, sizeof(*ia));
  863                 LIST_INIT(&ia->ia6_memberships);
  864                 /* Initialize the address and masks */
  865                 IFA_LOCK_INIT(&ia->ia_ifa);
  866                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
  867                 ia->ia_addr.sin6_family = AF_INET6;
  868                 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
  869                 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
  870                         /*
  871                          * XXX: some functions expect that ifa_dstaddr is not
  872                          * NULL for p2p interfaces.
  873                          */
  874                         ia->ia_ifa.ifa_dstaddr =
  875                             (struct sockaddr *)&ia->ia_dstaddr;
  876                 } else {
  877                         ia->ia_ifa.ifa_dstaddr = NULL;
  878                 }
  879                 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
  880 
  881                 ia->ia_ifp = ifp;
  882                 if ((oia = in6_ifaddr) != NULL) {
  883                         for ( ; oia->ia_next; oia = oia->ia_next)
  884                                 continue;
  885                         oia->ia_next = ia;
  886                 } else
  887                         in6_ifaddr = ia;
  888 
  889                 ia->ia_ifa.ifa_refcnt = 1;
  890                 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
  891         }
  892 
  893         /* set prefix mask */
  894         if (ifra->ifra_prefixmask.sin6_len) {
  895                 /*
  896                  * We prohibit changing the prefix length of an existing
  897                  * address, because
  898                  * + such an operation should be rare in IPv6, and
  899                  * + the operation would confuse prefix management.
  900                  */
  901                 if (ia->ia_prefixmask.sin6_len &&
  902                     in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
  903                         nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
  904                             " existing (%s) address should not be changed\n",
  905                             ip6_sprintf(&ia->ia_addr.sin6_addr)));
  906                         error = EINVAL;
  907                         goto unlink;
  908                 }
  909                 ia->ia_prefixmask = ifra->ifra_prefixmask;
  910         }
  911 
  912         /*
  913          * If a new destination address is specified, scrub the old one and
  914          * install the new destination.  Note that the interface must be
  915          * p2p or loopback (see the check above.)
  916          */
  917         if (dst6.sin6_family == AF_INET6 &&
  918             !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
  919                 int e;
  920 
  921                 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
  922                     (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
  923                         nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
  924                             "a route to the old destination: %s\n",
  925                             ip6_sprintf(&ia->ia_addr.sin6_addr)));
  926                         /* proceed anyway... */
  927                 } else
  928                         ia->ia_flags &= ~IFA_ROUTE;
  929                 ia->ia_dstaddr = dst6;
  930         }
  931 
  932         /* reset the interface and routing table appropriately. */
  933         if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
  934                 goto unlink;
  935 
  936         /*
  937          * We are done if we have simply modified an existing address.
  938          */
  939         if (!hostIsNew)
  940                 return (error);
  941 
  942         /*
  943          * Beyond this point, we should call in6_purgeaddr upon an error,
  944          * not just go to unlink.
  945          */
  946 
  947         if ((ifp->if_flags & IFF_MULTICAST) != 0) {
  948                 struct sockaddr_in6 mltaddr, mltmask;
  949                 struct in6_multi *in6m;
  950 
  951                 /* join solicited multicast addr for new host id */
  952                 struct in6_addr llsol;
  953 
  954                 bzero(&llsol, sizeof(struct in6_addr));
  955                 llsol.s6_addr16[0] = htons(0xff02);
  956                 llsol.s6_addr16[1] = htons(ifp->if_index);
  957                 llsol.s6_addr32[1] = 0;
  958                 llsol.s6_addr32[2] = htonl(1);
  959                 llsol.s6_addr32[3] =
  960                     ifra->ifra_addr.sin6_addr.s6_addr32[3];
  961                 llsol.s6_addr8[12] = 0xff;
  962                 imm = in6_joingroup(ifp, &llsol, &error);
  963                 if (imm == NULL) {
  964                         nd6log((LOG_WARNING,
  965                             "in6_update_ifa: addmulti failed for "
  966                             "%s on %s (errno=%d)\n",
  967                             ip6_sprintf(&llsol), if_name(ifp),
  968                             error));
  969                         in6_purgeaddr((struct ifaddr *)ia);
  970                         return (error);
  971                 }
  972                 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
  973 
  974                 bzero(&mltmask, sizeof(mltmask));
  975                 mltmask.sin6_len = sizeof(struct sockaddr_in6);
  976                 mltmask.sin6_family = AF_INET6;
  977                 mltmask.sin6_addr = in6mask32;
  978 
  979                 /*
  980                  * join link-local all-nodes address
  981                  */
  982                 bzero(&mltaddr, sizeof(mltaddr));
  983                 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
  984                 mltaddr.sin6_family = AF_INET6;
  985                 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
  986                 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
  987 
  988                 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
  989                 if (in6m == NULL) {
  990                         rtrequest(RTM_ADD,
  991                                   (struct sockaddr *)&mltaddr,
  992                                   (struct sockaddr *)&ia->ia_addr,
  993                                   (struct sockaddr *)&mltmask,
  994                                   RTF_UP|RTF_CLONING,  /* xxx */
  995                                   (struct rtentry **)0);
  996                 }
  997                 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error);
  998                 if (imm == NULL) {
  999                         nd6log((LOG_WARNING,
 1000                             "in6_update_ifa: addmulti failed for "
 1001                             "%s on %s (errno=%d)\n",
 1002                             ip6_sprintf(&mltaddr.sin6_addr),
 1003                             if_name(ifp), error));
 1004                         in6_purgeaddr((struct ifaddr *)ia);
 1005                         return (error);
 1006                 }
 1007                 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
 1008 
 1009                 /*
 1010                  * join node information group address
 1011                  */
 1012 #define hostnamelen     strlen(hostname)
 1013                 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
 1014                     == 0) {
 1015                         imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error);
 1016                         if (imm == NULL) {
 1017                                 nd6log((LOG_WARNING, "in6_update_ifa: "
 1018                                     "addmulti failed for "
 1019                                     "%s on %s (errno=%d)\n",
 1020                                     ip6_sprintf(&mltaddr.sin6_addr),
 1021                                     if_name(ifp), error));
 1022                                 /* XXX not very fatal, go on... */
 1023                         } else {
 1024                                 LIST_INSERT_HEAD(&ia->ia6_memberships,
 1025                                     imm, i6mm_chain);
 1026                         }
 1027                 }
 1028 #undef hostnamelen
 1029 
 1030                 /*
 1031                  * join node-local all-nodes address, on loopback.
 1032                  * XXX: since "node-local" is obsoleted by interface-local,
 1033                  *      we have to join the group on every interface with
 1034                  *      some interface-boundary restriction.
 1035                  */
 1036                 if (ifp->if_flags & IFF_LOOPBACK) {
 1037                         struct in6_ifaddr *ia_loop;
 1038 
 1039                         struct in6_addr loop6 = in6addr_loopback;
 1040                         ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
 1041 
 1042                         mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
 1043 
 1044                         IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
 1045                         if (in6m == NULL && ia_loop != NULL) {
 1046                                 rtrequest(RTM_ADD,
 1047                                           (struct sockaddr *)&mltaddr,
 1048                                           (struct sockaddr *)&ia_loop->ia_addr,
 1049                                           (struct sockaddr *)&mltmask,
 1050                                           RTF_UP,
 1051                                           (struct rtentry **)0);
 1052                         }
 1053                         imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error);
 1054                         if (imm == NULL) {
 1055                                 nd6log((LOG_WARNING, "in6_update_ifa: "
 1056                                     "addmulti failed for %s on %s "
 1057                                     "(errno=%d)\n",
 1058                                     ip6_sprintf(&mltaddr.sin6_addr),
 1059                                     if_name(ifp), error));
 1060                                 goto cleanup;
 1061                         }
 1062                         LIST_INSERT_HEAD(&ia->ia6_memberships, imm,
 1063                             i6mm_chain);
 1064                 }
 1065         }
 1066 
 1067         ia->ia6_flags = ifra->ifra_flags;
 1068         ia->ia6_flags &= ~IN6_IFF_DUPLICATED;   /*safety*/
 1069         ia->ia6_flags &= ~IN6_IFF_NODAD;        /* Mobile IPv6 */
 1070 
 1071         ia->ia6_lifetime = ifra->ifra_lifetime;
 1072         /* for sanity */
 1073         if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
 1074                 ia->ia6_lifetime.ia6t_expire =
 1075                         time_second + ia->ia6_lifetime.ia6t_vltime;
 1076         } else
 1077                 ia->ia6_lifetime.ia6t_expire = 0;
 1078         if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
 1079                 ia->ia6_lifetime.ia6t_preferred =
 1080                         time_second + ia->ia6_lifetime.ia6t_pltime;
 1081         } else
 1082                 ia->ia6_lifetime.ia6t_preferred = 0;
 1083 
 1084         /*
 1085          * Perform DAD, if needed.
 1086          * XXX It may be of use, if we can administratively
 1087          * disable DAD.
 1088          */
 1089         if (in6if_do_dad(ifp) && (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
 1090                 ia->ia6_flags |= IN6_IFF_TENTATIVE;
 1091                 nd6_dad_start((struct ifaddr *)ia, NULL);
 1092         }
 1093 
 1094         return (error);
 1095 
 1096   unlink:
 1097         /*
 1098          * XXX: if a change of an existing address failed, keep the entry
 1099          * anyway.
 1100          */
 1101         if (hostIsNew)
 1102                 in6_unlink_ifa(ia, ifp);
 1103         return (error);
 1104 
 1105   cleanup:
 1106         in6_purgeaddr(&ia->ia_ifa);
 1107         return error;
 1108 }
 1109 
 1110 void
 1111 in6_purgeaddr(ifa)
 1112         struct ifaddr *ifa;
 1113 {
 1114         struct ifnet *ifp = ifa->ifa_ifp;
 1115         struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
 1116         struct in6_multi_mship *imm;
 1117 
 1118         /* stop DAD processing */
 1119         nd6_dad_stop(ifa);
 1120 
 1121         /*
 1122          * delete route to the destination of the address being purged.
 1123          * The interface must be p2p or loopback in this case.
 1124          */
 1125         if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
 1126                 int e;
 1127 
 1128                 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
 1129                     != 0) {
 1130                         log(LOG_ERR, "in6_purgeaddr: failed to remove "
 1131                             "a route to the p2p destination: %s on %s, "
 1132                             "errno=%d\n",
 1133                             ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
 1134                             e);
 1135                         /* proceed anyway... */
 1136                 } else
 1137                         ia->ia_flags &= ~IFA_ROUTE;
 1138         }
 1139 
 1140         /* Remove ownaddr's loopback rtentry, if it exists. */
 1141         in6_ifremloop(&(ia->ia_ifa));
 1142 
 1143         /*
 1144          * leave from multicast groups we have joined for the interface
 1145          */
 1146         while ((imm = ia->ia6_memberships.lh_first) != NULL) {
 1147                 LIST_REMOVE(imm, i6mm_chain);
 1148                 in6_leavegroup(imm);
 1149         }
 1150 
 1151         in6_unlink_ifa(ia, ifp);
 1152 }
 1153 
 1154 static void
 1155 in6_unlink_ifa(ia, ifp)
 1156         struct in6_ifaddr *ia;
 1157         struct ifnet *ifp;
 1158 {
 1159         struct in6_ifaddr *oia;
 1160         int     s = splnet();
 1161 
 1162         TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
 1163 
 1164         oia = ia;
 1165         if (oia == (ia = in6_ifaddr))
 1166                 in6_ifaddr = ia->ia_next;
 1167         else {
 1168                 while (ia->ia_next && (ia->ia_next != oia))
 1169                         ia = ia->ia_next;
 1170                 if (ia->ia_next)
 1171                         ia->ia_next = oia->ia_next;
 1172                 else {
 1173                         /* search failed */
 1174                         printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
 1175                 }
 1176         }
 1177 
 1178         /*
 1179          * When an autoconfigured address is being removed, release the
 1180          * reference to the base prefix.  Also, since the release might
 1181          * affect the status of other (detached) addresses, call
 1182          * pfxlist_onlink_check().
 1183          */
 1184         if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
 1185                 if (oia->ia6_ndpr == NULL) {
 1186                         nd6log((LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
 1187                             "%p has no prefix\n", oia));
 1188                 } else {
 1189                         oia->ia6_ndpr->ndpr_refcnt--;
 1190                         oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
 1191                         oia->ia6_ndpr = NULL;
 1192                 }
 1193 
 1194                 pfxlist_onlink_check();
 1195         }
 1196 
 1197         /*
 1198          * release another refcnt for the link from in6_ifaddr.
 1199          * Note that we should decrement the refcnt at least once for all *BSD.
 1200          */
 1201         IFAFREE(&oia->ia_ifa);
 1202 
 1203         splx(s);
 1204 }
 1205 
 1206 void
 1207 in6_purgeif(ifp)
 1208         struct ifnet *ifp;
 1209 {
 1210         struct ifaddr *ifa, *nifa;
 1211 
 1212         for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
 1213                 nifa = TAILQ_NEXT(ifa, ifa_list);
 1214                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1215                         continue;
 1216                 in6_purgeaddr(ifa);
 1217         }
 1218 
 1219         in6_ifdetach(ifp);
 1220 }
 1221 
 1222 /*
 1223  * SIOC[GAD]LIFADDR.
 1224  *      SIOCGLIFADDR: get first address. (?)
 1225  *      SIOCGLIFADDR with IFLR_PREFIX:
 1226  *              get first address that matches the specified prefix.
 1227  *      SIOCALIFADDR: add the specified address.
 1228  *      SIOCALIFADDR with IFLR_PREFIX:
 1229  *              add the specified prefix, filling hostid part from
 1230  *              the first link-local address.  prefixlen must be <= 64.
 1231  *      SIOCDLIFADDR: delete the specified address.
 1232  *      SIOCDLIFADDR with IFLR_PREFIX:
 1233  *              delete the first address that matches the specified prefix.
 1234  * return values:
 1235  *      EINVAL on invalid parameters
 1236  *      EADDRNOTAVAIL on prefix match failed/specified address not found
 1237  *      other values may be returned from in6_ioctl()
 1238  *
 1239  * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
 1240  * this is to accomodate address naming scheme other than RFC2374,
 1241  * in the future.
 1242  * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
 1243  * address encoding scheme. (see figure on page 8)
 1244  */
 1245 static int
 1246 in6_lifaddr_ioctl(so, cmd, data, ifp, td)
 1247         struct socket *so;
 1248         u_long cmd;
 1249         caddr_t data;
 1250         struct ifnet *ifp;
 1251         struct thread *td;
 1252 {
 1253         struct if_laddrreq *iflr = (struct if_laddrreq *)data;
 1254         struct ifaddr *ifa;
 1255         struct sockaddr *sa;
 1256 
 1257         /* sanity checks */
 1258         if (!data || !ifp) {
 1259                 panic("invalid argument to in6_lifaddr_ioctl");
 1260                 /* NOTREACHED */
 1261         }
 1262 
 1263         switch (cmd) {
 1264         case SIOCGLIFADDR:
 1265                 /* address must be specified on GET with IFLR_PREFIX */
 1266                 if ((iflr->flags & IFLR_PREFIX) == 0)
 1267                         break;
 1268                 /* FALLTHROUGH */
 1269         case SIOCALIFADDR:
 1270         case SIOCDLIFADDR:
 1271                 /* address must be specified on ADD and DELETE */
 1272                 sa = (struct sockaddr *)&iflr->addr;
 1273                 if (sa->sa_family != AF_INET6)
 1274                         return EINVAL;
 1275                 if (sa->sa_len != sizeof(struct sockaddr_in6))
 1276                         return EINVAL;
 1277                 /* XXX need improvement */
 1278                 sa = (struct sockaddr *)&iflr->dstaddr;
 1279                 if (sa->sa_family && sa->sa_family != AF_INET6)
 1280                         return EINVAL;
 1281                 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
 1282                         return EINVAL;
 1283                 break;
 1284         default: /* shouldn't happen */
 1285 #if 0
 1286                 panic("invalid cmd to in6_lifaddr_ioctl");
 1287                 /* NOTREACHED */
 1288 #else
 1289                 return EOPNOTSUPP;
 1290 #endif
 1291         }
 1292         if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
 1293                 return EINVAL;
 1294 
 1295         switch (cmd) {
 1296         case SIOCALIFADDR:
 1297             {
 1298                 struct in6_aliasreq ifra;
 1299                 struct in6_addr *hostid = NULL;
 1300                 int prefixlen;
 1301 
 1302                 if ((iflr->flags & IFLR_PREFIX) != 0) {
 1303                         struct sockaddr_in6 *sin6;
 1304 
 1305                         /*
 1306                          * hostid is to fill in the hostid part of the
 1307                          * address.  hostid points to the first link-local
 1308                          * address attached to the interface.
 1309                          */
 1310                         ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
 1311                         if (!ifa)
 1312                                 return EADDRNOTAVAIL;
 1313                         hostid = IFA_IN6(ifa);
 1314 
 1315                         /* prefixlen must be <= 64. */
 1316                         if (64 < iflr->prefixlen)
 1317                                 return EINVAL;
 1318                         prefixlen = iflr->prefixlen;
 1319 
 1320                         /* hostid part must be zero. */
 1321                         sin6 = (struct sockaddr_in6 *)&iflr->addr;
 1322                         if (sin6->sin6_addr.s6_addr32[2] != 0 ||
 1323                             sin6->sin6_addr.s6_addr32[3] != 0) {
 1324                                 return EINVAL;
 1325                         }
 1326                 } else
 1327                         prefixlen = iflr->prefixlen;
 1328 
 1329                 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
 1330                 bzero(&ifra, sizeof(ifra));
 1331                 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
 1332 
 1333                 bcopy(&iflr->addr, &ifra.ifra_addr,
 1334                     ((struct sockaddr *)&iflr->addr)->sa_len);
 1335                 if (hostid) {
 1336                         /* fill in hostid part */
 1337                         ifra.ifra_addr.sin6_addr.s6_addr32[2] =
 1338                             hostid->s6_addr32[2];
 1339                         ifra.ifra_addr.sin6_addr.s6_addr32[3] =
 1340                             hostid->s6_addr32[3];
 1341                 }
 1342 
 1343                 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
 1344                         bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
 1345                             ((struct sockaddr *)&iflr->dstaddr)->sa_len);
 1346                         if (hostid) {
 1347                                 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
 1348                                     hostid->s6_addr32[2];
 1349                                 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
 1350                                     hostid->s6_addr32[3];
 1351                         }
 1352                 }
 1353 
 1354                 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
 1355                 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
 1356 
 1357                 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
 1358                 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
 1359             }
 1360         case SIOCGLIFADDR:
 1361         case SIOCDLIFADDR:
 1362             {
 1363                 struct in6_ifaddr *ia;
 1364                 struct in6_addr mask, candidate, match;
 1365                 struct sockaddr_in6 *sin6;
 1366                 int cmp;
 1367 
 1368                 bzero(&mask, sizeof(mask));
 1369                 if (iflr->flags & IFLR_PREFIX) {
 1370                         /* lookup a prefix rather than address. */
 1371                         in6_prefixlen2mask(&mask, iflr->prefixlen);
 1372 
 1373                         sin6 = (struct sockaddr_in6 *)&iflr->addr;
 1374                         bcopy(&sin6->sin6_addr, &match, sizeof(match));
 1375                         match.s6_addr32[0] &= mask.s6_addr32[0];
 1376                         match.s6_addr32[1] &= mask.s6_addr32[1];
 1377                         match.s6_addr32[2] &= mask.s6_addr32[2];
 1378                         match.s6_addr32[3] &= mask.s6_addr32[3];
 1379 
 1380                         /* if you set extra bits, that's wrong */
 1381                         if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
 1382                                 return EINVAL;
 1383 
 1384                         cmp = 1;
 1385                 } else {
 1386                         if (cmd == SIOCGLIFADDR) {
 1387                                 /* on getting an address, take the 1st match */
 1388                                 cmp = 0;        /* XXX */
 1389                         } else {
 1390                                 /* on deleting an address, do exact match */
 1391                                 in6_prefixlen2mask(&mask, 128);
 1392                                 sin6 = (struct sockaddr_in6 *)&iflr->addr;
 1393                                 bcopy(&sin6->sin6_addr, &match, sizeof(match));
 1394 
 1395                                 cmp = 1;
 1396                         }
 1397                 }
 1398 
 1399                 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
 1400                         if (ifa->ifa_addr->sa_family != AF_INET6)
 1401                                 continue;
 1402                         if (!cmp)
 1403                                 break;
 1404 
 1405                         bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
 1406                         /*
 1407                          * XXX: this is adhoc, but is necessary to allow
 1408                          * a user to specify fe80::/64 (not /10) for a
 1409                          * link-local address.
 1410                          */
 1411                         if (IN6_IS_ADDR_LINKLOCAL(&candidate))
 1412                                 candidate.s6_addr16[1] = 0;
 1413                         candidate.s6_addr32[0] &= mask.s6_addr32[0];
 1414                         candidate.s6_addr32[1] &= mask.s6_addr32[1];
 1415                         candidate.s6_addr32[2] &= mask.s6_addr32[2];
 1416                         candidate.s6_addr32[3] &= mask.s6_addr32[3];
 1417                         if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
 1418                                 break;
 1419                 }
 1420                 if (!ifa)
 1421                         return EADDRNOTAVAIL;
 1422                 ia = ifa2ia6(ifa);
 1423 
 1424                 if (cmd == SIOCGLIFADDR) {
 1425                         struct sockaddr_in6 *s6;
 1426 
 1427                         /* fill in the if_laddrreq structure */
 1428                         bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
 1429                         s6 = (struct sockaddr_in6 *)&iflr->addr;
 1430                         if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
 1431                                 s6->sin6_addr.s6_addr16[1] = 0;
 1432                                 if (in6_addr2zoneid(ifp, &s6->sin6_addr,
 1433                                     &s6->sin6_scope_id))
 1434                                         return (EINVAL);        /* XXX */
 1435                         }
 1436                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
 1437                                 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
 1438                                     ia->ia_dstaddr.sin6_len);
 1439                                 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
 1440                                 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
 1441                                         s6->sin6_addr.s6_addr16[1] = 0;
 1442                                         if (in6_addr2zoneid(ifp,
 1443                                             &s6->sin6_addr, &s6->sin6_scope_id))
 1444                                                 return (EINVAL); /* EINVAL */
 1445                                 }
 1446                         } else
 1447                                 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
 1448 
 1449                         iflr->prefixlen =
 1450                             in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
 1451 
 1452                         iflr->flags = ia->ia6_flags;    /* XXX */
 1453 
 1454                         return 0;
 1455                 } else {
 1456                         struct in6_aliasreq ifra;
 1457 
 1458                         /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
 1459                         bzero(&ifra, sizeof(ifra));
 1460                         bcopy(iflr->iflr_name, ifra.ifra_name,
 1461                             sizeof(ifra.ifra_name));
 1462 
 1463                         bcopy(&ia->ia_addr, &ifra.ifra_addr,
 1464                             ia->ia_addr.sin6_len);
 1465                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
 1466                                 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
 1467                                     ia->ia_dstaddr.sin6_len);
 1468                         } else {
 1469                                 bzero(&ifra.ifra_dstaddr,
 1470                                     sizeof(ifra.ifra_dstaddr));
 1471                         }
 1472                         bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
 1473                             ia->ia_prefixmask.sin6_len);
 1474 
 1475                         ifra.ifra_flags = ia->ia6_flags;
 1476                         return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
 1477                             ifp, td);
 1478                 }
 1479             }
 1480         }
 1481 
 1482         return EOPNOTSUPP;      /* just for safety */
 1483 }
 1484 
 1485 /*
 1486  * Initialize an interface's intetnet6 address
 1487  * and routing table entry.
 1488  */
 1489 static int
 1490 in6_ifinit(ifp, ia, sin6, newhost)
 1491         struct ifnet *ifp;
 1492         struct in6_ifaddr *ia;
 1493         struct sockaddr_in6 *sin6;
 1494         int newhost;
 1495 {
 1496         int     error = 0, plen, ifacount = 0;
 1497         int     s = splimp();
 1498         struct ifaddr *ifa;
 1499 
 1500         /*
 1501          * Give the interface a chance to initialize
 1502          * if this is its first address,
 1503          * and to validate the address if necessary.
 1504          */
 1505         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
 1506                 if (ifa->ifa_addr == NULL)
 1507                         continue;       /* just for safety */
 1508                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1509                         continue;
 1510                 ifacount++;
 1511         }
 1512 
 1513         ia->ia_addr = *sin6;
 1514 
 1515         if (ifacount <= 1 && ifp->if_ioctl &&
 1516             (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
 1517                 splx(s);
 1518                 return (error);
 1519         }
 1520         splx(s);
 1521 
 1522         ia->ia_ifa.ifa_metric = ifp->if_metric;
 1523 
 1524         /* we could do in(6)_socktrim here, but just omit it at this moment. */
 1525 
 1526         /*
 1527          * Special case:
 1528          * If a new destination address is specified for a point-to-point
 1529          * interface, install a route to the destination as an interface
 1530          * direct route.
 1531          * XXX: the logic below rejects assigning multiple addresses on a p2p
 1532          * interface that share a same destination.
 1533          */
 1534         plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
 1535         if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
 1536             ia->ia_dstaddr.sin6_family == AF_INET6) {
 1537                 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
 1538                                     RTF_UP | RTF_HOST)) != 0)
 1539                         return (error);
 1540                 ia->ia_flags |= IFA_ROUTE;
 1541         }
 1542         if (plen < 128) {
 1543                 /*
 1544                  * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
 1545                  */
 1546                 ia->ia_ifa.ifa_flags |= RTF_CLONING;
 1547         }
 1548 
 1549         /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
 1550         if (newhost) {
 1551                 /* set the rtrequest function to create llinfo */
 1552                 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
 1553                 in6_ifaddloop(&(ia->ia_ifa));
 1554         }
 1555 
 1556         return (error);
 1557 }
 1558 
 1559 struct in6_multi_mship *
 1560 in6_joingroup(ifp, addr, errorp)
 1561         struct ifnet *ifp;
 1562         struct in6_addr *addr;
 1563         int *errorp;
 1564 {
 1565         struct in6_multi_mship *imm;
 1566 
 1567         imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
 1568         if (!imm) {
 1569                 *errorp = ENOBUFS;
 1570                 return NULL;
 1571         }
 1572         imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
 1573         if (!imm->i6mm_maddr) {
 1574                 /* *errorp is alrady set */
 1575                 free(imm, M_IPMADDR);
 1576                 return NULL;
 1577         }
 1578         return imm;
 1579 }
 1580 
 1581 int
 1582 in6_leavegroup(imm)
 1583         struct in6_multi_mship *imm;
 1584 {
 1585 
 1586         if (imm->i6mm_maddr)
 1587                 in6_delmulti(imm->i6mm_maddr);
 1588         free(imm,  M_IPMADDR);
 1589         return 0;
 1590 }
 1591 
 1592 /*
 1593  * Find an IPv6 interface link-local address specific to an interface.
 1594  */
 1595 struct in6_ifaddr *
 1596 in6ifa_ifpforlinklocal(ifp, ignoreflags)
 1597         struct ifnet *ifp;
 1598         int ignoreflags;
 1599 {
 1600         struct ifaddr *ifa;
 1601 
 1602         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
 1603                 if (ifa->ifa_addr == NULL)
 1604                         continue;       /* just for safety */
 1605                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1606                         continue;
 1607                 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
 1608                         if ((((struct in6_ifaddr *)ifa)->ia6_flags &
 1609                              ignoreflags) != 0)
 1610                                 continue;
 1611                         break;
 1612                 }
 1613         }
 1614 
 1615         return ((struct in6_ifaddr *)ifa);
 1616 }
 1617 
 1618 
 1619 /*
 1620  * find the internet address corresponding to a given interface and address.
 1621  */
 1622 struct in6_ifaddr *
 1623 in6ifa_ifpwithaddr(ifp, addr)
 1624         struct ifnet *ifp;
 1625         struct in6_addr *addr;
 1626 {
 1627         struct ifaddr *ifa;
 1628 
 1629         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
 1630                 if (ifa->ifa_addr == NULL)
 1631                         continue;       /* just for safety */
 1632                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1633                         continue;
 1634                 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
 1635                         break;
 1636         }
 1637 
 1638         return ((struct in6_ifaddr *)ifa);
 1639 }
 1640 
 1641 /*
 1642  * Convert IP6 address to printable (loggable) representation.
 1643  */
 1644 static char digits[] = "0123456789abcdef";
 1645 static int ip6round = 0;
 1646 char *
 1647 ip6_sprintf(addr)
 1648         const struct in6_addr *addr;
 1649 {
 1650         static char ip6buf[8][48];
 1651         int i;
 1652         char *cp;
 1653         const u_int16_t *a = (const u_int16_t *)addr;
 1654         const u_int8_t *d;
 1655         int dcolon = 0;
 1656 
 1657         ip6round = (ip6round + 1) & 7;
 1658         cp = ip6buf[ip6round];
 1659 
 1660         for (i = 0; i < 8; i++) {
 1661                 if (dcolon == 1) {
 1662                         if (*a == 0) {
 1663                                 if (i == 7)
 1664                                         *cp++ = ':';
 1665                                 a++;
 1666                                 continue;
 1667                         } else
 1668                                 dcolon = 2;
 1669                 }
 1670                 if (*a == 0) {
 1671                         if (dcolon == 0 && *(a + 1) == 0) {
 1672                                 if (i == 0)
 1673                                         *cp++ = ':';
 1674                                 *cp++ = ':';
 1675                                 dcolon = 1;
 1676                         } else {
 1677                                 *cp++ = '';
 1678                                 *cp++ = ':';
 1679                         }
 1680                         a++;
 1681                         continue;
 1682                 }
 1683                 d = (const u_char *)a;
 1684                 *cp++ = digits[*d >> 4];
 1685                 *cp++ = digits[*d++ & 0xf];
 1686                 *cp++ = digits[*d >> 4];
 1687                 *cp++ = digits[*d & 0xf];
 1688                 *cp++ = ':';
 1689                 a++;
 1690         }
 1691         *--cp = 0;
 1692         return (ip6buf[ip6round]);
 1693 }
 1694 
 1695 int
 1696 in6_localaddr(in6)
 1697         struct in6_addr *in6;
 1698 {
 1699         struct in6_ifaddr *ia;
 1700 
 1701         if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
 1702                 return 1;
 1703 
 1704         for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
 1705                 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
 1706                     &ia->ia_prefixmask.sin6_addr)) {
 1707                         return 1;
 1708                 }
 1709         }
 1710 
 1711         return (0);
 1712 }
 1713 
 1714 int
 1715 in6_is_addr_deprecated(sa6)
 1716         struct sockaddr_in6 *sa6;
 1717 {
 1718         struct in6_ifaddr *ia;
 1719 
 1720         for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
 1721                 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
 1722                                        &sa6->sin6_addr) &&
 1723                     (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
 1724                         return (1); /* true */
 1725 
 1726                 /* XXX: do we still have to go thru the rest of the list? */
 1727         }
 1728 
 1729         return (0);             /* false */
 1730 }
 1731 
 1732 /*
 1733  * return length of part which dst and src are equal
 1734  * hard coding...
 1735  */
 1736 int
 1737 in6_matchlen(src, dst)
 1738 struct in6_addr *src, *dst;
 1739 {
 1740         int match = 0;
 1741         u_char *s = (u_char *)src, *d = (u_char *)dst;
 1742         u_char *lim = s + 16, r;
 1743 
 1744         while (s < lim)
 1745                 if ((r = (*d++ ^ *s++)) != 0) {
 1746                         while (r < 128) {
 1747                                 match++;
 1748                                 r <<= 1;
 1749                         }
 1750                         break;
 1751                 } else
 1752                         match += 8;
 1753         return match;
 1754 }
 1755 
 1756 /* XXX: to be scope conscious */
 1757 int
 1758 in6_are_prefix_equal(p1, p2, len)
 1759         struct in6_addr *p1, *p2;
 1760         int len;
 1761 {
 1762         int bytelen, bitlen;
 1763 
 1764         /* sanity check */
 1765         if (0 > len || len > 128) {
 1766                 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
 1767                     len);
 1768                 return (0);
 1769         }
 1770 
 1771         bytelen = len / 8;
 1772         bitlen = len % 8;
 1773 
 1774         if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
 1775                 return (0);
 1776         if (bitlen != 0 &&
 1777             p1->s6_addr[bytelen] >> (8 - bitlen) !=
 1778             p2->s6_addr[bytelen] >> (8 - bitlen))
 1779                 return (0);
 1780 
 1781         return (1);
 1782 }
 1783 
 1784 void
 1785 in6_prefixlen2mask(maskp, len)
 1786         struct in6_addr *maskp;
 1787         int len;
 1788 {
 1789         u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
 1790         int bytelen, bitlen, i;
 1791 
 1792         /* sanity check */
 1793         if (0 > len || len > 128) {
 1794                 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
 1795                     len);
 1796                 return;
 1797         }
 1798 
 1799         bzero(maskp, sizeof(*maskp));
 1800         bytelen = len / 8;
 1801         bitlen = len % 8;
 1802         for (i = 0; i < bytelen; i++)
 1803                 maskp->s6_addr[i] = 0xff;
 1804         if (bitlen)
 1805                 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
 1806 }
 1807 
 1808 /*
 1809  * return the best address out of the same scope. if no address was
 1810  * found, return the first valid address from designated IF.
 1811  */
 1812 struct in6_ifaddr *
 1813 in6_ifawithifp(ifp, dst)
 1814         struct ifnet *ifp;
 1815         struct in6_addr *dst;
 1816 {
 1817         int dst_scope = in6_addrscope(dst), blen = -1, tlen;
 1818         struct ifaddr *ifa;
 1819         struct in6_ifaddr *besta = 0;
 1820         struct in6_ifaddr *dep[2];      /* last-resort: deprecated */
 1821 
 1822         dep[0] = dep[1] = NULL;
 1823 
 1824         /*
 1825          * We first look for addresses in the same scope.
 1826          * If there is one, return it.
 1827          * If two or more, return one which matches the dst longest.
 1828          * If none, return one of global addresses assigned other ifs.
 1829          */
 1830         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
 1831                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1832                         continue;
 1833                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
 1834                         continue; /* XXX: is there any case to allow anycast? */
 1835                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
 1836                         continue; /* don't use this interface */
 1837                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
 1838                         continue;
 1839                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
 1840                         if (ip6_use_deprecated)
 1841                                 dep[0] = (struct in6_ifaddr *)ifa;
 1842                         continue;
 1843                 }
 1844 
 1845                 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
 1846                         /*
 1847                          * call in6_matchlen() as few as possible
 1848                          */
 1849                         if (besta) {
 1850                                 if (blen == -1)
 1851                                         blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
 1852                                 tlen = in6_matchlen(IFA_IN6(ifa), dst);
 1853                                 if (tlen > blen) {
 1854                                         blen = tlen;
 1855                                         besta = (struct in6_ifaddr *)ifa;
 1856                                 }
 1857                         } else
 1858                                 besta = (struct in6_ifaddr *)ifa;
 1859                 }
 1860         }
 1861         if (besta)
 1862                 return (besta);
 1863 
 1864         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
 1865                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1866                         continue;
 1867                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
 1868                         continue; /* XXX: is there any case to allow anycast? */
 1869                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
 1870                         continue; /* don't use this interface */
 1871                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
 1872                         continue;
 1873                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
 1874                         if (ip6_use_deprecated)
 1875                                 dep[1] = (struct in6_ifaddr *)ifa;
 1876                         continue;
 1877                 }
 1878 
 1879                 return (struct in6_ifaddr *)ifa;
 1880         }
 1881 
 1882         /* use the last-resort values, that are, deprecated addresses */
 1883         if (dep[0])
 1884                 return dep[0];
 1885         if (dep[1])
 1886                 return dep[1];
 1887 
 1888         return NULL;
 1889 }
 1890 
 1891 /*
 1892  * perform DAD when interface becomes IFF_UP.
 1893  */
 1894 void
 1895 in6_if_up(ifp)
 1896         struct ifnet *ifp;
 1897 {
 1898         struct ifaddr *ifa;
 1899         struct in6_ifaddr *ia;
 1900         int dad_delay;          /* delay ticks before DAD output */
 1901 
 1902         /*
 1903          * special cases, like 6to4, are handled in in6_ifattach
 1904          */
 1905         in6_ifattach(ifp, NULL);
 1906 
 1907         dad_delay = 0;
 1908         TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
 1909                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1910                         continue;
 1911                 ia = (struct in6_ifaddr *)ifa;
 1912                 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
 1913                         nd6_dad_start(ifa, &dad_delay);
 1914         }
 1915 }
 1916 
 1917 int
 1918 in6if_do_dad(ifp)
 1919         struct ifnet *ifp;
 1920 {
 1921         if ((ifp->if_flags & IFF_LOOPBACK) != 0)
 1922                 return (0);
 1923 
 1924         switch (ifp->if_type) {
 1925 #ifdef IFT_DUMMY
 1926         case IFT_DUMMY:
 1927 #endif
 1928         case IFT_FAITH:
 1929                 /*
 1930                  * These interfaces do not have the IFF_LOOPBACK flag,
 1931                  * but loop packets back.  We do not have to do DAD on such
 1932                  * interfaces.  We should even omit it, because loop-backed
 1933                  * NS would confuse the DAD procedure.
 1934                  */
 1935                 return (0);
 1936         default:
 1937                 /*
 1938                  * Our DAD routine requires the interface up and running.
 1939                  * However, some interfaces can be up before the RUNNING
 1940                  * status.  Additionaly, users may try to assign addresses
 1941                  * before the interface becomes up (or running).
 1942                  * We simply skip DAD in such a case as a work around.
 1943                  * XXX: we should rather mark "tentative" on such addresses,
 1944                  * and do DAD after the interface becomes ready.
 1945                  */
 1946                 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
 1947                     (IFF_UP|IFF_RUNNING))
 1948                         return (0);
 1949 
 1950                 return (1);
 1951         }
 1952 }
 1953 
 1954 /*
 1955  * Calculate max IPv6 MTU through all the interfaces and store it
 1956  * to in6_maxmtu.
 1957  */
 1958 void
 1959 in6_setmaxmtu()
 1960 {
 1961         unsigned long maxmtu = 0;
 1962         struct ifnet *ifp;
 1963 
 1964         IFNET_RLOCK();
 1965         for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
 1966                 /* this function can be called during ifnet initialization */
 1967                 if (!ifp->if_afdata[AF_INET6])
 1968                         continue;
 1969                 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
 1970                     IN6_LINKMTU(ifp) > maxmtu)
 1971                         maxmtu = IN6_LINKMTU(ifp);
 1972         }
 1973         IFNET_RUNLOCK();
 1974         if (maxmtu)          /* update only when maxmtu is positive */
 1975                 in6_maxmtu = maxmtu;
 1976 }
 1977 
 1978 void *
 1979 in6_domifattach(ifp)
 1980         struct ifnet *ifp;
 1981 {
 1982         struct in6_ifextra *ext;
 1983 
 1984         ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
 1985         bzero(ext, sizeof(*ext));
 1986 
 1987         ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
 1988             M_IFADDR, M_WAITOK);
 1989         bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
 1990 
 1991         ext->icmp6_ifstat =
 1992             (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
 1993             M_IFADDR, M_WAITOK);
 1994         bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
 1995 
 1996         ext->nd_ifinfo = nd6_ifattach(ifp);
 1997         ext->scope6_id = scope6_ifattach(ifp);
 1998         return ext;
 1999 }
 2000 
 2001 void
 2002 in6_domifdetach(ifp, aux)
 2003         struct ifnet *ifp;
 2004         void *aux;
 2005 {
 2006         struct in6_ifextra *ext = (struct in6_ifextra *)aux;
 2007 
 2008         scope6_ifdetach(ext->scope6_id);
 2009         nd6_ifdetach(ext->nd_ifinfo);
 2010         free(ext->in6_ifstat, M_IFADDR);
 2011         free(ext->icmp6_ifstat, M_IFADDR);
 2012         free(ext, M_IFADDR);
 2013 }
 2014 
 2015 /*
 2016  * Convert sockaddr_in6 to sockaddr_in.  Original sockaddr_in6 must be
 2017  * v4 mapped addr or v4 compat addr
 2018  */
 2019 void
 2020 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
 2021 {
 2022         bzero(sin, sizeof(*sin));
 2023         sin->sin_len = sizeof(struct sockaddr_in);
 2024         sin->sin_family = AF_INET;
 2025         sin->sin_port = sin6->sin6_port;
 2026         sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
 2027 }
 2028 
 2029 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
 2030 void
 2031 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
 2032 {
 2033         bzero(sin6, sizeof(*sin6));
 2034         sin6->sin6_len = sizeof(struct sockaddr_in6);
 2035         sin6->sin6_family = AF_INET6;
 2036         sin6->sin6_port = sin->sin_port;
 2037         sin6->sin6_addr.s6_addr32[0] = 0;
 2038         sin6->sin6_addr.s6_addr32[1] = 0;
 2039         sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
 2040         sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
 2041 }
 2042 
 2043 /* Convert sockaddr_in6 into sockaddr_in. */
 2044 void
 2045 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
 2046 {
 2047         struct sockaddr_in *sin_p;
 2048         struct sockaddr_in6 sin6;
 2049 
 2050         /*
 2051          * Save original sockaddr_in6 addr and convert it
 2052          * to sockaddr_in.
 2053          */
 2054         sin6 = *(struct sockaddr_in6 *)nam;
 2055         sin_p = (struct sockaddr_in *)nam;
 2056         in6_sin6_2_sin(sin_p, &sin6);
 2057 }
 2058 
 2059 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
 2060 void
 2061 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
 2062 {
 2063         struct sockaddr_in *sin_p;
 2064         struct sockaddr_in6 *sin6_p;
 2065 
 2066         MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
 2067                M_WAITOK);
 2068         sin_p = (struct sockaddr_in *)*nam;
 2069         in6_sin_2_v4mapsin6(sin_p, sin6_p);
 2070         FREE(*nam, M_SONAME);
 2071         *nam = (struct sockaddr *)sin6_p;
 2072 }

Cache object: 5353b0e501f159a8b8f8cc0c7cbc32a2


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