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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: f8da7165d2997659fdec6b9e3eea082b


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