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