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

Cache object: 3f76f2e5ec4d128e3b2281aab36058df


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