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