nd6_nbr.c revision 95023
162587Sitojun/*	$FreeBSD: head/sys/netinet6/nd6_nbr.c 95023 2002-04-19 04:46:24Z suz $	*/
295023Ssuz/*	$KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $	*/
362587Sitojun
453541Sshin/*
553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
653541Sshin * All rights reserved.
753541Sshin *
853541Sshin * Redistribution and use in source and binary forms, with or without
953541Sshin * modification, are permitted provided that the following conditions
1053541Sshin * are met:
1153541Sshin * 1. Redistributions of source code must retain the above copyright
1253541Sshin *    notice, this list of conditions and the following disclaimer.
1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1453541Sshin *    notice, this list of conditions and the following disclaimer in the
1553541Sshin *    documentation and/or other materials provided with the distribution.
1653541Sshin * 3. Neither the name of the project nor the names of its contributors
1753541Sshin *    may be used to endorse or promote products derived from this software
1853541Sshin *    without specific prior written permission.
1953541Sshin *
2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2353541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3053541Sshin * SUCH DAMAGE.
3153541Sshin */
3253541Sshin
3362587Sitojun#include "opt_inet.h"
3462587Sitojun#include "opt_inet6.h"
3555009Sshin#include "opt_ipsec.h"
3655009Sshin
3753541Sshin#include <sys/param.h>
3853541Sshin#include <sys/systm.h>
3953541Sshin#include <sys/malloc.h>
4053541Sshin#include <sys/mbuf.h>
4153541Sshin#include <sys/socket.h>
4253541Sshin#include <sys/sockio.h>
4353541Sshin#include <sys/time.h>
4453541Sshin#include <sys/kernel.h>
4553541Sshin#include <sys/errno.h>
4653541Sshin#include <sys/syslog.h>
4753541Sshin#include <sys/queue.h>
4878064Sume#include <sys/callout.h>
4953541Sshin
5053541Sshin#include <net/if.h>
5153541Sshin#include <net/if_types.h>
5253541Sshin#include <net/if_dl.h>
5353541Sshin#include <net/route.h>
5453541Sshin
5553541Sshin#include <netinet/in.h>
5653541Sshin#include <netinet/in_var.h>
5753541Sshin#include <netinet6/in6_var.h>
5862587Sitojun#include <netinet/ip6.h>
5953541Sshin#include <netinet6/ip6_var.h>
6053541Sshin#include <netinet6/nd6.h>
6162587Sitojun#include <netinet/icmp6.h>
6253541Sshin
6362587Sitojun#ifdef IPSEC
6462587Sitojun#include <netinet6/ipsec.h>
6562587Sitojun#ifdef INET6
6662587Sitojun#include <netinet6/ipsec6.h>
6762587Sitojun#endif
6862587Sitojun#endif
6962587Sitojun
7053541Sshin#include <net/net_osdep.h>
7153541Sshin
7262587Sitojun#define SDL(s) ((struct sockaddr_dl *)s)
7353541Sshin
7462587Sitojunstruct dadq;
7562587Sitojunstatic struct dadq *nd6_dad_find __P((struct ifaddr *));
7678064Sumestatic void nd6_dad_starttimer __P((struct dadq *, int));
7778064Sumestatic void nd6_dad_stoptimer __P((struct dadq *));
7862587Sitojunstatic void nd6_dad_timer __P((struct ifaddr *));
7962587Sitojunstatic void nd6_dad_ns_output __P((struct dadq *, struct ifaddr *));
8062587Sitojunstatic void nd6_dad_ns_input __P((struct ifaddr *));
8162587Sitojunstatic void nd6_dad_na_input __P((struct ifaddr *));
8253541Sshin
8362587Sitojunstatic int dad_ignore_ns = 0;	/* ignore NS in DAD - specwise incorrect*/
8462587Sitojunstatic int dad_maxtry = 15;	/* max # of *tries* to transmit DAD packet */
8553541Sshin
8653541Sshin/*
8753541Sshin * Input an Neighbor Solicitation Message.
8853541Sshin *
8953541Sshin * Based on RFC 2461
9053541Sshin * Based on RFC 2462 (duplicated address detection)
9153541Sshin */
9253541Sshinvoid
9353541Sshinnd6_ns_input(m, off, icmp6len)
9453541Sshin	struct mbuf *m;
9553541Sshin	int off, icmp6len;
9653541Sshin{
9753541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
9853541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
9962587Sitojun	struct nd_neighbor_solicit *nd_ns;
10053541Sshin	struct in6_addr saddr6 = ip6->ip6_src;
10153541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
10262587Sitojun	struct in6_addr taddr6;
10353541Sshin	struct in6_addr myaddr6;
10453541Sshin	char *lladdr = NULL;
10553541Sshin	struct ifaddr *ifa;
10653541Sshin	int lladdrlen = 0;
10753541Sshin	int anycast = 0, proxy = 0, tentative = 0;
10853541Sshin	int tlladdr;
10953541Sshin	union nd_opts ndopts;
11062587Sitojun	struct sockaddr_dl *proxydl = NULL;
11153541Sshin
11278064Sume#ifndef PULLDOWN_TEST
11378064Sume	IP6_EXTHDR_CHECK(m, off, icmp6len,);
11478064Sume	nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
11578064Sume#else
11678064Sume	IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
11778064Sume	if (nd_ns == NULL) {
11878064Sume		icmp6stat.icp6s_tooshort++;
11978064Sume		return;
12078064Sume	}
12178064Sume#endif
12278064Sume	ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
12378064Sume	taddr6 = nd_ns->nd_ns_target;
12478064Sume
12553541Sshin	if (ip6->ip6_hlim != 255) {
12678064Sume		nd6log((LOG_ERR,
12778064Sume		    "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
12878064Sume		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
12978064Sume		    ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
13078064Sume		goto bad;
13153541Sshin	}
13253541Sshin
13353541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
13453541Sshin		/* dst has to be solicited node multicast address. */
13553541Sshin		if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL
13695023Ssuz		    /* don't check ifindex portion */
13753541Sshin		    && daddr6.s6_addr32[1] == 0
13853541Sshin		    && daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE
13953541Sshin		    && daddr6.s6_addr8[12] == 0xff) {
14095023Ssuz			; /* good */
14153541Sshin		} else {
14278064Sume			nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
14378064Sume				"(wrong ip6 dst)\n"));
14453541Sshin			goto bad;
14553541Sshin		}
14653541Sshin	}
14753541Sshin
14853541Sshin	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
14978064Sume		nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
15053541Sshin		goto bad;
15153541Sshin	}
15253541Sshin
15353541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
15453541Sshin		taddr6.s6_addr16[1] = htons(ifp->if_index);
15553541Sshin
15653541Sshin	icmp6len -= sizeof(*nd_ns);
15753541Sshin	nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
15853541Sshin	if (nd6_options(&ndopts) < 0) {
15978064Sume		nd6log((LOG_INFO,
16078064Sume		    "nd6_ns_input: invalid ND option, ignored\n"));
16178064Sume		/* nd6_options have incremented stats */
16278064Sume		goto freeit;
16353541Sshin	}
16453541Sshin
16553541Sshin	if (ndopts.nd_opts_src_lladdr) {
16695023Ssuz		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
16753541Sshin		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
16853541Sshin	}
16953541Sshin
17053541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
17178064Sume		nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
17278064Sume		    "(link-layer address option)\n"));
17353541Sshin		goto bad;
17453541Sshin	}
17553541Sshin
17653541Sshin	/*
17753541Sshin	 * Attaching target link-layer address to the NA?
17853541Sshin	 * (RFC 2461 7.2.4)
17953541Sshin	 *
18053541Sshin	 * NS IP dst is unicast/anycast			MUST NOT add
18153541Sshin	 * NS IP dst is solicited-node multicast	MUST add
18253541Sshin	 *
18353541Sshin	 * In implementation, we add target link-layer address by default.
18453541Sshin	 * We do not add one in MUST NOT cases.
18553541Sshin	 */
18662587Sitojun#if 0 /* too much! */
18762587Sitojun	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &daddr6);
18862587Sitojun	if (ifa && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST))
18962587Sitojun		tlladdr = 0;
19062587Sitojun	else
19162587Sitojun#endif
19253541Sshin	if (!IN6_IS_ADDR_MULTICAST(&daddr6))
19353541Sshin		tlladdr = 0;
19453541Sshin	else
19553541Sshin		tlladdr = 1;
19653541Sshin
19753541Sshin	/*
19853541Sshin	 * Target address (taddr6) must be either:
19953541Sshin	 * (1) Valid unicast/anycast address for my receiving interface,
20053541Sshin	 * (2) Unicast address for which I'm offering proxy service, or
20153541Sshin	 * (3) "tentative" address on which DAD is being performed.
20253541Sshin	 */
20353541Sshin	/* (1) and (3) check. */
20453541Sshin	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
20553541Sshin
20653541Sshin	/* (2) check. */
20762587Sitojun	if (!ifa) {
20853541Sshin		struct rtentry *rt;
20953541Sshin		struct sockaddr_in6 tsin6;
21053541Sshin
21153541Sshin		bzero(&tsin6, sizeof tsin6);
21253541Sshin		tsin6.sin6_len = sizeof(struct sockaddr_in6);
21353541Sshin		tsin6.sin6_family = AF_INET6;
21453541Sshin		tsin6.sin6_addr = taddr6;
21553541Sshin
21653541Sshin		rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
21762587Sitojun		if (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
21862587Sitojun		    rt->rt_gateway->sa_family == AF_LINK) {
21953541Sshin			/*
22062587Sitojun			 * proxy NDP for single entry
22153541Sshin			 */
22262587Sitojun			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
22362587Sitojun				IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
22462587Sitojun			if (ifa) {
22553541Sshin				proxy = 1;
22662587Sitojun				proxydl = SDL(rt->rt_gateway);
22762587Sitojun			}
22853541Sshin		}
22962587Sitojun		if (rt)
23062587Sitojun			rtfree(rt);
23153541Sshin	}
23253541Sshin	if (!ifa) {
23353541Sshin		/*
23478064Sume		 * We've got an NS packet, and we don't have that adddress
23553541Sshin		 * assigned for us.  We MUST silently ignore it.
23653541Sshin		 * See RFC2461 7.2.3.
23753541Sshin		 */
23862587Sitojun		goto freeit;
23953541Sshin	}
24053541Sshin	myaddr6 = *IFA_IN6(ifa);
24153541Sshin	anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
24253541Sshin	tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
24353541Sshin	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
24462587Sitojun		goto freeit;
24553541Sshin
24653541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
24778064Sume		nd6log((LOG_INFO,
24853541Sshin		    "nd6_ns_input: lladdrlen mismatch for %s "
24953541Sshin		    "(if %d, NS packet %d)\n",
25078064Sume			ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
25178064Sume		goto bad;
25253541Sshin	}
25353541Sshin
25453541Sshin	if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
25595023Ssuz		nd6log((LOG_INFO,
25695023Ssuz			"nd6_ns_input: duplicate IP6 address %s\n",
25795023Ssuz			ip6_sprintf(&saddr6)));
25862587Sitojun		goto freeit;
25953541Sshin	}
26053541Sshin
26153541Sshin	/*
26253541Sshin	 * We have neighbor solicitation packet, with target address equals to
26353541Sshin	 * one of my tentative address.
26453541Sshin	 *
26553541Sshin	 * src addr	how to process?
26653541Sshin	 * ---		---
26753541Sshin	 * multicast	of course, invalid (rejected in ip6_input)
26853541Sshin	 * unicast	somebody is doing address resolution -> ignore
26953541Sshin	 * unspec	dup address detection
27053541Sshin	 *
27153541Sshin	 * The processing is defined in RFC 2462.
27253541Sshin	 */
27353541Sshin	if (tentative) {
27453541Sshin		/*
27553541Sshin		 * If source address is unspecified address, it is for
27653541Sshin		 * duplicated address detection.
27753541Sshin		 *
27853541Sshin		 * If not, the packet is for addess resolution;
27953541Sshin		 * silently ignore it.
28053541Sshin		 */
28153541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
28253541Sshin			nd6_dad_ns_input(ifa);
28353541Sshin
28462587Sitojun		goto freeit;
28553541Sshin	}
28653541Sshin
28753541Sshin	/*
28853541Sshin	 * If the source address is unspecified address, entries must not
28953541Sshin	 * be created or updated.
29053541Sshin	 * It looks that sender is performing DAD.  Output NA toward
29153541Sshin	 * all-node multicast address, to tell the sender that I'm using
29253541Sshin	 * the address.
29353541Sshin	 * S bit ("solicited") must be zero.
29453541Sshin	 */
29553541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
29653541Sshin		saddr6 = in6addr_linklocal_allnodes;
29753541Sshin		saddr6.s6_addr16[1] = htons(ifp->if_index);
29853541Sshin		nd6_na_output(ifp, &saddr6, &taddr6,
29953541Sshin			      ((anycast || proxy || !tlladdr)
30053541Sshin				      ? 0 : ND_NA_FLAG_OVERRIDE)
30153541Sshin			      	| (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
30262587Sitojun			      tlladdr, (struct sockaddr *)proxydl);
30362587Sitojun		goto freeit;
30453541Sshin	}
30553541Sshin
30653541Sshin	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_NEIGHBOR_SOLICIT, 0);
30753541Sshin
30853541Sshin	nd6_na_output(ifp, &saddr6, &taddr6,
30953541Sshin		      ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE)
31053541Sshin			| (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
31153541Sshin			| ND_NA_FLAG_SOLICITED,
31262587Sitojun		      tlladdr, (struct sockaddr *)proxydl);
31362587Sitojun freeit:
31462587Sitojun	m_freem(m);
31553541Sshin	return;
31653541Sshin
31753541Sshin bad:
31878064Sume	nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6)));
31978064Sume	nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6)));
32078064Sume	nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6)));
32178064Sume	icmp6stat.icp6s_badns++;
32262587Sitojun	m_freem(m);
32353541Sshin}
32453541Sshin
32553541Sshin/*
32653541Sshin * Output an Neighbor Solicitation Message. Caller specifies:
32753541Sshin *	- ICMP6 header source IP6 address
32853541Sshin *	- ND6 header target IP6 address
32953541Sshin *	- ND6 header source datalink address
33053541Sshin *
33153541Sshin * Based on RFC 2461
33253541Sshin * Based on RFC 2462 (duplicated address detection)
33353541Sshin */
33453541Sshinvoid
33553541Sshinnd6_ns_output(ifp, daddr6, taddr6, ln, dad)
33653541Sshin	struct ifnet *ifp;
33778064Sume	const struct in6_addr *daddr6, *taddr6;
33853541Sshin	struct llinfo_nd6 *ln;	/* for source address determination */
33953541Sshin	int dad;	/* duplicated address detection */
34053541Sshin{
34153541Sshin	struct mbuf *m;
34253541Sshin	struct ip6_hdr *ip6;
34353541Sshin	struct nd_neighbor_solicit *nd_ns;
34453541Sshin	struct in6_ifaddr *ia = NULL;
34553541Sshin	struct ip6_moptions im6o;
34653541Sshin	int icmp6len;
34762587Sitojun	int maxlen;
34853541Sshin	caddr_t mac;
34953541Sshin	struct ifnet *outif = NULL;
35053541Sshin
35153541Sshin	if (IN6_IS_ADDR_MULTICAST(taddr6))
35253541Sshin		return;
35353541Sshin
35462587Sitojun	/* estimate the size of message */
35562587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
35662587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
35762587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
35862587Sitojun#ifdef DIAGNOSTIC
35962587Sitojun		printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
36062587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
36162587Sitojun#endif
36253541Sshin		return;
36362587Sitojun	}
36453541Sshin
36562587Sitojun	MGETHDR(m, M_DONTWAIT, MT_DATA);
36662587Sitojun	if (m && max_linkhdr + maxlen >= MHLEN) {
36762587Sitojun		MCLGET(m, M_DONTWAIT);
36862587Sitojun		if ((m->m_flags & M_EXT) == 0) {
36962587Sitojun			m_free(m);
37062587Sitojun			m = NULL;
37162587Sitojun		}
37262587Sitojun	}
37362587Sitojun	if (m == NULL)
37462587Sitojun		return;
37578064Sume	m->m_pkthdr.rcvif = NULL;
37662587Sitojun
37753541Sshin	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
37853541Sshin		m->m_flags |= M_MCAST;
37953541Sshin		im6o.im6o_multicast_ifp = ifp;
38053541Sshin		im6o.im6o_multicast_hlim = 255;
38153541Sshin		im6o.im6o_multicast_loop = 0;
38253541Sshin	}
38353541Sshin
38453541Sshin	icmp6len = sizeof(*nd_ns);
38553541Sshin	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
38695023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
38753541Sshin
38853541Sshin	/* fill neighbor solicitation packet */
38953541Sshin	ip6 = mtod(m, struct ip6_hdr *);
39053541Sshin	ip6->ip6_flow = 0;
39162587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
39262587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
39353541Sshin	/* ip6->ip6_plen will be set later */
39453541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
39553541Sshin	ip6->ip6_hlim = 255;
39653541Sshin	if (daddr6)
39753541Sshin		ip6->ip6_dst = *daddr6;
39853541Sshin	else {
39953541Sshin		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
40053541Sshin		ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
40153541Sshin		ip6->ip6_dst.s6_addr32[1] = 0;
40253541Sshin		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
40353541Sshin		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
40453541Sshin		ip6->ip6_dst.s6_addr8[12] = 0xff;
40553541Sshin	}
40653541Sshin	if (!dad) {
40762587Sitojun#if 0	/* KAME way, exact address scope match */
40853541Sshin		/*
40962587Sitojun		 * Select a source whose scope is the same as that of the dest.
41062587Sitojun		 * Typically, the dest is link-local solicitation multicast
41162587Sitojun		 * (i.e. neighbor discovery) or link-local/global unicast
41262587Sitojun		 * (i.e. neighbor un-reachability detection).
41362587Sitojun		 */
41462587Sitojun		ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
41562587Sitojun		if (ia == NULL) {
41662587Sitojun			m_freem(m);
41762587Sitojun			return;
41862587Sitojun		}
41962587Sitojun		ip6->ip6_src = ia->ia_addr.sin6_addr;
42062587Sitojun#else	/* spec-wise correct */
42162587Sitojun		/*
42253541Sshin		 * RFC2461 7.2.2:
42353541Sshin		 * "If the source address of the packet prompting the
42453541Sshin		 * solicitation is the same as one of the addresses assigned
42553541Sshin		 * to the outgoing interface, that address SHOULD be placed
42653541Sshin		 * in the IP Source Address of the outgoing solicitation.
42753541Sshin		 * Otherwise, any one of the addresses assigned to the
42853541Sshin		 * interface should be used."
42953541Sshin		 *
43053541Sshin		 * We use the source address for the prompting packet
43153541Sshin		 * (saddr6), if:
43253541Sshin		 * - saddr6 is given from the caller (by giving "ln"), and
43353541Sshin		 * - saddr6 belongs to the outgoing interface.
43453541Sshin		 * Otherwise, we perform a scope-wise match.
43553541Sshin		 */
43695023Ssuz		struct ip6_hdr *hip6;		/* hold ip6 */
43753541Sshin		struct in6_addr *saddr6;
43853541Sshin
43953541Sshin		if (ln && ln->ln_hold) {
44053541Sshin			hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
44153541Sshin			/* XXX pullup? */
44253541Sshin			if (sizeof(*hip6) < ln->ln_hold->m_len)
44353541Sshin				saddr6 = &hip6->ip6_src;
44453541Sshin			else
44553541Sshin				saddr6 = NULL;
44653541Sshin		} else
44753541Sshin			saddr6 = NULL;
44853541Sshin		if (saddr6 && in6ifa_ifpwithaddr(ifp, saddr6))
44953541Sshin			bcopy(saddr6, &ip6->ip6_src, sizeof(*saddr6));
45053541Sshin		else {
45153541Sshin			ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
45253541Sshin			if (ia == NULL) {
45395023Ssuz				m_freem(m);
45453541Sshin				return;
45553541Sshin			}
45653541Sshin			ip6->ip6_src = ia->ia_addr.sin6_addr;
45753541Sshin		}
45862587Sitojun#endif
45953541Sshin	} else {
46053541Sshin		/*
46153541Sshin		 * Source address for DAD packet must always be IPv6
46253541Sshin		 * unspecified address. (0::0)
46353541Sshin		 */
46453541Sshin		bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
46553541Sshin	}
46653541Sshin	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
46753541Sshin	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
46853541Sshin	nd_ns->nd_ns_code = 0;
46953541Sshin	nd_ns->nd_ns_reserved = 0;
47053541Sshin	nd_ns->nd_ns_target = *taddr6;
47153541Sshin
47253541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&nd_ns->nd_ns_target))
47353541Sshin		nd_ns->nd_ns_target.s6_addr16[1] = 0;
47453541Sshin
47553541Sshin	/*
47653541Sshin	 * Add source link-layer address option.
47753541Sshin	 *
47853541Sshin	 *				spec		implementation
47953541Sshin	 *				---		---
48053541Sshin	 * DAD packet			MUST NOT	do not add the option
48153541Sshin	 * there's no link layer address:
48253541Sshin	 *				impossible	do not add the option
48353541Sshin	 * there's link layer address:
48453541Sshin	 *	Multicast NS		MUST add one	add the option
48553541Sshin	 *	Unicast NS		SHOULD add one	add the option
48653541Sshin	 */
48753541Sshin	if (!dad && (mac = nd6_ifptomac(ifp))) {
48853541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
48953541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
49053541Sshin		/* 8 byte alignments... */
49153541Sshin		optlen = (optlen + 7) & ~7;
49253541Sshin
49353541Sshin		m->m_pkthdr.len += optlen;
49453541Sshin		m->m_len += optlen;
49553541Sshin		icmp6len += optlen;
49653541Sshin		bzero((caddr_t)nd_opt, optlen);
49753541Sshin		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
49853541Sshin		nd_opt->nd_opt_len = optlen >> 3;
49953541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
50053541Sshin	}
50153541Sshin
50253541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
50353541Sshin	nd_ns->nd_ns_cksum = 0;
50453541Sshin	nd_ns->nd_ns_cksum
50553541Sshin		= in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
50653541Sshin
50762587Sitojun#ifdef IPSEC
50862587Sitojun	/* Don't lookup socket */
50978064Sume	(void)ipsec_setsocket(m, NULL);
51062587Sitojun#endif
51153541Sshin	ip6_output(m, NULL, NULL, dad ? IPV6_DADOUTPUT : 0, &im6o, &outif);
51253541Sshin	if (outif) {
51353541Sshin		icmp6_ifstat_inc(outif, ifs6_out_msg);
51453541Sshin		icmp6_ifstat_inc(outif, ifs6_out_neighborsolicit);
51553541Sshin	}
51653541Sshin	icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
51753541Sshin}
51853541Sshin
51953541Sshin/*
52053541Sshin * Neighbor advertisement input handling.
52153541Sshin *
52253541Sshin * Based on RFC 2461
52353541Sshin * Based on RFC 2462 (duplicated address detection)
52462587Sitojun *
52562587Sitojun * the following items are not implemented yet:
52662587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
52762587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
52853541Sshin */
52953541Sshinvoid
53053541Sshinnd6_na_input(m, off, icmp6len)
53153541Sshin	struct mbuf *m;
53253541Sshin	int off, icmp6len;
53353541Sshin{
53453541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
53553541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
53662587Sitojun	struct nd_neighbor_advert *nd_na;
53762587Sitojun#if 0
53862587Sitojun	struct in6_addr saddr6 = ip6->ip6_src;
53962587Sitojun#endif
54053541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
54162587Sitojun	struct in6_addr taddr6;
54262587Sitojun	int flags;
54362587Sitojun	int is_router;
54462587Sitojun	int is_solicited;
54562587Sitojun	int is_override;
54653541Sshin	char *lladdr = NULL;
54753541Sshin	int lladdrlen = 0;
54853541Sshin	struct ifaddr *ifa;
54953541Sshin	struct llinfo_nd6 *ln;
55053541Sshin	struct rtentry *rt;
55153541Sshin	struct sockaddr_dl *sdl;
55253541Sshin	union nd_opts ndopts;
55353541Sshin
55453541Sshin	if (ip6->ip6_hlim != 255) {
55578064Sume		nd6log((LOG_ERR,
55678064Sume		    "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
55778064Sume		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
55878064Sume		    ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
55978064Sume		goto bad;
56062587Sitojun	}
56162587Sitojun
56262587Sitojun#ifndef PULLDOWN_TEST
56362587Sitojun	IP6_EXTHDR_CHECK(m, off, icmp6len,);
56462587Sitojun	nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
56562587Sitojun#else
56662587Sitojun	IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
56762587Sitojun	if (nd_na == NULL) {
56862587Sitojun		icmp6stat.icp6s_tooshort++;
56953541Sshin		return;
57053541Sshin	}
57162587Sitojun#endif
57262587Sitojun	taddr6 = nd_na->nd_na_target;
57362587Sitojun	flags = nd_na->nd_na_flags_reserved;
57462587Sitojun	is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
57562587Sitojun	is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
57662587Sitojun	is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
57753541Sshin
57853541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&taddr6))
57953541Sshin		taddr6.s6_addr16[1] = htons(ifp->if_index);
58053541Sshin
58153541Sshin	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
58278064Sume		nd6log((LOG_ERR,
58353541Sshin		    "nd6_na_input: invalid target address %s\n",
58478064Sume		    ip6_sprintf(&taddr6)));
58578064Sume		goto bad;
58653541Sshin	}
58753541Sshin	if (IN6_IS_ADDR_MULTICAST(&daddr6))
58853541Sshin		if (is_solicited) {
58978064Sume			nd6log((LOG_ERR,
59078064Sume			    "nd6_na_input: a solicited adv is multicasted\n"));
59178064Sume			goto bad;
59253541Sshin		}
59353541Sshin
59453541Sshin	icmp6len -= sizeof(*nd_na);
59553541Sshin	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
59653541Sshin	if (nd6_options(&ndopts) < 0) {
59778064Sume		nd6log((LOG_INFO,
59878064Sume		    "nd6_na_input: invalid ND option, ignored\n"));
59978064Sume		/* nd6_options have incremented stats */
60062587Sitojun		goto freeit;
60153541Sshin	}
60253541Sshin
60353541Sshin	if (ndopts.nd_opts_tgt_lladdr) {
60453541Sshin		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
60553541Sshin		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
60653541Sshin	}
60753541Sshin
60853541Sshin	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
60953541Sshin
61053541Sshin	/*
61153541Sshin	 * Target address matches one of my interface address.
61253541Sshin	 *
61353541Sshin	 * If my address is tentative, this means that there's somebody
61453541Sshin	 * already using the same address as mine.  This indicates DAD failure.
61553541Sshin	 * This is defined in RFC 2462.
61653541Sshin	 *
61753541Sshin	 * Otherwise, process as defined in RFC 2461.
61853541Sshin	 */
61953541Sshin	if (ifa
62053541Sshin	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
62153541Sshin		nd6_dad_na_input(ifa);
62262587Sitojun		goto freeit;
62353541Sshin	}
62453541Sshin
62595023Ssuz	/* Just for safety, maybe unnecessary. */
62653541Sshin	if (ifa) {
62753541Sshin		log(LOG_ERR,
62853541Sshin		    "nd6_na_input: duplicate IP6 address %s\n",
62953541Sshin		    ip6_sprintf(&taddr6));
63062587Sitojun		goto freeit;
63153541Sshin	}
63253541Sshin
63353541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
63478064Sume		nd6log((LOG_INFO,
63553541Sshin		    "nd6_na_input: lladdrlen mismatch for %s "
63653541Sshin		    "(if %d, NA packet %d)\n",
63778064Sume			ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2));
63878064Sume		goto bad;
63953541Sshin	}
64053541Sshin
64153541Sshin	/*
64253541Sshin	 * If no neighbor cache entry is found, NA SHOULD silently be discarded.
64353541Sshin	 */
64453541Sshin	rt = nd6_lookup(&taddr6, 0, ifp);
64553541Sshin	if ((rt == NULL) ||
64653541Sshin	   ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
64753541Sshin	   ((sdl = SDL(rt->rt_gateway)) == NULL))
64862587Sitojun		goto freeit;
64953541Sshin
65053541Sshin	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
65153541Sshin		/*
65253541Sshin		 * If the link-layer has address, and no lladdr option came,
65353541Sshin		 * discard the packet.
65453541Sshin		 */
65553541Sshin		if (ifp->if_addrlen && !lladdr)
65662587Sitojun			goto freeit;
65753541Sshin
65853541Sshin		/*
65953541Sshin		 * Record link-layer address, and update the state.
66053541Sshin		 */
66153541Sshin		sdl->sdl_alen = ifp->if_addrlen;
66253541Sshin		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
66353541Sshin		if (is_solicited) {
66453541Sshin			ln->ln_state = ND6_LLINFO_REACHABLE;
66562587Sitojun			ln->ln_byhint = 0;
66653541Sshin			if (ln->ln_expire)
66753541Sshin				ln->ln_expire = time_second +
66878064Sume				    nd_ifinfo[rt->rt_ifp->if_index].reachable;
66978064Sume		} else {
67053541Sshin			ln->ln_state = ND6_LLINFO_STALE;
67178064Sume			ln->ln_expire = time_second + nd6_gctimer;
67278064Sume		}
67378064Sume		if ((ln->ln_router = is_router) != 0) {
67478064Sume			/*
67578064Sume			 * This means a router's state has changed from
67678064Sume			 * non-reachable to probably reachable, and might
67778064Sume			 * affect the status of associated prefixes..
67878064Sume			 */
67978064Sume			pfxlist_onlink_check();
68078064Sume		}
68153541Sshin	} else {
68253541Sshin		int llchange;
68353541Sshin
68453541Sshin		/*
68553541Sshin		 * Check if the link-layer address has changed or not.
68653541Sshin		 */
68753541Sshin		if (!lladdr)
68853541Sshin			llchange = 0;
68953541Sshin		else {
69053541Sshin			if (sdl->sdl_alen) {
69153541Sshin				if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
69253541Sshin					llchange = 1;
69353541Sshin				else
69453541Sshin					llchange = 0;
69553541Sshin			} else
69653541Sshin				llchange = 1;
69753541Sshin		}
69853541Sshin
69953541Sshin		/*
70053541Sshin		 * This is VERY complex.  Look at it with care.
70153541Sshin		 *
70253541Sshin		 * override solicit lladdr llchange	action
70353541Sshin		 *					(L: record lladdr)
70453541Sshin		 *
70553541Sshin		 *	0	0	n	--	(2c)
70653541Sshin		 *	0	0	y	n	(2b) L
70753541Sshin		 *	0	0	y	y	(1)    REACHABLE->STALE
70853541Sshin		 *	0	1	n	--	(2c)   *->REACHABLE
70953541Sshin		 *	0	1	y	n	(2b) L *->REACHABLE
71053541Sshin		 *	0	1	y	y	(1)    REACHABLE->STALE
71153541Sshin		 *	1	0	n	--	(2a)
71253541Sshin		 *	1	0	y	n	(2a) L
71353541Sshin		 *	1	0	y	y	(2a) L *->STALE
71453541Sshin		 *	1	1	n	--	(2a)   *->REACHABLE
71553541Sshin		 *	1	1	y	n	(2a) L *->REACHABLE
71653541Sshin		 *	1	1	y	y	(2a) L *->REACHABLE
71753541Sshin		 */
71853541Sshin		if (!is_override && (lladdr && llchange)) {	   /* (1) */
71953541Sshin			/*
72053541Sshin			 * If state is REACHABLE, make it STALE.
72153541Sshin			 * no other updates should be done.
72253541Sshin			 */
72378064Sume			if (ln->ln_state == ND6_LLINFO_REACHABLE) {
72453541Sshin				ln->ln_state = ND6_LLINFO_STALE;
72578064Sume				ln->ln_expire = time_second + nd6_gctimer;
72678064Sume			}
72762587Sitojun			goto freeit;
72853541Sshin		} else if (is_override				   /* (2a) */
72953541Sshin			|| (!is_override && (lladdr && !llchange)) /* (2b) */
73053541Sshin			|| !lladdr) {				   /* (2c) */
73153541Sshin			/*
73253541Sshin			 * Update link-local address, if any.
73353541Sshin			 */
73453541Sshin			if (lladdr) {
73553541Sshin				sdl->sdl_alen = ifp->if_addrlen;
73653541Sshin				bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
73753541Sshin			}
73853541Sshin
73953541Sshin			/*
74053541Sshin			 * If solicited, make the state REACHABLE.
74153541Sshin			 * If not solicited and the link-layer address was
74253541Sshin			 * changed, make it STALE.
74353541Sshin			 */
74453541Sshin			if (is_solicited) {
74553541Sshin				ln->ln_state = ND6_LLINFO_REACHABLE;
74662587Sitojun				ln->ln_byhint = 0;
74753541Sshin				if (ln->ln_expire) {
74853541Sshin					ln->ln_expire = time_second +
74978064Sume					    nd_ifinfo[ifp->if_index].reachable;
75053541Sshin				}
75153541Sshin			} else {
75278064Sume				if (lladdr && llchange) {
75353541Sshin					ln->ln_state = ND6_LLINFO_STALE;
75478064Sume					ln->ln_expire = time_second + nd6_gctimer;
75578064Sume				}
75653541Sshin			}
75753541Sshin		}
75853541Sshin
75953541Sshin		if (ln->ln_router && !is_router) {
76053541Sshin			/*
76153541Sshin			 * The peer dropped the router flag.
76253541Sshin			 * Remove the sender from the Default Router List and
76353541Sshin			 * update the Destination Cache entries.
76453541Sshin			 */
76553541Sshin			struct nd_defrouter *dr;
76653541Sshin			struct in6_addr *in6;
76753541Sshin			int s;
76853541Sshin
76953541Sshin			in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
77095023Ssuz
77195023Ssuz			/*
77295023Ssuz			 * Lock to protect the default router list.
77395023Ssuz			 * XXX: this might be unnecessary, since this function
77495023Ssuz			 * is only called under the network software interrupt
77595023Ssuz			 * context.  However, we keep it just for safety.
77695023Ssuz			 */
77753541Sshin			s = splnet();
77853541Sshin			dr = defrouter_lookup(in6, rt->rt_ifp);
77953541Sshin			if (dr)
78053541Sshin				defrtrlist_del(dr);
78153541Sshin			else if (!ip6_forwarding && ip6_accept_rtadv) {
78253541Sshin				/*
78353541Sshin				 * Even if the neighbor is not in the default
78453541Sshin				 * router list, the neighbor may be used
78553541Sshin				 * as a next hop for some destinations
78653541Sshin				 * (e.g. redirect case). So we must
78753541Sshin				 * call rt6_flush explicitly.
78853541Sshin				 */
78953541Sshin				rt6_flush(&ip6->ip6_src, rt->rt_ifp);
79053541Sshin			}
79153541Sshin			splx(s);
79253541Sshin		}
79353541Sshin		ln->ln_router = is_router;
79453541Sshin	}
79553541Sshin	rt->rt_flags &= ~RTF_REJECT;
79653541Sshin	ln->ln_asked = 0;
79753541Sshin	if (ln->ln_hold) {
79862587Sitojun		/*
79995023Ssuz		 * we assume ifp is not a loopback here, so just set the 2nd
80062587Sitojun		 * argument as the 1st one.
80162587Sitojun		 */
80262587Sitojun		nd6_output(ifp, ifp, ln->ln_hold,
80353541Sshin			   (struct sockaddr_in6 *)rt_key(rt), rt);
80453541Sshin		ln->ln_hold = 0;
80553541Sshin	}
80662587Sitojun
80762587Sitojun freeit:
80862587Sitojun	m_freem(m);
80978064Sume	return;
81078064Sume
81178064Sume bad:
81278064Sume	icmp6stat.icp6s_badna++;
81378064Sume	m_freem(m);
81453541Sshin}
81553541Sshin
81653541Sshin/*
81753541Sshin * Neighbor advertisement output handling.
81853541Sshin *
81953541Sshin * Based on RFC 2461
82053541Sshin *
82162587Sitojun * the following items are not implemented yet:
82262587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
82362587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
82453541Sshin */
82553541Sshinvoid
82662587Sitojunnd6_na_output(ifp, daddr6, taddr6, flags, tlladdr, sdl0)
82753541Sshin	struct ifnet *ifp;
82878064Sume	const struct in6_addr *daddr6, *taddr6;
82953541Sshin	u_long flags;
83062587Sitojun	int tlladdr;		/* 1 if include target link-layer address */
83162587Sitojun	struct sockaddr *sdl0;	/* sockaddr_dl (= proxy NA) or NULL */
83253541Sshin{
83353541Sshin	struct mbuf *m;
83453541Sshin	struct ip6_hdr *ip6;
83553541Sshin	struct nd_neighbor_advert *nd_na;
83653541Sshin	struct in6_ifaddr *ia = NULL;
83753541Sshin	struct ip6_moptions im6o;
83853541Sshin	int icmp6len;
83962587Sitojun	int maxlen;
84092733Speter	caddr_t mac = NULL;
84162587Sitojun	struct ifnet *outif = NULL;
84262587Sitojun
84362587Sitojun	/* estimate the size of message */
84462587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_na);
84562587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
84662587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
84762587Sitojun#ifdef DIAGNOSTIC
84862587Sitojun		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
84962587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
85062587Sitojun#endif
85153541Sshin		return;
85262587Sitojun	}
85353541Sshin
85462587Sitojun	MGETHDR(m, M_DONTWAIT, MT_DATA);
85562587Sitojun	if (m && max_linkhdr + maxlen >= MHLEN) {
85662587Sitojun		MCLGET(m, M_DONTWAIT);
85762587Sitojun		if ((m->m_flags & M_EXT) == 0) {
85862587Sitojun			m_free(m);
85962587Sitojun			m = NULL;
86062587Sitojun		}
86162587Sitojun	}
86262587Sitojun	if (m == NULL)
86362587Sitojun		return;
86478064Sume	m->m_pkthdr.rcvif = NULL;
86562587Sitojun
86653541Sshin	if (IN6_IS_ADDR_MULTICAST(daddr6)) {
86753541Sshin		m->m_flags |= M_MCAST;
86853541Sshin		im6o.im6o_multicast_ifp = ifp;
86953541Sshin		im6o.im6o_multicast_hlim = 255;
87053541Sshin		im6o.im6o_multicast_loop = 0;
87153541Sshin	}
87253541Sshin
87353541Sshin	icmp6len = sizeof(*nd_na);
87453541Sshin	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
87595023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
87653541Sshin
87753541Sshin	/* fill neighbor advertisement packet */
87853541Sshin	ip6 = mtod(m, struct ip6_hdr *);
87953541Sshin	ip6->ip6_flow = 0;
88062587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
88162587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
88253541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
88353541Sshin	ip6->ip6_hlim = 255;
88453541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(daddr6)) {
88553541Sshin		/* reply to DAD */
88653541Sshin		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
88753541Sshin		ip6->ip6_dst.s6_addr16[1] = htons(ifp->if_index);
88853541Sshin		ip6->ip6_dst.s6_addr32[1] = 0;
88953541Sshin		ip6->ip6_dst.s6_addr32[2] = 0;
89053541Sshin		ip6->ip6_dst.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
89153541Sshin		flags &= ~ND_NA_FLAG_SOLICITED;
89253541Sshin	} else
89353541Sshin		ip6->ip6_dst = *daddr6;
89453541Sshin
89553541Sshin	/*
89653541Sshin	 * Select a source whose scope is the same as that of the dest.
89753541Sshin	 */
89853541Sshin	ia = in6_ifawithifp(ifp, &ip6->ip6_dst);
89953541Sshin	if (ia == NULL) {
90053541Sshin		m_freem(m);
90153541Sshin		return;
90253541Sshin	}
90353541Sshin	ip6->ip6_src = ia->ia_addr.sin6_addr;
90453541Sshin	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
90553541Sshin	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
90653541Sshin	nd_na->nd_na_code = 0;
90753541Sshin	nd_na->nd_na_target = *taddr6;
90853541Sshin	if (IN6_IS_SCOPE_LINKLOCAL(&nd_na->nd_na_target))
90953541Sshin		nd_na->nd_na_target.s6_addr16[1] = 0;
91053541Sshin
91153541Sshin	/*
91253541Sshin	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
91353541Sshin	 * see nd6_ns_input() for details.
91453541Sshin	 * Basically, if NS packet is sent to unicast/anycast addr,
91553541Sshin	 * target lladdr option SHOULD NOT be included.
91653541Sshin	 */
91762587Sitojun	if (tlladdr) {
91862587Sitojun		/*
91962587Sitojun		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
92062587Sitojun		 * lladdr in sdl0.  If we are not proxying (sending NA for
92162587Sitojun		 * my address) use lladdr configured for the interface.
92262587Sitojun		 */
92362587Sitojun		if (sdl0 == NULL)
92462587Sitojun			mac = nd6_ifptomac(ifp);
92562587Sitojun		else if (sdl0->sa_family == AF_LINK) {
92662587Sitojun			struct sockaddr_dl *sdl;
92762587Sitojun			sdl = (struct sockaddr_dl *)sdl0;
92862587Sitojun			if (sdl->sdl_alen == ifp->if_addrlen)
92962587Sitojun				mac = LLADDR(sdl);
93062587Sitojun		}
93162587Sitojun	}
93262587Sitojun	if (tlladdr && mac) {
93353541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
93453541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
93553541Sshin
93653541Sshin		/* roundup to 8 bytes alignment! */
93753541Sshin		optlen = (optlen + 7) & ~7;
93853541Sshin
93953541Sshin		m->m_pkthdr.len += optlen;
94053541Sshin		m->m_len += optlen;
94153541Sshin		icmp6len += optlen;
94253541Sshin		bzero((caddr_t)nd_opt, optlen);
94353541Sshin		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
94453541Sshin		nd_opt->nd_opt_len = optlen >> 3;
94553541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
94653541Sshin	} else
94753541Sshin		flags &= ~ND_NA_FLAG_OVERRIDE;
94853541Sshin
94953541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
95053541Sshin	nd_na->nd_na_flags_reserved = flags;
95153541Sshin	nd_na->nd_na_cksum = 0;
95253541Sshin	nd_na->nd_na_cksum =
95353541Sshin		in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
95453541Sshin
95553541Sshin#ifdef IPSEC
95662587Sitojun	/* Don't lookup socket */
95778064Sume	(void)ipsec_setsocket(m, NULL);
95862587Sitojun#endif
95953541Sshin	ip6_output(m, NULL, NULL, 0, &im6o, &outif);
96053541Sshin	if (outif) {
96153541Sshin		icmp6_ifstat_inc(outif, ifs6_out_msg);
96253541Sshin		icmp6_ifstat_inc(outif, ifs6_out_neighboradvert);
96353541Sshin	}
96453541Sshin	icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
96553541Sshin}
96653541Sshin
96753541Sshincaddr_t
96853541Sshinnd6_ifptomac(ifp)
96953541Sshin	struct ifnet *ifp;
97053541Sshin{
97153541Sshin	switch (ifp->if_type) {
97253541Sshin	case IFT_ARCNET:
97353541Sshin	case IFT_ETHER:
97453541Sshin	case IFT_FDDI:
97578064Sume	case IFT_IEEE1394:
97678468Ssumikawa#ifdef IFT_L2VLAN
97778468Ssumikawa	case IFT_L2VLAN:
97878468Ssumikawa#endif
97978064Sume#ifdef IFT_IEEE80211
98078064Sume	case IFT_IEEE80211:
98178064Sume#endif
98253541Sshin		return ((caddr_t)(ifp + 1));
98353541Sshin		break;
98453541Sshin	default:
98553541Sshin		return NULL;
98653541Sshin	}
98753541Sshin}
98853541Sshin
98960938SjakeTAILQ_HEAD(dadq_head, dadq);
99053541Sshinstruct dadq {
99160938Sjake	TAILQ_ENTRY(dadq) dad_list;
99253541Sshin	struct ifaddr *dad_ifa;
99353541Sshin	int dad_count;		/* max NS to send */
99462587Sitojun	int dad_ns_tcount;	/* # of trials to send NS */
99553541Sshin	int dad_ns_ocount;	/* NS sent so far */
99653541Sshin	int dad_ns_icount;
99753541Sshin	int dad_na_icount;
99878064Sume	struct callout dad_timer_ch;
99953541Sshin};
100053541Sshin
100153541Sshinstatic struct dadq_head dadq;
100278064Sumestatic int dad_init = 0;
100353541Sshin
100453541Sshinstatic struct dadq *
100553541Sshinnd6_dad_find(ifa)
100653541Sshin	struct ifaddr *ifa;
100753541Sshin{
100853541Sshin	struct dadq *dp;
100953541Sshin
101062587Sitojun	for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
101153541Sshin		if (dp->dad_ifa == ifa)
101253541Sshin			return dp;
101353541Sshin	}
101453541Sshin	return NULL;
101553541Sshin}
101653541Sshin
101778064Sumestatic void
101878064Sumend6_dad_starttimer(dp, ticks)
101978064Sume	struct dadq *dp;
102078064Sume	int ticks;
102178064Sume{
102278064Sume
102378064Sume	callout_reset(&dp->dad_timer_ch, ticks,
102478064Sume	    (void (*) __P((void *)))nd6_dad_timer, (void *)dp->dad_ifa);
102578064Sume}
102678064Sume
102778064Sumestatic void
102878064Sumend6_dad_stoptimer(dp)
102978064Sume	struct dadq *dp;
103078064Sume{
103178064Sume
103278064Sume	callout_stop(&dp->dad_timer_ch);
103378064Sume}
103478064Sume
103553541Sshin/*
103653541Sshin * Start Duplicated Address Detection (DAD) for specified interface address.
103753541Sshin */
103853541Sshinvoid
103953541Sshinnd6_dad_start(ifa, tick)
104053541Sshin	struct ifaddr *ifa;
104153541Sshin	int *tick;	/* minimum delay ticks for IFF_UP event */
104253541Sshin{
104353541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
104453541Sshin	struct dadq *dp;
104553541Sshin
104653541Sshin	if (!dad_init) {
104753541Sshin		TAILQ_INIT(&dadq);
104853541Sshin		dad_init++;
104953541Sshin	}
105053541Sshin
105153541Sshin	/*
105253541Sshin	 * If we don't need DAD, don't do it.
105353541Sshin	 * There are several cases:
105453541Sshin	 * - DAD is disabled (ip6_dad_count == 0)
105553541Sshin	 * - the interface address is anycast
105653541Sshin	 */
105753541Sshin	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
105862587Sitojun		log(LOG_DEBUG,
105962587Sitojun			"nd6_dad_start: called with non-tentative address "
106053541Sshin			"%s(%s)\n",
106153541Sshin			ip6_sprintf(&ia->ia_addr.sin6_addr),
106253541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
106353541Sshin		return;
106453541Sshin	}
106553541Sshin	if (ia->ia6_flags & IN6_IFF_ANYCAST) {
106653541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
106753541Sshin		return;
106853541Sshin	}
106953541Sshin	if (!ip6_dad_count) {
107053541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
107153541Sshin		return;
107253541Sshin	}
107353541Sshin	if (!ifa->ifa_ifp)
107453541Sshin		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
107553541Sshin	if (!(ifa->ifa_ifp->if_flags & IFF_UP))
107653541Sshin		return;
107753541Sshin	if (nd6_dad_find(ifa) != NULL) {
107853541Sshin		/* DAD already in progress */
107953541Sshin		return;
108053541Sshin	}
108153541Sshin
108253541Sshin	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
108353541Sshin	if (dp == NULL) {
108462587Sitojun		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
108553541Sshin			"%s(%s)\n",
108653541Sshin			ip6_sprintf(&ia->ia_addr.sin6_addr),
108753541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
108853541Sshin		return;
108953541Sshin	}
109053541Sshin	bzero(dp, sizeof(*dp));
109178064Sume	callout_init(&dp->dad_timer_ch, 0);
109253541Sshin	TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
109353541Sshin
109478064Sume	nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
109578064Sume	    ip6_sprintf(&ia->ia_addr.sin6_addr)));
109653541Sshin
109753541Sshin	/*
109853541Sshin	 * Send NS packet for DAD, ip6_dad_count times.
109953541Sshin	 * Note that we must delay the first transmission, if this is the
110053541Sshin	 * first packet to be sent from the interface after interface
110153541Sshin	 * (re)initialization.
110253541Sshin	 */
110353541Sshin	dp->dad_ifa = ifa;
110495023Ssuz	IFAREF(ifa);	/* just for safety */
110553541Sshin	dp->dad_count = ip6_dad_count;
110653541Sshin	dp->dad_ns_icount = dp->dad_na_icount = 0;
110762587Sitojun	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
110895023Ssuz	if (tick == NULL) {
110962587Sitojun		nd6_dad_ns_output(dp, ifa);
111078064Sume		nd6_dad_starttimer(dp,
111178064Sume		    nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000);
111253541Sshin	} else {
111353541Sshin		int ntick;
111453541Sshin
111553541Sshin		if (*tick == 0)
111653541Sshin			ntick = random() % (MAX_RTR_SOLICITATION_DELAY * hz);
111753541Sshin		else
111853541Sshin			ntick = *tick + random() % (hz / 2);
111953541Sshin		*tick = ntick;
112078064Sume		nd6_dad_starttimer(dp, ntick);
112153541Sshin	}
112253541Sshin}
112353541Sshin
112478064Sume/*
112578064Sume * terminate DAD unconditionally.  used for address removals.
112678064Sume */
112778064Sumevoid
112878064Sumend6_dad_stop(ifa)
112978064Sume	struct ifaddr *ifa;
113078064Sume{
113178064Sume	struct dadq *dp;
113278064Sume
113378064Sume	if (!dad_init)
113478064Sume		return;
113578064Sume	dp = nd6_dad_find(ifa);
113678064Sume	if (!dp) {
113778064Sume		/* DAD wasn't started yet */
113878064Sume		return;
113978064Sume	}
114078064Sume
114178064Sume	nd6_dad_stoptimer(dp);
114278064Sume
114378064Sume	TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
114478064Sume	free(dp, M_IP6NDP);
114578064Sume	dp = NULL;
114678064Sume	IFAFREE(ifa);
114778064Sume}
114878064Sume
114953541Sshinstatic void
115053541Sshinnd6_dad_timer(ifa)
115153541Sshin	struct ifaddr *ifa;
115253541Sshin{
115353541Sshin	int s;
115453541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
115553541Sshin	struct dadq *dp;
115653541Sshin
115795023Ssuz	s = splnet();		/* XXX */
115853541Sshin
115953541Sshin	/* Sanity check */
116053541Sshin	if (ia == NULL) {
116162587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
116253541Sshin		goto done;
116353541Sshin	}
116453541Sshin	dp = nd6_dad_find(ifa);
116553541Sshin	if (dp == NULL) {
116662587Sitojun		log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
116753541Sshin		goto done;
116853541Sshin	}
116953541Sshin	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
117062587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
117153541Sshin			"%s(%s)\n",
117253541Sshin			ip6_sprintf(&ia->ia_addr.sin6_addr),
117353541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
117453541Sshin		goto done;
117553541Sshin	}
117653541Sshin	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
117762587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
117853541Sshin			"%s(%s)\n",
117953541Sshin			ip6_sprintf(&ia->ia_addr.sin6_addr),
118053541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
118153541Sshin		goto done;
118253541Sshin	}
118353541Sshin
118462587Sitojun	/* timeouted with IFF_{RUNNING,UP} check */
118562587Sitojun	if (dp->dad_ns_tcount > dad_maxtry) {
118678064Sume		nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
118778064Sume			if_name(ifa->ifa_ifp)));
118862587Sitojun
118962587Sitojun		TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
119062587Sitojun		free(dp, M_IP6NDP);
119162587Sitojun		dp = NULL;
119262587Sitojun		IFAFREE(ifa);
119362587Sitojun		goto done;
119462587Sitojun	}
119562587Sitojun
119653541Sshin	/* Need more checks? */
119753541Sshin	if (dp->dad_ns_ocount < dp->dad_count) {
119853541Sshin		/*
119953541Sshin		 * We have more NS to go.  Send NS packet for DAD.
120053541Sshin		 */
120162587Sitojun		nd6_dad_ns_output(dp, ifa);
120278064Sume		nd6_dad_starttimer(dp,
120378064Sume		    nd_ifinfo[ifa->ifa_ifp->if_index].retrans * hz / 1000);
120453541Sshin	} else {
120553541Sshin		/*
120653541Sshin		 * We have transmitted sufficient number of DAD packets.
120753541Sshin		 * See what we've got.
120853541Sshin		 */
120953541Sshin		int duplicate;
121053541Sshin
121153541Sshin		duplicate = 0;
121253541Sshin
121353541Sshin		if (dp->dad_na_icount) {
121453541Sshin			/*
121553541Sshin			 * the check is in nd6_dad_na_input(),
121653541Sshin			 * but just in case
121753541Sshin			 */
121853541Sshin			duplicate++;
121953541Sshin		}
122053541Sshin
122153541Sshin		if (dp->dad_ns_icount) {
122295023Ssuz#if 0 /* heuristics */
122362587Sitojun			/*
122462587Sitojun			 * if
122562587Sitojun			 * - we have sent many(?) DAD NS, and
122662587Sitojun			 * - the number of NS we sent equals to the
122762587Sitojun			 *   number of NS we've got, and
122862587Sitojun			 * - we've got no NA
122962587Sitojun			 * we may have a faulty network card/driver which
123062587Sitojun			 * loops back multicasts to myself.
123162587Sitojun			 */
123262587Sitojun			if (3 < dp->dad_count
123362587Sitojun			 && dp->dad_ns_icount == dp->dad_count
123462587Sitojun			 && dp->dad_na_icount == 0) {
123562587Sitojun				log(LOG_INFO, "DAD questionable for %s(%s): "
123662587Sitojun					"network card loops back multicast?\n",
123762587Sitojun					ip6_sprintf(&ia->ia_addr.sin6_addr),
123862587Sitojun					if_name(ifa->ifa_ifp));
123962587Sitojun				/* XXX consider it a duplicate or not? */
124062587Sitojun				/* duplicate++; */
124162587Sitojun			} else {
124262587Sitojun				/* We've seen NS, means DAD has failed. */
124362587Sitojun				duplicate++;
124462587Sitojun			}
124562587Sitojun#else
124653541Sshin			/* We've seen NS, means DAD has failed. */
124753541Sshin			duplicate++;
124862587Sitojun#endif
124953541Sshin		}
125053541Sshin
125153541Sshin		if (duplicate) {
125253541Sshin			/* (*dp) will be freed in nd6_dad_duplicated() */
125353541Sshin			dp = NULL;
125453541Sshin			nd6_dad_duplicated(ifa);
125553541Sshin		} else {
125653541Sshin			/*
125753541Sshin			 * We are done with DAD.  No NA came, no NS came.
125853541Sshin			 * duplicated address found.
125953541Sshin			 */
126053541Sshin			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
126153541Sshin
126278064Sume			nd6log((LOG_DEBUG,
126362587Sitojun			    "%s: DAD complete for %s - no duplicates found\n",
126462587Sitojun			    if_name(ifa->ifa_ifp),
126578064Sume			    ip6_sprintf(&ia->ia_addr.sin6_addr)));
126653541Sshin
126753541Sshin			TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
126853541Sshin			free(dp, M_IP6NDP);
126953541Sshin			dp = NULL;
127062587Sitojun			IFAFREE(ifa);
127153541Sshin		}
127253541Sshin	}
127353541Sshin
127453541Sshindone:
127553541Sshin	splx(s);
127653541Sshin}
127753541Sshin
127853541Sshinvoid
127953541Sshinnd6_dad_duplicated(ifa)
128053541Sshin	struct ifaddr *ifa;
128153541Sshin{
128253541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
128353541Sshin	struct dadq *dp;
128453541Sshin
128553541Sshin	dp = nd6_dad_find(ifa);
128653541Sshin	if (dp == NULL) {
128762587Sitojun		log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
128853541Sshin		return;
128953541Sshin	}
129053541Sshin
129178064Sume	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
129278064Sume	    "NS in/out=%d/%d, NA in=%d\n",
129378064Sume	    if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
129478064Sume	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
129553541Sshin
129653541Sshin	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
129753541Sshin	ia->ia6_flags |= IN6_IFF_DUPLICATED;
129853541Sshin
129953541Sshin	/* We are done with DAD, with duplicated address found. (failure) */
130078064Sume	nd6_dad_stoptimer(dp);
130153541Sshin
130262587Sitojun	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
130353541Sshin	    if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
130462587Sitojun	log(LOG_ERR, "%s: manual intervention required\n",
130562587Sitojun	    if_name(ifa->ifa_ifp));
130653541Sshin
130753541Sshin	TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
130853541Sshin	free(dp, M_IP6NDP);
130953541Sshin	dp = NULL;
131062587Sitojun	IFAFREE(ifa);
131153541Sshin}
131253541Sshin
131362587Sitojunstatic void
131462587Sitojunnd6_dad_ns_output(dp, ifa)
131562587Sitojun	struct dadq *dp;
131662587Sitojun	struct ifaddr *ifa;
131762587Sitojun{
131862587Sitojun	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
131962587Sitojun	struct ifnet *ifp = ifa->ifa_ifp;
132062587Sitojun
132162587Sitojun	dp->dad_ns_tcount++;
132262587Sitojun	if ((ifp->if_flags & IFF_UP) == 0) {
132362587Sitojun#if 0
132462587Sitojun		printf("%s: interface down?\n", if_name(ifp));
132562587Sitojun#endif
132662587Sitojun		return;
132762587Sitojun	}
132862587Sitojun	if ((ifp->if_flags & IFF_RUNNING) == 0) {
132962587Sitojun#if 0
133062587Sitojun		printf("%s: interface not running?\n", if_name(ifp));
133162587Sitojun#endif
133262587Sitojun		return;
133362587Sitojun	}
133462587Sitojun
133562587Sitojun	dp->dad_ns_ocount++;
133662587Sitojun	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
133762587Sitojun}
133862587Sitojun
133962587Sitojunstatic void
134053541Sshinnd6_dad_ns_input(ifa)
134153541Sshin	struct ifaddr *ifa;
134253541Sshin{
134353541Sshin	struct in6_ifaddr *ia;
134453541Sshin	struct ifnet *ifp;
134578064Sume	const struct in6_addr *taddr6;
134653541Sshin	struct dadq *dp;
134753541Sshin	int duplicate;
134853541Sshin
134953541Sshin	if (!ifa)
135053541Sshin		panic("ifa == NULL in nd6_dad_ns_input");
135153541Sshin
135253541Sshin	ia = (struct in6_ifaddr *)ifa;
135353541Sshin	ifp = ifa->ifa_ifp;
135453541Sshin	taddr6 = &ia->ia_addr.sin6_addr;
135553541Sshin	duplicate = 0;
135653541Sshin	dp = nd6_dad_find(ifa);
135753541Sshin
135853541Sshin	/* Quickhack - completely ignore DAD NS packets */
135953541Sshin	if (dad_ignore_ns) {
136078064Sume		nd6log((LOG_INFO,
136178064Sume		    "nd6_dad_ns_input: ignoring DAD NS packet for "
136253541Sshin		    "address %s(%s)\n", ip6_sprintf(taddr6),
136378064Sume		    if_name(ifa->ifa_ifp)));
136453541Sshin		return;
136553541Sshin	}
136653541Sshin
136753541Sshin	/*
136853541Sshin	 * if I'm yet to start DAD, someone else started using this address
136953541Sshin	 * first.  I have a duplicate and you win.
137053541Sshin	 */
137153541Sshin	if (!dp || dp->dad_ns_ocount == 0)
137253541Sshin		duplicate++;
137353541Sshin
137453541Sshin	/* XXX more checks for loopback situation - see nd6_dad_timer too */
137553541Sshin
137653541Sshin	if (duplicate) {
137753541Sshin		dp = NULL;	/* will be freed in nd6_dad_duplicated() */
137853541Sshin		nd6_dad_duplicated(ifa);
137953541Sshin	} else {
138053541Sshin		/*
138153541Sshin		 * not sure if I got a duplicate.
138253541Sshin		 * increment ns count and see what happens.
138353541Sshin		 */
138453541Sshin		if (dp)
138553541Sshin			dp->dad_ns_icount++;
138653541Sshin	}
138753541Sshin}
138853541Sshin
138962587Sitojunstatic void
139053541Sshinnd6_dad_na_input(ifa)
139153541Sshin	struct ifaddr *ifa;
139253541Sshin{
139353541Sshin	struct dadq *dp;
139453541Sshin
139553541Sshin	if (!ifa)
139653541Sshin		panic("ifa == NULL in nd6_dad_na_input");
139753541Sshin
139853541Sshin	dp = nd6_dad_find(ifa);
139953541Sshin	if (dp)
140053541Sshin		dp->dad_na_icount++;
140153541Sshin
140253541Sshin	/* remove the address. */
140353541Sshin	nd6_dad_duplicated(ifa);
140453541Sshin}
1405