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