1 /*
2 * Copyright (c) 2000-2021 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 /*
1104 * Enqueue work item to invoke callback for this
1105 * route entry
1106 */
1107 route_event_enqueue_nwk_wq_entry(rt, NULL,
1108 ROUTE_LLENTRY_RESOLVED, NULL, TRUE);
1109
1110 if ((ln->ln_router = (short)is_router) != 0) {
1111 struct radix_node_head *rnh = NULL;
1112 struct route_event rt_ev;
1113 route_event_init(&rt_ev, rt, NULL, ROUTE_LLENTRY_RESOLVED);
1114 /*
1115 * This means a router's state has changed from
1116 * non-reachable to probably reachable, and might
1117 * affect the status of associated prefixes..
1118 * We already have a reference on rt. Don't need to
1119 * take one for the unlock/lock.
1120 */
1121 RT_UNLOCK(rt);
1122 lck_mtx_lock(rnh_lock);
1123 rnh = rt_tables[AF_INET6];
1124
1125 if (rnh != NULL) {
1126 (void) rnh->rnh_walktree(rnh, route_event_walktree,
1127 (void *)&rt_ev);
1128 }
1129 lck_mtx_unlock(rnh_lock);
1130 lck_mtx_lock(nd6_mutex);
1131 pfxlist_onlink_check();
1132 lck_mtx_unlock(nd6_mutex);
1133 RT_LOCK(rt);
1134 }
1135 } else {
1136 int llchange = 0;
1137
1138 /*
1139 * Check if the link-layer address has changed or not.
1140 */
1141 if (lladdr == NULL) {
1142 llchange = 0;
1143 } else {
1144 if (sdl->sdl_alen) {
1145 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen)) {
1146 llchange = 1;
1147 } else {
1148 llchange = 0;
1149 }
1150 } else {
1151 llchange = 1;
1152 }
1153 }
1154
1155 /*
1156 * This is VERY complex. Look at it with care.
1157 *
1158 * override solicit lladdr llchange action
1159 * (L: record lladdr)
1160 *
1161 * 0 0 n -- (2c)
1162 * 0 0 y n (2b) L
1163 * 0 0 y y (1) REACHABLE->STALE
1164 * 0 1 n -- (2c) *->REACHABLE
1165 * 0 1 y n (2b) L *->REACHABLE
1166 * 0 1 y y (1) REACHABLE->STALE
1167 * 1 0 n -- (2a)
1168 * 1 0 y n (2a) L
1169 * 1 0 y y (2a) L *->STALE
1170 * 1 1 n -- (2a) *->REACHABLE
1171 * 1 1 y n (2a) L *->REACHABLE
1172 * 1 1 y y (2a) L *->REACHABLE
1173 */
1174 if (!is_override && (lladdr != NULL && llchange)) { /* (1) */
1175 /*
1176 * If state is REACHABLE, make it STALE.
1177 * no other updates should be done.
1178 */
1179 if (ln->ln_state == ND6_LLINFO_REACHABLE) {
1180 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);
1181 ln_setexpire(ln, timenow + nd6_gctimer);
1182 }
1183 RT_REMREF_LOCKED(rt);
1184 RT_UNLOCK(rt);
1185 goto freeit;
1186 } else if (is_override /* (2a) */
1187 || (!is_override && (lladdr && !llchange)) /* (2b) */
1188 || !lladdr) { /* (2c) */
1189 /*
1190 * Update link-local address, if any.
1191 */
1192 if (lladdr) {
1193 sdl->sdl_alen = ifp->if_addrlen;
1194 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1195 }
1196
1197 /*
1198 * If solicited, make the state REACHABLE.
1199 * If not solicited and the link-layer address was
1200 * changed, make it STALE.
1201 */
1202 if (is_solicited) {
1203 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
1204 if (ln->ln_expire != 0) {
1205 struct nd_ifinfo *ndi = NULL;
1206
1207 ndi = ND_IFINFO(ifp);
1208 VERIFY(ndi != NULL && ndi->initialized);
1209 lck_mtx_lock(&ndi->lock);
1210 ln_setexpire(ln,
1211 timenow + ndi->reachable);
1212 lck_mtx_unlock(&ndi->lock);
1213 RT_UNLOCK(rt);
1214 lck_mtx_lock(rnh_lock);
1215 nd6_sched_timeout(NULL, NULL);
1216 lck_mtx_unlock(rnh_lock);
1217 RT_LOCK(rt);
1218 }
1219 } else {
1220 if (lladdr && llchange) {
1221 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_STALE);
1222 ln_setexpire(ln, timenow + nd6_gctimer);
1223 }
1224 }
1225
1226 /*
1227 * XXX
1228 * The above is somewhat convoluted, for now just
1229 * issue a callback for LLENTRY changed.
1230 */
1231 /* Enqueue work item to invoke callback for this route entry */
1232 if (llchange) {
1233 route_event_enqueue_nwk_wq_entry(rt, NULL,
1234 ROUTE_LLENTRY_CHANGED, NULL, TRUE);
1235 }
1236
1237 /*
1238 * If the router's link-layer address has changed,
1239 * notify routes using this as gateway so they can
1240 * update any cached information.
1241 */
1242 if (ln->ln_router && is_router && llchange) {
1243 struct radix_node_head *rnh = NULL;
1244 struct route_event rt_ev;
1245 route_event_init(&rt_ev, rt, NULL, ROUTE_LLENTRY_CHANGED);
1246 /*
1247 * This means a router's state has changed from
1248 * non-reachable to probably reachable, and might
1249 * affect the status of associated prefixes..
1250 *
1251 * We already have a valid rt reference here.
1252 * We don't need to take another one for unlock/lock.
1253 */
1254 RT_UNLOCK(rt);
1255 lck_mtx_lock(rnh_lock);
1256 rnh = rt_tables[AF_INET6];
1257
1258 if (rnh != NULL) {
1259 (void) rnh->rnh_walktree(rnh, route_event_walktree,
1260 (void *)&rt_ev);
1261 }
1262 lck_mtx_unlock(rnh_lock);
1263 RT_LOCK(rt);
1264 }
1265 }
1266
1267 if (ln->ln_router && !is_router) {
1268 /*
1269 * The peer dropped the router flag.
1270 * Remove the sender from the Default Router List and
1271 * update the Destination Cache entries.
1272 */
1273 struct nd_defrouter *dr;
1274 struct in6_addr *in6;
1275 struct ifnet *rt_ifp = rt->rt_ifp;
1276
1277 in6 = &((struct sockaddr_in6 *)
1278 (void *)rt_key(rt))->sin6_addr;
1279
1280 RT_UNLOCK(rt);
1281 lck_mtx_lock(nd6_mutex);
1282 /*
1283 * XXX Handle router lists for route information option
1284 * as well.
1285 */
1286 dr = defrouter_lookup(NULL, in6, rt_ifp);
1287 if (dr) {
1288 TAILQ_REMOVE(&nd_defrouter_list, dr, dr_entry);
1289 defrtrlist_del(dr, NULL);
1290 NDDR_REMREF(dr); /* remove list reference */
1291 NDDR_REMREF(dr);
1292 lck_mtx_unlock(nd6_mutex);
1293 } else {
1294 lck_mtx_unlock(nd6_mutex);
1295 /*
1296 * Even if the neighbor is not in the
1297 * default router list, the neighbor
1298 * may be used as a next hop for some
1299 * destinations (e.g. redirect case).
1300 * So we must call rt6_flush explicitly.
1301 */
1302 rt6_flush(&ip6->ip6_src, rt_ifp);
1303 }
1304 RT_LOCK(rt);
1305 }
1306 ln->ln_router = (short)is_router;
1307 }
1308
1309 if (send_nc_alive_kev && (ifp->if_addrlen == IF_LLREACH_MAXLEN)) {
1310 struct kev_msg ev_msg;
1311 struct kev_nd6_ndalive nd6_ndalive;
1312 bzero(&ev_msg, sizeof(ev_msg));
1313 bzero(&nd6_ndalive, sizeof(nd6_ndalive));
1314 ev_msg.vendor_code = KEV_VENDOR_APPLE;
1315 ev_msg.kev_class = KEV_NETWORK_CLASS;
1316 ev_msg.kev_subclass = KEV_ND6_SUBCLASS;
1317 ev_msg.event_code = KEV_ND6_NDALIVE;
1318
1319 nd6_ndalive.link_data.if_family = ifp->if_family;
1320 nd6_ndalive.link_data.if_unit = ifp->if_unit;
1321 strlcpy(nd6_ndalive.link_data.if_name,
1322 ifp->if_name,
1323 sizeof(nd6_ndalive.link_data.if_name));
1324 ev_msg.dv[0].data_ptr = &nd6_ndalive;
1325 ev_msg.dv[0].data_length =
1326 sizeof(nd6_ndalive);
1327 dlil_post_complete_msg(NULL, &ev_msg);
1328 }
1329
1330 RT_LOCK_ASSERT_HELD(rt);
1331 rt->rt_flags &= ~RTF_REJECT;
1332
1333 /* cache the gateway (sender HW) address */
1334 nd6_llreach_alloc(rt, ifp, LLADDR(sdl), sdl->sdl_alen, TRUE);
1335
1336 /* update the llinfo, send a queued packet if there is one */
1337 ln->ln_asked = 0;
1338 if (ln->ln_hold != NULL) {
1339 struct mbuf *m_hold, *m_hold_next;
1340 struct sockaddr_in6 sin6;
1341
1342 rtkey_to_sa6(rt, &sin6);
1343 /*
1344 * reset the ln_hold in advance, to explicitly
1345 * prevent a ln_hold lookup in nd6_output()
1346 * (wouldn't happen, though...)
1347 */
1348 m_hold = ln->ln_hold;
1349 ln->ln_hold = NULL;
1350 for (; m_hold; m_hold = m_hold_next) {
1351 m_hold_next = m_hold->m_nextpkt;
1352 m_hold->m_nextpkt = NULL;
1353 /*
1354 * we assume ifp is not a loopback here, so just set
1355 * the 2nd argument as the 1st one.
1356 */
1357 RT_UNLOCK(rt);
1358 nd6_output(ifp, ifp, m_hold, &sin6, rt, NULL);
1359 RT_LOCK_SPIN(rt);
1360 }
1361 }
1362 RT_REMREF_LOCKED(rt);
1363 RT_UNLOCK(rt);
1364 m_freem(m);
1365 return;
1366
1367 bad:
1368 icmp6stat.icp6s_badna++;
1369 /* fall through */
1370 freeit:
1371 m_freem(m);
1372 return;
1373 }
1374
1375 /*
1376 * Neighbor advertisement output handling.
1377 *
1378 * Based on RFC 2461
1379 *
1380 * the following items are not implemented yet:
1381 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
1382 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
1383 *
1384 * tlladdr - 1 if include target link-layer address
1385 * sdl0 - sockaddr_dl (= proxy NA) or NULL
1386 */
1387 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)1388 nd6_na_output(
1389 struct ifnet *ifp,
1390 const struct in6_addr *daddr6_0,
1391 const struct in6_addr *taddr6,
1392 uint32_t flags,
1393 int tlladdr, /* 1 if include target link-layer address */
1394 struct sockaddr *sdl0) /* sockaddr_dl (= proxy NA) or NULL */
1395 {
1396 struct mbuf *m;
1397 struct ip6_hdr *ip6;
1398 struct nd_neighbor_advert *nd_na;
1399 struct ip6_moptions *im6o = NULL;
1400 caddr_t mac = NULL;
1401 struct route_in6 ro;
1402 struct in6_addr *src, src_storage, daddr6;
1403 struct in6_ifaddr *ia;
1404 struct sockaddr_in6 dst_sa;
1405 int icmp6len, maxlen, error;
1406 struct ifnet *outif = NULL;
1407
1408 struct ip6_out_args ip6oa;
1409 bzero(&ro, sizeof(ro));
1410
1411 daddr6 = *daddr6_0; /* make a local copy for modification */
1412
1413 bzero(&ip6oa, sizeof(ip6oa));
1414 ip6oa.ip6oa_boundif = ifp->if_index;
1415 ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR |
1416 IP6OAF_AWDL_UNRESTRICTED | IP6OAF_INTCOPROC_ALLOWED;
1417 ip6oa.ip6oa_sotc = SO_TC_UNSPEC;
1418 ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC;
1419
1420 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
1421
1422 /* estimate the size of message */
1423 maxlen = sizeof(*ip6) + sizeof(*nd_na);
1424 maxlen += (sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7;
1425 if (max_linkhdr + maxlen >= MCLBYTES) {
1426 #if DIAGNOSTIC
1427 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
1428 "(%d + %d > %d)\n", max_linkhdr, maxlen, MCLBYTES);
1429 #endif
1430 return;
1431 }
1432
1433 MGETHDR(m, M_DONTWAIT, MT_DATA); /* XXXMAC: mac_create_mbuf_linklayer() probably */
1434 if (m && max_linkhdr + maxlen >= MHLEN) {
1435 MCLGET(m, M_DONTWAIT);
1436 if ((m->m_flags & M_EXT) == 0) {
1437 m_free(m);
1438 m = NULL;
1439 }
1440 }
1441 if (m == NULL) {
1442 return;
1443 }
1444 m->m_pkthdr.rcvif = NULL;
1445
1446 if (IN6_IS_ADDR_MULTICAST(&daddr6)) {
1447 m->m_flags |= M_MCAST;
1448
1449 im6o = ip6_allocmoptions(Z_NOWAIT);
1450 if (im6o == NULL) {
1451 m_freem(m);
1452 return;
1453 }
1454
1455 im6o->im6o_multicast_ifp = ifp;
1456 im6o->im6o_multicast_hlim = IPV6_MAXHLIM;
1457 im6o->im6o_multicast_loop = 0;
1458 }
1459
1460 icmp6len = sizeof(*nd_na);
1461 m->m_pkthdr.len = m->m_len = sizeof(struct ip6_hdr) + icmp6len;
1462 m->m_data += max_linkhdr; /* or MH_ALIGN() equivalent? */
1463
1464 /* fill neighbor advertisement packet */
1465 ip6 = mtod(m, struct ip6_hdr *);
1466 ip6->ip6_flow = 0;
1467 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
1468 ip6->ip6_vfc |= IPV6_VERSION;
1469 ip6->ip6_nxt = IPPROTO_ICMPV6;
1470 ip6->ip6_hlim = IPV6_MAXHLIM;
1471 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6)) {
1472 /* reply to DAD */
1473 daddr6.s6_addr16[0] = IPV6_ADDR_INT16_MLL;
1474 daddr6.s6_addr16[1] = 0;
1475 daddr6.s6_addr32[1] = 0;
1476 daddr6.s6_addr32[2] = 0;
1477 daddr6.s6_addr32[3] = IPV6_ADDR_INT32_ONE;
1478 if (in6_setscope(&daddr6, ifp, NULL)) {
1479 goto bad;
1480 }
1481
1482 flags &= ~ND_NA_FLAG_SOLICITED;
1483 } else {
1484 ip6->ip6_dst = daddr6;
1485 ip6_output_setdstifscope(m, ifp->if_index, NULL);
1486 }
1487
1488 bzero(&dst_sa, sizeof(struct sockaddr_in6));
1489 dst_sa.sin6_family = AF_INET6;
1490 dst_sa.sin6_len = sizeof(struct sockaddr_in6);
1491 dst_sa.sin6_addr = daddr6;
1492
1493 /*
1494 * Select a source whose scope is the same as that of the dest.
1495 */
1496 bcopy(&dst_sa, &ro.ro_dst, sizeof(dst_sa));
1497 src = in6_selectsrc(&dst_sa, NULL, NULL, &ro, NULL, &src_storage,
1498 ip6oa.ip6oa_boundif, &error);
1499 if (src == NULL) {
1500 nd6log(debug, "nd6_na_output: source can't be "
1501 "determined: dst=%s, error=%d\n",
1502 ip6_sprintf(&dst_sa.sin6_addr), error);
1503 goto bad;
1504 }
1505 ip6->ip6_src = *src;
1506
1507 /*
1508 * RFC 4429 requires not setting "override" flag on NA packets sent
1509 * from optimistic addresses.
1510 */
1511 ia = in6ifa_ifpwithaddr(ifp, src);
1512 ip6_output_setsrcifscope(m, ifp->if_index, ia);
1513 if (ia != NULL) {
1514 if (ia->ia6_flags & IN6_IFF_OPTIMISTIC) {
1515 flags &= ~ND_NA_FLAG_OVERRIDE;
1516 }
1517 IFA_REMREF(&ia->ia_ifa);
1518 }
1519
1520 nd_na = (struct nd_neighbor_advert *)(ip6 + 1);
1521 nd_na->nd_na_type = ND_NEIGHBOR_ADVERT;
1522 nd_na->nd_na_code = 0;
1523 nd_na->nd_na_target = *taddr6;
1524 in6_clearscope(&nd_na->nd_na_target); /* XXX */
1525
1526 /*
1527 * "tlladdr" indicates NS's condition for adding tlladdr or not.
1528 * see nd6_ns_input() for details.
1529 * Basically, if NS packet is sent to unicast/anycast addr,
1530 * target lladdr option SHOULD NOT be included.
1531 */
1532 if (tlladdr) {
1533 /*
1534 * sdl0 != NULL indicates proxy NA. If we do proxy, use
1535 * lladdr in sdl0. If we are not proxying (sending NA for
1536 * my address) use lladdr configured for the interface.
1537 */
1538 if (sdl0 == NULL) {
1539 mac = nd6_ifptomac(ifp);
1540 } else if (sdl0->sa_family == AF_LINK) {
1541 struct sockaddr_dl *sdl;
1542 sdl = (struct sockaddr_dl *)(void *)sdl0;
1543 if (sdl->sdl_alen == ifp->if_addrlen) {
1544 mac = LLADDR(sdl);
1545 }
1546 }
1547 }
1548 if (tlladdr && mac) {
1549 int optlen = sizeof(struct nd_opt_hdr) + ifp->if_addrlen;
1550 struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd_na + 1);
1551
1552 /* roundup to 8 bytes alignment! */
1553 optlen = (optlen + 7) & ~7;
1554
1555 m->m_pkthdr.len += optlen;
1556 m->m_len += optlen;
1557 icmp6len += optlen;
1558 bzero((caddr_t)nd_opt, optlen);
1559 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1560 nd_opt->nd_opt_len = (uint8_t)(optlen >> 3);
1561 bcopy(mac, (caddr_t)(nd_opt + 1), ifp->if_addrlen);
1562 } else {
1563 flags &= ~ND_NA_FLAG_OVERRIDE;
1564 }
1565
1566 ip6->ip6_plen = htons((u_short)icmp6len);
1567 nd_na->nd_na_flags_reserved = flags;
1568 nd_na->nd_na_cksum = 0;
1569 nd_na->nd_na_cksum =
1570 in6_cksum(m, IPPROTO_ICMPV6, sizeof(struct ip6_hdr), icmp6len);
1571
1572 m->m_pkthdr.pkt_flags |= PKTF_INET6_RESOLVE;
1573
1574 if (ifp->if_eflags & IFEF_TXSTART) {
1575 /* Use control service class if the interface supports
1576 * transmit-start model.
1577 */
1578 (void) m_set_service_class(m, MBUF_SC_CTL);
1579 }
1580
1581 ip6oa.ip6oa_flags |= IP6OAF_SKIP_PF;
1582 ip6oa.ip6oa_flags |= IP6OAF_DONT_FRAG;
1583 ip6_output(m, NULL, NULL, IPV6_OUTARGS, im6o, &outif, &ip6oa);
1584 if (outif) {
1585 icmp6_ifstat_inc(outif, ifs6_out_msg);
1586 icmp6_ifstat_inc(outif, ifs6_out_neighboradvert);
1587 ifnet_release(outif);
1588 }
1589 icmp6stat.icp6s_outhist[ND_NEIGHBOR_ADVERT]++;
1590
1591 exit:
1592 if (im6o != NULL) {
1593 IM6O_REMREF(im6o);
1594 }
1595
1596 ROUTE_RELEASE(&ro);
1597 return;
1598
1599 bad:
1600 m_freem(m);
1601 goto exit;
1602 }
1603
1604 caddr_t
nd6_ifptomac(struct ifnet * ifp)1605 nd6_ifptomac(
1606 struct ifnet *ifp)
1607 {
1608 switch (ifp->if_type) {
1609 case IFT_ARCNET:
1610 case IFT_ETHER:
1611 case IFT_IEEE8023ADLAG:
1612 case IFT_FDDI:
1613 case IFT_IEEE1394:
1614 #ifdef IFT_L2VLAN
1615 case IFT_L2VLAN:
1616 #endif
1617 #ifdef IFT_IEEE80211
1618 case IFT_IEEE80211:
1619 #endif
1620 #ifdef IFT_CARP
1621 case IFT_CARP:
1622 #endif
1623 case IFT_BRIDGE:
1624 case IFT_ISO88025:
1625 case IFT_6LOWPAN:
1626 return (caddr_t)IF_LLADDR(ifp);
1627 default:
1628 return NULL;
1629 }
1630 }
1631
1632 TAILQ_HEAD(dadq_head, dadq);
1633 struct dadq {
1634 decl_lck_mtx_data(, dad_lock);
1635 u_int32_t dad_refcount; /* reference count */
1636 int dad_attached;
1637 TAILQ_ENTRY(dadq) dad_list;
1638 struct ifaddr *dad_ifa;
1639 int dad_count; /* max NS to send */
1640 int dad_ns_tcount; /* # of trials to send NS */
1641 int dad_ns_ocount; /* NS sent so far */
1642 int dad_ns_icount;
1643 int dad_na_icount;
1644 int dad_ns_lcount; /* looped back NS */
1645 int dad_loopbackprobe; /* probing state for loopback detection */
1646 uint8_t dad_lladdr[ETHER_ADDR_LEN];
1647 uint8_t dad_lladdrlen;
1648 #define ND_OPT_NONCE_LEN32 \
1649 ((ND_OPT_NONCE_LEN + sizeof(uint32_t) - 1)/sizeof(uint32_t))
1650 uint32_t dad_nonce[ND_OPT_NONCE_LEN32];
1651 };
1652
1653 static ZONE_DEFINE_TYPE(dad_zone, "nd6_dad", struct dadq, ZC_ZFREE_CLEARMEM);
1654 static struct dadq_head dadq;
1655
1656 void
nd6_nbr_init(void)1657 nd6_nbr_init(void)
1658 {
1659 int i;
1660
1661 TAILQ_INIT(&dadq);
1662
1663 bzero(&hostrtmask, sizeof hostrtmask);
1664 hostrtmask.sin6_family = AF_INET6;
1665 hostrtmask.sin6_len = sizeof hostrtmask;
1666 for (i = 0; i < sizeof hostrtmask.sin6_addr; ++i) {
1667 hostrtmask.sin6_addr.s6_addr[i] = 0xff;
1668 }
1669 }
1670
1671 static struct dadq *
nd6_dad_find(struct ifaddr * ifa,struct nd_opt_nonce * nonce)1672 nd6_dad_find(struct ifaddr *ifa, struct nd_opt_nonce *nonce)
1673 {
1674 struct dadq *dp;
1675
1676 lck_mtx_lock(&dad6_mutex);
1677 for (dp = dadq.tqh_first; dp; dp = dp->dad_list.tqe_next) {
1678 DAD_LOCK_SPIN(dp);
1679 if (dp->dad_ifa != ifa) {
1680 DAD_UNLOCK(dp);
1681 continue;
1682 }
1683
1684 /*
1685 * Skip if the nonce matches the received one.
1686 * +2 in the length is required because of type and
1687 * length fields are included in a header.
1688 */
1689 if (nonce != NULL &&
1690 nonce->nd_opt_nonce_len == (ND_OPT_NONCE_LEN + 2) / 8 &&
1691 memcmp(&nonce->nd_opt_nonce[0], &dp->dad_nonce[0],
1692 ND_OPT_NONCE_LEN) == 0) {
1693 nd6log(error, "%s: a looped back NS message is "
1694 "detected during DAD for %s. Ignoring.\n",
1695 if_name(ifa->ifa_ifp),
1696 ip6_sprintf(IFA_IN6(ifa)));
1697 dp->dad_ns_lcount++;
1698 ++ip6stat.ip6s_dad_loopcount;
1699 DAD_UNLOCK(dp);
1700 continue;
1701 }
1702
1703 DAD_ADDREF_LOCKED(dp);
1704 DAD_UNLOCK(dp);
1705 break;
1706 }
1707 lck_mtx_unlock(&dad6_mutex);
1708 return dp;
1709 }
1710
1711 void
nd6_dad_stoptimer(struct ifaddr * ifa)1712 nd6_dad_stoptimer(
1713 struct ifaddr *ifa)
1714 {
1715 untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
1716 }
1717
1718 /*
1719 * Start Duplicate Address Detection (DAD) for specified interface address.
1720 */
1721 void
nd6_dad_start(struct ifaddr * ifa,int * tick_delay)1722 nd6_dad_start(
1723 struct ifaddr *ifa,
1724 int *tick_delay) /* minimum delay ticks for IFF_UP event */
1725 {
1726 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1727 struct dadq *dp;
1728
1729 if (ifa->ifa_ifp == NULL) {
1730 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1731 }
1732
1733 nd6log2(debug, "%s - %s ifp %s ia6_flags 0x%x\n",
1734 __func__,
1735 ip6_sprintf(&ia->ia_addr.sin6_addr),
1736 if_name(ia->ia_ifp),
1737 ia->ia6_flags);
1738
1739 /*
1740 * If we don't need DAD, don't do it.
1741 * There are several cases:
1742 * - DAD is disabled (ip6_dad_count == 0)
1743 * - the interface address is anycast
1744 */
1745 IFA_LOCK(&ia->ia_ifa);
1746 if (!(ia->ia6_flags & IN6_IFF_DADPROGRESS)) {
1747 nd6log0(debug,
1748 "nd6_dad_start: not a tentative or optimistic address "
1749 "%s(%s)\n",
1750 ip6_sprintf(&ia->ia_addr.sin6_addr),
1751 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1752 IFA_UNLOCK(&ia->ia_ifa);
1753 return;
1754 }
1755 if (!ip6_dad_count || (ia->ia6_flags & IN6_IFF_ANYCAST) != 0) {
1756 ia->ia6_flags &= ~IN6_IFF_DADPROGRESS;
1757 IFA_UNLOCK(&ia->ia_ifa);
1758 return;
1759 }
1760 IFA_UNLOCK(&ia->ia_ifa);
1761
1762 if (!(ifa->ifa_ifp->if_flags & IFF_UP) ||
1763 (ifa->ifa_ifp->if_eflags & IFEF_IPV6_ND6ALT)) {
1764 return;
1765 }
1766 if ((dp = nd6_dad_find(ifa, NULL)) != NULL) {
1767 DAD_REMREF(dp);
1768 /* DAD already in progress */
1769 return;
1770 }
1771
1772 dp = zalloc_flags(dad_zone, Z_WAITOK | Z_ZERO);
1773 lck_mtx_init(&dp->dad_lock, &ifa_mtx_grp, &ifa_mtx_attr);
1774
1775 /* Callee adds one reference for us */
1776 dp = nd6_dad_attach(dp, ifa);
1777
1778 nd6log0(debug, "%s: starting %sDAD %sfor %s\n",
1779 if_name(ifa->ifa_ifp),
1780 (ia->ia6_flags & IN6_IFF_OPTIMISTIC) ? "optimistic " : "",
1781 (tick_delay == NULL) ? "immediately " : "",
1782 ip6_sprintf(&ia->ia_addr.sin6_addr));
1783
1784 /*
1785 * Send NS packet for DAD, ip6_dad_count times.
1786 * Note that we must delay the first transmission, if this is the
1787 * first packet to be sent from the interface after interface
1788 * (re)initialization.
1789 */
1790 if (tick_delay == NULL) {
1791 u_int32_t retrans;
1792 struct nd_ifinfo *ndi = NULL;
1793
1794 nd6_dad_ns_output(dp, ifa);
1795 ndi = ND_IFINFO(ifa->ifa_ifp);
1796 VERIFY(ndi != NULL && ndi->initialized);
1797 lck_mtx_lock(&ndi->lock);
1798 retrans = ndi->retrans * hz / 1000;
1799 lck_mtx_unlock(&ndi->lock);
1800 timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans);
1801 } else {
1802 int ntick;
1803
1804 if (*tick_delay == 0) {
1805 ntick = random() % (MAX_RTR_SOLICITATION_DELAY * hz);
1806 } else {
1807 ntick = *tick_delay + random() % (hz / 2);
1808 }
1809 *tick_delay = ntick;
1810 timeout((void (*)(void *))nd6_dad_timer, (void *)ifa,
1811 ntick);
1812 }
1813
1814 DAD_REMREF(dp); /* drop our reference */
1815 }
1816
1817 static struct dadq *
nd6_dad_attach(struct dadq * dp,struct ifaddr * ifa)1818 nd6_dad_attach(struct dadq *dp, struct ifaddr *ifa)
1819 {
1820 lck_mtx_lock(&dad6_mutex);
1821 DAD_LOCK(dp);
1822 dp->dad_ifa = ifa;
1823 IFA_ADDREF(ifa); /* for dad_ifa */
1824 dp->dad_count = ip6_dad_count;
1825 dp->dad_ns_icount = dp->dad_na_icount = 0;
1826 dp->dad_ns_ocount = dp->dad_ns_tcount = 0;
1827 dp->dad_ns_lcount = dp->dad_loopbackprobe = 0;
1828 VERIFY(!dp->dad_attached);
1829 dp->dad_attached = 1;
1830 dp->dad_lladdrlen = 0;
1831 DAD_ADDREF_LOCKED(dp); /* for caller */
1832 DAD_ADDREF_LOCKED(dp); /* for dadq_head list */
1833 TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1834 DAD_UNLOCK(dp);
1835 lck_mtx_unlock(&dad6_mutex);
1836
1837 return dp;
1838 }
1839
1840 static void
nd6_dad_detach(struct dadq * dp,struct ifaddr * ifa)1841 nd6_dad_detach(struct dadq *dp, struct ifaddr *ifa)
1842 {
1843 int detached;
1844
1845 lck_mtx_lock(&dad6_mutex);
1846 DAD_LOCK(dp);
1847 if ((detached = dp->dad_attached)) {
1848 VERIFY(dp->dad_ifa == ifa);
1849 TAILQ_REMOVE(&dadq, (struct dadq *)dp, dad_list);
1850 dp->dad_list.tqe_next = NULL;
1851 dp->dad_list.tqe_prev = NULL;
1852 dp->dad_attached = 0;
1853 }
1854 DAD_UNLOCK(dp);
1855 lck_mtx_unlock(&dad6_mutex);
1856 if (detached) {
1857 DAD_REMREF(dp); /* drop dadq_head reference */
1858 }
1859 }
1860
1861 /*
1862 * terminate DAD unconditionally. used for address removals.
1863 */
1864 void
nd6_dad_stop(struct ifaddr * ifa)1865 nd6_dad_stop(struct ifaddr *ifa)
1866 {
1867 struct dadq *dp;
1868
1869 dp = nd6_dad_find(ifa, NULL);
1870 if (!dp) {
1871 /* DAD wasn't started yet */
1872 return;
1873 }
1874
1875 untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
1876
1877 nd6_dad_detach(dp, ifa);
1878 DAD_REMREF(dp); /* drop our reference */
1879 }
1880
1881 static void
nd6_unsol_na_output(struct ifaddr * ifa)1882 nd6_unsol_na_output(struct ifaddr *ifa)
1883 {
1884 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1885 struct ifnet *ifp = ifa->ifa_ifp;
1886 struct in6_addr saddr6, taddr6;
1887
1888 if ((ifp->if_flags & IFF_UP) == 0 ||
1889 (ifp->if_flags & IFF_RUNNING) == 0 ||
1890 (ifp->if_eflags & IFEF_IPV6_ND6ALT) != 0) {
1891 return;
1892 }
1893
1894 IFA_LOCK_SPIN(&ia->ia_ifa);
1895 taddr6 = ia->ia_addr.sin6_addr;
1896 IFA_UNLOCK(&ia->ia_ifa);
1897 if (in6_setscope(&taddr6, ifp, NULL) != 0) {
1898 return;
1899 }
1900 saddr6 = in6addr_linklocal_allnodes;
1901 if (in6_setscope(&saddr6, ifp, NULL) != 0) {
1902 return;
1903 }
1904
1905 nd6log(info, "%s: sending unsolicited NA\n",
1906 if_name(ifa->ifa_ifp));
1907
1908 nd6_na_output(ifp, &saddr6, &taddr6, ND_NA_FLAG_OVERRIDE, 1, NULL);
1909 }
1910
1911 static void
nd6_dad_timer(struct ifaddr * ifa)1912 nd6_dad_timer(struct ifaddr *ifa)
1913 {
1914 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
1915 struct dadq *dp = NULL;
1916 struct nd_ifinfo *ndi = NULL;
1917 u_int32_t retrans;
1918
1919 /* Sanity check */
1920 if (ia == NULL) {
1921 nd6log0(error, "nd6_dad_timer: called with null parameter\n");
1922 goto done;
1923 }
1924
1925 nd6log2(debug, "%s - %s ifp %s ia6_flags 0x%x\n",
1926 __func__,
1927 ip6_sprintf(&ia->ia_addr.sin6_addr),
1928 if_name(ia->ia_ifp),
1929 ia->ia6_flags);
1930
1931 dp = nd6_dad_find(ifa, NULL);
1932 if (dp == NULL) {
1933 nd6log0(error, "nd6_dad_timer: DAD structure not found\n");
1934 goto done;
1935 }
1936 IFA_LOCK(&ia->ia_ifa);
1937 if (ia->ia6_flags & IN6_IFF_DUPLICATED) {
1938 nd6log0(error, "nd6_dad_timer: called with duplicated address "
1939 "%s(%s)\n",
1940 ip6_sprintf(&ia->ia_addr.sin6_addr),
1941 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1942 IFA_UNLOCK(&ia->ia_ifa);
1943 goto done;
1944 }
1945 if ((ia->ia6_flags & IN6_IFF_DADPROGRESS) == 0) {
1946 nd6log0(error, "nd6_dad_timer: not a tentative or optimistic "
1947 "address %s(%s)\n",
1948 ip6_sprintf(&ia->ia_addr.sin6_addr),
1949 ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1950 IFA_UNLOCK(&ia->ia_ifa);
1951 goto done;
1952 }
1953 IFA_UNLOCK(&ia->ia_ifa);
1954
1955 /* timeouted with IFF_{RUNNING,UP} check */
1956 DAD_LOCK(dp);
1957 if (dp->dad_ns_tcount > dad_maxtry) {
1958 DAD_UNLOCK(dp);
1959 nd6log0(info, "%s: could not run DAD, driver problem?\n",
1960 if_name(ifa->ifa_ifp));
1961
1962 nd6_dad_detach(dp, ifa);
1963 goto done;
1964 }
1965
1966 /* Need more checks? */
1967 if (dp->dad_ns_ocount < dp->dad_count) {
1968 DAD_UNLOCK(dp);
1969 /*
1970 * We have more NS to go. Send NS packet for DAD.
1971 */
1972 nd6_dad_ns_output(dp, ifa);
1973 ndi = ND_IFINFO(ifa->ifa_ifp);
1974 VERIFY(ndi != NULL && ndi->initialized);
1975 lck_mtx_lock(&ndi->lock);
1976 retrans = ndi->retrans * hz / 1000;
1977 lck_mtx_unlock(&ndi->lock);
1978 timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans);
1979 } else {
1980 /*
1981 * We have transmitted sufficient number of DAD packets.
1982 * See what we've got.
1983 */
1984 if (dp->dad_na_icount > 0 || dp->dad_ns_icount) {
1985 /* We've seen NS or NA, means DAD has failed. */
1986 DAD_UNLOCK(dp);
1987 nd6log0(info,
1988 "%s: duplicate IPv6 address %s if:%s [timer]\n",
1989 __func__, ip6_sprintf(&ia->ia_addr.sin6_addr),
1990 if_name(ia->ia_ifp));
1991 nd6_dad_duplicated(ifa);
1992 /* (*dp) will be freed in nd6_dad_duplicated() */
1993 #if SKYWALK
1994 SK_NXS_MS_IF_ADDR_GENCNT_INC(ia->ia_ifp);
1995 #endif /* SKYWALK */
1996 } else if (dad_enhanced != 0 &&
1997 dp->dad_ns_lcount > 0 &&
1998 dp->dad_ns_lcount > dp->dad_loopbackprobe) {
1999 dp->dad_loopbackprobe = dp->dad_ns_lcount;
2000 dp->dad_count =
2001 dp->dad_ns_ocount + dad_maxtry - 1;
2002 DAD_UNLOCK(dp);
2003 ndi = ND_IFINFO(ifa->ifa_ifp);
2004 VERIFY(ndi != NULL && ndi->initialized);
2005 lck_mtx_lock(&ndi->lock);
2006 retrans = ndi->retrans * hz / 1000;
2007 lck_mtx_unlock(&ndi->lock);
2008
2009 /*
2010 * Sec. 4.1 in RFC 7527 requires transmission of
2011 * additional probes until the loopback condition
2012 * becomes clear when a looped back probe is detected.
2013 */
2014 nd6log0(info,
2015 "%s: a looped back NS message is detected during DAD for %s. Another DAD probe is being sent on interface %s.\n",
2016 __func__, ip6_sprintf(&ia->ia_addr.sin6_addr),
2017 if_name(ia->ia_ifp));
2018 /*
2019 * Send an NS immediately and increase dad_count by
2020 * nd6_mmaxtries - 1.
2021 */
2022 nd6_dad_ns_output(dp, ifa);
2023 timeout((void (*)(void *))nd6_dad_timer, (void *)ifa, retrans);
2024 goto done;
2025 } else {
2026 boolean_t txunsolna;
2027 DAD_UNLOCK(dp);
2028 /*
2029 * We are done with DAD. No NA came, no NS came.
2030 * No duplicate address found.
2031 */
2032 IFA_LOCK_SPIN(&ia->ia_ifa);
2033 ia->ia6_flags &= ~IN6_IFF_DADPROGRESS;
2034 IFA_UNLOCK(&ia->ia_ifa);
2035
2036 ndi = ND_IFINFO(ifa->ifa_ifp);
2037 VERIFY(ndi != NULL && ndi->initialized);
2038 lck_mtx_lock(&ndi->lock);
2039 txunsolna = (ndi->flags & ND6_IFF_REPLICATED) != 0;
2040 lck_mtx_unlock(&ndi->lock);
2041
2042 if (txunsolna) {
2043 nd6_unsol_na_output(ifa);
2044 }
2045
2046 nd6log0(debug,
2047 "%s: DAD complete for %s - no duplicates found %s\n",
2048 if_name(ifa->ifa_ifp),
2049 ip6_sprintf(&ia->ia_addr.sin6_addr),
2050 txunsolna ? ", tx unsolicited NA with O=1" : ".");
2051
2052 if (dp->dad_ns_lcount > 0) {
2053 nd6log0(debug,
2054 "%s: DAD completed while "
2055 "a looped back NS message is detected "
2056 "during DAD for %s om interface %s\n",
2057 __func__,
2058 ip6_sprintf(&ia->ia_addr.sin6_addr),
2059 if_name(ia->ia_ifp));
2060 }
2061
2062 in6_post_msg(ia->ia_ifp, KEV_INET6_NEW_USER_ADDR, ia,
2063 dp->dad_lladdr);
2064 nd6_dad_detach(dp, ifa);
2065 #if SKYWALK
2066 SK_NXS_MS_IF_ADDR_GENCNT_INC(ia->ia_ifp);
2067 #endif /* SKYWALK */
2068 }
2069 }
2070
2071 done:
2072 if (dp != NULL) {
2073 DAD_REMREF(dp); /* drop our reference */
2074 }
2075 }
2076
2077 static void
nd6_dad_duplicated(struct ifaddr * ifa)2078 nd6_dad_duplicated(struct ifaddr *ifa)
2079 {
2080 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
2081 struct dadq *dp;
2082 struct ifnet *ifp = ifa->ifa_ifp;
2083 boolean_t candisable;
2084
2085 dp = nd6_dad_find(ifa, NULL);
2086 if (dp == NULL) {
2087 log(LOG_ERR, "%s: DAD structure not found.\n", __func__);
2088 return;
2089 }
2090 IFA_LOCK(&ia->ia_ifa);
2091 DAD_LOCK(dp);
2092 nd6log(error, "%s: NS in/out/loopback=%d/%d/%d, NA in=%d\n",
2093 __func__, dp->dad_ns_icount, dp->dad_ns_ocount, dp->dad_ns_lcount,
2094 dp->dad_na_icount);
2095 candisable = FALSE;
2096
2097 if (IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr) &&
2098 !(ia->ia6_flags & IN6_IFF_SECURED)) {
2099 struct in6_addr in6;
2100 struct ifaddr *llifa = NULL;
2101 struct sockaddr_dl *sdl = NULL;
2102 uint8_t *lladdr = dp->dad_lladdr;
2103 uint8_t lladdrlen = dp->dad_lladdrlen;
2104
2105 /*
2106 * To avoid over-reaction, we only apply this logic when we are
2107 * very sure that hardware addresses are supposed to be unique.
2108 */
2109 switch (ifp->if_type) {
2110 case IFT_BRIDGE:
2111 case IFT_ETHER:
2112 case IFT_FDDI:
2113 case IFT_ATM:
2114 case IFT_IEEE1394:
2115 #ifdef IFT_IEEE80211
2116 case IFT_IEEE80211:
2117 #endif
2118 /*
2119 * Check if our hardware address matches the
2120 * link layer information received in the
2121 * NS/NA
2122 */
2123 llifa = ifp->if_lladdr;
2124 IFA_LOCK(llifa);
2125 sdl = (struct sockaddr_dl *)(void *)
2126 llifa->ifa_addr;
2127 if (lladdrlen == sdl->sdl_alen &&
2128 bcmp(lladdr, LLADDR(sdl), lladdrlen) == 0) {
2129 candisable = TRUE;
2130 }
2131 IFA_UNLOCK(llifa);
2132
2133 in6 = ia->ia_addr.sin6_addr;
2134 if (in6_iid_from_hw(ifp, &in6) != 0) {
2135 break;
2136 }
2137
2138 /* Refine decision about whether IPv6 can be disabled */
2139 if (candisable &&
2140 !IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
2141 /*
2142 * Apply this logic only to the embedded MAC
2143 * address form of link-local IPv6 address.
2144 */
2145 candisable = FALSE;
2146 } else if (lladdr == NULL &&
2147 IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &in6)) {
2148 /*
2149 * We received a NA with no target link-layer
2150 * address option. This means that someone else
2151 * has our address. Mark it as a hardware
2152 * duplicate so we disable IPv6 later on.
2153 */
2154 candisable = TRUE;
2155 }
2156 break;
2157 default:
2158 break;
2159 }
2160 }
2161 DAD_UNLOCK(dp);
2162
2163 ia->ia6_flags &= ~IN6_IFF_DADPROGRESS;
2164 ia->ia6_flags |= IN6_IFF_DUPLICATED;
2165 in6_event_enqueue_nwk_wq_entry(IN6_ADDR_MARKED_DUPLICATED,
2166 ia->ia_ifa.ifa_ifp, &ia->ia_addr.sin6_addr,
2167 0);
2168 IFA_UNLOCK(&ia->ia_ifa);
2169
2170 /* increment DAD collision counter */
2171 ++ip6stat.ip6s_dad_collide;
2172
2173 /* We are done with DAD, with duplicated address found. (failure) */
2174 untimeout((void (*)(void *))nd6_dad_timer, (void *)ifa);
2175
2176 IFA_LOCK(&ia->ia_ifa);
2177 log(LOG_ERR, "%s: DAD complete for %s - duplicate found.\n",
2178 if_name(ifp), ip6_sprintf(&ia->ia_addr.sin6_addr));
2179 IFA_UNLOCK(&ia->ia_ifa);
2180
2181 if (candisable) {
2182 struct nd_ifinfo *ndi = ND_IFINFO(ifp);
2183 log(LOG_ERR, "%s: possible hardware address duplication "
2184 "detected, disabling IPv6 for interface.\n", if_name(ifp));
2185
2186 VERIFY((NULL != ndi) && (TRUE == ndi->initialized));
2187 ndi->flags |= ND6_IFF_IFDISABLED;
2188 /* Make sure to set IFEF_IPV6_DISABLED too */
2189 nd6_if_disable(ifp, TRUE);
2190 }
2191
2192 log(LOG_ERR,
2193 "%s: manual intervention may be required.\n",
2194 if_name(ifp));
2195
2196 /* Send an event to the configuration agent so that the
2197 * duplicate address will be notified to the user and will
2198 * be removed.
2199 */
2200 in6_post_msg(ifp, KEV_INET6_NEW_USER_ADDR, ia, dp->dad_lladdr);
2201 nd6_dad_detach(dp, ifa);
2202 DAD_REMREF(dp); /* drop our reference */
2203 }
2204
2205 static void
nd6_dad_ns_output(struct dadq * dp,struct ifaddr * ifa)2206 nd6_dad_ns_output(struct dadq *dp, struct ifaddr *ifa)
2207 {
2208 struct in6_ifaddr *ia = (struct in6_ifaddr *)ifa;
2209 struct ifnet *ifp = ifa->ifa_ifp;
2210 int i = 0;
2211 struct in6_addr taddr6;
2212
2213 DAD_LOCK(dp);
2214 dp->dad_ns_tcount++;
2215 if ((ifp->if_flags & IFF_UP) == 0) {
2216 DAD_UNLOCK(dp);
2217 return;
2218 }
2219 if ((ifp->if_flags & IFF_RUNNING) == 0) {
2220 DAD_UNLOCK(dp);
2221 return;
2222 }
2223
2224 dp->dad_ns_ocount++;
2225 DAD_UNLOCK(dp);
2226 IFA_LOCK_SPIN(&ia->ia_ifa);
2227 taddr6 = ia->ia_addr.sin6_addr;
2228 IFA_UNLOCK(&ia->ia_ifa);
2229 if (dad_enhanced != 0 && !(ifp->if_flags & IFF_POINTOPOINT)) {
2230 for (i = 0; i < ND_OPT_NONCE_LEN32; i++) {
2231 dp->dad_nonce[i] = RandomULong();
2232 }
2233 /*
2234 * XXXHRS: Note that in the case that
2235 * DupAddrDetectTransmits > 1, multiple NS messages with
2236 * different nonces can be looped back in an unexpected
2237 * order. The current implementation recognizes only
2238 * the latest nonce on the sender side. Practically it
2239 * should work well in almost all cases.
2240 */
2241 }
2242 nd6_ns_output(ifp, NULL, &taddr6, NULL,
2243 (uint8_t *)&dp->dad_nonce[0]);
2244 }
2245
2246 /*
2247 * @brief Called to process DAD NS
2248 *
2249 * @param ifa is the pointer to the interface's address
2250 * @param lladdr is source link layer information
2251 * @param lladdrlen is source's linklayer length
2252 *
2253 * @return void
2254 */
2255 static void
nd6_dad_ns_input(struct ifaddr * ifa,char * lladdr,int lladdrlen,struct nd_opt_nonce * ndopt_nonce)2256 nd6_dad_ns_input(struct ifaddr *ifa, char *lladdr,
2257 int lladdrlen, struct nd_opt_nonce *ndopt_nonce)
2258 {
2259 struct dadq *dp;
2260 VERIFY(ifa != NULL);
2261
2262 /* Ignore Nonce option when Enhanced DAD is disabled. */
2263 if (dad_enhanced == 0) {
2264 ndopt_nonce = NULL;
2265 }
2266
2267 dp = nd6_dad_find(ifa, ndopt_nonce);
2268 if (dp == NULL) {
2269 return;
2270 }
2271
2272 DAD_LOCK(dp);
2273 ++dp->dad_ns_icount;
2274 if (lladdr && lladdrlen >= ETHER_ADDR_LEN) {
2275 memcpy(dp->dad_lladdr, lladdr, ETHER_ADDR_LEN);
2276 /* fine to truncate as it is compared against sdl_alen */
2277 dp->dad_lladdrlen = (uint8_t)lladdrlen;
2278 }
2279 DAD_UNLOCK(dp);
2280 DAD_REMREF(dp);
2281 }
2282
2283 /*
2284 * @brief Called to process received NA for DAD
2285 *
2286 * @param m is the pointer to the packet's mbuf
2287 * @param ifp is the pointer to the interface on which packet
2288 * was receicved.
2289 * @param taddr is pointer to target's IPv6 address
2290 * @param lladdr is target's link layer information
2291 * @param lladdrlen is target's linklayer length
2292 *
2293 * @return NULL if the packet is consumed by DAD processing, else
2294 * pointer to the mbuf.
2295 */
2296 static struct mbuf *
nd6_dad_na_input(struct mbuf * m,struct ifnet * ifp,struct in6_addr * taddr,caddr_t lladdr,int lladdrlen)2297 nd6_dad_na_input(struct mbuf *m, struct ifnet *ifp, struct in6_addr *taddr,
2298 caddr_t lladdr, int lladdrlen)
2299 {
2300 struct ifaddr *ifa = NULL;
2301 struct in6_ifaddr *ia = NULL;
2302 struct dadq *dp = NULL;
2303 struct nd_ifinfo *ndi = NULL;
2304 boolean_t replicated;
2305
2306 ifa = (struct ifaddr *) in6ifa_ifpwithaddr(ifp, taddr);
2307 if (ifa == NULL) {
2308 return m;
2309 }
2310
2311 replicated = FALSE;
2312
2313 /* Get the ND6_IFF_REPLICATED flag. */
2314 ndi = ND_IFINFO(ifp);
2315 if (ndi != NULL && ndi->initialized) {
2316 lck_mtx_lock(&ndi->lock);
2317 replicated = !!(ndi->flags & ND6_IFF_REPLICATED);
2318 lck_mtx_unlock(&ndi->lock);
2319 }
2320
2321 if (replicated) {
2322 nd6log(info, "%s: ignoring duplicate NA on "
2323 "replicated interface %s\n", __func__, if_name(ifp));
2324 goto done;
2325 }
2326
2327 /* Lock the interface address until done (see label below). */
2328 IFA_LOCK(ifa);
2329 ia = (struct in6_ifaddr *) ifa;
2330
2331 if (!(ia->ia6_flags & IN6_IFF_DADPROGRESS)) {
2332 IFA_UNLOCK(ifa);
2333 nd6log(info, "%s: ignoring duplicate NA on "
2334 "%s [DAD not in progress]\n", __func__,
2335 if_name(ifp));
2336 goto done;
2337 }
2338
2339 /* Some sleep proxies improperly send the client's Ethernet address in
2340 * the target link-layer address option, so detect this by comparing
2341 * the L2-header source address, if we have seen it, with the target
2342 * address, and ignoring the NA if they don't match.
2343 */
2344 if (lladdr != NULL && lladdrlen >= ETHER_ADDR_LEN) {
2345 struct ip6aux *ip6a = ip6_findaux(m);
2346 if (ip6a && (ip6a->ip6a_flags & IP6A_HASEEN) != 0 &&
2347 bcmp(ip6a->ip6a_ehsrc, lladdr, ETHER_ADDR_LEN) != 0) {
2348 IFA_UNLOCK(ifa);
2349 nd6log(error, "%s: ignoring duplicate NA on %s "
2350 "[eh_src != tgtlladdr]\n", __func__, if_name(ifp));
2351 goto done;
2352 }
2353 }
2354
2355 IFA_UNLOCK(ifa);
2356
2357 dp = nd6_dad_find(ifa, NULL);
2358 if (dp == NULL) {
2359 nd6log(info, "%s: no DAD structure for %s on %s.\n",
2360 __func__, ip6_sprintf(taddr), if_name(ifp));
2361 goto done;
2362 }
2363
2364 DAD_LOCK_SPIN(dp);
2365 if (lladdr != NULL && lladdrlen >= ETHER_ADDR_LEN) {
2366 memcpy(dp->dad_lladdr, lladdr, ETHER_ADDR_LEN);
2367 dp->dad_lladdrlen = (uint8_t)lladdrlen;
2368 }
2369 dp->dad_na_icount++;
2370 DAD_UNLOCK(dp);
2371 DAD_REMREF(dp);
2372
2373 /* remove the address. */
2374 nd6log(info,
2375 "%s: duplicate IPv6 address %s [processing NA on %s]\n", __func__,
2376 ip6_sprintf(taddr), if_name(ifp));
2377 done:
2378 IFA_LOCK_ASSERT_NOTHELD(ifa);
2379 IFA_REMREF(ifa);
2380 m_freem(m);
2381 return NULL;
2382 }
2383
2384 static void
dad_addref(struct dadq * dp,int locked)2385 dad_addref(struct dadq *dp, int locked)
2386 {
2387 if (!locked) {
2388 DAD_LOCK_SPIN(dp);
2389 } else {
2390 DAD_LOCK_ASSERT_HELD(dp);
2391 }
2392
2393 if (++dp->dad_refcount == 0) {
2394 panic("%s: dad %p wraparound refcnt", __func__, dp);
2395 /* NOTREACHED */
2396 }
2397 if (!locked) {
2398 DAD_UNLOCK(dp);
2399 }
2400 }
2401
2402 static void
dad_remref(struct dadq * dp)2403 dad_remref(struct dadq *dp)
2404 {
2405 struct ifaddr *ifa;
2406
2407 DAD_LOCK_SPIN(dp);
2408 if (dp->dad_refcount == 0) {
2409 panic("%s: dad %p negative refcnt", __func__, dp);
2410 }
2411 --dp->dad_refcount;
2412 if (dp->dad_refcount > 0) {
2413 DAD_UNLOCK(dp);
2414 return;
2415 }
2416 DAD_UNLOCK(dp);
2417
2418 if (dp->dad_attached ||
2419 dp->dad_list.tqe_next != NULL || dp->dad_list.tqe_prev != NULL) {
2420 panic("%s: attached dad=%p is being freed", __func__, dp);
2421 /* NOTREACHED */
2422 }
2423
2424 if ((ifa = dp->dad_ifa) != NULL) {
2425 IFA_REMREF(ifa); /* drop dad_ifa reference */
2426 dp->dad_ifa = NULL;
2427 }
2428
2429 lck_mtx_destroy(&dp->dad_lock, &ifa_mtx_grp);
2430 zfree(dad_zone, dp);
2431 }
2432
2433 void
nd6_llreach_set_reachable(struct ifnet * ifp,void * addr,unsigned int alen)2434 nd6_llreach_set_reachable(struct ifnet *ifp, void *addr, unsigned int alen)
2435 {
2436 /* Nothing more to do if it's disabled */
2437 if (nd6_llreach_base == 0) {
2438 return;
2439 }
2440
2441 ifnet_llreach_set_reachable(ifp, ETHERTYPE_IPV6, addr, alen);
2442 }
2443
2444 void
nd6_alt_node_addr_decompose(struct ifnet * ifp,struct sockaddr * sa,struct sockaddr_dl * sdl,struct sockaddr_in6 * sin6)2445 nd6_alt_node_addr_decompose(struct ifnet *ifp, struct sockaddr *sa,
2446 struct sockaddr_dl* sdl, struct sockaddr_in6 *sin6)
2447 {
2448 static const size_t EUI64_LENGTH = 8;
2449
2450 VERIFY(nd6_need_cache(ifp));
2451 VERIFY(sa);
2452 VERIFY(sdl && (void *)sa != (void *)sdl);
2453 VERIFY(sin6 && (void *)sa != (void *)sin6);
2454
2455 bzero(sin6, sizeof(*sin6));
2456 sin6->sin6_len = sizeof *sin6;
2457 sin6->sin6_family = AF_INET6;
2458
2459 bzero(sdl, sizeof(*sdl));
2460 sdl->sdl_len = sizeof *sdl;
2461 sdl->sdl_family = AF_LINK;
2462 sdl->sdl_type = ifp->if_type;
2463 sdl->sdl_index = ifp->if_index;
2464
2465 switch (sa->sa_family) {
2466 case AF_INET6: {
2467 struct sockaddr_in6 *sin6a = (struct sockaddr_in6 *)(void *)sa;
2468 struct in6_addr *in6 = &sin6a->sin6_addr;
2469
2470 VERIFY(sa->sa_len == sizeof *sin6);
2471 VERIFY(strlen(ifp->if_name) <= IFNAMSIZ);
2472
2473 sdl->sdl_nlen = (u_char)strlen(ifp->if_name);
2474 bcopy(ifp->if_name, sdl->sdl_data, sdl->sdl_nlen);
2475 if (in6->s6_addr[11] == 0xff && in6->s6_addr[12] == 0xfe) {
2476 sdl->sdl_alen = ETHER_ADDR_LEN;
2477 LLADDR(sdl)[0] = (in6->s6_addr[8] ^ ND6_EUI64_UBIT);
2478 LLADDR(sdl)[1] = in6->s6_addr[9];
2479 LLADDR(sdl)[2] = in6->s6_addr[10];
2480 LLADDR(sdl)[3] = in6->s6_addr[13];
2481 LLADDR(sdl)[4] = in6->s6_addr[14];
2482 LLADDR(sdl)[5] = in6->s6_addr[15];
2483 } else {
2484 sdl->sdl_alen = EUI64_LENGTH;
2485 bcopy(&in6->s6_addr[8], LLADDR(sdl), EUI64_LENGTH);
2486 }
2487
2488 sdl->sdl_slen = 0;
2489 break;
2490 }
2491 case AF_LINK: {
2492 struct sockaddr_dl *sdla = (struct sockaddr_dl *)(void *)sa;
2493 struct in6_addr *in6 = &sin6->sin6_addr;
2494 caddr_t lla = LLADDR(sdla);
2495
2496 VERIFY(sa->sa_len <= sizeof(*sdl));
2497 bcopy(sa, sdl, sa->sa_len);
2498
2499 sin6->sin6_scope_id = sdla->sdl_index;
2500 if (sin6->sin6_scope_id == 0) {
2501 sin6->sin6_scope_id = ifp->if_index;
2502 }
2503 in6->s6_addr[0] = 0xfe;
2504 in6->s6_addr[1] = 0x80;
2505 if (sdla->sdl_alen == EUI64_LENGTH) {
2506 bcopy(lla, &in6->s6_addr[8], EUI64_LENGTH);
2507 } else {
2508 VERIFY(sdla->sdl_alen == ETHER_ADDR_LEN);
2509
2510 in6->s6_addr[8] = ((uint8_t) lla[0] ^ ND6_EUI64_UBIT);
2511 in6->s6_addr[9] = (uint8_t) lla[1];
2512 in6->s6_addr[10] = (uint8_t) lla[2];
2513 in6->s6_addr[11] = 0xff;
2514 in6->s6_addr[12] = 0xfe;
2515 in6->s6_addr[13] = (uint8_t) lla[3];
2516 in6->s6_addr[14] = (uint8_t) lla[4];
2517 in6->s6_addr[15] = (uint8_t) lla[5];
2518 }
2519
2520 break;
2521 }
2522 default:
2523 VERIFY(false);
2524 break;
2525 }
2526 }
2527
2528 int
nd6_alt_node_present(struct ifnet * ifp,struct sockaddr_in6 * sin6,struct sockaddr_dl * sdl,int32_t rssi,int lqm,int npm)2529 nd6_alt_node_present(struct ifnet *ifp, struct sockaddr_in6 *sin6,
2530 struct sockaddr_dl *sdl, int32_t rssi, int lqm, int npm)
2531 {
2532 struct rtentry *rt = NULL;
2533 struct llinfo_nd6 *ln = NULL;
2534 struct if_llreach *lr = NULL;
2535 int nd6_nc_updated = 0;
2536 const uint32_t temp_embedded_id = sin6->sin6_addr.s6_addr16[1];
2537 const uint32_t temp_ifscope_id = sin6->sin6_scope_id;
2538
2539 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2540 if (in6_embedded_scope) {
2541 if (temp_embedded_id == 0) {
2542 sin6->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
2543 }
2544 } else if (temp_ifscope_id == 0) {
2545 sin6->sin6_scope_id = ifp->if_index;
2546 }
2547 }
2548
2549 nd6_cache_lladdr(ifp, &sin6->sin6_addr, LLADDR(sdl), sdl->sdl_alen,
2550 ND_NEIGHBOR_ADVERT, 0, &nd6_nc_updated);
2551
2552 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
2553 lck_mtx_lock(rnh_lock);
2554
2555 rt = rtalloc1_scoped_locked((struct sockaddr *)sin6, 1, 0,
2556 ifp->if_index);
2557
2558 /* Restore the address that was passed to us */
2559 if (in6_embedded_scope) {
2560 if (temp_embedded_id == 0) {
2561 sin6->sin6_addr.s6_addr16[1] = 0;
2562 }
2563 } else if (temp_ifscope_id == 0) {
2564 sin6->sin6_scope_id = 0;
2565 }
2566
2567 if (rt != NULL) {
2568 RT_LOCK(rt);
2569 VERIFY(rt->rt_flags & RTF_LLINFO);
2570 VERIFY(rt->rt_llinfo);
2571
2572 ln = rt->rt_llinfo;
2573 ND6_CACHE_STATE_TRANSITION(ln, ND6_LLINFO_REACHABLE);
2574 ln_setexpire(ln, 0);
2575
2576 lr = ln->ln_llreach;
2577 if (lr) {
2578 IFLR_LOCK(lr);
2579 lr->lr_rssi = rssi;
2580 lr->lr_lqm = (int32_t) lqm;
2581 lr->lr_npm = (int32_t) npm;
2582 IFLR_UNLOCK(lr);
2583 }
2584
2585 RT_UNLOCK(rt);
2586 RT_REMREF(rt);
2587 }
2588
2589 lck_mtx_unlock(rnh_lock);
2590
2591 if (rt == NULL) {
2592 log(LOG_ERR, "%s: failed to add/update host route to %s.\n",
2593 __func__, ip6_sprintf(&sin6->sin6_addr));
2594 #if DEBUG || DEVELOPMENT
2595 if (ip6_p2p_debug) {
2596 panic("%s: failed to add/update host route to %s.\n",
2597 __func__, ip6_sprintf(&sin6->sin6_addr));
2598 }
2599 #endif
2600 return EHOSTUNREACH;
2601 }
2602
2603 nd6log(debug, "%s: Successfully added/updated host route to %s [lr=0x%llx]\n",
2604 __func__, ip6_sprintf(&sin6->sin6_addr),
2605 (uint64_t)VM_KERNEL_ADDRPERM(lr));
2606 /*
2607 * nd6_nc_updated not set implies that nothing was updated
2608 * in the neighbor cache. Convey that as EEXIST to callers.
2609 */
2610 if (nd6_nc_updated == 0) {
2611 return EEXIST;
2612 }
2613 return 0;
2614 }
2615
2616 int
nd6_alt_node_absent(struct ifnet * ifp,struct sockaddr_in6 * sin6,struct sockaddr_dl * sdl)2617 nd6_alt_node_absent(struct ifnet *ifp, struct sockaddr_in6 *sin6, struct sockaddr_dl *sdl)
2618 {
2619 struct rtentry *rt = NULL;
2620 int error = 0;
2621 const uint32_t temp_embedded_id = sin6->sin6_addr.s6_addr16[1];
2622 const uint32_t temp_ifscope_id = sin6->sin6_scope_id;
2623
2624 nd6log(debug, "%s: host route to %s\n", __func__,
2625 ip6_sprintf(&sin6->sin6_addr));
2626
2627 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
2628 if (in6_embedded_scope) {
2629 if (temp_embedded_id == 0) {
2630 sin6->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
2631 }
2632 } else if (temp_ifscope_id == 0) {
2633 sin6->sin6_scope_id = ifp->if_index;
2634 }
2635 }
2636
2637 LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED);
2638 lck_mtx_lock(rnh_lock);
2639
2640 rt = rtalloc1_scoped_locked((struct sockaddr *)sin6, 0, 0,
2641 ifp->if_index);
2642
2643 /* Restore the address that was passed to us */
2644 if (in6_embedded_scope) {
2645 if (temp_embedded_id == 0) {
2646 sin6->sin6_addr.s6_addr16[1] = 0;
2647 }
2648 } else if (temp_ifscope_id == 0) {
2649 sin6->sin6_scope_id = 0;
2650 }
2651
2652 if (rt != NULL) {
2653 RT_LOCK(rt);
2654 if (IS_DYNAMIC_DIRECT_HOSTROUTE(rt)) {
2655 /*
2656 * Copy the link layer information in SDL when present
2657 * as it later gets used to issue the kernel event for
2658 * node absence.
2659 */
2660 if (sdl != NULL && rt->rt_gateway != NULL &&
2661 rt->rt_gateway->sa_family == AF_LINK &&
2662 SDL(rt->rt_gateway)->sdl_len <= sizeof(*sdl)) {
2663 bcopy(rt->rt_gateway, sdl, SDL(rt->rt_gateway)->sdl_len);
2664 }
2665
2666 rt->rt_flags |= RTF_CONDEMNED;
2667 RT_UNLOCK(rt);
2668
2669 error = rtrequest_locked(RTM_DELETE, rt_key(rt),
2670 (struct sockaddr *)NULL, rt_mask(rt), 0,
2671 (struct rtentry **)NULL);
2672
2673 rtfree_locked(rt);
2674 } else {
2675 error = EHOSTUNREACH;
2676 RT_REMREF_LOCKED(rt);
2677 RT_UNLOCK(rt);
2678 }
2679 } else {
2680 error = EHOSTUNREACH;
2681 }
2682
2683 if (error == 0) {
2684 nd6log(debug, "%s: Successfully deleted host route to %s "
2685 "for interface %s.\n", __func__, ip6_sprintf(&sin6->sin6_addr),
2686 ifp->if_xname);
2687 } else {
2688 nd6log(error, "%s: Failed to delete host route to %s "
2689 "for interface %s with error :%d.\n", __func__,
2690 ip6_sprintf(&sin6->sin6_addr),
2691 ifp->if_xname, error);
2692 }
2693
2694 lck_mtx_unlock(rnh_lock);
2695 return error;
2696 }
2697