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