2 * Copyright (c) 2003-2020 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@
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
59 * Copyright (c) 1982, 1986, 1991, 1993
60 * The Regents of the University of California. All rights reserved.
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in the
69 * documentation and/or other materials provided with the distribution.
70 * 3. All advertising materials mentioning features or use of this software
71 * must display the following acknowledgement:
72 * This product includes software developed by the University of
73 * California, Berkeley and its contributors.
74 * 4. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
90 * @(#)in.c 8.2 (Berkeley) 11/15/93
94 #include <sys/param.h>
95 #include <sys/ioctl.h>
96 #include <sys/errno.h>
97 #include <sys/malloc.h>
98 #include <sys/socket.h>
99 #include <sys/socketvar.h>
100 #include <sys/sockio.h>
101 #include <sys/systm.h>
102 #include <sys/time.h>
103 #include <sys/kernel.h>
104 #include <sys/syslog.h>
105 #include <sys/kern_event.h>
106 #include <sys/mcache.h>
107 #include <sys/protosw.h>
108 #include <sys/sysctl.h>
110 #include <kern/locks.h>
111 #include <kern/zalloc.h>
112 #include <kern/clock.h>
113 #include <libkern/OSAtomic.h>
114 #include <machine/machine_routines.h>
115 #include <mach/boolean.h>
118 #include <net/if_types.h>
119 #include <net/if_var.h>
120 #include <net/route.h>
121 #include <net/if_dl.h>
122 #include <net/kpi_protocol.h>
123 #include <net/nwk_wq.h>
125 #include <netinet/in.h>
126 #include <netinet/in_var.h>
127 #include <netinet/if_ether.h>
128 #include <netinet/in_systm.h>
129 #include <netinet/ip.h>
130 #include <netinet/in_pcb.h>
131 #include <netinet/icmp6.h>
132 #include <netinet/tcp.h>
133 #include <netinet/tcp_seq.h>
134 #include <netinet/tcp_var.h>
136 #include <netinet6/nd6.h>
137 #include <netinet/ip6.h>
138 #include <netinet6/ip6_var.h>
139 #include <netinet6/mld6_var.h>
140 #include <netinet6/in6_ifattach.h>
141 #include <netinet6/scope6_var.h>
142 #include <netinet6/in6_var.h>
143 #include <netinet6/in6_pcb.h>
145 #include <net/net_osdep.h>
147 #include <net/dlil.h>
148 #include <net/if_llatbl.h>
151 #include <net/pfvar.h>
155 * Definitions of some costant IP6 addresses.
157 const struct in6_addr in6addr_any
= IN6ADDR_ANY_INIT
;
158 const struct in6_addr in6addr_loopback
= IN6ADDR_LOOPBACK_INIT
;
159 const struct in6_addr in6addr_nodelocal_allnodes
=
160 IN6ADDR_NODELOCAL_ALLNODES_INIT
;
161 const struct in6_addr in6addr_linklocal_allnodes
=
162 IN6ADDR_LINKLOCAL_ALLNODES_INIT
;
163 const struct in6_addr in6addr_linklocal_allrouters
=
164 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT
;
165 const struct in6_addr in6addr_linklocal_allv2routers
=
166 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT
;
167 const struct in6_addr in6addr_multicast_prefix
=
168 IN6ADDR_MULTICAST_PREFIX
;
170 const struct in6_addr in6mask0
= IN6MASK0
;
171 const struct in6_addr in6mask7
= IN6MASK7
;
172 const struct in6_addr in6mask8
= IN6MASK8
;
173 const struct in6_addr in6mask16
= IN6MASK16
;
174 const struct in6_addr in6mask32
= IN6MASK32
;
175 const struct in6_addr in6mask64
= IN6MASK64
;
176 const struct in6_addr in6mask96
= IN6MASK96
;
177 const struct in6_addr in6mask128
= IN6MASK128
;
179 const struct sockaddr_in6 sa6_any
= {
180 .sin6_len
= sizeof(sa6_any
),
181 .sin6_family
= AF_INET6
,
184 .sin6_addr
= IN6ADDR_ANY_INIT
,
188 static int in6ctl_associd(struct socket
*, u_long
, caddr_t
);
189 static int in6ctl_connid(struct socket
*, u_long
, caddr_t
);
190 static int in6ctl_conninfo(struct socket
*, u_long
, caddr_t
);
191 static int in6ctl_llstart(struct ifnet
*, u_long
, caddr_t
);
192 static int in6ctl_llstop(struct ifnet
*);
193 static int in6ctl_cgastart(struct ifnet
*, u_long
, caddr_t
);
194 static int in6ctl_gifaddr(struct ifnet
*, struct in6_ifaddr
*, u_long
,
196 static int in6ctl_gifstat(struct ifnet
*, u_long
, struct in6_ifreq
*);
197 static int in6ctl_alifetime(struct in6_ifaddr
*, u_long
, struct in6_ifreq
*,
199 static int in6ctl_aifaddr(struct ifnet
*, struct in6_aliasreq
*);
200 static void in6ctl_difaddr(struct ifnet
*, struct in6_ifaddr
*);
201 static int in6_autoconf(struct ifnet
*, int);
202 static int in6_setrouter(struct ifnet
*, ipv6_router_mode_t
);
203 static int in6_ifinit(struct ifnet
*, struct in6_ifaddr
*, int);
204 static int in6_ifaupdate_aux(struct in6_ifaddr
*, struct ifnet
*, int);
205 static void in6_unlink_ifa(struct in6_ifaddr
*, struct ifnet
*);
206 static struct in6_ifaddr
*in6_ifaddr_alloc(zalloc_flags_t
);
207 static void in6_ifaddr_attached(struct ifaddr
*);
208 static void in6_ifaddr_detached(struct ifaddr
*);
209 static void in6_ifaddr_free(struct ifaddr
*);
210 static void in6_ifaddr_trace(struct ifaddr
*, int);
211 #if defined(__LP64__)
212 static void in6_cgareq_32_to_64(const struct in6_cgareq_32
*,
213 struct in6_cgareq_64
*);
215 static void in6_cgareq_64_to_32(const struct in6_cgareq_64
*,
216 struct in6_cgareq_32
*);
218 static struct in6_aliasreq
*in6_aliasreq_to_native(void *, int,
219 struct in6_aliasreq
*);
220 static int in6_to_kamescope(struct sockaddr_in6
*, struct ifnet
*);
221 static int in6_getassocids(struct socket
*, uint32_t *, user_addr_t
);
222 static int in6_getconnids(struct socket
*, sae_associd_t
, uint32_t *,
225 static void in6_if_up_dad_start(struct ifnet
*);
227 #define IA6_HASH_INIT(ia) { \
228 (ia)->ia6_hash.tqe_next = (void *)(uintptr_t)-1; \
229 (ia)->ia6_hash.tqe_prev = (void *)(uintptr_t)-1; \
232 #define IA6_IS_HASHED(ia) \
233 (!((ia)->ia6_hash.tqe_next == (void *)(uintptr_t)-1 || \
234 (ia)->ia6_hash.tqe_prev == (void *)(uintptr_t)-1))
236 static void in6_iahash_remove(struct in6_ifaddr
*);
237 static void in6_iahash_insert(struct in6_ifaddr
*);
238 static void in6_iahash_insert_ptp(struct in6_ifaddr
*);
240 extern lck_mtx_t
*nd6_mutex
;
242 #define IN6IFA_TRACE_HIST_SIZE 32 /* size of trace history */
245 __private_extern__
unsigned int in6ifa_trace_hist_size
= IN6IFA_TRACE_HIST_SIZE
;
247 struct in6_ifaddr_dbg
{
248 struct in6_ifaddr in6ifa
; /* in6_ifaddr */
249 struct in6_ifaddr in6ifa_old
; /* saved in6_ifaddr */
250 u_int16_t in6ifa_refhold_cnt
; /* # of IFA_ADDREF */
251 u_int16_t in6ifa_refrele_cnt
; /* # of IFA_REMREF */
253 * Alloc and free callers.
255 ctrace_t in6ifa_alloc
;
256 ctrace_t in6ifa_free
;
258 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
260 ctrace_t in6ifa_refhold
[IN6IFA_TRACE_HIST_SIZE
];
261 ctrace_t in6ifa_refrele
[IN6IFA_TRACE_HIST_SIZE
];
265 TAILQ_ENTRY(in6_ifaddr_dbg
) in6ifa_trash_link
;
268 /* List of trash in6_ifaddr entries protected by in6ifa_trash_lock */
269 static TAILQ_HEAD(, in6_ifaddr_dbg
) in6ifa_trash_head
;
270 static decl_lck_mtx_data(, in6ifa_trash_lock
);
273 static unsigned int in6ifa_debug
= 1; /* debugging (enabled) */
275 static unsigned int in6ifa_debug
; /* debugging (disabled) */
277 static struct zone
*in6ifa_zone
; /* zone for in6_ifaddr */
278 #define IN6IFA_ZONE_NAME "in6_ifaddr" /* zone name */
280 struct eventhandler_lists_ctxt in6_evhdlr_ctxt
;
281 struct eventhandler_lists_ctxt in6_clat46_evhdlr_ctxt
;
283 * Subroutine for in6_ifaddloop() and in6_ifremloop().
284 * This routine does actual work.
287 in6_ifloop_request(int cmd
, struct ifaddr
*ifa
)
289 struct sockaddr_in6 all1_sa
;
290 struct rtentry
*nrt
= NULL
;
293 bzero(&all1_sa
, sizeof(all1_sa
));
294 all1_sa
.sin6_family
= AF_INET6
;
295 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
296 all1_sa
.sin6_addr
= in6mask128
;
299 * We specify the address itself as the gateway, and set the
300 * RTF_LLINFO flag, so that the corresponding host route would have
301 * the flag, and thus applications that assume traditional behavior
302 * would be happy. Note that we assume the caller of the function
303 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
304 * which changes the outgoing interface to the loopback interface.
305 * ifa_addr for INET6 is set once during init; no need to hold lock.
307 lck_mtx_lock(rnh_lock
);
308 e
= rtrequest_locked(cmd
, ifa
->ifa_addr
, ifa
->ifa_addr
,
309 (struct sockaddr
*)&all1_sa
, RTF_UP
| RTF_HOST
| RTF_LLINFO
, &nrt
);
311 log(LOG_ERR
, "in6_ifloop_request: "
312 "%s operation failed for %s (errno=%d)\n",
313 cmd
== RTM_ADD
? "ADD" : "DELETE",
314 ip6_sprintf(&((struct in6_ifaddr
*)ifa
)->ia_addr
.sin6_addr
),
322 * Make sure rt_ifa be equal to IFA, the second argument of the
324 * We need this because when we refer to rt_ifa->ia6_flags in
325 * ip6_input, we assume that the rt_ifa points to the address instead
326 * of the loopback address.
328 if (cmd
== RTM_ADD
&& nrt
&& ifa
!= nrt
->rt_ifa
) {
333 * Report the addition/removal of the address to the routing socket.
334 * XXX: since we called rtinit for a p2p interface with a destination,
335 * we end up reporting twice in such a case. Should we rather
336 * omit the second report?
339 rt_newaddrmsg(cmd
, ifa
, e
, nrt
);
340 if (cmd
== RTM_DELETE
) {
344 /* the cmd must be RTM_ADD here */
345 RT_REMREF_LOCKED(nrt
);
349 lck_mtx_unlock(rnh_lock
);
353 * Add ownaddr as loopback rtentry. We previously add the route only if
354 * necessary (ex. on a p2p link). However, since we now manage addresses
355 * separately from prefixes, we should always add the route. We can't
356 * rely on the cloning mechanism from the corresponding interface route
360 in6_ifaddloop(struct ifaddr
*ifa
)
365 * If there is no loopback entry, allocate one. ifa_addr for
366 * INET6 is set once during init; no need to hold lock.
368 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
372 if (rt
== NULL
|| (rt
->rt_flags
& RTF_HOST
) == 0 ||
373 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) == 0) {
375 RT_REMREF_LOCKED(rt
);
378 in6_ifloop_request(RTM_ADD
, ifa
);
379 } else if (rt
!= NULL
) {
380 RT_REMREF_LOCKED(rt
);
386 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
390 in6_ifremloop(struct ifaddr
*ifa
)
392 struct in6_ifaddr
*ia
;
397 * Some of BSD variants do not remove cloned routes
398 * from an interface direct route, when removing the direct route
399 * (see comments in net/net_osdep.h). Even for variants that do remove
400 * cloned routes, they could fail to remove the cloned routes when
401 * we handle multple addresses that share a common prefix.
402 * So, we should remove the route corresponding to the deleted address
403 * regardless of the result of in6_is_ifloop_auto().
407 * Delete the entry only if exact one ifa exists. More than one ifa
408 * can exist if we assign a same single address to multiple
409 * (probably p2p) interfaces.
410 * XXX: we should avoid such a configuration in IPv6...
412 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
413 TAILQ_FOREACH(ia
, IN6ADDR_HASH(IFA_IN6(ifa
)), ia6_hash
) {
414 IFA_LOCK(&ia
->ia_ifa
);
415 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa
), &ia
->ia_addr
.sin6_addr
)) {
418 IFA_UNLOCK(&ia
->ia_ifa
);
422 IFA_UNLOCK(&ia
->ia_ifa
);
424 lck_rw_done(&in6_ifaddr_rwlock
);
428 * Before deleting, check if a corresponding loopbacked host
429 * route surely exists. With this check, we can avoid to
430 * delete an interface direct route whose destination is same
431 * as the address being removed. This can happen when removing
432 * a subnet-router anycast address on an interface attahced
433 * to a shared medium. ifa_addr for INET6 is set once during
434 * init; no need to hold lock.
436 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
439 if ((rt
->rt_flags
& RTF_HOST
) != 0 &&
440 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
441 RT_REMREF_LOCKED(rt
);
443 in6_ifloop_request(RTM_DELETE
, ifa
);
453 in6_mask2len(struct in6_addr
*mask
, u_char
*lim0
)
456 u_char
*lim
= lim0
, *p
;
458 /* ignore the scope_id part */
459 if (lim0
== NULL
|| lim0
- (u_char
*)mask
> sizeof(*mask
)) {
460 lim
= (u_char
*)mask
+ sizeof(*mask
);
462 for (p
= (u_char
*)mask
; p
< lim
; x
++, p
++) {
469 for (y
= 0; y
< 8; y
++) {
470 if ((*p
& (0x80 >> y
)) == 0) {
477 * when the limit pointer is given, do a stricter check on the
481 if (y
!= 0 && (*p
& (0x00ff >> y
)) != 0) {
484 for (p
= p
+ 1; p
< lim
; p
++) {
495 in6_len2mask(struct in6_addr
*mask
, int len
)
499 bzero(mask
, sizeof(*mask
));
500 for (i
= 0; i
< len
/ 8; i
++) {
501 mask
->s6_addr8
[i
] = 0xff;
504 mask
->s6_addr8
[i
] = (0xff00 >> (len
% 8)) & 0xff;
509 in6_aliasreq_64_to_32(struct in6_aliasreq_64
*src
, struct in6_aliasreq_32
*dst
)
511 bzero(dst
, sizeof(*dst
));
512 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof(dst
->ifra_name
));
513 dst
->ifra_addr
= src
->ifra_addr
;
514 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
515 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
516 dst
->ifra_flags
= src
->ifra_flags
;
517 dst
->ifra_lifetime
.ia6t_expire
= (u_int32_t
)src
->ifra_lifetime
.ia6t_expire
;
518 dst
->ifra_lifetime
.ia6t_preferred
= (u_int32_t
)src
->ifra_lifetime
.ia6t_preferred
;
519 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
520 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
524 in6_aliasreq_32_to_64(struct in6_aliasreq_32
*src
, struct in6_aliasreq_64
*dst
)
526 bzero(dst
, sizeof(*dst
));
527 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof(dst
->ifra_name
));
528 dst
->ifra_addr
= src
->ifra_addr
;
529 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
530 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
531 dst
->ifra_flags
= src
->ifra_flags
;
532 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
533 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
534 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
535 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
538 #if defined(__LP64__)
540 in6_cgareq_32_to_64(const struct in6_cgareq_32
*src
,
541 struct in6_cgareq_64
*dst
)
543 bzero(dst
, sizeof(*dst
));
544 bcopy(src
->cgar_name
, dst
->cgar_name
, sizeof(dst
->cgar_name
));
545 dst
->cgar_flags
= src
->cgar_flags
;
546 bcopy(src
->cgar_cgaprep
.cga_modifier
.octets
,
547 dst
->cgar_cgaprep
.cga_modifier
.octets
,
548 sizeof(dst
->cgar_cgaprep
.cga_modifier
.octets
));
549 dst
->cgar_cgaprep
.cga_security_level
=
550 src
->cgar_cgaprep
.cga_security_level
;
551 dst
->cgar_lifetime
.ia6t_expire
= src
->cgar_lifetime
.ia6t_expire
;
552 dst
->cgar_lifetime
.ia6t_preferred
= src
->cgar_lifetime
.ia6t_preferred
;
553 dst
->cgar_lifetime
.ia6t_vltime
= src
->cgar_lifetime
.ia6t_vltime
;
554 dst
->cgar_lifetime
.ia6t_pltime
= src
->cgar_lifetime
.ia6t_pltime
;
555 dst
->cgar_collision_count
= src
->cgar_collision_count
;
559 #if !defined(__LP64__)
561 in6_cgareq_64_to_32(const struct in6_cgareq_64
*src
,
562 struct in6_cgareq_32
*dst
)
564 bzero(dst
, sizeof(*dst
));
565 bcopy(src
->cgar_name
, dst
->cgar_name
, sizeof(dst
->cgar_name
));
566 dst
->cgar_flags
= src
->cgar_flags
;
567 bcopy(src
->cgar_cgaprep
.cga_modifier
.octets
,
568 dst
->cgar_cgaprep
.cga_modifier
.octets
,
569 sizeof(dst
->cgar_cgaprep
.cga_modifier
.octets
));
570 dst
->cgar_cgaprep
.cga_security_level
=
571 src
->cgar_cgaprep
.cga_security_level
;
572 dst
->cgar_lifetime
.ia6t_expire
= src
->cgar_lifetime
.ia6t_expire
;
573 dst
->cgar_lifetime
.ia6t_preferred
= src
->cgar_lifetime
.ia6t_preferred
;
574 dst
->cgar_lifetime
.ia6t_vltime
= src
->cgar_lifetime
.ia6t_vltime
;
575 dst
->cgar_lifetime
.ia6t_pltime
= src
->cgar_lifetime
.ia6t_pltime
;
576 dst
->cgar_collision_count
= src
->cgar_collision_count
;
580 static struct in6_aliasreq
*
581 in6_aliasreq_to_native(void *data
, int data_is_64
, struct in6_aliasreq
*dst
)
583 #if defined(__LP64__)
585 bcopy(data
, dst
, sizeof(*dst
));
587 in6_aliasreq_32_to_64((struct in6_aliasreq_32
*)data
,
588 (struct in6_aliasreq_64
*)dst
);
592 in6_aliasreq_64_to_32((struct in6_aliasreq_64
*)data
,
593 (struct in6_aliasreq_32
*)dst
);
595 bcopy(data
, dst
, sizeof(*dst
));
597 #endif /* __LP64__ */
602 in6_cgareq_copy_from_user(const void *user_data
, int user_is_64
,
603 struct in6_cgareq
*cgareq
)
605 #if defined(__LP64__)
607 bcopy(user_data
, cgareq
, sizeof(*cgareq
));
609 in6_cgareq_32_to_64((const struct in6_cgareq_32
*)user_data
,
610 (struct in6_cgareq_64
*)cgareq
);
614 in6_cgareq_64_to_32((const struct in6_cgareq_64
*)user_data
,
615 (struct in6_cgareq_32
*)cgareq
);
617 bcopy(user_data
, cgareq
, sizeof(*cgareq
));
619 #endif /* __LP64__ */
622 static __attribute__((noinline
)) int
623 in6ctl_associd(struct socket
*so
, u_long cmd
, caddr_t data
)
627 struct so_aidreq32 a32
;
628 struct so_aidreq64 a64
;
634 case SIOCGASSOCIDS32
: { /* struct so_aidreq32 */
635 bcopy(data
, &u
.a32
, sizeof(u
.a32
));
636 error
= in6_getassocids(so
, &u
.a32
.sar_cnt
, u
.a32
.sar_aidp
);
638 bcopy(&u
.a32
, data
, sizeof(u
.a32
));
643 case SIOCGASSOCIDS64
: { /* struct so_aidreq64 */
644 bcopy(data
, &u
.a64
, sizeof(u
.a64
));
645 error
= in6_getassocids(so
, &u
.a64
.sar_cnt
, u
.a64
.sar_aidp
);
647 bcopy(&u
.a64
, data
, sizeof(u
.a64
));
660 static __attribute__((noinline
)) int
661 in6ctl_connid(struct socket
*so
, u_long cmd
, caddr_t data
)
665 struct so_cidreq32 c32
;
666 struct so_cidreq64 c64
;
672 case SIOCGCONNIDS32
: { /* struct so_cidreq32 */
673 bcopy(data
, &u
.c32
, sizeof(u
.c32
));
674 error
= in6_getconnids(so
, u
.c32
.scr_aid
, &u
.c32
.scr_cnt
,
677 bcopy(&u
.c32
, data
, sizeof(u
.c32
));
682 case SIOCGCONNIDS64
: { /* struct so_cidreq64 */
683 bcopy(data
, &u
.c64
, sizeof(u
.c64
));
684 error
= in6_getconnids(so
, u
.c64
.scr_aid
, &u
.c64
.scr_cnt
,
687 bcopy(&u
.c64
, data
, sizeof(u
.c64
));
700 static __attribute__((noinline
)) int
701 in6ctl_conninfo(struct socket
*so
, u_long cmd
, caddr_t data
)
705 struct so_cinforeq32 ci32
;
706 struct so_cinforeq64 ci64
;
712 case SIOCGCONNINFO32
: { /* struct so_cinforeq32 */
713 bcopy(data
, &u
.ci32
, sizeof(u
.ci32
));
714 error
= in6_getconninfo(so
, u
.ci32
.scir_cid
, &u
.ci32
.scir_flags
,
715 &u
.ci32
.scir_ifindex
, &u
.ci32
.scir_error
, u
.ci32
.scir_src
,
716 &u
.ci32
.scir_src_len
, u
.ci32
.scir_dst
, &u
.ci32
.scir_dst_len
,
717 &u
.ci32
.scir_aux_type
, u
.ci32
.scir_aux_data
,
718 &u
.ci32
.scir_aux_len
);
720 bcopy(&u
.ci32
, data
, sizeof(u
.ci32
));
725 case SIOCGCONNINFO64
: { /* struct so_cinforeq64 */
726 bcopy(data
, &u
.ci64
, sizeof(u
.ci64
));
727 error
= in6_getconninfo(so
, u
.ci64
.scir_cid
, &u
.ci64
.scir_flags
,
728 &u
.ci64
.scir_ifindex
, &u
.ci64
.scir_error
, u
.ci64
.scir_src
,
729 &u
.ci64
.scir_src_len
, u
.ci64
.scir_dst
, &u
.ci64
.scir_dst_len
,
730 &u
.ci64
.scir_aux_type
, u
.ci64
.scir_aux_data
,
731 &u
.ci64
.scir_aux_len
);
733 bcopy(&u
.ci64
, data
, sizeof(u
.ci64
));
746 static __attribute__((noinline
)) int
747 in6ctl_llstart(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
749 struct in6_aliasreq sifra
, *ifra
= NULL
;
756 case SIOCLL_START_32
: /* struct in6_aliasreq_32 */
757 case SIOCLL_START_64
: /* struct in6_aliasreq_64 */
758 is64
= (cmd
== SIOCLL_START_64
);
760 * Convert user ifra to the kernel form, when appropriate.
761 * This allows the conversion between different data models
762 * to be centralized, so that it can be passed around to other
763 * routines that are expecting the kernel form.
765 ifra
= in6_aliasreq_to_native(data
, is64
, &sifra
);
768 * NOTE: All the interface specific DLIL attachements should
769 * be done here. They are currently done in in6_ifattach_aux()
770 * for the interfaces that need it.
772 if (ifra
->ifra_addr
.sin6_family
== AF_INET6
&&
773 /* Only check ifra_dstaddr if valid */
774 (ifra
->ifra_dstaddr
.sin6_len
== 0 ||
775 ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
)) {
776 /* some interfaces may provide LinkLocal addresses */
777 error
= in6_ifattach_aliasreq(ifp
, NULL
, ifra
);
779 error
= in6_ifattach_aliasreq(ifp
, NULL
, NULL
);
782 in6_if_up_dad_start(ifp
);
794 static __attribute__((noinline
)) int
795 in6ctl_llstop(struct ifnet
*ifp
)
797 struct in6_ifaddr
*ia
;
798 struct nd_prefix pr0
, *pr
;
802 /* Remove link local addresses from interface */
803 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
804 boolean_t from_begining
= TRUE
;
805 while (from_begining
) {
806 from_begining
= FALSE
;
807 TAILQ_FOREACH(ia
, &in6_ifaddrhead
, ia6_link
) {
808 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
811 IFA_LOCK(&ia
->ia_ifa
);
812 if (IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
)) {
813 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
814 IFA_UNLOCK(&ia
->ia_ifa
);
815 lck_rw_done(&in6_ifaddr_rwlock
);
816 in6_purgeaddr(&ia
->ia_ifa
);
817 IFA_REMREF(&ia
->ia_ifa
); /* for us */
818 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
820 * Purging the address caused in6_ifaddr_rwlock
821 * to be dropped and reacquired;
822 * therefore search again from the beginning
823 * of in6_ifaddrs list.
825 from_begining
= TRUE
;
828 IFA_UNLOCK(&ia
->ia_ifa
);
831 lck_rw_done(&in6_ifaddr_rwlock
);
833 /* Delete the link local prefix */
834 bzero(&pr0
, sizeof(pr0
));
837 pr0
.ndpr_prefix
.sin6_addr
.s6_addr16
[0] = IPV6_ADDR_INT16_ULL
;
838 (void)in6_setscope(&pr0
.ndpr_prefix
.sin6_addr
, ifp
, NULL
);
839 pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_UNSPEC
);
841 lck_mtx_lock(nd6_mutex
);
845 NDPR_REMREF(pr
); /* Drop the reference from lookup */
846 lck_mtx_unlock(nd6_mutex
);
853 * This routine configures secure link local address
855 static __attribute__((noinline
)) int
856 in6ctl_cgastart(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
858 struct in6_cgareq llcgasr
;
864 case SIOCLL_CGASTART_32
: /* struct in6_cgareq_32 */
865 case SIOCLL_CGASTART_64
: /* struct in6_cgareq_64 */
866 is64
= (cmd
== SIOCLL_CGASTART_64
);
868 * Convert user cgareq to the kernel form, when appropriate.
869 * This allows the conversion between different data models
870 * to be centralized, so that it can be passed around to other
871 * routines that are expecting the kernel form.
873 in6_cgareq_copy_from_user(data
, is64
, &llcgasr
);
876 * NOTE: All the interface specific DLIL attachements
877 * should be done here. They are currently done in
878 * in6_ifattach_cgareq() for the interfaces that
881 error
= in6_ifattach_llcgareq(ifp
, &llcgasr
);
883 in6_if_up_dad_start(ifp
);
896 * Caller passes in the ioctl data pointer directly via "ifr", with the
897 * expectation that this routine always uses bcopy() or other byte-aligned
900 static __attribute__((noinline
)) int
901 in6ctl_gifaddr(struct ifnet
*ifp
, struct in6_ifaddr
*ia
, u_long cmd
,
902 struct in6_ifreq
*ifr
)
904 struct sockaddr_in6 addr
;
910 return EADDRNOTAVAIL
;
914 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
915 IFA_LOCK(&ia
->ia_ifa
);
916 bcopy(&ia
->ia_addr
, &addr
, sizeof(addr
));
917 IFA_UNLOCK(&ia
->ia_ifa
);
918 if ((error
= sa6_recoverscope(&addr
, TRUE
)) != 0) {
921 bcopy(&addr
, &ifr
->ifr_addr
, sizeof(addr
));
924 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
925 if (!(ifp
->if_flags
& IFF_POINTOPOINT
)) {
930 * XXX: should we check if ifa_dstaddr is NULL and return
933 IFA_LOCK(&ia
->ia_ifa
);
934 bcopy(&ia
->ia_dstaddr
, &addr
, sizeof(addr
));
935 IFA_UNLOCK(&ia
->ia_ifa
);
936 if ((error
= sa6_recoverscope(&addr
, TRUE
)) != 0) {
939 bcopy(&addr
, &ifr
->ifr_dstaddr
, sizeof(addr
));
951 * Caller passes in the ioctl data pointer directly via "ifr", with the
952 * expectation that this routine always uses bcopy() or other byte-aligned
955 static __attribute__((noinline
)) int
956 in6ctl_gifstat(struct ifnet
*ifp
, u_long cmd
, struct in6_ifreq
*ifr
)
958 int error
= 0, index
;
961 index
= ifp
->if_index
;
964 case SIOCGIFSTAT_IN6
: /* struct in6_ifreq */
965 /* N.B.: if_inet6data is never freed once set. */
966 if (IN6_IFEXTRA(ifp
) == NULL
) {
967 /* return (EAFNOSUPPORT)? */
968 bzero(&ifr
->ifr_ifru
.ifru_stat
,
969 sizeof(ifr
->ifr_ifru
.ifru_stat
));
971 bcopy(&IN6_IFEXTRA(ifp
)->in6_ifstat
,
972 &ifr
->ifr_ifru
.ifru_stat
,
973 sizeof(ifr
->ifr_ifru
.ifru_stat
));
977 case SIOCGIFSTAT_ICMP6
: /* struct in6_ifreq */
978 /* N.B.: if_inet6data is never freed once set. */
979 if (IN6_IFEXTRA(ifp
) == NULL
) {
980 /* return (EAFNOSUPPORT)? */
981 bzero(&ifr
->ifr_ifru
.ifru_icmp6stat
,
982 sizeof(ifr
->ifr_ifru
.ifru_icmp6stat
));
984 bcopy(&IN6_IFEXTRA(ifp
)->icmp6_ifstat
,
985 &ifr
->ifr_ifru
.ifru_icmp6stat
,
986 sizeof(ifr
->ifr_ifru
.ifru_icmp6stat
));
999 * Caller passes in the ioctl data pointer directly via "ifr", with the
1000 * expectation that this routine always uses bcopy() or other byte-aligned
1003 static __attribute__((noinline
)) int
1004 in6ctl_alifetime(struct in6_ifaddr
*ia
, u_long cmd
, struct in6_ifreq
*ifr
,
1007 uint64_t timenow
= net_uptime();
1008 struct in6_addrlifetime ia6_lt
;
1009 struct timeval caltime
;
1013 return EADDRNOTAVAIL
;
1017 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
1018 IFA_LOCK(&ia
->ia_ifa
);
1019 /* retrieve time as calendar time (last arg is 1) */
1020 in6ifa_getlifetime(ia
, &ia6_lt
, 1);
1022 struct in6_addrlifetime_64 lt
;
1024 bzero(<
, sizeof(lt
));
1025 lt
.ia6t_expire
= ia6_lt
.ia6t_expire
;
1026 lt
.ia6t_preferred
= ia6_lt
.ia6t_preferred
;
1027 lt
.ia6t_vltime
= ia6_lt
.ia6t_vltime
;
1028 lt
.ia6t_pltime
= ia6_lt
.ia6t_pltime
;
1029 bcopy(<
, &ifr
->ifr_ifru
.ifru_lifetime
, sizeof(ifr
->ifr_ifru
.ifru_lifetime
));
1031 struct in6_addrlifetime_32 lt
;
1033 bzero(<
, sizeof(lt
));
1034 lt
.ia6t_expire
= (uint32_t)ia6_lt
.ia6t_expire
;
1035 lt
.ia6t_preferred
= (uint32_t)ia6_lt
.ia6t_preferred
;
1036 lt
.ia6t_vltime
= (uint32_t)ia6_lt
.ia6t_vltime
;
1037 lt
.ia6t_pltime
= (uint32_t)ia6_lt
.ia6t_pltime
;
1038 bcopy(<
, &ifr
->ifr_ifru
.ifru_lifetime
, sizeof(ifr
->ifr_ifru
.ifru_lifetime
));
1040 IFA_UNLOCK(&ia
->ia_ifa
);
1043 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
1044 getmicrotime(&caltime
);
1046 /* sanity for overflow - beware unsigned */
1048 struct in6_addrlifetime_64 lt
;
1050 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof(lt
));
1051 if (lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
&&
1052 lt
.ia6t_vltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1056 if (lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
&&
1057 lt
.ia6t_pltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1062 struct in6_addrlifetime_32 lt
;
1064 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof(lt
));
1065 if (lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
&&
1066 lt
.ia6t_vltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1070 if (lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
&&
1071 lt
.ia6t_pltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1077 IFA_LOCK(&ia
->ia_ifa
);
1079 struct in6_addrlifetime_64 lt
;
1081 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof(lt
));
1082 ia6_lt
.ia6t_expire
= lt
.ia6t_expire
;
1083 ia6_lt
.ia6t_preferred
= lt
.ia6t_preferred
;
1084 ia6_lt
.ia6t_vltime
= lt
.ia6t_vltime
;
1085 ia6_lt
.ia6t_pltime
= lt
.ia6t_pltime
;
1087 struct in6_addrlifetime_32 lt
;
1089 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof(lt
));
1090 ia6_lt
.ia6t_expire
= (uint32_t)lt
.ia6t_expire
;
1091 ia6_lt
.ia6t_preferred
= (uint32_t)lt
.ia6t_preferred
;
1092 ia6_lt
.ia6t_vltime
= lt
.ia6t_vltime
;
1093 ia6_lt
.ia6t_pltime
= lt
.ia6t_pltime
;
1096 if (ia6_lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
1097 ia6_lt
.ia6t_expire
= timenow
+ ia6_lt
.ia6t_vltime
;
1099 ia6_lt
.ia6t_expire
= 0;
1102 if (ia6_lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
1103 ia6_lt
.ia6t_preferred
= timenow
+ ia6_lt
.ia6t_pltime
;
1105 ia6_lt
.ia6t_preferred
= 0;
1108 in6ifa_setlifetime(ia
, &ia6_lt
);
1109 IFA_UNLOCK(&ia
->ia_ifa
);
1121 in6ctl_clat46start(struct ifnet
*ifp
)
1123 struct nd_prefix
*pr
= NULL
;
1124 struct nd_prefix
*next
= NULL
;
1125 struct in6_ifaddr
*ia6
= NULL
;
1128 if (ifp
== lo_ifp
) {
1132 * Traverse the list of prefixes and find the first non-linklocal
1133 * prefix on the interface.
1134 * For that found eligible prefix, configure a CLAT46 reserved address.
1136 lck_mtx_lock(nd6_mutex
);
1137 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
1138 next
= pr
->ndpr_next
;
1141 if (pr
->ndpr_ifp
!= ifp
) {
1146 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
1151 if (pr
->ndpr_raf_auto
== 0) {
1156 if (pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
1161 if ((pr
->ndpr_stateflags
& NDPRF_CLAT46
) == 0
1162 && pr
->ndpr_vltime
!= 0) {
1163 NDPR_ADDREF(pr
); /* Take reference for rest of the processing */
1171 lck_mtx_unlock(nd6_mutex
);
1174 if ((ia6
= in6_pfx_newpersistaddr(pr
, FALSE
, &error
,
1175 TRUE
, CLAT46_COLLISION_COUNT_OFFSET
)) == NULL
) {
1177 "Could not configure CLAT46 address on"
1178 " interface %s.\n", ifp
->if_xname
);
1180 IFA_LOCK(&ia6
->ia_ifa
);
1183 NDPR_ADDREF(pr
); /* for addr reference */
1184 pr
->ndpr_stateflags
|= NDPRF_CLAT46
;
1186 VERIFY(pr
->ndpr_addrcnt
!= 0);
1188 IFA_UNLOCK(&ia6
->ia_ifa
);
1189 IFA_REMREF(&ia6
->ia_ifa
);
1192 * A newly added address might affect the status
1193 * of other addresses, so we check and update it.
1194 * XXX: what if address duplication happens?
1196 lck_mtx_lock(nd6_mutex
);
1197 pfxlist_onlink_check();
1198 lck_mtx_unlock(nd6_mutex
);
1205 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(void *)(ifa))
1208 * Generic INET6 control operations (ioctl's).
1210 * ifp is NULL if not an interface-specific ioctl.
1212 * Most of the routines called to handle the ioctls would end up being
1213 * tail-call optimized, which unfortunately causes this routine to
1214 * consume too much stack space; this is the reason for the "noinline"
1215 * attribute used on those routines.
1217 * If called directly from within the networking stack (as opposed to via
1218 * pru_control), the socket parameter may be NULL.
1221 in6_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
1224 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
1225 struct in6_aliasreq sifra
, *ifra
= NULL
;
1226 struct in6_ifaddr
*ia
= NULL
;
1227 struct sockaddr_in6 sin6
, *sa6
= NULL
;
1228 boolean_t privileged
= (proc_suser(p
) == 0);
1229 boolean_t p64
= proc_is64bit(p
);
1230 boolean_t so_unlocked
= FALSE
;
1231 int intval
, error
= 0;
1233 /* In case it's NULL, make sure it came from the kernel */
1234 VERIFY(so
!= NULL
|| p
== kernproc
);
1237 * ioctls which don't require ifp, may require socket.
1240 case SIOCAADDRCTL_POLICY
: /* struct in6_addrpolicy */
1241 case SIOCDADDRCTL_POLICY
: /* struct in6_addrpolicy */
1245 return in6_src_ioctl(cmd
, data
);
1248 case SIOCDRADD_IN6_32
: /* struct in6_defrouter_32 */
1249 case SIOCDRADD_IN6_64
: /* struct in6_defrouter_64 */
1250 case SIOCDRDEL_IN6_32
: /* struct in6_defrouter_32 */
1251 case SIOCDRDEL_IN6_64
: /* struct in6_defrouter_64 */
1255 return defrtrlist_ioctl(cmd
, data
);
1258 case SIOCGASSOCIDS32
: /* struct so_aidreq32 */
1259 case SIOCGASSOCIDS64
: /* struct so_aidreq64 */
1260 return in6ctl_associd(so
, cmd
, data
);
1263 case SIOCGCONNIDS32
: /* struct so_cidreq32 */
1264 case SIOCGCONNIDS64
: /* struct so_cidreq64 */
1265 return in6ctl_connid(so
, cmd
, data
);
1268 case SIOCGCONNINFO32
: /* struct so_cinforeq32 */
1269 case SIOCGCONNINFO64
: /* struct so_cinforeq64 */
1270 return in6ctl_conninfo(so
, cmd
, data
);
1275 * The rest of ioctls require ifp; reject if we don't have one;
1276 * return ENXIO to be consistent with ifioctl().
1283 * Unlock the socket since ifnet_ioctl() may be invoked by
1284 * one of the ioctl handlers below. Socket will be re-locked
1285 * prior to returning.
1288 socket_unlock(so
, 0);
1293 * ioctls which require ifp but not interface address.
1296 case SIOCAUTOCONF_START
: /* struct in6_ifreq */
1301 error
= in6_autoconf(ifp
, TRUE
);
1304 case SIOCAUTOCONF_STOP
: /* struct in6_ifreq */
1309 error
= in6_autoconf(ifp
, FALSE
);
1312 case SIOCLL_START_32
: /* struct in6_aliasreq_32 */
1313 case SIOCLL_START_64
: /* struct in6_aliasreq_64 */
1318 error
= in6ctl_llstart(ifp
, cmd
, data
);
1321 case SIOCLL_STOP
: /* struct in6_ifreq */
1326 error
= in6ctl_llstop(ifp
);
1329 case SIOCCLAT46_START
: /* struct in6_ifreq */
1334 error
= in6ctl_clat46start(ifp
);
1336 if_set_eflags(ifp
, IFEF_CLAT46
);
1340 case SIOCCLAT46_STOP
: /* struct in6_ifreq */
1347 * Not much to be done here and it might not be needed
1348 * It would usually be done when IPv6 configuration is being
1350 * XXX Probably STOP equivalent is not needed here.
1352 if_clear_eflags(ifp
, IFEF_CLAT46
);
1354 case SIOCGETROUTERMODE_IN6
: /* struct in6_ifreq */
1355 intval
= ifp
->if_ipv6_router_mode
;
1356 bcopy(&intval
, &((struct in6_ifreq
*)(void *)data
)->ifr_intval
,
1359 case SIOCSETROUTERMODE_IN6
: /* struct in6_ifreq */
1364 bcopy(&((struct in6_ifreq
*)(void *)data
)->ifr_intval
,
1365 &intval
, sizeof(intval
));
1367 case IPV6_ROUTER_MODE_DISABLED
:
1368 case IPV6_ROUTER_MODE_EXCLUSIVE
:
1369 case IPV6_ROUTER_MODE_HYBRID
:
1375 error
= in6_setrouter(ifp
, (ipv6_router_mode_t
)intval
);
1378 case SIOCPROTOATTACH_IN6_32
: /* struct in6_aliasreq_32 */
1379 case SIOCPROTOATTACH_IN6_64
: /* struct in6_aliasreq_64 */
1384 error
= in6_domifattach(ifp
);
1387 case SIOCPROTODETACH_IN6
: /* struct in6_ifreq */
1392 /* Cleanup interface routes and addresses */
1395 if ((error
= proto_unplumb(PF_INET6
, ifp
))) {
1396 log(LOG_ERR
, "SIOCPROTODETACH_IN6: %s error=%d\n",
1397 if_name(ifp
), error
);
1401 case SIOCSNDFLUSH_IN6
: /* struct in6_ifreq */
1402 case SIOCSPFXFLUSH_IN6
: /* struct in6_ifreq */
1403 case SIOCSRTRFLUSH_IN6
: /* struct in6_ifreq */
1404 case SIOCSDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
1405 case SIOCSDEFIFACE_IN6_64
: /* struct in6_ndifreq_64 */
1406 case SIOCSIFINFO_FLAGS
: /* struct in6_ndireq */
1407 case SIOCGIFCGAPREP_IN6_32
: /* struct in6_cgareq_32 */
1408 case SIOCGIFCGAPREP_IN6_64
: /* struct in6_cgareq_64 */
1409 case SIOCSIFCGAPREP_IN6_32
: /* struct in6_cgareq_32 */
1410 case SIOCSIFCGAPREP_IN6_64
: /* struct in6_cgareq_32 */
1416 case OSIOCGIFINFO_IN6
: /* struct in6_ondireq */
1417 case SIOCGIFINFO_IN6
: /* struct in6_ondireq */
1418 case SIOCGDRLST_IN6_32
: /* struct in6_drlist_32 */
1419 case SIOCGDRLST_IN6_64
: /* struct in6_drlist_64 */
1420 case SIOCGPRLST_IN6_32
: /* struct in6_prlist_32 */
1421 case SIOCGPRLST_IN6_64
: /* struct in6_prlist_64 */
1422 case SIOCGNBRINFO_IN6_32
: /* struct in6_nbrinfo_32 */
1423 case SIOCGNBRINFO_IN6_64
: /* struct in6_nbrinfo_64 */
1424 case SIOCGDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
1425 case SIOCGDEFIFACE_IN6_64
: /* struct in6_ndifreq_64 */
1426 error
= nd6_ioctl(cmd
, data
, ifp
);
1429 case SIOCSIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1430 case SIOCDIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1431 case SIOCAIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1432 case SIOCCIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1433 case SIOCSGIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1434 case SIOCGIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1436 "prefix ioctls are now invalidated. "
1437 "please use ifconfig.\n");
1441 case SIOCSSCOPE6
: /* struct in6_ifreq (deprecated) */
1442 case SIOCGSCOPE6
: /* struct in6_ifreq (deprecated) */
1443 case SIOCGSCOPE6DEF
: /* struct in6_ifreq (deprecated) */
1447 case SIOCLL_CGASTART_32
: /* struct in6_cgareq_32 */
1448 case SIOCLL_CGASTART_64
: /* struct in6_cgareq_64 */
1452 error
= in6ctl_cgastart(ifp
, cmd
, data
);
1456 case SIOCGIFSTAT_IN6
: /* struct in6_ifreq */
1457 case SIOCGIFSTAT_ICMP6
: /* struct in6_ifreq */
1458 error
= in6ctl_gifstat(ifp
, cmd
, ifr
);
1463 * ioctls which require interface address; obtain sockaddr_in6.
1466 case SIOCSIFADDR_IN6
: /* struct in6_ifreq (deprecated) */
1467 case SIOCSIFDSTADDR_IN6
: /* struct in6_ifreq (deprecated) */
1468 case SIOCSIFNETMASK_IN6
: /* struct in6_ifreq (deprecated) */
1470 * Since IPv6 allows a node to assign multiple addresses
1471 * on a single interface, SIOCSIFxxx ioctls are deprecated.
1473 /* we decided to obsolete this command (20000704) */
1477 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1478 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1484 * Convert user ifra to the kernel form, when appropriate.
1485 * This allows the conversion between different data models
1486 * to be centralized, so that it can be passed around to other
1487 * routines that are expecting the kernel form.
1489 ifra
= in6_aliasreq_to_native(data
,
1490 (cmd
== SIOCAIFADDR_IN6_64
), &sifra
);
1491 bcopy(&ifra
->ifra_addr
, &sin6
, sizeof(sin6
));
1495 case SIOCDIFADDR_IN6
: /* struct in6_ifreq */
1496 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
1502 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
1503 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
1504 case SIOCGIFNETMASK_IN6
: /* struct in6_ifreq */
1505 case SIOCGIFAFLAG_IN6
: /* struct in6_ifreq */
1506 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
1507 bcopy(&ifr
->ifr_addr
, &sin6
, sizeof(sin6
));
1510 case SIOCGIFDSTADDR
:
1511 case SIOCSIFDSTADDR
:
1512 case SIOCGIFBRDADDR
:
1513 case SIOCSIFBRDADDR
:
1514 case SIOCGIFNETMASK
:
1515 case SIOCSIFNETMASK
:
1520 /* Do not handle these AF_INET commands in AF_INET6 path */
1526 * Find address for this interface, if it exists.
1528 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
1529 * only, and used the first interface address as the target of other
1530 * operations (without checking ifra_addr). This was because netinet
1531 * code/API assumed at most 1 interface address per interface.
1532 * Since IPv6 allows a node to assign multiple addresses
1533 * on a single interface, we almost always look and check the
1534 * presence of ifra_addr, and reject invalid ones here.
1535 * It also decreases duplicated code among SIOC*_IN6 operations.
1538 if (sa6
!= NULL
&& sa6
->sin6_family
== AF_INET6
) {
1539 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
1540 if (sa6
->sin6_addr
.s6_addr16
[1] == 0) {
1541 /* link ID is not embedded by the user */
1542 sa6
->sin6_addr
.s6_addr16
[1] =
1543 htons(ifp
->if_index
);
1544 } else if (sa6
->sin6_addr
.s6_addr16
[1] !=
1545 htons(ifp
->if_index
)) {
1546 error
= EINVAL
; /* link ID contradicts */
1549 if (sa6
->sin6_scope_id
) {
1550 if (sa6
->sin6_scope_id
!=
1551 (u_int32_t
)ifp
->if_index
) {
1555 sa6
->sin6_scope_id
= 0; /* XXX: good way? */
1559 * Any failures from this point on must take into account
1560 * a non-NULL "ia" with an outstanding reference count, and
1561 * therefore requires IFA_REMREF. Jump to "done" label
1562 * instead of calling return if "ia" is valid.
1564 ia
= in6ifa_ifpwithaddr(ifp
, &sa6
->sin6_addr
);
1568 * SIOCDIFADDR_IN6/SIOCAIFADDR_IN6 specific tests.
1571 case SIOCDIFADDR_IN6
: /* struct in6_ifreq */
1573 error
= EADDRNOTAVAIL
;
1577 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1578 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1579 VERIFY(sa6
!= NULL
);
1581 * We always require users to specify a valid IPv6 address for
1582 * the corresponding operation. Use "sa6" instead of "ifra"
1583 * since SIOCDIFADDR_IN6 falls thru above.
1585 if (sa6
->sin6_family
!= AF_INET6
||
1586 sa6
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
1587 error
= EAFNOSUPPORT
;
1594 * And finally process address-related ioctls.
1597 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
1598 /* This interface is basically deprecated. use SIOCGIFCONF. */
1600 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
1601 error
= in6ctl_gifaddr(ifp
, ia
, cmd
, ifr
);
1604 case SIOCGIFNETMASK_IN6
: /* struct in6_ifreq */
1606 IFA_LOCK(&ia
->ia_ifa
);
1607 bcopy(&ia
->ia_prefixmask
, &ifr
->ifr_addr
,
1608 sizeof(struct sockaddr_in6
));
1609 IFA_UNLOCK(&ia
->ia_ifa
);
1611 error
= EADDRNOTAVAIL
;
1615 case SIOCGIFAFLAG_IN6
: /* struct in6_ifreq */
1617 IFA_LOCK(&ia
->ia_ifa
);
1618 bcopy(&ia
->ia6_flags
, &ifr
->ifr_ifru
.ifru_flags6
,
1619 sizeof(ifr
->ifr_ifru
.ifru_flags6
));
1620 IFA_UNLOCK(&ia
->ia_ifa
);
1622 error
= EADDRNOTAVAIL
;
1626 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
1627 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
1628 error
= in6ctl_alifetime(ia
, cmd
, ifr
, p64
);
1631 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1632 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1633 error
= in6ctl_aifaddr(ifp
, ifra
);
1636 case SIOCDIFADDR_IN6
:
1637 in6ctl_difaddr(ifp
, ia
);
1641 error
= ifnet_ioctl(ifp
, PF_INET6
, cmd
, data
);
1647 IFA_REMREF(&ia
->ia_ifa
);
1656 static __attribute__((noinline
)) int
1657 in6ctl_aifaddr(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
)
1659 int i
, error
, addtmp
;
1661 struct nd_prefix pr0
, *pr
;
1662 struct in6_ifaddr
*ia
;
1664 VERIFY(ifp
!= NULL
&& ifra
!= NULL
);
1667 /* Attempt to attach the protocol, in case it isn't attached */
1668 error
= in6_domifattach(ifp
);
1670 /* PF_INET6 wasn't previously attached */
1671 error
= in6_ifattach_aliasreq(ifp
, NULL
, NULL
);
1676 in6_if_up_dad_start(ifp
);
1677 } else if (error
!= EEXIST
) {
1682 * First, make or update the interface address structure, and link it
1685 error
= in6_update_ifa(ifp
, ifra
, 0, &ia
);
1691 /* Now, make the prefix on-link on the interface. */
1692 plen
= (uint8_t)in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
, NULL
);
1698 * NOTE: We'd rather create the prefix before the address, but we need
1699 * at least one address to install the corresponding interface route,
1700 * so we configure the address first.
1704 * Convert mask to prefix length (prefixmask has already been validated
1705 * in in6_update_ifa().
1707 bzero(&pr0
, sizeof(pr0
));
1708 pr0
.ndpr_plen
= plen
;
1710 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
1711 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
1713 /* apply the mask for safety. */
1714 for (i
= 0; i
< 4; i
++) {
1715 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1716 ifra
->ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
1720 * Since we don't have an API to set prefix (not address) lifetimes, we
1721 * just use the same lifetimes as addresses. The (temporarily)
1722 * installed lifetimes can be overridden by later advertised RAs (when
1723 * accept_rtadv is non 0), which is an intended behavior.
1725 pr0
.ndpr_raf_onlink
= 1; /* should be configurable? */
1726 pr0
.ndpr_raf_auto
= !!(ifra
->ifra_flags
& IN6_IFF_AUTOCONF
);
1727 if (ifra
->ifra_flags
& (IN6_IFF_AUTOCONF
| IN6_IFF_DYNAMIC
)) {
1728 pr0
.ndpr_vltime
= ifra
->ifra_lifetime
.ia6t_vltime
;
1729 pr0
.ndpr_pltime
= ifra
->ifra_lifetime
.ia6t_pltime
;
1731 pr0
.ndpr_vltime
= ND6_INFINITE_LIFETIME
;
1732 pr0
.ndpr_pltime
= ND6_INFINITE_LIFETIME
;
1734 pr0
.ndpr_stateflags
|= NDPRF_STATIC
;
1735 lck_mtx_init(&pr0
.ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
1737 /* add the prefix if there's none. */
1738 if ((pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_NEVER
)) == NULL
) {
1740 * nd6_prelist_add will install the corresponding interface
1743 error
= nd6_prelist_add(&pr0
, NULL
, &pr
, FALSE
);
1749 log(LOG_ERR
, "%s: nd6_prelist_add okay, but"
1750 " no prefix.\n", __func__
);
1756 IFA_LOCK(&ia
->ia_ifa
);
1758 /* if this is a new autoconfed addr */
1760 if (ia
->ia6_ndpr
== NULL
) {
1763 VERIFY(pr
->ndpr_addrcnt
!= 0);
1765 NDPR_ADDREF(pr
); /* for addr reference */
1768 * If this is the first autoconf address from the prefix,
1769 * create a temporary address as well (when specified).
1771 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
1773 pr
->ndpr_addrcnt
== 1) {
1779 IFA_UNLOCK(&ia
->ia_ifa
);
1783 e
= in6_tmpifadd(ia
, 1);
1785 log(LOG_NOTICE
, "%s: failed to create a"
1786 " temporary address, error=%d\n",
1792 * This might affect the status of autoconfigured addresses, that is,
1793 * this address might make other addresses detached.
1795 lck_mtx_lock(nd6_mutex
);
1796 pfxlist_onlink_check();
1797 lck_mtx_unlock(nd6_mutex
);
1799 /* Drop use count held above during lookup/add */
1804 IFA_REMREF(&ia
->ia_ifa
);
1809 static __attribute__((noinline
)) void
1810 in6ctl_difaddr(struct ifnet
*ifp
, struct in6_ifaddr
*ia
)
1813 struct nd_prefix pr0
, *pr
;
1815 VERIFY(ifp
!= NULL
&& ia
!= NULL
);
1818 * If the address being deleted is the only one that owns
1819 * the corresponding prefix, expire the prefix as well.
1820 * XXX: theoretically, we don't have to worry about such
1821 * relationship, since we separate the address management
1822 * and the prefix management. We do this, however, to provide
1823 * as much backward compatibility as possible in terms of
1824 * the ioctl operation.
1825 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
1827 IFA_LOCK(&ia
->ia_ifa
);
1828 bzero(&pr0
, sizeof(pr0
));
1830 pr0
.ndpr_plen
= (uint8_t)in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
);
1831 if (pr0
.ndpr_plen
== 128) {
1832 IFA_UNLOCK(&ia
->ia_ifa
);
1835 pr0
.ndpr_prefix
= ia
->ia_addr
;
1836 pr0
.ndpr_mask
= ia
->ia_prefixmask
.sin6_addr
;
1837 for (i
= 0; i
< 4; i
++) {
1838 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1839 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
];
1841 IFA_UNLOCK(&ia
->ia_ifa
);
1843 if ((pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_UNSPEC
)) != NULL
) {
1844 IFA_LOCK(&ia
->ia_ifa
);
1846 if (pr
->ndpr_addrcnt
== 1) {
1847 /* XXX: just for expiration */
1848 pr
->ndpr_expire
= 1;
1851 IFA_UNLOCK(&ia
->ia_ifa
);
1853 /* Drop use count held above during lookup */
1858 in6_purgeaddr(&ia
->ia_ifa
);
1861 static __attribute__((noinline
)) int
1862 in6_autoconf(struct ifnet
*ifp
, int enable
)
1866 VERIFY(ifp
!= NULL
);
1868 if (ifp
->if_flags
& IFF_LOOPBACK
) {
1874 * An interface in IPv6 router mode implies that it
1875 * is either configured with a static IP address or
1876 * autoconfigured via a locally-generated RA. Prevent
1877 * SIOCAUTOCONF_START from being set in that mode.
1879 ifnet_lock_exclusive(ifp
);
1880 if (ifp
->if_ipv6_router_mode
== IPV6_ROUTER_MODE_EXCLUSIVE
) {
1881 if_clear_eflags(ifp
, IFEF_ACCEPT_RTADV
);
1884 if_set_eflags(ifp
, IFEF_ACCEPT_RTADV
);
1886 ifnet_lock_done(ifp
);
1888 struct in6_ifaddr
*ia
= NULL
;
1890 if_clear_eflags(ifp
, IFEF_ACCEPT_RTADV
);
1892 /* Remove autoconfigured address from interface */
1893 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1894 boolean_t from_begining
= TRUE
;
1895 while (from_begining
) {
1896 from_begining
= FALSE
;
1897 TAILQ_FOREACH(ia
, &in6_ifaddrhead
, ia6_link
) {
1898 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
1901 IFA_LOCK(&ia
->ia_ifa
);
1902 if (ia
->ia6_flags
& IN6_IFF_AUTOCONF
) {
1903 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
1904 IFA_UNLOCK(&ia
->ia_ifa
);
1905 lck_rw_done(&in6_ifaddr_rwlock
);
1906 in6_purgeaddr(&ia
->ia_ifa
);
1907 IFA_REMREF(&ia
->ia_ifa
); /* for us */
1908 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1910 * Purging the address caused in6_ifaddr_rwlock
1911 * to be dropped and reacquired;
1912 * therefore search again from the beginning
1913 * of in6_ifaddrs list.
1915 from_begining
= TRUE
;
1918 IFA_UNLOCK(&ia
->ia_ifa
);
1921 lck_rw_done(&in6_ifaddr_rwlock
);
1927 * Handle SIOCSETROUTERMODE_IN6 to set the IPv6 router mode on the interface
1928 * Entering or exiting IPV6_ROUTER_MODE_EXCLUSIVE will result in the removal of
1929 * autoconfigured IPv6 addresses on the interface.
1931 static __attribute__((noinline
)) int
1932 in6_setrouter(struct ifnet
*ifp
, ipv6_router_mode_t mode
)
1935 ipv6_router_mode_t prev_mode
;
1937 VERIFY(ifp
!= NULL
);
1939 if (ifp
->if_flags
& IFF_LOOPBACK
) {
1943 prev_mode
= ifp
->if_ipv6_router_mode
;
1944 if (prev_mode
== mode
) {
1945 /* no mode change, there's nothing to do */
1948 if (mode
== IPV6_ROUTER_MODE_EXCLUSIVE
) {
1949 struct nd_ifinfo
*ndi
= NULL
;
1951 ndi
= ND_IFINFO(ifp
);
1952 if (ndi
!= NULL
&& ndi
->initialized
) {
1953 lck_mtx_lock(&ndi
->lock
);
1954 if (ndi
->flags
& ND6_IFF_PROXY_PREFIXES
) {
1955 /* No proxy if we are an advertising router */
1956 ndi
->flags
&= ~ND6_IFF_PROXY_PREFIXES
;
1957 lck_mtx_unlock(&ndi
->lock
);
1958 (void) nd6_if_prproxy(ifp
, FALSE
);
1960 lck_mtx_unlock(&ndi
->lock
);
1965 ifp
->if_ipv6_router_mode
= mode
;
1966 lck_mtx_lock(nd6_mutex
);
1967 defrouter_select(ifp
, NULL
);
1968 lck_mtx_unlock(nd6_mutex
);
1969 if_allmulti(ifp
, (mode
== IPV6_ROUTER_MODE_EXCLUSIVE
));
1970 if (mode
== IPV6_ROUTER_MODE_EXCLUSIVE
||
1971 (prev_mode
== IPV6_ROUTER_MODE_EXCLUSIVE
1972 && mode
== IPV6_ROUTER_MODE_DISABLED
)) {
1973 error
= in6_autoconf(ifp
, FALSE
);
1979 in6_to_kamescope(struct sockaddr_in6
*sin6
, struct ifnet
*ifp
)
1981 struct sockaddr_in6 tmp
;
1984 VERIFY(sin6
!= NULL
);
1987 error
= in6_recoverscope(&tmp
, &sin6
->sin6_addr
, ifp
);
1992 id
= in6_addr2scopeid(ifp
, &tmp
.sin6_addr
);
1993 if (tmp
.sin6_scope_id
== 0) {
1994 tmp
.sin6_scope_id
= id
;
1995 } else if (tmp
.sin6_scope_id
!= id
) {
1996 return EINVAL
; /* scope ID mismatch. */
1998 error
= in6_embedscope(&tmp
.sin6_addr
, &tmp
, NULL
, NULL
, NULL
);
2003 tmp
.sin6_scope_id
= 0;
2009 * When the address is being configured we should clear out certain flags
2010 * coming in from the caller.
2012 #define IN6_IFF_CLR_ADDR_FLAG_MASK (~(IN6_IFF_DEPRECATED | IN6_IFF_DETACHED | IN6_IFF_DUPLICATED))
2015 in6_ifaupdate_aux(struct in6_ifaddr
*ia
, struct ifnet
*ifp
, int ifaupflags
)
2017 struct sockaddr_in6 mltaddr
, mltmask
;
2018 struct in6_addr llsol
;
2020 struct in6_multi
*in6m_sol
;
2021 struct in6_multi_mship
*imm
;
2023 int delay
, error
= 0;
2025 VERIFY(ifp
!= NULL
&& ia
!= NULL
);
2029 nd6log2(debug
, "%s - %s ifp %s ia6_flags 0x%x ifaupflags 0x%x\n",
2031 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
2032 if_name(ia
->ia_ifp
),
2037 * Just to be safe, always clear certain flags when address
2038 * is being configured
2040 ia
->ia6_flags
&= IN6_IFF_CLR_ADDR_FLAG_MASK
;
2043 * Mark the address as tentative before joining multicast addresses,
2044 * so that corresponding MLD responses would not have a tentative
2047 if (in6if_do_dad(ifp
)) {
2048 in6_ifaddr_set_dadprogress(ia
);
2050 * Do not delay sending neighbor solicitations when using optimistic
2051 * duplicate address detection, c.f. RFC 4429.
2053 if (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
) {
2054 ifaupflags
&= ~IN6_IFAUPDATE_DADDELAY
;
2056 ifaupflags
|= IN6_IFAUPDATE_DADDELAY
;
2060 * If the interface has been marked to not perform
2061 * DAD, make sure to reset DAD in progress flags
2062 * that may come in from the caller.
2064 ia
->ia6_flags
&= ~IN6_IFF_DADPROGRESS
;
2067 /* Join necessary multicast groups */
2068 if ((ifp
->if_flags
& IFF_MULTICAST
) != 0) {
2069 /* join solicited multicast addr for new host id */
2070 bzero(&llsol
, sizeof(struct in6_addr
));
2071 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
2072 llsol
.s6_addr32
[1] = 0;
2073 llsol
.s6_addr32
[2] = htonl(1);
2074 llsol
.s6_addr32
[3] = ia
->ia_addr
.sin6_addr
.s6_addr32
[3];
2075 llsol
.s6_addr8
[12] = 0xff;
2076 if ((error
= in6_setscope(&llsol
, ifp
, NULL
)) != 0) {
2077 /* XXX: should not happen */
2078 log(LOG_ERR
, "%s: in6_setscope failed\n", __func__
);
2082 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
)) {
2084 * We need a random delay for DAD on the address
2085 * being configured. It also means delaying
2086 * transmission of the corresponding MLD report to
2087 * avoid report collision. [RFC 4862]
2089 delay
= random() % MAX_RTR_SOLICITATION_DELAY
;
2091 imm
= in6_joingroup(ifp
, &llsol
, &error
, delay
);
2094 "%s: addmulti failed for %s on %s (errno=%d)\n",
2095 __func__
, ip6_sprintf(&llsol
), if_name(ifp
),
2100 in6m_sol
= imm
->i6mm_maddr
;
2101 /* take a refcount for this routine */
2102 IN6M_ADDREF(in6m_sol
);
2105 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
2108 bzero(&mltmask
, sizeof(mltmask
));
2109 mltmask
.sin6_len
= sizeof(struct sockaddr_in6
);
2110 mltmask
.sin6_family
= AF_INET6
;
2111 mltmask
.sin6_addr
= in6mask32
;
2112 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
2115 * join link-local all-nodes address
2117 bzero(&mltaddr
, sizeof(mltaddr
));
2118 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
2119 mltaddr
.sin6_family
= AF_INET6
;
2120 mltaddr
.sin6_addr
= in6addr_linklocal_allnodes
;
2121 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
)) != 0) {
2122 goto unwind
; /* XXX: should not fail */
2125 * XXX: do we really need this automatic routes?
2126 * We should probably reconsider this stuff. Most applications
2127 * actually do not need the routes, since they usually specify
2128 * the outgoing interface.
2130 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
2131 ia
->ia_ifp
->if_index
);
2133 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
2134 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN
)) {
2140 error
= rtrequest_scoped(RTM_ADD
,
2141 (struct sockaddr
*)&mltaddr
,
2142 (struct sockaddr
*)&ia
->ia_addr
,
2143 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
2144 NULL
, ia
->ia_ifp
->if_index
);
2152 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
2155 "%s: addmulti failed for %s on %s (errno=%d)\n",
2156 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
2157 if_name(ifp
), error
);
2162 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
2166 * join node information group address
2168 #define hostnamelen strlen(hostname)
2170 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
)) {
2172 * The spec doesn't say anything about delay for this
2173 * group, but the same logic should apply.
2175 delay
= random() % MAX_RTR_SOLICITATION_DELAY
;
2177 lck_mtx_lock(&hostname_lock
);
2178 int n
= in6_nigroup(ifp
, hostname
, hostnamelen
, &mltaddr
.sin6_addr
);
2179 lck_mtx_unlock(&hostname_lock
);
2181 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
,
2182 delay
); /* XXX jinmei */
2185 "%s: addmulti failed for %s on %s "
2187 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
2188 if_name(ifp
), error
);
2189 /* XXX not very fatal, go on... */
2193 LIST_INSERT_HEAD(&ia
->ia6_memberships
,
2201 * join interface-local all-nodes address.
2202 * (ff01::1%ifN, and ff01::%ifN/32)
2204 mltaddr
.sin6_addr
= in6addr_nodelocal_allnodes
;
2205 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
)) != 0) {
2206 goto unwind
; /* XXX: should not fail */
2208 /* XXX: again, do we really need the route? */
2209 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
2210 ia
->ia_ifp
->if_index
);
2212 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
2213 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN
)) {
2219 error
= rtrequest_scoped(RTM_ADD
,
2220 (struct sockaddr
*)&mltaddr
,
2221 (struct sockaddr
*)&ia
->ia_addr
,
2222 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
2223 NULL
, ia
->ia_ifp
->if_index
);
2231 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
2234 "%s: addmulti failed for %s on %s (errno=%d)\n",
2235 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
2236 if_name(ifp
), error
);
2241 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
2246 * create a ff00::/8 route
2248 bzero(&mltmask
, sizeof(mltmask
));
2249 mltmask
.sin6_len
= sizeof(struct sockaddr_in6
);
2250 mltmask
.sin6_family
= AF_INET6
;
2251 mltmask
.sin6_addr
= in6mask8
;
2252 #define MLTMASK_LEN_8_BITS 1 /* ff00::/8 mltmask's masklen (=8bit=1octet) */
2254 bzero(&mltaddr
, sizeof(mltaddr
));
2255 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
2256 mltaddr
.sin6_family
= AF_INET6
;
2257 mltaddr
.sin6_addr
= in6addr_multicast_prefix
;
2259 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
2260 ia
->ia_ifp
->if_index
);
2262 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
2263 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN_8_BITS
)) {
2269 error
= rtrequest_scoped(RTM_ADD
,
2270 (struct sockaddr
*)&mltaddr
,
2271 (struct sockaddr
*)&ia
->ia_addr
,
2272 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
2273 NULL
, ia
->ia_ifp
->if_index
);
2281 #undef MLTMASK_LEN_8_BITS
2283 /* Ensure nd6_service() is scheduled as soon as it's convenient */
2284 ++nd6_sched_timeout_want
;
2288 * * Interface is marked to perform DAD, AND
2289 * * Address is not marked to skip DAD, AND
2290 * * Address is in a pre-DAD state (Tentative or Optimistic)
2293 if (in6if_do_dad(ifp
) && (ia
->ia6_flags
& IN6_IFF_NODAD
) == 0 &&
2294 (ia
->ia6_flags
& IN6_IFF_DADPROGRESS
) != 0) {
2295 int mindelay
, maxdelay
;
2296 int *delayptr
, delayval
;
2301 * Avoid the DAD delay if the caller wants us to skip it.
2302 * This is not compliant with RFC 2461, but it's only being
2303 * used for signalling and not for actual DAD.
2305 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
) &&
2306 !(ia
->ia6_flags
& IN6_IFF_SWIFTDAD
)) {
2308 * We need to impose a delay before sending an NS
2309 * for DAD. Check if we also needed a delay for the
2310 * corresponding MLD message. If we did, the delay
2311 * should be larger than the MLD delay (this could be
2312 * relaxed a bit, but this simple logic is at least
2316 if (in6m_sol
!= NULL
) {
2317 IN6M_LOCK(in6m_sol
);
2318 if (in6m_sol
->in6m_state
==
2319 MLD_REPORTING_MEMBER
) {
2320 mindelay
= in6m_sol
->in6m_timer
;
2322 IN6M_UNLOCK(in6m_sol
);
2324 maxdelay
= MAX_RTR_SOLICITATION_DELAY
* hz
;
2325 if (maxdelay
- mindelay
== 0) {
2329 (random() % (maxdelay
- mindelay
)) +
2332 delayptr
= &delayval
;
2335 nd6_dad_start((struct ifaddr
*)ia
, delayptr
);
2344 in6_purgeaddr(&ia
->ia_ifa
);
2347 /* release reference held for this routine */
2348 if (in6m_sol
!= NULL
) {
2349 IN6M_REMREF(in6m_sol
);
2355 * Request an IPv6 interface address. If the address is new, then it will be
2356 * constructed and appended to the interface address chains. The interface
2357 * address structure is optionally returned with a reference for the caller.
2360 in6_update_ifa(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
, int ifaupflags
,
2361 struct in6_ifaddr
**iar
)
2363 struct in6_addrlifetime ia6_lt
;
2364 struct in6_ifaddr
*ia
;
2366 struct ifaddr
*xifa
;
2367 struct in6_addrlifetime
*lt
;
2371 /* Sanity check parameters and initialize locals */
2372 VERIFY(ifp
!= NULL
&& ifra
!= NULL
&& iar
!= NULL
);
2378 * We always require users to specify a valid IPv6 address for
2379 * the corresponding operation.
2381 if (ifra
->ifra_addr
.sin6_family
!= AF_INET6
||
2382 ifra
->ifra_addr
.sin6_len
!= sizeof(struct sockaddr_in6
)) {
2383 error
= EAFNOSUPPORT
;
2387 /* Validate ifra_prefixmask.sin6_len is properly bounded. */
2388 if (ifra
->ifra_prefixmask
.sin6_len
== 0 ||
2389 ifra
->ifra_prefixmask
.sin6_len
> sizeof(struct sockaddr_in6
)) {
2394 /* Validate prefix length extracted from ifra_prefixmask structure. */
2395 plen
= (uint8_t)in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
2396 (u_char
*)&ifra
->ifra_prefixmask
+ ifra
->ifra_prefixmask
.sin6_len
);
2402 /* Validate lifetimes */
2403 lt
= &ifra
->ifra_lifetime
;
2404 if (lt
->ia6t_pltime
> lt
->ia6t_vltime
) {
2406 "%s: pltime 0x%x > vltime 0x%x for %s\n", __func__
,
2407 lt
->ia6t_pltime
, lt
->ia6t_vltime
,
2408 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
2412 if (lt
->ia6t_vltime
== 0) {
2414 * the following log might be noisy, but this is a typical
2415 * configuration mistake or a tool's bug.
2417 log(LOG_INFO
, "%s: valid lifetime is 0 for %s\n", __func__
,
2418 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
2422 * Before we lock the ifnet structure, we first check to see if the
2423 * address already exists. If so, then we don't allocate and link a
2426 struct sockaddr_in6 lookup_address
= ifra
->ifra_addr
;
2427 if (IN6_IS_ADDR_LINKLOCAL(&lookup_address
.sin6_addr
)) {
2428 if (lookup_address
.sin6_addr
.s6_addr16
[1] == 0) {
2429 /* link ID is not embedded by the user */
2430 lookup_address
.sin6_addr
.s6_addr16
[1] =
2431 htons(ifp
->if_index
);
2432 } else if (lookup_address
.sin6_addr
.s6_addr16
[1] !=
2433 htons(ifp
->if_index
)) {
2434 error
= EINVAL
; /* link ID contradicts */
2437 if (lookup_address
.sin6_scope_id
!= 0 &&
2438 lookup_address
.sin6_scope_id
!=
2439 (u_int32_t
)ifp
->if_index
) {
2445 ia
= in6ifa_ifpwithaddr(ifp
, &lookup_address
.sin6_addr
);
2451 * Validate destination address on interface types that require it.
2453 if ((ifp
->if_flags
& (IFF_LOOPBACK
| IFF_POINTOPOINT
)) != 0) {
2454 switch (ifra
->ifra_dstaddr
.sin6_family
) {
2457 /* noisy message for diagnostic purposes */
2459 "%s: prefix length < 128 with"
2460 " explicit dstaddr.\n", __func__
);
2470 error
= EAFNOSUPPORT
;
2473 } else if (ifra
->ifra_dstaddr
.sin6_family
!= AF_UNSPEC
) {
2475 "%s: dstaddr valid only on p2p and loopback interfaces.\n",
2481 timenow
= net_uptime();
2486 /* Is this the first new IPv6 address for the interface? */
2487 ifaupflags
|= IN6_IFAUPDATE_NEWADDR
;
2489 /* Allocate memory for IPv6 interface address structure. */
2490 how
= (ifaupflags
& IN6_IFAUPDATE_NOWAIT
) ? Z_NOWAIT
: Z_WAITOK
;
2491 ia
= in6_ifaddr_alloc(how
);
2500 * Initialize interface address structure.
2502 * Note well: none of these sockaddr_in6 structures contain a
2503 * valid sin6_port, sin6_flowinfo or even a sin6_scope_id field.
2504 * We still embed link-local scope identifiers at the end of an
2505 * arbitrary fe80::/32 prefix, for historical reasons. Also, the
2506 * ifa_dstaddr field is always non-NULL on point-to-point and
2507 * loopback interfaces, and conventionally points to a socket
2508 * address of AF_UNSPEC family when there is no destination.
2510 * Please enjoy the dancing sea turtle.
2512 IFA_ADDREF(ifa
); /* for this and optionally for caller */
2514 ifa
->ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
2515 if (ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
||
2516 (ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) != 0) {
2517 ifa
->ifa_dstaddr
= (struct sockaddr
*)&ia
->ia_dstaddr
;
2519 ifa
->ifa_netmask
= (struct sockaddr
*)&ia
->ia_prefixmask
;
2521 ifa
->ifa_metric
= ifp
->if_metric
;
2522 ifa
->ifa_rtrequest
= nd6_rtrequest
;
2524 LIST_INIT(&ia
->ia6_memberships
);
2525 ia
->ia_addr
.sin6_family
= AF_INET6
;
2526 ia
->ia_addr
.sin6_len
= sizeof(ia
->ia_addr
);
2527 ia
->ia_addr
.sin6_addr
= ifra
->ifra_addr
.sin6_addr
;
2528 ia
->ia_prefixmask
.sin6_family
= AF_INET6
;
2529 ia
->ia_prefixmask
.sin6_len
= sizeof(ia
->ia_prefixmask
);
2530 ia
->ia_prefixmask
.sin6_addr
= ifra
->ifra_prefixmask
.sin6_addr
;
2531 error
= in6_to_kamescope(&ia
->ia_addr
, ifp
);
2535 if (ifa
->ifa_dstaddr
!= NULL
) {
2536 ia
->ia_dstaddr
= ifra
->ifra_dstaddr
;
2537 error
= in6_to_kamescope(&ia
->ia_dstaddr
, ifp
);
2543 /* Append to address chains */
2544 ifnet_lock_exclusive(ifp
);
2545 ifaupflags
|= IN6_IFAUPDATE_1STADDR
;
2546 TAILQ_FOREACH(xifa
, &ifp
->if_addrlist
, ifa_list
) {
2547 IFA_LOCK_SPIN(xifa
);
2548 if (xifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2550 ifaupflags
&= ~IN6_IFAUPDATE_1STADDR
;
2557 if_attach_ifa(ifp
, ifa
); /* holds reference for ifnet link */
2559 ifnet_lock_done(ifp
);
2561 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2562 TAILQ_INSERT_TAIL(&in6_ifaddrhead
, ia
, ia6_link
);
2563 IFA_ADDREF(ifa
); /* hold for in6_ifaddrs link */
2564 lck_rw_done(&in6_ifaddr_rwlock
);
2567 ifaupflags
&= ~(IN6_IFAUPDATE_NEWADDR
| IN6_IFAUPDATE_1STADDR
);
2570 VERIFY(ia
!= NULL
&& ifa
== &ia
->ia_ifa
);
2574 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
2575 * to see if the address is deprecated or invalidated, but initialize
2576 * these members for applications.
2578 ia
->ia6_updatetime
= ia
->ia6_createtime
= timenow
;
2580 if (ia6_lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
2581 ia6_lt
.ia6t_expire
= timenow
+ ia6_lt
.ia6t_vltime
;
2583 ia6_lt
.ia6t_expire
= 0;
2585 if (ia6_lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
2586 ia6_lt
.ia6t_preferred
= timenow
+ ia6_lt
.ia6t_pltime
;
2588 ia6_lt
.ia6t_preferred
= 0;
2590 in6ifa_setlifetime(ia
, &ia6_lt
);
2593 * Backward compatibility - if IN6_IFF_DEPRECATED is set from the
2594 * userland, make it deprecated.
2596 if ((ia
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0) {
2597 ia
->ia6_lifetime
.ia6ti_pltime
= 0;
2598 ia
->ia6_lifetime
.ia6ti_preferred
= timenow
;
2602 * Update flag or prefix length
2605 ia
->ia6_flags
= ifra
->ifra_flags
;
2607 /* Release locks (new address available to concurrent tasks) */
2610 /* Further initialization of the interface address */
2611 error
= in6_ifinit(ifp
, ia
, ifaupflags
);
2616 /* Finish updating the address while other tasks are working with it */
2617 error
= in6_ifaupdate_aux(ia
, ifp
, ifaupflags
);
2622 /* Return success (optionally w/ address for caller). */
2624 (void) ifnet_notify_address(ifp
, AF_INET6
);
2630 VERIFY(ifa
== &ia
->ia_ifa
);
2641 in6_purgeaddr(struct ifaddr
*ifa
)
2643 struct ifnet
*ifp
= ifa
->ifa_ifp
;
2644 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
2645 struct in6_multi_mship
*imm
;
2647 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2649 /* stop DAD processing */
2653 * delete route to the destination of the address being purged.
2654 * The interface must be p2p or loopback in this case.
2657 if ((ia
->ia_flags
& IFA_ROUTE
) && ia
->ia_plen
== 128) {
2661 rtf
= (ia
->ia_dstaddr
.sin6_family
== AF_INET6
) ? RTF_HOST
: 0;
2662 error
= rtinit(&(ia
->ia_ifa
), RTM_DELETE
, rtf
);
2664 log(LOG_ERR
, "in6_purgeaddr: failed to remove "
2665 "a route to the p2p destination: %s on %s, "
2667 ip6_sprintf(&ia
->ia_addr
.sin6_addr
), if_name(ifp
),
2669 /* proceed anyway... */
2672 ia
->ia_flags
&= ~IFA_ROUTE
;
2676 /* Remove ownaddr's loopback rtentry, if it exists. */
2677 in6_ifremloop(&(ia
->ia_ifa
));
2680 * leave from multicast groups we have joined for the interface
2683 while ((imm
= ia
->ia6_memberships
.lh_first
) != NULL
) {
2684 LIST_REMOVE(imm
, i6mm_chain
);
2686 in6_leavegroup(imm
);
2691 /* in6_unlink_ifa() will need exclusive access */
2692 in6_unlink_ifa(ia
, ifp
);
2693 in6_post_msg(ifp
, KEV_INET6_ADDR_DELETED
, ia
, NULL
);
2695 (void) ifnet_notify_address(ifp
, AF_INET6
);
2699 in6_unlink_ifa(struct in6_ifaddr
*ia
, struct ifnet
*ifp
)
2701 struct in6_ifaddr
*nia
;
2705 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2710 ifnet_lock_exclusive(ifp
);
2712 if (ifa
->ifa_debug
& IFD_ATTACHED
) {
2713 if_detach_ifa(ifp
, ifa
);
2716 ifnet_lock_done(ifp
);
2719 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2720 TAILQ_FOREACH(nia
, &in6_ifaddrhead
, ia6_link
) {
2722 TAILQ_REMOVE(&in6_ifaddrhead
, ia
, ia6_link
);
2724 if (IA6_IS_HASHED(ia
)) {
2725 in6_iahash_remove(ia
);
2734 * When IPv6 address is being removed, release the
2735 * reference to the base prefix.
2736 * Also, since the release might, affect the status
2737 * of other (detached) addresses, call
2738 * pfxlist_onlink_check().
2742 * Only log the below message for addresses other than
2744 * Only one LLA (auto-configured or statically) is allowed
2746 * LLA prefix, while added to the prefix list, is not
2747 * reference countedi (as it is the only one).
2748 * The prefix also never expires on its own as LLAs
2749 * have infinite lifetime.
2751 * For now quiece down the log message for LLAs.
2753 if (!IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
)) {
2754 if (ia
->ia6_ndpr
== NULL
) {
2755 log(LOG_NOTICE
, "in6_unlink_ifa: IPv6 address "
2756 "0x%llx has no prefix\n",
2757 (uint64_t)VM_KERNEL_ADDRPERM(ia
));
2759 struct nd_prefix
*pr
= ia
->ia6_ndpr
;
2760 ia
->ia6_flags
&= ~IN6_IFF_AUTOCONF
;
2761 ia
->ia6_ndpr
= NULL
;
2763 VERIFY(pr
->ndpr_addrcnt
!= 0);
2765 if (ia
->ia6_flags
& IN6_IFF_CLAT46
) {
2766 pr
->ndpr_stateflags
&= ~NDPRF_CLAT46
;
2769 NDPR_REMREF(pr
); /* release addr reference */
2773 lck_rw_done(&in6_ifaddr_rwlock
);
2775 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0) {
2776 lck_mtx_lock(nd6_mutex
);
2777 pfxlist_onlink_check();
2778 lck_mtx_unlock(nd6_mutex
);
2781 * release another refcnt for the link from in6_ifaddrs.
2782 * Do this only if it's not already unlinked in the event that we lost
2783 * the race, since in6_ifaddr_rwlock was momentarily dropped above.
2789 /* release reference held for this routine */
2792 /* invalidate route caches */
2793 routegenid_inet6_update();
2797 in6_purgeif(struct ifnet
*ifp
)
2799 struct in6_ifaddr
*ia
;
2805 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2807 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2808 boolean_t from_begining
= TRUE
;
2809 while (from_begining
) {
2810 from_begining
= FALSE
;
2811 TAILQ_FOREACH(ia
, &in6_ifaddrhead
, ia6_link
) {
2812 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
2815 IFA_ADDREF(&ia
->ia_ifa
); /* for us */
2816 lck_rw_done(&in6_ifaddr_rwlock
);
2817 in6_purgeaddr(&ia
->ia_ifa
);
2818 IFA_REMREF(&ia
->ia_ifa
); /* for us */
2819 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2821 * Purging the address would have caused
2822 * in6_ifaddr_rwlock to be dropped and reacquired;
2823 * therefore search again from the beginning
2824 * of in6_ifaddrs list.
2826 from_begining
= TRUE
;
2830 lck_rw_done(&in6_ifaddr_rwlock
);
2836 * Initialize an interface's internet6 address and routing table entry.
2839 in6_ifinit(struct ifnet
*ifp
, struct in6_ifaddr
*ia
, int ifaupflags
)
2847 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2848 IFA_LOCK(&ia
->ia_ifa
);
2849 if (IA6_IS_HASHED(ia
)) {
2850 in6_iahash_remove(ia
);
2852 if ((ifp
->if_flags
& IFF_POINTOPOINT
)) {
2853 in6_iahash_insert_ptp(ia
);
2855 in6_iahash_insert(ia
);
2857 IFA_UNLOCK(&ia
->ia_ifa
);
2858 lck_rw_done(&in6_ifaddr_rwlock
);
2861 * NOTE: SIOCSIFADDR is defined with struct ifreq as parameter,
2862 * but here we are sending it down to the interface with a pointer
2863 * to struct ifaddr, for legacy reasons.
2865 if ((ifaupflags
& IN6_IFAUPDATE_1STADDR
) != 0) {
2866 error
= ifnet_ioctl(ifp
, PF_INET6
, SIOCSIFADDR
, ia
);
2868 if (error
!= EOPNOTSUPP
) {
2879 * If the destination address is specified for a point-to-point
2880 * interface, install a route to the destination as an interface
2883 if (!(ia
->ia_flags
& IFA_ROUTE
) && ia
->ia_plen
== 128 &&
2884 ia
->ia_dstaddr
.sin6_family
== AF_INET6
) {
2886 error
= rtinit(ifa
, RTM_ADD
, RTF_UP
| RTF_HOST
);
2891 ia
->ia_flags
|= IFA_ROUTE
;
2893 IFA_LOCK_ASSERT_HELD(ifa
);
2894 if (ia
->ia_plen
< 128) {
2896 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
2898 ia
->ia_flags
|= RTF_CLONING
;
2903 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
2904 if ((ifaupflags
& IN6_IFAUPDATE_NEWADDR
) != 0) {
2908 /* invalidate route caches */
2909 routegenid_inet6_update();
2915 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2916 IFA_LOCK(&ia
->ia_ifa
);
2917 if (IA6_IS_HASHED(ia
)) {
2918 in6_iahash_remove(ia
);
2920 IFA_UNLOCK(&ia
->ia_ifa
);
2921 lck_rw_done(&in6_ifaddr_rwlock
);
2927 in6_purgeaddrs(struct ifnet
*ifp
)
2933 * Find an IPv6 interface link-local address specific to an interface.
2936 in6ifa_ifpforlinklocal(struct ifnet
*ifp
, int ignoreflags
)
2940 ifnet_lock_shared(ifp
);
2941 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2944 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2948 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
))) {
2949 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2950 ignoreflags
) != 0) {
2954 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2960 ifnet_lock_done(ifp
);
2962 return (struct in6_ifaddr
*)ifa
;
2966 in6ifa_ifpwithflag(struct ifnet
* ifp
, int flag
)
2970 ifnet_lock_shared(ifp
);
2971 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2974 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2978 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
& flag
) == flag
) {
2979 IFA_ADDREF_LOCKED(ifa
);
2985 ifnet_lock_done(ifp
);
2987 return (struct in6_ifaddr
*)ifa
;
2991 * find the internet address corresponding to a given interface and address.
2994 in6ifa_ifpwithaddr(struct ifnet
*ifp
, struct in6_addr
*addr
)
2998 ifnet_lock_shared(ifp
);
2999 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
3002 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3006 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(ifa
))) {
3007 IFA_ADDREF_LOCKED(ifa
); /* for caller */
3013 ifnet_lock_done(ifp
);
3015 return (struct in6_ifaddr
*)ifa
;
3019 in6ifa_prproxyaddr(struct in6_addr
*addr
)
3021 struct in6_ifaddr
*ia
;
3023 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
3024 TAILQ_FOREACH(ia
, IN6ADDR_HASH(addr
), ia6_hash
) {
3025 IFA_LOCK(&ia
->ia_ifa
);
3026 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(&ia
->ia_ifa
))) {
3027 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
3028 IFA_UNLOCK(&ia
->ia_ifa
);
3031 IFA_UNLOCK(&ia
->ia_ifa
);
3033 lck_rw_done(&in6_ifaddr_rwlock
);
3035 if (ia
!= NULL
&& !nd6_prproxy_ifaddr(ia
)) {
3036 IFA_REMREF(&ia
->ia_ifa
);
3044 in6ifa_getlifetime(struct in6_ifaddr
*ia6
, struct in6_addrlifetime
*t_dst
,
3047 struct in6_addrlifetime_i
*t_src
= &ia6
->ia6_lifetime
;
3048 struct timeval caltime
;
3050 t_dst
->ia6t_vltime
= t_src
->ia6ti_vltime
;
3051 t_dst
->ia6t_pltime
= t_src
->ia6ti_pltime
;
3052 t_dst
->ia6t_expire
= 0;
3053 t_dst
->ia6t_preferred
= 0;
3055 /* account for system time change */
3056 getmicrotime(&caltime
);
3057 t_src
->ia6ti_base_calendartime
+=
3058 NET_CALCULATE_CLOCKSKEW(caltime
,
3059 t_src
->ia6ti_base_calendartime
, net_uptime(),
3060 t_src
->ia6ti_base_uptime
);
3063 if (t_src
->ia6ti_expire
!= 0 &&
3064 t_src
->ia6ti_vltime
!= ND6_INFINITE_LIFETIME
) {
3065 t_dst
->ia6t_expire
= t_src
->ia6ti_base_calendartime
+
3066 t_src
->ia6ti_expire
- t_src
->ia6ti_base_uptime
;
3069 if (t_src
->ia6ti_preferred
!= 0 &&
3070 t_src
->ia6ti_pltime
!= ND6_INFINITE_LIFETIME
) {
3071 t_dst
->ia6t_preferred
= t_src
->ia6ti_base_calendartime
+
3072 t_src
->ia6ti_preferred
- t_src
->ia6ti_base_uptime
;
3075 if (t_src
->ia6ti_expire
!= 0 &&
3076 t_src
->ia6ti_vltime
!= ND6_INFINITE_LIFETIME
) {
3077 t_dst
->ia6t_expire
= t_src
->ia6ti_expire
;
3080 if (t_src
->ia6ti_preferred
!= 0 &&
3081 t_src
->ia6ti_pltime
!= ND6_INFINITE_LIFETIME
) {
3082 t_dst
->ia6t_preferred
= t_src
->ia6ti_preferred
;
3088 in6ifa_setlifetime(struct in6_ifaddr
*ia6
, struct in6_addrlifetime
*t_src
)
3090 struct in6_addrlifetime_i
*t_dst
= &ia6
->ia6_lifetime
;
3091 struct timeval caltime
;
3093 /* account for system time change */
3094 getmicrotime(&caltime
);
3095 t_dst
->ia6ti_base_calendartime
+=
3096 NET_CALCULATE_CLOCKSKEW(caltime
,
3097 t_dst
->ia6ti_base_calendartime
, net_uptime(),
3098 t_dst
->ia6ti_base_uptime
);
3100 /* trust the caller for the values */
3101 t_dst
->ia6ti_expire
= t_src
->ia6t_expire
;
3102 t_dst
->ia6ti_preferred
= t_src
->ia6t_preferred
;
3103 t_dst
->ia6ti_vltime
= t_src
->ia6t_vltime
;
3104 t_dst
->ia6ti_pltime
= t_src
->ia6t_pltime
;
3108 * Convert IP6 address to printable (loggable) representation.
3111 ip6_sprintf(const struct in6_addr
*addr
)
3113 static const char digits
[] = "0123456789abcdef";
3114 static int ip6round
= 0;
3115 static char ip6buf
[8][48];
3119 const u_short
*a
= (const u_short
*)addr
;
3125 ip6round
= (ip6round
+ 1) & 7;
3126 cp
= ip6buf
[ip6round
];
3128 for (i
= 0; i
< 8; i
++) {
3141 if (dcolon
== 0 && *(a
+ 1) == 0) {
3154 d
= (const u_char
*)a
;
3156 if ((n
= *d
>> 4) != 0) {
3160 if ((n
= *d
++ & 0xf) != 0 || zpad
) {
3164 if ((n
= *d
>> 4) != 0 || zpad
) {
3168 if ((n
= *d
& 0xf) != 0 || zpad
) {
3175 return ip6buf
[ip6round
];
3179 in6addr_local(struct in6_addr
*in6
)
3182 struct sockaddr_in6 sin6
;
3185 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_SCOPE_LINKLOCAL(in6
)) {
3189 sin6
.sin6_family
= AF_INET6
;
3190 sin6
.sin6_len
= sizeof(sin6
);
3191 bcopy(in6
, &sin6
.sin6_addr
, sizeof(*in6
));
3192 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0);
3196 if (rt
->rt_gateway
->sa_family
== AF_LINK
) {
3202 local
= in6_localaddr(in6
);
3208 in6_localaddr(struct in6_addr
*in6
)
3210 struct in6_ifaddr
*ia
;
3212 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
) || IN6_IS_ADDR_MC_UNICAST_BASED_LINKLOCAL(in6
)) {
3216 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
3217 TAILQ_FOREACH(ia
, &in6_ifaddrhead
, ia6_link
) {
3218 IFA_LOCK_SPIN(&ia
->ia_ifa
);
3219 if (IN6_ARE_MASKED_ADDR_EQUAL(in6
, &ia
->ia_addr
.sin6_addr
,
3220 &ia
->ia_prefixmask
.sin6_addr
)) {
3221 IFA_UNLOCK(&ia
->ia_ifa
);
3222 lck_rw_done(&in6_ifaddr_rwlock
);
3225 IFA_UNLOCK(&ia
->ia_ifa
);
3227 lck_rw_done(&in6_ifaddr_rwlock
);
3232 * return length of part which dst and src are equal
3236 in6_matchlen(struct in6_addr
*src
, struct in6_addr
*dst
)
3239 u_char
*s
= (u_char
*)src
, *d
= (u_char
*)dst
;
3240 u_char
*lim
= s
+ 16, r
;
3243 if ((r
= (*d
++ ^ *s
++)) != 0) {
3256 /* XXX: to be scope conscious */
3258 in6_are_prefix_equal(struct in6_addr
*p1
, struct in6_addr
*p2
, int len
)
3260 int bytelen
, bitlen
;
3263 if (0 > len
|| len
> 128) {
3264 log(LOG_ERR
, "%s: invalid prefix length(%d)\n", __func__
, len
);
3271 if (bcmp(&p1
->s6_addr
, &p2
->s6_addr
, bytelen
)) {
3275 p1
->s6_addr
[bytelen
] >> (8 - bitlen
) !=
3276 p2
->s6_addr
[bytelen
] >> (8 - bitlen
)) {
3284 in6_prefixlen2mask(struct in6_addr
*maskp
, int len
)
3286 u_char maskarray
[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
3287 int bytelen
, bitlen
, i
;
3290 if (0 > len
|| len
> 128) {
3291 log(LOG_ERR
, "%s: invalid prefix length(%d)\n", __func__
, len
);
3295 bzero(maskp
, sizeof(*maskp
));
3298 for (i
= 0; i
< bytelen
; i
++) {
3299 maskp
->s6_addr
[i
] = 0xff;
3302 maskp
->s6_addr
[bytelen
] = maskarray
[bitlen
- 1];
3307 * return the best address out of the same scope
3310 in6_ifawithscope(struct ifnet
*oifp
, struct in6_addr
*dst
)
3312 int dst_scope
= in6_addrscope(dst
), src_scope
, best_scope
= 0;
3316 struct in6_ifaddr
*ifa_best
= NULL
;
3323 * We search for all addresses on all interfaces from the beginning.
3324 * Comparing an interface with the outgoing interface will be done
3325 * only at the final stage of tiebreaking.
3327 ifnet_head_lock_shared();
3328 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
3330 * We can never take an address that breaks the scope zone
3331 * of the destination.
3333 if (in6_addr2scopeid(ifp
, dst
) != in6_addr2scopeid(oifp
, dst
)) {
3337 ifnet_lock_shared(ifp
);
3338 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3339 int tlen
= -1, dscopecmp
, bscopecmp
, matchcmp
;
3342 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3346 src_scope
= in6_addrscope(IFA_IN6(ifa
));
3349 * Don't use an address before completing DAD
3350 * nor a duplicated address.
3352 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3353 (IN6_IFF_NOTREADY
| IN6_IFF_CLAT46
)) {
3357 /* XXX: is there any case to allow anycasts? */
3358 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3363 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3369 * If this is the first address we find,
3372 if (ifa_best
== NULL
) {
3377 * ifa_best is never NULL beyond this line except
3378 * within the block labeled "replace".
3382 * If ifa_best has a smaller scope than dst and
3383 * the current address has a larger one than
3384 * (or equal to) dst, always replace ifa_best.
3385 * Also, if the current address has a smaller scope
3386 * than dst, ignore it unless ifa_best also has a
3388 * Consequently, after the two if-clause below,
3389 * the followings must be satisfied:
3390 * (scope(src) < scope(dst) &&
3391 * scope(best) < scope(dst))
3393 * (scope(best) >= scope(dst) &&
3394 * scope(src) >= scope(dst))
3396 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0 &&
3397 IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) >= 0) {
3398 goto replace
; /* (A) */
3400 if (IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) < 0 &&
3401 IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) >= 0) {
3406 * A deprecated address SHOULD NOT be used in new
3407 * communications if an alternate (non-deprecated)
3408 * address is available and has sufficient scope.
3409 * RFC 4862, Section 5.5.4.
3411 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3412 IN6_IFF_DEPRECATED
) {
3414 * Ignore any deprecated addresses if
3415 * specified by configuration.
3417 if (!ip6_use_deprecated
) {
3422 * If we have already found a non-deprecated
3423 * candidate, just ignore deprecated addresses.
3425 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
)
3433 * A non-deprecated address is always preferred
3434 * to a deprecated one regardless of scopes and
3435 * address matching (Note invariants ensured by the
3436 * conditions (A) and (B) above.)
3438 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) &&
3439 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3440 IN6_IFF_DEPRECATED
) == 0) {
3445 * When we use temporary addresses described in
3446 * RFC 4941, we prefer temporary addresses to
3447 * public autoconf addresses. Again, note the
3448 * invariants from (A) and (B). Also note that we
3449 * don't have any preference between static addresses
3450 * and autoconf addresses (despite of whether or not
3451 * the latter is temporary or public.)
3453 if (ip6_use_tempaddr
) {
3454 struct in6_ifaddr
*ifat
;
3456 ifat
= (struct in6_ifaddr
*)ifa
;
3457 if ((ifa_best
->ia6_flags
&
3458 (IN6_IFF_AUTOCONF
| IN6_IFF_TEMPORARY
))
3459 == IN6_IFF_AUTOCONF
&&
3461 (IN6_IFF_AUTOCONF
| IN6_IFF_TEMPORARY
))
3462 == (IN6_IFF_AUTOCONF
| IN6_IFF_TEMPORARY
)) {
3465 if ((ifa_best
->ia6_flags
&
3466 (IN6_IFF_AUTOCONF
| IN6_IFF_TEMPORARY
))
3467 == (IN6_IFF_AUTOCONF
| IN6_IFF_TEMPORARY
) &&
3469 (IN6_IFF_AUTOCONF
| IN6_IFF_TEMPORARY
))
3470 == IN6_IFF_AUTOCONF
) {
3477 * At this point, we have two cases:
3478 * 1. we are looking at a non-deprecated address,
3479 * and ifa_best is also non-deprecated.
3480 * 2. we are looking at a deprecated address,
3481 * and ifa_best is also deprecated.
3482 * Also, we do not have to consider a case where
3483 * the scope of if_best is larger(smaller) than dst and
3484 * the scope of the current address is smaller(larger)
3485 * than dst. Such a case has already been covered.
3486 * Tiebreaking is done according to the following
3488 * - the scope comparison between the address and
3490 * - the scope comparison between the address and
3491 * ifa_best (bscopecmp)
3492 * - if the address match dst longer than ifa_best
3494 * - if the address is on the outgoing I/F (outI/F)
3496 * Roughly speaking, the selection policy is
3497 * - the most important item is scope. The same scope
3498 * is best. Then search for a larger scope.
3499 * Smaller scopes are the last resort.
3500 * - A deprecated address is chosen only when we have
3501 * no address that has an enough scope, but is
3502 * prefered to any addresses of smaller scopes
3503 * (this must be already done above.)
3504 * - addresses on the outgoing I/F are preferred to
3505 * ones on other interfaces if none of above
3506 * tiebreaks. In the table below, the column "bI"
3507 * means if the best_ifa is on the outgoing
3508 * interface, and the column "sI" means if the ifa
3509 * is on the outgoing interface.
3510 * - If there is no other reasons to choose one,
3511 * longest address match against dst is considered.
3513 * The precise decision table is as follows:
3514 * dscopecmp bscopecmp match bI oI | replace?
3515 * N/A equal N/A Y N | No (1)
3516 * N/A equal N/A N Y | Yes (2)
3517 * N/A equal larger N/A | Yes (3)
3518 * N/A equal !larger N/A | No (4)
3519 * larger larger N/A N/A | No (5)
3520 * larger smaller N/A N/A | Yes (6)
3521 * smaller larger N/A N/A | Yes (7)
3522 * smaller smaller N/A N/A | No (8)
3523 * equal smaller N/A N/A | Yes (9)
3524 * equal larger (already done at A above)
3526 dscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
);
3527 bscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, best_scope
);
3529 if (bscopecmp
== 0) {
3530 struct ifnet
*bifp
= ifa_best
->ia_ifp
;
3532 if (bifp
== oifp
&& ifp
!= oifp
) { /* (1) */
3536 if (bifp
!= oifp
&& ifp
== oifp
) { /* (2) */
3541 * Both bifp and ifp are on the outgoing
3542 * interface, or both two are on a different
3543 * interface from the outgoing I/F.
3544 * now we need address matching against dst
3547 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3548 matchcmp
= tlen
- blen
;
3549 if (matchcmp
> 0) { /* (3) */
3555 if (dscopecmp
> 0) {
3556 if (bscopecmp
> 0) { /* (5) */
3560 goto replace
; /* (6) */
3562 if (dscopecmp
< 0) {
3563 if (bscopecmp
> 0) { /* (7) */
3570 /* now dscopecmp must be 0 */
3571 if (bscopecmp
< 0) {
3572 goto replace
; /* (9) */
3575 IFA_ADDREF_LOCKED(ifa
); /* for ifa_best */
3576 blen
= tlen
>= 0 ? tlen
:
3577 in6_matchlen(IFA_IN6(ifa
), dst
);
3579 in6_addrscope(&ifa2ia6(ifa
)->ia_addr
.sin6_addr
);
3582 IFA_REMREF(&ifa_best
->ia_ifa
);
3584 ifa_best
= (struct in6_ifaddr
*)ifa
;
3586 ifnet_lock_done(ifp
);
3590 /* count statistics for future improvements */
3591 if (ifa_best
== NULL
) {
3592 ip6stat
.ip6s_sources_none
++;
3594 IFA_LOCK_SPIN(&ifa_best
->ia_ifa
);
3595 if (oifp
== ifa_best
->ia_ifp
) {
3596 ip6stat
.ip6s_sources_sameif
[best_scope
]++;
3598 ip6stat
.ip6s_sources_otherif
[best_scope
]++;
3601 if (best_scope
== dst_scope
) {
3602 ip6stat
.ip6s_sources_samescope
[best_scope
]++;
3604 ip6stat
.ip6s_sources_otherscope
[best_scope
]++;
3607 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0) {
3608 ip6stat
.ip6s_sources_deprecated
[best_scope
]++;
3610 IFA_UNLOCK(&ifa_best
->ia_ifa
);
3617 * return the best address out of the same scope. if no address was
3618 * found, return the first valid address from designated IF.
3621 in6_ifawithifp(struct ifnet
*ifp
, struct in6_addr
*dst
)
3623 int dst_scope
= in6_addrscope(dst
), blen
= -1, tlen
;
3625 struct in6_ifaddr
*besta
= NULL
;
3626 struct in6_ifaddr
*dep
[2]; /* last-resort: deprecated */
3628 dep
[0] = dep
[1] = NULL
;
3631 * We first look for addresses in the same scope.
3632 * If there is one, return it.
3633 * If two or more, return one which matches the dst longest.
3634 * If none, return one of global addresses assigned other ifs.
3636 ifnet_lock_shared(ifp
);
3637 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3639 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3643 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3645 continue; /* XXX: is there any case to allow anycast? */
3647 if (ifa2ia6(ifa
)->ia6_flags
& (IN6_IFF_NOTREADY
| IN6_IFF_CLAT46
)) {
3649 continue; /* don't use this interface */
3651 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3655 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3656 if (ip6_use_deprecated
) {
3657 IFA_ADDREF_LOCKED(ifa
); /* for dep[0] */
3659 if (dep
[0] != NULL
) {
3660 IFA_REMREF(&dep
[0]->ia_ifa
);
3662 dep
[0] = (struct in6_ifaddr
*)ifa
;
3669 if (dst_scope
== in6_addrscope(IFA_IN6(ifa
))) {
3671 * call in6_matchlen() as few as possible
3676 IFA_LOCK(&besta
->ia_ifa
);
3677 blen
= in6_matchlen(
3678 &besta
->ia_addr
.sin6_addr
, dst
);
3679 IFA_UNLOCK(&besta
->ia_ifa
);
3682 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3685 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3687 IFA_REMREF(&besta
->ia_ifa
);
3688 besta
= (struct in6_ifaddr
*)ifa
;
3693 besta
= (struct in6_ifaddr
*)ifa
;
3694 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3702 ifnet_lock_done(ifp
);
3703 if (dep
[0] != NULL
) {
3704 IFA_REMREF(&dep
[0]->ia_ifa
);
3709 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3711 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3715 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3717 continue; /* XXX: is there any case to allow anycast? */
3719 if (ifa2ia6(ifa
)->ia6_flags
& (IN6_IFF_NOTREADY
| IN6_IFF_CLAT46
)) {
3721 continue; /* don't use this interface */
3723 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3727 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3728 if (ip6_use_deprecated
) {
3729 IFA_ADDREF_LOCKED(ifa
); /* for dep[1] */
3731 if (dep
[1] != NULL
) {
3732 IFA_REMREF(&dep
[1]->ia_ifa
);
3734 dep
[1] = (struct in6_ifaddr
*)ifa
;
3740 IFA_ADDREF_LOCKED(ifa
); /* for caller */
3742 ifnet_lock_done(ifp
);
3743 if (dep
[0] != NULL
) {
3744 IFA_REMREF(&dep
[0]->ia_ifa
);
3746 if (dep
[1] != NULL
) {
3747 IFA_REMREF(&dep
[1]->ia_ifa
);
3749 return (struct in6_ifaddr
*)ifa
;
3751 ifnet_lock_done(ifp
);
3753 /* use the last-resort values, that are, deprecated addresses */
3755 if (dep
[1] != NULL
) {
3756 IFA_REMREF(&dep
[1]->ia_ifa
);
3768 * perform DAD when interface becomes IFF_UP.
3771 in6_if_up_dad_start(struct ifnet
*ifp
)
3774 struct nd_ifinfo
*ndi
= NULL
;
3776 ndi
= ND_IFINFO(ifp
);
3777 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3778 if (!(ndi
->flags
& ND6_IFF_DAD
)) {
3782 /* start DAD on all the interface addresses */
3783 ifnet_lock_exclusive(ifp
);
3784 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3785 struct in6_ifaddr
*ia6
;
3788 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3792 ia6
= (struct in6_ifaddr
*)ifa
;
3793 if (ia6
->ia6_flags
& IN6_IFF_DADPROGRESS
) {
3794 int delay
= 0; /* delay ticks before DAD output */
3796 nd6_dad_start(ifa
, &delay
);
3801 ifnet_lock_done(ifp
);
3808 struct nd_ifinfo
*ndi
= NULL
;
3810 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
3814 ndi
= ND_IFINFO(ifp
);
3815 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3816 if (!(ndi
->flags
& ND6_IFF_DAD
)) {
3821 * If we are using the alternative neighbor discovery
3822 * interface on this interface, then skip DAD.
3824 * Also, skip it for interfaces marked "local private"
3825 * for now, even when not marked as using the alternative
3826 * interface. This is for historical reasons.
3828 if (ifp
->if_eflags
&
3829 (IFEF_IPV6_ND6ALT
| IFEF_LOCALNET_PRIVATE
| IFEF_DIRECTLINK
)) {
3833 if (ifp
->if_family
== IFNET_FAMILY_IPSEC
||
3834 ifp
->if_family
== IFNET_FAMILY_UTUN
) {
3836 * Ignore DAD for tunneling virtual interfaces, which get
3837 * their IPv6 address explicitly assigned.
3842 switch (ifp
->if_type
) {
3848 * These interfaces do not have the IFF_LOOPBACK flag,
3849 * but loop packets back. We do not have to do DAD on such
3850 * interfaces. We should even omit it, because loop-backed
3851 * NS would confuse the DAD procedure.
3856 * Our DAD routine requires the interface up and running.
3857 * However, some interfaces can be up before the RUNNING
3858 * status. Additionaly, users may try to assign addresses
3859 * before the interface becomes up (or running).
3860 * We simply skip DAD in such a case as a work around.
3861 * XXX: we should rather mark "tentative" on such addresses,
3862 * and do DAD after the interface becomes ready.
3864 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) !=
3865 (IFF_UP
| IFF_RUNNING
)) {
3874 * Calculate max IPv6 MTU through all the interfaces and store it
3880 u_int32_t maxmtu
= 0;
3883 ifnet_head_lock_shared();
3884 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
3885 struct nd_ifinfo
*ndi
= NULL
;
3887 if ((ndi
= ND_IFINFO(ifp
)) != NULL
&& !ndi
->initialized
) {
3891 lck_mtx_lock(&ndi
->lock
);
3893 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
3894 IN6_LINKMTU(ifp
) > maxmtu
) {
3895 maxmtu
= IN6_LINKMTU(ifp
);
3898 lck_mtx_unlock(&ndi
->lock
);
3902 if (maxmtu
) { /* update only when maxmtu is positive */
3903 in6_maxmtu
= maxmtu
;
3907 * Provide the length of interface identifiers to be used for the link attached
3908 * to the given interface. The length should be defined in "IPv6 over
3909 * xxx-link" document. Note that address architecture might also define
3910 * the length for a particular set of address prefixes, regardless of the
3911 * link type. Also see RFC 4862 for additional background.
3914 in6_if2idlen(struct ifnet
*ifp
)
3916 switch (ifp
->if_type
) {
3917 case IFT_ETHER
: /* RFC2464 */
3918 case IFT_IEEE8023ADLAG
: /* IEEE802.3ad Link Aggregate */
3919 #ifdef IFT_PROPVIRTUAL
3920 case IFT_PROPVIRTUAL
: /* XXX: no RFC. treat it as ether */
3923 case IFT_L2VLAN
: /* ditto */
3925 #ifdef IFT_IEEE80211
3926 case IFT_IEEE80211
: /* ditto */
3929 case IFT_MIP
: /* ditto */
3932 case IFT_FDDI
: /* RFC2467 */
3934 case IFT_ISO88025
: /* RFC2470 (IPv6 over Token Ring) */
3936 case IFT_PPP
: /* RFC2472 */
3938 case IFT_ARCNET
: /* RFC2497 */
3940 case IFT_FRELAY
: /* RFC2590 */
3942 case IFT_IEEE1394
: /* RFC3146 */
3945 return 64; /* draft-ietf-v6ops-mech-v2-07 */
3947 return 64; /* XXX: is this really correct? */
3949 return 64; /* for utun interfaces */
3951 return 64; /* Packet Data over Cellular */
3953 return 64; /* Transparent bridge interface */
3955 return 64; /* 6LoWPAN */
3958 * Unknown link type:
3959 * It might be controversial to use the today's common constant
3960 * of 64 for these cases unconditionally. For full compliance,
3961 * we should return an error in this case. On the other hand,
3962 * if we simply miss the standard for the link type or a new
3963 * standard is defined for a new link type, the IFID length
3964 * is very likely to be the common constant. As a compromise,
3965 * we always use the constant, but make an explicit notice
3966 * indicating the "unknown" case.
3968 log(LOG_NOTICE
, "%s: unknown link type (%d)\n", __func__
,
3974 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
3975 * v4 mapped addr or v4 compat addr
3978 in6_sin6_2_sin(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3980 bzero(sin
, sizeof(*sin
));
3981 sin
->sin_len
= sizeof(struct sockaddr_in
);
3982 sin
->sin_family
= AF_INET
;
3983 sin
->sin_port
= sin6
->sin6_port
;
3984 sin
->sin_addr
.s_addr
= sin6
->sin6_addr
.s6_addr32
[3];
3987 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
3989 in6_sin_2_v4mapsin6(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3991 bzero(sin6
, sizeof(*sin6
));
3992 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
3993 sin6
->sin6_family
= AF_INET6
;
3994 sin6
->sin6_port
= sin
->sin_port
;
3995 sin6
->sin6_addr
.s6_addr32
[0] = 0;
3996 sin6
->sin6_addr
.s6_addr32
[1] = 0;
3997 if (sin
->sin_addr
.s_addr
) {
3998 sin6
->sin6_addr
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
3999 sin6
->sin6_addr
.s6_addr32
[3] = sin
->sin_addr
.s_addr
;
4001 sin6
->sin6_addr
.s6_addr32
[2] = 0;
4002 sin6
->sin6_addr
.s6_addr32
[3] = 0;
4006 /* Convert sockaddr_in6 into sockaddr_in. */
4008 in6_sin6_2_sin_in_sock(struct sockaddr
*nam
)
4010 struct sockaddr_in
*sin_p
;
4011 struct sockaddr_in6 sin6
;
4014 * Save original sockaddr_in6 addr and convert it
4017 sin6
= *(struct sockaddr_in6
*)(void *)nam
;
4018 sin_p
= (struct sockaddr_in
*)(void *)nam
;
4019 in6_sin6_2_sin(sin_p
, &sin6
);
4022 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
4024 in6_sin_2_v4mapsin6_in_sock(struct sockaddr
**nam
)
4026 struct sockaddr_in
*sin_p
;
4027 struct sockaddr_in6
*sin6_p
;
4029 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof(*sin6_p
), M_SONAME
,
4031 if (sin6_p
== NULL
) {
4034 sin_p
= (struct sockaddr_in
*)(void *)*nam
;
4035 in6_sin_2_v4mapsin6(sin_p
, sin6_p
);
4036 FREE(*nam
, M_SONAME
);
4037 *nam
= (struct sockaddr
*)sin6_p
;
4043 * Posts in6_event_data message kernel events.
4045 * To get the same size of kev_in6_data between ILP32 and LP64 data models
4046 * we are using a special version of the in6_addrlifetime structure that
4047 * uses only 32 bits fields to be compatible with Leopard, and that
4048 * are large enough to span 68 years.
4051 in6_post_msg(struct ifnet
*ifp
, u_int32_t event_code
, struct in6_ifaddr
*ifa
,
4054 struct kev_msg ev_msg
;
4055 struct kev_in6_data in6_event_data
;
4056 struct in6_addrlifetime ia6_lt
;
4058 bzero(&in6_event_data
, sizeof(struct kev_in6_data
));
4059 bzero(&ev_msg
, sizeof(struct kev_msg
));
4060 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
4061 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
4062 ev_msg
.kev_subclass
= KEV_INET6_SUBCLASS
;
4063 ev_msg
.event_code
= event_code
;
4066 IFA_LOCK(&ifa
->ia_ifa
);
4067 in6_event_data
.ia_addr
= ifa
->ia_addr
;
4068 in6_event_data
.ia_net
= ifa
->ia_net
;
4069 in6_event_data
.ia_dstaddr
= ifa
->ia_dstaddr
;
4070 in6_event_data
.ia_prefixmask
= ifa
->ia_prefixmask
;
4071 in6_event_data
.ia_plen
= ifa
->ia_plen
;
4072 in6_event_data
.ia6_flags
= (u_int32_t
)ifa
->ia6_flags
;
4074 /* retrieve time as calendar time (last arg is 1) */
4075 in6ifa_getlifetime(ifa
, &ia6_lt
, 1);
4076 in6_event_data
.ia_lifetime
.ia6t_expire
= (u_int32_t
)ia6_lt
.ia6t_expire
;
4077 in6_event_data
.ia_lifetime
.ia6t_preferred
= (u_int32_t
)ia6_lt
.ia6t_preferred
;
4078 in6_event_data
.ia_lifetime
.ia6t_vltime
= ia6_lt
.ia6t_vltime
;
4079 in6_event_data
.ia_lifetime
.ia6t_pltime
= ia6_lt
.ia6t_pltime
;
4080 IFA_UNLOCK(&ifa
->ia_ifa
);
4084 (void) strlcpy(&in6_event_data
.link_data
.if_name
[0],
4085 ifp
->if_name
, IFNAMSIZ
);
4086 in6_event_data
.link_data
.if_family
= ifp
->if_family
;
4087 in6_event_data
.link_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
4091 memcpy(&in6_event_data
.ia_mac
, mac
,
4092 sizeof(in6_event_data
.ia_mac
));
4095 ev_msg
.dv
[0].data_ptr
= &in6_event_data
;
4096 ev_msg
.dv
[0].data_length
= sizeof(in6_event_data
);
4097 ev_msg
.dv
[1].data_length
= 0;
4099 dlil_post_complete_msg(NULL
, &ev_msg
);
4103 * Called as part of ip6_init
4106 in6_ifaddr_init(void)
4111 PE_parse_boot_argn("ifa_debug", &in6ifa_debug
, sizeof(in6ifa_debug
));
4113 vm_size_t in6ifa_size
= (in6ifa_debug
== 0) ? sizeof(struct in6_ifaddr
) :
4114 sizeof(struct in6_ifaddr_dbg
);
4116 in6ifa_zone
= zone_create(IN6IFA_ZONE_NAME
, in6ifa_size
, ZC_ZFREE_CLEARMEM
);
4118 lck_mtx_init(&in6ifa_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
4119 TAILQ_INIT(&in6ifa_trash_head
);
4122 static struct in6_ifaddr
*
4123 in6_ifaddr_alloc(zalloc_flags_t how
)
4125 struct in6_ifaddr
*in6ifa
;
4127 in6ifa
= zalloc_flags(in6ifa_zone
, how
| Z_ZERO
);
4128 if (in6ifa
!= NULL
) {
4129 in6ifa
->ia_ifa
.ifa_free
= in6_ifaddr_free
;
4130 in6ifa
->ia_ifa
.ifa_debug
|= IFD_ALLOC
;
4131 in6ifa
->ia_ifa
.ifa_del_wc
= &in6ifa
->ia_ifa
.ifa_debug
;
4132 in6ifa
->ia_ifa
.ifa_del_waiters
= 0;
4133 ifa_lock_init(&in6ifa
->ia_ifa
);
4134 if (in6ifa_debug
!= 0) {
4135 struct in6_ifaddr_dbg
*in6ifa_dbg
=
4136 (struct in6_ifaddr_dbg
*)in6ifa
;
4137 in6ifa
->ia_ifa
.ifa_debug
|= IFD_DEBUG
;
4138 in6ifa
->ia_ifa
.ifa_trace
= in6_ifaddr_trace
;
4139 in6ifa
->ia_ifa
.ifa_attached
= in6_ifaddr_attached
;
4140 in6ifa
->ia_ifa
.ifa_detached
= in6_ifaddr_detached
;
4141 ctrace_record(&in6ifa_dbg
->in6ifa_alloc
);
4149 in6_ifaddr_free(struct ifaddr
*ifa
)
4151 IFA_LOCK_ASSERT_HELD(ifa
);
4153 if (ifa
->ifa_refcnt
!= 0) {
4154 panic("%s: ifa %p bad ref cnt", __func__
, ifa
);
4156 } else if (!(ifa
->ifa_debug
& IFD_ALLOC
)) {
4157 panic("%s: ifa %p cannot be freed", __func__
, ifa
);
4160 if (ifa
->ifa_debug
& IFD_DEBUG
) {
4161 struct in6_ifaddr_dbg
*in6ifa_dbg
=
4162 (struct in6_ifaddr_dbg
*)ifa
;
4163 ctrace_record(&in6ifa_dbg
->in6ifa_free
);
4164 bcopy(&in6ifa_dbg
->in6ifa
, &in6ifa_dbg
->in6ifa_old
,
4165 sizeof(struct in6_ifaddr
));
4166 if (ifa
->ifa_debug
& IFD_TRASHED
) {
4167 /* Become a regular mutex, just in case */
4168 IFA_CONVERT_LOCK(ifa
);
4169 lck_mtx_lock(&in6ifa_trash_lock
);
4170 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
,
4172 lck_mtx_unlock(&in6ifa_trash_lock
);
4173 ifa
->ifa_debug
&= ~IFD_TRASHED
;
4177 ifa_lock_destroy(ifa
);
4178 bzero(ifa
, sizeof(struct in6_ifaddr
));
4179 zfree(in6ifa_zone
, ifa
);
4183 in6_ifaddr_attached(struct ifaddr
*ifa
)
4185 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
4187 IFA_LOCK_ASSERT_HELD(ifa
);
4189 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
4190 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
4193 if (ifa
->ifa_debug
& IFD_TRASHED
) {
4194 /* Become a regular mutex, just in case */
4195 IFA_CONVERT_LOCK(ifa
);
4196 lck_mtx_lock(&in6ifa_trash_lock
);
4197 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
4198 lck_mtx_unlock(&in6ifa_trash_lock
);
4199 ifa
->ifa_debug
&= ~IFD_TRASHED
;
4204 in6_ifaddr_detached(struct ifaddr
*ifa
)
4206 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
4208 IFA_LOCK_ASSERT_HELD(ifa
);
4210 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
4211 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
4213 } else if (ifa
->ifa_debug
& IFD_TRASHED
) {
4214 panic("%s: ifa %p is already in trash list", __func__
, ifa
);
4217 ifa
->ifa_debug
|= IFD_TRASHED
;
4218 /* Become a regular mutex, just in case */
4219 IFA_CONVERT_LOCK(ifa
);
4220 lck_mtx_lock(&in6ifa_trash_lock
);
4221 TAILQ_INSERT_TAIL(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
4222 lck_mtx_unlock(&in6ifa_trash_lock
);
4226 in6_ifaddr_trace(struct ifaddr
*ifa
, int refhold
)
4228 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
4233 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
4234 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
4238 cnt
= &in6ifa_dbg
->in6ifa_refhold_cnt
;
4239 tr
= in6ifa_dbg
->in6ifa_refhold
;
4241 cnt
= &in6ifa_dbg
->in6ifa_refrele_cnt
;
4242 tr
= in6ifa_dbg
->in6ifa_refrele
;
4245 idx
= atomic_add_16_ov(cnt
, 1) % IN6IFA_TRACE_HIST_SIZE
;
4246 ctrace_record(&tr
[idx
]);
4250 * Handle SIOCGASSOCIDS ioctl for PF_INET6 domain.
4253 in6_getassocids(struct socket
*so
, uint32_t *cnt
, user_addr_t aidp
)
4255 struct in6pcb
*in6p
= sotoin6pcb(so
);
4258 if (in6p
== NULL
|| in6p
->inp_state
== INPCB_STATE_DEAD
) {
4262 /* IN6PCB has no concept of association */
4263 aid
= SAE_ASSOCID_ANY
;
4266 /* just asking how many there are? */
4267 if (aidp
== USER_ADDR_NULL
) {
4271 return copyout(&aid
, aidp
, sizeof(aid
));
4275 * Handle SIOCGCONNIDS ioctl for PF_INET6 domain.
4278 in6_getconnids(struct socket
*so
, sae_associd_t aid
, uint32_t *cnt
,
4281 struct in6pcb
*in6p
= sotoin6pcb(so
);
4284 if (in6p
== NULL
|| in6p
->inp_state
== INPCB_STATE_DEAD
) {
4288 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
) {
4292 /* if connected, return 1 connection count */
4293 *cnt
= ((so
->so_state
& SS_ISCONNECTED
) ? 1 : 0);
4295 /* just asking how many there are? */
4296 if (cidp
== USER_ADDR_NULL
) {
4300 /* if IN6PCB is connected, assign it connid 1 */
4301 cid
= ((*cnt
!= 0) ? 1 : SAE_CONNID_ANY
);
4303 return copyout(&cid
, cidp
, sizeof(cid
));
4307 * Handle SIOCGCONNINFO ioctl for PF_INET6 domain.
4310 in6_getconninfo(struct socket
*so
, sae_connid_t cid
, uint32_t *flags
,
4311 uint32_t *ifindex
, int32_t *soerror
, user_addr_t src
, socklen_t
*src_len
,
4312 user_addr_t dst
, socklen_t
*dst_len
, uint32_t *aux_type
,
4313 user_addr_t aux_data
, uint32_t *aux_len
)
4315 struct in6pcb
*in6p
= sotoin6pcb(so
);
4316 struct sockaddr_in6 sin6
;
4317 struct ifnet
*ifp
= NULL
;
4319 u_int32_t copy_len
= 0;
4322 * Don't test for INPCB_STATE_DEAD since this may be called
4323 * after SOF_PCBCLEARING is set, e.g. after tcp_close().
4330 if (cid
!= SAE_CONNID_ANY
&& cid
!= SAE_CONNID_ALL
&& cid
!= 1) {
4335 ifp
= in6p
->in6p_last_outifp
;
4336 *ifindex
= ((ifp
!= NULL
) ? ifp
->if_index
: 0);
4337 *soerror
= so
->so_error
;
4339 if (so
->so_state
& SS_ISCONNECTED
) {
4340 *flags
|= (CIF_CONNECTED
| CIF_PREFERRED
);
4342 if (in6p
->in6p_flags
& INP_BOUND_IF
) {
4343 *flags
|= CIF_BOUND_IF
;
4345 if (!(in6p
->in6p_flags
& INP_IN6ADDR_ANY
)) {
4346 *flags
|= CIF_BOUND_IP
;
4348 if (!(in6p
->in6p_flags
& INP_ANONPORT
)) {
4349 *flags
|= CIF_BOUND_PORT
;
4352 bzero(&sin6
, sizeof(sin6
));
4353 sin6
.sin6_len
= sizeof(sin6
);
4354 sin6
.sin6_family
= AF_INET6
;
4356 /* source address and port */
4357 sin6
.sin6_port
= in6p
->in6p_lport
;
4358 in6_recoverscope(&sin6
, &in6p
->in6p_laddr
, NULL
);
4359 if (*src_len
== 0) {
4360 *src_len
= sin6
.sin6_len
;
4362 if (src
!= USER_ADDR_NULL
) {
4363 copy_len
= min(*src_len
, sizeof(sin6
));
4364 error
= copyout(&sin6
, src
, copy_len
);
4368 *src_len
= copy_len
;
4372 /* destination address and port */
4373 sin6
.sin6_port
= in6p
->in6p_fport
;
4374 in6_recoverscope(&sin6
, &in6p
->in6p_faddr
, NULL
);
4375 if (*dst_len
== 0) {
4376 *dst_len
= sin6
.sin6_len
;
4378 if (dst
!= USER_ADDR_NULL
) {
4379 copy_len
= min(*dst_len
, sizeof(sin6
));
4380 error
= copyout(&sin6
, dst
, copy_len
);
4384 *dst_len
= copy_len
;
4388 if (SOCK_PROTO(so
) == IPPROTO_TCP
) {
4389 struct conninfo_tcp tcp_ci
;
4391 *aux_type
= CIAUX_TCP
;
4392 if (*aux_len
== 0) {
4393 *aux_len
= sizeof(tcp_ci
);
4395 if (aux_data
!= USER_ADDR_NULL
) {
4396 copy_len
= min(*aux_len
, sizeof(tcp_ci
));
4397 bzero(&tcp_ci
, sizeof(tcp_ci
));
4398 tcp_getconninfo(so
, &tcp_ci
);
4399 error
= copyout(&tcp_ci
, aux_data
, copy_len
);
4403 *aux_len
= copy_len
;
4418 * The switch statement below does nothing at runtime, as it serves as a
4419 * compile time check to ensure that all of the socket 'u' ioctls (those
4420 * in the 'u' group going thru soo_ioctl) that are made available by the
4421 * networking stack is unique. This works as long as this routine gets
4422 * updated each time a new interface ioctl gets added.
4424 * Any failures at compile time indicates duplicated ioctl values.
4426 static __attribute__((unused
)) void
4427 in6ioctl_cassert(void)
4430 * This is equivalent to _CASSERT() and the compiler wouldn't
4431 * generate any instructions, thus for compile time only.
4433 switch ((u_long
)0) {
4436 /* bsd/netinet6/in6_var.h */
4437 case SIOCAADDRCTL_POLICY
:
4438 case SIOCDADDRCTL_POLICY
:
4439 case SIOCDRADD_IN6_32
:
4440 case SIOCDRADD_IN6_64
:
4441 case SIOCDRDEL_IN6_32
:
4442 case SIOCDRDEL_IN6_64
:
4447 struct in6_llentry
{
4448 struct llentry base
;
4451 #define IN6_LLTBL_DEFAULT_HSIZE 32
4452 #define IN6_LLTBL_HASH(k, h) \
4453 ((((((((k) >> 8) ^ (k)) >> 8) ^ (k)) >> 8) ^ (k)) & ((h) - 1))
4456 * Do actual deallocation of @lle.
4459 in6_lltable_destroy_lle_unlocked(struct llentry
*lle
)
4461 LLE_LOCK_DESTROY(lle
);
4462 LLE_REQ_DESTROY(lle
);
4463 FREE(lle
, M_LLTABLE
);
4467 * Called by LLE_FREE_LOCKED when number of references
4471 in6_lltable_destroy_lle(struct llentry
*lle
)
4475 //thread_call_free(lle->lle_timer);
4476 in6_lltable_destroy_lle_unlocked(lle
);
4480 static struct llentry
*
4481 in6_lltable_new(const struct in6_addr
*addr6
, uint16_t flags
)
4483 #pragma unused(flags)
4484 struct in6_llentry
*lle
;
4486 MALLOC(lle
, struct in6_llentry
*, sizeof(struct in6_llentry
), M_LLTABLE
, M_NOWAIT
| M_ZERO
);
4487 if (lle
== NULL
) { /* NB: caller generates msg */
4491 lle
->base
.r_l3addr
.addr6
= *addr6
;
4492 lle
->base
.lle_refcnt
= 1;
4493 lle
->base
.lle_free
= in6_lltable_destroy_lle
;
4494 LLE_LOCK_INIT(&lle
->base
);
4495 LLE_REQ_INIT(&lle
->base
);
4498 lle
->base
.lle_timer
= thread_call_allocate(nd6_llinfo_timer
, lle
);
4500 if (lle
->base
.lle_timer
== NULL
) {
4501 printf("lle_timer thread call could not be allocated.\n");
4502 LLE_LOCK_DESTROY(&lle
->base
);
4503 LLE_REQ_DESTROY(&lle
->base
);
4504 FREE(lle
, M_LLTABLE
);
4512 in6_lltable_match_prefix(const struct sockaddr
*saddr
,
4513 const struct sockaddr
*smask
, uint16_t flags
, struct llentry
*lle
)
4515 const struct in6_addr
*addr
, *mask
, *lle_addr
;
4517 addr
= &((const struct sockaddr_in6
*)(const void *)saddr
)->sin6_addr
;
4518 mask
= &((const struct sockaddr_in6
*)(const void *)smask
)->sin6_addr
;
4519 lle_addr
= &lle
->r_l3addr
.addr6
;
4521 if (IN6_ARE_MASKED_ADDR_EQUAL(lle_addr
, addr
, mask
) == 0) {
4525 if (lle
->la_flags
& LLE_IFADDR
) {
4527 * Delete LLE_IFADDR records IFF address & flag matches.
4528 * Note that addr is the interface address within prefix
4531 if (IN6_ARE_ADDR_EQUAL(addr
, lle_addr
) &&
4532 (flags
& LLE_STATIC
) != 0) {
4538 /* flags & LLE_STATIC means deleting both dynamic and static entries */
4539 if ((flags
& LLE_STATIC
) || !(lle
->la_flags
& LLE_STATIC
)) {
4547 in6_lltable_free_entry(struct lltable
*llt
, struct llentry
*lle
)
4551 LLE_WLOCK_ASSERT(lle
);
4552 KASSERT(llt
!= NULL
, ("lltable is NULL"));
4554 /* Unlink entry from table */
4555 if ((lle
->la_flags
& LLE_LINKED
) != 0) {
4557 if_afdata_wlock_assert(ifp
, llt
->llt_af
);
4558 lltable_unlink_entry(llt
, lle
);
4563 if (thread_call_cancel(lle
->lle_timer
) == TRUE
) {
4571 in6_lltable_rtcheck(struct ifnet
*ifp
,
4572 uint16_t flags
, const struct sockaddr
*l3addr
)
4574 #pragma unused(flags)
4577 KASSERT(l3addr
->sa_family
== AF_INET6
,
4578 ("sin_family %d", l3addr
->sa_family
));
4579 /* XXX rtalloc1 should take a const param */
4580 rt
= rtalloc1(__DECONST(struct sockaddr
*, l3addr
), 0, 0);
4581 if (rt
== NULL
|| (rt
->rt_flags
& RTF_GATEWAY
) || rt
->rt_ifp
!= ifp
) {
4584 * Create an ND6 cache for an IPv6 neighbor
4585 * that is not covered by our own prefix.
4587 /* XXX ifaof_ifpforaddr should take a const param */
4588 ifa
= ifaof_ifpforaddr(__DECONST(struct sockaddr
*, l3addr
), ifp
);
4596 log(LOG_INFO
, "IPv6 address: \"%s\" is not on the network\n",
4597 ip6_sprintf(&((const struct sockaddr_in6
*)(const void *)l3addr
)->sin6_addr
));
4607 static inline uint32_t
4608 in6_lltable_hash_dst(const struct in6_addr
*dst
, uint32_t hsize
)
4610 return IN6_LLTBL_HASH(dst
->s6_addr32
[3], hsize
);
4614 in6_lltable_hash(const struct llentry
*lle
, uint32_t hsize
)
4616 return in6_lltable_hash_dst(&lle
->r_l3addr
.addr6
, hsize
);
4620 in6_lltable_fill_sa_entry(const struct llentry
*lle
, struct sockaddr
*sa
)
4622 struct sockaddr_in6
*sin6
;
4624 sin6
= (struct sockaddr_in6
*)(void *)sa
;
4625 bzero(sin6
, sizeof(*sin6
));
4626 sin6
->sin6_family
= AF_INET6
;
4627 sin6
->sin6_len
= sizeof(*sin6
);
4628 sin6
->sin6_addr
= lle
->r_l3addr
.addr6
;
4631 static inline struct llentry
*
4632 in6_lltable_find_dst(struct lltable
*llt
, const struct in6_addr
*dst
)
4634 struct llentry
*lle
;
4635 struct llentries
*lleh
;
4638 hashidx
= in6_lltable_hash_dst(dst
, llt
->llt_hsize
);
4639 lleh
= &llt
->lle_head
[hashidx
];
4640 LIST_FOREACH(lle
, lleh
, lle_next
) {
4641 if (lle
->la_flags
& LLE_DELETED
) {
4644 if (IN6_ARE_ADDR_EQUAL(&lle
->r_l3addr
.addr6
, dst
)) {
4653 in6_lltable_delete_entry(struct lltable
*llt
, struct llentry
*lle
)
4656 lle
->la_flags
|= LLE_DELETED
;
4657 EVENTHANDLER_INVOKE(NULL
, lle_event
, lle
, LLENTRY_DELETED
);
4659 log(LOG_INFO
, "ifaddr cache = %p is deleted\n", lle
);
4664 static struct llentry
*
4665 in6_lltable_alloc(struct lltable
*llt
, uint16_t flags
,
4666 const struct sockaddr
*l3addr
)
4668 const struct sockaddr_in6
*sin6
= (const struct sockaddr_in6
*)(const void *)l3addr
;
4669 struct ifnet
*ifp
= llt
->llt_ifp
;
4670 struct llentry
*lle
;
4672 KASSERT(l3addr
->sa_family
== AF_INET6
,
4673 ("sin_family %d", l3addr
->sa_family
));
4676 * A route that covers the given address must have
4677 * been installed 1st because we are doing a resolution,
4680 if (!(flags
& LLE_IFADDR
) &&
4681 in6_lltable_rtcheck(ifp
, flags
, l3addr
) != 0) {
4685 lle
= in6_lltable_new(&sin6
->sin6_addr
, flags
);
4687 log(LOG_INFO
, "lla_lookup: new lle malloc failed\n");
4690 lle
->la_flags
= (uint16_t)flags
;
4691 if ((flags
& LLE_IFADDR
) == LLE_IFADDR
) {
4692 lltable_set_entry_addr(ifp
, lle
, LLADDR(SDL(ifp
->if_lladdr
->ifa_addr
)));
4693 lle
->la_flags
|= LLE_STATIC
;
4696 if ((lle
->la_flags
& LLE_STATIC
) != 0) {
4697 lle
->ln_state
= ND6_LLINFO_REACHABLE
;
4703 static struct llentry
*
4704 in6_lltable_lookup(struct lltable
*llt
, uint16_t flags
,
4705 const struct sockaddr
*l3addr
)
4707 const struct sockaddr_in6
*sin6
= (const struct sockaddr_in6
*)(const void *)l3addr
;
4708 struct llentry
*lle
;
4710 IF_AFDATA_LOCK_ASSERT(llt
->llt_ifp
, llt
->llt_af
);
4711 KASSERT(l3addr
->sa_family
== AF_INET6
,
4712 ("sin_family %d", l3addr
->sa_family
));
4714 lle
= in6_lltable_find_dst(llt
, &sin6
->sin6_addr
);
4720 KASSERT((flags
& (LLE_UNLOCKED
| LLE_EXCLUSIVE
)) !=
4721 (LLE_UNLOCKED
| LLE_EXCLUSIVE
), ("wrong lle request flags: 0x%X",
4724 if (flags
& LLE_UNLOCKED
) {
4728 if (flags
& LLE_EXCLUSIVE
) {
4737 in6_lltable_dump_entry(struct lltable
*llt
, struct llentry
*lle
,
4738 struct sysctl_req
*wr
)
4740 struct ifnet
*ifp
= llt
->llt_ifp
;
4743 struct rt_msghdr rtm
;
4744 struct sockaddr_in6 sin6
;
4746 * ndp.c assumes that sdl is word aligned
4751 struct sockaddr_dl sdl
;
4753 struct sockaddr_dl
*sdl
;
4756 bzero(&ndpc
, sizeof(ndpc
));
4757 /* skip deleted entries */
4758 if ((lle
->la_flags
& LLE_DELETED
) == LLE_DELETED
) {
4761 /* Skip if jailed and not a valid IP of the prison. */
4762 lltable_fill_sa_entry(lle
,
4763 (struct sockaddr
*)&ndpc
.sin6
);
4765 * produce a msg made of:
4767 * struct sockaddr_in6 (IPv6)
4768 * struct sockaddr_dl;
4770 ndpc
.rtm
.rtm_msglen
= sizeof(ndpc
);
4771 ndpc
.rtm
.rtm_version
= RTM_VERSION
;
4772 ndpc
.rtm
.rtm_type
= RTM_GET
;
4773 ndpc
.rtm
.rtm_flags
= RTF_UP
;
4774 ndpc
.rtm
.rtm_addrs
= RTA_DST
| RTA_GATEWAY
;
4777 if (lle
->la_flags
& LLE_PUB
) {
4778 ndpc
.rtm
.rtm_flags
|= RTF_ANNOUNCE
;
4781 sdl
->sdl_family
= AF_LINK
;
4782 sdl
->sdl_len
= sizeof(*sdl
);
4783 sdl
->sdl_index
= ifp
->if_index
;
4784 sdl
->sdl_type
= ifp
->if_type
;
4785 if ((lle
->la_flags
& LLE_VALID
) == LLE_VALID
) {
4786 sdl
->sdl_alen
= ifp
->if_addrlen
;
4787 bcopy(&lle
->ll_addr
, LLADDR(sdl
), ifp
->if_addrlen
);
4790 bzero(LLADDR(sdl
), ifp
->if_addrlen
);
4792 if (lle
->la_expire
!= 0) {
4796 clock_get_calendar_microtime(&secs
, &usecs
);
4797 ndpc
.rtm
.rtm_rmx
.rmx_expire
= (int32_t)(lle
->la_expire
+
4798 lle
->lle_remtime
/ hz
+
4799 secs
- net_uptime());
4801 ndpc
.rtm
.rtm_flags
|= (RTF_HOST
| RTF_LLDATA
);
4802 if (lle
->la_flags
& LLE_STATIC
) {
4803 ndpc
.rtm
.rtm_flags
|= RTF_STATIC
;
4805 if (lle
->la_flags
& LLE_IFADDR
) {
4806 ndpc
.rtm
.rtm_flags
|= RTF_PINNED
;
4808 if (lle
->ln_router
!= 0) {
4809 ndpc
.rtm
.rtm_flags
|= RTF_GATEWAY
;
4811 ndpc
.rtm
.rtm_rmx
.rmx_pksent
= lle
->la_asked
;
4812 /* Store state in rmx_weight value */
4813 ndpc
.rtm
.rtm_rmx
.rmx_state
= lle
->ln_state
;
4814 ndpc
.rtm
.rtm_index
= ifp
->if_index
;
4815 error
= SYSCTL_OUT(wr
, &ndpc
, sizeof(ndpc
));
4821 in6_lltattach(struct ifnet
*ifp
)
4823 struct lltable
*llt
;
4825 llt
= lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE
);
4826 llt
->llt_af
= AF_INET6
;
4829 llt
->llt_lookup
= in6_lltable_lookup
;
4830 llt
->llt_alloc_entry
= in6_lltable_alloc
;
4831 llt
->llt_delete_entry
= in6_lltable_delete_entry
;
4832 llt
->llt_dump_entry
= in6_lltable_dump_entry
;
4833 llt
->llt_hash
= in6_lltable_hash
;
4834 llt
->llt_fill_sa_entry
= in6_lltable_fill_sa_entry
;
4835 llt
->llt_free_entry
= in6_lltable_free_entry
;
4836 llt
->llt_match_prefix
= in6_lltable_match_prefix
;
4843 in6_ip6_to_sockaddr(const struct in6_addr
*ip6
, u_int16_t port
,
4844 struct sockaddr_in6
*sin6
, u_int32_t maxlen
)
4846 if (maxlen
< sizeof(struct sockaddr_in6
)) {
4850 *sin6
= (struct sockaddr_in6
) {
4851 .sin6_family
= AF_INET6
,
4852 .sin6_len
= sizeof(*sin6
),
4857 if (IN6_IS_SCOPE_EMBED(&sin6
->sin6_addr
)) {
4858 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
4859 sin6
->sin6_addr
.s6_addr16
[1] = 0;
4865 in6_evhdlr_code_t in6_event_code
;
4866 struct ifnet
*in6_ifp
;
4867 struct in6_addr in6_address
;
4871 struct in6_event2kev in6_event2kev_array
[IN6_EVENT_MAX
] = {
4873 .in6_event_code
= IN6_ADDR_MARKED_DUPLICATED
,
4874 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4875 .in6_event_kev_code
= KEV_ND6_DAD_FAILURE
,
4876 .in6_event_str
= "IN6_ADDR_MARKED_DUPLICATED",
4879 .in6_event_code
= IN6_ADDR_MARKED_DETACHED
,
4880 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4881 .in6_event_kev_code
= KEV_ND6_ADDR_DETACHED
,
4882 .in6_event_str
= "IN6_ADDR_MARKED_DETACHED",
4885 .in6_event_code
= IN6_ADDR_MARKED_DEPRECATED
,
4886 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4887 .in6_event_kev_code
= KEV_ND6_ADDR_DEPRECATED
,
4888 .in6_event_str
= "IN6_ADDR_MARKED_DEPRECATED",
4891 .in6_event_code
= IN6_NDP_RTR_EXPIRY
,
4892 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4893 .in6_event_kev_code
= KEV_ND6_RTR_EXPIRED
,
4894 .in6_event_str
= "IN6_NDP_RTR_EXPIRY",
4897 .in6_event_code
= IN6_NDP_PFX_EXPIRY
,
4898 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4899 .in6_event_kev_code
= KEV_ND6_PFX_EXPIRED
,
4900 .in6_event_str
= "IN6_NDP_PFX_EXPIRY",
4903 .in6_event_code
= IN6_NDP_ADDR_EXPIRY
,
4904 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4905 .in6_event_kev_code
= KEV_ND6_ADDR_EXPIRED
,
4906 .in6_event_str
= "IN6_NDP_ADDR_EXPIRY",
4911 in6_eventhdlr_callback(struct eventhandler_entry_arg arg0 __unused
,
4912 in6_evhdlr_code_t in6_ev_code
, struct ifnet
*ifp
,
4913 struct in6_addr
*p_addr6
, uint32_t val
)
4915 struct kev_msg ev_msg
;
4916 struct kev_nd6_event nd6_event
;
4918 bzero(&ev_msg
, sizeof(ev_msg
));
4919 bzero(&nd6_event
, sizeof(nd6_event
));
4921 nd6log0(info
, "%s Event %s received for %s\n",
4922 __func__
, in6_event2kev_array
[in6_ev_code
].in6_event_str
,
4923 ip6_sprintf(p_addr6
));
4925 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
4926 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
4927 ev_msg
.kev_subclass
=
4928 in6_event2kev_array
[in6_ev_code
].in6_event_kev_subclass
;
4930 in6_event2kev_array
[in6_ev_code
].in6_event_kev_code
;
4932 nd6_event
.link_data
.if_family
= ifp
->if_family
;
4933 nd6_event
.link_data
.if_unit
= ifp
->if_unit
;
4934 strlcpy(nd6_event
.link_data
.if_name
, ifp
->if_name
,
4935 sizeof(nd6_event
.link_data
.if_name
));
4937 VERIFY(p_addr6
!= NULL
);
4938 bcopy(p_addr6
, &nd6_event
.in6_address
,
4939 sizeof(nd6_event
.in6_address
));
4940 nd6_event
.val
= val
;
4942 ev_msg
.dv
[0].data_ptr
= &nd6_event
;
4943 ev_msg
.dv
[0].data_length
= sizeof(nd6_event
);
4945 kev_post_msg(&ev_msg
);
4949 in6_event_callback(void *arg
)
4951 struct in6_event
*p_in6_ev
= (struct in6_event
*)arg
;
4953 EVENTHANDLER_INVOKE(&in6_evhdlr_ctxt
, in6_event
,
4954 p_in6_ev
->in6_event_code
, p_in6_ev
->in6_ifp
,
4955 &p_in6_ev
->in6_address
, p_in6_ev
->val
);
4958 struct in6_event_nwk_wq_entry
{
4959 struct nwk_wq_entry nwk_wqe
;
4960 struct in6_event in6_ev_arg
;
4964 in6_event_enqueue_nwk_wq_entry(in6_evhdlr_code_t in6_event_code
,
4965 struct ifnet
*ifp
, struct in6_addr
*p_addr6
,
4968 struct in6_event_nwk_wq_entry
*p_in6_ev
= NULL
;
4970 MALLOC(p_in6_ev
, struct in6_event_nwk_wq_entry
*,
4971 sizeof(struct in6_event_nwk_wq_entry
),
4972 M_NWKWQ
, M_WAITOK
| M_ZERO
);
4974 p_in6_ev
->nwk_wqe
.func
= in6_event_callback
;
4975 p_in6_ev
->nwk_wqe
.is_arg_managed
= TRUE
;
4976 p_in6_ev
->nwk_wqe
.arg
= &p_in6_ev
->in6_ev_arg
;
4978 p_in6_ev
->in6_ev_arg
.in6_event_code
= in6_event_code
;
4979 p_in6_ev
->in6_ev_arg
.in6_ifp
= ifp
;
4980 if (p_addr6
!= NULL
) {
4981 bcopy(p_addr6
, &p_in6_ev
->in6_ev_arg
.in6_address
,
4982 sizeof(p_in6_ev
->in6_ev_arg
.in6_address
));
4984 p_in6_ev
->in6_ev_arg
.val
= val
;
4986 nwk_wq_enqueue((struct nwk_wq_entry
*)p_in6_ev
);
4990 * Caller must hold in6_ifaddr_rwlock as writer.
4993 in6_iahash_remove(struct in6_ifaddr
*ia
)
4995 LCK_RW_ASSERT(&in6_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
4996 IFA_LOCK_ASSERT_HELD(&ia
->ia_ifa
);
4998 if (!IA6_IS_HASHED(ia
)) {
4999 panic("%s: attempt to remove wrong ia %p from ipv6 hash table\n", __func__
, ia
);
5002 TAILQ_REMOVE(IN6ADDR_HASH(&ia
->ia_addr
.sin6_addr
), ia
, ia6_hash
);
5004 if (IFA_REMREF_LOCKED(&ia
->ia_ifa
) == NULL
) {
5005 panic("%s: unexpected (missing) refcnt ifa=%p", __func__
,
5012 * Caller must hold in6_ifaddr_rwlock as writer.
5015 in6_iahash_insert(struct in6_ifaddr
*ia
)
5017 LCK_RW_ASSERT(&in6_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
5018 IFA_LOCK_ASSERT_HELD(&ia
->ia_ifa
);
5020 if (ia
->ia_addr
.sin6_family
!= AF_INET6
) {
5021 panic("%s: attempt to insert wrong ia %p into hash table\n", __func__
, ia
);
5023 } else if (IA6_IS_HASHED(ia
)) {
5024 panic("%s: attempt to double-insert ia %p into hash table\n", __func__
, ia
);
5027 TAILQ_INSERT_HEAD(IN6ADDR_HASH(&ia
->ia_addr
.sin6_addr
),
5029 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
5033 * Some point to point interfaces that are tunnels borrow the address from
5034 * an underlying interface (e.g. VPN server). In order for source address
5035 * selection logic to find the underlying interface first, we add the address
5036 * of borrowing point to point interfaces at the end of the list.
5037 * (see rdar://6733789)
5039 * Caller must hold in6_ifaddr_rwlock as writer.
5042 in6_iahash_insert_ptp(struct in6_ifaddr
*ia
)
5044 struct in6_ifaddr
*tmp_ifa
;
5045 struct ifnet
*tmp_ifp
;
5047 LCK_RW_ASSERT(&in6_ifaddr_rwlock
, LCK_RW_ASSERT_EXCLUSIVE
);
5048 IFA_LOCK_ASSERT_HELD(&ia
->ia_ifa
);
5050 if (ia
->ia_addr
.sin6_family
!= AF_INET6
) {
5051 panic("%s: attempt to insert wrong ia %p into hash table\n", __func__
, ia
);
5053 } else if (IA6_IS_HASHED(ia
)) {
5054 panic("%s: attempt to double-insert ia %p into hash table\n", __func__
, ia
);
5057 IFA_UNLOCK(&ia
->ia_ifa
);
5058 TAILQ_FOREACH(tmp_ifa
, IN6ADDR_HASH(&ia
->ia_addr
.sin6_addr
), ia6_hash
) {
5059 IFA_LOCK(&tmp_ifa
->ia_ifa
);
5060 /* ia->ia_addr won't change, so check without lock */
5061 if (IN6_ARE_ADDR_EQUAL(&tmp_ifa
->ia_addr
.sin6_addr
, &ia
->ia_addr
.sin6_addr
)) {
5062 IFA_UNLOCK(&tmp_ifa
->ia_ifa
);
5065 IFA_UNLOCK(&tmp_ifa
->ia_ifa
);
5067 tmp_ifp
= (tmp_ifa
== NULL
) ? NULL
: tmp_ifa
->ia_ifp
;
5069 IFA_LOCK(&ia
->ia_ifa
);
5070 if (tmp_ifp
== NULL
) {
5071 TAILQ_INSERT_HEAD(IN6ADDR_HASH(&ia
->ia_addr
.sin6_addr
),
5074 TAILQ_INSERT_TAIL(IN6ADDR_HASH(&ia
->ia_addr
.sin6_addr
),
5077 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);