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