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