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