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