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