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