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