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