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