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