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