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