2 * Copyright (c) 2003-2018 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * Copyright (c) 1982, 1986, 1991, 1993
60 * The Regents of the University of California. All rights reserved.
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in the
69 * documentation and/or other materials provided with the distribution.
70 * 3. All advertising materials mentioning features or use of this software
71 * must display the following acknowledgement:
72 * This product includes software developed by the University of
73 * California, Berkeley and its contributors.
74 * 4. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
90 * @(#)in.c 8.2 (Berkeley) 11/15/93
94 #include <sys/param.h>
95 #include <sys/ioctl.h>
96 #include <sys/errno.h>
97 #include <sys/malloc.h>
98 #include <sys/socket.h>
99 #include <sys/socketvar.h>
100 #include <sys/sockio.h>
101 #include <sys/systm.h>
102 #include <sys/time.h>
103 #include <sys/kernel.h>
104 #include <sys/syslog.h>
105 #include <sys/kern_event.h>
106 #include <sys/mcache.h>
107 #include <sys/protosw.h>
108 #include <sys/sysctl.h>
110 #include <kern/locks.h>
111 #include <kern/zalloc.h>
112 #include <kern/clock.h>
113 #include <libkern/OSAtomic.h>
114 #include <machine/machine_routines.h>
115 #include <mach/boolean.h>
118 #include <net/if_types.h>
119 #include <net/if_var.h>
120 #include <net/route.h>
121 #include <net/if_dl.h>
122 #include <net/kpi_protocol.h>
123 #include <net/nwk_wq.h>
125 #include <netinet/in.h>
126 #include <netinet/in_var.h>
127 #include <netinet/if_ether.h>
128 #include <netinet/in_systm.h>
129 #include <netinet/ip.h>
130 #include <netinet/in_pcb.h>
131 #include <netinet/icmp6.h>
132 #include <netinet/tcp.h>
133 #include <netinet/tcp_seq.h>
134 #include <netinet/tcp_var.h>
136 #include <netinet6/nd6.h>
137 #include <netinet/ip6.h>
138 #include <netinet6/ip6_var.h>
139 #include <netinet6/mld6_var.h>
140 #include <netinet6/in6_ifattach.h>
141 #include <netinet6/scope6_var.h>
142 #include <netinet6/in6_var.h>
143 #include <netinet6/in6_pcb.h>
145 #include <net/net_osdep.h>
147 #include <net/dlil.h>
148 #include <net/if_llatbl.h>
151 #include <net/pfvar.h>
155 * Definitions of some costant IP6 addresses.
157 const struct in6_addr in6addr_any
= IN6ADDR_ANY_INIT
;
158 const struct in6_addr in6addr_loopback
= IN6ADDR_LOOPBACK_INIT
;
159 const struct in6_addr in6addr_nodelocal_allnodes
=
160 IN6ADDR_NODELOCAL_ALLNODES_INIT
;
161 const struct in6_addr in6addr_linklocal_allnodes
=
162 IN6ADDR_LINKLOCAL_ALLNODES_INIT
;
163 const struct in6_addr in6addr_linklocal_allrouters
=
164 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT
;
165 const struct in6_addr in6addr_linklocal_allv2routers
=
166 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT
;
168 const struct in6_addr in6mask0
= IN6MASK0
;
169 const struct in6_addr in6mask7
= IN6MASK7
;
170 const struct in6_addr in6mask16
= IN6MASK16
;
171 const struct in6_addr in6mask32
= IN6MASK32
;
172 const struct in6_addr in6mask64
= IN6MASK64
;
173 const struct in6_addr in6mask96
= IN6MASK96
;
174 const struct in6_addr in6mask128
= IN6MASK128
;
176 const struct sockaddr_in6 sa6_any
= {
177 sizeof(sa6_any
), AF_INET6
, 0, 0, IN6ADDR_ANY_INIT
, 0
180 static int in6ctl_associd(struct socket
*, u_long
, caddr_t
);
181 static int in6ctl_connid(struct socket
*, u_long
, caddr_t
);
182 static int in6ctl_conninfo(struct socket
*, u_long
, caddr_t
);
183 static int in6ctl_llstart(struct ifnet
*, u_long
, caddr_t
);
184 static int in6ctl_llstop(struct ifnet
*);
185 static int in6ctl_cgastart(struct ifnet
*, u_long
, caddr_t
);
186 static int in6ctl_gifaddr(struct ifnet
*, struct in6_ifaddr
*, u_long
,
188 static int in6ctl_gifstat(struct ifnet
*, u_long
, struct in6_ifreq
*);
189 static int in6ctl_alifetime(struct in6_ifaddr
*, u_long
, struct in6_ifreq
*,
191 static int in6ctl_aifaddr(struct ifnet
*, struct in6_aliasreq
*);
192 static void in6ctl_difaddr(struct ifnet
*, struct in6_ifaddr
*);
193 static int in6_autoconf(struct ifnet
*, int);
194 static int in6_setrouter(struct ifnet
*, int);
195 static int in6_ifinit(struct ifnet
*, struct in6_ifaddr
*, int);
196 static int in6_ifaupdate_aux(struct in6_ifaddr
*, struct ifnet
*, int);
197 static void in6_unlink_ifa(struct in6_ifaddr
*, struct ifnet
*);
198 static struct in6_ifaddr
*in6_ifaddr_alloc(int);
199 static void in6_ifaddr_attached(struct ifaddr
*);
200 static void in6_ifaddr_detached(struct ifaddr
*);
201 static void in6_ifaddr_free(struct ifaddr
*);
202 static void in6_ifaddr_trace(struct ifaddr
*, int);
203 #if defined(__LP64__)
204 static void in6_cgareq_32_to_64(struct in6_cgareq_32
*,
205 struct in6_cgareq_64
*);
207 static void in6_cgareq_64_to_32(struct in6_cgareq_64
*,
208 struct in6_cgareq_32
*);
210 static struct in6_aliasreq
*in6_aliasreq_to_native(void *, int,
211 struct in6_aliasreq
*);
212 static struct in6_cgareq
*in6_cgareq_to_native(void *, int,
213 struct in6_cgareq
*);
214 static int in6_to_kamescope(struct sockaddr_in6
*, struct ifnet
*);
215 static int in6_getassocids(struct socket
*, uint32_t *, user_addr_t
);
216 static int in6_getconnids(struct socket
*, sae_associd_t
, uint32_t *,
219 static void in6_if_up_dad_start(struct ifnet
*);
221 extern lck_mtx_t
*nd6_mutex
;
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 */
264 struct eventhandler_lists_ctxt in6_evhdlr_ctxt
;
265 struct eventhandler_lists_ctxt in6_clat46_evhdlr_ctxt
;
267 * Subroutine for in6_ifaddloop() and in6_ifremloop().
268 * This routine does actual work.
271 in6_ifloop_request(int cmd
, struct ifaddr
*ifa
)
273 struct sockaddr_in6 all1_sa
;
274 struct rtentry
*nrt
= NULL
;
277 bzero(&all1_sa
, sizeof(all1_sa
));
278 all1_sa
.sin6_family
= AF_INET6
;
279 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
280 all1_sa
.sin6_addr
= in6mask128
;
283 * We specify the address itself as the gateway, and set the
284 * RTF_LLINFO flag, so that the corresponding host route would have
285 * the flag, and thus applications that assume traditional behavior
286 * would be happy. Note that we assume the caller of the function
287 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
288 * which changes the outgoing interface to the loopback interface.
289 * ifa_addr for INET6 is set once during init; no need to hold lock.
291 lck_mtx_lock(rnh_lock
);
292 e
= rtrequest_locked(cmd
, ifa
->ifa_addr
, ifa
->ifa_addr
,
293 (struct sockaddr
*)&all1_sa
, RTF_UP
| RTF_HOST
| RTF_LLINFO
, &nrt
);
295 log(LOG_ERR
, "in6_ifloop_request: "
296 "%s operation failed for %s (errno=%d)\n",
297 cmd
== RTM_ADD
? "ADD" : "DELETE",
298 ip6_sprintf(&((struct in6_ifaddr
*)ifa
)->ia_addr
.sin6_addr
),
306 * Make sure rt_ifa be equal to IFA, the second argument of the
308 * We need this because when we refer to rt_ifa->ia6_flags in
309 * ip6_input, we assume that the rt_ifa points to the address instead
310 * of the loopback address.
312 if (cmd
== RTM_ADD
&& nrt
&& ifa
!= nrt
->rt_ifa
) {
317 * Report the addition/removal of the address to the routing socket.
318 * XXX: since we called rtinit for a p2p interface with a destination,
319 * we end up reporting twice in such a case. Should we rather
320 * omit the second report?
323 rt_newaddrmsg(cmd
, ifa
, e
, nrt
);
324 if (cmd
== RTM_DELETE
) {
328 /* the cmd must be RTM_ADD here */
329 RT_REMREF_LOCKED(nrt
);
333 lck_mtx_unlock(rnh_lock
);
337 * Add ownaddr as loopback rtentry. We previously add the route only if
338 * necessary (ex. on a p2p link). However, since we now manage addresses
339 * separately from prefixes, we should always add the route. We can't
340 * rely on the cloning mechanism from the corresponding interface route
344 in6_ifaddloop(struct ifaddr
*ifa
)
349 * If there is no loopback entry, allocate one. ifa_addr for
350 * INET6 is set once during init; no need to hold lock.
352 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
356 if (rt
== NULL
|| (rt
->rt_flags
& RTF_HOST
) == 0 ||
357 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) == 0) {
359 RT_REMREF_LOCKED(rt
);
362 in6_ifloop_request(RTM_ADD
, ifa
);
363 } else if (rt
!= NULL
) {
364 RT_REMREF_LOCKED(rt
);
370 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
374 in6_ifremloop(struct ifaddr
*ifa
)
376 struct in6_ifaddr
*ia
;
381 * Some of BSD variants do not remove cloned routes
382 * from an interface direct route, when removing the direct route
383 * (see comments in net/net_osdep.h). Even for variants that do remove
384 * cloned routes, they could fail to remove the cloned routes when
385 * we handle multple addresses that share a common prefix.
386 * So, we should remove the route corresponding to the deleted address
387 * regardless of the result of in6_is_ifloop_auto().
391 * Delete the entry only if exact one ifa exists. More than one ifa
392 * can exist if we assign a same single address to multiple
393 * (probably p2p) interfaces.
394 * XXX: we should avoid such a configuration in IPv6...
396 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
397 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
398 IFA_LOCK(&ia
->ia_ifa
);
399 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa
), &ia
->ia_addr
.sin6_addr
)) {
402 IFA_UNLOCK(&ia
->ia_ifa
);
406 IFA_UNLOCK(&ia
->ia_ifa
);
408 lck_rw_done(&in6_ifaddr_rwlock
);
412 * Before deleting, check if a corresponding loopbacked host
413 * route surely exists. With this check, we can avoid to
414 * delete an interface direct route whose destination is same
415 * as the address being removed. This can happen when removing
416 * a subnet-router anycast address on an interface attahced
417 * to a shared medium. ifa_addr for INET6 is set once during
418 * init; no need to hold lock.
420 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
423 if ((rt
->rt_flags
& RTF_HOST
) != 0 &&
424 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
425 RT_REMREF_LOCKED(rt
);
427 in6_ifloop_request(RTM_DELETE
, ifa
);
437 in6_mask2len(struct in6_addr
*mask
, u_char
*lim0
)
440 u_char
*lim
= lim0
, *p
;
442 /* ignore the scope_id part */
443 if (lim0
== NULL
|| lim0
- (u_char
*)mask
> sizeof(*mask
)) {
444 lim
= (u_char
*)mask
+ sizeof(*mask
);
446 for (p
= (u_char
*)mask
; p
< lim
; x
++, p
++) {
453 for (y
= 0; y
< 8; y
++) {
454 if ((*p
& (0x80 >> y
)) == 0) {
461 * when the limit pointer is given, do a stricter check on the
465 if (y
!= 0 && (*p
& (0x00ff >> y
)) != 0) {
468 for (p
= p
+ 1; p
< lim
; p
++) {
479 in6_len2mask(struct in6_addr
*mask
, int len
)
483 bzero(mask
, sizeof(*mask
));
484 for (i
= 0; i
< len
/ 8; i
++) {
485 mask
->s6_addr8
[i
] = 0xff;
488 mask
->s6_addr8
[i
] = (0xff00 >> (len
% 8)) & 0xff;
493 in6_aliasreq_64_to_32(struct in6_aliasreq_64
*src
, struct in6_aliasreq_32
*dst
)
495 bzero(dst
, sizeof(*dst
));
496 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof(dst
->ifra_name
));
497 dst
->ifra_addr
= src
->ifra_addr
;
498 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
499 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
500 dst
->ifra_flags
= src
->ifra_flags
;
501 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
502 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
503 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
504 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
508 in6_aliasreq_32_to_64(struct in6_aliasreq_32
*src
, struct in6_aliasreq_64
*dst
)
510 bzero(dst
, sizeof(*dst
));
511 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof(dst
->ifra_name
));
512 dst
->ifra_addr
= src
->ifra_addr
;
513 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
514 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
515 dst
->ifra_flags
= src
->ifra_flags
;
516 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
517 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
518 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
519 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
522 #if defined(__LP64__)
524 in6_cgareq_32_to_64(struct in6_cgareq_32
*src
,
525 struct in6_cgareq_64
*dst
)
527 bzero(dst
, sizeof(*dst
));
528 bcopy(src
->cgar_name
, dst
->cgar_name
, sizeof(dst
->cgar_name
));
529 dst
->cgar_flags
= src
->cgar_flags
;
530 bcopy(src
->cgar_cgaprep
.cga_modifier
.octets
,
531 dst
->cgar_cgaprep
.cga_modifier
.octets
,
532 sizeof(dst
->cgar_cgaprep
.cga_modifier
.octets
));
533 dst
->cgar_cgaprep
.cga_security_level
=
534 src
->cgar_cgaprep
.cga_security_level
;
535 dst
->cgar_lifetime
.ia6t_expire
= src
->cgar_lifetime
.ia6t_expire
;
536 dst
->cgar_lifetime
.ia6t_preferred
= src
->cgar_lifetime
.ia6t_preferred
;
537 dst
->cgar_lifetime
.ia6t_vltime
= src
->cgar_lifetime
.ia6t_vltime
;
538 dst
->cgar_lifetime
.ia6t_pltime
= src
->cgar_lifetime
.ia6t_pltime
;
542 #if !defined(__LP64__)
544 in6_cgareq_64_to_32(struct in6_cgareq_64
*src
,
545 struct in6_cgareq_32
*dst
)
547 bzero(dst
, sizeof(*dst
));
548 bcopy(src
->cgar_name
, dst
->cgar_name
, sizeof(dst
->cgar_name
));
549 dst
->cgar_flags
= src
->cgar_flags
;
550 bcopy(src
->cgar_cgaprep
.cga_modifier
.octets
,
551 dst
->cgar_cgaprep
.cga_modifier
.octets
,
552 sizeof(dst
->cgar_cgaprep
.cga_modifier
.octets
));
553 dst
->cgar_cgaprep
.cga_security_level
=
554 src
->cgar_cgaprep
.cga_security_level
;
555 dst
->cgar_lifetime
.ia6t_expire
= src
->cgar_lifetime
.ia6t_expire
;
556 dst
->cgar_lifetime
.ia6t_preferred
= src
->cgar_lifetime
.ia6t_preferred
;
557 dst
->cgar_lifetime
.ia6t_vltime
= src
->cgar_lifetime
.ia6t_vltime
;
558 dst
->cgar_lifetime
.ia6t_pltime
= src
->cgar_lifetime
.ia6t_pltime
;
562 static struct in6_aliasreq
*
563 in6_aliasreq_to_native(void *data
, int data_is_64
, struct in6_aliasreq
*dst
)
565 #if defined(__LP64__)
567 bcopy(data
, dst
, sizeof(*dst
));
569 in6_aliasreq_32_to_64((struct in6_aliasreq_32
*)data
,
570 (struct in6_aliasreq_64
*)dst
);
574 in6_aliasreq_64_to_32((struct in6_aliasreq_64
*)data
,
575 (struct in6_aliasreq_32
*)dst
);
577 bcopy(data
, dst
, sizeof(*dst
));
579 #endif /* __LP64__ */
583 static struct in6_cgareq
*
584 in6_cgareq_to_native(void *data
, int is64
, struct in6_cgareq
*dst
)
586 #if defined(__LP64__)
588 bcopy(data
, dst
, sizeof(*dst
));
590 in6_cgareq_32_to_64((struct in6_cgareq_32
*)data
,
591 (struct in6_cgareq_64
*)dst
);
595 in6_cgareq_64_to_32((struct in6_cgareq_64
*)data
,
596 (struct in6_cgareq_32
*)dst
);
598 bcopy(data
, dst
, sizeof(*dst
));
600 #endif /* __LP64__ */
604 static __attribute__((noinline
)) int
605 in6ctl_associd(struct socket
*so
, u_long cmd
, caddr_t data
)
609 struct so_aidreq32 a32
;
610 struct so_aidreq64 a64
;
616 case SIOCGASSOCIDS32
: { /* struct so_aidreq32 */
617 bcopy(data
, &u
.a32
, sizeof(u
.a32
));
618 error
= in6_getassocids(so
, &u
.a32
.sar_cnt
, u
.a32
.sar_aidp
);
620 bcopy(&u
.a32
, data
, sizeof(u
.a32
));
625 case SIOCGASSOCIDS64
: { /* struct so_aidreq64 */
626 bcopy(data
, &u
.a64
, sizeof(u
.a64
));
627 error
= in6_getassocids(so
, &u
.a64
.sar_cnt
, u
.a64
.sar_aidp
);
629 bcopy(&u
.a64
, data
, sizeof(u
.a64
));
642 static __attribute__((noinline
)) int
643 in6ctl_connid(struct socket
*so
, u_long cmd
, caddr_t data
)
647 struct so_cidreq32 c32
;
648 struct so_cidreq64 c64
;
654 case SIOCGCONNIDS32
: { /* struct so_cidreq32 */
655 bcopy(data
, &u
.c32
, sizeof(u
.c32
));
656 error
= in6_getconnids(so
, u
.c32
.scr_aid
, &u
.c32
.scr_cnt
,
659 bcopy(&u
.c32
, data
, sizeof(u
.c32
));
664 case SIOCGCONNIDS64
: { /* struct so_cidreq64 */
665 bcopy(data
, &u
.c64
, sizeof(u
.c64
));
666 error
= in6_getconnids(so
, u
.c64
.scr_aid
, &u
.c64
.scr_cnt
,
669 bcopy(&u
.c64
, data
, sizeof(u
.c64
));
682 static __attribute__((noinline
)) int
683 in6ctl_conninfo(struct socket
*so
, u_long cmd
, caddr_t data
)
687 struct so_cinforeq32 ci32
;
688 struct so_cinforeq64 ci64
;
694 case SIOCGCONNINFO32
: { /* struct so_cinforeq32 */
695 bcopy(data
, &u
.ci32
, sizeof(u
.ci32
));
696 error
= in6_getconninfo(so
, u
.ci32
.scir_cid
, &u
.ci32
.scir_flags
,
697 &u
.ci32
.scir_ifindex
, &u
.ci32
.scir_error
, u
.ci32
.scir_src
,
698 &u
.ci32
.scir_src_len
, u
.ci32
.scir_dst
, &u
.ci32
.scir_dst_len
,
699 &u
.ci32
.scir_aux_type
, u
.ci32
.scir_aux_data
,
700 &u
.ci32
.scir_aux_len
);
702 bcopy(&u
.ci32
, data
, sizeof(u
.ci32
));
707 case SIOCGCONNINFO64
: { /* struct so_cinforeq64 */
708 bcopy(data
, &u
.ci64
, sizeof(u
.ci64
));
709 error
= in6_getconninfo(so
, u
.ci64
.scir_cid
, &u
.ci64
.scir_flags
,
710 &u
.ci64
.scir_ifindex
, &u
.ci64
.scir_error
, u
.ci64
.scir_src
,
711 &u
.ci64
.scir_src_len
, u
.ci64
.scir_dst
, &u
.ci64
.scir_dst_len
,
712 &u
.ci64
.scir_aux_type
, u
.ci64
.scir_aux_data
,
713 &u
.ci64
.scir_aux_len
);
715 bcopy(&u
.ci64
, data
, sizeof(u
.ci64
));
728 static __attribute__((noinline
)) int
729 in6ctl_llstart(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
731 struct in6_aliasreq sifra
, *ifra
= NULL
;
738 case SIOCLL_START_32
: /* struct in6_aliasreq_32 */
739 case SIOCLL_START_64
: /* struct in6_aliasreq_64 */
740 is64
= (cmd
== SIOCLL_START_64
);
742 * Convert user ifra to the kernel form, when appropriate.
743 * This allows the conversion between different data models
744 * to be centralized, so that it can be passed around to other
745 * routines that are expecting the kernel form.
747 ifra
= in6_aliasreq_to_native(data
, is64
, &sifra
);
750 * NOTE: All the interface specific DLIL attachements should
751 * be done here. They are currently done in in6_ifattach_aux()
752 * for the interfaces that need it.
754 if (ifra
->ifra_addr
.sin6_family
== AF_INET6
&&
755 /* Only check ifra_dstaddr if valid */
756 (ifra
->ifra_dstaddr
.sin6_len
== 0 ||
757 ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
)) {
758 /* some interfaces may provide LinkLocal addresses */
759 error
= in6_ifattach_aliasreq(ifp
, NULL
, ifra
);
761 error
= in6_ifattach_aliasreq(ifp
, NULL
, NULL
);
764 in6_if_up_dad_start(ifp
);
776 static __attribute__((noinline
)) int
777 in6ctl_llstop(struct ifnet
*ifp
)
779 struct in6_ifaddr
*ia
;
780 struct nd_prefix pr0
, *pr
;
784 /* Remove link local addresses from interface */
785 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
788 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
792 IFA_LOCK(&ia
->ia_ifa
);
793 if (IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
)) {
794 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
795 IFA_UNLOCK(&ia
->ia_ifa
);
796 lck_rw_done(&in6_ifaddr_rwlock
);
797 in6_purgeaddr(&ia
->ia_ifa
);
798 IFA_REMREF(&ia
->ia_ifa
); /* for us */
799 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
801 * Purging the address caused in6_ifaddr_rwlock
802 * to be dropped and reacquired;
803 * therefore search again from the beginning
804 * of in6_ifaddrs list.
809 IFA_UNLOCK(&ia
->ia_ifa
);
812 lck_rw_done(&in6_ifaddr_rwlock
);
814 /* Delete the link local prefix */
815 bzero(&pr0
, sizeof(pr0
));
818 pr0
.ndpr_prefix
.sin6_addr
.s6_addr16
[0] = IPV6_ADDR_INT16_ULL
;
819 in6_setscope(&pr0
.ndpr_prefix
.sin6_addr
, ifp
, NULL
);
820 pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_UNSPEC
);
822 lck_mtx_lock(nd6_mutex
);
826 NDPR_REMREF(pr
); /* Drop the reference from lookup */
827 lck_mtx_unlock(nd6_mutex
);
834 * This routine configures secure link local address
836 static __attribute__((noinline
)) int
837 in6ctl_cgastart(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
839 struct in6_cgareq llcgasr
;
845 case SIOCLL_CGASTART_32
: /* struct in6_cgareq_32 */
846 case SIOCLL_CGASTART_64
: /* struct in6_cgareq_64 */
847 is64
= (cmd
== SIOCLL_CGASTART_64
);
849 * Convert user cgareq to the kernel form, when appropriate.
850 * This allows the conversion between different data models
851 * to be centralized, so that it can be passed around to other
852 * routines that are expecting the kernel form.
854 in6_cgareq_to_native(data
, is64
, &llcgasr
);
857 * NOTE: All the interface specific DLIL attachements
858 * should be done here. They are currently done in
859 * in6_ifattach_cgareq() for the interfaces that
862 error
= in6_ifattach_llcgareq(ifp
, &llcgasr
);
864 in6_if_up_dad_start(ifp
);
877 * Caller passes in the ioctl data pointer directly via "ifr", with the
878 * expectation that this routine always uses bcopy() or other byte-aligned
881 static __attribute__((noinline
)) int
882 in6ctl_gifaddr(struct ifnet
*ifp
, struct in6_ifaddr
*ia
, u_long cmd
,
883 struct in6_ifreq
*ifr
)
885 struct sockaddr_in6 addr
;
891 return EADDRNOTAVAIL
;
895 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
896 IFA_LOCK(&ia
->ia_ifa
);
897 bcopy(&ia
->ia_addr
, &addr
, sizeof(addr
));
898 IFA_UNLOCK(&ia
->ia_ifa
);
899 if ((error
= sa6_recoverscope(&addr
, TRUE
)) != 0) {
902 bcopy(&addr
, &ifr
->ifr_addr
, sizeof(addr
));
905 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
906 if (!(ifp
->if_flags
& IFF_POINTOPOINT
)) {
911 * XXX: should we check if ifa_dstaddr is NULL and return
914 IFA_LOCK(&ia
->ia_ifa
);
915 bcopy(&ia
->ia_dstaddr
, &addr
, sizeof(addr
));
916 IFA_UNLOCK(&ia
->ia_ifa
);
917 if ((error
= sa6_recoverscope(&addr
, TRUE
)) != 0) {
920 bcopy(&addr
, &ifr
->ifr_dstaddr
, sizeof(addr
));
932 * Caller passes in the ioctl data pointer directly via "ifr", with the
933 * expectation that this routine always uses bcopy() or other byte-aligned
936 static __attribute__((noinline
)) int
937 in6ctl_gifstat(struct ifnet
*ifp
, u_long cmd
, struct in6_ifreq
*ifr
)
939 int error
= 0, index
;
942 index
= ifp
->if_index
;
945 case SIOCGIFSTAT_IN6
: /* struct in6_ifreq */
946 /* N.B.: if_inet6data is never freed once set. */
947 if (IN6_IFEXTRA(ifp
) == NULL
) {
948 /* return (EAFNOSUPPORT)? */
949 bzero(&ifr
->ifr_ifru
.ifru_stat
,
950 sizeof(ifr
->ifr_ifru
.ifru_stat
));
952 bcopy(&IN6_IFEXTRA(ifp
)->in6_ifstat
,
953 &ifr
->ifr_ifru
.ifru_stat
,
954 sizeof(ifr
->ifr_ifru
.ifru_stat
));
958 case SIOCGIFSTAT_ICMP6
: /* struct in6_ifreq */
959 /* N.B.: if_inet6data is never freed once set. */
960 if (IN6_IFEXTRA(ifp
) == NULL
) {
961 /* return (EAFNOSUPPORT)? */
962 bzero(&ifr
->ifr_ifru
.ifru_icmp6stat
,
963 sizeof(ifr
->ifr_ifru
.ifru_icmp6stat
));
965 bcopy(&IN6_IFEXTRA(ifp
)->icmp6_ifstat
,
966 &ifr
->ifr_ifru
.ifru_icmp6stat
,
967 sizeof(ifr
->ifr_ifru
.ifru_icmp6stat
));
980 * Caller passes in the ioctl data pointer directly via "ifr", with the
981 * expectation that this routine always uses bcopy() or other byte-aligned
984 static __attribute__((noinline
)) int
985 in6ctl_alifetime(struct in6_ifaddr
*ia
, u_long cmd
, struct in6_ifreq
*ifr
,
988 uint64_t timenow
= net_uptime();
989 struct in6_addrlifetime ia6_lt
;
990 struct timeval caltime
;
994 return EADDRNOTAVAIL
;
998 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
999 IFA_LOCK(&ia
->ia_ifa
);
1000 /* retrieve time as calendar time (last arg is 1) */
1001 in6ifa_getlifetime(ia
, &ia6_lt
, 1);
1003 struct in6_addrlifetime_64 lt
;
1005 bzero(<
, sizeof(lt
));
1006 lt
.ia6t_expire
= ia6_lt
.ia6t_expire
;
1007 lt
.ia6t_preferred
= ia6_lt
.ia6t_preferred
;
1008 lt
.ia6t_vltime
= ia6_lt
.ia6t_vltime
;
1009 lt
.ia6t_pltime
= ia6_lt
.ia6t_pltime
;
1010 bcopy(<
, &ifr
->ifr_ifru
.ifru_lifetime
, sizeof(lt
));
1012 struct in6_addrlifetime_32 lt
;
1014 bzero(<
, sizeof(lt
));
1015 lt
.ia6t_expire
= (uint32_t)ia6_lt
.ia6t_expire
;
1016 lt
.ia6t_preferred
= (uint32_t)ia6_lt
.ia6t_preferred
;
1017 lt
.ia6t_vltime
= (uint32_t)ia6_lt
.ia6t_vltime
;
1018 lt
.ia6t_pltime
= (uint32_t)ia6_lt
.ia6t_pltime
;
1019 bcopy(<
, &ifr
->ifr_ifru
.ifru_lifetime
, sizeof(lt
));
1021 IFA_UNLOCK(&ia
->ia_ifa
);
1024 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
1025 getmicrotime(&caltime
);
1027 /* sanity for overflow - beware unsigned */
1029 struct in6_addrlifetime_64 lt
;
1031 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof(lt
));
1032 if (lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
&&
1033 lt
.ia6t_vltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1037 if (lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
&&
1038 lt
.ia6t_pltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1043 struct in6_addrlifetime_32 lt
;
1045 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof(lt
));
1046 if (lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
&&
1047 lt
.ia6t_vltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1051 if (lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
&&
1052 lt
.ia6t_pltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1058 IFA_LOCK(&ia
->ia_ifa
);
1060 struct in6_addrlifetime_64 lt
;
1062 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof(lt
));
1063 ia6_lt
.ia6t_expire
= lt
.ia6t_expire
;
1064 ia6_lt
.ia6t_preferred
= lt
.ia6t_preferred
;
1065 ia6_lt
.ia6t_vltime
= lt
.ia6t_vltime
;
1066 ia6_lt
.ia6t_pltime
= lt
.ia6t_pltime
;
1068 struct in6_addrlifetime_32 lt
;
1070 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof(lt
));
1071 ia6_lt
.ia6t_expire
= (uint32_t)lt
.ia6t_expire
;
1072 ia6_lt
.ia6t_preferred
= (uint32_t)lt
.ia6t_preferred
;
1073 ia6_lt
.ia6t_vltime
= lt
.ia6t_vltime
;
1074 ia6_lt
.ia6t_pltime
= lt
.ia6t_pltime
;
1077 if (ia6_lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
1078 ia6_lt
.ia6t_expire
= timenow
+ ia6_lt
.ia6t_vltime
;
1080 ia6_lt
.ia6t_expire
= 0;
1083 if (ia6_lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
1084 ia6_lt
.ia6t_preferred
= timenow
+ ia6_lt
.ia6t_pltime
;
1086 ia6_lt
.ia6t_preferred
= 0;
1089 in6ifa_setlifetime(ia
, &ia6_lt
);
1090 IFA_UNLOCK(&ia
->ia_ifa
);
1102 in6ctl_clat46start(struct ifnet
*ifp
)
1104 struct nd_prefix
*pr
= NULL
;
1105 struct nd_prefix
*next
= NULL
;
1106 struct in6_ifaddr
*ia6
= NULL
;
1109 if (ifp
== lo_ifp
) {
1113 * Traverse the list of prefixes and find the first non-linklocal
1114 * prefix on the interface.
1115 * For that found eligible prefix, configure a CLAT46 reserved address.
1117 lck_mtx_lock(nd6_mutex
);
1118 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
1119 next
= pr
->ndpr_next
;
1122 if (pr
->ndpr_ifp
!= ifp
) {
1127 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
1132 if (pr
->ndpr_raf_auto
== 0) {
1137 if (pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
1142 if ((pr
->ndpr_stateflags
& NDPRF_CLAT46
) == 0
1143 && pr
->ndpr_vltime
!= 0) {
1144 NDPR_ADDREF_LOCKED(pr
); /* Take reference for rest of the processing */
1152 lck_mtx_unlock(nd6_mutex
);
1155 if ((ia6
= in6_pfx_newpersistaddr(pr
, FALSE
, &error
, TRUE
)) == NULL
) {
1156 nd6log0((LOG_ERR
, "Could not configure CLAT46 address on interface "
1157 "%s.\n", ifp
->if_xname
));
1159 IFA_LOCK(&ia6
->ia_ifa
);
1162 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
1163 pr
->ndpr_stateflags
|= NDPRF_CLAT46
;
1165 VERIFY(pr
->ndpr_addrcnt
!= 0);
1167 IFA_UNLOCK(&ia6
->ia_ifa
);
1168 IFA_REMREF(&ia6
->ia_ifa
);
1171 * A newly added address might affect the status
1172 * of other addresses, so we check and update it.
1173 * XXX: what if address duplication happens?
1175 lck_mtx_lock(nd6_mutex
);
1176 pfxlist_onlink_check();
1177 lck_mtx_unlock(nd6_mutex
);
1184 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(void *)(ifa))
1187 * Generic INET6 control operations (ioctl's).
1189 * ifp is NULL if not an interface-specific ioctl.
1191 * Most of the routines called to handle the ioctls would end up being
1192 * tail-call optimized, which unfortunately causes this routine to
1193 * consume too much stack space; this is the reason for the "noinline"
1194 * attribute used on those routines.
1196 * If called directly from within the networking stack (as opposed to via
1197 * pru_control), the socket parameter may be NULL.
1200 in6_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
1203 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
1204 struct in6_aliasreq sifra
, *ifra
= NULL
;
1205 struct in6_ifaddr
*ia
= NULL
;
1206 struct sockaddr_in6 sin6
, *sa6
= NULL
;
1207 boolean_t privileged
= (proc_suser(p
) == 0);
1208 boolean_t p64
= proc_is64bit(p
);
1209 boolean_t so_unlocked
= FALSE
;
1210 int intval
, error
= 0;
1212 /* In case it's NULL, make sure it came from the kernel */
1213 VERIFY(so
!= NULL
|| p
== kernproc
);
1216 * ioctls which don't require ifp, may require socket.
1219 case SIOCAADDRCTL_POLICY
: /* struct in6_addrpolicy */
1220 case SIOCDADDRCTL_POLICY
: /* struct in6_addrpolicy */
1224 return in6_src_ioctl(cmd
, data
);
1227 case SIOCDRADD_IN6_32
: /* struct in6_defrouter_32 */
1228 case SIOCDRADD_IN6_64
: /* struct in6_defrouter_64 */
1229 case SIOCDRDEL_IN6_32
: /* struct in6_defrouter_32 */
1230 case SIOCDRDEL_IN6_64
: /* struct in6_defrouter_64 */
1234 return defrtrlist_ioctl(cmd
, data
);
1237 case SIOCGASSOCIDS32
: /* struct so_aidreq32 */
1238 case SIOCGASSOCIDS64
: /* struct so_aidreq64 */
1239 return in6ctl_associd(so
, cmd
, data
);
1242 case SIOCGCONNIDS32
: /* struct so_cidreq32 */
1243 case SIOCGCONNIDS64
: /* struct so_cidreq64 */
1244 return in6ctl_connid(so
, cmd
, data
);
1247 case SIOCGCONNINFO32
: /* struct so_cinforeq32 */
1248 case SIOCGCONNINFO64
: /* struct so_cinforeq64 */
1249 return in6ctl_conninfo(so
, cmd
, data
);
1254 * The rest of ioctls require ifp; reject if we don't have one;
1255 * return ENXIO to be consistent with ifioctl().
1262 * Unlock the socket since ifnet_ioctl() may be invoked by
1263 * one of the ioctl handlers below. Socket will be re-locked
1264 * prior to returning.
1267 socket_unlock(so
, 0);
1272 * ioctls which require ifp but not interface address.
1275 case SIOCAUTOCONF_START
: /* struct in6_ifreq */
1280 error
= in6_autoconf(ifp
, TRUE
);
1283 case SIOCAUTOCONF_STOP
: /* struct in6_ifreq */
1288 error
= in6_autoconf(ifp
, FALSE
);
1291 case SIOCLL_START_32
: /* struct in6_aliasreq_32 */
1292 case SIOCLL_START_64
: /* struct in6_aliasreq_64 */
1297 error
= in6ctl_llstart(ifp
, cmd
, data
);
1300 case SIOCLL_STOP
: /* struct in6_ifreq */
1305 error
= in6ctl_llstop(ifp
);
1308 case SIOCCLAT46_START
: /* struct in6_ifreq */
1313 error
= in6ctl_clat46start(ifp
);
1315 ifp
->if_eflags
|= IFEF_CLAT46
;
1319 case SIOCCLAT46_STOP
: /* struct in6_ifreq */
1326 * Not much to be done here and it might not be needed
1327 * It would usually be done when IPv6 configuration is being
1329 * XXX Probably STOP equivalent is not needed here.
1331 ifp
->if_eflags
&= ~IFEF_CLAT46
;
1333 case SIOCSETROUTERMODE_IN6
: /* struct in6_ifreq */
1338 bcopy(&((struct in6_ifreq
*)(void *)data
)->ifr_intval
,
1339 &intval
, sizeof(intval
));
1341 error
= in6_setrouter(ifp
, intval
);
1344 case SIOCPROTOATTACH_IN6_32
: /* struct in6_aliasreq_32 */
1345 case SIOCPROTOATTACH_IN6_64
: /* struct in6_aliasreq_64 */
1350 error
= in6_domifattach(ifp
);
1353 case SIOCPROTODETACH_IN6
: /* struct in6_ifreq */
1358 /* Cleanup interface routes and addresses */
1361 if ((error
= proto_unplumb(PF_INET6
, ifp
))) {
1362 log(LOG_ERR
, "SIOCPROTODETACH_IN6: %s error=%d\n",
1363 if_name(ifp
), error
);
1367 case SIOCSNDFLUSH_IN6
: /* struct in6_ifreq */
1368 case SIOCSPFXFLUSH_IN6
: /* struct in6_ifreq */
1369 case SIOCSRTRFLUSH_IN6
: /* struct in6_ifreq */
1370 case SIOCSDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
1371 case SIOCSDEFIFACE_IN6_64
: /* struct in6_ndifreq_64 */
1372 case SIOCSIFINFO_FLAGS
: /* struct in6_ndireq */
1373 case SIOCGIFCGAPREP_IN6
: /* struct in6_ifreq */
1374 case SIOCSIFCGAPREP_IN6
: /* struct in6_ifreq */
1380 case OSIOCGIFINFO_IN6
: /* struct in6_ondireq */
1381 case SIOCGIFINFO_IN6
: /* struct in6_ondireq */
1382 case SIOCGDRLST_IN6_32
: /* struct in6_drlist_32 */
1383 case SIOCGDRLST_IN6_64
: /* struct in6_drlist_64 */
1384 case SIOCGPRLST_IN6_32
: /* struct in6_prlist_32 */
1385 case SIOCGPRLST_IN6_64
: /* struct in6_prlist_64 */
1386 case SIOCGNBRINFO_IN6_32
: /* struct in6_nbrinfo_32 */
1387 case SIOCGNBRINFO_IN6_64
: /* struct in6_nbrinfo_64 */
1388 case SIOCGDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
1389 case SIOCGDEFIFACE_IN6_64
: /* struct in6_ndifreq_64 */
1390 error
= nd6_ioctl(cmd
, data
, ifp
);
1393 case SIOCSIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1394 case SIOCDIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1395 case SIOCAIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1396 case SIOCCIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1397 case SIOCSGIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1398 case SIOCGIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1400 "prefix ioctls are now invalidated. "
1401 "please use ifconfig.\n");
1405 case SIOCSSCOPE6
: /* struct in6_ifreq (deprecated) */
1406 case SIOCGSCOPE6
: /* struct in6_ifreq (deprecated) */
1407 case SIOCGSCOPE6DEF
: /* struct in6_ifreq (deprecated) */
1411 case SIOCLL_CGASTART_32
: /* struct in6_cgareq_32 */
1412 case SIOCLL_CGASTART_64
: /* struct in6_cgareq_64 */
1416 error
= in6ctl_cgastart(ifp
, cmd
, data
);
1420 case SIOCGIFSTAT_IN6
: /* struct in6_ifreq */
1421 case SIOCGIFSTAT_ICMP6
: /* struct in6_ifreq */
1422 error
= in6ctl_gifstat(ifp
, cmd
, ifr
);
1427 * ioctls which require interface address; obtain sockaddr_in6.
1430 case SIOCSIFADDR_IN6
: /* struct in6_ifreq (deprecated) */
1431 case SIOCSIFDSTADDR_IN6
: /* struct in6_ifreq (deprecated) */
1432 case SIOCSIFNETMASK_IN6
: /* struct in6_ifreq (deprecated) */
1434 * Since IPv6 allows a node to assign multiple addresses
1435 * on a single interface, SIOCSIFxxx ioctls are deprecated.
1437 /* we decided to obsolete this command (20000704) */
1441 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1442 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1448 * Convert user ifra to the kernel form, when appropriate.
1449 * This allows the conversion between different data models
1450 * to be centralized, so that it can be passed around to other
1451 * routines that are expecting the kernel form.
1453 ifra
= in6_aliasreq_to_native(data
,
1454 (cmd
== SIOCAIFADDR_IN6_64
), &sifra
);
1455 bcopy(&ifra
->ifra_addr
, &sin6
, sizeof(sin6
));
1459 case SIOCDIFADDR_IN6
: /* struct in6_ifreq */
1460 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
1466 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
1467 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
1468 case SIOCGIFNETMASK_IN6
: /* struct in6_ifreq */
1469 case SIOCGIFAFLAG_IN6
: /* struct in6_ifreq */
1470 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
1471 bcopy(&ifr
->ifr_addr
, &sin6
, sizeof(sin6
));
1474 case SIOCGIFDSTADDR
:
1475 case SIOCSIFDSTADDR
:
1476 case SIOCGIFBRDADDR
:
1477 case SIOCSIFBRDADDR
:
1478 case SIOCGIFNETMASK
:
1479 case SIOCSIFNETMASK
:
1484 /* Do not handle these AF_INET commands in AF_INET6 path */
1490 * Find address for this interface, if it exists.
1492 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
1493 * only, and used the first interface address as the target of other
1494 * operations (without checking ifra_addr). This was because netinet
1495 * code/API assumed at most 1 interface address per interface.
1496 * Since IPv6 allows a node to assign multiple addresses
1497 * on a single interface, we almost always look and check the
1498 * presence of ifra_addr, and reject invalid ones here.
1499 * It also decreases duplicated code among SIOC*_IN6 operations.
1502 if (sa6
!= NULL
&& sa6
->sin6_family
== AF_INET6
) {
1503 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
1504 if (sa6
->sin6_addr
.s6_addr16
[1] == 0) {
1505 /* link ID is not embedded by the user */
1506 sa6
->sin6_addr
.s6_addr16
[1] =
1507 htons(ifp
->if_index
);
1508 } else if (sa6
->sin6_addr
.s6_addr16
[1] !=
1509 htons(ifp
->if_index
)) {
1510 error
= EINVAL
; /* link ID contradicts */
1513 if (sa6
->sin6_scope_id
) {
1514 if (sa6
->sin6_scope_id
!=
1515 (u_int32_t
)ifp
->if_index
) {
1519 sa6
->sin6_scope_id
= 0; /* XXX: good way? */
1523 * Any failures from this point on must take into account
1524 * a non-NULL "ia" with an outstanding reference count, and
1525 * therefore requires IFA_REMREF. Jump to "done" label
1526 * instead of calling return if "ia" is valid.
1528 ia
= in6ifa_ifpwithaddr(ifp
, &sa6
->sin6_addr
);
1532 * SIOCDIFADDR_IN6/SIOCAIFADDR_IN6 specific tests.
1535 case SIOCDIFADDR_IN6
: /* struct in6_ifreq */
1537 error
= EADDRNOTAVAIL
;
1541 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1542 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1543 VERIFY(sa6
!= NULL
);
1545 * We always require users to specify a valid IPv6 address for
1546 * the corresponding operation. Use "sa6" instead of "ifra"
1547 * since SIOCDIFADDR_IN6 falls thru above.
1549 if (sa6
->sin6_family
!= AF_INET6
||
1550 sa6
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
1551 error
= EAFNOSUPPORT
;
1558 * And finally process address-related ioctls.
1561 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
1562 /* This interface is basically deprecated. use SIOCGIFCONF. */
1564 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
1565 error
= in6ctl_gifaddr(ifp
, ia
, cmd
, ifr
);
1568 case SIOCGIFNETMASK_IN6
: /* struct in6_ifreq */
1570 IFA_LOCK(&ia
->ia_ifa
);
1571 bcopy(&ia
->ia_prefixmask
, &ifr
->ifr_addr
,
1572 sizeof(struct sockaddr_in6
));
1573 IFA_UNLOCK(&ia
->ia_ifa
);
1575 error
= EADDRNOTAVAIL
;
1579 case SIOCGIFAFLAG_IN6
: /* struct in6_ifreq */
1581 IFA_LOCK(&ia
->ia_ifa
);
1582 bcopy(&ia
->ia6_flags
, &ifr
->ifr_ifru
.ifru_flags6
,
1583 sizeof(ifr
->ifr_ifru
.ifru_flags6
));
1584 IFA_UNLOCK(&ia
->ia_ifa
);
1586 error
= EADDRNOTAVAIL
;
1590 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
1591 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
1592 error
= in6ctl_alifetime(ia
, cmd
, ifr
, p64
);
1595 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1596 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1597 error
= in6ctl_aifaddr(ifp
, ifra
);
1600 case SIOCDIFADDR_IN6
:
1601 in6ctl_difaddr(ifp
, ia
);
1605 error
= ifnet_ioctl(ifp
, PF_INET6
, cmd
, data
);
1611 IFA_REMREF(&ia
->ia_ifa
);
1620 static __attribute__((noinline
)) int
1621 in6ctl_aifaddr(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
)
1623 int i
, error
, addtmp
, plen
;
1624 struct nd_prefix pr0
, *pr
;
1625 struct in6_ifaddr
*ia
;
1627 VERIFY(ifp
!= NULL
&& ifra
!= NULL
);
1630 /* Attempt to attach the protocol, in case it isn't attached */
1631 error
= in6_domifattach(ifp
);
1633 /* PF_INET6 wasn't previously attached */
1634 error
= in6_ifattach_aliasreq(ifp
, NULL
, NULL
);
1639 in6_if_up_dad_start(ifp
);
1640 } else if (error
!= EEXIST
) {
1645 * First, make or update the interface address structure, and link it
1648 error
= in6_update_ifa(ifp
, ifra
, 0, &ia
);
1654 /* Now, make the prefix on-link on the interface. */
1655 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
, NULL
);
1661 * NOTE: We'd rather create the prefix before the address, but we need
1662 * at least one address to install the corresponding interface route,
1663 * so we configure the address first.
1667 * Convert mask to prefix length (prefixmask has already been validated
1668 * in in6_update_ifa().
1670 bzero(&pr0
, sizeof(pr0
));
1671 pr0
.ndpr_plen
= plen
;
1673 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
1674 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
1676 /* apply the mask for safety. */
1677 for (i
= 0; i
< 4; i
++) {
1678 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1679 ifra
->ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
1683 * Since we don't have an API to set prefix (not address) lifetimes, we
1684 * just use the same lifetimes as addresses. The (temporarily)
1685 * installed lifetimes can be overridden by later advertised RAs (when
1686 * accept_rtadv is non 0), which is an intended behavior.
1688 pr0
.ndpr_raf_onlink
= 1; /* should be configurable? */
1689 pr0
.ndpr_raf_auto
= !!(ifra
->ifra_flags
& IN6_IFF_AUTOCONF
);
1690 pr0
.ndpr_vltime
= ifra
->ifra_lifetime
.ia6t_vltime
;
1691 pr0
.ndpr_pltime
= ifra
->ifra_lifetime
.ia6t_pltime
;
1692 pr0
.ndpr_stateflags
|= NDPRF_STATIC
;
1693 lck_mtx_init(&pr0
.ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
1695 /* add the prefix if there's none. */
1696 if ((pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_NEVER
)) == NULL
) {
1698 * nd6_prelist_add will install the corresponding interface
1701 error
= nd6_prelist_add(&pr0
, NULL
, &pr
, FALSE
);
1707 log(LOG_ERR
, "%s: nd6_prelist_add okay, but"
1708 " no prefix.\n", __func__
);
1714 IFA_LOCK(&ia
->ia_ifa
);
1716 /* if this is a new autoconfed addr */
1718 if (ia
->ia6_ndpr
== NULL
) {
1721 VERIFY(pr
->ndpr_addrcnt
!= 0);
1723 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
1726 * If this is the first autoconf address from the prefix,
1727 * create a temporary address as well (when specified).
1729 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
1731 pr
->ndpr_addrcnt
== 1) {
1737 IFA_UNLOCK(&ia
->ia_ifa
);
1741 e
= in6_tmpifadd(ia
, 1);
1743 log(LOG_NOTICE
, "%s: failed to create a"
1744 " temporary address, error=%d\n",
1750 * This might affect the status of autoconfigured addresses, that is,
1751 * this address might make other addresses detached.
1753 lck_mtx_lock(nd6_mutex
);
1754 pfxlist_onlink_check();
1755 lck_mtx_unlock(nd6_mutex
);
1757 /* Drop use count held above during lookup/add */
1762 IFA_REMREF(&ia
->ia_ifa
);
1767 static __attribute__((noinline
)) void
1768 in6ctl_difaddr(struct ifnet
*ifp
, struct in6_ifaddr
*ia
)
1771 struct nd_prefix pr0
, *pr
;
1773 VERIFY(ifp
!= NULL
&& ia
!= NULL
);
1776 * If the address being deleted is the only one that owns
1777 * the corresponding prefix, expire the prefix as well.
1778 * XXX: theoretically, we don't have to worry about such
1779 * relationship, since we separate the address management
1780 * and the prefix management. We do this, however, to provide
1781 * as much backward compatibility as possible in terms of
1782 * the ioctl operation.
1783 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
1785 IFA_LOCK(&ia
->ia_ifa
);
1786 bzero(&pr0
, sizeof(pr0
));
1788 pr0
.ndpr_plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
);
1789 if (pr0
.ndpr_plen
== 128) {
1790 IFA_UNLOCK(&ia
->ia_ifa
);
1793 pr0
.ndpr_prefix
= ia
->ia_addr
;
1794 pr0
.ndpr_mask
= ia
->ia_prefixmask
.sin6_addr
;
1795 for (i
= 0; i
< 4; i
++) {
1796 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1797 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
];
1799 IFA_UNLOCK(&ia
->ia_ifa
);
1801 if ((pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_UNSPEC
)) != NULL
) {
1802 IFA_LOCK(&ia
->ia_ifa
);
1804 if (pr
->ndpr_addrcnt
== 1) {
1805 /* XXX: just for expiration */
1806 pr
->ndpr_expire
= 1;
1809 IFA_UNLOCK(&ia
->ia_ifa
);
1811 /* Drop use count held above during lookup */
1816 in6_purgeaddr(&ia
->ia_ifa
);
1819 static __attribute__((noinline
)) int
1820 in6_autoconf(struct ifnet
*ifp
, int enable
)
1824 VERIFY(ifp
!= NULL
);
1826 if (ifp
->if_flags
& IFF_LOOPBACK
) {
1832 * An interface in IPv6 router mode implies that it
1833 * is either configured with a static IP address or
1834 * autoconfigured via a locally-generated RA. Prevent
1835 * SIOCAUTOCONF_START from being set in that mode.
1837 ifnet_lock_exclusive(ifp
);
1838 if (ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
1839 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADV
;
1842 ifp
->if_eflags
|= IFEF_ACCEPT_RTADV
;
1844 ifnet_lock_done(ifp
);
1846 struct in6_ifaddr
*ia
= NULL
;
1848 ifnet_lock_exclusive(ifp
);
1849 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADV
;
1850 ifnet_lock_done(ifp
);
1852 /* Remove autoconfigured address from interface */
1853 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1855 while (ia
!= NULL
) {
1856 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
1860 IFA_LOCK(&ia
->ia_ifa
);
1861 if (ia
->ia6_flags
& IN6_IFF_AUTOCONF
) {
1862 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
1863 IFA_UNLOCK(&ia
->ia_ifa
);
1864 lck_rw_done(&in6_ifaddr_rwlock
);
1865 in6_purgeaddr(&ia
->ia_ifa
);
1866 IFA_REMREF(&ia
->ia_ifa
); /* for us */
1867 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1869 * Purging the address caused in6_ifaddr_rwlock
1870 * to be dropped and reacquired;
1871 * therefore search again from the beginning
1872 * of in6_ifaddrs list.
1877 IFA_UNLOCK(&ia
->ia_ifa
);
1880 lck_rw_done(&in6_ifaddr_rwlock
);
1886 * Handle SIOCSETROUTERMODE_IN6 to set or clear the IPv6 router mode flag on
1887 * the interface. Entering or exiting this mode will result in the removal of
1888 * autoconfigured IPv6 addresses on the interface.
1890 static __attribute__((noinline
)) int
1891 in6_setrouter(struct ifnet
*ifp
, int enable
)
1893 VERIFY(ifp
!= NULL
);
1895 if (ifp
->if_flags
& IFF_LOOPBACK
) {
1900 struct nd_ifinfo
*ndi
= NULL
;
1902 ndi
= ND_IFINFO(ifp
);
1903 if (ndi
!= NULL
&& ndi
->initialized
) {
1904 lck_mtx_lock(&ndi
->lock
);
1905 if (ndi
->flags
& ND6_IFF_PROXY_PREFIXES
) {
1906 /* No proxy if we are an advertising router */
1907 ndi
->flags
&= ~ND6_IFF_PROXY_PREFIXES
;
1908 lck_mtx_unlock(&ndi
->lock
);
1909 (void) nd6_if_prproxy(ifp
, FALSE
);
1911 lck_mtx_unlock(&ndi
->lock
);
1916 ifnet_lock_exclusive(ifp
);
1918 ifp
->if_eflags
|= IFEF_IPV6_ROUTER
;
1920 ifp
->if_eflags
&= ~IFEF_IPV6_ROUTER
;
1922 ifnet_lock_done(ifp
);
1924 lck_mtx_lock(nd6_mutex
);
1925 defrouter_select(ifp
);
1926 lck_mtx_unlock(nd6_mutex
);
1928 if_allmulti(ifp
, enable
);
1930 return in6_autoconf(ifp
, FALSE
);
1934 in6_to_kamescope(struct sockaddr_in6
*sin6
, struct ifnet
*ifp
)
1936 struct sockaddr_in6 tmp
;
1939 VERIFY(sin6
!= NULL
);
1942 error
= in6_recoverscope(&tmp
, &sin6
->sin6_addr
, ifp
);
1947 id
= in6_addr2scopeid(ifp
, &tmp
.sin6_addr
);
1948 if (tmp
.sin6_scope_id
== 0) {
1949 tmp
.sin6_scope_id
= id
;
1950 } else if (tmp
.sin6_scope_id
!= id
) {
1951 return EINVAL
; /* scope ID mismatch. */
1953 error
= in6_embedscope(&tmp
.sin6_addr
, &tmp
, NULL
, NULL
, NULL
);
1958 tmp
.sin6_scope_id
= 0;
1964 * When the address is being configured we should clear out certain flags
1965 * coming in from the caller.
1967 #define IN6_IFF_CLR_ADDR_FLAG_MASK (~(IN6_IFF_DEPRECATED | IN6_IFF_DETACHED | IN6_IFF_DUPLICATED))
1970 in6_ifaupdate_aux(struct in6_ifaddr
*ia
, struct ifnet
*ifp
, int ifaupflags
)
1972 struct sockaddr_in6 mltaddr
, mltmask
;
1973 struct in6_addr llsol
;
1975 struct in6_multi
*in6m_sol
;
1976 struct in6_multi_mship
*imm
;
1978 int delay
, error
= 0;
1980 VERIFY(ifp
!= NULL
&& ia
!= NULL
);
1984 nd6log2((LOG_DEBUG
, "%s - %s ifp %s ia6_flags 0x%x ifaupflags 0x%x\n",
1986 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1987 if_name(ia
->ia_ifp
),
1992 * Just to be safe, always clear certain flags when address
1993 * is being configured
1995 ia
->ia6_flags
&= IN6_IFF_CLR_ADDR_FLAG_MASK
;
1998 * Mark the address as tentative before joining multicast addresses,
1999 * so that corresponding MLD responses would not have a tentative
2002 if (in6if_do_dad(ifp
)) {
2003 in6_ifaddr_set_dadprogress(ia
);
2005 * Do not delay sending neighbor solicitations when using optimistic
2006 * duplicate address detection, c.f. RFC 4429.
2008 if (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
) {
2009 ifaupflags
&= ~IN6_IFAUPDATE_DADDELAY
;
2011 ifaupflags
|= IN6_IFAUPDATE_DADDELAY
;
2015 * If the interface has been marked to not perform
2016 * DAD, make sure to reset DAD in progress flags
2017 * that may come in from the caller.
2019 ia
->ia6_flags
&= ~IN6_IFF_DADPROGRESS
;
2022 /* Join necessary multicast groups */
2023 if ((ifp
->if_flags
& IFF_MULTICAST
) != 0) {
2024 /* join solicited multicast addr for new host id */
2025 bzero(&llsol
, sizeof(struct in6_addr
));
2026 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
2027 llsol
.s6_addr32
[1] = 0;
2028 llsol
.s6_addr32
[2] = htonl(1);
2029 llsol
.s6_addr32
[3] = ia
->ia_addr
.sin6_addr
.s6_addr32
[3];
2030 llsol
.s6_addr8
[12] = 0xff;
2031 if ((error
= in6_setscope(&llsol
, ifp
, NULL
)) != 0) {
2032 /* XXX: should not happen */
2033 log(LOG_ERR
, "%s: in6_setscope failed\n", __func__
);
2037 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
)) {
2039 * We need a random delay for DAD on the address
2040 * being configured. It also means delaying
2041 * transmission of the corresponding MLD report to
2042 * avoid report collision. [RFC 4862]
2044 delay
= random() % MAX_RTR_SOLICITATION_DELAY
;
2046 imm
= in6_joingroup(ifp
, &llsol
, &error
, delay
);
2048 nd6log((LOG_WARNING
,
2049 "%s: addmulti failed for %s on %s (errno=%d)\n",
2050 __func__
, ip6_sprintf(&llsol
), if_name(ifp
),
2055 in6m_sol
= imm
->i6mm_maddr
;
2056 /* take a refcount for this routine */
2057 IN6M_ADDREF(in6m_sol
);
2060 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
2063 bzero(&mltmask
, sizeof(mltmask
));
2064 mltmask
.sin6_len
= sizeof(struct sockaddr_in6
);
2065 mltmask
.sin6_family
= AF_INET6
;
2066 mltmask
.sin6_addr
= in6mask32
;
2067 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
2070 * join link-local all-nodes address
2072 bzero(&mltaddr
, sizeof(mltaddr
));
2073 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
2074 mltaddr
.sin6_family
= AF_INET6
;
2075 mltaddr
.sin6_addr
= in6addr_linklocal_allnodes
;
2076 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
)) != 0) {
2077 goto unwind
; /* XXX: should not fail */
2080 * XXX: do we really need this automatic routes?
2081 * We should probably reconsider this stuff. Most applications
2082 * actually do not need the routes, since they usually specify
2083 * the outgoing interface.
2085 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
2086 ia
->ia_ifp
->if_index
);
2088 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
2089 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN
)) {
2095 error
= rtrequest_scoped(RTM_ADD
,
2096 (struct sockaddr
*)&mltaddr
,
2097 (struct sockaddr
*)&ia
->ia_addr
,
2098 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
2099 NULL
, ia
->ia_ifp
->if_index
);
2107 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
2109 nd6log((LOG_WARNING
,
2110 "%s: addmulti failed for %s on %s (errno=%d)\n",
2111 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
2112 if_name(ifp
), error
));
2117 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
2121 * join node information group address
2123 #define hostnamelen strlen(hostname)
2125 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
)) {
2127 * The spec doesn't say anything about delay for this
2128 * group, but the same logic should apply.
2130 delay
= random() % MAX_RTR_SOLICITATION_DELAY
;
2132 if (in6_nigroup(ifp
, hostname
, hostnamelen
, &mltaddr
.sin6_addr
)
2134 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
,
2135 delay
); /* XXX jinmei */
2137 nd6log((LOG_WARNING
,
2138 "%s: addmulti failed for %s on %s "
2140 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
2141 if_name(ifp
), error
));
2142 /* XXX not very fatal, go on... */
2146 LIST_INSERT_HEAD(&ia
->ia6_memberships
,
2154 * join interface-local all-nodes address.
2155 * (ff01::1%ifN, and ff01::%ifN/32)
2157 mltaddr
.sin6_addr
= in6addr_nodelocal_allnodes
;
2158 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
)) != 0) {
2159 goto unwind
; /* XXX: should not fail */
2161 /* XXX: again, do we really need the route? */
2162 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
2163 ia
->ia_ifp
->if_index
);
2165 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
2166 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN
)) {
2172 error
= rtrequest_scoped(RTM_ADD
,
2173 (struct sockaddr
*)&mltaddr
,
2174 (struct sockaddr
*)&ia
->ia_addr
,
2175 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
2176 NULL
, ia
->ia_ifp
->if_index
);
2184 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
2186 nd6log((LOG_WARNING
,
2187 "%s: addmulti failed for %s on %s (errno=%d)\n",
2188 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
2189 if_name(ifp
), error
));
2194 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
2199 /* Ensure nd6_service() is scheduled as soon as it's convenient */
2200 ++nd6_sched_timeout_want
;
2203 * Perform DAD, if needed.
2204 * XXX It may be of use, if we can administratively
2208 if (in6if_do_dad(ifp
) && ((ifa
->ifa_flags
& IN6_IFF_NODAD
) == 0) &&
2209 (ia
->ia6_flags
& IN6_IFF_DADPROGRESS
)) {
2210 int mindelay
, maxdelay
;
2211 int *delayptr
, delayval
;
2216 * Avoid the DAD delay if the caller wants us to skip it.
2217 * This is not compliant with RFC 2461, but it's only being
2218 * used for signalling and not for actual DAD.
2220 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
) &&
2221 !(ia
->ia6_flags
& IN6_IFF_SWIFTDAD
)) {
2223 * We need to impose a delay before sending an NS
2224 * for DAD. Check if we also needed a delay for the
2225 * corresponding MLD message. If we did, the delay
2226 * should be larger than the MLD delay (this could be
2227 * relaxed a bit, but this simple logic is at least
2231 if (in6m_sol
!= NULL
) {
2232 IN6M_LOCK(in6m_sol
);
2233 if (in6m_sol
->in6m_state
==
2234 MLD_REPORTING_MEMBER
) {
2235 mindelay
= in6m_sol
->in6m_timer
;
2237 IN6M_UNLOCK(in6m_sol
);
2239 maxdelay
= MAX_RTR_SOLICITATION_DELAY
* hz
;
2240 if (maxdelay
- mindelay
== 0) {
2244 (random() % (maxdelay
- mindelay
)) +
2247 delayptr
= &delayval
;
2250 nd6_dad_start((struct ifaddr
*)ia
, delayptr
);
2259 in6_purgeaddr(&ia
->ia_ifa
);
2262 /* release reference held for this routine */
2263 if (in6m_sol
!= NULL
) {
2264 IN6M_REMREF(in6m_sol
);
2270 * Request an IPv6 interface address. If the address is new, then it will be
2271 * constructed and appended to the interface address chains. The interface
2272 * address structure is optionally returned with a reference for the caller.
2275 in6_update_ifa(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
, int ifaupflags
,
2276 struct in6_ifaddr
**iar
)
2278 struct in6_addrlifetime ia6_lt
;
2279 struct in6_ifaddr
*ia
;
2281 struct ifaddr
*xifa
;
2282 struct in6_addrlifetime
*lt
;
2286 /* Sanity check parameters and initialize locals */
2287 VERIFY(ifp
!= NULL
&& ifra
!= NULL
&& iar
!= NULL
);
2293 * We always require users to specify a valid IPv6 address for
2294 * the corresponding operation.
2296 if (ifra
->ifra_addr
.sin6_family
!= AF_INET6
||
2297 ifra
->ifra_addr
.sin6_len
!= sizeof(struct sockaddr_in6
)) {
2298 error
= EAFNOSUPPORT
;
2302 /* Validate ifra_prefixmask.sin6_len is properly bounded. */
2303 if (ifra
->ifra_prefixmask
.sin6_len
== 0 ||
2304 ifra
->ifra_prefixmask
.sin6_len
> sizeof(struct sockaddr_in6
)) {
2309 /* Validate prefix length extracted from ifra_prefixmask structure. */
2310 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
2311 (u_char
*)&ifra
->ifra_prefixmask
+ ifra
->ifra_prefixmask
.sin6_len
);
2317 /* Validate lifetimes */
2318 lt
= &ifra
->ifra_lifetime
;
2319 if (lt
->ia6t_pltime
> lt
->ia6t_vltime
) {
2321 "%s: pltime 0x%x > vltime 0x%x for %s\n", __func__
,
2322 lt
->ia6t_pltime
, lt
->ia6t_vltime
,
2323 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
2327 if (lt
->ia6t_vltime
== 0) {
2329 * the following log might be noisy, but this is a typical
2330 * configuration mistake or a tool's bug.
2332 log(LOG_INFO
, "%s: valid lifetime is 0 for %s\n", __func__
,
2333 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
2337 * Before we lock the ifnet structure, we first check to see if the
2338 * address already exists. If so, then we don't allocate and link a
2341 ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
);
2347 * Validate destination address on interface types that require it.
2349 if ((ifp
->if_flags
& (IFF_LOOPBACK
| IFF_POINTOPOINT
)) != 0) {
2350 switch (ifra
->ifra_dstaddr
.sin6_family
) {
2353 /* noisy message for diagnostic purposes */
2355 "%s: prefix length < 128 with"
2356 " explicit dstaddr.\n", __func__
);
2366 error
= EAFNOSUPPORT
;
2369 } else if (ifra
->ifra_dstaddr
.sin6_family
!= AF_UNSPEC
) {
2371 "%s: dstaddr valid only on p2p and loopback interfaces.\n",
2377 timenow
= net_uptime();
2382 /* Is this the first new IPv6 address for the interface? */
2383 ifaupflags
|= IN6_IFAUPDATE_NEWADDR
;
2385 /* Allocate memory for IPv6 interface address structure. */
2386 how
= !(ifaupflags
& IN6_IFAUPDATE_NOWAIT
) ? M_WAITOK
: 0;
2387 ia
= in6_ifaddr_alloc(how
);
2396 * Initialize interface address structure.
2398 * Note well: none of these sockaddr_in6 structures contain a
2399 * valid sin6_port, sin6_flowinfo or even a sin6_scope_id field.
2400 * We still embed link-local scope identifiers at the end of an
2401 * arbitrary fe80::/32 prefix, for historical reasons. Also, the
2402 * ifa_dstaddr field is always non-NULL on point-to-point and
2403 * loopback interfaces, and conventionally points to a socket
2404 * address of AF_UNSPEC family when there is no destination.
2406 * Please enjoy the dancing sea turtle.
2408 IFA_ADDREF(ifa
); /* for this and optionally for caller */
2409 ifa
->ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
2410 if (ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
||
2411 (ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) != 0) {
2412 ifa
->ifa_dstaddr
= (struct sockaddr
*)&ia
->ia_dstaddr
;
2414 ifa
->ifa_netmask
= (struct sockaddr
*)&ia
->ia_prefixmask
;
2416 ifa
->ifa_metric
= ifp
->if_metric
;
2417 ifa
->ifa_rtrequest
= nd6_rtrequest
;
2419 LIST_INIT(&ia
->ia6_memberships
);
2420 ia
->ia_addr
.sin6_family
= AF_INET6
;
2421 ia
->ia_addr
.sin6_len
= sizeof(ia
->ia_addr
);
2422 ia
->ia_addr
.sin6_addr
= ifra
->ifra_addr
.sin6_addr
;
2423 ia
->ia_prefixmask
.sin6_family
= AF_INET6
;
2424 ia
->ia_prefixmask
.sin6_len
= sizeof(ia
->ia_prefixmask
);
2425 ia
->ia_prefixmask
.sin6_addr
= ifra
->ifra_prefixmask
.sin6_addr
;
2426 error
= in6_to_kamescope(&ia
->ia_addr
, ifp
);
2430 if (ifa
->ifa_dstaddr
!= NULL
) {
2431 ia
->ia_dstaddr
= ifra
->ifra_dstaddr
;
2432 error
= in6_to_kamescope(&ia
->ia_dstaddr
, ifp
);
2438 /* Append to address chains */
2439 ifnet_lock_exclusive(ifp
);
2440 ifaupflags
|= IN6_IFAUPDATE_1STADDR
;
2441 TAILQ_FOREACH(xifa
, &ifp
->if_addrlist
, ifa_list
) {
2442 IFA_LOCK_SPIN(xifa
);
2443 if (xifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2445 ifaupflags
&= ~IN6_IFAUPDATE_1STADDR
;
2452 if_attach_ifa(ifp
, ifa
); /* holds reference for ifnet link */
2454 ifnet_lock_done(ifp
);
2456 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2457 if (in6_ifaddrs
!= NULL
) {
2458 struct in6_ifaddr
*iac
;
2459 for (iac
= in6_ifaddrs
; iac
->ia_next
!= NULL
;
2460 iac
= iac
->ia_next
) {
2467 IFA_ADDREF(ifa
); /* hold for in6_ifaddrs link */
2468 lck_rw_done(&in6_ifaddr_rwlock
);
2471 ifaupflags
&= ~(IN6_IFAUPDATE_NEWADDR
| IN6_IFAUPDATE_1STADDR
);
2474 VERIFY(ia
!= NULL
&& ifa
== &ia
->ia_ifa
);
2478 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
2479 * to see if the address is deprecated or invalidated, but initialize
2480 * these members for applications.
2482 ia
->ia6_updatetime
= ia
->ia6_createtime
= timenow
;
2484 if (ia6_lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
2485 ia6_lt
.ia6t_expire
= timenow
+ ia6_lt
.ia6t_vltime
;
2487 ia6_lt
.ia6t_expire
= 0;
2489 if (ia6_lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
2490 ia6_lt
.ia6t_preferred
= timenow
+ ia6_lt
.ia6t_pltime
;
2492 ia6_lt
.ia6t_preferred
= 0;
2494 in6ifa_setlifetime(ia
, &ia6_lt
);
2497 * Backward compatibility - if IN6_IFF_DEPRECATED is set from the
2498 * userland, make it deprecated.
2500 if ((ia
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0) {
2501 ia
->ia6_lifetime
.ia6ti_pltime
= 0;
2502 ia
->ia6_lifetime
.ia6ti_preferred
= timenow
;
2506 * Update flag or prefix length
2509 ia
->ia6_flags
= ifra
->ifra_flags
;
2511 /* Release locks (new address available to concurrent tasks) */
2514 /* Further initialization of the interface address */
2515 error
= in6_ifinit(ifp
, ia
, ifaupflags
);
2520 /* Finish updating the address while other tasks are working with it */
2521 error
= in6_ifaupdate_aux(ia
, ifp
, ifaupflags
);
2526 /* Return success (optionally w/ address for caller). */
2528 (void) ifnet_notify_address(ifp
, AF_INET6
);
2534 VERIFY(ifa
== &ia
->ia_ifa
);
2545 in6_purgeaddr(struct ifaddr
*ifa
)
2547 struct ifnet
*ifp
= ifa
->ifa_ifp
;
2548 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
2549 struct in6_multi_mship
*imm
;
2551 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2553 /* stop DAD processing */
2557 * delete route to the destination of the address being purged.
2558 * The interface must be p2p or loopback in this case.
2561 if ((ia
->ia_flags
& IFA_ROUTE
) && ia
->ia_plen
== 128) {
2565 rtf
= (ia
->ia_dstaddr
.sin6_family
== AF_INET6
) ? RTF_HOST
: 0;
2566 error
= rtinit(&(ia
->ia_ifa
), RTM_DELETE
, rtf
);
2568 log(LOG_ERR
, "in6_purgeaddr: failed to remove "
2569 "a route to the p2p destination: %s on %s, "
2571 ip6_sprintf(&ia
->ia_addr
.sin6_addr
), if_name(ifp
),
2573 /* proceed anyway... */
2576 ia
->ia_flags
&= ~IFA_ROUTE
;
2580 /* Remove ownaddr's loopback rtentry, if it exists. */
2581 in6_ifremloop(&(ia
->ia_ifa
));
2584 * leave from multicast groups we have joined for the interface
2587 while ((imm
= ia
->ia6_memberships
.lh_first
) != NULL
) {
2588 LIST_REMOVE(imm
, i6mm_chain
);
2590 in6_leavegroup(imm
);
2595 /* in6_unlink_ifa() will need exclusive access */
2596 in6_unlink_ifa(ia
, ifp
);
2597 in6_post_msg(ifp
, KEV_INET6_ADDR_DELETED
, ia
, NULL
);
2599 (void) ifnet_notify_address(ifp
, AF_INET6
);
2603 in6_unlink_ifa(struct in6_ifaddr
*ia
, struct ifnet
*ifp
)
2605 struct in6_ifaddr
*oia
;
2609 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2614 ifnet_lock_exclusive(ifp
);
2616 if (ifa
->ifa_debug
& IFD_ATTACHED
) {
2617 if_detach_ifa(ifp
, ifa
);
2620 ifnet_lock_done(ifp
);
2623 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2625 if (oia
== (ia
= in6_ifaddrs
)) {
2626 in6_ifaddrs
= ia
->ia_next
;
2628 while (ia
->ia_next
&& (ia
->ia_next
!= oia
)) {
2632 ia
->ia_next
= oia
->ia_next
;
2635 log(LOG_NOTICE
, "%s: search failed.\n", __func__
);
2641 * When IPv6 address is being removed, release the
2642 * reference to the base prefix.
2643 * Also, since the release might, affect the status
2644 * of other (detached) addresses, call
2645 * pfxlist_onlink_check().
2650 * Only log the below message for addresses other than
2652 * Only one LLA (auto-configured or statically) is allowed
2654 * LLA prefix, while added to the prefix list, is not
2655 * reference countedi (as it is the only one).
2656 * The prefix also never expires on its own as LLAs
2657 * have infinite lifetime.
2659 * For now quiece down the log message for LLAs.
2661 if (!IN6_IS_ADDR_LINKLOCAL(&oia
->ia_addr
.sin6_addr
)) {
2662 if (oia
->ia6_ndpr
== NULL
) {
2663 log(LOG_NOTICE
, "in6_unlink_ifa: IPv6 address "
2664 "0x%llx has no prefix\n",
2665 (uint64_t)VM_KERNEL_ADDRPERM(oia
));
2667 struct nd_prefix
*pr
= oia
->ia6_ndpr
;
2668 oia
->ia6_flags
&= ~IN6_IFF_AUTOCONF
;
2669 oia
->ia6_ndpr
= NULL
;
2671 VERIFY(pr
->ndpr_addrcnt
!= 0);
2673 if (oia
->ia6_flags
& IN6_IFF_CLAT46
) {
2674 pr
->ndpr_stateflags
&= ~NDPRF_CLAT46
;
2677 NDPR_REMREF(pr
); /* release addr reference */
2681 lck_rw_done(&in6_ifaddr_rwlock
);
2683 if ((oia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0) {
2684 lck_mtx_lock(nd6_mutex
);
2685 pfxlist_onlink_check();
2686 lck_mtx_unlock(nd6_mutex
);
2689 * release another refcnt for the link from in6_ifaddrs.
2690 * Do this only if it's not already unlinked in the event that we lost
2691 * the race, since in6_ifaddr_rwlock was momentarily dropped above.
2697 /* release reference held for this routine */
2700 /* invalidate route caches */
2701 routegenid_inet6_update();
2705 in6_purgeif(struct ifnet
*ifp
)
2707 struct in6_ifaddr
*ia
;
2713 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2715 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2717 while (ia
!= NULL
) {
2718 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
2722 IFA_ADDREF(&ia
->ia_ifa
); /* for us */
2723 lck_rw_done(&in6_ifaddr_rwlock
);
2724 in6_purgeaddr(&ia
->ia_ifa
);
2725 IFA_REMREF(&ia
->ia_ifa
); /* for us */
2726 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2728 * Purging the address would have caused
2729 * in6_ifaddr_rwlock to be dropped and reacquired;
2730 * therefore search again from the beginning
2731 * of in6_ifaddrs list.
2735 lck_rw_done(&in6_ifaddr_rwlock
);
2741 * Initialize an interface's internet6 address and routing table entry.
2744 in6_ifinit(struct ifnet
*ifp
, struct in6_ifaddr
*ia
, int ifaupflags
)
2753 * NOTE: SIOCSIFADDR is defined with struct ifreq as parameter,
2754 * but here we are sending it down to the interface with a pointer
2755 * to struct ifaddr, for legacy reasons.
2757 if ((ifaupflags
& IN6_IFAUPDATE_1STADDR
) != 0) {
2758 error
= ifnet_ioctl(ifp
, PF_INET6
, SIOCSIFADDR
, ia
);
2760 if (error
!= EOPNOTSUPP
) {
2771 * If the destination address is specified for a point-to-point
2772 * interface, install a route to the destination as an interface
2775 if (!(ia
->ia_flags
& IFA_ROUTE
) && ia
->ia_plen
== 128 &&
2776 ia
->ia_dstaddr
.sin6_family
== AF_INET6
) {
2778 error
= rtinit(ifa
, RTM_ADD
, RTF_UP
| RTF_HOST
);
2783 ia
->ia_flags
|= IFA_ROUTE
;
2785 IFA_LOCK_ASSERT_HELD(ifa
);
2786 if (ia
->ia_plen
< 128) {
2788 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
2790 ia
->ia_flags
|= RTF_CLONING
;
2795 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
2796 if ((ifaupflags
& IN6_IFAUPDATE_NEWADDR
) != 0) {
2800 /* invalidate route caches */
2801 routegenid_inet6_update();
2808 in6_purgeaddrs(struct ifnet
*ifp
)
2814 * Find an IPv6 interface link-local address specific to an interface.
2817 in6ifa_ifpforlinklocal(struct ifnet
*ifp
, int ignoreflags
)
2821 ifnet_lock_shared(ifp
);
2822 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2825 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2829 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
))) {
2830 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2831 ignoreflags
) != 0) {
2835 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2841 ifnet_lock_done(ifp
);
2843 return (struct in6_ifaddr
*)ifa
;
2847 in6ifa_ifpwithflag(struct ifnet
* ifp
, int flag
)
2851 ifnet_lock_shared(ifp
);
2852 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2855 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2859 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
& flag
) == flag
) {
2860 IFA_ADDREF_LOCKED(ifa
);
2866 ifnet_lock_done(ifp
);
2868 return (struct in6_ifaddr
*)ifa
;
2872 * find the internet address corresponding to a given interface and address.
2875 in6ifa_ifpwithaddr(struct ifnet
*ifp
, struct in6_addr
*addr
)
2879 ifnet_lock_shared(ifp
);
2880 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2883 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2887 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(ifa
))) {
2888 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2894 ifnet_lock_done(ifp
);
2896 return (struct in6_ifaddr
*)ifa
;
2900 in6ifa_prproxyaddr(struct in6_addr
*addr
)
2902 struct in6_ifaddr
*ia
;
2904 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2905 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2906 IFA_LOCK(&ia
->ia_ifa
);
2907 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(&ia
->ia_ifa
))) {
2908 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
2909 IFA_UNLOCK(&ia
->ia_ifa
);
2912 IFA_UNLOCK(&ia
->ia_ifa
);
2914 lck_rw_done(&in6_ifaddr_rwlock
);
2916 if (ia
!= NULL
&& !nd6_prproxy_ifaddr(ia
)) {
2917 IFA_REMREF(&ia
->ia_ifa
);
2925 in6ifa_getlifetime(struct in6_ifaddr
*ia6
, struct in6_addrlifetime
*t_dst
,
2928 struct in6_addrlifetime_i
*t_src
= &ia6
->ia6_lifetime
;
2929 struct timeval caltime
;
2931 t_dst
->ia6t_vltime
= t_src
->ia6ti_vltime
;
2932 t_dst
->ia6t_pltime
= t_src
->ia6ti_pltime
;
2933 t_dst
->ia6t_expire
= 0;
2934 t_dst
->ia6t_preferred
= 0;
2936 /* account for system time change */
2937 getmicrotime(&caltime
);
2938 t_src
->ia6ti_base_calendartime
+=
2939 NET_CALCULATE_CLOCKSKEW(caltime
,
2940 t_src
->ia6ti_base_calendartime
, net_uptime(),
2941 t_src
->ia6ti_base_uptime
);
2944 if (t_src
->ia6ti_expire
!= 0 &&
2945 t_src
->ia6ti_vltime
!= ND6_INFINITE_LIFETIME
) {
2946 t_dst
->ia6t_expire
= t_src
->ia6ti_base_calendartime
+
2947 t_src
->ia6ti_expire
- t_src
->ia6ti_base_uptime
;
2950 if (t_src
->ia6ti_preferred
!= 0 &&
2951 t_src
->ia6ti_pltime
!= ND6_INFINITE_LIFETIME
) {
2952 t_dst
->ia6t_preferred
= t_src
->ia6ti_base_calendartime
+
2953 t_src
->ia6ti_preferred
- t_src
->ia6ti_base_uptime
;
2956 if (t_src
->ia6ti_expire
!= 0 &&
2957 t_src
->ia6ti_vltime
!= ND6_INFINITE_LIFETIME
) {
2958 t_dst
->ia6t_expire
= t_src
->ia6ti_expire
;
2961 if (t_src
->ia6ti_preferred
!= 0 &&
2962 t_src
->ia6ti_pltime
!= ND6_INFINITE_LIFETIME
) {
2963 t_dst
->ia6t_preferred
= t_src
->ia6ti_preferred
;
2969 in6ifa_setlifetime(struct in6_ifaddr
*ia6
, struct in6_addrlifetime
*t_src
)
2971 struct in6_addrlifetime_i
*t_dst
= &ia6
->ia6_lifetime
;
2972 struct timeval caltime
;
2974 /* account for system time change */
2975 getmicrotime(&caltime
);
2976 t_dst
->ia6ti_base_calendartime
+=
2977 NET_CALCULATE_CLOCKSKEW(caltime
,
2978 t_dst
->ia6ti_base_calendartime
, net_uptime(),
2979 t_dst
->ia6ti_base_uptime
);
2981 /* trust the caller for the values */
2982 t_dst
->ia6ti_expire
= t_src
->ia6t_expire
;
2983 t_dst
->ia6ti_preferred
= t_src
->ia6t_preferred
;
2984 t_dst
->ia6ti_vltime
= t_src
->ia6t_vltime
;
2985 t_dst
->ia6ti_pltime
= t_src
->ia6t_pltime
;
2989 * Convert IP6 address to printable (loggable) representation.
2992 ip6_sprintf(const struct in6_addr
*addr
)
2994 static const char digits
[] = "0123456789abcdef";
2995 static int ip6round
= 0;
2996 static char ip6buf
[8][48];
3000 const u_short
*a
= (const u_short
*)addr
;
3006 ip6round
= (ip6round
+ 1) & 7;
3007 cp
= ip6buf
[ip6round
];
3009 for (i
= 0; i
< 8; i
++) {
3022 if (dcolon
== 0 && *(a
+ 1) == 0) {
3035 d
= (const u_char
*)a
;
3037 if ((n
= *d
>> 4) != 0) {
3041 if ((n
= *d
++ & 0xf) != 0 || zpad
) {
3045 if ((n
= *d
>> 4) != 0 || zpad
) {
3049 if ((n
= *d
& 0xf) != 0 || zpad
) {
3056 return ip6buf
[ip6round
];
3060 in6addr_local(struct in6_addr
*in6
)
3063 struct sockaddr_in6 sin6
;
3066 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_SCOPE_LINKLOCAL(in6
)) {
3070 sin6
.sin6_family
= AF_INET6
;
3071 sin6
.sin6_len
= sizeof(sin6
);
3072 bcopy(in6
, &sin6
.sin6_addr
, sizeof(*in6
));
3073 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0);
3077 if (rt
->rt_gateway
->sa_family
== AF_LINK
) {
3083 local
= in6_localaddr(in6
);
3089 in6_localaddr(struct in6_addr
*in6
)
3091 struct in6_ifaddr
*ia
;
3093 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
)) {
3097 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
3098 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
3099 IFA_LOCK_SPIN(&ia
->ia_ifa
);
3100 if (IN6_ARE_MASKED_ADDR_EQUAL(in6
, &ia
->ia_addr
.sin6_addr
,
3101 &ia
->ia_prefixmask
.sin6_addr
)) {
3102 IFA_UNLOCK(&ia
->ia_ifa
);
3103 lck_rw_done(&in6_ifaddr_rwlock
);
3106 IFA_UNLOCK(&ia
->ia_ifa
);
3108 lck_rw_done(&in6_ifaddr_rwlock
);
3113 * return length of part which dst and src are equal
3117 in6_matchlen(struct in6_addr
*src
, struct in6_addr
*dst
)
3120 u_char
*s
= (u_char
*)src
, *d
= (u_char
*)dst
;
3121 u_char
*lim
= s
+ 16, r
;
3124 if ((r
= (*d
++ ^ *s
++)) != 0) {
3137 /* XXX: to be scope conscious */
3139 in6_are_prefix_equal(struct in6_addr
*p1
, struct in6_addr
*p2
, int len
)
3141 int bytelen
, bitlen
;
3144 if (0 > len
|| len
> 128) {
3145 log(LOG_ERR
, "%s: invalid prefix length(%d)\n", __func__
, len
);
3152 if (bcmp(&p1
->s6_addr
, &p2
->s6_addr
, bytelen
)) {
3156 p1
->s6_addr
[bytelen
] >> (8 - bitlen
) !=
3157 p2
->s6_addr
[bytelen
] >> (8 - bitlen
)) {
3165 in6_prefixlen2mask(struct in6_addr
*maskp
, int len
)
3167 u_char maskarray
[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
3168 int bytelen
, bitlen
, i
;
3171 if (0 > len
|| len
> 128) {
3172 log(LOG_ERR
, "%s: invalid prefix length(%d)\n", __func__
, len
);
3176 bzero(maskp
, sizeof(*maskp
));
3179 for (i
= 0; i
< bytelen
; i
++) {
3180 maskp
->s6_addr
[i
] = 0xff;
3183 maskp
->s6_addr
[bytelen
] = maskarray
[bitlen
- 1];
3188 * return the best address out of the same scope
3191 in6_ifawithscope(struct ifnet
*oifp
, struct in6_addr
*dst
)
3193 int dst_scope
= in6_addrscope(dst
), src_scope
, best_scope
= 0;
3197 struct in6_ifaddr
*ifa_best
= NULL
;
3204 * We search for all addresses on all interfaces from the beginning.
3205 * Comparing an interface with the outgoing interface will be done
3206 * only at the final stage of tiebreaking.
3208 ifnet_head_lock_shared();
3209 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
3211 * We can never take an address that breaks the scope zone
3212 * of the destination.
3214 if (in6_addr2scopeid(ifp
, dst
) != in6_addr2scopeid(oifp
, dst
)) {
3218 ifnet_lock_shared(ifp
);
3219 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3220 int tlen
= -1, dscopecmp
, bscopecmp
, matchcmp
;
3223 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3227 src_scope
= in6_addrscope(IFA_IN6(ifa
));
3230 * Don't use an address before completing DAD
3231 * nor a duplicated address.
3233 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3234 (IN6_IFF_NOTREADY
| IN6_IFF_CLAT46
)) {
3238 /* XXX: is there any case to allow anycasts? */
3239 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3244 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3250 * If this is the first address we find,
3253 if (ifa_best
== NULL
) {
3258 * ifa_best is never NULL beyond this line except
3259 * within the block labeled "replace".
3263 * If ifa_best has a smaller scope than dst and
3264 * the current address has a larger one than
3265 * (or equal to) dst, always replace ifa_best.
3266 * Also, if the current address has a smaller scope
3267 * than dst, ignore it unless ifa_best also has a
3269 * Consequently, after the two if-clause below,
3270 * the followings must be satisfied:
3271 * (scope(src) < scope(dst) &&
3272 * scope(best) < scope(dst))
3274 * (scope(best) >= scope(dst) &&
3275 * scope(src) >= scope(dst))
3277 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0 &&
3278 IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) >= 0) {
3279 goto replace
; /* (A) */
3281 if (IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) < 0 &&
3282 IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) >= 0) {
3287 * A deprecated address SHOULD NOT be used in new
3288 * communications if an alternate (non-deprecated)
3289 * address is available and has sufficient scope.
3290 * RFC 4862, Section 5.5.4.
3292 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3293 IN6_IFF_DEPRECATED
) {
3295 * Ignore any deprecated addresses if
3296 * specified by configuration.
3298 if (!ip6_use_deprecated
) {
3303 * If we have already found a non-deprecated
3304 * candidate, just ignore deprecated addresses.
3306 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
)
3314 * A non-deprecated address is always preferred
3315 * to a deprecated one regardless of scopes and
3316 * address matching (Note invariants ensured by the
3317 * conditions (A) and (B) above.)
3319 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) &&
3320 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3321 IN6_IFF_DEPRECATED
) == 0) {
3326 * When we use temporary addresses described in
3327 * RFC 4941, we prefer temporary addresses to
3328 * public autoconf addresses. Again, note the
3329 * invariants from (A) and (B). Also note that we
3330 * don't have any preference between static addresses
3331 * and autoconf addresses (despite of whether or not
3332 * the latter is temporary or public.)
3334 if (ip6_use_tempaddr
) {
3335 struct in6_ifaddr
*ifat
;
3337 ifat
= (struct in6_ifaddr
*)ifa
;
3338 if ((ifa_best
->ia6_flags
&
3339 (IN6_IFF_AUTOCONF
| IN6_IFF_TEMPORARY
))
3340 == IN6_IFF_AUTOCONF
&&
3342 (IN6_IFF_AUTOCONF
| IN6_IFF_TEMPORARY
))
3343 == (IN6_IFF_AUTOCONF
| IN6_IFF_TEMPORARY
)) {
3346 if ((ifa_best
->ia6_flags
&
3347 (IN6_IFF_AUTOCONF
| IN6_IFF_TEMPORARY
))
3348 == (IN6_IFF_AUTOCONF
| IN6_IFF_TEMPORARY
) &&
3350 (IN6_IFF_AUTOCONF
| IN6_IFF_TEMPORARY
))
3351 == IN6_IFF_AUTOCONF
) {
3358 * At this point, we have two cases:
3359 * 1. we are looking at a non-deprecated address,
3360 * and ifa_best is also non-deprecated.
3361 * 2. we are looking at a deprecated address,
3362 * and ifa_best is also deprecated.
3363 * Also, we do not have to consider a case where
3364 * the scope of if_best is larger(smaller) than dst and
3365 * the scope of the current address is smaller(larger)
3366 * than dst. Such a case has already been covered.
3367 * Tiebreaking is done according to the following
3369 * - the scope comparison between the address and
3371 * - the scope comparison between the address and
3372 * ifa_best (bscopecmp)
3373 * - if the address match dst longer than ifa_best
3375 * - if the address is on the outgoing I/F (outI/F)
3377 * Roughly speaking, the selection policy is
3378 * - the most important item is scope. The same scope
3379 * is best. Then search for a larger scope.
3380 * Smaller scopes are the last resort.
3381 * - A deprecated address is chosen only when we have
3382 * no address that has an enough scope, but is
3383 * prefered to any addresses of smaller scopes
3384 * (this must be already done above.)
3385 * - addresses on the outgoing I/F are preferred to
3386 * ones on other interfaces if none of above
3387 * tiebreaks. In the table below, the column "bI"
3388 * means if the best_ifa is on the outgoing
3389 * interface, and the column "sI" means if the ifa
3390 * is on the outgoing interface.
3391 * - If there is no other reasons to choose one,
3392 * longest address match against dst is considered.
3394 * The precise decision table is as follows:
3395 * dscopecmp bscopecmp match bI oI | replace?
3396 * N/A equal N/A Y N | No (1)
3397 * N/A equal N/A N Y | Yes (2)
3398 * N/A equal larger N/A | Yes (3)
3399 * N/A equal !larger N/A | No (4)
3400 * larger larger N/A N/A | No (5)
3401 * larger smaller N/A N/A | Yes (6)
3402 * smaller larger N/A N/A | Yes (7)
3403 * smaller smaller N/A N/A | No (8)
3404 * equal smaller N/A N/A | Yes (9)
3405 * equal larger (already done at A above)
3407 dscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
);
3408 bscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, best_scope
);
3410 if (bscopecmp
== 0) {
3411 struct ifnet
*bifp
= ifa_best
->ia_ifp
;
3413 if (bifp
== oifp
&& ifp
!= oifp
) { /* (1) */
3417 if (bifp
!= oifp
&& ifp
== oifp
) { /* (2) */
3422 * Both bifp and ifp are on the outgoing
3423 * interface, or both two are on a different
3424 * interface from the outgoing I/F.
3425 * now we need address matching against dst
3428 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3429 matchcmp
= tlen
- blen
;
3430 if (matchcmp
> 0) { /* (3) */
3436 if (dscopecmp
> 0) {
3437 if (bscopecmp
> 0) { /* (5) */
3441 goto replace
; /* (6) */
3443 if (dscopecmp
< 0) {
3444 if (bscopecmp
> 0) { /* (7) */
3451 /* now dscopecmp must be 0 */
3452 if (bscopecmp
< 0) {
3453 goto replace
; /* (9) */
3456 IFA_ADDREF_LOCKED(ifa
); /* for ifa_best */
3457 blen
= tlen
>= 0 ? tlen
:
3458 in6_matchlen(IFA_IN6(ifa
), dst
);
3460 in6_addrscope(&ifa2ia6(ifa
)->ia_addr
.sin6_addr
);
3463 IFA_REMREF(&ifa_best
->ia_ifa
);
3465 ifa_best
= (struct in6_ifaddr
*)ifa
;
3467 ifnet_lock_done(ifp
);
3471 /* count statistics for future improvements */
3472 if (ifa_best
== NULL
) {
3473 ip6stat
.ip6s_sources_none
++;
3475 IFA_LOCK_SPIN(&ifa_best
->ia_ifa
);
3476 if (oifp
== ifa_best
->ia_ifp
) {
3477 ip6stat
.ip6s_sources_sameif
[best_scope
]++;
3479 ip6stat
.ip6s_sources_otherif
[best_scope
]++;
3482 if (best_scope
== dst_scope
) {
3483 ip6stat
.ip6s_sources_samescope
[best_scope
]++;
3485 ip6stat
.ip6s_sources_otherscope
[best_scope
]++;
3488 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0) {
3489 ip6stat
.ip6s_sources_deprecated
[best_scope
]++;
3491 IFA_UNLOCK(&ifa_best
->ia_ifa
);
3498 * return the best address out of the same scope. if no address was
3499 * found, return the first valid address from designated IF.
3502 in6_ifawithifp(struct ifnet
*ifp
, struct in6_addr
*dst
)
3504 int dst_scope
= in6_addrscope(dst
), blen
= -1, tlen
;
3506 struct in6_ifaddr
*besta
= NULL
;
3507 struct in6_ifaddr
*dep
[2]; /* last-resort: deprecated */
3509 dep
[0] = dep
[1] = NULL
;
3512 * We first look for addresses in the same scope.
3513 * If there is one, return it.
3514 * If two or more, return one which matches the dst longest.
3515 * If none, return one of global addresses assigned other ifs.
3517 ifnet_lock_shared(ifp
);
3518 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3520 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3524 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3526 continue; /* XXX: is there any case to allow anycast? */
3528 if (ifa2ia6(ifa
)->ia6_flags
& (IN6_IFF_NOTREADY
| IN6_IFF_CLAT46
)) {
3530 continue; /* don't use this interface */
3532 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3536 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3537 if (ip6_use_deprecated
) {
3538 IFA_ADDREF_LOCKED(ifa
); /* for dep[0] */
3540 if (dep
[0] != NULL
) {
3541 IFA_REMREF(&dep
[0]->ia_ifa
);
3543 dep
[0] = (struct in6_ifaddr
*)ifa
;
3550 if (dst_scope
== in6_addrscope(IFA_IN6(ifa
))) {
3552 * call in6_matchlen() as few as possible
3557 IFA_LOCK(&besta
->ia_ifa
);
3558 blen
= in6_matchlen(
3559 &besta
->ia_addr
.sin6_addr
, dst
);
3560 IFA_UNLOCK(&besta
->ia_ifa
);
3563 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3566 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3568 IFA_REMREF(&besta
->ia_ifa
);
3569 besta
= (struct in6_ifaddr
*)ifa
;
3574 besta
= (struct in6_ifaddr
*)ifa
;
3575 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3583 ifnet_lock_done(ifp
);
3584 if (dep
[0] != NULL
) {
3585 IFA_REMREF(&dep
[0]->ia_ifa
);
3590 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3592 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3596 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3598 continue; /* XXX: is there any case to allow anycast? */
3600 if (ifa2ia6(ifa
)->ia6_flags
& (IN6_IFF_NOTREADY
| IN6_IFF_CLAT46
)) {
3602 continue; /* don't use this interface */
3604 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3608 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3609 if (ip6_use_deprecated
) {
3610 IFA_ADDREF_LOCKED(ifa
); /* for dep[1] */
3612 if (dep
[1] != NULL
) {
3613 IFA_REMREF(&dep
[1]->ia_ifa
);
3615 dep
[1] = (struct in6_ifaddr
*)ifa
;
3621 IFA_ADDREF_LOCKED(ifa
); /* for caller */
3623 ifnet_lock_done(ifp
);
3624 if (dep
[0] != NULL
) {
3625 IFA_REMREF(&dep
[0]->ia_ifa
);
3627 if (dep
[1] != NULL
) {
3628 IFA_REMREF(&dep
[1]->ia_ifa
);
3630 return (struct in6_ifaddr
*)ifa
;
3632 ifnet_lock_done(ifp
);
3634 /* use the last-resort values, that are, deprecated addresses */
3636 if (dep
[1] != NULL
) {
3637 IFA_REMREF(&dep
[1]->ia_ifa
);
3649 * perform DAD when interface becomes IFF_UP.
3652 in6_if_up_dad_start(struct ifnet
*ifp
)
3655 struct nd_ifinfo
*ndi
= NULL
;
3657 ndi
= ND_IFINFO(ifp
);
3658 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3659 if (!(ndi
->flags
& ND6_IFF_DAD
)) {
3663 /* start DAD on all the interface addresses */
3664 ifnet_lock_exclusive(ifp
);
3665 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3666 struct in6_ifaddr
*ia6
;
3669 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3673 ia6
= (struct in6_ifaddr
*)ifa
;
3674 if (ia6
->ia6_flags
& IN6_IFF_DADPROGRESS
) {
3675 int delay
= 0; /* delay ticks before DAD output */
3677 nd6_dad_start(ifa
, &delay
);
3682 ifnet_lock_done(ifp
);
3689 struct nd_ifinfo
*ndi
= NULL
;
3691 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
3695 ndi
= ND_IFINFO(ifp
);
3696 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3697 if (!(ndi
->flags
& ND6_IFF_DAD
)) {
3702 * If we are using the alternative neighbor discovery
3703 * interface on this interface, then skip DAD.
3705 * Also, skip it for interfaces marked "local private"
3706 * for now, even when not marked as using the alternative
3707 * interface. This is for historical reasons.
3709 if (ifp
->if_eflags
&
3710 (IFEF_IPV6_ND6ALT
| IFEF_LOCALNET_PRIVATE
| IFEF_DIRECTLINK
)) {
3714 if (ifp
->if_subfamily
== IFNET_SUBFAMILY_IPSEC
||
3715 ifp
->if_subfamily
== IFNET_SUBFAMILY_UTUN
) {
3717 * Ignore DAD for tunneling virtual interfaces, which get
3718 * their IPv6 address explicitly assigned.
3723 switch (ifp
->if_type
) {
3729 * These interfaces do not have the IFF_LOOPBACK flag,
3730 * but loop packets back. We do not have to do DAD on such
3731 * interfaces. We should even omit it, because loop-backed
3732 * NS would confuse the DAD procedure.
3737 * Our DAD routine requires the interface up and running.
3738 * However, some interfaces can be up before the RUNNING
3739 * status. Additionaly, users may try to assign addresses
3740 * before the interface becomes up (or running).
3741 * We simply skip DAD in such a case as a work around.
3742 * XXX: we should rather mark "tentative" on such addresses,
3743 * and do DAD after the interface becomes ready.
3745 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) !=
3746 (IFF_UP
| IFF_RUNNING
)) {
3755 * Calculate max IPv6 MTU through all the interfaces and store it
3761 u_int32_t maxmtu
= 0;
3764 ifnet_head_lock_shared();
3765 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
3766 struct nd_ifinfo
*ndi
= NULL
;
3768 if ((ndi
= ND_IFINFO(ifp
)) != NULL
&& !ndi
->initialized
) {
3772 lck_mtx_lock(&ndi
->lock
);
3774 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
3775 IN6_LINKMTU(ifp
) > maxmtu
) {
3776 maxmtu
= IN6_LINKMTU(ifp
);
3779 lck_mtx_unlock(&ndi
->lock
);
3783 if (maxmtu
) { /* update only when maxmtu is positive */
3784 in6_maxmtu
= maxmtu
;
3788 * Provide the length of interface identifiers to be used for the link attached
3789 * to the given interface. The length should be defined in "IPv6 over
3790 * xxx-link" document. Note that address architecture might also define
3791 * the length for a particular set of address prefixes, regardless of the
3792 * link type. Also see RFC 4862 for additional background.
3795 in6_if2idlen(struct ifnet
*ifp
)
3797 switch (ifp
->if_type
) {
3798 case IFT_ETHER
: /* RFC2464 */
3799 case IFT_IEEE8023ADLAG
: /* IEEE802.3ad Link Aggregate */
3800 #ifdef IFT_PROPVIRTUAL
3801 case IFT_PROPVIRTUAL
: /* XXX: no RFC. treat it as ether */
3804 case IFT_L2VLAN
: /* ditto */
3806 #ifdef IFT_IEEE80211
3807 case IFT_IEEE80211
: /* ditto */
3810 case IFT_MIP
: /* ditto */
3813 case IFT_FDDI
: /* RFC2467 */
3815 case IFT_ISO88025
: /* RFC2470 (IPv6 over Token Ring) */
3817 case IFT_PPP
: /* RFC2472 */
3819 case IFT_ARCNET
: /* RFC2497 */
3821 case IFT_FRELAY
: /* RFC2590 */
3823 case IFT_IEEE1394
: /* RFC3146 */
3826 return 64; /* draft-ietf-v6ops-mech-v2-07 */
3828 return 64; /* XXX: is this really correct? */
3830 return 64; /* for utun interfaces */
3832 return 64; /* Packet Data over Cellular */
3834 return 64; /* Transparent bridge interface */
3837 * Unknown link type:
3838 * It might be controversial to use the today's common constant
3839 * of 64 for these cases unconditionally. For full compliance,
3840 * we should return an error in this case. On the other hand,
3841 * if we simply miss the standard for the link type or a new
3842 * standard is defined for a new link type, the IFID length
3843 * is very likely to be the common constant. As a compromise,
3844 * we always use the constant, but make an explicit notice
3845 * indicating the "unknown" case.
3847 log(LOG_NOTICE
, "%s: unknown link type (%d)\n", __func__
,
3853 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
3854 * v4 mapped addr or v4 compat addr
3857 in6_sin6_2_sin(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3859 bzero(sin
, sizeof(*sin
));
3860 sin
->sin_len
= sizeof(struct sockaddr_in
);
3861 sin
->sin_family
= AF_INET
;
3862 sin
->sin_port
= sin6
->sin6_port
;
3863 sin
->sin_addr
.s_addr
= sin6
->sin6_addr
.s6_addr32
[3];
3866 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
3868 in6_sin_2_v4mapsin6(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3870 bzero(sin6
, sizeof(*sin6
));
3871 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
3872 sin6
->sin6_family
= AF_INET6
;
3873 sin6
->sin6_port
= sin
->sin_port
;
3874 sin6
->sin6_addr
.s6_addr32
[0] = 0;
3875 sin6
->sin6_addr
.s6_addr32
[1] = 0;
3876 if (sin
->sin_addr
.s_addr
) {
3877 sin6
->sin6_addr
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
3878 sin6
->sin6_addr
.s6_addr32
[3] = sin
->sin_addr
.s_addr
;
3880 sin6
->sin6_addr
.s6_addr32
[2] = 0;
3881 sin6
->sin6_addr
.s6_addr32
[3] = 0;
3885 /* Convert sockaddr_in6 into sockaddr_in. */
3887 in6_sin6_2_sin_in_sock(struct sockaddr
*nam
)
3889 struct sockaddr_in
*sin_p
;
3890 struct sockaddr_in6 sin6
;
3893 * Save original sockaddr_in6 addr and convert it
3896 sin6
= *(struct sockaddr_in6
*)(void *)nam
;
3897 sin_p
= (struct sockaddr_in
*)(void *)nam
;
3898 in6_sin6_2_sin(sin_p
, &sin6
);
3901 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
3903 in6_sin_2_v4mapsin6_in_sock(struct sockaddr
**nam
)
3905 struct sockaddr_in
*sin_p
;
3906 struct sockaddr_in6
*sin6_p
;
3908 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof(*sin6_p
), M_SONAME
,
3910 if (sin6_p
== NULL
) {
3913 sin_p
= (struct sockaddr_in
*)(void *)*nam
;
3914 in6_sin_2_v4mapsin6(sin_p
, sin6_p
);
3915 FREE(*nam
, M_SONAME
);
3916 *nam
= (struct sockaddr
*)sin6_p
;
3922 * Posts in6_event_data message kernel events.
3924 * To get the same size of kev_in6_data between ILP32 and LP64 data models
3925 * we are using a special version of the in6_addrlifetime structure that
3926 * uses only 32 bits fields to be compatible with Leopard, and that
3927 * are large enough to span 68 years.
3930 in6_post_msg(struct ifnet
*ifp
, u_int32_t event_code
, struct in6_ifaddr
*ifa
,
3933 struct kev_msg ev_msg
;
3934 struct kev_in6_data in6_event_data
;
3935 struct in6_addrlifetime ia6_lt
;
3937 bzero(&in6_event_data
, sizeof(struct kev_in6_data
));
3938 bzero(&ev_msg
, sizeof(struct kev_msg
));
3939 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3940 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3941 ev_msg
.kev_subclass
= KEV_INET6_SUBCLASS
;
3942 ev_msg
.event_code
= event_code
;
3945 IFA_LOCK(&ifa
->ia_ifa
);
3946 in6_event_data
.ia_addr
= ifa
->ia_addr
;
3947 in6_event_data
.ia_net
= ifa
->ia_net
;
3948 in6_event_data
.ia_dstaddr
= ifa
->ia_dstaddr
;
3949 in6_event_data
.ia_prefixmask
= ifa
->ia_prefixmask
;
3950 in6_event_data
.ia_plen
= ifa
->ia_plen
;
3951 in6_event_data
.ia6_flags
= (u_int32_t
)ifa
->ia6_flags
;
3953 /* retrieve time as calendar time (last arg is 1) */
3954 in6ifa_getlifetime(ifa
, &ia6_lt
, 1);
3955 in6_event_data
.ia_lifetime
.ia6t_expire
= ia6_lt
.ia6t_expire
;
3956 in6_event_data
.ia_lifetime
.ia6t_preferred
= ia6_lt
.ia6t_preferred
;
3957 in6_event_data
.ia_lifetime
.ia6t_vltime
= ia6_lt
.ia6t_vltime
;
3958 in6_event_data
.ia_lifetime
.ia6t_pltime
= ia6_lt
.ia6t_pltime
;
3959 IFA_UNLOCK(&ifa
->ia_ifa
);
3963 (void) strlcpy(&in6_event_data
.link_data
.if_name
[0],
3964 ifp
->if_name
, IFNAMSIZ
);
3965 in6_event_data
.link_data
.if_family
= ifp
->if_family
;
3966 in6_event_data
.link_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
3970 memcpy(&in6_event_data
.ia_mac
, mac
,
3971 sizeof(in6_event_data
.ia_mac
));
3974 ev_msg
.dv
[0].data_ptr
= &in6_event_data
;
3975 ev_msg
.dv
[0].data_length
= sizeof(in6_event_data
);
3976 ev_msg
.dv
[1].data_length
= 0;
3978 dlil_post_complete_msg(NULL
, &ev_msg
);
3982 * Called as part of ip6_init
3985 in6_ifaddr_init(void)
3990 PE_parse_boot_argn("ifa_debug", &in6ifa_debug
, sizeof(in6ifa_debug
));
3992 in6ifa_size
= (in6ifa_debug
== 0) ? sizeof(struct in6_ifaddr
) :
3993 sizeof(struct in6_ifaddr_dbg
);
3995 in6ifa_zone
= zinit(in6ifa_size
, IN6IFA_ZONE_MAX
* in6ifa_size
,
3996 0, IN6IFA_ZONE_NAME
);
3997 if (in6ifa_zone
== NULL
) {
3998 panic("%s: failed allocating %s", __func__
, IN6IFA_ZONE_NAME
);
4001 zone_change(in6ifa_zone
, Z_EXPAND
, TRUE
);
4002 zone_change(in6ifa_zone
, Z_CALLERACCT
, FALSE
);
4004 lck_mtx_init(&in6ifa_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
4005 TAILQ_INIT(&in6ifa_trash_head
);
4008 static struct in6_ifaddr
*
4009 in6_ifaddr_alloc(int how
)
4011 struct in6_ifaddr
*in6ifa
;
4013 in6ifa
= (how
== M_WAITOK
) ? zalloc(in6ifa_zone
) :
4014 zalloc_noblock(in6ifa_zone
);
4015 if (in6ifa
!= NULL
) {
4016 bzero(in6ifa
, in6ifa_size
);
4017 in6ifa
->ia_ifa
.ifa_free
= in6_ifaddr_free
;
4018 in6ifa
->ia_ifa
.ifa_debug
|= IFD_ALLOC
;
4019 ifa_lock_init(&in6ifa
->ia_ifa
);
4020 if (in6ifa_debug
!= 0) {
4021 struct in6_ifaddr_dbg
*in6ifa_dbg
=
4022 (struct in6_ifaddr_dbg
*)in6ifa
;
4023 in6ifa
->ia_ifa
.ifa_debug
|= IFD_DEBUG
;
4024 in6ifa
->ia_ifa
.ifa_trace
= in6_ifaddr_trace
;
4025 in6ifa
->ia_ifa
.ifa_attached
= in6_ifaddr_attached
;
4026 in6ifa
->ia_ifa
.ifa_detached
= in6_ifaddr_detached
;
4027 ctrace_record(&in6ifa_dbg
->in6ifa_alloc
);
4035 in6_ifaddr_free(struct ifaddr
*ifa
)
4037 IFA_LOCK_ASSERT_HELD(ifa
);
4039 if (ifa
->ifa_refcnt
!= 0) {
4040 panic("%s: ifa %p bad ref cnt", __func__
, ifa
);
4042 } else if (!(ifa
->ifa_debug
& IFD_ALLOC
)) {
4043 panic("%s: ifa %p cannot be freed", __func__
, ifa
);
4046 if (ifa
->ifa_debug
& IFD_DEBUG
) {
4047 struct in6_ifaddr_dbg
*in6ifa_dbg
=
4048 (struct in6_ifaddr_dbg
*)ifa
;
4049 ctrace_record(&in6ifa_dbg
->in6ifa_free
);
4050 bcopy(&in6ifa_dbg
->in6ifa
, &in6ifa_dbg
->in6ifa_old
,
4051 sizeof(struct in6_ifaddr
));
4052 if (ifa
->ifa_debug
& IFD_TRASHED
) {
4053 /* Become a regular mutex, just in case */
4054 IFA_CONVERT_LOCK(ifa
);
4055 lck_mtx_lock(&in6ifa_trash_lock
);
4056 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
,
4058 lck_mtx_unlock(&in6ifa_trash_lock
);
4059 ifa
->ifa_debug
&= ~IFD_TRASHED
;
4063 ifa_lock_destroy(ifa
);
4064 bzero(ifa
, sizeof(struct in6_ifaddr
));
4065 zfree(in6ifa_zone
, ifa
);
4069 in6_ifaddr_attached(struct ifaddr
*ifa
)
4071 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
4073 IFA_LOCK_ASSERT_HELD(ifa
);
4075 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
4076 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
4079 if (ifa
->ifa_debug
& IFD_TRASHED
) {
4080 /* Become a regular mutex, just in case */
4081 IFA_CONVERT_LOCK(ifa
);
4082 lck_mtx_lock(&in6ifa_trash_lock
);
4083 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
4084 lck_mtx_unlock(&in6ifa_trash_lock
);
4085 ifa
->ifa_debug
&= ~IFD_TRASHED
;
4090 in6_ifaddr_detached(struct ifaddr
*ifa
)
4092 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
4094 IFA_LOCK_ASSERT_HELD(ifa
);
4096 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
4097 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
4099 } else if (ifa
->ifa_debug
& IFD_TRASHED
) {
4100 panic("%s: ifa %p is already in trash list", __func__
, ifa
);
4103 ifa
->ifa_debug
|= IFD_TRASHED
;
4104 /* Become a regular mutex, just in case */
4105 IFA_CONVERT_LOCK(ifa
);
4106 lck_mtx_lock(&in6ifa_trash_lock
);
4107 TAILQ_INSERT_TAIL(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
4108 lck_mtx_unlock(&in6ifa_trash_lock
);
4112 in6_ifaddr_trace(struct ifaddr
*ifa
, int refhold
)
4114 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
4119 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
4120 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
4124 cnt
= &in6ifa_dbg
->in6ifa_refhold_cnt
;
4125 tr
= in6ifa_dbg
->in6ifa_refhold
;
4127 cnt
= &in6ifa_dbg
->in6ifa_refrele_cnt
;
4128 tr
= in6ifa_dbg
->in6ifa_refrele
;
4131 idx
= atomic_add_16_ov(cnt
, 1) % IN6IFA_TRACE_HIST_SIZE
;
4132 ctrace_record(&tr
[idx
]);
4136 * Handle SIOCGASSOCIDS ioctl for PF_INET6 domain.
4139 in6_getassocids(struct socket
*so
, uint32_t *cnt
, user_addr_t aidp
)
4141 struct in6pcb
*in6p
= sotoin6pcb(so
);
4144 if (in6p
== NULL
|| in6p
->inp_state
== INPCB_STATE_DEAD
) {
4148 /* IN6PCB has no concept of association */
4149 aid
= SAE_ASSOCID_ANY
;
4152 /* just asking how many there are? */
4153 if (aidp
== USER_ADDR_NULL
) {
4157 return copyout(&aid
, aidp
, sizeof(aid
));
4161 * Handle SIOCGCONNIDS ioctl for PF_INET6 domain.
4164 in6_getconnids(struct socket
*so
, sae_associd_t aid
, uint32_t *cnt
,
4167 struct in6pcb
*in6p
= sotoin6pcb(so
);
4170 if (in6p
== NULL
|| in6p
->inp_state
== INPCB_STATE_DEAD
) {
4174 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
) {
4178 /* if connected, return 1 connection count */
4179 *cnt
= ((so
->so_state
& SS_ISCONNECTED
) ? 1 : 0);
4181 /* just asking how many there are? */
4182 if (cidp
== USER_ADDR_NULL
) {
4186 /* if IN6PCB is connected, assign it connid 1 */
4187 cid
= ((*cnt
!= 0) ? 1 : SAE_CONNID_ANY
);
4189 return copyout(&cid
, cidp
, sizeof(cid
));
4193 * Handle SIOCGCONNINFO ioctl for PF_INET6 domain.
4196 in6_getconninfo(struct socket
*so
, sae_connid_t cid
, uint32_t *flags
,
4197 uint32_t *ifindex
, int32_t *soerror
, user_addr_t src
, socklen_t
*src_len
,
4198 user_addr_t dst
, socklen_t
*dst_len
, uint32_t *aux_type
,
4199 user_addr_t aux_data
, uint32_t *aux_len
)
4201 struct in6pcb
*in6p
= sotoin6pcb(so
);
4202 struct sockaddr_in6 sin6
;
4203 struct ifnet
*ifp
= NULL
;
4205 u_int32_t copy_len
= 0;
4208 * Don't test for INPCB_STATE_DEAD since this may be called
4209 * after SOF_PCBCLEARING is set, e.g. after tcp_close().
4216 if (cid
!= SAE_CONNID_ANY
&& cid
!= SAE_CONNID_ALL
&& cid
!= 1) {
4221 ifp
= in6p
->in6p_last_outifp
;
4222 *ifindex
= ((ifp
!= NULL
) ? ifp
->if_index
: 0);
4223 *soerror
= so
->so_error
;
4225 if (so
->so_state
& SS_ISCONNECTED
) {
4226 *flags
|= (CIF_CONNECTED
| CIF_PREFERRED
);
4228 if (in6p
->in6p_flags
& INP_BOUND_IF
) {
4229 *flags
|= CIF_BOUND_IF
;
4231 if (!(in6p
->in6p_flags
& INP_IN6ADDR_ANY
)) {
4232 *flags
|= CIF_BOUND_IP
;
4234 if (!(in6p
->in6p_flags
& INP_ANONPORT
)) {
4235 *flags
|= CIF_BOUND_PORT
;
4238 bzero(&sin6
, sizeof(sin6
));
4239 sin6
.sin6_len
= sizeof(sin6
);
4240 sin6
.sin6_family
= AF_INET6
;
4242 /* source address and port */
4243 sin6
.sin6_port
= in6p
->in6p_lport
;
4244 in6_recoverscope(&sin6
, &in6p
->in6p_laddr
, NULL
);
4245 if (*src_len
== 0) {
4246 *src_len
= sin6
.sin6_len
;
4248 if (src
!= USER_ADDR_NULL
) {
4249 copy_len
= min(*src_len
, sizeof(sin6
));
4250 error
= copyout(&sin6
, src
, copy_len
);
4254 *src_len
= copy_len
;
4258 /* destination address and port */
4259 sin6
.sin6_port
= in6p
->in6p_fport
;
4260 in6_recoverscope(&sin6
, &in6p
->in6p_faddr
, NULL
);
4261 if (*dst_len
== 0) {
4262 *dst_len
= sin6
.sin6_len
;
4264 if (dst
!= USER_ADDR_NULL
) {
4265 copy_len
= min(*dst_len
, sizeof(sin6
));
4266 error
= copyout(&sin6
, dst
, copy_len
);
4270 *dst_len
= copy_len
;
4274 if (SOCK_PROTO(so
) == IPPROTO_TCP
) {
4275 struct conninfo_tcp tcp_ci
;
4277 *aux_type
= CIAUX_TCP
;
4278 if (*aux_len
== 0) {
4279 *aux_len
= sizeof(tcp_ci
);
4281 if (aux_data
!= USER_ADDR_NULL
) {
4282 copy_len
= min(*aux_len
, sizeof(tcp_ci
));
4283 bzero(&tcp_ci
, sizeof(tcp_ci
));
4284 tcp_getconninfo(so
, &tcp_ci
);
4285 error
= copyout(&tcp_ci
, aux_data
, copy_len
);
4289 *aux_len
= copy_len
;
4304 * The switch statement below does nothing at runtime, as it serves as a
4305 * compile time check to ensure that all of the socket 'u' ioctls (those
4306 * in the 'u' group going thru soo_ioctl) that are made available by the
4307 * networking stack is unique. This works as long as this routine gets
4308 * updated each time a new interface ioctl gets added.
4310 * Any failures at compile time indicates duplicated ioctl values.
4312 static __attribute__((unused
)) void
4313 in6ioctl_cassert(void)
4316 * This is equivalent to _CASSERT() and the compiler wouldn't
4317 * generate any instructions, thus for compile time only.
4319 switch ((u_long
)0) {
4322 /* bsd/netinet6/in6_var.h */
4323 case SIOCAADDRCTL_POLICY
:
4324 case SIOCDADDRCTL_POLICY
:
4325 case SIOCDRADD_IN6_32
:
4326 case SIOCDRADD_IN6_64
:
4327 case SIOCDRDEL_IN6_32
:
4328 case SIOCDRDEL_IN6_64
:
4333 struct in6_llentry
{
4334 struct llentry base
;
4337 #define IN6_LLTBL_DEFAULT_HSIZE 32
4338 #define IN6_LLTBL_HASH(k, h) \
4339 ((((((((k) >> 8) ^ (k)) >> 8) ^ (k)) >> 8) ^ (k)) & ((h) - 1))
4342 * Do actual deallocation of @lle.
4345 in6_lltable_destroy_lle_unlocked(struct llentry
*lle
)
4347 LLE_LOCK_DESTROY(lle
);
4348 LLE_REQ_DESTROY(lle
);
4349 FREE(lle
, M_LLTABLE
);
4353 * Called by LLE_FREE_LOCKED when number of references
4357 in6_lltable_destroy_lle(struct llentry
*lle
)
4361 //thread_call_free(lle->lle_timer);
4362 in6_lltable_destroy_lle_unlocked(lle
);
4366 static struct llentry
*
4367 in6_lltable_new(const struct in6_addr
*addr6
, u_int flags
)
4369 #pragma unused(flags)
4370 struct in6_llentry
*lle
;
4372 MALLOC(lle
, struct in6_llentry
*, sizeof(struct in6_llentry
), M_LLTABLE
, M_NOWAIT
| M_ZERO
);
4373 if (lle
== NULL
) { /* NB: caller generates msg */
4377 lle
->base
.r_l3addr
.addr6
= *addr6
;
4378 lle
->base
.lle_refcnt
= 1;
4379 lle
->base
.lle_free
= in6_lltable_destroy_lle
;
4380 LLE_LOCK_INIT(&lle
->base
);
4381 LLE_REQ_INIT(&lle
->base
);
4384 lle
->base
.lle_timer
= thread_call_allocate(nd6_llinfo_timer
, lle
);
4386 if (lle
->base
.lle_timer
== NULL
) {
4387 printf("lle_timer thread call could not be allocated.\n");
4388 LLE_LOCK_DESTROY(&lle
->base
);
4389 LLE_REQ_DESTROY(&lle
->base
);
4390 FREE(lle
, M_LLTABLE
);
4398 in6_lltable_match_prefix(const struct sockaddr
*saddr
,
4399 const struct sockaddr
*smask
, u_int flags
, struct llentry
*lle
)
4401 const struct in6_addr
*addr
, *mask
, *lle_addr
;
4403 addr
= &((const struct sockaddr_in6
*)(const void *)saddr
)->sin6_addr
;
4404 mask
= &((const struct sockaddr_in6
*)(const void *)smask
)->sin6_addr
;
4405 lle_addr
= &lle
->r_l3addr
.addr6
;
4407 if (IN6_ARE_MASKED_ADDR_EQUAL(lle_addr
, addr
, mask
) == 0) {
4411 if (lle
->la_flags
& LLE_IFADDR
) {
4413 * Delete LLE_IFADDR records IFF address & flag matches.
4414 * Note that addr is the interface address within prefix
4417 if (IN6_ARE_ADDR_EQUAL(addr
, lle_addr
) &&
4418 (flags
& LLE_STATIC
) != 0) {
4424 /* flags & LLE_STATIC means deleting both dynamic and static entries */
4425 if ((flags
& LLE_STATIC
) || !(lle
->la_flags
& LLE_STATIC
)) {
4433 in6_lltable_free_entry(struct lltable
*llt
, struct llentry
*lle
)
4437 LLE_WLOCK_ASSERT(lle
);
4438 KASSERT(llt
!= NULL
, ("lltable is NULL"));
4440 /* Unlink entry from table */
4441 if ((lle
->la_flags
& LLE_LINKED
) != 0) {
4443 if_afdata_wlock_assert(ifp
, llt
->llt_af
);
4444 lltable_unlink_entry(llt
, lle
);
4449 if (thread_call_cancel(lle
->lle_timer
) == TRUE
) {
4457 in6_lltable_rtcheck(struct ifnet
*ifp
,
4458 u_int flags
, const struct sockaddr
*l3addr
)
4460 #pragma unused(flags)
4463 KASSERT(l3addr
->sa_family
== AF_INET6
,
4464 ("sin_family %d", l3addr
->sa_family
));
4465 /* XXX rtalloc1 should take a const param */
4466 rt
= rtalloc1(__DECONST(struct sockaddr
*, l3addr
), 0, 0);
4467 if (rt
== NULL
|| (rt
->rt_flags
& RTF_GATEWAY
) || rt
->rt_ifp
!= ifp
) {
4470 * Create an ND6 cache for an IPv6 neighbor
4471 * that is not covered by our own prefix.
4473 /* XXX ifaof_ifpforaddr should take a const param */
4474 ifa
= ifaof_ifpforaddr(__DECONST(struct sockaddr
*, l3addr
), ifp
);
4482 log(LOG_INFO
, "IPv6 address: \"%s\" is not on the network\n",
4483 ip6_sprintf(&((const struct sockaddr_in6
*)(const void *)l3addr
)->sin6_addr
));
4493 static inline uint32_t
4494 in6_lltable_hash_dst(const struct in6_addr
*dst
, uint32_t hsize
)
4496 return IN6_LLTBL_HASH(dst
->s6_addr32
[3], hsize
);
4500 in6_lltable_hash(const struct llentry
*lle
, uint32_t hsize
)
4502 return in6_lltable_hash_dst(&lle
->r_l3addr
.addr6
, hsize
);
4506 in6_lltable_fill_sa_entry(const struct llentry
*lle
, struct sockaddr
*sa
)
4508 struct sockaddr_in6
*sin6
;
4510 sin6
= (struct sockaddr_in6
*)(void *)sa
;
4511 bzero(sin6
, sizeof(*sin6
));
4512 sin6
->sin6_family
= AF_INET6
;
4513 sin6
->sin6_len
= sizeof(*sin6
);
4514 sin6
->sin6_addr
= lle
->r_l3addr
.addr6
;
4517 static inline struct llentry
*
4518 in6_lltable_find_dst(struct lltable
*llt
, const struct in6_addr
*dst
)
4520 struct llentry
*lle
;
4521 struct llentries
*lleh
;
4524 hashidx
= in6_lltable_hash_dst(dst
, llt
->llt_hsize
);
4525 lleh
= &llt
->lle_head
[hashidx
];
4526 LIST_FOREACH(lle
, lleh
, lle_next
) {
4527 if (lle
->la_flags
& LLE_DELETED
) {
4530 if (IN6_ARE_ADDR_EQUAL(&lle
->r_l3addr
.addr6
, dst
)) {
4539 in6_lltable_delete_entry(struct lltable
*llt
, struct llentry
*lle
)
4542 lle
->la_flags
|= LLE_DELETED
;
4543 EVENTHANDLER_INVOKE(NULL
, lle_event
, lle
, LLENTRY_DELETED
);
4545 log(LOG_INFO
, "ifaddr cache = %p is deleted\n", lle
);
4550 static struct llentry
*
4551 in6_lltable_alloc(struct lltable
*llt
, u_int flags
,
4552 const struct sockaddr
*l3addr
)
4554 const struct sockaddr_in6
*sin6
= (const struct sockaddr_in6
*)(const void *)l3addr
;
4555 struct ifnet
*ifp
= llt
->llt_ifp
;
4556 struct llentry
*lle
;
4558 KASSERT(l3addr
->sa_family
== AF_INET6
,
4559 ("sin_family %d", l3addr
->sa_family
));
4562 * A route that covers the given address must have
4563 * been installed 1st because we are doing a resolution,
4566 if (!(flags
& LLE_IFADDR
) &&
4567 in6_lltable_rtcheck(ifp
, flags
, l3addr
) != 0) {
4571 lle
= in6_lltable_new(&sin6
->sin6_addr
, flags
);
4573 log(LOG_INFO
, "lla_lookup: new lle malloc failed\n");
4576 lle
->la_flags
= flags
;
4577 if ((flags
& LLE_IFADDR
) == LLE_IFADDR
) {
4578 lltable_set_entry_addr(ifp
, lle
, LLADDR(SDL(ifp
->if_lladdr
->ifa_addr
)));
4579 lle
->la_flags
|= LLE_STATIC
;
4582 if ((lle
->la_flags
& LLE_STATIC
) != 0) {
4583 lle
->ln_state
= ND6_LLINFO_REACHABLE
;
4589 static struct llentry
*
4590 in6_lltable_lookup(struct lltable
*llt
, u_int flags
,
4591 const struct sockaddr
*l3addr
)
4593 const struct sockaddr_in6
*sin6
= (const struct sockaddr_in6
*)(const void *)l3addr
;
4594 struct llentry
*lle
;
4596 IF_AFDATA_LOCK_ASSERT(llt
->llt_ifp
, llt
->llt_af
);
4597 KASSERT(l3addr
->sa_family
== AF_INET6
,
4598 ("sin_family %d", l3addr
->sa_family
));
4600 lle
= in6_lltable_find_dst(llt
, &sin6
->sin6_addr
);
4606 KASSERT((flags
& (LLE_UNLOCKED
| LLE_EXCLUSIVE
)) !=
4607 (LLE_UNLOCKED
| LLE_EXCLUSIVE
), ("wrong lle request flags: 0x%X",
4610 if (flags
& LLE_UNLOCKED
) {
4614 if (flags
& LLE_EXCLUSIVE
) {
4623 in6_lltable_dump_entry(struct lltable
*llt
, struct llentry
*lle
,
4624 struct sysctl_req
*wr
)
4626 struct ifnet
*ifp
= llt
->llt_ifp
;
4629 struct rt_msghdr rtm
;
4630 struct sockaddr_in6 sin6
;
4632 * ndp.c assumes that sdl is word aligned
4637 struct sockaddr_dl sdl
;
4639 struct sockaddr_dl
*sdl
;
4642 bzero(&ndpc
, sizeof(ndpc
));
4643 /* skip deleted entries */
4644 if ((lle
->la_flags
& LLE_DELETED
) == LLE_DELETED
) {
4647 /* Skip if jailed and not a valid IP of the prison. */
4648 lltable_fill_sa_entry(lle
,
4649 (struct sockaddr
*)&ndpc
.sin6
);
4651 * produce a msg made of:
4653 * struct sockaddr_in6 (IPv6)
4654 * struct sockaddr_dl;
4656 ndpc
.rtm
.rtm_msglen
= sizeof(ndpc
);
4657 ndpc
.rtm
.rtm_version
= RTM_VERSION
;
4658 ndpc
.rtm
.rtm_type
= RTM_GET
;
4659 ndpc
.rtm
.rtm_flags
= RTF_UP
;
4660 ndpc
.rtm
.rtm_addrs
= RTA_DST
| RTA_GATEWAY
;
4663 if (lle
->la_flags
& LLE_PUB
) {
4664 ndpc
.rtm
.rtm_flags
|= RTF_ANNOUNCE
;
4667 sdl
->sdl_family
= AF_LINK
;
4668 sdl
->sdl_len
= sizeof(*sdl
);
4669 sdl
->sdl_index
= ifp
->if_index
;
4670 sdl
->sdl_type
= ifp
->if_type
;
4671 if ((lle
->la_flags
& LLE_VALID
) == LLE_VALID
) {
4672 sdl
->sdl_alen
= ifp
->if_addrlen
;
4673 bcopy(&lle
->ll_addr
, LLADDR(sdl
), ifp
->if_addrlen
);
4676 bzero(LLADDR(sdl
), ifp
->if_addrlen
);
4678 if (lle
->la_expire
!= 0) {
4682 clock_get_calendar_microtime(&secs
, &usecs
);
4683 ndpc
.rtm
.rtm_rmx
.rmx_expire
= lle
->la_expire
+
4684 lle
->lle_remtime
/ hz
+
4685 secs
- net_uptime();
4687 ndpc
.rtm
.rtm_flags
|= (RTF_HOST
| RTF_LLDATA
);
4688 if (lle
->la_flags
& LLE_STATIC
) {
4689 ndpc
.rtm
.rtm_flags
|= RTF_STATIC
;
4691 if (lle
->la_flags
& LLE_IFADDR
) {
4692 ndpc
.rtm
.rtm_flags
|= RTF_PINNED
;
4694 if (lle
->ln_router
!= 0) {
4695 ndpc
.rtm
.rtm_flags
|= RTF_GATEWAY
;
4697 ndpc
.rtm
.rtm_rmx
.rmx_pksent
= lle
->la_asked
;
4698 /* Store state in rmx_weight value */
4699 ndpc
.rtm
.rtm_rmx
.rmx_state
= lle
->ln_state
;
4700 ndpc
.rtm
.rtm_index
= ifp
->if_index
;
4701 error
= SYSCTL_OUT(wr
, &ndpc
, sizeof(ndpc
));
4707 in6_lltattach(struct ifnet
*ifp
)
4709 struct lltable
*llt
;
4711 llt
= lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE
);
4712 llt
->llt_af
= AF_INET6
;
4715 llt
->llt_lookup
= in6_lltable_lookup
;
4716 llt
->llt_alloc_entry
= in6_lltable_alloc
;
4717 llt
->llt_delete_entry
= in6_lltable_delete_entry
;
4718 llt
->llt_dump_entry
= in6_lltable_dump_entry
;
4719 llt
->llt_hash
= in6_lltable_hash
;
4720 llt
->llt_fill_sa_entry
= in6_lltable_fill_sa_entry
;
4721 llt
->llt_free_entry
= in6_lltable_free_entry
;
4722 llt
->llt_match_prefix
= in6_lltable_match_prefix
;
4729 in6_ip6_to_sockaddr(const struct in6_addr
*ip6
, u_int16_t port
,
4730 struct sockaddr_in6
*sin6
, u_int32_t maxlen
)
4732 if (maxlen
< sizeof(struct sockaddr_in6
)) {
4736 *sin6
= (struct sockaddr_in6
) {
4737 .sin6_family
= AF_INET6
,
4738 .sin6_len
= sizeof(*sin6
),
4743 if (IN6_IS_SCOPE_EMBED(&sin6
->sin6_addr
)) {
4744 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
4745 sin6
->sin6_addr
.s6_addr16
[1] = 0;
4751 in6_evhdlr_code_t in6_event_code
;
4752 struct ifnet
*in6_ifp
;
4753 struct in6_addr in6_address
;
4757 struct in6_event2kev in6_event2kev_array
[IN6_EVENT_MAX
] = {
4759 .in6_event_code
= IN6_ADDR_MARKED_DUPLICATED
,
4760 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4761 .in6_event_kev_code
= KEV_ND6_DAD_FAILURE
,
4762 .in6_event_str
= "IN6_ADDR_MARKED_DUPLICATED",
4765 .in6_event_code
= IN6_ADDR_MARKED_DETACHED
,
4766 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4767 .in6_event_kev_code
= KEV_ND6_ADDR_DETACHED
,
4768 .in6_event_str
= "IN6_ADDR_MARKED_DETACHED",
4771 .in6_event_code
= IN6_ADDR_MARKED_DEPRECATED
,
4772 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4773 .in6_event_kev_code
= KEV_ND6_ADDR_DEPRECATED
,
4774 .in6_event_str
= "IN6_ADDR_MARKED_DEPRECATED",
4777 .in6_event_code
= IN6_NDP_RTR_EXPIRY
,
4778 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4779 .in6_event_kev_code
= KEV_ND6_RTR_EXPIRED
,
4780 .in6_event_str
= "IN6_NDP_RTR_EXPIRY",
4783 .in6_event_code
= IN6_NDP_PFX_EXPIRY
,
4784 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4785 .in6_event_kev_code
= KEV_ND6_PFX_EXPIRED
,
4786 .in6_event_str
= "IN6_NDP_PFX_EXPIRY",
4789 .in6_event_code
= IN6_NDP_ADDR_EXPIRY
,
4790 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4791 .in6_event_kev_code
= KEV_ND6_ADDR_EXPIRED
,
4792 .in6_event_str
= "IN6_NDP_ADDR_EXPIRY",
4797 in6_eventhdlr_callback(struct eventhandler_entry_arg arg0 __unused
,
4798 in6_evhdlr_code_t in6_ev_code
, struct ifnet
*ifp
,
4799 struct in6_addr
*p_addr6
, uint32_t val
)
4801 struct kev_msg ev_msg
;
4802 struct kev_nd6_event nd6_event
;
4804 bzero(&ev_msg
, sizeof(ev_msg
));
4805 bzero(&nd6_event
, sizeof(nd6_event
));
4807 nd6log0((LOG_INFO
, "%s Event %s received for %s\n",
4808 __func__
, in6_event2kev_array
[in6_ev_code
].in6_event_str
,
4809 ip6_sprintf(p_addr6
)));
4811 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
4812 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
4813 ev_msg
.kev_subclass
=
4814 in6_event2kev_array
[in6_ev_code
].in6_event_kev_subclass
;
4816 in6_event2kev_array
[in6_ev_code
].in6_event_kev_code
;
4818 nd6_event
.link_data
.if_family
= ifp
->if_family
;
4819 nd6_event
.link_data
.if_unit
= ifp
->if_unit
;
4820 strlcpy(nd6_event
.link_data
.if_name
, ifp
->if_name
,
4821 sizeof(nd6_event
.link_data
.if_name
));
4823 VERIFY(p_addr6
!= NULL
);
4824 bcopy(p_addr6
, &nd6_event
.in6_address
,
4825 sizeof(nd6_event
.in6_address
));
4826 nd6_event
.val
= val
;
4828 ev_msg
.dv
[0].data_ptr
= &nd6_event
;
4829 ev_msg
.dv
[0].data_length
= sizeof(nd6_event
);
4831 kev_post_msg(&ev_msg
);
4835 in6_event_callback(void *arg
)
4837 struct in6_event
*p_in6_ev
= (struct in6_event
*)arg
;
4839 EVENTHANDLER_INVOKE(&in6_evhdlr_ctxt
, in6_event
,
4840 p_in6_ev
->in6_event_code
, p_in6_ev
->in6_ifp
,
4841 &p_in6_ev
->in6_address
, p_in6_ev
->val
);
4844 struct in6_event_nwk_wq_entry
{
4845 struct nwk_wq_entry nwk_wqe
;
4846 struct in6_event in6_ev_arg
;
4850 in6_event_enqueue_nwk_wq_entry(in6_evhdlr_code_t in6_event_code
,
4851 struct ifnet
*ifp
, struct in6_addr
*p_addr6
,
4854 struct in6_event_nwk_wq_entry
*p_in6_ev
= NULL
;
4856 MALLOC(p_in6_ev
, struct in6_event_nwk_wq_entry
*,
4857 sizeof(struct in6_event_nwk_wq_entry
),
4858 M_NWKWQ
, M_WAITOK
| M_ZERO
);
4860 p_in6_ev
->nwk_wqe
.func
= in6_event_callback
;
4861 p_in6_ev
->nwk_wqe
.is_arg_managed
= TRUE
;
4862 p_in6_ev
->nwk_wqe
.arg
= &p_in6_ev
->in6_ev_arg
;
4864 p_in6_ev
->in6_ev_arg
.in6_event_code
= in6_event_code
;
4865 p_in6_ev
->in6_ev_arg
.in6_ifp
= ifp
;
4866 if (p_addr6
!= NULL
) {
4867 bcopy(p_addr6
, &p_in6_ev
->in6_ev_arg
.in6_address
,
4868 sizeof(p_in6_ev
->in6_ev_arg
.in6_address
));
4870 p_in6_ev
->in6_ev_arg
.val
= val
;
4872 nwk_wq_enqueue((struct nwk_wq_entry
*)p_in6_ev
);