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