nd6_nbr.c revision 186119
1139826Simp/*-
253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
353541Sshin * All rights reserved.
453541Sshin *
553541Sshin * Redistribution and use in source and binary forms, with or without
653541Sshin * modification, are permitted provided that the following conditions
753541Sshin * are met:
853541Sshin * 1. Redistributions of source code must retain the above copyright
953541Sshin *    notice, this list of conditions and the following disclaimer.
1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1153541Sshin *    notice, this list of conditions and the following disclaimer in the
1253541Sshin *    documentation and/or other materials provided with the distribution.
1353541Sshin * 3. Neither the name of the project nor the names of its contributors
1453541Sshin *    may be used to endorse or promote products derived from this software
1553541Sshin *    without specific prior written permission.
1653541Sshin *
1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2053541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2753541Sshin * SUCH DAMAGE.
28174510Sobrien *
29174510Sobrien *	$KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $
3053541Sshin */
3153541Sshin
32174510Sobrien#include <sys/cdefs.h>
33174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/nd6_nbr.c 186119 2008-12-15 06:10:57Z qingli $");
34174510Sobrien
3562587Sitojun#include "opt_inet.h"
3662587Sitojun#include "opt_inet6.h"
37142215Sglebius#include "opt_ipsec.h"
38142215Sglebius#include "opt_carp.h"
39178167Sqingli#include "opt_mpath.h"
4055009Sshin
4153541Sshin#include <sys/param.h>
4253541Sshin#include <sys/systm.h>
4353541Sshin#include <sys/malloc.h>
44186119Sqingli#include <sys/lock.h>
45186119Sqingli#include <sys/rwlock.h>
4653541Sshin#include <sys/mbuf.h>
4753541Sshin#include <sys/socket.h>
4853541Sshin#include <sys/sockio.h>
4953541Sshin#include <sys/time.h>
5053541Sshin#include <sys/kernel.h>
5153541Sshin#include <sys/errno.h>
5253541Sshin#include <sys/syslog.h>
5353541Sshin#include <sys/queue.h>
5478064Sume#include <sys/callout.h>
55181803Sbz#include <sys/vimage.h>
5653541Sshin
5753541Sshin#include <net/if.h>
5853541Sshin#include <net/if_types.h>
5953541Sshin#include <net/if_dl.h>
60147306Sbrooks#include <net/if_var.h>
6153541Sshin#include <net/route.h>
62178167Sqingli#ifdef RADIX_MPATH
63178167Sqingli#include <net/radix_mpath.h>
64178167Sqingli#endif
6553541Sshin
6653541Sshin#include <netinet/in.h>
6753541Sshin#include <netinet/in_var.h>
68186119Sqingli#include <net/if_llatbl.h>
69186119Sqingli#define	L3_ADDR_SIN6(le)	((struct sockaddr_in6 *) L3_ADDR(le))
7053541Sshin#include <netinet6/in6_var.h>
71151477Ssuz#include <netinet6/in6_ifattach.h>
7262587Sitojun#include <netinet/ip6.h>
7353541Sshin#include <netinet6/ip6_var.h>
74148385Sume#include <netinet6/scope6_var.h>
7553541Sshin#include <netinet6/nd6.h>
7662587Sitojun#include <netinet/icmp6.h>
77185571Sbz#include <netinet6/vinet6.h>
7853541Sshin
79142215Sglebius#ifdef DEV_CARP
80142215Sglebius#include <netinet/ip_carp.h>
81142215Sglebius#endif
82142215Sglebius
8362587Sitojun#define SDL(s) ((struct sockaddr_dl *)s)
8453541Sshin
8562587Sitojunstruct dadq;
86175162Sobrienstatic struct dadq *nd6_dad_find(struct ifaddr *);
87175162Sobrienstatic void nd6_dad_starttimer(struct dadq *, int);
88175162Sobrienstatic void nd6_dad_stoptimer(struct dadq *);
89175162Sobrienstatic void nd6_dad_timer(struct ifaddr *);
90175162Sobrienstatic void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
91175162Sobrienstatic void nd6_dad_ns_input(struct ifaddr *);
92175162Sobrienstatic void nd6_dad_na_input(struct ifaddr *);
9353541Sshin
94185088Szec#ifdef VIMAGE_GLOBALS
95185088Szecint dad_ignore_ns;
96185088Szecint dad_maxtry;
97185088Szec#endif
9853541Sshin
9953541Sshin/*
100108470Sschweikh * Input a Neighbor Solicitation Message.
10153541Sshin *
10253541Sshin * Based on RFC 2461
103148987Sume * Based on RFC 2462 (duplicate address detection)
10453541Sshin */
10553541Sshinvoid
106171259Sdelphijnd6_ns_input(struct mbuf *m, int off, int icmp6len)
10753541Sshin{
108183550Szec	INIT_VNET_INET6(curvnet);
10953541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
11053541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
11162587Sitojun	struct nd_neighbor_solicit *nd_ns;
11253541Sshin	struct in6_addr saddr6 = ip6->ip6_src;
11353541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
11462587Sitojun	struct in6_addr taddr6;
11553541Sshin	struct in6_addr myaddr6;
11653541Sshin	char *lladdr = NULL;
117142215Sglebius	struct ifaddr *ifa = NULL;
11853541Sshin	int lladdrlen = 0;
11953541Sshin	int anycast = 0, proxy = 0, tentative = 0;
12053541Sshin	int tlladdr;
12153541Sshin	union nd_opts ndopts;
12262587Sitojun	struct sockaddr_dl *proxydl = NULL;
123165118Sbz	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
12453541Sshin
12578064Sume#ifndef PULLDOWN_TEST
12678064Sume	IP6_EXTHDR_CHECK(m, off, icmp6len,);
12778064Sume	nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
12878064Sume#else
12978064Sume	IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
13078064Sume	if (nd_ns == NULL) {
131181803Sbz		V_icmp6stat.icp6s_tooshort++;
13278064Sume		return;
13378064Sume	}
13478064Sume#endif
13578064Sume	ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
13678064Sume	taddr6 = nd_ns->nd_ns_target;
137148385Sume	if (in6_setscope(&taddr6, ifp, NULL) != 0)
138148385Sume		goto bad;
13978064Sume
14053541Sshin	if (ip6->ip6_hlim != 255) {
14178064Sume		nd6log((LOG_ERR,
14278064Sume		    "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
143165118Sbz		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
144165118Sbz		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
14578064Sume		goto bad;
14653541Sshin	}
14753541Sshin
14853541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
149148987Sume		/* dst has to be a solicited node multicast address. */
150120941Sume		if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
15195023Ssuz		    /* don't check ifindex portion */
152120941Sume		    daddr6.s6_addr32[1] == 0 &&
153120941Sume		    daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
154120941Sume		    daddr6.s6_addr8[12] == 0xff) {
15595023Ssuz			; /* good */
15653541Sshin		} else {
15778064Sume			nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
158120941Sume			    "(wrong ip6 dst)\n"));
15953541Sshin			goto bad;
16053541Sshin		}
161185348Szec	} else if (!V_nd6_onlink_ns_rfc4861) {
162183529Scperciva		struct sockaddr_in6 src_sa6;
163183529Scperciva
164183529Scperciva		/*
165183529Scperciva		 * According to recent IETF discussions, it is not a good idea
166183529Scperciva		 * to accept a NS from an address which would not be deemed
167183529Scperciva		 * to be a neighbor otherwise.  This point is expected to be
168183529Scperciva		 * clarified in future revisions of the specification.
169183529Scperciva		 */
170183529Scperciva		bzero(&src_sa6, sizeof(src_sa6));
171183529Scperciva		src_sa6.sin6_family = AF_INET6;
172183529Scperciva		src_sa6.sin6_len = sizeof(src_sa6);
173183529Scperciva		src_sa6.sin6_addr = saddr6;
174186119Sqingli		if (nd6_is_addr_neighbor(&src_sa6, ifp) == 0) {
175183529Scperciva			nd6log((LOG_INFO, "nd6_ns_input: "
176183529Scperciva				"NS packet from non-neighbor\n"));
177183529Scperciva			goto bad;
178183529Scperciva		}
17953541Sshin	}
18053541Sshin
18153541Sshin	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
18278064Sume		nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
18353541Sshin		goto bad;
18453541Sshin	}
18553541Sshin
18653541Sshin	icmp6len -= sizeof(*nd_ns);
18753541Sshin	nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
18853541Sshin	if (nd6_options(&ndopts) < 0) {
18978064Sume		nd6log((LOG_INFO,
19078064Sume		    "nd6_ns_input: invalid ND option, ignored\n"));
19178064Sume		/* nd6_options have incremented stats */
19278064Sume		goto freeit;
19353541Sshin	}
19453541Sshin
19553541Sshin	if (ndopts.nd_opts_src_lladdr) {
19695023Ssuz		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
19753541Sshin		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
19853541Sshin	}
199120941Sume
20053541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
20178064Sume		nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
20278064Sume		    "(link-layer address option)\n"));
20353541Sshin		goto bad;
20453541Sshin	}
20553541Sshin
20653541Sshin	/*
20753541Sshin	 * Attaching target link-layer address to the NA?
20853541Sshin	 * (RFC 2461 7.2.4)
20953541Sshin	 *
21053541Sshin	 * NS IP dst is unicast/anycast			MUST NOT add
21153541Sshin	 * NS IP dst is solicited-node multicast	MUST add
21253541Sshin	 *
21353541Sshin	 * In implementation, we add target link-layer address by default.
21453541Sshin	 * We do not add one in MUST NOT cases.
21553541Sshin	 */
21653541Sshin	if (!IN6_IS_ADDR_MULTICAST(&daddr6))
21753541Sshin		tlladdr = 0;
21853541Sshin	else
21953541Sshin		tlladdr = 1;
22053541Sshin
22153541Sshin	/*
22253541Sshin	 * Target address (taddr6) must be either:
22353541Sshin	 * (1) Valid unicast/anycast address for my receiving interface,
22453541Sshin	 * (2) Unicast address for which I'm offering proxy service, or
22553541Sshin	 * (3) "tentative" address on which DAD is being performed.
22653541Sshin	 */
22753541Sshin	/* (1) and (3) check. */
228142215Sglebius#ifdef DEV_CARP
229142215Sglebius	if (ifp->if_carp)
230142215Sglebius		ifa = carp_iamatch6(ifp->if_carp, &taddr6);
231151465Ssuz	if (ifa == NULL)
232142215Sglebius		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
233142215Sglebius#else
23453541Sshin	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
235142215Sglebius#endif
23653541Sshin
23753541Sshin	/* (2) check. */
238151465Ssuz	if (ifa == NULL) {
23953541Sshin		struct rtentry *rt;
24053541Sshin		struct sockaddr_in6 tsin6;
241121765Ssam		int need_proxy;
242178167Sqingli#ifdef RADIX_MPATH
243178167Sqingli		struct route_in6 ro;
244178167Sqingli#endif
24553541Sshin
246171260Sdelphij		bzero(&tsin6, sizeof tsin6);
24753541Sshin		tsin6.sin6_len = sizeof(struct sockaddr_in6);
24853541Sshin		tsin6.sin6_family = AF_INET6;
24953541Sshin		tsin6.sin6_addr = taddr6;
25053541Sshin
251178167Sqingli#ifdef RADIX_MPATH
252178167Sqingli		bzero(&ro, sizeof(ro));
253178167Sqingli		ro.ro_dst = tsin6;
254178167Sqingli		rtalloc_mpath((struct route *)&ro, RTF_ANNOUNCE);
255178167Sqingli		rt = ro.ro_rt;
256178167Sqingli#else
25753541Sshin		rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
258178167Sqingli#endif
259121765Ssam		need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
260121765Ssam		    rt->rt_gateway->sa_family == AF_LINK);
261121765Ssam		if (rt)
262121765Ssam			rtfree(rt);
263121765Ssam		if (need_proxy) {
26453541Sshin			/*
26562587Sitojun			 * proxy NDP for single entry
26653541Sshin			 */
26762587Sitojun			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
26862587Sitojun				IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
26962587Sitojun			if (ifa) {
27053541Sshin				proxy = 1;
27162587Sitojun				proxydl = SDL(rt->rt_gateway);
27262587Sitojun			}
27353541Sshin		}
27453541Sshin	}
275151479Ssuz	if (ifa == NULL) {
27653541Sshin		/*
27778064Sume		 * We've got an NS packet, and we don't have that adddress
27853541Sshin		 * assigned for us.  We MUST silently ignore it.
27953541Sshin		 * See RFC2461 7.2.3.
28053541Sshin		 */
28162587Sitojun		goto freeit;
28253541Sshin	}
28353541Sshin	myaddr6 = *IFA_IN6(ifa);
28453541Sshin	anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
28553541Sshin	tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
28653541Sshin	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
28762587Sitojun		goto freeit;
28853541Sshin
28953541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
290120941Sume		nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
29153541Sshin		    "(if %d, NS packet %d)\n",
292165118Sbz		    ip6_sprintf(ip6bufs, &taddr6),
293120941Sume		    ifp->if_addrlen, lladdrlen - 2));
29478064Sume		goto bad;
29553541Sshin	}
29653541Sshin
29753541Sshin	if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
298120941Sume		nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
299165118Sbz		    ip6_sprintf(ip6bufs, &saddr6)));
30062587Sitojun		goto freeit;
30153541Sshin	}
30253541Sshin
30353541Sshin	/*
30453541Sshin	 * We have neighbor solicitation packet, with target address equals to
30553541Sshin	 * one of my tentative address.
30653541Sshin	 *
30753541Sshin	 * src addr	how to process?
30853541Sshin	 * ---		---
30953541Sshin	 * multicast	of course, invalid (rejected in ip6_input)
31053541Sshin	 * unicast	somebody is doing address resolution -> ignore
31153541Sshin	 * unspec	dup address detection
31253541Sshin	 *
31353541Sshin	 * The processing is defined in RFC 2462.
31453541Sshin	 */
31553541Sshin	if (tentative) {
31653541Sshin		/*
31753541Sshin		 * If source address is unspecified address, it is for
318148987Sume		 * duplicate address detection.
31953541Sshin		 *
32053541Sshin		 * If not, the packet is for addess resolution;
32153541Sshin		 * silently ignore it.
32253541Sshin		 */
32353541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
32453541Sshin			nd6_dad_ns_input(ifa);
32553541Sshin
32662587Sitojun		goto freeit;
32753541Sshin	}
32853541Sshin
32953541Sshin	/*
33053541Sshin	 * If the source address is unspecified address, entries must not
33153541Sshin	 * be created or updated.
33253541Sshin	 * It looks that sender is performing DAD.  Output NA toward
33353541Sshin	 * all-node multicast address, to tell the sender that I'm using
33453541Sshin	 * the address.
33553541Sshin	 * S bit ("solicited") must be zero.
33653541Sshin	 */
33753541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
338148385Sume		struct in6_addr in6_all;
339148385Sume
340148385Sume		in6_all = in6addr_linklocal_allnodes;
341148385Sume		if (in6_setscope(&in6_all, ifp, NULL) != 0)
342148385Sume			goto bad;
343148385Sume		nd6_na_output(ifp, &in6_all, &taddr6,
344120941Sume		    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
345181803Sbz		    (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
346120941Sume		    tlladdr, (struct sockaddr *)proxydl);
34762587Sitojun		goto freeit;
34853541Sshin	}
34953541Sshin
350120941Sume	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
351120941Sume	    ND_NEIGHBOR_SOLICIT, 0);
35253541Sshin
35353541Sshin	nd6_na_output(ifp, &saddr6, &taddr6,
354120941Sume	    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
355181803Sbz	    (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
356120941Sume	    tlladdr, (struct sockaddr *)proxydl);
35762587Sitojun freeit:
35862587Sitojun	m_freem(m);
35953541Sshin	return;
36053541Sshin
36153541Sshin bad:
362165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
363165118Sbz		ip6_sprintf(ip6bufs, &saddr6)));
364165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
365165118Sbz		ip6_sprintf(ip6bufs, &daddr6)));
366165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
367165118Sbz		ip6_sprintf(ip6bufs, &taddr6)));
368181803Sbz	V_icmp6stat.icp6s_badns++;
36962587Sitojun	m_freem(m);
37053541Sshin}
37153541Sshin
37253541Sshin/*
373108470Sschweikh * Output a Neighbor Solicitation Message. Caller specifies:
37453541Sshin *	- ICMP6 header source IP6 address
37553541Sshin *	- ND6 header target IP6 address
37653541Sshin *	- ND6 header source datalink address
37753541Sshin *
37853541Sshin * Based on RFC 2461
379148987Sume * Based on RFC 2462 (duplicate address detection)
380171259Sdelphij *
381171259Sdelphij *   ln - for source address determination
382171259Sdelphij *  dad - duplicate address detection
38353541Sshin */
38453541Sshinvoid
385186119Sqinglind6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
386186119Sqingli    const struct in6_addr *taddr6, struct llentry *ln, int dad)
38753541Sshin{
388183550Szec	INIT_VNET_INET6(ifp->if_vnet);
38953541Sshin	struct mbuf *m;
39053541Sshin	struct ip6_hdr *ip6;
39153541Sshin	struct nd_neighbor_solicit *nd_ns;
392148385Sume	struct in6_addr *src, src_in;
39353541Sshin	struct ip6_moptions im6o;
39453541Sshin	int icmp6len;
39562587Sitojun	int maxlen;
39653541Sshin	caddr_t mac;
397148385Sume	struct route_in6 ro;
398120941Sume
399148385Sume	bzero(&ro, sizeof(ro));
400148385Sume
40153541Sshin	if (IN6_IS_ADDR_MULTICAST(taddr6))
40253541Sshin		return;
40353541Sshin
40462587Sitojun	/* estimate the size of message */
40562587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
40662587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
40762587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
40862587Sitojun#ifdef DIAGNOSTIC
40962587Sitojun		printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
41062587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
41162587Sitojun#endif
41253541Sshin		return;
41362587Sitojun	}
41453541Sshin
415111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
41662587Sitojun	if (m && max_linkhdr + maxlen >= MHLEN) {
417111119Simp		MCLGET(m, M_DONTWAIT);
41862587Sitojun		if ((m->m_flags & M_EXT) == 0) {
41962587Sitojun			m_free(m);
42062587Sitojun			m = NULL;
42162587Sitojun		}
42262587Sitojun	}
42362587Sitojun	if (m == NULL)
42462587Sitojun		return;
42578064Sume	m->m_pkthdr.rcvif = NULL;
42662587Sitojun
42753541Sshin	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
42853541Sshin		m->m_flags |= M_MCAST;
42953541Sshin		im6o.im6o_multicast_ifp = ifp;
43053541Sshin		im6o.im6o_multicast_hlim = 255;
43153541Sshin		im6o.im6o_multicast_loop = 0;
43253541Sshin	}
43353541Sshin
43453541Sshin	icmp6len = sizeof(*nd_ns);
43553541Sshin	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
43695023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
43753541Sshin
43853541Sshin	/* fill neighbor solicitation packet */
43953541Sshin	ip6 = mtod(m, struct ip6_hdr *);
44053541Sshin	ip6->ip6_flow = 0;
44162587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
44262587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
44353541Sshin	/* ip6->ip6_plen will be set later */
44453541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
44553541Sshin	ip6->ip6_hlim = 255;
44653541Sshin	if (daddr6)
44753541Sshin		ip6->ip6_dst = *daddr6;
44853541Sshin	else {
44953541Sshin		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
450148385Sume		ip6->ip6_dst.s6_addr16[1] = 0;
45153541Sshin		ip6->ip6_dst.s6_addr32[1] = 0;
45253541Sshin		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
45353541Sshin		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
45453541Sshin		ip6->ip6_dst.s6_addr8[12] = 0xff;
455148385Sume		if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
456148385Sume			goto bad;
45753541Sshin	}
45853541Sshin	if (!dad) {
45953541Sshin		/*
46053541Sshin		 * RFC2461 7.2.2:
46153541Sshin		 * "If the source address of the packet prompting the
46253541Sshin		 * solicitation is the same as one of the addresses assigned
46353541Sshin		 * to the outgoing interface, that address SHOULD be placed
46453541Sshin		 * in the IP Source Address of the outgoing solicitation.
46553541Sshin		 * Otherwise, any one of the addresses assigned to the
46653541Sshin		 * interface should be used."
46753541Sshin		 *
46853541Sshin		 * We use the source address for the prompting packet
46953541Sshin		 * (saddr6), if:
47053541Sshin		 * - saddr6 is given from the caller (by giving "ln"), and
47153541Sshin		 * - saddr6 belongs to the outgoing interface.
472148385Sume		 * Otherwise, we perform the source address selection as usual.
47353541Sshin		 */
47495023Ssuz		struct ip6_hdr *hip6;		/* hold ip6 */
475148385Sume		struct in6_addr *hsrc = NULL;
47653541Sshin
477186119Sqingli		if (ln && ln->la_hold) {
478148987Sume			/*
479186119Sqingli			 * assuming every packet in la_hold has the same IP
480148987Sume			 * header
481148987Sume			 */
482186119Sqingli			hip6 = mtod(ln->la_hold, struct ip6_hdr *);
48353541Sshin			/* XXX pullup? */
484186119Sqingli			if (sizeof(*hip6) < ln->la_hold->m_len)
485148385Sume				hsrc = &hip6->ip6_src;
48653541Sshin			else
487148385Sume				hsrc = NULL;
488148385Sume		}
489148385Sume		if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc))
490148385Sume			src = hsrc;
49153541Sshin		else {
492148385Sume			int error;
493148385Sume			struct sockaddr_in6 dst_sa;
494148385Sume
495148385Sume			bzero(&dst_sa, sizeof(dst_sa));
496148385Sume			dst_sa.sin6_family = AF_INET6;
497148385Sume			dst_sa.sin6_len = sizeof(dst_sa);
498148385Sume			dst_sa.sin6_addr = ip6->ip6_dst;
499148385Sume
500148385Sume			src = in6_selectsrc(&dst_sa, NULL,
501148385Sume			    NULL, &ro, NULL, NULL, &error);
502148385Sume			if (src == NULL) {
503165118Sbz				char ip6buf[INET6_ADDRSTRLEN];
504148385Sume				nd6log((LOG_DEBUG,
505148385Sume				    "nd6_ns_output: source can't be "
506148385Sume				    "determined: dst=%s, error=%d\n",
507165118Sbz				    ip6_sprintf(ip6buf, &dst_sa.sin6_addr),
508165118Sbz				    error));
509148385Sume				goto bad;
51053541Sshin			}
51153541Sshin		}
51253541Sshin	} else {
51353541Sshin		/*
51453541Sshin		 * Source address for DAD packet must always be IPv6
51553541Sshin		 * unspecified address. (0::0)
516148385Sume		 * We actually don't have to 0-clear the address (we did it
517148385Sume		 * above), but we do so here explicitly to make the intention
518148385Sume		 * clearer.
51953541Sshin		 */
520148385Sume		bzero(&src_in, sizeof(src_in));
521148385Sume		src = &src_in;
52253541Sshin	}
523148385Sume	ip6->ip6_src = *src;
52453541Sshin	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
52553541Sshin	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
52653541Sshin	nd_ns->nd_ns_code = 0;
52753541Sshin	nd_ns->nd_ns_reserved = 0;
52853541Sshin	nd_ns->nd_ns_target = *taddr6;
529121315Sume	in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
53053541Sshin
53153541Sshin	/*
53253541Sshin	 * Add source link-layer address option.
53353541Sshin	 *
53453541Sshin	 *				spec		implementation
53553541Sshin	 *				---		---
53653541Sshin	 * DAD packet			MUST NOT	do not add the option
53753541Sshin	 * there's no link layer address:
53853541Sshin	 *				impossible	do not add the option
53953541Sshin	 * there's link layer address:
54053541Sshin	 *	Multicast NS		MUST add one	add the option
54153541Sshin	 *	Unicast NS		SHOULD add one	add the option
54253541Sshin	 */
54353541Sshin	if (!dad && (mac = nd6_ifptomac(ifp))) {
54453541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
54553541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
54653541Sshin		/* 8 byte alignments... */
54753541Sshin		optlen = (optlen + 7) & ~7;
548120941Sume
54953541Sshin		m->m_pkthdr.len += optlen;
55053541Sshin		m->m_len += optlen;
55153541Sshin		icmp6len += optlen;
55253541Sshin		bzero((caddr_t)nd_opt, optlen);
55353541Sshin		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
55453541Sshin		nd_opt->nd_opt_len = optlen >> 3;
55553541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
55653541Sshin	}
55753541Sshin
55853541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
55953541Sshin	nd_ns->nd_ns_cksum = 0;
560120941Sume	nd_ns->nd_ns_cksum =
561120941Sume	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
56253541Sshin
563151536Ssuz	ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
564148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_msg);
565148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
566181803Sbz	V_icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
567148385Sume
568148385Sume	if (ro.ro_rt) {		/* we don't cache this route. */
569148385Sume		RTFREE(ro.ro_rt);
57053541Sshin	}
571148385Sume	return;
572148385Sume
573148385Sume  bad:
574148385Sume	if (ro.ro_rt) {
575148385Sume		RTFREE(ro.ro_rt);
576148385Sume	}
577148385Sume	m_freem(m);
578148385Sume	return;
57953541Sshin}
58053541Sshin
58153541Sshin/*
58253541Sshin * Neighbor advertisement input handling.
58353541Sshin *
58453541Sshin * Based on RFC 2461
585148987Sume * Based on RFC 2462 (duplicate address detection)
58662587Sitojun *
58762587Sitojun * the following items are not implemented yet:
58862587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
58962587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
59053541Sshin */
59153541Sshinvoid
592171259Sdelphijnd6_na_input(struct mbuf *m, int off, int icmp6len)
59353541Sshin{
594183550Szec	INIT_VNET_INET6(curvnet);
59553541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
59653541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
59762587Sitojun	struct nd_neighbor_advert *nd_na;
59853541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
59962587Sitojun	struct in6_addr taddr6;
60062587Sitojun	int flags;
60162587Sitojun	int is_router;
60262587Sitojun	int is_solicited;
60362587Sitojun	int is_override;
60453541Sshin	char *lladdr = NULL;
60553541Sshin	int lladdrlen = 0;
60653541Sshin	struct ifaddr *ifa;
607186119Sqingli	struct llentry *ln = NULL;
60853541Sshin	union nd_opts ndopts;
609186119Sqingli	struct mbuf *chain = NULL;
610186119Sqingli	struct sockaddr_in6 sin6;
611165118Sbz	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
61253541Sshin
61353541Sshin	if (ip6->ip6_hlim != 255) {
61478064Sume		nd6log((LOG_ERR,
61578064Sume		    "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
616165118Sbz		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
617165118Sbz		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
61878064Sume		goto bad;
61962587Sitojun	}
62062587Sitojun
62162587Sitojun#ifndef PULLDOWN_TEST
62262587Sitojun	IP6_EXTHDR_CHECK(m, off, icmp6len,);
62362587Sitojun	nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
62462587Sitojun#else
62562587Sitojun	IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
62662587Sitojun	if (nd_na == NULL) {
627181803Sbz		V_icmp6stat.icp6s_tooshort++;
62853541Sshin		return;
62953541Sshin	}
63062587Sitojun#endif
631148385Sume
63262587Sitojun	flags = nd_na->nd_na_flags_reserved;
63362587Sitojun	is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
63462587Sitojun	is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
63562587Sitojun	is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
63653541Sshin
637148385Sume	taddr6 = nd_na->nd_na_target;
638148385Sume	if (in6_setscope(&taddr6, ifp, NULL))
639150202Ssuz		goto bad;	/* XXX: impossible */
64053541Sshin
64153541Sshin	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
64278064Sume		nd6log((LOG_ERR,
64353541Sshin		    "nd6_na_input: invalid target address %s\n",
644165118Sbz		    ip6_sprintf(ip6bufs, &taddr6)));
64578064Sume		goto bad;
64653541Sshin	}
64753541Sshin	if (IN6_IS_ADDR_MULTICAST(&daddr6))
64853541Sshin		if (is_solicited) {
64978064Sume			nd6log((LOG_ERR,
65078064Sume			    "nd6_na_input: a solicited adv is multicasted\n"));
65178064Sume			goto bad;
65253541Sshin		}
65353541Sshin
65453541Sshin	icmp6len -= sizeof(*nd_na);
65553541Sshin	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
65653541Sshin	if (nd6_options(&ndopts) < 0) {
65778064Sume		nd6log((LOG_INFO,
65878064Sume		    "nd6_na_input: invalid ND option, ignored\n"));
65978064Sume		/* nd6_options have incremented stats */
66062587Sitojun		goto freeit;
66153541Sshin	}
66253541Sshin
66353541Sshin	if (ndopts.nd_opts_tgt_lladdr) {
66453541Sshin		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
66553541Sshin		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
66653541Sshin	}
66753541Sshin
66853541Sshin	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
66953541Sshin
67053541Sshin	/*
67153541Sshin	 * Target address matches one of my interface address.
67253541Sshin	 *
67353541Sshin	 * If my address is tentative, this means that there's somebody
67453541Sshin	 * already using the same address as mine.  This indicates DAD failure.
67553541Sshin	 * This is defined in RFC 2462.
67653541Sshin	 *
67753541Sshin	 * Otherwise, process as defined in RFC 2461.
67853541Sshin	 */
67953541Sshin	if (ifa
68053541Sshin	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
68153541Sshin		nd6_dad_na_input(ifa);
68262587Sitojun		goto freeit;
68353541Sshin	}
68453541Sshin
68595023Ssuz	/* Just for safety, maybe unnecessary. */
68653541Sshin	if (ifa) {
68753541Sshin		log(LOG_ERR,
68853541Sshin		    "nd6_na_input: duplicate IP6 address %s\n",
689165118Sbz		    ip6_sprintf(ip6bufs, &taddr6));
69062587Sitojun		goto freeit;
69153541Sshin	}
69253541Sshin
69353541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
694120941Sume		nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
695165118Sbz		    "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
696120941Sume		    ifp->if_addrlen, lladdrlen - 2));
69778064Sume		goto bad;
69853541Sshin	}
69953541Sshin
70053541Sshin	/*
701120941Sume	 * If no neighbor cache entry is found, NA SHOULD silently be
702120941Sume	 * discarded.
70353541Sshin	 */
704186119Sqingli	IF_AFDATA_LOCK(ifp);
705186119Sqingli	ln = nd6_lookup(&taddr6, LLE_EXCLUSIVE, ifp);
706186119Sqingli	IF_AFDATA_UNLOCK(ifp);
707186119Sqingli	if (ln == NULL) {
70862587Sitojun		goto freeit;
709186119Sqingli	}
71053541Sshin
71153541Sshin	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
71253541Sshin		/*
71353541Sshin		 * If the link-layer has address, and no lladdr option came,
71453541Sshin		 * discard the packet.
71553541Sshin		 */
716186119Sqingli		if (ifp->if_addrlen && lladdr == NULL) {
71762587Sitojun			goto freeit;
718186119Sqingli		}
71953541Sshin
72053541Sshin		/*
72153541Sshin		 * Record link-layer address, and update the state.
72253541Sshin		 */
723186119Sqingli		bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
724186119Sqingli		ln->la_flags |= LLE_VALID;
72553541Sshin		if (is_solicited) {
72653541Sshin			ln->ln_state = ND6_LLINFO_REACHABLE;
72762587Sitojun			ln->ln_byhint = 0;
728151539Ssuz			if (!ND6_LLINFO_PERMANENT(ln)) {
729186119Sqingli				nd6_llinfo_settimer_locked(ln,
730186119Sqingli				    (long)ND_IFINFO(ln->lle_tbl->llt_ifp)->reachable * hz);
731120941Sume			}
73278064Sume		} else {
73353541Sshin			ln->ln_state = ND6_LLINFO_STALE;
734186119Sqingli			nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
73578064Sume		}
73678064Sume		if ((ln->ln_router = is_router) != 0) {
73778064Sume			/*
73878064Sume			 * This means a router's state has changed from
73978064Sume			 * non-reachable to probably reachable, and might
74078064Sume			 * affect the status of associated prefixes..
74178064Sume			 */
74278064Sume			pfxlist_onlink_check();
74378064Sume		}
74453541Sshin	} else {
74553541Sshin		int llchange;
74653541Sshin
74753541Sshin		/*
74853541Sshin		 * Check if the link-layer address has changed or not.
74953541Sshin		 */
750151465Ssuz		if (lladdr == NULL)
75153541Sshin			llchange = 0;
75253541Sshin		else {
753186119Sqingli			if (ln->la_flags & LLE_VALID) {
754186119Sqingli				if (bcmp(lladdr, &ln->ll_addr, ifp->if_addrlen))
75553541Sshin					llchange = 1;
75653541Sshin				else
75753541Sshin					llchange = 0;
75853541Sshin			} else
75953541Sshin				llchange = 1;
76053541Sshin		}
76153541Sshin
76253541Sshin		/*
76353541Sshin		 * This is VERY complex.  Look at it with care.
76453541Sshin		 *
76553541Sshin		 * override solicit lladdr llchange	action
76653541Sshin		 *					(L: record lladdr)
76753541Sshin		 *
76853541Sshin		 *	0	0	n	--	(2c)
76953541Sshin		 *	0	0	y	n	(2b) L
77053541Sshin		 *	0	0	y	y	(1)    REACHABLE->STALE
77153541Sshin		 *	0	1	n	--	(2c)   *->REACHABLE
77253541Sshin		 *	0	1	y	n	(2b) L *->REACHABLE
77353541Sshin		 *	0	1	y	y	(1)    REACHABLE->STALE
77453541Sshin		 *	1	0	n	--	(2a)
77553541Sshin		 *	1	0	y	n	(2a) L
77653541Sshin		 *	1	0	y	y	(2a) L *->STALE
77753541Sshin		 *	1	1	n	--	(2a)   *->REACHABLE
77853541Sshin		 *	1	1	y	n	(2a) L *->REACHABLE
77953541Sshin		 *	1	1	y	y	(2a) L *->REACHABLE
78053541Sshin		 */
781151539Ssuz		if (!is_override && (lladdr != NULL && llchange)) {  /* (1) */
78253541Sshin			/*
78353541Sshin			 * If state is REACHABLE, make it STALE.
78453541Sshin			 * no other updates should be done.
78553541Sshin			 */
78678064Sume			if (ln->ln_state == ND6_LLINFO_REACHABLE) {
78753541Sshin				ln->ln_state = ND6_LLINFO_STALE;
788186119Sqingli				nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
78978064Sume			}
79062587Sitojun			goto freeit;
79153541Sshin		} else if (is_override				   /* (2a) */
792151465Ssuz			|| (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
793151465Ssuz			|| lladdr == NULL) {			   /* (2c) */
79453541Sshin			/*
79553541Sshin			 * Update link-local address, if any.
79653541Sshin			 */
797151465Ssuz			if (lladdr != NULL) {
798186119Sqingli				bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
799186119Sqingli				ln->la_flags |= LLE_VALID;
80053541Sshin			}
80153541Sshin
80253541Sshin			/*
80353541Sshin			 * If solicited, make the state REACHABLE.
80453541Sshin			 * If not solicited and the link-layer address was
80553541Sshin			 * changed, make it STALE.
80653541Sshin			 */
80753541Sshin			if (is_solicited) {
80853541Sshin				ln->ln_state = ND6_LLINFO_REACHABLE;
80962587Sitojun				ln->ln_byhint = 0;
810151539Ssuz				if (!ND6_LLINFO_PERMANENT(ln)) {
811186119Sqingli					nd6_llinfo_settimer_locked(ln,
812151539Ssuz					    (long)ND_IFINFO(ifp)->reachable * hz);
81353541Sshin				}
81453541Sshin			} else {
815151465Ssuz				if (lladdr != NULL && llchange) {
81653541Sshin					ln->ln_state = ND6_LLINFO_STALE;
817186119Sqingli					nd6_llinfo_settimer_locked(ln,
818181803Sbz					    (long)V_nd6_gctimer * hz);
81978064Sume				}
82053541Sshin			}
82153541Sshin		}
82253541Sshin
82353541Sshin		if (ln->ln_router && !is_router) {
82453541Sshin			/*
82553541Sshin			 * The peer dropped the router flag.
82653541Sshin			 * Remove the sender from the Default Router List and
82753541Sshin			 * update the Destination Cache entries.
82853541Sshin			 */
82953541Sshin			struct nd_defrouter *dr;
83053541Sshin			struct in6_addr *in6;
83153541Sshin
832186119Sqingli			in6 = &L3_ADDR_SIN6(ln)->sin6_addr;
83395023Ssuz
83495023Ssuz			/*
83595023Ssuz			 * Lock to protect the default router list.
83695023Ssuz			 * XXX: this might be unnecessary, since this function
83795023Ssuz			 * is only called under the network software interrupt
838120941Sume			 * context.  However, we keep it just for safety.
83995023Ssuz			 */
840186119Sqingli			dr = defrouter_lookup(in6, ln->lle_tbl->llt_ifp);
84153541Sshin			if (dr)
84253541Sshin				defrtrlist_del(dr);
843181803Sbz			else if (!V_ip6_forwarding) {
84453541Sshin				/*
84553541Sshin				 * Even if the neighbor is not in the default
84653541Sshin				 * router list, the neighbor may be used
84753541Sshin				 * as a next hop for some destinations
84853541Sshin				 * (e.g. redirect case). So we must
84953541Sshin				 * call rt6_flush explicitly.
85053541Sshin				 */
851128422Sluigi				rt6_flush(&ip6->ip6_src, ifp);
85253541Sshin			}
85353541Sshin		}
85453541Sshin		ln->ln_router = is_router;
85553541Sshin	}
856186119Sqingli        /* XXX - QL
857186119Sqingli	 *  Does this matter?
858186119Sqingli	 *  rt->rt_flags &= ~RTF_REJECT;
859186119Sqingli	 */
860186119Sqingli	ln->la_asked = 0;
861186119Sqingli	if (ln->la_hold) {
862151539Ssuz		struct mbuf *m_hold, *m_hold_next;
863151539Ssuz
864169273Ssuz		/*
865186119Sqingli		 * reset the la_hold in advance, to explicitly
866186119Sqingli		 * prevent a la_hold lookup in nd6_output()
867169273Ssuz		 * (wouldn't happen, though...)
868169273Ssuz		 */
869186119Sqingli		for (m_hold = ln->la_hold, ln->la_hold = NULL;
870169241Ssuz		    m_hold; m_hold = m_hold_next) {
871151539Ssuz			m_hold_next = m_hold->m_nextpkt;
872169241Ssuz			m_hold->m_nextpkt = NULL;
873151539Ssuz			/*
874151539Ssuz			 * we assume ifp is not a loopback here, so just set
875151539Ssuz			 * the 2nd argument as the 1st one.
876151539Ssuz			 */
877186119Sqingli			nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain);
878151539Ssuz		}
87953541Sshin	}
880186119Sqingli freeit:
881186119Sqingli	if (ln) {
882186119Sqingli		if (chain)
883186119Sqingli			memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6));
884186119Sqingli		LLE_WUNLOCK(ln);
88562587Sitojun
886186119Sqingli		if (chain)
887186119Sqingli			nd6_output_flush(ifp, ifp, chain, &sin6, NULL);
888186119Sqingli	}
88962587Sitojun	m_freem(m);
89078064Sume	return;
89178064Sume
89278064Sume bad:
893186119Sqingli	if (ln)
894186119Sqingli		LLE_WUNLOCK(ln);
895186119Sqingli
896181803Sbz	V_icmp6stat.icp6s_badna++;
89778064Sume	m_freem(m);
89853541Sshin}
89953541Sshin
90053541Sshin/*
90153541Sshin * Neighbor advertisement output handling.
90253541Sshin *
90353541Sshin * Based on RFC 2461
90453541Sshin *
90562587Sitojun * the following items are not implemented yet:
90662587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
90762587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
908171259Sdelphij *
909171259Sdelphij * tlladdr - 1 if include target link-layer address
910171259Sdelphij * sdl0 - sockaddr_dl (= proxy NA) or NULL
91153541Sshin */
91253541Sshinvoid
913171259Sdelphijnd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
914171259Sdelphij    const struct in6_addr *taddr6, u_long flags, int tlladdr,
915171259Sdelphij    struct sockaddr *sdl0)
91653541Sshin{
917183550Szec	INIT_VNET_INET6(ifp->if_vnet);
91853541Sshin	struct mbuf *m;
91953541Sshin	struct ip6_hdr *ip6;
92053541Sshin	struct nd_neighbor_advert *nd_na;
92153541Sshin	struct ip6_moptions im6o;
922148385Sume	struct in6_addr *src, daddr6;
923148385Sume	struct sockaddr_in6 dst_sa;
924148385Sume	int icmp6len, maxlen, error;
92592733Speter	caddr_t mac = NULL;
926148385Sume	struct route_in6 ro;
92762587Sitojun
928148385Sume	bzero(&ro, sizeof(ro));
929148385Sume
930148385Sume	daddr6 = *daddr6_0;	/* make a local copy for modification */
931148385Sume
93262587Sitojun	/* estimate the size of message */
93362587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_na);
93462587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
93562587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
93662587Sitojun#ifdef DIAGNOSTIC
93762587Sitojun		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
93862587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
93962587Sitojun#endif
94053541Sshin		return;
94162587Sitojun	}
94253541Sshin
943111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
94462587Sitojun	if (m && max_linkhdr + maxlen >= MHLEN) {
945111119Simp		MCLGET(m, M_DONTWAIT);
94662587Sitojun		if ((m->m_flags & M_EXT) == 0) {
94762587Sitojun			m_free(m);
94862587Sitojun			m = NULL;
94962587Sitojun		}
95062587Sitojun	}
95162587Sitojun	if (m == NULL)
95262587Sitojun		return;
95378064Sume	m->m_pkthdr.rcvif = NULL;
95462587Sitojun
955148385Sume	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
95653541Sshin		m->m_flags |= M_MCAST;
95753541Sshin		im6o.im6o_multicast_ifp = ifp;
95853541Sshin		im6o.im6o_multicast_hlim = 255;
95953541Sshin		im6o.im6o_multicast_loop = 0;
96053541Sshin	}
96153541Sshin
96253541Sshin	icmp6len = sizeof(*nd_na);
96353541Sshin	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
96495023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
96553541Sshin
96653541Sshin	/* fill neighbor advertisement packet */
96753541Sshin	ip6 = mtod(m, struct ip6_hdr *);
96853541Sshin	ip6->ip6_flow = 0;
96962587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
97062587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
97153541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
97253541Sshin	ip6->ip6_hlim = 255;
973148385Sume	if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
97453541Sshin		/* reply to DAD */
975153227Sume		daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
976153227Sume		daddr6.s6_addr16[1] = 0;
977153227Sume		daddr6.s6_addr32[1] = 0;
978153227Sume		daddr6.s6_addr32[2] = 0;
979153227Sume		daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
980148385Sume		if (in6_setscope(&daddr6, ifp, NULL))
981148385Sume			goto bad;
982148385Sume
98353541Sshin		flags &= ~ND_NA_FLAG_SOLICITED;
984148385Sume	}
985148385Sume	ip6->ip6_dst = daddr6;
986148385Sume	bzero(&dst_sa, sizeof(struct sockaddr_in6));
987148385Sume	dst_sa.sin6_family = AF_INET6;
988148385Sume	dst_sa.sin6_len = sizeof(struct sockaddr_in6);
989148385Sume	dst_sa.sin6_addr = daddr6;
99053541Sshin
99153541Sshin	/*
99253541Sshin	 * Select a source whose scope is the same as that of the dest.
99353541Sshin	 */
994148385Sume	bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
995148385Sume	src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &error);
996148385Sume	if (src == NULL) {
997165118Sbz		char ip6buf[INET6_ADDRSTRLEN];
998148385Sume		nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
999148385Sume		    "determined: dst=%s, error=%d\n",
1000165118Sbz		    ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
1001148385Sume		goto bad;
100253541Sshin	}
1003148385Sume	ip6->ip6_src = *src;
100453541Sshin	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
100553541Sshin	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
100653541Sshin	nd_na->nd_na_code = 0;
100753541Sshin	nd_na->nd_na_target = *taddr6;
1008121315Sume	in6_clearscope(&nd_na->nd_na_target); /* XXX */
100953541Sshin
101053541Sshin	/*
101153541Sshin	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
101253541Sshin	 * see nd6_ns_input() for details.
101353541Sshin	 * Basically, if NS packet is sent to unicast/anycast addr,
101453541Sshin	 * target lladdr option SHOULD NOT be included.
101553541Sshin	 */
101662587Sitojun	if (tlladdr) {
101762587Sitojun		/*
101862587Sitojun		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
101962587Sitojun		 * lladdr in sdl0.  If we are not proxying (sending NA for
102062587Sitojun		 * my address) use lladdr configured for the interface.
102162587Sitojun		 */
1022142215Sglebius		if (sdl0 == NULL) {
1023142215Sglebius#ifdef DEV_CARP
1024142215Sglebius			if (ifp->if_carp)
1025142215Sglebius				mac = carp_macmatch6(ifp->if_carp, m, taddr6);
1026142215Sglebius			if (mac == NULL)
1027142215Sglebius				mac = nd6_ifptomac(ifp);
1028142215Sglebius#else
102962587Sitojun			mac = nd6_ifptomac(ifp);
1030142215Sglebius#endif
1031142215Sglebius		} else if (sdl0->sa_family == AF_LINK) {
103262587Sitojun			struct sockaddr_dl *sdl;
103362587Sitojun			sdl = (struct sockaddr_dl *)sdl0;
103462587Sitojun			if (sdl->sdl_alen == ifp->if_addrlen)
103562587Sitojun				mac = LLADDR(sdl);
103662587Sitojun		}
103762587Sitojun	}
103862587Sitojun	if (tlladdr && mac) {
103953541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
104053541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1041120941Sume
104253541Sshin		/* roundup to 8 bytes alignment! */
104353541Sshin		optlen = (optlen + 7) & ~7;
104453541Sshin
104553541Sshin		m->m_pkthdr.len += optlen;
104653541Sshin		m->m_len += optlen;
104753541Sshin		icmp6len += optlen;
104853541Sshin		bzero((caddr_t)nd_opt, optlen);
104953541Sshin		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
105053541Sshin		nd_opt->nd_opt_len = optlen >> 3;
105153541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
105253541Sshin	} else
105353541Sshin		flags &= ~ND_NA_FLAG_OVERRIDE;
105453541Sshin
105553541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
105653541Sshin	nd_na->nd_na_flags_reserved = flags;
105753541Sshin	nd_na->nd_na_cksum = 0;
105853541Sshin	nd_na->nd_na_cksum =
1059120941Sume	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
106053541Sshin
1061148385Sume	ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1062148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_msg);
1063148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1064181803Sbz	V_icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
1065148385Sume
1066148385Sume	if (ro.ro_rt) {		/* we don't cache this route. */
1067148385Sume		RTFREE(ro.ro_rt);
106853541Sshin	}
1069148385Sume	return;
1070148385Sume
1071148385Sume  bad:
1072148385Sume	if (ro.ro_rt) {
1073148385Sume		RTFREE(ro.ro_rt);
1074148385Sume	}
1075148385Sume	m_freem(m);
1076148385Sume	return;
107753541Sshin}
107853541Sshin
107953541Sshincaddr_t
1080171259Sdelphijnd6_ifptomac(struct ifnet *ifp)
108153541Sshin{
108253541Sshin	switch (ifp->if_type) {
108353541Sshin	case IFT_ARCNET:
108453541Sshin	case IFT_ETHER:
108553541Sshin	case IFT_FDDI:
108678064Sume	case IFT_IEEE1394:
108778468Ssumikawa#ifdef IFT_L2VLAN
108878468Ssumikawa	case IFT_L2VLAN:
108978468Ssumikawa#endif
109078064Sume#ifdef IFT_IEEE80211
109178064Sume	case IFT_IEEE80211:
109278064Sume#endif
1093142215Sglebius#ifdef IFT_CARP
1094142215Sglebius	case IFT_CARP:
1095142215Sglebius#endif
1096149829Sthompsa	case IFT_BRIDGE:
1097120049Smdodd	case IFT_ISO88025:
1098147306Sbrooks		return IF_LLADDR(ifp);
109953541Sshin	default:
110053541Sshin		return NULL;
110153541Sshin	}
110253541Sshin}
110353541Sshin
110460938SjakeTAILQ_HEAD(dadq_head, dadq);
110553541Sshinstruct dadq {
110660938Sjake	TAILQ_ENTRY(dadq) dad_list;
110753541Sshin	struct ifaddr *dad_ifa;
110853541Sshin	int dad_count;		/* max NS to send */
110962587Sitojun	int dad_ns_tcount;	/* # of trials to send NS */
111053541Sshin	int dad_ns_ocount;	/* NS sent so far */
111153541Sshin	int dad_ns_icount;
111253541Sshin	int dad_na_icount;
111378064Sume	struct callout dad_timer_ch;
111453541Sshin};
111553541Sshin
1116185088Szec#ifdef VIMAGE_GLOBALS
111753541Sshinstatic struct dadq_head dadq;
1118185088Szecint dad_init;
1119185088Szec#endif
112053541Sshin
112153541Sshinstatic struct dadq *
1122171259Sdelphijnd6_dad_find(struct ifaddr *ifa)
112353541Sshin{
1124183550Szec	INIT_VNET_INET6(curvnet);
112553541Sshin	struct dadq *dp;
112653541Sshin
1127181803Sbz	for (dp = V_dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
112853541Sshin		if (dp->dad_ifa == ifa)
112953541Sshin			return dp;
113053541Sshin	}
113153541Sshin	return NULL;
113253541Sshin}
113353541Sshin
113478064Sumestatic void
1135171259Sdelphijnd6_dad_starttimer(struct dadq *dp, int ticks)
113678064Sume{
113778064Sume
113878064Sume	callout_reset(&dp->dad_timer_ch, ticks,
1139175162Sobrien	    (void (*)(void *))nd6_dad_timer, (void *)dp->dad_ifa);
114078064Sume}
114178064Sume
114278064Sumestatic void
1143171259Sdelphijnd6_dad_stoptimer(struct dadq *dp)
114478064Sume{
114578064Sume
114678064Sume	callout_stop(&dp->dad_timer_ch);
114778064Sume}
114878064Sume
114953541Sshin/*
1150148987Sume * Start Duplicate Address Detection (DAD) for specified interface address.
115153541Sshin */
115253541Sshinvoid
1153171259Sdelphijnd6_dad_start(struct ifaddr *ifa, int delay)
115453541Sshin{
1155183550Szec	INIT_VNET_INET6(curvnet);
115653541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
115753541Sshin	struct dadq *dp;
1158165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
115953541Sshin
1160181803Sbz	if (!V_dad_init) {
1161181803Sbz		TAILQ_INIT(&V_dadq);
1162181803Sbz		V_dad_init++;
116353541Sshin	}
116453541Sshin
116553541Sshin	/*
116653541Sshin	 * If we don't need DAD, don't do it.
116753541Sshin	 * There are several cases:
116853541Sshin	 * - DAD is disabled (ip6_dad_count == 0)
116953541Sshin	 * - the interface address is anycast
117053541Sshin	 */
117153541Sshin	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
117262587Sitojun		log(LOG_DEBUG,
117362587Sitojun			"nd6_dad_start: called with non-tentative address "
117453541Sshin			"%s(%s)\n",
1175165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
117653541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
117753541Sshin		return;
117853541Sshin	}
117953541Sshin	if (ia->ia6_flags & IN6_IFF_ANYCAST) {
118053541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
118153541Sshin		return;
118253541Sshin	}
1183181803Sbz	if (!V_ip6_dad_count) {
118453541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
118553541Sshin		return;
118653541Sshin	}
1187151465Ssuz	if (ifa->ifa_ifp == NULL)
118853541Sshin		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1189120941Sume	if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
119053541Sshin		return;
1191120941Sume	}
119253541Sshin	if (nd6_dad_find(ifa) != NULL) {
119353541Sshin		/* DAD already in progress */
119453541Sshin		return;
119553541Sshin	}
119653541Sshin
119753541Sshin	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
119853541Sshin	if (dp == NULL) {
119962587Sitojun		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
120053541Sshin			"%s(%s)\n",
1201165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
120253541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
120353541Sshin		return;
120453541Sshin	}
120553541Sshin	bzero(dp, sizeof(*dp));
120678064Sume	callout_init(&dp->dad_timer_ch, 0);
1207182150Sjulian	TAILQ_INSERT_TAIL(&V_dadq, (struct dadq *)dp, dad_list);
120853541Sshin
120978064Sume	nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1210165118Sbz	    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
121153541Sshin
121253541Sshin	/*
121353541Sshin	 * Send NS packet for DAD, ip6_dad_count times.
121453541Sshin	 * Note that we must delay the first transmission, if this is the
121553541Sshin	 * first packet to be sent from the interface after interface
121653541Sshin	 * (re)initialization.
121753541Sshin	 */
121853541Sshin	dp->dad_ifa = ifa;
121995023Ssuz	IFAREF(ifa);	/* just for safety */
1220181803Sbz	dp->dad_count = V_ip6_dad_count;
122153541Sshin	dp->dad_ns_icount = dp->dad_na_icount = 0;
122262587Sitojun	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1223151539Ssuz	if (delay == 0) {
122462587Sitojun		nd6_dad_ns_output(dp, ifa);
1225121161Sume		nd6_dad_starttimer(dp,
1226151539Ssuz		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
122753541Sshin	} else {
1228151539Ssuz		nd6_dad_starttimer(dp, delay);
122953541Sshin	}
123053541Sshin}
123153541Sshin
123278064Sume/*
123378064Sume * terminate DAD unconditionally.  used for address removals.
123478064Sume */
123578064Sumevoid
1236171259Sdelphijnd6_dad_stop(struct ifaddr *ifa)
123778064Sume{
1238183550Szec	INIT_VNET_INET6(curvnet);
123978064Sume	struct dadq *dp;
124078064Sume
1241181803Sbz	if (!V_dad_init)
124278064Sume		return;
124378064Sume	dp = nd6_dad_find(ifa);
124478064Sume	if (!dp) {
124578064Sume		/* DAD wasn't started yet */
124678064Sume		return;
124778064Sume	}
124878064Sume
124978064Sume	nd6_dad_stoptimer(dp);
125078064Sume
1251181803Sbz	TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
125278064Sume	free(dp, M_IP6NDP);
125378064Sume	dp = NULL;
125478064Sume	IFAFREE(ifa);
125578064Sume}
125678064Sume
125753541Sshinstatic void
1258171259Sdelphijnd6_dad_timer(struct ifaddr *ifa)
125953541Sshin{
1260183550Szec	CURVNET_SET(dp->dad_vnet);
1261183550Szec	INIT_VNET_INET6(curvnet);
126253541Sshin	int s;
126353541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
126453541Sshin	struct dadq *dp;
1265165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
126653541Sshin
126795023Ssuz	s = splnet();		/* XXX */
126853541Sshin
126953541Sshin	/* Sanity check */
127053541Sshin	if (ia == NULL) {
127162587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
127253541Sshin		goto done;
127353541Sshin	}
127453541Sshin	dp = nd6_dad_find(ifa);
127553541Sshin	if (dp == NULL) {
127662587Sitojun		log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
127753541Sshin		goto done;
127853541Sshin	}
127953541Sshin	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
128062587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
128153541Sshin			"%s(%s)\n",
1282165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
128353541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
128453541Sshin		goto done;
128553541Sshin	}
128653541Sshin	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
128762587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
128853541Sshin			"%s(%s)\n",
1289165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
129053541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
129153541Sshin		goto done;
129253541Sshin	}
129353541Sshin
129462587Sitojun	/* timeouted with IFF_{RUNNING,UP} check */
1295181803Sbz	if (dp->dad_ns_tcount > V_dad_maxtry) {
129678064Sume		nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1297120941Sume		    if_name(ifa->ifa_ifp)));
129862587Sitojun
1299181803Sbz		TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
130062587Sitojun		free(dp, M_IP6NDP);
130162587Sitojun		dp = NULL;
130262587Sitojun		IFAFREE(ifa);
130362587Sitojun		goto done;
130462587Sitojun	}
130562587Sitojun
130653541Sshin	/* Need more checks? */
130753541Sshin	if (dp->dad_ns_ocount < dp->dad_count) {
130853541Sshin		/*
130953541Sshin		 * We have more NS to go.  Send NS packet for DAD.
131053541Sshin		 */
131162587Sitojun		nd6_dad_ns_output(dp, ifa);
1312120941Sume		nd6_dad_starttimer(dp,
1313151539Ssuz		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
131453541Sshin	} else {
131553541Sshin		/*
131653541Sshin		 * We have transmitted sufficient number of DAD packets.
131753541Sshin		 * See what we've got.
131853541Sshin		 */
131953541Sshin		int duplicate;
132053541Sshin
132153541Sshin		duplicate = 0;
132253541Sshin
132353541Sshin		if (dp->dad_na_icount) {
132453541Sshin			/*
132553541Sshin			 * the check is in nd6_dad_na_input(),
132653541Sshin			 * but just in case
132753541Sshin			 */
132853541Sshin			duplicate++;
132953541Sshin		}
133053541Sshin
133153541Sshin		if (dp->dad_ns_icount) {
133253541Sshin			/* We've seen NS, means DAD has failed. */
133353541Sshin			duplicate++;
133453541Sshin		}
133553541Sshin
133653541Sshin		if (duplicate) {
133753541Sshin			/* (*dp) will be freed in nd6_dad_duplicated() */
133853541Sshin			dp = NULL;
133953541Sshin			nd6_dad_duplicated(ifa);
134053541Sshin		} else {
134153541Sshin			/*
134253541Sshin			 * We are done with DAD.  No NA came, no NS came.
1343148987Sume			 * No duplicate address found.
134453541Sshin			 */
134553541Sshin			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
134653541Sshin
134778064Sume			nd6log((LOG_DEBUG,
134862587Sitojun			    "%s: DAD complete for %s - no duplicates found\n",
134962587Sitojun			    if_name(ifa->ifa_ifp),
1350165118Sbz			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
135153541Sshin
1352181803Sbz			TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
135353541Sshin			free(dp, M_IP6NDP);
135453541Sshin			dp = NULL;
135562587Sitojun			IFAFREE(ifa);
135653541Sshin		}
135753541Sshin	}
135853541Sshin
135953541Sshindone:
136053541Sshin	splx(s);
1361183550Szec	CURVNET_RESTORE();
136253541Sshin}
136353541Sshin
136453541Sshinvoid
1365171259Sdelphijnd6_dad_duplicated(struct ifaddr *ifa)
136653541Sshin{
1367183550Szec	INIT_VNET_INET6(curvnet);
136853541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1369151477Ssuz	struct ifnet *ifp;
137053541Sshin	struct dadq *dp;
1371165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
137253541Sshin
137353541Sshin	dp = nd6_dad_find(ifa);
137453541Sshin	if (dp == NULL) {
137562587Sitojun		log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
137653541Sshin		return;
137753541Sshin	}
137853541Sshin
137978064Sume	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
138078064Sume	    "NS in/out=%d/%d, NA in=%d\n",
1381165118Sbz	    if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
138278064Sume	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
138353541Sshin
138453541Sshin	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
138553541Sshin	ia->ia6_flags |= IN6_IFF_DUPLICATED;
138653541Sshin
1387148987Sume	/* We are done with DAD, with duplicate address found. (failure) */
138878064Sume	nd6_dad_stoptimer(dp);
138953541Sshin
1390151477Ssuz	ifp = ifa->ifa_ifp;
139162587Sitojun	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1392165118Sbz	    if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
139362587Sitojun	log(LOG_ERR, "%s: manual intervention required\n",
1394151477Ssuz	    if_name(ifp));
139553541Sshin
1396151477Ssuz	/*
1397151477Ssuz	 * If the address is a link-local address formed from an interface
1398151477Ssuz	 * identifier based on the hardware address which is supposed to be
1399151477Ssuz	 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1400151477Ssuz	 * operation on the interface SHOULD be disabled.
1401151477Ssuz	 * [rfc2462bis-03 Section 5.4.5]
1402151477Ssuz	 */
1403151477Ssuz	if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1404151477Ssuz		struct in6_addr in6;
1405151477Ssuz
1406151477Ssuz		/*
1407151477Ssuz		 * To avoid over-reaction, we only apply this logic when we are
1408151477Ssuz		 * very sure that hardware addresses are supposed to be unique.
1409151477Ssuz		 */
1410151477Ssuz		switch (ifp->if_type) {
1411151477Ssuz		case IFT_ETHER:
1412151477Ssuz		case IFT_FDDI:
1413151477Ssuz		case IFT_ATM:
1414151477Ssuz		case IFT_IEEE1394:
1415151477Ssuz#ifdef IFT_IEEE80211
1416151477Ssuz		case IFT_IEEE80211:
1417151477Ssuz#endif
1418151477Ssuz			in6 = ia->ia_addr.sin6_addr;
1419151477Ssuz			if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1420151477Ssuz			    IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1421151477Ssuz				ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1422151477Ssuz				log(LOG_ERR, "%s: possible hardware address "
1423151477Ssuz				    "duplication detected, disable IPv6\n",
1424151477Ssuz				    if_name(ifp));
1425151477Ssuz			}
1426151477Ssuz			break;
1427151477Ssuz		}
1428151477Ssuz	}
1429151477Ssuz
1430181803Sbz	TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
143153541Sshin	free(dp, M_IP6NDP);
143253541Sshin	dp = NULL;
143362587Sitojun	IFAFREE(ifa);
143453541Sshin}
143553541Sshin
143662587Sitojunstatic void
1437171259Sdelphijnd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
143862587Sitojun{
143962587Sitojun	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
144062587Sitojun	struct ifnet *ifp = ifa->ifa_ifp;
144162587Sitojun
144262587Sitojun	dp->dad_ns_tcount++;
144362587Sitojun	if ((ifp->if_flags & IFF_UP) == 0) {
144462587Sitojun		return;
144562587Sitojun	}
1446148887Srwatson	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
144762587Sitojun		return;
144862587Sitojun	}
144962587Sitojun
145062587Sitojun	dp->dad_ns_ocount++;
145162587Sitojun	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
145262587Sitojun}
145362587Sitojun
145462587Sitojunstatic void
1455171259Sdelphijnd6_dad_ns_input(struct ifaddr *ifa)
145653541Sshin{
1457183550Szec	INIT_VNET_INET6(curvnet);
145853541Sshin	struct in6_ifaddr *ia;
145953541Sshin	struct ifnet *ifp;
146078064Sume	const struct in6_addr *taddr6;
146153541Sshin	struct dadq *dp;
146253541Sshin	int duplicate;
146353541Sshin
1464151465Ssuz	if (ifa == NULL)
146553541Sshin		panic("ifa == NULL in nd6_dad_ns_input");
146653541Sshin
146753541Sshin	ia = (struct in6_ifaddr *)ifa;
146853541Sshin	ifp = ifa->ifa_ifp;
146953541Sshin	taddr6 = &ia->ia_addr.sin6_addr;
147053541Sshin	duplicate = 0;
147153541Sshin	dp = nd6_dad_find(ifa);
147253541Sshin
147353541Sshin	/* Quickhack - completely ignore DAD NS packets */
1474181803Sbz	if (V_dad_ignore_ns) {
1475165118Sbz		char ip6buf[INET6_ADDRSTRLEN];
147678064Sume		nd6log((LOG_INFO,
147778064Sume		    "nd6_dad_ns_input: ignoring DAD NS packet for "
1478165118Sbz		    "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6),
147978064Sume		    if_name(ifa->ifa_ifp)));
148053541Sshin		return;
148153541Sshin	}
148253541Sshin
148353541Sshin	/*
148453541Sshin	 * if I'm yet to start DAD, someone else started using this address
148553541Sshin	 * first.  I have a duplicate and you win.
148653541Sshin	 */
1487151465Ssuz	if (dp == NULL || dp->dad_ns_ocount == 0)
148853541Sshin		duplicate++;
148953541Sshin
149053541Sshin	/* XXX more checks for loopback situation - see nd6_dad_timer too */
149153541Sshin
149253541Sshin	if (duplicate) {
149353541Sshin		dp = NULL;	/* will be freed in nd6_dad_duplicated() */
149453541Sshin		nd6_dad_duplicated(ifa);
149553541Sshin	} else {
149653541Sshin		/*
149753541Sshin		 * not sure if I got a duplicate.
149853541Sshin		 * increment ns count and see what happens.
149953541Sshin		 */
150053541Sshin		if (dp)
150153541Sshin			dp->dad_ns_icount++;
150253541Sshin	}
150353541Sshin}
150453541Sshin
150562587Sitojunstatic void
1506171259Sdelphijnd6_dad_na_input(struct ifaddr *ifa)
150753541Sshin{
150853541Sshin	struct dadq *dp;
150953541Sshin
1510151465Ssuz	if (ifa == NULL)
151153541Sshin		panic("ifa == NULL in nd6_dad_na_input");
151253541Sshin
151353541Sshin	dp = nd6_dad_find(ifa);
151453541Sshin	if (dp)
151553541Sshin		dp->dad_na_icount++;
151653541Sshin
151753541Sshin	/* remove the address. */
151853541Sshin	nd6_dad_duplicated(ifa);
151953541Sshin}
1520