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
),
305 * Make sure rt_ifa be equal to IFA, the second argument of the
307 * We need this because when we refer to rt_ifa->ia6_flags in
308 * ip6_input, we assume that the rt_ifa points to the address instead
309 * of the loopback address.
311 if (cmd
== RTM_ADD
&& nrt
&& ifa
!= nrt
->rt_ifa
) {
316 * Report the addition/removal of the address to the routing socket.
317 * XXX: since we called rtinit for a p2p interface with a destination,
318 * we end up reporting twice in such a case. Should we rather
319 * omit the second report?
322 rt_newaddrmsg(cmd
, ifa
, e
, nrt
);
323 if (cmd
== RTM_DELETE
) {
327 /* the cmd must be RTM_ADD here */
328 RT_REMREF_LOCKED(nrt
);
332 lck_mtx_unlock(rnh_lock
);
336 * Add ownaddr as loopback rtentry. We previously add the route only if
337 * necessary (ex. on a p2p link). However, since we now manage addresses
338 * separately from prefixes, we should always add the route. We can't
339 * rely on the cloning mechanism from the corresponding interface route
343 in6_ifaddloop(struct ifaddr
*ifa
)
348 * If there is no loopback entry, allocate one. ifa_addr for
349 * INET6 is set once during init; no need to hold lock.
351 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
354 if (rt
== NULL
|| (rt
->rt_flags
& RTF_HOST
) == 0 ||
355 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) == 0) {
357 RT_REMREF_LOCKED(rt
);
360 in6_ifloop_request(RTM_ADD
, ifa
);
361 } else if (rt
!= NULL
) {
362 RT_REMREF_LOCKED(rt
);
368 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
372 in6_ifremloop(struct ifaddr
*ifa
)
374 struct in6_ifaddr
*ia
;
379 * Some of BSD variants do not remove cloned routes
380 * from an interface direct route, when removing the direct route
381 * (see comments in net/net_osdep.h). Even for variants that do remove
382 * cloned routes, they could fail to remove the cloned routes when
383 * we handle multple addresses that share a common prefix.
384 * So, we should remove the route corresponding to the deleted address
385 * regardless of the result of in6_is_ifloop_auto().
389 * Delete the entry only if exact one ifa exists. More than one ifa
390 * can exist if we assign a same single address to multiple
391 * (probably p2p) interfaces.
392 * XXX: we should avoid such a configuration in IPv6...
394 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
395 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
396 IFA_LOCK(&ia
->ia_ifa
);
397 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa
), &ia
->ia_addr
.sin6_addr
)) {
400 IFA_UNLOCK(&ia
->ia_ifa
);
404 IFA_UNLOCK(&ia
->ia_ifa
);
406 lck_rw_done(&in6_ifaddr_rwlock
);
410 * Before deleting, check if a corresponding loopbacked host
411 * route surely exists. With this check, we can avoid to
412 * delete an interface direct route whose destination is same
413 * as the address being removed. This can happen when removing
414 * a subnet-router anycast address on an interface attahced
415 * to a shared medium. ifa_addr for INET6 is set once during
416 * init; no need to hold lock.
418 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
421 if ((rt
->rt_flags
& RTF_HOST
) != 0 &&
422 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
423 RT_REMREF_LOCKED(rt
);
425 in6_ifloop_request(RTM_DELETE
, ifa
);
435 in6_mask2len(struct in6_addr
*mask
, u_char
*lim0
)
438 u_char
*lim
= lim0
, *p
;
440 /* ignore the scope_id part */
441 if (lim0
== NULL
|| lim0
- (u_char
*)mask
> sizeof (*mask
))
442 lim
= (u_char
*)mask
+ sizeof (*mask
);
443 for (p
= (u_char
*)mask
; p
< lim
; x
++, p
++) {
449 for (y
= 0; y
< 8; y
++) {
450 if ((*p
& (0x80 >> y
)) == 0)
456 * when the limit pointer is given, do a stricter check on the
460 if (y
!= 0 && (*p
& (0x00ff >> y
)) != 0)
462 for (p
= p
+ 1; p
< lim
; p
++)
471 in6_len2mask(struct in6_addr
*mask
, int len
)
475 bzero(mask
, sizeof (*mask
));
476 for (i
= 0; i
< len
/ 8; i
++)
477 mask
->s6_addr8
[i
] = 0xff;
479 mask
->s6_addr8
[i
] = (0xff00 >> (len
% 8)) & 0xff;
483 in6_aliasreq_64_to_32(struct in6_aliasreq_64
*src
, struct in6_aliasreq_32
*dst
)
485 bzero(dst
, sizeof (*dst
));
486 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof (dst
->ifra_name
));
487 dst
->ifra_addr
= src
->ifra_addr
;
488 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
489 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
490 dst
->ifra_flags
= src
->ifra_flags
;
491 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
492 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
493 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
494 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
498 in6_aliasreq_32_to_64(struct in6_aliasreq_32
*src
, struct in6_aliasreq_64
*dst
)
500 bzero(dst
, sizeof (*dst
));
501 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof (dst
->ifra_name
));
502 dst
->ifra_addr
= src
->ifra_addr
;
503 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
504 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
505 dst
->ifra_flags
= src
->ifra_flags
;
506 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
507 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
508 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
509 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
512 #if defined(__LP64__)
514 in6_cgareq_32_to_64(struct in6_cgareq_32
*src
,
515 struct in6_cgareq_64
*dst
)
517 bzero(dst
, sizeof (*dst
));
518 bcopy(src
->cgar_name
, dst
->cgar_name
, sizeof (dst
->cgar_name
));
519 dst
->cgar_flags
= src
->cgar_flags
;
520 bcopy(src
->cgar_cgaprep
.cga_modifier
.octets
,
521 dst
->cgar_cgaprep
.cga_modifier
.octets
,
522 sizeof (dst
->cgar_cgaprep
.cga_modifier
.octets
));
523 dst
->cgar_cgaprep
.cga_security_level
=
524 src
->cgar_cgaprep
.cga_security_level
;
525 dst
->cgar_lifetime
.ia6t_expire
= src
->cgar_lifetime
.ia6t_expire
;
526 dst
->cgar_lifetime
.ia6t_preferred
= src
->cgar_lifetime
.ia6t_preferred
;
527 dst
->cgar_lifetime
.ia6t_vltime
= src
->cgar_lifetime
.ia6t_vltime
;
528 dst
->cgar_lifetime
.ia6t_pltime
= src
->cgar_lifetime
.ia6t_pltime
;
532 #if !defined(__LP64__)
534 in6_cgareq_64_to_32(struct in6_cgareq_64
*src
,
535 struct in6_cgareq_32
*dst
)
537 bzero(dst
, sizeof (*dst
));
538 bcopy(src
->cgar_name
, dst
->cgar_name
, sizeof (dst
->cgar_name
));
539 dst
->cgar_flags
= src
->cgar_flags
;
540 bcopy(src
->cgar_cgaprep
.cga_modifier
.octets
,
541 dst
->cgar_cgaprep
.cga_modifier
.octets
,
542 sizeof (dst
->cgar_cgaprep
.cga_modifier
.octets
));
543 dst
->cgar_cgaprep
.cga_security_level
=
544 src
->cgar_cgaprep
.cga_security_level
;
545 dst
->cgar_lifetime
.ia6t_expire
= src
->cgar_lifetime
.ia6t_expire
;
546 dst
->cgar_lifetime
.ia6t_preferred
= src
->cgar_lifetime
.ia6t_preferred
;
547 dst
->cgar_lifetime
.ia6t_vltime
= src
->cgar_lifetime
.ia6t_vltime
;
548 dst
->cgar_lifetime
.ia6t_pltime
= src
->cgar_lifetime
.ia6t_pltime
;
552 static struct in6_aliasreq
*
553 in6_aliasreq_to_native(void *data
, int data_is_64
, struct in6_aliasreq
*dst
)
555 #if defined(__LP64__)
557 bcopy(data
, dst
, sizeof (*dst
));
559 in6_aliasreq_32_to_64((struct in6_aliasreq_32
*)data
,
560 (struct in6_aliasreq_64
*)dst
);
563 in6_aliasreq_64_to_32((struct in6_aliasreq_64
*)data
,
564 (struct in6_aliasreq_32
*)dst
);
566 bcopy(data
, dst
, sizeof (*dst
));
567 #endif /* __LP64__ */
571 static struct in6_cgareq
*
572 in6_cgareq_to_native(void *data
, int is64
, struct in6_cgareq
*dst
)
574 #if defined(__LP64__)
576 bcopy(data
, dst
, sizeof (*dst
));
578 in6_cgareq_32_to_64((struct in6_cgareq_32
*)data
,
579 (struct in6_cgareq_64
*)dst
);
582 in6_cgareq_64_to_32((struct in6_cgareq_64
*)data
,
583 (struct in6_cgareq_32
*)dst
);
585 bcopy(data
, dst
, sizeof (*dst
));
586 #endif /* __LP64__ */
590 static __attribute__((noinline
)) int
591 in6ctl_associd(struct socket
*so
, u_long cmd
, caddr_t data
)
595 struct so_aidreq32 a32
;
596 struct so_aidreq64 a64
;
602 case SIOCGASSOCIDS32
: { /* struct so_aidreq32 */
603 bcopy(data
, &u
.a32
, sizeof (u
.a32
));
604 error
= in6_getassocids(so
, &u
.a32
.sar_cnt
, u
.a32
.sar_aidp
);
606 bcopy(&u
.a32
, data
, sizeof (u
.a32
));
610 case SIOCGASSOCIDS64
: { /* struct so_aidreq64 */
611 bcopy(data
, &u
.a64
, sizeof (u
.a64
));
612 error
= in6_getassocids(so
, &u
.a64
.sar_cnt
, u
.a64
.sar_aidp
);
614 bcopy(&u
.a64
, data
, sizeof (u
.a64
));
626 static __attribute__((noinline
)) int
627 in6ctl_connid(struct socket
*so
, u_long cmd
, caddr_t data
)
631 struct so_cidreq32 c32
;
632 struct so_cidreq64 c64
;
638 case SIOCGCONNIDS32
: { /* struct so_cidreq32 */
639 bcopy(data
, &u
.c32
, sizeof (u
.c32
));
640 error
= in6_getconnids(so
, u
.c32
.scr_aid
, &u
.c32
.scr_cnt
,
643 bcopy(&u
.c32
, data
, sizeof (u
.c32
));
647 case SIOCGCONNIDS64
: { /* struct so_cidreq64 */
648 bcopy(data
, &u
.c64
, sizeof (u
.c64
));
649 error
= in6_getconnids(so
, u
.c64
.scr_aid
, &u
.c64
.scr_cnt
,
652 bcopy(&u
.c64
, data
, sizeof (u
.c64
));
664 static __attribute__((noinline
)) int
665 in6ctl_conninfo(struct socket
*so
, u_long cmd
, caddr_t data
)
669 struct so_cinforeq32 ci32
;
670 struct so_cinforeq64 ci64
;
676 case SIOCGCONNINFO32
: { /* struct so_cinforeq32 */
677 bcopy(data
, &u
.ci32
, sizeof (u
.ci32
));
678 error
= in6_getconninfo(so
, u
.ci32
.scir_cid
, &u
.ci32
.scir_flags
,
679 &u
.ci32
.scir_ifindex
, &u
.ci32
.scir_error
, u
.ci32
.scir_src
,
680 &u
.ci32
.scir_src_len
, u
.ci32
.scir_dst
, &u
.ci32
.scir_dst_len
,
681 &u
.ci32
.scir_aux_type
, u
.ci32
.scir_aux_data
,
682 &u
.ci32
.scir_aux_len
);
684 bcopy(&u
.ci32
, data
, sizeof (u
.ci32
));
688 case SIOCGCONNINFO64
: { /* struct so_cinforeq64 */
689 bcopy(data
, &u
.ci64
, sizeof (u
.ci64
));
690 error
= in6_getconninfo(so
, u
.ci64
.scir_cid
, &u
.ci64
.scir_flags
,
691 &u
.ci64
.scir_ifindex
, &u
.ci64
.scir_error
, u
.ci64
.scir_src
,
692 &u
.ci64
.scir_src_len
, u
.ci64
.scir_dst
, &u
.ci64
.scir_dst_len
,
693 &u
.ci64
.scir_aux_type
, u
.ci64
.scir_aux_data
,
694 &u
.ci64
.scir_aux_len
);
696 bcopy(&u
.ci64
, data
, sizeof (u
.ci64
));
708 static __attribute__((noinline
)) int
709 in6ctl_llstart(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
711 struct in6_aliasreq sifra
, *ifra
= NULL
;
718 case SIOCLL_START_32
: /* struct in6_aliasreq_32 */
719 case SIOCLL_START_64
: /* struct in6_aliasreq_64 */
720 is64
= (cmd
== SIOCLL_START_64
);
722 * Convert user ifra to the kernel form, when appropriate.
723 * This allows the conversion between different data models
724 * to be centralized, so that it can be passed around to other
725 * routines that are expecting the kernel form.
727 ifra
= in6_aliasreq_to_native(data
, is64
, &sifra
);
730 * NOTE: All the interface specific DLIL attachements should
731 * be done here. They are currently done in in6_ifattach_aux()
732 * for the interfaces that need it.
734 if (ifra
->ifra_addr
.sin6_family
== AF_INET6
&&
735 /* Only check ifra_dstaddr if valid */
736 (ifra
->ifra_dstaddr
.sin6_len
== 0 ||
737 ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
)) {
738 /* some interfaces may provide LinkLocal addresses */
739 error
= in6_ifattach_aliasreq(ifp
, NULL
, ifra
);
741 error
= in6_ifattach_aliasreq(ifp
, NULL
, NULL
);
744 in6_if_up_dad_start(ifp
);
755 static __attribute__((noinline
)) int
756 in6ctl_llstop(struct ifnet
*ifp
)
758 struct in6_ifaddr
*ia
;
759 struct nd_prefix pr0
, *pr
;
763 /* Remove link local addresses from interface */
764 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
767 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
771 IFA_LOCK(&ia
->ia_ifa
);
772 if (IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
)) {
773 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
774 IFA_UNLOCK(&ia
->ia_ifa
);
775 lck_rw_done(&in6_ifaddr_rwlock
);
776 in6_purgeaddr(&ia
->ia_ifa
);
777 IFA_REMREF(&ia
->ia_ifa
); /* for us */
778 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
780 * Purging the address caused in6_ifaddr_rwlock
781 * to be dropped and reacquired;
782 * therefore search again from the beginning
783 * of in6_ifaddrs list.
788 IFA_UNLOCK(&ia
->ia_ifa
);
791 lck_rw_done(&in6_ifaddr_rwlock
);
793 /* Delete the link local prefix */
794 bzero(&pr0
, sizeof(pr0
));
797 pr0
.ndpr_prefix
.sin6_addr
.s6_addr16
[0] = IPV6_ADDR_INT16_ULL
;
798 in6_setscope(&pr0
.ndpr_prefix
.sin6_addr
, ifp
, NULL
);
799 pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_UNSPEC
);
801 lck_mtx_lock(nd6_mutex
);
805 NDPR_REMREF(pr
); /* Drop the reference from lookup */
806 lck_mtx_unlock(nd6_mutex
);
813 * This routine configures secure link local address
815 static __attribute__((noinline
)) int
816 in6ctl_cgastart(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
818 struct in6_cgareq llcgasr
;
824 case SIOCLL_CGASTART_32
: /* struct in6_cgareq_32 */
825 case SIOCLL_CGASTART_64
: /* struct in6_cgareq_64 */
826 is64
= (cmd
== SIOCLL_CGASTART_64
);
828 * Convert user cgareq to the kernel form, when appropriate.
829 * This allows the conversion between different data models
830 * to be centralized, so that it can be passed around to other
831 * routines that are expecting the kernel form.
833 in6_cgareq_to_native(data
, is64
, &llcgasr
);
836 * NOTE: All the interface specific DLIL attachements
837 * should be done here. They are currently done in
838 * in6_ifattach_cgareq() for the interfaces that
841 error
= in6_ifattach_llcgareq(ifp
, &llcgasr
);
843 in6_if_up_dad_start(ifp
);
855 * Caller passes in the ioctl data pointer directly via "ifr", with the
856 * expectation that this routine always uses bcopy() or other byte-aligned
859 static __attribute__((noinline
)) int
860 in6ctl_gifaddr(struct ifnet
*ifp
, struct in6_ifaddr
*ia
, u_long cmd
,
861 struct in6_ifreq
*ifr
)
863 struct sockaddr_in6 addr
;
869 return (EADDRNOTAVAIL
);
872 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
873 IFA_LOCK(&ia
->ia_ifa
);
874 bcopy(&ia
->ia_addr
, &addr
, sizeof (addr
));
875 IFA_UNLOCK(&ia
->ia_ifa
);
876 if ((error
= sa6_recoverscope(&addr
, TRUE
)) != 0)
878 bcopy(&addr
, &ifr
->ifr_addr
, sizeof (addr
));
881 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
882 if (!(ifp
->if_flags
& IFF_POINTOPOINT
)) {
887 * XXX: should we check if ifa_dstaddr is NULL and return
890 IFA_LOCK(&ia
->ia_ifa
);
891 bcopy(&ia
->ia_dstaddr
, &addr
, sizeof (addr
));
892 IFA_UNLOCK(&ia
->ia_ifa
);
893 if ((error
= sa6_recoverscope(&addr
, TRUE
)) != 0)
895 bcopy(&addr
, &ifr
->ifr_dstaddr
, sizeof (addr
));
907 * Caller passes in the ioctl data pointer directly via "ifr", with the
908 * expectation that this routine always uses bcopy() or other byte-aligned
911 static __attribute__((noinline
)) int
912 in6ctl_gifstat(struct ifnet
*ifp
, u_long cmd
, struct in6_ifreq
*ifr
)
914 int error
= 0, index
;
917 index
= ifp
->if_index
;
920 case SIOCGIFSTAT_IN6
: /* struct in6_ifreq */
921 /* N.B.: if_inet6data is never freed once set. */
922 if (IN6_IFEXTRA(ifp
) == NULL
) {
923 /* return (EAFNOSUPPORT)? */
924 bzero(&ifr
->ifr_ifru
.ifru_stat
,
925 sizeof (ifr
->ifr_ifru
.ifru_stat
));
927 bcopy(&IN6_IFEXTRA(ifp
)->in6_ifstat
,
928 &ifr
->ifr_ifru
.ifru_stat
,
929 sizeof (ifr
->ifr_ifru
.ifru_stat
));
933 case SIOCGIFSTAT_ICMP6
: /* struct in6_ifreq */
934 /* N.B.: if_inet6data is never freed once set. */
935 if (IN6_IFEXTRA(ifp
) == NULL
) {
936 /* return (EAFNOSUPPORT)? */
937 bzero(&ifr
->ifr_ifru
.ifru_icmp6stat
,
938 sizeof (ifr
->ifr_ifru
.ifru_icmp6stat
));
940 bcopy(&IN6_IFEXTRA(ifp
)->icmp6_ifstat
,
941 &ifr
->ifr_ifru
.ifru_icmp6stat
,
942 sizeof (ifr
->ifr_ifru
.ifru_icmp6stat
));
955 * Caller passes in the ioctl data pointer directly via "ifr", with the
956 * expectation that this routine always uses bcopy() or other byte-aligned
959 static __attribute__((noinline
)) int
960 in6ctl_alifetime(struct in6_ifaddr
*ia
, u_long cmd
, struct in6_ifreq
*ifr
,
963 uint64_t timenow
= net_uptime();
964 struct in6_addrlifetime ia6_lt
;
965 struct timeval caltime
;
969 return (EADDRNOTAVAIL
);
972 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
973 IFA_LOCK(&ia
->ia_ifa
);
974 /* retrieve time as calendar time (last arg is 1) */
975 in6ifa_getlifetime(ia
, &ia6_lt
, 1);
977 struct in6_addrlifetime_64 lt
;
979 bzero(<
, sizeof (lt
));
980 lt
.ia6t_expire
= ia6_lt
.ia6t_expire
;
981 lt
.ia6t_preferred
= ia6_lt
.ia6t_preferred
;
982 lt
.ia6t_vltime
= ia6_lt
.ia6t_vltime
;
983 lt
.ia6t_pltime
= ia6_lt
.ia6t_pltime
;
984 bcopy(<
, &ifr
->ifr_ifru
.ifru_lifetime
, sizeof (lt
));
986 struct in6_addrlifetime_32 lt
;
988 bzero(<
, sizeof (lt
));
989 lt
.ia6t_expire
= (uint32_t)ia6_lt
.ia6t_expire
;
990 lt
.ia6t_preferred
= (uint32_t)ia6_lt
.ia6t_preferred
;
991 lt
.ia6t_vltime
= (uint32_t)ia6_lt
.ia6t_vltime
;
992 lt
.ia6t_pltime
= (uint32_t)ia6_lt
.ia6t_pltime
;
993 bcopy(<
, &ifr
->ifr_ifru
.ifru_lifetime
, sizeof (lt
));
995 IFA_UNLOCK(&ia
->ia_ifa
);
998 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
999 getmicrotime(&caltime
);
1001 /* sanity for overflow - beware unsigned */
1003 struct in6_addrlifetime_64 lt
;
1005 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1006 if (lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
&&
1007 lt
.ia6t_vltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1011 if (lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
&&
1012 lt
.ia6t_pltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1017 struct in6_addrlifetime_32 lt
;
1019 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1020 if (lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
&&
1021 lt
.ia6t_vltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1025 if (lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
&&
1026 lt
.ia6t_pltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1032 IFA_LOCK(&ia
->ia_ifa
);
1034 struct in6_addrlifetime_64 lt
;
1036 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1037 ia6_lt
.ia6t_expire
= lt
.ia6t_expire
;
1038 ia6_lt
.ia6t_preferred
= lt
.ia6t_preferred
;
1039 ia6_lt
.ia6t_vltime
= lt
.ia6t_vltime
;
1040 ia6_lt
.ia6t_pltime
= lt
.ia6t_pltime
;
1042 struct in6_addrlifetime_32 lt
;
1044 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1045 ia6_lt
.ia6t_expire
= (uint32_t)lt
.ia6t_expire
;
1046 ia6_lt
.ia6t_preferred
= (uint32_t)lt
.ia6t_preferred
;
1047 ia6_lt
.ia6t_vltime
= lt
.ia6t_vltime
;
1048 ia6_lt
.ia6t_pltime
= lt
.ia6t_pltime
;
1051 if (ia6_lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
)
1052 ia6_lt
.ia6t_expire
= timenow
+ ia6_lt
.ia6t_vltime
;
1054 ia6_lt
.ia6t_expire
= 0;
1056 if (ia6_lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
)
1057 ia6_lt
.ia6t_preferred
= timenow
+ ia6_lt
.ia6t_pltime
;
1059 ia6_lt
.ia6t_preferred
= 0;
1061 in6ifa_setlifetime(ia
, &ia6_lt
);
1062 IFA_UNLOCK(&ia
->ia_ifa
);
1074 in6ctl_clat46start(struct ifnet
*ifp
)
1076 struct nd_prefix
*pr
= NULL
;
1077 struct nd_prefix
*next
= NULL
;
1078 struct in6_ifaddr
*ia6
= NULL
;
1084 * Traverse the list of prefixes and find the first non-linklocal
1085 * prefix on the interface.
1086 * For that found eligible prefix, configure a CLAT46 reserved address.
1088 lck_mtx_lock(nd6_mutex
);
1089 for (pr
= nd_prefix
.lh_first
; pr
; pr
= next
) {
1090 next
= pr
->ndpr_next
;
1093 if (pr
->ndpr_ifp
!= ifp
) {
1098 if (IN6_IS_ADDR_LINKLOCAL(&pr
->ndpr_prefix
.sin6_addr
)) {
1103 if (pr
->ndpr_raf_auto
== 0) {
1108 if (pr
->ndpr_stateflags
& NDPRF_DEFUNCT
) {
1113 if ((pr
->ndpr_stateflags
& NDPRF_CLAT46
) == 0
1114 && pr
->ndpr_vltime
!= 0) {
1115 NDPR_ADDREF_LOCKED(pr
); /* Take reference for rest of the processing */
1123 lck_mtx_unlock(nd6_mutex
);
1126 if ((ia6
= in6_pfx_newpersistaddr(pr
, FALSE
, &error
, TRUE
)) == NULL
) {
1127 nd6log0((LOG_ERR
, "Could not configure CLAT46 address on interface "
1128 "%s.\n", ifp
->if_xname
));
1130 IFA_LOCK(&ia6
->ia_ifa
);
1133 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
1134 pr
->ndpr_stateflags
|= NDPRF_CLAT46
;
1136 VERIFY(pr
->ndpr_addrcnt
!= 0);
1138 IFA_UNLOCK(&ia6
->ia_ifa
);
1139 IFA_REMREF(&ia6
->ia_ifa
);
1142 * A newly added address might affect the status
1143 * of other addresses, so we check and update it.
1144 * XXX: what if address duplication happens?
1146 lck_mtx_lock(nd6_mutex
);
1147 pfxlist_onlink_check();
1148 lck_mtx_unlock(nd6_mutex
);
1155 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(void *)(ifa))
1158 * Generic INET6 control operations (ioctl's).
1160 * ifp is NULL if not an interface-specific ioctl.
1162 * Most of the routines called to handle the ioctls would end up being
1163 * tail-call optimized, which unfortunately causes this routine to
1164 * consume too much stack space; this is the reason for the "noinline"
1165 * attribute used on those routines.
1167 * If called directly from within the networking stack (as opposed to via
1168 * pru_control), the socket parameter may be NULL.
1171 in6_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
1174 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
1175 struct in6_aliasreq sifra
, *ifra
= NULL
;
1176 struct in6_ifaddr
*ia
= NULL
;
1177 struct sockaddr_in6 sin6
, *sa6
= NULL
;
1178 boolean_t privileged
= (proc_suser(p
) == 0);
1179 boolean_t p64
= proc_is64bit(p
);
1180 boolean_t so_unlocked
= FALSE
;
1181 int intval
, error
= 0;
1183 /* In case it's NULL, make sure it came from the kernel */
1184 VERIFY(so
!= NULL
|| p
== kernproc
);
1187 * ioctls which don't require ifp, may require socket.
1190 case SIOCAADDRCTL_POLICY
: /* struct in6_addrpolicy */
1191 case SIOCDADDRCTL_POLICY
: /* struct in6_addrpolicy */
1194 return (in6_src_ioctl(cmd
, data
));
1197 case SIOCDRADD_IN6_32
: /* struct in6_defrouter_32 */
1198 case SIOCDRADD_IN6_64
: /* struct in6_defrouter_64 */
1199 case SIOCDRDEL_IN6_32
: /* struct in6_defrouter_32 */
1200 case SIOCDRDEL_IN6_64
: /* struct in6_defrouter_64 */
1203 return (defrtrlist_ioctl(cmd
, data
));
1206 case SIOCGASSOCIDS32
: /* struct so_aidreq32 */
1207 case SIOCGASSOCIDS64
: /* struct so_aidreq64 */
1208 return (in6ctl_associd(so
, cmd
, data
));
1211 case SIOCGCONNIDS32
: /* struct so_cidreq32 */
1212 case SIOCGCONNIDS64
: /* struct so_cidreq64 */
1213 return (in6ctl_connid(so
, cmd
, data
));
1216 case SIOCGCONNINFO32
: /* struct so_cinforeq32 */
1217 case SIOCGCONNINFO64
: /* struct so_cinforeq64 */
1218 return (in6ctl_conninfo(so
, cmd
, data
));
1223 * The rest of ioctls require ifp; reject if we don't have one;
1224 * return ENXIO to be consistent with ifioctl().
1230 * Unlock the socket since ifnet_ioctl() may be invoked by
1231 * one of the ioctl handlers below. Socket will be re-locked
1232 * prior to returning.
1235 socket_unlock(so
, 0);
1240 * ioctls which require ifp but not interface address.
1243 case SIOCAUTOCONF_START
: /* struct in6_ifreq */
1248 error
= in6_autoconf(ifp
, TRUE
);
1251 case SIOCAUTOCONF_STOP
: /* struct in6_ifreq */
1256 error
= in6_autoconf(ifp
, FALSE
);
1259 case SIOCLL_START_32
: /* struct in6_aliasreq_32 */
1260 case SIOCLL_START_64
: /* struct in6_aliasreq_64 */
1265 error
= in6ctl_llstart(ifp
, cmd
, data
);
1268 case SIOCLL_STOP
: /* struct in6_ifreq */
1273 error
= in6ctl_llstop(ifp
);
1276 case SIOCCLAT46_START
: /* struct in6_ifreq */
1281 error
= in6ctl_clat46start(ifp
);
1283 ifp
->if_eflags
|= IFEF_CLAT46
;
1286 case SIOCCLAT46_STOP
: /* struct in6_ifreq */
1293 * Not much to be done here and it might not be needed
1294 * It would usually be done when IPv6 configuration is being
1296 * XXX Probably STOP equivalent is not needed here.
1298 ifp
->if_eflags
&= ~IFEF_CLAT46
;
1300 case SIOCSETROUTERMODE_IN6
: /* struct in6_ifreq */
1305 bcopy(&((struct in6_ifreq
*)(void *)data
)->ifr_intval
,
1306 &intval
, sizeof (intval
));
1308 error
= in6_setrouter(ifp
, intval
);
1311 case SIOCPROTOATTACH_IN6_32
: /* struct in6_aliasreq_32 */
1312 case SIOCPROTOATTACH_IN6_64
: /* struct in6_aliasreq_64 */
1317 error
= in6_domifattach(ifp
);
1320 case SIOCPROTODETACH_IN6
: /* struct in6_ifreq */
1325 /* Cleanup interface routes and addresses */
1328 if ((error
= proto_unplumb(PF_INET6
, ifp
)))
1329 log(LOG_ERR
, "SIOCPROTODETACH_IN6: %s error=%d\n",
1330 if_name(ifp
), error
);
1333 case SIOCSNDFLUSH_IN6
: /* struct in6_ifreq */
1334 case SIOCSPFXFLUSH_IN6
: /* struct in6_ifreq */
1335 case SIOCSRTRFLUSH_IN6
: /* struct in6_ifreq */
1336 case SIOCSDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
1337 case SIOCSDEFIFACE_IN6_64
: /* struct in6_ndifreq_64 */
1338 case SIOCSIFINFO_FLAGS
: /* struct in6_ndireq */
1339 case SIOCGIFCGAPREP_IN6
: /* struct in6_ifreq */
1340 case SIOCSIFCGAPREP_IN6
: /* struct in6_ifreq */
1346 case OSIOCGIFINFO_IN6
: /* struct in6_ondireq */
1347 case SIOCGIFINFO_IN6
: /* struct in6_ondireq */
1348 case SIOCGDRLST_IN6_32
: /* struct in6_drlist_32 */
1349 case SIOCGDRLST_IN6_64
: /* struct in6_drlist_64 */
1350 case SIOCGPRLST_IN6_32
: /* struct in6_prlist_32 */
1351 case SIOCGPRLST_IN6_64
: /* struct in6_prlist_64 */
1352 case SIOCGNBRINFO_IN6_32
: /* struct in6_nbrinfo_32 */
1353 case SIOCGNBRINFO_IN6_64
: /* struct in6_nbrinfo_64 */
1354 case SIOCGDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
1355 case SIOCGDEFIFACE_IN6_64
: /* struct in6_ndifreq_64 */
1356 error
= nd6_ioctl(cmd
, data
, ifp
);
1359 case SIOCSIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1360 case SIOCDIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1361 case SIOCAIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1362 case SIOCCIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1363 case SIOCSGIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1364 case SIOCGIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1366 "prefix ioctls are now invalidated. "
1367 "please use ifconfig.\n");
1371 case SIOCSSCOPE6
: /* struct in6_ifreq (deprecated) */
1372 case SIOCGSCOPE6
: /* struct in6_ifreq (deprecated) */
1373 case SIOCGSCOPE6DEF
: /* struct in6_ifreq (deprecated) */
1377 case SIOCLL_CGASTART_32
: /* struct in6_cgareq_32 */
1378 case SIOCLL_CGASTART_64
: /* struct in6_cgareq_64 */
1382 error
= in6ctl_cgastart(ifp
, cmd
, data
);
1385 case SIOCGIFSTAT_IN6
: /* struct in6_ifreq */
1386 case SIOCGIFSTAT_ICMP6
: /* struct in6_ifreq */
1387 error
= in6ctl_gifstat(ifp
, cmd
, ifr
);
1392 * ioctls which require interface address; obtain sockaddr_in6.
1395 case SIOCSIFADDR_IN6
: /* struct in6_ifreq (deprecated) */
1396 case SIOCSIFDSTADDR_IN6
: /* struct in6_ifreq (deprecated) */
1397 case SIOCSIFNETMASK_IN6
: /* struct in6_ifreq (deprecated) */
1399 * Since IPv6 allows a node to assign multiple addresses
1400 * on a single interface, SIOCSIFxxx ioctls are deprecated.
1402 /* we decided to obsolete this command (20000704) */
1406 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1407 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1413 * Convert user ifra to the kernel form, when appropriate.
1414 * This allows the conversion between different data models
1415 * to be centralized, so that it can be passed around to other
1416 * routines that are expecting the kernel form.
1418 ifra
= in6_aliasreq_to_native(data
,
1419 (cmd
== SIOCAIFADDR_IN6_64
), &sifra
);
1420 bcopy(&ifra
->ifra_addr
, &sin6
, sizeof (sin6
));
1424 case SIOCDIFADDR_IN6
: /* struct in6_ifreq */
1425 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
1431 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
1432 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
1433 case SIOCGIFNETMASK_IN6
: /* struct in6_ifreq */
1434 case SIOCGIFAFLAG_IN6
: /* struct in6_ifreq */
1435 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
1436 bcopy(&ifr
->ifr_addr
, &sin6
, sizeof (sin6
));
1439 case SIOCGIFDSTADDR
:
1440 case SIOCSIFDSTADDR
:
1441 case SIOCGIFBRDADDR
:
1442 case SIOCSIFBRDADDR
:
1443 case SIOCGIFNETMASK
:
1444 case SIOCSIFNETMASK
:
1449 /* Do not handle these AF_INET commands in AF_INET6 path */
1455 * Find address for this interface, if it exists.
1457 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
1458 * only, and used the first interface address as the target of other
1459 * operations (without checking ifra_addr). This was because netinet
1460 * code/API assumed at most 1 interface address per interface.
1461 * Since IPv6 allows a node to assign multiple addresses
1462 * on a single interface, we almost always look and check the
1463 * presence of ifra_addr, and reject invalid ones here.
1464 * It also decreases duplicated code among SIOC*_IN6 operations.
1467 if (sa6
!= NULL
&& sa6
->sin6_family
== AF_INET6
) {
1468 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
1469 if (sa6
->sin6_addr
.s6_addr16
[1] == 0) {
1470 /* link ID is not embedded by the user */
1471 sa6
->sin6_addr
.s6_addr16
[1] =
1472 htons(ifp
->if_index
);
1473 } else if (sa6
->sin6_addr
.s6_addr16
[1] !=
1474 htons(ifp
->if_index
)) {
1475 error
= EINVAL
; /* link ID contradicts */
1478 if (sa6
->sin6_scope_id
) {
1479 if (sa6
->sin6_scope_id
!=
1480 (u_int32_t
)ifp
->if_index
) {
1484 sa6
->sin6_scope_id
= 0; /* XXX: good way? */
1488 * Any failures from this point on must take into account
1489 * a non-NULL "ia" with an outstanding reference count, and
1490 * therefore requires IFA_REMREF. Jump to "done" label
1491 * instead of calling return if "ia" is valid.
1493 ia
= in6ifa_ifpwithaddr(ifp
, &sa6
->sin6_addr
);
1497 * SIOCDIFADDR_IN6/SIOCAIFADDR_IN6 specific tests.
1500 case SIOCDIFADDR_IN6
: /* struct in6_ifreq */
1502 error
= EADDRNOTAVAIL
;
1506 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1507 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1508 VERIFY(sa6
!= NULL
);
1510 * We always require users to specify a valid IPv6 address for
1511 * the corresponding operation. Use "sa6" instead of "ifra"
1512 * since SIOCDIFADDR_IN6 falls thru above.
1514 if (sa6
->sin6_family
!= AF_INET6
||
1515 sa6
->sin6_len
!= sizeof (struct sockaddr_in6
)) {
1516 error
= EAFNOSUPPORT
;
1523 * And finally process address-related ioctls.
1526 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
1527 /* This interface is basically deprecated. use SIOCGIFCONF. */
1529 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
1530 error
= in6ctl_gifaddr(ifp
, ia
, cmd
, ifr
);
1533 case SIOCGIFNETMASK_IN6
: /* struct in6_ifreq */
1535 IFA_LOCK(&ia
->ia_ifa
);
1536 bcopy(&ia
->ia_prefixmask
, &ifr
->ifr_addr
,
1537 sizeof (struct sockaddr_in6
));
1538 IFA_UNLOCK(&ia
->ia_ifa
);
1540 error
= EADDRNOTAVAIL
;
1544 case SIOCGIFAFLAG_IN6
: /* struct in6_ifreq */
1546 IFA_LOCK(&ia
->ia_ifa
);
1547 bcopy(&ia
->ia6_flags
, &ifr
->ifr_ifru
.ifru_flags6
,
1548 sizeof (ifr
->ifr_ifru
.ifru_flags6
));
1549 IFA_UNLOCK(&ia
->ia_ifa
);
1551 error
= EADDRNOTAVAIL
;
1555 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
1556 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
1557 error
= in6ctl_alifetime(ia
, cmd
, ifr
, p64
);
1560 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1561 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1562 error
= in6ctl_aifaddr(ifp
, ifra
);
1565 case SIOCDIFADDR_IN6
:
1566 in6ctl_difaddr(ifp
, ia
);
1570 error
= ifnet_ioctl(ifp
, PF_INET6
, cmd
, data
);
1576 IFA_REMREF(&ia
->ia_ifa
);
1583 static __attribute__((noinline
)) int
1584 in6ctl_aifaddr(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
)
1586 int i
, error
, addtmp
, plen
;
1587 struct nd_prefix pr0
, *pr
;
1588 struct in6_ifaddr
*ia
;
1590 VERIFY(ifp
!= NULL
&& ifra
!= NULL
);
1593 /* Attempt to attach the protocol, in case it isn't attached */
1594 error
= in6_domifattach(ifp
);
1596 /* PF_INET6 wasn't previously attached */
1597 error
= in6_ifattach_aliasreq(ifp
, NULL
, NULL
);
1601 in6_if_up_dad_start(ifp
);
1602 } else if (error
!= EEXIST
) {
1607 * First, make or update the interface address structure, and link it
1610 error
= in6_update_ifa(ifp
, ifra
, 0, &ia
);
1615 /* Now, make the prefix on-link on the interface. */
1616 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
, NULL
);
1621 * NOTE: We'd rather create the prefix before the address, but we need
1622 * at least one address to install the corresponding interface route,
1623 * so we configure the address first.
1627 * Convert mask to prefix length (prefixmask has already been validated
1628 * in in6_update_ifa().
1630 bzero(&pr0
, sizeof (pr0
));
1631 pr0
.ndpr_plen
= plen
;
1633 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
1634 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
1636 /* apply the mask for safety. */
1637 for (i
= 0; i
< 4; i
++) {
1638 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1639 ifra
->ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
1643 * Since we don't have an API to set prefix (not address) lifetimes, we
1644 * just use the same lifetimes as addresses. The (temporarily)
1645 * installed lifetimes can be overridden by later advertised RAs (when
1646 * accept_rtadv is non 0), which is an intended behavior.
1648 pr0
.ndpr_raf_onlink
= 1; /* should be configurable? */
1649 pr0
.ndpr_raf_auto
= !!(ifra
->ifra_flags
& IN6_IFF_AUTOCONF
);
1650 pr0
.ndpr_vltime
= ifra
->ifra_lifetime
.ia6t_vltime
;
1651 pr0
.ndpr_pltime
= ifra
->ifra_lifetime
.ia6t_pltime
;
1652 pr0
.ndpr_stateflags
|= NDPRF_STATIC
;
1653 lck_mtx_init(&pr0
.ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
1655 /* add the prefix if there's none. */
1656 if ((pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_NEVER
)) == NULL
) {
1658 * nd6_prelist_add will install the corresponding interface
1661 error
= nd6_prelist_add(&pr0
, NULL
, &pr
, FALSE
);
1666 log(LOG_ERR
, "%s: nd6_prelist_add okay, but"
1667 " no prefix.\n", __func__
);
1673 IFA_LOCK(&ia
->ia_ifa
);
1675 /* if this is a new autoconfed addr */
1677 if (ia
->ia6_ndpr
== NULL
) {
1680 VERIFY(pr
->ndpr_addrcnt
!= 0);
1682 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
1685 * If this is the first autoconf address from the prefix,
1686 * create a temporary address as well (when specified).
1688 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
1690 pr
->ndpr_addrcnt
== 1) {
1696 IFA_UNLOCK(&ia
->ia_ifa
);
1700 e
= in6_tmpifadd(ia
, 1);
1702 log(LOG_NOTICE
, "%s: failed to create a"
1703 " temporary address, error=%d\n",
1708 * This might affect the status of autoconfigured addresses, that is,
1709 * this address might make other addresses detached.
1711 lck_mtx_lock(nd6_mutex
);
1712 pfxlist_onlink_check();
1713 lck_mtx_unlock(nd6_mutex
);
1715 /* Drop use count held above during lookup/add */
1720 IFA_REMREF(&ia
->ia_ifa
);
1724 static __attribute__((noinline
)) void
1725 in6ctl_difaddr(struct ifnet
*ifp
, struct in6_ifaddr
*ia
)
1728 struct nd_prefix pr0
, *pr
;
1730 VERIFY(ifp
!= NULL
&& ia
!= NULL
);
1733 * If the address being deleted is the only one that owns
1734 * the corresponding prefix, expire the prefix as well.
1735 * XXX: theoretically, we don't have to worry about such
1736 * relationship, since we separate the address management
1737 * and the prefix management. We do this, however, to provide
1738 * as much backward compatibility as possible in terms of
1739 * the ioctl operation.
1740 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
1742 IFA_LOCK(&ia
->ia_ifa
);
1743 bzero(&pr0
, sizeof (pr0
));
1745 pr0
.ndpr_plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
);
1746 if (pr0
.ndpr_plen
== 128) {
1747 IFA_UNLOCK(&ia
->ia_ifa
);
1750 pr0
.ndpr_prefix
= ia
->ia_addr
;
1751 pr0
.ndpr_mask
= ia
->ia_prefixmask
.sin6_addr
;
1752 for (i
= 0; i
< 4; i
++) {
1753 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1754 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
];
1756 IFA_UNLOCK(&ia
->ia_ifa
);
1758 if ((pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_UNSPEC
)) != NULL
) {
1759 IFA_LOCK(&ia
->ia_ifa
);
1761 if (pr
->ndpr_addrcnt
== 1) {
1762 /* XXX: just for expiration */
1763 pr
->ndpr_expire
= 1;
1766 IFA_UNLOCK(&ia
->ia_ifa
);
1768 /* Drop use count held above during lookup */
1773 in6_purgeaddr(&ia
->ia_ifa
);
1776 static __attribute__((noinline
)) int
1777 in6_autoconf(struct ifnet
*ifp
, int enable
)
1781 VERIFY(ifp
!= NULL
);
1783 if (ifp
->if_flags
& IFF_LOOPBACK
)
1788 * An interface in IPv6 router mode implies that it
1789 * is either configured with a static IP address or
1790 * autoconfigured via a locally-generated RA. Prevent
1791 * SIOCAUTOCONF_START from being set in that mode.
1793 ifnet_lock_exclusive(ifp
);
1794 if (ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
1795 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADV
;
1798 ifp
->if_eflags
|= IFEF_ACCEPT_RTADV
;
1800 ifnet_lock_done(ifp
);
1802 struct in6_ifaddr
*ia
= NULL
;
1804 ifnet_lock_exclusive(ifp
);
1805 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADV
;
1806 ifnet_lock_done(ifp
);
1808 /* Remove autoconfigured address from interface */
1809 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1811 while (ia
!= NULL
) {
1812 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
1816 IFA_LOCK(&ia
->ia_ifa
);
1817 if (ia
->ia6_flags
& IN6_IFF_AUTOCONF
) {
1818 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
1819 IFA_UNLOCK(&ia
->ia_ifa
);
1820 lck_rw_done(&in6_ifaddr_rwlock
);
1821 in6_purgeaddr(&ia
->ia_ifa
);
1822 IFA_REMREF(&ia
->ia_ifa
); /* for us */
1823 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1825 * Purging the address caused in6_ifaddr_rwlock
1826 * to be dropped and reacquired;
1827 * therefore search again from the beginning
1828 * of in6_ifaddrs list.
1833 IFA_UNLOCK(&ia
->ia_ifa
);
1836 lck_rw_done(&in6_ifaddr_rwlock
);
1842 * Handle SIOCSETROUTERMODE_IN6 to set or clear the IPv6 router mode flag on
1843 * the interface. Entering or exiting this mode will result in the removal of
1844 * autoconfigured IPv6 addresses on the interface.
1846 static __attribute__((noinline
)) int
1847 in6_setrouter(struct ifnet
*ifp
, int enable
)
1849 VERIFY(ifp
!= NULL
);
1851 if (ifp
->if_flags
& IFF_LOOPBACK
)
1855 struct nd_ifinfo
*ndi
= NULL
;
1857 ndi
= ND_IFINFO(ifp
);
1858 if (ndi
!= NULL
&& ndi
->initialized
) {
1859 lck_mtx_lock(&ndi
->lock
);
1860 if (ndi
->flags
& ND6_IFF_PROXY_PREFIXES
) {
1861 /* No proxy if we are an advertising router */
1862 ndi
->flags
&= ~ND6_IFF_PROXY_PREFIXES
;
1863 lck_mtx_unlock(&ndi
->lock
);
1864 (void) nd6_if_prproxy(ifp
, FALSE
);
1866 lck_mtx_unlock(&ndi
->lock
);
1871 ifnet_lock_exclusive(ifp
);
1873 ifp
->if_eflags
|= IFEF_IPV6_ROUTER
;
1875 ifp
->if_eflags
&= ~IFEF_IPV6_ROUTER
;
1877 ifnet_lock_done(ifp
);
1879 lck_mtx_lock(nd6_mutex
);
1880 defrouter_select(ifp
);
1881 lck_mtx_unlock(nd6_mutex
);
1883 if_allmulti(ifp
, enable
);
1885 return (in6_autoconf(ifp
, FALSE
));
1889 in6_to_kamescope(struct sockaddr_in6
*sin6
, struct ifnet
*ifp
)
1891 struct sockaddr_in6 tmp
;
1894 VERIFY(sin6
!= NULL
);
1897 error
= in6_recoverscope(&tmp
, &sin6
->sin6_addr
, ifp
);
1901 id
= in6_addr2scopeid(ifp
, &tmp
.sin6_addr
);
1902 if (tmp
.sin6_scope_id
== 0)
1903 tmp
.sin6_scope_id
= id
;
1904 else if (tmp
.sin6_scope_id
!= id
)
1905 return (EINVAL
); /* scope ID mismatch. */
1907 error
= in6_embedscope(&tmp
.sin6_addr
, &tmp
, NULL
, NULL
, NULL
);
1911 tmp
.sin6_scope_id
= 0;
1917 * When the address is being configured we should clear out certain flags
1918 * coming in from the caller.
1920 #define IN6_IFF_CLR_ADDR_FLAG_MASK (~(IN6_IFF_DEPRECATED | IN6_IFF_DETACHED | IN6_IFF_DUPLICATED))
1923 in6_ifaupdate_aux(struct in6_ifaddr
*ia
, struct ifnet
*ifp
, int ifaupflags
)
1925 struct sockaddr_in6 mltaddr
, mltmask
;
1926 struct in6_addr llsol
;
1928 struct in6_multi
*in6m_sol
;
1929 struct in6_multi_mship
*imm
;
1931 int delay
, error
= 0;
1933 VERIFY(ifp
!= NULL
&& ia
!= NULL
);
1937 nd6log2((LOG_DEBUG
, "%s - %s ifp %s ia6_flags 0x%x ifaupflags 0x%x\n",
1939 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1940 if_name(ia
->ia_ifp
),
1945 * Just to be safe, always clear certain flags when address
1946 * is being configured
1948 ia
->ia6_flags
&= IN6_IFF_CLR_ADDR_FLAG_MASK
;
1951 * Mark the address as tentative before joining multicast addresses,
1952 * so that corresponding MLD responses would not have a tentative
1955 if (in6if_do_dad(ifp
)) {
1956 in6_ifaddr_set_dadprogress(ia
);
1958 * Do not delay sending neighbor solicitations when using optimistic
1959 * duplicate address detection, c.f. RFC 4429.
1961 if (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
)
1962 ifaupflags
&= ~IN6_IFAUPDATE_DADDELAY
;
1964 ifaupflags
|= IN6_IFAUPDATE_DADDELAY
;
1967 * If the interface has been marked to not perform
1968 * DAD, make sure to reset DAD in progress flags
1969 * that may come in from the caller.
1971 ia
->ia6_flags
&= ~IN6_IFF_DADPROGRESS
;
1974 /* Join necessary multicast groups */
1975 if ((ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1977 /* join solicited multicast addr for new host id */
1978 bzero(&llsol
, sizeof (struct in6_addr
));
1979 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
1980 llsol
.s6_addr32
[1] = 0;
1981 llsol
.s6_addr32
[2] = htonl(1);
1982 llsol
.s6_addr32
[3] = ia
->ia_addr
.sin6_addr
.s6_addr32
[3];
1983 llsol
.s6_addr8
[12] = 0xff;
1984 if ((error
= in6_setscope(&llsol
, ifp
, NULL
)) != 0) {
1985 /* XXX: should not happen */
1986 log(LOG_ERR
, "%s: in6_setscope failed\n", __func__
);
1990 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
)) {
1992 * We need a random delay for DAD on the address
1993 * being configured. It also means delaying
1994 * transmission of the corresponding MLD report to
1995 * avoid report collision. [RFC 4862]
1997 delay
= random() % MAX_RTR_SOLICITATION_DELAY
;
1999 imm
= in6_joingroup(ifp
, &llsol
, &error
, delay
);
2001 nd6log((LOG_WARNING
,
2002 "%s: addmulti failed for %s on %s (errno=%d)\n",
2003 __func__
, ip6_sprintf(&llsol
), if_name(ifp
),
2008 in6m_sol
= imm
->i6mm_maddr
;
2009 /* take a refcount for this routine */
2010 IN6M_ADDREF(in6m_sol
);
2013 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
2016 bzero(&mltmask
, sizeof (mltmask
));
2017 mltmask
.sin6_len
= sizeof (struct sockaddr_in6
);
2018 mltmask
.sin6_family
= AF_INET6
;
2019 mltmask
.sin6_addr
= in6mask32
;
2020 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
2023 * join link-local all-nodes address
2025 bzero(&mltaddr
, sizeof (mltaddr
));
2026 mltaddr
.sin6_len
= sizeof (struct sockaddr_in6
);
2027 mltaddr
.sin6_family
= AF_INET6
;
2028 mltaddr
.sin6_addr
= in6addr_linklocal_allnodes
;
2029 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
)) != 0)
2030 goto unwind
; /* XXX: should not fail */
2033 * XXX: do we really need this automatic routes?
2034 * We should probably reconsider this stuff. Most applications
2035 * actually do not need the routes, since they usually specify
2036 * the outgoing interface.
2038 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
2039 ia
->ia_ifp
->if_index
);
2041 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
2042 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN
)) {
2048 error
= rtrequest_scoped(RTM_ADD
,
2049 (struct sockaddr
*)&mltaddr
,
2050 (struct sockaddr
*)&ia
->ia_addr
,
2051 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
2052 NULL
, ia
->ia_ifp
->if_index
);
2059 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
2061 nd6log((LOG_WARNING
,
2062 "%s: addmulti failed for %s on %s (errno=%d)\n",
2063 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
2064 if_name(ifp
), error
));
2069 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
2073 * join node information group address
2075 #define hostnamelen strlen(hostname)
2077 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
)) {
2079 * The spec doesn't say anything about delay for this
2080 * group, but the same logic should apply.
2082 delay
= random() % MAX_RTR_SOLICITATION_DELAY
;
2084 if (in6_nigroup(ifp
, hostname
, hostnamelen
, &mltaddr
.sin6_addr
)
2086 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
,
2087 delay
); /* XXX jinmei */
2089 nd6log((LOG_WARNING
,
2090 "%s: addmulti failed for %s on %s "
2092 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
2093 if_name(ifp
), error
));
2094 /* XXX not very fatal, go on... */
2098 LIST_INSERT_HEAD(&ia
->ia6_memberships
,
2106 * join interface-local all-nodes address.
2107 * (ff01::1%ifN, and ff01::%ifN/32)
2109 mltaddr
.sin6_addr
= in6addr_nodelocal_allnodes
;
2110 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
)) != 0)
2111 goto unwind
; /* XXX: should not fail */
2112 /* XXX: again, do we really need the route? */
2113 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
2114 ia
->ia_ifp
->if_index
);
2116 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
2117 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN
)) {
2123 error
= rtrequest_scoped(RTM_ADD
,
2124 (struct sockaddr
*)&mltaddr
,
2125 (struct sockaddr
*)&ia
->ia_addr
,
2126 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
2127 NULL
, ia
->ia_ifp
->if_index
);
2133 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
2135 nd6log((LOG_WARNING
,
2136 "%s: addmulti failed for %s on %s (errno=%d)\n",
2137 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
2138 if_name(ifp
), error
));
2143 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
2148 /* Ensure nd6_service() is scheduled as soon as it's convenient */
2149 ++nd6_sched_timeout_want
;
2152 * Perform DAD, if needed.
2153 * XXX It may be of use, if we can administratively
2157 if (in6if_do_dad(ifp
) && ((ifa
->ifa_flags
& IN6_IFF_NODAD
) == 0) &&
2158 (ia
->ia6_flags
& IN6_IFF_DADPROGRESS
)) {
2159 int mindelay
, maxdelay
;
2160 int *delayptr
, delayval
;
2165 * Avoid the DAD delay if the caller wants us to skip it.
2166 * This is not compliant with RFC 2461, but it's only being
2167 * used for signalling and not for actual DAD.
2169 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
) &&
2170 !(ia
->ia6_flags
& IN6_IFF_SWIFTDAD
)) {
2172 * We need to impose a delay before sending an NS
2173 * for DAD. Check if we also needed a delay for the
2174 * corresponding MLD message. If we did, the delay
2175 * should be larger than the MLD delay (this could be
2176 * relaxed a bit, but this simple logic is at least
2180 if (in6m_sol
!= NULL
) {
2181 IN6M_LOCK(in6m_sol
);
2182 if (in6m_sol
->in6m_state
==
2183 MLD_REPORTING_MEMBER
)
2184 mindelay
= in6m_sol
->in6m_timer
;
2185 IN6M_UNLOCK(in6m_sol
);
2187 maxdelay
= MAX_RTR_SOLICITATION_DELAY
* hz
;
2188 if (maxdelay
- mindelay
== 0)
2192 (random() % (maxdelay
- mindelay
)) +
2195 delayptr
= &delayval
;
2198 nd6_dad_start((struct ifaddr
*)ia
, delayptr
);
2207 in6_purgeaddr(&ia
->ia_ifa
);
2210 /* release reference held for this routine */
2211 if (in6m_sol
!= NULL
)
2212 IN6M_REMREF(in6m_sol
);
2217 * Request an IPv6 interface address. If the address is new, then it will be
2218 * constructed and appended to the interface address chains. The interface
2219 * address structure is optionally returned with a reference for the caller.
2222 in6_update_ifa(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
, int ifaupflags
,
2223 struct in6_ifaddr
**iar
)
2225 struct in6_addrlifetime ia6_lt
;
2226 struct in6_ifaddr
*ia
;
2228 struct ifaddr
*xifa
;
2229 struct in6_addrlifetime
*lt
;
2233 /* Sanity check parameters and initialize locals */
2234 VERIFY(ifp
!= NULL
&& ifra
!= NULL
&& iar
!= NULL
);
2240 * We always require users to specify a valid IPv6 address for
2241 * the corresponding operation.
2243 if (ifra
->ifra_addr
.sin6_family
!= AF_INET6
||
2244 ifra
->ifra_addr
.sin6_len
!= sizeof (struct sockaddr_in6
)) {
2245 error
= EAFNOSUPPORT
;
2249 /* Validate ifra_prefixmask.sin6_len is properly bounded. */
2250 if (ifra
->ifra_prefixmask
.sin6_len
== 0 ||
2251 ifra
->ifra_prefixmask
.sin6_len
> sizeof (struct sockaddr_in6
)) {
2256 /* Validate prefix length extracted from ifra_prefixmask structure. */
2257 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
2258 (u_char
*)&ifra
->ifra_prefixmask
+ ifra
->ifra_prefixmask
.sin6_len
);
2264 /* Validate lifetimes */
2265 lt
= &ifra
->ifra_lifetime
;
2266 if (lt
->ia6t_pltime
> lt
->ia6t_vltime
) {
2268 "%s: pltime 0x%x > vltime 0x%x for %s\n", __func__
,
2269 lt
->ia6t_pltime
, lt
->ia6t_vltime
,
2270 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
2274 if (lt
->ia6t_vltime
== 0) {
2276 * the following log might be noisy, but this is a typical
2277 * configuration mistake or a tool's bug.
2279 log(LOG_INFO
, "%s: valid lifetime is 0 for %s\n", __func__
,
2280 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
2284 * Before we lock the ifnet structure, we first check to see if the
2285 * address already exists. If so, then we don't allocate and link a
2288 ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
);
2293 * Validate destination address on interface types that require it.
2295 if ((ifp
->if_flags
& (IFF_LOOPBACK
|IFF_POINTOPOINT
)) != 0) {
2296 switch (ifra
->ifra_dstaddr
.sin6_family
) {
2299 /* noisy message for diagnostic purposes */
2301 "%s: prefix length < 128 with"
2302 " explicit dstaddr.\n", __func__
);
2312 error
= EAFNOSUPPORT
;
2315 } else if (ifra
->ifra_dstaddr
.sin6_family
!= AF_UNSPEC
) {
2317 "%s: dstaddr valid only on p2p and loopback interfaces.\n",
2323 timenow
= net_uptime();
2328 /* Is this the first new IPv6 address for the interface? */
2329 ifaupflags
|= IN6_IFAUPDATE_NEWADDR
;
2331 /* Allocate memory for IPv6 interface address structure. */
2332 how
= !(ifaupflags
& IN6_IFAUPDATE_NOWAIT
) ? M_WAITOK
: 0;
2333 ia
= in6_ifaddr_alloc(how
);
2342 * Initialize interface address structure.
2344 * Note well: none of these sockaddr_in6 structures contain a
2345 * valid sin6_port, sin6_flowinfo or even a sin6_scope_id field.
2346 * We still embed link-local scope identifiers at the end of an
2347 * arbitrary fe80::/32 prefix, for historical reasons. Also, the
2348 * ifa_dstaddr field is always non-NULL on point-to-point and
2349 * loopback interfaces, and conventionally points to a socket
2350 * address of AF_UNSPEC family when there is no destination.
2352 * Please enjoy the dancing sea turtle.
2354 IFA_ADDREF(ifa
); /* for this and optionally for caller */
2355 ifa
->ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
2356 if (ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
||
2357 (ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) != 0)
2358 ifa
->ifa_dstaddr
= (struct sockaddr
*)&ia
->ia_dstaddr
;
2359 ifa
->ifa_netmask
= (struct sockaddr
*)&ia
->ia_prefixmask
;
2361 ifa
->ifa_metric
= ifp
->if_metric
;
2362 ifa
->ifa_rtrequest
= nd6_rtrequest
;
2364 LIST_INIT(&ia
->ia6_memberships
);
2365 ia
->ia_addr
.sin6_family
= AF_INET6
;
2366 ia
->ia_addr
.sin6_len
= sizeof (ia
->ia_addr
);
2367 ia
->ia_addr
.sin6_addr
= ifra
->ifra_addr
.sin6_addr
;
2368 ia
->ia_prefixmask
.sin6_family
= AF_INET6
;
2369 ia
->ia_prefixmask
.sin6_len
= sizeof (ia
->ia_prefixmask
);
2370 ia
->ia_prefixmask
.sin6_addr
= ifra
->ifra_prefixmask
.sin6_addr
;
2371 error
= in6_to_kamescope(&ia
->ia_addr
, ifp
);
2374 if (ifa
->ifa_dstaddr
!= NULL
) {
2375 ia
->ia_dstaddr
= ifra
->ifra_dstaddr
;
2376 error
= in6_to_kamescope(&ia
->ia_dstaddr
, ifp
);
2381 /* Append to address chains */
2382 ifnet_lock_exclusive(ifp
);
2383 ifaupflags
|= IN6_IFAUPDATE_1STADDR
;
2384 TAILQ_FOREACH(xifa
, &ifp
->if_addrlist
, ifa_list
) {
2385 IFA_LOCK_SPIN(xifa
);
2386 if (xifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2388 ifaupflags
&= ~IN6_IFAUPDATE_1STADDR
;
2395 if_attach_ifa(ifp
, ifa
); /* holds reference for ifnet link */
2397 ifnet_lock_done(ifp
);
2399 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2400 if (in6_ifaddrs
!= NULL
) {
2401 struct in6_ifaddr
*iac
;
2402 for (iac
= in6_ifaddrs
; iac
->ia_next
!= NULL
;
2409 IFA_ADDREF(ifa
); /* hold for in6_ifaddrs link */
2410 lck_rw_done(&in6_ifaddr_rwlock
);
2413 ifaupflags
&= ~(IN6_IFAUPDATE_NEWADDR
|IN6_IFAUPDATE_1STADDR
);
2416 VERIFY(ia
!= NULL
&& ifa
== &ia
->ia_ifa
);
2420 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
2421 * to see if the address is deprecated or invalidated, but initialize
2422 * these members for applications.
2424 ia
->ia6_updatetime
= ia
->ia6_createtime
= timenow
;
2426 if (ia6_lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
)
2427 ia6_lt
.ia6t_expire
= timenow
+ ia6_lt
.ia6t_vltime
;
2429 ia6_lt
.ia6t_expire
= 0;
2430 if (ia6_lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
)
2431 ia6_lt
.ia6t_preferred
= timenow
+ ia6_lt
.ia6t_pltime
;
2433 ia6_lt
.ia6t_preferred
= 0;
2434 in6ifa_setlifetime(ia
, &ia6_lt
);
2437 * Backward compatibility - if IN6_IFF_DEPRECATED is set from the
2438 * userland, make it deprecated.
2440 if ((ia
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0) {
2441 ia
->ia6_lifetime
.ia6ti_pltime
= 0;
2442 ia
->ia6_lifetime
.ia6ti_preferred
= timenow
;
2446 * Update flag or prefix length
2449 ia
->ia6_flags
= ifra
->ifra_flags
;
2451 /* Release locks (new address available to concurrent tasks) */
2454 /* Further initialization of the interface address */
2455 error
= in6_ifinit(ifp
, ia
, ifaupflags
);
2459 /* Finish updating the address while other tasks are working with it */
2460 error
= in6_ifaupdate_aux(ia
, ifp
, ifaupflags
);
2464 /* Return success (optionally w/ address for caller). */
2466 (void) ifnet_notify_address(ifp
, AF_INET6
);
2472 VERIFY(ifa
== &ia
->ia_ifa
);
2483 in6_purgeaddr(struct ifaddr
*ifa
)
2485 struct ifnet
*ifp
= ifa
->ifa_ifp
;
2486 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
2487 struct in6_multi_mship
*imm
;
2489 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2491 /* stop DAD processing */
2495 * delete route to the destination of the address being purged.
2496 * The interface must be p2p or loopback in this case.
2499 if ((ia
->ia_flags
& IFA_ROUTE
) && ia
->ia_plen
== 128) {
2503 rtf
= (ia
->ia_dstaddr
.sin6_family
== AF_INET6
) ? RTF_HOST
: 0;
2504 error
= rtinit(&(ia
->ia_ifa
), RTM_DELETE
, rtf
);
2506 log(LOG_ERR
, "in6_purgeaddr: failed to remove "
2507 "a route to the p2p destination: %s on %s, "
2509 ip6_sprintf(&ia
->ia_addr
.sin6_addr
), if_name(ifp
),
2511 /* proceed anyway... */
2514 ia
->ia_flags
&= ~IFA_ROUTE
;
2518 /* Remove ownaddr's loopback rtentry, if it exists. */
2519 in6_ifremloop(&(ia
->ia_ifa
));
2522 * leave from multicast groups we have joined for the interface
2525 while ((imm
= ia
->ia6_memberships
.lh_first
) != NULL
) {
2526 LIST_REMOVE(imm
, i6mm_chain
);
2528 in6_leavegroup(imm
);
2533 /* in6_unlink_ifa() will need exclusive access */
2534 in6_unlink_ifa(ia
, ifp
);
2535 in6_post_msg(ifp
, KEV_INET6_ADDR_DELETED
, ia
, NULL
);
2537 (void) ifnet_notify_address(ifp
, AF_INET6
);
2541 in6_unlink_ifa(struct in6_ifaddr
*ia
, struct ifnet
*ifp
)
2543 struct in6_ifaddr
*oia
;
2547 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2552 ifnet_lock_exclusive(ifp
);
2554 if (ifa
->ifa_debug
& IFD_ATTACHED
)
2555 if_detach_ifa(ifp
, ifa
);
2557 ifnet_lock_done(ifp
);
2560 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2562 if (oia
== (ia
= in6_ifaddrs
)) {
2563 in6_ifaddrs
= ia
->ia_next
;
2565 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
2568 ia
->ia_next
= oia
->ia_next
;
2571 log(LOG_NOTICE
, "%s: search failed.\n", __func__
);
2577 * When IPv6 address is being removed, release the
2578 * reference to the base prefix.
2579 * Also, since the release might, affect the status
2580 * of other (detached) addresses, call
2581 * pfxlist_onlink_check().
2586 * Only log the below message for addresses other than
2588 * Only one LLA (auto-configured or statically) is allowed
2590 * LLA prefix, while added to the prefix list, is not
2591 * reference countedi (as it is the only one).
2592 * The prefix also never expires on its own as LLAs
2593 * have infinite lifetime.
2595 * For now quiece down the log message for LLAs.
2597 if (!IN6_IS_ADDR_LINKLOCAL(&oia
->ia_addr
.sin6_addr
)) {
2598 if (oia
->ia6_ndpr
== NULL
)
2599 log(LOG_NOTICE
, "in6_unlink_ifa: IPv6 address "
2600 "0x%llx has no prefix\n",
2601 (uint64_t)VM_KERNEL_ADDRPERM(oia
));
2603 struct nd_prefix
*pr
= oia
->ia6_ndpr
;
2604 oia
->ia6_flags
&= ~IN6_IFF_AUTOCONF
;
2605 oia
->ia6_ndpr
= NULL
;
2607 VERIFY(pr
->ndpr_addrcnt
!= 0);
2609 if (oia
->ia6_flags
& IN6_IFF_CLAT46
)
2610 pr
->ndpr_stateflags
&= ~NDPRF_CLAT46
;
2612 NDPR_REMREF(pr
); /* release addr reference */
2616 lck_rw_done(&in6_ifaddr_rwlock
);
2618 if ((oia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0) {
2619 lck_mtx_lock(nd6_mutex
);
2620 pfxlist_onlink_check();
2621 lck_mtx_unlock(nd6_mutex
);
2624 * release another refcnt for the link from in6_ifaddrs.
2625 * Do this only if it's not already unlinked in the event that we lost
2626 * the race, since in6_ifaddr_rwlock was momentarily dropped above.
2631 /* release reference held for this routine */
2634 /* invalidate route caches */
2635 routegenid_inet6_update();
2639 in6_purgeif(struct ifnet
*ifp
)
2641 struct in6_ifaddr
*ia
;
2646 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2648 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2650 while (ia
!= NULL
) {
2651 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
2655 IFA_ADDREF(&ia
->ia_ifa
); /* for us */
2656 lck_rw_done(&in6_ifaddr_rwlock
);
2657 in6_purgeaddr(&ia
->ia_ifa
);
2658 IFA_REMREF(&ia
->ia_ifa
); /* for us */
2659 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2661 * Purging the address would have caused
2662 * in6_ifaddr_rwlock to be dropped and reacquired;
2663 * therefore search again from the beginning
2664 * of in6_ifaddrs list.
2668 lck_rw_done(&in6_ifaddr_rwlock
);
2674 * Initialize an interface's internet6 address and routing table entry.
2677 in6_ifinit(struct ifnet
*ifp
, struct in6_ifaddr
*ia
, int ifaupflags
)
2686 * NOTE: SIOCSIFADDR is defined with struct ifreq as parameter,
2687 * but here we are sending it down to the interface with a pointer
2688 * to struct ifaddr, for legacy reasons.
2690 if ((ifaupflags
& IN6_IFAUPDATE_1STADDR
) != 0) {
2691 error
= ifnet_ioctl(ifp
, PF_INET6
, SIOCSIFADDR
, ia
);
2693 if (error
!= EOPNOTSUPP
)
2703 * If the destination address is specified for a point-to-point
2704 * interface, install a route to the destination as an interface
2707 if (!(ia
->ia_flags
& IFA_ROUTE
) && ia
->ia_plen
== 128 &&
2708 ia
->ia_dstaddr
.sin6_family
== AF_INET6
) {
2710 error
= rtinit(ifa
, RTM_ADD
, RTF_UP
| RTF_HOST
);
2714 ia
->ia_flags
|= IFA_ROUTE
;
2716 IFA_LOCK_ASSERT_HELD(ifa
);
2717 if (ia
->ia_plen
< 128) {
2719 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
2721 ia
->ia_flags
|= RTF_CLONING
;
2726 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
2727 if ((ifaupflags
& IN6_IFAUPDATE_NEWADDR
) != 0)
2730 /* invalidate route caches */
2731 routegenid_inet6_update();
2738 in6_purgeaddrs(struct ifnet
*ifp
)
2744 * Find an IPv6 interface link-local address specific to an interface.
2747 in6ifa_ifpforlinklocal(struct ifnet
*ifp
, int ignoreflags
)
2751 ifnet_lock_shared(ifp
);
2752 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2755 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2759 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
))) {
2760 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2761 ignoreflags
) != 0) {
2765 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2771 ifnet_lock_done(ifp
);
2773 return ((struct in6_ifaddr
*)ifa
);
2777 in6ifa_ifpwithflag(struct ifnet
* ifp
, int flag
)
2781 ifnet_lock_shared(ifp
);
2782 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2785 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2789 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
& flag
) == flag
) {
2790 IFA_ADDREF_LOCKED(ifa
);
2796 ifnet_lock_done(ifp
);
2798 return ((struct in6_ifaddr
*)ifa
);
2802 * find the internet address corresponding to a given interface and address.
2805 in6ifa_ifpwithaddr(struct ifnet
*ifp
, struct in6_addr
*addr
)
2809 ifnet_lock_shared(ifp
);
2810 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2813 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2817 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(ifa
))) {
2818 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2824 ifnet_lock_done(ifp
);
2826 return ((struct in6_ifaddr
*)ifa
);
2830 in6ifa_prproxyaddr(struct in6_addr
*addr
)
2832 struct in6_ifaddr
*ia
;
2834 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2835 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2836 IFA_LOCK(&ia
->ia_ifa
);
2837 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(&ia
->ia_ifa
))) {
2838 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
2839 IFA_UNLOCK(&ia
->ia_ifa
);
2842 IFA_UNLOCK(&ia
->ia_ifa
);
2844 lck_rw_done(&in6_ifaddr_rwlock
);
2846 if (ia
!= NULL
&& !nd6_prproxy_ifaddr(ia
)) {
2847 IFA_REMREF(&ia
->ia_ifa
);
2855 in6ifa_getlifetime(struct in6_ifaddr
*ia6
, struct in6_addrlifetime
*t_dst
,
2858 struct in6_addrlifetime_i
*t_src
= &ia6
->ia6_lifetime
;
2859 struct timeval caltime
;
2861 t_dst
->ia6t_vltime
= t_src
->ia6ti_vltime
;
2862 t_dst
->ia6t_pltime
= t_src
->ia6ti_pltime
;
2863 t_dst
->ia6t_expire
= 0;
2864 t_dst
->ia6t_preferred
= 0;
2866 /* account for system time change */
2867 getmicrotime(&caltime
);
2868 t_src
->ia6ti_base_calendartime
+=
2869 NET_CALCULATE_CLOCKSKEW(caltime
,
2870 t_src
->ia6ti_base_calendartime
, net_uptime(),
2871 t_src
->ia6ti_base_uptime
);
2874 if (t_src
->ia6ti_expire
!= 0 &&
2875 t_src
->ia6ti_vltime
!= ND6_INFINITE_LIFETIME
)
2876 t_dst
->ia6t_expire
= t_src
->ia6ti_base_calendartime
+
2877 t_src
->ia6ti_expire
- t_src
->ia6ti_base_uptime
;
2879 if (t_src
->ia6ti_preferred
!= 0 &&
2880 t_src
->ia6ti_pltime
!= ND6_INFINITE_LIFETIME
)
2881 t_dst
->ia6t_preferred
= t_src
->ia6ti_base_calendartime
+
2882 t_src
->ia6ti_preferred
- t_src
->ia6ti_base_uptime
;
2884 if (t_src
->ia6ti_expire
!= 0 &&
2885 t_src
->ia6ti_vltime
!= ND6_INFINITE_LIFETIME
)
2886 t_dst
->ia6t_expire
= t_src
->ia6ti_expire
;
2888 if (t_src
->ia6ti_preferred
!= 0 &&
2889 t_src
->ia6ti_pltime
!= ND6_INFINITE_LIFETIME
)
2890 t_dst
->ia6t_preferred
= t_src
->ia6ti_preferred
;
2895 in6ifa_setlifetime(struct in6_ifaddr
*ia6
, struct in6_addrlifetime
*t_src
)
2897 struct in6_addrlifetime_i
*t_dst
= &ia6
->ia6_lifetime
;
2898 struct timeval caltime
;
2900 /* account for system time change */
2901 getmicrotime(&caltime
);
2902 t_dst
->ia6ti_base_calendartime
+=
2903 NET_CALCULATE_CLOCKSKEW(caltime
,
2904 t_dst
->ia6ti_base_calendartime
, net_uptime(),
2905 t_dst
->ia6ti_base_uptime
);
2907 /* trust the caller for the values */
2908 t_dst
->ia6ti_expire
= t_src
->ia6t_expire
;
2909 t_dst
->ia6ti_preferred
= t_src
->ia6t_preferred
;
2910 t_dst
->ia6ti_vltime
= t_src
->ia6t_vltime
;
2911 t_dst
->ia6ti_pltime
= t_src
->ia6t_pltime
;
2915 * Convert IP6 address to printable (loggable) representation.
2918 ip6_sprintf(const struct in6_addr
*addr
)
2920 static const char digits
[] = "0123456789abcdef";
2921 static int ip6round
= 0;
2922 static char ip6buf
[8][48];
2926 const u_short
*a
= (const u_short
*)addr
;
2932 ip6round
= (ip6round
+ 1) & 7;
2933 cp
= ip6buf
[ip6round
];
2935 for (i
= 0; i
< 8; i
++) {
2946 if (dcolon
== 0 && *(a
+ 1) == 0) {
2958 d
= (const u_char
*)a
;
2960 if ((n
= *d
>> 4) != 0) {
2964 if ((n
= *d
++ & 0xf) != 0 || zpad
) {
2968 if ((n
= *d
>> 4) != 0 || zpad
) {
2972 if ((n
= *d
& 0xf) != 0 || zpad
)
2978 return (ip6buf
[ip6round
]);
2982 in6addr_local(struct in6_addr
*in6
)
2985 struct sockaddr_in6 sin6
;
2988 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_SCOPE_LINKLOCAL(in6
))
2991 sin6
.sin6_family
= AF_INET6
;
2992 sin6
.sin6_len
= sizeof (sin6
);
2993 bcopy(in6
, &sin6
.sin6_addr
, sizeof (*in6
));
2994 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0);
2998 if (rt
->rt_gateway
->sa_family
== AF_LINK
)
3003 local
= in6_localaddr(in6
);
3009 in6_localaddr(struct in6_addr
*in6
)
3011 struct in6_ifaddr
*ia
;
3013 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
))
3016 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
3017 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
3018 IFA_LOCK_SPIN(&ia
->ia_ifa
);
3019 if (IN6_ARE_MASKED_ADDR_EQUAL(in6
, &ia
->ia_addr
.sin6_addr
,
3020 &ia
->ia_prefixmask
.sin6_addr
)) {
3021 IFA_UNLOCK(&ia
->ia_ifa
);
3022 lck_rw_done(&in6_ifaddr_rwlock
);
3025 IFA_UNLOCK(&ia
->ia_ifa
);
3027 lck_rw_done(&in6_ifaddr_rwlock
);
3032 * return length of part which dst and src are equal
3036 in6_matchlen(struct in6_addr
*src
, struct in6_addr
*dst
)
3039 u_char
*s
= (u_char
*)src
, *d
= (u_char
*)dst
;
3040 u_char
*lim
= s
+ 16, r
;
3043 if ((r
= (*d
++ ^ *s
++)) != 0) {
3054 /* XXX: to be scope conscious */
3056 in6_are_prefix_equal(struct in6_addr
*p1
, struct in6_addr
*p2
, int len
)
3058 int bytelen
, bitlen
;
3061 if (0 > len
|| len
> 128) {
3062 log(LOG_ERR
, "%s: invalid prefix length(%d)\n", __func__
, len
);
3069 if (bcmp(&p1
->s6_addr
, &p2
->s6_addr
, bytelen
))
3072 p1
->s6_addr
[bytelen
] >> (8 - bitlen
) !=
3073 p2
->s6_addr
[bytelen
] >> (8 - bitlen
))
3080 in6_prefixlen2mask(struct in6_addr
*maskp
, int len
)
3082 u_char maskarray
[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
3083 int bytelen
, bitlen
, i
;
3086 if (0 > len
|| len
> 128) {
3087 log(LOG_ERR
, "%s: invalid prefix length(%d)\n", __func__
, len
);
3091 bzero(maskp
, sizeof (*maskp
));
3094 for (i
= 0; i
< bytelen
; i
++)
3095 maskp
->s6_addr
[i
] = 0xff;
3097 maskp
->s6_addr
[bytelen
] = maskarray
[bitlen
- 1];
3101 * return the best address out of the same scope
3104 in6_ifawithscope(struct ifnet
*oifp
, struct in6_addr
*dst
)
3106 int dst_scope
= in6_addrscope(dst
), src_scope
, best_scope
= 0;
3110 struct in6_ifaddr
*ifa_best
= NULL
;
3117 * We search for all addresses on all interfaces from the beginning.
3118 * Comparing an interface with the outgoing interface will be done
3119 * only at the final stage of tiebreaking.
3121 ifnet_head_lock_shared();
3122 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
3124 * We can never take an address that breaks the scope zone
3125 * of the destination.
3127 if (in6_addr2scopeid(ifp
, dst
) != in6_addr2scopeid(oifp
, dst
))
3130 ifnet_lock_shared(ifp
);
3131 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3132 int tlen
= -1, dscopecmp
, bscopecmp
, matchcmp
;
3135 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3139 src_scope
= in6_addrscope(IFA_IN6(ifa
));
3142 * Don't use an address before completing DAD
3143 * nor a duplicated address.
3145 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3146 (IN6_IFF_NOTREADY
| IN6_IFF_CLAT46
)) {
3150 /* XXX: is there any case to allow anycasts? */
3151 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3156 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3162 * If this is the first address we find,
3165 if (ifa_best
== NULL
)
3169 * ifa_best is never NULL beyond this line except
3170 * within the block labeled "replace".
3174 * If ifa_best has a smaller scope than dst and
3175 * the current address has a larger one than
3176 * (or equal to) dst, always replace ifa_best.
3177 * Also, if the current address has a smaller scope
3178 * than dst, ignore it unless ifa_best also has a
3180 * Consequently, after the two if-clause below,
3181 * the followings must be satisfied:
3182 * (scope(src) < scope(dst) &&
3183 * scope(best) < scope(dst))
3185 * (scope(best) >= scope(dst) &&
3186 * scope(src) >= scope(dst))
3188 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0 &&
3189 IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) >= 0)
3190 goto replace
; /* (A) */
3191 if (IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) < 0 &&
3192 IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) >= 0) {
3197 * A deprecated address SHOULD NOT be used in new
3198 * communications if an alternate (non-deprecated)
3199 * address is available and has sufficient scope.
3200 * RFC 4862, Section 5.5.4.
3202 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3203 IN6_IFF_DEPRECATED
) {
3205 * Ignore any deprecated addresses if
3206 * specified by configuration.
3208 if (!ip6_use_deprecated
) {
3213 * If we have already found a non-deprecated
3214 * candidate, just ignore deprecated addresses.
3216 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
)
3224 * A non-deprecated address is always preferred
3225 * to a deprecated one regardless of scopes and
3226 * address matching (Note invariants ensured by the
3227 * conditions (A) and (B) above.)
3229 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) &&
3230 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3231 IN6_IFF_DEPRECATED
) == 0)
3235 * When we use temporary addresses described in
3236 * RFC 4941, we prefer temporary addresses to
3237 * public autoconf addresses. Again, note the
3238 * invariants from (A) and (B). Also note that we
3239 * don't have any preference between static addresses
3240 * and autoconf addresses (despite of whether or not
3241 * the latter is temporary or public.)
3243 if (ip6_use_tempaddr
) {
3244 struct in6_ifaddr
*ifat
;
3246 ifat
= (struct in6_ifaddr
*)ifa
;
3247 if ((ifa_best
->ia6_flags
&
3248 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3249 == IN6_IFF_AUTOCONF
&&
3251 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3252 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
)) {
3255 if ((ifa_best
->ia6_flags
&
3256 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3257 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
) &&
3259 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3260 == IN6_IFF_AUTOCONF
) {
3267 * At this point, we have two cases:
3268 * 1. we are looking at a non-deprecated address,
3269 * and ifa_best is also non-deprecated.
3270 * 2. we are looking at a deprecated address,
3271 * and ifa_best is also deprecated.
3272 * Also, we do not have to consider a case where
3273 * the scope of if_best is larger(smaller) than dst and
3274 * the scope of the current address is smaller(larger)
3275 * than dst. Such a case has already been covered.
3276 * Tiebreaking is done according to the following
3278 * - the scope comparison between the address and
3280 * - the scope comparison between the address and
3281 * ifa_best (bscopecmp)
3282 * - if the address match dst longer than ifa_best
3284 * - if the address is on the outgoing I/F (outI/F)
3286 * Roughly speaking, the selection policy is
3287 * - the most important item is scope. The same scope
3288 * is best. Then search for a larger scope.
3289 * Smaller scopes are the last resort.
3290 * - A deprecated address is chosen only when we have
3291 * no address that has an enough scope, but is
3292 * prefered to any addresses of smaller scopes
3293 * (this must be already done above.)
3294 * - addresses on the outgoing I/F are preferred to
3295 * ones on other interfaces if none of above
3296 * tiebreaks. In the table below, the column "bI"
3297 * means if the best_ifa is on the outgoing
3298 * interface, and the column "sI" means if the ifa
3299 * is on the outgoing interface.
3300 * - If there is no other reasons to choose one,
3301 * longest address match against dst is considered.
3303 * The precise decision table is as follows:
3304 * dscopecmp bscopecmp match bI oI | replace?
3305 * N/A equal N/A Y N | No (1)
3306 * N/A equal N/A N Y | Yes (2)
3307 * N/A equal larger N/A | Yes (3)
3308 * N/A equal !larger N/A | No (4)
3309 * larger larger N/A N/A | No (5)
3310 * larger smaller N/A N/A | Yes (6)
3311 * smaller larger N/A N/A | Yes (7)
3312 * smaller smaller N/A N/A | No (8)
3313 * equal smaller N/A N/A | Yes (9)
3314 * equal larger (already done at A above)
3316 dscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
);
3317 bscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, best_scope
);
3319 if (bscopecmp
== 0) {
3320 struct ifnet
*bifp
= ifa_best
->ia_ifp
;
3322 if (bifp
== oifp
&& ifp
!= oifp
) { /* (1) */
3326 if (bifp
!= oifp
&& ifp
== oifp
) /* (2) */
3330 * Both bifp and ifp are on the outgoing
3331 * interface, or both two are on a different
3332 * interface from the outgoing I/F.
3333 * now we need address matching against dst
3336 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3337 matchcmp
= tlen
- blen
;
3338 if (matchcmp
> 0) /* (3) */
3343 if (dscopecmp
> 0) {
3344 if (bscopecmp
> 0) { /* (5) */
3348 goto replace
; /* (6) */
3350 if (dscopecmp
< 0) {
3351 if (bscopecmp
> 0) /* (7) */
3357 /* now dscopecmp must be 0 */
3359 goto replace
; /* (9) */
3362 IFA_ADDREF_LOCKED(ifa
); /* for ifa_best */
3363 blen
= tlen
>= 0 ? tlen
:
3364 in6_matchlen(IFA_IN6(ifa
), dst
);
3366 in6_addrscope(&ifa2ia6(ifa
)->ia_addr
.sin6_addr
);
3369 IFA_REMREF(&ifa_best
->ia_ifa
);
3370 ifa_best
= (struct in6_ifaddr
*)ifa
;
3372 ifnet_lock_done(ifp
);
3376 /* count statistics for future improvements */
3377 if (ifa_best
== NULL
)
3378 ip6stat
.ip6s_sources_none
++;
3380 IFA_LOCK_SPIN(&ifa_best
->ia_ifa
);
3381 if (oifp
== ifa_best
->ia_ifp
)
3382 ip6stat
.ip6s_sources_sameif
[best_scope
]++;
3384 ip6stat
.ip6s_sources_otherif
[best_scope
]++;
3386 if (best_scope
== dst_scope
)
3387 ip6stat
.ip6s_sources_samescope
[best_scope
]++;
3389 ip6stat
.ip6s_sources_otherscope
[best_scope
]++;
3391 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0)
3392 ip6stat
.ip6s_sources_deprecated
[best_scope
]++;
3393 IFA_UNLOCK(&ifa_best
->ia_ifa
);
3400 * return the best address out of the same scope. if no address was
3401 * found, return the first valid address from designated IF.
3404 in6_ifawithifp(struct ifnet
*ifp
, struct in6_addr
*dst
)
3406 int dst_scope
= in6_addrscope(dst
), blen
= -1, tlen
;
3408 struct in6_ifaddr
*besta
= NULL
;
3409 struct in6_ifaddr
*dep
[2]; /* last-resort: deprecated */
3411 dep
[0] = dep
[1] = NULL
;
3414 * We first look for addresses in the same scope.
3415 * If there is one, return it.
3416 * If two or more, return one which matches the dst longest.
3417 * If none, return one of global addresses assigned other ifs.
3419 ifnet_lock_shared(ifp
);
3420 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3422 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3426 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3428 continue; /* XXX: is there any case to allow anycast? */
3430 if (ifa2ia6(ifa
)->ia6_flags
& (IN6_IFF_NOTREADY
| IN6_IFF_CLAT46
)) {
3432 continue; /* don't use this interface */
3434 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3438 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3439 if (ip6_use_deprecated
) {
3440 IFA_ADDREF_LOCKED(ifa
); /* for dep[0] */
3443 IFA_REMREF(&dep
[0]->ia_ifa
);
3444 dep
[0] = (struct in6_ifaddr
*)ifa
;
3451 if (dst_scope
== in6_addrscope(IFA_IN6(ifa
))) {
3453 * call in6_matchlen() as few as possible
3458 IFA_LOCK(&besta
->ia_ifa
);
3459 blen
= in6_matchlen(
3460 &besta
->ia_addr
.sin6_addr
, dst
);
3461 IFA_UNLOCK(&besta
->ia_ifa
);
3464 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3467 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3469 IFA_REMREF(&besta
->ia_ifa
);
3470 besta
= (struct in6_ifaddr
*)ifa
;
3475 besta
= (struct in6_ifaddr
*)ifa
;
3476 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3484 ifnet_lock_done(ifp
);
3486 IFA_REMREF(&dep
[0]->ia_ifa
);
3490 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3492 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3496 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3498 continue; /* XXX: is there any case to allow anycast? */
3500 if (ifa2ia6(ifa
)->ia6_flags
& (IN6_IFF_NOTREADY
| IN6_IFF_CLAT46
)) {
3502 continue; /* don't use this interface */
3504 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3508 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3509 if (ip6_use_deprecated
) {
3510 IFA_ADDREF_LOCKED(ifa
); /* for dep[1] */
3513 IFA_REMREF(&dep
[1]->ia_ifa
);
3514 dep
[1] = (struct in6_ifaddr
*)ifa
;
3520 IFA_ADDREF_LOCKED(ifa
); /* for caller */
3522 ifnet_lock_done(ifp
);
3524 IFA_REMREF(&dep
[0]->ia_ifa
);
3526 IFA_REMREF(&dep
[1]->ia_ifa
);
3527 return ((struct in6_ifaddr
*)ifa
);
3529 ifnet_lock_done(ifp
);
3531 /* use the last-resort values, that are, deprecated addresses */
3534 IFA_REMREF(&dep
[1]->ia_ifa
);
3544 * perform DAD when interface becomes IFF_UP.
3547 in6_if_up_dad_start(struct ifnet
*ifp
)
3550 struct nd_ifinfo
*ndi
= NULL
;
3552 ndi
= ND_IFINFO(ifp
);
3553 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3554 if (!(ndi
->flags
& ND6_IFF_DAD
))
3557 /* start DAD on all the interface addresses */
3558 ifnet_lock_exclusive(ifp
);
3559 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3560 struct in6_ifaddr
*ia6
;
3563 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3567 ia6
= (struct in6_ifaddr
*)ifa
;
3568 if (ia6
->ia6_flags
& IN6_IFF_DADPROGRESS
) {
3569 int delay
= 0; /* delay ticks before DAD output */
3571 nd6_dad_start(ifa
, &delay
);
3576 ifnet_lock_done(ifp
);
3583 struct nd_ifinfo
*ndi
= NULL
;
3585 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0)
3588 ndi
= ND_IFINFO(ifp
);
3589 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3590 if (!(ndi
->flags
& ND6_IFF_DAD
))
3594 * If we are using the alternative neighbor discovery
3595 * interface on this interface, then skip DAD.
3597 * Also, skip it for interfaces marked "local private"
3598 * for now, even when not marked as using the alternative
3599 * interface. This is for historical reasons.
3601 if (ifp
->if_eflags
&
3602 (IFEF_IPV6_ND6ALT
|IFEF_LOCALNET_PRIVATE
|IFEF_DIRECTLINK
))
3605 if (ifp
->if_subfamily
== IFNET_SUBFAMILY_IPSEC
||
3606 ifp
->if_subfamily
== IFNET_SUBFAMILY_UTUN
) {
3608 * Ignore DAD for tunneling virtual interfaces, which get
3609 * their IPv6 address explicitly assigned.
3614 switch (ifp
->if_type
) {
3620 * These interfaces do not have the IFF_LOOPBACK flag,
3621 * but loop packets back. We do not have to do DAD on such
3622 * interfaces. We should even omit it, because loop-backed
3623 * NS would confuse the DAD procedure.
3628 * Our DAD routine requires the interface up and running.
3629 * However, some interfaces can be up before the RUNNING
3630 * status. Additionaly, users may try to assign addresses
3631 * before the interface becomes up (or running).
3632 * We simply skip DAD in such a case as a work around.
3633 * XXX: we should rather mark "tentative" on such addresses,
3634 * and do DAD after the interface becomes ready.
3636 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) !=
3637 (IFF_UP
|IFF_RUNNING
))
3645 * Calculate max IPv6 MTU through all the interfaces and store it
3651 u_int32_t maxmtu
= 0;
3654 ifnet_head_lock_shared();
3655 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
3656 struct nd_ifinfo
*ndi
= NULL
;
3658 if ((ndi
= ND_IFINFO(ifp
)) != NULL
&& !ndi
->initialized
)
3661 lck_mtx_lock(&ndi
->lock
);
3662 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
3663 IN6_LINKMTU(ifp
) > maxmtu
)
3664 maxmtu
= IN6_LINKMTU(ifp
);
3666 lck_mtx_unlock(&ndi
->lock
);
3669 if (maxmtu
) /* update only when maxmtu is positive */
3670 in6_maxmtu
= maxmtu
;
3673 * Provide the length of interface identifiers to be used for the link attached
3674 * to the given interface. The length should be defined in "IPv6 over
3675 * xxx-link" document. Note that address architecture might also define
3676 * the length for a particular set of address prefixes, regardless of the
3677 * link type. Also see RFC 4862 for additional background.
3680 in6_if2idlen(struct ifnet
*ifp
)
3682 switch (ifp
->if_type
) {
3683 case IFT_ETHER
: /* RFC2464 */
3684 case IFT_IEEE8023ADLAG
: /* IEEE802.3ad Link Aggregate */
3685 #ifdef IFT_PROPVIRTUAL
3686 case IFT_PROPVIRTUAL
: /* XXX: no RFC. treat it as ether */
3689 case IFT_L2VLAN
: /* ditto */
3691 #ifdef IFT_IEEE80211
3692 case IFT_IEEE80211
: /* ditto */
3695 case IFT_MIP
: /* ditto */
3698 case IFT_FDDI
: /* RFC2467 */
3700 case IFT_ISO88025
: /* RFC2470 (IPv6 over Token Ring) */
3702 case IFT_PPP
: /* RFC2472 */
3704 case IFT_ARCNET
: /* RFC2497 */
3706 case IFT_FRELAY
: /* RFC2590 */
3708 case IFT_IEEE1394
: /* RFC3146 */
3711 return (64); /* draft-ietf-v6ops-mech-v2-07 */
3713 return (64); /* XXX: is this really correct? */
3715 return (64); /* for utun interfaces */
3717 return (64); /* Packet Data over Cellular */
3719 return (64); /* Transparent bridge interface */
3722 * Unknown link type:
3723 * It might be controversial to use the today's common constant
3724 * of 64 for these cases unconditionally. For full compliance,
3725 * we should return an error in this case. On the other hand,
3726 * if we simply miss the standard for the link type or a new
3727 * standard is defined for a new link type, the IFID length
3728 * is very likely to be the common constant. As a compromise,
3729 * we always use the constant, but make an explicit notice
3730 * indicating the "unknown" case.
3732 log(LOG_NOTICE
, "%s: unknown link type (%d)\n", __func__
,
3738 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
3739 * v4 mapped addr or v4 compat addr
3742 in6_sin6_2_sin(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3744 bzero(sin
, sizeof (*sin
));
3745 sin
->sin_len
= sizeof (struct sockaddr_in
);
3746 sin
->sin_family
= AF_INET
;
3747 sin
->sin_port
= sin6
->sin6_port
;
3748 sin
->sin_addr
.s_addr
= sin6
->sin6_addr
.s6_addr32
[3];
3751 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
3753 in6_sin_2_v4mapsin6(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3755 bzero(sin6
, sizeof (*sin6
));
3756 sin6
->sin6_len
= sizeof (struct sockaddr_in6
);
3757 sin6
->sin6_family
= AF_INET6
;
3758 sin6
->sin6_port
= sin
->sin_port
;
3759 sin6
->sin6_addr
.s6_addr32
[0] = 0;
3760 sin6
->sin6_addr
.s6_addr32
[1] = 0;
3761 if (sin
->sin_addr
.s_addr
) {
3762 sin6
->sin6_addr
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
3763 sin6
->sin6_addr
.s6_addr32
[3] = sin
->sin_addr
.s_addr
;
3765 sin6
->sin6_addr
.s6_addr32
[2] = 0;
3766 sin6
->sin6_addr
.s6_addr32
[3] = 0;
3770 /* Convert sockaddr_in6 into sockaddr_in. */
3772 in6_sin6_2_sin_in_sock(struct sockaddr
*nam
)
3774 struct sockaddr_in
*sin_p
;
3775 struct sockaddr_in6 sin6
;
3778 * Save original sockaddr_in6 addr and convert it
3781 sin6
= *(struct sockaddr_in6
*)(void *)nam
;
3782 sin_p
= (struct sockaddr_in
*)(void *)nam
;
3783 in6_sin6_2_sin(sin_p
, &sin6
);
3786 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
3788 in6_sin_2_v4mapsin6_in_sock(struct sockaddr
**nam
)
3790 struct sockaddr_in
*sin_p
;
3791 struct sockaddr_in6
*sin6_p
;
3793 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof (*sin6_p
), M_SONAME
,
3797 sin_p
= (struct sockaddr_in
*)(void *)*nam
;
3798 in6_sin_2_v4mapsin6(sin_p
, sin6_p
);
3799 FREE(*nam
, M_SONAME
);
3800 *nam
= (struct sockaddr
*)sin6_p
;
3806 * Posts in6_event_data message kernel events.
3808 * To get the same size of kev_in6_data between ILP32 and LP64 data models
3809 * we are using a special version of the in6_addrlifetime structure that
3810 * uses only 32 bits fields to be compatible with Leopard, and that
3811 * are large enough to span 68 years.
3814 in6_post_msg(struct ifnet
*ifp
, u_int32_t event_code
, struct in6_ifaddr
*ifa
,
3817 struct kev_msg ev_msg
;
3818 struct kev_in6_data in6_event_data
;
3819 struct in6_addrlifetime ia6_lt
;
3821 bzero(&in6_event_data
, sizeof (struct kev_in6_data
));
3822 bzero(&ev_msg
, sizeof (struct kev_msg
));
3823 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3824 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3825 ev_msg
.kev_subclass
= KEV_INET6_SUBCLASS
;
3826 ev_msg
.event_code
= event_code
;
3829 IFA_LOCK(&ifa
->ia_ifa
);
3830 in6_event_data
.ia_addr
= ifa
->ia_addr
;
3831 in6_event_data
.ia_net
= ifa
->ia_net
;
3832 in6_event_data
.ia_dstaddr
= ifa
->ia_dstaddr
;
3833 in6_event_data
.ia_prefixmask
= ifa
->ia_prefixmask
;
3834 in6_event_data
.ia_plen
= ifa
->ia_plen
;
3835 in6_event_data
.ia6_flags
= (u_int32_t
)ifa
->ia6_flags
;
3837 /* retrieve time as calendar time (last arg is 1) */
3838 in6ifa_getlifetime(ifa
, &ia6_lt
, 1);
3839 in6_event_data
.ia_lifetime
.ia6t_expire
= ia6_lt
.ia6t_expire
;
3840 in6_event_data
.ia_lifetime
.ia6t_preferred
= ia6_lt
.ia6t_preferred
;
3841 in6_event_data
.ia_lifetime
.ia6t_vltime
= ia6_lt
.ia6t_vltime
;
3842 in6_event_data
.ia_lifetime
.ia6t_pltime
= ia6_lt
.ia6t_pltime
;
3843 IFA_UNLOCK(&ifa
->ia_ifa
);
3847 (void) strlcpy(&in6_event_data
.link_data
.if_name
[0],
3848 ifp
->if_name
, IFNAMSIZ
);
3849 in6_event_data
.link_data
.if_family
= ifp
->if_family
;
3850 in6_event_data
.link_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
3854 memcpy(&in6_event_data
.ia_mac
, mac
,
3855 sizeof(in6_event_data
.ia_mac
));
3857 ev_msg
.dv
[0].data_ptr
= &in6_event_data
;
3858 ev_msg
.dv
[0].data_length
= sizeof (in6_event_data
);
3859 ev_msg
.dv
[1].data_length
= 0;
3861 dlil_post_complete_msg(NULL
, &ev_msg
);
3865 * Called as part of ip6_init
3868 in6_ifaddr_init(void)
3873 PE_parse_boot_argn("ifa_debug", &in6ifa_debug
, sizeof (in6ifa_debug
));
3875 in6ifa_size
= (in6ifa_debug
== 0) ? sizeof (struct in6_ifaddr
) :
3876 sizeof (struct in6_ifaddr_dbg
);
3878 in6ifa_zone
= zinit(in6ifa_size
, IN6IFA_ZONE_MAX
* in6ifa_size
,
3879 0, IN6IFA_ZONE_NAME
);
3880 if (in6ifa_zone
== NULL
) {
3881 panic("%s: failed allocating %s", __func__
, IN6IFA_ZONE_NAME
);
3884 zone_change(in6ifa_zone
, Z_EXPAND
, TRUE
);
3885 zone_change(in6ifa_zone
, Z_CALLERACCT
, FALSE
);
3887 lck_mtx_init(&in6ifa_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3888 TAILQ_INIT(&in6ifa_trash_head
);
3891 static struct in6_ifaddr
*
3892 in6_ifaddr_alloc(int how
)
3894 struct in6_ifaddr
*in6ifa
;
3896 in6ifa
= (how
== M_WAITOK
) ? zalloc(in6ifa_zone
) :
3897 zalloc_noblock(in6ifa_zone
);
3898 if (in6ifa
!= NULL
) {
3899 bzero(in6ifa
, in6ifa_size
);
3900 in6ifa
->ia_ifa
.ifa_free
= in6_ifaddr_free
;
3901 in6ifa
->ia_ifa
.ifa_debug
|= IFD_ALLOC
;
3902 ifa_lock_init(&in6ifa
->ia_ifa
);
3903 if (in6ifa_debug
!= 0) {
3904 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3905 (struct in6_ifaddr_dbg
*)in6ifa
;
3906 in6ifa
->ia_ifa
.ifa_debug
|= IFD_DEBUG
;
3907 in6ifa
->ia_ifa
.ifa_trace
= in6_ifaddr_trace
;
3908 in6ifa
->ia_ifa
.ifa_attached
= in6_ifaddr_attached
;
3909 in6ifa
->ia_ifa
.ifa_detached
= in6_ifaddr_detached
;
3910 ctrace_record(&in6ifa_dbg
->in6ifa_alloc
);
3918 in6_ifaddr_free(struct ifaddr
*ifa
)
3920 IFA_LOCK_ASSERT_HELD(ifa
);
3922 if (ifa
->ifa_refcnt
!= 0) {
3923 panic("%s: ifa %p bad ref cnt", __func__
, ifa
);
3925 } else if (!(ifa
->ifa_debug
& IFD_ALLOC
)) {
3926 panic("%s: ifa %p cannot be freed", __func__
, ifa
);
3929 if (ifa
->ifa_debug
& IFD_DEBUG
) {
3930 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3931 (struct in6_ifaddr_dbg
*)ifa
;
3932 ctrace_record(&in6ifa_dbg
->in6ifa_free
);
3933 bcopy(&in6ifa_dbg
->in6ifa
, &in6ifa_dbg
->in6ifa_old
,
3934 sizeof (struct in6_ifaddr
));
3935 if (ifa
->ifa_debug
& IFD_TRASHED
) {
3936 /* Become a regular mutex, just in case */
3937 IFA_CONVERT_LOCK(ifa
);
3938 lck_mtx_lock(&in6ifa_trash_lock
);
3939 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
,
3941 lck_mtx_unlock(&in6ifa_trash_lock
);
3942 ifa
->ifa_debug
&= ~IFD_TRASHED
;
3946 ifa_lock_destroy(ifa
);
3947 bzero(ifa
, sizeof (struct in6_ifaddr
));
3948 zfree(in6ifa_zone
, ifa
);
3952 in6_ifaddr_attached(struct ifaddr
*ifa
)
3954 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3956 IFA_LOCK_ASSERT_HELD(ifa
);
3958 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3959 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3962 if (ifa
->ifa_debug
& IFD_TRASHED
) {
3963 /* Become a regular mutex, just in case */
3964 IFA_CONVERT_LOCK(ifa
);
3965 lck_mtx_lock(&in6ifa_trash_lock
);
3966 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
3967 lck_mtx_unlock(&in6ifa_trash_lock
);
3968 ifa
->ifa_debug
&= ~IFD_TRASHED
;
3973 in6_ifaddr_detached(struct ifaddr
*ifa
)
3975 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3977 IFA_LOCK_ASSERT_HELD(ifa
);
3979 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3980 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3982 } else if (ifa
->ifa_debug
& IFD_TRASHED
) {
3983 panic("%s: ifa %p is already in trash list", __func__
, ifa
);
3986 ifa
->ifa_debug
|= IFD_TRASHED
;
3987 /* Become a regular mutex, just in case */
3988 IFA_CONVERT_LOCK(ifa
);
3989 lck_mtx_lock(&in6ifa_trash_lock
);
3990 TAILQ_INSERT_TAIL(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
3991 lck_mtx_unlock(&in6ifa_trash_lock
);
3995 in6_ifaddr_trace(struct ifaddr
*ifa
, int refhold
)
3997 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
4002 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
4003 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
4007 cnt
= &in6ifa_dbg
->in6ifa_refhold_cnt
;
4008 tr
= in6ifa_dbg
->in6ifa_refhold
;
4010 cnt
= &in6ifa_dbg
->in6ifa_refrele_cnt
;
4011 tr
= in6ifa_dbg
->in6ifa_refrele
;
4014 idx
= atomic_add_16_ov(cnt
, 1) % IN6IFA_TRACE_HIST_SIZE
;
4015 ctrace_record(&tr
[idx
]);
4019 * Handle SIOCGASSOCIDS ioctl for PF_INET6 domain.
4022 in6_getassocids(struct socket
*so
, uint32_t *cnt
, user_addr_t aidp
)
4024 struct in6pcb
*in6p
= sotoin6pcb(so
);
4027 if (in6p
== NULL
|| in6p
->inp_state
== INPCB_STATE_DEAD
)
4030 /* IN6PCB has no concept of association */
4031 aid
= SAE_ASSOCID_ANY
;
4034 /* just asking how many there are? */
4035 if (aidp
== USER_ADDR_NULL
)
4038 return (copyout(&aid
, aidp
, sizeof (aid
)));
4042 * Handle SIOCGCONNIDS ioctl for PF_INET6 domain.
4045 in6_getconnids(struct socket
*so
, sae_associd_t aid
, uint32_t *cnt
,
4048 struct in6pcb
*in6p
= sotoin6pcb(so
);
4051 if (in6p
== NULL
|| in6p
->inp_state
== INPCB_STATE_DEAD
)
4054 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
)
4057 /* if connected, return 1 connection count */
4058 *cnt
= ((so
->so_state
& SS_ISCONNECTED
) ? 1 : 0);
4060 /* just asking how many there are? */
4061 if (cidp
== USER_ADDR_NULL
)
4064 /* if IN6PCB is connected, assign it connid 1 */
4065 cid
= ((*cnt
!= 0) ? 1 : SAE_CONNID_ANY
);
4067 return (copyout(&cid
, cidp
, sizeof (cid
)));
4071 * Handle SIOCGCONNINFO ioctl for PF_INET6 domain.
4074 in6_getconninfo(struct socket
*so
, sae_connid_t cid
, uint32_t *flags
,
4075 uint32_t *ifindex
, int32_t *soerror
, user_addr_t src
, socklen_t
*src_len
,
4076 user_addr_t dst
, socklen_t
*dst_len
, uint32_t *aux_type
,
4077 user_addr_t aux_data
, uint32_t *aux_len
)
4079 struct in6pcb
*in6p
= sotoin6pcb(so
);
4080 struct sockaddr_in6 sin6
;
4081 struct ifnet
*ifp
= NULL
;
4083 u_int32_t copy_len
= 0;
4086 * Don't test for INPCB_STATE_DEAD since this may be called
4087 * after SOF_PCBCLEARING is set, e.g. after tcp_close().
4094 if (cid
!= SAE_CONNID_ANY
&& cid
!= SAE_CONNID_ALL
&& cid
!= 1) {
4099 ifp
= in6p
->in6p_last_outifp
;
4100 *ifindex
= ((ifp
!= NULL
) ? ifp
->if_index
: 0);
4101 *soerror
= so
->so_error
;
4103 if (so
->so_state
& SS_ISCONNECTED
)
4104 *flags
|= (CIF_CONNECTED
| CIF_PREFERRED
);
4105 if (in6p
->in6p_flags
& INP_BOUND_IF
)
4106 *flags
|= CIF_BOUND_IF
;
4107 if (!(in6p
->in6p_flags
& INP_IN6ADDR_ANY
))
4108 *flags
|= CIF_BOUND_IP
;
4109 if (!(in6p
->in6p_flags
& INP_ANONPORT
))
4110 *flags
|= CIF_BOUND_PORT
;
4112 bzero(&sin6
, sizeof (sin6
));
4113 sin6
.sin6_len
= sizeof (sin6
);
4114 sin6
.sin6_family
= AF_INET6
;
4116 /* source address and port */
4117 sin6
.sin6_port
= in6p
->in6p_lport
;
4118 in6_recoverscope(&sin6
, &in6p
->in6p_laddr
, NULL
);
4119 if (*src_len
== 0) {
4120 *src_len
= sin6
.sin6_len
;
4122 if (src
!= USER_ADDR_NULL
) {
4123 copy_len
= min(*src_len
, sizeof (sin6
));
4124 error
= copyout(&sin6
, src
, copy_len
);
4127 *src_len
= copy_len
;
4131 /* destination address and port */
4132 sin6
.sin6_port
= in6p
->in6p_fport
;
4133 in6_recoverscope(&sin6
, &in6p
->in6p_faddr
, NULL
);
4134 if (*dst_len
== 0) {
4135 *dst_len
= sin6
.sin6_len
;
4137 if (dst
!= USER_ADDR_NULL
) {
4138 copy_len
= min(*dst_len
, sizeof (sin6
));
4139 error
= copyout(&sin6
, dst
, copy_len
);
4142 *dst_len
= copy_len
;
4146 if (SOCK_PROTO(so
) == IPPROTO_TCP
) {
4147 struct conninfo_tcp tcp_ci
;
4149 *aux_type
= CIAUX_TCP
;
4150 if (*aux_len
== 0) {
4151 *aux_len
= sizeof (tcp_ci
);
4153 if (aux_data
!= USER_ADDR_NULL
) {
4154 copy_len
= min(*aux_len
, sizeof (tcp_ci
));
4155 bzero(&tcp_ci
, sizeof (tcp_ci
));
4156 tcp_getconninfo(so
, &tcp_ci
);
4157 error
= copyout(&tcp_ci
, aux_data
, copy_len
);
4160 *aux_len
= copy_len
;
4175 * The switch statement below does nothing at runtime, as it serves as a
4176 * compile time check to ensure that all of the socket 'u' ioctls (those
4177 * in the 'u' group going thru soo_ioctl) that are made available by the
4178 * networking stack is unique. This works as long as this routine gets
4179 * updated each time a new interface ioctl gets added.
4181 * Any failures at compile time indicates duplicated ioctl values.
4183 static __attribute__((unused
)) void
4184 in6ioctl_cassert(void)
4187 * This is equivalent to _CASSERT() and the compiler wouldn't
4188 * generate any instructions, thus for compile time only.
4190 switch ((u_long
)0) {
4193 /* bsd/netinet6/in6_var.h */
4194 case SIOCAADDRCTL_POLICY
:
4195 case SIOCDADDRCTL_POLICY
:
4196 case SIOCDRADD_IN6_32
:
4197 case SIOCDRADD_IN6_64
:
4198 case SIOCDRDEL_IN6_32
:
4199 case SIOCDRDEL_IN6_64
:
4204 struct in6_llentry
{
4205 struct llentry base
;
4208 #define IN6_LLTBL_DEFAULT_HSIZE 32
4209 #define IN6_LLTBL_HASH(k, h) \
4210 ((((((((k) >> 8) ^ (k)) >> 8) ^ (k)) >> 8) ^ (k)) & ((h) - 1))
4213 * Do actual deallocation of @lle.
4216 in6_lltable_destroy_lle_unlocked(struct llentry
*lle
)
4218 LLE_LOCK_DESTROY(lle
);
4219 LLE_REQ_DESTROY(lle
);
4220 FREE(lle
, M_LLTABLE
);
4224 * Called by LLE_FREE_LOCKED when number of references
4228 in6_lltable_destroy_lle(struct llentry
*lle
)
4232 //thread_call_free(lle->lle_timer);
4233 in6_lltable_destroy_lle_unlocked(lle
);
4237 static struct llentry
*
4238 in6_lltable_new(const struct in6_addr
*addr6
, u_int flags
)
4240 #pragma unused(flags)
4241 struct in6_llentry
*lle
;
4243 MALLOC(lle
, struct in6_llentry
*, sizeof(struct in6_llentry
), M_LLTABLE
, M_NOWAIT
| M_ZERO
);
4244 if (lle
== NULL
) /* NB: caller generates msg */
4247 lle
->base
.r_l3addr
.addr6
= *addr6
;
4248 lle
->base
.lle_refcnt
= 1;
4249 lle
->base
.lle_free
= in6_lltable_destroy_lle
;
4250 LLE_LOCK_INIT(&lle
->base
);
4251 LLE_REQ_INIT(&lle
->base
);
4254 lle
->base
.lle_timer
= thread_call_allocate(nd6_llinfo_timer
, lle
);
4256 if (lle
->base
.lle_timer
== NULL
) {
4257 printf("lle_timer thread call could not be allocated.\n");
4258 LLE_LOCK_DESTROY(&lle
->base
);
4259 LLE_REQ_DESTROY(&lle
->base
);
4260 FREE(lle
, M_LLTABLE
);
4264 return (&lle
->base
);
4268 in6_lltable_match_prefix(const struct sockaddr
*saddr
,
4269 const struct sockaddr
*smask
, u_int flags
, struct llentry
*lle
)
4271 const struct in6_addr
*addr
, *mask
, *lle_addr
;
4273 addr
= &((const struct sockaddr_in6
*)(const void *)saddr
)->sin6_addr
;
4274 mask
= &((const struct sockaddr_in6
*)(const void *)smask
)->sin6_addr
;
4275 lle_addr
= &lle
->r_l3addr
.addr6
;
4277 if (IN6_ARE_MASKED_ADDR_EQUAL(lle_addr
, addr
, mask
) == 0)
4280 if (lle
->la_flags
& LLE_IFADDR
) {
4282 * Delete LLE_IFADDR records IFF address & flag matches.
4283 * Note that addr is the interface address within prefix
4286 if (IN6_ARE_ADDR_EQUAL(addr
, lle_addr
) &&
4287 (flags
& LLE_STATIC
) != 0)
4292 /* flags & LLE_STATIC means deleting both dynamic and static entries */
4293 if ((flags
& LLE_STATIC
) || !(lle
->la_flags
& LLE_STATIC
))
4300 in6_lltable_free_entry(struct lltable
*llt
, struct llentry
*lle
)
4304 LLE_WLOCK_ASSERT(lle
);
4305 KASSERT(llt
!= NULL
, ("lltable is NULL"));
4307 /* Unlink entry from table */
4308 if ((lle
->la_flags
& LLE_LINKED
) != 0) {
4310 if_afdata_wlock_assert(ifp
, llt
->llt_af
);
4311 lltable_unlink_entry(llt
, lle
);
4316 if (thread_call_cancel(lle
->lle_timer
) == TRUE
)
4323 in6_lltable_rtcheck(struct ifnet
*ifp
,
4324 u_int flags
, const struct sockaddr
*l3addr
)
4326 #pragma unused(flags)
4329 KASSERT(l3addr
->sa_family
== AF_INET6
,
4330 ("sin_family %d", l3addr
->sa_family
));
4331 /* XXX rtalloc1 should take a const param */
4332 rt
= rtalloc1(__DECONST(struct sockaddr
*, l3addr
), 0, 0);
4333 if (rt
== NULL
|| (rt
->rt_flags
& RTF_GATEWAY
) || rt
->rt_ifp
!= ifp
) {
4336 * Create an ND6 cache for an IPv6 neighbor
4337 * that is not covered by our own prefix.
4339 /* XXX ifaof_ifpforaddr should take a const param */
4340 ifa
= ifaof_ifpforaddr(__DECONST(struct sockaddr
*, l3addr
), ifp
);
4347 log(LOG_INFO
, "IPv6 address: \"%s\" is not on the network\n",
4348 ip6_sprintf(&((const struct sockaddr_in6
*)(const void *)l3addr
)->sin6_addr
));
4357 static inline uint32_t
4358 in6_lltable_hash_dst(const struct in6_addr
*dst
, uint32_t hsize
)
4360 return (IN6_LLTBL_HASH(dst
->s6_addr32
[3], hsize
));
4364 in6_lltable_hash(const struct llentry
*lle
, uint32_t hsize
)
4366 return (in6_lltable_hash_dst(&lle
->r_l3addr
.addr6
, hsize
));
4370 in6_lltable_fill_sa_entry(const struct llentry
*lle
, struct sockaddr
*sa
)
4372 struct sockaddr_in6
*sin6
;
4374 sin6
= (struct sockaddr_in6
*)(void *)sa
;
4375 bzero(sin6
, sizeof(*sin6
));
4376 sin6
->sin6_family
= AF_INET6
;
4377 sin6
->sin6_len
= sizeof(*sin6
);
4378 sin6
->sin6_addr
= lle
->r_l3addr
.addr6
;
4381 static inline struct llentry
*
4382 in6_lltable_find_dst(struct lltable
*llt
, const struct in6_addr
*dst
)
4384 struct llentry
*lle
;
4385 struct llentries
*lleh
;
4388 hashidx
= in6_lltable_hash_dst(dst
, llt
->llt_hsize
);
4389 lleh
= &llt
->lle_head
[hashidx
];
4390 LIST_FOREACH(lle
, lleh
, lle_next
) {
4391 if (lle
->la_flags
& LLE_DELETED
)
4393 if (IN6_ARE_ADDR_EQUAL(&lle
->r_l3addr
.addr6
, dst
))
4401 in6_lltable_delete_entry(struct lltable
*llt
, struct llentry
*lle
)
4404 lle
->la_flags
|= LLE_DELETED
;
4405 EVENTHANDLER_INVOKE(NULL
, lle_event
, lle
, LLENTRY_DELETED
);
4407 log(LOG_INFO
, "ifaddr cache = %p is deleted\n", lle
);
4412 static struct llentry
*
4413 in6_lltable_alloc(struct lltable
*llt
, u_int flags
,
4414 const struct sockaddr
*l3addr
)
4416 const struct sockaddr_in6
*sin6
= (const struct sockaddr_in6
*)(const void *)l3addr
;
4417 struct ifnet
*ifp
= llt
->llt_ifp
;
4418 struct llentry
*lle
;
4420 KASSERT(l3addr
->sa_family
== AF_INET6
,
4421 ("sin_family %d", l3addr
->sa_family
));
4424 * A route that covers the given address must have
4425 * been installed 1st because we are doing a resolution,
4428 if (!(flags
& LLE_IFADDR
) &&
4429 in6_lltable_rtcheck(ifp
, flags
, l3addr
) != 0)
4432 lle
= in6_lltable_new(&sin6
->sin6_addr
, flags
);
4434 log(LOG_INFO
, "lla_lookup: new lle malloc failed\n");
4437 lle
->la_flags
= flags
;
4438 if ((flags
& LLE_IFADDR
) == LLE_IFADDR
) {
4439 lltable_set_entry_addr(ifp
, lle
, LLADDR(SDL(ifp
->if_lladdr
->ifa_addr
)));
4440 lle
->la_flags
|= LLE_STATIC
;
4443 if ((lle
->la_flags
& LLE_STATIC
) != 0)
4444 lle
->ln_state
= ND6_LLINFO_REACHABLE
;
4449 static struct llentry
*
4450 in6_lltable_lookup(struct lltable
*llt
, u_int flags
,
4451 const struct sockaddr
*l3addr
)
4453 const struct sockaddr_in6
*sin6
= (const struct sockaddr_in6
*)(const void *)l3addr
;
4454 struct llentry
*lle
;
4456 IF_AFDATA_LOCK_ASSERT(llt
->llt_ifp
, llt
->llt_af
);
4457 KASSERT(l3addr
->sa_family
== AF_INET6
,
4458 ("sin_family %d", l3addr
->sa_family
));
4460 lle
= in6_lltable_find_dst(llt
, &sin6
->sin6_addr
);
4465 KASSERT((flags
& (LLE_UNLOCKED
|LLE_EXCLUSIVE
)) !=
4466 (LLE_UNLOCKED
|LLE_EXCLUSIVE
),("wrong lle request flags: 0x%X",
4469 if (flags
& LLE_UNLOCKED
)
4472 if (flags
& LLE_EXCLUSIVE
)
4480 in6_lltable_dump_entry(struct lltable
*llt
, struct llentry
*lle
,
4481 struct sysctl_req
*wr
)
4483 struct ifnet
*ifp
= llt
->llt_ifp
;
4486 struct rt_msghdr rtm
;
4487 struct sockaddr_in6 sin6
;
4489 * ndp.c assumes that sdl is word aligned
4494 struct sockaddr_dl sdl
;
4496 struct sockaddr_dl
*sdl
;
4499 bzero(&ndpc
, sizeof(ndpc
));
4500 /* skip deleted entries */
4501 if ((lle
->la_flags
& LLE_DELETED
) == LLE_DELETED
)
4503 /* Skip if jailed and not a valid IP of the prison. */
4504 lltable_fill_sa_entry(lle
,
4505 (struct sockaddr
*)&ndpc
.sin6
);
4507 * produce a msg made of:
4509 * struct sockaddr_in6 (IPv6)
4510 * struct sockaddr_dl;
4512 ndpc
.rtm
.rtm_msglen
= sizeof(ndpc
);
4513 ndpc
.rtm
.rtm_version
= RTM_VERSION
;
4514 ndpc
.rtm
.rtm_type
= RTM_GET
;
4515 ndpc
.rtm
.rtm_flags
= RTF_UP
;
4516 ndpc
.rtm
.rtm_addrs
= RTA_DST
| RTA_GATEWAY
;
4519 if (lle
->la_flags
& LLE_PUB
)
4520 ndpc
.rtm
.rtm_flags
|= RTF_ANNOUNCE
;
4522 sdl
->sdl_family
= AF_LINK
;
4523 sdl
->sdl_len
= sizeof(*sdl
);
4524 sdl
->sdl_index
= ifp
->if_index
;
4525 sdl
->sdl_type
= ifp
->if_type
;
4526 if ((lle
->la_flags
& LLE_VALID
) == LLE_VALID
) {
4527 sdl
->sdl_alen
= ifp
->if_addrlen
;
4528 bcopy(&lle
->ll_addr
, LLADDR(sdl
), ifp
->if_addrlen
);
4531 bzero(LLADDR(sdl
), ifp
->if_addrlen
);
4533 if (lle
->la_expire
!= 0) {
4537 clock_get_calendar_microtime(&secs
, &usecs
);
4538 ndpc
.rtm
.rtm_rmx
.rmx_expire
= lle
->la_expire
+
4539 lle
->lle_remtime
/ hz
+
4540 secs
- net_uptime();
4542 ndpc
.rtm
.rtm_flags
|= (RTF_HOST
| RTF_LLDATA
);
4543 if (lle
->la_flags
& LLE_STATIC
)
4544 ndpc
.rtm
.rtm_flags
|= RTF_STATIC
;
4545 if (lle
->la_flags
& LLE_IFADDR
)
4546 ndpc
.rtm
.rtm_flags
|= RTF_PINNED
;
4547 if (lle
->ln_router
!= 0)
4548 ndpc
.rtm
.rtm_flags
|= RTF_GATEWAY
;
4549 ndpc
.rtm
.rtm_rmx
.rmx_pksent
= lle
->la_asked
;
4550 /* Store state in rmx_weight value */
4551 ndpc
.rtm
.rtm_rmx
.rmx_state
= lle
->ln_state
;
4552 ndpc
.rtm
.rtm_index
= ifp
->if_index
;
4553 error
= SYSCTL_OUT(wr
, &ndpc
, sizeof(ndpc
));
4559 in6_lltattach(struct ifnet
*ifp
)
4561 struct lltable
*llt
;
4563 llt
= lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE
);
4564 llt
->llt_af
= AF_INET6
;
4567 llt
->llt_lookup
= in6_lltable_lookup
;
4568 llt
->llt_alloc_entry
= in6_lltable_alloc
;
4569 llt
->llt_delete_entry
= in6_lltable_delete_entry
;
4570 llt
->llt_dump_entry
= in6_lltable_dump_entry
;
4571 llt
->llt_hash
= in6_lltable_hash
;
4572 llt
->llt_fill_sa_entry
= in6_lltable_fill_sa_entry
;
4573 llt
->llt_free_entry
= in6_lltable_free_entry
;
4574 llt
->llt_match_prefix
= in6_lltable_match_prefix
;
4581 in6_ip6_to_sockaddr(const struct in6_addr
*ip6
, u_int16_t port
,
4582 struct sockaddr_in6
*sin6
, u_int32_t maxlen
)
4584 if (maxlen
< sizeof(struct sockaddr_in6
)) {
4588 *sin6
= (struct sockaddr_in6
) {
4589 .sin6_family
= AF_INET6
,
4590 .sin6_len
= sizeof(*sin6
),
4595 if (IN6_IS_SCOPE_EMBED(&sin6
->sin6_addr
))
4597 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
4598 sin6
->sin6_addr
.s6_addr16
[1] = 0;
4604 in6_evhdlr_code_t in6_event_code
;
4605 struct ifnet
*in6_ifp
;
4606 struct in6_addr in6_address
;
4610 struct in6_event2kev in6_event2kev_array
[IN6_EVENT_MAX
] = {
4612 .in6_event_code
= IN6_ADDR_MARKED_DUPLICATED
,
4613 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4614 .in6_event_kev_code
= KEV_ND6_DAD_FAILURE
,
4615 .in6_event_str
= "IN6_ADDR_MARKED_DUPLICATED",
4618 .in6_event_code
= IN6_ADDR_MARKED_DETACHED
,
4619 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4620 .in6_event_kev_code
= KEV_ND6_ADDR_DETACHED
,
4621 .in6_event_str
= "IN6_ADDR_MARKED_DETACHED",
4624 .in6_event_code
= IN6_ADDR_MARKED_DEPRECATED
,
4625 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4626 .in6_event_kev_code
= KEV_ND6_ADDR_DEPRECATED
,
4627 .in6_event_str
= "IN6_ADDR_MARKED_DEPRECATED",
4630 .in6_event_code
= IN6_NDP_RTR_EXPIRY
,
4631 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4632 .in6_event_kev_code
= KEV_ND6_RTR_EXPIRED
,
4633 .in6_event_str
= "IN6_NDP_RTR_EXPIRY",
4636 .in6_event_code
= IN6_NDP_PFX_EXPIRY
,
4637 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4638 .in6_event_kev_code
= KEV_ND6_PFX_EXPIRED
,
4639 .in6_event_str
= "IN6_NDP_PFX_EXPIRY",
4642 .in6_event_code
= IN6_NDP_ADDR_EXPIRY
,
4643 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4644 .in6_event_kev_code
= KEV_ND6_ADDR_EXPIRED
,
4645 .in6_event_str
= "IN6_NDP_ADDR_EXPIRY",
4650 in6_eventhdlr_callback(struct eventhandler_entry_arg arg0 __unused
,
4651 in6_evhdlr_code_t in6_ev_code
, struct ifnet
*ifp
,
4652 struct in6_addr
*p_addr6
, uint32_t val
)
4654 struct kev_msg ev_msg
;
4655 struct kev_nd6_event nd6_event
;
4657 bzero(&ev_msg
, sizeof(ev_msg
));
4658 bzero(&nd6_event
, sizeof(nd6_event
));
4660 nd6log0((LOG_INFO
, "%s Event %s received for %s\n",
4661 __func__
, in6_event2kev_array
[in6_ev_code
].in6_event_str
,
4662 ip6_sprintf(p_addr6
)));
4664 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
4665 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
4666 ev_msg
.kev_subclass
=
4667 in6_event2kev_array
[in6_ev_code
].in6_event_kev_subclass
;
4669 in6_event2kev_array
[in6_ev_code
].in6_event_kev_code
;
4671 nd6_event
.link_data
.if_family
= ifp
->if_family
;
4672 nd6_event
.link_data
.if_unit
= ifp
->if_unit
;
4673 strlcpy(nd6_event
.link_data
.if_name
, ifp
->if_name
,
4674 sizeof(nd6_event
.link_data
.if_name
));
4676 VERIFY(p_addr6
!= NULL
);
4677 bcopy(p_addr6
, &nd6_event
.in6_address
,
4678 sizeof(nd6_event
.in6_address
));
4679 nd6_event
.val
= val
;
4681 ev_msg
.dv
[0].data_ptr
= &nd6_event
;
4682 ev_msg
.dv
[0].data_length
= sizeof(nd6_event
);
4684 kev_post_msg(&ev_msg
);
4688 in6_event_callback(void *arg
)
4690 struct in6_event
*p_in6_ev
= (struct in6_event
*)arg
;
4692 EVENTHANDLER_INVOKE(&in6_evhdlr_ctxt
, in6_event
,
4693 p_in6_ev
->in6_event_code
, p_in6_ev
->in6_ifp
,
4694 &p_in6_ev
->in6_address
, p_in6_ev
->val
);
4697 struct in6_event_nwk_wq_entry
4699 struct nwk_wq_entry nwk_wqe
;
4700 struct in6_event in6_ev_arg
;
4704 in6_event_enqueue_nwk_wq_entry(in6_evhdlr_code_t in6_event_code
,
4705 struct ifnet
*ifp
, struct in6_addr
*p_addr6
,
4708 struct in6_event_nwk_wq_entry
*p_in6_ev
= NULL
;
4710 MALLOC(p_in6_ev
, struct in6_event_nwk_wq_entry
*,
4711 sizeof(struct in6_event_nwk_wq_entry
),
4712 M_NWKWQ
, M_WAITOK
| M_ZERO
);
4714 p_in6_ev
->nwk_wqe
.func
= in6_event_callback
;
4715 p_in6_ev
->nwk_wqe
.is_arg_managed
= TRUE
;
4716 p_in6_ev
->nwk_wqe
.arg
= &p_in6_ev
->in6_ev_arg
;
4718 p_in6_ev
->in6_ev_arg
.in6_event_code
= in6_event_code
;
4719 p_in6_ev
->in6_ev_arg
.in6_ifp
= ifp
;
4720 if (p_addr6
!= NULL
) {
4721 bcopy(p_addr6
, &p_in6_ev
->in6_ev_arg
.in6_address
,
4722 sizeof(p_in6_ev
->in6_ev_arg
.in6_address
));
4724 p_in6_ev
->in6_ev_arg
.val
= val
;
4726 nwk_wq_enqueue((struct nwk_wq_entry
*)p_in6_ev
);