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