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