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