nd6_nbr.c revision 185571
1/*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	$KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/netinet6/nd6_nbr.c 185571 2008-12-02 21:37:28Z bz $");
34
35#include "opt_inet.h"
36#include "opt_inet6.h"
37#include "opt_ipsec.h"
38#include "opt_carp.h"
39#include "opt_mpath.h"
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/socket.h>
46#include <sys/sockio.h>
47#include <sys/time.h>
48#include <sys/kernel.h>
49#include <sys/errno.h>
50#include <sys/syslog.h>
51#include <sys/queue.h>
52#include <sys/callout.h>
53#include <sys/vimage.h>
54
55#include <net/if.h>
56#include <net/if_types.h>
57#include <net/if_dl.h>
58#include <net/if_var.h>
59#include <net/route.h>
60#ifdef RADIX_MPATH
61#include <net/radix_mpath.h>
62#endif
63
64#include <netinet/in.h>
65#include <netinet/in_var.h>
66#include <netinet6/in6_var.h>
67#include <netinet6/in6_ifattach.h>
68#include <netinet/ip6.h>
69#include <netinet6/ip6_var.h>
70#include <netinet6/scope6_var.h>
71#include <netinet6/nd6.h>
72#include <netinet/icmp6.h>
73#include <netinet6/vinet6.h>
74
75#ifdef DEV_CARP
76#include <netinet/ip_carp.h>
77#endif
78
79#define SDL(s) ((struct sockaddr_dl *)s)
80
81struct dadq;
82static struct dadq *nd6_dad_find(struct ifaddr *);
83static void nd6_dad_starttimer(struct dadq *, int);
84static void nd6_dad_stoptimer(struct dadq *);
85static void nd6_dad_timer(struct ifaddr *);
86static void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
87static void nd6_dad_ns_input(struct ifaddr *);
88static void nd6_dad_na_input(struct ifaddr *);
89
90#ifdef VIMAGE_GLOBALS
91int dad_ignore_ns;
92int dad_maxtry;
93#endif
94
95/*
96 * Input a Neighbor Solicitation Message.
97 *
98 * Based on RFC 2461
99 * Based on RFC 2462 (duplicate address detection)
100 */
101void
102nd6_ns_input(struct mbuf *m, int off, int icmp6len)
103{
104	INIT_VNET_INET6(curvnet);
105	struct ifnet *ifp = m->m_pkthdr.rcvif;
106	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
107	struct nd_neighbor_solicit *nd_ns;
108	struct in6_addr saddr6 = ip6->ip6_src;
109	struct in6_addr daddr6 = ip6->ip6_dst;
110	struct in6_addr taddr6;
111	struct in6_addr myaddr6;
112	char *lladdr = NULL;
113	struct ifaddr *ifa = NULL;
114	int lladdrlen = 0;
115	int anycast = 0, proxy = 0, tentative = 0;
116	int tlladdr;
117	union nd_opts ndopts;
118	struct sockaddr_dl *proxydl = NULL;
119	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
120
121#ifndef PULLDOWN_TEST
122	IP6_EXTHDR_CHECK(m, off, icmp6len,);
123	nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
124#else
125	IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
126	if (nd_ns == NULL) {
127		V_icmp6stat.icp6s_tooshort++;
128		return;
129	}
130#endif
131	ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
132	taddr6 = nd_ns->nd_ns_target;
133	if (in6_setscope(&taddr6, ifp, NULL) != 0)
134		goto bad;
135
136	if (ip6->ip6_hlim != 255) {
137		nd6log((LOG_ERR,
138		    "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
139		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
140		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
141		goto bad;
142	}
143
144	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
145		/* dst has to be a solicited node multicast address. */
146		if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
147		    /* don't check ifindex portion */
148		    daddr6.s6_addr32[1] == 0 &&
149		    daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
150		    daddr6.s6_addr8[12] == 0xff) {
151			; /* good */
152		} else {
153			nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
154			    "(wrong ip6 dst)\n"));
155			goto bad;
156		}
157	} else if (!V_nd6_onlink_ns_rfc4861) {
158		struct sockaddr_in6 src_sa6;
159
160		/*
161		 * According to recent IETF discussions, it is not a good idea
162		 * to accept a NS from an address which would not be deemed
163		 * to be a neighbor otherwise.  This point is expected to be
164		 * clarified in future revisions of the specification.
165		 */
166		bzero(&src_sa6, sizeof(src_sa6));
167		src_sa6.sin6_family = AF_INET6;
168		src_sa6.sin6_len = sizeof(src_sa6);
169		src_sa6.sin6_addr = saddr6;
170		if (!nd6_is_addr_neighbor(&src_sa6, ifp)) {
171			nd6log((LOG_INFO, "nd6_ns_input: "
172				"NS packet from non-neighbor\n"));
173			goto bad;
174		}
175	}
176
177	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
178		nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
179		goto bad;
180	}
181
182	icmp6len -= sizeof(*nd_ns);
183	nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
184	if (nd6_options(&ndopts) < 0) {
185		nd6log((LOG_INFO,
186		    "nd6_ns_input: invalid ND option, ignored\n"));
187		/* nd6_options have incremented stats */
188		goto freeit;
189	}
190
191	if (ndopts.nd_opts_src_lladdr) {
192		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
193		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
194	}
195
196	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
197		nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
198		    "(link-layer address option)\n"));
199		goto bad;
200	}
201
202	/*
203	 * Attaching target link-layer address to the NA?
204	 * (RFC 2461 7.2.4)
205	 *
206	 * NS IP dst is unicast/anycast			MUST NOT add
207	 * NS IP dst is solicited-node multicast	MUST add
208	 *
209	 * In implementation, we add target link-layer address by default.
210	 * We do not add one in MUST NOT cases.
211	 */
212	if (!IN6_IS_ADDR_MULTICAST(&daddr6))
213		tlladdr = 0;
214	else
215		tlladdr = 1;
216
217	/*
218	 * Target address (taddr6) must be either:
219	 * (1) Valid unicast/anycast address for my receiving interface,
220	 * (2) Unicast address for which I'm offering proxy service, or
221	 * (3) "tentative" address on which DAD is being performed.
222	 */
223	/* (1) and (3) check. */
224#ifdef DEV_CARP
225	if (ifp->if_carp)
226		ifa = carp_iamatch6(ifp->if_carp, &taddr6);
227	if (ifa == NULL)
228		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
229#else
230	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
231#endif
232
233	/* (2) check. */
234	if (ifa == NULL) {
235		struct rtentry *rt;
236		struct sockaddr_in6 tsin6;
237		int need_proxy;
238#ifdef RADIX_MPATH
239		struct route_in6 ro;
240#endif
241
242		bzero(&tsin6, sizeof tsin6);
243		tsin6.sin6_len = sizeof(struct sockaddr_in6);
244		tsin6.sin6_family = AF_INET6;
245		tsin6.sin6_addr = taddr6;
246
247#ifdef RADIX_MPATH
248		bzero(&ro, sizeof(ro));
249		ro.ro_dst = tsin6;
250		rtalloc_mpath((struct route *)&ro, RTF_ANNOUNCE);
251		rt = ro.ro_rt;
252#else
253		rt = rtalloc1((struct sockaddr *)&tsin6, 0, 0);
254#endif
255		need_proxy = (rt && (rt->rt_flags & RTF_ANNOUNCE) != 0 &&
256		    rt->rt_gateway->sa_family == AF_LINK);
257		if (rt)
258			rtfree(rt);
259		if (need_proxy) {
260			/*
261			 * proxy NDP for single entry
262			 */
263			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp,
264				IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
265			if (ifa) {
266				proxy = 1;
267				proxydl = SDL(rt->rt_gateway);
268			}
269		}
270	}
271	if (ifa == NULL) {
272		/*
273		 * We've got an NS packet, and we don't have that adddress
274		 * assigned for us.  We MUST silently ignore it.
275		 * See RFC2461 7.2.3.
276		 */
277		goto freeit;
278	}
279	myaddr6 = *IFA_IN6(ifa);
280	anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
281	tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
282	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
283		goto freeit;
284
285	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
286		nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
287		    "(if %d, NS packet %d)\n",
288		    ip6_sprintf(ip6bufs, &taddr6),
289		    ifp->if_addrlen, lladdrlen - 2));
290		goto bad;
291	}
292
293	if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
294		nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
295		    ip6_sprintf(ip6bufs, &saddr6)));
296		goto freeit;
297	}
298
299	/*
300	 * We have neighbor solicitation packet, with target address equals to
301	 * one of my tentative address.
302	 *
303	 * src addr	how to process?
304	 * ---		---
305	 * multicast	of course, invalid (rejected in ip6_input)
306	 * unicast	somebody is doing address resolution -> ignore
307	 * unspec	dup address detection
308	 *
309	 * The processing is defined in RFC 2462.
310	 */
311	if (tentative) {
312		/*
313		 * If source address is unspecified address, it is for
314		 * duplicate address detection.
315		 *
316		 * If not, the packet is for addess resolution;
317		 * silently ignore it.
318		 */
319		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
320			nd6_dad_ns_input(ifa);
321
322		goto freeit;
323	}
324
325	/*
326	 * If the source address is unspecified address, entries must not
327	 * be created or updated.
328	 * It looks that sender is performing DAD.  Output NA toward
329	 * all-node multicast address, to tell the sender that I'm using
330	 * the address.
331	 * S bit ("solicited") must be zero.
332	 */
333	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
334		struct in6_addr in6_all;
335
336		in6_all = in6addr_linklocal_allnodes;
337		if (in6_setscope(&in6_all, ifp, NULL) != 0)
338			goto bad;
339		nd6_na_output(ifp, &in6_all, &taddr6,
340		    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
341		    (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0),
342		    tlladdr, (struct sockaddr *)proxydl);
343		goto freeit;
344	}
345
346	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
347	    ND_NEIGHBOR_SOLICIT, 0);
348
349	nd6_na_output(ifp, &saddr6, &taddr6,
350	    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
351	    (V_ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
352	    tlladdr, (struct sockaddr *)proxydl);
353 freeit:
354	m_freem(m);
355	return;
356
357 bad:
358	nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
359		ip6_sprintf(ip6bufs, &saddr6)));
360	nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
361		ip6_sprintf(ip6bufs, &daddr6)));
362	nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
363		ip6_sprintf(ip6bufs, &taddr6)));
364	V_icmp6stat.icp6s_badns++;
365	m_freem(m);
366}
367
368/*
369 * Output a Neighbor Solicitation Message. Caller specifies:
370 *	- ICMP6 header source IP6 address
371 *	- ND6 header target IP6 address
372 *	- ND6 header source datalink address
373 *
374 * Based on RFC 2461
375 * Based on RFC 2462 (duplicate address detection)
376 *
377 *   ln - for source address determination
378 *  dad - duplicate address detection
379 */
380void
381nd6_ns_output(struct ifnet *ifp, const struct in6_addr *daddr6,
382    const struct in6_addr *taddr6, struct llinfo_nd6 *ln, int dad)
383{
384	INIT_VNET_INET6(ifp->if_vnet);
385	struct mbuf *m;
386	struct ip6_hdr *ip6;
387	struct nd_neighbor_solicit *nd_ns;
388	struct in6_addr *src, src_in;
389	struct ip6_moptions im6o;
390	int icmp6len;
391	int maxlen;
392	caddr_t mac;
393	struct route_in6 ro;
394
395	bzero(&ro, sizeof(ro));
396
397	if (IN6_IS_ADDR_MULTICAST(taddr6))
398		return;
399
400	/* estimate the size of message */
401	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
402	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
403	if (max_linkhdr + maxlen >= MCLBYTES) {
404#ifdef DIAGNOSTIC
405		printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
406		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
407#endif
408		return;
409	}
410
411	MGETHDR(m, M_DONTWAIT, MT_DATA);
412	if (m && max_linkhdr + maxlen >= MHLEN) {
413		MCLGET(m, M_DONTWAIT);
414		if ((m->m_flags & M_EXT) == 0) {
415			m_free(m);
416			m = NULL;
417		}
418	}
419	if (m == NULL)
420		return;
421	m->m_pkthdr.rcvif = NULL;
422
423	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
424		m->m_flags |= M_MCAST;
425		im6o.im6o_multicast_ifp = ifp;
426		im6o.im6o_multicast_hlim = 255;
427		im6o.im6o_multicast_loop = 0;
428	}
429
430	icmp6len = sizeof(*nd_ns);
431	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
432	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
433
434	/* fill neighbor solicitation packet */
435	ip6 = mtod(m, struct ip6_hdr *);
436	ip6->ip6_flow = 0;
437	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
438	ip6->ip6_vfc |= IPV6_VERSION;
439	/* ip6->ip6_plen will be set later */
440	ip6->ip6_nxt = IPPROTO_ICMPV6;
441	ip6->ip6_hlim = 255;
442	if (daddr6)
443		ip6->ip6_dst = *daddr6;
444	else {
445		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
446		ip6->ip6_dst.s6_addr16[1] = 0;
447		ip6->ip6_dst.s6_addr32[1] = 0;
448		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
449		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
450		ip6->ip6_dst.s6_addr8[12] = 0xff;
451		if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
452			goto bad;
453	}
454	if (!dad) {
455		/*
456		 * RFC2461 7.2.2:
457		 * "If the source address of the packet prompting the
458		 * solicitation is the same as one of the addresses assigned
459		 * to the outgoing interface, that address SHOULD be placed
460		 * in the IP Source Address of the outgoing solicitation.
461		 * Otherwise, any one of the addresses assigned to the
462		 * interface should be used."
463		 *
464		 * We use the source address for the prompting packet
465		 * (saddr6), if:
466		 * - saddr6 is given from the caller (by giving "ln"), and
467		 * - saddr6 belongs to the outgoing interface.
468		 * Otherwise, we perform the source address selection as usual.
469		 */
470		struct ip6_hdr *hip6;		/* hold ip6 */
471		struct in6_addr *hsrc = NULL;
472
473		if (ln && ln->ln_hold) {
474			/*
475			 * assuming every packet in ln_hold has the same IP
476			 * header
477			 */
478			hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
479			/* XXX pullup? */
480			if (sizeof(*hip6) < ln->ln_hold->m_len)
481				hsrc = &hip6->ip6_src;
482			else
483				hsrc = NULL;
484		}
485		if (hsrc && in6ifa_ifpwithaddr(ifp, hsrc))
486			src = hsrc;
487		else {
488			int error;
489			struct sockaddr_in6 dst_sa;
490
491			bzero(&dst_sa, sizeof(dst_sa));
492			dst_sa.sin6_family = AF_INET6;
493			dst_sa.sin6_len = sizeof(dst_sa);
494			dst_sa.sin6_addr = ip6->ip6_dst;
495
496			src = in6_selectsrc(&dst_sa, NULL,
497			    NULL, &ro, NULL, NULL, &error);
498			if (src == NULL) {
499				char ip6buf[INET6_ADDRSTRLEN];
500				nd6log((LOG_DEBUG,
501				    "nd6_ns_output: source can't be "
502				    "determined: dst=%s, error=%d\n",
503				    ip6_sprintf(ip6buf, &dst_sa.sin6_addr),
504				    error));
505				goto bad;
506			}
507		}
508	} else {
509		/*
510		 * Source address for DAD packet must always be IPv6
511		 * unspecified address. (0::0)
512		 * We actually don't have to 0-clear the address (we did it
513		 * above), but we do so here explicitly to make the intention
514		 * clearer.
515		 */
516		bzero(&src_in, sizeof(src_in));
517		src = &src_in;
518	}
519	ip6->ip6_src = *src;
520	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
521	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
522	nd_ns->nd_ns_code = 0;
523	nd_ns->nd_ns_reserved = 0;
524	nd_ns->nd_ns_target = *taddr6;
525	in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
526
527	/*
528	 * Add source link-layer address option.
529	 *
530	 *				spec		implementation
531	 *				---		---
532	 * DAD packet			MUST NOT	do not add the option
533	 * there's no link layer address:
534	 *				impossible	do not add the option
535	 * there's link layer address:
536	 *	Multicast NS		MUST add one	add the option
537	 *	Unicast NS		SHOULD add one	add the option
538	 */
539	if (!dad && (mac = nd6_ifptomac(ifp))) {
540		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
541		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
542		/* 8 byte alignments... */
543		optlen = (optlen + 7) & ~7;
544
545		m->m_pkthdr.len += optlen;
546		m->m_len += optlen;
547		icmp6len += optlen;
548		bzero((caddr_t)nd_opt, optlen);
549		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
550		nd_opt->nd_opt_len = optlen >> 3;
551		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
552	}
553
554	ip6->ip6_plen = htons((u_short)icmp6len);
555	nd_ns->nd_ns_cksum = 0;
556	nd_ns->nd_ns_cksum =
557	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
558
559	ip6_output(m, NULL, &ro, dad ? IPV6_UNSPECSRC : 0, &im6o, NULL, NULL);
560	icmp6_ifstat_inc(ifp, ifs6_out_msg);
561	icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
562	V_icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
563
564	if (ro.ro_rt) {		/* we don't cache this route. */
565		RTFREE(ro.ro_rt);
566	}
567	return;
568
569  bad:
570	if (ro.ro_rt) {
571		RTFREE(ro.ro_rt);
572	}
573	m_freem(m);
574	return;
575}
576
577/*
578 * Neighbor advertisement input handling.
579 *
580 * Based on RFC 2461
581 * Based on RFC 2462 (duplicate address detection)
582 *
583 * the following items are not implemented yet:
584 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
585 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
586 */
587void
588nd6_na_input(struct mbuf *m, int off, int icmp6len)
589{
590	INIT_VNET_INET6(curvnet);
591	struct ifnet *ifp = m->m_pkthdr.rcvif;
592	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
593	struct nd_neighbor_advert *nd_na;
594	struct in6_addr daddr6 = ip6->ip6_dst;
595	struct in6_addr taddr6;
596	int flags;
597	int is_router;
598	int is_solicited;
599	int is_override;
600	char *lladdr = NULL;
601	int lladdrlen = 0;
602	struct ifaddr *ifa;
603	struct llinfo_nd6 *ln;
604	struct rtentry *rt;
605	struct sockaddr_dl *sdl;
606	union nd_opts ndopts;
607	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
608
609	if (ip6->ip6_hlim != 255) {
610		nd6log((LOG_ERR,
611		    "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
612		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
613		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
614		goto bad;
615	}
616
617#ifndef PULLDOWN_TEST
618	IP6_EXTHDR_CHECK(m, off, icmp6len,);
619	nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
620#else
621	IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
622	if (nd_na == NULL) {
623		V_icmp6stat.icp6s_tooshort++;
624		return;
625	}
626#endif
627
628	flags = nd_na->nd_na_flags_reserved;
629	is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
630	is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
631	is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
632
633	taddr6 = nd_na->nd_na_target;
634	if (in6_setscope(&taddr6, ifp, NULL))
635		goto bad;	/* XXX: impossible */
636
637	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
638		nd6log((LOG_ERR,
639		    "nd6_na_input: invalid target address %s\n",
640		    ip6_sprintf(ip6bufs, &taddr6)));
641		goto bad;
642	}
643	if (IN6_IS_ADDR_MULTICAST(&daddr6))
644		if (is_solicited) {
645			nd6log((LOG_ERR,
646			    "nd6_na_input: a solicited adv is multicasted\n"));
647			goto bad;
648		}
649
650	icmp6len -= sizeof(*nd_na);
651	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
652	if (nd6_options(&ndopts) < 0) {
653		nd6log((LOG_INFO,
654		    "nd6_na_input: invalid ND option, ignored\n"));
655		/* nd6_options have incremented stats */
656		goto freeit;
657	}
658
659	if (ndopts.nd_opts_tgt_lladdr) {
660		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
661		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
662	}
663
664	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
665
666	/*
667	 * Target address matches one of my interface address.
668	 *
669	 * If my address is tentative, this means that there's somebody
670	 * already using the same address as mine.  This indicates DAD failure.
671	 * This is defined in RFC 2462.
672	 *
673	 * Otherwise, process as defined in RFC 2461.
674	 */
675	if (ifa
676	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
677		nd6_dad_na_input(ifa);
678		goto freeit;
679	}
680
681	/* Just for safety, maybe unnecessary. */
682	if (ifa) {
683		log(LOG_ERR,
684		    "nd6_na_input: duplicate IP6 address %s\n",
685		    ip6_sprintf(ip6bufs, &taddr6));
686		goto freeit;
687	}
688
689	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
690		nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
691		    "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
692		    ifp->if_addrlen, lladdrlen - 2));
693		goto bad;
694	}
695
696	/*
697	 * If no neighbor cache entry is found, NA SHOULD silently be
698	 * discarded.
699	 */
700	rt = nd6_lookup(&taddr6, 0, ifp);
701	if ((rt == NULL) ||
702	   ((ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) ||
703	   ((sdl = SDL(rt->rt_gateway)) == NULL))
704		goto freeit;
705
706	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
707		/*
708		 * If the link-layer has address, and no lladdr option came,
709		 * discard the packet.
710		 */
711		if (ifp->if_addrlen && lladdr == NULL)
712			goto freeit;
713
714		/*
715		 * Record link-layer address, and update the state.
716		 */
717		sdl->sdl_alen = ifp->if_addrlen;
718		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
719		if (is_solicited) {
720			ln->ln_state = ND6_LLINFO_REACHABLE;
721			ln->ln_byhint = 0;
722			if (!ND6_LLINFO_PERMANENT(ln)) {
723				nd6_llinfo_settimer(ln,
724				    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
725			}
726		} else {
727			ln->ln_state = ND6_LLINFO_STALE;
728			nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
729		}
730		if ((ln->ln_router = is_router) != 0) {
731			/*
732			 * This means a router's state has changed from
733			 * non-reachable to probably reachable, and might
734			 * affect the status of associated prefixes..
735			 */
736			pfxlist_onlink_check();
737		}
738	} else {
739		int llchange;
740
741		/*
742		 * Check if the link-layer address has changed or not.
743		 */
744		if (lladdr == NULL)
745			llchange = 0;
746		else {
747			if (sdl->sdl_alen) {
748				if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
749					llchange = 1;
750				else
751					llchange = 0;
752			} else
753				llchange = 1;
754		}
755
756		/*
757		 * This is VERY complex.  Look at it with care.
758		 *
759		 * override solicit lladdr llchange	action
760		 *					(L: record lladdr)
761		 *
762		 *	0	0	n	--	(2c)
763		 *	0	0	y	n	(2b) L
764		 *	0	0	y	y	(1)    REACHABLE->STALE
765		 *	0	1	n	--	(2c)   *->REACHABLE
766		 *	0	1	y	n	(2b) L *->REACHABLE
767		 *	0	1	y	y	(1)    REACHABLE->STALE
768		 *	1	0	n	--	(2a)
769		 *	1	0	y	n	(2a) L
770		 *	1	0	y	y	(2a) L *->STALE
771		 *	1	1	n	--	(2a)   *->REACHABLE
772		 *	1	1	y	n	(2a) L *->REACHABLE
773		 *	1	1	y	y	(2a) L *->REACHABLE
774		 */
775		if (!is_override && (lladdr != NULL && llchange)) {  /* (1) */
776			/*
777			 * If state is REACHABLE, make it STALE.
778			 * no other updates should be done.
779			 */
780			if (ln->ln_state == ND6_LLINFO_REACHABLE) {
781				ln->ln_state = ND6_LLINFO_STALE;
782				nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
783			}
784			goto freeit;
785		} else if (is_override				   /* (2a) */
786			|| (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
787			|| lladdr == NULL) {			   /* (2c) */
788			/*
789			 * Update link-local address, if any.
790			 */
791			if (lladdr != NULL) {
792				sdl->sdl_alen = ifp->if_addrlen;
793				bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
794			}
795
796			/*
797			 * If solicited, make the state REACHABLE.
798			 * If not solicited and the link-layer address was
799			 * changed, make it STALE.
800			 */
801			if (is_solicited) {
802				ln->ln_state = ND6_LLINFO_REACHABLE;
803				ln->ln_byhint = 0;
804				if (!ND6_LLINFO_PERMANENT(ln)) {
805					nd6_llinfo_settimer(ln,
806					    (long)ND_IFINFO(ifp)->reachable * hz);
807				}
808			} else {
809				if (lladdr != NULL && llchange) {
810					ln->ln_state = ND6_LLINFO_STALE;
811					nd6_llinfo_settimer(ln,
812					    (long)V_nd6_gctimer * hz);
813				}
814			}
815		}
816
817		if (ln->ln_router && !is_router) {
818			/*
819			 * The peer dropped the router flag.
820			 * Remove the sender from the Default Router List and
821			 * update the Destination Cache entries.
822			 */
823			struct nd_defrouter *dr;
824			struct in6_addr *in6;
825			int s;
826
827			in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
828
829			/*
830			 * Lock to protect the default router list.
831			 * XXX: this might be unnecessary, since this function
832			 * is only called under the network software interrupt
833			 * context.  However, we keep it just for safety.
834			 */
835			s = splnet();
836			dr = defrouter_lookup(in6, ifp);
837			if (dr)
838				defrtrlist_del(dr);
839			else if (!V_ip6_forwarding) {
840				/*
841				 * Even if the neighbor is not in the default
842				 * router list, the neighbor may be used
843				 * as a next hop for some destinations
844				 * (e.g. redirect case). So we must
845				 * call rt6_flush explicitly.
846				 */
847				rt6_flush(&ip6->ip6_src, ifp);
848			}
849			splx(s);
850		}
851		ln->ln_router = is_router;
852	}
853	rt->rt_flags &= ~RTF_REJECT;
854	ln->ln_asked = 0;
855	if (ln->ln_hold) {
856		struct mbuf *m_hold, *m_hold_next;
857
858		/*
859		 * reset the ln_hold in advance, to explicitly
860		 * prevent a ln_hold lookup in nd6_output()
861		 * (wouldn't happen, though...)
862		 */
863		for (m_hold = ln->ln_hold;
864		    m_hold; m_hold = m_hold_next) {
865			m_hold_next = m_hold->m_nextpkt;
866			m_hold->m_nextpkt = NULL;
867			/*
868			 * we assume ifp is not a loopback here, so just set
869			 * the 2nd argument as the 1st one.
870			 */
871			nd6_output(ifp, ifp, m_hold,
872			    (struct sockaddr_in6 *)rt_key(rt), rt);
873		}
874		ln->ln_hold = NULL;
875	}
876
877 freeit:
878	m_freem(m);
879	return;
880
881 bad:
882	V_icmp6stat.icp6s_badna++;
883	m_freem(m);
884}
885
886/*
887 * Neighbor advertisement output handling.
888 *
889 * Based on RFC 2461
890 *
891 * the following items are not implemented yet:
892 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
893 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
894 *
895 * tlladdr - 1 if include target link-layer address
896 * sdl0 - sockaddr_dl (= proxy NA) or NULL
897 */
898void
899nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
900    const struct in6_addr *taddr6, u_long flags, int tlladdr,
901    struct sockaddr *sdl0)
902{
903	INIT_VNET_INET6(ifp->if_vnet);
904	struct mbuf *m;
905	struct ip6_hdr *ip6;
906	struct nd_neighbor_advert *nd_na;
907	struct ip6_moptions im6o;
908	struct in6_addr *src, daddr6;
909	struct sockaddr_in6 dst_sa;
910	int icmp6len, maxlen, error;
911	caddr_t mac = NULL;
912	struct route_in6 ro;
913
914	bzero(&ro, sizeof(ro));
915
916	daddr6 = *daddr6_0;	/* make a local copy for modification */
917
918	/* estimate the size of message */
919	maxlen = sizeof(*ip6) + sizeof(*nd_na);
920	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
921	if (max_linkhdr + maxlen >= MCLBYTES) {
922#ifdef DIAGNOSTIC
923		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
924		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
925#endif
926		return;
927	}
928
929	MGETHDR(m, M_DONTWAIT, MT_DATA);
930	if (m && max_linkhdr + maxlen >= MHLEN) {
931		MCLGET(m, M_DONTWAIT);
932		if ((m->m_flags & M_EXT) == 0) {
933			m_free(m);
934			m = NULL;
935		}
936	}
937	if (m == NULL)
938		return;
939	m->m_pkthdr.rcvif = NULL;
940
941	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
942		m->m_flags |= M_MCAST;
943		im6o.im6o_multicast_ifp = ifp;
944		im6o.im6o_multicast_hlim = 255;
945		im6o.im6o_multicast_loop = 0;
946	}
947
948	icmp6len = sizeof(*nd_na);
949	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
950	m->m_data += max_linkhdr;	/* or MH_ALIGN() equivalent? */
951
952	/* fill neighbor advertisement packet */
953	ip6 = mtod(m, struct ip6_hdr *);
954	ip6->ip6_flow = 0;
955	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
956	ip6->ip6_vfc |= IPV6_VERSION;
957	ip6->ip6_nxt = IPPROTO_ICMPV6;
958	ip6->ip6_hlim = 255;
959	if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
960		/* reply to DAD */
961		daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
962		daddr6.s6_addr16[1] = 0;
963		daddr6.s6_addr32[1] = 0;
964		daddr6.s6_addr32[2] = 0;
965		daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
966		if (in6_setscope(&daddr6, ifp, NULL))
967			goto bad;
968
969		flags &= ~ND_NA_FLAG_SOLICITED;
970	}
971	ip6->ip6_dst = daddr6;
972	bzero(&dst_sa, sizeof(struct sockaddr_in6));
973	dst_sa.sin6_family = AF_INET6;
974	dst_sa.sin6_len = sizeof(struct sockaddr_in6);
975	dst_sa.sin6_addr = daddr6;
976
977	/*
978	 * Select a source whose scope is the same as that of the dest.
979	 */
980	bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
981	src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, NULL, &error);
982	if (src == NULL) {
983		char ip6buf[INET6_ADDRSTRLEN];
984		nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
985		    "determined: dst=%s, error=%d\n",
986		    ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
987		goto bad;
988	}
989	ip6->ip6_src = *src;
990	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
991	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
992	nd_na->nd_na_code = 0;
993	nd_na->nd_na_target = *taddr6;
994	in6_clearscope(&nd_na->nd_na_target); /* XXX */
995
996	/*
997	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
998	 * see nd6_ns_input() for details.
999	 * Basically, if NS packet is sent to unicast/anycast addr,
1000	 * target lladdr option SHOULD NOT be included.
1001	 */
1002	if (tlladdr) {
1003		/*
1004		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
1005		 * lladdr in sdl0.  If we are not proxying (sending NA for
1006		 * my address) use lladdr configured for the interface.
1007		 */
1008		if (sdl0 == NULL) {
1009#ifdef DEV_CARP
1010			if (ifp->if_carp)
1011				mac = carp_macmatch6(ifp->if_carp, m, taddr6);
1012			if (mac == NULL)
1013				mac = nd6_ifptomac(ifp);
1014#else
1015			mac = nd6_ifptomac(ifp);
1016#endif
1017		} else if (sdl0->sa_family == AF_LINK) {
1018			struct sockaddr_dl *sdl;
1019			sdl = (struct sockaddr_dl *)sdl0;
1020			if (sdl->sdl_alen == ifp->if_addrlen)
1021				mac = LLADDR(sdl);
1022		}
1023	}
1024	if (tlladdr && mac) {
1025		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1026		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1027
1028		/* roundup to 8 bytes alignment! */
1029		optlen = (optlen + 7) & ~7;
1030
1031		m->m_pkthdr.len += optlen;
1032		m->m_len += optlen;
1033		icmp6len += optlen;
1034		bzero((caddr_t)nd_opt, optlen);
1035		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1036		nd_opt->nd_opt_len = optlen >> 3;
1037		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
1038	} else
1039		flags &= ~ND_NA_FLAG_OVERRIDE;
1040
1041	ip6->ip6_plen = htons((u_short)icmp6len);
1042	nd_na->nd_na_flags_reserved = flags;
1043	nd_na->nd_na_cksum = 0;
1044	nd_na->nd_na_cksum =
1045	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1046
1047	ip6_output(m, NULL, &ro, 0, &im6o, NULL, NULL);
1048	icmp6_ifstat_inc(ifp, ifs6_out_msg);
1049	icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1050	V_icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
1051
1052	if (ro.ro_rt) {		/* we don't cache this route. */
1053		RTFREE(ro.ro_rt);
1054	}
1055	return;
1056
1057  bad:
1058	if (ro.ro_rt) {
1059		RTFREE(ro.ro_rt);
1060	}
1061	m_freem(m);
1062	return;
1063}
1064
1065caddr_t
1066nd6_ifptomac(struct ifnet *ifp)
1067{
1068	switch (ifp->if_type) {
1069	case IFT_ARCNET:
1070	case IFT_ETHER:
1071	case IFT_FDDI:
1072	case IFT_IEEE1394:
1073#ifdef IFT_L2VLAN
1074	case IFT_L2VLAN:
1075#endif
1076#ifdef IFT_IEEE80211
1077	case IFT_IEEE80211:
1078#endif
1079#ifdef IFT_CARP
1080	case IFT_CARP:
1081#endif
1082	case IFT_BRIDGE:
1083	case IFT_ISO88025:
1084		return IF_LLADDR(ifp);
1085	default:
1086		return NULL;
1087	}
1088}
1089
1090TAILQ_HEAD(dadq_head, dadq);
1091struct dadq {
1092	TAILQ_ENTRY(dadq) dad_list;
1093	struct ifaddr *dad_ifa;
1094	int dad_count;		/* max NS to send */
1095	int dad_ns_tcount;	/* # of trials to send NS */
1096	int dad_ns_ocount;	/* NS sent so far */
1097	int dad_ns_icount;
1098	int dad_na_icount;
1099	struct callout dad_timer_ch;
1100};
1101
1102#ifdef VIMAGE_GLOBALS
1103static struct dadq_head dadq;
1104int dad_init;
1105#endif
1106
1107static struct dadq *
1108nd6_dad_find(struct ifaddr *ifa)
1109{
1110	INIT_VNET_INET6(curvnet);
1111	struct dadq *dp;
1112
1113	for (dp = V_dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
1114		if (dp->dad_ifa == ifa)
1115			return dp;
1116	}
1117	return NULL;
1118}
1119
1120static void
1121nd6_dad_starttimer(struct dadq *dp, int ticks)
1122{
1123
1124	callout_reset(&dp->dad_timer_ch, ticks,
1125	    (void (*)(void *))nd6_dad_timer, (void *)dp->dad_ifa);
1126}
1127
1128static void
1129nd6_dad_stoptimer(struct dadq *dp)
1130{
1131
1132	callout_stop(&dp->dad_timer_ch);
1133}
1134
1135/*
1136 * Start Duplicate Address Detection (DAD) for specified interface address.
1137 */
1138void
1139nd6_dad_start(struct ifaddr *ifa, int delay)
1140{
1141	INIT_VNET_INET6(curvnet);
1142	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1143	struct dadq *dp;
1144	char ip6buf[INET6_ADDRSTRLEN];
1145
1146	if (!V_dad_init) {
1147		TAILQ_INIT(&V_dadq);
1148		V_dad_init++;
1149	}
1150
1151	/*
1152	 * If we don't need DAD, don't do it.
1153	 * There are several cases:
1154	 * - DAD is disabled (ip6_dad_count == 0)
1155	 * - the interface address is anycast
1156	 */
1157	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1158		log(LOG_DEBUG,
1159			"nd6_dad_start: called with non-tentative address "
1160			"%s(%s)\n",
1161			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1162			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1163		return;
1164	}
1165	if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1166		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1167		return;
1168	}
1169	if (!V_ip6_dad_count) {
1170		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1171		return;
1172	}
1173	if (ifa->ifa_ifp == NULL)
1174		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1175	if (!(ifa->ifa_ifp->if_flags & IFF_UP)) {
1176		return;
1177	}
1178	if (nd6_dad_find(ifa) != NULL) {
1179		/* DAD already in progress */
1180		return;
1181	}
1182
1183	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT);
1184	if (dp == NULL) {
1185		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1186			"%s(%s)\n",
1187			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1188			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1189		return;
1190	}
1191	bzero(dp, sizeof(*dp));
1192	callout_init(&dp->dad_timer_ch, 0);
1193	TAILQ_INSERT_TAIL(&V_dadq, (struct dadq *)dp, dad_list);
1194
1195	nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1196	    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1197
1198	/*
1199	 * Send NS packet for DAD, ip6_dad_count times.
1200	 * Note that we must delay the first transmission, if this is the
1201	 * first packet to be sent from the interface after interface
1202	 * (re)initialization.
1203	 */
1204	dp->dad_ifa = ifa;
1205	IFAREF(ifa);	/* just for safety */
1206	dp->dad_count = V_ip6_dad_count;
1207	dp->dad_ns_icount = dp->dad_na_icount = 0;
1208	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1209	if (delay == 0) {
1210		nd6_dad_ns_output(dp, ifa);
1211		nd6_dad_starttimer(dp,
1212		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1213	} else {
1214		nd6_dad_starttimer(dp, delay);
1215	}
1216}
1217
1218/*
1219 * terminate DAD unconditionally.  used for address removals.
1220 */
1221void
1222nd6_dad_stop(struct ifaddr *ifa)
1223{
1224	INIT_VNET_INET6(curvnet);
1225	struct dadq *dp;
1226
1227	if (!V_dad_init)
1228		return;
1229	dp = nd6_dad_find(ifa);
1230	if (!dp) {
1231		/* DAD wasn't started yet */
1232		return;
1233	}
1234
1235	nd6_dad_stoptimer(dp);
1236
1237	TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1238	free(dp, M_IP6NDP);
1239	dp = NULL;
1240	IFAFREE(ifa);
1241}
1242
1243static void
1244nd6_dad_timer(struct ifaddr *ifa)
1245{
1246	CURVNET_SET(dp->dad_vnet);
1247	INIT_VNET_INET6(curvnet);
1248	int s;
1249	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1250	struct dadq *dp;
1251	char ip6buf[INET6_ADDRSTRLEN];
1252
1253	s = splnet();		/* XXX */
1254
1255	/* Sanity check */
1256	if (ia == NULL) {
1257		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1258		goto done;
1259	}
1260	dp = nd6_dad_find(ifa);
1261	if (dp == NULL) {
1262		log(LOG_ERR, "nd6_dad_timer: DAD structure not found\n");
1263		goto done;
1264	}
1265	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1266		log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1267			"%s(%s)\n",
1268			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1269			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1270		goto done;
1271	}
1272	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1273		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1274			"%s(%s)\n",
1275			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1276			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1277		goto done;
1278	}
1279
1280	/* timeouted with IFF_{RUNNING,UP} check */
1281	if (dp->dad_ns_tcount > V_dad_maxtry) {
1282		nd6log((LOG_INFO, "%s: could not run DAD, driver problem?\n",
1283		    if_name(ifa->ifa_ifp)));
1284
1285		TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1286		free(dp, M_IP6NDP);
1287		dp = NULL;
1288		IFAFREE(ifa);
1289		goto done;
1290	}
1291
1292	/* Need more checks? */
1293	if (dp->dad_ns_ocount < dp->dad_count) {
1294		/*
1295		 * We have more NS to go.  Send NS packet for DAD.
1296		 */
1297		nd6_dad_ns_output(dp, ifa);
1298		nd6_dad_starttimer(dp,
1299		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000);
1300	} else {
1301		/*
1302		 * We have transmitted sufficient number of DAD packets.
1303		 * See what we've got.
1304		 */
1305		int duplicate;
1306
1307		duplicate = 0;
1308
1309		if (dp->dad_na_icount) {
1310			/*
1311			 * the check is in nd6_dad_na_input(),
1312			 * but just in case
1313			 */
1314			duplicate++;
1315		}
1316
1317		if (dp->dad_ns_icount) {
1318			/* We've seen NS, means DAD has failed. */
1319			duplicate++;
1320		}
1321
1322		if (duplicate) {
1323			/* (*dp) will be freed in nd6_dad_duplicated() */
1324			dp = NULL;
1325			nd6_dad_duplicated(ifa);
1326		} else {
1327			/*
1328			 * We are done with DAD.  No NA came, no NS came.
1329			 * No duplicate address found.
1330			 */
1331			ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1332
1333			nd6log((LOG_DEBUG,
1334			    "%s: DAD complete for %s - no duplicates found\n",
1335			    if_name(ifa->ifa_ifp),
1336			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1337
1338			TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1339			free(dp, M_IP6NDP);
1340			dp = NULL;
1341			IFAFREE(ifa);
1342		}
1343	}
1344
1345done:
1346	splx(s);
1347	CURVNET_RESTORE();
1348}
1349
1350void
1351nd6_dad_duplicated(struct ifaddr *ifa)
1352{
1353	INIT_VNET_INET6(curvnet);
1354	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1355	struct ifnet *ifp;
1356	struct dadq *dp;
1357	char ip6buf[INET6_ADDRSTRLEN];
1358
1359	dp = nd6_dad_find(ifa);
1360	if (dp == NULL) {
1361		log(LOG_ERR, "nd6_dad_duplicated: DAD structure not found\n");
1362		return;
1363	}
1364
1365	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1366	    "NS in/out=%d/%d, NA in=%d\n",
1367	    if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1368	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_na_icount);
1369
1370	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1371	ia->ia6_flags |= IN6_IFF_DUPLICATED;
1372
1373	/* We are done with DAD, with duplicate address found. (failure) */
1374	nd6_dad_stoptimer(dp);
1375
1376	ifp = ifa->ifa_ifp;
1377	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1378	    if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
1379	log(LOG_ERR, "%s: manual intervention required\n",
1380	    if_name(ifp));
1381
1382	/*
1383	 * If the address is a link-local address formed from an interface
1384	 * identifier based on the hardware address which is supposed to be
1385	 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1386	 * operation on the interface SHOULD be disabled.
1387	 * [rfc2462bis-03 Section 5.4.5]
1388	 */
1389	if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1390		struct in6_addr in6;
1391
1392		/*
1393		 * To avoid over-reaction, we only apply this logic when we are
1394		 * very sure that hardware addresses are supposed to be unique.
1395		 */
1396		switch (ifp->if_type) {
1397		case IFT_ETHER:
1398		case IFT_FDDI:
1399		case IFT_ATM:
1400		case IFT_IEEE1394:
1401#ifdef IFT_IEEE80211
1402		case IFT_IEEE80211:
1403#endif
1404			in6 = ia->ia_addr.sin6_addr;
1405			if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1406			    IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1407				ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1408				log(LOG_ERR, "%s: possible hardware address "
1409				    "duplication detected, disable IPv6\n",
1410				    if_name(ifp));
1411			}
1412			break;
1413		}
1414	}
1415
1416	TAILQ_REMOVE(&V_dadq, (struct dadq *)dp, dad_list);
1417	free(dp, M_IP6NDP);
1418	dp = NULL;
1419	IFAFREE(ifa);
1420}
1421
1422static void
1423nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
1424{
1425	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1426	struct ifnet *ifp = ifa->ifa_ifp;
1427
1428	dp->dad_ns_tcount++;
1429	if ((ifp->if_flags & IFF_UP) == 0) {
1430		return;
1431	}
1432	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1433		return;
1434	}
1435
1436	dp->dad_ns_ocount++;
1437	nd6_ns_output(ifp, NULL, &ia->ia_addr.sin6_addr, NULL, 1);
1438}
1439
1440static void
1441nd6_dad_ns_input(struct ifaddr *ifa)
1442{
1443	INIT_VNET_INET6(curvnet);
1444	struct in6_ifaddr *ia;
1445	struct ifnet *ifp;
1446	const struct in6_addr *taddr6;
1447	struct dadq *dp;
1448	int duplicate;
1449
1450	if (ifa == NULL)
1451		panic("ifa == NULL in nd6_dad_ns_input");
1452
1453	ia = (struct in6_ifaddr *)ifa;
1454	ifp = ifa->ifa_ifp;
1455	taddr6 = &ia->ia_addr.sin6_addr;
1456	duplicate = 0;
1457	dp = nd6_dad_find(ifa);
1458
1459	/* Quickhack - completely ignore DAD NS packets */
1460	if (V_dad_ignore_ns) {
1461		char ip6buf[INET6_ADDRSTRLEN];
1462		nd6log((LOG_INFO,
1463		    "nd6_dad_ns_input: ignoring DAD NS packet for "
1464		    "address %s(%s)\n", ip6_sprintf(ip6buf, taddr6),
1465		    if_name(ifa->ifa_ifp)));
1466		return;
1467	}
1468
1469	/*
1470	 * if I'm yet to start DAD, someone else started using this address
1471	 * first.  I have a duplicate and you win.
1472	 */
1473	if (dp == NULL || dp->dad_ns_ocount == 0)
1474		duplicate++;
1475
1476	/* XXX more checks for loopback situation - see nd6_dad_timer too */
1477
1478	if (duplicate) {
1479		dp = NULL;	/* will be freed in nd6_dad_duplicated() */
1480		nd6_dad_duplicated(ifa);
1481	} else {
1482		/*
1483		 * not sure if I got a duplicate.
1484		 * increment ns count and see what happens.
1485		 */
1486		if (dp)
1487			dp->dad_ns_icount++;
1488	}
1489}
1490
1491static void
1492nd6_dad_na_input(struct ifaddr *ifa)
1493{
1494	struct dadq *dp;
1495
1496	if (ifa == NULL)
1497		panic("ifa == NULL in nd6_dad_na_input");
1498
1499	dp = nd6_dad_find(ifa);
1500	if (dp)
1501		dp->dad_na_icount++;
1502
1503	/* remove the address. */
1504	nd6_dad_duplicated(ifa);
1505}
1506