xref: /netbsd-src/sys/netinet6/nd6.c (revision 9ddb6ab554e70fb9bbd90c3d96b812bc57755a14)
1 /*	$NetBSD: nd6.c,v 1.141 2012/02/03 03:32:45 christos Exp $	*/
2 /*	$KAME: nd6.c,v 1.279 2002/06/08 11:16:51 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.c,v 1.141 2012/02/03 03:32:45 christos Exp $");
35 
36 #include "opt_ipsec.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/callout.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sockio.h>
46 #include <sys/time.h>
47 #include <sys/kernel.h>
48 #include <sys/protosw.h>
49 #include <sys/errno.h>
50 #include <sys/ioctl.h>
51 #include <sys/syslog.h>
52 #include <sys/queue.h>
53 #include <sys/cprng.h>
54 
55 #include <net/if.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
59 #include <net/if_ether.h>
60 #include <net/if_fddi.h>
61 #include <net/if_arc.h>
62 
63 #include <netinet/in.h>
64 #include <netinet6/in6_var.h>
65 #include <netinet/ip6.h>
66 #include <netinet6/ip6_var.h>
67 #include <netinet6/scope6_var.h>
68 #include <netinet6/nd6.h>
69 #include <netinet/icmp6.h>
70 #include <netinet6/icmp6_private.h>
71 
72 #ifdef KAME_IPSEC
73 #include <netinet6/ipsec.h>
74 #endif
75 
76 #include <net/net_osdep.h>
77 
78 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
79 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
80 
81 /* timer values */
82 int	nd6_prune	= 1;	/* walk list every 1 seconds */
83 int	nd6_delay	= 5;	/* delay first probe time 5 second */
84 int	nd6_umaxtries	= 3;	/* maximum unicast query */
85 int	nd6_mmaxtries	= 3;	/* maximum multicast query */
86 int	nd6_useloopback = 1;	/* use loopback interface for local traffic */
87 int	nd6_gctimer	= (60 * 60 * 24); /* 1 day: garbage collection timer */
88 
89 /* preventing too many loops in ND option parsing */
90 int nd6_maxndopt = 10;	/* max # of ND options allowed */
91 
92 int nd6_maxnudhint = 0;	/* max # of subsequent upper layer hints */
93 
94 int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */
95 
96 #ifdef ND6_DEBUG
97 int nd6_debug = 1;
98 #else
99 int nd6_debug = 0;
100 #endif
101 
102 /* for debugging? */
103 static int nd6_inuse, nd6_allocated;
104 
105 struct llinfo_nd6 llinfo_nd6 = {
106 	.ln_prev = &llinfo_nd6,
107 	.ln_next = &llinfo_nd6,
108 };
109 struct nd_drhead nd_defrouter;
110 struct nd_prhead nd_prefix = { 0 };
111 
112 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
113 static const struct sockaddr_in6 all1_sa = {
114 	  .sin6_family = AF_INET6
115 	, .sin6_len = sizeof(struct sockaddr_in6)
116 	, .sin6_addr = {.s6_addr = {0xff, 0xff, 0xff, 0xff,
117 				    0xff, 0xff, 0xff, 0xff,
118 				    0xff, 0xff, 0xff, 0xff,
119 				    0xff, 0xff, 0xff, 0xff}}
120 };
121 
122 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
123 static void nd6_slowtimo(void *);
124 static int regen_tmpaddr(struct in6_ifaddr *);
125 static struct llinfo_nd6 *nd6_free(struct rtentry *, int);
126 static void nd6_llinfo_timer(void *);
127 static void clear_llinfo_pqueue(struct llinfo_nd6 *);
128 
129 callout_t nd6_slowtimo_ch;
130 callout_t nd6_timer_ch;
131 extern callout_t in6_tmpaddrtimer_ch;
132 
133 static int fill_drlist(void *, size_t *, size_t);
134 static int fill_prlist(void *, size_t *, size_t);
135 
136 MALLOC_DEFINE(M_IP6NDP, "NDP", "IPv6 Neighbour Discovery");
137 
138 void
139 nd6_init(void)
140 {
141 	static int nd6_init_done = 0;
142 
143 	if (nd6_init_done) {
144 		log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
145 		return;
146 	}
147 
148 	/* initialization of the default router list */
149 	TAILQ_INIT(&nd_defrouter);
150 
151 	nd6_init_done = 1;
152 
153 	callout_init(&nd6_slowtimo_ch, CALLOUT_MPSAFE);
154 	callout_init(&nd6_timer_ch, CALLOUT_MPSAFE);
155 
156 	/* start timer */
157 	callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
158 	    nd6_slowtimo, NULL);
159 }
160 
161 struct nd_ifinfo *
162 nd6_ifattach(struct ifnet *ifp)
163 {
164 	struct nd_ifinfo *nd;
165 
166 	nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK|M_ZERO);
167 
168 	nd->initialized = 1;
169 
170 	nd->chlim = IPV6_DEFHLIM;
171 	nd->basereachable = REACHABLE_TIME;
172 	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
173 	nd->retrans = RETRANS_TIMER;
174 	/*
175 	 * Note that the default value of ip6_accept_rtadv is 0.
176 	 * Because we do not set ND6_IFF_OVERRIDE_RTADV here, we won't
177 	 * accept RAs by default.
178 	 */
179 	nd->flags = ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV;
180 
181 	/* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
182 	nd6_setmtu0(ifp, nd);
183 
184 	return nd;
185 }
186 
187 void
188 nd6_ifdetach(struct nd_ifinfo *nd)
189 {
190 
191 	free(nd, M_IP6NDP);
192 }
193 
194 void
195 nd6_setmtu(struct ifnet *ifp)
196 {
197 	nd6_setmtu0(ifp, ND_IFINFO(ifp));
198 }
199 
200 void
201 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
202 {
203 	u_int32_t omaxmtu;
204 
205 	omaxmtu = ndi->maxmtu;
206 
207 	switch (ifp->if_type) {
208 	case IFT_ARCNET:
209 		ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
210 		break;
211 	case IFT_FDDI:
212 		ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu);
213 		break;
214 	default:
215 		ndi->maxmtu = ifp->if_mtu;
216 		break;
217 	}
218 
219 	/*
220 	 * Decreasing the interface MTU under IPV6 minimum MTU may cause
221 	 * undesirable situation.  We thus notify the operator of the change
222 	 * explicitly.  The check for omaxmtu is necessary to restrict the
223 	 * log to the case of changing the MTU, not initializing it.
224 	 */
225 	if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
226 		log(LOG_NOTICE, "nd6_setmtu0: new link MTU on %s (%lu) is too"
227 		    " small for IPv6 which needs %lu\n",
228 		    if_name(ifp), (unsigned long)ndi->maxmtu, (unsigned long)
229 		    IPV6_MMTU);
230 	}
231 
232 	if (ndi->maxmtu > in6_maxmtu)
233 		in6_setmaxmtu(); /* check all interfaces just in case */
234 }
235 
236 void
237 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
238 {
239 
240 	memset(ndopts, 0, sizeof(*ndopts));
241 	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
242 	ndopts->nd_opts_last
243 		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
244 
245 	if (icmp6len == 0) {
246 		ndopts->nd_opts_done = 1;
247 		ndopts->nd_opts_search = NULL;
248 	}
249 }
250 
251 /*
252  * Take one ND option.
253  */
254 struct nd_opt_hdr *
255 nd6_option(union nd_opts *ndopts)
256 {
257 	struct nd_opt_hdr *nd_opt;
258 	int olen;
259 
260 	if (ndopts == NULL)
261 		panic("ndopts == NULL in nd6_option");
262 	if (ndopts->nd_opts_last == NULL)
263 		panic("uninitialized ndopts in nd6_option");
264 	if (ndopts->nd_opts_search == NULL)
265 		return NULL;
266 	if (ndopts->nd_opts_done)
267 		return NULL;
268 
269 	nd_opt = ndopts->nd_opts_search;
270 
271 	/* make sure nd_opt_len is inside the buffer */
272 	if ((void *)&nd_opt->nd_opt_len >= (void *)ndopts->nd_opts_last) {
273 		memset(ndopts, 0, sizeof(*ndopts));
274 		return NULL;
275 	}
276 
277 	olen = nd_opt->nd_opt_len << 3;
278 	if (olen == 0) {
279 		/*
280 		 * Message validation requires that all included
281 		 * options have a length that is greater than zero.
282 		 */
283 		memset(ndopts, 0, sizeof(*ndopts));
284 		return NULL;
285 	}
286 
287 	ndopts->nd_opts_search = (struct nd_opt_hdr *)((char *)nd_opt + olen);
288 	if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
289 		/* option overruns the end of buffer, invalid */
290 		memset(ndopts, 0, sizeof(*ndopts));
291 		return NULL;
292 	} else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
293 		/* reached the end of options chain */
294 		ndopts->nd_opts_done = 1;
295 		ndopts->nd_opts_search = NULL;
296 	}
297 	return nd_opt;
298 }
299 
300 /*
301  * Parse multiple ND options.
302  * This function is much easier to use, for ND routines that do not need
303  * multiple options of the same type.
304  */
305 int
306 nd6_options(union nd_opts *ndopts)
307 {
308 	struct nd_opt_hdr *nd_opt;
309 	int i = 0;
310 
311 	if (ndopts == NULL)
312 		panic("ndopts == NULL in nd6_options");
313 	if (ndopts->nd_opts_last == NULL)
314 		panic("uninitialized ndopts in nd6_options");
315 	if (ndopts->nd_opts_search == NULL)
316 		return 0;
317 
318 	while (1) {
319 		nd_opt = nd6_option(ndopts);
320 		if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
321 			/*
322 			 * Message validation requires that all included
323 			 * options have a length that is greater than zero.
324 			 */
325 			ICMP6_STATINC(ICMP6_STAT_ND_BADOPT);
326 			memset(ndopts, 0, sizeof(*ndopts));
327 			return -1;
328 		}
329 
330 		if (nd_opt == NULL)
331 			goto skip1;
332 
333 		switch (nd_opt->nd_opt_type) {
334 		case ND_OPT_SOURCE_LINKADDR:
335 		case ND_OPT_TARGET_LINKADDR:
336 		case ND_OPT_MTU:
337 		case ND_OPT_REDIRECTED_HEADER:
338 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
339 				nd6log((LOG_INFO,
340 				    "duplicated ND6 option found (type=%d)\n",
341 				    nd_opt->nd_opt_type));
342 				/* XXX bark? */
343 			} else {
344 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
345 					= nd_opt;
346 			}
347 			break;
348 		case ND_OPT_PREFIX_INFORMATION:
349 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
350 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
351 					= nd_opt;
352 			}
353 			ndopts->nd_opts_pi_end =
354 				(struct nd_opt_prefix_info *)nd_opt;
355 			break;
356 		default:
357 			/*
358 			 * Unknown options must be silently ignored,
359 			 * to accommodate future extension to the protocol.
360 			 */
361 			nd6log((LOG_DEBUG,
362 			    "nd6_options: unsupported option %d - "
363 			    "option ignored\n", nd_opt->nd_opt_type));
364 		}
365 
366 skip1:
367 		i++;
368 		if (i > nd6_maxndopt) {
369 			ICMP6_STATINC(ICMP6_STAT_ND_TOOMANYOPT);
370 			nd6log((LOG_INFO, "too many loop in nd opt\n"));
371 			break;
372 		}
373 
374 		if (ndopts->nd_opts_done)
375 			break;
376 	}
377 
378 	return 0;
379 }
380 
381 /*
382  * ND6 timer routine to handle ND6 entries
383  */
384 void
385 nd6_llinfo_settimer(struct llinfo_nd6 *ln, long xtick)
386 {
387 	int s;
388 
389 	s = splsoftnet();
390 
391 	if (xtick < 0) {
392 		ln->ln_expire = 0;
393 		ln->ln_ntick = 0;
394 		callout_stop(&ln->ln_timer_ch);
395 	} else {
396 		ln->ln_expire = time_second + xtick / hz;
397 		if (xtick > INT_MAX) {
398 			ln->ln_ntick = xtick - INT_MAX;
399 			callout_reset(&ln->ln_timer_ch, INT_MAX,
400 			    nd6_llinfo_timer, ln);
401 		} else {
402 			ln->ln_ntick = 0;
403 			callout_reset(&ln->ln_timer_ch, xtick,
404 			    nd6_llinfo_timer, ln);
405 		}
406 	}
407 
408 	splx(s);
409 }
410 
411 static void
412 nd6_llinfo_timer(void *arg)
413 {
414 	struct llinfo_nd6 *ln;
415 	struct rtentry *rt;
416 	const struct sockaddr_in6 *dst;
417 	struct ifnet *ifp;
418 	struct nd_ifinfo *ndi = NULL;
419 
420 	mutex_enter(softnet_lock);
421 	KERNEL_LOCK(1, NULL);
422 
423 	ln = (struct llinfo_nd6 *)arg;
424 
425 	if (ln->ln_ntick > 0) {
426 		nd6_llinfo_settimer(ln, ln->ln_ntick);
427 		KERNEL_UNLOCK_ONE(NULL);
428 		mutex_exit(softnet_lock);
429 		return;
430 	}
431 
432 	if ((rt = ln->ln_rt) == NULL)
433 		panic("ln->ln_rt == NULL");
434 	if ((ifp = rt->rt_ifp) == NULL)
435 		panic("ln->ln_rt->rt_ifp == NULL");
436 	ndi = ND_IFINFO(ifp);
437 	dst = satocsin6(rt_getkey(rt));
438 
439 	/* sanity check */
440 	if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
441 		panic("rt_llinfo(%p) is not equal to ln(%p)",
442 		      rt->rt_llinfo, ln);
443 	if (!dst)
444 		panic("dst=0 in nd6_timer(ln=%p)", ln);
445 
446 	switch (ln->ln_state) {
447 	case ND6_LLINFO_INCOMPLETE:
448 		if (ln->ln_asked < nd6_mmaxtries) {
449 			ln->ln_asked++;
450 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
451 			nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
452 		} else {
453 			struct mbuf *m = ln->ln_hold;
454 			if (m) {
455 				struct mbuf *m0;
456 
457 				/*
458 				 * assuming every packet in ln_hold has
459 				 * the same IP header
460 				 */
461 				m0 = m->m_nextpkt;
462 				m->m_nextpkt = NULL;
463 				icmp6_error2(m, ICMP6_DST_UNREACH,
464 				    ICMP6_DST_UNREACH_ADDR, 0, rt->rt_ifp);
465 
466 				ln->ln_hold = m0;
467 				clear_llinfo_pqueue(ln);
468  			}
469 			(void)nd6_free(rt, 0);
470 			ln = NULL;
471 		}
472 		break;
473 	case ND6_LLINFO_REACHABLE:
474 		if (!ND6_LLINFO_PERMANENT(ln)) {
475 			ln->ln_state = ND6_LLINFO_STALE;
476 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
477 		}
478 		break;
479 
480 	case ND6_LLINFO_STALE:
481 		/* Garbage Collection(RFC 2461 5.3) */
482 		if (!ND6_LLINFO_PERMANENT(ln)) {
483 			(void)nd6_free(rt, 1);
484 			ln = NULL;
485 		}
486 		break;
487 
488 	case ND6_LLINFO_DELAY:
489 		if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
490 			/* We need NUD */
491 			ln->ln_asked = 1;
492 			ln->ln_state = ND6_LLINFO_PROBE;
493 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
494 			nd6_ns_output(ifp, &dst->sin6_addr,
495 			    &dst->sin6_addr, ln, 0);
496 		} else {
497 			ln->ln_state = ND6_LLINFO_STALE; /* XXX */
498 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
499 		}
500 		break;
501 	case ND6_LLINFO_PROBE:
502 		if (ln->ln_asked < nd6_umaxtries) {
503 			ln->ln_asked++;
504 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
505 			nd6_ns_output(ifp, &dst->sin6_addr,
506 			    &dst->sin6_addr, ln, 0);
507 		} else {
508 			(void)nd6_free(rt, 0);
509 			ln = NULL;
510 		}
511 		break;
512 	}
513 
514 	KERNEL_UNLOCK_ONE(NULL);
515 	mutex_exit(softnet_lock);
516 }
517 
518 /*
519  * ND6 timer routine to expire default route list and prefix list
520  */
521 void
522 nd6_timer(void *ignored_arg)
523 {
524 	struct nd_defrouter *next_dr, *dr;
525 	struct nd_prefix *next_pr, *pr;
526 	struct in6_ifaddr *ia6, *nia6;
527 
528 	callout_reset(&nd6_timer_ch, nd6_prune * hz,
529 	    nd6_timer, NULL);
530 
531 	mutex_enter(softnet_lock);
532 	KERNEL_LOCK(1, NULL);
533 
534 	/* expire default router list */
535 
536 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, next_dr) {
537 		if (dr->expire && dr->expire < time_second) {
538 			defrtrlist_del(dr);
539 		}
540 	}
541 
542 	/*
543 	 * expire interface addresses.
544 	 * in the past the loop was inside prefix expiry processing.
545 	 * However, from a stricter speci-confrmance standpoint, we should
546 	 * rather separate address lifetimes and prefix lifetimes.
547 	 */
548   addrloop:
549 	for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
550 		nia6 = ia6->ia_next;
551 		/* check address lifetime */
552 		if (IFA6_IS_INVALID(ia6)) {
553 			int regen = 0;
554 
555 			/*
556 			 * If the expiring address is temporary, try
557 			 * regenerating a new one.  This would be useful when
558 			 * we suspended a laptop PC, then turned it on after a
559 			 * period that could invalidate all temporary
560 			 * addresses.  Although we may have to restart the
561 			 * loop (see below), it must be after purging the
562 			 * address.  Otherwise, we'd see an infinite loop of
563 			 * regeneration.
564 			 */
565 			if (ip6_use_tempaddr &&
566 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
567 				if (regen_tmpaddr(ia6) == 0)
568 					regen = 1;
569 			}
570 
571  			in6_purgeaddr(&ia6->ia_ifa);
572 
573 			if (regen)
574 				goto addrloop; /* XXX: see below */
575 		} else if (IFA6_IS_DEPRECATED(ia6)) {
576 			int oldflags = ia6->ia6_flags;
577 
578  			ia6->ia6_flags |= IN6_IFF_DEPRECATED;
579 
580 			/*
581 			 * If a temporary address has just become deprecated,
582 			 * regenerate a new one if possible.
583 			 */
584 			if (ip6_use_tempaddr &&
585 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
586 			    (oldflags & IN6_IFF_DEPRECATED) == 0) {
587 
588 				if (regen_tmpaddr(ia6) == 0) {
589 					/*
590 					 * A new temporary address is
591 					 * generated.
592 					 * XXX: this means the address chain
593 					 * has changed while we are still in
594 					 * the loop.  Although the change
595 					 * would not cause disaster (because
596 					 * it's not a deletion, but an
597 					 * addition,) we'd rather restart the
598 					 * loop just for safety.  Or does this
599 					 * significantly reduce performance??
600 					 */
601 					goto addrloop;
602 				}
603 			}
604 		} else {
605 			/*
606 			 * A new RA might have made a deprecated address
607 			 * preferred.
608 			 */
609 			ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
610 		}
611 	}
612 
613 	/* expire prefix list */
614 	LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, next_pr) {
615 		/*
616 		 * check prefix lifetime.
617 		 * since pltime is just for autoconf, pltime processing for
618 		 * prefix is not necessary.
619 		 */
620 		if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
621 		    time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
622 
623 			/*
624 			 * address expiration and prefix expiration are
625 			 * separate.  NEVER perform in6_purgeaddr here.
626 			 */
627 
628 			prelist_remove(pr);
629 		}
630 	}
631 
632 	KERNEL_UNLOCK_ONE(NULL);
633 	mutex_exit(softnet_lock);
634 }
635 
636 /* ia6: deprecated/invalidated temporary address */
637 static int
638 regen_tmpaddr(struct in6_ifaddr *ia6)
639 {
640 	struct ifaddr *ifa;
641 	struct ifnet *ifp;
642 	struct in6_ifaddr *public_ifa6 = NULL;
643 
644 	ifp = ia6->ia_ifa.ifa_ifp;
645 	IFADDR_FOREACH(ifa, ifp) {
646 		struct in6_ifaddr *it6;
647 
648 		if (ifa->ifa_addr->sa_family != AF_INET6)
649 			continue;
650 
651 		it6 = (struct in6_ifaddr *)ifa;
652 
653 		/* ignore no autoconf addresses. */
654 		if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
655 			continue;
656 
657 		/* ignore autoconf addresses with different prefixes. */
658 		if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
659 			continue;
660 
661 		/*
662 		 * Now we are looking at an autoconf address with the same
663 		 * prefix as ours.  If the address is temporary and is still
664 		 * preferred, do not create another one.  It would be rare, but
665 		 * could happen, for example, when we resume a laptop PC after
666 		 * a long period.
667 		 */
668 		if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
669 		    !IFA6_IS_DEPRECATED(it6)) {
670 			public_ifa6 = NULL;
671 			break;
672 		}
673 
674 		/*
675 		 * This is a public autoconf address that has the same prefix
676 		 * as ours.  If it is preferred, keep it.  We can't break the
677 		 * loop here, because there may be a still-preferred temporary
678 		 * address with the prefix.
679 		 */
680 		if (!IFA6_IS_DEPRECATED(it6))
681 		    public_ifa6 = it6;
682 	}
683 
684 	if (public_ifa6 != NULL) {
685 		int e;
686 
687 		/*
688 		 * Random factor is introduced in the preferred lifetime, so
689 		 * we do not need additional delay (3rd arg to in6_tmpifadd).
690 		 */
691 		if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
692 			log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
693 			    " tmp addr, errno=%d\n", e);
694 			return -1;
695 		}
696 		return 0;
697 	}
698 
699 	return -1;
700 }
701 
702 bool
703 nd6_accepts_rtadv(const struct nd_ifinfo *ndi)
704 {
705 	switch (ndi->flags & (ND6_IFF_ACCEPT_RTADV|ND6_IFF_OVERRIDE_RTADV)) {
706 	case ND6_IFF_OVERRIDE_RTADV|ND6_IFF_ACCEPT_RTADV:
707 		return true;
708 	case ND6_IFF_ACCEPT_RTADV:
709 		return ip6_accept_rtadv != 0;
710 	case ND6_IFF_OVERRIDE_RTADV:
711 	case 0:
712 	default:
713 		return false;
714 	}
715 }
716 
717 /*
718  * Nuke neighbor cache/prefix/default router management table, right before
719  * ifp goes away.
720  */
721 void
722 nd6_purge(struct ifnet *ifp)
723 {
724 	struct llinfo_nd6 *ln, *nln;
725 	struct nd_defrouter *dr, *ndr;
726 	struct nd_prefix *pr, *npr;
727 
728 	/*
729 	 * Nuke default router list entries toward ifp.
730 	 * We defer removal of default router list entries that is installed
731 	 * in the routing table, in order to keep additional side effects as
732 	 * small as possible.
733 	 */
734 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
735 		if (dr->installed)
736 			continue;
737 
738 		if (dr->ifp == ifp)
739 			defrtrlist_del(dr);
740 	}
741 
742 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
743 		if (!dr->installed)
744 			continue;
745 
746 		if (dr->ifp == ifp)
747 			defrtrlist_del(dr);
748 	}
749 
750 	/* Nuke prefix list entries toward ifp */
751 	LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
752 		if (pr->ndpr_ifp == ifp) {
753 			/*
754 			 * Because if_detach() does *not* release prefixes
755 			 * while purging addresses the reference count will
756 			 * still be above zero. We therefore reset it to
757 			 * make sure that the prefix really gets purged.
758 			 */
759 			pr->ndpr_refcnt = 0;
760 			/*
761 			 * Previously, pr->ndpr_addr is removed as well,
762 			 * but I strongly believe we don't have to do it.
763 			 * nd6_purge() is only called from in6_ifdetach(),
764 			 * which removes all the associated interface addresses
765 			 * by itself.
766 			 * (jinmei@kame.net 20010129)
767 			 */
768 			prelist_remove(pr);
769 		}
770 	}
771 
772 	/* cancel default outgoing interface setting */
773 	if (nd6_defifindex == ifp->if_index)
774 		nd6_setdefaultiface(0);
775 
776 	/* XXX: too restrictive? */
777 	if (!ip6_forwarding && ifp->if_afdata[AF_INET6]) {
778 		struct nd_ifinfo *ndi = ND_IFINFO(ifp);
779 		if (ndi && nd6_accepts_rtadv(ndi)) {
780 			/* refresh default router list */
781 			defrouter_select();
782 		}
783 	}
784 
785 	/*
786 	 * Nuke neighbor cache entries for the ifp.
787 	 * Note that rt->rt_ifp may not be the same as ifp,
788 	 * due to KAME goto ours hack.  See RTM_RESOLVE case in
789 	 * nd6_rtrequest(), and ip6_input().
790 	 */
791 	ln = llinfo_nd6.ln_next;
792 	while (ln != NULL && ln != &llinfo_nd6) {
793 		struct rtentry *rt;
794 		const struct sockaddr_dl *sdl;
795 
796 		nln = ln->ln_next;
797 		rt = ln->ln_rt;
798 		if (rt && rt->rt_gateway &&
799 		    rt->rt_gateway->sa_family == AF_LINK) {
800 			sdl = satocsdl(rt->rt_gateway);
801 			if (sdl->sdl_index == ifp->if_index)
802 				nln = nd6_free(rt, 0);
803 		}
804 		ln = nln;
805 	}
806 }
807 
808 struct rtentry *
809 nd6_lookup(const struct in6_addr *addr6, int create, struct ifnet *ifp)
810 {
811 	struct rtentry *rt;
812 	struct sockaddr_in6 sin6;
813 
814 	sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
815 	rt = rtalloc1((struct sockaddr *)&sin6, create);
816 	if (rt != NULL && (rt->rt_flags & RTF_LLINFO) == 0) {
817 		/*
818 		 * This is the case for the default route.
819 		 * If we want to create a neighbor cache for the address, we
820 		 * should free the route for the destination and allocate an
821 		 * interface route.
822 		 */
823 		if (create) {
824 			RTFREE(rt);
825 			rt = NULL;
826 		}
827 	}
828 	if (rt != NULL)
829 		;
830 	else if (create && ifp) {
831 		int e;
832 
833 		/*
834 		 * If no route is available and create is set,
835 		 * we allocate a host route for the destination
836 		 * and treat it like an interface route.
837 		 * This hack is necessary for a neighbor which can't
838 		 * be covered by our own prefix.
839 		 */
840 		struct ifaddr *ifa =
841 		    ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
842 		if (ifa == NULL)
843 			return NULL;
844 
845 		/*
846 		 * Create a new route.  RTF_LLINFO is necessary
847 		 * to create a Neighbor Cache entry for the
848 		 * destination in nd6_rtrequest which will be
849 		 * called in rtrequest via ifa->ifa_rtrequest.
850 		 */
851 		if ((e = rtrequest(RTM_ADD, (const struct sockaddr *)&sin6,
852 		    ifa->ifa_addr, (const struct sockaddr *)&all1_sa,
853 		    (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
854 		    ~RTF_CLONING, &rt)) != 0) {
855 #if 0
856 			log(LOG_ERR,
857 			    "nd6_lookup: failed to add route for a "
858 			    "neighbor(%s), errno=%d\n",
859 			    ip6_sprintf(addr6), e);
860 #endif
861 			return NULL;
862 		}
863 		if (rt == NULL)
864 			return NULL;
865 		if (rt->rt_llinfo) {
866 			struct llinfo_nd6 *ln =
867 			    (struct llinfo_nd6 *)rt->rt_llinfo;
868 			ln->ln_state = ND6_LLINFO_NOSTATE;
869 		}
870 	} else
871 		return NULL;
872 	rt->rt_refcnt--;
873 	/*
874 	 * Validation for the entry.
875 	 * Note that the check for rt_llinfo is necessary because a cloned
876 	 * route from a parent route that has the L flag (e.g. the default
877 	 * route to a p2p interface) may have the flag, too, while the
878 	 * destination is not actually a neighbor.
879 	 * XXX: we can't use rt->rt_ifp to check for the interface, since
880 	 *      it might be the loopback interface if the entry is for our
881 	 *      own address on a non-loopback interface. Instead, we should
882 	 *      use rt->rt_ifa->ifa_ifp, which would specify the REAL
883 	 *	interface.
884 	 * Note also that ifa_ifp and ifp may differ when we connect two
885 	 * interfaces to a same link, install a link prefix to an interface,
886 	 * and try to install a neighbor cache on an interface that does not
887 	 * have a route to the prefix.
888 	 */
889 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
890 	    rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
891 	    (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
892 		if (create) {
893 			nd6log((LOG_DEBUG,
894 			    "nd6_lookup: failed to lookup %s (if = %s)\n",
895 			    ip6_sprintf(addr6),
896 			    ifp ? if_name(ifp) : "unspec"));
897 		}
898 		return NULL;
899 	}
900 	return rt;
901 }
902 
903 /*
904  * Detect if a given IPv6 address identifies a neighbor on a given link.
905  * XXX: should take care of the destination of a p2p link?
906  */
907 int
908 nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
909 {
910 	struct nd_prefix *pr;
911 
912 	/*
913 	 * A link-local address is always a neighbor.
914 	 * XXX: a link does not necessarily specify a single interface.
915 	 */
916 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
917 		struct sockaddr_in6 sin6_copy;
918 		u_int32_t zone;
919 
920 		/*
921 		 * We need sin6_copy since sa6_recoverscope() may modify the
922 		 * content (XXX).
923 		 */
924 		sin6_copy = *addr;
925 		if (sa6_recoverscope(&sin6_copy))
926 			return 0; /* XXX: should be impossible */
927 		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
928 			return 0;
929 		if (sin6_copy.sin6_scope_id == zone)
930 			return 1;
931 		else
932 			return 0;
933 	}
934 
935 	/*
936 	 * If the address matches one of our on-link prefixes, it should be a
937 	 * neighbor.
938 	 */
939 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
940 		if (pr->ndpr_ifp != ifp)
941 			continue;
942 
943 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
944 			continue;
945 
946 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
947 		    &addr->sin6_addr, &pr->ndpr_mask))
948 			return 1;
949 	}
950 
951 	/*
952 	 * If the default router list is empty, all addresses are regarded
953 	 * as on-link, and thus, as a neighbor.
954 	 * XXX: we restrict the condition to hosts, because routers usually do
955 	 * not have the "default router list".
956 	 */
957 	if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
958 	    nd6_defifindex == ifp->if_index) {
959 		return 1;
960 	}
961 
962 	/*
963 	 * Even if the address matches none of our addresses, it might be
964 	 * in the neighbor cache.
965 	 */
966 	if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL)
967 		return 1;
968 
969 	return 0;
970 }
971 
972 /*
973  * Free an nd6 llinfo entry.
974  * Since the function would cause significant changes in the kernel, DO NOT
975  * make it global, unless you have a strong reason for the change, and are sure
976  * that the change is safe.
977  */
978 static struct llinfo_nd6 *
979 nd6_free(struct rtentry *rt, int gc)
980 {
981 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
982 	struct in6_addr in6 = satocsin6(rt_getkey(rt))->sin6_addr;
983 	struct nd_defrouter *dr;
984 
985 	/*
986 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
987 	 * even though it is not harmful, it was not really necessary.
988 	 */
989 
990 	/* cancel timer */
991 	nd6_llinfo_settimer(ln, -1);
992 
993 	if (!ip6_forwarding) {
994 		int s;
995 		s = splsoftnet();
996 		dr = defrouter_lookup(&satocsin6(rt_getkey(rt))->sin6_addr,
997 		    rt->rt_ifp);
998 
999 		if (dr != NULL && dr->expire &&
1000 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
1001 			/*
1002 			 * If the reason for the deletion is just garbage
1003 			 * collection, and the neighbor is an active default
1004 			 * router, do not delete it.  Instead, reset the GC
1005 			 * timer using the router's lifetime.
1006 			 * Simply deleting the entry would affect default
1007 			 * router selection, which is not necessarily a good
1008 			 * thing, especially when we're using router preference
1009 			 * values.
1010 			 * XXX: the check for ln_state would be redundant,
1011 			 *      but we intentionally keep it just in case.
1012 			 */
1013 			if (dr->expire > time_second)
1014 				nd6_llinfo_settimer(ln,
1015 				    (dr->expire - time_second) * hz);
1016 			else
1017 				nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
1018 			splx(s);
1019 			return ln->ln_next;
1020 		}
1021 
1022 		if (ln->ln_router || dr) {
1023 			/*
1024 			 * rt6_flush must be called whether or not the neighbor
1025 			 * is in the Default Router List.
1026 			 * See a corresponding comment in nd6_na_input().
1027 			 */
1028 			rt6_flush(&in6, rt->rt_ifp);
1029 		}
1030 
1031 		if (dr) {
1032 			/*
1033 			 * Unreachablity of a router might affect the default
1034 			 * router selection and on-link detection of advertised
1035 			 * prefixes.
1036 			 */
1037 
1038 			/*
1039 			 * Temporarily fake the state to choose a new default
1040 			 * router and to perform on-link determination of
1041 			 * prefixes correctly.
1042 			 * Below the state will be set correctly,
1043 			 * or the entry itself will be deleted.
1044 			 */
1045 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
1046 
1047 			/*
1048 			 * Since defrouter_select() does not affect the
1049 			 * on-link determination and MIP6 needs the check
1050 			 * before the default router selection, we perform
1051 			 * the check now.
1052 			 */
1053 			pfxlist_onlink_check();
1054 
1055 			/*
1056 			 * refresh default router list
1057 			 */
1058 			defrouter_select();
1059 		}
1060 		splx(s);
1061 	}
1062 
1063 	/*
1064 	 * Before deleting the entry, remember the next entry as the
1065 	 * return value.  We need this because pfxlist_onlink_check() above
1066 	 * might have freed other entries (particularly the old next entry) as
1067 	 * a side effect (XXX).
1068 	 */
1069 	next = ln->ln_next;
1070 
1071 	/*
1072 	 * Detach the route from the routing tree and the list of neighbor
1073 	 * caches, and disable the route entry not to be used in already
1074 	 * cached routes.
1075 	 */
1076 	rtrequest(RTM_DELETE, rt_getkey(rt), NULL, rt_mask(rt), 0, NULL);
1077 
1078 	return next;
1079 }
1080 
1081 /*
1082  * Upper-layer reachability hint for Neighbor Unreachability Detection.
1083  *
1084  * XXX cost-effective methods?
1085  */
1086 void
1087 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1088 {
1089 	struct llinfo_nd6 *ln;
1090 
1091 	/*
1092 	 * If the caller specified "rt", use that.  Otherwise, resolve the
1093 	 * routing table by supplied "dst6".
1094 	 */
1095 	if (rt == NULL) {
1096 		if (dst6 == NULL)
1097 			return;
1098 		if ((rt = nd6_lookup(dst6, 0, NULL)) == NULL)
1099 			return;
1100 	}
1101 
1102 	if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
1103 	    (rt->rt_flags & RTF_LLINFO) == 0 ||
1104 	    !rt->rt_llinfo || !rt->rt_gateway ||
1105 	    rt->rt_gateway->sa_family != AF_LINK) {
1106 		/* This is not a host route. */
1107 		return;
1108 	}
1109 
1110 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1111 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
1112 		return;
1113 
1114 	/*
1115 	 * if we get upper-layer reachability confirmation many times,
1116 	 * it is possible we have false information.
1117 	 */
1118 	if (!force) {
1119 		ln->ln_byhint++;
1120 		if (ln->ln_byhint > nd6_maxnudhint)
1121 			return;
1122 	}
1123 
1124 	ln->ln_state = ND6_LLINFO_REACHABLE;
1125 	if (!ND6_LLINFO_PERMANENT(ln)) {
1126 		nd6_llinfo_settimer(ln,
1127 		    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
1128 	}
1129 }
1130 
1131 void
1132 nd6_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
1133 {
1134 	struct sockaddr *gate = rt->rt_gateway;
1135 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1136 	struct ifnet *ifp = rt->rt_ifp;
1137 	uint8_t namelen = strlen(ifp->if_xname), addrlen = ifp->if_addrlen;
1138 	struct ifaddr *ifa;
1139 
1140 	RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1141 
1142 	if (req == RTM_LLINFO_UPD) {
1143 		int rc;
1144 		struct in6_addr *in6;
1145 		struct in6_addr in6_all;
1146 		int anycast;
1147 
1148 		if ((ifa = info->rti_ifa) == NULL)
1149 			return;
1150 
1151 		in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
1152 		anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST;
1153 
1154 		in6_all = in6addr_linklocal_allnodes;
1155 		if ((rc = in6_setscope(&in6_all, ifa->ifa_ifp, NULL)) != 0) {
1156 			log(LOG_ERR, "%s: failed to set scope %s "
1157 			    "(errno=%d)\n", __func__, if_name(ifp), rc);
1158 			return;
1159 		}
1160 
1161 		/* XXX don't set Override for proxy addresses */
1162 		nd6_na_output(ifa->ifa_ifp, &in6_all, in6,
1163 		    (anycast ? 0 : ND_NA_FLAG_OVERRIDE)
1164 #if 0
1165 		    | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
1166 #endif
1167 		    , 1, NULL);
1168 		return;
1169 	}
1170 
1171 	if ((rt->rt_flags & RTF_GATEWAY) != 0)
1172 		return;
1173 
1174 	if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1175 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1176 		/*
1177 		 * This is probably an interface direct route for a link
1178 		 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1179 		 * We do not need special treatment below for such a route.
1180 		 * Moreover, the RTF_LLINFO flag which would be set below
1181 		 * would annoy the ndp(8) command.
1182 		 */
1183 		return;
1184 	}
1185 
1186 	if (req == RTM_RESOLVE &&
1187 	    (nd6_need_cache(ifp) == 0 || /* stf case */
1188 	     !nd6_is_addr_neighbor(satocsin6(rt_getkey(rt)), ifp))) {
1189 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1190 		/*
1191 		 * FreeBSD and BSD/OS often make a cloned host route based
1192 		 * on a less-specific route (e.g. the default route).
1193 		 * If the less specific route does not have a "gateway"
1194 		 * (this is the case when the route just goes to a p2p or an
1195 		 * stf interface), we'll mistakenly make a neighbor cache for
1196 		 * the host route, and will see strange neighbor solicitation
1197 		 * for the corresponding destination.  In order to avoid the
1198 		 * confusion, we check if the destination of the route is
1199 		 * a neighbor in terms of neighbor discovery, and stop the
1200 		 * process if not.  Additionally, we remove the LLINFO flag
1201 		 * so that ndp(8) will not try to get the neighbor information
1202 		 * of the destination.
1203 		 */
1204 		rt->rt_flags &= ~RTF_LLINFO;
1205 		return;
1206 	}
1207 
1208 	switch (req) {
1209 	case RTM_ADD:
1210 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1211 		/*
1212 		 * There is no backward compatibility :)
1213 		 *
1214 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1215 		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1216 		 *	   rt->rt_flags |= RTF_CLONING;
1217 		 */
1218 		if ((rt->rt_flags & RTF_CLONING) ||
1219 		    ((rt->rt_flags & RTF_LLINFO) && ln == NULL)) {
1220 			union {
1221 				struct sockaddr sa;
1222 				struct sockaddr_dl sdl;
1223 				struct sockaddr_storage ss;
1224 			} u;
1225 			/*
1226 			 * Case 1: This route should come from a route to
1227 			 * interface (RTF_CLONING case) or the route should be
1228 			 * treated as on-link but is currently not
1229 			 * (RTF_LLINFO && ln == NULL case).
1230 			 */
1231 			if (sockaddr_dl_init(&u.sdl, sizeof(u.ss),
1232 			    ifp->if_index, ifp->if_type,
1233 			    NULL, namelen, NULL, addrlen) == NULL) {
1234 				printf("%s.%d: sockaddr_dl_init(, %zu, ) "
1235 				    "failed on %s\n", __func__, __LINE__,
1236 				    sizeof(u.ss), if_name(ifp));
1237 			}
1238 			rt_setgate(rt, &u.sa);
1239 			gate = rt->rt_gateway;
1240 			RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1241 			if (ln != NULL)
1242 				nd6_llinfo_settimer(ln, 0);
1243 			RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1244 			if ((rt->rt_flags & RTF_CLONING) != 0)
1245 				break;
1246 		}
1247 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1248 		/*
1249 		 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
1250 		 * We don't do that here since llinfo is not ready yet.
1251 		 *
1252 		 * There are also couple of other things to be discussed:
1253 		 * - unsolicited NA code needs improvement beforehand
1254 		 * - RFC2461 says we MAY send multicast unsolicited NA
1255 		 *   (7.2.6 paragraph 4), however, it also says that we
1256 		 *   SHOULD provide a mechanism to prevent multicast NA storm.
1257 		 *   we don't have anything like it right now.
1258 		 *   note that the mechanism needs a mutual agreement
1259 		 *   between proxies, which means that we need to implement
1260 		 *   a new protocol, or a new kludge.
1261 		 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1262 		 *   we need to check ip6forwarding before sending it.
1263 		 *   (or should we allow proxy ND configuration only for
1264 		 *   routers?  there's no mention about proxy ND from hosts)
1265 		 */
1266 #if 0
1267 		/* XXX it does not work */
1268 		if (rt->rt_flags & RTF_ANNOUNCE)
1269 			nd6_na_output(ifp,
1270 			      &satocsin6(rt_getkey(rt))->sin6_addr,
1271 			      &satocsin6(rt_getkey(rt))->sin6_addr,
1272 			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1273 			      1, NULL);
1274 #endif
1275 		/* FALLTHROUGH */
1276 	case RTM_RESOLVE:
1277 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1278 			RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1279 			/*
1280 			 * Address resolution isn't necessary for a point to
1281 			 * point link, so we can skip this test for a p2p link.
1282 			 */
1283 			if (gate->sa_family != AF_LINK ||
1284 			    gate->sa_len <
1285 			    sockaddr_dl_measure(namelen, addrlen)) {
1286 				log(LOG_DEBUG,
1287 				    "nd6_rtrequest: bad gateway value: %s\n",
1288 				    if_name(ifp));
1289 				break;
1290 			}
1291 			satosdl(gate)->sdl_type = ifp->if_type;
1292 			satosdl(gate)->sdl_index = ifp->if_index;
1293 			RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1294 		}
1295 		if (ln != NULL)
1296 			break;	/* This happens on a route change */
1297 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1298 		/*
1299 		 * Case 2: This route may come from cloning, or a manual route
1300 		 * add with a LL address.
1301 		 */
1302 		R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1303 		rt->rt_llinfo = ln;
1304 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1305 		if (ln == NULL) {
1306 			log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1307 			break;
1308 		}
1309 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1310 		nd6_inuse++;
1311 		nd6_allocated++;
1312 		memset(ln, 0, sizeof(*ln));
1313 		ln->ln_rt = rt;
1314 		callout_init(&ln->ln_timer_ch, CALLOUT_MPSAFE);
1315 		/* this is required for "ndp" command. - shin */
1316 		if (req == RTM_ADD) {
1317 		        /*
1318 			 * gate should have some valid AF_LINK entry,
1319 			 * and ln->ln_expire should have some lifetime
1320 			 * which is specified by ndp command.
1321 			 */
1322 			ln->ln_state = ND6_LLINFO_REACHABLE;
1323 			ln->ln_byhint = 0;
1324 		} else {
1325 		        /*
1326 			 * When req == RTM_RESOLVE, rt is created and
1327 			 * initialized in rtrequest(), so rt_expire is 0.
1328 			 */
1329 			ln->ln_state = ND6_LLINFO_NOSTATE;
1330 			nd6_llinfo_settimer(ln, 0);
1331 		}
1332 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1333 		rt->rt_flags |= RTF_LLINFO;
1334 		ln->ln_next = llinfo_nd6.ln_next;
1335 		llinfo_nd6.ln_next = ln;
1336 		ln->ln_prev = &llinfo_nd6;
1337 		ln->ln_next->ln_prev = ln;
1338 
1339 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1340 		/*
1341 		 * check if rt_getkey(rt) is an address assigned
1342 		 * to the interface.
1343 		 */
1344 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
1345 		    &satocsin6(rt_getkey(rt))->sin6_addr);
1346 		RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key);
1347 		if (ifa != NULL) {
1348 			const void *mac;
1349 			nd6_llinfo_settimer(ln, -1);
1350 			ln->ln_state = ND6_LLINFO_REACHABLE;
1351 			ln->ln_byhint = 0;
1352 			if ((mac = nd6_ifptomac(ifp)) != NULL) {
1353 				/* XXX check for error */
1354 				if (sockaddr_dl_setaddr(satosdl(gate),
1355 				    gate->sa_len, mac,
1356 				    ifp->if_addrlen) == NULL) {
1357 					printf("%s.%d: "
1358 					    "sockaddr_dl_setaddr(, %d, ) "
1359 					    "failed on %s\n", __func__,
1360 					    __LINE__, gate->sa_len,
1361 					    if_name(ifp));
1362 				}
1363 			}
1364 			if (nd6_useloopback) {
1365 				ifp = rt->rt_ifp = lo0ifp;	/* XXX */
1366 				/*
1367 				 * Make sure rt_ifa be equal to the ifaddr
1368 				 * corresponding to the address.
1369 				 * We need this because when we refer
1370 				 * rt_ifa->ia6_flags in ip6_input, we assume
1371 				 * that the rt_ifa points to the address instead
1372 				 * of the loopback address.
1373 				 */
1374 				if (ifa != rt->rt_ifa)
1375 					rt_replace_ifa(rt, ifa);
1376 				rt->rt_flags &= ~RTF_CLONED;
1377 			}
1378 		} else if (rt->rt_flags & RTF_ANNOUNCE) {
1379 			nd6_llinfo_settimer(ln, -1);
1380 			ln->ln_state = ND6_LLINFO_REACHABLE;
1381 			ln->ln_byhint = 0;
1382 
1383 			/* join solicited node multicast for proxy ND */
1384 			if (ifp->if_flags & IFF_MULTICAST) {
1385 				struct in6_addr llsol;
1386 				int error;
1387 
1388 				llsol = satocsin6(rt_getkey(rt))->sin6_addr;
1389 				llsol.s6_addr32[0] = htonl(0xff020000);
1390 				llsol.s6_addr32[1] = 0;
1391 				llsol.s6_addr32[2] = htonl(1);
1392 				llsol.s6_addr8[12] = 0xff;
1393 				if (in6_setscope(&llsol, ifp, NULL))
1394 					break;
1395 				if (!in6_addmulti(&llsol, ifp, &error, 0)) {
1396 					nd6log((LOG_ERR, "%s: failed to join "
1397 					    "%s (errno=%d)\n", if_name(ifp),
1398 					    ip6_sprintf(&llsol), error));
1399 				}
1400 			}
1401 		}
1402 		break;
1403 
1404 	case RTM_DELETE:
1405 		if (ln == NULL)
1406 			break;
1407 		/* leave from solicited node multicast for proxy ND */
1408 		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1409 		    (ifp->if_flags & IFF_MULTICAST) != 0) {
1410 			struct in6_addr llsol;
1411 			struct in6_multi *in6m;
1412 
1413 			llsol = satocsin6(rt_getkey(rt))->sin6_addr;
1414 			llsol.s6_addr32[0] = htonl(0xff020000);
1415 			llsol.s6_addr32[1] = 0;
1416 			llsol.s6_addr32[2] = htonl(1);
1417 			llsol.s6_addr8[12] = 0xff;
1418 			if (in6_setscope(&llsol, ifp, NULL) == 0) {
1419 				IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1420 				if (in6m)
1421 					in6_delmulti(in6m);
1422 			}
1423 		}
1424 		nd6_inuse--;
1425 		ln->ln_next->ln_prev = ln->ln_prev;
1426 		ln->ln_prev->ln_next = ln->ln_next;
1427 		ln->ln_prev = NULL;
1428 		nd6_llinfo_settimer(ln, -1);
1429 		rt->rt_llinfo = 0;
1430 		rt->rt_flags &= ~RTF_LLINFO;
1431 		clear_llinfo_pqueue(ln);
1432 		Free(ln);
1433 	}
1434 }
1435 
1436 int
1437 nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp)
1438 {
1439 	struct in6_drlist *drl = (struct in6_drlist *)data;
1440 	struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1441 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1442 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1443 	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1444 	struct nd_defrouter *dr;
1445 	struct nd_prefix *pr;
1446 	struct rtentry *rt;
1447 	int i = 0, error = 0;
1448 	int s;
1449 
1450 	switch (cmd) {
1451 	case SIOCGDRLST_IN6:
1452 		/*
1453 		 * obsolete API, use sysctl under net.inet6.icmp6
1454 		 */
1455 		memset(drl, 0, sizeof(*drl));
1456 		s = splsoftnet();
1457 		TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
1458 			if (i >= DRLSTSIZ)
1459 				break;
1460 			drl->defrouter[i].rtaddr = dr->rtaddr;
1461 			in6_clearscope(&drl->defrouter[i].rtaddr);
1462 
1463 			drl->defrouter[i].flags = dr->flags;
1464 			drl->defrouter[i].rtlifetime = dr->rtlifetime;
1465 			drl->defrouter[i].expire = dr->expire;
1466 			drl->defrouter[i].if_index = dr->ifp->if_index;
1467 			i++;
1468 		}
1469 		splx(s);
1470 		break;
1471 	case SIOCGPRLST_IN6:
1472 		/*
1473 		 * obsolete API, use sysctl under net.inet6.icmp6
1474 		 *
1475 		 * XXX the structure in6_prlist was changed in backward-
1476 		 * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
1477 		 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1478 		 */
1479 		/*
1480 		 * XXX meaning of fields, especialy "raflags", is very
1481 		 * differnet between RA prefix list and RR/static prefix list.
1482 		 * how about separating ioctls into two?
1483 		 */
1484 		memset(oprl, 0, sizeof(*oprl));
1485 		s = splsoftnet();
1486 		LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1487 			struct nd_pfxrouter *pfr;
1488 			int j;
1489 
1490 			if (i >= PRLSTSIZ)
1491 				break;
1492 			oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1493 			oprl->prefix[i].raflags = pr->ndpr_raf;
1494 			oprl->prefix[i].prefixlen = pr->ndpr_plen;
1495 			oprl->prefix[i].vltime = pr->ndpr_vltime;
1496 			oprl->prefix[i].pltime = pr->ndpr_pltime;
1497 			oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1498 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1499 				oprl->prefix[i].expire = 0;
1500 			else {
1501 				time_t maxexpire;
1502 
1503 				/* XXX: we assume time_t is signed. */
1504 				maxexpire = (-1) &
1505 				    ~((time_t)1 <<
1506 				    ((sizeof(maxexpire) * 8) - 1));
1507 				if (pr->ndpr_vltime <
1508 				    maxexpire - pr->ndpr_lastupdate) {
1509 					oprl->prefix[i].expire =
1510 						 pr->ndpr_lastupdate +
1511 						pr->ndpr_vltime;
1512 				} else
1513 					oprl->prefix[i].expire = maxexpire;
1514 			}
1515 
1516 			j = 0;
1517 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
1518 				if (j < DRLSTSIZ) {
1519 #define RTRADDR oprl->prefix[i].advrtr[j]
1520 					RTRADDR = pfr->router->rtaddr;
1521 					in6_clearscope(&RTRADDR);
1522 #undef RTRADDR
1523 				}
1524 				j++;
1525 			}
1526 			oprl->prefix[i].advrtrs = j;
1527 			oprl->prefix[i].origin = PR_ORIG_RA;
1528 
1529 			i++;
1530 		}
1531 		splx(s);
1532 
1533 		break;
1534 	case OSIOCGIFINFO_IN6:
1535 #define ND	ndi->ndi
1536 		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
1537 		memset(&ND, 0, sizeof(ND));
1538 		ND.linkmtu = IN6_LINKMTU(ifp);
1539 		ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1540 		ND.basereachable = ND_IFINFO(ifp)->basereachable;
1541 		ND.reachable = ND_IFINFO(ifp)->reachable;
1542 		ND.retrans = ND_IFINFO(ifp)->retrans;
1543 		ND.flags = ND_IFINFO(ifp)->flags;
1544 		ND.recalctm = ND_IFINFO(ifp)->recalctm;
1545 		ND.chlim = ND_IFINFO(ifp)->chlim;
1546 		break;
1547 	case SIOCGIFINFO_IN6:
1548 		ND = *ND_IFINFO(ifp);
1549 		break;
1550 	case SIOCSIFINFO_IN6:
1551 		/*
1552 		 * used to change host variables from userland.
1553 		 * intented for a use on router to reflect RA configurations.
1554 		 */
1555 		/* 0 means 'unspecified' */
1556 		if (ND.linkmtu != 0) {
1557 			if (ND.linkmtu < IPV6_MMTU ||
1558 			    ND.linkmtu > IN6_LINKMTU(ifp)) {
1559 				error = EINVAL;
1560 				break;
1561 			}
1562 			ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1563 		}
1564 
1565 		if (ND.basereachable != 0) {
1566 			int obasereachable = ND_IFINFO(ifp)->basereachable;
1567 
1568 			ND_IFINFO(ifp)->basereachable = ND.basereachable;
1569 			if (ND.basereachable != obasereachable)
1570 				ND_IFINFO(ifp)->reachable =
1571 				    ND_COMPUTE_RTIME(ND.basereachable);
1572 		}
1573 		if (ND.retrans != 0)
1574 			ND_IFINFO(ifp)->retrans = ND.retrans;
1575 		if (ND.chlim != 0)
1576 			ND_IFINFO(ifp)->chlim = ND.chlim;
1577 		/* FALLTHROUGH */
1578 	case SIOCSIFINFO_FLAGS:
1579 		ND_IFINFO(ifp)->flags = ND.flags;
1580 		break;
1581 #undef ND
1582 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1583 		/* sync kernel routing table with the default router list */
1584 		defrouter_reset();
1585 		defrouter_select();
1586 		break;
1587 	case SIOCSPFXFLUSH_IN6:
1588 	{
1589 		/* flush all the prefix advertised by routers */
1590 		struct nd_prefix *pfx, *next;
1591 
1592 		s = splsoftnet();
1593 		LIST_FOREACH_SAFE(pfx, &nd_prefix, ndpr_entry, next) {
1594 			struct in6_ifaddr *ia, *ia_next;
1595 
1596 			if (IN6_IS_ADDR_LINKLOCAL(&pfx->ndpr_prefix.sin6_addr))
1597 				continue; /* XXX */
1598 
1599 			/* do we really have to remove addresses as well? */
1600 			for (ia = in6_ifaddr; ia; ia = ia_next) {
1601 				/* ia might be removed.  keep the next ptr. */
1602 				ia_next = ia->ia_next;
1603 
1604 				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1605 					continue;
1606 
1607 				if (ia->ia6_ndpr == pfx)
1608 					in6_purgeaddr(&ia->ia_ifa);
1609 			}
1610 			prelist_remove(pfx);
1611 		}
1612 		splx(s);
1613 		break;
1614 	}
1615 	case SIOCSRTRFLUSH_IN6:
1616 	{
1617 		/* flush all the default routers */
1618 		struct nd_defrouter *drtr, *next;
1619 
1620 		s = splsoftnet();
1621 		defrouter_reset();
1622 		TAILQ_FOREACH_SAFE(drtr, &nd_defrouter, dr_entry, next) {
1623 			defrtrlist_del(drtr);
1624 		}
1625 		defrouter_select();
1626 		splx(s);
1627 		break;
1628 	}
1629 	case SIOCGNBRINFO_IN6:
1630 	{
1631 		struct llinfo_nd6 *ln;
1632 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1633 
1634 		if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1635 			return error;
1636 
1637 		s = splsoftnet();
1638 		if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL ||
1639 		    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) {
1640 			error = EINVAL;
1641 			splx(s);
1642 			break;
1643 		}
1644 		nbi->state = ln->ln_state;
1645 		nbi->asked = ln->ln_asked;
1646 		nbi->isrouter = ln->ln_router;
1647 		nbi->expire = ln->ln_expire;
1648 		splx(s);
1649 
1650 		break;
1651 	}
1652 	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1653 		ndif->ifindex = nd6_defifindex;
1654 		break;
1655 	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1656 		return nd6_setdefaultiface(ndif->ifindex);
1657 	}
1658 	return error;
1659 }
1660 
1661 void
1662 nd6_llinfo_release_pkts(struct llinfo_nd6 *ln, struct ifnet *ifp,
1663     struct rtentry *rt)
1664 {
1665 	struct mbuf *m_hold, *m_hold_next;
1666 
1667 	for (m_hold = ln->ln_hold, ln->ln_hold = NULL;
1668 	     m_hold != NULL;
1669 	     m_hold = m_hold_next) {
1670 		m_hold_next = m_hold->m_nextpkt;
1671 		m_hold->m_nextpkt = NULL;
1672 
1673 		/*
1674 		 * we assume ifp is not a p2p here, so
1675 		 * just set the 2nd argument as the
1676 		 * 1st one.
1677 		 */
1678 		nd6_output(ifp, ifp, m_hold, satocsin6(rt_getkey(rt)), rt);
1679 	}
1680 }
1681 
1682 /*
1683  * Create neighbor cache entry and cache link-layer address,
1684  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1685  */
1686 struct rtentry *
1687 nd6_cache_lladdr(
1688     struct ifnet *ifp,
1689     struct in6_addr *from,
1690     char *lladdr,
1691     int lladdrlen,
1692     int type,	/* ICMP6 type */
1693     int code	/* type dependent information */
1694 )
1695 {
1696 	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
1697 	struct rtentry *rt = NULL;
1698 	struct llinfo_nd6 *ln = NULL;
1699 	int is_newentry;
1700 	struct sockaddr_dl *sdl = NULL;
1701 	int do_update;
1702 	int olladdr;
1703 	int llchange;
1704 	int newstate = 0;
1705 
1706 	if (ifp == NULL)
1707 		panic("ifp == NULL in nd6_cache_lladdr");
1708 	if (from == NULL)
1709 		panic("from == NULL in nd6_cache_lladdr");
1710 
1711 	/* nothing must be updated for unspecified address */
1712 	if (IN6_IS_ADDR_UNSPECIFIED(from))
1713 		return NULL;
1714 
1715 	/*
1716 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1717 	 * the caller.
1718 	 *
1719 	 * XXX If the link does not have link-layer adderss, what should
1720 	 * we do? (ifp->if_addrlen == 0)
1721 	 * Spec says nothing in sections for RA, RS and NA.  There's small
1722 	 * description on it in NS section (RFC 2461 7.2.3).
1723 	 */
1724 
1725 	rt = nd6_lookup(from, 0, ifp);
1726 	if (rt == NULL) {
1727 #if 0
1728 		/* nothing must be done if there's no lladdr */
1729 		if (!lladdr || !lladdrlen)
1730 			return NULL;
1731 #endif
1732 
1733 		rt = nd6_lookup(from, 1, ifp);
1734 		is_newentry = 1;
1735 	} else {
1736 		/* do nothing if static ndp is set */
1737 		if (rt->rt_flags & RTF_STATIC)
1738 			return NULL;
1739 		is_newentry = 0;
1740 	}
1741 
1742 	if (rt == NULL)
1743 		return NULL;
1744 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1745 fail:
1746 		(void)nd6_free(rt, 0);
1747 		return NULL;
1748 	}
1749 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1750 	if (ln == NULL)
1751 		goto fail;
1752 	if (rt->rt_gateway == NULL)
1753 		goto fail;
1754 	if (rt->rt_gateway->sa_family != AF_LINK)
1755 		goto fail;
1756 	sdl = satosdl(rt->rt_gateway);
1757 
1758 	olladdr = (sdl->sdl_alen) ? 1 : 0;
1759 	if (olladdr && lladdr) {
1760 		if (memcmp(lladdr, CLLADDR(sdl), ifp->if_addrlen))
1761 			llchange = 1;
1762 		else
1763 			llchange = 0;
1764 	} else
1765 		llchange = 0;
1766 
1767 	/*
1768 	 * newentry olladdr  lladdr  llchange	(*=record)
1769 	 *	0	n	n	--	(1)
1770 	 *	0	y	n	--	(2)
1771 	 *	0	n	y	--	(3) * STALE
1772 	 *	0	y	y	n	(4) *
1773 	 *	0	y	y	y	(5) * STALE
1774 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1775 	 *	1	--	y	--	(7) * STALE
1776 	 */
1777 
1778 	if (lladdr) {		/* (3-5) and (7) */
1779 		/*
1780 		 * Record source link-layer address
1781 		 * XXX is it dependent to ifp->if_type?
1782 		 */
1783 		/* XXX check for error */
1784 		if (sockaddr_dl_setaddr(sdl, sdl->sdl_len, lladdr,
1785 		    ifp->if_addrlen) == NULL) {
1786 			printf("%s.%d: sockaddr_dl_setaddr(, %d, ) "
1787 			    "failed on %s\n", __func__, __LINE__,
1788 			    sdl->sdl_len, if_name(ifp));
1789 		}
1790 	}
1791 
1792 	if (!is_newentry) {
1793 		if ((!olladdr && lladdr) ||		/* (3) */
1794 		    (olladdr && lladdr && llchange)) {	/* (5) */
1795 			do_update = 1;
1796 			newstate = ND6_LLINFO_STALE;
1797 		} else					/* (1-2,4) */
1798 			do_update = 0;
1799 	} else {
1800 		do_update = 1;
1801 		if (lladdr == NULL)			/* (6) */
1802 			newstate = ND6_LLINFO_NOSTATE;
1803 		else					/* (7) */
1804 			newstate = ND6_LLINFO_STALE;
1805 	}
1806 
1807 	if (do_update) {
1808 		/*
1809 		 * Update the state of the neighbor cache.
1810 		 */
1811 		ln->ln_state = newstate;
1812 
1813 		if (ln->ln_state == ND6_LLINFO_STALE) {
1814 			/*
1815 			 * XXX: since nd6_output() below will cause
1816 			 * state tansition to DELAY and reset the timer,
1817 			 * we must set the timer now, although it is actually
1818 			 * meaningless.
1819 			 */
1820 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
1821 
1822 			nd6_llinfo_release_pkts(ln, ifp, rt);
1823 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1824 			/* probe right away */
1825 			nd6_llinfo_settimer((void *)ln, 0);
1826 		}
1827 	}
1828 
1829 	/*
1830 	 * ICMP6 type dependent behavior.
1831 	 *
1832 	 * NS: clear IsRouter if new entry
1833 	 * RS: clear IsRouter
1834 	 * RA: set IsRouter if there's lladdr
1835 	 * redir: clear IsRouter if new entry
1836 	 *
1837 	 * RA case, (1):
1838 	 * The spec says that we must set IsRouter in the following cases:
1839 	 * - If lladdr exist, set IsRouter.  This means (1-5).
1840 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1841 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1842 	 * A quetion arises for (1) case.  (1) case has no lladdr in the
1843 	 * neighbor cache, this is similar to (6).
1844 	 * This case is rare but we figured that we MUST NOT set IsRouter.
1845 	 *
1846 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1847 	 *							D R
1848 	 *	0	n	n	--	(1)	c   ?     s
1849 	 *	0	y	n	--	(2)	c   s     s
1850 	 *	0	n	y	--	(3)	c   s     s
1851 	 *	0	y	y	n	(4)	c   s     s
1852 	 *	0	y	y	y	(5)	c   s     s
1853 	 *	1	--	n	--	(6) c	c 	c s
1854 	 *	1	--	y	--	(7) c	c   s	c s
1855 	 *
1856 	 *					(c=clear s=set)
1857 	 */
1858 	switch (type & 0xff) {
1859 	case ND_NEIGHBOR_SOLICIT:
1860 		/*
1861 		 * New entry must have is_router flag cleared.
1862 		 */
1863 		if (is_newentry)	/* (6-7) */
1864 			ln->ln_router = 0;
1865 		break;
1866 	case ND_REDIRECT:
1867 		/*
1868 		 * If the icmp is a redirect to a better router, always set the
1869 		 * is_router flag.  Otherwise, if the entry is newly created,
1870 		 * clear the flag.  [RFC 2461, sec 8.3]
1871 		 */
1872 		if (code == ND_REDIRECT_ROUTER)
1873 			ln->ln_router = 1;
1874 		else if (is_newentry) /* (6-7) */
1875 			ln->ln_router = 0;
1876 		break;
1877 	case ND_ROUTER_SOLICIT:
1878 		/*
1879 		 * is_router flag must always be cleared.
1880 		 */
1881 		ln->ln_router = 0;
1882 		break;
1883 	case ND_ROUTER_ADVERT:
1884 		/*
1885 		 * Mark an entry with lladdr as a router.
1886 		 */
1887 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
1888 		    (is_newentry && lladdr)) {			/* (7) */
1889 			ln->ln_router = 1;
1890 		}
1891 		break;
1892 	}
1893 
1894 	/*
1895 	 * When the link-layer address of a router changes, select the
1896 	 * best router again.  In particular, when the neighbor entry is newly
1897 	 * created, it might affect the selection policy.
1898 	 * Question: can we restrict the first condition to the "is_newentry"
1899 	 * case?
1900 	 * XXX: when we hear an RA from a new router with the link-layer
1901 	 * address option, defrouter_select() is called twice, since
1902 	 * defrtrlist_update called the function as well.  However, I believe
1903 	 * we can compromise the overhead, since it only happens the first
1904 	 * time.
1905 	 * XXX: although defrouter_select() should not have a bad effect
1906 	 * for those are not autoconfigured hosts, we explicitly avoid such
1907 	 * cases for safety.
1908 	 */
1909 	if (do_update && ln->ln_router && !ip6_forwarding &&
1910 	    nd6_accepts_rtadv(ndi))
1911 		defrouter_select();
1912 
1913 	return rt;
1914 }
1915 
1916 static void
1917 nd6_slowtimo(void *ignored_arg)
1918 {
1919 	struct nd_ifinfo *nd6if;
1920 	struct ifnet *ifp;
1921 
1922 	mutex_enter(softnet_lock);
1923 	KERNEL_LOCK(1, NULL);
1924       	callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1925 	    nd6_slowtimo, NULL);
1926 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1927 		nd6if = ND_IFINFO(ifp);
1928 		if (nd6if->basereachable && /* already initialized */
1929 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1930 			/*
1931 			 * Since reachable time rarely changes by router
1932 			 * advertisements, we SHOULD insure that a new random
1933 			 * value gets recomputed at least once every few hours.
1934 			 * (RFC 2461, 6.3.4)
1935 			 */
1936 			nd6if->recalctm = nd6_recalc_reachtm_interval;
1937 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1938 		}
1939 	}
1940 	KERNEL_UNLOCK_ONE(NULL);
1941 	mutex_exit(softnet_lock);
1942 }
1943 
1944 #define senderr(e) { error = (e); goto bad;}
1945 int
1946 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
1947     const struct sockaddr_in6 *dst, struct rtentry *rt0)
1948 {
1949 	struct mbuf *m = m0;
1950 	struct rtentry *rt = rt0;
1951 	struct sockaddr_in6 *gw6 = NULL;
1952 	struct llinfo_nd6 *ln = NULL;
1953 	int error = 0;
1954 
1955 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1956 		goto sendpkt;
1957 
1958 	if (nd6_need_cache(ifp) == 0)
1959 		goto sendpkt;
1960 
1961 	/*
1962 	 * next hop determination.  This routine is derived from ether_output.
1963 	 */
1964 	if (rt) {
1965 		if ((rt->rt_flags & RTF_UP) == 0) {
1966 			if ((rt0 = rt = rtalloc1(sin6tocsa(dst), 1)) != NULL) {
1967 				rt->rt_refcnt--;
1968 				if (rt->rt_ifp != ifp)
1969 					senderr(EHOSTUNREACH);
1970 			} else
1971 				senderr(EHOSTUNREACH);
1972 		}
1973 
1974 		if (rt->rt_flags & RTF_GATEWAY) {
1975 			gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1976 
1977 			/*
1978 			 * We skip link-layer address resolution and NUD
1979 			 * if the gateway is not a neighbor from ND point
1980 			 * of view, regardless of the value of nd_ifinfo.flags.
1981 			 * The second condition is a bit tricky; we skip
1982 			 * if the gateway is our own address, which is
1983 			 * sometimes used to install a route to a p2p link.
1984 			 */
1985 			if (!nd6_is_addr_neighbor(gw6, ifp) ||
1986 			    in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1987 				/*
1988 				 * We allow this kind of tricky route only
1989 				 * when the outgoing interface is p2p.
1990 				 * XXX: we may need a more generic rule here.
1991 				 */
1992 				if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1993 					senderr(EHOSTUNREACH);
1994 
1995 				goto sendpkt;
1996 			}
1997 
1998 			if (rt->rt_gwroute == NULL)
1999 				goto lookup;
2000 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
2001 				rtfree(rt); rt = rt0;
2002 			lookup:
2003 				rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
2004 				if ((rt = rt->rt_gwroute) == NULL)
2005 					senderr(EHOSTUNREACH);
2006 				/* the "G" test below also prevents rt == rt0 */
2007 				if ((rt->rt_flags & RTF_GATEWAY) ||
2008 				    (rt->rt_ifp != ifp)) {
2009 					rt->rt_refcnt--;
2010 					rt0->rt_gwroute = NULL;
2011 					senderr(EHOSTUNREACH);
2012 				}
2013 			}
2014 		}
2015 	}
2016 
2017 	/*
2018 	 * Address resolution or Neighbor Unreachability Detection
2019 	 * for the next hop.
2020 	 * At this point, the destination of the packet must be a unicast
2021 	 * or an anycast address(i.e. not a multicast).
2022 	 */
2023 
2024 	/* Look up the neighbor cache for the nexthop */
2025 	if (rt != NULL && (rt->rt_flags & RTF_LLINFO) != 0)
2026 		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
2027 	else {
2028 		/*
2029 		 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2030 		 * the condition below is not very efficient.  But we believe
2031 		 * it is tolerable, because this should be a rare case.
2032 		 */
2033 		if (nd6_is_addr_neighbor(dst, ifp) &&
2034 		    (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
2035 			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
2036 	}
2037 	if (ln == NULL || rt == NULL) {
2038 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
2039 		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
2040 			log(LOG_DEBUG,
2041 			    "nd6_output: can't allocate llinfo for %s "
2042 			    "(ln=%p, rt=%p)\n",
2043 			    ip6_sprintf(&dst->sin6_addr), ln, rt);
2044 			senderr(EIO);	/* XXX: good error? */
2045 		}
2046 
2047 		goto sendpkt;	/* send anyway */
2048 	}
2049 
2050 	/* We don't have to do link-layer address resolution on a p2p link. */
2051 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
2052 	    ln->ln_state < ND6_LLINFO_REACHABLE) {
2053 		ln->ln_state = ND6_LLINFO_STALE;
2054 		nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
2055 	}
2056 
2057 	/*
2058 	 * The first time we send a packet to a neighbor whose entry is
2059 	 * STALE, we have to change the state to DELAY and a sets a timer to
2060 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2061 	 * neighbor unreachability detection on expiration.
2062 	 * (RFC 2461 7.3.3)
2063 	 */
2064 	if (ln->ln_state == ND6_LLINFO_STALE) {
2065 		ln->ln_asked = 0;
2066 		ln->ln_state = ND6_LLINFO_DELAY;
2067 		nd6_llinfo_settimer(ln, (long)nd6_delay * hz);
2068 	}
2069 
2070 	/*
2071 	 * If the neighbor cache entry has a state other than INCOMPLETE
2072 	 * (i.e. its link-layer address is already resolved), just
2073 	 * send the packet.
2074 	 */
2075 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
2076 		goto sendpkt;
2077 
2078 	/*
2079 	 * There is a neighbor cache entry, but no ethernet address
2080 	 * response yet.  Append this latest packet to the end of the
2081 	 * packet queue in the mbuf, unless the number of the packet
2082 	 * does not exceed nd6_maxqueuelen.  When it exceeds nd6_maxqueuelen,
2083 	 * the oldest packet in the queue will be removed.
2084 	 */
2085 	if (ln->ln_state == ND6_LLINFO_NOSTATE)
2086 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
2087 	if (ln->ln_hold) {
2088 		struct mbuf *m_hold;
2089 		int i;
2090 
2091 		i = 0;
2092 		for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) {
2093 			i++;
2094 			if (m_hold->m_nextpkt == NULL) {
2095 				m_hold->m_nextpkt = m;
2096 				break;
2097 			}
2098 		}
2099 		while (i >= nd6_maxqueuelen) {
2100 			m_hold = ln->ln_hold;
2101 			ln->ln_hold = ln->ln_hold->m_nextpkt;
2102 			m_freem(m_hold);
2103 			i--;
2104 		}
2105 	} else {
2106 		ln->ln_hold = m;
2107 	}
2108 
2109 	/*
2110 	 * If there has been no NS for the neighbor after entering the
2111 	 * INCOMPLETE state, send the first solicitation.
2112 	 */
2113 	if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
2114 		ln->ln_asked++;
2115 		nd6_llinfo_settimer(ln,
2116 		    (long)ND_IFINFO(ifp)->retrans * hz / 1000);
2117 		nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
2118 	}
2119 	return 0;
2120 
2121   sendpkt:
2122 	/* discard the packet if IPv6 operation is disabled on the interface */
2123 	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
2124 		error = ENETDOWN; /* better error? */
2125 		goto bad;
2126 	}
2127 
2128 #ifdef KAME_IPSEC
2129 	/* clean ipsec history once it goes out of the node */
2130 	ipsec_delaux(m);
2131 #endif
2132 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2133 		return (*ifp->if_output)(origifp, m, sin6tocsa(dst), rt);
2134 	return (*ifp->if_output)(ifp, m, sin6tocsa(dst), rt);
2135 
2136   bad:
2137 	if (m != NULL)
2138 		m_freem(m);
2139 	return error;
2140 }
2141 #undef senderr
2142 
2143 int
2144 nd6_need_cache(struct ifnet *ifp)
2145 {
2146 	/*
2147 	 * XXX: we currently do not make neighbor cache on any interface
2148 	 * other than ARCnet, Ethernet, FDDI and GIF.
2149 	 *
2150 	 * RFC2893 says:
2151 	 * - unidirectional tunnels needs no ND
2152 	 */
2153 	switch (ifp->if_type) {
2154 	case IFT_ARCNET:
2155 	case IFT_ETHER:
2156 	case IFT_FDDI:
2157 	case IFT_IEEE1394:
2158 	case IFT_CARP:
2159 	case IFT_GIF:		/* XXX need more cases? */
2160 	case IFT_PPP:
2161 	case IFT_TUNNEL:
2162 		return 1;
2163 	default:
2164 		return 0;
2165 	}
2166 }
2167 
2168 int
2169 nd6_storelladdr(const struct ifnet *ifp, const struct rtentry *rt,
2170     struct mbuf *m, const struct sockaddr *dst, uint8_t *lldst,
2171     size_t dstsize)
2172 {
2173 	const struct sockaddr_dl *sdl;
2174 
2175 	if (m->m_flags & M_MCAST) {
2176 		switch (ifp->if_type) {
2177 		case IFT_ETHER:
2178 		case IFT_FDDI:
2179 			ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr,
2180 			    lldst);
2181 			return 1;
2182 		case IFT_IEEE1394:
2183 			memcpy(lldst, ifp->if_broadcastaddr,
2184 			    MIN(dstsize, ifp->if_addrlen));
2185 			return 1;
2186 		case IFT_ARCNET:
2187 			*lldst = 0;
2188 			return 1;
2189 		default:
2190 			m_freem(m);
2191 			return 0;
2192 		}
2193 	}
2194 
2195 	if (rt == NULL) {
2196 		/* this could happen, if we could not allocate memory */
2197 		m_freem(m);
2198 		return 0;
2199 	}
2200 	if (rt->rt_gateway->sa_family != AF_LINK) {
2201 		printf("%s: something odd happens\n", __func__);
2202 		m_freem(m);
2203 		return 0;
2204 	}
2205 	sdl = satocsdl(rt->rt_gateway);
2206 	if (sdl->sdl_alen == 0 || sdl->sdl_alen > dstsize) {
2207 		/* this should be impossible, but we bark here for debugging */
2208 		printf("%s: sdl_alen == %" PRIu8 ", dst=%s, if=%s\n", __func__,
2209 		    sdl->sdl_alen, ip6_sprintf(&satocsin6(dst)->sin6_addr),
2210 		    if_name(ifp));
2211 		m_freem(m);
2212 		return 0;
2213 	}
2214 
2215 	memcpy(lldst, CLLADDR(sdl), MIN(dstsize, sdl->sdl_alen));
2216 	return 1;
2217 }
2218 
2219 static void
2220 clear_llinfo_pqueue(struct llinfo_nd6 *ln)
2221 {
2222 	struct mbuf *m_hold, *m_hold_next;
2223 
2224 	for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
2225 		m_hold_next = m_hold->m_nextpkt;
2226 		m_hold->m_nextpkt = NULL;
2227 		m_freem(m_hold);
2228 	}
2229 
2230 	ln->ln_hold = NULL;
2231 	return;
2232 }
2233 
2234 int
2235 nd6_sysctl(
2236     int name,
2237     void *oldp,	/* syscall arg, need copyout */
2238     size_t *oldlenp,
2239     void *newp,	/* syscall arg, need copyin */
2240     size_t newlen
2241 )
2242 {
2243 	void *p;
2244 	size_t ol;
2245 	int error;
2246 
2247 	error = 0;
2248 
2249 	if (newp)
2250 		return EPERM;
2251 	if (oldp && !oldlenp)
2252 		return EINVAL;
2253 	ol = oldlenp ? *oldlenp : 0;
2254 
2255 	if (oldp) {
2256 		p = malloc(*oldlenp, M_TEMP, M_WAITOK);
2257 		if (p == NULL)
2258 			return ENOMEM;
2259 	} else
2260 		p = NULL;
2261 	switch (name) {
2262 	case ICMPV6CTL_ND6_DRLIST:
2263 		error = fill_drlist(p, oldlenp, ol);
2264 		if (!error && p != NULL && oldp != NULL)
2265 			error = copyout(p, oldp, *oldlenp);
2266 		break;
2267 
2268 	case ICMPV6CTL_ND6_PRLIST:
2269 		error = fill_prlist(p, oldlenp, ol);
2270 		if (!error && p != NULL && oldp != NULL)
2271 			error = copyout(p, oldp, *oldlenp);
2272 		break;
2273 
2274 	case ICMPV6CTL_ND6_MAXQLEN:
2275 		break;
2276 
2277 	default:
2278 		error = ENOPROTOOPT;
2279 		break;
2280 	}
2281 	if (p)
2282 		free(p, M_TEMP);
2283 
2284 	return error;
2285 }
2286 
2287 static int
2288 fill_drlist(void *oldp, size_t *oldlenp, size_t ol)
2289 {
2290 	int error = 0, s;
2291 	struct in6_defrouter *d = NULL, *de = NULL;
2292 	struct nd_defrouter *dr;
2293 	size_t l;
2294 
2295 	s = splsoftnet();
2296 
2297 	if (oldp) {
2298 		d = (struct in6_defrouter *)oldp;
2299 		de = (struct in6_defrouter *)((char *)oldp + *oldlenp);
2300 	}
2301 	l = 0;
2302 
2303 	TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
2304 
2305 		if (oldp && d + 1 <= de) {
2306 			memset(d, 0, sizeof(*d));
2307 			sockaddr_in6_init(&d->rtaddr, &dr->rtaddr, 0, 0, 0);
2308 			if (sa6_recoverscope(&d->rtaddr)) {
2309 				log(LOG_ERR,
2310 				    "scope error in router list (%s)\n",
2311 				    ip6_sprintf(&d->rtaddr.sin6_addr));
2312 				/* XXX: press on... */
2313 			}
2314 			d->flags = dr->flags;
2315 			d->rtlifetime = dr->rtlifetime;
2316 			d->expire = dr->expire;
2317 			d->if_index = dr->ifp->if_index;
2318 		}
2319 
2320 		l += sizeof(*d);
2321 		if (d)
2322 			d++;
2323 	}
2324 
2325 	if (oldp) {
2326 		if (l > ol)
2327 			error = ENOMEM;
2328 	}
2329 	if (oldlenp)
2330 		*oldlenp = l;	/* (void *)d - (void *)oldp */
2331 
2332 	splx(s);
2333 
2334 	return error;
2335 }
2336 
2337 static int
2338 fill_prlist(void *oldp, size_t *oldlenp, size_t ol)
2339 {
2340 	int error = 0, s;
2341 	struct nd_prefix *pr;
2342 	struct in6_prefix *p = NULL;
2343 	struct in6_prefix *pe = NULL;
2344 	size_t l;
2345 
2346 	s = splsoftnet();
2347 
2348 	if (oldp) {
2349 		p = (struct in6_prefix *)oldp;
2350 		pe = (struct in6_prefix *)((char *)oldp + *oldlenp);
2351 	}
2352 	l = 0;
2353 
2354 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
2355 		u_short advrtrs;
2356 		size_t advance;
2357 		struct sockaddr_in6 *sin6;
2358 		struct sockaddr_in6 *s6;
2359 		struct nd_pfxrouter *pfr;
2360 
2361 		if (oldp && p + 1 <= pe)
2362 		{
2363 			memset(p, 0, sizeof(*p));
2364 			sin6 = (struct sockaddr_in6 *)(p + 1);
2365 
2366 			p->prefix = pr->ndpr_prefix;
2367 			if (sa6_recoverscope(&p->prefix)) {
2368 				log(LOG_ERR,
2369 				    "scope error in prefix list (%s)\n",
2370 				    ip6_sprintf(&p->prefix.sin6_addr));
2371 				/* XXX: press on... */
2372 			}
2373 			p->raflags = pr->ndpr_raf;
2374 			p->prefixlen = pr->ndpr_plen;
2375 			p->vltime = pr->ndpr_vltime;
2376 			p->pltime = pr->ndpr_pltime;
2377 			p->if_index = pr->ndpr_ifp->if_index;
2378 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2379 				p->expire = 0;
2380 			else {
2381 				time_t maxexpire;
2382 
2383 				/* XXX: we assume time_t is signed. */
2384 				maxexpire = (-1) &
2385 				    ~((time_t)1 <<
2386 				    ((sizeof(maxexpire) * 8) - 1));
2387 				if (pr->ndpr_vltime <
2388 				    maxexpire - pr->ndpr_lastupdate) {
2389 					p->expire = pr->ndpr_lastupdate +
2390 						pr->ndpr_vltime;
2391 				} else
2392 					p->expire = maxexpire;
2393 			}
2394 			p->refcnt = pr->ndpr_refcnt;
2395 			p->flags = pr->ndpr_stateflags;
2396 			p->origin = PR_ORIG_RA;
2397 			advrtrs = 0;
2398 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2399 				if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2400 					advrtrs++;
2401 					continue;
2402 				}
2403 				s6 = &sin6[advrtrs];
2404 				sockaddr_in6_init(s6, &pfr->router->rtaddr,
2405 				    0, 0, 0);
2406 				if (sa6_recoverscope(s6)) {
2407 					log(LOG_ERR,
2408 					    "scope error in "
2409 					    "prefix list (%s)\n",
2410 					    ip6_sprintf(&pfr->router->rtaddr));
2411 				}
2412 				advrtrs++;
2413 			}
2414 			p->advrtrs = advrtrs;
2415 		}
2416 		else {
2417 			advrtrs = 0;
2418 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
2419 				advrtrs++;
2420 		}
2421 
2422 		advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2423 		l += advance;
2424 		if (p)
2425 			p = (struct in6_prefix *)((char *)p + advance);
2426 	}
2427 
2428 	if (oldp) {
2429 		*oldlenp = l;	/* (void *)d - (void *)oldp */
2430 		if (l > ol)
2431 			error = ENOMEM;
2432 	} else
2433 		*oldlenp = l;
2434 
2435 	splx(s);
2436 
2437 	return error;
2438 }
2439