nd6_nbr.c revision 153227
162587Sitojun/*	$FreeBSD: head/sys/netinet6/nd6_nbr.c 153227 2005-12-08 06:43:39Z ume $	*/
295023Ssuz/*	$KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $	*/
362587Sitojun
4139826Simp/*-
553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
653541Sshin * All rights reserved.
753541Sshin *
853541Sshin * Redistribution and use in source and binary forms, with or without
953541Sshin * modification, are permitted provided that the following conditions
1053541Sshin * are met:
1153541Sshin * 1. Redistributions of source code must retain the above copyright
1253541Sshin *    notice, this list of conditions and the following disclaimer.
1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1453541Sshin *    notice, this list of conditions and the following disclaimer in the
1553541Sshin *    documentation and/or other materials provided with the distribution.
1653541Sshin * 3. Neither the name of the project nor the names of its contributors
1753541Sshin *    may be used to endorse or promote products derived from this software
1853541Sshin *    without specific prior written permission.
1953541Sshin *
2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2353541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3053541Sshin * SUCH DAMAGE.
3153541Sshin */
3253541Sshin
3362587Sitojun#include "opt_inet.h"
3462587Sitojun#include "opt_inet6.h"
35142215Sglebius#include "opt_ipsec.h"
36142215Sglebius#include "opt_carp.h"
3755009Sshin
3853541Sshin#include <sys/param.h>
3953541Sshin#include <sys/systm.h>
4053541Sshin#include <sys/malloc.h>
4153541Sshin#include <sys/mbuf.h>
4253541Sshin#include <sys/socket.h>
4353541Sshin#include <sys/sockio.h>
4453541Sshin#include <sys/time.h>
4553541Sshin#include <sys/kernel.h>
4653541Sshin#include <sys/errno.h>
4753541Sshin#include <sys/syslog.h>
4853541Sshin#include <sys/queue.h>
4978064Sume#include <sys/callout.h>
5053541Sshin
5153541Sshin#include <net/if.h>
5253541Sshin#include <net/if_types.h>
5353541Sshin#include <net/if_dl.h>
54147306Sbrooks#include <net/if_var.h>
5553541Sshin#include <net/route.h>
5653541Sshin
5753541Sshin#include <netinet/in.h>
5853541Sshin#include <netinet/in_var.h>
5953541Sshin#include <netinet6/in6_var.h>
60151477Ssuz#include <netinet6/in6_ifattach.h>
6162587Sitojun#include <netinet/ip6.h>
6253541Sshin#include <netinet6/ip6_var.h>
63148385Sume#include <netinet6/scope6_var.h>
6453541Sshin#include <netinet6/nd6.h>
6562587Sitojun#include <netinet/icmp6.h>
6653541Sshin
67142215Sglebius#ifdef DEV_CARP
68142215Sglebius#include <netinet/ip_carp.h>
69142215Sglebius#endif
70142215Sglebius
7153541Sshin#include <net/net_osdep.h>
7253541Sshin
7362587Sitojun#define SDL(s) ((struct sockaddr_dl *)s)
7453541Sshin
7562587Sitojunstruct dadq;
7662587Sitojunstatic struct dadq *nd6_dad_find __P((struct ifaddr *));
7778064Sumestatic void nd6_dad_starttimer __P((struct dadq *, int));
7878064Sumestatic void nd6_dad_stoptimer __P((struct dadq *));
7962587Sitojunstatic void nd6_dad_timer __P((struct ifaddr *));
8062587Sitojunstatic void nd6_dad_ns_output __P((struct dadq *, struct ifaddr *));
8162587Sitojunstatic void nd6_dad_ns_input __P((struct ifaddr *));
8262587Sitojunstatic void nd6_dad_na_input __P((struct ifaddr *));
8353541Sshin
8462587Sitojunstatic int dad_ignore_ns = 0;	/* ignore NS in DAD - specwise incorrect*/
8562587Sitojunstatic int dad_maxtry = 15;	/* max # of *tries* to transmit DAD packet */
8653541Sshin
8753541Sshin/*
88108470Sschweikh * Input a Neighbor Solicitation Message.
8953541Sshin *
9053541Sshin * Based on RFC 2461
91148987Sume * Based on RFC 2462 (duplicate address detection)
9253541Sshin */
9353541Sshinvoid
9453541Sshinnd6_ns_input(m, off, icmp6len)
9553541Sshin	struct mbuf *m;
9653541Sshin	int off, icmp6len;
9753541Sshin{
9853541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
9953541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
10062587Sitojun	struct nd_neighbor_solicit *nd_ns;
10153541Sshin	struct in6_addr saddr6 = ip6->ip6_src;
10253541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
10362587Sitojun	struct in6_addr taddr6;
10453541Sshin	struct in6_addr myaddr6;
10553541Sshin	char *lladdr = NULL;
106142215Sglebius	struct ifaddr *ifa = NULL;
10753541Sshin	int lladdrlen = 0;
10853541Sshin	int anycast = 0, proxy = 0, tentative = 0;
10953541Sshin	int tlladdr;
11053541Sshin	union nd_opts ndopts;
11162587Sitojun	struct sockaddr_dl *proxydl = NULL;
11253541Sshin
11378064Sume#ifndef PULLDOWN_TEST
11478064Sume	IP6_EXTHDR_CHECK(m, off, icmp6len,);
11578064Sume	nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
11678064Sume#else
11778064Sume	IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
11878064Sume	if (nd_ns == NULL) {
11978064Sume		icmp6stat.icp6s_tooshort++;
12078064Sume		return;
12178064Sume	}
12278064Sume#endif
12378064Sume	ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
12478064Sume	taddr6 = nd_ns->nd_ns_target;
125148385Sume	if (in6_setscope(&taddr6, ifp, NULL) != 0)
126148385Sume		goto bad;
12778064Sume
12853541Sshin	if (ip6->ip6_hlim != 255) {
12978064Sume		nd6log((LOG_ERR,
13078064Sume		    "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
13178064Sume		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
13278064Sume		    ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
13378064Sume		goto bad;
13453541Sshin	}
13553541Sshin
13653541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
137148987Sume		/* dst has to be a solicited node multicast address. */
138120941Sume		if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
13995023Ssuz		    /* don't check ifindex portion */
140120941Sume		    daddr6.s6_addr32[1] == 0 &&
141120941Sume		    daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
142120941Sume		    daddr6.s6_addr8[12] == 0xff) {
14395023Ssuz			; /* good */
14453541Sshin		} else {
14578064Sume			nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
146120941Sume			    "(wrong ip6 dst)\n"));
14753541Sshin			goto bad;
14853541Sshin		}
14953541Sshin	}
15053541Sshin
15153541Sshin	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
15278064Sume		nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
15353541Sshin		goto bad;
15453541Sshin	}
15553541Sshin
15653541Sshin	icmp6len -= sizeof(*nd_ns);
15753541Sshin	nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
15853541Sshin	if (nd6_options(&ndopts) < 0) {
15978064Sume		nd6log((LOG_INFO,
16078064Sume		    "nd6_ns_input: invalid ND option, ignored\n"));
16178064Sume		/* nd6_options have incremented stats */
16278064Sume		goto freeit;
16353541Sshin	}
16453541Sshin
16553541Sshin	if (ndopts.nd_opts_src_lladdr) {
16695023Ssuz		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
16753541Sshin		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
16853541Sshin	}
169120941Sume
17053541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
17178064Sume		nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
17278064Sume		    "(link-layer address option)\n"));
17353541Sshin		goto bad;
17453541Sshin	}
17553541Sshin
17653541Sshin	/*
17753541Sshin	 * Attaching target link-layer address to the NA?
17853541Sshin	 * (RFC 2461 7.2.4)
17953541Sshin	 *
18053541Sshin	 * NS IP dst is unicast/anycast			MUST NOT add
18153541Sshin	 * NS IP dst is solicited-node multicast	MUST add
18253541Sshin	 *
18353541Sshin	 * In implementation, we add target link-layer address by default.
18453541Sshin	 * We do not add one in MUST NOT cases.
18553541Sshin	 */
18653541Sshin	if (!IN6_IS_ADDR_MULTICAST(&daddr6))
18753541Sshin		tlladdr = 0;
18853541Sshin	else
18953541Sshin		tlladdr = 1;
19053541Sshin
19153541Sshin	/*
19253541Sshin	 * Target address (taddr6) must be either:
19353541Sshin	 * (1) Valid unicast/anycast address for my receiving interface,
19453541Sshin	 * (2) Unicast address for which I'm offering proxy service, or
19553541Sshin	 * (3) "tentative" address on which DAD is being performed.
19653541Sshin	 */
19753541Sshin	/* (1) and (3) check. */
198142215Sglebius#ifdef DEV_CARP
199142215Sglebius	if (ifp->if_carp)
200142215Sglebius		ifa = carp_iamatch6(ifp->if_carp, &taddr6);
201151465Ssuz	if (ifa == NULL)
202142215Sglebius		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
203142215Sglebius#else
20453541Sshin	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
205142215Sglebius#endif
20653541Sshin
20753541Sshin	/* (2) check. */
208151465Ssuz	if (ifa == NULL) {
20953541Sshin		struct rtentry *rt;
21053541Sshin		struct sockaddr_in6 tsin6;
211121765Ssam		int need_proxy;
21253541Sshin
21353541Sshin		bzero(&tsin6, sizeof tsin6);
21453541Sshin		tsin6.sin6_len = sizeof(struct sockaddr_in6);
21553541Sshin		tsin6.sin6_family = AF_INET6;
21653541Sshin		tsin6.sin6_addr = taddr6;
21753541Sshin
21853541Sshin		rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
219121765Ssam		need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
220121765Ssam		    rt->rt_gateway->sa_family == AF_LINK);
221121765Ssam		if (rt)
222121765Ssam			rtfree(rt);
223121765Ssam		if (need_proxy) {
22453541Sshin			/*
22562587Sitojun			 * proxy NDP for single entry
22653541Sshin			 */
22762587Sitojun			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
22862587Sitojun				IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
22962587Sitojun			if (ifa) {
23053541Sshin				proxy = 1;
23162587Sitojun				proxydl = SDL(rt->rt_gateway);
23262587Sitojun			}
23353541Sshin		}
23453541Sshin	}
235151479Ssuz	if (ifa == NULL) {
23653541Sshin		/*
23778064Sume		 * We've got an NS packet, and we don't have that adddress
23853541Sshin		 * assigned for us.  We MUST silently ignore it.
23953541Sshin		 * See RFC2461 7.2.3.
24053541Sshin		 */
24162587Sitojun		goto freeit;
24253541Sshin	}
24353541Sshin	myaddr6 = *IFA_IN6(ifa);
24453541Sshin	anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
24553541Sshin	tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
24653541Sshin	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
24762587Sitojun		goto freeit;
24853541Sshin
24953541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
250120941Sume		nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
25153541Sshin		    "(if %d, NS packet %d)\n",
252120941Sume		    ip6_sprintf(&taddr6),
253120941Sume		    ifp->if_addrlen, lladdrlen - 2));
25478064Sume		goto bad;
25553541Sshin	}
25653541Sshin
25753541Sshin	if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
258120941Sume		nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
259120941Sume		    ip6_sprintf(&saddr6)));
26062587Sitojun		goto freeit;
26153541Sshin	}
26253541Sshin
26353541Sshin	/*
26453541Sshin	 * We have neighbor solicitation packet, with target address equals to
26553541Sshin	 * one of my tentative address.
26653541Sshin	 *
26753541Sshin	 * src addr	how to process?
26853541Sshin	 * ---		---
26953541Sshin	 * multicast	of course, invalid (rejected in ip6_input)
27053541Sshin	 * unicast	somebody is doing address resolution -> ignore
27153541Sshin	 * unspec	dup address detection
27253541Sshin	 *
27353541Sshin	 * The processing is defined in RFC 2462.
27453541Sshin	 */
27553541Sshin	if (tentative) {
27653541Sshin		/*
27753541Sshin		 * If source address is unspecified address, it is for
278148987Sume		 * duplicate address detection.
27953541Sshin		 *
28053541Sshin		 * If not, the packet is for addess resolution;
28153541Sshin		 * silently ignore it.
28253541Sshin		 */
28353541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
28453541Sshin			nd6_dad_ns_input(ifa);
28553541Sshin
28662587Sitojun		goto freeit;
28753541Sshin	}
28853541Sshin
28953541Sshin	/*
29053541Sshin	 * If the source address is unspecified address, entries must not
29153541Sshin	 * be created or updated.
29253541Sshin	 * It looks that sender is performing DAD.  Output NA toward
29353541Sshin	 * all-node multicast address, to tell the sender that I'm using
29453541Sshin	 * the address.
29553541Sshin	 * S bit ("solicited") must be zero.
29653541Sshin	 */
29753541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
298148385Sume		struct in6_addr in6_all;
299148385Sume
300148385Sume		in6_all = in6addr_linklocal_allnodes;
301148385Sume		if (in6_setscope(&in6_all, ifp, NULL) != 0)
302148385Sume			goto bad;
303148385Sume		nd6_na_output(ifp, &in6_all, &taddr6,
304120941Sume		    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
305120941Sume		    (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
306120941Sume		    tlladdr, (struct sockaddr *)proxydl);
30762587Sitojun		goto freeit;
30853541Sshin	}
30953541Sshin
310120941Sume	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
311120941Sume	    ND_NEIGHBOR_SOLICIT, 0);
31253541Sshin
31353541Sshin	nd6_na_output(ifp, &saddr6, &taddr6,
314120941Sume	    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
315120941Sume	    (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
316120941Sume	    tlladdr, (struct sockaddr *)proxydl);
31762587Sitojun freeit:
31862587Sitojun	m_freem(m);
31953541Sshin	return;
32053541Sshin
32153541Sshin bad:
32278064Sume	nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6)));
32378064Sume	nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6)));
32478064Sume	nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6)));
32578064Sume	icmp6stat.icp6s_badns++;
32662587Sitojun	m_freem(m);
32753541Sshin}
32853541Sshin
32953541Sshin/*
330108470Sschweikh * Output a Neighbor Solicitation Message. Caller specifies:
33153541Sshin *	- ICMP6 header source IP6 address
33253541Sshin *	- ND6 header target IP6 address
33353541Sshin *	- ND6 header source datalink address
33453541Sshin *
33553541Sshin * Based on RFC 2461
336148987Sume * Based on RFC 2462 (duplicate address detection)
33753541Sshin */
33853541Sshinvoid
33953541Sshinnd6_ns_output(ifp, daddr6, taddr6, ln, dad)
34053541Sshin	struct ifnet *ifp;
34178064Sume	const struct in6_addr *daddr6, *taddr6;
34253541Sshin	struct llinfo_nd6 *ln;	/* for source address determination */
343148987Sume	int dad;	/* duplicate address detection */
34453541Sshin{
34553541Sshin	struct mbuf *m;
34653541Sshin	struct ip6_hdr *ip6;
34753541Sshin	struct nd_neighbor_solicit *nd_ns;
348148385Sume	struct in6_addr *src, src_in;
34953541Sshin	struct ip6_moptions im6o;
35053541Sshin	int icmp6len;
35162587Sitojun	int maxlen;
35253541Sshin	caddr_t mac;
353148385Sume	struct route_in6 ro;
354120941Sume
355148385Sume	bzero(&ro, sizeof(ro));
356148385Sume
35753541Sshin	if (IN6_IS_ADDR_MULTICAST(taddr6))
35853541Sshin		return;
35953541Sshin
36062587Sitojun	/* estimate the size of message */
36162587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
36262587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
36362587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
36462587Sitojun#ifdef DIAGNOSTIC
36562587Sitojun		printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
36662587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
36762587Sitojun#endif
36853541Sshin		return;
36962587Sitojun	}
37053541Sshin
371111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
37262587Sitojun	if (m && max_linkhdr + maxlen >= MHLEN) {
373111119Simp		MCLGET(m, M_DONTWAIT);
37462587Sitojun		if ((m->m_flags & M_EXT) == 0) {
37562587Sitojun			m_free(m);
37662587Sitojun			m = NULL;
37762587Sitojun		}
37862587Sitojun	}
37962587Sitojun	if (m == NULL)
38062587Sitojun		return;
38178064Sume	m->m_pkthdr.rcvif = NULL;
38262587Sitojun
38353541Sshin	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
38453541Sshin		m->m_flags |= M_MCAST;
38553541Sshin		im6o.im6o_multicast_ifp = ifp;
38653541Sshin		im6o.im6o_multicast_hlim = 255;
38753541Sshin		im6o.im6o_multicast_loop = 0;
38853541Sshin	}
38953541Sshin
39053541Sshin	icmp6len = sizeof(*nd_ns);
39153541Sshin	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
39295023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
39353541Sshin
39453541Sshin	/* fill neighbor solicitation packet */
39553541Sshin	ip6 = mtod(m, struct ip6_hdr *);
39653541Sshin	ip6->ip6_flow = 0;
39762587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
39862587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
39953541Sshin	/* ip6->ip6_plen will be set later */
40053541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
40153541Sshin	ip6->ip6_hlim = 255;
40253541Sshin	if (daddr6)
40353541Sshin		ip6->ip6_dst = *daddr6;
40453541Sshin	else {
40553541Sshin		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
406148385Sume		ip6->ip6_dst.s6_addr16[1] = 0;
40753541Sshin		ip6->ip6_dst.s6_addr32[1] = 0;
40853541Sshin		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
40953541Sshin		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
41053541Sshin		ip6->ip6_dst.s6_addr8[12] = 0xff;
411148385Sume		if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
412148385Sume			goto bad;
41353541Sshin	}
41453541Sshin	if (!dad) {
41553541Sshin		/*
41653541Sshin		 * RFC2461 7.2.2:
41753541Sshin		 * "If the source address of the packet prompting the
41853541Sshin		 * solicitation is the same as one of the addresses assigned
41953541Sshin		 * to the outgoing interface, that address SHOULD be placed
42053541Sshin		 * in the IP Source Address of the outgoing solicitation.
42153541Sshin		 * Otherwise, any one of the addresses assigned to the
42253541Sshin		 * interface should be used."
42353541Sshin		 *
42453541Sshin		 * We use the source address for the prompting packet
42553541Sshin		 * (saddr6), if:
42653541Sshin		 * - saddr6 is given from the caller (by giving "ln"), and
42753541Sshin		 * - saddr6 belongs to the outgoing interface.
428148385Sume		 * Otherwise, we perform the source address selection as usual.
42953541Sshin		 */
43095023Ssuz		struct ip6_hdr *hip6;		/* hold ip6 */
431148385Sume		struct in6_addr *hsrc = NULL;
43253541Sshin
43353541Sshin		if (ln && ln->ln_hold) {
434148987Sume			/*
435148987Sume			 * assuming every packet in ln_hold has the same IP
436148987Sume			 * header
437148987Sume			 */
43853541Sshin			hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
43953541Sshin			/* XXX pullup? */
44053541Sshin			if (sizeof(*hip6) < ln->ln_hold->m_len)
441148385Sume				hsrc = &hip6->ip6_src;
44253541Sshin			else
443148385Sume				hsrc = NULL;
444148385Sume		}
445148385Sume		if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc))
446148385Sume			src = hsrc;
44753541Sshin		else {
448148385Sume			int error;
449148385Sume			struct sockaddr_in6 dst_sa;
450148385Sume
451148385Sume			bzero(&dst_sa, sizeof(dst_sa));
452148385Sume			dst_sa.sin6_family = AF_INET6;
453148385Sume			dst_sa.sin6_len = sizeof(dst_sa);
454148385Sume			dst_sa.sin6_addr = ip6->ip6_dst;
455148385Sume
456148385Sume			src = in6_selectsrc(&dst_sa, NULL,
457148385Sume			    NULL, &ro, NULL, NULL, &error);
458148385Sume			if (src == NULL) {
459148385Sume				nd6log((LOG_DEBUG,
460148385Sume				    "nd6_ns_output: source can't be "
461148385Sume				    "determined: dst=%s, error=%d\n",
462148385Sume				    ip6_sprintf(&dst_sa.sin6_addr), error));
463148385Sume				goto bad;
46453541Sshin			}
46553541Sshin		}
46653541Sshin	} else {
46753541Sshin		/*
46853541Sshin		 * Source address for DAD packet must always be IPv6
46953541Sshin		 * unspecified address. (0::0)
470148385Sume		 * We actually don't have to 0-clear the address (we did it
471148385Sume		 * above), but we do so here explicitly to make the intention
472148385Sume		 * clearer.
47353541Sshin		 */
474148385Sume		bzero(&src_in, sizeof(src_in));
475148385Sume		src = &src_in;
47653541Sshin	}
477148385Sume	ip6->ip6_src = *src;
47853541Sshin	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
47953541Sshin	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
48053541Sshin	nd_ns->nd_ns_code = 0;
48153541Sshin	nd_ns->nd_ns_reserved = 0;
48253541Sshin	nd_ns->nd_ns_target = *taddr6;
483121315Sume	in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
48453541Sshin
48553541Sshin	/*
48653541Sshin	 * Add source link-layer address option.
48753541Sshin	 *
48853541Sshin	 *				spec		implementation
48953541Sshin	 *				---		---
49053541Sshin	 * DAD packet			MUST NOT	do not add the option
49153541Sshin	 * there's no link layer address:
49253541Sshin	 *				impossible	do not add the option
49353541Sshin	 * there's link layer address:
49453541Sshin	 *	Multicast NS		MUST add one	add the option
49553541Sshin	 *	Unicast NS		SHOULD add one	add the option
49653541Sshin	 */
49753541Sshin	if (!dad && (mac = nd6_ifptomac(ifp))) {
49853541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
49953541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
50053541Sshin		/* 8 byte alignments... */
50153541Sshin		optlen = (optlen + 7) & ~7;
502120941Sume
50353541Sshin		m->m_pkthdr.len += optlen;
50453541Sshin		m->m_len += optlen;
50553541Sshin		icmp6len += optlen;
50653541Sshin		bzero((caddr_t)nd_opt, optlen);
50753541Sshin		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
50853541Sshin		nd_opt->nd_opt_len = optlen >> 3;
50953541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
51053541Sshin	}
51153541Sshin
51253541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
51353541Sshin	nd_ns->nd_ns_cksum = 0;
514120941Sume	nd_ns->nd_ns_cksum =
515120941Sume	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
51653541Sshin
517151536Ssuz	ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
518148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_msg);
519148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
520148385Sume	icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
521148385Sume
522148385Sume	if (ro.ro_rt) {		/* we don't cache this route. */
523148385Sume		RTFREE(ro.ro_rt);
52453541Sshin	}
525148385Sume	return;
526148385Sume
527148385Sume  bad:
528148385Sume	if (ro.ro_rt) {
529148385Sume		RTFREE(ro.ro_rt);
530148385Sume	}
531148385Sume	m_freem(m);
532148385Sume	return;
53353541Sshin}
53453541Sshin
53553541Sshin/*
53653541Sshin * Neighbor advertisement input handling.
53753541Sshin *
53853541Sshin * Based on RFC 2461
539148987Sume * Based on RFC 2462 (duplicate address detection)
54062587Sitojun *
54162587Sitojun * the following items are not implemented yet:
54262587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
54362587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
54453541Sshin */
54553541Sshinvoid
54653541Sshinnd6_na_input(m, off, icmp6len)
54753541Sshin	struct mbuf *m;
54853541Sshin	int off, icmp6len;
54953541Sshin{
55053541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
55153541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
55262587Sitojun	struct nd_neighbor_advert *nd_na;
55353541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
55462587Sitojun	struct in6_addr taddr6;
55562587Sitojun	int flags;
55662587Sitojun	int is_router;
55762587Sitojun	int is_solicited;
55862587Sitojun	int is_override;
55953541Sshin	char *lladdr = NULL;
56053541Sshin	int lladdrlen = 0;
56153541Sshin	struct ifaddr *ifa;
56253541Sshin	struct llinfo_nd6 *ln;
56353541Sshin	struct rtentry *rt;
56453541Sshin	struct sockaddr_dl *sdl;
56553541Sshin	union nd_opts ndopts;
56653541Sshin
56753541Sshin	if (ip6->ip6_hlim != 255) {
56878064Sume		nd6log((LOG_ERR,
56978064Sume		    "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
57078064Sume		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
57178064Sume		    ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
57278064Sume		goto bad;
57362587Sitojun	}
57462587Sitojun
57562587Sitojun#ifndef PULLDOWN_TEST
57662587Sitojun	IP6_EXTHDR_CHECK(m, off, icmp6len,);
57762587Sitojun	nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
57862587Sitojun#else
57962587Sitojun	IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
58062587Sitojun	if (nd_na == NULL) {
58162587Sitojun		icmp6stat.icp6s_tooshort++;
58253541Sshin		return;
58353541Sshin	}
58462587Sitojun#endif
585148385Sume
58662587Sitojun	flags = nd_na->nd_na_flags_reserved;
58762587Sitojun	is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
58862587Sitojun	is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
58962587Sitojun	is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
59053541Sshin
591148385Sume	taddr6 = nd_na->nd_na_target;
592148385Sume	if (in6_setscope(&taddr6, ifp, NULL))
593150202Ssuz		goto bad;	/* XXX: impossible */
59453541Sshin
59553541Sshin	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
59678064Sume		nd6log((LOG_ERR,
59753541Sshin		    "nd6_na_input: invalid target address %s\n",
59878064Sume		    ip6_sprintf(&taddr6)));
59978064Sume		goto bad;
60053541Sshin	}
60153541Sshin	if (IN6_IS_ADDR_MULTICAST(&daddr6))
60253541Sshin		if (is_solicited) {
60378064Sume			nd6log((LOG_ERR,
60478064Sume			    "nd6_na_input: a solicited adv is multicasted\n"));
60578064Sume			goto bad;
60653541Sshin		}
60753541Sshin
60853541Sshin	icmp6len -= sizeof(*nd_na);
60953541Sshin	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
61053541Sshin	if (nd6_options(&ndopts) < 0) {
61178064Sume		nd6log((LOG_INFO,
61278064Sume		    "nd6_na_input: invalid ND option, ignored\n"));
61378064Sume		/* nd6_options have incremented stats */
61462587Sitojun		goto freeit;
61553541Sshin	}
61653541Sshin
61753541Sshin	if (ndopts.nd_opts_tgt_lladdr) {
61853541Sshin		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
61953541Sshin		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
62053541Sshin	}
62153541Sshin
62253541Sshin	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
62353541Sshin
62453541Sshin	/*
62553541Sshin	 * Target address matches one of my interface address.
62653541Sshin	 *
62753541Sshin	 * If my address is tentative, this means that there's somebody
62853541Sshin	 * already using the same address as mine.  This indicates DAD failure.
62953541Sshin	 * This is defined in RFC 2462.
63053541Sshin	 *
63153541Sshin	 * Otherwise, process as defined in RFC 2461.
63253541Sshin	 */
63353541Sshin	if (ifa
63453541Sshin	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
63553541Sshin		nd6_dad_na_input(ifa);
63662587Sitojun		goto freeit;
63753541Sshin	}
63853541Sshin
63995023Ssuz	/* Just for safety, maybe unnecessary. */
64053541Sshin	if (ifa) {
64153541Sshin		log(LOG_ERR,
64253541Sshin		    "nd6_na_input: duplicate IP6 address %s\n",
64353541Sshin		    ip6_sprintf(&taddr6));
64462587Sitojun		goto freeit;
64553541Sshin	}
64653541Sshin
64753541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
648120941Sume		nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
649120941Sume		    "(if %d, NA packet %d)\n", ip6_sprintf(&taddr6),
650120941Sume		    ifp->if_addrlen, lladdrlen - 2));
65178064Sume		goto bad;
65253541Sshin	}
65353541Sshin
65453541Sshin	/*
655120941Sume	 * If no neighbor cache entry is found, NA SHOULD silently be
656120941Sume	 * discarded.
65753541Sshin	 */
65853541Sshin	rt = nd6_lookup(&taddr6, 0, ifp);
65953541Sshin	if ((rt == NULL) ||
66053541Sshin	   ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
66153541Sshin	   ((sdl = SDL(rt->rt_gateway)) == NULL))
66262587Sitojun		goto freeit;
66353541Sshin
66453541Sshin	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
66553541Sshin		/*
66653541Sshin		 * If the link-layer has address, and no lladdr option came,
66753541Sshin		 * discard the packet.
66853541Sshin		 */
669151465Ssuz		if (ifp->if_addrlen && lladdr == NULL)
67062587Sitojun			goto freeit;
67153541Sshin
67253541Sshin		/*
67353541Sshin		 * Record link-layer address, and update the state.
67453541Sshin		 */
67553541Sshin		sdl->sdl_alen = ifp->if_addrlen;
67653541Sshin		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
67753541Sshin		if (is_solicited) {
67853541Sshin			ln->ln_state = ND6_LLINFO_REACHABLE;
67962587Sitojun			ln->ln_byhint = 0;
680151539Ssuz			if (!ND6_LLINFO_PERMANENT(ln)) {
681151539Ssuz				nd6_llinfo_settimer(ln,
682151539Ssuz				    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
683120941Sume			}
68478064Sume		} else {
68553541Sshin			ln->ln_state = ND6_LLINFO_STALE;
686151539Ssuz			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
68778064Sume		}
68878064Sume		if ((ln->ln_router = is_router) != 0) {
68978064Sume			/*
69078064Sume			 * This means a router's state has changed from
69178064Sume			 * non-reachable to probably reachable, and might
69278064Sume			 * affect the status of associated prefixes..
69378064Sume			 */
69478064Sume			pfxlist_onlink_check();
69578064Sume		}
69653541Sshin	} else {
69753541Sshin		int llchange;
69853541Sshin
69953541Sshin		/*
70053541Sshin		 * Check if the link-layer address has changed or not.
70153541Sshin		 */
702151465Ssuz		if (lladdr == NULL)
70353541Sshin			llchange = 0;
70453541Sshin		else {
70553541Sshin			if (sdl->sdl_alen) {
70653541Sshin				if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
70753541Sshin					llchange = 1;
70853541Sshin				else
70953541Sshin					llchange = 0;
71053541Sshin			} else
71153541Sshin				llchange = 1;
71253541Sshin		}
71353541Sshin
71453541Sshin		/*
71553541Sshin		 * This is VERY complex.  Look at it with care.
71653541Sshin		 *
71753541Sshin		 * override solicit lladdr llchange	action
71853541Sshin		 *					(L: record lladdr)
71953541Sshin		 *
72053541Sshin		 *	0	0	n	--	(2c)
72153541Sshin		 *	0	0	y	n	(2b) L
72253541Sshin		 *	0	0	y	y	(1)    REACHABLE->STALE
72353541Sshin		 *	0	1	n	--	(2c)   *->REACHABLE
72453541Sshin		 *	0	1	y	n	(2b) L *->REACHABLE
72553541Sshin		 *	0	1	y	y	(1)    REACHABLE->STALE
72653541Sshin		 *	1	0	n	--	(2a)
72753541Sshin		 *	1	0	y	n	(2a) L
72853541Sshin		 *	1	0	y	y	(2a) L *->STALE
72953541Sshin		 *	1	1	n	--	(2a)   *->REACHABLE
73053541Sshin		 *	1	1	y	n	(2a) L *->REACHABLE
73153541Sshin		 *	1	1	y	y	(2a) L *->REACHABLE
73253541Sshin		 */
733151539Ssuz		if (!is_override && (lladdr != NULL && llchange)) {  /* (1) */
73453541Sshin			/*
73553541Sshin			 * If state is REACHABLE, make it STALE.
73653541Sshin			 * no other updates should be done.
73753541Sshin			 */
73878064Sume			if (ln->ln_state == ND6_LLINFO_REACHABLE) {
73953541Sshin				ln->ln_state = ND6_LLINFO_STALE;
740151539Ssuz				nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
74178064Sume			}
74262587Sitojun			goto freeit;
74353541Sshin		} else if (is_override				   /* (2a) */
744151465Ssuz			|| (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
745151465Ssuz			|| lladdr == NULL) {			   /* (2c) */
74653541Sshin			/*
74753541Sshin			 * Update link-local address, if any.
74853541Sshin			 */
749151465Ssuz			if (lladdr != NULL) {
75053541Sshin				sdl->sdl_alen = ifp->if_addrlen;
75153541Sshin				bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
75253541Sshin			}
75353541Sshin
75453541Sshin			/*
75553541Sshin			 * If solicited, make the state REACHABLE.
75653541Sshin			 * If not solicited and the link-layer address was
75753541Sshin			 * changed, make it STALE.
75853541Sshin			 */
75953541Sshin			if (is_solicited) {
76053541Sshin				ln->ln_state = ND6_LLINFO_REACHABLE;
76162587Sitojun				ln->ln_byhint = 0;
762151539Ssuz				if (!ND6_LLINFO_PERMANENT(ln)) {
763151539Ssuz					nd6_llinfo_settimer(ln,
764151539Ssuz					    (long)ND_IFINFO(ifp)->reachable * hz);
76553541Sshin				}
76653541Sshin			} else {
767151465Ssuz				if (lladdr != NULL && llchange) {
76853541Sshin					ln->ln_state = ND6_LLINFO_STALE;
769151539Ssuz					nd6_llinfo_settimer(ln,
770151539Ssuz					    (long)nd6_gctimer * hz);
77178064Sume				}
77253541Sshin			}
77353541Sshin		}
77453541Sshin
77553541Sshin		if (ln->ln_router && !is_router) {
77653541Sshin			/*
77753541Sshin			 * The peer dropped the router flag.
77853541Sshin			 * Remove the sender from the Default Router List and
77953541Sshin			 * update the Destination Cache entries.
78053541Sshin			 */
78153541Sshin			struct nd_defrouter *dr;
78253541Sshin			struct in6_addr *in6;
78353541Sshin			int s;
78453541Sshin
78553541Sshin			in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
78695023Ssuz
78795023Ssuz			/*
78895023Ssuz			 * Lock to protect the default router list.
78995023Ssuz			 * XXX: this might be unnecessary, since this function
79095023Ssuz			 * is only called under the network software interrupt
791120941Sume			 * context.  However, we keep it just for safety.
79295023Ssuz			 */
79353541Sshin			s = splnet();
794128422Sluigi			dr = defrouter_lookup(in6, ifp);
79553541Sshin			if (dr)
79653541Sshin				defrtrlist_del(dr);
797151539Ssuz			else if (!ip6_forwarding) {
79853541Sshin				/*
79953541Sshin				 * Even if the neighbor is not in the default
80053541Sshin				 * router list, the neighbor may be used
80153541Sshin				 * as a next hop for some destinations
80253541Sshin				 * (e.g. redirect case). So we must
80353541Sshin				 * call rt6_flush explicitly.
80453541Sshin				 */
805128422Sluigi				rt6_flush(&ip6->ip6_src, ifp);
80653541Sshin			}
80753541Sshin			splx(s);
80853541Sshin		}
80953541Sshin		ln->ln_router = is_router;
81053541Sshin	}
81153541Sshin	rt->rt_flags &= ~RTF_REJECT;
81253541Sshin	ln->ln_asked = 0;
81353541Sshin	if (ln->ln_hold) {
814151539Ssuz		struct mbuf *m_hold, *m_hold_next;
815151539Ssuz
816151539Ssuz		for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
817151539Ssuz			struct mbuf *mpkt = NULL;
818151539Ssuz
819151539Ssuz			m_hold_next = m_hold->m_nextpkt;
820151539Ssuz			mpkt = m_copym(m_hold, 0, M_COPYALL, M_DONTWAIT);
821151539Ssuz			if (mpkt == NULL) {
822151539Ssuz				m_freem(m_hold);
823151539Ssuz				break;
824151539Ssuz			}
825151539Ssuz			mpkt->m_nextpkt = NULL;
826151539Ssuz			/*
827151539Ssuz			 * we assume ifp is not a loopback here, so just set
828151539Ssuz			 * the 2nd argument as the 1st one.
829151539Ssuz			 */
830151539Ssuz			nd6_output(ifp, ifp, mpkt,
831151539Ssuz			    (struct sockaddr_in6 *)rt_key(rt), rt);
832151539Ssuz		}
833120941Sume		ln->ln_hold = NULL;
83453541Sshin	}
83562587Sitojun
83662587Sitojun freeit:
83762587Sitojun	m_freem(m);
83878064Sume	return;
83978064Sume
84078064Sume bad:
84178064Sume	icmp6stat.icp6s_badna++;
84278064Sume	m_freem(m);
84353541Sshin}
84453541Sshin
84553541Sshin/*
84653541Sshin * Neighbor advertisement output handling.
84753541Sshin *
84853541Sshin * Based on RFC 2461
84953541Sshin *
85062587Sitojun * the following items are not implemented yet:
85162587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
85262587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
85353541Sshin */
85453541Sshinvoid
855148385Sumend6_na_output(ifp, daddr6_0, taddr6, flags, tlladdr, sdl0)
85653541Sshin	struct ifnet *ifp;
857148385Sume	const struct in6_addr *daddr6_0, *taddr6;
85853541Sshin	u_long flags;
85962587Sitojun	int tlladdr;		/* 1 if include target link-layer address */
86062587Sitojun	struct sockaddr *sdl0;	/* sockaddr_dl (= proxy NA) or NULL */
86153541Sshin{
86253541Sshin	struct mbuf *m;
86353541Sshin	struct ip6_hdr *ip6;
86453541Sshin	struct nd_neighbor_advert *nd_na;
86553541Sshin	struct ip6_moptions im6o;
866148385Sume	struct in6_addr *src, daddr6;
867148385Sume	struct sockaddr_in6 dst_sa;
868148385Sume	int icmp6len, maxlen, error;
86992733Speter	caddr_t mac = NULL;
870148385Sume	struct route_in6 ro;
87162587Sitojun
872148385Sume	bzero(&ro, sizeof(ro));
873148385Sume
874148385Sume	daddr6 = *daddr6_0;	/* make a local copy for modification */
875148385Sume
87662587Sitojun	/* estimate the size of message */
87762587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_na);
87862587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
87962587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
88062587Sitojun#ifdef DIAGNOSTIC
88162587Sitojun		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
88262587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
88362587Sitojun#endif
88453541Sshin		return;
88562587Sitojun	}
88653541Sshin
887111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
88862587Sitojun	if (m && max_linkhdr + maxlen >= MHLEN) {
889111119Simp		MCLGET(m, M_DONTWAIT);
89062587Sitojun		if ((m->m_flags & M_EXT) == 0) {
89162587Sitojun			m_free(m);
89262587Sitojun			m = NULL;
89362587Sitojun		}
89462587Sitojun	}
89562587Sitojun	if (m == NULL)
89662587Sitojun		return;
89778064Sume	m->m_pkthdr.rcvif = NULL;
89862587Sitojun
899148385Sume	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
90053541Sshin		m->m_flags |= M_MCAST;
90153541Sshin		im6o.im6o_multicast_ifp = ifp;
90253541Sshin		im6o.im6o_multicast_hlim = 255;
90353541Sshin		im6o.im6o_multicast_loop = 0;
90453541Sshin	}
90553541Sshin
90653541Sshin	icmp6len = sizeof(*nd_na);
90753541Sshin	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
90895023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
90953541Sshin
91053541Sshin	/* fill neighbor advertisement packet */
91153541Sshin	ip6 = mtod(m, struct ip6_hdr *);
91253541Sshin	ip6->ip6_flow = 0;
91362587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
91462587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
91553541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
91653541Sshin	ip6->ip6_hlim = 255;
917148385Sume	if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
91853541Sshin		/* reply to DAD */
919153227Sume		daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
920153227Sume		daddr6.s6_addr16[1] = 0;
921153227Sume		daddr6.s6_addr32[1] = 0;
922153227Sume		daddr6.s6_addr32[2] = 0;
923153227Sume		daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
924148385Sume		if (in6_setscope(&daddr6, ifp, NULL))
925148385Sume			goto bad;
926148385Sume
92753541Sshin		flags &= ~ND_NA_FLAG_SOLICITED;
928148385Sume	}
929148385Sume	ip6->ip6_dst = daddr6;
930148385Sume	bzero(&dst_sa, sizeof(struct sockaddr_in6));
931148385Sume	dst_sa.sin6_family = AF_INET6;
932148385Sume	dst_sa.sin6_len = sizeof(struct sockaddr_in6);
933148385Sume	dst_sa.sin6_addr = daddr6;
93453541Sshin
93553541Sshin	/*
93653541Sshin	 * Select a source whose scope is the same as that of the dest.
93753541Sshin	 */
938148385Sume	bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
939148385Sume	src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &error);
940148385Sume	if (src == NULL) {
941148385Sume		nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
942148385Sume		    "determined: dst=%s, error=%d\n",
943148385Sume		    ip6_sprintf(&dst_sa.sin6_addr), error));
944148385Sume		goto bad;
94553541Sshin	}
946148385Sume	ip6->ip6_src = *src;
94753541Sshin	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
94853541Sshin	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
94953541Sshin	nd_na->nd_na_code = 0;
95053541Sshin	nd_na->nd_na_target = *taddr6;
951121315Sume	in6_clearscope(&nd_na->nd_na_target); /* XXX */
95253541Sshin
95353541Sshin	/*
95453541Sshin	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
95553541Sshin	 * see nd6_ns_input() for details.
95653541Sshin	 * Basically, if NS packet is sent to unicast/anycast addr,
95753541Sshin	 * target lladdr option SHOULD NOT be included.
95853541Sshin	 */
95962587Sitojun	if (tlladdr) {
96062587Sitojun		/*
96162587Sitojun		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
96262587Sitojun		 * lladdr in sdl0.  If we are not proxying (sending NA for
96362587Sitojun		 * my address) use lladdr configured for the interface.
96462587Sitojun		 */
965142215Sglebius		if (sdl0 == NULL) {
966142215Sglebius#ifdef DEV_CARP
967142215Sglebius			if (ifp->if_carp)
968142215Sglebius				mac = carp_macmatch6(ifp->if_carp, m, taddr6);
969142215Sglebius			if (mac == NULL)
970142215Sglebius				mac = nd6_ifptomac(ifp);
971142215Sglebius#else
97262587Sitojun			mac = nd6_ifptomac(ifp);
973142215Sglebius#endif
974142215Sglebius		} else if (sdl0->sa_family == AF_LINK) {
97562587Sitojun			struct sockaddr_dl *sdl;
97662587Sitojun			sdl = (struct sockaddr_dl *)sdl0;
97762587Sitojun			if (sdl->sdl_alen == ifp->if_addrlen)
97862587Sitojun				mac = LLADDR(sdl);
97962587Sitojun		}
98062587Sitojun	}
98162587Sitojun	if (tlladdr && mac) {
98253541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
98353541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
984120941Sume
98553541Sshin		/* roundup to 8 bytes alignment! */
98653541Sshin		optlen = (optlen + 7) & ~7;
98753541Sshin
98853541Sshin		m->m_pkthdr.len += optlen;
98953541Sshin		m->m_len += optlen;
99053541Sshin		icmp6len += optlen;
99153541Sshin		bzero((caddr_t)nd_opt, optlen);
99253541Sshin		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
99353541Sshin		nd_opt->nd_opt_len = optlen >> 3;
99453541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
99553541Sshin	} else
99653541Sshin		flags &= ~ND_NA_FLAG_OVERRIDE;
99753541Sshin
99853541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
99953541Sshin	nd_na->nd_na_flags_reserved = flags;
100053541Sshin	nd_na->nd_na_cksum = 0;
100153541Sshin	nd_na->nd_na_cksum =
1002120941Sume	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
100353541Sshin
1004148385Sume	ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1005148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_msg);
1006148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1007148385Sume	icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
1008148385Sume
1009148385Sume	if (ro.ro_rt) {		/* we don't cache this route. */
1010148385Sume		RTFREE(ro.ro_rt);
101153541Sshin	}
1012148385Sume	return;
1013148385Sume
1014148385Sume  bad:
1015148385Sume	if (ro.ro_rt) {
1016148385Sume		RTFREE(ro.ro_rt);
1017148385Sume	}
1018148385Sume	m_freem(m);
1019148385Sume	return;
102053541Sshin}
102153541Sshin
102253541Sshincaddr_t
102353541Sshinnd6_ifptomac(ifp)
102453541Sshin	struct ifnet *ifp;
102553541Sshin{
102653541Sshin	switch (ifp->if_type) {
102753541Sshin	case IFT_ARCNET:
102853541Sshin	case IFT_ETHER:
102953541Sshin	case IFT_FDDI:
103078064Sume	case IFT_IEEE1394:
103178468Ssumikawa#ifdef IFT_L2VLAN
103278468Ssumikawa	case IFT_L2VLAN:
103378468Ssumikawa#endif
103478064Sume#ifdef IFT_IEEE80211
103578064Sume	case IFT_IEEE80211:
103678064Sume#endif
1037142215Sglebius#ifdef IFT_CARP
1038142215Sglebius	case IFT_CARP:
1039142215Sglebius#endif
1040149829Sthompsa	case IFT_BRIDGE:
1041120049Smdodd	case IFT_ISO88025:
1042147306Sbrooks		return IF_LLADDR(ifp);
104353541Sshin	default:
104453541Sshin		return NULL;
104553541Sshin	}
104653541Sshin}
104753541Sshin
104860938SjakeTAILQ_HEAD(dadq_head, dadq);
104953541Sshinstruct dadq {
105060938Sjake	TAILQ_ENTRY(dadq) dad_list;
105153541Sshin	struct ifaddr *dad_ifa;
105253541Sshin	int dad_count;		/* max NS to send */
105362587Sitojun	int dad_ns_tcount;	/* # of trials to send NS */
105453541Sshin	int dad_ns_ocount;	/* NS sent so far */
105553541Sshin	int dad_ns_icount;
105653541Sshin	int dad_na_icount;
105778064Sume	struct callout dad_timer_ch;
105853541Sshin};
105953541Sshin
106053541Sshinstatic struct dadq_head dadq;
106178064Sumestatic int dad_init = 0;
106253541Sshin
106353541Sshinstatic struct dadq *
106453541Sshinnd6_dad_find(ifa)
106553541Sshin	struct ifaddr *ifa;
106653541Sshin{
106753541Sshin	struct dadq *dp;
106853541Sshin
106962587Sitojun	for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
107053541Sshin		if (dp->dad_ifa == ifa)
107153541Sshin			return dp;
107253541Sshin	}
107353541Sshin	return NULL;
107453541Sshin}
107553541Sshin
107678064Sumestatic void
107778064Sumend6_dad_starttimer(dp, ticks)
107878064Sume	struct dadq *dp;
107978064Sume	int ticks;
108078064Sume{
108178064Sume
108278064Sume	callout_reset(&dp->dad_timer_ch, ticks,
108378064Sume	    (void (*) __P((void *)))nd6_dad_timer, (void *)dp->dad_ifa);
108478064Sume}
108578064Sume
108678064Sumestatic void
108778064Sumend6_dad_stoptimer(dp)
108878064Sume	struct dadq *dp;
108978064Sume{
109078064Sume
109178064Sume	callout_stop(&dp->dad_timer_ch);
109278064Sume}
109378064Sume
109453541Sshin/*
1095148987Sume * Start Duplicate Address Detection (DAD) for specified interface address.
109653541Sshin */
109753541Sshinvoid
1098151539Ssuznd6_dad_start(ifa, delay)
109953541Sshin	struct ifaddr *ifa;
1100151539Ssuz	int delay;
110153541Sshin{
110253541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
110353541Sshin	struct dadq *dp;
110453541Sshin
110553541Sshin	if (!dad_init) {
110653541Sshin		TAILQ_INIT(&dadq);
110753541Sshin		dad_init++;
110853541Sshin	}
110953541Sshin
111053541Sshin	/*
111153541Sshin	 * If we don't need DAD, don't do it.
111253541Sshin	 * There are several cases:
111353541Sshin	 * - DAD is disabled (ip6_dad_count == 0)
111453541Sshin	 * - the interface address is anycast
111553541Sshin	 */
111653541Sshin	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
111762587Sitojun		log(LOG_DEBUG,
111862587Sitojun			"nd6_dad_start: called with non-tentative address "
111953541Sshin			"%s(%s)\n",
112053541Sshin			ip6_sprintf(&ia->ia_addr.sin6_addr),
112153541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
112253541Sshin		return;
112353541Sshin	}
112453541Sshin	if (ia->ia6_flags & IN6_IFF_ANYCAST) {
112553541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
112653541Sshin		return;
112753541Sshin	}
112853541Sshin	if (!ip6_dad_count) {
112953541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
113053541Sshin		return;
113153541Sshin	}
1132151465Ssuz	if (ifa->ifa_ifp == NULL)
113353541Sshin		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1134120941Sume	if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
113553541Sshin		return;
1136120941Sume	}
113753541Sshin	if (nd6_dad_find(ifa) != NULL) {
113853541Sshin		/* DAD already in progress */
113953541Sshin		return;
114053541Sshin	}
114153541Sshin
114253541Sshin	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
114353541Sshin	if (dp == NULL) {
114462587Sitojun		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
114553541Sshin			"%s(%s)\n",
114653541Sshin			ip6_sprintf(&ia->ia_addr.sin6_addr),
114753541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
114853541Sshin		return;
114953541Sshin	}
115053541Sshin	bzero(dp, sizeof(*dp));
115178064Sume	callout_init(&dp->dad_timer_ch, 0);
115253541Sshin	TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
115353541Sshin
115478064Sume	nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
115578064Sume	    ip6_sprintf(&ia->ia_addr.sin6_addr)));
115653541Sshin
115753541Sshin	/*
115853541Sshin	 * Send NS packet for DAD, ip6_dad_count times.
115953541Sshin	 * Note that we must delay the first transmission, if this is the
116053541Sshin	 * first packet to be sent from the interface after interface
116153541Sshin	 * (re)initialization.
116253541Sshin	 */
116353541Sshin	dp->dad_ifa = ifa;
116495023Ssuz	IFAREF(ifa);	/* just for safety */
116553541Sshin	dp->dad_count = ip6_dad_count;
116653541Sshin	dp->dad_ns_icount = dp->dad_na_icount = 0;
116762587Sitojun	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1168151539Ssuz	if (delay == 0) {
116962587Sitojun		nd6_dad_ns_output(dp, ifa);
1170121161Sume		nd6_dad_starttimer(dp,
1171151539Ssuz		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
117253541Sshin	} else {
1173151539Ssuz		nd6_dad_starttimer(dp, delay);
117453541Sshin	}
117553541Sshin}
117653541Sshin
117778064Sume/*
117878064Sume * terminate DAD unconditionally.  used for address removals.
117978064Sume */
118078064Sumevoid
118178064Sumend6_dad_stop(ifa)
118278064Sume	struct ifaddr *ifa;
118378064Sume{
118478064Sume	struct dadq *dp;
118578064Sume
118678064Sume	if (!dad_init)
118778064Sume		return;
118878064Sume	dp = nd6_dad_find(ifa);
118978064Sume	if (!dp) {
119078064Sume		/* DAD wasn't started yet */
119178064Sume		return;
119278064Sume	}
119378064Sume
119478064Sume	nd6_dad_stoptimer(dp);
119578064Sume
119678064Sume	TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
119778064Sume	free(dp, M_IP6NDP);
119878064Sume	dp = NULL;
119978064Sume	IFAFREE(ifa);
120078064Sume}
120178064Sume
120253541Sshinstatic void
120353541Sshinnd6_dad_timer(ifa)
120453541Sshin	struct ifaddr *ifa;
120553541Sshin{
120653541Sshin	int s;
120753541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
120853541Sshin	struct dadq *dp;
120953541Sshin
121095023Ssuz	s = splnet();		/* XXX */
121153541Sshin
121253541Sshin	/* Sanity check */
121353541Sshin	if (ia == NULL) {
121462587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
121553541Sshin		goto done;
121653541Sshin	}
121753541Sshin	dp = nd6_dad_find(ifa);
121853541Sshin	if (dp == NULL) {
121962587Sitojun		log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
122053541Sshin		goto done;
122153541Sshin	}
122253541Sshin	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
122362587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
122453541Sshin			"%s(%s)\n",
122553541Sshin			ip6_sprintf(&ia->ia_addr.sin6_addr),
122653541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
122753541Sshin		goto done;
122853541Sshin	}
122953541Sshin	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
123062587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
123153541Sshin			"%s(%s)\n",
123253541Sshin			ip6_sprintf(&ia->ia_addr.sin6_addr),
123353541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
123453541Sshin		goto done;
123553541Sshin	}
123653541Sshin
123762587Sitojun	/* timeouted with IFF_{RUNNING,UP} check */
123862587Sitojun	if (dp->dad_ns_tcount > dad_maxtry) {
123978064Sume		nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1240120941Sume		    if_name(ifa->ifa_ifp)));
124162587Sitojun
124262587Sitojun		TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
124362587Sitojun		free(dp, M_IP6NDP);
124462587Sitojun		dp = NULL;
124562587Sitojun		IFAFREE(ifa);
124662587Sitojun		goto done;
124762587Sitojun	}
124862587Sitojun
124953541Sshin	/* Need more checks? */
125053541Sshin	if (dp->dad_ns_ocount < dp->dad_count) {
125153541Sshin		/*
125253541Sshin		 * We have more NS to go.  Send NS packet for DAD.
125353541Sshin		 */
125462587Sitojun		nd6_dad_ns_output(dp, ifa);
1255120941Sume		nd6_dad_starttimer(dp,
1256151539Ssuz		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
125753541Sshin	} else {
125853541Sshin		/*
125953541Sshin		 * We have transmitted sufficient number of DAD packets.
126053541Sshin		 * See what we've got.
126153541Sshin		 */
126253541Sshin		int duplicate;
126353541Sshin
126453541Sshin		duplicate = 0;
126553541Sshin
126653541Sshin		if (dp->dad_na_icount) {
126753541Sshin			/*
126853541Sshin			 * the check is in nd6_dad_na_input(),
126953541Sshin			 * but just in case
127053541Sshin			 */
127153541Sshin			duplicate++;
127253541Sshin		}
127353541Sshin
127453541Sshin		if (dp->dad_ns_icount) {
127553541Sshin			/* We've seen NS, means DAD has failed. */
127653541Sshin			duplicate++;
127753541Sshin		}
127853541Sshin
127953541Sshin		if (duplicate) {
128053541Sshin			/* (*dp) will be freed in nd6_dad_duplicated() */
128153541Sshin			dp = NULL;
128253541Sshin			nd6_dad_duplicated(ifa);
128353541Sshin		} else {
128453541Sshin			/*
128553541Sshin			 * We are done with DAD.  No NA came, no NS came.
1286148987Sume			 * No duplicate address found.
128753541Sshin			 */
128853541Sshin			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
128953541Sshin
129078064Sume			nd6log((LOG_DEBUG,
129162587Sitojun			    "%s: DAD complete for %s - no duplicates found\n",
129262587Sitojun			    if_name(ifa->ifa_ifp),
129378064Sume			    ip6_sprintf(&ia->ia_addr.sin6_addr)));
129453541Sshin
129553541Sshin			TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
129653541Sshin			free(dp, M_IP6NDP);
129753541Sshin			dp = NULL;
129862587Sitojun			IFAFREE(ifa);
129953541Sshin		}
130053541Sshin	}
130153541Sshin
130253541Sshindone:
130353541Sshin	splx(s);
130453541Sshin}
130553541Sshin
130653541Sshinvoid
130753541Sshinnd6_dad_duplicated(ifa)
130853541Sshin	struct ifaddr *ifa;
130953541Sshin{
131053541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1311151477Ssuz	struct ifnet *ifp;
131253541Sshin	struct dadq *dp;
131353541Sshin
131453541Sshin	dp = nd6_dad_find(ifa);
131553541Sshin	if (dp == NULL) {
131662587Sitojun		log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
131753541Sshin		return;
131853541Sshin	}
131953541Sshin
132078064Sume	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
132178064Sume	    "NS in/out=%d/%d, NA in=%d\n",
132278064Sume	    if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
132378064Sume	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
132453541Sshin
132553541Sshin	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
132653541Sshin	ia->ia6_flags |= IN6_IFF_DUPLICATED;
132753541Sshin
1328148987Sume	/* We are done with DAD, with duplicate address found. (failure) */
132978064Sume	nd6_dad_stoptimer(dp);
133053541Sshin
1331151477Ssuz	ifp = ifa->ifa_ifp;
133262587Sitojun	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1333151477Ssuz	    if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
133462587Sitojun	log(LOG_ERR, "%s: manual intervention required\n",
1335151477Ssuz	    if_name(ifp));
133653541Sshin
1337151477Ssuz	/*
1338151477Ssuz	 * If the address is a link-local address formed from an interface
1339151477Ssuz	 * identifier based on the hardware address which is supposed to be
1340151477Ssuz	 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1341151477Ssuz	 * operation on the interface SHOULD be disabled.
1342151477Ssuz	 * [rfc2462bis-03 Section 5.4.5]
1343151477Ssuz	 */
1344151477Ssuz	if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1345151477Ssuz		struct in6_addr in6;
1346151477Ssuz
1347151477Ssuz		/*
1348151477Ssuz		 * To avoid over-reaction, we only apply this logic when we are
1349151477Ssuz		 * very sure that hardware addresses are supposed to be unique.
1350151477Ssuz		 */
1351151477Ssuz		switch (ifp->if_type) {
1352151477Ssuz		case IFT_ETHER:
1353151477Ssuz		case IFT_FDDI:
1354151477Ssuz		case IFT_ATM:
1355151477Ssuz		case IFT_IEEE1394:
1356151477Ssuz#ifdef IFT_IEEE80211
1357151477Ssuz		case IFT_IEEE80211:
1358151477Ssuz#endif
1359151477Ssuz			in6 = ia->ia_addr.sin6_addr;
1360151477Ssuz			if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1361151477Ssuz			    IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1362151477Ssuz				ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1363151477Ssuz				log(LOG_ERR, "%s: possible hardware address "
1364151477Ssuz				    "duplication detected, disable IPv6\n",
1365151477Ssuz				    if_name(ifp));
1366151477Ssuz			}
1367151477Ssuz			break;
1368151477Ssuz		}
1369151477Ssuz	}
1370151477Ssuz
137153541Sshin	TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
137253541Sshin	free(dp, M_IP6NDP);
137353541Sshin	dp = NULL;
137462587Sitojun	IFAFREE(ifa);
137553541Sshin}
137653541Sshin
137762587Sitojunstatic void
137862587Sitojunnd6_dad_ns_output(dp, ifa)
137962587Sitojun	struct dadq *dp;
138062587Sitojun	struct ifaddr *ifa;
138162587Sitojun{
138262587Sitojun	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
138362587Sitojun	struct ifnet *ifp = ifa->ifa_ifp;
138462587Sitojun
138562587Sitojun	dp->dad_ns_tcount++;
138662587Sitojun	if ((ifp->if_flags & IFF_UP) == 0) {
138762587Sitojun		return;
138862587Sitojun	}
1389148887Srwatson	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
139062587Sitojun		return;
139162587Sitojun	}
139262587Sitojun
139362587Sitojun	dp->dad_ns_ocount++;
139462587Sitojun	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
139562587Sitojun}
139662587Sitojun
139762587Sitojunstatic void
139853541Sshinnd6_dad_ns_input(ifa)
139953541Sshin	struct ifaddr *ifa;
140053541Sshin{
140153541Sshin	struct in6_ifaddr *ia;
140253541Sshin	struct ifnet *ifp;
140378064Sume	const struct in6_addr *taddr6;
140453541Sshin	struct dadq *dp;
140553541Sshin	int duplicate;
140653541Sshin
1407151465Ssuz	if (ifa == NULL)
140853541Sshin		panic("ifa == NULL in nd6_dad_ns_input");
140953541Sshin
141053541Sshin	ia = (struct in6_ifaddr *)ifa;
141153541Sshin	ifp = ifa->ifa_ifp;
141253541Sshin	taddr6 = &ia->ia_addr.sin6_addr;
141353541Sshin	duplicate = 0;
141453541Sshin	dp = nd6_dad_find(ifa);
141553541Sshin
141653541Sshin	/* Quickhack - completely ignore DAD NS packets */
141753541Sshin	if (dad_ignore_ns) {
141878064Sume		nd6log((LOG_INFO,
141978064Sume		    "nd6_dad_ns_input: ignoring DAD NS packet for "
142053541Sshin		    "address %s(%s)\n", ip6_sprintf(taddr6),
142178064Sume		    if_name(ifa->ifa_ifp)));
142253541Sshin		return;
142353541Sshin	}
142453541Sshin
142553541Sshin	/*
142653541Sshin	 * if I'm yet to start DAD, someone else started using this address
142753541Sshin	 * first.  I have a duplicate and you win.
142853541Sshin	 */
1429151465Ssuz	if (dp == NULL || dp->dad_ns_ocount == 0)
143053541Sshin		duplicate++;
143153541Sshin
143253541Sshin	/* XXX more checks for loopback situation - see nd6_dad_timer too */
143353541Sshin
143453541Sshin	if (duplicate) {
143553541Sshin		dp = NULL;	/* will be freed in nd6_dad_duplicated() */
143653541Sshin		nd6_dad_duplicated(ifa);
143753541Sshin	} else {
143853541Sshin		/*
143953541Sshin		 * not sure if I got a duplicate.
144053541Sshin		 * increment ns count and see what happens.
144153541Sshin		 */
144253541Sshin		if (dp)
144353541Sshin			dp->dad_ns_icount++;
144453541Sshin	}
144553541Sshin}
144653541Sshin
144762587Sitojunstatic void
144853541Sshinnd6_dad_na_input(ifa)
144953541Sshin	struct ifaddr *ifa;
145053541Sshin{
145153541Sshin	struct dadq *dp;
145253541Sshin
1453151465Ssuz	if (ifa == NULL)
145453541Sshin		panic("ifa == NULL in nd6_dad_na_input");
145553541Sshin
145653541Sshin	dp = nd6_dad_find(ifa);
145753541Sshin	if (dp)
145853541Sshin		dp->dad_na_icount++;
145953541Sshin
146053541Sshin	/* remove the address. */
146153541Sshin	nd6_dad_duplicated(ifa);
146253541Sshin}
1463