Home | History | Annotate | Line # | Download | only in netinet6
nd6_rtr.c revision 1.22
      1 /*	$NetBSD: nd6_rtr.c,v 1.22 2001/04/04 06:28:41 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/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/malloc.h>
     36 #include <sys/mbuf.h>
     37 #include <sys/socket.h>
     38 #include <sys/sockio.h>
     39 #include <sys/time.h>
     40 #include <sys/kernel.h>
     41 #include <sys/errno.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/syslog.h>
     44 
     45 #include <net/if.h>
     46 #include <net/if_types.h>
     47 #include <net/if_dl.h>
     48 #include <net/route.h>
     49 #include <net/radix.h>
     50 
     51 #include <netinet/in.h>
     52 #include <netinet6/in6_var.h>
     53 #include <netinet/ip6.h>
     54 #include <netinet6/ip6_var.h>
     55 #include <netinet6/nd6.h>
     56 #include <netinet/icmp6.h>
     57 
     58 #include <net/net_osdep.h>
     59 
     60 #define SDL(s)	((struct sockaddr_dl *)s)
     61 
     62 static struct nd_defrouter *defrtrlist_update __P((struct nd_defrouter *));
     63 static int prelist_add __P((struct nd_prefix *, struct nd_defrouter *));
     64 static struct nd_prefix *prefix_lookup __P((struct nd_prefix *));
     65 static struct in6_ifaddr *in6_ifadd __P((struct ifnet *, struct in6_addr *,
     66 			  struct in6_addr *, int));
     67 static struct nd_pfxrouter *pfxrtr_lookup __P((struct nd_prefix *,
     68 					       struct nd_defrouter *));
     69 static void pfxrtr_add __P((struct nd_prefix *, struct nd_defrouter *));
     70 static void pfxrtr_del __P((struct nd_pfxrouter *));
     71 static struct nd_pfxrouter *find_pfxlist_reachable_router __P((struct nd_prefix *));
     72 static void nd6_detach_prefix __P((struct nd_prefix *));
     73 static void nd6_attach_prefix __P((struct nd_prefix *));
     74 static void defrouter_addifreq __P((struct ifnet *));
     75 
     76 static void in6_init_address_ltimes __P((struct nd_prefix *ndpr,
     77 					 struct in6_addrlifetime *lt6,
     78 					 int update_vltime));
     79 
     80 static int rt6_deleteroute __P((struct radix_node *, void *));
     81 
     82 extern int nd6_recalc_reachtm_interval;
     83 
     84 struct ifnet *nd6_defifp;
     85 int nd6_defifindex;
     86 
     87 /*
     88  * Receive Router Solicitation Message - just for routers.
     89  * Router solicitation/advertisement is mostly managed by userland program
     90  * (rtadvd) so here we have no function like nd6_ra_output().
     91  *
     92  * Based on RFC 2461
     93  */
     94 void
     95 nd6_rs_input(m, off, icmp6len)
     96 	struct	mbuf *m;
     97 	int off, icmp6len;
     98 {
     99 	struct ifnet *ifp = m->m_pkthdr.rcvif;
    100 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    101 	struct nd_router_solicit *nd_rs;
    102 	struct in6_addr saddr6 = ip6->ip6_src;
    103 #if 0
    104 	struct in6_addr daddr6 = ip6->ip6_dst;
    105 #endif
    106 	char *lladdr = NULL;
    107 	int lladdrlen = 0;
    108 #if 0
    109 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)NULL;
    110 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
    111 	struct rtentry *rt = NULL;
    112 	int is_newentry;
    113 #endif
    114 	union nd_opts ndopts;
    115 
    116 	/* If I'm not a router, ignore it. */
    117 	if (ip6_accept_rtadv != 0 || ip6_forwarding != 1)
    118 		goto freeit;
    119 
    120 	/* Sanity checks */
    121 	if (ip6->ip6_hlim != 255) {
    122 		nd6log((LOG_ERR,
    123 		    "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n",
    124 		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
    125 		    ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
    126 		goto bad;
    127 	}
    128 
    129 	/*
    130 	 * Don't update the neighbor cache, if src = ::.
    131 	 * This indicates that the src has no IP address assigned yet.
    132 	 */
    133 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
    134 		goto freeit;
    135 
    136 #ifndef PULLDOWN_TEST
    137 	IP6_EXTHDR_CHECK(m, off, icmp6len,);
    138 	nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off);
    139 #else
    140 	IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len);
    141 	if (nd_rs == NULL) {
    142 		icmp6stat.icp6s_tooshort++;
    143 		return;
    144 	}
    145 #endif
    146 
    147 	icmp6len -= sizeof(*nd_rs);
    148 	nd6_option_init(nd_rs + 1, icmp6len, &ndopts);
    149 	if (nd6_options(&ndopts) < 0) {
    150 		nd6log((LOG_INFO,
    151 		    "nd6_rs_input: invalid ND option, ignored\n"));
    152 		/* nd6_options have incremented stats */
    153 		goto freeit;
    154 	}
    155 
    156 	if (ndopts.nd_opts_src_lladdr) {
    157 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
    158 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
    159 	}
    160 
    161 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
    162 		nd6log((LOG_INFO,
    163 		    "nd6_rs_input: lladdrlen mismatch for %s "
    164 		    "(if %d, RS packet %d)\n",
    165 			ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2));
    166 		goto bad;
    167 	}
    168 
    169 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0);
    170 
    171  freeit:
    172 	m_freem(m);
    173 	return;
    174 
    175  bad:
    176 	icmp6stat.icp6s_badrs++;
    177 	m_freem(m);
    178 }
    179 
    180 /*
    181  * Receive Router Advertisement Message.
    182  *
    183  * Based on RFC 2461
    184  * TODO: on-link bit on prefix information
    185  * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing
    186  */
    187 void
    188 nd6_ra_input(m, off, icmp6len)
    189 	struct	mbuf *m;
    190 	int off, icmp6len;
    191 {
    192 	struct ifnet *ifp = m->m_pkthdr.rcvif;
    193 	struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
    194 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    195 	struct nd_router_advert *nd_ra;
    196 	struct in6_addr saddr6 = ip6->ip6_src;
    197 #if 0
    198 	struct in6_addr daddr6 = ip6->ip6_dst;
    199 	int flags; /* = nd_ra->nd_ra_flags_reserved; */
    200 	int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0);
    201 	int is_other = ((flags & ND_RA_FLAG_OTHER) != 0);
    202 #endif
    203 	union nd_opts ndopts;
    204 	struct nd_defrouter *dr;
    205 
    206 	if (ip6_accept_rtadv == 0)
    207 		goto freeit;
    208 
    209 	if (ip6->ip6_hlim != 255) {
    210 		nd6log((LOG_ERR,
    211 		    "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n",
    212 		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
    213 		    ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
    214 		goto bad;
    215 	}
    216 
    217 	if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) {
    218 		nd6log((LOG_ERR,
    219 		    "nd6_ra_input: src %s is not link-local\n",
    220 		    ip6_sprintf(&saddr6)));
    221 		goto bad;
    222 	}
    223 
    224 #ifndef PULLDOWN_TEST
    225 	IP6_EXTHDR_CHECK(m, off, icmp6len,);
    226 	nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off);
    227 #else
    228 	IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len);
    229 	if (nd_ra == NULL) {
    230 		icmp6stat.icp6s_tooshort++;
    231 		return;
    232 	}
    233 #endif
    234 
    235 	icmp6len -= sizeof(*nd_ra);
    236 	nd6_option_init(nd_ra + 1, icmp6len, &ndopts);
    237 	if (nd6_options(&ndopts) < 0) {
    238 		nd6log((LOG_INFO,
    239 		    "nd6_ra_input: invalid ND option, ignored\n"));
    240 		/* nd6_options have incremented stats */
    241 		goto freeit;
    242 	}
    243 
    244     {
    245 	struct nd_defrouter dr0;
    246 	u_int32_t advreachable = nd_ra->nd_ra_reachable;
    247 	long time_second = time.tv_sec;
    248 
    249 	dr0.rtaddr = saddr6;
    250 	dr0.flags  = nd_ra->nd_ra_flags_reserved;
    251 	dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime);
    252 	dr0.expire = time_second + dr0.rtlifetime;
    253 	dr0.ifp = ifp;
    254 	/* unspecified or not? (RFC 2461 6.3.4) */
    255 	if (advreachable) {
    256 		NTOHL(advreachable);
    257 		if (advreachable <= MAX_REACHABLE_TIME &&
    258 		    ndi->basereachable != advreachable) {
    259 			ndi->basereachable = advreachable;
    260 			ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
    261 			ndi->recalctm = nd6_recalc_reachtm_interval; /* reset */
    262 		}
    263 	}
    264 	if (nd_ra->nd_ra_retransmit)
    265 		ndi->retrans = ntohl(nd_ra->nd_ra_retransmit);
    266 	if (nd_ra->nd_ra_curhoplimit)
    267 		ndi->chlim = nd_ra->nd_ra_curhoplimit;
    268 	dr = defrtrlist_update(&dr0);
    269     }
    270 
    271 	/*
    272 	 * prefix
    273 	 */
    274 	if (ndopts.nd_opts_pi) {
    275 		struct nd_opt_hdr *pt;
    276 		struct nd_opt_prefix_info *pi;
    277 		struct nd_prefix pr;
    278 
    279 		for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi;
    280 		     pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end;
    281 		     pt = (struct nd_opt_hdr *)((caddr_t)pt +
    282 						(pt->nd_opt_len << 3))) {
    283 			if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION)
    284 				continue;
    285 			pi = (struct nd_opt_prefix_info *)pt;
    286 
    287 			if (pi->nd_opt_pi_len != 4) {
    288 				nd6log((LOG_INFO,
    289 				    "nd6_ra_input: invalid option "
    290 				    "len %d for prefix information option, "
    291 				    "ignored\n", pi->nd_opt_pi_len));
    292 				continue;
    293 			}
    294 
    295 			if (128 < pi->nd_opt_pi_prefix_len) {
    296 				nd6log((LOG_INFO,
    297 				    "nd6_ra_input: invalid prefix "
    298 				    "len %d for prefix information option, "
    299 				    "ignored\n", pi->nd_opt_pi_prefix_len));
    300 				continue;
    301 			}
    302 
    303 			if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix)
    304 			 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) {
    305 				nd6log((LOG_INFO,
    306 				    "nd6_ra_input: invalid prefix "
    307 				    "%s, ignored\n",
    308 				    ip6_sprintf(&pi->nd_opt_pi_prefix)));
    309 				continue;
    310 			}
    311 
    312 			/* aggregatable unicast address, rfc2374 */
    313 			if ((pi->nd_opt_pi_prefix.s6_addr8[0] & 0xe0) == 0x20
    314 			 && pi->nd_opt_pi_prefix_len != 64) {
    315 				nd6log((LOG_INFO,
    316 				    "nd6_ra_input: invalid prefixlen "
    317 				    "%d for rfc2374 prefix %s, ignored\n",
    318 				    pi->nd_opt_pi_prefix_len,
    319 				    ip6_sprintf(&pi->nd_opt_pi_prefix)));
    320 				continue;
    321 			}
    322 
    323 			bzero(&pr, sizeof(pr));
    324 			pr.ndpr_prefix.sin6_family = AF_INET6;
    325 			pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix);
    326 			pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix;
    327 			pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif;
    328 
    329 			pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved &
    330 					      ND_OPT_PI_FLAG_ONLINK) ? 1 : 0;
    331 			pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved &
    332 					    ND_OPT_PI_FLAG_AUTO) ? 1 : 0;
    333 			pr.ndpr_plen = pi->nd_opt_pi_prefix_len;
    334 			pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time);
    335 			pr.ndpr_pltime =
    336 				ntohl(pi->nd_opt_pi_preferred_time);
    337 
    338 			if (in6_init_prefix_ltimes(&pr))
    339 				continue; /* prefix lifetime init failed */
    340 
    341 			(void)prelist_update(&pr, dr, m);
    342 		}
    343 	}
    344 
    345 	/*
    346 	 * MTU
    347 	 */
    348 	if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) {
    349 		u_int32_t mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
    350 
    351 		/* lower bound */
    352 		if (mtu < IPV6_MMTU) {
    353 			nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option "
    354 			    "mtu=%d sent from %s, ignoring\n",
    355 			    mtu, ip6_sprintf(&ip6->ip6_src)));
    356 			goto skip;
    357 		}
    358 
    359 		/* upper bound */
    360 		if (ndi->maxmtu) {
    361 			if (mtu <= ndi->maxmtu) {
    362 				int change = (ndi->linkmtu != mtu);
    363 
    364 				ndi->linkmtu = mtu;
    365 				if (change) /* in6_maxmtu may change */
    366 					in6_setmaxmtu();
    367 			} else {
    368 				nd6log((LOG_INFO, "nd6_ra_input: bogus mtu "
    369 				    "mtu=%d sent from %s; "
    370 				    "exceeds maxmtu %d, ignoring\n",
    371 				    mtu, ip6_sprintf(&ip6->ip6_src),
    372 				    ndi->maxmtu));
    373 			}
    374 		} else {
    375 			nd6log((LOG_INFO, "nd6_ra_input: mtu option "
    376 			    "mtu=%d sent from %s; maxmtu unknown, "
    377 			    "ignoring\n",
    378 			    mtu, ip6_sprintf(&ip6->ip6_src)));
    379 		}
    380 	}
    381 
    382  skip:
    383 
    384 	/*
    385 	 * Src linkaddress
    386 	 */
    387     {
    388 	char *lladdr = NULL;
    389 	int lladdrlen = 0;
    390 
    391 	if (ndopts.nd_opts_src_lladdr) {
    392 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
    393 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
    394 	}
    395 
    396 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
    397 		nd6log((LOG_INFO,
    398 		    "nd6_ra_input: lladdrlen mismatch for %s "
    399 		    "(if %d, RA packet %d)\n",
    400 			ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2));
    401 		goto bad;
    402 	}
    403 
    404 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_ADVERT, 0);
    405 
    406 	/*
    407 	 * Installing a link-layer address might change the state of the
    408 	 * router's neighbor cache, which might also affect our on-link
    409 	 * detection of adveritsed prefixes.
    410 	 */
    411 	pfxlist_onlink_check();
    412     }
    413 
    414  freeit:
    415 	m_freem(m);
    416 	return;
    417 
    418  bad:
    419 	icmp6stat.icp6s_badra++;
    420 	m_freem(m);
    421 }
    422 
    423 /*
    424  * default router list proccessing sub routines
    425  */
    426 void
    427 defrouter_addreq(new)
    428 	struct nd_defrouter *new;
    429 {
    430 	struct sockaddr_in6 def, mask, gate;
    431 	int s;
    432 
    433 	Bzero(&def, sizeof(def));
    434 	Bzero(&mask, sizeof(mask));
    435 	Bzero(&gate, sizeof(gate));
    436 
    437 	def.sin6_len = mask.sin6_len = gate.sin6_len
    438 		= sizeof(struct sockaddr_in6);
    439 	def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
    440 	gate.sin6_addr = new->rtaddr;
    441 
    442 	s = splsoftnet();
    443 	(void)rtrequest(RTM_ADD, (struct sockaddr *)&def,
    444 		(struct sockaddr *)&gate, (struct sockaddr *)&mask,
    445 		RTF_GATEWAY, NULL);
    446 	splx(s);
    447 	return;
    448 }
    449 
    450 /* Add a route to a given interface as default */
    451 static void
    452 defrouter_addifreq(ifp)
    453 	struct ifnet *ifp;
    454 {
    455 	struct sockaddr_in6 def, mask;
    456 	struct ifaddr *ifa;
    457 	int error, flags;
    458 
    459 	bzero(&def, sizeof(def));
    460 	bzero(&mask, sizeof(mask));
    461 
    462 	def.sin6_len = mask.sin6_len = sizeof(struct sockaddr_in6);
    463 	def.sin6_family = mask.sin6_family = AF_INET6;
    464 
    465 	/*
    466 	 * Search for an ifaddr beloging to the specified interface.
    467 	 * XXX: An IPv6 address are required to be assigned on the interface.
    468 	 */
    469 	if ((ifa = ifaof_ifpforaddr((struct sockaddr *)&def, ifp)) == NULL) {
    470 		nd6log((LOG_ERR,	/* better error? */
    471 		    "defrouter_addifreq: failed to find an ifaddr "
    472 		    "to install a route to interface %s\n",
    473 		    if_name(ifp)));
    474 		return;
    475 	}
    476 
    477 	flags = ifa->ifa_flags;
    478 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0)
    479 		flags &= ~RTF_CLONING;
    480 	if ((error = rtrequest(RTM_ADD, (struct sockaddr *)&def,
    481 			       ifa->ifa_addr, (struct sockaddr *)&mask,
    482 			       flags, NULL)) != 0) {
    483 		nd6log((LOG_ERR,
    484 		    "defrouter_addifreq: failed to install a route to "
    485 		    "interface %s (errno = %d)\n",
    486 		    if_name(ifp), error));
    487 	}
    488 }
    489 
    490 struct nd_defrouter *
    491 defrouter_lookup(addr, ifp)
    492 	struct in6_addr *addr;
    493 	struct ifnet *ifp;
    494 {
    495 	struct nd_defrouter *dr;
    496 
    497 	for (dr = TAILQ_FIRST(&nd_defrouter); dr;
    498 	     dr = TAILQ_NEXT(dr, dr_entry)) {
    499 		if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr))
    500 			return(dr);
    501 	}
    502 
    503 	return(NULL);		/* search failed */
    504 }
    505 
    506 void
    507 defrouter_delreq(dr, dofree)
    508 	struct nd_defrouter *dr;
    509 	int dofree;
    510 {
    511 	struct sockaddr_in6 def, mask, gate;
    512 
    513 	Bzero(&def, sizeof(def));
    514 	Bzero(&mask, sizeof(mask));
    515 	Bzero(&gate, sizeof(gate));
    516 
    517 	def.sin6_len = mask.sin6_len = gate.sin6_len
    518 		= sizeof(struct sockaddr_in6);
    519 	def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6;
    520 	gate.sin6_addr = dr->rtaddr;
    521 
    522 	rtrequest(RTM_DELETE, (struct sockaddr *)&def,
    523 		  (struct sockaddr *)&gate,
    524 		  (struct sockaddr *)&mask,
    525 		  RTF_GATEWAY, (struct rtentry **)0);
    526 
    527 	if (dofree)		/* XXX: necessary? */
    528 		free(dr, M_IP6NDP);
    529 }
    530 
    531 void
    532 defrtrlist_del(dr)
    533 	struct nd_defrouter *dr;
    534 {
    535 	struct nd_defrouter *deldr = NULL;
    536 	struct nd_prefix *pr;
    537 
    538 	/*
    539 	 * Flush all the routing table entries that use the router
    540 	 * as a next hop.
    541 	 */
    542 	if (!ip6_forwarding && ip6_accept_rtadv) {
    543 		/* above is a good condition? */
    544 		rt6_flush(&dr->rtaddr, dr->ifp);
    545 	}
    546 
    547 	if (dr == TAILQ_FIRST(&nd_defrouter))
    548 		deldr = dr;	/* The router is primary. */
    549 
    550 	TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
    551 
    552 	/*
    553 	 * Also delete all the pointers to the router in each prefix lists.
    554 	 */
    555 	for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
    556 		struct nd_pfxrouter *pfxrtr;
    557 		if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL)
    558 			pfxrtr_del(pfxrtr);
    559 	}
    560 	pfxlist_onlink_check();
    561 
    562 	/*
    563 	 * If the router is the primary one, choose a new one.
    564 	 * Note that defrouter_select() will remove the current gateway
    565 	 * from the routing table.
    566 	 */
    567 	if (deldr)
    568 		defrouter_select();
    569 
    570 	free(dr, M_IP6NDP);
    571 }
    572 
    573 /*
    574  * Default Router Selection according to Section 6.3.6 of RFC 2461:
    575  * 1) Routers that are reachable or probably reachable should be
    576  *    preferred.
    577  * 2) When no routers on the list are known to be reachable or
    578  *    probably reachable, routers SHOULD be selected in a round-robin
    579  *    fashion.
    580  * 3) If the Default Router List is empty, assume that all
    581  *    destinations are on-link.
    582  */
    583 void
    584 defrouter_select()
    585 {
    586 	int s = splsoftnet();
    587 	struct nd_defrouter *dr, anydr;
    588 	struct rtentry *rt = NULL;
    589 	struct llinfo_nd6 *ln = NULL;
    590 
    591 	/*
    592 	 * Search for a (probably) reachable router from the list.
    593 	 */
    594 	for (dr = TAILQ_FIRST(&nd_defrouter); dr;
    595 	     dr = TAILQ_NEXT(dr, dr_entry)) {
    596 		if ((rt = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) &&
    597 		    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
    598 		    ND6_IS_LLINFO_PROBREACH(ln)) {
    599 			/* Got it, and move it to the head */
    600 			TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
    601 			TAILQ_INSERT_HEAD(&nd_defrouter, dr, dr_entry);
    602 			break;
    603 		}
    604 	}
    605 
    606 	if ((dr = TAILQ_FIRST(&nd_defrouter))) {
    607 		/*
    608 		 * De-install the previous default gateway and install
    609 		 * a new one.
    610 		 * Note that if there is no reachable router in the list,
    611 		 * the head entry will be used anyway.
    612 		 * XXX: do we have to check the current routing table entry?
    613 		 */
    614 		bzero(&anydr, sizeof(anydr));
    615 		defrouter_delreq(&anydr, 0);
    616 		defrouter_addreq(dr);
    617 	}
    618 	else {
    619 		/*
    620 		 * The Default Router List is empty, so install the default
    621 		 * route to an inteface.
    622 		 * XXX: The specification does not say this mechanism should
    623 		 * be restricted to hosts, but this would be not useful
    624 		 * (even harmful) for routers.
    625 		 */
    626 		if (!ip6_forwarding) {
    627 			/*
    628 			 * De-install the current default route
    629 			 * in advance.
    630 			 */
    631 			bzero(&anydr, sizeof(anydr));
    632 			defrouter_delreq(&anydr, 0);
    633 			if (nd6_defifp) {
    634 				/*
    635 				 * Install a route to the default interface
    636 				 * as default route.
    637 				 * XXX: we enable this for host only, because
    638 				 * this may override a default route installed
    639 				 * a user process (e.g. routing daemon) in a
    640 				 * router case.
    641 				 */
    642 				defrouter_addifreq(nd6_defifp);
    643 			} else {
    644 				nd6log((LOG_INFO, "defrouter_select: "
    645 				    "there's no default router and no default"
    646 				    " interface\n"));
    647 			}
    648 		}
    649 	}
    650 
    651 	splx(s);
    652 	return;
    653 }
    654 
    655 static struct nd_defrouter *
    656 defrtrlist_update(new)
    657 	struct nd_defrouter *new;
    658 {
    659 	struct nd_defrouter *dr, *n;
    660 	int s = splsoftnet();
    661 
    662 	if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) {
    663 		/* entry exists */
    664 		if (new->rtlifetime == 0) {
    665 			defrtrlist_del(dr);
    666 			dr = NULL;
    667 		} else {
    668 			/* override */
    669 			dr->flags = new->flags; /* xxx flag check */
    670 			dr->rtlifetime = new->rtlifetime;
    671 			dr->expire = new->expire;
    672 		}
    673 		splx(s);
    674 		return(dr);
    675 	}
    676 
    677 	/* entry does not exist */
    678 	if (new->rtlifetime == 0) {
    679 		splx(s);
    680 		return(NULL);
    681 	}
    682 
    683 	n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT);
    684 	if (n == NULL) {
    685 		splx(s);
    686 		return(NULL);
    687 	}
    688 	bzero(n, sizeof(*n));
    689 	*n = *new;
    690 
    691 	/*
    692 	 * Insert the new router at the end of the Default Router List.
    693 	 * If there is no other router, install it anyway. Otherwise,
    694 	 * just continue to use the current default router.
    695 	 */
    696 	TAILQ_INSERT_TAIL(&nd_defrouter, n, dr_entry);
    697 	if (TAILQ_FIRST(&nd_defrouter) == n)
    698 		defrouter_select();
    699 	splx(s);
    700 
    701 	return(n);
    702 }
    703 
    704 static struct nd_pfxrouter *
    705 pfxrtr_lookup(pr, dr)
    706 	struct nd_prefix *pr;
    707 	struct nd_defrouter *dr;
    708 {
    709 	struct nd_pfxrouter *search;
    710 
    711 	for (search = pr->ndpr_advrtrs.lh_first; search; search = search->pfr_next) {
    712 		if (search->router == dr)
    713 			break;
    714 	}
    715 
    716 	return(search);
    717 }
    718 
    719 static void
    720 pfxrtr_add(pr, dr)
    721 	struct nd_prefix *pr;
    722 	struct nd_defrouter *dr;
    723 {
    724 	struct nd_pfxrouter *new;
    725 
    726 	new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
    727 	if (new == NULL)
    728 		return;
    729 	bzero(new, sizeof(*new));
    730 	new->router = dr;
    731 
    732 	LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry);
    733 
    734 	pfxlist_onlink_check();
    735 }
    736 
    737 static void
    738 pfxrtr_del(pfr)
    739 	struct nd_pfxrouter *pfr;
    740 {
    741 	LIST_REMOVE(pfr, pfr_entry);
    742 	free(pfr, M_IP6NDP);
    743 }
    744 
    745 static struct nd_prefix *
    746 prefix_lookup(pr)
    747 	struct nd_prefix *pr;
    748 {
    749 	struct nd_prefix *search;
    750 
    751 	for (search = nd_prefix.lh_first; search; search = search->ndpr_next) {
    752 		if (pr->ndpr_ifp == search->ndpr_ifp &&
    753 		    pr->ndpr_plen == search->ndpr_plen &&
    754 		    in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr,
    755 					 &search->ndpr_prefix.sin6_addr,
    756 					 pr->ndpr_plen)
    757 		    ) {
    758 			break;
    759 		}
    760 	}
    761 
    762 	return(search);
    763 }
    764 
    765 static int
    766 prelist_add(pr, dr)
    767 	struct nd_prefix *pr;
    768 	struct nd_defrouter *dr;
    769 {
    770 	struct nd_prefix *new;
    771 	int i, s;
    772 
    773 	new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT);
    774 	if (new == NULL)
    775 		return ENOMEM;
    776 	bzero(new, sizeof(*new));
    777 	*new = *pr;
    778 
    779 	/* initilization */
    780 	new->ndpr_statef_onlink = pr->ndpr_statef_onlink;
    781 	LIST_INIT(&new->ndpr_advrtrs);
    782 	in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen);
    783 	/* make prefix in the canonical form */
    784 	for (i = 0; i < 4; i++)
    785 		new->ndpr_prefix.sin6_addr.s6_addr32[i] &=
    786 			new->ndpr_mask.s6_addr32[i];
    787 
    788 	/* xxx ND_OPT_PI_FLAG_ONLINK processing */
    789 
    790 	s = splsoftnet();
    791 	/* link ndpr_entry to nd_prefix list */
    792 	LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry);
    793 	splx(s);
    794 
    795 	if (dr)
    796 		pfxrtr_add(new, dr);
    797 
    798 	return 0;
    799 }
    800 
    801 void
    802 prelist_remove(pr)
    803 	struct nd_prefix *pr;
    804 {
    805 	struct nd_pfxrouter *pfr, *next;
    806 	int s;
    807 
    808 	s = splsoftnet();
    809 	/* unlink ndpr_entry from nd_prefix list */
    810 	LIST_REMOVE(pr, ndpr_entry);
    811 
    812 	/* free list of routers that adversed the prefix */
    813 	for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) {
    814 		next = pfr->pfr_next;
    815 
    816 		free(pfr, M_IP6NDP);
    817 	}
    818 	splx(s);
    819 
    820 	free(pr, M_IP6NDP);
    821 
    822 	pfxlist_onlink_check();
    823 }
    824 
    825 /*
    826  * NOTE: We set address lifetime to keep
    827  *	address lifetime <= prefix lifetime
    828  * invariant.  This is to simplify on-link determination code.
    829  * If onlink determination is udated, this routine may have to be updated too.
    830  */
    831 int
    832 prelist_update(new, dr, m)
    833 	struct nd_prefix *new;
    834 	struct nd_defrouter *dr; /* may be NULL */
    835 	struct mbuf *m;
    836 {
    837 	struct in6_ifaddr *ia6 = NULL;
    838 	struct nd_prefix *pr;
    839 	int s = splsoftnet();
    840 	int error = 0;
    841 	int auth;
    842 	struct in6_addrlifetime *lt6;
    843 
    844 	auth = 0;
    845 	if (m) {
    846 		/*
    847 		 * Authenticity for NA consists authentication for
    848 		 * both IP header and IP datagrams, doesn't it ?
    849 		 */
    850 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM)
    851 		auth = (m->m_flags & M_AUTHIPHDR
    852 		     && m->m_flags & M_AUTHIPDGM) ? 1 : 0;
    853 #endif
    854 	}
    855 
    856 	if ((pr = prefix_lookup(new)) != NULL) {
    857 		if (pr->ndpr_ifp != new->ndpr_ifp) {
    858 			error = EADDRNOTAVAIL;
    859 			goto end;
    860 		}
    861 		/* update prefix information */
    862 		pr->ndpr_flags = new->ndpr_flags;
    863 		pr->ndpr_vltime = new->ndpr_vltime;
    864 		pr->ndpr_pltime = new->ndpr_pltime;
    865 		pr->ndpr_preferred = new->ndpr_preferred;
    866 		pr->ndpr_expire = new->ndpr_expire;
    867 
    868 		/*
    869 		 * RFC 2462 5.5.3 (d) or (e)
    870 		 * We got a prefix which we have seen in the past.
    871 		 */
    872 		if (!new->ndpr_raf_auto)
    873 			goto noautoconf1;
    874 
    875 		if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
    876 			ia6 = NULL;
    877 		else
    878 			ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
    879 
    880 		if (ia6 == NULL) {
    881 			/*
    882 			 * Special case:
    883 			 * (1) We have seen the prefix advertised before, but
    884 			 * we have never performed autoconfig for this prefix.
    885 			 * This is because Autonomous bit was 0 previously, or
    886 			 * autoconfig failed due to some other reasons.
    887 			 * (2) We have seen the prefix advertised before and
    888 			 * we have performed autoconfig in the past, but
    889 			 * we seem to have no interface address right now.
    890 			 * This is because the interface address have expired.
    891 			 *
    892 			 * This prefix is fresh, with respect to autoconfig
    893 			 * process.
    894 			 *
    895 			 * Add an address based on RFC 2462 5.5.3 (d).
    896 			 */
    897 			ia6 = in6_ifadd(pr->ndpr_ifp,
    898 				&pr->ndpr_prefix.sin6_addr, &pr->ndpr_addr,
    899 				new->ndpr_plen);
    900 			if (!ia6) {
    901 				error = EADDRNOTAVAIL;
    902 				nd6log((LOG_ERR,
    903 				    "prelist_update: failed to add a "
    904 				    "new address\n"));
    905 				goto noautoconf1;
    906 			}
    907 
    908 			lt6 = &ia6->ia6_lifetime;
    909 
    910 			/* address lifetime <= prefix lifetime */
    911 			lt6->ia6t_vltime = new->ndpr_vltime;
    912 			lt6->ia6t_pltime = new->ndpr_pltime;
    913 			in6_init_address_ltimes(new, lt6, 1);
    914 		} else {
    915 #define TWOHOUR		(120*60)
    916 			/*
    917 			 * We have seen the prefix before, and we have added
    918 			 * interface address in the past.  We still have
    919 			 * the interface address assigned.
    920 			 *
    921 			 * update address lifetime based on RFC 2462
    922 			 * 5.5.3 (e).
    923 			 */
    924 			int update = 0;
    925 
    926 			lt6 = &ia6->ia6_lifetime;
    927 
    928 #if 0	/* RFC 2462 5.5.3 (e) */
    929 			lt6->ia6t_pltime = new->ndpr_pltime;
    930 			if (TWOHOUR < new->ndpr_vltime
    931 			 || lt6pr->nd < new->ndpr_vltime) {
    932 				lt6->ia6t_vltime = new->ndpr_vltime;
    933 				update++;
    934 			} else if (auth
    935 				&& lt6->ia6t_vltime <= TWOHOUR0
    936 				&& new->ndpr_vltime <= lt6->ia6t_vltime) {
    937 				lt6->ia6t_vltime = new->ndpr_vltime;
    938 				update++;
    939 			} else {
    940 				lt6->ia6t_vltime = TWOHOUR;
    941 				update++;
    942 			}
    943 
    944 			/* 2 hour rule is not imposed for pref lifetime */
    945 			new->ndpr_apltime = new->ndpr_pltime;
    946 			lt6->ia6t_pltime = new->ndpr_pltime;
    947 #else	/* update from Jim Bound, (ipng 6712) */
    948 			if (TWOHOUR < new->ndpr_vltime
    949 			 || lt6->ia6t_vltime < new->ndpr_vltime) {
    950 				lt6->ia6t_vltime = new->ndpr_vltime;
    951 				update++;
    952 			} else if (auth) {
    953 				lt6->ia6t_vltime = new->ndpr_vltime;
    954 				update++;
    955 			}
    956 
    957 			/* jim bound rule is not imposed for pref lifetime */
    958 			lt6->ia6t_pltime = new->ndpr_pltime;
    959 #endif
    960 			in6_init_address_ltimes(new, lt6, update);
    961 		}
    962 
    963  noautoconf1:
    964 
    965 #if 0
    966 		/* address lifetime expire processing, RFC 2462 5.5.4. */
    967 		if (pr->ndpr_preferred && pr->ndpr_preferred < time_second) {
    968 			struct in6_ifaddr *ia6;
    969 
    970 			ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
    971 			if (ia6)
    972 				ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
    973 		}
    974 #endif
    975 
    976 		if (dr && pfxrtr_lookup(pr, dr) == NULL)
    977 			pfxrtr_add(pr, dr);
    978 	} else {
    979 		int error_tmp;
    980 
    981 		if (new->ndpr_vltime == 0) goto end;
    982 
    983 		bzero(&new->ndpr_addr, sizeof(struct in6_addr));
    984 
    985 		/*
    986 		 * RFC 2462 5.5.3 (d)
    987 		 * We got a fresh prefix.  Perform some sanity checks
    988 		 * and add an interface address by appending interface ID
    989 		 * to the advertised prefix.
    990 		 */
    991 		if (!new->ndpr_raf_auto)
    992 			goto noautoconf2;
    993 
    994 		ia6 = in6_ifadd(new->ndpr_ifp, &new->ndpr_prefix.sin6_addr,
    995 			  &new->ndpr_addr, new->ndpr_plen);
    996 		if (!ia6) {
    997 			error = EADDRNOTAVAIL;
    998 			nd6log((LOG_ERR, "prelist_update: "
    999 				"failed to add a new address\n"));
   1000 			goto noautoconf2;
   1001 		}
   1002 		/* set onlink bit if an interface route is configured */
   1003 		new->ndpr_statef_onlink = (ia6->ia_flags & IFA_ROUTE) ? 1 : 0;
   1004 
   1005 		lt6 = &ia6->ia6_lifetime;
   1006 
   1007 		/* address lifetime <= prefix lifetime */
   1008 		lt6->ia6t_vltime = new->ndpr_vltime;
   1009 		lt6->ia6t_pltime = new->ndpr_pltime;
   1010 		in6_init_address_ltimes(new, lt6, 1);
   1011 
   1012  noautoconf2:
   1013 		error_tmp = prelist_add(new, dr);
   1014 		error = error_tmp ? error_tmp : error;
   1015 	}
   1016 
   1017  end:
   1018 	splx(s);
   1019 	return error;
   1020 }
   1021 
   1022 /*
   1023  * A supplement function used in the on-link detection below;
   1024  * detect if a given prefix has a (probably) reachable advertising router.
   1025  * XXX: lengthy function name...
   1026  */
   1027 static struct nd_pfxrouter *
   1028 find_pfxlist_reachable_router(pr)
   1029 	struct nd_prefix *pr;
   1030 {
   1031 	struct nd_pfxrouter *pfxrtr;
   1032 	struct rtentry *rt;
   1033 	struct llinfo_nd6 *ln;
   1034 
   1035 	for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr;
   1036 	     pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) {
   1037 		if ((rt = nd6_lookup(&pfxrtr->router->rtaddr, 0,
   1038 				     pfxrtr->router->ifp)) &&
   1039 		    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) &&
   1040 		    ND6_IS_LLINFO_PROBREACH(ln))
   1041 			break;	/* found */
   1042 	}
   1043 
   1044 	return(pfxrtr);
   1045 
   1046 }
   1047 
   1048 /*
   1049  * Check if each prefix in the prefix list has at least one available router
   1050  * that advertised the prefix (A router is "available" if its neighbor cache
   1051  * entry has reachable or probably reachable).
   1052  * If the check fails, the prefix may be off-link, because, for example,
   1053  * we have moved from the network but the lifetime of the prefix has not
   1054  * been expired yet. So we should not use the prefix if there is another
   1055  * prefix that has an available router.
   1056  * But if there is no prefix that has an available router, we still regards
   1057  * all the prefixes as on-link. This is because we can't tell if all the
   1058  * routers are simply dead or if we really moved from the network and there
   1059  * is no router around us.
   1060  */
   1061 void
   1062 pfxlist_onlink_check()
   1063 {
   1064 	struct nd_prefix *pr;
   1065 
   1066 	/*
   1067 	 * Check if there is a prefix that has a reachable advertising
   1068 	 * router.
   1069 	 */
   1070 	for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
   1071 		if (find_pfxlist_reachable_router(pr))
   1072 			break;
   1073 	}
   1074 
   1075 	if (pr) {
   1076 		/*
   1077 		 * There is at least one prefix that has a reachable router.
   1078 		 * First, detach prefixes which has no reachable advertising
   1079 		 * router and then attach other prefixes.
   1080 		 * The order is important since an attached prefix and a
   1081 		 * detached prefix may have a same interface route.
   1082 		 */
   1083 		for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
   1084 			if (find_pfxlist_reachable_router(pr) == NULL &&
   1085 			    pr->ndpr_statef_onlink) {
   1086 				pr->ndpr_statef_onlink = 0;
   1087 				nd6_detach_prefix(pr);
   1088 			}
   1089 		}
   1090 		for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
   1091 			if (find_pfxlist_reachable_router(pr) &&
   1092 			    pr->ndpr_statef_onlink == 0)
   1093 				nd6_attach_prefix(pr);
   1094 		}
   1095 	}
   1096 	else {
   1097 		/* there is no prefix that has a reachable router */
   1098 		for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next)
   1099 			if (pr->ndpr_statef_onlink == 0)
   1100 				nd6_attach_prefix(pr);
   1101 	}
   1102 }
   1103 
   1104 static void
   1105 nd6_detach_prefix(pr)
   1106 	struct nd_prefix *pr;
   1107 {
   1108 	struct in6_ifaddr *ia6;
   1109 	struct sockaddr_in6 sa6, mask6;
   1110 
   1111 	/*
   1112 	 * Delete the interface route associated with the prefix.
   1113 	 */
   1114 	bzero(&sa6, sizeof(sa6));
   1115 	sa6.sin6_family = AF_INET6;
   1116 	sa6.sin6_len = sizeof(sa6);
   1117 	bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr,
   1118 	      sizeof(struct in6_addr));
   1119 	bzero(&mask6, sizeof(mask6));
   1120 	mask6.sin6_family = AF_INET6;
   1121 	mask6.sin6_len = sizeof(sa6);
   1122 	bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr));
   1123 	{
   1124 		int e;
   1125 
   1126 		e = rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL,
   1127 			      (struct sockaddr *)&mask6, 0, NULL);
   1128 		if (e) {
   1129 			nd6log((LOG_ERR,
   1130 			    "nd6_detach_prefix: failed to delete route: "
   1131 			    "%s/%d (errno = %d)\n",
   1132 			    ip6_sprintf(&sa6.sin6_addr),
   1133 			    pr->ndpr_plen,
   1134 			    e));
   1135 		}
   1136 	}
   1137 
   1138 	/*
   1139 	 * Mark the address derived from the prefix detached so that
   1140 	 * it won't be used as a source address for a new connection.
   1141 	 */
   1142 	if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
   1143 		ia6 = NULL;
   1144 	else
   1145 		ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
   1146 	if (ia6)
   1147 		ia6->ia6_flags |= IN6_IFF_DETACHED;
   1148 }
   1149 
   1150 static void
   1151 nd6_attach_prefix(pr)
   1152 	struct nd_prefix *pr;
   1153 {
   1154 	struct ifaddr *ifa;
   1155 	struct in6_ifaddr *ia6;
   1156 
   1157 	/*
   1158 	 * Add the interface route associated with the prefix(if necessary)
   1159 	 * Should we consider if the L bit is set in pr->ndpr_flags?
   1160 	 */
   1161 	ifa = ifaof_ifpforaddr((struct sockaddr *)&pr->ndpr_prefix,
   1162 			       pr->ndpr_ifp);
   1163 	if (ifa == NULL) {
   1164 		nd6log((LOG_ERR,
   1165 		    "nd6_attach_prefix: failed to find any ifaddr"
   1166 		    " to add route for a prefix(%s/%d)\n",
   1167 		    ip6_sprintf(&pr->ndpr_addr), pr->ndpr_plen));
   1168 	}
   1169 	else {
   1170 		int e;
   1171 		struct sockaddr_in6 mask6;
   1172 
   1173 		bzero(&mask6, sizeof(mask6));
   1174 		mask6.sin6_family = AF_INET6;
   1175 		mask6.sin6_len = sizeof(mask6);
   1176 		mask6.sin6_addr = pr->ndpr_mask;
   1177 		e = rtrequest(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix,
   1178 			      ifa->ifa_addr, (struct sockaddr *)&mask6,
   1179 			      ifa->ifa_flags, NULL);
   1180 		if (e == 0)
   1181 			pr->ndpr_statef_onlink = 1;
   1182 		else {
   1183 			nd6log((LOG_DEBUG,
   1184 			    "nd6_attach_prefix: failed to add route for"
   1185 			    " a prefix(%s/%d), errno = %d\n",
   1186 			    ip6_sprintf(&pr->ndpr_addr), pr->ndpr_plen, e));
   1187 		}
   1188 	}
   1189 
   1190 	/*
   1191 	 * Now the address derived from the prefix can be used as a source
   1192 	 * for a new connection, so clear the detached flag.
   1193 	 */
   1194 	if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
   1195 		ia6 = NULL;
   1196 	else
   1197 		ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
   1198 	if (ia6) {
   1199 		ia6->ia6_flags &= ~IN6_IFF_DETACHED;
   1200 		if (pr->ndpr_statef_onlink)
   1201 			ia6->ia_flags |= IFA_ROUTE;
   1202 	}
   1203 }
   1204 
   1205 static struct in6_ifaddr *
   1206 in6_ifadd(ifp, in6, addr, prefixlen)
   1207 	struct ifnet *ifp;
   1208 	struct in6_addr *in6;
   1209 	struct in6_addr *addr;
   1210 	int prefixlen;	/* prefix len of the new prefix in "in6" */
   1211 {
   1212 	struct ifaddr *ifa;
   1213 	struct in6_ifaddr *ia, *ib, *oia;
   1214 	int s, error;
   1215 	struct in6_addr mask;
   1216 
   1217 	in6_len2mask(&mask, prefixlen);
   1218 
   1219 	/* find link-local address (will be interface ID) */
   1220 	ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);/* 0 is OK? */
   1221 	if (ifa)
   1222 		ib = (struct in6_ifaddr *)ifa;
   1223 	else
   1224 		return NULL;
   1225 
   1226 #if 0 /* don't care link local addr state, and always do DAD */
   1227 	/* if link-local address is not eligible, do not autoconfigure. */
   1228 	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) {
   1229 		printf("in6_ifadd: link-local address not ready\n");
   1230 		return NULL;
   1231 	}
   1232 #endif
   1233 
   1234 	/* prefixlen + ifidlen must be equal to 128 */
   1235 	if (prefixlen != in6_mask2len(&ib->ia_prefixmask.sin6_addr)) {
   1236 		nd6log((LOG_ERR, "in6_ifadd: wrong prefixlen for %s"
   1237 			"(prefix=%d ifid=%d)\n", if_name(ifp),
   1238 			prefixlen,
   1239 			128 - in6_mask2len(&ib->ia_prefixmask.sin6_addr)));
   1240 		return NULL;
   1241 	}
   1242 
   1243 	/* make ifaddr */
   1244 	ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_DONTWAIT);
   1245 	if (ia == NULL) {
   1246 		printf("ENOBUFS in in6_ifadd %d\n", __LINE__);
   1247 		return NULL;
   1248 	}
   1249 
   1250 	bzero((caddr_t)ia, sizeof(*ia));
   1251 	ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
   1252 	if (ifp->if_flags & IFF_POINTOPOINT)
   1253 		ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
   1254 	else
   1255 		ia->ia_ifa.ifa_dstaddr = NULL;
   1256 	ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
   1257 	ia->ia_ifp = ifp;
   1258 
   1259 	/* link to in6_ifaddr */
   1260 	if ((oia = in6_ifaddr) != NULL) {
   1261 		for( ; oia->ia_next; oia = oia->ia_next)
   1262 			continue;
   1263 		oia->ia_next = ia;
   1264 	} else {
   1265 		/*
   1266 		 * This should be impossible, since we have at least one
   1267 		 * link-local address (see the beginning of this function).
   1268 		 * XXX: should we rather panic here?
   1269 		 */
   1270 		printf("in6_ifadd: in6_ifaddr is NULL (impossible!)\n");
   1271 		in6_ifaddr = ia;
   1272 	}
   1273 	/* gain a refcnt for the link from in6_ifaddr */
   1274 	IFAREF((struct ifaddr *)ia);
   1275 
   1276 	/* link to if_addrlist */
   1277 	TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
   1278 	/* gain another refcnt for the link from if_addrlist */
   1279 	IFAREF((struct ifaddr *)ia);
   1280 
   1281 	/* new address */
   1282 	ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
   1283 	ia->ia_addr.sin6_family = AF_INET6;
   1284 	/* prefix */
   1285 	bcopy(in6, &ia->ia_addr.sin6_addr, sizeof(ia->ia_addr.sin6_addr));
   1286 	ia->ia_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0];
   1287 	ia->ia_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1];
   1288 	ia->ia_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2];
   1289 	ia->ia_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3];
   1290 	/* interface ID */
   1291 	ia->ia_addr.sin6_addr.s6_addr32[0]
   1292 		|= (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]);
   1293 	ia->ia_addr.sin6_addr.s6_addr32[1]
   1294 		|= (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]);
   1295 	ia->ia_addr.sin6_addr.s6_addr32[2]
   1296 		|= (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]);
   1297 	ia->ia_addr.sin6_addr.s6_addr32[3]
   1298 		|= (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]);
   1299 
   1300 	/* new prefix */
   1301 	ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
   1302 	ia->ia_prefixmask.sin6_family = AF_INET6;
   1303 	bcopy(&mask, &ia->ia_prefixmask.sin6_addr,
   1304 		sizeof(ia->ia_prefixmask.sin6_addr));
   1305 
   1306 	/* same routine */
   1307 	ia->ia_ifa.ifa_rtrequest =
   1308 		(ifp->if_type == IFT_PPP) ? nd6_p2p_rtrequest : nd6_rtrequest;
   1309 	ia->ia_ifa.ifa_flags |= RTF_CLONING;
   1310 	ia->ia_ifa.ifa_metric = ifp->if_metric;
   1311 
   1312 	/* add interface route */
   1313 	if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP|RTF_CLONING))) {
   1314 		nd6log((LOG_NOTICE,
   1315 		    "in6_ifadd: failed to add an interface route "
   1316 		    "for %s/%d on %s, errno = %d\n",
   1317 		    ip6_sprintf(&ia->ia_addr.sin6_addr), prefixlen,
   1318 		    if_name(ifp), error));
   1319 	}
   1320 	else
   1321 		ia->ia_flags |= IFA_ROUTE;
   1322 
   1323 	*addr = ia->ia_addr.sin6_addr;
   1324 
   1325 	if (ifp->if_flags & IFF_MULTICAST) {
   1326 		int error;	/* not used */
   1327 		struct in6_addr sol6;
   1328 
   1329 		/* Restore saved multicast addresses(if any). */
   1330 		in6_restoremkludge(ia, ifp);
   1331 
   1332 		/* join solicited node multicast address */
   1333 		bzero(&sol6, sizeof(sol6));
   1334 		sol6.s6_addr16[0] = htons(0xff02);
   1335 		sol6.s6_addr16[1] = htons(ifp->if_index);
   1336 		sol6.s6_addr32[1] = 0;
   1337 		sol6.s6_addr32[2] = htonl(1);
   1338 		sol6.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
   1339 		sol6.s6_addr8[12] = 0xff;
   1340 		(void)in6_addmulti(&sol6, ifp, &error);
   1341 	}
   1342 
   1343 	ia->ia6_flags |= IN6_IFF_TENTATIVE;
   1344 
   1345 	/*
   1346 	 * To make the interface up. Only AF_INET6 in ia is used...
   1347 	 */
   1348 	s = splimp();
   1349 	if (ifp->if_ioctl && (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)) {
   1350 		splx(s);
   1351 		return NULL;
   1352 	}
   1353 	splx(s);
   1354 
   1355 	/* Perform DAD, if needed. */
   1356 	nd6_dad_start((struct ifaddr *)ia, NULL);
   1357 
   1358 	return ia;
   1359 }
   1360 
   1361 int
   1362 in6_ifdel(ifp, in6)
   1363 	struct ifnet *ifp;
   1364 	struct in6_addr *in6;
   1365 {
   1366 	struct in6_ifaddr *ia = (struct in6_ifaddr *)NULL;
   1367 	struct in6_ifaddr *oia = (struct in6_ifaddr *)NULL;
   1368 
   1369 	if (!ifp)
   1370 		return -1;
   1371 
   1372 	ia = in6ifa_ifpwithaddr(ifp, in6);
   1373 	if (!ia)
   1374 		return -1;
   1375 
   1376 	if (ifp->if_flags & IFF_MULTICAST) {
   1377 		/*
   1378 		 * delete solicited multicast addr for deleting host id
   1379 		 */
   1380 		struct in6_multi *in6m;
   1381 		struct in6_addr llsol;
   1382 		bzero(&llsol, sizeof(struct in6_addr));
   1383 		llsol.s6_addr16[0] = htons(0xff02);
   1384 		llsol.s6_addr16[1] = htons(ifp->if_index);
   1385 		llsol.s6_addr32[1] = 0;
   1386 		llsol.s6_addr32[2] = htonl(1);
   1387 		llsol.s6_addr32[3] =
   1388 				ia->ia_addr.sin6_addr.s6_addr32[3];
   1389 		llsol.s6_addr8[12] = 0xff;
   1390 
   1391 		IN6_LOOKUP_MULTI(llsol, ifp, in6m);
   1392 		if (in6m)
   1393 			in6_delmulti(in6m);
   1394 	}
   1395 
   1396 	if (ia->ia_flags & IFA_ROUTE) {
   1397 		rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
   1398 		ia->ia_flags &= ~IFA_ROUTE;
   1399 	}
   1400 
   1401 	TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
   1402 	IFAFREE(&ia->ia_ifa);
   1403 
   1404 	/* lladdr is never deleted */
   1405 	oia = ia;
   1406 	if (oia == (ia = in6_ifaddr))
   1407 		in6_ifaddr = ia->ia_next;
   1408 	else {
   1409 		while (ia->ia_next && (ia->ia_next != oia))
   1410 			ia = ia->ia_next;
   1411 		if (ia->ia_next)
   1412 			ia->ia_next = oia->ia_next;
   1413 		else
   1414 			return -1;
   1415 	}
   1416 
   1417 	in6_savemkludge(oia);
   1418 	IFAFREE((&oia->ia_ifa));
   1419 /* xxx
   1420 	rtrequest(RTM_DELETE,
   1421 		  (struct sockaddr *)&ia->ia_addr,
   1422 		  (struct sockaddr *)0
   1423 		  (struct sockaddr *)&ia->ia_prefixmask,
   1424 		  RTF_UP|RTF_CLONING,
   1425 		  (struct rtentry **)0);
   1426 */
   1427 	return 0;
   1428 }
   1429 
   1430 int
   1431 in6_init_prefix_ltimes(struct nd_prefix *ndpr)
   1432 {
   1433 	long time_second = time.tv_sec;
   1434 
   1435 	/* check if preferred lifetime > valid lifetime */
   1436 	if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) {
   1437 		nd6log((LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime"
   1438 		    "(%d) is greater than valid lifetime(%d)\n",
   1439 		    (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime));
   1440 		return (EINVAL);
   1441 	}
   1442 	if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME)
   1443 		ndpr->ndpr_preferred = 0;
   1444 	else
   1445 		ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime;
   1446 	if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME)
   1447 		ndpr->ndpr_expire = 0;
   1448 	else
   1449 		ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime;
   1450 
   1451 	return 0;
   1452 }
   1453 
   1454 static void
   1455 in6_init_address_ltimes(struct nd_prefix *new,
   1456 			struct in6_addrlifetime *lt6,
   1457 			int update_vltime)
   1458 {
   1459 	long time_second = time.tv_sec;
   1460 
   1461 	/* Valid lifetime must not be updated unless explicitly specified. */
   1462 	if (update_vltime) {
   1463 		/* init ia6t_expire */
   1464 		if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME)
   1465 			lt6->ia6t_expire = 0;
   1466 		else {
   1467 			lt6->ia6t_expire = time_second;
   1468 			lt6->ia6t_expire += lt6->ia6t_vltime;
   1469 		}
   1470 		/* Ensure addr lifetime <= prefix lifetime. */
   1471 		if (new->ndpr_expire && lt6->ia6t_expire &&
   1472 		    new->ndpr_expire < lt6->ia6t_expire)
   1473 			lt6->ia6t_expire = new->ndpr_expire;
   1474 	}
   1475 
   1476 	/* init ia6t_preferred */
   1477 	if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME)
   1478 		lt6->ia6t_preferred = 0;
   1479 	else {
   1480 		lt6->ia6t_preferred = time_second;
   1481 		lt6->ia6t_preferred += lt6->ia6t_pltime;
   1482 	}
   1483 	 /* Ensure addr lifetime <= prefix lifetime. */
   1484 	if (new->ndpr_preferred && lt6->ia6t_preferred
   1485 	    && new->ndpr_preferred < lt6->ia6t_preferred)
   1486 		lt6->ia6t_preferred = new->ndpr_preferred;
   1487 }
   1488 
   1489 /*
   1490  * Delete all the routing table entries that use the specified gateway.
   1491  * XXX: this function causes search through all entries of routing table, so
   1492  * it shouldn't be called when acting as a router.
   1493  */
   1494 void
   1495 rt6_flush(gateway, ifp)
   1496     struct in6_addr *gateway;
   1497     struct ifnet *ifp;
   1498 {
   1499 	struct radix_node_head *rnh = rt_tables[AF_INET6];
   1500 	int s = splsoftnet();
   1501 
   1502 	/* We'll care only link-local addresses */
   1503 	if (!IN6_IS_ADDR_LINKLOCAL(gateway)) {
   1504 		splx(s);
   1505 		return;
   1506 	}
   1507 	/* XXX: hack for KAME's link-local address kludge */
   1508 	gateway->s6_addr16[1] = htons(ifp->if_index);
   1509 
   1510 	rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway);
   1511 	splx(s);
   1512 }
   1513 
   1514 static int
   1515 rt6_deleteroute(rn, arg)
   1516 	struct radix_node *rn;
   1517 	void *arg;
   1518 {
   1519 #define SIN6(s)	((struct sockaddr_in6 *)s)
   1520 	struct rtentry *rt = (struct rtentry *)rn;
   1521 	struct in6_addr *gate = (struct in6_addr *)arg;
   1522 
   1523 	if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6)
   1524 		return(0);
   1525 
   1526 	if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr))
   1527 		return(0);
   1528 
   1529 	/*
   1530 	 * We delete only host route. This means, in particular, we don't
   1531 	 * delete default route.
   1532 	 */
   1533 	if ((rt->rt_flags & RTF_HOST) == 0)
   1534 		return(0);
   1535 
   1536 	return(rtrequest(RTM_DELETE, rt_key(rt),
   1537 			 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0));
   1538 #undef SIN6
   1539 }
   1540 
   1541 int
   1542 nd6_setdefaultiface(ifindex)
   1543 	int ifindex;
   1544 {
   1545 	int error = 0;
   1546 
   1547 	if (ifindex < 0 || if_index < ifindex)
   1548 		return(EINVAL);
   1549 
   1550 	if (nd6_defifindex != ifindex) {
   1551 		nd6_defifindex = ifindex;
   1552 		if (nd6_defifindex > 0)
   1553 			nd6_defifp = ifindex2ifnet[nd6_defifindex];
   1554 		else
   1555 			nd6_defifp = NULL;
   1556 
   1557 		/*
   1558 		 * If the Default Router List is empty, install a route
   1559 		 * to the specified interface as default or remove the default
   1560 		 * route when the default interface becomes canceled.
   1561 		 * The check for the queue is actually redundant, but
   1562 		 * we do this here to avoid re-install the default route
   1563 		 * if the list is NOT empty.
   1564 		 */
   1565 		if (TAILQ_FIRST(&nd_defrouter) == NULL)
   1566 			defrouter_select();
   1567 	}
   1568 
   1569 	return(error);
   1570 }
   1571