xref: /freebsd/sys/netinet6/nd6_nbr.c (revision b626f5a73a48f44a31a200291b141e1da408a2ff)
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$KAME: nd6_nbr.c,v 1.86 2002/01/21 02:33:04 jinmei Exp $
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37 #include "opt_ipsec.h"
38 #include "opt_mpath.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/libkern.h>
44 #include <sys/lock.h>
45 #include <sys/rwlock.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/time.h>
50 #include <sys/kernel.h>
51 #include <sys/errno.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
54 #include <sys/queue.h>
55 #include <sys/callout.h>
56 #include <sys/refcount.h>
57 
58 #include <net/if.h>
59 #include <net/if_types.h>
60 #include <net/if_dl.h>
61 #include <net/if_var.h>
62 #include <net/route.h>
63 #ifdef RADIX_MPATH
64 #include <net/radix_mpath.h>
65 #endif
66 #include <net/vnet.h>
67 
68 #include <netinet/in.h>
69 #include <netinet/in_var.h>
70 #include <net/if_llatbl.h>
71 #include <netinet6/in6_var.h>
72 #include <netinet6/in6_ifattach.h>
73 #include <netinet/ip6.h>
74 #include <netinet6/ip6_var.h>
75 #include <netinet6/scope6_var.h>
76 #include <netinet6/nd6.h>
77 #include <netinet/icmp6.h>
78 #include <netinet/ip_carp.h>
79 #include <netinet6/send.h>
80 
81 #define SDL(s) ((struct sockaddr_dl *)s)
82 
83 struct dadq;
84 static struct dadq *nd6_dad_find(struct ifaddr *, struct nd_opt_nonce *);
85 static void nd6_dad_add(struct dadq *dp);
86 static void nd6_dad_del(struct dadq *dp);
87 static void nd6_dad_rele(struct dadq *);
88 static void nd6_dad_starttimer(struct dadq *, int, int);
89 static void nd6_dad_stoptimer(struct dadq *);
90 static void nd6_dad_timer(struct dadq *);
91 static void nd6_dad_duplicated(struct ifaddr *, struct dadq *);
92 static void nd6_dad_ns_output(struct dadq *);
93 static void nd6_dad_ns_input(struct ifaddr *, struct nd_opt_nonce *);
94 static void nd6_dad_na_input(struct ifaddr *);
95 static void nd6_na_output_fib(struct ifnet *, const struct in6_addr *,
96     const struct in6_addr *, u_long, int, struct sockaddr *, u_int);
97 static void nd6_ns_output_fib(struct ifnet *, const struct in6_addr *,
98     const struct in6_addr *, const struct in6_addr *, uint8_t *, u_int);
99 
100 static VNET_DEFINE(int, dad_enhanced) = 1;
101 #define	V_dad_enhanced			VNET(dad_enhanced)
102 
103 SYSCTL_DECL(_net_inet6_ip6);
104 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, dad_enhanced, CTLFLAG_VNET | CTLFLAG_RW,
105     &VNET_NAME(dad_enhanced), 0,
106     "Enable Enhanced DAD, which adds a random nonce to NS messages for DAD.");
107 
108 static VNET_DEFINE(int, dad_maxtry) = 15;	/* max # of *tries* to
109 						   transmit DAD packet */
110 #define	V_dad_maxtry			VNET(dad_maxtry)
111 
112 /*
113  * Input a Neighbor Solicitation Message.
114  *
115  * Based on RFC 2461
116  * Based on RFC 2462 (duplicate address detection)
117  */
118 void
119 nd6_ns_input(struct mbuf *m, int off, int icmp6len)
120 {
121 	struct ifnet *ifp = m->m_pkthdr.rcvif;
122 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
123 	struct nd_neighbor_solicit *nd_ns;
124 	struct in6_addr saddr6 = ip6->ip6_src;
125 	struct in6_addr daddr6 = ip6->ip6_dst;
126 	struct in6_addr taddr6;
127 	struct in6_addr myaddr6;
128 	char *lladdr = NULL;
129 	struct ifaddr *ifa = NULL;
130 	int lladdrlen = 0;
131 	int anycast = 0, proxy = 0, tentative = 0;
132 	int tlladdr;
133 	int rflag;
134 	union nd_opts ndopts;
135 	struct sockaddr_dl proxydl;
136 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
137 
138 	rflag = (V_ip6_forwarding) ? ND_NA_FLAG_ROUTER : 0;
139 	if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV && V_ip6_norbit_raif)
140 		rflag = 0;
141 #ifndef PULLDOWN_TEST
142 	IP6_EXTHDR_CHECK(m, off, icmp6len,);
143 	nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
144 #else
145 	IP6_EXTHDR_GET(nd_ns, struct nd_neighbor_solicit *, m, off, icmp6len);
146 	if (nd_ns == NULL) {
147 		ICMP6STAT_INC(icp6s_tooshort);
148 		return;
149 	}
150 #endif
151 	ip6 = mtod(m, struct ip6_hdr *); /* adjust pointer for safety */
152 	taddr6 = nd_ns->nd_ns_target;
153 	if (in6_setscope(&taddr6, ifp, NULL) != 0)
154 		goto bad;
155 
156 	if (ip6->ip6_hlim != 255) {
157 		nd6log((LOG_ERR,
158 		    "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
159 		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
160 		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
161 		goto bad;
162 	}
163 
164 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
165 		/* dst has to be a solicited node multicast address. */
166 		if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
167 		    /* don't check ifindex portion */
168 		    daddr6.s6_addr32[1] == 0 &&
169 		    daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
170 		    daddr6.s6_addr8[12] == 0xff) {
171 			; /* good */
172 		} else {
173 			nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
174 			    "(wrong ip6 dst)\n"));
175 			goto bad;
176 		}
177 	} else if (!V_nd6_onlink_ns_rfc4861) {
178 		struct sockaddr_in6 src_sa6;
179 
180 		/*
181 		 * According to recent IETF discussions, it is not a good idea
182 		 * to accept a NS from an address which would not be deemed
183 		 * to be a neighbor otherwise.  This point is expected to be
184 		 * clarified in future revisions of the specification.
185 		 */
186 		bzero(&src_sa6, sizeof(src_sa6));
187 		src_sa6.sin6_family = AF_INET6;
188 		src_sa6.sin6_len = sizeof(src_sa6);
189 		src_sa6.sin6_addr = saddr6;
190 		if (nd6_is_addr_neighbor(&src_sa6, ifp) == 0) {
191 			nd6log((LOG_INFO, "nd6_ns_input: "
192 				"NS packet from non-neighbor\n"));
193 			goto bad;
194 		}
195 	}
196 
197 	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
198 		nd6log((LOG_INFO, "nd6_ns_input: bad NS target (multicast)\n"));
199 		goto bad;
200 	}
201 
202 	icmp6len -= sizeof(*nd_ns);
203 	nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
204 	if (nd6_options(&ndopts) < 0) {
205 		nd6log((LOG_INFO,
206 		    "nd6_ns_input: invalid ND option, ignored\n"));
207 		/* nd6_options have incremented stats */
208 		goto freeit;
209 	}
210 
211 	if (ndopts.nd_opts_src_lladdr) {
212 		lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1);
213 		lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3;
214 	}
215 
216 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) && lladdr) {
217 		nd6log((LOG_INFO, "nd6_ns_input: bad DAD packet "
218 		    "(link-layer address option)\n"));
219 		goto bad;
220 	}
221 
222 	/*
223 	 * Attaching target link-layer address to the NA?
224 	 * (RFC 2461 7.2.4)
225 	 *
226 	 * NS IP dst is unicast/anycast			MUST NOT add
227 	 * NS IP dst is solicited-node multicast	MUST add
228 	 *
229 	 * In implementation, we add target link-layer address by default.
230 	 * We do not add one in MUST NOT cases.
231 	 */
232 	if (!IN6_IS_ADDR_MULTICAST(&daddr6))
233 		tlladdr = 0;
234 	else
235 		tlladdr = 1;
236 
237 	/*
238 	 * Target address (taddr6) must be either:
239 	 * (1) Valid unicast/anycast address for my receiving interface,
240 	 * (2) Unicast address for which I'm offering proxy service, or
241 	 * (3) "tentative" address on which DAD is being performed.
242 	 */
243 	/* (1) and (3) check. */
244 	if (ifp->if_carp)
245 		ifa = (*carp_iamatch6_p)(ifp, &taddr6);
246 	else
247 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
248 
249 	/* (2) check. */
250 	if (ifa == NULL) {
251 		struct sockaddr_dl rt_gateway;
252 		struct rt_addrinfo info;
253 		struct sockaddr_in6 dst6;
254 
255 		bzero(&dst6, sizeof(dst6));
256 		dst6.sin6_len = sizeof(struct sockaddr_in6);
257 		dst6.sin6_family = AF_INET6;
258 		dst6.sin6_addr = taddr6;
259 
260 		bzero(&rt_gateway, sizeof(rt_gateway));
261 		rt_gateway.sdl_len = sizeof(rt_gateway);
262 		bzero(&info, sizeof(info));
263 		info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&rt_gateway;
264 
265 		/* Always use the default FIB. */
266 		if (rib_lookup_info(RT_DEFAULT_FIB, (struct sockaddr *)&dst6,
267 		    0, 0, &info) == 0) {
268 			if ((info.rti_flags & RTF_ANNOUNCE) != 0 &&
269 			    rt_gateway.sdl_family == AF_LINK) {
270 
271 				/*
272 				 * proxy NDP for single entry
273 				 */
274 				proxydl = *SDL(&rt_gateway);
275 				ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(
276 				    ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST);
277 				if (ifa)
278 					proxy = 1;
279 			}
280 		}
281 	}
282 	if (ifa == NULL) {
283 		/*
284 		 * We've got an NS packet, and we don't have that adddress
285 		 * assigned for us.  We MUST silently ignore it.
286 		 * See RFC2461 7.2.3.
287 		 */
288 		goto freeit;
289 	}
290 	myaddr6 = *IFA_IN6(ifa);
291 	anycast = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST;
292 	tentative = ((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE;
293 	if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DUPLICATED)
294 		goto freeit;
295 
296 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
297 		nd6log((LOG_INFO, "nd6_ns_input: lladdrlen mismatch for %s "
298 		    "(if %d, NS packet %d)\n",
299 		    ip6_sprintf(ip6bufs, &taddr6),
300 		    ifp->if_addrlen, lladdrlen - 2));
301 		goto bad;
302 	}
303 
304 	if (IN6_ARE_ADDR_EQUAL(&myaddr6, &saddr6)) {
305 		nd6log((LOG_INFO, "nd6_ns_input: duplicate IP6 address %s\n",
306 		    ip6_sprintf(ip6bufs, &saddr6)));
307 		goto freeit;
308 	}
309 
310 	/*
311 	 * We have neighbor solicitation packet, with target address equals to
312 	 * one of my tentative address.
313 	 *
314 	 * src addr	how to process?
315 	 * ---		---
316 	 * multicast	of course, invalid (rejected in ip6_input)
317 	 * unicast	somebody is doing address resolution -> ignore
318 	 * unspec	dup address detection
319 	 *
320 	 * The processing is defined in RFC 2462.
321 	 */
322 	if (tentative) {
323 		/*
324 		 * If source address is unspecified address, it is for
325 		 * duplicate address detection.
326 		 *
327 		 * If not, the packet is for addess resolution;
328 		 * silently ignore it.
329 		 */
330 		if (IN6_IS_ADDR_UNSPECIFIED(&saddr6))
331 			nd6_dad_ns_input(ifa, ndopts.nd_opts_nonce);
332 
333 		goto freeit;
334 	}
335 
336 	/*
337 	 * If the source address is unspecified address, entries must not
338 	 * be created or updated.
339 	 * It looks that sender is performing DAD.  Output NA toward
340 	 * all-node multicast address, to tell the sender that I'm using
341 	 * the address.
342 	 * S bit ("solicited") must be zero.
343 	 */
344 	if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) {
345 		struct in6_addr in6_all;
346 
347 		in6_all = in6addr_linklocal_allnodes;
348 		if (in6_setscope(&in6_all, ifp, NULL) != 0)
349 			goto bad;
350 		nd6_na_output_fib(ifp, &in6_all, &taddr6,
351 		    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
352 		    rflag, tlladdr, proxy ? (struct sockaddr *)&proxydl : NULL,
353 		    M_GETFIB(m));
354 		goto freeit;
355 	}
356 
357 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
358 	    ND_NEIGHBOR_SOLICIT, 0);
359 
360 	nd6_na_output_fib(ifp, &saddr6, &taddr6,
361 	    ((anycast || proxy || !tlladdr) ? 0 : ND_NA_FLAG_OVERRIDE) |
362 	    rflag | ND_NA_FLAG_SOLICITED, tlladdr,
363 	    proxy ? (struct sockaddr *)&proxydl : NULL, M_GETFIB(m));
364  freeit:
365 	if (ifa != NULL)
366 		ifa_free(ifa);
367 	m_freem(m);
368 	return;
369 
370  bad:
371 	nd6log((LOG_ERR, "nd6_ns_input: src=%s\n",
372 		ip6_sprintf(ip6bufs, &saddr6)));
373 	nd6log((LOG_ERR, "nd6_ns_input: dst=%s\n",
374 		ip6_sprintf(ip6bufs, &daddr6)));
375 	nd6log((LOG_ERR, "nd6_ns_input: tgt=%s\n",
376 		ip6_sprintf(ip6bufs, &taddr6)));
377 	ICMP6STAT_INC(icp6s_badns);
378 	if (ifa != NULL)
379 		ifa_free(ifa);
380 	m_freem(m);
381 }
382 
383 /*
384  * Output a Neighbor Solicitation Message. Caller specifies:
385  *	- ICMP6 header source IP6 address
386  *	- ND6 header target IP6 address
387  *	- ND6 header source datalink address
388  *
389  * Based on RFC 2461
390  * Based on RFC 2462 (duplicate address detection)
391  *
392  *    ln - for source address determination
393  * nonce - If non-NULL, NS is used for duplicate address detection and
394  *         the value (length is ND_OPT_NONCE_LEN) is used as a random nonce.
395  */
396 static void
397 nd6_ns_output_fib(struct ifnet *ifp, const struct in6_addr *saddr6,
398     const struct in6_addr *daddr6, const struct in6_addr *taddr6,
399     uint8_t *nonce, u_int fibnum)
400 {
401 	struct mbuf *m;
402 	struct m_tag *mtag;
403 	struct ip6_hdr *ip6;
404 	struct nd_neighbor_solicit *nd_ns;
405 	struct ip6_moptions im6o;
406 	int icmp6len;
407 	int maxlen;
408 	caddr_t mac;
409 
410 	if (IN6_IS_ADDR_MULTICAST(taddr6))
411 		return;
412 
413 	/* estimate the size of message */
414 	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
415 	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
416 	KASSERT(max_linkhdr + maxlen <= MCLBYTES, (
417 	    "%s: max_linkhdr + maxlen > MCLBYTES (%d + %d > %d)",
418 	    __func__, max_linkhdr, maxlen, MCLBYTES));
419 
420 	if (max_linkhdr + maxlen > MHLEN)
421 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
422 	else
423 		m = m_gethdr(M_NOWAIT, MT_DATA);
424 	if (m == NULL)
425 		return;
426 	M_SETFIB(m, fibnum);
427 
428 	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
429 		m->m_flags |= M_MCAST;
430 		im6o.im6o_multicast_ifp = ifp;
431 		im6o.im6o_multicast_hlim = 255;
432 		im6o.im6o_multicast_loop = 0;
433 	}
434 
435 	icmp6len = sizeof(*nd_ns);
436 	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
437 	m->m_data += max_linkhdr;	/* or M_ALIGN() equivalent? */
438 
439 	/* fill neighbor solicitation packet */
440 	ip6 = mtod(m, struct ip6_hdr *);
441 	ip6->ip6_flow = 0;
442 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
443 	ip6->ip6_vfc |= IPV6_VERSION;
444 	/* ip6->ip6_plen will be set later */
445 	ip6->ip6_nxt = IPPROTO_ICMPV6;
446 	ip6->ip6_hlim = 255;
447 	if (daddr6)
448 		ip6->ip6_dst = *daddr6;
449 	else {
450 		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
451 		ip6->ip6_dst.s6_addr16[1] = 0;
452 		ip6->ip6_dst.s6_addr32[1] = 0;
453 		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
454 		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
455 		ip6->ip6_dst.s6_addr8[12] = 0xff;
456 		if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0)
457 			goto bad;
458 	}
459 	if (nonce == NULL) {
460 		struct ifaddr *ifa = NULL;
461 
462 		/*
463 		 * RFC2461 7.2.2:
464 		 * "If the source address of the packet prompting the
465 		 * solicitation is the same as one of the addresses assigned
466 		 * to the outgoing interface, that address SHOULD be placed
467 		 * in the IP Source Address of the outgoing solicitation.
468 		 * Otherwise, any one of the addresses assigned to the
469 		 * interface should be used."
470 		 *
471 		 * We use the source address for the prompting packet
472 		 * (saddr6), if saddr6 belongs to the outgoing interface.
473 		 * Otherwise, we perform the source address selection as usual.
474 		 */
475 
476 		if (saddr6 != NULL)
477 			ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, saddr6);
478 		if (ifa != NULL) {
479 			/* ip6_src set already. */
480 			ip6->ip6_src = *saddr6;
481 			ifa_free(ifa);
482 		} else {
483 			int error;
484 			struct sockaddr_in6 dst_sa;
485 			struct in6_addr src_in;
486 			struct ifnet *oifp;
487 
488 			bzero(&dst_sa, sizeof(dst_sa));
489 			dst_sa.sin6_family = AF_INET6;
490 			dst_sa.sin6_len = sizeof(dst_sa);
491 			dst_sa.sin6_addr = ip6->ip6_dst;
492 
493 			oifp = ifp;
494 			error = in6_selectsrc(&dst_sa, NULL,
495 			    NULL, NULL, &oifp, &src_in);
496 			if (error) {
497 				char ip6buf[INET6_ADDRSTRLEN];
498 				nd6log((LOG_DEBUG, "%s: source can't be "
499 				    "determined: dst=%s, error=%d\n", __func__,
500 				    ip6_sprintf(ip6buf, &dst_sa.sin6_addr),
501 				    error));
502 				goto bad;
503 			}
504 			ip6->ip6_src = src_in;
505 		}
506 	} else {
507 		/*
508 		 * Source address for DAD packet must always be IPv6
509 		 * unspecified address. (0::0)
510 		 * We actually don't have to 0-clear the address (we did it
511 		 * above), but we do so here explicitly to make the intention
512 		 * clearer.
513 		 */
514 		bzero(&ip6->ip6_src, sizeof(ip6->ip6_src));
515 	}
516 	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
517 	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
518 	nd_ns->nd_ns_code = 0;
519 	nd_ns->nd_ns_reserved = 0;
520 	nd_ns->nd_ns_target = *taddr6;
521 	in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
522 
523 	/*
524 	 * Add source link-layer address option.
525 	 *
526 	 *				spec		implementation
527 	 *				---		---
528 	 * DAD packet			MUST NOT	do not add the option
529 	 * there's no link layer address:
530 	 *				impossible	do not add the option
531 	 * there's link layer address:
532 	 *	Multicast NS		MUST add one	add the option
533 	 *	Unicast NS		SHOULD add one	add the option
534 	 */
535 	if (nonce == NULL && (mac = nd6_ifptomac(ifp))) {
536 		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
537 		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
538 		/* 8 byte alignments... */
539 		optlen = (optlen + 7) & ~7;
540 
541 		m->m_pkthdr.len += optlen;
542 		m->m_len += optlen;
543 		icmp6len += optlen;
544 		bzero((caddr_t)nd_opt, optlen);
545 		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
546 		nd_opt->nd_opt_len = optlen >> 3;
547 		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
548 	}
549 	/*
550 	 * Add a Nonce option (RFC 3971) to detect looped back NS messages.
551 	 * This behavior is documented as Enhanced Duplicate Address
552 	 * Detection in RFC 7527.
553 	 * net.inet6.ip6.dad_enhanced=0 disables this.
554 	 */
555 	if (V_dad_enhanced != 0 && nonce != NULL) {
556 		int optlen = sizeof(struct nd_opt_hdr) + ND_OPT_NONCE_LEN;
557 		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
558 		/* 8-byte alignment is required. */
559 		optlen = (optlen + 7) & ~7;
560 
561 		m->m_pkthdr.len += optlen;
562 		m->m_len += optlen;
563 		icmp6len += optlen;
564 		bzero((caddr_t)nd_opt, optlen);
565 		nd_opt->nd_opt_type = ND_OPT_NONCE;
566 		nd_opt->nd_opt_len = optlen >> 3;
567 		bcopy(nonce, (caddr_t)(nd_opt + 1), ND_OPT_NONCE_LEN);
568 	}
569 	ip6->ip6_plen = htons((u_short)icmp6len);
570 	nd_ns->nd_ns_cksum = 0;
571 	nd_ns->nd_ns_cksum =
572 	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
573 
574 	if (send_sendso_input_hook != NULL) {
575 		mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
576 			sizeof(unsigned short), M_NOWAIT);
577 		if (mtag == NULL)
578 			goto bad;
579 		*(unsigned short *)(mtag + 1) = nd_ns->nd_ns_type;
580 		m_tag_prepend(m, mtag);
581 	}
582 
583 	ip6_output(m, NULL, NULL, (nonce != NULL) ? IPV6_UNSPECSRC : 0,
584 	    &im6o, NULL, NULL);
585 	icmp6_ifstat_inc(ifp, ifs6_out_msg);
586 	icmp6_ifstat_inc(ifp, ifs6_out_neighborsolicit);
587 	ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_SOLICIT]);
588 
589 	return;
590 
591   bad:
592 	m_freem(m);
593 	return;
594 }
595 
596 #ifndef BURN_BRIDGES
597 void
598 nd6_ns_output(struct ifnet *ifp, const struct in6_addr *saddr6,
599     const struct in6_addr *daddr6, const struct in6_addr *taddr6,uint8_t *nonce)
600 {
601 
602 	nd6_ns_output_fib(ifp, saddr6, daddr6, taddr6, nonce, RT_DEFAULT_FIB);
603 }
604 #endif
605 /*
606  * Neighbor advertisement input handling.
607  *
608  * Based on RFC 2461
609  * Based on RFC 2462 (duplicate address detection)
610  *
611  * the following items are not implemented yet:
612  * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
613  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
614  */
615 void
616 nd6_na_input(struct mbuf *m, int off, int icmp6len)
617 {
618 	struct ifnet *ifp = m->m_pkthdr.rcvif;
619 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
620 	struct nd_neighbor_advert *nd_na;
621 	struct in6_addr daddr6 = ip6->ip6_dst;
622 	struct in6_addr taddr6;
623 	int flags;
624 	int is_router;
625 	int is_solicited;
626 	int is_override;
627 	char *lladdr = NULL;
628 	int lladdrlen = 0;
629 	int checklink = 0;
630 	struct ifaddr *ifa;
631 	struct llentry *ln = NULL;
632 	union nd_opts ndopts;
633 	struct mbuf *chain = NULL;
634 	struct sockaddr_in6 sin6;
635 	u_char linkhdr[LLE_MAX_LINKHDR];
636 	size_t linkhdrsize;
637 	int lladdr_off;
638 	char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
639 
640 	if (ip6->ip6_hlim != 255) {
641 		nd6log((LOG_ERR,
642 		    "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
643 		    ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src),
644 		    ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp)));
645 		goto bad;
646 	}
647 
648 #ifndef PULLDOWN_TEST
649 	IP6_EXTHDR_CHECK(m, off, icmp6len,);
650 	nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
651 #else
652 	IP6_EXTHDR_GET(nd_na, struct nd_neighbor_advert *, m, off, icmp6len);
653 	if (nd_na == NULL) {
654 		ICMP6STAT_INC(icp6s_tooshort);
655 		return;
656 	}
657 #endif
658 
659 	flags = nd_na->nd_na_flags_reserved;
660 	is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
661 	is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
662 	is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
663 	memset(&sin6, 0, sizeof(sin6));
664 
665 	taddr6 = nd_na->nd_na_target;
666 	if (in6_setscope(&taddr6, ifp, NULL))
667 		goto bad;	/* XXX: impossible */
668 
669 	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
670 		nd6log((LOG_ERR,
671 		    "nd6_na_input: invalid target address %s\n",
672 		    ip6_sprintf(ip6bufs, &taddr6)));
673 		goto bad;
674 	}
675 	if (IN6_IS_ADDR_MULTICAST(&daddr6))
676 		if (is_solicited) {
677 			nd6log((LOG_ERR,
678 			    "nd6_na_input: a solicited adv is multicasted\n"));
679 			goto bad;
680 		}
681 
682 	icmp6len -= sizeof(*nd_na);
683 	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
684 	if (nd6_options(&ndopts) < 0) {
685 		nd6log((LOG_INFO,
686 		    "nd6_na_input: invalid ND option, ignored\n"));
687 		/* nd6_options have incremented stats */
688 		goto freeit;
689 	}
690 
691 	if (ndopts.nd_opts_tgt_lladdr) {
692 		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
693 		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
694 	}
695 
696 	/*
697 	 * This effectively disables the DAD check on a non-master CARP
698 	 * address.
699 	 */
700 	if (ifp->if_carp)
701 		ifa = (*carp_iamatch6_p)(ifp, &taddr6);
702 	else
703 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
704 
705 	/*
706 	 * Target address matches one of my interface address.
707 	 *
708 	 * If my address is tentative, this means that there's somebody
709 	 * already using the same address as mine.  This indicates DAD failure.
710 	 * This is defined in RFC 2462.
711 	 *
712 	 * Otherwise, process as defined in RFC 2461.
713 	 */
714 	if (ifa
715 	 && (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_TENTATIVE)) {
716 		nd6_dad_na_input(ifa);
717 		ifa_free(ifa);
718 		goto freeit;
719 	}
720 
721 	/* Just for safety, maybe unnecessary. */
722 	if (ifa) {
723 		ifa_free(ifa);
724 		log(LOG_ERR,
725 		    "nd6_na_input: duplicate IP6 address %s\n",
726 		    ip6_sprintf(ip6bufs, &taddr6));
727 		goto freeit;
728 	}
729 
730 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
731 		nd6log((LOG_INFO, "nd6_na_input: lladdrlen mismatch for %s "
732 		    "(if %d, NA packet %d)\n", ip6_sprintf(ip6bufs, &taddr6),
733 		    ifp->if_addrlen, lladdrlen - 2));
734 		goto bad;
735 	}
736 
737 	/*
738 	 * If no neighbor cache entry is found, NA SHOULD silently be
739 	 * discarded.
740 	 */
741 	IF_AFDATA_RLOCK(ifp);
742 	ln = nd6_lookup(&taddr6, LLE_EXCLUSIVE, ifp);
743 	IF_AFDATA_RUNLOCK(ifp);
744 	if (ln == NULL) {
745 		goto freeit;
746 	}
747 
748 	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
749 		/*
750 		 * If the link-layer has address, and no lladdr option came,
751 		 * discard the packet.
752 		 */
753 		if (ifp->if_addrlen && lladdr == NULL) {
754 			goto freeit;
755 		}
756 
757 		/*
758 		 * Record link-layer address, and update the state.
759 		 */
760 		linkhdrsize = sizeof(linkhdr);
761 		if (lltable_calc_llheader(ifp, AF_INET6, lladdr,
762 		    linkhdr, &linkhdrsize, &lladdr_off) != 0)
763 			return;
764 
765 		if (lltable_try_set_entry_addr(ifp, ln, linkhdr, linkhdrsize,
766 		    lladdr_off) == 0) {
767 			ln = NULL;
768 			goto freeit;
769 		}
770 		EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
771 		if (is_solicited)
772 			nd6_llinfo_setstate(ln, ND6_LLINFO_REACHABLE);
773 		else
774 			nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
775 		if ((ln->ln_router = is_router) != 0) {
776 			/*
777 			 * This means a router's state has changed from
778 			 * non-reachable to probably reachable, and might
779 			 * affect the status of associated prefixes..
780 			 */
781 			checklink = 1;
782 		}
783 	} else {
784 		int llchange;
785 
786 		/*
787 		 * Check if the link-layer address has changed or not.
788 		 */
789 		if (lladdr == NULL)
790 			llchange = 0;
791 		else {
792 			if (ln->la_flags & LLE_VALID) {
793 				if (bcmp(lladdr, ln->ll_addr, ifp->if_addrlen))
794 					llchange = 1;
795 				else
796 					llchange = 0;
797 			} else
798 				llchange = 1;
799 		}
800 
801 		/*
802 		 * This is VERY complex.  Look at it with care.
803 		 *
804 		 * override solicit lladdr llchange	action
805 		 *					(L: record lladdr)
806 		 *
807 		 *	0	0	n	--	(2c)
808 		 *	0	0	y	n	(2b) L
809 		 *	0	0	y	y	(1)    REACHABLE->STALE
810 		 *	0	1	n	--	(2c)   *->REACHABLE
811 		 *	0	1	y	n	(2b) L *->REACHABLE
812 		 *	0	1	y	y	(1)    REACHABLE->STALE
813 		 *	1	0	n	--	(2a)
814 		 *	1	0	y	n	(2a) L
815 		 *	1	0	y	y	(2a) L *->STALE
816 		 *	1	1	n	--	(2a)   *->REACHABLE
817 		 *	1	1	y	n	(2a) L *->REACHABLE
818 		 *	1	1	y	y	(2a) L *->REACHABLE
819 		 */
820 		if (!is_override && (lladdr != NULL && llchange)) {  /* (1) */
821 			/*
822 			 * If state is REACHABLE, make it STALE.
823 			 * no other updates should be done.
824 			 */
825 			if (ln->ln_state == ND6_LLINFO_REACHABLE)
826 				nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
827 			goto freeit;
828 		} else if (is_override				   /* (2a) */
829 			|| (!is_override && (lladdr != NULL && !llchange)) /* (2b) */
830 			|| lladdr == NULL) {			   /* (2c) */
831 			/*
832 			 * Update link-local address, if any.
833 			 */
834 			if (lladdr != NULL) {
835 				linkhdrsize = sizeof(linkhdr);
836 				if (lltable_calc_llheader(ifp, AF_INET6, lladdr,
837 				    linkhdr, &linkhdrsize, &lladdr_off) != 0)
838 					goto freeit;
839 				if (lltable_try_set_entry_addr(ifp, ln, linkhdr,
840 				    linkhdrsize, lladdr_off) == 0) {
841 					ln = NULL;
842 					goto freeit;
843 				}
844 				EVENTHANDLER_INVOKE(lle_event, ln,
845 				    LLENTRY_RESOLVED);
846 			}
847 
848 			/*
849 			 * If solicited, make the state REACHABLE.
850 			 * If not solicited and the link-layer address was
851 			 * changed, make it STALE.
852 			 */
853 			if (is_solicited)
854 				nd6_llinfo_setstate(ln, ND6_LLINFO_REACHABLE);
855 			else {
856 				if (lladdr != NULL && llchange)
857 					nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
858 			}
859 		}
860 
861 		if (ln->ln_router && !is_router) {
862 			/*
863 			 * The peer dropped the router flag.
864 			 * Remove the sender from the Default Router List and
865 			 * update the Destination Cache entries.
866 			 */
867 			struct nd_defrouter *dr;
868 			struct in6_addr *in6;
869 			struct ifnet *nd6_ifp;
870 
871 			in6 = &ln->r_l3addr.addr6;
872 
873 			/*
874 			 * Lock to protect the default router list.
875 			 * XXX: this might be unnecessary, since this function
876 			 * is only called under the network software interrupt
877 			 * context.  However, we keep it just for safety.
878 			 */
879 			nd6_ifp = lltable_get_ifp(ln->lle_tbl);
880 			dr = defrouter_lookup(in6, nd6_ifp);
881 			if (dr)
882 				defrtrlist_del(dr);
883 			else if (ND_IFINFO(nd6_ifp)->flags &
884 			    ND6_IFF_ACCEPT_RTADV) {
885 				/*
886 				 * Even if the neighbor is not in the default
887 				 * router list, the neighbor may be used
888 				 * as a next hop for some destinations
889 				 * (e.g. redirect case). So we must
890 				 * call rt6_flush explicitly.
891 				 */
892 				rt6_flush(&ip6->ip6_src, ifp);
893 			}
894 		}
895 		ln->ln_router = is_router;
896 	}
897         /* XXX - QL
898 	 *  Does this matter?
899 	 *  rt->rt_flags &= ~RTF_REJECT;
900 	 */
901 	ln->la_asked = 0;
902 	if (ln->la_hold != NULL)
903 		nd6_grab_holdchain(ln, &chain, &sin6);
904  freeit:
905 	if (ln != NULL)
906 		LLE_WUNLOCK(ln);
907 
908 	if (chain != NULL)
909 		nd6_flush_holdchain(ifp, ifp, chain, &sin6);
910 
911 	if (checklink)
912 		pfxlist_onlink_check();
913 
914 	m_freem(m);
915 	return;
916 
917  bad:
918 	if (ln != NULL)
919 		LLE_WUNLOCK(ln);
920 
921 	ICMP6STAT_INC(icp6s_badna);
922 	m_freem(m);
923 }
924 
925 /*
926  * Neighbor advertisement output handling.
927  *
928  * Based on RFC 2461
929  *
930  * the following items are not implemented yet:
931  * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
932  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
933  *
934  * tlladdr - 1 if include target link-layer address
935  * sdl0 - sockaddr_dl (= proxy NA) or NULL
936  */
937 static void
938 nd6_na_output_fib(struct ifnet *ifp, const struct in6_addr *daddr6_0,
939     const struct in6_addr *taddr6, u_long flags, int tlladdr,
940     struct sockaddr *sdl0, u_int fibnum)
941 {
942 	struct mbuf *m;
943 	struct m_tag *mtag;
944 	struct ifnet *oifp;
945 	struct ip6_hdr *ip6;
946 	struct nd_neighbor_advert *nd_na;
947 	struct ip6_moptions im6o;
948 	struct in6_addr src, daddr6;
949 	struct sockaddr_in6 dst_sa;
950 	int icmp6len, maxlen, error;
951 	caddr_t mac = NULL;
952 
953 	daddr6 = *daddr6_0;	/* make a local copy for modification */
954 
955 	/* estimate the size of message */
956 	maxlen = sizeof(*ip6) + sizeof(*nd_na);
957 	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
958 	KASSERT(max_linkhdr + maxlen <= MCLBYTES, (
959 	    "%s: max_linkhdr + maxlen > MCLBYTES (%d + %d > %d)",
960 	    __func__, max_linkhdr, maxlen, MCLBYTES));
961 
962 	if (max_linkhdr + maxlen > MHLEN)
963 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
964 	else
965 		m = m_gethdr(M_NOWAIT, MT_DATA);
966 	if (m == NULL)
967 		return;
968 	M_SETFIB(m, fibnum);
969 
970 	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
971 		m->m_flags |= M_MCAST;
972 		im6o.im6o_multicast_ifp = ifp;
973 		im6o.im6o_multicast_hlim = 255;
974 		im6o.im6o_multicast_loop = 0;
975 	}
976 
977 	icmp6len = sizeof(*nd_na);
978 	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
979 	m->m_data += max_linkhdr;	/* or M_ALIGN() equivalent? */
980 
981 	/* fill neighbor advertisement packet */
982 	ip6 = mtod(m, struct ip6_hdr *);
983 	ip6->ip6_flow = 0;
984 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
985 	ip6->ip6_vfc |= IPV6_VERSION;
986 	ip6->ip6_nxt = IPPROTO_ICMPV6;
987 	ip6->ip6_hlim = 255;
988 	if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
989 		/* reply to DAD */
990 		daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
991 		daddr6.s6_addr16[1] = 0;
992 		daddr6.s6_addr32[1] = 0;
993 		daddr6.s6_addr32[2] = 0;
994 		daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
995 		if (in6_setscope(&daddr6, ifp, NULL))
996 			goto bad;
997 
998 		flags &= ~ND_NA_FLAG_SOLICITED;
999 	}
1000 	ip6->ip6_dst = daddr6;
1001 	bzero(&dst_sa, sizeof(struct sockaddr_in6));
1002 	dst_sa.sin6_family = AF_INET6;
1003 	dst_sa.sin6_len = sizeof(struct sockaddr_in6);
1004 	dst_sa.sin6_addr = daddr6;
1005 
1006 	/*
1007 	 * Select a source whose scope is the same as that of the dest.
1008 	 */
1009 	oifp = ifp;
1010 	error = in6_selectsrc(&dst_sa, NULL, NULL, NULL, &oifp, &src);
1011 	if (error) {
1012 		char ip6buf[INET6_ADDRSTRLEN];
1013 		nd6log((LOG_DEBUG, "nd6_na_output: source can't be "
1014 		    "determined: dst=%s, error=%d\n",
1015 		    ip6_sprintf(ip6buf, &dst_sa.sin6_addr), error));
1016 		goto bad;
1017 	}
1018 	ip6->ip6_src = src;
1019 	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
1020 	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
1021 	nd_na->nd_na_code = 0;
1022 	nd_na->nd_na_target = *taddr6;
1023 	in6_clearscope(&nd_na->nd_na_target); /* XXX */
1024 
1025 	/*
1026 	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
1027 	 * see nd6_ns_input() for details.
1028 	 * Basically, if NS packet is sent to unicast/anycast addr,
1029 	 * target lladdr option SHOULD NOT be included.
1030 	 */
1031 	if (tlladdr) {
1032 		/*
1033 		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
1034 		 * lladdr in sdl0.  If we are not proxying (sending NA for
1035 		 * my address) use lladdr configured for the interface.
1036 		 */
1037 		if (sdl0 == NULL) {
1038 			if (ifp->if_carp)
1039 				mac = (*carp_macmatch6_p)(ifp, m, taddr6);
1040 			if (mac == NULL)
1041 				mac = nd6_ifptomac(ifp);
1042 		} else if (sdl0->sa_family == AF_LINK) {
1043 			struct sockaddr_dl *sdl;
1044 			sdl = (struct sockaddr_dl *)sdl0;
1045 			if (sdl->sdl_alen == ifp->if_addrlen)
1046 				mac = LLADDR(sdl);
1047 		}
1048 	}
1049 	if (tlladdr && mac) {
1050 		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1051 		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1052 
1053 		/* roundup to 8 bytes alignment! */
1054 		optlen = (optlen + 7) & ~7;
1055 
1056 		m->m_pkthdr.len += optlen;
1057 		m->m_len += optlen;
1058 		icmp6len += optlen;
1059 		bzero((caddr_t)nd_opt, optlen);
1060 		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1061 		nd_opt->nd_opt_len = optlen >> 3;
1062 		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
1063 	} else
1064 		flags &= ~ND_NA_FLAG_OVERRIDE;
1065 
1066 	ip6->ip6_plen = htons((u_short)icmp6len);
1067 	nd_na->nd_na_flags_reserved = flags;
1068 	nd_na->nd_na_cksum = 0;
1069 	nd_na->nd_na_cksum =
1070 	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1071 
1072 	if (send_sendso_input_hook != NULL) {
1073 		mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
1074 		    sizeof(unsigned short), M_NOWAIT);
1075 		if (mtag == NULL)
1076 			goto bad;
1077 		*(unsigned short *)(mtag + 1) = nd_na->nd_na_type;
1078 		m_tag_prepend(m, mtag);
1079 	}
1080 
1081 	ip6_output(m, NULL, NULL, 0, &im6o, NULL, NULL);
1082 	icmp6_ifstat_inc(ifp, ifs6_out_msg);
1083 	icmp6_ifstat_inc(ifp, ifs6_out_neighboradvert);
1084 	ICMP6STAT_INC(icp6s_outhist[ND_NEIGHBOR_ADVERT]);
1085 
1086 	return;
1087 
1088   bad:
1089 	m_freem(m);
1090 	return;
1091 }
1092 
1093 #ifndef BURN_BRIDGES
1094 void
1095 nd6_na_output(struct ifnet *ifp, const struct in6_addr *daddr6_0,
1096     const struct in6_addr *taddr6, u_long flags, int tlladdr,
1097     struct sockaddr *sdl0)
1098 {
1099 
1100 	nd6_na_output_fib(ifp, daddr6_0, taddr6, flags, tlladdr, sdl0,
1101 	    RT_DEFAULT_FIB);
1102 }
1103 #endif
1104 
1105 caddr_t
1106 nd6_ifptomac(struct ifnet *ifp)
1107 {
1108 	switch (ifp->if_type) {
1109 	case IFT_ARCNET:
1110 	case IFT_ETHER:
1111 	case IFT_FDDI:
1112 	case IFT_IEEE1394:
1113 	case IFT_L2VLAN:
1114 	case IFT_IEEE80211:
1115 	case IFT_INFINIBAND:
1116 	case IFT_BRIDGE:
1117 	case IFT_ISO88025:
1118 		return IF_LLADDR(ifp);
1119 	default:
1120 		return NULL;
1121 	}
1122 }
1123 
1124 struct dadq {
1125 	TAILQ_ENTRY(dadq) dad_list;
1126 	struct ifaddr *dad_ifa;
1127 	int dad_count;		/* max NS to send */
1128 	int dad_ns_tcount;	/* # of trials to send NS */
1129 	int dad_ns_ocount;	/* NS sent so far */
1130 	int dad_ns_icount;
1131 	int dad_na_icount;
1132 	int dad_ns_lcount;	/* looped back NS */
1133 	int dad_loopbackprobe;	/* probing state for loopback detection */
1134 	struct callout dad_timer_ch;
1135 	struct vnet *dad_vnet;
1136 	u_int dad_refcnt;
1137 #define	ND_OPT_NONCE_LEN32 \
1138 		((ND_OPT_NONCE_LEN + sizeof(uint32_t) - 1)/sizeof(uint32_t))
1139 	uint32_t dad_nonce[ND_OPT_NONCE_LEN32];
1140 };
1141 
1142 static VNET_DEFINE(TAILQ_HEAD(, dadq), dadq);
1143 static VNET_DEFINE(struct rwlock, dad_rwlock);
1144 #define	V_dadq			VNET(dadq)
1145 #define	V_dad_rwlock		VNET(dad_rwlock)
1146 
1147 #define	DADQ_RLOCK()		rw_rlock(&V_dad_rwlock)
1148 #define	DADQ_RUNLOCK()		rw_runlock(&V_dad_rwlock)
1149 #define	DADQ_WLOCK()		rw_wlock(&V_dad_rwlock)
1150 #define	DADQ_WUNLOCK()		rw_wunlock(&V_dad_rwlock)
1151 
1152 static void
1153 nd6_dad_add(struct dadq *dp)
1154 {
1155 
1156 	DADQ_WLOCK();
1157 	TAILQ_INSERT_TAIL(&V_dadq, dp, dad_list);
1158 	DADQ_WUNLOCK();
1159 }
1160 
1161 static void
1162 nd6_dad_del(struct dadq *dp)
1163 {
1164 
1165 	DADQ_WLOCK();
1166 	TAILQ_REMOVE(&V_dadq, dp, dad_list);
1167 	DADQ_WUNLOCK();
1168 	nd6_dad_rele(dp);
1169 }
1170 
1171 static struct dadq *
1172 nd6_dad_find(struct ifaddr *ifa, struct nd_opt_nonce *n)
1173 {
1174 	struct dadq *dp;
1175 
1176 	DADQ_RLOCK();
1177 	TAILQ_FOREACH(dp, &V_dadq, dad_list) {
1178 		if (dp->dad_ifa != ifa)
1179 			continue;
1180 		/*
1181 		 * Skip if the nonce matches the received one.
1182 		 * +2 in the length is required because of type and
1183 		 * length fields are included in a header.
1184 		 */
1185 		if (n != NULL &&
1186 		    n->nd_opt_nonce_len == (ND_OPT_NONCE_LEN + 2) / 8 &&
1187 		    memcmp(&n->nd_opt_nonce[0], &dp->dad_nonce[0],
1188 		        ND_OPT_NONCE_LEN) == 0) {
1189 			dp->dad_ns_lcount++;
1190 			continue;
1191 		}
1192 		refcount_acquire(&dp->dad_refcnt);
1193 		break;
1194 	}
1195 	DADQ_RUNLOCK();
1196 
1197 	return (dp);
1198 }
1199 
1200 static void
1201 nd6_dad_starttimer(struct dadq *dp, int ticks, int send_ns)
1202 {
1203 
1204 	if (send_ns != 0)
1205 		nd6_dad_ns_output(dp);
1206 	callout_reset(&dp->dad_timer_ch, ticks,
1207 	    (void (*)(void *))nd6_dad_timer, (void *)dp);
1208 }
1209 
1210 static void
1211 nd6_dad_stoptimer(struct dadq *dp)
1212 {
1213 
1214 	callout_drain(&dp->dad_timer_ch);
1215 }
1216 
1217 static void
1218 nd6_dad_rele(struct dadq *dp)
1219 {
1220 
1221 	if (refcount_release(&dp->dad_refcnt)) {
1222 		ifa_free(dp->dad_ifa);
1223 		free(dp, M_IP6NDP);
1224 	}
1225 }
1226 
1227 void
1228 nd6_dad_init(void)
1229 {
1230 
1231 	rw_init(&V_dad_rwlock, "nd6 DAD queue");
1232 	TAILQ_INIT(&V_dadq);
1233 }
1234 
1235 /*
1236  * Start Duplicate Address Detection (DAD) for specified interface address.
1237  */
1238 void
1239 nd6_dad_start(struct ifaddr *ifa, int delay)
1240 {
1241 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1242 	struct dadq *dp;
1243 	char ip6buf[INET6_ADDRSTRLEN];
1244 	int send_ns;
1245 
1246 	/*
1247 	 * If we don't need DAD, don't do it.
1248 	 * There are several cases:
1249 	 * - DAD is disabled (ip6_dad_count == 0)
1250 	 * - the interface address is anycast
1251 	 */
1252 	if (!(ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1253 		log(LOG_DEBUG,
1254 			"nd6_dad_start: called with non-tentative address "
1255 			"%s(%s)\n",
1256 			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1257 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1258 		return;
1259 	}
1260 	if (ia->ia6_flags & IN6_IFF_ANYCAST) {
1261 		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1262 		return;
1263 	}
1264 	if (!V_ip6_dad_count) {
1265 		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1266 		return;
1267 	}
1268 	if (ifa->ifa_ifp == NULL)
1269 		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1270 	if (ND_IFINFO(ifa->ifa_ifp)->flags & ND6_IFF_NO_DAD) {
1271 		ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1272 		return;
1273 	}
1274 	if (!(ifa->ifa_ifp->if_flags & IFF_UP) ||
1275 	    !(ifa->ifa_ifp->if_drv_flags & IFF_DRV_RUNNING) ||
1276 	    (ND_IFINFO(ifa->ifa_ifp)->flags & ND6_IFF_IFDISABLED)) {
1277 		ia->ia6_flags |= IN6_IFF_TENTATIVE;
1278 		return;
1279 	}
1280 	if ((dp = nd6_dad_find(ifa, NULL)) != NULL) {
1281 		/*
1282 		 * DAD already in progress.  Let the existing entry
1283 		 * to finish it.
1284 		 */
1285 		return;
1286 	}
1287 
1288 	dp = malloc(sizeof(*dp), M_IP6NDP, M_NOWAIT | M_ZERO);
1289 	if (dp == NULL) {
1290 		log(LOG_ERR, "nd6_dad_start: memory allocation failed for "
1291 			"%s(%s)\n",
1292 			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1293 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1294 		return;
1295 	}
1296 	callout_init(&dp->dad_timer_ch, 0);
1297 #ifdef VIMAGE
1298 	dp->dad_vnet = curvnet;
1299 #endif
1300 	nd6log((LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1301 	    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1302 
1303 	/*
1304 	 * Send NS packet for DAD, ip6_dad_count times.
1305 	 * Note that we must delay the first transmission, if this is the
1306 	 * first packet to be sent from the interface after interface
1307 	 * (re)initialization.
1308 	 */
1309 	dp->dad_ifa = ifa;
1310 	ifa_ref(dp->dad_ifa);
1311 	dp->dad_count = V_ip6_dad_count;
1312 	dp->dad_ns_icount = dp->dad_na_icount = 0;
1313 	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1314 	dp->dad_ns_lcount = dp->dad_loopbackprobe = 0;
1315 	refcount_init(&dp->dad_refcnt, 1);
1316 	nd6_dad_add(dp);
1317 	send_ns = 0;
1318 	if (delay == 0) {
1319 		send_ns = 1;
1320 		delay = (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000;
1321 	}
1322 	nd6_dad_starttimer(dp, delay, send_ns);
1323 }
1324 
1325 /*
1326  * terminate DAD unconditionally.  used for address removals.
1327  */
1328 void
1329 nd6_dad_stop(struct ifaddr *ifa)
1330 {
1331 	struct dadq *dp;
1332 
1333 	dp = nd6_dad_find(ifa, NULL);
1334 	if (!dp) {
1335 		/* DAD wasn't started yet */
1336 		return;
1337 	}
1338 
1339 	nd6_dad_stoptimer(dp);
1340 
1341 	/*
1342 	 * The DAD queue entry may have been removed by nd6_dad_timer() while
1343 	 * we were waiting for it to stop, so re-do the lookup.
1344 	 */
1345 	nd6_dad_rele(dp);
1346 	if (nd6_dad_find(ifa, NULL) == NULL)
1347 		return;
1348 
1349 	nd6_dad_del(dp);
1350 	nd6_dad_rele(dp);
1351 }
1352 
1353 static void
1354 nd6_dad_timer(struct dadq *dp)
1355 {
1356 	CURVNET_SET(dp->dad_vnet);
1357 	struct ifaddr *ifa = dp->dad_ifa;
1358 	struct ifnet *ifp = dp->dad_ifa->ifa_ifp;
1359 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1360 	char ip6buf[INET6_ADDRSTRLEN];
1361 
1362 	/* Sanity check */
1363 	if (ia == NULL) {
1364 		log(LOG_ERR, "nd6_dad_timer: called with null parameter\n");
1365 		goto err;
1366 	}
1367 	if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
1368 		/* Do not need DAD for ifdisabled interface. */
1369 		log(LOG_ERR, "nd6_dad_timer: cancel DAD on %s because of "
1370 		    "ND6_IFF_IFDISABLED.\n", ifp->if_xname);
1371 		goto err;
1372 	}
1373 	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1374 		log(LOG_ERR, "nd6_dad_timer: called with duplicated address "
1375 			"%s(%s)\n",
1376 			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1377 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1378 		goto err;
1379 	}
1380 	if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) {
1381 		log(LOG_ERR, "nd6_dad_timer: called with non-tentative address "
1382 			"%s(%s)\n",
1383 			ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1384 			ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1385 		goto err;
1386 	}
1387 
1388 	/* Stop DAD if the interface is down even after dad_maxtry attempts. */
1389 	if ((dp->dad_ns_tcount > V_dad_maxtry) &&
1390 	    (((ifp->if_flags & IFF_UP) == 0) ||
1391 	     ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0))) {
1392 		nd6log((LOG_INFO, "%s: could not run DAD "
1393 		    "because the interface was down or not running.\n",
1394 		    if_name(ifa->ifa_ifp)));
1395 		goto err;
1396 	}
1397 
1398 	/* Need more checks? */
1399 	if (dp->dad_ns_ocount < dp->dad_count) {
1400 		/*
1401 		 * We have more NS to go.  Send NS packet for DAD.
1402 		 */
1403 		nd6_dad_starttimer(dp,
1404 		    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000, 1);
1405 		goto done;
1406 	} else {
1407 		/*
1408 		 * We have transmitted sufficient number of DAD packets.
1409 		 * See what we've got.
1410 		 */
1411 		if (dp->dad_ns_icount > 0 || dp->dad_na_icount > 0)
1412 			/* We've seen NS or NA, means DAD has failed. */
1413 			nd6_dad_duplicated(ifa, dp);
1414 		else if (V_dad_enhanced != 0 &&
1415 		    dp->dad_ns_lcount > 0 &&
1416 		    dp->dad_ns_lcount > dp->dad_loopbackprobe) {
1417 			/*
1418 			 * Sec. 4.1 in RFC 7527 requires transmission of
1419 			 * additional probes until the loopback condition
1420 			 * becomes clear when a looped back probe is detected.
1421 			 */
1422 			log(LOG_ERR, "%s: a looped back NS message is "
1423 			    "detected during DAD for %s.  "
1424 			    "Another DAD probes are being sent.\n",
1425 			    if_name(ifa->ifa_ifp),
1426 			    ip6_sprintf(ip6buf, IFA_IN6(ifa)));
1427 			dp->dad_loopbackprobe = dp->dad_ns_lcount;
1428 			/*
1429 			 * Send an NS immediately and increase dad_count by
1430 			 * V_nd6_mmaxtries - 1.
1431 			 */
1432 			dp->dad_count =
1433 			    dp->dad_ns_ocount + V_nd6_mmaxtries - 1;
1434 			nd6_dad_starttimer(dp,
1435 			    (long)ND_IFINFO(ifa->ifa_ifp)->retrans * hz / 1000,
1436 			    1);
1437 			goto done;
1438 		} else {
1439 			/*
1440 			 * We are done with DAD.  No NA came, no NS came.
1441 			 * No duplicate address found.  Check IFDISABLED flag
1442 			 * again in case that it is changed between the
1443 			 * beginning of this function and here.
1444 			 */
1445 			if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) == 0)
1446 				ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1447 
1448 			nd6log((LOG_DEBUG,
1449 			    "%s: DAD complete for %s - no duplicates found\n",
1450 			    if_name(ifa->ifa_ifp),
1451 			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1452 			if (dp->dad_ns_lcount > 0)
1453 				log(LOG_ERR, "%s: DAD completed while "
1454 				    "a looped back NS message is detected "
1455 				    "during DAD for %s.\n",
1456 				    if_name(ifa->ifa_ifp),
1457 				    ip6_sprintf(ip6buf, IFA_IN6(ifa)));
1458 		}
1459 	}
1460 err:
1461 	nd6_dad_del(dp);
1462 done:
1463 	CURVNET_RESTORE();
1464 }
1465 
1466 static void
1467 nd6_dad_duplicated(struct ifaddr *ifa, struct dadq *dp)
1468 {
1469 	struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1470 	struct ifnet *ifp;
1471 	char ip6buf[INET6_ADDRSTRLEN];
1472 
1473 	log(LOG_ERR, "%s: DAD detected duplicate IPv6 address %s: "
1474 	    "NS in/out/loopback=%d/%d/%d, NA in=%d\n",
1475 	    if_name(ifa->ifa_ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1476 	    dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_ns_lcount,
1477 	    dp->dad_na_icount);
1478 
1479 	ia->ia6_flags &= ~IN6_IFF_TENTATIVE;
1480 	ia->ia6_flags |= IN6_IFF_DUPLICATED;
1481 
1482 	ifp = ifa->ifa_ifp;
1483 	log(LOG_ERR, "%s: DAD complete for %s - duplicate found\n",
1484 	    if_name(ifp), ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr));
1485 	log(LOG_ERR, "%s: manual intervention required\n",
1486 	    if_name(ifp));
1487 
1488 	/*
1489 	 * If the address is a link-local address formed from an interface
1490 	 * identifier based on the hardware address which is supposed to be
1491 	 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1492 	 * operation on the interface SHOULD be disabled.
1493 	 * [RFC 4862, Section 5.4.5]
1494 	 */
1495 	if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) {
1496 		struct in6_addr in6;
1497 
1498 		/*
1499 		 * To avoid over-reaction, we only apply this logic when we are
1500 		 * very sure that hardware addresses are supposed to be unique.
1501 		 */
1502 		switch (ifp->if_type) {
1503 		case IFT_ETHER:
1504 		case IFT_FDDI:
1505 		case IFT_ATM:
1506 		case IFT_IEEE1394:
1507 		case IFT_IEEE80211:
1508 		case IFT_INFINIBAND:
1509 			in6 = ia->ia_addr.sin6_addr;
1510 			if (in6_get_hw_ifid(ifp, &in6) == 0 &&
1511 			    IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
1512 				ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
1513 				log(LOG_ERR, "%s: possible hardware address "
1514 				    "duplication detected, disable IPv6\n",
1515 				    if_name(ifp));
1516 			}
1517 			break;
1518 		}
1519 	}
1520 }
1521 
1522 static void
1523 nd6_dad_ns_output(struct dadq *dp)
1524 {
1525 	struct in6_ifaddr *ia = (struct in6_ifaddr *)dp->dad_ifa;
1526 	struct ifnet *ifp = dp->dad_ifa->ifa_ifp;
1527 	int i;
1528 
1529 	dp->dad_ns_tcount++;
1530 	if ((ifp->if_flags & IFF_UP) == 0) {
1531 		return;
1532 	}
1533 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1534 		return;
1535 	}
1536 
1537 	dp->dad_ns_ocount++;
1538 	if (V_dad_enhanced != 0) {
1539 		for (i = 0; i < ND_OPT_NONCE_LEN32; i++)
1540 			dp->dad_nonce[i] = arc4random();
1541 		/*
1542 		 * XXXHRS: Note that in the case that
1543 		 * DupAddrDetectTransmits > 1, multiple NS messages with
1544 		 * different nonces can be looped back in an unexpected
1545 		 * order.  The current implementation recognizes only
1546 		 * the latest nonce on the sender side.  Practically it
1547 		 * should work well in almost all cases.
1548 		 */
1549 	}
1550 	nd6_ns_output(ifp, NULL, NULL, &ia->ia_addr.sin6_addr,
1551 	    (uint8_t *)&dp->dad_nonce[0]);
1552 }
1553 
1554 static void
1555 nd6_dad_ns_input(struct ifaddr *ifa, struct nd_opt_nonce *ndopt_nonce)
1556 {
1557 	struct in6_ifaddr *ia;
1558 	struct ifnet *ifp;
1559 	const struct in6_addr *taddr6;
1560 	struct dadq *dp;
1561 
1562 	if (ifa == NULL)
1563 		panic("ifa == NULL in nd6_dad_ns_input");
1564 
1565 	ia = (struct in6_ifaddr *)ifa;
1566 	ifp = ifa->ifa_ifp;
1567 	taddr6 = &ia->ia_addr.sin6_addr;
1568 	/* Ignore Nonce option when Enhanced DAD is disabled. */
1569 	if (V_dad_enhanced == 0)
1570 		ndopt_nonce = NULL;
1571 	dp = nd6_dad_find(ifa, ndopt_nonce);
1572 	if (dp == NULL)
1573 		return;
1574 
1575 	dp->dad_ns_icount++;
1576 	nd6_dad_rele(dp);
1577 }
1578 
1579 static void
1580 nd6_dad_na_input(struct ifaddr *ifa)
1581 {
1582 	struct dadq *dp;
1583 
1584 	if (ifa == NULL)
1585 		panic("ifa == NULL in nd6_dad_na_input");
1586 
1587 	dp = nd6_dad_find(ifa, NULL);
1588 	if (dp != NULL) {
1589 		dp->dad_na_icount++;
1590 		nd6_dad_rele(dp);
1591 	}
1592 }
1593