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