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