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