Home | History | Annotate | Line # | Download | only in netinet6
nd6_nbr.c revision 1.153
      1 /*	$NetBSD: nd6_nbr.c,v 1.153 2018/03/19 03:14:08 ozaki-r Exp $	*/
      2 /*	$KAME: nd6_nbr.c,v 1.61 2001/02/10 16:06:14 jinmei 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_nbr.c,v 1.153 2018/03/19 03:14:08 ozaki-r Exp $");
     35 
     36 #ifdef _KERNEL_OPT
     37 #include "opt_inet.h"
     38 #include "opt_net_mpsafe.h"
     39 #endif
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kmem.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/socket.h>
     46 #include <sys/socketvar.h>
     47 #include <sys/sockio.h>
     48 #include <sys/time.h>
     49 #include <sys/kernel.h>
     50 #include <sys/errno.h>
     51 #include <sys/ioctl.h>
     52 #include <sys/syslog.h>
     53 #include <sys/queue.h>
     54 #include <sys/callout.h>
     55 #include <sys/cprng.h>
     56 
     57 #include <net/if.h>
     58 #include <net/if_types.h>
     59 #include <net/if_dl.h>
     60 #include <net/route.h>
     61 
     62 #include <netinet/in.h>
     63 #include <netinet/in_var.h>
     64 #include <netinet6/in6_var.h>
     65 #include <netinet6/in6_ifattach.h>
     66 #include <netinet/ip6.h>
     67 #include <netinet6/ip6_var.h>
     68 #include <netinet6/scope6_var.h>
     69 #include <netinet6/nd6.h>
     70 #include <netinet/icmp6.h>
     71 #include <netinet6/icmp6_private.h>
     72 
     73 #include "carp.h"
     74 #if NCARP > 0
     75 #include <netinet/ip_carp.h>
     76 #endif
     77 
     78 #include <net/net_osdep.h>
     79 
     80 struct dadq;
     81 static struct dadq *nd6_dad_find(struct ifaddr *, struct nd_opt_nonce *);
     82 static void nd6_dad_starttimer(struct dadq *, int);
     83 static void nd6_dad_destroytimer(struct dadq *);
     84 static void nd6_dad_timer(struct dadq *);
     85 static void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
     86 static void nd6_dad_ns_input(struct ifaddr *, struct nd_opt_nonce *);
     87 static void nd6_dad_na_input(struct ifaddr *);
     88 static void nd6_dad_duplicated(struct dadq *);
     89 
     90 static int dad_maxtry = 15;	/* max # of *tries* to transmit DAD packet */
     91 
     92 /*
     93  * Input a Neighbor Solicitation Message.
     94  *
     95  * Based on RFC 2461
     96  * Based on RFC 2462 (duplicate address detection)
     97  */
     98 void
     99 nd6_ns_input(struct mbuf *m, int off, int icmp6len)
    100 {
    101 	struct ifnet *ifp;
    102 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    103 	struct nd_neighbor_solicit *nd_ns;
    104 	struct in6_addr saddr6 = ip6->ip6_src;
    105 	struct in6_addr daddr6 = ip6->ip6_dst;
    106 	struct in6_addr taddr6;
    107 	struct in6_addr myaddr6;
    108 	char *lladdr = NULL;
    109 	struct ifaddr *ifa = NULL;
    110 	int lladdrlen = 0;
    111 	int anycast = 0, proxy = 0, tentative = 0;
    112 	int router = ip6_forwarding;
    113 	int tlladdr;
    114 	union nd_opts ndopts;
    115 	const struct sockaddr_dl *proxydl = NULL;
    116 	struct psref psref;
    117 	struct psref psref_ia;
    118 	char ip6buf[INET6_ADDRSTRLEN], ip6buf2[INET6_ADDRSTRLEN];
    119 
    120 	ifp = m_get_rcvif_psref(m, &psref);
    121 	if (ifp == NULL)
    122 		goto freeit;
    123 
    124 	IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
    125 	if (nd_ns == NULL) {
    126 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    127 		m_put_rcvif_psref(ifp, &psref);
    128 		return;
    129 	}
    130 	ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
    131 	taddr6 = nd_ns->nd_ns_target;
    132 	if (in6_setscope(&taddr6, ifp, NULL) != 0)
    133 		goto bad;
    134 
    135 	if (ip6->ip6_hlim != 255) {
    136 		nd6log(LOG_ERR, "invalid hlim (%d) from %s to %s on %s\n",
    137 		    ip6->ip6_hlim, IN6_PRINT(ip6buf, &ip6->ip6_src),
    138 		    IN6_PRINT(ip6buf2, &ip6->ip6_dst), if_name(ifp));
    139 		goto bad;
    140 	}
    141 
    142 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
    143 		/* dst has to be a solicited node multicast address. */
    144 		/* don't check ifindex portion */
    145 		if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
    146 		    daddr6.s6_addr32[1] == 0 &&
    147 		    daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
    148 		    daddr6.s6_addr8[12] == 0xff) {
    149 			; /* good */
    150 		} else {
    151 			nd6log(LOG_INFO, "bad DAD packet (wrong ip6 dst)\n");
    152 			goto bad;
    153 		}
    154 	} else {
    155 		struct sockaddr_in6 ssin6;
    156 
    157 		/*
    158 		 * Make sure the source address is from a neighbor's address.
    159 		 */
    160 		sockaddr_in6_init(&ssin6, &saddr6, 0, 0, 0);
    161 		if (nd6_is_addr_neighbor(&ssin6, ifp) == 0) {
    162 			nd6log(LOG_INFO,
    163 			    "NS packet from non-neighbor %s on %s\n",
    164 			    IN6_PRINT(ip6buf, &saddr6), if_name(ifp));
    165 			goto bad;
    166 		}
    167 	}
    168 
    169 
    170 	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
    171 		nd6log(LOG_INFO, "bad NS target (multicast)\n");
    172 		goto bad;
    173 	}
    174 
    175 	icmp6len -= sizeof(*nd_ns);
    176 	nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
    177 	if (nd6_options(&ndopts) < 0) {
    178 		nd6log(LOG_INFO, "invalid ND option, ignored\n");
    179 		/* nd6_options have incremented stats */
    180 		goto freeit;
    181 	}
    182 
    183 	if (ndopts.nd_opts_src_lladdr) {
    184 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
    185 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
    186 	}
    187 
    188 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
    189 		nd6log(LOG_INFO,
    190 		    "bad DAD packet (link-layer address option)\n");
    191 		goto bad;
    192 	}
    193 
    194 	/*
    195 	 * Attaching target link-layer address to the NA?
    196 	 * (RFC 2461 7.2.4)
    197 	 *
    198 	 * NS IP dst is multicast			MUST add
    199 	 * Otherwise					MAY be omitted
    200 	 *
    201 	 * In this implementation, we omit the target link-layer address
    202 	 * in the "MAY" case.
    203 	 */
    204 #if 0 /* too much! */
    205 	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &daddr6);
    206 	if (ifa && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST))
    207 		tlladdr = 0;
    208 	else
    209 #endif
    210 	if (!IN6_IS_ADDR_MULTICAST(&daddr6))
    211 		tlladdr = 0;
    212 	else
    213 		tlladdr = 1;
    214 
    215 	/*
    216 	 * Target address (taddr6) must be either:
    217 	 * (1) Valid unicast/anycast address for my receiving interface,
    218 	 * (2) Unicast address for which I'm offering proxy service, or
    219 	 * (3) "tentative" address on which DAD is being performed.
    220 	 */
    221 	/* (1) and (3) check. */
    222 #if NCARP > 0
    223 	if (ifp->if_carp && ifp->if_type != IFT_CARP) {
    224 		int s = pserialize_read_enter();
    225 		ifa = carp_iamatch6(ifp->if_carp, &taddr6);
    226 		if (ifa != NULL)
    227 			ifa_acquire(ifa, &psref_ia);
    228 		pserialize_read_exit(s);
    229 	} else
    230 		ifa = NULL;
    231 	if (!ifa)
    232 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp, &taddr6,
    233 		    &psref_ia);
    234 #else
    235 	ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp, &taddr6,
    236 	    &psref_ia);
    237 #endif
    238 
    239 	/* (2) check. */
    240 	if (ifa == NULL) {
    241 		struct rtentry *rt;
    242 		struct sockaddr_in6 tsin6;
    243 
    244 		sockaddr_in6_init(&tsin6, &taddr6, 0, 0, 0);
    245 
    246 		rt = rtalloc1(sin6tosa(&tsin6), 0);
    247 		if (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
    248 		    rt->rt_gateway->sa_family == AF_LINK) {
    249 			/*
    250 			 * proxy NDP for single entry
    251 			 */
    252 			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal_psref(ifp,
    253 				IN6_IFF_NOTREADY|IN6_IFF_ANYCAST, &psref_ia);
    254 			if (ifa) {
    255 				proxy = 1;
    256 				proxydl = satocsdl(rt->rt_gateway);
    257 				router = 0;	/* XXX */
    258 			}
    259 		}
    260 		if (rt)
    261 			rt_unref(rt);
    262 	}
    263 	if (ifa == NULL) {
    264 		/*
    265 		 * We've got an NS packet, and we don't have that address
    266 		 * assigned for us.  We MUST silently ignore it.
    267 		 * See RFC2461 7.2.3.
    268 		 */
    269 		goto freeit;
    270 	}
    271 	myaddr6 = *IFA_IN6(ifa);
    272 	anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
    273 	tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
    274 	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
    275 		goto freeit;
    276 
    277 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
    278 		nd6log(LOG_INFO, "lladdrlen mismatch for %s "
    279 		    "(if %d, NS packet %d)\n",
    280 		    IN6_PRINT(ip6buf, &taddr6),
    281 		    ifp->if_addrlen, lladdrlen - 2);
    282 		goto bad;
    283 	}
    284 
    285 	if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
    286 		nd6log(LOG_INFO, "duplicate IP6 address %s\n",
    287 		    IN6_PRINT(ip6buf, &saddr6));
    288 		goto freeit;
    289 	}
    290 
    291 	/*
    292 	 * We have neighbor solicitation packet, with target address equals to
    293 	 * one of my tentative address.
    294 	 *
    295 	 * src addr	how to process?
    296 	 * ---		---
    297 	 * multicast	of course, invalid (rejected in ip6_input)
    298 	 * unicast	somebody is doing address resolution -> ignore
    299 	 * unspec	dup address detection
    300 	 *
    301 	 * The processing is defined in RFC 2462.
    302 	 */
    303 	if (tentative) {
    304 		/*
    305 		 * If source address is unspecified address, it is for
    306 		 * duplicate address detection.
    307 		 *
    308 		 * If not, the packet is for addess resolution;
    309 		 * silently ignore it.
    310 		 */
    311 		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
    312 			nd6_dad_ns_input(ifa, ndopts.nd_opts_nonce);
    313 		ifa_release(ifa, &psref_ia);
    314 		ifa = NULL;
    315 
    316 		goto freeit;
    317 	}
    318 	ifa_release(ifa, &psref_ia);
    319 	ifa = NULL;
    320 
    321 	/*
    322 	 * If the source address is unspecified address, entries must not
    323 	 * be created or updated.
    324 	 * It looks that sender is performing DAD.  Output NA toward
    325 	 * all-node multicast address, to tell the sender that I'm using
    326 	 * the address.
    327 	 * S bit ("solicited") must be zero.
    328 	 */
    329 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
    330 		struct in6_addr in6_all;
    331 
    332 		in6_all = in6addr_linklocal_allnodes;
    333 		if (in6_setscope(&in6_all, ifp, NULL) != 0)
    334 			goto bad;
    335 		nd6_na_output(ifp, &in6_all, &taddr6,
    336 		    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
    337 		    (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
    338 		    tlladdr, (const struct sockaddr *)proxydl);
    339 		goto freeit;
    340 	}
    341 
    342 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_NEIGHBOR_SOLICIT, 0);
    343 
    344 	nd6_na_output(ifp, &saddr6, &taddr6,
    345 	    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
    346 	    (router ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
    347 	    tlladdr, (const struct sockaddr *)proxydl);
    348  freeit:
    349 	ifa_release(ifa, &psref_ia);
    350 	m_put_rcvif_psref(ifp, &psref);
    351 	m_freem(m);
    352 	return;
    353 
    354  bad:
    355 	nd6log(LOG_ERR, "src=%s\n", IN6_PRINT(ip6buf, &saddr6));
    356 	nd6log(LOG_ERR, "dst=%s\n", IN6_PRINT(ip6buf, &daddr6));
    357 	nd6log(LOG_ERR, "tgt=%s\n", IN6_PRINT(ip6buf, &taddr6));
    358 	ICMP6_STATINC(ICMP6_STAT_BADNS);
    359 	ifa_release(ifa, &psref_ia);
    360 	m_put_rcvif_psref(ifp, &psref);
    361 	m_freem(m);
    362 }
    363 
    364 /*
    365  * Output a Neighbor Solicitation Message. Caller specifies:
    366  *	- ICMP6 header source IP6 address
    367  *	- ND6 header target IP6 address
    368  *	- ND6 header source datalink address
    369  *
    370  * Based on RFC 2461
    371  * Based on RFC 2462 (duplicate address detection)
    372  */
    373 void
    374 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
    375     const struct in6_addr *taddr6,
    376     struct in6_addr *hsrc,
    377     uint8_t *nonce		/* duplicate address detection */)
    378 {
    379 	struct mbuf *m;
    380 	struct ip6_hdr *ip6;
    381 	struct nd_neighbor_solicit *nd_ns;
    382 	struct in6_addr *src, src_in;
    383 	struct ip6_moptions im6o;
    384 	int icmp6len;
    385 	int maxlen;
    386 	const void *mac;
    387 	struct route ro;
    388 
    389 	if (IN6_IS_ADDR_MULTICAST(taddr6))
    390 		return;
    391 
    392 	memset(&ro, 0, sizeof(ro));
    393 
    394 	/* estimate the size of message */
    395 	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
    396 	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
    397 	KASSERTMSG(max_linkhdr + maxlen < MCLBYTES,
    398 	    "max_linkhdr + maxlen >= MCLBYTES (%d + %d >= %d)",
    399 	    max_linkhdr, maxlen, MCLBYTES);
    400 
    401 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    402 	if (m && max_linkhdr + maxlen >= MHLEN) {
    403 		MCLGET(m, M_DONTWAIT);
    404 		if ((m->m_flags & M_EXT) == 0) {
    405 			m_free(m);
    406 			m = NULL;
    407 		}
    408 	}
    409 	if (m == NULL)
    410 		return;
    411 	m_reset_rcvif(m);
    412 
    413 	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
    414 		m->m_flags |= M_MCAST;
    415 		im6o.im6o_multicast_if_index = if_get_index(ifp);
    416 		im6o.im6o_multicast_hlim = 255;
    417 		im6o.im6o_multicast_loop = 0;
    418 	}
    419 
    420 	icmp6len = sizeof(*nd_ns);
    421 	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
    422 	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
    423 
    424 	/* fill neighbor solicitation packet */
    425 	ip6 = mtod(m, struct ip6_hdr *);
    426 	ip6->ip6_flow = 0;
    427 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
    428 	ip6->ip6_vfc |= IPV6_VERSION;
    429 	/* ip6->ip6_plen will be set later */
    430 	ip6->ip6_nxt = IPPROTO_ICMPV6;
    431 	ip6->ip6_hlim = 255;
    432 	if (daddr6)
    433 		ip6->ip6_dst = *daddr6;
    434 	else {
    435 		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
    436 		ip6->ip6_dst.s6_addr16[1] = 0;
    437 		ip6->ip6_dst.s6_addr32[1] = 0;
    438 		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
    439 		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
    440 		ip6->ip6_dst.s6_addr8[12] = 0xff;
    441 		if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
    442 			goto bad;
    443 	}
    444 	if (nonce == NULL) {
    445 		int s;
    446 		/*
    447 		 * RFC2461 7.2.2:
    448 		 * "If the source address of the packet prompting the
    449 		 * solicitation is the same as one of the addresses assigned
    450 		 * to the outgoing interface, that address SHOULD be placed
    451 		 * in the IP Source Address of the outgoing solicitation.
    452 		 * Otherwise, any one of the addresses assigned to the
    453 		 * interface should be used."
    454 		 *
    455 		 * We use the source address for the prompting packet
    456 		 * (hsrc), if:
    457 		 * - hsrc is given from the caller (by giving "ln"), and
    458 		 * - hsrc belongs to the outgoing interface.
    459 		 * Otherwise, we perform the source address selection as usual.
    460 		 */
    461 		s = pserialize_read_enter();
    462 		if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc)) {
    463 			pserialize_read_exit(s);
    464 			src = hsrc;
    465 		} else {
    466 			int error;
    467 			struct sockaddr_in6 dst_sa;
    468 
    469 			pserialize_read_exit(s);
    470 
    471 			sockaddr_in6_init(&dst_sa, &ip6->ip6_dst, 0, 0, 0);
    472 
    473 			error = in6_selectsrc(&dst_sa, NULL,
    474 			    NULL, &ro, NULL, NULL, NULL, &src_in);
    475 			if (error != 0) {
    476 				char ip6buf[INET6_ADDRSTRLEN];
    477 				nd6log(LOG_DEBUG, "source can't be "
    478 				    "determined: dst=%s, error=%d\n",
    479 				    IN6_PRINT(ip6buf, &dst_sa.sin6_addr),
    480 				    error);
    481 				pserialize_read_exit(s);
    482 				goto bad;
    483 			}
    484 			src = &src_in;
    485 		}
    486 	} else {
    487 		/*
    488 		 * Source address for DAD packet must always be IPv6
    489 		 * unspecified address. (0::0)
    490 		 * We actually don't have to 0-clear the address (we did it
    491 		 * above), but we do so here explicitly to make the intention
    492 		 * clearer.
    493 		 */
    494 		memset(&src_in, 0, sizeof(src_in));
    495 		src = &src_in;
    496 	}
    497 	ip6->ip6_src = *src;
    498 	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
    499 	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
    500 	nd_ns->nd_ns_code = 0;
    501 	nd_ns->nd_ns_reserved = 0;
    502 	nd_ns->nd_ns_target = *taddr6;
    503 	in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
    504 
    505 	/*
    506 	 * Add source link-layer address option.
    507 	 *
    508 	 *				spec		implementation
    509 	 *				---		---
    510 	 * DAD packet			MUST NOT	do not add the option
    511 	 * there's no link layer address:
    512 	 *				impossible	do not add the option
    513 	 * there's link layer address:
    514 	 *	Multicast NS		MUST add one	add the option
    515 	 *	Unicast NS		SHOULD add one	add the option
    516 	 */
    517 	if (nonce == NULL && (mac = nd6_ifptomac(ifp))) {
    518 		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
    519 		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
    520 		/* 8 byte alignments... */
    521 		optlen = (optlen + 7) & ~7;
    522 
    523 		m->m_pkthdr.len += optlen;
    524 		m->m_len += optlen;
    525 		icmp6len += optlen;
    526 		memset((void *)nd_opt, 0, optlen);
    527 		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
    528 		nd_opt->nd_opt_len = optlen >> 3;
    529 		memcpy((void *)(nd_opt + 1), mac, ifp->if_addrlen);
    530 	}
    531 
    532 	/* Add a nonce option (RFC 3971) to detect looped back NS messages.
    533 	 * This behavior is documented in RFC 7527. */
    534 	if (nonce != NULL) {
    535 		int optlen = sizeof(struct nd_opt_hdr) + ND_OPT_NONCE_LEN;
    536 		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
    537 
    538 		/* 8-byte alignment is required. */
    539 		optlen = (optlen + 7) & ~7;
    540 		m->m_pkthdr.len += optlen;
    541 		m->m_len += optlen;
    542 		icmp6len += optlen;
    543 		memset(nd_opt, 0, optlen);
    544 		nd_opt->nd_opt_type = ND_OPT_NONCE;
    545 		nd_opt->nd_opt_len = optlen >> 3;
    546 		memcpy(nd_opt + 1, nonce, ND_OPT_NONCE_LEN);
    547 	}
    548 
    549 	ip6->ip6_plen = htons((u_int16_t)icmp6len);
    550 	nd_ns->nd_ns_cksum = 0;
    551 	nd_ns->nd_ns_cksum =
    552 	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
    553 
    554 	ip6_output(m, NULL, &ro, nonce != NULL ? IPV6_UNSPECSRC : 0,
    555 	    &im6o, NULL, NULL);
    556 	icmp6_ifstat_inc(ifp, ifs6_out_msg);
    557 	icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
    558 	ICMP6_STATINC(ICMP6_STAT_OUTHIST + ND_NEIGHBOR_SOLICIT);
    559 
    560 	rtcache_free(&ro);
    561 	return;
    562 
    563   bad:
    564 	rtcache_free(&ro);
    565 	m_freem(m);
    566 	return;
    567 }
    568 
    569 /*
    570  * Neighbor advertisement input handling.
    571  *
    572  * Based on RFC 2461
    573  * Based on RFC 2462 (duplicate address detection)
    574  *
    575  * the following items are not implemented yet:
    576  * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
    577  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
    578  */
    579 void
    580 nd6_na_input(struct mbuf *m, int off, int icmp6len)
    581 {
    582 	struct ifnet *ifp;
    583 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    584 	struct nd_neighbor_advert *nd_na;
    585 	struct in6_addr saddr6 = ip6->ip6_src;
    586 	struct in6_addr daddr6 = ip6->ip6_dst;
    587 	struct in6_addr taddr6;
    588 	int flags;
    589 	int is_router;
    590 	int is_solicited;
    591 	int is_override;
    592 	char *lladdr = NULL;
    593 	int lladdrlen = 0;
    594 	struct ifaddr *ifa;
    595 	struct llentry *ln = NULL;
    596 	union nd_opts ndopts;
    597 	struct sockaddr_in6 ssin6;
    598 	int rt_announce;
    599 	bool checklink = false;
    600 	struct psref psref;
    601 	struct psref psref_ia;
    602 	char ip6buf[INET6_ADDRSTRLEN], ip6buf2[INET6_ADDRSTRLEN];
    603 
    604 	ifp = m_get_rcvif_psref(m, &psref);
    605 	if (ifp == NULL)
    606 		goto freeit;
    607 
    608 	if (ip6->ip6_hlim != 255) {
    609 		nd6log(LOG_ERR,
    610 		    "invalid hlim (%d) from %s to %s on %s\n",
    611 		    ip6->ip6_hlim, IN6_PRINT(ip6buf, &ip6->ip6_src),
    612 		    IN6_PRINT(ip6buf2, &ip6->ip6_dst), if_name(ifp));
    613 		goto bad;
    614 	}
    615 
    616 	IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
    617 	if (nd_na == NULL) {
    618 		m_put_rcvif_psref(ifp, &psref);
    619 		ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
    620 		return;
    621 	}
    622 
    623 	flags = nd_na->nd_na_flags_reserved;
    624 	is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
    625 	is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
    626 	is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
    627 
    628 	taddr6 = nd_na->nd_na_target;
    629 	if (in6_setscope(&taddr6, ifp, NULL)) {
    630 		goto bad;
    631 	}
    632 
    633 	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
    634 		nd6log(LOG_ERR, "invalid target address %s\n",
    635 		    IN6_PRINT(ip6buf, &taddr6));
    636 		goto bad;
    637 	}
    638 	if (is_solicited && IN6_IS_ADDR_MULTICAST(&daddr6)) {
    639 		nd6log(LOG_ERR, "a solicited adv is multicasted\n");
    640 		goto bad;
    641 	}
    642 
    643 	icmp6len -= sizeof(*nd_na);
    644 	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
    645 	if (nd6_options(&ndopts) < 0) {
    646 		nd6log(LOG_INFO, "invalid ND option, ignored\n");
    647 		/* nd6_options have incremented stats */
    648 		goto freeit;
    649 	}
    650 
    651 	if (ndopts.nd_opts_tgt_lladdr) {
    652 		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
    653 		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
    654 	}
    655 
    656 	ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp, &taddr6, &psref_ia);
    657 
    658 	/*
    659 	 * Target address matches one of my interface address.
    660 	 *
    661 	 * If my address is tentative, this means that there's somebody
    662 	 * already using the same address as mine.  This indicates DAD failure.
    663 	 * This is defined in RFC 2462.
    664 	 *
    665 	 * Otherwise, process as defined in RFC 2461.
    666 	 */
    667 	if (ifa
    668 	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
    669 		nd6_dad_na_input(ifa);
    670 		ifa_release(ifa, &psref_ia);
    671 		ifa = NULL;
    672 		goto freeit;
    673 	}
    674 
    675 	/* Just for safety, maybe unnecessary. */
    676 	if (ifa) {
    677 		log(LOG_ERR,
    678 		    "nd6_na_input: duplicate IP6 address %s\n",
    679 		    IN6_PRINT(ip6buf, &taddr6));
    680 		ifa_release(ifa, &psref_ia);
    681 		ifa = NULL;
    682 		goto freeit;
    683 	}
    684 
    685 	/*
    686 	 * Make sure the source address is from a neighbor's address.
    687 	 */
    688 	sockaddr_in6_init(&ssin6, &saddr6, 0, 0, 0);
    689 	if (nd6_is_addr_neighbor(&ssin6, ifp) == 0) {
    690 		nd6log(LOG_INFO, "ND packet from non-neighbor %s on %s\n",
    691 		    IN6_PRINT(ip6buf, &saddr6), if_name(ifp));
    692 		goto bad;
    693 	}
    694 
    695 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
    696 		nd6log(LOG_INFO, "lladdrlen mismatch for %s "
    697 		    "(if %d, NA packet %d)\n", IN6_PRINT(ip6buf, &taddr6),
    698 		    ifp->if_addrlen, lladdrlen - 2);
    699 		goto bad;
    700 	}
    701 
    702 	/*
    703 	 * If no neighbor cache entry is found, NA SHOULD silently be
    704 	 * discarded.
    705 	 */
    706 	ln = nd6_lookup(&taddr6, ifp, true);
    707 	if (ln == NULL)
    708 		goto freeit;
    709 
    710 	rt_announce = 0;
    711 	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
    712 		/*
    713 		 * If the link-layer has address, and no lladdr option came,
    714 		 * discard the packet.
    715 		 */
    716 		if (ifp->if_addrlen && !lladdr)
    717 			goto freeit;
    718 
    719 		/*
    720 		 * Record link-layer address, and update the state.
    721 		 */
    722 		memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen);
    723 		ln->la_flags |= LLE_VALID;
    724 		rt_announce = 1;
    725 		if (is_solicited) {
    726 			ln->ln_state = ND6_LLINFO_REACHABLE;
    727 			ln->ln_byhint = 0;
    728 			if (!ND6_LLINFO_PERMANENT(ln)) {
    729 				nd6_llinfo_settimer(ln,
    730 				    ND_IFINFO(ln->lle_tbl->llt_ifp)->reachable * hz);
    731 			}
    732 		} else {
    733 			ln->ln_state = ND6_LLINFO_STALE;
    734 			nd6_llinfo_settimer(ln, nd6_gctimer * hz);
    735 		}
    736 		if ((ln->ln_router = is_router) != 0) {
    737 			/*
    738 			 * This means a router's state has changed from
    739 			 * non-reachable to probably reachable, and might
    740 			 * affect the status of associated prefixes..
    741 			 */
    742 			checklink = true;
    743 		}
    744 	} else {
    745 		int llchange;
    746 
    747 		/*
    748 		 * Check if the link-layer address has changed or not.
    749 		 */
    750 		if (lladdr == NULL)
    751 			llchange = 0;
    752 		else {
    753 			if (ln->la_flags & LLE_VALID) {
    754 				if (memcmp(lladdr, &ln->ll_addr, ifp->if_addrlen))
    755 					llchange = rt_announce = 1;
    756 				else
    757 					llchange = 0;
    758 			} else
    759 				llchange = rt_announce = 1;
    760 		}
    761 
    762 		/*
    763 		 * This is VERY complex.  Look at it with care.
    764 		 *
    765 		 * override solicit lladdr llchange	action
    766 		 *					(L: record lladdr)
    767 		 *
    768 		 *	0	0	n	--	(2c)
    769 		 *	0	0	y	n	(2b) L
    770 		 *	0	0	y	y	(1)    REACHABLE->STALE
    771 		 *	0	1	n	--	(2c)   *->REACHABLE
    772 		 *	0	1	y	n	(2b) L *->REACHABLE
    773 		 *	0	1	y	y	(1)    REACHABLE->STALE
    774 		 *	1	0	n	--	(2a)
    775 		 *	1	0	y	n	(2a) L
    776 		 *	1	0	y	y	(2a) L *->STALE
    777 		 *	1	1	n	--	(2a)   *->REACHABLE
    778 		 *	1	1	y	n	(2a) L *->REACHABLE
    779 		 *	1	1	y	y	(2a) L *->REACHABLE
    780 		 */
    781 		if (!is_override && lladdr != NULL && llchange) { /* (1) */
    782 			/*
    783 			 * If state is REACHABLE, make it STALE.
    784 			 * no other updates should be done.
    785 			 */
    786 			if (ln->ln_state == ND6_LLINFO_REACHABLE) {
    787 				ln->ln_state = ND6_LLINFO_STALE;
    788 				nd6_llinfo_settimer(ln, nd6_gctimer * hz);
    789 			}
    790 			goto freeit;
    791 		} else if (is_override				   /* (2a) */
    792 		    || (!is_override && lladdr != NULL && !llchange) /* (2b) */
    793 		    || lladdr == NULL) {			   /* (2c) */
    794 			/*
    795 			 * Update link-local address, if any.
    796 			 */
    797 			if (lladdr != NULL) {
    798 				memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen);
    799 				ln->la_flags |= LLE_VALID;
    800 			}
    801 
    802 			/*
    803 			 * If solicited, make the state REACHABLE.
    804 			 * If not solicited and the link-layer address was
    805 			 * changed, make it STALE.
    806 			 */
    807 			if (is_solicited) {
    808 				ln->ln_state = ND6_LLINFO_REACHABLE;
    809 				ln->ln_byhint = 0;
    810 				if (!ND6_LLINFO_PERMANENT(ln)) {
    811 					nd6_llinfo_settimer(ln,
    812 					    ND_IFINFO(ifp)->reachable * hz);
    813 				}
    814 			} else {
    815 				if (lladdr && llchange) {
    816 					ln->ln_state = ND6_LLINFO_STALE;
    817 					nd6_llinfo_settimer(ln,
    818 					    nd6_gctimer * hz);
    819 				}
    820 			}
    821 		}
    822 
    823 		if (ln->ln_router && !is_router) {
    824 			/*
    825 			 * The peer dropped the router flag.
    826 			 * Remove the sender from the Default Router List and
    827 			 * update the Destination Cache entries.
    828 			 */
    829 			struct nd_defrouter *dr;
    830 			const struct in6_addr *in6;
    831 
    832 			in6 = &ln->r_l3addr.addr6;
    833 
    834 			ND6_WLOCK();
    835 			dr = nd6_defrouter_lookup(in6, ln->lle_tbl->llt_ifp);
    836 			if (dr)
    837 				nd6_defrtrlist_del(dr, NULL);
    838 			else if (!ip6_forwarding) {
    839 				/*
    840 				 * Even if the neighbor is not in the default
    841 				 * router list, the neighbor may be used
    842 				 * as a next hop for some destinations
    843 				 * (e.g. redirect case). So we must
    844 				 * call nd6_rt_flush explicitly.
    845 				 */
    846 				nd6_rt_flush(&ip6->ip6_src, ln->lle_tbl->llt_ifp);
    847 			}
    848 			ND6_UNLOCK();
    849 		}
    850 		ln->ln_router = is_router;
    851 	}
    852         /*
    853 	 * XXX: does this matter?
    854 	 * rt->rt_flags &= ~RTF_REJECT;
    855 	 */
    856 	ln->ln_asked = 0;
    857 	nd6_llinfo_release_pkts(ln, ifp);
    858 	/* FIXME */
    859 #if 0
    860 	if (rt_announce) /* tell user process about any new lladdr */
    861 		rt_newmsg(RTM_CHANGE, rt);
    862 #endif
    863 
    864  freeit:
    865 	if (ln != NULL)
    866 		LLE_WUNLOCK(ln);
    867 
    868 	if (checklink) {
    869 		ND6_WLOCK();
    870 		nd6_pfxlist_onlink_check();
    871 		ND6_UNLOCK();
    872 	}
    873 
    874 	m_put_rcvif_psref(ifp, &psref);
    875 	m_freem(m);
    876 	return;
    877 
    878  bad:
    879 	if (ln != NULL)
    880 		LLE_WUNLOCK(ln);
    881 
    882 	ICMP6_STATINC(ICMP6_STAT_BADNA);
    883 	m_put_rcvif_psref(ifp, &psref);
    884 	m_freem(m);
    885 }
    886 
    887 /*
    888  * Neighbor advertisement output handling.
    889  *
    890  * Based on RFC 2461
    891  *
    892  * the following items are not implemented yet:
    893  * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
    894  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
    895  */
    896 void
    897 nd6_na_output(
    898 	struct ifnet *ifp,
    899 	const struct in6_addr *daddr6_0,
    900 	const struct in6_addr *taddr6,
    901 	u_long flags,
    902 	int tlladdr,		/* 1 if include target link-layer address */
    903 	const struct sockaddr *sdl0)	/* sockaddr_dl (= proxy NA) or NULL */
    904 {
    905 	struct mbuf *m;
    906 	struct ip6_hdr *ip6;
    907 	struct nd_neighbor_advert *nd_na;
    908 	struct ip6_moptions im6o;
    909 	struct sockaddr *dst;
    910 	union {
    911 		struct sockaddr		dst;
    912 		struct sockaddr_in6	dst6;
    913 	} u;
    914 	struct in6_addr daddr6;
    915 	int icmp6len, maxlen, error;
    916 	const void *mac;
    917 	struct route ro;
    918 
    919 	mac = NULL;
    920 	memset(&ro, 0, sizeof(ro));
    921 
    922 	daddr6 = *daddr6_0;	/* make a local copy for modification */
    923 
    924 	/* estimate the size of message */
    925 	maxlen = sizeof(*ip6) + sizeof(*nd_na);
    926 	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
    927 	KASSERTMSG(max_linkhdr + maxlen < MCLBYTES,
    928 	    "max_linkhdr + maxlen >= MCLBYTES (%d + %d >= %d)",
    929 	    max_linkhdr, maxlen, MCLBYTES);
    930 
    931 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    932 	if (m && max_linkhdr + maxlen >= MHLEN) {
    933 		MCLGET(m, M_DONTWAIT);
    934 		if ((m->m_flags & M_EXT) == 0) {
    935 			m_free(m);
    936 			m = NULL;
    937 		}
    938 	}
    939 	if (m == NULL)
    940 		return;
    941 	m_reset_rcvif(m);
    942 
    943 	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
    944 		m->m_flags |= M_MCAST;
    945 		im6o.im6o_multicast_if_index = if_get_index(ifp);
    946 		im6o.im6o_multicast_hlim = 255;
    947 		im6o.im6o_multicast_loop = 0;
    948 	}
    949 
    950 	icmp6len = sizeof(*nd_na);
    951 	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
    952 	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
    953 
    954 	/* fill neighbor advertisement packet */
    955 	ip6 = mtod(m, struct ip6_hdr *);
    956 	ip6->ip6_flow = 0;
    957 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
    958 	ip6->ip6_vfc |= IPV6_VERSION;
    959 	ip6->ip6_nxt = IPPROTO_ICMPV6;
    960 	ip6->ip6_hlim = 255;
    961 	if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
    962 		/* reply to DAD */
    963 		daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
    964 		daddr6.s6_addr16[1] = 0;
    965 		daddr6.s6_addr32[1] = 0;
    966 		daddr6.s6_addr32[2] = 0;
    967 		daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
    968 		if (in6_setscope(&daddr6, ifp, NULL))
    969 			goto bad;
    970 
    971 		flags &= ~ND_NA_FLAG_SOLICITED;
    972 	}
    973 	ip6->ip6_dst = daddr6;
    974 	sockaddr_in6_init(&u.dst6, &daddr6, 0, 0, 0);
    975 	dst = &u.dst;
    976 	if (rtcache_setdst(&ro, dst) != 0)
    977 		goto bad;
    978 
    979 	/*
    980 	 * Select a source whose scope is the same as that of the dest.
    981 	 */
    982 	error = in6_selectsrc(satosin6(dst), NULL, NULL, &ro, NULL, NULL, NULL,
    983 	    &ip6->ip6_src);
    984 	if (error != 0) {
    985 		char ip6buf[INET6_ADDRSTRLEN];
    986 		nd6log(LOG_DEBUG, "source can't be "
    987 		    "determined: dst=%s, error=%d\n",
    988 		    IN6_PRINT(ip6buf, &satocsin6(dst)->sin6_addr), error);
    989 		goto bad;
    990 	}
    991 	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
    992 	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
    993 	nd_na->nd_na_code = 0;
    994 	nd_na->nd_na_target = *taddr6;
    995 	in6_clearscope(&nd_na->nd_na_target); /* XXX */
    996 
    997 	/*
    998 	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
    999 	 * see nd6_ns_input() for details.
   1000 	 * Basically, if NS packet is sent to unicast/anycast addr,
   1001 	 * target lladdr option SHOULD NOT be included.
   1002 	 */
   1003 	if (tlladdr) {
   1004 		/*
   1005 		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
   1006 		 * lladdr in sdl0.  If we are not proxying (sending NA for
   1007 		 * my address) use lladdr configured for the interface.
   1008 		 */
   1009 		if (sdl0 == NULL)
   1010 			mac = nd6_ifptomac(ifp);
   1011 		else if (sdl0->sa_family == AF_LINK) {
   1012 			const struct sockaddr_dl *sdl;
   1013 			sdl = satocsdl(sdl0);
   1014 			if (sdl->sdl_alen == ifp->if_addrlen)
   1015 				mac = CLLADDR(sdl);
   1016 		}
   1017 	}
   1018 	if (tlladdr && mac) {
   1019 		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
   1020 		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
   1021 
   1022 		/* roundup to 8 bytes alignment! */
   1023 		optlen = (optlen + 7) & ~7;
   1024 
   1025 		m->m_pkthdr.len += optlen;
   1026 		m->m_len += optlen;
   1027 		icmp6len += optlen;
   1028 		memset((void *)nd_opt, 0, optlen);
   1029 		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
   1030 		nd_opt->nd_opt_len = optlen >> 3;
   1031 		memcpy((void *)(nd_opt + 1), mac, ifp->if_addrlen);
   1032 	} else
   1033 		flags &= ~ND_NA_FLAG_OVERRIDE;
   1034 
   1035 	ip6->ip6_plen = htons((u_int16_t)icmp6len);
   1036 	nd_na->nd_na_flags_reserved = flags;
   1037 	nd_na->nd_na_cksum = 0;
   1038 	nd_na->nd_na_cksum =
   1039 	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
   1040 
   1041 	ip6_output(m, NULL, NULL, 0, &im6o, NULL, NULL);
   1042 
   1043 	icmp6_ifstat_inc(ifp, ifs6_out_msg);
   1044 	icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
   1045 	ICMP6_STATINC(ICMP6_STAT_OUTHIST + ND_NEIGHBOR_ADVERT);
   1046 
   1047 	rtcache_free(&ro);
   1048 	return;
   1049 
   1050   bad:
   1051 	rtcache_free(&ro);
   1052 	m_freem(m);
   1053 	return;
   1054 }
   1055 
   1056 const void *
   1057 nd6_ifptomac(const struct ifnet *ifp)
   1058 {
   1059 	switch (ifp->if_type) {
   1060 	case IFT_ARCNET:
   1061 	case IFT_ETHER:
   1062 	case IFT_FDDI:
   1063 	case IFT_IEEE1394:
   1064 	case IFT_PROPVIRTUAL:
   1065 	case IFT_CARP:
   1066 	case IFT_L2VLAN:
   1067 	case IFT_IEEE80211:
   1068 		return CLLADDR(ifp->if_sadl);
   1069 	default:
   1070 		return NULL;
   1071 	}
   1072 }
   1073 
   1074 TAILQ_HEAD(dadq_head, dadq);
   1075 struct dadq {
   1076 	TAILQ_ENTRY(dadq) dad_list;
   1077 	struct ifaddr *dad_ifa;
   1078 	int dad_count;			/* max NS to send */
   1079 	int dad_ns_tcount;		/* # of trials to send NS */
   1080 	int dad_ns_ocount;		/* NS sent so far */
   1081 	int dad_ns_icount;
   1082 	int dad_na_icount;
   1083 	int dad_ns_lcount;		/* looped back NS */
   1084 	struct callout dad_timer_ch;
   1085 #define	ND_OPT_NONCE_STORE	3	/* dad_count should not exceed this */
   1086 	/*
   1087 	 * The default ip6_dad_count is 1 as specified by RFC 4862 and
   1088 	 * practically must users won't exceed this.
   1089 	 * A storage of 3 is defaulted to here, in-case the administrator wants
   1090 	 * to match the equivalent behaviour in our ARP implementation.
   1091 	 * This constraint could be removed by sending the on wire nonce as
   1092 	 * hmac(key, dad_ns_ocount), but that would increase the nonce size
   1093 	 * sent on the wire.
   1094 	 */
   1095 	uint8_t dad_nonce[ND_OPT_NONCE_STORE][ND_OPT_NONCE_LEN];
   1096 };
   1097 
   1098 static struct dadq_head dadq;
   1099 static int dad_init = 0;
   1100 static kmutex_t nd6_dad_lock;
   1101 
   1102 static struct dadq *
   1103 nd6_dad_find(struct ifaddr *ifa, struct nd_opt_nonce *nonce)
   1104 {
   1105 	struct dadq *dp;
   1106 	int i, nonce_max;
   1107 
   1108 	KASSERT(mutex_owned(&nd6_dad_lock));
   1109 
   1110 	TAILQ_FOREACH(dp, &dadq, dad_list) {
   1111 		if (dp->dad_ifa != ifa)
   1112 			continue;
   1113 
   1114 		if (nonce == NULL ||
   1115 		    nonce->nd_opt_nonce_len != (ND_OPT_NONCE_LEN + 2) / 8)
   1116 			break;
   1117 
   1118 		nonce_max = MIN(dp->dad_ns_ocount, ND_OPT_NONCE_STORE);
   1119 		for (i = 0; i < nonce_max; i++) {
   1120 			if (memcmp(nonce->nd_opt_nonce,
   1121 			    dp->dad_nonce[i],
   1122 			    ND_OPT_NONCE_LEN) == 0)
   1123 				break;
   1124 		}
   1125 		if (i < nonce_max) {
   1126 			char ip6buf[INET6_ADDRSTRLEN];
   1127 
   1128 			log(LOG_DEBUG,
   1129 			    "%s: detected a looped back NS message for %s\n",
   1130 			    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???",
   1131 			    IN6_PRINT(ip6buf, IFA_IN6(ifa)));
   1132 			dp->dad_ns_lcount++;
   1133 			continue;
   1134 		}
   1135 
   1136 		break;
   1137 	}
   1138 	return dp;
   1139 }
   1140 
   1141 static void
   1142 nd6_dad_starttimer(struct dadq *dp, int ticks)
   1143 {
   1144 
   1145 	callout_reset(&dp->dad_timer_ch, ticks,
   1146 	    (void (*)(void *))nd6_dad_timer, dp);
   1147 }
   1148 
   1149 static void
   1150 nd6_dad_stoptimer(struct dadq *dp)
   1151 {
   1152 
   1153 	KASSERT(mutex_owned(&nd6_dad_lock));
   1154 
   1155 	TAILQ_REMOVE(&dadq, dp, dad_list);
   1156 	/* Tell the timer that dp is being destroyed. */
   1157 	dp->dad_ifa = NULL;
   1158 	callout_halt(&dp->dad_timer_ch, &nd6_dad_lock);
   1159 }
   1160 
   1161 static void
   1162 nd6_dad_destroytimer(struct dadq *dp)
   1163 {
   1164 
   1165 	KASSERT(dp->dad_ifa == NULL);
   1166 	callout_destroy(&dp->dad_timer_ch);
   1167 	kmem_intr_free(dp, sizeof(*dp));
   1168 }
   1169 
   1170 /*
   1171  * Start Duplicate Address Detection (DAD) for specified interface address.
   1172  *
   1173  * Note that callout is used when xtick > 0 and not when xtick == 0.
   1174  *
   1175  * xtick: minimum delay ticks for IFF_UP event
   1176  */
   1177 void
   1178 nd6_dad_start(struct ifaddr *ifa, int xtick)
   1179 {
   1180 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
   1181 	struct dadq *dp;
   1182 	char ip6buf[INET6_ADDRSTRLEN];
   1183 
   1184 	if (!dad_init) {
   1185 		TAILQ_INIT(&dadq);
   1186 		mutex_init(&nd6_dad_lock, MUTEX_DEFAULT, IPL_NONE);
   1187 		dad_init++;
   1188 	}
   1189 
   1190 	/*
   1191 	 * If we don't need DAD, don't do it.
   1192 	 * There are several cases:
   1193 	 * - DAD is disabled (ip6_dad_count == 0)
   1194 	 * - the interface address is anycast
   1195 	 */
   1196 	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
   1197 		log(LOG_DEBUG,
   1198 			"nd6_dad_start: called with non-tentative address "
   1199 			"%s(%s)\n",
   1200 			IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr),
   1201 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1202 		return;
   1203 	}
   1204 	if (ia->ia6_flags & IN6_IFF_ANYCAST || !ip6_dad_count) {
   1205 		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
   1206 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1207 		return;
   1208 	}
   1209 	KASSERT(ifa->ifa_ifp != NULL);
   1210 	if (!(ifa->ifa_ifp->if_flags & IFF_UP))
   1211 		return;
   1212 
   1213 	dp = kmem_intr_alloc(sizeof(*dp), KM_NOSLEEP);
   1214 
   1215 	mutex_enter(&nd6_dad_lock);
   1216 	if (nd6_dad_find(ifa, NULL) != NULL) {
   1217 		mutex_exit(&nd6_dad_lock);
   1218 		/* DAD already in progress */
   1219 		if (dp != NULL)
   1220 			kmem_intr_free(dp, sizeof(*dp));
   1221 		return;
   1222 	}
   1223 
   1224 	if (dp == NULL) {
   1225 		mutex_exit(&nd6_dad_lock);
   1226 		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
   1227 			"%s(%s)\n",
   1228 			IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr),
   1229 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1230 		return;
   1231 	}
   1232 
   1233 	/*
   1234 	 * Send NS packet for DAD, ip6_dad_count times.
   1235 	 * Note that we must delay the first transmission, if this is the
   1236 	 * first packet to be sent from the interface after interface
   1237 	 * (re)initialization.
   1238 	 */
   1239 	callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE);
   1240 	dp->dad_ifa = ifa;
   1241 	ifaref(ifa);	/* just for safety */
   1242 	dp->dad_count = ip6_dad_count;
   1243 	dp->dad_ns_icount = dp->dad_na_icount = 0;
   1244 	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
   1245 	dp->dad_ns_lcount = 0;
   1246 	TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
   1247 
   1248 	nd6log(LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
   1249 	    IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr));
   1250 
   1251 	if (xtick == 0) {
   1252 		nd6_dad_ns_output(dp, ifa);
   1253 		nd6_dad_starttimer(dp,
   1254 		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
   1255 	} else
   1256 		nd6_dad_starttimer(dp, xtick);
   1257 	mutex_exit(&nd6_dad_lock);
   1258 }
   1259 
   1260 /*
   1261  * terminate DAD unconditionally.  used for address removals.
   1262  */
   1263 void
   1264 nd6_dad_stop(struct ifaddr *ifa)
   1265 {
   1266 	struct dadq *dp;
   1267 
   1268 	if (!dad_init)
   1269 		return;
   1270 
   1271 	mutex_enter(&nd6_dad_lock);
   1272 	dp = nd6_dad_find(ifa, NULL);
   1273 	if (dp == NULL) {
   1274 		mutex_exit(&nd6_dad_lock);
   1275 		/* DAD wasn't started yet */
   1276 		return;
   1277 	}
   1278 
   1279 	/* Prevent the timer from running anymore. */
   1280 	nd6_dad_stoptimer(dp);
   1281 
   1282 	mutex_exit(&nd6_dad_lock);
   1283 
   1284 	nd6_dad_destroytimer(dp);
   1285 	ifafree(ifa);
   1286 }
   1287 
   1288 static void
   1289 nd6_dad_timer(struct dadq *dp)
   1290 {
   1291 	struct ifaddr *ifa;
   1292 	struct in6_ifaddr *ia;
   1293 	int duplicate = 0;
   1294 	char ip6buf[INET6_ADDRSTRLEN];
   1295 	bool need_free = false;
   1296 
   1297 	KERNEL_LOCK_UNLESS_NET_MPSAFE();
   1298 	mutex_enter(&nd6_dad_lock);
   1299 
   1300 	ifa = dp->dad_ifa;
   1301 	if (ifa == NULL) {
   1302 		/* dp is being destroyed by someone.  Do nothing. */
   1303 		goto done;
   1304 	}
   1305 
   1306 	ia = (struct in6_ifaddr *)ifa;
   1307 	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
   1308 		log(LOG_ERR, "nd6_dad_timer: called with duplicate address "
   1309 			"%s(%s)\n",
   1310 			IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr),
   1311 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1312 		goto done;
   1313 	}
   1314 	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
   1315 		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
   1316 			"%s(%s)\n",
   1317 			IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr),
   1318 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1319 		goto done;
   1320 	}
   1321 
   1322 	/* timeouted with IFF_{RUNNING,UP} check */
   1323 	if (dp->dad_ns_tcount > dad_maxtry) {
   1324 		nd6log(LOG_INFO, "%s: could not run DAD, driver problem?\n",
   1325 			if_name(ifa->ifa_ifp));
   1326 
   1327 		nd6_dad_stoptimer(dp);
   1328 		need_free = true;
   1329 		goto done;
   1330 	}
   1331 
   1332 	/* Need more checks? */
   1333 	if (dp->dad_ns_ocount < dp->dad_count) {
   1334 		/*
   1335 		 * We have more NS to go.  Send NS packet for DAD.
   1336 		 */
   1337 		nd6_dad_ns_output(dp, ifa);
   1338 		nd6_dad_starttimer(dp,
   1339 		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
   1340 	} else {
   1341 		/*
   1342 		 * We have transmitted sufficient number of DAD packets.
   1343 		 * See what we've got.
   1344 		 */
   1345 		if (dp->dad_na_icount) {
   1346 			/*
   1347 			 * the check is in nd6_dad_na_input(),
   1348 			 * but just in case
   1349 			 */
   1350 			duplicate++;
   1351 		}
   1352 
   1353 		if (dp->dad_ns_icount) {
   1354 			/* We've seen NS, means DAD has failed. */
   1355 			duplicate++;
   1356 		}
   1357 
   1358 		if (duplicate) {
   1359 			nd6_dad_duplicated(dp);
   1360 			nd6_dad_stoptimer(dp);
   1361 			need_free = true;
   1362 		} else {
   1363 			/*
   1364 			 * We are done with DAD.  No NA came, no NS came.
   1365 			 * No duplicate address found.
   1366 			 */
   1367 			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
   1368 			rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1369 
   1370 			nd6log(LOG_DEBUG,
   1371 			    "%s: DAD complete for %s - no duplicates found\n",
   1372 			    if_name(ifa->ifa_ifp),
   1373 			    IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr));
   1374 
   1375 			nd6_dad_stoptimer(dp);
   1376 			need_free = true;
   1377 		}
   1378 	}
   1379 done:
   1380 	mutex_exit(&nd6_dad_lock);
   1381 
   1382 	if (need_free) {
   1383 		nd6_dad_destroytimer(dp);
   1384 		KASSERT(ifa != NULL);
   1385 		ifafree(ifa);
   1386 	}
   1387 
   1388 	KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
   1389 }
   1390 
   1391 static void
   1392 nd6_dad_duplicated(struct dadq *dp)
   1393 {
   1394 	struct ifaddr *ifa = dp->dad_ifa;
   1395 	struct in6_ifaddr *ia;
   1396 	struct ifnet *ifp;
   1397 	char ip6buf[INET6_ADDRSTRLEN];
   1398 
   1399 	KASSERT(mutex_owned(&nd6_dad_lock));
   1400 	KASSERT(ifa != NULL);
   1401 
   1402 	ifp = ifa->ifa_ifp;
   1403 	ia = (struct in6_ifaddr *)ifa;
   1404 	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
   1405 	    "NS in/out/loopback=%d/%d/%d, NA in=%d\n",
   1406 	    if_name(ifp), IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr),
   1407 	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_ns_lcount,
   1408 	    dp->dad_na_icount);
   1409 
   1410 	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
   1411 	ia->ia6_flags |= IN6_IFF_DUPLICATED;
   1412 
   1413 	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
   1414 	    if_name(ifp), IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr));
   1415 	log(LOG_ERR, "%s: manual intervention required\n",
   1416 	    if_name(ifp));
   1417 
   1418 	/* Inform the routing socket that DAD has completed */
   1419 	rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1420 
   1421 	/*
   1422 	 * If the address is a link-local address formed from an interface
   1423 	 * identifier based on the hardware address which is supposed to be
   1424 	 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
   1425 	 * operation on the interface SHOULD be disabled.
   1426 	 * [rfc2462bis-03 Section 5.4.5]
   1427 	 */
   1428 	if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
   1429 		struct in6_addr in6;
   1430 
   1431 		/*
   1432 		 * To avoid over-reaction, we only apply this logic when we are
   1433 		 * very sure that hardware addresses are supposed to be unique.
   1434 		 */
   1435 		switch (ifp->if_type) {
   1436 		case IFT_ETHER:
   1437 		case IFT_FDDI:
   1438 		case IFT_ATM:
   1439 		case IFT_IEEE1394:
   1440 		case IFT_IEEE80211:
   1441 			in6 = ia->ia_addr.sin6_addr;
   1442 			if (in6_get_hw_ifid(ifp, &in6) == 0 &&
   1443 			    IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
   1444 				ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
   1445 				log(LOG_ERR, "%s: possible hardware address "
   1446 				    "duplication detected, disable IPv6\n",
   1447 				    if_name(ifp));
   1448 			}
   1449 			break;
   1450 		}
   1451 	}
   1452 }
   1453 
   1454 static void
   1455 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
   1456 {
   1457 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
   1458 	struct ifnet *ifp = ifa->ifa_ifp;
   1459 	uint8_t *nonce;
   1460 
   1461 	dp->dad_ns_tcount++;
   1462 	if ((ifp->if_flags & IFF_UP) == 0) {
   1463 #if 0
   1464 		printf("%s: interface down?\n", if_name(ifp));
   1465 #endif
   1466 		return;
   1467 	}
   1468 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
   1469 #if 0
   1470 		printf("%s: interface not running?\n", if_name(ifp));
   1471 #endif
   1472 		return;
   1473 	}
   1474 
   1475 	dp->dad_ns_tcount = 0;
   1476 	nonce = dp->dad_nonce[dp->dad_ns_ocount % ND_OPT_NONCE_STORE];
   1477 	cprng_fast(nonce, ND_OPT_NONCE_LEN);
   1478 	dp->dad_ns_ocount++;
   1479 
   1480 	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, nonce);
   1481 }
   1482 
   1483 static void
   1484 nd6_dad_ns_input(struct ifaddr *ifa, struct nd_opt_nonce *nonce)
   1485 {
   1486 	struct dadq *dp;
   1487 	int duplicate;
   1488 
   1489 	if (ifa == NULL)
   1490 		panic("ifa == NULL in nd6_dad_ns_input");
   1491 
   1492 	duplicate = 0;
   1493 
   1494 	mutex_enter(&nd6_dad_lock);
   1495 	dp = nd6_dad_find(ifa, nonce);
   1496 
   1497 	/*
   1498 	 * if I'm yet to start DAD, someone else started using this address
   1499 	 * first.  I have a duplicate and you win.
   1500 	 */
   1501 	if (dp == NULL || dp->dad_ns_ocount == 0)
   1502 		duplicate++;
   1503 
   1504 	/* XXX more checks for loopback situation - see nd6_dad_timer too */
   1505 
   1506 	if (duplicate) {
   1507 		if (dp) {
   1508 			nd6_dad_duplicated(dp);
   1509 			nd6_dad_stoptimer(dp);
   1510 		}
   1511 	} else {
   1512 		/*
   1513 		 * not sure if I got a duplicate.
   1514 		 * increment ns count and see what happens.
   1515 		 */
   1516 		if (dp)
   1517 			dp->dad_ns_icount++;
   1518 	}
   1519 	mutex_exit(&nd6_dad_lock);
   1520 
   1521 	if (duplicate && dp) {
   1522 		nd6_dad_destroytimer(dp);
   1523 		ifafree(ifa);
   1524 	}
   1525 }
   1526 
   1527 static void
   1528 nd6_dad_na_input(struct ifaddr *ifa)
   1529 {
   1530 	struct dadq *dp;
   1531 
   1532 	KASSERT(ifa != NULL);
   1533 
   1534 	mutex_enter(&nd6_dad_lock);
   1535 
   1536 	dp = nd6_dad_find(ifa, NULL);
   1537 	if (dp == NULL) {
   1538 		mutex_exit(&nd6_dad_lock);
   1539 		return;
   1540 	}
   1541 
   1542 	dp->dad_na_icount++;
   1543 
   1544 	/* remove the address. */
   1545 	nd6_dad_duplicated(dp);
   1546 	nd6_dad_stoptimer(dp);
   1547 
   1548 	mutex_exit(&nd6_dad_lock);
   1549 
   1550 	nd6_dad_destroytimer(dp);
   1551 	ifafree(ifa);
   1552 }
   1553