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/nd6_rtr.c

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

    1 /*-
    2  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. Neither the name of the project nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  *
   31  *      $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __FBSDID("$FreeBSD: releng/12.0/sys/netinet6/nd6_rtr.c 336676 2018-07-24 16:35:52Z andrew $");
   36 
   37 #include "opt_inet.h"
   38 #include "opt_inet6.h"
   39 
   40 #include <sys/param.h>
   41 #include <sys/systm.h>
   42 #include <sys/malloc.h>
   43 #include <sys/mbuf.h>
   44 #include <sys/refcount.h>
   45 #include <sys/socket.h>
   46 #include <sys/sockio.h>
   47 #include <sys/time.h>
   48 #include <sys/kernel.h>
   49 #include <sys/lock.h>
   50 #include <sys/errno.h>
   51 #include <sys/rmlock.h>
   52 #include <sys/rwlock.h>
   53 #include <sys/syslog.h>
   54 #include <sys/queue.h>
   55 
   56 #include <net/if.h>
   57 #include <net/if_var.h>
   58 #include <net/if_types.h>
   59 #include <net/if_dl.h>
   60 #include <net/route.h>
   61 #include <net/route_var.h>
   62 #include <net/radix.h>
   63 #include <net/vnet.h>
   64 
   65 #include <netinet/in.h>
   66 #include <net/if_llatbl.h>
   67 #include <netinet6/in6_var.h>
   68 #include <netinet6/in6_ifattach.h>
   69 #include <netinet/ip6.h>
   70 #include <netinet6/ip6_var.h>
   71 #include <netinet6/nd6.h>
   72 #include <netinet/icmp6.h>
   73 #include <netinet6/scope6_var.h>
   74 
   75 static int rtpref(struct nd_defrouter *);
   76 static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *);
   77 static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *,
   78     struct mbuf *, int);
   79 static struct in6_ifaddr *in6_ifadd(struct nd_prefixctl *, int);
   80 static struct nd_pfxrouter *pfxrtr_lookup(struct nd_prefix *,
   81     struct nd_defrouter *);
   82 static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *);
   83 static void pfxrtr_del(struct nd_pfxrouter *);
   84 static struct nd_pfxrouter *find_pfxlist_reachable_router(struct nd_prefix *);
   85 static void defrouter_delreq(struct nd_defrouter *);
   86 static void nd6_rtmsg(int, struct rtentry *);
   87 
   88 static int in6_init_prefix_ltimes(struct nd_prefix *);
   89 static void in6_init_address_ltimes(struct nd_prefix *,
   90     struct in6_addrlifetime *);
   91 
   92 static int rt6_deleteroute(const struct rtentry *, void *);
   93 
   94 VNET_DECLARE(int, nd6_recalc_reachtm_interval);
   95 #define V_nd6_recalc_reachtm_interval   VNET(nd6_recalc_reachtm_interval)
   96 
   97 VNET_DEFINE_STATIC(struct ifnet *, nd6_defifp);
   98 VNET_DEFINE(int, nd6_defifindex);
   99 #define V_nd6_defifp                    VNET(nd6_defifp)
  100 
  101 VNET_DEFINE(int, ip6_use_tempaddr) = 0;
  102 
  103 VNET_DEFINE(int, ip6_desync_factor);
  104 VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME;
  105 VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME;
  106 
  107 VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE;
  108 
  109 /* RTPREF_MEDIUM has to be 0! */
  110 #define RTPREF_HIGH     1
  111 #define RTPREF_MEDIUM   0
  112 #define RTPREF_LOW      (-1)
  113 #define RTPREF_RESERVED (-2)
  114 #define RTPREF_INVALID  (-3)    /* internal */
  115 
  116 /*
  117  * Receive Router Solicitation Message - just for routers.
  118  * Router solicitation/advertisement is mostly managed by userland program
  119  * (rtadvd) so here we have no function like nd6_ra_output().
  120  *
  121  * Based on RFC 2461
  122  */
  123 void
  124 nd6_rs_input(struct mbuf *m, int off, int icmp6len)
  125 {
  126         struct ifnet *ifp = m->m_pkthdr.rcvif;
  127         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  128         struct nd_router_solicit *nd_rs;
  129         struct in6_addr saddr6 = ip6->ip6_src;
  130         char *lladdr = NULL;
  131         int lladdrlen = 0;
  132         union nd_opts ndopts;
  133         char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
  134 
  135         /*
  136          * Accept RS only when V_ip6_forwarding=1 and the interface has
  137          * no ND6_IFF_ACCEPT_RTADV.
  138          */
  139         if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV)
  140                 goto freeit;
  141 
  142         /* RFC 6980: Nodes MUST silently ignore fragments */   
  143         if(m->m_flags & M_FRAGMENTED)
  144                 goto freeit;
  145 
  146         /* Sanity checks */
  147         if (ip6->ip6_hlim != 255) {
  148                 nd6log((LOG_ERR,
  149                     "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
  150                     ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
  151                     ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
  152                 goto bad;
  153         }
  154 
  155         /*
  156          * Don't update the neighbor cache, if src = ::.
  157          * This indicates that the src has no IP address assigned yet.
  158          */
  159         if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
  160                 goto freeit;
  161 
  162 #ifndef PULLDOWN_TEST
  163         IP6_EXTHDR_CHECK(m, off, icmp6len,);
  164         nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
  165 #else
  166         IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
  167         if (nd_rs == NULL) {
  168                 ICMP6STAT_INC(icp6s_tooshort);
  169                 return;
  170         }
  171 #endif
  172 
  173         icmp6len -= sizeof(*nd_rs);
  174         nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
  175         if (nd6_options(&ndopts) < 0) {
  176                 nd6log((LOG_INFO,
  177                     "nd6_rs_input: invalid ND option, ignored\n"));
  178                 /* nd6_options have incremented stats */
  179                 goto freeit;
  180         }
  181 
  182         if (ndopts.nd_opts_src_lladdr) {
  183                 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
  184                 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
  185         }
  186 
  187         if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
  188                 nd6log((LOG_INFO,
  189                     "nd6_rs_input: lladdrlen mismatch for %s "
  190                     "(if %d, RS packet %d)\n",
  191                     ip6_sprintf(ip6bufs, &saddr6),
  192                     ifp->if_addrlen, lladdrlen - 2));
  193                 goto bad;
  194         }
  195 
  196         nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
  197 
  198  freeit:
  199         m_freem(m);
  200         return;
  201 
  202  bad:
  203         ICMP6STAT_INC(icp6s_badrs);
  204         m_freem(m);
  205 }
  206 
  207 /*
  208  * Receive Router Advertisement Message.
  209  *
  210  * Based on RFC 2461
  211  * TODO: on-link bit on prefix information
  212  * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
  213  */
  214 void
  215 nd6_ra_input(struct mbuf *m, int off, int icmp6len)
  216 {
  217         struct ifnet *ifp = m->m_pkthdr.rcvif;
  218         struct nd_ifinfo *ndi = ND_IFINFO(ifp);
  219         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  220         struct nd_router_advert *nd_ra;
  221         struct in6_addr saddr6 = ip6->ip6_src;
  222         int mcast = 0;
  223         union nd_opts ndopts;
  224         struct nd_defrouter *dr;
  225         char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
  226 
  227         dr = NULL;
  228 
  229         /*
  230          * We only accept RAs only when the per-interface flag
  231          * ND6_IFF_ACCEPT_RTADV is on the receiving interface.
  232          */
  233         if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV))
  234                 goto freeit;
  235 
  236         /* RFC 6980: Nodes MUST silently ignore fragments */
  237         if(m->m_flags & M_FRAGMENTED)
  238                 goto freeit;
  239 
  240         if (ip6->ip6_hlim != 255) {
  241                 nd6log((LOG_ERR,
  242                     "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
  243                     ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
  244                     ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
  245                 goto bad;
  246         }
  247 
  248         if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
  249                 nd6log((LOG_ERR,
  250                     "nd6_ra_input: src %s is not link-local\n",
  251                     ip6_sprintf(ip6bufs, &saddr6)));
  252                 goto bad;
  253         }
  254 
  255 #ifndef PULLDOWN_TEST
  256         IP6_EXTHDR_CHECK(m, off, icmp6len,);
  257         nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
  258 #else
  259         IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
  260         if (nd_ra == NULL) {
  261                 ICMP6STAT_INC(icp6s_tooshort);
  262                 return;
  263         }
  264 #endif
  265 
  266         icmp6len -= sizeof(*nd_ra);
  267         nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
  268         if (nd6_options(&ndopts) < 0) {
  269                 nd6log((LOG_INFO,
  270                     "nd6_ra_input: invalid ND option, ignored\n"));
  271                 /* nd6_options have incremented stats */
  272                 goto freeit;
  273         }
  274 
  275     {
  276         struct nd_defrouter dr0;
  277         u_int32_t advreachable = nd_ra->nd_ra_reachable;
  278 
  279         /* remember if this is a multicasted advertisement */
  280         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
  281                 mcast = 1;
  282 
  283         bzero(&dr0, sizeof(dr0));
  284         dr0.rtaddr = saddr6;
  285         dr0.raflags = nd_ra->nd_ra_flags_reserved;
  286         /*
  287          * Effectively-disable routes from RA messages when
  288          * ND6_IFF_NO_RADR enabled on the receiving interface or
  289          * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1).
  290          */
  291         if (ndi->flags & ND6_IFF_NO_RADR)
  292                 dr0.rtlifetime = 0;
  293         else if (V_ip6_forwarding && !V_ip6_rfc6204w3)
  294                 dr0.rtlifetime = 0;
  295         else
  296                 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
  297         dr0.expire = time_uptime + dr0.rtlifetime;
  298         dr0.ifp = ifp;
  299         /* unspecified or not? (RFC 2461 6.3.4) */
  300         if (advreachable) {
  301                 advreachable = ntohl(advreachable);
  302                 if (advreachable <= MAX_REACHABLE_TIME &&
  303                     ndi->basereachable != advreachable) {
  304                         ndi->basereachable = advreachable;
  305                         ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
  306                         ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */
  307                 }
  308         }
  309         if (nd_ra->nd_ra_retransmit)
  310                 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
  311         if (nd_ra->nd_ra_curhoplimit) {
  312                 if (ndi->chlim < nd_ra->nd_ra_curhoplimit)
  313                         ndi->chlim = nd_ra->nd_ra_curhoplimit;
  314                 else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) {
  315                         log(LOG_ERR, "RA with a lower CurHopLimit sent from "
  316                             "%s on %s (current = %d, received = %d). "
  317                             "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src),
  318                             if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit);
  319                 }
  320         }
  321         dr = defrtrlist_update(&dr0);
  322     }
  323 
  324         /*
  325          * prefix
  326          */
  327         if (ndopts.nd_opts_pi) {
  328                 struct nd_opt_hdr *pt;
  329                 struct nd_opt_prefix_info *pi = NULL;
  330                 struct nd_prefixctl pr;
  331 
  332                 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
  333                      pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
  334                      pt = (struct nd_opt_hdr *)((caddr_t)pt +
  335                                                 (pt->nd_opt_len << 3))) {
  336                         if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
  337                                 continue;
  338                         pi = (struct nd_opt_prefix_info *)pt;
  339 
  340                         if (pi->nd_opt_pi_len != 4) {
  341                                 nd6log((LOG_INFO,
  342                                     "nd6_ra_input: invalid option "
  343                                     "len %d for prefix information option, "
  344                                     "ignored\n", pi->nd_opt_pi_len));
  345                                 continue;
  346                         }
  347 
  348                         if (128 < pi->nd_opt_pi_prefix_len) {
  349                                 nd6log((LOG_INFO,
  350                                     "nd6_ra_input: invalid prefix "
  351                                     "len %d for prefix information option, "
  352                                     "ignored\n", pi->nd_opt_pi_prefix_len));
  353                                 continue;
  354                         }
  355 
  356                         if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
  357                          || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
  358                                 nd6log((LOG_INFO,
  359                                     "nd6_ra_input: invalid prefix "
  360                                     "%s, ignored\n",
  361                                     ip6_sprintf(ip6bufs,
  362                                         &pi->nd_opt_pi_prefix)));
  363                                 continue;
  364                         }
  365 
  366                         bzero(&pr, sizeof(pr));
  367                         pr.ndpr_prefix.sin6_family = AF_INET6;
  368                         pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
  369                         pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
  370                         pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
  371 
  372                         pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
  373                             ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
  374                         pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
  375                             ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
  376                         pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
  377                         pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
  378                         pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time);
  379                         (void)prelist_update(&pr, dr, m, mcast);
  380                 }
  381         }
  382         if (dr != NULL) {
  383                 defrouter_rele(dr);
  384                 dr = NULL;
  385         }
  386 
  387         /*
  388          * MTU
  389          */
  390         if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
  391                 u_long mtu;
  392                 u_long maxmtu;
  393 
  394                 mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
  395 
  396                 /* lower bound */
  397                 if (mtu < IPV6_MMTU) {
  398                         nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
  399                             "mtu=%lu sent from %s, ignoring\n",
  400                             mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src)));
  401                         goto skip;
  402                 }
  403 
  404                 /* upper bound */
  405                 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu)
  406                     ? ndi->maxmtu : ifp->if_mtu;
  407                 if (mtu <= maxmtu) {
  408                         int change = (ndi->linkmtu != mtu);
  409 
  410                         ndi->linkmtu = mtu;
  411                         if (change) {
  412                                 /* in6_maxmtu may change */
  413                                 in6_setmaxmtu();
  414                                 rt_updatemtu(ifp);
  415                         }
  416                 } else {
  417                         nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
  418                             "mtu=%lu sent from %s; "
  419                             "exceeds maxmtu %lu, ignoring\n",
  420                             mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu));
  421                 }
  422         }
  423 
  424  skip:
  425 
  426         /*
  427          * Source link layer address
  428          */
  429     {
  430         char *lladdr = NULL;
  431         int lladdrlen = 0;
  432 
  433         if (ndopts.nd_opts_src_lladdr) {
  434                 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
  435                 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
  436         }
  437 
  438         if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
  439                 nd6log((LOG_INFO,
  440                     "nd6_ra_input: lladdrlen mismatch for %s "
  441                     "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6),
  442                     ifp->if_addrlen, lladdrlen - 2));
  443                 goto bad;
  444         }
  445 
  446         nd6_cache_lladdr(ifp, &saddr6, lladdr,
  447             lladdrlen, ND_ROUTER_ADVERT, 0);
  448 
  449         /*
  450          * Installing a link-layer address might change the state of the
  451          * router's neighbor cache, which might also affect our on-link
  452          * detection of adveritsed prefixes.
  453          */
  454         pfxlist_onlink_check();
  455     }
  456 
  457  freeit:
  458         m_freem(m);
  459         return;
  460 
  461  bad:
  462         ICMP6STAT_INC(icp6s_badra);
  463         m_freem(m);
  464 }
  465 
  466 /* tell the change to user processes watching the routing socket. */
  467 static void
  468 nd6_rtmsg(int cmd, struct rtentry *rt)
  469 {
  470         struct rt_addrinfo info;
  471         struct ifnet *ifp;
  472         struct ifaddr *ifa;
  473 
  474         bzero((caddr_t)&info, sizeof(info));
  475         info.rti_info[RTAX_DST] = rt_key(rt);
  476         info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
  477         info.rti_info[RTAX_NETMASK] = rt_mask(rt);
  478         ifp = rt->rt_ifp;
  479         if (ifp != NULL) {
  480                 IF_ADDR_RLOCK(ifp);
  481                 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
  482                 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
  483                 ifa_ref(ifa);
  484                 IF_ADDR_RUNLOCK(ifp);
  485                 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
  486         } else
  487                 ifa = NULL;
  488 
  489         rt_missmsg_fib(cmd, &info, rt->rt_flags, 0, rt->rt_fibnum);
  490         if (ifa != NULL)
  491                 ifa_free(ifa);
  492 }
  493 
  494 /*
  495  * default router list processing sub routines
  496  */
  497 
  498 static void
  499 defrouter_addreq(struct nd_defrouter *new)
  500 {
  501         struct sockaddr_in6 def, mask, gate;
  502         struct rtentry *newrt = NULL;
  503         int error;
  504 
  505         bzero(&def, sizeof(def));
  506         bzero(&mask, sizeof(mask));
  507         bzero(&gate, sizeof(gate));
  508 
  509         def.sin6_len = mask.sin6_len = gate.sin6_len =
  510             sizeof(struct sockaddr_in6);
  511         def.sin6_family = gate.sin6_family = AF_INET6;
  512         gate.sin6_addr = new->rtaddr;
  513 
  514         error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def,
  515             (struct sockaddr *)&gate, (struct sockaddr *)&mask,
  516             RTF_GATEWAY, &newrt, new->ifp->if_fib);
  517         if (newrt) {
  518                 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */
  519                 RTFREE(newrt);
  520         }
  521         if (error == 0)
  522                 new->installed = 1;
  523 }
  524 
  525 struct nd_defrouter *
  526 defrouter_lookup_locked(struct in6_addr *addr, struct ifnet *ifp)
  527 {
  528         struct nd_defrouter *dr;
  529 
  530         ND6_LOCK_ASSERT();
  531         TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
  532                 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) {
  533                         defrouter_ref(dr);
  534                         return (dr);
  535                 }
  536         return (NULL);
  537 }
  538 
  539 struct nd_defrouter *
  540 defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp)
  541 {
  542         struct nd_defrouter *dr;
  543 
  544         ND6_RLOCK();
  545         dr = defrouter_lookup_locked(addr, ifp);
  546         ND6_RUNLOCK();
  547         return (dr);
  548 }
  549 
  550 void
  551 defrouter_ref(struct nd_defrouter *dr)
  552 {
  553 
  554         refcount_acquire(&dr->refcnt);
  555 }
  556 
  557 void
  558 defrouter_rele(struct nd_defrouter *dr)
  559 {
  560 
  561         if (refcount_release(&dr->refcnt))
  562                 free(dr, M_IP6NDP);
  563 }
  564 
  565 /*
  566  * Remove the default route for a given router.
  567  * This is just a subroutine function for defrouter_select_fib(), and
  568  * should not be called from anywhere else.
  569  */
  570 static void
  571 defrouter_delreq(struct nd_defrouter *dr)
  572 {
  573         struct sockaddr_in6 def, mask, gate;
  574         struct rtentry *oldrt = NULL;
  575 
  576         bzero(&def, sizeof(def));
  577         bzero(&mask, sizeof(mask));
  578         bzero(&gate, sizeof(gate));
  579 
  580         def.sin6_len = mask.sin6_len = gate.sin6_len =
  581             sizeof(struct sockaddr_in6);
  582         def.sin6_family = gate.sin6_family = AF_INET6;
  583         gate.sin6_addr = dr->rtaddr;
  584 
  585         in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def,
  586             (struct sockaddr *)&gate,
  587             (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, dr->ifp->if_fib);
  588         if (oldrt) {
  589                 nd6_rtmsg(RTM_DELETE, oldrt);
  590                 RTFREE(oldrt);
  591         }
  592 
  593         dr->installed = 0;
  594 }
  595 
  596 /*
  597  * Remove all default routes from default router list.
  598  */
  599 void
  600 defrouter_reset(void)
  601 {
  602         struct nd_defrouter *dr, **dra;
  603         int count, i;
  604 
  605         count = i = 0;
  606 
  607         /*
  608          * We can't delete routes with the ND lock held, so make a copy of the
  609          * current default router list and use that when deleting routes.
  610          */
  611         ND6_RLOCK();
  612         TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry)
  613                 count++;
  614         ND6_RUNLOCK();
  615 
  616         dra = malloc(count * sizeof(*dra), M_TEMP, M_WAITOK | M_ZERO);
  617 
  618         ND6_RLOCK();
  619         TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
  620                 if (i == count)
  621                         break;
  622                 defrouter_ref(dr);
  623                 dra[i++] = dr;
  624         }
  625         ND6_RUNLOCK();
  626 
  627         for (i = 0; i < count && dra[i] != NULL; i++) {
  628                 defrouter_delreq(dra[i]);
  629                 defrouter_rele(dra[i]);
  630         }
  631         free(dra, M_TEMP);
  632 
  633         /*
  634          * XXX should we also nuke any default routers in the kernel, by
  635          * going through them by rtalloc1()?
  636          */
  637 }
  638 
  639 /*
  640  * Look up a matching default router list entry and remove it. Returns true if a
  641  * matching entry was found, false otherwise.
  642  */
  643 bool
  644 defrouter_remove(struct in6_addr *addr, struct ifnet *ifp)
  645 {
  646         struct nd_defrouter *dr;
  647 
  648         ND6_WLOCK();
  649         dr = defrouter_lookup_locked(addr, ifp);
  650         if (dr == NULL) {
  651                 ND6_WUNLOCK();
  652                 return (false);
  653         }
  654 
  655         defrouter_unlink(dr, NULL);
  656         ND6_WUNLOCK();
  657         defrouter_del(dr);
  658         defrouter_rele(dr);
  659         return (true);
  660 }
  661 
  662 /*
  663  * Remove a router from the global list and optionally stash it in a
  664  * caller-supplied queue.
  665  *
  666  * The ND lock must be held.
  667  */
  668 void
  669 defrouter_unlink(struct nd_defrouter *dr, struct nd_drhead *drq)
  670 {
  671 
  672         ND6_WLOCK_ASSERT();
  673         TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
  674         V_nd6_list_genid++;
  675         if (drq != NULL)
  676                 TAILQ_INSERT_TAIL(drq, dr, dr_entry);
  677 }
  678 
  679 void
  680 defrouter_del(struct nd_defrouter *dr)
  681 {
  682         struct nd_defrouter *deldr = NULL;
  683         struct nd_prefix *pr;
  684         struct nd_pfxrouter *pfxrtr;
  685 
  686         ND6_UNLOCK_ASSERT();
  687 
  688         /*
  689          * Flush all the routing table entries that use the router
  690          * as a next hop.
  691          */
  692         if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV)
  693                 rt6_flush(&dr->rtaddr, dr->ifp);
  694 
  695         if (dr->installed) {
  696                 deldr = dr;
  697                 defrouter_delreq(dr);
  698         }
  699 
  700         /*
  701          * Also delete all the pointers to the router in each prefix lists.
  702          */
  703         ND6_WLOCK();
  704         LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
  705                 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
  706                         pfxrtr_del(pfxrtr);
  707         }
  708         ND6_WUNLOCK();
  709 
  710         pfxlist_onlink_check();
  711 
  712         /*
  713          * If the router is the primary one, choose a new one.
  714          * Note that defrouter_select_fib() will remove the current
  715          * gateway from the routing table.
  716          */
  717         if (deldr)
  718                 defrouter_select_fib(deldr->ifp->if_fib);
  719 
  720         /*
  721          * Release the list reference.
  722          */
  723         defrouter_rele(dr);
  724 }
  725 
  726 /*
  727  * Default Router Selection according to Section 6.3.6 of RFC 2461 and
  728  * draft-ietf-ipngwg-router-selection:
  729  * 1) Routers that are reachable or probably reachable should be preferred.
  730  *    If we have more than one (probably) reachable router, prefer ones
  731  *    with the highest router preference.
  732  * 2) When no routers on the list are known to be reachable or
  733  *    probably reachable, routers SHOULD be selected in a round-robin
  734  *    fashion, regardless of router preference values.
  735  * 3) If the Default Router List is empty, assume that all
  736  *    destinations are on-link.
  737  *
  738  * We assume nd_defrouter is sorted by router preference value.
  739  * Since the code below covers both with and without router preference cases,
  740  * we do not need to classify the cases by ifdef.
  741  *
  742  * At this moment, we do not try to install more than one default router,
  743  * even when the multipath routing is available, because we're not sure about
  744  * the benefits for stub hosts comparing to the risk of making the code
  745  * complicated and the possibility of introducing bugs.
  746  *
  747  * We maintain a single list of routers for multiple FIBs, only considering one
  748  * at a time based on the receiving interface's FIB. If @fibnum is RT_ALL_FIBS,
  749  * we do the whole thing multiple times.
  750  */
  751 void
  752 defrouter_select_fib(int fibnum)
  753 {
  754         struct nd_defrouter *dr, *selected_dr, *installed_dr;
  755         struct llentry *ln = NULL;
  756 
  757         if (fibnum == RT_ALL_FIBS) {
  758                 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
  759                         defrouter_select_fib(fibnum);
  760                 }
  761         }
  762 
  763         ND6_RLOCK();
  764         /*
  765          * Let's handle easy case (3) first:
  766          * If default router list is empty, there's nothing to be done.
  767          */
  768         if (TAILQ_EMPTY(&V_nd_defrouter)) {
  769                 ND6_RUNLOCK();
  770                 return;
  771         }
  772 
  773         /*
  774          * Search for a (probably) reachable router from the list.
  775          * We just pick up the first reachable one (if any), assuming that
  776          * the ordering rule of the list described in defrtrlist_update().
  777          */
  778         selected_dr = installed_dr = NULL;
  779         TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
  780                 IF_AFDATA_RLOCK(dr->ifp);
  781                 if (selected_dr == NULL && dr->ifp->if_fib == fibnum &&
  782                     (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
  783                     ND6_IS_LLINFO_PROBREACH(ln)) {
  784                         selected_dr = dr;
  785                         defrouter_ref(selected_dr);
  786                 }
  787                 IF_AFDATA_RUNLOCK(dr->ifp);
  788                 if (ln != NULL) {
  789                         LLE_RUNLOCK(ln);
  790                         ln = NULL;
  791                 }
  792 
  793                 if (dr->installed && dr->ifp->if_fib == fibnum) {
  794                         if (installed_dr == NULL) {
  795                                 installed_dr = dr;
  796                                 defrouter_ref(installed_dr);
  797                         } else {
  798                                 /*
  799                                  * this should not happen.
  800                                  * warn for diagnosis.
  801                                  */
  802                                 log(LOG_ERR, "defrouter_select_fib: more than "
  803                                              "one router is installed\n");
  804                         }
  805                 }
  806         }
  807         /*
  808          * If none of the default routers was found to be reachable,
  809          * round-robin the list regardless of preference.
  810          * Otherwise, if we have an installed router, check if the selected
  811          * (reachable) router should really be preferred to the installed one.
  812          * We only prefer the new router when the old one is not reachable
  813          * or when the new one has a really higher preference value.
  814          */
  815         if (selected_dr == NULL) {
  816                 if (installed_dr == NULL ||
  817                     TAILQ_NEXT(installed_dr, dr_entry) == NULL)
  818                         dr = TAILQ_FIRST(&V_nd_defrouter);
  819                 else
  820                         dr = TAILQ_NEXT(installed_dr, dr_entry);
  821 
  822                 /* Ensure we select a router for this FIB. */
  823                 TAILQ_FOREACH_FROM(dr, &V_nd_defrouter, dr_entry) {
  824                         if (dr->ifp->if_fib == fibnum) {
  825                                 selected_dr = dr;
  826                                 defrouter_ref(selected_dr);
  827                                 break;
  828                         }
  829                 }
  830         } else if (installed_dr != NULL) {
  831                 IF_AFDATA_RLOCK(installed_dr->ifp);
  832                 if ((ln = nd6_lookup(&installed_dr->rtaddr, 0,
  833                                      installed_dr->ifp)) &&
  834                     ND6_IS_LLINFO_PROBREACH(ln) &&
  835                     installed_dr->ifp->if_fib == fibnum &&
  836                     rtpref(selected_dr) <= rtpref(installed_dr)) {
  837                         defrouter_rele(selected_dr);
  838                         selected_dr = installed_dr;
  839                 }
  840                 IF_AFDATA_RUNLOCK(installed_dr->ifp);
  841                 if (ln != NULL)
  842                         LLE_RUNLOCK(ln);
  843         }
  844         ND6_RUNLOCK();
  845 
  846         /*
  847          * If we selected a router for this FIB and it's different
  848          * than the installed one, remove the installed router and
  849          * install the selected one in its place.
  850          */
  851         if (installed_dr != selected_dr) {
  852                 if (installed_dr != NULL) {
  853                         defrouter_delreq(installed_dr);
  854                         defrouter_rele(installed_dr);
  855                 }
  856                 if (selected_dr != NULL)
  857                         defrouter_addreq(selected_dr);
  858         }
  859         if (selected_dr != NULL)
  860                 defrouter_rele(selected_dr);
  861 }
  862 
  863 /*
  864  * Maintain old KPI for default router selection.
  865  * If unspecified, we can re-select routers for all FIBs.
  866  */
  867 void
  868 defrouter_select(void)
  869 {
  870         defrouter_select_fib(RT_ALL_FIBS);
  871 }
  872 
  873 /*
  874  * for default router selection
  875  * regards router-preference field as a 2-bit signed integer
  876  */
  877 static int
  878 rtpref(struct nd_defrouter *dr)
  879 {
  880         switch (dr->raflags & ND_RA_FLAG_RTPREF_MASK) {
  881         case ND_RA_FLAG_RTPREF_HIGH:
  882                 return (RTPREF_HIGH);
  883         case ND_RA_FLAG_RTPREF_MEDIUM:
  884         case ND_RA_FLAG_RTPREF_RSV:
  885                 return (RTPREF_MEDIUM);
  886         case ND_RA_FLAG_RTPREF_LOW:
  887                 return (RTPREF_LOW);
  888         default:
  889                 /*
  890                  * This case should never happen.  If it did, it would mean a
  891                  * serious bug of kernel internal.  We thus always bark here.
  892                  * Or, can we even panic?
  893                  */
  894                 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->raflags);
  895                 return (RTPREF_INVALID);
  896         }
  897         /* NOTREACHED */
  898 }
  899 
  900 static struct nd_defrouter *
  901 defrtrlist_update(struct nd_defrouter *new)
  902 {
  903         struct nd_defrouter *dr, *n;
  904         uint64_t genid;
  905         int oldpref;
  906         bool writelocked;
  907 
  908         if (new->rtlifetime == 0) {
  909                 defrouter_remove(&new->rtaddr, new->ifp);
  910                 return (NULL);
  911         }
  912 
  913         ND6_RLOCK();
  914         writelocked = false;
  915 restart:
  916         dr = defrouter_lookup_locked(&new->rtaddr, new->ifp);
  917         if (dr != NULL) {
  918                 oldpref = rtpref(dr);
  919 
  920                 /* override */
  921                 dr->raflags = new->raflags; /* XXX flag check */
  922                 dr->rtlifetime = new->rtlifetime;
  923                 dr->expire = new->expire;
  924 
  925                 /*
  926                  * If the preference does not change, there's no need
  927                  * to sort the entries. Also make sure the selected
  928                  * router is still installed in the kernel.
  929                  */
  930                 if (dr->installed && rtpref(new) == oldpref) {
  931                         if (writelocked)
  932                                 ND6_WUNLOCK();
  933                         else
  934                                 ND6_RUNLOCK();
  935                         return (dr);
  936                 }
  937         }
  938 
  939         /*
  940          * The router needs to be reinserted into the default router
  941          * list, so upgrade to a write lock. If that fails and the list
  942          * has potentially changed while the lock was dropped, we'll
  943          * redo the lookup with the write lock held.
  944          */
  945         if (!writelocked) {
  946                 writelocked = true;
  947                 if (!ND6_TRY_UPGRADE()) {
  948                         genid = V_nd6_list_genid;
  949                         ND6_RUNLOCK();
  950                         ND6_WLOCK();
  951                         if (genid != V_nd6_list_genid)
  952                                 goto restart;
  953                 }
  954         }
  955 
  956         if (dr != NULL) {
  957                 /*
  958                  * The preferred router may have changed, so relocate this
  959                  * router.
  960                  */
  961                 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry);
  962                 n = dr;
  963         } else {
  964                 n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO);
  965                 if (n == NULL) {
  966                         ND6_WUNLOCK();
  967                         return (NULL);
  968                 }
  969                 memcpy(n, new, sizeof(*n));
  970                 /* Initialize with an extra reference for the caller. */
  971                 refcount_init(&n->refcnt, 2);
  972         }
  973 
  974         /*
  975          * Insert the new router in the Default Router List;
  976          * The Default Router List should be in the descending order
  977          * of router-preferece.  Routers with the same preference are
  978          * sorted in the arriving time order.
  979          */
  980 
  981         /* insert at the end of the group */
  982         TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
  983                 if (rtpref(n) > rtpref(dr))
  984                         break;
  985         }
  986         if (dr != NULL)
  987                 TAILQ_INSERT_BEFORE(dr, n, dr_entry);
  988         else
  989                 TAILQ_INSERT_TAIL(&V_nd_defrouter, n, dr_entry);
  990         V_nd6_list_genid++;
  991         ND6_WUNLOCK();
  992 
  993         defrouter_select_fib(new->ifp->if_fib);
  994 
  995         return (n);
  996 }
  997 
  998 static struct nd_pfxrouter *
  999 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr)
 1000 {
 1001         struct nd_pfxrouter *search;
 1002 
 1003         ND6_LOCK_ASSERT();
 1004 
 1005         LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) {
 1006                 if (search->router == dr)
 1007                         break;
 1008         }
 1009         return (search);
 1010 }
 1011 
 1012 static void
 1013 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr)
 1014 {
 1015         struct nd_pfxrouter *new;
 1016         bool update;
 1017 
 1018         ND6_UNLOCK_ASSERT();
 1019 
 1020         ND6_RLOCK();
 1021         if (pfxrtr_lookup(pr, dr) != NULL) {
 1022                 ND6_RUNLOCK();
 1023                 return;
 1024         }
 1025         ND6_RUNLOCK();
 1026 
 1027         new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
 1028         if (new == NULL)
 1029                 return;
 1030         defrouter_ref(dr);
 1031         new->router = dr;
 1032 
 1033         ND6_WLOCK();
 1034         if (pfxrtr_lookup(pr, dr) == NULL) {
 1035                 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
 1036                 update = true;
 1037         } else {
 1038                 /* We lost a race to add the reference. */
 1039                 defrouter_rele(dr);
 1040                 free(new, M_IP6NDP);
 1041                 update = false;
 1042         }
 1043         ND6_WUNLOCK();
 1044 
 1045         if (update)
 1046                 pfxlist_onlink_check();
 1047 }
 1048 
 1049 static void
 1050 pfxrtr_del(struct nd_pfxrouter *pfr)
 1051 {
 1052 
 1053         ND6_WLOCK_ASSERT();
 1054 
 1055         LIST_REMOVE(pfr, pfr_entry);
 1056         defrouter_rele(pfr->router);
 1057         free(pfr, M_IP6NDP);
 1058 }
 1059 
 1060 static struct nd_prefix *
 1061 nd6_prefix_lookup_locked(struct nd_prefixctl *key)
 1062 {
 1063         struct nd_prefix *search;
 1064 
 1065         ND6_LOCK_ASSERT();
 1066 
 1067         LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) {
 1068                 if (key->ndpr_ifp == search->ndpr_ifp &&
 1069                     key->ndpr_plen == search->ndpr_plen &&
 1070                     in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr,
 1071                     &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) {
 1072                         nd6_prefix_ref(search);
 1073                         break;
 1074                 }
 1075         }
 1076         return (search);
 1077 }
 1078 
 1079 struct nd_prefix *
 1080 nd6_prefix_lookup(struct nd_prefixctl *key)
 1081 {
 1082         struct nd_prefix *search;
 1083 
 1084         ND6_RLOCK();
 1085         search = nd6_prefix_lookup_locked(key);
 1086         ND6_RUNLOCK();
 1087         return (search);
 1088 }
 1089 
 1090 void
 1091 nd6_prefix_ref(struct nd_prefix *pr)
 1092 {
 1093 
 1094         refcount_acquire(&pr->ndpr_refcnt);
 1095 }
 1096 
 1097 void
 1098 nd6_prefix_rele(struct nd_prefix *pr)
 1099 {
 1100 
 1101         if (refcount_release(&pr->ndpr_refcnt)) {
 1102                 KASSERT(LIST_EMPTY(&pr->ndpr_advrtrs),
 1103                     ("prefix %p has advertising routers", pr));
 1104                 free(pr, M_IP6NDP);
 1105         }
 1106 }
 1107 
 1108 int
 1109 nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr,
 1110     struct nd_prefix **newp)
 1111 {
 1112         struct nd_prefix *new;
 1113         char ip6buf[INET6_ADDRSTRLEN];
 1114         int error;
 1115 
 1116         new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO);
 1117         if (new == NULL)
 1118                 return (ENOMEM);
 1119         refcount_init(&new->ndpr_refcnt, newp != NULL ? 2 : 1);
 1120         new->ndpr_ifp = pr->ndpr_ifp;
 1121         new->ndpr_prefix = pr->ndpr_prefix;
 1122         new->ndpr_plen = pr->ndpr_plen;
 1123         new->ndpr_vltime = pr->ndpr_vltime;
 1124         new->ndpr_pltime = pr->ndpr_pltime;
 1125         new->ndpr_flags = pr->ndpr_flags;
 1126         if ((error = in6_init_prefix_ltimes(new)) != 0) {
 1127                 free(new, M_IP6NDP);
 1128                 return (error);
 1129         }
 1130         new->ndpr_lastupdate = time_uptime;
 1131 
 1132         /* initialization */
 1133         LIST_INIT(&new->ndpr_advrtrs);
 1134         in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
 1135         /* make prefix in the canonical form */
 1136         IN6_MASK_ADDR(&new->ndpr_prefix.sin6_addr, &new->ndpr_mask);
 1137 
 1138         ND6_WLOCK();
 1139         LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry);
 1140         V_nd6_list_genid++;
 1141         ND6_WUNLOCK();
 1142 
 1143         /* ND_OPT_PI_FLAG_ONLINK processing */
 1144         if (new->ndpr_raf_onlink) {
 1145                 ND6_ONLINK_LOCK();
 1146                 if ((error = nd6_prefix_onlink(new)) != 0) {
 1147                         nd6log((LOG_ERR, "nd6_prelist_add: failed to make "
 1148                             "the prefix %s/%d on-link on %s (errno=%d)\n",
 1149                             ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
 1150                             pr->ndpr_plen, if_name(pr->ndpr_ifp), error));
 1151                         /* proceed anyway. XXX: is it correct? */
 1152                 }
 1153                 ND6_ONLINK_UNLOCK();
 1154         }
 1155 
 1156         if (dr != NULL)
 1157                 pfxrtr_add(new, dr);
 1158         if (newp != NULL)
 1159                 *newp = new;
 1160         return (0);
 1161 }
 1162 
 1163 /*
 1164  * Remove a prefix from the prefix list and optionally stash it in a
 1165  * caller-provided list.
 1166  *
 1167  * The ND6 lock must be held.
 1168  */
 1169 void
 1170 nd6_prefix_unlink(struct nd_prefix *pr, struct nd_prhead *list)
 1171 {
 1172 
 1173         ND6_WLOCK_ASSERT();
 1174 
 1175         LIST_REMOVE(pr, ndpr_entry);
 1176         V_nd6_list_genid++;
 1177         if (list != NULL)
 1178                 LIST_INSERT_HEAD(list, pr, ndpr_entry);
 1179 }
 1180 
 1181 /*
 1182  * Free an unlinked prefix, first marking it off-link if necessary.
 1183  */
 1184 void
 1185 nd6_prefix_del(struct nd_prefix *pr)
 1186 {
 1187         struct nd_pfxrouter *pfr, *next;
 1188         int e;
 1189         char ip6buf[INET6_ADDRSTRLEN];
 1190 
 1191         KASSERT(pr->ndpr_addrcnt == 0,
 1192             ("prefix %p has referencing addresses", pr));
 1193         ND6_UNLOCK_ASSERT();
 1194 
 1195         /*
 1196          * Though these flags are now meaningless, we'd rather keep the value
 1197          * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users
 1198          * when executing "ndp -p".
 1199          */
 1200         if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
 1201                 ND6_ONLINK_LOCK();
 1202                 if ((e = nd6_prefix_offlink(pr)) != 0) {
 1203                         nd6log((LOG_ERR,
 1204                             "nd6_prefix_del: failed to make %s/%d offlink "
 1205                             "on %s, errno=%d\n",
 1206                             ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
 1207                             pr->ndpr_plen, if_name(pr->ndpr_ifp), e));
 1208                         /* what should we do? */
 1209                 }
 1210                 ND6_ONLINK_UNLOCK();
 1211         }
 1212 
 1213         /* Release references to routers that have advertised this prefix. */
 1214         ND6_WLOCK();
 1215         LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next)
 1216                 pfxrtr_del(pfr);
 1217         ND6_WUNLOCK();
 1218 
 1219         nd6_prefix_rele(pr);
 1220 
 1221         pfxlist_onlink_check();
 1222 }
 1223 
 1224 static int
 1225 prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr,
 1226     struct mbuf *m, int mcast)
 1227 {
 1228         struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL;
 1229         struct ifaddr *ifa;
 1230         struct ifnet *ifp = new->ndpr_ifp;
 1231         struct nd_prefix *pr;
 1232         int error = 0;
 1233         int auth;
 1234         struct in6_addrlifetime lt6_tmp;
 1235         char ip6buf[INET6_ADDRSTRLEN];
 1236 
 1237         auth = 0;
 1238         if (m) {
 1239                 /*
 1240                  * Authenticity for NA consists authentication for
 1241                  * both IP header and IP datagrams, doesn't it ?
 1242                  */
 1243 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
 1244                 auth = ((m->m_flags & M_AUTHIPHDR) &&
 1245                     (m->m_flags & M_AUTHIPDGM));
 1246 #endif
 1247         }
 1248 
 1249         if ((pr = nd6_prefix_lookup(new)) != NULL) {
 1250                 /*
 1251                  * nd6_prefix_lookup() ensures that pr and new have the same
 1252                  * prefix on a same interface.
 1253                  */
 1254 
 1255                 /*
 1256                  * Update prefix information.  Note that the on-link (L) bit
 1257                  * and the autonomous (A) bit should NOT be changed from 1
 1258                  * to 0.
 1259                  */
 1260                 if (new->ndpr_raf_onlink == 1)
 1261                         pr->ndpr_raf_onlink = 1;
 1262                 if (new->ndpr_raf_auto == 1)
 1263                         pr->ndpr_raf_auto = 1;
 1264                 if (new->ndpr_raf_onlink) {
 1265                         pr->ndpr_vltime = new->ndpr_vltime;
 1266                         pr->ndpr_pltime = new->ndpr_pltime;
 1267                         (void)in6_init_prefix_ltimes(pr); /* XXX error case? */
 1268                         pr->ndpr_lastupdate = time_uptime;
 1269                 }
 1270 
 1271                 if (new->ndpr_raf_onlink &&
 1272                     (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) {
 1273                         ND6_ONLINK_LOCK();
 1274                         if ((error = nd6_prefix_onlink(pr)) != 0) {
 1275                                 nd6log((LOG_ERR,
 1276                                     "prelist_update: failed to make "
 1277                                     "the prefix %s/%d on-link on %s "
 1278                                     "(errno=%d)\n",
 1279                                     ip6_sprintf(ip6buf,
 1280                                         &pr->ndpr_prefix.sin6_addr),
 1281                                     pr->ndpr_plen, if_name(pr->ndpr_ifp),
 1282                                     error));
 1283                                 /* proceed anyway. XXX: is it correct? */
 1284                         }
 1285                         ND6_ONLINK_UNLOCK();
 1286                 }
 1287 
 1288                 if (dr != NULL)
 1289                         pfxrtr_add(pr, dr);
 1290         } else {
 1291                 if (new->ndpr_vltime == 0)
 1292                         goto end;
 1293                 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0)
 1294                         goto end;
 1295 
 1296                 error = nd6_prelist_add(new, dr, &pr);
 1297                 if (error != 0) {
 1298                         nd6log((LOG_NOTICE, "prelist_update: "
 1299                             "nd6_prelist_add failed for %s/%d on %s errno=%d\n",
 1300                             ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr),
 1301                             new->ndpr_plen, if_name(new->ndpr_ifp), error));
 1302                         goto end; /* we should just give up in this case. */
 1303                 }
 1304 
 1305                 /*
 1306                  * XXX: from the ND point of view, we can ignore a prefix
 1307                  * with the on-link bit being zero.  However, we need a
 1308                  * prefix structure for references from autoconfigured
 1309                  * addresses.  Thus, we explicitly make sure that the prefix
 1310                  * itself expires now.
 1311                  */
 1312                 if (pr->ndpr_raf_onlink == 0) {
 1313                         pr->ndpr_vltime = 0;
 1314                         pr->ndpr_pltime = 0;
 1315                         in6_init_prefix_ltimes(pr);
 1316                 }
 1317         }
 1318 
 1319         /*
 1320          * Address autoconfiguration based on Section 5.5.3 of RFC 2462.
 1321          * Note that pr must be non NULL at this point.
 1322          */
 1323 
 1324         /* 5.5.3 (a). Ignore the prefix without the A bit set. */
 1325         if (!new->ndpr_raf_auto)
 1326                 goto end;
 1327 
 1328         /*
 1329          * 5.5.3 (b). the link-local prefix should have been ignored in
 1330          * nd6_ra_input.
 1331          */
 1332 
 1333         /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */
 1334         if (new->ndpr_pltime > new->ndpr_vltime) {
 1335                 error = EINVAL; /* XXX: won't be used */
 1336                 goto end;
 1337         }
 1338 
 1339         /*
 1340          * 5.5.3 (d).  If the prefix advertised is not equal to the prefix of
 1341          * an address configured by stateless autoconfiguration already in the
 1342          * list of addresses associated with the interface, and the Valid
 1343          * Lifetime is not 0, form an address.  We first check if we have
 1344          * a matching prefix.
 1345          * Note: we apply a clarification in rfc2462bis-02 here.  We only
 1346          * consider autoconfigured addresses while RFC2462 simply said
 1347          * "address".
 1348          */
 1349         IF_ADDR_RLOCK(ifp);
 1350         CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1351                 struct in6_ifaddr *ifa6;
 1352                 u_int32_t remaininglifetime;
 1353 
 1354                 if (ifa->ifa_addr->sa_family != AF_INET6)
 1355                         continue;
 1356 
 1357                 ifa6 = (struct in6_ifaddr *)ifa;
 1358 
 1359                 /*
 1360                  * We only consider autoconfigured addresses as per rfc2462bis.
 1361                  */
 1362                 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF))
 1363                         continue;
 1364 
 1365                 /*
 1366                  * Spec is not clear here, but I believe we should concentrate
 1367                  * on unicast (i.e. not anycast) addresses.
 1368                  * XXX: other ia6_flags? detached or duplicated?
 1369                  */
 1370                 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0)
 1371                         continue;
 1372 
 1373                 /*
 1374                  * Ignore the address if it is not associated with a prefix
 1375                  * or is associated with a prefix that is different from this
 1376                  * one.  (pr is never NULL here)
 1377                  */
 1378                 if (ifa6->ia6_ndpr != pr)
 1379                         continue;
 1380 
 1381                 if (ia6_match == NULL) /* remember the first one */
 1382                         ia6_match = ifa6;
 1383 
 1384                 /*
 1385                  * An already autoconfigured address matched.  Now that we
 1386                  * are sure there is at least one matched address, we can
 1387                  * proceed to 5.5.3. (e): update the lifetimes according to the
 1388                  * "two hours" rule and the privacy extension.
 1389                  * We apply some clarifications in rfc2462bis:
 1390                  * - use remaininglifetime instead of storedlifetime as a
 1391                  *   variable name
 1392                  * - remove the dead code in the "two-hour" rule
 1393                  */
 1394 #define TWOHOUR         (120*60)
 1395                 lt6_tmp = ifa6->ia6_lifetime;
 1396 
 1397                 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME)
 1398                         remaininglifetime = ND6_INFINITE_LIFETIME;
 1399                 else if (time_uptime - ifa6->ia6_updatetime >
 1400                          lt6_tmp.ia6t_vltime) {
 1401                         /*
 1402                          * The case of "invalid" address.  We should usually
 1403                          * not see this case.
 1404                          */
 1405                         remaininglifetime = 0;
 1406                 } else
 1407                         remaininglifetime = lt6_tmp.ia6t_vltime -
 1408                             (time_uptime - ifa6->ia6_updatetime);
 1409 
 1410                 /* when not updating, keep the current stored lifetime. */
 1411                 lt6_tmp.ia6t_vltime = remaininglifetime;
 1412 
 1413                 if (TWOHOUR < new->ndpr_vltime ||
 1414                     remaininglifetime < new->ndpr_vltime) {
 1415                         lt6_tmp.ia6t_vltime = new->ndpr_vltime;
 1416                 } else if (remaininglifetime <= TWOHOUR) {
 1417                         if (auth) {
 1418                                 lt6_tmp.ia6t_vltime = new->ndpr_vltime;
 1419                         }
 1420                 } else {
 1421                         /*
 1422                          * new->ndpr_vltime <= TWOHOUR &&
 1423                          * TWOHOUR < remaininglifetime
 1424                          */
 1425                         lt6_tmp.ia6t_vltime = TWOHOUR;
 1426                 }
 1427 
 1428                 /* The 2 hour rule is not imposed for preferred lifetime. */
 1429                 lt6_tmp.ia6t_pltime = new->ndpr_pltime;
 1430 
 1431                 in6_init_address_ltimes(pr, &lt6_tmp);
 1432 
 1433                 /*
 1434                  * We need to treat lifetimes for temporary addresses
 1435                  * differently, according to
 1436                  * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1);
 1437                  * we only update the lifetimes when they are in the maximum
 1438                  * intervals.
 1439                  */
 1440                 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
 1441                         u_int32_t maxvltime, maxpltime;
 1442 
 1443                         if (V_ip6_temp_valid_lifetime >
 1444                             (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
 1445                             V_ip6_desync_factor)) {
 1446                                 maxvltime = V_ip6_temp_valid_lifetime -
 1447                                     (time_uptime - ifa6->ia6_createtime) -
 1448                                     V_ip6_desync_factor;
 1449                         } else
 1450                                 maxvltime = 0;
 1451                         if (V_ip6_temp_preferred_lifetime >
 1452                             (u_int32_t)((time_uptime - ifa6->ia6_createtime) +
 1453                             V_ip6_desync_factor)) {
 1454                                 maxpltime = V_ip6_temp_preferred_lifetime -
 1455                                     (time_uptime - ifa6->ia6_createtime) -
 1456                                     V_ip6_desync_factor;
 1457                         } else
 1458                                 maxpltime = 0;
 1459 
 1460                         if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME ||
 1461                             lt6_tmp.ia6t_vltime > maxvltime) {
 1462                                 lt6_tmp.ia6t_vltime = maxvltime;
 1463                         }
 1464                         if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME ||
 1465                             lt6_tmp.ia6t_pltime > maxpltime) {
 1466                                 lt6_tmp.ia6t_pltime = maxpltime;
 1467                         }
 1468                 }
 1469                 ifa6->ia6_lifetime = lt6_tmp;
 1470                 ifa6->ia6_updatetime = time_uptime;
 1471         }
 1472         IF_ADDR_RUNLOCK(ifp);
 1473         if (ia6_match == NULL && new->ndpr_vltime) {
 1474                 int ifidlen;
 1475 
 1476                 /*
 1477                  * 5.5.3 (d) (continued)
 1478                  * No address matched and the valid lifetime is non-zero.
 1479                  * Create a new address.
 1480                  */
 1481 
 1482                 /*
 1483                  * Prefix Length check:
 1484                  * If the sum of the prefix length and interface identifier
 1485                  * length does not equal 128 bits, the Prefix Information
 1486                  * option MUST be ignored.  The length of the interface
 1487                  * identifier is defined in a separate link-type specific
 1488                  * document.
 1489                  */
 1490                 ifidlen = in6_if2idlen(ifp);
 1491                 if (ifidlen < 0) {
 1492                         /* this should not happen, so we always log it. */
 1493                         log(LOG_ERR, "prelist_update: IFID undefined (%s)\n",
 1494                             if_name(ifp));
 1495                         goto end;
 1496                 }
 1497                 if (ifidlen + pr->ndpr_plen != 128) {
 1498                         nd6log((LOG_INFO,
 1499                             "prelist_update: invalid prefixlen "
 1500                             "%d for %s, ignored\n",
 1501                             pr->ndpr_plen, if_name(ifp)));
 1502                         goto end;
 1503                 }
 1504 
 1505                 if ((ia6 = in6_ifadd(new, mcast)) != NULL) {
 1506                         /*
 1507                          * note that we should use pr (not new) for reference.
 1508                          */
 1509                         pr->ndpr_addrcnt++;
 1510                         ia6->ia6_ndpr = pr;
 1511 
 1512                         /*
 1513                          * RFC 3041 3.3 (2).
 1514                          * When a new public address is created as described
 1515                          * in RFC2462, also create a new temporary address.
 1516                          *
 1517                          * RFC 3041 3.5.
 1518                          * When an interface connects to a new link, a new
 1519                          * randomized interface identifier should be generated
 1520                          * immediately together with a new set of temporary
 1521                          * addresses.  Thus, we specifiy 1 as the 2nd arg of
 1522                          * in6_tmpifadd().
 1523                          */
 1524                         if (V_ip6_use_tempaddr) {
 1525                                 int e;
 1526                                 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) {
 1527                                         nd6log((LOG_NOTICE, "prelist_update: "
 1528                                             "failed to create a temporary "
 1529                                             "address, errno=%d\n",
 1530                                             e));
 1531                                 }
 1532                         }
 1533                         ifa_free(&ia6->ia_ifa);
 1534 
 1535                         /*
 1536                          * A newly added address might affect the status
 1537                          * of other addresses, so we check and update it.
 1538                          * XXX: what if address duplication happens?
 1539                          */
 1540                         pfxlist_onlink_check();
 1541                 } else {
 1542                         /* just set an error. do not bark here. */
 1543                         error = EADDRNOTAVAIL; /* XXX: might be unused. */
 1544                 }
 1545         }
 1546 
 1547 end:
 1548         if (pr != NULL)
 1549                 nd6_prefix_rele(pr);
 1550         return (error);
 1551 }
 1552 
 1553 /*
 1554  * A supplement function used in the on-link detection below;
 1555  * detect if a given prefix has a (probably) reachable advertising router.
 1556  * XXX: lengthy function name...
 1557  */
 1558 static struct nd_pfxrouter *
 1559 find_pfxlist_reachable_router(struct nd_prefix *pr)
 1560 {
 1561         struct nd_pfxrouter *pfxrtr;
 1562         struct llentry *ln;
 1563         int canreach;
 1564 
 1565         ND6_LOCK_ASSERT();
 1566 
 1567         LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) {
 1568                 IF_AFDATA_RLOCK(pfxrtr->router->ifp);
 1569                 ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp);
 1570                 IF_AFDATA_RUNLOCK(pfxrtr->router->ifp);
 1571                 if (ln == NULL)
 1572                         continue;
 1573                 canreach = ND6_IS_LLINFO_PROBREACH(ln);
 1574                 LLE_RUNLOCK(ln);
 1575                 if (canreach)
 1576                         break;
 1577         }
 1578         return (pfxrtr);
 1579 }
 1580 
 1581 /*
 1582  * Check if each prefix in the prefix list has at least one available router
 1583  * that advertised the prefix (a router is "available" if its neighbor cache
 1584  * entry is reachable or probably reachable).
 1585  * If the check fails, the prefix may be off-link, because, for example,
 1586  * we have moved from the network but the lifetime of the prefix has not
 1587  * expired yet.  So we should not use the prefix if there is another prefix
 1588  * that has an available router.
 1589  * But, if there is no prefix that has an available router, we still regard
 1590  * all the prefixes as on-link.  This is because we can't tell if all the
 1591  * routers are simply dead or if we really moved from the network and there
 1592  * is no router around us.
 1593  */
 1594 void
 1595 pfxlist_onlink_check(void)
 1596 {
 1597         struct nd_prefix *pr;
 1598         struct in6_ifaddr *ifa;
 1599         struct nd_defrouter *dr;
 1600         struct nd_pfxrouter *pfxrtr = NULL;
 1601         struct rm_priotracker in6_ifa_tracker;
 1602         uint64_t genid;
 1603         uint32_t flags;
 1604 
 1605         ND6_ONLINK_LOCK();
 1606         ND6_RLOCK();
 1607 
 1608         /*
 1609          * Check if there is a prefix that has a reachable advertising
 1610          * router.
 1611          */
 1612         LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
 1613                 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr))
 1614                         break;
 1615         }
 1616 
 1617         /*
 1618          * If we have no such prefix, check whether we still have a router
 1619          * that does not advertise any prefixes.
 1620          */
 1621         if (pr == NULL) {
 1622                 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
 1623                         struct nd_prefix *pr0;
 1624 
 1625                         LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) {
 1626                                 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL)
 1627                                         break;
 1628                         }
 1629                         if (pfxrtr != NULL)
 1630                                 break;
 1631                 }
 1632         }
 1633         if (pr != NULL || (!TAILQ_EMPTY(&V_nd_defrouter) && pfxrtr == NULL)) {
 1634                 /*
 1635                  * There is at least one prefix that has a reachable router,
 1636                  * or at least a router which probably does not advertise
 1637                  * any prefixes.  The latter would be the case when we move
 1638                  * to a new link where we have a router that does not provide
 1639                  * prefixes and we configure an address by hand.
 1640                  * Detach prefixes which have no reachable advertising
 1641                  * router, and attach other prefixes.
 1642                  */
 1643                 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
 1644                         /* XXX: a link-local prefix should never be detached */
 1645                         if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
 1646                             pr->ndpr_raf_onlink == 0 ||
 1647                             pr->ndpr_raf_auto == 0)
 1648                                 continue;
 1649 
 1650                         if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 &&
 1651                             find_pfxlist_reachable_router(pr) == NULL)
 1652                                 pr->ndpr_stateflags |= NDPRF_DETACHED;
 1653                         else if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 &&
 1654                             find_pfxlist_reachable_router(pr) != NULL)
 1655                                 pr->ndpr_stateflags &= ~NDPRF_DETACHED;
 1656                 }
 1657         } else {
 1658                 /* there is no prefix that has a reachable router */
 1659                 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
 1660                         if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
 1661                             pr->ndpr_raf_onlink == 0 ||
 1662                             pr->ndpr_raf_auto == 0)
 1663                                 continue;
 1664                         pr->ndpr_stateflags &= ~NDPRF_DETACHED;
 1665                 }
 1666         }
 1667 
 1668         /*
 1669          * Remove each interface route associated with a (just) detached
 1670          * prefix, and reinstall the interface route for a (just) attached
 1671          * prefix.  Note that all attempt of reinstallation does not
 1672          * necessarily success, when a same prefix is shared among multiple
 1673          * interfaces.  Such cases will be handled in nd6_prefix_onlink,
 1674          * so we don't have to care about them.
 1675          */
 1676 restart:
 1677         LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
 1678                 char ip6buf[INET6_ADDRSTRLEN];
 1679                 int e;
 1680 
 1681                 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) ||
 1682                     pr->ndpr_raf_onlink == 0 ||
 1683                     pr->ndpr_raf_auto == 0)
 1684                         continue;
 1685 
 1686                 flags = pr->ndpr_stateflags & (NDPRF_DETACHED | NDPRF_ONLINK);
 1687                 if (flags == 0 || flags == (NDPRF_DETACHED | NDPRF_ONLINK)) {
 1688                         genid = V_nd6_list_genid;
 1689                         ND6_RUNLOCK();
 1690                         if ((flags & NDPRF_ONLINK) != 0 &&
 1691                             (e = nd6_prefix_offlink(pr)) != 0) {
 1692                                 nd6log((LOG_ERR,
 1693                                     "pfxlist_onlink_check: failed to "
 1694                                     "make %s/%d offlink, errno=%d\n",
 1695                                     ip6_sprintf(ip6buf,
 1696                                             &pr->ndpr_prefix.sin6_addr),
 1697                                             pr->ndpr_plen, e));
 1698                         } else if ((flags & NDPRF_ONLINK) == 0 &&
 1699                             (e = nd6_prefix_onlink(pr)) != 0) {
 1700                                 nd6log((LOG_ERR,
 1701                                     "pfxlist_onlink_check: failed to "
 1702                                     "make %s/%d onlink, errno=%d\n",
 1703                                     ip6_sprintf(ip6buf,
 1704                                             &pr->ndpr_prefix.sin6_addr),
 1705                                             pr->ndpr_plen, e));
 1706                         }
 1707                         ND6_RLOCK();
 1708                         if (genid != V_nd6_list_genid)
 1709                                 goto restart;
 1710                 }
 1711         }
 1712 
 1713         /*
 1714          * Changes on the prefix status might affect address status as well.
 1715          * Make sure that all addresses derived from an attached prefix are
 1716          * attached, and that all addresses derived from a detached prefix are
 1717          * detached.  Note, however, that a manually configured address should
 1718          * always be attached.
 1719          * The precise detection logic is same as the one for prefixes.
 1720          */
 1721         IN6_IFADDR_RLOCK(&in6_ifa_tracker);
 1722         CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
 1723                 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF))
 1724                         continue;
 1725 
 1726                 if (ifa->ia6_ndpr == NULL) {
 1727                         /*
 1728                          * This can happen when we first configure the address
 1729                          * (i.e. the address exists, but the prefix does not).
 1730                          * XXX: complicated relationships...
 1731                          */
 1732                         continue;
 1733                 }
 1734 
 1735                 if (find_pfxlist_reachable_router(ifa->ia6_ndpr))
 1736                         break;
 1737         }
 1738         if (ifa) {
 1739                 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
 1740                         if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
 1741                                 continue;
 1742 
 1743                         if (ifa->ia6_ndpr == NULL) /* XXX: see above. */
 1744                                 continue;
 1745 
 1746                         if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) {
 1747                                 if (ifa->ia6_flags & IN6_IFF_DETACHED) {
 1748                                         ifa->ia6_flags &= ~IN6_IFF_DETACHED;
 1749                                         ifa->ia6_flags |= IN6_IFF_TENTATIVE;
 1750                                         nd6_dad_start((struct ifaddr *)ifa, 0);
 1751                                 }
 1752                         } else {
 1753                                 ifa->ia6_flags |= IN6_IFF_DETACHED;
 1754                         }
 1755                 }
 1756         } else {
 1757                 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) {
 1758                         if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0)
 1759                                 continue;
 1760 
 1761                         if (ifa->ia6_flags & IN6_IFF_DETACHED) {
 1762                                 ifa->ia6_flags &= ~IN6_IFF_DETACHED;
 1763                                 ifa->ia6_flags |= IN6_IFF_TENTATIVE;
 1764                                 /* Do we need a delay in this case? */
 1765                                 nd6_dad_start((struct ifaddr *)ifa, 0);
 1766                         }
 1767                 }
 1768         }
 1769         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
 1770         ND6_RUNLOCK();
 1771         ND6_ONLINK_UNLOCK();
 1772 }
 1773 
 1774 static int
 1775 nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa)
 1776 {
 1777         static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
 1778         struct rib_head *rnh;
 1779         struct rtentry *rt;
 1780         struct sockaddr_in6 mask6;
 1781         u_long rtflags;
 1782         int error, a_failure, fibnum, maxfib;
 1783 
 1784         /*
 1785          * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs.
 1786          * ifa->ifa_rtrequest = nd6_rtrequest;
 1787          */
 1788         bzero(&mask6, sizeof(mask6));
 1789         mask6.sin6_len = sizeof(mask6);
 1790         mask6.sin6_addr = pr->ndpr_mask;
 1791         rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP;
 1792 
 1793         if(V_rt_add_addr_allfibs) {
 1794                 fibnum = 0;
 1795                 maxfib = rt_numfibs;
 1796         } else {
 1797                 fibnum = ifa->ifa_ifp->if_fib;
 1798                 maxfib = fibnum + 1;
 1799         }
 1800         a_failure = 0;
 1801         for (; fibnum < maxfib; fibnum++) {
 1802 
 1803                 rt = NULL;
 1804                 error = in6_rtrequest(RTM_ADD,
 1805                     (struct sockaddr *)&pr->ndpr_prefix, ifa->ifa_addr,
 1806                     (struct sockaddr *)&mask6, rtflags, &rt, fibnum);
 1807                 if (error == 0) {
 1808                         KASSERT(rt != NULL, ("%s: in6_rtrequest return no "
 1809                             "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__,
 1810                             error, pr, ifa));
 1811 
 1812                         rnh = rt_tables_get_rnh(rt->rt_fibnum, AF_INET6);
 1813                         /* XXX what if rhn == NULL? */
 1814                         RIB_WLOCK(rnh);
 1815                         RT_LOCK(rt);
 1816                         if (rt_setgate(rt, rt_key(rt),
 1817                             (struct sockaddr *)&null_sdl) == 0) {
 1818                                 struct sockaddr_dl *dl;
 1819 
 1820                                 dl = (struct sockaddr_dl *)rt->rt_gateway;
 1821                                 dl->sdl_type = rt->rt_ifp->if_type;
 1822                                 dl->sdl_index = rt->rt_ifp->if_index;
 1823                         }
 1824                         RIB_WUNLOCK(rnh);
 1825                         nd6_rtmsg(RTM_ADD, rt);
 1826                         RT_UNLOCK(rt);
 1827                         pr->ndpr_stateflags |= NDPRF_ONLINK;
 1828                 } else {
 1829                         char ip6buf[INET6_ADDRSTRLEN];
 1830                         char ip6bufg[INET6_ADDRSTRLEN];
 1831                         char ip6bufm[INET6_ADDRSTRLEN];
 1832                         struct sockaddr_in6 *sin6;
 1833 
 1834                         sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
 1835                         nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add "
 1836                             "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, "
 1837                             "flags=%lx errno = %d\n",
 1838                             ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
 1839                             pr->ndpr_plen, if_name(pr->ndpr_ifp),
 1840                             ip6_sprintf(ip6bufg, &sin6->sin6_addr),
 1841                             ip6_sprintf(ip6bufm, &mask6.sin6_addr),
 1842                             rtflags, error));
 1843 
 1844                         /* Save last error to return, see rtinit(). */
 1845                         a_failure = error;
 1846                 }
 1847 
 1848                 if (rt != NULL) {
 1849                         RT_LOCK(rt);
 1850                         RT_REMREF(rt);
 1851                         RT_UNLOCK(rt);
 1852                 }
 1853         }
 1854 
 1855         /* Return the last error we got. */
 1856         return (a_failure);
 1857 }
 1858 
 1859 int
 1860 nd6_prefix_onlink(struct nd_prefix *pr)
 1861 {
 1862         struct ifaddr *ifa;
 1863         struct ifnet *ifp = pr->ndpr_ifp;
 1864         struct nd_prefix *opr;
 1865         char ip6buf[INET6_ADDRSTRLEN];
 1866         int error;
 1867 
 1868         ND6_ONLINK_LOCK_ASSERT();
 1869         ND6_UNLOCK_ASSERT();
 1870 
 1871         if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0)
 1872                 return (EEXIST);
 1873 
 1874         /*
 1875          * Add the interface route associated with the prefix.  Before
 1876          * installing the route, check if there's the same prefix on another
 1877          * interface, and the prefix has already installed the interface route.
 1878          * Although such a configuration is expected to be rare, we explicitly
 1879          * allow it.
 1880          */
 1881         ND6_RLOCK();
 1882         LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
 1883                 if (opr == pr)
 1884                         continue;
 1885 
 1886                 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0)
 1887                         continue;
 1888 
 1889                 if (!V_rt_add_addr_allfibs &&
 1890                     opr->ndpr_ifp->if_fib != pr->ndpr_ifp->if_fib)
 1891                         continue;
 1892 
 1893                 if (opr->ndpr_plen == pr->ndpr_plen &&
 1894                     in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
 1895                     &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
 1896                         ND6_RUNLOCK();
 1897                         return (0);
 1898                 }
 1899         }
 1900         ND6_RUNLOCK();
 1901 
 1902         /*
 1903          * We prefer link-local addresses as the associated interface address.
 1904          */
 1905         /* search for a link-local addr */
 1906         ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
 1907             IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
 1908         if (ifa == NULL) {
 1909                 /* XXX: freebsd does not have ifa_ifwithaf */
 1910                 IF_ADDR_RLOCK(ifp);
 1911                 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
 1912                         if (ifa->ifa_addr->sa_family == AF_INET6) {
 1913                                 ifa_ref(ifa);
 1914                                 break;
 1915                         }
 1916                 }
 1917                 IF_ADDR_RUNLOCK(ifp);
 1918                 /* should we care about ia6_flags? */
 1919         }
 1920         if (ifa == NULL) {
 1921                 /*
 1922                  * This can still happen, when, for example, we receive an RA
 1923                  * containing a prefix with the L bit set and the A bit clear,
 1924                  * after removing all IPv6 addresses on the receiving
 1925                  * interface.  This should, of course, be rare though.
 1926                  */
 1927                 nd6log((LOG_NOTICE,
 1928                     "nd6_prefix_onlink: failed to find any ifaddr"
 1929                     " to add route for a prefix(%s/%d) on %s\n",
 1930                     ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr),
 1931                     pr->ndpr_plen, if_name(ifp)));
 1932                 return (0);
 1933         }
 1934 
 1935         error = nd6_prefix_onlink_rtrequest(pr, ifa);
 1936 
 1937         if (ifa != NULL)
 1938                 ifa_free(ifa);
 1939 
 1940         return (error);
 1941 }
 1942 
 1943 int
 1944 nd6_prefix_offlink(struct nd_prefix *pr)
 1945 {
 1946         int error = 0;
 1947         struct ifnet *ifp = pr->ndpr_ifp;
 1948         struct nd_prefix *opr;
 1949         struct sockaddr_in6 sa6, mask6;
 1950         struct rtentry *rt;
 1951         char ip6buf[INET6_ADDRSTRLEN];
 1952         uint64_t genid;
 1953         int fibnum, maxfib, a_failure;
 1954 
 1955         ND6_ONLINK_LOCK_ASSERT();
 1956         ND6_UNLOCK_ASSERT();
 1957 
 1958         if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0)
 1959                 return (EEXIST);
 1960 
 1961         bzero(&sa6, sizeof(sa6));
 1962         sa6.sin6_family = AF_INET6;
 1963         sa6.sin6_len = sizeof(sa6);
 1964         bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
 1965             sizeof(struct in6_addr));
 1966         bzero(&mask6, sizeof(mask6));
 1967         mask6.sin6_family = AF_INET6;
 1968         mask6.sin6_len = sizeof(sa6);
 1969         bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
 1970 
 1971         if (V_rt_add_addr_allfibs) {
 1972                 fibnum = 0;
 1973                 maxfib = rt_numfibs;
 1974         } else {
 1975                 fibnum = ifp->if_fib;
 1976                 maxfib = fibnum + 1;
 1977         }
 1978 
 1979         a_failure = 0;
 1980         for (; fibnum < maxfib; fibnum++) {
 1981                 rt = NULL;
 1982                 error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
 1983                     (struct sockaddr *)&mask6, 0, &rt, fibnum);
 1984                 if (error == 0) {
 1985                         /* report the route deletion to the routing socket. */
 1986                         if (rt != NULL)
 1987                                 nd6_rtmsg(RTM_DELETE, rt);
 1988                 } else {
 1989                         /* Save last error to return, see rtinit(). */
 1990                         a_failure = error;
 1991                 }
 1992                 if (rt != NULL) {
 1993                         RTFREE(rt);
 1994                 }
 1995         }
 1996         error = a_failure;
 1997         a_failure = 1;
 1998         if (error == 0) {
 1999                 pr->ndpr_stateflags &= ~NDPRF_ONLINK;
 2000 
 2001                 /*
 2002                  * There might be the same prefix on another interface,
 2003                  * the prefix which could not be on-link just because we have
 2004                  * the interface route (see comments in nd6_prefix_onlink).
 2005                  * If there's one, try to make the prefix on-link on the
 2006                  * interface.
 2007                  */
 2008                 ND6_RLOCK();
 2009 restart:
 2010                 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) {
 2011                         /*
 2012                          * KAME specific: detached prefixes should not be
 2013                          * on-link.
 2014                          */
 2015                         if (opr == pr || (opr->ndpr_stateflags &
 2016                             (NDPRF_ONLINK | NDPRF_DETACHED)) != 0)
 2017                                 continue;
 2018 
 2019                         if (opr->ndpr_plen == pr->ndpr_plen &&
 2020                             in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
 2021                             &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) {
 2022                                 int e;
 2023 
 2024                                 genid = V_nd6_list_genid;
 2025                                 ND6_RUNLOCK();
 2026                                 if ((e = nd6_prefix_onlink(opr)) != 0) {
 2027                                         nd6log((LOG_ERR,
 2028                                             "nd6_prefix_offlink: failed to "
 2029                                             "recover a prefix %s/%d from %s "
 2030                                             "to %s (errno = %d)\n",
 2031                                             ip6_sprintf(ip6buf,
 2032                                                 &opr->ndpr_prefix.sin6_addr),
 2033                                             opr->ndpr_plen, if_name(ifp),
 2034                                             if_name(opr->ndpr_ifp), e));
 2035                                 } else
 2036                                         a_failure = 0;
 2037                                 ND6_RLOCK();
 2038                                 if (genid != V_nd6_list_genid)
 2039                                         goto restart;
 2040                         }
 2041                 }
 2042                 ND6_RUNLOCK();
 2043         } else {
 2044                 /* XXX: can we still set the NDPRF_ONLINK flag? */
 2045                 nd6log((LOG_ERR,
 2046                     "nd6_prefix_offlink: failed to delete route: "
 2047                     "%s/%d on %s (errno = %d)\n",
 2048                     ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen,
 2049                     if_name(ifp), error));
 2050         }
 2051 
 2052         if (a_failure)
 2053                 lltable_prefix_free(AF_INET6, (struct sockaddr *)&sa6,
 2054                     (struct sockaddr *)&mask6, LLE_STATIC);
 2055 
 2056         return (error);
 2057 }
 2058 
 2059 static struct in6_ifaddr *
 2060 in6_ifadd(struct nd_prefixctl *pr, int mcast)
 2061 {
 2062         struct ifnet *ifp = pr->ndpr_ifp;
 2063         struct ifaddr *ifa;
 2064         struct in6_aliasreq ifra;
 2065         struct in6_ifaddr *ia, *ib;
 2066         int error, plen0;
 2067         struct in6_addr mask;
 2068         int prefixlen = pr->ndpr_plen;
 2069         int updateflags;
 2070         char ip6buf[INET6_ADDRSTRLEN];
 2071 
 2072         in6_prefixlen2mask(&mask, prefixlen);
 2073 
 2074         /*
 2075          * find a link-local address (will be interface ID).
 2076          * Is it really mandatory? Theoretically, a global or a site-local
 2077          * address can be configured without a link-local address, if we
 2078          * have a unique interface identifier...
 2079          *
 2080          * it is not mandatory to have a link-local address, we can generate
 2081          * interface identifier on the fly.  we do this because:
 2082          * (1) it should be the easiest way to find interface identifier.
 2083          * (2) RFC2462 5.4 suggesting the use of the same interface identifier
 2084          * for multiple addresses on a single interface, and possible shortcut
 2085          * of DAD.  we omitted DAD for this reason in the past.
 2086          * (3) a user can prevent autoconfiguration of global address
 2087          * by removing link-local address by hand (this is partly because we
 2088          * don't have other way to control the use of IPv6 on an interface.
 2089          * this has been our design choice - cf. NRL's "ifconfig auto").
 2090          * (4) it is easier to manage when an interface has addresses
 2091          * with the same interface identifier, than to have multiple addresses
 2092          * with different interface identifiers.
 2093          */
 2094         ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */
 2095         if (ifa)
 2096                 ib = (struct in6_ifaddr *)ifa;
 2097         else
 2098                 return NULL;
 2099 
 2100         /* prefixlen + ifidlen must be equal to 128 */
 2101         plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL);
 2102         if (prefixlen != plen0) {
 2103                 ifa_free(ifa);
 2104                 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s "
 2105                     "(prefix=%d ifid=%d)\n",
 2106                     if_name(ifp), prefixlen, 128 - plen0));
 2107                 return NULL;
 2108         }
 2109 
 2110         /* make ifaddr */
 2111         in6_prepare_ifra(&ifra, &pr->ndpr_prefix.sin6_addr, &mask);
 2112 
 2113         IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, &mask);
 2114         /* interface ID */
 2115         ifra.ifra_addr.sin6_addr.s6_addr32[0] |=
 2116             (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
 2117         ifra.ifra_addr.sin6_addr.s6_addr32[1] |=
 2118             (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
 2119         ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
 2120             (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
 2121         ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
 2122             (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
 2123         ifa_free(ifa);
 2124 
 2125         /* lifetimes. */
 2126         ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime;
 2127         ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime;
 2128 
 2129         /* XXX: scope zone ID? */
 2130 
 2131         ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */
 2132 
 2133         /*
 2134          * Make sure that we do not have this address already.  This should
 2135          * usually not happen, but we can still see this case, e.g., if we
 2136          * have manually configured the exact address to be configured.
 2137          */
 2138         ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
 2139             &ifra.ifra_addr.sin6_addr);
 2140         if (ifa != NULL) {
 2141                 ifa_free(ifa);
 2142                 /* this should be rare enough to make an explicit log */
 2143                 log(LOG_INFO, "in6_ifadd: %s is already configured\n",
 2144                     ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr));
 2145                 return (NULL);
 2146         }
 2147 
 2148         /*
 2149          * Allocate ifaddr structure, link into chain, etc.
 2150          * If we are going to create a new address upon receiving a multicasted
 2151          * RA, we need to impose a random delay before starting DAD.
 2152          * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2]
 2153          */
 2154         updateflags = 0;
 2155         if (mcast)
 2156                 updateflags |= IN6_IFAUPDATE_DADDELAY;
 2157         if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) {
 2158                 nd6log((LOG_ERR,
 2159                     "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n",
 2160                     ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr),
 2161                     if_name(ifp), error));
 2162                 return (NULL);  /* ifaddr must not have been allocated. */
 2163         }
 2164 
 2165         ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
 2166         /*
 2167          * XXXRW: Assumption of non-NULLness here might not be true with
 2168          * fine-grained locking -- should we validate it?  Or just return
 2169          * earlier ifa rather than looking it up again?
 2170          */
 2171         return (ia);            /* this is always non-NULL  and referenced. */
 2172 }
 2173 
 2174 /*
 2175  * ia0 - corresponding public address
 2176  */
 2177 int
 2178 in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay)
 2179 {
 2180         struct ifnet *ifp = ia0->ia_ifa.ifa_ifp;
 2181         struct in6_ifaddr *newia;
 2182         struct in6_aliasreq ifra;
 2183         int error;
 2184         int trylimit = 3;       /* XXX: adhoc value */
 2185         int updateflags;
 2186         u_int32_t randid[2];
 2187         time_t vltime0, pltime0;
 2188 
 2189         in6_prepare_ifra(&ifra, &ia0->ia_addr.sin6_addr,
 2190             &ia0->ia_prefixmask.sin6_addr);
 2191 
 2192         ifra.ifra_addr = ia0->ia_addr;  /* XXX: do we need this ? */
 2193         /* clear the old IFID */
 2194         IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr,
 2195             &ifra.ifra_prefixmask.sin6_addr);
 2196 
 2197   again:
 2198         if (in6_get_tmpifid(ifp, (u_int8_t *)randid,
 2199             (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) {
 2200                 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good "
 2201                     "random IFID\n"));
 2202                 return (EINVAL);
 2203         }
 2204         ifra.ifra_addr.sin6_addr.s6_addr32[2] |=
 2205             (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2]));
 2206         ifra.ifra_addr.sin6_addr.s6_addr32[3] |=
 2207             (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3]));
 2208 
 2209         /*
 2210          * in6_get_tmpifid() quite likely provided a unique interface ID.
 2211          * However, we may still have a chance to see collision, because
 2212          * there may be a time lag between generation of the ID and generation
 2213          * of the address.  So, we'll do one more sanity check.
 2214          */
 2215 
 2216         if (in6_localip(&ifra.ifra_addr.sin6_addr) != 0) {
 2217                 if (trylimit-- > 0) {
 2218                         forcegen = 1;
 2219                         goto again;
 2220                 }
 2221 
 2222                 /* Give up.  Something strange should have happened.  */
 2223                 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to "
 2224                     "find a unique random IFID\n"));
 2225                 return (EEXIST);
 2226         }
 2227 
 2228         /*
 2229          * The Valid Lifetime is the lower of the Valid Lifetime of the
 2230          * public address or TEMP_VALID_LIFETIME.
 2231          * The Preferred Lifetime is the lower of the Preferred Lifetime
 2232          * of the public address or TEMP_PREFERRED_LIFETIME -
 2233          * DESYNC_FACTOR.
 2234          */
 2235         if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
 2236                 vltime0 = IFA6_IS_INVALID(ia0) ? 0 :
 2237                     (ia0->ia6_lifetime.ia6t_vltime -
 2238                     (time_uptime - ia0->ia6_updatetime));
 2239                 if (vltime0 > V_ip6_temp_valid_lifetime)
 2240                         vltime0 = V_ip6_temp_valid_lifetime;
 2241         } else
 2242                 vltime0 = V_ip6_temp_valid_lifetime;
 2243         if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
 2244                 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 :
 2245                     (ia0->ia6_lifetime.ia6t_pltime -
 2246                     (time_uptime - ia0->ia6_updatetime));
 2247                 if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){
 2248                         pltime0 = V_ip6_temp_preferred_lifetime -
 2249                             V_ip6_desync_factor;
 2250                 }
 2251         } else
 2252                 pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor;
 2253         ifra.ifra_lifetime.ia6t_vltime = vltime0;
 2254         ifra.ifra_lifetime.ia6t_pltime = pltime0;
 2255 
 2256         /*
 2257          * A temporary address is created only if this calculated Preferred
 2258          * Lifetime is greater than REGEN_ADVANCE time units.
 2259          */
 2260         if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance)
 2261                 return (0);
 2262 
 2263         /* XXX: scope zone ID? */
 2264 
 2265         ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY);
 2266 
 2267         /* allocate ifaddr structure, link into chain, etc. */
 2268         updateflags = 0;
 2269         if (delay)
 2270                 updateflags |= IN6_IFAUPDATE_DADDELAY;
 2271         if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0)
 2272                 return (error);
 2273 
 2274         newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr);
 2275         if (newia == NULL) {    /* XXX: can it happen? */
 2276                 nd6log((LOG_ERR,
 2277                     "in6_tmpifadd: ifa update succeeded, but we got "
 2278                     "no ifaddr\n"));
 2279                 return (EINVAL); /* XXX */
 2280         }
 2281         newia->ia6_ndpr = ia0->ia6_ndpr;
 2282         newia->ia6_ndpr->ndpr_addrcnt++;
 2283         ifa_free(&newia->ia_ifa);
 2284 
 2285         /*
 2286          * A newly added address might affect the status of other addresses.
 2287          * XXX: when the temporary address is generated with a new public
 2288          * address, the onlink check is redundant.  However, it would be safe
 2289          * to do the check explicitly everywhere a new address is generated,
 2290          * and, in fact, we surely need the check when we create a new
 2291          * temporary address due to deprecation of an old temporary address.
 2292          */
 2293         pfxlist_onlink_check();
 2294 
 2295         return (0);
 2296 }
 2297 
 2298 static int
 2299 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
 2300 {
 2301         if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
 2302                 ndpr->ndpr_preferred = 0;
 2303         else
 2304                 ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime;
 2305         if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
 2306                 ndpr->ndpr_expire = 0;
 2307         else
 2308                 ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime;
 2309 
 2310         return 0;
 2311 }
 2312 
 2313 static void
 2314 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6)
 2315 {
 2316         /* init ia6t_expire */
 2317         if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
 2318                 lt6->ia6t_expire = 0;
 2319         else {
 2320                 lt6->ia6t_expire = time_uptime;
 2321                 lt6->ia6t_expire += lt6->ia6t_vltime;
 2322         }
 2323 
 2324         /* init ia6t_preferred */
 2325         if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
 2326                 lt6->ia6t_preferred = 0;
 2327         else {
 2328                 lt6->ia6t_preferred = time_uptime;
 2329                 lt6->ia6t_preferred += lt6->ia6t_pltime;
 2330         }
 2331 }
 2332 
 2333 /*
 2334  * Delete all the routing table entries that use the specified gateway.
 2335  * XXX: this function causes search through all entries of routing table, so
 2336  * it shouldn't be called when acting as a router.
 2337  */
 2338 void
 2339 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp)
 2340 {
 2341 
 2342         /* We'll care only link-local addresses */
 2343         if (!IN6_IS_ADDR_LINKLOCAL(gateway))
 2344                 return;
 2345 
 2346         /* XXX Do we really need to walk any but the default FIB? */
 2347         rt_foreach_fib_walk_del(AF_INET6, rt6_deleteroute, (void *)gateway);
 2348 }
 2349 
 2350 static int
 2351 rt6_deleteroute(const struct rtentry *rt, void *arg)
 2352 {
 2353 #define SIN6(s) ((struct sockaddr_in6 *)s)
 2354         struct in6_addr *gate = (struct in6_addr *)arg;
 2355 
 2356         if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
 2357                 return (0);
 2358 
 2359         if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) {
 2360                 return (0);
 2361         }
 2362 
 2363         /*
 2364          * Do not delete a static route.
 2365          * XXX: this seems to be a bit ad-hoc. Should we consider the
 2366          * 'cloned' bit instead?
 2367          */
 2368         if ((rt->rt_flags & RTF_STATIC) != 0)
 2369                 return (0);
 2370 
 2371         /*
 2372          * We delete only host route. This means, in particular, we don't
 2373          * delete default route.
 2374          */
 2375         if ((rt->rt_flags & RTF_HOST) == 0)
 2376                 return (0);
 2377 
 2378         return (1);
 2379 #undef SIN6
 2380 }
 2381 
 2382 int
 2383 nd6_setdefaultiface(int ifindex)
 2384 {
 2385         int error = 0;
 2386 
 2387         if (ifindex < 0 || V_if_index < ifindex)
 2388                 return (EINVAL);
 2389         if (ifindex != 0 && !ifnet_byindex(ifindex))
 2390                 return (EINVAL);
 2391 
 2392         if (V_nd6_defifindex != ifindex) {
 2393                 V_nd6_defifindex = ifindex;
 2394                 if (V_nd6_defifindex > 0)
 2395                         V_nd6_defifp = ifnet_byindex(V_nd6_defifindex);
 2396                 else
 2397                         V_nd6_defifp = NULL;
 2398 
 2399                 /*
 2400                  * Our current implementation assumes one-to-one maping between
 2401                  * interfaces and links, so it would be natural to use the
 2402                  * default interface as the default link.
 2403                  */
 2404                 scope6_setdefault(V_nd6_defifp);
 2405         }
 2406 
 2407         return (error);
 2408 }

Cache object: dac4a17289ef9ad519e062413815b1a0


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