xref: /freebsd/sys/netinet6/nd6.c (revision 3e11bd9e2a2b1cbd4283c87c93e3cc75e3f2dacb)
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_inet.h"
36 #include "opt_inet6.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/sockio.h>
45 #include <sys/time.h>
46 #include <sys/kernel.h>
47 #include <sys/protosw.h>
48 #include <sys/errno.h>
49 #include <sys/syslog.h>
50 #include <sys/lock.h>
51 #include <sys/rwlock.h>
52 #include <sys/queue.h>
53 #include <sys/sdt.h>
54 #include <sys/sysctl.h>
55 
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_arc.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/iso88025.h>
62 #include <net/fddi.h>
63 #include <net/route.h>
64 #include <net/vnet.h>
65 
66 #include <netinet/in.h>
67 #include <netinet/in_kdtrace.h>
68 #include <net/if_llatbl.h>
69 #define	L3_ADDR_SIN6(le)	((struct sockaddr_in6 *) L3_ADDR(le))
70 #include <netinet/if_ether.h>
71 #include <netinet6/in6_var.h>
72 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
74 #include <netinet6/scope6_var.h>
75 #include <netinet6/nd6.h>
76 #include <netinet6/in6_ifattach.h>
77 #include <netinet/icmp6.h>
78 #include <netinet6/send.h>
79 
80 #include <sys/limits.h>
81 
82 #include <security/mac/mac_framework.h>
83 
84 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
85 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
86 
87 #define SIN6(s) ((const struct sockaddr_in6 *)(s))
88 
89 /* timer values */
90 VNET_DEFINE(int, nd6_prune)	= 1;	/* walk list every 1 seconds */
91 VNET_DEFINE(int, nd6_delay)	= 5;	/* delay first probe time 5 second */
92 VNET_DEFINE(int, nd6_umaxtries)	= 3;	/* maximum unicast query */
93 VNET_DEFINE(int, nd6_mmaxtries)	= 3;	/* maximum multicast query */
94 VNET_DEFINE(int, nd6_useloopback) = 1;	/* use loopback interface for
95 					 * local traffic */
96 VNET_DEFINE(int, nd6_gctimer)	= (60 * 60 * 24); /* 1 day: garbage
97 					 * collection timer */
98 
99 /* preventing too many loops in ND option parsing */
100 static VNET_DEFINE(int, nd6_maxndopt) = 10; /* max # of ND options allowed */
101 
102 VNET_DEFINE(int, nd6_maxnudhint) = 0;	/* max # of subsequent upper
103 					 * layer hints */
104 static VNET_DEFINE(int, nd6_maxqueuelen) = 1; /* max pkts cached in unresolved
105 					 * ND entries */
106 #define	V_nd6_maxndopt			VNET(nd6_maxndopt)
107 #define	V_nd6_maxqueuelen		VNET(nd6_maxqueuelen)
108 
109 #ifdef ND6_DEBUG
110 VNET_DEFINE(int, nd6_debug) = 1;
111 #else
112 VNET_DEFINE(int, nd6_debug) = 0;
113 #endif
114 
115 /* for debugging? */
116 #if 0
117 static int nd6_inuse, nd6_allocated;
118 #endif
119 
120 VNET_DEFINE(struct nd_drhead, nd_defrouter);
121 VNET_DEFINE(struct nd_prhead, nd_prefix);
122 
123 VNET_DEFINE(int, nd6_recalc_reachtm_interval) = ND6_RECALC_REACHTM_INTERVAL;
124 #define	V_nd6_recalc_reachtm_interval	VNET(nd6_recalc_reachtm_interval)
125 
126 int	(*send_sendso_input_hook)(struct mbuf *, struct ifnet *, int, int);
127 
128 static int nd6_is_new_addr_neighbor(struct sockaddr_in6 *,
129 	struct ifnet *);
130 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
131 static void nd6_slowtimo(void *);
132 static int regen_tmpaddr(struct in6_ifaddr *);
133 static struct llentry *nd6_free(struct llentry *, int);
134 static void nd6_llinfo_timer(void *);
135 static void clear_llinfo_pqueue(struct llentry *);
136 static void nd6_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
137 
138 static VNET_DEFINE(struct callout, nd6_slowtimo_ch);
139 #define	V_nd6_slowtimo_ch		VNET(nd6_slowtimo_ch)
140 
141 VNET_DEFINE(struct callout, nd6_timer_ch);
142 
143 void
144 nd6_init(void)
145 {
146 
147 	LIST_INIT(&V_nd_prefix);
148 
149 	/* initialization of the default router list */
150 	TAILQ_INIT(&V_nd_defrouter);
151 
152 	/* start timer */
153 	callout_init(&V_nd6_slowtimo_ch, 0);
154 	callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
155 	    nd6_slowtimo, curvnet);
156 }
157 
158 #ifdef VIMAGE
159 void
160 nd6_destroy()
161 {
162 
163 	callout_drain(&V_nd6_slowtimo_ch);
164 	callout_drain(&V_nd6_timer_ch);
165 }
166 #endif
167 
168 struct nd_ifinfo *
169 nd6_ifattach(struct ifnet *ifp)
170 {
171 	struct nd_ifinfo *nd;
172 
173 	nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK|M_ZERO);
174 	nd->initialized = 1;
175 
176 	nd->chlim = IPV6_DEFHLIM;
177 	nd->basereachable = REACHABLE_TIME;
178 	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
179 	nd->retrans = RETRANS_TIMER;
180 
181 	nd->flags = ND6_IFF_PERFORMNUD;
182 
183 	/* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL.
184 	 * XXXHRS: Clear ND6_IFF_AUTO_LINKLOCAL on an IFT_BRIDGE interface by
185 	 * default regardless of the V_ip6_auto_linklocal configuration to
186 	 * give a reasonable default behavior.
187 	 */
188 	if ((V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) ||
189 	    (ifp->if_flags & IFF_LOOPBACK))
190 		nd->flags |= ND6_IFF_AUTO_LINKLOCAL;
191 	/*
192 	 * A loopback interface does not need to accept RTADV.
193 	 * XXXHRS: Clear ND6_IFF_ACCEPT_RTADV on an IFT_BRIDGE interface by
194 	 * default regardless of the V_ip6_accept_rtadv configuration to
195 	 * prevent the interface from accepting RA messages arrived
196 	 * on one of the member interfaces with ND6_IFF_ACCEPT_RTADV.
197 	 */
198 	if (V_ip6_accept_rtadv &&
199 	    !(ifp->if_flags & IFF_LOOPBACK) &&
200 	    (ifp->if_type != IFT_BRIDGE))
201 			nd->flags |= ND6_IFF_ACCEPT_RTADV;
202 	if (V_ip6_no_radr && !(ifp->if_flags & IFF_LOOPBACK))
203 		nd->flags |= ND6_IFF_NO_RADR;
204 
205 	/* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
206 	nd6_setmtu0(ifp, nd);
207 
208 	return nd;
209 }
210 
211 void
212 nd6_ifdetach(struct nd_ifinfo *nd)
213 {
214 
215 	free(nd, M_IP6NDP);
216 }
217 
218 /*
219  * Reset ND level link MTU. This function is called when the physical MTU
220  * changes, which means we might have to adjust the ND level MTU.
221  */
222 void
223 nd6_setmtu(struct ifnet *ifp)
224 {
225 
226 	nd6_setmtu0(ifp, ND_IFINFO(ifp));
227 }
228 
229 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
230 void
231 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
232 {
233 	u_int32_t omaxmtu;
234 
235 	omaxmtu = ndi->maxmtu;
236 
237 	switch (ifp->if_type) {
238 	case IFT_ARCNET:
239 		ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
240 		break;
241 	case IFT_FDDI:
242 		ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
243 		break;
244 	case IFT_ISO88025:
245 		 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
246 		 break;
247 	default:
248 		ndi->maxmtu = ifp->if_mtu;
249 		break;
250 	}
251 
252 	/*
253 	 * Decreasing the interface MTU under IPV6 minimum MTU may cause
254 	 * undesirable situation.  We thus notify the operator of the change
255 	 * explicitly.  The check for omaxmtu is necessary to restrict the
256 	 * log to the case of changing the MTU, not initializing it.
257 	 */
258 	if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
259 		log(LOG_NOTICE, "nd6_setmtu0: "
260 		    "new link MTU on %s (%lu) is too small for IPv6\n",
261 		    if_name(ifp), (unsigned long)ndi->maxmtu);
262 	}
263 
264 	if (ndi->maxmtu > V_in6_maxmtu)
265 		in6_setmaxmtu(); /* check all interfaces just in case */
266 
267 }
268 
269 void
270 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
271 {
272 
273 	bzero(ndopts, sizeof(*ndopts));
274 	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
275 	ndopts->nd_opts_last
276 		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
277 
278 	if (icmp6len == 0) {
279 		ndopts->nd_opts_done = 1;
280 		ndopts->nd_opts_search = NULL;
281 	}
282 }
283 
284 /*
285  * Take one ND option.
286  */
287 struct nd_opt_hdr *
288 nd6_option(union nd_opts *ndopts)
289 {
290 	struct nd_opt_hdr *nd_opt;
291 	int olen;
292 
293 	KASSERT(ndopts != NULL, ("%s: ndopts == NULL", __func__));
294 	KASSERT(ndopts->nd_opts_last != NULL, ("%s: uninitialized ndopts",
295 	    __func__));
296 	if (ndopts->nd_opts_search == NULL)
297 		return NULL;
298 	if (ndopts->nd_opts_done)
299 		return NULL;
300 
301 	nd_opt = ndopts->nd_opts_search;
302 
303 	/* make sure nd_opt_len is inside the buffer */
304 	if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
305 		bzero(ndopts, sizeof(*ndopts));
306 		return NULL;
307 	}
308 
309 	olen = nd_opt->nd_opt_len << 3;
310 	if (olen == 0) {
311 		/*
312 		 * Message validation requires that all included
313 		 * options have a length that is greater than zero.
314 		 */
315 		bzero(ndopts, sizeof(*ndopts));
316 		return NULL;
317 	}
318 
319 	ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
320 	if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
321 		/* option overruns the end of buffer, invalid */
322 		bzero(ndopts, sizeof(*ndopts));
323 		return NULL;
324 	} else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
325 		/* reached the end of options chain */
326 		ndopts->nd_opts_done = 1;
327 		ndopts->nd_opts_search = NULL;
328 	}
329 	return nd_opt;
330 }
331 
332 /*
333  * Parse multiple ND options.
334  * This function is much easier to use, for ND routines that do not need
335  * multiple options of the same type.
336  */
337 int
338 nd6_options(union nd_opts *ndopts)
339 {
340 	struct nd_opt_hdr *nd_opt;
341 	int i = 0;
342 
343 	KASSERT(ndopts != NULL, ("%s: ndopts == NULL", __func__));
344 	KASSERT(ndopts->nd_opts_last != NULL, ("%s: uninitialized ndopts",
345 	    __func__));
346 	if (ndopts->nd_opts_search == NULL)
347 		return 0;
348 
349 	while (1) {
350 		nd_opt = nd6_option(ndopts);
351 		if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
352 			/*
353 			 * Message validation requires that all included
354 			 * options have a length that is greater than zero.
355 			 */
356 			ICMP6STAT_INC(icp6s_nd_badopt);
357 			bzero(ndopts, sizeof(*ndopts));
358 			return -1;
359 		}
360 
361 		if (nd_opt == NULL)
362 			goto skip1;
363 
364 		switch (nd_opt->nd_opt_type) {
365 		case ND_OPT_SOURCE_LINKADDR:
366 		case ND_OPT_TARGET_LINKADDR:
367 		case ND_OPT_MTU:
368 		case ND_OPT_REDIRECTED_HEADER:
369 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
370 				nd6log((LOG_INFO,
371 				    "duplicated ND6 option found (type=%d)\n",
372 				    nd_opt->nd_opt_type));
373 				/* XXX bark? */
374 			} else {
375 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
376 					= nd_opt;
377 			}
378 			break;
379 		case ND_OPT_PREFIX_INFORMATION:
380 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
381 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
382 					= nd_opt;
383 			}
384 			ndopts->nd_opts_pi_end =
385 				(struct nd_opt_prefix_info *)nd_opt;
386 			break;
387 		/* What about ND_OPT_ROUTE_INFO? RFC 4191 */
388 		case ND_OPT_RDNSS:	/* RFC 6106 */
389 		case ND_OPT_DNSSL:	/* RFC 6106 */
390 			/*
391 			 * Silently ignore options we know and do not care about
392 			 * in the kernel.
393 			 */
394 			break;
395 		default:
396 			/*
397 			 * Unknown options must be silently ignored,
398 			 * to accomodate future extension to the protocol.
399 			 */
400 			nd6log((LOG_DEBUG,
401 			    "nd6_options: unsupported option %d - "
402 			    "option ignored\n", nd_opt->nd_opt_type));
403 		}
404 
405 skip1:
406 		i++;
407 		if (i > V_nd6_maxndopt) {
408 			ICMP6STAT_INC(icp6s_nd_toomanyopt);
409 			nd6log((LOG_INFO, "too many loop in nd opt\n"));
410 			break;
411 		}
412 
413 		if (ndopts->nd_opts_done)
414 			break;
415 	}
416 
417 	return 0;
418 }
419 
420 /*
421  * ND6 timer routine to handle ND6 entries
422  */
423 void
424 nd6_llinfo_settimer_locked(struct llentry *ln, long tick)
425 {
426 	int canceled;
427 
428 	LLE_WLOCK_ASSERT(ln);
429 
430 	if (tick < 0) {
431 		ln->la_expire = 0;
432 		ln->ln_ntick = 0;
433 		canceled = callout_stop(&ln->ln_timer_ch);
434 	} else {
435 		ln->la_expire = time_uptime + tick / hz;
436 		LLE_ADDREF(ln);
437 		if (tick > INT_MAX) {
438 			ln->ln_ntick = tick - INT_MAX;
439 			canceled = callout_reset(&ln->ln_timer_ch, INT_MAX,
440 			    nd6_llinfo_timer, ln);
441 		} else {
442 			ln->ln_ntick = 0;
443 			canceled = callout_reset(&ln->ln_timer_ch, tick,
444 			    nd6_llinfo_timer, ln);
445 		}
446 	}
447 	if (canceled)
448 		LLE_REMREF(ln);
449 }
450 
451 void
452 nd6_llinfo_settimer(struct llentry *ln, long tick)
453 {
454 
455 	LLE_WLOCK(ln);
456 	nd6_llinfo_settimer_locked(ln, tick);
457 	LLE_WUNLOCK(ln);
458 }
459 
460 static void
461 nd6_llinfo_timer(void *arg)
462 {
463 	struct llentry *ln;
464 	struct in6_addr *dst;
465 	struct ifnet *ifp;
466 	struct nd_ifinfo *ndi = NULL;
467 
468 	KASSERT(arg != NULL, ("%s: arg NULL", __func__));
469 	ln = (struct llentry *)arg;
470 	LLE_WLOCK_ASSERT(ln);
471 	ifp = ln->lle_tbl->llt_ifp;
472 
473 	CURVNET_SET(ifp->if_vnet);
474 
475 	if (ln->ln_ntick > 0) {
476 		if (ln->ln_ntick > INT_MAX) {
477 			ln->ln_ntick -= INT_MAX;
478 			nd6_llinfo_settimer_locked(ln, INT_MAX);
479 		} else {
480 			ln->ln_ntick = 0;
481 			nd6_llinfo_settimer_locked(ln, ln->ln_ntick);
482 		}
483 		goto done;
484 	}
485 
486 	ndi = ND_IFINFO(ifp);
487 	dst = &L3_ADDR_SIN6(ln)->sin6_addr;
488 	if (ln->la_flags & LLE_STATIC) {
489 		goto done;
490 	}
491 
492 	if (ln->la_flags & LLE_DELETED) {
493 		(void)nd6_free(ln, 0);
494 		ln = NULL;
495 		goto done;
496 	}
497 
498 	switch (ln->ln_state) {
499 	case ND6_LLINFO_INCOMPLETE:
500 		if (ln->la_asked < V_nd6_mmaxtries) {
501 			ln->la_asked++;
502 			nd6_llinfo_settimer_locked(ln, (long)ndi->retrans * hz / 1000);
503 			LLE_WUNLOCK(ln);
504 			nd6_ns_output(ifp, NULL, dst, ln, 0);
505 			LLE_WLOCK(ln);
506 		} else {
507 			struct mbuf *m = ln->la_hold;
508 			if (m) {
509 				struct mbuf *m0;
510 
511 				/*
512 				 * assuming every packet in la_hold has the
513 				 * same IP header.  Send error after unlock.
514 				 */
515 				m0 = m->m_nextpkt;
516 				m->m_nextpkt = NULL;
517 				ln->la_hold = m0;
518 				clear_llinfo_pqueue(ln);
519 			}
520 			EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_TIMEDOUT);
521 			(void)nd6_free(ln, 0);
522 			ln = NULL;
523 			if (m != NULL)
524 				icmp6_error2(m, ICMP6_DST_UNREACH,
525 				    ICMP6_DST_UNREACH_ADDR, 0, ifp);
526 		}
527 		break;
528 	case ND6_LLINFO_REACHABLE:
529 		if (!ND6_LLINFO_PERMANENT(ln)) {
530 			ln->ln_state = ND6_LLINFO_STALE;
531 			nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
532 		}
533 		break;
534 
535 	case ND6_LLINFO_STALE:
536 		/* Garbage Collection(RFC 2461 5.3) */
537 		if (!ND6_LLINFO_PERMANENT(ln)) {
538 			EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_EXPIRED);
539 			(void)nd6_free(ln, 1);
540 			ln = NULL;
541 		}
542 		break;
543 
544 	case ND6_LLINFO_DELAY:
545 		if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
546 			/* We need NUD */
547 			ln->la_asked = 1;
548 			ln->ln_state = ND6_LLINFO_PROBE;
549 			nd6_llinfo_settimer_locked(ln, (long)ndi->retrans * hz / 1000);
550 			LLE_WUNLOCK(ln);
551 			nd6_ns_output(ifp, dst, dst, ln, 0);
552 			LLE_WLOCK(ln);
553 		} else {
554 			ln->ln_state = ND6_LLINFO_STALE; /* XXX */
555 			nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
556 		}
557 		break;
558 	case ND6_LLINFO_PROBE:
559 		if (ln->la_asked < V_nd6_umaxtries) {
560 			ln->la_asked++;
561 			nd6_llinfo_settimer_locked(ln, (long)ndi->retrans * hz / 1000);
562 			LLE_WUNLOCK(ln);
563 			nd6_ns_output(ifp, dst, dst, ln, 0);
564 			LLE_WLOCK(ln);
565 		} else {
566 			EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_EXPIRED);
567 			(void)nd6_free(ln, 0);
568 			ln = NULL;
569 		}
570 		break;
571 	default:
572 		panic("%s: paths in a dark night can be confusing: %d",
573 		    __func__, ln->ln_state);
574 	}
575 done:
576 	if (ln != NULL)
577 		LLE_FREE_LOCKED(ln);
578 	CURVNET_RESTORE();
579 }
580 
581 
582 /*
583  * ND6 timer routine to expire default route list and prefix list
584  */
585 void
586 nd6_timer(void *arg)
587 {
588 	CURVNET_SET((struct vnet *) arg);
589 	struct nd_defrouter *dr, *ndr;
590 	struct nd_prefix *pr, *npr;
591 	struct in6_ifaddr *ia6, *nia6;
592 
593 	callout_reset(&V_nd6_timer_ch, V_nd6_prune * hz,
594 	    nd6_timer, curvnet);
595 
596 	/* expire default router list */
597 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
598 		if (dr->expire && dr->expire < time_uptime)
599 			defrtrlist_del(dr);
600 	}
601 
602 	/*
603 	 * expire interface addresses.
604 	 * in the past the loop was inside prefix expiry processing.
605 	 * However, from a stricter speci-confrmance standpoint, we should
606 	 * rather separate address lifetimes and prefix lifetimes.
607 	 *
608 	 * XXXRW: in6_ifaddrhead locking.
609 	 */
610   addrloop:
611 	TAILQ_FOREACH_SAFE(ia6, &V_in6_ifaddrhead, ia_link, nia6) {
612 		/* check address lifetime */
613 		if (IFA6_IS_INVALID(ia6)) {
614 			int regen = 0;
615 
616 			/*
617 			 * If the expiring address is temporary, try
618 			 * regenerating a new one.  This would be useful when
619 			 * we suspended a laptop PC, then turned it on after a
620 			 * period that could invalidate all temporary
621 			 * addresses.  Although we may have to restart the
622 			 * loop (see below), it must be after purging the
623 			 * address.  Otherwise, we'd see an infinite loop of
624 			 * regeneration.
625 			 */
626 			if (V_ip6_use_tempaddr &&
627 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
628 				if (regen_tmpaddr(ia6) == 0)
629 					regen = 1;
630 			}
631 
632 			in6_purgeaddr(&ia6->ia_ifa);
633 
634 			if (regen)
635 				goto addrloop; /* XXX: see below */
636 		} else if (IFA6_IS_DEPRECATED(ia6)) {
637 			int oldflags = ia6->ia6_flags;
638 
639 			ia6->ia6_flags |= IN6_IFF_DEPRECATED;
640 
641 			/*
642 			 * If a temporary address has just become deprecated,
643 			 * regenerate a new one if possible.
644 			 */
645 			if (V_ip6_use_tempaddr &&
646 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
647 			    (oldflags & IN6_IFF_DEPRECATED) == 0) {
648 
649 				if (regen_tmpaddr(ia6) == 0) {
650 					/*
651 					 * A new temporary address is
652 					 * generated.
653 					 * XXX: this means the address chain
654 					 * has changed while we are still in
655 					 * the loop.  Although the change
656 					 * would not cause disaster (because
657 					 * it's not a deletion, but an
658 					 * addition,) we'd rather restart the
659 					 * loop just for safety.  Or does this
660 					 * significantly reduce performance??
661 					 */
662 					goto addrloop;
663 				}
664 			}
665 		} else {
666 			/*
667 			 * A new RA might have made a deprecated address
668 			 * preferred.
669 			 */
670 			ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
671 		}
672 	}
673 
674 	/* expire prefix list */
675 	LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) {
676 		/*
677 		 * check prefix lifetime.
678 		 * since pltime is just for autoconf, pltime processing for
679 		 * prefix is not necessary.
680 		 */
681 		if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
682 		    time_uptime - pr->ndpr_lastupdate > pr->ndpr_vltime) {
683 
684 			/*
685 			 * address expiration and prefix expiration are
686 			 * separate.  NEVER perform in6_purgeaddr here.
687 			 */
688 			prelist_remove(pr);
689 		}
690 	}
691 	CURVNET_RESTORE();
692 }
693 
694 /*
695  * ia6 - deprecated/invalidated temporary address
696  */
697 static int
698 regen_tmpaddr(struct in6_ifaddr *ia6)
699 {
700 	struct ifaddr *ifa;
701 	struct ifnet *ifp;
702 	struct in6_ifaddr *public_ifa6 = NULL;
703 
704 	ifp = ia6->ia_ifa.ifa_ifp;
705 	IF_ADDR_RLOCK(ifp);
706 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
707 		struct in6_ifaddr *it6;
708 
709 		if (ifa->ifa_addr->sa_family != AF_INET6)
710 			continue;
711 
712 		it6 = (struct in6_ifaddr *)ifa;
713 
714 		/* ignore no autoconf addresses. */
715 		if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
716 			continue;
717 
718 		/* ignore autoconf addresses with different prefixes. */
719 		if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
720 			continue;
721 
722 		/*
723 		 * Now we are looking at an autoconf address with the same
724 		 * prefix as ours.  If the address is temporary and is still
725 		 * preferred, do not create another one.  It would be rare, but
726 		 * could happen, for example, when we resume a laptop PC after
727 		 * a long period.
728 		 */
729 		if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
730 		    !IFA6_IS_DEPRECATED(it6)) {
731 			public_ifa6 = NULL;
732 			break;
733 		}
734 
735 		/*
736 		 * This is a public autoconf address that has the same prefix
737 		 * as ours.  If it is preferred, keep it.  We can't break the
738 		 * loop here, because there may be a still-preferred temporary
739 		 * address with the prefix.
740 		 */
741 		if (!IFA6_IS_DEPRECATED(it6))
742 		    public_ifa6 = it6;
743 
744 		if (public_ifa6 != NULL)
745 			ifa_ref(&public_ifa6->ia_ifa);
746 	}
747 	IF_ADDR_RUNLOCK(ifp);
748 
749 	if (public_ifa6 != NULL) {
750 		int e;
751 
752 		if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
753 			ifa_free(&public_ifa6->ia_ifa);
754 			log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
755 			    " tmp addr,errno=%d\n", e);
756 			return (-1);
757 		}
758 		ifa_free(&public_ifa6->ia_ifa);
759 		return (0);
760 	}
761 
762 	return (-1);
763 }
764 
765 /*
766  * Nuke neighbor cache/prefix/default router management table, right before
767  * ifp goes away.
768  */
769 void
770 nd6_purge(struct ifnet *ifp)
771 {
772 	struct nd_defrouter *dr, *ndr;
773 	struct nd_prefix *pr, *npr;
774 
775 	/*
776 	 * Nuke default router list entries toward ifp.
777 	 * We defer removal of default router list entries that is installed
778 	 * in the routing table, in order to keep additional side effects as
779 	 * small as possible.
780 	 */
781 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
782 		if (dr->installed)
783 			continue;
784 
785 		if (dr->ifp == ifp)
786 			defrtrlist_del(dr);
787 	}
788 
789 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
790 		if (!dr->installed)
791 			continue;
792 
793 		if (dr->ifp == ifp)
794 			defrtrlist_del(dr);
795 	}
796 
797 	/* Nuke prefix list entries toward ifp */
798 	LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) {
799 		if (pr->ndpr_ifp == ifp) {
800 			/*
801 			 * Because if_detach() does *not* release prefixes
802 			 * while purging addresses the reference count will
803 			 * still be above zero. We therefore reset it to
804 			 * make sure that the prefix really gets purged.
805 			 */
806 			pr->ndpr_refcnt = 0;
807 
808 			/*
809 			 * Previously, pr->ndpr_addr is removed as well,
810 			 * but I strongly believe we don't have to do it.
811 			 * nd6_purge() is only called from in6_ifdetach(),
812 			 * which removes all the associated interface addresses
813 			 * by itself.
814 			 * (jinmei@kame.net 20010129)
815 			 */
816 			prelist_remove(pr);
817 		}
818 	}
819 
820 	/* cancel default outgoing interface setting */
821 	if (V_nd6_defifindex == ifp->if_index)
822 		nd6_setdefaultiface(0);
823 
824 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
825 		/* Refresh default router list. */
826 		defrouter_select();
827 	}
828 
829 	/* XXXXX
830 	 * We do not nuke the neighbor cache entries here any more
831 	 * because the neighbor cache is kept in if_afdata[AF_INET6].
832 	 * nd6_purge() is invoked by in6_ifdetach() which is called
833 	 * from if_detach() where everything gets purged. So let
834 	 * in6_domifdetach() do the actual L2 table purging work.
835 	 */
836 }
837 
838 /*
839  * the caller acquires and releases the lock on the lltbls
840  * Returns the llentry locked
841  */
842 struct llentry *
843 nd6_lookup(struct in6_addr *addr6, int flags, struct ifnet *ifp)
844 {
845 	struct sockaddr_in6 sin6;
846 	struct llentry *ln;
847 	int llflags;
848 
849 	bzero(&sin6, sizeof(sin6));
850 	sin6.sin6_len = sizeof(struct sockaddr_in6);
851 	sin6.sin6_family = AF_INET6;
852 	sin6.sin6_addr = *addr6;
853 
854 	IF_AFDATA_LOCK_ASSERT(ifp);
855 
856 	llflags = 0;
857 	if (flags & ND6_CREATE)
858 	    llflags |= LLE_CREATE;
859 	if (flags & ND6_EXCLUSIVE)
860 	    llflags |= LLE_EXCLUSIVE;
861 
862 	ln = lla_lookup(LLTABLE6(ifp), llflags, (struct sockaddr *)&sin6);
863 	if ((ln != NULL) && (llflags & LLE_CREATE))
864 		ln->ln_state = ND6_LLINFO_NOSTATE;
865 
866 	return (ln);
867 }
868 
869 /*
870  * Test whether a given IPv6 address is a neighbor or not, ignoring
871  * the actual neighbor cache.  The neighbor cache is ignored in order
872  * to not reenter the routing code from within itself.
873  */
874 static int
875 nd6_is_new_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
876 {
877 	struct nd_prefix *pr;
878 	struct ifaddr *dstaddr;
879 
880 	/*
881 	 * A link-local address is always a neighbor.
882 	 * XXX: a link does not necessarily specify a single interface.
883 	 */
884 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
885 		struct sockaddr_in6 sin6_copy;
886 		u_int32_t zone;
887 
888 		/*
889 		 * We need sin6_copy since sa6_recoverscope() may modify the
890 		 * content (XXX).
891 		 */
892 		sin6_copy = *addr;
893 		if (sa6_recoverscope(&sin6_copy))
894 			return (0); /* XXX: should be impossible */
895 		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
896 			return (0);
897 		if (sin6_copy.sin6_scope_id == zone)
898 			return (1);
899 		else
900 			return (0);
901 	}
902 
903 	/*
904 	 * If the address matches one of our addresses,
905 	 * it should be a neighbor.
906 	 * If the address matches one of our on-link prefixes, it should be a
907 	 * neighbor.
908 	 */
909 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
910 		if (pr->ndpr_ifp != ifp)
911 			continue;
912 
913 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) {
914 			struct rtentry *rt;
915 
916 			/* Always use the default FIB here. */
917 			rt = in6_rtalloc1((struct sockaddr *)&pr->ndpr_prefix,
918 			    0, 0, RT_DEFAULT_FIB);
919 			if (rt == NULL)
920 				continue;
921 			/*
922 			 * This is the case where multiple interfaces
923 			 * have the same prefix, but only one is installed
924 			 * into the routing table and that prefix entry
925 			 * is not the one being examined here. In the case
926 			 * where RADIX_MPATH is enabled, multiple route
927 			 * entries (of the same rt_key value) will be
928 			 * installed because the interface addresses all
929 			 * differ.
930 			 */
931 			if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
932 			       &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr)) {
933 				RTFREE_LOCKED(rt);
934 				continue;
935 			}
936 			RTFREE_LOCKED(rt);
937 		}
938 
939 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
940 		    &addr->sin6_addr, &pr->ndpr_mask))
941 			return (1);
942 	}
943 
944 	/*
945 	 * If the address is assigned on the node of the other side of
946 	 * a p2p interface, the address should be a neighbor.
947 	 */
948 	dstaddr = ifa_ifwithdstaddr((struct sockaddr *)addr, RT_ALL_FIBS);
949 	if (dstaddr != NULL) {
950 		if (dstaddr->ifa_ifp == ifp) {
951 			ifa_free(dstaddr);
952 			return (1);
953 		}
954 		ifa_free(dstaddr);
955 	}
956 
957 	/*
958 	 * If the default router list is empty, all addresses are regarded
959 	 * as on-link, and thus, as a neighbor.
960 	 */
961 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV &&
962 	    TAILQ_EMPTY(&V_nd_defrouter) &&
963 	    V_nd6_defifindex == ifp->if_index) {
964 		return (1);
965 	}
966 
967 	return (0);
968 }
969 
970 
971 /*
972  * Detect if a given IPv6 address identifies a neighbor on a given link.
973  * XXX: should take care of the destination of a p2p link?
974  */
975 int
976 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
977 {
978 	struct llentry *lle;
979 	int rc = 0;
980 
981 	IF_AFDATA_UNLOCK_ASSERT(ifp);
982 	if (nd6_is_new_addr_neighbor(addr, ifp))
983 		return (1);
984 
985 	/*
986 	 * Even if the address matches none of our addresses, it might be
987 	 * in the neighbor cache.
988 	 */
989 	IF_AFDATA_RLOCK(ifp);
990 	if ((lle = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL) {
991 		LLE_RUNLOCK(lle);
992 		rc = 1;
993 	}
994 	IF_AFDATA_RUNLOCK(ifp);
995 	return (rc);
996 }
997 
998 /*
999  * Free an nd6 llinfo entry.
1000  * Since the function would cause significant changes in the kernel, DO NOT
1001  * make it global, unless you have a strong reason for the change, and are sure
1002  * that the change is safe.
1003  */
1004 static struct llentry *
1005 nd6_free(struct llentry *ln, int gc)
1006 {
1007         struct llentry *next;
1008 	struct nd_defrouter *dr;
1009 	struct ifnet *ifp;
1010 
1011 	LLE_WLOCK_ASSERT(ln);
1012 
1013 	/*
1014 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1015 	 * even though it is not harmful, it was not really necessary.
1016 	 */
1017 
1018 	/* cancel timer */
1019 	nd6_llinfo_settimer_locked(ln, -1);
1020 
1021 	ifp = ln->lle_tbl->llt_ifp;
1022 
1023 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1024 		dr = defrouter_lookup(&L3_ADDR_SIN6(ln)->sin6_addr, ifp);
1025 
1026 		if (dr != NULL && dr->expire &&
1027 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
1028 			/*
1029 			 * If the reason for the deletion is just garbage
1030 			 * collection, and the neighbor is an active default
1031 			 * router, do not delete it.  Instead, reset the GC
1032 			 * timer using the router's lifetime.
1033 			 * Simply deleting the entry would affect default
1034 			 * router selection, which is not necessarily a good
1035 			 * thing, especially when we're using router preference
1036 			 * values.
1037 			 * XXX: the check for ln_state would be redundant,
1038 			 *      but we intentionally keep it just in case.
1039 			 */
1040 			if (dr->expire > time_uptime)
1041 				nd6_llinfo_settimer_locked(ln,
1042 				    (dr->expire - time_uptime) * hz);
1043 			else
1044 				nd6_llinfo_settimer_locked(ln,
1045 				    (long)V_nd6_gctimer * hz);
1046 
1047 			next = LIST_NEXT(ln, lle_next);
1048 			LLE_REMREF(ln);
1049 			LLE_WUNLOCK(ln);
1050 			return (next);
1051 		}
1052 
1053 		if (dr) {
1054 			/*
1055 			 * Unreachablity of a router might affect the default
1056 			 * router selection and on-link detection of advertised
1057 			 * prefixes.
1058 			 */
1059 
1060 			/*
1061 			 * Temporarily fake the state to choose a new default
1062 			 * router and to perform on-link determination of
1063 			 * prefixes correctly.
1064 			 * Below the state will be set correctly,
1065 			 * or the entry itself will be deleted.
1066 			 */
1067 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
1068 		}
1069 
1070 		if (ln->ln_router || dr) {
1071 
1072 			/*
1073 			 * We need to unlock to avoid a LOR with rt6_flush() with the
1074 			 * rnh and for the calls to pfxlist_onlink_check() and
1075 			 * defrouter_select() in the block further down for calls
1076 			 * into nd6_lookup().  We still hold a ref.
1077 			 */
1078 			LLE_WUNLOCK(ln);
1079 
1080 			/*
1081 			 * rt6_flush must be called whether or not the neighbor
1082 			 * is in the Default Router List.
1083 			 * See a corresponding comment in nd6_na_input().
1084 			 */
1085 			rt6_flush(&L3_ADDR_SIN6(ln)->sin6_addr, ifp);
1086 		}
1087 
1088 		if (dr) {
1089 			/*
1090 			 * Since defrouter_select() does not affect the
1091 			 * on-link determination and MIP6 needs the check
1092 			 * before the default router selection, we perform
1093 			 * the check now.
1094 			 */
1095 			pfxlist_onlink_check();
1096 
1097 			/*
1098 			 * Refresh default router list.
1099 			 */
1100 			defrouter_select();
1101 		}
1102 
1103 		if (ln->ln_router || dr)
1104 			LLE_WLOCK(ln);
1105 	}
1106 
1107 	/*
1108 	 * Before deleting the entry, remember the next entry as the
1109 	 * return value.  We need this because pfxlist_onlink_check() above
1110 	 * might have freed other entries (particularly the old next entry) as
1111 	 * a side effect (XXX).
1112 	 */
1113 	next = LIST_NEXT(ln, lle_next);
1114 
1115 	/*
1116 	 * Save to unlock. We still hold an extra reference and will not
1117 	 * free(9) in llentry_free() if someone else holds one as well.
1118 	 */
1119 	LLE_WUNLOCK(ln);
1120 	IF_AFDATA_LOCK(ifp);
1121 	LLE_WLOCK(ln);
1122 
1123 	/* Guard against race with other llentry_free(). */
1124 	if (ln->la_flags & LLE_LINKED) {
1125 		LLE_REMREF(ln);
1126 		llentry_free(ln);
1127 	} else
1128 		LLE_FREE_LOCKED(ln);
1129 
1130 	IF_AFDATA_UNLOCK(ifp);
1131 
1132 	return (next);
1133 }
1134 
1135 /*
1136  * Upper-layer reachability hint for Neighbor Unreachability Detection.
1137  *
1138  * XXX cost-effective methods?
1139  */
1140 void
1141 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1142 {
1143 	struct llentry *ln;
1144 	struct ifnet *ifp;
1145 
1146 	if ((dst6 == NULL) || (rt == NULL))
1147 		return;
1148 
1149 	ifp = rt->rt_ifp;
1150 	IF_AFDATA_RLOCK(ifp);
1151 	ln = nd6_lookup(dst6, ND6_EXCLUSIVE, NULL);
1152 	IF_AFDATA_RUNLOCK(ifp);
1153 	if (ln == NULL)
1154 		return;
1155 
1156 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
1157 		goto done;
1158 
1159 	/*
1160 	 * if we get upper-layer reachability confirmation many times,
1161 	 * it is possible we have false information.
1162 	 */
1163 	if (!force) {
1164 		ln->ln_byhint++;
1165 		if (ln->ln_byhint > V_nd6_maxnudhint) {
1166 			goto done;
1167 		}
1168 	}
1169 
1170  	ln->ln_state = ND6_LLINFO_REACHABLE;
1171 	if (!ND6_LLINFO_PERMANENT(ln)) {
1172 		nd6_llinfo_settimer_locked(ln,
1173 		    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
1174 	}
1175 done:
1176 	LLE_WUNLOCK(ln);
1177 }
1178 
1179 
1180 /*
1181  * Rejuvenate this function for routing operations related
1182  * processing.
1183  */
1184 void
1185 nd6_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
1186 {
1187 	struct sockaddr_in6 *gateway;
1188 	struct nd_defrouter *dr;
1189 	struct ifnet *ifp;
1190 
1191 	gateway = (struct sockaddr_in6 *)rt->rt_gateway;
1192 	ifp = rt->rt_ifp;
1193 
1194 	switch (req) {
1195 	case RTM_ADD:
1196 		break;
1197 
1198 	case RTM_DELETE:
1199 		if (!ifp)
1200 			return;
1201 		/*
1202 		 * Only indirect routes are interesting.
1203 		 */
1204 		if ((rt->rt_flags & RTF_GATEWAY) == 0)
1205 			return;
1206 		/*
1207 		 * check for default route
1208 		 */
1209 		if (IN6_ARE_ADDR_EQUAL(&in6addr_any,
1210 				       &SIN6(rt_key(rt))->sin6_addr)) {
1211 
1212 			dr = defrouter_lookup(&gateway->sin6_addr, ifp);
1213 			if (dr != NULL)
1214 				dr->installed = 0;
1215 		}
1216 		break;
1217 	}
1218 }
1219 
1220 
1221 int
1222 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1223 {
1224 	struct in6_drlist *drl = (struct in6_drlist *)data;
1225 	struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1226 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1227 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1228 	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1229 	struct nd_defrouter *dr;
1230 	struct nd_prefix *pr;
1231 	int i = 0, error = 0;
1232 
1233 	if (ifp->if_afdata[AF_INET6] == NULL)
1234 		return (EPFNOSUPPORT);
1235 	switch (cmd) {
1236 	case SIOCGDRLST_IN6:
1237 		/*
1238 		 * obsolete API, use sysctl under net.inet6.icmp6
1239 		 */
1240 		bzero(drl, sizeof(*drl));
1241 		TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
1242 			if (i >= DRLSTSIZ)
1243 				break;
1244 			drl->defrouter[i].rtaddr = dr->rtaddr;
1245 			in6_clearscope(&drl->defrouter[i].rtaddr);
1246 
1247 			drl->defrouter[i].flags = dr->flags;
1248 			drl->defrouter[i].rtlifetime = dr->rtlifetime;
1249 			drl->defrouter[i].expire = dr->expire +
1250 			    (time_second - time_uptime);
1251 			drl->defrouter[i].if_index = dr->ifp->if_index;
1252 			i++;
1253 		}
1254 		break;
1255 	case SIOCGPRLST_IN6:
1256 		/*
1257 		 * obsolete API, use sysctl under net.inet6.icmp6
1258 		 *
1259 		 * XXX the structure in6_prlist was changed in backward-
1260 		 * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
1261 		 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1262 		 */
1263 		/*
1264 		 * XXX meaning of fields, especialy "raflags", is very
1265 		 * differnet between RA prefix list and RR/static prefix list.
1266 		 * how about separating ioctls into two?
1267 		 */
1268 		bzero(oprl, sizeof(*oprl));
1269 		LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1270 			struct nd_pfxrouter *pfr;
1271 			int j;
1272 
1273 			if (i >= PRLSTSIZ)
1274 				break;
1275 			oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1276 			oprl->prefix[i].raflags = pr->ndpr_raf;
1277 			oprl->prefix[i].prefixlen = pr->ndpr_plen;
1278 			oprl->prefix[i].vltime = pr->ndpr_vltime;
1279 			oprl->prefix[i].pltime = pr->ndpr_pltime;
1280 			oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1281 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1282 				oprl->prefix[i].expire = 0;
1283 			else {
1284 				time_t maxexpire;
1285 
1286 				/* XXX: we assume time_t is signed. */
1287 				maxexpire = (-1) &
1288 				    ~((time_t)1 <<
1289 				    ((sizeof(maxexpire) * 8) - 1));
1290 				if (pr->ndpr_vltime <
1291 				    maxexpire - pr->ndpr_lastupdate) {
1292 					oprl->prefix[i].expire =
1293 					    pr->ndpr_lastupdate +
1294 					    pr->ndpr_vltime +
1295 					    (time_second - time_uptime);
1296 				} else
1297 					oprl->prefix[i].expire = maxexpire;
1298 			}
1299 
1300 			j = 0;
1301 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
1302 				if (j < DRLSTSIZ) {
1303 #define RTRADDR oprl->prefix[i].advrtr[j]
1304 					RTRADDR = pfr->router->rtaddr;
1305 					in6_clearscope(&RTRADDR);
1306 #undef RTRADDR
1307 				}
1308 				j++;
1309 			}
1310 			oprl->prefix[i].advrtrs = j;
1311 			oprl->prefix[i].origin = PR_ORIG_RA;
1312 
1313 			i++;
1314 		}
1315 
1316 		break;
1317 	case OSIOCGIFINFO_IN6:
1318 #define ND	ndi->ndi
1319 		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
1320 		bzero(&ND, sizeof(ND));
1321 		ND.linkmtu = IN6_LINKMTU(ifp);
1322 		ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1323 		ND.basereachable = ND_IFINFO(ifp)->basereachable;
1324 		ND.reachable = ND_IFINFO(ifp)->reachable;
1325 		ND.retrans = ND_IFINFO(ifp)->retrans;
1326 		ND.flags = ND_IFINFO(ifp)->flags;
1327 		ND.recalctm = ND_IFINFO(ifp)->recalctm;
1328 		ND.chlim = ND_IFINFO(ifp)->chlim;
1329 		break;
1330 	case SIOCGIFINFO_IN6:
1331 		ND = *ND_IFINFO(ifp);
1332 		break;
1333 	case SIOCSIFINFO_IN6:
1334 		/*
1335 		 * used to change host variables from userland.
1336 		 * intented for a use on router to reflect RA configurations.
1337 		 */
1338 		/* 0 means 'unspecified' */
1339 		if (ND.linkmtu != 0) {
1340 			if (ND.linkmtu < IPV6_MMTU ||
1341 			    ND.linkmtu > IN6_LINKMTU(ifp)) {
1342 				error = EINVAL;
1343 				break;
1344 			}
1345 			ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1346 		}
1347 
1348 		if (ND.basereachable != 0) {
1349 			int obasereachable = ND_IFINFO(ifp)->basereachable;
1350 
1351 			ND_IFINFO(ifp)->basereachable = ND.basereachable;
1352 			if (ND.basereachable != obasereachable)
1353 				ND_IFINFO(ifp)->reachable =
1354 				    ND_COMPUTE_RTIME(ND.basereachable);
1355 		}
1356 		if (ND.retrans != 0)
1357 			ND_IFINFO(ifp)->retrans = ND.retrans;
1358 		if (ND.chlim != 0)
1359 			ND_IFINFO(ifp)->chlim = ND.chlim;
1360 		/* FALLTHROUGH */
1361 	case SIOCSIFINFO_FLAGS:
1362 	{
1363 		struct ifaddr *ifa;
1364 		struct in6_ifaddr *ia;
1365 
1366 		if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1367 		    !(ND.flags & ND6_IFF_IFDISABLED)) {
1368 			/* ifdisabled 1->0 transision */
1369 
1370 			/*
1371 			 * If the interface is marked as ND6_IFF_IFDISABLED and
1372 			 * has an link-local address with IN6_IFF_DUPLICATED,
1373 			 * do not clear ND6_IFF_IFDISABLED.
1374 			 * See RFC 4862, Section 5.4.5.
1375 			 */
1376 			int duplicated_linklocal = 0;
1377 
1378 			IF_ADDR_RLOCK(ifp);
1379 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1380 				if (ifa->ifa_addr->sa_family != AF_INET6)
1381 					continue;
1382 				ia = (struct in6_ifaddr *)ifa;
1383 				if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
1384 				    IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) {
1385 					duplicated_linklocal = 1;
1386 					break;
1387 				}
1388 			}
1389 			IF_ADDR_RUNLOCK(ifp);
1390 
1391 			if (duplicated_linklocal) {
1392 				ND.flags |= ND6_IFF_IFDISABLED;
1393 				log(LOG_ERR, "Cannot enable an interface"
1394 				    " with a link-local address marked"
1395 				    " duplicate.\n");
1396 			} else {
1397 				ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED;
1398 				if (ifp->if_flags & IFF_UP)
1399 					in6_if_up(ifp);
1400 			}
1401 		} else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1402 			    (ND.flags & ND6_IFF_IFDISABLED)) {
1403 			/* ifdisabled 0->1 transision */
1404 			/* Mark all IPv6 address as tentative. */
1405 
1406 			ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1407 			IF_ADDR_RLOCK(ifp);
1408 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1409 				if (ifa->ifa_addr->sa_family != AF_INET6)
1410 					continue;
1411 				ia = (struct in6_ifaddr *)ifa;
1412 				ia->ia6_flags |= IN6_IFF_TENTATIVE;
1413 			}
1414 			IF_ADDR_RUNLOCK(ifp);
1415 		}
1416 
1417 		if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) {
1418 			if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) {
1419 				/* auto_linklocal 0->1 transision */
1420 
1421 				/* If no link-local address on ifp, configure */
1422 				ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL;
1423 				in6_ifattach(ifp, NULL);
1424 			} else if (!(ND.flags & ND6_IFF_IFDISABLED) &&
1425 			    ifp->if_flags & IFF_UP) {
1426 				/*
1427 				 * When the IF already has
1428 				 * ND6_IFF_AUTO_LINKLOCAL, no link-local
1429 				 * address is assigned, and IFF_UP, try to
1430 				 * assign one.
1431 				 */
1432 				int haslinklocal = 0;
1433 
1434 				IF_ADDR_RLOCK(ifp);
1435 				TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1436 					if (ifa->ifa_addr->sa_family != AF_INET6)
1437 						continue;
1438 					ia = (struct in6_ifaddr *)ifa;
1439 					if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) {
1440 						haslinklocal = 1;
1441 						break;
1442 					}
1443 				}
1444 				IF_ADDR_RUNLOCK(ifp);
1445 				if (!haslinklocal)
1446 					in6_ifattach(ifp, NULL);
1447 			}
1448 		}
1449 	}
1450 		ND_IFINFO(ifp)->flags = ND.flags;
1451 		break;
1452 #undef ND
1453 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1454 		/* sync kernel routing table with the default router list */
1455 		defrouter_reset();
1456 		defrouter_select();
1457 		break;
1458 	case SIOCSPFXFLUSH_IN6:
1459 	{
1460 		/* flush all the prefix advertised by routers */
1461 		struct nd_prefix *pr, *next;
1462 
1463 		LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, next) {
1464 			struct in6_ifaddr *ia, *ia_next;
1465 
1466 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1467 				continue; /* XXX */
1468 
1469 			/* do we really have to remove addresses as well? */
1470 			/* XXXRW: in6_ifaddrhead locking. */
1471 			TAILQ_FOREACH_SAFE(ia, &V_in6_ifaddrhead, ia_link,
1472 			    ia_next) {
1473 				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1474 					continue;
1475 
1476 				if (ia->ia6_ndpr == pr)
1477 					in6_purgeaddr(&ia->ia_ifa);
1478 			}
1479 			prelist_remove(pr);
1480 		}
1481 		break;
1482 	}
1483 	case SIOCSRTRFLUSH_IN6:
1484 	{
1485 		/* flush all the default routers */
1486 		struct nd_defrouter *dr, *next;
1487 
1488 		defrouter_reset();
1489 		TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, next) {
1490 			defrtrlist_del(dr);
1491 		}
1492 		defrouter_select();
1493 		break;
1494 	}
1495 	case SIOCGNBRINFO_IN6:
1496 	{
1497 		struct llentry *ln;
1498 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1499 
1500 		if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1501 			return (error);
1502 
1503 		IF_AFDATA_RLOCK(ifp);
1504 		ln = nd6_lookup(&nb_addr, 0, ifp);
1505 		IF_AFDATA_RUNLOCK(ifp);
1506 
1507 		if (ln == NULL) {
1508 			error = EINVAL;
1509 			break;
1510 		}
1511 		nbi->state = ln->ln_state;
1512 		nbi->asked = ln->la_asked;
1513 		nbi->isrouter = ln->ln_router;
1514 		if (ln->la_expire == 0)
1515 			nbi->expire = 0;
1516 		else
1517 			nbi->expire = ln->la_expire +
1518 			    (time_second - time_uptime);
1519 		LLE_RUNLOCK(ln);
1520 		break;
1521 	}
1522 	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1523 		ndif->ifindex = V_nd6_defifindex;
1524 		break;
1525 	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1526 		return (nd6_setdefaultiface(ndif->ifindex));
1527 	}
1528 	return (error);
1529 }
1530 
1531 /*
1532  * Create neighbor cache entry and cache link-layer address,
1533  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1534  *
1535  * type - ICMP6 type
1536  * code - type dependent information
1537  *
1538  * XXXXX
1539  *  The caller of this function already acquired the ndp
1540  *  cache table lock because the cache entry is returned.
1541  */
1542 struct llentry *
1543 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1544     int lladdrlen, int type, int code)
1545 {
1546 	struct llentry *ln = NULL;
1547 	int is_newentry;
1548 	int do_update;
1549 	int olladdr;
1550 	int llchange;
1551 	int flags;
1552 	int newstate = 0;
1553 	uint16_t router = 0;
1554 	struct sockaddr_in6 sin6;
1555 	struct mbuf *chain = NULL;
1556 	int static_route = 0;
1557 
1558 	IF_AFDATA_UNLOCK_ASSERT(ifp);
1559 
1560 	KASSERT(ifp != NULL, ("%s: ifp == NULL", __func__));
1561 	KASSERT(from != NULL, ("%s: from == NULL", __func__));
1562 
1563 	/* nothing must be updated for unspecified address */
1564 	if (IN6_IS_ADDR_UNSPECIFIED(from))
1565 		return NULL;
1566 
1567 	/*
1568 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1569 	 * the caller.
1570 	 *
1571 	 * XXX If the link does not have link-layer adderss, what should
1572 	 * we do? (ifp->if_addrlen == 0)
1573 	 * Spec says nothing in sections for RA, RS and NA.  There's small
1574 	 * description on it in NS section (RFC 2461 7.2.3).
1575 	 */
1576 	flags = lladdr ? ND6_EXCLUSIVE : 0;
1577 	IF_AFDATA_RLOCK(ifp);
1578 	ln = nd6_lookup(from, flags, ifp);
1579 	IF_AFDATA_RUNLOCK(ifp);
1580 	if (ln == NULL) {
1581 		flags |= ND6_EXCLUSIVE;
1582 		IF_AFDATA_LOCK(ifp);
1583 		ln = nd6_lookup(from, flags | ND6_CREATE, ifp);
1584 		IF_AFDATA_UNLOCK(ifp);
1585 		is_newentry = 1;
1586 	} else {
1587 		/* do nothing if static ndp is set */
1588 		if (ln->la_flags & LLE_STATIC) {
1589 			static_route = 1;
1590 			goto done;
1591 		}
1592 		is_newentry = 0;
1593 	}
1594 	if (ln == NULL)
1595 		return (NULL);
1596 
1597 	olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
1598 	if (olladdr && lladdr) {
1599 		llchange = bcmp(lladdr, &ln->ll_addr,
1600 		    ifp->if_addrlen);
1601 	} else
1602 		llchange = 0;
1603 
1604 	/*
1605 	 * newentry olladdr  lladdr  llchange	(*=record)
1606 	 *	0	n	n	--	(1)
1607 	 *	0	y	n	--	(2)
1608 	 *	0	n	y	--	(3) * STALE
1609 	 *	0	y	y	n	(4) *
1610 	 *	0	y	y	y	(5) * STALE
1611 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1612 	 *	1	--	y	--	(7) * STALE
1613 	 */
1614 
1615 	if (lladdr) {		/* (3-5) and (7) */
1616 		/*
1617 		 * Record source link-layer address
1618 		 * XXX is it dependent to ifp->if_type?
1619 		 */
1620 		bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
1621 		ln->la_flags |= LLE_VALID;
1622 		EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
1623 	}
1624 
1625 	if (!is_newentry) {
1626 		if ((!olladdr && lladdr != NULL) ||	/* (3) */
1627 		    (olladdr && lladdr != NULL && llchange)) {	/* (5) */
1628 			do_update = 1;
1629 			newstate = ND6_LLINFO_STALE;
1630 		} else					/* (1-2,4) */
1631 			do_update = 0;
1632 	} else {
1633 		do_update = 1;
1634 		if (lladdr == NULL)			/* (6) */
1635 			newstate = ND6_LLINFO_NOSTATE;
1636 		else					/* (7) */
1637 			newstate = ND6_LLINFO_STALE;
1638 	}
1639 
1640 	if (do_update) {
1641 		/*
1642 		 * Update the state of the neighbor cache.
1643 		 */
1644 		ln->ln_state = newstate;
1645 
1646 		if (ln->ln_state == ND6_LLINFO_STALE) {
1647 			/*
1648 			 * XXX: since nd6_output() below will cause
1649 			 * state tansition to DELAY and reset the timer,
1650 			 * we must set the timer now, although it is actually
1651 			 * meaningless.
1652 			 */
1653 			nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
1654 
1655 			if (ln->la_hold) {
1656 				struct mbuf *m_hold, *m_hold_next;
1657 
1658 				/*
1659 				 * reset the la_hold in advance, to explicitly
1660 				 * prevent a la_hold lookup in nd6_output()
1661 				 * (wouldn't happen, though...)
1662 				 */
1663 				for (m_hold = ln->la_hold, ln->la_hold = NULL;
1664 				    m_hold; m_hold = m_hold_next) {
1665 					m_hold_next = m_hold->m_nextpkt;
1666 					m_hold->m_nextpkt = NULL;
1667 
1668 					/*
1669 					 * we assume ifp is not a p2p here, so
1670 					 * just set the 2nd argument as the
1671 					 * 1st one.
1672 					 */
1673 					nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain);
1674 				}
1675 				/*
1676 				 * If we have mbufs in the chain we need to do
1677 				 * deferred transmit. Copy the address from the
1678 				 * llentry before dropping the lock down below.
1679 				 */
1680 				if (chain != NULL)
1681 					memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6));
1682 			}
1683 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1684 			/* probe right away */
1685 			nd6_llinfo_settimer_locked((void *)ln, 0);
1686 		}
1687 	}
1688 
1689 	/*
1690 	 * ICMP6 type dependent behavior.
1691 	 *
1692 	 * NS: clear IsRouter if new entry
1693 	 * RS: clear IsRouter
1694 	 * RA: set IsRouter if there's lladdr
1695 	 * redir: clear IsRouter if new entry
1696 	 *
1697 	 * RA case, (1):
1698 	 * The spec says that we must set IsRouter in the following cases:
1699 	 * - If lladdr exist, set IsRouter.  This means (1-5).
1700 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1701 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1702 	 * A quetion arises for (1) case.  (1) case has no lladdr in the
1703 	 * neighbor cache, this is similar to (6).
1704 	 * This case is rare but we figured that we MUST NOT set IsRouter.
1705 	 *
1706 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1707 	 *							D R
1708 	 *	0	n	n	--	(1)	c   ?     s
1709 	 *	0	y	n	--	(2)	c   s     s
1710 	 *	0	n	y	--	(3)	c   s     s
1711 	 *	0	y	y	n	(4)	c   s     s
1712 	 *	0	y	y	y	(5)	c   s     s
1713 	 *	1	--	n	--	(6) c	c	c s
1714 	 *	1	--	y	--	(7) c	c   s	c s
1715 	 *
1716 	 *					(c=clear s=set)
1717 	 */
1718 	switch (type & 0xff) {
1719 	case ND_NEIGHBOR_SOLICIT:
1720 		/*
1721 		 * New entry must have is_router flag cleared.
1722 		 */
1723 		if (is_newentry)	/* (6-7) */
1724 			ln->ln_router = 0;
1725 		break;
1726 	case ND_REDIRECT:
1727 		/*
1728 		 * If the icmp is a redirect to a better router, always set the
1729 		 * is_router flag.  Otherwise, if the entry is newly created,
1730 		 * clear the flag.  [RFC 2461, sec 8.3]
1731 		 */
1732 		if (code == ND_REDIRECT_ROUTER)
1733 			ln->ln_router = 1;
1734 		else if (is_newentry) /* (6-7) */
1735 			ln->ln_router = 0;
1736 		break;
1737 	case ND_ROUTER_SOLICIT:
1738 		/*
1739 		 * is_router flag must always be cleared.
1740 		 */
1741 		ln->ln_router = 0;
1742 		break;
1743 	case ND_ROUTER_ADVERT:
1744 		/*
1745 		 * Mark an entry with lladdr as a router.
1746 		 */
1747 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
1748 		    (is_newentry && lladdr)) {			/* (7) */
1749 			ln->ln_router = 1;
1750 		}
1751 		break;
1752 	}
1753 
1754 	if (ln != NULL) {
1755 		static_route = (ln->la_flags & LLE_STATIC);
1756 		router = ln->ln_router;
1757 
1758 		if (flags & ND6_EXCLUSIVE)
1759 			LLE_WUNLOCK(ln);
1760 		else
1761 			LLE_RUNLOCK(ln);
1762 		if (static_route)
1763 			ln = NULL;
1764 	}
1765 	if (chain)
1766 		nd6_output_flush(ifp, ifp, chain, &sin6);
1767 
1768 	/*
1769 	 * When the link-layer address of a router changes, select the
1770 	 * best router again.  In particular, when the neighbor entry is newly
1771 	 * created, it might affect the selection policy.
1772 	 * Question: can we restrict the first condition to the "is_newentry"
1773 	 * case?
1774 	 * XXX: when we hear an RA from a new router with the link-layer
1775 	 * address option, defrouter_select() is called twice, since
1776 	 * defrtrlist_update called the function as well.  However, I believe
1777 	 * we can compromise the overhead, since it only happens the first
1778 	 * time.
1779 	 * XXX: although defrouter_select() should not have a bad effect
1780 	 * for those are not autoconfigured hosts, we explicitly avoid such
1781 	 * cases for safety.
1782 	 */
1783 	if (do_update && router &&
1784 	    ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1785 		/*
1786 		 * guaranteed recursion
1787 		 */
1788 		defrouter_select();
1789 	}
1790 
1791 	return (ln);
1792 done:
1793 	if (ln != NULL) {
1794 		if (flags & ND6_EXCLUSIVE)
1795 			LLE_WUNLOCK(ln);
1796 		else
1797 			LLE_RUNLOCK(ln);
1798 		if (static_route)
1799 			ln = NULL;
1800 	}
1801 	return (ln);
1802 }
1803 
1804 static void
1805 nd6_slowtimo(void *arg)
1806 {
1807 	CURVNET_SET((struct vnet *) arg);
1808 	struct nd_ifinfo *nd6if;
1809 	struct ifnet *ifp;
1810 
1811 	callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1812 	    nd6_slowtimo, curvnet);
1813 	IFNET_RLOCK_NOSLEEP();
1814 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1815 		if (ifp->if_afdata[AF_INET6] == NULL)
1816 			continue;
1817 		nd6if = ND_IFINFO(ifp);
1818 		if (nd6if->basereachable && /* already initialized */
1819 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1820 			/*
1821 			 * Since reachable time rarely changes by router
1822 			 * advertisements, we SHOULD insure that a new random
1823 			 * value gets recomputed at least once every few hours.
1824 			 * (RFC 2461, 6.3.4)
1825 			 */
1826 			nd6if->recalctm = V_nd6_recalc_reachtm_interval;
1827 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1828 		}
1829 	}
1830 	IFNET_RUNLOCK_NOSLEEP();
1831 	CURVNET_RESTORE();
1832 }
1833 
1834 /*
1835  * IPv6 packet output - light version.
1836  * Checks if destination LLE exists and is in proper state
1837  * (e.g no modification required). If not true, fall back to
1838  * "heavy" version.
1839  */
1840 int
1841 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1842     struct sockaddr_in6 *dst, struct rtentry *rt0)
1843 {
1844 	struct llentry *ln = NULL;
1845 	int error = 0;
1846 
1847 	/* discard the packet if IPv6 operation is disabled on the interface */
1848 	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
1849 		m_freem(m);
1850 		return (ENETDOWN); /* better error? */
1851 	}
1852 
1853 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1854 		goto sendpkt;
1855 
1856 	if (nd6_need_cache(ifp) == 0)
1857 		goto sendpkt;
1858 
1859 	IF_AFDATA_RLOCK(ifp);
1860 	ln = nd6_lookup(&dst->sin6_addr, 0, ifp);
1861 	IF_AFDATA_RUNLOCK(ifp);
1862 
1863 	/*
1864 	 * Perform fast path for the following cases:
1865 	 * 1) lle state is REACHABLE
1866 	 * 2) lle state is DELAY (NS message sentNS message sent)
1867 	 *
1868 	 * Every other case involves lle modification, so we handle
1869 	 * them separately.
1870 	 */
1871 	if (ln == NULL || (ln->ln_state != ND6_LLINFO_REACHABLE &&
1872 	    ln->ln_state != ND6_LLINFO_DELAY)) {
1873 		/* Fall back to slow processing path */
1874 		if (ln != NULL)
1875 			LLE_RUNLOCK(ln);
1876 		return (nd6_output_lle(ifp, origifp, m, dst, rt0, NULL, NULL));
1877 	}
1878 
1879 sendpkt:
1880 	if (ln != NULL)
1881 		LLE_RUNLOCK(ln);
1882 
1883 #ifdef MAC
1884 	mac_netinet6_nd6_send(ifp, m);
1885 #endif
1886 
1887 	/*
1888 	 * If called from nd6_ns_output() (NS), nd6_na_output() (NA),
1889 	 * icmp6_redirect_output() (REDIRECT) or from rip6_output() (RS, RA
1890 	 * as handled by rtsol and rtadvd), mbufs will be tagged for SeND
1891 	 * to be diverted to user space.  When re-injected into the kernel,
1892 	 * send_output() will directly dispatch them to the outgoing interface.
1893 	 */
1894 	if (send_sendso_input_hook != NULL) {
1895 		struct m_tag *mtag;
1896 		struct ip6_hdr *ip6;
1897 		int ip6len;
1898 		mtag = m_tag_find(m, PACKET_TAG_ND_OUTGOING, NULL);
1899 		if (mtag != NULL) {
1900 			ip6 = mtod(m, struct ip6_hdr *);
1901 			ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
1902 			/* Use the SEND socket */
1903 			error = send_sendso_input_hook(m, ifp, SND_OUT,
1904 			    ip6len);
1905 			/* -1 == no app on SEND socket */
1906 			if (error == 0 || error != -1)
1907 			    return (error);
1908 		}
1909 	}
1910 
1911 	m_clrprotoflags(m);	/* Avoid confusing lower layers. */
1912 	IP_PROBE(send, NULL, NULL, mtod(m, struct ip6_hdr *), ifp, NULL,
1913 	    mtod(m, struct ip6_hdr *));
1914 
1915 	if ((ifp->if_flags & IFF_LOOPBACK) == 0)
1916 		origifp = ifp;
1917 
1918 	error = (*ifp->if_output)(origifp, m, (struct sockaddr *)dst, NULL);
1919 	return (error);
1920 }
1921 
1922 
1923 /*
1924  * Output IPv6 packet - heavy version.
1925  * Function assume that either
1926  * 1) destination LLE does not exist, is invalid or stale, so
1927  *   ND6_EXCLUSIVE lock needs to be acquired
1928  * 2) destination lle is provided (with ND6_EXCLUSIVE lock),
1929  *   in that case packets are queued in &chain.
1930  *
1931  */
1932 int
1933 nd6_output_lle(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1934     struct sockaddr_in6 *dst, struct rtentry *rt0, struct llentry *lle,
1935 	struct mbuf **chain)
1936 {
1937 	struct m_tag *mtag;
1938 	struct ip6_hdr *ip6;
1939 	int error = 0;
1940 	int flags = 0;
1941 	int has_lle = 0;
1942 	int ip6len;
1943 
1944 #ifdef INVARIANTS
1945 	if (lle != NULL) {
1946 
1947 		LLE_WLOCK_ASSERT(lle);
1948 
1949 		KASSERT(chain != NULL, (" lle locked but no mbuf chain pointer passed"));
1950 	}
1951 #endif
1952 	KASSERT(m != NULL, ("NULL mbuf, nothing to send"));
1953 	/* discard the packet if IPv6 operation is disabled on the interface */
1954 	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
1955 		m_freem(m);
1956 		return (ENETDOWN); /* better error? */
1957 	}
1958 
1959 	if (lle != NULL)
1960 		has_lle = 1;
1961 
1962 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1963 		goto sendpkt;
1964 
1965 	if (nd6_need_cache(ifp) == 0)
1966 		goto sendpkt;
1967 
1968 	/*
1969 	 * Address resolution or Neighbor Unreachability Detection
1970 	 * for the next hop.
1971 	 * At this point, the destination of the packet must be a unicast
1972 	 * or an anycast address(i.e. not a multicast).
1973 	 */
1974 	if (lle == NULL) {
1975 		IF_AFDATA_RLOCK(ifp);
1976 		lle = nd6_lookup(&dst->sin6_addr, ND6_EXCLUSIVE, ifp);
1977 		IF_AFDATA_RUNLOCK(ifp);
1978 		if ((lle == NULL) && nd6_is_addr_neighbor(dst, ifp))  {
1979 			/*
1980 			 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1981 			 * the condition below is not very efficient.  But we believe
1982 			 * it is tolerable, because this should be a rare case.
1983 			 */
1984 			flags = ND6_CREATE | ND6_EXCLUSIVE;
1985 			IF_AFDATA_LOCK(ifp);
1986 			lle = nd6_lookup(&dst->sin6_addr, flags, ifp);
1987 			IF_AFDATA_UNLOCK(ifp);
1988 		}
1989 	}
1990 	if (lle == NULL) {
1991 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1992 		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1993 			char ip6buf[INET6_ADDRSTRLEN];
1994 			log(LOG_DEBUG,
1995 			    "nd6_output: can't allocate llinfo for %s "
1996 			    "(ln=%p)\n",
1997 			    ip6_sprintf(ip6buf, &dst->sin6_addr), lle);
1998 			m_freem(m);
1999 			return (ENOBUFS);
2000 		}
2001 		goto sendpkt;	/* send anyway */
2002 	}
2003 
2004 	LLE_WLOCK_ASSERT(lle);
2005 
2006 	/* We don't have to do link-layer address resolution on a p2p link. */
2007 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
2008 	    lle->ln_state < ND6_LLINFO_REACHABLE) {
2009 		lle->ln_state = ND6_LLINFO_STALE;
2010 		nd6_llinfo_settimer_locked(lle, (long)V_nd6_gctimer * hz);
2011 	}
2012 
2013 	/*
2014 	 * The first time we send a packet to a neighbor whose entry is
2015 	 * STALE, we have to change the state to DELAY and a sets a timer to
2016 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2017 	 * neighbor unreachability detection on expiration.
2018 	 * (RFC 2461 7.3.3)
2019 	 */
2020 	if (lle->ln_state == ND6_LLINFO_STALE) {
2021 		lle->la_asked = 0;
2022 		lle->ln_state = ND6_LLINFO_DELAY;
2023 		nd6_llinfo_settimer_locked(lle, (long)V_nd6_delay * hz);
2024 	}
2025 
2026 	/*
2027 	 * If the neighbor cache entry has a state other than INCOMPLETE
2028 	 * (i.e. its link-layer address is already resolved), just
2029 	 * send the packet.
2030 	 */
2031 	if (lle->ln_state > ND6_LLINFO_INCOMPLETE)
2032 		goto sendpkt;
2033 
2034 	/*
2035 	 * There is a neighbor cache entry, but no ethernet address
2036 	 * response yet.  Append this latest packet to the end of the
2037 	 * packet queue in the mbuf, unless the number of the packet
2038 	 * does not exceed nd6_maxqueuelen.  When it exceeds nd6_maxqueuelen,
2039 	 * the oldest packet in the queue will be removed.
2040 	 */
2041 	if (lle->ln_state == ND6_LLINFO_NOSTATE)
2042 		lle->ln_state = ND6_LLINFO_INCOMPLETE;
2043 
2044 	if (lle->la_hold != NULL) {
2045 		struct mbuf *m_hold;
2046 		int i;
2047 
2048 		i = 0;
2049 		for (m_hold = lle->la_hold; m_hold; m_hold = m_hold->m_nextpkt){
2050 			i++;
2051 			if (m_hold->m_nextpkt == NULL) {
2052 				m_hold->m_nextpkt = m;
2053 				break;
2054 			}
2055 		}
2056 		while (i >= V_nd6_maxqueuelen) {
2057 			m_hold = lle->la_hold;
2058 			lle->la_hold = lle->la_hold->m_nextpkt;
2059 			m_freem(m_hold);
2060 			i--;
2061 		}
2062 	} else {
2063 		lle->la_hold = m;
2064 	}
2065 
2066 	/*
2067 	 * If there has been no NS for the neighbor after entering the
2068 	 * INCOMPLETE state, send the first solicitation.
2069 	 */
2070 	if (!ND6_LLINFO_PERMANENT(lle) && lle->la_asked == 0) {
2071 		lle->la_asked++;
2072 
2073 		nd6_llinfo_settimer_locked(lle,
2074 		    (long)ND_IFINFO(ifp)->retrans * hz / 1000);
2075 		LLE_WUNLOCK(lle);
2076 		nd6_ns_output(ifp, NULL, &dst->sin6_addr, lle, 0);
2077 		if (has_lle != 0)
2078 			LLE_WLOCK(lle);
2079 	} else if (has_lle == 0) {
2080 		/*
2081 		 * We did the lookup (no lle arg) so we
2082 		 * need to do the unlock here.
2083 		 */
2084 		LLE_WUNLOCK(lle);
2085 	}
2086 
2087 	return (0);
2088 
2089   sendpkt:
2090 	/*
2091 	 * ln is valid and the caller did not pass in
2092 	 * an llentry
2093 	 */
2094 	if (lle != NULL && has_lle == 0)
2095 		LLE_WUNLOCK(lle);
2096 
2097 #ifdef MAC
2098 	mac_netinet6_nd6_send(ifp, m);
2099 #endif
2100 
2101 	/*
2102 	 * If called from nd6_ns_output() (NS), nd6_na_output() (NA),
2103 	 * icmp6_redirect_output() (REDIRECT) or from rip6_output() (RS, RA
2104 	 * as handled by rtsol and rtadvd), mbufs will be tagged for SeND
2105 	 * to be diverted to user space.  When re-injected into the kernel,
2106 	 * send_output() will directly dispatch them to the outgoing interface.
2107 	 */
2108 	if (send_sendso_input_hook != NULL) {
2109 		mtag = m_tag_find(m, PACKET_TAG_ND_OUTGOING, NULL);
2110 		if (mtag != NULL) {
2111 			ip6 = mtod(m, struct ip6_hdr *);
2112 			ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
2113 			/* Use the SEND socket */
2114 			error = send_sendso_input_hook(m, ifp, SND_OUT,
2115 			    ip6len);
2116 			/* -1 == no app on SEND socket */
2117 			if (error == 0 || error != -1)
2118 			    return (error);
2119 		}
2120 	}
2121 
2122 	/*
2123 	 * We were passed in a pointer to an lle with the lock held
2124 	 * this means that we can't call if_output as we will
2125 	 * recurse on the lle lock - so what we do is we create
2126 	 * a list of mbufs to send and transmit them in the caller
2127 	 * after the lock is dropped
2128 	 */
2129 	if (has_lle != 0) {
2130 		if (*chain == NULL)
2131 			*chain = m;
2132 		else {
2133 			struct mbuf *mb;
2134 
2135 			/*
2136 			 * append mbuf to end of deferred chain
2137 			 */
2138 			mb = *chain;
2139 			while (mb->m_nextpkt != NULL)
2140 				mb = mb->m_nextpkt;
2141 			mb->m_nextpkt = m;
2142 		}
2143 		return (error);
2144 	}
2145 	m_clrprotoflags(m);	/* Avoid confusing lower layers. */
2146 	IP_PROBE(send, NULL, NULL, mtod(m, struct ip6_hdr *), ifp, NULL,
2147 	    mtod(m, struct ip6_hdr *));
2148 
2149 	if ((ifp->if_flags & IFF_LOOPBACK) == 0)
2150 		origifp = ifp;
2151 
2152 	error = (*ifp->if_output)(origifp, m, (struct sockaddr *)dst, NULL);
2153 	return (error);
2154 }
2155 
2156 
2157 int
2158 nd6_output_flush(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *chain,
2159     struct sockaddr_in6 *dst)
2160 {
2161 	struct mbuf *m, *m_head;
2162 	struct ifnet *outifp;
2163 	int error = 0;
2164 
2165 	m_head = chain;
2166 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2167 		outifp = origifp;
2168 	else
2169 		outifp = ifp;
2170 
2171 	while (m_head) {
2172 		m = m_head;
2173 		m_head = m_head->m_nextpkt;
2174 		error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, NULL);
2175 	}
2176 
2177 	/*
2178 	 * XXX
2179 	 * note that intermediate errors are blindly ignored - but this is
2180 	 * the same convention as used with nd6_output when called by
2181 	 * nd6_cache_lladdr
2182 	 */
2183 	return (error);
2184 }
2185 
2186 
2187 int
2188 nd6_need_cache(struct ifnet *ifp)
2189 {
2190 	/*
2191 	 * XXX: we currently do not make neighbor cache on any interface
2192 	 * other than ARCnet, Ethernet, FDDI and GIF.
2193 	 *
2194 	 * RFC2893 says:
2195 	 * - unidirectional tunnels needs no ND
2196 	 */
2197 	switch (ifp->if_type) {
2198 	case IFT_ARCNET:
2199 	case IFT_ETHER:
2200 	case IFT_FDDI:
2201 	case IFT_IEEE1394:
2202 #ifdef IFT_L2VLAN
2203 	case IFT_L2VLAN:
2204 #endif
2205 #ifdef IFT_IEEE80211
2206 	case IFT_IEEE80211:
2207 #endif
2208 	case IFT_INFINIBAND:
2209 	case IFT_BRIDGE:
2210 	case IFT_PROPVIRTUAL:
2211 		return (1);
2212 	default:
2213 		return (0);
2214 	}
2215 }
2216 
2217 /*
2218  * Add pernament ND6 link-layer record for given
2219  * interface address.
2220  *
2221  * Very similar to IPv4 arp_ifinit(), but:
2222  * 1) IPv6 DAD is performed in different place
2223  * 2) It is called by IPv6 protocol stack in contrast to
2224  * arp_ifinit() which is typically called in SIOCSIFADDR
2225  * driver ioctl handler.
2226  *
2227  */
2228 int
2229 nd6_add_ifa_lle(struct in6_ifaddr *ia)
2230 {
2231 	struct ifnet *ifp;
2232 	struct llentry *ln;
2233 
2234 	ifp = ia->ia_ifa.ifa_ifp;
2235 	if (nd6_need_cache(ifp) == 0)
2236 		return (0);
2237 	IF_AFDATA_LOCK(ifp);
2238 	ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
2239 	ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR |
2240 	    LLE_EXCLUSIVE), (struct sockaddr *)&ia->ia_addr);
2241 	IF_AFDATA_UNLOCK(ifp);
2242 	if (ln != NULL) {
2243 		ln->la_expire = 0;  /* for IPv6 this means permanent */
2244 		ln->ln_state = ND6_LLINFO_REACHABLE;
2245 		LLE_WUNLOCK(ln);
2246 		in6_newaddrmsg(ia, RTM_ADD);
2247 		return (0);
2248 	}
2249 
2250 	return (ENOBUFS);
2251 }
2252 
2253 /*
2254  * Removes ALL lle records for interface address prefix.
2255  * XXXME: That's probably not we really want to do, we need
2256  * to remove address record only and keep other records
2257  * until we determine if given prefix is really going
2258  * to be removed.
2259  */
2260 void
2261 nd6_rem_ifa_lle(struct in6_ifaddr *ia)
2262 {
2263 	struct sockaddr_in6 mask, addr;
2264 	struct ifnet *ifp;
2265 
2266 	in6_newaddrmsg(ia, RTM_DELETE);
2267 
2268 	ifp = ia->ia_ifa.ifa_ifp;
2269 	memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
2270 	memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
2271 	lltable_prefix_free(AF_INET6, (struct sockaddr *)&addr,
2272 	            (struct sockaddr *)&mask, LLE_STATIC);
2273 }
2274 
2275 /*
2276  * the callers of this function need to be re-worked to drop
2277  * the lle lock, drop here for now
2278  */
2279 int
2280 nd6_storelladdr(struct ifnet *ifp, struct mbuf *m,
2281     const struct sockaddr *dst, u_char *desten, struct llentry **lle)
2282 {
2283 	struct llentry *ln;
2284 
2285 	*lle = NULL;
2286 	IF_AFDATA_UNLOCK_ASSERT(ifp);
2287 	if (m != NULL && m->m_flags & M_MCAST) {
2288 		int i;
2289 
2290 		switch (ifp->if_type) {
2291 		case IFT_ETHER:
2292 		case IFT_FDDI:
2293 #ifdef IFT_L2VLAN
2294 		case IFT_L2VLAN:
2295 #endif
2296 #ifdef IFT_IEEE80211
2297 		case IFT_IEEE80211:
2298 #endif
2299 		case IFT_BRIDGE:
2300 		case IFT_ISO88025:
2301 			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2302 						 desten);
2303 			return (0);
2304 		case IFT_IEEE1394:
2305 			/*
2306 			 * netbsd can use if_broadcastaddr, but we don't do so
2307 			 * to reduce # of ifdef.
2308 			 */
2309 			for (i = 0; i < ifp->if_addrlen; i++)
2310 				desten[i] = ~0;
2311 			return (0);
2312 		case IFT_ARCNET:
2313 			*desten = 0;
2314 			return (0);
2315 		default:
2316 			m_freem(m);
2317 			return (EAFNOSUPPORT);
2318 		}
2319 	}
2320 
2321 
2322 	/*
2323 	 * the entry should have been created in nd6_store_lladdr
2324 	 */
2325 	IF_AFDATA_RLOCK(ifp);
2326 	ln = lla_lookup(LLTABLE6(ifp), 0, dst);
2327 	IF_AFDATA_RUNLOCK(ifp);
2328 	if ((ln == NULL) || !(ln->la_flags & LLE_VALID)) {
2329 		if (ln != NULL)
2330 			LLE_RUNLOCK(ln);
2331 		/* this could happen, if we could not allocate memory */
2332 		m_freem(m);
2333 		return (1);
2334 	}
2335 
2336 	bcopy(&ln->ll_addr, desten, ifp->if_addrlen);
2337 	*lle = ln;
2338 	LLE_RUNLOCK(ln);
2339 	/*
2340 	 * A *small* use after free race exists here
2341 	 */
2342 	return (0);
2343 }
2344 
2345 static void
2346 clear_llinfo_pqueue(struct llentry *ln)
2347 {
2348 	struct mbuf *m_hold, *m_hold_next;
2349 
2350 	for (m_hold = ln->la_hold; m_hold; m_hold = m_hold_next) {
2351 		m_hold_next = m_hold->m_nextpkt;
2352 		m_freem(m_hold);
2353 	}
2354 
2355 	ln->la_hold = NULL;
2356 	return;
2357 }
2358 
2359 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2360 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2361 #ifdef SYSCTL_DECL
2362 SYSCTL_DECL(_net_inet6_icmp6);
2363 #endif
2364 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2365 	CTLFLAG_RD, nd6_sysctl_drlist, "");
2366 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2367 	CTLFLAG_RD, nd6_sysctl_prlist, "");
2368 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, nd6_maxqueuelen,
2369 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_maxqueuelen), 1, "");
2370 SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_gctimer,
2371 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_gctimer), (60 * 60 * 24), "");
2372 
2373 static int
2374 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2375 {
2376 	struct in6_defrouter d;
2377 	struct nd_defrouter *dr;
2378 	int error;
2379 
2380 	if (req->newptr)
2381 		return (EPERM);
2382 
2383 	bzero(&d, sizeof(d));
2384 	d.rtaddr.sin6_family = AF_INET6;
2385 	d.rtaddr.sin6_len = sizeof(d.rtaddr);
2386 
2387 	/*
2388 	 * XXX locking
2389 	 */
2390 	TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
2391 		d.rtaddr.sin6_addr = dr->rtaddr;
2392 		error = sa6_recoverscope(&d.rtaddr);
2393 		if (error != 0)
2394 			return (error);
2395 		d.flags = dr->flags;
2396 		d.rtlifetime = dr->rtlifetime;
2397 		d.expire = dr->expire + (time_second - time_uptime);
2398 		d.if_index = dr->ifp->if_index;
2399 		error = SYSCTL_OUT(req, &d, sizeof(d));
2400 		if (error != 0)
2401 			return (error);
2402 	}
2403 	return (0);
2404 }
2405 
2406 static int
2407 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2408 {
2409 	struct in6_prefix p;
2410 	struct sockaddr_in6 s6;
2411 	struct nd_prefix *pr;
2412 	struct nd_pfxrouter *pfr;
2413 	time_t maxexpire;
2414 	int error;
2415 	char ip6buf[INET6_ADDRSTRLEN];
2416 
2417 	if (req->newptr)
2418 		return (EPERM);
2419 
2420 	bzero(&p, sizeof(p));
2421 	p.origin = PR_ORIG_RA;
2422 	bzero(&s6, sizeof(s6));
2423 	s6.sin6_family = AF_INET6;
2424 	s6.sin6_len = sizeof(s6);
2425 
2426 	/*
2427 	 * XXX locking
2428 	 */
2429 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
2430 		p.prefix = pr->ndpr_prefix;
2431 		if (sa6_recoverscope(&p.prefix)) {
2432 			log(LOG_ERR, "scope error in prefix list (%s)\n",
2433 			    ip6_sprintf(ip6buf, &p.prefix.sin6_addr));
2434 			/* XXX: press on... */
2435 		}
2436 		p.raflags = pr->ndpr_raf;
2437 		p.prefixlen = pr->ndpr_plen;
2438 		p.vltime = pr->ndpr_vltime;
2439 		p.pltime = pr->ndpr_pltime;
2440 		p.if_index = pr->ndpr_ifp->if_index;
2441 		if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2442 			p.expire = 0;
2443 		else {
2444 			/* XXX: we assume time_t is signed. */
2445 			maxexpire = (-1) &
2446 			    ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
2447 			if (pr->ndpr_vltime < maxexpire - pr->ndpr_lastupdate)
2448 				p.expire = pr->ndpr_lastupdate +
2449 				    pr->ndpr_vltime +
2450 				    (time_second - time_uptime);
2451 			else
2452 				p.expire = maxexpire;
2453 		}
2454 		p.refcnt = pr->ndpr_refcnt;
2455 		p.flags = pr->ndpr_stateflags;
2456 		p.advrtrs = 0;
2457 		LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
2458 			p.advrtrs++;
2459 		error = SYSCTL_OUT(req, &p, sizeof(p));
2460 		if (error != 0)
2461 			return (error);
2462 		LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2463 			s6.sin6_addr = pfr->router->rtaddr;
2464 			if (sa6_recoverscope(&s6))
2465 				log(LOG_ERR,
2466 				    "scope error in prefix list (%s)\n",
2467 				    ip6_sprintf(ip6buf, &pfr->router->rtaddr));
2468 			error = SYSCTL_OUT(req, &s6, sizeof(s6));
2469 			if (error != 0)
2470 				return (error);
2471 		}
2472 	}
2473 	return (0);
2474 }
2475