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