nd6_nbr.c revision 216022
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 216022 2010-11-29 00:04:08Z bz $");
34174510Sobrien
3562587Sitojun#include "opt_inet.h"
3662587Sitojun#include "opt_inet6.h"
37142215Sglebius#include "opt_ipsec.h"
38178167Sqingli#include "opt_mpath.h"
3955009Sshin
4053541Sshin#include <sys/param.h>
4153541Sshin#include <sys/systm.h>
4253541Sshin#include <sys/malloc.h>
43186119Sqingli#include <sys/lock.h>
44186119Sqingli#include <sys/rwlock.h>
4553541Sshin#include <sys/mbuf.h>
4653541Sshin#include <sys/socket.h>
4753541Sshin#include <sys/sockio.h>
4853541Sshin#include <sys/time.h>
4953541Sshin#include <sys/kernel.h>
5053541Sshin#include <sys/errno.h>
5153541Sshin#include <sys/syslog.h>
5253541Sshin#include <sys/queue.h>
5378064Sume#include <sys/callout.h>
5453541Sshin
5553541Sshin#include <net/if.h>
5653541Sshin#include <net/if_types.h>
5753541Sshin#include <net/if_dl.h>
58147306Sbrooks#include <net/if_var.h>
5953541Sshin#include <net/route.h>
60178167Sqingli#ifdef RADIX_MPATH
61178167Sqingli#include <net/radix_mpath.h>
62178167Sqingli#endif
6353541Sshin
6453541Sshin#include <netinet/in.h>
6553541Sshin#include <netinet/in_var.h>
66186119Sqingli#include <net/if_llatbl.h>
67186119Sqingli#define	L3_ADDR_SIN6(le)	((struct sockaddr_in6 *) L3_ADDR(le))
6853541Sshin#include <netinet6/in6_var.h>
69151477Ssuz#include <netinet6/in6_ifattach.h>
7062587Sitojun#include <netinet/ip6.h>
7153541Sshin#include <netinet6/ip6_var.h>
72148385Sume#include <netinet6/scope6_var.h>
7353541Sshin#include <netinet6/nd6.h>
7462587Sitojun#include <netinet/icmp6.h>
75211193Swill#include <netinet/ip_carp.h>
76211501Sanchie#include <netinet6/send.h>
7753541Sshin
7862587Sitojun#define SDL(s) ((struct sockaddr_dl *)s)
7953541Sshin
8062587Sitojunstruct dadq;
81175162Sobrienstatic struct dadq *nd6_dad_find(struct ifaddr *);
82175162Sobrienstatic void nd6_dad_starttimer(struct dadq *, int);
83175162Sobrienstatic void nd6_dad_stoptimer(struct dadq *);
84191816Szecstatic void nd6_dad_timer(struct dadq *);
85175162Sobrienstatic void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
86175162Sobrienstatic void nd6_dad_ns_input(struct ifaddr *);
87175162Sobrienstatic void nd6_dad_na_input(struct ifaddr *);
8853541Sshin
89207369SbzVNET_DEFINE(int, dad_ignore_ns) = 0;	/* ignore NS in DAD - specwise incorrect*/
90207369SbzVNET_DEFINE(int, dad_maxtry) = 15;	/* max # of *tries* to transmit DAD packet */
91195727Srwatson#define	V_dad_ignore_ns			VNET(dad_ignore_ns)
92195727Srwatson#define	V_dad_maxtry			VNET(dad_maxtry)
93195699Srwatson
9453541Sshin/*
95108470Sschweikh * Input a Neighbor Solicitation Message.
9653541Sshin *
9753541Sshin * Based on RFC 2461
98148987Sume * Based on RFC 2462 (duplicate address detection)
9953541Sshin */
10053541Sshinvoid
101171259Sdelphijnd6_ns_input(struct mbuf *m, int off, int icmp6len)
10253541Sshin{
10353541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
10453541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
10562587Sitojun	struct nd_neighbor_solicit *nd_ns;
10653541Sshin	struct in6_addr saddr6 = ip6->ip6_src;
10753541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
10862587Sitojun	struct in6_addr taddr6;
10953541Sshin	struct in6_addr myaddr6;
11053541Sshin	char *lladdr = NULL;
111142215Sglebius	struct ifaddr *ifa = NULL;
11253541Sshin	int lladdrlen = 0;
11353541Sshin	int anycast = 0, proxy = 0, tentative = 0;
11453541Sshin	int tlladdr;
11553541Sshin	union nd_opts ndopts;
11662587Sitojun	struct sockaddr_dl *proxydl = NULL;
117165118Sbz	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
11853541Sshin
11978064Sume#ifndef PULLDOWN_TEST
12078064Sume	IP6_EXTHDR_CHECK(m, off, icmp6len,);
12178064Sume	nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
12278064Sume#else
12378064Sume	IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
12478064Sume	if (nd_ns == NULL) {
125190964Srwatson		ICMP6STAT_INC(icp6s_tooshort);
12678064Sume		return;
12778064Sume	}
12878064Sume#endif
12978064Sume	ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
13078064Sume	taddr6 = nd_ns->nd_ns_target;
131148385Sume	if (in6_setscope(&taddr6, ifp, NULL) != 0)
132148385Sume		goto bad;
13378064Sume
13453541Sshin	if (ip6->ip6_hlim != 255) {
13578064Sume		nd6log((LOG_ERR,
13678064Sume		    "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
137165118Sbz		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
138165118Sbz		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
13978064Sume		goto bad;
14053541Sshin	}
14153541Sshin
14253541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
143148987Sume		/* dst has to be a solicited node multicast address. */
144120941Sume		if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
14595023Ssuz		    /* don't check ifindex portion */
146120941Sume		    daddr6.s6_addr32[1] == 0 &&
147120941Sume		    daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
148120941Sume		    daddr6.s6_addr8[12] == 0xff) {
14995023Ssuz			; /* good */
15053541Sshin		} else {
15178064Sume			nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
152120941Sume			    "(wrong ip6 dst)\n"));
15353541Sshin			goto bad;
15453541Sshin		}
155185348Szec	} else if (!V_nd6_onlink_ns_rfc4861) {
156183529Scperciva		struct sockaddr_in6 src_sa6;
157183529Scperciva
158183529Scperciva		/*
159183529Scperciva		 * According to recent IETF discussions, it is not a good idea
160183529Scperciva		 * to accept a NS from an address which would not be deemed
161183529Scperciva		 * to be a neighbor otherwise.  This point is expected to be
162183529Scperciva		 * clarified in future revisions of the specification.
163183529Scperciva		 */
164183529Scperciva		bzero(&src_sa6, sizeof(src_sa6));
165183529Scperciva		src_sa6.sin6_family = AF_INET6;
166183529Scperciva		src_sa6.sin6_len = sizeof(src_sa6);
167183529Scperciva		src_sa6.sin6_addr = saddr6;
168186119Sqingli		if (nd6_is_addr_neighbor(&src_sa6, ifp) == 0) {
169183529Scperciva			nd6log((LOG_INFO, "nd6_ns_input: "
170183529Scperciva				"NS packet from non-neighbor\n"));
171183529Scperciva			goto bad;
172183529Scperciva		}
17353541Sshin	}
17453541Sshin
17553541Sshin	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
17678064Sume		nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
17753541Sshin		goto bad;
17853541Sshin	}
17953541Sshin
18053541Sshin	icmp6len -= sizeof(*nd_ns);
18153541Sshin	nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
18253541Sshin	if (nd6_options(&ndopts) < 0) {
18378064Sume		nd6log((LOG_INFO,
18478064Sume		    "nd6_ns_input: invalid ND option, ignored\n"));
18578064Sume		/* nd6_options have incremented stats */
18678064Sume		goto freeit;
18753541Sshin	}
18853541Sshin
18953541Sshin	if (ndopts.nd_opts_src_lladdr) {
19095023Ssuz		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
19153541Sshin		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
19253541Sshin	}
193120941Sume
19453541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
19578064Sume		nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
19678064Sume		    "(link-layer address option)\n"));
19753541Sshin		goto bad;
19853541Sshin	}
19953541Sshin
20053541Sshin	/*
20153541Sshin	 * Attaching target link-layer address to the NA?
20253541Sshin	 * (RFC 2461 7.2.4)
20353541Sshin	 *
20453541Sshin	 * NS IP dst is unicast/anycast			MUST NOT add
20553541Sshin	 * NS IP dst is solicited-node multicast	MUST add
20653541Sshin	 *
20753541Sshin	 * In implementation, we add target link-layer address by default.
20853541Sshin	 * We do not add one in MUST NOT cases.
20953541Sshin	 */
21053541Sshin	if (!IN6_IS_ADDR_MULTICAST(&daddr6))
21153541Sshin		tlladdr = 0;
21253541Sshin	else
21353541Sshin		tlladdr = 1;
21453541Sshin
21553541Sshin	/*
21653541Sshin	 * Target address (taddr6) must be either:
21753541Sshin	 * (1) Valid unicast/anycast address for my receiving interface,
21853541Sshin	 * (2) Unicast address for which I'm offering proxy service, or
21953541Sshin	 * (3) "tentative" address on which DAD is being performed.
22053541Sshin	 */
22153541Sshin	/* (1) and (3) check. */
222142215Sglebius	if (ifp->if_carp)
223211157Swill		ifa = (*carp_iamatch6_p)(ifp, &taddr6);
224151465Ssuz	if (ifa == NULL)
225142215Sglebius		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
22653541Sshin
22753541Sshin	/* (2) check. */
228151465Ssuz	if (ifa == NULL) {
22953541Sshin		struct rtentry *rt;
23053541Sshin		struct sockaddr_in6 tsin6;
231121765Ssam		int need_proxy;
232178167Sqingli#ifdef RADIX_MPATH
233178167Sqingli		struct route_in6 ro;
234178167Sqingli#endif
23553541Sshin
236171260Sdelphij		bzero(&tsin6, sizeof tsin6);
23753541Sshin		tsin6.sin6_len = sizeof(struct sockaddr_in6);
23853541Sshin		tsin6.sin6_family = AF_INET6;
23953541Sshin		tsin6.sin6_addr = taddr6;
24053541Sshin
241178167Sqingli#ifdef RADIX_MPATH
242178167Sqingli		bzero(&ro, sizeof(ro));
243178167Sqingli		ro.ro_dst = tsin6;
244178167Sqingli		rtalloc_mpath((struct route *)&ro, RTF_ANNOUNCE);
245178167Sqingli		rt = ro.ro_rt;
246178167Sqingli#else
24753541Sshin		rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
248178167Sqingli#endif
249121765Ssam		need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
250121765Ssam		    rt->rt_gateway->sa_family == AF_LINK);
251121765Ssam		if (rt)
252187946Sbz			RTFREE_LOCKED(rt);
253121765Ssam		if (need_proxy) {
25453541Sshin			/*
25562587Sitojun			 * proxy NDP for single entry
25653541Sshin			 */
25762587Sitojun			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
25862587Sitojun				IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
25962587Sitojun			if (ifa) {
26053541Sshin				proxy = 1;
26162587Sitojun				proxydl = SDL(rt->rt_gateway);
26262587Sitojun			}
26353541Sshin		}
26453541Sshin	}
265151479Ssuz	if (ifa == NULL) {
26653541Sshin		/*
26778064Sume		 * We've got an NS packet, and we don't have that adddress
26853541Sshin		 * assigned for us.  We MUST silently ignore it.
26953541Sshin		 * See RFC2461 7.2.3.
27053541Sshin		 */
27162587Sitojun		goto freeit;
27253541Sshin	}
27353541Sshin	myaddr6 = *IFA_IN6(ifa);
27453541Sshin	anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
27553541Sshin	tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
27653541Sshin	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
27762587Sitojun		goto freeit;
27853541Sshin
27953541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
280120941Sume		nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
28153541Sshin		    "(if %d, NS packet %d)\n",
282165118Sbz		    ip6_sprintf(ip6bufs, &taddr6),
283120941Sume		    ifp->if_addrlen, lladdrlen - 2));
28478064Sume		goto bad;
28553541Sshin	}
28653541Sshin
28753541Sshin	if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
288120941Sume		nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
289165118Sbz		    ip6_sprintf(ip6bufs, &saddr6)));
29062587Sitojun		goto freeit;
29153541Sshin	}
29253541Sshin
29353541Sshin	/*
29453541Sshin	 * We have neighbor solicitation packet, with target address equals to
29553541Sshin	 * one of my tentative address.
29653541Sshin	 *
29753541Sshin	 * src addr	how to process?
29853541Sshin	 * ---		---
29953541Sshin	 * multicast	of course, invalid (rejected in ip6_input)
30053541Sshin	 * unicast	somebody is doing address resolution -> ignore
30153541Sshin	 * unspec	dup address detection
30253541Sshin	 *
30353541Sshin	 * The processing is defined in RFC 2462.
30453541Sshin	 */
30553541Sshin	if (tentative) {
30653541Sshin		/*
30753541Sshin		 * If source address is unspecified address, it is for
308148987Sume		 * duplicate address detection.
30953541Sshin		 *
31053541Sshin		 * If not, the packet is for addess resolution;
31153541Sshin		 * silently ignore it.
31253541Sshin		 */
31353541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
31453541Sshin			nd6_dad_ns_input(ifa);
31553541Sshin
31662587Sitojun		goto freeit;
31753541Sshin	}
31853541Sshin
31953541Sshin	/*
32053541Sshin	 * If the source address is unspecified address, entries must not
32153541Sshin	 * be created or updated.
32253541Sshin	 * It looks that sender is performing DAD.  Output NA toward
32353541Sshin	 * all-node multicast address, to tell the sender that I'm using
32453541Sshin	 * the address.
32553541Sshin	 * S bit ("solicited") must be zero.
32653541Sshin	 */
32753541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
328148385Sume		struct in6_addr in6_all;
329148385Sume
330148385Sume		in6_all = in6addr_linklocal_allnodes;
331148385Sume		if (in6_setscope(&in6_all, ifp, NULL) != 0)
332148385Sume			goto bad;
333148385Sume		nd6_na_output(ifp, &in6_all, &taddr6,
334120941Sume		    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
335181803Sbz		    (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
336120941Sume		    tlladdr, (struct sockaddr *)proxydl);
33762587Sitojun		goto freeit;
33853541Sshin	}
33953541Sshin
340120941Sume	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
341120941Sume	    ND_NEIGHBOR_SOLICIT, 0);
34253541Sshin
34353541Sshin	nd6_na_output(ifp, &saddr6, &taddr6,
344120941Sume	    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
345181803Sbz	    (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
346120941Sume	    tlladdr, (struct sockaddr *)proxydl);
34762587Sitojun freeit:
348194760Srwatson	if (ifa != NULL)
349194760Srwatson		ifa_free(ifa);
35062587Sitojun	m_freem(m);
35153541Sshin	return;
35253541Sshin
35353541Sshin bad:
354165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
355165118Sbz		ip6_sprintf(ip6bufs, &saddr6)));
356165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
357165118Sbz		ip6_sprintf(ip6bufs, &daddr6)));
358165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
359165118Sbz		ip6_sprintf(ip6bufs, &taddr6)));
360190964Srwatson	ICMP6STAT_INC(icp6s_badns);
361194760Srwatson	if (ifa != NULL)
362194760Srwatson		ifa_free(ifa);
36362587Sitojun	m_freem(m);
36453541Sshin}
36553541Sshin
36653541Sshin/*
367108470Sschweikh * Output a Neighbor Solicitation Message. Caller specifies:
36853541Sshin *	- ICMP6 header source IP6 address
36953541Sshin *	- ND6 header target IP6 address
37053541Sshin *	- ND6 header source datalink address
37153541Sshin *
37253541Sshin * Based on RFC 2461
373148987Sume * Based on RFC 2462 (duplicate address detection)
374171259Sdelphij *
375171259Sdelphij *   ln - for source address determination
376171259Sdelphij *  dad - duplicate address detection
37753541Sshin */
37853541Sshinvoid
379186119Sqinglind6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
380186119Sqingli    const struct in6_addr *taddr6, struct llentry *ln, int dad)
38153541Sshin{
38253541Sshin	struct mbuf *m;
383211501Sanchie	struct m_tag *mtag;
38453541Sshin	struct ip6_hdr *ip6;
38553541Sshin	struct nd_neighbor_solicit *nd_ns;
38653541Sshin	struct ip6_moptions im6o;
38753541Sshin	int icmp6len;
38862587Sitojun	int maxlen;
38953541Sshin	caddr_t mac;
390148385Sume	struct route_in6 ro;
391120941Sume
39253541Sshin	if (IN6_IS_ADDR_MULTICAST(taddr6))
39353541Sshin		return;
39453541Sshin
39562587Sitojun	/* estimate the size of message */
39662587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
39762587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
39862587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
39962587Sitojun#ifdef DIAGNOSTIC
40062587Sitojun		printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
40162587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
40262587Sitojun#endif
40353541Sshin		return;
40462587Sitojun	}
40553541Sshin
406111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
40762587Sitojun	if (m && max_linkhdr + maxlen >= MHLEN) {
408111119Simp		MCLGET(m, M_DONTWAIT);
40962587Sitojun		if ((m->m_flags & M_EXT) == 0) {
41062587Sitojun			m_free(m);
41162587Sitojun			m = NULL;
41262587Sitojun		}
41362587Sitojun	}
41462587Sitojun	if (m == NULL)
41562587Sitojun		return;
41678064Sume	m->m_pkthdr.rcvif = NULL;
41762587Sitojun
418215559Sbz	bzero(&ro, sizeof(ro));
419215559Sbz
42053541Sshin	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
42153541Sshin		m->m_flags |= M_MCAST;
42253541Sshin		im6o.im6o_multicast_ifp = ifp;
42353541Sshin		im6o.im6o_multicast_hlim = 255;
42453541Sshin		im6o.im6o_multicast_loop = 0;
42553541Sshin	}
42653541Sshin
42753541Sshin	icmp6len = sizeof(*nd_ns);
42853541Sshin	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
42995023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
43053541Sshin
43153541Sshin	/* fill neighbor solicitation packet */
43253541Sshin	ip6 = mtod(m, struct ip6_hdr *);
43353541Sshin	ip6->ip6_flow = 0;
43462587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
43562587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
43653541Sshin	/* ip6->ip6_plen will be set later */
43753541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
43853541Sshin	ip6->ip6_hlim = 255;
43953541Sshin	if (daddr6)
44053541Sshin		ip6->ip6_dst = *daddr6;
44153541Sshin	else {
44253541Sshin		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
443148385Sume		ip6->ip6_dst.s6_addr16[1] = 0;
44453541Sshin		ip6->ip6_dst.s6_addr32[1] = 0;
44553541Sshin		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
44653541Sshin		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
44753541Sshin		ip6->ip6_dst.s6_addr8[12] = 0xff;
448148385Sume		if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
449148385Sume			goto bad;
45053541Sshin	}
45153541Sshin	if (!dad) {
452194760Srwatson		struct ifaddr *ifa;
453194760Srwatson
45453541Sshin		/*
45553541Sshin		 * RFC2461 7.2.2:
45653541Sshin		 * "If the source address of the packet prompting the
45753541Sshin		 * solicitation is the same as one of the addresses assigned
45853541Sshin		 * to the outgoing interface, that address SHOULD be placed
45953541Sshin		 * in the IP Source Address of the outgoing solicitation.
46053541Sshin		 * Otherwise, any one of the addresses assigned to the
46153541Sshin		 * interface should be used."
46253541Sshin		 *
46353541Sshin		 * We use the source address for the prompting packet
46453541Sshin		 * (saddr6), if:
46553541Sshin		 * - saddr6 is given from the caller (by giving "ln"), and
46653541Sshin		 * - saddr6 belongs to the outgoing interface.
467148385Sume		 * Otherwise, we perform the source address selection as usual.
46853541Sshin		 */
469216022Sbz		struct in6_addr *hsrc;
47053541Sshin
471216022Sbz		hsrc = NULL;
472216022Sbz		if (ln != NULL) {
473216022Sbz			LLE_RLOCK(ln);
474216022Sbz			if (ln->la_hold != NULL) {
475216022Sbz				struct ip6_hdr *hip6;		/* hold ip6 */
476216022Sbz
477216022Sbz				/*
478216022Sbz				 * assuming every packet in la_hold has the same IP
479216022Sbz				 * header
480216022Sbz				 */
481216022Sbz				hip6 = mtod(ln->la_hold, struct ip6_hdr *);
482216022Sbz				/* XXX pullup? */
483216022Sbz				if (sizeof(*hip6) < ln->la_hold->m_len) {
484216022Sbz					ip6->ip6_src = hip6->ip6_src;
485216022Sbz					hsrc = &hip6->ip6_src;
486216022Sbz				}
487216022Sbz			}
488216022Sbz			LLE_RUNLOCK(ln);
489148385Sume		}
490194760Srwatson		if (hsrc && (ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
491194760Srwatson		    hsrc)) != NULL) {
492216022Sbz			/* ip6_src set already. */
493194760Srwatson			ifa_free(ifa);
494194760Srwatson		} else {
495148385Sume			int error;
496148385Sume			struct sockaddr_in6 dst_sa;
497216022Sbz			struct in6_addr src_in;
498148385Sume
499148385Sume			bzero(&dst_sa, sizeof(dst_sa));
500148385Sume			dst_sa.sin6_family = AF_INET6;
501148385Sume			dst_sa.sin6_len = sizeof(dst_sa);
502148385Sume			dst_sa.sin6_addr = ip6->ip6_dst;
503148385Sume
504194777Sbz			error = in6_selectsrc(&dst_sa, NULL,
505194777Sbz			    NULL, &ro, NULL, NULL, &src_in);
506194777Sbz			if (error) {
507165118Sbz				char ip6buf[INET6_ADDRSTRLEN];
508148385Sume				nd6log((LOG_DEBUG,
509148385Sume				    "nd6_ns_output: source can't be "
510148385Sume				    "determined: dst=%s, error=%d\n",
511165118Sbz				    ip6_sprintf(ip6buf, &dst_sa.sin6_addr),
512165118Sbz				    error));
513148385Sume				goto bad;
51453541Sshin			}
515216022Sbz			ip6->ip6_src = src_in;
51653541Sshin		}
51753541Sshin	} else {
51853541Sshin		/*
51953541Sshin		 * Source address for DAD packet must always be IPv6
52053541Sshin		 * unspecified address. (0::0)
521148385Sume		 * We actually don't have to 0-clear the address (we did it
522148385Sume		 * above), but we do so here explicitly to make the intention
523148385Sume		 * clearer.
52453541Sshin		 */
525216022Sbz		bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
52653541Sshin	}
52753541Sshin	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
52853541Sshin	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
52953541Sshin	nd_ns->nd_ns_code = 0;
53053541Sshin	nd_ns->nd_ns_reserved = 0;
53153541Sshin	nd_ns->nd_ns_target = *taddr6;
532121315Sume	in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
53353541Sshin
53453541Sshin	/*
53553541Sshin	 * Add source link-layer address option.
53653541Sshin	 *
53753541Sshin	 *				spec		implementation
53853541Sshin	 *				---		---
53953541Sshin	 * DAD packet			MUST NOT	do not add the option
54053541Sshin	 * there's no link layer address:
54153541Sshin	 *				impossible	do not add the option
54253541Sshin	 * there's link layer address:
54353541Sshin	 *	Multicast NS		MUST add one	add the option
54453541Sshin	 *	Unicast NS		SHOULD add one	add the option
54553541Sshin	 */
54653541Sshin	if (!dad && (mac = nd6_ifptomac(ifp))) {
54753541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
54853541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
54953541Sshin		/* 8 byte alignments... */
55053541Sshin		optlen = (optlen + 7) & ~7;
551120941Sume
55253541Sshin		m->m_pkthdr.len += optlen;
55353541Sshin		m->m_len += optlen;
55453541Sshin		icmp6len += optlen;
55553541Sshin		bzero((caddr_t)nd_opt, optlen);
55653541Sshin		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
55753541Sshin		nd_opt->nd_opt_len = optlen >> 3;
55853541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
55953541Sshin	}
56053541Sshin
56153541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
56253541Sshin	nd_ns->nd_ns_cksum = 0;
563120941Sume	nd_ns->nd_ns_cksum =
564120941Sume	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
56553541Sshin
566211501Sanchie	if (send_sendso_input_hook != NULL) {
567211501Sanchie		mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
568211501Sanchie			sizeof(unsigned short), M_NOWAIT);
569211501Sanchie		if (mtag == NULL)
570211501Sanchie			goto bad;
571211501Sanchie		*(unsigned short *)(mtag + 1) = nd_ns->nd_ns_type;
572211501Sanchie		m_tag_prepend(m, mtag);
573211501Sanchie	}
574211501Sanchie
575151536Ssuz	ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
576148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_msg);
577148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
578190964Srwatson	ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_SOLICIT]);
579148385Sume
580148385Sume	if (ro.ro_rt) {		/* we don't cache this route. */
581148385Sume		RTFREE(ro.ro_rt);
58253541Sshin	}
583148385Sume	return;
584148385Sume
585148385Sume  bad:
586148385Sume	if (ro.ro_rt) {
587148385Sume		RTFREE(ro.ro_rt);
588148385Sume	}
589148385Sume	m_freem(m);
590148385Sume	return;
59153541Sshin}
59253541Sshin
59353541Sshin/*
59453541Sshin * Neighbor advertisement input handling.
59553541Sshin *
59653541Sshin * Based on RFC 2461
597148987Sume * Based on RFC 2462 (duplicate address detection)
59862587Sitojun *
59962587Sitojun * the following items are not implemented yet:
60062587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
60162587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
60253541Sshin */
60353541Sshinvoid
604171259Sdelphijnd6_na_input(struct mbuf *m, int off, int icmp6len)
60553541Sshin{
60653541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
60753541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
60862587Sitojun	struct nd_neighbor_advert *nd_na;
60953541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
61062587Sitojun	struct in6_addr taddr6;
61162587Sitojun	int flags;
61262587Sitojun	int is_router;
61362587Sitojun	int is_solicited;
61462587Sitojun	int is_override;
61553541Sshin	char *lladdr = NULL;
61653541Sshin	int lladdrlen = 0;
617186468Skmacy	int checklink = 0;
61853541Sshin	struct ifaddr *ifa;
619186119Sqingli	struct llentry *ln = NULL;
62053541Sshin	union nd_opts ndopts;
621186119Sqingli	struct mbuf *chain = NULL;
622211501Sanchie	struct m_tag *mtag;
623186119Sqingli	struct sockaddr_in6 sin6;
624165118Sbz	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
62553541Sshin
62653541Sshin	if (ip6->ip6_hlim != 255) {
62778064Sume		nd6log((LOG_ERR,
62878064Sume		    "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
629165118Sbz		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
630165118Sbz		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
63178064Sume		goto bad;
63262587Sitojun	}
63362587Sitojun
63462587Sitojun#ifndef PULLDOWN_TEST
63562587Sitojun	IP6_EXTHDR_CHECK(m, off, icmp6len,);
63662587Sitojun	nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
63762587Sitojun#else
63862587Sitojun	IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
63962587Sitojun	if (nd_na == NULL) {
640190964Srwatson		ICMP6STAT_INC(icp6s_tooshort);
64153541Sshin		return;
64253541Sshin	}
64362587Sitojun#endif
644148385Sume
64562587Sitojun	flags = nd_na->nd_na_flags_reserved;
64662587Sitojun	is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
64762587Sitojun	is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
64862587Sitojun	is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
64953541Sshin
650148385Sume	taddr6 = nd_na->nd_na_target;
651148385Sume	if (in6_setscope(&taddr6, ifp, NULL))
652150202Ssuz		goto bad;	/* XXX: impossible */
65353541Sshin
65453541Sshin	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
65578064Sume		nd6log((LOG_ERR,
65653541Sshin		    "nd6_na_input: invalid target address %s\n",
657165118Sbz		    ip6_sprintf(ip6bufs, &taddr6)));
65878064Sume		goto bad;
65953541Sshin	}
66053541Sshin	if (IN6_IS_ADDR_MULTICAST(&daddr6))
66153541Sshin		if (is_solicited) {
66278064Sume			nd6log((LOG_ERR,
66378064Sume			    "nd6_na_input: a solicited adv is multicasted\n"));
66478064Sume			goto bad;
66553541Sshin		}
66653541Sshin
66753541Sshin	icmp6len -= sizeof(*nd_na);
66853541Sshin	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
66953541Sshin	if (nd6_options(&ndopts) < 0) {
67078064Sume		nd6log((LOG_INFO,
67178064Sume		    "nd6_na_input: invalid ND option, ignored\n"));
67278064Sume		/* nd6_options have incremented stats */
67362587Sitojun		goto freeit;
67453541Sshin	}
67553541Sshin
67653541Sshin	if (ndopts.nd_opts_tgt_lladdr) {
67753541Sshin		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
67853541Sshin		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
67953541Sshin	}
68053541Sshin
68153541Sshin	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
68253541Sshin
68353541Sshin	/*
68453541Sshin	 * Target address matches one of my interface address.
68553541Sshin	 *
68653541Sshin	 * If my address is tentative, this means that there's somebody
68753541Sshin	 * already using the same address as mine.  This indicates DAD failure.
68853541Sshin	 * This is defined in RFC 2462.
68953541Sshin	 *
69053541Sshin	 * Otherwise, process as defined in RFC 2461.
69153541Sshin	 */
69253541Sshin	if (ifa
69353541Sshin	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
694194760Srwatson		ifa_free(ifa);
69553541Sshin		nd6_dad_na_input(ifa);
69662587Sitojun		goto freeit;
69753541Sshin	}
69853541Sshin
69995023Ssuz	/* Just for safety, maybe unnecessary. */
70053541Sshin	if (ifa) {
701194760Srwatson		ifa_free(ifa);
70253541Sshin		log(LOG_ERR,
70353541Sshin		    "nd6_na_input: duplicate IP6 address %s\n",
704165118Sbz		    ip6_sprintf(ip6bufs, &taddr6));
70562587Sitojun		goto freeit;
70653541Sshin	}
70753541Sshin
70853541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
709120941Sume		nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
710165118Sbz		    "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
711120941Sume		    ifp->if_addrlen, lladdrlen - 2));
71278064Sume		goto bad;
71353541Sshin	}
71453541Sshin
71553541Sshin	/*
716120941Sume	 * If no neighbor cache entry is found, NA SHOULD silently be
717120941Sume	 * discarded.
71853541Sshin	 */
719186119Sqingli	IF_AFDATA_LOCK(ifp);
720186119Sqingli	ln = nd6_lookup(&taddr6, LLE_EXCLUSIVE, ifp);
721186119Sqingli	IF_AFDATA_UNLOCK(ifp);
722186119Sqingli	if (ln == NULL) {
72362587Sitojun		goto freeit;
724186119Sqingli	}
72553541Sshin
72653541Sshin	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
72753541Sshin		/*
72853541Sshin		 * If the link-layer has address, and no lladdr option came,
72953541Sshin		 * discard the packet.
73053541Sshin		 */
731186119Sqingli		if (ifp->if_addrlen && lladdr == NULL) {
73262587Sitojun			goto freeit;
733186119Sqingli		}
73453541Sshin
73553541Sshin		/*
73653541Sshin		 * Record link-layer address, and update the state.
73753541Sshin		 */
738186119Sqingli		bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
739186119Sqingli		ln->la_flags |= LLE_VALID;
74053541Sshin		if (is_solicited) {
74153541Sshin			ln->ln_state = ND6_LLINFO_REACHABLE;
74262587Sitojun			ln->ln_byhint = 0;
743151539Ssuz			if (!ND6_LLINFO_PERMANENT(ln)) {
744186119Sqingli				nd6_llinfo_settimer_locked(ln,
745186119Sqingli				    (long)ND_IFINFO(ln->lle_tbl->llt_ifp)->reachable * hz);
746120941Sume			}
74778064Sume		} else {
74853541Sshin			ln->ln_state = ND6_LLINFO_STALE;
749186119Sqingli			nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
75078064Sume		}
75178064Sume		if ((ln->ln_router = is_router) != 0) {
75278064Sume			/*
75378064Sume			 * This means a router's state has changed from
75478064Sume			 * non-reachable to probably reachable, and might
75578064Sume			 * affect the status of associated prefixes..
75678064Sume			 */
757186468Skmacy			checklink = 1;
75878064Sume		}
75953541Sshin	} else {
76053541Sshin		int llchange;
76153541Sshin
76253541Sshin		/*
76353541Sshin		 * Check if the link-layer address has changed or not.
76453541Sshin		 */
765151465Ssuz		if (lladdr == NULL)
76653541Sshin			llchange = 0;
76753541Sshin		else {
768186119Sqingli			if (ln->la_flags & LLE_VALID) {
769186119Sqingli				if (bcmp(lladdr, &ln->ll_addr, ifp->if_addrlen))
77053541Sshin					llchange = 1;
77153541Sshin				else
77253541Sshin					llchange = 0;
77353541Sshin			} else
77453541Sshin				llchange = 1;
77553541Sshin		}
77653541Sshin
77753541Sshin		/*
77853541Sshin		 * This is VERY complex.  Look at it with care.
77953541Sshin		 *
78053541Sshin		 * override solicit lladdr llchange	action
78153541Sshin		 *					(L: record lladdr)
78253541Sshin		 *
78353541Sshin		 *	0	0	n	--	(2c)
78453541Sshin		 *	0	0	y	n	(2b) L
78553541Sshin		 *	0	0	y	y	(1)    REACHABLE->STALE
78653541Sshin		 *	0	1	n	--	(2c)   *->REACHABLE
78753541Sshin		 *	0	1	y	n	(2b) L *->REACHABLE
78853541Sshin		 *	0	1	y	y	(1)    REACHABLE->STALE
78953541Sshin		 *	1	0	n	--	(2a)
79053541Sshin		 *	1	0	y	n	(2a) L
79153541Sshin		 *	1	0	y	y	(2a) L *->STALE
79253541Sshin		 *	1	1	n	--	(2a)   *->REACHABLE
79353541Sshin		 *	1	1	y	n	(2a) L *->REACHABLE
79453541Sshin		 *	1	1	y	y	(2a) L *->REACHABLE
79553541Sshin		 */
796151539Ssuz		if (!is_override && (lladdr != NULL && llchange)) {  /* (1) */
79753541Sshin			/*
79853541Sshin			 * If state is REACHABLE, make it STALE.
79953541Sshin			 * no other updates should be done.
80053541Sshin			 */
80178064Sume			if (ln->ln_state == ND6_LLINFO_REACHABLE) {
80253541Sshin				ln->ln_state = ND6_LLINFO_STALE;
803186119Sqingli				nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
80478064Sume			}
80562587Sitojun			goto freeit;
80653541Sshin		} else if (is_override				   /* (2a) */
807151465Ssuz			|| (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
808151465Ssuz			|| lladdr == NULL) {			   /* (2c) */
80953541Sshin			/*
81053541Sshin			 * Update link-local address, if any.
81153541Sshin			 */
812151465Ssuz			if (lladdr != NULL) {
813186119Sqingli				bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
814186119Sqingli				ln->la_flags |= LLE_VALID;
81553541Sshin			}
81653541Sshin
81753541Sshin			/*
81853541Sshin			 * If solicited, make the state REACHABLE.
81953541Sshin			 * If not solicited and the link-layer address was
82053541Sshin			 * changed, make it STALE.
82153541Sshin			 */
82253541Sshin			if (is_solicited) {
82353541Sshin				ln->ln_state = ND6_LLINFO_REACHABLE;
82462587Sitojun				ln->ln_byhint = 0;
825151539Ssuz				if (!ND6_LLINFO_PERMANENT(ln)) {
826186119Sqingli					nd6_llinfo_settimer_locked(ln,
827151539Ssuz					    (long)ND_IFINFO(ifp)->reachable * hz);
82853541Sshin				}
82953541Sshin			} else {
830151465Ssuz				if (lladdr != NULL && llchange) {
83153541Sshin					ln->ln_state = ND6_LLINFO_STALE;
832186119Sqingli					nd6_llinfo_settimer_locked(ln,
833181803Sbz					    (long)V_nd6_gctimer * hz);
83478064Sume				}
83553541Sshin			}
83653541Sshin		}
83753541Sshin
83853541Sshin		if (ln->ln_router && !is_router) {
83953541Sshin			/*
84053541Sshin			 * The peer dropped the router flag.
84153541Sshin			 * Remove the sender from the Default Router List and
84253541Sshin			 * update the Destination Cache entries.
84353541Sshin			 */
84453541Sshin			struct nd_defrouter *dr;
84553541Sshin			struct in6_addr *in6;
84653541Sshin
847186119Sqingli			in6 = &L3_ADDR_SIN6(ln)->sin6_addr;
84895023Ssuz
84995023Ssuz			/*
85095023Ssuz			 * Lock to protect the default router list.
85195023Ssuz			 * XXX: this might be unnecessary, since this function
85295023Ssuz			 * is only called under the network software interrupt
853120941Sume			 * context.  However, we keep it just for safety.
85495023Ssuz			 */
855186119Sqingli			dr = defrouter_lookup(in6, ln->lle_tbl->llt_ifp);
85653541Sshin			if (dr)
85753541Sshin				defrtrlist_del(dr);
858181803Sbz			else if (!V_ip6_forwarding) {
85953541Sshin				/*
86053541Sshin				 * Even if the neighbor is not in the default
86153541Sshin				 * router list, the neighbor may be used
86253541Sshin				 * as a next hop for some destinations
86353541Sshin				 * (e.g. redirect case). So we must
86453541Sshin				 * call rt6_flush explicitly.
86553541Sshin				 */
866128422Sluigi				rt6_flush(&ip6->ip6_src, ifp);
86753541Sshin			}
86853541Sshin		}
86953541Sshin		ln->ln_router = is_router;
87053541Sshin	}
871186119Sqingli        /* XXX - QL
872186119Sqingli	 *  Does this matter?
873186119Sqingli	 *  rt->rt_flags &= ~RTF_REJECT;
874186119Sqingli	 */
875186119Sqingli	ln->la_asked = 0;
876186119Sqingli	if (ln->la_hold) {
877151539Ssuz		struct mbuf *m_hold, *m_hold_next;
878151539Ssuz
879169273Ssuz		/*
880186119Sqingli		 * reset the la_hold in advance, to explicitly
881186119Sqingli		 * prevent a la_hold lookup in nd6_output()
882169273Ssuz		 * (wouldn't happen, though...)
883169273Ssuz		 */
884186119Sqingli		for (m_hold = ln->la_hold, ln->la_hold = NULL;
885169241Ssuz		    m_hold; m_hold = m_hold_next) {
886151539Ssuz			m_hold_next = m_hold->m_nextpkt;
887169241Ssuz			m_hold->m_nextpkt = NULL;
888151539Ssuz			/*
889151539Ssuz			 * we assume ifp is not a loopback here, so just set
890151539Ssuz			 * the 2nd argument as the 1st one.
891151539Ssuz			 */
892211501Sanchie
893211501Sanchie			if (send_sendso_input_hook != NULL) {
894211501Sanchie				mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
895211501Sanchie				    sizeof(unsigned short), M_NOWAIT);
896211501Sanchie				if (mtag == NULL)
897211501Sanchie					goto bad;
898211501Sanchie				m_tag_prepend(m, mtag);
899211501Sanchie			}
900211501Sanchie
901186119Sqingli			nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain);
902151539Ssuz		}
90353541Sshin	}
904186119Sqingli freeit:
905186160Skmacy	if (ln != NULL) {
906186119Sqingli		if (chain)
907186119Sqingli			memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6));
908186119Sqingli		LLE_WUNLOCK(ln);
90962587Sitojun
910186119Sqingli		if (chain)
911186119Sqingli			nd6_output_flush(ifp, ifp, chain, &sin6, NULL);
912186119Sqingli	}
913186468Skmacy	if (checklink)
914186468Skmacy		pfxlist_onlink_check();
915186468Skmacy
91662587Sitojun	m_freem(m);
91778064Sume	return;
91878064Sume
91978064Sume bad:
920186160Skmacy	if (ln != NULL)
921186119Sqingli		LLE_WUNLOCK(ln);
922186119Sqingli
923190964Srwatson	ICMP6STAT_INC(icp6s_badna);
92478064Sume	m_freem(m);
92553541Sshin}
92653541Sshin
92753541Sshin/*
92853541Sshin * Neighbor advertisement output handling.
92953541Sshin *
93053541Sshin * Based on RFC 2461
93153541Sshin *
93262587Sitojun * the following items are not implemented yet:
93362587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
93462587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
935171259Sdelphij *
936171259Sdelphij * tlladdr - 1 if include target link-layer address
937171259Sdelphij * sdl0 - sockaddr_dl (= proxy NA) or NULL
93853541Sshin */
93953541Sshinvoid
940171259Sdelphijnd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
941171259Sdelphij    const struct in6_addr *taddr6, u_long flags, int tlladdr,
942171259Sdelphij    struct sockaddr *sdl0)
94353541Sshin{
94453541Sshin	struct mbuf *m;
945211501Sanchie	struct m_tag *mtag;
94653541Sshin	struct ip6_hdr *ip6;
94753541Sshin	struct nd_neighbor_advert *nd_na;
94853541Sshin	struct ip6_moptions im6o;
949194777Sbz	struct in6_addr src, daddr6;
950148385Sume	struct sockaddr_in6 dst_sa;
951148385Sume	int icmp6len, maxlen, error;
95292733Speter	caddr_t mac = NULL;
953148385Sume	struct route_in6 ro;
95462587Sitojun
955148385Sume	bzero(&ro, sizeof(ro));
956148385Sume
957148385Sume	daddr6 = *daddr6_0;	/* make a local copy for modification */
958148385Sume
95962587Sitojun	/* estimate the size of message */
96062587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_na);
96162587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
96262587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
96362587Sitojun#ifdef DIAGNOSTIC
96462587Sitojun		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
96562587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
96662587Sitojun#endif
96753541Sshin		return;
96862587Sitojun	}
96953541Sshin
970111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
97162587Sitojun	if (m && max_linkhdr + maxlen >= MHLEN) {
972111119Simp		MCLGET(m, M_DONTWAIT);
97362587Sitojun		if ((m->m_flags & M_EXT) == 0) {
97462587Sitojun			m_free(m);
97562587Sitojun			m = NULL;
97662587Sitojun		}
97762587Sitojun	}
97862587Sitojun	if (m == NULL)
97962587Sitojun		return;
98078064Sume	m->m_pkthdr.rcvif = NULL;
98162587Sitojun
982148385Sume	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
98353541Sshin		m->m_flags |= M_MCAST;
98453541Sshin		im6o.im6o_multicast_ifp = ifp;
98553541Sshin		im6o.im6o_multicast_hlim = 255;
98653541Sshin		im6o.im6o_multicast_loop = 0;
98753541Sshin	}
98853541Sshin
98953541Sshin	icmp6len = sizeof(*nd_na);
99053541Sshin	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
99195023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
99253541Sshin
99353541Sshin	/* fill neighbor advertisement packet */
99453541Sshin	ip6 = mtod(m, struct ip6_hdr *);
99553541Sshin	ip6->ip6_flow = 0;
99662587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
99762587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
99853541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
99953541Sshin	ip6->ip6_hlim = 255;
1000148385Sume	if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
100153541Sshin		/* reply to DAD */
1002153227Sume		daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
1003153227Sume		daddr6.s6_addr16[1] = 0;
1004153227Sume		daddr6.s6_addr32[1] = 0;
1005153227Sume		daddr6.s6_addr32[2] = 0;
1006153227Sume		daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
1007148385Sume		if (in6_setscope(&daddr6, ifp, NULL))
1008148385Sume			goto bad;
1009148385Sume
101053541Sshin		flags &= ~ND_NA_FLAG_SOLICITED;
1011148385Sume	}
1012148385Sume	ip6->ip6_dst = daddr6;
1013148385Sume	bzero(&dst_sa, sizeof(struct sockaddr_in6));
1014148385Sume	dst_sa.sin6_family = AF_INET6;
1015148385Sume	dst_sa.sin6_len = sizeof(struct sockaddr_in6);
1016148385Sume	dst_sa.sin6_addr = daddr6;
101753541Sshin
101853541Sshin	/*
101953541Sshin	 * Select a source whose scope is the same as that of the dest.
102053541Sshin	 */
1021148385Sume	bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
1022194777Sbz	error = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &src);
1023194777Sbz	if (error) {
1024165118Sbz		char ip6buf[INET6_ADDRSTRLEN];
1025148385Sume		nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
1026148385Sume		    "determined: dst=%s, error=%d\n",
1027165118Sbz		    ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
1028148385Sume		goto bad;
102953541Sshin	}
1030194777Sbz	ip6->ip6_src = src;
103153541Sshin	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
103253541Sshin	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
103353541Sshin	nd_na->nd_na_code = 0;
103453541Sshin	nd_na->nd_na_target = *taddr6;
1035121315Sume	in6_clearscope(&nd_na->nd_na_target); /* XXX */
103653541Sshin
103753541Sshin	/*
103853541Sshin	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
103953541Sshin	 * see nd6_ns_input() for details.
104053541Sshin	 * Basically, if NS packet is sent to unicast/anycast addr,
104153541Sshin	 * target lladdr option SHOULD NOT be included.
104253541Sshin	 */
104362587Sitojun	if (tlladdr) {
104462587Sitojun		/*
104562587Sitojun		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
104662587Sitojun		 * lladdr in sdl0.  If we are not proxying (sending NA for
104762587Sitojun		 * my address) use lladdr configured for the interface.
104862587Sitojun		 */
1049142215Sglebius		if (sdl0 == NULL) {
1050142215Sglebius			if (ifp->if_carp)
1051211157Swill				mac = (*carp_macmatch6_p)(ifp, m, taddr6);
1052142215Sglebius			if (mac == NULL)
1053142215Sglebius				mac = nd6_ifptomac(ifp);
1054142215Sglebius		} else if (sdl0->sa_family == AF_LINK) {
105562587Sitojun			struct sockaddr_dl *sdl;
105662587Sitojun			sdl = (struct sockaddr_dl *)sdl0;
105762587Sitojun			if (sdl->sdl_alen == ifp->if_addrlen)
105862587Sitojun				mac = LLADDR(sdl);
105962587Sitojun		}
106062587Sitojun	}
106162587Sitojun	if (tlladdr && mac) {
106253541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
106353541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1064120941Sume
106553541Sshin		/* roundup to 8 bytes alignment! */
106653541Sshin		optlen = (optlen + 7) & ~7;
106753541Sshin
106853541Sshin		m->m_pkthdr.len += optlen;
106953541Sshin		m->m_len += optlen;
107053541Sshin		icmp6len += optlen;
107153541Sshin		bzero((caddr_t)nd_opt, optlen);
107253541Sshin		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
107353541Sshin		nd_opt->nd_opt_len = optlen >> 3;
107453541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
107553541Sshin	} else
107653541Sshin		flags &= ~ND_NA_FLAG_OVERRIDE;
107753541Sshin
107853541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
107953541Sshin	nd_na->nd_na_flags_reserved = flags;
108053541Sshin	nd_na->nd_na_cksum = 0;
108153541Sshin	nd_na->nd_na_cksum =
1082120941Sume	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
108353541Sshin
1084211501Sanchie	if (send_sendso_input_hook != NULL) {
1085211501Sanchie		mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
1086211501Sanchie		    sizeof(unsigned short), M_NOWAIT);
1087211501Sanchie		if (mtag == NULL)
1088211501Sanchie			goto bad;
1089211501Sanchie		*(unsigned short *)(mtag + 1) = nd_na->nd_na_type;
1090211501Sanchie		m_tag_prepend(m, mtag);
1091211501Sanchie	}
1092211501Sanchie
1093148385Sume	ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1094148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_msg);
1095148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1096190964Srwatson	ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_ADVERT]);
1097148385Sume
1098148385Sume	if (ro.ro_rt) {		/* we don't cache this route. */
1099148385Sume		RTFREE(ro.ro_rt);
110053541Sshin	}
1101148385Sume	return;
1102148385Sume
1103148385Sume  bad:
1104148385Sume	if (ro.ro_rt) {
1105148385Sume		RTFREE(ro.ro_rt);
1106148385Sume	}
1107148385Sume	m_freem(m);
1108148385Sume	return;
110953541Sshin}
111053541Sshin
111153541Sshincaddr_t
1112171259Sdelphijnd6_ifptomac(struct ifnet *ifp)
111353541Sshin{
111453541Sshin	switch (ifp->if_type) {
111553541Sshin	case IFT_ARCNET:
111653541Sshin	case IFT_ETHER:
111753541Sshin	case IFT_FDDI:
111878064Sume	case IFT_IEEE1394:
111978468Ssumikawa#ifdef IFT_L2VLAN
112078468Ssumikawa	case IFT_L2VLAN:
112178468Ssumikawa#endif
112278064Sume#ifdef IFT_IEEE80211
112378064Sume	case IFT_IEEE80211:
112478064Sume#endif
1125142215Sglebius#ifdef IFT_CARP
1126142215Sglebius	case IFT_CARP:
1127142215Sglebius#endif
1128149829Sthompsa	case IFT_BRIDGE:
1129120049Smdodd	case IFT_ISO88025:
1130147306Sbrooks		return IF_LLADDR(ifp);
113153541Sshin	default:
113253541Sshin		return NULL;
113353541Sshin	}
113453541Sshin}
113553541Sshin
113653541Sshinstruct dadq {
113760938Sjake	TAILQ_ENTRY(dadq) dad_list;
113853541Sshin	struct ifaddr *dad_ifa;
113953541Sshin	int dad_count;		/* max NS to send */
114062587Sitojun	int dad_ns_tcount;	/* # of trials to send NS */
114153541Sshin	int dad_ns_ocount;	/* NS sent so far */
114253541Sshin	int dad_ns_icount;
114353541Sshin	int dad_na_icount;
114478064Sume	struct callout dad_timer_ch;
1145191816Szec	struct vnet *dad_vnet;
114653541Sshin};
114753541Sshin
1148215701Sdimstatic VNET_DEFINE(TAILQ_HEAD(, dadq), dadq);
1149207369SbzVNET_DEFINE(int, dad_init) = 0;
1150195727Srwatson#define	V_dadq				VNET(dadq)
1151195727Srwatson#define	V_dad_init			VNET(dad_init)
1152195699Srwatson
115353541Sshinstatic struct dadq *
1154171259Sdelphijnd6_dad_find(struct ifaddr *ifa)
115553541Sshin{
115653541Sshin	struct dadq *dp;
115753541Sshin
1158181803Sbz	for (dp = V_dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
115953541Sshin		if (dp->dad_ifa == ifa)
116053541Sshin			return dp;
116153541Sshin	}
116253541Sshin	return NULL;
116353541Sshin}
116453541Sshin
116578064Sumestatic void
1166171259Sdelphijnd6_dad_starttimer(struct dadq *dp, int ticks)
116778064Sume{
116878064Sume
116978064Sume	callout_reset(&dp->dad_timer_ch, ticks,
1170191816Szec	    (void (*)(void *))nd6_dad_timer, (void *)dp);
117178064Sume}
117278064Sume
117378064Sumestatic void
1174171259Sdelphijnd6_dad_stoptimer(struct dadq *dp)
117578064Sume{
117678064Sume
117778064Sume	callout_stop(&dp->dad_timer_ch);
117878064Sume}
117978064Sume
118053541Sshin/*
1181148987Sume * Start Duplicate Address Detection (DAD) for specified interface address.
118253541Sshin */
118353541Sshinvoid
1184171259Sdelphijnd6_dad_start(struct ifaddr *ifa, int delay)
118553541Sshin{
118653541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
118753541Sshin	struct dadq *dp;
1188165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
118953541Sshin
1190181803Sbz	if (!V_dad_init) {
1191181803Sbz		TAILQ_INIT(&V_dadq);
1192181803Sbz		V_dad_init++;
119353541Sshin	}
119453541Sshin
119553541Sshin	/*
119653541Sshin	 * If we don't need DAD, don't do it.
119753541Sshin	 * There are several cases:
119853541Sshin	 * - DAD is disabled (ip6_dad_count == 0)
119953541Sshin	 * - the interface address is anycast
120053541Sshin	 */
120153541Sshin	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
120262587Sitojun		log(LOG_DEBUG,
120362587Sitojun			"nd6_dad_start: called with non-tentative address "
120453541Sshin			"%s(%s)\n",
1205165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
120653541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
120753541Sshin		return;
120853541Sshin	}
120953541Sshin	if (ia->ia6_flags & IN6_IFF_ANYCAST) {
121053541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
121153541Sshin		return;
121253541Sshin	}
1213181803Sbz	if (!V_ip6_dad_count) {
121453541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
121553541Sshin		return;
121653541Sshin	}
1217151465Ssuz	if (ifa->ifa_ifp == NULL)
121853541Sshin		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1219120941Sume	if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
122053541Sshin		return;
1221120941Sume	}
1222197138Shrs	if (ND_IFINFO(ifa->ifa_ifp)->flags & ND6_IFF_IFDISABLED)
1223197138Shrs		return;
122453541Sshin	if (nd6_dad_find(ifa) != NULL) {
122553541Sshin		/* DAD already in progress */
122653541Sshin		return;
122753541Sshin	}
122853541Sshin
122953541Sshin	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
123053541Sshin	if (dp == NULL) {
123162587Sitojun		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
123253541Sshin			"%s(%s)\n",
1233165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
123453541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
123553541Sshin		return;
123653541Sshin	}
123753541Sshin	bzero(dp, sizeof(*dp));
123878064Sume	callout_init(&dp->dad_timer_ch, 0);
1239191816Szec#ifdef VIMAGE
1240191816Szec	dp->dad_vnet = curvnet;
1241191816Szec#endif
1242182150Sjulian	TAILQ_INSERT_TAIL(&V_dadq, (struct dadq *)dp, dad_list);
124353541Sshin
124478064Sume	nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1245165118Sbz	    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
124653541Sshin
124753541Sshin	/*
124853541Sshin	 * Send NS packet for DAD, ip6_dad_count times.
124953541Sshin	 * Note that we must delay the first transmission, if this is the
125053541Sshin	 * first packet to be sent from the interface after interface
125153541Sshin	 * (re)initialization.
125253541Sshin	 */
125353541Sshin	dp->dad_ifa = ifa;
1254194602Srwatson	ifa_ref(ifa);	/* just for safety */
1255181803Sbz	dp->dad_count = V_ip6_dad_count;
125653541Sshin	dp->dad_ns_icount = dp->dad_na_icount = 0;
125762587Sitojun	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1258151539Ssuz	if (delay == 0) {
125962587Sitojun		nd6_dad_ns_output(dp, ifa);
1260121161Sume		nd6_dad_starttimer(dp,
1261151539Ssuz		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
126253541Sshin	} else {
1263151539Ssuz		nd6_dad_starttimer(dp, delay);
126453541Sshin	}
126553541Sshin}
126653541Sshin
126778064Sume/*
126878064Sume * terminate DAD unconditionally.  used for address removals.
126978064Sume */
127078064Sumevoid
1271171259Sdelphijnd6_dad_stop(struct ifaddr *ifa)
127278064Sume{
127378064Sume	struct dadq *dp;
127478064Sume
1275181803Sbz	if (!V_dad_init)
127678064Sume		return;
127778064Sume	dp = nd6_dad_find(ifa);
127878064Sume	if (!dp) {
127978064Sume		/* DAD wasn't started yet */
128078064Sume		return;
128178064Sume	}
128278064Sume
128378064Sume	nd6_dad_stoptimer(dp);
128478064Sume
1285181803Sbz	TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
128678064Sume	free(dp, M_IP6NDP);
128778064Sume	dp = NULL;
1288194602Srwatson	ifa_free(ifa);
128978064Sume}
129078064Sume
129153541Sshinstatic void
1292191816Szecnd6_dad_timer(struct dadq *dp)
129353541Sshin{
1294183550Szec	CURVNET_SET(dp->dad_vnet);
129553541Sshin	int s;
1296191816Szec	struct ifaddr *ifa = dp->dad_ifa;
129753541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1298165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
129953541Sshin
130095023Ssuz	s = splnet();		/* XXX */
130153541Sshin
130253541Sshin	/* Sanity check */
130353541Sshin	if (ia == NULL) {
130462587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
130553541Sshin		goto done;
130653541Sshin	}
130753541Sshin	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
130862587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
130953541Sshin			"%s(%s)\n",
1310165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
131153541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
131253541Sshin		goto done;
131353541Sshin	}
131453541Sshin	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
131562587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
131653541Sshin			"%s(%s)\n",
1317165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
131853541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
131953541Sshin		goto done;
132053541Sshin	}
132153541Sshin
132262587Sitojun	/* timeouted with IFF_{RUNNING,UP} check */
1323181803Sbz	if (dp->dad_ns_tcount > V_dad_maxtry) {
132478064Sume		nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1325120941Sume		    if_name(ifa->ifa_ifp)));
132662587Sitojun
1327181803Sbz		TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
132862587Sitojun		free(dp, M_IP6NDP);
132962587Sitojun		dp = NULL;
1330194602Srwatson		ifa_free(ifa);
133162587Sitojun		goto done;
133262587Sitojun	}
133362587Sitojun
133453541Sshin	/* Need more checks? */
133553541Sshin	if (dp->dad_ns_ocount < dp->dad_count) {
133653541Sshin		/*
133753541Sshin		 * We have more NS to go.  Send NS packet for DAD.
133853541Sshin		 */
133962587Sitojun		nd6_dad_ns_output(dp, ifa);
1340120941Sume		nd6_dad_starttimer(dp,
1341151539Ssuz		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
134253541Sshin	} else {
134353541Sshin		/*
134453541Sshin		 * We have transmitted sufficient number of DAD packets.
134553541Sshin		 * See what we've got.
134653541Sshin		 */
134753541Sshin		int duplicate;
134853541Sshin
134953541Sshin		duplicate = 0;
135053541Sshin
135153541Sshin		if (dp->dad_na_icount) {
135253541Sshin			/*
135353541Sshin			 * the check is in nd6_dad_na_input(),
135453541Sshin			 * but just in case
135553541Sshin			 */
135653541Sshin			duplicate++;
135753541Sshin		}
135853541Sshin
135953541Sshin		if (dp->dad_ns_icount) {
136053541Sshin			/* We've seen NS, means DAD has failed. */
136153541Sshin			duplicate++;
136253541Sshin		}
136353541Sshin
136453541Sshin		if (duplicate) {
136553541Sshin			/* (*dp) will be freed in nd6_dad_duplicated() */
136653541Sshin			dp = NULL;
136753541Sshin			nd6_dad_duplicated(ifa);
136853541Sshin		} else {
136953541Sshin			/*
137053541Sshin			 * We are done with DAD.  No NA came, no NS came.
1371148987Sume			 * No duplicate address found.
137253541Sshin			 */
137353541Sshin			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
137453541Sshin
137578064Sume			nd6log((LOG_DEBUG,
137662587Sitojun			    "%s: DAD complete for %s - no duplicates found\n",
137762587Sitojun			    if_name(ifa->ifa_ifp),
1378165118Sbz			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
137953541Sshin
1380181803Sbz			TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
138153541Sshin			free(dp, M_IP6NDP);
138253541Sshin			dp = NULL;
1383194602Srwatson			ifa_free(ifa);
138453541Sshin		}
138553541Sshin	}
138653541Sshin
138753541Sshindone:
138853541Sshin	splx(s);
1389183550Szec	CURVNET_RESTORE();
139053541Sshin}
139153541Sshin
139253541Sshinvoid
1393171259Sdelphijnd6_dad_duplicated(struct ifaddr *ifa)
139453541Sshin{
139553541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1396151477Ssuz	struct ifnet *ifp;
139753541Sshin	struct dadq *dp;
1398165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
139953541Sshin
140053541Sshin	dp = nd6_dad_find(ifa);
140153541Sshin	if (dp == NULL) {
140262587Sitojun		log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
140353541Sshin		return;
140453541Sshin	}
140553541Sshin
140678064Sume	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
140778064Sume	    "NS in/out=%d/%d, NA in=%d\n",
1408165118Sbz	    if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
140978064Sume	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
141053541Sshin
141153541Sshin	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
141253541Sshin	ia->ia6_flags |= IN6_IFF_DUPLICATED;
141353541Sshin
1414148987Sume	/* We are done with DAD, with duplicate address found. (failure) */
141578064Sume	nd6_dad_stoptimer(dp);
141653541Sshin
1417151477Ssuz	ifp = ifa->ifa_ifp;
141862587Sitojun	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1419165118Sbz	    if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
142062587Sitojun	log(LOG_ERR, "%s: manual intervention required\n",
1421151477Ssuz	    if_name(ifp));
142253541Sshin
1423151477Ssuz	/*
1424151477Ssuz	 * If the address is a link-local address formed from an interface
1425151477Ssuz	 * identifier based on the hardware address which is supposed to be
1426151477Ssuz	 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1427151477Ssuz	 * operation on the interface SHOULD be disabled.
1428197138Shrs	 * [RFC 4862, Section 5.4.5]
1429151477Ssuz	 */
1430151477Ssuz	if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1431151477Ssuz		struct in6_addr in6;
1432151477Ssuz
1433151477Ssuz		/*
1434151477Ssuz		 * To avoid over-reaction, we only apply this logic when we are
1435151477Ssuz		 * very sure that hardware addresses are supposed to be unique.
1436151477Ssuz		 */
1437151477Ssuz		switch (ifp->if_type) {
1438151477Ssuz		case IFT_ETHER:
1439151477Ssuz		case IFT_FDDI:
1440151477Ssuz		case IFT_ATM:
1441151477Ssuz		case IFT_IEEE1394:
1442151477Ssuz#ifdef IFT_IEEE80211
1443151477Ssuz		case IFT_IEEE80211:
1444151477Ssuz#endif
1445151477Ssuz			in6 = ia->ia_addr.sin6_addr;
1446151477Ssuz			if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1447151477Ssuz			    IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1448151477Ssuz				ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1449151477Ssuz				log(LOG_ERR, "%s: possible hardware address "
1450151477Ssuz				    "duplication detected, disable IPv6\n",
1451151477Ssuz				    if_name(ifp));
1452151477Ssuz			}
1453151477Ssuz			break;
1454151477Ssuz		}
1455151477Ssuz	}
1456151477Ssuz
1457181803Sbz	TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
145853541Sshin	free(dp, M_IP6NDP);
145953541Sshin	dp = NULL;
1460194602Srwatson	ifa_free(ifa);
146153541Sshin}
146253541Sshin
146362587Sitojunstatic void
1464171259Sdelphijnd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
146562587Sitojun{
146662587Sitojun	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
146762587Sitojun	struct ifnet *ifp = ifa->ifa_ifp;
146862587Sitojun
146962587Sitojun	dp->dad_ns_tcount++;
147062587Sitojun	if ((ifp->if_flags & IFF_UP) == 0) {
147162587Sitojun		return;
147262587Sitojun	}
1473148887Srwatson	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
147462587Sitojun		return;
147562587Sitojun	}
147662587Sitojun
147762587Sitojun	dp->dad_ns_ocount++;
147862587Sitojun	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
147962587Sitojun}
148062587Sitojun
148162587Sitojunstatic void
1482171259Sdelphijnd6_dad_ns_input(struct ifaddr *ifa)
148353541Sshin{
148453541Sshin	struct in6_ifaddr *ia;
148553541Sshin	struct ifnet *ifp;
148678064Sume	const struct in6_addr *taddr6;
148753541Sshin	struct dadq *dp;
148853541Sshin	int duplicate;
148953541Sshin
1490151465Ssuz	if (ifa == NULL)
149153541Sshin		panic("ifa == NULL in nd6_dad_ns_input");
149253541Sshin
149353541Sshin	ia = (struct in6_ifaddr *)ifa;
149453541Sshin	ifp = ifa->ifa_ifp;
149553541Sshin	taddr6 = &ia->ia_addr.sin6_addr;
149653541Sshin	duplicate = 0;
149753541Sshin	dp = nd6_dad_find(ifa);
149853541Sshin
149953541Sshin	/* Quickhack - completely ignore DAD NS packets */
1500181803Sbz	if (V_dad_ignore_ns) {
1501165118Sbz		char ip6buf[INET6_ADDRSTRLEN];
150278064Sume		nd6log((LOG_INFO,
150378064Sume		    "nd6_dad_ns_input: ignoring DAD NS packet for "
1504165118Sbz		    "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6),
150578064Sume		    if_name(ifa->ifa_ifp)));
150653541Sshin		return;
150753541Sshin	}
150853541Sshin
150953541Sshin	/*
151053541Sshin	 * if I'm yet to start DAD, someone else started using this address
151153541Sshin	 * first.  I have a duplicate and you win.
151253541Sshin	 */
1513151465Ssuz	if (dp == NULL || dp->dad_ns_ocount == 0)
151453541Sshin		duplicate++;
151553541Sshin
151653541Sshin	/* XXX more checks for loopback situation - see nd6_dad_timer too */
151753541Sshin
151853541Sshin	if (duplicate) {
151953541Sshin		dp = NULL;	/* will be freed in nd6_dad_duplicated() */
152053541Sshin		nd6_dad_duplicated(ifa);
152153541Sshin	} else {
152253541Sshin		/*
152353541Sshin		 * not sure if I got a duplicate.
152453541Sshin		 * increment ns count and see what happens.
152553541Sshin		 */
152653541Sshin		if (dp)
152753541Sshin			dp->dad_ns_icount++;
152853541Sshin	}
152953541Sshin}
153053541Sshin
153162587Sitojunstatic void
1532171259Sdelphijnd6_dad_na_input(struct ifaddr *ifa)
153353541Sshin{
153453541Sshin	struct dadq *dp;
153553541Sshin
1536151465Ssuz	if (ifa == NULL)
153753541Sshin		panic("ifa == NULL in nd6_dad_na_input");
153853541Sshin
153953541Sshin	dp = nd6_dad_find(ifa);
154053541Sshin	if (dp)
154153541Sshin		dp->dad_na_icount++;
154253541Sshin
154353541Sshin	/* remove the address. */
154453541Sshin	nd6_dad_duplicated(ifa);
154553541Sshin}
1546