2 * Copyright (c) 2003-2016 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>
109 #include <kern/locks.h>
110 #include <kern/zalloc.h>
111 #include <libkern/OSAtomic.h>
112 #include <machine/machine_routines.h>
113 #include <mach/boolean.h>
116 #include <net/if_types.h>
117 #include <net/if_var.h>
118 #include <net/route.h>
119 #include <net/if_dl.h>
120 #include <net/kpi_protocol.h>
122 #include <netinet/in.h>
123 #include <netinet/in_var.h>
124 #include <netinet/if_ether.h>
125 #include <netinet/in_systm.h>
126 #include <netinet/ip.h>
127 #include <netinet/in_pcb.h>
128 #include <netinet/icmp6.h>
129 #include <netinet/tcp.h>
130 #include <netinet/tcp_seq.h>
131 #include <netinet/tcp_var.h>
133 #include <netinet6/nd6.h>
134 #include <netinet/ip6.h>
135 #include <netinet6/ip6_var.h>
136 #include <netinet6/mld6_var.h>
137 #include <netinet6/in6_ifattach.h>
138 #include <netinet6/scope6_var.h>
139 #include <netinet6/in6_var.h>
140 #include <netinet6/in6_pcb.h>
142 #include <net/net_osdep.h>
144 #include <net/dlil.h>
147 #include <net/pfvar.h>
151 * Definitions of some costant IP6 addresses.
153 const struct in6_addr in6addr_any
= IN6ADDR_ANY_INIT
;
154 const struct in6_addr in6addr_loopback
= IN6ADDR_LOOPBACK_INIT
;
155 const struct in6_addr in6addr_nodelocal_allnodes
=
156 IN6ADDR_NODELOCAL_ALLNODES_INIT
;
157 const struct in6_addr in6addr_linklocal_allnodes
=
158 IN6ADDR_LINKLOCAL_ALLNODES_INIT
;
159 const struct in6_addr in6addr_linklocal_allrouters
=
160 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT
;
161 const struct in6_addr in6addr_linklocal_allv2routers
=
162 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT
;
164 const struct in6_addr in6mask0
= IN6MASK0
;
165 const struct in6_addr in6mask7
= IN6MASK7
;
166 const struct in6_addr in6mask16
= IN6MASK16
;
167 const struct in6_addr in6mask32
= IN6MASK32
;
168 const struct in6_addr in6mask64
= IN6MASK64
;
169 const struct in6_addr in6mask96
= IN6MASK96
;
170 const struct in6_addr in6mask128
= IN6MASK128
;
172 const struct sockaddr_in6 sa6_any
= {
173 sizeof (sa6_any
), AF_INET6
, 0, 0, IN6ADDR_ANY_INIT
, 0
176 static int in6ctl_associd(struct socket
*, u_long
, caddr_t
);
177 static int in6ctl_connid(struct socket
*, u_long
, caddr_t
);
178 static int in6ctl_conninfo(struct socket
*, u_long
, caddr_t
);
179 static int in6ctl_llstart(struct ifnet
*, u_long
, caddr_t
);
180 static int in6ctl_llstop(struct ifnet
*);
181 static int in6ctl_cgastart(struct ifnet
*, u_long
, caddr_t
);
182 static int in6ctl_gifaddr(struct ifnet
*, struct in6_ifaddr
*, u_long
,
184 static int in6ctl_gifstat(struct ifnet
*, u_long
, struct in6_ifreq
*);
185 static int in6ctl_alifetime(struct in6_ifaddr
*, u_long
, struct in6_ifreq
*,
187 static int in6ctl_aifaddr(struct ifnet
*, struct in6_aliasreq
*);
188 static void in6ctl_difaddr(struct ifnet
*, struct in6_ifaddr
*);
189 static int in6_autoconf(struct ifnet
*, int);
190 static int in6_setrouter(struct ifnet
*, int);
191 static int in6_ifinit(struct ifnet
*, struct in6_ifaddr
*, int);
192 static int in6_ifaupdate_aux(struct in6_ifaddr
*, struct ifnet
*, int);
193 static void in6_unlink_ifa(struct in6_ifaddr
*, struct ifnet
*);
194 static struct in6_ifaddr
*in6_ifaddr_alloc(int);
195 static void in6_ifaddr_attached(struct ifaddr
*);
196 static void in6_ifaddr_detached(struct ifaddr
*);
197 static void in6_ifaddr_free(struct ifaddr
*);
198 static void in6_ifaddr_trace(struct ifaddr
*, int);
199 #if defined(__LP64__)
200 static void in6_cgareq_32_to_64(struct in6_cgareq_32
*,
201 struct in6_cgareq_64
*);
203 static void in6_cgareq_64_to_32(struct in6_cgareq_64
*,
204 struct in6_cgareq_32
*);
206 static struct in6_aliasreq
*in6_aliasreq_to_native(void *, int,
207 struct in6_aliasreq
*);
208 static struct in6_cgareq
*in6_cgareq_to_native(void *, int,
209 struct in6_cgareq
*);
210 static int in6_to_kamescope(struct sockaddr_in6
*, struct ifnet
*);
211 static int in6_getassocids(struct socket
*, uint32_t *, user_addr_t
);
212 static int in6_getconnids(struct socket
*, sae_associd_t
, uint32_t *,
214 static int in6_getconninfo(struct socket
*, sae_connid_t
, uint32_t *,
215 uint32_t *, int32_t *, user_addr_t
, socklen_t
*, user_addr_t
, socklen_t
*,
216 uint32_t *, user_addr_t
, uint32_t *);
218 static void in6_if_up_dad_start(struct ifnet
*);
220 extern lck_mtx_t
*nd6_mutex
;
222 #define IN6IFA_TRACE_HIST_SIZE 32 /* size of trace history */
225 __private_extern__
unsigned int in6ifa_trace_hist_size
= IN6IFA_TRACE_HIST_SIZE
;
227 struct in6_ifaddr_dbg
{
228 struct in6_ifaddr in6ifa
; /* in6_ifaddr */
229 struct in6_ifaddr in6ifa_old
; /* saved in6_ifaddr */
230 u_int16_t in6ifa_refhold_cnt
; /* # of IFA_ADDREF */
231 u_int16_t in6ifa_refrele_cnt
; /* # of IFA_REMREF */
233 * Alloc and free callers.
235 ctrace_t in6ifa_alloc
;
236 ctrace_t in6ifa_free
;
238 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
240 ctrace_t in6ifa_refhold
[IN6IFA_TRACE_HIST_SIZE
];
241 ctrace_t in6ifa_refrele
[IN6IFA_TRACE_HIST_SIZE
];
245 TAILQ_ENTRY(in6_ifaddr_dbg
) in6ifa_trash_link
;
248 /* List of trash in6_ifaddr entries protected by in6ifa_trash_lock */
249 static TAILQ_HEAD(, in6_ifaddr_dbg
) in6ifa_trash_head
;
250 static decl_lck_mtx_data(, in6ifa_trash_lock
);
253 static unsigned int in6ifa_debug
= 1; /* debugging (enabled) */
255 static unsigned int in6ifa_debug
; /* debugging (disabled) */
257 static unsigned int in6ifa_size
; /* size of zone element */
258 static struct zone
*in6ifa_zone
; /* zone for in6_ifaddr */
260 #define IN6IFA_ZONE_MAX 64 /* maximum elements in zone */
261 #define IN6IFA_ZONE_NAME "in6_ifaddr" /* zone name */
264 * Subroutine for in6_ifaddloop() and in6_ifremloop().
265 * This routine does actual work.
268 in6_ifloop_request(int cmd
, struct ifaddr
*ifa
)
270 struct sockaddr_in6 all1_sa
;
271 struct rtentry
*nrt
= NULL
;
274 bzero(&all1_sa
, sizeof (all1_sa
));
275 all1_sa
.sin6_family
= AF_INET6
;
276 all1_sa
.sin6_len
= sizeof (struct sockaddr_in6
);
277 all1_sa
.sin6_addr
= in6mask128
;
280 * We specify the address itself as the gateway, and set the
281 * RTF_LLINFO flag, so that the corresponding host route would have
282 * the flag, and thus applications that assume traditional behavior
283 * would be happy. Note that we assume the caller of the function
284 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
285 * which changes the outgoing interface to the loopback interface.
286 * ifa_addr for INET6 is set once during init; no need to hold lock.
288 lck_mtx_lock(rnh_lock
);
289 e
= rtrequest_locked(cmd
, ifa
->ifa_addr
, ifa
->ifa_addr
,
290 (struct sockaddr
*)&all1_sa
, RTF_UP
|RTF_HOST
|RTF_LLINFO
, &nrt
);
292 log(LOG_ERR
, "in6_ifloop_request: "
293 "%s operation failed for %s (errno=%d)\n",
294 cmd
== RTM_ADD
? "ADD" : "DELETE",
295 ip6_sprintf(&((struct in6_ifaddr
*)ifa
)->ia_addr
.sin6_addr
),
302 * Make sure rt_ifa be equal to IFA, the second argument of the
304 * We need this because when we refer to rt_ifa->ia6_flags in
305 * ip6_input, we assume that the rt_ifa points to the address instead
306 * of the loopback address.
308 if (cmd
== RTM_ADD
&& nrt
&& ifa
!= nrt
->rt_ifa
) {
313 * Report the addition/removal of the address to the routing socket.
314 * XXX: since we called rtinit for a p2p interface with a destination,
315 * we end up reporting twice in such a case. Should we rather
316 * omit the second report?
319 rt_newaddrmsg(cmd
, ifa
, e
, nrt
);
320 if (cmd
== RTM_DELETE
) {
324 /* the cmd must be RTM_ADD here */
325 RT_REMREF_LOCKED(nrt
);
329 lck_mtx_unlock(rnh_lock
);
333 * Add ownaddr as loopback rtentry. We previously add the route only if
334 * necessary (ex. on a p2p link). However, since we now manage addresses
335 * separately from prefixes, we should always add the route. We can't
336 * rely on the cloning mechanism from the corresponding interface route
340 in6_ifaddloop(struct ifaddr
*ifa
)
345 * If there is no loopback entry, allocate one. ifa_addr for
346 * INET6 is set once during init; no need to hold lock.
348 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
351 if (rt
== NULL
|| (rt
->rt_flags
& RTF_HOST
) == 0 ||
352 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) == 0) {
354 RT_REMREF_LOCKED(rt
);
357 in6_ifloop_request(RTM_ADD
, ifa
);
358 } else if (rt
!= NULL
) {
359 RT_REMREF_LOCKED(rt
);
365 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
369 in6_ifremloop(struct ifaddr
*ifa
)
371 struct in6_ifaddr
*ia
;
376 * Some of BSD variants do not remove cloned routes
377 * from an interface direct route, when removing the direct route
378 * (see comments in net/net_osdep.h). Even for variants that do remove
379 * cloned routes, they could fail to remove the cloned routes when
380 * we handle multple addresses that share a common prefix.
381 * So, we should remove the route corresponding to the deleted address
382 * regardless of the result of in6_is_ifloop_auto().
386 * Delete the entry only if exact one ifa exists. More than one ifa
387 * can exist if we assign a same single address to multiple
388 * (probably p2p) interfaces.
389 * XXX: we should avoid such a configuration in IPv6...
391 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
392 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
393 IFA_LOCK(&ia
->ia_ifa
);
394 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa
), &ia
->ia_addr
.sin6_addr
)) {
397 IFA_UNLOCK(&ia
->ia_ifa
);
401 IFA_UNLOCK(&ia
->ia_ifa
);
403 lck_rw_done(&in6_ifaddr_rwlock
);
407 * Before deleting, check if a corresponding loopbacked host
408 * route surely exists. With this check, we can avoid to
409 * delete an interface direct route whose destination is same
410 * as the address being removed. This can happen when removing
411 * a subnet-router anycast address on an interface attahced
412 * to a shared medium. ifa_addr for INET6 is set once during
413 * init; no need to hold lock.
415 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
418 if ((rt
->rt_flags
& RTF_HOST
) != 0 &&
419 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
420 RT_REMREF_LOCKED(rt
);
422 in6_ifloop_request(RTM_DELETE
, ifa
);
432 in6_mask2len(struct in6_addr
*mask
, u_char
*lim0
)
435 u_char
*lim
= lim0
, *p
;
437 /* ignore the scope_id part */
438 if (lim0
== NULL
|| lim0
- (u_char
*)mask
> sizeof (*mask
))
439 lim
= (u_char
*)mask
+ sizeof (*mask
);
440 for (p
= (u_char
*)mask
; p
< lim
; x
++, p
++) {
446 for (y
= 0; y
< 8; y
++) {
447 if ((*p
& (0x80 >> y
)) == 0)
453 * when the limit pointer is given, do a stricter check on the
457 if (y
!= 0 && (*p
& (0x00ff >> y
)) != 0)
459 for (p
= p
+ 1; p
< lim
; p
++)
468 in6_len2mask(struct in6_addr
*mask
, int len
)
472 bzero(mask
, sizeof (*mask
));
473 for (i
= 0; i
< len
/ 8; i
++)
474 mask
->s6_addr8
[i
] = 0xff;
476 mask
->s6_addr8
[i
] = (0xff00 >> (len
% 8)) & 0xff;
480 in6_aliasreq_64_to_32(struct in6_aliasreq_64
*src
, struct in6_aliasreq_32
*dst
)
482 bzero(dst
, sizeof (*dst
));
483 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof (dst
->ifra_name
));
484 dst
->ifra_addr
= src
->ifra_addr
;
485 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
486 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
487 dst
->ifra_flags
= src
->ifra_flags
;
488 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
489 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
490 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
491 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
495 in6_aliasreq_32_to_64(struct in6_aliasreq_32
*src
, struct in6_aliasreq_64
*dst
)
497 bzero(dst
, sizeof (*dst
));
498 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof (dst
->ifra_name
));
499 dst
->ifra_addr
= src
->ifra_addr
;
500 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
501 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
502 dst
->ifra_flags
= src
->ifra_flags
;
503 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
504 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
505 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
506 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
509 #if defined(__LP64__)
511 in6_cgareq_32_to_64(struct in6_cgareq_32
*src
,
512 struct in6_cgareq_64
*dst
)
514 bzero(dst
, sizeof (*dst
));
515 bcopy(src
->cgar_name
, dst
->cgar_name
, sizeof (dst
->cgar_name
));
516 dst
->cgar_flags
= src
->cgar_flags
;
517 bcopy(src
->cgar_cgaprep
.cga_modifier
.octets
,
518 dst
->cgar_cgaprep
.cga_modifier
.octets
,
519 sizeof (dst
->cgar_cgaprep
.cga_modifier
.octets
));
520 dst
->cgar_cgaprep
.cga_security_level
=
521 src
->cgar_cgaprep
.cga_security_level
;
522 dst
->cgar_lifetime
.ia6t_expire
= src
->cgar_lifetime
.ia6t_expire
;
523 dst
->cgar_lifetime
.ia6t_preferred
= src
->cgar_lifetime
.ia6t_preferred
;
524 dst
->cgar_lifetime
.ia6t_vltime
= src
->cgar_lifetime
.ia6t_vltime
;
525 dst
->cgar_lifetime
.ia6t_pltime
= src
->cgar_lifetime
.ia6t_pltime
;
529 #if !defined(__LP64__)
531 in6_cgareq_64_to_32(struct in6_cgareq_64
*src
,
532 struct in6_cgareq_32
*dst
)
534 bzero(dst
, sizeof (*dst
));
535 bcopy(src
->cgar_name
, dst
->cgar_name
, sizeof (dst
->cgar_name
));
536 dst
->cgar_flags
= src
->cgar_flags
;
537 bcopy(src
->cgar_cgaprep
.cga_modifier
.octets
,
538 dst
->cgar_cgaprep
.cga_modifier
.octets
,
539 sizeof (dst
->cgar_cgaprep
.cga_modifier
.octets
));
540 dst
->cgar_cgaprep
.cga_security_level
=
541 src
->cgar_cgaprep
.cga_security_level
;
542 dst
->cgar_lifetime
.ia6t_expire
= src
->cgar_lifetime
.ia6t_expire
;
543 dst
->cgar_lifetime
.ia6t_preferred
= src
->cgar_lifetime
.ia6t_preferred
;
544 dst
->cgar_lifetime
.ia6t_vltime
= src
->cgar_lifetime
.ia6t_vltime
;
545 dst
->cgar_lifetime
.ia6t_pltime
= src
->cgar_lifetime
.ia6t_pltime
;
549 static struct in6_aliasreq
*
550 in6_aliasreq_to_native(void *data
, int data_is_64
, struct in6_aliasreq
*dst
)
552 #if defined(__LP64__)
554 bcopy(data
, dst
, sizeof (*dst
));
556 in6_aliasreq_32_to_64((struct in6_aliasreq_32
*)data
,
557 (struct in6_aliasreq_64
*)dst
);
560 in6_aliasreq_64_to_32((struct in6_aliasreq_64
*)data
,
561 (struct in6_aliasreq_32
*)dst
);
563 bcopy(data
, dst
, sizeof (*dst
));
564 #endif /* __LP64__ */
568 static struct in6_cgareq
*
569 in6_cgareq_to_native(void *data
, int is64
, struct in6_cgareq
*dst
)
571 #if defined(__LP64__)
573 bcopy(data
, dst
, sizeof (*dst
));
575 in6_cgareq_32_to_64((struct in6_cgareq_32
*)data
,
576 (struct in6_cgareq_64
*)dst
);
579 in6_cgareq_64_to_32((struct in6_cgareq_64
*)data
,
580 (struct in6_cgareq_32
*)dst
);
582 bcopy(data
, dst
, sizeof (*dst
));
583 #endif /* __LP64__ */
587 static __attribute__((noinline
)) int
588 in6ctl_associd(struct socket
*so
, u_long cmd
, caddr_t data
)
592 struct so_aidreq32 a32
;
593 struct so_aidreq64 a64
;
599 case SIOCGASSOCIDS32
: { /* struct so_aidreq32 */
600 bcopy(data
, &u
.a32
, sizeof (u
.a32
));
601 error
= in6_getassocids(so
, &u
.a32
.sar_cnt
, u
.a32
.sar_aidp
);
603 bcopy(&u
.a32
, data
, sizeof (u
.a32
));
607 case SIOCGASSOCIDS64
: { /* struct so_aidreq64 */
608 bcopy(data
, &u
.a64
, sizeof (u
.a64
));
609 error
= in6_getassocids(so
, &u
.a64
.sar_cnt
, u
.a64
.sar_aidp
);
611 bcopy(&u
.a64
, data
, sizeof (u
.a64
));
623 static __attribute__((noinline
)) int
624 in6ctl_connid(struct socket
*so
, u_long cmd
, caddr_t data
)
628 struct so_cidreq32 c32
;
629 struct so_cidreq64 c64
;
635 case SIOCGCONNIDS32
: { /* struct so_cidreq32 */
636 bcopy(data
, &u
.c32
, sizeof (u
.c32
));
637 error
= in6_getconnids(so
, u
.c32
.scr_aid
, &u
.c32
.scr_cnt
,
640 bcopy(&u
.c32
, data
, sizeof (u
.c32
));
644 case SIOCGCONNIDS64
: { /* struct so_cidreq64 */
645 bcopy(data
, &u
.c64
, sizeof (u
.c64
));
646 error
= in6_getconnids(so
, u
.c64
.scr_aid
, &u
.c64
.scr_cnt
,
649 bcopy(&u
.c64
, data
, sizeof (u
.c64
));
661 static __attribute__((noinline
)) int
662 in6ctl_conninfo(struct socket
*so
, u_long cmd
, caddr_t data
)
666 struct so_cinforeq32 ci32
;
667 struct so_cinforeq64 ci64
;
673 case SIOCGCONNINFO32
: { /* struct so_cinforeq32 */
674 bcopy(data
, &u
.ci32
, sizeof (u
.ci32
));
675 error
= in6_getconninfo(so
, u
.ci32
.scir_cid
, &u
.ci32
.scir_flags
,
676 &u
.ci32
.scir_ifindex
, &u
.ci32
.scir_error
, u
.ci32
.scir_src
,
677 &u
.ci32
.scir_src_len
, u
.ci32
.scir_dst
, &u
.ci32
.scir_dst_len
,
678 &u
.ci32
.scir_aux_type
, u
.ci32
.scir_aux_data
,
679 &u
.ci32
.scir_aux_len
);
681 bcopy(&u
.ci32
, data
, sizeof (u
.ci32
));
685 case SIOCGCONNINFO64
: { /* struct so_cinforeq64 */
686 bcopy(data
, &u
.ci64
, sizeof (u
.ci64
));
687 error
= in6_getconninfo(so
, u
.ci64
.scir_cid
, &u
.ci64
.scir_flags
,
688 &u
.ci64
.scir_ifindex
, &u
.ci64
.scir_error
, u
.ci64
.scir_src
,
689 &u
.ci64
.scir_src_len
, u
.ci64
.scir_dst
, &u
.ci64
.scir_dst_len
,
690 &u
.ci64
.scir_aux_type
, u
.ci64
.scir_aux_data
,
691 &u
.ci64
.scir_aux_len
);
693 bcopy(&u
.ci64
, data
, sizeof (u
.ci64
));
705 static __attribute__((noinline
)) int
706 in6ctl_llstart(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
708 struct in6_aliasreq sifra
, *ifra
= NULL
;
715 case SIOCLL_START_32
: /* struct in6_aliasreq_32 */
716 case SIOCLL_START_64
: /* struct in6_aliasreq_64 */
717 is64
= (cmd
== SIOCLL_START_64
);
719 * Convert user ifra to the kernel form, when appropriate.
720 * This allows the conversion between different data models
721 * to be centralized, so that it can be passed around to other
722 * routines that are expecting the kernel form.
724 ifra
= in6_aliasreq_to_native(data
, is64
, &sifra
);
727 * NOTE: All the interface specific DLIL attachements should
728 * be done here. They are currently done in in6_ifattach_aux()
729 * for the interfaces that need it.
731 if (ifra
->ifra_addr
.sin6_family
== AF_INET6
&&
732 /* Only check ifra_dstaddr if valid */
733 (ifra
->ifra_dstaddr
.sin6_len
== 0 ||
734 ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
)) {
735 /* some interfaces may provide LinkLocal addresses */
736 error
= in6_ifattach_aliasreq(ifp
, NULL
, ifra
);
738 error
= in6_ifattach_aliasreq(ifp
, NULL
, NULL
);
741 in6_if_up_dad_start(ifp
);
752 static __attribute__((noinline
)) int
753 in6ctl_llstop(struct ifnet
*ifp
)
755 struct in6_ifaddr
*ia
;
756 struct nd_prefix pr0
, *pr
;
760 /* Remove link local addresses from interface */
761 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
764 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
768 IFA_LOCK(&ia
->ia_ifa
);
769 if (IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
)) {
770 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
771 IFA_UNLOCK(&ia
->ia_ifa
);
772 lck_rw_done(&in6_ifaddr_rwlock
);
773 in6_purgeaddr(&ia
->ia_ifa
);
774 IFA_REMREF(&ia
->ia_ifa
); /* for us */
775 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
777 * Purging the address caused in6_ifaddr_rwlock
778 * to be dropped and reacquired;
779 * therefore search again from the beginning
780 * of in6_ifaddrs list.
785 IFA_UNLOCK(&ia
->ia_ifa
);
788 lck_rw_done(&in6_ifaddr_rwlock
);
790 /* Delete the link local prefix */
791 bzero(&pr0
, sizeof(pr0
));
794 pr0
.ndpr_prefix
.sin6_addr
.s6_addr16
[0] = IPV6_ADDR_INT16_ULL
;
795 in6_setscope(&pr0
.ndpr_prefix
.sin6_addr
, ifp
, NULL
);
796 pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_UNSPEC
);
798 lck_mtx_lock(nd6_mutex
);
802 NDPR_REMREF(pr
); /* Drop the reference from lookup */
803 lck_mtx_unlock(nd6_mutex
);
810 * This routine configures secure link local address
812 static __attribute__((noinline
)) int
813 in6ctl_cgastart(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
815 struct in6_cgareq llcgasr
;
821 case SIOCLL_CGASTART_32
: /* struct in6_cgareq_32 */
822 case SIOCLL_CGASTART_64
: /* struct in6_cgareq_64 */
823 is64
= (cmd
== SIOCLL_CGASTART_64
);
825 * Convert user cgareq to the kernel form, when appropriate.
826 * This allows the conversion between different data models
827 * to be centralized, so that it can be passed around to other
828 * routines that are expecting the kernel form.
830 in6_cgareq_to_native(data
, is64
, &llcgasr
);
833 * NOTE: All the interface specific DLIL attachements
834 * should be done here. They are currently done in
835 * in6_ifattach_cgareq() for the interfaces that
838 error
= in6_ifattach_llcgareq(ifp
, &llcgasr
);
840 in6_if_up_dad_start(ifp
);
852 * Caller passes in the ioctl data pointer directly via "ifr", with the
853 * expectation that this routine always uses bcopy() or other byte-aligned
856 static __attribute__((noinline
)) int
857 in6ctl_gifaddr(struct ifnet
*ifp
, struct in6_ifaddr
*ia
, u_long cmd
,
858 struct in6_ifreq
*ifr
)
860 struct sockaddr_in6 addr
;
866 return (EADDRNOTAVAIL
);
869 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
870 IFA_LOCK(&ia
->ia_ifa
);
871 bcopy(&ia
->ia_addr
, &addr
, sizeof (addr
));
872 IFA_UNLOCK(&ia
->ia_ifa
);
873 if ((error
= sa6_recoverscope(&addr
, TRUE
)) != 0)
875 bcopy(&addr
, &ifr
->ifr_addr
, sizeof (addr
));
878 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
879 if (!(ifp
->if_flags
& IFF_POINTOPOINT
)) {
884 * XXX: should we check if ifa_dstaddr is NULL and return
887 IFA_LOCK(&ia
->ia_ifa
);
888 bcopy(&ia
->ia_dstaddr
, &addr
, sizeof (addr
));
889 IFA_UNLOCK(&ia
->ia_ifa
);
890 if ((error
= sa6_recoverscope(&addr
, TRUE
)) != 0)
892 bcopy(&addr
, &ifr
->ifr_dstaddr
, sizeof (addr
));
904 * Caller passes in the ioctl data pointer directly via "ifr", with the
905 * expectation that this routine always uses bcopy() or other byte-aligned
908 static __attribute__((noinline
)) int
909 in6ctl_gifstat(struct ifnet
*ifp
, u_long cmd
, struct in6_ifreq
*ifr
)
911 int error
= 0, index
;
914 index
= ifp
->if_index
;
917 case SIOCGIFSTAT_IN6
: /* struct in6_ifreq */
918 /* N.B.: if_inet6data is never freed once set. */
919 if (IN6_IFEXTRA(ifp
) == NULL
) {
920 /* return (EAFNOSUPPORT)? */
921 bzero(&ifr
->ifr_ifru
.ifru_stat
,
922 sizeof (ifr
->ifr_ifru
.ifru_stat
));
924 bcopy(&IN6_IFEXTRA(ifp
)->in6_ifstat
,
925 &ifr
->ifr_ifru
.ifru_stat
,
926 sizeof (ifr
->ifr_ifru
.ifru_stat
));
930 case SIOCGIFSTAT_ICMP6
: /* struct in6_ifreq */
931 /* N.B.: if_inet6data is never freed once set. */
932 if (IN6_IFEXTRA(ifp
) == NULL
) {
933 /* return (EAFNOSUPPORT)? */
934 bzero(&ifr
->ifr_ifru
.ifru_stat
,
935 sizeof (ifr
->ifr_ifru
.ifru_icmp6stat
));
937 bcopy(&IN6_IFEXTRA(ifp
)->icmp6_ifstat
,
938 &ifr
->ifr_ifru
.ifru_icmp6stat
,
939 sizeof (ifr
->ifr_ifru
.ifru_icmp6stat
));
952 * Caller passes in the ioctl data pointer directly via "ifr", with the
953 * expectation that this routine always uses bcopy() or other byte-aligned
956 static __attribute__((noinline
)) int
957 in6ctl_alifetime(struct in6_ifaddr
*ia
, u_long cmd
, struct in6_ifreq
*ifr
,
960 uint64_t timenow
= net_uptime();
961 struct in6_addrlifetime ia6_lt
;
962 struct timeval caltime
;
966 return (EADDRNOTAVAIL
);
969 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
970 IFA_LOCK(&ia
->ia_ifa
);
971 /* retrieve time as calendar time (last arg is 1) */
972 in6ifa_getlifetime(ia
, &ia6_lt
, 1);
974 struct in6_addrlifetime_64 lt
;
976 bzero(<
, sizeof (lt
));
977 lt
.ia6t_expire
= ia6_lt
.ia6t_expire
;
978 lt
.ia6t_preferred
= ia6_lt
.ia6t_preferred
;
979 lt
.ia6t_vltime
= ia6_lt
.ia6t_vltime
;
980 lt
.ia6t_pltime
= ia6_lt
.ia6t_pltime
;
981 bcopy(<
, &ifr
->ifr_ifru
.ifru_lifetime
, sizeof (lt
));
983 struct in6_addrlifetime_32 lt
;
985 bzero(<
, sizeof (lt
));
986 lt
.ia6t_expire
= (uint32_t)ia6_lt
.ia6t_expire
;
987 lt
.ia6t_preferred
= (uint32_t)ia6_lt
.ia6t_preferred
;
988 lt
.ia6t_vltime
= (uint32_t)ia6_lt
.ia6t_vltime
;
989 lt
.ia6t_pltime
= (uint32_t)ia6_lt
.ia6t_pltime
;
990 bcopy(<
, &ifr
->ifr_ifru
.ifru_lifetime
, sizeof (lt
));
992 IFA_UNLOCK(&ia
->ia_ifa
);
995 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
996 getmicrotime(&caltime
);
998 /* sanity for overflow - beware unsigned */
1000 struct in6_addrlifetime_64 lt
;
1002 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1003 if (lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
&&
1004 lt
.ia6t_vltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1008 if (lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
&&
1009 lt
.ia6t_pltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1014 struct in6_addrlifetime_32 lt
;
1016 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1017 if (lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
&&
1018 lt
.ia6t_vltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1022 if (lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
&&
1023 lt
.ia6t_pltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1029 IFA_LOCK(&ia
->ia_ifa
);
1031 struct in6_addrlifetime_64 lt
;
1033 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1034 ia6_lt
.ia6t_expire
= lt
.ia6t_expire
;
1035 ia6_lt
.ia6t_preferred
= lt
.ia6t_preferred
;
1036 ia6_lt
.ia6t_vltime
= lt
.ia6t_vltime
;
1037 ia6_lt
.ia6t_pltime
= lt
.ia6t_pltime
;
1039 struct in6_addrlifetime_32 lt
;
1041 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1042 ia6_lt
.ia6t_expire
= (uint32_t)lt
.ia6t_expire
;
1043 ia6_lt
.ia6t_preferred
= (uint32_t)lt
.ia6t_preferred
;
1044 ia6_lt
.ia6t_vltime
= lt
.ia6t_vltime
;
1045 ia6_lt
.ia6t_pltime
= lt
.ia6t_pltime
;
1048 if (ia6_lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
)
1049 ia6_lt
.ia6t_expire
= timenow
+ ia6_lt
.ia6t_vltime
;
1051 ia6_lt
.ia6t_expire
= 0;
1053 if (ia6_lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
)
1054 ia6_lt
.ia6t_preferred
= timenow
+ ia6_lt
.ia6t_pltime
;
1056 ia6_lt
.ia6t_preferred
= 0;
1058 in6ifa_setlifetime(ia
, &ia6_lt
);
1059 IFA_UNLOCK(&ia
->ia_ifa
);
1070 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(void *)(ifa))
1073 * Generic INET6 control operations (ioctl's).
1075 * ifp is NULL if not an interface-specific ioctl.
1077 * Most of the routines called to handle the ioctls would end up being
1078 * tail-call optimized, which unfortunately causes this routine to
1079 * consume too much stack space; this is the reason for the "noinline"
1080 * attribute used on those routines.
1082 * If called directly from within the networking stack (as opposed to via
1083 * pru_control), the socket parameter may be NULL.
1086 in6_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
1089 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
1090 struct in6_aliasreq sifra
, *ifra
= NULL
;
1091 struct in6_ifaddr
*ia
= NULL
;
1092 struct sockaddr_in6 sin6
, *sa6
= NULL
;
1093 boolean_t privileged
= (proc_suser(p
) == 0);
1094 boolean_t p64
= proc_is64bit(p
);
1095 boolean_t so_unlocked
= FALSE
;
1096 int intval
, error
= 0;
1098 /* In case it's NULL, make sure it came from the kernel */
1099 VERIFY(so
!= NULL
|| p
== kernproc
);
1102 * ioctls which don't require ifp, may require socket.
1105 case SIOCAADDRCTL_POLICY
: /* struct in6_addrpolicy */
1106 case SIOCDADDRCTL_POLICY
: /* struct in6_addrpolicy */
1109 return (in6_src_ioctl(cmd
, data
));
1112 case SIOCDRADD_IN6_32
: /* struct in6_defrouter_32 */
1113 case SIOCDRADD_IN6_64
: /* struct in6_defrouter_64 */
1114 case SIOCDRDEL_IN6_32
: /* struct in6_defrouter_32 */
1115 case SIOCDRDEL_IN6_64
: /* struct in6_defrouter_64 */
1118 return (defrtrlist_ioctl(cmd
, data
));
1121 case SIOCGASSOCIDS32
: /* struct so_aidreq32 */
1122 case SIOCGASSOCIDS64
: /* struct so_aidreq64 */
1123 return (in6ctl_associd(so
, cmd
, data
));
1126 case SIOCGCONNIDS32
: /* struct so_cidreq32 */
1127 case SIOCGCONNIDS64
: /* struct so_cidreq64 */
1128 return (in6ctl_connid(so
, cmd
, data
));
1131 case SIOCGCONNINFO32
: /* struct so_cinforeq32 */
1132 case SIOCGCONNINFO64
: /* struct so_cinforeq64 */
1133 return (in6ctl_conninfo(so
, cmd
, data
));
1138 * The rest of ioctls require ifp; reject if we don't have one;
1139 * return ENXIO to be consistent with ifioctl().
1145 * Unlock the socket since ifnet_ioctl() may be invoked by
1146 * one of the ioctl handlers below. Socket will be re-locked
1147 * prior to returning.
1150 socket_unlock(so
, 0);
1155 * ioctls which require ifp but not interface address.
1158 case SIOCAUTOCONF_START
: /* struct in6_ifreq */
1163 error
= in6_autoconf(ifp
, TRUE
);
1166 case SIOCAUTOCONF_STOP
: /* struct in6_ifreq */
1171 error
= in6_autoconf(ifp
, FALSE
);
1174 case SIOCLL_START_32
: /* struct in6_aliasreq_32 */
1175 case SIOCLL_START_64
: /* struct in6_aliasreq_64 */
1180 error
= in6ctl_llstart(ifp
, cmd
, data
);
1183 case SIOCLL_STOP
: /* struct in6_ifreq */
1188 error
= in6ctl_llstop(ifp
);
1191 case SIOCSETROUTERMODE_IN6
: /* struct in6_ifreq */
1196 bcopy(&((struct in6_ifreq
*)(void *)data
)->ifr_intval
,
1197 &intval
, sizeof (intval
));
1199 error
= in6_setrouter(ifp
, intval
);
1202 case SIOCPROTOATTACH_IN6_32
: /* struct in6_aliasreq_32 */
1203 case SIOCPROTOATTACH_IN6_64
: /* struct in6_aliasreq_64 */
1208 error
= in6_domifattach(ifp
);
1211 case SIOCPROTODETACH_IN6
: /* struct in6_ifreq */
1216 /* Cleanup interface routes and addresses */
1219 if ((error
= proto_unplumb(PF_INET6
, ifp
)))
1220 log(LOG_ERR
, "SIOCPROTODETACH_IN6: %s error=%d\n",
1221 if_name(ifp
), error
);
1224 case SIOCSNDFLUSH_IN6
: /* struct in6_ifreq */
1225 case SIOCSPFXFLUSH_IN6
: /* struct in6_ifreq */
1226 case SIOCSRTRFLUSH_IN6
: /* struct in6_ifreq */
1227 case SIOCSDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
1228 case SIOCSDEFIFACE_IN6_64
: /* struct in6_ndifreq_64 */
1229 case SIOCSIFINFO_FLAGS
: /* struct in6_ndireq */
1230 case SIOCGIFCGAPREP_IN6
: /* struct in6_ifreq */
1231 case SIOCSIFCGAPREP_IN6
: /* struct in6_ifreq */
1237 case OSIOCGIFINFO_IN6
: /* struct in6_ondireq */
1238 case SIOCGIFINFO_IN6
: /* struct in6_ondireq */
1239 case SIOCGDRLST_IN6_32
: /* struct in6_drlist_32 */
1240 case SIOCGDRLST_IN6_64
: /* struct in6_drlist_64 */
1241 case SIOCGPRLST_IN6_32
: /* struct in6_prlist_32 */
1242 case SIOCGPRLST_IN6_64
: /* struct in6_prlist_64 */
1243 case SIOCGNBRINFO_IN6_32
: /* struct in6_nbrinfo_32 */
1244 case SIOCGNBRINFO_IN6_64
: /* struct in6_nbrinfo_64 */
1245 case SIOCGDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
1246 case SIOCGDEFIFACE_IN6_64
: /* struct in6_ndifreq_64 */
1247 error
= nd6_ioctl(cmd
, data
, ifp
);
1250 case SIOCSIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1251 case SIOCDIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1252 case SIOCAIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1253 case SIOCCIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1254 case SIOCSGIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1255 case SIOCGIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1257 "prefix ioctls are now invalidated. "
1258 "please use ifconfig.\n");
1262 case SIOCSSCOPE6
: /* struct in6_ifreq (deprecated) */
1263 case SIOCGSCOPE6
: /* struct in6_ifreq (deprecated) */
1264 case SIOCGSCOPE6DEF
: /* struct in6_ifreq (deprecated) */
1268 case SIOCLL_CGASTART_32
: /* struct in6_cgareq_32 */
1269 case SIOCLL_CGASTART_64
: /* struct in6_cgareq_64 */
1273 error
= in6ctl_cgastart(ifp
, cmd
, data
);
1276 case SIOCGIFSTAT_IN6
: /* struct in6_ifreq */
1277 case SIOCGIFSTAT_ICMP6
: /* struct in6_ifreq */
1278 error
= in6ctl_gifstat(ifp
, cmd
, ifr
);
1283 * ioctls which require interface address; obtain sockaddr_in6.
1286 case SIOCSIFADDR_IN6
: /* struct in6_ifreq (deprecated) */
1287 case SIOCSIFDSTADDR_IN6
: /* struct in6_ifreq (deprecated) */
1288 case SIOCSIFNETMASK_IN6
: /* struct in6_ifreq (deprecated) */
1290 * Since IPv6 allows a node to assign multiple addresses
1291 * on a single interface, SIOCSIFxxx ioctls are deprecated.
1293 /* we decided to obsolete this command (20000704) */
1297 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1298 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1304 * Convert user ifra to the kernel form, when appropriate.
1305 * This allows the conversion between different data models
1306 * to be centralized, so that it can be passed around to other
1307 * routines that are expecting the kernel form.
1309 ifra
= in6_aliasreq_to_native(data
,
1310 (cmd
== SIOCAIFADDR_IN6_64
), &sifra
);
1311 bcopy(&ifra
->ifra_addr
, &sin6
, sizeof (sin6
));
1315 case SIOCDIFADDR_IN6
: /* struct in6_ifreq */
1316 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
1322 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
1323 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
1324 case SIOCGIFNETMASK_IN6
: /* struct in6_ifreq */
1325 case SIOCGIFAFLAG_IN6
: /* struct in6_ifreq */
1326 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
1327 bcopy(&ifr
->ifr_addr
, &sin6
, sizeof (sin6
));
1333 * Find address for this interface, if it exists.
1335 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
1336 * only, and used the first interface address as the target of other
1337 * operations (without checking ifra_addr). This was because netinet
1338 * code/API assumed at most 1 interface address per interface.
1339 * Since IPv6 allows a node to assign multiple addresses
1340 * on a single interface, we almost always look and check the
1341 * presence of ifra_addr, and reject invalid ones here.
1342 * It also decreases duplicated code among SIOC*_IN6 operations.
1345 if (sa6
!= NULL
&& sa6
->sin6_family
== AF_INET6
) {
1346 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
1347 if (sa6
->sin6_addr
.s6_addr16
[1] == 0) {
1348 /* link ID is not embedded by the user */
1349 sa6
->sin6_addr
.s6_addr16
[1] =
1350 htons(ifp
->if_index
);
1351 } else if (sa6
->sin6_addr
.s6_addr16
[1] !=
1352 htons(ifp
->if_index
)) {
1353 error
= EINVAL
; /* link ID contradicts */
1356 if (sa6
->sin6_scope_id
) {
1357 if (sa6
->sin6_scope_id
!=
1358 (u_int32_t
)ifp
->if_index
) {
1362 sa6
->sin6_scope_id
= 0; /* XXX: good way? */
1366 * Any failures from this point on must take into account
1367 * a non-NULL "ia" with an outstanding reference count, and
1368 * therefore requires IFA_REMREF. Jump to "done" label
1369 * instead of calling return if "ia" is valid.
1371 ia
= in6ifa_ifpwithaddr(ifp
, &sa6
->sin6_addr
);
1375 * SIOCDIFADDR_IN6/SIOCAIFADDR_IN6 specific tests.
1378 case SIOCDIFADDR_IN6
: /* struct in6_ifreq */
1380 error
= EADDRNOTAVAIL
;
1384 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1385 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1386 VERIFY(sa6
!= NULL
);
1388 * We always require users to specify a valid IPv6 address for
1389 * the corresponding operation. Use "sa6" instead of "ifra"
1390 * since SIOCDIFADDR_IN6 falls thru above.
1392 if (sa6
->sin6_family
!= AF_INET6
||
1393 sa6
->sin6_len
!= sizeof (struct sockaddr_in6
)) {
1394 error
= EAFNOSUPPORT
;
1401 * And finally process address-related ioctls.
1404 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
1405 /* This interface is basically deprecated. use SIOCGIFCONF. */
1407 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
1408 error
= in6ctl_gifaddr(ifp
, ia
, cmd
, ifr
);
1411 case SIOCGIFNETMASK_IN6
: /* struct in6_ifreq */
1413 IFA_LOCK(&ia
->ia_ifa
);
1414 bcopy(&ia
->ia_prefixmask
, &ifr
->ifr_addr
,
1415 sizeof (struct sockaddr_in6
));
1416 IFA_UNLOCK(&ia
->ia_ifa
);
1418 error
= EADDRNOTAVAIL
;
1422 case SIOCGIFAFLAG_IN6
: /* struct in6_ifreq */
1424 IFA_LOCK(&ia
->ia_ifa
);
1425 bcopy(&ia
->ia6_flags
, &ifr
->ifr_ifru
.ifru_flags6
,
1426 sizeof (ifr
->ifr_ifru
.ifru_flags6
));
1427 IFA_UNLOCK(&ia
->ia_ifa
);
1429 error
= EADDRNOTAVAIL
;
1433 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
1434 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
1435 error
= in6ctl_alifetime(ia
, cmd
, ifr
, p64
);
1438 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1439 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1440 error
= in6ctl_aifaddr(ifp
, ifra
);
1443 case SIOCDIFADDR_IN6
:
1444 in6ctl_difaddr(ifp
, ia
);
1448 error
= ifnet_ioctl(ifp
, PF_INET6
, cmd
, data
);
1454 IFA_REMREF(&ia
->ia_ifa
);
1461 static __attribute__((noinline
)) int
1462 in6ctl_aifaddr(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
)
1464 int i
, error
, addtmp
, plen
;
1465 struct nd_prefix pr0
, *pr
;
1466 struct in6_ifaddr
*ia
;
1468 VERIFY(ifp
!= NULL
&& ifra
!= NULL
);
1471 /* Attempt to attach the protocol, in case it isn't attached */
1472 error
= in6_domifattach(ifp
);
1474 /* PF_INET6 wasn't previously attached */
1475 error
= in6_ifattach_aliasreq(ifp
, NULL
, NULL
);
1479 in6_if_up_dad_start(ifp
);
1480 } else if (error
!= EEXIST
) {
1485 * First, make or update the interface address structure, and link it
1488 error
= in6_update_ifa(ifp
, ifra
, 0, &ia
);
1493 /* Now, make the prefix on-link on the interface. */
1494 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
, NULL
);
1499 * NOTE: We'd rather create the prefix before the address, but we need
1500 * at least one address to install the corresponding interface route,
1501 * so we configure the address first.
1505 * Convert mask to prefix length (prefixmask has already been validated
1506 * in in6_update_ifa().
1508 bzero(&pr0
, sizeof (pr0
));
1509 pr0
.ndpr_plen
= plen
;
1511 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
1512 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
1514 /* apply the mask for safety. */
1515 for (i
= 0; i
< 4; i
++) {
1516 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1517 ifra
->ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
1521 * Since we don't have an API to set prefix (not address) lifetimes, we
1522 * just use the same lifetimes as addresses. The (temporarily)
1523 * installed lifetimes can be overridden by later advertised RAs (when
1524 * accept_rtadv is non 0), which is an intended behavior.
1526 pr0
.ndpr_raf_onlink
= 1; /* should be configurable? */
1527 pr0
.ndpr_raf_auto
= !!(ifra
->ifra_flags
& IN6_IFF_AUTOCONF
);
1528 pr0
.ndpr_vltime
= ifra
->ifra_lifetime
.ia6t_vltime
;
1529 pr0
.ndpr_pltime
= ifra
->ifra_lifetime
.ia6t_pltime
;
1530 pr0
.ndpr_stateflags
|= NDPRF_STATIC
;
1531 lck_mtx_init(&pr0
.ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
1533 /* add the prefix if there's none. */
1534 if ((pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_NEVER
)) == NULL
) {
1536 * nd6_prelist_add will install the corresponding interface
1539 error
= nd6_prelist_add(&pr0
, NULL
, &pr
, FALSE
);
1544 log(LOG_ERR
, "%s: nd6_prelist_add okay, but"
1545 " no prefix.\n", __func__
);
1551 IFA_LOCK(&ia
->ia_ifa
);
1553 /* if this is a new autoconfed addr */
1555 if (ia
->ia6_ndpr
== NULL
) {
1558 VERIFY(pr
->ndpr_addrcnt
!= 0);
1560 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
1563 * If this is the first autoconf address from the prefix,
1564 * create a temporary address as well (when specified).
1566 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
1568 pr
->ndpr_addrcnt
== 1) {
1574 IFA_UNLOCK(&ia
->ia_ifa
);
1578 e
= in6_tmpifadd(ia
, 1);
1580 log(LOG_NOTICE
, "%s: failed to create a"
1581 " temporary address, error=%d\n",
1586 * This might affect the status of autoconfigured addresses, that is,
1587 * this address might make other addresses detached.
1589 lck_mtx_lock(nd6_mutex
);
1590 pfxlist_onlink_check();
1591 lck_mtx_unlock(nd6_mutex
);
1593 /* Drop use count held above during lookup/add */
1598 IFA_REMREF(&ia
->ia_ifa
);
1602 static __attribute__((noinline
)) void
1603 in6ctl_difaddr(struct ifnet
*ifp
, struct in6_ifaddr
*ia
)
1606 struct nd_prefix pr0
, *pr
;
1608 VERIFY(ifp
!= NULL
&& ia
!= NULL
);
1611 * If the address being deleted is the only one that owns
1612 * the corresponding prefix, expire the prefix as well.
1613 * XXX: theoretically, we don't have to worry about such
1614 * relationship, since we separate the address management
1615 * and the prefix management. We do this, however, to provide
1616 * as much backward compatibility as possible in terms of
1617 * the ioctl operation.
1618 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
1620 IFA_LOCK(&ia
->ia_ifa
);
1621 bzero(&pr0
, sizeof (pr0
));
1623 pr0
.ndpr_plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
);
1624 if (pr0
.ndpr_plen
== 128) {
1625 IFA_UNLOCK(&ia
->ia_ifa
);
1628 pr0
.ndpr_prefix
= ia
->ia_addr
;
1629 pr0
.ndpr_mask
= ia
->ia_prefixmask
.sin6_addr
;
1630 for (i
= 0; i
< 4; i
++) {
1631 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1632 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
];
1634 IFA_UNLOCK(&ia
->ia_ifa
);
1636 if ((pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_UNSPEC
)) != NULL
) {
1637 IFA_LOCK(&ia
->ia_ifa
);
1639 if (pr
->ndpr_addrcnt
== 1) {
1640 /* XXX: just for expiration */
1641 pr
->ndpr_expire
= 1;
1644 IFA_UNLOCK(&ia
->ia_ifa
);
1646 /* Drop use count held above during lookup */
1651 in6_purgeaddr(&ia
->ia_ifa
);
1654 static __attribute__((noinline
)) int
1655 in6_autoconf(struct ifnet
*ifp
, int enable
)
1659 VERIFY(ifp
!= NULL
);
1661 if (ifp
->if_flags
& IFF_LOOPBACK
)
1666 * An interface in IPv6 router mode implies that it
1667 * is either configured with a static IP address or
1668 * autoconfigured via a locally-generated RA. Prevent
1669 * SIOCAUTOCONF_START from being set in that mode.
1671 ifnet_lock_exclusive(ifp
);
1672 if (ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
1673 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADV
;
1676 ifp
->if_eflags
|= IFEF_ACCEPT_RTADV
;
1678 ifnet_lock_done(ifp
);
1680 struct in6_ifaddr
*ia
= NULL
;
1682 ifnet_lock_exclusive(ifp
);
1683 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADV
;
1684 ifnet_lock_done(ifp
);
1686 /* Remove autoconfigured address from interface */
1687 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1689 while (ia
!= NULL
) {
1690 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
1694 IFA_LOCK(&ia
->ia_ifa
);
1695 if (ia
->ia6_flags
& IN6_IFF_AUTOCONF
) {
1696 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
1697 IFA_UNLOCK(&ia
->ia_ifa
);
1698 lck_rw_done(&in6_ifaddr_rwlock
);
1699 in6_purgeaddr(&ia
->ia_ifa
);
1700 IFA_REMREF(&ia
->ia_ifa
); /* for us */
1701 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1703 * Purging the address caused in6_ifaddr_rwlock
1704 * to be dropped and reacquired;
1705 * therefore search again from the beginning
1706 * of in6_ifaddrs list.
1711 IFA_UNLOCK(&ia
->ia_ifa
);
1714 lck_rw_done(&in6_ifaddr_rwlock
);
1720 * Handle SIOCSETROUTERMODE_IN6 to set or clear the IPv6 router mode flag on
1721 * the interface. Entering or exiting this mode will result in the removal of
1722 * autoconfigured IPv6 addresses on the interface.
1724 static __attribute__((noinline
)) int
1725 in6_setrouter(struct ifnet
*ifp
, int enable
)
1727 VERIFY(ifp
!= NULL
);
1729 if (ifp
->if_flags
& IFF_LOOPBACK
)
1733 struct nd_ifinfo
*ndi
= NULL
;
1735 ndi
= ND_IFINFO(ifp
);
1736 if (ndi
!= NULL
&& ndi
->initialized
) {
1737 lck_mtx_lock(&ndi
->lock
);
1738 if (ndi
->flags
& ND6_IFF_PROXY_PREFIXES
) {
1739 /* No proxy if we are an advertising router */
1740 ndi
->flags
&= ~ND6_IFF_PROXY_PREFIXES
;
1741 lck_mtx_unlock(&ndi
->lock
);
1742 (void) nd6_if_prproxy(ifp
, FALSE
);
1744 lck_mtx_unlock(&ndi
->lock
);
1749 ifnet_lock_exclusive(ifp
);
1751 ifp
->if_eflags
|= IFEF_IPV6_ROUTER
;
1753 ifp
->if_eflags
&= ~IFEF_IPV6_ROUTER
;
1755 ifnet_lock_done(ifp
);
1757 lck_mtx_lock(nd6_mutex
);
1758 defrouter_select(ifp
);
1759 lck_mtx_unlock(nd6_mutex
);
1761 if_allmulti(ifp
, enable
);
1763 return (in6_autoconf(ifp
, FALSE
));
1767 in6_to_kamescope(struct sockaddr_in6
*sin6
, struct ifnet
*ifp
)
1769 struct sockaddr_in6 tmp
;
1772 VERIFY(sin6
!= NULL
);
1775 error
= in6_recoverscope(&tmp
, &sin6
->sin6_addr
, ifp
);
1779 id
= in6_addr2scopeid(ifp
, &tmp
.sin6_addr
);
1780 if (tmp
.sin6_scope_id
== 0)
1781 tmp
.sin6_scope_id
= id
;
1782 else if (tmp
.sin6_scope_id
!= id
)
1783 return (EINVAL
); /* scope ID mismatch. */
1785 error
= in6_embedscope(&tmp
.sin6_addr
, &tmp
, NULL
, NULL
, NULL
);
1789 tmp
.sin6_scope_id
= 0;
1795 in6_ifaupdate_aux(struct in6_ifaddr
*ia
, struct ifnet
*ifp
, int ifaupflags
)
1797 struct sockaddr_in6 mltaddr
, mltmask
;
1798 struct in6_addr llsol
;
1800 struct in6_multi
*in6m_sol
;
1801 struct in6_multi_mship
*imm
;
1803 int delay
, error
= 0;
1805 VERIFY(ifp
!= NULL
&& ia
!= NULL
);
1809 nd6log2((LOG_DEBUG
, "%s - %s ifp %s ia6_flags 0x%x ifaupflags 0x%x\n",
1811 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1812 if_name(ia
->ia_ifp
),
1817 * Mark the address as tentative before joining multicast addresses,
1818 * so that corresponding MLD responses would not have a tentative
1821 ia
->ia6_flags
&= ~IN6_IFF_DUPLICATED
; /* safety */
1822 if (in6if_do_dad(ifp
))
1823 in6_ifaddr_set_dadprogress(ia
);
1826 * Do not delay sending neighbor solicitations when using optimistic
1827 * duplicate address detection, c.f. RFC 4429.
1829 if (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
)
1830 ifaupflags
&= ~IN6_IFAUPDATE_DADDELAY
;
1832 ifaupflags
|= IN6_IFAUPDATE_DADDELAY
;
1834 /* Join necessary multicast groups */
1835 if ((ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1837 /* join solicited multicast addr for new host id */
1838 bzero(&llsol
, sizeof (struct in6_addr
));
1839 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
1840 llsol
.s6_addr32
[1] = 0;
1841 llsol
.s6_addr32
[2] = htonl(1);
1842 llsol
.s6_addr32
[3] = ia
->ia_addr
.sin6_addr
.s6_addr32
[3];
1843 llsol
.s6_addr8
[12] = 0xff;
1844 if ((error
= in6_setscope(&llsol
, ifp
, NULL
)) != 0) {
1845 /* XXX: should not happen */
1846 log(LOG_ERR
, "%s: in6_setscope failed\n", __func__
);
1850 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
)) {
1852 * We need a random delay for DAD on the address
1853 * being configured. It also means delaying
1854 * transmission of the corresponding MLD report to
1855 * avoid report collision. [RFC 4862]
1857 delay
= random() % MAX_RTR_SOLICITATION_DELAY
;
1859 imm
= in6_joingroup(ifp
, &llsol
, &error
, delay
);
1861 nd6log((LOG_WARNING
,
1862 "%s: addmulti failed for %s on %s (errno=%d)\n",
1863 __func__
, ip6_sprintf(&llsol
), if_name(ifp
),
1868 in6m_sol
= imm
->i6mm_maddr
;
1869 /* take a refcount for this routine */
1870 IN6M_ADDREF(in6m_sol
);
1873 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
1876 bzero(&mltmask
, sizeof (mltmask
));
1877 mltmask
.sin6_len
= sizeof (struct sockaddr_in6
);
1878 mltmask
.sin6_family
= AF_INET6
;
1879 mltmask
.sin6_addr
= in6mask32
;
1880 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
1883 * join link-local all-nodes address
1885 bzero(&mltaddr
, sizeof (mltaddr
));
1886 mltaddr
.sin6_len
= sizeof (struct sockaddr_in6
);
1887 mltaddr
.sin6_family
= AF_INET6
;
1888 mltaddr
.sin6_addr
= in6addr_linklocal_allnodes
;
1889 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
)) != 0)
1890 goto unwind
; /* XXX: should not fail */
1893 * XXX: do we really need this automatic routes?
1894 * We should probably reconsider this stuff. Most applications
1895 * actually do not need the routes, since they usually specify
1896 * the outgoing interface.
1898 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
1899 ia
->ia_ifp
->if_index
);
1901 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
1902 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN
)) {
1908 error
= rtrequest_scoped(RTM_ADD
,
1909 (struct sockaddr
*)&mltaddr
,
1910 (struct sockaddr
*)&ia
->ia_addr
,
1911 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
1912 NULL
, ia
->ia_ifp
->if_index
);
1919 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
1921 nd6log((LOG_WARNING
,
1922 "%s: addmulti failed for %s on %s (errno=%d)\n",
1923 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
1924 if_name(ifp
), error
));
1929 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
1933 * join node information group address
1935 #define hostnamelen strlen(hostname)
1937 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
)) {
1939 * The spec doesn't say anything about delay for this
1940 * group, but the same logic should apply.
1942 delay
= random() % MAX_RTR_SOLICITATION_DELAY
;
1944 if (in6_nigroup(ifp
, hostname
, hostnamelen
, &mltaddr
.sin6_addr
)
1946 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
,
1947 delay
); /* XXX jinmei */
1949 nd6log((LOG_WARNING
,
1950 "%s: addmulti failed for %s on %s "
1952 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
1953 if_name(ifp
), error
));
1954 /* XXX not very fatal, go on... */
1958 LIST_INSERT_HEAD(&ia
->ia6_memberships
,
1966 * join interface-local all-nodes address.
1967 * (ff01::1%ifN, and ff01::%ifN/32)
1969 mltaddr
.sin6_addr
= in6addr_nodelocal_allnodes
;
1970 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
)) != 0)
1971 goto unwind
; /* XXX: should not fail */
1972 /* XXX: again, do we really need the route? */
1973 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
1974 ia
->ia_ifp
->if_index
);
1976 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
1977 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN
)) {
1983 error
= rtrequest_scoped(RTM_ADD
,
1984 (struct sockaddr
*)&mltaddr
,
1985 (struct sockaddr
*)&ia
->ia_addr
,
1986 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
1987 NULL
, ia
->ia_ifp
->if_index
);
1993 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
1995 nd6log((LOG_WARNING
,
1996 "%s: addmulti failed for %s on %s (errno=%d)\n",
1997 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
1998 if_name(ifp
), error
));
2003 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
2008 /* Ensure nd6_service() is scheduled as soon as it's convenient */
2009 ++nd6_sched_timeout_want
;
2012 * Perform DAD, if needed.
2013 * XXX It may be of use, if we can administratively
2017 if (in6if_do_dad(ifp
) && ((ifa
->ifa_flags
& IN6_IFF_NODAD
) == 0) &&
2018 (ia
->ia6_flags
& IN6_IFF_DADPROGRESS
)) {
2019 int mindelay
, maxdelay
;
2020 int *delayptr
, delayval
;
2025 * Avoid the DAD delay if the caller wants us to skip it.
2026 * This is not compliant with RFC 2461, but it's only being
2027 * used for signalling and not for actual DAD.
2029 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
) &&
2030 !(ia
->ia6_flags
& IN6_IFF_SWIFTDAD
)) {
2032 * We need to impose a delay before sending an NS
2033 * for DAD. Check if we also needed a delay for the
2034 * corresponding MLD message. If we did, the delay
2035 * should be larger than the MLD delay (this could be
2036 * relaxed a bit, but this simple logic is at least
2040 if (in6m_sol
!= NULL
) {
2041 IN6M_LOCK(in6m_sol
);
2042 if (in6m_sol
->in6m_state
==
2043 MLD_REPORTING_MEMBER
)
2044 mindelay
= in6m_sol
->in6m_timer
;
2045 IN6M_UNLOCK(in6m_sol
);
2047 maxdelay
= MAX_RTR_SOLICITATION_DELAY
* hz
;
2048 if (maxdelay
- mindelay
== 0)
2052 (random() % (maxdelay
- mindelay
)) +
2055 delayptr
= &delayval
;
2058 nd6_dad_start((struct ifaddr
*)ia
, delayptr
);
2067 in6_purgeaddr(&ia
->ia_ifa
);
2070 /* release reference held for this routine */
2071 if (in6m_sol
!= NULL
)
2072 IN6M_REMREF(in6m_sol
);
2077 * Request an IPv6 interface address. If the address is new, then it will be
2078 * constructed and appended to the interface address chains. The interface
2079 * address structure is optionally returned with a reference for the caller.
2082 in6_update_ifa(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
, int ifaupflags
,
2083 struct in6_ifaddr
**iar
)
2085 struct in6_addrlifetime ia6_lt
;
2086 struct in6_ifaddr
*ia
;
2088 struct ifaddr
*xifa
;
2089 struct in6_addrlifetime
*lt
;
2093 /* Sanity check parameters and initialize locals */
2094 VERIFY(ifp
!= NULL
&& ifra
!= NULL
&& iar
!= NULL
);
2100 * We always require users to specify a valid IPv6 address for
2101 * the corresponding operation.
2103 if (ifra
->ifra_addr
.sin6_family
!= AF_INET6
||
2104 ifra
->ifra_addr
.sin6_len
!= sizeof (struct sockaddr_in6
)) {
2105 error
= EAFNOSUPPORT
;
2109 /* Validate ifra_prefixmask.sin6_len is properly bounded. */
2110 if (ifra
->ifra_prefixmask
.sin6_len
== 0 ||
2111 ifra
->ifra_prefixmask
.sin6_len
> sizeof (struct sockaddr_in6
)) {
2116 /* Validate prefix length extracted from ifra_prefixmask structure. */
2117 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
2118 (u_char
*)&ifra
->ifra_prefixmask
+ ifra
->ifra_prefixmask
.sin6_len
);
2124 /* Validate lifetimes */
2125 lt
= &ifra
->ifra_lifetime
;
2126 if (lt
->ia6t_pltime
> lt
->ia6t_vltime
) {
2128 "%s: pltime 0x%x > vltime 0x%x for %s\n", __func__
,
2129 lt
->ia6t_pltime
, lt
->ia6t_vltime
,
2130 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
2134 if (lt
->ia6t_vltime
== 0) {
2136 * the following log might be noisy, but this is a typical
2137 * configuration mistake or a tool's bug.
2139 log(LOG_INFO
, "%s: valid lifetime is 0 for %s\n", __func__
,
2140 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
2144 * Before we lock the ifnet structure, we first check to see if the
2145 * address already exists. If so, then we don't allocate and link a
2148 ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
);
2153 * Validate destination address on interface types that require it.
2155 if ((ifp
->if_flags
& (IFF_LOOPBACK
|IFF_POINTOPOINT
)) != 0) {
2156 switch (ifra
->ifra_dstaddr
.sin6_family
) {
2159 /* noisy message for diagnostic purposes */
2161 "%s: prefix length < 128 with"
2162 " explicit dstaddr.\n", __func__
);
2172 error
= EAFNOSUPPORT
;
2175 } else if (ifra
->ifra_dstaddr
.sin6_family
!= AF_UNSPEC
) {
2177 "%s: dstaddr valid only on p2p and loopback interfaces.\n",
2183 timenow
= net_uptime();
2188 /* Is this the first new IPv6 address for the interface? */
2189 ifaupflags
|= IN6_IFAUPDATE_NEWADDR
;
2191 /* Allocate memory for IPv6 interface address structure. */
2192 how
= !(ifaupflags
& IN6_IFAUPDATE_NOWAIT
) ? M_WAITOK
: 0;
2193 ia
= in6_ifaddr_alloc(how
);
2202 * Initialize interface address structure.
2204 * Note well: none of these sockaddr_in6 structures contain a
2205 * valid sin6_port, sin6_flowinfo or even a sin6_scope_id field.
2206 * We still embed link-local scope identifiers at the end of an
2207 * arbitrary fe80::/32 prefix, for historical reasons. Also, the
2208 * ifa_dstaddr field is always non-NULL on point-to-point and
2209 * loopback interfaces, and conventionally points to a socket
2210 * address of AF_UNSPEC family when there is no destination.
2212 * Please enjoy the dancing sea turtle.
2214 IFA_ADDREF(ifa
); /* for this and optionally for caller */
2215 ifa
->ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
2216 if (ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
||
2217 (ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) != 0)
2218 ifa
->ifa_dstaddr
= (struct sockaddr
*)&ia
->ia_dstaddr
;
2219 ifa
->ifa_netmask
= (struct sockaddr
*)&ia
->ia_prefixmask
;
2221 ifa
->ifa_metric
= ifp
->if_metric
;
2222 ifa
->ifa_rtrequest
= nd6_rtrequest
;
2224 LIST_INIT(&ia
->ia6_memberships
);
2225 ia
->ia_addr
.sin6_family
= AF_INET6
;
2226 ia
->ia_addr
.sin6_len
= sizeof (ia
->ia_addr
);
2227 ia
->ia_addr
.sin6_addr
= ifra
->ifra_addr
.sin6_addr
;
2228 ia
->ia_prefixmask
.sin6_family
= AF_INET6
;
2229 ia
->ia_prefixmask
.sin6_len
= sizeof (ia
->ia_prefixmask
);
2230 ia
->ia_prefixmask
.sin6_addr
= ifra
->ifra_prefixmask
.sin6_addr
;
2231 error
= in6_to_kamescope(&ia
->ia_addr
, ifp
);
2234 if (ifa
->ifa_dstaddr
!= NULL
) {
2235 ia
->ia_dstaddr
= ifra
->ifra_dstaddr
;
2236 error
= in6_to_kamescope(&ia
->ia_dstaddr
, ifp
);
2241 /* Append to address chains */
2242 ifnet_lock_exclusive(ifp
);
2243 ifaupflags
|= IN6_IFAUPDATE_1STADDR
;
2244 TAILQ_FOREACH(xifa
, &ifp
->if_addrlist
, ifa_list
) {
2245 IFA_LOCK_SPIN(xifa
);
2246 if (xifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2248 ifaupflags
&= ~IN6_IFAUPDATE_1STADDR
;
2255 if_attach_ifa(ifp
, ifa
); /* holds reference for ifnet link */
2257 ifnet_lock_done(ifp
);
2259 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2260 if (in6_ifaddrs
!= NULL
) {
2261 struct in6_ifaddr
*iac
;
2262 for (iac
= in6_ifaddrs
; iac
->ia_next
!= NULL
;
2269 IFA_ADDREF(ifa
); /* hold for in6_ifaddrs link */
2270 lck_rw_done(&in6_ifaddr_rwlock
);
2273 ifaupflags
&= ~(IN6_IFAUPDATE_NEWADDR
|IN6_IFAUPDATE_1STADDR
);
2276 VERIFY(ia
!= NULL
&& ifa
== &ia
->ia_ifa
);
2280 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
2281 * to see if the address is deprecated or invalidated, but initialize
2282 * these members for applications.
2284 ia
->ia6_updatetime
= ia
->ia6_createtime
= timenow
;
2286 if (ia6_lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
)
2287 ia6_lt
.ia6t_expire
= timenow
+ ia6_lt
.ia6t_vltime
;
2289 ia6_lt
.ia6t_expire
= 0;
2290 if (ia6_lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
)
2291 ia6_lt
.ia6t_preferred
= timenow
+ ia6_lt
.ia6t_pltime
;
2293 ia6_lt
.ia6t_preferred
= 0;
2294 in6ifa_setlifetime(ia
, &ia6_lt
);
2297 * Backward compatibility - if IN6_IFF_DEPRECATED is set from the
2298 * userland, make it deprecated.
2300 if ((ia
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0) {
2301 ia
->ia6_lifetime
.ia6ti_pltime
= 0;
2302 ia
->ia6_lifetime
.ia6ti_preferred
= timenow
;
2306 * Update flag or prefix length
2309 ia
->ia6_flags
= ifra
->ifra_flags
;
2311 /* Release locks (new address available to concurrent tasks) */
2314 /* Further initialization of the interface address */
2315 error
= in6_ifinit(ifp
, ia
, ifaupflags
);
2319 /* Finish updating the address while other tasks are working with it */
2320 error
= in6_ifaupdate_aux(ia
, ifp
, ifaupflags
);
2324 /* Return success (optionally w/ address for caller). */
2326 (void) ifnet_notify_address(ifp
, AF_INET6
);
2332 VERIFY(ifa
== &ia
->ia_ifa
);
2343 in6_purgeaddr(struct ifaddr
*ifa
)
2345 struct ifnet
*ifp
= ifa
->ifa_ifp
;
2346 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
2347 struct in6_multi_mship
*imm
;
2349 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2351 /* stop DAD processing */
2355 * delete route to the destination of the address being purged.
2356 * The interface must be p2p or loopback in this case.
2359 if ((ia
->ia_flags
& IFA_ROUTE
) && ia
->ia_plen
== 128) {
2363 rtf
= (ia
->ia_dstaddr
.sin6_family
== AF_INET6
) ? RTF_HOST
: 0;
2364 error
= rtinit(&(ia
->ia_ifa
), RTM_DELETE
, rtf
);
2366 log(LOG_ERR
, "in6_purgeaddr: failed to remove "
2367 "a route to the p2p destination: %s on %s, "
2369 ip6_sprintf(&ia
->ia_addr
.sin6_addr
), if_name(ifp
),
2371 /* proceed anyway... */
2374 ia
->ia_flags
&= ~IFA_ROUTE
;
2378 /* Remove ownaddr's loopback rtentry, if it exists. */
2379 in6_ifremloop(&(ia
->ia_ifa
));
2382 * leave from multicast groups we have joined for the interface
2385 while ((imm
= ia
->ia6_memberships
.lh_first
) != NULL
) {
2386 LIST_REMOVE(imm
, i6mm_chain
);
2388 in6_leavegroup(imm
);
2393 /* in6_unlink_ifa() will need exclusive access */
2394 in6_unlink_ifa(ia
, ifp
);
2395 in6_post_msg(ifp
, KEV_INET6_ADDR_DELETED
, ia
, NULL
);
2397 (void) ifnet_notify_address(ifp
, AF_INET6
);
2401 in6_unlink_ifa(struct in6_ifaddr
*ia
, struct ifnet
*ifp
)
2403 struct in6_ifaddr
*oia
;
2407 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2412 ifnet_lock_exclusive(ifp
);
2414 if (ifa
->ifa_debug
& IFD_ATTACHED
)
2415 if_detach_ifa(ifp
, ifa
);
2417 ifnet_lock_done(ifp
);
2420 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2422 if (oia
== (ia
= in6_ifaddrs
)) {
2423 in6_ifaddrs
= ia
->ia_next
;
2425 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
2428 ia
->ia_next
= oia
->ia_next
;
2431 log(LOG_NOTICE
, "%s: search failed.\n", __func__
);
2437 * When IPv6 address is being removed, release the
2438 * reference to the base prefix.
2439 * Also, since the release might, affect the status
2440 * of other (detached) addresses, call
2441 * pfxlist_onlink_check().
2446 * Only log the below message for addresses other than
2448 * Only one LLA (auto-configured or statically) is allowed
2450 * LLA prefix, while added to the prefix list, is not
2451 * reference countedi (as it is the only one).
2452 * The prefix also never expires on its own as LLAs
2453 * have infinite lifetime.
2455 * For now quiece down the log message for LLAs.
2457 if (!IN6_IS_ADDR_LINKLOCAL(&oia
->ia_addr
.sin6_addr
)) {
2458 if (oia
->ia6_ndpr
== NULL
)
2459 log(LOG_NOTICE
, "in6_unlink_ifa: IPv6 address "
2460 "0x%llx has no prefix\n",
2461 (uint64_t)VM_KERNEL_ADDRPERM(oia
));
2463 struct nd_prefix
*pr
= oia
->ia6_ndpr
;
2464 oia
->ia6_flags
&= ~IN6_IFF_AUTOCONF
;
2465 oia
->ia6_ndpr
= NULL
;
2467 VERIFY(pr
->ndpr_addrcnt
!= 0);
2470 NDPR_REMREF(pr
); /* release addr reference */
2474 lck_rw_done(&in6_ifaddr_rwlock
);
2476 if ((oia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0) {
2477 lck_mtx_lock(nd6_mutex
);
2478 pfxlist_onlink_check();
2479 lck_mtx_unlock(nd6_mutex
);
2482 * release another refcnt for the link from in6_ifaddrs.
2483 * Do this only if it's not already unlinked in the event that we lost
2484 * the race, since in6_ifaddr_rwlock was momentarily dropped above.
2489 /* release reference held for this routine */
2492 /* invalidate route caches */
2493 routegenid_inet6_update();
2497 in6_purgeif(struct ifnet
*ifp
)
2499 struct in6_ifaddr
*ia
;
2504 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2506 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2508 while (ia
!= NULL
) {
2509 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
2513 IFA_ADDREF(&ia
->ia_ifa
); /* for us */
2514 lck_rw_done(&in6_ifaddr_rwlock
);
2515 in6_purgeaddr(&ia
->ia_ifa
);
2516 IFA_REMREF(&ia
->ia_ifa
); /* for us */
2517 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2519 * Purging the address would have caused
2520 * in6_ifaddr_rwlock to be dropped and reacquired;
2521 * therefore search again from the beginning
2522 * of in6_ifaddrs list.
2526 lck_rw_done(&in6_ifaddr_rwlock
);
2532 * Initialize an interface's internet6 address and routing table entry.
2535 in6_ifinit(struct ifnet
*ifp
, struct in6_ifaddr
*ia
, int ifaupflags
)
2544 * NOTE: SIOCSIFADDR is defined with struct ifreq as parameter,
2545 * but here we are sending it down to the interface with a pointer
2546 * to struct ifaddr, for legacy reasons.
2548 if ((ifaupflags
& IN6_IFAUPDATE_1STADDR
) != 0) {
2549 error
= ifnet_ioctl(ifp
, PF_INET6
, SIOCSIFADDR
, ia
);
2551 if (error
!= EOPNOTSUPP
)
2561 * If the destination address is specified for a point-to-point
2562 * interface, install a route to the destination as an interface
2565 if (!(ia
->ia_flags
& IFA_ROUTE
) && ia
->ia_plen
== 128 &&
2566 ia
->ia_dstaddr
.sin6_family
== AF_INET6
) {
2568 error
= rtinit(ifa
, RTM_ADD
, RTF_UP
| RTF_HOST
);
2572 ia
->ia_flags
|= IFA_ROUTE
;
2574 IFA_LOCK_ASSERT_HELD(ifa
);
2575 if (ia
->ia_plen
< 128) {
2577 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
2579 ia
->ia_flags
|= RTF_CLONING
;
2584 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
2585 if ((ifaupflags
& IN6_IFAUPDATE_NEWADDR
) != 0)
2588 /* invalidate route caches */
2589 routegenid_inet6_update();
2596 in6_purgeaddrs(struct ifnet
*ifp
)
2602 * Find an IPv6 interface link-local address specific to an interface.
2605 in6ifa_ifpforlinklocal(struct ifnet
*ifp
, int ignoreflags
)
2609 ifnet_lock_shared(ifp
);
2610 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2613 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2617 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
))) {
2618 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2619 ignoreflags
) != 0) {
2623 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2629 ifnet_lock_done(ifp
);
2631 return ((struct in6_ifaddr
*)ifa
);
2635 * find the internet address corresponding to a given interface and address.
2638 in6ifa_ifpwithaddr(struct ifnet
*ifp
, struct in6_addr
*addr
)
2642 ifnet_lock_shared(ifp
);
2643 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2646 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2650 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(ifa
))) {
2651 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2657 ifnet_lock_done(ifp
);
2659 return ((struct in6_ifaddr
*)ifa
);
2663 in6ifa_prproxyaddr(struct in6_addr
*addr
)
2665 struct in6_ifaddr
*ia
;
2667 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2668 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2669 IFA_LOCK(&ia
->ia_ifa
);
2670 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(&ia
->ia_ifa
))) {
2671 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
2672 IFA_UNLOCK(&ia
->ia_ifa
);
2675 IFA_UNLOCK(&ia
->ia_ifa
);
2677 lck_rw_done(&in6_ifaddr_rwlock
);
2679 if (ia
!= NULL
&& !nd6_prproxy_ifaddr(ia
)) {
2680 IFA_REMREF(&ia
->ia_ifa
);
2688 in6ifa_getlifetime(struct in6_ifaddr
*ia6
, struct in6_addrlifetime
*t_dst
,
2691 struct in6_addrlifetime_i
*t_src
= &ia6
->ia6_lifetime
;
2692 struct timeval caltime
;
2694 t_dst
->ia6t_vltime
= t_src
->ia6ti_vltime
;
2695 t_dst
->ia6t_pltime
= t_src
->ia6ti_pltime
;
2696 t_dst
->ia6t_expire
= 0;
2697 t_dst
->ia6t_preferred
= 0;
2699 /* account for system time change */
2700 getmicrotime(&caltime
);
2701 t_src
->ia6ti_base_calendartime
+=
2702 NET_CALCULATE_CLOCKSKEW(caltime
,
2703 t_src
->ia6ti_base_calendartime
, net_uptime(),
2704 t_src
->ia6ti_base_uptime
);
2707 if (t_src
->ia6ti_expire
!= 0 &&
2708 t_src
->ia6ti_vltime
!= ND6_INFINITE_LIFETIME
)
2709 t_dst
->ia6t_expire
= t_src
->ia6ti_base_calendartime
+
2710 t_src
->ia6ti_expire
- t_src
->ia6ti_base_uptime
;
2712 if (t_src
->ia6ti_preferred
!= 0 &&
2713 t_src
->ia6ti_pltime
!= ND6_INFINITE_LIFETIME
)
2714 t_dst
->ia6t_preferred
= t_src
->ia6ti_base_calendartime
+
2715 t_src
->ia6ti_preferred
- t_src
->ia6ti_base_uptime
;
2717 if (t_src
->ia6ti_expire
!= 0 &&
2718 t_src
->ia6ti_vltime
!= ND6_INFINITE_LIFETIME
)
2719 t_dst
->ia6t_expire
= t_src
->ia6ti_expire
;
2721 if (t_src
->ia6ti_preferred
!= 0 &&
2722 t_src
->ia6ti_pltime
!= ND6_INFINITE_LIFETIME
)
2723 t_dst
->ia6t_preferred
= t_src
->ia6ti_preferred
;
2728 in6ifa_setlifetime(struct in6_ifaddr
*ia6
, struct in6_addrlifetime
*t_src
)
2730 struct in6_addrlifetime_i
*t_dst
= &ia6
->ia6_lifetime
;
2731 struct timeval caltime
;
2733 /* account for system time change */
2734 getmicrotime(&caltime
);
2735 t_dst
->ia6ti_base_calendartime
+=
2736 NET_CALCULATE_CLOCKSKEW(caltime
,
2737 t_dst
->ia6ti_base_calendartime
, net_uptime(),
2738 t_dst
->ia6ti_base_uptime
);
2740 /* trust the caller for the values */
2741 t_dst
->ia6ti_expire
= t_src
->ia6t_expire
;
2742 t_dst
->ia6ti_preferred
= t_src
->ia6t_preferred
;
2743 t_dst
->ia6ti_vltime
= t_src
->ia6t_vltime
;
2744 t_dst
->ia6ti_pltime
= t_src
->ia6t_pltime
;
2748 * Convert IP6 address to printable (loggable) representation.
2751 ip6_sprintf(const struct in6_addr
*addr
)
2753 static const char digits
[] = "0123456789abcdef";
2754 static int ip6round
= 0;
2755 static char ip6buf
[8][48];
2759 const u_short
*a
= (const u_short
*)addr
;
2765 ip6round
= (ip6round
+ 1) & 7;
2766 cp
= ip6buf
[ip6round
];
2768 for (i
= 0; i
< 8; i
++) {
2779 if (dcolon
== 0 && *(a
+ 1) == 0) {
2791 d
= (const u_char
*)a
;
2793 if ((n
= *d
>> 4) != 0) {
2797 if ((n
= *d
++ & 0xf) != 0 || zpad
) {
2801 if ((n
= *d
>> 4) != 0 || zpad
) {
2805 if ((n
= *d
& 0xf) != 0 || zpad
)
2811 return (ip6buf
[ip6round
]);
2815 in6addr_local(struct in6_addr
*in6
)
2818 struct sockaddr_in6 sin6
;
2821 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_SCOPE_LINKLOCAL(in6
))
2824 sin6
.sin6_family
= AF_INET6
;
2825 sin6
.sin6_len
= sizeof (sin6
);
2826 bcopy(in6
, &sin6
.sin6_addr
, sizeof (*in6
));
2827 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0);
2831 if (rt
->rt_gateway
->sa_family
== AF_LINK
)
2836 local
= in6_localaddr(in6
);
2842 in6_localaddr(struct in6_addr
*in6
)
2844 struct in6_ifaddr
*ia
;
2846 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
))
2849 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2850 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2851 IFA_LOCK_SPIN(&ia
->ia_ifa
);
2852 if (IN6_ARE_MASKED_ADDR_EQUAL(in6
, &ia
->ia_addr
.sin6_addr
,
2853 &ia
->ia_prefixmask
.sin6_addr
)) {
2854 IFA_UNLOCK(&ia
->ia_ifa
);
2855 lck_rw_done(&in6_ifaddr_rwlock
);
2858 IFA_UNLOCK(&ia
->ia_ifa
);
2860 lck_rw_done(&in6_ifaddr_rwlock
);
2865 * return length of part which dst and src are equal
2869 in6_matchlen(struct in6_addr
*src
, struct in6_addr
*dst
)
2872 u_char
*s
= (u_char
*)src
, *d
= (u_char
*)dst
;
2873 u_char
*lim
= s
+ 16, r
;
2876 if ((r
= (*d
++ ^ *s
++)) != 0) {
2887 /* XXX: to be scope conscious */
2889 in6_are_prefix_equal(struct in6_addr
*p1
, struct in6_addr
*p2
, int len
)
2891 int bytelen
, bitlen
;
2894 if (0 > len
|| len
> 128) {
2895 log(LOG_ERR
, "%s: invalid prefix length(%d)\n", __func__
, len
);
2902 if (bcmp(&p1
->s6_addr
, &p2
->s6_addr
, bytelen
))
2905 p1
->s6_addr
[bytelen
] >> (8 - bitlen
) !=
2906 p2
->s6_addr
[bytelen
] >> (8 - bitlen
))
2913 in6_prefixlen2mask(struct in6_addr
*maskp
, int len
)
2915 u_char maskarray
[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2916 int bytelen
, bitlen
, i
;
2919 if (0 > len
|| len
> 128) {
2920 log(LOG_ERR
, "%s: invalid prefix length(%d)\n", __func__
, len
);
2924 bzero(maskp
, sizeof (*maskp
));
2927 for (i
= 0; i
< bytelen
; i
++)
2928 maskp
->s6_addr
[i
] = 0xff;
2930 maskp
->s6_addr
[bytelen
] = maskarray
[bitlen
- 1];
2934 * return the best address out of the same scope
2937 in6_ifawithscope(struct ifnet
*oifp
, struct in6_addr
*dst
)
2939 int dst_scope
= in6_addrscope(dst
), src_scope
, best_scope
= 0;
2943 struct in6_ifaddr
*ifa_best
= NULL
;
2950 * We search for all addresses on all interfaces from the beginning.
2951 * Comparing an interface with the outgoing interface will be done
2952 * only at the final stage of tiebreaking.
2954 ifnet_head_lock_shared();
2955 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
2957 * We can never take an address that breaks the scope zone
2958 * of the destination.
2960 if (in6_addr2scopeid(ifp
, dst
) != in6_addr2scopeid(oifp
, dst
))
2963 ifnet_lock_shared(ifp
);
2964 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
2965 int tlen
= -1, dscopecmp
, bscopecmp
, matchcmp
;
2968 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2972 src_scope
= in6_addrscope(IFA_IN6(ifa
));
2975 * Don't use an address before completing DAD
2976 * nor a duplicated address.
2978 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2983 /* XXX: is there any case to allow anycasts? */
2984 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2989 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2995 * If this is the first address we find,
2998 if (ifa_best
== NULL
)
3002 * ifa_best is never NULL beyond this line except
3003 * within the block labeled "replace".
3007 * If ifa_best has a smaller scope than dst and
3008 * the current address has a larger one than
3009 * (or equal to) dst, always replace ifa_best.
3010 * Also, if the current address has a smaller scope
3011 * than dst, ignore it unless ifa_best also has a
3013 * Consequently, after the two if-clause below,
3014 * the followings must be satisfied:
3015 * (scope(src) < scope(dst) &&
3016 * scope(best) < scope(dst))
3018 * (scope(best) >= scope(dst) &&
3019 * scope(src) >= scope(dst))
3021 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0 &&
3022 IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) >= 0)
3023 goto replace
; /* (A) */
3024 if (IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) < 0 &&
3025 IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) >= 0) {
3030 * A deprecated address SHOULD NOT be used in new
3031 * communications if an alternate (non-deprecated)
3032 * address is available and has sufficient scope.
3033 * RFC 4862, Section 5.5.4.
3035 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3036 IN6_IFF_DEPRECATED
) {
3038 * Ignore any deprecated addresses if
3039 * specified by configuration.
3041 if (!ip6_use_deprecated
) {
3046 * If we have already found a non-deprecated
3047 * candidate, just ignore deprecated addresses.
3049 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
)
3057 * A non-deprecated address is always preferred
3058 * to a deprecated one regardless of scopes and
3059 * address matching (Note invariants ensured by the
3060 * conditions (A) and (B) above.)
3062 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) &&
3063 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3064 IN6_IFF_DEPRECATED
) == 0)
3068 * When we use temporary addresses described in
3069 * RFC 4941, we prefer temporary addresses to
3070 * public autoconf addresses. Again, note the
3071 * invariants from (A) and (B). Also note that we
3072 * don't have any preference between static addresses
3073 * and autoconf addresses (despite of whether or not
3074 * the latter is temporary or public.)
3076 if (ip6_use_tempaddr
) {
3077 struct in6_ifaddr
*ifat
;
3079 ifat
= (struct in6_ifaddr
*)ifa
;
3080 if ((ifa_best
->ia6_flags
&
3081 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3082 == IN6_IFF_AUTOCONF
&&
3084 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3085 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
)) {
3088 if ((ifa_best
->ia6_flags
&
3089 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3090 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
) &&
3092 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3093 == IN6_IFF_AUTOCONF
) {
3100 * At this point, we have two cases:
3101 * 1. we are looking at a non-deprecated address,
3102 * and ifa_best is also non-deprecated.
3103 * 2. we are looking at a deprecated address,
3104 * and ifa_best is also deprecated.
3105 * Also, we do not have to consider a case where
3106 * the scope of if_best is larger(smaller) than dst and
3107 * the scope of the current address is smaller(larger)
3108 * than dst. Such a case has already been covered.
3109 * Tiebreaking is done according to the following
3111 * - the scope comparison between the address and
3113 * - the scope comparison between the address and
3114 * ifa_best (bscopecmp)
3115 * - if the address match dst longer than ifa_best
3117 * - if the address is on the outgoing I/F (outI/F)
3119 * Roughly speaking, the selection policy is
3120 * - the most important item is scope. The same scope
3121 * is best. Then search for a larger scope.
3122 * Smaller scopes are the last resort.
3123 * - A deprecated address is chosen only when we have
3124 * no address that has an enough scope, but is
3125 * prefered to any addresses of smaller scopes
3126 * (this must be already done above.)
3127 * - addresses on the outgoing I/F are preferred to
3128 * ones on other interfaces if none of above
3129 * tiebreaks. In the table below, the column "bI"
3130 * means if the best_ifa is on the outgoing
3131 * interface, and the column "sI" means if the ifa
3132 * is on the outgoing interface.
3133 * - If there is no other reasons to choose one,
3134 * longest address match against dst is considered.
3136 * The precise decision table is as follows:
3137 * dscopecmp bscopecmp match bI oI | replace?
3138 * N/A equal N/A Y N | No (1)
3139 * N/A equal N/A N Y | Yes (2)
3140 * N/A equal larger N/A | Yes (3)
3141 * N/A equal !larger N/A | No (4)
3142 * larger larger N/A N/A | No (5)
3143 * larger smaller N/A N/A | Yes (6)
3144 * smaller larger N/A N/A | Yes (7)
3145 * smaller smaller N/A N/A | No (8)
3146 * equal smaller N/A N/A | Yes (9)
3147 * equal larger (already done at A above)
3149 dscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
);
3150 bscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, best_scope
);
3152 if (bscopecmp
== 0) {
3153 struct ifnet
*bifp
= ifa_best
->ia_ifp
;
3155 if (bifp
== oifp
&& ifp
!= oifp
) { /* (1) */
3159 if (bifp
!= oifp
&& ifp
== oifp
) /* (2) */
3163 * Both bifp and ifp are on the outgoing
3164 * interface, or both two are on a different
3165 * interface from the outgoing I/F.
3166 * now we need address matching against dst
3169 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3170 matchcmp
= tlen
- blen
;
3171 if (matchcmp
> 0) /* (3) */
3176 if (dscopecmp
> 0) {
3177 if (bscopecmp
> 0) { /* (5) */
3181 goto replace
; /* (6) */
3183 if (dscopecmp
< 0) {
3184 if (bscopecmp
> 0) /* (7) */
3190 /* now dscopecmp must be 0 */
3192 goto replace
; /* (9) */
3195 IFA_ADDREF_LOCKED(ifa
); /* for ifa_best */
3196 blen
= tlen
>= 0 ? tlen
:
3197 in6_matchlen(IFA_IN6(ifa
), dst
);
3199 in6_addrscope(&ifa2ia6(ifa
)->ia_addr
.sin6_addr
);
3202 IFA_REMREF(&ifa_best
->ia_ifa
);
3203 ifa_best
= (struct in6_ifaddr
*)ifa
;
3205 ifnet_lock_done(ifp
);
3209 /* count statistics for future improvements */
3210 if (ifa_best
== NULL
)
3211 ip6stat
.ip6s_sources_none
++;
3213 IFA_LOCK_SPIN(&ifa_best
->ia_ifa
);
3214 if (oifp
== ifa_best
->ia_ifp
)
3215 ip6stat
.ip6s_sources_sameif
[best_scope
]++;
3217 ip6stat
.ip6s_sources_otherif
[best_scope
]++;
3219 if (best_scope
== dst_scope
)
3220 ip6stat
.ip6s_sources_samescope
[best_scope
]++;
3222 ip6stat
.ip6s_sources_otherscope
[best_scope
]++;
3224 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0)
3225 ip6stat
.ip6s_sources_deprecated
[best_scope
]++;
3226 IFA_UNLOCK(&ifa_best
->ia_ifa
);
3233 * return the best address out of the same scope. if no address was
3234 * found, return the first valid address from designated IF.
3237 in6_ifawithifp(struct ifnet
*ifp
, struct in6_addr
*dst
)
3239 int dst_scope
= in6_addrscope(dst
), blen
= -1, tlen
;
3241 struct in6_ifaddr
*besta
= NULL
;
3242 struct in6_ifaddr
*dep
[2]; /* last-resort: deprecated */
3244 dep
[0] = dep
[1] = NULL
;
3247 * We first look for addresses in the same scope.
3248 * If there is one, return it.
3249 * If two or more, return one which matches the dst longest.
3250 * If none, return one of global addresses assigned other ifs.
3252 ifnet_lock_shared(ifp
);
3253 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3255 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3259 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3261 continue; /* XXX: is there any case to allow anycast? */
3263 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_NOTREADY
) {
3265 continue; /* don't use this interface */
3267 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3271 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3272 if (ip6_use_deprecated
) {
3273 IFA_ADDREF_LOCKED(ifa
); /* for dep[0] */
3276 IFA_REMREF(&dep
[0]->ia_ifa
);
3277 dep
[0] = (struct in6_ifaddr
*)ifa
;
3284 if (dst_scope
== in6_addrscope(IFA_IN6(ifa
))) {
3286 * call in6_matchlen() as few as possible
3291 IFA_LOCK(&besta
->ia_ifa
);
3292 blen
= in6_matchlen(
3293 &besta
->ia_addr
.sin6_addr
, dst
);
3294 IFA_UNLOCK(&besta
->ia_ifa
);
3297 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3300 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3302 IFA_REMREF(&besta
->ia_ifa
);
3303 besta
= (struct in6_ifaddr
*)ifa
;
3308 besta
= (struct in6_ifaddr
*)ifa
;
3309 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3317 ifnet_lock_done(ifp
);
3319 IFA_REMREF(&dep
[0]->ia_ifa
);
3323 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3325 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3329 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3331 continue; /* XXX: is there any case to allow anycast? */
3333 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_NOTREADY
) {
3335 continue; /* don't use this interface */
3337 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3341 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3342 if (ip6_use_deprecated
) {
3343 IFA_ADDREF_LOCKED(ifa
); /* for dep[1] */
3346 IFA_REMREF(&dep
[1]->ia_ifa
);
3347 dep
[1] = (struct in6_ifaddr
*)ifa
;
3353 IFA_ADDREF_LOCKED(ifa
); /* for caller */
3355 ifnet_lock_done(ifp
);
3357 IFA_REMREF(&dep
[0]->ia_ifa
);
3359 IFA_REMREF(&dep
[1]->ia_ifa
);
3360 return ((struct in6_ifaddr
*)ifa
);
3362 ifnet_lock_done(ifp
);
3364 /* use the last-resort values, that are, deprecated addresses */
3367 IFA_REMREF(&dep
[1]->ia_ifa
);
3377 * perform DAD when interface becomes IFF_UP.
3380 in6_if_up_dad_start(struct ifnet
*ifp
)
3383 struct nd_ifinfo
*ndi
= NULL
;
3385 ndi
= ND_IFINFO(ifp
);
3386 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3387 if (!(ndi
->flags
& ND6_IFF_DAD
))
3390 /* start DAD on all the interface addresses */
3391 ifnet_lock_exclusive(ifp
);
3392 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3393 struct in6_ifaddr
*ia6
;
3396 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3400 ia6
= (struct in6_ifaddr
*)ifa
;
3401 if (ia6
->ia6_flags
& IN6_IFF_DADPROGRESS
) {
3402 int delay
= 0; /* delay ticks before DAD output */
3404 nd6_dad_start(ifa
, &delay
);
3409 ifnet_lock_done(ifp
);
3416 struct nd_ifinfo
*ndi
= NULL
;
3418 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0)
3421 ndi
= ND_IFINFO(ifp
);
3422 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3423 if (!(ndi
->flags
& ND6_IFF_DAD
))
3427 * If we are using the alternative neighbor discovery
3428 * interface on this interface, then skip DAD.
3430 * Also, skip it for interfaces marked "local private"
3431 * for now, even when not marked as using the alternative
3432 * interface. This is for historical reasons.
3434 if (ifp
->if_eflags
&
3435 (IFEF_IPV6_ND6ALT
|IFEF_LOCALNET_PRIVATE
|IFEF_DIRECTLINK
))
3438 switch (ifp
->if_type
) {
3444 * These interfaces do not have the IFF_LOOPBACK flag,
3445 * but loop packets back. We do not have to do DAD on such
3446 * interfaces. We should even omit it, because loop-backed
3447 * NS would confuse the DAD procedure.
3452 * Our DAD routine requires the interface up and running.
3453 * However, some interfaces can be up before the RUNNING
3454 * status. Additionaly, users may try to assign addresses
3455 * before the interface becomes up (or running).
3456 * We simply skip DAD in such a case as a work around.
3457 * XXX: we should rather mark "tentative" on such addresses,
3458 * and do DAD after the interface becomes ready.
3460 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) !=
3461 (IFF_UP
|IFF_RUNNING
))
3469 * Calculate max IPv6 MTU through all the interfaces and store it
3475 u_int32_t maxmtu
= 0;
3478 ifnet_head_lock_shared();
3479 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
3480 struct nd_ifinfo
*ndi
= NULL
;
3482 if ((ndi
= ND_IFINFO(ifp
)) != NULL
&& !ndi
->initialized
)
3485 lck_mtx_lock(&ndi
->lock
);
3486 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
3487 IN6_LINKMTU(ifp
) > maxmtu
)
3488 maxmtu
= IN6_LINKMTU(ifp
);
3490 lck_mtx_unlock(&ndi
->lock
);
3493 if (maxmtu
) /* update only when maxmtu is positive */
3494 in6_maxmtu
= maxmtu
;
3497 * Provide the length of interface identifiers to be used for the link attached
3498 * to the given interface. The length should be defined in "IPv6 over
3499 * xxx-link" document. Note that address architecture might also define
3500 * the length for a particular set of address prefixes, regardless of the
3501 * link type. Also see RFC 4862 for additional background.
3504 in6_if2idlen(struct ifnet
*ifp
)
3506 switch (ifp
->if_type
) {
3507 case IFT_ETHER
: /* RFC2464 */
3508 case IFT_IEEE8023ADLAG
: /* IEEE802.3ad Link Aggregate */
3509 #ifdef IFT_PROPVIRTUAL
3510 case IFT_PROPVIRTUAL
: /* XXX: no RFC. treat it as ether */
3513 case IFT_L2VLAN
: /* ditto */
3515 #ifdef IFT_IEEE80211
3516 case IFT_IEEE80211
: /* ditto */
3519 case IFT_MIP
: /* ditto */
3522 case IFT_FDDI
: /* RFC2467 */
3524 case IFT_ISO88025
: /* RFC2470 (IPv6 over Token Ring) */
3526 case IFT_PPP
: /* RFC2472 */
3528 case IFT_ARCNET
: /* RFC2497 */
3530 case IFT_FRELAY
: /* RFC2590 */
3532 case IFT_IEEE1394
: /* RFC3146 */
3535 return (64); /* draft-ietf-v6ops-mech-v2-07 */
3537 return (64); /* XXX: is this really correct? */
3539 return (64); /* for utun interfaces */
3541 return (64); /* Packet Data over Cellular */
3543 return (64); /* Transparent bridge interface */
3546 * Unknown link type:
3547 * It might be controversial to use the today's common constant
3548 * of 64 for these cases unconditionally. For full compliance,
3549 * we should return an error in this case. On the other hand,
3550 * if we simply miss the standard for the link type or a new
3551 * standard is defined for a new link type, the IFID length
3552 * is very likely to be the common constant. As a compromise,
3553 * we always use the constant, but make an explicit notice
3554 * indicating the "unknown" case.
3556 log(LOG_NOTICE
, "%s: unknown link type (%d)\n", __func__
,
3562 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
3563 * v4 mapped addr or v4 compat addr
3566 in6_sin6_2_sin(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3568 bzero(sin
, sizeof (*sin
));
3569 sin
->sin_len
= sizeof (struct sockaddr_in
);
3570 sin
->sin_family
= AF_INET
;
3571 sin
->sin_port
= sin6
->sin6_port
;
3572 sin
->sin_addr
.s_addr
= sin6
->sin6_addr
.s6_addr32
[3];
3575 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
3577 in6_sin_2_v4mapsin6(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3579 bzero(sin6
, sizeof (*sin6
));
3580 sin6
->sin6_len
= sizeof (struct sockaddr_in6
);
3581 sin6
->sin6_family
= AF_INET6
;
3582 sin6
->sin6_port
= sin
->sin_port
;
3583 sin6
->sin6_addr
.s6_addr32
[0] = 0;
3584 sin6
->sin6_addr
.s6_addr32
[1] = 0;
3585 if (sin
->sin_addr
.s_addr
) {
3586 sin6
->sin6_addr
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
3587 sin6
->sin6_addr
.s6_addr32
[3] = sin
->sin_addr
.s_addr
;
3589 sin6
->sin6_addr
.s6_addr32
[2] = 0;
3590 sin6
->sin6_addr
.s6_addr32
[3] = 0;
3594 /* Convert sockaddr_in6 into sockaddr_in. */
3596 in6_sin6_2_sin_in_sock(struct sockaddr
*nam
)
3598 struct sockaddr_in
*sin_p
;
3599 struct sockaddr_in6 sin6
;
3602 * Save original sockaddr_in6 addr and convert it
3605 sin6
= *(struct sockaddr_in6
*)(void *)nam
;
3606 sin_p
= (struct sockaddr_in
*)(void *)nam
;
3607 in6_sin6_2_sin(sin_p
, &sin6
);
3610 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
3612 in6_sin_2_v4mapsin6_in_sock(struct sockaddr
**nam
)
3614 struct sockaddr_in
*sin_p
;
3615 struct sockaddr_in6
*sin6_p
;
3617 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof (*sin6_p
), M_SONAME
,
3621 sin_p
= (struct sockaddr_in
*)(void *)*nam
;
3622 in6_sin_2_v4mapsin6(sin_p
, sin6_p
);
3623 FREE(*nam
, M_SONAME
);
3624 *nam
= (struct sockaddr
*)sin6_p
;
3630 * Posts in6_event_data message kernel events.
3632 * To get the same size of kev_in6_data between ILP32 and LP64 data models
3633 * we are using a special version of the in6_addrlifetime structure that
3634 * uses only 32 bits fields to be compatible with Leopard, and that
3635 * are large enough to span 68 years.
3638 in6_post_msg(struct ifnet
*ifp
, u_int32_t event_code
, struct in6_ifaddr
*ifa
,
3641 struct kev_msg ev_msg
;
3642 struct kev_in6_data in6_event_data
;
3643 struct in6_addrlifetime ia6_lt
;
3645 bzero(&in6_event_data
, sizeof (struct kev_in6_data
));
3646 bzero(&ev_msg
, sizeof (struct kev_msg
));
3647 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3648 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3649 ev_msg
.kev_subclass
= KEV_INET6_SUBCLASS
;
3650 ev_msg
.event_code
= event_code
;
3652 IFA_LOCK(&ifa
->ia_ifa
);
3653 in6_event_data
.ia_addr
= ifa
->ia_addr
;
3654 in6_event_data
.ia_net
= ifa
->ia_net
;
3655 in6_event_data
.ia_dstaddr
= ifa
->ia_dstaddr
;
3656 in6_event_data
.ia_prefixmask
= ifa
->ia_prefixmask
;
3657 in6_event_data
.ia_plen
= ifa
->ia_plen
;
3658 in6_event_data
.ia6_flags
= (u_int32_t
)ifa
->ia6_flags
;
3660 /* retrieve time as calendar time (last arg is 1) */
3661 in6ifa_getlifetime(ifa
, &ia6_lt
, 1);
3662 in6_event_data
.ia_lifetime
.ia6t_expire
= ia6_lt
.ia6t_expire
;
3663 in6_event_data
.ia_lifetime
.ia6t_preferred
= ia6_lt
.ia6t_preferred
;
3664 in6_event_data
.ia_lifetime
.ia6t_vltime
= ia6_lt
.ia6t_vltime
;
3665 in6_event_data
.ia_lifetime
.ia6t_pltime
= ia6_lt
.ia6t_pltime
;
3666 IFA_UNLOCK(&ifa
->ia_ifa
);
3669 (void) strlcpy(&in6_event_data
.link_data
.if_name
[0],
3670 ifp
->if_name
, IFNAMSIZ
);
3671 in6_event_data
.link_data
.if_family
= ifp
->if_family
;
3672 in6_event_data
.link_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
3676 memcpy(&in6_event_data
.ia_mac
, mac
,
3677 sizeof(in6_event_data
.ia_mac
));
3679 ev_msg
.dv
[0].data_ptr
= &in6_event_data
;
3680 ev_msg
.dv
[0].data_length
= sizeof (in6_event_data
);
3681 ev_msg
.dv
[1].data_length
= 0;
3683 dlil_post_complete_msg(NULL
, &ev_msg
);
3687 * Called as part of ip6_init
3690 in6_ifaddr_init(void)
3695 PE_parse_boot_argn("ifa_debug", &in6ifa_debug
, sizeof (in6ifa_debug
));
3697 in6ifa_size
= (in6ifa_debug
== 0) ? sizeof (struct in6_ifaddr
) :
3698 sizeof (struct in6_ifaddr_dbg
);
3700 in6ifa_zone
= zinit(in6ifa_size
, IN6IFA_ZONE_MAX
* in6ifa_size
,
3701 0, IN6IFA_ZONE_NAME
);
3702 if (in6ifa_zone
== NULL
) {
3703 panic("%s: failed allocating %s", __func__
, IN6IFA_ZONE_NAME
);
3706 zone_change(in6ifa_zone
, Z_EXPAND
, TRUE
);
3707 zone_change(in6ifa_zone
, Z_CALLERACCT
, FALSE
);
3709 lck_mtx_init(&in6ifa_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3710 TAILQ_INIT(&in6ifa_trash_head
);
3713 static struct in6_ifaddr
*
3714 in6_ifaddr_alloc(int how
)
3716 struct in6_ifaddr
*in6ifa
;
3718 in6ifa
= (how
== M_WAITOK
) ? zalloc(in6ifa_zone
) :
3719 zalloc_noblock(in6ifa_zone
);
3720 if (in6ifa
!= NULL
) {
3721 bzero(in6ifa
, in6ifa_size
);
3722 in6ifa
->ia_ifa
.ifa_free
= in6_ifaddr_free
;
3723 in6ifa
->ia_ifa
.ifa_debug
|= IFD_ALLOC
;
3724 ifa_lock_init(&in6ifa
->ia_ifa
);
3725 if (in6ifa_debug
!= 0) {
3726 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3727 (struct in6_ifaddr_dbg
*)in6ifa
;
3728 in6ifa
->ia_ifa
.ifa_debug
|= IFD_DEBUG
;
3729 in6ifa
->ia_ifa
.ifa_trace
= in6_ifaddr_trace
;
3730 in6ifa
->ia_ifa
.ifa_attached
= in6_ifaddr_attached
;
3731 in6ifa
->ia_ifa
.ifa_detached
= in6_ifaddr_detached
;
3732 ctrace_record(&in6ifa_dbg
->in6ifa_alloc
);
3740 in6_ifaddr_free(struct ifaddr
*ifa
)
3742 IFA_LOCK_ASSERT_HELD(ifa
);
3744 if (ifa
->ifa_refcnt
!= 0) {
3745 panic("%s: ifa %p bad ref cnt", __func__
, ifa
);
3747 } else if (!(ifa
->ifa_debug
& IFD_ALLOC
)) {
3748 panic("%s: ifa %p cannot be freed", __func__
, ifa
);
3751 if (ifa
->ifa_debug
& IFD_DEBUG
) {
3752 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3753 (struct in6_ifaddr_dbg
*)ifa
;
3754 ctrace_record(&in6ifa_dbg
->in6ifa_free
);
3755 bcopy(&in6ifa_dbg
->in6ifa
, &in6ifa_dbg
->in6ifa_old
,
3756 sizeof (struct in6_ifaddr
));
3757 if (ifa
->ifa_debug
& IFD_TRASHED
) {
3758 /* Become a regular mutex, just in case */
3759 IFA_CONVERT_LOCK(ifa
);
3760 lck_mtx_lock(&in6ifa_trash_lock
);
3761 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
,
3763 lck_mtx_unlock(&in6ifa_trash_lock
);
3764 ifa
->ifa_debug
&= ~IFD_TRASHED
;
3768 ifa_lock_destroy(ifa
);
3769 bzero(ifa
, sizeof (struct in6_ifaddr
));
3770 zfree(in6ifa_zone
, ifa
);
3774 in6_ifaddr_attached(struct ifaddr
*ifa
)
3776 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3778 IFA_LOCK_ASSERT_HELD(ifa
);
3780 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3781 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3784 if (ifa
->ifa_debug
& IFD_TRASHED
) {
3785 /* Become a regular mutex, just in case */
3786 IFA_CONVERT_LOCK(ifa
);
3787 lck_mtx_lock(&in6ifa_trash_lock
);
3788 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
3789 lck_mtx_unlock(&in6ifa_trash_lock
);
3790 ifa
->ifa_debug
&= ~IFD_TRASHED
;
3795 in6_ifaddr_detached(struct ifaddr
*ifa
)
3797 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3799 IFA_LOCK_ASSERT_HELD(ifa
);
3801 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3802 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3804 } else if (ifa
->ifa_debug
& IFD_TRASHED
) {
3805 panic("%s: ifa %p is already in trash list", __func__
, ifa
);
3808 ifa
->ifa_debug
|= IFD_TRASHED
;
3809 /* Become a regular mutex, just in case */
3810 IFA_CONVERT_LOCK(ifa
);
3811 lck_mtx_lock(&in6ifa_trash_lock
);
3812 TAILQ_INSERT_TAIL(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
3813 lck_mtx_unlock(&in6ifa_trash_lock
);
3817 in6_ifaddr_trace(struct ifaddr
*ifa
, int refhold
)
3819 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3824 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3825 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3829 cnt
= &in6ifa_dbg
->in6ifa_refhold_cnt
;
3830 tr
= in6ifa_dbg
->in6ifa_refhold
;
3832 cnt
= &in6ifa_dbg
->in6ifa_refrele_cnt
;
3833 tr
= in6ifa_dbg
->in6ifa_refrele
;
3836 idx
= atomic_add_16_ov(cnt
, 1) % IN6IFA_TRACE_HIST_SIZE
;
3837 ctrace_record(&tr
[idx
]);
3841 * Handle SIOCGASSOCIDS ioctl for PF_INET6 domain.
3844 in6_getassocids(struct socket
*so
, uint32_t *cnt
, user_addr_t aidp
)
3846 struct in6pcb
*in6p
= sotoin6pcb(so
);
3849 if (in6p
== NULL
|| in6p
->inp_state
== INPCB_STATE_DEAD
)
3852 /* IN6PCB has no concept of association */
3853 aid
= SAE_ASSOCID_ANY
;
3856 /* just asking how many there are? */
3857 if (aidp
== USER_ADDR_NULL
)
3860 return (copyout(&aid
, aidp
, sizeof (aid
)));
3864 * Handle SIOCGCONNIDS ioctl for PF_INET6 domain.
3867 in6_getconnids(struct socket
*so
, sae_associd_t aid
, uint32_t *cnt
,
3870 struct in6pcb
*in6p
= sotoin6pcb(so
);
3873 if (in6p
== NULL
|| in6p
->inp_state
== INPCB_STATE_DEAD
)
3876 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
)
3879 /* if connected, return 1 connection count */
3880 *cnt
= ((so
->so_state
& SS_ISCONNECTED
) ? 1 : 0);
3882 /* just asking how many there are? */
3883 if (cidp
== USER_ADDR_NULL
)
3886 /* if IN6PCB is connected, assign it connid 1 */
3887 cid
= ((*cnt
!= 0) ? 1 : SAE_CONNID_ANY
);
3889 return (copyout(&cid
, cidp
, sizeof (cid
)));
3893 * Handle SIOCGCONNINFO ioctl for PF_INET6 domain.
3896 in6_getconninfo(struct socket
*so
, sae_connid_t cid
, uint32_t *flags
,
3897 uint32_t *ifindex
, int32_t *soerror
, user_addr_t src
, socklen_t
*src_len
,
3898 user_addr_t dst
, socklen_t
*dst_len
, uint32_t *aux_type
,
3899 user_addr_t aux_data
, uint32_t *aux_len
)
3901 #pragma unused(aux_data)
3902 struct in6pcb
*in6p
= sotoin6pcb(so
);
3903 struct sockaddr_in6 sin6
;
3904 struct ifnet
*ifp
= NULL
;
3906 u_int32_t copy_len
= 0;
3909 * Don't test for INPCB_STATE_DEAD since this may be called
3910 * after SOF_PCBCLEARING is set, e.g. after tcp_close().
3917 if (cid
!= SAE_CONNID_ANY
&& cid
!= SAE_CONNID_ALL
&& cid
!= 1) {
3922 ifp
= in6p
->in6p_last_outifp
;
3923 *ifindex
= ((ifp
!= NULL
) ? ifp
->if_index
: 0);
3924 *soerror
= so
->so_error
;
3926 if (so
->so_state
& SS_ISCONNECTED
)
3927 *flags
|= (CIF_CONNECTED
| CIF_PREFERRED
);
3928 if (in6p
->in6p_flags
& INP_BOUND_IF
)
3929 *flags
|= CIF_BOUND_IF
;
3930 if (!(in6p
->in6p_flags
& INP_IN6ADDR_ANY
))
3931 *flags
|= CIF_BOUND_IP
;
3932 if (!(in6p
->in6p_flags
& INP_ANONPORT
))
3933 *flags
|= CIF_BOUND_PORT
;
3935 bzero(&sin6
, sizeof (sin6
));
3936 sin6
.sin6_len
= sizeof (sin6
);
3937 sin6
.sin6_family
= AF_INET6
;
3939 /* source address and port */
3940 sin6
.sin6_port
= in6p
->in6p_lport
;
3941 in6_recoverscope(&sin6
, &in6p
->in6p_laddr
, NULL
);
3942 if (*src_len
== 0) {
3943 *src_len
= sin6
.sin6_len
;
3945 if (src
!= USER_ADDR_NULL
) {
3946 copy_len
= min(*src_len
, sizeof (sin6
));
3947 error
= copyout(&sin6
, src
, copy_len
);
3950 *src_len
= copy_len
;
3954 /* destination address and port */
3955 sin6
.sin6_port
= in6p
->in6p_fport
;
3956 in6_recoverscope(&sin6
, &in6p
->in6p_faddr
, NULL
);
3957 if (*dst_len
== 0) {
3958 *dst_len
= sin6
.sin6_len
;
3960 if (dst
!= USER_ADDR_NULL
) {
3961 copy_len
= min(*dst_len
, sizeof (sin6
));
3962 error
= copyout(&sin6
, dst
, copy_len
);
3965 *dst_len
= copy_len
;
3971 if (SOCK_PROTO(so
) == IPPROTO_TCP
) {
3972 struct conninfo_tcp tcp_ci
;
3974 *aux_type
= CIAUX_TCP
;
3975 if (*aux_len
== 0) {
3976 *aux_len
= sizeof (tcp_ci
);
3978 if (aux_data
!= USER_ADDR_NULL
) {
3979 copy_len
= min(*aux_len
, sizeof (tcp_ci
));
3980 bzero(&tcp_ci
, sizeof (tcp_ci
));
3981 tcp_getconninfo(so
, &tcp_ci
);
3982 error
= copyout(&tcp_ci
, aux_data
, copy_len
);
3985 *aux_len
= copy_len
;
3997 * The switch statement below does nothing at runtime, as it serves as a
3998 * compile time check to ensure that all of the socket 'u' ioctls (those
3999 * in the 'u' group going thru soo_ioctl) that are made available by the
4000 * networking stack is unique. This works as long as this routine gets
4001 * updated each time a new interface ioctl gets added.
4003 * Any failures at compile time indicates duplicated ioctl values.
4005 static __attribute__((unused
)) void
4006 in6ioctl_cassert(void)
4009 * This is equivalent to _CASSERT() and the compiler wouldn't
4010 * generate any instructions, thus for compile time only.
4012 switch ((u_long
)0) {
4015 /* bsd/netinet6/in6_var.h */
4016 case SIOCAADDRCTL_POLICY
:
4017 case SIOCDADDRCTL_POLICY
:
4018 case SIOCDRADD_IN6_32
:
4019 case SIOCDRADD_IN6_64
:
4020 case SIOCDRDEL_IN6_32
:
4021 case SIOCDRDEL_IN6_64
: