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