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