2 * Copyright (c) 2003-2009 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>
110 #include <kern/locks.h>
111 #include <kern/zalloc.h>
112 #include <libkern/OSAtomic.h>
113 #include <machine/machine_routines.h>
116 #include <net/if_types.h>
117 #include <net/if_var.h>
118 #include <net/route.h>
119 #include <net/if_dl.h>
120 #include <net/kpi_protocol.h>
122 #include <netinet/in.h>
123 #include <netinet/in_var.h>
124 #include <netinet/if_ether.h>
125 #ifndef SCOPEDROUTING
126 #include <netinet/in_systm.h>
127 #include <netinet/ip.h>
128 #include <netinet/in_pcb.h>
131 #include <netinet6/nd6.h>
132 #include <netinet/ip6.h>
133 #include <netinet6/ip6_var.h>
134 #include <netinet6/mld6_var.h>
135 #include <netinet6/ip6_mroute.h>
136 #include <netinet6/in6_ifattach.h>
137 #include <netinet6/scope6_var.h>
138 #ifndef SCOPEDROUTING
139 #include <netinet6/in6_pcb.h>
142 #include <net/net_osdep.h>
145 #include <net/pfvar.h>
149 MALLOC_DEFINE(M_IPMADDR
, "in6_multi", "internet multicast address");
152 * Definitions of some costant IP6 addresses.
154 const struct in6_addr in6addr_any
= IN6ADDR_ANY_INIT
;
155 const struct in6_addr in6addr_loopback
= IN6ADDR_LOOPBACK_INIT
;
156 const struct in6_addr in6addr_nodelocal_allnodes
=
157 IN6ADDR_NODELOCAL_ALLNODES_INIT
;
158 const struct in6_addr in6addr_linklocal_allnodes
=
159 IN6ADDR_LINKLOCAL_ALLNODES_INIT
;
160 const struct in6_addr in6addr_linklocal_allrouters
=
161 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT
;
163 const struct in6_addr in6mask0
= IN6MASK0
;
164 const struct in6_addr in6mask32
= IN6MASK32
;
165 const struct in6_addr in6mask64
= IN6MASK64
;
166 const struct in6_addr in6mask96
= IN6MASK96
;
167 const struct in6_addr in6mask128
= IN6MASK128
;
169 const struct sockaddr_in6 sa6_any
= {sizeof(sa6_any
), AF_INET6
,
170 0, 0, IN6ADDR_ANY_INIT
, 0};
172 static int in6_lifaddr_ioctl(struct socket
*, u_long
, caddr_t
,
173 struct ifnet
*, struct proc
*);
174 static int in6_ifinit(struct ifnet
*, struct in6_ifaddr
*,
175 struct sockaddr_in6
*, int);
176 static void in6_unlink_ifa(struct in6_ifaddr
*, struct ifnet
*, int);
177 static struct in6_ifaddr
*in6_ifaddr_alloc(int);
178 static void in6_ifaddr_free(struct ifaddr
*);
179 static void in6_ifaddr_trace(struct ifaddr
*, int);
180 static struct in6_aliasreq
*in6_aliasreq_to_native(void *, int,
181 struct in6_aliasreq
*);
183 struct in6_multihead in6_multihead
; /* XXX BSS initialization */
184 extern lck_mtx_t
*nd6_mutex
;
185 extern lck_mtx_t
*ip6_mutex
;
186 extern int in6_init2done
;
188 struct in6_ifaddr_dbg
{
189 struct in6_ifaddr in6ifa
; /* in6_ifaddr */
190 struct in6_ifaddr in6ifa_old
; /* saved in6_ifaddr */
191 u_int16_t in6ifa_refhold_cnt
; /* # of ifaref */
192 u_int16_t in6ifa_refrele_cnt
; /* # of ifafree */
194 * Alloc and free callers.
196 ctrace_t in6ifa_alloc
;
197 ctrace_t in6ifa_free
;
199 * Circular lists of ifaref and ifafree callers.
201 ctrace_t in6ifa_refhold
[CTRACE_HIST_SIZE
];
202 ctrace_t in6ifa_refrele
[CTRACE_HIST_SIZE
];
205 static unsigned int in6ifa_debug
; /* debug flags */
206 static unsigned int in6ifa_size
; /* size of zone element */
207 static struct zone
*in6ifa_zone
; /* zone for in6_ifaddr */
209 #define IN6IFA_ZONE_MAX 64 /* maximum elements in zone */
210 #define IN6IFA_ZONE_NAME "in6_ifaddr" /* zone name */
213 * Subroutine for in6_ifaddloop() and in6_ifremloop().
214 * This routine does actual work.
217 in6_ifloop_request(int cmd
, struct ifaddr
*ifa
)
219 struct sockaddr_in6 all1_sa
;
220 struct rtentry
*nrt
= NULL
;
223 bzero(&all1_sa
, sizeof(all1_sa
));
224 all1_sa
.sin6_family
= AF_INET6
;
225 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
226 all1_sa
.sin6_addr
= in6mask128
;
229 * We specify the address itself as the gateway, and set the
230 * RTF_LLINFO flag, so that the corresponding host route would have
231 * the flag, and thus applications that assume traditional behavior
232 * would be happy. Note that we assume the caller of the function
233 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
234 * which changes the outgoing interface to the loopback interface.
236 lck_mtx_lock(rnh_lock
);
237 e
= rtrequest_locked(cmd
, ifa
->ifa_addr
, ifa
->ifa_addr
,
238 (struct sockaddr
*)&all1_sa
,
239 RTF_UP
|RTF_HOST
|RTF_LLINFO
, &nrt
);
241 log(LOG_ERR
, "in6_ifloop_request: "
242 "%s operation failed for %s (errno=%d)\n",
243 cmd
== RTM_ADD
? "ADD" : "DELETE",
244 ip6_sprintf(&((struct in6_ifaddr
*)ifa
)->ia_addr
.sin6_addr
),
251 * Make sure rt_ifa be equal to IFA, the second argument of the
253 * We need this because when we refer to rt_ifa->ia6_flags in
254 * ip6_input, we assume that the rt_ifa points to the address instead
255 * of the loopback address.
257 if (cmd
== RTM_ADD
&& nrt
&& ifa
!= nrt
->rt_ifa
) {
262 * Report the addition/removal of the address to the routing socket.
263 * XXX: since we called rtinit for a p2p interface with a destination,
264 * we end up reporting twice in such a case. Should we rather
265 * omit the second report?
268 rt_newaddrmsg(cmd
, ifa
, e
, nrt
);
269 if (cmd
== RTM_DELETE
) {
273 /* the cmd must be RTM_ADD here */
274 RT_REMREF_LOCKED(nrt
);
278 lck_mtx_unlock(rnh_lock
);
282 * Add ownaddr as loopback rtentry. We previously add the route only if
283 * necessary (ex. on a p2p link). However, since we now manage addresses
284 * separately from prefixes, we should always add the route. We can't
285 * rely on the cloning mechanism from the corresponding interface route
289 in6_ifaddloop(struct ifaddr
*ifa
)
293 /* If there is no loopback entry, allocate one. */
294 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
297 if (rt
== NULL
|| (rt
->rt_flags
& RTF_HOST
) == 0 ||
298 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) == 0) {
300 RT_REMREF_LOCKED(rt
);
303 in6_ifloop_request(RTM_ADD
, ifa
);
304 } else if (rt
!= NULL
) {
305 RT_REMREF_LOCKED(rt
);
311 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
315 in6_ifremloop(struct ifaddr
*ifa
, int locked
)
317 struct in6_ifaddr
*ia
;
322 * Some of BSD variants do not remove cloned routes
323 * from an interface direct route, when removing the direct route
324 * (see comments in net/net_osdep.h). Even for variants that do remove
325 * cloned routes, they could fail to remove the cloned routes when
326 * we handle multple addresses that share a common prefix.
327 * So, we should remove the route corresponding to the deleted address
328 * regardless of the result of in6_is_ifloop_auto().
332 * Delete the entry only if exact one ifa exists. More than one ifa
333 * can exist if we assign a same single address to multiple
334 * (probably p2p) interfaces.
335 * XXX: we should avoid such a configuration in IPv6...
338 lck_mtx_lock(nd6_mutex
);
339 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
340 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa
), &ia
->ia_addr
.sin6_addr
)) {
347 lck_mtx_unlock(nd6_mutex
);
351 * Before deleting, check if a corresponding loopbacked host
352 * route surely exists. With this check, we can avoid to
353 * delete an interface direct route whose destination is same
354 * as the address being removed. This can happen when remofing
355 * a subnet-router anycast address on an interface attahced
356 * to a shared medium.
358 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
361 if ((rt
->rt_flags
& RTF_HOST
) != 0 &&
362 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
363 RT_REMREF_LOCKED(rt
);
365 in6_ifloop_request(RTM_DELETE
, ifa
);
376 in6_ifindex2scopeid(idx
)
381 struct sockaddr_in6
*sin6
;
383 ifnet_head_lock_shared();
384 if (idx
<= 0 || if_index
< idx
) {
389 ifp
= ifindex2ifnet
[idx
];
392 ifnet_lock_shared(ifp
);
393 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
395 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
397 sin6
= (struct sockaddr_in6
*)ifa
->ifa_addr
;
398 if (IN6_IS_ADDR_SITELOCAL(&sin6
->sin6_addr
)) {
399 int scopeid
= sin6
->sin6_scope_id
& 0xffff;
400 ifnet_lock_done(ifp
);
404 ifnet_lock_done(ifp
);
412 in6_mask2len(mask
, lim0
)
413 struct in6_addr
*mask
;
417 u_char
*lim
= lim0
, *p
;
420 lim0
- (u_char
*)mask
> sizeof(*mask
)) /* ignore the scope_id part */
421 lim
= (u_char
*)mask
+ sizeof(*mask
);
422 for (p
= (u_char
*)mask
; p
< lim
; x
++, p
++) {
428 for (y
= 0; y
< 8; y
++) {
429 if ((*p
& (0x80 >> y
)) == 0)
435 * when the limit pointer is given, do a stricter check on the
439 if (y
!= 0 && (*p
& (0x00ff >> y
)) != 0)
441 for (p
= p
+ 1; p
< lim
; p
++)
450 in6_len2mask(mask
, len
)
451 struct in6_addr
*mask
;
456 bzero(mask
, sizeof(*mask
));
457 for (i
= 0; i
< len
/ 8; i
++)
458 mask
->s6_addr8
[i
] = 0xff;
460 mask
->s6_addr8
[i
] = (0xff00 >> (len
% 8)) & 0xff;
464 in6_aliasreq_64_to_32(struct in6_aliasreq_64
*src
, struct in6_aliasreq_32
*dst
)
466 bzero(dst
, sizeof (*dst
));
467 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof (dst
->ifra_name
));
468 dst
->ifra_addr
= src
->ifra_addr
;
469 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
470 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
471 dst
->ifra_flags
= src
->ifra_flags
;
472 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
473 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
474 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
475 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
479 in6_aliasreq_32_to_64(struct in6_aliasreq_32
*src
, struct in6_aliasreq_64
*dst
)
481 bzero(dst
, sizeof (*dst
));
482 bcopy(src
->ifra_name
, dst
->ifra_name
, sizeof (dst
->ifra_name
));
483 dst
->ifra_addr
= src
->ifra_addr
;
484 dst
->ifra_dstaddr
= src
->ifra_dstaddr
;
485 dst
->ifra_prefixmask
= src
->ifra_prefixmask
;
486 dst
->ifra_flags
= src
->ifra_flags
;
487 dst
->ifra_lifetime
.ia6t_expire
= src
->ifra_lifetime
.ia6t_expire
;
488 dst
->ifra_lifetime
.ia6t_preferred
= src
->ifra_lifetime
.ia6t_preferred
;
489 dst
->ifra_lifetime
.ia6t_vltime
= src
->ifra_lifetime
.ia6t_vltime
;
490 dst
->ifra_lifetime
.ia6t_pltime
= src
->ifra_lifetime
.ia6t_pltime
;
493 static struct in6_aliasreq
*
494 in6_aliasreq_to_native(void *data
, int data_is_64
, struct in6_aliasreq
*dst
)
496 #if defined(__LP64__)
500 in6_aliasreq_32_to_64((struct in6_aliasreq_32
*)data
,
501 (struct in6_aliasreq_64
*)dst
);
504 in6_aliasreq_64_to_32((struct in6_aliasreq_64
*)data
,
505 (struct in6_aliasreq_32
*)dst
);
508 #endif /* __LP64__ */
512 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
513 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
516 in6_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
,
519 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)data
;
520 struct in6_ifaddr
*ia
= NULL
;
521 struct in6_aliasreq sifra
;
522 struct in6_aliasreq
*ifra
= NULL
;
523 struct sockaddr_in6
*sa6
;
524 int index
, privileged
, error
= 0;
525 struct timeval timenow
;
526 int p64
= proc_is64bit(p
);
528 getmicrotime(&timenow
);
530 privileged
= (proc_suser(p
) == 0);
533 case SIOCGETSGCNT_IN6
:
534 case SIOCGETMIFCNT_IN6_32
:
535 case SIOCGETMIFCNT_IN6_64
:
536 return (mrt6_ioctl(cmd
, data
));
543 case SIOCAUTOCONF_START
:
544 case SIOCAUTOCONF_STOP
:
545 case SIOCLL_START_32
:
546 case SIOCLL_START_64
:
548 case SIOCPROTOATTACH_IN6_32
:
549 case SIOCPROTOATTACH_IN6_64
:
550 case SIOCPROTODETACH_IN6
:
554 case SIOCSNDFLUSH_IN6
:
555 case SIOCSPFXFLUSH_IN6
:
556 case SIOCSRTRFLUSH_IN6
:
557 case SIOCSDEFIFACE_IN6_32
:
558 case SIOCSDEFIFACE_IN6_64
:
559 case SIOCSIFINFO_FLAGS
:
563 case OSIOCGIFINFO_IN6
:
564 case SIOCGIFINFO_IN6
:
565 case SIOCGDRLST_IN6_32
:
566 case SIOCGDRLST_IN6_64
:
567 case SIOCGPRLST_IN6_32
:
568 case SIOCGPRLST_IN6_64
:
569 case SIOCGNBRINFO_IN6_32
:
570 case SIOCGNBRINFO_IN6_64
:
571 case SIOCGDEFIFACE_IN6_32
:
572 case SIOCGDEFIFACE_IN6_64
:
573 return (nd6_ioctl(cmd
, data
, ifp
));
577 case SIOCSIFPREFIX_IN6
:
578 case SIOCDIFPREFIX_IN6
:
579 case SIOCAIFPREFIX_IN6
:
580 case SIOCCIFPREFIX_IN6
:
581 case SIOCSGIFPREFIX_IN6
:
582 case SIOCGIFPREFIX_IN6
:
584 "prefix ioctls are now invalidated. "
585 "please use ifconfig.\n");
593 return (scope6_set(ifp
, ifr
->ifr_ifru
.ifru_scope_id
));
597 return (scope6_get(ifp
, ifr
->ifr_ifru
.ifru_scope_id
));
601 return (scope6_get_default(ifr
->ifr_ifru
.ifru_scope_id
));
611 return (in6_lifaddr_ioctl(so
, cmd
, data
, ifp
, p
));
615 * Point ifra and sa6 to the right places depending on the command.
618 case SIOCLL_START_32
:
619 case SIOCAIFADDR_IN6_32
:
621 * Convert user ifra to the kernel form, when appropriate.
622 * This allows the conversion between different data models
623 * to be centralized, so that it can be passed around to other
624 * routines that are expecting the kernel form.
626 ifra
= in6_aliasreq_to_native(data
, 0, &sifra
);
627 sa6
= (struct sockaddr_in6
*)&ifra
->ifra_addr
;
630 case SIOCLL_START_64
:
631 case SIOCAIFADDR_IN6_64
:
632 ifra
= in6_aliasreq_to_native(data
, 1, &sifra
);
633 sa6
= (struct sockaddr_in6
*)&ifra
->ifra_addr
;
636 case SIOCSIFADDR_IN6
: /* deprecated */
637 case SIOCGIFADDR_IN6
:
638 case SIOCSIFDSTADDR_IN6
: /* deprecated */
639 case SIOCSIFNETMASK_IN6
: /* deprecated */
640 case SIOCGIFDSTADDR_IN6
:
641 case SIOCGIFNETMASK_IN6
:
642 case SIOCDIFADDR_IN6
:
643 case SIOCGIFPSRCADDR_IN6
:
644 case SIOCGIFPDSTADDR_IN6
:
645 case SIOCGIFAFLAG_IN6
:
646 case SIOCGIFALIFETIME_IN6
:
647 case SIOCSIFALIFETIME_IN6
:
648 case SIOCGIFSTAT_IN6
:
649 case SIOCGIFSTAT_ICMP6
:
650 sa6
= &ifr
->ifr_addr
;
660 case SIOCAUTOCONF_START
:
661 ifnet_lock_exclusive(ifp
);
662 ifp
->if_eflags
|= IFEF_ACCEPT_RTADVD
;
663 ifnet_lock_done(ifp
);
667 case SIOCAUTOCONF_STOP
: {
668 struct in6_ifaddr
*nia
= NULL
;
670 ifnet_lock_exclusive(ifp
);
671 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADVD
;
672 ifnet_lock_done(ifp
);
674 /* nuke prefix list. this may try to remove some ifaddrs as well */
675 in6_purgeprefix(ifp
);
677 /* removed autoconfigured address from interface */
678 lck_mtx_lock(nd6_mutex
);
679 for (ia
= in6_ifaddrs
; ia
!= NULL
; ia
= nia
) {
681 if (ia
->ia_ifa
.ifa_ifp
!= ifp
)
683 if (ia
->ia6_flags
& IN6_IFF_AUTOCONF
)
684 in6_purgeaddr(&ia
->ia_ifa
, 1);
686 lck_mtx_unlock(nd6_mutex
);
690 case SIOCLL_START_32
:
691 case SIOCLL_START_64
:
693 * NOTE: All the interface specific DLIL attachements should
694 * be done here. They are currently done in in6_ifattach()
695 * for the interfaces that need it.
697 if (((ifp
->if_type
== IFT_PPP
) || ((ifp
->if_eflags
& IFEF_NOAUTOIPV6LL
) != 0)) &&
698 ifra
->ifra_addr
.sin6_family
== AF_INET6
&&
699 ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
) {
700 /* some interfaces may provide LinkLocal addresses */
701 error
= in6_if_up(ifp
, ifra
);
703 error
= in6_if_up(ifp
, 0);
709 struct in6_ifaddr
*nia
= NULL
;
711 /* removed link local addresses from interface */
713 lck_mtx_lock(nd6_mutex
);
714 for (ia
= in6_ifaddrs
; ia
!= NULL
; ia
= nia
) {
716 if (ia
->ia_ifa
.ifa_ifp
!= ifp
)
718 if (IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
))
719 in6_purgeaddr(&ia
->ia_ifa
, 1);
721 lck_mtx_unlock(nd6_mutex
);
725 case SIOCPROTOATTACH_IN6_32
:
726 case SIOCPROTOATTACH_IN6_64
:
727 if ((error
= proto_plumb(PF_INET6
, ifp
)))
728 printf("SIOCPROTOATTACH_IN6: %s "
729 "error=%d\n", if_name(ifp
), error
);
733 case SIOCPROTODETACH_IN6
:
734 /* Cleanup interface routes and addresses */
737 if ((error
= proto_unplumb(PF_INET6
, ifp
)))
738 printf("SIOCPROTODETACH_IN6: %s error=%d\n",
739 if_name(ifp
), error
);
744 * Find address for this interface, if it exists; depending
745 * on the ioctl command, sa6 points to the address in ifra/ifr.
747 if (sa6
!= NULL
&& sa6
->sin6_family
== AF_INET6
) {
748 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
749 if (sa6
->sin6_addr
.s6_addr16
[1] == 0) {
750 /* link ID is not embedded by the user */
751 sa6
->sin6_addr
.s6_addr16
[1] =
752 htons(ifp
->if_index
);
753 } else if (sa6
->sin6_addr
.s6_addr16
[1] !=
754 htons(ifp
->if_index
)) {
755 return (EINVAL
); /* link ID contradicts */
757 if (sa6
->sin6_scope_id
) {
758 if (sa6
->sin6_scope_id
!=
759 (u_int32_t
)ifp
->if_index
)
761 sa6
->sin6_scope_id
= 0; /* XXX: good way? */
764 ia
= in6ifa_ifpwithaddr(ifp
, &sa6
->sin6_addr
);
770 case SIOCSIFADDR_IN6
:
771 case SIOCSIFDSTADDR_IN6
:
772 case SIOCSIFNETMASK_IN6
:
774 * Since IPv6 allows a node to assign multiple addresses
775 * on a single interface, SIOCSIFxxx ioctls are not suitable
776 * and should be unused.
778 /* we decided to obsolete this command (20000704) */
782 case SIOCDIFADDR_IN6
:
784 * for IPv4, we look for existing in_ifaddr here to allow
785 * "ifconfig if0 delete" to remove first IPv4 address on the
786 * interface. For IPv6, as the spec allow multiple interface
787 * address from the day one, we consider "remove the first one"
788 * semantics to be not preferable.
791 error
= EADDRNOTAVAIL
;
795 case SIOCAIFADDR_IN6_32
:
796 case SIOCAIFADDR_IN6_64
:
798 * We always require users to specify a valid IPv6 address for
799 * the corresponding operation. Use "sa6" instead of "ifra"
800 * since SIOCDIFADDR_IN6 falls thru above.
802 if (sa6
->sin6_family
!= AF_INET6
||
803 sa6
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
804 error
= EAFNOSUPPORT
;
814 case SIOCGIFADDR_IN6
:
815 /* This interface is basically deprecated. use SIOCGIFCONF. */
817 case SIOCGIFAFLAG_IN6
:
818 case SIOCGIFNETMASK_IN6
:
819 case SIOCGIFDSTADDR_IN6
:
820 case SIOCGIFALIFETIME_IN6
:
821 /* must think again about its semantics */
823 error
= EADDRNOTAVAIL
;
828 case SIOCSIFALIFETIME_IN6
:
834 error
= EADDRNOTAVAIL
;
837 /* sanity for overflow - beware unsigned */
839 struct in6_addrlifetime_64
*lt
;
841 lt
= (struct in6_addrlifetime_64
*)
842 &ifr
->ifr_ifru
.ifru_lifetime
;
843 if (lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
844 && lt
->ia6t_vltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
848 if (lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
849 && lt
->ia6t_pltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
854 struct in6_addrlifetime_32
*lt
;
856 lt
= (struct in6_addrlifetime_32
*)
857 &ifr
->ifr_ifru
.ifru_lifetime
;
858 if (lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
859 && lt
->ia6t_vltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
863 if (lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
864 && lt
->ia6t_pltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
873 case SIOCGIFADDR_IN6
:
874 ifr
->ifr_addr
= ia
->ia_addr
;
877 case SIOCGIFDSTADDR_IN6
:
878 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0) {
883 * XXX: should we check if ifa_dstaddr is NULL and return
886 ifr
->ifr_dstaddr
= ia
->ia_dstaddr
;
889 case SIOCGIFNETMASK_IN6
:
890 ifr
->ifr_addr
= ia
->ia_prefixmask
;
893 case SIOCGIFAFLAG_IN6
:
894 ifr
->ifr_ifru
.ifru_flags6
= ia
->ia6_flags
;
897 case SIOCGIFSTAT_IN6
:
902 index
= ifp
->if_index
;
903 lck_mtx_lock(ip6_mutex
);
904 if (in6_ifstat
== NULL
|| index
>= in6_ifstatmax
905 || in6_ifstat
[index
] == NULL
) {
906 /* return EAFNOSUPPORT? */
907 bzero(&ifr
->ifr_ifru
.ifru_stat
,
908 sizeof (ifr
->ifr_ifru
.ifru_stat
));
910 ifr
->ifr_ifru
.ifru_stat
= *in6_ifstat
[index
];
912 lck_mtx_unlock(ip6_mutex
);
915 case SIOCGIFSTAT_ICMP6
:
920 index
= ifp
->if_index
;
921 lck_mtx_lock(ip6_mutex
);
922 if (icmp6_ifstat
== NULL
|| index
>= icmp6_ifstatmax
||
923 icmp6_ifstat
[index
] == NULL
) {
924 /* return EAFNOSUPPORT? */
925 bzero(&ifr
->ifr_ifru
.ifru_stat
,
926 sizeof (ifr
->ifr_ifru
.ifru_icmp6stat
));
928 ifr
->ifr_ifru
.ifru_icmp6stat
= *icmp6_ifstat
[index
];
930 lck_mtx_unlock(ip6_mutex
);
933 case SIOCGIFALIFETIME_IN6
:
935 struct in6_addrlifetime_64
*lt
;
937 lt
= (struct in6_addrlifetime_64
*)
938 &ifr
->ifr_ifru
.ifru_lifetime
;
939 lt
->ia6t_expire
= ia
->ia6_lifetime
.ia6t_expire
;
940 lt
->ia6t_preferred
= ia
->ia6_lifetime
.ia6t_preferred
;
941 lt
->ia6t_vltime
= ia
->ia6_lifetime
.ia6t_vltime
;
942 lt
->ia6t_pltime
= ia
->ia6_lifetime
.ia6t_pltime
;
944 struct in6_addrlifetime_32
*lt
;
946 lt
= (struct in6_addrlifetime_32
*)
947 &ifr
->ifr_ifru
.ifru_lifetime
;
949 (uint32_t)ia
->ia6_lifetime
.ia6t_expire
;
951 (uint32_t)ia
->ia6_lifetime
.ia6t_preferred
;
953 (uint32_t)ia
->ia6_lifetime
.ia6t_vltime
;
955 (uint32_t)ia
->ia6_lifetime
.ia6t_pltime
;
959 case SIOCSIFALIFETIME_IN6
:
961 struct in6_addrlifetime_64
*lt
;
963 lt
= (struct in6_addrlifetime_64
*)
964 &ifr
->ifr_ifru
.ifru_lifetime
;
965 ia
->ia6_lifetime
.ia6t_expire
= lt
->ia6t_expire
;
966 ia
->ia6_lifetime
.ia6t_preferred
= lt
->ia6t_preferred
;
967 ia
->ia6_lifetime
.ia6t_vltime
= lt
->ia6t_vltime
;
968 ia
->ia6_lifetime
.ia6t_pltime
= lt
->ia6t_pltime
;
970 struct in6_addrlifetime_32
*lt
;
972 lt
= (struct in6_addrlifetime_32
*)
973 &ifr
->ifr_ifru
.ifru_lifetime
;
974 ia
->ia6_lifetime
.ia6t_expire
=
975 (uint32_t)lt
->ia6t_expire
;
976 ia
->ia6_lifetime
.ia6t_preferred
=
977 (uint32_t)lt
->ia6t_preferred
;
978 ia
->ia6_lifetime
.ia6t_vltime
= lt
->ia6t_vltime
;
979 ia
->ia6_lifetime
.ia6t_pltime
= lt
->ia6t_pltime
;
982 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
983 ia
->ia6_lifetime
.ia6t_expire
=
984 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_vltime
;
986 ia
->ia6_lifetime
.ia6t_expire
= 0;
987 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
988 ia
->ia6_lifetime
.ia6t_preferred
=
989 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_pltime
;
991 ia
->ia6_lifetime
.ia6t_preferred
= 0;
994 case SIOCAIFADDR_IN6_32
:
995 case SIOCAIFADDR_IN6_64
: {
997 struct nd_prefix pr0
, *pr
;
999 /* Attempt to attache the protocol, in case it isn't attached */
1000 error
= proto_plumb(PF_INET6
, ifp
);
1002 if (error
!= EEXIST
) {
1003 printf("SIOCAIFADDR_IN6: %s can't plumb "
1004 "protocol error=%d\n", if_name(ifp
), error
);
1008 /* Ignore, EEXIST */
1011 /* PF_INET6 wasn't previously attached */
1012 if ((error
= in6_if_up(ifp
, NULL
)) != 0)
1017 * first, make or update the interface address structure,
1018 * and link it to the list.
1020 if ((error
= in6_update_ifa(ifp
, ifra
, ia
, M_WAITOK
)) != 0)
1024 * then, make the prefix on-link on the interface.
1025 * XXX: we'd rather create the prefix before the address, but
1026 * we need at least one address to install the corresponding
1027 * interface route, so we configure the address first.
1031 * convert mask to prefix length (prefixmask has already
1032 * been validated in in6_update_ifa().
1034 bzero(&pr0
, sizeof(pr0
));
1036 pr0
.ndpr_plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
1038 if (pr0
.ndpr_plen
== 128)
1039 break; /* we don't need to install a host route. */
1040 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
1041 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
1042 /* apply the mask for safety. */
1043 for (i
= 0; i
< 4; i
++) {
1044 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1045 ifra
->ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
1048 * XXX: since we don't have an API to set prefix (not address)
1049 * lifetimes, we just use the same lifetimes as addresses.
1050 * The (temporarily) installed lifetimes can be overridden by
1051 * later advertised RAs (when accept_rtadv is non 0), which is
1052 * an intended behavior.
1054 pr0
.ndpr_raf_onlink
= 1; /* should be configurable? */
1056 ((ifra
->ifra_flags
& IN6_IFF_AUTOCONF
) != 0);
1057 pr0
.ndpr_vltime
= ifra
->ifra_lifetime
.ia6t_vltime
;
1058 pr0
.ndpr_pltime
= ifra
->ifra_lifetime
.ia6t_pltime
;
1060 /* add the prefix if there's one. */
1061 if ((pr
= nd6_prefix_lookup(&pr0
)) == NULL
) {
1063 * nd6_prelist_add will install the corresponding
1066 if ((error
= nd6_prelist_add(&pr0
, NULL
, &pr
)) != 0)
1069 log(LOG_ERR
, "nd6_prelist_add succedded but "
1076 ifafree(&ia
->ia_ifa
);
1077 if ((ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
))
1079 /* XXX: this should not happen! */
1080 log(LOG_ERR
, "in6_control: addition succeeded, but"
1083 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
1084 ia
->ia6_ndpr
== NULL
) { /* new autoconfed addr */
1085 lck_mtx_lock(nd6_mutex
);
1087 lck_mtx_unlock(nd6_mutex
);
1091 * If this is the first autoconf address from
1092 * the prefix, create a temporary address
1093 * as well (when specified).
1095 if (ip6_use_tempaddr
&&
1096 pr
->ndpr_refcnt
== 1) {
1098 if ((e
= in6_tmpifadd(ia
, 1,
1100 log(LOG_NOTICE
, "in6_control: "
1101 "failed to create a "
1102 "temporary address, "
1110 * this might affect the status of autoconfigured
1111 * addresses, that is, this address might make
1112 * other addresses detached.
1114 pfxlist_onlink_check(0);
1117 /* Drop use count held above during lookup/add */
1118 ndpr_rele(pr
, FALSE
);
1120 pf_ifaddr_hook(ifp
, cmd
);
1125 case SIOCDIFADDR_IN6
: {
1127 struct nd_prefix pr0
, *pr
;
1130 * If the address being deleted is the only one that owns
1131 * the corresponding prefix, expire the prefix as well.
1132 * XXX: theoretically, we don't have to warry about such
1133 * relationship, since we separate the address management
1134 * and the prefix management. We do this, however, to provide
1135 * as much backward compatibility as possible in terms of
1136 * the ioctl operation.
1138 bzero(&pr0
, sizeof(pr0
));
1140 pr0
.ndpr_plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
1142 if (pr0
.ndpr_plen
== 128)
1144 pr0
.ndpr_prefix
= ia
->ia_addr
;
1145 pr0
.ndpr_mask
= ia
->ia_prefixmask
.sin6_addr
;
1146 for (i
= 0; i
< 4; i
++) {
1147 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1148 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
];
1151 * The logic of the following condition is a bit complicated.
1152 * We expire the prefix when
1153 * 1. the address obeys autoconfiguration and it is the
1154 * only owner of the associated prefix, or
1155 * 2. the address does not obey autoconf and there is no
1156 * other owner of the prefix.
1158 if ((pr
= nd6_prefix_lookup(&pr0
)) != NULL
&&
1159 (((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
1160 pr
->ndpr_refcnt
== 1) ||
1161 ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 &&
1162 pr
->ndpr_refcnt
== 0))) {
1163 pr
->ndpr_expire
= 1; /* XXX: just for expiration */
1166 /* Drop use count held above during lookup */
1168 ndpr_rele(pr
, FALSE
);
1171 in6_purgeaddr(&ia
->ia_ifa
, 0);
1173 pf_ifaddr_hook(ifp
, cmd
);
1179 error
= ifnet_ioctl(ifp
, PF_INET6
, cmd
, data
);
1184 ifafree(&ia
->ia_ifa
);
1189 * Update parameters of an IPv6 interface address.
1190 * If necessary, a new entry is created and linked into address chains.
1191 * This function is separated from in6_control().
1192 * XXX: should this be performed under splnet()?
1195 in6_update_ifa(ifp
, ifra
, ia
, how
)
1197 struct in6_aliasreq
*ifra
;
1198 struct in6_ifaddr
*ia
;
1201 int error
= 0, hostIsNew
= 0, plen
= -1;
1202 struct in6_ifaddr
*oia
;
1203 struct sockaddr_in6 dst6
;
1204 struct in6_addrlifetime
*lt
;
1205 struct timeval timenow
;
1208 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1209 /* Validate parameters */
1210 if (ifp
== NULL
|| ifra
== NULL
) /* this maybe redundant */
1214 * The destination address for a p2p link must have a family
1215 * of AF_UNSPEC or AF_INET6.
1217 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
1218 ifra
->ifra_dstaddr
.sin6_family
!= AF_INET6
&&
1219 ifra
->ifra_dstaddr
.sin6_family
!= AF_UNSPEC
)
1220 return(EAFNOSUPPORT
);
1222 * validate ifra_prefixmask. don't check sin6_family, netmask
1223 * does not carry fields other than sin6_len.
1225 if (ifra
->ifra_prefixmask
.sin6_len
> sizeof(struct sockaddr_in6
))
1228 * Set the address family value for the mask if it was not set.
1231 if (ifra
->ifra_prefixmask
.sin6_len
== sizeof(struct sockaddr_in6
) &&
1232 ifra
->ifra_prefixmask
.sin6_family
== 0) {
1233 ifra
->ifra_prefixmask
.sin6_family
= AF_INET6
;
1236 * Because the IPv6 address architecture is classless, we require
1237 * users to specify a (non 0) prefix length (mask) for a new address.
1238 * We also require the prefix (when specified) mask is valid, and thus
1239 * reject a non-consecutive mask.
1241 if (ia
== NULL
&& ifra
->ifra_prefixmask
.sin6_len
== 0)
1243 if (ifra
->ifra_prefixmask
.sin6_len
!= 0) {
1244 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
1245 (u_char
*)&ifra
->ifra_prefixmask
+
1246 ifra
->ifra_prefixmask
.sin6_len
);
1252 * In this case, ia must not be NULL. We just use its prefix
1255 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
);
1258 * If the destination address on a p2p interface is specified,
1259 * and the address is a scoped one, validate/set the scope
1262 dst6
= ifra
->ifra_dstaddr
;
1263 if ((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) &&
1264 (dst6
.sin6_family
== AF_INET6
)) {
1267 #ifndef SCOPEDROUTING
1268 if ((error
= in6_recoverscope(&dst6
,
1269 &ifra
->ifra_dstaddr
.sin6_addr
,
1273 scopeid
= in6_addr2scopeid(ifp
, &dst6
.sin6_addr
);
1274 if (dst6
.sin6_scope_id
== 0) /* user omit to specify the ID. */
1275 dst6
.sin6_scope_id
= scopeid
;
1276 else if (dst6
.sin6_scope_id
!= scopeid
)
1277 return(EINVAL
); /* scope ID mismatch. */
1278 #ifndef SCOPEDROUTING
1279 if ((error
= in6_embedscope(&dst6
.sin6_addr
, &dst6
, NULL
, NULL
))
1282 dst6
.sin6_scope_id
= 0; /* XXX */
1286 * The destination address can be specified only for a p2p or a
1287 * loopback interface. If specified, the corresponding prefix length
1290 if (ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
) {
1291 if ((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) == 0) {
1292 /* XXX: noisy message */
1293 log(LOG_INFO
, "in6_update_ifa: a destination can be "
1294 "specified for a p2p or a loopback IF only\n");
1299 * The following message seems noisy, but we dare to
1300 * add it for diagnosis.
1302 log(LOG_INFO
, "in6_update_ifa: prefixlen must be 128 "
1303 "when dstaddr is specified\n");
1307 /* lifetime consistency check */
1309 getmicrotime(&timenow
);
1310 lt
= &ifra
->ifra_lifetime
;
1311 if (lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
1312 && lt
->ia6t_vltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
1315 if (lt
->ia6t_vltime
== 0) {
1317 * the following log might be noisy, but this is a typical
1318 * configuration mistake or a tool's bug.
1321 "in6_update_ifa: valid lifetime is 0 for %s\n",
1322 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
1324 if (lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
1325 && lt
->ia6t_pltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
1330 * If this is a new address, allocate a new ifaddr and link it
1336 * in6_update_ifa() may be called in a process of a received
1337 * RA; in such a case, we should call malloc with M_NOWAIT.
1338 * The exception to this is during init time or as part of
1339 * handling an ioctl, when we know it's okay to do M_WAITOK.
1341 ia
= in6_ifaddr_alloc(how
);
1344 /* Initialize the address and masks */
1345 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
1346 ia
->ia_addr
.sin6_family
= AF_INET6
;
1347 ia
->ia_addr
.sin6_len
= sizeof(ia
->ia_addr
);
1348 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) != 0) {
1350 * XXX: some functions expect that ifa_dstaddr is not
1351 * NULL for p2p interfaces.
1353 ia
->ia_ifa
.ifa_dstaddr
1354 = (struct sockaddr
*)&ia
->ia_dstaddr
;
1356 ia
->ia_ifa
.ifa_dstaddr
= NULL
;
1358 ia
->ia_ifa
.ifa_netmask
1359 = (struct sockaddr
*)&ia
->ia_prefixmask
;
1362 ifaref(&ia
->ia_ifa
);
1363 lck_mtx_lock(nd6_mutex
);
1364 if ((oia
= in6_ifaddrs
) != NULL
) {
1365 for ( ; oia
->ia_next
; oia
= oia
->ia_next
)
1370 lck_mtx_unlock(nd6_mutex
);
1372 ifnet_lock_exclusive(ifp
);
1373 if_attach_ifa(ifp
, &ia
->ia_ifa
);
1374 ifnet_lock_done(ifp
);
1377 /* set prefix mask */
1378 if (ifra
->ifra_prefixmask
.sin6_len
) {
1380 * We prohibit changing the prefix length of an existing
1382 * + such an operation should be rare in IPv6, and
1383 * + the operation would confuse prefix management.
1385 if (ia
->ia_prefixmask
.sin6_len
&&
1386 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
) != plen
) {
1387 log(LOG_INFO
, "in6_update_ifa: the prefix length of an"
1388 " existing (%s) address should not be changed\n",
1389 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1393 ia
->ia_prefixmask
= ifra
->ifra_prefixmask
;
1397 * If a new destination address is specified, scrub the old one and
1398 * install the new destination. Note that the interface must be
1399 * p2p or loopback (see the check above.)
1401 if (dst6
.sin6_family
== AF_INET6
&&
1402 !IN6_ARE_ADDR_EQUAL(&dst6
.sin6_addr
,
1403 &ia
->ia_dstaddr
.sin6_addr
)) {
1406 if ((ia
->ia_flags
& IFA_ROUTE
) != 0 &&
1407 (e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
))
1409 log(LOG_ERR
, "in6_update_ifa: failed to remove "
1410 "a route to the old destination: %s\n",
1411 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1412 /* proceed anyway... */
1415 ia
->ia_flags
&= ~IFA_ROUTE
;
1416 ia
->ia_dstaddr
= dst6
;
1419 /* reset the interface and routing table appropriately. */
1420 if ((error
= in6_ifinit(ifp
, ia
, &ifra
->ifra_addr
, hostIsNew
)) != 0)
1424 * Beyond this point, we should call in6_purgeaddr upon an error,
1425 * not just go to unlink.
1428 #if 0 /* disable this mechanism for now */
1429 /* update prefix list */
1431 (ifra
->ifra_flags
& IN6_IFF_NOPFX
) == 0) { /* XXX */
1434 iilen
= (sizeof(ia
->ia_prefixmask
.sin6_addr
) << 3) - plen
;
1435 if ((error
= in6_prefix_add_ifid(iilen
, ia
)) != 0) {
1436 in6_purgeaddr((struct ifaddr
*)ia
, 0);
1442 if ((ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1443 struct sockaddr_in6 mltaddr
, mltmask
;
1444 struct in6_multi
*in6m
;
1448 * join solicited multicast addr for new host id
1450 struct in6_addr llsol
;
1451 bzero(&llsol
, sizeof(struct in6_addr
));
1452 llsol
.s6_addr16
[0] = htons(0xff02);
1453 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1454 llsol
.s6_addr32
[1] = 0;
1455 llsol
.s6_addr32
[2] = htonl(1);
1456 llsol
.s6_addr32
[3] =
1457 ifra
->ifra_addr
.sin6_addr
.s6_addr32
[3];
1458 llsol
.s6_addr8
[12] = 0xff;
1459 (void)in6_addmulti(&llsol
, ifp
, &error
, 0);
1462 "in6_update_ifa: addmulti failed for "
1463 "%s on %s (errno=%d)\n",
1464 ip6_sprintf(&llsol
), if_name(ifp
),
1466 in6_purgeaddr((struct ifaddr
*)ia
, 0);
1471 bzero(&mltmask
, sizeof(mltmask
));
1472 mltmask
.sin6_len
= sizeof(struct sockaddr_in6
);
1473 mltmask
.sin6_family
= AF_INET6
;
1474 mltmask
.sin6_addr
= in6mask32
;
1477 * join link-local all-nodes address
1479 bzero(&mltaddr
, sizeof(mltaddr
));
1480 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
1481 mltaddr
.sin6_family
= AF_INET6
;
1482 mltaddr
.sin6_addr
= in6addr_linklocal_allnodes
;
1483 mltaddr
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
1485 ifnet_lock_shared(ifp
);
1486 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1487 ifnet_lock_done(ifp
);
1490 (struct sockaddr
*)&mltaddr
,
1491 (struct sockaddr
*)&ia
->ia_addr
,
1492 (struct sockaddr
*)&mltmask
,
1493 RTF_UP
|RTF_CLONING
, /* xxx */
1494 (struct rtentry
**)0);
1495 (void)in6_addmulti(&mltaddr
.sin6_addr
, ifp
, &error
, 0);
1498 "in6_update_ifa: addmulti failed for "
1499 "%s on %s (errno=%d)\n",
1500 ip6_sprintf(&mltaddr
.sin6_addr
),
1501 if_name(ifp
), error
);
1506 * join node information group address
1508 #define hostnamelen strlen(hostname)
1509 if (in6_nigroup(ifp
, hostname
, hostnamelen
, &mltaddr
.sin6_addr
)
1511 ifnet_lock_shared(ifp
);
1512 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1513 ifnet_lock_done(ifp
);
1514 if (in6m
== NULL
&& ia
!= NULL
) {
1515 (void)in6_addmulti(&mltaddr
.sin6_addr
,
1518 log(LOG_WARNING
, "in6_update_ifa: "
1519 "addmulti failed for "
1520 "%s on %s (errno=%d)\n",
1521 ip6_sprintf(&mltaddr
.sin6_addr
),
1522 if_name(ifp
), error
);
1529 * join node-local all-nodes address, on loopback.
1530 * XXX: since "node-local" is obsoleted by interface-local,
1531 * we have to join the group on every interface with
1532 * some interface-boundary restriction.
1534 if (ifp
->if_flags
& IFF_LOOPBACK
) {
1535 struct in6_ifaddr
*ia_loop
;
1537 struct in6_addr loop6
= in6addr_loopback
;
1538 ia_loop
= in6ifa_ifpwithaddr(ifp
, &loop6
);
1540 mltaddr
.sin6_addr
= in6addr_nodelocal_allnodes
;
1542 ifnet_lock_shared(ifp
);
1543 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1544 ifnet_lock_done(ifp
);
1545 if (in6m
== NULL
&& ia_loop
!= NULL
) {
1547 (struct sockaddr
*)&mltaddr
,
1548 (struct sockaddr
*)&ia_loop
->ia_addr
,
1549 (struct sockaddr
*)&mltmask
,
1551 (struct rtentry
**)0);
1552 (void)in6_addmulti(&mltaddr
.sin6_addr
, ifp
,
1555 log(LOG_WARNING
, "in6_update_ifa: "
1556 "addmulti failed for %s on %s "
1558 ip6_sprintf(&mltaddr
.sin6_addr
),
1559 if_name(ifp
), error
);
1562 if (ia_loop
!= NULL
)
1563 ifafree(&ia_loop
->ia_ifa
);
1567 ia
->ia6_flags
= ifra
->ifra_flags
;
1568 ia
->ia6_flags
&= ~IN6_IFF_DUPLICATED
; /*safety*/
1569 ia
->ia6_flags
&= ~IN6_IFF_NODAD
; /* Mobile IPv6 */
1571 ia
->ia6_lifetime
= ifra
->ifra_lifetime
;
1573 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
1574 ia
->ia6_lifetime
.ia6t_expire
=
1575 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_vltime
;
1577 ia
->ia6_lifetime
.ia6t_expire
= 0;
1578 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
1579 ia
->ia6_lifetime
.ia6t_preferred
=
1580 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_pltime
;
1582 ia
->ia6_lifetime
.ia6t_preferred
= 0;
1585 * make sure to initialize ND6 information. this is to workaround
1586 * issues with interfaces with IPv6 addresses, which have never brought
1587 * up. We are assuming that it is safe to nd6_ifattach multiple times.
1589 if ((error
= nd6_ifattach(ifp
)) != 0)
1593 * Perform DAD, if needed.
1594 * XXX It may be of use, if we can administratively
1597 if (in6if_do_dad(ifp
) && (ifra
->ifra_flags
& IN6_IFF_NODAD
) == 0) {
1598 ia
->ia6_flags
|= IN6_IFF_TENTATIVE
;
1599 nd6_dad_start((struct ifaddr
*)ia
, NULL
);
1606 * XXX: if a change of an existing address failed, keep the entry
1610 in6_unlink_ifa(ia
, ifp
, 0);
1616 struct ifaddr
*ifa
, int nd6_locked
)
1618 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1619 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*) ifa
;
1621 /* stop DAD processing */
1625 * delete route to the destination of the address being purged.
1626 * The interface must be p2p or loopback in this case.
1628 if ((ia
->ia_flags
& IFA_ROUTE
) != 0 && ia
->ia_dstaddr
.sin6_len
!= 0) {
1631 if ((e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
))
1633 log(LOG_ERR
, "in6_purgeaddr: failed to remove "
1634 "a route to the p2p destination: %s on %s, "
1636 ip6_sprintf(&ia
->ia_addr
.sin6_addr
), if_name(ifp
),
1638 /* proceed anyway... */
1641 ia
->ia_flags
&= ~IFA_ROUTE
;
1644 /* Remove ownaddr's loopback rtentry, if it exists. */
1645 in6_ifremloop(&(ia
->ia_ifa
), nd6_locked
);
1647 if (ifp
->if_flags
& IFF_MULTICAST
) {
1649 * delete solicited multicast addr for deleting host id
1651 struct in6_multi
*in6m
;
1652 struct in6_addr llsol
;
1653 bzero(&llsol
, sizeof(struct in6_addr
));
1654 llsol
.s6_addr16
[0] = htons(0xff02);
1655 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1656 llsol
.s6_addr32
[1] = 0;
1657 llsol
.s6_addr32
[2] = htonl(1);
1658 llsol
.s6_addr32
[3] =
1659 ia
->ia_addr
.sin6_addr
.s6_addr32
[3];
1660 llsol
.s6_addr8
[12] = 0xff;
1662 ifnet_lock_shared(ifp
);
1663 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1664 ifnet_lock_done(ifp
);
1666 in6_delmulti(in6m
, nd6_locked
);
1669 in6_unlink_ifa(ia
, ifp
, nd6_locked
);
1670 in6_post_msg(ifp
, KEV_INET6_ADDR_DELETED
, ia
);
1674 in6_unlink_ifa(ia
, ifp
, nd6_locked
)
1675 struct in6_ifaddr
*ia
;
1680 struct in6_ifaddr
*oia
;
1682 ifnet_lock_exclusive(ifp
);
1683 if_detach_ifa(ifp
, &ia
->ia_ifa
);
1684 ifnet_lock_done(ifp
);
1687 lck_mtx_lock(nd6_mutex
);
1689 if (oia
== (ia
= in6_ifaddrs
))
1690 in6_ifaddrs
= ia
->ia_next
;
1692 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
1695 ia
->ia_next
= oia
->ia_next
;
1698 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1701 if (oia
->ia6_ifpr
) { /* check for safety */
1702 plen
= in6_mask2len(&oia
->ia_prefixmask
.sin6_addr
, NULL
);
1703 iilen
= (sizeof(oia
->ia_prefixmask
.sin6_addr
) << 3) - plen
;
1704 in6_prefix_remove_ifid(iilen
, oia
);
1708 * When an autoconfigured address is being removed, release the
1709 * reference to the base prefix. Also, since the release might
1710 * affect the status of other (detached) addresses, call
1711 * pfxlist_onlink_check().
1713 if ((oia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0) {
1714 if (oia
->ia6_ndpr
== NULL
) {
1715 log(LOG_NOTICE
, "in6_unlink_ifa: autoconf'ed address "
1716 "%p has no prefix\n", oia
);
1718 oia
->ia6_ndpr
->ndpr_refcnt
--;
1719 oia
->ia6_flags
&= ~IN6_IFF_AUTOCONF
;
1720 oia
->ia6_ndpr
= NULL
;
1723 pfxlist_onlink_check(1);
1726 lck_mtx_unlock(nd6_mutex
);
1730 * release another refcnt for the link from in6_ifaddrs.
1731 * Note that we should decrement the refcnt at least once for all *BSD.
1733 ifafree(&oia
->ia_ifa
);
1741 struct in6_ifaddr
*ia
, *nia
= NULL
;
1743 if (ifp
== NULL
|| &ifp
->if_addrlist
== NULL
)
1746 lck_mtx_lock(nd6_mutex
);
1747 for (ia
= in6_ifaddrs
; ia
!= NULL
; ia
= nia
)
1750 if (ia
->ia_ifa
.ifa_ifp
!= ifp
)
1752 in6_purgeaddr(&ia
->ia_ifa
, 1);
1754 lck_mtx_unlock(nd6_mutex
);
1761 * SIOCGLIFADDR: get first address. (?)
1762 * SIOCGLIFADDR with IFLR_PREFIX:
1763 * get first address that matches the specified prefix.
1764 * SIOCALIFADDR: add the specified address.
1765 * SIOCALIFADDR with IFLR_PREFIX:
1766 * add the specified prefix, filling hostaddr part from
1767 * the first link-local address. prefixlen must be <= 64.
1768 * SIOCDLIFADDR: delete the specified address.
1769 * SIOCDLIFADDR with IFLR_PREFIX:
1770 * delete the first address that matches the specified prefix.
1772 * EINVAL on invalid parameters
1773 * EADDRNOTAVAIL on prefix match failed/specified address not found
1774 * other values may be returned from in6_ioctl()
1776 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1777 * this is to accomodate address naming scheme other than RFC2374,
1779 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1780 * address encoding scheme. (see figure on page 8)
1783 in6_lifaddr_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
1784 struct ifnet
*ifp
, struct proc
*p
)
1786 struct if_laddrreq
*iflr
= (struct if_laddrreq
*)data
;
1787 struct ifaddr
*ifa
= NULL
;
1788 struct sockaddr
*sa
;
1789 int p64
= proc_is64bit(p
);
1792 if (!data
|| !ifp
) {
1793 panic("invalid argument to in6_lifaddr_ioctl");
1799 /* address must be specified on GET with IFLR_PREFIX */
1800 if ((iflr
->flags
& IFLR_PREFIX
) == 0)
1805 /* address must be specified on ADD and DELETE */
1806 sa
= (struct sockaddr
*)&iflr
->addr
;
1807 if (sa
->sa_family
!= AF_INET6
)
1809 if (sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1811 /* XXX need improvement */
1812 sa
= (struct sockaddr
*)&iflr
->dstaddr
;
1813 if (sa
->sa_family
&& sa
->sa_family
!= AF_INET6
)
1815 if (sa
->sa_len
&& sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1818 default: /* shouldn't happen */
1820 panic("invalid cmd to in6_lifaddr_ioctl");
1826 if (sizeof(struct in6_addr
) * 8 < iflr
->prefixlen
)
1832 struct in6_aliasreq ifra
;
1833 struct in6_addr hostaddr
;
1835 int hostid_found
= 0;
1837 if ((iflr
->flags
& IFLR_PREFIX
) != 0) {
1838 struct sockaddr_in6
*sin6
;
1841 * hostaddr is to fill in the hostaddr part of the
1842 * address. hostaddr points to the first link-local
1843 * address attached to the interface.
1845 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
, 0);
1847 return EADDRNOTAVAIL
;
1848 hostaddr
= *IFA_IN6(ifa
);
1853 /* prefixlen must be <= 64. */
1854 if (64 < iflr
->prefixlen
)
1856 prefixlen
= iflr
->prefixlen
;
1858 /* hostaddr part must be zero. */
1859 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1860 if (sin6
->sin6_addr
.s6_addr32
[2] != 0
1861 || sin6
->sin6_addr
.s6_addr32
[3] != 0) {
1865 prefixlen
= iflr
->prefixlen
;
1867 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1868 bzero(&ifra
, sizeof(ifra
));
1869 bcopy(iflr
->iflr_name
, ifra
.ifra_name
, sizeof(ifra
.ifra_name
));
1871 bcopy(&iflr
->addr
, &ifra
.ifra_addr
,
1872 ((struct sockaddr
*)&iflr
->addr
)->sa_len
);
1874 /* fill in hostaddr part */
1875 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] =
1876 hostaddr
.s6_addr32
[2];
1877 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] =
1878 hostaddr
.s6_addr32
[3];
1881 if (((struct sockaddr
*)&iflr
->dstaddr
)->sa_family
) { /*XXX*/
1882 bcopy(&iflr
->dstaddr
, &ifra
.ifra_dstaddr
,
1883 ((struct sockaddr
*)&iflr
->dstaddr
)->sa_len
);
1885 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[2] =
1886 hostaddr
.s6_addr32
[2];
1887 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[3] =
1888 hostaddr
.s6_addr32
[3];
1892 ifra
.ifra_prefixmask
.sin6_len
= sizeof(struct sockaddr_in6
);
1893 in6_len2mask(&ifra
.ifra_prefixmask
.sin6_addr
, prefixlen
);
1895 ifra
.ifra_flags
= iflr
->flags
& ~IFLR_PREFIX
;
1897 #if defined(__LP64__)
1898 struct in6_aliasreq_32 ifra_32
;
1900 * Use 32-bit ioctl and structure for 32-bit process.
1902 in6_aliasreq_64_to_32((struct in6_aliasreq_64
*)&ifra
,
1904 return (in6_control(so
, SIOCAIFADDR_IN6_32
,
1905 (caddr_t
)&ifra_32
, ifp
, p
));
1907 return (in6_control(so
, SIOCAIFADDR_IN6
,
1908 (caddr_t
)&ifra
, ifp
, p
));
1909 #endif /* __LP64__ */
1911 #if defined(__LP64__)
1912 return (in6_control(so
, SIOCAIFADDR_IN6
,
1913 (caddr_t
)&ifra
, ifp
, p
));
1915 struct in6_aliasreq_64 ifra_64
;
1917 * Use 64-bit ioctl and structure for 64-bit process.
1919 in6_aliasreq_32_to_64((struct in6_aliasreq_32
*)&ifra
,
1921 return (in6_control(so
, SIOCAIFADDR_IN6_64
,
1922 (caddr_t
)&ifra_64
, ifp
, p
));
1923 #endif /* __LP64__ */
1930 struct in6_ifaddr
*ia
;
1931 struct in6_addr mask
, candidate
, match
;
1932 struct sockaddr_in6
*sin6
;
1935 bzero(&mask
, sizeof(mask
));
1936 if (iflr
->flags
& IFLR_PREFIX
) {
1937 /* lookup a prefix rather than address. */
1938 in6_len2mask(&mask
, iflr
->prefixlen
);
1940 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1941 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
1942 match
.s6_addr32
[0] &= mask
.s6_addr32
[0];
1943 match
.s6_addr32
[1] &= mask
.s6_addr32
[1];
1944 match
.s6_addr32
[2] &= mask
.s6_addr32
[2];
1945 match
.s6_addr32
[3] &= mask
.s6_addr32
[3];
1947 /* if you set extra bits, that's wrong */
1948 if (bcmp(&match
, &sin6
->sin6_addr
, sizeof(match
)))
1953 if (cmd
== SIOCGLIFADDR
) {
1954 /* on getting an address, take the 1st match */
1957 /* on deleting an address, do exact match */
1958 in6_len2mask(&mask
, 128);
1959 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1960 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
1966 ifnet_lock_shared(ifp
);
1967 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1969 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1974 bcopy(IFA_IN6(ifa
), &candidate
, sizeof(candidate
));
1975 #ifndef SCOPEDROUTING
1977 * XXX: this is adhoc, but is necessary to allow
1978 * a user to specify fe80::/64 (not /10) for a
1979 * link-local address.
1981 if (IN6_IS_ADDR_LINKLOCAL(&candidate
))
1982 candidate
.s6_addr16
[1] = 0;
1984 candidate
.s6_addr32
[0] &= mask
.s6_addr32
[0];
1985 candidate
.s6_addr32
[1] &= mask
.s6_addr32
[1];
1986 candidate
.s6_addr32
[2] &= mask
.s6_addr32
[2];
1987 candidate
.s6_addr32
[3] &= mask
.s6_addr32
[3];
1988 if (IN6_ARE_ADDR_EQUAL(&candidate
, &match
))
1991 ifnet_lock_done(ifp
);
1993 return EADDRNOTAVAIL
;
1996 if (cmd
== SIOCGLIFADDR
) {
1997 #ifndef SCOPEDROUTING
1998 struct sockaddr_in6
*s6
;
2001 /* fill in the if_laddrreq structure */
2002 bcopy(&ia
->ia_addr
, &iflr
->addr
, ia
->ia_addr
.sin6_len
);
2003 #ifndef SCOPEDROUTING /* XXX see above */
2004 s6
= (struct sockaddr_in6
*)&iflr
->addr
;
2005 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
2006 s6
->sin6_addr
.s6_addr16
[1] = 0;
2008 in6_addr2scopeid(ifp
, &s6
->sin6_addr
);
2011 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
2012 bcopy(&ia
->ia_dstaddr
, &iflr
->dstaddr
,
2013 ia
->ia_dstaddr
.sin6_len
);
2014 #ifndef SCOPEDROUTING /* XXX see above */
2015 s6
= (struct sockaddr_in6
*)&iflr
->dstaddr
;
2016 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
2017 s6
->sin6_addr
.s6_addr16
[1] = 0;
2019 in6_addr2scopeid(ifp
,
2024 bzero(&iflr
->dstaddr
, sizeof(iflr
->dstaddr
));
2027 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
2030 iflr
->flags
= ia
->ia6_flags
; /* XXX */
2034 struct in6_aliasreq ifra
;
2036 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
2037 bzero(&ifra
, sizeof(ifra
));
2038 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
2039 sizeof(ifra
.ifra_name
));
2041 bcopy(&ia
->ia_addr
, &ifra
.ifra_addr
,
2042 ia
->ia_addr
.sin6_len
);
2043 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
2044 bcopy(&ia
->ia_dstaddr
, &ifra
.ifra_dstaddr
,
2045 ia
->ia_dstaddr
.sin6_len
);
2047 bzero(&ifra
.ifra_dstaddr
,
2048 sizeof(ifra
.ifra_dstaddr
));
2050 bcopy(&ia
->ia_prefixmask
, &ifra
.ifra_dstaddr
,
2051 ia
->ia_prefixmask
.sin6_len
);
2053 ifra
.ifra_flags
= ia
->ia6_flags
;
2055 #if defined(__LP64__)
2056 struct in6_aliasreq_32 ifra_32
;
2058 * Use 32-bit structure for 32-bit process.
2059 * SIOCDIFADDR_IN6 is encoded with in6_ifreq,
2060 * so it stays the same since the size does
2061 * not change. The data part of the ioctl,
2062 * however, is of a different structure, i.e.
2065 in6_aliasreq_64_to_32(
2066 (struct in6_aliasreq_64
*)&ifra
, &ifra_32
);
2067 return (in6_control(so
, SIOCDIFADDR_IN6
,
2068 (caddr_t
)&ifra_32
, ifp
, p
));
2070 return (in6_control(so
, SIOCDIFADDR_IN6
,
2071 (caddr_t
)&ifra
, ifp
, p
));
2072 #endif /* __LP64__ */
2074 #if defined(__LP64__)
2075 return (in6_control(so
, SIOCDIFADDR_IN6
,
2076 (caddr_t
)&ifra
, ifp
, p
));
2078 struct in6_aliasreq_64 ifra_64
;
2080 * Use 64-bit structure for 64-bit process.
2081 * SIOCDIFADDR_IN6 is encoded with in6_ifreq,
2082 * so it stays the same since the size does
2083 * not change. The data part of the ioctl,
2084 * however, is of a different structure, i.e.
2087 in6_aliasreq_32_to_64(
2088 (struct in6_aliasreq_32
*)&ifra
, &ifra_64
);
2089 return (in6_control(so
, SIOCDIFADDR_IN6
,
2090 (caddr_t
)&ifra_64
, ifp
, p
));
2091 #endif /* __LP64__ */
2098 return EOPNOTSUPP
; /* just for safety */
2102 * Initialize an interface's intetnet6 address
2103 * and routing table entry.
2106 in6_ifinit(ifp
, ia
, sin6
, newhost
)
2108 struct in6_ifaddr
*ia
;
2109 struct sockaddr_in6
*sin6
;
2112 int error
= 0, plen
, ifacount
= 0;
2116 * Give the interface a chance to initialize
2117 * if this is its first address,
2118 * and to validate the address if necessary.
2120 ifnet_lock_shared(ifp
);
2121 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2123 if (ifa
->ifa_addr
== NULL
)
2124 continue; /* just for safety */
2125 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2129 ifnet_lock_done(ifp
);
2131 ia
->ia_addr
= *sin6
;
2134 if (ifacount
<= 1 &&
2135 (error
= ifnet_ioctl(ifp
, PF_INET6
, SIOCSIFADDR
, ia
))) {
2141 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
2143 /* we could do in(6)_socktrim here, but just omit it at this moment. */
2147 * If the destination address is specified for a point-to-point
2148 * interface, install a route to the destination as an interface
2151 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
); /* XXX */
2152 if (plen
== 128 && ia
->ia_dstaddr
.sin6_family
== AF_INET6
) {
2153 if ((error
= rtinit(&(ia
->ia_ifa
), (int)RTM_ADD
,
2154 RTF_UP
| RTF_HOST
)) != 0)
2156 ia
->ia_flags
|= IFA_ROUTE
;
2160 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
2162 ia
->ia_ifa
.ifa_flags
|= RTF_CLONING
;
2165 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
2167 /* set the rtrequest function to create llinfo */
2168 ia
->ia_ifa
.ifa_rtrequest
= nd6_rtrequest
;
2169 in6_ifaddloop(&(ia
->ia_ifa
));
2176 * Add an address to the list of IP6 multicast addresses for a
2180 in6_addmulti(maddr6
, ifp
, errorp
, nd6_locked
)
2181 struct in6_addr
*maddr6
;
2186 struct in6_multi
*in6m
;
2187 struct sockaddr_in6 sin6
;
2188 struct ifmultiaddr
*ifma
;
2193 * Call generic routine to add membership or increment
2194 * refcount. It wants addresses in the form of a sockaddr,
2195 * so we build one here (being careful to zero the unused bytes).
2197 bzero(&sin6
, sizeof sin6
);
2198 sin6
.sin6_family
= AF_INET6
;
2199 sin6
.sin6_len
= sizeof sin6
;
2200 sin6
.sin6_addr
= *maddr6
;
2201 *errorp
= if_addmulti(ifp
, (struct sockaddr
*)&sin6
, &ifma
);
2207 * If ifma->ifma_protospec is null, then if_addmulti() created
2208 * a new record. Otherwise, we are done.
2210 if (ifma
->ifma_protospec
!= 0)
2211 return ifma
->ifma_protospec
;
2213 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
2214 at interrupt time? If so, need to fix if_addmulti. XXX */
2215 in6m
= (struct in6_multi
*)_MALLOC(sizeof(*in6m
), M_IPMADDR
, M_NOWAIT
);
2220 bzero(in6m
, sizeof *in6m
);
2221 in6m
->in6m_addr
= *maddr6
;
2222 in6m
->in6m_ifp
= ifp
;
2223 in6m
->in6m_ifma
= ifma
;
2224 ifma
->ifma_protospec
= in6m
;
2225 if (nd6_locked
== 0)
2226 lck_mtx_lock(nd6_mutex
);
2227 LIST_INSERT_HEAD(&in6_multihead
, in6m
, in6m_entry
);
2228 if (nd6_locked
== 0)
2229 lck_mtx_unlock(nd6_mutex
);
2232 * Let MLD6 know that we have joined a new IP6 multicast
2235 mld6_start_listening(in6m
);
2240 * Delete a multicast address record.
2244 struct in6_multi
*in6m
, int nd6locked
)
2246 struct ifmultiaddr
*ifma
= in6m
->in6m_ifma
;
2248 if (ifma
&& ifma
->ifma_usecount
== 1) {
2250 * No remaining claims to this record; let MLD6 know
2251 * that we are leaving the multicast group.
2253 mld6_stop_listening(in6m
);
2254 ifma
->ifma_protospec
= 0;
2256 lck_mtx_lock(nd6_mutex
);
2257 LIST_REMOVE(in6m
, in6m_entry
);
2259 lck_mtx_unlock(nd6_mutex
);
2260 FREE(in6m
, M_IPMADDR
);
2262 /* XXX - should be separate API for when we have an ifma? */
2264 if_delmultiaddr(ifma
, 0);
2270 * Find an IPv6 interface link-local address specific to an interface.
2273 in6ifa_ifpforlinklocal(ifp
, ignoreflags
)
2279 ifnet_lock_shared(ifp
);
2280 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2282 if (ifa
->ifa_addr
== NULL
)
2283 continue; /* just for safety */
2284 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2286 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
))) {
2287 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2295 ifnet_lock_done(ifp
);
2297 return((struct in6_ifaddr
*)ifa
);
2301 * find the internet address corresponding to a given interface and address.
2304 in6ifa_ifpwithaddr(ifp
, addr
)
2306 struct in6_addr
*addr
;
2310 ifnet_lock_shared(ifp
);
2311 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2313 if (ifa
->ifa_addr
== NULL
)
2314 continue; /* just for safety */
2315 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2317 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(ifa
)))
2322 ifnet_lock_done(ifp
);
2324 return((struct in6_ifaddr
*)ifa
);
2328 * Convert IP6 address to printable (loggable) representation.
2330 static char digits
[] = "0123456789abcdef";
2331 static int ip6round
= 0;
2334 const struct in6_addr
*addr
;
2336 static char ip6buf
[8][48];
2339 const u_short
*a
= (const u_short
*)addr
;
2343 ip6round
= (ip6round
+ 1) & 7;
2344 cp
= ip6buf
[ip6round
];
2346 for (i
= 0; i
< 8; i
++) {
2357 if (dcolon
== 0 && *(a
+ 1) == 0) {
2369 d
= (const u_char
*)a
;
2370 *cp
++ = digits
[*d
>> 4];
2371 *cp
++ = digits
[*d
++ & 0xf];
2372 *cp
++ = digits
[*d
>> 4];
2373 *cp
++ = digits
[*d
& 0xf];
2378 return(ip6buf
[ip6round
]);
2382 in6addr_local(struct in6_addr
*in6
)
2385 struct sockaddr_in6 sin6
;
2388 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
))
2391 sin6
.sin6_family
= AF_INET6
;
2392 sin6
.sin6_len
= sizeof (sin6
);
2393 bcopy(in6
, &sin6
.sin6_addr
, sizeof (*in6
));
2394 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0);
2398 if (rt
->rt_gateway
->sa_family
== AF_LINK
)
2403 local
= in6_localaddr(in6
);
2410 struct in6_addr
*in6
;
2412 struct in6_ifaddr
*ia
;
2414 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
))
2417 lck_mtx_lock(nd6_mutex
);
2418 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
)
2419 if (IN6_ARE_MASKED_ADDR_EQUAL(in6
, &ia
->ia_addr
.sin6_addr
,
2420 &ia
->ia_prefixmask
.sin6_addr
)) {
2421 lck_mtx_unlock(nd6_mutex
);
2425 lck_mtx_unlock(nd6_mutex
);
2430 in6_is_addr_deprecated(sa6
)
2431 struct sockaddr_in6
*sa6
;
2433 struct in6_ifaddr
*ia
;
2435 lck_mtx_lock(nd6_mutex
);
2436 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2437 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
,
2440 ia
->ia_addr
.sin6_scope_id
== sa6
->sin6_scope_id
&&
2442 (ia
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0) {
2443 lck_mtx_unlock(nd6_mutex
);
2444 return(1); /* true */
2447 /* XXX: do we still have to go thru the rest of the list? */
2450 lck_mtx_unlock(nd6_mutex
);
2451 return(0); /* false */
2455 * return length of part which dst and src are equal
2459 in6_matchlen(src
, dst
)
2460 struct in6_addr
*src
, *dst
;
2463 u_char
*s
= (u_char
*)src
, *d
= (u_char
*)dst
;
2464 u_char
*lim
= s
+ 16, r
;
2467 if ((r
= (*d
++ ^ *s
++)) != 0) {
2478 /* XXX: to be scope conscious */
2480 in6_are_prefix_equal(p1
, p2
, len
)
2481 struct in6_addr
*p1
, *p2
;
2484 int bytelen
, bitlen
;
2487 if (0 > len
|| len
> 128) {
2488 log(LOG_ERR
, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2496 if (bcmp(&p1
->s6_addr
, &p2
->s6_addr
, bytelen
))
2499 p1
->s6_addr
[bytelen
] >> (8 - bitlen
) !=
2500 p2
->s6_addr
[bytelen
] >> (8 - bitlen
))
2507 in6_prefixlen2mask(maskp
, len
)
2508 struct in6_addr
*maskp
;
2511 u_char maskarray
[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2512 int bytelen
, bitlen
, i
;
2515 if (0 > len
|| len
> 128) {
2516 log(LOG_ERR
, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2521 bzero(maskp
, sizeof(*maskp
));
2524 for (i
= 0; i
< bytelen
; i
++)
2525 maskp
->s6_addr
[i
] = 0xff;
2527 maskp
->s6_addr
[bytelen
] = maskarray
[bitlen
- 1];
2531 * return the best address out of the same scope
2536 struct in6_addr
*dst
)
2538 int dst_scope
= in6_addrscope(dst
), src_scope
, best_scope
= 0;
2542 struct in6_ifaddr
*ifa_best
= NULL
;
2546 printf("in6_ifawithscope: output interface is not specified\n");
2552 * We search for all addresses on all interfaces from the beginning.
2553 * Comparing an interface with the outgoing interface will be done
2554 * only at the final stage of tiebreaking.
2556 ifnet_head_lock_shared();
2557 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
2559 * We can never take an address that breaks the scope zone
2560 * of the destination.
2562 if (in6_addr2scopeid(ifp
, dst
) != in6_addr2scopeid(oifp
, dst
))
2565 ifnet_lock_shared(ifp
);
2566 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2568 int tlen
= -1, dscopecmp
, bscopecmp
, matchcmp
;
2570 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2573 src_scope
= in6_addrscope(IFA_IN6(ifa
));
2576 * Don't use an address before completing DAD
2577 * nor a duplicated address.
2579 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2583 /* XXX: is there any case to allow anycasts? */
2584 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2588 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2593 * If this is the first address we find,
2596 if (ifa_best
== NULL
)
2600 * ifa_best is never NULL beyond this line except
2601 * within the block labeled "replace".
2605 * If ifa_best has a smaller scope than dst and
2606 * the current address has a larger one than
2607 * (or equal to) dst, always replace ifa_best.
2608 * Also, if the current address has a smaller scope
2609 * than dst, ignore it unless ifa_best also has a
2611 * Consequently, after the two if-clause below,
2612 * the followings must be satisfied:
2613 * (scope(src) < scope(dst) &&
2614 * scope(best) < scope(dst))
2616 * (scope(best) >= scope(dst) &&
2617 * scope(src) >= scope(dst))
2619 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0 &&
2620 IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) >= 0)
2621 goto replace
; /* (A) */
2622 if (IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) < 0 &&
2623 IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) >= 0)
2627 * A deprecated address SHOULD NOT be used in new
2628 * communications if an alternate (non-deprecated)
2629 * address is available and has sufficient scope.
2630 * RFC 2462, Section 5.5.4.
2632 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2633 IN6_IFF_DEPRECATED
) {
2635 * Ignore any deprecated addresses if
2636 * specified by configuration.
2638 if (!ip6_use_deprecated
)
2642 * If we have already found a non-deprecated
2643 * candidate, just ignore deprecated addresses.
2645 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
)
2651 * A non-deprecated address is always preferred
2652 * to a deprecated one regardless of scopes and
2653 * address matching (Note invariants ensured by the
2654 * conditions (A) and (B) above.)
2656 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) &&
2657 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2658 IN6_IFF_DEPRECATED
) == 0)
2662 * When we use temporary addresses described in
2663 * RFC 3041, we prefer temporary addresses to
2664 * public autoconf addresses. Again, note the
2665 * invariants from (A) and (B). Also note that we
2666 * don't have any preference between static addresses
2667 * and autoconf addresses (despite of whether or not
2668 * the latter is temporary or public.)
2670 if (ip6_use_tempaddr
) {
2671 struct in6_ifaddr
*ifat
;
2673 ifat
= (struct in6_ifaddr
*)ifa
;
2674 if ((ifa_best
->ia6_flags
&
2675 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2676 == IN6_IFF_AUTOCONF
&&
2678 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2679 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
)) {
2682 if ((ifa_best
->ia6_flags
&
2683 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2684 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
) &&
2686 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2687 == IN6_IFF_AUTOCONF
) {
2693 * At this point, we have two cases:
2694 * 1. we are looking at a non-deprecated address,
2695 * and ifa_best is also non-deprecated.
2696 * 2. we are looking at a deprecated address,
2697 * and ifa_best is also deprecated.
2698 * Also, we do not have to consider a case where
2699 * the scope of if_best is larger(smaller) than dst and
2700 * the scope of the current address is smaller(larger)
2701 * than dst. Such a case has already been covered.
2702 * Tiebreaking is done according to the following
2704 * - the scope comparison between the address and
2706 * - the scope comparison between the address and
2707 * ifa_best (bscopecmp)
2708 * - if the address match dst longer than ifa_best
2710 * - if the address is on the outgoing I/F (outI/F)
2712 * Roughly speaking, the selection policy is
2713 * - the most important item is scope. The same scope
2714 * is best. Then search for a larger scope.
2715 * Smaller scopes are the last resort.
2716 * - A deprecated address is chosen only when we have
2717 * no address that has an enough scope, but is
2718 * prefered to any addresses of smaller scopes
2719 * (this must be already done above.)
2720 * - addresses on the outgoing I/F are preferred to
2721 * ones on other interfaces if none of above
2722 * tiebreaks. In the table below, the column "bI"
2723 * means if the best_ifa is on the outgoing
2724 * interface, and the column "sI" means if the ifa
2725 * is on the outgoing interface.
2726 * - If there is no other reasons to choose one,
2727 * longest address match against dst is considered.
2729 * The precise decision table is as follows:
2730 * dscopecmp bscopecmp match bI oI | replace?
2731 * N/A equal N/A Y N | No (1)
2732 * N/A equal N/A N Y | Yes (2)
2733 * N/A equal larger N/A | Yes (3)
2734 * N/A equal !larger N/A | No (4)
2735 * larger larger N/A N/A | No (5)
2736 * larger smaller N/A N/A | Yes (6)
2737 * smaller larger N/A N/A | Yes (7)
2738 * smaller smaller N/A N/A | No (8)
2739 * equal smaller N/A N/A | Yes (9)
2740 * equal larger (already done at A above)
2742 dscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
);
2743 bscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, best_scope
);
2745 if (bscopecmp
== 0) {
2746 struct ifnet
*bifp
= ifa_best
->ia_ifp
;
2748 if (bifp
== oifp
&& ifp
!= oifp
) /* (1) */
2750 if (bifp
!= oifp
&& ifp
== oifp
) /* (2) */
2754 * Both bifp and ifp are on the outgoing
2755 * interface, or both two are on a different
2756 * interface from the outgoing I/F.
2757 * now we need address matching against dst
2760 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
2761 matchcmp
= tlen
- blen
;
2762 if (matchcmp
> 0) /* (3) */
2766 if (dscopecmp
> 0) {
2767 if (bscopecmp
> 0) /* (5) */
2769 goto replace
; /* (6) */
2771 if (dscopecmp
< 0) {
2772 if (bscopecmp
> 0) /* (7) */
2777 /* now dscopecmp must be 0 */
2779 goto replace
; /* (9) */
2784 ifafree(&ifa_best
->ia_ifa
);
2785 ifa_best
= (struct in6_ifaddr
*)ifa
;
2786 blen
= tlen
>= 0 ? tlen
:
2787 in6_matchlen(IFA_IN6(ifa
), dst
);
2788 best_scope
= in6_addrscope(&ifa_best
->ia_addr
.sin6_addr
);
2790 ifnet_lock_done(ifp
);
2794 /* count statistics for future improvements */
2795 if (ifa_best
== NULL
)
2796 ip6stat
.ip6s_sources_none
++;
2798 if (oifp
== ifa_best
->ia_ifp
)
2799 ip6stat
.ip6s_sources_sameif
[best_scope
]++;
2801 ip6stat
.ip6s_sources_otherif
[best_scope
]++;
2803 if (best_scope
== dst_scope
)
2804 ip6stat
.ip6s_sources_samescope
[best_scope
]++;
2806 ip6stat
.ip6s_sources_otherscope
[best_scope
]++;
2808 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0)
2809 ip6stat
.ip6s_sources_deprecated
[best_scope
]++;
2816 * return the best address out of the same scope. if no address was
2817 * found, return the first valid address from designated IF.
2822 struct in6_addr
*dst
)
2824 int dst_scope
= in6_addrscope(dst
), blen
= -1, tlen
;
2826 struct in6_ifaddr
*besta
= 0;
2827 struct in6_ifaddr
*dep
[2]; /* last-resort: deprecated */
2829 dep
[0] = dep
[1] = NULL
;
2832 * We first look for addresses in the same scope.
2833 * If there is one, return it.
2834 * If two or more, return one which matches the dst longest.
2835 * If none, return one of global addresses assigned other ifs.
2837 ifnet_lock_shared(ifp
);
2838 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2840 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2842 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
)
2843 continue; /* XXX: is there any case to allow anycast? */
2844 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_NOTREADY
)
2845 continue; /* don't use this interface */
2846 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DETACHED
)
2848 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
2849 if (ip6_use_deprecated
) {
2851 ifafree(&dep
[0]->ia_ifa
);
2852 dep
[0] = (struct in6_ifaddr
*)ifa
;
2858 if (dst_scope
== in6_addrscope(IFA_IN6(ifa
))) {
2860 * call in6_matchlen() as few as possible
2864 blen
= in6_matchlen(&besta
->ia_addr
.sin6_addr
, dst
);
2865 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
2868 besta
= (struct in6_ifaddr
*)ifa
;
2871 besta
= (struct in6_ifaddr
*)ifa
;
2875 ifaref(&besta
->ia_ifa
);
2876 ifnet_lock_done(ifp
);
2878 ifafree(&dep
[0]->ia_ifa
);
2882 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2884 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2886 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
)
2887 continue; /* XXX: is there any case to allow anycast? */
2888 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_NOTREADY
)
2889 continue; /* don't use this interface */
2890 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DETACHED
)
2892 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
2893 if (ip6_use_deprecated
) {
2895 ifafree(&dep
[1]->ia_ifa
);
2896 dep
[1] = (struct in6_ifaddr
*)ifa
;
2903 ifnet_lock_done(ifp
);
2905 ifafree(&dep
[0]->ia_ifa
);
2907 ifafree(&dep
[1]->ia_ifa
);
2908 return (struct in6_ifaddr
*)ifa
;
2910 ifnet_lock_done(ifp
);
2912 /* use the last-resort values, that are, deprecated addresses */
2915 ifafree(&dep
[1]->ia_ifa
);
2925 * perform DAD when interface becomes IFF_UP.
2930 struct in6_aliasreq
*ifra
)
2933 struct in6_ifaddr
*ia
;
2934 int dad_delay
; /* delay ticks before DAD output */
2941 * special cases, like 6to4, are handled in in6_ifattach
2943 error
= in6_ifattach(ifp
, NULL
, ifra
);
2948 ifnet_lock_exclusive(ifp
);
2949 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2951 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2953 ia
= (struct in6_ifaddr
*)ifa
;
2954 if (ia
->ia6_flags
& IN6_IFF_TENTATIVE
)
2955 nd6_dad_start(ifa
, &dad_delay
);
2957 ifnet_lock_done(ifp
);
2966 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0)
2969 switch (ifp
->if_type
) {
2975 * These interfaces do not have the IFF_LOOPBACK flag,
2976 * but loop packets back. We do not have to do DAD on such
2977 * interfaces. We should even omit it, because loop-backed
2978 * NS would confuse the DAD procedure.
2983 * Our DAD routine requires the interface up and running.
2984 * However, some interfaces can be up before the RUNNING
2985 * status. Additionaly, users may try to assign addresses
2986 * before the interface becomes up (or running).
2987 * We simply skip DAD in such a case as a work around.
2988 * XXX: we should rather mark "tentative" on such addresses,
2989 * and do DAD after the interface becomes ready.
2991 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) !=
2992 (IFF_UP
|IFF_RUNNING
))
3000 * Calculate max IPv6 MTU through all the interfaces and store it
3006 u_int32_t maxmtu
= 0;
3009 ifnet_head_lock_shared();
3010 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
3011 lck_rw_lock_shared(nd_if_rwlock
);
3012 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
3013 IN6_LINKMTU(ifp
) > maxmtu
)
3014 maxmtu
= IN6_LINKMTU(ifp
);
3015 lck_rw_done(nd_if_rwlock
);
3018 if (maxmtu
) /* update only when maxmtu is positive */
3019 in6_maxmtu
= maxmtu
;
3023 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
3024 * v4 mapped addr or v4 compat addr
3027 in6_sin6_2_sin(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3029 bzero(sin
, sizeof(*sin
));
3030 sin
->sin_len
= sizeof(struct sockaddr_in
);
3031 sin
->sin_family
= AF_INET
;
3032 sin
->sin_port
= sin6
->sin6_port
;
3033 sin
->sin_addr
.s_addr
= sin6
->sin6_addr
.s6_addr32
[3];
3036 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
3038 in6_sin_2_v4mapsin6(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3040 bzero(sin6
, sizeof(*sin6
));
3041 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
3042 sin6
->sin6_family
= AF_INET6
;
3043 sin6
->sin6_port
= sin
->sin_port
;
3044 sin6
->sin6_addr
.s6_addr32
[0] = 0;
3045 sin6
->sin6_addr
.s6_addr32
[1] = 0;
3046 sin6
->sin6_addr
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
3047 sin6
->sin6_addr
.s6_addr32
[3] = sin
->sin_addr
.s_addr
;
3050 /* Convert sockaddr_in6 into sockaddr_in. */
3052 in6_sin6_2_sin_in_sock(struct sockaddr
*nam
)
3054 struct sockaddr_in
*sin_p
;
3055 struct sockaddr_in6 sin6
;
3058 * Save original sockaddr_in6 addr and convert it
3061 sin6
= *(struct sockaddr_in6
*)nam
;
3062 sin_p
= (struct sockaddr_in
*)nam
;
3063 in6_sin6_2_sin(sin_p
, &sin6
);
3066 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
3068 in6_sin_2_v4mapsin6_in_sock(struct sockaddr
**nam
)
3070 struct sockaddr_in
*sin_p
;
3071 struct sockaddr_in6
*sin6_p
;
3073 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof *sin6_p
, M_SONAME
,
3077 sin_p
= (struct sockaddr_in
*)*nam
;
3078 in6_sin_2_v4mapsin6(sin_p
, sin6_p
);
3079 FREE(*nam
, M_SONAME
);
3080 *nam
= (struct sockaddr
*)sin6_p
;
3086 * Posts in6_event_data message kernel events.
3088 * To get the same size of kev_in6_data between ILP32 and LP64 data models
3089 * we are using a special version of the in6_addrlifetime structure that
3090 * uses only 32 bits fields to be compatible with Leopard, and that
3091 * are large enough to span 68 years.
3094 in6_post_msg(struct ifnet
*ifp
, u_int32_t event_code
, struct in6_ifaddr
*ifa
)
3096 struct kev_msg ev_msg
;
3097 struct kev_in6_data in6_event_data
;
3099 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3100 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3101 ev_msg
.kev_subclass
= KEV_INET6_SUBCLASS
;
3102 ev_msg
.event_code
= event_code
;
3104 in6_event_data
.ia_addr
= ifa
->ia_addr
;
3105 in6_event_data
.ia_net
= ifa
->ia_net
;
3106 in6_event_data
.ia_dstaddr
= ifa
->ia_dstaddr
;
3107 in6_event_data
.ia_prefixmask
= ifa
->ia_prefixmask
;
3108 in6_event_data
.ia_plen
= ifa
->ia_plen
;
3109 in6_event_data
.ia6_flags
= (u_int32_t
)ifa
->ia6_flags
;
3111 in6_event_data
.ia_lifetime
.ia6t_expire
=
3112 ifa
->ia6_lifetime
.ia6t_expire
;
3113 in6_event_data
.ia_lifetime
.ia6t_preferred
=
3114 ifa
->ia6_lifetime
.ia6t_preferred
;
3115 in6_event_data
.ia_lifetime
.ia6t_vltime
=
3116 ifa
->ia6_lifetime
.ia6t_vltime
;
3117 in6_event_data
.ia_lifetime
.ia6t_pltime
=
3118 ifa
->ia6_lifetime
.ia6t_pltime
;
3121 strncpy(&in6_event_data
.link_data
.if_name
[0],
3122 ifp
->if_name
, IFNAMSIZ
);
3123 in6_event_data
.link_data
.if_family
= ifp
->if_family
;
3124 in6_event_data
.link_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3127 ev_msg
.dv
[0].data_ptr
= &in6_event_data
;
3128 ev_msg
.dv
[0].data_length
= sizeof (in6_event_data
);
3129 ev_msg
.dv
[1].data_length
= 0;
3131 kev_post_msg(&ev_msg
);
3135 * Called as part of ip6_init
3138 in6_ifaddr_init(void)
3140 PE_parse_boot_argn("ifa_debug", &in6ifa_debug
, sizeof (in6ifa_debug
));
3142 in6ifa_size
= (in6ifa_debug
== 0) ? sizeof (struct in6_ifaddr
) :
3143 sizeof (struct in6_ifaddr_dbg
);
3145 in6ifa_zone
= zinit(in6ifa_size
, IN6IFA_ZONE_MAX
* in6ifa_size
,
3146 0, IN6IFA_ZONE_NAME
);
3147 if (in6ifa_zone
== NULL
)
3148 panic("%s: failed allocating %s", __func__
, IN6IFA_ZONE_NAME
);
3150 zone_change(in6ifa_zone
, Z_EXPAND
, TRUE
);
3153 static struct in6_ifaddr
*
3154 in6_ifaddr_alloc(int how
)
3156 struct in6_ifaddr
*in6ifa
;
3158 in6ifa
= (how
== M_WAITOK
) ? zalloc(in6ifa_zone
) :
3159 zalloc_noblock(in6ifa_zone
);
3160 if (in6ifa
!= NULL
) {
3161 bzero(in6ifa
, in6ifa_size
);
3162 in6ifa
->ia_ifa
.ifa_free
= in6_ifaddr_free
;
3163 in6ifa
->ia_ifa
.ifa_debug
|= IFD_ALLOC
;
3164 if (in6ifa_debug
!= 0) {
3165 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3166 (struct in6_ifaddr_dbg
*)in6ifa
;
3167 in6ifa
->ia_ifa
.ifa_debug
|= IFD_DEBUG
;
3168 in6ifa
->ia_ifa
.ifa_trace
= in6_ifaddr_trace
;
3169 ctrace_record(&in6ifa_dbg
->in6ifa_alloc
);
3176 in6_ifaddr_free(struct ifaddr
*ifa
)
3178 if (ifa
->ifa_refcnt
!= 0)
3179 panic("%s: ifa %p bad ref cnt", __func__
, ifa
);
3180 if (!(ifa
->ifa_debug
& IFD_ALLOC
))
3181 panic("%s: ifa %p cannot be freed", __func__
, ifa
);
3183 if (ifa
->ifa_debug
& IFD_DEBUG
) {
3184 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3185 (struct in6_ifaddr_dbg
*)ifa
;
3186 ctrace_record(&in6ifa_dbg
->in6ifa_free
);
3187 bcopy(&in6ifa_dbg
->in6ifa
, &in6ifa_dbg
->in6ifa_old
,
3188 sizeof (struct in6_ifaddr
));
3190 bzero(ifa
, sizeof (struct in6_ifaddr
));
3191 zfree(in6ifa_zone
, ifa
);
3195 in6_ifaddr_trace(struct ifaddr
*ifa
, int refhold
)
3197 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3202 if (!(ifa
->ifa_debug
& IFD_DEBUG
))
3203 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3206 cnt
= &in6ifa_dbg
->in6ifa_refhold_cnt
;
3207 tr
= in6ifa_dbg
->in6ifa_refhold
;
3209 cnt
= &in6ifa_dbg
->in6ifa_refrele_cnt
;
3210 tr
= in6ifa_dbg
->in6ifa_refrele
;
3213 idx
= OSAddAtomic16(1, (volatile SInt16
*)cnt
) % CTRACE_HIST_SIZE
;
3214 ctrace_record(&tr
[idx
]);