Home | History | Annotate | Line # | Download | only in netinet6
nd6_nbr.c revision 1.15
      1 /*	$NetBSD: nd6_nbr.c,v 1.15 2000/02/07 05:42:28 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include "opt_inet.h"
     33 #include "opt_ipsec.h"
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/malloc.h>
     38 #include <sys/mbuf.h>
     39 #include <sys/socket.h>
     40 #include <sys/sockio.h>
     41 #include <sys/time.h>
     42 #include <sys/kernel.h>
     43 #include <sys/errno.h>
     44 #include <sys/ioctl.h>
     45 #include <sys/syslog.h>
     46 #include <sys/queue.h>
     47 
     48 #include <net/if.h>
     49 #include <net/if_types.h>
     50 #include <net/if_dl.h>
     51 #include <net/route.h>
     52 
     53 #include <netinet/in.h>
     54 #include <netinet/in_var.h>
     55 #include <netinet6/in6_var.h>
     56 #include <netinet/ip6.h>
     57 #include <netinet6/ip6_var.h>
     58 #include <netinet6/nd6.h>
     59 #include <netinet/icmp6.h>
     60 
     61 #include <net/net_osdep.h>
     62 
     63 #define SDL(s) ((struct sockaddr_dl *)s)
     64 
     65 struct dadq;
     66 static struct dadq *nd6_dad_find __P((struct ifaddr *));
     67 static void nd6_dad_timer __P((struct ifaddr *));
     68 static void nd6_dad_ns_output __P((struct dadq *, struct ifaddr *));
     69 static void nd6_dad_ns_input __P((struct ifaddr *));
     70 static void nd6_dad_na_input __P((struct ifaddr *));
     71 
     72 static int dad_ignore_ns = 0;	/* ignore NS in DAD - specwise incorrect*/
     73 static int dad_maxtry = 15;	/* max # of *tries* to transmit DAD packet */
     74 
     75 /*
     76  * Input an Neighbor Solicitation Message.
     77  *
     78  * Based on RFC 2461
     79  * Based on RFC 2462 (duplicated address detection)
     80  *
     81  * XXX proxy advertisement
     82  */
     83 void
     84 nd6_ns_input(m, off, icmp6len)
     85 	struct mbuf *m;
     86 	int off, icmp6len;
     87 {
     88 	struct ifnet *ifp = m->m_pkthdr.rcvif;
     89 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
     90 	struct nd_neighbor_solicit *nd_ns
     91 		= (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
     92 	struct in6_addr saddr6 = ip6->ip6_src;
     93 	struct in6_addr daddr6 = ip6->ip6_dst;
     94 	struct in6_addr taddr6 = nd_ns->nd_ns_target;
     95 	struct in6_addr myaddr6;
     96 	char *lladdr = NULL;
     97 	struct ifaddr *ifa;
     98 	int lladdrlen = 0;
     99 	int anycast = 0, proxy = 0, tentative = 0;
    100 	int tlladdr;
    101 	union nd_opts ndopts;
    102 
    103 	if (ip6->ip6_hlim != 255) {
    104 		log(LOG_ERR,
    105 		    "nd6_ns_input: invalid hlim %d\n", ip6->ip6_hlim);
    106 		return;
    107 	}
    108 
    109 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
    110 		/* dst has to be solicited node multicast address. */
    111 		if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL
    112 		    /*don't check ifindex portion*/
    113 		    && daddr6.s6_addr32[1] == 0
    114 		    && daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE
    115 		    && daddr6.s6_addr8[12] == 0xff) {
    116 			; /*good*/
    117 		} else {
    118 			log(LOG_INFO, "nd6_ns_input: bad DAD packet "
    119 				"(wrong ip6 dst)\n");
    120 			goto bad;
    121 		}
    122 	}
    123 
    124 	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
    125 		log(LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n");
    126 		goto bad;
    127 	}
    128 
    129 	if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
    130 		taddr6.s6_addr16[1] = htons(ifp->if_index);
    131 
    132 	icmp6len -= sizeof(*nd_ns);
    133 	nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
    134 	if (nd6_options(&ndopts) < 0) {
    135 		log(LOG_INFO, "nd6_ns_input: invalid ND option, ignored\n");
    136 		goto bad;
    137 	}
    138 
    139 	if (ndopts.nd_opts_src_lladdr) {
    140 		lladdr = (char *)(ndopts.nd_opts_src_lladdr +1);
    141 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
    142 	}
    143 
    144 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
    145 		log(LOG_INFO, "nd6_ns_input: bad DAD packet "
    146 			"(link-layer address option)\n");
    147 		goto bad;
    148 	}
    149 
    150 	/*
    151 	 * Attaching target link-layer address to the NA?
    152 	 * (RFC 2461 7.2.4)
    153 	 *
    154 	 * NS IP dst is unicast/anycast			MUST NOT add
    155 	 * NS IP dst is solicited-node multicast	MUST add
    156 	 *
    157 	 * In implementation, we add target link-layer address by default.
    158 	 * We do not add one in MUST NOT cases.
    159 	 */
    160 #if 0 /* too much! */
    161 	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &daddr6);
    162 	if (ifa && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST))
    163 		tlladdr = 0;
    164 	else
    165 #endif
    166 	if (!IN6_IS_ADDR_MULTICAST(&daddr6))
    167 		tlladdr = 0;
    168 	else
    169 		tlladdr = 1;
    170 
    171 	/*
    172 	 * Target address (taddr6) must be either:
    173 	 * (1) Valid unicast/anycast address for my receiving interface,
    174 	 * (2) Unicast address for which I'm offering proxy service, or
    175 	 * (3) "tentative" address on which DAD is being performed.
    176 	 */
    177 	/* (1) and (3) check. */
    178 	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
    179 
    180 	/* (2) check. */
    181 	if (!ifa && nd6_proxyall) {
    182 		struct rtentry *rt;
    183 		struct sockaddr_in6 tsin6;
    184 
    185 		bzero(&tsin6, sizeof tsin6);
    186 		tsin6.sin6_len = sizeof(struct sockaddr_in6);
    187 		tsin6.sin6_family = AF_INET6;
    188 		tsin6.sin6_addr = taddr6;
    189 
    190 		rt = rtalloc1((struct sockaddr *)&tsin6, 0);
    191 		if (rt && rt->rt_ifp != ifp) {
    192 			/*
    193 			 * search link local addr for ifp, and use it for
    194 			 * proxy NA.
    195 			 */
    196 			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp);
    197 			if (ifa)
    198 				proxy = 1;
    199 		}
    200 		rtfree(rt);
    201 	}
    202 	if (!ifa) {
    203 		/*
    204 		 * We've got a NS packet, and we don't have that adddress
    205 		 * assigned for us.  We MUST silently ignore it.
    206 		 * See RFC2461 7.2.3.
    207 		 */
    208 		return;
    209 	}
    210 	myaddr6 = *IFA_IN6(ifa);
    211 	anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
    212 	tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
    213 	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
    214 		return;
    215 
    216 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
    217 		log(LOG_INFO,
    218 		    "nd6_ns_input: lladdrlen mismatch for %s "
    219 		    "(if %d, NS packet %d)\n",
    220 			ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2);
    221 	}
    222 
    223 	if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
    224 		log(LOG_INFO,
    225 		    "nd6_ns_input: duplicate IP6 address %s\n",
    226 		    ip6_sprintf(&saddr6));
    227 		return;
    228 	}
    229 
    230 	/*
    231 	 * We have neighbor solicitation packet, with target address equals to
    232 	 * one of my tentative address.
    233 	 *
    234 	 * src addr	how to process?
    235 	 * ---		---
    236 	 * multicast	of course, invalid (rejected in ip6_input)
    237 	 * unicast	somebody is doing address resolution -> ignore
    238 	 * unspec	dup address detection
    239 	 *
    240 	 * The processing is defined in RFC 2462.
    241 	 */
    242 	if (tentative) {
    243 		/*
    244 		 * If source address is unspecified address, it is for
    245 		 * duplicated address detection.
    246 		 *
    247 		 * If not, the packet is for addess resolution;
    248 		 * silently ignore it.
    249 		 */
    250 		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
    251 			nd6_dad_ns_input(ifa);
    252 
    253 		return;
    254 	}
    255 
    256 	/*
    257 	 * If the source address is unspecified address, entries must not
    258 	 * be created or updated.
    259 	 * It looks that sender is performing DAD.  Output NA toward
    260 	 * all-node multicast address, to tell the sender that I'm using
    261 	 * the address.
    262 	 * S bit ("solicited") must be zero.
    263 	 */
    264 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
    265 		saddr6 = in6addr_linklocal_allnodes;
    266 		saddr6.s6_addr16[1] = htons(ifp->if_index);
    267 		nd6_na_output(ifp, &saddr6, &taddr6,
    268 			      ((anycast || proxy || !tlladdr)
    269 				      ? 0 : ND_NA_FLAG_OVERRIDE)
    270 			      	| (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
    271 			      tlladdr);
    272 		return;
    273 	}
    274 
    275 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_NEIGHBOR_SOLICIT, 0);
    276 
    277 	nd6_na_output(ifp, &saddr6, &taddr6,
    278 		      ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE)
    279 			| (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
    280 			| ND_NA_FLAG_SOLICITED,
    281 		      tlladdr);
    282 	return;
    283 
    284  bad:
    285 	log(LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6));
    286 	log(LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6));
    287 	log(LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6));
    288 	return;
    289 }
    290 
    291 /*
    292  * Output an Neighbor Solicitation Message. Caller specifies:
    293  *	- ICMP6 header source IP6 address
    294  *	- ND6 header target IP6 address
    295  *	- ND6 header source datalink address
    296  *
    297  * Based on RFC 2461
    298  * Based on RFC 2462 (duplicated address detection)
    299  */
    300 void
    301 nd6_ns_output(ifp, daddr6, taddr6, ln, dad)
    302 	struct ifnet *ifp;
    303 	struct in6_addr *daddr6, *taddr6;
    304 	struct llinfo_nd6 *ln;	/* for source address determination */
    305 	int dad;	/* duplicated address detection */
    306 {
    307 	struct mbuf *m;
    308 	struct ip6_hdr *ip6;
    309 	struct nd_neighbor_solicit *nd_ns;
    310 	struct in6_ifaddr *ia = NULL;
    311 	struct ip6_moptions im6o;
    312 	int icmp6len;
    313 	int maxlen;
    314 	caddr_t mac;
    315 	struct ifnet *outif = NULL;
    316 
    317 	if (IN6_IS_ADDR_MULTICAST(taddr6))
    318 		return;
    319 
    320 	/* estimate the size of message */
    321 	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
    322 	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
    323 	if (max_linkhdr + maxlen >= MCLBYTES) {
    324 #ifdef DIAGNOSTIC
    325 		printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
    326 		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
    327 #endif
    328 		return;
    329 	}
    330 
    331 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    332 	if (m && max_linkhdr + maxlen >= MHLEN) {
    333 		MCLGET(m, M_DONTWAIT);
    334 		if ((m->m_flags & M_EXT) == 0) {
    335 			m_free(m);
    336 			m = NULL;
    337 		}
    338 	}
    339 	if (m == NULL)
    340 		return;
    341 
    342 	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
    343 		m->m_flags |= M_MCAST;
    344 		im6o.im6o_multicast_ifp = ifp;
    345 		im6o.im6o_multicast_hlim = 255;
    346 		im6o.im6o_multicast_loop = 0;
    347 	}
    348 
    349 	icmp6len = sizeof(*nd_ns);
    350 	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
    351 	m->m_data += max_linkhdr;	/*or MH_ALIGN() equivalent?*/
    352 
    353 	/* fill neighbor solicitation packet */
    354 	ip6 = mtod(m, struct ip6_hdr *);
    355 	ip6->ip6_flow = 0;
    356 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
    357 	ip6->ip6_vfc |= IPV6_VERSION;
    358 	/* ip6->ip6_plen will be set later */
    359 	ip6->ip6_nxt = IPPROTO_ICMPV6;
    360 	ip6->ip6_hlim = 255;
    361 	if (daddr6)
    362 		ip6->ip6_dst = *daddr6;
    363 	else {
    364 		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
    365 		ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
    366 		ip6->ip6_dst.s6_addr32[1] = 0;
    367 		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
    368 		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
    369 		ip6->ip6_dst.s6_addr8[12] = 0xff;
    370 	}
    371 	if (!dad) {
    372 #if 0	/* KAME way, exact address scope match */
    373 		/*
    374 		 * Select a source whose scope is the same as that of the dest.
    375 		 * Typically, the dest is link-local solicitation multicast
    376 		 * (i.e. neighbor discovery) or link-local/global unicast
    377 		 * (i.e. neighbor un-reachability detection).
    378 		 */
    379 		ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
    380 		if (ia == NULL) {
    381 			m_freem(m);
    382 			return;
    383 		}
    384 		ip6->ip6_src = ia->ia_addr.sin6_addr;
    385 #else	/* spec-wise correct */
    386 		/*
    387 		 * RFC2461 7.2.2:
    388 		 * "If the source address of the packet prompting the
    389 		 * solicitation is the same as one of the addresses assigned
    390 		 * to the outgoing interface, that address SHOULD be placed
    391 		 * in the IP Source Address of the outgoing solicitation.
    392 		 * Otherwise, any one of the addresses assigned to the
    393 		 * interface should be used."
    394 		 *
    395 		 * We use the source address for the prompting packet
    396 		 * (saddr6), if:
    397 		 * - saddr6 is given from the caller (by giving "ln"), and
    398 		 * - saddr6 belongs to the outgoing interface.
    399 		 * Otherwise, we perform a scope-wise match.
    400 		 */
    401 		struct ip6_hdr *hip6;		/*hold ip6*/
    402 		struct in6_addr *saddr6;
    403 
    404 		if (ln && ln->ln_hold) {
    405 			hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
    406 			/* XXX pullup? */
    407 			if (sizeof(*hip6) < ln->ln_hold->m_len)
    408 				saddr6 = &hip6->ip6_src;
    409 			else
    410 				saddr6 = NULL;
    411 		} else
    412 			saddr6 = NULL;
    413 		if (saddr6 && in6ifa_ifpwithaddr(ifp, saddr6))
    414 			bcopy(saddr6, &ip6->ip6_src, sizeof(*saddr6));
    415 		else {
    416 			ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
    417 			if (ia == NULL) {
    418 				m_freem(m);	/*XXX*/
    419 				return;
    420 			}
    421 			ip6->ip6_src = ia->ia_addr.sin6_addr;
    422 		}
    423 #endif
    424 	} else {
    425 		/*
    426 		 * Source address for DAD packet must always be IPv6
    427 		 * unspecified address. (0::0)
    428 		 */
    429 		bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
    430 	}
    431 	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
    432 	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
    433 	nd_ns->nd_ns_code = 0;
    434 	nd_ns->nd_ns_reserved = 0;
    435 	nd_ns->nd_ns_target = *taddr6;
    436 
    437 	if (IN6_IS_SCOPE_LINKLOCAL(&nd_ns->nd_ns_target))
    438 		nd_ns->nd_ns_target.s6_addr16[1] = 0;
    439 
    440 	/*
    441 	 * Add source link-layer address option.
    442 	 *
    443 	 *				spec		implementation
    444 	 *				---		---
    445 	 * DAD packet			MUST NOT	do not add the option
    446 	 * there's no link layer address:
    447 	 *				impossible	do not add the option
    448 	 * there's link layer address:
    449 	 *	Multicast NS		MUST add one	add the option
    450 	 *	Unicast NS		SHOULD add one	add the option
    451 	 */
    452 	if (!dad && (mac = nd6_ifptomac(ifp))) {
    453 		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
    454 		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
    455 		/* 8 byte alignments... */
    456 		optlen = (optlen + 7) & ~7;
    457 
    458 		m->m_pkthdr.len += optlen;
    459 		m->m_len += optlen;
    460 		icmp6len += optlen;
    461 		bzero((caddr_t)nd_opt, optlen);
    462 		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
    463 		nd_opt->nd_opt_len = optlen >> 3;
    464 		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
    465 	}
    466 
    467 	ip6->ip6_plen = htons((u_short)icmp6len);
    468 	nd_ns->nd_ns_cksum = 0;
    469 	nd_ns->nd_ns_cksum
    470 		= in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
    471 
    472 #ifdef IPSEC
    473 	m->m_pkthdr.rcvif = NULL;
    474 #endif /*IPSEC*/
    475 	ip6_output(m, NULL, NULL, dad ? IPV6_DADOUTPUT : 0, &im6o, &outif);
    476 	if (outif) {
    477 		icmp6_ifstat_inc(outif, ifs6_out_msg);
    478 		icmp6_ifstat_inc(outif, ifs6_out_neighborsolicit);
    479 	}
    480 	icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
    481 }
    482 
    483 /*
    484  * Neighbor advertisement input handling.
    485  *
    486  * Based on RFC 2461
    487  * Based on RFC 2462 (duplicated address detection)
    488  */
    489 void
    490 nd6_na_input(m, off, icmp6len)
    491 	struct mbuf *m;
    492 	int off, icmp6len;
    493 {
    494 	struct ifnet *ifp = m->m_pkthdr.rcvif;
    495 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    496 	struct nd_neighbor_advert *nd_na
    497 		= (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
    498 #if 0
    499 	struct in6_addr saddr6 = ip6->ip6_src;
    500 #endif
    501 	struct in6_addr daddr6 = ip6->ip6_dst;
    502 	struct in6_addr taddr6 = nd_na->nd_na_target;
    503 	int flags = nd_na->nd_na_flags_reserved;
    504 	int is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
    505 	int is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
    506 	int is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
    507 	char *lladdr = NULL;
    508 	int lladdrlen = 0;
    509 	struct ifaddr *ifa;
    510 	struct llinfo_nd6 *ln;
    511 	struct rtentry *rt;
    512 	struct sockaddr_dl *sdl;
    513 	union nd_opts ndopts;
    514 
    515 	if (ip6->ip6_hlim != 255) {
    516 		log(LOG_ERR,
    517 		    "nd6_na_input: invalid hlim %d\n", ip6->ip6_hlim);
    518 		return;
    519 	}
    520 
    521 	if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
    522 		taddr6.s6_addr16[1] = htons(ifp->if_index);
    523 
    524 	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
    525 		log(LOG_ERR,
    526 		    "nd6_na_input: invalid target address %s\n",
    527 		    ip6_sprintf(&taddr6));
    528 		return;
    529 	}
    530 	if (IN6_IS_ADDR_MULTICAST(&daddr6))
    531 		if (is_solicited) {
    532 			log(LOG_ERR,
    533 			    "nd6_na_input: a solicited adv is multicasted\n");
    534 			return;
    535 		}
    536 
    537 	icmp6len -= sizeof(*nd_na);
    538 	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
    539 	if (nd6_options(&ndopts) < 0) {
    540 		log(LOG_INFO, "nd6_na_input: invalid ND option, ignored\n");
    541 		return;
    542 	}
    543 
    544 	if (ndopts.nd_opts_tgt_lladdr) {
    545 		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
    546 		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
    547 	}
    548 
    549 	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
    550 
    551 	/*
    552 	 * Target address matches one of my interface address.
    553 	 *
    554 	 * If my address is tentative, this means that there's somebody
    555 	 * already using the same address as mine.  This indicates DAD failure.
    556 	 * This is defined in RFC 2462.
    557 	 *
    558 	 * Otherwise, process as defined in RFC 2461.
    559 	 */
    560 	if (ifa
    561 	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
    562 		nd6_dad_na_input(ifa);
    563 		return;
    564 	}
    565 
    566 	/* Just for safety, maybe unnecessery. */
    567 	if (ifa) {
    568 		log(LOG_ERR,
    569 		    "nd6_na_input: duplicate IP6 address %s\n",
    570 		    ip6_sprintf(&taddr6));
    571 		return;
    572 	}
    573 
    574 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
    575 		log(LOG_INFO,
    576 		    "nd6_na_input: lladdrlen mismatch for %s "
    577 		    "(if %d, NA packet %d)\n",
    578 			ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2);
    579 	}
    580 
    581 	/*
    582 	 * If no neighbor cache entry is found, NA SHOULD silently be discarded.
    583 	 */
    584 	rt = nd6_lookup(&taddr6, 0, ifp);
    585 	if ((rt == NULL) ||
    586 	   ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
    587 	   ((sdl = SDL(rt->rt_gateway)) == NULL))
    588 		return;
    589 
    590 	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
    591 		/*
    592 		 * If the link-layer has address, and no lladdr option came,
    593 		 * discard the packet.
    594 		 */
    595 		if (ifp->if_addrlen && !lladdr)
    596 			return;
    597 
    598 		/*
    599 		 * Record link-layer address, and update the state.
    600 		 */
    601 		sdl->sdl_alen = ifp->if_addrlen;
    602 		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
    603 		if (is_solicited) {
    604 			ln->ln_state = ND6_LLINFO_REACHABLE;
    605 			if (ln->ln_expire)
    606 				ln->ln_expire = time.tv_sec +
    607 					nd_ifinfo[rt->rt_ifp->if_index].reachable;
    608 		} else
    609 			ln->ln_state = ND6_LLINFO_STALE;
    610 		ln->ln_router = is_router;
    611 	} else {
    612 		int llchange;
    613 
    614 		/*
    615 		 * Check if the link-layer address has changed or not.
    616 		 */
    617 		if (!lladdr)
    618 			llchange = 0;
    619 		else {
    620 			if (sdl->sdl_alen) {
    621 				if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
    622 					llchange = 1;
    623 				else
    624 					llchange = 0;
    625 			} else
    626 				llchange = 1;
    627 		}
    628 
    629 		/*
    630 		 * This is VERY complex.  Look at it with care.
    631 		 *
    632 		 * override solicit lladdr llchange	action
    633 		 *					(L: record lladdr)
    634 		 *
    635 		 *	0	0	n	--	(2c)
    636 		 *	0	0	y	n	(2b) L
    637 		 *	0	0	y	y	(1)    REACHABLE->STALE
    638 		 *	0	1	n	--	(2c)   *->REACHABLE
    639 		 *	0	1	y	n	(2b) L *->REACHABLE
    640 		 *	0	1	y	y	(1)    REACHABLE->STALE
    641 		 *	1	0	n	--	(2a)
    642 		 *	1	0	y	n	(2a) L
    643 		 *	1	0	y	y	(2a) L *->STALE
    644 		 *	1	1	n	--	(2a)   *->REACHABLE
    645 		 *	1	1	y	n	(2a) L *->REACHABLE
    646 		 *	1	1	y	y	(2a) L *->REACHABLE
    647 		 */
    648 		if (!is_override && (lladdr && llchange)) {	   /* (1) */
    649 			/*
    650 			 * If state is REACHABLE, make it STALE.
    651 			 * no other updates should be done.
    652 			 */
    653 			if (ln->ln_state == ND6_LLINFO_REACHABLE)
    654 				ln->ln_state = ND6_LLINFO_STALE;
    655 			return;
    656 		} else if (is_override				   /* (2a) */
    657 			|| (!is_override && (lladdr && !llchange)) /* (2b) */
    658 			|| !lladdr) {				   /* (2c) */
    659 			/*
    660 			 * Update link-local address, if any.
    661 			 */
    662 			if (lladdr) {
    663 				sdl->sdl_alen = ifp->if_addrlen;
    664 				bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
    665 			}
    666 
    667 			/*
    668 			 * If solicited, make the state REACHABLE.
    669 			 * If not solicited and the link-layer address was
    670 			 * changed, make it STALE.
    671 			 */
    672 			if (is_solicited) {
    673 				ln->ln_state = ND6_LLINFO_REACHABLE;
    674 				if (ln->ln_expire) {
    675 					ln->ln_expire = time.tv_sec +
    676 						nd_ifinfo[ifp->if_index].reachable;
    677 				}
    678 			} else {
    679 				if (lladdr && llchange)
    680 					ln->ln_state = ND6_LLINFO_STALE;
    681 			}
    682 		}
    683 
    684 		if (ln->ln_router && !is_router) {
    685 			/*
    686 			 * The peer dropped the router flag.
    687 			 * Remove the sender from the Default Router List and
    688 			 * update the Destination Cache entries.
    689 			 */
    690 			struct nd_defrouter *dr;
    691 			struct in6_addr *in6;
    692 			int s;
    693 
    694 			in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
    695 			s = splsoftnet();
    696 			dr = defrouter_lookup(in6, rt->rt_ifp);
    697 			if (dr)
    698 				defrtrlist_del(dr);
    699 			else if (!ip6_forwarding && ip6_accept_rtadv) {
    700 				/*
    701 				 * Even if the neighbor is not in the default
    702 				 * router list, the neighbor may be used
    703 				 * as a next hop for some destinations
    704 				 * (e.g. redirect case). So we must
    705 				 * call rt6_flush explicitly.
    706 				 */
    707 				rt6_flush(&ip6->ip6_src, rt->rt_ifp);
    708 			}
    709 			splx(s);
    710 		}
    711 		ln->ln_router = is_router;
    712 	}
    713 	rt->rt_flags &= ~RTF_REJECT;
    714 	ln->ln_asked = 0;
    715 	if (ln->ln_hold) {
    716 #ifdef OLDIP6OUTPUT
    717 		(*ifp->if_output)(ifp, ln->ln_hold, rt_key(rt), rt);
    718 #else
    719 		nd6_output(ifp, ln->ln_hold,
    720 			   (struct sockaddr_in6 *)rt_key(rt), rt);
    721 #endif
    722 		ln->ln_hold = 0;
    723 	}
    724 }
    725 
    726 /*
    727  * Neighbor advertisement output handling.
    728  *
    729  * Based on RFC 2461
    730  *
    731  * XXX NA delay for anycast address is not implemented yet
    732  *      (RFC 2461 7.2.7)
    733  * XXX proxy advertisement?
    734  */
    735 void
    736 nd6_na_output(ifp, daddr6, taddr6, flags, tlladdr)
    737 	struct ifnet *ifp;
    738 	struct in6_addr *daddr6, *taddr6;
    739 	u_long flags;
    740 	int tlladdr;	/* 1 if include target link-layer address */
    741 {
    742 	struct mbuf *m;
    743 	struct ip6_hdr *ip6;
    744 	struct nd_neighbor_advert *nd_na;
    745 	struct in6_ifaddr *ia = NULL;
    746 	struct ip6_moptions im6o;
    747 	int icmp6len;
    748 	int maxlen;
    749 	caddr_t mac;
    750 	struct ifnet *outif = NULL;
    751 
    752 	/* estimate the size of message */
    753 	maxlen = sizeof(*ip6) + sizeof(*nd_na);
    754 	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
    755 	if (max_linkhdr + maxlen >= MCLBYTES) {
    756 #ifdef DIAGNOSTIC
    757 		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
    758 		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
    759 #endif
    760 		return;
    761 	}
    762 
    763 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    764 	if (m && max_linkhdr + maxlen >= MHLEN) {
    765 		MCLGET(m, M_DONTWAIT);
    766 		if ((m->m_flags & M_EXT) == 0) {
    767 			m_free(m);
    768 			m = NULL;
    769 		}
    770 	}
    771 	if (m == NULL)
    772 		return;
    773 
    774 	if (IN6_IS_ADDR_MULTICAST(daddr6)) {
    775 		m->m_flags |= M_MCAST;
    776 		im6o.im6o_multicast_ifp = ifp;
    777 		im6o.im6o_multicast_hlim = 255;
    778 		im6o.im6o_multicast_loop = 0;
    779 	}
    780 
    781 	icmp6len = sizeof(*nd_na);
    782 	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
    783 	m->m_data += max_linkhdr;	/*or MH_ALIGN() equivalent?*/
    784 
    785 	/* fill neighbor advertisement packet */
    786 	ip6 = mtod(m, struct ip6_hdr *);
    787 	ip6->ip6_flow = 0;
    788 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
    789 	ip6->ip6_vfc |= IPV6_VERSION;
    790 	ip6->ip6_nxt = IPPROTO_ICMPV6;
    791 	ip6->ip6_hlim = 255;
    792 	if (IN6_IS_ADDR_UNSPECIFIED(daddr6)) {
    793 		/* reply to DAD */
    794 		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
    795 		ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
    796 		ip6->ip6_dst.s6_addr32[1] = 0;
    797 		ip6->ip6_dst.s6_addr32[2] = 0;
    798 		ip6->ip6_dst.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
    799 		flags &= ~ND_NA_FLAG_SOLICITED;
    800 	} else
    801 		ip6->ip6_dst = *daddr6;
    802 
    803 	/*
    804 	 * Select a source whose scope is the same as that of the dest.
    805 	 */
    806 	ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
    807 	if (ia == NULL) {
    808 		m_freem(m);
    809 		return;
    810 	}
    811 	ip6->ip6_src = ia->ia_addr.sin6_addr;
    812 	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
    813 	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
    814 	nd_na->nd_na_code = 0;
    815 	nd_na->nd_na_target = *taddr6;
    816 	if (IN6_IS_SCOPE_LINKLOCAL(&nd_na->nd_na_target))
    817 		nd_na->nd_na_target.s6_addr16[1] = 0;
    818 
    819 	/*
    820 	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
    821 	 * see nd6_ns_input() for details.
    822 	 * Basically, if NS packet is sent to unicast/anycast addr,
    823 	 * target lladdr option SHOULD NOT be included.
    824 	 */
    825 	if (tlladdr && (mac = nd6_ifptomac(ifp))) {
    826 		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
    827 		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
    828 
    829 		/* roundup to 8 bytes alignment! */
    830 		optlen = (optlen + 7) & ~7;
    831 
    832 		m->m_pkthdr.len += optlen;
    833 		m->m_len += optlen;
    834 		icmp6len += optlen;
    835 		bzero((caddr_t)nd_opt, optlen);
    836 		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
    837 		nd_opt->nd_opt_len = optlen >> 3;
    838 		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
    839 	} else
    840 		flags &= ~ND_NA_FLAG_OVERRIDE;
    841 
    842 	ip6->ip6_plen = htons((u_short)icmp6len);
    843 	nd_na->nd_na_flags_reserved = flags;
    844 	nd_na->nd_na_cksum = 0;
    845 	nd_na->nd_na_cksum =
    846 		in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
    847 
    848 #ifdef IPSEC
    849 	m->m_pkthdr.rcvif = NULL;
    850 #endif /*IPSEC*/
    851 	ip6_output(m, NULL, NULL, 0, &im6o, &outif);
    852 	if (outif) {
    853 		icmp6_ifstat_inc(outif, ifs6_out_msg);
    854 		icmp6_ifstat_inc(outif, ifs6_out_neighboradvert);
    855 	}
    856 	icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
    857 }
    858 
    859 caddr_t
    860 nd6_ifptomac(ifp)
    861 	struct ifnet *ifp;
    862 {
    863 	switch (ifp->if_type) {
    864 	case IFT_ARCNET:
    865 	case IFT_ETHER:
    866 	case IFT_FDDI:
    867 		return LLADDR(ifp->if_sadl);
    868 		break;
    869 	default:
    870 		return NULL;
    871 	}
    872 }
    873 
    874 TAILQ_HEAD(dadq_head, dadq);
    875 struct dadq {
    876 	TAILQ_ENTRY(dadq) dad_list;
    877 	struct ifaddr *dad_ifa;
    878 	int dad_count;		/* max NS to send */
    879 	int dad_ns_tcount;	/* # of trials to send NS */
    880 	int dad_ns_ocount;	/* NS sent so far */
    881 	int dad_ns_icount;
    882 	int dad_na_icount;
    883 };
    884 
    885 static struct dadq_head dadq;
    886 
    887 static struct dadq *
    888 nd6_dad_find(ifa)
    889 	struct ifaddr *ifa;
    890 {
    891 	struct dadq *dp;
    892 
    893 	for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
    894 		if (dp->dad_ifa == ifa)
    895 			return dp;
    896 	}
    897 	return NULL;
    898 }
    899 
    900 /*
    901  * Start Duplicated Address Detection (DAD) for specified interface address.
    902  */
    903 void
    904 nd6_dad_start(ifa, tick)
    905 	struct ifaddr *ifa;
    906 	int *tick;	/* minimum delay ticks for IFF_UP event */
    907 {
    908 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
    909 	struct dadq *dp;
    910 	static int dad_init = 0;
    911 
    912 	if (!dad_init) {
    913 		TAILQ_INIT(&dadq);
    914 		dad_init++;
    915 	}
    916 
    917 	/*
    918 	 * If we don't need DAD, don't do it.
    919 	 * There are several cases:
    920 	 * - DAD is disabled (ip6_dad_count == 0)
    921 	 * - the interface address is anycast
    922 	 */
    923 	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
    924 		log(LOG_DEBUG,
    925 			"nd6_dad_start: called with non-tentative address "
    926 			"%s(%s)\n",
    927 			ip6_sprintf(&ia->ia_addr.sin6_addr),
    928 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
    929 		return;
    930 	}
    931 	if (ia->ia6_flags & IN6_IFF_ANYCAST) {
    932 		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
    933 		return;
    934 	}
    935 	if (!ip6_dad_count) {
    936 		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
    937 		return;
    938 	}
    939 	if (!ifa->ifa_ifp)
    940 		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
    941 	if (!(ifa->ifa_ifp->if_flags & IFF_UP))
    942 		return;
    943 	if (nd6_dad_find(ifa) != NULL) {
    944 		/* DAD already in progress */
    945 		return;
    946 	}
    947 
    948 	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
    949 	if (dp == NULL) {
    950 		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
    951 			"%s(%s)\n",
    952 			ip6_sprintf(&ia->ia_addr.sin6_addr),
    953 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
    954 		return;
    955 	}
    956 	bzero(dp, sizeof(*dp));
    957 	TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
    958 
    959 	log(LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
    960 	    ip6_sprintf(&ia->ia_addr.sin6_addr));
    961 
    962 	/*
    963 	 * Send NS packet for DAD, ip6_dad_count times.
    964 	 * Note that we must delay the first transmission, if this is the
    965 	 * first packet to be sent from the interface after interface
    966 	 * (re)initialization.
    967 	 */
    968 	dp->dad_ifa = ifa;
    969 	IFAREF(ifa);		/* just for safety */
    970 	dp->dad_count = ip6_dad_count;
    971 	dp->dad_ns_icount = dp->dad_na_icount = 0;
    972 	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
    973 	if (!tick) {
    974 		nd6_dad_ns_output(dp, ifa);
    975 		timeout((void (*) __P((void *)))nd6_dad_timer, (void *)ifa,
    976 			nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000);
    977 	} else {
    978 		int ntick;
    979 
    980 		if (*tick == 0)
    981 			ntick = random() % (MAX_RTR_SOLICITATION_DELAY * hz);
    982 		else
    983 			ntick = *tick + random() % (hz / 2);
    984 		*tick = ntick;
    985 		timeout((void (*) __P((void *)))nd6_dad_timer, (void *)ifa,
    986 			ntick);
    987 	}
    988 }
    989 
    990 static void
    991 nd6_dad_timer(ifa)
    992 	struct ifaddr *ifa;
    993 {
    994 	int s;
    995 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
    996 	struct dadq *dp;
    997 
    998 	s = splsoftnet();	/*XXX*/
    999 
   1000 	/* Sanity check */
   1001 	if (ia == NULL) {
   1002 		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
   1003 		goto done;
   1004 	}
   1005 	dp = nd6_dad_find(ifa);
   1006 	if (dp == NULL) {
   1007 		log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
   1008 		goto done;
   1009 	}
   1010 	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
   1011 		log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
   1012 			"%s(%s)\n",
   1013 			ip6_sprintf(&ia->ia_addr.sin6_addr),
   1014 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1015 		goto done;
   1016 	}
   1017 	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
   1018 		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
   1019 			"%s(%s)\n",
   1020 			ip6_sprintf(&ia->ia_addr.sin6_addr),
   1021 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1022 		goto done;
   1023 	}
   1024 
   1025 	/* timeouted with IFF_{RUNNING,UP} check */
   1026 	if (dp->dad_ns_tcount > dad_maxtry) {
   1027 		log(LOG_ERR, "%s: could not run DAD, driver problem?\n",
   1028 		    if_name(ifa->ifa_ifp));
   1029 
   1030 		TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
   1031 		free(dp, M_IP6NDP);
   1032 		dp = NULL;
   1033 		IFAFREE(ifa);
   1034 		goto done;
   1035 	}
   1036 
   1037 	/* Need more checks? */
   1038 	if (dp->dad_ns_ocount < dp->dad_count) {
   1039 		/*
   1040 		 * We have more NS to go.  Send NS packet for DAD.
   1041 		 */
   1042 		nd6_dad_ns_output(dp, ifa);
   1043 		timeout((void (*) __P((void *)))nd6_dad_timer, (void *)ifa,
   1044 			nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000);
   1045 	} else {
   1046 		/*
   1047 		 * We have transmitted sufficient number of DAD packets.
   1048 		 * See what we've got.
   1049 		 */
   1050 		int duplicate;
   1051 
   1052 		duplicate = 0;
   1053 
   1054 		if (dp->dad_na_icount) {
   1055 			/*
   1056 			 * the check is in nd6_dad_na_input(),
   1057 			 * but just in case
   1058 			 */
   1059 			duplicate++;
   1060 		}
   1061 
   1062 		if (dp->dad_ns_icount) {
   1063 #if 0 /*heuristics*/
   1064 			/*
   1065 			 * if
   1066 			 * - we have sent many(?) DAD NS, and
   1067 			 * - the number of NS we sent equals to the
   1068 			 *   number of NS we've got, and
   1069 			 * - we've got no NA
   1070 			 * we may have a faulty network card/driver which
   1071 			 * loops back multicasts to myself.
   1072 			 */
   1073 			if (3 < dp->dad_count
   1074 			 && dp->dad_ns_icount == dp->dad_count
   1075 			 && dp->dad_na_icount == 0) {
   1076 				log(LOG_INFO, "DAD questionable for %s(%s): "
   1077 					"network card loops back multicast?\n",
   1078 					ip6_sprintf(&ia->ia_addr.sin6_addr),
   1079 					if_name(ifa->ifa_ifp));
   1080 				/* XXX consider it a duplicate or not? */
   1081 				/* duplicate++; */
   1082 			} else {
   1083 				/* We've seen NS, means DAD has failed. */
   1084 				duplicate++;
   1085 			}
   1086 #else
   1087 			/* We've seen NS, means DAD has failed. */
   1088 			duplicate++;
   1089 #endif
   1090 		}
   1091 
   1092 		if (duplicate) {
   1093 			/* (*dp) will be freed in nd6_dad_duplicated() */
   1094 			dp = NULL;
   1095 			nd6_dad_duplicated(ifa);
   1096 		} else {
   1097 			/*
   1098 			 * We are done with DAD.  No NA came, no NS came.
   1099 			 * duplicated address found.
   1100 			 */
   1101 			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
   1102 
   1103 			log(LOG_INFO,
   1104 			    "%s: DAD complete for %s - no duplicates found\n",
   1105 			    if_name(ifa->ifa_ifp),
   1106 			    ip6_sprintf(&ia->ia_addr.sin6_addr));
   1107 
   1108 			TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
   1109 			free(dp, M_IP6NDP);
   1110 			dp = NULL;
   1111 			IFAFREE(ifa);
   1112 		}
   1113 	}
   1114 
   1115 done:
   1116 	splx(s);
   1117 }
   1118 
   1119 void
   1120 nd6_dad_duplicated(ifa)
   1121 	struct ifaddr *ifa;
   1122 {
   1123 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
   1124 	struct dadq *dp;
   1125 
   1126 	dp = nd6_dad_find(ifa);
   1127 	if (dp == NULL) {
   1128 		log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
   1129 		return;
   1130 	}
   1131 
   1132 	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: %d NS, "
   1133 	    "%d NA\n", if_name(ifa->ifa_ifp),
   1134 	    ip6_sprintf(&ia->ia_addr.sin6_addr),
   1135 	    dp->dad_ns_icount, dp->dad_na_icount);
   1136 
   1137 	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
   1138 	ia->ia6_flags |= IN6_IFF_DUPLICATED;
   1139 
   1140 	/* We are done with DAD, with duplicated address found. (failure) */
   1141 	untimeout((void (*) __P((void *)))nd6_dad_timer, (void *)ifa);
   1142 
   1143 	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
   1144 	    if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
   1145 	log(LOG_ERR, "%s: manual intervention required\n",
   1146 	    if_name(ifa->ifa_ifp));
   1147 
   1148 	TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
   1149 	free(dp, M_IP6NDP);
   1150 	dp = NULL;
   1151 	IFAFREE(ifa);
   1152 }
   1153 
   1154 static void
   1155 nd6_dad_ns_output(dp, ifa)
   1156 	struct dadq *dp;
   1157 	struct ifaddr *ifa;
   1158 {
   1159 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
   1160 	struct ifnet *ifp = ifa->ifa_ifp;
   1161 
   1162 	dp->dad_ns_tcount++;
   1163 	if ((ifp->if_flags & IFF_UP) == 0) {
   1164 #if 0
   1165 		printf("%s: interface down?\n", if_name(ifp));
   1166 #endif
   1167 		return;
   1168 	}
   1169 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
   1170 #if 0
   1171 		printf("%s: interface not running?\n", if_name(ifp));
   1172 #endif
   1173 		return;
   1174 	}
   1175 
   1176 	dp->dad_ns_ocount++;
   1177 	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
   1178 }
   1179 
   1180 static void
   1181 nd6_dad_ns_input(ifa)
   1182 	struct ifaddr *ifa;
   1183 {
   1184 	struct in6_ifaddr *ia;
   1185 	struct ifnet *ifp;
   1186 	struct in6_addr *taddr6;
   1187 	struct dadq *dp;
   1188 	int duplicate;
   1189 
   1190 	if (!ifa)
   1191 		panic("ifa == NULL in nd6_dad_ns_input");
   1192 
   1193 	ia = (struct in6_ifaddr *)ifa;
   1194 	ifp = ifa->ifa_ifp;
   1195 	taddr6 = &ia->ia_addr.sin6_addr;
   1196 	duplicate = 0;
   1197 	dp = nd6_dad_find(ifa);
   1198 
   1199 	/*
   1200 	 * If it is from myself, ignore this.
   1201 	 */
   1202 	if (ifp && (ifp->if_flags & IFF_LOOPBACK))
   1203 		return;
   1204 
   1205 	/* Quickhack - completely ignore DAD NS packets */
   1206 	if (dad_ignore_ns) {
   1207 		log(LOG_INFO, "nd6_dad_ns_input: ignoring DAD NS packet for "
   1208 		    "address %s(%s)\n", ip6_sprintf(taddr6),
   1209 		    if_name(ifa->ifa_ifp));
   1210 		return;
   1211 	}
   1212 
   1213 	/*
   1214 	 * if I'm yet to start DAD, someone else started using this address
   1215 	 * first.  I have a duplicate and you win.
   1216 	 */
   1217 	if (!dp || dp->dad_ns_ocount == 0)
   1218 		duplicate++;
   1219 
   1220 	/* XXX more checks for loopback situation - see nd6_dad_timer too */
   1221 
   1222 	if (duplicate) {
   1223 		dp = NULL;	/* will be freed in nd6_dad_duplicated() */
   1224 		nd6_dad_duplicated(ifa);
   1225 	} else {
   1226 		/*
   1227 		 * not sure if I got a duplicate.
   1228 		 * increment ns count and see what happens.
   1229 		 */
   1230 		if (dp)
   1231 			dp->dad_ns_icount++;
   1232 	}
   1233 }
   1234 
   1235 static void
   1236 nd6_dad_na_input(ifa)
   1237 	struct ifaddr *ifa;
   1238 {
   1239 	struct dadq *dp;
   1240 
   1241 	if (!ifa)
   1242 		panic("ifa == NULL in nd6_dad_na_input");
   1243 
   1244 	dp = nd6_dad_find(ifa);
   1245 	if (dp)
   1246 		dp->dad_na_icount++;
   1247 
   1248 	/* remove the address. */
   1249 	nd6_dad_duplicated(ifa);
   1250 }
   1251