xref: /freebsd/sys/netinet6/nd6.c (revision b6a05070fa77edc7ce6e60b61623fd806e807be6)
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 #include <netinet/if_ether.h>
70 #include <netinet6/in6_var.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
73 #include <netinet6/scope6_var.h>
74 #include <netinet6/nd6.h>
75 #include <netinet6/in6_ifattach.h>
76 #include <netinet/icmp6.h>
77 #include <netinet6/send.h>
78 
79 #include <sys/limits.h>
80 
81 #include <security/mac/mac_framework.h>
82 
83 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
84 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
85 
86 #define SIN6(s) ((const struct sockaddr_in6 *)(s))
87 
88 /* timer values */
89 VNET_DEFINE(int, nd6_prune)	= 1;	/* walk list every 1 seconds */
90 VNET_DEFINE(int, nd6_delay)	= 5;	/* delay first probe time 5 second */
91 VNET_DEFINE(int, nd6_umaxtries)	= 3;	/* maximum unicast query */
92 VNET_DEFINE(int, nd6_mmaxtries)	= 3;	/* maximum multicast query */
93 VNET_DEFINE(int, nd6_useloopback) = 1;	/* use loopback interface for
94 					 * local traffic */
95 VNET_DEFINE(int, nd6_gctimer)	= (60 * 60 * 24); /* 1 day: garbage
96 					 * collection timer */
97 
98 /* preventing too many loops in ND option parsing */
99 static VNET_DEFINE(int, nd6_maxndopt) = 10; /* max # of ND options allowed */
100 
101 VNET_DEFINE(int, nd6_maxnudhint) = 0;	/* max # of subsequent upper
102 					 * layer hints */
103 static VNET_DEFINE(int, nd6_maxqueuelen) = 1; /* max pkts cached in unresolved
104 					 * ND entries */
105 #define	V_nd6_maxndopt			VNET(nd6_maxndopt)
106 #define	V_nd6_maxqueuelen		VNET(nd6_maxqueuelen)
107 
108 #ifdef ND6_DEBUG
109 VNET_DEFINE(int, nd6_debug) = 1;
110 #else
111 VNET_DEFINE(int, nd6_debug) = 0;
112 #endif
113 
114 static eventhandler_tag lle_event_eh;
115 
116 /* for debugging? */
117 #if 0
118 static int nd6_inuse, nd6_allocated;
119 #endif
120 
121 VNET_DEFINE(struct nd_drhead, nd_defrouter);
122 VNET_DEFINE(struct nd_prhead, nd_prefix);
123 
124 VNET_DEFINE(int, nd6_recalc_reachtm_interval) = ND6_RECALC_REACHTM_INTERVAL;
125 #define	V_nd6_recalc_reachtm_interval	VNET(nd6_recalc_reachtm_interval)
126 
127 int	(*send_sendso_input_hook)(struct mbuf *, struct ifnet *, int, int);
128 
129 static int nd6_is_new_addr_neighbor(const struct sockaddr_in6 *,
130 	struct ifnet *);
131 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
132 static void nd6_slowtimo(void *);
133 static int regen_tmpaddr(struct in6_ifaddr *);
134 static void nd6_free(struct llentry *, int);
135 static void nd6_free_redirect(const struct llentry *);
136 static void nd6_llinfo_timer(void *);
137 static void nd6_llinfo_settimer_locked(struct llentry *, long);
138 static void clear_llinfo_pqueue(struct llentry *);
139 static void nd6_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
140 static int nd6_resolve_slow(struct ifnet *, struct mbuf *,
141     const struct sockaddr_in6 *, u_char *, uint32_t *);
142 static int nd6_need_cache(struct ifnet *);
143 
144 
145 static VNET_DEFINE(struct callout, nd6_slowtimo_ch);
146 #define	V_nd6_slowtimo_ch		VNET(nd6_slowtimo_ch)
147 
148 VNET_DEFINE(struct callout, nd6_timer_ch);
149 
150 static void
151 nd6_lle_event(void *arg __unused, struct llentry *lle, int evt)
152 {
153 	struct rt_addrinfo rtinfo;
154 	struct sockaddr_in6 dst;
155 	struct sockaddr_dl gw;
156 	struct ifnet *ifp;
157 	int type;
158 
159 	LLE_WLOCK_ASSERT(lle);
160 
161 	if (lltable_get_af(lle->lle_tbl) != AF_INET6)
162 		return;
163 
164 	switch (evt) {
165 	case LLENTRY_RESOLVED:
166 		type = RTM_ADD;
167 		KASSERT(lle->la_flags & LLE_VALID,
168 		    ("%s: %p resolved but not valid?", __func__, lle));
169 		break;
170 	case LLENTRY_EXPIRED:
171 		type = RTM_DELETE;
172 		break;
173 	default:
174 		return;
175 	}
176 
177 	ifp = lltable_get_ifp(lle->lle_tbl);
178 
179 	bzero(&dst, sizeof(dst));
180 	bzero(&gw, sizeof(gw));
181 	bzero(&rtinfo, sizeof(rtinfo));
182 	lltable_fill_sa_entry(lle, (struct sockaddr *)&dst);
183 	dst.sin6_scope_id = in6_getscopezone(ifp,
184 	    in6_addrscope(&dst.sin6_addr));
185 	gw.sdl_len = sizeof(struct sockaddr_dl);
186 	gw.sdl_family = AF_LINK;
187 	gw.sdl_alen = ifp->if_addrlen;
188 	gw.sdl_index = ifp->if_index;
189 	gw.sdl_type = ifp->if_type;
190 	if (evt == LLENTRY_RESOLVED)
191 		bcopy(&lle->ll_addr, gw.sdl_data, ifp->if_addrlen);
192 	rtinfo.rti_info[RTAX_DST] = (struct sockaddr *)&dst;
193 	rtinfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gw;
194 	rtinfo.rti_addrs = RTA_DST | RTA_GATEWAY;
195 	rt_missmsg_fib(type, &rtinfo, RTF_HOST | RTF_LLDATA | (
196 	    type == RTM_ADD ? RTF_UP: 0), 0, RT_DEFAULT_FIB);
197 }
198 
199 void
200 nd6_init(void)
201 {
202 
203 	LIST_INIT(&V_nd_prefix);
204 
205 	/* initialization of the default router list */
206 	TAILQ_INIT(&V_nd_defrouter);
207 
208 	/* start timer */
209 	callout_init(&V_nd6_slowtimo_ch, 0);
210 	callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
211 	    nd6_slowtimo, curvnet);
212 
213 	nd6_dad_init();
214 	if (IS_DEFAULT_VNET(curvnet))
215 		lle_event_eh = EVENTHANDLER_REGISTER(lle_event, nd6_lle_event,
216 		    NULL, EVENTHANDLER_PRI_ANY);
217 }
218 
219 #ifdef VIMAGE
220 void
221 nd6_destroy()
222 {
223 
224 	callout_drain(&V_nd6_slowtimo_ch);
225 	callout_drain(&V_nd6_timer_ch);
226 	if (IS_DEFAULT_VNET(curvnet))
227 		EVENTHANDLER_DEREGISTER(lle_event, lle_event_eh);
228 }
229 #endif
230 
231 struct nd_ifinfo *
232 nd6_ifattach(struct ifnet *ifp)
233 {
234 	struct nd_ifinfo *nd;
235 
236 	nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK|M_ZERO);
237 	nd->initialized = 1;
238 
239 	nd->chlim = IPV6_DEFHLIM;
240 	nd->basereachable = REACHABLE_TIME;
241 	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
242 	nd->retrans = RETRANS_TIMER;
243 
244 	nd->flags = ND6_IFF_PERFORMNUD;
245 
246 	/* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL.
247 	 * XXXHRS: Clear ND6_IFF_AUTO_LINKLOCAL on an IFT_BRIDGE interface by
248 	 * default regardless of the V_ip6_auto_linklocal configuration to
249 	 * give a reasonable default behavior.
250 	 */
251 	if ((V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) ||
252 	    (ifp->if_flags & IFF_LOOPBACK))
253 		nd->flags |= ND6_IFF_AUTO_LINKLOCAL;
254 	/*
255 	 * A loopback interface does not need to accept RTADV.
256 	 * XXXHRS: Clear ND6_IFF_ACCEPT_RTADV on an IFT_BRIDGE interface by
257 	 * default regardless of the V_ip6_accept_rtadv configuration to
258 	 * prevent the interface from accepting RA messages arrived
259 	 * on one of the member interfaces with ND6_IFF_ACCEPT_RTADV.
260 	 */
261 	if (V_ip6_accept_rtadv &&
262 	    !(ifp->if_flags & IFF_LOOPBACK) &&
263 	    (ifp->if_type != IFT_BRIDGE))
264 			nd->flags |= ND6_IFF_ACCEPT_RTADV;
265 	if (V_ip6_no_radr && !(ifp->if_flags & IFF_LOOPBACK))
266 		nd->flags |= ND6_IFF_NO_RADR;
267 
268 	/* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
269 	nd6_setmtu0(ifp, nd);
270 
271 	return nd;
272 }
273 
274 void
275 nd6_ifdetach(struct nd_ifinfo *nd)
276 {
277 
278 	free(nd, M_IP6NDP);
279 }
280 
281 /*
282  * Reset ND level link MTU. This function is called when the physical MTU
283  * changes, which means we might have to adjust the ND level MTU.
284  */
285 void
286 nd6_setmtu(struct ifnet *ifp)
287 {
288 	if (ifp->if_afdata[AF_INET6] == NULL)
289 		return;
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 static 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->lle_timer);
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->lle_timer, INT_MAX,
506 			    nd6_llinfo_timer, ln);
507 		} else {
508 			ln->ln_ntick = 0;
509 			canceled = callout_reset(&ln->lle_timer, tick,
510 			    nd6_llinfo_timer, ln);
511 		}
512 	}
513 	if (canceled > 0)
514 		LLE_REMREF(ln);
515 }
516 
517 /*
518  * Gets source address of the first packet in hold queue
519  * and stores it in @src.
520  * Returns pointer to @src (if hold queue is not empty) or NULL.
521  *
522  * Set noinline to be dtrace-friendly
523  */
524 static __noinline struct in6_addr *
525 nd6_llinfo_get_holdsrc(struct llentry *ln, struct in6_addr *src)
526 {
527 	struct ip6_hdr hdr;
528 	struct mbuf *m;
529 
530 	if (ln->la_hold == NULL)
531 		return (NULL);
532 
533 	/*
534 	 * assume every packet in la_hold has the same IP header
535 	 */
536 	m = ln->la_hold;
537 	if (sizeof(hdr) > m->m_len)
538 		return (NULL);
539 
540 	m_copydata(m, 0, sizeof(hdr), (caddr_t)&hdr);
541 	*src = hdr.ip6_src;
542 
543 	return (src);
544 }
545 
546 /*
547  * Checks if we need to switch from STALE state.
548  *
549  * RFC 4861 requires switching from STALE to DELAY state
550  * on first packet matching entry, waiting V_nd6_delay and
551  * transition to PROBE state (if upper layer confirmation was
552  * not received).
553  *
554  * This code performs a bit differently:
555  * On packet hit we don't change state (but desired state
556  * can be guessed by control plane). However, after V_nd6_delay
557  * seconds code will transition to PROBE state (so DELAY state
558  * is kinda skipped in most situations).
559  *
560  * Typically, V_nd6_gctimer is bigger than V_nd6_delay, so
561  * we perform the following upon entering STALE state:
562  *
563  * 1) Arm timer to run each V_nd6_delay seconds to make sure that
564  * if packet was transmitted at the start of given interval, we
565  * would be able to switch to PROBE state in V_nd6_delay seconds
566  * as user expects.
567  *
568  * 2) Reschedule timer until original V_nd6_gctimer expires keeping
569  * lle in STALE state (remaining timer value stored in lle_remtime).
570  *
571  * 3) Reschedule timer if packet was transmitted less that V_nd6_delay
572  * seconds ago.
573  *
574  * Returns non-zero value if the entry is still STALE (storing
575  * the next timer interval in @pdelay).
576  *
577  * Returns zero value if original timer expired or we need to switch to
578  * PROBE (store that in @do_switch variable).
579  */
580 static int
581 nd6_is_stale(struct llentry *lle, long *pdelay, int *do_switch)
582 {
583 	int nd_delay, nd_gctimer, r_skip_req;
584 	time_t lle_hittime;
585 	long delay;
586 
587 	*do_switch = 0;
588 	nd_gctimer = V_nd6_gctimer;
589 	nd_delay = V_nd6_delay;
590 
591 	LLE_REQ_LOCK(lle);
592 	r_skip_req = lle->r_skip_req;
593 	lle_hittime = lle->lle_hittime;
594 	LLE_REQ_UNLOCK(lle);
595 
596 	if (r_skip_req > 0) {
597 
598 		/*
599 		 * Nonzero r_skip_req value was set upon entering
600 		 * STALE state. Since value was not changed, no
601 		 * packets were passed using this lle. Ask for
602 		 * timer reschedule and keep STALE state.
603 		 */
604 		delay = (long)(MIN(nd_gctimer, nd_delay));
605 		delay *= hz;
606 		if (lle->lle_remtime > delay)
607 			lle->lle_remtime -= delay;
608 		else {
609 			delay = lle->lle_remtime;
610 			lle->lle_remtime = 0;
611 		}
612 
613 		if (delay == 0) {
614 
615 			/*
616 			 * The original ng6_gctime timeout ended,
617 			 * no more rescheduling.
618 			 */
619 			return (0);
620 		}
621 
622 		*pdelay = delay;
623 		return (1);
624 	}
625 
626 	/*
627 	 * Packet received. Verify timestamp
628 	 */
629 	delay = (long)(time_uptime - lle_hittime);
630 	if (delay < nd_delay) {
631 
632 		/*
633 		 * V_nd6_delay still not passed since the first
634 		 * hit in STALE state.
635 		 * Reshedule timer and return.
636 		 */
637 		*pdelay = (long)(nd_delay - delay) * hz;
638 		return (1);
639 	}
640 
641 	/* Request switching to probe */
642 	*do_switch = 1;
643 	return (0);
644 }
645 
646 
647 /*
648  * Switch @lle state to new state optionally arming timers.
649  *
650  * Set noinline to be dtrace-friendly
651  */
652 __noinline void
653 nd6_llinfo_setstate(struct llentry *lle, int newstate)
654 {
655 	struct ifnet *ifp;
656 	int nd_gctimer, nd_delay;
657 	long delay, remtime;
658 
659 	delay = 0;
660 	remtime = 0;
661 
662 	switch (newstate) {
663 	case ND6_LLINFO_INCOMPLETE:
664 		ifp = lle->lle_tbl->llt_ifp;
665 		delay = (long)ND_IFINFO(ifp)->retrans * hz / 1000;
666 		break;
667 	case ND6_LLINFO_REACHABLE:
668 		if (!ND6_LLINFO_PERMANENT(lle)) {
669 			ifp = lle->lle_tbl->llt_ifp;
670 			delay = (long)ND_IFINFO(ifp)->reachable * hz;
671 		}
672 		break;
673 	case ND6_LLINFO_STALE:
674 
675 		/*
676 		 * Notify fast path that we want to know if any packet
677 		 * is transmitted by setting r_skip_req.
678 		 */
679 		LLE_REQ_LOCK(lle);
680 		lle->r_skip_req = 1;
681 		LLE_REQ_UNLOCK(lle);
682 		nd_delay = V_nd6_delay;
683 		nd_gctimer = V_nd6_gctimer;
684 
685 		delay = (long)(MIN(nd_gctimer, nd_delay)) * hz;
686 		remtime = (long)nd_gctimer * hz - delay;
687 		break;
688 	case ND6_LLINFO_DELAY:
689 		lle->la_asked = 0;
690 		delay = (long)V_nd6_delay * hz;
691 		break;
692 	}
693 
694 	if (delay > 0)
695 		nd6_llinfo_settimer_locked(lle, delay);
696 
697 	lle->lle_remtime = remtime;
698 	lle->ln_state = newstate;
699 }
700 
701 /*
702  * Timer-dependent part of nd state machine.
703  *
704  * Set noinline to be dtrace-friendly
705  */
706 static __noinline void
707 nd6_llinfo_timer(void *arg)
708 {
709 	struct llentry *ln;
710 	struct in6_addr *dst, *pdst, *psrc, src;
711 	struct ifnet *ifp;
712 	struct nd_ifinfo *ndi = NULL;
713 	int do_switch, send_ns;
714 	long delay;
715 
716 	KASSERT(arg != NULL, ("%s: arg NULL", __func__));
717 	ln = (struct llentry *)arg;
718 	LLE_WLOCK(ln);
719 	if (callout_pending(&ln->lle_timer)) {
720 		/*
721 		 * Here we are a bit odd here in the treatment of
722 		 * active/pending. If the pending bit is set, it got
723 		 * rescheduled before I ran. The active
724 		 * bit we ignore, since if it was stopped
725 		 * in ll_tablefree() and was currently running
726 		 * it would have return 0 so the code would
727 		 * not have deleted it since the callout could
728 		 * not be stopped so we want to go through
729 		 * with the delete here now. If the callout
730 		 * was restarted, the pending bit will be back on and
731 		 * we just want to bail since the callout_reset would
732 		 * return 1 and our reference would have been removed
733 		 * by nd6_llinfo_settimer_locked above since canceled
734 		 * would have been 1.
735 		 */
736 		LLE_WUNLOCK(ln);
737 		return;
738 	}
739 	ifp = ln->lle_tbl->llt_ifp;
740 	CURVNET_SET(ifp->if_vnet);
741 	ndi = ND_IFINFO(ifp);
742 	send_ns = 0;
743 	dst = &ln->r_l3addr.addr6;
744 	pdst = dst;
745 
746 	if (ln->ln_ntick > 0) {
747 		if (ln->ln_ntick > INT_MAX) {
748 			ln->ln_ntick -= INT_MAX;
749 			nd6_llinfo_settimer_locked(ln, INT_MAX);
750 		} else {
751 			ln->ln_ntick = 0;
752 			nd6_llinfo_settimer_locked(ln, ln->ln_ntick);
753 		}
754 		goto done;
755 	}
756 
757 	if (ln->la_flags & LLE_STATIC) {
758 		goto done;
759 	}
760 
761 	if (ln->la_flags & LLE_DELETED) {
762 		nd6_free(ln, 0);
763 		ln = NULL;
764 		goto done;
765 	}
766 
767 	switch (ln->ln_state) {
768 	case ND6_LLINFO_INCOMPLETE:
769 		if (ln->la_asked < V_nd6_mmaxtries) {
770 			ln->la_asked++;
771 			send_ns = 1;
772 			/* Send NS to multicast address */
773 			pdst = NULL;
774 		} else {
775 			struct mbuf *m = ln->la_hold;
776 			if (m) {
777 				struct mbuf *m0;
778 
779 				/*
780 				 * assuming every packet in la_hold has the
781 				 * same IP header.  Send error after unlock.
782 				 */
783 				m0 = m->m_nextpkt;
784 				m->m_nextpkt = NULL;
785 				ln->la_hold = m0;
786 				clear_llinfo_pqueue(ln);
787 			}
788 			EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_TIMEDOUT);
789 			nd6_free(ln, 0);
790 			ln = NULL;
791 			if (m != NULL)
792 				icmp6_error2(m, ICMP6_DST_UNREACH,
793 				    ICMP6_DST_UNREACH_ADDR, 0, ifp);
794 		}
795 		break;
796 	case ND6_LLINFO_REACHABLE:
797 		if (!ND6_LLINFO_PERMANENT(ln))
798 			nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
799 		break;
800 
801 	case ND6_LLINFO_STALE:
802 		if (nd6_is_stale(ln, &delay, &do_switch) != 0) {
803 
804 			/*
805 			 * No packet has used this entry and GC timeout
806 			 * has not been passed. Reshedule timer and
807 			 * return.
808 			 */
809 			nd6_llinfo_settimer_locked(ln, delay);
810 			break;
811 		}
812 
813 		if (do_switch == 0) {
814 
815 			/*
816 			 * GC timer has ended and entry hasn't been used.
817 			 * Run Garbage collector (RFC 4861, 5.3)
818 			 */
819 			if (!ND6_LLINFO_PERMANENT(ln)) {
820 				EVENTHANDLER_INVOKE(lle_event, ln,
821 				    LLENTRY_EXPIRED);
822 				nd6_free(ln, 1);
823 				ln = NULL;
824 			}
825 			break;
826 		}
827 
828 		/* Entry has been used AND delay timer has ended. */
829 
830 		/* FALLTHROUGH */
831 
832 	case ND6_LLINFO_DELAY:
833 		if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
834 			/* We need NUD */
835 			ln->la_asked = 1;
836 			nd6_llinfo_setstate(ln, ND6_LLINFO_PROBE);
837 			send_ns = 1;
838 		} else
839 			nd6_llinfo_setstate(ln, ND6_LLINFO_STALE); /* XXX */
840 		break;
841 	case ND6_LLINFO_PROBE:
842 		if (ln->la_asked < V_nd6_umaxtries) {
843 			ln->la_asked++;
844 			send_ns = 1;
845 		} else {
846 			EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_EXPIRED);
847 			nd6_free(ln, 0);
848 			ln = NULL;
849 		}
850 		break;
851 	default:
852 		panic("%s: paths in a dark night can be confusing: %d",
853 		    __func__, ln->ln_state);
854 	}
855 done:
856 	if (send_ns != 0) {
857 		nd6_llinfo_settimer_locked(ln, (long)ndi->retrans * hz / 1000);
858 		psrc = nd6_llinfo_get_holdsrc(ln, &src);
859 		LLE_FREE_LOCKED(ln);
860 		ln = NULL;
861 		nd6_ns_output(ifp, psrc, pdst, dst, NULL);
862 	}
863 
864 	if (ln != NULL)
865 		LLE_FREE_LOCKED(ln);
866 	CURVNET_RESTORE();
867 }
868 
869 
870 /*
871  * ND6 timer routine to expire default route list and prefix list
872  */
873 void
874 nd6_timer(void *arg)
875 {
876 	CURVNET_SET((struct vnet *) arg);
877 	struct nd_defrouter *dr, *ndr;
878 	struct nd_prefix *pr, *npr;
879 	struct in6_ifaddr *ia6, *nia6;
880 
881 	callout_reset(&V_nd6_timer_ch, V_nd6_prune * hz,
882 	    nd6_timer, curvnet);
883 
884 	/* expire default router list */
885 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
886 		if (dr->expire && dr->expire < time_uptime)
887 			defrtrlist_del(dr);
888 	}
889 
890 	/*
891 	 * expire interface addresses.
892 	 * in the past the loop was inside prefix expiry processing.
893 	 * However, from a stricter speci-confrmance standpoint, we should
894 	 * rather separate address lifetimes and prefix lifetimes.
895 	 *
896 	 * XXXRW: in6_ifaddrhead locking.
897 	 */
898   addrloop:
899 	TAILQ_FOREACH_SAFE(ia6, &V_in6_ifaddrhead, ia_link, nia6) {
900 		/* check address lifetime */
901 		if (IFA6_IS_INVALID(ia6)) {
902 			int regen = 0;
903 
904 			/*
905 			 * If the expiring address is temporary, try
906 			 * regenerating a new one.  This would be useful when
907 			 * we suspended a laptop PC, then turned it on after a
908 			 * period that could invalidate all temporary
909 			 * addresses.  Although we may have to restart the
910 			 * loop (see below), it must be after purging the
911 			 * address.  Otherwise, we'd see an infinite loop of
912 			 * regeneration.
913 			 */
914 			if (V_ip6_use_tempaddr &&
915 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
916 				if (regen_tmpaddr(ia6) == 0)
917 					regen = 1;
918 			}
919 
920 			in6_purgeaddr(&ia6->ia_ifa);
921 
922 			if (regen)
923 				goto addrloop; /* XXX: see below */
924 		} else if (IFA6_IS_DEPRECATED(ia6)) {
925 			int oldflags = ia6->ia6_flags;
926 
927 			ia6->ia6_flags |= IN6_IFF_DEPRECATED;
928 
929 			/*
930 			 * If a temporary address has just become deprecated,
931 			 * regenerate a new one if possible.
932 			 */
933 			if (V_ip6_use_tempaddr &&
934 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
935 			    (oldflags & IN6_IFF_DEPRECATED) == 0) {
936 
937 				if (regen_tmpaddr(ia6) == 0) {
938 					/*
939 					 * A new temporary address is
940 					 * generated.
941 					 * XXX: this means the address chain
942 					 * has changed while we are still in
943 					 * the loop.  Although the change
944 					 * would not cause disaster (because
945 					 * it's not a deletion, but an
946 					 * addition,) we'd rather restart the
947 					 * loop just for safety.  Or does this
948 					 * significantly reduce performance??
949 					 */
950 					goto addrloop;
951 				}
952 			}
953 		} else if ((ia6->ia6_flags & IN6_IFF_TENTATIVE) != 0) {
954 			/*
955 			 * Schedule DAD for a tentative address.  This happens
956 			 * if the interface was down or not running
957 			 * when the address was configured.
958 			 */
959 			int delay;
960 
961 			delay = arc4random() %
962 			    (MAX_RTR_SOLICITATION_DELAY * hz);
963 			nd6_dad_start((struct ifaddr *)ia6, delay);
964 		} else {
965 			/*
966 			 * Check status of the interface.  If it is down,
967 			 * mark the address as tentative for future DAD.
968 			 */
969 			if ((ia6->ia_ifp->if_flags & IFF_UP) == 0 ||
970 			    (ia6->ia_ifp->if_drv_flags & IFF_DRV_RUNNING)
971 				== 0 ||
972 			    (ND_IFINFO(ia6->ia_ifp)->flags &
973 				ND6_IFF_IFDISABLED) != 0) {
974 				ia6->ia6_flags &= ~IN6_IFF_DUPLICATED;
975 				ia6->ia6_flags |= IN6_IFF_TENTATIVE;
976 			}
977 			/*
978 			 * A new RA might have made a deprecated address
979 			 * preferred.
980 			 */
981 			ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
982 		}
983 	}
984 
985 	/* expire prefix list */
986 	LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) {
987 		/*
988 		 * check prefix lifetime.
989 		 * since pltime is just for autoconf, pltime processing for
990 		 * prefix is not necessary.
991 		 */
992 		if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
993 		    time_uptime - pr->ndpr_lastupdate > pr->ndpr_vltime) {
994 
995 			/*
996 			 * address expiration and prefix expiration are
997 			 * separate.  NEVER perform in6_purgeaddr here.
998 			 */
999 			prelist_remove(pr);
1000 		}
1001 	}
1002 	CURVNET_RESTORE();
1003 }
1004 
1005 /*
1006  * ia6 - deprecated/invalidated temporary address
1007  */
1008 static int
1009 regen_tmpaddr(struct in6_ifaddr *ia6)
1010 {
1011 	struct ifaddr *ifa;
1012 	struct ifnet *ifp;
1013 	struct in6_ifaddr *public_ifa6 = NULL;
1014 
1015 	ifp = ia6->ia_ifa.ifa_ifp;
1016 	IF_ADDR_RLOCK(ifp);
1017 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1018 		struct in6_ifaddr *it6;
1019 
1020 		if (ifa->ifa_addr->sa_family != AF_INET6)
1021 			continue;
1022 
1023 		it6 = (struct in6_ifaddr *)ifa;
1024 
1025 		/* ignore no autoconf addresses. */
1026 		if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1027 			continue;
1028 
1029 		/* ignore autoconf addresses with different prefixes. */
1030 		if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
1031 			continue;
1032 
1033 		/*
1034 		 * Now we are looking at an autoconf address with the same
1035 		 * prefix as ours.  If the address is temporary and is still
1036 		 * preferred, do not create another one.  It would be rare, but
1037 		 * could happen, for example, when we resume a laptop PC after
1038 		 * a long period.
1039 		 */
1040 		if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1041 		    !IFA6_IS_DEPRECATED(it6)) {
1042 			public_ifa6 = NULL;
1043 			break;
1044 		}
1045 
1046 		/*
1047 		 * This is a public autoconf address that has the same prefix
1048 		 * as ours.  If it is preferred, keep it.  We can't break the
1049 		 * loop here, because there may be a still-preferred temporary
1050 		 * address with the prefix.
1051 		 */
1052 		if (!IFA6_IS_DEPRECATED(it6))
1053 			public_ifa6 = it6;
1054 	}
1055 	if (public_ifa6 != NULL)
1056 		ifa_ref(&public_ifa6->ia_ifa);
1057 	IF_ADDR_RUNLOCK(ifp);
1058 
1059 	if (public_ifa6 != NULL) {
1060 		int e;
1061 
1062 		if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
1063 			ifa_free(&public_ifa6->ia_ifa);
1064 			log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
1065 			    " tmp addr,errno=%d\n", e);
1066 			return (-1);
1067 		}
1068 		ifa_free(&public_ifa6->ia_ifa);
1069 		return (0);
1070 	}
1071 
1072 	return (-1);
1073 }
1074 
1075 /*
1076  * Nuke neighbor cache/prefix/default router management table, right before
1077  * ifp goes away.
1078  */
1079 void
1080 nd6_purge(struct ifnet *ifp)
1081 {
1082 	struct nd_defrouter *dr, *ndr;
1083 	struct nd_prefix *pr, *npr;
1084 
1085 	/*
1086 	 * Nuke default router list entries toward ifp.
1087 	 * We defer removal of default router list entries that is installed
1088 	 * in the routing table, in order to keep additional side effects as
1089 	 * small as possible.
1090 	 */
1091 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
1092 		if (dr->installed)
1093 			continue;
1094 
1095 		if (dr->ifp == ifp)
1096 			defrtrlist_del(dr);
1097 	}
1098 
1099 	TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) {
1100 		if (!dr->installed)
1101 			continue;
1102 
1103 		if (dr->ifp == ifp)
1104 			defrtrlist_del(dr);
1105 	}
1106 
1107 	/* Nuke prefix list entries toward ifp */
1108 	LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) {
1109 		if (pr->ndpr_ifp == ifp) {
1110 			/*
1111 			 * Because if_detach() does *not* release prefixes
1112 			 * while purging addresses the reference count will
1113 			 * still be above zero. We therefore reset it to
1114 			 * make sure that the prefix really gets purged.
1115 			 */
1116 			pr->ndpr_refcnt = 0;
1117 
1118 			/*
1119 			 * Previously, pr->ndpr_addr is removed as well,
1120 			 * but I strongly believe we don't have to do it.
1121 			 * nd6_purge() is only called from in6_ifdetach(),
1122 			 * which removes all the associated interface addresses
1123 			 * by itself.
1124 			 * (jinmei@kame.net 20010129)
1125 			 */
1126 			prelist_remove(pr);
1127 		}
1128 	}
1129 
1130 	/* cancel default outgoing interface setting */
1131 	if (V_nd6_defifindex == ifp->if_index)
1132 		nd6_setdefaultiface(0);
1133 
1134 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1135 		/* Refresh default router list. */
1136 		defrouter_select();
1137 	}
1138 
1139 	/* XXXXX
1140 	 * We do not nuke the neighbor cache entries here any more
1141 	 * because the neighbor cache is kept in if_afdata[AF_INET6].
1142 	 * nd6_purge() is invoked by in6_ifdetach() which is called
1143 	 * from if_detach() where everything gets purged. So let
1144 	 * in6_domifdetach() do the actual L2 table purging work.
1145 	 */
1146 }
1147 
1148 /*
1149  * the caller acquires and releases the lock on the lltbls
1150  * Returns the llentry locked
1151  */
1152 struct llentry *
1153 nd6_lookup(const struct in6_addr *addr6, int flags, struct ifnet *ifp)
1154 {
1155 	struct sockaddr_in6 sin6;
1156 	struct llentry *ln;
1157 
1158 	bzero(&sin6, sizeof(sin6));
1159 	sin6.sin6_len = sizeof(struct sockaddr_in6);
1160 	sin6.sin6_family = AF_INET6;
1161 	sin6.sin6_addr = *addr6;
1162 
1163 	IF_AFDATA_LOCK_ASSERT(ifp);
1164 
1165 	ln = lla_lookup(LLTABLE6(ifp), flags, (struct sockaddr *)&sin6);
1166 
1167 	return (ln);
1168 }
1169 
1170 struct llentry *
1171 nd6_alloc(const struct in6_addr *addr6, int flags, struct ifnet *ifp)
1172 {
1173 	struct sockaddr_in6 sin6;
1174 	struct llentry *ln;
1175 
1176 	bzero(&sin6, sizeof(sin6));
1177 	sin6.sin6_len = sizeof(struct sockaddr_in6);
1178 	sin6.sin6_family = AF_INET6;
1179 	sin6.sin6_addr = *addr6;
1180 
1181 	ln = lltable_alloc_entry(LLTABLE6(ifp), 0, (struct sockaddr *)&sin6);
1182 	if (ln != NULL)
1183 		ln->ln_state = ND6_LLINFO_NOSTATE;
1184 
1185 	return (ln);
1186 }
1187 
1188 /*
1189  * Test whether a given IPv6 address is a neighbor or not, ignoring
1190  * the actual neighbor cache.  The neighbor cache is ignored in order
1191  * to not reenter the routing code from within itself.
1192  */
1193 static int
1194 nd6_is_new_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
1195 {
1196 	struct nd_prefix *pr;
1197 	struct ifaddr *dstaddr;
1198 
1199 	/*
1200 	 * A link-local address is always a neighbor.
1201 	 * XXX: a link does not necessarily specify a single interface.
1202 	 */
1203 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
1204 		struct sockaddr_in6 sin6_copy;
1205 		u_int32_t zone;
1206 
1207 		/*
1208 		 * We need sin6_copy since sa6_recoverscope() may modify the
1209 		 * content (XXX).
1210 		 */
1211 		sin6_copy = *addr;
1212 		if (sa6_recoverscope(&sin6_copy))
1213 			return (0); /* XXX: should be impossible */
1214 		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
1215 			return (0);
1216 		if (sin6_copy.sin6_scope_id == zone)
1217 			return (1);
1218 		else
1219 			return (0);
1220 	}
1221 
1222 	/*
1223 	 * If the address matches one of our addresses,
1224 	 * it should be a neighbor.
1225 	 * If the address matches one of our on-link prefixes, it should be a
1226 	 * neighbor.
1227 	 */
1228 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1229 		if (pr->ndpr_ifp != ifp)
1230 			continue;
1231 
1232 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) {
1233 			struct rtentry *rt;
1234 
1235 			/* Always use the default FIB here. */
1236 			rt = in6_rtalloc1((struct sockaddr *)&pr->ndpr_prefix,
1237 			    0, 0, RT_DEFAULT_FIB);
1238 			if (rt == NULL)
1239 				continue;
1240 			/*
1241 			 * This is the case where multiple interfaces
1242 			 * have the same prefix, but only one is installed
1243 			 * into the routing table and that prefix entry
1244 			 * is not the one being examined here. In the case
1245 			 * where RADIX_MPATH is enabled, multiple route
1246 			 * entries (of the same rt_key value) will be
1247 			 * installed because the interface addresses all
1248 			 * differ.
1249 			 */
1250 			if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1251 			       &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr)) {
1252 				RTFREE_LOCKED(rt);
1253 				continue;
1254 			}
1255 			RTFREE_LOCKED(rt);
1256 		}
1257 
1258 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1259 		    &addr->sin6_addr, &pr->ndpr_mask))
1260 			return (1);
1261 	}
1262 
1263 	/*
1264 	 * If the address is assigned on the node of the other side of
1265 	 * a p2p interface, the address should be a neighbor.
1266 	 */
1267 	dstaddr = ifa_ifwithdstaddr((const struct sockaddr *)addr, RT_ALL_FIBS);
1268 	if (dstaddr != NULL) {
1269 		if (dstaddr->ifa_ifp == ifp) {
1270 			ifa_free(dstaddr);
1271 			return (1);
1272 		}
1273 		ifa_free(dstaddr);
1274 	}
1275 
1276 	/*
1277 	 * If the default router list is empty, all addresses are regarded
1278 	 * as on-link, and thus, as a neighbor.
1279 	 */
1280 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV &&
1281 	    TAILQ_EMPTY(&V_nd_defrouter) &&
1282 	    V_nd6_defifindex == ifp->if_index) {
1283 		return (1);
1284 	}
1285 
1286 	return (0);
1287 }
1288 
1289 
1290 /*
1291  * Detect if a given IPv6 address identifies a neighbor on a given link.
1292  * XXX: should take care of the destination of a p2p link?
1293  */
1294 int
1295 nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
1296 {
1297 	struct llentry *lle;
1298 	int rc = 0;
1299 
1300 	IF_AFDATA_UNLOCK_ASSERT(ifp);
1301 	if (nd6_is_new_addr_neighbor(addr, ifp))
1302 		return (1);
1303 
1304 	/*
1305 	 * Even if the address matches none of our addresses, it might be
1306 	 * in the neighbor cache.
1307 	 */
1308 	IF_AFDATA_RLOCK(ifp);
1309 	if ((lle = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL) {
1310 		LLE_RUNLOCK(lle);
1311 		rc = 1;
1312 	}
1313 	IF_AFDATA_RUNLOCK(ifp);
1314 	return (rc);
1315 }
1316 
1317 /*
1318  * Free an nd6 llinfo entry.
1319  * Since the function would cause significant changes in the kernel, DO NOT
1320  * make it global, unless you have a strong reason for the change, and are sure
1321  * that the change is safe.
1322  *
1323  * Set noinline to be dtrace-friendly
1324  */
1325 static __noinline void
1326 nd6_free(struct llentry *ln, int gc)
1327 {
1328 	struct nd_defrouter *dr;
1329 	struct ifnet *ifp;
1330 
1331 	LLE_WLOCK_ASSERT(ln);
1332 
1333 	/*
1334 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1335 	 * even though it is not harmful, it was not really necessary.
1336 	 */
1337 
1338 	/* cancel timer */
1339 	nd6_llinfo_settimer_locked(ln, -1);
1340 
1341 	ifp = ln->lle_tbl->llt_ifp;
1342 
1343 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1344 		dr = defrouter_lookup(&ln->r_l3addr.addr6, ifp);
1345 
1346 		if (dr != NULL && dr->expire &&
1347 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
1348 			/*
1349 			 * If the reason for the deletion is just garbage
1350 			 * collection, and the neighbor is an active default
1351 			 * router, do not delete it.  Instead, reset the GC
1352 			 * timer using the router's lifetime.
1353 			 * Simply deleting the entry would affect default
1354 			 * router selection, which is not necessarily a good
1355 			 * thing, especially when we're using router preference
1356 			 * values.
1357 			 * XXX: the check for ln_state would be redundant,
1358 			 *      but we intentionally keep it just in case.
1359 			 */
1360 			if (dr->expire > time_uptime)
1361 				nd6_llinfo_settimer_locked(ln,
1362 				    (dr->expire - time_uptime) * hz);
1363 			else
1364 				nd6_llinfo_settimer_locked(ln,
1365 				    (long)V_nd6_gctimer * hz);
1366 
1367 			LLE_REMREF(ln);
1368 			LLE_WUNLOCK(ln);
1369 			return;
1370 		}
1371 
1372 		if (dr) {
1373 			/*
1374 			 * Unreachablity of a router might affect the default
1375 			 * router selection and on-link detection of advertised
1376 			 * prefixes.
1377 			 */
1378 
1379 			/*
1380 			 * Temporarily fake the state to choose a new default
1381 			 * router and to perform on-link determination of
1382 			 * prefixes correctly.
1383 			 * Below the state will be set correctly,
1384 			 * or the entry itself will be deleted.
1385 			 */
1386 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
1387 		}
1388 
1389 		if (ln->ln_router || dr) {
1390 
1391 			/*
1392 			 * We need to unlock to avoid a LOR with rt6_flush() with the
1393 			 * rnh and for the calls to pfxlist_onlink_check() and
1394 			 * defrouter_select() in the block further down for calls
1395 			 * into nd6_lookup().  We still hold a ref.
1396 			 */
1397 			LLE_WUNLOCK(ln);
1398 
1399 			/*
1400 			 * rt6_flush must be called whether or not the neighbor
1401 			 * is in the Default Router List.
1402 			 * See a corresponding comment in nd6_na_input().
1403 			 */
1404 			rt6_flush(&ln->r_l3addr.addr6, ifp);
1405 		}
1406 
1407 		if (dr) {
1408 			/*
1409 			 * Since defrouter_select() does not affect the
1410 			 * on-link determination and MIP6 needs the check
1411 			 * before the default router selection, we perform
1412 			 * the check now.
1413 			 */
1414 			pfxlist_onlink_check();
1415 
1416 			/*
1417 			 * Refresh default router list.
1418 			 */
1419 			defrouter_select();
1420 		}
1421 
1422 		/*
1423 		 * If this entry was added by an on-link redirect, remove the
1424 		 * corresponding host route.
1425 		 */
1426 		if (ln->la_flags & LLE_REDIRECT)
1427 			nd6_free_redirect(ln);
1428 
1429 		if (ln->ln_router || dr)
1430 			LLE_WLOCK(ln);
1431 	}
1432 
1433 	/*
1434 	 * Save to unlock. We still hold an extra reference and will not
1435 	 * free(9) in llentry_free() if someone else holds one as well.
1436 	 */
1437 	LLE_WUNLOCK(ln);
1438 	IF_AFDATA_LOCK(ifp);
1439 	LLE_WLOCK(ln);
1440 	/* Guard against race with other llentry_free(). */
1441 	if (ln->la_flags & LLE_LINKED) {
1442 		/* Remove callout reference */
1443 		LLE_REMREF(ln);
1444 		lltable_unlink_entry(ln->lle_tbl, ln);
1445 	}
1446 	IF_AFDATA_UNLOCK(ifp);
1447 
1448 	llentry_free(ln);
1449 }
1450 
1451 static int
1452 nd6_isdynrte(const struct rtentry *rt, void *xap)
1453 {
1454 
1455 	if (rt->rt_flags == (RTF_UP | RTF_HOST | RTF_DYNAMIC))
1456 		return (1);
1457 
1458 	return (0);
1459 }
1460 /*
1461  * Remove the rtentry for the given llentry,
1462  * both of which were installed by a redirect.
1463  */
1464 static void
1465 nd6_free_redirect(const struct llentry *ln)
1466 {
1467 	int fibnum;
1468 	struct sockaddr_in6 sin6;
1469 	struct rt_addrinfo info;
1470 
1471 	lltable_fill_sa_entry(ln, (struct sockaddr *)&sin6);
1472 	memset(&info, 0, sizeof(info));
1473 	info.rti_info[RTAX_DST] = (struct sockaddr *)&sin6;
1474 	info.rti_filter = nd6_isdynrte;
1475 
1476 	for (fibnum = 0; fibnum < rt_numfibs; fibnum++)
1477 		rtrequest1_fib(RTM_DELETE, &info, NULL, fibnum);
1478 }
1479 
1480 /*
1481  * Rejuvenate this function for routing operations related
1482  * processing.
1483  */
1484 void
1485 nd6_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
1486 {
1487 	struct sockaddr_in6 *gateway;
1488 	struct nd_defrouter *dr;
1489 	struct ifnet *ifp;
1490 
1491 	gateway = (struct sockaddr_in6 *)rt->rt_gateway;
1492 	ifp = rt->rt_ifp;
1493 
1494 	switch (req) {
1495 	case RTM_ADD:
1496 		break;
1497 
1498 	case RTM_DELETE:
1499 		if (!ifp)
1500 			return;
1501 		/*
1502 		 * Only indirect routes are interesting.
1503 		 */
1504 		if ((rt->rt_flags & RTF_GATEWAY) == 0)
1505 			return;
1506 		/*
1507 		 * check for default route
1508 		 */
1509 		if (IN6_ARE_ADDR_EQUAL(&in6addr_any,
1510 				       &SIN6(rt_key(rt))->sin6_addr)) {
1511 
1512 			dr = defrouter_lookup(&gateway->sin6_addr, ifp);
1513 			if (dr != NULL)
1514 				dr->installed = 0;
1515 		}
1516 		break;
1517 	}
1518 }
1519 
1520 
1521 int
1522 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1523 {
1524 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1525 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1526 	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1527 	int error = 0;
1528 
1529 	if (ifp->if_afdata[AF_INET6] == NULL)
1530 		return (EPFNOSUPPORT);
1531 	switch (cmd) {
1532 	case OSIOCGIFINFO_IN6:
1533 #define ND	ndi->ndi
1534 		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
1535 		bzero(&ND, sizeof(ND));
1536 		ND.linkmtu = IN6_LINKMTU(ifp);
1537 		ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1538 		ND.basereachable = ND_IFINFO(ifp)->basereachable;
1539 		ND.reachable = ND_IFINFO(ifp)->reachable;
1540 		ND.retrans = ND_IFINFO(ifp)->retrans;
1541 		ND.flags = ND_IFINFO(ifp)->flags;
1542 		ND.recalctm = ND_IFINFO(ifp)->recalctm;
1543 		ND.chlim = ND_IFINFO(ifp)->chlim;
1544 		break;
1545 	case SIOCGIFINFO_IN6:
1546 		ND = *ND_IFINFO(ifp);
1547 		break;
1548 	case SIOCSIFINFO_IN6:
1549 		/*
1550 		 * used to change host variables from userland.
1551 		 * intented for a use on router to reflect RA configurations.
1552 		 */
1553 		/* 0 means 'unspecified' */
1554 		if (ND.linkmtu != 0) {
1555 			if (ND.linkmtu < IPV6_MMTU ||
1556 			    ND.linkmtu > IN6_LINKMTU(ifp)) {
1557 				error = EINVAL;
1558 				break;
1559 			}
1560 			ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1561 		}
1562 
1563 		if (ND.basereachable != 0) {
1564 			int obasereachable = ND_IFINFO(ifp)->basereachable;
1565 
1566 			ND_IFINFO(ifp)->basereachable = ND.basereachable;
1567 			if (ND.basereachable != obasereachable)
1568 				ND_IFINFO(ifp)->reachable =
1569 				    ND_COMPUTE_RTIME(ND.basereachable);
1570 		}
1571 		if (ND.retrans != 0)
1572 			ND_IFINFO(ifp)->retrans = ND.retrans;
1573 		if (ND.chlim != 0)
1574 			ND_IFINFO(ifp)->chlim = ND.chlim;
1575 		/* FALLTHROUGH */
1576 	case SIOCSIFINFO_FLAGS:
1577 	{
1578 		struct ifaddr *ifa;
1579 		struct in6_ifaddr *ia;
1580 
1581 		if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1582 		    !(ND.flags & ND6_IFF_IFDISABLED)) {
1583 			/* ifdisabled 1->0 transision */
1584 
1585 			/*
1586 			 * If the interface is marked as ND6_IFF_IFDISABLED and
1587 			 * has an link-local address with IN6_IFF_DUPLICATED,
1588 			 * do not clear ND6_IFF_IFDISABLED.
1589 			 * See RFC 4862, Section 5.4.5.
1590 			 */
1591 			IF_ADDR_RLOCK(ifp);
1592 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1593 				if (ifa->ifa_addr->sa_family != AF_INET6)
1594 					continue;
1595 				ia = (struct in6_ifaddr *)ifa;
1596 				if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
1597 				    IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
1598 					break;
1599 			}
1600 			IF_ADDR_RUNLOCK(ifp);
1601 
1602 			if (ifa != NULL) {
1603 				/* LLA is duplicated. */
1604 				ND.flags |= ND6_IFF_IFDISABLED;
1605 				log(LOG_ERR, "Cannot enable an interface"
1606 				    " with a link-local address marked"
1607 				    " duplicate.\n");
1608 			} else {
1609 				ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED;
1610 				if (ifp->if_flags & IFF_UP)
1611 					in6_if_up(ifp);
1612 			}
1613 		} else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
1614 			    (ND.flags & ND6_IFF_IFDISABLED)) {
1615 			/* ifdisabled 0->1 transision */
1616 			/* Mark all IPv6 address as tentative. */
1617 
1618 			ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1619 			if (V_ip6_dad_count > 0 &&
1620 			    (ND_IFINFO(ifp)->flags & ND6_IFF_NO_DAD) == 0) {
1621 				IF_ADDR_RLOCK(ifp);
1622 				TAILQ_FOREACH(ifa, &ifp->if_addrhead,
1623 				    ifa_link) {
1624 					if (ifa->ifa_addr->sa_family !=
1625 					    AF_INET6)
1626 						continue;
1627 					ia = (struct in6_ifaddr *)ifa;
1628 					ia->ia6_flags |= IN6_IFF_TENTATIVE;
1629 				}
1630 				IF_ADDR_RUNLOCK(ifp);
1631 			}
1632 		}
1633 
1634 		if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) {
1635 			if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) {
1636 				/* auto_linklocal 0->1 transision */
1637 
1638 				/* If no link-local address on ifp, configure */
1639 				ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL;
1640 				in6_ifattach(ifp, NULL);
1641 			} else if (!(ND.flags & ND6_IFF_IFDISABLED) &&
1642 			    ifp->if_flags & IFF_UP) {
1643 				/*
1644 				 * When the IF already has
1645 				 * ND6_IFF_AUTO_LINKLOCAL, no link-local
1646 				 * address is assigned, and IFF_UP, try to
1647 				 * assign one.
1648 				 */
1649 				IF_ADDR_RLOCK(ifp);
1650 				TAILQ_FOREACH(ifa, &ifp->if_addrhead,
1651 				    ifa_link) {
1652 					if (ifa->ifa_addr->sa_family !=
1653 					    AF_INET6)
1654 						continue;
1655 					ia = (struct in6_ifaddr *)ifa;
1656 					if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
1657 						break;
1658 				}
1659 				IF_ADDR_RUNLOCK(ifp);
1660 				if (ifa != NULL)
1661 					/* No LLA is configured. */
1662 					in6_ifattach(ifp, NULL);
1663 			}
1664 		}
1665 	}
1666 		ND_IFINFO(ifp)->flags = ND.flags;
1667 		break;
1668 #undef ND
1669 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1670 		/* sync kernel routing table with the default router list */
1671 		defrouter_reset();
1672 		defrouter_select();
1673 		break;
1674 	case SIOCSPFXFLUSH_IN6:
1675 	{
1676 		/* flush all the prefix advertised by routers */
1677 		struct nd_prefix *pr, *next;
1678 
1679 		LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, next) {
1680 			struct in6_ifaddr *ia, *ia_next;
1681 
1682 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1683 				continue; /* XXX */
1684 
1685 			/* do we really have to remove addresses as well? */
1686 			/* XXXRW: in6_ifaddrhead locking. */
1687 			TAILQ_FOREACH_SAFE(ia, &V_in6_ifaddrhead, ia_link,
1688 			    ia_next) {
1689 				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1690 					continue;
1691 
1692 				if (ia->ia6_ndpr == pr)
1693 					in6_purgeaddr(&ia->ia_ifa);
1694 			}
1695 			prelist_remove(pr);
1696 		}
1697 		break;
1698 	}
1699 	case SIOCSRTRFLUSH_IN6:
1700 	{
1701 		/* flush all the default routers */
1702 		struct nd_defrouter *dr, *next;
1703 
1704 		defrouter_reset();
1705 		TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, next) {
1706 			defrtrlist_del(dr);
1707 		}
1708 		defrouter_select();
1709 		break;
1710 	}
1711 	case SIOCGNBRINFO_IN6:
1712 	{
1713 		struct llentry *ln;
1714 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1715 
1716 		if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1717 			return (error);
1718 
1719 		IF_AFDATA_RLOCK(ifp);
1720 		ln = nd6_lookup(&nb_addr, 0, ifp);
1721 		IF_AFDATA_RUNLOCK(ifp);
1722 
1723 		if (ln == NULL) {
1724 			error = EINVAL;
1725 			break;
1726 		}
1727 		nbi->state = ln->ln_state;
1728 		nbi->asked = ln->la_asked;
1729 		nbi->isrouter = ln->ln_router;
1730 		if (ln->la_expire == 0)
1731 			nbi->expire = 0;
1732 		else
1733 			nbi->expire = ln->la_expire + ln->lle_remtime / hz +
1734 			    (time_second - time_uptime);
1735 		LLE_RUNLOCK(ln);
1736 		break;
1737 	}
1738 	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1739 		ndif->ifindex = V_nd6_defifindex;
1740 		break;
1741 	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1742 		return (nd6_setdefaultiface(ndif->ifindex));
1743 	}
1744 	return (error);
1745 }
1746 
1747 /*
1748  * Calculates new isRouter value based on provided parameters and
1749  * returns it.
1750  */
1751 static int
1752 nd6_is_router(int type, int code, int is_new, int old_addr, int new_addr,
1753     int ln_router)
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 	 *   is_new  old_addr new_addr 	    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	(4)	c   s     s
1779 	 *	0	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_new)					/* (6-7) */
1791 			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_router = 1;
1801 		else {
1802 			if (is_new)				/* (6-7) */
1803 				ln_router = 0;
1804 		}
1805 		break;
1806 	case ND_ROUTER_SOLICIT:
1807 		/*
1808 		 * is_router flag must always be cleared.
1809 		 */
1810 		ln_router = 0;
1811 		break;
1812 	case ND_ROUTER_ADVERT:
1813 		/*
1814 		 * Mark an entry with lladdr as a router.
1815 		 */
1816 		if ((!is_new && (old_addr || new_addr)) ||	/* (2-5) */
1817 		    (is_new && new_addr)) {			/* (7) */
1818 			ln_router = 1;
1819 		}
1820 		break;
1821 	}
1822 
1823 	return (ln_router);
1824 }
1825 
1826 /*
1827  * Create neighbor cache entry and cache link-layer address,
1828  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1829  *
1830  * type - ICMP6 type
1831  * code - type dependent information
1832  *
1833  */
1834 void
1835 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1836     int lladdrlen, int type, int code)
1837 {
1838 	struct llentry *ln = NULL, *ln_tmp;
1839 	int is_newentry;
1840 	int do_update;
1841 	int olladdr;
1842 	int llchange;
1843 	int flags;
1844 	uint16_t router = 0;
1845 	struct sockaddr_in6 sin6;
1846 	struct mbuf *chain = NULL;
1847 
1848 	IF_AFDATA_UNLOCK_ASSERT(ifp);
1849 
1850 	KASSERT(ifp != NULL, ("%s: ifp == NULL", __func__));
1851 	KASSERT(from != NULL, ("%s: from == NULL", __func__));
1852 
1853 	/* nothing must be updated for unspecified address */
1854 	if (IN6_IS_ADDR_UNSPECIFIED(from))
1855 		return;
1856 
1857 	/*
1858 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1859 	 * the caller.
1860 	 *
1861 	 * XXX If the link does not have link-layer adderss, what should
1862 	 * we do? (ifp->if_addrlen == 0)
1863 	 * Spec says nothing in sections for RA, RS and NA.  There's small
1864 	 * description on it in NS section (RFC 2461 7.2.3).
1865 	 */
1866 	flags = lladdr ? LLE_EXCLUSIVE : 0;
1867 	IF_AFDATA_RLOCK(ifp);
1868 	ln = nd6_lookup(from, flags, ifp);
1869 	IF_AFDATA_RUNLOCK(ifp);
1870 	is_newentry = 0;
1871 	if (ln == NULL) {
1872 		flags |= LLE_EXCLUSIVE;
1873 		ln = nd6_alloc(from, 0, ifp);
1874 		if (ln == NULL)
1875 			return;
1876 
1877 		/*
1878 		 * Since we already know all the data for the new entry,
1879 		 * fill it before insertion.
1880 		 */
1881 		if (lladdr != NULL)
1882 			lltable_set_entry_addr(ifp, ln, lladdr);
1883 		IF_AFDATA_WLOCK(ifp);
1884 		LLE_WLOCK(ln);
1885 		/* Prefer any existing lle over newly-created one */
1886 		ln_tmp = nd6_lookup(from, LLE_EXCLUSIVE, ifp);
1887 		if (ln_tmp == NULL)
1888 			lltable_link_entry(LLTABLE6(ifp), ln);
1889 		IF_AFDATA_WUNLOCK(ifp);
1890 		if (ln_tmp == NULL) {
1891 			/* No existing lle, mark as new entry (6,7) */
1892 			is_newentry = 1;
1893 			nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
1894 			if (lladdr != NULL)	/* (7) */
1895 				EVENTHANDLER_INVOKE(lle_event, ln,
1896 				    LLENTRY_RESOLVED);
1897 		} else {
1898 			lltable_free_entry(LLTABLE6(ifp), ln);
1899 			ln = ln_tmp;
1900 			ln_tmp = NULL;
1901 		}
1902 	}
1903 	/* do nothing if static ndp is set */
1904 	if ((ln->la_flags & LLE_STATIC)) {
1905 		if (flags & LLE_EXCLUSIVE)
1906 			LLE_WUNLOCK(ln);
1907 		else
1908 			LLE_RUNLOCK(ln);
1909 		return;
1910 	}
1911 
1912 	olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
1913 	if (olladdr && lladdr) {
1914 		llchange = bcmp(lladdr, &ln->ll_addr,
1915 		    ifp->if_addrlen);
1916 	} else if (!olladdr && lladdr)
1917 		llchange = 1;
1918 	else
1919 		llchange = 0;
1920 
1921 	/*
1922 	 * newentry olladdr  lladdr  llchange	(*=record)
1923 	 *	0	n	n	--	(1)
1924 	 *	0	y	n	--	(2)
1925 	 *	0	n	y	y	(3) * STALE
1926 	 *	0	y	y	n	(4) *
1927 	 *	0	y	y	y	(5) * STALE
1928 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1929 	 *	1	--	y	--	(7) * STALE
1930 	 */
1931 
1932 	do_update = 0;
1933 	if (is_newentry == 0 && llchange != 0) {
1934 		do_update = 1;	/* (3,5) */
1935 
1936 		/*
1937 		 * Record source link-layer address
1938 		 * XXX is it dependent to ifp->if_type?
1939 		 */
1940 		if (lltable_try_set_entry_addr(ifp, ln, lladdr) == 0) {
1941 			/* Entry was deleted */
1942 			return;
1943 		}
1944 
1945 		nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
1946 
1947 		EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
1948 
1949 		if (ln->la_hold != NULL)
1950 			nd6_grab_holdchain(ln, &chain, &sin6);
1951 	}
1952 
1953 	/* Calculates new router status */
1954 	router = nd6_is_router(type, code, is_newentry, olladdr,
1955 	    lladdr != NULL ? 1 : 0, ln->ln_router);
1956 
1957 	ln->ln_router = router;
1958 	/* Mark non-router redirects with special flag */
1959 	if ((type & 0xFF) == ND_REDIRECT && code != ND_REDIRECT_ROUTER)
1960 		ln->la_flags |= LLE_REDIRECT;
1961 
1962 	if (flags & LLE_EXCLUSIVE)
1963 		LLE_WUNLOCK(ln);
1964 	else
1965 		LLE_RUNLOCK(ln);
1966 
1967 	if (chain != NULL)
1968 		nd6_flush_holdchain(ifp, ifp, chain, &sin6);
1969 
1970 	/*
1971 	 * When the link-layer address of a router changes, select the
1972 	 * best router again.  In particular, when the neighbor entry is newly
1973 	 * created, it might affect the selection policy.
1974 	 * Question: can we restrict the first condition to the "is_newentry"
1975 	 * case?
1976 	 * XXX: when we hear an RA from a new router with the link-layer
1977 	 * address option, defrouter_select() is called twice, since
1978 	 * defrtrlist_update called the function as well.  However, I believe
1979 	 * we can compromise the overhead, since it only happens the first
1980 	 * time.
1981 	 * XXX: although defrouter_select() should not have a bad effect
1982 	 * for those are not autoconfigured hosts, we explicitly avoid such
1983 	 * cases for safety.
1984 	 */
1985 	if ((do_update || is_newentry) && router &&
1986 	    ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) {
1987 		/*
1988 		 * guaranteed recursion
1989 		 */
1990 		defrouter_select();
1991 	}
1992 }
1993 
1994 static void
1995 nd6_slowtimo(void *arg)
1996 {
1997 	CURVNET_SET((struct vnet *) arg);
1998 	struct nd_ifinfo *nd6if;
1999 	struct ifnet *ifp;
2000 
2001 	callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
2002 	    nd6_slowtimo, curvnet);
2003 	IFNET_RLOCK_NOSLEEP();
2004 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2005 		if (ifp->if_afdata[AF_INET6] == NULL)
2006 			continue;
2007 		nd6if = ND_IFINFO(ifp);
2008 		if (nd6if->basereachable && /* already initialized */
2009 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
2010 			/*
2011 			 * Since reachable time rarely changes by router
2012 			 * advertisements, we SHOULD insure that a new random
2013 			 * value gets recomputed at least once every few hours.
2014 			 * (RFC 2461, 6.3.4)
2015 			 */
2016 			nd6if->recalctm = V_nd6_recalc_reachtm_interval;
2017 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
2018 		}
2019 	}
2020 	IFNET_RUNLOCK_NOSLEEP();
2021 	CURVNET_RESTORE();
2022 }
2023 
2024 void
2025 nd6_grab_holdchain(struct llentry *ln, struct mbuf **chain,
2026     struct sockaddr_in6 *sin6)
2027 {
2028 
2029 	LLE_WLOCK_ASSERT(ln);
2030 
2031 	*chain = ln->la_hold;
2032 	ln->la_hold = NULL;
2033 	lltable_fill_sa_entry(ln, (struct sockaddr *)sin6);
2034 
2035 	if (ln->ln_state == ND6_LLINFO_STALE) {
2036 
2037 		/*
2038 		 * The first time we send a packet to a
2039 		 * neighbor whose entry is STALE, we have
2040 		 * to change the state to DELAY and a sets
2041 		 * a timer to expire in DELAY_FIRST_PROBE_TIME
2042 		 * seconds to ensure do neighbor unreachability
2043 		 * detection on expiration.
2044 		 * (RFC 2461 7.3.3)
2045 		 */
2046 		nd6_llinfo_setstate(ln, ND6_LLINFO_DELAY);
2047 	}
2048 }
2049 
2050 int
2051 nd6_output_ifp(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
2052     struct sockaddr_in6 *dst, struct route *ro)
2053 {
2054 	int error;
2055 	int ip6len;
2056 	struct ip6_hdr *ip6;
2057 	struct m_tag *mtag;
2058 
2059 #ifdef MAC
2060 	mac_netinet6_nd6_send(ifp, m);
2061 #endif
2062 
2063 	/*
2064 	 * If called from nd6_ns_output() (NS), nd6_na_output() (NA),
2065 	 * icmp6_redirect_output() (REDIRECT) or from rip6_output() (RS, RA
2066 	 * as handled by rtsol and rtadvd), mbufs will be tagged for SeND
2067 	 * to be diverted to user space.  When re-injected into the kernel,
2068 	 * send_output() will directly dispatch them to the outgoing interface.
2069 	 */
2070 	if (send_sendso_input_hook != NULL) {
2071 		mtag = m_tag_find(m, PACKET_TAG_ND_OUTGOING, NULL);
2072 		if (mtag != NULL) {
2073 			ip6 = mtod(m, struct ip6_hdr *);
2074 			ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
2075 			/* Use the SEND socket */
2076 			error = send_sendso_input_hook(m, ifp, SND_OUT,
2077 			    ip6len);
2078 			/* -1 == no app on SEND socket */
2079 			if (error == 0 || error != -1)
2080 			    return (error);
2081 		}
2082 	}
2083 
2084 	m_clrprotoflags(m);	/* Avoid confusing lower layers. */
2085 	IP_PROBE(send, NULL, NULL, mtod(m, struct ip6_hdr *), ifp, NULL,
2086 	    mtod(m, struct ip6_hdr *));
2087 
2088 	if ((ifp->if_flags & IFF_LOOPBACK) == 0)
2089 		origifp = ifp;
2090 
2091 	error = (*ifp->if_output)(origifp, m, (struct sockaddr *)dst, ro);
2092 	return (error);
2093 }
2094 
2095 /*
2096  * Do L2 address resolution for @sa_dst address. Stores found
2097  * address in @desten buffer. Copy of lle ln_flags can be also
2098  * saved in @pflags if @pflags is non-NULL.
2099  *
2100  * If destination LLE does not exists or lle state modification
2101  * is required, call "slow" version.
2102  *
2103  * Return values:
2104  * - 0 on success (address copied to buffer).
2105  * - EWOULDBLOCK (no local error, but address is still unresolved)
2106  * - other errors (alloc failure, etc)
2107  */
2108 int
2109 nd6_resolve(struct ifnet *ifp, int is_gw, struct mbuf *m,
2110     const struct sockaddr *sa_dst, u_char *desten, uint32_t *pflags)
2111 {
2112 	struct llentry *ln = NULL;
2113 	const struct sockaddr_in6 *dst6;
2114 
2115 	if (pflags != NULL)
2116 		*pflags = 0;
2117 
2118 	dst6 = (const struct sockaddr_in6 *)sa_dst;
2119 
2120 	/* discard the packet if IPv6 operation is disabled on the interface */
2121 	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
2122 		m_freem(m);
2123 		return (ENETDOWN); /* better error? */
2124 	}
2125 
2126 	if (m != NULL && m->m_flags & M_MCAST) {
2127 		switch (ifp->if_type) {
2128 		case IFT_ETHER:
2129 		case IFT_FDDI:
2130 		case IFT_L2VLAN:
2131 		case IFT_IEEE80211:
2132 		case IFT_BRIDGE:
2133 		case IFT_ISO88025:
2134 			ETHER_MAP_IPV6_MULTICAST(&dst6->sin6_addr,
2135 						 desten);
2136 			return (0);
2137 		default:
2138 			m_freem(m);
2139 			return (EAFNOSUPPORT);
2140 		}
2141 	}
2142 
2143 	IF_AFDATA_RLOCK(ifp);
2144 	ln = nd6_lookup(&dst6->sin6_addr, LLE_UNLOCKED, ifp);
2145 	if (ln != NULL && (ln->r_flags & RLLE_VALID) != 0) {
2146 		/* Entry found, let's copy lle info */
2147 		bcopy(&ln->ll_addr, desten, ifp->if_addrlen);
2148 		if (pflags != NULL)
2149 			*pflags = LLE_VALID | (ln->r_flags & RLLE_IFADDR);
2150 		/* Check if we have feedback request from nd6 timer */
2151 		if (ln->r_skip_req != 0) {
2152 			LLE_REQ_LOCK(ln);
2153 			ln->r_skip_req = 0; /* Notify that entry was used */
2154 			ln->lle_hittime = time_uptime;
2155 			LLE_REQ_UNLOCK(ln);
2156 		}
2157 		IF_AFDATA_RUNLOCK(ifp);
2158 		return (0);
2159 	}
2160 	IF_AFDATA_RUNLOCK(ifp);
2161 
2162 	return (nd6_resolve_slow(ifp, m, dst6, desten, pflags));
2163 }
2164 
2165 
2166 /*
2167  * Do L2 address resolution for @sa_dst address. Stores found
2168  * address in @desten buffer. Copy of lle ln_flags can be also
2169  * saved in @pflags if @pflags is non-NULL.
2170  *
2171  * Heavy version.
2172  * Function assume that destination LLE does not exist,
2173  * is invalid or stale, so LLE_EXCLUSIVE lock needs to be acquired.
2174  *
2175  * Set noinline to be dtrace-friendly
2176  */
2177 static __noinline int
2178 nd6_resolve_slow(struct ifnet *ifp, struct mbuf *m,
2179     const struct sockaddr_in6 *dst, u_char *desten, uint32_t *pflags)
2180 {
2181 	struct llentry *lle = NULL, *lle_tmp;
2182 	struct in6_addr *psrc, src;
2183 	int send_ns;
2184 
2185 	/*
2186 	 * Address resolution or Neighbor Unreachability Detection
2187 	 * for the next hop.
2188 	 * At this point, the destination of the packet must be a unicast
2189 	 * or an anycast address(i.e. not a multicast).
2190 	 */
2191 	if (lle == NULL) {
2192 		IF_AFDATA_RLOCK(ifp);
2193 		lle = nd6_lookup(&dst->sin6_addr, LLE_EXCLUSIVE, ifp);
2194 		IF_AFDATA_RUNLOCK(ifp);
2195 		if ((lle == NULL) && nd6_is_addr_neighbor(dst, ifp))  {
2196 			/*
2197 			 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2198 			 * the condition below is not very efficient.  But we believe
2199 			 * it is tolerable, because this should be a rare case.
2200 			 */
2201 			lle = nd6_alloc(&dst->sin6_addr, 0, ifp);
2202 			if (lle == NULL) {
2203 				char ip6buf[INET6_ADDRSTRLEN];
2204 				log(LOG_DEBUG,
2205 				    "nd6_output: can't allocate llinfo for %s "
2206 				    "(ln=%p)\n",
2207 				    ip6_sprintf(ip6buf, &dst->sin6_addr), lle);
2208 				m_freem(m);
2209 				return (ENOBUFS);
2210 			}
2211 
2212 			IF_AFDATA_WLOCK(ifp);
2213 			LLE_WLOCK(lle);
2214 			/* Prefer any existing entry over newly-created one */
2215 			lle_tmp = nd6_lookup(&dst->sin6_addr, LLE_EXCLUSIVE, ifp);
2216 			if (lle_tmp == NULL)
2217 				lltable_link_entry(LLTABLE6(ifp), lle);
2218 			IF_AFDATA_WUNLOCK(ifp);
2219 			if (lle_tmp != NULL) {
2220 				lltable_free_entry(LLTABLE6(ifp), lle);
2221 				lle = lle_tmp;
2222 				lle_tmp = NULL;
2223 			}
2224 		}
2225 	}
2226 	if (lle == NULL) {
2227 		if (!(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
2228 			m_freem(m);
2229 			return (ENOBUFS);
2230 		}
2231 
2232 		if (m != NULL)
2233 			m_freem(m);
2234 		return (ENOBUFS);
2235 	}
2236 
2237 	LLE_WLOCK_ASSERT(lle);
2238 
2239 	/*
2240 	 * The first time we send a packet to a neighbor whose entry is
2241 	 * STALE, we have to change the state to DELAY and a sets a timer to
2242 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2243 	 * neighbor unreachability detection on expiration.
2244 	 * (RFC 2461 7.3.3)
2245 	 */
2246 	if (lle->ln_state == ND6_LLINFO_STALE)
2247 		nd6_llinfo_setstate(lle, ND6_LLINFO_DELAY);
2248 
2249 	/*
2250 	 * If the neighbor cache entry has a state other than INCOMPLETE
2251 	 * (i.e. its link-layer address is already resolved), just
2252 	 * send the packet.
2253 	 */
2254 	if (lle->ln_state > ND6_LLINFO_INCOMPLETE) {
2255 		bcopy(&lle->ll_addr, desten, ifp->if_addrlen);
2256 		if (pflags != NULL)
2257 			*pflags = lle->la_flags;
2258 		LLE_WUNLOCK(lle);
2259 		return (0);
2260 	}
2261 
2262 	/*
2263 	 * There is a neighbor cache entry, but no ethernet address
2264 	 * response yet.  Append this latest packet to the end of the
2265 	 * packet queue in the mbuf, unless the number of the packet
2266 	 * does not exceed nd6_maxqueuelen.  When it exceeds nd6_maxqueuelen,
2267 	 * the oldest packet in the queue will be removed.
2268 	 */
2269 
2270 	if (lle->la_hold != NULL) {
2271 		struct mbuf *m_hold;
2272 		int i;
2273 
2274 		i = 0;
2275 		for (m_hold = lle->la_hold; m_hold; m_hold = m_hold->m_nextpkt){
2276 			i++;
2277 			if (m_hold->m_nextpkt == NULL) {
2278 				m_hold->m_nextpkt = m;
2279 				break;
2280 			}
2281 		}
2282 		while (i >= V_nd6_maxqueuelen) {
2283 			m_hold = lle->la_hold;
2284 			lle->la_hold = lle->la_hold->m_nextpkt;
2285 			m_freem(m_hold);
2286 			i--;
2287 		}
2288 	} else {
2289 		lle->la_hold = m;
2290 	}
2291 
2292 	/*
2293 	 * If there has been no NS for the neighbor after entering the
2294 	 * INCOMPLETE state, send the first solicitation.
2295 	 * Note that for newly-created lle la_asked will be 0,
2296 	 * so we will transition from ND6_LLINFO_NOSTATE to
2297 	 * ND6_LLINFO_INCOMPLETE state here.
2298 	 */
2299 	psrc = NULL;
2300 	send_ns = 0;
2301 	if (lle->la_asked == 0) {
2302 		lle->la_asked++;
2303 		send_ns = 1;
2304 		psrc = nd6_llinfo_get_holdsrc(lle, &src);
2305 
2306 		nd6_llinfo_setstate(lle, ND6_LLINFO_INCOMPLETE);
2307 	}
2308 	LLE_WUNLOCK(lle);
2309 	if (send_ns != 0)
2310 		nd6_ns_output(ifp, psrc, NULL, &dst->sin6_addr, NULL);
2311 
2312 	return (EWOULDBLOCK);
2313 }
2314 
2315 
2316 int
2317 nd6_flush_holdchain(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *chain,
2318     struct sockaddr_in6 *dst)
2319 {
2320 	struct mbuf *m, *m_head;
2321 	struct ifnet *outifp;
2322 	int error = 0;
2323 
2324 	m_head = chain;
2325 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2326 		outifp = origifp;
2327 	else
2328 		outifp = ifp;
2329 
2330 	while (m_head) {
2331 		m = m_head;
2332 		m_head = m_head->m_nextpkt;
2333 		error = nd6_output_ifp(ifp, origifp, m, dst, NULL);
2334 	}
2335 
2336 	/*
2337 	 * XXX
2338 	 * note that intermediate errors are blindly ignored
2339 	 */
2340 	return (error);
2341 }
2342 
2343 static int
2344 nd6_need_cache(struct ifnet *ifp)
2345 {
2346 	/*
2347 	 * XXX: we currently do not make neighbor cache on any interface
2348 	 * other than ARCnet, Ethernet, FDDI and GIF.
2349 	 *
2350 	 * RFC2893 says:
2351 	 * - unidirectional tunnels needs no ND
2352 	 */
2353 	switch (ifp->if_type) {
2354 	case IFT_ARCNET:
2355 	case IFT_ETHER:
2356 	case IFT_FDDI:
2357 	case IFT_IEEE1394:
2358 	case IFT_L2VLAN:
2359 	case IFT_IEEE80211:
2360 	case IFT_INFINIBAND:
2361 	case IFT_BRIDGE:
2362 	case IFT_PROPVIRTUAL:
2363 		return (1);
2364 	default:
2365 		return (0);
2366 	}
2367 }
2368 
2369 /*
2370  * Add pernament ND6 link-layer record for given
2371  * interface address.
2372  *
2373  * Very similar to IPv4 arp_ifinit(), but:
2374  * 1) IPv6 DAD is performed in different place
2375  * 2) It is called by IPv6 protocol stack in contrast to
2376  * arp_ifinit() which is typically called in SIOCSIFADDR
2377  * driver ioctl handler.
2378  *
2379  */
2380 int
2381 nd6_add_ifa_lle(struct in6_ifaddr *ia)
2382 {
2383 	struct ifnet *ifp;
2384 	struct llentry *ln, *ln_tmp;
2385 	struct sockaddr *dst;
2386 
2387 	ifp = ia->ia_ifa.ifa_ifp;
2388 	if (nd6_need_cache(ifp) == 0)
2389 		return (0);
2390 
2391 	ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
2392 	dst = (struct sockaddr *)&ia->ia_addr;
2393 	ln = lltable_alloc_entry(LLTABLE6(ifp), LLE_IFADDR, dst);
2394 	if (ln == NULL)
2395 		return (ENOBUFS);
2396 
2397 	IF_AFDATA_WLOCK(ifp);
2398 	LLE_WLOCK(ln);
2399 	/* Unlink any entry if exists */
2400 	ln_tmp = lla_lookup(LLTABLE6(ifp), LLE_EXCLUSIVE, dst);
2401 	if (ln_tmp != NULL)
2402 		lltable_unlink_entry(LLTABLE6(ifp), ln_tmp);
2403 	lltable_link_entry(LLTABLE6(ifp), ln);
2404 	IF_AFDATA_WUNLOCK(ifp);
2405 
2406 	if (ln_tmp != NULL)
2407 		EVENTHANDLER_INVOKE(lle_event, ln_tmp, LLENTRY_EXPIRED);
2408 	EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
2409 
2410 	LLE_WUNLOCK(ln);
2411 	if (ln_tmp != NULL)
2412 		llentry_free(ln_tmp);
2413 
2414 	return (0);
2415 }
2416 
2417 /*
2418  * Removes either all lle entries for given @ia, or lle
2419  * corresponding to @ia address.
2420  */
2421 void
2422 nd6_rem_ifa_lle(struct in6_ifaddr *ia, int all)
2423 {
2424 	struct sockaddr_in6 mask, addr;
2425 	struct sockaddr *saddr, *smask;
2426 	struct ifnet *ifp;
2427 
2428 	ifp = ia->ia_ifa.ifa_ifp;
2429 	memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
2430 	memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
2431 	saddr = (struct sockaddr *)&addr;
2432 	smask = (struct sockaddr *)&mask;
2433 
2434 	if (all != 0)
2435 		lltable_prefix_free(AF_INET6, saddr, smask, LLE_STATIC);
2436 	else
2437 		lltable_delete_addr(LLTABLE6(ifp), LLE_IFADDR, saddr);
2438 }
2439 
2440 static void
2441 clear_llinfo_pqueue(struct llentry *ln)
2442 {
2443 	struct mbuf *m_hold, *m_hold_next;
2444 
2445 	for (m_hold = ln->la_hold; m_hold; m_hold = m_hold_next) {
2446 		m_hold_next = m_hold->m_nextpkt;
2447 		m_freem(m_hold);
2448 	}
2449 
2450 	ln->la_hold = NULL;
2451 	return;
2452 }
2453 
2454 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2455 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2456 #ifdef SYSCTL_DECL
2457 SYSCTL_DECL(_net_inet6_icmp6);
2458 #endif
2459 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2460 	CTLFLAG_RD, nd6_sysctl_drlist, "");
2461 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2462 	CTLFLAG_RD, nd6_sysctl_prlist, "");
2463 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, nd6_maxqueuelen,
2464 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_maxqueuelen), 1, "");
2465 SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_gctimer,
2466 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_gctimer), (60 * 60 * 24), "");
2467 
2468 static int
2469 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2470 {
2471 	struct in6_defrouter d;
2472 	struct nd_defrouter *dr;
2473 	int error;
2474 
2475 	if (req->newptr)
2476 		return (EPERM);
2477 
2478 	bzero(&d, sizeof(d));
2479 	d.rtaddr.sin6_family = AF_INET6;
2480 	d.rtaddr.sin6_len = sizeof(d.rtaddr);
2481 
2482 	/*
2483 	 * XXX locking
2484 	 */
2485 	TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) {
2486 		d.rtaddr.sin6_addr = dr->rtaddr;
2487 		error = sa6_recoverscope(&d.rtaddr);
2488 		if (error != 0)
2489 			return (error);
2490 		d.flags = dr->flags;
2491 		d.rtlifetime = dr->rtlifetime;
2492 		d.expire = dr->expire + (time_second - time_uptime);
2493 		d.if_index = dr->ifp->if_index;
2494 		error = SYSCTL_OUT(req, &d, sizeof(d));
2495 		if (error != 0)
2496 			return (error);
2497 	}
2498 	return (0);
2499 }
2500 
2501 static int
2502 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2503 {
2504 	struct in6_prefix p;
2505 	struct sockaddr_in6 s6;
2506 	struct nd_prefix *pr;
2507 	struct nd_pfxrouter *pfr;
2508 	time_t maxexpire;
2509 	int error;
2510 	char ip6buf[INET6_ADDRSTRLEN];
2511 
2512 	if (req->newptr)
2513 		return (EPERM);
2514 
2515 	bzero(&p, sizeof(p));
2516 	p.origin = PR_ORIG_RA;
2517 	bzero(&s6, sizeof(s6));
2518 	s6.sin6_family = AF_INET6;
2519 	s6.sin6_len = sizeof(s6);
2520 
2521 	/*
2522 	 * XXX locking
2523 	 */
2524 	LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
2525 		p.prefix = pr->ndpr_prefix;
2526 		if (sa6_recoverscope(&p.prefix)) {
2527 			log(LOG_ERR, "scope error in prefix list (%s)\n",
2528 			    ip6_sprintf(ip6buf, &p.prefix.sin6_addr));
2529 			/* XXX: press on... */
2530 		}
2531 		p.raflags = pr->ndpr_raf;
2532 		p.prefixlen = pr->ndpr_plen;
2533 		p.vltime = pr->ndpr_vltime;
2534 		p.pltime = pr->ndpr_pltime;
2535 		p.if_index = pr->ndpr_ifp->if_index;
2536 		if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2537 			p.expire = 0;
2538 		else {
2539 			/* XXX: we assume time_t is signed. */
2540 			maxexpire = (-1) &
2541 			    ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
2542 			if (pr->ndpr_vltime < maxexpire - pr->ndpr_lastupdate)
2543 				p.expire = pr->ndpr_lastupdate +
2544 				    pr->ndpr_vltime +
2545 				    (time_second - time_uptime);
2546 			else
2547 				p.expire = maxexpire;
2548 		}
2549 		p.refcnt = pr->ndpr_refcnt;
2550 		p.flags = pr->ndpr_stateflags;
2551 		p.advrtrs = 0;
2552 		LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
2553 			p.advrtrs++;
2554 		error = SYSCTL_OUT(req, &p, sizeof(p));
2555 		if (error != 0)
2556 			return (error);
2557 		LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2558 			s6.sin6_addr = pfr->router->rtaddr;
2559 			if (sa6_recoverscope(&s6))
2560 				log(LOG_ERR,
2561 				    "scope error in prefix list (%s)\n",
2562 				    ip6_sprintf(ip6buf, &pfr->router->rtaddr));
2563 			error = SYSCTL_OUT(req, &s6, sizeof(s6));
2564 			if (error != 0)
2565 				return (error);
2566 		}
2567 	}
2568 	return (0);
2569 }
2570