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