2 * Copyright (c) 2003-2012 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
, struct if_laddrreq
*,
174 struct ifnet
*, struct proc
*);
175 static int in6_autoconf(struct ifnet
*, int);
176 static int in6_setrouter(struct ifnet
*, int);
177 static int in6_ifinit(struct ifnet
*, struct in6_ifaddr
*,
178 struct sockaddr_in6
*, int);
179 static void in6_unlink_ifa(struct in6_ifaddr
*, struct ifnet
*);
180 static struct in6_ifaddr
*in6_ifaddr_alloc(int);
181 static void in6_ifaddr_attached(struct ifaddr
*);
182 static void in6_ifaddr_detached(struct ifaddr
*);
183 static void in6_ifaddr_free(struct ifaddr
*);
184 static void in6_ifaddr_trace(struct ifaddr
*, int);
185 static struct in6_aliasreq
*in6_aliasreq_to_native(void *, int,
186 struct in6_aliasreq
*);
188 static void in6_ifaddr_set_dadprogress(struct in6_ifaddr
*);
190 extern lck_mtx_t
*nd6_mutex
;
191 extern int in6_init2done
;
193 #define IN6IFA_TRACE_HIST_SIZE 32 /* size of trace history */
196 __private_extern__
unsigned int in6ifa_trace_hist_size
= IN6IFA_TRACE_HIST_SIZE
;
198 struct in6_ifaddr_dbg
{
199 struct in6_ifaddr in6ifa
; /* in6_ifaddr */
200 struct in6_ifaddr in6ifa_old
; /* saved in6_ifaddr */
201 u_int16_t in6ifa_refhold_cnt
; /* # of IFA_ADDREF */
202 u_int16_t in6ifa_refrele_cnt
; /* # of IFA_REMREF */
204 * Alloc and free callers.
206 ctrace_t in6ifa_alloc
;
207 ctrace_t in6ifa_free
;
209 * Circular lists of IFA_ADDREF and IFA_REMREF callers.
211 ctrace_t in6ifa_refhold
[IN6IFA_TRACE_HIST_SIZE
];
212 ctrace_t in6ifa_refrele
[IN6IFA_TRACE_HIST_SIZE
];
216 TAILQ_ENTRY(in6_ifaddr_dbg
) in6ifa_trash_link
;
219 /* List of trash in6_ifaddr entries protected by in6ifa_trash_lock */
220 static TAILQ_HEAD(, in6_ifaddr_dbg
) in6ifa_trash_head
;
221 static decl_lck_mtx_data(, in6ifa_trash_lock
);
224 static unsigned int in6ifa_debug
= 1; /* debugging (enabled) */
226 static unsigned int in6ifa_debug
; /* debugging (disabled) */
228 static unsigned int in6ifa_size
; /* size of zone element */
229 static struct zone
*in6ifa_zone
; /* zone for in6_ifaddr */
231 #define IN6IFA_ZONE_MAX 64 /* maximum elements in zone */
232 #define IN6IFA_ZONE_NAME "in6_ifaddr" /* zone name */
235 * Subroutine for in6_ifaddloop() and in6_ifremloop().
236 * This routine does actual work.
239 in6_ifloop_request(int cmd
, struct ifaddr
*ifa
)
241 struct sockaddr_in6 all1_sa
;
242 struct rtentry
*nrt
= NULL
;
245 bzero(&all1_sa
, sizeof(all1_sa
));
246 all1_sa
.sin6_family
= AF_INET6
;
247 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
248 all1_sa
.sin6_addr
= in6mask128
;
251 * We specify the address itself as the gateway, and set the
252 * RTF_LLINFO flag, so that the corresponding host route would have
253 * the flag, and thus applications that assume traditional behavior
254 * would be happy. Note that we assume the caller of the function
255 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
256 * which changes the outgoing interface to the loopback interface.
257 * ifa_addr for INET6 is set once during init; no need to hold lock.
259 lck_mtx_lock(rnh_lock
);
260 e
= rtrequest_locked(cmd
, ifa
->ifa_addr
, ifa
->ifa_addr
,
261 (struct sockaddr
*)&all1_sa
,
262 RTF_UP
|RTF_HOST
|RTF_LLINFO
, &nrt
);
264 log(LOG_ERR
, "in6_ifloop_request: "
265 "%s operation failed for %s (errno=%d)\n",
266 cmd
== RTM_ADD
? "ADD" : "DELETE",
267 ip6_sprintf(&((struct in6_ifaddr
*)ifa
)->ia_addr
.sin6_addr
),
274 * Make sure rt_ifa be equal to IFA, the second argument of the
276 * We need this because when we refer to rt_ifa->ia6_flags in
277 * ip6_input, we assume that the rt_ifa points to the address instead
278 * of the loopback address.
280 if (cmd
== RTM_ADD
&& nrt
&& ifa
!= nrt
->rt_ifa
) {
285 * Report the addition/removal of the address to the routing socket.
286 * XXX: since we called rtinit for a p2p interface with a destination,
287 * we end up reporting twice in such a case. Should we rather
288 * omit the second report?
291 rt_newaddrmsg(cmd
, ifa
, e
, nrt
);
292 if (cmd
== RTM_DELETE
) {
296 /* the cmd must be RTM_ADD here */
297 RT_REMREF_LOCKED(nrt
);
301 lck_mtx_unlock(rnh_lock
);
305 * Add ownaddr as loopback rtentry. We previously add the route only if
306 * necessary (ex. on a p2p link). However, since we now manage addresses
307 * separately from prefixes, we should always add the route. We can't
308 * rely on the cloning mechanism from the corresponding interface route
312 in6_ifaddloop(struct ifaddr
*ifa
)
317 * If there is no loopback entry, allocate one. ifa_addr for
318 * INET6 is set once during init; no need to hold lock.
320 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
323 if (rt
== NULL
|| (rt
->rt_flags
& RTF_HOST
) == 0 ||
324 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) == 0) {
326 RT_REMREF_LOCKED(rt
);
329 in6_ifloop_request(RTM_ADD
, ifa
);
330 } else if (rt
!= NULL
) {
331 RT_REMREF_LOCKED(rt
);
337 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
341 in6_ifremloop(struct ifaddr
*ifa
)
343 struct in6_ifaddr
*ia
;
348 * Some of BSD variants do not remove cloned routes
349 * from an interface direct route, when removing the direct route
350 * (see comments in net/net_osdep.h). Even for variants that do remove
351 * cloned routes, they could fail to remove the cloned routes when
352 * we handle multple addresses that share a common prefix.
353 * So, we should remove the route corresponding to the deleted address
354 * regardless of the result of in6_is_ifloop_auto().
358 * Delete the entry only if exact one ifa exists. More than one ifa
359 * can exist if we assign a same single address to multiple
360 * (probably p2p) interfaces.
361 * XXX: we should avoid such a configuration in IPv6...
363 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
364 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
365 IFA_LOCK(&ia
->ia_ifa
);
366 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa
), &ia
->ia_addr
.sin6_addr
)) {
369 IFA_UNLOCK(&ia
->ia_ifa
);
373 IFA_UNLOCK(&ia
->ia_ifa
);
375 lck_rw_done(&in6_ifaddr_rwlock
);
379 * Before deleting, check if a corresponding loopbacked host
380 * route surely exists. With this check, we can avoid to
381 * delete an interface direct route whose destination is same
382 * as the address being removed. This can happen when removing
383 * a subnet-router anycast address on an interface attahced
384 * to a shared medium. ifa_addr for INET6 is set once during
385 * init; no need to hold lock.
387 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
390 if ((rt
->rt_flags
& RTF_HOST
) != 0 &&
391 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
392 RT_REMREF_LOCKED(rt
);
394 in6_ifloop_request(RTM_DELETE
, ifa
);
404 in6_mask2len(mask
, lim0
)
405 struct in6_addr
*mask
;
409 u_char
*lim
= lim0
, *p
;
411 /* ignore the scope_id part */
412 if (lim0
== NULL
|| lim0
- (u_char
*)mask
> sizeof(*mask
))
413 lim
= (u_char
*)mask
+ sizeof(*mask
);
414 for (p
= (u_char
*)mask
; p
< lim
; x
++, p
++) {
420 for (y
= 0; y
< 8; y
++) {
421 if ((*p
& (0x80 >> y
)) == 0)
427 * when the limit pointer is given, do a stricter check on the
431 if (y
!= 0 && (*p
& (0x00ff >> y
)) != 0)
433 for (p
= p
+ 1; p
< lim
; p
++)
442 in6_len2mask(mask
, len
)
443 struct in6_addr
*mask
;
448 bzero(mask
, sizeof(*mask
));
449 for (i
= 0; i
< len
/ 8; i
++)
450 mask
->s6_addr8
[i
] = 0xff;
452 mask
->s6_addr8
[i
] = (0xff00 >> (len
% 8)) & 0xff;
456 in6_aliasreq_64_to_32(struct in6_aliasreq_64
*src
, struct in6_aliasreq_32
*dst
)
458 bzero(dst
, sizeof (*dst
));
459 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof (dst
->ifra_name
));
460 dst
->ifra_addr
= src
->ifra_addr
;
461 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
462 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
463 dst
->ifra_flags
= src
->ifra_flags
;
464 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
465 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
466 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
467 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
471 in6_aliasreq_32_to_64(struct in6_aliasreq_32
*src
, struct in6_aliasreq_64
*dst
)
473 bzero(dst
, sizeof (*dst
));
474 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof (dst
->ifra_name
));
475 dst
->ifra_addr
= src
->ifra_addr
;
476 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
477 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
478 dst
->ifra_flags
= src
->ifra_flags
;
479 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
480 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
481 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
482 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
485 static struct in6_aliasreq
*
486 in6_aliasreq_to_native(void *data
, int data_is_64
, struct in6_aliasreq
*dst
)
488 #if defined(__LP64__)
490 bcopy(data
, dst
, sizeof (*dst
));
492 in6_aliasreq_32_to_64((struct in6_aliasreq_32
*)data
,
493 (struct in6_aliasreq_64
*)dst
);
496 in6_aliasreq_64_to_32((struct in6_aliasreq_64
*)data
,
497 (struct in6_aliasreq_32
*)dst
);
499 bcopy(data
, dst
, sizeof (*dst
));
500 #endif /* __LP64__ */
504 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(void *)(ifa))
507 in6_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
510 struct in6_aliasreq sifra
, *ifra
= NULL
;
511 struct in6_ifaddr
*ia
= NULL
;
512 struct sockaddr_in6 sin6
, *sa6
= NULL
;
513 int index
, privileged
, error
= 0;
514 u_int32_t ifru_scope_id
[16];
515 struct timeval timenow
;
516 int p64
= proc_is64bit(p
);
518 getmicrotime(&timenow
);
520 privileged
= (proc_suser(p
) == 0);
523 case SIOCGETSGCNT_IN6
: /* struct sioc_sg_req6 */
524 case SIOCGETMIFCNT_IN6_32
: /* struct sioc_mif_req6_32 */
525 case SIOCGETMIFCNT_IN6_64
: /* struct sioc_mif_req6_64 */
526 return (mrt6_ioctl(cmd
, data
));
530 case SIOCAADDRCTL_POLICY
: /* struct in6_addrpolicy */
531 case SIOCDADDRCTL_POLICY
: /* struct in6_addrpolicy */
534 return (in6_src_ioctl(cmd
, data
));
537 case SIOCDRADD_IN6_32
: /* struct in6_defrouter_32 */
538 case SIOCDRADD_IN6_64
: /* struct in6_defrouter_64 */
539 case SIOCDRDEL_IN6_32
: /* struct in6_defrouter_32 */
540 case SIOCDRDEL_IN6_64
: /* struct in6_defrouter_64 */
543 return (defrtrlist_ioctl(cmd
, data
));
551 case SIOCAUTOCONF_START
: /* struct in6_ifreq */
552 case SIOCAUTOCONF_STOP
: /* struct in6_ifreq */
553 case SIOCLL_START_32
: /* struct in6_aliasreq_32 */
554 case SIOCLL_START_64
: /* struct in6_aliasreq_64 */
555 case SIOCLL_STOP
: /* struct in6_ifreq */
556 case SIOCSETROUTERMODE_IN6
: /* struct in6_ifreq */
557 case SIOCPROTOATTACH_IN6_32
: /* struct in6_aliasreq_32 */
558 case SIOCPROTOATTACH_IN6_64
: /* struct in6_aliasreq_64 */
559 case SIOCPROTODETACH_IN6
: /* struct in6_ifreq */
564 case SIOCSNDFLUSH_IN6
: /* struct in6_ifreq */
565 case SIOCSPFXFLUSH_IN6
: /* struct in6_ifreq */
566 case SIOCSRTRFLUSH_IN6
: /* struct in6_ifreq */
567 case SIOCSDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
568 case SIOCSDEFIFACE_IN6_64
: /* struct in6_ndifreq_64 */
569 case SIOCSIFINFO_FLAGS
: /* struct in6_ndireq */
573 case OSIOCGIFINFO_IN6
: /* struct in6_ondireq */
574 case SIOCGIFINFO_IN6
: /* struct in6_ondireq */
575 case SIOCGDRLST_IN6_32
: /* struct in6_drlist_32 */
576 case SIOCGDRLST_IN6_64
: /* struct in6_drlist_64 */
577 case SIOCGPRLST_IN6_32
: /* struct in6_prlist_32 */
578 case SIOCGPRLST_IN6_64
: /* struct in6_prlist_64 */
579 case SIOCGNBRINFO_IN6_32
: /* struct in6_nbrinfo_32 */
580 case SIOCGNBRINFO_IN6_64
: /* struct in6_nbrinfo_64 */
581 case SIOCGDEFIFACE_IN6_32
: /* struct in6_ndifreq_32 */
582 case SIOCGDEFIFACE_IN6_64
: /* struct in6_ndifreq_64 */
583 return (nd6_ioctl(cmd
, data
, ifp
));
586 case SIOCSIFPREFIX_IN6
: /* struct in6_prefixreq */
587 case SIOCDIFPREFIX_IN6
: /* struct in6_prefixreq */
588 case SIOCAIFPREFIX_IN6
: /* struct in6_rrenumreq */
589 case SIOCCIFPREFIX_IN6
: /* struct in6_rrenumreq */
590 case SIOCSGIFPREFIX_IN6
: /* struct in6_rrenumreq */
591 case SIOCGIFPREFIX_IN6
: /* struct in6_prefixreq */
593 "prefix ioctls are now invalidated. "
594 "please use ifconfig.\n");
598 case SIOCSSCOPE6
: { /* struct in6_ifreq */
599 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
604 bcopy(ifr
->ifr_ifru
.ifru_scope_id
, ifru_scope_id
,
605 sizeof (ifru_scope_id
));
607 return (scope6_set(ifp
, ifru_scope_id
));
611 case SIOCGSCOPE6
: { /* struct in6_ifreq */
612 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
614 bcopy(ifr
->ifr_ifru
.ifru_scope_id
, ifru_scope_id
,
615 sizeof (ifru_scope_id
));
617 return (scope6_get(ifp
, ifru_scope_id
));
621 case SIOCGSCOPE6DEF
: { /* struct in6_ifreq */
622 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
624 bcopy(ifr
->ifr_ifru
.ifru_scope_id
, ifru_scope_id
,
625 sizeof (ifru_scope_id
));
627 return (scope6_get_default(ifru_scope_id
));
631 case SIOCALIFADDR
: /* struct if_laddrreq */
632 case SIOCDLIFADDR
: /* struct if_laddrreq */
636 case SIOCGLIFADDR
: { /* struct if_laddrreq */
637 struct if_laddrreq iflr
;
639 bcopy(data
, &iflr
, sizeof (iflr
));
640 error
= in6_lifaddr_ioctl(so
, cmd
, &iflr
, ifp
, p
);
641 bcopy(&iflr
, data
, sizeof (iflr
));
648 case SIOCLL_START_32
: /* struct in6_aliasreq_32 */
649 case SIOCAIFADDR_IN6_32
: { /* struct in6_aliasreq_32 */
651 * Convert user ifra to the kernel form, when appropriate.
652 * This allows the conversion between different data models
653 * to be centralized, so that it can be passed around to other
654 * routines that are expecting the kernel form.
656 ifra
= in6_aliasreq_to_native(data
, 0, &sifra
);
657 bcopy(&ifra
->ifra_addr
, &sin6
, sizeof (sin6
));
662 case SIOCLL_START_64
: /* struct in6_aliasreq_64 */
663 case SIOCAIFADDR_IN6_64
: { /* struct in6_aliasreq_64 */
665 * Convert user ifra to the kernel form, when appropriate.
666 * This allows the conversion between different data models
667 * to be centralized, so that it can be passed around to other
668 * routines that are expecting the kernel form.
670 ifra
= in6_aliasreq_to_native(data
, 1, &sifra
);
671 bcopy(&ifra
->ifra_addr
, &sin6
, sizeof (sin6
));
676 case SIOCSIFADDR_IN6
: /* struct in6_ifreq (deprecated) */
677 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
678 case SIOCSIFDSTADDR_IN6
: /* struct in6_ifreq (deprecated) */
679 case SIOCSIFNETMASK_IN6
: /* struct in6_ifreq (deprecated) */
680 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
681 case SIOCGIFNETMASK_IN6
: /* struct in6_ifreq */
682 case SIOCDIFADDR_IN6
: /* struct in6_ifreq */
683 case SIOCGIFPSRCADDR_IN6
: /* struct in6_ifreq */
684 case SIOCGIFPDSTADDR_IN6
: /* struct in6_ifreq */
685 case SIOCGIFAFLAG_IN6
: /* struct in6_ifreq */
686 case SIOCSNDFLUSH_IN6
: /* struct in6_ifreq */
687 case SIOCSPFXFLUSH_IN6
: /* struct in6_ifreq */
688 case SIOCSRTRFLUSH_IN6
: /* struct in6_ifreq */
689 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
690 case SIOCSIFALIFETIME_IN6
: /* struct in6_ifreq */
691 case SIOCGIFSTAT_IN6
: /* struct in6_ifreq */
692 case SIOCGIFSTAT_ICMP6
: { /* struct in6_ifreq */
693 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
695 bcopy(&ifr
->ifr_addr
, &sin6
, sizeof (sin6
));
705 case SIOCAUTOCONF_START
:
706 return (in6_autoconf(ifp
, TRUE
));
709 case SIOCAUTOCONF_STOP
:
710 return (in6_autoconf(ifp
, FALSE
));
713 case SIOCLL_START_32
:
714 case SIOCLL_START_64
:
715 VERIFY(ifra
!= NULL
);
717 * NOTE: All the interface specific DLIL attachements should
718 * be done here. They are currently done in in6_ifattach()
719 * for the interfaces that need it.
721 if ((ifp
->if_eflags
& IFEF_NOAUTOIPV6LL
) != 0 &&
722 ifra
->ifra_addr
.sin6_family
== AF_INET6
&&
723 ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
) {
724 /* some interfaces may provide LinkLocal addresses */
725 error
= in6_if_up(ifp
, ifra
);
727 error
= in6_if_up(ifp
, NULL
);
733 /* Remove link local addresses from interface */
734 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
737 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
741 IFA_LOCK(&ia
->ia_ifa
);
742 if (IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
)) {
743 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
744 IFA_UNLOCK(&ia
->ia_ifa
);
745 lck_rw_done(&in6_ifaddr_rwlock
);
746 in6_purgeaddr(&ia
->ia_ifa
);
747 IFA_REMREF(&ia
->ia_ifa
); /* for us */
748 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
750 * Purging the address caused in6_ifaddr_rwlock
751 * to be dropped and reacquired;
752 * therefore search again from the beginning
753 * of in6_ifaddrs list.
758 IFA_UNLOCK(&ia
->ia_ifa
);
761 lck_rw_done(&in6_ifaddr_rwlock
);
765 case SIOCSETROUTERMODE_IN6
: { /* struct in6_ifreq */
769 bcopy(&((struct in6_ifreq
*)(void *)data
)->ifr_intval
,
770 &intval
, sizeof (intval
));
772 return (in6_setrouter(ifp
, intval
));
776 case SIOCPROTOATTACH_IN6_32
: /* struct in6_aliasreq_32 */
777 case SIOCPROTOATTACH_IN6_64
: /* struct in6_aliasreq_64 */
778 return (in6_domifattach(ifp
));
781 case SIOCPROTODETACH_IN6
: /* struct in6_ifreq */
782 /* Cleanup interface routes and addresses */
785 if ((error
= proto_unplumb(PF_INET6
, ifp
)))
786 printf("SIOCPROTODETACH_IN6: %s error=%d\n",
787 if_name(ifp
), error
);
793 * Find address for this interface, if it exists.
795 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
796 * only, and used the first interface address as the target of other
797 * operations (without checking ifra_addr). This was because netinet
798 * code/API assumed at most 1 interface address per interface.
799 * Since IPv6 allows a node to assign multiple addresses
800 * on a single interface, we almost always look and check the
801 * presence of ifra_addr, and reject invalid ones here.
802 * It also decreases duplicated code among SIOC*_IN6 operations.
804 if (sa6
!= NULL
&& sa6
->sin6_family
== AF_INET6
) {
805 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
806 if (sa6
->sin6_addr
.s6_addr16
[1] == 0) {
807 /* link ID is not embedded by the user */
808 sa6
->sin6_addr
.s6_addr16
[1] =
809 htons(ifp
->if_index
);
810 } else if (sa6
->sin6_addr
.s6_addr16
[1] !=
811 htons(ifp
->if_index
)) {
812 return (EINVAL
); /* link ID contradicts */
814 if (sa6
->sin6_scope_id
) {
815 if (sa6
->sin6_scope_id
!=
816 (u_int32_t
)ifp
->if_index
)
818 sa6
->sin6_scope_id
= 0; /* XXX: good way? */
821 ia
= in6ifa_ifpwithaddr(ifp
, &sa6
->sin6_addr
);
827 case SIOCSIFADDR_IN6
: /* struct in6_ifreq */
828 case SIOCSIFDSTADDR_IN6
: /* struct in6_ifreq */
829 case SIOCSIFNETMASK_IN6
: /* struct in6_ifreq */
831 * Since IPv6 allows a node to assign multiple addresses
832 * on a single interface, SIOCSIFxxx ioctls are deprecated.
834 /* we decided to obsolete this command (20000704) */
838 case SIOCDIFADDR_IN6
: /* struct in6_ifreq */
840 * for IPv4, we look for existing in_ifaddr here to allow
841 * "ifconfig if0 delete" to remove the first IPv4 address on
842 * the interface. For IPv6, as the spec allows multiple
843 * interface address from the day one, we consider "remove the
844 * first one" semantics to be not preferable.
847 error
= EADDRNOTAVAIL
;
851 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
852 case SIOCAIFADDR_IN6_64
: /* struct in6_aliasreq_64 */
855 * We always require users to specify a valid IPv6 address for
856 * the corresponding operation. Use "sa6" instead of "ifra"
857 * since SIOCDIFADDR_IN6 falls thru above.
859 if (sa6
->sin6_family
!= AF_INET6
||
860 sa6
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
861 error
= EAFNOSUPPORT
;
870 case SIOCGIFADDR_IN6
: /* struct in6_ifreq */
871 /* This interface is basically deprecated. use SIOCGIFCONF. */
873 case SIOCGIFAFLAG_IN6
: /* struct in6_ifreq */
874 case SIOCGIFNETMASK_IN6
: /* struct in6_ifreq */
875 case SIOCGIFDSTADDR_IN6
: /* struct in6_ifreq */
876 case SIOCGIFALIFETIME_IN6
: /* struct in6_ifreq */
877 /* must think again about its semantics */
879 error
= EADDRNOTAVAIL
;
884 case SIOCSIFALIFETIME_IN6
: { /* struct in6_ifreq */
885 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
892 error
= EADDRNOTAVAIL
;
895 /* sanity for overflow - beware unsigned */
897 struct in6_addrlifetime_64 lt
;
899 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
900 if (((ia
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0
901 || lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
)
902 && lt
.ia6t_vltime
+ timenow
.tv_sec
<
907 if (((ia
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0
908 || lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
)
909 && lt
.ia6t_pltime
+ timenow
.tv_sec
<
915 struct in6_addrlifetime_32 lt
;
917 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
918 if (((ia
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0
919 || lt
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
)
920 && lt
.ia6t_vltime
+ timenow
.tv_sec
<
925 if (((ia
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0
926 || lt
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
)
927 && lt
.ia6t_pltime
+ timenow
.tv_sec
<
938 case SIOCGIFADDR_IN6
: { /* struct in6_ifreq */
939 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
940 struct sockaddr_in6 addr
;
942 IFA_LOCK(&ia
->ia_ifa
);
943 bcopy(&ia
->ia_addr
, &addr
, sizeof (addr
));
944 IFA_UNLOCK(&ia
->ia_ifa
);
945 if ((error
= sa6_recoverscope(&addr
, TRUE
)) != 0) {
946 IFA_REMREF(&ia
->ia_ifa
);
949 bcopy(&addr
, &ifr
->ifr_addr
, sizeof (addr
));
953 case SIOCGIFDSTADDR_IN6
: { /* struct in6_ifreq */
954 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
955 struct sockaddr_in6 dstaddr
;
957 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0) {
962 * XXX: should we check if ifa_dstaddr is NULL and return
965 IFA_LOCK(&ia
->ia_ifa
);
966 bcopy(&ia
->ia_dstaddr
, &dstaddr
, sizeof (dstaddr
));
967 IFA_UNLOCK(&ia
->ia_ifa
);
968 if ((error
= sa6_recoverscope(&dstaddr
, TRUE
)) != 0) {
969 IFA_REMREF(&ia
->ia_ifa
);
972 bcopy(&dstaddr
, &ifr
->ifr_dstaddr
, sizeof (dstaddr
));
976 case SIOCGIFNETMASK_IN6
: { /* struct in6_ifreq */
977 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
979 IFA_LOCK(&ia
->ia_ifa
);
980 bcopy(&ia
->ia_prefixmask
, &ifr
->ifr_addr
,
981 sizeof (struct sockaddr_in6
));
982 IFA_UNLOCK(&ia
->ia_ifa
);
986 case SIOCGIFAFLAG_IN6
: { /* struct in6_ifreq */
987 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
989 IFA_LOCK(&ia
->ia_ifa
);
990 bcopy(&ia
->ia6_flags
, &ifr
->ifr_ifru
.ifru_flags6
,
991 sizeof (ifr
->ifr_ifru
.ifru_flags6
));
992 IFA_UNLOCK(&ia
->ia_ifa
);
996 case SIOCGIFSTAT_IN6
: { /* struct in6_ifreq */
997 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
1003 index
= ifp
->if_index
;
1004 lck_rw_lock_shared(&in6_ifs_rwlock
);
1005 if (in6_ifstat
== NULL
|| index
>= in6_ifstatmax
||
1006 in6_ifstat
[index
] == NULL
) {
1007 /* return EAFNOSUPPORT? */
1008 bzero(&ifr
->ifr_ifru
.ifru_stat
,
1009 sizeof (ifr
->ifr_ifru
.ifru_stat
));
1011 bcopy(in6_ifstat
[index
], &ifr
->ifr_ifru
.ifru_stat
,
1012 sizeof (ifr
->ifr_ifru
.ifru_stat
));
1014 lck_rw_done(&in6_ifs_rwlock
);
1018 case SIOCGIFSTAT_ICMP6
: { /* struct in6_ifreq */
1019 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
1025 index
= ifp
->if_index
;
1026 lck_rw_lock_shared(&icmp6_ifs_rwlock
);
1027 if (icmp6_ifstat
== NULL
|| index
>= icmp6_ifstatmax
||
1028 icmp6_ifstat
[index
] == NULL
) {
1029 /* return EAFNOSUPPORT? */
1030 bzero(&ifr
->ifr_ifru
.ifru_stat
,
1031 sizeof (ifr
->ifr_ifru
.ifru_icmp6stat
));
1033 bcopy(icmp6_ifstat
[index
],
1034 &ifr
->ifr_ifru
.ifru_icmp6stat
,
1035 sizeof (ifr
->ifr_ifru
.ifru_icmp6stat
));
1037 lck_rw_done(&icmp6_ifs_rwlock
);
1041 case SIOCGIFALIFETIME_IN6
: { /* struct in6_ifreq */
1042 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
1044 IFA_LOCK(&ia
->ia_ifa
);
1046 struct in6_addrlifetime_64 lt
;
1048 bzero(<
, sizeof (lt
));
1049 lt
.ia6t_expire
= ia
->ia6_lifetime
.ia6t_expire
;
1050 lt
.ia6t_preferred
= ia
->ia6_lifetime
.ia6t_preferred
;
1051 lt
.ia6t_vltime
= ia
->ia6_lifetime
.ia6t_vltime
;
1052 lt
.ia6t_pltime
= ia
->ia6_lifetime
.ia6t_pltime
;
1053 bcopy(<
, &ifr
->ifr_ifru
.ifru_lifetime
, sizeof (lt
));
1055 struct in6_addrlifetime_32 lt
;
1057 bzero(<
, sizeof (lt
));
1058 lt
.ia6t_expire
= (uint32_t)ia
->ia6_lifetime
.ia6t_expire
;
1060 (uint32_t)ia
->ia6_lifetime
.ia6t_preferred
;
1061 lt
.ia6t_vltime
= (uint32_t)ia
->ia6_lifetime
.ia6t_vltime
;
1062 lt
.ia6t_pltime
= (uint32_t)ia
->ia6_lifetime
.ia6t_pltime
;
1063 bcopy(<
, &ifr
->ifr_ifru
.ifru_lifetime
, sizeof (lt
));
1065 IFA_UNLOCK(&ia
->ia_ifa
);
1069 case SIOCSIFALIFETIME_IN6
: { /* struct in6_ifreq */
1070 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)(void *)data
;
1072 IFA_LOCK(&ia
->ia_ifa
);
1074 struct in6_addrlifetime_64 lt
;
1076 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1077 ia
->ia6_lifetime
.ia6t_expire
= lt
.ia6t_expire
;
1078 ia
->ia6_lifetime
.ia6t_preferred
= lt
.ia6t_preferred
;
1079 ia
->ia6_lifetime
.ia6t_vltime
= lt
.ia6t_vltime
;
1080 ia
->ia6_lifetime
.ia6t_pltime
= lt
.ia6t_pltime
;
1082 struct in6_addrlifetime_32 lt
;
1084 bcopy(&ifr
->ifr_ifru
.ifru_lifetime
, <
, sizeof (lt
));
1085 ia
->ia6_lifetime
.ia6t_expire
=
1086 (uint32_t)lt
.ia6t_expire
;
1087 ia
->ia6_lifetime
.ia6t_preferred
=
1088 (uint32_t)lt
.ia6t_preferred
;
1089 ia
->ia6_lifetime
.ia6t_vltime
= lt
.ia6t_vltime
;
1090 ia
->ia6_lifetime
.ia6t_pltime
= lt
.ia6t_pltime
;
1093 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
||
1094 (ia
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
1095 ia
->ia6_lifetime
.ia6t_expire
=
1096 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_vltime
;
1098 ia
->ia6_lifetime
.ia6t_expire
= 0;
1099 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
||
1100 (ia
->ia6_flags
& IN6_IFF_TEMPORARY
) != 0) {
1101 ia
->ia6_lifetime
.ia6t_preferred
=
1102 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_pltime
;
1104 ia
->ia6_lifetime
.ia6t_preferred
= 0;
1105 IFA_UNLOCK(&ia
->ia_ifa
);
1109 case SIOCAIFADDR_IN6_32
: /* struct in6_aliasreq_32 */
1110 case SIOCAIFADDR_IN6_64
: { /* struct in6_aliasreq_64 */
1112 struct nd_prefix pr0
, *pr
;
1114 VERIFY(ifra
!= NULL
);
1116 /* Attempt to attach the protocol, in case it isn't attached */
1117 error
= in6_domifattach(ifp
);
1119 if (error
== EEXIST
)
1124 /* PF_INET6 wasn't previously attached */
1125 if ((error
= in6_if_up(ifp
, NULL
)) != 0)
1130 * first, make or update the interface address structure,
1131 * and link it to the list.
1133 if ((error
= in6_update_ifa(ifp
, ifra
, ia
, 0, M_WAITOK
)) != 0)
1137 * then, make the prefix on-link on the interface.
1138 * XXX: we'd rather create the prefix before the address, but
1139 * we need at least one address to install the corresponding
1140 * interface route, so we configure the address first.
1144 * convert mask to prefix length (prefixmask has already
1145 * been validated in in6_update_ifa().
1147 bzero(&pr0
, sizeof(pr0
));
1148 lck_mtx_init(&pr0
.ndpr_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
1150 pr0
.ndpr_plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
1152 if (pr0
.ndpr_plen
== 128)
1153 break; /* we don't need to install a host route. */
1154 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
1155 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
1156 /* apply the mask for safety. */
1157 for (i
= 0; i
< 4; i
++) {
1158 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1159 ifra
->ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
1162 * XXX: since we don't have an API to set prefix (not address)
1163 * lifetimes, we just use the same lifetimes as addresses.
1164 * The (temporarily) installed lifetimes can be overridden by
1165 * later advertised RAs (when accept_rtadv is non 0), which is
1166 * an intended behavior.
1168 pr0
.ndpr_raf_onlink
= 1; /* should be configurable? */
1170 ((ifra
->ifra_flags
& IN6_IFF_AUTOCONF
) != 0);
1171 pr0
.ndpr_vltime
= ifra
->ifra_lifetime
.ia6t_vltime
;
1172 pr0
.ndpr_pltime
= ifra
->ifra_lifetime
.ia6t_pltime
;
1173 pr0
.ndpr_stateflags
|= NDPRF_STATIC
;
1175 /* add the prefix if there's one. */
1176 if ((pr
= nd6_prefix_lookup(&pr0
)) == NULL
) {
1178 * nd6_prelist_add will install the corresponding
1181 if ((error
= nd6_prelist_add(&pr0
, NULL
, &pr
,
1185 log(LOG_ERR
, "nd6_prelist_add succedded but "
1192 IFA_REMREF(&ia
->ia_ifa
);
1193 if ((ia
= in6ifa_ifpwithaddr(ifp
,
1194 &ifra
->ifra_addr
.sin6_addr
)) == NULL
) {
1195 /* XXX: this should not happen! */
1196 log(LOG_ERR
, "in6_control: addition succeeded, but"
1199 IFA_LOCK(&ia
->ia_ifa
);
1200 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
1201 ia
->ia6_ndpr
== NULL
) { /* new autoconfed addr */
1204 VERIFY(pr
->ndpr_addrcnt
!= 0);
1206 NDPR_ADDREF_LOCKED(pr
); /* for addr reference */
1209 * If this is the first autoconf address from
1210 * the prefix, create a temporary address
1211 * as well (when specified).
1213 if (ip6_use_tempaddr
&&
1214 pr
->ndpr_addrcnt
== 1) {
1218 IFA_UNLOCK(&ia
->ia_ifa
);
1220 if ((e
= in6_tmpifadd(ia
, 1,
1222 log(LOG_NOTICE
, "in6_control: "
1223 "failed to create a "
1224 "temporary address, "
1230 IFA_UNLOCK(&ia
->ia_ifa
);
1233 IFA_UNLOCK(&ia
->ia_ifa
);
1236 * this might affect the status of autoconfigured
1237 * addresses, that is, this address might make
1238 * other addresses detached.
1240 lck_mtx_lock(nd6_mutex
);
1241 pfxlist_onlink_check();
1242 lck_mtx_unlock(nd6_mutex
);
1245 /* Drop use count held above during lookup/add */
1248 pf_ifaddr_hook(ifp
, cmd
);
1253 case SIOCDIFADDR_IN6
: { /* struct in6_ifreq */
1255 struct nd_prefix pr0
, *pr
;
1258 * If the address being deleted is the only one that owns
1259 * the corresponding prefix, expire the prefix as well.
1260 * XXX: theoretically, we don't have to worry about such
1261 * relationship, since we separate the address management
1262 * and the prefix management. We do this, however, to provide
1263 * as much backward compatibility as possible in terms of
1264 * the ioctl operation.
1265 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
1267 IFA_LOCK(&ia
->ia_ifa
);
1268 bzero(&pr0
, sizeof(pr0
));
1270 pr0
.ndpr_plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
1272 if (pr0
.ndpr_plen
== 128) {
1273 IFA_UNLOCK(&ia
->ia_ifa
);
1276 pr0
.ndpr_prefix
= ia
->ia_addr
;
1277 pr0
.ndpr_mask
= ia
->ia_prefixmask
.sin6_addr
;
1278 for (i
= 0; i
< 4; i
++) {
1279 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1280 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
];
1282 IFA_UNLOCK(&ia
->ia_ifa
);
1284 * The logic of the following condition is a bit complicated.
1285 * We expire the prefix when
1286 * 1. the address obeys autoconfiguration and it is the
1287 * only owner of the associated prefix, or
1288 * 2. the address does not obey autoconf and there is no
1289 * other owner of the prefix.
1291 if ((pr
= nd6_prefix_lookup(&pr0
)) != NULL
) {
1292 IFA_LOCK(&ia
->ia_ifa
);
1294 if (((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
1295 pr
->ndpr_addrcnt
== 1) ||
1296 ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 &&
1297 pr
->ndpr_addrcnt
== 0)) {
1298 /* XXX: just for expiration */
1299 pr
->ndpr_expire
= 1;
1302 IFA_UNLOCK(&ia
->ia_ifa
);
1304 /* Drop use count held above during lookup */
1309 in6_purgeaddr(&ia
->ia_ifa
);
1311 pf_ifaddr_hook(ifp
, cmd
);
1317 error
= ifnet_ioctl(ifp
, PF_INET6
, cmd
, data
);
1323 IFA_REMREF(&ia
->ia_ifa
);
1328 in6_autoconf(struct ifnet
*ifp
, int enable
)
1332 if (ifp
->if_flags
& IFF_LOOPBACK
)
1337 * An interface in IPv6 router mode implies that it
1338 * is either configured with a static IP address or
1339 * autoconfigured via a locally-generated RA. Prevent
1340 * SIOCAUTOCONF_START from being set in that mode.
1342 ifnet_lock_exclusive(ifp
);
1343 if (ifp
->if_eflags
& IFEF_IPV6_ROUTER
) {
1344 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADV
;
1347 ifp
->if_eflags
|= IFEF_ACCEPT_RTADV
;
1349 ifnet_lock_done(ifp
);
1351 struct in6_ifaddr
*ia
= NULL
;
1353 ifnet_lock_exclusive(ifp
);
1354 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADV
;
1355 ifnet_lock_done(ifp
);
1357 /* Remove autoconfigured address from interface */
1358 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1360 while (ia
!= NULL
) {
1361 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
1365 IFA_LOCK(&ia
->ia_ifa
);
1366 if (ia
->ia6_flags
& IN6_IFF_AUTOCONF
) {
1367 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for us */
1368 IFA_UNLOCK(&ia
->ia_ifa
);
1369 lck_rw_done(&in6_ifaddr_rwlock
);
1370 in6_purgeaddr(&ia
->ia_ifa
);
1371 IFA_REMREF(&ia
->ia_ifa
); /* for us */
1372 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1374 * Purging the address caused in6_ifaddr_rwlock
1375 * to be dropped and reacquired;
1376 * therefore search again from the beginning
1377 * of in6_ifaddrs list.
1382 IFA_UNLOCK(&ia
->ia_ifa
);
1385 lck_rw_done(&in6_ifaddr_rwlock
);
1391 * Handle SIOCSETROUTERMODE_IN6 to set or clear the IPv6 router mode flag on
1392 * the interface. Entering or exiting this mode will result in the removal of
1393 * autoconfigured IPv6 addresses on the interface.
1396 in6_setrouter(struct ifnet
*ifp
, int enable
)
1398 if (ifp
->if_flags
& IFF_LOOPBACK
)
1402 struct nd_ifinfo
*ndi
;
1404 lck_rw_lock_shared(nd_if_rwlock
);
1405 ndi
= ND_IFINFO(ifp
);
1406 if (ndi
!= NULL
&& ndi
->initialized
) {
1407 lck_mtx_lock(&ndi
->lock
);
1408 if (ndi
->flags
& ND6_IFF_PROXY_PREFIXES
) {
1409 /* No proxy if we are an advertising router */
1410 ndi
->flags
&= ~ND6_IFF_PROXY_PREFIXES
;
1411 lck_mtx_unlock(&ndi
->lock
);
1412 lck_rw_done(nd_if_rwlock
);
1413 (void) nd6_if_prproxy(ifp
, FALSE
);
1415 lck_mtx_unlock(&ndi
->lock
);
1416 lck_rw_done(nd_if_rwlock
);
1419 lck_rw_done(nd_if_rwlock
);
1423 ifnet_lock_exclusive(ifp
);
1425 ifp
->if_eflags
|= IFEF_IPV6_ROUTER
;
1427 ifp
->if_eflags
&= ~IFEF_IPV6_ROUTER
;
1429 ifnet_lock_done(ifp
);
1431 lck_mtx_lock(nd6_mutex
);
1432 defrouter_select(ifp
);
1433 lck_mtx_unlock(nd6_mutex
);
1435 if_allmulti(ifp
, enable
);
1437 return (in6_autoconf(ifp
, FALSE
));
1441 * Update parameters of an IPv6 interface address.
1442 * If necessary, a new entry is created and linked into address chains.
1443 * This function is separated from in6_control().
1446 in6_update_ifa(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
,
1447 struct in6_ifaddr
*ia
, int flags
, int how
)
1449 int error
= 0, hostIsNew
= 0, plen
= -1;
1450 struct in6_ifaddr
*oia
;
1451 struct sockaddr_in6 dst6
;
1452 struct in6_addrlifetime
*lt
;
1453 struct in6_multi
*in6m_sol
= NULL
;
1454 struct in6_multi_mship
*imm
;
1455 struct timeval timenow
;
1457 struct ifaddr
*ifa
= NULL
;
1461 /* Validate parameters */
1462 if (ifp
== NULL
|| ifra
== NULL
) /* this maybe redundant */
1466 * The destination address for a p2p link must have a family
1467 * of AF_UNSPEC or AF_INET6.
1469 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
1470 ifra
->ifra_dstaddr
.sin6_family
!= AF_INET6
&&
1471 ifra
->ifra_dstaddr
.sin6_family
!= AF_UNSPEC
)
1472 return(EAFNOSUPPORT
);
1474 * validate ifra_prefixmask. don't check sin6_family, netmask
1475 * does not carry fields other than sin6_len.
1477 if (ifra
->ifra_prefixmask
.sin6_len
> sizeof(struct sockaddr_in6
))
1480 * Set the address family value for the mask if it was not set.
1483 if (ifra
->ifra_prefixmask
.sin6_len
== sizeof(struct sockaddr_in6
) &&
1484 ifra
->ifra_prefixmask
.sin6_family
== 0) {
1485 ifra
->ifra_prefixmask
.sin6_family
= AF_INET6
;
1488 * Because the IPv6 address architecture is classless, we require
1489 * users to specify a (non 0) prefix length (mask) for a new address.
1490 * We also require the prefix (when specified) mask is valid, and thus
1491 * reject a non-consecutive mask.
1493 if (ia
== NULL
&& ifra
->ifra_prefixmask
.sin6_len
== 0)
1495 if (ifra
->ifra_prefixmask
.sin6_len
!= 0) {
1496 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
1497 (u_char
*)&ifra
->ifra_prefixmask
+
1498 ifra
->ifra_prefixmask
.sin6_len
);
1503 * In this case, ia must not be NULL. We just use its prefix
1506 IFA_LOCK(&ia
->ia_ifa
);
1507 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
);
1508 IFA_UNLOCK(&ia
->ia_ifa
);
1511 * If the destination address on a p2p interface is specified,
1512 * and the address is a scoped one, validate/set the scope
1515 dst6
= ifra
->ifra_dstaddr
;
1516 if (((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) != 0 ) &&
1517 (dst6
.sin6_family
== AF_INET6
)) {
1520 if ((error
= in6_recoverscope(&dst6
,
1521 &ifra
->ifra_dstaddr
.sin6_addr
,
1525 scopeid
= in6_addr2scopeid(ifp
, &dst6
.sin6_addr
);
1526 if (dst6
.sin6_scope_id
== 0) /* user omit to specify the ID. */
1527 dst6
.sin6_scope_id
= scopeid
;
1528 else if (dst6
.sin6_scope_id
!= scopeid
)
1529 return(EINVAL
); /* scope ID mismatch. */
1531 if ((error
= in6_embedscope(&dst6
.sin6_addr
, &dst6
, NULL
, NULL
,
1534 dst6
.sin6_scope_id
= 0; /* XXX */
1537 * The destination address can be specified only for a p2p or a
1538 * loopback interface. If specified, the corresponding prefix length
1541 if (ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
) {
1542 if ((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) == 0) {
1543 /* XXX: noisy message */
1544 log(LOG_INFO
, "in6_update_ifa: a destination can be "
1545 "specified for a p2p or a loopback IF only\n");
1550 * The following message seems noisy, but we dare to
1551 * add it for diagnosis.
1553 log(LOG_INFO
, "in6_update_ifa: prefixlen must be 128 "
1554 "when dstaddr is specified\n");
1558 /* lifetime consistency check */
1560 getmicrotime(&timenow
);
1561 lt
= &ifra
->ifra_lifetime
;
1562 if ((lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
1563 || (ifra
->ifra_flags
& IN6_IFF_TEMPORARY
) != 0)
1564 && lt
->ia6t_vltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
1567 if (lt
->ia6t_vltime
== 0) {
1569 * the following log might be noisy, but this is a typical
1570 * configuration mistake or a tool's bug.
1573 "in6_update_ifa: valid lifetime is 0 for %s\n",
1574 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
1576 if ((lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
1577 || (ifra
->ifra_flags
& IN6_IFF_TEMPORARY
) != 0)
1578 && lt
->ia6t_pltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
1583 * If this is a new address, allocate a new ifaddr and link it
1589 * in6_update_ifa() may be called in a process of a received
1590 * RA; in such a case, we should call malloc with M_NOWAIT.
1591 * The exception to this is during init time or as part of
1592 * handling an ioctl, when we know it's okay to do M_WAITOK.
1594 ia
= in6_ifaddr_alloc(how
);
1597 ifnet_lock_exclusive(ifp
);
1598 IFA_LOCK(&ia
->ia_ifa
);
1599 LIST_INIT(&ia
->ia6_memberships
);
1600 /* Initialize the address and masks, and put time stamp */
1601 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
1602 ia
->ia_addr
.sin6_family
= AF_INET6
;
1603 ia
->ia_addr
.sin6_len
= sizeof(ia
->ia_addr
);
1604 ia
->ia6_createtime
= timenow
.tv_sec
;
1605 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) != 0) {
1607 * XXX: some functions expect that ifa_dstaddr is not
1608 * NULL for p2p interfaces.
1610 ia
->ia_ifa
.ifa_dstaddr
1611 = (struct sockaddr
*)&ia
->ia_dstaddr
;
1613 ia
->ia_ifa
.ifa_dstaddr
= NULL
;
1615 ia
->ia_ifa
.ifa_netmask
1616 = (struct sockaddr
*)&ia
->ia_prefixmask
;
1619 /* if_attach_ifa() holds a reference for ifa_link */
1620 if_attach_ifa(ifp
, &ia
->ia_ifa
);
1621 /* hold a reference for this routine */
1622 IFA_ADDREF_LOCKED(&ia
->ia_ifa
);
1623 IFA_UNLOCK(&ia
->ia_ifa
);
1624 ifnet_lock_done(ifp
);
1625 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
1626 /* Hold a reference for in6_ifaddrs link */
1627 IFA_ADDREF(&ia
->ia_ifa
);
1628 if ((oia
= in6_ifaddrs
) != NULL
) {
1629 for ( ; oia
->ia_next
; oia
= oia
->ia_next
)
1635 lck_rw_done(&in6_ifaddr_rwlock
);
1637 /* hold a reference for this routine */
1638 IFA_ADDREF(&ia
->ia_ifa
);
1644 /* update timestamp */
1645 ia
->ia6_updatetime
= timenow
.tv_sec
;
1647 /* set prefix mask */
1648 if (ifra
->ifra_prefixmask
.sin6_len
) {
1650 * We prohibit changing the prefix length of an existing
1652 * + such an operation should be rare in IPv6, and
1653 * + the operation would confuse prefix management.
1655 if (ia
->ia_prefixmask
.sin6_len
&&
1656 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
) != plen
) {
1657 log(LOG_INFO
, "in6_update_ifa: the prefix length of an"
1658 " existing (%s) address should not be changed\n",
1659 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1664 ia
->ia_prefixmask
= ifra
->ifra_prefixmask
;
1668 * If a new destination address is specified, scrub the old one and
1669 * install the new destination. Note that the interface must be
1670 * p2p or loopback (see the check above.)
1672 if (dst6
.sin6_family
== AF_INET6
&&
1673 !IN6_ARE_ADDR_EQUAL(&dst6
.sin6_addr
, &ia
->ia_dstaddr
.sin6_addr
)) {
1674 if ((ia
->ia_flags
& IFA_ROUTE
)) {
1678 if ((e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
,
1680 log(LOG_ERR
, "in6_update_ifa: failed to remove "
1681 "a route to the old destination: %s\n",
1682 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1683 /* proceed anyway... */
1687 ia
->ia_flags
&= ~IFA_ROUTE
;
1689 IFA_LOCK_ASSERT_HELD(ifa
);
1690 ia
->ia_dstaddr
= dst6
;
1694 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1695 * to see if the address is deprecated or invalidated, but initialize
1696 * these members for applications.
1698 ia
->ia6_lifetime
= ifra
->ifra_lifetime
;
1699 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
1700 || (ifra
->ifra_flags
& IN6_IFF_TEMPORARY
) != 0) {
1701 ia
->ia6_lifetime
.ia6t_expire
=
1702 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_vltime
;
1704 ia
->ia6_lifetime
.ia6t_expire
= 0;
1705 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
1706 || (ifra
->ifra_flags
& IN6_IFF_TEMPORARY
) != 0) {
1707 ia
->ia6_lifetime
.ia6t_preferred
=
1708 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_pltime
;
1710 ia
->ia6_lifetime
.ia6t_preferred
= 0;
1713 /* reset the interface and routing table appropriately. */
1714 if ((error
= in6_ifinit(ifp
, ia
, &ifra
->ifra_addr
, hostIsNew
)) != 0)
1719 * configure address flags.
1721 ia
->ia6_flags
= ifra
->ifra_flags
;
1723 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1724 * userland, make it deprecated.
1726 if ((ifra
->ifra_flags
& IN6_IFF_DEPRECATED
) != 0) {
1727 ia
->ia6_lifetime
.ia6t_pltime
= 0;
1728 ia
->ia6_lifetime
.ia6t_preferred
= timenow
.tv_sec
;
1731 * Mark the address as tentative before joining multicast addresses,
1732 * so that corresponding MLD responses would not have a tentative
1735 ia
->ia6_flags
&= ~IN6_IFF_DUPLICATED
; /* safety */
1736 if (hostIsNew
&& in6if_do_dad(ifp
))
1737 in6_ifaddr_set_dadprogress(ia
);
1740 * Do not delay sending neighbor solicitations when using optimistic
1741 * duplicate address detection, c.f. RFC 4429.
1743 if (ia
->ia6_flags
& IN6_IFF_OPTIMISTIC
)
1744 flags
&= ~IN6_IFAUPDATE_DADDELAY
;
1747 * We are done if we have simply modified an existing address.
1751 /* release reference held for this routine */
1756 * Beyond this point, we should call in6_purgeaddr upon an error,
1757 * not just go to unlink.
1759 IFA_LOCK_ASSERT_HELD(ifa
);
1760 /* Join necessary multicast groups */
1761 if ((ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1762 struct sockaddr_in6 mltaddr
, mltmask
;
1763 struct in6_addr llsol
;
1766 /* join solicited multicast addr for new host id */
1767 bzero(&llsol
, sizeof(struct in6_addr
));
1768 llsol
.s6_addr32
[0] = IPV6_ADDR_INT32_MLL
;
1769 llsol
.s6_addr32
[1] = 0;
1770 llsol
.s6_addr32
[2] = htonl(1);
1771 llsol
.s6_addr32
[3] = ifra
->ifra_addr
.sin6_addr
.s6_addr32
[3];
1772 llsol
.s6_addr8
[12] = 0xff;
1773 if ((error
= in6_setscope(&llsol
, ifp
, NULL
)) != 0) {
1774 /* XXX: should not happen */
1775 log(LOG_ERR
, "in6_update_ifa: "
1776 "in6_setscope failed\n");
1780 if ((flags
& IN6_IFAUPDATE_DADDELAY
)) {
1782 * We need a random delay for DAD on the address
1783 * being configured. It also means delaying
1784 * transmission of the corresponding MLD report to
1785 * avoid report collision.
1786 * [draft-ietf-ipv6-rfc2462bis-02.txt]
1789 (MAX_RTR_SOLICITATION_DELAY
* PR_SLOWHZ
);
1791 imm
= in6_joingroup(ifp
, &llsol
, &error
, delay
);
1793 nd6log((LOG_WARNING
,
1794 "in6_update_ifa: addmulti failed for "
1795 "%s on %s (errno=%d)\n",
1796 ip6_sprintf(&llsol
), if_name(ifp
),
1798 in6_purgeaddr((struct ifaddr
*)ia
);
1799 /* release reference held for this routine */
1803 in6m_sol
= imm
->i6mm_maddr
;
1804 /* take a refcount for this routine */
1805 IN6M_ADDREF(in6m_sol
);
1808 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
1811 bzero(&mltmask
, sizeof(mltmask
));
1812 mltmask
.sin6_len
= sizeof(struct sockaddr_in6
);
1813 mltmask
.sin6_family
= AF_INET6
;
1814 mltmask
.sin6_addr
= in6mask32
;
1815 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
1818 * join link-local all-nodes address
1820 bzero(&mltaddr
, sizeof(mltaddr
));
1821 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
1822 mltaddr
.sin6_family
= AF_INET6
;
1823 mltaddr
.sin6_addr
= in6addr_linklocal_allnodes
;
1824 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
)) !=
1826 goto cleanup
; /* XXX: should not fail */
1829 * XXX: do we really need this automatic routes?
1830 * We should probably reconsider this stuff. Most applications
1831 * actually do not need the routes, since they usually specify
1832 * the outgoing interface.
1834 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
1835 ia
->ia_ifp
->if_index
);
1837 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
1838 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN
)) {
1844 error
= rtrequest_scoped(RTM_ADD
,
1845 (struct sockaddr
*)&mltaddr
,
1846 (struct sockaddr
*)&ia
->ia_addr
,
1847 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
1848 NULL
, ia
->ia_ifp
->if_index
);
1855 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
1857 nd6log((LOG_WARNING
,
1858 "in6_update_ifa: addmulti failed for "
1859 "%s on %s (errno=%d)\n",
1860 ip6_sprintf(&mltaddr
.sin6_addr
),
1861 if_name(ifp
), error
));
1865 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
1869 * join node information group address
1871 #define hostnamelen strlen(hostname)
1873 if ((flags
& IN6_IFAUPDATE_DADDELAY
)) {
1875 * The spec doesn't say anything about delay for this
1876 * group, but the same logic should apply.
1879 (MAX_RTR_SOLICITATION_DELAY
* PR_SLOWHZ
);
1881 if (in6_nigroup(ifp
, hostname
, hostnamelen
, &mltaddr
.sin6_addr
)
1883 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
,
1884 delay
); /* XXX jinmei */
1886 nd6log((LOG_WARNING
, "in6_update_ifa: "
1887 "addmulti failed for %s on %s "
1889 ip6_sprintf(&mltaddr
.sin6_addr
),
1890 if_name(ifp
), error
));
1891 /* XXX not very fatal, go on... */
1894 LIST_INSERT_HEAD(&ia
->ia6_memberships
,
1902 * join interface-local all-nodes address.
1903 * (ff01::1%ifN, and ff01::%ifN/32)
1905 mltaddr
.sin6_addr
= in6addr_nodelocal_allnodes
;
1906 if ((error
= in6_setscope(&mltaddr
.sin6_addr
, ifp
, NULL
))
1908 goto cleanup
; /* XXX: should not fail */
1909 /* XXX: again, do we really need the route? */
1910 rt
= rtalloc1_scoped((struct sockaddr
*)&mltaddr
, 0, 0UL,
1911 ia
->ia_ifp
->if_index
);
1913 if (memcmp(&mltaddr
.sin6_addr
, &((struct sockaddr_in6
*)
1914 (void *)rt_key(rt
))->sin6_addr
, MLTMASK_LEN
)) {
1920 error
= rtrequest_scoped(RTM_ADD
,
1921 (struct sockaddr
*)&mltaddr
,
1922 (struct sockaddr
*)&ia
->ia_addr
,
1923 (struct sockaddr
*)&mltmask
, RTF_UP
| RTF_CLONING
,
1924 NULL
, ia
->ia_ifp
->if_index
);
1930 imm
= in6_joingroup(ifp
, &mltaddr
.sin6_addr
, &error
, 0);
1932 nd6log((LOG_WARNING
, "in6_update_ifa: "
1933 "addmulti failed for %s on %s "
1935 ip6_sprintf(&mltaddr
.sin6_addr
),
1936 if_name(ifp
), error
));
1940 LIST_INSERT_HEAD(&ia
->ia6_memberships
, imm
, i6mm_chain
);
1941 /* keep it locked */
1944 IFA_LOCK_ASSERT_HELD(ifa
);
1946 * Make sure to initialize ND6 information. this is to workaround
1947 * issues with interfaces with IPv6 addresses, which have never brought
1948 * up. We are assuming that it is safe to nd6_ifattach multiple times.
1949 * NOTE: this is how stf0 gets initialized
1951 if ((error
= nd6_ifattach(ifp
)) != 0)
1955 * Perform DAD, if needed.
1956 * XXX It may be of use, if we can administratively
1959 if (hostIsNew
&& in6if_do_dad(ifp
) &&
1960 ((ifra
->ifra_flags
& IN6_IFF_NODAD
) == 0) &&
1961 (ia
->ia6_flags
& IN6_IFF_DADPROGRESS
))
1963 int mindelay
, maxdelay
;
1964 int *delayptr
, delayval
;
1968 if ((flags
& IN6_IFAUPDATE_DADDELAY
)) {
1970 * We need to impose a delay before sending an NS
1971 * for DAD. Check if we also needed a delay for the
1972 * corresponding MLD message. If we did, the delay
1973 * should be larger than the MLD delay (this could be
1974 * relaxed a bit, but this simple logic is at least
1978 if (in6m_sol
!= NULL
) {
1979 IN6M_LOCK(in6m_sol
);
1980 if (in6m_sol
->in6m_state
== MLD_REPORTING_MEMBER
)
1981 mindelay
= in6m_sol
->in6m_timer
;
1982 IN6M_UNLOCK(in6m_sol
);
1984 maxdelay
= MAX_RTR_SOLICITATION_DELAY
* hz
;
1985 if (maxdelay
- mindelay
== 0)
1989 (random() % (maxdelay
- mindelay
)) +
1992 delayptr
= &delayval
;
1994 nd6_dad_start((struct ifaddr
*)ia
, delayptr
);
1999 /* release reference held for this routine */
2002 if (in6m_sol
!= NULL
)
2003 IN6M_REMREF(in6m_sol
);
2008 * XXX: if a change of an existing address failed, keep the entry
2012 in6_unlink_ifa(ia
, ifp
);
2017 in6_purgeaddr(&ia
->ia_ifa
);
2022 in6_purgeaddr(struct ifaddr
*ifa
)
2024 struct ifnet
*ifp
= ifa
->ifa_ifp
;
2025 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*) ifa
;
2026 struct in6_multi_mship
*imm
;
2028 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2030 /* stop DAD processing */
2034 * delete route to the destination of the address being purged.
2035 * The interface must be p2p or loopback in this case.
2038 if ((ia
->ia_flags
& IFA_ROUTE
) != 0 && ia
->ia_dstaddr
.sin6_len
!= 0) {
2042 if ((e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
))
2044 log(LOG_ERR
, "in6_purgeaddr: failed to remove "
2045 "a route to the p2p destination: %s on %s, "
2047 ip6_sprintf(&ia
->ia_addr
.sin6_addr
), if_name(ifp
),
2049 /* proceed anyway... */
2052 ia
->ia_flags
&= ~IFA_ROUTE
;
2058 IFA_LOCK_ASSERT_NOTHELD(ifa
);
2060 /* Remove ownaddr's loopback rtentry, if it exists. */
2061 in6_ifremloop(&(ia
->ia_ifa
));
2064 * leave from multicast groups we have joined for the interface
2067 while ((imm
= ia
->ia6_memberships
.lh_first
) != NULL
) {
2068 LIST_REMOVE(imm
, i6mm_chain
);
2070 in6_leavegroup(imm
);
2075 /* in6_unlink_ifa() will need exclusive access */
2076 in6_unlink_ifa(ia
, ifp
);
2077 in6_post_msg(ifp
, KEV_INET6_ADDR_DELETED
, ia
);
2081 in6_unlink_ifa(struct in6_ifaddr
*ia
, struct ifnet
*ifp
)
2083 struct in6_ifaddr
*oia
;
2087 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2092 ifnet_lock_exclusive(ifp
);
2094 if (ifa
->ifa_debug
& IFD_ATTACHED
)
2095 if_detach_ifa(ifp
, ifa
);
2097 ifnet_lock_done(ifp
);
2100 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2102 if (oia
== (ia
= in6_ifaddrs
)) {
2103 in6_ifaddrs
= ia
->ia_next
;
2105 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
2108 ia
->ia_next
= oia
->ia_next
;
2111 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
2117 * When an autoconfigured address is being removed, release the
2118 * reference to the base prefix. Also, since the release might
2119 * affect the status of other (detached) addresses, call
2120 * pfxlist_onlink_check().
2124 if ((oia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0) {
2125 if (oia
->ia6_ndpr
== NULL
) {
2126 log(LOG_NOTICE
, "in6_unlink_ifa: autoconf'ed address "
2127 "%p has no prefix\n", oia
);
2129 struct nd_prefix
*pr
= oia
->ia6_ndpr
;
2131 oia
->ia6_flags
&= ~IN6_IFF_AUTOCONF
;
2132 oia
->ia6_ndpr
= NULL
;
2134 VERIFY(pr
->ndpr_addrcnt
!= 0);
2137 NDPR_REMREF(pr
); /* release addr reference */
2140 lck_rw_done(&in6_ifaddr_rwlock
);
2141 lck_mtx_lock(nd6_mutex
);
2142 pfxlist_onlink_check();
2143 lck_mtx_unlock(nd6_mutex
);
2146 lck_rw_done(&in6_ifaddr_rwlock
);
2150 * release another refcnt for the link from in6_ifaddrs.
2151 * Do this only if it's not already unlinked in the event that we lost
2152 * the race, since in6_ifaddr_rwlock was momentarily dropped above.
2157 /* release reference held for this routine */
2162 in6_purgeif(struct ifnet
*ifp
)
2164 struct in6_ifaddr
*ia
;
2169 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
2171 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2173 while (ia
!= NULL
) {
2174 if (ia
->ia_ifa
.ifa_ifp
!= ifp
) {
2178 IFA_ADDREF(&ia
->ia_ifa
); /* for us */
2179 lck_rw_done(&in6_ifaddr_rwlock
);
2180 in6_purgeaddr(&ia
->ia_ifa
);
2181 IFA_REMREF(&ia
->ia_ifa
); /* for us */
2182 lck_rw_lock_exclusive(&in6_ifaddr_rwlock
);
2184 * Purging the address would have caused
2185 * in6_ifaddr_rwlock to be dropped and reacquired;
2186 * therefore search again from the beginning
2187 * of in6_ifaddrs list.
2191 lck_rw_done(&in6_ifaddr_rwlock
);
2198 * SIOCGLIFADDR: get first address. (?)
2199 * SIOCGLIFADDR with IFLR_PREFIX:
2200 * get first address that matches the specified prefix.
2201 * SIOCALIFADDR: add the specified address.
2202 * SIOCALIFADDR with IFLR_PREFIX:
2203 * add the specified prefix, filling hostaddr part from
2204 * the first link-local address. prefixlen must be <= 64.
2205 * SIOCDLIFADDR: delete the specified address.
2206 * SIOCDLIFADDR with IFLR_PREFIX:
2207 * delete the first address that matches the specified prefix.
2209 * EINVAL on invalid parameters
2210 * EADDRNOTAVAIL on prefix match failed/specified address not found
2211 * other values may be returned from in6_ioctl()
2213 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
2214 * this is to accomodate address naming scheme other than RFC2374,
2216 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
2217 * address encoding scheme. (see figure on page 8)
2220 in6_lifaddr_ioctl(struct socket
*so
, u_long cmd
, struct if_laddrreq
*iflr
,
2221 struct ifnet
*ifp
, struct proc
*p
)
2223 struct ifaddr
*ifa
= NULL
;
2224 struct sockaddr
*sa
;
2225 int p64
= proc_is64bit(p
);
2227 VERIFY(ifp
!= NULL
);
2231 /* address must be specified on GET with IFLR_PREFIX */
2232 if ((iflr
->flags
& IFLR_PREFIX
) == 0)
2237 /* address must be specified on ADD and DELETE */
2238 sa
= (struct sockaddr
*)&iflr
->addr
;
2239 if (sa
->sa_family
!= AF_INET6
)
2241 if (sa
->sa_len
!= sizeof(struct sockaddr_in6
))
2243 /* XXX need improvement */
2244 sa
= (struct sockaddr
*)&iflr
->dstaddr
;
2245 if (sa
->sa_family
&& sa
->sa_family
!= AF_INET6
)
2247 if (sa
->sa_len
&& sa
->sa_len
!= sizeof(struct sockaddr_in6
))
2250 default: /* shouldn't happen */
2252 panic("invalid cmd to in6_lifaddr_ioctl");
2258 if (sizeof(struct in6_addr
) * 8 < iflr
->prefixlen
)
2264 struct in6_aliasreq ifra
;
2265 struct in6_addr hostaddr
;
2267 int hostid_found
= 0;
2269 if ((iflr
->flags
& IFLR_PREFIX
) != 0) {
2270 struct sockaddr_in6
*sin6
;
2273 * hostaddr is to fill in the hostaddr part of the
2274 * address. hostaddr points to the first link-local
2275 * address attached to the interface.
2277 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
, 0);
2279 return EADDRNOTAVAIL
;
2281 hostaddr
= *IFA_IN6(ifa
);
2287 /* prefixlen must be <= 64. */
2288 if (64 < iflr
->prefixlen
)
2290 prefixlen
= iflr
->prefixlen
;
2292 /* hostid part must be zero. */
2293 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
2294 if (sin6
->sin6_addr
.s6_addr32
[2] != 0 ||
2295 sin6
->sin6_addr
.s6_addr32
[3] != 0) {
2299 prefixlen
= iflr
->prefixlen
;
2301 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
2302 bzero(&ifra
, sizeof(ifra
));
2303 bcopy(iflr
->iflr_name
, ifra
.ifra_name
, sizeof(ifra
.ifra_name
));
2305 bcopy(&iflr
->addr
, &ifra
.ifra_addr
,
2306 ((struct sockaddr
*)&iflr
->addr
)->sa_len
);
2308 /* fill in hostaddr part */
2309 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] =
2310 hostaddr
.s6_addr32
[2];
2311 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] =
2312 hostaddr
.s6_addr32
[3];
2315 if (((struct sockaddr
*)&iflr
->dstaddr
)->sa_family
) { /*XXX*/
2316 bcopy(&iflr
->dstaddr
, &ifra
.ifra_dstaddr
,
2317 ((struct sockaddr
*)&iflr
->dstaddr
)->sa_len
);
2319 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[2] =
2320 hostaddr
.s6_addr32
[2];
2321 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[3] =
2322 hostaddr
.s6_addr32
[3];
2326 ifra
.ifra_prefixmask
.sin6_len
= sizeof(struct sockaddr_in6
);
2327 in6_prefixlen2mask(&ifra
.ifra_prefixmask
.sin6_addr
, prefixlen
);
2329 ifra
.ifra_flags
= iflr
->flags
& ~IFLR_PREFIX
;
2331 #if defined(__LP64__)
2332 struct in6_aliasreq_32 ifra_32
;
2334 * Use 32-bit ioctl and structure for 32-bit process.
2336 in6_aliasreq_64_to_32((struct in6_aliasreq_64
*)&ifra
,
2338 return (in6_control(so
, SIOCAIFADDR_IN6_32
,
2339 (caddr_t
)&ifra_32
, ifp
, p
));
2341 return (in6_control(so
, SIOCAIFADDR_IN6
,
2342 (caddr_t
)&ifra
, ifp
, p
));
2343 #endif /* __LP64__ */
2345 #if defined(__LP64__)
2346 return (in6_control(so
, SIOCAIFADDR_IN6
,
2347 (caddr_t
)&ifra
, ifp
, p
));
2349 struct in6_aliasreq_64 ifra_64
;
2351 * Use 64-bit ioctl and structure for 64-bit process.
2353 in6_aliasreq_32_to_64((struct in6_aliasreq_32
*)&ifra
,
2355 return (in6_control(so
, SIOCAIFADDR_IN6_64
,
2356 (caddr_t
)&ifra_64
, ifp
, p
));
2357 #endif /* __LP64__ */
2364 struct in6_ifaddr
*ia
;
2365 struct in6_addr mask
, candidate
, match
;
2366 struct sockaddr_in6
*sin6
;
2369 bzero(&mask
, sizeof(mask
));
2370 if (iflr
->flags
& IFLR_PREFIX
) {
2371 /* lookup a prefix rather than address. */
2372 in6_prefixlen2mask(&mask
, iflr
->prefixlen
);
2374 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
2375 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
2376 match
.s6_addr32
[0] &= mask
.s6_addr32
[0];
2377 match
.s6_addr32
[1] &= mask
.s6_addr32
[1];
2378 match
.s6_addr32
[2] &= mask
.s6_addr32
[2];
2379 match
.s6_addr32
[3] &= mask
.s6_addr32
[3];
2381 /* if you set extra bits, that's wrong */
2382 if (bcmp(&match
, &sin6
->sin6_addr
, sizeof(match
)))
2387 if (cmd
== SIOCGLIFADDR
) {
2388 /* on getting an address, take the 1st match */
2391 /* on deleting an address, do exact match */
2392 in6_prefixlen2mask(&mask
, 128);
2393 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
2394 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
2400 ifnet_lock_shared(ifp
);
2401 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2404 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2413 bcopy(IFA_IN6(ifa
), &candidate
, sizeof(candidate
));
2416 * XXX: this is adhoc, but is necessary to allow
2417 * a user to specify fe80::/64 (not /10) for a
2418 * link-local address.
2420 if (IN6_IS_ADDR_LINKLOCAL(&candidate
))
2421 candidate
.s6_addr16
[1] = 0;
2422 candidate
.s6_addr32
[0] &= mask
.s6_addr32
[0];
2423 candidate
.s6_addr32
[1] &= mask
.s6_addr32
[1];
2424 candidate
.s6_addr32
[2] &= mask
.s6_addr32
[2];
2425 candidate
.s6_addr32
[3] &= mask
.s6_addr32
[3];
2426 if (IN6_ARE_ADDR_EQUAL(&candidate
, &match
))
2431 ifnet_lock_done(ifp
);
2433 return EADDRNOTAVAIL
;
2436 if (cmd
== SIOCGLIFADDR
) {
2437 struct sockaddr_in6
*s6
;
2440 /* fill in the if_laddrreq structure */
2441 bcopy(&ia
->ia_addr
, &iflr
->addr
, ia
->ia_addr
.sin6_len
);
2442 s6
= (struct sockaddr_in6
*)&iflr
->addr
;
2443 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
2444 s6
->sin6_addr
.s6_addr16
[1] = 0;
2446 in6_addr2scopeid(ifp
, &s6
->sin6_addr
);
2448 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
2449 bcopy(&ia
->ia_dstaddr
, &iflr
->dstaddr
,
2450 ia
->ia_dstaddr
.sin6_len
);
2451 s6
= (struct sockaddr_in6
*)&iflr
->dstaddr
;
2452 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
2453 s6
->sin6_addr
.s6_addr16
[1] = 0;
2455 in6_addr2scopeid(ifp
,
2459 bzero(&iflr
->dstaddr
, sizeof(iflr
->dstaddr
));
2462 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
2465 iflr
->flags
= ia
->ia6_flags
; /* XXX */
2470 struct in6_aliasreq ifra
;
2472 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
2473 bzero(&ifra
, sizeof(ifra
));
2474 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
2475 sizeof(ifra
.ifra_name
));
2478 bcopy(&ia
->ia_addr
, &ifra
.ifra_addr
,
2479 ia
->ia_addr
.sin6_len
);
2480 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
2481 bcopy(&ia
->ia_dstaddr
, &ifra
.ifra_dstaddr
,
2482 ia
->ia_dstaddr
.sin6_len
);
2484 bzero(&ifra
.ifra_dstaddr
,
2485 sizeof(ifra
.ifra_dstaddr
));
2487 bcopy(&ia
->ia_prefixmask
, &ifra
.ifra_dstaddr
,
2488 ia
->ia_prefixmask
.sin6_len
);
2490 ifra
.ifra_flags
= ia
->ia6_flags
;
2494 #if defined(__LP64__)
2495 struct in6_aliasreq_32 ifra_32
;
2497 * Use 32-bit structure for 32-bit process.
2498 * SIOCDIFADDR_IN6 is encoded with in6_ifreq,
2499 * so it stays the same since the size does
2500 * not change. The data part of the ioctl,
2501 * however, is of a different structure, i.e.
2504 in6_aliasreq_64_to_32(
2505 (struct in6_aliasreq_64
*)&ifra
, &ifra_32
);
2506 return (in6_control(so
, SIOCDIFADDR_IN6
,
2507 (caddr_t
)&ifra_32
, ifp
, p
));
2509 return (in6_control(so
, SIOCDIFADDR_IN6
,
2510 (caddr_t
)&ifra
, ifp
, p
));
2511 #endif /* __LP64__ */
2513 #if defined(__LP64__)
2514 return (in6_control(so
, SIOCDIFADDR_IN6
,
2515 (caddr_t
)&ifra
, ifp
, p
));
2517 struct in6_aliasreq_64 ifra_64
;
2519 * Use 64-bit structure for 64-bit process.
2520 * SIOCDIFADDR_IN6 is encoded with in6_ifreq,
2521 * so it stays the same since the size does
2522 * not change. The data part of the ioctl,
2523 * however, is of a different structure, i.e.
2526 in6_aliasreq_32_to_64(
2527 (struct in6_aliasreq_32
*)&ifra
, &ifra_64
);
2528 return (in6_control(so
, SIOCDIFADDR_IN6
,
2529 (caddr_t
)&ifra_64
, ifp
, p
));
2530 #endif /* __LP64__ */
2537 return EOPNOTSUPP
; /* just for safety */
2541 * Initialize an interface's intetnet6 address
2542 * and routing table entry.
2545 in6_ifinit(ifp
, ia
, sin6
, newhost
)
2547 struct in6_ifaddr
*ia
;
2548 struct sockaddr_in6
*sin6
;
2551 int error
= 0, plen
, ifacount
= 0;
2555 * Give the interface a chance to initialize
2556 * if this is its first address,
2557 * and to validate the address if necessary.
2559 ifnet_lock_shared(ifp
);
2560 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2563 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2570 ifnet_lock_done(ifp
);
2574 ia
->ia_addr
= *sin6
;
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 (ifacount
<= 1 &&
2583 (error
= ifnet_ioctl(ifp
, PF_INET6
, SIOCSIFADDR
, ia
))) {
2584 if (error
== EOPNOTSUPP
)
2591 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
2593 /* we could do in(6)_socktrim here, but just omit it at this moment. */
2597 * If the destination address is specified for a point-to-point
2598 * interface, install a route to the destination as an interface
2601 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
); /* XXX */
2602 if (plen
== 128 && ia
->ia_dstaddr
.sin6_family
== AF_INET6
) {
2604 if ((error
= rtinit(&(ia
->ia_ifa
), (int)RTM_ADD
,
2605 RTF_UP
| RTF_HOST
)) != 0)
2608 ia
->ia_flags
|= IFA_ROUTE
;
2610 IFA_LOCK_ASSERT_HELD(ifa
);
2613 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
2615 ia
->ia_ifa
.ifa_flags
|= RTF_CLONING
;
2618 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
2620 /* set the rtrequest function to create llinfo */
2621 ia
->ia_ifa
.ifa_rtrequest
= nd6_rtrequest
;
2623 in6_ifaddloop(&(ia
->ia_ifa
));
2632 in6_purgeaddrs(struct ifnet
*ifp
)
2638 * Find an IPv6 interface link-local address specific to an interface.
2641 in6ifa_ifpforlinklocal(ifp
, ignoreflags
)
2647 ifnet_lock_shared(ifp
);
2648 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2651 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2655 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
))) {
2656 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2657 ignoreflags
) != 0) {
2661 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2667 ifnet_lock_done(ifp
);
2669 return((struct in6_ifaddr
*)ifa
);
2673 * find the internet address corresponding to a given interface and address.
2676 in6ifa_ifpwithaddr(ifp
, addr
)
2678 struct in6_addr
*addr
;
2682 ifnet_lock_shared(ifp
);
2683 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2686 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2690 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(ifa
))) {
2691 IFA_ADDREF_LOCKED(ifa
); /* for caller */
2697 ifnet_lock_done(ifp
);
2699 return((struct in6_ifaddr
*)ifa
);
2703 in6ifa_prproxyaddr(struct in6_addr
*addr
)
2705 struct in6_ifaddr
*ia
;
2707 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2708 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2709 IFA_LOCK(&ia
->ia_ifa
);
2710 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(&ia
->ia_ifa
))) {
2711 IFA_ADDREF_LOCKED(&ia
->ia_ifa
); /* for caller */
2712 IFA_UNLOCK(&ia
->ia_ifa
);
2715 IFA_UNLOCK(&ia
->ia_ifa
);
2717 lck_rw_done(&in6_ifaddr_rwlock
);
2719 if (ia
!= NULL
&& !nd6_prproxy_ifaddr(ia
)) {
2720 IFA_REMREF(&ia
->ia_ifa
);
2728 * Convert IP6 address to printable (loggable) representation.
2730 static char digits
[] = "0123456789abcdef";
2731 static int ip6round
= 0;
2734 const struct in6_addr
*addr
;
2736 static char ip6buf
[8][48];
2739 const u_short
*a
= (const u_short
*)addr
;
2743 ip6round
= (ip6round
+ 1) & 7;
2744 cp
= ip6buf
[ip6round
];
2746 for (i
= 0; i
< 8; i
++) {
2757 if (dcolon
== 0 && *(a
+ 1) == 0) {
2769 d
= (const u_char
*)a
;
2770 *cp
++ = digits
[*d
>> 4];
2771 *cp
++ = digits
[*d
++ & 0xf];
2772 *cp
++ = digits
[*d
>> 4];
2773 *cp
++ = digits
[*d
& 0xf];
2778 return(ip6buf
[ip6round
]);
2782 in6addr_local(struct in6_addr
*in6
)
2785 struct sockaddr_in6 sin6
;
2788 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_SCOPE_LINKLOCAL(in6
))
2791 sin6
.sin6_family
= AF_INET6
;
2792 sin6
.sin6_len
= sizeof (sin6
);
2793 bcopy(in6
, &sin6
.sin6_addr
, sizeof (*in6
));
2794 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0);
2798 if (rt
->rt_gateway
->sa_family
== AF_LINK
)
2803 local
= in6_localaddr(in6
);
2809 in6_localaddr(struct in6_addr
*in6
)
2811 struct in6_ifaddr
*ia
;
2813 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
))
2816 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2817 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2818 IFA_LOCK_SPIN(&ia
->ia_ifa
);
2819 if (IN6_ARE_MASKED_ADDR_EQUAL(in6
, &ia
->ia_addr
.sin6_addr
,
2820 &ia
->ia_prefixmask
.sin6_addr
)) {
2821 IFA_UNLOCK(&ia
->ia_ifa
);
2822 lck_rw_done(&in6_ifaddr_rwlock
);
2825 IFA_UNLOCK(&ia
->ia_ifa
);
2827 lck_rw_done(&in6_ifaddr_rwlock
);
2832 in6_is_addr_deprecated(struct sockaddr_in6
*sa6
)
2834 struct in6_ifaddr
*ia
;
2836 lck_rw_lock_shared(&in6_ifaddr_rwlock
);
2837 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2838 IFA_LOCK_SPIN(&ia
->ia_ifa
);
2839 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
,
2841 (ia
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0) {
2842 IFA_UNLOCK(&ia
->ia_ifa
);
2843 lck_rw_done(&in6_ifaddr_rwlock
);
2844 return(1); /* true */
2846 /* XXX: do we still have to go thru the rest of the list? */
2847 IFA_UNLOCK(&ia
->ia_ifa
);
2850 lck_rw_done(&in6_ifaddr_rwlock
);
2851 return(0); /* false */
2855 * return length of part which dst and src are equal
2859 in6_matchlen(src
, dst
)
2860 struct in6_addr
*src
, *dst
;
2863 u_char
*s
= (u_char
*)src
, *d
= (u_char
*)dst
;
2864 u_char
*lim
= s
+ 16, r
;
2867 if ((r
= (*d
++ ^ *s
++)) != 0) {
2878 /* XXX: to be scope conscious */
2880 in6_are_prefix_equal(p1
, p2
, len
)
2881 struct in6_addr
*p1
, *p2
;
2884 int bytelen
, bitlen
;
2887 if (0 > len
|| len
> 128) {
2888 log(LOG_ERR
, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2896 if (bcmp(&p1
->s6_addr
, &p2
->s6_addr
, bytelen
))
2899 p1
->s6_addr
[bytelen
] >> (8 - bitlen
) !=
2900 p2
->s6_addr
[bytelen
] >> (8 - bitlen
))
2907 in6_prefixlen2mask(maskp
, len
)
2908 struct in6_addr
*maskp
;
2911 u_char maskarray
[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2912 int bytelen
, bitlen
, i
;
2915 if (0 > len
|| len
> 128) {
2916 log(LOG_ERR
, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2921 bzero(maskp
, sizeof(*maskp
));
2924 for (i
= 0; i
< bytelen
; i
++)
2925 maskp
->s6_addr
[i
] = 0xff;
2927 maskp
->s6_addr
[bytelen
] = maskarray
[bitlen
- 1];
2931 * return the best address out of the same scope
2936 struct in6_addr
*dst
)
2938 int dst_scope
= in6_addrscope(dst
), src_scope
, best_scope
= 0;
2942 struct in6_ifaddr
*ifa_best
= NULL
;
2949 * We search for all addresses on all interfaces from the beginning.
2950 * Comparing an interface with the outgoing interface will be done
2951 * only at the final stage of tiebreaking.
2953 ifnet_head_lock_shared();
2954 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
2956 * We can never take an address that breaks the scope zone
2957 * of the destination.
2959 if (in6_addr2scopeid(ifp
, dst
) != in6_addr2scopeid(oifp
, dst
))
2962 ifnet_lock_shared(ifp
);
2963 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2965 int tlen
= -1, dscopecmp
, bscopecmp
, matchcmp
;
2968 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
2972 src_scope
= in6_addrscope(IFA_IN6(ifa
));
2975 * Don't use an address before completing DAD
2976 * nor a duplicated address.
2978 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2983 /* XXX: is there any case to allow anycasts? */
2984 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2989 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2995 * If this is the first address we find,
2998 if (ifa_best
== NULL
)
3002 * ifa_best is never NULL beyond this line except
3003 * within the block labeled "replace".
3007 * If ifa_best has a smaller scope than dst and
3008 * the current address has a larger one than
3009 * (or equal to) dst, always replace ifa_best.
3010 * Also, if the current address has a smaller scope
3011 * than dst, ignore it unless ifa_best also has a
3013 * Consequently, after the two if-clause below,
3014 * the followings must be satisfied:
3015 * (scope(src) < scope(dst) &&
3016 * scope(best) < scope(dst))
3018 * (scope(best) >= scope(dst) &&
3019 * scope(src) >= scope(dst))
3021 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0 &&
3022 IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) >= 0)
3023 goto replace
; /* (A) */
3024 if (IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) < 0 &&
3025 IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) >= 0) {
3030 * A deprecated address SHOULD NOT be used in new
3031 * communications if an alternate (non-deprecated)
3032 * address is available and has sufficient scope.
3033 * RFC 2462, Section 5.5.4.
3035 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3036 IN6_IFF_DEPRECATED
) {
3038 * Ignore any deprecated addresses if
3039 * specified by configuration.
3041 if (!ip6_use_deprecated
) {
3046 * If we have already found a non-deprecated
3047 * candidate, just ignore deprecated addresses.
3049 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
)
3057 * A non-deprecated address is always preferred
3058 * to a deprecated one regardless of scopes and
3059 * address matching (Note invariants ensured by the
3060 * conditions (A) and (B) above.)
3062 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) &&
3063 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
3064 IN6_IFF_DEPRECATED
) == 0)
3068 * When we use temporary addresses described in
3069 * RFC 4941, we prefer temporary addresses to
3070 * public autoconf addresses. Again, note the
3071 * invariants from (A) and (B). Also note that we
3072 * don't have any preference between static addresses
3073 * and autoconf addresses (despite of whether or not
3074 * the latter is temporary or public.)
3076 if (ip6_use_tempaddr
) {
3077 struct in6_ifaddr
*ifat
;
3079 ifat
= (struct in6_ifaddr
*)ifa
;
3080 if ((ifa_best
->ia6_flags
&
3081 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3082 == IN6_IFF_AUTOCONF
&&
3084 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3085 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
)) {
3088 if ((ifa_best
->ia6_flags
&
3089 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3090 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
) &&
3092 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
3093 == IN6_IFF_AUTOCONF
) {
3100 * At this point, we have two cases:
3101 * 1. we are looking at a non-deprecated address,
3102 * and ifa_best is also non-deprecated.
3103 * 2. we are looking at a deprecated address,
3104 * and ifa_best is also deprecated.
3105 * Also, we do not have to consider a case where
3106 * the scope of if_best is larger(smaller) than dst and
3107 * the scope of the current address is smaller(larger)
3108 * than dst. Such a case has already been covered.
3109 * Tiebreaking is done according to the following
3111 * - the scope comparison between the address and
3113 * - the scope comparison between the address and
3114 * ifa_best (bscopecmp)
3115 * - if the address match dst longer than ifa_best
3117 * - if the address is on the outgoing I/F (outI/F)
3119 * Roughly speaking, the selection policy is
3120 * - the most important item is scope. The same scope
3121 * is best. Then search for a larger scope.
3122 * Smaller scopes are the last resort.
3123 * - A deprecated address is chosen only when we have
3124 * no address that has an enough scope, but is
3125 * prefered to any addresses of smaller scopes
3126 * (this must be already done above.)
3127 * - addresses on the outgoing I/F are preferred to
3128 * ones on other interfaces if none of above
3129 * tiebreaks. In the table below, the column "bI"
3130 * means if the best_ifa is on the outgoing
3131 * interface, and the column "sI" means if the ifa
3132 * is on the outgoing interface.
3133 * - If there is no other reasons to choose one,
3134 * longest address match against dst is considered.
3136 * The precise decision table is as follows:
3137 * dscopecmp bscopecmp match bI oI | replace?
3138 * N/A equal N/A Y N | No (1)
3139 * N/A equal N/A N Y | Yes (2)
3140 * N/A equal larger N/A | Yes (3)
3141 * N/A equal !larger N/A | No (4)
3142 * larger larger N/A N/A | No (5)
3143 * larger smaller N/A N/A | Yes (6)
3144 * smaller larger N/A N/A | Yes (7)
3145 * smaller smaller N/A N/A | No (8)
3146 * equal smaller N/A N/A | Yes (9)
3147 * equal larger (already done at A above)
3149 dscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
);
3150 bscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, best_scope
);
3152 if (bscopecmp
== 0) {
3153 struct ifnet
*bifp
= ifa_best
->ia_ifp
;
3155 if (bifp
== oifp
&& ifp
!= oifp
) { /* (1) */
3159 if (bifp
!= oifp
&& ifp
== oifp
) /* (2) */
3163 * Both bifp and ifp are on the outgoing
3164 * interface, or both two are on a different
3165 * interface from the outgoing I/F.
3166 * now we need address matching against dst
3169 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3170 matchcmp
= tlen
- blen
;
3171 if (matchcmp
> 0) /* (3) */
3176 if (dscopecmp
> 0) {
3177 if (bscopecmp
> 0) { /* (5) */
3181 goto replace
; /* (6) */
3183 if (dscopecmp
< 0) {
3184 if (bscopecmp
> 0) /* (7) */
3190 /* now dscopecmp must be 0 */
3192 goto replace
; /* (9) */
3195 IFA_ADDREF_LOCKED(ifa
); /* for ifa_best */
3196 blen
= tlen
>= 0 ? tlen
:
3197 in6_matchlen(IFA_IN6(ifa
), dst
);
3198 best_scope
= in6_addrscope(&ifa2ia6(ifa
)->ia_addr
.sin6_addr
);
3201 IFA_REMREF(&ifa_best
->ia_ifa
);
3202 ifa_best
= (struct in6_ifaddr
*)ifa
;
3204 ifnet_lock_done(ifp
);
3208 /* count statistics for future improvements */
3209 if (ifa_best
== NULL
)
3210 ip6stat
.ip6s_sources_none
++;
3212 IFA_LOCK_SPIN(&ifa_best
->ia_ifa
);
3213 if (oifp
== ifa_best
->ia_ifp
)
3214 ip6stat
.ip6s_sources_sameif
[best_scope
]++;
3216 ip6stat
.ip6s_sources_otherif
[best_scope
]++;
3218 if (best_scope
== dst_scope
)
3219 ip6stat
.ip6s_sources_samescope
[best_scope
]++;
3221 ip6stat
.ip6s_sources_otherscope
[best_scope
]++;
3223 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0)
3224 ip6stat
.ip6s_sources_deprecated
[best_scope
]++;
3225 IFA_UNLOCK(&ifa_best
->ia_ifa
);
3232 * return the best address out of the same scope. if no address was
3233 * found, return the first valid address from designated IF.
3238 struct in6_addr
*dst
)
3240 int dst_scope
= in6_addrscope(dst
), blen
= -1, tlen
;
3242 struct in6_ifaddr
*besta
= NULL
;
3243 struct in6_ifaddr
*dep
[2]; /* last-resort: deprecated */
3245 dep
[0] = dep
[1] = NULL
;
3248 * We first look for addresses in the same scope.
3249 * If there is one, return it.
3250 * If two or more, return one which matches the dst longest.
3251 * If none, return one of global addresses assigned other ifs.
3253 ifnet_lock_shared(ifp
);
3254 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
3257 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3261 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3263 continue; /* XXX: is there any case to allow anycast? */
3265 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_NOTREADY
) {
3267 continue; /* don't use this interface */
3269 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3273 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3274 if (ip6_use_deprecated
) {
3275 IFA_ADDREF_LOCKED(ifa
); /* for dep[0] */
3278 IFA_REMREF(&dep
[0]->ia_ifa
);
3279 dep
[0] = (struct in6_ifaddr
*)ifa
;
3286 if (dst_scope
== in6_addrscope(IFA_IN6(ifa
))) {
3288 * call in6_matchlen() as few as possible
3293 IFA_LOCK(&besta
->ia_ifa
);
3294 blen
= in6_matchlen(&besta
->ia_addr
.sin6_addr
, dst
);
3295 IFA_UNLOCK(&besta
->ia_ifa
);
3298 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
3301 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3303 IFA_REMREF(&besta
->ia_ifa
);
3304 besta
= (struct in6_ifaddr
*)ifa
;
3309 besta
= (struct in6_ifaddr
*)ifa
;
3310 IFA_ADDREF_LOCKED(ifa
); /* for besta */
3318 ifnet_lock_done(ifp
);
3320 IFA_REMREF(&dep
[0]->ia_ifa
);
3324 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
3327 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3331 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_ANYCAST
) {
3333 continue; /* XXX: is there any case to allow anycast? */
3335 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_NOTREADY
) {
3337 continue; /* don't use this interface */
3339 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DETACHED
) {
3343 if (ifa2ia6(ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
3344 if (ip6_use_deprecated
) {
3345 IFA_ADDREF_LOCKED(ifa
); /* for dep[1] */
3348 IFA_REMREF(&dep
[1]->ia_ifa
);
3349 dep
[1] = (struct in6_ifaddr
*)ifa
;
3355 IFA_ADDREF_LOCKED(ifa
); /* for caller */
3357 ifnet_lock_done(ifp
);
3359 IFA_REMREF(&dep
[0]->ia_ifa
);
3361 IFA_REMREF(&dep
[1]->ia_ifa
);
3362 return (struct in6_ifaddr
*)ifa
;
3364 ifnet_lock_done(ifp
);
3366 /* use the last-resort values, that are, deprecated addresses */
3369 IFA_REMREF(&dep
[1]->ia_ifa
);
3379 * perform DAD when interface becomes IFF_UP.
3384 struct in6_aliasreq
*ifra
)
3387 struct in6_ifaddr
*ia
;
3388 int dad_delay
; /* delay ticks before DAD output */
3392 * special cases, like 6to4, are handled in in6_ifattach
3394 error
= in6_ifattach(ifp
, NULL
, ifra
);
3400 ifnet_lock_exclusive(ifp
);
3401 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
3404 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
3408 ia
= (struct in6_ifaddr
*)ifa
;
3409 if (ia
->ia6_flags
& IN6_IFF_DADPROGRESS
) {
3411 nd6_dad_start(ifa
, &dad_delay
);
3416 ifnet_lock_done(ifp
);
3425 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0)
3429 * If we are using the alternative neighbor discovery
3430 * interface on this interface, then skip DAD.
3432 * Also, skip it for interfaces marked "local private"
3433 * for now, even when not marked as using the alternative
3434 * interface. This is for historical reasons.
3436 if (ifp
->if_eflags
& (IFEF_IPV6_ND6ALT
|IFEF_LOCALNET_PRIVATE
))
3439 switch (ifp
->if_type
) {
3445 * These interfaces do not have the IFF_LOOPBACK flag,
3446 * but loop packets back. We do not have to do DAD on such
3447 * interfaces. We should even omit it, because loop-backed
3448 * NS would confuse the DAD procedure.
3453 * Our DAD routine requires the interface up and running.
3454 * However, some interfaces can be up before the RUNNING
3455 * status. Additionaly, users may try to assign addresses
3456 * before the interface becomes up (or running).
3457 * We simply skip DAD in such a case as a work around.
3458 * XXX: we should rather mark "tentative" on such addresses,
3459 * and do DAD after the interface becomes ready.
3461 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) !=
3462 (IFF_UP
|IFF_RUNNING
))
3470 * Calculate max IPv6 MTU through all the interfaces and store it
3476 u_int32_t maxmtu
= 0;
3479 ifnet_head_lock_shared();
3480 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
3481 struct nd_ifinfo
*ndi
;
3483 lck_rw_lock_shared(nd_if_rwlock
);
3484 if ((ndi
= ND_IFINFO(ifp
)) != NULL
&& !ndi
->initialized
)
3487 lck_mtx_lock(&ndi
->lock
);
3488 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
3489 IN6_LINKMTU(ifp
) > maxmtu
)
3490 maxmtu
= IN6_LINKMTU(ifp
);
3492 lck_mtx_unlock(&ndi
->lock
);
3493 lck_rw_done(nd_if_rwlock
);
3496 if (maxmtu
) /* update only when maxmtu is positive */
3497 in6_maxmtu
= maxmtu
;
3500 * Provide the length of interface identifiers to be used for the link attached
3501 * to the given interface. The length should be defined in "IPv6 over
3502 * xxx-link" document. Note that address architecture might also define
3503 * the length for a particular set of address prefixes, regardless of the
3504 * link type. As clarified in rfc2462bis, those two definitions should be
3505 * consistent, and those really are as of August 2004.
3508 in6_if2idlen(struct ifnet
*ifp
)
3510 switch (ifp
->if_type
) {
3511 case IFT_ETHER
: /* RFC2464 */
3512 case IFT_IEEE8023ADLAG
: /* IEEE802.3ad Link Aggregate */
3513 #ifdef IFT_PROPVIRTUAL
3514 case IFT_PROPVIRTUAL
: /* XXX: no RFC. treat it as ether */
3517 case IFT_L2VLAN
: /* ditto */
3519 #ifdef IFT_IEEE80211
3520 case IFT_IEEE80211
: /* ditto */
3523 case IFT_MIP
: /* ditto */
3526 case IFT_FDDI
: /* RFC2467 */
3528 case IFT_ISO88025
: /* RFC2470 (IPv6 over Token Ring) */
3530 case IFT_PPP
: /* RFC2472 */
3532 case IFT_ARCNET
: /* RFC2497 */
3534 case IFT_FRELAY
: /* RFC2590 */
3536 case IFT_IEEE1394
: /* RFC3146 */
3539 return (64); /* draft-ietf-v6ops-mech-v2-07 */
3541 return (64); /* XXX: is this really correct? */
3543 return (64); /* for utun interfaces */
3545 return (64); /* Packet Data over Cellular */
3547 return (64); /* Transparent bridge interface */
3550 * Unknown link type:
3551 * It might be controversial to use the today's common constant
3552 * of 64 for these cases unconditionally. For full compliance,
3553 * we should return an error in this case. On the other hand,
3554 * if we simply miss the standard for the link type or a new
3555 * standard is defined for a new link type, the IFID length
3556 * is very likely to be the common constant. As a compromise,
3557 * we always use the constant, but make an explicit notice
3558 * indicating the "unknown" case.
3560 printf("in6_if2idlen: unknown link type (%d)\n", ifp
->if_type
);
3565 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
3566 * v4 mapped addr or v4 compat addr
3569 in6_sin6_2_sin(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3571 bzero(sin
, sizeof(*sin
));
3572 sin
->sin_len
= sizeof(struct sockaddr_in
);
3573 sin
->sin_family
= AF_INET
;
3574 sin
->sin_port
= sin6
->sin6_port
;
3575 sin
->sin_addr
.s_addr
= sin6
->sin6_addr
.s6_addr32
[3];
3578 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
3580 in6_sin_2_v4mapsin6(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3582 bzero(sin6
, sizeof(*sin6
));
3583 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
3584 sin6
->sin6_family
= AF_INET6
;
3585 sin6
->sin6_port
= sin
->sin_port
;
3586 sin6
->sin6_addr
.s6_addr32
[0] = 0;
3587 sin6
->sin6_addr
.s6_addr32
[1] = 0;
3588 if (sin
->sin_addr
.s_addr
) {
3589 sin6
->sin6_addr
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
3590 sin6
->sin6_addr
.s6_addr32
[3] = sin
->sin_addr
.s_addr
;
3593 sin6
->sin6_addr
.s6_addr32
[2] = 0;
3594 sin6
->sin6_addr
.s6_addr32
[3] = 0;
3598 /* Convert sockaddr_in6 into sockaddr_in. */
3600 in6_sin6_2_sin_in_sock(struct sockaddr
*nam
)
3602 struct sockaddr_in
*sin_p
;
3603 struct sockaddr_in6 sin6
;
3606 * Save original sockaddr_in6 addr and convert it
3609 sin6
= *(struct sockaddr_in6
*)(void *)nam
;
3610 sin_p
= (struct sockaddr_in
*)(void *)nam
;
3611 in6_sin6_2_sin(sin_p
, &sin6
);
3614 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
3616 in6_sin_2_v4mapsin6_in_sock(struct sockaddr
**nam
)
3618 struct sockaddr_in
*sin_p
;
3619 struct sockaddr_in6
*sin6_p
;
3621 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof *sin6_p
, M_SONAME
,
3625 sin_p
= (struct sockaddr_in
*)(void *)*nam
;
3626 in6_sin_2_v4mapsin6(sin_p
, sin6_p
);
3627 FREE(*nam
, M_SONAME
);
3628 *nam
= (struct sockaddr
*)sin6_p
;
3634 * Posts in6_event_data message kernel events.
3636 * To get the same size of kev_in6_data between ILP32 and LP64 data models
3637 * we are using a special version of the in6_addrlifetime structure that
3638 * uses only 32 bits fields to be compatible with Leopard, and that
3639 * are large enough to span 68 years.
3642 in6_post_msg(struct ifnet
*ifp
, u_int32_t event_code
, struct in6_ifaddr
*ifa
)
3644 struct kev_msg ev_msg
;
3645 struct kev_in6_data in6_event_data
;
3647 bzero(&in6_event_data
, sizeof(struct kev_in6_data
));
3648 bzero(&ev_msg
, sizeof(struct kev_msg
));
3649 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3650 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3651 ev_msg
.kev_subclass
= KEV_INET6_SUBCLASS
;
3652 ev_msg
.event_code
= event_code
;
3654 IFA_LOCK(&ifa
->ia_ifa
);
3655 in6_event_data
.ia_addr
= ifa
->ia_addr
;
3656 in6_event_data
.ia_net
= ifa
->ia_net
;
3657 in6_event_data
.ia_dstaddr
= ifa
->ia_dstaddr
;
3658 in6_event_data
.ia_prefixmask
= ifa
->ia_prefixmask
;
3659 in6_event_data
.ia_plen
= ifa
->ia_plen
;
3660 in6_event_data
.ia6_flags
= (u_int32_t
)ifa
->ia6_flags
;
3662 in6_event_data
.ia_lifetime
.ia6t_expire
=
3663 ifa
->ia6_lifetime
.ia6t_expire
;
3664 in6_event_data
.ia_lifetime
.ia6t_preferred
=
3665 ifa
->ia6_lifetime
.ia6t_preferred
;
3666 in6_event_data
.ia_lifetime
.ia6t_vltime
=
3667 ifa
->ia6_lifetime
.ia6t_vltime
;
3668 in6_event_data
.ia_lifetime
.ia6t_pltime
=
3669 ifa
->ia6_lifetime
.ia6t_pltime
;
3670 IFA_UNLOCK(&ifa
->ia_ifa
);
3673 (void) strncpy(&in6_event_data
.link_data
.if_name
[0],
3674 ifp
->if_name
, IFNAMSIZ
);
3675 in6_event_data
.link_data
.if_family
= ifp
->if_family
;
3676 in6_event_data
.link_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3679 ev_msg
.dv
[0].data_ptr
= &in6_event_data
;
3680 ev_msg
.dv
[0].data_length
= sizeof (in6_event_data
);
3681 ev_msg
.dv
[1].data_length
= 0;
3683 kev_post_msg(&ev_msg
);
3687 * Called as part of ip6_init
3690 in6_ifaddr_init(void)
3694 PE_parse_boot_argn("ifa_debug", &in6ifa_debug
, sizeof (in6ifa_debug
));
3696 in6ifa_size
= (in6ifa_debug
== 0) ? sizeof (struct in6_ifaddr
) :
3697 sizeof (struct in6_ifaddr_dbg
);
3699 in6ifa_zone
= zinit(in6ifa_size
, IN6IFA_ZONE_MAX
* in6ifa_size
,
3700 0, IN6IFA_ZONE_NAME
);
3701 if (in6ifa_zone
== NULL
) {
3702 panic("%s: failed allocating %s", __func__
, IN6IFA_ZONE_NAME
);
3705 zone_change(in6ifa_zone
, Z_EXPAND
, TRUE
);
3706 zone_change(in6ifa_zone
, Z_CALLERACCT
, FALSE
);
3708 lck_mtx_init(&in6ifa_trash_lock
, ifa_mtx_grp
, ifa_mtx_attr
);
3709 TAILQ_INIT(&in6ifa_trash_head
);
3712 static struct in6_ifaddr
*
3713 in6_ifaddr_alloc(int how
)
3715 struct in6_ifaddr
*in6ifa
;
3717 in6ifa
= (how
== M_WAITOK
) ? zalloc(in6ifa_zone
) :
3718 zalloc_noblock(in6ifa_zone
);
3719 if (in6ifa
!= NULL
) {
3720 bzero(in6ifa
, in6ifa_size
);
3721 in6ifa
->ia_ifa
.ifa_free
= in6_ifaddr_free
;
3722 in6ifa
->ia_ifa
.ifa_debug
|= IFD_ALLOC
;
3723 ifa_lock_init(&in6ifa
->ia_ifa
);
3724 if (in6ifa_debug
!= 0) {
3725 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3726 (struct in6_ifaddr_dbg
*)in6ifa
;
3727 in6ifa
->ia_ifa
.ifa_debug
|= IFD_DEBUG
;
3728 in6ifa
->ia_ifa
.ifa_trace
= in6_ifaddr_trace
;
3729 in6ifa
->ia_ifa
.ifa_attached
= in6_ifaddr_attached
;
3730 in6ifa
->ia_ifa
.ifa_detached
= in6_ifaddr_detached
;
3731 ctrace_record(&in6ifa_dbg
->in6ifa_alloc
);
3738 in6_ifaddr_free(struct ifaddr
*ifa
)
3740 IFA_LOCK_ASSERT_HELD(ifa
);
3742 if (ifa
->ifa_refcnt
!= 0) {
3743 panic("%s: ifa %p bad ref cnt", __func__
, ifa
);
3745 } else if (!(ifa
->ifa_debug
& IFD_ALLOC
)) {
3746 panic("%s: ifa %p cannot be freed", __func__
, ifa
);
3749 if (ifa
->ifa_debug
& IFD_DEBUG
) {
3750 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3751 (struct in6_ifaddr_dbg
*)ifa
;
3752 ctrace_record(&in6ifa_dbg
->in6ifa_free
);
3753 bcopy(&in6ifa_dbg
->in6ifa
, &in6ifa_dbg
->in6ifa_old
,
3754 sizeof (struct in6_ifaddr
));
3755 if (ifa
->ifa_debug
& IFD_TRASHED
) {
3756 /* Become a regular mutex, just in case */
3757 IFA_CONVERT_LOCK(ifa
);
3758 lck_mtx_lock(&in6ifa_trash_lock
);
3759 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
,
3761 lck_mtx_unlock(&in6ifa_trash_lock
);
3762 ifa
->ifa_debug
&= ~IFD_TRASHED
;
3766 ifa_lock_destroy(ifa
);
3767 bzero(ifa
, sizeof (struct in6_ifaddr
));
3768 zfree(in6ifa_zone
, ifa
);
3772 in6_ifaddr_attached(struct ifaddr
*ifa
)
3774 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3776 IFA_LOCK_ASSERT_HELD(ifa
);
3778 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3779 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3782 if (ifa
->ifa_debug
& IFD_TRASHED
) {
3783 /* Become a regular mutex, just in case */
3784 IFA_CONVERT_LOCK(ifa
);
3785 lck_mtx_lock(&in6ifa_trash_lock
);
3786 TAILQ_REMOVE(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
3787 lck_mtx_unlock(&in6ifa_trash_lock
);
3788 ifa
->ifa_debug
&= ~IFD_TRASHED
;
3793 in6_ifaddr_detached(struct ifaddr
*ifa
)
3795 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3797 IFA_LOCK_ASSERT_HELD(ifa
);
3799 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3800 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3802 } else if (ifa
->ifa_debug
& IFD_TRASHED
) {
3803 panic("%s: ifa %p is already in trash list", __func__
, ifa
);
3806 ifa
->ifa_debug
|= IFD_TRASHED
;
3807 /* Become a regular mutex, just in case */
3808 IFA_CONVERT_LOCK(ifa
);
3809 lck_mtx_lock(&in6ifa_trash_lock
);
3810 TAILQ_INSERT_TAIL(&in6ifa_trash_head
, in6ifa_dbg
, in6ifa_trash_link
);
3811 lck_mtx_unlock(&in6ifa_trash_lock
);
3815 in6_ifaddr_trace(struct ifaddr
*ifa
, int refhold
)
3817 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3822 if (!(ifa
->ifa_debug
& IFD_DEBUG
)) {
3823 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3827 cnt
= &in6ifa_dbg
->in6ifa_refhold_cnt
;
3828 tr
= in6ifa_dbg
->in6ifa_refhold
;
3830 cnt
= &in6ifa_dbg
->in6ifa_refrele_cnt
;
3831 tr
= in6ifa_dbg
->in6ifa_refrele
;
3834 idx
= atomic_add_16_ov(cnt
, 1) % IN6IFA_TRACE_HIST_SIZE
;
3835 ctrace_record(&tr
[idx
]);
3839 in6_ifaddr_set_dadprogress(struct in6_ifaddr
*ia
)
3841 uint32_t flags
= IN6_IFF_TENTATIVE
;
3842 uint32_t optdad
= nd6_optimistic_dad
;
3844 if (optdad
&& (ia
->ia_ifp
->if_eflags
& IFEF_IPV6_ROUTER
) == 0) {
3845 if ((optdad
& ND6_OPTIMISTIC_DAD_LINKLOCAL
) &&
3846 IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
))
3847 flags
= IN6_IFF_OPTIMISTIC
;
3848 else if ((optdad
& ND6_OPTIMISTIC_DAD_AUTOCONF
) &&
3849 (ia
->ia6_flags
& IN6_IFF_AUTOCONF
)) {
3850 if (ia
->ia6_flags
& IN6_IFF_TEMPORARY
) {
3851 if (optdad
& ND6_OPTIMISTIC_DAD_TEMPORARY
)
3852 flags
= IN6_IFF_OPTIMISTIC
;
3854 flags
= IN6_IFF_OPTIMISTIC
;
3856 } else if ((optdad
& ND6_OPTIMISTIC_DAD_DYNAMIC
) &&
3857 (ia
->ia6_flags
& IN6_IFF_DYNAMIC
)) {
3858 if (ia
->ia6_flags
& IN6_IFF_TEMPORARY
) {
3859 if (optdad
& ND6_OPTIMISTIC_DAD_TEMPORARY
)
3860 flags
= IN6_IFF_OPTIMISTIC
;
3862 flags
= IN6_IFF_OPTIMISTIC
;
3867 ia
->ia6_flags
&= ~(IN6_IFF_DUPLICATED
| IN6_IFF_DADPROGRESS
);
3868 ia
->ia6_flags
|= flags
;