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