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


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

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

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

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

Cache object: 009d51fbbcee787498946e3530dbcd81


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