xref: /freebsd-src/sys/netinet6/nd6.c (revision 5ec9cb893bd22bf2d47ab2fef29aae6ee5e1d131)
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 = (flags & ND6_EXCLUSIVE) ? LLE_EXCLUSIVE : 0;
941 	ln = lla_lookup(LLTABLE6(ifp), llflags, (struct sockaddr *)&sin6);
942 
943 	return (ln);
944 }
945 
946 /*
947  * the caller acquires and releases the lock on the lltbls
948  * Returns the llentry wlocked
949  */
950 struct llentry *
951 nd6_create(struct in6_addr *addr6, int flags, struct ifnet *ifp)
952 {
953 	struct sockaddr_in6 sin6;
954 	struct llentry *ln;
955 
956 	bzero(&sin6, sizeof(sin6));
957 	sin6.sin6_len = sizeof(struct sockaddr_in6);
958 	sin6.sin6_family = AF_INET6;
959 	sin6.sin6_addr = *addr6;
960 
961 	IF_AFDATA_WLOCK_ASSERT(ifp);
962 
963 	ln = lla_create(LLTABLE6(ifp), 0, (struct sockaddr *)&sin6);
964 	if (ln != NULL)
965 		ln->ln_state = ND6_LLINFO_NOSTATE;
966 
967 	return (ln);
968 }
969 
970 /*
971  * Test whether a given IPv6 address is a neighbor or not, ignoring
972  * the actual neighbor cache.  The neighbor cache is ignored in order
973  * to not reenter the routing code from within itself.
974  */
975 static int
976 nd6_is_new_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
977 {
978 	struct nd_prefix *pr;
979 	struct ifaddr *dstaddr;
980 
981 	/*
982 	 * A link-local address is always a neighbor.
983 	 * XXX: a link does not necessarily specify a single interface.
984 	 */
985 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
986 		struct sockaddr_in6 sin6_copy;
987 		u_int32_t zone;
988 
989 		/*
990 		 * We need sin6_copy since sa6_recoverscope() may modify the
991 		 * content (XXX).
992 		 */
993 		sin6_copy = *addr;
994 		if (sa6_recoverscope(&sin6_copy))
995 			return (0); /* XXX: should be impossible */
996 		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
997 			return (0);
998 		if (sin6_copy.sin6_scope_id == zone)
999 			return (1);
1000 		else
1001 			return (0);
1002 	}
1003 
1004 	/*
1005 	 * If the address matches one of our addresses,
1006 	 * it should be a neighbor.
1007 	 * If the address matches one of our on-link prefixes, it should be a
1008 	 * neighbor.
1009 	 */
1010 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1011 		if (pr->ndpr_ifp != ifp)
1012 			continue;
1013 
1014 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) {
1015 			struct rtentry *rt;
1016 
1017 			/* Always use the default FIB here. */
1018 			rt = in6_rtalloc1((struct sockaddr *)&pr->ndpr_prefix,
1019 			    0, 0, RT_DEFAULT_FIB);
1020 			if (rt == NULL)
1021 				continue;
1022 			/*
1023 			 * This is the case where multiple interfaces
1024 			 * have the same prefix, but only one is installed
1025 			 * into the routing table and that prefix entry
1026 			 * is not the one being examined here. In the case
1027 			 * where RADIX_MPATH is enabled, multiple route
1028 			 * entries (of the same rt_key value) will be
1029 			 * installed because the interface addresses all
1030 			 * differ.
1031 			 */
1032 			if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1033 			       &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr)) {
1034 				RTFREE_LOCKED(rt);
1035 				continue;
1036 			}
1037 			RTFREE_LOCKED(rt);
1038 		}
1039 
1040 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1041 		    &addr->sin6_addr, &pr->ndpr_mask))
1042 			return (1);
1043 	}
1044 
1045 	/*
1046 	 * If the address is assigned on the node of the other side of
1047 	 * a p2p interface, the address should be a neighbor.
1048 	 */
1049 	dstaddr = ifa_ifwithdstaddr((struct sockaddr *)addr, RT_ALL_FIBS);
1050 	if (dstaddr != NULL) {
1051 		if (dstaddr->ifa_ifp == ifp) {
1052 			ifa_free(dstaddr);
1053 			return (1);
1054 		}
1055 		ifa_free(dstaddr);
1056 	}
1057 
1058 	/*
1059 	 * If the default router list is empty, all addresses are regarded
1060 	 * as on-link, and thus, as a neighbor.
1061 	 */
1062 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV &&
1063 	    TAILQ_EMPTY(&V_nd_defrouter) &&
1064 	    V_nd6_defifindex == ifp->if_index) {
1065 		return (1);
1066 	}
1067 
1068 	return (0);
1069 }
1070 
1071 
1072 /*
1073  * Detect if a given IPv6 address identifies a neighbor on a given link.
1074  * XXX: should take care of the destination of a p2p link?
1075  */
1076 int
1077 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
1078 {
1079 	struct llentry *lle;
1080 	int rc = 0;
1081 
1082 	IF_AFDATA_UNLOCK_ASSERT(ifp);
1083 	if (nd6_is_new_addr_neighbor(addr, ifp))
1084 		return (1);
1085 
1086 	/*
1087 	 * Even if the address matches none of our addresses, it might be
1088 	 * in the neighbor cache.
1089 	 */
1090 	IF_AFDATA_RLOCK(ifp);
1091 	if ((lle = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL) {
1092 		LLE_RUNLOCK(lle);
1093 		rc = 1;
1094 	}
1095 	IF_AFDATA_RUNLOCK(ifp);
1096 	return (rc);
1097 }
1098 
1099 /*
1100  * Free an nd6 llinfo entry.
1101  * Since the function would cause significant changes in the kernel, DO NOT
1102  * make it global, unless you have a strong reason for the change, and are sure
1103  * that the change is safe.
1104  */
1105 static struct llentry *
1106 nd6_free(struct llentry *ln, int gc)
1107 {
1108         struct llentry *next;
1109 	struct nd_defrouter *dr;
1110 	struct ifnet *ifp;
1111 
1112 	LLE_WLOCK_ASSERT(ln);
1113 
1114 	/*
1115 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1116 	 * even though it is not harmful, it was not really necessary.
1117 	 */
1118 
1119 	/* cancel timer */
1120 	nd6_llinfo_settimer_locked(ln, -1);
1121 
1122 	ifp = ln->lle_tbl->llt_ifp;
1123 
1124 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1125 		dr = defrouter_lookup(&L3_ADDR_SIN6(ln)->sin6_addr, ifp);
1126 
1127 		if (dr != NULL && dr->expire &&
1128 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
1129 			/*
1130 			 * If the reason for the deletion is just garbage
1131 			 * collection, and the neighbor is an active default
1132 			 * router, do not delete it.  Instead, reset the GC
1133 			 * timer using the router's lifetime.
1134 			 * Simply deleting the entry would affect default
1135 			 * router selection, which is not necessarily a good
1136 			 * thing, especially when we're using router preference
1137 			 * values.
1138 			 * XXX: the check for ln_state would be redundant,
1139 			 *      but we intentionally keep it just in case.
1140 			 */
1141 			if (dr->expire > time_uptime)
1142 				nd6_llinfo_settimer_locked(ln,
1143 				    (dr->expire - time_uptime) * hz);
1144 			else
1145 				nd6_llinfo_settimer_locked(ln,
1146 				    (long)V_nd6_gctimer * hz);
1147 
1148 			next = LIST_NEXT(ln, lle_next);
1149 			LLE_REMREF(ln);
1150 			LLE_WUNLOCK(ln);
1151 			return (next);
1152 		}
1153 
1154 		if (dr) {
1155 			/*
1156 			 * Unreachablity of a router might affect the default
1157 			 * router selection and on-link detection of advertised
1158 			 * prefixes.
1159 			 */
1160 
1161 			/*
1162 			 * Temporarily fake the state to choose a new default
1163 			 * router and to perform on-link determination of
1164 			 * prefixes correctly.
1165 			 * Below the state will be set correctly,
1166 			 * or the entry itself will be deleted.
1167 			 */
1168 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
1169 		}
1170 
1171 		if (ln->ln_router || dr) {
1172 
1173 			/*
1174 			 * We need to unlock to avoid a LOR with rt6_flush() with the
1175 			 * rnh and for the calls to pfxlist_onlink_check() and
1176 			 * defrouter_select() in the block further down for calls
1177 			 * into nd6_lookup().  We still hold a ref.
1178 			 */
1179 			LLE_WUNLOCK(ln);
1180 
1181 			/*
1182 			 * rt6_flush must be called whether or not the neighbor
1183 			 * is in the Default Router List.
1184 			 * See a corresponding comment in nd6_na_input().
1185 			 */
1186 			rt6_flush(&L3_ADDR_SIN6(ln)->sin6_addr, ifp);
1187 		}
1188 
1189 		if (dr) {
1190 			/*
1191 			 * Since defrouter_select() does not affect the
1192 			 * on-link determination and MIP6 needs the check
1193 			 * before the default router selection, we perform
1194 			 * the check now.
1195 			 */
1196 			pfxlist_onlink_check();
1197 
1198 			/*
1199 			 * Refresh default router list.
1200 			 */
1201 			defrouter_select();
1202 		}
1203 
1204 		if (ln->ln_router || dr)
1205 			LLE_WLOCK(ln);
1206 	}
1207 
1208 	/*
1209 	 * Before deleting the entry, remember the next entry as the
1210 	 * return value.  We need this because pfxlist_onlink_check() above
1211 	 * might have freed other entries (particularly the old next entry) as
1212 	 * a side effect (XXX).
1213 	 */
1214 	next = LIST_NEXT(ln, lle_next);
1215 
1216 	/*
1217 	 * Save to unlock. We still hold an extra reference and will not
1218 	 * free(9) in llentry_free() if someone else holds one as well.
1219 	 */
1220 	LLE_WUNLOCK(ln);
1221 	IF_AFDATA_LOCK(ifp);
1222 	LLE_WLOCK(ln);
1223 
1224 	/* Guard against race with other llentry_free(). */
1225 	if (ln->la_flags & LLE_LINKED) {
1226 		LLE_REMREF(ln);
1227 		llentry_free(ln);
1228 	} else
1229 		LLE_FREE_LOCKED(ln);
1230 
1231 	IF_AFDATA_UNLOCK(ifp);
1232 
1233 	return (next);
1234 }
1235 
1236 /*
1237  * Upper-layer reachability hint for Neighbor Unreachability Detection.
1238  *
1239  * XXX cost-effective methods?
1240  */
1241 void
1242 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1243 {
1244 	struct llentry *ln;
1245 	struct ifnet *ifp;
1246 
1247 	if ((dst6 == NULL) || (rt == NULL))
1248 		return;
1249 
1250 	ifp = rt->rt_ifp;
1251 	IF_AFDATA_RLOCK(ifp);
1252 	ln = nd6_lookup(dst6, ND6_EXCLUSIVE, NULL);
1253 	IF_AFDATA_RUNLOCK(ifp);
1254 	if (ln == NULL)
1255 		return;
1256 
1257 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
1258 		goto done;
1259 
1260 	/*
1261 	 * if we get upper-layer reachability confirmation many times,
1262 	 * it is possible we have false information.
1263 	 */
1264 	if (!force) {
1265 		ln->ln_byhint++;
1266 		if (ln->ln_byhint > V_nd6_maxnudhint) {
1267 			goto done;
1268 		}
1269 	}
1270 
1271  	ln->ln_state = ND6_LLINFO_REACHABLE;
1272 	if (!ND6_LLINFO_PERMANENT(ln)) {
1273 		nd6_llinfo_settimer_locked(ln,
1274 		    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
1275 	}
1276 done:
1277 	LLE_WUNLOCK(ln);
1278 }
1279 
1280 
1281 /*
1282  * Rejuvenate this function for routing operations related
1283  * processing.
1284  */
1285 void
1286 nd6_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
1287 {
1288 	struct sockaddr_in6 *gateway;
1289 	struct nd_defrouter *dr;
1290 	struct ifnet *ifp;
1291 
1292 	gateway = (struct sockaddr_in6 *)rt->rt_gateway;
1293 	ifp = rt->rt_ifp;
1294 
1295 	switch (req) {
1296 	case RTM_ADD:
1297 		break;
1298 
1299 	case RTM_DELETE:
1300 		if (!ifp)
1301 			return;
1302 		/*
1303 		 * Only indirect routes are interesting.
1304 		 */
1305 		if ((rt->rt_flags & RTF_GATEWAY) == 0)
1306 			return;
1307 		/*
1308 		 * check for default route
1309 		 */
1310 		if (IN6_ARE_ADDR_EQUAL(&in6addr_any,
1311 				       &SIN6(rt_key(rt))->sin6_addr)) {
1312 
1313 			dr = defrouter_lookup(&gateway->sin6_addr, ifp);
1314 			if (dr != NULL)
1315 				dr->installed = 0;
1316 		}
1317 		break;
1318 	}
1319 }
1320 
1321 
1322 int
1323 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1324 {
1325 	struct in6_drlist *drl = (struct in6_drlist *)data;
1326 	struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1327 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1328 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1329 	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1330 	struct nd_defrouter *dr;
1331 	struct nd_prefix *pr;
1332 	int i = 0, error = 0;
1333 
1334 	if (ifp->if_afdata[AF_INET6] == NULL)
1335 		return (EPFNOSUPPORT);
1336 	switch (cmd) {
1337 	case SIOCGDRLST_IN6:
1338 		/*
1339 		 * obsolete API, use sysctl under net.inet6.icmp6
1340 		 */
1341 		bzero(drl, sizeof(*drl));
1342 		TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
1343 			if (i >= DRLSTSIZ)
1344 				break;
1345 			drl->defrouter[i].rtaddr = dr->rtaddr;
1346 			in6_clearscope(&drl->defrouter[i].rtaddr);
1347 
1348 			drl->defrouter[i].flags = dr->flags;
1349 			drl->defrouter[i].rtlifetime = dr->rtlifetime;
1350 			drl->defrouter[i].expire = dr->expire +
1351 			    (time_second - time_uptime);
1352 			drl->defrouter[i].if_index = dr->ifp->if_index;
1353 			i++;
1354 		}
1355 		break;
1356 	case SIOCGPRLST_IN6:
1357 		/*
1358 		 * obsolete API, use sysctl under net.inet6.icmp6
1359 		 *
1360 		 * XXX the structure in6_prlist was changed in backward-
1361 		 * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
1362 		 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1363 		 */
1364 		/*
1365 		 * XXX meaning of fields, especialy "raflags", is very
1366 		 * differnet between RA prefix list and RR/static prefix list.
1367 		 * how about separating ioctls into two?
1368 		 */
1369 		bzero(oprl, sizeof(*oprl));
1370 		LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1371 			struct nd_pfxrouter *pfr;
1372 			int j;
1373 
1374 			if (i >= PRLSTSIZ)
1375 				break;
1376 			oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1377 			oprl->prefix[i].raflags = pr->ndpr_raf;
1378 			oprl->prefix[i].prefixlen = pr->ndpr_plen;
1379 			oprl->prefix[i].vltime = pr->ndpr_vltime;
1380 			oprl->prefix[i].pltime = pr->ndpr_pltime;
1381 			oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1382 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1383 				oprl->prefix[i].expire = 0;
1384 			else {
1385 				time_t maxexpire;
1386 
1387 				/* XXX: we assume time_t is signed. */
1388 				maxexpire = (-1) &
1389 				    ~((time_t)1 <<
1390 				    ((sizeof(maxexpire) * 8) - 1));
1391 				if (pr->ndpr_vltime <
1392 				    maxexpire - pr->ndpr_lastupdate) {
1393 					oprl->prefix[i].expire =
1394 					    pr->ndpr_lastupdate +
1395 					    pr->ndpr_vltime +
1396 					    (time_second - time_uptime);
1397 				} else
1398 					oprl->prefix[i].expire = maxexpire;
1399 			}
1400 
1401 			j = 0;
1402 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
1403 				if (j < DRLSTSIZ) {
1404 #define RTRADDR oprl->prefix[i].advrtr[j]
1405 					RTRADDR = pfr->router->rtaddr;
1406 					in6_clearscope(&RTRADDR);
1407 #undef RTRADDR
1408 				}
1409 				j++;
1410 			}
1411 			oprl->prefix[i].advrtrs = j;
1412 			oprl->prefix[i].origin = PR_ORIG_RA;
1413 
1414 			i++;
1415 		}
1416 
1417 		break;
1418 	case OSIOCGIFINFO_IN6:
1419 #define ND	ndi->ndi
1420 		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
1421 		bzero(&ND, sizeof(ND));
1422 		ND.linkmtu = IN6_LINKMTU(ifp);
1423 		ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1424 		ND.basereachable = ND_IFINFO(ifp)->basereachable;
1425 		ND.reachable = ND_IFINFO(ifp)->reachable;
1426 		ND.retrans = ND_IFINFO(ifp)->retrans;
1427 		ND.flags = ND_IFINFO(ifp)->flags;
1428 		ND.recalctm = ND_IFINFO(ifp)->recalctm;
1429 		ND.chlim = ND_IFINFO(ifp)->chlim;
1430 		break;
1431 	case SIOCGIFINFO_IN6:
1432 		ND = *ND_IFINFO(ifp);
1433 		break;
1434 	case SIOCSIFINFO_IN6:
1435 		/*
1436 		 * used to change host variables from userland.
1437 		 * intented for a use on router to reflect RA configurations.
1438 		 */
1439 		/* 0 means 'unspecified' */
1440 		if (ND.linkmtu != 0) {
1441 			if (ND.linkmtu < IPV6_MMTU ||
1442 			    ND.linkmtu > IN6_LINKMTU(ifp)) {
1443 				error = EINVAL;
1444 				break;
1445 			}
1446 			ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1447 		}
1448 
1449 		if (ND.basereachable != 0) {
1450 			int obasereachable = ND_IFINFO(ifp)->basereachable;
1451 
1452 			ND_IFINFO(ifp)->basereachable = ND.basereachable;
1453 			if (ND.basereachable != obasereachable)
1454 				ND_IFINFO(ifp)->reachable =
1455 				    ND_COMPUTE_RTIME(ND.basereachable);
1456 		}
1457 		if (ND.retrans != 0)
1458 			ND_IFINFO(ifp)->retrans = ND.retrans;
1459 		if (ND.chlim != 0)
1460 			ND_IFINFO(ifp)->chlim = ND.chlim;
1461 		/* FALLTHROUGH */
1462 	case SIOCSIFINFO_FLAGS:
1463 	{
1464 		struct ifaddr *ifa;
1465 		struct in6_ifaddr *ia;
1466 
1467 		if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1468 		    !(ND.flags & ND6_IFF_IFDISABLED)) {
1469 			/* ifdisabled 1->0 transision */
1470 
1471 			/*
1472 			 * If the interface is marked as ND6_IFF_IFDISABLED and
1473 			 * has an link-local address with IN6_IFF_DUPLICATED,
1474 			 * do not clear ND6_IFF_IFDISABLED.
1475 			 * See RFC 4862, Section 5.4.5.
1476 			 */
1477 			int duplicated_linklocal = 0;
1478 
1479 			IF_ADDR_RLOCK(ifp);
1480 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1481 				if (ifa->ifa_addr->sa_family != AF_INET6)
1482 					continue;
1483 				ia = (struct in6_ifaddr *)ifa;
1484 				if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
1485 				    IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) {
1486 					duplicated_linklocal = 1;
1487 					break;
1488 				}
1489 			}
1490 			IF_ADDR_RUNLOCK(ifp);
1491 
1492 			if (duplicated_linklocal) {
1493 				ND.flags |= ND6_IFF_IFDISABLED;
1494 				log(LOG_ERR, "Cannot enable an interface"
1495 				    " with a link-local address marked"
1496 				    " duplicate.\n");
1497 			} else {
1498 				ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED;
1499 				if (ifp->if_flags & IFF_UP)
1500 					in6_if_up(ifp);
1501 			}
1502 		} else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1503 			    (ND.flags & ND6_IFF_IFDISABLED)) {
1504 			/* ifdisabled 0->1 transision */
1505 			/* Mark all IPv6 address as tentative. */
1506 
1507 			ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1508 			IF_ADDR_RLOCK(ifp);
1509 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1510 				if (ifa->ifa_addr->sa_family != AF_INET6)
1511 					continue;
1512 				ia = (struct in6_ifaddr *)ifa;
1513 				ia->ia6_flags |= IN6_IFF_TENTATIVE;
1514 			}
1515 			IF_ADDR_RUNLOCK(ifp);
1516 		}
1517 
1518 		if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) {
1519 			if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) {
1520 				/* auto_linklocal 0->1 transision */
1521 
1522 				/* If no link-local address on ifp, configure */
1523 				ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL;
1524 				in6_ifattach(ifp, NULL);
1525 			} else if (!(ND.flags & ND6_IFF_IFDISABLED) &&
1526 			    ifp->if_flags & IFF_UP) {
1527 				/*
1528 				 * When the IF already has
1529 				 * ND6_IFF_AUTO_LINKLOCAL, no link-local
1530 				 * address is assigned, and IFF_UP, try to
1531 				 * assign one.
1532 				 */
1533 				int haslinklocal = 0;
1534 
1535 				IF_ADDR_RLOCK(ifp);
1536 				TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1537 					if (ifa->ifa_addr->sa_family != AF_INET6)
1538 						continue;
1539 					ia = (struct in6_ifaddr *)ifa;
1540 					if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) {
1541 						haslinklocal = 1;
1542 						break;
1543 					}
1544 				}
1545 				IF_ADDR_RUNLOCK(ifp);
1546 				if (!haslinklocal)
1547 					in6_ifattach(ifp, NULL);
1548 			}
1549 		}
1550 	}
1551 		ND_IFINFO(ifp)->flags = ND.flags;
1552 		break;
1553 #undef ND
1554 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1555 		/* sync kernel routing table with the default router list */
1556 		defrouter_reset();
1557 		defrouter_select();
1558 		break;
1559 	case SIOCSPFXFLUSH_IN6:
1560 	{
1561 		/* flush all the prefix advertised by routers */
1562 		struct nd_prefix *pr, *next;
1563 
1564 		LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, next) {
1565 			struct in6_ifaddr *ia, *ia_next;
1566 
1567 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1568 				continue; /* XXX */
1569 
1570 			/* do we really have to remove addresses as well? */
1571 			/* XXXRW: in6_ifaddrhead locking. */
1572 			TAILQ_FOREACH_SAFE(ia, &V_in6_ifaddrhead, ia_link,
1573 			    ia_next) {
1574 				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1575 					continue;
1576 
1577 				if (ia->ia6_ndpr == pr)
1578 					in6_purgeaddr(&ia->ia_ifa);
1579 			}
1580 			prelist_remove(pr);
1581 		}
1582 		break;
1583 	}
1584 	case SIOCSRTRFLUSH_IN6:
1585 	{
1586 		/* flush all the default routers */
1587 		struct nd_defrouter *dr, *next;
1588 
1589 		defrouter_reset();
1590 		TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, next) {
1591 			defrtrlist_del(dr);
1592 		}
1593 		defrouter_select();
1594 		break;
1595 	}
1596 	case SIOCGNBRINFO_IN6:
1597 	{
1598 		struct llentry *ln;
1599 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1600 
1601 		if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1602 			return (error);
1603 
1604 		IF_AFDATA_RLOCK(ifp);
1605 		ln = nd6_lookup(&nb_addr, 0, ifp);
1606 		IF_AFDATA_RUNLOCK(ifp);
1607 
1608 		if (ln == NULL) {
1609 			error = EINVAL;
1610 			break;
1611 		}
1612 		nbi->state = ln->ln_state;
1613 		nbi->asked = ln->la_asked;
1614 		nbi->isrouter = ln->ln_router;
1615 		if (ln->la_expire == 0)
1616 			nbi->expire = 0;
1617 		else
1618 			nbi->expire = ln->la_expire +
1619 			    (time_second - time_uptime);
1620 		LLE_RUNLOCK(ln);
1621 		break;
1622 	}
1623 	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1624 		ndif->ifindex = V_nd6_defifindex;
1625 		break;
1626 	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1627 		return (nd6_setdefaultiface(ndif->ifindex));
1628 	}
1629 	return (error);
1630 }
1631 
1632 /*
1633  * Create neighbor cache entry and cache link-layer address,
1634  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1635  *
1636  * type - ICMP6 type
1637  * code - type dependent information
1638  *
1639  * XXXXX
1640  *  The caller of this function already acquired the ndp
1641  *  cache table lock because the cache entry is returned.
1642  */
1643 struct llentry *
1644 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1645     int lladdrlen, int type, int code)
1646 {
1647 	struct llentry *ln = NULL;
1648 	int is_newentry;
1649 	int do_update;
1650 	int olladdr;
1651 	int llchange;
1652 	int flags;
1653 	int newstate = 0;
1654 	uint16_t router = 0;
1655 	struct sockaddr_in6 sin6;
1656 	struct mbuf *chain = NULL;
1657 	int static_route = 0;
1658 
1659 	IF_AFDATA_UNLOCK_ASSERT(ifp);
1660 
1661 	KASSERT(ifp != NULL, ("%s: ifp == NULL", __func__));
1662 	KASSERT(from != NULL, ("%s: from == NULL", __func__));
1663 
1664 	/* nothing must be updated for unspecified address */
1665 	if (IN6_IS_ADDR_UNSPECIFIED(from))
1666 		return NULL;
1667 
1668 	/*
1669 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1670 	 * the caller.
1671 	 *
1672 	 * XXX If the link does not have link-layer adderss, what should
1673 	 * we do? (ifp->if_addrlen == 0)
1674 	 * Spec says nothing in sections for RA, RS and NA.  There's small
1675 	 * description on it in NS section (RFC 2461 7.2.3).
1676 	 */
1677 	flags = lladdr ? ND6_EXCLUSIVE : 0;
1678 	IF_AFDATA_RLOCK(ifp);
1679 	ln = nd6_lookup(from, flags, ifp);
1680 	IF_AFDATA_RUNLOCK(ifp);
1681 	if (ln == NULL) {
1682 		flags |= ND6_EXCLUSIVE;
1683 		IF_AFDATA_LOCK(ifp);
1684 		ln = nd6_create(from, 0, ifp);
1685 		IF_AFDATA_UNLOCK(ifp);
1686 		is_newentry = 1;
1687 	} else {
1688 		/* do nothing if static ndp is set */
1689 		if (ln->la_flags & LLE_STATIC) {
1690 			static_route = 1;
1691 			goto done;
1692 		}
1693 		is_newentry = 0;
1694 	}
1695 	if (ln == NULL)
1696 		return (NULL);
1697 
1698 	olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
1699 	if (olladdr && lladdr) {
1700 		llchange = bcmp(lladdr, &ln->ll_addr,
1701 		    ifp->if_addrlen);
1702 	} else
1703 		llchange = 0;
1704 
1705 	/*
1706 	 * newentry olladdr  lladdr  llchange	(*=record)
1707 	 *	0	n	n	--	(1)
1708 	 *	0	y	n	--	(2)
1709 	 *	0	n	y	--	(3) * STALE
1710 	 *	0	y	y	n	(4) *
1711 	 *	0	y	y	y	(5) * STALE
1712 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1713 	 *	1	--	y	--	(7) * STALE
1714 	 */
1715 
1716 	if (lladdr) {		/* (3-5) and (7) */
1717 		/*
1718 		 * Record source link-layer address
1719 		 * XXX is it dependent to ifp->if_type?
1720 		 */
1721 		bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
1722 		ln->la_flags |= LLE_VALID;
1723 		EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
1724 	}
1725 
1726 	if (!is_newentry) {
1727 		if ((!olladdr && lladdr != NULL) ||	/* (3) */
1728 		    (olladdr && lladdr != NULL && llchange)) {	/* (5) */
1729 			do_update = 1;
1730 			newstate = ND6_LLINFO_STALE;
1731 		} else					/* (1-2,4) */
1732 			do_update = 0;
1733 	} else {
1734 		do_update = 1;
1735 		if (lladdr == NULL)			/* (6) */
1736 			newstate = ND6_LLINFO_NOSTATE;
1737 		else					/* (7) */
1738 			newstate = ND6_LLINFO_STALE;
1739 	}
1740 
1741 	if (do_update) {
1742 		/*
1743 		 * Update the state of the neighbor cache.
1744 		 */
1745 		ln->ln_state = newstate;
1746 
1747 		if (ln->ln_state == ND6_LLINFO_STALE) {
1748 			if (ln->la_hold != NULL)
1749 				nd6_grab_holdchain(ln, &chain, &sin6);
1750 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1751 			/* probe right away */
1752 			nd6_llinfo_settimer_locked((void *)ln, 0);
1753 		}
1754 	}
1755 
1756 	/*
1757 	 * ICMP6 type dependent behavior.
1758 	 *
1759 	 * NS: clear IsRouter if new entry
1760 	 * RS: clear IsRouter
1761 	 * RA: set IsRouter if there's lladdr
1762 	 * redir: clear IsRouter if new entry
1763 	 *
1764 	 * RA case, (1):
1765 	 * The spec says that we must set IsRouter in the following cases:
1766 	 * - If lladdr exist, set IsRouter.  This means (1-5).
1767 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1768 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1769 	 * A quetion arises for (1) case.  (1) case has no lladdr in the
1770 	 * neighbor cache, this is similar to (6).
1771 	 * This case is rare but we figured that we MUST NOT set IsRouter.
1772 	 *
1773 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1774 	 *							D R
1775 	 *	0	n	n	--	(1)	c   ?     s
1776 	 *	0	y	n	--	(2)	c   s     s
1777 	 *	0	n	y	--	(3)	c   s     s
1778 	 *	0	y	y	n	(4)	c   s     s
1779 	 *	0	y	y	y	(5)	c   s     s
1780 	 *	1	--	n	--	(6) c	c	c s
1781 	 *	1	--	y	--	(7) c	c   s	c s
1782 	 *
1783 	 *					(c=clear s=set)
1784 	 */
1785 	switch (type & 0xff) {
1786 	case ND_NEIGHBOR_SOLICIT:
1787 		/*
1788 		 * New entry must have is_router flag cleared.
1789 		 */
1790 		if (is_newentry)	/* (6-7) */
1791 			ln->ln_router = 0;
1792 		break;
1793 	case ND_REDIRECT:
1794 		/*
1795 		 * If the icmp is a redirect to a better router, always set the
1796 		 * is_router flag.  Otherwise, if the entry is newly created,
1797 		 * clear the flag.  [RFC 2461, sec 8.3]
1798 		 */
1799 		if (code == ND_REDIRECT_ROUTER)
1800 			ln->ln_router = 1;
1801 		else if (is_newentry) /* (6-7) */
1802 			ln->ln_router = 0;
1803 		break;
1804 	case ND_ROUTER_SOLICIT:
1805 		/*
1806 		 * is_router flag must always be cleared.
1807 		 */
1808 		ln->ln_router = 0;
1809 		break;
1810 	case ND_ROUTER_ADVERT:
1811 		/*
1812 		 * Mark an entry with lladdr as a router.
1813 		 */
1814 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
1815 		    (is_newentry && lladdr)) {			/* (7) */
1816 			ln->ln_router = 1;
1817 		}
1818 		break;
1819 	}
1820 
1821 	if (ln != NULL) {
1822 		static_route = (ln->la_flags & LLE_STATIC);
1823 		router = ln->ln_router;
1824 
1825 		if (flags & ND6_EXCLUSIVE)
1826 			LLE_WUNLOCK(ln);
1827 		else
1828 			LLE_RUNLOCK(ln);
1829 		if (static_route)
1830 			ln = NULL;
1831 	}
1832 	if (chain != NULL)
1833 		nd6_flush_holdchain(ifp, ifp, chain, &sin6);
1834 
1835 	/*
1836 	 * When the link-layer address of a router changes, select the
1837 	 * best router again.  In particular, when the neighbor entry is newly
1838 	 * created, it might affect the selection policy.
1839 	 * Question: can we restrict the first condition to the "is_newentry"
1840 	 * case?
1841 	 * XXX: when we hear an RA from a new router with the link-layer
1842 	 * address option, defrouter_select() is called twice, since
1843 	 * defrtrlist_update called the function as well.  However, I believe
1844 	 * we can compromise the overhead, since it only happens the first
1845 	 * time.
1846 	 * XXX: although defrouter_select() should not have a bad effect
1847 	 * for those are not autoconfigured hosts, we explicitly avoid such
1848 	 * cases for safety.
1849 	 */
1850 	if (do_update && router &&
1851 	    ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1852 		/*
1853 		 * guaranteed recursion
1854 		 */
1855 		defrouter_select();
1856 	}
1857 
1858 	return (ln);
1859 done:
1860 	if (ln != NULL) {
1861 		if (flags & ND6_EXCLUSIVE)
1862 			LLE_WUNLOCK(ln);
1863 		else
1864 			LLE_RUNLOCK(ln);
1865 		if (static_route)
1866 			ln = NULL;
1867 	}
1868 	return (ln);
1869 }
1870 
1871 static void
1872 nd6_slowtimo(void *arg)
1873 {
1874 	CURVNET_SET((struct vnet *) arg);
1875 	struct nd_ifinfo *nd6if;
1876 	struct ifnet *ifp;
1877 
1878 	callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1879 	    nd6_slowtimo, curvnet);
1880 	IFNET_RLOCK_NOSLEEP();
1881 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1882 		if (ifp->if_afdata[AF_INET6] == NULL)
1883 			continue;
1884 		nd6if = ND_IFINFO(ifp);
1885 		if (nd6if->basereachable && /* already initialized */
1886 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1887 			/*
1888 			 * Since reachable time rarely changes by router
1889 			 * advertisements, we SHOULD insure that a new random
1890 			 * value gets recomputed at least once every few hours.
1891 			 * (RFC 2461, 6.3.4)
1892 			 */
1893 			nd6if->recalctm = V_nd6_recalc_reachtm_interval;
1894 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1895 		}
1896 	}
1897 	IFNET_RUNLOCK_NOSLEEP();
1898 	CURVNET_RESTORE();
1899 }
1900 
1901 void
1902 nd6_grab_holdchain(struct llentry *ln, struct mbuf **chain,
1903     struct sockaddr_in6 *sin6)
1904 {
1905 
1906 	LLE_WLOCK_ASSERT(ln);
1907 
1908 	*chain = ln->la_hold;
1909 	ln->la_hold = NULL;
1910 	memcpy(sin6, L3_ADDR_SIN6(ln), sizeof(*sin6));
1911 
1912 	if (ln->ln_state == ND6_LLINFO_STALE) {
1913 
1914 		/*
1915 		 * The first time we send a packet to a
1916 		 * neighbor whose entry is STALE, we have
1917 		 * to change the state to DELAY and a sets
1918 		 * a timer to expire in DELAY_FIRST_PROBE_TIME
1919 		 * seconds to ensure do neighbor unreachability
1920 		 * detection on expiration.
1921 		 * (RFC 2461 7.3.3)
1922 		 */
1923 		ln->la_asked = 0;
1924 		ln->ln_state = ND6_LLINFO_DELAY;
1925 		nd6_llinfo_settimer_locked(ln, (long)V_nd6_delay * hz);
1926 	}
1927 }
1928 
1929 static int
1930 nd6_output_ifp(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1931     struct sockaddr_in6 *dst)
1932 {
1933 	int error;
1934 	int ip6len;
1935 	struct ip6_hdr *ip6;
1936 	struct m_tag *mtag;
1937 
1938 #ifdef MAC
1939 	mac_netinet6_nd6_send(ifp, m);
1940 #endif
1941 
1942 	/*
1943 	 * If called from nd6_ns_output() (NS), nd6_na_output() (NA),
1944 	 * icmp6_redirect_output() (REDIRECT) or from rip6_output() (RS, RA
1945 	 * as handled by rtsol and rtadvd), mbufs will be tagged for SeND
1946 	 * to be diverted to user space.  When re-injected into the kernel,
1947 	 * send_output() will directly dispatch them to the outgoing interface.
1948 	 */
1949 	if (send_sendso_input_hook != NULL) {
1950 		mtag = m_tag_find(m, PACKET_TAG_ND_OUTGOING, NULL);
1951 		if (mtag != NULL) {
1952 			ip6 = mtod(m, struct ip6_hdr *);
1953 			ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
1954 			/* Use the SEND socket */
1955 			error = send_sendso_input_hook(m, ifp, SND_OUT,
1956 			    ip6len);
1957 			/* -1 == no app on SEND socket */
1958 			if (error == 0 || error != -1)
1959 			    return (error);
1960 		}
1961 	}
1962 
1963 	m_clrprotoflags(m);	/* Avoid confusing lower layers. */
1964 	IP_PROBE(send, NULL, NULL, mtod(m, struct ip6_hdr *), ifp, NULL,
1965 	    mtod(m, struct ip6_hdr *));
1966 
1967 	if ((ifp->if_flags & IFF_LOOPBACK) == 0)
1968 		origifp = ifp;
1969 
1970 	error = (*ifp->if_output)(origifp, m, (struct sockaddr *)dst, NULL);
1971 	return (error);
1972 }
1973 
1974 /*
1975  * IPv6 packet output - light version.
1976  * Checks if destination LLE exists and is in proper state
1977  * (e.g no modification required). If not true, fall back to
1978  * "heavy" version.
1979  */
1980 int
1981 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
1982     struct sockaddr_in6 *dst, struct rtentry *rt0)
1983 {
1984 	struct llentry *ln = NULL;
1985 
1986 	/* discard the packet if IPv6 operation is disabled on the interface */
1987 	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
1988 		m_freem(m);
1989 		return (ENETDOWN); /* better error? */
1990 	}
1991 
1992 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1993 		goto sendpkt;
1994 
1995 	if (nd6_need_cache(ifp) == 0)
1996 		goto sendpkt;
1997 
1998 	IF_AFDATA_RLOCK(ifp);
1999 	ln = nd6_lookup(&dst->sin6_addr, 0, ifp);
2000 	IF_AFDATA_RUNLOCK(ifp);
2001 
2002 	/*
2003 	 * Perform fast path for the following cases:
2004 	 * 1) lle state is REACHABLE
2005 	 * 2) lle state is DELAY (NS message sentNS message sent)
2006 	 *
2007 	 * Every other case involves lle modification, so we handle
2008 	 * them separately.
2009 	 */
2010 	if (ln == NULL || (ln->ln_state != ND6_LLINFO_REACHABLE &&
2011 	    ln->ln_state != ND6_LLINFO_DELAY)) {
2012 		/* Fall back to slow processing path */
2013 		if (ln != NULL)
2014 			LLE_RUNLOCK(ln);
2015 		return (nd6_output_lle(ifp, origifp, m, dst));
2016 	}
2017 
2018 sendpkt:
2019 	if (ln != NULL)
2020 		LLE_RUNLOCK(ln);
2021 
2022 	return (nd6_output_ifp(ifp, origifp, m, dst));
2023 }
2024 
2025 
2026 /*
2027  * Output IPv6 packet - heavy version.
2028  * Function assume that either
2029  * 1) destination LLE does not exist, is invalid or stale, so
2030  *   ND6_EXCLUSIVE lock needs to be acquired
2031  * 2) destination lle is provided (with ND6_EXCLUSIVE lock),
2032  *   in that case packets are queued in &chain.
2033  *
2034  */
2035 static int
2036 nd6_output_lle(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
2037     struct sockaddr_in6 *dst)
2038 {
2039 	struct llentry *lle = NULL;
2040 
2041 	KASSERT(m != NULL, ("NULL mbuf, nothing to send"));
2042 	/* discard the packet if IPv6 operation is disabled on the interface */
2043 	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
2044 		m_freem(m);
2045 		return (ENETDOWN); /* better error? */
2046 	}
2047 
2048 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
2049 		goto sendpkt;
2050 
2051 	if (nd6_need_cache(ifp) == 0)
2052 		goto sendpkt;
2053 
2054 	/*
2055 	 * Address resolution or Neighbor Unreachability Detection
2056 	 * for the next hop.
2057 	 * At this point, the destination of the packet must be a unicast
2058 	 * or an anycast address(i.e. not a multicast).
2059 	 */
2060 	if (lle == NULL) {
2061 		IF_AFDATA_RLOCK(ifp);
2062 		lle = nd6_lookup(&dst->sin6_addr, ND6_EXCLUSIVE, ifp);
2063 		IF_AFDATA_RUNLOCK(ifp);
2064 		if ((lle == NULL) && nd6_is_addr_neighbor(dst, ifp))  {
2065 			/*
2066 			 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2067 			 * the condition below is not very efficient.  But we believe
2068 			 * it is tolerable, because this should be a rare case.
2069 			 */
2070 			IF_AFDATA_LOCK(ifp);
2071 			lle = nd6_create(&dst->sin6_addr, 0, ifp);
2072 			IF_AFDATA_UNLOCK(ifp);
2073 		}
2074 	}
2075 	if (lle == NULL) {
2076 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
2077 		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
2078 			char ip6buf[INET6_ADDRSTRLEN];
2079 			log(LOG_DEBUG,
2080 			    "nd6_output: can't allocate llinfo for %s "
2081 			    "(ln=%p)\n",
2082 			    ip6_sprintf(ip6buf, &dst->sin6_addr), lle);
2083 			m_freem(m);
2084 			return (ENOBUFS);
2085 		}
2086 		goto sendpkt;	/* send anyway */
2087 	}
2088 
2089 	LLE_WLOCK_ASSERT(lle);
2090 
2091 	/* We don't have to do link-layer address resolution on a p2p link. */
2092 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
2093 	    lle->ln_state < ND6_LLINFO_REACHABLE) {
2094 		lle->ln_state = ND6_LLINFO_STALE;
2095 		nd6_llinfo_settimer_locked(lle, (long)V_nd6_gctimer * hz);
2096 	}
2097 
2098 	/*
2099 	 * The first time we send a packet to a neighbor whose entry is
2100 	 * STALE, we have to change the state to DELAY and a sets a timer to
2101 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2102 	 * neighbor unreachability detection on expiration.
2103 	 * (RFC 2461 7.3.3)
2104 	 */
2105 	if (lle->ln_state == ND6_LLINFO_STALE) {
2106 		lle->la_asked = 0;
2107 		lle->ln_state = ND6_LLINFO_DELAY;
2108 		nd6_llinfo_settimer_locked(lle, (long)V_nd6_delay * hz);
2109 	}
2110 
2111 	/*
2112 	 * If the neighbor cache entry has a state other than INCOMPLETE
2113 	 * (i.e. its link-layer address is already resolved), just
2114 	 * send the packet.
2115 	 */
2116 	if (lle->ln_state > ND6_LLINFO_INCOMPLETE)
2117 		goto sendpkt;
2118 
2119 	/*
2120 	 * There is a neighbor cache entry, but no ethernet address
2121 	 * response yet.  Append this latest packet to the end of the
2122 	 * packet queue in the mbuf, unless the number of the packet
2123 	 * does not exceed nd6_maxqueuelen.  When it exceeds nd6_maxqueuelen,
2124 	 * the oldest packet in the queue will be removed.
2125 	 */
2126 	if (lle->ln_state == ND6_LLINFO_NOSTATE)
2127 		lle->ln_state = ND6_LLINFO_INCOMPLETE;
2128 
2129 	if (lle->la_hold != NULL) {
2130 		struct mbuf *m_hold;
2131 		int i;
2132 
2133 		i = 0;
2134 		for (m_hold = lle->la_hold; m_hold; m_hold = m_hold->m_nextpkt){
2135 			i++;
2136 			if (m_hold->m_nextpkt == NULL) {
2137 				m_hold->m_nextpkt = m;
2138 				break;
2139 			}
2140 		}
2141 		while (i >= V_nd6_maxqueuelen) {
2142 			m_hold = lle->la_hold;
2143 			lle->la_hold = lle->la_hold->m_nextpkt;
2144 			m_freem(m_hold);
2145 			i--;
2146 		}
2147 	} else {
2148 		lle->la_hold = m;
2149 	}
2150 
2151 	/*
2152 	 * If there has been no NS for the neighbor after entering the
2153 	 * INCOMPLETE state, send the first solicitation.
2154 	 */
2155 	if (!ND6_LLINFO_PERMANENT(lle) && lle->la_asked == 0) {
2156 		lle->la_asked++;
2157 
2158 		nd6_llinfo_settimer_locked(lle,
2159 		    (long)ND_IFINFO(ifp)->retrans * hz / 1000);
2160 		LLE_WUNLOCK(lle);
2161 		nd6_ns_output(ifp, NULL, &dst->sin6_addr, lle, NULL);
2162 	} else {
2163 		/* We did the lookup so we need to do the unlock here. */
2164 		LLE_WUNLOCK(lle);
2165 	}
2166 
2167 	return (0);
2168 
2169   sendpkt:
2170 	if (lle != NULL)
2171 		LLE_WUNLOCK(lle);
2172 
2173 	return (nd6_output_ifp(ifp, origifp, m, dst));
2174 }
2175 
2176 
2177 int
2178 nd6_flush_holdchain(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *chain,
2179     struct sockaddr_in6 *dst)
2180 {
2181 	struct mbuf *m, *m_head;
2182 	struct ifnet *outifp;
2183 	int error = 0;
2184 
2185 	m_head = chain;
2186 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2187 		outifp = origifp;
2188 	else
2189 		outifp = ifp;
2190 
2191 	while (m_head) {
2192 		m = m_head;
2193 		m_head = m_head->m_nextpkt;
2194 		error = nd6_output_ifp(ifp, origifp, m, dst);
2195 	}
2196 
2197 	/*
2198 	 * XXX
2199 	 * note that intermediate errors are blindly ignored - but this is
2200 	 * the same convention as used with nd6_output when called by
2201 	 * nd6_cache_lladdr
2202 	 */
2203 	return (error);
2204 }
2205 
2206 
2207 int
2208 nd6_need_cache(struct ifnet *ifp)
2209 {
2210 	/*
2211 	 * XXX: we currently do not make neighbor cache on any interface
2212 	 * other than ARCnet, Ethernet, FDDI and GIF.
2213 	 *
2214 	 * RFC2893 says:
2215 	 * - unidirectional tunnels needs no ND
2216 	 */
2217 	switch (ifp->if_type) {
2218 	case IFT_ARCNET:
2219 	case IFT_ETHER:
2220 	case IFT_FDDI:
2221 	case IFT_IEEE1394:
2222 	case IFT_L2VLAN:
2223 	case IFT_IEEE80211:
2224 	case IFT_INFINIBAND:
2225 	case IFT_BRIDGE:
2226 	case IFT_PROPVIRTUAL:
2227 		return (1);
2228 	default:
2229 		return (0);
2230 	}
2231 }
2232 
2233 /*
2234  * Add pernament ND6 link-layer record for given
2235  * interface address.
2236  *
2237  * Very similar to IPv4 arp_ifinit(), but:
2238  * 1) IPv6 DAD is performed in different place
2239  * 2) It is called by IPv6 protocol stack in contrast to
2240  * arp_ifinit() which is typically called in SIOCSIFADDR
2241  * driver ioctl handler.
2242  *
2243  */
2244 int
2245 nd6_add_ifa_lle(struct in6_ifaddr *ia)
2246 {
2247 	struct ifnet *ifp;
2248 	struct llentry *ln;
2249 
2250 	ifp = ia->ia_ifa.ifa_ifp;
2251 	if (nd6_need_cache(ifp) == 0)
2252 		return (0);
2253 	IF_AFDATA_LOCK(ifp);
2254 	ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
2255 	ln = lla_create(LLTABLE6(ifp), LLE_IFADDR,
2256 	    (struct sockaddr *)&ia->ia_addr);
2257 	IF_AFDATA_UNLOCK(ifp);
2258 	if (ln != NULL) {
2259 		ln->la_expire = 0;  /* for IPv6 this means permanent */
2260 		ln->ln_state = ND6_LLINFO_REACHABLE;
2261 		LLE_WUNLOCK(ln);
2262 		return (0);
2263 	}
2264 
2265 	return (ENOBUFS);
2266 }
2267 
2268 /*
2269  * Removes ALL lle records for interface address prefix.
2270  * XXXME: That's probably not we really want to do, we need
2271  * to remove address record only and keep other records
2272  * until we determine if given prefix is really going
2273  * to be removed.
2274  */
2275 void
2276 nd6_rem_ifa_lle(struct in6_ifaddr *ia)
2277 {
2278 	struct sockaddr_in6 mask, addr;
2279 	struct ifnet *ifp;
2280 
2281 	ifp = ia->ia_ifa.ifa_ifp;
2282 	memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
2283 	memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
2284 	lltable_prefix_free(AF_INET6, (struct sockaddr *)&addr,
2285 	            (struct sockaddr *)&mask, LLE_STATIC);
2286 }
2287 
2288 /*
2289  * the callers of this function need to be re-worked to drop
2290  * the lle lock, drop here for now
2291  */
2292 int
2293 nd6_storelladdr(struct ifnet *ifp, struct mbuf *m,
2294     const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
2295 {
2296 	struct llentry *ln;
2297 
2298 	if (pflags != NULL)
2299 		*pflags = 0;
2300 	IF_AFDATA_UNLOCK_ASSERT(ifp);
2301 	if (m != NULL && m->m_flags & M_MCAST) {
2302 		switch (ifp->if_type) {
2303 		case IFT_ETHER:
2304 		case IFT_FDDI:
2305 		case IFT_L2VLAN:
2306 		case IFT_IEEE80211:
2307 		case IFT_BRIDGE:
2308 		case IFT_ISO88025:
2309 			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2310 						 desten);
2311 			return (0);
2312 		default:
2313 			m_freem(m);
2314 			return (EAFNOSUPPORT);
2315 		}
2316 	}
2317 
2318 
2319 	/*
2320 	 * the entry should have been created in nd6_store_lladdr
2321 	 */
2322 	IF_AFDATA_RLOCK(ifp);
2323 	ln = lla_lookup(LLTABLE6(ifp), 0, dst);
2324 	IF_AFDATA_RUNLOCK(ifp);
2325 	if ((ln == NULL) || !(ln->la_flags & LLE_VALID)) {
2326 		if (ln != NULL)
2327 			LLE_RUNLOCK(ln);
2328 		/* this could happen, if we could not allocate memory */
2329 		m_freem(m);
2330 		return (1);
2331 	}
2332 
2333 	bcopy(&ln->ll_addr, desten, ifp->if_addrlen);
2334 	if (pflags != NULL)
2335 		*pflags = ln->la_flags;
2336 	LLE_RUNLOCK(ln);
2337 	/*
2338 	 * A *small* use after free race exists here
2339 	 */
2340 	return (0);
2341 }
2342 
2343 static void
2344 clear_llinfo_pqueue(struct llentry *ln)
2345 {
2346 	struct mbuf *m_hold, *m_hold_next;
2347 
2348 	for (m_hold = ln->la_hold; m_hold; m_hold = m_hold_next) {
2349 		m_hold_next = m_hold->m_nextpkt;
2350 		m_freem(m_hold);
2351 	}
2352 
2353 	ln->la_hold = NULL;
2354 	return;
2355 }
2356 
2357 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2358 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2359 #ifdef SYSCTL_DECL
2360 SYSCTL_DECL(_net_inet6_icmp6);
2361 #endif
2362 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2363 	CTLFLAG_RD, nd6_sysctl_drlist, "");
2364 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2365 	CTLFLAG_RD, nd6_sysctl_prlist, "");
2366 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, nd6_maxqueuelen,
2367 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_maxqueuelen), 1, "");
2368 SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_gctimer,
2369 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_gctimer), (60 * 60 * 24), "");
2370 
2371 static int
2372 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2373 {
2374 	struct in6_defrouter d;
2375 	struct nd_defrouter *dr;
2376 	int error;
2377 
2378 	if (req->newptr)
2379 		return (EPERM);
2380 
2381 	bzero(&d, sizeof(d));
2382 	d.rtaddr.sin6_family = AF_INET6;
2383 	d.rtaddr.sin6_len = sizeof(d.rtaddr);
2384 
2385 	/*
2386 	 * XXX locking
2387 	 */
2388 	TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
2389 		d.rtaddr.sin6_addr = dr->rtaddr;
2390 		error = sa6_recoverscope(&d.rtaddr);
2391 		if (error != 0)
2392 			return (error);
2393 		d.flags = dr->flags;
2394 		d.rtlifetime = dr->rtlifetime;
2395 		d.expire = dr->expire + (time_second - time_uptime);
2396 		d.if_index = dr->ifp->if_index;
2397 		error = SYSCTL_OUT(req, &d, sizeof(d));
2398 		if (error != 0)
2399 			return (error);
2400 	}
2401 	return (0);
2402 }
2403 
2404 static int
2405 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2406 {
2407 	struct in6_prefix p;
2408 	struct sockaddr_in6 s6;
2409 	struct nd_prefix *pr;
2410 	struct nd_pfxrouter *pfr;
2411 	time_t maxexpire;
2412 	int error;
2413 	char ip6buf[INET6_ADDRSTRLEN];
2414 
2415 	if (req->newptr)
2416 		return (EPERM);
2417 
2418 	bzero(&p, sizeof(p));
2419 	p.origin = PR_ORIG_RA;
2420 	bzero(&s6, sizeof(s6));
2421 	s6.sin6_family = AF_INET6;
2422 	s6.sin6_len = sizeof(s6);
2423 
2424 	/*
2425 	 * XXX locking
2426 	 */
2427 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
2428 		p.prefix = pr->ndpr_prefix;
2429 		if (sa6_recoverscope(&p.prefix)) {
2430 			log(LOG_ERR, "scope error in prefix list (%s)\n",
2431 			    ip6_sprintf(ip6buf, &p.prefix.sin6_addr));
2432 			/* XXX: press on... */
2433 		}
2434 		p.raflags = pr->ndpr_raf;
2435 		p.prefixlen = pr->ndpr_plen;
2436 		p.vltime = pr->ndpr_vltime;
2437 		p.pltime = pr->ndpr_pltime;
2438 		p.if_index = pr->ndpr_ifp->if_index;
2439 		if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2440 			p.expire = 0;
2441 		else {
2442 			/* XXX: we assume time_t is signed. */
2443 			maxexpire = (-1) &
2444 			    ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
2445 			if (pr->ndpr_vltime < maxexpire - pr->ndpr_lastupdate)
2446 				p.expire = pr->ndpr_lastupdate +
2447 				    pr->ndpr_vltime +
2448 				    (time_second - time_uptime);
2449 			else
2450 				p.expire = maxexpire;
2451 		}
2452 		p.refcnt = pr->ndpr_refcnt;
2453 		p.flags = pr->ndpr_stateflags;
2454 		p.advrtrs = 0;
2455 		LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
2456 			p.advrtrs++;
2457 		error = SYSCTL_OUT(req, &p, sizeof(p));
2458 		if (error != 0)
2459 			return (error);
2460 		LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2461 			s6.sin6_addr = pfr->router->rtaddr;
2462 			if (sa6_recoverscope(&s6))
2463 				log(LOG_ERR,
2464 				    "scope error in prefix list (%s)\n",
2465 				    ip6_sprintf(ip6buf, &pfr->router->rtaddr));
2466 			error = SYSCTL_OUT(req, &s6, sizeof(s6));
2467 			if (error != 0)
2468 				return (error);
2469 		}
2470 	}
2471 	return (0);
2472 }
2473