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