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


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

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

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

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

Cache object: da040dc377d746787ac0c943189d3d27


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