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