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