2 * Copyright (c) 2003-2017 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
;
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_stat
,
938 sizeof (ifr
->ifr_ifru
.ifru_icmp6stat
));
940 bcopy(&IN6_IFEXTRA(ifp
)->icmp6_ifstat
,
941 &ifr
->ifr_ifru
.ifru_icmp6stat
,
942 sizeof (ifr
->ifr_ifru
.ifru_icmp6stat
));
955 * Caller passes in the ioctl data pointer directly via "ifr", with the
956 * expectation that this routine always uses bcopy() or other byte-aligned
959 static __attribute__((noinline
)) int
960 in6ctl_alifetime(struct in6_ifaddr
*ia
, u_long cmd
, struct in6_ifreq
*ifr
,
963 uint64_t timenow
= net_uptime();
964 struct in6_addrlifetime ia6_lt
;
965 struct timeval caltime
;
969 return (EADDRNOTAVAIL
);
972 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
973 IFA_LOCK(&ia
->ia_ifa
);
974 /* retrieve time as calendar time (last arg is 1) */
975 in6ifa_getlifetime(ia
, &ia6_lt
, 1);
977 struct in6_addrlifetime_64 lt
;
979 bzero(<
, sizeof (lt
));
980 lt
.ia6t_expire
= ia6_lt
.ia6t_expire
;
981 lt
.ia6t_preferred
= ia6_lt
.ia6t_preferred
;
982 lt
.ia6t_vltime
= ia6_lt
.ia6t_vltime
;
983 lt
.ia6t_pltime
= ia6_lt
.ia6t_pltime
;
984 bcopy(<
, &ifr
->ifr_ifru
.ifru_lifetime
, sizeof (lt
));
986 struct in6_addrlifetime_32 lt
;
988 bzero(<
, sizeof (lt
));
989 lt
.ia6t_expire
= (uint32_t)ia6_lt
.ia6t_expire
;
990 lt
.ia6t_preferred
= (uint32_t)ia6_lt
.ia6t_preferred
;
991 lt
.ia6t_vltime
= (uint32_t)ia6_lt
.ia6t_vltime
;
992 lt
.ia6t_pltime
= (uint32_t)ia6_lt
.ia6t_pltime
;
993 bcopy(<
, &ifr
->ifr_ifru
.ifru_lifetime
, sizeof (lt
));
995 IFA_UNLOCK(&ia
->ia_ifa
);
998 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
999 getmicrotime(&caltime
);
1001 /* sanity for overflow - beware unsigned */
1003 struct in6_addrlifetime_64 lt
;
1005 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1006 if (lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
&&
1007 lt
.ia6t_vltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1011 if (lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
&&
1012 lt
.ia6t_pltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1017 struct in6_addrlifetime_32 lt
;
1019 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1020 if (lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
&&
1021 lt
.ia6t_vltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1025 if (lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
&&
1026 lt
.ia6t_pltime
+ caltime
.tv_sec
< caltime
.tv_sec
) {
1032 IFA_LOCK(&ia
->ia_ifa
);
1034 struct in6_addrlifetime_64 lt
;
1036 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1037 ia6_lt
.ia6t_expire
= lt
.ia6t_expire
;
1038 ia6_lt
.ia6t_preferred
= lt
.ia6t_preferred
;
1039 ia6_lt
.ia6t_vltime
= lt
.ia6t_vltime
;
1040 ia6_lt
.ia6t_pltime
= lt
.ia6t_pltime
;
1042 struct in6_addrlifetime_32 lt
;
1044 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1045 ia6_lt
.ia6t_expire
= (uint32_t)lt
.ia6t_expire
;
1046 ia6_lt
.ia6t_preferred
= (uint32_t)lt
.ia6t_preferred
;
1047 ia6_lt
.ia6t_vltime
= lt
.ia6t_vltime
;
1048 ia6_lt
.ia6t_pltime
= lt
.ia6t_pltime
;
1051 if (ia6_lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
)
1052 ia6_lt
.ia6t_expire
= timenow
+ ia6_lt
.ia6t_vltime
;
1054 ia6_lt
.ia6t_expire
= 0;
1056 if (ia6_lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
)
1057 ia6_lt
.ia6t_preferred
= timenow
+ ia6_lt
.ia6t_pltime
;
1059 ia6_lt
.ia6t_preferred
= 0;
1061 in6ifa_setlifetime(ia
, &ia6_lt
);
1062 IFA_UNLOCK(&ia
->ia_ifa
);
1073 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(void *)(ifa))
1076 * Generic INET6 control operations (ioctl's).
1078 * ifp is NULL if not an interface-specific ioctl.
1080 * Most of the routines called to handle the ioctls would end up being
1081 * tail-call optimized, which unfortunately causes this routine to
1082 * consume too much stack space; this is the reason for the "noinline"
1083 * attribute used on those routines.
1085 * If called directly from within the networking stack (as opposed to via
1086 * pru_control), the socket parameter may be NULL.
1089 in6_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
1092 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
1093 struct in6_aliasreq sifra
, *ifra
= NULL
;
1094 struct in6_ifaddr
*ia
= NULL
;
1095 struct sockaddr_in6 sin6
, *sa6
= NULL
;
1096 boolean_t privileged
= (proc_suser(p
) == 0);
1097 boolean_t p64
= proc_is64bit(p
);
1098 boolean_t so_unlocked
= FALSE
;
1099 int intval
, error
= 0;
1101 /* In case it's NULL, make sure it came from the kernel */
1102 VERIFY(so
!= NULL
|| p
== kernproc
);
1105 * ioctls which don't require ifp, may require socket.
1108 case SIOCAADDRCTL_POLICY
: /* struct in6_addrpolicy */
1109 case SIOCDADDRCTL_POLICY
: /* struct in6_addrpolicy */
1112 return (in6_src_ioctl(cmd
, data
));
1115 case SIOCDRADD_IN6_32
: /* struct in6_defrouter_32 */
1116 case SIOCDRADD_IN6_64
: /* struct in6_defrouter_64 */
1117 case SIOCDRDEL_IN6_32
: /* struct in6_defrouter_32 */
1118 case SIOCDRDEL_IN6_64
: /* struct in6_defrouter_64 */
1121 return (defrtrlist_ioctl(cmd
, data
));
1124 case SIOCGASSOCIDS32
: /* struct so_aidreq32 */
1125 case SIOCGASSOCIDS64
: /* struct so_aidreq64 */
1126 return (in6ctl_associd(so
, cmd
, data
));
1129 case SIOCGCONNIDS32
: /* struct so_cidreq32 */
1130 case SIOCGCONNIDS64
: /* struct so_cidreq64 */
1131 return (in6ctl_connid(so
, cmd
, data
));
1134 case SIOCGCONNINFO32
: /* struct so_cinforeq32 */
1135 case SIOCGCONNINFO64
: /* struct so_cinforeq64 */
1136 return (in6ctl_conninfo(so
, cmd
, data
));
1141 * The rest of ioctls require ifp; reject if we don't have one;
1142 * return ENXIO to be consistent with ifioctl().
1148 * Unlock the socket since ifnet_ioctl() may be invoked by
1149 * one of the ioctl handlers below. Socket will be re-locked
1150 * prior to returning.
1153 socket_unlock(so
, 0);
1158 * ioctls which require ifp but not interface address.
1161 case SIOCAUTOCONF_START
: /* struct in6_ifreq */
1166 error
= in6_autoconf(ifp
, TRUE
);
1169 case SIOCAUTOCONF_STOP
: /* struct in6_ifreq */
1174 error
= in6_autoconf(ifp
, FALSE
);
1177 case SIOCLL_START_32
: /* struct in6_aliasreq_32 */
1178 case SIOCLL_START_64
: /* struct in6_aliasreq_64 */
1183 error
= in6ctl_llstart(ifp
, cmd
, data
);
1186 case SIOCLL_STOP
: /* struct in6_ifreq */
1191 error
= in6ctl_llstop(ifp
);
1194 case SIOCSETROUTERMODE_IN6
: /* struct in6_ifreq */
1199 bcopy(&((struct in6_ifreq
*)(void *)data
)->ifr_intval
,
1200 &intval
, sizeof (intval
));
1202 error
= in6_setrouter(ifp
, intval
);
1205 case SIOCPROTOATTACH_IN6_32
: /* struct in6_aliasreq_32 */
1206 case SIOCPROTOATTACH_IN6_64
: /* struct in6_aliasreq_64 */
1211 error
= in6_domifattach(ifp
);
1214 case SIOCPROTODETACH_IN6
: /* struct in6_ifreq */
1219 /* Cleanup interface routes and addresses */
1222 if ((error
= proto_unplumb(PF_INET6
, ifp
)))
1223 log(LOG_ERR
, "SIOCPROTODETACH_IN6: %s error=%d\n",
1224 if_name(ifp
), error
);
1227 case SIOCSNDFLUSH_IN6
: /* struct in6_ifreq */
1228 case SIOCSPFXFLUSH_IN6
: /* struct in6_ifreq */
1229 case SIOCSRTRFLUSH_IN6
: /* struct in6_ifreq */
1230 case SIOCSDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
1231 case SIOCSDEFIFACE_IN6_64
: /* struct in6_ndifreq_64 */
1232 case SIOCSIFINFO_FLAGS
: /* struct in6_ndireq */
1233 case SIOCGIFCGAPREP_IN6
: /* struct in6_ifreq */
1234 case SIOCSIFCGAPREP_IN6
: /* struct in6_ifreq */
1240 case OSIOCGIFINFO_IN6
: /* struct in6_ondireq */
1241 case SIOCGIFINFO_IN6
: /* struct in6_ondireq */
1242 case SIOCGDRLST_IN6_32
: /* struct in6_drlist_32 */
1243 case SIOCGDRLST_IN6_64
: /* struct in6_drlist_64 */
1244 case SIOCGPRLST_IN6_32
: /* struct in6_prlist_32 */
1245 case SIOCGPRLST_IN6_64
: /* struct in6_prlist_64 */
1246 case SIOCGNBRINFO_IN6_32
: /* struct in6_nbrinfo_32 */
1247 case SIOCGNBRINFO_IN6_64
: /* struct in6_nbrinfo_64 */
1248 case SIOCGDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
1249 case SIOCGDEFIFACE_IN6_64
: /* struct in6_ndifreq_64 */
1250 error
= nd6_ioctl(cmd
, data
, ifp
);
1253 case SIOCSIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1254 case SIOCDIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1255 case SIOCAIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1256 case SIOCCIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1257 case SIOCSGIFPREFIX_IN6
: /* struct in6_rrenumreq (deprecated) */
1258 case SIOCGIFPREFIX_IN6
: /* struct in6_prefixreq (deprecated) */
1260 "prefix ioctls are now invalidated. "
1261 "please use ifconfig.\n");
1265 case SIOCSSCOPE6
: /* struct in6_ifreq (deprecated) */
1266 case SIOCGSCOPE6
: /* struct in6_ifreq (deprecated) */
1267 case SIOCGSCOPE6DEF
: /* struct in6_ifreq (deprecated) */
1271 case SIOCLL_CGASTART_32
: /* struct in6_cgareq_32 */
1272 case SIOCLL_CGASTART_64
: /* struct in6_cgareq_64 */
1276 error
= in6ctl_cgastart(ifp
, cmd
, data
);
1279 case SIOCGIFSTAT_IN6
: /* struct in6_ifreq */
1280 case SIOCGIFSTAT_ICMP6
: /* struct in6_ifreq */
1281 error
= in6ctl_gifstat(ifp
, cmd
, ifr
);
1286 * ioctls which require interface address; obtain sockaddr_in6.
1289 case SIOCSIFADDR_IN6
: /* struct in6_ifreq (deprecated) */
1290 case SIOCSIFDSTADDR_IN6
: /* struct in6_ifreq (deprecated) */
1291 case SIOCSIFNETMASK_IN6
: /* struct in6_ifreq (deprecated) */
1293 * Since IPv6 allows a node to assign multiple addresses
1294 * on a single interface, SIOCSIFxxx ioctls are deprecated.
1296 /* we decided to obsolete this command (20000704) */
1300 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1301 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1307 * Convert user ifra to the kernel form, when appropriate.
1308 * This allows the conversion between different data models
1309 * to be centralized, so that it can be passed around to other
1310 * routines that are expecting the kernel form.
1312 ifra
= in6_aliasreq_to_native(data
,
1313 (cmd
== SIOCAIFADDR_IN6_64
), &sifra
);
1314 bcopy(&ifra
->ifra_addr
, &sin6
, sizeof (sin6
));
1318 case SIOCDIFADDR_IN6
: /* struct in6_ifreq */
1319 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
1325 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
1326 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
1327 case SIOCGIFNETMASK_IN6
: /* struct in6_ifreq */
1328 case SIOCGIFAFLAG_IN6
: /* struct in6_ifreq */
1329 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
1330 bcopy(&ifr
->ifr_addr
, &sin6
, sizeof (sin6
));
1333 case SIOCGIFDSTADDR
:
1334 case SIOCSIFDSTADDR
:
1335 case SIOCGIFBRDADDR
:
1336 case SIOCSIFBRDADDR
:
1337 case SIOCGIFNETMASK
:
1338 case SIOCSIFNETMASK
:
1343 /* Do not handle these AF_INET commands in AF_INET6 path */
1349 * Find address for this interface, if it exists.
1351 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
1352 * only, and used the first interface address as the target of other
1353 * operations (without checking ifra_addr). This was because netinet
1354 * code/API assumed at most 1 interface address per interface.
1355 * Since IPv6 allows a node to assign multiple addresses
1356 * on a single interface, we almost always look and check the
1357 * presence of ifra_addr, and reject invalid ones here.
1358 * It also decreases duplicated code among SIOC*_IN6 operations.
1361 if (sa6
!= NULL
&& sa6
->sin6_family
== AF_INET6
) {
1362 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
1363 if (sa6
->sin6_addr
.s6_addr16
[1] == 0) {
1364 /* link ID is not embedded by the user */
1365 sa6
->sin6_addr
.s6_addr16
[1] =
1366 htons(ifp
->if_index
);
1367 } else if (sa6
->sin6_addr
.s6_addr16
[1] !=
1368 htons(ifp
->if_index
)) {
1369 error
= EINVAL
; /* link ID contradicts */
1372 if (sa6
->sin6_scope_id
) {
1373 if (sa6
->sin6_scope_id
!=
1374 (u_int32_t
)ifp
->if_index
) {
1378 sa6
->sin6_scope_id
= 0; /* XXX: good way? */
1382 * Any failures from this point on must take into account
1383 * a non-NULL "ia" with an outstanding reference count, and
1384 * therefore requires IFA_REMREF. Jump to "done" label
1385 * instead of calling return if "ia" is valid.
1387 ia
= in6ifa_ifpwithaddr(ifp
, &sa6
->sin6_addr
);
1391 * SIOCDIFADDR_IN6/SIOCAIFADDR_IN6 specific tests.
1394 case SIOCDIFADDR_IN6
: /* struct in6_ifreq */
1396 error
= EADDRNOTAVAIL
;
1400 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1401 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1402 VERIFY(sa6
!= NULL
);
1404 * We always require users to specify a valid IPv6 address for
1405 * the corresponding operation. Use "sa6" instead of "ifra"
1406 * since SIOCDIFADDR_IN6 falls thru above.
1408 if (sa6
->sin6_family
!= AF_INET6
||
1409 sa6
->sin6_len
!= sizeof (struct sockaddr_in6
)) {
1410 error
= EAFNOSUPPORT
;
1417 * And finally process address-related ioctls.
1420 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
1421 /* This interface is basically deprecated. use SIOCGIFCONF. */
1423 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
1424 error
= in6ctl_gifaddr(ifp
, ia
, cmd
, ifr
);
1427 case SIOCGIFNETMASK_IN6
: /* struct in6_ifreq */
1429 IFA_LOCK(&ia
->ia_ifa
);
1430 bcopy(&ia
->ia_prefixmask
, &ifr
->ifr_addr
,
1431 sizeof (struct sockaddr_in6
));
1432 IFA_UNLOCK(&ia
->ia_ifa
);
1434 error
= EADDRNOTAVAIL
;
1438 case SIOCGIFAFLAG_IN6
: /* struct in6_ifreq */
1440 IFA_LOCK(&ia
->ia_ifa
);
1441 bcopy(&ia
->ia6_flags
, &ifr
->ifr_ifru
.ifru_flags6
,
1442 sizeof (ifr
->ifr_ifru
.ifru_flags6
));
1443 IFA_UNLOCK(&ia
->ia_ifa
);
1445 error
= EADDRNOTAVAIL
;
1449 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
1450 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
1451 error
= in6ctl_alifetime(ia
, cmd
, ifr
, p64
);
1454 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1455 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
1456 error
= in6ctl_aifaddr(ifp
, ifra
);
1459 case SIOCDIFADDR_IN6
:
1460 in6ctl_difaddr(ifp
, ia
);
1464 error
= ifnet_ioctl(ifp
, PF_INET6
, cmd
, data
);
1470 IFA_REMREF(&ia
->ia_ifa
);
1477 static __attribute__((noinline
)) int
1478 in6ctl_aifaddr(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
)
1480 int i
, error
, addtmp
, plen
;
1481 struct nd_prefix pr0
, *pr
;
1482 struct in6_ifaddr
*ia
;
1484 VERIFY(ifp
!= NULL
&& ifra
!= NULL
);
1487 /* Attempt to attach the protocol, in case it isn't attached */
1488 error
= in6_domifattach(ifp
);
1490 /* PF_INET6 wasn't previously attached */
1491 error
= in6_ifattach_aliasreq(ifp
, NULL
, NULL
);
1495 in6_if_up_dad_start(ifp
);
1496 } else if (error
!= EEXIST
) {
1501 * First, make or update the interface address structure, and link it
1504 error
= in6_update_ifa(ifp
, ifra
, 0, &ia
);
1509 /* Now, make the prefix on-link on the interface. */
1510 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
, NULL
);
1515 * NOTE: We'd rather create the prefix before the address, but we need
1516 * at least one address to install the corresponding interface route,
1517 * so we configure the address first.
1521 * Convert mask to prefix length (prefixmask has already been validated
1522 * in in6_update_ifa().
1524 bzero(&pr0
, sizeof (pr0
));
1525 pr0
.ndpr_plen
= plen
;
1527 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
1528 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
1530 /* apply the mask for safety. */
1531 for (i
= 0; i
< 4; i
++) {
1532 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1533 ifra
->ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
1537 * Since we don't have an API to set prefix (not address) lifetimes, we
1538 * just use the same lifetimes as addresses. The (temporarily)
1539 * installed lifetimes can be overridden by later advertised RAs (when
1540 * accept_rtadv is non 0), which is an intended behavior.
1542 pr0
.ndpr_raf_onlink
= 1; /* should be configurable? */
1543 pr0
.ndpr_raf_auto
= !!(ifra
->ifra_flags
& IN6_IFF_AUTOCONF
);
1544 pr0
.ndpr_vltime
= ifra
->ifra_lifetime
.ia6t_vltime
;
1545 pr0
.ndpr_pltime
= ifra
->ifra_lifetime
.ia6t_pltime
;
1546 pr0
.ndpr_stateflags
|= NDPRF_STATIC
;
1547 lck_mtx_init(&pr0
.ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
1549 /* add the prefix if there's none. */
1550 if ((pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_NEVER
)) == NULL
) {
1552 * nd6_prelist_add will install the corresponding interface
1555 error
= nd6_prelist_add(&pr0
, NULL
, &pr
, FALSE
);
1560 log(LOG_ERR
, "%s: nd6_prelist_add okay, but"
1561 " no prefix.\n", __func__
);
1567 IFA_LOCK(&ia
->ia_ifa
);
1569 /* if this is a new autoconfed addr */
1571 if (ia
->ia6_ndpr
== NULL
) {
1574 VERIFY(pr
->ndpr_addrcnt
!= 0);
1576 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
1579 * If this is the first autoconf address from the prefix,
1580 * create a temporary address as well (when specified).
1582 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
1584 pr
->ndpr_addrcnt
== 1) {
1590 IFA_UNLOCK(&ia
->ia_ifa
);
1594 e
= in6_tmpifadd(ia
, 1);
1596 log(LOG_NOTICE
, "%s: failed to create a"
1597 " temporary address, error=%d\n",
1602 * This might affect the status of autoconfigured addresses, that is,
1603 * this address might make other addresses detached.
1605 lck_mtx_lock(nd6_mutex
);
1606 pfxlist_onlink_check();
1607 lck_mtx_unlock(nd6_mutex
);
1609 /* Drop use count held above during lookup/add */
1614 IFA_REMREF(&ia
->ia_ifa
);
1618 static __attribute__((noinline
)) void
1619 in6ctl_difaddr(struct ifnet
*ifp
, struct in6_ifaddr
*ia
)
1622 struct nd_prefix pr0
, *pr
;
1624 VERIFY(ifp
!= NULL
&& ia
!= NULL
);
1627 * If the address being deleted is the only one that owns
1628 * the corresponding prefix, expire the prefix as well.
1629 * XXX: theoretically, we don't have to worry about such
1630 * relationship, since we separate the address management
1631 * and the prefix management. We do this, however, to provide
1632 * as much backward compatibility as possible in terms of
1633 * the ioctl operation.
1634 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
1636 IFA_LOCK(&ia
->ia_ifa
);
1637 bzero(&pr0
, sizeof (pr0
));
1639 pr0
.ndpr_plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
);
1640 if (pr0
.ndpr_plen
== 128) {
1641 IFA_UNLOCK(&ia
->ia_ifa
);
1644 pr0
.ndpr_prefix
= ia
->ia_addr
;
1645 pr0
.ndpr_mask
= ia
->ia_prefixmask
.sin6_addr
;
1646 for (i
= 0; i
< 4; i
++) {
1647 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1648 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
];
1650 IFA_UNLOCK(&ia
->ia_ifa
);
1652 if ((pr
= nd6_prefix_lookup(&pr0
, ND6_PREFIX_EXPIRY_UNSPEC
)) != NULL
) {
1653 IFA_LOCK(&ia
->ia_ifa
);
1655 if (pr
->ndpr_addrcnt
== 1) {
1656 /* XXX: just for expiration */
1657 pr
->ndpr_expire
= 1;
1660 IFA_UNLOCK(&ia
->ia_ifa
);
1662 /* Drop use count held above during lookup */
1667 in6_purgeaddr(&ia
->ia_ifa
);
1670 static __attribute__((noinline
)) int
1671 in6_autoconf(struct ifnet
*ifp
, int enable
)
1675 VERIFY(ifp
!= NULL
);
1677 if (ifp
->if_flags
& IFF_LOOPBACK
)
1682 * An interface in IPv6 router mode implies that it
1683 * is either configured with a static IP address or
1684 * autoconfigured via a locally-generated RA. Prevent
1685 * SIOCAUTOCONF_START from being set in that mode.
1687 ifnet_lock_exclusive(ifp
);
1688 if (ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
1689 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADV
;
1692 ifp
->if_eflags
|= IFEF_ACCEPT_RTADV
;
1694 ifnet_lock_done(ifp
);
1696 struct in6_ifaddr
*ia
= NULL
;
1698 ifnet_lock_exclusive(ifp
);
1699 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADV
;
1700 ifnet_lock_done(ifp
);
1702 /* Remove autoconfigured address from interface */
1703 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1705 while (ia
!= NULL
) {
1706 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
1710 IFA_LOCK(&ia
->ia_ifa
);
1711 if (ia
->ia6_flags
& IN6_IFF_AUTOCONF
) {
1712 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
1713 IFA_UNLOCK(&ia
->ia_ifa
);
1714 lck_rw_done(&in6_ifaddr_rwlock
);
1715 in6_purgeaddr(&ia
->ia_ifa
);
1716 IFA_REMREF(&ia
->ia_ifa
); /* for us */
1717 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1719 * Purging the address caused in6_ifaddr_rwlock
1720 * to be dropped and reacquired;
1721 * therefore search again from the beginning
1722 * of in6_ifaddrs list.
1727 IFA_UNLOCK(&ia
->ia_ifa
);
1730 lck_rw_done(&in6_ifaddr_rwlock
);
1736 * Handle SIOCSETROUTERMODE_IN6 to set or clear the IPv6 router mode flag on
1737 * the interface. Entering or exiting this mode will result in the removal of
1738 * autoconfigured IPv6 addresses on the interface.
1740 static __attribute__((noinline
)) int
1741 in6_setrouter(struct ifnet
*ifp
, int enable
)
1743 VERIFY(ifp
!= NULL
);
1745 if (ifp
->if_flags
& IFF_LOOPBACK
)
1749 struct nd_ifinfo
*ndi
= NULL
;
1751 ndi
= ND_IFINFO(ifp
);
1752 if (ndi
!= NULL
&& ndi
->initialized
) {
1753 lck_mtx_lock(&ndi
->lock
);
1754 if (ndi
->flags
& ND6_IFF_PROXY_PREFIXES
) {
1755 /* No proxy if we are an advertising router */
1756 ndi
->flags
&= ~ND6_IFF_PROXY_PREFIXES
;
1757 lck_mtx_unlock(&ndi
->lock
);
1758 (void) nd6_if_prproxy(ifp
, FALSE
);
1760 lck_mtx_unlock(&ndi
->lock
);
1765 ifnet_lock_exclusive(ifp
);
1767 ifp
->if_eflags
|= IFEF_IPV6_ROUTER
;
1769 ifp
->if_eflags
&= ~IFEF_IPV6_ROUTER
;
1771 ifnet_lock_done(ifp
);
1773 lck_mtx_lock(nd6_mutex
);
1774 defrouter_select(ifp
);
1775 lck_mtx_unlock(nd6_mutex
);
1777 if_allmulti(ifp
, enable
);
1779 return (in6_autoconf(ifp
, FALSE
));
1783 in6_to_kamescope(struct sockaddr_in6
*sin6
, struct ifnet
*ifp
)
1785 struct sockaddr_in6 tmp
;
1788 VERIFY(sin6
!= NULL
);
1791 error
= in6_recoverscope(&tmp
, &sin6
->sin6_addr
, ifp
);
1795 id
= in6_addr2scopeid(ifp
, &tmp
.sin6_addr
);
1796 if (tmp
.sin6_scope_id
== 0)
1797 tmp
.sin6_scope_id
= id
;
1798 else if (tmp
.sin6_scope_id
!= id
)
1799 return (EINVAL
); /* scope ID mismatch. */
1801 error
= in6_embedscope(&tmp
.sin6_addr
, &tmp
, NULL
, NULL
, NULL
);
1805 tmp
.sin6_scope_id
= 0;
1811 * When the address is being configured we should clear out certain flags
1812 * coming in from the caller.
1814 #define IN6_IFF_CLR_ADDR_FLAG_MASK (~(IN6_IFF_DEPRECATED | IN6_IFF_DETACHED | IN6_IFF_DUPLICATED))
1817 in6_ifaupdate_aux(struct in6_ifaddr
*ia
, struct ifnet
*ifp
, int ifaupflags
)
1819 struct sockaddr_in6 mltaddr
, mltmask
;
1820 struct in6_addr llsol
;
1822 struct in6_multi
*in6m_sol
;
1823 struct in6_multi_mship
*imm
;
1825 int delay
, error
= 0;
1827 VERIFY(ifp
!= NULL
&& ia
!= NULL
);
1831 nd6log2((LOG_DEBUG
, "%s - %s ifp %s ia6_flags 0x%x ifaupflags 0x%x\n",
1833 ip6_sprintf(&ia
->ia_addr
.sin6_addr
),
1834 if_name(ia
->ia_ifp
),
1839 * Just to be safe, always clear certain flags when address
1840 * is being configured
1842 ia
->ia6_flags
&= IN6_IFF_CLR_ADDR_FLAG_MASK
;
1845 * Mark the address as tentative before joining multicast addresses,
1846 * so that corresponding MLD responses would not have a tentative
1849 if (in6if_do_dad(ifp
)) {
1850 in6_ifaddr_set_dadprogress(ia
);
1852 * Do not delay sending neighbor solicitations when using optimistic
1853 * duplicate address detection, c.f. RFC 4429.
1855 if (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
)
1856 ifaupflags
&= ~IN6_IFAUPDATE_DADDELAY
;
1858 ifaupflags
|= IN6_IFAUPDATE_DADDELAY
;
1861 * If the interface has been marked to not perform
1862 * DAD, make sure to reset DAD in progress flags
1863 * that may come in from the caller.
1865 ia
->ia6_flags
&= ~IN6_IFF_DADPROGRESS
;
1868 /* Join necessary multicast groups */
1869 if ((ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1871 /* join solicited multicast addr for new host id */
1872 bzero(&llsol
, sizeof (struct in6_addr
));
1873 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
1874 llsol
.s6_addr32
[1] = 0;
1875 llsol
.s6_addr32
[2] = htonl(1);
1876 llsol
.s6_addr32
[3] = ia
->ia_addr
.sin6_addr
.s6_addr32
[3];
1877 llsol
.s6_addr8
[12] = 0xff;
1878 if ((error
= in6_setscope(&llsol
, ifp
, NULL
)) != 0) {
1879 /* XXX: should not happen */
1880 log(LOG_ERR
, "%s: in6_setscope failed\n", __func__
);
1884 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
)) {
1886 * We need a random delay for DAD on the address
1887 * being configured. It also means delaying
1888 * transmission of the corresponding MLD report to
1889 * avoid report collision. [RFC 4862]
1891 delay
= random() % MAX_RTR_SOLICITATION_DELAY
;
1893 imm
= in6_joingroup(ifp
, &llsol
, &error
, delay
);
1895 nd6log((LOG_WARNING
,
1896 "%s: addmulti failed for %s on %s (errno=%d)\n",
1897 __func__
, ip6_sprintf(&llsol
), if_name(ifp
),
1902 in6m_sol
= imm
->i6mm_maddr
;
1903 /* take a refcount for this routine */
1904 IN6M_ADDREF(in6m_sol
);
1907 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
1910 bzero(&mltmask
, sizeof (mltmask
));
1911 mltmask
.sin6_len
= sizeof (struct sockaddr_in6
);
1912 mltmask
.sin6_family
= AF_INET6
;
1913 mltmask
.sin6_addr
= in6mask32
;
1914 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
1917 * join link-local all-nodes address
1919 bzero(&mltaddr
, sizeof (mltaddr
));
1920 mltaddr
.sin6_len
= sizeof (struct sockaddr_in6
);
1921 mltaddr
.sin6_family
= AF_INET6
;
1922 mltaddr
.sin6_addr
= in6addr_linklocal_allnodes
;
1923 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
)) != 0)
1924 goto unwind
; /* XXX: should not fail */
1927 * XXX: do we really need this automatic routes?
1928 * We should probably reconsider this stuff. Most applications
1929 * actually do not need the routes, since they usually specify
1930 * the outgoing interface.
1932 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
1933 ia
->ia_ifp
->if_index
);
1935 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
1936 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN
)) {
1942 error
= rtrequest_scoped(RTM_ADD
,
1943 (struct sockaddr
*)&mltaddr
,
1944 (struct sockaddr
*)&ia
->ia_addr
,
1945 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
1946 NULL
, ia
->ia_ifp
->if_index
);
1953 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
1955 nd6log((LOG_WARNING
,
1956 "%s: addmulti failed for %s on %s (errno=%d)\n",
1957 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
1958 if_name(ifp
), error
));
1963 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
1967 * join node information group address
1969 #define hostnamelen strlen(hostname)
1971 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
)) {
1973 * The spec doesn't say anything about delay for this
1974 * group, but the same logic should apply.
1976 delay
= random() % MAX_RTR_SOLICITATION_DELAY
;
1978 if (in6_nigroup(ifp
, hostname
, hostnamelen
, &mltaddr
.sin6_addr
)
1980 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
,
1981 delay
); /* XXX jinmei */
1983 nd6log((LOG_WARNING
,
1984 "%s: addmulti failed for %s on %s "
1986 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
1987 if_name(ifp
), error
));
1988 /* XXX not very fatal, go on... */
1992 LIST_INSERT_HEAD(&ia
->ia6_memberships
,
2000 * join interface-local all-nodes address.
2001 * (ff01::1%ifN, and ff01::%ifN/32)
2003 mltaddr
.sin6_addr
= in6addr_nodelocal_allnodes
;
2004 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
)) != 0)
2005 goto unwind
; /* XXX: should not fail */
2006 /* XXX: again, do we really need the route? */
2007 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
2008 ia
->ia_ifp
->if_index
);
2010 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
2011 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN
)) {
2017 error
= rtrequest_scoped(RTM_ADD
,
2018 (struct sockaddr
*)&mltaddr
,
2019 (struct sockaddr
*)&ia
->ia_addr
,
2020 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
2021 NULL
, ia
->ia_ifp
->if_index
);
2027 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
2029 nd6log((LOG_WARNING
,
2030 "%s: addmulti failed for %s on %s (errno=%d)\n",
2031 __func__
, ip6_sprintf(&mltaddr
.sin6_addr
),
2032 if_name(ifp
), error
));
2037 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
2042 /* Ensure nd6_service() is scheduled as soon as it's convenient */
2043 ++nd6_sched_timeout_want
;
2046 * Perform DAD, if needed.
2047 * XXX It may be of use, if we can administratively
2051 if (in6if_do_dad(ifp
) && ((ifa
->ifa_flags
& IN6_IFF_NODAD
) == 0) &&
2052 (ia
->ia6_flags
& IN6_IFF_DADPROGRESS
)) {
2053 int mindelay
, maxdelay
;
2054 int *delayptr
, delayval
;
2059 * Avoid the DAD delay if the caller wants us to skip it.
2060 * This is not compliant with RFC 2461, but it's only being
2061 * used for signalling and not for actual DAD.
2063 if ((ifaupflags
& IN6_IFAUPDATE_DADDELAY
) &&
2064 !(ia
->ia6_flags
& IN6_IFF_SWIFTDAD
)) {
2066 * We need to impose a delay before sending an NS
2067 * for DAD. Check if we also needed a delay for the
2068 * corresponding MLD message. If we did, the delay
2069 * should be larger than the MLD delay (this could be
2070 * relaxed a bit, but this simple logic is at least
2074 if (in6m_sol
!= NULL
) {
2075 IN6M_LOCK(in6m_sol
);
2076 if (in6m_sol
->in6m_state
==
2077 MLD_REPORTING_MEMBER
)
2078 mindelay
= in6m_sol
->in6m_timer
;
2079 IN6M_UNLOCK(in6m_sol
);
2081 maxdelay
= MAX_RTR_SOLICITATION_DELAY
* hz
;
2082 if (maxdelay
- mindelay
== 0)
2086 (random() % (maxdelay
- mindelay
)) +
2089 delayptr
= &delayval
;
2092 nd6_dad_start((struct ifaddr
*)ia
, delayptr
);
2101 in6_purgeaddr(&ia
->ia_ifa
);
2104 /* release reference held for this routine */
2105 if (in6m_sol
!= NULL
)
2106 IN6M_REMREF(in6m_sol
);
2111 * Request an IPv6 interface address. If the address is new, then it will be
2112 * constructed and appended to the interface address chains. The interface
2113 * address structure is optionally returned with a reference for the caller.
2116 in6_update_ifa(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
, int ifaupflags
,
2117 struct in6_ifaddr
**iar
)
2119 struct in6_addrlifetime ia6_lt
;
2120 struct in6_ifaddr
*ia
;
2122 struct ifaddr
*xifa
;
2123 struct in6_addrlifetime
*lt
;
2127 /* Sanity check parameters and initialize locals */
2128 VERIFY(ifp
!= NULL
&& ifra
!= NULL
&& iar
!= NULL
);
2134 * We always require users to specify a valid IPv6 address for
2135 * the corresponding operation.
2137 if (ifra
->ifra_addr
.sin6_family
!= AF_INET6
||
2138 ifra
->ifra_addr
.sin6_len
!= sizeof (struct sockaddr_in6
)) {
2139 error
= EAFNOSUPPORT
;
2143 /* Validate ifra_prefixmask.sin6_len is properly bounded. */
2144 if (ifra
->ifra_prefixmask
.sin6_len
== 0 ||
2145 ifra
->ifra_prefixmask
.sin6_len
> sizeof (struct sockaddr_in6
)) {
2150 /* Validate prefix length extracted from ifra_prefixmask structure. */
2151 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
2152 (u_char
*)&ifra
->ifra_prefixmask
+ ifra
->ifra_prefixmask
.sin6_len
);
2158 /* Validate lifetimes */
2159 lt
= &ifra
->ifra_lifetime
;
2160 if (lt
->ia6t_pltime
> lt
->ia6t_vltime
) {
2162 "%s: pltime 0x%x > vltime 0x%x for %s\n", __func__
,
2163 lt
->ia6t_pltime
, lt
->ia6t_vltime
,
2164 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
2168 if (lt
->ia6t_vltime
== 0) {
2170 * the following log might be noisy, but this is a typical
2171 * configuration mistake or a tool's bug.
2173 log(LOG_INFO
, "%s: valid lifetime is 0 for %s\n", __func__
,
2174 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
2178 * Before we lock the ifnet structure, we first check to see if the
2179 * address already exists. If so, then we don't allocate and link a
2182 ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
);
2187 * Validate destination address on interface types that require it.
2189 if ((ifp
->if_flags
& (IFF_LOOPBACK
|IFF_POINTOPOINT
)) != 0) {
2190 switch (ifra
->ifra_dstaddr
.sin6_family
) {
2193 /* noisy message for diagnostic purposes */
2195 "%s: prefix length < 128 with"
2196 " explicit dstaddr.\n", __func__
);
2206 error
= EAFNOSUPPORT
;
2209 } else if (ifra
->ifra_dstaddr
.sin6_family
!= AF_UNSPEC
) {
2211 "%s: dstaddr valid only on p2p and loopback interfaces.\n",
2217 timenow
= net_uptime();
2222 /* Is this the first new IPv6 address for the interface? */
2223 ifaupflags
|= IN6_IFAUPDATE_NEWADDR
;
2225 /* Allocate memory for IPv6 interface address structure. */
2226 how
= !(ifaupflags
& IN6_IFAUPDATE_NOWAIT
) ? M_WAITOK
: 0;
2227 ia
= in6_ifaddr_alloc(how
);
2236 * Initialize interface address structure.
2238 * Note well: none of these sockaddr_in6 structures contain a
2239 * valid sin6_port, sin6_flowinfo or even a sin6_scope_id field.
2240 * We still embed link-local scope identifiers at the end of an
2241 * arbitrary fe80::/32 prefix, for historical reasons. Also, the
2242 * ifa_dstaddr field is always non-NULL on point-to-point and
2243 * loopback interfaces, and conventionally points to a socket
2244 * address of AF_UNSPEC family when there is no destination.
2246 * Please enjoy the dancing sea turtle.
2248 IFA_ADDREF(ifa
); /* for this and optionally for caller */
2249 ifa
->ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
2250 if (ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
||
2251 (ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) != 0)
2252 ifa
->ifa_dstaddr
= (struct sockaddr
*)&ia
->ia_dstaddr
;
2253 ifa
->ifa_netmask
= (struct sockaddr
*)&ia
->ia_prefixmask
;
2255 ifa
->ifa_metric
= ifp
->if_metric
;
2256 ifa
->ifa_rtrequest
= nd6_rtrequest
;
2258 LIST_INIT(&ia
->ia6_memberships
);
2259 ia
->ia_addr
.sin6_family
= AF_INET6
;
2260 ia
->ia_addr
.sin6_len
= sizeof (ia
->ia_addr
);
2261 ia
->ia_addr
.sin6_addr
= ifra
->ifra_addr
.sin6_addr
;
2262 ia
->ia_prefixmask
.sin6_family
= AF_INET6
;
2263 ia
->ia_prefixmask
.sin6_len
= sizeof (ia
->ia_prefixmask
);
2264 ia
->ia_prefixmask
.sin6_addr
= ifra
->ifra_prefixmask
.sin6_addr
;
2265 error
= in6_to_kamescope(&ia
->ia_addr
, ifp
);
2268 if (ifa
->ifa_dstaddr
!= NULL
) {
2269 ia
->ia_dstaddr
= ifra
->ifra_dstaddr
;
2270 error
= in6_to_kamescope(&ia
->ia_dstaddr
, ifp
);
2275 /* Append to address chains */
2276 ifnet_lock_exclusive(ifp
);
2277 ifaupflags
|= IN6_IFAUPDATE_1STADDR
;
2278 TAILQ_FOREACH(xifa
, &ifp
->if_addrlist
, ifa_list
) {
2279 IFA_LOCK_SPIN(xifa
);
2280 if (xifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2282 ifaupflags
&= ~IN6_IFAUPDATE_1STADDR
;
2289 if_attach_ifa(ifp
, ifa
); /* holds reference for ifnet link */
2291 ifnet_lock_done(ifp
);
2293 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2294 if (in6_ifaddrs
!= NULL
) {
2295 struct in6_ifaddr
*iac
;
2296 for (iac
= in6_ifaddrs
; iac
->ia_next
!= NULL
;
2303 IFA_ADDREF(ifa
); /* hold for in6_ifaddrs link */
2304 lck_rw_done(&in6_ifaddr_rwlock
);
2307 ifaupflags
&= ~(IN6_IFAUPDATE_NEWADDR
|IN6_IFAUPDATE_1STADDR
);
2310 VERIFY(ia
!= NULL
&& ifa
== &ia
->ia_ifa
);
2314 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
2315 * to see if the address is deprecated or invalidated, but initialize
2316 * these members for applications.
2318 ia
->ia6_updatetime
= ia
->ia6_createtime
= timenow
;
2320 if (ia6_lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
)
2321 ia6_lt
.ia6t_expire
= timenow
+ ia6_lt
.ia6t_vltime
;
2323 ia6_lt
.ia6t_expire
= 0;
2324 if (ia6_lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
)
2325 ia6_lt
.ia6t_preferred
= timenow
+ ia6_lt
.ia6t_pltime
;
2327 ia6_lt
.ia6t_preferred
= 0;
2328 in6ifa_setlifetime(ia
, &ia6_lt
);
2331 * Backward compatibility - if IN6_IFF_DEPRECATED is set from the
2332 * userland, make it deprecated.
2334 if ((ia
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0) {
2335 ia
->ia6_lifetime
.ia6ti_pltime
= 0;
2336 ia
->ia6_lifetime
.ia6ti_preferred
= timenow
;
2340 * Update flag or prefix length
2343 ia
->ia6_flags
= ifra
->ifra_flags
;
2345 /* Release locks (new address available to concurrent tasks) */
2348 /* Further initialization of the interface address */
2349 error
= in6_ifinit(ifp
, ia
, ifaupflags
);
2353 /* Finish updating the address while other tasks are working with it */
2354 error
= in6_ifaupdate_aux(ia
, ifp
, ifaupflags
);
2358 /* Return success (optionally w/ address for caller). */
2360 (void) ifnet_notify_address(ifp
, AF_INET6
);
2366 VERIFY(ifa
== &ia
->ia_ifa
);
2377 in6_purgeaddr(struct ifaddr
*ifa
)
2379 struct ifnet
*ifp
= ifa
->ifa_ifp
;
2380 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*)ifa
;
2381 struct in6_multi_mship
*imm
;
2383 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2385 /* stop DAD processing */
2389 * delete route to the destination of the address being purged.
2390 * The interface must be p2p or loopback in this case.
2393 if ((ia
->ia_flags
& IFA_ROUTE
) && ia
->ia_plen
== 128) {
2397 rtf
= (ia
->ia_dstaddr
.sin6_family
== AF_INET6
) ? RTF_HOST
: 0;
2398 error
= rtinit(&(ia
->ia_ifa
), RTM_DELETE
, rtf
);
2400 log(LOG_ERR
, "in6_purgeaddr: failed to remove "
2401 "a route to the p2p destination: %s on %s, "
2403 ip6_sprintf(&ia
->ia_addr
.sin6_addr
), if_name(ifp
),
2405 /* proceed anyway... */
2408 ia
->ia_flags
&= ~IFA_ROUTE
;
2412 /* Remove ownaddr's loopback rtentry, if it exists. */
2413 in6_ifremloop(&(ia
->ia_ifa
));
2416 * leave from multicast groups we have joined for the interface
2419 while ((imm
= ia
->ia6_memberships
.lh_first
) != NULL
) {
2420 LIST_REMOVE(imm
, i6mm_chain
);
2422 in6_leavegroup(imm
);
2427 /* in6_unlink_ifa() will need exclusive access */
2428 in6_unlink_ifa(ia
, ifp
);
2429 in6_post_msg(ifp
, KEV_INET6_ADDR_DELETED
, ia
, NULL
);
2431 (void) ifnet_notify_address(ifp
, AF_INET6
);
2435 in6_unlink_ifa(struct in6_ifaddr
*ia
, struct ifnet
*ifp
)
2437 struct in6_ifaddr
*oia
;
2441 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2446 ifnet_lock_exclusive(ifp
);
2448 if (ifa
->ifa_debug
& IFD_ATTACHED
)
2449 if_detach_ifa(ifp
, ifa
);
2451 ifnet_lock_done(ifp
);
2454 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2456 if (oia
== (ia
= in6_ifaddrs
)) {
2457 in6_ifaddrs
= ia
->ia_next
;
2459 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
2462 ia
->ia_next
= oia
->ia_next
;
2465 log(LOG_NOTICE
, "%s: search failed.\n", __func__
);
2471 * When IPv6 address is being removed, release the
2472 * reference to the base prefix.
2473 * Also, since the release might, affect the status
2474 * of other (detached) addresses, call
2475 * pfxlist_onlink_check().
2480 * Only log the below message for addresses other than
2482 * Only one LLA (auto-configured or statically) is allowed
2484 * LLA prefix, while added to the prefix list, is not
2485 * reference countedi (as it is the only one).
2486 * The prefix also never expires on its own as LLAs
2487 * have infinite lifetime.
2489 * For now quiece down the log message for LLAs.
2491 if (!IN6_IS_ADDR_LINKLOCAL(&oia
->ia_addr
.sin6_addr
)) {
2492 if (oia
->ia6_ndpr
== NULL
)
2493 log(LOG_NOTICE
, "in6_unlink_ifa: IPv6 address "
2494 "0x%llx has no prefix\n",
2495 (uint64_t)VM_KERNEL_ADDRPERM(oia
));
2497 struct nd_prefix
*pr
= oia
->ia6_ndpr
;
2498 oia
->ia6_flags
&= ~IN6_IFF_AUTOCONF
;
2499 oia
->ia6_ndpr
= NULL
;
2501 VERIFY(pr
->ndpr_addrcnt
!= 0);
2504 NDPR_REMREF(pr
); /* release addr reference */
2508 lck_rw_done(&in6_ifaddr_rwlock
);
2510 if ((oia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0) {
2511 lck_mtx_lock(nd6_mutex
);
2512 pfxlist_onlink_check();
2513 lck_mtx_unlock(nd6_mutex
);
2516 * release another refcnt for the link from in6_ifaddrs.
2517 * Do this only if it's not already unlinked in the event that we lost
2518 * the race, since in6_ifaddr_rwlock was momentarily dropped above.
2523 /* release reference held for this routine */
2526 /* invalidate route caches */
2527 routegenid_inet6_update();
2531 in6_purgeif(struct ifnet
*ifp
)
2533 struct in6_ifaddr
*ia
;
2538 LCK_MTX_ASSERT(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2540 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2542 while (ia
!= NULL
) {
2543 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
2547 IFA_ADDREF(&ia
->ia_ifa
); /* for us */
2548 lck_rw_done(&in6_ifaddr_rwlock
);
2549 in6_purgeaddr(&ia
->ia_ifa
);
2550 IFA_REMREF(&ia
->ia_ifa
); /* for us */
2551 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2553 * Purging the address would have caused
2554 * in6_ifaddr_rwlock to be dropped and reacquired;
2555 * therefore search again from the beginning
2556 * of in6_ifaddrs list.
2560 lck_rw_done(&in6_ifaddr_rwlock
);
2566 * Initialize an interface's internet6 address and routing table entry.
2569 in6_ifinit(struct ifnet
*ifp
, struct in6_ifaddr
*ia
, int ifaupflags
)
2578 * NOTE: SIOCSIFADDR is defined with struct ifreq as parameter,
2579 * but here we are sending it down to the interface with a pointer
2580 * to struct ifaddr, for legacy reasons.
2582 if ((ifaupflags
& IN6_IFAUPDATE_1STADDR
) != 0) {
2583 error
= ifnet_ioctl(ifp
, PF_INET6
, SIOCSIFADDR
, ia
);
2585 if (error
!= EOPNOTSUPP
)
2595 * If the destination address is specified for a point-to-point
2596 * interface, install a route to the destination as an interface
2599 if (!(ia
->ia_flags
& IFA_ROUTE
) && ia
->ia_plen
== 128 &&
2600 ia
->ia_dstaddr
.sin6_family
== AF_INET6
) {
2602 error
= rtinit(ifa
, RTM_ADD
, RTF_UP
| RTF_HOST
);
2606 ia
->ia_flags
|= IFA_ROUTE
;
2608 IFA_LOCK_ASSERT_HELD(ifa
);
2609 if (ia
->ia_plen
< 128) {
2611 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
2613 ia
->ia_flags
|= RTF_CLONING
;
2618 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
2619 if ((ifaupflags
& IN6_IFAUPDATE_NEWADDR
) != 0)
2622 /* invalidate route caches */
2623 routegenid_inet6_update();
2630 in6_purgeaddrs(struct ifnet
*ifp
)
2636 * Find an IPv6 interface link-local address specific to an interface.
2639 in6ifa_ifpforlinklocal(struct ifnet
*ifp
, int ignoreflags
)
2643 ifnet_lock_shared(ifp
);
2644 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2647 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2651 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
))) {
2652 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2653 ignoreflags
) != 0) {
2657 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2663 ifnet_lock_done(ifp
);
2665 return ((struct in6_ifaddr
*)ifa
);
2669 * find the internet address corresponding to a given interface and address.
2672 in6ifa_ifpwithaddr(struct ifnet
*ifp
, struct in6_addr
*addr
)
2676 ifnet_lock_shared(ifp
);
2677 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2680 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2684 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(ifa
))) {
2685 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2691 ifnet_lock_done(ifp
);
2693 return ((struct in6_ifaddr
*)ifa
);
2697 in6ifa_prproxyaddr(struct in6_addr
*addr
)
2699 struct in6_ifaddr
*ia
;
2701 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2702 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2703 IFA_LOCK(&ia
->ia_ifa
);
2704 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(&ia
->ia_ifa
))) {
2705 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
2706 IFA_UNLOCK(&ia
->ia_ifa
);
2709 IFA_UNLOCK(&ia
->ia_ifa
);
2711 lck_rw_done(&in6_ifaddr_rwlock
);
2713 if (ia
!= NULL
&& !nd6_prproxy_ifaddr(ia
)) {
2714 IFA_REMREF(&ia
->ia_ifa
);
2722 in6ifa_getlifetime(struct in6_ifaddr
*ia6
, struct in6_addrlifetime
*t_dst
,
2725 struct in6_addrlifetime_i
*t_src
= &ia6
->ia6_lifetime
;
2726 struct timeval caltime
;
2728 t_dst
->ia6t_vltime
= t_src
->ia6ti_vltime
;
2729 t_dst
->ia6t_pltime
= t_src
->ia6ti_pltime
;
2730 t_dst
->ia6t_expire
= 0;
2731 t_dst
->ia6t_preferred
= 0;
2733 /* account for system time change */
2734 getmicrotime(&caltime
);
2735 t_src
->ia6ti_base_calendartime
+=
2736 NET_CALCULATE_CLOCKSKEW(caltime
,
2737 t_src
->ia6ti_base_calendartime
, net_uptime(),
2738 t_src
->ia6ti_base_uptime
);
2741 if (t_src
->ia6ti_expire
!= 0 &&
2742 t_src
->ia6ti_vltime
!= ND6_INFINITE_LIFETIME
)
2743 t_dst
->ia6t_expire
= t_src
->ia6ti_base_calendartime
+
2744 t_src
->ia6ti_expire
- t_src
->ia6ti_base_uptime
;
2746 if (t_src
->ia6ti_preferred
!= 0 &&
2747 t_src
->ia6ti_pltime
!= ND6_INFINITE_LIFETIME
)
2748 t_dst
->ia6t_preferred
= t_src
->ia6ti_base_calendartime
+
2749 t_src
->ia6ti_preferred
- t_src
->ia6ti_base_uptime
;
2751 if (t_src
->ia6ti_expire
!= 0 &&
2752 t_src
->ia6ti_vltime
!= ND6_INFINITE_LIFETIME
)
2753 t_dst
->ia6t_expire
= t_src
->ia6ti_expire
;
2755 if (t_src
->ia6ti_preferred
!= 0 &&
2756 t_src
->ia6ti_pltime
!= ND6_INFINITE_LIFETIME
)
2757 t_dst
->ia6t_preferred
= t_src
->ia6ti_preferred
;
2762 in6ifa_setlifetime(struct in6_ifaddr
*ia6
, struct in6_addrlifetime
*t_src
)
2764 struct in6_addrlifetime_i
*t_dst
= &ia6
->ia6_lifetime
;
2765 struct timeval caltime
;
2767 /* account for system time change */
2768 getmicrotime(&caltime
);
2769 t_dst
->ia6ti_base_calendartime
+=
2770 NET_CALCULATE_CLOCKSKEW(caltime
,
2771 t_dst
->ia6ti_base_calendartime
, net_uptime(),
2772 t_dst
->ia6ti_base_uptime
);
2774 /* trust the caller for the values */
2775 t_dst
->ia6ti_expire
= t_src
->ia6t_expire
;
2776 t_dst
->ia6ti_preferred
= t_src
->ia6t_preferred
;
2777 t_dst
->ia6ti_vltime
= t_src
->ia6t_vltime
;
2778 t_dst
->ia6ti_pltime
= t_src
->ia6t_pltime
;
2782 * Convert IP6 address to printable (loggable) representation.
2785 ip6_sprintf(const struct in6_addr
*addr
)
2787 static const char digits
[] = "0123456789abcdef";
2788 static int ip6round
= 0;
2789 static char ip6buf
[8][48];
2793 const u_short
*a
= (const u_short
*)addr
;
2799 ip6round
= (ip6round
+ 1) & 7;
2800 cp
= ip6buf
[ip6round
];
2802 for (i
= 0; i
< 8; i
++) {
2813 if (dcolon
== 0 && *(a
+ 1) == 0) {
2825 d
= (const u_char
*)a
;
2827 if ((n
= *d
>> 4) != 0) {
2831 if ((n
= *d
++ & 0xf) != 0 || zpad
) {
2835 if ((n
= *d
>> 4) != 0 || zpad
) {
2839 if ((n
= *d
& 0xf) != 0 || zpad
)
2845 return (ip6buf
[ip6round
]);
2849 in6addr_local(struct in6_addr
*in6
)
2852 struct sockaddr_in6 sin6
;
2855 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_SCOPE_LINKLOCAL(in6
))
2858 sin6
.sin6_family
= AF_INET6
;
2859 sin6
.sin6_len
= sizeof (sin6
);
2860 bcopy(in6
, &sin6
.sin6_addr
, sizeof (*in6
));
2861 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0);
2865 if (rt
->rt_gateway
->sa_family
== AF_LINK
)
2870 local
= in6_localaddr(in6
);
2876 in6_localaddr(struct in6_addr
*in6
)
2878 struct in6_ifaddr
*ia
;
2880 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
))
2883 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2884 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2885 IFA_LOCK_SPIN(&ia
->ia_ifa
);
2886 if (IN6_ARE_MASKED_ADDR_EQUAL(in6
, &ia
->ia_addr
.sin6_addr
,
2887 &ia
->ia_prefixmask
.sin6_addr
)) {
2888 IFA_UNLOCK(&ia
->ia_ifa
);
2889 lck_rw_done(&in6_ifaddr_rwlock
);
2892 IFA_UNLOCK(&ia
->ia_ifa
);
2894 lck_rw_done(&in6_ifaddr_rwlock
);
2899 * return length of part which dst and src are equal
2903 in6_matchlen(struct in6_addr
*src
, struct in6_addr
*dst
)
2906 u_char
*s
= (u_char
*)src
, *d
= (u_char
*)dst
;
2907 u_char
*lim
= s
+ 16, r
;
2910 if ((r
= (*d
++ ^ *s
++)) != 0) {
2921 /* XXX: to be scope conscious */
2923 in6_are_prefix_equal(struct in6_addr
*p1
, struct in6_addr
*p2
, int len
)
2925 int bytelen
, bitlen
;
2928 if (0 > len
|| len
> 128) {
2929 log(LOG_ERR
, "%s: invalid prefix length(%d)\n", __func__
, len
);
2936 if (bcmp(&p1
->s6_addr
, &p2
->s6_addr
, bytelen
))
2939 p1
->s6_addr
[bytelen
] >> (8 - bitlen
) !=
2940 p2
->s6_addr
[bytelen
] >> (8 - bitlen
))
2947 in6_prefixlen2mask(struct in6_addr
*maskp
, int len
)
2949 u_char maskarray
[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2950 int bytelen
, bitlen
, i
;
2953 if (0 > len
|| len
> 128) {
2954 log(LOG_ERR
, "%s: invalid prefix length(%d)\n", __func__
, len
);
2958 bzero(maskp
, sizeof (*maskp
));
2961 for (i
= 0; i
< bytelen
; i
++)
2962 maskp
->s6_addr
[i
] = 0xff;
2964 maskp
->s6_addr
[bytelen
] = maskarray
[bitlen
- 1];
2968 * return the best address out of the same scope
2971 in6_ifawithscope(struct ifnet
*oifp
, struct in6_addr
*dst
)
2973 int dst_scope
= in6_addrscope(dst
), src_scope
, best_scope
= 0;
2977 struct in6_ifaddr
*ifa_best
= NULL
;
2984 * We search for all addresses on all interfaces from the beginning.
2985 * Comparing an interface with the outgoing interface will be done
2986 * only at the final stage of tiebreaking.
2988 ifnet_head_lock_shared();
2989 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
2991 * We can never take an address that breaks the scope zone
2992 * of the destination.
2994 if (in6_addr2scopeid(ifp
, dst
) != in6_addr2scopeid(oifp
, dst
))
2997 ifnet_lock_shared(ifp
);
2998 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
2999 int tlen
= -1, dscopecmp
, bscopecmp
, matchcmp
;
3002 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3006 src_scope
= in6_addrscope(IFA_IN6(ifa
));
3009 * Don't use an address before completing DAD
3010 * nor a duplicated address.
3012 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3017 /* XXX: is there any case to allow anycasts? */
3018 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3023 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3029 * If this is the first address we find,
3032 if (ifa_best
== NULL
)
3036 * ifa_best is never NULL beyond this line except
3037 * within the block labeled "replace".
3041 * If ifa_best has a smaller scope than dst and
3042 * the current address has a larger one than
3043 * (or equal to) dst, always replace ifa_best.
3044 * Also, if the current address has a smaller scope
3045 * than dst, ignore it unless ifa_best also has a
3047 * Consequently, after the two if-clause below,
3048 * the followings must be satisfied:
3049 * (scope(src) < scope(dst) &&
3050 * scope(best) < scope(dst))
3052 * (scope(best) >= scope(dst) &&
3053 * scope(src) >= scope(dst))
3055 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0 &&
3056 IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) >= 0)
3057 goto replace
; /* (A) */
3058 if (IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) < 0 &&
3059 IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) >= 0) {
3064 * A deprecated address SHOULD NOT be used in new
3065 * communications if an alternate (non-deprecated)
3066 * address is available and has sufficient scope.
3067 * RFC 4862, Section 5.5.4.
3069 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3070 IN6_IFF_DEPRECATED
) {
3072 * Ignore any deprecated addresses if
3073 * specified by configuration.
3075 if (!ip6_use_deprecated
) {
3080 * If we have already found a non-deprecated
3081 * candidate, just ignore deprecated addresses.
3083 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
)
3091 * A non-deprecated address is always preferred
3092 * to a deprecated one regardless of scopes and
3093 * address matching (Note invariants ensured by the
3094 * conditions (A) and (B) above.)
3096 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) &&
3097 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3098 IN6_IFF_DEPRECATED
) == 0)
3102 * When we use temporary addresses described in
3103 * RFC 4941, we prefer temporary addresses to
3104 * public autoconf addresses. Again, note the
3105 * invariants from (A) and (B). Also note that we
3106 * don't have any preference between static addresses
3107 * and autoconf addresses (despite of whether or not
3108 * the latter is temporary or public.)
3110 if (ip6_use_tempaddr
) {
3111 struct in6_ifaddr
*ifat
;
3113 ifat
= (struct in6_ifaddr
*)ifa
;
3114 if ((ifa_best
->ia6_flags
&
3115 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3116 == IN6_IFF_AUTOCONF
&&
3118 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3119 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
)) {
3122 if ((ifa_best
->ia6_flags
&
3123 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3124 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
) &&
3126 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3127 == IN6_IFF_AUTOCONF
) {
3134 * At this point, we have two cases:
3135 * 1. we are looking at a non-deprecated address,
3136 * and ifa_best is also non-deprecated.
3137 * 2. we are looking at a deprecated address,
3138 * and ifa_best is also deprecated.
3139 * Also, we do not have to consider a case where
3140 * the scope of if_best is larger(smaller) than dst and
3141 * the scope of the current address is smaller(larger)
3142 * than dst. Such a case has already been covered.
3143 * Tiebreaking is done according to the following
3145 * - the scope comparison between the address and
3147 * - the scope comparison between the address and
3148 * ifa_best (bscopecmp)
3149 * - if the address match dst longer than ifa_best
3151 * - if the address is on the outgoing I/F (outI/F)
3153 * Roughly speaking, the selection policy is
3154 * - the most important item is scope. The same scope
3155 * is best. Then search for a larger scope.
3156 * Smaller scopes are the last resort.
3157 * - A deprecated address is chosen only when we have
3158 * no address that has an enough scope, but is
3159 * prefered to any addresses of smaller scopes
3160 * (this must be already done above.)
3161 * - addresses on the outgoing I/F are preferred to
3162 * ones on other interfaces if none of above
3163 * tiebreaks. In the table below, the column "bI"
3164 * means if the best_ifa is on the outgoing
3165 * interface, and the column "sI" means if the ifa
3166 * is on the outgoing interface.
3167 * - If there is no other reasons to choose one,
3168 * longest address match against dst is considered.
3170 * The precise decision table is as follows:
3171 * dscopecmp bscopecmp match bI oI | replace?
3172 * N/A equal N/A Y N | No (1)
3173 * N/A equal N/A N Y | Yes (2)
3174 * N/A equal larger N/A | Yes (3)
3175 * N/A equal !larger N/A | No (4)
3176 * larger larger N/A N/A | No (5)
3177 * larger smaller N/A N/A | Yes (6)
3178 * smaller larger N/A N/A | Yes (7)
3179 * smaller smaller N/A N/A | No (8)
3180 * equal smaller N/A N/A | Yes (9)
3181 * equal larger (already done at A above)
3183 dscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
);
3184 bscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, best_scope
);
3186 if (bscopecmp
== 0) {
3187 struct ifnet
*bifp
= ifa_best
->ia_ifp
;
3189 if (bifp
== oifp
&& ifp
!= oifp
) { /* (1) */
3193 if (bifp
!= oifp
&& ifp
== oifp
) /* (2) */
3197 * Both bifp and ifp are on the outgoing
3198 * interface, or both two are on a different
3199 * interface from the outgoing I/F.
3200 * now we need address matching against dst
3203 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3204 matchcmp
= tlen
- blen
;
3205 if (matchcmp
> 0) /* (3) */
3210 if (dscopecmp
> 0) {
3211 if (bscopecmp
> 0) { /* (5) */
3215 goto replace
; /* (6) */
3217 if (dscopecmp
< 0) {
3218 if (bscopecmp
> 0) /* (7) */
3224 /* now dscopecmp must be 0 */
3226 goto replace
; /* (9) */
3229 IFA_ADDREF_LOCKED(ifa
); /* for ifa_best */
3230 blen
= tlen
>= 0 ? tlen
:
3231 in6_matchlen(IFA_IN6(ifa
), dst
);
3233 in6_addrscope(&ifa2ia6(ifa
)->ia_addr
.sin6_addr
);
3236 IFA_REMREF(&ifa_best
->ia_ifa
);
3237 ifa_best
= (struct in6_ifaddr
*)ifa
;
3239 ifnet_lock_done(ifp
);
3243 /* count statistics for future improvements */
3244 if (ifa_best
== NULL
)
3245 ip6stat
.ip6s_sources_none
++;
3247 IFA_LOCK_SPIN(&ifa_best
->ia_ifa
);
3248 if (oifp
== ifa_best
->ia_ifp
)
3249 ip6stat
.ip6s_sources_sameif
[best_scope
]++;
3251 ip6stat
.ip6s_sources_otherif
[best_scope
]++;
3253 if (best_scope
== dst_scope
)
3254 ip6stat
.ip6s_sources_samescope
[best_scope
]++;
3256 ip6stat
.ip6s_sources_otherscope
[best_scope
]++;
3258 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0)
3259 ip6stat
.ip6s_sources_deprecated
[best_scope
]++;
3260 IFA_UNLOCK(&ifa_best
->ia_ifa
);
3267 * return the best address out of the same scope. if no address was
3268 * found, return the first valid address from designated IF.
3271 in6_ifawithifp(struct ifnet
*ifp
, struct in6_addr
*dst
)
3273 int dst_scope
= in6_addrscope(dst
), blen
= -1, tlen
;
3275 struct in6_ifaddr
*besta
= NULL
;
3276 struct in6_ifaddr
*dep
[2]; /* last-resort: deprecated */
3278 dep
[0] = dep
[1] = NULL
;
3281 * We first look for addresses in the same scope.
3282 * If there is one, return it.
3283 * If two or more, return one which matches the dst longest.
3284 * If none, return one of global addresses assigned other ifs.
3286 ifnet_lock_shared(ifp
);
3287 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3289 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3293 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3295 continue; /* XXX: is there any case to allow anycast? */
3297 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_NOTREADY
) {
3299 continue; /* don't use this interface */
3301 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3305 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3306 if (ip6_use_deprecated
) {
3307 IFA_ADDREF_LOCKED(ifa
); /* for dep[0] */
3310 IFA_REMREF(&dep
[0]->ia_ifa
);
3311 dep
[0] = (struct in6_ifaddr
*)ifa
;
3318 if (dst_scope
== in6_addrscope(IFA_IN6(ifa
))) {
3320 * call in6_matchlen() as few as possible
3325 IFA_LOCK(&besta
->ia_ifa
);
3326 blen
= in6_matchlen(
3327 &besta
->ia_addr
.sin6_addr
, dst
);
3328 IFA_UNLOCK(&besta
->ia_ifa
);
3331 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3334 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3336 IFA_REMREF(&besta
->ia_ifa
);
3337 besta
= (struct in6_ifaddr
*)ifa
;
3342 besta
= (struct in6_ifaddr
*)ifa
;
3343 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3351 ifnet_lock_done(ifp
);
3353 IFA_REMREF(&dep
[0]->ia_ifa
);
3357 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3359 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3363 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3365 continue; /* XXX: is there any case to allow anycast? */
3367 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_NOTREADY
) {
3369 continue; /* don't use this interface */
3371 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3375 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3376 if (ip6_use_deprecated
) {
3377 IFA_ADDREF_LOCKED(ifa
); /* for dep[1] */
3380 IFA_REMREF(&dep
[1]->ia_ifa
);
3381 dep
[1] = (struct in6_ifaddr
*)ifa
;
3387 IFA_ADDREF_LOCKED(ifa
); /* for caller */
3389 ifnet_lock_done(ifp
);
3391 IFA_REMREF(&dep
[0]->ia_ifa
);
3393 IFA_REMREF(&dep
[1]->ia_ifa
);
3394 return ((struct in6_ifaddr
*)ifa
);
3396 ifnet_lock_done(ifp
);
3398 /* use the last-resort values, that are, deprecated addresses */
3401 IFA_REMREF(&dep
[1]->ia_ifa
);
3411 * perform DAD when interface becomes IFF_UP.
3414 in6_if_up_dad_start(struct ifnet
*ifp
)
3417 struct nd_ifinfo
*ndi
= NULL
;
3419 ndi
= ND_IFINFO(ifp
);
3420 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3421 if (!(ndi
->flags
& ND6_IFF_DAD
))
3424 /* start DAD on all the interface addresses */
3425 ifnet_lock_exclusive(ifp
);
3426 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
) {
3427 struct in6_ifaddr
*ia6
;
3430 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3434 ia6
= (struct in6_ifaddr
*)ifa
;
3435 if (ia6
->ia6_flags
& IN6_IFF_DADPROGRESS
) {
3436 int delay
= 0; /* delay ticks before DAD output */
3438 nd6_dad_start(ifa
, &delay
);
3443 ifnet_lock_done(ifp
);
3450 struct nd_ifinfo
*ndi
= NULL
;
3452 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0)
3455 ndi
= ND_IFINFO(ifp
);
3456 VERIFY((NULL
!= ndi
) && (TRUE
== ndi
->initialized
));
3457 if (!(ndi
->flags
& ND6_IFF_DAD
))
3461 * If we are using the alternative neighbor discovery
3462 * interface on this interface, then skip DAD.
3464 * Also, skip it for interfaces marked "local private"
3465 * for now, even when not marked as using the alternative
3466 * interface. This is for historical reasons.
3468 if (ifp
->if_eflags
&
3469 (IFEF_IPV6_ND6ALT
|IFEF_LOCALNET_PRIVATE
|IFEF_DIRECTLINK
))
3472 if (ifp
->if_subfamily
== IFNET_SUBFAMILY_IPSEC
||
3473 ifp
->if_subfamily
== IFNET_SUBFAMILY_UTUN
) {
3475 * Ignore DAD for tunneling virtual interfaces, which get
3476 * their IPv6 address explicitly assigned.
3481 switch (ifp
->if_type
) {
3487 * These interfaces do not have the IFF_LOOPBACK flag,
3488 * but loop packets back. We do not have to do DAD on such
3489 * interfaces. We should even omit it, because loop-backed
3490 * NS would confuse the DAD procedure.
3495 * Our DAD routine requires the interface up and running.
3496 * However, some interfaces can be up before the RUNNING
3497 * status. Additionaly, users may try to assign addresses
3498 * before the interface becomes up (or running).
3499 * We simply skip DAD in such a case as a work around.
3500 * XXX: we should rather mark "tentative" on such addresses,
3501 * and do DAD after the interface becomes ready.
3503 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) !=
3504 (IFF_UP
|IFF_RUNNING
))
3512 * Calculate max IPv6 MTU through all the interfaces and store it
3518 u_int32_t maxmtu
= 0;
3521 ifnet_head_lock_shared();
3522 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
3523 struct nd_ifinfo
*ndi
= NULL
;
3525 if ((ndi
= ND_IFINFO(ifp
)) != NULL
&& !ndi
->initialized
)
3528 lck_mtx_lock(&ndi
->lock
);
3529 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
3530 IN6_LINKMTU(ifp
) > maxmtu
)
3531 maxmtu
= IN6_LINKMTU(ifp
);
3533 lck_mtx_unlock(&ndi
->lock
);
3536 if (maxmtu
) /* update only when maxmtu is positive */
3537 in6_maxmtu
= maxmtu
;
3540 * Provide the length of interface identifiers to be used for the link attached
3541 * to the given interface. The length should be defined in "IPv6 over
3542 * xxx-link" document. Note that address architecture might also define
3543 * the length for a particular set of address prefixes, regardless of the
3544 * link type. Also see RFC 4862 for additional background.
3547 in6_if2idlen(struct ifnet
*ifp
)
3549 switch (ifp
->if_type
) {
3550 case IFT_ETHER
: /* RFC2464 */
3551 case IFT_IEEE8023ADLAG
: /* IEEE802.3ad Link Aggregate */
3552 #ifdef IFT_PROPVIRTUAL
3553 case IFT_PROPVIRTUAL
: /* XXX: no RFC. treat it as ether */
3556 case IFT_L2VLAN
: /* ditto */
3558 #ifdef IFT_IEEE80211
3559 case IFT_IEEE80211
: /* ditto */
3562 case IFT_MIP
: /* ditto */
3565 case IFT_FDDI
: /* RFC2467 */
3567 case IFT_ISO88025
: /* RFC2470 (IPv6 over Token Ring) */
3569 case IFT_PPP
: /* RFC2472 */
3571 case IFT_ARCNET
: /* RFC2497 */
3573 case IFT_FRELAY
: /* RFC2590 */
3575 case IFT_IEEE1394
: /* RFC3146 */
3578 return (64); /* draft-ietf-v6ops-mech-v2-07 */
3580 return (64); /* XXX: is this really correct? */
3582 return (64); /* for utun interfaces */
3584 return (64); /* Packet Data over Cellular */
3586 return (64); /* Transparent bridge interface */
3589 * Unknown link type:
3590 * It might be controversial to use the today's common constant
3591 * of 64 for these cases unconditionally. For full compliance,
3592 * we should return an error in this case. On the other hand,
3593 * if we simply miss the standard for the link type or a new
3594 * standard is defined for a new link type, the IFID length
3595 * is very likely to be the common constant. As a compromise,
3596 * we always use the constant, but make an explicit notice
3597 * indicating the "unknown" case.
3599 log(LOG_NOTICE
, "%s: unknown link type (%d)\n", __func__
,
3605 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
3606 * v4 mapped addr or v4 compat addr
3609 in6_sin6_2_sin(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3611 bzero(sin
, sizeof (*sin
));
3612 sin
->sin_len
= sizeof (struct sockaddr_in
);
3613 sin
->sin_family
= AF_INET
;
3614 sin
->sin_port
= sin6
->sin6_port
;
3615 sin
->sin_addr
.s_addr
= sin6
->sin6_addr
.s6_addr32
[3];
3618 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
3620 in6_sin_2_v4mapsin6(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3622 bzero(sin6
, sizeof (*sin6
));
3623 sin6
->sin6_len
= sizeof (struct sockaddr_in6
);
3624 sin6
->sin6_family
= AF_INET6
;
3625 sin6
->sin6_port
= sin
->sin_port
;
3626 sin6
->sin6_addr
.s6_addr32
[0] = 0;
3627 sin6
->sin6_addr
.s6_addr32
[1] = 0;
3628 if (sin
->sin_addr
.s_addr
) {
3629 sin6
->sin6_addr
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
3630 sin6
->sin6_addr
.s6_addr32
[3] = sin
->sin_addr
.s_addr
;
3632 sin6
->sin6_addr
.s6_addr32
[2] = 0;
3633 sin6
->sin6_addr
.s6_addr32
[3] = 0;
3637 /* Convert sockaddr_in6 into sockaddr_in. */
3639 in6_sin6_2_sin_in_sock(struct sockaddr
*nam
)
3641 struct sockaddr_in
*sin_p
;
3642 struct sockaddr_in6 sin6
;
3645 * Save original sockaddr_in6 addr and convert it
3648 sin6
= *(struct sockaddr_in6
*)(void *)nam
;
3649 sin_p
= (struct sockaddr_in
*)(void *)nam
;
3650 in6_sin6_2_sin(sin_p
, &sin6
);
3653 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
3655 in6_sin_2_v4mapsin6_in_sock(struct sockaddr
**nam
)
3657 struct sockaddr_in
*sin_p
;
3658 struct sockaddr_in6
*sin6_p
;
3660 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof (*sin6_p
), M_SONAME
,
3664 sin_p
= (struct sockaddr_in
*)(void *)*nam
;
3665 in6_sin_2_v4mapsin6(sin_p
, sin6_p
);
3666 FREE(*nam
, M_SONAME
);
3667 *nam
= (struct sockaddr
*)sin6_p
;
3673 * Posts in6_event_data message kernel events.
3675 * To get the same size of kev_in6_data between ILP32 and LP64 data models
3676 * we are using a special version of the in6_addrlifetime structure that
3677 * uses only 32 bits fields to be compatible with Leopard, and that
3678 * are large enough to span 68 years.
3681 in6_post_msg(struct ifnet
*ifp
, u_int32_t event_code
, struct in6_ifaddr
*ifa
,
3684 struct kev_msg ev_msg
;
3685 struct kev_in6_data in6_event_data
;
3686 struct in6_addrlifetime ia6_lt
;
3688 bzero(&in6_event_data
, sizeof (struct kev_in6_data
));
3689 bzero(&ev_msg
, sizeof (struct kev_msg
));
3690 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3691 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3692 ev_msg
.kev_subclass
= KEV_INET6_SUBCLASS
;
3693 ev_msg
.event_code
= event_code
;
3696 IFA_LOCK(&ifa
->ia_ifa
);
3697 in6_event_data
.ia_addr
= ifa
->ia_addr
;
3698 in6_event_data
.ia_net
= ifa
->ia_net
;
3699 in6_event_data
.ia_dstaddr
= ifa
->ia_dstaddr
;
3700 in6_event_data
.ia_prefixmask
= ifa
->ia_prefixmask
;
3701 in6_event_data
.ia_plen
= ifa
->ia_plen
;
3702 in6_event_data
.ia6_flags
= (u_int32_t
)ifa
->ia6_flags
;
3704 /* retrieve time as calendar time (last arg is 1) */
3705 in6ifa_getlifetime(ifa
, &ia6_lt
, 1);
3706 in6_event_data
.ia_lifetime
.ia6t_expire
= ia6_lt
.ia6t_expire
;
3707 in6_event_data
.ia_lifetime
.ia6t_preferred
= ia6_lt
.ia6t_preferred
;
3708 in6_event_data
.ia_lifetime
.ia6t_vltime
= ia6_lt
.ia6t_vltime
;
3709 in6_event_data
.ia_lifetime
.ia6t_pltime
= ia6_lt
.ia6t_pltime
;
3710 IFA_UNLOCK(&ifa
->ia_ifa
);
3714 (void) strlcpy(&in6_event_data
.link_data
.if_name
[0],
3715 ifp
->if_name
, IFNAMSIZ
);
3716 in6_event_data
.link_data
.if_family
= ifp
->if_family
;
3717 in6_event_data
.link_data
.if_unit
= (u_int32_t
)ifp
->if_unit
;
3721 memcpy(&in6_event_data
.ia_mac
, mac
,
3722 sizeof(in6_event_data
.ia_mac
));
3724 ev_msg
.dv
[0].data_ptr
= &in6_event_data
;
3725 ev_msg
.dv
[0].data_length
= sizeof (in6_event_data
);
3726 ev_msg
.dv
[1].data_length
= 0;
3728 dlil_post_complete_msg(NULL
, &ev_msg
);
3732 * Called as part of ip6_init
3735 in6_ifaddr_init(void)
3740 PE_parse_boot_argn("ifa_debug", &in6ifa_debug
, sizeof (in6ifa_debug
));
3742 in6ifa_size
= (in6ifa_debug
== 0) ? sizeof (struct in6_ifaddr
) :
3743 sizeof (struct in6_ifaddr_dbg
);
3745 in6ifa_zone
= zinit(in6ifa_size
, IN6IFA_ZONE_MAX
* in6ifa_size
,
3746 0, IN6IFA_ZONE_NAME
);
3747 if (in6ifa_zone
== NULL
) {
3748 panic("%s: failed allocating %s", __func__
, IN6IFA_ZONE_NAME
);
3751 zone_change(in6ifa_zone
, Z_EXPAND
, TRUE
);
3752 zone_change(in6ifa_zone
, Z_CALLERACCT
, FALSE
);
3754 lck_mtx_init(&in6ifa_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3755 TAILQ_INIT(&in6ifa_trash_head
);
3758 static struct in6_ifaddr
*
3759 in6_ifaddr_alloc(int how
)
3761 struct in6_ifaddr
*in6ifa
;
3763 in6ifa
= (how
== M_WAITOK
) ? zalloc(in6ifa_zone
) :
3764 zalloc_noblock(in6ifa_zone
);
3765 if (in6ifa
!= NULL
) {
3766 bzero(in6ifa
, in6ifa_size
);
3767 in6ifa
->ia_ifa
.ifa_free
= in6_ifaddr_free
;
3768 in6ifa
->ia_ifa
.ifa_debug
|= IFD_ALLOC
;
3769 ifa_lock_init(&in6ifa
->ia_ifa
);
3770 if (in6ifa_debug
!= 0) {
3771 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3772 (struct in6_ifaddr_dbg
*)in6ifa
;
3773 in6ifa
->ia_ifa
.ifa_debug
|= IFD_DEBUG
;
3774 in6ifa
->ia_ifa
.ifa_trace
= in6_ifaddr_trace
;
3775 in6ifa
->ia_ifa
.ifa_attached
= in6_ifaddr_attached
;
3776 in6ifa
->ia_ifa
.ifa_detached
= in6_ifaddr_detached
;
3777 ctrace_record(&in6ifa_dbg
->in6ifa_alloc
);
3785 in6_ifaddr_free(struct ifaddr
*ifa
)
3787 IFA_LOCK_ASSERT_HELD(ifa
);
3789 if (ifa
->ifa_refcnt
!= 0) {
3790 panic("%s: ifa %p bad ref cnt", __func__
, ifa
);
3792 } else if (!(ifa
->ifa_debug
& IFD_ALLOC
)) {
3793 panic("%s: ifa %p cannot be freed", __func__
, ifa
);
3796 if (ifa
->ifa_debug
& IFD_DEBUG
) {
3797 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3798 (struct in6_ifaddr_dbg
*)ifa
;
3799 ctrace_record(&in6ifa_dbg
->in6ifa_free
);
3800 bcopy(&in6ifa_dbg
->in6ifa
, &in6ifa_dbg
->in6ifa_old
,
3801 sizeof (struct in6_ifaddr
));
3802 if (ifa
->ifa_debug
& IFD_TRASHED
) {
3803 /* Become a regular mutex, just in case */
3804 IFA_CONVERT_LOCK(ifa
);
3805 lck_mtx_lock(&in6ifa_trash_lock
);
3806 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
,
3808 lck_mtx_unlock(&in6ifa_trash_lock
);
3809 ifa
->ifa_debug
&= ~IFD_TRASHED
;
3813 ifa_lock_destroy(ifa
);
3814 bzero(ifa
, sizeof (struct in6_ifaddr
));
3815 zfree(in6ifa_zone
, ifa
);
3819 in6_ifaddr_attached(struct ifaddr
*ifa
)
3821 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3823 IFA_LOCK_ASSERT_HELD(ifa
);
3825 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3826 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3829 if (ifa
->ifa_debug
& IFD_TRASHED
) {
3830 /* Become a regular mutex, just in case */
3831 IFA_CONVERT_LOCK(ifa
);
3832 lck_mtx_lock(&in6ifa_trash_lock
);
3833 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
3834 lck_mtx_unlock(&in6ifa_trash_lock
);
3835 ifa
->ifa_debug
&= ~IFD_TRASHED
;
3840 in6_ifaddr_detached(struct ifaddr
*ifa
)
3842 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3844 IFA_LOCK_ASSERT_HELD(ifa
);
3846 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3847 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3849 } else if (ifa
->ifa_debug
& IFD_TRASHED
) {
3850 panic("%s: ifa %p is already in trash list", __func__
, ifa
);
3853 ifa
->ifa_debug
|= IFD_TRASHED
;
3854 /* Become a regular mutex, just in case */
3855 IFA_CONVERT_LOCK(ifa
);
3856 lck_mtx_lock(&in6ifa_trash_lock
);
3857 TAILQ_INSERT_TAIL(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
3858 lck_mtx_unlock(&in6ifa_trash_lock
);
3862 in6_ifaddr_trace(struct ifaddr
*ifa
, int refhold
)
3864 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3869 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3870 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3874 cnt
= &in6ifa_dbg
->in6ifa_refhold_cnt
;
3875 tr
= in6ifa_dbg
->in6ifa_refhold
;
3877 cnt
= &in6ifa_dbg
->in6ifa_refrele_cnt
;
3878 tr
= in6ifa_dbg
->in6ifa_refrele
;
3881 idx
= atomic_add_16_ov(cnt
, 1) % IN6IFA_TRACE_HIST_SIZE
;
3882 ctrace_record(&tr
[idx
]);
3886 * Handle SIOCGASSOCIDS ioctl for PF_INET6 domain.
3889 in6_getassocids(struct socket
*so
, uint32_t *cnt
, user_addr_t aidp
)
3891 struct in6pcb
*in6p
= sotoin6pcb(so
);
3894 if (in6p
== NULL
|| in6p
->inp_state
== INPCB_STATE_DEAD
)
3897 /* IN6PCB has no concept of association */
3898 aid
= SAE_ASSOCID_ANY
;
3901 /* just asking how many there are? */
3902 if (aidp
== USER_ADDR_NULL
)
3905 return (copyout(&aid
, aidp
, sizeof (aid
)));
3909 * Handle SIOCGCONNIDS ioctl for PF_INET6 domain.
3912 in6_getconnids(struct socket
*so
, sae_associd_t aid
, uint32_t *cnt
,
3915 struct in6pcb
*in6p
= sotoin6pcb(so
);
3918 if (in6p
== NULL
|| in6p
->inp_state
== INPCB_STATE_DEAD
)
3921 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
)
3924 /* if connected, return 1 connection count */
3925 *cnt
= ((so
->so_state
& SS_ISCONNECTED
) ? 1 : 0);
3927 /* just asking how many there are? */
3928 if (cidp
== USER_ADDR_NULL
)
3931 /* if IN6PCB is connected, assign it connid 1 */
3932 cid
= ((*cnt
!= 0) ? 1 : SAE_CONNID_ANY
);
3934 return (copyout(&cid
, cidp
, sizeof (cid
)));
3938 * Handle SIOCGCONNINFO ioctl for PF_INET6 domain.
3941 in6_getconninfo(struct socket
*so
, sae_connid_t cid
, uint32_t *flags
,
3942 uint32_t *ifindex
, int32_t *soerror
, user_addr_t src
, socklen_t
*src_len
,
3943 user_addr_t dst
, socklen_t
*dst_len
, uint32_t *aux_type
,
3944 user_addr_t aux_data
, uint32_t *aux_len
)
3946 struct in6pcb
*in6p
= sotoin6pcb(so
);
3947 struct sockaddr_in6 sin6
;
3948 struct ifnet
*ifp
= NULL
;
3950 u_int32_t copy_len
= 0;
3953 * Don't test for INPCB_STATE_DEAD since this may be called
3954 * after SOF_PCBCLEARING is set, e.g. after tcp_close().
3961 if (cid
!= SAE_CONNID_ANY
&& cid
!= SAE_CONNID_ALL
&& cid
!= 1) {
3966 ifp
= in6p
->in6p_last_outifp
;
3967 *ifindex
= ((ifp
!= NULL
) ? ifp
->if_index
: 0);
3968 *soerror
= so
->so_error
;
3970 if (so
->so_state
& SS_ISCONNECTED
)
3971 *flags
|= (CIF_CONNECTED
| CIF_PREFERRED
);
3972 if (in6p
->in6p_flags
& INP_BOUND_IF
)
3973 *flags
|= CIF_BOUND_IF
;
3974 if (!(in6p
->in6p_flags
& INP_IN6ADDR_ANY
))
3975 *flags
|= CIF_BOUND_IP
;
3976 if (!(in6p
->in6p_flags
& INP_ANONPORT
))
3977 *flags
|= CIF_BOUND_PORT
;
3979 bzero(&sin6
, sizeof (sin6
));
3980 sin6
.sin6_len
= sizeof (sin6
);
3981 sin6
.sin6_family
= AF_INET6
;
3983 /* source address and port */
3984 sin6
.sin6_port
= in6p
->in6p_lport
;
3985 in6_recoverscope(&sin6
, &in6p
->in6p_laddr
, NULL
);
3986 if (*src_len
== 0) {
3987 *src_len
= sin6
.sin6_len
;
3989 if (src
!= USER_ADDR_NULL
) {
3990 copy_len
= min(*src_len
, sizeof (sin6
));
3991 error
= copyout(&sin6
, src
, copy_len
);
3994 *src_len
= copy_len
;
3998 /* destination address and port */
3999 sin6
.sin6_port
= in6p
->in6p_fport
;
4000 in6_recoverscope(&sin6
, &in6p
->in6p_faddr
, NULL
);
4001 if (*dst_len
== 0) {
4002 *dst_len
= sin6
.sin6_len
;
4004 if (dst
!= USER_ADDR_NULL
) {
4005 copy_len
= min(*dst_len
, sizeof (sin6
));
4006 error
= copyout(&sin6
, dst
, copy_len
);
4009 *dst_len
= copy_len
;
4013 if (SOCK_PROTO(so
) == IPPROTO_TCP
) {
4014 struct conninfo_tcp tcp_ci
;
4016 *aux_type
= CIAUX_TCP
;
4017 if (*aux_len
== 0) {
4018 *aux_len
= sizeof (tcp_ci
);
4020 if (aux_data
!= USER_ADDR_NULL
) {
4021 copy_len
= min(*aux_len
, sizeof (tcp_ci
));
4022 bzero(&tcp_ci
, sizeof (tcp_ci
));
4023 tcp_getconninfo(so
, &tcp_ci
);
4024 error
= copyout(&tcp_ci
, aux_data
, copy_len
);
4027 *aux_len
= copy_len
;
4042 * The switch statement below does nothing at runtime, as it serves as a
4043 * compile time check to ensure that all of the socket 'u' ioctls (those
4044 * in the 'u' group going thru soo_ioctl) that are made available by the
4045 * networking stack is unique. This works as long as this routine gets
4046 * updated each time a new interface ioctl gets added.
4048 * Any failures at compile time indicates duplicated ioctl values.
4050 static __attribute__((unused
)) void
4051 in6ioctl_cassert(void)
4054 * This is equivalent to _CASSERT() and the compiler wouldn't
4055 * generate any instructions, thus for compile time only.
4057 switch ((u_long
)0) {
4060 /* bsd/netinet6/in6_var.h */
4061 case SIOCAADDRCTL_POLICY
:
4062 case SIOCDADDRCTL_POLICY
:
4063 case SIOCDRADD_IN6_32
:
4064 case SIOCDRADD_IN6_64
:
4065 case SIOCDRDEL_IN6_32
:
4066 case SIOCDRDEL_IN6_64
:
4071 struct in6_llentry
{
4072 struct llentry base
;
4075 #define IN6_LLTBL_DEFAULT_HSIZE 32
4076 #define IN6_LLTBL_HASH(k, h) \
4077 ((((((((k) >> 8) ^ (k)) >> 8) ^ (k)) >> 8) ^ (k)) & ((h) - 1))
4080 * Do actual deallocation of @lle.
4083 in6_lltable_destroy_lle_unlocked(struct llentry
*lle
)
4085 LLE_LOCK_DESTROY(lle
);
4086 LLE_REQ_DESTROY(lle
);
4087 FREE(lle
, M_LLTABLE
);
4091 * Called by LLE_FREE_LOCKED when number of references
4095 in6_lltable_destroy_lle(struct llentry
*lle
)
4099 //thread_call_free(lle->lle_timer);
4100 in6_lltable_destroy_lle_unlocked(lle
);
4104 static struct llentry
*
4105 in6_lltable_new(const struct in6_addr
*addr6
, u_int flags
)
4107 #pragma unused(flags)
4108 struct in6_llentry
*lle
;
4110 MALLOC(lle
, struct in6_llentry
*, sizeof(struct in6_llentry
), M_LLTABLE
, M_NOWAIT
| M_ZERO
);
4111 if (lle
== NULL
) /* NB: caller generates msg */
4114 lle
->base
.r_l3addr
.addr6
= *addr6
;
4115 lle
->base
.lle_refcnt
= 1;
4116 lle
->base
.lle_free
= in6_lltable_destroy_lle
;
4117 LLE_LOCK_INIT(&lle
->base
);
4118 LLE_REQ_INIT(&lle
->base
);
4121 lle
->base
.lle_timer
= thread_call_allocate(nd6_llinfo_timer
, lle
);
4123 if (lle
->base
.lle_timer
== NULL
) {
4124 printf("lle_timer thread call could not be allocated.\n");
4125 LLE_LOCK_DESTROY(&lle
->base
);
4126 LLE_REQ_DESTROY(&lle
->base
);
4127 FREE(lle
, M_LLTABLE
);
4131 return (&lle
->base
);
4135 in6_lltable_match_prefix(const struct sockaddr
*saddr
,
4136 const struct sockaddr
*smask
, u_int flags
, struct llentry
*lle
)
4138 const struct in6_addr
*addr
, *mask
, *lle_addr
;
4140 addr
= &((const struct sockaddr_in6
*)(const void *)saddr
)->sin6_addr
;
4141 mask
= &((const struct sockaddr_in6
*)(const void *)smask
)->sin6_addr
;
4142 lle_addr
= &lle
->r_l3addr
.addr6
;
4144 if (IN6_ARE_MASKED_ADDR_EQUAL(lle_addr
, addr
, mask
) == 0)
4147 if (lle
->la_flags
& LLE_IFADDR
) {
4149 * Delete LLE_IFADDR records IFF address & flag matches.
4150 * Note that addr is the interface address within prefix
4153 if (IN6_ARE_ADDR_EQUAL(addr
, lle_addr
) &&
4154 (flags
& LLE_STATIC
) != 0)
4159 /* flags & LLE_STATIC means deleting both dynamic and static entries */
4160 if ((flags
& LLE_STATIC
) || !(lle
->la_flags
& LLE_STATIC
))
4167 in6_lltable_free_entry(struct lltable
*llt
, struct llentry
*lle
)
4171 LLE_WLOCK_ASSERT(lle
);
4172 KASSERT(llt
!= NULL
, ("lltable is NULL"));
4174 /* Unlink entry from table */
4175 if ((lle
->la_flags
& LLE_LINKED
) != 0) {
4177 if_afdata_wlock_assert(ifp
, llt
->llt_af
);
4178 lltable_unlink_entry(llt
, lle
);
4183 if (thread_call_cancel(lle
->lle_timer
) == TRUE
)
4190 in6_lltable_rtcheck(struct ifnet
*ifp
,
4191 u_int flags
, const struct sockaddr
*l3addr
)
4193 #pragma unused(flags)
4196 KASSERT(l3addr
->sa_family
== AF_INET6
,
4197 ("sin_family %d", l3addr
->sa_family
));
4198 /* XXX rtalloc1 should take a const param */
4199 rt
= rtalloc1(__DECONST(struct sockaddr
*, l3addr
), 0, 0);
4200 if (rt
== NULL
|| (rt
->rt_flags
& RTF_GATEWAY
) || rt
->rt_ifp
!= ifp
) {
4203 * Create an ND6 cache for an IPv6 neighbor
4204 * that is not covered by our own prefix.
4206 /* XXX ifaof_ifpforaddr should take a const param */
4207 ifa
= ifaof_ifpforaddr(__DECONST(struct sockaddr
*, l3addr
), ifp
);
4214 log(LOG_INFO
, "IPv6 address: \"%s\" is not on the network\n",
4215 ip6_sprintf(&((const struct sockaddr_in6
*)(const void *)l3addr
)->sin6_addr
));
4224 static inline uint32_t
4225 in6_lltable_hash_dst(const struct in6_addr
*dst
, uint32_t hsize
)
4227 return (IN6_LLTBL_HASH(dst
->s6_addr32
[3], hsize
));
4231 in6_lltable_hash(const struct llentry
*lle
, uint32_t hsize
)
4233 return (in6_lltable_hash_dst(&lle
->r_l3addr
.addr6
, hsize
));
4237 in6_lltable_fill_sa_entry(const struct llentry
*lle
, struct sockaddr
*sa
)
4239 struct sockaddr_in6
*sin6
;
4241 sin6
= (struct sockaddr_in6
*)(void *)sa
;
4242 bzero(sin6
, sizeof(*sin6
));
4243 sin6
->sin6_family
= AF_INET6
;
4244 sin6
->sin6_len
= sizeof(*sin6
);
4245 sin6
->sin6_addr
= lle
->r_l3addr
.addr6
;
4248 static inline struct llentry
*
4249 in6_lltable_find_dst(struct lltable
*llt
, const struct in6_addr
*dst
)
4251 struct llentry
*lle
;
4252 struct llentries
*lleh
;
4255 hashidx
= in6_lltable_hash_dst(dst
, llt
->llt_hsize
);
4256 lleh
= &llt
->lle_head
[hashidx
];
4257 LIST_FOREACH(lle
, lleh
, lle_next
) {
4258 if (lle
->la_flags
& LLE_DELETED
)
4260 if (IN6_ARE_ADDR_EQUAL(&lle
->r_l3addr
.addr6
, dst
))
4268 in6_lltable_delete_entry(struct lltable
*llt
, struct llentry
*lle
)
4271 lle
->la_flags
|= LLE_DELETED
;
4272 EVENTHANDLER_INVOKE(NULL
, lle_event
, lle
, LLENTRY_DELETED
);
4274 log(LOG_INFO
, "ifaddr cache = %p is deleted\n", lle
);
4279 static struct llentry
*
4280 in6_lltable_alloc(struct lltable
*llt
, u_int flags
,
4281 const struct sockaddr
*l3addr
)
4283 const struct sockaddr_in6
*sin6
= (const struct sockaddr_in6
*)(const void *)l3addr
;
4284 struct ifnet
*ifp
= llt
->llt_ifp
;
4285 struct llentry
*lle
;
4287 KASSERT(l3addr
->sa_family
== AF_INET6
,
4288 ("sin_family %d", l3addr
->sa_family
));
4291 * A route that covers the given address must have
4292 * been installed 1st because we are doing a resolution,
4295 if (!(flags
& LLE_IFADDR
) &&
4296 in6_lltable_rtcheck(ifp
, flags
, l3addr
) != 0)
4299 lle
= in6_lltable_new(&sin6
->sin6_addr
, flags
);
4301 log(LOG_INFO
, "lla_lookup: new lle malloc failed\n");
4304 lle
->la_flags
= flags
;
4305 if ((flags
& LLE_IFADDR
) == LLE_IFADDR
) {
4306 lltable_set_entry_addr(ifp
, lle
, LLADDR(SDL(ifp
->if_lladdr
->ifa_addr
)));
4307 lle
->la_flags
|= LLE_STATIC
;
4310 if ((lle
->la_flags
& LLE_STATIC
) != 0)
4311 lle
->ln_state
= ND6_LLINFO_REACHABLE
;
4316 static struct llentry
*
4317 in6_lltable_lookup(struct lltable
*llt
, u_int flags
,
4318 const struct sockaddr
*l3addr
)
4320 const struct sockaddr_in6
*sin6
= (const struct sockaddr_in6
*)(const void *)l3addr
;
4321 struct llentry
*lle
;
4323 IF_AFDATA_LOCK_ASSERT(llt
->llt_ifp
, llt
->llt_af
);
4324 KASSERT(l3addr
->sa_family
== AF_INET6
,
4325 ("sin_family %d", l3addr
->sa_family
));
4327 lle
= in6_lltable_find_dst(llt
, &sin6
->sin6_addr
);
4332 KASSERT((flags
& (LLE_UNLOCKED
|LLE_EXCLUSIVE
)) !=
4333 (LLE_UNLOCKED
|LLE_EXCLUSIVE
),("wrong lle request flags: 0x%X",
4336 if (flags
& LLE_UNLOCKED
)
4339 if (flags
& LLE_EXCLUSIVE
)
4347 in6_lltable_dump_entry(struct lltable
*llt
, struct llentry
*lle
,
4348 struct sysctl_req
*wr
)
4350 struct ifnet
*ifp
= llt
->llt_ifp
;
4353 struct rt_msghdr rtm
;
4354 struct sockaddr_in6 sin6
;
4356 * ndp.c assumes that sdl is word aligned
4361 struct sockaddr_dl sdl
;
4363 struct sockaddr_dl
*sdl
;
4366 bzero(&ndpc
, sizeof(ndpc
));
4367 /* skip deleted entries */
4368 if ((lle
->la_flags
& LLE_DELETED
) == LLE_DELETED
)
4370 /* Skip if jailed and not a valid IP of the prison. */
4371 lltable_fill_sa_entry(lle
,
4372 (struct sockaddr
*)&ndpc
.sin6
);
4374 * produce a msg made of:
4376 * struct sockaddr_in6 (IPv6)
4377 * struct sockaddr_dl;
4379 ndpc
.rtm
.rtm_msglen
= sizeof(ndpc
);
4380 ndpc
.rtm
.rtm_version
= RTM_VERSION
;
4381 ndpc
.rtm
.rtm_type
= RTM_GET
;
4382 ndpc
.rtm
.rtm_flags
= RTF_UP
;
4383 ndpc
.rtm
.rtm_addrs
= RTA_DST
| RTA_GATEWAY
;
4386 if (lle
->la_flags
& LLE_PUB
)
4387 ndpc
.rtm
.rtm_flags
|= RTF_ANNOUNCE
;
4389 sdl
->sdl_family
= AF_LINK
;
4390 sdl
->sdl_len
= sizeof(*sdl
);
4391 sdl
->sdl_index
= ifp
->if_index
;
4392 sdl
->sdl_type
= ifp
->if_type
;
4393 if ((lle
->la_flags
& LLE_VALID
) == LLE_VALID
) {
4394 sdl
->sdl_alen
= ifp
->if_addrlen
;
4395 bcopy(&lle
->ll_addr
, LLADDR(sdl
), ifp
->if_addrlen
);
4398 bzero(LLADDR(sdl
), ifp
->if_addrlen
);
4400 if (lle
->la_expire
!= 0) {
4404 clock_get_calendar_microtime(&secs
, &usecs
);
4405 ndpc
.rtm
.rtm_rmx
.rmx_expire
= lle
->la_expire
+
4406 lle
->lle_remtime
/ hz
+
4407 secs
- net_uptime();
4409 ndpc
.rtm
.rtm_flags
|= (RTF_HOST
| RTF_LLDATA
);
4410 if (lle
->la_flags
& LLE_STATIC
)
4411 ndpc
.rtm
.rtm_flags
|= RTF_STATIC
;
4412 if (lle
->la_flags
& LLE_IFADDR
)
4413 ndpc
.rtm
.rtm_flags
|= RTF_PINNED
;
4414 if (lle
->ln_router
!= 0)
4415 ndpc
.rtm
.rtm_flags
|= RTF_GATEWAY
;
4416 ndpc
.rtm
.rtm_rmx
.rmx_pksent
= lle
->la_asked
;
4417 /* Store state in rmx_weight value */
4418 ndpc
.rtm
.rtm_rmx
.rmx_state
= lle
->ln_state
;
4419 ndpc
.rtm
.rtm_index
= ifp
->if_index
;
4420 error
= SYSCTL_OUT(wr
, &ndpc
, sizeof(ndpc
));
4426 in6_lltattach(struct ifnet
*ifp
)
4428 struct lltable
*llt
;
4430 llt
= lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE
);
4431 llt
->llt_af
= AF_INET6
;
4434 llt
->llt_lookup
= in6_lltable_lookup
;
4435 llt
->llt_alloc_entry
= in6_lltable_alloc
;
4436 llt
->llt_delete_entry
= in6_lltable_delete_entry
;
4437 llt
->llt_dump_entry
= in6_lltable_dump_entry
;
4438 llt
->llt_hash
= in6_lltable_hash
;
4439 llt
->llt_fill_sa_entry
= in6_lltable_fill_sa_entry
;
4440 llt
->llt_free_entry
= in6_lltable_free_entry
;
4441 llt
->llt_match_prefix
= in6_lltable_match_prefix
;
4448 in6_ip6_to_sockaddr(const struct in6_addr
*ip6
, u_int16_t port
,
4449 struct sockaddr_in6
*sin6
, u_int32_t maxlen
)
4451 if (maxlen
< sizeof(struct sockaddr_in6
)) {
4455 *sin6
= (struct sockaddr_in6
) {
4456 .sin6_family
= AF_INET6
,
4457 .sin6_len
= sizeof(*sin6
),
4462 if (IN6_IS_SCOPE_EMBED(&sin6
->sin6_addr
))
4464 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
4465 sin6
->sin6_addr
.s6_addr16
[1] = 0;
4471 in6_evhdlr_code_t in6_event_code
;
4472 struct ifnet
*in6_ifp
;
4473 struct in6_addr in6_address
;
4477 struct in6_event2kev in6_event2kev_array
[IN6_EVENT_MAX
] = {
4479 .in6_event_code
= IN6_ADDR_MARKED_DUPLICATED
,
4480 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4481 .in6_event_kev_code
= KEV_ND6_DAD_FAILURE
,
4482 .in6_event_str
= "IN6_ADDR_MARKED_DUPLICATED",
4485 .in6_event_code
= IN6_ADDR_MARKED_DETACHED
,
4486 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4487 .in6_event_kev_code
= KEV_ND6_ADDR_DETACHED
,
4488 .in6_event_str
= "IN6_ADDR_MARKED_DETACHED",
4491 .in6_event_code
= IN6_ADDR_MARKED_DEPRECATED
,
4492 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4493 .in6_event_kev_code
= KEV_ND6_ADDR_DEPRECATED
,
4494 .in6_event_str
= "IN6_ADDR_MARKED_DEPRECATED",
4497 .in6_event_code
= IN6_NDP_RTR_EXPIRY
,
4498 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4499 .in6_event_kev_code
= KEV_ND6_RTR_EXPIRED
,
4500 .in6_event_str
= "IN6_NDP_RTR_EXPIRY",
4503 .in6_event_code
= IN6_NDP_PFX_EXPIRY
,
4504 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4505 .in6_event_kev_code
= KEV_ND6_PFX_EXPIRED
,
4506 .in6_event_str
= "IN6_NDP_PFX_EXPIRY",
4509 .in6_event_code
= IN6_NDP_ADDR_EXPIRY
,
4510 .in6_event_kev_subclass
= KEV_ND6_SUBCLASS
,
4511 .in6_event_kev_code
= KEV_ND6_ADDR_EXPIRED
,
4512 .in6_event_str
= "IN6_NDP_ADDR_EXPIRY",
4517 in6_eventhdlr_callback(struct eventhandler_entry_arg arg0 __unused
,
4518 in6_evhdlr_code_t in6_ev_code
, struct ifnet
*ifp
,
4519 struct in6_addr
*p_addr6
, uint32_t val
)
4521 struct kev_msg ev_msg
;
4522 struct kev_nd6_event nd6_event
;
4524 bzero(&ev_msg
, sizeof(ev_msg
));
4525 bzero(&nd6_event
, sizeof(nd6_event
));
4527 nd6log0((LOG_INFO
, "%s Event %s received for %s\n",
4528 __func__
, in6_event2kev_array
[in6_ev_code
].in6_event_str
,
4529 ip6_sprintf(p_addr6
)));
4531 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
4532 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
4533 ev_msg
.kev_subclass
=
4534 in6_event2kev_array
[in6_ev_code
].in6_event_kev_subclass
;
4536 in6_event2kev_array
[in6_ev_code
].in6_event_kev_code
;
4538 nd6_event
.link_data
.if_family
= ifp
->if_family
;
4539 nd6_event
.link_data
.if_unit
= ifp
->if_unit
;
4540 strlcpy(nd6_event
.link_data
.if_name
, ifp
->if_name
,
4541 sizeof(nd6_event
.link_data
.if_name
));
4543 VERIFY(p_addr6
!= NULL
);
4544 bcopy(p_addr6
, &nd6_event
.in6_address
,
4545 sizeof(nd6_event
.in6_address
));
4546 nd6_event
.val
= val
;
4548 ev_msg
.dv
[0].data_ptr
= &nd6_event
;
4549 ev_msg
.dv
[0].data_length
= sizeof(nd6_event
);
4551 kev_post_msg(&ev_msg
);
4555 in6_event_callback(void *arg
)
4557 struct in6_event
*p_in6_ev
= (struct in6_event
*)arg
;
4559 EVENTHANDLER_INVOKE(&in6_evhdlr_ctxt
, in6_event
,
4560 p_in6_ev
->in6_event_code
, p_in6_ev
->in6_ifp
,
4561 &p_in6_ev
->in6_address
, p_in6_ev
->val
);
4564 struct in6_event_nwk_wq_entry
4566 struct nwk_wq_entry nwk_wqe
;
4567 struct in6_event in6_ev_arg
;
4571 in6_event_enqueue_nwk_wq_entry(in6_evhdlr_code_t in6_event_code
,
4572 struct ifnet
*ifp
, struct in6_addr
*p_addr6
,
4575 struct in6_event_nwk_wq_entry
*p_in6_ev
= NULL
;
4577 MALLOC(p_in6_ev
, struct in6_event_nwk_wq_entry
*,
4578 sizeof(struct in6_event_nwk_wq_entry
),
4579 M_NWKWQ
, M_WAITOK
| M_ZERO
);
4581 p_in6_ev
->nwk_wqe
.func
= in6_event_callback
;
4582 p_in6_ev
->nwk_wqe
.is_arg_managed
= TRUE
;
4583 p_in6_ev
->nwk_wqe
.arg
= &p_in6_ev
->in6_ev_arg
;
4585 p_in6_ev
->in6_ev_arg
.in6_event_code
= in6_event_code
;
4586 p_in6_ev
->in6_ev_arg
.in6_ifp
= ifp
;
4587 if (p_addr6
!= NULL
) {
4588 bcopy(p_addr6
, &p_in6_ev
->in6_ev_arg
.in6_address
,
4589 sizeof(p_in6_ev
->in6_ev_arg
.in6_address
));
4591 p_in6_ev
->in6_ev_arg
.val
= val
;
4593 nwk_wq_enqueue((struct nwk_wq_entry
*)p_in6_ev
);