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