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