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