2 * Copyright (c) 2003-2011 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@
29 /* $FreeBSD: src/sys/netinet6/in6.c,v 1.7.2.7 2001/08/06 20:26:22 ume Exp $ */
30 /* $KAME: in6.c,v 1.187 2001/05/24 07:43:59 itojun Exp $ */
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * Copyright (c) 1982, 1986, 1991, 1993
63 * The Regents of the University of California. All rights reserved.
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. All advertising materials mentioning features or use of this software
74 * must display the following acknowledgement:
75 * This product includes software developed by the University of
76 * California, Berkeley and its contributors.
77 * 4. Neither the name of the University nor the names of its contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
81 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
93 * @(#)in.c 8.2 (Berkeley) 11/15/93
97 #include <sys/param.h>
98 #include <sys/ioctl.h>
99 #include <sys/errno.h>
100 #include <sys/malloc.h>
101 #include <sys/socket.h>
102 #include <sys/socketvar.h>
103 #include <sys/sockio.h>
104 #include <sys/systm.h>
105 #include <sys/time.h>
106 #include <sys/kernel.h>
107 #include <sys/syslog.h>
108 #include <sys/kern_event.h>
109 #include <sys/mcache.h>
110 #include <sys/protosw.h>
112 #include <kern/locks.h>
113 #include <kern/zalloc.h>
114 #include <libkern/OSAtomic.h>
115 #include <machine/machine_routines.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>
124 #include <netinet/in.h>
125 #include <netinet/in_var.h>
126 #include <netinet/if_ether.h>
127 #include <netinet/in_systm.h>
128 #include <netinet/ip.h>
129 #include <netinet/in_pcb.h>
130 #include <netinet/icmp6.h>
132 #include <netinet6/nd6.h>
133 #include <netinet/ip6.h>
134 #include <netinet6/ip6_var.h>
135 #include <netinet6/mld6_var.h>
136 #include <netinet6/ip6_mroute.h>
137 #include <netinet6/in6_ifattach.h>
138 #include <netinet6/scope6_var.h>
139 #include <netinet6/in6_var.h>
140 #include <netinet6/in6_pcb.h>
142 #include <net/net_osdep.h>
145 #include <net/pfvar.h>
149 * Definitions of some costant IP6 addresses.
151 const struct in6_addr in6addr_any
= IN6ADDR_ANY_INIT
;
152 const struct in6_addr in6addr_loopback
= IN6ADDR_LOOPBACK_INIT
;
153 const struct in6_addr in6addr_nodelocal_allnodes
=
154 IN6ADDR_NODELOCAL_ALLNODES_INIT
;
155 const struct in6_addr in6addr_linklocal_allnodes
=
156 IN6ADDR_LINKLOCAL_ALLNODES_INIT
;
157 const struct in6_addr in6addr_linklocal_allrouters
=
158 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT
;
159 const struct in6_addr in6addr_linklocal_allv2routers
=
160 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT
;
162 const struct in6_addr in6mask0
= IN6MASK0
;
163 const struct in6_addr in6mask7
= IN6MASK7
;
164 const struct in6_addr in6mask16
= IN6MASK16
;
165 const struct in6_addr in6mask32
= IN6MASK32
;
166 const struct in6_addr in6mask64
= IN6MASK64
;
167 const struct in6_addr in6mask96
= IN6MASK96
;
168 const struct in6_addr in6mask128
= IN6MASK128
;
170 const struct sockaddr_in6 sa6_any
= {sizeof(sa6_any
), AF_INET6
,
171 0, 0, IN6ADDR_ANY_INIT
, 0};
173 static int in6_lifaddr_ioctl(struct socket
*, u_long
, caddr_t
,
174 struct ifnet
*, struct proc
*);
175 static int in6_ifinit(struct ifnet
*, struct in6_ifaddr
*,
176 struct sockaddr_in6
*, int);
177 static void in6_unlink_ifa(struct in6_ifaddr
*, struct ifnet
*);
178 static struct in6_ifaddr
*in6_ifaddr_alloc(int);
179 static void in6_ifaddr_attached(struct ifaddr
*);
180 static void in6_ifaddr_detached(struct ifaddr
*);
181 static void in6_ifaddr_free(struct ifaddr
*);
182 static void in6_ifaddr_trace(struct ifaddr
*, int);
183 static struct in6_aliasreq
*in6_aliasreq_to_native(void *, int,
184 struct in6_aliasreq
*);
186 extern lck_mtx_t
*nd6_mutex
;
187 extern int in6_init2done
;
189 #define IN6IFA_TRACE_HIST_SIZE 32 /* size of trace history */
192 __private_extern__
unsigned int in6ifa_trace_hist_size
= IN6IFA_TRACE_HIST_SIZE
;
194 struct in6_ifaddr_dbg
{
195 struct in6_ifaddr in6ifa
; /* in6_ifaddr */
196 struct in6_ifaddr in6ifa_old
; /* saved in6_ifaddr */
197 u_int16_t in6ifa_refhold_cnt
; /* # of IFA_ADDREF */
198 u_int16_t in6ifa_refrele_cnt
; /* # of IFA_REMREF */
200 * Alloc and free callers.
202 ctrace_t in6ifa_alloc
;
203 ctrace_t in6ifa_free
;
205 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
207 ctrace_t in6ifa_refhold
[IN6IFA_TRACE_HIST_SIZE
];
208 ctrace_t in6ifa_refrele
[IN6IFA_TRACE_HIST_SIZE
];
212 TAILQ_ENTRY(in6_ifaddr_dbg
) in6ifa_trash_link
;
215 /* List of trash in6_ifaddr entries protected by in6ifa_trash_lock */
216 static TAILQ_HEAD(, in6_ifaddr_dbg
) in6ifa_trash_head
;
217 static decl_lck_mtx_data(, in6ifa_trash_lock
);
220 static unsigned int in6ifa_debug
= 1; /* debugging (enabled) */
222 static unsigned int in6ifa_debug
; /* debugging (disabled) */
224 static unsigned int in6ifa_size
; /* size of zone element */
225 static struct zone
*in6ifa_zone
; /* zone for in6_ifaddr */
227 #define IN6IFA_ZONE_MAX 64 /* maximum elements in zone */
228 #define IN6IFA_ZONE_NAME "in6_ifaddr" /* zone name */
231 * Subroutine for in6_ifaddloop() and in6_ifremloop().
232 * This routine does actual work.
235 in6_ifloop_request(int cmd
, struct ifaddr
*ifa
)
237 struct sockaddr_in6 all1_sa
;
238 struct rtentry
*nrt
= NULL
;
241 bzero(&all1_sa
, sizeof(all1_sa
));
242 all1_sa
.sin6_family
= AF_INET6
;
243 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
244 all1_sa
.sin6_addr
= in6mask128
;
247 * We specify the address itself as the gateway, and set the
248 * RTF_LLINFO flag, so that the corresponding host route would have
249 * the flag, and thus applications that assume traditional behavior
250 * would be happy. Note that we assume the caller of the function
251 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
252 * which changes the outgoing interface to the loopback interface.
253 * ifa_addr for INET6 is set once during init; no need to hold lock.
255 lck_mtx_lock(rnh_lock
);
256 e
= rtrequest_locked(cmd
, ifa
->ifa_addr
, ifa
->ifa_addr
,
257 (struct sockaddr
*)&all1_sa
,
258 RTF_UP
|RTF_HOST
|RTF_LLINFO
, &nrt
);
260 log(LOG_ERR
, "in6_ifloop_request: "
261 "%s operation failed for %s (errno=%d)\n",
262 cmd
== RTM_ADD
? "ADD" : "DELETE",
263 ip6_sprintf(&((struct in6_ifaddr
*)ifa
)->ia_addr
.sin6_addr
),
270 * Make sure rt_ifa be equal to IFA, the second argument of the
272 * We need this because when we refer to rt_ifa->ia6_flags in
273 * ip6_input, we assume that the rt_ifa points to the address instead
274 * of the loopback address.
276 if (cmd
== RTM_ADD
&& nrt
&& ifa
!= nrt
->rt_ifa
) {
281 * Report the addition/removal of the address to the routing socket.
282 * XXX: since we called rtinit for a p2p interface with a destination,
283 * we end up reporting twice in such a case. Should we rather
284 * omit the second report?
287 rt_newaddrmsg(cmd
, ifa
, e
, nrt
);
288 if (cmd
== RTM_DELETE
) {
292 /* the cmd must be RTM_ADD here */
293 RT_REMREF_LOCKED(nrt
);
297 lck_mtx_unlock(rnh_lock
);
301 * Add ownaddr as loopback rtentry. We previously add the route only if
302 * necessary (ex. on a p2p link). However, since we now manage addresses
303 * separately from prefixes, we should always add the route. We can't
304 * rely on the cloning mechanism from the corresponding interface route
308 in6_ifaddloop(struct ifaddr
*ifa
)
313 * If there is no loopback entry, allocate one. ifa_addr for
314 * INET6 is set once during init; no need to hold lock.
316 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
319 if (rt
== NULL
|| (rt
->rt_flags
& RTF_HOST
) == 0 ||
320 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) == 0) {
322 RT_REMREF_LOCKED(rt
);
325 in6_ifloop_request(RTM_ADD
, ifa
);
326 } else if (rt
!= NULL
) {
327 RT_REMREF_LOCKED(rt
);
333 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
337 in6_ifremloop(struct ifaddr
*ifa
)
339 struct in6_ifaddr
*ia
;
344 * Some of BSD variants do not remove cloned routes
345 * from an interface direct route, when removing the direct route
346 * (see comments in net/net_osdep.h). Even for variants that do remove
347 * cloned routes, they could fail to remove the cloned routes when
348 * we handle multple addresses that share a common prefix.
349 * So, we should remove the route corresponding to the deleted address
350 * regardless of the result of in6_is_ifloop_auto().
354 * Delete the entry only if exact one ifa exists. More than one ifa
355 * can exist if we assign a same single address to multiple
356 * (probably p2p) interfaces.
357 * XXX: we should avoid such a configuration in IPv6...
359 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
360 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
361 IFA_LOCK(&ia
->ia_ifa
);
362 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa
), &ia
->ia_addr
.sin6_addr
)) {
365 IFA_UNLOCK(&ia
->ia_ifa
);
369 IFA_UNLOCK(&ia
->ia_ifa
);
371 lck_rw_done(&in6_ifaddr_rwlock
);
375 * Before deleting, check if a corresponding loopbacked host
376 * route surely exists. With this check, we can avoid to
377 * delete an interface direct route whose destination is same
378 * as the address being removed. This can happen when removing
379 * a subnet-router anycast address on an interface attahced
380 * to a shared medium. ifa_addr for INET6 is set once during
381 * init; no need to hold lock.
383 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
386 if ((rt
->rt_flags
& RTF_HOST
) != 0 &&
387 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
388 RT_REMREF_LOCKED(rt
);
390 in6_ifloop_request(RTM_DELETE
, ifa
);
400 in6_mask2len(mask
, lim0
)
401 struct in6_addr
*mask
;
405 u_char
*lim
= lim0
, *p
;
407 /* ignore the scope_id part */
408 if (lim0
== NULL
|| lim0
- (u_char
*)mask
> sizeof(*mask
))
409 lim
= (u_char
*)mask
+ sizeof(*mask
);
410 for (p
= (u_char
*)mask
; p
< lim
; x
++, p
++) {
416 for (y
= 0; y
< 8; y
++) {
417 if ((*p
& (0x80 >> y
)) == 0)
423 * when the limit pointer is given, do a stricter check on the
427 if (y
!= 0 && (*p
& (0x00ff >> y
)) != 0)
429 for (p
= p
+ 1; p
< lim
; p
++)
438 in6_len2mask(mask
, len
)
439 struct in6_addr
*mask
;
444 bzero(mask
, sizeof(*mask
));
445 for (i
= 0; i
< len
/ 8; i
++)
446 mask
->s6_addr8
[i
] = 0xff;
448 mask
->s6_addr8
[i
] = (0xff00 >> (len
% 8)) & 0xff;
452 in6_aliasreq_64_to_32(struct in6_aliasreq_64
*src
, struct in6_aliasreq_32
*dst
)
454 bzero(dst
, sizeof (*dst
));
455 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof (dst
->ifra_name
));
456 dst
->ifra_addr
= src
->ifra_addr
;
457 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
458 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
459 dst
->ifra_flags
= src
->ifra_flags
;
460 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
461 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
462 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
463 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
467 in6_aliasreq_32_to_64(struct in6_aliasreq_32
*src
, struct in6_aliasreq_64
*dst
)
469 bzero(dst
, sizeof (*dst
));
470 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof (dst
->ifra_name
));
471 dst
->ifra_addr
= src
->ifra_addr
;
472 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
473 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
474 dst
->ifra_flags
= src
->ifra_flags
;
475 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
476 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
477 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
478 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
481 static struct in6_aliasreq
*
482 in6_aliasreq_to_native(void *data
, int data_is_64
, struct in6_aliasreq
*dst
)
484 #if defined(__LP64__)
488 in6_aliasreq_32_to_64((struct in6_aliasreq_32
*)data
,
489 (struct in6_aliasreq_64
*)dst
);
492 in6_aliasreq_64_to_32((struct in6_aliasreq_64
*)data
,
493 (struct in6_aliasreq_32
*)dst
);
496 #endif /* __LP64__ */
500 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
501 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
504 in6_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
507 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)data
;
508 struct in6_ifaddr
*ia
= NULL
;
509 struct in6_aliasreq sifra
;
510 struct in6_aliasreq
*ifra
= NULL
;
511 struct sockaddr_in6
*sa6
;
512 int index
, privileged
, error
= 0;
513 struct timeval timenow
;
514 int p64
= proc_is64bit(p
);
516 getmicrotime(&timenow
);
518 privileged
= (proc_suser(p
) == 0);
521 case SIOCGETSGCNT_IN6
:
522 case SIOCGETMIFCNT_IN6_32
:
523 case SIOCGETMIFCNT_IN6_64
:
524 return (mrt6_ioctl(cmd
, data
));
529 case SIOCAADDRCTL_POLICY
:
530 case SIOCDADDRCTL_POLICY
:
533 return (in6_src_ioctl(cmd
, data
));
537 case SIOCDRADD_IN6_32
:
538 case SIOCDRADD_IN6_64
:
539 case SIOCDRDEL_IN6_32
:
540 case SIOCDRDEL_IN6_64
:
543 return (defrtrlist_ioctl(cmd
, data
));
550 case SIOCAUTOCONF_START
:
551 case SIOCAUTOCONF_STOP
:
552 case SIOCLL_START_32
:
553 case SIOCLL_START_64
:
555 case SIOCPROTOATTACH_IN6_32
:
556 case SIOCPROTOATTACH_IN6_64
:
557 case SIOCPROTODETACH_IN6
:
561 case SIOCSNDFLUSH_IN6
:
562 case SIOCSPFXFLUSH_IN6
:
563 case SIOCSRTRFLUSH_IN6
:
564 case SIOCSDEFIFACE_IN6_32
:
565 case SIOCSDEFIFACE_IN6_64
:
566 case SIOCSIFINFO_FLAGS
:
570 case OSIOCGIFINFO_IN6
:
571 case SIOCGIFINFO_IN6
:
572 case SIOCGDRLST_IN6_32
:
573 case SIOCGDRLST_IN6_64
:
574 case SIOCGPRLST_IN6_32
:
575 case SIOCGPRLST_IN6_64
:
576 case SIOCGNBRINFO_IN6_32
:
577 case SIOCGNBRINFO_IN6_64
:
578 case SIOCGDEFIFACE_IN6_32
:
579 case SIOCGDEFIFACE_IN6_64
:
580 return (nd6_ioctl(cmd
, data
, ifp
));
584 case SIOCSIFPREFIX_IN6
:
585 case SIOCDIFPREFIX_IN6
:
586 case SIOCAIFPREFIX_IN6
:
587 case SIOCCIFPREFIX_IN6
:
588 case SIOCSGIFPREFIX_IN6
:
589 case SIOCGIFPREFIX_IN6
:
591 "prefix ioctls are now invalidated. "
592 "please use ifconfig.\n");
600 return (scope6_set(ifp
, ifr
->ifr_ifru
.ifru_scope_id
));
604 return (scope6_get(ifp
, ifr
->ifr_ifru
.ifru_scope_id
));
608 return (scope6_get_default(ifr
->ifr_ifru
.ifru_scope_id
));
618 return (in6_lifaddr_ioctl(so
, cmd
, data
, ifp
, p
));
622 * Find address for this interface, if it exists.
624 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
625 * only, and used the first interface address as the target of other
626 * operations (without checking ifra_addr). This was because netinet
627 * code/API assumed at most 1 interface address per interface.
628 * Since IPv6 allows a node to assign multiple addresses
629 * on a single interface, we almost always look and check the
630 * presence of ifra_addr, and reject invalid ones here.
631 * It also decreases duplicated code among SIOC*_IN6 operations.
634 case SIOCLL_START_32
:
635 case SIOCAIFADDR_IN6_32
:
637 * Convert user ifra to the kernel form, when appropriate.
638 * This allows the conversion between different data models
639 * to be centralized, so that it can be passed around to other
640 * routines that are expecting the kernel form.
642 ifra
= in6_aliasreq_to_native(data
, 0, &sifra
);
643 sa6
= (struct sockaddr_in6
*)&ifra
->ifra_addr
;
646 case SIOCLL_START_64
:
647 case SIOCAIFADDR_IN6_64
:
648 ifra
= in6_aliasreq_to_native(data
, 1, &sifra
);
649 sa6
= (struct sockaddr_in6
*)&ifra
->ifra_addr
;
652 case SIOCSIFADDR_IN6
: /* deprecated */
653 case SIOCGIFADDR_IN6
:
654 case SIOCSIFDSTADDR_IN6
: /* deprecated */
655 case SIOCSIFNETMASK_IN6
: /* deprecated */
656 case SIOCGIFDSTADDR_IN6
:
657 case SIOCGIFNETMASK_IN6
:
658 case SIOCDIFADDR_IN6
:
659 case SIOCGIFPSRCADDR_IN6
:
660 case SIOCGIFPDSTADDR_IN6
:
661 case SIOCGIFAFLAG_IN6
:
662 case SIOCSNDFLUSH_IN6
:
663 case SIOCSPFXFLUSH_IN6
:
664 case SIOCSRTRFLUSH_IN6
:
665 case SIOCGIFALIFETIME_IN6
:
666 case SIOCSIFALIFETIME_IN6
:
667 case SIOCGIFSTAT_IN6
:
668 case SIOCGIFSTAT_ICMP6
:
669 sa6
= &ifr
->ifr_addr
;
679 case SIOCAUTOCONF_START
:
680 ifnet_lock_exclusive(ifp
);
681 ifp
->if_eflags
|= IFEF_ACCEPT_RTADVD
;
682 ifnet_lock_done(ifp
);
686 case SIOCAUTOCONF_STOP
: {
687 ifnet_lock_exclusive(ifp
);
688 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADVD
;
689 ifnet_lock_done(ifp
);
691 /* Remove autoconfigured address from interface */
692 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
695 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
699 IFA_LOCK(&ia
->ia_ifa
);
700 if (ia
->ia6_flags
& IN6_IFF_AUTOCONF
) {
701 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
702 IFA_UNLOCK(&ia
->ia_ifa
);
703 lck_rw_done(&in6_ifaddr_rwlock
);
704 in6_purgeaddr(&ia
->ia_ifa
);
705 IFA_REMREF(&ia
->ia_ifa
); /* for us */
706 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
708 * Purging the address caused in6_ifaddr_rwlock
709 * to be dropped and reacquired;
710 * therefore search again from the beginning
711 * of in6_ifaddrs list.
716 IFA_UNLOCK(&ia
->ia_ifa
);
719 lck_rw_done(&in6_ifaddr_rwlock
);
723 case SIOCLL_START_32
:
724 case SIOCLL_START_64
:
726 * NOTE: All the interface specific DLIL attachements should
727 * be done here. They are currently done in in6_ifattach()
728 * for the interfaces that need it.
730 if ((ifp
->if_eflags
& IFEF_NOAUTOIPV6LL
) != 0 &&
731 ifra
->ifra_addr
.sin6_family
== AF_INET6
&&
732 ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
) {
733 /* some interfaces may provide LinkLocal addresses */
734 error
= in6_if_up(ifp
, ifra
);
736 error
= in6_if_up(ifp
, 0);
742 /* Remove link local addresses from interface */
743 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
746 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
750 IFA_LOCK(&ia
->ia_ifa
);
751 if (IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
)) {
752 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
753 IFA_UNLOCK(&ia
->ia_ifa
);
754 lck_rw_done(&in6_ifaddr_rwlock
);
755 in6_purgeaddr(&ia
->ia_ifa
);
756 IFA_REMREF(&ia
->ia_ifa
); /* for us */
757 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
759 * Purging the address caused in6_ifaddr_rwlock
760 * to be dropped and reacquired;
761 * therefore search again from the beginning
762 * of in6_ifaddrs list.
767 IFA_UNLOCK(&ia
->ia_ifa
);
770 lck_rw_done(&in6_ifaddr_rwlock
);
774 case SIOCPROTOATTACH_IN6_32
:
775 case SIOCPROTOATTACH_IN6_64
:
776 return (in6_domifattach(ifp
));
779 case SIOCPROTODETACH_IN6
:
780 /* Cleanup interface routes and addresses */
783 if ((error
= proto_unplumb(PF_INET6
, ifp
)))
784 printf("SIOCPROTODETACH_IN6: %s error=%d\n",
785 if_name(ifp
), error
);
790 * Find address for this interface, if it exists; depending
791 * on the ioctl command, sa6 points to the address in ifra/ifr.
793 if (sa6
!= NULL
&& sa6
->sin6_family
== AF_INET6
) {
794 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
795 if (sa6
->sin6_addr
.s6_addr16
[1] == 0) {
796 /* link ID is not embedded by the user */
797 sa6
->sin6_addr
.s6_addr16
[1] =
798 htons(ifp
->if_index
);
799 } else if (sa6
->sin6_addr
.s6_addr16
[1] !=
800 htons(ifp
->if_index
)) {
801 return (EINVAL
); /* link ID contradicts */
803 if (sa6
->sin6_scope_id
) {
804 if (sa6
->sin6_scope_id
!=
805 (u_int32_t
)ifp
->if_index
)
807 sa6
->sin6_scope_id
= 0; /* XXX: good way? */
810 ia
= in6ifa_ifpwithaddr(ifp
, &sa6
->sin6_addr
);
816 case SIOCSIFADDR_IN6
:
817 case SIOCSIFDSTADDR_IN6
:
818 case SIOCSIFNETMASK_IN6
:
820 * Since IPv6 allows a node to assign multiple addresses
821 * on a single interface, SIOCSIFxxx ioctls are deprecated.
823 /* we decided to obsolete this command (20000704) */
827 case SIOCDIFADDR_IN6
:
829 * for IPv4, we look for existing in_ifaddr here to allow
830 * "ifconfig if0 delete" to remove the first IPv4 address on
831 * the interface. For IPv6, as the spec allows multiple
832 * interface address from the day one, we consider "remove the
833 * first one" semantics to be not preferable.
836 error
= EADDRNOTAVAIL
;
840 case SIOCAIFADDR_IN6_32
:
841 case SIOCAIFADDR_IN6_64
:
843 * We always require users to specify a valid IPv6 address for
844 * the corresponding operation. Use "sa6" instead of "ifra"
845 * since SIOCDIFADDR_IN6 falls thru above.
847 if (sa6
->sin6_family
!= AF_INET6
||
848 sa6
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
849 error
= EAFNOSUPPORT
;
859 case SIOCGIFADDR_IN6
:
860 /* This interface is basically deprecated. use SIOCGIFCONF. */
862 case SIOCGIFAFLAG_IN6
:
863 case SIOCGIFNETMASK_IN6
:
864 case SIOCGIFDSTADDR_IN6
:
865 case SIOCGIFALIFETIME_IN6
:
866 /* must think again about its semantics */
868 error
= EADDRNOTAVAIL
;
873 case SIOCSIFALIFETIME_IN6
:
879 error
= EADDRNOTAVAIL
;
882 /* sanity for overflow - beware unsigned */
884 struct in6_addrlifetime_64
*lt
;
886 lt
= (struct in6_addrlifetime_64
*)
887 &ifr
->ifr_ifru
.ifru_lifetime
;
888 if (((ia
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0
889 || lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
)
890 && lt
->ia6t_vltime
+ timenow
.tv_sec
<
895 if (((ia
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0
896 || lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
)
897 && lt
->ia6t_pltime
+ timenow
.tv_sec
<
903 struct in6_addrlifetime_32
*lt
;
905 lt
= (struct in6_addrlifetime_32
*)
906 &ifr
->ifr_ifru
.ifru_lifetime
;
907 if (((ia
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0
908 || lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
)
909 && lt
->ia6t_vltime
+ timenow
.tv_sec
<
914 if (((ia
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0
915 || lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
)
916 && lt
->ia6t_pltime
+ timenow
.tv_sec
<
927 case SIOCGIFADDR_IN6
:
928 IFA_LOCK(&ia
->ia_ifa
);
929 ifr
->ifr_addr
= ia
->ia_addr
;
930 IFA_UNLOCK(&ia
->ia_ifa
);
931 if ((error
= sa6_recoverscope(&ifr
->ifr_addr
)) != 0) {
932 IFA_REMREF(&ia
->ia_ifa
);
937 case SIOCGIFDSTADDR_IN6
:
938 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0) {
943 * XXX: should we check if ifa_dstaddr is NULL and return
946 IFA_LOCK(&ia
->ia_ifa
);
947 ifr
->ifr_dstaddr
= ia
->ia_dstaddr
;
948 IFA_UNLOCK(&ia
->ia_ifa
);
949 if ((error
= sa6_recoverscope(&ifr
->ifr_dstaddr
)) != 0) {
950 IFA_REMREF(&ia
->ia_ifa
);
955 case SIOCGIFNETMASK_IN6
:
956 IFA_LOCK(&ia
->ia_ifa
);
957 ifr
->ifr_addr
= ia
->ia_prefixmask
;
958 IFA_UNLOCK(&ia
->ia_ifa
);
961 case SIOCGIFAFLAG_IN6
:
962 IFA_LOCK(&ia
->ia_ifa
);
963 ifr
->ifr_ifru
.ifru_flags6
= ia
->ia6_flags
;
964 IFA_UNLOCK(&ia
->ia_ifa
);
967 case SIOCGIFSTAT_IN6
:
972 index
= ifp
->if_index
;
973 lck_rw_lock_shared(&in6_ifs_rwlock
);
974 if (in6_ifstat
== NULL
|| index
>= in6_ifstatmax
975 || in6_ifstat
[index
] == NULL
) {
976 /* return EAFNOSUPPORT? */
977 bzero(&ifr
->ifr_ifru
.ifru_stat
,
978 sizeof (ifr
->ifr_ifru
.ifru_stat
));
980 ifr
->ifr_ifru
.ifru_stat
= *in6_ifstat
[index
];
982 lck_rw_done(&in6_ifs_rwlock
);
985 case SIOCGIFSTAT_ICMP6
:
990 index
= ifp
->if_index
;
991 lck_rw_lock_shared(&icmp6_ifs_rwlock
);
992 if (icmp6_ifstat
== NULL
|| index
>= icmp6_ifstatmax
||
993 icmp6_ifstat
[index
] == NULL
) {
994 /* return EAFNOSUPPORT? */
995 bzero(&ifr
->ifr_ifru
.ifru_stat
,
996 sizeof (ifr
->ifr_ifru
.ifru_icmp6stat
));
998 ifr
->ifr_ifru
.ifru_icmp6stat
= *icmp6_ifstat
[index
];
1000 lck_rw_done(&icmp6_ifs_rwlock
);
1003 case SIOCGIFALIFETIME_IN6
:
1004 IFA_LOCK(&ia
->ia_ifa
);
1006 struct in6_addrlifetime_64
*lt
;
1008 lt
= (struct in6_addrlifetime_64
*)
1009 &ifr
->ifr_ifru
.ifru_lifetime
;
1010 lt
->ia6t_expire
= ia
->ia6_lifetime
.ia6t_expire
;
1011 lt
->ia6t_preferred
= ia
->ia6_lifetime
.ia6t_preferred
;
1012 lt
->ia6t_vltime
= ia
->ia6_lifetime
.ia6t_vltime
;
1013 lt
->ia6t_pltime
= ia
->ia6_lifetime
.ia6t_pltime
;
1015 struct in6_addrlifetime_32
*lt
;
1017 lt
= (struct in6_addrlifetime_32
*)
1018 &ifr
->ifr_ifru
.ifru_lifetime
;
1020 (uint32_t)ia
->ia6_lifetime
.ia6t_expire
;
1021 lt
->ia6t_preferred
=
1022 (uint32_t)ia
->ia6_lifetime
.ia6t_preferred
;
1024 (uint32_t)ia
->ia6_lifetime
.ia6t_vltime
;
1026 (uint32_t)ia
->ia6_lifetime
.ia6t_pltime
;
1028 IFA_UNLOCK(&ia
->ia_ifa
);
1031 case SIOCSIFALIFETIME_IN6
:
1032 IFA_LOCK(&ia
->ia_ifa
);
1034 struct in6_addrlifetime_64
*lt
;
1036 lt
= (struct in6_addrlifetime_64
*)
1037 &ifr
->ifr_ifru
.ifru_lifetime
;
1038 ia
->ia6_lifetime
.ia6t_expire
= lt
->ia6t_expire
;
1039 ia
->ia6_lifetime
.ia6t_preferred
= lt
->ia6t_preferred
;
1040 ia
->ia6_lifetime
.ia6t_vltime
= lt
->ia6t_vltime
;
1041 ia
->ia6_lifetime
.ia6t_pltime
= lt
->ia6t_pltime
;
1043 struct in6_addrlifetime_32
*lt
;
1045 lt
= (struct in6_addrlifetime_32
*)
1046 &ifr
->ifr_ifru
.ifru_lifetime
;
1047 ia
->ia6_lifetime
.ia6t_expire
=
1048 (uint32_t)lt
->ia6t_expire
;
1049 ia
->ia6_lifetime
.ia6t_preferred
=
1050 (uint32_t)lt
->ia6t_preferred
;
1051 ia
->ia6_lifetime
.ia6t_vltime
= lt
->ia6t_vltime
;
1052 ia
->ia6_lifetime
.ia6t_pltime
= lt
->ia6t_pltime
;
1055 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
||
1056 (ia
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
1057 ia
->ia6_lifetime
.ia6t_expire
=
1058 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_vltime
;
1060 ia
->ia6_lifetime
.ia6t_expire
= 0;
1061 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
||
1062 (ia
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
1063 ia
->ia6_lifetime
.ia6t_preferred
=
1064 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_pltime
;
1066 ia
->ia6_lifetime
.ia6t_preferred
= 0;
1067 IFA_UNLOCK(&ia
->ia_ifa
);
1070 case SIOCAIFADDR_IN6_32
:
1071 case SIOCAIFADDR_IN6_64
: {
1073 struct nd_prefix pr0
, *pr
;
1075 /* Attempt to attach the protocol, in case it isn't attached */
1076 error
= in6_domifattach(ifp
);
1078 if (error
== EEXIST
)
1083 /* PF_INET6 wasn't previously attached */
1084 if ((error
= in6_if_up(ifp
, NULL
)) != 0)
1089 * first, make or update the interface address structure,
1090 * and link it to the list.
1092 if ((error
= in6_update_ifa(ifp
, ifra
, ia
, 0, M_WAITOK
)) != 0)
1096 * then, make the prefix on-link on the interface.
1097 * XXX: we'd rather create the prefix before the address, but
1098 * we need at least one address to install the corresponding
1099 * interface route, so we configure the address first.
1103 * convert mask to prefix length (prefixmask has already
1104 * been validated in in6_update_ifa().
1106 bzero(&pr0
, sizeof(pr0
));
1108 pr0
.ndpr_plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
1110 if (pr0
.ndpr_plen
== 128)
1111 break; /* we don't need to install a host route. */
1112 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
1113 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
1114 /* apply the mask for safety. */
1115 for (i
= 0; i
< 4; i
++) {
1116 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1117 ifra
->ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
1120 * XXX: since we don't have an API to set prefix (not address)
1121 * lifetimes, we just use the same lifetimes as addresses.
1122 * The (temporarily) installed lifetimes can be overridden by
1123 * later advertised RAs (when accept_rtadv is non 0), which is
1124 * an intended behavior.
1126 pr0
.ndpr_raf_onlink
= 1; /* should be configurable? */
1128 ((ifra
->ifra_flags
& IN6_IFF_AUTOCONF
) != 0);
1129 pr0
.ndpr_vltime
= ifra
->ifra_lifetime
.ia6t_vltime
;
1130 pr0
.ndpr_pltime
= ifra
->ifra_lifetime
.ia6t_pltime
;
1131 pr0
.ndpr_stateflags
|= NDPRF_STATIC
;
1133 /* add the prefix if there's one. */
1134 if ((pr
= nd6_prefix_lookup(&pr0
)) == NULL
) {
1136 * nd6_prelist_add will install the corresponding
1139 if ((error
= nd6_prelist_add(&pr0
, NULL
, &pr
, FALSE
)) != 0)
1142 log(LOG_ERR
, "nd6_prelist_add succedded but "
1149 IFA_REMREF(&ia
->ia_ifa
);
1150 if ((ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
))
1152 /* XXX: this should not happen! */
1153 log(LOG_ERR
, "in6_control: addition succeeded, but"
1156 IFA_LOCK(&ia
->ia_ifa
);
1157 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
1158 ia
->ia6_ndpr
== NULL
) { /* new autoconfed addr */
1161 VERIFY(pr
->ndpr_addrcnt
!= 0);
1163 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
1166 * If this is the first autoconf address from
1167 * the prefix, create a temporary address
1168 * as well (when specified).
1170 if (ip6_use_tempaddr
&&
1171 pr
->ndpr_addrcnt
== 1) {
1175 IFA_UNLOCK(&ia
->ia_ifa
);
1177 if ((e
= in6_tmpifadd(ia
, 1,
1179 log(LOG_NOTICE
, "in6_control: "
1180 "failed to create a "
1181 "temporary address, "
1187 IFA_UNLOCK(&ia
->ia_ifa
);
1190 IFA_UNLOCK(&ia
->ia_ifa
);
1193 * this might affect the status of autoconfigured
1194 * addresses, that is, this address might make
1195 * other addresses detached.
1197 lck_mtx_lock(nd6_mutex
);
1198 pfxlist_onlink_check();
1199 lck_mtx_unlock(nd6_mutex
);
1202 /* Drop use count held above during lookup/add */
1205 pf_ifaddr_hook(ifp
, cmd
);
1210 case SIOCDIFADDR_IN6
: {
1212 struct nd_prefix pr0
, *pr
;
1215 * If the address being deleted is the only one that owns
1216 * the corresponding prefix, expire the prefix as well.
1217 * XXX: theoretically, we don't have to worry about such
1218 * relationship, since we separate the address management
1219 * and the prefix management. We do this, however, to provide
1220 * as much backward compatibility as possible in terms of
1221 * the ioctl operation.
1222 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
1224 IFA_LOCK(&ia
->ia_ifa
);
1225 bzero(&pr0
, sizeof(pr0
));
1227 pr0
.ndpr_plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
1229 if (pr0
.ndpr_plen
== 128) {
1230 IFA_UNLOCK(&ia
->ia_ifa
);
1233 pr0
.ndpr_prefix
= ia
->ia_addr
;
1234 pr0
.ndpr_mask
= ia
->ia_prefixmask
.sin6_addr
;
1235 for (i
= 0; i
< 4; i
++) {
1236 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1237 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
];
1239 IFA_UNLOCK(&ia
->ia_ifa
);
1241 * The logic of the following condition is a bit complicated.
1242 * We expire the prefix when
1243 * 1. the address obeys autoconfiguration and it is the
1244 * only owner of the associated prefix, or
1245 * 2. the address does not obey autoconf and there is no
1246 * other owner of the prefix.
1248 if ((pr
= nd6_prefix_lookup(&pr0
)) != NULL
) {
1249 IFA_LOCK(&ia
->ia_ifa
);
1251 if (((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
1252 pr
->ndpr_addrcnt
== 1) ||
1253 ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 &&
1254 pr
->ndpr_addrcnt
== 0)) {
1255 pr
->ndpr_expire
= 1; /* XXX: just for expiration */
1258 IFA_UNLOCK(&ia
->ia_ifa
);
1260 /* Drop use count held above during lookup */
1265 in6_purgeaddr(&ia
->ia_ifa
);
1267 pf_ifaddr_hook(ifp
, cmd
);
1273 error
= ifnet_ioctl(ifp
, PF_INET6
, cmd
, data
);
1278 IFA_REMREF(&ia
->ia_ifa
);
1283 * Update parameters of an IPv6 interface address.
1284 * If necessary, a new entry is created and linked into address chains.
1285 * This function is separated from in6_control().
1288 in6_update_ifa(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
,
1289 struct in6_ifaddr
*ia
, int flags
, int how
)
1291 int error
= 0, hostIsNew
= 0, plen
= -1;
1292 struct in6_ifaddr
*oia
;
1293 struct sockaddr_in6 dst6
;
1294 struct in6_addrlifetime
*lt
;
1295 struct in6_multi
*in6m_sol
= NULL
;
1296 struct in6_multi_mship
*imm
;
1297 struct timeval timenow
;
1299 struct ifaddr
*ifa
= NULL
;
1303 /* Validate parameters */
1304 if (ifp
== NULL
|| ifra
== NULL
) /* this maybe redundant */
1308 * The destination address for a p2p link must have a family
1309 * of AF_UNSPEC or AF_INET6.
1311 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
1312 ifra
->ifra_dstaddr
.sin6_family
!= AF_INET6
&&
1313 ifra
->ifra_dstaddr
.sin6_family
!= AF_UNSPEC
)
1314 return(EAFNOSUPPORT
);
1316 * validate ifra_prefixmask. don't check sin6_family, netmask
1317 * does not carry fields other than sin6_len.
1319 if (ifra
->ifra_prefixmask
.sin6_len
> sizeof(struct sockaddr_in6
))
1322 * Set the address family value for the mask if it was not set.
1325 if (ifra
->ifra_prefixmask
.sin6_len
== sizeof(struct sockaddr_in6
) &&
1326 ifra
->ifra_prefixmask
.sin6_family
== 0) {
1327 ifra
->ifra_prefixmask
.sin6_family
= AF_INET6
;
1330 * Because the IPv6 address architecture is classless, we require
1331 * users to specify a (non 0) prefix length (mask) for a new address.
1332 * We also require the prefix (when specified) mask is valid, and thus
1333 * reject a non-consecutive mask.
1335 if (ia
== NULL
&& ifra
->ifra_prefixmask
.sin6_len
== 0)
1337 if (ifra
->ifra_prefixmask
.sin6_len
!= 0) {
1338 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
1339 (u_char
*)&ifra
->ifra_prefixmask
+
1340 ifra
->ifra_prefixmask
.sin6_len
);
1345 * In this case, ia must not be NULL. We just use its prefix
1348 IFA_LOCK(&ia
->ia_ifa
);
1349 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
);
1350 IFA_UNLOCK(&ia
->ia_ifa
);
1353 * If the destination address on a p2p interface is specified,
1354 * and the address is a scoped one, validate/set the scope
1357 dst6
= ifra
->ifra_dstaddr
;
1358 if (((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) != 0 ) &&
1359 (dst6
.sin6_family
== AF_INET6
)) {
1362 if ((error
= in6_recoverscope(&dst6
,
1363 &ifra
->ifra_dstaddr
.sin6_addr
,
1367 scopeid
= in6_addr2scopeid(ifp
, &dst6
.sin6_addr
);
1368 if (dst6
.sin6_scope_id
== 0) /* user omit to specify the ID. */
1369 dst6
.sin6_scope_id
= scopeid
;
1370 else if (dst6
.sin6_scope_id
!= scopeid
)
1371 return(EINVAL
); /* scope ID mismatch. */
1373 if ((error
= in6_embedscope(&dst6
.sin6_addr
, &dst6
, NULL
, NULL
,
1376 dst6
.sin6_scope_id
= 0; /* XXX */
1379 * The destination address can be specified only for a p2p or a
1380 * loopback interface. If specified, the corresponding prefix length
1383 if (ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
) {
1384 if ((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) == 0) {
1385 /* XXX: noisy message */
1386 log(LOG_INFO
, "in6_update_ifa: a destination can be "
1387 "specified for a p2p or a loopback IF only\n");
1392 * The following message seems noisy, but we dare to
1393 * add it for diagnosis.
1395 log(LOG_INFO
, "in6_update_ifa: prefixlen must be 128 "
1396 "when dstaddr is specified\n");
1400 /* lifetime consistency check */
1402 getmicrotime(&timenow
);
1403 lt
= &ifra
->ifra_lifetime
;
1404 if ((lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
1405 || (ifra
->ifra_flags
& IN6_IFF_TEMPORARY
) != 0)
1406 && lt
->ia6t_vltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
1409 if (lt
->ia6t_vltime
== 0) {
1411 * the following log might be noisy, but this is a typical
1412 * configuration mistake or a tool's bug.
1415 "in6_update_ifa: valid lifetime is 0 for %s\n",
1416 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
1418 if ((lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
1419 || (ifra
->ifra_flags
& IN6_IFF_TEMPORARY
) != 0)
1420 && lt
->ia6t_pltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
1425 * If this is a new address, allocate a new ifaddr and link it
1431 * in6_update_ifa() may be called in a process of a received
1432 * RA; in such a case, we should call malloc with M_NOWAIT.
1433 * The exception to this is during init time or as part of
1434 * handling an ioctl, when we know it's okay to do M_WAITOK.
1436 ia
= in6_ifaddr_alloc(how
);
1439 ifnet_lock_exclusive(ifp
);
1440 IFA_LOCK(&ia
->ia_ifa
);
1441 LIST_INIT(&ia
->ia6_memberships
);
1442 /* Initialize the address and masks, and put time stamp */
1443 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
1444 ia
->ia_addr
.sin6_family
= AF_INET6
;
1445 ia
->ia_addr
.sin6_len
= sizeof(ia
->ia_addr
);
1446 ia
->ia6_createtime
= timenow
.tv_sec
;
1447 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) != 0) {
1449 * XXX: some functions expect that ifa_dstaddr is not
1450 * NULL for p2p interfaces.
1452 ia
->ia_ifa
.ifa_dstaddr
1453 = (struct sockaddr
*)&ia
->ia_dstaddr
;
1455 ia
->ia_ifa
.ifa_dstaddr
= NULL
;
1457 ia
->ia_ifa
.ifa_netmask
1458 = (struct sockaddr
*)&ia
->ia_prefixmask
;
1461 /* if_attach_ifa() holds a reference for ifa_link */
1462 if_attach_ifa(ifp
, &ia
->ia_ifa
);
1463 /* hold a reference for this routine */
1464 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
1465 IFA_UNLOCK(&ia
->ia_ifa
);
1466 ifnet_lock_done(ifp
);
1467 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1468 /* Hold a reference for in6_ifaddrs link */
1469 IFA_ADDREF(&ia
->ia_ifa
);
1470 if ((oia
= in6_ifaddrs
) != NULL
) {
1471 for ( ; oia
->ia_next
; oia
= oia
->ia_next
)
1477 lck_rw_done(&in6_ifaddr_rwlock
);
1479 /* hold a reference for this routine */
1480 IFA_ADDREF(&ia
->ia_ifa
);
1486 /* update timestamp */
1487 ia
->ia6_updatetime
= timenow
.tv_sec
;
1489 /* set prefix mask */
1490 if (ifra
->ifra_prefixmask
.sin6_len
) {
1492 * We prohibit changing the prefix length of an existing
1494 * + such an operation should be rare in IPv6, and
1495 * + the operation would confuse prefix management.
1497 if (ia
->ia_prefixmask
.sin6_len
&&
1498 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
) != plen
) {
1499 log(LOG_INFO
, "in6_update_ifa: the prefix length of an"
1500 " existing (%s) address should not be changed\n",
1501 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1506 ia
->ia_prefixmask
= ifra
->ifra_prefixmask
;
1510 * If a new destination address is specified, scrub the old one and
1511 * install the new destination. Note that the interface must be
1512 * p2p or loopback (see the check above.)
1514 if (dst6
.sin6_family
== AF_INET6
&&
1515 !IN6_ARE_ADDR_EQUAL(&dst6
.sin6_addr
, &ia
->ia_dstaddr
.sin6_addr
)) {
1516 if ((ia
->ia_flags
& IFA_ROUTE
)) {
1520 if ((e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
,
1522 log(LOG_ERR
, "in6_update_ifa: failed to remove "
1523 "a route to the old destination: %s\n",
1524 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1525 /* proceed anyway... */
1529 ia
->ia_flags
&= ~IFA_ROUTE
;
1531 IFA_LOCK_ASSERT_HELD(ifa
);
1532 ia
->ia_dstaddr
= dst6
;
1536 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1537 * to see if the address is deprecated or invalidated, but initialize
1538 * these members for applications.
1540 ia
->ia6_lifetime
= ifra
->ifra_lifetime
;
1541 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
1542 || (ifra
->ifra_flags
& IN6_IFF_TEMPORARY
) != 0) {
1543 ia
->ia6_lifetime
.ia6t_expire
=
1544 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_vltime
;
1546 ia
->ia6_lifetime
.ia6t_expire
= 0;
1547 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
1548 || (ifra
->ifra_flags
& IN6_IFF_TEMPORARY
) != 0) {
1549 ia
->ia6_lifetime
.ia6t_preferred
=
1550 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_pltime
;
1552 ia
->ia6_lifetime
.ia6t_preferred
= 0;
1555 /* reset the interface and routing table appropriately. */
1556 if ((error
= in6_ifinit(ifp
, ia
, &ifra
->ifra_addr
, hostIsNew
)) != 0)
1561 * configure address flags.
1563 ia
->ia6_flags
= ifra
->ifra_flags
;
1565 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1566 * userland, make it deprecated.
1568 if ((ifra
->ifra_flags
& IN6_IFF_DEPRECATED
) != 0) {
1569 ia
->ia6_lifetime
.ia6t_pltime
= 0;
1570 ia
->ia6_lifetime
.ia6t_preferred
= timenow
.tv_sec
;
1573 * Make the address tentative before joining multicast addresses,
1574 * so that corresponding MLD responses would not have a tentative
1577 ia
->ia6_flags
&= ~IN6_IFF_DUPLICATED
; /* safety */
1578 if (hostIsNew
&& in6if_do_dad(ifp
))
1579 ia
->ia6_flags
|= IN6_IFF_TENTATIVE
;
1582 * We are done if we have simply modified an existing address.
1586 /* release reference held for this routine */
1591 * Beyond this point, we should call in6_purgeaddr upon an error,
1592 * not just go to unlink.
1594 IFA_LOCK_ASSERT_HELD(ifa
);
1595 /* Join necessary multicast groups */
1596 if ((ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1597 struct sockaddr_in6 mltaddr
, mltmask
;
1598 struct in6_addr llsol
;
1601 /* join solicited multicast addr for new host id */
1602 bzero(&llsol
, sizeof(struct in6_addr
));
1603 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
1604 llsol
.s6_addr32
[1] = 0;
1605 llsol
.s6_addr32
[2] = htonl(1);
1606 llsol
.s6_addr32
[3] = ifra
->ifra_addr
.sin6_addr
.s6_addr32
[3];
1607 llsol
.s6_addr8
[12] = 0xff;
1608 if ((error
= in6_setscope(&llsol
, ifp
, NULL
)) != 0) {
1609 /* XXX: should not happen */
1610 log(LOG_ERR
, "in6_update_ifa: "
1611 "in6_setscope failed\n");
1615 if ((flags
& IN6_IFAUPDATE_DADDELAY
)) {
1617 * We need a random delay for DAD on the address
1618 * being configured. It also means delaying
1619 * transmission of the corresponding MLD report to
1620 * avoid report collision.
1621 * [draft-ietf-ipv6-rfc2462bis-02.txt]
1624 (MAX_RTR_SOLICITATION_DELAY
* PR_SLOWHZ
);
1626 imm
= in6_joingroup(ifp
, &llsol
, &error
, delay
);
1628 nd6log((LOG_WARNING
,
1629 "in6_update_ifa: addmulti failed for "
1630 "%s on %s (errno=%d)\n",
1631 ip6_sprintf(&llsol
), if_name(ifp
),
1633 in6_purgeaddr((struct ifaddr
*)ia
);
1634 /* release reference held for this routine */
1638 in6m_sol
= imm
->i6mm_maddr
;
1639 /* take a refcount for this routine */
1640 IN6M_ADDREF(in6m_sol
);
1643 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
1646 bzero(&mltmask
, sizeof(mltmask
));
1647 mltmask
.sin6_len
= sizeof(struct sockaddr_in6
);
1648 mltmask
.sin6_family
= AF_INET6
;
1649 mltmask
.sin6_addr
= in6mask32
;
1650 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
1653 * join link-local all-nodes address
1655 bzero(&mltaddr
, sizeof(mltaddr
));
1656 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
1657 mltaddr
.sin6_family
= AF_INET6
;
1658 mltaddr
.sin6_addr
= in6addr_linklocal_allnodes
;
1659 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
)) !=
1661 goto cleanup
; /* XXX: should not fail */
1664 * XXX: do we really need this automatic routes?
1665 * We should probably reconsider this stuff. Most applications
1666 * actually do not need the routes, since they usually specify
1667 * the outgoing interface.
1669 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
1670 ia
->ia_ifp
->if_index
);
1672 if (memcmp(&mltaddr
.sin6_addr
,
1673 &((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
,
1680 error
= rtrequest_scoped(RTM_ADD
,
1681 (struct sockaddr
*)&mltaddr
,
1682 (struct sockaddr
*)&ia
->ia_addr
,
1683 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
1684 NULL
, ia
->ia_ifp
->if_index
);
1691 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
1693 nd6log((LOG_WARNING
,
1694 "in6_update_ifa: addmulti failed for "
1695 "%s on %s (errno=%d)\n",
1696 ip6_sprintf(&mltaddr
.sin6_addr
),
1697 if_name(ifp
), error
));
1701 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
1705 * join node information group address
1707 #define hostnamelen strlen(hostname)
1709 if ((flags
& IN6_IFAUPDATE_DADDELAY
)) {
1711 * The spec doesn't say anything about delay for this
1712 * group, but the same logic should apply.
1715 (MAX_RTR_SOLICITATION_DELAY
* PR_SLOWHZ
);
1717 if (in6_nigroup(ifp
, hostname
, hostnamelen
, &mltaddr
.sin6_addr
)
1719 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
,
1720 delay
); /* XXX jinmei */
1722 nd6log((LOG_WARNING
, "in6_update_ifa: "
1723 "addmulti failed for %s on %s "
1725 ip6_sprintf(&mltaddr
.sin6_addr
),
1726 if_name(ifp
), error
));
1727 /* XXX not very fatal, go on... */
1730 LIST_INSERT_HEAD(&ia
->ia6_memberships
,
1738 * join interface-local all-nodes address.
1739 * (ff01::1%ifN, and ff01::%ifN/32)
1741 mltaddr
.sin6_addr
= in6addr_nodelocal_allnodes
;
1742 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
))
1744 goto cleanup
; /* XXX: should not fail */
1745 /* XXX: again, do we really need the route? */
1746 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
1747 ia
->ia_ifp
->if_index
);
1749 if (memcmp(&mltaddr
.sin6_addr
,
1750 &((struct sockaddr_in6
*)rt_key(rt
))->sin6_addr
,
1757 error
= rtrequest_scoped(RTM_ADD
,
1758 (struct sockaddr
*)&mltaddr
,
1759 (struct sockaddr
*)&ia
->ia_addr
,
1760 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
1761 NULL
, ia
->ia_ifp
->if_index
);
1767 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
1769 nd6log((LOG_WARNING
, "in6_update_ifa: "
1770 "addmulti failed for %s on %s "
1772 ip6_sprintf(&mltaddr
.sin6_addr
),
1773 if_name(ifp
), error
));
1777 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
1778 /* keep it locked */
1781 IFA_LOCK_ASSERT_HELD(ifa
);
1783 * Make sure to initialize ND6 information. this is to workaround
1784 * issues with interfaces with IPv6 addresses, which have never brought
1785 * up. We are assuming that it is safe to nd6_ifattach multiple times.
1786 * NOTE: this is how stf0 gets initialized
1788 if ((error
= nd6_ifattach(ifp
)) != 0)
1792 * Perform DAD, if needed.
1793 * XXX It may be of use, if we can administratively
1796 if (hostIsNew
&& in6if_do_dad(ifp
) &&
1797 ((ifra
->ifra_flags
& IN6_IFF_NODAD
) == 0) &&
1798 (ia
->ia6_flags
& IN6_IFF_TENTATIVE
))
1800 int mindelay
, maxdelay
;
1804 if ((flags
& IN6_IFAUPDATE_DADDELAY
)) {
1806 * We need to impose a delay before sending an NS
1807 * for DAD. Check if we also needed a delay for the
1808 * corresponding MLD message. If we did, the delay
1809 * should be larger than the MLD delay (this could be
1810 * relaxed a bit, but this simple logic is at least
1814 if (in6m_sol
!= NULL
) {
1815 IN6M_LOCK(in6m_sol
);
1816 if (in6m_sol
->in6m_state
== MLD_REPORTING_MEMBER
)
1817 mindelay
= in6m_sol
->in6m_timer
;
1818 IN6M_UNLOCK(in6m_sol
);
1820 maxdelay
= MAX_RTR_SOLICITATION_DELAY
* hz
;
1821 if (maxdelay
- mindelay
== 0)
1825 (random() % (maxdelay
- mindelay
)) +
1829 nd6_dad_start((struct ifaddr
*)ia
, &delay
);
1834 /* release reference held for this routine */
1837 if (in6m_sol
!= NULL
)
1838 IN6M_REMREF(in6m_sol
);
1843 * XXX: if a change of an existing address failed, keep the entry
1847 in6_unlink_ifa(ia
, ifp
);
1852 in6_purgeaddr(&ia
->ia_ifa
);
1857 in6_purgeaddr(struct ifaddr
*ifa
)
1859 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1860 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*) ifa
;
1861 struct in6_multi_mship
*imm
;
1863 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1865 /* stop DAD processing */
1869 * delete route to the destination of the address being purged.
1870 * The interface must be p2p or loopback in this case.
1873 if ((ia
->ia_flags
& IFA_ROUTE
) != 0 && ia
->ia_dstaddr
.sin6_len
!= 0) {
1877 if ((e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
))
1879 log(LOG_ERR
, "in6_purgeaddr: failed to remove "
1880 "a route to the p2p destination: %s on %s, "
1882 ip6_sprintf(&ia
->ia_addr
.sin6_addr
), if_name(ifp
),
1884 /* proceed anyway... */
1887 ia
->ia_flags
&= ~IFA_ROUTE
;
1893 IFA_LOCK_ASSERT_NOTHELD(ifa
);
1895 /* Remove ownaddr's loopback rtentry, if it exists. */
1896 in6_ifremloop(&(ia
->ia_ifa
));
1899 * leave from multicast groups we have joined for the interface
1902 while ((imm
= ia
->ia6_memberships
.lh_first
) != NULL
) {
1903 LIST_REMOVE(imm
, i6mm_chain
);
1905 in6_leavegroup(imm
);
1910 /* in6_unlink_ifa() will need exclusive access */
1911 in6_unlink_ifa(ia
, ifp
);
1912 in6_post_msg(ifp
, KEV_INET6_ADDR_DELETED
, ia
);
1916 in6_unlink_ifa(struct in6_ifaddr
*ia
, struct ifnet
*ifp
)
1918 struct in6_ifaddr
*oia
;
1922 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1927 ifnet_lock_exclusive(ifp
);
1929 if (ifa
->ifa_debug
& IFD_ATTACHED
)
1930 if_detach_ifa(ifp
, ifa
);
1932 ifnet_lock_done(ifp
);
1935 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1937 if (oia
== (ia
= in6_ifaddrs
)) {
1938 in6_ifaddrs
= ia
->ia_next
;
1940 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
1943 ia
->ia_next
= oia
->ia_next
;
1946 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1952 * When an autoconfigured address is being removed, release the
1953 * reference to the base prefix. Also, since the release might
1954 * affect the status of other (detached) addresses, call
1955 * pfxlist_onlink_check().
1959 if ((oia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0) {
1960 if (oia
->ia6_ndpr
== NULL
) {
1961 log(LOG_NOTICE
, "in6_unlink_ifa: autoconf'ed address "
1962 "%p has no prefix\n", oia
);
1964 struct nd_prefix
*pr
= oia
->ia6_ndpr
;
1966 oia
->ia6_flags
&= ~IN6_IFF_AUTOCONF
;
1967 oia
->ia6_ndpr
= NULL
;
1969 VERIFY(pr
->ndpr_addrcnt
!= 0);
1972 NDPR_REMREF(pr
); /* release addr reference */
1975 lck_rw_done(&in6_ifaddr_rwlock
);
1976 lck_mtx_lock(nd6_mutex
);
1977 pfxlist_onlink_check();
1978 lck_mtx_unlock(nd6_mutex
);
1981 lck_rw_done(&in6_ifaddr_rwlock
);
1985 * release another refcnt for the link from in6_ifaddrs.
1986 * Do this only if it's not already unlinked in the event that we lost
1987 * the race, since in6_ifaddr_rwlock was momentarily dropped above.
1992 /* release reference held for this routine */
1997 in6_purgeif(struct ifnet
*ifp
)
1999 struct in6_ifaddr
*ia
;
2004 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2006 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2008 while (ia
!= NULL
) {
2009 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
2013 IFA_ADDREF(&ia
->ia_ifa
); /* for us */
2014 lck_rw_done(&in6_ifaddr_rwlock
);
2015 in6_purgeaddr(&ia
->ia_ifa
);
2016 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2017 IFA_REMREF(&ia
->ia_ifa
); /* for us */
2019 * Purging the address would have caused
2020 * in6_ifaddr_rwlock to be dropped and reacquired;
2021 * therefore search again from the beginning
2022 * of in6_ifaddrs list.
2026 lck_rw_done(&in6_ifaddr_rwlock
);
2033 * SIOCGLIFADDR: get first address. (?)
2034 * SIOCGLIFADDR with IFLR_PREFIX:
2035 * get first address that matches the specified prefix.
2036 * SIOCALIFADDR: add the specified address.
2037 * SIOCALIFADDR with IFLR_PREFIX:
2038 * add the specified prefix, filling hostaddr part from
2039 * the first link-local address. prefixlen must be <= 64.
2040 * SIOCDLIFADDR: delete the specified address.
2041 * SIOCDLIFADDR with IFLR_PREFIX:
2042 * delete the first address that matches the specified prefix.
2044 * EINVAL on invalid parameters
2045 * EADDRNOTAVAIL on prefix match failed/specified address not found
2046 * other values may be returned from in6_ioctl()
2048 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
2049 * this is to accomodate address naming scheme other than RFC2374,
2051 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
2052 * address encoding scheme. (see figure on page 8)
2055 in6_lifaddr_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
2056 struct ifnet
*ifp
, struct proc
*p
)
2058 struct if_laddrreq
*iflr
= (struct if_laddrreq
*)data
;
2059 struct ifaddr
*ifa
= NULL
;
2060 struct sockaddr
*sa
;
2061 int p64
= proc_is64bit(p
);
2064 if (!data
|| !ifp
) {
2065 panic("invalid argument to in6_lifaddr_ioctl");
2071 /* address must be specified on GET with IFLR_PREFIX */
2072 if ((iflr
->flags
& IFLR_PREFIX
) == 0)
2077 /* address must be specified on ADD and DELETE */
2078 sa
= (struct sockaddr
*)&iflr
->addr
;
2079 if (sa
->sa_family
!= AF_INET6
)
2081 if (sa
->sa_len
!= sizeof(struct sockaddr_in6
))
2083 /* XXX need improvement */
2084 sa
= (struct sockaddr
*)&iflr
->dstaddr
;
2085 if (sa
->sa_family
&& sa
->sa_family
!= AF_INET6
)
2087 if (sa
->sa_len
&& sa
->sa_len
!= sizeof(struct sockaddr_in6
))
2090 default: /* shouldn't happen */
2092 panic("invalid cmd to in6_lifaddr_ioctl");
2098 if (sizeof(struct in6_addr
) * 8 < iflr
->prefixlen
)
2104 struct in6_aliasreq ifra
;
2105 struct in6_addr hostaddr
;
2107 int hostid_found
= 0;
2109 if ((iflr
->flags
& IFLR_PREFIX
) != 0) {
2110 struct sockaddr_in6
*sin6
;
2113 * hostaddr is to fill in the hostaddr part of the
2114 * address. hostaddr points to the first link-local
2115 * address attached to the interface.
2117 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
, 0);
2119 return EADDRNOTAVAIL
;
2121 hostaddr
= *IFA_IN6(ifa
);
2127 /* prefixlen must be <= 64. */
2128 if (64 < iflr
->prefixlen
)
2130 prefixlen
= iflr
->prefixlen
;
2132 /* hostid part must be zero. */
2133 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
2134 if (sin6
->sin6_addr
.s6_addr32
[2] != 0 ||
2135 sin6
->sin6_addr
.s6_addr32
[3] != 0) {
2139 prefixlen
= iflr
->prefixlen
;
2141 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
2142 bzero(&ifra
, sizeof(ifra
));
2143 bcopy(iflr
->iflr_name
, ifra
.ifra_name
, sizeof(ifra
.ifra_name
));
2145 bcopy(&iflr
->addr
, &ifra
.ifra_addr
,
2146 ((struct sockaddr
*)&iflr
->addr
)->sa_len
);
2148 /* fill in hostaddr part */
2149 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] =
2150 hostaddr
.s6_addr32
[2];
2151 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] =
2152 hostaddr
.s6_addr32
[3];
2155 if (((struct sockaddr
*)&iflr
->dstaddr
)->sa_family
) { /*XXX*/
2156 bcopy(&iflr
->dstaddr
, &ifra
.ifra_dstaddr
,
2157 ((struct sockaddr
*)&iflr
->dstaddr
)->sa_len
);
2159 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[2] =
2160 hostaddr
.s6_addr32
[2];
2161 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[3] =
2162 hostaddr
.s6_addr32
[3];
2166 ifra
.ifra_prefixmask
.sin6_len
= sizeof(struct sockaddr_in6
);
2167 in6_prefixlen2mask(&ifra
.ifra_prefixmask
.sin6_addr
, prefixlen
);
2169 ifra
.ifra_flags
= iflr
->flags
& ~IFLR_PREFIX
;
2171 #if defined(__LP64__)
2172 struct in6_aliasreq_32 ifra_32
;
2174 * Use 32-bit ioctl and structure for 32-bit process.
2176 in6_aliasreq_64_to_32((struct in6_aliasreq_64
*)&ifra
,
2178 return (in6_control(so
, SIOCAIFADDR_IN6_32
,
2179 (caddr_t
)&ifra_32
, ifp
, p
));
2181 return (in6_control(so
, SIOCAIFADDR_IN6
,
2182 (caddr_t
)&ifra
, ifp
, p
));
2183 #endif /* __LP64__ */
2185 #if defined(__LP64__)
2186 return (in6_control(so
, SIOCAIFADDR_IN6
,
2187 (caddr_t
)&ifra
, ifp
, p
));
2189 struct in6_aliasreq_64 ifra_64
;
2191 * Use 64-bit ioctl and structure for 64-bit process.
2193 in6_aliasreq_32_to_64((struct in6_aliasreq_32
*)&ifra
,
2195 return (in6_control(so
, SIOCAIFADDR_IN6_64
,
2196 (caddr_t
)&ifra_64
, ifp
, p
));
2197 #endif /* __LP64__ */
2204 struct in6_ifaddr
*ia
;
2205 struct in6_addr mask
, candidate
, match
;
2206 struct sockaddr_in6
*sin6
;
2209 bzero(&mask
, sizeof(mask
));
2210 if (iflr
->flags
& IFLR_PREFIX
) {
2211 /* lookup a prefix rather than address. */
2212 in6_prefixlen2mask(&mask
, iflr
->prefixlen
);
2214 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
2215 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
2216 match
.s6_addr32
[0] &= mask
.s6_addr32
[0];
2217 match
.s6_addr32
[1] &= mask
.s6_addr32
[1];
2218 match
.s6_addr32
[2] &= mask
.s6_addr32
[2];
2219 match
.s6_addr32
[3] &= mask
.s6_addr32
[3];
2221 /* if you set extra bits, that's wrong */
2222 if (bcmp(&match
, &sin6
->sin6_addr
, sizeof(match
)))
2227 if (cmd
== SIOCGLIFADDR
) {
2228 /* on getting an address, take the 1st match */
2231 /* on deleting an address, do exact match */
2232 in6_prefixlen2mask(&mask
, 128);
2233 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
2234 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
2240 ifnet_lock_shared(ifp
);
2241 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2244 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2253 bcopy(IFA_IN6(ifa
), &candidate
, sizeof(candidate
));
2256 * XXX: this is adhoc, but is necessary to allow
2257 * a user to specify fe80::/64 (not /10) for a
2258 * link-local address.
2260 if (IN6_IS_ADDR_LINKLOCAL(&candidate
))
2261 candidate
.s6_addr16
[1] = 0;
2262 candidate
.s6_addr32
[0] &= mask
.s6_addr32
[0];
2263 candidate
.s6_addr32
[1] &= mask
.s6_addr32
[1];
2264 candidate
.s6_addr32
[2] &= mask
.s6_addr32
[2];
2265 candidate
.s6_addr32
[3] &= mask
.s6_addr32
[3];
2266 if (IN6_ARE_ADDR_EQUAL(&candidate
, &match
))
2271 ifnet_lock_done(ifp
);
2273 return EADDRNOTAVAIL
;
2276 if (cmd
== SIOCGLIFADDR
) {
2277 struct sockaddr_in6
*s6
;
2280 /* fill in the if_laddrreq structure */
2281 bcopy(&ia
->ia_addr
, &iflr
->addr
, ia
->ia_addr
.sin6_len
);
2282 s6
= (struct sockaddr_in6
*)&iflr
->addr
;
2283 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
2284 s6
->sin6_addr
.s6_addr16
[1] = 0;
2286 in6_addr2scopeid(ifp
, &s6
->sin6_addr
);
2288 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
2289 bcopy(&ia
->ia_dstaddr
, &iflr
->dstaddr
,
2290 ia
->ia_dstaddr
.sin6_len
);
2291 s6
= (struct sockaddr_in6
*)&iflr
->dstaddr
;
2292 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
2293 s6
->sin6_addr
.s6_addr16
[1] = 0;
2295 in6_addr2scopeid(ifp
,
2299 bzero(&iflr
->dstaddr
, sizeof(iflr
->dstaddr
));
2302 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
2305 iflr
->flags
= ia
->ia6_flags
; /* XXX */
2310 struct in6_aliasreq ifra
;
2312 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
2313 bzero(&ifra
, sizeof(ifra
));
2314 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
2315 sizeof(ifra
.ifra_name
));
2318 bcopy(&ia
->ia_addr
, &ifra
.ifra_addr
,
2319 ia
->ia_addr
.sin6_len
);
2320 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
2321 bcopy(&ia
->ia_dstaddr
, &ifra
.ifra_dstaddr
,
2322 ia
->ia_dstaddr
.sin6_len
);
2324 bzero(&ifra
.ifra_dstaddr
,
2325 sizeof(ifra
.ifra_dstaddr
));
2327 bcopy(&ia
->ia_prefixmask
, &ifra
.ifra_dstaddr
,
2328 ia
->ia_prefixmask
.sin6_len
);
2330 ifra
.ifra_flags
= ia
->ia6_flags
;
2334 #if defined(__LP64__)
2335 struct in6_aliasreq_32 ifra_32
;
2337 * Use 32-bit structure for 32-bit process.
2338 * SIOCDIFADDR_IN6 is encoded with in6_ifreq,
2339 * so it stays the same since the size does
2340 * not change. The data part of the ioctl,
2341 * however, is of a different structure, i.e.
2344 in6_aliasreq_64_to_32(
2345 (struct in6_aliasreq_64
*)&ifra
, &ifra_32
);
2346 return (in6_control(so
, SIOCDIFADDR_IN6
,
2347 (caddr_t
)&ifra_32
, ifp
, p
));
2349 return (in6_control(so
, SIOCDIFADDR_IN6
,
2350 (caddr_t
)&ifra
, ifp
, p
));
2351 #endif /* __LP64__ */
2353 #if defined(__LP64__)
2354 return (in6_control(so
, SIOCDIFADDR_IN6
,
2355 (caddr_t
)&ifra
, ifp
, p
));
2357 struct in6_aliasreq_64 ifra_64
;
2359 * Use 64-bit structure for 64-bit process.
2360 * SIOCDIFADDR_IN6 is encoded with in6_ifreq,
2361 * so it stays the same since the size does
2362 * not change. The data part of the ioctl,
2363 * however, is of a different structure, i.e.
2366 in6_aliasreq_32_to_64(
2367 (struct in6_aliasreq_32
*)&ifra
, &ifra_64
);
2368 return (in6_control(so
, SIOCDIFADDR_IN6
,
2369 (caddr_t
)&ifra_64
, ifp
, p
));
2370 #endif /* __LP64__ */
2377 return EOPNOTSUPP
; /* just for safety */
2381 * Initialize an interface's intetnet6 address
2382 * and routing table entry.
2385 in6_ifinit(ifp
, ia
, sin6
, newhost
)
2387 struct in6_ifaddr
*ia
;
2388 struct sockaddr_in6
*sin6
;
2391 int error
= 0, plen
, ifacount
= 0;
2395 * Give the interface a chance to initialize
2396 * if this is its first address,
2397 * and to validate the address if necessary.
2399 ifnet_lock_shared(ifp
);
2400 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2403 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2410 ifnet_lock_done(ifp
);
2414 ia
->ia_addr
= *sin6
;
2417 if (ifacount
<= 1 &&
2418 (error
= ifnet_ioctl(ifp
, PF_INET6
, SIOCSIFADDR
, ia
))) {
2419 if (error
== EOPNOTSUPP
)
2426 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
2428 /* we could do in(6)_socktrim here, but just omit it at this moment. */
2432 * If the destination address is specified for a point-to-point
2433 * interface, install a route to the destination as an interface
2436 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
); /* XXX */
2437 if (plen
== 128 && ia
->ia_dstaddr
.sin6_family
== AF_INET6
) {
2439 if ((error
= rtinit(&(ia
->ia_ifa
), (int)RTM_ADD
,
2440 RTF_UP
| RTF_HOST
)) != 0)
2443 ia
->ia_flags
|= IFA_ROUTE
;
2445 IFA_LOCK_ASSERT_HELD(ifa
);
2448 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
2450 ia
->ia_ifa
.ifa_flags
|= RTF_CLONING
;
2453 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
2455 /* set the rtrequest function to create llinfo */
2456 ia
->ia_ifa
.ifa_rtrequest
= nd6_rtrequest
;
2458 in6_ifaddloop(&(ia
->ia_ifa
));
2467 in6_purgeaddrs(struct ifnet
*ifp
)
2473 * Find an IPv6 interface link-local address specific to an interface.
2476 in6ifa_ifpforlinklocal(ifp
, ignoreflags
)
2482 ifnet_lock_shared(ifp
);
2483 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2486 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2490 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
))) {
2491 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2492 ignoreflags
) != 0) {
2496 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2502 ifnet_lock_done(ifp
);
2504 return((struct in6_ifaddr
*)ifa
);
2508 * find the internet address corresponding to a given interface and address.
2511 in6ifa_ifpwithaddr(ifp
, addr
)
2513 struct in6_addr
*addr
;
2517 ifnet_lock_shared(ifp
);
2518 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2521 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2525 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(ifa
))) {
2526 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2532 ifnet_lock_done(ifp
);
2534 return((struct in6_ifaddr
*)ifa
);
2538 * Convert IP6 address to printable (loggable) representation.
2540 static char digits
[] = "0123456789abcdef";
2541 static int ip6round
= 0;
2544 const struct in6_addr
*addr
;
2546 static char ip6buf
[8][48];
2549 const u_short
*a
= (const u_short
*)addr
;
2553 ip6round
= (ip6round
+ 1) & 7;
2554 cp
= ip6buf
[ip6round
];
2556 for (i
= 0; i
< 8; i
++) {
2567 if (dcolon
== 0 && *(a
+ 1) == 0) {
2579 d
= (const u_char
*)a
;
2580 *cp
++ = digits
[*d
>> 4];
2581 *cp
++ = digits
[*d
++ & 0xf];
2582 *cp
++ = digits
[*d
>> 4];
2583 *cp
++ = digits
[*d
& 0xf];
2588 return(ip6buf
[ip6round
]);
2592 in6addr_local(struct in6_addr
*in6
)
2595 struct sockaddr_in6 sin6
;
2598 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_SCOPE_LINKLOCAL(in6
))
2601 sin6
.sin6_family
= AF_INET6
;
2602 sin6
.sin6_len
= sizeof (sin6
);
2603 bcopy(in6
, &sin6
.sin6_addr
, sizeof (*in6
));
2604 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0);
2608 if (rt
->rt_gateway
->sa_family
== AF_LINK
)
2613 local
= in6_localaddr(in6
);
2619 in6_localaddr(struct in6_addr
*in6
)
2621 struct in6_ifaddr
*ia
;
2623 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
))
2626 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2627 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2628 IFA_LOCK_SPIN(&ia
->ia_ifa
);
2629 if (IN6_ARE_MASKED_ADDR_EQUAL(in6
, &ia
->ia_addr
.sin6_addr
,
2630 &ia
->ia_prefixmask
.sin6_addr
)) {
2631 IFA_UNLOCK(&ia
->ia_ifa
);
2632 lck_rw_done(&in6_ifaddr_rwlock
);
2635 IFA_UNLOCK(&ia
->ia_ifa
);
2637 lck_rw_done(&in6_ifaddr_rwlock
);
2642 in6_is_addr_deprecated(struct sockaddr_in6
*sa6
)
2644 struct in6_ifaddr
*ia
;
2646 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2647 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2648 IFA_LOCK_SPIN(&ia
->ia_ifa
);
2649 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
,
2651 (ia
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0) {
2652 IFA_UNLOCK(&ia
->ia_ifa
);
2653 lck_rw_done(&in6_ifaddr_rwlock
);
2654 return(1); /* true */
2656 /* XXX: do we still have to go thru the rest of the list? */
2657 IFA_UNLOCK(&ia
->ia_ifa
);
2660 lck_rw_done(&in6_ifaddr_rwlock
);
2661 return(0); /* false */
2665 * return length of part which dst and src are equal
2669 in6_matchlen(src
, dst
)
2670 struct in6_addr
*src
, *dst
;
2673 u_char
*s
= (u_char
*)src
, *d
= (u_char
*)dst
;
2674 u_char
*lim
= s
+ 16, r
;
2677 if ((r
= (*d
++ ^ *s
++)) != 0) {
2688 /* XXX: to be scope conscious */
2690 in6_are_prefix_equal(p1
, p2
, len
)
2691 struct in6_addr
*p1
, *p2
;
2694 int bytelen
, bitlen
;
2697 if (0 > len
|| len
> 128) {
2698 log(LOG_ERR
, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2706 if (bcmp(&p1
->s6_addr
, &p2
->s6_addr
, bytelen
))
2709 p1
->s6_addr
[bytelen
] >> (8 - bitlen
) !=
2710 p2
->s6_addr
[bytelen
] >> (8 - bitlen
))
2717 in6_prefixlen2mask(maskp
, len
)
2718 struct in6_addr
*maskp
;
2721 u_char maskarray
[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2722 int bytelen
, bitlen
, i
;
2725 if (0 > len
|| len
> 128) {
2726 log(LOG_ERR
, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2731 bzero(maskp
, sizeof(*maskp
));
2734 for (i
= 0; i
< bytelen
; i
++)
2735 maskp
->s6_addr
[i
] = 0xff;
2737 maskp
->s6_addr
[bytelen
] = maskarray
[bitlen
- 1];
2741 * return the best address out of the same scope
2746 struct in6_addr
*dst
)
2748 int dst_scope
= in6_addrscope(dst
), src_scope
, best_scope
= 0;
2752 struct in6_ifaddr
*ifa_best
= NULL
;
2755 /* output interface is not specified */
2760 * We search for all addresses on all interfaces from the beginning.
2761 * Comparing an interface with the outgoing interface will be done
2762 * only at the final stage of tiebreaking.
2764 ifnet_head_lock_shared();
2765 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
2767 * We can never take an address that breaks the scope zone
2768 * of the destination.
2770 if (in6_addr2scopeid(ifp
, dst
) != in6_addr2scopeid(oifp
, dst
))
2773 ifnet_lock_shared(ifp
);
2774 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2776 int tlen
= -1, dscopecmp
, bscopecmp
, matchcmp
;
2779 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2783 src_scope
= in6_addrscope(IFA_IN6(ifa
));
2786 * Don't use an address before completing DAD
2787 * nor a duplicated address.
2789 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2794 /* XXX: is there any case to allow anycasts? */
2795 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2800 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2806 * If this is the first address we find,
2809 if (ifa_best
== NULL
)
2813 * ifa_best is never NULL beyond this line except
2814 * within the block labeled "replace".
2818 * If ifa_best has a smaller scope than dst and
2819 * the current address has a larger one than
2820 * (or equal to) dst, always replace ifa_best.
2821 * Also, if the current address has a smaller scope
2822 * than dst, ignore it unless ifa_best also has a
2824 * Consequently, after the two if-clause below,
2825 * the followings must be satisfied:
2826 * (scope(src) < scope(dst) &&
2827 * scope(best) < scope(dst))
2829 * (scope(best) >= scope(dst) &&
2830 * scope(src) >= scope(dst))
2832 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0 &&
2833 IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) >= 0)
2834 goto replace
; /* (A) */
2835 if (IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) < 0 &&
2836 IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) >= 0) {
2841 * A deprecated address SHOULD NOT be used in new
2842 * communications if an alternate (non-deprecated)
2843 * address is available and has sufficient scope.
2844 * RFC 2462, Section 5.5.4.
2846 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2847 IN6_IFF_DEPRECATED
) {
2849 * Ignore any deprecated addresses if
2850 * specified by configuration.
2852 if (!ip6_use_deprecated
) {
2857 * If we have already found a non-deprecated
2858 * candidate, just ignore deprecated addresses.
2860 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
)
2868 * A non-deprecated address is always preferred
2869 * to a deprecated one regardless of scopes and
2870 * address matching (Note invariants ensured by the
2871 * conditions (A) and (B) above.)
2873 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) &&
2874 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2875 IN6_IFF_DEPRECATED
) == 0)
2879 * When we use temporary addresses described in
2880 * RFC 4941, we prefer temporary addresses to
2881 * public autoconf addresses. Again, note the
2882 * invariants from (A) and (B). Also note that we
2883 * don't have any preference between static addresses
2884 * and autoconf addresses (despite of whether or not
2885 * the latter is temporary or public.)
2887 if (ip6_use_tempaddr
) {
2888 struct in6_ifaddr
*ifat
;
2890 ifat
= (struct in6_ifaddr
*)ifa
;
2891 if ((ifa_best
->ia6_flags
&
2892 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2893 == IN6_IFF_AUTOCONF
&&
2895 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2896 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
)) {
2899 if ((ifa_best
->ia6_flags
&
2900 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2901 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
) &&
2903 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2904 == IN6_IFF_AUTOCONF
) {
2911 * At this point, we have two cases:
2912 * 1. we are looking at a non-deprecated address,
2913 * and ifa_best is also non-deprecated.
2914 * 2. we are looking at a deprecated address,
2915 * and ifa_best is also deprecated.
2916 * Also, we do not have to consider a case where
2917 * the scope of if_best is larger(smaller) than dst and
2918 * the scope of the current address is smaller(larger)
2919 * than dst. Such a case has already been covered.
2920 * Tiebreaking is done according to the following
2922 * - the scope comparison between the address and
2924 * - the scope comparison between the address and
2925 * ifa_best (bscopecmp)
2926 * - if the address match dst longer than ifa_best
2928 * - if the address is on the outgoing I/F (outI/F)
2930 * Roughly speaking, the selection policy is
2931 * - the most important item is scope. The same scope
2932 * is best. Then search for a larger scope.
2933 * Smaller scopes are the last resort.
2934 * - A deprecated address is chosen only when we have
2935 * no address that has an enough scope, but is
2936 * prefered to any addresses of smaller scopes
2937 * (this must be already done above.)
2938 * - addresses on the outgoing I/F are preferred to
2939 * ones on other interfaces if none of above
2940 * tiebreaks. In the table below, the column "bI"
2941 * means if the best_ifa is on the outgoing
2942 * interface, and the column "sI" means if the ifa
2943 * is on the outgoing interface.
2944 * - If there is no other reasons to choose one,
2945 * longest address match against dst is considered.
2947 * The precise decision table is as follows:
2948 * dscopecmp bscopecmp match bI oI | replace?
2949 * N/A equal N/A Y N | No (1)
2950 * N/A equal N/A N Y | Yes (2)
2951 * N/A equal larger N/A | Yes (3)
2952 * N/A equal !larger N/A | No (4)
2953 * larger larger N/A N/A | No (5)
2954 * larger smaller N/A N/A | Yes (6)
2955 * smaller larger N/A N/A | Yes (7)
2956 * smaller smaller N/A N/A | No (8)
2957 * equal smaller N/A N/A | Yes (9)
2958 * equal larger (already done at A above)
2960 dscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
);
2961 bscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, best_scope
);
2963 if (bscopecmp
== 0) {
2964 struct ifnet
*bifp
= ifa_best
->ia_ifp
;
2966 if (bifp
== oifp
&& ifp
!= oifp
) { /* (1) */
2970 if (bifp
!= oifp
&& ifp
== oifp
) /* (2) */
2974 * Both bifp and ifp are on the outgoing
2975 * interface, or both two are on a different
2976 * interface from the outgoing I/F.
2977 * now we need address matching against dst
2980 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
2981 matchcmp
= tlen
- blen
;
2982 if (matchcmp
> 0) /* (3) */
2987 if (dscopecmp
> 0) {
2988 if (bscopecmp
> 0) { /* (5) */
2992 goto replace
; /* (6) */
2994 if (dscopecmp
< 0) {
2995 if (bscopecmp
> 0) /* (7) */
3001 /* now dscopecmp must be 0 */
3003 goto replace
; /* (9) */
3006 IFA_ADDREF_LOCKED(ifa
); /* for ifa_best */
3007 blen
= tlen
>= 0 ? tlen
:
3008 in6_matchlen(IFA_IN6(ifa
), dst
);
3009 best_scope
= in6_addrscope(&ifa2ia6(ifa
)->ia_addr
.sin6_addr
);
3012 IFA_REMREF(&ifa_best
->ia_ifa
);
3013 ifa_best
= (struct in6_ifaddr
*)ifa
;
3015 ifnet_lock_done(ifp
);
3019 /* count statistics for future improvements */
3020 if (ifa_best
== NULL
)
3021 ip6stat
.ip6s_sources_none
++;
3023 IFA_LOCK_SPIN(&ifa_best
->ia_ifa
);
3024 if (oifp
== ifa_best
->ia_ifp
)
3025 ip6stat
.ip6s_sources_sameif
[best_scope
]++;
3027 ip6stat
.ip6s_sources_otherif
[best_scope
]++;
3029 if (best_scope
== dst_scope
)
3030 ip6stat
.ip6s_sources_samescope
[best_scope
]++;
3032 ip6stat
.ip6s_sources_otherscope
[best_scope
]++;
3034 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0)
3035 ip6stat
.ip6s_sources_deprecated
[best_scope
]++;
3036 IFA_UNLOCK(&ifa_best
->ia_ifa
);
3043 * return the best address out of the same scope. if no address was
3044 * found, return the first valid address from designated IF.
3049 struct in6_addr
*dst
)
3051 int dst_scope
= in6_addrscope(dst
), blen
= -1, tlen
;
3053 struct in6_ifaddr
*besta
= NULL
;
3054 struct in6_ifaddr
*dep
[2]; /* last-resort: deprecated */
3056 dep
[0] = dep
[1] = NULL
;
3059 * We first look for addresses in the same scope.
3060 * If there is one, return it.
3061 * If two or more, return one which matches the dst longest.
3062 * If none, return one of global addresses assigned other ifs.
3064 ifnet_lock_shared(ifp
);
3065 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
3068 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3072 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3074 continue; /* XXX: is there any case to allow anycast? */
3076 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_NOTREADY
) {
3078 continue; /* don't use this interface */
3080 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3084 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3085 if (ip6_use_deprecated
) {
3086 IFA_ADDREF_LOCKED(ifa
); /* for dep[0] */
3089 IFA_REMREF(&dep
[0]->ia_ifa
);
3090 dep
[0] = (struct in6_ifaddr
*)ifa
;
3097 if (dst_scope
== in6_addrscope(IFA_IN6(ifa
))) {
3099 * call in6_matchlen() as few as possible
3104 IFA_LOCK(&besta
->ia_ifa
);
3105 blen
= in6_matchlen(&besta
->ia_addr
.sin6_addr
, dst
);
3106 IFA_UNLOCK(&besta
->ia_ifa
);
3109 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3112 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3114 IFA_REMREF(&besta
->ia_ifa
);
3115 besta
= (struct in6_ifaddr
*)ifa
;
3120 besta
= (struct in6_ifaddr
*)ifa
;
3121 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3129 ifnet_lock_done(ifp
);
3131 IFA_REMREF(&dep
[0]->ia_ifa
);
3135 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
3138 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3142 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3144 continue; /* XXX: is there any case to allow anycast? */
3146 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_NOTREADY
) {
3148 continue; /* don't use this interface */
3150 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3154 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3155 if (ip6_use_deprecated
) {
3156 IFA_ADDREF_LOCKED(ifa
); /* for dep[1] */
3159 IFA_REMREF(&dep
[1]->ia_ifa
);
3160 dep
[1] = (struct in6_ifaddr
*)ifa
;
3166 IFA_ADDREF_LOCKED(ifa
); /* for caller */
3168 ifnet_lock_done(ifp
);
3170 IFA_REMREF(&dep
[0]->ia_ifa
);
3172 IFA_REMREF(&dep
[1]->ia_ifa
);
3173 return (struct in6_ifaddr
*)ifa
;
3175 ifnet_lock_done(ifp
);
3177 /* use the last-resort values, that are, deprecated addresses */
3180 IFA_REMREF(&dep
[1]->ia_ifa
);
3190 * perform DAD when interface becomes IFF_UP.
3195 struct in6_aliasreq
*ifra
)
3198 struct in6_ifaddr
*ia
;
3199 int dad_delay
; /* delay ticks before DAD output */
3206 * special cases, like 6to4, are handled in in6_ifattach
3208 error
= in6_ifattach(ifp
, NULL
, ifra
);
3214 ifnet_lock_exclusive(ifp
);
3215 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
3218 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3222 ia
= (struct in6_ifaddr
*)ifa
;
3223 if (ia
->ia6_flags
& IN6_IFF_TENTATIVE
) {
3225 nd6_dad_start(ifa
, &dad_delay
);
3230 ifnet_lock_done(ifp
);
3239 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0)
3243 * Skip DAD on service triggered interfaces, for now,
3244 * until we have support for Opportunistic Duplicate
3245 * Address Detection [RFC 4429] and we can then back
3248 if (ifp
->if_eflags
& IFEF_SERVICE_TRIGGERED
)
3251 switch (ifp
->if_type
) {
3257 * These interfaces do not have the IFF_LOOPBACK flag,
3258 * but loop packets back. We do not have to do DAD on such
3259 * interfaces. We should even omit it, because loop-backed
3260 * NS would confuse the DAD procedure.
3265 * Our DAD routine requires the interface up and running.
3266 * However, some interfaces can be up before the RUNNING
3267 * status. Additionaly, users may try to assign addresses
3268 * before the interface becomes up (or running).
3269 * We simply skip DAD in such a case as a work around.
3270 * XXX: we should rather mark "tentative" on such addresses,
3271 * and do DAD after the interface becomes ready.
3273 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) !=
3274 (IFF_UP
|IFF_RUNNING
))
3282 * Calculate max IPv6 MTU through all the interfaces and store it
3288 u_int32_t maxmtu
= 0;
3291 ifnet_head_lock_shared();
3292 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
3293 lck_rw_lock_shared(nd_if_rwlock
);
3294 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
3295 IN6_LINKMTU(ifp
) > maxmtu
)
3296 maxmtu
= IN6_LINKMTU(ifp
);
3297 lck_rw_done(nd_if_rwlock
);
3300 if (maxmtu
) /* update only when maxmtu is positive */
3301 in6_maxmtu
= maxmtu
;
3304 * Provide the length of interface identifiers to be used for the link attached
3305 * to the given interface. The length should be defined in "IPv6 over
3306 * xxx-link" document. Note that address architecture might also define
3307 * the length for a particular set of address prefixes, regardless of the
3308 * link type. As clarified in rfc2462bis, those two definitions should be
3309 * consistent, and those really are as of August 2004.
3312 in6_if2idlen(struct ifnet
*ifp
)
3314 switch (ifp
->if_type
) {
3315 case IFT_ETHER
: /* RFC2464 */
3316 case IFT_IEEE8023ADLAG
: /* IEEE802.3ad Link Aggregate */
3317 #ifdef IFT_PROPVIRTUAL
3318 case IFT_PROPVIRTUAL
: /* XXX: no RFC. treat it as ether */
3321 case IFT_L2VLAN
: /* ditto */
3323 #ifdef IFT_IEEE80211
3324 case IFT_IEEE80211
: /* ditto */
3327 case IFT_MIP
: /* ditto */
3330 case IFT_FDDI
: /* RFC2467 */
3332 case IFT_ISO88025
: /* RFC2470 (IPv6 over Token Ring) */
3334 case IFT_PPP
: /* RFC2472 */
3336 case IFT_ARCNET
: /* RFC2497 */
3338 case IFT_FRELAY
: /* RFC2590 */
3340 case IFT_IEEE1394
: /* RFC3146 */
3343 return (64); /* draft-ietf-v6ops-mech-v2-07 */
3345 return (64); /* XXX: is this really correct? */
3347 return (64); /* for utun interfaces */
3349 return (64); /* Packet Data over Cellular */
3352 * Unknown link type:
3353 * It might be controversial to use the today's common constant
3354 * of 64 for these cases unconditionally. For full compliance,
3355 * we should return an error in this case. On the other hand,
3356 * if we simply miss the standard for the link type or a new
3357 * standard is defined for a new link type, the IFID length
3358 * is very likely to be the common constant. As a compromise,
3359 * we always use the constant, but make an explicit notice
3360 * indicating the "unknown" case.
3362 printf("in6_if2idlen: unknown link type (%d)\n", ifp
->if_type
);
3367 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
3368 * v4 mapped addr or v4 compat addr
3371 in6_sin6_2_sin(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3373 bzero(sin
, sizeof(*sin
));
3374 sin
->sin_len
= sizeof(struct sockaddr_in
);
3375 sin
->sin_family
= AF_INET
;
3376 sin
->sin_port
= sin6
->sin6_port
;
3377 sin
->sin_addr
.s_addr
= sin6
->sin6_addr
.s6_addr32
[3];
3380 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
3382 in6_sin_2_v4mapsin6(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3384 bzero(sin6
, sizeof(*sin6
));
3385 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
3386 sin6
->sin6_family
= AF_INET6
;
3387 sin6
->sin6_port
= sin
->sin_port
;
3388 sin6
->sin6_addr
.s6_addr32
[0] = 0;
3389 sin6
->sin6_addr
.s6_addr32
[1] = 0;
3390 sin6
->sin6_addr
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
3391 sin6
->sin6_addr
.s6_addr32
[3] = sin
->sin_addr
.s_addr
;
3394 /* Convert sockaddr_in6 into sockaddr_in. */
3396 in6_sin6_2_sin_in_sock(struct sockaddr
*nam
)
3398 struct sockaddr_in
*sin_p
;
3399 struct sockaddr_in6 sin6
;
3402 * Save original sockaddr_in6 addr and convert it
3405 sin6
= *(struct sockaddr_in6
*)nam
;
3406 sin_p
= (struct sockaddr_in
*)nam
;
3407 in6_sin6_2_sin(sin_p
, &sin6
);
3410 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
3412 in6_sin_2_v4mapsin6_in_sock(struct sockaddr
**nam
)
3414 struct sockaddr_in
*sin_p
;
3415 struct sockaddr_in6
*sin6_p
;
3417 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof *sin6_p
, M_SONAME
,
3421 sin_p
= (struct sockaddr_in
*)*nam
;
3422 in6_sin_2_v4mapsin6(sin_p
, sin6_p
);
3423 FREE(*nam
, M_SONAME
);
3424 *nam
= (struct sockaddr
*)sin6_p
;
3430 * Posts in6_event_data message kernel events.
3432 * To get the same size of kev_in6_data between ILP32 and LP64 data models
3433 * we are using a special version of the in6_addrlifetime structure that
3434 * uses only 32 bits fields to be compatible with Leopard, and that
3435 * are large enough to span 68 years.
3438 in6_post_msg(struct ifnet
*ifp
, u_int32_t event_code
, struct in6_ifaddr
*ifa
)
3440 struct kev_msg ev_msg
;
3441 struct kev_in6_data in6_event_data
;
3443 bzero(&in6_event_data
, sizeof(struct kev_in6_data
));
3444 bzero(&ev_msg
, sizeof(struct kev_msg
));
3445 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3446 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3447 ev_msg
.kev_subclass
= KEV_INET6_SUBCLASS
;
3448 ev_msg
.event_code
= event_code
;
3450 IFA_LOCK(&ifa
->ia_ifa
);
3451 in6_event_data
.ia_addr
= ifa
->ia_addr
;
3452 in6_event_data
.ia_net
= ifa
->ia_net
;
3453 in6_event_data
.ia_dstaddr
= ifa
->ia_dstaddr
;
3454 in6_event_data
.ia_prefixmask
= ifa
->ia_prefixmask
;
3455 in6_event_data
.ia_plen
= ifa
->ia_plen
;
3456 in6_event_data
.ia6_flags
= (u_int32_t
)ifa
->ia6_flags
;
3458 in6_event_data
.ia_lifetime
.ia6t_expire
=
3459 ifa
->ia6_lifetime
.ia6t_expire
;
3460 in6_event_data
.ia_lifetime
.ia6t_preferred
=
3461 ifa
->ia6_lifetime
.ia6t_preferred
;
3462 in6_event_data
.ia_lifetime
.ia6t_vltime
=
3463 ifa
->ia6_lifetime
.ia6t_vltime
;
3464 in6_event_data
.ia_lifetime
.ia6t_pltime
=
3465 ifa
->ia6_lifetime
.ia6t_pltime
;
3466 IFA_UNLOCK(&ifa
->ia_ifa
);
3469 strncpy(&in6_event_data
.link_data
.if_name
[0],
3470 ifp
->if_name
, IFNAMSIZ
);
3471 in6_event_data
.link_data
.if_family
= ifp
->if_family
;
3472 in6_event_data
.link_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3475 ev_msg
.dv
[0].data_ptr
= &in6_event_data
;
3476 ev_msg
.dv
[0].data_length
= sizeof (in6_event_data
);
3477 ev_msg
.dv
[1].data_length
= 0;
3479 kev_post_msg(&ev_msg
);
3483 * Called as part of ip6_init
3486 in6_ifaddr_init(void)
3490 PE_parse_boot_argn("ifa_debug", &in6ifa_debug
, sizeof (in6ifa_debug
));
3492 in6ifa_size
= (in6ifa_debug
== 0) ? sizeof (struct in6_ifaddr
) :
3493 sizeof (struct in6_ifaddr_dbg
);
3495 in6ifa_zone
= zinit(in6ifa_size
, IN6IFA_ZONE_MAX
* in6ifa_size
,
3496 0, IN6IFA_ZONE_NAME
);
3497 if (in6ifa_zone
== NULL
) {
3498 panic("%s: failed allocating %s", __func__
, IN6IFA_ZONE_NAME
);
3501 zone_change(in6ifa_zone
, Z_EXPAND
, TRUE
);
3502 zone_change(in6ifa_zone
, Z_CALLERACCT
, FALSE
);
3504 lck_mtx_init(&in6ifa_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3505 TAILQ_INIT(&in6ifa_trash_head
);
3508 static struct in6_ifaddr
*
3509 in6_ifaddr_alloc(int how
)
3511 struct in6_ifaddr
*in6ifa
;
3513 in6ifa
= (how
== M_WAITOK
) ? zalloc(in6ifa_zone
) :
3514 zalloc_noblock(in6ifa_zone
);
3515 if (in6ifa
!= NULL
) {
3516 bzero(in6ifa
, in6ifa_size
);
3517 in6ifa
->ia_ifa
.ifa_free
= in6_ifaddr_free
;
3518 in6ifa
->ia_ifa
.ifa_debug
|= IFD_ALLOC
;
3519 ifa_lock_init(&in6ifa
->ia_ifa
);
3520 if (in6ifa_debug
!= 0) {
3521 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3522 (struct in6_ifaddr_dbg
*)in6ifa
;
3523 in6ifa
->ia_ifa
.ifa_debug
|= IFD_DEBUG
;
3524 in6ifa
->ia_ifa
.ifa_trace
= in6_ifaddr_trace
;
3525 in6ifa
->ia_ifa
.ifa_attached
= in6_ifaddr_attached
;
3526 in6ifa
->ia_ifa
.ifa_detached
= in6_ifaddr_detached
;
3527 ctrace_record(&in6ifa_dbg
->in6ifa_alloc
);
3534 in6_ifaddr_free(struct ifaddr
*ifa
)
3536 IFA_LOCK_ASSERT_HELD(ifa
);
3538 if (ifa
->ifa_refcnt
!= 0) {
3539 panic("%s: ifa %p bad ref cnt", __func__
, ifa
);
3541 } else if (!(ifa
->ifa_debug
& IFD_ALLOC
)) {
3542 panic("%s: ifa %p cannot be freed", __func__
, ifa
);
3545 if (ifa
->ifa_debug
& IFD_DEBUG
) {
3546 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3547 (struct in6_ifaddr_dbg
*)ifa
;
3548 ctrace_record(&in6ifa_dbg
->in6ifa_free
);
3549 bcopy(&in6ifa_dbg
->in6ifa
, &in6ifa_dbg
->in6ifa_old
,
3550 sizeof (struct in6_ifaddr
));
3551 if (ifa
->ifa_debug
& IFD_TRASHED
) {
3552 /* Become a regular mutex, just in case */
3553 IFA_CONVERT_LOCK(ifa
);
3554 lck_mtx_lock(&in6ifa_trash_lock
);
3555 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
,
3557 lck_mtx_unlock(&in6ifa_trash_lock
);
3558 ifa
->ifa_debug
&= ~IFD_TRASHED
;
3562 ifa_lock_destroy(ifa
);
3563 bzero(ifa
, sizeof (struct in6_ifaddr
));
3564 zfree(in6ifa_zone
, ifa
);
3568 in6_ifaddr_attached(struct ifaddr
*ifa
)
3570 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3572 IFA_LOCK_ASSERT_HELD(ifa
);
3574 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3575 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3578 if (ifa
->ifa_debug
& IFD_TRASHED
) {
3579 /* Become a regular mutex, just in case */
3580 IFA_CONVERT_LOCK(ifa
);
3581 lck_mtx_lock(&in6ifa_trash_lock
);
3582 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
3583 lck_mtx_unlock(&in6ifa_trash_lock
);
3584 ifa
->ifa_debug
&= ~IFD_TRASHED
;
3589 in6_ifaddr_detached(struct ifaddr
*ifa
)
3591 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3593 IFA_LOCK_ASSERT_HELD(ifa
);
3595 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3596 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3598 } else if (ifa
->ifa_debug
& IFD_TRASHED
) {
3599 panic("%s: ifa %p is already in trash list", __func__
, ifa
);
3602 ifa
->ifa_debug
|= IFD_TRASHED
;
3603 /* Become a regular mutex, just in case */
3604 IFA_CONVERT_LOCK(ifa
);
3605 lck_mtx_lock(&in6ifa_trash_lock
);
3606 TAILQ_INSERT_TAIL(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
3607 lck_mtx_unlock(&in6ifa_trash_lock
);
3611 in6_ifaddr_trace(struct ifaddr
*ifa
, int refhold
)
3613 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3618 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3619 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3623 cnt
= &in6ifa_dbg
->in6ifa_refhold_cnt
;
3624 tr
= in6ifa_dbg
->in6ifa_refhold
;
3626 cnt
= &in6ifa_dbg
->in6ifa_refrele_cnt
;
3627 tr
= in6ifa_dbg
->in6ifa_refrele
;
3630 idx
= atomic_add_16_ov(cnt
, 1) % IN6IFA_TRACE_HIST_SIZE
;
3631 ctrace_record(&tr
[idx
]);