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