2 * Copyright (c) 2003-2014 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>
145 #include <net/pfvar.h>
149 * Definitions of some costant IP6 addresses.
151 const struct in6_addr in6addr_any
= IN6ADDR_ANY_INIT
;
152 const struct in6_addr in6addr_loopback
= IN6ADDR_LOOPBACK_INIT
;
153 const struct in6_addr in6addr_nodelocal_allnodes
=
154 IN6ADDR_NODELOCAL_ALLNODES_INIT
;
155 const struct in6_addr in6addr_linklocal_allnodes
=
156 IN6ADDR_LINKLOCAL_ALLNODES_INIT
;
157 const struct in6_addr in6addr_linklocal_allrouters
=
158 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT
;
159 const struct in6_addr in6addr_linklocal_allv2routers
=
160 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT
;
162 const struct in6_addr in6mask0
= IN6MASK0
;
163 const struct in6_addr in6mask7
= IN6MASK7
;
164 const struct in6_addr in6mask16
= IN6MASK16
;
165 const struct in6_addr in6mask32
= IN6MASK32
;
166 const struct in6_addr in6mask64
= IN6MASK64
;
167 const struct in6_addr in6mask96
= IN6MASK96
;
168 const struct in6_addr in6mask128
= IN6MASK128
;
170 const struct sockaddr_in6 sa6_any
= {
171 sizeof (sa6_any
), AF_INET6
, 0, 0, IN6ADDR_ANY_INIT
, 0
174 static int in6ctl_associd(struct socket
*, u_long
, caddr_t
);
175 static int in6ctl_connid(struct socket
*, u_long
, caddr_t
);
176 static int in6ctl_conninfo(struct socket
*, u_long
, caddr_t
);
177 static int in6ctl_llstart(struct ifnet
*, u_long
, caddr_t
);
178 static int in6ctl_llstop(struct ifnet
*);
179 static int in6ctl_cgastart(struct ifnet
*, u_long
, caddr_t
);
180 static int in6ctl_gifaddr(struct ifnet
*, struct in6_ifaddr
*, u_long
,
182 static int in6ctl_gifstat(struct ifnet
*, u_long
, struct in6_ifreq
*);
183 static int in6ctl_alifetime(struct in6_ifaddr
*, u_long
, struct in6_ifreq
*,
185 static int in6ctl_aifaddr(struct ifnet
*, struct in6_aliasreq
*);
186 static void in6ctl_difaddr(struct ifnet
*, struct in6_ifaddr
*);
187 static int in6_autoconf(struct ifnet
*, int);
188 static int in6_setrouter(struct ifnet
*, int);
189 static int in6_ifinit(struct ifnet
*, struct in6_ifaddr
*, int);
190 static int in6_ifaupdate_aux(struct in6_ifaddr
*, struct ifnet
*, int);
191 static void in6_unlink_ifa(struct in6_ifaddr
*, struct ifnet
*);
192 static struct in6_ifaddr
*in6_ifaddr_alloc(int);
193 static void in6_ifaddr_attached(struct ifaddr
*);
194 static void in6_ifaddr_detached(struct ifaddr
*);
195 static void in6_ifaddr_free(struct ifaddr
*);
196 static void in6_ifaddr_trace(struct ifaddr
*, int);
197 #if defined(__LP64__)
198 static void in6_llstartreq_32_to_64(struct in6_llstartreq_32
*,
199 struct in6_llstartreq_64
*);
201 static void in6_llstartreq_64_to_32(struct in6_llstartreq_64
*,
202 struct in6_llstartreq_32
*);
204 static struct in6_aliasreq
*in6_aliasreq_to_native(void *, int,
205 struct in6_aliasreq
*);
206 static struct in6_llstartreq
*in6_llstartreq_to_native(void *, int,
207 struct in6_llstartreq
*);
208 static int in6_to_kamescope(struct sockaddr_in6
*, struct ifnet
*);
210 static void in6_ifaddr_set_dadprogress(struct in6_ifaddr
*);
212 static int in6_getassocids(struct socket
*, uint32_t *, user_addr_t
);
213 static int in6_getconnids(struct socket
*, associd_t
, uint32_t *, user_addr_t
);
214 static int in6_getconninfo(struct socket
*, 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
;
221 extern int in6_init2done
;
223 #define IN6IFA_TRACE_HIST_SIZE 32 /* size of trace history */
226 __private_extern__
unsigned int in6ifa_trace_hist_size
= IN6IFA_TRACE_HIST_SIZE
;
228 struct in6_ifaddr_dbg
{
229 struct in6_ifaddr in6ifa
; /* in6_ifaddr */
230 struct in6_ifaddr in6ifa_old
; /* saved in6_ifaddr */
231 u_int16_t in6ifa_refhold_cnt
; /* # of IFA_ADDREF */
232 u_int16_t in6ifa_refrele_cnt
; /* # of IFA_REMREF */
234 * Alloc and free callers.
236 ctrace_t in6ifa_alloc
;
237 ctrace_t in6ifa_free
;
239 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
241 ctrace_t in6ifa_refhold
[IN6IFA_TRACE_HIST_SIZE
];
242 ctrace_t in6ifa_refrele
[IN6IFA_TRACE_HIST_SIZE
];
246 TAILQ_ENTRY(in6_ifaddr_dbg
) in6ifa_trash_link
;
249 /* List of trash in6_ifaddr entries protected by in6ifa_trash_lock */
250 static TAILQ_HEAD(, in6_ifaddr_dbg
) in6ifa_trash_head
;
251 static decl_lck_mtx_data(, in6ifa_trash_lock
);
254 static unsigned int in6ifa_debug
= 1; /* debugging (enabled) */
256 static unsigned int in6ifa_debug
; /* debugging (disabled) */
258 static unsigned int in6ifa_size
; /* size of zone element */
259 static struct zone
*in6ifa_zone
; /* zone for in6_ifaddr */
261 #define IN6IFA_ZONE_MAX 64 /* maximum elements in zone */
262 #define IN6IFA_ZONE_NAME "in6_ifaddr" /* zone name */
265 * Subroutine for in6_ifaddloop() and in6_ifremloop().
266 * This routine does actual work.
269 in6_ifloop_request(int cmd
, struct ifaddr
*ifa
)
271 struct sockaddr_in6 all1_sa
;
272 struct rtentry
*nrt
= NULL
;
275 bzero(&all1_sa
, sizeof (all1_sa
));
276 all1_sa
.sin6_family
= AF_INET6
;
277 all1_sa
.sin6_len
= sizeof (struct sockaddr_in6
);
278 all1_sa
.sin6_addr
= in6mask128
;
281 * We specify the address itself as the gateway, and set the
282 * RTF_LLINFO flag, so that the corresponding host route would have
283 * the flag, and thus applications that assume traditional behavior
284 * would be happy. Note that we assume the caller of the function
285 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
286 * which changes the outgoing interface to the loopback interface.
287 * ifa_addr for INET6 is set once during init; no need to hold lock.
289 lck_mtx_lock(rnh_lock
);
290 e
= rtrequest_locked(cmd
, ifa
->ifa_addr
, ifa
->ifa_addr
,
291 (struct sockaddr
*)&all1_sa
, RTF_UP
|RTF_HOST
|RTF_LLINFO
, &nrt
);
293 log(LOG_ERR
, "in6_ifloop_request: "
294 "%s operation failed for %s (errno=%d)\n",
295 cmd
== RTM_ADD
? "ADD" : "DELETE",
296 ip6_sprintf(&((struct in6_ifaddr
*)ifa
)->ia_addr
.sin6_addr
),
303 * Make sure rt_ifa be equal to IFA, the second argument of the
305 * We need this because when we refer to rt_ifa->ia6_flags in
306 * ip6_input, we assume that the rt_ifa points to the address instead
307 * of the loopback address.
309 if (cmd
== RTM_ADD
&& nrt
&& ifa
!= nrt
->rt_ifa
) {
314 * Report the addition/removal of the address to the routing socket.
315 * XXX: since we called rtinit for a p2p interface with a destination,
316 * we end up reporting twice in such a case. Should we rather
317 * omit the second report?
320 rt_newaddrmsg(cmd
, ifa
, e
, nrt
);
321 if (cmd
== RTM_DELETE
) {
325 /* the cmd must be RTM_ADD here */
326 RT_REMREF_LOCKED(nrt
);
330 lck_mtx_unlock(rnh_lock
);
334 * Add ownaddr as loopback rtentry. We previously add the route only if
335 * necessary (ex. on a p2p link). However, since we now manage addresses
336 * separately from prefixes, we should always add the route. We can't
337 * rely on the cloning mechanism from the corresponding interface route
341 in6_ifaddloop(struct ifaddr
*ifa
)
346 * If there is no loopback entry, allocate one. ifa_addr for
347 * INET6 is set once during init; no need to hold lock.
349 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
352 if (rt
== NULL
|| (rt
->rt_flags
& RTF_HOST
) == 0 ||
353 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) == 0) {
355 RT_REMREF_LOCKED(rt
);
358 in6_ifloop_request(RTM_ADD
, ifa
);
359 } else if (rt
!= NULL
) {
360 RT_REMREF_LOCKED(rt
);
366 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
370 in6_ifremloop(struct ifaddr
*ifa
)
372 struct in6_ifaddr
*ia
;
377 * Some of BSD variants do not remove cloned routes
378 * from an interface direct route, when removing the direct route
379 * (see comments in net/net_osdep.h). Even for variants that do remove
380 * cloned routes, they could fail to remove the cloned routes when
381 * we handle multple addresses that share a common prefix.
382 * So, we should remove the route corresponding to the deleted address
383 * regardless of the result of in6_is_ifloop_auto().
387 * Delete the entry only if exact one ifa exists. More than one ifa
388 * can exist if we assign a same single address to multiple
389 * (probably p2p) interfaces.
390 * XXX: we should avoid such a configuration in IPv6...
392 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
393 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
394 IFA_LOCK(&ia
->ia_ifa
);
395 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa
), &ia
->ia_addr
.sin6_addr
)) {
398 IFA_UNLOCK(&ia
->ia_ifa
);
402 IFA_UNLOCK(&ia
->ia_ifa
);
404 lck_rw_done(&in6_ifaddr_rwlock
);
408 * Before deleting, check if a corresponding loopbacked host
409 * route surely exists. With this check, we can avoid to
410 * delete an interface direct route whose destination is same
411 * as the address being removed. This can happen when removing
412 * a subnet-router anycast address on an interface attahced
413 * to a shared medium. ifa_addr for INET6 is set once during
414 * init; no need to hold lock.
416 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
419 if ((rt
->rt_flags
& RTF_HOST
) != 0 &&
420 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
421 RT_REMREF_LOCKED(rt
);
423 in6_ifloop_request(RTM_DELETE
, ifa
);
433 in6_mask2len(mask
, lim0
)
434 struct in6_addr
*mask
;
438 u_char
*lim
= lim0
, *p
;
440 /* ignore the scope_id part */
441 if (lim0
== NULL
|| lim0
- (u_char
*)mask
> sizeof (*mask
))
442 lim
= (u_char
*)mask
+ sizeof (*mask
);
443 for (p
= (u_char
*)mask
; p
< lim
; x
++, p
++) {
449 for (y
= 0; y
< 8; y
++) {
450 if ((*p
& (0x80 >> y
)) == 0)
456 * when the limit pointer is given, do a stricter check on the
460 if (y
!= 0 && (*p
& (0x00ff >> y
)) != 0)
462 for (p
= p
+ 1; p
< lim
; p
++)
471 in6_len2mask(mask
, len
)
472 struct in6_addr
*mask
;
477 bzero(mask
, sizeof (*mask
));
478 for (i
= 0; i
< len
/ 8; i
++)
479 mask
->s6_addr8
[i
] = 0xff;
481 mask
->s6_addr8
[i
] = (0xff00 >> (len
% 8)) & 0xff;
485 in6_aliasreq_64_to_32(struct in6_aliasreq_64
*src
, struct in6_aliasreq_32
*dst
)
487 bzero(dst
, sizeof (*dst
));
488 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof (dst
->ifra_name
));
489 dst
->ifra_addr
= src
->ifra_addr
;
490 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
491 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
492 dst
->ifra_flags
= src
->ifra_flags
;
493 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
494 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
495 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
496 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
500 in6_aliasreq_32_to_64(struct in6_aliasreq_32
*src
, struct in6_aliasreq_64
*dst
)
502 bzero(dst
, sizeof (*dst
));
503 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof (dst
->ifra_name
));
504 dst
->ifra_addr
= src
->ifra_addr
;
505 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
506 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
507 dst
->ifra_flags
= src
->ifra_flags
;
508 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
509 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
510 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
511 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
514 #if defined(__LP64__)
516 in6_llstartreq_32_to_64(struct in6_llstartreq_32
*src
,
517 struct in6_llstartreq_64
*dst
)
519 bzero(dst
, sizeof (*dst
));
520 bcopy(src
->llsr_name
, dst
->llsr_name
, sizeof (dst
->llsr_name
));
521 dst
->llsr_flags
= src
->llsr_flags
;
522 bcopy(src
->llsr_cgaprep
.cga_modifier
.octets
,
523 dst
->llsr_cgaprep
.cga_modifier
.octets
,
524 sizeof (dst
->llsr_cgaprep
.cga_modifier
.octets
));
525 dst
->llsr_cgaprep
.cga_security_level
=
526 src
->llsr_cgaprep
.cga_security_level
;
527 dst
->llsr_lifetime
.ia6t_expire
= src
->llsr_lifetime
.ia6t_expire
;
528 dst
->llsr_lifetime
.ia6t_preferred
= src
->llsr_lifetime
.ia6t_preferred
;
529 dst
->llsr_lifetime
.ia6t_vltime
= src
->llsr_lifetime
.ia6t_vltime
;
530 dst
->llsr_lifetime
.ia6t_pltime
= src
->llsr_lifetime
.ia6t_pltime
;
534 #if !defined(__LP64__)
536 in6_llstartreq_64_to_32(struct in6_llstartreq_64
*src
,
537 struct in6_llstartreq_32
*dst
)
539 bzero(dst
, sizeof (*dst
));
540 bcopy(src
->llsr_name
, dst
->llsr_name
, sizeof (dst
->llsr_name
));
541 dst
->llsr_flags
= src
->llsr_flags
;
542 bcopy(src
->llsr_cgaprep
.cga_modifier
.octets
,
543 dst
->llsr_cgaprep
.cga_modifier
.octets
,
544 sizeof (dst
->llsr_cgaprep
.cga_modifier
.octets
));
545 dst
->llsr_cgaprep
.cga_security_level
=
546 src
->llsr_cgaprep
.cga_security_level
;
547 dst
->llsr_lifetime
.ia6t_expire
= src
->llsr_lifetime
.ia6t_expire
;
548 dst
->llsr_lifetime
.ia6t_preferred
= src
->llsr_lifetime
.ia6t_preferred
;
549 dst
->llsr_lifetime
.ia6t_vltime
= src
->llsr_lifetime
.ia6t_vltime
;
550 dst
->llsr_lifetime
.ia6t_pltime
= src
->llsr_lifetime
.ia6t_pltime
;
554 static struct in6_aliasreq
*
555 in6_aliasreq_to_native(void *data
, int data_is_64
, struct in6_aliasreq
*dst
)
557 #if defined(__LP64__)
559 bcopy(data
, dst
, sizeof (*dst
));
561 in6_aliasreq_32_to_64((struct in6_aliasreq_32
*)data
,
562 (struct in6_aliasreq_64
*)dst
);
565 in6_aliasreq_64_to_32((struct in6_aliasreq_64
*)data
,
566 (struct in6_aliasreq_32
*)dst
);
568 bcopy(data
, dst
, sizeof (*dst
));
569 #endif /* __LP64__ */
573 static struct in6_llstartreq
*
574 in6_llstartreq_to_native(void *data
, int is64
, struct in6_llstartreq
*dst
)
576 #if defined(__LP64__)
578 bcopy(data
, dst
, sizeof (*dst
));
580 in6_llstartreq_32_to_64((struct in6_llstartreq_32
*)data
,
581 (struct in6_llstartreq_64
*)dst
);
584 in6_llstartreq_64_to_32((struct in6_llstartreq_64
*)data
,
585 (struct in6_llstartreq_32
*)dst
);
587 bcopy(data
, dst
, sizeof (*dst
));
588 #endif /* __LP64__ */
592 static __attribute__((noinline
)) int
593 in6ctl_associd(struct socket
*so
, u_long cmd
, caddr_t data
)
597 struct so_aidreq32 a32
;
598 struct so_aidreq64 a64
;
604 case SIOCGASSOCIDS32
: { /* struct so_aidreq32 */
605 bcopy(data
, &u
.a32
, sizeof (u
.a32
));
606 error
= in6_getassocids(so
, &u
.a32
.sar_cnt
, u
.a32
.sar_aidp
);
608 bcopy(&u
.a32
, data
, sizeof (u
.a32
));
612 case SIOCGASSOCIDS64
: { /* struct so_aidreq64 */
613 bcopy(data
, &u
.a64
, sizeof (u
.a64
));
614 error
= in6_getassocids(so
, &u
.a64
.sar_cnt
, u
.a64
.sar_aidp
);
616 bcopy(&u
.a64
, data
, sizeof (u
.a64
));
628 static __attribute__((noinline
)) int
629 in6ctl_connid(struct socket
*so
, u_long cmd
, caddr_t data
)
633 struct so_cidreq32 c32
;
634 struct so_cidreq64 c64
;
640 case SIOCGCONNIDS32
: { /* struct so_cidreq32 */
641 bcopy(data
, &u
.c32
, sizeof (u
.c32
));
642 error
= in6_getconnids(so
, u
.c32
.scr_aid
, &u
.c32
.scr_cnt
,
645 bcopy(&u
.c32
, data
, sizeof (u
.c32
));
649 case SIOCGCONNIDS64
: { /* struct so_cidreq64 */
650 bcopy(data
, &u
.c64
, sizeof (u
.c64
));
651 error
= in6_getconnids(so
, u
.c64
.scr_aid
, &u
.c64
.scr_cnt
,
654 bcopy(&u
.c64
, data
, sizeof (u
.c64
));
666 static __attribute__((noinline
)) int
667 in6ctl_conninfo(struct socket
*so
, u_long cmd
, caddr_t data
)
671 struct so_cinforeq32 ci32
;
672 struct so_cinforeq64 ci64
;
678 case SIOCGCONNINFO32
: { /* struct so_cinforeq32 */
679 bcopy(data
, &u
.ci32
, sizeof (u
.ci32
));
680 error
= in6_getconninfo(so
, u
.ci32
.scir_cid
, &u
.ci32
.scir_flags
,
681 &u
.ci32
.scir_ifindex
, &u
.ci32
.scir_error
, u
.ci32
.scir_src
,
682 &u
.ci32
.scir_src_len
, u
.ci32
.scir_dst
, &u
.ci32
.scir_dst_len
,
683 &u
.ci32
.scir_aux_type
, u
.ci32
.scir_aux_data
,
684 &u
.ci32
.scir_aux_len
);
686 bcopy(&u
.ci32
, data
, sizeof (u
.ci32
));
690 case SIOCGCONNINFO64
: { /* struct so_cinforeq64 */
691 bcopy(data
, &u
.ci64
, sizeof (u
.ci64
));
692 error
= in6_getconninfo(so
, u
.ci64
.scir_cid
, &u
.ci64
.scir_flags
,
693 &u
.ci64
.scir_ifindex
, &u
.ci64
.scir_error
, u
.ci64
.scir_src
,
694 &u
.ci64
.scir_src_len
, u
.ci64
.scir_dst
, &u
.ci64
.scir_dst_len
,
695 &u
.ci64
.scir_aux_type
, u
.ci64
.scir_aux_data
,
696 &u
.ci64
.scir_aux_len
);
698 bcopy(&u
.ci64
, data
, sizeof (u
.ci64
));
710 static __attribute__((noinline
)) int
711 in6ctl_llstart(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
713 struct in6_aliasreq sifra
, *ifra
= NULL
;
720 case SIOCLL_START_32
: /* struct in6_aliasreq_32 */
721 case SIOCLL_START_64
: /* struct in6_aliasreq_64 */
722 is64
= (cmd
== SIOCLL_START_64
);
724 * Convert user ifra to the kernel form, when appropriate.
725 * This allows the conversion between different data models
726 * to be centralized, so that it can be passed around to other
727 * routines that are expecting the kernel form.
729 ifra
= in6_aliasreq_to_native(data
, is64
, &sifra
);
732 * NOTE: All the interface specific DLIL attachements should
733 * be done here. They are currently done in in6_ifattach_aux()
734 * for the interfaces that need it.
736 if ((ifp
->if_eflags
& IFEF_NOAUTOIPV6LL
) != 0 &&
737 ifra
->ifra_addr
.sin6_family
== AF_INET6
&&
738 /* Only check ifra_dstaddr if valid */
739 (ifra
->ifra_dstaddr
.sin6_len
== 0 ||
740 ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
)) {
741 /* some interfaces may provide LinkLocal addresses */
742 error
= in6_ifattach_aliasreq(ifp
, NULL
, ifra
);
744 error
= in6_ifattach_aliasreq(ifp
, NULL
, NULL
);
747 in6_if_up_dad_start(ifp
);
758 static __attribute__((noinline
)) int
759 in6ctl_llstop(struct ifnet
*ifp
)
761 struct in6_ifaddr
*ia
;
762 struct nd_prefix pr0
, *pr
;
766 /* Remove link local addresses from interface */
767 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
770 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
774 IFA_LOCK(&ia
->ia_ifa
);
775 if (IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
)) {
776 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
777 IFA_UNLOCK(&ia
->ia_ifa
);
778 lck_rw_done(&in6_ifaddr_rwlock
);
779 in6_purgeaddr(&ia
->ia_ifa
);
780 IFA_REMREF(&ia
->ia_ifa
); /* for us */
781 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
783 * Purging the address caused in6_ifaddr_rwlock
784 * to be dropped and reacquired;
785 * therefore search again from the beginning
786 * of in6_ifaddrs list.
791 IFA_UNLOCK(&ia
->ia_ifa
);
794 lck_rw_done(&in6_ifaddr_rwlock
);
796 /* Delete the link local prefix */
797 bzero(&pr0
, sizeof(pr0
));
800 pr0
.ndpr_prefix
.sin6_addr
.s6_addr16
[0] = IPV6_ADDR_INT16_ULL
;
801 in6_setscope(&pr0
.ndpr_prefix
.sin6_addr
, ifp
, NULL
);
802 pr
= nd6_prefix_lookup(&pr0
);
804 lck_mtx_lock(nd6_mutex
);
808 NDPR_REMREF(pr
); /* Drop the reference from lookup */
809 lck_mtx_unlock(nd6_mutex
);
815 static __attribute__((noinline
)) int
816 in6ctl_cgastart(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
818 struct in6_llstartreq llsr
;
824 case SIOCLL_CGASTART_32
: /* struct in6_llstartreq_32 */
825 case SIOCLL_CGASTART_64
: /* struct in6_llstartreq_64 */
826 is64
= (cmd
== SIOCLL_CGASTART_64
);
828 * Convert user llstartreq to the kernel form, when appropriate.
829 * This allows the conversion between different data models
830 * to be centralized, so that it can be passed around to other
831 * routines that are expecting the kernel form.
833 in6_llstartreq_to_native(data
, is64
, &llsr
);
836 * NOTE: All the interface specific DLIL attachements
837 * should be done here. They are currently done in
838 * in6_ifattach_llstartreq() for the interfaces that
841 error
= in6_ifattach_llstartreq(ifp
, &llsr
);
843 in6_if_up_dad_start(ifp
);
855 * Caller passes in the ioctl data pointer directly via "ifr", with the
856 * expectation that this routine always uses bcopy() or other byte-aligned
859 static __attribute__((noinline
)) int
860 in6ctl_gifaddr(struct ifnet
*ifp
, struct in6_ifaddr
*ia
, u_long cmd
,
861 struct in6_ifreq
*ifr
)
863 struct sockaddr_in6 addr
;
869 return (EADDRNOTAVAIL
);
872 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
873 IFA_LOCK(&ia
->ia_ifa
);
874 bcopy(&ia
->ia_addr
, &addr
, sizeof (addr
));
875 IFA_UNLOCK(&ia
->ia_ifa
);
876 if ((error
= sa6_recoverscope(&addr
, TRUE
)) != 0)
878 bcopy(&addr
, &ifr
->ifr_addr
, sizeof (addr
));
881 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
882 if (!(ifp
->if_flags
& IFF_POINTOPOINT
)) {
887 * XXX: should we check if ifa_dstaddr is NULL and return
890 IFA_LOCK(&ia
->ia_ifa
);
891 bcopy(&ia
->ia_dstaddr
, &addr
, sizeof (addr
));
892 IFA_UNLOCK(&ia
->ia_ifa
);
893 if ((error
= sa6_recoverscope(&addr
, TRUE
)) != 0)
895 bcopy(&addr
, &ifr
->ifr_dstaddr
, sizeof (addr
));
907 * Caller passes in the ioctl data pointer directly via "ifr", with the
908 * expectation that this routine always uses bcopy() or other byte-aligned
911 static __attribute__((noinline
)) int
912 in6ctl_gifstat(struct ifnet
*ifp
, u_long cmd
, struct in6_ifreq
*ifr
)
914 int error
= 0, index
;
917 index
= ifp
->if_index
;
920 case SIOCGIFSTAT_IN6
: /* struct in6_ifreq */
921 /* N.B.: if_inet6data is never freed once set. */
922 if (IN6_IFEXTRA(ifp
) == NULL
) {
923 /* return (EAFNOSUPPORT)? */
924 bzero(&ifr
->ifr_ifru
.ifru_stat
,
925 sizeof (ifr
->ifr_ifru
.ifru_stat
));
927 bcopy(&IN6_IFEXTRA(ifp
)->in6_ifstat
,
928 &ifr
->ifr_ifru
.ifru_stat
,
929 sizeof (ifr
->ifr_ifru
.ifru_stat
));
933 case SIOCGIFSTAT_ICMP6
: /* struct in6_ifreq */
934 /* N.B.: if_inet6data is never freed once set. */
935 if (IN6_IFEXTRA(ifp
) == NULL
) {
936 /* return (EAFNOSUPPORT)? */
937 bzero(&ifr
->ifr_ifru
.ifru_stat
,
938 sizeof (ifr
->ifr_ifru
.ifru_icmp6stat
));
940 bcopy(&IN6_IFEXTRA(ifp
)->icmp6_ifstat
,
941 &ifr
->ifr_ifru
.ifru_icmp6stat
,
942 sizeof (ifr
->ifr_ifru
.ifru_icmp6stat
));
955 * Caller passes in the ioctl data pointer directly via "ifr", with the
956 * expectation that this routine always uses bcopy() or other byte-aligned
959 static __attribute__((noinline
)) int
960 in6ctl_alifetime(struct in6_ifaddr
*ia
, u_long cmd
, struct in6_ifreq
*ifr
,
963 uint64_t timenow
= net_uptime();
964 struct in6_addrlifetime ia6_lt
;
965 struct timeval caltime
;
969 return (EADDRNOTAVAIL
);
972 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
973 IFA_LOCK(&ia
->ia_ifa
);
974 /* retrieve time as calendar time (last arg is 1) */
975 in6ifa_getlifetime(ia
, &ia6_lt
, 1);
977 struct in6_addrlifetime_64 lt
;
979 bzero(<
, sizeof (lt
));
980 lt
.ia6t_expire
= ia6_lt
.ia6t_expire
;
981 lt
.ia6t_preferred
= ia6_lt
.ia6t_preferred
;
982 lt
.ia6t_vltime
= ia6_lt
.ia6t_vltime
;
983 lt
.ia6t_pltime
= ia6_lt
.ia6t_pltime
;
984 bcopy(<
, &ifr
->ifr_ifru
.ifru_lifetime
, sizeof (lt
));
986 struct in6_addrlifetime_32 lt
;
988 bzero(<
, sizeof (lt
));
989 lt
.ia6t_expire
= (uint32_t)ia6_lt
.ia6t_expire
;
990 lt
.ia6t_preferred
= (uint32_t)ia6_lt
.ia6t_preferred
;
991 lt
.ia6t_vltime
= (uint32_t)ia6_lt
.ia6t_vltime
;
992 lt
.ia6t_pltime
= (uint32_t)ia6_lt
.ia6t_pltime
;
993 bcopy(<
, &ifr
->ifr_ifru
.ifru_lifetime
, sizeof (lt
));
995 IFA_UNLOCK(&ia
->ia_ifa
);
998 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
999 getmicrotime(&caltime
);
1001 /* sanity for overflow - beware unsigned */
1003 struct in6_addrlifetime_64 lt
;
1005 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1006 if (lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
&&
1007 lt
.ia6t_vltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1011 if (lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
&&
1012 lt
.ia6t_pltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1017 struct in6_addrlifetime_32 lt
;
1019 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1020 if (lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
&&
1021 lt
.ia6t_vltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1025 if (lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
&&
1026 lt
.ia6t_pltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1032 IFA_LOCK(&ia
->ia_ifa
);
1034 struct in6_addrlifetime_64 lt
;
1036 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1037 ia6_lt
.ia6t_expire
= lt
.ia6t_expire
;
1038 ia6_lt
.ia6t_preferred
= lt
.ia6t_preferred
;
1039 ia6_lt
.ia6t_vltime
= lt
.ia6t_vltime
;
1040 ia6_lt
.ia6t_pltime
= lt
.ia6t_pltime
;
1042 struct in6_addrlifetime_32 lt
;
1044 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1045 ia6_lt
.ia6t_expire
= (uint32_t)lt
.ia6t_expire
;
1046 ia6_lt
.ia6t_preferred
= (uint32_t)lt
.ia6t_preferred
;
1047 ia6_lt
.ia6t_vltime
= lt
.ia6t_vltime
;
1048 ia6_lt
.ia6t_pltime
= lt
.ia6t_pltime
;
1051 if (ia6_lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
)
1052 ia6_lt
.ia6t_expire
= timenow
+ ia6_lt
.ia6t_vltime
;
1054 ia6_lt
.ia6t_expire
= 0;
1056 if (ia6_lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
)
1057 ia6_lt
.ia6t_preferred
= timenow
+ ia6_lt
.ia6t_pltime
;
1059 ia6_lt
.ia6t_preferred
= 0;
1061 in6ifa_setlifetime(ia
, &ia6_lt
);
1062 IFA_UNLOCK(&ia
->ia_ifa
);
1073 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(void *)(ifa))
1076 * Generic INET6 control operations (ioctl's).
1078 * ifp is NULL if not an interface-specific ioctl.
1080 * Most of the routines called to handle the ioctls would end up being
1081 * tail-call optimized, which unfortunately causes this routine to
1082 * consume too much stack space; this is the reason for the "noinline"
1083 * attribute used on those routines.
1085 * If called directly from within the networking stack (as opposed to via
1086 * pru_control), the socket parameter may be NULL.
1089 in6_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
1092 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
1093 struct in6_aliasreq sifra
, *ifra
= NULL
;
1094 struct in6_ifaddr
*ia
= NULL
;
1095 struct sockaddr_in6 sin6
, *sa6
= NULL
;
1096 boolean_t privileged
= (proc_suser(p
) == 0);
1097 boolean_t p64
= proc_is64bit(p
);
1098 boolean_t so_unlocked
= FALSE
;
1099 int intval
, error
= 0;
1101 /* In case it's NULL, make sure it came from the kernel */
1102 VERIFY(so
!= NULL
|| p
== kernproc
);
1105 * ioctls which don't require ifp, may require socket.
1108 case SIOCAADDRCTL_POLICY
: /* struct in6_addrpolicy */
1109 case SIOCDADDRCTL_POLICY
: /* struct in6_addrpolicy */
1112 return (in6_src_ioctl(cmd
, data
));
1115 case SIOCDRADD_IN6_32
: /* struct in6_defrouter_32 */
1116 case SIOCDRADD_IN6_64
: /* struct in6_defrouter_64 */
1117 case SIOCDRDEL_IN6_32
: /* struct in6_defrouter_32 */
1118 case SIOCDRDEL_IN6_64
: /* struct in6_defrouter_64 */
1121 return (defrtrlist_ioctl(cmd
, data
));
1124 case SIOCGASSOCIDS32
: /* struct so_aidreq32 */
1125 case SIOCGASSOCIDS64
: /* struct so_aidreq64 */
1126 return (in6ctl_associd(so
, cmd
, data
));
1129 case SIOCGCONNIDS32
: /* struct so_cidreq32 */
1130 case SIOCGCONNIDS64
: /* struct so_cidreq64 */
1131 return (in6ctl_connid(so
, cmd
, data
));
1134 case SIOCGCONNINFO32
: /* struct so_cinforeq32 */
1135 case SIOCGCONNINFO64
: /* struct so_cinforeq64 */
1136 return (in6ctl_conninfo(so
, cmd
, data
));
1141 * The rest of ioctls require ifp; reject if we don't have one;
1142 * return ENXIO to be consistent with ifioctl().
1148 * ioctls which require ifp but not interface address.
1151 case SIOCAUTOCONF_START
: /* struct in6_ifreq */
1154 return (in6_autoconf(ifp
, TRUE
));
1157 case SIOCAUTOCONF_STOP
: /* struct in6_ifreq */
1160 return (in6_autoconf(ifp
, FALSE
));
1163 case SIOCLL_START_32
: /* struct in6_aliasreq_32 */
1164 case SIOCLL_START_64
: /* struct in6_aliasreq_64 */
1167 return (in6ctl_llstart(ifp
, cmd
, data
));
1170 case SIOCLL_STOP
: /* struct in6_ifreq */
1173 return (in6ctl_llstop(ifp
));
1176 case SIOCSETROUTERMODE_IN6
: /* struct in6_ifreq */
1180 bcopy(&((struct in6_ifreq
*)(void *)data
)->ifr_intval
,
1181 &intval
, sizeof (intval
));
1183 return (in6_setrouter(ifp
, intval
));
1186 case SIOCPROTOATTACH_IN6_32
: /* struct in6_aliasreq_32 */
1187 case SIOCPROTOATTACH_IN6_64
: /* struct in6_aliasreq_64 */
1190 return (in6_domifattach(ifp
));
1193 case SIOCPROTODETACH_IN6
: /* struct in6_ifreq */
1197 /* Cleanup interface routes and addresses */
1200 if ((error
= proto_unplumb(PF_INET6
, ifp
)))
1201 log(LOG_ERR
, "SIOCPROTODETACH_IN6: %s error=%d\n",
1202 if_name(ifp
), error
);
1206 case SIOCSNDFLUSH_IN6
: /* struct in6_ifreq */
1207 case SIOCSPFXFLUSH_IN6
: /* struct in6_ifreq */
1208 case SIOCSRTRFLUSH_IN6
: /* struct in6_ifreq */
1209 case SIOCSDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
1210 case SIOCSDEFIFACE_IN6_64
: /* struct in6_ndifreq_64 */
1211 case SIOCSIFINFO_FLAGS
: /* struct in6_ndireq */
1215 case OSIOCGIFINFO_IN6
: /* struct in6_ondireq */
1216 case SIOCGIFINFO_IN6
: /* struct in6_ondireq */
1217 case SIOCGDRLST_IN6_32
: /* struct in6_drlist_32 */
1218 case SIOCGDRLST_IN6_64
: /* struct in6_drlist_64 */
1219 case SIOCGPRLST_IN6_32
: /* struct in6_prlist_32 */
1220 case SIOCGPRLST_IN6_64
: /* struct in6_prlist_64 */
1221 case SIOCGNBRINFO_IN6_32
: /* struct in6_nbrinfo_32 */
1222 case SIOCGNBRINFO_IN6_64
: /* struct in6_nbrinfo_64 */
1223 case SIOCGDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
1224 case SIOCGDEFIFACE_IN6_64
: /* struct in6_ndifreq_64 */
1225 return (nd6_ioctl(cmd
, data
, ifp
));
1228 case SIOCSIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1229 case SIOCDIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1230 case SIOCAIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1231 case SIOCCIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1232 case SIOCSGIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1233 case SIOCGIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1235 "prefix ioctls are now invalidated. "
1236 "please use ifconfig.\n");
1237 return (EOPNOTSUPP
);
1240 case SIOCSSCOPE6
: /* struct in6_ifreq (deprecated) */
1241 case SIOCGSCOPE6
: /* struct in6_ifreq (deprecated) */
1242 case SIOCGSCOPE6DEF
: /* struct in6_ifreq (deprecated) */
1243 return (EOPNOTSUPP
);
1246 case SIOCLL_CGASTART_32
: /* struct in6_llstartreq_32 */
1247 case SIOCLL_CGASTART_64
: /* struct in6_llstartreq_64 */
1250 return (in6ctl_cgastart(ifp
, cmd
, data
));
1253 case SIOCGIFSTAT_IN6
: /* struct in6_ifreq */
1254 case SIOCGIFSTAT_ICMP6
: /* struct in6_ifreq */
1255 return (in6ctl_gifstat(ifp
, cmd
, ifr
));
1260 * ioctls which require interface address; obtain sockaddr_in6.
1263 case SIOCSIFADDR_IN6
: /* struct in6_ifreq (deprecated) */
1264 case SIOCSIFDSTADDR_IN6
: /* struct in6_ifreq (deprecated) */
1265 case SIOCSIFNETMASK_IN6
: /* struct in6_ifreq (deprecated) */
1267 * Since IPv6 allows a node to assign multiple addresses
1268 * on a single interface, SIOCSIFxxx ioctls are deprecated.
1270 /* we decided to obsolete this command (20000704) */
1271 return (EOPNOTSUPP
);
1274 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1275 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1279 * Convert user ifra to the kernel form, when appropriate.
1280 * This allows the conversion between different data models
1281 * to be centralized, so that it can be passed around to other
1282 * routines that are expecting the kernel form.
1284 ifra
= in6_aliasreq_to_native(data
,
1285 (cmd
== SIOCAIFADDR_IN6_64
), &sifra
);
1286 bcopy(&ifra
->ifra_addr
, &sin6
, sizeof (sin6
));
1290 case SIOCDIFADDR_IN6
: /* struct in6_ifreq */
1291 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
1295 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
1296 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
1297 case SIOCGIFNETMASK_IN6
: /* struct in6_ifreq */
1298 case SIOCGIFAFLAG_IN6
: /* struct in6_ifreq */
1299 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
1300 bcopy(&ifr
->ifr_addr
, &sin6
, sizeof (sin6
));
1306 * Find address for this interface, if it exists.
1308 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
1309 * only, and used the first interface address as the target of other
1310 * operations (without checking ifra_addr). This was because netinet
1311 * code/API assumed at most 1 interface address per interface.
1312 * Since IPv6 allows a node to assign multiple addresses
1313 * on a single interface, we almost always look and check the
1314 * presence of ifra_addr, and reject invalid ones here.
1315 * It also decreases duplicated code among SIOC*_IN6 operations.
1318 if (sa6
!= NULL
&& sa6
->sin6_family
== AF_INET6
) {
1319 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
1320 if (sa6
->sin6_addr
.s6_addr16
[1] == 0) {
1321 /* link ID is not embedded by the user */
1322 sa6
->sin6_addr
.s6_addr16
[1] =
1323 htons(ifp
->if_index
);
1324 } else if (sa6
->sin6_addr
.s6_addr16
[1] !=
1325 htons(ifp
->if_index
)) {
1326 return (EINVAL
); /* link ID contradicts */
1328 if (sa6
->sin6_scope_id
) {
1329 if (sa6
->sin6_scope_id
!=
1330 (u_int32_t
)ifp
->if_index
)
1332 sa6
->sin6_scope_id
= 0; /* XXX: good way? */
1336 * Any failures from this point on must take into account
1337 * a non-NULL "ia" with an outstanding reference count, and
1338 * therefore requires IFA_REMREF. Jump to "done" label
1339 * instead of calling return if "ia" is valid.
1341 ia
= in6ifa_ifpwithaddr(ifp
, &sa6
->sin6_addr
);
1345 * SIOCDIFADDR_IN6/SIOCAIFADDR_IN6 specific tests.
1348 case SIOCDIFADDR_IN6
: /* struct in6_ifreq */
1350 return (EADDRNOTAVAIL
);
1352 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1353 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1354 VERIFY(sa6
!= NULL
);
1356 * We always require users to specify a valid IPv6 address for
1357 * the corresponding operation. Use "sa6" instead of "ifra"
1358 * since SIOCDIFADDR_IN6 falls thru above.
1360 if (sa6
->sin6_family
!= AF_INET6
||
1361 sa6
->sin6_len
!= sizeof (struct sockaddr_in6
)) {
1362 error
= EAFNOSUPPORT
;
1369 * Unlock the socket since ifnet_ioctl() may be invoked by
1370 * one of the ioctl handlers below. Socket will be re-locked
1371 * prior to returning.
1374 socket_unlock(so
, 0);
1379 * And finally process address-related ioctls.
1382 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
1383 /* This interface is basically deprecated. use SIOCGIFCONF. */
1385 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
1386 error
= in6ctl_gifaddr(ifp
, ia
, cmd
, ifr
);
1389 case SIOCGIFNETMASK_IN6
: /* struct in6_ifreq */
1391 IFA_LOCK(&ia
->ia_ifa
);
1392 bcopy(&ia
->ia_prefixmask
, &ifr
->ifr_addr
,
1393 sizeof (struct sockaddr_in6
));
1394 IFA_UNLOCK(&ia
->ia_ifa
);
1396 error
= EADDRNOTAVAIL
;
1400 case SIOCGIFAFLAG_IN6
: /* struct in6_ifreq */
1402 IFA_LOCK(&ia
->ia_ifa
);
1403 bcopy(&ia
->ia6_flags
, &ifr
->ifr_ifru
.ifru_flags6
,
1404 sizeof (ifr
->ifr_ifru
.ifru_flags6
));
1405 IFA_UNLOCK(&ia
->ia_ifa
);
1407 error
= EADDRNOTAVAIL
;
1411 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
1412 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
1413 error
= in6ctl_alifetime(ia
, cmd
, ifr
, p64
);
1416 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1417 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1418 error
= in6ctl_aifaddr(ifp
, ifra
);
1421 case SIOCDIFADDR_IN6
:
1422 in6ctl_difaddr(ifp
, ia
);
1426 error
= ifnet_ioctl(ifp
, PF_INET6
, cmd
, data
);
1432 IFA_REMREF(&ia
->ia_ifa
);
1439 static __attribute__((noinline
)) int
1440 in6ctl_aifaddr(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
)
1442 int i
, error
, addtmp
, plen
;
1443 struct nd_prefix pr0
, *pr
;
1444 struct in6_ifaddr
*ia
;
1446 VERIFY(ifp
!= NULL
&& ifra
!= NULL
);
1449 /* Attempt to attach the protocol, in case it isn't attached */
1450 error
= in6_domifattach(ifp
);
1452 /* PF_INET6 wasn't previously attached */
1453 error
= in6_ifattach_aliasreq(ifp
, NULL
, NULL
);
1457 in6_if_up_dad_start(ifp
);
1458 } else if (error
!= EEXIST
) {
1463 * First, make or update the interface address structure, and link it
1466 error
= in6_update_ifa(ifp
, ifra
, 0, &ia
);
1471 /* Now, make the prefix on-link on the interface. */
1472 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
, NULL
);
1477 * NOTE: We'd rather create the prefix before the address, but we need
1478 * at least one address to install the corresponding interface route,
1479 * so we configure the address first.
1483 * Convert mask to prefix length (prefixmask has already been validated
1484 * in in6_update_ifa().
1486 bzero(&pr0
, sizeof (pr0
));
1487 pr0
.ndpr_plen
= plen
;
1489 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
1490 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
1492 /* apply the mask for safety. */
1493 for (i
= 0; i
< 4; i
++) {
1494 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1495 ifra
->ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
1499 * Since we don't have an API to set prefix (not address) lifetimes, we
1500 * just use the same lifetimes as addresses. The (temporarily)
1501 * installed lifetimes can be overridden by later advertised RAs (when
1502 * accept_rtadv is non 0), which is an intended behavior.
1504 pr0
.ndpr_raf_onlink
= 1; /* should be configurable? */
1505 pr0
.ndpr_raf_auto
= !!(ifra
->ifra_flags
& IN6_IFF_AUTOCONF
);
1506 pr0
.ndpr_vltime
= ifra
->ifra_lifetime
.ia6t_vltime
;
1507 pr0
.ndpr_pltime
= ifra
->ifra_lifetime
.ia6t_pltime
;
1508 pr0
.ndpr_stateflags
|= NDPRF_STATIC
;
1509 lck_mtx_init(&pr0
.ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
1511 /* add the prefix if there's one. */
1512 if ((pr
= nd6_prefix_lookup(&pr0
)) == NULL
) {
1514 * nd6_prelist_add will install the corresponding interface
1517 error
= nd6_prelist_add(&pr0
, NULL
, &pr
, FALSE
);
1522 log(LOG_ERR
, "%s: nd6_prelist_add okay, but"
1523 " no prefix.\n", __func__
);
1529 IFA_LOCK(&ia
->ia_ifa
);
1531 /* if this is a new autoconfed addr */
1533 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 && ia
->ia6_ndpr
== NULL
) {
1536 VERIFY(pr
->ndpr_addrcnt
!= 0);
1538 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
1541 * If this is the first autoconf address from the prefix,
1542 * create a temporary address as well (when specified).
1544 addtmp
= (ip6_use_tempaddr
&& pr
->ndpr_addrcnt
== 1);
1548 IFA_UNLOCK(&ia
->ia_ifa
);
1552 e
= in6_tmpifadd(ia
, 1);
1554 log(LOG_NOTICE
, "%s: failed to create a"
1555 " temporary address, error=%d\n",
1560 * This might affect the status of autoconfigured addresses, that is,
1561 * this address might make other addresses detached.
1563 lck_mtx_lock(nd6_mutex
);
1564 pfxlist_onlink_check();
1565 lck_mtx_unlock(nd6_mutex
);
1567 /* Drop use count held above during lookup/add */
1572 IFA_REMREF(&ia
->ia_ifa
);
1576 static __attribute__((noinline
)) void
1577 in6ctl_difaddr(struct ifnet
*ifp
, struct in6_ifaddr
*ia
)
1580 struct nd_prefix pr0
, *pr
;
1582 VERIFY(ifp
!= NULL
&& ia
!= NULL
);
1585 * If the address being deleted is the only one that owns
1586 * the corresponding prefix, expire the prefix as well.
1587 * XXX: theoretically, we don't have to worry about such
1588 * relationship, since we separate the address management
1589 * and the prefix management. We do this, however, to provide
1590 * as much backward compatibility as possible in terms of
1591 * the ioctl operation.
1592 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
1594 IFA_LOCK(&ia
->ia_ifa
);
1595 bzero(&pr0
, sizeof (pr0
));
1597 pr0
.ndpr_plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
);
1598 if (pr0
.ndpr_plen
== 128) {
1599 IFA_UNLOCK(&ia
->ia_ifa
);
1602 pr0
.ndpr_prefix
= ia
->ia_addr
;
1603 pr0
.ndpr_mask
= ia
->ia_prefixmask
.sin6_addr
;
1604 for (i
= 0; i
< 4; i
++) {
1605 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1606 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
];
1608 IFA_UNLOCK(&ia
->ia_ifa
);
1610 * The logic of the following condition is a bit complicated.
1611 * We expire the prefix when
1612 * 1. the address obeys autoconfiguration and it is the
1613 * only owner of the associated prefix, or
1614 * 2. the address does not obey autoconf and there is no
1615 * other owner of the prefix.
1617 if ((pr
= nd6_prefix_lookup(&pr0
)) != NULL
) {
1618 IFA_LOCK(&ia
->ia_ifa
);
1620 if (((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
1621 pr
->ndpr_addrcnt
== 1) ||
1622 ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 &&
1623 pr
->ndpr_addrcnt
== 0)) {
1624 /* XXX: just for expiration */
1625 pr
->ndpr_expire
= 1;
1628 IFA_UNLOCK(&ia
->ia_ifa
);
1630 /* Drop use count held above during lookup */
1635 in6_purgeaddr(&ia
->ia_ifa
);
1638 static __attribute__((noinline
)) int
1639 in6_autoconf(struct ifnet
*ifp
, int enable
)
1643 VERIFY(ifp
!= NULL
);
1645 if (ifp
->if_flags
& IFF_LOOPBACK
)
1650 * An interface in IPv6 router mode implies that it
1651 * is either configured with a static IP address or
1652 * autoconfigured via a locally-generated RA. Prevent
1653 * SIOCAUTOCONF_START from being set in that mode.
1655 ifnet_lock_exclusive(ifp
);
1656 if (ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
1657 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADV
;
1660 ifp
->if_eflags
|= IFEF_ACCEPT_RTADV
;
1662 ifnet_lock_done(ifp
);
1664 struct in6_ifaddr
*ia
= NULL
;
1666 ifnet_lock_exclusive(ifp
);
1667 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADV
;
1668 ifnet_lock_done(ifp
);
1670 /* Remove autoconfigured address from interface */
1671 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1673 while (ia
!= NULL
) {
1674 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
1678 IFA_LOCK(&ia
->ia_ifa
);
1679 if (ia
->ia6_flags
& IN6_IFF_AUTOCONF
) {
1680 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
1681 IFA_UNLOCK(&ia
->ia_ifa
);
1682 lck_rw_done(&in6_ifaddr_rwlock
);
1683 in6_purgeaddr(&ia
->ia_ifa
);
1684 IFA_REMREF(&ia
->ia_ifa
); /* for us */
1685 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1687 * Purging the address caused in6_ifaddr_rwlock
1688 * to be dropped and reacquired;
1689 * therefore search again from the beginning
1690 * of in6_ifaddrs list.
1695 IFA_UNLOCK(&ia
->ia_ifa
);
1698 lck_rw_done(&in6_ifaddr_rwlock
);
1704 * Handle SIOCSETROUTERMODE_IN6 to set or clear the IPv6 router mode flag on
1705 * the interface. Entering or exiting this mode will result in the removal of
1706 * autoconfigured IPv6 addresses on the interface.
1708 static __attribute__((noinline
)) int
1709 in6_setrouter(struct ifnet
*ifp
, int enable
)
1711 VERIFY(ifp
!= NULL
);
1713 if (ifp
->if_flags
& IFF_LOOPBACK
)
1717 struct nd_ifinfo
*ndi
;
1719 lck_rw_lock_shared(nd_if_rwlock
);
1720 ndi
= ND_IFINFO(ifp
);
1721 if (ndi
!= NULL
&& ndi
->initialized
) {
1722 lck_mtx_lock(&ndi
->lock
);
1723 if (ndi
->flags
& ND6_IFF_PROXY_PREFIXES
) {
1724 /* No proxy if we are an advertising router */
1725 ndi
->flags
&= ~ND6_IFF_PROXY_PREFIXES
;
1726 lck_mtx_unlock(&ndi
->lock
);
1727 lck_rw_done(nd_if_rwlock
);
1728 (void) nd6_if_prproxy(ifp
, FALSE
);
1730 lck_mtx_unlock(&ndi
->lock
);
1731 lck_rw_done(nd_if_rwlock
);
1734 lck_rw_done(nd_if_rwlock
);
1738 ifnet_lock_exclusive(ifp
);
1740 ifp
->if_eflags
|= IFEF_IPV6_ROUTER
;
1742 ifp
->if_eflags
&= ~IFEF_IPV6_ROUTER
;
1744 ifnet_lock_done(ifp
);
1746 lck_mtx_lock(nd6_mutex
);
1747 defrouter_select(ifp
);
1748 lck_mtx_unlock(nd6_mutex
);
1750 if_allmulti(ifp
, enable
);
1752 return (in6_autoconf(ifp
, FALSE
));
1756 in6_to_kamescope(struct sockaddr_in6
*sin6
, struct ifnet
*ifp
)
1758 struct sockaddr_in6 tmp
;
1761 VERIFY(sin6
!= NULL
);
1764 error
= in6_recoverscope(&tmp
, &sin6
->sin6_addr
, ifp
);
1768 id
= in6_addr2scopeid(ifp
, &tmp
.sin6_addr
);
1769 if (tmp
.sin6_scope_id
== 0)
1770 tmp
.sin6_scope_id
= id
;
1771 else if (tmp
.sin6_scope_id
!= id
)
1772 return (EINVAL
); /* scope ID mismatch. */
1774 error
= in6_embedscope(&tmp
.sin6_addr
, &tmp
, NULL
, NULL
, NULL
);
1778 tmp
.sin6_scope_id
= 0;
1784 in6_ifaupdate_aux(struct in6_ifaddr
*ia
, struct ifnet
*ifp
, int ifaupflags
)
1786 struct sockaddr_in6 mltaddr
, mltmask
;
1787 struct in6_addr llsol
;
1789 struct in6_multi
*in6m_sol
;
1790 struct in6_multi_mship
*imm
;
1794 VERIFY(ifp
!= NULL
&& ia
!= NULL
);
1798 nd6log2((LOG_DEBUG
, "%s - %s ifp %s ia6_flags 0x%x ifaupflags 0x%x\n",
1800 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1801 if_name(ia
->ia_ifp
),
1806 * Mark the address as tentative before joining multicast addresses,
1807 * so that corresponding MLD responses would not have a tentative
1810 ia
->ia6_flags
&= ~IN6_IFF_DUPLICATED
; /* safety */
1811 if (in6if_do_dad(ifp
))
1812 in6_ifaddr_set_dadprogress(ia
);
1814 /* Join necessary multicast groups */
1815 if ((ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1817 /* join solicited multicast addr for new host id */
1818 bzero(&llsol
, sizeof (struct in6_addr
));
1819 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
1820 llsol
.s6_addr32
[1] = 0;
1821 llsol
.s6_addr32
[2] = htonl(1);
1822 llsol
.s6_addr32
[3] = ia
->ia_addr
.sin6_addr
.s6_addr32
[3];
1823 llsol
.s6_addr8
[12] = 0xff;
1824 if ((error
= in6_setscope(&llsol
, ifp
, NULL
)) != 0) {
1825 /* XXX: should not happen */
1826 log(LOG_ERR
, "%s: in6_setscope failed\n", __func__
);
1830 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
)) {
1832 * We need a random delay for DAD on the address
1833 * being configured. It also means delaying
1834 * transmission of the corresponding MLD report to
1835 * avoid report collision. [RFC 4862]
1837 delay
= random() % MAX_RTR_SOLICITATION_DELAY
;
1839 imm
= in6_joingroup(ifp
, &llsol
, &error
, delay
);
1841 nd6log((LOG_WARNING
,
1842 "%s: addmulti failed for %s on %s (errno=%d)\n",
1843 __func__
, ip6_sprintf(&llsol
), if_name(ifp
),
1848 in6m_sol
= imm
->i6mm_maddr
;
1849 /* take a refcount for this routine */
1850 IN6M_ADDREF(in6m_sol
);
1853 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
1856 bzero(&mltmask
, sizeof (mltmask
));
1857 mltmask
.sin6_len
= sizeof (struct sockaddr_in6
);
1858 mltmask
.sin6_family
= AF_INET6
;
1859 mltmask
.sin6_addr
= in6mask32
;
1860 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
1863 * join link-local all-nodes address
1865 bzero(&mltaddr
, sizeof (mltaddr
));
1866 mltaddr
.sin6_len
= sizeof (struct sockaddr_in6
);
1867 mltaddr
.sin6_family
= AF_INET6
;
1868 mltaddr
.sin6_addr
= in6addr_linklocal_allnodes
;
1869 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
)) != 0)
1870 goto unwind
; /* XXX: should not fail */
1873 * XXX: do we really need this automatic routes?
1874 * We should probably reconsider this stuff. Most applications
1875 * actually do not need the routes, since they usually specify
1876 * the outgoing interface.
1878 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
1879 ia
->ia_ifp
->if_index
);
1881 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
1882 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN
)) {
1888 error
= rtrequest_scoped(RTM_ADD
,
1889 (struct sockaddr
*)&mltaddr
,
1890 (struct sockaddr
*)&ia
->ia_addr
,
1891 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
1892 NULL
, ia
->ia_ifp
->if_index
);
1899 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
1901 nd6log((LOG_WARNING
,
1902 "%s: addmulti failed for %s on %s (errno=%d)\n",
1903 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
1904 if_name(ifp
), error
));
1909 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
1913 * join node information group address
1915 #define hostnamelen strlen(hostname)
1917 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
)) {
1919 * The spec doesn't say anything about delay for this
1920 * group, but the same logic should apply.
1922 delay
= random() % MAX_RTR_SOLICITATION_DELAY
;
1924 if (in6_nigroup(ifp
, hostname
, hostnamelen
, &mltaddr
.sin6_addr
)
1926 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
,
1927 delay
); /* XXX jinmei */
1929 nd6log((LOG_WARNING
,
1930 "%s: addmulti failed for %s on %s "
1932 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
1933 if_name(ifp
), error
));
1934 /* XXX not very fatal, go on... */
1938 LIST_INSERT_HEAD(&ia
->ia6_memberships
,
1946 * join interface-local all-nodes address.
1947 * (ff01::1%ifN, and ff01::%ifN/32)
1949 mltaddr
.sin6_addr
= in6addr_nodelocal_allnodes
;
1950 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
)) != 0)
1951 goto unwind
; /* XXX: should not fail */
1952 /* XXX: again, do we really need the route? */
1953 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
1954 ia
->ia_ifp
->if_index
);
1956 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
1957 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN
)) {
1963 error
= rtrequest_scoped(RTM_ADD
,
1964 (struct sockaddr
*)&mltaddr
,
1965 (struct sockaddr
*)&ia
->ia_addr
,
1966 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
1967 NULL
, ia
->ia_ifp
->if_index
);
1973 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
1975 nd6log((LOG_WARNING
,
1976 "%s: addmulti failed for %s on %s (errno=%d)\n",
1977 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
1978 if_name(ifp
), error
));
1983 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
1989 * Make sure to initialize ND6 information. this is to workaround
1990 * issues with interfaces with IPv6 addresses, which have never brought
1991 * up. We are assuming that it is safe to nd6_ifattach multiple times.
1992 * NOTE: this is how stf0 gets initialized
1994 if ((error
= nd6_ifattach(ifp
)) != 0)
1997 /* Ensure nd6_service() is scheduled as soon as it's convenient */
1998 ++nd6_sched_timeout_want
;
2001 * Perform DAD, if needed.
2002 * XXX It may be of use, if we can administratively
2006 if (in6if_do_dad(ifp
) && ((ifa
->ifa_flags
& IN6_IFF_NODAD
) == 0) &&
2007 (ia
->ia6_flags
& IN6_IFF_DADPROGRESS
)) {
2008 int mindelay
, maxdelay
;
2009 int *delayptr
, delayval
;
2014 * Avoid the DAD delay if the caller wants us to skip it.
2015 * This is not compliant with RFC 2461, but it's only being
2016 * used for signalling and not for actual DAD.
2018 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
) &&
2019 !(ia
->ia6_flags
& IN6_IFF_SWIFTDAD
)) {
2021 * We need to impose a delay before sending an NS
2022 * for DAD. Check if we also needed a delay for the
2023 * corresponding MLD message. If we did, the delay
2024 * should be larger than the MLD delay (this could be
2025 * relaxed a bit, but this simple logic is at least
2029 if (in6m_sol
!= NULL
) {
2030 IN6M_LOCK(in6m_sol
);
2031 if (in6m_sol
->in6m_state
==
2032 MLD_REPORTING_MEMBER
)
2033 mindelay
= in6m_sol
->in6m_timer
;
2034 IN6M_UNLOCK(in6m_sol
);
2036 maxdelay
= MAX_RTR_SOLICITATION_DELAY
* hz
;
2037 if (maxdelay
- mindelay
== 0)
2041 (random() % (maxdelay
- mindelay
)) +
2044 delayptr
= &delayval
;
2047 nd6_dad_start((struct ifaddr
*)ia
, delayptr
);
2056 in6_purgeaddr(&ia
->ia_ifa
);
2059 /* release reference held for this routine */
2060 if (in6m_sol
!= NULL
)
2061 IN6M_REMREF(in6m_sol
);
2066 * Request an IPv6 interface address. If the address is new, then it will be
2067 * constructed and appended to the interface address chains. The interface
2068 * address structure is optionally returned with a reference for the caller.
2071 in6_update_ifa(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
, int ifaupflags
,
2072 struct in6_ifaddr
**iar
)
2074 struct in6_addrlifetime ia6_lt
;
2075 struct in6_ifaddr
*ia
;
2077 struct ifaddr
*xifa
;
2078 struct in6_addrlifetime
*lt
;
2082 /* Sanity check parameters and initialize locals */
2083 VERIFY(ifp
!= NULL
&& ifra
!= NULL
&& iar
!= NULL
);
2089 * We always require users to specify a valid IPv6 address for
2090 * the corresponding operation.
2092 if (ifra
->ifra_addr
.sin6_family
!= AF_INET6
||
2093 ifra
->ifra_addr
.sin6_len
!= sizeof (struct sockaddr_in6
)) {
2094 error
= EAFNOSUPPORT
;
2098 /* Validate ifra_prefixmask.sin6_len is properly bounded. */
2099 if (ifra
->ifra_prefixmask
.sin6_len
== 0 ||
2100 ifra
->ifra_prefixmask
.sin6_len
> sizeof (struct sockaddr_in6
)) {
2105 /* Validate prefix length extracted from ifra_prefixmask structure. */
2106 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
2107 (u_char
*)&ifra
->ifra_prefixmask
+ ifra
->ifra_prefixmask
.sin6_len
);
2113 /* Validate lifetimes */
2114 lt
= &ifra
->ifra_lifetime
;
2115 if (lt
->ia6t_pltime
> lt
->ia6t_vltime
) {
2117 "%s: pltime 0x%x > vltime 0x%x for %s\n", __func__
,
2118 lt
->ia6t_pltime
, lt
->ia6t_vltime
,
2119 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
2123 if (lt
->ia6t_vltime
== 0) {
2125 * the following log might be noisy, but this is a typical
2126 * configuration mistake or a tool's bug.
2128 log(LOG_INFO
, "%s: valid lifetime is 0 for %s\n", __func__
,
2129 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
2133 * Before we lock the ifnet structure, we first check to see if the
2134 * address already exists. If so, then we don't allocate and link a
2137 ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
);
2142 * Validate destination address on interface types that require it.
2144 if ((ifp
->if_flags
& (IFF_LOOPBACK
|IFF_POINTOPOINT
)) != 0) {
2145 switch (ifra
->ifra_dstaddr
.sin6_family
) {
2148 /* noisy message for diagnostic purposes */
2150 "%s: prefix length < 128 with"
2151 " explicit dstaddr.\n", __func__
);
2161 error
= EAFNOSUPPORT
;
2164 } else if (ifra
->ifra_dstaddr
.sin6_family
!= AF_UNSPEC
) {
2166 "%s: dstaddr valid only on p2p and loopback interfaces.\n",
2172 timenow
= net_uptime();
2177 /* Is this the first new IPv6 address for the interface? */
2178 ifaupflags
|= IN6_IFAUPDATE_NEWADDR
;
2180 /* Allocate memory for IPv6 interface address structure. */
2181 how
= !(ifaupflags
& IN6_IFAUPDATE_NOWAIT
) ? M_WAITOK
: 0;
2182 ia
= in6_ifaddr_alloc(how
);
2191 * Initialize interface address structure.
2193 * Note well: none of these sockaddr_in6 structures contain a
2194 * valid sin6_port, sin6_flowinfo or even a sin6_scope_id field.
2195 * We still embed link-local scope identifiers at the end of an
2196 * arbitrary fe80::/32 prefix, for historical reasons. Also, the
2197 * ifa_dstaddr field is always non-NULL on point-to-point and
2198 * loopback interfaces, and conventionally points to a socket
2199 * address of AF_UNSPEC family when there is no destination.
2201 * Please enjoy the dancing sea turtle.
2203 IFA_ADDREF(ifa
); /* for this and optionally for caller */
2204 ifa
->ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
2205 if (ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
||
2206 (ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) != 0)
2207 ifa
->ifa_dstaddr
= (struct sockaddr
*)&ia
->ia_dstaddr
;
2208 ifa
->ifa_netmask
= (struct sockaddr
*)&ia
->ia_prefixmask
;
2210 ifa
->ifa_metric
= ifp
->if_metric
;
2211 ifa
->ifa_rtrequest
= nd6_rtrequest
;
2213 LIST_INIT(&ia
->ia6_memberships
);
2214 ia
->ia_addr
.sin6_family
= AF_INET6
;
2215 ia
->ia_addr
.sin6_len
= sizeof (ia
->ia_addr
);
2216 ia
->ia_addr
.sin6_addr
= ifra
->ifra_addr
.sin6_addr
;
2217 ia
->ia_prefixmask
.sin6_family
= AF_INET6
;
2218 ia
->ia_prefixmask
.sin6_len
= sizeof (ia
->ia_prefixmask
);
2219 ia
->ia_prefixmask
.sin6_addr
= ifra
->ifra_prefixmask
.sin6_addr
;
2220 error
= in6_to_kamescope(&ia
->ia_addr
, ifp
);
2223 if (ifa
->ifa_dstaddr
!= NULL
) {
2224 ia
->ia_dstaddr
= ifra
->ifra_dstaddr
;
2225 error
= in6_to_kamescope(&ia
->ia_dstaddr
, ifp
);
2230 /* Append to address chains */
2231 ifnet_lock_exclusive(ifp
);
2232 ifaupflags
|= IN6_IFAUPDATE_1STADDR
;
2233 TAILQ_FOREACH(xifa
, &ifp
->if_addrlist
, ifa_list
) {
2234 IFA_LOCK_SPIN(xifa
);
2235 if (xifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2237 ifaupflags
&= ~IN6_IFAUPDATE_1STADDR
;
2244 if_attach_ifa(ifp
, ifa
); /* holds reference for ifnet link */
2246 ifnet_lock_done(ifp
);
2248 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2249 if (in6_ifaddrs
!= NULL
) {
2250 struct in6_ifaddr
*iac
;
2251 for (iac
= in6_ifaddrs
; iac
->ia_next
!= NULL
;
2258 IFA_ADDREF(ifa
); /* hold for in6_ifaddrs link */
2259 lck_rw_done(&in6_ifaddr_rwlock
);
2262 ifaupflags
&= ~(IN6_IFAUPDATE_NEWADDR
|IN6_IFAUPDATE_1STADDR
);
2265 VERIFY(ia
!= NULL
&& ifa
== &ia
->ia_ifa
);
2269 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
2270 * to see if the address is deprecated or invalidated, but initialize
2271 * these members for applications.
2273 ia
->ia6_updatetime
= ia
->ia6_createtime
= timenow
;
2275 if (ia6_lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
)
2276 ia6_lt
.ia6t_expire
= timenow
+ ia6_lt
.ia6t_vltime
;
2278 ia6_lt
.ia6t_expire
= 0;
2279 if (ia6_lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
)
2280 ia6_lt
.ia6t_preferred
= timenow
+ ia6_lt
.ia6t_pltime
;
2282 ia6_lt
.ia6t_preferred
= 0;
2283 in6ifa_setlifetime(ia
, &ia6_lt
);
2286 * Backward compatibility - if IN6_IFF_DEPRECATED is set from the
2287 * userland, make it deprecated.
2289 if ((ia
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0) {
2290 ia
->ia6_lifetime
.ia6ti_pltime
= 0;
2291 ia
->ia6_lifetime
.ia6ti_preferred
= timenow
;
2295 * Do not delay sending neighbor solicitations when using optimistic
2296 * duplicate address detection, c.f. RFC 4429.
2298 if ((ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
) == 0)
2299 ifaupflags
|= IN6_IFAUPDATE_DADDELAY
;
2302 * Update flag or prefix length
2305 ia
->ia6_flags
= ifra
->ifra_flags
;
2307 /* Release locks (new address available to concurrent tasks) */
2310 /* Further initialization of the interface address */
2311 error
= in6_ifinit(ifp
, ia
, ifaupflags
);
2315 /* Finish updating the address while other tasks are working with it */
2316 error
= in6_ifaupdate_aux(ia
, ifp
, ifaupflags
);
2320 /* Return success (optionally w/ address for caller). */
2322 (void) ifnet_notify_address(ifp
, AF_INET6
);
2328 VERIFY(ifa
== &ia
->ia_ifa
);
2339 in6_purgeaddr(struct ifaddr
*ifa
)
2341 struct ifnet
*ifp
= ifa
->ifa_ifp
;
2342 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
2343 struct in6_multi_mship
*imm
;
2345 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2347 /* stop DAD processing */
2351 * delete route to the destination of the address being purged.
2352 * The interface must be p2p or loopback in this case.
2355 if ((ia
->ia_flags
& IFA_ROUTE
) && ia
->ia_plen
== 128) {
2359 rtf
= (ia
->ia_dstaddr
.sin6_family
== AF_INET6
) ? RTF_HOST
: 0;
2360 error
= rtinit(&(ia
->ia_ifa
), RTM_DELETE
, rtf
);
2362 log(LOG_ERR
, "in6_purgeaddr: failed to remove "
2363 "a route to the p2p destination: %s on %s, "
2365 ip6_sprintf(&ia
->ia_addr
.sin6_addr
), if_name(ifp
),
2367 /* proceed anyway... */
2370 ia
->ia_flags
&= ~IFA_ROUTE
;
2374 /* Remove ownaddr's loopback rtentry, if it exists. */
2375 in6_ifremloop(&(ia
->ia_ifa
));
2378 * leave from multicast groups we have joined for the interface
2381 while ((imm
= ia
->ia6_memberships
.lh_first
) != NULL
) {
2382 LIST_REMOVE(imm
, i6mm_chain
);
2384 in6_leavegroup(imm
);
2389 /* in6_unlink_ifa() will need exclusive access */
2390 in6_unlink_ifa(ia
, ifp
);
2391 in6_post_msg(ifp
, KEV_INET6_ADDR_DELETED
, ia
, NULL
);
2393 (void) ifnet_notify_address(ifp
, AF_INET6
);
2397 in6_unlink_ifa(struct in6_ifaddr
*ia
, struct ifnet
*ifp
)
2399 struct in6_ifaddr
*oia
;
2403 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2408 ifnet_lock_exclusive(ifp
);
2410 if (ifa
->ifa_debug
& IFD_ATTACHED
)
2411 if_detach_ifa(ifp
, ifa
);
2413 ifnet_lock_done(ifp
);
2416 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2418 if (oia
== (ia
= in6_ifaddrs
)) {
2419 in6_ifaddrs
= ia
->ia_next
;
2421 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
2424 ia
->ia_next
= oia
->ia_next
;
2427 log(LOG_NOTICE
, "%s: search failed.\n", __func__
);
2433 * When an autoconfigured address is being removed, release the
2434 * reference to the base prefix. Also, since the release might
2435 * affect the status of other (detached) addresses, call
2436 * pfxlist_onlink_check().
2440 if ((oia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0) {
2441 if (oia
->ia6_ndpr
== NULL
) {
2442 log(LOG_NOTICE
, "in6_unlink_ifa: autoconf'ed address "
2443 "0x%llx has no prefix\n",
2444 (uint64_t)VM_KERNEL_ADDRPERM(oia
));
2446 struct nd_prefix
*pr
= oia
->ia6_ndpr
;
2448 oia
->ia6_flags
&= ~IN6_IFF_AUTOCONF
;
2449 oia
->ia6_ndpr
= NULL
;
2451 VERIFY(pr
->ndpr_addrcnt
!= 0);
2454 NDPR_REMREF(pr
); /* release addr reference */
2457 lck_rw_done(&in6_ifaddr_rwlock
);
2458 lck_mtx_lock(nd6_mutex
);
2459 pfxlist_onlink_check();
2460 lck_mtx_unlock(nd6_mutex
);
2463 lck_rw_done(&in6_ifaddr_rwlock
);
2467 * release another refcnt for the link from in6_ifaddrs.
2468 * Do this only if it's not already unlinked in the event that we lost
2469 * the race, since in6_ifaddr_rwlock was momentarily dropped above.
2474 /* release reference held for this routine */
2477 /* invalidate route caches */
2478 routegenid_inet6_update();
2482 in6_purgeif(struct ifnet
*ifp
)
2484 struct in6_ifaddr
*ia
;
2489 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2491 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2493 while (ia
!= NULL
) {
2494 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
2498 IFA_ADDREF(&ia
->ia_ifa
); /* for us */
2499 lck_rw_done(&in6_ifaddr_rwlock
);
2500 in6_purgeaddr(&ia
->ia_ifa
);
2501 IFA_REMREF(&ia
->ia_ifa
); /* for us */
2502 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2504 * Purging the address would have caused
2505 * in6_ifaddr_rwlock to be dropped and reacquired;
2506 * therefore search again from the beginning
2507 * of in6_ifaddrs list.
2511 lck_rw_done(&in6_ifaddr_rwlock
);
2517 * Initialize an interface's internet6 address and routing table entry.
2520 in6_ifinit(struct ifnet
*ifp
, struct in6_ifaddr
*ia
, int ifaupflags
)
2529 * NOTE: SIOCSIFADDR is defined with struct ifreq as parameter,
2530 * but here we are sending it down to the interface with a pointer
2531 * to struct ifaddr, for legacy reasons.
2533 if ((ifaupflags
& IN6_IFAUPDATE_1STADDR
) != 0) {
2534 error
= ifnet_ioctl(ifp
, PF_INET6
, SIOCSIFADDR
, ia
);
2536 if (error
!= EOPNOTSUPP
)
2546 * If the destination address is specified for a point-to-point
2547 * interface, install a route to the destination as an interface
2550 if (!(ia
->ia_flags
& IFA_ROUTE
) && ia
->ia_plen
== 128 &&
2551 ia
->ia_dstaddr
.sin6_family
== AF_INET6
) {
2553 error
= rtinit(ifa
, RTM_ADD
, RTF_UP
| RTF_HOST
);
2557 ia
->ia_flags
|= IFA_ROUTE
;
2559 IFA_LOCK_ASSERT_HELD(ifa
);
2560 if (ia
->ia_plen
< 128) {
2562 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
2564 ia
->ia_flags
|= RTF_CLONING
;
2569 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
2570 if ((ifaupflags
& IN6_IFAUPDATE_NEWADDR
) != 0)
2573 /* invalidate route caches */
2574 routegenid_inet6_update();
2581 in6_purgeaddrs(struct ifnet
*ifp
)
2587 * Find an IPv6 interface link-local address specific to an interface.
2590 in6ifa_ifpforlinklocal(ifp
, ignoreflags
)
2596 ifnet_lock_shared(ifp
);
2597 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2600 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2604 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
))) {
2605 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2606 ignoreflags
) != 0) {
2610 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2616 ifnet_lock_done(ifp
);
2618 return ((struct in6_ifaddr
*)ifa
);
2622 * find the internet address corresponding to a given interface and address.
2625 in6ifa_ifpwithaddr(ifp
, addr
)
2627 struct in6_addr
*addr
;
2631 ifnet_lock_shared(ifp
);
2632 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2635 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2639 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(ifa
))) {
2640 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2646 ifnet_lock_done(ifp
);
2648 return ((struct in6_ifaddr
*)ifa
);
2652 in6ifa_prproxyaddr(struct in6_addr
*addr
)
2654 struct in6_ifaddr
*ia
;
2656 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2657 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2658 IFA_LOCK(&ia
->ia_ifa
);
2659 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(&ia
->ia_ifa
))) {
2660 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
2661 IFA_UNLOCK(&ia
->ia_ifa
);
2664 IFA_UNLOCK(&ia
->ia_ifa
);
2666 lck_rw_done(&in6_ifaddr_rwlock
);
2668 if (ia
!= NULL
&& !nd6_prproxy_ifaddr(ia
)) {
2669 IFA_REMREF(&ia
->ia_ifa
);
2677 in6ifa_getlifetime(struct in6_ifaddr
*ia6
, struct in6_addrlifetime
*t_dst
,
2680 struct in6_addrlifetime_i
*t_src
= &ia6
->ia6_lifetime
;
2681 struct timeval caltime
;
2683 t_dst
->ia6t_vltime
= t_src
->ia6ti_vltime
;
2684 t_dst
->ia6t_pltime
= t_src
->ia6ti_pltime
;
2685 t_dst
->ia6t_expire
= 0;
2686 t_dst
->ia6t_preferred
= 0;
2688 /* account for system time change */
2689 getmicrotime(&caltime
);
2690 t_src
->ia6ti_base_calendartime
+=
2691 NET_CALCULATE_CLOCKSKEW(caltime
,
2692 t_src
->ia6ti_base_calendartime
, net_uptime(),
2693 t_src
->ia6ti_base_uptime
);
2696 if (t_src
->ia6ti_expire
!= 0 &&
2697 t_src
->ia6ti_vltime
!= ND6_INFINITE_LIFETIME
)
2698 t_dst
->ia6t_expire
= t_src
->ia6ti_base_calendartime
+
2699 t_src
->ia6ti_expire
- t_src
->ia6ti_base_uptime
;
2701 if (t_src
->ia6ti_preferred
!= 0 &&
2702 t_src
->ia6ti_pltime
!= ND6_INFINITE_LIFETIME
)
2703 t_dst
->ia6t_preferred
= t_src
->ia6ti_base_calendartime
+
2704 t_src
->ia6ti_preferred
- t_src
->ia6ti_base_uptime
;
2706 if (t_src
->ia6ti_expire
!= 0 &&
2707 t_src
->ia6ti_vltime
!= ND6_INFINITE_LIFETIME
)
2708 t_dst
->ia6t_expire
= t_src
->ia6ti_expire
;
2710 if (t_src
->ia6ti_preferred
!= 0 &&
2711 t_src
->ia6ti_pltime
!= ND6_INFINITE_LIFETIME
)
2712 t_dst
->ia6t_preferred
= t_src
->ia6ti_preferred
;
2717 in6ifa_setlifetime(struct in6_ifaddr
*ia6
, struct in6_addrlifetime
*t_src
)
2719 struct in6_addrlifetime_i
*t_dst
= &ia6
->ia6_lifetime
;
2720 struct timeval caltime
;
2722 /* account for system time change */
2723 getmicrotime(&caltime
);
2724 t_dst
->ia6ti_base_calendartime
+=
2725 NET_CALCULATE_CLOCKSKEW(caltime
,
2726 t_dst
->ia6ti_base_calendartime
, net_uptime(),
2727 t_dst
->ia6ti_base_uptime
);
2729 /* trust the caller for the values */
2730 t_dst
->ia6ti_expire
= t_src
->ia6t_expire
;
2731 t_dst
->ia6ti_preferred
= t_src
->ia6t_preferred
;
2732 t_dst
->ia6ti_vltime
= t_src
->ia6t_vltime
;
2733 t_dst
->ia6ti_pltime
= t_src
->ia6t_pltime
;
2737 * Convert IP6 address to printable (loggable) representation.
2740 ip6_sprintf(const struct in6_addr
*addr
)
2742 static const char digits
[] = "0123456789abcdef";
2743 static int ip6round
= 0;
2744 static char ip6buf
[8][48];
2748 const u_short
*a
= (const u_short
*)addr
;
2754 ip6round
= (ip6round
+ 1) & 7;
2755 cp
= ip6buf
[ip6round
];
2757 for (i
= 0; i
< 8; i
++) {
2768 if (dcolon
== 0 && *(a
+ 1) == 0) {
2780 d
= (const u_char
*)a
;
2782 if ((n
= *d
>> 4) != 0) {
2786 if ((n
= *d
++ & 0xf) != 0 || zpad
) {
2790 if ((n
= *d
>> 4) != 0 || zpad
) {
2794 if ((n
= *d
& 0xf) != 0 || zpad
)
2800 return (ip6buf
[ip6round
]);
2804 in6addr_local(struct in6_addr
*in6
)
2807 struct sockaddr_in6 sin6
;
2810 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_SCOPE_LINKLOCAL(in6
))
2813 sin6
.sin6_family
= AF_INET6
;
2814 sin6
.sin6_len
= sizeof (sin6
);
2815 bcopy(in6
, &sin6
.sin6_addr
, sizeof (*in6
));
2816 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0);
2820 if (rt
->rt_gateway
->sa_family
== AF_LINK
)
2825 local
= in6_localaddr(in6
);
2831 in6_localaddr(struct in6_addr
*in6
)
2833 struct in6_ifaddr
*ia
;
2835 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
))
2838 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2839 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2840 IFA_LOCK_SPIN(&ia
->ia_ifa
);
2841 if (IN6_ARE_MASKED_ADDR_EQUAL(in6
, &ia
->ia_addr
.sin6_addr
,
2842 &ia
->ia_prefixmask
.sin6_addr
)) {
2843 IFA_UNLOCK(&ia
->ia_ifa
);
2844 lck_rw_done(&in6_ifaddr_rwlock
);
2847 IFA_UNLOCK(&ia
->ia_ifa
);
2849 lck_rw_done(&in6_ifaddr_rwlock
);
2854 in6_is_addr_deprecated(struct sockaddr_in6
*sa6
)
2856 struct in6_ifaddr
*ia
;
2858 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2859 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2860 IFA_LOCK_SPIN(&ia
->ia_ifa
);
2861 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
,
2863 (ia
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0) {
2864 IFA_UNLOCK(&ia
->ia_ifa
);
2865 lck_rw_done(&in6_ifaddr_rwlock
);
2866 return (1); /* true */
2868 /* XXX: do we still have to go thru the rest of the list? */
2869 IFA_UNLOCK(&ia
->ia_ifa
);
2872 lck_rw_done(&in6_ifaddr_rwlock
);
2873 return (0); /* false */
2877 * return length of part which dst and src are equal
2881 in6_matchlen(src
, dst
)
2882 struct in6_addr
*src
, *dst
;
2885 u_char
*s
= (u_char
*)src
, *d
= (u_char
*)dst
;
2886 u_char
*lim
= s
+ 16, r
;
2889 if ((r
= (*d
++ ^ *s
++)) != 0) {
2900 /* XXX: to be scope conscious */
2902 in6_are_prefix_equal(p1
, p2
, len
)
2903 struct in6_addr
*p1
, *p2
;
2906 int bytelen
, bitlen
;
2909 if (0 > len
|| len
> 128) {
2910 log(LOG_ERR
, "%s: invalid prefix length(%d)\n", __func__
, len
);
2917 if (bcmp(&p1
->s6_addr
, &p2
->s6_addr
, bytelen
))
2920 p1
->s6_addr
[bytelen
] >> (8 - bitlen
) !=
2921 p2
->s6_addr
[bytelen
] >> (8 - bitlen
))
2928 in6_prefixlen2mask(maskp
, len
)
2929 struct in6_addr
*maskp
;
2932 u_char maskarray
[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2933 int bytelen
, bitlen
, i
;
2936 if (0 > len
|| len
> 128) {
2937 log(LOG_ERR
, "%s: invalid prefix length(%d)\n", __func__
, len
);
2941 bzero(maskp
, sizeof (*maskp
));
2944 for (i
= 0; i
< bytelen
; i
++)
2945 maskp
->s6_addr
[i
] = 0xff;
2947 maskp
->s6_addr
[bytelen
] = maskarray
[bitlen
- 1];
2951 * return the best address out of the same scope
2954 in6_ifawithscope(struct ifnet
*oifp
, struct in6_addr
*dst
)
2956 int dst_scope
= in6_addrscope(dst
), src_scope
, best_scope
= 0;
2960 struct in6_ifaddr
*ifa_best
= NULL
;
2967 * We search for all addresses on all interfaces from the beginning.
2968 * Comparing an interface with the outgoing interface will be done
2969 * only at the final stage of tiebreaking.
2971 ifnet_head_lock_shared();
2972 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
2974 * We can never take an address that breaks the scope zone
2975 * of the destination.
2977 if (in6_addr2scopeid(ifp
, dst
) != in6_addr2scopeid(oifp
, dst
))
2980 ifnet_lock_shared(ifp
);
2981 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
2982 int tlen
= -1, dscopecmp
, bscopecmp
, matchcmp
;
2985 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2989 src_scope
= in6_addrscope(IFA_IN6(ifa
));
2992 * Don't use an address before completing DAD
2993 * nor a duplicated address.
2995 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3000 /* XXX: is there any case to allow anycasts? */
3001 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3006 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3012 * If this is the first address we find,
3015 if (ifa_best
== NULL
)
3019 * ifa_best is never NULL beyond this line except
3020 * within the block labeled "replace".
3024 * If ifa_best has a smaller scope than dst and
3025 * the current address has a larger one than
3026 * (or equal to) dst, always replace ifa_best.
3027 * Also, if the current address has a smaller scope
3028 * than dst, ignore it unless ifa_best also has a
3030 * Consequently, after the two if-clause below,
3031 * the followings must be satisfied:
3032 * (scope(src) < scope(dst) &&
3033 * scope(best) < scope(dst))
3035 * (scope(best) >= scope(dst) &&
3036 * scope(src) >= scope(dst))
3038 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0 &&
3039 IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) >= 0)
3040 goto replace
; /* (A) */
3041 if (IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) < 0 &&
3042 IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) >= 0) {
3047 * A deprecated address SHOULD NOT be used in new
3048 * communications if an alternate (non-deprecated)
3049 * address is available and has sufficient scope.
3050 * RFC 4862, Section 5.5.4.
3052 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3053 IN6_IFF_DEPRECATED
) {
3055 * Ignore any deprecated addresses if
3056 * specified by configuration.
3058 if (!ip6_use_deprecated
) {
3063 * If we have already found a non-deprecated
3064 * candidate, just ignore deprecated addresses.
3066 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
)
3074 * A non-deprecated address is always preferred
3075 * to a deprecated one regardless of scopes and
3076 * address matching (Note invariants ensured by the
3077 * conditions (A) and (B) above.)
3079 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) &&
3080 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3081 IN6_IFF_DEPRECATED
) == 0)
3085 * When we use temporary addresses described in
3086 * RFC 4941, we prefer temporary addresses to
3087 * public autoconf addresses. Again, note the
3088 * invariants from (A) and (B). Also note that we
3089 * don't have any preference between static addresses
3090 * and autoconf addresses (despite of whether or not
3091 * the latter is temporary or public.)
3093 if (ip6_use_tempaddr
) {
3094 struct in6_ifaddr
*ifat
;
3096 ifat
= (struct in6_ifaddr
*)ifa
;
3097 if ((ifa_best
->ia6_flags
&
3098 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3099 == IN6_IFF_AUTOCONF
&&
3101 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3102 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
)) {
3105 if ((ifa_best
->ia6_flags
&
3106 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3107 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
) &&
3109 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3110 == IN6_IFF_AUTOCONF
) {
3117 * At this point, we have two cases:
3118 * 1. we are looking at a non-deprecated address,
3119 * and ifa_best is also non-deprecated.
3120 * 2. we are looking at a deprecated address,
3121 * and ifa_best is also deprecated.
3122 * Also, we do not have to consider a case where
3123 * the scope of if_best is larger(smaller) than dst and
3124 * the scope of the current address is smaller(larger)
3125 * than dst. Such a case has already been covered.
3126 * Tiebreaking is done according to the following
3128 * - the scope comparison between the address and
3130 * - the scope comparison between the address and
3131 * ifa_best (bscopecmp)
3132 * - if the address match dst longer than ifa_best
3134 * - if the address is on the outgoing I/F (outI/F)
3136 * Roughly speaking, the selection policy is
3137 * - the most important item is scope. The same scope
3138 * is best. Then search for a larger scope.
3139 * Smaller scopes are the last resort.
3140 * - A deprecated address is chosen only when we have
3141 * no address that has an enough scope, but is
3142 * prefered to any addresses of smaller scopes
3143 * (this must be already done above.)
3144 * - addresses on the outgoing I/F are preferred to
3145 * ones on other interfaces if none of above
3146 * tiebreaks. In the table below, the column "bI"
3147 * means if the best_ifa is on the outgoing
3148 * interface, and the column "sI" means if the ifa
3149 * is on the outgoing interface.
3150 * - If there is no other reasons to choose one,
3151 * longest address match against dst is considered.
3153 * The precise decision table is as follows:
3154 * dscopecmp bscopecmp match bI oI | replace?
3155 * N/A equal N/A Y N | No (1)
3156 * N/A equal N/A N Y | Yes (2)
3157 * N/A equal larger N/A | Yes (3)
3158 * N/A equal !larger N/A | No (4)
3159 * larger larger N/A N/A | No (5)
3160 * larger smaller N/A N/A | Yes (6)
3161 * smaller larger N/A N/A | Yes (7)
3162 * smaller smaller N/A N/A | No (8)
3163 * equal smaller N/A N/A | Yes (9)
3164 * equal larger (already done at A above)
3166 dscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
);
3167 bscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, best_scope
);
3169 if (bscopecmp
== 0) {
3170 struct ifnet
*bifp
= ifa_best
->ia_ifp
;
3172 if (bifp
== oifp
&& ifp
!= oifp
) { /* (1) */
3176 if (bifp
!= oifp
&& ifp
== oifp
) /* (2) */
3180 * Both bifp and ifp are on the outgoing
3181 * interface, or both two are on a different
3182 * interface from the outgoing I/F.
3183 * now we need address matching against dst
3186 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3187 matchcmp
= tlen
- blen
;
3188 if (matchcmp
> 0) /* (3) */
3193 if (dscopecmp
> 0) {
3194 if (bscopecmp
> 0) { /* (5) */
3198 goto replace
; /* (6) */
3200 if (dscopecmp
< 0) {
3201 if (bscopecmp
> 0) /* (7) */
3207 /* now dscopecmp must be 0 */
3209 goto replace
; /* (9) */
3212 IFA_ADDREF_LOCKED(ifa
); /* for ifa_best */
3213 blen
= tlen
>= 0 ? tlen
:
3214 in6_matchlen(IFA_IN6(ifa
), dst
);
3216 in6_addrscope(&ifa2ia6(ifa
)->ia_addr
.sin6_addr
);
3219 IFA_REMREF(&ifa_best
->ia_ifa
);
3220 ifa_best
= (struct in6_ifaddr
*)ifa
;
3222 ifnet_lock_done(ifp
);
3226 /* count statistics for future improvements */
3227 if (ifa_best
== NULL
)
3228 ip6stat
.ip6s_sources_none
++;
3230 IFA_LOCK_SPIN(&ifa_best
->ia_ifa
);
3231 if (oifp
== ifa_best
->ia_ifp
)
3232 ip6stat
.ip6s_sources_sameif
[best_scope
]++;
3234 ip6stat
.ip6s_sources_otherif
[best_scope
]++;
3236 if (best_scope
== dst_scope
)
3237 ip6stat
.ip6s_sources_samescope
[best_scope
]++;
3239 ip6stat
.ip6s_sources_otherscope
[best_scope
]++;
3241 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0)
3242 ip6stat
.ip6s_sources_deprecated
[best_scope
]++;
3243 IFA_UNLOCK(&ifa_best
->ia_ifa
);
3250 * return the best address out of the same scope. if no address was
3251 * found, return the first valid address from designated IF.
3254 in6_ifawithifp(struct ifnet
*ifp
, struct in6_addr
*dst
)
3256 int dst_scope
= in6_addrscope(dst
), blen
= -1, tlen
;
3258 struct in6_ifaddr
*besta
= NULL
;
3259 struct in6_ifaddr
*dep
[2]; /* last-resort: deprecated */
3261 dep
[0] = dep
[1] = NULL
;
3264 * We first look for addresses in the same scope.
3265 * If there is one, return it.
3266 * If two or more, return one which matches the dst longest.
3267 * If none, return one of global addresses assigned other ifs.
3269 ifnet_lock_shared(ifp
);
3270 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3272 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3276 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3278 continue; /* XXX: is there any case to allow anycast? */
3280 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_NOTREADY
) {
3282 continue; /* don't use this interface */
3284 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3288 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3289 if (ip6_use_deprecated
) {
3290 IFA_ADDREF_LOCKED(ifa
); /* for dep[0] */
3293 IFA_REMREF(&dep
[0]->ia_ifa
);
3294 dep
[0] = (struct in6_ifaddr
*)ifa
;
3301 if (dst_scope
== in6_addrscope(IFA_IN6(ifa
))) {
3303 * call in6_matchlen() as few as possible
3308 IFA_LOCK(&besta
->ia_ifa
);
3309 blen
= in6_matchlen(
3310 &besta
->ia_addr
.sin6_addr
, dst
);
3311 IFA_UNLOCK(&besta
->ia_ifa
);
3314 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3317 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3319 IFA_REMREF(&besta
->ia_ifa
);
3320 besta
= (struct in6_ifaddr
*)ifa
;
3325 besta
= (struct in6_ifaddr
*)ifa
;
3326 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3334 ifnet_lock_done(ifp
);
3336 IFA_REMREF(&dep
[0]->ia_ifa
);
3340 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3342 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3346 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3348 continue; /* XXX: is there any case to allow anycast? */
3350 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_NOTREADY
) {
3352 continue; /* don't use this interface */
3354 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3358 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3359 if (ip6_use_deprecated
) {
3360 IFA_ADDREF_LOCKED(ifa
); /* for dep[1] */
3363 IFA_REMREF(&dep
[1]->ia_ifa
);
3364 dep
[1] = (struct in6_ifaddr
*)ifa
;
3370 IFA_ADDREF_LOCKED(ifa
); /* for caller */
3372 ifnet_lock_done(ifp
);
3374 IFA_REMREF(&dep
[0]->ia_ifa
);
3376 IFA_REMREF(&dep
[1]->ia_ifa
);
3377 return ((struct in6_ifaddr
*)ifa
);
3379 ifnet_lock_done(ifp
);
3381 /* use the last-resort values, that are, deprecated addresses */
3384 IFA_REMREF(&dep
[1]->ia_ifa
);
3394 * perform DAD when interface becomes IFF_UP.
3397 in6_if_up_dad_start(struct ifnet
*ifp
)
3401 /* start DAD on all the interface addresses */
3402 ifnet_lock_exclusive(ifp
);
3403 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3404 struct in6_ifaddr
*ia6
;
3407 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3411 ia6
= (struct in6_ifaddr
*)ifa
;
3412 if (ia6
->ia6_flags
& IN6_IFF_DADPROGRESS
) {
3413 int delay
= 0; /* delay ticks before DAD output */
3415 nd6_dad_start(ifa
, &delay
);
3420 ifnet_lock_done(ifp
);
3427 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0)
3431 * If we are using the alternative neighbor discovery
3432 * interface on this interface, then skip DAD.
3434 * Also, skip it for interfaces marked "local private"
3435 * for now, even when not marked as using the alternative
3436 * interface. This is for historical reasons.
3438 if (ifp
->if_eflags
&
3439 (IFEF_IPV6_ND6ALT
|IFEF_LOCALNET_PRIVATE
|IFEF_DIRECTLINK
))
3442 switch (ifp
->if_type
) {
3448 * These interfaces do not have the IFF_LOOPBACK flag,
3449 * but loop packets back. We do not have to do DAD on such
3450 * interfaces. We should even omit it, because loop-backed
3451 * NS would confuse the DAD procedure.
3456 * Our DAD routine requires the interface up and running.
3457 * However, some interfaces can be up before the RUNNING
3458 * status. Additionaly, users may try to assign addresses
3459 * before the interface becomes up (or running).
3460 * We simply skip DAD in such a case as a work around.
3461 * XXX: we should rather mark "tentative" on such addresses,
3462 * and do DAD after the interface becomes ready.
3464 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) !=
3465 (IFF_UP
|IFF_RUNNING
))
3473 * Calculate max IPv6 MTU through all the interfaces and store it
3479 u_int32_t maxmtu
= 0;
3482 ifnet_head_lock_shared();
3483 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
3484 struct nd_ifinfo
*ndi
;
3486 lck_rw_lock_shared(nd_if_rwlock
);
3487 if ((ndi
= ND_IFINFO(ifp
)) != NULL
&& !ndi
->initialized
)
3490 lck_mtx_lock(&ndi
->lock
);
3491 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
3492 IN6_LINKMTU(ifp
) > maxmtu
)
3493 maxmtu
= IN6_LINKMTU(ifp
);
3495 lck_mtx_unlock(&ndi
->lock
);
3496 lck_rw_done(nd_if_rwlock
);
3499 if (maxmtu
) /* update only when maxmtu is positive */
3500 in6_maxmtu
= maxmtu
;
3503 * Provide the length of interface identifiers to be used for the link attached
3504 * to the given interface. The length should be defined in "IPv6 over
3505 * xxx-link" document. Note that address architecture might also define
3506 * the length for a particular set of address prefixes, regardless of the
3507 * link type. Also see RFC 4862 for additional background.
3510 in6_if2idlen(struct ifnet
*ifp
)
3512 switch (ifp
->if_type
) {
3513 case IFT_ETHER
: /* RFC2464 */
3514 case IFT_IEEE8023ADLAG
: /* IEEE802.3ad Link Aggregate */
3515 #ifdef IFT_PROPVIRTUAL
3516 case IFT_PROPVIRTUAL
: /* XXX: no RFC. treat it as ether */
3519 case IFT_L2VLAN
: /* ditto */
3521 #ifdef IFT_IEEE80211
3522 case IFT_IEEE80211
: /* ditto */
3525 case IFT_MIP
: /* ditto */
3528 case IFT_FDDI
: /* RFC2467 */
3530 case IFT_ISO88025
: /* RFC2470 (IPv6 over Token Ring) */
3532 case IFT_PPP
: /* RFC2472 */
3534 case IFT_ARCNET
: /* RFC2497 */
3536 case IFT_FRELAY
: /* RFC2590 */
3538 case IFT_IEEE1394
: /* RFC3146 */
3541 return (64); /* draft-ietf-v6ops-mech-v2-07 */
3543 return (64); /* XXX: is this really correct? */
3545 return (64); /* for utun interfaces */
3547 return (64); /* Packet Data over Cellular */
3549 return (64); /* Transparent bridge interface */
3552 * Unknown link type:
3553 * It might be controversial to use the today's common constant
3554 * of 64 for these cases unconditionally. For full compliance,
3555 * we should return an error in this case. On the other hand,
3556 * if we simply miss the standard for the link type or a new
3557 * standard is defined for a new link type, the IFID length
3558 * is very likely to be the common constant. As a compromise,
3559 * we always use the constant, but make an explicit notice
3560 * indicating the "unknown" case.
3562 log(LOG_NOTICE
, "%s: unknown link type (%d)\n", __func__
,
3568 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
3569 * v4 mapped addr or v4 compat addr
3572 in6_sin6_2_sin(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3574 bzero(sin
, sizeof (*sin
));
3575 sin
->sin_len
= sizeof (struct sockaddr_in
);
3576 sin
->sin_family
= AF_INET
;
3577 sin
->sin_port
= sin6
->sin6_port
;
3578 sin
->sin_addr
.s_addr
= sin6
->sin6_addr
.s6_addr32
[3];
3581 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
3583 in6_sin_2_v4mapsin6(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3585 bzero(sin6
, sizeof (*sin6
));
3586 sin6
->sin6_len
= sizeof (struct sockaddr_in6
);
3587 sin6
->sin6_family
= AF_INET6
;
3588 sin6
->sin6_port
= sin
->sin_port
;
3589 sin6
->sin6_addr
.s6_addr32
[0] = 0;
3590 sin6
->sin6_addr
.s6_addr32
[1] = 0;
3591 if (sin
->sin_addr
.s_addr
) {
3592 sin6
->sin6_addr
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
3593 sin6
->sin6_addr
.s6_addr32
[3] = sin
->sin_addr
.s_addr
;
3595 sin6
->sin6_addr
.s6_addr32
[2] = 0;
3596 sin6
->sin6_addr
.s6_addr32
[3] = 0;
3600 /* Convert sockaddr_in6 into sockaddr_in. */
3602 in6_sin6_2_sin_in_sock(struct sockaddr
*nam
)
3604 struct sockaddr_in
*sin_p
;
3605 struct sockaddr_in6 sin6
;
3608 * Save original sockaddr_in6 addr and convert it
3611 sin6
= *(struct sockaddr_in6
*)(void *)nam
;
3612 sin_p
= (struct sockaddr_in
*)(void *)nam
;
3613 in6_sin6_2_sin(sin_p
, &sin6
);
3616 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
3618 in6_sin_2_v4mapsin6_in_sock(struct sockaddr
**nam
)
3620 struct sockaddr_in
*sin_p
;
3621 struct sockaddr_in6
*sin6_p
;
3623 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof (*sin6_p
), M_SONAME
,
3627 sin_p
= (struct sockaddr_in
*)(void *)*nam
;
3628 in6_sin_2_v4mapsin6(sin_p
, sin6_p
);
3629 FREE(*nam
, M_SONAME
);
3630 *nam
= (struct sockaddr
*)sin6_p
;
3636 * Posts in6_event_data message kernel events.
3638 * To get the same size of kev_in6_data between ILP32 and LP64 data models
3639 * we are using a special version of the in6_addrlifetime structure that
3640 * uses only 32 bits fields to be compatible with Leopard, and that
3641 * are large enough to span 68 years.
3644 in6_post_msg(struct ifnet
*ifp
, u_int32_t event_code
, struct in6_ifaddr
*ifa
,
3647 struct kev_msg ev_msg
;
3648 struct kev_in6_data in6_event_data
;
3649 struct in6_addrlifetime ia6_lt
;
3651 bzero(&in6_event_data
, sizeof (struct kev_in6_data
));
3652 bzero(&ev_msg
, sizeof (struct kev_msg
));
3653 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3654 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3655 ev_msg
.kev_subclass
= KEV_INET6_SUBCLASS
;
3656 ev_msg
.event_code
= event_code
;
3658 IFA_LOCK(&ifa
->ia_ifa
);
3659 in6_event_data
.ia_addr
= ifa
->ia_addr
;
3660 in6_event_data
.ia_net
= ifa
->ia_net
;
3661 in6_event_data
.ia_dstaddr
= ifa
->ia_dstaddr
;
3662 in6_event_data
.ia_prefixmask
= ifa
->ia_prefixmask
;
3663 in6_event_data
.ia_plen
= ifa
->ia_plen
;
3664 in6_event_data
.ia6_flags
= (u_int32_t
)ifa
->ia6_flags
;
3666 /* retrieve time as calendar time (last arg is 1) */
3667 in6ifa_getlifetime(ifa
, &ia6_lt
, 1);
3668 in6_event_data
.ia_lifetime
.ia6t_expire
= ia6_lt
.ia6t_expire
;
3669 in6_event_data
.ia_lifetime
.ia6t_preferred
= ia6_lt
.ia6t_preferred
;
3670 in6_event_data
.ia_lifetime
.ia6t_vltime
= ia6_lt
.ia6t_vltime
;
3671 in6_event_data
.ia_lifetime
.ia6t_pltime
= ia6_lt
.ia6t_pltime
;
3672 IFA_UNLOCK(&ifa
->ia_ifa
);
3675 (void) strlcpy(&in6_event_data
.link_data
.if_name
[0],
3676 ifp
->if_name
, IFNAMSIZ
);
3677 in6_event_data
.link_data
.if_family
= ifp
->if_family
;
3678 in6_event_data
.link_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
3682 memcpy(&in6_event_data
.ia_mac
, mac
,
3683 sizeof(in6_event_data
.ia_mac
));
3685 ev_msg
.dv
[0].data_ptr
= &in6_event_data
;
3686 ev_msg
.dv
[0].data_length
= sizeof (in6_event_data
);
3687 ev_msg
.dv
[1].data_length
= 0;
3689 kev_post_msg(&ev_msg
);
3693 * Called as part of ip6_init
3696 in6_ifaddr_init(void)
3701 PE_parse_boot_argn("ifa_debug", &in6ifa_debug
, sizeof (in6ifa_debug
));
3703 in6ifa_size
= (in6ifa_debug
== 0) ? sizeof (struct in6_ifaddr
) :
3704 sizeof (struct in6_ifaddr_dbg
);
3706 in6ifa_zone
= zinit(in6ifa_size
, IN6IFA_ZONE_MAX
* in6ifa_size
,
3707 0, IN6IFA_ZONE_NAME
);
3708 if (in6ifa_zone
== NULL
) {
3709 panic("%s: failed allocating %s", __func__
, IN6IFA_ZONE_NAME
);
3712 zone_change(in6ifa_zone
, Z_EXPAND
, TRUE
);
3713 zone_change(in6ifa_zone
, Z_CALLERACCT
, FALSE
);
3715 lck_mtx_init(&in6ifa_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3716 TAILQ_INIT(&in6ifa_trash_head
);
3719 static struct in6_ifaddr
*
3720 in6_ifaddr_alloc(int how
)
3722 struct in6_ifaddr
*in6ifa
;
3724 in6ifa
= (how
== M_WAITOK
) ? zalloc(in6ifa_zone
) :
3725 zalloc_noblock(in6ifa_zone
);
3726 if (in6ifa
!= NULL
) {
3727 bzero(in6ifa
, in6ifa_size
);
3728 in6ifa
->ia_ifa
.ifa_free
= in6_ifaddr_free
;
3729 in6ifa
->ia_ifa
.ifa_debug
|= IFD_ALLOC
;
3730 ifa_lock_init(&in6ifa
->ia_ifa
);
3731 if (in6ifa_debug
!= 0) {
3732 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3733 (struct in6_ifaddr_dbg
*)in6ifa
;
3734 in6ifa
->ia_ifa
.ifa_debug
|= IFD_DEBUG
;
3735 in6ifa
->ia_ifa
.ifa_trace
= in6_ifaddr_trace
;
3736 in6ifa
->ia_ifa
.ifa_attached
= in6_ifaddr_attached
;
3737 in6ifa
->ia_ifa
.ifa_detached
= in6_ifaddr_detached
;
3738 ctrace_record(&in6ifa_dbg
->in6ifa_alloc
);
3746 in6_ifaddr_free(struct ifaddr
*ifa
)
3748 IFA_LOCK_ASSERT_HELD(ifa
);
3750 if (ifa
->ifa_refcnt
!= 0) {
3751 panic("%s: ifa %p bad ref cnt", __func__
, ifa
);
3753 } else if (!(ifa
->ifa_debug
& IFD_ALLOC
)) {
3754 panic("%s: ifa %p cannot be freed", __func__
, ifa
);
3757 if (ifa
->ifa_debug
& IFD_DEBUG
) {
3758 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3759 (struct in6_ifaddr_dbg
*)ifa
;
3760 ctrace_record(&in6ifa_dbg
->in6ifa_free
);
3761 bcopy(&in6ifa_dbg
->in6ifa
, &in6ifa_dbg
->in6ifa_old
,
3762 sizeof (struct in6_ifaddr
));
3763 if (ifa
->ifa_debug
& IFD_TRASHED
) {
3764 /* Become a regular mutex, just in case */
3765 IFA_CONVERT_LOCK(ifa
);
3766 lck_mtx_lock(&in6ifa_trash_lock
);
3767 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
,
3769 lck_mtx_unlock(&in6ifa_trash_lock
);
3770 ifa
->ifa_debug
&= ~IFD_TRASHED
;
3774 ifa_lock_destroy(ifa
);
3775 bzero(ifa
, sizeof (struct in6_ifaddr
));
3776 zfree(in6ifa_zone
, ifa
);
3780 in6_ifaddr_attached(struct ifaddr
*ifa
)
3782 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3784 IFA_LOCK_ASSERT_HELD(ifa
);
3786 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3787 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3790 if (ifa
->ifa_debug
& IFD_TRASHED
) {
3791 /* Become a regular mutex, just in case */
3792 IFA_CONVERT_LOCK(ifa
);
3793 lck_mtx_lock(&in6ifa_trash_lock
);
3794 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
3795 lck_mtx_unlock(&in6ifa_trash_lock
);
3796 ifa
->ifa_debug
&= ~IFD_TRASHED
;
3801 in6_ifaddr_detached(struct ifaddr
*ifa
)
3803 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3805 IFA_LOCK_ASSERT_HELD(ifa
);
3807 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3808 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3810 } else if (ifa
->ifa_debug
& IFD_TRASHED
) {
3811 panic("%s: ifa %p is already in trash list", __func__
, ifa
);
3814 ifa
->ifa_debug
|= IFD_TRASHED
;
3815 /* Become a regular mutex, just in case */
3816 IFA_CONVERT_LOCK(ifa
);
3817 lck_mtx_lock(&in6ifa_trash_lock
);
3818 TAILQ_INSERT_TAIL(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
3819 lck_mtx_unlock(&in6ifa_trash_lock
);
3823 in6_ifaddr_trace(struct ifaddr
*ifa
, int refhold
)
3825 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3830 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3831 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3835 cnt
= &in6ifa_dbg
->in6ifa_refhold_cnt
;
3836 tr
= in6ifa_dbg
->in6ifa_refhold
;
3838 cnt
= &in6ifa_dbg
->in6ifa_refrele_cnt
;
3839 tr
= in6ifa_dbg
->in6ifa_refrele
;
3842 idx
= atomic_add_16_ov(cnt
, 1) % IN6IFA_TRACE_HIST_SIZE
;
3843 ctrace_record(&tr
[idx
]);
3847 in6_ifaddr_set_dadprogress(struct in6_ifaddr
*ia
)
3849 struct ifnet
* ifp
= ia
->ia_ifp
;
3850 uint32_t flags
= IN6_IFF_TENTATIVE
;
3851 uint32_t optdad
= nd6_optimistic_dad
;
3854 if ((ifp
->if_eflags
& IFEF_IPV6_ROUTER
) != 0) {
3857 struct nd_ifinfo
*ndi
;
3859 lck_rw_lock_shared(nd_if_rwlock
);
3860 ndi
= ND_IFINFO(ifp
);
3861 VERIFY (ndi
!= NULL
&& ndi
->initialized
);
3862 lck_mtx_lock(&ndi
->lock
);
3863 if ((ndi
->flags
& ND6_IFF_REPLICATED
) != 0) {
3866 lck_mtx_unlock(&ndi
->lock
);
3867 lck_rw_done(nd_if_rwlock
);
3872 if ((optdad
& ND6_OPTIMISTIC_DAD_LINKLOCAL
) &&
3873 IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
))
3874 flags
= IN6_IFF_OPTIMISTIC
;
3875 else if ((optdad
& ND6_OPTIMISTIC_DAD_AUTOCONF
) &&
3876 (ia
->ia6_flags
& IN6_IFF_AUTOCONF
)) {
3877 if (ia
->ia6_flags
& IN6_IFF_TEMPORARY
) {
3878 if (optdad
& ND6_OPTIMISTIC_DAD_TEMPORARY
)
3879 flags
= IN6_IFF_OPTIMISTIC
;
3880 } else if (ia
->ia6_flags
& IN6_IFF_SECURED
) {
3881 if (optdad
& ND6_OPTIMISTIC_DAD_SECURED
)
3882 flags
= IN6_IFF_OPTIMISTIC
;
3884 } else if ((optdad
& ND6_OPTIMISTIC_DAD_DYNAMIC
) &&
3885 (ia
->ia6_flags
& IN6_IFF_DYNAMIC
)) {
3886 if (ia
->ia6_flags
& IN6_IFF_TEMPORARY
) {
3887 if (optdad
& ND6_OPTIMISTIC_DAD_TEMPORARY
)
3888 flags
= IN6_IFF_OPTIMISTIC
;
3890 flags
= IN6_IFF_OPTIMISTIC
;
3892 } else if ((optdad
& ND6_OPTIMISTIC_DAD_MANUAL
) &&
3893 (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
)) {
3896 * Bypass tentative for address assignments
3897 * not covered above (e.g. manual) upon request
3899 if (!IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
) &&
3900 !(ia
->ia6_flags
& IN6_IFF_AUTOCONF
) &&
3901 !(ia
->ia6_flags
& IN6_IFF_DYNAMIC
))
3902 flags
= IN6_IFF_OPTIMISTIC
;
3906 ia
->ia6_flags
&= ~(IN6_IFF_DUPLICATED
| IN6_IFF_DADPROGRESS
);
3907 ia
->ia6_flags
|= flags
;
3909 nd6log2((LOG_DEBUG
, "%s - %s ifp %s ia6_flags 0x%x\n",
3911 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
3912 if_name(ia
->ia_ifp
),
3917 * Handle SIOCGASSOCIDS ioctl for PF_INET6 domain.
3920 in6_getassocids(struct socket
*so
, uint32_t *cnt
, user_addr_t aidp
)
3922 struct in6pcb
*in6p
= sotoin6pcb(so
);
3925 if (in6p
== NULL
|| in6p
->inp_state
== INPCB_STATE_DEAD
)
3928 /* IN6PCB has no concept of association */
3932 /* just asking how many there are? */
3933 if (aidp
== USER_ADDR_NULL
)
3936 return (copyout(&aid
, aidp
, sizeof (aid
)));
3940 * Handle SIOCGCONNIDS ioctl for PF_INET6 domain.
3943 in6_getconnids(struct socket
*so
, associd_t aid
, uint32_t *cnt
,
3946 struct in6pcb
*in6p
= sotoin6pcb(so
);
3949 if (in6p
== NULL
|| in6p
->inp_state
== INPCB_STATE_DEAD
)
3952 if (aid
!= ASSOCID_ANY
&& aid
!= ASSOCID_ALL
)
3955 /* if connected, return 1 connection count */
3956 *cnt
= ((so
->so_state
& SS_ISCONNECTED
) ? 1 : 0);
3958 /* just asking how many there are? */
3959 if (cidp
== USER_ADDR_NULL
)
3962 /* if IN6PCB is connected, assign it connid 1 */
3963 cid
= ((*cnt
!= 0) ? 1 : CONNID_ANY
);
3965 return (copyout(&cid
, cidp
, sizeof (cid
)));
3969 * Handle SIOCGCONNINFO ioctl for PF_INET6 domain.
3972 in6_getconninfo(struct socket
*so
, connid_t cid
, uint32_t *flags
,
3973 uint32_t *ifindex
, int32_t *soerror
, user_addr_t src
, socklen_t
*src_len
,
3974 user_addr_t dst
, socklen_t
*dst_len
, uint32_t *aux_type
,
3975 user_addr_t aux_data
, uint32_t *aux_len
)
3977 #pragma unused(aux_data)
3978 struct in6pcb
*in6p
= sotoin6pcb(so
);
3979 struct sockaddr_in6 sin6
;
3980 struct ifnet
*ifp
= NULL
;
3982 u_int32_t copy_len
= 0;
3985 * Don't test for INPCB_STATE_DEAD since this may be called
3986 * after SOF_PCBCLEARING is set, e.g. after tcp_close().
3993 if (cid
!= CONNID_ANY
&& cid
!= CONNID_ALL
&& cid
!= 1) {
3998 ifp
= in6p
->in6p_last_outifp
;
3999 *ifindex
= ((ifp
!= NULL
) ? ifp
->if_index
: 0);
4000 *soerror
= so
->so_error
;
4002 if (so
->so_state
& SS_ISCONNECTED
)
4003 *flags
|= (CIF_CONNECTED
| CIF_PREFERRED
);
4004 if (in6p
->in6p_flags
& INP_BOUND_IF
)
4005 *flags
|= CIF_BOUND_IF
;
4006 if (!(in6p
->in6p_flags
& INP_IN6ADDR_ANY
))
4007 *flags
|= CIF_BOUND_IP
;
4008 if (!(in6p
->in6p_flags
& INP_ANONPORT
))
4009 *flags
|= CIF_BOUND_PORT
;
4011 bzero(&sin6
, sizeof (sin6
));
4012 sin6
.sin6_len
= sizeof (sin6
);
4013 sin6
.sin6_family
= AF_INET6
;
4015 /* source address and port */
4016 sin6
.sin6_port
= in6p
->in6p_lport
;
4017 in6_recoverscope(&sin6
, &in6p
->in6p_laddr
, NULL
);
4018 if (*src_len
== 0) {
4019 *src_len
= sin6
.sin6_len
;
4021 if (src
!= USER_ADDR_NULL
) {
4022 copy_len
= min(*src_len
, sizeof (sin6
));
4023 error
= copyout(&sin6
, src
, copy_len
);
4026 *src_len
= copy_len
;
4030 /* destination address and port */
4031 sin6
.sin6_port
= in6p
->in6p_fport
;
4032 in6_recoverscope(&sin6
, &in6p
->in6p_faddr
, NULL
);
4033 if (*dst_len
== 0) {
4034 *dst_len
= sin6
.sin6_len
;
4036 if (dst
!= USER_ADDR_NULL
) {
4037 copy_len
= min(*dst_len
, sizeof (sin6
));
4038 error
= copyout(&sin6
, dst
, copy_len
);
4041 *dst_len
= copy_len
;
4047 if (SOCK_PROTO(so
) == IPPROTO_TCP
) {
4048 struct conninfo_tcp tcp_ci
;
4050 *aux_type
= CIAUX_TCP
;
4051 if (*aux_len
== 0) {
4052 *aux_len
= sizeof (tcp_ci
);
4054 if (aux_data
!= USER_ADDR_NULL
) {
4055 copy_len
= min(*aux_len
, sizeof (tcp_ci
));
4056 bzero(&tcp_ci
, sizeof (tcp_ci
));
4057 tcp_getconninfo(so
, &tcp_ci
);
4058 error
= copyout(&tcp_ci
, aux_data
, copy_len
);
4061 *aux_len
= copy_len
;
4073 * The switch statement below does nothing at runtime, as it serves as a
4074 * compile time check to ensure that all of the socket 'u' ioctls (those
4075 * in the 'u' group going thru soo_ioctl) that are made available by the
4076 * networking stack is unique. This works as long as this routine gets
4077 * updated each time a new interface ioctl gets added.
4079 * Any failures at compile time indicates duplicated ioctl values.
4081 static __attribute__((unused
)) void
4082 in6ioctl_cassert(void)
4085 * This is equivalent to _CASSERT() and the compiler wouldn't
4086 * generate any instructions, thus for compile time only.
4088 switch ((u_long
)0) {
4091 /* bsd/netinet6/in6_var.h */
4092 case SIOCAADDRCTL_POLICY
:
4093 case SIOCDADDRCTL_POLICY
:
4094 case SIOCDRADD_IN6_32
:
4095 case SIOCDRADD_IN6_64
:
4096 case SIOCDRDEL_IN6_32
:
4097 case SIOCDRDEL_IN6_64
: