Home | History | Annotate | Line # | Download | only in netinet6
nd6.c revision 1.271
      1  1.271       roy /*	$NetBSD: nd6.c,v 1.271 2020/06/12 11:04:45 roy Exp $	*/
      2   1.65    itojun /*	$KAME: nd6.c,v 1.279 2002/06/08 11:16:51 itojun Exp $	*/
      3    1.4   thorpej 
      4    1.2    itojun /*
      5    1.2    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6    1.2    itojun  * All rights reserved.
      7   1.25    itojun  *
      8    1.2    itojun  * Redistribution and use in source and binary forms, with or without
      9    1.2    itojun  * modification, are permitted provided that the following conditions
     10    1.2    itojun  * are met:
     11    1.2    itojun  * 1. Redistributions of source code must retain the above copyright
     12    1.2    itojun  *    notice, this list of conditions and the following disclaimer.
     13    1.2    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14    1.2    itojun  *    notice, this list of conditions and the following disclaimer in the
     15    1.2    itojun  *    documentation and/or other materials provided with the distribution.
     16    1.2    itojun  * 3. Neither the name of the project nor the names of its contributors
     17    1.2    itojun  *    may be used to endorse or promote products derived from this software
     18    1.2    itojun  *    without specific prior written permission.
     19   1.25    itojun  *
     20    1.2    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21    1.2    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22    1.2    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23    1.2    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24    1.2    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25    1.2    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26    1.2    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27    1.2    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28    1.2    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29    1.2    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30    1.2    itojun  * SUCH DAMAGE.
     31    1.2    itojun  */
     32    1.2    itojun 
     33   1.55     lukem #include <sys/cdefs.h>
     34  1.271       roy __KERNEL_RCSID(0, "$NetBSD: nd6.c,v 1.271 2020/06/12 11:04:45 roy Exp $");
     35  1.162     ozaki 
     36  1.169     pooka #ifdef _KERNEL_OPT
     37  1.271       roy #include "opt_compat_netbsd.h"
     38  1.162     ozaki #include "opt_net_mpsafe.h"
     39  1.169     pooka #endif
     40   1.87    itojun 
     41  1.153       roy #include "bridge.h"
     42  1.153       roy #include "carp.h"
     43    1.2    itojun 
     44    1.2    itojun #include <sys/param.h>
     45    1.2    itojun #include <sys/systm.h>
     46   1.20   thorpej #include <sys/callout.h>
     47  1.229     ozaki #include <sys/kmem.h>
     48    1.2    itojun #include <sys/mbuf.h>
     49    1.2    itojun #include <sys/socket.h>
     50  1.126        ad #include <sys/socketvar.h>
     51    1.2    itojun #include <sys/sockio.h>
     52    1.2    itojun #include <sys/time.h>
     53    1.2    itojun #include <sys/kernel.h>
     54    1.2    itojun #include <sys/errno.h>
     55    1.2    itojun #include <sys/ioctl.h>
     56    1.2    itojun #include <sys/syslog.h>
     57    1.2    itojun #include <sys/queue.h>
     58  1.138       tls #include <sys/cprng.h>
     59  1.203     ozaki #include <sys/workqueue.h>
     60    1.2    itojun 
     61    1.2    itojun #include <net/if.h>
     62    1.2    itojun #include <net/if_dl.h>
     63  1.181     ozaki #include <net/if_llatbl.h>
     64    1.2    itojun #include <net/if_types.h>
     65    1.2    itojun #include <net/route.h>
     66   1.62    itojun #include <net/if_ether.h>
     67   1.62    itojun #include <net/if_arc.h>
     68    1.2    itojun 
     69    1.2    itojun #include <netinet/in.h>
     70    1.2    itojun #include <netinet6/in6_var.h>
     71   1.17    itojun #include <netinet/ip6.h>
     72    1.2    itojun #include <netinet6/ip6_var.h>
     73   1.96    rpaulo #include <netinet6/scope6_var.h>
     74    1.2    itojun #include <netinet6/nd6.h>
     75  1.151       roy #include <netinet6/in6_ifattach.h>
     76   1.17    itojun #include <netinet/icmp6.h>
     77  1.125   thorpej #include <netinet6/icmp6_private.h>
     78   1.87    itojun 
     79  1.271       roy #ifdef COMPAT_90
     80  1.271       roy #include <compat/netinet6/in6_var.h>
     81  1.271       roy #include <compat/netinet6/nd6.h>
     82  1.271       roy #endif
     83  1.271       roy 
     84    1.2    itojun #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
     85    1.2    itojun #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
     86    1.2    itojun 
     87    1.2    itojun /* timer values */
     88    1.2    itojun int	nd6_prune	= 1;	/* walk list every 1 seconds */
     89    1.2    itojun int	nd6_delay	= 5;	/* delay first probe time 5 second */
     90    1.2    itojun int	nd6_umaxtries	= 3;	/* maximum unicast query */
     91    1.2    itojun int	nd6_mmaxtries	= 3;	/* maximum multicast query */
     92    1.2    itojun int	nd6_useloopback = 1;	/* use loopback interface for local traffic */
     93   1.42    itojun int	nd6_gctimer	= (60 * 60 * 24); /* 1 day: garbage collection timer */
     94    1.2    itojun 
     95   1.12    itojun /* preventing too many loops in ND option parsing */
     96   1.12    itojun int nd6_maxndopt = 10;	/* max # of ND options allowed */
     97   1.12    itojun 
     98   1.31    itojun int nd6_maxnudhint = 0;	/* max # of subsequent upper layer hints */
     99   1.31    itojun 
    100   1.99    rpaulo int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */
    101   1.99    rpaulo 
    102   1.36    itojun #ifdef ND6_DEBUG
    103   1.36    itojun int nd6_debug = 1;
    104   1.36    itojun #else
    105   1.36    itojun int nd6_debug = 0;
    106   1.36    itojun #endif
    107   1.36    itojun 
    108  1.220     ozaki krwlock_t nd6_lock __cacheline_aligned;
    109  1.220     ozaki 
    110    1.2    itojun int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
    111    1.2    itojun 
    112  1.121    dyoung static void nd6_slowtimo(void *);
    113  1.188     ozaki static void nd6_free(struct llentry *, int);
    114  1.121    dyoung static void nd6_llinfo_timer(void *);
    115  1.186     ozaki static void nd6_timer(void *);
    116  1.203     ozaki static void nd6_timer_work(struct work *, void *);
    117  1.181     ozaki static void clear_llinfo_pqueue(struct llentry *);
    118  1.217     ozaki static struct nd_opt_hdr *nd6_option(union nd_opts *);
    119    1.2    itojun 
    120  1.186     ozaki static callout_t nd6_slowtimo_ch;
    121  1.186     ozaki static callout_t nd6_timer_ch;
    122  1.203     ozaki static struct workqueue	*nd6_timer_wq;
    123  1.203     ozaki static struct work	nd6_timer_wk;
    124   1.20   thorpej 
    125   1.79   thorpej MALLOC_DEFINE(M_IP6NDP, "NDP", "IPv6 Neighbour Discovery");
    126   1.65    itojun 
    127    1.2    itojun void
    128  1.112    dyoung nd6_init(void)
    129    1.2    itojun {
    130  1.203     ozaki 	int error;
    131   1.12    itojun 
    132  1.264     ozaki 	nd6_nbr_init();
    133  1.264     ozaki 
    134  1.220     ozaki 	rw_init(&nd6_lock);
    135  1.220     ozaki 
    136  1.126        ad 	callout_init(&nd6_slowtimo_ch, CALLOUT_MPSAFE);
    137  1.126        ad 	callout_init(&nd6_timer_ch, CALLOUT_MPSAFE);
    138  1.116        ad 
    139  1.203     ozaki 	error = workqueue_create(&nd6_timer_wq, "nd6_timer",
    140  1.203     ozaki 	    nd6_timer_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
    141  1.203     ozaki 	if (error)
    142  1.203     ozaki 		panic("%s: workqueue_create failed (%d)\n", __func__, error);
    143  1.203     ozaki 
    144    1.2    itojun 	/* start timer */
    145   1.20   thorpej 	callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
    146   1.20   thorpej 	    nd6_slowtimo, NULL);
    147  1.186     ozaki 	callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
    148    1.2    itojun }
    149    1.2    itojun 
    150  1.271       roy struct nd_kifinfo *
    151  1.112    dyoung nd6_ifattach(struct ifnet *ifp)
    152    1.2    itojun {
    153  1.271       roy 	struct nd_kifinfo *nd;
    154    1.2    itojun 
    155  1.229     ozaki 	nd = kmem_zalloc(sizeof(*nd), KM_SLEEP);
    156    1.2    itojun 
    157   1.58    itojun 	nd->chlim = IPV6_DEFHLIM;
    158   1.58    itojun 	nd->basereachable = REACHABLE_TIME;
    159   1.58    itojun 	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
    160   1.58    itojun 	nd->retrans = RETRANS_TIMER;
    161  1.151       roy 
    162  1.271       roy 	nd->flags = ND6_IFF_PERFORMNUD;
    163   1.61    itojun 
    164  1.151       roy 	/* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL.
    165  1.151       roy 	 * A bridge interface should not have ND6_IFF_AUTO_LINKLOCAL
    166  1.154       snj 	 * because one of its members should. */
    167  1.151       roy 	if ((ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) ||
    168  1.151       roy 	    (ifp->if_flags & IFF_LOOPBACK))
    169  1.151       roy 		nd->flags |= ND6_IFF_AUTO_LINKLOCAL;
    170  1.151       roy 
    171   1.58    itojun 	return nd;
    172   1.58    itojun }
    173   1.21    itojun 
    174   1.58    itojun void
    175  1.158    martin nd6_ifdetach(struct ifnet *ifp, struct in6_ifextra *ext)
    176   1.58    itojun {
    177   1.21    itojun 
    178  1.219     ozaki 	/* Ensure all IPv6 addresses are purged before calling nd6_purge */
    179  1.219     ozaki 	if_purgeaddrs(ifp, AF_INET6, in6_purgeaddr);
    180  1.158    martin 	nd6_purge(ifp, ext);
    181  1.271       roy 	kmem_free(ext->nd_ifinfo, sizeof(struct nd_kifinfo));
    182    1.2    itojun }
    183    1.2    itojun 
    184    1.2    itojun void
    185  1.112    dyoung nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
    186    1.2    itojun {
    187   1.58    itojun 
    188  1.129    dyoung 	memset(ndopts, 0, sizeof(*ndopts));
    189    1.2    itojun 	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
    190    1.2    itojun 	ndopts->nd_opts_last
    191    1.2    itojun 		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
    192    1.2    itojun 
    193    1.2    itojun 	if (icmp6len == 0) {
    194    1.2    itojun 		ndopts->nd_opts_done = 1;
    195    1.2    itojun 		ndopts->nd_opts_search = NULL;
    196    1.2    itojun 	}
    197    1.2    itojun }
    198    1.2    itojun 
    199    1.2    itojun /*
    200    1.2    itojun  * Take one ND option.
    201    1.2    itojun  */
    202  1.217     ozaki static struct nd_opt_hdr *
    203  1.112    dyoung nd6_option(union nd_opts *ndopts)
    204    1.2    itojun {
    205    1.2    itojun 	struct nd_opt_hdr *nd_opt;
    206    1.2    itojun 	int olen;
    207    1.2    itojun 
    208  1.163     ozaki 	KASSERT(ndopts != NULL);
    209  1.163     ozaki 	KASSERT(ndopts->nd_opts_last != NULL);
    210  1.163     ozaki 
    211   1.99    rpaulo 	if (ndopts->nd_opts_search == NULL)
    212    1.2    itojun 		return NULL;
    213    1.2    itojun 	if (ndopts->nd_opts_done)
    214    1.2    itojun 		return NULL;
    215    1.2    itojun 
    216    1.2    itojun 	nd_opt = ndopts->nd_opts_search;
    217    1.2    itojun 
    218   1.40    itojun 	/* make sure nd_opt_len is inside the buffer */
    219  1.111  christos 	if ((void *)&nd_opt->nd_opt_len >= (void *)ndopts->nd_opts_last) {
    220  1.129    dyoung 		memset(ndopts, 0, sizeof(*ndopts));
    221   1.40    itojun 		return NULL;
    222   1.40    itojun 	}
    223   1.40    itojun 
    224    1.2    itojun 	olen = nd_opt->nd_opt_len << 3;
    225    1.2    itojun 	if (olen == 0) {
    226    1.2    itojun 		/*
    227    1.2    itojun 		 * Message validation requires that all included
    228    1.2    itojun 		 * options have a length that is greater than zero.
    229    1.2    itojun 		 */
    230  1.129    dyoung 		memset(ndopts, 0, sizeof(*ndopts));
    231    1.2    itojun 		return NULL;
    232    1.2    itojun 	}
    233    1.2    itojun 
    234  1.111  christos 	ndopts->nd_opts_search = (struct nd_opt_hdr *)((char *)nd_opt + olen);
    235   1.40    itojun 	if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
    236   1.40    itojun 		/* option overruns the end of buffer, invalid */
    237  1.129    dyoung 		memset(ndopts, 0, sizeof(*ndopts));
    238   1.40    itojun 		return NULL;
    239   1.40    itojun 	} else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
    240   1.40    itojun 		/* reached the end of options chain */
    241    1.2    itojun 		ndopts->nd_opts_done = 1;
    242    1.2    itojun 		ndopts->nd_opts_search = NULL;
    243    1.2    itojun 	}
    244    1.2    itojun 	return nd_opt;
    245    1.2    itojun }
    246    1.2    itojun 
    247    1.2    itojun /*
    248    1.2    itojun  * Parse multiple ND options.
    249    1.2    itojun  * This function is much easier to use, for ND routines that do not need
    250    1.2    itojun  * multiple options of the same type.
    251    1.2    itojun  */
    252    1.2    itojun int
    253  1.112    dyoung nd6_options(union nd_opts *ndopts)
    254    1.2    itojun {
    255    1.2    itojun 	struct nd_opt_hdr *nd_opt;
    256    1.2    itojun 	int i = 0;
    257    1.2    itojun 
    258  1.163     ozaki 	KASSERT(ndopts != NULL);
    259  1.163     ozaki 	KASSERT(ndopts->nd_opts_last != NULL);
    260  1.163     ozaki 
    261   1.99    rpaulo 	if (ndopts->nd_opts_search == NULL)
    262    1.2    itojun 		return 0;
    263   1.99    rpaulo 
    264    1.2    itojun 	while (1) {
    265    1.2    itojun 		nd_opt = nd6_option(ndopts);
    266   1.99    rpaulo 		if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
    267    1.2    itojun 			/*
    268    1.2    itojun 			 * Message validation requires that all included
    269    1.2    itojun 			 * options have a length that is greater than zero.
    270    1.2    itojun 			 */
    271  1.125   thorpej 			ICMP6_STATINC(ICMP6_STAT_ND_BADOPT);
    272  1.129    dyoung 			memset(ndopts, 0, sizeof(*ndopts));
    273    1.2    itojun 			return -1;
    274    1.2    itojun 		}
    275    1.2    itojun 
    276   1.99    rpaulo 		if (nd_opt == NULL)
    277    1.2    itojun 			goto skip1;
    278    1.2    itojun 
    279    1.2    itojun 		switch (nd_opt->nd_opt_type) {
    280    1.2    itojun 		case ND_OPT_SOURCE_LINKADDR:
    281    1.2    itojun 		case ND_OPT_TARGET_LINKADDR:
    282    1.2    itojun 		case ND_OPT_MTU:
    283    1.2    itojun 		case ND_OPT_REDIRECTED_HEADER:
    284  1.247       roy 		case ND_OPT_NONCE:
    285    1.2    itojun 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
    286  1.187     ozaki 				nd6log(LOG_INFO,
    287   1.36    itojun 				    "duplicated ND6 option found (type=%d)\n",
    288  1.187     ozaki 				    nd_opt->nd_opt_type);
    289    1.2    itojun 				/* XXX bark? */
    290    1.2    itojun 			} else {
    291    1.2    itojun 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
    292    1.2    itojun 					= nd_opt;
    293    1.2    itojun 			}
    294    1.2    itojun 			break;
    295    1.2    itojun 		case ND_OPT_PREFIX_INFORMATION:
    296    1.2    itojun 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
    297    1.2    itojun 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
    298    1.2    itojun 					= nd_opt;
    299    1.2    itojun 			}
    300    1.2    itojun 			ndopts->nd_opts_pi_end =
    301    1.2    itojun 				(struct nd_opt_prefix_info *)nd_opt;
    302    1.2    itojun 			break;
    303    1.2    itojun 		default:
    304    1.2    itojun 			/*
    305    1.2    itojun 			 * Unknown options must be silently ignored,
    306  1.109  christos 			 * to accommodate future extension to the protocol.
    307    1.2    itojun 			 */
    308  1.187     ozaki 			nd6log(LOG_DEBUG,
    309    1.2    itojun 			    "nd6_options: unsupported option %d - "
    310  1.187     ozaki 			    "option ignored\n", nd_opt->nd_opt_type);
    311    1.2    itojun 		}
    312    1.2    itojun 
    313    1.2    itojun skip1:
    314    1.2    itojun 		i++;
    315   1.12    itojun 		if (i > nd6_maxndopt) {
    316  1.125   thorpej 			ICMP6_STATINC(ICMP6_STAT_ND_TOOMANYOPT);
    317  1.187     ozaki 			nd6log(LOG_INFO, "too many loop in nd opt\n");
    318    1.2    itojun 			break;
    319    1.2    itojun 		}
    320    1.2    itojun 
    321    1.2    itojun 		if (ndopts->nd_opts_done)
    322    1.2    itojun 			break;
    323    1.2    itojun 	}
    324    1.2    itojun 
    325    1.2    itojun 	return 0;
    326    1.2    itojun }
    327    1.2    itojun 
    328    1.2    itojun /*
    329   1.86    itojun  * ND6 timer routine to handle ND6 entries
    330    1.2    itojun  */
    331    1.2    itojun void
    332  1.188     ozaki nd6_llinfo_settimer(struct llentry *ln, time_t xtick)
    333   1.86    itojun {
    334   1.86    itojun 
    335  1.182     ozaki 	CTASSERT(sizeof(time_t) > sizeof(int));
    336  1.181     ozaki 	LLE_WLOCK_ASSERT(ln);
    337   1.86    itojun 
    338  1.222     ozaki 	KASSERT(xtick >= 0);
    339  1.222     ozaki 
    340  1.246     ozaki 	/*
    341  1.246     ozaki 	 * We have to take care of a reference leak which occurs if
    342  1.246     ozaki 	 * callout_reset overwrites a pending callout schedule.  Unfortunately
    343  1.246     ozaki 	 * we don't have a mean to know the overwrite, so we need to know it
    344  1.246     ozaki 	 * using callout_stop.  We need to call callout_pending first to exclude
    345  1.246     ozaki 	 * the case that the callout has never been scheduled.
    346  1.246     ozaki 	 */
    347  1.246     ozaki 	if (callout_pending(&ln->la_timer)) {
    348  1.246     ozaki 		bool expired = callout_stop(&ln->la_timer);
    349  1.246     ozaki 		if (!expired)
    350  1.246     ozaki 			LLE_REMREF(ln);
    351  1.246     ozaki 	}
    352  1.246     ozaki 
    353  1.222     ozaki 	ln->ln_expire = time_uptime + xtick / hz;
    354  1.222     ozaki 	LLE_ADDREF(ln);
    355  1.222     ozaki 	if (xtick > INT_MAX) {
    356  1.222     ozaki 		ln->ln_ntick = xtick - INT_MAX;
    357  1.222     ozaki 		callout_reset(&ln->ln_timer_ch, INT_MAX,
    358  1.222     ozaki 		    nd6_llinfo_timer, ln);
    359  1.222     ozaki 	} else {
    360   1.86    itojun 		ln->ln_ntick = 0;
    361  1.222     ozaki 		callout_reset(&ln->ln_timer_ch, xtick,
    362  1.222     ozaki 		    nd6_llinfo_timer, ln);
    363   1.86    itojun 	}
    364  1.181     ozaki }
    365  1.181     ozaki 
    366  1.179     ozaki /*
    367  1.179     ozaki  * Gets source address of the first packet in hold queue
    368  1.179     ozaki  * and stores it in @src.
    369  1.179     ozaki  * Returns pointer to @src (if hold queue is not empty) or NULL.
    370  1.179     ozaki  */
    371  1.179     ozaki static struct in6_addr *
    372  1.181     ozaki nd6_llinfo_get_holdsrc(struct llentry *ln, struct in6_addr *src)
    373  1.179     ozaki {
    374  1.179     ozaki 	struct ip6_hdr *hip6;
    375  1.179     ozaki 
    376  1.179     ozaki 	if (ln == NULL || ln->ln_hold == NULL)
    377  1.179     ozaki 		return NULL;
    378  1.179     ozaki 
    379  1.179     ozaki 	/*
    380  1.179     ozaki 	 * assuming every packet in ln_hold has the same IP header
    381  1.179     ozaki 	 */
    382  1.179     ozaki 	hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
    383  1.179     ozaki 	/* XXX pullup? */
    384  1.179     ozaki 	if (sizeof(*hip6) < ln->ln_hold->m_len)
    385  1.179     ozaki 		*src = hip6->ip6_src;
    386  1.179     ozaki 	else
    387  1.179     ozaki 		src = NULL;
    388  1.179     ozaki 
    389  1.179     ozaki 	return src;
    390  1.179     ozaki }
    391  1.179     ozaki 
    392   1.86    itojun static void
    393  1.112    dyoung nd6_llinfo_timer(void *arg)
    394    1.2    itojun {
    395  1.181     ozaki 	struct llentry *ln = arg;
    396   1.65    itojun 	struct ifnet *ifp;
    397  1.271       roy 	struct nd_kifinfo *ndi;
    398  1.178     ozaki 	bool send_ns = false;
    399  1.178     ozaki 	const struct in6_addr *daddr6 = NULL;
    400  1.262       roy 	const struct in6_addr *taddr6 = &ln->r_l3addr.addr6;
    401  1.267       roy 	struct sockaddr_in6 dsin6, tsin6;
    402  1.268  christos 	struct mbuf *m = NULL;
    403  1.269       roy 	bool missed = false;
    404   1.63    itojun 
    405  1.239     ozaki 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
    406  1.246     ozaki 
    407  1.181     ozaki 	LLE_WLOCK(ln);
    408  1.222     ozaki 	if ((ln->la_flags & LLE_LINKED) == 0)
    409  1.222     ozaki 		goto out;
    410  1.181     ozaki 	if (ln->ln_ntick > 0) {
    411  1.188     ozaki 		nd6_llinfo_settimer(ln, ln->ln_ntick);
    412  1.181     ozaki 		goto out;
    413  1.181     ozaki 	}
    414   1.86    itojun 
    415  1.181     ozaki 	ifp = ln->lle_tbl->llt_ifp;
    416  1.173     ozaki 	KASSERT(ifp != NULL);
    417  1.173     ozaki 
    418   1.86    itojun 	ndi = ND_IFINFO(ifp);
    419   1.86    itojun 
    420   1.86    itojun 	switch (ln->ln_state) {
    421  1.263       roy 	case ND6_LLINFO_WAITDELETE:
    422  1.263       roy 		LLE_REMREF(ln);
    423  1.263       roy 		nd6_free(ln, 0);
    424  1.263       roy 		ln = NULL;
    425  1.263       roy 		break;
    426  1.263       roy 
    427   1.86    itojun 	case ND6_LLINFO_INCOMPLETE:
    428  1.263       roy 		if (ln->ln_asked++ < nd6_mmaxtries) {
    429  1.178     ozaki 			send_ns = true;
    430  1.262       roy 			break;
    431  1.262       roy 		}
    432  1.262       roy 
    433  1.269       roy 		missed = true;
    434  1.269       roy 		sockaddr_in6_init(&tsin6, taddr6, 0, 0, 0);
    435  1.269       roy 
    436  1.262       roy 		if (ln->ln_hold) {
    437  1.268  christos 			struct mbuf *m0;
    438  1.268  christos 
    439  1.268  christos 			m = ln->ln_hold;
    440  1.262       roy 
    441  1.262       roy 			/*
    442  1.262       roy 			 * assuming every packet in ln_hold has
    443  1.262       roy 			 * the same IP header
    444  1.262       roy 			 */
    445  1.262       roy 			m0 = m->m_nextpkt;
    446  1.262       roy 			m->m_nextpkt = NULL;
    447  1.262       roy 			ln->ln_hold = m0;
    448  1.262       roy 			clear_llinfo_pqueue(ln);
    449    1.2    itojun 		}
    450  1.262       roy 
    451  1.263       roy 		/*
    452  1.263       roy 		 * Move to the ND6_LLINFO_WAITDELETE state for another
    453  1.263       roy 		 * interval at which point the llentry will be freed
    454  1.263       roy 		 * unless it's attempted to be used again and we'll
    455  1.263       roy 		 * resend NS again, rinse and repeat.
    456  1.263       roy 		 */
    457  1.263       roy 		ln->ln_state = ND6_LLINFO_WAITDELETE;
    458  1.263       roy 		if (ln->ln_asked == nd6_mmaxtries)
    459  1.263       roy 			nd6_llinfo_settimer(ln, ndi->retrans * hz / 1000);
    460  1.263       roy 		else
    461  1.263       roy 			send_ns = true;
    462   1.86    itojun 		break;
    463  1.262       roy 
    464   1.86    itojun 	case ND6_LLINFO_REACHABLE:
    465   1.86    itojun 		if (!ND6_LLINFO_PERMANENT(ln)) {
    466   1.86    itojun 			ln->ln_state = ND6_LLINFO_STALE;
    467  1.188     ozaki 			nd6_llinfo_settimer(ln, nd6_gctimer * hz);
    468    1.2    itojun 		}
    469   1.86    itojun 		break;
    470    1.2    itojun 
    471  1.143  christos 	case ND6_LLINFO_PURGE:
    472   1.86    itojun 	case ND6_LLINFO_STALE:
    473   1.86    itojun 		/* Garbage Collection(RFC 2461 5.3) */
    474   1.86    itojun 		if (!ND6_LLINFO_PERMANENT(ln)) {
    475  1.256  christos 			LLE_REMREF(ln);
    476  1.188     ozaki 			nd6_free(ln, 1);
    477   1.86    itojun 			ln = NULL;
    478    1.2    itojun 		}
    479   1.86    itojun 		break;
    480   1.54    itojun 
    481   1.86    itojun 	case ND6_LLINFO_DELAY:
    482  1.271       roy 		if (ndi->flags & ND6_IFF_PERFORMNUD) {
    483   1.86    itojun 			/* We need NUD */
    484   1.86    itojun 			ln->ln_asked = 1;
    485   1.86    itojun 			ln->ln_state = ND6_LLINFO_PROBE;
    486  1.188     ozaki 			daddr6 = &ln->r_l3addr.addr6;
    487  1.178     ozaki 			send_ns = true;
    488   1.86    itojun 		} else {
    489   1.86    itojun 			ln->ln_state = ND6_LLINFO_STALE; /* XXX */
    490  1.188     ozaki 			nd6_llinfo_settimer(ln, nd6_gctimer * hz);
    491   1.86    itojun 		}
    492   1.86    itojun 		break;
    493   1.86    itojun 	case ND6_LLINFO_PROBE:
    494   1.86    itojun 		if (ln->ln_asked < nd6_umaxtries) {
    495   1.86    itojun 			ln->ln_asked++;
    496  1.188     ozaki 			daddr6 = &ln->r_l3addr.addr6;
    497  1.178     ozaki 			send_ns = true;
    498   1.86    itojun 		} else {
    499  1.256  christos 			LLE_REMREF(ln);
    500  1.188     ozaki 			nd6_free(ln, 0);
    501   1.86    itojun 			ln = NULL;
    502   1.86    itojun 		}
    503   1.86    itojun 		break;
    504   1.86    itojun 	}
    505   1.65    itojun 
    506  1.178     ozaki 	if (send_ns) {
    507  1.179     ozaki 		struct in6_addr src, *psrc;
    508  1.179     ozaki 
    509  1.188     ozaki 		nd6_llinfo_settimer(ln, ndi->retrans * hz / 1000);
    510  1.179     ozaki 		psrc = nd6_llinfo_get_holdsrc(ln, &src);
    511  1.181     ozaki 		LLE_FREE_LOCKED(ln);
    512  1.181     ozaki 		ln = NULL;
    513  1.247       roy 		nd6_ns_output(ifp, daddr6, taddr6, psrc, NULL);
    514  1.178     ozaki 	}
    515  1.178     ozaki 
    516  1.181     ozaki out:
    517  1.181     ozaki 	if (ln != NULL)
    518  1.181     ozaki 		LLE_FREE_LOCKED(ln);
    519  1.239     ozaki 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
    520  1.269       roy 	if (missed) {
    521  1.269       roy 		struct in6_addr mdaddr6 = zeroin6_addr;
    522  1.269       roy 		struct sockaddr *sa;
    523  1.269       roy 
    524  1.269       roy 		if (m != NULL)
    525  1.269       roy 			icmp6_error2(m, ICMP6_DST_UNREACH,
    526  1.269       roy 			    ICMP6_DST_UNREACH_ADDR, 0, ifp, &mdaddr6);
    527  1.269       roy 		if (!IN6_IS_ADDR_UNSPECIFIED(&mdaddr6)) {
    528  1.269       roy 			sockaddr_in6_init(&dsin6, &mdaddr6, 0, 0, 0);
    529  1.269       roy 			sa = sin6tosa(&dsin6);
    530  1.269       roy 		} else
    531  1.269       roy 			sa = NULL;
    532  1.269       roy 		rt_clonedmsg(RTM_MISS, sa, sin6tosa(&tsin6), NULL, ifp);
    533  1.268  christos 	}
    534   1.86    itojun }
    535   1.42    itojun 
    536   1.86    itojun /*
    537   1.86    itojun  * ND6 timer routine to expire default route list and prefix list
    538   1.86    itojun  */
    539  1.186     ozaki static void
    540  1.203     ozaki nd6_timer_work(struct work *wk, void *arg)
    541   1.86    itojun {
    542   1.86    itojun 	struct in6_ifaddr *ia6, *nia6;
    543  1.205     ozaki 	int s, bound;
    544  1.205     ozaki 	struct psref psref;
    545   1.42    itojun 
    546   1.86    itojun 	callout_reset(&nd6_timer_ch, nd6_prune * hz,
    547   1.86    itojun 	    nd6_timer, NULL);
    548   1.63    itojun 
    549  1.239     ozaki 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
    550  1.126        ad 
    551  1.271       roy 	/* expire interface addresses */
    552  1.205     ozaki 	bound = curlwp_bind();
    553  1.205     ozaki 	s = pserialize_read_enter();
    554  1.205     ozaki 	for (ia6 = IN6_ADDRLIST_READER_FIRST(); ia6; ia6 = nia6) {
    555  1.205     ozaki 		nia6 = IN6_ADDRLIST_READER_NEXT(ia6);
    556  1.205     ozaki 
    557  1.205     ozaki 		ia6_acquire(ia6, &psref);
    558  1.205     ozaki 		pserialize_read_exit(s);
    559  1.205     ozaki 
    560   1.65    itojun 		/* check address lifetime */
    561   1.65    itojun 		if (IFA6_IS_INVALID(ia6)) {
    562  1.240     ozaki 			struct ifnet *ifp;
    563   1.99    rpaulo 
    564  1.240     ozaki 			ifp = ia6->ia_ifa.ifa_ifp;
    565  1.240     ozaki 			IFNET_LOCK(ifp);
    566  1.240     ozaki 			/*
    567  1.240     ozaki 			 * Need to take the lock first to prevent if_detach
    568  1.240     ozaki 			 * from running in6_purgeaddr concurrently.
    569  1.240     ozaki 			 */
    570  1.240     ozaki 			if (!if_is_deactivated(ifp)) {
    571  1.240     ozaki 				ia6_release(ia6, &psref);
    572  1.240     ozaki 				in6_purgeaddr(&ia6->ia_ifa);
    573  1.240     ozaki 			} else {
    574  1.240     ozaki 				/*
    575  1.240     ozaki 				 * ifp is being destroyed, ia6 will be destroyed
    576  1.240     ozaki 				 * by if_detach.
    577  1.240     ozaki 				 */
    578  1.240     ozaki 				ia6_release(ia6, &psref);
    579  1.240     ozaki 			}
    580  1.205     ozaki 			ia6 = NULL;
    581  1.240     ozaki 			IFNET_UNLOCK(ifp);
    582   1.99    rpaulo 		} else if (IFA6_IS_DEPRECATED(ia6)) {
    583   1.99    rpaulo 			int oldflags = ia6->ia6_flags;
    584   1.99    rpaulo 
    585  1.145       roy 			if ((oldflags & IN6_IFF_DEPRECATED) == 0) {
    586  1.145       roy 				ia6->ia6_flags |= IN6_IFF_DEPRECATED;
    587  1.253       roy 				rt_addrmsg(RTM_NEWADDR, (struct ifaddr *)ia6);
    588  1.145       roy 			}
    589   1.65    itojun 		} else {
    590   1.65    itojun 			/*
    591   1.65    itojun 			 * A new RA might have made a deprecated address
    592   1.65    itojun 			 * preferred.
    593   1.65    itojun 			 */
    594  1.145       roy 			if (ia6->ia6_flags & IN6_IFF_DEPRECATED) {
    595  1.145       roy 				ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
    596  1.253       roy 				rt_addrmsg(RTM_NEWADDR, (struct ifaddr *)ia6);
    597  1.145       roy 			}
    598    1.2    itojun 		}
    599  1.205     ozaki 		s = pserialize_read_enter();
    600  1.205     ozaki 		ia6_release(ia6, &psref);
    601   1.65    itojun 	}
    602  1.205     ozaki 	pserialize_read_exit(s);
    603  1.205     ozaki 	curlwp_bindx(bound);
    604    1.2    itojun 
    605  1.239     ozaki 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
    606   1.16    itojun }
    607   1.16    itojun 
    608  1.203     ozaki static void
    609  1.203     ozaki nd6_timer(void *ignored_arg)
    610  1.203     ozaki {
    611  1.203     ozaki 
    612  1.203     ozaki 	workqueue_enqueue(nd6_timer_wq, &nd6_timer_wk, NULL);
    613  1.203     ozaki }
    614  1.203     ozaki 
    615   1.16    itojun /*
    616   1.16    itojun  * Nuke neighbor cache/prefix/default router management table, right before
    617   1.16    itojun  * ifp goes away.
    618   1.16    itojun  */
    619   1.16    itojun void
    620  1.158    martin nd6_purge(struct ifnet *ifp, struct in6_ifextra *ext)
    621   1.16    itojun {
    622   1.16    itojun 
    623   1.65    itojun 	/*
    624  1.158    martin 	 * During detach, the ND info might be already removed, but
    625  1.158    martin 	 * then is explitly passed as argument.
    626  1.158    martin 	 * Otherwise get it from ifp->if_afdata.
    627  1.158    martin 	 */
    628  1.158    martin 	if (ext == NULL)
    629  1.158    martin 		ext = ifp->if_afdata[AF_INET6];
    630  1.158    martin 	if (ext == NULL)
    631  1.158    martin 		return;
    632  1.158    martin 
    633   1.16    itojun 	/*
    634  1.181     ozaki 	 * We may not need to nuke the neighbor cache entries here
    635  1.181     ozaki 	 * because the neighbor cache is kept in if_afdata[AF_INET6].
    636  1.181     ozaki 	 * nd6_purge() is invoked by in6_ifdetach() which is called
    637  1.181     ozaki 	 * from if_detach() where everything gets purged. However
    638  1.181     ozaki 	 * in6_ifdetach is directly called from vlan(4), so we still
    639  1.181     ozaki 	 * need to purge entries here.
    640   1.16    itojun 	 */
    641  1.181     ozaki 	if (ext->lltable != NULL)
    642  1.181     ozaki 		lltable_purge_entries(ext->lltable);
    643    1.2    itojun }
    644    1.2    itojun 
    645  1.188     ozaki struct llentry *
    646  1.188     ozaki nd6_lookup(const struct in6_addr *addr6, const struct ifnet *ifp, bool wlock)
    647  1.188     ozaki {
    648  1.188     ozaki 	struct sockaddr_in6 sin6;
    649  1.188     ozaki 	struct llentry *ln;
    650  1.188     ozaki 
    651  1.188     ozaki 	sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
    652  1.188     ozaki 
    653  1.188     ozaki 	IF_AFDATA_RLOCK(ifp);
    654  1.188     ozaki 	ln = lla_lookup(LLTABLE6(ifp), wlock ? LLE_EXCLUSIVE : 0,
    655  1.188     ozaki 	    sin6tosa(&sin6));
    656  1.188     ozaki 	IF_AFDATA_RUNLOCK(ifp);
    657  1.188     ozaki 
    658  1.188     ozaki 	return ln;
    659  1.188     ozaki }
    660  1.188     ozaki 
    661  1.188     ozaki struct llentry *
    662  1.188     ozaki nd6_create(const struct in6_addr *addr6, const struct ifnet *ifp)
    663    1.2    itojun {
    664    1.2    itojun 	struct sockaddr_in6 sin6;
    665  1.188     ozaki 	struct llentry *ln;
    666  1.237     ozaki 	struct rtentry *rt;
    667    1.2    itojun 
    668  1.122    dyoung 	sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
    669  1.237     ozaki 	rt = rtalloc1(sin6tosa(&sin6), 0);
    670  1.188     ozaki 
    671  1.188     ozaki 	IF_AFDATA_WLOCK(ifp);
    672  1.237     ozaki 	ln = lla_create(LLTABLE6(ifp), LLE_EXCLUSIVE, sin6tosa(&sin6), rt);
    673  1.188     ozaki 	IF_AFDATA_WUNLOCK(ifp);
    674  1.188     ozaki 
    675  1.237     ozaki 	if (rt != NULL)
    676  1.237     ozaki 		rt_unref(rt);
    677  1.188     ozaki 	if (ln != NULL)
    678  1.188     ozaki 		ln->ln_state = ND6_LLINFO_NOSTATE;
    679  1.188     ozaki 
    680  1.188     ozaki 	return ln;
    681  1.188     ozaki }
    682  1.188     ozaki 
    683  1.188     ozaki /*
    684  1.188     ozaki  * Test whether a given IPv6 address is a neighbor or not, ignoring
    685  1.188     ozaki  * the actual neighbor cache.  The neighbor cache is ignored in order
    686  1.188     ozaki  * to not reenter the routing code from within itself.
    687  1.188     ozaki  */
    688  1.188     ozaki static int
    689  1.188     ozaki nd6_is_new_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
    690  1.188     ozaki {
    691  1.188     ozaki 	struct ifaddr *dstaddr;
    692  1.205     ozaki 	int s;
    693  1.188     ozaki 
    694  1.188     ozaki 	/*
    695  1.188     ozaki 	 * A link-local address is always a neighbor.
    696  1.188     ozaki 	 * XXX: a link does not necessarily specify a single interface.
    697  1.188     ozaki 	 */
    698  1.188     ozaki 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
    699  1.188     ozaki 		struct sockaddr_in6 sin6_copy;
    700  1.188     ozaki 		u_int32_t zone;
    701  1.188     ozaki 
    702    1.2    itojun 		/*
    703  1.188     ozaki 		 * We need sin6_copy since sa6_recoverscope() may modify the
    704  1.188     ozaki 		 * content (XXX).
    705  1.188     ozaki 		 */
    706  1.188     ozaki 		sin6_copy = *addr;
    707  1.188     ozaki 		if (sa6_recoverscope(&sin6_copy))
    708  1.188     ozaki 			return 0; /* XXX: should be impossible */
    709  1.188     ozaki 		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
    710  1.188     ozaki 			return 0;
    711  1.188     ozaki 		if (sin6_copy.sin6_scope_id == zone)
    712  1.188     ozaki 			return 1;
    713  1.188     ozaki 		else
    714  1.188     ozaki 			return 0;
    715  1.188     ozaki 	}
    716  1.188     ozaki 
    717  1.188     ozaki 	/*
    718  1.188     ozaki 	 * If the address is assigned on the node of the other side of
    719  1.188     ozaki 	 * a p2p interface, the address should be a neighbor.
    720  1.188     ozaki 	 */
    721  1.205     ozaki 	s = pserialize_read_enter();
    722  1.204     ozaki 	dstaddr = ifa_ifwithdstaddr(sin6tocsa(addr));
    723  1.188     ozaki 	if (dstaddr != NULL) {
    724  1.188     ozaki 		if (dstaddr->ifa_ifp == ifp) {
    725  1.205     ozaki 			pserialize_read_exit(s);
    726  1.188     ozaki 			return 1;
    727  1.188     ozaki 		}
    728  1.188     ozaki 	}
    729  1.205     ozaki 	pserialize_read_exit(s);
    730  1.147       roy 
    731  1.188     ozaki 	return 0;
    732  1.147       roy }
    733  1.147       roy 
    734    1.6    itojun /*
    735    1.6    itojun  * Detect if a given IPv6 address identifies a neighbor on a given link.
    736   1.25    itojun  * XXX: should take care of the destination of a p2p link?
    737    1.6    itojun  */
    738    1.6    itojun int
    739  1.110    dyoung nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
    740    1.6    itojun {
    741  1.188     ozaki 	struct llentry *ln;
    742  1.165     ozaki 	struct rtentry *rt;
    743    1.6    itojun 
    744   1.29    itojun 	/*
    745   1.29    itojun 	 * A link-local address is always a neighbor.
    746   1.52    itojun 	 * XXX: a link does not necessarily specify a single interface.
    747   1.29    itojun 	 */
    748   1.96    rpaulo 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
    749   1.96    rpaulo 		struct sockaddr_in6 sin6_copy;
    750   1.96    rpaulo 		u_int32_t zone;
    751   1.96    rpaulo 
    752   1.96    rpaulo 		/*
    753   1.96    rpaulo 		 * We need sin6_copy since sa6_recoverscope() may modify the
    754   1.96    rpaulo 		 * content (XXX).
    755   1.96    rpaulo 		 */
    756   1.96    rpaulo 		sin6_copy = *addr;
    757   1.96    rpaulo 		if (sa6_recoverscope(&sin6_copy))
    758  1.112    dyoung 			return 0; /* XXX: should be impossible */
    759   1.96    rpaulo 		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
    760  1.112    dyoung 			return 0;
    761   1.96    rpaulo 		if (sin6_copy.sin6_scope_id == zone)
    762  1.112    dyoung 			return 1;
    763   1.96    rpaulo 		else
    764  1.112    dyoung 			return 0;
    765   1.96    rpaulo 	}
    766    1.6    itojun 
    767  1.188     ozaki 	if (nd6_is_new_addr_neighbor(addr, ifp))
    768  1.188     ozaki 		return 1;
    769  1.188     ozaki 
    770    1.6    itojun 	/*
    771  1.188     ozaki 	 * Even if the address matches none of our addresses, it might be
    772  1.188     ozaki 	 * in the neighbor cache or a connected route.
    773    1.6    itojun 	 */
    774  1.188     ozaki 	ln = nd6_lookup(&addr->sin6_addr, ifp, false);
    775  1.188     ozaki 	if (ln != NULL) {
    776  1.188     ozaki 		LLE_RUNLOCK(ln);
    777  1.188     ozaki 		return 1;
    778  1.188     ozaki 	}
    779  1.188     ozaki 
    780  1.188     ozaki 	rt = rtalloc1(sin6tocsa(addr), 0);
    781  1.188     ozaki 	if (rt == NULL)
    782  1.188     ozaki 		return 0;
    783  1.188     ozaki 
    784  1.188     ozaki 	if ((rt->rt_flags & RTF_CONNECTED) && (rt->rt_ifp == ifp
    785  1.188     ozaki #if NBRIDGE > 0
    786  1.188     ozaki 	    || rt->rt_ifp->if_bridge == ifp->if_bridge
    787  1.188     ozaki #endif
    788  1.188     ozaki #if NCARP > 0
    789  1.188     ozaki 	    || (ifp->if_type == IFT_CARP && rt->rt_ifp == ifp->if_carpdev) ||
    790  1.188     ozaki 	    (rt->rt_ifp->if_type == IFT_CARP && rt->rt_ifp->if_carpdev == ifp)||
    791  1.188     ozaki 	    (ifp->if_type == IFT_CARP && rt->rt_ifp->if_type == IFT_CARP &&
    792  1.188     ozaki 	    rt->rt_ifp->if_carpdev == ifp->if_carpdev)
    793  1.188     ozaki #endif
    794  1.188     ozaki 	    )) {
    795  1.216     ozaki 		rt_unref(rt);
    796  1.112    dyoung 		return 1;
    797  1.165     ozaki 	}
    798  1.216     ozaki 	rt_unref(rt);
    799    1.6    itojun 
    800  1.112    dyoung 	return 0;
    801    1.2    itojun }
    802    1.2    itojun 
    803    1.2    itojun /*
    804    1.2    itojun  * Free an nd6 llinfo entry.
    805   1.54    itojun  * Since the function would cause significant changes in the kernel, DO NOT
    806   1.54    itojun  * make it global, unless you have a strong reason for the change, and are sure
    807   1.54    itojun  * that the change is safe.
    808    1.2    itojun  */
    809  1.181     ozaki static void
    810  1.188     ozaki nd6_free(struct llentry *ln, int gc)
    811    1.2    itojun {
    812  1.188     ozaki 	struct ifnet *ifp;
    813    1.2    itojun 
    814  1.181     ozaki 	KASSERT(ln != NULL);
    815  1.181     ozaki 	LLE_WLOCK_ASSERT(ln);
    816  1.181     ozaki 
    817   1.18    itojun 	/*
    818  1.271       roy 	 * If the reason for the deletion is just garbage collection,
    819  1.271       roy 	 * and the neighbor is an active router, do not delete it.
    820  1.271       roy 	 * Instead, reset the GC timer using the router's lifetime.
    821  1.271       roy 	 * XXX: the check for ln_state should be redundant,
    822  1.271       roy 	 *      but we intentionally keep it just in case.
    823   1.18    itojun 	 */
    824  1.271       roy 	if (!ip6_forwarding && ln->ln_router &&
    825  1.271       roy 	    ln->ln_state == ND6_LLINFO_STALE && gc)
    826  1.271       roy 	{
    827  1.271       roy 		if (ln->ln_expire > time_uptime)
    828  1.271       roy 			nd6_llinfo_settimer(ln,
    829  1.271       roy 			    (ln->ln_expire - time_uptime) * hz);
    830  1.271       roy 		else
    831  1.271       roy 			nd6_llinfo_settimer(ln, nd6_gctimer * hz);
    832  1.260       roy 		LLE_WUNLOCK(ln);
    833  1.271       roy 		return;
    834  1.271       roy 	}
    835   1.12    itojun 
    836  1.271       roy 	ifp = ln->lle_tbl->llt_ifp;
    837   1.12    itojun 
    838  1.262       roy 	if (ln->la_flags & LLE_VALID || gc) {
    839  1.262       roy 		struct sockaddr_in6 sin6;
    840  1.262       roy 		const char *lladdr;
    841  1.262       roy 
    842  1.271       roy 		sockaddr_in6_init(&sin6, &ln->r_l3addr.addr6, 0, 0, 0);
    843  1.262       roy 		lladdr = ln->la_flags & LLE_VALID ?
    844  1.262       roy 		    (const char *)&ln->ll_addr : NULL;
    845  1.267       roy 		rt_clonedmsg(RTM_DELETE, NULL, sin6tosa(&sin6), lladdr, ifp);
    846  1.262       roy 	}
    847  1.259       roy 
    848  1.188     ozaki 	/*
    849  1.188     ozaki 	 * Save to unlock. We still hold an extra reference and will not
    850  1.188     ozaki 	 * free(9) in llentry_free() if someone else holds one as well.
    851  1.188     ozaki 	 */
    852  1.181     ozaki 	LLE_WUNLOCK(ln);
    853  1.188     ozaki 	IF_AFDATA_LOCK(ifp);
    854  1.188     ozaki 	LLE_WLOCK(ln);
    855  1.188     ozaki 
    856  1.222     ozaki 	lltable_free_entry(LLTABLE6(ifp), ln);
    857  1.188     ozaki 
    858  1.188     ozaki 	IF_AFDATA_UNLOCK(ifp);
    859    1.2    itojun }
    860    1.2    itojun 
    861    1.2    itojun /*
    862    1.2    itojun  * Upper-layer reachability hint for Neighbor Unreachability Detection.
    863    1.2    itojun  *
    864   1.98    rpaulo  * XXX cost-effective methods?
    865    1.2    itojun  */
    866    1.2    itojun void
    867  1.171     ozaki nd6_nud_hint(struct rtentry *rt)
    868    1.2    itojun {
    869  1.181     ozaki 	struct llentry *ln;
    870  1.188     ozaki 	struct ifnet *ifp;
    871    1.2    itojun 
    872  1.164     ozaki 	if (rt == NULL)
    873  1.164     ozaki 		return;
    874    1.2    itojun 
    875  1.188     ozaki 	ifp = rt->rt_ifp;
    876  1.188     ozaki 	ln = nd6_lookup(&(satocsin6(rt_getkey(rt)))->sin6_addr, ifp, true);
    877  1.188     ozaki 	if (ln == NULL)
    878  1.171     ozaki 		return;
    879    1.2    itojun 
    880   1.22    itojun 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
    881  1.188     ozaki 		goto done;
    882    1.2    itojun 
    883   1.31    itojun 	/*
    884   1.31    itojun 	 * if we get upper-layer reachability confirmation many times,
    885   1.31    itojun 	 * it is possible we have false information.
    886   1.31    itojun 	 */
    887  1.164     ozaki 	ln->ln_byhint++;
    888  1.164     ozaki 	if (ln->ln_byhint > nd6_maxnudhint)
    889  1.188     ozaki 		goto done;
    890   1.31    itojun 
    891    1.2    itojun 	ln->ln_state = ND6_LLINFO_REACHABLE;
    892  1.188     ozaki 	if (!ND6_LLINFO_PERMANENT(ln))
    893  1.188     ozaki 		nd6_llinfo_settimer(ln, ND_IFINFO(rt->rt_ifp)->reachable * hz);
    894  1.188     ozaki 
    895  1.188     ozaki done:
    896  1.188     ozaki 	LLE_WUNLOCK(ln);
    897  1.171     ozaki 
    898  1.165     ozaki 	return;
    899    1.2    itojun }
    900    1.2    itojun 
    901  1.207     ozaki struct gc_args {
    902  1.207     ozaki 	int gc_entries;
    903  1.207     ozaki 	const struct in6_addr *skip_in6;
    904  1.207     ozaki };
    905  1.207     ozaki 
    906  1.181     ozaki static int
    907  1.181     ozaki nd6_purge_entry(struct lltable *llt, struct llentry *ln, void *farg)
    908  1.181     ozaki {
    909  1.207     ozaki 	struct gc_args *args = farg;
    910  1.207     ozaki 	int *n = &args->gc_entries;
    911  1.207     ozaki 	const struct in6_addr *skip_in6 = args->skip_in6;
    912  1.181     ozaki 
    913  1.181     ozaki 	if (*n <= 0)
    914  1.181     ozaki 		return 0;
    915  1.181     ozaki 
    916  1.181     ozaki 	if (ND6_LLINFO_PERMANENT(ln))
    917  1.181     ozaki 		return 0;
    918  1.181     ozaki 
    919  1.207     ozaki 	if (IN6_ARE_ADDR_EQUAL(&ln->r_l3addr.addr6, skip_in6))
    920  1.207     ozaki 		return 0;
    921  1.207     ozaki 
    922  1.181     ozaki 	LLE_WLOCK(ln);
    923  1.181     ozaki 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
    924  1.181     ozaki 		ln->ln_state = ND6_LLINFO_STALE;
    925  1.181     ozaki 	else
    926  1.181     ozaki 		ln->ln_state = ND6_LLINFO_PURGE;
    927  1.188     ozaki 	nd6_llinfo_settimer(ln, 0);
    928  1.181     ozaki 	LLE_WUNLOCK(ln);
    929  1.181     ozaki 
    930  1.181     ozaki 	(*n)--;
    931  1.181     ozaki 	return 0;
    932  1.181     ozaki }
    933  1.181     ozaki 
    934  1.181     ozaki static void
    935  1.207     ozaki nd6_gc_neighbors(struct lltable *llt, const struct in6_addr *in6)
    936  1.181     ozaki {
    937  1.181     ozaki 
    938  1.181     ozaki 	if (ip6_neighborgcthresh >= 0 &&
    939  1.181     ozaki 	    lltable_get_entry_count(llt) >= ip6_neighborgcthresh) {
    940  1.207     ozaki 		struct gc_args gc_args = {10, in6};
    941  1.181     ozaki 		/*
    942  1.181     ozaki 		 * XXX entries that are "less recently used" should be
    943  1.181     ozaki 		 * freed first.
    944  1.181     ozaki 		 */
    945  1.207     ozaki 		lltable_foreach_lle(llt, nd6_purge_entry, &gc_args);
    946  1.181     ozaki 	}
    947  1.181     ozaki }
    948  1.181     ozaki 
    949    1.2    itojun void
    950  1.130    dyoung nd6_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
    951    1.2    itojun {
    952    1.2    itojun 	struct sockaddr *gate = rt->rt_gateway;
    953    1.2    itojun 	struct ifnet *ifp = rt->rt_ifp;
    954  1.119    dyoung 	uint8_t namelen = strlen(ifp->if_xname), addrlen = ifp->if_addrlen;
    955    1.2    itojun 	struct ifaddr *ifa;
    956    1.2    itojun 
    957  1.144     joerg 	RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
    958  1.117    dyoung 
    959  1.131    dyoung 	if (req == RTM_LLINFO_UPD) {
    960  1.131    dyoung 		int rc;
    961  1.131    dyoung 		struct in6_addr *in6;
    962  1.131    dyoung 		struct in6_addr in6_all;
    963  1.131    dyoung 		int anycast;
    964  1.131    dyoung 
    965  1.131    dyoung 		if ((ifa = info->rti_ifa) == NULL)
    966  1.131    dyoung 			return;
    967  1.131    dyoung 
    968  1.131    dyoung 		in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
    969  1.131    dyoung 		anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST;
    970  1.131    dyoung 
    971  1.131    dyoung 		in6_all = in6addr_linklocal_allnodes;
    972  1.131    dyoung 		if ((rc = in6_setscope(&in6_all, ifa->ifa_ifp, NULL)) != 0) {
    973  1.131    dyoung 			log(LOG_ERR, "%s: failed to set scope %s "
    974  1.131    dyoung 			    "(errno=%d)\n", __func__, if_name(ifp), rc);
    975  1.131    dyoung 			return;
    976  1.131    dyoung 		}
    977  1.131    dyoung 
    978  1.131    dyoung 		/* XXX don't set Override for proxy addresses */
    979  1.131    dyoung 		nd6_na_output(ifa->ifa_ifp, &in6_all, in6,
    980  1.131    dyoung 		    (anycast ? 0 : ND_NA_FLAG_OVERRIDE)
    981  1.131    dyoung #if 0
    982  1.131    dyoung 		    | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
    983  1.131    dyoung #endif
    984  1.131    dyoung 		    , 1, NULL);
    985  1.131    dyoung 		return;
    986  1.131    dyoung 	}
    987  1.131    dyoung 
    988  1.270       roy 	if ((rt->rt_flags & RTF_GATEWAY) != 0) {
    989  1.270       roy 		if (req != RTM_ADD)
    990  1.270       roy 			return;
    991  1.270       roy 		/*
    992  1.270       roy 		 * linklayers with particular MTU limitation.
    993  1.270       roy 		 */
    994  1.270       roy 		switch(ifp->if_type) {
    995  1.270       roy #if NARCNET > 0
    996  1.270       roy 		case IFT_ARCNET:
    997  1.270       roy 			if (rt->rt_rmx.rmx_mtu > ARC_PHDS_MAXMTU) /* RFC2497 */
    998  1.270       roy 				rt->rt_rmx.rmx_mtu = ARC_PHDS_MAXMTU;
    999  1.270       roy 			break;
   1000  1.270       roy #endif
   1001  1.270       roy 		}
   1002    1.2    itojun 		return;
   1003  1.270       roy 	}
   1004    1.2    itojun 
   1005   1.54    itojun 	if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
   1006  1.144     joerg 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1007   1.54    itojun 		/*
   1008   1.54    itojun 		 * This is probably an interface direct route for a link
   1009   1.54    itojun 		 * which does not need neighbor caches (e.g. fe80::%lo0/64).
   1010   1.54    itojun 		 * We do not need special treatment below for such a route.
   1011   1.54    itojun 		 * Moreover, the RTF_LLINFO flag which would be set below
   1012   1.54    itojun 		 * would annoy the ndp(8) command.
   1013   1.54    itojun 		 */
   1014   1.54    itojun 		return;
   1015   1.54    itojun 	}
   1016   1.54    itojun 
   1017    1.2    itojun 	switch (req) {
   1018  1.205     ozaki 	case RTM_ADD: {
   1019  1.238     ozaki 		struct psref psref;
   1020  1.205     ozaki 
   1021  1.144     joerg 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1022    1.2    itojun 		/*
   1023    1.2    itojun 		 * There is no backward compatibility :)
   1024    1.2    itojun 		 *
   1025    1.2    itojun 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
   1026    1.2    itojun 		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
   1027    1.2    itojun 		 *	   rt->rt_flags |= RTF_CLONING;
   1028    1.2    itojun 		 */
   1029  1.188     ozaki 		/* XXX should move to route.c? */
   1030  1.188     ozaki 		if (rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL)) {
   1031  1.120    dyoung 			union {
   1032  1.120    dyoung 				struct sockaddr sa;
   1033  1.120    dyoung 				struct sockaddr_dl sdl;
   1034  1.120    dyoung 				struct sockaddr_storage ss;
   1035  1.120    dyoung 			} u;
   1036    1.2    itojun 			/*
   1037   1.84    itojun 			 * Case 1: This route should come from a route to
   1038   1.84    itojun 			 * interface (RTF_CLONING case) or the route should be
   1039   1.84    itojun 			 * treated as on-link but is currently not
   1040  1.129    dyoung 			 * (RTF_LLINFO && ln == NULL case).
   1041    1.2    itojun 			 */
   1042  1.136    dyoung 			if (sockaddr_dl_init(&u.sdl, sizeof(u.ss),
   1043  1.120    dyoung 			    ifp->if_index, ifp->if_type,
   1044  1.136    dyoung 			    NULL, namelen, NULL, addrlen) == NULL) {
   1045  1.136    dyoung 				printf("%s.%d: sockaddr_dl_init(, %zu, ) "
   1046  1.136    dyoung 				    "failed on %s\n", __func__, __LINE__,
   1047  1.136    dyoung 				    sizeof(u.ss), if_name(ifp));
   1048  1.136    dyoung 			}
   1049  1.120    dyoung 			rt_setgate(rt, &u.sa);
   1050  1.119    dyoung 			gate = rt->rt_gateway;
   1051  1.144     joerg 			RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1052  1.192     ozaki 			if (gate == NULL) {
   1053  1.192     ozaki 				log(LOG_ERR,
   1054  1.192     ozaki 				    "%s: rt_setgate failed on %s\n", __func__,
   1055  1.192     ozaki 				    if_name(ifp));
   1056  1.192     ozaki 				break;
   1057  1.192     ozaki 			}
   1058  1.188     ozaki 
   1059  1.144     joerg 			RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1060  1.188     ozaki 			if ((rt->rt_flags & RTF_CONNECTED) != 0)
   1061    1.2    itojun 				break;
   1062    1.2    itojun 		}
   1063  1.144     joerg 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1064   1.18    itojun 		/*
   1065   1.18    itojun 		 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
   1066   1.18    itojun 		 * We don't do that here since llinfo is not ready yet.
   1067   1.18    itojun 		 *
   1068   1.18    itojun 		 * There are also couple of other things to be discussed:
   1069   1.18    itojun 		 * - unsolicited NA code needs improvement beforehand
   1070   1.18    itojun 		 * - RFC2461 says we MAY send multicast unsolicited NA
   1071   1.18    itojun 		 *   (7.2.6 paragraph 4), however, it also says that we
   1072   1.18    itojun 		 *   SHOULD provide a mechanism to prevent multicast NA storm.
   1073   1.18    itojun 		 *   we don't have anything like it right now.
   1074   1.38    itojun 		 *   note that the mechanism needs a mutual agreement
   1075   1.18    itojun 		 *   between proxies, which means that we need to implement
   1076   1.38    itojun 		 *   a new protocol, or a new kludge.
   1077   1.38    itojun 		 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
   1078   1.18    itojun 		 *   we need to check ip6forwarding before sending it.
   1079   1.18    itojun 		 *   (or should we allow proxy ND configuration only for
   1080   1.18    itojun 		 *   routers?  there's no mention about proxy ND from hosts)
   1081   1.18    itojun 		 */
   1082   1.18    itojun #if 0
   1083   1.18    itojun 		/* XXX it does not work */
   1084    1.2    itojun 		if (rt->rt_flags & RTF_ANNOUNCE)
   1085    1.2    itojun 			nd6_na_output(ifp,
   1086  1.118    dyoung 			      &satocsin6(rt_getkey(rt))->sin6_addr,
   1087  1.118    dyoung 			      &satocsin6(rt_getkey(rt))->sin6_addr,
   1088   1.18    itojun 			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
   1089   1.18    itojun 			      1, NULL);
   1090   1.18    itojun #endif
   1091  1.188     ozaki 
   1092   1.65    itojun 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
   1093  1.144     joerg 			RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1094   1.26    itojun 			/*
   1095   1.26    itojun 			 * Address resolution isn't necessary for a point to
   1096   1.26    itojun 			 * point link, so we can skip this test for a p2p link.
   1097   1.26    itojun 			 */
   1098   1.26    itojun 			if (gate->sa_family != AF_LINK ||
   1099  1.119    dyoung 			    gate->sa_len <
   1100  1.119    dyoung 			    sockaddr_dl_measure(namelen, addrlen)) {
   1101   1.26    itojun 				log(LOG_DEBUG,
   1102   1.36    itojun 				    "nd6_rtrequest: bad gateway value: %s\n",
   1103   1.36    itojun 				    if_name(ifp));
   1104   1.26    itojun 				break;
   1105   1.26    itojun 			}
   1106  1.118    dyoung 			satosdl(gate)->sdl_type = ifp->if_type;
   1107  1.118    dyoung 			satosdl(gate)->sdl_index = ifp->if_index;
   1108  1.144     joerg 			RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1109    1.2    itojun 		}
   1110  1.144     joerg 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1111    1.2    itojun 
   1112  1.143  christos 		/*
   1113  1.198     ozaki 		 * When called from rt_ifa_addlocal, we cannot depend on that
   1114  1.198     ozaki 		 * the address (rt_getkey(rt)) exits in the address list of the
   1115  1.198     ozaki 		 * interface. So check RTF_LOCAL instead.
   1116  1.198     ozaki 		 */
   1117  1.198     ozaki 		if (rt->rt_flags & RTF_LOCAL) {
   1118  1.198     ozaki 			if (nd6_useloopback)
   1119  1.198     ozaki 				rt->rt_ifp = lo0ifp;	/* XXX */
   1120  1.198     ozaki 			break;
   1121  1.198     ozaki 		}
   1122  1.198     ozaki 
   1123  1.198     ozaki 		/*
   1124  1.117    dyoung 		 * check if rt_getkey(rt) is an address assigned
   1125    1.2    itojun 		 * to the interface.
   1126    1.2    itojun 		 */
   1127  1.238     ozaki 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp,
   1128  1.238     ozaki 		    &satocsin6(rt_getkey(rt))->sin6_addr, &psref);
   1129  1.129    dyoung 		if (ifa != NULL) {
   1130    1.2    itojun 			if (nd6_useloopback) {
   1131  1.195     ozaki 				rt->rt_ifp = lo0ifp;	/* XXX */
   1132    1.2    itojun 				/*
   1133    1.2    itojun 				 * Make sure rt_ifa be equal to the ifaddr
   1134    1.2    itojun 				 * corresponding to the address.
   1135    1.2    itojun 				 * We need this because when we refer
   1136    1.2    itojun 				 * rt_ifa->ia6_flags in ip6_input, we assume
   1137    1.2    itojun 				 * that the rt_ifa points to the address instead
   1138    1.2    itojun 				 * of the loopback address.
   1139    1.2    itojun 				 */
   1140  1.251     ozaki 				if (!ISSET(info->rti_flags, RTF_DONTCHANGEIFA)
   1141  1.251     ozaki 				    && ifa != rt->rt_ifa)
   1142  1.106    dyoung 					rt_replace_ifa(rt, ifa);
   1143    1.2    itojun 			}
   1144   1.18    itojun 		} else if (rt->rt_flags & RTF_ANNOUNCE) {
   1145   1.18    itojun 			/* join solicited node multicast for proxy ND */
   1146   1.18    itojun 			if (ifp->if_flags & IFF_MULTICAST) {
   1147   1.18    itojun 				struct in6_addr llsol;
   1148   1.18    itojun 				int error;
   1149   1.18    itojun 
   1150  1.118    dyoung 				llsol = satocsin6(rt_getkey(rt))->sin6_addr;
   1151   1.96    rpaulo 				llsol.s6_addr32[0] = htonl(0xff020000);
   1152   1.18    itojun 				llsol.s6_addr32[1] = 0;
   1153   1.18    itojun 				llsol.s6_addr32[2] = htonl(1);
   1154   1.18    itojun 				llsol.s6_addr8[12] = 0xff;
   1155   1.96    rpaulo 				if (in6_setscope(&llsol, ifp, NULL))
   1156  1.188     ozaki 					goto out;
   1157   1.99    rpaulo 				if (!in6_addmulti(&llsol, ifp, &error, 0)) {
   1158  1.225       ryo 					char ip6buf[INET6_ADDRSTRLEN];
   1159  1.187     ozaki 					nd6log(LOG_ERR, "%s: failed to join "
   1160   1.54    itojun 					    "%s (errno=%d)\n", if_name(ifp),
   1161  1.226  christos 					    IN6_PRINT(ip6buf, &llsol), error);
   1162   1.46    itojun 				}
   1163   1.18    itojun 			}
   1164    1.2    itojun 		}
   1165  1.188     ozaki 	out:
   1166  1.238     ozaki 		ifa_release(ifa, &psref);
   1167  1.181     ozaki 		/*
   1168  1.181     ozaki 		 * If we have too many cache entries, initiate immediate
   1169  1.181     ozaki 		 * purging for some entries.
   1170  1.181     ozaki 		 */
   1171  1.188     ozaki 		if (rt->rt_ifp != NULL)
   1172  1.207     ozaki 			nd6_gc_neighbors(LLTABLE6(rt->rt_ifp), NULL);
   1173    1.2    itojun 		break;
   1174  1.205     ozaki 	    }
   1175    1.2    itojun 
   1176    1.2    itojun 	case RTM_DELETE:
   1177   1.18    itojun 		/* leave from solicited node multicast for proxy ND */
   1178   1.18    itojun 		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
   1179   1.18    itojun 		    (ifp->if_flags & IFF_MULTICAST) != 0) {
   1180   1.18    itojun 			struct in6_addr llsol;
   1181   1.18    itojun 
   1182  1.118    dyoung 			llsol = satocsin6(rt_getkey(rt))->sin6_addr;
   1183   1.96    rpaulo 			llsol.s6_addr32[0] = htonl(0xff020000);
   1184   1.18    itojun 			llsol.s6_addr32[1] = 0;
   1185   1.18    itojun 			llsol.s6_addr32[2] = htonl(1);
   1186   1.18    itojun 			llsol.s6_addr8[12] = 0xff;
   1187  1.249     ozaki 			if (in6_setscope(&llsol, ifp, NULL) == 0)
   1188  1.249     ozaki 				in6_lookup_and_delete_multi(&llsol, ifp);
   1189   1.18    itojun 		}
   1190  1.188     ozaki 		break;
   1191    1.2    itojun 	}
   1192    1.2    itojun }
   1193    1.2    itojun 
   1194  1.271       roy static void
   1195  1.271       roy nd6_setifflags(struct ifnet *ifp, uint32_t flags)
   1196    1.2    itojun {
   1197  1.271       roy 	struct nd_kifinfo *ndi = ND_IFINFO(ifp);
   1198  1.271       roy 	struct ifaddr *ifa;
   1199  1.271       roy 	struct in6_ifaddr *ia;
   1200  1.271       roy 	int s;
   1201    1.2    itojun 
   1202  1.271       roy 	if (ndi->flags & ND6_IFF_IFDISABLED && !(flags & ND6_IFF_IFDISABLED)) {
   1203   1.65    itojun 		/*
   1204  1.271       roy 		 * If the interface is marked as ND6_IFF_IFDISABLED and
   1205  1.271       roy 		 * has a link-local address with IN6_IFF_DUPLICATED,
   1206  1.271       roy 		 * do not clear ND6_IFF_IFDISABLED.
   1207  1.271       roy 		 * See RFC 4862, section 5.4.5.
   1208   1.65    itojun 		 */
   1209  1.271       roy 		bool duplicated_linklocal = false;
   1210  1.271       roy 
   1211  1.271       roy 		s = pserialize_read_enter();
   1212  1.271       roy 		IFADDR_READER_FOREACH(ifa, ifp) {
   1213  1.271       roy 			if (ifa->ifa_addr->sa_family != AF_INET6)
   1214  1.271       roy 				continue;
   1215  1.271       roy 			ia = (struct in6_ifaddr *)ifa;
   1216  1.271       roy 			if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
   1217  1.271       roy 			    IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
   1218  1.271       roy 			{
   1219  1.271       roy 				duplicated_linklocal = true;
   1220  1.108    dyoung 				break;
   1221  1.271       roy 			}
   1222  1.271       roy 		}
   1223  1.271       roy 		pserialize_read_exit(s);
   1224    1.2    itojun 
   1225  1.271       roy 		if (duplicated_linklocal) {
   1226  1.271       roy 			flags |= ND6_IFF_IFDISABLED;
   1227  1.271       roy 			log(LOG_ERR, "%s: Cannot enable an interface"
   1228  1.271       roy 			    " with a link-local address marked"
   1229  1.271       roy 			    " duplicate.\n", if_name(ifp));
   1230  1.271       roy 		} else {
   1231  1.271       roy 			ndi->flags &= ~ND6_IFF_IFDISABLED;
   1232  1.271       roy 			if (ifp->if_flags & IFF_UP)
   1233  1.271       roy 				in6_if_up(ifp);
   1234    1.2    itojun 		}
   1235  1.271       roy 	} else if (!(ndi->flags & ND6_IFF_IFDISABLED) &&
   1236  1.271       roy 	    (flags & ND6_IFF_IFDISABLED))
   1237  1.271       roy 	{
   1238  1.271       roy 		struct psref psref;
   1239  1.271       roy 		int bound = curlwp_bind();
   1240  1.271       roy 
   1241  1.271       roy 		/* Mark all IPv6 addresses as tentative. */
   1242    1.2    itojun 
   1243  1.271       roy 		ndi->flags |= ND6_IFF_IFDISABLED;
   1244  1.271       roy 		s = pserialize_read_enter();
   1245  1.271       roy 		IFADDR_READER_FOREACH(ifa, ifp) {
   1246  1.271       roy 			if (ifa->ifa_addr->sa_family != AF_INET6)
   1247  1.271       roy 				continue;
   1248  1.271       roy 			ifa_acquire(ifa, &psref);
   1249  1.271       roy 			pserialize_read_exit(s);
   1250    1.2    itojun 
   1251  1.271       roy 			nd6_dad_stop(ifa);
   1252    1.2    itojun 
   1253  1.271       roy 			ia = (struct in6_ifaddr *)ifa;
   1254  1.271       roy 			ia->ia6_flags |= IN6_IFF_TENTATIVE;
   1255   1.12    itojun 
   1256  1.271       roy 			s = pserialize_read_enter();
   1257  1.271       roy 			ifa_release(ifa, &psref);
   1258   1.99    rpaulo 		}
   1259  1.271       roy 		pserialize_read_exit(s);
   1260  1.271       roy 		curlwp_bindx(bound);
   1261  1.271       roy 	}
   1262   1.99    rpaulo 
   1263  1.271       roy 	if (flags & ND6_IFF_AUTO_LINKLOCAL) {
   1264  1.271       roy 		if (!(ndi->flags & ND6_IFF_AUTO_LINKLOCAL)) {
   1265  1.271       roy 			/* auto_linklocal 0->1 transition */
   1266  1.148       roy 
   1267  1.271       roy 			ndi->flags |= ND6_IFF_AUTO_LINKLOCAL;
   1268  1.271       roy 			in6_ifattach(ifp, NULL);
   1269  1.271       roy 		} else if (!(flags & ND6_IFF_IFDISABLED) &&
   1270  1.271       roy 		    ifp->if_flags & IFF_UP)
   1271  1.148       roy 		{
   1272  1.148       roy 			/*
   1273  1.271       roy 			 * When the IF already has
   1274  1.271       roy 			 * ND6_IFF_AUTO_LINKLOCAL, no link-local
   1275  1.271       roy 			 * address is assigned, and IFF_UP, try to
   1276  1.271       roy 			 * assign one.
   1277  1.148       roy 			 */
   1278  1.271       roy 			bool haslinklocal = 0;
   1279  1.148       roy 
   1280  1.205     ozaki 			s = pserialize_read_enter();
   1281  1.202     ozaki 			IFADDR_READER_FOREACH(ifa, ifp) {
   1282  1.271       roy 				if (ifa->ifa_addr->sa_family !=AF_INET6)
   1283  1.148       roy 					continue;
   1284  1.148       roy 				ia = (struct in6_ifaddr *)ifa;
   1285  1.271       roy 				if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))){
   1286  1.271       roy 					haslinklocal = true;
   1287  1.148       roy 					break;
   1288  1.148       roy 				}
   1289  1.148       roy 			}
   1290  1.205     ozaki 			pserialize_read_exit(s);
   1291  1.271       roy 			if (!haslinklocal)
   1292  1.271       roy 				in6_ifattach(ifp, NULL);
   1293  1.148       roy 		}
   1294  1.271       roy 	}
   1295  1.151       roy 
   1296  1.271       roy 	ndi->flags = flags;
   1297  1.271       roy }
   1298  1.151       roy 
   1299  1.271       roy int
   1300  1.271       roy nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp)
   1301  1.271       roy {
   1302  1.271       roy #ifdef OSIOCGIFINFO_IN6_90
   1303  1.271       roy 	struct in6_ndireq90 *ondi = (struct in6_ndireq90 *)data;
   1304  1.271       roy 	struct in6_ndifreq90 *ndif = (struct in6_ndifreq90 *)data;
   1305  1.271       roy #define OND	ondi->ndi
   1306  1.271       roy #endif
   1307  1.271       roy 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
   1308  1.271       roy 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
   1309  1.271       roy 	struct nd_kifinfo *ifndi = ND_IFINFO(ifp);
   1310  1.271       roy 	int error = 0;
   1311  1.271       roy #define ND     ndi->ndi
   1312  1.151       roy 
   1313  1.271       roy 	switch (cmd) {
   1314  1.271       roy #ifdef OSIOCSRTRFLUSH_IN6
   1315  1.271       roy 	case OSIOCGDRLST_IN6:		/* FALLTHROUGH */
   1316  1.271       roy 	case OSIOCGPRLST_IN6:		/* FALLTHROUGH */
   1317  1.271       roy 	case OSIOCSNDFLUSH_IN6:		/* FALLTHROUGH */
   1318  1.271       roy 	case OSIOCSPFXFLUSH_IN6:	/* FALLTHROUGH */
   1319  1.271       roy 	case OSIOCSRTRFLUSH_IN6:	/* FALLTHROUGH */
   1320  1.271       roy 		break;
   1321  1.271       roy 	case OSIOCGDEFIFACE_IN6:
   1322  1.271       roy 		ndif->ifindex = 0;
   1323   1.26    itojun 		break;
   1324  1.271       roy 	case OSIOCSDEFIFACE_IN6:
   1325  1.271       roy 		error = ENOTSUP;
   1326    1.2    itojun 		break;
   1327  1.271       roy #endif
   1328  1.271       roy #ifdef OSIOCGIFINFO_IN6
   1329  1.271       roy 	case OSIOCGIFINFO_IN6:		/* FALLTHROUGH */
   1330  1.271       roy #endif
   1331  1.271       roy #ifdef OSIOCGIFINFO_IN6_90
   1332  1.271       roy 	case OSIOCGIFINFO_IN6_90:
   1333  1.271       roy 		memset(&OND, 0, sizeof(OND));
   1334  1.271       roy 		OND.initialized = 1;
   1335  1.271       roy 		OND.chlim = ifndi->chlim;
   1336  1.271       roy 		OND.basereachable = ifndi->basereachable;
   1337  1.271       roy 		OND.retrans = ifndi->retrans;
   1338  1.271       roy 		OND.flags = ifndi->flags;
   1339  1.271       roy 		break;
   1340  1.271       roy 	case OSIOCSIFINFO_IN6_90:
   1341  1.271       roy 		/* Allow userland to set Neighour Unreachability Detection
   1342  1.271       roy 		 * timers. */
   1343  1.271       roy 		if (OND.chlim != 0)
   1344  1.271       roy 			ifndi->chlim = OND.chlim;
   1345  1.271       roy 		if (OND.basereachable != 0 &&
   1346  1.271       roy 		    OND.basereachable != ifndi->basereachable)
   1347  1.271       roy 		{
   1348  1.271       roy 			ifndi->basereachable = OND.basereachable;
   1349  1.271       roy 			ifndi->reachable = ND_COMPUTE_RTIME(OND.basereachable);
   1350    1.2    itojun 		}
   1351  1.271       roy 		if (OND.retrans != 0)
   1352  1.271       roy 			ifndi->retrans = OND.retrans;
   1353  1.271       roy 		/* Retain the old behaviour .... */
   1354  1.271       roy 		/* FALLTHROUGH */
   1355  1.271       roy 	case OSIOCSIFINFO_FLAGS_90:
   1356  1.271       roy 		nd6_setifflags(ifp, OND.flags);
   1357    1.2    itojun 		break;
   1358  1.271       roy #undef OND
   1359  1.252       roy #endif
   1360  1.271       roy 	case SIOCGIFINFO_IN6:
   1361  1.271       roy 		ND.chlim = ifndi->chlim;
   1362  1.271       roy 		ND.basereachable = ifndi->basereachable;
   1363  1.271       roy 		ND.retrans = ifndi->retrans;
   1364  1.271       roy 		ND.flags = ifndi->flags;
   1365  1.271       roy 		break;
   1366  1.271       roy 	case SIOCSIFINFO_IN6:
   1367  1.271       roy 		/* Allow userland to set Neighour Unreachability Detection
   1368  1.271       roy 		 * timers. */
   1369  1.271       roy 		if (ND.chlim != 0)
   1370  1.271       roy 			ifndi->chlim = ND.chlim;
   1371  1.271       roy 		if (ND.basereachable != 0 &&
   1372  1.271       roy 		    ND.basereachable != ifndi->basereachable)
   1373  1.271       roy 		{
   1374  1.271       roy 			ifndi->basereachable = ND.basereachable;
   1375  1.271       roy 			ifndi->reachable = ND_COMPUTE_RTIME(ND.basereachable);
   1376    1.2    itojun 		}
   1377  1.271       roy 		if (ND.retrans != 0)
   1378  1.271       roy 			ifndi->retrans = ND.retrans;
   1379  1.271       roy 		break;
   1380  1.271       roy 	case SIOCSIFINFO_FLAGS:
   1381  1.271       roy 		nd6_setifflags(ifp, ND.flags);
   1382    1.2    itojun 		break;
   1383  1.271       roy #undef ND
   1384    1.2    itojun 	case SIOCGNBRINFO_IN6:
   1385   1.99    rpaulo 	{
   1386  1.181     ozaki 		struct llentry *ln;
   1387   1.12    itojun 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
   1388   1.12    itojun 
   1389   1.96    rpaulo 		if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
   1390  1.112    dyoung 			return error;
   1391    1.2    itojun 
   1392  1.188     ozaki 		ln = nd6_lookup(&nb_addr, ifp, false);
   1393  1.165     ozaki 		if (ln == NULL) {
   1394   1.12    itojun 			error = EINVAL;
   1395   1.12    itojun 			break;
   1396   1.12    itojun 		}
   1397   1.12    itojun 		nbi->state = ln->ln_state;
   1398   1.12    itojun 		nbi->asked = ln->ln_asked;
   1399   1.12    itojun 		nbi->isrouter = ln->ln_router;
   1400  1.166     ozaki 		nbi->expire = ln->ln_expire ?
   1401  1.166     ozaki 		    time_mono_to_wall(ln->ln_expire) : 0;
   1402  1.188     ozaki 		LLE_RUNLOCK(ln);
   1403   1.63    itojun 
   1404   1.12    itojun 		break;
   1405   1.65    itojun 	}
   1406    1.2    itojun 	}
   1407  1.112    dyoung 	return error;
   1408    1.2    itojun }
   1409    1.2    itojun 
   1410  1.115    dyoung void
   1411  1.188     ozaki nd6_llinfo_release_pkts(struct llentry *ln, struct ifnet *ifp)
   1412  1.115    dyoung {
   1413  1.115    dyoung 	struct mbuf *m_hold, *m_hold_next;
   1414  1.188     ozaki 	struct sockaddr_in6 sin6;
   1415  1.188     ozaki 
   1416  1.188     ozaki 	LLE_WLOCK_ASSERT(ln);
   1417  1.188     ozaki 
   1418  1.188     ozaki 	sockaddr_in6_init(&sin6, &ln->r_l3addr.addr6, 0, 0, 0);
   1419  1.115    dyoung 
   1420  1.188     ozaki 	m_hold = ln->la_hold, ln->la_hold = NULL, ln->la_numheld = 0;
   1421  1.188     ozaki 
   1422  1.188     ozaki 	LLE_WUNLOCK(ln);
   1423  1.188     ozaki 	for (; m_hold != NULL; m_hold = m_hold_next) {
   1424  1.115    dyoung 		m_hold_next = m_hold->m_nextpkt;
   1425  1.115    dyoung 		m_hold->m_nextpkt = NULL;
   1426  1.115    dyoung 
   1427  1.115    dyoung 		/*
   1428  1.115    dyoung 		 * we assume ifp is not a p2p here, so
   1429  1.115    dyoung 		 * just set the 2nd argument as the
   1430  1.115    dyoung 		 * 1st one.
   1431  1.115    dyoung 		 */
   1432  1.227     ozaki 		ip6_if_output(ifp, ifp, m_hold, &sin6, NULL);
   1433  1.115    dyoung 	}
   1434  1.188     ozaki 	LLE_WLOCK(ln);
   1435  1.115    dyoung }
   1436  1.115    dyoung 
   1437    1.2    itojun /*
   1438    1.2    itojun  * Create neighbor cache entry and cache link-layer address,
   1439   1.52    itojun  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
   1440    1.2    itojun  */
   1441  1.165     ozaki void
   1442  1.105  christos nd6_cache_lladdr(
   1443  1.105  christos     struct ifnet *ifp,
   1444  1.105  christos     struct in6_addr *from,
   1445  1.105  christos     char *lladdr,
   1446  1.107  christos     int lladdrlen,
   1447  1.105  christos     int type,	/* ICMP6 type */
   1448  1.105  christos     int code	/* type dependent information */
   1449  1.105  christos )
   1450    1.2    itojun {
   1451  1.181     ozaki 	struct llentry *ln = NULL;
   1452    1.2    itojun 	int is_newentry;
   1453    1.2    itojun 	int do_update;
   1454    1.2    itojun 	int olladdr;
   1455    1.2    itojun 	int llchange;
   1456    1.2    itojun 	int newstate = 0;
   1457    1.2    itojun 
   1458  1.163     ozaki 	KASSERT(ifp != NULL);
   1459  1.163     ozaki 	KASSERT(from != NULL);
   1460    1.2    itojun 
   1461    1.2    itojun 	/* nothing must be updated for unspecified address */
   1462    1.2    itojun 	if (IN6_IS_ADDR_UNSPECIFIED(from))
   1463  1.165     ozaki 		return;
   1464    1.2    itojun 
   1465    1.2    itojun 	/*
   1466    1.2    itojun 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
   1467    1.2    itojun 	 * the caller.
   1468    1.2    itojun 	 *
   1469    1.2    itojun 	 * XXX If the link does not have link-layer adderss, what should
   1470    1.2    itojun 	 * we do? (ifp->if_addrlen == 0)
   1471    1.2    itojun 	 * Spec says nothing in sections for RA, RS and NA.  There's small
   1472    1.2    itojun 	 * description on it in NS section (RFC 2461 7.2.3).
   1473    1.2    itojun 	 */
   1474    1.2    itojun 
   1475  1.188     ozaki 	ln = nd6_lookup(from, ifp, true);
   1476  1.188     ozaki 	if (ln == NULL) {
   1477    1.2    itojun #if 0
   1478    1.2    itojun 		/* nothing must be done if there's no lladdr */
   1479    1.2    itojun 		if (!lladdr || !lladdrlen)
   1480    1.2    itojun 			return NULL;
   1481    1.2    itojun #endif
   1482    1.2    itojun 
   1483  1.188     ozaki 		ln = nd6_create(from, ifp);
   1484    1.2    itojun 		is_newentry = 1;
   1485   1.43    itojun 	} else {
   1486   1.43    itojun 		/* do nothing if static ndp is set */
   1487  1.188     ozaki 		if (ln->la_flags & LLE_STATIC) {
   1488  1.188     ozaki 			LLE_WUNLOCK(ln);
   1489  1.165     ozaki 			return;
   1490  1.165     ozaki 		}
   1491    1.2    itojun 		is_newentry = 0;
   1492   1.43    itojun 	}
   1493    1.2    itojun 
   1494  1.188     ozaki 	if (ln == NULL)
   1495  1.165     ozaki 		return;
   1496    1.2    itojun 
   1497  1.188     ozaki 	olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
   1498    1.2    itojun 	if (olladdr && lladdr) {
   1499  1.188     ozaki 		llchange = memcmp(lladdr, &ln->ll_addr, ifp->if_addrlen);
   1500    1.2    itojun 	} else
   1501    1.2    itojun 		llchange = 0;
   1502    1.2    itojun 
   1503    1.2    itojun 	/*
   1504    1.2    itojun 	 * newentry olladdr  lladdr  llchange	(*=record)
   1505    1.2    itojun 	 *	0	n	n	--	(1)
   1506    1.2    itojun 	 *	0	y	n	--	(2)
   1507    1.2    itojun 	 *	0	n	y	--	(3) * STALE
   1508    1.2    itojun 	 *	0	y	y	n	(4) *
   1509    1.2    itojun 	 *	0	y	y	y	(5) * STALE
   1510    1.2    itojun 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
   1511    1.2    itojun 	 *	1	--	y	--	(7) * STALE
   1512    1.2    itojun 	 */
   1513    1.2    itojun 
   1514   1.52    itojun 	if (lladdr) {		/* (3-5) and (7) */
   1515    1.2    itojun 		/*
   1516    1.2    itojun 		 * Record source link-layer address
   1517    1.2    itojun 		 * XXX is it dependent to ifp->if_type?
   1518    1.2    itojun 		 */
   1519  1.188     ozaki 		memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen);
   1520  1.188     ozaki 		ln->la_flags |= LLE_VALID;
   1521    1.2    itojun 	}
   1522    1.2    itojun 
   1523    1.2    itojun 	if (!is_newentry) {
   1524   1.65    itojun 		if ((!olladdr && lladdr) ||		/* (3) */
   1525   1.65    itojun 		    (olladdr && lladdr && llchange)) {	/* (5) */
   1526    1.2    itojun 			do_update = 1;
   1527    1.2    itojun 			newstate = ND6_LLINFO_STALE;
   1528   1.52    itojun 		} else					/* (1-2,4) */
   1529    1.2    itojun 			do_update = 0;
   1530    1.2    itojun 	} else {
   1531    1.2    itojun 		do_update = 1;
   1532   1.99    rpaulo 		if (lladdr == NULL)			/* (6) */
   1533    1.2    itojun 			newstate = ND6_LLINFO_NOSTATE;
   1534   1.52    itojun 		else					/* (7) */
   1535    1.2    itojun 			newstate = ND6_LLINFO_STALE;
   1536    1.2    itojun 	}
   1537    1.2    itojun 
   1538    1.2    itojun 	if (do_update) {
   1539    1.2    itojun 		/*
   1540    1.2    itojun 		 * Update the state of the neighbor cache.
   1541    1.2    itojun 		 */
   1542    1.2    itojun 		ln->ln_state = newstate;
   1543    1.2    itojun 
   1544    1.2    itojun 		if (ln->ln_state == ND6_LLINFO_STALE) {
   1545   1.44    itojun 			/*
   1546   1.44    itojun 			 * XXX: since nd6_output() below will cause
   1547   1.44    itojun 			 * state tansition to DELAY and reset the timer,
   1548   1.44    itojun 			 * we must set the timer now, although it is actually
   1549   1.44    itojun 			 * meaningless.
   1550   1.44    itojun 			 */
   1551  1.182     ozaki 			nd6_llinfo_settimer(ln, nd6_gctimer * hz);
   1552   1.44    itojun 
   1553  1.188     ozaki 			nd6_llinfo_release_pkts(ln, ifp);
   1554    1.2    itojun 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
   1555    1.2    itojun 			/* probe right away */
   1556   1.86    itojun 			nd6_llinfo_settimer((void *)ln, 0);
   1557    1.2    itojun 		}
   1558    1.2    itojun 	}
   1559    1.2    itojun 
   1560    1.2    itojun 	/*
   1561    1.2    itojun 	 * ICMP6 type dependent behavior.
   1562    1.2    itojun 	 *
   1563    1.2    itojun 	 * NS: clear IsRouter if new entry
   1564    1.2    itojun 	 * RS: clear IsRouter
   1565    1.2    itojun 	 * RA: set IsRouter if there's lladdr
   1566    1.2    itojun 	 * redir: clear IsRouter if new entry
   1567    1.2    itojun 	 *
   1568    1.2    itojun 	 * RA case, (1):
   1569    1.2    itojun 	 * The spec says that we must set IsRouter in the following cases:
   1570    1.2    itojun 	 * - If lladdr exist, set IsRouter.  This means (1-5).
   1571    1.2    itojun 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
   1572    1.2    itojun 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
   1573    1.2    itojun 	 * A quetion arises for (1) case.  (1) case has no lladdr in the
   1574    1.2    itojun 	 * neighbor cache, this is similar to (6).
   1575    1.2    itojun 	 * This case is rare but we figured that we MUST NOT set IsRouter.
   1576    1.2    itojun 	 *
   1577    1.2    itojun 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
   1578    1.8    itojun 	 *							D R
   1579    1.8    itojun 	 *	0	n	n	--	(1)	c   ?     s
   1580    1.8    itojun 	 *	0	y	n	--	(2)	c   s     s
   1581    1.8    itojun 	 *	0	n	y	--	(3)	c   s     s
   1582    1.8    itojun 	 *	0	y	y	n	(4)	c   s     s
   1583    1.8    itojun 	 *	0	y	y	y	(5)	c   s     s
   1584    1.8    itojun 	 *	1	--	n	--	(6) c	c 	c s
   1585    1.8    itojun 	 *	1	--	y	--	(7) c	c   s	c s
   1586    1.2    itojun 	 *
   1587    1.2    itojun 	 *					(c=clear s=set)
   1588    1.2    itojun 	 */
   1589    1.2    itojun 	switch (type & 0xff) {
   1590    1.2    itojun 	case ND_NEIGHBOR_SOLICIT:
   1591    1.2    itojun 		/*
   1592    1.2    itojun 		 * New entry must have is_router flag cleared.
   1593    1.2    itojun 		 */
   1594   1.52    itojun 		if (is_newentry)	/* (6-7) */
   1595    1.8    itojun 			ln->ln_router = 0;
   1596    1.8    itojun 		break;
   1597    1.8    itojun 	case ND_REDIRECT:
   1598    1.8    itojun 		/*
   1599    1.8    itojun 		 * If the icmp is a redirect to a better router, always set the
   1600   1.52    itojun 		 * is_router flag.  Otherwise, if the entry is newly created,
   1601   1.52    itojun 		 * clear the flag.  [RFC 2461, sec 8.3]
   1602    1.8    itojun 		 */
   1603    1.8    itojun 		if (code == ND_REDIRECT_ROUTER)
   1604    1.8    itojun 			ln->ln_router = 1;
   1605   1.52    itojun 		else if (is_newentry) /* (6-7) */
   1606    1.2    itojun 			ln->ln_router = 0;
   1607    1.2    itojun 		break;
   1608    1.2    itojun 	case ND_ROUTER_SOLICIT:
   1609    1.2    itojun 		/*
   1610    1.2    itojun 		 * is_router flag must always be cleared.
   1611    1.2    itojun 		 */
   1612    1.2    itojun 		ln->ln_router = 0;
   1613    1.2    itojun 		break;
   1614    1.2    itojun 	case ND_ROUTER_ADVERT:
   1615    1.2    itojun 		/*
   1616    1.2    itojun 		 * Mark an entry with lladdr as a router.
   1617    1.2    itojun 		 */
   1618   1.65    itojun 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
   1619   1.65    itojun 		    (is_newentry && lladdr)) {			/* (7) */
   1620    1.2    itojun 			ln->ln_router = 1;
   1621    1.2    itojun 		}
   1622    1.2    itojun 		break;
   1623    1.2    itojun 	}
   1624   1.47    itojun 
   1625  1.262       roy 	if (do_update && lladdr != NULL) {
   1626  1.259       roy 		struct sockaddr_in6 sin6;
   1627  1.259       roy 
   1628  1.259       roy 		sockaddr_in6_init(&sin6, from, 0, 0, 0);
   1629  1.259       roy 		rt_clonedmsg(is_newentry ? RTM_ADD : RTM_CHANGE,
   1630  1.267       roy 		    NULL, sin6tosa(&sin6), lladdr, ifp);
   1631  1.259       roy 	}
   1632  1.188     ozaki 
   1633  1.271       roy 	if (ln != NULL)
   1634  1.188     ozaki 		LLE_WUNLOCK(ln);
   1635  1.188     ozaki 
   1636  1.188     ozaki 	/*
   1637  1.188     ozaki 	 * If we have too many cache entries, initiate immediate
   1638  1.188     ozaki 	 * purging for some entries.
   1639  1.188     ozaki 	 */
   1640  1.188     ozaki 	if (is_newentry)
   1641  1.207     ozaki 		nd6_gc_neighbors(LLTABLE6(ifp), &ln->r_l3addr.addr6);
   1642    1.2    itojun }
   1643    1.2    itojun 
   1644    1.2    itojun static void
   1645  1.107  christos nd6_slowtimo(void *ignored_arg)
   1646    1.2    itojun {
   1647  1.271       roy 	struct nd_kifinfo *ndi;
   1648   1.58    itojun 	struct ifnet *ifp;
   1649  1.194     ozaki 	int s;
   1650    1.2    itojun 
   1651  1.239     ozaki 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
   1652  1.170     ozaki 	callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
   1653   1.20   thorpej 	    nd6_slowtimo, NULL);
   1654  1.194     ozaki 
   1655  1.194     ozaki 	s = pserialize_read_enter();
   1656  1.194     ozaki 	IFNET_READER_FOREACH(ifp) {
   1657  1.271       roy 		ndi = ND_IFINFO(ifp);
   1658  1.271       roy 		if (ndi->basereachable && /* already initialized */
   1659  1.271       roy 		    (ndi->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
   1660    1.2    itojun 			/*
   1661    1.2    itojun 			 * Since reachable time rarely changes by router
   1662    1.2    itojun 			 * advertisements, we SHOULD insure that a new random
   1663    1.2    itojun 			 * value gets recomputed at least once every few hours.
   1664    1.2    itojun 			 * (RFC 2461, 6.3.4)
   1665    1.2    itojun 			 */
   1666  1.271       roy 			ndi->recalctm = nd6_recalc_reachtm_interval;
   1667  1.271       roy 			ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
   1668    1.2    itojun 		}
   1669    1.2    itojun 	}
   1670  1.194     ozaki 	pserialize_read_exit(s);
   1671  1.194     ozaki 
   1672  1.239     ozaki 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
   1673    1.2    itojun }
   1674    1.2    itojun 
   1675  1.227     ozaki /*
   1676  1.227     ozaki  * Return 0 if a neighbor cache is found. Return EWOULDBLOCK if a cache is not
   1677  1.227     ozaki  * found and trying to resolve a neighbor; in this case the mbuf is queued in
   1678  1.227     ozaki  * the list. Otherwise return errno after freeing the mbuf.
   1679  1.227     ozaki  */
   1680  1.188     ozaki int
   1681  1.227     ozaki nd6_resolve(struct ifnet *ifp, const struct rtentry *rt, struct mbuf *m,
   1682  1.227     ozaki     const struct sockaddr *_dst, uint8_t *lldst, size_t dstsize)
   1683  1.176     ozaki {
   1684  1.181     ozaki 	struct llentry *ln = NULL;
   1685  1.188     ozaki 	bool created = false;
   1686  1.227     ozaki 	const struct sockaddr_in6 *dst = satocsin6(_dst);
   1687  1.263       roy 	int error;
   1688  1.271       roy 	struct nd_kifinfo *ndi = ND_IFINFO(ifp);
   1689  1.193     ozaki 
   1690  1.227     ozaki 	/* discard the packet if IPv6 operation is disabled on the interface */
   1691  1.271       roy 	if (ndi->flags & ND6_IFF_IFDISABLED) {
   1692  1.227     ozaki 		m_freem(m);
   1693  1.227     ozaki 		return ENETDOWN; /* better error? */
   1694  1.193     ozaki 	}
   1695  1.193     ozaki 
   1696    1.2    itojun 	/*
   1697    1.2    itojun 	 * Address resolution or Neighbor Unreachability Detection
   1698    1.2    itojun 	 * for the next hop.
   1699    1.2    itojun 	 * At this point, the destination of the packet must be a unicast
   1700    1.2    itojun 	 * or an anycast address(i.e. not a multicast).
   1701    1.2    itojun 	 */
   1702    1.2    itojun 
   1703    1.2    itojun 	/* Look up the neighbor cache for the nexthop */
   1704  1.227     ozaki 	ln = nd6_lookup(&dst->sin6_addr, ifp, false);
   1705  1.227     ozaki 
   1706  1.255     ozaki 	if (ln != NULL && (ln->la_flags & LLE_VALID) != 0 &&
   1707  1.255     ozaki 	    ln->ln_state == ND6_LLINFO_REACHABLE) {
   1708  1.227     ozaki 		/* Fast path */
   1709  1.227     ozaki 		memcpy(lldst, &ln->ll_addr, MIN(dstsize, ifp->if_addrlen));
   1710  1.227     ozaki 		LLE_RUNLOCK(ln);
   1711  1.227     ozaki 		return 0;
   1712  1.227     ozaki 	}
   1713  1.227     ozaki 	if (ln != NULL)
   1714  1.227     ozaki 		LLE_RUNLOCK(ln);
   1715  1.227     ozaki 
   1716  1.227     ozaki 	/* Slow path */
   1717  1.188     ozaki 	ln = nd6_lookup(&dst->sin6_addr, ifp, true);
   1718  1.227     ozaki 	if (ln == NULL && nd6_is_addr_neighbor(dst, ifp))  {
   1719   1.29    itojun 		/*
   1720   1.29    itojun 		 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
   1721   1.52    itojun 		 * the condition below is not very efficient.  But we believe
   1722   1.29    itojun 		 * it is tolerable, because this should be a rare case.
   1723   1.29    itojun 		 */
   1724  1.188     ozaki 		ln = nd6_create(&dst->sin6_addr, ifp);
   1725  1.227     ozaki 		if (ln == NULL) {
   1726  1.225       ryo 			char ip6buf[INET6_ADDRSTRLEN];
   1727   1.29    itojun 			log(LOG_DEBUG,
   1728  1.227     ozaki 			    "%s: can't allocate llinfo for %s "
   1729  1.227     ozaki 			    "(ln=%p, rt=%p)\n", __func__,
   1730  1.226  christos 			    IN6_PRINT(ip6buf, &dst->sin6_addr), ln, rt);
   1731  1.227     ozaki 			m_freem(m);
   1732  1.227     ozaki 			return ENOBUFS;
   1733   1.29    itojun 		}
   1734  1.227     ozaki 		created = true;
   1735    1.2    itojun 	}
   1736    1.2    itojun 
   1737  1.236     ozaki 	if (ln == NULL) {
   1738  1.236     ozaki 		m_freem(m);
   1739  1.236     ozaki 		return ENETDOWN; /* better error? */
   1740  1.236     ozaki 	}
   1741  1.236     ozaki 
   1742  1.188     ozaki 	LLE_WLOCK_ASSERT(ln);
   1743  1.188     ozaki 
   1744   1.26    itojun 	/* We don't have to do link-layer address resolution on a p2p link. */
   1745   1.26    itojun 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
   1746   1.42    itojun 	    ln->ln_state < ND6_LLINFO_REACHABLE) {
   1747   1.26    itojun 		ln->ln_state = ND6_LLINFO_STALE;
   1748  1.182     ozaki 		nd6_llinfo_settimer(ln, nd6_gctimer * hz);
   1749   1.42    itojun 	}
   1750    1.2    itojun 
   1751    1.2    itojun 	/*
   1752    1.2    itojun 	 * The first time we send a packet to a neighbor whose entry is
   1753    1.2    itojun 	 * STALE, we have to change the state to DELAY and a sets a timer to
   1754    1.2    itojun 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
   1755    1.2    itojun 	 * neighbor unreachability detection on expiration.
   1756    1.2    itojun 	 * (RFC 2461 7.3.3)
   1757    1.2    itojun 	 */
   1758    1.2    itojun 	if (ln->ln_state == ND6_LLINFO_STALE) {
   1759    1.2    itojun 		ln->ln_asked = 0;
   1760    1.2    itojun 		ln->ln_state = ND6_LLINFO_DELAY;
   1761  1.182     ozaki 		nd6_llinfo_settimer(ln, nd6_delay * hz);
   1762    1.2    itojun 	}
   1763    1.2    itojun 
   1764    1.2    itojun 	/*
   1765  1.255     ozaki 	 * If the neighbor cache entry has a state other than INCOMPLETE
   1766  1.255     ozaki 	 * (i.e. its link-layer address is already resolved), just
   1767  1.255     ozaki 	 * send the packet.
   1768  1.255     ozaki 	 */
   1769  1.255     ozaki 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE) {
   1770  1.255     ozaki 		KASSERT((ln->la_flags & LLE_VALID) != 0);
   1771  1.255     ozaki 		memcpy(lldst, &ln->ll_addr, MIN(dstsize, ifp->if_addrlen));
   1772  1.255     ozaki 		LLE_WUNLOCK(ln);
   1773  1.255     ozaki 		return 0;
   1774  1.255     ozaki 	}
   1775  1.255     ozaki 
   1776  1.255     ozaki 	/*
   1777    1.2    itojun 	 * There is a neighbor cache entry, but no ethernet address
   1778   1.99    rpaulo 	 * response yet.  Append this latest packet to the end of the
   1779   1.99    rpaulo 	 * packet queue in the mbuf, unless the number of the packet
   1780   1.99    rpaulo 	 * does not exceed nd6_maxqueuelen.  When it exceeds nd6_maxqueuelen,
   1781   1.99    rpaulo 	 * the oldest packet in the queue will be removed.
   1782    1.2    itojun 	 */
   1783  1.263       roy 	if (ln->ln_state == ND6_LLINFO_NOSTATE ||
   1784  1.263       roy 	    ln->ln_state == ND6_LLINFO_WAITDELETE)
   1785    1.2    itojun 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
   1786   1.99    rpaulo 	if (ln->ln_hold) {
   1787   1.99    rpaulo 		struct mbuf *m_hold;
   1788   1.99    rpaulo 		int i;
   1789   1.99    rpaulo 
   1790   1.99    rpaulo 		i = 0;
   1791   1.99    rpaulo 		for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) {
   1792   1.99    rpaulo 			i++;
   1793   1.99    rpaulo 			if (m_hold->m_nextpkt == NULL) {
   1794   1.99    rpaulo 				m_hold->m_nextpkt = m;
   1795   1.99    rpaulo 				break;
   1796   1.99    rpaulo 			}
   1797   1.99    rpaulo 		}
   1798   1.99    rpaulo 		while (i >= nd6_maxqueuelen) {
   1799   1.99    rpaulo 			m_hold = ln->ln_hold;
   1800   1.99    rpaulo 			ln->ln_hold = ln->ln_hold->m_nextpkt;
   1801  1.100    rpaulo 			m_freem(m_hold);
   1802   1.99    rpaulo 			i--;
   1803   1.99    rpaulo 		}
   1804   1.99    rpaulo 	} else {
   1805   1.99    rpaulo 		ln->ln_hold = m;
   1806   1.99    rpaulo 	}
   1807   1.99    rpaulo 
   1808  1.263       roy 	if (ln->ln_asked >= nd6_mmaxtries)
   1809  1.263       roy 		error = (rt != NULL && rt->rt_flags & RTF_GATEWAY) ?
   1810  1.263       roy 		    EHOSTUNREACH : EHOSTDOWN;
   1811  1.263       roy 	else
   1812  1.263       roy 		error = EWOULDBLOCK;
   1813  1.263       roy 
   1814   1.64    itojun 	/*
   1815   1.64    itojun 	 * If there has been no NS for the neighbor after entering the
   1816   1.64    itojun 	 * INCOMPLETE state, send the first solicitation.
   1817   1.64    itojun 	 */
   1818   1.86    itojun 	if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
   1819  1.179     ozaki 		struct in6_addr src, *psrc;
   1820  1.179     ozaki 
   1821   1.64    itojun 		ln->ln_asked++;
   1822  1.271       roy 		nd6_llinfo_settimer(ln, ndi->retrans * hz / 1000);
   1823  1.179     ozaki 		psrc = nd6_llinfo_get_holdsrc(ln, &src);
   1824  1.188     ozaki 		LLE_WUNLOCK(ln);
   1825  1.247       roy 		nd6_ns_output(ifp, NULL, &dst->sin6_addr, psrc, NULL);
   1826  1.262       roy 	} else
   1827  1.188     ozaki 		LLE_WUNLOCK(ln);
   1828  1.188     ozaki 
   1829  1.188     ozaki 	if (created)
   1830  1.207     ozaki 		nd6_gc_neighbors(LLTABLE6(ifp), &dst->sin6_addr);
   1831  1.165     ozaki 
   1832  1.263       roy 	return error;
   1833   1.63    itojun }
   1834   1.54    itojun 
   1835   1.54    itojun int
   1836  1.112    dyoung nd6_need_cache(struct ifnet *ifp)
   1837   1.54    itojun {
   1838   1.54    itojun 	/*
   1839   1.54    itojun 	 * XXX: we currently do not make neighbor cache on any interface
   1840  1.266   thorpej 	 * other than ARCnet, Ethernet, and GIF.
   1841   1.54    itojun 	 *
   1842   1.54    itojun 	 * RFC2893 says:
   1843   1.54    itojun 	 * - unidirectional tunnels needs no ND
   1844   1.54    itojun 	 */
   1845   1.54    itojun 	switch (ifp->if_type) {
   1846   1.54    itojun 	case IFT_ARCNET:
   1847   1.54    itojun 	case IFT_ETHER:
   1848   1.54    itojun 	case IFT_IEEE1394:
   1849  1.102  liamjfoy 	case IFT_CARP:
   1850   1.54    itojun 	case IFT_GIF:		/* XXX need more cases? */
   1851   1.99    rpaulo 	case IFT_PPP:
   1852   1.99    rpaulo 	case IFT_TUNNEL:
   1853  1.112    dyoung 		return 1;
   1854   1.54    itojun 	default:
   1855  1.112    dyoung 		return 0;
   1856   1.54    itojun 	}
   1857   1.54    itojun }
   1858    1.2    itojun 
   1859  1.100    rpaulo static void
   1860  1.181     ozaki clear_llinfo_pqueue(struct llentry *ln)
   1861  1.100    rpaulo {
   1862  1.100    rpaulo 	struct mbuf *m_hold, *m_hold_next;
   1863  1.100    rpaulo 
   1864  1.100    rpaulo 	for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
   1865  1.100    rpaulo 		m_hold_next = m_hold->m_nextpkt;
   1866  1.100    rpaulo 		m_hold->m_nextpkt = NULL;
   1867  1.100    rpaulo 		m_freem(m_hold);
   1868  1.100    rpaulo 	}
   1869  1.100    rpaulo 
   1870  1.100    rpaulo 	ln->ln_hold = NULL;
   1871  1.100    rpaulo 	return;
   1872  1.100    rpaulo }
   1873  1.271       roy 
   1874   1.65    itojun int
   1875  1.105  christos nd6_sysctl(
   1876  1.105  christos     int name,
   1877  1.105  christos     void *oldp,	/* syscall arg, need copyout */
   1878  1.105  christos     size_t *oldlenp,
   1879  1.105  christos     void *newp,	/* syscall arg, need copyin */
   1880  1.107  christos     size_t newlen
   1881  1.105  christos )
   1882   1.65    itojun {
   1883   1.65    itojun 
   1884   1.65    itojun 	if (newp)
   1885   1.65    itojun 		return EPERM;
   1886  1.241  pgoyette 
   1887   1.65    itojun 	switch (name) {
   1888  1.271       roy #ifdef COMPAT_90
   1889  1.271       roy 	case OICMPV6CTL_ND6_DRLIST: /* FALLTHROUGH */
   1890  1.271       roy 	case OICMPV6CTL_ND6_PRLIST:
   1891  1.271       roy 		*oldlenp = 0;
   1892  1.271       roy 		return 0;
   1893  1.271       roy #endif
   1894   1.99    rpaulo 	case ICMPV6CTL_ND6_MAXQLEN:
   1895  1.242  pgoyette 		return 0;
   1896   1.65    itojun 	default:
   1897  1.242  pgoyette 		return ENOPROTOOPT;
   1898   1.65    itojun 	}
   1899  1.212     ozaki }
   1900