nd6_nbr.c revision 182150
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 182150 2008-08-25 06:09:32Z julian $");
34174510Sobrien
3562587Sitojun#include "opt_inet.h"
3662587Sitojun#include "opt_inet6.h"
37142215Sglebius#include "opt_ipsec.h"
38142215Sglebius#include "opt_carp.h"
39178167Sqingli#include "opt_mpath.h"
4055009Sshin
4153541Sshin#include <sys/param.h>
4253541Sshin#include <sys/systm.h>
4353541Sshin#include <sys/malloc.h>
4453541Sshin#include <sys/mbuf.h>
4553541Sshin#include <sys/socket.h>
4653541Sshin#include <sys/sockio.h>
4753541Sshin#include <sys/time.h>
4853541Sshin#include <sys/kernel.h>
4953541Sshin#include <sys/errno.h>
5053541Sshin#include <sys/syslog.h>
5153541Sshin#include <sys/queue.h>
5278064Sume#include <sys/callout.h>
53181803Sbz#include <sys/vimage.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>
6653541Sshin#include <netinet6/in6_var.h>
67151477Ssuz#include <netinet6/in6_ifattach.h>
6862587Sitojun#include <netinet/ip6.h>
6953541Sshin#include <netinet6/ip6_var.h>
70148385Sume#include <netinet6/scope6_var.h>
7153541Sshin#include <netinet6/nd6.h>
7262587Sitojun#include <netinet/icmp6.h>
7353541Sshin
74142215Sglebius#ifdef DEV_CARP
75142215Sglebius#include <netinet/ip_carp.h>
76142215Sglebius#endif
77142215Sglebius
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 *);
84175162Sobrienstatic void nd6_dad_timer(struct ifaddr *);
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
8962587Sitojunstatic int dad_ignore_ns = 0;	/* ignore NS in DAD - specwise incorrect*/
9062587Sitojunstatic int dad_maxtry = 15;	/* max # of *tries* to transmit DAD packet */
9153541Sshin
9253541Sshin/*
93108470Sschweikh * Input a Neighbor Solicitation Message.
9453541Sshin *
9553541Sshin * Based on RFC 2461
96148987Sume * Based on RFC 2462 (duplicate address detection)
9753541Sshin */
9853541Sshinvoid
99171259Sdelphijnd6_ns_input(struct mbuf *m, int off, int icmp6len)
10053541Sshin{
10153541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
10253541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
10362587Sitojun	struct nd_neighbor_solicit *nd_ns;
10453541Sshin	struct in6_addr saddr6 = ip6->ip6_src;
10553541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
10662587Sitojun	struct in6_addr taddr6;
10753541Sshin	struct in6_addr myaddr6;
10853541Sshin	char *lladdr = NULL;
109142215Sglebius	struct ifaddr *ifa = NULL;
11053541Sshin	int lladdrlen = 0;
11153541Sshin	int anycast = 0, proxy = 0, tentative = 0;
11253541Sshin	int tlladdr;
11353541Sshin	union nd_opts ndopts;
11462587Sitojun	struct sockaddr_dl *proxydl = NULL;
115165118Sbz	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
11653541Sshin
11778064Sume#ifndef PULLDOWN_TEST
11878064Sume	IP6_EXTHDR_CHECK(m, off, icmp6len,);
11978064Sume	nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
12078064Sume#else
12178064Sume	IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
12278064Sume	if (nd_ns == NULL) {
123181803Sbz		V_icmp6stat.icp6s_tooshort++;
12478064Sume		return;
12578064Sume	}
12678064Sume#endif
12778064Sume	ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
12878064Sume	taddr6 = nd_ns->nd_ns_target;
129148385Sume	if (in6_setscope(&taddr6, ifp, NULL) != 0)
130148385Sume		goto bad;
13178064Sume
13253541Sshin	if (ip6->ip6_hlim != 255) {
13378064Sume		nd6log((LOG_ERR,
13478064Sume		    "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
135165118Sbz		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
136165118Sbz		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
13778064Sume		goto bad;
13853541Sshin	}
13953541Sshin
14053541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
141148987Sume		/* dst has to be a solicited node multicast address. */
142120941Sume		if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
14395023Ssuz		    /* don't check ifindex portion */
144120941Sume		    daddr6.s6_addr32[1] == 0 &&
145120941Sume		    daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
146120941Sume		    daddr6.s6_addr8[12] == 0xff) {
14795023Ssuz			; /* good */
14853541Sshin		} else {
14978064Sume			nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
150120941Sume			    "(wrong ip6 dst)\n"));
15153541Sshin			goto bad;
15253541Sshin		}
15353541Sshin	}
15453541Sshin
15553541Sshin	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
15678064Sume		nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
15753541Sshin		goto bad;
15853541Sshin	}
15953541Sshin
16053541Sshin	icmp6len -= sizeof(*nd_ns);
16153541Sshin	nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
16253541Sshin	if (nd6_options(&ndopts) < 0) {
16378064Sume		nd6log((LOG_INFO,
16478064Sume		    "nd6_ns_input: invalid ND option, ignored\n"));
16578064Sume		/* nd6_options have incremented stats */
16678064Sume		goto freeit;
16753541Sshin	}
16853541Sshin
16953541Sshin	if (ndopts.nd_opts_src_lladdr) {
17095023Ssuz		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
17153541Sshin		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
17253541Sshin	}
173120941Sume
17453541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
17578064Sume		nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
17678064Sume		    "(link-layer address option)\n"));
17753541Sshin		goto bad;
17853541Sshin	}
17953541Sshin
18053541Sshin	/*
18153541Sshin	 * Attaching target link-layer address to the NA?
18253541Sshin	 * (RFC 2461 7.2.4)
18353541Sshin	 *
18453541Sshin	 * NS IP dst is unicast/anycast			MUST NOT add
18553541Sshin	 * NS IP dst is solicited-node multicast	MUST add
18653541Sshin	 *
18753541Sshin	 * In implementation, we add target link-layer address by default.
18853541Sshin	 * We do not add one in MUST NOT cases.
18953541Sshin	 */
19053541Sshin	if (!IN6_IS_ADDR_MULTICAST(&daddr6))
19153541Sshin		tlladdr = 0;
19253541Sshin	else
19353541Sshin		tlladdr = 1;
19453541Sshin
19553541Sshin	/*
19653541Sshin	 * Target address (taddr6) must be either:
19753541Sshin	 * (1) Valid unicast/anycast address for my receiving interface,
19853541Sshin	 * (2) Unicast address for which I'm offering proxy service, or
19953541Sshin	 * (3) "tentative" address on which DAD is being performed.
20053541Sshin	 */
20153541Sshin	/* (1) and (3) check. */
202142215Sglebius#ifdef DEV_CARP
203142215Sglebius	if (ifp->if_carp)
204142215Sglebius		ifa = carp_iamatch6(ifp->if_carp, &taddr6);
205151465Ssuz	if (ifa == NULL)
206142215Sglebius		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
207142215Sglebius#else
20853541Sshin	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
209142215Sglebius#endif
21053541Sshin
21153541Sshin	/* (2) check. */
212151465Ssuz	if (ifa == NULL) {
21353541Sshin		struct rtentry *rt;
21453541Sshin		struct sockaddr_in6 tsin6;
215121765Ssam		int need_proxy;
216178167Sqingli#ifdef RADIX_MPATH
217178167Sqingli		struct route_in6 ro;
218178167Sqingli#endif
21953541Sshin
220171260Sdelphij		bzero(&tsin6, sizeof tsin6);
22153541Sshin		tsin6.sin6_len = sizeof(struct sockaddr_in6);
22253541Sshin		tsin6.sin6_family = AF_INET6;
22353541Sshin		tsin6.sin6_addr = taddr6;
22453541Sshin
225178167Sqingli#ifdef RADIX_MPATH
226178167Sqingli		bzero(&ro, sizeof(ro));
227178167Sqingli		ro.ro_dst = tsin6;
228178167Sqingli		rtalloc_mpath((struct route *)&ro, RTF_ANNOUNCE);
229178167Sqingli		rt = ro.ro_rt;
230178167Sqingli#else
23153541Sshin		rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
232178167Sqingli#endif
233121765Ssam		need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
234121765Ssam		    rt->rt_gateway->sa_family == AF_LINK);
235121765Ssam		if (rt)
236121765Ssam			rtfree(rt);
237121765Ssam		if (need_proxy) {
23853541Sshin			/*
23962587Sitojun			 * proxy NDP for single entry
24053541Sshin			 */
24162587Sitojun			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
24262587Sitojun				IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
24362587Sitojun			if (ifa) {
24453541Sshin				proxy = 1;
24562587Sitojun				proxydl = SDL(rt->rt_gateway);
24662587Sitojun			}
24753541Sshin		}
24853541Sshin	}
249151479Ssuz	if (ifa == NULL) {
25053541Sshin		/*
25178064Sume		 * We've got an NS packet, and we don't have that adddress
25253541Sshin		 * assigned for us.  We MUST silently ignore it.
25353541Sshin		 * See RFC2461 7.2.3.
25453541Sshin		 */
25562587Sitojun		goto freeit;
25653541Sshin	}
25753541Sshin	myaddr6 = *IFA_IN6(ifa);
25853541Sshin	anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
25953541Sshin	tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
26053541Sshin	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
26162587Sitojun		goto freeit;
26253541Sshin
26353541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
264120941Sume		nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
26553541Sshin		    "(if %d, NS packet %d)\n",
266165118Sbz		    ip6_sprintf(ip6bufs, &taddr6),
267120941Sume		    ifp->if_addrlen, lladdrlen - 2));
26878064Sume		goto bad;
26953541Sshin	}
27053541Sshin
27153541Sshin	if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
272120941Sume		nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
273165118Sbz		    ip6_sprintf(ip6bufs, &saddr6)));
27462587Sitojun		goto freeit;
27553541Sshin	}
27653541Sshin
27753541Sshin	/*
27853541Sshin	 * We have neighbor solicitation packet, with target address equals to
27953541Sshin	 * one of my tentative address.
28053541Sshin	 *
28153541Sshin	 * src addr	how to process?
28253541Sshin	 * ---		---
28353541Sshin	 * multicast	of course, invalid (rejected in ip6_input)
28453541Sshin	 * unicast	somebody is doing address resolution -> ignore
28553541Sshin	 * unspec	dup address detection
28653541Sshin	 *
28753541Sshin	 * The processing is defined in RFC 2462.
28853541Sshin	 */
28953541Sshin	if (tentative) {
29053541Sshin		/*
29153541Sshin		 * If source address is unspecified address, it is for
292148987Sume		 * duplicate address detection.
29353541Sshin		 *
29453541Sshin		 * If not, the packet is for addess resolution;
29553541Sshin		 * silently ignore it.
29653541Sshin		 */
29753541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
29853541Sshin			nd6_dad_ns_input(ifa);
29953541Sshin
30062587Sitojun		goto freeit;
30153541Sshin	}
30253541Sshin
30353541Sshin	/*
30453541Sshin	 * If the source address is unspecified address, entries must not
30553541Sshin	 * be created or updated.
30653541Sshin	 * It looks that sender is performing DAD.  Output NA toward
30753541Sshin	 * all-node multicast address, to tell the sender that I'm using
30853541Sshin	 * the address.
30953541Sshin	 * S bit ("solicited") must be zero.
31053541Sshin	 */
31153541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
312148385Sume		struct in6_addr in6_all;
313148385Sume
314148385Sume		in6_all = in6addr_linklocal_allnodes;
315148385Sume		if (in6_setscope(&in6_all, ifp, NULL) != 0)
316148385Sume			goto bad;
317148385Sume		nd6_na_output(ifp, &in6_all, &taddr6,
318120941Sume		    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
319181803Sbz		    (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
320120941Sume		    tlladdr, (struct sockaddr *)proxydl);
32162587Sitojun		goto freeit;
32253541Sshin	}
32353541Sshin
324120941Sume	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
325120941Sume	    ND_NEIGHBOR_SOLICIT, 0);
32653541Sshin
32753541Sshin	nd6_na_output(ifp, &saddr6, &taddr6,
328120941Sume	    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
329181803Sbz	    (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
330120941Sume	    tlladdr, (struct sockaddr *)proxydl);
33162587Sitojun freeit:
33262587Sitojun	m_freem(m);
33353541Sshin	return;
33453541Sshin
33553541Sshin bad:
336165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
337165118Sbz		ip6_sprintf(ip6bufs, &saddr6)));
338165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
339165118Sbz		ip6_sprintf(ip6bufs, &daddr6)));
340165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
341165118Sbz		ip6_sprintf(ip6bufs, &taddr6)));
342181803Sbz	V_icmp6stat.icp6s_badns++;
34362587Sitojun	m_freem(m);
34453541Sshin}
34553541Sshin
34653541Sshin/*
347108470Sschweikh * Output a Neighbor Solicitation Message. Caller specifies:
34853541Sshin *	- ICMP6 header source IP6 address
34953541Sshin *	- ND6 header target IP6 address
35053541Sshin *	- ND6 header source datalink address
35153541Sshin *
35253541Sshin * Based on RFC 2461
353148987Sume * Based on RFC 2462 (duplicate address detection)
354171259Sdelphij *
355171259Sdelphij *   ln - for source address determination
356171259Sdelphij *  dad - duplicate address detection
35753541Sshin */
35853541Sshinvoid
359171259Sdelphijnd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
360171259Sdelphij    const struct in6_addr *taddr6, struct llinfo_nd6 *ln, int dad)
36153541Sshin{
36253541Sshin	struct mbuf *m;
36353541Sshin	struct ip6_hdr *ip6;
36453541Sshin	struct nd_neighbor_solicit *nd_ns;
365148385Sume	struct in6_addr *src, src_in;
36653541Sshin	struct ip6_moptions im6o;
36753541Sshin	int icmp6len;
36862587Sitojun	int maxlen;
36953541Sshin	caddr_t mac;
370148385Sume	struct route_in6 ro;
371120941Sume
372148385Sume	bzero(&ro, sizeof(ro));
373148385Sume
37453541Sshin	if (IN6_IS_ADDR_MULTICAST(taddr6))
37553541Sshin		return;
37653541Sshin
37762587Sitojun	/* estimate the size of message */
37862587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
37962587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
38062587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
38162587Sitojun#ifdef DIAGNOSTIC
38262587Sitojun		printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
38362587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
38462587Sitojun#endif
38553541Sshin		return;
38662587Sitojun	}
38753541Sshin
388111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
38962587Sitojun	if (m && max_linkhdr + maxlen >= MHLEN) {
390111119Simp		MCLGET(m, M_DONTWAIT);
39162587Sitojun		if ((m->m_flags & M_EXT) == 0) {
39262587Sitojun			m_free(m);
39362587Sitojun			m = NULL;
39462587Sitojun		}
39562587Sitojun	}
39662587Sitojun	if (m == NULL)
39762587Sitojun		return;
39878064Sume	m->m_pkthdr.rcvif = NULL;
39962587Sitojun
40053541Sshin	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
40153541Sshin		m->m_flags |= M_MCAST;
40253541Sshin		im6o.im6o_multicast_ifp = ifp;
40353541Sshin		im6o.im6o_multicast_hlim = 255;
40453541Sshin		im6o.im6o_multicast_loop = 0;
40553541Sshin	}
40653541Sshin
40753541Sshin	icmp6len = sizeof(*nd_ns);
40853541Sshin	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
40995023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
41053541Sshin
41153541Sshin	/* fill neighbor solicitation packet */
41253541Sshin	ip6 = mtod(m, struct ip6_hdr *);
41353541Sshin	ip6->ip6_flow = 0;
41462587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
41562587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
41653541Sshin	/* ip6->ip6_plen will be set later */
41753541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
41853541Sshin	ip6->ip6_hlim = 255;
41953541Sshin	if (daddr6)
42053541Sshin		ip6->ip6_dst = *daddr6;
42153541Sshin	else {
42253541Sshin		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
423148385Sume		ip6->ip6_dst.s6_addr16[1] = 0;
42453541Sshin		ip6->ip6_dst.s6_addr32[1] = 0;
42553541Sshin		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
42653541Sshin		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
42753541Sshin		ip6->ip6_dst.s6_addr8[12] = 0xff;
428148385Sume		if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
429148385Sume			goto bad;
43053541Sshin	}
43153541Sshin	if (!dad) {
43253541Sshin		/*
43353541Sshin		 * RFC2461 7.2.2:
43453541Sshin		 * "If the source address of the packet prompting the
43553541Sshin		 * solicitation is the same as one of the addresses assigned
43653541Sshin		 * to the outgoing interface, that address SHOULD be placed
43753541Sshin		 * in the IP Source Address of the outgoing solicitation.
43853541Sshin		 * Otherwise, any one of the addresses assigned to the
43953541Sshin		 * interface should be used."
44053541Sshin		 *
44153541Sshin		 * We use the source address for the prompting packet
44253541Sshin		 * (saddr6), if:
44353541Sshin		 * - saddr6 is given from the caller (by giving "ln"), and
44453541Sshin		 * - saddr6 belongs to the outgoing interface.
445148385Sume		 * Otherwise, we perform the source address selection as usual.
44653541Sshin		 */
44795023Ssuz		struct ip6_hdr *hip6;		/* hold ip6 */
448148385Sume		struct in6_addr *hsrc = NULL;
44953541Sshin
45053541Sshin		if (ln && ln->ln_hold) {
451148987Sume			/*
452148987Sume			 * assuming every packet in ln_hold has the same IP
453148987Sume			 * header
454148987Sume			 */
45553541Sshin			hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
45653541Sshin			/* XXX pullup? */
45753541Sshin			if (sizeof(*hip6) < ln->ln_hold->m_len)
458148385Sume				hsrc = &hip6->ip6_src;
45953541Sshin			else
460148385Sume				hsrc = NULL;
461148385Sume		}
462148385Sume		if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc))
463148385Sume			src = hsrc;
46453541Sshin		else {
465148385Sume			int error;
466148385Sume			struct sockaddr_in6 dst_sa;
467148385Sume
468148385Sume			bzero(&dst_sa, sizeof(dst_sa));
469148385Sume			dst_sa.sin6_family = AF_INET6;
470148385Sume			dst_sa.sin6_len = sizeof(dst_sa);
471148385Sume			dst_sa.sin6_addr = ip6->ip6_dst;
472148385Sume
473148385Sume			src = in6_selectsrc(&dst_sa, NULL,
474148385Sume			    NULL, &ro, NULL, NULL, &error);
475148385Sume			if (src == NULL) {
476165118Sbz				char ip6buf[INET6_ADDRSTRLEN];
477148385Sume				nd6log((LOG_DEBUG,
478148385Sume				    "nd6_ns_output: source can't be "
479148385Sume				    "determined: dst=%s, error=%d\n",
480165118Sbz				    ip6_sprintf(ip6buf, &dst_sa.sin6_addr),
481165118Sbz				    error));
482148385Sume				goto bad;
48353541Sshin			}
48453541Sshin		}
48553541Sshin	} else {
48653541Sshin		/*
48753541Sshin		 * Source address for DAD packet must always be IPv6
48853541Sshin		 * unspecified address. (0::0)
489148385Sume		 * We actually don't have to 0-clear the address (we did it
490148385Sume		 * above), but we do so here explicitly to make the intention
491148385Sume		 * clearer.
49253541Sshin		 */
493148385Sume		bzero(&src_in, sizeof(src_in));
494148385Sume		src = &src_in;
49553541Sshin	}
496148385Sume	ip6->ip6_src = *src;
49753541Sshin	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
49853541Sshin	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
49953541Sshin	nd_ns->nd_ns_code = 0;
50053541Sshin	nd_ns->nd_ns_reserved = 0;
50153541Sshin	nd_ns->nd_ns_target = *taddr6;
502121315Sume	in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
50353541Sshin
50453541Sshin	/*
50553541Sshin	 * Add source link-layer address option.
50653541Sshin	 *
50753541Sshin	 *				spec		implementation
50853541Sshin	 *				---		---
50953541Sshin	 * DAD packet			MUST NOT	do not add the option
51053541Sshin	 * there's no link layer address:
51153541Sshin	 *				impossible	do not add the option
51253541Sshin	 * there's link layer address:
51353541Sshin	 *	Multicast NS		MUST add one	add the option
51453541Sshin	 *	Unicast NS		SHOULD add one	add the option
51553541Sshin	 */
51653541Sshin	if (!dad && (mac = nd6_ifptomac(ifp))) {
51753541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
51853541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
51953541Sshin		/* 8 byte alignments... */
52053541Sshin		optlen = (optlen + 7) & ~7;
521120941Sume
52253541Sshin		m->m_pkthdr.len += optlen;
52353541Sshin		m->m_len += optlen;
52453541Sshin		icmp6len += optlen;
52553541Sshin		bzero((caddr_t)nd_opt, optlen);
52653541Sshin		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
52753541Sshin		nd_opt->nd_opt_len = optlen >> 3;
52853541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
52953541Sshin	}
53053541Sshin
53153541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
53253541Sshin	nd_ns->nd_ns_cksum = 0;
533120941Sume	nd_ns->nd_ns_cksum =
534120941Sume	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
53553541Sshin
536151536Ssuz	ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
537148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_msg);
538148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
539181803Sbz	V_icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
540148385Sume
541148385Sume	if (ro.ro_rt) {		/* we don't cache this route. */
542148385Sume		RTFREE(ro.ro_rt);
54353541Sshin	}
544148385Sume	return;
545148385Sume
546148385Sume  bad:
547148385Sume	if (ro.ro_rt) {
548148385Sume		RTFREE(ro.ro_rt);
549148385Sume	}
550148385Sume	m_freem(m);
551148385Sume	return;
55253541Sshin}
55353541Sshin
55453541Sshin/*
55553541Sshin * Neighbor advertisement input handling.
55653541Sshin *
55753541Sshin * Based on RFC 2461
558148987Sume * Based on RFC 2462 (duplicate address detection)
55962587Sitojun *
56062587Sitojun * the following items are not implemented yet:
56162587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
56262587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
56353541Sshin */
56453541Sshinvoid
565171259Sdelphijnd6_na_input(struct mbuf *m, int off, int icmp6len)
56653541Sshin{
56753541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
56853541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
56962587Sitojun	struct nd_neighbor_advert *nd_na;
57053541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
57162587Sitojun	struct in6_addr taddr6;
57262587Sitojun	int flags;
57362587Sitojun	int is_router;
57462587Sitojun	int is_solicited;
57562587Sitojun	int is_override;
57653541Sshin	char *lladdr = NULL;
57753541Sshin	int lladdrlen = 0;
57853541Sshin	struct ifaddr *ifa;
57953541Sshin	struct llinfo_nd6 *ln;
58053541Sshin	struct rtentry *rt;
58153541Sshin	struct sockaddr_dl *sdl;
58253541Sshin	union nd_opts ndopts;
583165118Sbz	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
58453541Sshin
58553541Sshin	if (ip6->ip6_hlim != 255) {
58678064Sume		nd6log((LOG_ERR,
58778064Sume		    "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
588165118Sbz		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
589165118Sbz		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
59078064Sume		goto bad;
59162587Sitojun	}
59262587Sitojun
59362587Sitojun#ifndef PULLDOWN_TEST
59462587Sitojun	IP6_EXTHDR_CHECK(m, off, icmp6len,);
59562587Sitojun	nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
59662587Sitojun#else
59762587Sitojun	IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
59862587Sitojun	if (nd_na == NULL) {
599181803Sbz		V_icmp6stat.icp6s_tooshort++;
60053541Sshin		return;
60153541Sshin	}
60262587Sitojun#endif
603148385Sume
60462587Sitojun	flags = nd_na->nd_na_flags_reserved;
60562587Sitojun	is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
60662587Sitojun	is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
60762587Sitojun	is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
60853541Sshin
609148385Sume	taddr6 = nd_na->nd_na_target;
610148385Sume	if (in6_setscope(&taddr6, ifp, NULL))
611150202Ssuz		goto bad;	/* XXX: impossible */
61253541Sshin
61353541Sshin	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
61478064Sume		nd6log((LOG_ERR,
61553541Sshin		    "nd6_na_input: invalid target address %s\n",
616165118Sbz		    ip6_sprintf(ip6bufs, &taddr6)));
61778064Sume		goto bad;
61853541Sshin	}
61953541Sshin	if (IN6_IS_ADDR_MULTICAST(&daddr6))
62053541Sshin		if (is_solicited) {
62178064Sume			nd6log((LOG_ERR,
62278064Sume			    "nd6_na_input: a solicited adv is multicasted\n"));
62378064Sume			goto bad;
62453541Sshin		}
62553541Sshin
62653541Sshin	icmp6len -= sizeof(*nd_na);
62753541Sshin	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
62853541Sshin	if (nd6_options(&ndopts) < 0) {
62978064Sume		nd6log((LOG_INFO,
63078064Sume		    "nd6_na_input: invalid ND option, ignored\n"));
63178064Sume		/* nd6_options have incremented stats */
63262587Sitojun		goto freeit;
63353541Sshin	}
63453541Sshin
63553541Sshin	if (ndopts.nd_opts_tgt_lladdr) {
63653541Sshin		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
63753541Sshin		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
63853541Sshin	}
63953541Sshin
64053541Sshin	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
64153541Sshin
64253541Sshin	/*
64353541Sshin	 * Target address matches one of my interface address.
64453541Sshin	 *
64553541Sshin	 * If my address is tentative, this means that there's somebody
64653541Sshin	 * already using the same address as mine.  This indicates DAD failure.
64753541Sshin	 * This is defined in RFC 2462.
64853541Sshin	 *
64953541Sshin	 * Otherwise, process as defined in RFC 2461.
65053541Sshin	 */
65153541Sshin	if (ifa
65253541Sshin	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
65353541Sshin		nd6_dad_na_input(ifa);
65462587Sitojun		goto freeit;
65553541Sshin	}
65653541Sshin
65795023Ssuz	/* Just for safety, maybe unnecessary. */
65853541Sshin	if (ifa) {
65953541Sshin		log(LOG_ERR,
66053541Sshin		    "nd6_na_input: duplicate IP6 address %s\n",
661165118Sbz		    ip6_sprintf(ip6bufs, &taddr6));
66262587Sitojun		goto freeit;
66353541Sshin	}
66453541Sshin
66553541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
666120941Sume		nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
667165118Sbz		    "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
668120941Sume		    ifp->if_addrlen, lladdrlen - 2));
66978064Sume		goto bad;
67053541Sshin	}
67153541Sshin
67253541Sshin	/*
673120941Sume	 * If no neighbor cache entry is found, NA SHOULD silently be
674120941Sume	 * discarded.
67553541Sshin	 */
67653541Sshin	rt = nd6_lookup(&taddr6, 0, ifp);
67753541Sshin	if ((rt == NULL) ||
67853541Sshin	   ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
67953541Sshin	   ((sdl = SDL(rt->rt_gateway)) == NULL))
68062587Sitojun		goto freeit;
68153541Sshin
68253541Sshin	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
68353541Sshin		/*
68453541Sshin		 * If the link-layer has address, and no lladdr option came,
68553541Sshin		 * discard the packet.
68653541Sshin		 */
687151465Ssuz		if (ifp->if_addrlen && lladdr == NULL)
68862587Sitojun			goto freeit;
68953541Sshin
69053541Sshin		/*
69153541Sshin		 * Record link-layer address, and update the state.
69253541Sshin		 */
69353541Sshin		sdl->sdl_alen = ifp->if_addrlen;
69453541Sshin		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
69553541Sshin		if (is_solicited) {
69653541Sshin			ln->ln_state = ND6_LLINFO_REACHABLE;
69762587Sitojun			ln->ln_byhint = 0;
698151539Ssuz			if (!ND6_LLINFO_PERMANENT(ln)) {
699151539Ssuz				nd6_llinfo_settimer(ln,
700151539Ssuz				    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
701120941Sume			}
70278064Sume		} else {
70353541Sshin			ln->ln_state = ND6_LLINFO_STALE;
704181803Sbz			nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
70578064Sume		}
70678064Sume		if ((ln->ln_router = is_router) != 0) {
70778064Sume			/*
70878064Sume			 * This means a router's state has changed from
70978064Sume			 * non-reachable to probably reachable, and might
71078064Sume			 * affect the status of associated prefixes..
71178064Sume			 */
71278064Sume			pfxlist_onlink_check();
71378064Sume		}
71453541Sshin	} else {
71553541Sshin		int llchange;
71653541Sshin
71753541Sshin		/*
71853541Sshin		 * Check if the link-layer address has changed or not.
71953541Sshin		 */
720151465Ssuz		if (lladdr == NULL)
72153541Sshin			llchange = 0;
72253541Sshin		else {
72353541Sshin			if (sdl->sdl_alen) {
72453541Sshin				if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
72553541Sshin					llchange = 1;
72653541Sshin				else
72753541Sshin					llchange = 0;
72853541Sshin			} else
72953541Sshin				llchange = 1;
73053541Sshin		}
73153541Sshin
73253541Sshin		/*
73353541Sshin		 * This is VERY complex.  Look at it with care.
73453541Sshin		 *
73553541Sshin		 * override solicit lladdr llchange	action
73653541Sshin		 *					(L: record lladdr)
73753541Sshin		 *
73853541Sshin		 *	0	0	n	--	(2c)
73953541Sshin		 *	0	0	y	n	(2b) L
74053541Sshin		 *	0	0	y	y	(1)    REACHABLE->STALE
74153541Sshin		 *	0	1	n	--	(2c)   *->REACHABLE
74253541Sshin		 *	0	1	y	n	(2b) L *->REACHABLE
74353541Sshin		 *	0	1	y	y	(1)    REACHABLE->STALE
74453541Sshin		 *	1	0	n	--	(2a)
74553541Sshin		 *	1	0	y	n	(2a) L
74653541Sshin		 *	1	0	y	y	(2a) L *->STALE
74753541Sshin		 *	1	1	n	--	(2a)   *->REACHABLE
74853541Sshin		 *	1	1	y	n	(2a) L *->REACHABLE
74953541Sshin		 *	1	1	y	y	(2a) L *->REACHABLE
75053541Sshin		 */
751151539Ssuz		if (!is_override && (lladdr != NULL && llchange)) {  /* (1) */
75253541Sshin			/*
75353541Sshin			 * If state is REACHABLE, make it STALE.
75453541Sshin			 * no other updates should be done.
75553541Sshin			 */
75678064Sume			if (ln->ln_state == ND6_LLINFO_REACHABLE) {
75753541Sshin				ln->ln_state = ND6_LLINFO_STALE;
758181803Sbz				nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
75978064Sume			}
76062587Sitojun			goto freeit;
76153541Sshin		} else if (is_override				   /* (2a) */
762151465Ssuz			|| (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
763151465Ssuz			|| lladdr == NULL) {			   /* (2c) */
76453541Sshin			/*
76553541Sshin			 * Update link-local address, if any.
76653541Sshin			 */
767151465Ssuz			if (lladdr != NULL) {
76853541Sshin				sdl->sdl_alen = ifp->if_addrlen;
76953541Sshin				bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
77053541Sshin			}
77153541Sshin
77253541Sshin			/*
77353541Sshin			 * If solicited, make the state REACHABLE.
77453541Sshin			 * If not solicited and the link-layer address was
77553541Sshin			 * changed, make it STALE.
77653541Sshin			 */
77753541Sshin			if (is_solicited) {
77853541Sshin				ln->ln_state = ND6_LLINFO_REACHABLE;
77962587Sitojun				ln->ln_byhint = 0;
780151539Ssuz				if (!ND6_LLINFO_PERMANENT(ln)) {
781151539Ssuz					nd6_llinfo_settimer(ln,
782151539Ssuz					    (long)ND_IFINFO(ifp)->reachable * hz);
78353541Sshin				}
78453541Sshin			} else {
785151465Ssuz				if (lladdr != NULL && llchange) {
78653541Sshin					ln->ln_state = ND6_LLINFO_STALE;
787151539Ssuz					nd6_llinfo_settimer(ln,
788181803Sbz					    (long)V_nd6_gctimer * hz);
78978064Sume				}
79053541Sshin			}
79153541Sshin		}
79253541Sshin
79353541Sshin		if (ln->ln_router && !is_router) {
79453541Sshin			/*
79553541Sshin			 * The peer dropped the router flag.
79653541Sshin			 * Remove the sender from the Default Router List and
79753541Sshin			 * update the Destination Cache entries.
79853541Sshin			 */
79953541Sshin			struct nd_defrouter *dr;
80053541Sshin			struct in6_addr *in6;
80153541Sshin			int s;
80253541Sshin
80353541Sshin			in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
80495023Ssuz
80595023Ssuz			/*
80695023Ssuz			 * Lock to protect the default router list.
80795023Ssuz			 * XXX: this might be unnecessary, since this function
80895023Ssuz			 * is only called under the network software interrupt
809120941Sume			 * context.  However, we keep it just for safety.
81095023Ssuz			 */
81153541Sshin			s = splnet();
812128422Sluigi			dr = defrouter_lookup(in6, ifp);
81353541Sshin			if (dr)
81453541Sshin				defrtrlist_del(dr);
815181803Sbz			else if (!V_ip6_forwarding) {
81653541Sshin				/*
81753541Sshin				 * Even if the neighbor is not in the default
81853541Sshin				 * router list, the neighbor may be used
81953541Sshin				 * as a next hop for some destinations
82053541Sshin				 * (e.g. redirect case). So we must
82153541Sshin				 * call rt6_flush explicitly.
82253541Sshin				 */
823128422Sluigi				rt6_flush(&ip6->ip6_src, ifp);
82453541Sshin			}
82553541Sshin			splx(s);
82653541Sshin		}
82753541Sshin		ln->ln_router = is_router;
82853541Sshin	}
82953541Sshin	rt->rt_flags &= ~RTF_REJECT;
83053541Sshin	ln->ln_asked = 0;
83153541Sshin	if (ln->ln_hold) {
832151539Ssuz		struct mbuf *m_hold, *m_hold_next;
833151539Ssuz
834169273Ssuz		/*
835169273Ssuz		 * reset the ln_hold in advance, to explicitly
836169273Ssuz		 * prevent a ln_hold lookup in nd6_output()
837169273Ssuz		 * (wouldn't happen, though...)
838169273Ssuz		 */
839171133Sgnn		for (m_hold = ln->ln_hold;
840169241Ssuz		    m_hold; m_hold = m_hold_next) {
841151539Ssuz			m_hold_next = m_hold->m_nextpkt;
842169241Ssuz			m_hold->m_nextpkt = NULL;
843151539Ssuz			/*
844151539Ssuz			 * we assume ifp is not a loopback here, so just set
845151539Ssuz			 * the 2nd argument as the 1st one.
846151539Ssuz			 */
847169241Ssuz			nd6_output(ifp, ifp, m_hold,
848151539Ssuz			    (struct sockaddr_in6 *)rt_key(rt), rt);
849151539Ssuz		}
850171133Sgnn		ln->ln_hold = NULL;
85153541Sshin	}
85262587Sitojun
85362587Sitojun freeit:
85462587Sitojun	m_freem(m);
85578064Sume	return;
85678064Sume
85778064Sume bad:
858181803Sbz	V_icmp6stat.icp6s_badna++;
85978064Sume	m_freem(m);
86053541Sshin}
86153541Sshin
86253541Sshin/*
86353541Sshin * Neighbor advertisement output handling.
86453541Sshin *
86553541Sshin * Based on RFC 2461
86653541Sshin *
86762587Sitojun * the following items are not implemented yet:
86862587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
86962587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
870171259Sdelphij *
871171259Sdelphij * tlladdr - 1 if include target link-layer address
872171259Sdelphij * sdl0 - sockaddr_dl (= proxy NA) or NULL
87353541Sshin */
87453541Sshinvoid
875171259Sdelphijnd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
876171259Sdelphij    const struct in6_addr *taddr6, u_long flags, int tlladdr,
877171259Sdelphij    struct sockaddr *sdl0)
87853541Sshin{
87953541Sshin	struct mbuf *m;
88053541Sshin	struct ip6_hdr *ip6;
88153541Sshin	struct nd_neighbor_advert *nd_na;
88253541Sshin	struct ip6_moptions im6o;
883148385Sume	struct in6_addr *src, daddr6;
884148385Sume	struct sockaddr_in6 dst_sa;
885148385Sume	int icmp6len, maxlen, error;
88692733Speter	caddr_t mac = NULL;
887148385Sume	struct route_in6 ro;
88862587Sitojun
889148385Sume	bzero(&ro, sizeof(ro));
890148385Sume
891148385Sume	daddr6 = *daddr6_0;	/* make a local copy for modification */
892148385Sume
89362587Sitojun	/* estimate the size of message */
89462587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_na);
89562587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
89662587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
89762587Sitojun#ifdef DIAGNOSTIC
89862587Sitojun		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
89962587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
90062587Sitojun#endif
90153541Sshin		return;
90262587Sitojun	}
90353541Sshin
904111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
90562587Sitojun	if (m && max_linkhdr + maxlen >= MHLEN) {
906111119Simp		MCLGET(m, M_DONTWAIT);
90762587Sitojun		if ((m->m_flags & M_EXT) == 0) {
90862587Sitojun			m_free(m);
90962587Sitojun			m = NULL;
91062587Sitojun		}
91162587Sitojun	}
91262587Sitojun	if (m == NULL)
91362587Sitojun		return;
91478064Sume	m->m_pkthdr.rcvif = NULL;
91562587Sitojun
916148385Sume	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
91753541Sshin		m->m_flags |= M_MCAST;
91853541Sshin		im6o.im6o_multicast_ifp = ifp;
91953541Sshin		im6o.im6o_multicast_hlim = 255;
92053541Sshin		im6o.im6o_multicast_loop = 0;
92153541Sshin	}
92253541Sshin
92353541Sshin	icmp6len = sizeof(*nd_na);
92453541Sshin	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
92595023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
92653541Sshin
92753541Sshin	/* fill neighbor advertisement packet */
92853541Sshin	ip6 = mtod(m, struct ip6_hdr *);
92953541Sshin	ip6->ip6_flow = 0;
93062587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
93162587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
93253541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
93353541Sshin	ip6->ip6_hlim = 255;
934148385Sume	if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
93553541Sshin		/* reply to DAD */
936153227Sume		daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
937153227Sume		daddr6.s6_addr16[1] = 0;
938153227Sume		daddr6.s6_addr32[1] = 0;
939153227Sume		daddr6.s6_addr32[2] = 0;
940153227Sume		daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
941148385Sume		if (in6_setscope(&daddr6, ifp, NULL))
942148385Sume			goto bad;
943148385Sume
94453541Sshin		flags &= ~ND_NA_FLAG_SOLICITED;
945148385Sume	}
946148385Sume	ip6->ip6_dst = daddr6;
947148385Sume	bzero(&dst_sa, sizeof(struct sockaddr_in6));
948148385Sume	dst_sa.sin6_family = AF_INET6;
949148385Sume	dst_sa.sin6_len = sizeof(struct sockaddr_in6);
950148385Sume	dst_sa.sin6_addr = daddr6;
95153541Sshin
95253541Sshin	/*
95353541Sshin	 * Select a source whose scope is the same as that of the dest.
95453541Sshin	 */
955148385Sume	bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
956148385Sume	src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &error);
957148385Sume	if (src == NULL) {
958165118Sbz		char ip6buf[INET6_ADDRSTRLEN];
959148385Sume		nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
960148385Sume		    "determined: dst=%s, error=%d\n",
961165118Sbz		    ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
962148385Sume		goto bad;
96353541Sshin	}
964148385Sume	ip6->ip6_src = *src;
96553541Sshin	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
96653541Sshin	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
96753541Sshin	nd_na->nd_na_code = 0;
96853541Sshin	nd_na->nd_na_target = *taddr6;
969121315Sume	in6_clearscope(&nd_na->nd_na_target); /* XXX */
97053541Sshin
97153541Sshin	/*
97253541Sshin	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
97353541Sshin	 * see nd6_ns_input() for details.
97453541Sshin	 * Basically, if NS packet is sent to unicast/anycast addr,
97553541Sshin	 * target lladdr option SHOULD NOT be included.
97653541Sshin	 */
97762587Sitojun	if (tlladdr) {
97862587Sitojun		/*
97962587Sitojun		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
98062587Sitojun		 * lladdr in sdl0.  If we are not proxying (sending NA for
98162587Sitojun		 * my address) use lladdr configured for the interface.
98262587Sitojun		 */
983142215Sglebius		if (sdl0 == NULL) {
984142215Sglebius#ifdef DEV_CARP
985142215Sglebius			if (ifp->if_carp)
986142215Sglebius				mac = carp_macmatch6(ifp->if_carp, m, taddr6);
987142215Sglebius			if (mac == NULL)
988142215Sglebius				mac = nd6_ifptomac(ifp);
989142215Sglebius#else
99062587Sitojun			mac = nd6_ifptomac(ifp);
991142215Sglebius#endif
992142215Sglebius		} else if (sdl0->sa_family == AF_LINK) {
99362587Sitojun			struct sockaddr_dl *sdl;
99462587Sitojun			sdl = (struct sockaddr_dl *)sdl0;
99562587Sitojun			if (sdl->sdl_alen == ifp->if_addrlen)
99662587Sitojun				mac = LLADDR(sdl);
99762587Sitojun		}
99862587Sitojun	}
99962587Sitojun	if (tlladdr && mac) {
100053541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
100153541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1002120941Sume
100353541Sshin		/* roundup to 8 bytes alignment! */
100453541Sshin		optlen = (optlen + 7) & ~7;
100553541Sshin
100653541Sshin		m->m_pkthdr.len += optlen;
100753541Sshin		m->m_len += optlen;
100853541Sshin		icmp6len += optlen;
100953541Sshin		bzero((caddr_t)nd_opt, optlen);
101053541Sshin		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
101153541Sshin		nd_opt->nd_opt_len = optlen >> 3;
101253541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
101353541Sshin	} else
101453541Sshin		flags &= ~ND_NA_FLAG_OVERRIDE;
101553541Sshin
101653541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
101753541Sshin	nd_na->nd_na_flags_reserved = flags;
101853541Sshin	nd_na->nd_na_cksum = 0;
101953541Sshin	nd_na->nd_na_cksum =
1020120941Sume	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
102153541Sshin
1022148385Sume	ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1023148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_msg);
1024148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1025181803Sbz	V_icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
1026148385Sume
1027148385Sume	if (ro.ro_rt) {		/* we don't cache this route. */
1028148385Sume		RTFREE(ro.ro_rt);
102953541Sshin	}
1030148385Sume	return;
1031148385Sume
1032148385Sume  bad:
1033148385Sume	if (ro.ro_rt) {
1034148385Sume		RTFREE(ro.ro_rt);
1035148385Sume	}
1036148385Sume	m_freem(m);
1037148385Sume	return;
103853541Sshin}
103953541Sshin
104053541Sshincaddr_t
1041171259Sdelphijnd6_ifptomac(struct ifnet *ifp)
104253541Sshin{
104353541Sshin	switch (ifp->if_type) {
104453541Sshin	case IFT_ARCNET:
104553541Sshin	case IFT_ETHER:
104653541Sshin	case IFT_FDDI:
104778064Sume	case IFT_IEEE1394:
104878468Ssumikawa#ifdef IFT_L2VLAN
104978468Ssumikawa	case IFT_L2VLAN:
105078468Ssumikawa#endif
105178064Sume#ifdef IFT_IEEE80211
105278064Sume	case IFT_IEEE80211:
105378064Sume#endif
1054142215Sglebius#ifdef IFT_CARP
1055142215Sglebius	case IFT_CARP:
1056142215Sglebius#endif
1057149829Sthompsa	case IFT_BRIDGE:
1058120049Smdodd	case IFT_ISO88025:
1059147306Sbrooks		return IF_LLADDR(ifp);
106053541Sshin	default:
106153541Sshin		return NULL;
106253541Sshin	}
106353541Sshin}
106453541Sshin
106560938SjakeTAILQ_HEAD(dadq_head, dadq);
106653541Sshinstruct dadq {
106760938Sjake	TAILQ_ENTRY(dadq) dad_list;
106853541Sshin	struct ifaddr *dad_ifa;
106953541Sshin	int dad_count;		/* max NS to send */
107062587Sitojun	int dad_ns_tcount;	/* # of trials to send NS */
107153541Sshin	int dad_ns_ocount;	/* NS sent so far */
107253541Sshin	int dad_ns_icount;
107353541Sshin	int dad_na_icount;
107478064Sume	struct callout dad_timer_ch;
107553541Sshin};
107653541Sshin
107753541Sshinstatic struct dadq_head dadq;
107878064Sumestatic int dad_init = 0;
107953541Sshin
108053541Sshinstatic struct dadq *
1081171259Sdelphijnd6_dad_find(struct ifaddr *ifa)
108253541Sshin{
108353541Sshin	struct dadq *dp;
108453541Sshin
1085181803Sbz	for (dp = V_dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
108653541Sshin		if (dp->dad_ifa == ifa)
108753541Sshin			return dp;
108853541Sshin	}
108953541Sshin	return NULL;
109053541Sshin}
109153541Sshin
109278064Sumestatic void
1093171259Sdelphijnd6_dad_starttimer(struct dadq *dp, int ticks)
109478064Sume{
109578064Sume
109678064Sume	callout_reset(&dp->dad_timer_ch, ticks,
1097175162Sobrien	    (void (*)(void *))nd6_dad_timer, (void *)dp->dad_ifa);
109878064Sume}
109978064Sume
110078064Sumestatic void
1101171259Sdelphijnd6_dad_stoptimer(struct dadq *dp)
110278064Sume{
110378064Sume
110478064Sume	callout_stop(&dp->dad_timer_ch);
110578064Sume}
110678064Sume
110753541Sshin/*
1108148987Sume * Start Duplicate Address Detection (DAD) for specified interface address.
110953541Sshin */
111053541Sshinvoid
1111171259Sdelphijnd6_dad_start(struct ifaddr *ifa, int delay)
111253541Sshin{
111353541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
111453541Sshin	struct dadq *dp;
1115165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
111653541Sshin
1117181803Sbz	if (!V_dad_init) {
1118181803Sbz		TAILQ_INIT(&V_dadq);
1119181803Sbz		V_dad_init++;
112053541Sshin	}
112153541Sshin
112253541Sshin	/*
112353541Sshin	 * If we don't need DAD, don't do it.
112453541Sshin	 * There are several cases:
112553541Sshin	 * - DAD is disabled (ip6_dad_count == 0)
112653541Sshin	 * - the interface address is anycast
112753541Sshin	 */
112853541Sshin	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
112962587Sitojun		log(LOG_DEBUG,
113062587Sitojun			"nd6_dad_start: called with non-tentative address "
113153541Sshin			"%s(%s)\n",
1132165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
113353541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
113453541Sshin		return;
113553541Sshin	}
113653541Sshin	if (ia->ia6_flags & IN6_IFF_ANYCAST) {
113753541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
113853541Sshin		return;
113953541Sshin	}
1140181803Sbz	if (!V_ip6_dad_count) {
114153541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
114253541Sshin		return;
114353541Sshin	}
1144151465Ssuz	if (ifa->ifa_ifp == NULL)
114553541Sshin		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1146120941Sume	if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
114753541Sshin		return;
1148120941Sume	}
114953541Sshin	if (nd6_dad_find(ifa) != NULL) {
115053541Sshin		/* DAD already in progress */
115153541Sshin		return;
115253541Sshin	}
115353541Sshin
115453541Sshin	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
115553541Sshin	if (dp == NULL) {
115662587Sitojun		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
115753541Sshin			"%s(%s)\n",
1158165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
115953541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
116053541Sshin		return;
116153541Sshin	}
116253541Sshin	bzero(dp, sizeof(*dp));
116378064Sume	callout_init(&dp->dad_timer_ch, 0);
1164182150Sjulian	TAILQ_INSERT_TAIL(&V_dadq, (struct dadq *)dp, dad_list);
116553541Sshin
116678064Sume	nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1167165118Sbz	    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
116853541Sshin
116953541Sshin	/*
117053541Sshin	 * Send NS packet for DAD, ip6_dad_count times.
117153541Sshin	 * Note that we must delay the first transmission, if this is the
117253541Sshin	 * first packet to be sent from the interface after interface
117353541Sshin	 * (re)initialization.
117453541Sshin	 */
117553541Sshin	dp->dad_ifa = ifa;
117695023Ssuz	IFAREF(ifa);	/* just for safety */
1177181803Sbz	dp->dad_count = V_ip6_dad_count;
117853541Sshin	dp->dad_ns_icount = dp->dad_na_icount = 0;
117962587Sitojun	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1180151539Ssuz	if (delay == 0) {
118162587Sitojun		nd6_dad_ns_output(dp, ifa);
1182121161Sume		nd6_dad_starttimer(dp,
1183151539Ssuz		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
118453541Sshin	} else {
1185151539Ssuz		nd6_dad_starttimer(dp, delay);
118653541Sshin	}
118753541Sshin}
118853541Sshin
118978064Sume/*
119078064Sume * terminate DAD unconditionally.  used for address removals.
119178064Sume */
119278064Sumevoid
1193171259Sdelphijnd6_dad_stop(struct ifaddr *ifa)
119478064Sume{
119578064Sume	struct dadq *dp;
119678064Sume
1197181803Sbz	if (!V_dad_init)
119878064Sume		return;
119978064Sume	dp = nd6_dad_find(ifa);
120078064Sume	if (!dp) {
120178064Sume		/* DAD wasn't started yet */
120278064Sume		return;
120378064Sume	}
120478064Sume
120578064Sume	nd6_dad_stoptimer(dp);
120678064Sume
1207181803Sbz	TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
120878064Sume	free(dp, M_IP6NDP);
120978064Sume	dp = NULL;
121078064Sume	IFAFREE(ifa);
121178064Sume}
121278064Sume
121353541Sshinstatic void
1214171259Sdelphijnd6_dad_timer(struct ifaddr *ifa)
121553541Sshin{
121653541Sshin	int s;
121753541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
121853541Sshin	struct dadq *dp;
1219165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
122053541Sshin
122195023Ssuz	s = splnet();		/* XXX */
122253541Sshin
122353541Sshin	/* Sanity check */
122453541Sshin	if (ia == NULL) {
122562587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
122653541Sshin		goto done;
122753541Sshin	}
122853541Sshin	dp = nd6_dad_find(ifa);
122953541Sshin	if (dp == NULL) {
123062587Sitojun		log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
123153541Sshin		goto done;
123253541Sshin	}
123353541Sshin	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
123462587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
123553541Sshin			"%s(%s)\n",
1236165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
123753541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
123853541Sshin		goto done;
123953541Sshin	}
124053541Sshin	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
124162587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
124253541Sshin			"%s(%s)\n",
1243165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
124453541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
124553541Sshin		goto done;
124653541Sshin	}
124753541Sshin
124862587Sitojun	/* timeouted with IFF_{RUNNING,UP} check */
1249181803Sbz	if (dp->dad_ns_tcount > V_dad_maxtry) {
125078064Sume		nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1251120941Sume		    if_name(ifa->ifa_ifp)));
125262587Sitojun
1253181803Sbz		TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
125462587Sitojun		free(dp, M_IP6NDP);
125562587Sitojun		dp = NULL;
125662587Sitojun		IFAFREE(ifa);
125762587Sitojun		goto done;
125862587Sitojun	}
125962587Sitojun
126053541Sshin	/* Need more checks? */
126153541Sshin	if (dp->dad_ns_ocount < dp->dad_count) {
126253541Sshin		/*
126353541Sshin		 * We have more NS to go.  Send NS packet for DAD.
126453541Sshin		 */
126562587Sitojun		nd6_dad_ns_output(dp, ifa);
1266120941Sume		nd6_dad_starttimer(dp,
1267151539Ssuz		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
126853541Sshin	} else {
126953541Sshin		/*
127053541Sshin		 * We have transmitted sufficient number of DAD packets.
127153541Sshin		 * See what we've got.
127253541Sshin		 */
127353541Sshin		int duplicate;
127453541Sshin
127553541Sshin		duplicate = 0;
127653541Sshin
127753541Sshin		if (dp->dad_na_icount) {
127853541Sshin			/*
127953541Sshin			 * the check is in nd6_dad_na_input(),
128053541Sshin			 * but just in case
128153541Sshin			 */
128253541Sshin			duplicate++;
128353541Sshin		}
128453541Sshin
128553541Sshin		if (dp->dad_ns_icount) {
128653541Sshin			/* We've seen NS, means DAD has failed. */
128753541Sshin			duplicate++;
128853541Sshin		}
128953541Sshin
129053541Sshin		if (duplicate) {
129153541Sshin			/* (*dp) will be freed in nd6_dad_duplicated() */
129253541Sshin			dp = NULL;
129353541Sshin			nd6_dad_duplicated(ifa);
129453541Sshin		} else {
129553541Sshin			/*
129653541Sshin			 * We are done with DAD.  No NA came, no NS came.
1297148987Sume			 * No duplicate address found.
129853541Sshin			 */
129953541Sshin			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
130053541Sshin
130178064Sume			nd6log((LOG_DEBUG,
130262587Sitojun			    "%s: DAD complete for %s - no duplicates found\n",
130362587Sitojun			    if_name(ifa->ifa_ifp),
1304165118Sbz			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
130553541Sshin
1306181803Sbz			TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
130753541Sshin			free(dp, M_IP6NDP);
130853541Sshin			dp = NULL;
130962587Sitojun			IFAFREE(ifa);
131053541Sshin		}
131153541Sshin	}
131253541Sshin
131353541Sshindone:
131453541Sshin	splx(s);
131553541Sshin}
131653541Sshin
131753541Sshinvoid
1318171259Sdelphijnd6_dad_duplicated(struct ifaddr *ifa)
131953541Sshin{
132053541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1321151477Ssuz	struct ifnet *ifp;
132253541Sshin	struct dadq *dp;
1323165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
132453541Sshin
132553541Sshin	dp = nd6_dad_find(ifa);
132653541Sshin	if (dp == NULL) {
132762587Sitojun		log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
132853541Sshin		return;
132953541Sshin	}
133053541Sshin
133178064Sume	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
133278064Sume	    "NS in/out=%d/%d, NA in=%d\n",
1333165118Sbz	    if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
133478064Sume	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
133553541Sshin
133653541Sshin	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
133753541Sshin	ia->ia6_flags |= IN6_IFF_DUPLICATED;
133853541Sshin
1339148987Sume	/* We are done with DAD, with duplicate address found. (failure) */
134078064Sume	nd6_dad_stoptimer(dp);
134153541Sshin
1342151477Ssuz	ifp = ifa->ifa_ifp;
134362587Sitojun	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1344165118Sbz	    if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
134562587Sitojun	log(LOG_ERR, "%s: manual intervention required\n",
1346151477Ssuz	    if_name(ifp));
134753541Sshin
1348151477Ssuz	/*
1349151477Ssuz	 * If the address is a link-local address formed from an interface
1350151477Ssuz	 * identifier based on the hardware address which is supposed to be
1351151477Ssuz	 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1352151477Ssuz	 * operation on the interface SHOULD be disabled.
1353151477Ssuz	 * [rfc2462bis-03 Section 5.4.5]
1354151477Ssuz	 */
1355151477Ssuz	if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1356151477Ssuz		struct in6_addr in6;
1357151477Ssuz
1358151477Ssuz		/*
1359151477Ssuz		 * To avoid over-reaction, we only apply this logic when we are
1360151477Ssuz		 * very sure that hardware addresses are supposed to be unique.
1361151477Ssuz		 */
1362151477Ssuz		switch (ifp->if_type) {
1363151477Ssuz		case IFT_ETHER:
1364151477Ssuz		case IFT_FDDI:
1365151477Ssuz		case IFT_ATM:
1366151477Ssuz		case IFT_IEEE1394:
1367151477Ssuz#ifdef IFT_IEEE80211
1368151477Ssuz		case IFT_IEEE80211:
1369151477Ssuz#endif
1370151477Ssuz			in6 = ia->ia_addr.sin6_addr;
1371151477Ssuz			if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1372151477Ssuz			    IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1373151477Ssuz				ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1374151477Ssuz				log(LOG_ERR, "%s: possible hardware address "
1375151477Ssuz				    "duplication detected, disable IPv6\n",
1376151477Ssuz				    if_name(ifp));
1377151477Ssuz			}
1378151477Ssuz			break;
1379151477Ssuz		}
1380151477Ssuz	}
1381151477Ssuz
1382181803Sbz	TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
138353541Sshin	free(dp, M_IP6NDP);
138453541Sshin	dp = NULL;
138562587Sitojun	IFAFREE(ifa);
138653541Sshin}
138753541Sshin
138862587Sitojunstatic void
1389171259Sdelphijnd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
139062587Sitojun{
139162587Sitojun	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
139262587Sitojun	struct ifnet *ifp = ifa->ifa_ifp;
139362587Sitojun
139462587Sitojun	dp->dad_ns_tcount++;
139562587Sitojun	if ((ifp->if_flags & IFF_UP) == 0) {
139662587Sitojun		return;
139762587Sitojun	}
1398148887Srwatson	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
139962587Sitojun		return;
140062587Sitojun	}
140162587Sitojun
140262587Sitojun	dp->dad_ns_ocount++;
140362587Sitojun	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
140462587Sitojun}
140562587Sitojun
140662587Sitojunstatic void
1407171259Sdelphijnd6_dad_ns_input(struct ifaddr *ifa)
140853541Sshin{
140953541Sshin	struct in6_ifaddr *ia;
141053541Sshin	struct ifnet *ifp;
141178064Sume	const struct in6_addr *taddr6;
141253541Sshin	struct dadq *dp;
141353541Sshin	int duplicate;
141453541Sshin
1415151465Ssuz	if (ifa == NULL)
141653541Sshin		panic("ifa == NULL in nd6_dad_ns_input");
141753541Sshin
141853541Sshin	ia = (struct in6_ifaddr *)ifa;
141953541Sshin	ifp = ifa->ifa_ifp;
142053541Sshin	taddr6 = &ia->ia_addr.sin6_addr;
142153541Sshin	duplicate = 0;
142253541Sshin	dp = nd6_dad_find(ifa);
142353541Sshin
142453541Sshin	/* Quickhack - completely ignore DAD NS packets */
1425181803Sbz	if (V_dad_ignore_ns) {
1426165118Sbz		char ip6buf[INET6_ADDRSTRLEN];
142778064Sume		nd6log((LOG_INFO,
142878064Sume		    "nd6_dad_ns_input: ignoring DAD NS packet for "
1429165118Sbz		    "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6),
143078064Sume		    if_name(ifa->ifa_ifp)));
143153541Sshin		return;
143253541Sshin	}
143353541Sshin
143453541Sshin	/*
143553541Sshin	 * if I'm yet to start DAD, someone else started using this address
143653541Sshin	 * first.  I have a duplicate and you win.
143753541Sshin	 */
1438151465Ssuz	if (dp == NULL || dp->dad_ns_ocount == 0)
143953541Sshin		duplicate++;
144053541Sshin
144153541Sshin	/* XXX more checks for loopback situation - see nd6_dad_timer too */
144253541Sshin
144353541Sshin	if (duplicate) {
144453541Sshin		dp = NULL;	/* will be freed in nd6_dad_duplicated() */
144553541Sshin		nd6_dad_duplicated(ifa);
144653541Sshin	} else {
144753541Sshin		/*
144853541Sshin		 * not sure if I got a duplicate.
144953541Sshin		 * increment ns count and see what happens.
145053541Sshin		 */
145153541Sshin		if (dp)
145253541Sshin			dp->dad_ns_icount++;
145353541Sshin	}
145453541Sshin}
145553541Sshin
145662587Sitojunstatic void
1457171259Sdelphijnd6_dad_na_input(struct ifaddr *ifa)
145853541Sshin{
145953541Sshin	struct dadq *dp;
146053541Sshin
1461151465Ssuz	if (ifa == NULL)
146253541Sshin		panic("ifa == NULL in nd6_dad_na_input");
146353541Sshin
146453541Sshin	dp = nd6_dad_find(ifa);
146553541Sshin	if (dp)
146653541Sshin		dp->dad_na_icount++;
146753541Sshin
146853541Sshin	/* remove the address. */
146953541Sshin	nd6_dad_duplicated(ifa);
147053541Sshin}
1471