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