nd6_nbr.c revision 165118
162587Sitojun/*	$FreeBSD: head/sys/netinet6/nd6_nbr.c 165118 2006-12-12 12:17:58Z bz $	*/
295023Ssuz/*	$KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $	*/
362587Sitojun
4139826Simp/*-
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"
35142215Sglebius#include "opt_ipsec.h"
36142215Sglebius#include "opt_carp.h"
3755009Sshin
3853541Sshin#include <sys/param.h>
3953541Sshin#include <sys/systm.h>
4053541Sshin#include <sys/malloc.h>
4153541Sshin#include <sys/mbuf.h>
4253541Sshin#include <sys/socket.h>
4353541Sshin#include <sys/sockio.h>
4453541Sshin#include <sys/time.h>
4553541Sshin#include <sys/kernel.h>
4653541Sshin#include <sys/errno.h>
4753541Sshin#include <sys/syslog.h>
4853541Sshin#include <sys/queue.h>
4978064Sume#include <sys/callout.h>
5053541Sshin
5153541Sshin#include <net/if.h>
5253541Sshin#include <net/if_types.h>
5353541Sshin#include <net/if_dl.h>
54147306Sbrooks#include <net/if_var.h>
5553541Sshin#include <net/route.h>
5653541Sshin
5753541Sshin#include <netinet/in.h>
5853541Sshin#include <netinet/in_var.h>
5953541Sshin#include <netinet6/in6_var.h>
60151477Ssuz#include <netinet6/in6_ifattach.h>
6162587Sitojun#include <netinet/ip6.h>
6253541Sshin#include <netinet6/ip6_var.h>
63148385Sume#include <netinet6/scope6_var.h>
6453541Sshin#include <netinet6/nd6.h>
6562587Sitojun#include <netinet/icmp6.h>
6653541Sshin
67142215Sglebius#ifdef DEV_CARP
68142215Sglebius#include <netinet/ip_carp.h>
69142215Sglebius#endif
70142215Sglebius
7162587Sitojun#define SDL(s) ((struct sockaddr_dl *)s)
7253541Sshin
7362587Sitojunstruct dadq;
7462587Sitojunstatic struct dadq *nd6_dad_find __P((struct ifaddr *));
7578064Sumestatic void nd6_dad_starttimer __P((struct dadq *, int));
7678064Sumestatic void nd6_dad_stoptimer __P((struct dadq *));
7762587Sitojunstatic void nd6_dad_timer __P((struct ifaddr *));
7862587Sitojunstatic void nd6_dad_ns_output __P((struct dadq *, struct ifaddr *));
7962587Sitojunstatic void nd6_dad_ns_input __P((struct ifaddr *));
8062587Sitojunstatic void nd6_dad_na_input __P((struct ifaddr *));
8153541Sshin
8262587Sitojunstatic int dad_ignore_ns = 0;	/* ignore NS in DAD - specwise incorrect*/
8362587Sitojunstatic int dad_maxtry = 15;	/* max # of *tries* to transmit DAD packet */
8453541Sshin
8553541Sshin/*
86108470Sschweikh * Input a Neighbor Solicitation Message.
8753541Sshin *
8853541Sshin * Based on RFC 2461
89148987Sume * Based on RFC 2462 (duplicate address detection)
9053541Sshin */
9153541Sshinvoid
9253541Sshinnd6_ns_input(m, off, icmp6len)
9353541Sshin	struct mbuf *m;
9453541Sshin	int off, icmp6len;
9553541Sshin{
9653541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
9753541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
9862587Sitojun	struct nd_neighbor_solicit *nd_ns;
9953541Sshin	struct in6_addr saddr6 = ip6->ip6_src;
10053541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
10162587Sitojun	struct in6_addr taddr6;
10253541Sshin	struct in6_addr myaddr6;
10353541Sshin	char *lladdr = NULL;
104142215Sglebius	struct ifaddr *ifa = NULL;
10553541Sshin	int lladdrlen = 0;
10653541Sshin	int anycast = 0, proxy = 0, tentative = 0;
10753541Sshin	int tlladdr;
10853541Sshin	union nd_opts ndopts;
10962587Sitojun	struct sockaddr_dl *proxydl = NULL;
110165118Sbz	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
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;
124148385Sume	if (in6_setscope(&taddr6, ifp, NULL) != 0)
125148385Sume		goto bad;
12678064Sume
12753541Sshin	if (ip6->ip6_hlim != 255) {
12878064Sume		nd6log((LOG_ERR,
12978064Sume		    "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
130165118Sbz		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
131165118Sbz		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
13278064Sume		goto bad;
13353541Sshin	}
13453541Sshin
13553541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
136148987Sume		/* dst has to be a solicited node multicast address. */
137120941Sume		if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
13895023Ssuz		    /* don't check ifindex portion */
139120941Sume		    daddr6.s6_addr32[1] == 0 &&
140120941Sume		    daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
141120941Sume		    daddr6.s6_addr8[12] == 0xff) {
14295023Ssuz			; /* good */
14353541Sshin		} else {
14478064Sume			nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
145120941Sume			    "(wrong ip6 dst)\n"));
14653541Sshin			goto bad;
14753541Sshin		}
14853541Sshin	}
14953541Sshin
15053541Sshin	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
15178064Sume		nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
15253541Sshin		goto bad;
15353541Sshin	}
15453541Sshin
15553541Sshin	icmp6len -= sizeof(*nd_ns);
15653541Sshin	nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
15753541Sshin	if (nd6_options(&ndopts) < 0) {
15878064Sume		nd6log((LOG_INFO,
15978064Sume		    "nd6_ns_input: invalid ND option, ignored\n"));
16078064Sume		/* nd6_options have incremented stats */
16178064Sume		goto freeit;
16253541Sshin	}
16353541Sshin
16453541Sshin	if (ndopts.nd_opts_src_lladdr) {
16595023Ssuz		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
16653541Sshin		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
16753541Sshin	}
168120941Sume
16953541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
17078064Sume		nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
17178064Sume		    "(link-layer address option)\n"));
17253541Sshin		goto bad;
17353541Sshin	}
17453541Sshin
17553541Sshin	/*
17653541Sshin	 * Attaching target link-layer address to the NA?
17753541Sshin	 * (RFC 2461 7.2.4)
17853541Sshin	 *
17953541Sshin	 * NS IP dst is unicast/anycast			MUST NOT add
18053541Sshin	 * NS IP dst is solicited-node multicast	MUST add
18153541Sshin	 *
18253541Sshin	 * In implementation, we add target link-layer address by default.
18353541Sshin	 * We do not add one in MUST NOT cases.
18453541Sshin	 */
18553541Sshin	if (!IN6_IS_ADDR_MULTICAST(&daddr6))
18653541Sshin		tlladdr = 0;
18753541Sshin	else
18853541Sshin		tlladdr = 1;
18953541Sshin
19053541Sshin	/*
19153541Sshin	 * Target address (taddr6) must be either:
19253541Sshin	 * (1) Valid unicast/anycast address for my receiving interface,
19353541Sshin	 * (2) Unicast address for which I'm offering proxy service, or
19453541Sshin	 * (3) "tentative" address on which DAD is being performed.
19553541Sshin	 */
19653541Sshin	/* (1) and (3) check. */
197142215Sglebius#ifdef DEV_CARP
198142215Sglebius	if (ifp->if_carp)
199142215Sglebius		ifa = carp_iamatch6(ifp->if_carp, &taddr6);
200151465Ssuz	if (ifa == NULL)
201142215Sglebius		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
202142215Sglebius#else
20353541Sshin	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
204142215Sglebius#endif
20553541Sshin
20653541Sshin	/* (2) check. */
207151465Ssuz	if (ifa == NULL) {
20853541Sshin		struct rtentry *rt;
20953541Sshin		struct sockaddr_in6 tsin6;
210121765Ssam		int need_proxy;
21153541Sshin
21253541Sshin		bzero(&tsin6, sizeof tsin6);
21353541Sshin		tsin6.sin6_len = sizeof(struct sockaddr_in6);
21453541Sshin		tsin6.sin6_family = AF_INET6;
21553541Sshin		tsin6.sin6_addr = taddr6;
21653541Sshin
21753541Sshin		rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
218121765Ssam		need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
219121765Ssam		    rt->rt_gateway->sa_family == AF_LINK);
220121765Ssam		if (rt)
221121765Ssam			rtfree(rt);
222121765Ssam		if (need_proxy) {
22353541Sshin			/*
22462587Sitojun			 * proxy NDP for single entry
22553541Sshin			 */
22662587Sitojun			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
22762587Sitojun				IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
22862587Sitojun			if (ifa) {
22953541Sshin				proxy = 1;
23062587Sitojun				proxydl = SDL(rt->rt_gateway);
23162587Sitojun			}
23253541Sshin		}
23353541Sshin	}
234151479Ssuz	if (ifa == NULL) {
23553541Sshin		/*
23678064Sume		 * We've got an NS packet, and we don't have that adddress
23753541Sshin		 * assigned for us.  We MUST silently ignore it.
23853541Sshin		 * See RFC2461 7.2.3.
23953541Sshin		 */
24062587Sitojun		goto freeit;
24153541Sshin	}
24253541Sshin	myaddr6 = *IFA_IN6(ifa);
24353541Sshin	anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
24453541Sshin	tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
24553541Sshin	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
24662587Sitojun		goto freeit;
24753541Sshin
24853541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
249120941Sume		nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
25053541Sshin		    "(if %d, NS packet %d)\n",
251165118Sbz		    ip6_sprintf(ip6bufs, &taddr6),
252120941Sume		    ifp->if_addrlen, lladdrlen - 2));
25378064Sume		goto bad;
25453541Sshin	}
25553541Sshin
25653541Sshin	if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
257120941Sume		nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
258165118Sbz		    ip6_sprintf(ip6bufs, &saddr6)));
25962587Sitojun		goto freeit;
26053541Sshin	}
26153541Sshin
26253541Sshin	/*
26353541Sshin	 * We have neighbor solicitation packet, with target address equals to
26453541Sshin	 * one of my tentative address.
26553541Sshin	 *
26653541Sshin	 * src addr	how to process?
26753541Sshin	 * ---		---
26853541Sshin	 * multicast	of course, invalid (rejected in ip6_input)
26953541Sshin	 * unicast	somebody is doing address resolution -> ignore
27053541Sshin	 * unspec	dup address detection
27153541Sshin	 *
27253541Sshin	 * The processing is defined in RFC 2462.
27353541Sshin	 */
27453541Sshin	if (tentative) {
27553541Sshin		/*
27653541Sshin		 * If source address is unspecified address, it is for
277148987Sume		 * duplicate address detection.
27853541Sshin		 *
27953541Sshin		 * If not, the packet is for addess resolution;
28053541Sshin		 * silently ignore it.
28153541Sshin		 */
28253541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
28353541Sshin			nd6_dad_ns_input(ifa);
28453541Sshin
28562587Sitojun		goto freeit;
28653541Sshin	}
28753541Sshin
28853541Sshin	/*
28953541Sshin	 * If the source address is unspecified address, entries must not
29053541Sshin	 * be created or updated.
29153541Sshin	 * It looks that sender is performing DAD.  Output NA toward
29253541Sshin	 * all-node multicast address, to tell the sender that I'm using
29353541Sshin	 * the address.
29453541Sshin	 * S bit ("solicited") must be zero.
29553541Sshin	 */
29653541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
297148385Sume		struct in6_addr in6_all;
298148385Sume
299148385Sume		in6_all = in6addr_linklocal_allnodes;
300148385Sume		if (in6_setscope(&in6_all, ifp, NULL) != 0)
301148385Sume			goto bad;
302148385Sume		nd6_na_output(ifp, &in6_all, &taddr6,
303120941Sume		    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
304120941Sume		    (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
305120941Sume		    tlladdr, (struct sockaddr *)proxydl);
30662587Sitojun		goto freeit;
30753541Sshin	}
30853541Sshin
309120941Sume	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
310120941Sume	    ND_NEIGHBOR_SOLICIT, 0);
31153541Sshin
31253541Sshin	nd6_na_output(ifp, &saddr6, &taddr6,
313120941Sume	    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
314120941Sume	    (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
315120941Sume	    tlladdr, (struct sockaddr *)proxydl);
31662587Sitojun freeit:
31762587Sitojun	m_freem(m);
31853541Sshin	return;
31953541Sshin
32053541Sshin bad:
321165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
322165118Sbz		ip6_sprintf(ip6bufs, &saddr6)));
323165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
324165118Sbz		ip6_sprintf(ip6bufs, &daddr6)));
325165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
326165118Sbz		ip6_sprintf(ip6bufs, &taddr6)));
32778064Sume	icmp6stat.icp6s_badns++;
32862587Sitojun	m_freem(m);
32953541Sshin}
33053541Sshin
33153541Sshin/*
332108470Sschweikh * Output a Neighbor Solicitation Message. Caller specifies:
33353541Sshin *	- ICMP6 header source IP6 address
33453541Sshin *	- ND6 header target IP6 address
33553541Sshin *	- ND6 header source datalink address
33653541Sshin *
33753541Sshin * Based on RFC 2461
338148987Sume * Based on RFC 2462 (duplicate address detection)
33953541Sshin */
34053541Sshinvoid
34153541Sshinnd6_ns_output(ifp, daddr6, taddr6, ln, dad)
34253541Sshin	struct ifnet *ifp;
34378064Sume	const struct in6_addr *daddr6, *taddr6;
34453541Sshin	struct llinfo_nd6 *ln;	/* for source address determination */
345148987Sume	int dad;	/* duplicate address detection */
34653541Sshin{
34753541Sshin	struct mbuf *m;
34853541Sshin	struct ip6_hdr *ip6;
34953541Sshin	struct nd_neighbor_solicit *nd_ns;
350148385Sume	struct in6_addr *src, src_in;
35153541Sshin	struct ip6_moptions im6o;
35253541Sshin	int icmp6len;
35362587Sitojun	int maxlen;
35453541Sshin	caddr_t mac;
355148385Sume	struct route_in6 ro;
356120941Sume
357148385Sume	bzero(&ro, sizeof(ro));
358148385Sume
35953541Sshin	if (IN6_IS_ADDR_MULTICAST(taddr6))
36053541Sshin		return;
36153541Sshin
36262587Sitojun	/* estimate the size of message */
36362587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
36462587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
36562587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
36662587Sitojun#ifdef DIAGNOSTIC
36762587Sitojun		printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
36862587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
36962587Sitojun#endif
37053541Sshin		return;
37162587Sitojun	}
37253541Sshin
373111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
37462587Sitojun	if (m && max_linkhdr + maxlen >= MHLEN) {
375111119Simp		MCLGET(m, M_DONTWAIT);
37662587Sitojun		if ((m->m_flags & M_EXT) == 0) {
37762587Sitojun			m_free(m);
37862587Sitojun			m = NULL;
37962587Sitojun		}
38062587Sitojun	}
38162587Sitojun	if (m == NULL)
38262587Sitojun		return;
38378064Sume	m->m_pkthdr.rcvif = NULL;
38462587Sitojun
38553541Sshin	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
38653541Sshin		m->m_flags |= M_MCAST;
38753541Sshin		im6o.im6o_multicast_ifp = ifp;
38853541Sshin		im6o.im6o_multicast_hlim = 255;
38953541Sshin		im6o.im6o_multicast_loop = 0;
39053541Sshin	}
39153541Sshin
39253541Sshin	icmp6len = sizeof(*nd_ns);
39353541Sshin	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
39495023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
39553541Sshin
39653541Sshin	/* fill neighbor solicitation packet */
39753541Sshin	ip6 = mtod(m, struct ip6_hdr *);
39853541Sshin	ip6->ip6_flow = 0;
39962587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
40062587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
40153541Sshin	/* ip6->ip6_plen will be set later */
40253541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
40353541Sshin	ip6->ip6_hlim = 255;
40453541Sshin	if (daddr6)
40553541Sshin		ip6->ip6_dst = *daddr6;
40653541Sshin	else {
40753541Sshin		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
408148385Sume		ip6->ip6_dst.s6_addr16[1] = 0;
40953541Sshin		ip6->ip6_dst.s6_addr32[1] = 0;
41053541Sshin		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
41153541Sshin		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
41253541Sshin		ip6->ip6_dst.s6_addr8[12] = 0xff;
413148385Sume		if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
414148385Sume			goto bad;
41553541Sshin	}
41653541Sshin	if (!dad) {
41753541Sshin		/*
41853541Sshin		 * RFC2461 7.2.2:
41953541Sshin		 * "If the source address of the packet prompting the
42053541Sshin		 * solicitation is the same as one of the addresses assigned
42153541Sshin		 * to the outgoing interface, that address SHOULD be placed
42253541Sshin		 * in the IP Source Address of the outgoing solicitation.
42353541Sshin		 * Otherwise, any one of the addresses assigned to the
42453541Sshin		 * interface should be used."
42553541Sshin		 *
42653541Sshin		 * We use the source address for the prompting packet
42753541Sshin		 * (saddr6), if:
42853541Sshin		 * - saddr6 is given from the caller (by giving "ln"), and
42953541Sshin		 * - saddr6 belongs to the outgoing interface.
430148385Sume		 * Otherwise, we perform the source address selection as usual.
43153541Sshin		 */
43295023Ssuz		struct ip6_hdr *hip6;		/* hold ip6 */
433148385Sume		struct in6_addr *hsrc = NULL;
43453541Sshin
43553541Sshin		if (ln && ln->ln_hold) {
436148987Sume			/*
437148987Sume			 * assuming every packet in ln_hold has the same IP
438148987Sume			 * header
439148987Sume			 */
44053541Sshin			hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
44153541Sshin			/* XXX pullup? */
44253541Sshin			if (sizeof(*hip6) < ln->ln_hold->m_len)
443148385Sume				hsrc = &hip6->ip6_src;
44453541Sshin			else
445148385Sume				hsrc = NULL;
446148385Sume		}
447148385Sume		if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc))
448148385Sume			src = hsrc;
44953541Sshin		else {
450148385Sume			int error;
451148385Sume			struct sockaddr_in6 dst_sa;
452148385Sume
453148385Sume			bzero(&dst_sa, sizeof(dst_sa));
454148385Sume			dst_sa.sin6_family = AF_INET6;
455148385Sume			dst_sa.sin6_len = sizeof(dst_sa);
456148385Sume			dst_sa.sin6_addr = ip6->ip6_dst;
457148385Sume
458148385Sume			src = in6_selectsrc(&dst_sa, NULL,
459148385Sume			    NULL, &ro, NULL, NULL, &error);
460148385Sume			if (src == NULL) {
461165118Sbz				char ip6buf[INET6_ADDRSTRLEN];
462148385Sume				nd6log((LOG_DEBUG,
463148385Sume				    "nd6_ns_output: source can't be "
464148385Sume				    "determined: dst=%s, error=%d\n",
465165118Sbz				    ip6_sprintf(ip6buf, &dst_sa.sin6_addr),
466165118Sbz				    error));
467148385Sume				goto bad;
46853541Sshin			}
46953541Sshin		}
47053541Sshin	} else {
47153541Sshin		/*
47253541Sshin		 * Source address for DAD packet must always be IPv6
47353541Sshin		 * unspecified address. (0::0)
474148385Sume		 * We actually don't have to 0-clear the address (we did it
475148385Sume		 * above), but we do so here explicitly to make the intention
476148385Sume		 * clearer.
47753541Sshin		 */
478148385Sume		bzero(&src_in, sizeof(src_in));
479148385Sume		src = &src_in;
48053541Sshin	}
481148385Sume	ip6->ip6_src = *src;
48253541Sshin	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
48353541Sshin	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
48453541Sshin	nd_ns->nd_ns_code = 0;
48553541Sshin	nd_ns->nd_ns_reserved = 0;
48653541Sshin	nd_ns->nd_ns_target = *taddr6;
487121315Sume	in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
48853541Sshin
48953541Sshin	/*
49053541Sshin	 * Add source link-layer address option.
49153541Sshin	 *
49253541Sshin	 *				spec		implementation
49353541Sshin	 *				---		---
49453541Sshin	 * DAD packet			MUST NOT	do not add the option
49553541Sshin	 * there's no link layer address:
49653541Sshin	 *				impossible	do not add the option
49753541Sshin	 * there's link layer address:
49853541Sshin	 *	Multicast NS		MUST add one	add the option
49953541Sshin	 *	Unicast NS		SHOULD add one	add the option
50053541Sshin	 */
50153541Sshin	if (!dad && (mac = nd6_ifptomac(ifp))) {
50253541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
50353541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
50453541Sshin		/* 8 byte alignments... */
50553541Sshin		optlen = (optlen + 7) & ~7;
506120941Sume
50753541Sshin		m->m_pkthdr.len += optlen;
50853541Sshin		m->m_len += optlen;
50953541Sshin		icmp6len += optlen;
51053541Sshin		bzero((caddr_t)nd_opt, optlen);
51153541Sshin		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
51253541Sshin		nd_opt->nd_opt_len = optlen >> 3;
51353541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
51453541Sshin	}
51553541Sshin
51653541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
51753541Sshin	nd_ns->nd_ns_cksum = 0;
518120941Sume	nd_ns->nd_ns_cksum =
519120941Sume	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
52053541Sshin
521151536Ssuz	ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
522148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_msg);
523148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
524148385Sume	icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
525148385Sume
526148385Sume	if (ro.ro_rt) {		/* we don't cache this route. */
527148385Sume		RTFREE(ro.ro_rt);
52853541Sshin	}
529148385Sume	return;
530148385Sume
531148385Sume  bad:
532148385Sume	if (ro.ro_rt) {
533148385Sume		RTFREE(ro.ro_rt);
534148385Sume	}
535148385Sume	m_freem(m);
536148385Sume	return;
53753541Sshin}
53853541Sshin
53953541Sshin/*
54053541Sshin * Neighbor advertisement input handling.
54153541Sshin *
54253541Sshin * Based on RFC 2461
543148987Sume * Based on RFC 2462 (duplicate address detection)
54462587Sitojun *
54562587Sitojun * the following items are not implemented yet:
54662587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
54762587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
54853541Sshin */
54953541Sshinvoid
55053541Sshinnd6_na_input(m, off, icmp6len)
55153541Sshin	struct mbuf *m;
55253541Sshin	int off, icmp6len;
55353541Sshin{
55453541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
55553541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
55662587Sitojun	struct nd_neighbor_advert *nd_na;
55753541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
55862587Sitojun	struct in6_addr taddr6;
55962587Sitojun	int flags;
56062587Sitojun	int is_router;
56162587Sitojun	int is_solicited;
56262587Sitojun	int is_override;
56353541Sshin	char *lladdr = NULL;
56453541Sshin	int lladdrlen = 0;
56553541Sshin	struct ifaddr *ifa;
56653541Sshin	struct llinfo_nd6 *ln;
56753541Sshin	struct rtentry *rt;
56853541Sshin	struct sockaddr_dl *sdl;
56953541Sshin	union nd_opts ndopts;
570165118Sbz	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
57153541Sshin
57253541Sshin	if (ip6->ip6_hlim != 255) {
57378064Sume		nd6log((LOG_ERR,
57478064Sume		    "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
575165118Sbz		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
576165118Sbz		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
57778064Sume		goto bad;
57862587Sitojun	}
57962587Sitojun
58062587Sitojun#ifndef PULLDOWN_TEST
58162587Sitojun	IP6_EXTHDR_CHECK(m, off, icmp6len,);
58262587Sitojun	nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
58362587Sitojun#else
58462587Sitojun	IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
58562587Sitojun	if (nd_na == NULL) {
58662587Sitojun		icmp6stat.icp6s_tooshort++;
58753541Sshin		return;
58853541Sshin	}
58962587Sitojun#endif
590148385Sume
59162587Sitojun	flags = nd_na->nd_na_flags_reserved;
59262587Sitojun	is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
59362587Sitojun	is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
59462587Sitojun	is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
59553541Sshin
596148385Sume	taddr6 = nd_na->nd_na_target;
597148385Sume	if (in6_setscope(&taddr6, ifp, NULL))
598150202Ssuz		goto bad;	/* XXX: impossible */
59953541Sshin
60053541Sshin	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
60178064Sume		nd6log((LOG_ERR,
60253541Sshin		    "nd6_na_input: invalid target address %s\n",
603165118Sbz		    ip6_sprintf(ip6bufs, &taddr6)));
60478064Sume		goto bad;
60553541Sshin	}
60653541Sshin	if (IN6_IS_ADDR_MULTICAST(&daddr6))
60753541Sshin		if (is_solicited) {
60878064Sume			nd6log((LOG_ERR,
60978064Sume			    "nd6_na_input: a solicited adv is multicasted\n"));
61078064Sume			goto bad;
61153541Sshin		}
61253541Sshin
61353541Sshin	icmp6len -= sizeof(*nd_na);
61453541Sshin	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
61553541Sshin	if (nd6_options(&ndopts) < 0) {
61678064Sume		nd6log((LOG_INFO,
61778064Sume		    "nd6_na_input: invalid ND option, ignored\n"));
61878064Sume		/* nd6_options have incremented stats */
61962587Sitojun		goto freeit;
62053541Sshin	}
62153541Sshin
62253541Sshin	if (ndopts.nd_opts_tgt_lladdr) {
62353541Sshin		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
62453541Sshin		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
62553541Sshin	}
62653541Sshin
62753541Sshin	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
62853541Sshin
62953541Sshin	/*
63053541Sshin	 * Target address matches one of my interface address.
63153541Sshin	 *
63253541Sshin	 * If my address is tentative, this means that there's somebody
63353541Sshin	 * already using the same address as mine.  This indicates DAD failure.
63453541Sshin	 * This is defined in RFC 2462.
63553541Sshin	 *
63653541Sshin	 * Otherwise, process as defined in RFC 2461.
63753541Sshin	 */
63853541Sshin	if (ifa
63953541Sshin	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
64053541Sshin		nd6_dad_na_input(ifa);
64162587Sitojun		goto freeit;
64253541Sshin	}
64353541Sshin
64495023Ssuz	/* Just for safety, maybe unnecessary. */
64553541Sshin	if (ifa) {
64653541Sshin		log(LOG_ERR,
64753541Sshin		    "nd6_na_input: duplicate IP6 address %s\n",
648165118Sbz		    ip6_sprintf(ip6bufs, &taddr6));
64962587Sitojun		goto freeit;
65053541Sshin	}
65153541Sshin
65253541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
653120941Sume		nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
654165118Sbz		    "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
655120941Sume		    ifp->if_addrlen, lladdrlen - 2));
65678064Sume		goto bad;
65753541Sshin	}
65853541Sshin
65953541Sshin	/*
660120941Sume	 * If no neighbor cache entry is found, NA SHOULD silently be
661120941Sume	 * discarded.
66253541Sshin	 */
66353541Sshin	rt = nd6_lookup(&taddr6, 0, ifp);
66453541Sshin	if ((rt == NULL) ||
66553541Sshin	   ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
66653541Sshin	   ((sdl = SDL(rt->rt_gateway)) == NULL))
66762587Sitojun		goto freeit;
66853541Sshin
66953541Sshin	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
67053541Sshin		/*
67153541Sshin		 * If the link-layer has address, and no lladdr option came,
67253541Sshin		 * discard the packet.
67353541Sshin		 */
674151465Ssuz		if (ifp->if_addrlen && lladdr == NULL)
67562587Sitojun			goto freeit;
67653541Sshin
67753541Sshin		/*
67853541Sshin		 * Record link-layer address, and update the state.
67953541Sshin		 */
68053541Sshin		sdl->sdl_alen = ifp->if_addrlen;
68153541Sshin		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
68253541Sshin		if (is_solicited) {
68353541Sshin			ln->ln_state = ND6_LLINFO_REACHABLE;
68462587Sitojun			ln->ln_byhint = 0;
685151539Ssuz			if (!ND6_LLINFO_PERMANENT(ln)) {
686151539Ssuz				nd6_llinfo_settimer(ln,
687151539Ssuz				    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
688120941Sume			}
68978064Sume		} else {
69053541Sshin			ln->ln_state = ND6_LLINFO_STALE;
691151539Ssuz			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
69278064Sume		}
69378064Sume		if ((ln->ln_router = is_router) != 0) {
69478064Sume			/*
69578064Sume			 * This means a router's state has changed from
69678064Sume			 * non-reachable to probably reachable, and might
69778064Sume			 * affect the status of associated prefixes..
69878064Sume			 */
69978064Sume			pfxlist_onlink_check();
70078064Sume		}
70153541Sshin	} else {
70253541Sshin		int llchange;
70353541Sshin
70453541Sshin		/*
70553541Sshin		 * Check if the link-layer address has changed or not.
70653541Sshin		 */
707151465Ssuz		if (lladdr == NULL)
70853541Sshin			llchange = 0;
70953541Sshin		else {
71053541Sshin			if (sdl->sdl_alen) {
71153541Sshin				if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
71253541Sshin					llchange = 1;
71353541Sshin				else
71453541Sshin					llchange = 0;
71553541Sshin			} else
71653541Sshin				llchange = 1;
71753541Sshin		}
71853541Sshin
71953541Sshin		/*
72053541Sshin		 * This is VERY complex.  Look at it with care.
72153541Sshin		 *
72253541Sshin		 * override solicit lladdr llchange	action
72353541Sshin		 *					(L: record lladdr)
72453541Sshin		 *
72553541Sshin		 *	0	0	n	--	(2c)
72653541Sshin		 *	0	0	y	n	(2b) L
72753541Sshin		 *	0	0	y	y	(1)    REACHABLE->STALE
72853541Sshin		 *	0	1	n	--	(2c)   *->REACHABLE
72953541Sshin		 *	0	1	y	n	(2b) L *->REACHABLE
73053541Sshin		 *	0	1	y	y	(1)    REACHABLE->STALE
73153541Sshin		 *	1	0	n	--	(2a)
73253541Sshin		 *	1	0	y	n	(2a) L
73353541Sshin		 *	1	0	y	y	(2a) L *->STALE
73453541Sshin		 *	1	1	n	--	(2a)   *->REACHABLE
73553541Sshin		 *	1	1	y	n	(2a) L *->REACHABLE
73653541Sshin		 *	1	1	y	y	(2a) L *->REACHABLE
73753541Sshin		 */
738151539Ssuz		if (!is_override && (lladdr != NULL && llchange)) {  /* (1) */
73953541Sshin			/*
74053541Sshin			 * If state is REACHABLE, make it STALE.
74153541Sshin			 * no other updates should be done.
74253541Sshin			 */
74378064Sume			if (ln->ln_state == ND6_LLINFO_REACHABLE) {
74453541Sshin				ln->ln_state = ND6_LLINFO_STALE;
745151539Ssuz				nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
74678064Sume			}
74762587Sitojun			goto freeit;
74853541Sshin		} else if (is_override				   /* (2a) */
749151465Ssuz			|| (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
750151465Ssuz			|| lladdr == NULL) {			   /* (2c) */
75153541Sshin			/*
75253541Sshin			 * Update link-local address, if any.
75353541Sshin			 */
754151465Ssuz			if (lladdr != NULL) {
75553541Sshin				sdl->sdl_alen = ifp->if_addrlen;
75653541Sshin				bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
75753541Sshin			}
75853541Sshin
75953541Sshin			/*
76053541Sshin			 * If solicited, make the state REACHABLE.
76153541Sshin			 * If not solicited and the link-layer address was
76253541Sshin			 * changed, make it STALE.
76353541Sshin			 */
76453541Sshin			if (is_solicited) {
76553541Sshin				ln->ln_state = ND6_LLINFO_REACHABLE;
76662587Sitojun				ln->ln_byhint = 0;
767151539Ssuz				if (!ND6_LLINFO_PERMANENT(ln)) {
768151539Ssuz					nd6_llinfo_settimer(ln,
769151539Ssuz					    (long)ND_IFINFO(ifp)->reachable * hz);
77053541Sshin				}
77153541Sshin			} else {
772151465Ssuz				if (lladdr != NULL && llchange) {
77353541Sshin					ln->ln_state = ND6_LLINFO_STALE;
774151539Ssuz					nd6_llinfo_settimer(ln,
775151539Ssuz					    (long)nd6_gctimer * hz);
77678064Sume				}
77753541Sshin			}
77853541Sshin		}
77953541Sshin
78053541Sshin		if (ln->ln_router && !is_router) {
78153541Sshin			/*
78253541Sshin			 * The peer dropped the router flag.
78353541Sshin			 * Remove the sender from the Default Router List and
78453541Sshin			 * update the Destination Cache entries.
78553541Sshin			 */
78653541Sshin			struct nd_defrouter *dr;
78753541Sshin			struct in6_addr *in6;
78853541Sshin			int s;
78953541Sshin
79053541Sshin			in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
79195023Ssuz
79295023Ssuz			/*
79395023Ssuz			 * Lock to protect the default router list.
79495023Ssuz			 * XXX: this might be unnecessary, since this function
79595023Ssuz			 * is only called under the network software interrupt
796120941Sume			 * context.  However, we keep it just for safety.
79795023Ssuz			 */
79853541Sshin			s = splnet();
799128422Sluigi			dr = defrouter_lookup(in6, ifp);
80053541Sshin			if (dr)
80153541Sshin				defrtrlist_del(dr);
802151539Ssuz			else if (!ip6_forwarding) {
80353541Sshin				/*
80453541Sshin				 * Even if the neighbor is not in the default
80553541Sshin				 * router list, the neighbor may be used
80653541Sshin				 * as a next hop for some destinations
80753541Sshin				 * (e.g. redirect case). So we must
80853541Sshin				 * call rt6_flush explicitly.
80953541Sshin				 */
810128422Sluigi				rt6_flush(&ip6->ip6_src, ifp);
81153541Sshin			}
81253541Sshin			splx(s);
81353541Sshin		}
81453541Sshin		ln->ln_router = is_router;
81553541Sshin	}
81653541Sshin	rt->rt_flags &= ~RTF_REJECT;
81753541Sshin	ln->ln_asked = 0;
81853541Sshin	if (ln->ln_hold) {
819151539Ssuz		struct mbuf *m_hold, *m_hold_next;
820151539Ssuz
821151539Ssuz		for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
822151539Ssuz			struct mbuf *mpkt = NULL;
823151539Ssuz
824151539Ssuz			m_hold_next = m_hold->m_nextpkt;
825151539Ssuz			mpkt = m_copym(m_hold, 0, M_COPYALL, M_DONTWAIT);
826151539Ssuz			if (mpkt == NULL) {
827151539Ssuz				m_freem(m_hold);
828151539Ssuz				break;
829151539Ssuz			}
830151539Ssuz			mpkt->m_nextpkt = NULL;
831151539Ssuz			/*
832151539Ssuz			 * we assume ifp is not a loopback here, so just set
833151539Ssuz			 * the 2nd argument as the 1st one.
834151539Ssuz			 */
835151539Ssuz			nd6_output(ifp, ifp, mpkt,
836151539Ssuz			    (struct sockaddr_in6 *)rt_key(rt), rt);
837151539Ssuz		}
838120941Sume		ln->ln_hold = NULL;
83953541Sshin	}
84062587Sitojun
84162587Sitojun freeit:
84262587Sitojun	m_freem(m);
84378064Sume	return;
84478064Sume
84578064Sume bad:
84678064Sume	icmp6stat.icp6s_badna++;
84778064Sume	m_freem(m);
84853541Sshin}
84953541Sshin
85053541Sshin/*
85153541Sshin * Neighbor advertisement output handling.
85253541Sshin *
85353541Sshin * Based on RFC 2461
85453541Sshin *
85562587Sitojun * the following items are not implemented yet:
85662587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
85762587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
85853541Sshin */
85953541Sshinvoid
860148385Sumend6_na_output(ifp, daddr6_0, taddr6, flags, tlladdr, sdl0)
86153541Sshin	struct ifnet *ifp;
862148385Sume	const struct in6_addr *daddr6_0, *taddr6;
86353541Sshin	u_long flags;
86462587Sitojun	int tlladdr;		/* 1 if include target link-layer address */
86562587Sitojun	struct sockaddr *sdl0;	/* sockaddr_dl (= proxy NA) or NULL */
86653541Sshin{
86753541Sshin	struct mbuf *m;
86853541Sshin	struct ip6_hdr *ip6;
86953541Sshin	struct nd_neighbor_advert *nd_na;
87053541Sshin	struct ip6_moptions im6o;
871148385Sume	struct in6_addr *src, daddr6;
872148385Sume	struct sockaddr_in6 dst_sa;
873148385Sume	int icmp6len, maxlen, error;
87492733Speter	caddr_t mac = NULL;
875148385Sume	struct route_in6 ro;
87662587Sitojun
877148385Sume	bzero(&ro, sizeof(ro));
878148385Sume
879148385Sume	daddr6 = *daddr6_0;	/* make a local copy for modification */
880148385Sume
88162587Sitojun	/* estimate the size of message */
88262587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_na);
88362587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
88462587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
88562587Sitojun#ifdef DIAGNOSTIC
88662587Sitojun		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
88762587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
88862587Sitojun#endif
88953541Sshin		return;
89062587Sitojun	}
89153541Sshin
892111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
89362587Sitojun	if (m && max_linkhdr + maxlen >= MHLEN) {
894111119Simp		MCLGET(m, M_DONTWAIT);
89562587Sitojun		if ((m->m_flags & M_EXT) == 0) {
89662587Sitojun			m_free(m);
89762587Sitojun			m = NULL;
89862587Sitojun		}
89962587Sitojun	}
90062587Sitojun	if (m == NULL)
90162587Sitojun		return;
90278064Sume	m->m_pkthdr.rcvif = NULL;
90362587Sitojun
904148385Sume	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
90553541Sshin		m->m_flags |= M_MCAST;
90653541Sshin		im6o.im6o_multicast_ifp = ifp;
90753541Sshin		im6o.im6o_multicast_hlim = 255;
90853541Sshin		im6o.im6o_multicast_loop = 0;
90953541Sshin	}
91053541Sshin
91153541Sshin	icmp6len = sizeof(*nd_na);
91253541Sshin	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
91395023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
91453541Sshin
91553541Sshin	/* fill neighbor advertisement packet */
91653541Sshin	ip6 = mtod(m, struct ip6_hdr *);
91753541Sshin	ip6->ip6_flow = 0;
91862587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
91962587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
92053541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
92153541Sshin	ip6->ip6_hlim = 255;
922148385Sume	if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
92353541Sshin		/* reply to DAD */
924153227Sume		daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
925153227Sume		daddr6.s6_addr16[1] = 0;
926153227Sume		daddr6.s6_addr32[1] = 0;
927153227Sume		daddr6.s6_addr32[2] = 0;
928153227Sume		daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
929148385Sume		if (in6_setscope(&daddr6, ifp, NULL))
930148385Sume			goto bad;
931148385Sume
93253541Sshin		flags &= ~ND_NA_FLAG_SOLICITED;
933148385Sume	}
934148385Sume	ip6->ip6_dst = daddr6;
935148385Sume	bzero(&dst_sa, sizeof(struct sockaddr_in6));
936148385Sume	dst_sa.sin6_family = AF_INET6;
937148385Sume	dst_sa.sin6_len = sizeof(struct sockaddr_in6);
938148385Sume	dst_sa.sin6_addr = daddr6;
93953541Sshin
94053541Sshin	/*
94153541Sshin	 * Select a source whose scope is the same as that of the dest.
94253541Sshin	 */
943148385Sume	bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
944148385Sume	src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &error);
945148385Sume	if (src == NULL) {
946165118Sbz		char ip6buf[INET6_ADDRSTRLEN];
947148385Sume		nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
948148385Sume		    "determined: dst=%s, error=%d\n",
949165118Sbz		    ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
950148385Sume		goto bad;
95153541Sshin	}
952148385Sume	ip6->ip6_src = *src;
95353541Sshin	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
95453541Sshin	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
95553541Sshin	nd_na->nd_na_code = 0;
95653541Sshin	nd_na->nd_na_target = *taddr6;
957121315Sume	in6_clearscope(&nd_na->nd_na_target); /* XXX */
95853541Sshin
95953541Sshin	/*
96053541Sshin	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
96153541Sshin	 * see nd6_ns_input() for details.
96253541Sshin	 * Basically, if NS packet is sent to unicast/anycast addr,
96353541Sshin	 * target lladdr option SHOULD NOT be included.
96453541Sshin	 */
96562587Sitojun	if (tlladdr) {
96662587Sitojun		/*
96762587Sitojun		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
96862587Sitojun		 * lladdr in sdl0.  If we are not proxying (sending NA for
96962587Sitojun		 * my address) use lladdr configured for the interface.
97062587Sitojun		 */
971142215Sglebius		if (sdl0 == NULL) {
972142215Sglebius#ifdef DEV_CARP
973142215Sglebius			if (ifp->if_carp)
974142215Sglebius				mac = carp_macmatch6(ifp->if_carp, m, taddr6);
975142215Sglebius			if (mac == NULL)
976142215Sglebius				mac = nd6_ifptomac(ifp);
977142215Sglebius#else
97862587Sitojun			mac = nd6_ifptomac(ifp);
979142215Sglebius#endif
980142215Sglebius		} else if (sdl0->sa_family == AF_LINK) {
98162587Sitojun			struct sockaddr_dl *sdl;
98262587Sitojun			sdl = (struct sockaddr_dl *)sdl0;
98362587Sitojun			if (sdl->sdl_alen == ifp->if_addrlen)
98462587Sitojun				mac = LLADDR(sdl);
98562587Sitojun		}
98662587Sitojun	}
98762587Sitojun	if (tlladdr && mac) {
98853541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
98953541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
990120941Sume
99153541Sshin		/* roundup to 8 bytes alignment! */
99253541Sshin		optlen = (optlen + 7) & ~7;
99353541Sshin
99453541Sshin		m->m_pkthdr.len += optlen;
99553541Sshin		m->m_len += optlen;
99653541Sshin		icmp6len += optlen;
99753541Sshin		bzero((caddr_t)nd_opt, optlen);
99853541Sshin		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
99953541Sshin		nd_opt->nd_opt_len = optlen >> 3;
100053541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
100153541Sshin	} else
100253541Sshin		flags &= ~ND_NA_FLAG_OVERRIDE;
100353541Sshin
100453541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
100553541Sshin	nd_na->nd_na_flags_reserved = flags;
100653541Sshin	nd_na->nd_na_cksum = 0;
100753541Sshin	nd_na->nd_na_cksum =
1008120941Sume	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
100953541Sshin
1010148385Sume	ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1011148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_msg);
1012148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1013148385Sume	icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
1014148385Sume
1015148385Sume	if (ro.ro_rt) {		/* we don't cache this route. */
1016148385Sume		RTFREE(ro.ro_rt);
101753541Sshin	}
1018148385Sume	return;
1019148385Sume
1020148385Sume  bad:
1021148385Sume	if (ro.ro_rt) {
1022148385Sume		RTFREE(ro.ro_rt);
1023148385Sume	}
1024148385Sume	m_freem(m);
1025148385Sume	return;
102653541Sshin}
102753541Sshin
102853541Sshincaddr_t
102953541Sshinnd6_ifptomac(ifp)
103053541Sshin	struct ifnet *ifp;
103153541Sshin{
103253541Sshin	switch (ifp->if_type) {
103353541Sshin	case IFT_ARCNET:
103453541Sshin	case IFT_ETHER:
103553541Sshin	case IFT_FDDI:
103678064Sume	case IFT_IEEE1394:
103778468Ssumikawa#ifdef IFT_L2VLAN
103878468Ssumikawa	case IFT_L2VLAN:
103978468Ssumikawa#endif
104078064Sume#ifdef IFT_IEEE80211
104178064Sume	case IFT_IEEE80211:
104278064Sume#endif
1043142215Sglebius#ifdef IFT_CARP
1044142215Sglebius	case IFT_CARP:
1045142215Sglebius#endif
1046149829Sthompsa	case IFT_BRIDGE:
1047120049Smdodd	case IFT_ISO88025:
1048147306Sbrooks		return IF_LLADDR(ifp);
104953541Sshin	default:
105053541Sshin		return NULL;
105153541Sshin	}
105253541Sshin}
105353541Sshin
105460938SjakeTAILQ_HEAD(dadq_head, dadq);
105553541Sshinstruct dadq {
105660938Sjake	TAILQ_ENTRY(dadq) dad_list;
105753541Sshin	struct ifaddr *dad_ifa;
105853541Sshin	int dad_count;		/* max NS to send */
105962587Sitojun	int dad_ns_tcount;	/* # of trials to send NS */
106053541Sshin	int dad_ns_ocount;	/* NS sent so far */
106153541Sshin	int dad_ns_icount;
106253541Sshin	int dad_na_icount;
106378064Sume	struct callout dad_timer_ch;
106453541Sshin};
106553541Sshin
106653541Sshinstatic struct dadq_head dadq;
106778064Sumestatic int dad_init = 0;
106853541Sshin
106953541Sshinstatic struct dadq *
107053541Sshinnd6_dad_find(ifa)
107153541Sshin	struct ifaddr *ifa;
107253541Sshin{
107353541Sshin	struct dadq *dp;
107453541Sshin
107562587Sitojun	for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
107653541Sshin		if (dp->dad_ifa == ifa)
107753541Sshin			return dp;
107853541Sshin	}
107953541Sshin	return NULL;
108053541Sshin}
108153541Sshin
108278064Sumestatic void
108378064Sumend6_dad_starttimer(dp, ticks)
108478064Sume	struct dadq *dp;
108578064Sume	int ticks;
108678064Sume{
108778064Sume
108878064Sume	callout_reset(&dp->dad_timer_ch, ticks,
108978064Sume	    (void (*) __P((void *)))nd6_dad_timer, (void *)dp->dad_ifa);
109078064Sume}
109178064Sume
109278064Sumestatic void
109378064Sumend6_dad_stoptimer(dp)
109478064Sume	struct dadq *dp;
109578064Sume{
109678064Sume
109778064Sume	callout_stop(&dp->dad_timer_ch);
109878064Sume}
109978064Sume
110053541Sshin/*
1101148987Sume * Start Duplicate Address Detection (DAD) for specified interface address.
110253541Sshin */
110353541Sshinvoid
1104151539Ssuznd6_dad_start(ifa, delay)
110553541Sshin	struct ifaddr *ifa;
1106151539Ssuz	int delay;
110753541Sshin{
110853541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
110953541Sshin	struct dadq *dp;
1110165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
111153541Sshin
111253541Sshin	if (!dad_init) {
111353541Sshin		TAILQ_INIT(&dadq);
111453541Sshin		dad_init++;
111553541Sshin	}
111653541Sshin
111753541Sshin	/*
111853541Sshin	 * If we don't need DAD, don't do it.
111953541Sshin	 * There are several cases:
112053541Sshin	 * - DAD is disabled (ip6_dad_count == 0)
112153541Sshin	 * - the interface address is anycast
112253541Sshin	 */
112353541Sshin	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
112462587Sitojun		log(LOG_DEBUG,
112562587Sitojun			"nd6_dad_start: called with non-tentative address "
112653541Sshin			"%s(%s)\n",
1127165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
112853541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
112953541Sshin		return;
113053541Sshin	}
113153541Sshin	if (ia->ia6_flags & IN6_IFF_ANYCAST) {
113253541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
113353541Sshin		return;
113453541Sshin	}
113553541Sshin	if (!ip6_dad_count) {
113653541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
113753541Sshin		return;
113853541Sshin	}
1139151465Ssuz	if (ifa->ifa_ifp == NULL)
114053541Sshin		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1141120941Sume	if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
114253541Sshin		return;
1143120941Sume	}
114453541Sshin	if (nd6_dad_find(ifa) != NULL) {
114553541Sshin		/* DAD already in progress */
114653541Sshin		return;
114753541Sshin	}
114853541Sshin
114953541Sshin	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
115053541Sshin	if (dp == NULL) {
115162587Sitojun		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
115253541Sshin			"%s(%s)\n",
1153165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
115453541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
115553541Sshin		return;
115653541Sshin	}
115753541Sshin	bzero(dp, sizeof(*dp));
115878064Sume	callout_init(&dp->dad_timer_ch, 0);
115953541Sshin	TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
116053541Sshin
116178064Sume	nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1162165118Sbz	    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
116353541Sshin
116453541Sshin	/*
116553541Sshin	 * Send NS packet for DAD, ip6_dad_count times.
116653541Sshin	 * Note that we must delay the first transmission, if this is the
116753541Sshin	 * first packet to be sent from the interface after interface
116853541Sshin	 * (re)initialization.
116953541Sshin	 */
117053541Sshin	dp->dad_ifa = ifa;
117195023Ssuz	IFAREF(ifa);	/* just for safety */
117253541Sshin	dp->dad_count = ip6_dad_count;
117353541Sshin	dp->dad_ns_icount = dp->dad_na_icount = 0;
117462587Sitojun	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1175151539Ssuz	if (delay == 0) {
117662587Sitojun		nd6_dad_ns_output(dp, ifa);
1177121161Sume		nd6_dad_starttimer(dp,
1178151539Ssuz		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
117953541Sshin	} else {
1180151539Ssuz		nd6_dad_starttimer(dp, delay);
118153541Sshin	}
118253541Sshin}
118353541Sshin
118478064Sume/*
118578064Sume * terminate DAD unconditionally.  used for address removals.
118678064Sume */
118778064Sumevoid
118878064Sumend6_dad_stop(ifa)
118978064Sume	struct ifaddr *ifa;
119078064Sume{
119178064Sume	struct dadq *dp;
119278064Sume
119378064Sume	if (!dad_init)
119478064Sume		return;
119578064Sume	dp = nd6_dad_find(ifa);
119678064Sume	if (!dp) {
119778064Sume		/* DAD wasn't started yet */
119878064Sume		return;
119978064Sume	}
120078064Sume
120178064Sume	nd6_dad_stoptimer(dp);
120278064Sume
120378064Sume	TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
120478064Sume	free(dp, M_IP6NDP);
120578064Sume	dp = NULL;
120678064Sume	IFAFREE(ifa);
120778064Sume}
120878064Sume
120953541Sshinstatic void
121053541Sshinnd6_dad_timer(ifa)
121153541Sshin	struct ifaddr *ifa;
121253541Sshin{
121353541Sshin	int s;
121453541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
121553541Sshin	struct dadq *dp;
1216165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
121753541Sshin
121895023Ssuz	s = splnet();		/* XXX */
121953541Sshin
122053541Sshin	/* Sanity check */
122153541Sshin	if (ia == NULL) {
122262587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
122353541Sshin		goto done;
122453541Sshin	}
122553541Sshin	dp = nd6_dad_find(ifa);
122653541Sshin	if (dp == NULL) {
122762587Sitojun		log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
122853541Sshin		goto done;
122953541Sshin	}
123053541Sshin	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
123162587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
123253541Sshin			"%s(%s)\n",
1233165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
123453541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
123553541Sshin		goto done;
123653541Sshin	}
123753541Sshin	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
123862587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
123953541Sshin			"%s(%s)\n",
1240165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
124153541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
124253541Sshin		goto done;
124353541Sshin	}
124453541Sshin
124562587Sitojun	/* timeouted with IFF_{RUNNING,UP} check */
124662587Sitojun	if (dp->dad_ns_tcount > dad_maxtry) {
124778064Sume		nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1248120941Sume		    if_name(ifa->ifa_ifp)));
124962587Sitojun
125062587Sitojun		TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
125162587Sitojun		free(dp, M_IP6NDP);
125262587Sitojun		dp = NULL;
125362587Sitojun		IFAFREE(ifa);
125462587Sitojun		goto done;
125562587Sitojun	}
125662587Sitojun
125753541Sshin	/* Need more checks? */
125853541Sshin	if (dp->dad_ns_ocount < dp->dad_count) {
125953541Sshin		/*
126053541Sshin		 * We have more NS to go.  Send NS packet for DAD.
126153541Sshin		 */
126262587Sitojun		nd6_dad_ns_output(dp, ifa);
1263120941Sume		nd6_dad_starttimer(dp,
1264151539Ssuz		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
126553541Sshin	} else {
126653541Sshin		/*
126753541Sshin		 * We have transmitted sufficient number of DAD packets.
126853541Sshin		 * See what we've got.
126953541Sshin		 */
127053541Sshin		int duplicate;
127153541Sshin
127253541Sshin		duplicate = 0;
127353541Sshin
127453541Sshin		if (dp->dad_na_icount) {
127553541Sshin			/*
127653541Sshin			 * the check is in nd6_dad_na_input(),
127753541Sshin			 * but just in case
127853541Sshin			 */
127953541Sshin			duplicate++;
128053541Sshin		}
128153541Sshin
128253541Sshin		if (dp->dad_ns_icount) {
128353541Sshin			/* We've seen NS, means DAD has failed. */
128453541Sshin			duplicate++;
128553541Sshin		}
128653541Sshin
128753541Sshin		if (duplicate) {
128853541Sshin			/* (*dp) will be freed in nd6_dad_duplicated() */
128953541Sshin			dp = NULL;
129053541Sshin			nd6_dad_duplicated(ifa);
129153541Sshin		} else {
129253541Sshin			/*
129353541Sshin			 * We are done with DAD.  No NA came, no NS came.
1294148987Sume			 * No duplicate address found.
129553541Sshin			 */
129653541Sshin			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
129753541Sshin
129878064Sume			nd6log((LOG_DEBUG,
129962587Sitojun			    "%s: DAD complete for %s - no duplicates found\n",
130062587Sitojun			    if_name(ifa->ifa_ifp),
1301165118Sbz			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
130253541Sshin
130353541Sshin			TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
130453541Sshin			free(dp, M_IP6NDP);
130553541Sshin			dp = NULL;
130662587Sitojun			IFAFREE(ifa);
130753541Sshin		}
130853541Sshin	}
130953541Sshin
131053541Sshindone:
131153541Sshin	splx(s);
131253541Sshin}
131353541Sshin
131453541Sshinvoid
131553541Sshinnd6_dad_duplicated(ifa)
131653541Sshin	struct ifaddr *ifa;
131753541Sshin{
131853541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1319151477Ssuz	struct ifnet *ifp;
132053541Sshin	struct dadq *dp;
1321165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
132253541Sshin
132353541Sshin	dp = nd6_dad_find(ifa);
132453541Sshin	if (dp == NULL) {
132562587Sitojun		log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
132653541Sshin		return;
132753541Sshin	}
132853541Sshin
132978064Sume	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
133078064Sume	    "NS in/out=%d/%d, NA in=%d\n",
1331165118Sbz	    if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
133278064Sume	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
133353541Sshin
133453541Sshin	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
133553541Sshin	ia->ia6_flags |= IN6_IFF_DUPLICATED;
133653541Sshin
1337148987Sume	/* We are done with DAD, with duplicate address found. (failure) */
133878064Sume	nd6_dad_stoptimer(dp);
133953541Sshin
1340151477Ssuz	ifp = ifa->ifa_ifp;
134162587Sitojun	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1342165118Sbz	    if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
134362587Sitojun	log(LOG_ERR, "%s: manual intervention required\n",
1344151477Ssuz	    if_name(ifp));
134553541Sshin
1346151477Ssuz	/*
1347151477Ssuz	 * If the address is a link-local address formed from an interface
1348151477Ssuz	 * identifier based on the hardware address which is supposed to be
1349151477Ssuz	 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1350151477Ssuz	 * operation on the interface SHOULD be disabled.
1351151477Ssuz	 * [rfc2462bis-03 Section 5.4.5]
1352151477Ssuz	 */
1353151477Ssuz	if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1354151477Ssuz		struct in6_addr in6;
1355151477Ssuz
1356151477Ssuz		/*
1357151477Ssuz		 * To avoid over-reaction, we only apply this logic when we are
1358151477Ssuz		 * very sure that hardware addresses are supposed to be unique.
1359151477Ssuz		 */
1360151477Ssuz		switch (ifp->if_type) {
1361151477Ssuz		case IFT_ETHER:
1362151477Ssuz		case IFT_FDDI:
1363151477Ssuz		case IFT_ATM:
1364151477Ssuz		case IFT_IEEE1394:
1365151477Ssuz#ifdef IFT_IEEE80211
1366151477Ssuz		case IFT_IEEE80211:
1367151477Ssuz#endif
1368151477Ssuz			in6 = ia->ia_addr.sin6_addr;
1369151477Ssuz			if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1370151477Ssuz			    IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1371151477Ssuz				ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1372151477Ssuz				log(LOG_ERR, "%s: possible hardware address "
1373151477Ssuz				    "duplication detected, disable IPv6\n",
1374151477Ssuz				    if_name(ifp));
1375151477Ssuz			}
1376151477Ssuz			break;
1377151477Ssuz		}
1378151477Ssuz	}
1379151477Ssuz
138053541Sshin	TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
138153541Sshin	free(dp, M_IP6NDP);
138253541Sshin	dp = NULL;
138362587Sitojun	IFAFREE(ifa);
138453541Sshin}
138553541Sshin
138662587Sitojunstatic void
138762587Sitojunnd6_dad_ns_output(dp, ifa)
138862587Sitojun	struct dadq *dp;
138962587Sitojun	struct ifaddr *ifa;
139062587Sitojun{
139162587Sitojun	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
139262587Sitojun	struct ifnet *ifp = ifa->ifa_ifp;
139362587Sitojun
139462587Sitojun	dp->dad_ns_tcount++;
139562587Sitojun	if ((ifp->if_flags & IFF_UP) == 0) {
139662587Sitojun		return;
139762587Sitojun	}
1398148887Srwatson	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
139962587Sitojun		return;
140062587Sitojun	}
140162587Sitojun
140262587Sitojun	dp->dad_ns_ocount++;
140362587Sitojun	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
140462587Sitojun}
140562587Sitojun
140662587Sitojunstatic void
140753541Sshinnd6_dad_ns_input(ifa)
140853541Sshin	struct ifaddr *ifa;
140953541Sshin{
141053541Sshin	struct in6_ifaddr *ia;
141153541Sshin	struct ifnet *ifp;
141278064Sume	const struct in6_addr *taddr6;
141353541Sshin	struct dadq *dp;
141453541Sshin	int duplicate;
141553541Sshin
1416151465Ssuz	if (ifa == NULL)
141753541Sshin		panic("ifa == NULL in nd6_dad_ns_input");
141853541Sshin
141953541Sshin	ia = (struct in6_ifaddr *)ifa;
142053541Sshin	ifp = ifa->ifa_ifp;
142153541Sshin	taddr6 = &ia->ia_addr.sin6_addr;
142253541Sshin	duplicate = 0;
142353541Sshin	dp = nd6_dad_find(ifa);
142453541Sshin
142553541Sshin	/* Quickhack - completely ignore DAD NS packets */
142653541Sshin	if (dad_ignore_ns) {
1427165118Sbz		char ip6buf[INET6_ADDRSTRLEN];
142878064Sume		nd6log((LOG_INFO,
142978064Sume		    "nd6_dad_ns_input: ignoring DAD NS packet for "
1430165118Sbz		    "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6),
143178064Sume		    if_name(ifa->ifa_ifp)));
143253541Sshin		return;
143353541Sshin	}
143453541Sshin
143553541Sshin	/*
143653541Sshin	 * if I'm yet to start DAD, someone else started using this address
143753541Sshin	 * first.  I have a duplicate and you win.
143853541Sshin	 */
1439151465Ssuz	if (dp == NULL || dp->dad_ns_ocount == 0)
144053541Sshin		duplicate++;
144153541Sshin
144253541Sshin	/* XXX more checks for loopback situation - see nd6_dad_timer too */
144353541Sshin
144453541Sshin	if (duplicate) {
144553541Sshin		dp = NULL;	/* will be freed in nd6_dad_duplicated() */
144653541Sshin		nd6_dad_duplicated(ifa);
144753541Sshin	} else {
144853541Sshin		/*
144953541Sshin		 * not sure if I got a duplicate.
145053541Sshin		 * increment ns count and see what happens.
145153541Sshin		 */
145253541Sshin		if (dp)
145353541Sshin			dp->dad_ns_icount++;
145453541Sshin	}
145553541Sshin}
145653541Sshin
145762587Sitojunstatic void
145853541Sshinnd6_dad_na_input(ifa)
145953541Sshin	struct ifaddr *ifa;
146053541Sshin{
146153541Sshin	struct dadq *dp;
146253541Sshin
1463151465Ssuz	if (ifa == NULL)
146453541Sshin		panic("ifa == NULL in nd6_dad_na_input");
146553541Sshin
146653541Sshin	dp = nd6_dad_find(ifa);
146753541Sshin	if (dp)
146853541Sshin		dp->dad_na_icount++;
146953541Sshin
147053541Sshin	/* remove the address. */
147153541Sshin	nd6_dad_duplicated(ifa);
147253541Sshin}
1473