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