Home | History | Annotate | Line # | Download | only in netinet6
nd6_nbr.c revision 1.109
      1 /*	$NetBSD: nd6_nbr.c,v 1.109 2015/07/17 02:21:08 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.109 2015/07/17 02:21:08 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 = NULL;
    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 	if (rt != NULL)
    830 		rtfree(rt);
    831 	return;
    832 
    833  bad:
    834 	ICMP6_STATINC(ICMP6_STAT_BADNA);
    835 	m_freem(m);
    836 }
    837 
    838 /*
    839  * Neighbor advertisement output handling.
    840  *
    841  * Based on RFC 2461
    842  *
    843  * the following items are not implemented yet:
    844  * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
    845  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
    846  */
    847 void
    848 nd6_na_output(
    849 	struct ifnet *ifp,
    850 	const struct in6_addr *daddr6_0,
    851 	const struct in6_addr *taddr6,
    852 	u_long flags,
    853 	int tlladdr,		/* 1 if include target link-layer address */
    854 	const struct sockaddr *sdl0)	/* sockaddr_dl (= proxy NA) or NULL */
    855 {
    856 	struct mbuf *m;
    857 	struct ip6_hdr *ip6;
    858 	struct nd_neighbor_advert *nd_na;
    859 	struct ip6_moptions im6o;
    860 	struct sockaddr *dst;
    861 	union {
    862 		struct sockaddr		dst;
    863 		struct sockaddr_in6	dst6;
    864 	} u;
    865 	struct in6_addr *src, daddr6;
    866 	int icmp6len, maxlen, error;
    867 	const void *mac;
    868 	struct route ro;
    869 
    870 	mac = NULL;
    871 	memset(&ro, 0, sizeof(ro));
    872 
    873 	daddr6 = *daddr6_0;	/* make a local copy for modification */
    874 
    875 	/* estimate the size of message */
    876 	maxlen = sizeof(*ip6) + sizeof(*nd_na);
    877 	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
    878 #ifdef DIAGNOSTIC
    879 	if (max_linkhdr + maxlen >= MCLBYTES) {
    880 		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
    881 		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
    882 		panic("nd6_na_output: insufficient MCLBYTES");
    883 		/* NOTREACHED */
    884 	}
    885 #endif
    886 
    887 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    888 	if (m && max_linkhdr + maxlen >= MHLEN) {
    889 		MCLGET(m, M_DONTWAIT);
    890 		if ((m->m_flags & M_EXT) == 0) {
    891 			m_free(m);
    892 			m = NULL;
    893 		}
    894 	}
    895 	if (m == NULL)
    896 		return;
    897 	m->m_pkthdr.rcvif = NULL;
    898 
    899 	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
    900 		m->m_flags |= M_MCAST;
    901 		im6o.im6o_multicast_ifp = ifp;
    902 		im6o.im6o_multicast_hlim = 255;
    903 		im6o.im6o_multicast_loop = 0;
    904 	}
    905 
    906 	icmp6len = sizeof(*nd_na);
    907 	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
    908 	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
    909 
    910 	/* fill neighbor advertisement packet */
    911 	ip6 = mtod(m, struct ip6_hdr *);
    912 	ip6->ip6_flow = 0;
    913 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
    914 	ip6->ip6_vfc |= IPV6_VERSION;
    915 	ip6->ip6_nxt = IPPROTO_ICMPV6;
    916 	ip6->ip6_hlim = 255;
    917 	if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
    918 		/* reply to DAD */
    919 		daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
    920 		daddr6.s6_addr16[1] = 0;
    921 		daddr6.s6_addr32[1] = 0;
    922 		daddr6.s6_addr32[2] = 0;
    923 		daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
    924 		if (in6_setscope(&daddr6, ifp, NULL))
    925 			goto bad;
    926 
    927 		flags &= ~ND_NA_FLAG_SOLICITED;
    928 	}
    929 	ip6->ip6_dst = daddr6;
    930 	sockaddr_in6_init(&u.dst6, &daddr6, 0, 0, 0);
    931 	dst = &u.dst;
    932 	if (rtcache_setdst(&ro, dst) != 0)
    933 		goto bad;
    934 
    935 	/*
    936 	 * Select a source whose scope is the same as that of the dest.
    937 	 */
    938 	src = in6_selectsrc(satosin6(dst), NULL, NULL, &ro, NULL, NULL, &error);
    939 	if (src == NULL) {
    940 		nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
    941 		    "determined: dst=%s, error=%d\n",
    942 		    ip6_sprintf(&satocsin6(dst)->sin6_addr), error));
    943 		goto bad;
    944 	}
    945 	ip6->ip6_src = *src;
    946 	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
    947 	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
    948 	nd_na->nd_na_code = 0;
    949 	nd_na->nd_na_target = *taddr6;
    950 	in6_clearscope(&nd_na->nd_na_target); /* XXX */
    951 
    952 	/*
    953 	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
    954 	 * see nd6_ns_input() for details.
    955 	 * Basically, if NS packet is sent to unicast/anycast addr,
    956 	 * target lladdr option SHOULD NOT be included.
    957 	 */
    958 	if (tlladdr) {
    959 		/*
    960 		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
    961 		 * lladdr in sdl0.  If we are not proxying (sending NA for
    962 		 * my address) use lladdr configured for the interface.
    963 		 */
    964 		if (sdl0 == NULL)
    965 			mac = nd6_ifptomac(ifp);
    966 		else if (sdl0->sa_family == AF_LINK) {
    967 			const struct sockaddr_dl *sdl;
    968 			sdl = satocsdl(sdl0);
    969 			if (sdl->sdl_alen == ifp->if_addrlen)
    970 				mac = CLLADDR(sdl);
    971 		}
    972 	}
    973 	if (tlladdr && mac) {
    974 		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
    975 		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
    976 
    977 		/* roundup to 8 bytes alignment! */
    978 		optlen = (optlen + 7) & ~7;
    979 
    980 		m->m_pkthdr.len += optlen;
    981 		m->m_len += optlen;
    982 		icmp6len += optlen;
    983 		memset((void *)nd_opt, 0, optlen);
    984 		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
    985 		nd_opt->nd_opt_len = optlen >> 3;
    986 		memcpy((void *)(nd_opt + 1), mac, ifp->if_addrlen);
    987 	} else
    988 		flags &= ~ND_NA_FLAG_OVERRIDE;
    989 
    990 	ip6->ip6_plen = htons((u_int16_t)icmp6len);
    991 	nd_na->nd_na_flags_reserved = flags;
    992 	nd_na->nd_na_cksum = 0;
    993 	nd_na->nd_na_cksum =
    994 	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
    995 
    996 	ip6_output(m, NULL, NULL, 0, &im6o, NULL, NULL);
    997 
    998 	icmp6_ifstat_inc(ifp, ifs6_out_msg);
    999 	icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
   1000 	ICMP6_STATINC(ICMP6_STAT_OUTHIST + ND_NEIGHBOR_ADVERT);
   1001 
   1002 	rtcache_free(&ro);
   1003 	return;
   1004 
   1005   bad:
   1006 	rtcache_free(&ro);
   1007 	m_freem(m);
   1008 	return;
   1009 }
   1010 
   1011 const void *
   1012 nd6_ifptomac(const struct ifnet *ifp)
   1013 {
   1014 	switch (ifp->if_type) {
   1015 	case IFT_ARCNET:
   1016 	case IFT_ETHER:
   1017 	case IFT_FDDI:
   1018 	case IFT_IEEE1394:
   1019 	case IFT_PROPVIRTUAL:
   1020 	case IFT_CARP:
   1021 	case IFT_L2VLAN:
   1022 	case IFT_IEEE80211:
   1023 		return CLLADDR(ifp->if_sadl);
   1024 	default:
   1025 		return NULL;
   1026 	}
   1027 }
   1028 
   1029 TAILQ_HEAD(dadq_head, dadq);
   1030 struct dadq {
   1031 	TAILQ_ENTRY(dadq) dad_list;
   1032 	struct ifaddr *dad_ifa;
   1033 	int dad_count;		/* max NS to send */
   1034 	int dad_ns_tcount;	/* # of trials to send NS */
   1035 	int dad_ns_ocount;	/* NS sent so far */
   1036 	int dad_ns_icount;
   1037 	int dad_na_icount;
   1038 	struct callout dad_timer_ch;
   1039 };
   1040 
   1041 static struct dadq_head dadq;
   1042 static int dad_init = 0;
   1043 
   1044 static struct dadq *
   1045 nd6_dad_find(struct ifaddr *ifa)
   1046 {
   1047 	struct dadq *dp;
   1048 
   1049 	TAILQ_FOREACH(dp, &dadq, dad_list) {
   1050 		if (dp->dad_ifa == ifa)
   1051 			return dp;
   1052 	}
   1053 	return NULL;
   1054 }
   1055 
   1056 static void
   1057 nd6_dad_starttimer(struct dadq *dp, int ticks)
   1058 {
   1059 
   1060 	callout_reset(&dp->dad_timer_ch, ticks,
   1061 	    (void (*)(void *))nd6_dad_timer, (void *)dp->dad_ifa);
   1062 }
   1063 
   1064 static void
   1065 nd6_dad_stoptimer(struct dadq *dp)
   1066 {
   1067 
   1068 	callout_stop(&dp->dad_timer_ch);
   1069 }
   1070 
   1071 /*
   1072  * Start Duplicate Address Detection (DAD) for specified interface address.
   1073  *
   1074  * Note that callout is used when xtick > 0 and not when xtick == 0.
   1075  *
   1076  * xtick: minimum delay ticks for IFF_UP event
   1077  */
   1078 void
   1079 nd6_dad_start(struct ifaddr *ifa, int xtick)
   1080 {
   1081 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
   1082 	struct dadq *dp;
   1083 
   1084 	if (!dad_init) {
   1085 		TAILQ_INIT(&dadq);
   1086 		dad_init++;
   1087 	}
   1088 
   1089 	/*
   1090 	 * If we don't need DAD, don't do it.
   1091 	 * There are several cases:
   1092 	 * - DAD is disabled (ip6_dad_count == 0)
   1093 	 * - the interface address is anycast
   1094 	 */
   1095 	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
   1096 		log(LOG_DEBUG,
   1097 			"nd6_dad_start: called with non-tentative address "
   1098 			"%s(%s)\n",
   1099 			ip6_sprintf(&ia->ia_addr.sin6_addr),
   1100 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1101 		return;
   1102 	}
   1103 	if (ia->ia6_flags & IN6_IFF_ANYCAST || !ip6_dad_count) {
   1104 		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
   1105 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1106 		return;
   1107 	}
   1108 	if (ifa->ifa_ifp == NULL)
   1109 		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
   1110 	if (!(ifa->ifa_ifp->if_flags & IFF_UP))
   1111 		return;
   1112 	if (nd6_dad_find(ifa) != NULL) {
   1113 		/* DAD already in progress */
   1114 		return;
   1115 	}
   1116 
   1117 	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
   1118 	if (dp == NULL) {
   1119 		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
   1120 			"%s(%s)\n",
   1121 			ip6_sprintf(&ia->ia_addr.sin6_addr),
   1122 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1123 		return;
   1124 	}
   1125 	memset(dp, 0, sizeof(*dp));
   1126 	callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE);
   1127 	TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
   1128 
   1129 	nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
   1130 	    ip6_sprintf(&ia->ia_addr.sin6_addr)));
   1131 
   1132 	/*
   1133 	 * Send NS packet for DAD, ip6_dad_count times.
   1134 	 * Note that we must delay the first transmission, if this is the
   1135 	 * first packet to be sent from the interface after interface
   1136 	 * (re)initialization.
   1137 	 */
   1138 	dp->dad_ifa = ifa;
   1139 	ifaref(ifa);	/* just for safety */
   1140 	dp->dad_count = ip6_dad_count;
   1141 	dp->dad_ns_icount = dp->dad_na_icount = 0;
   1142 	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
   1143 	if (xtick == 0) {
   1144 		nd6_dad_ns_output(dp, ifa);
   1145 		nd6_dad_starttimer(dp,
   1146 		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
   1147 	} else
   1148 		nd6_dad_starttimer(dp, xtick);
   1149 }
   1150 
   1151 /*
   1152  * terminate DAD unconditionally.  used for address removals.
   1153  */
   1154 void
   1155 nd6_dad_stop(struct ifaddr *ifa)
   1156 {
   1157 	struct dadq *dp;
   1158 
   1159 	if (!dad_init)
   1160 		return;
   1161 	dp = nd6_dad_find(ifa);
   1162 	if (dp == NULL) {
   1163 		/* DAD wasn't started yet */
   1164 		return;
   1165 	}
   1166 
   1167 	nd6_dad_stoptimer(dp);
   1168 
   1169 	TAILQ_REMOVE(&dadq, dp, dad_list);
   1170 	free(dp, M_IP6NDP);
   1171 	dp = NULL;
   1172 	ifafree(ifa);
   1173 }
   1174 
   1175 static void
   1176 nd6_dad_timer(struct ifaddr *ifa)
   1177 {
   1178 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
   1179 	struct dadq *dp;
   1180 
   1181 	mutex_enter(softnet_lock);
   1182 	KERNEL_LOCK(1, NULL);
   1183 
   1184 	/* Sanity check */
   1185 	if (ia == NULL) {
   1186 		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
   1187 		goto done;
   1188 	}
   1189 	dp = nd6_dad_find(ifa);
   1190 	if (dp == NULL) {
   1191 		log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
   1192 		goto done;
   1193 	}
   1194 	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
   1195 		log(LOG_ERR, "nd6_dad_timer: called with duplicate address "
   1196 			"%s(%s)\n",
   1197 			ip6_sprintf(&ia->ia_addr.sin6_addr),
   1198 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1199 		goto done;
   1200 	}
   1201 	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
   1202 		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
   1203 			"%s(%s)\n",
   1204 			ip6_sprintf(&ia->ia_addr.sin6_addr),
   1205 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1206 		goto done;
   1207 	}
   1208 
   1209 	/* timeouted with IFF_{RUNNING,UP} check */
   1210 	if (dp->dad_ns_tcount > dad_maxtry) {
   1211 		nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
   1212 			if_name(ifa->ifa_ifp)));
   1213 
   1214 		TAILQ_REMOVE(&dadq, dp, dad_list);
   1215 		free(dp, M_IP6NDP);
   1216 		dp = NULL;
   1217 		ifafree(ifa);
   1218 		goto done;
   1219 	}
   1220 
   1221 	/* Need more checks? */
   1222 	if (dp->dad_ns_ocount < dp->dad_count) {
   1223 		/*
   1224 		 * We have more NS to go.  Send NS packet for DAD.
   1225 		 */
   1226 		nd6_dad_ns_output(dp, ifa);
   1227 		nd6_dad_starttimer(dp,
   1228 		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
   1229 	} else {
   1230 		/*
   1231 		 * We have transmitted sufficient number of DAD packets.
   1232 		 * See what we've got.
   1233 		 */
   1234 		int duplicate;
   1235 
   1236 		duplicate = 0;
   1237 
   1238 		if (dp->dad_na_icount) {
   1239 			/*
   1240 			 * the check is in nd6_dad_na_input(),
   1241 			 * but just in case
   1242 			 */
   1243 			duplicate++;
   1244 		}
   1245 
   1246 		if (dp->dad_ns_icount) {
   1247 			/* We've seen NS, means DAD has failed. */
   1248 			duplicate++;
   1249 		}
   1250 
   1251 		if (duplicate) {
   1252 			/* (*dp) will be freed in nd6_dad_duplicated() */
   1253 			dp = NULL;
   1254 			nd6_dad_duplicated(ifa);
   1255 		} else {
   1256 			/*
   1257 			 * We are done with DAD.  No NA came, no NS came.
   1258 			 * No duplicate address found.
   1259 			 */
   1260 			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
   1261 			rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1262 
   1263 			nd6log((LOG_DEBUG,
   1264 			    "%s: DAD complete for %s - no duplicates found\n",
   1265 			    if_name(ifa->ifa_ifp),
   1266 			    ip6_sprintf(&ia->ia_addr.sin6_addr)));
   1267 
   1268 			TAILQ_REMOVE(&dadq, dp, dad_list);
   1269 			free(dp, M_IP6NDP);
   1270 			dp = NULL;
   1271 			ifafree(ifa);
   1272 		}
   1273 	}
   1274 
   1275 done:
   1276 	KERNEL_UNLOCK_ONE(NULL);
   1277 	mutex_exit(softnet_lock);
   1278 }
   1279 
   1280 void
   1281 nd6_dad_duplicated(struct ifaddr *ifa)
   1282 {
   1283 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
   1284 	struct ifnet *ifp;
   1285 	struct dadq *dp;
   1286 
   1287 	dp = nd6_dad_find(ifa);
   1288 	if (dp == NULL) {
   1289 		log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
   1290 		return;
   1291 	}
   1292 
   1293 	ifp = ifa->ifa_ifp;
   1294 	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
   1295 	    "NS in/out=%d/%d, NA in=%d\n",
   1296 	    if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
   1297 	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
   1298 
   1299 	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
   1300 	ia->ia6_flags |= IN6_IFF_DUPLICATED;
   1301 
   1302 	/* We are done with DAD, with duplicated address found. (failure) */
   1303 	nd6_dad_stoptimer(dp);
   1304 
   1305 	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
   1306 	    if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
   1307 	log(LOG_ERR, "%s: manual intervention required\n",
   1308 	    if_name(ifp));
   1309 
   1310 	/* Inform the routing socket that DAD has completed */
   1311 	rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1312 
   1313 	/*
   1314 	 * If the address is a link-local address formed from an interface
   1315 	 * identifier based on the hardware address which is supposed to be
   1316 	 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
   1317 	 * operation on the interface SHOULD be disabled.
   1318 	 * [rfc2462bis-03 Section 5.4.5]
   1319 	 */
   1320 	if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
   1321 		struct in6_addr in6;
   1322 
   1323 		/*
   1324 		 * To avoid over-reaction, we only apply this logic when we are
   1325 		 * very sure that hardware addresses are supposed to be unique.
   1326 		 */
   1327 		switch (ifp->if_type) {
   1328 		case IFT_ETHER:
   1329 		case IFT_FDDI:
   1330 		case IFT_ATM:
   1331 		case IFT_IEEE1394:
   1332 #ifdef IFT_IEEE80211
   1333 		case IFT_IEEE80211:
   1334 #endif
   1335 			in6 = ia->ia_addr.sin6_addr;
   1336 			if (in6_get_hw_ifid(ifp, &in6) == 0 &&
   1337 			    IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
   1338 				ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
   1339 				log(LOG_ERR, "%s: possible hardware address "
   1340 				    "duplication detected, disable IPv6\n",
   1341 				    if_name(ifp));
   1342 			}
   1343 			break;
   1344 		}
   1345 	}
   1346 
   1347 	TAILQ_REMOVE(&dadq, dp, dad_list);
   1348 	free(dp, M_IP6NDP);
   1349 	dp = NULL;
   1350 	ifafree(ifa);
   1351 }
   1352 
   1353 static void
   1354 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
   1355 {
   1356 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
   1357 	struct ifnet *ifp = ifa->ifa_ifp;
   1358 
   1359 	dp->dad_ns_tcount++;
   1360 	if ((ifp->if_flags & IFF_UP) == 0) {
   1361 #if 0
   1362 		printf("%s: interface down?\n", if_name(ifp));
   1363 #endif
   1364 		return;
   1365 	}
   1366 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
   1367 #if 0
   1368 		printf("%s: interface not running?\n", if_name(ifp));
   1369 #endif
   1370 		return;
   1371 	}
   1372 
   1373 	dp->dad_ns_tcount = 0;
   1374 	dp->dad_ns_ocount++;
   1375 	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
   1376 }
   1377 
   1378 static void
   1379 nd6_dad_ns_input(struct ifaddr *ifa)
   1380 {
   1381 	struct in6_ifaddr *ia;
   1382 	const struct in6_addr *taddr6;
   1383 	struct dadq *dp;
   1384 	int duplicate;
   1385 
   1386 	if (ifa == NULL)
   1387 		panic("ifa == NULL in nd6_dad_ns_input");
   1388 
   1389 	ia = (struct in6_ifaddr *)ifa;
   1390 	taddr6 = &ia->ia_addr.sin6_addr;
   1391 	duplicate = 0;
   1392 	dp = nd6_dad_find(ifa);
   1393 
   1394 	/* Quickhack - completely ignore DAD NS packets */
   1395 	if (dad_ignore_ns) {
   1396 		nd6log((LOG_INFO,
   1397 		    "nd6_dad_ns_input: ignoring DAD NS packet for "
   1398 		    "address %s(%s)\n", ip6_sprintf(taddr6),
   1399 		    if_name(ifa->ifa_ifp)));
   1400 		return;
   1401 	}
   1402 
   1403 	/*
   1404 	 * if I'm yet to start DAD, someone else started using this address
   1405 	 * first.  I have a duplicate and you win.
   1406 	 */
   1407 	if (dp == NULL || dp->dad_ns_ocount == 0)
   1408 		duplicate++;
   1409 
   1410 	/* XXX more checks for loopback situation - see nd6_dad_timer too */
   1411 
   1412 	if (duplicate) {
   1413 		dp = NULL;	/* will be freed in nd6_dad_duplicated() */
   1414 		nd6_dad_duplicated(ifa);
   1415 	} else {
   1416 		/*
   1417 		 * not sure if I got a duplicate.
   1418 		 * increment ns count and see what happens.
   1419 		 */
   1420 		if (dp)
   1421 			dp->dad_ns_icount++;
   1422 	}
   1423 }
   1424 
   1425 static void
   1426 nd6_dad_na_input(struct ifaddr *ifa)
   1427 {
   1428 	struct dadq *dp;
   1429 
   1430 	if (ifa == NULL)
   1431 		panic("ifa == NULL in nd6_dad_na_input");
   1432 
   1433 	dp = nd6_dad_find(ifa);
   1434 	if (dp)
   1435 		dp->dad_na_icount++;
   1436 
   1437 	/* remove the address. */
   1438 	nd6_dad_duplicated(ifa);
   1439 }
   1440