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