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