nd6_nbr.c revision 151536
1168404Spjd/*	$FreeBSD: head/sys/netinet6/nd6_nbr.c 151536 2005-10-21 15:45:13Z suz $	*/
2168404Spjd/*	$KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $	*/
3168404Spjd
4168404Spjd/*-
5168404Spjd * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6168404Spjd * All rights reserved.
7168404Spjd *
8168404Spjd * Redistribution and use in source and binary forms, with or without
9168404Spjd * modification, are permitted provided that the following conditions
10168404Spjd * are met:
11168404Spjd * 1. Redistributions of source code must retain the above copyright
12168404Spjd *    notice, this list of conditions and the following disclaimer.
13168404Spjd * 2. Redistributions in binary form must reproduce the above copyright
14168404Spjd *    notice, this list of conditions and the following disclaimer in the
15168404Spjd *    documentation and/or other materials provided with the distribution.
16168404Spjd * 3. Neither the name of the project nor the names of its contributors
17168404Spjd *    may be used to endorse or promote products derived from this software
18168404Spjd *    without specific prior written permission.
19168404Spjd *
20168404Spjd * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21168404Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22168404Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23219089Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24228103Smm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25230402Smm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26230404Smm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27236155Smm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28235222Smm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29235216Smm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30251646Sdelphij * SUCH DAMAGE.
31168404Spjd */
32168404Spjd
33168404Spjd#include "opt_inet.h"
34168404Spjd#include "opt_inet6.h"
35168404Spjd#include "opt_ipsec.h"
36168404Spjd#include "opt_carp.h"
37168404Spjd
38209962Smm#include <sys/param.h>
39168404Spjd#include <sys/systm.h>
40168404Spjd#include <sys/malloc.h>
41168404Spjd#include <sys/mbuf.h>
42168404Spjd#include <sys/socket.h>
43185029Spjd#include <sys/sockio.h>
44168404Spjd#include <sys/time.h>
45168404Spjd#include <sys/kernel.h>
46168404Spjd#include <sys/errno.h>
47168404Spjd#include <sys/syslog.h>
48168404Spjd#include <sys/queue.h>
49168404Spjd#include <sys/callout.h>
50168404Spjd
51168404Spjd#include <net/if.h>
52168404Spjd#include <net/if_types.h>
53168404Spjd#include <net/if_dl.h>
54168404Spjd#include <net/if_var.h>
55168404Spjd#include <net/route.h>
56185029Spjd
57168404Spjd#include <netinet/in.h>
58168404Spjd#include <netinet/in_var.h>
59168404Spjd#include <netinet6/in6_var.h>
60168404Spjd#include <netinet6/in6_ifattach.h>
61248571Smm#include <netinet/ip6.h>
62248571Smm#include <netinet6/ip6_var.h>
63168404Spjd#include <netinet6/scope6_var.h>
64168404Spjd#include <netinet6/nd6.h>
65168404Spjd#include <netinet/icmp6.h>
66168404Spjd
67168404Spjd#ifdef DEV_CARP
68168404Spjd#include <netinet/ip_carp.h>
69168404Spjd#endif
70168404Spjd
71168404Spjd#include <net/net_osdep.h>
72168404Spjd
73168404Spjd#define SDL(s) ((struct sockaddr_dl *)s)
74168404Spjd
75168404Spjdstruct dadq;
76168404Spjdstatic struct dadq *nd6_dad_find __P((struct ifaddr *));
77168404Spjdstatic void nd6_dad_starttimer __P((struct dadq *, int));
78168404Spjdstatic void nd6_dad_stoptimer __P((struct dadq *));
79168404Spjdstatic void nd6_dad_timer __P((struct ifaddr *));
80168404Spjdstatic void nd6_dad_ns_output __P((struct dadq *, struct ifaddr *));
81168404Spjdstatic void nd6_dad_ns_input __P((struct ifaddr *));
82168404Spjdstatic void nd6_dad_na_input __P((struct ifaddr *));
83168404Spjd
84168404Spjdstatic int dad_ignore_ns = 0;	/* ignore NS in DAD - specwise incorrect*/
85168404Spjdstatic int dad_maxtry = 15;	/* max # of *tries* to transmit DAD packet */
86168404Spjd
87168404Spjd/*
88168404Spjd * Input a Neighbor Solicitation Message.
89168404Spjd *
90168404Spjd * Based on RFC 2461
91168404Spjd * Based on RFC 2462 (duplicate address detection)
92168404Spjd */
93168404Spjdvoid
94168404Spjdnd6_ns_input(m, off, icmp6len)
95168404Spjd	struct mbuf *m;
96219089Spjd	int off, icmp6len;
97168404Spjd{
98168404Spjd	struct ifnet *ifp = m->m_pkthdr.rcvif;
99168404Spjd	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
100168404Spjd	struct nd_neighbor_solicit *nd_ns;
101168404Spjd	struct in6_addr saddr6 = ip6->ip6_src;
102168404Spjd	struct in6_addr daddr6 = ip6->ip6_dst;
103168404Spjd	struct in6_addr taddr6;
104168404Spjd	struct in6_addr myaddr6;
105168404Spjd	char *lladdr = NULL;
106168498Spjd	struct ifaddr *ifa = NULL;
107185029Spjd	int lladdrlen = 0;
108185029Spjd	int anycast = 0, proxy = 0, tentative = 0;
109185029Spjd	int tlladdr;
110185029Spjd	union nd_opts ndopts;
111185029Spjd	struct sockaddr_dl *proxydl = NULL;
112185029Spjd
113185029Spjd#ifndef PULLDOWN_TEST
114185029Spjd	IP6_EXTHDR_CHECK(m, off, icmp6len,);
115185029Spjd	nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
116185029Spjd#else
117185029Spjd	IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
118185029Spjd	if (nd_ns == NULL) {
119185029Spjd		icmp6stat.icp6s_tooshort++;
120185029Spjd		return;
121213197Smm	}
122219089Spjd#endif
123219089Spjd	ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
124219089Spjd	taddr6 = nd_ns->nd_ns_target;
125219089Spjd	if (in6_setscope(&taddr6, ifp, NULL) != 0)
126219089Spjd		goto bad;
127219089Spjd
128219089Spjd	if (ip6->ip6_hlim != 255) {
129219089Spjd		nd6log((LOG_ERR,
130219089Spjd		    "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
131219089Spjd		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
132219089Spjd		    ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
133168404Spjd		goto bad;
134248571Smm	}
135168404Spjd
136168404Spjd	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
137185029Spjd		/* dst has to be a solicited node multicast address. */
138185029Spjd		if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
139185029Spjd		    /* don't check ifindex portion */
140185029Spjd		    daddr6.s6_addr32[1] == 0 &&
141185029Spjd		    daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
142185029Spjd		    daddr6.s6_addr8[12] == 0xff) {
143185029Spjd			; /* good */
144185029Spjd		} else {
145185029Spjd			nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
146185029Spjd			    "(wrong ip6 dst)\n"));
147185029Spjd			goto bad;
148185029Spjd		}
149185029Spjd	}
150185029Spjd
151185029Spjd	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
152185029Spjd		nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
153185029Spjd		goto bad;
154185029Spjd	}
155185029Spjd
156185029Spjd	icmp6len -= sizeof(*nd_ns);
157185029Spjd	nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
158185029Spjd	if (nd6_options(&ndopts) < 0) {
159185029Spjd		nd6log((LOG_INFO,
160185029Spjd		    "nd6_ns_input: invalid ND option, ignored\n"));
161185029Spjd		/* nd6_options have incremented stats */
162185029Spjd		goto freeit;
163185029Spjd	}
164185029Spjd
165185029Spjd	if (ndopts.nd_opts_src_lladdr) {
166185029Spjd		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
167185029Spjd		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
168185029Spjd	}
169185029Spjd
170185029Spjd	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
171185029Spjd		nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
172185029Spjd		    "(link-layer address option)\n"));
173168404Spjd		goto bad;
174168404Spjd	}
175168404Spjd
176168404Spjd	/*
177168404Spjd	 * Attaching target link-layer address to the NA?
178168404Spjd	 * (RFC 2461 7.2.4)
179168404Spjd	 *
180168404Spjd	 * NS IP dst is unicast/anycast			MUST NOT add
181168404Spjd	 * NS IP dst is solicited-node multicast	MUST add
182168404Spjd	 *
183168404Spjd	 * In implementation, we add target link-layer address by default.
184168404Spjd	 * We do not add one in MUST NOT cases.
185168404Spjd	 */
186168404Spjd	if (!IN6_IS_ADDR_MULTICAST(&daddr6))
187168404Spjd		tlladdr = 0;
188168404Spjd	else
189168404Spjd		tlladdr = 1;
190248571Smm
191248571Smm	/*
192248571Smm	 * Target address (taddr6) must be either:
193168404Spjd	 * (1) Valid unicast/anycast address for my receiving interface,
194168404Spjd	 * (2) Unicast address for which I'm offering proxy service, or
195168404Spjd	 * (3) "tentative" address on which DAD is being performed.
196209962Smm	 */
197209962Smm	/* (1) and (3) check. */
198209962Smm#ifdef DEV_CARP
199209962Smm	if (ifp->if_carp)
200209962Smm		ifa = carp_iamatch6(ifp->if_carp, &taddr6);
201209962Smm	if (ifa == NULL)
202209962Smm		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
203209962Smm#else
204168404Spjd	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
205168404Spjd#endif
206168404Spjd
207168404Spjd	/* (2) check. */
208168404Spjd	if (ifa == NULL) {
209168404Spjd		struct rtentry *rt;
210168404Spjd		struct sockaddr_in6 tsin6;
211168404Spjd		int need_proxy;
212168404Spjd
213224169Sgibbs		bzero(&tsin6, sizeof tsin6);
214224169Sgibbs		tsin6.sin6_len = sizeof(struct sockaddr_in6);
215185029Spjd		tsin6.sin6_family = AF_INET6;
216168404Spjd		tsin6.sin6_addr = taddr6;
217168404Spjd
218168404Spjd		rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
219168404Spjd		need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
220168404Spjd		    rt->rt_gateway->sa_family == AF_LINK);
221168404Spjd		if (rt)
222168404Spjd			rtfree(rt);
223168404Spjd		if (need_proxy) {
224168404Spjd			/*
225168404Spjd			 * proxy NDP for single entry
226168404Spjd			 */
227185029Spjd			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
228248571Smm				IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
229168404Spjd			if (ifa) {
230168404Spjd				proxy = 1;
231219089Spjd				proxydl = SDL(rt->rt_gateway);
232219089Spjd			}
233219089Spjd		}
234219089Spjd	}
235219089Spjd	if (ifa == NULL) {
236219089Spjd		/*
237219089Spjd		 * We've got an NS packet, and we don't have that adddress
238219089Spjd		 * assigned for us.  We MUST silently ignore it.
239168404Spjd		 * See RFC2461 7.2.3.
240168404Spjd		 */
241168404Spjd		goto freeit;
242219089Spjd	}
243219089Spjd	myaddr6 = *IFA_IN6(ifa);
244228103Smm	anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
245236155Smm	tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
246168404Spjd	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
247185029Spjd		goto freeit;
248185029Spjd
249185029Spjd	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
250185029Spjd		nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
251185029Spjd		    "(if %d, NS packet %d)\n",
252168404Spjd		    ip6_sprintf(&taddr6),
253168404Spjd		    ifp->if_addrlen, lladdrlen - 2));
254219089Spjd		goto bad;
255219089Spjd	}
256168404Spjd
257219089Spjd	if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
258219089Spjd		nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
259185029Spjd		    ip6_sprintf(&saddr6)));
260185029Spjd		goto freeit;
261185029Spjd	}
262185029Spjd
263219089Spjd	/*
264219089Spjd	 * We have neighbor solicitation packet, with target address equals to
265224169Sgibbs	 * one of my tentative address.
266185029Spjd	 *
267168404Spjd	 * src addr	how to process?
268168404Spjd	 * ---		---
269168404Spjd	 * multicast	of course, invalid (rejected in ip6_input)
270168404Spjd	 * unicast	somebody is doing address resolution -> ignore
271185029Spjd	 * unspec	dup address detection
272185029Spjd	 *
273185029Spjd	 * The processing is defined in RFC 2462.
274185029Spjd	 */
275185029Spjd	if (tentative) {
276168404Spjd		/*
277168404Spjd		 * If source address is unspecified address, it is for
278168404Spjd		 * duplicate address detection.
279168404Spjd		 *
280168404Spjd		 * If not, the packet is for addess resolution;
281168404Spjd		 * silently ignore it.
282168404Spjd		 */
283168404Spjd		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
284168404Spjd			nd6_dad_ns_input(ifa);
285168404Spjd
286168404Spjd		goto freeit;
287168404Spjd	}
288168404Spjd
289168404Spjd	/*
290168404Spjd	 * If the source address is unspecified address, entries must not
291168404Spjd	 * be created or updated.
292168404Spjd	 * It looks that sender is performing DAD.  Output NA toward
293168404Spjd	 * all-node multicast address, to tell the sender that I'm using
294168404Spjd	 * the address.
295168404Spjd	 * S bit ("solicited") must be zero.
296168404Spjd	 */
297168498Spjd	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
298185029Spjd		struct in6_addr in6_all;
299185029Spjd
300209962Smm		in6_all = in6addr_linklocal_allnodes;
301209962Smm		if (in6_setscope(&in6_all, ifp, NULL) != 0)
302209962Smm			goto bad;
303236884Smm		nd6_na_output(ifp, &in6_all, &taddr6,
304236884Smm		    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
305236884Smm		    (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
306236884Smm		    tlladdr, (struct sockaddr *)proxydl);
307236884Smm		goto freeit;
308236884Smm	}
309236884Smm
310236884Smm	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
311236884Smm	    ND_NEIGHBOR_SOLICIT, 0);
312209962Smm
313209962Smm	nd6_na_output(ifp, &saddr6, &taddr6,
314209962Smm	    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
315209962Smm	    (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
316185029Spjd	    tlladdr, (struct sockaddr *)proxydl);
317185029Spjd freeit:
318168404Spjd	m_freem(m);
319168404Spjd	return;
320168404Spjd
321168404Spjd bad:
322168404Spjd	nd6log((LOG_ERR, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6)));
323168404Spjd	nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6)));
324238926Smm	nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6)));
325238926Smm	icmp6stat.icp6s_badns++;
326168404Spjd	m_freem(m);
327168404Spjd}
328219089Spjd
329254591Sgibbs/*
330168404Spjd * Output a Neighbor Solicitation Message. Caller specifies:
331168404Spjd *	- ICMP6 header source IP6 address
332168404Spjd *	- ND6 header target IP6 address
333168404Spjd *	- ND6 header source datalink address
334168404Spjd *
335168404Spjd * Based on RFC 2461
336168404Spjd * Based on RFC 2462 (duplicate address detection)
337168404Spjd */
338168404Spjdvoid
339219089Spjdnd6_ns_output(ifp, daddr6, taddr6, ln, dad)
340168404Spjd	struct ifnet *ifp;
341168404Spjd	const struct in6_addr *daddr6, *taddr6;
342168404Spjd	struct llinfo_nd6 *ln;	/* for source address determination */
343168404Spjd	int dad;	/* duplicate address detection */
344168404Spjd{
345236884Smm	struct mbuf *m;
346168404Spjd	struct ip6_hdr *ip6;
347168404Spjd	struct nd_neighbor_solicit *nd_ns;
348168404Spjd	struct in6_addr *src, src_in;
349168404Spjd	struct ip6_moptions im6o;
350168404Spjd	int icmp6len;
351168404Spjd	int maxlen;
352248571Smm	caddr_t mac;
353248571Smm	struct route_in6 ro;
354168404Spjd
355185029Spjd	bzero(&ro, sizeof(ro));
356185029Spjd
357219089Spjd	if (IN6_IS_ADDR_MULTICAST(taddr6))
358236884Smm		return;
359168404Spjd
360168404Spjd	/* estimate the size of message */
361168404Spjd	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
362168404Spjd	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
363219089Spjd	if (max_linkhdr + maxlen >= MCLBYTES) {
364219089Spjd#ifdef DIAGNOSTIC
365219089Spjd		printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
366219089Spjd		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
367219089Spjd#endif
368219089Spjd		return;
369219089Spjd	}
370219089Spjd
371219089Spjd	MGETHDR(m, M_DONTWAIT, MT_DATA);
372219089Spjd	if (m && max_linkhdr + maxlen >= MHLEN) {
373219089Spjd		MCLGET(m, M_DONTWAIT);
374219089Spjd		if ((m->m_flags & M_EXT) == 0) {
375219089Spjd			m_free(m);
376219089Spjd			m = NULL;
377219089Spjd		}
378168404Spjd	}
379185029Spjd	if (m == NULL)
380185029Spjd		return;
381168404Spjd	m->m_pkthdr.rcvif = NULL;
382168404Spjd
383168404Spjd	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
384168404Spjd		m->m_flags |= M_MCAST;
385185029Spjd		im6o.im6o_multicast_ifp = ifp;
386185029Spjd		im6o.im6o_multicast_hlim = 255;
387248571Smm		im6o.im6o_multicast_loop = 0;
388219089Spjd	}
389219089Spjd
390219089Spjd	icmp6len = sizeof(*nd_ns);
391185029Spjd	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
392168404Spjd	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
393219089Spjd
394219089Spjd	/* fill neighbor solicitation packet */
395168404Spjd	ip6 = mtod(m, struct ip6_hdr *);
396168404Spjd	ip6->ip6_flow = 0;
397248571Smm	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
398219089Spjd	ip6->ip6_vfc |= IPV6_VERSION;
399219089Spjd	/* ip6->ip6_plen will be set later */
400219089Spjd	ip6->ip6_nxt = IPPROTO_ICMPV6;
401219089Spjd	ip6->ip6_hlim = 255;
402168404Spjd	if (daddr6)
403168404Spjd		ip6->ip6_dst = *daddr6;
404168404Spjd	else {
405168404Spjd		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
406168404Spjd		ip6->ip6_dst.s6_addr16[1] = 0;
407228103Smm		ip6->ip6_dst.s6_addr32[1] = 0;
408168404Spjd		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
409168404Spjd		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
410168404Spjd		ip6->ip6_dst.s6_addr8[12] = 0xff;
411185029Spjd		if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
412168404Spjd			goto bad;
413168404Spjd	}
414168404Spjd	if (!dad) {
415168404Spjd		/*
416168404Spjd		 * RFC2461 7.2.2:
417185029Spjd		 * "If the source address of the packet prompting the
418185029Spjd		 * solicitation is the same as one of the addresses assigned
419185029Spjd		 * to the outgoing interface, that address SHOULD be placed
420185029Spjd		 * in the IP Source Address of the outgoing solicitation.
421185029Spjd		 * Otherwise, any one of the addresses assigned to the
422185029Spjd		 * interface should be used."
423185029Spjd		 *
424185029Spjd		 * We use the source address for the prompting packet
425185029Spjd		 * (saddr6), if:
426185029Spjd		 * - saddr6 is given from the caller (by giving "ln"), and
427185029Spjd		 * - saddr6 belongs to the outgoing interface.
428185029Spjd		 * Otherwise, we perform the source address selection as usual.
429168404Spjd		 */
430168404Spjd		struct ip6_hdr *hip6;		/* hold ip6 */
431168404Spjd		struct in6_addr *hsrc = NULL;
432185029Spjd
433219089Spjd		if (ln && ln->ln_hold) {
434219089Spjd			/*
435168404Spjd			 * assuming every packet in ln_hold has the same IP
436185029Spjd			 * header
437209962Smm			 */
438209962Smm			hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
439209962Smm			/* XXX pullup? */
440209962Smm			if (sizeof(*hip6) < ln->ln_hold->m_len)
441228103Smm				hsrc = &hip6->ip6_src;
442228103Smm			else
443228103Smm				hsrc = NULL;
444228103Smm		}
445236884Smm		if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc))
446236884Smm			src = hsrc;
447168404Spjd		else {
448219089Spjd			int error;
449168404Spjd			struct sockaddr_in6 dst_sa;
450168404Spjd
451185029Spjd			bzero(&dst_sa, sizeof(dst_sa));
452219089Spjd			dst_sa.sin6_family = AF_INET6;
453228103Smm			dst_sa.sin6_len = sizeof(dst_sa);
454168404Spjd			dst_sa.sin6_addr = ip6->ip6_dst;
455228103Smm
456185029Spjd			src = in6_selectsrc(&dst_sa, NULL,
457185029Spjd			    NULL, &ro, NULL, NULL, &error);
458168404Spjd			if (src == NULL) {
459185029Spjd				nd6log((LOG_DEBUG,
460168404Spjd				    "nd6_ns_output: source can't be "
461168404Spjd				    "determined: dst=%s, error=%d\n",
462219089Spjd				    ip6_sprintf(&dst_sa.sin6_addr), error));
463168404Spjd				goto bad;
464185029Spjd			}
465168404Spjd		}
466219089Spjd	} else {
467205198Sdelphij		/*
468168404Spjd		 * Source address for DAD packet must always be IPv6
469168404Spjd		 * unspecified address. (0::0)
470168404Spjd		 * We actually don't have to 0-clear the address (we did it
471168404Spjd		 * above), but we do so here explicitly to make the intention
472236884Smm		 * clearer.
473236884Smm		 */
474236884Smm		bzero(&src_in, sizeof(src_in));
475236884Smm		src = &src_in;
476236884Smm	}
477236884Smm	ip6->ip6_src = *src;
478236884Smm	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
479168404Spjd	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
480185029Spjd	nd_ns->nd_ns_code = 0;
481168404Spjd	nd_ns->nd_ns_reserved = 0;
482185029Spjd	nd_ns->nd_ns_target = *taddr6;
483236884Smm	in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
484236884Smm
485185029Spjd	/*
486185029Spjd	 * Add source link-layer address option.
487185029Spjd	 *
488185029Spjd	 *				spec		implementation
489185029Spjd	 *				---		---
490185029Spjd	 * DAD packet			MUST NOT	do not add the option
491185029Spjd	 * there's no link layer address:
492185029Spjd	 *				impossible	do not add the option
493185029Spjd	 * there's link layer address:
494185029Spjd	 *	Multicast NS		MUST add one	add the option
495185029Spjd	 *	Unicast NS		SHOULD add one	add the option
496185029Spjd	 */
497185029Spjd	if (!dad && (mac = nd6_ifptomac(ifp))) {
498185029Spjd		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
499219089Spjd		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
500219089Spjd		/* 8 byte alignments... */
501219089Spjd		optlen = (optlen + 7) & ~7;
502219089Spjd
503219089Spjd		m->m_pkthdr.len += optlen;
504219089Spjd		m->m_len += optlen;
505219089Spjd		icmp6len += optlen;
506219089Spjd		bzero((caddr_t)nd_opt, optlen);
507219089Spjd		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
508219089Spjd		nd_opt->nd_opt_len = optlen >> 3;
509219089Spjd		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
510185029Spjd	}
511185029Spjd
512185029Spjd	ip6->ip6_plen = htons((u_short)icmp6len);
513185029Spjd	nd_ns->nd_ns_cksum = 0;
514168404Spjd	nd_ns->nd_ns_cksum =
515219089Spjd	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
516219089Spjd
517168404Spjd	ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
518168404Spjd	icmp6_ifstat_inc(ifp, ifs6_out_msg);
519168404Spjd	icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
520185029Spjd	icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
521185029Spjd
522185029Spjd	if (ro.ro_rt) {		/* we don't cache this route. */
523168404Spjd		RTFREE(ro.ro_rt);
524185029Spjd	}
525219089Spjd	return;
526219089Spjd
527168404Spjd  bad:
528168404Spjd	if (ro.ro_rt) {
529168404Spjd		RTFREE(ro.ro_rt);
530168404Spjd	}
531168404Spjd	m_freem(m);
532168404Spjd	return;
533168404Spjd}
534168404Spjd
535168404Spjd/*
536230438Spjd * Neighbor advertisement input handling.
537219089Spjd *
538228103Smm * Based on RFC 2461
539168404Spjd * Based on RFC 2462 (duplicate address detection)
540219089Spjd *
541219089Spjd * the following items are not implemented yet:
542219089Spjd * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
543219089Spjd * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
544219089Spjd */
545219089Spjdvoid
546219089Spjdnd6_na_input(m, off, icmp6len)
547219089Spjd	struct mbuf *m;
548219089Spjd	int off, icmp6len;
549219089Spjd{
550219089Spjd	struct ifnet *ifp = m->m_pkthdr.rcvif;
551168404Spjd	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
552168404Spjd	struct nd_neighbor_advert *nd_na;
553168404Spjd	struct in6_addr daddr6 = ip6->ip6_dst;
554168404Spjd	struct in6_addr taddr6;
555168404Spjd	int flags;
556185029Spjd	int is_router;
557219089Spjd	int is_solicited;
558219089Spjd	int is_override;
559248571Smm	char *lladdr = NULL;
560168404Spjd	int lladdrlen = 0;
561185029Spjd	struct ifaddr *ifa;
562248571Smm	struct llinfo_nd6 *ln;
563248571Smm	struct rtentry *rt;
564185029Spjd	struct sockaddr_dl *sdl;
565219089Spjd	union nd_opts ndopts;
566226705Spjd
567226705Spjd	if (ip6->ip6_hlim != 255) {
568226705Spjd		nd6log((LOG_ERR,
569226705Spjd		    "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
570226705Spjd		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
571226705Spjd		    ip6_sprintf(&ip6->ip6_dst), if_name(ifp)));
572235216Smm		goto bad;
573235216Smm	}
574235216Smm
575226705Spjd#ifndef PULLDOWN_TEST
576226705Spjd	IP6_EXTHDR_CHECK(m, off, icmp6len,);
577240870Spjd	nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
578240870Spjd#else
579226705Spjd	IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
580219089Spjd	if (nd_na == NULL) {
581219089Spjd		icmp6stat.icp6s_tooshort++;
582228103Smm		return;
583219089Spjd	}
584219089Spjd#endif
585228103Smm
586219089Spjd	flags = nd_na->nd_na_flags_reserved;
587219089Spjd	is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
588228103Smm	is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
589219089Spjd	is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
590219089Spjd
591228103Smm	taddr6 = nd_na->nd_na_target;
592219089Spjd	if (in6_setscope(&taddr6, ifp, NULL))
593219089Spjd		goto bad;	/* XXX: impossible */
594228103Smm
595219089Spjd	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
596219089Spjd		nd6log((LOG_ERR,
597228103Smm		    "nd6_na_input: invalid target address %s\n",
598228103Smm		    ip6_sprintf(&taddr6)));
599228103Smm		goto bad;
600228103Smm	}
601228103Smm	if (IN6_IS_ADDR_MULTICAST(&daddr6))
602228103Smm		if (is_solicited) {
603228103Smm			nd6log((LOG_ERR,
604235222Smm			    "nd6_na_input: a solicited adv is multicasted\n"));
605235222Smm			goto bad;
606235222Smm		}
607219089Spjd
608219089Spjd	icmp6len -= sizeof(*nd_na);
609219089Spjd	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
610219089Spjd	if (nd6_options(&ndopts) < 0) {
611228103Smm		nd6log((LOG_INFO,
612228103Smm		    "nd6_na_input: invalid ND option, ignored\n"));
613219089Spjd		/* nd6_options have incremented stats */
614168404Spjd		goto freeit;
615248571Smm	}
616251646Sdelphij
617251646Sdelphij	if (ndopts.nd_opts_tgt_lladdr) {
618219089Spjd		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
619219089Spjd		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
620219089Spjd	}
621168404Spjd
622209962Smm	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
623209962Smm
624209962Smm	/*
625219089Spjd	 * Target address matches one of my interface address.
626219089Spjd	 *
627209962Smm	 * If my address is tentative, this means that there's somebody
628219089Spjd	 * already using the same address as mine.  This indicates DAD failure.
629219089Spjd	 * This is defined in RFC 2462.
630219089Spjd	 *
631185029Spjd	 * Otherwise, process as defined in RFC 2461.
632185029Spjd	 */
633228103Smm	if (ifa
634185029Spjd	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
635185029Spjd		nd6_dad_na_input(ifa);
636228103Smm		goto freeit;
637185029Spjd	}
638219089Spjd
639219089Spjd	/* Just for safety, maybe unnecessary. */
640219089Spjd	if (ifa) {
641219089Spjd		log(LOG_ERR,
642228103Smm		    "nd6_na_input: duplicate IP6 address %s\n",
643219089Spjd		    ip6_sprintf(&taddr6));
644185029Spjd		goto freeit;
645228103Smm	}
646185029Spjd
647185029Spjd	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
648228103Smm		nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
649185029Spjd		    "(if %d, NA packet %d)\n", ip6_sprintf(&taddr6),
650185029Spjd		    ifp->if_addrlen, lladdrlen - 2));
651228103Smm		goto bad;
652185029Spjd	}
653185029Spjd
654228103Smm	/*
655200516Sdelphij	 * If no neighbor cache entry is found, NA SHOULD silently be
656200516Sdelphij	 * discarded.
657228103Smm	 */
658185029Spjd	rt = nd6_lookup(&taddr6, 0, ifp);
659185029Spjd	if ((rt == NULL) ||
660228103Smm	   ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
661185029Spjd	   ((sdl = SDL(rt->rt_gateway)) == NULL))
662185029Spjd		goto freeit;
663219089Spjd
664219089Spjd	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
665219089Spjd		/*
666219089Spjd		 * If the link-layer has address, and no lladdr option came,
667219089Spjd		 * discard the packet.
668219089Spjd		 */
669219089Spjd		if (ifp->if_addrlen && lladdr == NULL)
670219089Spjd			goto freeit;
671219089Spjd
672168404Spjd		/*
673168404Spjd		 * Record link-layer address, and update the state.
674168404Spjd		 */
675168404Spjd		sdl->sdl_alen = ifp->if_addrlen;
676168404Spjd		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
677168404Spjd		if (is_solicited) {
678168404Spjd			ln->ln_state = ND6_LLINFO_REACHABLE;
679185029Spjd			ln->ln_byhint = 0;
680185029Spjd			if (ln->ln_expire) {
681185029Spjd				ln->ln_expire = time_second +
682168404Spjd				    ND_IFINFO(rt->rt_ifp)->reachable;
683168404Spjd			}
684168404Spjd		} else {
685168404Spjd			ln->ln_state = ND6_LLINFO_STALE;
686168404Spjd			ln->ln_expire = time_second + nd6_gctimer;
687168404Spjd		}
688168404Spjd		if ((ln->ln_router = is_router) != 0) {
689168404Spjd			/*
690168404Spjd			 * This means a router's state has changed from
691168404Spjd			 * non-reachable to probably reachable, and might
692168404Spjd			 * affect the status of associated prefixes..
693168404Spjd			 */
694168404Spjd			pfxlist_onlink_check();
695168404Spjd		}
696168404Spjd	} else {
697168404Spjd		int llchange;
698168404Spjd
699168404Spjd		/*
700185029Spjd		 * Check if the link-layer address has changed or not.
701168404Spjd		 */
702168404Spjd		if (lladdr == NULL)
703185029Spjd			llchange = 0;
704168404Spjd		else {
705185029Spjd			if (sdl->sdl_alen) {
706185029Spjd				if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
707168404Spjd					llchange = 1;
708185029Spjd				else
709168404Spjd					llchange = 0;
710185029Spjd			} else
711185029Spjd				llchange = 1;
712185029Spjd		}
713209962Smm
714185029Spjd		/*
715168404Spjd		 * This is VERY complex.  Look at it with care.
716168404Spjd		 *
717168404Spjd		 * override solicit lladdr llchange	action
718168404Spjd		 *					(L: record lladdr)
719168404Spjd		 *
720168404Spjd		 *	0	0	n	--	(2c)
721168404Spjd		 *	0	0	y	n	(2b) L
722168404Spjd		 *	0	0	y	y	(1)    REACHABLE->STALE
723168404Spjd		 *	0	1	n	--	(2c)   *->REACHABLE
724168404Spjd		 *	0	1	y	n	(2b) L *->REACHABLE
725168404Spjd		 *	0	1	y	y	(1)    REACHABLE->STALE
726168404Spjd		 *	1	0	n	--	(2a)
727168404Spjd		 *	1	0	y	n	(2a) L
728168404Spjd		 *	1	0	y	y	(2a) L *->STALE
729168404Spjd		 *	1	1	n	--	(2a)   *->REACHABLE
730168404Spjd		 *	1	1	y	n	(2a) L *->REACHABLE
731168404Spjd		 *	1	1	y	y	(2a) L *->REACHABLE
732168404Spjd		 */
733168404Spjd		if (!is_override && (lladdr != NULL && llchange)) {   /* (1) */
734168404Spjd			/*
735168404Spjd			 * If state is REACHABLE, make it STALE.
736168404Spjd			 * no other updates should be done.
737168404Spjd			 */
738168404Spjd			if (ln->ln_state == ND6_LLINFO_REACHABLE) {
739168404Spjd				ln->ln_state = ND6_LLINFO_STALE;
740168404Spjd				ln->ln_expire = time_second + nd6_gctimer;
741168404Spjd			}
742168404Spjd			goto freeit;
743168404Spjd		} else if (is_override				   /* (2a) */
744168404Spjd			|| (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
745219089Spjd			|| lladdr == NULL) {			   /* (2c) */
746168404Spjd			/*
747168404Spjd			 * Update link-local address, if any.
748219089Spjd			 */
749168404Spjd			if (lladdr != NULL) {
750185029Spjd				sdl->sdl_alen = ifp->if_addrlen;
751185029Spjd				bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
752185029Spjd			}
753168404Spjd
754209962Smm			/*
755209962Smm			 * If solicited, make the state REACHABLE.
756209962Smm			 * If not solicited and the link-layer address was
757209962Smm			 * changed, make it STALE.
758209962Smm			 */
759209962Smm			if (is_solicited) {
760209962Smm				ln->ln_state = ND6_LLINFO_REACHABLE;
761209962Smm				ln->ln_byhint = 0;
762209962Smm				if (ln->ln_expire) {
763168404Spjd					ln->ln_expire = time_second +
764168404Spjd					    ND_IFINFO(ifp)->reachable;
765168404Spjd				}
766168404Spjd			} else {
767168404Spjd				if (lladdr != NULL && llchange) {
768168404Spjd					ln->ln_state = ND6_LLINFO_STALE;
769219089Spjd					ln->ln_expire = time_second + nd6_gctimer;
770219089Spjd				}
771219089Spjd			}
772219089Spjd		}
773219089Spjd
774219089Spjd		if (ln->ln_router && !is_router) {
775219089Spjd			/*
776219089Spjd			 * The peer dropped the router flag.
777219089Spjd			 * Remove the sender from the Default Router List and
778219089Spjd			 * update the Destination Cache entries.
779219089Spjd			 */
780219089Spjd			struct nd_defrouter *dr;
781168404Spjd			struct in6_addr *in6;
782168404Spjd			int s;
783219089Spjd
784168404Spjd			in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
785168404Spjd
786168404Spjd			/*
787168404Spjd			 * Lock to protect the default router list.
788168404Spjd			 * XXX: this might be unnecessary, since this function
789168404Spjd			 * is only called under the network software interrupt
790			 * context.  However, we keep it just for safety.
791			 */
792			s = splnet();
793			dr = defrouter_lookup(in6, ifp);
794			if (dr)
795				defrtrlist_del(dr);
796			else if (!ip6_forwarding && ip6_accept_rtadv) {
797				/*
798				 * Even if the neighbor is not in the default
799				 * router list, the neighbor may be used
800				 * as a next hop for some destinations
801				 * (e.g. redirect case). So we must
802				 * call rt6_flush explicitly.
803				 */
804				rt6_flush(&ip6->ip6_src, ifp);
805			}
806			splx(s);
807		}
808		ln->ln_router = is_router;
809	}
810	rt->rt_flags &= ~RTF_REJECT;
811	ln->ln_asked = 0;
812	if (ln->ln_hold) {
813		/*
814		 * we assume ifp is not a loopback here, so just set the 2nd
815		 * argument as the 1st one.
816		 */
817		nd6_output(ifp, ifp, ln->ln_hold,
818			   (struct sockaddr_in6 *)rt_key(rt), rt);
819		ln->ln_hold = NULL;
820	}
821
822 freeit:
823	m_freem(m);
824	return;
825
826 bad:
827	icmp6stat.icp6s_badna++;
828	m_freem(m);
829}
830
831/*
832 * Neighbor advertisement output handling.
833 *
834 * Based on RFC 2461
835 *
836 * the following items are not implemented yet:
837 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
838 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
839 */
840void
841nd6_na_output(ifp, daddr6_0, taddr6, flags, tlladdr, sdl0)
842	struct ifnet *ifp;
843	const struct in6_addr *daddr6_0, *taddr6;
844	u_long flags;
845	int tlladdr;		/* 1 if include target link-layer address */
846	struct sockaddr *sdl0;	/* sockaddr_dl (= proxy NA) or NULL */
847{
848	struct mbuf *m;
849	struct ip6_hdr *ip6;
850	struct nd_neighbor_advert *nd_na;
851	struct ip6_moptions im6o;
852	struct in6_addr *src, daddr6;
853	struct sockaddr_in6 dst_sa;
854	int icmp6len, maxlen, error;
855	caddr_t mac = NULL;
856	struct route_in6 ro;
857
858	bzero(&ro, sizeof(ro));
859
860	daddr6 = *daddr6_0;	/* make a local copy for modification */
861
862	/* estimate the size of message */
863	maxlen = sizeof(*ip6) + sizeof(*nd_na);
864	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
865	if (max_linkhdr + maxlen >= MCLBYTES) {
866#ifdef DIAGNOSTIC
867		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
868		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
869#endif
870		return;
871	}
872
873	MGETHDR(m, M_DONTWAIT, MT_DATA);
874	if (m && max_linkhdr + maxlen >= MHLEN) {
875		MCLGET(m, M_DONTWAIT);
876		if ((m->m_flags & M_EXT) == 0) {
877			m_free(m);
878			m = NULL;
879		}
880	}
881	if (m == NULL)
882		return;
883	m->m_pkthdr.rcvif = NULL;
884
885	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
886		m->m_flags |= M_MCAST;
887		im6o.im6o_multicast_ifp = ifp;
888		im6o.im6o_multicast_hlim = 255;
889		im6o.im6o_multicast_loop = 0;
890	}
891
892	icmp6len = sizeof(*nd_na);
893	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
894	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
895
896	/* fill neighbor advertisement packet */
897	ip6 = mtod(m, struct ip6_hdr *);
898	ip6->ip6_flow = 0;
899	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
900	ip6->ip6_vfc |= IPV6_VERSION;
901	ip6->ip6_nxt = IPPROTO_ICMPV6;
902	ip6->ip6_hlim = 255;
903	if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
904		/* reply to DAD */
905		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
906		ip6->ip6_dst.s6_addr16[1] = 0;
907		ip6->ip6_dst.s6_addr32[1] = 0;
908		ip6->ip6_dst.s6_addr32[2] = 0;
909		ip6->ip6_dst.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
910		if (in6_setscope(&daddr6, ifp, NULL))
911			goto bad;
912
913		flags &= ~ND_NA_FLAG_SOLICITED;
914	}
915	ip6->ip6_dst = daddr6;
916	bzero(&dst_sa, sizeof(struct sockaddr_in6));
917	dst_sa.sin6_family = AF_INET6;
918	dst_sa.sin6_len = sizeof(struct sockaddr_in6);
919	dst_sa.sin6_addr = daddr6;
920
921	/*
922	 * Select a source whose scope is the same as that of the dest.
923	 */
924	bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
925	src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &error);
926	if (src == NULL) {
927		nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
928		    "determined: dst=%s, error=%d\n",
929		    ip6_sprintf(&dst_sa.sin6_addr), error));
930		goto bad;
931	}
932	ip6->ip6_src = *src;
933	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
934	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
935	nd_na->nd_na_code = 0;
936	nd_na->nd_na_target = *taddr6;
937	in6_clearscope(&nd_na->nd_na_target); /* XXX */
938
939	/*
940	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
941	 * see nd6_ns_input() for details.
942	 * Basically, if NS packet is sent to unicast/anycast addr,
943	 * target lladdr option SHOULD NOT be included.
944	 */
945	if (tlladdr) {
946		/*
947		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
948		 * lladdr in sdl0.  If we are not proxying (sending NA for
949		 * my address) use lladdr configured for the interface.
950		 */
951		if (sdl0 == NULL) {
952#ifdef DEV_CARP
953			if (ifp->if_carp)
954				mac = carp_macmatch6(ifp->if_carp, m, taddr6);
955			if (mac == NULL)
956				mac = nd6_ifptomac(ifp);
957#else
958			mac = nd6_ifptomac(ifp);
959#endif
960		} else if (sdl0->sa_family == AF_LINK) {
961			struct sockaddr_dl *sdl;
962			sdl = (struct sockaddr_dl *)sdl0;
963			if (sdl->sdl_alen == ifp->if_addrlen)
964				mac = LLADDR(sdl);
965		}
966	}
967	if (tlladdr && mac) {
968		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
969		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
970
971		/* roundup to 8 bytes alignment! */
972		optlen = (optlen + 7) & ~7;
973
974		m->m_pkthdr.len += optlen;
975		m->m_len += optlen;
976		icmp6len += optlen;
977		bzero((caddr_t)nd_opt, optlen);
978		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
979		nd_opt->nd_opt_len = optlen >> 3;
980		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
981	} else
982		flags &= ~ND_NA_FLAG_OVERRIDE;
983
984	ip6->ip6_plen = htons((u_short)icmp6len);
985	nd_na->nd_na_flags_reserved = flags;
986	nd_na->nd_na_cksum = 0;
987	nd_na->nd_na_cksum =
988	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
989
990	ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
991	icmp6_ifstat_inc(ifp, ifs6_out_msg);
992	icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
993	icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
994
995	if (ro.ro_rt) {		/* we don't cache this route. */
996		RTFREE(ro.ro_rt);
997	}
998	return;
999
1000  bad:
1001	if (ro.ro_rt) {
1002		RTFREE(ro.ro_rt);
1003	}
1004	m_freem(m);
1005	return;
1006}
1007
1008caddr_t
1009nd6_ifptomac(ifp)
1010	struct ifnet *ifp;
1011{
1012	switch (ifp->if_type) {
1013	case IFT_ARCNET:
1014	case IFT_ETHER:
1015	case IFT_FDDI:
1016	case IFT_IEEE1394:
1017#ifdef IFT_L2VLAN
1018	case IFT_L2VLAN:
1019#endif
1020#ifdef IFT_IEEE80211
1021	case IFT_IEEE80211:
1022#endif
1023#ifdef IFT_CARP
1024	case IFT_CARP:
1025#endif
1026	case IFT_BRIDGE:
1027	case IFT_ISO88025:
1028		return IF_LLADDR(ifp);
1029	default:
1030		return NULL;
1031	}
1032}
1033
1034TAILQ_HEAD(dadq_head, dadq);
1035struct dadq {
1036	TAILQ_ENTRY(dadq) dad_list;
1037	struct ifaddr *dad_ifa;
1038	int dad_count;		/* max NS to send */
1039	int dad_ns_tcount;	/* # of trials to send NS */
1040	int dad_ns_ocount;	/* NS sent so far */
1041	int dad_ns_icount;
1042	int dad_na_icount;
1043	struct callout dad_timer_ch;
1044};
1045
1046static struct dadq_head dadq;
1047static int dad_init = 0;
1048
1049static struct dadq *
1050nd6_dad_find(ifa)
1051	struct ifaddr *ifa;
1052{
1053	struct dadq *dp;
1054
1055	for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
1056		if (dp->dad_ifa == ifa)
1057			return dp;
1058	}
1059	return NULL;
1060}
1061
1062static void
1063nd6_dad_starttimer(dp, ticks)
1064	struct dadq *dp;
1065	int ticks;
1066{
1067
1068	callout_reset(&dp->dad_timer_ch, ticks,
1069	    (void (*) __P((void *)))nd6_dad_timer, (void *)dp->dad_ifa);
1070}
1071
1072static void
1073nd6_dad_stoptimer(dp)
1074	struct dadq *dp;
1075{
1076
1077	callout_stop(&dp->dad_timer_ch);
1078}
1079
1080/*
1081 * Start Duplicate Address Detection (DAD) for specified interface address.
1082 */
1083void
1084nd6_dad_start(ifa, tick)
1085	struct ifaddr *ifa;
1086	int *tick;	/* minimum delay ticks for IFF_UP event */
1087{
1088	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1089	struct dadq *dp;
1090
1091	if (!dad_init) {
1092		TAILQ_INIT(&dadq);
1093		dad_init++;
1094	}
1095
1096	/*
1097	 * If we don't need DAD, don't do it.
1098	 * There are several cases:
1099	 * - DAD is disabled (ip6_dad_count == 0)
1100	 * - the interface address is anycast
1101	 */
1102	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1103		log(LOG_DEBUG,
1104			"nd6_dad_start: called with non-tentative address "
1105			"%s(%s)\n",
1106			ip6_sprintf(&ia->ia_addr.sin6_addr),
1107			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1108		return;
1109	}
1110	if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1111		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1112		return;
1113	}
1114	if (!ip6_dad_count) {
1115		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1116		return;
1117	}
1118	if (ifa->ifa_ifp == NULL)
1119		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1120	if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
1121		return;
1122	}
1123	if (nd6_dad_find(ifa) != NULL) {
1124		/* DAD already in progress */
1125		return;
1126	}
1127
1128	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1129	if (dp == NULL) {
1130		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1131			"%s(%s)\n",
1132			ip6_sprintf(&ia->ia_addr.sin6_addr),
1133			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1134		return;
1135	}
1136	bzero(dp, sizeof(*dp));
1137	callout_init(&dp->dad_timer_ch, 0);
1138	TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1139
1140	nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1141	    ip6_sprintf(&ia->ia_addr.sin6_addr)));
1142
1143	/*
1144	 * Send NS packet for DAD, ip6_dad_count times.
1145	 * Note that we must delay the first transmission, if this is the
1146	 * first packet to be sent from the interface after interface
1147	 * (re)initialization.
1148	 */
1149	dp->dad_ifa = ifa;
1150	IFAREF(ifa);	/* just for safety */
1151	dp->dad_count = ip6_dad_count;
1152	dp->dad_ns_icount = dp->dad_na_icount = 0;
1153	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1154	if (tick == NULL) {
1155		nd6_dad_ns_output(dp, ifa);
1156		nd6_dad_starttimer(dp,
1157		    ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1158	} else {
1159		int ntick;
1160
1161		if (*tick == 0)
1162			ntick = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
1163		else
1164			ntick = *tick + arc4random() % (hz / 2);
1165		*tick = ntick;
1166		nd6_dad_starttimer(dp, ntick);
1167	}
1168}
1169
1170/*
1171 * terminate DAD unconditionally.  used for address removals.
1172 */
1173void
1174nd6_dad_stop(ifa)
1175	struct ifaddr *ifa;
1176{
1177	struct dadq *dp;
1178
1179	if (!dad_init)
1180		return;
1181	dp = nd6_dad_find(ifa);
1182	if (!dp) {
1183		/* DAD wasn't started yet */
1184		return;
1185	}
1186
1187	nd6_dad_stoptimer(dp);
1188
1189	TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1190	free(dp, M_IP6NDP);
1191	dp = NULL;
1192	IFAFREE(ifa);
1193}
1194
1195static void
1196nd6_dad_timer(ifa)
1197	struct ifaddr *ifa;
1198{
1199	int s;
1200	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1201	struct dadq *dp;
1202
1203	s = splnet();		/* XXX */
1204
1205	/* Sanity check */
1206	if (ia == NULL) {
1207		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1208		goto done;
1209	}
1210	dp = nd6_dad_find(ifa);
1211	if (dp == NULL) {
1212		log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
1213		goto done;
1214	}
1215	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1216		log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1217			"%s(%s)\n",
1218			ip6_sprintf(&ia->ia_addr.sin6_addr),
1219			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1220		goto done;
1221	}
1222	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1223		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1224			"%s(%s)\n",
1225			ip6_sprintf(&ia->ia_addr.sin6_addr),
1226			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1227		goto done;
1228	}
1229
1230	/* timeouted with IFF_{RUNNING,UP} check */
1231	if (dp->dad_ns_tcount > dad_maxtry) {
1232		nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1233		    if_name(ifa->ifa_ifp)));
1234
1235		TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1236		free(dp, M_IP6NDP);
1237		dp = NULL;
1238		IFAFREE(ifa);
1239		goto done;
1240	}
1241
1242	/* Need more checks? */
1243	if (dp->dad_ns_ocount < dp->dad_count) {
1244		/*
1245		 * We have more NS to go.  Send NS packet for DAD.
1246		 */
1247		nd6_dad_ns_output(dp, ifa);
1248		nd6_dad_starttimer(dp,
1249		    ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1250	} else {
1251		/*
1252		 * We have transmitted sufficient number of DAD packets.
1253		 * See what we've got.
1254		 */
1255		int duplicate;
1256
1257		duplicate = 0;
1258
1259		if (dp->dad_na_icount) {
1260			/*
1261			 * the check is in nd6_dad_na_input(),
1262			 * but just in case
1263			 */
1264			duplicate++;
1265		}
1266
1267		if (dp->dad_ns_icount) {
1268			/* We've seen NS, means DAD has failed. */
1269			duplicate++;
1270		}
1271
1272		if (duplicate) {
1273			/* (*dp) will be freed in nd6_dad_duplicated() */
1274			dp = NULL;
1275			nd6_dad_duplicated(ifa);
1276		} else {
1277			/*
1278			 * We are done with DAD.  No NA came, no NS came.
1279			 * No duplicate address found.
1280			 */
1281			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1282
1283			nd6log((LOG_DEBUG,
1284			    "%s: DAD complete for %s - no duplicates found\n",
1285			    if_name(ifa->ifa_ifp),
1286			    ip6_sprintf(&ia->ia_addr.sin6_addr)));
1287
1288			TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1289			free(dp, M_IP6NDP);
1290			dp = NULL;
1291			IFAFREE(ifa);
1292		}
1293	}
1294
1295done:
1296	splx(s);
1297}
1298
1299void
1300nd6_dad_duplicated(ifa)
1301	struct ifaddr *ifa;
1302{
1303	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1304	struct ifnet *ifp;
1305	struct dadq *dp;
1306
1307	dp = nd6_dad_find(ifa);
1308	if (dp == NULL) {
1309		log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1310		return;
1311	}
1312
1313	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1314	    "NS in/out=%d/%d, NA in=%d\n",
1315	    if_name(ifa->ifa_ifp), ip6_sprintf(&ia->ia_addr.sin6_addr),
1316	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1317
1318	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1319	ia->ia6_flags |= IN6_IFF_DUPLICATED;
1320
1321	/* We are done with DAD, with duplicate address found. (failure) */
1322	nd6_dad_stoptimer(dp);
1323
1324	ifp = ifa->ifa_ifp;
1325	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1326	    if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
1327	log(LOG_ERR, "%s: manual intervention required\n",
1328	    if_name(ifp));
1329
1330	/*
1331	 * If the address is a link-local address formed from an interface
1332	 * identifier based on the hardware address which is supposed to be
1333	 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1334	 * operation on the interface SHOULD be disabled.
1335	 * [rfc2462bis-03 Section 5.4.5]
1336	 */
1337	if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1338		struct in6_addr in6;
1339
1340		/*
1341		 * To avoid over-reaction, we only apply this logic when we are
1342		 * very sure that hardware addresses are supposed to be unique.
1343		 */
1344		switch (ifp->if_type) {
1345		case IFT_ETHER:
1346		case IFT_FDDI:
1347		case IFT_ATM:
1348		case IFT_IEEE1394:
1349#ifdef IFT_IEEE80211
1350		case IFT_IEEE80211:
1351#endif
1352			in6 = ia->ia_addr.sin6_addr;
1353			if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1354			    IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1355				ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1356				log(LOG_ERR, "%s: possible hardware address "
1357				    "duplication detected, disable IPv6\n",
1358				    if_name(ifp));
1359			}
1360			break;
1361		}
1362	}
1363
1364	TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1365	free(dp, M_IP6NDP);
1366	dp = NULL;
1367	IFAFREE(ifa);
1368}
1369
1370static void
1371nd6_dad_ns_output(dp, ifa)
1372	struct dadq *dp;
1373	struct ifaddr *ifa;
1374{
1375	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1376	struct ifnet *ifp = ifa->ifa_ifp;
1377
1378	dp->dad_ns_tcount++;
1379	if ((ifp->if_flags & IFF_UP) == 0) {
1380		return;
1381	}
1382	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1383		return;
1384	}
1385
1386	dp->dad_ns_ocount++;
1387	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1388}
1389
1390static void
1391nd6_dad_ns_input(ifa)
1392	struct ifaddr *ifa;
1393{
1394	struct in6_ifaddr *ia;
1395	struct ifnet *ifp;
1396	const struct in6_addr *taddr6;
1397	struct dadq *dp;
1398	int duplicate;
1399
1400	if (ifa == NULL)
1401		panic("ifa == NULL in nd6_dad_ns_input");
1402
1403	ia = (struct in6_ifaddr *)ifa;
1404	ifp = ifa->ifa_ifp;
1405	taddr6 = &ia->ia_addr.sin6_addr;
1406	duplicate = 0;
1407	dp = nd6_dad_find(ifa);
1408
1409	/* Quickhack - completely ignore DAD NS packets */
1410	if (dad_ignore_ns) {
1411		nd6log((LOG_INFO,
1412		    "nd6_dad_ns_input: ignoring DAD NS packet for "
1413		    "address %s(%s)\n", ip6_sprintf(taddr6),
1414		    if_name(ifa->ifa_ifp)));
1415		return;
1416	}
1417
1418	/*
1419	 * if I'm yet to start DAD, someone else started using this address
1420	 * first.  I have a duplicate and you win.
1421	 */
1422	if (dp == NULL || dp->dad_ns_ocount == 0)
1423		duplicate++;
1424
1425	/* XXX more checks for loopback situation - see nd6_dad_timer too */
1426
1427	if (duplicate) {
1428		dp = NULL;	/* will be freed in nd6_dad_duplicated() */
1429		nd6_dad_duplicated(ifa);
1430	} else {
1431		/*
1432		 * not sure if I got a duplicate.
1433		 * increment ns count and see what happens.
1434		 */
1435		if (dp)
1436			dp->dad_ns_icount++;
1437	}
1438}
1439
1440static void
1441nd6_dad_na_input(ifa)
1442	struct ifaddr *ifa;
1443{
1444	struct dadq *dp;
1445
1446	if (ifa == NULL)
1447		panic("ifa == NULL in nd6_dad_na_input");
1448
1449	dp = nd6_dad_find(ifa);
1450	if (dp)
1451		dp->dad_na_icount++;
1452
1453	/* remove the address. */
1454	nd6_dad_duplicated(ifa);
1455}
1456