xref: /xnu-11417.140.69/bsd/netinet6/nd6_nbr.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1 /*
2  * Copyright (c) 2000-2025 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /*
30  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 3. Neither the name of the project nor the names of its contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57 
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/malloc.h>
61 #include <sys/mbuf.h>
62 #include <sys/socket.h>
63 #include <sys/sockio.h>
64 #include <sys/time.h>
65 #include <sys/kernel.h>
66 #include <sys/errno.h>
67 #include <sys/syslog.h>
68 #include <sys/sysctl.h>
69 #include <sys/mcache.h>
70 #include <sys/protosw.h>
71 #include <kern/queue.h>
72 #include <dev/random/randomdev.h>
73 
74 #include <kern/locks.h>
75 #include <kern/zalloc.h>
76 
77 #include <net/if.h>
78 #include <net/if_var.h>
79 #include <net/if_types.h>
80 #include <net/if_dl.h>
81 #include <net/if_llreach.h>
82 #include <net/route.h>
83 #include <net/dlil.h>
84 #include <net/nwk_wq.h>
85 #include <net/droptap.h>
86 
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet6/in6_var.h>
90 #include <netinet6/in6_ifattach.h>
91 #include <netinet/ip6.h>
92 #include <netinet6/ip6_var.h>
93 #include <netinet6/nd6.h>
94 #include <netinet6/scope6_var.h>
95 #include <netinet/icmp6.h>
96 
97 #if IPSEC
98 #include <netinet6/ipsec.h>
99 #include <netinet6/ipsec6.h>
100 #endif
101 
102 #include <net/sockaddr_utils.h>
103 
104 struct dadq;
105 static struct dadq *nd6_dad_find(struct ifaddr *, struct nd_opt_nonce *);
106 void nd6_dad_stoptimer(struct ifaddr *);
107 static void nd6_dad_timer(struct ifaddr *);
108 static void nd6_dad_ns_output(struct dadq *, struct ifaddr *);
109 static void nd6_dad_ns_input(struct ifaddr *, char *__sized_by(lladdrlen), int lladdrlen, struct nd_opt_nonce *);
110 static struct mbuf *nd6_dad_na_input(struct mbuf *, struct ifnet *,
111     struct in6_addr *, caddr_t  __counted_by(lladdrlen), int lladdrlen);
112 static void dad_addref(struct dadq *, int);
113 static void dad_remref(struct dadq *);
114 static struct dadq *nd6_dad_attach(struct dadq *, struct ifaddr *);
115 static void nd6_dad_detach(struct dadq *, struct ifaddr *);
116 static void nd6_dad_duplicated(struct ifaddr *);
117 
118 static int dad_maxtry = 15;     /* max # of *tries* to transmit DAD packet */
119 
120 #define DAD_LOCK_ASSERT_HELD(_dp)                                       \
121 	LCK_MTX_ASSERT(&(_dp)->dad_lock, LCK_MTX_ASSERT_OWNED)
122 
123 #define DAD_LOCK_ASSERT_NOTHELD(_dp)                                    \
124 	LCK_MTX_ASSERT(&(_dp)->dad_lock, LCK_MTX_ASSERT_NOTOWNED)
125 
126 #define DAD_LOCK(_dp)                                                   \
127 	lck_mtx_lock(&(_dp)->dad_lock)
128 
129 #define DAD_LOCK_SPIN(_dp)                                              \
130 	lck_mtx_lock_spin(&(_dp)->dad_lock)
131 
132 #define DAD_CONVERT_LOCK(_dp) do {                                      \
133 	DAD_LOCK_ASSERT_HELD(_dp);                                      \
134 	lck_mtx_convert_spin(&(_dp)->dad_lock);                         \
135 } while (0)
136 
137 #define DAD_UNLOCK(_dp)                                                 \
138 	lck_mtx_unlock(&(_dp)->dad_lock)
139 
140 #define DAD_ADDREF(_dp)                                                 \
141 	dad_addref(_dp, 0)
142 
143 #define DAD_ADDREF_LOCKED(_dp)                                          \
144 	dad_addref(_dp, 1)
145 
146 #define DAD_REMREF(_dp)                                                 \
147 	dad_remref(_dp)
148 
149 static LCK_MTX_DECLARE_ATTR(dad6_mutex, &ip6_mutex_grp, &ip6_mutex_attr);
150 
151 static struct sockaddr_in6 hostrtmask;
152 
153 static int nd6_llreach_base = 30;        /* seconds */
154 SYSCTL_DECL(_net_inet6_icmp6);
155 SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_llreach_base,
156     CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_llreach_base, 0,
157     "default ND6 link-layer reachability max lifetime (in seconds)");
158 
159 int dad_enhanced = ND6_DAD_ENHANCED_DEFAULT;
160 SYSCTL_DECL(_net_inet6_ip6);
161 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, dad_enhanced, CTLFLAG_RW | CTLFLAG_LOCKED,
162     &dad_enhanced, 0,
163     "Enable Enhanced DAD, which adds a random nonce to NS messages for DAD.");
164 
165 static uint32_t nd6_dad_nonce_max_count = 3;
166 SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, nd6_dad_nonce_max_count,
167     CTLFLAG_RW | CTLFLAG_LOCKED, &nd6_dad_nonce_max_count, 0, "Number of times to ignore same nonce for DAD");
168 
169 #if DEBUG || DEVELOPMENT
170 static int  ip6_p2p_debug = 0;
171 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, ip6_p2p_debug, CTLFLAG_RW | CTLFLAG_LOCKED,
172     &ip6_p2p_debug, 0,
173     "Enable more instrumentation for IPv6 P2P use-case");
174 #endif
175 
176 /*
177  * Obtain a link-layer source cache entry for the sender.
178  *
179  * NOTE: This is currently only for ND6/Ethernet.
180  */
181 void
nd6_llreach_alloc(struct rtentry * rt,struct ifnet * ifp,void * addr __sized_by (alen),unsigned int alen,boolean_t solicited)182 nd6_llreach_alloc(struct rtentry *rt, struct ifnet *ifp, void *addr __sized_by(alen),
183     unsigned int alen, boolean_t solicited)
184 {
185 	struct llinfo_nd6 *__single ln = rt->rt_llinfo;
186 
187 	if (nd6_llreach_base != 0 &&
188 	    (ln->ln_expire != 0 || (ifp->if_eflags & IFEF_IPV6_ND6ALT) != 0) &&
189 	    !(rt->rt_ifp->if_flags & IFF_LOOPBACK) &&
190 	    ifp->if_addrlen == IF_LLREACH_MAXLEN &&     /* Ethernet */
191 	    alen == ifp->if_addrlen) {
192 		struct if_llreach *__single lr;
193 		const char *why = NULL, *type = "";
194 
195 		/* Become a regular mutex, just in case */
196 		RT_CONVERT_LOCK(rt);
197 
198 		if ((lr = ln->ln_llreach) != NULL) {
199 			type = (solicited ? "ND6 advertisement" :
200 			    "ND6 unsolicited announcement");
201 			/*
202 			 * If target has changed, create a new record;
203 			 * otherwise keep existing record.
204 			 */
205 			IFLR_LOCK(lr);
206 			if (bcmp(addr, lr->lr_key.addr, alen) != 0) {
207 				IFLR_UNLOCK(lr);
208 				/* Purge any link-layer info caching */
209 				VERIFY(rt->rt_llinfo_purge != NULL);
210 				rt->rt_llinfo_purge(rt);
211 				lr = NULL;
212 				why = " for different target HW address; "
213 				    "using new llreach record";
214 			} else {
215 				lr->lr_probes = 0;      /* reset probe count */
216 				IFLR_UNLOCK(lr);
217 				if (solicited) {
218 					why = " for same target HW address; "
219 					    "keeping existing llreach record";
220 				}
221 			}
222 		}
223 
224 		if (lr == NULL) {
225 			lr = ln->ln_llreach = ifnet_llreach_alloc(ifp,
226 			    ETHERTYPE_IPV6, addr, alen, nd6_llreach_base);
227 			if (lr != NULL) {
228 				lr->lr_probes = 0;      /* reset probe count */
229 				if (why == NULL) {
230 					why = "creating new llreach record";
231 				}
232 			}
233 		}
234 
235 		if (nd6_debug && lr != NULL && why != NULL) {
236 			char tmp[MAX_IPv6_STR_LEN];
237 
238 			nd6log2(debug, "%s: %s%s for %s iface=%s\n", __func__,
239 			    type, why, inet_ntop(AF_INET6,
240 			    &SIN6(rt_key(rt))->sin6_addr, tmp, sizeof(tmp)),
241 			    if_name(ifp));
242 		}
243 	}
244 }
245 
246 void
nd6_llreach_use(struct llinfo_nd6 * ln)247 nd6_llreach_use(struct llinfo_nd6 *ln)
248 {
249 	if (ln->ln_llreach != NULL) {
250 		ln->ln_lastused = net_uptime();
251 	}
252 }
253 
254 /*
255  * Input a Neighbor Solicitation Message.
256  *
257  * Based on RFC 4861
258  * Based on RFC 4862 (duplicate address detection)
259  */
260 void
nd6_ns_input(struct mbuf * m,int off,int icmp6len)261 nd6_ns_input(
262 	struct mbuf *m,
263 	int off,
264 	int icmp6len)
265 {
266 	ifnet_ref_t ifp = m->m_pkthdr.rcvif;
267 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
268 	struct nd_neighbor_solicit *nd_ns = NULL;
269 	struct in6_addr saddr6 = ip6->ip6_src;
270 	struct in6_addr daddr6 = ip6->ip6_dst;
271 	uint32_t saddr_ifscope = IN6_IS_SCOPE_EMBED(&saddr6) ? ip6_input_getsrcifscope(m) : IFSCOPE_NONE;
272 	struct in6_addr taddr6 = {};
273 	struct in6_addr myaddr6 = {};
274 	uint32_t myaddr_ifscope = IFSCOPE_NONE;
275 	int lladdrlen = 0;
276 	char *lladdr = NULL;
277 	struct ifaddr *__single ifa = NULL;
278 	int anycast = 0, proxy = 0, dadprogress = 0;
279 	int tlladdr = 0;
280 	union nd_opts ndopts = {};
281 	struct sockaddr_dl proxydl = {};
282 	boolean_t advrouter = FALSE;
283 	boolean_t is_dad_probe = FALSE;
284 	int oflgclr = 0;
285 	uint32_t taddr_ifscope;
286 
287 	/* Expect 32-bit aligned data pointer on strict-align platforms */
288 	MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
289 
290 	IP6_EXTHDR_CHECK(m, off, icmp6len, return );
291 	ip6 = mtod(m, struct ip6_hdr *);
292 	nd_ns = (struct nd_neighbor_solicit *)((caddr_t)ip6 + off);
293 	m->m_pkthdr.pkt_flags |= PKTF_INET6_RESOLVE;
294 
295 	taddr6 = nd_ns->nd_ns_target;
296 	if (in6_setscope(&taddr6, ifp, &taddr_ifscope) != 0) {
297 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_SCOPE, NULL, 0);
298 		goto bad;
299 	}
300 
301 	if (ip6->ip6_hlim != IPV6_MAXHLIM) {
302 		nd6log(error,
303 		    "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
304 		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
305 		    ip6_sprintf(&ip6->ip6_dst), if_name(ifp));
306 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_HLIM, NULL, 0);
307 		goto bad;
308 	}
309 
310 	is_dad_probe = IN6_IS_ADDR_UNSPECIFIED(&saddr6);
311 	if (is_dad_probe) {
312 		/* dst has to be a solicited node multicast address. */
313 		if (daddr6.s6_addr16[0] == IPV6_ADDR_INT16_MLL &&
314 		    /* don't check ifindex portion */
315 		    daddr6.s6_addr32[1] == 0 &&
316 		    daddr6.s6_addr32[2] == IPV6_ADDR_INT32_ONE &&
317 		    daddr6.s6_addr8[12] == 0xff) {
318 			; /* good */
319 		} else {
320 			nd6log(info, "nd6_ns_input: bad DAD packet (wrong ip6 dst)\n");
321 			m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_DAD, NULL, 0);
322 			goto bad;
323 		}
324 	} else if (!nd6_onlink_ns_rfc4861) {
325 		struct sockaddr_in6 src_sa6;
326 
327 		/*
328 		 * According to recent IETF discussions, it is not a good idea
329 		 * to accept a NS from an address which would not be deemed
330 		 * to be a neighbor otherwise.  This point is expected to be
331 		 * clarified in future revisions of the specification.
332 		 */
333 		SOCKADDR_ZERO(&src_sa6, sizeof(src_sa6));
334 		src_sa6.sin6_family = AF_INET6;
335 		src_sa6.sin6_len = sizeof(src_sa6);
336 		src_sa6.sin6_addr = saddr6;
337 		if (!in6_embedded_scope) {
338 			src_sa6.sin6_scope_id = saddr_ifscope;
339 		}
340 		if (!nd6_is_addr_neighbor(&src_sa6, ifp, 0)) {
341 			nd6log(info, "nd6_ns_input: NS packet from non-neighbor\n");
342 			m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_ND_STATE, NULL, 0);
343 			goto bad;
344 		}
345 	}
346 
347 	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
348 		nd6log(info, "nd6_ns_input: bad NS target (multicast)\n");
349 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_ND_STATE, NULL, 0);
350 		goto bad;
351 	}
352 
353 	icmp6len -= sizeof(*nd_ns);
354 
355 	nd6_option_init(nd_ns + 1, icmp6len, &ndopts);
356 	if (nd6_options(&ndopts) < 0) {
357 		nd6log(info, "nd6_ns_input: invalid ND option, ignored\n");
358 		/* nd6_options have incremented stats */
359 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_ND_STATE, NULL, 0);
360 		goto bad;
361 	}
362 
363 	if (ndopts.nd_opts_src_lladdr) {
364 		ND_OPT_LLADDR(ndopts.nd_opts_src_lladdr, nd_opt_len, lladdr, lladdrlen);
365 	}
366 
367 	if (is_dad_probe && lladdr) {
368 		nd6log(info, "nd6_ns_input: bad DAD packet "
369 		    "(link-layer address option)\n");
370 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_DAD, NULL, 0);
371 		goto bad;
372 	}
373 
374 	/*
375 	 * Attaching target link-layer address to the NA?
376 	 * (RFC 2461 7.2.4)
377 	 *
378 	 * NS IP dst is unicast/anycast			MUST NOT add
379 	 * NS IP dst is solicited-node multicast	MUST add
380 	 *
381 	 * In implementation, we add target link-layer address by default.
382 	 * We do not add one in MUST NOT cases.
383 	 */
384 	if (!IN6_IS_ADDR_MULTICAST(&daddr6)) {
385 		tlladdr = 0;
386 	} else {
387 		tlladdr = 1;
388 	}
389 
390 	/*
391 	 * Target address (taddr6) must be either:
392 	 * (1) Valid unicast/anycast address for my receiving interface,
393 	 * (2) Unicast address for which I'm offering proxy service, or
394 	 * (3) "tentative" or "optimistic" address [DAD is in progress].
395 	 */
396 	/* (1) and (3) check. */
397 	ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, &taddr6);
398 
399 	/* (2) check. */
400 	if (ifa == NULL) {
401 		rtentry_ref_t rt;
402 		struct sockaddr_in6 tsin6;
403 
404 		SOCKADDR_ZERO(&tsin6, sizeof tsin6);
405 		tsin6.sin6_len = sizeof(struct sockaddr_in6);
406 		tsin6.sin6_family = AF_INET6;
407 		tsin6.sin6_addr = taddr6;
408 
409 		rt = rtalloc1_scoped(SA(&tsin6), 0, 0, ifp->if_index);
410 
411 		if (rt != NULL) {
412 			RT_LOCK(rt);
413 			if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
414 			    rt->rt_gateway->sa_family == AF_LINK) {
415 				/*
416 				 * proxy NDP for single entry
417 				 */
418 				ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(
419 					ifp, IN6_IFF_NOTREADY | IN6_IFF_ANYCAST);
420 				if (ifa) {
421 					proxy = 1;
422 					proxydl = *SDL(rt->rt_gateway);
423 				}
424 			}
425 			RT_UNLOCK(rt);
426 			rtfree(rt);
427 		}
428 	}
429 	if (ifa == NULL && ip6_forwarding && nd6_prproxy) {
430 		/*
431 		 * Is the target address part of the prefix that is being
432 		 * proxied and installed on another interface?
433 		 */
434 		ifa = (struct ifaddr *)in6ifa_prproxyaddr(&taddr6, taddr_ifscope);
435 	}
436 	if (ifa == NULL) {
437 		/*
438 		 * We've got an NS packet, and we don't have that address
439 		 * assigned for us.  We MUST silently ignore it on this
440 		 * interface, c.f. RFC 4861 7.2.3.
441 		 *
442 		 * Forwarding associated with NDPRF_PRPROXY may apply.
443 		 */
444 		if (ip6_forwarding && nd6_prproxy) {
445 			size_t noncelen = (ndopts.nd_opts_nonce == NULL) ? 0 :
446 			    ndopts.nd_opts_nonce->nd_opt_nonce_len << 3;
447 			uint8_t *nonce = (ndopts.nd_opts_nonce == NULL) ? NULL :
448 			    __unsafe_forge_bidi_indexable(uint8_t *, ndopts.nd_opts_nonce->nd_opt_nonce, noncelen);
449 
450 			nd6_prproxy_ns_input(ifp, &saddr6, lladdr,
451 			    lladdrlen, &daddr6, &taddr6,
452 			    nonce, noncelen);
453 		}
454 		goto freeit;
455 	}
456 	IFA_LOCK(ifa);
457 	myaddr6 = *IFA_IN6(ifa);
458 	myaddr_ifscope = IFA_SIN6_SCOPE(ifa);
459 	anycast = (ifatoia6(ifa))->ia6_flags & IN6_IFF_ANYCAST;
460 	dadprogress =
461 	    (ifatoia6(ifa))->ia6_flags & IN6_IFF_DADPROGRESS;
462 	if ((ifatoia6(ifa))->ia6_flags & IN6_IFF_DUPLICATED) {
463 		IFA_UNLOCK(ifa);
464 		goto freeit;
465 	}
466 	IFA_UNLOCK(ifa);
467 
468 	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
469 		nd6log(info,
470 		    "nd6_ns_input: lladdrlen mismatch for %s "
471 		    "(if %d, NS packet %d)\n",
472 		    ip6_sprintf(&taddr6), ifp->if_addrlen, lladdrlen - 2);
473 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_ND_STATE, NULL, 0);
474 		goto bad;
475 	}
476 
477 	if (in6_are_addr_equal_scoped(&myaddr6, &saddr6, myaddr_ifscope, saddr_ifscope)) {
478 		nd6log(info,
479 		    "nd6_ns_input: duplicate IP6 address %s\n",
480 		    ip6_sprintf(&saddr6));
481 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_ND_STATE, NULL, 0);
482 		goto bad;
483 	}
484 
485 	/*
486 	 * We have neighbor solicitation packet, with target address equals to
487 	 * one of my DAD in-progress addresses.
488 	 *
489 	 * src addr	how to process?
490 	 * ---		---
491 	 * multicast	of course, invalid (rejected in ip6_input)
492 	 * unicast	somebody is doing address resolution
493 	 * unspec	dup address detection
494 	 *
495 	 * The processing is defined in the "draft standard" RFC 4862 (and by
496 	 * RFC 4429, which is a "proposed standard" update to its obsolete
497 	 * predecessor, RFC 2462)  The reason optimistic DAD is not included
498 	 * in RFC 4862 is entirely due to IETF procedural considerations.
499 	 */
500 	if (dadprogress) {
501 		/*
502 		 * If source address is unspecified address, it is for
503 		 * duplicate address detection.
504 		 *
505 		 * If not, the packet is for addess resolution;
506 		 * silently ignore it when not optimistic
507 		 *
508 		 * Per RFC 4429 the reply for an optimistic address must
509 		 * have the Override flag cleared
510 		 */
511 		if (!is_dad_probe && (dadprogress & IN6_IFF_OPTIMISTIC) != 0) {
512 			oflgclr = 1;
513 		} else {
514 			if (is_dad_probe) {
515 				nd6_dad_ns_input(ifa, lladdr, lladdrlen, ndopts.nd_opts_nonce);
516 			}
517 
518 			goto freeit;
519 		}
520 	}
521 
522 	/* Are we an advertising router on this interface? */
523 	advrouter = (ifp->if_ipv6_router_mode != IPV6_ROUTER_MODE_DISABLED);
524 
525 	/*
526 	 * If the source address is unspecified address, entries must not
527 	 * be created or updated.
528 	 * It looks that sender is performing DAD.  If I'm using the address,
529 	 * and it's a "preferred" address, i.e. not optimistic, then output NA
530 	 * toward all-node multicast address, to tell the sender that I'm using
531 	 * the address.
532 	 * S bit ("solicited") must be zero.
533 	 */
534 	if (is_dad_probe) {
535 		saddr6 = in6addr_linklocal_allnodes;
536 		if (in6_setscope(&saddr6, ifp, NULL) != 0) {
537 			m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_SCOPE, NULL, 0);
538 			goto bad;
539 		}
540 		if ((dadprogress & IN6_IFF_OPTIMISTIC) == 0) {
541 			nd6_na_output(ifp, &saddr6, &taddr6,
542 			    ((anycast || proxy || !tlladdr) ? 0 :
543 			    ND_NA_FLAG_OVERRIDE) | (advrouter ?
544 			    ND_NA_FLAG_ROUTER : 0), tlladdr, proxy ?
545 			    SA(&proxydl) : NULL);
546 		}
547 		goto freeit;
548 	}
549 
550 	nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen,
551 	    ND_NEIGHBOR_SOLICIT, 0, NULL);
552 
553 	nd6_na_output(ifp, &saddr6, &taddr6,
554 	    ((anycast || proxy || !tlladdr || oflgclr) ? 0 : ND_NA_FLAG_OVERRIDE) |
555 	    (advrouter ? ND_NA_FLAG_ROUTER : 0) | ND_NA_FLAG_SOLICITED,
556 	    tlladdr, proxy ? SA(&proxydl) : NULL);
557 freeit:
558 	m_freem(m);
559 	if (ifa != NULL) {
560 		ifa_remref(ifa);
561 	}
562 	return;
563 
564 bad:
565 	nd6log(error, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6));
566 	nd6log(error, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6));
567 	nd6log(error, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6));
568 	icmp6stat.icp6s_badns++;
569 	if (ifa != NULL) {
570 		ifa_remref(ifa);
571 	}
572 }
573 
574 /*
575  * Output a Neighbor Solicitation Message. Caller specifies:
576  *	- ICMP6 header source IP6 address
577  *	- ND6 header target IP6 address
578  *	- ND6 header source datalink address
579  *
580  * Based on RFC 4861
581  * Based on RFC 4862 (duplicate address detection)
582  * Based on RFC 4429 (optimistic duplicate address detection)
583  *
584  * Caller must bump up ln->ln_rt refcnt to make sure 'ln' doesn't go
585  * away if there is a llinfo_nd6 passed in.
586  */
587 void
nd6_ns_output(struct ifnet * ifp,const struct in6_addr * daddr6,const struct in6_addr * taddr6,struct llinfo_nd6 * ln,uint8_t * __counted_by (noncelen)nonce,size_t noncelen)588 nd6_ns_output(
589 	struct ifnet *ifp,
590 	const struct in6_addr *daddr6,
591 	const struct in6_addr *taddr6,
592 	struct llinfo_nd6 *ln,  /* for source address determination */
593 	uint8_t *__counted_by(noncelen) nonce,
594 	size_t noncelen)
595 {
596 #pragma unused(noncelen)
597 
598 	mbuf_ref_t m;
599 	struct ip6_hdr *ip6;
600 	struct nd_neighbor_solicit *nd_ns;
601 	struct in6_ifaddr *ia = NULL;
602 	struct in6_addr *__single src, src_in, src_storage;
603 	struct ip6_moptions *__single im6o = NULL;
604 	ifnet_ref_t outif = NULL;
605 	int icmp6len;
606 	int maxlen;
607 	int flags;
608 	caddr_t mac;
609 	struct route_in6 ro;
610 	struct ip6_out_args ip6oa;
611 	u_int32_t rtflags = 0;
612 	boolean_t is_optimistic = FALSE;
613 	drop_reason_t drop_reason = DROP_REASON_UNSPECIFIED;
614 
615 	if ((ifp->if_eflags & IFEF_IPV6_ND6ALT) || IN6_IS_ADDR_MULTICAST(taddr6)) {
616 		return;
617 	}
618 
619 	bzero(&ro, sizeof(ro));
620 	bzero(&ip6oa, sizeof(ip6oa));
621 	ip6oa.ip6oa_boundif = ifp->if_index;
622 	ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR |
623 	    IP6OAF_AWDL_UNRESTRICTED | IP6OAF_INTCOPROC_ALLOWED |
624 	    IP6OAF_MANAGEMENT_ALLOWED | IP6OAF_ULTRA_CONSTRAINED_ALLOWED;
625 	ip6oa.ip6oa_sotc = SO_TC_UNSPEC;
626 	ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
627 
628 	ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
629 
630 	/* estimate the size of message */
631 	maxlen = sizeof(*ip6) + sizeof(*nd_ns);
632 	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
633 	if (max_linkhdr + maxlen >= MCLBYTES) {
634 #if DIAGNOSTIC
635 		printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
636 		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
637 #endif
638 		return;
639 	}
640 
641 	MGETHDR(m, M_DONTWAIT, MT_DATA);        /* XXXMAC: mac_create_mbuf_linklayer() probably */
642 	if (m && max_linkhdr + maxlen >= MHLEN) {
643 		MCLGET(m, M_DONTWAIT);
644 		if ((m->m_flags & M_EXT) == 0) {
645 			m_free(m);
646 			m = NULL;
647 		}
648 	}
649 	if (m == NULL) {
650 		return;
651 	}
652 	m->m_pkthdr.rcvif = NULL;
653 
654 	if (daddr6 == NULL || IN6_IS_ADDR_MULTICAST(daddr6)) {
655 		m->m_flags |= M_MCAST;
656 
657 		im6o = ip6_allocmoptions(Z_NOWAIT);
658 		if (im6o == NULL) {
659 			m_freem(m);
660 			return;
661 		}
662 
663 		im6o->im6o_multicast_ifp = ifp;
664 		im6o->im6o_multicast_hlim = IPV6_MAXHLIM;
665 		im6o->im6o_multicast_loop = 0;
666 	}
667 
668 	icmp6len = sizeof(*nd_ns);
669 	m->m_pkthdr.len = m->m_len = sizeof(*ip6) + icmp6len;
670 	m->m_data += max_linkhdr;       /* or MH_ALIGN() equivalent? */
671 
672 	/* fill neighbor solicitation packet */
673 	ip6 = mtod(m, struct ip6_hdr *);
674 	ip6->ip6_flow = 0;
675 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
676 	ip6->ip6_vfc |= IPV6_VERSION;
677 	/* ip6->ip6_plen will be set later */
678 	ip6->ip6_nxt = IPPROTO_ICMPV6;
679 	ip6->ip6_hlim = IPV6_MAXHLIM;
680 	if (daddr6) {
681 		ip6->ip6_dst = *daddr6;
682 		ip6_output_setdstifscope(m, ifp->if_index, NULL);
683 	} else {
684 		ip6->ip6_dst.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
685 		ip6->ip6_dst.s6_addr16[1] = 0;
686 		ip6->ip6_dst.s6_addr32[1] = 0;
687 		ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_ONE;
688 		ip6->ip6_dst.s6_addr32[3] = taddr6->s6_addr32[3];
689 		ip6->ip6_dst.s6_addr8[12] = 0xff;
690 		ip6_output_setdstifscope(m, ifp->if_index, NULL);
691 		if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) {
692 			drop_reason = DROP_REASON_IP6_BAD_SCOPE;
693 			goto bad;
694 		}
695 	}
696 	if (nonce == NULL) {
697 		/*
698 		 * RFC2461 7.2.2:
699 		 * "If the source address of the packet prompting the
700 		 * solicitation is the same as one of the addresses assigned
701 		 * to the outgoing interface, that address SHOULD be placed
702 		 * in the IP Source Address of the outgoing solicitation.
703 		 * Otherwise, any one of the addresses assigned to the
704 		 * interface should be used."
705 		 *
706 		 * We use the source address for the prompting packet
707 		 * (saddr6), if:
708 		 * - saddr6 is given from the caller (by giving "ln"), and
709 		 * - saddr6 belongs to the outgoing interface.
710 		 * Otherwise, we perform the source address selection as usual.
711 		 */
712 		struct ip6_hdr *__single hip6;           /* hold ip6 */
713 		struct in6_addr hsrc = {};
714 
715 		/* Caller holds ref on this route */
716 		if (ln != NULL) {
717 			RT_LOCK(ln->ln_rt);
718 			/*
719 			 * assuming every packet in ln_hold has the same IP
720 			 * header
721 			 */
722 			if (ln->ln_hold != NULL) {
723 				hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
724 				/* XXX pullup? */
725 				if (sizeof(*hip6) < ln->ln_hold->m_len) {
726 					memcpy(&hsrc, &hip6->ip6_src, sizeof(struct in6_addr));
727 				}
728 			}
729 			/* Update probe count, if applicable */
730 			if (ln->ln_llreach != NULL) {
731 				IFLR_LOCK_SPIN(ln->ln_llreach);
732 				ln->ln_llreach->lr_probes++;
733 				IFLR_UNLOCK(ln->ln_llreach);
734 			}
735 			rtflags = ln->ln_rt->rt_flags;
736 			RT_UNLOCK(ln->ln_rt);
737 		}
738 		if (!IN6_IS_ADDR_UNSPECIFIED(&hsrc) && (ia = in6ifa_ifpwithaddr(ifp, &hsrc)) &&
739 		    (ia->ia6_flags & IN6_IFF_OPTIMISTIC) == 0) {
740 			src = &hsrc;
741 		} else {
742 			int error;
743 			struct sockaddr_in6 dst_sa;
744 
745 			SOCKADDR_ZERO(&dst_sa, sizeof(dst_sa));
746 			dst_sa.sin6_family = AF_INET6;
747 			dst_sa.sin6_len = sizeof(dst_sa);
748 			dst_sa.sin6_addr = ip6->ip6_dst;
749 
750 			src = in6_selectsrc(&dst_sa, NULL,
751 			    NULL, &ro, NULL, &src_storage, ip6oa.ip6oa_boundif,
752 			    &error);
753 			if (src == NULL) {
754 				nd6log(info,
755 				    "nd6_ns_output: source can't be "
756 				    "determined: dst=%s, error=%d\n",
757 				    ip6_sprintf(&dst_sa.sin6_addr),
758 				    error);
759 				drop_reason = DROP_REASON_IP_SRC_ADDR_NO_AVAIL;
760 				goto bad;
761 			}
762 
763 			if (ia != NULL) {
764 				ifa_remref(&ia->ia_ifa);
765 				ia = NULL;
766 			}
767 			/*
768 			 * RFC 4429 section 3.2:
769 			 * When a node has a unicast packet to send
770 			 * from an Optimistic Address to a neighbor,
771 			 * but does not know the neighbor's link-layer
772 			 * address, it MUST NOT perform Address
773 			 * Resolution.
774 			 */
775 			ia = in6ifa_ifpwithaddr(ifp, src);
776 			if (ia == NULL) {
777 				nd6log(info,
778 				    "nd6_ns_output: no preferred source "
779 				    "available: dst=%s\n",
780 				    ip6_sprintf(&dst_sa.sin6_addr));
781 				drop_reason = DROP_REASON_IP_SRC_ADDR_NO_AVAIL;
782 				goto bad;
783 			}
784 			if (ia->ia6_flags & IN6_IFF_OPTIMISTIC) {
785 				is_optimistic = TRUE;
786 				nd6log(info,
787 				    "nd6_ns_output: preferred source "
788 				    "available is optimistic: dst=%s\n",
789 				    ip6_sprintf(&dst_sa.sin6_addr));
790 			}
791 		}
792 	} else {
793 		/*
794 		 * Source address for DAD packet must always be IPv6
795 		 * unspecified address. (0::0)
796 		 * We actually don't have to 0-clear the address (we did it
797 		 * above), but we do so here explicitly to make the intention
798 		 * clearer.
799 		 */
800 		bzero(&src_in, sizeof(src_in));
801 		src = &src_in;
802 		ip6oa.ip6oa_flags &= ~IP6OAF_BOUND_SRCADDR;
803 	}
804 
805 	ip6->ip6_src = *src;
806 	ip6_output_setsrcifscope(m, ifp->if_index, ia);
807 	nd_ns = (struct nd_neighbor_solicit *)(ip6 + 1);
808 	nd_ns->nd_ns_type = ND_NEIGHBOR_SOLICIT;
809 	nd_ns->nd_ns_code = 0;
810 	nd_ns->nd_ns_reserved = 0;
811 	nd_ns->nd_ns_target = *taddr6;
812 	in6_clearscope(&nd_ns->nd_ns_target); /* XXX */
813 
814 	/*
815 	 * Add source link-layer address option.
816 	 *
817 	 *				spec		implementation
818 	 *				---		---
819 	 * DAD packet			MUST NOT	do not add the option
820 	 * Source is optimistic         MUST NOT        do not add the option
821 	 * there's no link layer address:
822 	 *				impossible	do not add the option
823 	 * there's link layer address:
824 	 *	Multicast NS		MUST add one	add the option
825 	 *	Unicast NS		SHOULD add one	add the option
826 	 *
827 	 * XXX We deviate from RFC 4429 and still use optimistic DAD as source
828 	 * for address resolution. However to ensure that we do not interfere
829 	 * with neighbor cache entries of other neighbors, we MUST ensure
830 	 * that SLLAO is not sent. Also note, sending multicast NS without SLLAO
831 	 * is also a deviation from RFC 4861.
832 	 */
833 	if (nonce == NULL && (mac = nd6_ifptomac(ifp)) && !is_optimistic) {
834 		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
835 		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
836 		/* 8 byte alignments... */
837 		optlen = (optlen + 7) & ~7;
838 
839 		m->m_pkthdr.len += optlen;
840 		m->m_len += optlen;
841 		icmp6len += optlen;
842 		bzero((caddr_t)nd_opt, optlen);
843 		nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
844 		nd_opt->nd_opt_len = (uint8_t)(optlen >> 3);
845 		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
846 	}
847 	/*
848 	 * Add a Nonce option (RFC 3971) to detect looped back NS messages.
849 	 * This behavior is documented as Enhanced Duplicate Address
850 	 * Detection in draft-ietf-6man-enhanced-dad-13.
851 	 * net.inet6.ip6.dad_enhanced=0 disables this.
852 	 */
853 	if (dad_enhanced != 0 && nonce != NULL && !(ifp->if_flags & IFF_POINTOPOINT)) {
854 		int optlen = sizeof(struct nd_opt_hdr) + ND_OPT_NONCE_LEN;
855 		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_ns + 1);
856 		/* 8-byte alignment is required. */
857 		optlen = (optlen + 7) & ~7;
858 
859 		m->m_pkthdr.len += optlen;
860 		m->m_len += optlen;
861 		icmp6len += optlen;
862 		bzero((caddr_t)nd_opt, optlen);
863 		nd_opt->nd_opt_type = ND_OPT_NONCE;
864 		nd_opt->nd_opt_len = (uint8_t)(optlen >> 3);
865 		bcopy(nonce, (caddr_t)(nd_opt + 1), ND_OPT_NONCE_LEN);
866 	}
867 	ip6->ip6_plen = htons((u_short)icmp6len);
868 	nd_ns->nd_ns_cksum = 0;
869 	nd_ns->nd_ns_cksum
870 	        = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), icmp6len);
871 
872 	flags = nonce ? IPV6_UNSPECSRC : 0;
873 	flags |= IPV6_OUTARGS;
874 
875 	/*
876 	 * PKTF_{INET,INET6}_RESOLVE_RTR are mutually exclusive, so make
877 	 * sure only one of them is set (just in case.)
878 	 */
879 	m->m_pkthdr.pkt_flags &= ~(PKTF_INET_RESOLVE | PKTF_RESOLVE_RTR);
880 	m->m_pkthdr.pkt_flags |= PKTF_INET6_RESOLVE;
881 	/*
882 	 * If this is a NS for resolving the (default) router, mark
883 	 * the packet accordingly so that the driver can find out,
884 	 * in case it needs to perform driver-specific action(s).
885 	 */
886 	if (rtflags & RTF_ROUTER) {
887 		m->m_pkthdr.pkt_flags |= PKTF_RESOLVE_RTR;
888 	}
889 
890 	if (ifp->if_eflags & IFEF_TXSTART) {
891 		/*
892 		 * Use control service class if the interface
893 		 * supports transmit-start model
894 		 */
895 		(void) m_set_service_class(m, MBUF_SC_CTL);
896 	}
897 
898 	ip6oa.ip6oa_flags |= IP6OAF_SKIP_PF;
899 	ip6oa.ip6oa_flags |= IP6OAF_DONT_FRAG;
900 	ip6_output(m, NULL, NULL, flags, im6o, &outif, &ip6oa);
901 	if (outif) {
902 		icmp6_ifstat_inc(outif, ifs6_out_msg);
903 		icmp6_ifstat_inc(outif, ifs6_out_neighborsolicit);
904 		ifnet_release(outif);
905 	}
906 	icmp6stat.icp6s_outhist[ND_NEIGHBOR_SOLICIT]++;
907 
908 exit:
909 	if (im6o != NULL) {
910 		IM6O_REMREF(im6o);
911 	}
912 
913 	ROUTE_RELEASE(&ro);     /* we don't cache this route. */
914 
915 	if (ia != NULL) {
916 		ifa_remref(&ia->ia_ifa);
917 	}
918 	return;
919 
920 bad:
921 	m_drop(m, DROPTAP_FLAG_DIR_OUT | DROPTAP_FLAG_L2_MISSING, drop_reason, NULL, 0);
922 	goto exit;
923 }
924 
925 /*
926  * Neighbor advertisement input handling.
927  *
928  * Based on RFC 4861
929  * Based on RFC 4862 (duplicate address detection)
930  *
931  * the following items are not implemented yet:
932  * - anycast advertisement delay rule (RFC 4861 7.2.7, SHOULD)
933  * - proxy advertisement delay rule (RFC 4861 7.2.8, last paragraph, "should")
934  */
935 void
nd6_na_input(struct mbuf * m,int off,int icmp6len)936 nd6_na_input(struct mbuf *m, int off, int icmp6len)
937 {
938 	ifnet_ref_t ifp = m->m_pkthdr.rcvif;
939 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
940 	struct nd_neighbor_advert *nd_na;
941 	struct in6_addr saddr6 = ip6->ip6_src;
942 	struct in6_addr daddr6 = ip6->ip6_dst;
943 	struct in6_addr taddr6;
944 	int flags;
945 	int is_router;
946 	int is_solicited;
947 	int is_override;
948 	char *lladdr = NULL;
949 	int lladdrlen = 0;
950 	struct llinfo_nd6 *__single ln;
951 	rtentry_ref_t rt;
952 	struct sockaddr_dl *sdl;
953 	union nd_opts ndopts;
954 	uint64_t timenow;
955 	bool send_nc_alive_kev = false;
956 	drop_reason_t drop_reason = DROP_REASON_UNSPECIFIED;
957 
958 	if ((ifp->if_eflags & IFEF_IPV6_ND6ALT) != 0) {
959 		nd6log(info, "nd6_na_input: on ND6ALT interface!\n");
960 		drop_reason = DROP_REASON_IP6_NO_ND6ALT_IF;
961 		goto freeit;
962 	}
963 
964 	/* Expect 32-bit aligned data pointer on strict-align platforms */
965 	MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
966 
967 	if (ip6->ip6_hlim != IPV6_MAXHLIM) {
968 		nd6log(error,
969 		    "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
970 		    ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src),
971 		    ip6_sprintf(&ip6->ip6_dst), if_name(ifp));
972 		drop_reason = DROP_REASON_IP6_BAD_HLIM;
973 		goto bad;
974 	}
975 
976 	IP6_EXTHDR_CHECK(m, off, icmp6len, return );
977 	ip6 = mtod(m, struct ip6_hdr *);
978 	nd_na = (struct nd_neighbor_advert *)((caddr_t)ip6 + off);
979 	m->m_pkthdr.pkt_flags |= PKTF_INET6_RESOLVE;
980 
981 	flags = nd_na->nd_na_flags_reserved;
982 	is_router = ((flags & ND_NA_FLAG_ROUTER) != 0);
983 	is_solicited = ((flags & ND_NA_FLAG_SOLICITED) != 0);
984 	is_override = ((flags & ND_NA_FLAG_OVERRIDE) != 0);
985 
986 	taddr6 = nd_na->nd_na_target;
987 	if (in6_setscope(&taddr6, ifp, NULL)) {
988 		drop_reason = DROP_REASON_IP6_BAD_SCOPE;
989 		goto bad;       /* XXX: impossible */
990 	}
991 	if (IN6_IS_ADDR_MULTICAST(&taddr6)) {
992 		nd6log(error,
993 		    "nd6_na_input: invalid target address %s\n",
994 		    ip6_sprintf(&taddr6));
995 		drop_reason = DROP_REASON_IP_DST_ADDR_NO_AVAIL;
996 		goto bad;
997 	}
998 	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
999 		if (is_solicited) {
1000 			nd6log(error,
1001 			    "nd6_na_input: a solicited adv is multicasted\n");
1002 			drop_reason = DROP_REASON_IP6_BAD_ND_STATE;
1003 			goto bad;
1004 		}
1005 	}
1006 
1007 	icmp6len -= sizeof(*nd_na);
1008 	nd6_option_init(nd_na + 1, icmp6len, &ndopts);
1009 	if (nd6_options(&ndopts) < 0) {
1010 		nd6log(info,
1011 		    "nd6_na_input: invalid ND option, ignored\n");
1012 		/* nd6_options have incremented stats */
1013 		drop_reason = DROP_REASON_IP6_TOO_MANY_OPTIONS;
1014 		goto freeit;
1015 	}
1016 
1017 	if (ndopts.nd_opts_tgt_lladdr) {
1018 		ND_OPT_LLADDR(ndopts.nd_opts_tgt_lladdr, nd_opt_len, lladdr, lladdrlen);
1019 
1020 		if (((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
1021 			nd6log(info,
1022 			    "nd6_na_input: lladdrlen mismatch for %s "
1023 			    "(if %d, NA packet %d)\n",
1024 			    ip6_sprintf(&taddr6), ifp->if_addrlen,
1025 			    lladdrlen - 2);
1026 			drop_reason = DROP_REASON_IP6_BAD_ND_STATE;
1027 			goto bad;
1028 		}
1029 	}
1030 
1031 	m = nd6_dad_na_input(m, ifp, &taddr6, lladdr, lladdrlen);
1032 	if (m == NULL) {
1033 		return;
1034 	}
1035 
1036 	/* Forwarding associated with NDPRF_PRPROXY may apply. */
1037 	if (ip6_forwarding && nd6_prproxy) {
1038 		nd6_prproxy_na_input(ifp, &saddr6, &daddr6, &taddr6, flags);
1039 	}
1040 
1041 	/*
1042 	 * If no neighbor cache entry is found, NA SHOULD silently be
1043 	 * discarded.  If we are forwarding (and Scoped Routing is in
1044 	 * effect), try to see if there is a neighbor cache entry on
1045 	 * another interface (in case we are doing prefix proxying.)
1046 	 */
1047 	if ((rt = nd6_lookup(&taddr6, 0, ifp, 0)) == NULL) {
1048 		if (!ip6_forwarding || !nd6_prproxy) {
1049 			drop_reason = DROP_REASON_IP6_BAD_ND_STATE;
1050 			goto freeit;
1051 		}
1052 
1053 		if ((rt = nd6_lookup(&taddr6, 0, NULL, 0)) == NULL) {
1054 			drop_reason = DROP_REASON_IP6_BAD_ND_STATE;
1055 			goto freeit;
1056 		}
1057 
1058 		RT_LOCK_ASSERT_HELD(rt);
1059 		if (rt->rt_ifp != ifp) {
1060 			/*
1061 			 * Purge any link-layer info caching.
1062 			 */
1063 			if (rt->rt_llinfo_purge != NULL) {
1064 				rt->rt_llinfo_purge(rt);
1065 			}
1066 
1067 			/* Adjust route ref count for the interfaces */
1068 			if (rt->rt_if_ref_fn != NULL) {
1069 				rt->rt_if_ref_fn(ifp, 1);
1070 				rt->rt_if_ref_fn(rt->rt_ifp, -1);
1071 			}
1072 
1073 			/* Change the interface when the existing route is on */
1074 			rt->rt_ifp = ifp;
1075 
1076 			/*
1077 			 * If rmx_mtu is not locked, update it
1078 			 * to the MTU used by the new interface.
1079 			 */
1080 			if (!(rt->rt_rmx.rmx_locks & RTV_MTU)) {
1081 				rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
1082 			}
1083 		}
1084 	}
1085 
1086 	RT_LOCK_ASSERT_HELD(rt);
1087 	if ((ln = rt->rt_llinfo) == NULL ||
1088 	    (sdl = SDL(rt->rt_gateway)) == NULL) {
1089 		RT_REMREF_LOCKED(rt);
1090 		RT_UNLOCK(rt);
1091 		drop_reason = DROP_REASON_IP6_BAD_ND_STATE;
1092 		goto freeit;
1093 	}
1094 
1095 	timenow = net_uptime();
1096 
1097 	if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1098 		/*
1099 		 * If the link-layer has address, and no lladdr option came,
1100 		 * discard the packet.
1101 		 */
1102 		if (ifp->if_addrlen && !lladdr) {
1103 			RT_REMREF_LOCKED(rt);
1104 			RT_UNLOCK(rt);
1105 			drop_reason = DROP_REASON_IP6_BAD_ND_STATE;
1106 			goto freeit;
1107 		}
1108 
1109 		/*
1110 		 * Record link-layer address, and update the state.
1111 		 */
1112 		sdl->sdl_alen = ifp->if_addrlen;
1113 		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1114 		if (is_solicited) {
1115 			send_nc_alive_kev = (rt->rt_flags & RTF_ROUTER) ? true : false;
1116 			ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
1117 			if (ln->ln_expire != 0) {
1118 				struct nd_ifinfo *__single ndi = NULL;
1119 
1120 				ndi = ND_IFINFO(rt->rt_ifp);
1121 				VERIFY(ndi != NULL && ndi->initialized);
1122 				lck_mtx_lock(&ndi->lock);
1123 				ln_setexpire(ln, timenow + ndi->reachable);
1124 				lck_mtx_unlock(&ndi->lock);
1125 				RT_UNLOCK(rt);
1126 				lck_mtx_lock(rnh_lock);
1127 				nd6_sched_timeout(NULL, NULL);
1128 				lck_mtx_unlock(rnh_lock);
1129 				RT_LOCK(rt);
1130 			}
1131 		} else {
1132 			ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);
1133 			ln_setexpire(ln, timenow + nd6_gctimer);
1134 		}
1135 
1136 		/*
1137 		 * Enqueue work item to invoke callback for this
1138 		 * route entry
1139 		 */
1140 		route_event_enqueue_nwk_wq_entry(rt, NULL,
1141 		    ROUTE_LLENTRY_RESOLVED, NULL, TRUE);
1142 
1143 		if ((ln->ln_router = (short)is_router) != 0) {
1144 			struct radix_node_head  *__single rnh = NULL;
1145 			struct in6_addr rt_addr = SIN6(rt_key(rt))->sin6_addr;
1146 			ifnet_ref_t rt_ifp = rt->rt_ifp;
1147 
1148 			struct route_event rt_ev;
1149 			route_event_init(&rt_ev, rt, NULL, ROUTE_LLENTRY_RESOLVED);
1150 			/*
1151 			 * This means a router's state has changed from
1152 			 * non-reachable to probably reachable, and might
1153 			 * affect the status of associated prefixes..
1154 			 * We already have a reference on rt. Don't need to
1155 			 * take one for the unlock/lock.
1156 			 */
1157 			RT_UNLOCK(rt);
1158 			defrouter_set_reachability(&rt_addr, rt_ifp, TRUE);
1159 			lck_mtx_lock(rnh_lock);
1160 			rnh = rt_tables[AF_INET6];
1161 
1162 			if (rnh != NULL) {
1163 				(void) rnh->rnh_walktree(rnh, route_event_walktree,
1164 				    (void *)&rt_ev);
1165 			}
1166 			lck_mtx_unlock(rnh_lock);
1167 			lck_mtx_lock(nd6_mutex);
1168 			pfxlist_onlink_check();
1169 			lck_mtx_unlock(nd6_mutex);
1170 			RT_LOCK(rt);
1171 		}
1172 	} else {
1173 		int llchange = 0;
1174 
1175 		/*
1176 		 * Check if the link-layer address has changed or not.
1177 		 */
1178 		if (lladdr == NULL) {
1179 			llchange = 0;
1180 		} else {
1181 			if (sdl->sdl_alen) {
1182 				if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen)) {
1183 					llchange = 1;
1184 				} else {
1185 					llchange = 0;
1186 				}
1187 			} else {
1188 				llchange = 1;
1189 			}
1190 		}
1191 
1192 		/*
1193 		 * This is VERY complex. Look at it with care.
1194 		 *
1195 		 * override solicit lladdr llchange	action
1196 		 *					(L: record lladdr)
1197 		 *
1198 		 *	0	0	n	--	(2c)
1199 		 *	0	0	y	n	(2b) L
1200 		 *	0	0	y	y	(1)    REACHABLE->STALE
1201 		 *	0	1	n	--	(2c)   *->REACHABLE
1202 		 *	0	1	y	n	(2b) L *->REACHABLE
1203 		 *	0	1	y	y	(1)    REACHABLE->STALE
1204 		 *	1	0	n	--	(2a)
1205 		 *	1	0	y	n	(2a) L
1206 		 *	1	0	y	y	(2a) L *->STALE
1207 		 *	1	1	n	--	(2a)   *->REACHABLE
1208 		 *	1	1	y	n	(2a) L *->REACHABLE
1209 		 *	1	1	y	y	(2a) L *->REACHABLE
1210 		 */
1211 		if (!is_override && (lladdr != NULL && llchange)) {  /* (1) */
1212 			/*
1213 			 * If state is REACHABLE, make it STALE.
1214 			 * no other updates should be done.
1215 			 */
1216 			if (ln->ln_state == ND6_LLINFO_REACHABLE) {
1217 				ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);
1218 				ln_setexpire(ln, timenow + nd6_gctimer);
1219 			}
1220 			RT_REMREF_LOCKED(rt);
1221 			RT_UNLOCK(rt);
1222 			drop_reason = DROP_REASON_IP6_BAD_ND_STATE;
1223 			goto freeit;
1224 		} else if (is_override                             /* (2a) */
1225 		    || (!is_override && (lladdr && !llchange))     /* (2b) */
1226 		    || !lladdr) {                                  /* (2c) */
1227 			/*
1228 			 * Update link-local address, if any.
1229 			 */
1230 			if (lladdr) {
1231 				sdl->sdl_alen = ifp->if_addrlen;
1232 				bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1233 			}
1234 
1235 			/*
1236 			 * If solicited, make the state REACHABLE.
1237 			 * If not solicited and the link-layer address was
1238 			 * changed, make it STALE.
1239 			 */
1240 			if (is_solicited) {
1241 				ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
1242 				if (ln->ln_expire != 0) {
1243 					struct nd_ifinfo *__single ndi = NULL;
1244 
1245 					ndi = ND_IFINFO(ifp);
1246 					VERIFY(ndi != NULL && ndi->initialized);
1247 					lck_mtx_lock(&ndi->lock);
1248 					ln_setexpire(ln,
1249 					    timenow + ndi->reachable);
1250 					lck_mtx_unlock(&ndi->lock);
1251 					RT_UNLOCK(rt);
1252 					lck_mtx_lock(rnh_lock);
1253 					nd6_sched_timeout(NULL, NULL);
1254 					lck_mtx_unlock(rnh_lock);
1255 					RT_LOCK(rt);
1256 				}
1257 			} else {
1258 				if (lladdr && llchange) {
1259 					ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);
1260 					ln_setexpire(ln, timenow + nd6_gctimer);
1261 				}
1262 			}
1263 
1264 			/*
1265 			 * XXX
1266 			 * The above is somewhat convoluted, for now just
1267 			 * issue a callback for LLENTRY changed.
1268 			 */
1269 			/* Enqueue work item to invoke callback for this route entry */
1270 			if (llchange) {
1271 				route_event_enqueue_nwk_wq_entry(rt, NULL,
1272 				    ROUTE_LLENTRY_CHANGED, NULL, TRUE);
1273 			}
1274 
1275 			/*
1276 			 * If the router's link-layer address has changed,
1277 			 * notify routes using this as gateway so they can
1278 			 * update any cached information.
1279 			 */
1280 			if (ln->ln_router && is_router && llchange) {
1281 				struct radix_node_head *__single rnh = NULL;
1282 				struct in6_addr rt_addr = SIN6(rt_key(rt))->sin6_addr;
1283 				ifnet_ref_t rt_ifp = rt->rt_ifp;
1284 				struct route_event rt_ev;
1285 				route_event_init(&rt_ev, rt, NULL, ROUTE_LLENTRY_CHANGED);
1286 
1287 				/*
1288 				 * This means a router's state has changed from
1289 				 * non-reachable to probably reachable, and might
1290 				 * affect the status of associated prefixes..
1291 				 *
1292 				 * We already have a valid rt reference here.
1293 				 * We don't need to take another one for unlock/lock.
1294 				 */
1295 				RT_UNLOCK(rt);
1296 				defrouter_set_reachability(&rt_addr, rt_ifp, TRUE);
1297 				lck_mtx_lock(rnh_lock);
1298 				rnh = rt_tables[AF_INET6];
1299 
1300 				if (rnh != NULL) {
1301 					(void) rnh->rnh_walktree(rnh, route_event_walktree,
1302 					    (void *)&rt_ev);
1303 				}
1304 				lck_mtx_unlock(rnh_lock);
1305 				RT_LOCK(rt);
1306 			}
1307 		}
1308 
1309 		if (ln->ln_router && !is_router) {
1310 			/*
1311 			 * The peer dropped the router flag.
1312 			 * Remove the sender from the Default Router List and
1313 			 * update the Destination Cache entries.
1314 			 */
1315 			struct nd_defrouter *__single dr;
1316 			struct in6_addr *__single in6;
1317 			ifnet_ref_t rt_ifp = rt->rt_ifp;
1318 
1319 			in6 = &SIN6(rt_key(rt))->sin6_addr;
1320 
1321 			RT_UNLOCK(rt);
1322 			lck_mtx_lock(nd6_mutex);
1323 			/*
1324 			 * XXX Handle router lists for route information option
1325 			 * as well.
1326 			 */
1327 			dr = defrouter_lookup(NULL, in6, rt_ifp);
1328 			if (dr) {
1329 				TAILQ_REMOVE(&nd_defrouter_list, dr, dr_entry);
1330 				defrtrlist_del(dr, NULL);
1331 				NDDR_REMREF(dr);        /* remove list reference */
1332 				NDDR_REMREF(dr);
1333 				lck_mtx_unlock(nd6_mutex);
1334 			} else {
1335 				lck_mtx_unlock(nd6_mutex);
1336 				/*
1337 				 * Even if the neighbor is not in the
1338 				 * default router list, the neighbor
1339 				 * may be used as a next hop for some
1340 				 * destinations (e.g. redirect case).
1341 				 * So we must call rt6_flush explicitly.
1342 				 */
1343 				rt6_flush(&ip6->ip6_src, rt_ifp);
1344 			}
1345 			RT_LOCK(rt);
1346 		}
1347 		ln->ln_router = (short)is_router;
1348 	}
1349 
1350 	if (send_nc_alive_kev && (ifp->if_addrlen == IF_LLREACH_MAXLEN)) {
1351 		struct kev_msg ev_msg;
1352 		struct kev_nd6_ndalive nd6_ndalive;
1353 		bzero(&ev_msg, sizeof(ev_msg));
1354 		bzero(&nd6_ndalive, sizeof(nd6_ndalive));
1355 		ev_msg.vendor_code      = KEV_VENDOR_APPLE;
1356 		ev_msg.kev_class        = KEV_NETWORK_CLASS;
1357 		ev_msg.kev_subclass     = KEV_ND6_SUBCLASS;
1358 		ev_msg.event_code       = KEV_ND6_NDALIVE;
1359 
1360 		nd6_ndalive.link_data.if_family = ifp->if_family;
1361 		nd6_ndalive.link_data.if_unit = ifp->if_unit;
1362 		strlcpy(nd6_ndalive.link_data.if_name,
1363 		    ifp->if_name,
1364 		    sizeof(nd6_ndalive.link_data.if_name));
1365 		ev_msg.dv[0].data_ptr = &nd6_ndalive;
1366 		ev_msg.dv[0].data_length =
1367 		    sizeof(nd6_ndalive);
1368 		dlil_post_complete_msg(NULL, &ev_msg);
1369 	}
1370 
1371 	RT_LOCK_ASSERT_HELD(rt);
1372 	rt->rt_flags &= ~RTF_REJECT;
1373 
1374 	/* cache the gateway (sender HW) address */
1375 	nd6_llreach_alloc(rt, ifp, LLADDR(sdl), sdl->sdl_alen, TRUE);
1376 
1377 	/* update the llinfo, send a queued packet if there is one */
1378 	ln->ln_asked = 0;
1379 	if (ln->ln_hold != NULL) {
1380 		mbuf_ref_t m_hold, m_hold_next;
1381 		struct sockaddr_in6 sin6;
1382 
1383 		rtkey_to_sa6(rt, &sin6);
1384 		/*
1385 		 * reset the ln_hold in advance, to explicitly
1386 		 * prevent a ln_hold lookup in nd6_output()
1387 		 * (wouldn't happen, though...)
1388 		 */
1389 		m_hold = ln->ln_hold;
1390 		ln->ln_hold = NULL;
1391 		for (; m_hold; m_hold = m_hold_next) {
1392 			m_hold_next = m_hold->m_nextpkt;
1393 			m_hold->m_nextpkt = NULL;
1394 			/*
1395 			 * we assume ifp is not a loopback here, so just set
1396 			 * the 2nd argument as the 1st one.
1397 			 */
1398 			RT_UNLOCK(rt);
1399 			nd6_output(ifp, ifp, m_hold, &sin6, rt, NULL);
1400 			RT_LOCK_SPIN(rt);
1401 		}
1402 	}
1403 	RT_REMREF_LOCKED(rt);
1404 	RT_UNLOCK(rt);
1405 	m_freem(m);
1406 	return;
1407 
1408 bad:
1409 	icmp6stat.icp6s_badna++;
1410 	/* fall through */
1411 
1412 freeit:
1413 	m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, drop_reason, NULL, 0);
1414 	return;
1415 }
1416 
1417 /*
1418  * Neighbor advertisement output handling.
1419  *
1420  * Based on RFC 2461
1421  *
1422  * the following items are not implemented yet:
1423  * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
1424  * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
1425  *
1426  * tlladdr - 1 if include target link-layer address
1427  * sdl0 - sockaddr_dl (= proxy NA) or NULL
1428  */
1429 void
nd6_na_output(struct ifnet * ifp,const struct in6_addr * daddr6_0,const struct in6_addr * taddr6,uint32_t flags,int tlladdr,struct sockaddr * sdl0)1430 nd6_na_output(
1431 	struct ifnet *ifp,
1432 	const struct in6_addr *daddr6_0,
1433 	const struct in6_addr *taddr6,
1434 	uint32_t flags,
1435 	int tlladdr,            /* 1 if include target link-layer address */
1436 	struct sockaddr *sdl0)  /* sockaddr_dl (= proxy NA) or NULL */
1437 {
1438 	mbuf_ref_t m;
1439 	struct ip6_hdr *ip6;
1440 	struct nd_neighbor_advert *nd_na;
1441 	struct ip6_moptions *__single im6o = NULL;
1442 	caddr_t mac = NULL;
1443 	struct route_in6 ro;
1444 	struct in6_addr *__single src;
1445 	struct in6_addr src_storage, daddr6;
1446 	struct in6_ifaddr *__single ia;
1447 	struct sockaddr_in6 dst_sa;
1448 	int icmp6len, maxlen, error;
1449 	ifnet_ref_t outif = NULL;
1450 
1451 	struct ip6_out_args ip6oa;
1452 	bzero(&ro, sizeof(ro));
1453 
1454 	daddr6 = *daddr6_0;     /* make a local copy for modification */
1455 
1456 	bzero(&ip6oa, sizeof(ip6oa));
1457 	ip6oa.ip6oa_boundif = ifp->if_index;
1458 	ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR |
1459 	    IP6OAF_AWDL_UNRESTRICTED | IP6OAF_INTCOPROC_ALLOWED |
1460 	    IP6OAF_MANAGEMENT_ALLOWED | IP6OAF_ULTRA_CONSTRAINED_ALLOWED;
1461 	ip6oa.ip6oa_sotc = SO_TC_UNSPEC;
1462 	ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
1463 
1464 	ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
1465 
1466 	/* estimate the size of message */
1467 	maxlen = sizeof(*ip6) + sizeof(*nd_na);
1468 	maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
1469 	if (max_linkhdr + maxlen >= MCLBYTES) {
1470 #if DIAGNOSTIC
1471 		printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
1472 		    "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
1473 #endif
1474 		return;
1475 	}
1476 
1477 	MGETHDR(m, M_DONTWAIT, MT_DATA);        /* XXXMAC: mac_create_mbuf_linklayer() probably */
1478 	if (m && max_linkhdr + maxlen >= MHLEN) {
1479 		MCLGET(m, M_DONTWAIT);
1480 		if ((m->m_flags & M_EXT) == 0) {
1481 			m_free(m);
1482 			m = NULL;
1483 		}
1484 	}
1485 	if (m == NULL) {
1486 		return;
1487 	}
1488 	m->m_pkthdr.rcvif = NULL;
1489 
1490 	if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
1491 		m->m_flags |= M_MCAST;
1492 
1493 		im6o = ip6_allocmoptions(Z_NOWAIT);
1494 		if (im6o == NULL) {
1495 			m_freem(m);
1496 			return;
1497 		}
1498 
1499 		im6o->im6o_multicast_ifp = ifp;
1500 		im6o->im6o_multicast_hlim = IPV6_MAXHLIM;
1501 		im6o->im6o_multicast_loop = 0;
1502 	}
1503 
1504 	icmp6len = sizeof(*nd_na);
1505 	m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
1506 	m->m_data += max_linkhdr;       /* or MH_ALIGN() equivalent? */
1507 
1508 	/* fill neighbor advertisement packet */
1509 	ip6 = mtod(m, struct ip6_hdr *);
1510 	ip6->ip6_flow = 0;
1511 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
1512 	ip6->ip6_vfc |= IPV6_VERSION;
1513 	ip6->ip6_nxt = IPPROTO_ICMPV6;
1514 	ip6->ip6_hlim = IPV6_MAXHLIM;
1515 	if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
1516 		/* reply to DAD */
1517 		daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
1518 		daddr6.s6_addr16[1] = 0;
1519 		daddr6.s6_addr32[1] = 0;
1520 		daddr6.s6_addr32[2] = 0;
1521 		daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
1522 		if (in6_setscope(&daddr6, ifp, NULL)) {
1523 			m_drop(m, DROPTAP_FLAG_DIR_OUT | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_SCOPE, NULL, 0);
1524 			goto exit;
1525 		}
1526 
1527 		flags &= ~ND_NA_FLAG_SOLICITED;
1528 	} else {
1529 		ip6->ip6_dst = daddr6;
1530 		ip6_output_setdstifscope(m, ifp->if_index, NULL);
1531 	}
1532 
1533 	SOCKADDR_ZERO(&dst_sa, sizeof(struct sockaddr_in6));
1534 	dst_sa.sin6_family = AF_INET6;
1535 	dst_sa.sin6_len = sizeof(struct sockaddr_in6);
1536 	dst_sa.sin6_addr = daddr6;
1537 
1538 	/*
1539 	 * Select a source whose scope is the same as that of the dest.
1540 	 */
1541 	SOCKADDR_COPY(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
1542 	src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, &src_storage,
1543 	    ip6oa.ip6oa_boundif, &error);
1544 	if (src == NULL) {
1545 		nd6log(info, "nd6_na_output: source can't be "
1546 		    "determined: dst=%s, error=%d\n",
1547 		    ip6_sprintf(&dst_sa.sin6_addr), error);
1548 		m_drop(m, DROPTAP_FLAG_DIR_IN | DROPTAP_FLAG_L2_MISSING, DROP_REASON_IP6_BAD_ND_STATE, NULL, 0);
1549 		goto exit;
1550 	}
1551 	ip6->ip6_src = *src;
1552 
1553 	/*
1554 	 * RFC 4429 requires not setting "override" flag on NA packets sent
1555 	 * from optimistic addresses.
1556 	 */
1557 	ia = in6ifa_ifpwithaddr(ifp, src);
1558 	ip6_output_setsrcifscope(m, ifp->if_index, ia);
1559 	if (ia != NULL) {
1560 		if (ia->ia6_flags & IN6_IFF_OPTIMISTIC) {
1561 			flags &= ~ND_NA_FLAG_OVERRIDE;
1562 		}
1563 		ifa_remref(&ia->ia_ifa);
1564 	}
1565 
1566 	nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
1567 	nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
1568 	nd_na->nd_na_code = 0;
1569 	nd_na->nd_na_target = *taddr6;
1570 	in6_clearscope(&nd_na->nd_na_target); /* XXX */
1571 
1572 	/*
1573 	 * "tlladdr" indicates NS's condition for adding tlladdr or not.
1574 	 * see nd6_ns_input() for details.
1575 	 * Basically, if NS packet is sent to unicast/anycast addr,
1576 	 * target lladdr option SHOULD NOT be included.
1577 	 */
1578 	if (tlladdr) {
1579 		/*
1580 		 * sdl0 != NULL indicates proxy NA.  If we do proxy, use
1581 		 * lladdr in sdl0.  If we are not proxying (sending NA for
1582 		 * my address) use lladdr configured for the interface.
1583 		 */
1584 		if (sdl0 == NULL) {
1585 			mac = nd6_ifptomac(ifp);
1586 		} else if (sdl0->sa_family == AF_LINK) {
1587 			struct sockaddr_dl *sdl;
1588 			sdl = SDL(sdl0);
1589 			if (sdl->sdl_alen == ifp->if_addrlen) {
1590 				mac = LLADDR(sdl);
1591 			}
1592 		}
1593 	}
1594 	if (tlladdr && mac) {
1595 		int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1596 		struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1597 
1598 		/* roundup to 8 bytes alignment! */
1599 		optlen = (optlen + 7) & ~7;
1600 
1601 		m->m_pkthdr.len += optlen;
1602 		m->m_len += optlen;
1603 		icmp6len += optlen;
1604 		bzero((caddr_t)nd_opt, optlen);
1605 		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1606 		nd_opt->nd_opt_len = (uint8_t)(optlen >> 3);
1607 		bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
1608 	} else {
1609 		flags &= ~ND_NA_FLAG_OVERRIDE;
1610 	}
1611 
1612 	ip6->ip6_plen = htons((u_short)icmp6len);
1613 	nd_na->nd_na_flags_reserved = flags;
1614 	nd_na->nd_na_cksum = 0;
1615 	nd_na->nd_na_cksum =
1616 	    in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1617 
1618 	m->m_pkthdr.pkt_flags |= PKTF_INET6_RESOLVE;
1619 
1620 	if (ifp->if_eflags & IFEF_TXSTART) {
1621 		/* Use control service class if the interface supports
1622 		 * transmit-start model.
1623 		 */
1624 		(void) m_set_service_class(m, MBUF_SC_CTL);
1625 	}
1626 
1627 	ip6oa.ip6oa_flags |= IP6OAF_SKIP_PF;
1628 	ip6oa.ip6oa_flags |= IP6OAF_DONT_FRAG;
1629 	ip6_output(m, NULL, NULL, IPV6_OUTARGS, im6o, &outif, &ip6oa);
1630 	if (outif) {
1631 		icmp6_ifstat_inc(outif, ifs6_out_msg);
1632 		icmp6_ifstat_inc(outif, ifs6_out_neighboradvert);
1633 		ifnet_release(outif);
1634 	}
1635 	icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
1636 
1637 exit:
1638 	if (im6o != NULL) {
1639 		IM6O_REMREF(im6o);
1640 	}
1641 
1642 	ROUTE_RELEASE(&ro);
1643 }
1644 
1645 TAILQ_HEAD(dadq_head, dadq);
1646 struct dadq {
1647 	decl_lck_mtx_data(, dad_lock);
1648 	u_int32_t dad_refcount; /* reference count */
1649 	int dad_attached;
1650 	TAILQ_ENTRY(dadq) dad_list;
1651 	struct ifaddr *dad_ifa;
1652 	int dad_count;          /* max NS to send */
1653 	int dad_ns_tcount;      /* # of trials to send NS */
1654 	int dad_ns_ocount;      /* NS sent so far */
1655 	int dad_ns_icount;
1656 	int dad_na_icount;
1657 	int dad_ns_lcount;      /* looped back NS */
1658 	int dad_loopbackprobe;  /* probing state for loopback detection */
1659 	uint8_t dad_lladdr[ETHER_ADDR_LEN];
1660 	uint8_t dad_lladdrlen;
1661 #define ND_OPT_NONCE_LEN32 \
1662     ((ND_OPT_NONCE_LEN + sizeof(uint32_t) - 1)/sizeof(uint32_t))
1663 	uint32_t dad_nonce[ND_OPT_NONCE_LEN32];
1664 	uint32_t dad_same_nonce_count; /* # of consecutive times we've ignored DAD failure because of optimistic DAD  */
1665 };
1666 
1667 static KALLOC_TYPE_DEFINE(dad_zone, struct dadq, NET_KT_DEFAULT);
1668 static struct dadq_head dadq;
1669 
1670 void
nd6_nbr_init(void)1671 nd6_nbr_init(void)
1672 {
1673 	int i;
1674 
1675 	TAILQ_INIT(&dadq);
1676 
1677 	SOCKADDR_ZERO(&hostrtmask, sizeof hostrtmask);
1678 	hostrtmask.sin6_family = AF_INET6;
1679 	hostrtmask.sin6_len = sizeof hostrtmask;
1680 	for (i = 0; i < sizeof hostrtmask.sin6_addr; ++i) {
1681 		hostrtmask.sin6_addr.s6_addr[i] = 0xff;
1682 	}
1683 }
1684 
1685 static struct dadq *
nd6_dad_find(struct ifaddr * ifa,struct nd_opt_nonce * nonce)1686 nd6_dad_find(struct ifaddr *ifa, struct nd_opt_nonce *nonce)
1687 {
1688 	struct dadq *__single dp;
1689 	boolean_t same_nonce = false;
1690 
1691 	lck_mtx_lock(&dad6_mutex);
1692 	for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
1693 		DAD_LOCK_SPIN(dp);
1694 		if (dp->dad_ifa != ifa) {
1695 			DAD_UNLOCK(dp);
1696 			continue;
1697 		}
1698 
1699 		/*
1700 		 * Skip if the nonce matches the received one.
1701 		 * +2 in the length is required because of type and
1702 		 * length fields are included in a header.
1703 		 */
1704 		same_nonce = nonce != NULL &&
1705 		    nonce->nd_opt_nonce_len == (ND_OPT_NONCE_LEN + 2) / 8 &&
1706 		    memcmp(&nonce->nd_opt_nonce[0], &dp->dad_nonce[0],
1707 		    ND_OPT_NONCE_LEN) == 0;
1708 
1709 		if (same_nonce &&
1710 		    dp->dad_same_nonce_count <= nd6_dad_nonce_max_count) {
1711 			nd6log(error, "%s: a looped back NS message is "
1712 			    "detected during DAD for if=%s %s. Ignoring.\n",
1713 			    __func__,
1714 			    if_name(ifa->ifa_ifp),
1715 			    ip6_sprintf(IFA_IN6(ifa)));
1716 			dp->dad_same_nonce_count++;
1717 			dp->dad_ns_lcount++;
1718 			++ip6stat.ip6s_dad_loopcount;
1719 			DAD_UNLOCK(dp);
1720 			continue;
1721 		} else if (!same_nonce) {
1722 			// Not the same nonce, reset counter
1723 			dp->dad_same_nonce_count = 1;
1724 		}
1725 
1726 		DAD_ADDREF_LOCKED(dp);
1727 		DAD_UNLOCK(dp);
1728 		break;
1729 	}
1730 	lck_mtx_unlock(&dad6_mutex);
1731 	return dp;
1732 }
1733 
1734 void
nd6_dad_stoptimer(struct ifaddr * ifa)1735 nd6_dad_stoptimer(
1736 	struct ifaddr *ifa)
1737 {
1738 	untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
1739 }
1740 
1741 /*
1742  * Start Duplicate Address Detection (DAD) for specified interface address.
1743  */
1744 void
nd6_dad_start(struct ifaddr * ifa,int * tick_delay)1745 nd6_dad_start(
1746 	struct ifaddr *ifa,
1747 	int *tick_delay)        /* minimum delay ticks for IFF_UP event */
1748 {
1749 	struct in6_ifaddr *__single ia = ifatoia6(ifa);
1750 	struct dadq *__single dp;
1751 
1752 	if (ifa->ifa_ifp == NULL) {
1753 		panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1754 	}
1755 
1756 	nd6log2(debug, "%s - %s ifp %s ia6_flags 0x%x\n",
1757 	    __func__,
1758 	    ip6_sprintf(&ia->ia_addr.sin6_addr),
1759 	    if_name(ia->ia_ifp),
1760 	    ia->ia6_flags);
1761 
1762 	/*
1763 	 * If we don't need DAD, don't do it.
1764 	 * There are several cases:
1765 	 * - DAD is disabled (ip6_dad_count == 0)
1766 	 * - the interface address is anycast
1767 	 */
1768 	IFA_LOCK(&ia->ia_ifa);
1769 	if (!(ia->ia6_flags & IN6_IFF_DADPROGRESS)) {
1770 		nd6log0(info,
1771 		    "nd6_dad_start: not a tentative or optimistic address "
1772 		    "%s(%s)\n",
1773 		    ip6_sprintf(&ia->ia_addr.sin6_addr),
1774 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1775 		IFA_UNLOCK(&ia->ia_ifa);
1776 		return;
1777 	}
1778 	if (!ip6_dad_count || (ia->ia6_flags & IN6_IFF_ANYCAST) != 0) {
1779 		ia->ia6_flags &= ~IN6_IFF_DADPROGRESS;
1780 		IFA_UNLOCK(&ia->ia_ifa);
1781 		return;
1782 	}
1783 	IFA_UNLOCK(&ia->ia_ifa);
1784 
1785 	if (!(ifa->ifa_ifp->if_flags & IFF_UP) ||
1786 	    (ifa->ifa_ifp->if_eflags & IFEF_IPV6_ND6ALT)) {
1787 		return;
1788 	}
1789 	if ((dp = nd6_dad_find(ifa, NULL)) != NULL) {
1790 		DAD_REMREF(dp);
1791 		/* DAD already in progress */
1792 		return;
1793 	}
1794 
1795 	dp = zalloc_flags(dad_zone, Z_WAITOK | Z_ZERO);
1796 	lck_mtx_init(&dp->dad_lock, &ifa_mtx_grp, &ifa_mtx_attr);
1797 
1798 	/* Callee adds one reference for us */
1799 	dp = nd6_dad_attach(dp, ifa);
1800 
1801 	nd6log0(info, "%s: starting %sDAD %sfor %s\n",
1802 	    if_name(ifa->ifa_ifp),
1803 	    (ia->ia6_flags & IN6_IFF_OPTIMISTIC) ? "optimistic " : "",
1804 	    (tick_delay == NULL) ? "immediately " : "",
1805 	    ip6_sprintf(&ia->ia_addr.sin6_addr));
1806 
1807 	/*
1808 	 * Send NS packet for DAD, ip6_dad_count times.
1809 	 * Note that we must delay the first transmission, if this is the
1810 	 * first packet to be sent from the interface after interface
1811 	 * (re)initialization.
1812 	 */
1813 	if (tick_delay == NULL) {
1814 		u_int32_t retrans;
1815 		struct nd_ifinfo *__single ndi = NULL;
1816 
1817 		nd6_dad_ns_output(dp, ifa);
1818 		ndi = ND_IFINFO(ifa->ifa_ifp);
1819 		VERIFY(ndi != NULL && ndi->initialized);
1820 		lck_mtx_lock(&ndi->lock);
1821 		retrans = ndi->retrans * hz / 1000;
1822 		lck_mtx_unlock(&ndi->lock);
1823 		timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans);
1824 	} else {
1825 		int ntick;
1826 
1827 		if (*tick_delay == 0) {
1828 			ntick = random() % (MAX_RTR_SOLICITATION_DELAY * hz);
1829 		} else {
1830 			ntick = *tick_delay + random() % (hz / 2);
1831 		}
1832 		*tick_delay = ntick;
1833 		timeout((void (*)(void *))nd6_dad_timer, (void *)ifa,
1834 		    ntick);
1835 	}
1836 
1837 	DAD_REMREF(dp);         /* drop our reference */
1838 }
1839 
1840 static struct dadq *
nd6_dad_attach(struct dadq * dp,struct ifaddr * ifa)1841 nd6_dad_attach(struct dadq *dp, struct ifaddr *ifa)
1842 {
1843 	lck_mtx_lock(&dad6_mutex);
1844 	DAD_LOCK(dp);
1845 	dp->dad_ifa = ifa;
1846 	ifa_addref(ifa);        /* for dad_ifa */
1847 	dp->dad_count = ip6_dad_count;
1848 	dp->dad_ns_icount = dp->dad_na_icount = 0;
1849 	dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1850 	dp->dad_ns_lcount = dp->dad_loopbackprobe = 0;
1851 	VERIFY(!dp->dad_attached);
1852 	dp->dad_same_nonce_count = 1;
1853 	dp->dad_attached = 1;
1854 	dp->dad_lladdrlen = 0;
1855 	DAD_ADDREF_LOCKED(dp);  /* for caller */
1856 	DAD_ADDREF_LOCKED(dp);  /* for dadq_head list */
1857 	TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1858 	DAD_UNLOCK(dp);
1859 	lck_mtx_unlock(&dad6_mutex);
1860 
1861 	return dp;
1862 }
1863 
1864 static void
nd6_dad_detach(struct dadq * dp,struct ifaddr * ifa)1865 nd6_dad_detach(struct dadq *dp, struct ifaddr *ifa)
1866 {
1867 	int detached;
1868 
1869 	lck_mtx_lock(&dad6_mutex);
1870 	DAD_LOCK(dp);
1871 	if ((detached = dp->dad_attached)) {
1872 		VERIFY(dp->dad_ifa == ifa);
1873 		TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1874 		dp->dad_list.tqe_next = NULL;
1875 		dp->dad_list.tqe_prev = NULL;
1876 		dp->dad_attached = 0;
1877 	}
1878 	DAD_UNLOCK(dp);
1879 	lck_mtx_unlock(&dad6_mutex);
1880 	if (detached) {
1881 		DAD_REMREF(dp);         /* drop dadq_head reference */
1882 	}
1883 }
1884 
1885 /*
1886  * terminate DAD unconditionally.  used for address removals.
1887  */
1888 void
nd6_dad_stop(struct ifaddr * ifa)1889 nd6_dad_stop(struct ifaddr *ifa)
1890 {
1891 	struct dadq *__single dp;
1892 
1893 	dp = nd6_dad_find(ifa, NULL);
1894 	if (!dp) {
1895 		/* DAD wasn't started yet */
1896 		return;
1897 	}
1898 
1899 	untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
1900 
1901 	nd6_dad_detach(dp, ifa);
1902 	DAD_REMREF(dp);         /* drop our reference */
1903 }
1904 
1905 static void
nd6_unsol_na_output(struct ifaddr * ifa)1906 nd6_unsol_na_output(struct ifaddr *ifa)
1907 {
1908 	struct in6_ifaddr *__single ia = ifatoia6(ifa);
1909 	ifnet_ref_t ifp = ifa->ifa_ifp;
1910 	struct in6_addr saddr6, taddr6;
1911 
1912 	if ((ifp->if_flags & IFF_UP) == 0 ||
1913 	    (ifp->if_flags & IFF_RUNNING) == 0 ||
1914 	    (ifp->if_eflags & IFEF_IPV6_ND6ALT) != 0) {
1915 		return;
1916 	}
1917 
1918 	IFA_LOCK_SPIN(&ia->ia_ifa);
1919 	taddr6 = ia->ia_addr.sin6_addr;
1920 	IFA_UNLOCK(&ia->ia_ifa);
1921 	if (in6_setscope(&taddr6, ifp, NULL) != 0) {
1922 		return;
1923 	}
1924 	saddr6 = in6addr_linklocal_allnodes;
1925 	if (in6_setscope(&saddr6, ifp, NULL) != 0) {
1926 		return;
1927 	}
1928 
1929 	nd6log(info, "%s: sending unsolicited NA\n",
1930 	    if_name(ifa->ifa_ifp));
1931 
1932 	nd6_na_output(ifp, &saddr6, &taddr6, ND_NA_FLAG_OVERRIDE, 1, NULL);
1933 }
1934 
1935 static void
nd6_dad_timer(struct ifaddr * ifa)1936 nd6_dad_timer(struct ifaddr *ifa)
1937 {
1938 	struct in6_ifaddr *__single ia = ifatoia6(ifa);
1939 	struct dadq *__single dp = NULL;
1940 	struct nd_ifinfo *ndi = NULL;
1941 	u_int32_t retrans;
1942 
1943 	/* Sanity check */
1944 	if (ia == NULL) {
1945 		nd6log0(error, "nd6_dad_timer: called with null parameter\n");
1946 		goto done;
1947 	}
1948 
1949 	nd6log2(debug, "%s - %s ifp %s ia6_flags 0x%x\n",
1950 	    __func__,
1951 	    ip6_sprintf(&ia->ia_addr.sin6_addr),
1952 	    if_name(ia->ia_ifp),
1953 	    ia->ia6_flags);
1954 
1955 	dp = nd6_dad_find(ifa, NULL);
1956 	if (dp == NULL) {
1957 		nd6log0(error, "nd6_dad_timer: DAD structure not found\n");
1958 		goto done;
1959 	}
1960 	IFA_LOCK(&ia->ia_ifa);
1961 	if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1962 		nd6log0(error, "nd6_dad_timer: called with duplicated address "
1963 		    "%s(%s)\n",
1964 		    ip6_sprintf(&ia->ia_addr.sin6_addr),
1965 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1966 		IFA_UNLOCK(&ia->ia_ifa);
1967 		goto done;
1968 	}
1969 	if ((ia->ia6_flags & IN6_IFF_DADPROGRESS) == 0) {
1970 		nd6log0(error, "nd6_dad_timer: not a tentative or optimistic "
1971 		    "address %s(%s)\n",
1972 		    ip6_sprintf(&ia->ia_addr.sin6_addr),
1973 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1974 		IFA_UNLOCK(&ia->ia_ifa);
1975 		goto done;
1976 	}
1977 	IFA_UNLOCK(&ia->ia_ifa);
1978 
1979 	/* timeouted with IFF_{RUNNING,UP} check */
1980 	DAD_LOCK(dp);
1981 	if (dp->dad_ns_tcount > dad_maxtry) {
1982 		DAD_UNLOCK(dp);
1983 		nd6log0(info, "%s: could not run DAD, driver problem?\n",
1984 		    if_name(ifa->ifa_ifp));
1985 
1986 		nd6_dad_detach(dp, ifa);
1987 		goto done;
1988 	}
1989 
1990 	/* Need more checks? */
1991 	if (dp->dad_ns_ocount < dp->dad_count) {
1992 		DAD_UNLOCK(dp);
1993 		/*
1994 		 * We have more NS to go.  Send NS packet for DAD.
1995 		 */
1996 		nd6_dad_ns_output(dp, ifa);
1997 		ndi = ND_IFINFO(ifa->ifa_ifp);
1998 		VERIFY(ndi != NULL && ndi->initialized);
1999 		lck_mtx_lock(&ndi->lock);
2000 		retrans = ndi->retrans * hz / 1000;
2001 		lck_mtx_unlock(&ndi->lock);
2002 		timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans);
2003 	} else {
2004 		/*
2005 		 * We have transmitted sufficient number of DAD packets.
2006 		 * See what we've got.
2007 		 */
2008 		if (dp->dad_na_icount > 0 || dp->dad_ns_icount) {
2009 			/* We've seen NS or NA, means DAD has failed. */
2010 			DAD_UNLOCK(dp);
2011 			nd6log0(info,
2012 			    "%s: duplicate IPv6 address %s if:%s [timer]\n",
2013 			    __func__, ip6_sprintf(&ia->ia_addr.sin6_addr),
2014 			    if_name(ia->ia_ifp));
2015 			nd6_dad_duplicated(ifa);
2016 			/* (*dp) will be freed in nd6_dad_duplicated() */
2017 #if SKYWALK
2018 			SK_NXS_MS_IF_ADDR_GENCNT_INC(ia->ia_ifp);
2019 #endif /* SKYWALK */
2020 		} else if (dad_enhanced != 0 &&
2021 		    dp->dad_ns_lcount > 0 &&
2022 		    dp->dad_ns_lcount > dp->dad_loopbackprobe &&
2023 		    dp->dad_same_nonce_count > 0 &&
2024 		    dp->dad_same_nonce_count > nd6_dad_nonce_max_count) {
2025 			dp->dad_loopbackprobe = dp->dad_ns_lcount;
2026 			dp->dad_count =
2027 			    dp->dad_ns_ocount + dad_maxtry - 1;
2028 			DAD_UNLOCK(dp);
2029 			ndi = ND_IFINFO(ifa->ifa_ifp);
2030 			VERIFY(ndi != NULL && ndi->initialized);
2031 			lck_mtx_lock(&ndi->lock);
2032 			retrans = ndi->retrans * hz / 1000;
2033 			lck_mtx_unlock(&ndi->lock);
2034 
2035 			/*
2036 			 * Sec. 4.1 in RFC 7527 requires transmission of
2037 			 * additional probes until the loopback condition
2038 			 * becomes clear when a looped back probe is detected.
2039 			 */
2040 			nd6log0(info,
2041 			    "%s: a looped back NS message is detected during DAD for %s. Another DAD probe is being sent on interface %s.\n",
2042 			    __func__, ip6_sprintf(&ia->ia_addr.sin6_addr),
2043 			    if_name(ia->ia_ifp));
2044 			/*
2045 			 * Send an NS immediately and increase dad_count by
2046 			 * nd6_mmaxtries - 1.
2047 			 */
2048 			nd6_dad_ns_output(dp, ifa);
2049 			timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans);
2050 			goto done;
2051 		} else {
2052 			boolean_t txunsolna;
2053 			DAD_UNLOCK(dp);
2054 			/*
2055 			 * We are done with DAD.  No NA came, no NS came.
2056 			 * No duplicate address found.
2057 			 */
2058 			IFA_LOCK_SPIN(&ia->ia_ifa);
2059 			ia->ia6_flags &= ~IN6_IFF_DADPROGRESS;
2060 			IFA_UNLOCK(&ia->ia_ifa);
2061 
2062 			ndi = ND_IFINFO(ifa->ifa_ifp);
2063 			VERIFY(ndi != NULL && ndi->initialized);
2064 			lck_mtx_lock(&ndi->lock);
2065 			txunsolna = (ndi->flags & ND6_IFF_REPLICATED) != 0;
2066 			lck_mtx_unlock(&ndi->lock);
2067 
2068 			if (txunsolna) {
2069 				nd6_unsol_na_output(ifa);
2070 			}
2071 
2072 			nd6log0(info,
2073 			    "%s: DAD complete for %s - no duplicates found %s\n",
2074 			    if_name(ifa->ifa_ifp),
2075 			    ip6_sprintf(&ia->ia_addr.sin6_addr),
2076 			    txunsolna ? ", tx unsolicited NA with O=1" : ".");
2077 
2078 			if (dp->dad_ns_lcount > 0) {
2079 				nd6log0(info,
2080 				    "%s: DAD completed while "
2081 				    "a looped back NS message is detected "
2082 				    "during DAD for %s om interface %s\n",
2083 				    __func__,
2084 				    ip6_sprintf(&ia->ia_addr.sin6_addr),
2085 				    if_name(ia->ia_ifp));
2086 			}
2087 
2088 			in6_post_msg(ia->ia_ifp, KEV_INET6_NEW_USER_ADDR, ia,
2089 			    dp->dad_lladdr, ETHER_ADDR_LEN);
2090 			nd6_dad_detach(dp, ifa);
2091 #if SKYWALK
2092 			SK_NXS_MS_IF_ADDR_GENCNT_INC(ia->ia_ifp);
2093 #endif /* SKYWALK */
2094 		}
2095 	}
2096 
2097 done:
2098 	if (dp != NULL) {
2099 		DAD_REMREF(dp);         /* drop our reference */
2100 	}
2101 }
2102 
2103 static void
nd6_dad_duplicated(struct ifaddr * ifa)2104 nd6_dad_duplicated(struct ifaddr *ifa)
2105 {
2106 	struct in6_ifaddr *__single ia = ifatoia6(ifa);
2107 	struct dadq *__single dp;
2108 	ifnet_ref_t ifp = ifa->ifa_ifp;
2109 	boolean_t candisable;
2110 
2111 	dp = nd6_dad_find(ifa, NULL);
2112 	if (dp == NULL) {
2113 		log(LOG_ERR, "%s: DAD structure not found.\n", __func__);
2114 		return;
2115 	}
2116 	IFA_LOCK(&ia->ia_ifa);
2117 	DAD_LOCK(dp);
2118 	nd6log(error, "%s: NS in/out/loopback=%d/%d/%d, NA in=%d\n",
2119 	    __func__, dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_ns_lcount,
2120 	    dp->dad_na_icount);
2121 	candisable = FALSE;
2122 
2123 	if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr) &&
2124 	    !(ia->ia6_flags & IN6_IFF_SECURED)) {
2125 		struct in6_addr in6;
2126 		struct ifaddr *__single llifa = NULL;
2127 		struct sockaddr_dl *sdl = NULL;
2128 		uint8_t *lladdr = dp->dad_lladdr;
2129 		uint8_t lladdrlen = dp->dad_lladdrlen;
2130 
2131 		/*
2132 		 * To avoid over-reaction, we only apply this logic when we are
2133 		 * very sure that hardware addresses are supposed to be unique.
2134 		 */
2135 		switch (ifp->if_type) {
2136 		case IFT_BRIDGE:
2137 		case IFT_ETHER:
2138 		case IFT_FDDI:
2139 		case IFT_ATM:
2140 		case IFT_IEEE1394:
2141 #ifdef IFT_IEEE80211
2142 		case IFT_IEEE80211:
2143 #endif
2144 			/*
2145 			 * Check if our hardware address matches the
2146 			 * link layer information received in the
2147 			 * NS/NA
2148 			 */
2149 			llifa = ifp->if_lladdr;
2150 			IFA_LOCK(llifa);
2151 			sdl = SDL(llifa->ifa_addr);
2152 			if (lladdrlen == sdl->sdl_alen &&
2153 			    bcmp(lladdr, LLADDR(sdl), lladdrlen) == 0) {
2154 				candisable = TRUE;
2155 			}
2156 			IFA_UNLOCK(llifa);
2157 
2158 			in6 = ia->ia_addr.sin6_addr;
2159 			if (in6_iid_from_hw(ifp, &in6) != 0) {
2160 				break;
2161 			}
2162 
2163 			/* Refine decision about whether IPv6 can be disabled */
2164 			if (candisable &&
2165 			    !IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
2166 				/*
2167 				 * Apply this logic only to the embedded MAC
2168 				 * address form of link-local IPv6 address.
2169 				 */
2170 				candisable = FALSE;
2171 			} else if (lladdr == NULL &&
2172 			    IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
2173 				/*
2174 				 * We received a NA with no target link-layer
2175 				 * address option. This means that someone else
2176 				 * has our address. Mark it as a hardware
2177 				 * duplicate so we disable IPv6 later on.
2178 				 */
2179 				candisable = TRUE;
2180 			}
2181 			break;
2182 		default:
2183 			break;
2184 		}
2185 	}
2186 	DAD_UNLOCK(dp);
2187 
2188 	ia->ia6_flags &= ~IN6_IFF_DADPROGRESS;
2189 	ia->ia6_flags |= IN6_IFF_DUPLICATED;
2190 	in6_event_enqueue_nwk_wq_entry(IN6_ADDR_MARKED_DUPLICATED,
2191 	    ia->ia_ifa.ifa_ifp, &ia->ia_addr.sin6_addr,
2192 	    0);
2193 	IFA_UNLOCK(&ia->ia_ifa);
2194 
2195 	/* increment DAD collision counter */
2196 	++ip6stat.ip6s_dad_collide;
2197 
2198 	/* We are done with DAD, with duplicated address found. (failure) */
2199 	untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
2200 
2201 	IFA_LOCK(&ia->ia_ifa);
2202 	log(LOG_ERR, "%s: DAD complete for %s - duplicate found.\n",
2203 	    if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
2204 	IFA_UNLOCK(&ia->ia_ifa);
2205 
2206 	if (candisable) {
2207 		struct nd_ifinfo *__single ndi = ND_IFINFO(ifp);
2208 		log(LOG_ERR, "%s: possible hardware address duplication "
2209 		    "detected, disabling IPv6 for interface.\n", if_name(ifp));
2210 
2211 		VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
2212 		ndi->flags |= ND6_IFF_IFDISABLED;
2213 		/* Make sure to set IFEF_IPV6_DISABLED too */
2214 		nd6_if_disable(ifp, TRUE);
2215 	}
2216 
2217 	log(LOG_ERR,
2218 	    "%s: manual intervention may be required.\n",
2219 	    if_name(ifp));
2220 
2221 	/* Send an event to the configuration agent so that the
2222 	 * duplicate address will be notified to the user and will
2223 	 * be removed.
2224 	 */
2225 	in6_post_msg(ifp, KEV_INET6_NEW_USER_ADDR, ia, dp->dad_lladdr, ETHER_ADDR_LEN);
2226 	nd6_dad_detach(dp, ifa);
2227 	DAD_REMREF(dp);         /* drop our reference */
2228 }
2229 
2230 static void
nd6_dad_ns_output(struct dadq * dp,struct ifaddr * ifa)2231 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
2232 {
2233 	struct in6_ifaddr *__single ia = ifatoia6(ifa);
2234 	ifnet_ref_t ifp = ifa->ifa_ifp;
2235 	int i = 0;
2236 	struct in6_addr taddr6;
2237 
2238 	DAD_LOCK(dp);
2239 	dp->dad_ns_tcount++;
2240 	if ((ifp->if_flags & IFF_UP) == 0) {
2241 		DAD_UNLOCK(dp);
2242 		return;
2243 	}
2244 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
2245 		DAD_UNLOCK(dp);
2246 		return;
2247 	}
2248 
2249 	dp->dad_ns_ocount++;
2250 	DAD_UNLOCK(dp);
2251 	IFA_LOCK_SPIN(&ia->ia_ifa);
2252 	taddr6 = ia->ia_addr.sin6_addr;
2253 	IFA_UNLOCK(&ia->ia_ifa);
2254 	if (dad_enhanced != 0 && !(ifp->if_flags & IFF_POINTOPOINT)) {
2255 		for (i = 0; i < ND_OPT_NONCE_LEN32; i++) {
2256 			dp->dad_nonce[i] = RandomULong();
2257 		}
2258 
2259 		/*
2260 		 * XXXHRS: Note that in the case that
2261 		 * DupAddrDetectTransmits > 1, multiple NS messages with
2262 		 * different nonces can be looped back in an unexpected
2263 		 * order.  The current implementation recognizes only
2264 		 * the latest nonce on the sender side.  Practically it
2265 		 * should work well in almost all cases.
2266 		 */
2267 	}
2268 	nd6_ns_output(ifp, NULL, &taddr6, NULL,
2269 	    (uint8_t *)&dp->dad_nonce[0], ND_OPT_NONCE_LEN);
2270 }
2271 
2272 /*
2273  * @brief       Called to process DAD NS
2274  *
2275  * @param       ifa is the pointer to the interface's address
2276  * @param       lladdr is source link layer information
2277  * @param       lladdrlen is source's linklayer length
2278  *
2279  * @return      void
2280  */
2281 static void
nd6_dad_ns_input(struct ifaddr * ifa,char * lladdr __sized_by (lladdrlen),int lladdrlen,struct nd_opt_nonce * ndopt_nonce)2282 nd6_dad_ns_input(struct ifaddr *ifa, char *lladdr __sized_by(lladdrlen),
2283     int lladdrlen, struct nd_opt_nonce *ndopt_nonce)
2284 {
2285 	struct dadq *__single dp;
2286 	VERIFY(ifa != NULL);
2287 
2288 	/* Ignore Nonce option when Enhanced DAD is disabled. */
2289 	if (dad_enhanced == 0) {
2290 		ndopt_nonce = NULL;
2291 	}
2292 
2293 	dp = nd6_dad_find(ifa, ndopt_nonce);
2294 	if (dp == NULL) {
2295 		return;
2296 	}
2297 
2298 	DAD_LOCK(dp);
2299 	++dp->dad_ns_icount;
2300 	if (lladdr && lladdrlen >= ETHER_ADDR_LEN) {
2301 		memcpy(dp->dad_lladdr, lladdr, ETHER_ADDR_LEN);
2302 		/* fine to truncate as it is compared against sdl_alen */
2303 		dp->dad_lladdrlen = (uint8_t)lladdrlen;
2304 	}
2305 	DAD_UNLOCK(dp);
2306 	DAD_REMREF(dp);
2307 }
2308 
2309 /*
2310  * @brief	Called to process received NA for DAD
2311  *
2312  * @param	m is the pointer to the packet's mbuf
2313  * @param	ifp is the pointer to the interface on which packet
2314  *              was receicved.
2315  * @param	taddr is pointer to target's IPv6 address
2316  * @param	lladdr is target's link layer information
2317  * @param	lladdrlen is target's linklayer length
2318  *
2319  * @return	NULL if the packet is consumed by DAD processing, else
2320  *              pointer to the mbuf.
2321  */
2322 static struct mbuf *
nd6_dad_na_input(struct mbuf * m,struct ifnet * ifp,struct in6_addr * taddr,caddr_t lladdr __counted_by (lladdrlen),int lladdrlen)2323 nd6_dad_na_input(struct mbuf *m, struct ifnet *ifp, struct in6_addr *taddr,
2324     caddr_t lladdr __counted_by(lladdrlen), int lladdrlen)
2325 {
2326 	struct ifaddr *__single ifa = NULL;
2327 	struct in6_ifaddr *__single ia = NULL;
2328 	struct dadq *__single dp = NULL;
2329 	struct nd_ifinfo *__single ndi = NULL;
2330 	boolean_t replicated;
2331 
2332 	ifa = (struct ifaddr *) in6ifa_ifpwithaddr(ifp, taddr);
2333 	if (ifa == NULL) {
2334 		return m;
2335 	}
2336 
2337 	replicated = FALSE;
2338 
2339 	/* Get the ND6_IFF_REPLICATED flag. */
2340 	ndi = ND_IFINFO(ifp);
2341 	if (ndi != NULL && ndi->initialized) {
2342 		lck_mtx_lock(&ndi->lock);
2343 		replicated = !!(ndi->flags & ND6_IFF_REPLICATED);
2344 		lck_mtx_unlock(&ndi->lock);
2345 	}
2346 
2347 	if (replicated) {
2348 		nd6log(info, "%s: ignoring duplicate NA on "
2349 		    "replicated interface %s\n", __func__, if_name(ifp));
2350 		goto done;
2351 	}
2352 
2353 	/* Lock the interface address until done (see label below). */
2354 	IFA_LOCK(ifa);
2355 	ia = ifatoia6(ifa);
2356 
2357 	if (!(ia->ia6_flags & IN6_IFF_DADPROGRESS)) {
2358 		IFA_UNLOCK(ifa);
2359 		nd6log(info, "%s: ignoring duplicate NA on "
2360 		    "%s [DAD not in progress]\n", __func__,
2361 		    if_name(ifp));
2362 		goto done;
2363 	}
2364 
2365 	/* Some sleep proxies improperly send the client's Ethernet address in
2366 	 * the target link-layer address option, so detect this by comparing
2367 	 * the L2-header source address, if we have seen it, with the target
2368 	 * address, and ignoring the NA if they don't match.
2369 	 */
2370 	if (lladdr != NULL && lladdrlen >= ETHER_ADDR_LEN) {
2371 		struct ip6aux *__single ip6a = ip6_findaux(m);
2372 		if (ip6a && (ip6a->ip6a_flags & IP6A_HASEEN) != 0 &&
2373 		    bcmp(ip6a->ip6a_ehsrc, lladdr, ETHER_ADDR_LEN) != 0) {
2374 			IFA_UNLOCK(ifa);
2375 			nd6log(error, "%s: ignoring duplicate NA on %s "
2376 			    "[eh_src != tgtlladdr]\n", __func__, if_name(ifp));
2377 			goto done;
2378 		}
2379 	}
2380 
2381 	IFA_UNLOCK(ifa);
2382 
2383 	dp = nd6_dad_find(ifa, NULL);
2384 	if (dp == NULL) {
2385 		nd6log(info, "%s: no DAD structure for %s on %s.\n",
2386 		    __func__, ip6_sprintf(taddr), if_name(ifp));
2387 		goto done;
2388 	}
2389 
2390 	DAD_LOCK_SPIN(dp);
2391 	if (lladdr != NULL && lladdrlen >= ETHER_ADDR_LEN) {
2392 		memcpy(dp->dad_lladdr, lladdr, ETHER_ADDR_LEN);
2393 		dp->dad_lladdrlen = (uint8_t)lladdrlen;
2394 	}
2395 	dp->dad_na_icount++;
2396 	DAD_UNLOCK(dp);
2397 	DAD_REMREF(dp);
2398 
2399 	/* remove the address. */
2400 	nd6log(info,
2401 	    "%s: duplicate IPv6 address %s [processing NA on %s]\n", __func__,
2402 	    ip6_sprintf(taddr), if_name(ifp));
2403 done:
2404 	IFA_LOCK_ASSERT_NOTHELD(ifa);
2405 	ifa_remref(ifa);
2406 	m_freem(m);
2407 	return NULL;
2408 }
2409 
2410 static void
dad_addref(struct dadq * dp,int locked)2411 dad_addref(struct dadq *dp, int locked)
2412 {
2413 	if (!locked) {
2414 		DAD_LOCK_SPIN(dp);
2415 	} else {
2416 		DAD_LOCK_ASSERT_HELD(dp);
2417 	}
2418 
2419 	if (++dp->dad_refcount == 0) {
2420 		panic("%s: dad %p wraparound refcnt", __func__, dp);
2421 		/* NOTREACHED */
2422 	}
2423 	if (!locked) {
2424 		DAD_UNLOCK(dp);
2425 	}
2426 }
2427 
2428 static void
dad_remref(struct dadq * dp)2429 dad_remref(struct dadq *dp)
2430 {
2431 	struct ifaddr *__single ifa;
2432 
2433 	DAD_LOCK_SPIN(dp);
2434 	if (dp->dad_refcount == 0) {
2435 		panic("%s: dad %p negative refcnt", __func__, dp);
2436 	}
2437 	--dp->dad_refcount;
2438 	if (dp->dad_refcount > 0) {
2439 		DAD_UNLOCK(dp);
2440 		return;
2441 	}
2442 	DAD_UNLOCK(dp);
2443 
2444 	if (dp->dad_attached ||
2445 	    dp->dad_list.tqe_next != NULL || dp->dad_list.tqe_prev != NULL) {
2446 		panic("%s: attached dad=%p is being freed", __func__, dp);
2447 		/* NOTREACHED */
2448 	}
2449 
2450 	if ((ifa = dp->dad_ifa) != NULL) {
2451 		ifa_remref(ifa);        /* drop dad_ifa reference */
2452 		dp->dad_ifa = NULL;
2453 	}
2454 
2455 	lck_mtx_destroy(&dp->dad_lock, &ifa_mtx_grp);
2456 	zfree(dad_zone, dp);
2457 }
2458 
2459 void
nd6_llreach_set_reachable(struct ifnet * ifp,void * __sized_by (alen)addr,unsigned int alen)2460 nd6_llreach_set_reachable(struct ifnet *ifp, void *__sized_by(alen) addr, unsigned int alen)
2461 {
2462 	/* Nothing more to do if it's disabled */
2463 	if (nd6_llreach_base == 0) {
2464 		return;
2465 	}
2466 
2467 	ifnet_llreach_set_reachable(ifp, ETHERTYPE_IPV6, addr, alen);
2468 }
2469 
2470 void
nd6_alt_node_addr_decompose(struct ifnet * ifp,struct sockaddr * sa,struct sockaddr_dl * isdl,struct sockaddr_in6 * sin6)2471 nd6_alt_node_addr_decompose(struct ifnet *ifp, struct sockaddr *sa,
2472     struct sockaddr_dl* isdl, struct sockaddr_in6 *sin6)
2473 {
2474 	static const size_t EUI64_LENGTH = 8;
2475 	struct sockaddr_dl *sdl = SDL(isdl);
2476 
2477 	VERIFY(nd6_need_cache(ifp));
2478 	VERIFY(sa);
2479 	VERIFY(sdl && (void *)sa != (void *)sdl);
2480 	VERIFY(sin6 && (void *)sa != (void *)sin6);
2481 
2482 	SOCKADDR_ZERO(sin6, sizeof(*sin6));
2483 	sin6->sin6_len = sizeof *sin6;
2484 	sin6->sin6_family = AF_INET6;
2485 
2486 	SOCKADDR_ZERO(sdl, sizeof(*sdl));
2487 	sdl->sdl_len = sizeof *sdl;
2488 	sdl->sdl_family = AF_LINK;
2489 	sdl->sdl_type = ifp->if_type;
2490 	sdl->sdl_index = ifp->if_index;
2491 	sdl->sdl_nlen = 0;
2492 
2493 	switch (sa->sa_family) {
2494 	case AF_INET6: {
2495 		struct sockaddr_in6 *__single sin6a = SIN6(sa);
2496 		struct in6_addr *__single in6 = &sin6a->sin6_addr;
2497 
2498 		VERIFY(sa->sa_len == sizeof *sin6);
2499 		if (in6->s6_addr[11] == 0xff && in6->s6_addr[12] == 0xfe) {
2500 			sdl->sdl_alen = ETHER_ADDR_LEN;
2501 			LLADDR(sdl)[0] = (in6->s6_addr[8] ^ ND6_EUI64_UBIT);
2502 			LLADDR(sdl)[1] = in6->s6_addr[9];
2503 			LLADDR(sdl)[2] = in6->s6_addr[10];
2504 			LLADDR(sdl)[3] = in6->s6_addr[13];
2505 			LLADDR(sdl)[4] = in6->s6_addr[14];
2506 			LLADDR(sdl)[5] = in6->s6_addr[15];
2507 		} else {
2508 			sdl->sdl_alen = EUI64_LENGTH;
2509 			bcopy(&in6->s6_addr[8], LLADDR(sdl), EUI64_LENGTH);
2510 		}
2511 
2512 		sdl->sdl_slen = 0;
2513 		break;
2514 	}
2515 	case AF_LINK: {
2516 		struct sockaddr_dl *sdla = SDL(sa);
2517 		struct in6_addr *__single in6 = &sin6->sin6_addr;
2518 		caddr_t lla = LLADDR(sdla);
2519 		VERIFY(sa->sa_len <= sizeof(*sdl));
2520 		SOCKADDR_COPY(sa, sdl, sa->sa_len);
2521 
2522 		sin6->sin6_scope_id = sdla->sdl_index;
2523 		if (sin6->sin6_scope_id == 0) {
2524 			sin6->sin6_scope_id = ifp->if_index;
2525 		}
2526 		in6->s6_addr[0] = 0xfe;
2527 		in6->s6_addr[1] = 0x80;
2528 		if (sdla->sdl_alen == EUI64_LENGTH) {
2529 			bcopy(lla, &in6->s6_addr[8], EUI64_LENGTH);
2530 		} else {
2531 			VERIFY(sdla->sdl_alen == ETHER_ADDR_LEN);
2532 
2533 			in6->s6_addr[8] = ((uint8_t) lla[0] ^ ND6_EUI64_UBIT);
2534 			in6->s6_addr[9] = (uint8_t) lla[1];
2535 			in6->s6_addr[10] = (uint8_t) lla[2];
2536 			in6->s6_addr[11] = 0xff;
2537 			in6->s6_addr[12] = 0xfe;
2538 			in6->s6_addr[13] = (uint8_t) lla[3];
2539 			in6->s6_addr[14] = (uint8_t) lla[4];
2540 			in6->s6_addr[15] = (uint8_t) lla[5];
2541 		}
2542 
2543 		break;
2544 	}
2545 	default:
2546 		VERIFY(false);
2547 		break;
2548 	}
2549 }
2550 
2551 int
nd6_alt_node_present(struct ifnet * ifp,struct sockaddr_in6 * sin6,struct sockaddr_dl * isdl,int32_t rssi,int lqm,int npm)2552 nd6_alt_node_present(struct ifnet *ifp, struct sockaddr_in6 *sin6,
2553     struct sockaddr_dl *isdl, int32_t rssi, int lqm, int npm)
2554 {
2555 	struct sockaddr_dl *sdl = SDL(isdl);
2556 	rtentry_ref_t rt = NULL;
2557 	struct llinfo_nd6 *__single ln = NULL;
2558 	struct if_llreach *__single lr = NULL;
2559 	int nd6_nc_updated = 0;
2560 	const uint32_t temp_embedded_id = sin6->sin6_addr.s6_addr16[1];
2561 	const uint32_t temp_ifscope_id = sin6->sin6_scope_id;
2562 
2563 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2564 		if (in6_embedded_scope) {
2565 			if (temp_embedded_id == 0) {
2566 				sin6->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
2567 			}
2568 		} else if (temp_ifscope_id == 0) {
2569 			sin6->sin6_scope_id = ifp->if_index;
2570 		}
2571 	}
2572 
2573 	nd6_cache_lladdr(ifp, &sin6->sin6_addr, LLADDR(sdl), sdl->sdl_alen,
2574 	    ND_NEIGHBOR_ADVERT, 0, &nd6_nc_updated);
2575 
2576 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
2577 	lck_mtx_lock(rnh_lock);
2578 
2579 	rt = rtalloc1_scoped_locked(SA(sin6), 1, 0, ifp->if_index);
2580 
2581 	/* Restore the address that was passed to us */
2582 	if (in6_embedded_scope) {
2583 		if (temp_embedded_id == 0) {
2584 			sin6->sin6_addr.s6_addr16[1] = 0;
2585 		}
2586 	} else if (temp_ifscope_id == 0) {
2587 		sin6->sin6_scope_id = 0;
2588 	}
2589 
2590 	if (rt != NULL) {
2591 		RT_LOCK(rt);
2592 		VERIFY(rt->rt_flags & RTF_LLINFO);
2593 		VERIFY(rt->rt_llinfo);
2594 
2595 		ln = rt->rt_llinfo;
2596 		ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
2597 		ln_setexpire(ln, 0);
2598 
2599 		lr = ln->ln_llreach;
2600 		if (lr) {
2601 			IFLR_LOCK(lr);
2602 			lr->lr_rssi = rssi;
2603 			lr->lr_lqm = (int32_t) lqm;
2604 			lr->lr_npm = (int32_t) npm;
2605 			IFLR_UNLOCK(lr);
2606 		}
2607 
2608 		RT_UNLOCK(rt);
2609 		RT_REMREF(rt);
2610 	}
2611 
2612 	lck_mtx_unlock(rnh_lock);
2613 
2614 	if (rt == NULL) {
2615 		log(LOG_ERR, "%s: failed to add/update host route to %s.\n",
2616 		    __func__, ip6_sprintf(&sin6->sin6_addr));
2617 #if DEBUG || DEVELOPMENT
2618 		if (ip6_p2p_debug) {
2619 			panic("%s: failed to add/update host route to %s.\n",
2620 			    __func__, ip6_sprintf(&sin6->sin6_addr));
2621 		}
2622 #endif
2623 		return EHOSTUNREACH;
2624 	}
2625 
2626 	nd6log(info, "%s: Successfully added/updated host route to %s [lr=0x%llx]\n",
2627 	    __func__, ip6_sprintf(&sin6->sin6_addr),
2628 	    (uint64_t)VM_KERNEL_ADDRPERM(lr));
2629 	/*
2630 	 * nd6_nc_updated not set implies that nothing was updated
2631 	 * in the neighbor cache. Convey that as EEXIST to callers.
2632 	 */
2633 	if (nd6_nc_updated == 0) {
2634 		return EEXIST;
2635 	}
2636 	return 0;
2637 }
2638 
2639 int
nd6_alt_node_absent(struct ifnet * ifp,struct sockaddr_in6 * sin6,struct sockaddr_dl * sdl)2640 nd6_alt_node_absent(struct ifnet *ifp, struct sockaddr_in6 *sin6, struct sockaddr_dl *sdl)
2641 {
2642 	rtentry_ref_t rt = NULL;
2643 	int error = 0;
2644 	const uint32_t temp_embedded_id = sin6->sin6_addr.s6_addr16[1];
2645 	const uint32_t temp_ifscope_id = sin6->sin6_scope_id;
2646 
2647 	nd6log2(debug, "%s: host route to %s\n", __func__,
2648 	    ip6_sprintf(&sin6->sin6_addr));
2649 
2650 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2651 		if (in6_embedded_scope) {
2652 			if (temp_embedded_id == 0) {
2653 				sin6->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
2654 			}
2655 		} else if (temp_ifscope_id == 0) {
2656 			sin6->sin6_scope_id = ifp->if_index;
2657 		}
2658 	}
2659 
2660 	LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
2661 	lck_mtx_lock(rnh_lock);
2662 
2663 	rt = rtalloc1_scoped_locked(SA(sin6), 0, 0, ifp->if_index);
2664 
2665 	/* Restore the address that was passed to us */
2666 	if (in6_embedded_scope) {
2667 		if (temp_embedded_id == 0) {
2668 			sin6->sin6_addr.s6_addr16[1] = 0;
2669 		}
2670 	} else if (temp_ifscope_id == 0) {
2671 		sin6->sin6_scope_id = 0;
2672 	}
2673 
2674 	if (rt != NULL) {
2675 		RT_LOCK(rt);
2676 		if (IS_DYNAMIC_DIRECT_HOSTROUTE(rt)) {
2677 			/*
2678 			 * Copy the link layer information in SDL when present
2679 			 * as it later gets used to issue the kernel event for
2680 			 * node absence.
2681 			 */
2682 			if (sdl != NULL && rt->rt_gateway != NULL &&
2683 			    rt->rt_gateway->sa_family == AF_LINK &&
2684 			    SDL(rt->rt_gateway)->sdl_len <= sizeof(*sdl)) {
2685 				SOCKADDR_COPY(rt->rt_gateway, sdl, SDL(rt->rt_gateway)->sdl_len);
2686 			}
2687 
2688 			rt->rt_flags |= RTF_CONDEMNED;
2689 			RT_UNLOCK(rt);
2690 
2691 			error = rtrequest_locked(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
2692 			    0, (struct rtentry **)NULL);
2693 
2694 			rtfree_locked(rt);
2695 		} else {
2696 			error = EHOSTUNREACH;
2697 			RT_REMREF_LOCKED(rt);
2698 			RT_UNLOCK(rt);
2699 		}
2700 	} else {
2701 		error = EHOSTUNREACH;
2702 	}
2703 
2704 	if (error == 0) {
2705 		nd6log(info, "%s: Successfully deleted host route to %s "
2706 		    "for interface %s.\n", __func__, ip6_sprintf(&sin6->sin6_addr),
2707 		    ifp->if_xname);
2708 	} else {
2709 		nd6log(error, "%s: Failed to delete host route to %s "
2710 		    "for interface %s with error :%d.\n", __func__,
2711 		    ip6_sprintf(&sin6->sin6_addr),
2712 		    ifp->if_xname, error);
2713 	}
2714 
2715 	lck_mtx_unlock(rnh_lock);
2716 	return error;
2717 }
2718