nd6_nbr.c revision 231852
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 231852 2012-02-17 02:39:58Z bz $");
34174510Sobrien
3562587Sitojun#include "opt_inet.h"
3662587Sitojun#include "opt_inet6.h"
37142215Sglebius#include "opt_ipsec.h"
38178167Sqingli#include "opt_mpath.h"
3955009Sshin
4053541Sshin#include <sys/param.h>
4153541Sshin#include <sys/systm.h>
4253541Sshin#include <sys/malloc.h>
43186119Sqingli#include <sys/lock.h>
44186119Sqingli#include <sys/rwlock.h>
4553541Sshin#include <sys/mbuf.h>
4653541Sshin#include <sys/socket.h>
4753541Sshin#include <sys/sockio.h>
4853541Sshin#include <sys/time.h>
4953541Sshin#include <sys/kernel.h>
5053541Sshin#include <sys/errno.h>
5153541Sshin#include <sys/syslog.h>
5253541Sshin#include <sys/queue.h>
5378064Sume#include <sys/callout.h>
5453541Sshin
5553541Sshin#include <net/if.h>
5653541Sshin#include <net/if_types.h>
5753541Sshin#include <net/if_dl.h>
58147306Sbrooks#include <net/if_var.h>
5953541Sshin#include <net/route.h>
60178167Sqingli#ifdef RADIX_MPATH
61178167Sqingli#include <net/radix_mpath.h>
62178167Sqingli#endif
6353541Sshin
6453541Sshin#include <netinet/in.h>
6553541Sshin#include <netinet/in_var.h>
66186119Sqingli#include <net/if_llatbl.h>
67186119Sqingli#define	L3_ADDR_SIN6(le)	((struct sockaddr_in6 *) L3_ADDR(le))
6853541Sshin#include <netinet6/in6_var.h>
69151477Ssuz#include <netinet6/in6_ifattach.h>
7062587Sitojun#include <netinet/ip6.h>
7153541Sshin#include <netinet6/ip6_var.h>
72148385Sume#include <netinet6/scope6_var.h>
7353541Sshin#include <netinet6/nd6.h>
7462587Sitojun#include <netinet/icmp6.h>
75211193Swill#include <netinet/ip_carp.h>
76211501Sanchie#include <netinet6/send.h>
7753541Sshin
7862587Sitojun#define SDL(s) ((struct sockaddr_dl *)s)
7953541Sshin
8062587Sitojunstruct dadq;
81175162Sobrienstatic struct dadq *nd6_dad_find(struct ifaddr *);
82175162Sobrienstatic void nd6_dad_starttimer(struct dadq *, int);
83175162Sobrienstatic void nd6_dad_stoptimer(struct dadq *);
84191816Szecstatic void nd6_dad_timer(struct dadq *);
85175162Sobrienstatic void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
86175162Sobrienstatic void nd6_dad_ns_input(struct ifaddr *);
87175162Sobrienstatic void nd6_dad_na_input(struct ifaddr *);
88231852Sbzstatic void nd6_na_output_fib(struct ifnet *, const struct in6_addr *,
89231852Sbz    const struct in6_addr *, u_long, int, struct sockaddr *, u_int);
9053541Sshin
91207369SbzVNET_DEFINE(int, dad_ignore_ns) = 0;	/* ignore NS in DAD - specwise incorrect*/
92207369SbzVNET_DEFINE(int, dad_maxtry) = 15;	/* max # of *tries* to transmit DAD packet */
93195727Srwatson#define	V_dad_ignore_ns			VNET(dad_ignore_ns)
94195727Srwatson#define	V_dad_maxtry			VNET(dad_maxtry)
95195699Srwatson
9653541Sshin/*
97108470Sschweikh * Input a Neighbor Solicitation Message.
9853541Sshin *
9953541Sshin * Based on RFC 2461
100148987Sume * Based on RFC 2462 (duplicate address detection)
10153541Sshin */
10253541Sshinvoid
103171259Sdelphijnd6_ns_input(struct mbuf *m, int off, int icmp6len)
10453541Sshin{
10553541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
10653541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
10762587Sitojun	struct nd_neighbor_solicit *nd_ns;
10853541Sshin	struct in6_addr saddr6 = ip6->ip6_src;
10953541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
11062587Sitojun	struct in6_addr taddr6;
11153541Sshin	struct in6_addr myaddr6;
11253541Sshin	char *lladdr = NULL;
113142215Sglebius	struct ifaddr *ifa = NULL;
11453541Sshin	int lladdrlen = 0;
11553541Sshin	int anycast = 0, proxy = 0, tentative = 0;
11653541Sshin	int tlladdr;
117222728Shrs	int rflag;
11853541Sshin	union nd_opts ndopts;
119219562Sbz	struct sockaddr_dl proxydl;
120165118Sbz	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
12153541Sshin
122222728Shrs	rflag = (V_ip6_forwarding) ? ND_NA_FLAG_ROUTER : 0;
123222728Shrs	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV && V_ip6_norbit_raif)
124222728Shrs		rflag = 0;
12578064Sume#ifndef PULLDOWN_TEST
12678064Sume	IP6_EXTHDR_CHECK(m, off, icmp6len,);
12778064Sume	nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
12878064Sume#else
12978064Sume	IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
13078064Sume	if (nd_ns == NULL) {
131190964Srwatson		ICMP6STAT_INC(icp6s_tooshort);
13278064Sume		return;
13378064Sume	}
13478064Sume#endif
13578064Sume	ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
13678064Sume	taddr6 = nd_ns->nd_ns_target;
137148385Sume	if (in6_setscope(&taddr6, ifp, NULL) != 0)
138148385Sume		goto bad;
13978064Sume
14053541Sshin	if (ip6->ip6_hlim != 255) {
14178064Sume		nd6log((LOG_ERR,
14278064Sume		    "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
143165118Sbz		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
144165118Sbz		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
14578064Sume		goto bad;
14653541Sshin	}
14753541Sshin
14853541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
149148987Sume		/* dst has to be a solicited node multicast address. */
150120941Sume		if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
15195023Ssuz		    /* don't check ifindex portion */
152120941Sume		    daddr6.s6_addr32[1] == 0 &&
153120941Sume		    daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
154120941Sume		    daddr6.s6_addr8[12] == 0xff) {
15595023Ssuz			; /* good */
15653541Sshin		} else {
15778064Sume			nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
158120941Sume			    "(wrong ip6 dst)\n"));
15953541Sshin			goto bad;
16053541Sshin		}
161185348Szec	} else if (!V_nd6_onlink_ns_rfc4861) {
162183529Scperciva		struct sockaddr_in6 src_sa6;
163183529Scperciva
164183529Scperciva		/*
165183529Scperciva		 * According to recent IETF discussions, it is not a good idea
166183529Scperciva		 * to accept a NS from an address which would not be deemed
167183529Scperciva		 * to be a neighbor otherwise.  This point is expected to be
168183529Scperciva		 * clarified in future revisions of the specification.
169183529Scperciva		 */
170183529Scperciva		bzero(&src_sa6, sizeof(src_sa6));
171183529Scperciva		src_sa6.sin6_family = AF_INET6;
172183529Scperciva		src_sa6.sin6_len = sizeof(src_sa6);
173183529Scperciva		src_sa6.sin6_addr = saddr6;
174186119Sqingli		if (nd6_is_addr_neighbor(&src_sa6, ifp) == 0) {
175183529Scperciva			nd6log((LOG_INFO, "nd6_ns_input: "
176183529Scperciva				"NS packet from non-neighbor\n"));
177183529Scperciva			goto bad;
178183529Scperciva		}
17953541Sshin	}
18053541Sshin
18153541Sshin	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
18278064Sume		nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
18353541Sshin		goto bad;
18453541Sshin	}
18553541Sshin
18653541Sshin	icmp6len -= sizeof(*nd_ns);
18753541Sshin	nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
18853541Sshin	if (nd6_options(&ndopts) < 0) {
18978064Sume		nd6log((LOG_INFO,
19078064Sume		    "nd6_ns_input: invalid ND option, ignored\n"));
19178064Sume		/* nd6_options have incremented stats */
19278064Sume		goto freeit;
19353541Sshin	}
19453541Sshin
19553541Sshin	if (ndopts.nd_opts_src_lladdr) {
19695023Ssuz		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
19753541Sshin		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
19853541Sshin	}
199120941Sume
20053541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
20178064Sume		nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
20278064Sume		    "(link-layer address option)\n"));
20353541Sshin		goto bad;
20453541Sshin	}
20553541Sshin
20653541Sshin	/*
20753541Sshin	 * Attaching target link-layer address to the NA?
20853541Sshin	 * (RFC 2461 7.2.4)
20953541Sshin	 *
21053541Sshin	 * NS IP dst is unicast/anycast			MUST NOT add
21153541Sshin	 * NS IP dst is solicited-node multicast	MUST add
21253541Sshin	 *
21353541Sshin	 * In implementation, we add target link-layer address by default.
21453541Sshin	 * We do not add one in MUST NOT cases.
21553541Sshin	 */
21653541Sshin	if (!IN6_IS_ADDR_MULTICAST(&daddr6))
21753541Sshin		tlladdr = 0;
21853541Sshin	else
21953541Sshin		tlladdr = 1;
22053541Sshin
22153541Sshin	/*
22253541Sshin	 * Target address (taddr6) must be either:
22353541Sshin	 * (1) Valid unicast/anycast address for my receiving interface,
22453541Sshin	 * (2) Unicast address for which I'm offering proxy service, or
22553541Sshin	 * (3) "tentative" address on which DAD is being performed.
22653541Sshin	 */
22753541Sshin	/* (1) and (3) check. */
228142215Sglebius	if (ifp->if_carp)
229211157Swill		ifa = (*carp_iamatch6_p)(ifp, &taddr6);
230228571Sglebius	else
231142215Sglebius		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
23253541Sshin
23353541Sshin	/* (2) check. */
234151465Ssuz	if (ifa == NULL) {
23553541Sshin		struct rtentry *rt;
23653541Sshin		struct sockaddr_in6 tsin6;
237121765Ssam		int need_proxy;
238178167Sqingli#ifdef RADIX_MPATH
239178167Sqingli		struct route_in6 ro;
240178167Sqingli#endif
24153541Sshin
242171260Sdelphij		bzero(&tsin6, sizeof tsin6);
24353541Sshin		tsin6.sin6_len = sizeof(struct sockaddr_in6);
24453541Sshin		tsin6.sin6_family = AF_INET6;
24553541Sshin		tsin6.sin6_addr = taddr6;
24653541Sshin
247231852Sbz		/* Always use the default FIB. */
248178167Sqingli#ifdef RADIX_MPATH
249178167Sqingli		bzero(&ro, sizeof(ro));
250178167Sqingli		ro.ro_dst = tsin6;
251231852Sbz		rtalloc_mpath_fib((struct route *)&ro, RTF_ANNOUNCE,
252231852Sbz		    RT_DEFAULT_FIB);
253178167Sqingli		rt = ro.ro_rt;
254178167Sqingli#else
255231852Sbz		rt = in6_rtalloc1((struct sockaddr *)&tsin6, 0, 0,
256231852Sbz		    RT_DEFAULT_FIB);
257178167Sqingli#endif
258121765Ssam		need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
259121765Ssam		    rt->rt_gateway->sa_family == AF_LINK);
260219562Sbz		if (rt != NULL) {
261219562Sbz			/*
262219562Sbz			 * Make a copy while we can be sure that rt_gateway
263219562Sbz			 * is still stable before unlocking to avoid lock
264219562Sbz			 * order problems.  proxydl will only be used if
265219562Sbz			 * proxy will be set in the next block.
266219562Sbz			 */
267219562Sbz			if (need_proxy)
268219562Sbz				proxydl = *SDL(rt->rt_gateway);
269187946Sbz			RTFREE_LOCKED(rt);
270219562Sbz		}
271121765Ssam		if (need_proxy) {
27253541Sshin			/*
27362587Sitojun			 * proxy NDP for single entry
27453541Sshin			 */
27562587Sitojun			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
27662587Sitojun				IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
277219562Sbz			if (ifa)
27853541Sshin				proxy = 1;
27953541Sshin		}
28053541Sshin	}
281151479Ssuz	if (ifa == NULL) {
28253541Sshin		/*
28378064Sume		 * We've got an NS packet, and we don't have that adddress
28453541Sshin		 * assigned for us.  We MUST silently ignore it.
28553541Sshin		 * See RFC2461 7.2.3.
28653541Sshin		 */
28762587Sitojun		goto freeit;
28853541Sshin	}
28953541Sshin	myaddr6 = *IFA_IN6(ifa);
29053541Sshin	anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
29153541Sshin	tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
29253541Sshin	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
29362587Sitojun		goto freeit;
29453541Sshin
29553541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
296120941Sume		nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
29753541Sshin		    "(if %d, NS packet %d)\n",
298165118Sbz		    ip6_sprintf(ip6bufs, &taddr6),
299120941Sume		    ifp->if_addrlen, lladdrlen - 2));
30078064Sume		goto bad;
30153541Sshin	}
30253541Sshin
30353541Sshin	if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
304120941Sume		nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
305165118Sbz		    ip6_sprintf(ip6bufs, &saddr6)));
30662587Sitojun		goto freeit;
30753541Sshin	}
30853541Sshin
30953541Sshin	/*
31053541Sshin	 * We have neighbor solicitation packet, with target address equals to
31153541Sshin	 * one of my tentative address.
31253541Sshin	 *
31353541Sshin	 * src addr	how to process?
31453541Sshin	 * ---		---
31553541Sshin	 * multicast	of course, invalid (rejected in ip6_input)
31653541Sshin	 * unicast	somebody is doing address resolution -> ignore
31753541Sshin	 * unspec	dup address detection
31853541Sshin	 *
31953541Sshin	 * The processing is defined in RFC 2462.
32053541Sshin	 */
32153541Sshin	if (tentative) {
32253541Sshin		/*
32353541Sshin		 * If source address is unspecified address, it is for
324148987Sume		 * duplicate address detection.
32553541Sshin		 *
32653541Sshin		 * If not, the packet is for addess resolution;
32753541Sshin		 * silently ignore it.
32853541Sshin		 */
32953541Sshin		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
33053541Sshin			nd6_dad_ns_input(ifa);
33153541Sshin
33262587Sitojun		goto freeit;
33353541Sshin	}
33453541Sshin
33553541Sshin	/*
33653541Sshin	 * If the source address is unspecified address, entries must not
33753541Sshin	 * be created or updated.
33853541Sshin	 * It looks that sender is performing DAD.  Output NA toward
33953541Sshin	 * all-node multicast address, to tell the sender that I'm using
34053541Sshin	 * the address.
34153541Sshin	 * S bit ("solicited") must be zero.
34253541Sshin	 */
34353541Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
344148385Sume		struct in6_addr in6_all;
345148385Sume
346148385Sume		in6_all = in6addr_linklocal_allnodes;
347148385Sume		if (in6_setscope(&in6_all, ifp, NULL) != 0)
348148385Sume			goto bad;
349231852Sbz		nd6_na_output_fib(ifp, &in6_all, &taddr6,
350120941Sume		    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
351231852Sbz		    rflag, tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL,
352231852Sbz		    M_GETFIB(m));
35362587Sitojun		goto freeit;
35453541Sshin	}
35553541Sshin
356120941Sume	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
357120941Sume	    ND_NEIGHBOR_SOLICIT, 0);
35853541Sshin
359231852Sbz	nd6_na_output_fib(ifp, &saddr6, &taddr6,
360120941Sume	    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
361222728Shrs	    rflag | ND_NA_FLAG_SOLICITED, tlladdr,
362231852Sbz	    proxy ? (struct sockaddr *)&proxydl : NULL, M_GETFIB(m));
36362587Sitojun freeit:
364194760Srwatson	if (ifa != NULL)
365194760Srwatson		ifa_free(ifa);
36662587Sitojun	m_freem(m);
36753541Sshin	return;
36853541Sshin
36953541Sshin bad:
370165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
371165118Sbz		ip6_sprintf(ip6bufs, &saddr6)));
372165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
373165118Sbz		ip6_sprintf(ip6bufs, &daddr6)));
374165118Sbz	nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
375165118Sbz		ip6_sprintf(ip6bufs, &taddr6)));
376190964Srwatson	ICMP6STAT_INC(icp6s_badns);
377194760Srwatson	if (ifa != NULL)
378194760Srwatson		ifa_free(ifa);
37962587Sitojun	m_freem(m);
38053541Sshin}
38153541Sshin
38253541Sshin/*
383108470Sschweikh * Output a Neighbor Solicitation Message. Caller specifies:
38453541Sshin *	- ICMP6 header source IP6 address
38553541Sshin *	- ND6 header target IP6 address
38653541Sshin *	- ND6 header source datalink address
38753541Sshin *
38853541Sshin * Based on RFC 2461
389148987Sume * Based on RFC 2462 (duplicate address detection)
390171259Sdelphij *
391171259Sdelphij *   ln - for source address determination
392171259Sdelphij *  dad - duplicate address detection
39353541Sshin */
39453541Sshinvoid
395186119Sqinglind6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
396186119Sqingli    const struct in6_addr *taddr6, struct llentry *ln, int dad)
39753541Sshin{
39853541Sshin	struct mbuf *m;
399211501Sanchie	struct m_tag *mtag;
40053541Sshin	struct ip6_hdr *ip6;
40153541Sshin	struct nd_neighbor_solicit *nd_ns;
40253541Sshin	struct ip6_moptions im6o;
40353541Sshin	int icmp6len;
40462587Sitojun	int maxlen;
40553541Sshin	caddr_t mac;
406148385Sume	struct route_in6 ro;
407120941Sume
40853541Sshin	if (IN6_IS_ADDR_MULTICAST(taddr6))
40953541Sshin		return;
41053541Sshin
41162587Sitojun	/* estimate the size of message */
41262587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
41362587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
41462587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
41562587Sitojun#ifdef DIAGNOSTIC
41662587Sitojun		printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
41762587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
41862587Sitojun#endif
41953541Sshin		return;
42062587Sitojun	}
42153541Sshin
422111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
42362587Sitojun	if (m && max_linkhdr + maxlen >= MHLEN) {
424111119Simp		MCLGET(m, M_DONTWAIT);
42562587Sitojun		if ((m->m_flags & M_EXT) == 0) {
42662587Sitojun			m_free(m);
42762587Sitojun			m = NULL;
42862587Sitojun		}
42962587Sitojun	}
43062587Sitojun	if (m == NULL)
43162587Sitojun		return;
43278064Sume	m->m_pkthdr.rcvif = NULL;
43362587Sitojun
434215559Sbz	bzero(&ro, sizeof(ro));
435215559Sbz
43653541Sshin	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
43753541Sshin		m->m_flags |= M_MCAST;
43853541Sshin		im6o.im6o_multicast_ifp = ifp;
43953541Sshin		im6o.im6o_multicast_hlim = 255;
44053541Sshin		im6o.im6o_multicast_loop = 0;
44153541Sshin	}
44253541Sshin
44353541Sshin	icmp6len = sizeof(*nd_ns);
44453541Sshin	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
44595023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
44653541Sshin
44753541Sshin	/* fill neighbor solicitation packet */
44853541Sshin	ip6 = mtod(m, struct ip6_hdr *);
44953541Sshin	ip6->ip6_flow = 0;
45062587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
45162587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
45253541Sshin	/* ip6->ip6_plen will be set later */
45353541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
45453541Sshin	ip6->ip6_hlim = 255;
45553541Sshin	if (daddr6)
45653541Sshin		ip6->ip6_dst = *daddr6;
45753541Sshin	else {
45853541Sshin		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
459148385Sume		ip6->ip6_dst.s6_addr16[1] = 0;
46053541Sshin		ip6->ip6_dst.s6_addr32[1] = 0;
46153541Sshin		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
46253541Sshin		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
46353541Sshin		ip6->ip6_dst.s6_addr8[12] = 0xff;
464148385Sume		if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
465148385Sume			goto bad;
46653541Sshin	}
46753541Sshin	if (!dad) {
468194760Srwatson		struct ifaddr *ifa;
469194760Srwatson
47053541Sshin		/*
47153541Sshin		 * RFC2461 7.2.2:
47253541Sshin		 * "If the source address of the packet prompting the
47353541Sshin		 * solicitation is the same as one of the addresses assigned
47453541Sshin		 * to the outgoing interface, that address SHOULD be placed
47553541Sshin		 * in the IP Source Address of the outgoing solicitation.
47653541Sshin		 * Otherwise, any one of the addresses assigned to the
47753541Sshin		 * interface should be used."
47853541Sshin		 *
47953541Sshin		 * We use the source address for the prompting packet
48053541Sshin		 * (saddr6), if:
48153541Sshin		 * - saddr6 is given from the caller (by giving "ln"), and
48253541Sshin		 * - saddr6 belongs to the outgoing interface.
483148385Sume		 * Otherwise, we perform the source address selection as usual.
48453541Sshin		 */
485216022Sbz		struct in6_addr *hsrc;
48653541Sshin
487216022Sbz		hsrc = NULL;
488216022Sbz		if (ln != NULL) {
489216022Sbz			LLE_RLOCK(ln);
490216022Sbz			if (ln->la_hold != NULL) {
491216022Sbz				struct ip6_hdr *hip6;		/* hold ip6 */
492216022Sbz
493216022Sbz				/*
494216022Sbz				 * assuming every packet in la_hold has the same IP
495216022Sbz				 * header
496216022Sbz				 */
497216022Sbz				hip6 = mtod(ln->la_hold, struct ip6_hdr *);
498216022Sbz				/* XXX pullup? */
499216022Sbz				if (sizeof(*hip6) < ln->la_hold->m_len) {
500216022Sbz					ip6->ip6_src = hip6->ip6_src;
501216022Sbz					hsrc = &hip6->ip6_src;
502216022Sbz				}
503216022Sbz			}
504216022Sbz			LLE_RUNLOCK(ln);
505148385Sume		}
506194760Srwatson		if (hsrc && (ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
507194760Srwatson		    hsrc)) != NULL) {
508216022Sbz			/* ip6_src set already. */
509194760Srwatson			ifa_free(ifa);
510194760Srwatson		} else {
511148385Sume			int error;
512148385Sume			struct sockaddr_in6 dst_sa;
513216022Sbz			struct in6_addr src_in;
514231852Sbz			struct ifnet *oifp;
515148385Sume
516148385Sume			bzero(&dst_sa, sizeof(dst_sa));
517148385Sume			dst_sa.sin6_family = AF_INET6;
518148385Sume			dst_sa.sin6_len = sizeof(dst_sa);
519148385Sume			dst_sa.sin6_addr = ip6->ip6_dst;
520148385Sume
521231852Sbz			oifp = ifp;
522194777Sbz			error = in6_selectsrc(&dst_sa, NULL,
523231852Sbz			    NULL, &ro, NULL, &oifp, &src_in);
524194777Sbz			if (error) {
525165118Sbz				char ip6buf[INET6_ADDRSTRLEN];
526148385Sume				nd6log((LOG_DEBUG,
527148385Sume				    "nd6_ns_output: source can't be "
528148385Sume				    "determined: dst=%s, error=%d\n",
529165118Sbz				    ip6_sprintf(ip6buf, &dst_sa.sin6_addr),
530165118Sbz				    error));
531148385Sume				goto bad;
53253541Sshin			}
533216022Sbz			ip6->ip6_src = src_in;
53453541Sshin		}
53553541Sshin	} else {
53653541Sshin		/*
53753541Sshin		 * Source address for DAD packet must always be IPv6
53853541Sshin		 * unspecified address. (0::0)
539148385Sume		 * We actually don't have to 0-clear the address (we did it
540148385Sume		 * above), but we do so here explicitly to make the intention
541148385Sume		 * clearer.
54253541Sshin		 */
543216022Sbz		bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
54453541Sshin	}
54553541Sshin	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
54653541Sshin	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
54753541Sshin	nd_ns->nd_ns_code = 0;
54853541Sshin	nd_ns->nd_ns_reserved = 0;
54953541Sshin	nd_ns->nd_ns_target = *taddr6;
550121315Sume	in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
55153541Sshin
55253541Sshin	/*
55353541Sshin	 * Add source link-layer address option.
55453541Sshin	 *
55553541Sshin	 *				spec		implementation
55653541Sshin	 *				---		---
55753541Sshin	 * DAD packet			MUST NOT	do not add the option
55853541Sshin	 * there's no link layer address:
55953541Sshin	 *				impossible	do not add the option
56053541Sshin	 * there's link layer address:
56153541Sshin	 *	Multicast NS		MUST add one	add the option
56253541Sshin	 *	Unicast NS		SHOULD add one	add the option
56353541Sshin	 */
56453541Sshin	if (!dad && (mac = nd6_ifptomac(ifp))) {
56553541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
56653541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
56753541Sshin		/* 8 byte alignments... */
56853541Sshin		optlen = (optlen + 7) & ~7;
569120941Sume
57053541Sshin		m->m_pkthdr.len += optlen;
57153541Sshin		m->m_len += optlen;
57253541Sshin		icmp6len += optlen;
57353541Sshin		bzero((caddr_t)nd_opt, optlen);
57453541Sshin		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
57553541Sshin		nd_opt->nd_opt_len = optlen >> 3;
57653541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
57753541Sshin	}
57853541Sshin
57953541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
58053541Sshin	nd_ns->nd_ns_cksum = 0;
581120941Sume	nd_ns->nd_ns_cksum =
582120941Sume	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
58353541Sshin
584211501Sanchie	if (send_sendso_input_hook != NULL) {
585211501Sanchie		mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
586211501Sanchie			sizeof(unsigned short), M_NOWAIT);
587211501Sanchie		if (mtag == NULL)
588211501Sanchie			goto bad;
589211501Sanchie		*(unsigned short *)(mtag + 1) = nd_ns->nd_ns_type;
590211501Sanchie		m_tag_prepend(m, mtag);
591211501Sanchie	}
592211501Sanchie
593151536Ssuz	ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
594148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_msg);
595148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
596190964Srwatson	ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_SOLICIT]);
597148385Sume
598148385Sume	if (ro.ro_rt) {		/* we don't cache this route. */
599148385Sume		RTFREE(ro.ro_rt);
60053541Sshin	}
601148385Sume	return;
602148385Sume
603148385Sume  bad:
604148385Sume	if (ro.ro_rt) {
605148385Sume		RTFREE(ro.ro_rt);
606148385Sume	}
607148385Sume	m_freem(m);
608148385Sume	return;
60953541Sshin}
61053541Sshin
61153541Sshin/*
61253541Sshin * Neighbor advertisement input handling.
61353541Sshin *
61453541Sshin * Based on RFC 2461
615148987Sume * Based on RFC 2462 (duplicate address detection)
61662587Sitojun *
61762587Sitojun * the following items are not implemented yet:
61862587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
61962587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
62053541Sshin */
62153541Sshinvoid
622171259Sdelphijnd6_na_input(struct mbuf *m, int off, int icmp6len)
62353541Sshin{
62453541Sshin	struct ifnet *ifp = m->m_pkthdr.rcvif;
62553541Sshin	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
62662587Sitojun	struct nd_neighbor_advert *nd_na;
62753541Sshin	struct in6_addr daddr6 = ip6->ip6_dst;
62862587Sitojun	struct in6_addr taddr6;
62962587Sitojun	int flags;
63062587Sitojun	int is_router;
63162587Sitojun	int is_solicited;
63262587Sitojun	int is_override;
63353541Sshin	char *lladdr = NULL;
63453541Sshin	int lladdrlen = 0;
635186468Skmacy	int checklink = 0;
63653541Sshin	struct ifaddr *ifa;
637186119Sqingli	struct llentry *ln = NULL;
63853541Sshin	union nd_opts ndopts;
639186119Sqingli	struct mbuf *chain = NULL;
640211501Sanchie	struct m_tag *mtag;
641186119Sqingli	struct sockaddr_in6 sin6;
642165118Sbz	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
64353541Sshin
64453541Sshin	if (ip6->ip6_hlim != 255) {
64578064Sume		nd6log((LOG_ERR,
64678064Sume		    "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
647165118Sbz		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
648165118Sbz		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
64978064Sume		goto bad;
65062587Sitojun	}
65162587Sitojun
65262587Sitojun#ifndef PULLDOWN_TEST
65362587Sitojun	IP6_EXTHDR_CHECK(m, off, icmp6len,);
65462587Sitojun	nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
65562587Sitojun#else
65662587Sitojun	IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
65762587Sitojun	if (nd_na == NULL) {
658190964Srwatson		ICMP6STAT_INC(icp6s_tooshort);
65953541Sshin		return;
66053541Sshin	}
66162587Sitojun#endif
662148385Sume
66362587Sitojun	flags = nd_na->nd_na_flags_reserved;
66462587Sitojun	is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
66562587Sitojun	is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
66662587Sitojun	is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
66753541Sshin
668148385Sume	taddr6 = nd_na->nd_na_target;
669148385Sume	if (in6_setscope(&taddr6, ifp, NULL))
670150202Ssuz		goto bad;	/* XXX: impossible */
67153541Sshin
67253541Sshin	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
67378064Sume		nd6log((LOG_ERR,
67453541Sshin		    "nd6_na_input: invalid target address %s\n",
675165118Sbz		    ip6_sprintf(ip6bufs, &taddr6)));
67678064Sume		goto bad;
67753541Sshin	}
67853541Sshin	if (IN6_IS_ADDR_MULTICAST(&daddr6))
67953541Sshin		if (is_solicited) {
68078064Sume			nd6log((LOG_ERR,
68178064Sume			    "nd6_na_input: a solicited adv is multicasted\n"));
68278064Sume			goto bad;
68353541Sshin		}
68453541Sshin
68553541Sshin	icmp6len -= sizeof(*nd_na);
68653541Sshin	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
68753541Sshin	if (nd6_options(&ndopts) < 0) {
68878064Sume		nd6log((LOG_INFO,
68978064Sume		    "nd6_na_input: invalid ND option, ignored\n"));
69078064Sume		/* nd6_options have incremented stats */
69162587Sitojun		goto freeit;
69253541Sshin	}
69353541Sshin
69453541Sshin	if (ndopts.nd_opts_tgt_lladdr) {
69553541Sshin		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
69653541Sshin		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
69753541Sshin	}
69853541Sshin
699228571Sglebius	/*
700228571Sglebius	 * This effectively disables the DAD check on a non-master CARP
701228571Sglebius	 * address.
702228571Sglebius	 */
703228571Sglebius	if (ifp->if_carp)
704228571Sglebius		ifa = (*carp_iamatch6_p)(ifp, &taddr6);
705228571Sglebius	else
706228571Sglebius		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
70753541Sshin
70853541Sshin	/*
70953541Sshin	 * Target address matches one of my interface address.
71053541Sshin	 *
71153541Sshin	 * If my address is tentative, this means that there's somebody
71253541Sshin	 * already using the same address as mine.  This indicates DAD failure.
71353541Sshin	 * This is defined in RFC 2462.
71453541Sshin	 *
71553541Sshin	 * Otherwise, process as defined in RFC 2461.
71653541Sshin	 */
71753541Sshin	if (ifa
71853541Sshin	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
719194760Srwatson		ifa_free(ifa);
72053541Sshin		nd6_dad_na_input(ifa);
72162587Sitojun		goto freeit;
72253541Sshin	}
72353541Sshin
72495023Ssuz	/* Just for safety, maybe unnecessary. */
72553541Sshin	if (ifa) {
726194760Srwatson		ifa_free(ifa);
72753541Sshin		log(LOG_ERR,
72853541Sshin		    "nd6_na_input: duplicate IP6 address %s\n",
729165118Sbz		    ip6_sprintf(ip6bufs, &taddr6));
73062587Sitojun		goto freeit;
73153541Sshin	}
73253541Sshin
73353541Sshin	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
734120941Sume		nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
735165118Sbz		    "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
736120941Sume		    ifp->if_addrlen, lladdrlen - 2));
73778064Sume		goto bad;
73853541Sshin	}
73953541Sshin
74053541Sshin	/*
741120941Sume	 * If no neighbor cache entry is found, NA SHOULD silently be
742120941Sume	 * discarded.
74353541Sshin	 */
744186119Sqingli	IF_AFDATA_LOCK(ifp);
745186119Sqingli	ln = nd6_lookup(&taddr6, LLE_EXCLUSIVE, ifp);
746186119Sqingli	IF_AFDATA_UNLOCK(ifp);
747186119Sqingli	if (ln == NULL) {
74862587Sitojun		goto freeit;
749186119Sqingli	}
75053541Sshin
75153541Sshin	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
75253541Sshin		/*
75353541Sshin		 * If the link-layer has address, and no lladdr option came,
75453541Sshin		 * discard the packet.
75553541Sshin		 */
756186119Sqingli		if (ifp->if_addrlen && lladdr == NULL) {
75762587Sitojun			goto freeit;
758186119Sqingli		}
75953541Sshin
76053541Sshin		/*
76153541Sshin		 * Record link-layer address, and update the state.
76253541Sshin		 */
763186119Sqingli		bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
764186119Sqingli		ln->la_flags |= LLE_VALID;
76553541Sshin		if (is_solicited) {
76653541Sshin			ln->ln_state = ND6_LLINFO_REACHABLE;
76762587Sitojun			ln->ln_byhint = 0;
768151539Ssuz			if (!ND6_LLINFO_PERMANENT(ln)) {
769186119Sqingli				nd6_llinfo_settimer_locked(ln,
770186119Sqingli				    (long)ND_IFINFO(ln->lle_tbl->llt_ifp)->reachable * hz);
771120941Sume			}
77278064Sume		} else {
77353541Sshin			ln->ln_state = ND6_LLINFO_STALE;
774186119Sqingli			nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
77578064Sume		}
77678064Sume		if ((ln->ln_router = is_router) != 0) {
77778064Sume			/*
77878064Sume			 * This means a router's state has changed from
77978064Sume			 * non-reachable to probably reachable, and might
78078064Sume			 * affect the status of associated prefixes..
78178064Sume			 */
782186468Skmacy			checklink = 1;
78378064Sume		}
78453541Sshin	} else {
78553541Sshin		int llchange;
78653541Sshin
78753541Sshin		/*
78853541Sshin		 * Check if the link-layer address has changed or not.
78953541Sshin		 */
790151465Ssuz		if (lladdr == NULL)
79153541Sshin			llchange = 0;
79253541Sshin		else {
793186119Sqingli			if (ln->la_flags & LLE_VALID) {
794186119Sqingli				if (bcmp(lladdr, &ln->ll_addr, ifp->if_addrlen))
79553541Sshin					llchange = 1;
79653541Sshin				else
79753541Sshin					llchange = 0;
79853541Sshin			} else
79953541Sshin				llchange = 1;
80053541Sshin		}
80153541Sshin
80253541Sshin		/*
80353541Sshin		 * This is VERY complex.  Look at it with care.
80453541Sshin		 *
80553541Sshin		 * override solicit lladdr llchange	action
80653541Sshin		 *					(L: record lladdr)
80753541Sshin		 *
80853541Sshin		 *	0	0	n	--	(2c)
80953541Sshin		 *	0	0	y	n	(2b) L
81053541Sshin		 *	0	0	y	y	(1)    REACHABLE->STALE
81153541Sshin		 *	0	1	n	--	(2c)   *->REACHABLE
81253541Sshin		 *	0	1	y	n	(2b) L *->REACHABLE
81353541Sshin		 *	0	1	y	y	(1)    REACHABLE->STALE
81453541Sshin		 *	1	0	n	--	(2a)
81553541Sshin		 *	1	0	y	n	(2a) L
81653541Sshin		 *	1	0	y	y	(2a) L *->STALE
81753541Sshin		 *	1	1	n	--	(2a)   *->REACHABLE
81853541Sshin		 *	1	1	y	n	(2a) L *->REACHABLE
81953541Sshin		 *	1	1	y	y	(2a) L *->REACHABLE
82053541Sshin		 */
821151539Ssuz		if (!is_override && (lladdr != NULL && llchange)) {  /* (1) */
82253541Sshin			/*
82353541Sshin			 * If state is REACHABLE, make it STALE.
82453541Sshin			 * no other updates should be done.
82553541Sshin			 */
82678064Sume			if (ln->ln_state == ND6_LLINFO_REACHABLE) {
82753541Sshin				ln->ln_state = ND6_LLINFO_STALE;
828186119Sqingli				nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
82978064Sume			}
83062587Sitojun			goto freeit;
83153541Sshin		} else if (is_override				   /* (2a) */
832151465Ssuz			|| (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
833151465Ssuz			|| lladdr == NULL) {			   /* (2c) */
83453541Sshin			/*
83553541Sshin			 * Update link-local address, if any.
83653541Sshin			 */
837151465Ssuz			if (lladdr != NULL) {
838186119Sqingli				bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
839186119Sqingli				ln->la_flags |= LLE_VALID;
84053541Sshin			}
84153541Sshin
84253541Sshin			/*
84353541Sshin			 * If solicited, make the state REACHABLE.
84453541Sshin			 * If not solicited and the link-layer address was
84553541Sshin			 * changed, make it STALE.
84653541Sshin			 */
84753541Sshin			if (is_solicited) {
84853541Sshin				ln->ln_state = ND6_LLINFO_REACHABLE;
84962587Sitojun				ln->ln_byhint = 0;
850151539Ssuz				if (!ND6_LLINFO_PERMANENT(ln)) {
851186119Sqingli					nd6_llinfo_settimer_locked(ln,
852151539Ssuz					    (long)ND_IFINFO(ifp)->reachable * hz);
85353541Sshin				}
85453541Sshin			} else {
855151465Ssuz				if (lladdr != NULL && llchange) {
85653541Sshin					ln->ln_state = ND6_LLINFO_STALE;
857186119Sqingli					nd6_llinfo_settimer_locked(ln,
858181803Sbz					    (long)V_nd6_gctimer * hz);
85978064Sume				}
86053541Sshin			}
86153541Sshin		}
86253541Sshin
86353541Sshin		if (ln->ln_router && !is_router) {
86453541Sshin			/*
86553541Sshin			 * The peer dropped the router flag.
86653541Sshin			 * Remove the sender from the Default Router List and
86753541Sshin			 * update the Destination Cache entries.
86853541Sshin			 */
86953541Sshin			struct nd_defrouter *dr;
87053541Sshin			struct in6_addr *in6;
87153541Sshin
872186119Sqingli			in6 = &L3_ADDR_SIN6(ln)->sin6_addr;
87395023Ssuz
87495023Ssuz			/*
87595023Ssuz			 * Lock to protect the default router list.
87695023Ssuz			 * XXX: this might be unnecessary, since this function
87795023Ssuz			 * is only called under the network software interrupt
878120941Sume			 * context.  However, we keep it just for safety.
87995023Ssuz			 */
880186119Sqingli			dr = defrouter_lookup(in6, ln->lle_tbl->llt_ifp);
88153541Sshin			if (dr)
88253541Sshin				defrtrlist_del(dr);
883222728Shrs			else if (ND_IFINFO(ln->lle_tbl->llt_ifp)->flags &
884222728Shrs			    ND6_IFF_ACCEPT_RTADV) {
88553541Sshin				/*
88653541Sshin				 * Even if the neighbor is not in the default
88753541Sshin				 * router list, the neighbor may be used
88853541Sshin				 * as a next hop for some destinations
88953541Sshin				 * (e.g. redirect case). So we must
89053541Sshin				 * call rt6_flush explicitly.
89153541Sshin				 */
892128422Sluigi				rt6_flush(&ip6->ip6_src, ifp);
89353541Sshin			}
89453541Sshin		}
89553541Sshin		ln->ln_router = is_router;
89653541Sshin	}
897186119Sqingli        /* XXX - QL
898186119Sqingli	 *  Does this matter?
899186119Sqingli	 *  rt->rt_flags &= ~RTF_REJECT;
900186119Sqingli	 */
901186119Sqingli	ln->la_asked = 0;
902186119Sqingli	if (ln->la_hold) {
903151539Ssuz		struct mbuf *m_hold, *m_hold_next;
904151539Ssuz
905169273Ssuz		/*
906186119Sqingli		 * reset the la_hold in advance, to explicitly
907186119Sqingli		 * prevent a la_hold lookup in nd6_output()
908169273Ssuz		 * (wouldn't happen, though...)
909169273Ssuz		 */
910186119Sqingli		for (m_hold = ln->la_hold, ln->la_hold = NULL;
911169241Ssuz		    m_hold; m_hold = m_hold_next) {
912151539Ssuz			m_hold_next = m_hold->m_nextpkt;
913169241Ssuz			m_hold->m_nextpkt = NULL;
914151539Ssuz			/*
915151539Ssuz			 * we assume ifp is not a loopback here, so just set
916151539Ssuz			 * the 2nd argument as the 1st one.
917151539Ssuz			 */
918211501Sanchie
919211501Sanchie			if (send_sendso_input_hook != NULL) {
920211501Sanchie				mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
921211501Sanchie				    sizeof(unsigned short), M_NOWAIT);
922211501Sanchie				if (mtag == NULL)
923211501Sanchie					goto bad;
924211501Sanchie				m_tag_prepend(m, mtag);
925211501Sanchie			}
926211501Sanchie
927186119Sqingli			nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain);
928151539Ssuz		}
92953541Sshin	}
930186119Sqingli freeit:
931186160Skmacy	if (ln != NULL) {
932186119Sqingli		if (chain)
933186119Sqingli			memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6));
934186119Sqingli		LLE_WUNLOCK(ln);
93562587Sitojun
936186119Sqingli		if (chain)
937186119Sqingli			nd6_output_flush(ifp, ifp, chain, &sin6, NULL);
938186119Sqingli	}
939186468Skmacy	if (checklink)
940186468Skmacy		pfxlist_onlink_check();
941186468Skmacy
94262587Sitojun	m_freem(m);
94378064Sume	return;
94478064Sume
94578064Sume bad:
946186160Skmacy	if (ln != NULL)
947186119Sqingli		LLE_WUNLOCK(ln);
948186119Sqingli
949190964Srwatson	ICMP6STAT_INC(icp6s_badna);
95078064Sume	m_freem(m);
95153541Sshin}
95253541Sshin
95353541Sshin/*
95453541Sshin * Neighbor advertisement output handling.
95553541Sshin *
95653541Sshin * Based on RFC 2461
95753541Sshin *
95862587Sitojun * the following items are not implemented yet:
95962587Sitojun * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
96062587Sitojun * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
961171259Sdelphij *
962171259Sdelphij * tlladdr - 1 if include target link-layer address
963171259Sdelphij * sdl0 - sockaddr_dl (= proxy NA) or NULL
96453541Sshin */
965231852Sbzstatic void
966231852Sbznd6_na_output_fib(struct ifnet *ifp, const struct in6_addr *daddr6_0,
967171259Sdelphij    const struct in6_addr *taddr6, u_long flags, int tlladdr,
968231852Sbz    struct sockaddr *sdl0, u_int fibnum)
96953541Sshin{
97053541Sshin	struct mbuf *m;
971211501Sanchie	struct m_tag *mtag;
972231852Sbz	struct ifnet *oifp;
97353541Sshin	struct ip6_hdr *ip6;
97453541Sshin	struct nd_neighbor_advert *nd_na;
97553541Sshin	struct ip6_moptions im6o;
976194777Sbz	struct in6_addr src, daddr6;
977148385Sume	struct sockaddr_in6 dst_sa;
978148385Sume	int icmp6len, maxlen, error;
97992733Speter	caddr_t mac = NULL;
980148385Sume	struct route_in6 ro;
98162587Sitojun
982148385Sume	bzero(&ro, sizeof(ro));
983148385Sume
984148385Sume	daddr6 = *daddr6_0;	/* make a local copy for modification */
985148385Sume
98662587Sitojun	/* estimate the size of message */
98762587Sitojun	maxlen = sizeof(*ip6) + sizeof(*nd_na);
98862587Sitojun	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
98962587Sitojun	if (max_linkhdr + maxlen >= MCLBYTES) {
99062587Sitojun#ifdef DIAGNOSTIC
99162587Sitojun		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
99262587Sitojun		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
99362587Sitojun#endif
99453541Sshin		return;
99562587Sitojun	}
99653541Sshin
997111119Simp	MGETHDR(m, M_DONTWAIT, MT_DATA);
99862587Sitojun	if (m && max_linkhdr + maxlen >= MHLEN) {
999111119Simp		MCLGET(m, M_DONTWAIT);
100062587Sitojun		if ((m->m_flags & M_EXT) == 0) {
100162587Sitojun			m_free(m);
100262587Sitojun			m = NULL;
100362587Sitojun		}
100462587Sitojun	}
100562587Sitojun	if (m == NULL)
100662587Sitojun		return;
100778064Sume	m->m_pkthdr.rcvif = NULL;
1008231852Sbz	M_SETFIB(m, fibnum);
100962587Sitojun
1010148385Sume	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
101153541Sshin		m->m_flags |= M_MCAST;
101253541Sshin		im6o.im6o_multicast_ifp = ifp;
101353541Sshin		im6o.im6o_multicast_hlim = 255;
101453541Sshin		im6o.im6o_multicast_loop = 0;
101553541Sshin	}
101653541Sshin
101753541Sshin	icmp6len = sizeof(*nd_na);
101853541Sshin	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
101995023Ssuz	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
102053541Sshin
102153541Sshin	/* fill neighbor advertisement packet */
102253541Sshin	ip6 = mtod(m, struct ip6_hdr *);
102353541Sshin	ip6->ip6_flow = 0;
102462587Sitojun	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
102562587Sitojun	ip6->ip6_vfc |= IPV6_VERSION;
102653541Sshin	ip6->ip6_nxt = IPPROTO_ICMPV6;
102753541Sshin	ip6->ip6_hlim = 255;
1028148385Sume	if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
102953541Sshin		/* reply to DAD */
1030153227Sume		daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
1031153227Sume		daddr6.s6_addr16[1] = 0;
1032153227Sume		daddr6.s6_addr32[1] = 0;
1033153227Sume		daddr6.s6_addr32[2] = 0;
1034153227Sume		daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
1035148385Sume		if (in6_setscope(&daddr6, ifp, NULL))
1036148385Sume			goto bad;
1037148385Sume
103853541Sshin		flags &= ~ND_NA_FLAG_SOLICITED;
1039148385Sume	}
1040148385Sume	ip6->ip6_dst = daddr6;
1041148385Sume	bzero(&dst_sa, sizeof(struct sockaddr_in6));
1042148385Sume	dst_sa.sin6_family = AF_INET6;
1043148385Sume	dst_sa.sin6_len = sizeof(struct sockaddr_in6);
1044148385Sume	dst_sa.sin6_addr = daddr6;
104553541Sshin
104653541Sshin	/*
104753541Sshin	 * Select a source whose scope is the same as that of the dest.
104853541Sshin	 */
1049148385Sume	bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
1050231852Sbz	oifp = ifp;
1051231852Sbz	error = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, &oifp, &src);
1052194777Sbz	if (error) {
1053165118Sbz		char ip6buf[INET6_ADDRSTRLEN];
1054148385Sume		nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
1055148385Sume		    "determined: dst=%s, error=%d\n",
1056165118Sbz		    ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
1057148385Sume		goto bad;
105853541Sshin	}
1059194777Sbz	ip6->ip6_src = src;
106053541Sshin	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
106153541Sshin	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
106253541Sshin	nd_na->nd_na_code = 0;
106353541Sshin	nd_na->nd_na_target = *taddr6;
1064121315Sume	in6_clearscope(&nd_na->nd_na_target); /* XXX */
106553541Sshin
106653541Sshin	/*
106753541Sshin	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
106853541Sshin	 * see nd6_ns_input() for details.
106953541Sshin	 * Basically, if NS packet is sent to unicast/anycast addr,
107053541Sshin	 * target lladdr option SHOULD NOT be included.
107153541Sshin	 */
107262587Sitojun	if (tlladdr) {
107362587Sitojun		/*
107462587Sitojun		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
107562587Sitojun		 * lladdr in sdl0.  If we are not proxying (sending NA for
107662587Sitojun		 * my address) use lladdr configured for the interface.
107762587Sitojun		 */
1078142215Sglebius		if (sdl0 == NULL) {
1079142215Sglebius			if (ifp->if_carp)
1080211157Swill				mac = (*carp_macmatch6_p)(ifp, m, taddr6);
1081142215Sglebius			if (mac == NULL)
1082142215Sglebius				mac = nd6_ifptomac(ifp);
1083142215Sglebius		} else if (sdl0->sa_family == AF_LINK) {
108462587Sitojun			struct sockaddr_dl *sdl;
108562587Sitojun			sdl = (struct sockaddr_dl *)sdl0;
108662587Sitojun			if (sdl->sdl_alen == ifp->if_addrlen)
108762587Sitojun				mac = LLADDR(sdl);
108862587Sitojun		}
108962587Sitojun	}
109062587Sitojun	if (tlladdr && mac) {
109153541Sshin		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
109253541Sshin		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1093120941Sume
109453541Sshin		/* roundup to 8 bytes alignment! */
109553541Sshin		optlen = (optlen + 7) & ~7;
109653541Sshin
109753541Sshin		m->m_pkthdr.len += optlen;
109853541Sshin		m->m_len += optlen;
109953541Sshin		icmp6len += optlen;
110053541Sshin		bzero((caddr_t)nd_opt, optlen);
110153541Sshin		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
110253541Sshin		nd_opt->nd_opt_len = optlen >> 3;
110353541Sshin		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
110453541Sshin	} else
110553541Sshin		flags &= ~ND_NA_FLAG_OVERRIDE;
110653541Sshin
110753541Sshin	ip6->ip6_plen = htons((u_short)icmp6len);
110853541Sshin	nd_na->nd_na_flags_reserved = flags;
110953541Sshin	nd_na->nd_na_cksum = 0;
111053541Sshin	nd_na->nd_na_cksum =
1111120941Sume	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
111253541Sshin
1113211501Sanchie	if (send_sendso_input_hook != NULL) {
1114211501Sanchie		mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
1115211501Sanchie		    sizeof(unsigned short), M_NOWAIT);
1116211501Sanchie		if (mtag == NULL)
1117211501Sanchie			goto bad;
1118211501Sanchie		*(unsigned short *)(mtag + 1) = nd_na->nd_na_type;
1119211501Sanchie		m_tag_prepend(m, mtag);
1120211501Sanchie	}
1121211501Sanchie
1122148385Sume	ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1123148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_msg);
1124148385Sume	icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1125190964Srwatson	ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_ADVERT]);
1126148385Sume
1127148385Sume	if (ro.ro_rt) {		/* we don't cache this route. */
1128148385Sume		RTFREE(ro.ro_rt);
112953541Sshin	}
1130148385Sume	return;
1131148385Sume
1132148385Sume  bad:
1133148385Sume	if (ro.ro_rt) {
1134148385Sume		RTFREE(ro.ro_rt);
1135148385Sume	}
1136148385Sume	m_freem(m);
1137148385Sume	return;
113853541Sshin}
113953541Sshin
1140231852Sbz#ifndef BURN_BRIDGES
1141231852Sbzvoid
1142231852Sbznd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
1143231852Sbz    const struct in6_addr *taddr6, u_long flags, int tlladdr,
1144231852Sbz    struct sockaddr *sdl0)
1145231852Sbz{
1146231852Sbz
1147231852Sbz	nd6_na_output_fib(ifp, daddr6_0, taddr6, flags, tlladdr, sdl0,
1148231852Sbz	    RT_DEFAULT_FIB);
1149231852Sbz}
1150231852Sbz#endif
1151231852Sbz
115253541Sshincaddr_t
1153171259Sdelphijnd6_ifptomac(struct ifnet *ifp)
115453541Sshin{
115553541Sshin	switch (ifp->if_type) {
115653541Sshin	case IFT_ARCNET:
115753541Sshin	case IFT_ETHER:
115853541Sshin	case IFT_FDDI:
115978064Sume	case IFT_IEEE1394:
116078468Ssumikawa#ifdef IFT_L2VLAN
116178468Ssumikawa	case IFT_L2VLAN:
116278468Ssumikawa#endif
116378064Sume#ifdef IFT_IEEE80211
116478064Sume	case IFT_IEEE80211:
116578064Sume#endif
1166219819Sjeff	case IFT_INFINIBAND:
1167149829Sthompsa	case IFT_BRIDGE:
1168120049Smdodd	case IFT_ISO88025:
1169147306Sbrooks		return IF_LLADDR(ifp);
117053541Sshin	default:
117153541Sshin		return NULL;
117253541Sshin	}
117353541Sshin}
117453541Sshin
117553541Sshinstruct dadq {
117660938Sjake	TAILQ_ENTRY(dadq) dad_list;
117753541Sshin	struct ifaddr *dad_ifa;
117853541Sshin	int dad_count;		/* max NS to send */
117962587Sitojun	int dad_ns_tcount;	/* # of trials to send NS */
118053541Sshin	int dad_ns_ocount;	/* NS sent so far */
118153541Sshin	int dad_ns_icount;
118253541Sshin	int dad_na_icount;
118378064Sume	struct callout dad_timer_ch;
1184191816Szec	struct vnet *dad_vnet;
118553541Sshin};
118653541Sshin
1187215701Sdimstatic VNET_DEFINE(TAILQ_HEAD(, dadq), dadq);
1188207369SbzVNET_DEFINE(int, dad_init) = 0;
1189195727Srwatson#define	V_dadq				VNET(dadq)
1190195727Srwatson#define	V_dad_init			VNET(dad_init)
1191195699Srwatson
119253541Sshinstatic struct dadq *
1193171259Sdelphijnd6_dad_find(struct ifaddr *ifa)
119453541Sshin{
119553541Sshin	struct dadq *dp;
119653541Sshin
1197226340Sglebius	TAILQ_FOREACH(dp, &V_dadq, dad_list)
119853541Sshin		if (dp->dad_ifa == ifa)
1199226340Sglebius			return (dp);
1200226340Sglebius
1201226340Sglebius	return (NULL);
120253541Sshin}
120353541Sshin
120478064Sumestatic void
1205171259Sdelphijnd6_dad_starttimer(struct dadq *dp, int ticks)
120678064Sume{
120778064Sume
120878064Sume	callout_reset(&dp->dad_timer_ch, ticks,
1209191816Szec	    (void (*)(void *))nd6_dad_timer, (void *)dp);
121078064Sume}
121178064Sume
121278064Sumestatic void
1213171259Sdelphijnd6_dad_stoptimer(struct dadq *dp)
121478064Sume{
121578064Sume
121678064Sume	callout_stop(&dp->dad_timer_ch);
121778064Sume}
121878064Sume
121953541Sshin/*
1220148987Sume * Start Duplicate Address Detection (DAD) for specified interface address.
122153541Sshin */
122253541Sshinvoid
1223171259Sdelphijnd6_dad_start(struct ifaddr *ifa, int delay)
122453541Sshin{
122553541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
122653541Sshin	struct dadq *dp;
1227165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
122853541Sshin
1229181803Sbz	if (!V_dad_init) {
1230181803Sbz		TAILQ_INIT(&V_dadq);
1231181803Sbz		V_dad_init++;
123253541Sshin	}
123353541Sshin
123453541Sshin	/*
123553541Sshin	 * If we don't need DAD, don't do it.
123653541Sshin	 * There are several cases:
123753541Sshin	 * - DAD is disabled (ip6_dad_count == 0)
123853541Sshin	 * - the interface address is anycast
123953541Sshin	 */
124053541Sshin	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
124162587Sitojun		log(LOG_DEBUG,
124262587Sitojun			"nd6_dad_start: called with non-tentative address "
124353541Sshin			"%s(%s)\n",
1244165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
124553541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
124653541Sshin		return;
124753541Sshin	}
124853541Sshin	if (ia->ia6_flags & IN6_IFF_ANYCAST) {
124953541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
125053541Sshin		return;
125153541Sshin	}
1252181803Sbz	if (!V_ip6_dad_count) {
125353541Sshin		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
125453541Sshin		return;
125553541Sshin	}
1256151465Ssuz	if (ifa->ifa_ifp == NULL)
125753541Sshin		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1258120941Sume	if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
125953541Sshin		return;
1260120941Sume	}
1261197138Shrs	if (ND_IFINFO(ifa->ifa_ifp)->flags & ND6_IFF_IFDISABLED)
1262197138Shrs		return;
126353541Sshin	if (nd6_dad_find(ifa) != NULL) {
126453541Sshin		/* DAD already in progress */
126553541Sshin		return;
126653541Sshin	}
126753541Sshin
126853541Sshin	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
126953541Sshin	if (dp == NULL) {
127062587Sitojun		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
127153541Sshin			"%s(%s)\n",
1272165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
127353541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
127453541Sshin		return;
127553541Sshin	}
127653541Sshin	bzero(dp, sizeof(*dp));
127778064Sume	callout_init(&dp->dad_timer_ch, 0);
1278191816Szec#ifdef VIMAGE
1279191816Szec	dp->dad_vnet = curvnet;
1280191816Szec#endif
1281182150Sjulian	TAILQ_INSERT_TAIL(&V_dadq, (struct dadq *)dp, dad_list);
128253541Sshin
128378064Sume	nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1284165118Sbz	    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
128553541Sshin
128653541Sshin	/*
128753541Sshin	 * Send NS packet for DAD, ip6_dad_count times.
128853541Sshin	 * Note that we must delay the first transmission, if this is the
128953541Sshin	 * first packet to be sent from the interface after interface
129053541Sshin	 * (re)initialization.
129153541Sshin	 */
129253541Sshin	dp->dad_ifa = ifa;
1293194602Srwatson	ifa_ref(ifa);	/* just for safety */
1294181803Sbz	dp->dad_count = V_ip6_dad_count;
129553541Sshin	dp->dad_ns_icount = dp->dad_na_icount = 0;
129662587Sitojun	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1297151539Ssuz	if (delay == 0) {
129862587Sitojun		nd6_dad_ns_output(dp, ifa);
1299121161Sume		nd6_dad_starttimer(dp,
1300151539Ssuz		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
130153541Sshin	} else {
1302151539Ssuz		nd6_dad_starttimer(dp, delay);
130353541Sshin	}
130453541Sshin}
130553541Sshin
130678064Sume/*
130778064Sume * terminate DAD unconditionally.  used for address removals.
130878064Sume */
130978064Sumevoid
1310171259Sdelphijnd6_dad_stop(struct ifaddr *ifa)
131178064Sume{
131278064Sume	struct dadq *dp;
131378064Sume
1314181803Sbz	if (!V_dad_init)
131578064Sume		return;
131678064Sume	dp = nd6_dad_find(ifa);
131778064Sume	if (!dp) {
131878064Sume		/* DAD wasn't started yet */
131978064Sume		return;
132078064Sume	}
132178064Sume
132278064Sume	nd6_dad_stoptimer(dp);
132378064Sume
1324181803Sbz	TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
132578064Sume	free(dp, M_IP6NDP);
132678064Sume	dp = NULL;
1327194602Srwatson	ifa_free(ifa);
132878064Sume}
132978064Sume
133053541Sshinstatic void
1331191816Szecnd6_dad_timer(struct dadq *dp)
133253541Sshin{
1333183550Szec	CURVNET_SET(dp->dad_vnet);
133453541Sshin	int s;
1335191816Szec	struct ifaddr *ifa = dp->dad_ifa;
133653541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1337165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
133853541Sshin
133995023Ssuz	s = splnet();		/* XXX */
134053541Sshin
134153541Sshin	/* Sanity check */
134253541Sshin	if (ia == NULL) {
134362587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
134453541Sshin		goto done;
134553541Sshin	}
134653541Sshin	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
134762587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
134853541Sshin			"%s(%s)\n",
1349165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
135053541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
135153541Sshin		goto done;
135253541Sshin	}
135353541Sshin	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
135462587Sitojun		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
135553541Sshin			"%s(%s)\n",
1356165118Sbz			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
135753541Sshin			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
135853541Sshin		goto done;
135953541Sshin	}
136053541Sshin
136162587Sitojun	/* timeouted with IFF_{RUNNING,UP} check */
1362181803Sbz	if (dp->dad_ns_tcount > V_dad_maxtry) {
136378064Sume		nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1364120941Sume		    if_name(ifa->ifa_ifp)));
136562587Sitojun
1366181803Sbz		TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
136762587Sitojun		free(dp, M_IP6NDP);
136862587Sitojun		dp = NULL;
1369194602Srwatson		ifa_free(ifa);
137062587Sitojun		goto done;
137162587Sitojun	}
137262587Sitojun
137353541Sshin	/* Need more checks? */
137453541Sshin	if (dp->dad_ns_ocount < dp->dad_count) {
137553541Sshin		/*
137653541Sshin		 * We have more NS to go.  Send NS packet for DAD.
137753541Sshin		 */
137862587Sitojun		nd6_dad_ns_output(dp, ifa);
1379120941Sume		nd6_dad_starttimer(dp,
1380151539Ssuz		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
138153541Sshin	} else {
138253541Sshin		/*
138353541Sshin		 * We have transmitted sufficient number of DAD packets.
138453541Sshin		 * See what we've got.
138553541Sshin		 */
138653541Sshin		int duplicate;
138753541Sshin
138853541Sshin		duplicate = 0;
138953541Sshin
139053541Sshin		if (dp->dad_na_icount) {
139153541Sshin			/*
139253541Sshin			 * the check is in nd6_dad_na_input(),
139353541Sshin			 * but just in case
139453541Sshin			 */
139553541Sshin			duplicate++;
139653541Sshin		}
139753541Sshin
139853541Sshin		if (dp->dad_ns_icount) {
139953541Sshin			/* We've seen NS, means DAD has failed. */
140053541Sshin			duplicate++;
140153541Sshin		}
140253541Sshin
140353541Sshin		if (duplicate) {
140453541Sshin			/* (*dp) will be freed in nd6_dad_duplicated() */
140553541Sshin			dp = NULL;
140653541Sshin			nd6_dad_duplicated(ifa);
140753541Sshin		} else {
140853541Sshin			/*
140953541Sshin			 * We are done with DAD.  No NA came, no NS came.
1410148987Sume			 * No duplicate address found.
141153541Sshin			 */
141253541Sshin			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
141353541Sshin
141478064Sume			nd6log((LOG_DEBUG,
141562587Sitojun			    "%s: DAD complete for %s - no duplicates found\n",
141662587Sitojun			    if_name(ifa->ifa_ifp),
1417165118Sbz			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
141853541Sshin
1419181803Sbz			TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
142053541Sshin			free(dp, M_IP6NDP);
142153541Sshin			dp = NULL;
1422194602Srwatson			ifa_free(ifa);
142353541Sshin		}
142453541Sshin	}
142553541Sshin
142653541Sshindone:
142753541Sshin	splx(s);
1428183550Szec	CURVNET_RESTORE();
142953541Sshin}
143053541Sshin
143153541Sshinvoid
1432171259Sdelphijnd6_dad_duplicated(struct ifaddr *ifa)
143353541Sshin{
143453541Sshin	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1435151477Ssuz	struct ifnet *ifp;
143653541Sshin	struct dadq *dp;
1437165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
143853541Sshin
143953541Sshin	dp = nd6_dad_find(ifa);
144053541Sshin	if (dp == NULL) {
144162587Sitojun		log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
144253541Sshin		return;
144353541Sshin	}
144453541Sshin
144578064Sume	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
144678064Sume	    "NS in/out=%d/%d, NA in=%d\n",
1447165118Sbz	    if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
144878064Sume	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
144953541Sshin
145053541Sshin	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
145153541Sshin	ia->ia6_flags |= IN6_IFF_DUPLICATED;
145253541Sshin
1453148987Sume	/* We are done with DAD, with duplicate address found. (failure) */
145478064Sume	nd6_dad_stoptimer(dp);
145553541Sshin
1456151477Ssuz	ifp = ifa->ifa_ifp;
145762587Sitojun	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1458165118Sbz	    if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
145962587Sitojun	log(LOG_ERR, "%s: manual intervention required\n",
1460151477Ssuz	    if_name(ifp));
146153541Sshin
1462151477Ssuz	/*
1463151477Ssuz	 * If the address is a link-local address formed from an interface
1464151477Ssuz	 * identifier based on the hardware address which is supposed to be
1465151477Ssuz	 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1466151477Ssuz	 * operation on the interface SHOULD be disabled.
1467197138Shrs	 * [RFC 4862, Section 5.4.5]
1468151477Ssuz	 */
1469151477Ssuz	if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1470151477Ssuz		struct in6_addr in6;
1471151477Ssuz
1472151477Ssuz		/*
1473151477Ssuz		 * To avoid over-reaction, we only apply this logic when we are
1474151477Ssuz		 * very sure that hardware addresses are supposed to be unique.
1475151477Ssuz		 */
1476151477Ssuz		switch (ifp->if_type) {
1477151477Ssuz		case IFT_ETHER:
1478151477Ssuz		case IFT_FDDI:
1479151477Ssuz		case IFT_ATM:
1480151477Ssuz		case IFT_IEEE1394:
1481151477Ssuz#ifdef IFT_IEEE80211
1482151477Ssuz		case IFT_IEEE80211:
1483151477Ssuz#endif
1484219819Sjeff		case IFT_INFINIBAND:
1485151477Ssuz			in6 = ia->ia_addr.sin6_addr;
1486151477Ssuz			if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1487151477Ssuz			    IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1488151477Ssuz				ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1489151477Ssuz				log(LOG_ERR, "%s: possible hardware address "
1490151477Ssuz				    "duplication detected, disable IPv6\n",
1491151477Ssuz				    if_name(ifp));
1492151477Ssuz			}
1493151477Ssuz			break;
1494151477Ssuz		}
1495151477Ssuz	}
1496151477Ssuz
1497181803Sbz	TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
149853541Sshin	free(dp, M_IP6NDP);
149953541Sshin	dp = NULL;
1500194602Srwatson	ifa_free(ifa);
150153541Sshin}
150253541Sshin
150362587Sitojunstatic void
1504171259Sdelphijnd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
150562587Sitojun{
150662587Sitojun	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
150762587Sitojun	struct ifnet *ifp = ifa->ifa_ifp;
150862587Sitojun
150962587Sitojun	dp->dad_ns_tcount++;
151062587Sitojun	if ((ifp->if_flags & IFF_UP) == 0) {
151162587Sitojun		return;
151262587Sitojun	}
1513148887Srwatson	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
151462587Sitojun		return;
151562587Sitojun	}
151662587Sitojun
151762587Sitojun	dp->dad_ns_ocount++;
151862587Sitojun	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
151962587Sitojun}
152062587Sitojun
152162587Sitojunstatic void
1522171259Sdelphijnd6_dad_ns_input(struct ifaddr *ifa)
152353541Sshin{
152453541Sshin	struct in6_ifaddr *ia;
152553541Sshin	struct ifnet *ifp;
152678064Sume	const struct in6_addr *taddr6;
152753541Sshin	struct dadq *dp;
152853541Sshin	int duplicate;
152953541Sshin
1530151465Ssuz	if (ifa == NULL)
153153541Sshin		panic("ifa == NULL in nd6_dad_ns_input");
153253541Sshin
153353541Sshin	ia = (struct in6_ifaddr *)ifa;
153453541Sshin	ifp = ifa->ifa_ifp;
153553541Sshin	taddr6 = &ia->ia_addr.sin6_addr;
153653541Sshin	duplicate = 0;
153753541Sshin	dp = nd6_dad_find(ifa);
153853541Sshin
153953541Sshin	/* Quickhack - completely ignore DAD NS packets */
1540181803Sbz	if (V_dad_ignore_ns) {
1541165118Sbz		char ip6buf[INET6_ADDRSTRLEN];
154278064Sume		nd6log((LOG_INFO,
154378064Sume		    "nd6_dad_ns_input: ignoring DAD NS packet for "
1544165118Sbz		    "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6),
154578064Sume		    if_name(ifa->ifa_ifp)));
154653541Sshin		return;
154753541Sshin	}
154853541Sshin
154953541Sshin	/*
155053541Sshin	 * if I'm yet to start DAD, someone else started using this address
155153541Sshin	 * first.  I have a duplicate and you win.
155253541Sshin	 */
1553151465Ssuz	if (dp == NULL || dp->dad_ns_ocount == 0)
155453541Sshin		duplicate++;
155553541Sshin
155653541Sshin	/* XXX more checks for loopback situation - see nd6_dad_timer too */
155753541Sshin
155853541Sshin	if (duplicate) {
155953541Sshin		dp = NULL;	/* will be freed in nd6_dad_duplicated() */
156053541Sshin		nd6_dad_duplicated(ifa);
156153541Sshin	} else {
156253541Sshin		/*
156353541Sshin		 * not sure if I got a duplicate.
156453541Sshin		 * increment ns count and see what happens.
156553541Sshin		 */
156653541Sshin		if (dp)
156753541Sshin			dp->dad_ns_icount++;
156853541Sshin	}
156953541Sshin}
157053541Sshin
157162587Sitojunstatic void
1572171259Sdelphijnd6_dad_na_input(struct ifaddr *ifa)
157353541Sshin{
157453541Sshin	struct dadq *dp;
157553541Sshin
1576151465Ssuz	if (ifa == NULL)
157753541Sshin		panic("ifa == NULL in nd6_dad_na_input");
157853541Sshin
157953541Sshin	dp = nd6_dad_find(ifa);
158053541Sshin	if (dp)
158153541Sshin		dp->dad_na_icount++;
158253541Sshin
158353541Sshin	/* remove the address. */
158453541Sshin	nd6_dad_duplicated(ifa);
158553541Sshin}
1586