2 * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* $FreeBSD: src/sys/netinet6/nd6_nbr.c,v 1.4.2.4 2001/07/06 05:32:25 sumikawa Exp $ */
29 /* $KAME: nd6_nbr.c,v 1.64 2001/05/17 03:48:30 itojun Exp $ */
32 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the project nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/malloc.h>
64 #include <sys/socket.h>
65 #include <sys/sockio.h>
67 #include <sys/kernel.h>
68 #include <sys/errno.h>
69 #include <sys/syslog.h>
70 #include <sys/sysctl.h>
71 #include <sys/mcache.h>
72 #include <sys/protosw.h>
73 #include <kern/queue.h>
75 #include <kern/locks.h>
76 #include <kern/zalloc.h>
79 #include <net/if_var.h>
80 #include <net/if_types.h>
81 #include <net/if_dl.h>
82 #include <net/if_llreach.h>
83 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet6/in6_var.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet6/nd6.h>
91 #include <netinet6/scope6_var.h>
92 #include <netinet/icmp6.h>
95 #include <netinet6/ipsec.h>
97 #include <netinet6/ipsec6.h>
99 extern int ipsec_bypass
;
102 #include <net/net_osdep.h>
104 #define SDL(s) ((struct sockaddr_dl *)s)
107 static struct dadq
*nd6_dad_find(struct ifaddr
*);
108 void nd6_dad_stoptimer(struct ifaddr
*);
109 static void nd6_dad_timer(struct ifaddr
*);
110 static void nd6_dad_ns_output(struct dadq
*, struct ifaddr
*);
111 static void nd6_dad_ns_input(struct ifaddr
*);
112 static void nd6_dad_na_input(struct ifaddr
*, caddr_t
, int);
113 static void dad_addref(struct dadq
*, int);
114 static void dad_remref(struct dadq
*);
115 static struct dadq
*nd6_dad_attach(struct dadq
*, struct ifaddr
*);
116 static void nd6_dad_detach(struct dadq
*, struct ifaddr
*);
118 static int dad_ignore_ns
= 0; /* ignore NS in DAD - specwise incorrect*/
119 static int dad_maxtry
= 15; /* max # of *tries* to transmit DAD packet */
121 static unsigned int dad_size
; /* size of zone element */
122 static struct zone
*dad_zone
; /* zone for dadq */
124 #define DAD_ZONE_MAX 64 /* maximum elements in zone */
125 #define DAD_ZONE_NAME "nd6_dad" /* zone name */
127 #define DAD_LOCK_ASSERT_HELD(_dp) \
128 lck_mtx_assert(&(_dp)->dad_lock, LCK_MTX_ASSERT_OWNED)
130 #define DAD_LOCK_ASSERT_NOTHELD(_dp) \
131 lck_mtx_assert(&(_dp)->dad_lock, LCK_MTX_ASSERT_NOTOWNED)
133 #define DAD_LOCK(_dp) \
134 lck_mtx_lock(&(_dp)->dad_lock)
136 #define DAD_LOCK_SPIN(_dp) \
137 lck_mtx_lock_spin(&(_dp)->dad_lock)
139 #define DAD_CONVERT_LOCK(_dp) do { \
140 DAD_LOCK_ASSERT_HELD(_dp); \
141 lck_mtx_convert_spin(&(_dp)->dad_lock); \
144 #define DAD_UNLOCK(_dp) \
145 lck_mtx_unlock(&(_dp)->dad_lock)
147 #define DAD_ADDREF(_dp) \
150 #define DAD_ADDREF_LOCKED(_dp) \
153 #define DAD_REMREF(_dp) \
156 extern lck_mtx_t
*dad6_mutex
;
157 extern lck_mtx_t
*nd6_mutex
;
158 extern int in6_get_hw_ifid(struct ifnet
*, struct in6_addr
*);
160 static int nd6_llreach_base
= (LL_BASE_REACHABLE
/ 1000); /* seconds */
162 SYSCTL_DECL(_net_inet6_icmp6
);
164 SYSCTL_INT(_net_inet6_icmp6
, OID_AUTO
, nd6_llreach_base
,
165 CTLFLAG_RW
| CTLFLAG_LOCKED
, &nd6_llreach_base
, LL_BASE_REACHABLE
,
166 "default ND6 link-layer reachability max lifetime (in seconds)");
168 #define SIN6(s) ((struct sockaddr_in6 *)s)
171 * Obtain a link-layer source cache entry for the sender.
173 * NOTE: This is currently only for ND6/Ethernet.
176 nd6_llreach_alloc(struct rtentry
*rt
, struct ifnet
*ifp
, void *addr
,
177 unsigned int alen
, boolean_t solicited
)
179 struct llinfo_nd6
*ln
= rt
->rt_llinfo
;
181 if (nd6_llreach_base
!= 0 &&
182 ln
->ln_expire
!= 0 && rt
->rt_ifp
!= lo_ifp
&&
183 ifp
->if_addrlen
== IF_LLREACH_MAXLEN
&& /* Ethernet */
184 alen
== ifp
->if_addrlen
) {
185 struct if_llreach
*lr
;
186 const char *why
= NULL
, *type
= "";
188 /* Become a regular mutex, just in case */
191 if ((lr
= ln
->ln_llreach
) != NULL
) {
192 type
= (solicited
? "ND6 advertisement" :
193 "ND6 unsolicited announcement");
195 * If target has changed, create a new record;
196 * otherwise keep existing record.
199 if (bcmp(addr
, lr
->lr_key
.addr
, alen
) != 0) {
201 /* Purge any link-layer info caching */
202 VERIFY(rt
->rt_llinfo_purge
!= NULL
);
203 rt
->rt_llinfo_purge(rt
);
205 why
= " for different target HW address; "
206 "using new llreach record";
208 lr
->lr_probes
= 0; /* reset probe count */
211 why
= " for same target HW address; "
212 "keeping existing llreach record";
218 lr
= ln
->ln_llreach
= ifnet_llreach_alloc(ifp
,
219 ETHERTYPE_IPV6
, addr
, alen
, nd6_llreach_base
);
221 lr
->lr_probes
= 0; /* reset probe count */
223 why
= "creating new llreach record";
227 if (nd6_debug
&& lr
!= NULL
&& why
!= NULL
) {
228 char tmp
[MAX_IPv6_STR_LEN
];
230 nd6log((LOG_DEBUG
, "%s%d: %s%s for %s\n", ifp
->if_name
,
231 ifp
->if_unit
, type
, why
, inet_ntop(AF_INET6
,
232 &SIN6(rt_key(rt
))->sin6_addr
, tmp
, sizeof (tmp
))));
238 nd6_llreach_use(struct llinfo_nd6
*ln
)
240 if (ln
->ln_llreach
!= NULL
)
241 ln
->ln_lastused
= net_uptime();
245 * Input a Neighbor Solicitation Message.
248 * Based on RFC 2462 (duplicate address detection)
256 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
257 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
258 struct nd_neighbor_solicit
*nd_ns
;
259 struct in6_addr saddr6
= ip6
->ip6_src
;
260 struct in6_addr daddr6
= ip6
->ip6_dst
;
261 struct in6_addr taddr6
;
262 struct in6_addr myaddr6
;
264 struct ifaddr
*ifa
= NULL
;
266 int anycast
= 0, proxy
= 0, tentative
= 0;
268 union nd_opts ndopts
;
269 struct sockaddr_dl proxydl
;
271 #ifndef PULLDOWN_TEST
272 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
273 nd_ns
= (struct nd_neighbor_solicit
*)((caddr_t
)ip6
+ off
);
275 IP6_EXTHDR_GET(nd_ns
, struct nd_neighbor_solicit
*, m
, off
, icmp6len
);
277 icmp6stat
.icp6s_tooshort
++;
281 ip6
= mtod(m
, struct ip6_hdr
*); /* adjust pointer for safety */
282 taddr6
= nd_ns
->nd_ns_target
;
283 if (in6_setscope(&taddr6
, ifp
, NULL
) != 0)
286 if (ip6
->ip6_hlim
!= 255) {
288 "nd6_ns_input: invalid hlim (%d) from %s to %s on %s\n",
289 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
290 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
294 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
)) {
295 /* dst has to be a solicited node multicast address. */
296 if (daddr6
.s6_addr16
[0] == IPV6_ADDR_INT16_MLL
&&
297 /* don't check ifindex portion */
298 daddr6
.s6_addr32
[1] == 0 &&
299 daddr6
.s6_addr32
[2] == IPV6_ADDR_INT32_ONE
&&
300 daddr6
.s6_addr8
[12] == 0xff) {
303 nd6log((LOG_INFO
, "nd6_ns_input: bad DAD packet "
304 "(wrong ip6 dst)\n"));
307 } else if (!nd6_onlink_ns_rfc4861
) {
308 struct sockaddr_in6 src_sa6
;
311 * According to recent IETF discussions, it is not a good idea
312 * to accept a NS from an address which would not be deemed
313 * to be a neighbor otherwise. This point is expected to be
314 * clarified in future revisions of the specification.
316 bzero(&src_sa6
, sizeof(src_sa6
));
317 src_sa6
.sin6_family
= AF_INET6
;
318 src_sa6
.sin6_len
= sizeof(src_sa6
);
319 src_sa6
.sin6_addr
= saddr6
;
320 if (!nd6_is_addr_neighbor(&src_sa6
, ifp
, 0)) {
321 nd6log((LOG_INFO
, "nd6_ns_input: "
322 "NS packet from non-neighbor\n"));
327 if (IN6_IS_ADDR_MULTICAST(&taddr6
)) {
328 nd6log((LOG_INFO
, "nd6_ns_input: bad NS target (multicast)\n"));
332 icmp6len
-= sizeof(*nd_ns
);
333 nd6_option_init(nd_ns
+ 1, icmp6len
, &ndopts
);
334 if (nd6_options(&ndopts
) < 0) {
336 "nd6_ns_input: invalid ND option, ignored\n"));
337 /* nd6_options have incremented stats */
341 if (ndopts
.nd_opts_src_lladdr
) {
342 lladdr
= (char *)(ndopts
.nd_opts_src_lladdr
+ 1);
343 lladdrlen
= ndopts
.nd_opts_src_lladdr
->nd_opt_len
<< 3;
346 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) && lladdr
) {
347 nd6log((LOG_INFO
, "nd6_ns_input: bad DAD packet "
348 "(link-layer address option)\n"));
353 * Attaching target link-layer address to the NA?
356 * NS IP dst is unicast/anycast MUST NOT add
357 * NS IP dst is solicited-node multicast MUST add
359 * In implementation, we add target link-layer address by default.
360 * We do not add one in MUST NOT cases.
362 if (!IN6_IS_ADDR_MULTICAST(&daddr6
))
368 * Target address (taddr6) must be either:
369 * (1) Valid unicast/anycast address for my receiving interface,
370 * (2) Unicast address for which I'm offering proxy service, or
371 * (3) "tentative" address on which DAD is being performed.
373 /* (1) and (3) check. */
374 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &taddr6
);
379 struct sockaddr_in6 tsin6
;
381 bzero(&tsin6
, sizeof tsin6
);
382 tsin6
.sin6_len
= sizeof(struct sockaddr_in6
);
383 tsin6
.sin6_family
= AF_INET6
;
384 tsin6
.sin6_addr
= taddr6
;
386 rt
= rtalloc1_scoped((struct sockaddr
*)&tsin6
, 0, 0,
391 if ((rt
->rt_flags
& RTF_ANNOUNCE
) != 0 &&
392 rt
->rt_gateway
->sa_family
== AF_LINK
) {
394 * proxy NDP for single entry
396 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(
397 ifp
, IN6_IFF_NOTREADY
|IN6_IFF_ANYCAST
);
400 proxydl
= *SDL(rt
->rt_gateway
);
409 * We've got an NS packet, and we don't have that adddress
410 * assigned for us. We MUST silently ignore it.
416 myaddr6
= *IFA_IN6(ifa
);
417 anycast
= ((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
;
418 tentative
= ((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_TENTATIVE
;
419 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DUPLICATED
) {
425 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
427 "nd6_ns_input: lladdrlen mismatch for %s "
428 "(if %d, NS packet %d)\n",
429 ip6_sprintf(&taddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
433 if (IN6_ARE_ADDR_EQUAL(&myaddr6
, &saddr6
)) {
435 "nd6_ns_input: duplicate IP6 address %s\n",
436 ip6_sprintf(&saddr6
)));
441 * We have neighbor solicitation packet, with target address equals to
442 * one of my tentative address.
444 * src addr how to process?
446 * multicast of course, invalid (rejected in ip6_input)
447 * unicast somebody is doing address resolution -> ignore
448 * unspec dup address detection
450 * The processing is defined in RFC 2462.
454 * If source address is unspecified address, it is for
455 * duplicate address detection.
457 * If not, the packet is for addess resolution;
458 * silently ignore it.
460 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
))
461 nd6_dad_ns_input(ifa
);
467 * If the source address is unspecified address, entries must not
468 * be created or updated.
469 * It looks that sender is performing DAD. Output NA toward
470 * all-node multicast address, to tell the sender that I'm using
472 * S bit ("solicited") must be zero.
474 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6
)) {
475 saddr6
= in6addr_linklocal_allnodes
;
476 if (in6_setscope(&saddr6
, ifp
, NULL
) != 0)
478 nd6_na_output(ifp
, &saddr6
, &taddr6
,
479 ((anycast
|| proxy
|| !tlladdr
)
480 ? 0 : ND_NA_FLAG_OVERRIDE
)
481 | (ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0),
482 tlladdr
, proxy
? (struct sockaddr
*)&proxydl
: NULL
);
486 nd6_cache_lladdr(ifp
, &saddr6
, lladdr
, lladdrlen
, ND_NEIGHBOR_SOLICIT
, 0);
488 nd6_na_output(ifp
, &saddr6
, &taddr6
,
489 ((anycast
|| proxy
|| !tlladdr
) ? 0 : ND_NA_FLAG_OVERRIDE
)
490 | (ip6_forwarding
? ND_NA_FLAG_ROUTER
: 0)
491 | ND_NA_FLAG_SOLICITED
,
492 tlladdr
, proxy
? (struct sockaddr
*)&proxydl
: NULL
);
500 nd6log((LOG_ERR
, "nd6_ns_input: src=%s\n", ip6_sprintf(&saddr6
)));
501 nd6log((LOG_ERR
, "nd6_ns_input: dst=%s\n", ip6_sprintf(&daddr6
)));
502 nd6log((LOG_ERR
, "nd6_ns_input: tgt=%s\n", ip6_sprintf(&taddr6
)));
503 icmp6stat
.icp6s_badns
++;
510 * Output a Neighbor Solicitation Message. Caller specifies:
511 * - ICMP6 header source IP6 address
512 * - ND6 header target IP6 address
513 * - ND6 header source datalink address
516 * Based on RFC 2462 (duplicate address detection)
518 * Caller must bump up ln->ln_rt refcnt to make sure 'ln' doesn't go
519 * away if there is a llinfo_nd6 passed in.
524 const struct in6_addr
*daddr6
,
525 const struct in6_addr
*taddr6
,
526 struct llinfo_nd6
*ln
, /* for source address determination */
527 int dad
) /* duplicated address detection */
531 struct nd_neighbor_solicit
*nd_ns
;
532 struct in6_ifaddr
*ia
= NULL
;
533 struct in6_addr
*src
, src_in
, src_storage
;
534 struct ip6_moptions
*im6o
= NULL
;
535 struct ifnet
*outif
= NULL
;
541 struct ip6_out_args ip6oa
= { IFSCOPE_NONE
, 0 };
543 bzero(&ro
, sizeof(ro
));
545 if (IN6_IS_ADDR_MULTICAST(taddr6
))
548 ip6oa
.ip6oa_boundif
= ifp
->if_index
;
550 /* estimate the size of message */
551 maxlen
= sizeof(*ip6
) + sizeof(*nd_ns
);
552 maxlen
+= (sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7;
553 if (max_linkhdr
+ maxlen
>= MCLBYTES
) {
555 printf("nd6_ns_output: max_linkhdr + maxlen >= MCLBYTES "
556 "(%d + %d > %d)\n", max_linkhdr
, maxlen
, MCLBYTES
);
561 MGETHDR(m
, M_DONTWAIT
, MT_DATA
); /* XXXMAC: mac_create_mbuf_linklayer() probably */
562 if (m
&& max_linkhdr
+ maxlen
>= MHLEN
) {
563 MCLGET(m
, M_DONTWAIT
);
564 if ((m
->m_flags
& M_EXT
) == 0) {
571 m
->m_pkthdr
.rcvif
= NULL
;
573 if (daddr6
== NULL
|| IN6_IS_ADDR_MULTICAST(daddr6
)) {
574 m
->m_flags
|= M_MCAST
;
576 im6o
= ip6_allocmoptions(M_DONTWAIT
);
582 im6o
->im6o_multicast_ifp
= ifp
;
583 im6o
->im6o_multicast_hlim
= 255;
584 im6o
->im6o_multicast_loop
= 0;
587 icmp6len
= sizeof(*nd_ns
);
588 m
->m_pkthdr
.len
= m
->m_len
= sizeof(*ip6
) + icmp6len
;
589 m
->m_data
+= max_linkhdr
; /* or MH_ALIGN() equivalent? */
591 /* fill neighbor solicitation packet */
592 ip6
= mtod(m
, struct ip6_hdr
*);
594 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
595 ip6
->ip6_vfc
|= IPV6_VERSION
;
596 /* ip6->ip6_plen will be set later */
597 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
600 ip6
->ip6_dst
= *daddr6
;
602 ip6
->ip6_dst
.s6_addr16
[0] = IPV6_ADDR_INT16_MLL
;
603 ip6
->ip6_dst
.s6_addr16
[1] = 0;
604 ip6
->ip6_dst
.s6_addr32
[1] = 0;
605 ip6
->ip6_dst
.s6_addr32
[2] = IPV6_ADDR_INT32_ONE
;
606 ip6
->ip6_dst
.s6_addr32
[3] = taddr6
->s6_addr32
[3];
607 ip6
->ip6_dst
.s6_addr8
[12] = 0xff;
608 if (in6_setscope(&ip6
->ip6_dst
, ifp
, NULL
) != 0)
614 * "If the source address of the packet prompting the
615 * solicitation is the same as one of the addresses assigned
616 * to the outgoing interface, that address SHOULD be placed
617 * in the IP Source Address of the outgoing solicitation.
618 * Otherwise, any one of the addresses assigned to the
619 * interface should be used."
621 * We use the source address for the prompting packet
623 * - saddr6 is given from the caller (by giving "ln"), and
624 * - saddr6 belongs to the outgoing interface.
625 * Otherwise, we perform the source address selection as usual.
627 struct ip6_hdr
*hip6
; /* hold ip6 */
628 struct in6_addr
*hsrc
= NULL
;
630 /* Caller holds ref on this route */
634 * assuming every packet in ln_hold has the same IP
637 if (ln
->ln_hold
!= NULL
) {
638 hip6
= mtod(ln
->ln_hold
, struct ip6_hdr
*);
640 if (sizeof (*hip6
) < ln
->ln_hold
->m_len
)
641 hsrc
= &hip6
->ip6_src
;
645 /* Update probe count, if applicable */
646 if (ln
->ln_llreach
!= NULL
) {
647 IFLR_LOCK_SPIN(ln
->ln_llreach
);
648 ln
->ln_llreach
->lr_probes
++;
649 IFLR_UNLOCK(ln
->ln_llreach
);
651 RT_UNLOCK(ln
->ln_rt
);
655 IFA_REMREF(&ia
->ia_ifa
);
658 if (hsrc
!= NULL
&& (ia
= in6ifa_ifpwithaddr(ifp
, hsrc
))) {
660 IFA_REMREF(&ia
->ia_ifa
);
664 struct sockaddr_in6 dst_sa
;
666 bzero(&dst_sa
, sizeof(dst_sa
));
667 dst_sa
.sin6_family
= AF_INET6
;
668 dst_sa
.sin6_len
= sizeof(dst_sa
);
669 dst_sa
.sin6_addr
= ip6
->ip6_dst
;
671 src
= in6_selectsrc(&dst_sa
, NULL
,
672 NULL
, &ro
, NULL
, &src_storage
, ip6oa
.ip6oa_boundif
,
676 "nd6_ns_output: source can't be "
677 "determined: dst=%s, error=%d\n",
678 ip6_sprintf(&dst_sa
.sin6_addr
),
685 * Source address for DAD packet must always be IPv6
686 * unspecified address. (0::0)
687 * We actually don't have to 0-clear the address (we did it
688 * above), but we do so here explicitly to make the intention
691 bzero(&src_in
, sizeof(src_in
));
695 nd_ns
= (struct nd_neighbor_solicit
*)(ip6
+ 1);
696 nd_ns
->nd_ns_type
= ND_NEIGHBOR_SOLICIT
;
697 nd_ns
->nd_ns_code
= 0;
698 nd_ns
->nd_ns_reserved
= 0;
699 nd_ns
->nd_ns_target
= *taddr6
;
700 in6_clearscope(&nd_ns
->nd_ns_target
); /* XXX */
703 * Add source link-layer address option.
705 * spec implementation
707 * DAD packet MUST NOT do not add the option
708 * there's no link layer address:
709 * impossible do not add the option
710 * there's link layer address:
711 * Multicast NS MUST add one add the option
712 * Unicast NS SHOULD add one add the option
714 if (!dad
&& (mac
= nd6_ifptomac(ifp
))) {
715 int optlen
= sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
;
716 struct nd_opt_hdr
*nd_opt
= (struct nd_opt_hdr
*)(nd_ns
+ 1);
717 /* 8 byte alignments... */
718 optlen
= (optlen
+ 7) & ~7;
720 m
->m_pkthdr
.len
+= optlen
;
723 bzero((caddr_t
)nd_opt
, optlen
);
724 nd_opt
->nd_opt_type
= ND_OPT_SOURCE_LINKADDR
;
725 nd_opt
->nd_opt_len
= optlen
>> 3;
726 bcopy(mac
, (caddr_t
)(nd_opt
+ 1), ifp
->if_addrlen
);
729 ip6
->ip6_plen
= htons((u_short
)icmp6len
);
730 nd_ns
->nd_ns_cksum
= 0;
732 = in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(*ip6
), icmp6len
);
735 /* Don't lookup socket */
736 if (ipsec_bypass
== 0)
737 (void)ipsec_setsocket(m
, NULL
);
739 flags
= dad
? IPV6_UNSPECSRC
: 0;
740 flags
|= IPV6_OUTARGS
;
742 ip6_output(m
, NULL
, NULL
, flags
, im6o
, &outif
, &ip6oa
);
744 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
745 icmp6_ifstat_inc(outif
, ifs6_out_neighborsolicit
);
746 ifnet_release(outif
);
748 icmp6stat
.icp6s_outhist
[ND_NEIGHBOR_SOLICIT
]++;
752 if (ro
.ro_rt
) { /* we don't cache this route. */
756 IFA_REMREF(&ia
->ia_ifa
);
767 IFA_REMREF(&ia
->ia_ifa
);
772 * Neighbor advertisement input handling.
775 * Based on RFC 2462 (duplicate address detection)
777 * the following items are not implemented yet:
778 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
779 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
787 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
788 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
789 struct nd_neighbor_advert
*nd_na
;
790 struct in6_addr daddr6
= ip6
->ip6_dst
;
791 struct in6_addr taddr6
;
798 struct ifaddr
*ifa
= NULL
;
799 struct llinfo_nd6
*ln
;
801 struct sockaddr_dl
*sdl
;
802 union nd_opts ndopts
;
803 struct timeval timenow
;
805 if (ip6
->ip6_hlim
!= 255) {
807 "nd6_na_input: invalid hlim (%d) from %s to %s on %s\n",
808 ip6
->ip6_hlim
, ip6_sprintf(&ip6
->ip6_src
),
809 ip6_sprintf(&ip6
->ip6_dst
), if_name(ifp
)));
813 #ifndef PULLDOWN_TEST
814 IP6_EXTHDR_CHECK(m
, off
, icmp6len
, return);
815 nd_na
= (struct nd_neighbor_advert
*)((caddr_t
)ip6
+ off
);
817 IP6_EXTHDR_GET(nd_na
, struct nd_neighbor_advert
*, m
, off
, icmp6len
);
819 icmp6stat
.icp6s_tooshort
++;
824 flags
= nd_na
->nd_na_flags_reserved
;
825 is_router
= ((flags
& ND_NA_FLAG_ROUTER
) != 0);
826 is_solicited
= ((flags
& ND_NA_FLAG_SOLICITED
) != 0);
827 is_override
= ((flags
& ND_NA_FLAG_OVERRIDE
) != 0);
829 taddr6
= nd_na
->nd_na_target
;
830 if (in6_setscope(&taddr6
, ifp
, NULL
))
831 goto bad
; /* XXX: impossible */
833 if (IN6_IS_ADDR_MULTICAST(&taddr6
)) {
835 "nd6_na_input: invalid target address %s\n",
836 ip6_sprintf(&taddr6
)));
839 if (IN6_IS_ADDR_MULTICAST(&daddr6
))
842 "nd6_na_input: a solicited adv is multicasted\n"));
846 icmp6len
-= sizeof(*nd_na
);
847 nd6_option_init(nd_na
+ 1, icmp6len
, &ndopts
);
848 if (nd6_options(&ndopts
) < 0) {
850 "nd6_na_input: invalid ND option, ignored\n"));
851 /* nd6_options have incremented stats */
855 if (ndopts
.nd_opts_tgt_lladdr
) {
856 lladdr
= (char *)(ndopts
.nd_opts_tgt_lladdr
+ 1);
857 lladdrlen
= ndopts
.nd_opts_tgt_lladdr
->nd_opt_len
<< 3;
860 ifa
= (struct ifaddr
*)in6ifa_ifpwithaddr(ifp
, &taddr6
);
863 * Target address matches one of my interface address.
865 * If my address is tentative, this means that there's somebody
866 * already using the same address as mine. This indicates DAD failure.
867 * This is defined in RFC 2462.
869 * Otherwise, process as defined in RFC 2461.
873 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_TENTATIVE
) {
875 nd6_dad_na_input(ifa
, lladdr
, lladdrlen
);
881 /* Just for safety, maybe unnecessary. */
884 "nd6_na_input: duplicate IP6 address %s\n",
885 ip6_sprintf(&taddr6
));
889 if (lladdr
&& ((ifp
->if_addrlen
+ 2 + 7) & ~7) != lladdrlen
) {
891 "nd6_na_input: lladdrlen mismatch for %s "
892 "(if %d, NA packet %d)\n",
893 ip6_sprintf(&taddr6
), ifp
->if_addrlen
, lladdrlen
- 2));
898 * If no neighbor cache entry is found, NA SHOULD silently be
901 if ((rt
= nd6_lookup(&taddr6
, 0, ifp
, 0)) == NULL
)
904 RT_LOCK_ASSERT_HELD(rt
);
905 if ((ln
= rt
->rt_llinfo
) == NULL
||
906 (sdl
= SDL(rt
->rt_gateway
)) == NULL
) {
907 RT_REMREF_LOCKED(rt
);
912 getmicrotime(&timenow
);
913 if (ln
->ln_state
== ND6_LLINFO_INCOMPLETE
) {
915 * If the link-layer has address, and no lladdr option came,
916 * discard the packet.
918 if (ifp
->if_addrlen
&& !lladdr
) {
919 RT_REMREF_LOCKED(rt
);
925 * Record link-layer address, and update the state.
927 sdl
->sdl_alen
= ifp
->if_addrlen
;
928 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
930 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
933 lck_rw_lock_shared(nd_if_rwlock
);
934 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
,
935 nd_ifinfo
[rt
->rt_ifp
->if_index
].reachable
);
936 lck_rw_done(nd_if_rwlock
);
939 ln
->ln_state
= ND6_LLINFO_STALE
;
940 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
,
943 if ((ln
->ln_router
= is_router
) != 0) {
945 * This means a router's state has changed from
946 * non-reachable to probably reachable, and might
947 * affect the status of associated prefixes..
950 lck_mtx_lock(nd6_mutex
);
951 pfxlist_onlink_check();
952 lck_mtx_unlock(nd6_mutex
);
959 * Check if the link-layer address has changed or not.
965 if (bcmp(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
))
974 * This is VERY complex. Look at it with care.
976 * override solicit lladdr llchange action
981 * 0 0 y y (1) REACHABLE->STALE
982 * 0 1 n -- (2c) *->REACHABLE
983 * 0 1 y n (2b) L *->REACHABLE
984 * 0 1 y y (1) REACHABLE->STALE
987 * 1 0 y y (2a) L *->STALE
988 * 1 1 n -- (2a) *->REACHABLE
989 * 1 1 y n (2a) L *->REACHABLE
990 * 1 1 y y (2a) L *->REACHABLE
992 if (!is_override
&& (lladdr
!= NULL
&& llchange
)) { /* (1) */
994 * If state is REACHABLE, make it STALE.
995 * no other updates should be done.
997 if (ln
->ln_state
== ND6_LLINFO_REACHABLE
) {
998 ln
->ln_state
= ND6_LLINFO_STALE
;
999 ln
->ln_expire
= rt_expiry(rt
, timenow
.tv_sec
,
1002 RT_REMREF_LOCKED(rt
);
1005 } else if (is_override
/* (2a) */
1006 || (!is_override
&& (lladdr
&& !llchange
)) /* (2b) */
1007 || !lladdr
) { /* (2c) */
1009 * Update link-local address, if any.
1012 sdl
->sdl_alen
= ifp
->if_addrlen
;
1013 bcopy(lladdr
, LLADDR(sdl
), ifp
->if_addrlen
);
1017 * If solicited, make the state REACHABLE.
1018 * If not solicited and the link-layer address was
1019 * changed, make it STALE.
1022 ln
->ln_state
= ND6_LLINFO_REACHABLE
;
1024 if (ln
->ln_expire
) {
1025 lck_rw_lock_shared(nd_if_rwlock
);
1027 rt_expiry(rt
, timenow
.tv_sec
,
1028 nd_ifinfo
[ifp
->if_index
].reachable
);
1029 lck_rw_done(nd_if_rwlock
);
1032 if (lladdr
&& llchange
) {
1033 ln
->ln_state
= ND6_LLINFO_STALE
;
1034 ln
->ln_expire
= rt_expiry(rt
,
1035 timenow
.tv_sec
, nd6_gctimer
);
1040 if (ln
->ln_router
&& !is_router
) {
1042 * The peer dropped the router flag.
1043 * Remove the sender from the Default Router List and
1044 * update the Destination Cache entries.
1046 struct nd_defrouter
*dr
;
1047 struct in6_addr
*in6
;
1048 struct ifnet
*rt_ifp
= rt
->rt_ifp
;
1050 in6
= &((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
;
1053 * Lock to protect the default router list.
1054 * XXX: this might be unnecessary, since this function
1055 * is only called under the network software interrupt
1056 * context. However, we keep it just for safety.
1059 lck_mtx_lock(nd6_mutex
);
1060 dr
= defrouter_lookup(in6
, rt_ifp
);
1064 lck_mtx_unlock(nd6_mutex
);
1067 lck_mtx_unlock(nd6_mutex
);
1068 if (!ip6_forwarding
&& (ip6_accept_rtadv
|| (rt_ifp
->if_eflags
& IFEF_ACCEPT_RTADVD
))) {
1070 * Even if the neighbor is not in the default
1071 * router list, the neighbor may be used
1072 * as a next hop for some destinations
1073 * (e.g. redirect case). So we must
1074 * call rt6_flush explicitly.
1076 rt6_flush(&ip6
->ip6_src
, rt_ifp
);
1081 ln
->ln_router
= is_router
;
1083 RT_LOCK_ASSERT_HELD(rt
);
1084 rt
->rt_flags
&= ~RTF_REJECT
;
1086 /* cache the gateway (sender HW) address */
1087 nd6_llreach_alloc(rt
, ifp
, LLADDR(sdl
), sdl
->sdl_alen
, TRUE
);
1089 /* update the llinfo, send a queued packet if there is one */
1091 if (ln
->ln_hold
!= NULL
) {
1092 struct mbuf
*m_hold
, *m_hold_next
;
1093 struct sockaddr_in6 sin6
;
1095 rtkey_to_sa6(rt
, &sin6
);
1097 * reset the ln_hold in advance, to explicitly
1098 * prevent a ln_hold lookup in nd6_output()
1099 * (wouldn't happen, though...)
1101 for (m_hold
= ln
->ln_hold
;
1102 m_hold
; m_hold
= m_hold_next
) {
1103 m_hold_next
= m_hold
->m_nextpkt
;
1104 m_hold
->m_nextpkt
= NULL
;
1106 * we assume ifp is not a loopback here, so just set
1107 * the 2nd argument as the 1st one.
1110 nd6_output(ifp
, ifp
, m_hold
, &sin6
, rt
);
1116 RT_REMREF_LOCKED(rt
);
1126 icmp6stat
.icp6s_badna
++;
1133 * Neighbor advertisement output handling.
1137 * the following items are not implemented yet:
1138 * - proxy advertisement delay rule (RFC2461 7.2.8, last paragraph, SHOULD)
1139 * - anycast advertisement delay rule (RFC2461 7.2.7, SHOULD)
1141 * tlladdr - 1 if include target link-layer address
1142 * sdl0 - sockaddr_dl (= proxy NA) or NULL
1147 const struct in6_addr
*daddr6_0
,
1148 const struct in6_addr
*taddr6
,
1150 int tlladdr
, /* 1 if include target link-layer address */
1151 struct sockaddr
*sdl0
) /* sockaddr_dl (= proxy NA) or NULL */
1154 struct ip6_hdr
*ip6
;
1155 struct nd_neighbor_advert
*nd_na
;
1156 struct ip6_moptions
*im6o
= NULL
;
1158 struct route_in6 ro
;
1159 struct in6_addr
*src
, src_storage
, daddr6
;
1160 struct sockaddr_in6 dst_sa
;
1161 int icmp6len
, maxlen
, error
;
1162 struct ifnet
*outif
= NULL
;
1163 struct ip6_out_args ip6oa
= { IFSCOPE_NONE
, 0 };
1165 bzero(&ro
, sizeof(ro
));
1167 daddr6
= *daddr6_0
; /* make a local copy for modification */
1169 ip6oa
.ip6oa_boundif
= ifp
->if_index
;
1171 /* estimate the size of message */
1172 maxlen
= sizeof(*ip6
) + sizeof(*nd_na
);
1173 maxlen
+= (sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
+ 7) & ~7;
1174 if (max_linkhdr
+ maxlen
>= MCLBYTES
) {
1176 printf("nd6_na_output: max_linkhdr + maxlen >= MCLBYTES "
1177 "(%d + %d > %d)\n", max_linkhdr
, maxlen
, MCLBYTES
);
1182 MGETHDR(m
, M_DONTWAIT
, MT_DATA
); /* XXXMAC: mac_create_mbuf_linklayer() probably */
1183 if (m
&& max_linkhdr
+ maxlen
>= MHLEN
) {
1184 MCLGET(m
, M_DONTWAIT
);
1185 if ((m
->m_flags
& M_EXT
) == 0) {
1192 m
->m_pkthdr
.rcvif
= NULL
;
1194 if (IN6_IS_ADDR_MULTICAST(&daddr6
)) {
1195 m
->m_flags
|= M_MCAST
;
1197 im6o
= ip6_allocmoptions(M_DONTWAIT
);
1203 im6o
->im6o_multicast_ifp
= ifp
;
1204 im6o
->im6o_multicast_hlim
= 255;
1205 im6o
->im6o_multicast_loop
= 0;
1208 icmp6len
= sizeof(*nd_na
);
1209 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct ip6_hdr
) + icmp6len
;
1210 m
->m_data
+= max_linkhdr
; /* or MH_ALIGN() equivalent? */
1212 /* fill neighbor advertisement packet */
1213 ip6
= mtod(m
, struct ip6_hdr
*);
1215 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
1216 ip6
->ip6_vfc
|= IPV6_VERSION
;
1217 ip6
->ip6_nxt
= IPPROTO_ICMPV6
;
1218 ip6
->ip6_hlim
= 255;
1219 if (IN6_IS_ADDR_UNSPECIFIED(&daddr6
)) {
1221 daddr6
.s6_addr16
[0] = IPV6_ADDR_INT16_MLL
;
1222 daddr6
.s6_addr16
[1] = 0;
1223 daddr6
.s6_addr32
[1] = 0;
1224 daddr6
.s6_addr32
[2] = 0;
1225 daddr6
.s6_addr32
[3] = IPV6_ADDR_INT32_ONE
;
1226 if (in6_setscope(&daddr6
, ifp
, NULL
))
1229 flags
&= ~ND_NA_FLAG_SOLICITED
;
1231 ip6
->ip6_dst
= daddr6
;
1233 bzero(&dst_sa
, sizeof(struct sockaddr_in6
));
1234 dst_sa
.sin6_family
= AF_INET6
;
1235 dst_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
1236 dst_sa
.sin6_addr
= daddr6
;
1239 * Select a source whose scope is the same as that of the dest.
1241 bcopy(&dst_sa
, &ro
.ro_dst
, sizeof(dst_sa
));
1242 src
= in6_selectsrc(&dst_sa
, NULL
, NULL
, &ro
, NULL
, &src_storage
,
1243 ip6oa
.ip6oa_boundif
, &error
);
1245 nd6log((LOG_DEBUG
, "nd6_na_output: source can't be "
1246 "determined: dst=%s, error=%d\n",
1247 ip6_sprintf(&dst_sa
.sin6_addr
), error
));
1250 ip6
->ip6_src
= *src
;
1252 nd_na
= (struct nd_neighbor_advert
*)(ip6
+ 1);
1253 nd_na
->nd_na_type
= ND_NEIGHBOR_ADVERT
;
1254 nd_na
->nd_na_code
= 0;
1255 nd_na
->nd_na_target
= *taddr6
;
1256 in6_clearscope(&nd_na
->nd_na_target
); /* XXX */
1259 * "tlladdr" indicates NS's condition for adding tlladdr or not.
1260 * see nd6_ns_input() for details.
1261 * Basically, if NS packet is sent to unicast/anycast addr,
1262 * target lladdr option SHOULD NOT be included.
1266 * sdl0 != NULL indicates proxy NA. If we do proxy, use
1267 * lladdr in sdl0. If we are not proxying (sending NA for
1268 * my address) use lladdr configured for the interface.
1271 mac
= nd6_ifptomac(ifp
);
1272 else if (sdl0
->sa_family
== AF_LINK
) {
1273 struct sockaddr_dl
*sdl
;
1274 sdl
= (struct sockaddr_dl
*)sdl0
;
1275 if (sdl
->sdl_alen
== ifp
->if_addrlen
)
1279 if (tlladdr
&& mac
) {
1280 int optlen
= sizeof(struct nd_opt_hdr
) + ifp
->if_addrlen
;
1281 struct nd_opt_hdr
*nd_opt
= (struct nd_opt_hdr
*)(nd_na
+ 1);
1283 /* roundup to 8 bytes alignment! */
1284 optlen
= (optlen
+ 7) & ~7;
1286 m
->m_pkthdr
.len
+= optlen
;
1289 bzero((caddr_t
)nd_opt
, optlen
);
1290 nd_opt
->nd_opt_type
= ND_OPT_TARGET_LINKADDR
;
1291 nd_opt
->nd_opt_len
= optlen
>> 3;
1292 bcopy(mac
, (caddr_t
)(nd_opt
+ 1), ifp
->if_addrlen
);
1294 flags
&= ~ND_NA_FLAG_OVERRIDE
;
1296 ip6
->ip6_plen
= htons((u_short
)icmp6len
);
1297 nd_na
->nd_na_flags_reserved
= flags
;
1298 nd_na
->nd_na_cksum
= 0;
1299 nd_na
->nd_na_cksum
=
1300 in6_cksum(m
, IPPROTO_ICMPV6
, sizeof(struct ip6_hdr
), icmp6len
);
1303 /* Don't lookup socket */
1304 if (ipsec_bypass
== 0)
1305 (void)ipsec_setsocket(m
, NULL
);
1307 ip6_output(m
, NULL
, NULL
, IPV6_OUTARGS
, im6o
, &outif
, &ip6oa
);
1309 icmp6_ifstat_inc(outif
, ifs6_out_msg
);
1310 icmp6_ifstat_inc(outif
, ifs6_out_neighboradvert
);
1311 ifnet_release(outif
);
1313 icmp6stat
.icp6s_outhist
[ND_NEIGHBOR_ADVERT
]++;
1336 switch (ifp
->if_type
) {
1339 case IFT_IEEE8023ADLAG
:
1345 #ifdef IFT_IEEE80211
1353 return ((caddr_t
)ifnet_lladdr(ifp
));
1359 TAILQ_HEAD(dadq_head
, dadq
);
1361 decl_lck_mtx_data(, dad_lock
);
1362 u_int32_t dad_refcount
; /* reference count */
1364 TAILQ_ENTRY(dadq
) dad_list
;
1365 struct ifaddr
*dad_ifa
;
1366 int dad_count
; /* max NS to send */
1367 int dad_ns_tcount
; /* # of trials to send NS */
1368 int dad_ns_ocount
; /* NS sent so far */
1371 int dad_na_ixcount
; /* Count of IFDISABLED eligible NA rx'd */
1374 static struct dadq_head dadq
;
1381 dad_size
= sizeof (struct dadq
);
1382 dad_zone
= zinit(dad_size
, DAD_ZONE_MAX
* dad_size
, 0, DAD_ZONE_NAME
);
1383 if (dad_zone
== NULL
) {
1384 panic("%s: failed allocating %s", __func__
, DAD_ZONE_NAME
);
1387 zone_change(dad_zone
, Z_EXPAND
, TRUE
);
1388 zone_change(dad_zone
, Z_CALLERACCT
, FALSE
);
1391 static struct dadq
*
1392 nd6_dad_find(struct ifaddr
*ifa
)
1396 lck_mtx_lock(dad6_mutex
);
1397 for (dp
= dadq
.tqh_first
; dp
; dp
= dp
->dad_list
.tqe_next
) {
1399 if (dp
->dad_ifa
== ifa
) {
1400 DAD_ADDREF_LOCKED(dp
);
1402 lck_mtx_unlock(dad6_mutex
);
1407 lck_mtx_unlock(dad6_mutex
);
1416 untimeout((void (*)(void *))nd6_dad_timer
, (void *)ifa
);
1420 * Start Duplicate Address Detection (DAD) for specified interface address.
1425 int *tick_delay
) /* minimum delay ticks for IFF_UP event */
1427 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1431 * If we don't need DAD, don't do it.
1432 * There are several cases:
1433 * - DAD is disabled (ip6_dad_count == 0)
1434 * - the interface address is anycast
1436 IFA_LOCK(&ia
->ia_ifa
);
1437 if (!(ia
->ia6_flags
& IN6_IFF_TENTATIVE
)) {
1439 "nd6_dad_start: called with non-tentative address "
1441 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1442 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1443 IFA_UNLOCK(&ia
->ia_ifa
);
1446 if (ia
->ia6_flags
& IN6_IFF_ANYCAST
) {
1447 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1448 IFA_UNLOCK(&ia
->ia_ifa
);
1451 if (!ip6_dad_count
) {
1452 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1453 IFA_UNLOCK(&ia
->ia_ifa
);
1456 IFA_UNLOCK(&ia
->ia_ifa
);
1457 if (ifa
->ifa_ifp
== NULL
)
1458 panic("nd6_dad_start: ifa->ifa_ifp == NULL");
1459 if (!(ifa
->ifa_ifp
->if_flags
& IFF_UP
)) {
1462 if ((dp
= nd6_dad_find(ifa
)) != NULL
) {
1464 /* DAD already in progress */
1468 dp
= zalloc(dad_zone
);
1470 log(LOG_ERR
, "nd6_dad_start: memory allocation failed for "
1472 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1473 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1476 bzero(dp
, dad_size
);
1477 lck_mtx_init(&dp
->dad_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
1479 /* Callee adds one reference for us */
1480 dp
= nd6_dad_attach(dp
, ifa
);
1482 nd6log((LOG_DEBUG
, "%s: starting DAD for %s\n", if_name(ifa
->ifa_ifp
),
1483 ip6_sprintf(&ia
->ia_addr
.sin6_addr
)));
1486 * Send NS packet for DAD, ip6_dad_count times.
1487 * Note that we must delay the first transmission, if this is the
1488 * first packet to be sent from the interface after interface
1489 * (re)initialization.
1491 if (tick_delay
== NULL
) {
1493 nd6_dad_ns_output(dp
, ifa
);
1494 lck_rw_lock_shared(nd_if_rwlock
);
1495 retrans
= nd_ifinfo
[ifa
->ifa_ifp
->if_index
].retrans
* hz
/ 1000;
1496 lck_rw_done(nd_if_rwlock
);
1497 timeout((void (*)(void *))nd6_dad_timer
, (void *)ifa
, retrans
);
1501 if (*tick_delay
== 0)
1502 ntick
= random() % (MAX_RTR_SOLICITATION_DELAY
* hz
);
1504 ntick
= *tick_delay
+ random() % (hz
/ 2);
1505 *tick_delay
= ntick
;
1506 timeout((void (*)(void *))nd6_dad_timer
, (void *)ifa
,
1510 DAD_REMREF(dp
); /* drop our reference */
1513 static struct dadq
*
1514 nd6_dad_attach(struct dadq
*dp
, struct ifaddr
*ifa
)
1516 lck_mtx_lock(dad6_mutex
);
1519 IFA_ADDREF(ifa
); /* for dad_ifa */
1520 dp
->dad_count
= ip6_dad_count
;
1521 dp
->dad_ns_icount
= dp
->dad_na_icount
= 0;
1522 dp
->dad_ns_ocount
= dp
->dad_ns_tcount
= 0;
1523 dp
->dad_na_ixcount
= 0;
1524 VERIFY(!dp
->dad_attached
);
1525 dp
->dad_attached
= 1;
1526 DAD_ADDREF_LOCKED(dp
); /* for caller */
1527 DAD_ADDREF_LOCKED(dp
); /* for dadq_head list */
1528 TAILQ_INSERT_TAIL(&dadq
, (struct dadq
*)dp
, dad_list
);
1530 lck_mtx_unlock(dad6_mutex
);
1536 nd6_dad_detach(struct dadq
*dp
, struct ifaddr
*ifa
)
1540 lck_mtx_lock(dad6_mutex
);
1542 if ((detached
= dp
->dad_attached
)) {
1543 VERIFY(dp
->dad_ifa
== ifa
);
1544 TAILQ_REMOVE(&dadq
, (struct dadq
*)dp
, dad_list
);
1545 dp
->dad_list
.tqe_next
= NULL
;
1546 dp
->dad_list
.tqe_prev
= NULL
;
1547 dp
->dad_attached
= 0;
1550 lck_mtx_unlock(dad6_mutex
);
1552 DAD_REMREF(dp
); /* drop dadq_head reference */
1557 * terminate DAD unconditionally. used for address removals.
1560 nd6_dad_stop(struct ifaddr
*ifa
)
1564 dp
= nd6_dad_find(ifa
);
1566 /* DAD wasn't started yet */
1570 untimeout((void (*)(void *))nd6_dad_timer
, (void *)ifa
);
1572 nd6_dad_detach(dp
, ifa
);
1573 DAD_REMREF(dp
); /* drop our reference */
1578 nd6_unsol_na_output(struct ifaddr
*ifa
)
1580 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1581 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1582 struct in6_addr saddr6
, taddr6
;
1584 if ((ifp
->if_flags
& IFF_UP
) == 0 ||
1585 (ifp
->if_flags
& IFF_RUNNING
) == 0)
1588 IFA_LOCK_SPIN(&ia
->ia_ifa
);
1589 taddr6
= ia
->ia_addr
.sin6_addr
;
1590 IFA_UNLOCK(&ia
->ia_ifa
);
1591 if (in6_setscope(&taddr6
, ifp
, NULL
) != 0)
1593 saddr6
= in6addr_linklocal_allnodes
;
1594 if (in6_setscope(&saddr6
, ifp
, NULL
) != 0)
1597 nd6log((LOG_INFO
, "%s: sending unsolicited NA\n",
1598 if_name(ifa
->ifa_ifp
)));
1600 nd6_na_output(ifp
, &saddr6
, &taddr6
, ND_NA_FLAG_OVERRIDE
, 1, NULL
);
1604 nd6_dad_timer(struct ifaddr
*ifa
)
1606 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1607 struct dadq
*dp
= NULL
;
1611 log(LOG_ERR
, "nd6_dad_timer: called with null parameter\n");
1614 dp
= nd6_dad_find(ifa
);
1616 log(LOG_ERR
, "nd6_dad_timer: DAD structure not found\n");
1619 IFA_LOCK(&ia
->ia_ifa
);
1620 if (ia
->ia6_flags
& IN6_IFF_DUPLICATED
) {
1621 log(LOG_ERR
, "nd6_dad_timer: called with duplicated address "
1623 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1624 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1625 IFA_UNLOCK(&ia
->ia_ifa
);
1628 if ((ia
->ia6_flags
& IN6_IFF_TENTATIVE
) == 0) {
1629 log(LOG_ERR
, "nd6_dad_timer: called with non-tentative address "
1631 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1632 ifa
->ifa_ifp
? if_name(ifa
->ifa_ifp
) : "???");
1633 IFA_UNLOCK(&ia
->ia_ifa
);
1636 IFA_UNLOCK(&ia
->ia_ifa
);
1638 /* timeouted with IFF_{RUNNING,UP} check */
1640 if (dp
->dad_ns_tcount
> dad_maxtry
) {
1642 nd6log((LOG_INFO
, "%s: could not run DAD, driver problem?\n",
1643 if_name(ifa
->ifa_ifp
)));
1645 nd6_dad_detach(dp
, ifa
);
1649 /* Need more checks? */
1650 if (dp
->dad_ns_ocount
< dp
->dad_count
) {
1654 * We have more NS to go. Send NS packet for DAD.
1656 nd6_dad_ns_output(dp
, ifa
);
1657 lck_rw_lock_shared(nd_if_rwlock
);
1658 retrans
= nd_ifinfo
[ifa
->ifa_ifp
->if_index
].retrans
* hz
/ 1000;
1659 lck_rw_done(nd_if_rwlock
);
1660 timeout((void (*)(void *))nd6_dad_timer
, (void *)ifa
, retrans
);
1663 * We have transmitted sufficient number of DAD packets.
1664 * See what we've got.
1670 if (dp
->dad_na_icount
) {
1672 * the check is in nd6_dad_na_input(),
1678 if (dp
->dad_ns_icount
) {
1679 /* We've seen NS, means DAD has failed. */
1685 /* (*dp) will be freed in nd6_dad_duplicated() */
1686 nd6_dad_duplicated(ifa
, TRUE
);
1689 * We are done with DAD. No NA came, no NS came.
1690 * No duplicate address found.
1692 IFA_LOCK_SPIN(&ia
->ia_ifa
);
1693 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1694 IFA_UNLOCK(&ia
->ia_ifa
);
1697 "%s: DAD complete for %s - no duplicates found\n",
1698 if_name(ifa
->ifa_ifp
),
1699 ip6_sprintf(&ia
->ia_addr
.sin6_addr
)));
1701 * Send an Unsolicited Neighbor Advertisement so that
1702 * other machines on the network are aware of us
1703 * (important when we are waking from sleep).
1705 nd6_unsol_na_output(ifa
);
1706 in6_post_msg(ia
->ia_ifp
, KEV_INET6_NEW_USER_ADDR
, ia
);
1707 nd6_dad_detach(dp
, ifa
);
1713 DAD_REMREF(dp
); /* drop our reference */
1717 nd6_dad_duplicated(struct ifaddr
*ifa
, boolean_t dontignhwdup
)
1719 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1721 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1724 dp
= nd6_dad_find(ifa
);
1726 log(LOG_ERR
, "nd6_dad_duplicated: DAD structure not found\n");
1731 IFA_LOCK(&ia
->ia_ifa
);
1733 log(LOG_ERR
, "%s: DAD detected duplicate IPv6 address %s: "
1734 "NS in/out=%d/%d, NA in=%d inx=%d\n",
1735 if_name(ifp
), ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1736 dp
->dad_ns_icount
, dp
->dad_ns_ocount
, dp
->dad_na_icount
,
1737 dp
->dad_na_ixcount
);
1738 hwdupposs
= dp
->dad_na_ixcount
;
1740 ia
->ia6_flags
&= ~IN6_IFF_TENTATIVE
;
1741 ia
->ia6_flags
|= IN6_IFF_DUPLICATED
;
1742 IFA_UNLOCK(&ia
->ia_ifa
);
1744 /* We are done with DAD, with duplicated address found. (failure) */
1745 untimeout((void (*)(void *))nd6_dad_timer
, (void *)ifa
);
1747 IFA_LOCK(&ia
->ia_ifa
);
1748 log(LOG_ERR
, "%s: DAD complete for %s - duplicate found\n",
1749 if_name(ifp
), ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1750 log(LOG_ERR
, "%s: manual intervention required\n",
1752 IFA_UNLOCK(&ia
->ia_ifa
);
1755 (dontignhwdup
&& IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
))) {
1756 log(LOG_ERR
, "%s: possible hardware address duplication "
1757 "detected, disable IPv6\n", if_name(ifp
));
1759 lck_rw_lock_shared(nd_if_rwlock
);
1760 nd_ifinfo
[ifp
->if_index
].flags
|=
1762 lck_rw_done(nd_if_rwlock
);
1765 /* Send an event to the configuration agent so that the
1766 * duplicate address will be notified to the user and will
1769 in6_post_msg(ifp
, KEV_INET6_NEW_USER_ADDR
, ia
);
1770 nd6_dad_detach(dp
, ifa
);
1771 DAD_REMREF(dp
); /* drop our reference */
1775 nd6_dad_ns_output(struct dadq
*dp
, struct ifaddr
*ifa
)
1777 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1778 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1779 struct in6_addr taddr6
;
1782 dp
->dad_ns_tcount
++;
1783 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1787 if ((ifp
->if_flags
& IFF_RUNNING
) == 0) {
1792 dp
->dad_ns_ocount
++;
1794 IFA_LOCK_SPIN(&ia
->ia_ifa
);
1795 taddr6
= ia
->ia_addr
.sin6_addr
;
1796 IFA_UNLOCK(&ia
->ia_ifa
);
1797 nd6_ns_output(ifp
, NULL
, &taddr6
, NULL
, 1);
1801 nd6_dad_ns_input(struct ifaddr
*ifa
)
1808 panic("ifa == NULL in nd6_dad_ns_input");
1812 dp
= nd6_dad_find(ifa
);
1814 /* Quickhack - completely ignore DAD NS packets */
1815 if (dad_ignore_ns
) {
1816 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1817 IFA_LOCK(&ia
->ia_ifa
);
1819 "nd6_dad_ns_input: ignoring DAD NS packet for "
1820 "address %s(%s)\n", ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1821 if_name(ifa
->ifa_ifp
)));
1822 IFA_UNLOCK(&ia
->ia_ifa
);
1827 * if I'm yet to start DAD, someone else started using this address
1828 * first. I have a duplicate and you win.
1832 if (dp
== NULL
|| dp
->dad_ns_ocount
== 0)
1835 /* XXX more checks for loopback situation - see nd6_dad_timer too */
1843 nd6_dad_duplicated(ifa
, TRUE
);
1844 } else if (dp
!= NULL
) {
1846 * not sure if I got a duplicate.
1847 * increment ns count and see what happens.
1849 dp
->dad_ns_icount
++;
1856 nd6_dad_na_input(struct ifaddr
*ifa
, caddr_t lladdr
, int lladdrlen
)
1858 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
1863 panic("ifa == NULL in nd6_dad_na_input");
1865 dp
= nd6_dad_find(ifa
);
1867 log(LOG_ERR
, "nd6_dad_na_input: DAD structure not found\n");
1872 * If the address is a link-local address formed from an interface
1873 * identifier based on the hardware address which is supposed to be
1874 * uniquely assigned (e.g., EUI-64 for an Ethernet interface), IP
1875 * operation on the interface SHOULD be disabled according to RFC 4862,
1876 * section 5.4.5, but here we decide not to disable if the target
1877 * hardware address is not also ours, which is a transitory possibility
1878 * in the presence of network-resident sleep proxies on the local link.
1882 if (IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
)) {
1884 struct in6_addr in6
;
1890 * To avoid over-reaction, we only apply this logic when we are
1891 * very sure that hardware addresses are supposed to be unique.
1893 switch (ifp
->if_type
) {
1899 #ifdef IFT_IEEE80211
1902 /* Check if our hardware address matches the target */
1903 if (lladdr
!= NULL
&& lladdrlen
> 0) {
1904 struct ifaddr
*llifa
;
1905 struct sockaddr_dl
*sdl
;
1907 llifa
= ifp
->if_lladdr
;
1909 sdl
= (struct sockaddr_dl
*)llifa
->ifa_addr
;
1910 if (lladdrlen
== sdl
->sdl_alen
||
1911 bcmp(lladdr
, LLADDR(sdl
), lladdrlen
) == 0)
1915 in6
= ia
->ia_addr
.sin6_addr
;
1916 if (in6_get_hw_ifid(ifp
, &in6
) != 0)
1919 * Apply this logic only to the EUI-64 form of
1920 * link-local interface identifiers.
1924 !IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, &in6
)) {
1926 } else if (lladdr
== NULL
&&
1927 IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
, &in6
)) {
1929 * We received a NA with no target link-layer
1930 * address option. This means that someone else
1931 * has our address. Mark it as a hardware
1932 * duplicate so we disable IPv6 later on.
1946 dp
->dad_na_icount
++;
1948 dp
->dad_na_ixcount
++;
1952 /* remove the address. */
1953 nd6_dad_duplicated(ifa
, FALSE
);
1957 dad_addref(struct dadq
*dp
, int locked
)
1962 DAD_LOCK_ASSERT_HELD(dp
);
1964 if (++dp
->dad_refcount
== 0) {
1965 panic("%s: dad %p wraparound refcnt\n", __func__
, dp
);
1973 dad_remref(struct dadq
*dp
)
1978 if (dp
->dad_refcount
== 0)
1979 panic("%s: dad %p negative refcnt\n", __func__
, dp
);
1981 if (dp
->dad_refcount
> 0) {
1987 if (dp
->dad_attached
||
1988 dp
->dad_list
.tqe_next
!= NULL
|| dp
->dad_list
.tqe_prev
!= NULL
) {
1989 panic("%s: attached dad=%p is being freed", __func__
, dp
);
1993 if ((ifa
= dp
->dad_ifa
) != NULL
) {
1994 IFA_REMREF(ifa
); /* drop dad_ifa reference */
1998 lck_mtx_destroy(&dp
->dad_lock
, ifa_mtx_grp
);
1999 zfree(dad_zone
, dp
);
2003 nd6_llreach_set_reachable(struct ifnet
*ifp
, void *addr
, unsigned int alen
)
2005 /* Nothing more to do if it's disabled */
2006 if (nd6_llreach_base
== 0)
2009 ifnet_llreach_set_reachable(ifp
, ETHERTYPE_IPV6
, addr
, alen
);