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