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


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

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

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

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

Cache object: f553e7ec7cceef3bccbe9ca93bef606f


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