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