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