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