nd6_nbr.c revision 257985
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 257985 2013-11-11 12:49:00Z ae $");
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 *);
88231852Sbzstatic void nd6_na_output_fib(struct ifnet *, const struct in6_addr *,
89231852Sbz    const struct in6_addr *, u_long, int, struct sockaddr *, u_int);
9053541Sshin
91207369SbzVNET_DEFINE(int, dad_ignore_ns) = 0;	/* ignore NS in DAD - specwise incorrect*/
92207369SbzVNET_DEFINE(int, dad_maxtry) = 15;	/* max # of *tries* to transmit DAD packet */
93195727Srwatson#define	V_dad_ignore_ns			VNET(dad_ignore_ns)
94195727Srwatson#define	V_dad_maxtry			VNET(dad_maxtry)
95195699Srwatson
9653541Sshin/*
97108470Sschweikh * Input a Neighbor Solicitation Message.
9853541Sshin *
9953541Sshin * Based on RFC 2461
100148987Sume * Based on RFC 2462 (duplicate address detection)
10153541Sshin */
10253541Sshinvoid
103171259Sdelphijnd6_ns_input(struct mbuf *m, int off, int icmp6len)
10453541Sshin{
10553541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
10653541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
10762587Sitojun	struct nd_neighbor_solicit *nd_ns;
10853541Sshin	struct in6_addr saddr6 = ip6->ip6_src;
10953541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
11062587Sitojun	struct in6_addr taddr6;
11153541Sshin	struct in6_addr myaddr6;
11253541Sshin	char *lladdr = NULL;
113142215Sglebius	struct ifaddr *ifa = NULL;
11453541Sshin	int lladdrlen = 0;
11553541Sshin	int anycast = 0, proxy = 0, tentative = 0;
11653541Sshin	int tlladdr;
117222728Shrs	int rflag;
11853541Sshin	union nd_opts ndopts;
119219562Sbz	struct sockaddr_dl proxydl;
120165118Sbz	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
12153541Sshin
122222728Shrs	rflag = (V_ip6_forwarding) ? ND_NA_FLAG_ROUTER : 0;
123222728Shrs	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV && V_ip6_norbit_raif)
124222728Shrs		rflag = 0;
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) {
131190964Srwatson		ICMP6STAT_INC(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	if (ifp->if_carp)
229211157Swill		ifa = (*carp_iamatch6_p)(ifp, &taddr6);
230228571Sglebius	else
231142215Sglebius		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
23253541Sshin
23353541Sshin	/* (2) check. */
234151465Ssuz	if (ifa == NULL) {
235257985Sae		struct route_in6 ro;
236121765Ssam		int need_proxy;
23753541Sshin
238257985Sae		bzero(&ro, sizeof(ro));
239257985Sae		ro.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
240257985Sae		ro.ro_dst.sin6_family = AF_INET6;
241257985Sae		ro.ro_dst.sin6_addr = taddr6;
24253541Sshin
243231852Sbz		/* Always use the default FIB. */
244178167Sqingli#ifdef RADIX_MPATH
245231852Sbz		rtalloc_mpath_fib((struct route *)&ro, RTF_ANNOUNCE,
246231852Sbz		    RT_DEFAULT_FIB);
247178167Sqingli#else
248257985Sae		in6_rtalloc(&ro, RT_DEFAULT_FIB);
249178167Sqingli#endif
250257985Sae		need_proxy = (ro.ro_rt &&
251257985Sae		    (ro.ro_rt->rt_flags & RTF_ANNOUNCE) != 0 &&
252257985Sae		    ro.ro_rt->rt_gateway->sa_family == AF_LINK);
253257985Sae		if (ro.ro_rt != NULL) {
254219562Sbz			if (need_proxy)
255257985Sae				proxydl = *SDL(ro.ro_rt->rt_gateway);
256257985Sae			RTFREE(ro.ro_rt);
257219562Sbz		}
258121765Ssam		if (need_proxy) {
25953541Sshin			/*
26062587Sitojun			 * proxy NDP for single entry
26153541Sshin			 */
26262587Sitojun			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
26362587Sitojun				IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
264219562Sbz			if (ifa)
26553541Sshin				proxy = 1;
26653541Sshin		}
26753541Sshin	}
268151479Ssuz	if (ifa == NULL) {
26953541Sshin		/*
27078064Sume		 * We've got an NS packet, and we don't have that adddress
27153541Sshin		 * assigned for us.  We MUST silently ignore it.
27253541Sshin		 * See RFC2461 7.2.3.
27353541Sshin		 */
27462587Sitojun		goto freeit;
27553541Sshin	}
27653541Sshin	myaddr6 = *IFA_IN6(ifa);
27753541Sshin	anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
27853541Sshin	tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
27953541Sshin	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
28062587Sitojun		goto freeit;
28153541Sshin
28253541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
283120941Sume		nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
28453541Sshin		    "(if %d, NS packet %d)\n",
285165118Sbz		    ip6_sprintf(ip6bufs, &taddr6),
286120941Sume		    ifp->if_addrlen, lladdrlen - 2));
28778064Sume		goto bad;
28853541Sshin	}
28953541Sshin
29053541Sshin	if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
291120941Sume		nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
292165118Sbz		    ip6_sprintf(ip6bufs, &saddr6)));
29362587Sitojun		goto freeit;
29453541Sshin	}
29553541Sshin
29653541Sshin	/*
29753541Sshin	 * We have neighbor solicitation packet, with target address equals to
29853541Sshin	 * one of my tentative address.
29953541Sshin	 *
30053541Sshin	 * src addr	how to process?
30153541Sshin	 * ---		---
30253541Sshin	 * multicast	of course, invalid (rejected in ip6_input)
30353541Sshin	 * unicast	somebody is doing address resolution -> ignore
30453541Sshin	 * unspec	dup address detection
30553541Sshin	 *
30653541Sshin	 * The processing is defined in RFC 2462.
30753541Sshin	 */
30853541Sshin	if (tentative) {
30953541Sshin		/*
31053541Sshin		 * If source address is unspecified address, it is for
311148987Sume		 * duplicate address detection.
31253541Sshin		 *
31353541Sshin		 * If not, the packet is for addess resolution;
31453541Sshin		 * silently ignore it.
31553541Sshin		 */
31653541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
31753541Sshin			nd6_dad_ns_input(ifa);
31853541Sshin
31962587Sitojun		goto freeit;
32053541Sshin	}
32153541Sshin
32253541Sshin	/*
32353541Sshin	 * If the source address is unspecified address, entries must not
32453541Sshin	 * be created or updated.
32553541Sshin	 * It looks that sender is performing DAD.  Output NA toward
32653541Sshin	 * all-node multicast address, to tell the sender that I'm using
32753541Sshin	 * the address.
32853541Sshin	 * S bit ("solicited") must be zero.
32953541Sshin	 */
33053541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
331148385Sume		struct in6_addr in6_all;
332148385Sume
333148385Sume		in6_all = in6addr_linklocal_allnodes;
334148385Sume		if (in6_setscope(&in6_all, ifp, NULL) != 0)
335148385Sume			goto bad;
336231852Sbz		nd6_na_output_fib(ifp, &in6_all, &taddr6,
337120941Sume		    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
338231852Sbz		    rflag, tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL,
339231852Sbz		    M_GETFIB(m));
34062587Sitojun		goto freeit;
34153541Sshin	}
34253541Sshin
343120941Sume	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
344120941Sume	    ND_NEIGHBOR_SOLICIT, 0);
34553541Sshin
346231852Sbz	nd6_na_output_fib(ifp, &saddr6, &taddr6,
347120941Sume	    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
348222728Shrs	    rflag | ND_NA_FLAG_SOLICITED, tlladdr,
349231852Sbz	    proxy ? (struct sockaddr *)&proxydl : NULL, M_GETFIB(m));
35062587Sitojun freeit:
351194760Srwatson	if (ifa != NULL)
352194760Srwatson		ifa_free(ifa);
35362587Sitojun	m_freem(m);
35453541Sshin	return;
35553541Sshin
35653541Sshin bad:
357165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
358165118Sbz		ip6_sprintf(ip6bufs, &saddr6)));
359165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
360165118Sbz		ip6_sprintf(ip6bufs, &daddr6)));
361165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
362165118Sbz		ip6_sprintf(ip6bufs, &taddr6)));
363190964Srwatson	ICMP6STAT_INC(icp6s_badns);
364194760Srwatson	if (ifa != NULL)
365194760Srwatson		ifa_free(ifa);
36662587Sitojun	m_freem(m);
36753541Sshin}
36853541Sshin
36953541Sshin/*
370108470Sschweikh * Output a Neighbor Solicitation Message. Caller specifies:
37153541Sshin *	- ICMP6 header source IP6 address
37253541Sshin *	- ND6 header target IP6 address
37353541Sshin *	- ND6 header source datalink address
37453541Sshin *
37553541Sshin * Based on RFC 2461
376148987Sume * Based on RFC 2462 (duplicate address detection)
377171259Sdelphij *
378171259Sdelphij *   ln - for source address determination
379171259Sdelphij *  dad - duplicate address detection
38053541Sshin */
38153541Sshinvoid
382186119Sqinglind6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
383186119Sqingli    const struct in6_addr *taddr6, struct llentry *ln, int dad)
38453541Sshin{
38553541Sshin	struct mbuf *m;
386211501Sanchie	struct m_tag *mtag;
38753541Sshin	struct ip6_hdr *ip6;
38853541Sshin	struct nd_neighbor_solicit *nd_ns;
38953541Sshin	struct ip6_moptions im6o;
39053541Sshin	int icmp6len;
39162587Sitojun	int maxlen;
39253541Sshin	caddr_t mac;
393148385Sume	struct route_in6 ro;
394120941Sume
39553541Sshin	if (IN6_IS_ADDR_MULTICAST(taddr6))
39653541Sshin		return;
39753541Sshin
39862587Sitojun	/* estimate the size of message */
39962587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
40062587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
40162587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
40262587Sitojun#ifdef DIAGNOSTIC
40362587Sitojun		printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
40462587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
40562587Sitojun#endif
40653541Sshin		return;
40762587Sitojun	}
40853541Sshin
409248328Sglebius	if (max_linkhdr + maxlen > MHLEN)
410248328Sglebius		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
411248328Sglebius	else
412248328Sglebius		m = m_gethdr(M_NOWAIT, MT_DATA);
41362587Sitojun	if (m == NULL)
41462587Sitojun		return;
41562587Sitojun
416215559Sbz	bzero(&ro, sizeof(ro));
417215559Sbz
41853541Sshin	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
41953541Sshin		m->m_flags |= M_MCAST;
42053541Sshin		im6o.im6o_multicast_ifp = ifp;
42153541Sshin		im6o.im6o_multicast_hlim = 255;
42253541Sshin		im6o.im6o_multicast_loop = 0;
42353541Sshin	}
42453541Sshin
42553541Sshin	icmp6len = sizeof(*nd_ns);
42653541Sshin	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
42795023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
42853541Sshin
42953541Sshin	/* fill neighbor solicitation packet */
43053541Sshin	ip6 = mtod(m, struct ip6_hdr *);
43153541Sshin	ip6->ip6_flow = 0;
43262587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
43362587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
43453541Sshin	/* ip6->ip6_plen will be set later */
43553541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
43653541Sshin	ip6->ip6_hlim = 255;
43753541Sshin	if (daddr6)
43853541Sshin		ip6->ip6_dst = *daddr6;
43953541Sshin	else {
44053541Sshin		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
441148385Sume		ip6->ip6_dst.s6_addr16[1] = 0;
44253541Sshin		ip6->ip6_dst.s6_addr32[1] = 0;
44353541Sshin		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
44453541Sshin		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
44553541Sshin		ip6->ip6_dst.s6_addr8[12] = 0xff;
446148385Sume		if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
447148385Sume			goto bad;
44853541Sshin	}
44953541Sshin	if (!dad) {
450194760Srwatson		struct ifaddr *ifa;
451194760Srwatson
45253541Sshin		/*
45353541Sshin		 * RFC2461 7.2.2:
45453541Sshin		 * "If the source address of the packet prompting the
45553541Sshin		 * solicitation is the same as one of the addresses assigned
45653541Sshin		 * to the outgoing interface, that address SHOULD be placed
45753541Sshin		 * in the IP Source Address of the outgoing solicitation.
45853541Sshin		 * Otherwise, any one of the addresses assigned to the
45953541Sshin		 * interface should be used."
46053541Sshin		 *
46153541Sshin		 * We use the source address for the prompting packet
46253541Sshin		 * (saddr6), if:
46353541Sshin		 * - saddr6 is given from the caller (by giving "ln"), and
46453541Sshin		 * - saddr6 belongs to the outgoing interface.
465148385Sume		 * Otherwise, we perform the source address selection as usual.
46653541Sshin		 */
467216022Sbz		struct in6_addr *hsrc;
46853541Sshin
469216022Sbz		hsrc = NULL;
470216022Sbz		if (ln != NULL) {
471216022Sbz			LLE_RLOCK(ln);
472216022Sbz			if (ln->la_hold != NULL) {
473216022Sbz				struct ip6_hdr *hip6;		/* hold ip6 */
474216022Sbz
475216022Sbz				/*
476216022Sbz				 * assuming every packet in la_hold has the same IP
477216022Sbz				 * header
478216022Sbz				 */
479216022Sbz				hip6 = mtod(ln->la_hold, struct ip6_hdr *);
480216022Sbz				/* XXX pullup? */
481216022Sbz				if (sizeof(*hip6) < ln->la_hold->m_len) {
482216022Sbz					ip6->ip6_src = hip6->ip6_src;
483216022Sbz					hsrc = &hip6->ip6_src;
484216022Sbz				}
485216022Sbz			}
486216022Sbz			LLE_RUNLOCK(ln);
487148385Sume		}
488194760Srwatson		if (hsrc && (ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
489194760Srwatson		    hsrc)) != NULL) {
490216022Sbz			/* ip6_src set already. */
491194760Srwatson			ifa_free(ifa);
492194760Srwatson		} else {
493148385Sume			int error;
494148385Sume			struct sockaddr_in6 dst_sa;
495216022Sbz			struct in6_addr src_in;
496231852Sbz			struct ifnet *oifp;
497148385Sume
498148385Sume			bzero(&dst_sa, sizeof(dst_sa));
499148385Sume			dst_sa.sin6_family = AF_INET6;
500148385Sume			dst_sa.sin6_len = sizeof(dst_sa);
501148385Sume			dst_sa.sin6_addr = ip6->ip6_dst;
502148385Sume
503231852Sbz			oifp = ifp;
504194777Sbz			error = in6_selectsrc(&dst_sa, NULL,
505231852Sbz			    NULL, &ro, NULL, &oifp, &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
580238092Sglebius	/* We don't cache this route. */
581238092Sglebius	RO_RTFREE(&ro);
582238092Sglebius
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
681228571Sglebius	/*
682228571Sglebius	 * This effectively disables the DAD check on a non-master CARP
683228571Sglebius	 * address.
684228571Sglebius	 */
685228571Sglebius	if (ifp->if_carp)
686228571Sglebius		ifa = (*carp_iamatch6_p)(ifp, &taddr6);
687228571Sglebius	else
688228571Sglebius		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
68953541Sshin
69053541Sshin	/*
69153541Sshin	 * Target address matches one of my interface address.
69253541Sshin	 *
69353541Sshin	 * If my address is tentative, this means that there's somebody
69453541Sshin	 * already using the same address as mine.  This indicates DAD failure.
69553541Sshin	 * This is defined in RFC 2462.
69653541Sshin	 *
69753541Sshin	 * Otherwise, process as defined in RFC 2461.
69853541Sshin	 */
69953541Sshin	if (ifa
70053541Sshin	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
701194760Srwatson		ifa_free(ifa);
70253541Sshin		nd6_dad_na_input(ifa);
70362587Sitojun		goto freeit;
70453541Sshin	}
70553541Sshin
70695023Ssuz	/* Just for safety, maybe unnecessary. */
70753541Sshin	if (ifa) {
708194760Srwatson		ifa_free(ifa);
70953541Sshin		log(LOG_ERR,
71053541Sshin		    "nd6_na_input: duplicate IP6 address %s\n",
711165118Sbz		    ip6_sprintf(ip6bufs, &taddr6));
71262587Sitojun		goto freeit;
71353541Sshin	}
71453541Sshin
71553541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
716120941Sume		nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
717165118Sbz		    "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
718120941Sume		    ifp->if_addrlen, lladdrlen - 2));
71978064Sume		goto bad;
72053541Sshin	}
72153541Sshin
72253541Sshin	/*
723120941Sume	 * If no neighbor cache entry is found, NA SHOULD silently be
724120941Sume	 * discarded.
72553541Sshin	 */
726186119Sqingli	IF_AFDATA_LOCK(ifp);
727186119Sqingli	ln = nd6_lookup(&taddr6, LLE_EXCLUSIVE, ifp);
728186119Sqingli	IF_AFDATA_UNLOCK(ifp);
729186119Sqingli	if (ln == NULL) {
73062587Sitojun		goto freeit;
731186119Sqingli	}
73253541Sshin
73353541Sshin	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
73453541Sshin		/*
73553541Sshin		 * If the link-layer has address, and no lladdr option came,
73653541Sshin		 * discard the packet.
73753541Sshin		 */
738186119Sqingli		if (ifp->if_addrlen && lladdr == NULL) {
73962587Sitojun			goto freeit;
740186119Sqingli		}
74153541Sshin
74253541Sshin		/*
74353541Sshin		 * Record link-layer address, and update the state.
74453541Sshin		 */
745186119Sqingli		bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
746186119Sqingli		ln->la_flags |= LLE_VALID;
747245925Snp		EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
74853541Sshin		if (is_solicited) {
74953541Sshin			ln->ln_state = ND6_LLINFO_REACHABLE;
75062587Sitojun			ln->ln_byhint = 0;
751151539Ssuz			if (!ND6_LLINFO_PERMANENT(ln)) {
752186119Sqingli				nd6_llinfo_settimer_locked(ln,
753186119Sqingli				    (long)ND_IFINFO(ln->lle_tbl->llt_ifp)->reachable * hz);
754120941Sume			}
75578064Sume		} else {
75653541Sshin			ln->ln_state = ND6_LLINFO_STALE;
757186119Sqingli			nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
75878064Sume		}
75978064Sume		if ((ln->ln_router = is_router) != 0) {
76078064Sume			/*
76178064Sume			 * This means a router's state has changed from
76278064Sume			 * non-reachable to probably reachable, and might
76378064Sume			 * affect the status of associated prefixes..
76478064Sume			 */
765186468Skmacy			checklink = 1;
76678064Sume		}
76753541Sshin	} else {
76853541Sshin		int llchange;
76953541Sshin
77053541Sshin		/*
77153541Sshin		 * Check if the link-layer address has changed or not.
77253541Sshin		 */
773151465Ssuz		if (lladdr == NULL)
77453541Sshin			llchange = 0;
77553541Sshin		else {
776186119Sqingli			if (ln->la_flags & LLE_VALID) {
777186119Sqingli				if (bcmp(lladdr, &ln->ll_addr, ifp->if_addrlen))
77853541Sshin					llchange = 1;
77953541Sshin				else
78053541Sshin					llchange = 0;
78153541Sshin			} else
78253541Sshin				llchange = 1;
78353541Sshin		}
78453541Sshin
78553541Sshin		/*
78653541Sshin		 * This is VERY complex.  Look at it with care.
78753541Sshin		 *
78853541Sshin		 * override solicit lladdr llchange	action
78953541Sshin		 *					(L: record lladdr)
79053541Sshin		 *
79153541Sshin		 *	0	0	n	--	(2c)
79253541Sshin		 *	0	0	y	n	(2b) L
79353541Sshin		 *	0	0	y	y	(1)    REACHABLE->STALE
79453541Sshin		 *	0	1	n	--	(2c)   *->REACHABLE
79553541Sshin		 *	0	1	y	n	(2b) L *->REACHABLE
79653541Sshin		 *	0	1	y	y	(1)    REACHABLE->STALE
79753541Sshin		 *	1	0	n	--	(2a)
79853541Sshin		 *	1	0	y	n	(2a) L
79953541Sshin		 *	1	0	y	y	(2a) L *->STALE
80053541Sshin		 *	1	1	n	--	(2a)   *->REACHABLE
80153541Sshin		 *	1	1	y	n	(2a) L *->REACHABLE
80253541Sshin		 *	1	1	y	y	(2a) L *->REACHABLE
80353541Sshin		 */
804151539Ssuz		if (!is_override && (lladdr != NULL && llchange)) {  /* (1) */
80553541Sshin			/*
80653541Sshin			 * If state is REACHABLE, make it STALE.
80753541Sshin			 * no other updates should be done.
80853541Sshin			 */
80978064Sume			if (ln->ln_state == ND6_LLINFO_REACHABLE) {
81053541Sshin				ln->ln_state = ND6_LLINFO_STALE;
811186119Sqingli				nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
81278064Sume			}
81362587Sitojun			goto freeit;
81453541Sshin		} else if (is_override				   /* (2a) */
815151465Ssuz			|| (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
816151465Ssuz			|| lladdr == NULL) {			   /* (2c) */
81753541Sshin			/*
81853541Sshin			 * Update link-local address, if any.
81953541Sshin			 */
820151465Ssuz			if (lladdr != NULL) {
821186119Sqingli				bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
822186119Sqingli				ln->la_flags |= LLE_VALID;
823245925Snp				EVENTHANDLER_INVOKE(lle_event, ln,
824245925Snp				    LLENTRY_RESOLVED);
82553541Sshin			}
82653541Sshin
82753541Sshin			/*
82853541Sshin			 * If solicited, make the state REACHABLE.
82953541Sshin			 * If not solicited and the link-layer address was
83053541Sshin			 * changed, make it STALE.
83153541Sshin			 */
83253541Sshin			if (is_solicited) {
83353541Sshin				ln->ln_state = ND6_LLINFO_REACHABLE;
83462587Sitojun				ln->ln_byhint = 0;
835151539Ssuz				if (!ND6_LLINFO_PERMANENT(ln)) {
836186119Sqingli					nd6_llinfo_settimer_locked(ln,
837151539Ssuz					    (long)ND_IFINFO(ifp)->reachable * hz);
83853541Sshin				}
83953541Sshin			} else {
840151465Ssuz				if (lladdr != NULL && llchange) {
84153541Sshin					ln->ln_state = ND6_LLINFO_STALE;
842186119Sqingli					nd6_llinfo_settimer_locked(ln,
843181803Sbz					    (long)V_nd6_gctimer * hz);
84478064Sume				}
84553541Sshin			}
84653541Sshin		}
84753541Sshin
84853541Sshin		if (ln->ln_router && !is_router) {
84953541Sshin			/*
85053541Sshin			 * The peer dropped the router flag.
85153541Sshin			 * Remove the sender from the Default Router List and
85253541Sshin			 * update the Destination Cache entries.
85353541Sshin			 */
85453541Sshin			struct nd_defrouter *dr;
85553541Sshin			struct in6_addr *in6;
85653541Sshin
857186119Sqingli			in6 = &L3_ADDR_SIN6(ln)->sin6_addr;
85895023Ssuz
85995023Ssuz			/*
86095023Ssuz			 * Lock to protect the default router list.
86195023Ssuz			 * XXX: this might be unnecessary, since this function
86295023Ssuz			 * is only called under the network software interrupt
863120941Sume			 * context.  However, we keep it just for safety.
86495023Ssuz			 */
865186119Sqingli			dr = defrouter_lookup(in6, ln->lle_tbl->llt_ifp);
86653541Sshin			if (dr)
86753541Sshin				defrtrlist_del(dr);
868222728Shrs			else if (ND_IFINFO(ln->lle_tbl->llt_ifp)->flags &
869222728Shrs			    ND6_IFF_ACCEPT_RTADV) {
87053541Sshin				/*
87153541Sshin				 * Even if the neighbor is not in the default
87253541Sshin				 * router list, the neighbor may be used
87353541Sshin				 * as a next hop for some destinations
87453541Sshin				 * (e.g. redirect case). So we must
87553541Sshin				 * call rt6_flush explicitly.
87653541Sshin				 */
877128422Sluigi				rt6_flush(&ip6->ip6_src, ifp);
87853541Sshin			}
87953541Sshin		}
88053541Sshin		ln->ln_router = is_router;
88153541Sshin	}
882186119Sqingli        /* XXX - QL
883186119Sqingli	 *  Does this matter?
884186119Sqingli	 *  rt->rt_flags &= ~RTF_REJECT;
885186119Sqingli	 */
886186119Sqingli	ln->la_asked = 0;
887186119Sqingli	if (ln->la_hold) {
888151539Ssuz		struct mbuf *m_hold, *m_hold_next;
889151539Ssuz
890169273Ssuz		/*
891186119Sqingli		 * reset the la_hold in advance, to explicitly
892186119Sqingli		 * prevent a la_hold lookup in nd6_output()
893169273Ssuz		 * (wouldn't happen, though...)
894169273Ssuz		 */
895186119Sqingli		for (m_hold = ln->la_hold, ln->la_hold = NULL;
896169241Ssuz		    m_hold; m_hold = m_hold_next) {
897151539Ssuz			m_hold_next = m_hold->m_nextpkt;
898169241Ssuz			m_hold->m_nextpkt = NULL;
899151539Ssuz			/*
900151539Ssuz			 * we assume ifp is not a loopback here, so just set
901151539Ssuz			 * the 2nd argument as the 1st one.
902151539Ssuz			 */
903211501Sanchie
904211501Sanchie			if (send_sendso_input_hook != NULL) {
905211501Sanchie				mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
906211501Sanchie				    sizeof(unsigned short), M_NOWAIT);
907211501Sanchie				if (mtag == NULL)
908211501Sanchie					goto bad;
909211501Sanchie				m_tag_prepend(m, mtag);
910211501Sanchie			}
911211501Sanchie
912186119Sqingli			nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain);
913151539Ssuz		}
91453541Sshin	}
915186119Sqingli freeit:
916186160Skmacy	if (ln != NULL) {
917186119Sqingli		if (chain)
918186119Sqingli			memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6));
919186119Sqingli		LLE_WUNLOCK(ln);
92062587Sitojun
921186119Sqingli		if (chain)
922186119Sqingli			nd6_output_flush(ifp, ifp, chain, &sin6, NULL);
923186119Sqingli	}
924186468Skmacy	if (checklink)
925186468Skmacy		pfxlist_onlink_check();
926186468Skmacy
92762587Sitojun	m_freem(m);
92878064Sume	return;
92978064Sume
93078064Sume bad:
931186160Skmacy	if (ln != NULL)
932186119Sqingli		LLE_WUNLOCK(ln);
933186119Sqingli
934190964Srwatson	ICMP6STAT_INC(icp6s_badna);
93578064Sume	m_freem(m);
93653541Sshin}
93753541Sshin
93853541Sshin/*
93953541Sshin * Neighbor advertisement output handling.
94053541Sshin *
94153541Sshin * Based on RFC 2461
94253541Sshin *
94362587Sitojun * the following items are not implemented yet:
94462587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
94562587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
946171259Sdelphij *
947171259Sdelphij * tlladdr - 1 if include target link-layer address
948171259Sdelphij * sdl0 - sockaddr_dl (= proxy NA) or NULL
94953541Sshin */
950231852Sbzstatic void
951231852Sbznd6_na_output_fib(struct ifnet *ifp, const struct in6_addr *daddr6_0,
952171259Sdelphij    const struct in6_addr *taddr6, u_long flags, int tlladdr,
953231852Sbz    struct sockaddr *sdl0, u_int fibnum)
95453541Sshin{
95553541Sshin	struct mbuf *m;
956211501Sanchie	struct m_tag *mtag;
957231852Sbz	struct ifnet *oifp;
95853541Sshin	struct ip6_hdr *ip6;
95953541Sshin	struct nd_neighbor_advert *nd_na;
96053541Sshin	struct ip6_moptions im6o;
961194777Sbz	struct in6_addr src, daddr6;
962148385Sume	struct sockaddr_in6 dst_sa;
963148385Sume	int icmp6len, maxlen, error;
96492733Speter	caddr_t mac = NULL;
965148385Sume	struct route_in6 ro;
96662587Sitojun
967148385Sume	bzero(&ro, sizeof(ro));
968148385Sume
969148385Sume	daddr6 = *daddr6_0;	/* make a local copy for modification */
970148385Sume
97162587Sitojun	/* estimate the size of message */
97262587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_na);
97362587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
97462587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
97562587Sitojun#ifdef DIAGNOSTIC
97662587Sitojun		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
97762587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
97862587Sitojun#endif
97953541Sshin		return;
98062587Sitojun	}
98153541Sshin
982248328Sglebius	if (max_linkhdr + maxlen > MHLEN)
983248328Sglebius		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
984248328Sglebius	else
985248328Sglebius		m = m_gethdr(M_NOWAIT, MT_DATA);
98662587Sitojun	if (m == NULL)
98762587Sitojun		return;
988231852Sbz	M_SETFIB(m, fibnum);
98962587Sitojun
990148385Sume	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
99153541Sshin		m->m_flags |= M_MCAST;
99253541Sshin		im6o.im6o_multicast_ifp = ifp;
99353541Sshin		im6o.im6o_multicast_hlim = 255;
99453541Sshin		im6o.im6o_multicast_loop = 0;
99553541Sshin	}
99653541Sshin
99753541Sshin	icmp6len = sizeof(*nd_na);
99853541Sshin	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
99995023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
100053541Sshin
100153541Sshin	/* fill neighbor advertisement packet */
100253541Sshin	ip6 = mtod(m, struct ip6_hdr *);
100353541Sshin	ip6->ip6_flow = 0;
100462587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
100562587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
100653541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
100753541Sshin	ip6->ip6_hlim = 255;
1008148385Sume	if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
100953541Sshin		/* reply to DAD */
1010153227Sume		daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
1011153227Sume		daddr6.s6_addr16[1] = 0;
1012153227Sume		daddr6.s6_addr32[1] = 0;
1013153227Sume		daddr6.s6_addr32[2] = 0;
1014153227Sume		daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
1015148385Sume		if (in6_setscope(&daddr6, ifp, NULL))
1016148385Sume			goto bad;
1017148385Sume
101853541Sshin		flags &= ~ND_NA_FLAG_SOLICITED;
1019148385Sume	}
1020148385Sume	ip6->ip6_dst = daddr6;
1021148385Sume	bzero(&dst_sa, sizeof(struct sockaddr_in6));
1022148385Sume	dst_sa.sin6_family = AF_INET6;
1023148385Sume	dst_sa.sin6_len = sizeof(struct sockaddr_in6);
1024148385Sume	dst_sa.sin6_addr = daddr6;
102553541Sshin
102653541Sshin	/*
102753541Sshin	 * Select a source whose scope is the same as that of the dest.
102853541Sshin	 */
1029148385Sume	bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
1030231852Sbz	oifp = ifp;
1031231852Sbz	error = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, &oifp, &src);
1032194777Sbz	if (error) {
1033165118Sbz		char ip6buf[INET6_ADDRSTRLEN];
1034148385Sume		nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
1035148385Sume		    "determined: dst=%s, error=%d\n",
1036165118Sbz		    ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
1037148385Sume		goto bad;
103853541Sshin	}
1039194777Sbz	ip6->ip6_src = src;
104053541Sshin	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
104153541Sshin	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
104253541Sshin	nd_na->nd_na_code = 0;
104353541Sshin	nd_na->nd_na_target = *taddr6;
1044121315Sume	in6_clearscope(&nd_na->nd_na_target); /* XXX */
104553541Sshin
104653541Sshin	/*
104753541Sshin	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
104853541Sshin	 * see nd6_ns_input() for details.
104953541Sshin	 * Basically, if NS packet is sent to unicast/anycast addr,
105053541Sshin	 * target lladdr option SHOULD NOT be included.
105153541Sshin	 */
105262587Sitojun	if (tlladdr) {
105362587Sitojun		/*
105462587Sitojun		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
105562587Sitojun		 * lladdr in sdl0.  If we are not proxying (sending NA for
105662587Sitojun		 * my address) use lladdr configured for the interface.
105762587Sitojun		 */
1058142215Sglebius		if (sdl0 == NULL) {
1059142215Sglebius			if (ifp->if_carp)
1060211157Swill				mac = (*carp_macmatch6_p)(ifp, m, taddr6);
1061142215Sglebius			if (mac == NULL)
1062142215Sglebius				mac = nd6_ifptomac(ifp);
1063142215Sglebius		} else if (sdl0->sa_family == AF_LINK) {
106462587Sitojun			struct sockaddr_dl *sdl;
106562587Sitojun			sdl = (struct sockaddr_dl *)sdl0;
106662587Sitojun			if (sdl->sdl_alen == ifp->if_addrlen)
106762587Sitojun				mac = LLADDR(sdl);
106862587Sitojun		}
106962587Sitojun	}
107062587Sitojun	if (tlladdr && mac) {
107153541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
107253541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1073120941Sume
107453541Sshin		/* roundup to 8 bytes alignment! */
107553541Sshin		optlen = (optlen + 7) & ~7;
107653541Sshin
107753541Sshin		m->m_pkthdr.len += optlen;
107853541Sshin		m->m_len += optlen;
107953541Sshin		icmp6len += optlen;
108053541Sshin		bzero((caddr_t)nd_opt, optlen);
108153541Sshin		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
108253541Sshin		nd_opt->nd_opt_len = optlen >> 3;
108353541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
108453541Sshin	} else
108553541Sshin		flags &= ~ND_NA_FLAG_OVERRIDE;
108653541Sshin
108753541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
108853541Sshin	nd_na->nd_na_flags_reserved = flags;
108953541Sshin	nd_na->nd_na_cksum = 0;
109053541Sshin	nd_na->nd_na_cksum =
1091120941Sume	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
109253541Sshin
1093211501Sanchie	if (send_sendso_input_hook != NULL) {
1094211501Sanchie		mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
1095211501Sanchie		    sizeof(unsigned short), M_NOWAIT);
1096211501Sanchie		if (mtag == NULL)
1097211501Sanchie			goto bad;
1098211501Sanchie		*(unsigned short *)(mtag + 1) = nd_na->nd_na_type;
1099211501Sanchie		m_tag_prepend(m, mtag);
1100211501Sanchie	}
1101211501Sanchie
1102148385Sume	ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1103148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_msg);
1104148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1105190964Srwatson	ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_ADVERT]);
1106148385Sume
1107238092Sglebius	/* We don't cache this route. */
1108238092Sglebius	RO_RTFREE(&ro);
1109238092Sglebius
1110148385Sume	return;
1111148385Sume
1112148385Sume  bad:
1113148385Sume	if (ro.ro_rt) {
1114148385Sume		RTFREE(ro.ro_rt);
1115148385Sume	}
1116148385Sume	m_freem(m);
1117148385Sume	return;
111853541Sshin}
111953541Sshin
1120231852Sbz#ifndef BURN_BRIDGES
1121231852Sbzvoid
1122231852Sbznd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
1123231852Sbz    const struct in6_addr *taddr6, u_long flags, int tlladdr,
1124231852Sbz    struct sockaddr *sdl0)
1125231852Sbz{
1126231852Sbz
1127231852Sbz	nd6_na_output_fib(ifp, daddr6_0, taddr6, flags, tlladdr, sdl0,
1128231852Sbz	    RT_DEFAULT_FIB);
1129231852Sbz}
1130231852Sbz#endif
1131231852Sbz
113253541Sshincaddr_t
1133171259Sdelphijnd6_ifptomac(struct ifnet *ifp)
113453541Sshin{
113553541Sshin	switch (ifp->if_type) {
113653541Sshin	case IFT_ARCNET:
113753541Sshin	case IFT_ETHER:
113853541Sshin	case IFT_FDDI:
113978064Sume	case IFT_IEEE1394:
114078468Ssumikawa#ifdef IFT_L2VLAN
114178468Ssumikawa	case IFT_L2VLAN:
114278468Ssumikawa#endif
114378064Sume#ifdef IFT_IEEE80211
114478064Sume	case IFT_IEEE80211:
114578064Sume#endif
1146219819Sjeff	case IFT_INFINIBAND:
1147149829Sthompsa	case IFT_BRIDGE:
1148120049Smdodd	case IFT_ISO88025:
1149147306Sbrooks		return IF_LLADDR(ifp);
115053541Sshin	default:
115153541Sshin		return NULL;
115253541Sshin	}
115353541Sshin}
115453541Sshin
115553541Sshinstruct dadq {
115660938Sjake	TAILQ_ENTRY(dadq) dad_list;
115753541Sshin	struct ifaddr *dad_ifa;
115853541Sshin	int dad_count;		/* max NS to send */
115962587Sitojun	int dad_ns_tcount;	/* # of trials to send NS */
116053541Sshin	int dad_ns_ocount;	/* NS sent so far */
116153541Sshin	int dad_ns_icount;
116253541Sshin	int dad_na_icount;
116378064Sume	struct callout dad_timer_ch;
1164191816Szec	struct vnet *dad_vnet;
116553541Sshin};
116653541Sshin
1167215701Sdimstatic VNET_DEFINE(TAILQ_HEAD(, dadq), dadq);
1168207369SbzVNET_DEFINE(int, dad_init) = 0;
1169195727Srwatson#define	V_dadq				VNET(dadq)
1170195727Srwatson#define	V_dad_init			VNET(dad_init)
1171195699Srwatson
117253541Sshinstatic struct dadq *
1173171259Sdelphijnd6_dad_find(struct ifaddr *ifa)
117453541Sshin{
117553541Sshin	struct dadq *dp;
117653541Sshin
1177226340Sglebius	TAILQ_FOREACH(dp, &V_dadq, dad_list)
117853541Sshin		if (dp->dad_ifa == ifa)
1179226340Sglebius			return (dp);
1180226340Sglebius
1181226340Sglebius	return (NULL);
118253541Sshin}
118353541Sshin
118478064Sumestatic void
1185171259Sdelphijnd6_dad_starttimer(struct dadq *dp, int ticks)
118678064Sume{
118778064Sume
118878064Sume	callout_reset(&dp->dad_timer_ch, ticks,
1189191816Szec	    (void (*)(void *))nd6_dad_timer, (void *)dp);
119078064Sume}
119178064Sume
119278064Sumestatic void
1193171259Sdelphijnd6_dad_stoptimer(struct dadq *dp)
119478064Sume{
119578064Sume
119678064Sume	callout_stop(&dp->dad_timer_ch);
119778064Sume}
119878064Sume
119953541Sshin/*
1200148987Sume * Start Duplicate Address Detection (DAD) for specified interface address.
120153541Sshin */
120253541Sshinvoid
1203171259Sdelphijnd6_dad_start(struct ifaddr *ifa, int delay)
120453541Sshin{
120553541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
120653541Sshin	struct dadq *dp;
1207165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
120853541Sshin
1209181803Sbz	if (!V_dad_init) {
1210181803Sbz		TAILQ_INIT(&V_dadq);
1211181803Sbz		V_dad_init++;
121253541Sshin	}
121353541Sshin
121453541Sshin	/*
121553541Sshin	 * If we don't need DAD, don't do it.
121653541Sshin	 * There are several cases:
121753541Sshin	 * - DAD is disabled (ip6_dad_count == 0)
121853541Sshin	 * - the interface address is anycast
121953541Sshin	 */
122053541Sshin	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
122162587Sitojun		log(LOG_DEBUG,
122262587Sitojun			"nd6_dad_start: called with non-tentative address "
122353541Sshin			"%s(%s)\n",
1224165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
122553541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
122653541Sshin		return;
122753541Sshin	}
122853541Sshin	if (ia->ia6_flags & IN6_IFF_ANYCAST) {
122953541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
123053541Sshin		return;
123153541Sshin	}
1232181803Sbz	if (!V_ip6_dad_count) {
123353541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
123453541Sshin		return;
123553541Sshin	}
1236151465Ssuz	if (ifa->ifa_ifp == NULL)
123753541Sshin		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1238120941Sume	if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
123953541Sshin		return;
1240120941Sume	}
1241197138Shrs	if (ND_IFINFO(ifa->ifa_ifp)->flags & ND6_IFF_IFDISABLED)
1242197138Shrs		return;
124353541Sshin	if (nd6_dad_find(ifa) != NULL) {
124453541Sshin		/* DAD already in progress */
124553541Sshin		return;
124653541Sshin	}
124753541Sshin
124853541Sshin	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
124953541Sshin	if (dp == NULL) {
125062587Sitojun		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
125153541Sshin			"%s(%s)\n",
1252165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
125353541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
125453541Sshin		return;
125553541Sshin	}
125653541Sshin	bzero(dp, sizeof(*dp));
125778064Sume	callout_init(&dp->dad_timer_ch, 0);
1258191816Szec#ifdef VIMAGE
1259191816Szec	dp->dad_vnet = curvnet;
1260191816Szec#endif
1261182150Sjulian	TAILQ_INSERT_TAIL(&V_dadq, (struct dadq *)dp, dad_list);
126253541Sshin
126378064Sume	nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1264165118Sbz	    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
126553541Sshin
126653541Sshin	/*
126753541Sshin	 * Send NS packet for DAD, ip6_dad_count times.
126853541Sshin	 * Note that we must delay the first transmission, if this is the
126953541Sshin	 * first packet to be sent from the interface after interface
127053541Sshin	 * (re)initialization.
127153541Sshin	 */
127253541Sshin	dp->dad_ifa = ifa;
1273194602Srwatson	ifa_ref(ifa);	/* just for safety */
1274181803Sbz	dp->dad_count = V_ip6_dad_count;
127553541Sshin	dp->dad_ns_icount = dp->dad_na_icount = 0;
127662587Sitojun	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1277151539Ssuz	if (delay == 0) {
127862587Sitojun		nd6_dad_ns_output(dp, ifa);
1279121161Sume		nd6_dad_starttimer(dp,
1280151539Ssuz		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
128153541Sshin	} else {
1282151539Ssuz		nd6_dad_starttimer(dp, delay);
128353541Sshin	}
128453541Sshin}
128553541Sshin
128678064Sume/*
128778064Sume * terminate DAD unconditionally.  used for address removals.
128878064Sume */
128978064Sumevoid
1290171259Sdelphijnd6_dad_stop(struct ifaddr *ifa)
129178064Sume{
129278064Sume	struct dadq *dp;
129378064Sume
1294181803Sbz	if (!V_dad_init)
129578064Sume		return;
129678064Sume	dp = nd6_dad_find(ifa);
129778064Sume	if (!dp) {
129878064Sume		/* DAD wasn't started yet */
129978064Sume		return;
130078064Sume	}
130178064Sume
130278064Sume	nd6_dad_stoptimer(dp);
130378064Sume
1304181803Sbz	TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
130578064Sume	free(dp, M_IP6NDP);
130678064Sume	dp = NULL;
1307194602Srwatson	ifa_free(ifa);
130878064Sume}
130978064Sume
131053541Sshinstatic void
1311191816Szecnd6_dad_timer(struct dadq *dp)
131253541Sshin{
1313183550Szec	CURVNET_SET(dp->dad_vnet);
1314191816Szec	struct ifaddr *ifa = dp->dad_ifa;
131553541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1316165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
131753541Sshin
131853541Sshin	/* Sanity check */
131953541Sshin	if (ia == NULL) {
132062587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
132153541Sshin		goto done;
132253541Sshin	}
132353541Sshin	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
132462587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
132553541Sshin			"%s(%s)\n",
1326165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
132753541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
132853541Sshin		goto done;
132953541Sshin	}
133053541Sshin	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
133162587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
133253541Sshin			"%s(%s)\n",
1333165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
133453541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
133553541Sshin		goto done;
133653541Sshin	}
133753541Sshin
133862587Sitojun	/* timeouted with IFF_{RUNNING,UP} check */
1339181803Sbz	if (dp->dad_ns_tcount > V_dad_maxtry) {
134078064Sume		nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1341120941Sume		    if_name(ifa->ifa_ifp)));
134262587Sitojun
1343181803Sbz		TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
134462587Sitojun		free(dp, M_IP6NDP);
134562587Sitojun		dp = NULL;
1346194602Srwatson		ifa_free(ifa);
134762587Sitojun		goto done;
134862587Sitojun	}
134962587Sitojun
135053541Sshin	/* Need more checks? */
135153541Sshin	if (dp->dad_ns_ocount < dp->dad_count) {
135253541Sshin		/*
135353541Sshin		 * We have more NS to go.  Send NS packet for DAD.
135453541Sshin		 */
135562587Sitojun		nd6_dad_ns_output(dp, ifa);
1356120941Sume		nd6_dad_starttimer(dp,
1357151539Ssuz		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
135853541Sshin	} else {
135953541Sshin		/*
136053541Sshin		 * We have transmitted sufficient number of DAD packets.
136153541Sshin		 * See what we've got.
136253541Sshin		 */
136353541Sshin		int duplicate;
136453541Sshin
136553541Sshin		duplicate = 0;
136653541Sshin
136753541Sshin		if (dp->dad_na_icount) {
136853541Sshin			/*
136953541Sshin			 * the check is in nd6_dad_na_input(),
137053541Sshin			 * but just in case
137153541Sshin			 */
137253541Sshin			duplicate++;
137353541Sshin		}
137453541Sshin
137553541Sshin		if (dp->dad_ns_icount) {
137653541Sshin			/* We've seen NS, means DAD has failed. */
137753541Sshin			duplicate++;
137853541Sshin		}
137953541Sshin
138053541Sshin		if (duplicate) {
138153541Sshin			/* (*dp) will be freed in nd6_dad_duplicated() */
138253541Sshin			dp = NULL;
138353541Sshin			nd6_dad_duplicated(ifa);
138453541Sshin		} else {
138553541Sshin			/*
138653541Sshin			 * We are done with DAD.  No NA came, no NS came.
1387148987Sume			 * No duplicate address found.
138853541Sshin			 */
138953541Sshin			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
139053541Sshin
139178064Sume			nd6log((LOG_DEBUG,
139262587Sitojun			    "%s: DAD complete for %s - no duplicates found\n",
139362587Sitojun			    if_name(ifa->ifa_ifp),
1394165118Sbz			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
139553541Sshin
1396181803Sbz			TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
139753541Sshin			free(dp, M_IP6NDP);
139853541Sshin			dp = NULL;
1399194602Srwatson			ifa_free(ifa);
140053541Sshin		}
140153541Sshin	}
140253541Sshin
140353541Sshindone:
1404183550Szec	CURVNET_RESTORE();
140553541Sshin}
140653541Sshin
140753541Sshinvoid
1408171259Sdelphijnd6_dad_duplicated(struct ifaddr *ifa)
140953541Sshin{
141053541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1411151477Ssuz	struct ifnet *ifp;
141253541Sshin	struct dadq *dp;
1413165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
141453541Sshin
141553541Sshin	dp = nd6_dad_find(ifa);
141653541Sshin	if (dp == NULL) {
141762587Sitojun		log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
141853541Sshin		return;
141953541Sshin	}
142053541Sshin
142178064Sume	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
142278064Sume	    "NS in/out=%d/%d, NA in=%d\n",
1423165118Sbz	    if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
142478064Sume	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
142553541Sshin
142653541Sshin	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
142753541Sshin	ia->ia6_flags |= IN6_IFF_DUPLICATED;
142853541Sshin
1429148987Sume	/* We are done with DAD, with duplicate address found. (failure) */
143078064Sume	nd6_dad_stoptimer(dp);
143153541Sshin
1432151477Ssuz	ifp = ifa->ifa_ifp;
143362587Sitojun	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1434165118Sbz	    if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
143562587Sitojun	log(LOG_ERR, "%s: manual intervention required\n",
1436151477Ssuz	    if_name(ifp));
143753541Sshin
1438151477Ssuz	/*
1439151477Ssuz	 * If the address is a link-local address formed from an interface
1440151477Ssuz	 * identifier based on the hardware address which is supposed to be
1441151477Ssuz	 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1442151477Ssuz	 * operation on the interface SHOULD be disabled.
1443197138Shrs	 * [RFC 4862, Section 5.4.5]
1444151477Ssuz	 */
1445151477Ssuz	if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1446151477Ssuz		struct in6_addr in6;
1447151477Ssuz
1448151477Ssuz		/*
1449151477Ssuz		 * To avoid over-reaction, we only apply this logic when we are
1450151477Ssuz		 * very sure that hardware addresses are supposed to be unique.
1451151477Ssuz		 */
1452151477Ssuz		switch (ifp->if_type) {
1453151477Ssuz		case IFT_ETHER:
1454151477Ssuz		case IFT_FDDI:
1455151477Ssuz		case IFT_ATM:
1456151477Ssuz		case IFT_IEEE1394:
1457151477Ssuz#ifdef IFT_IEEE80211
1458151477Ssuz		case IFT_IEEE80211:
1459151477Ssuz#endif
1460219819Sjeff		case IFT_INFINIBAND:
1461151477Ssuz			in6 = ia->ia_addr.sin6_addr;
1462151477Ssuz			if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1463151477Ssuz			    IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1464151477Ssuz				ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1465151477Ssuz				log(LOG_ERR, "%s: possible hardware address "
1466151477Ssuz				    "duplication detected, disable IPv6\n",
1467151477Ssuz				    if_name(ifp));
1468151477Ssuz			}
1469151477Ssuz			break;
1470151477Ssuz		}
1471151477Ssuz	}
1472151477Ssuz
1473181803Sbz	TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
147453541Sshin	free(dp, M_IP6NDP);
147553541Sshin	dp = NULL;
1476194602Srwatson	ifa_free(ifa);
147753541Sshin}
147853541Sshin
147962587Sitojunstatic void
1480171259Sdelphijnd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
148162587Sitojun{
148262587Sitojun	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
148362587Sitojun	struct ifnet *ifp = ifa->ifa_ifp;
148462587Sitojun
148562587Sitojun	dp->dad_ns_tcount++;
148662587Sitojun	if ((ifp->if_flags & IFF_UP) == 0) {
148762587Sitojun		return;
148862587Sitojun	}
1489148887Srwatson	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
149062587Sitojun		return;
149162587Sitojun	}
149262587Sitojun
149362587Sitojun	dp->dad_ns_ocount++;
149462587Sitojun	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
149562587Sitojun}
149662587Sitojun
149762587Sitojunstatic void
1498171259Sdelphijnd6_dad_ns_input(struct ifaddr *ifa)
149953541Sshin{
150053541Sshin	struct in6_ifaddr *ia;
150153541Sshin	struct ifnet *ifp;
150278064Sume	const struct in6_addr *taddr6;
150353541Sshin	struct dadq *dp;
150453541Sshin	int duplicate;
150553541Sshin
1506151465Ssuz	if (ifa == NULL)
150753541Sshin		panic("ifa == NULL in nd6_dad_ns_input");
150853541Sshin
150953541Sshin	ia = (struct in6_ifaddr *)ifa;
151053541Sshin	ifp = ifa->ifa_ifp;
151153541Sshin	taddr6 = &ia->ia_addr.sin6_addr;
151253541Sshin	duplicate = 0;
151353541Sshin	dp = nd6_dad_find(ifa);
151453541Sshin
151553541Sshin	/* Quickhack - completely ignore DAD NS packets */
1516181803Sbz	if (V_dad_ignore_ns) {
1517165118Sbz		char ip6buf[INET6_ADDRSTRLEN];
151878064Sume		nd6log((LOG_INFO,
151978064Sume		    "nd6_dad_ns_input: ignoring DAD NS packet for "
1520165118Sbz		    "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6),
152178064Sume		    if_name(ifa->ifa_ifp)));
152253541Sshin		return;
152353541Sshin	}
152453541Sshin
152553541Sshin	/*
152653541Sshin	 * if I'm yet to start DAD, someone else started using this address
152753541Sshin	 * first.  I have a duplicate and you win.
152853541Sshin	 */
1529151465Ssuz	if (dp == NULL || dp->dad_ns_ocount == 0)
153053541Sshin		duplicate++;
153153541Sshin
153253541Sshin	/* XXX more checks for loopback situation - see nd6_dad_timer too */
153353541Sshin
153453541Sshin	if (duplicate) {
153553541Sshin		dp = NULL;	/* will be freed in nd6_dad_duplicated() */
153653541Sshin		nd6_dad_duplicated(ifa);
153753541Sshin	} else {
153853541Sshin		/*
153953541Sshin		 * not sure if I got a duplicate.
154053541Sshin		 * increment ns count and see what happens.
154153541Sshin		 */
154253541Sshin		if (dp)
154353541Sshin			dp->dad_ns_icount++;
154453541Sshin	}
154553541Sshin}
154653541Sshin
154762587Sitojunstatic void
1548171259Sdelphijnd6_dad_na_input(struct ifaddr *ifa)
154953541Sshin{
155053541Sshin	struct dadq *dp;
155153541Sshin
1552151465Ssuz	if (ifa == NULL)
155353541Sshin		panic("ifa == NULL in nd6_dad_na_input");
155453541Sshin
155553541Sshin	dp = nd6_dad_find(ifa);
155653541Sshin	if (dp)
155753541Sshin		dp->dad_na_icount++;
155853541Sshin
155953541Sshin	/* remove the address. */
156053541Sshin	nd6_dad_duplicated(ifa);
156153541Sshin}
1562