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