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


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

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

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

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

Cache object: bfff14af877255aec782c07c5dbd5e80


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