2 * Copyright (c) 2003-2008 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 switch (ifp
->if_type
) {
728 #if IFT_BRIDGE /*OpenBSD 2.8*/
729 /* some of the interfaces are inherently not IPv6 capable */
735 if ((error
= proto_plumb(PF_INET6
, ifp
)))
736 printf("SIOCPROTOATTACH_IN6: %s "
737 "error=%d\n", if_name(ifp
), error
);
744 case SIOCPROTODETACH_IN6
:
745 /* Cleanup interface routes and addresses */
748 if ((error
= proto_unplumb(PF_INET6
, ifp
)))
749 printf("SIOCPROTODETACH_IN6: %s error=%d\n",
750 if_name(ifp
), error
);
755 * Find address for this interface, if it exists; depending
756 * on the ioctl command, sa6 points to the address in ifra/ifr.
758 if (sa6
!= NULL
&& sa6
->sin6_family
== AF_INET6
) {
759 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
760 if (sa6
->sin6_addr
.s6_addr16
[1] == 0) {
761 /* link ID is not embedded by the user */
762 sa6
->sin6_addr
.s6_addr16
[1] =
763 htons(ifp
->if_index
);
764 } else if (sa6
->sin6_addr
.s6_addr16
[1] !=
765 htons(ifp
->if_index
)) {
766 return (EINVAL
); /* link ID contradicts */
768 if (sa6
->sin6_scope_id
) {
769 if (sa6
->sin6_scope_id
!=
770 (u_int32_t
)ifp
->if_index
)
772 sa6
->sin6_scope_id
= 0; /* XXX: good way? */
775 ia
= in6ifa_ifpwithaddr(ifp
, &sa6
->sin6_addr
);
781 case SIOCSIFADDR_IN6
:
782 case SIOCSIFDSTADDR_IN6
:
783 case SIOCSIFNETMASK_IN6
:
785 * Since IPv6 allows a node to assign multiple addresses
786 * on a single interface, SIOCSIFxxx ioctls are not suitable
787 * and should be unused.
789 /* we decided to obsolete this command (20000704) */
793 case SIOCDIFADDR_IN6
:
795 * for IPv4, we look for existing in_ifaddr here to allow
796 * "ifconfig if0 delete" to remove first IPv4 address on the
797 * interface. For IPv6, as the spec allow multiple interface
798 * address from the day one, we consider "remove the first one"
799 * semantics to be not preferable.
802 error
= EADDRNOTAVAIL
;
806 case SIOCAIFADDR_IN6_32
:
807 case SIOCAIFADDR_IN6_64
:
809 * We always require users to specify a valid IPv6 address for
810 * the corresponding operation. Use "sa6" instead of "ifra"
811 * since SIOCDIFADDR_IN6 falls thru above.
813 if (sa6
->sin6_family
!= AF_INET6
||
814 sa6
->sin6_len
!= sizeof(struct sockaddr_in6
)) {
815 error
= EAFNOSUPPORT
;
825 case SIOCGIFADDR_IN6
:
826 /* This interface is basically deprecated. use SIOCGIFCONF. */
828 case SIOCGIFAFLAG_IN6
:
829 case SIOCGIFNETMASK_IN6
:
830 case SIOCGIFDSTADDR_IN6
:
831 case SIOCGIFALIFETIME_IN6
:
832 /* must think again about its semantics */
834 error
= EADDRNOTAVAIL
;
839 case SIOCSIFALIFETIME_IN6
:
845 error
= EADDRNOTAVAIL
;
848 /* sanity for overflow - beware unsigned */
850 struct in6_addrlifetime_64
*lt
;
852 lt
= (struct in6_addrlifetime_64
*)
853 &ifr
->ifr_ifru
.ifru_lifetime
;
854 if (lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
855 && lt
->ia6t_vltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
859 if (lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
860 && lt
->ia6t_pltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
865 struct in6_addrlifetime_32
*lt
;
867 lt
= (struct in6_addrlifetime_32
*)
868 &ifr
->ifr_ifru
.ifru_lifetime
;
869 if (lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
870 && lt
->ia6t_vltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
874 if (lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
875 && lt
->ia6t_pltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
884 case SIOCGIFADDR_IN6
:
885 ifr
->ifr_addr
= ia
->ia_addr
;
888 case SIOCGIFDSTADDR_IN6
:
889 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0) {
894 * XXX: should we check if ifa_dstaddr is NULL and return
897 ifr
->ifr_dstaddr
= ia
->ia_dstaddr
;
900 case SIOCGIFNETMASK_IN6
:
901 ifr
->ifr_addr
= ia
->ia_prefixmask
;
904 case SIOCGIFAFLAG_IN6
:
905 ifr
->ifr_ifru
.ifru_flags6
= ia
->ia6_flags
;
908 case SIOCGIFSTAT_IN6
:
913 index
= ifp
->if_index
;
914 lck_mtx_lock(ip6_mutex
);
915 if (in6_ifstat
== NULL
|| index
>= in6_ifstatmax
916 || in6_ifstat
[index
] == NULL
) {
917 /* return EAFNOSUPPORT? */
918 bzero(&ifr
->ifr_ifru
.ifru_stat
,
919 sizeof (ifr
->ifr_ifru
.ifru_stat
));
921 ifr
->ifr_ifru
.ifru_stat
= *in6_ifstat
[index
];
923 lck_mtx_unlock(ip6_mutex
);
926 case SIOCGIFSTAT_ICMP6
:
931 index
= ifp
->if_index
;
932 lck_mtx_lock(ip6_mutex
);
933 if (icmp6_ifstat
== NULL
|| index
>= icmp6_ifstatmax
||
934 icmp6_ifstat
[index
] == NULL
) {
935 /* return EAFNOSUPPORT? */
936 bzero(&ifr
->ifr_ifru
.ifru_stat
,
937 sizeof (ifr
->ifr_ifru
.ifru_icmp6stat
));
939 ifr
->ifr_ifru
.ifru_icmp6stat
= *icmp6_ifstat
[index
];
941 lck_mtx_unlock(ip6_mutex
);
944 case SIOCGIFALIFETIME_IN6
:
946 struct in6_addrlifetime_64
*lt
;
948 lt
= (struct in6_addrlifetime_64
*)
949 &ifr
->ifr_ifru
.ifru_lifetime
;
950 lt
->ia6t_expire
= ia
->ia6_lifetime
.ia6t_expire
;
951 lt
->ia6t_preferred
= ia
->ia6_lifetime
.ia6t_preferred
;
952 lt
->ia6t_vltime
= ia
->ia6_lifetime
.ia6t_vltime
;
953 lt
->ia6t_pltime
= ia
->ia6_lifetime
.ia6t_pltime
;
955 struct in6_addrlifetime_32
*lt
;
957 lt
= (struct in6_addrlifetime_32
*)
958 &ifr
->ifr_ifru
.ifru_lifetime
;
960 (uint32_t)ia
->ia6_lifetime
.ia6t_expire
;
962 (uint32_t)ia
->ia6_lifetime
.ia6t_preferred
;
964 (uint32_t)ia
->ia6_lifetime
.ia6t_vltime
;
966 (uint32_t)ia
->ia6_lifetime
.ia6t_pltime
;
970 case SIOCSIFALIFETIME_IN6
:
972 struct in6_addrlifetime_64
*lt
;
974 lt
= (struct in6_addrlifetime_64
*)
975 &ifr
->ifr_ifru
.ifru_lifetime
;
976 ia
->ia6_lifetime
.ia6t_expire
= lt
->ia6t_expire
;
977 ia
->ia6_lifetime
.ia6t_preferred
= lt
->ia6t_preferred
;
978 ia
->ia6_lifetime
.ia6t_vltime
= lt
->ia6t_vltime
;
979 ia
->ia6_lifetime
.ia6t_pltime
= lt
->ia6t_pltime
;
981 struct in6_addrlifetime_32
*lt
;
983 lt
= (struct in6_addrlifetime_32
*)
984 &ifr
->ifr_ifru
.ifru_lifetime
;
985 ia
->ia6_lifetime
.ia6t_expire
=
986 (uint32_t)lt
->ia6t_expire
;
987 ia
->ia6_lifetime
.ia6t_preferred
=
988 (uint32_t)lt
->ia6t_preferred
;
989 ia
->ia6_lifetime
.ia6t_vltime
= lt
->ia6t_vltime
;
990 ia
->ia6_lifetime
.ia6t_pltime
= lt
->ia6t_pltime
;
993 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
994 ia
->ia6_lifetime
.ia6t_expire
=
995 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_vltime
;
997 ia
->ia6_lifetime
.ia6t_expire
= 0;
998 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
999 ia
->ia6_lifetime
.ia6t_preferred
=
1000 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_pltime
;
1002 ia
->ia6_lifetime
.ia6t_preferred
= 0;
1005 case SIOCAIFADDR_IN6_32
:
1006 case SIOCAIFADDR_IN6_64
: {
1008 struct nd_prefix pr0
, *pr
;
1010 /* Attempt to attache the protocol, in case it isn't attached */
1011 error
= proto_plumb(PF_INET6
, ifp
);
1013 if (error
!= EEXIST
) {
1014 printf("SIOCAIFADDR_IN6: %s can't plumb "
1015 "protocol error=%d\n", if_name(ifp
), error
);
1019 /* Ignore, EEXIST */
1022 /* PF_INET6 wasn't previously attached */
1023 if ((error
= in6_if_up(ifp
, NULL
)) != 0)
1028 * first, make or update the interface address structure,
1029 * and link it to the list.
1031 if ((error
= in6_update_ifa(ifp
, ifra
, ia
, M_WAITOK
)) != 0)
1035 * then, make the prefix on-link on the interface.
1036 * XXX: we'd rather create the prefix before the address, but
1037 * we need at least one address to install the corresponding
1038 * interface route, so we configure the address first.
1042 * convert mask to prefix length (prefixmask has already
1043 * been validated in in6_update_ifa().
1045 bzero(&pr0
, sizeof(pr0
));
1047 pr0
.ndpr_plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
1049 if (pr0
.ndpr_plen
== 128)
1050 break; /* we don't need to install a host route. */
1051 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
1052 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
1053 /* apply the mask for safety. */
1054 for (i
= 0; i
< 4; i
++) {
1055 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1056 ifra
->ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
1059 * XXX: since we don't have an API to set prefix (not address)
1060 * lifetimes, we just use the same lifetimes as addresses.
1061 * The (temporarily) installed lifetimes can be overridden by
1062 * later advertised RAs (when accept_rtadv is non 0), which is
1063 * an intended behavior.
1065 pr0
.ndpr_raf_onlink
= 1; /* should be configurable? */
1067 ((ifra
->ifra_flags
& IN6_IFF_AUTOCONF
) != 0);
1068 pr0
.ndpr_vltime
= ifra
->ifra_lifetime
.ia6t_vltime
;
1069 pr0
.ndpr_pltime
= ifra
->ifra_lifetime
.ia6t_pltime
;
1071 /* add the prefix if there's one. */
1072 if ((pr
= nd6_prefix_lookup(&pr0
)) == NULL
) {
1074 * nd6_prelist_add will install the corresponding
1077 if ((error
= nd6_prelist_add(&pr0
, NULL
, &pr
)) != 0)
1080 log(LOG_ERR
, "nd6_prelist_add succedded but "
1087 ifafree(&ia
->ia_ifa
);
1088 if ((ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
))
1090 /* XXX: this should not happen! */
1091 log(LOG_ERR
, "in6_control: addition succeeded, but"
1094 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
1095 ia
->ia6_ndpr
== NULL
) { /* new autoconfed addr */
1096 lck_mtx_lock(nd6_mutex
);
1098 lck_mtx_unlock(nd6_mutex
);
1102 * If this is the first autoconf address from
1103 * the prefix, create a temporary address
1104 * as well (when specified).
1106 if (ip6_use_tempaddr
&&
1107 pr
->ndpr_refcnt
== 1) {
1109 if ((e
= in6_tmpifadd(ia
, 1,
1111 log(LOG_NOTICE
, "in6_control: "
1112 "failed to create a "
1113 "temporary address, "
1121 * this might affect the status of autoconfigured
1122 * addresses, that is, this address might make
1123 * other addresses detached.
1125 pfxlist_onlink_check(0);
1128 /* Drop use count held above during lookup/add */
1129 ndpr_rele(pr
, FALSE
);
1131 pf_ifaddr_hook(ifp
, cmd
);
1136 case SIOCDIFADDR_IN6
: {
1138 struct nd_prefix pr0
, *pr
;
1141 * If the address being deleted is the only one that owns
1142 * the corresponding prefix, expire the prefix as well.
1143 * XXX: theoretically, we don't have to warry about such
1144 * relationship, since we separate the address management
1145 * and the prefix management. We do this, however, to provide
1146 * as much backward compatibility as possible in terms of
1147 * the ioctl operation.
1149 bzero(&pr0
, sizeof(pr0
));
1151 pr0
.ndpr_plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
1153 if (pr0
.ndpr_plen
== 128)
1155 pr0
.ndpr_prefix
= ia
->ia_addr
;
1156 pr0
.ndpr_mask
= ia
->ia_prefixmask
.sin6_addr
;
1157 for (i
= 0; i
< 4; i
++) {
1158 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
1159 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
];
1162 * The logic of the following condition is a bit complicated.
1163 * We expire the prefix when
1164 * 1. the address obeys autoconfiguration and it is the
1165 * only owner of the associated prefix, or
1166 * 2. the address does not obey autoconf and there is no
1167 * other owner of the prefix.
1169 if ((pr
= nd6_prefix_lookup(&pr0
)) != NULL
&&
1170 (((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
1171 pr
->ndpr_refcnt
== 1) ||
1172 ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 &&
1173 pr
->ndpr_refcnt
== 0))) {
1174 pr
->ndpr_expire
= 1; /* XXX: just for expiration */
1177 /* Drop use count held above during lookup */
1179 ndpr_rele(pr
, FALSE
);
1182 in6_purgeaddr(&ia
->ia_ifa
, 0);
1184 pf_ifaddr_hook(ifp
, cmd
);
1190 error
= ifnet_ioctl(ifp
, PF_INET6
, cmd
, data
);
1195 ifafree(&ia
->ia_ifa
);
1200 * Update parameters of an IPv6 interface address.
1201 * If necessary, a new entry is created and linked into address chains.
1202 * This function is separated from in6_control().
1203 * XXX: should this be performed under splnet()?
1206 in6_update_ifa(ifp
, ifra
, ia
, how
)
1208 struct in6_aliasreq
*ifra
;
1209 struct in6_ifaddr
*ia
;
1212 int error
= 0, hostIsNew
= 0, plen
= -1;
1213 struct in6_ifaddr
*oia
;
1214 struct sockaddr_in6 dst6
;
1215 struct in6_addrlifetime
*lt
;
1216 struct timeval timenow
;
1219 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
1220 /* Validate parameters */
1221 if (ifp
== NULL
|| ifra
== NULL
) /* this maybe redundant */
1225 * The destination address for a p2p link must have a family
1226 * of AF_UNSPEC or AF_INET6.
1228 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
1229 ifra
->ifra_dstaddr
.sin6_family
!= AF_INET6
&&
1230 ifra
->ifra_dstaddr
.sin6_family
!= AF_UNSPEC
)
1231 return(EAFNOSUPPORT
);
1233 * validate ifra_prefixmask. don't check sin6_family, netmask
1234 * does not carry fields other than sin6_len.
1236 if (ifra
->ifra_prefixmask
.sin6_len
> sizeof(struct sockaddr_in6
))
1239 * Set the address family value for the mask if it was not set.
1242 if (ifra
->ifra_prefixmask
.sin6_len
== sizeof(struct sockaddr_in6
) &&
1243 ifra
->ifra_prefixmask
.sin6_family
== 0) {
1244 ifra
->ifra_prefixmask
.sin6_family
= AF_INET6
;
1247 * Because the IPv6 address architecture is classless, we require
1248 * users to specify a (non 0) prefix length (mask) for a new address.
1249 * We also require the prefix (when specified) mask is valid, and thus
1250 * reject a non-consecutive mask.
1252 if (ia
== NULL
&& ifra
->ifra_prefixmask
.sin6_len
== 0)
1254 if (ifra
->ifra_prefixmask
.sin6_len
!= 0) {
1255 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
1256 (u_char
*)&ifra
->ifra_prefixmask
+
1257 ifra
->ifra_prefixmask
.sin6_len
);
1263 * In this case, ia must not be NULL. We just use its prefix
1266 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
);
1269 * If the destination address on a p2p interface is specified,
1270 * and the address is a scoped one, validate/set the scope
1273 dst6
= ifra
->ifra_dstaddr
;
1274 if ((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) &&
1275 (dst6
.sin6_family
== AF_INET6
)) {
1278 #ifndef SCOPEDROUTING
1279 if ((error
= in6_recoverscope(&dst6
,
1280 &ifra
->ifra_dstaddr
.sin6_addr
,
1284 scopeid
= in6_addr2scopeid(ifp
, &dst6
.sin6_addr
);
1285 if (dst6
.sin6_scope_id
== 0) /* user omit to specify the ID. */
1286 dst6
.sin6_scope_id
= scopeid
;
1287 else if (dst6
.sin6_scope_id
!= scopeid
)
1288 return(EINVAL
); /* scope ID mismatch. */
1289 #ifndef SCOPEDROUTING
1290 if ((error
= in6_embedscope(&dst6
.sin6_addr
, &dst6
, NULL
, NULL
))
1293 dst6
.sin6_scope_id
= 0; /* XXX */
1297 * The destination address can be specified only for a p2p or a
1298 * loopback interface. If specified, the corresponding prefix length
1301 if (ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
) {
1302 if ((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) == 0) {
1303 /* XXX: noisy message */
1304 log(LOG_INFO
, "in6_update_ifa: a destination can be "
1305 "specified for a p2p or a loopback IF only\n");
1310 * The following message seems noisy, but we dare to
1311 * add it for diagnosis.
1313 log(LOG_INFO
, "in6_update_ifa: prefixlen must be 128 "
1314 "when dstaddr is specified\n");
1318 /* lifetime consistency check */
1320 getmicrotime(&timenow
);
1321 lt
= &ifra
->ifra_lifetime
;
1322 if (lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
1323 && lt
->ia6t_vltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
1326 if (lt
->ia6t_vltime
== 0) {
1328 * the following log might be noisy, but this is a typical
1329 * configuration mistake or a tool's bug.
1332 "in6_update_ifa: valid lifetime is 0 for %s\n",
1333 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
1335 if (lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
1336 && lt
->ia6t_pltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
1341 * If this is a new address, allocate a new ifaddr and link it
1347 * in6_update_ifa() may be called in a process of a received
1348 * RA; in such a case, we should call malloc with M_NOWAIT.
1349 * The exception to this is during init time or as part of
1350 * handling an ioctl, when we know it's okay to do M_WAITOK.
1352 ia
= in6_ifaddr_alloc(how
);
1355 /* Initialize the address and masks */
1356 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
1357 ia
->ia_addr
.sin6_family
= AF_INET6
;
1358 ia
->ia_addr
.sin6_len
= sizeof(ia
->ia_addr
);
1359 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) != 0) {
1361 * XXX: some functions expect that ifa_dstaddr is not
1362 * NULL for p2p interfaces.
1364 ia
->ia_ifa
.ifa_dstaddr
1365 = (struct sockaddr
*)&ia
->ia_dstaddr
;
1367 ia
->ia_ifa
.ifa_dstaddr
= NULL
;
1369 ia
->ia_ifa
.ifa_netmask
1370 = (struct sockaddr
*)&ia
->ia_prefixmask
;
1373 ifaref(&ia
->ia_ifa
);
1374 lck_mtx_lock(nd6_mutex
);
1375 if ((oia
= in6_ifaddrs
) != NULL
) {
1376 for ( ; oia
->ia_next
; oia
= oia
->ia_next
)
1381 lck_mtx_unlock(nd6_mutex
);
1383 ifnet_lock_exclusive(ifp
);
1384 if_attach_ifa(ifp
, &ia
->ia_ifa
);
1385 ifnet_lock_done(ifp
);
1388 /* set prefix mask */
1389 if (ifra
->ifra_prefixmask
.sin6_len
) {
1391 * We prohibit changing the prefix length of an existing
1393 * + such an operation should be rare in IPv6, and
1394 * + the operation would confuse prefix management.
1396 if (ia
->ia_prefixmask
.sin6_len
&&
1397 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
) != plen
) {
1398 log(LOG_INFO
, "in6_update_ifa: the prefix length of an"
1399 " existing (%s) address should not be changed\n",
1400 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1404 ia
->ia_prefixmask
= ifra
->ifra_prefixmask
;
1408 * If a new destination address is specified, scrub the old one and
1409 * install the new destination. Note that the interface must be
1410 * p2p or loopback (see the check above.)
1412 if (dst6
.sin6_family
== AF_INET6
&&
1413 !IN6_ARE_ADDR_EQUAL(&dst6
.sin6_addr
,
1414 &ia
->ia_dstaddr
.sin6_addr
)) {
1417 if ((ia
->ia_flags
& IFA_ROUTE
) != 0 &&
1418 (e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
))
1420 log(LOG_ERR
, "in6_update_ifa: failed to remove "
1421 "a route to the old destination: %s\n",
1422 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1423 /* proceed anyway... */
1426 ia
->ia_flags
&= ~IFA_ROUTE
;
1427 ia
->ia_dstaddr
= dst6
;
1430 /* reset the interface and routing table appropriately. */
1431 if ((error
= in6_ifinit(ifp
, ia
, &ifra
->ifra_addr
, hostIsNew
)) != 0)
1435 * Beyond this point, we should call in6_purgeaddr upon an error,
1436 * not just go to unlink.
1439 #if 0 /* disable this mechanism for now */
1440 /* update prefix list */
1442 (ifra
->ifra_flags
& IN6_IFF_NOPFX
) == 0) { /* XXX */
1445 iilen
= (sizeof(ia
->ia_prefixmask
.sin6_addr
) << 3) - plen
;
1446 if ((error
= in6_prefix_add_ifid(iilen
, ia
)) != 0) {
1447 in6_purgeaddr((struct ifaddr
*)ia
, 0);
1453 if ((ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1454 struct sockaddr_in6 mltaddr
, mltmask
;
1455 struct in6_multi
*in6m
;
1459 * join solicited multicast addr for new host id
1461 struct in6_addr llsol
;
1462 bzero(&llsol
, sizeof(struct in6_addr
));
1463 llsol
.s6_addr16
[0] = htons(0xff02);
1464 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1465 llsol
.s6_addr32
[1] = 0;
1466 llsol
.s6_addr32
[2] = htonl(1);
1467 llsol
.s6_addr32
[3] =
1468 ifra
->ifra_addr
.sin6_addr
.s6_addr32
[3];
1469 llsol
.s6_addr8
[12] = 0xff;
1470 (void)in6_addmulti(&llsol
, ifp
, &error
, 0);
1473 "in6_update_ifa: addmulti failed for "
1474 "%s on %s (errno=%d)\n",
1475 ip6_sprintf(&llsol
), if_name(ifp
),
1477 in6_purgeaddr((struct ifaddr
*)ia
, 0);
1482 bzero(&mltmask
, sizeof(mltmask
));
1483 mltmask
.sin6_len
= sizeof(struct sockaddr_in6
);
1484 mltmask
.sin6_family
= AF_INET6
;
1485 mltmask
.sin6_addr
= in6mask32
;
1488 * join link-local all-nodes address
1490 bzero(&mltaddr
, sizeof(mltaddr
));
1491 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
1492 mltaddr
.sin6_family
= AF_INET6
;
1493 mltaddr
.sin6_addr
= in6addr_linklocal_allnodes
;
1494 mltaddr
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
1496 ifnet_lock_shared(ifp
);
1497 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1498 ifnet_lock_done(ifp
);
1501 (struct sockaddr
*)&mltaddr
,
1502 (struct sockaddr
*)&ia
->ia_addr
,
1503 (struct sockaddr
*)&mltmask
,
1504 RTF_UP
|RTF_CLONING
, /* xxx */
1505 (struct rtentry
**)0);
1506 (void)in6_addmulti(&mltaddr
.sin6_addr
, ifp
, &error
, 0);
1509 "in6_update_ifa: addmulti failed for "
1510 "%s on %s (errno=%d)\n",
1511 ip6_sprintf(&mltaddr
.sin6_addr
),
1512 if_name(ifp
), error
);
1517 * join node information group address
1519 #define hostnamelen strlen(hostname)
1520 if (in6_nigroup(ifp
, hostname
, hostnamelen
, &mltaddr
.sin6_addr
)
1522 ifnet_lock_shared(ifp
);
1523 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1524 ifnet_lock_done(ifp
);
1525 if (in6m
== NULL
&& ia
!= NULL
) {
1526 (void)in6_addmulti(&mltaddr
.sin6_addr
,
1529 log(LOG_WARNING
, "in6_update_ifa: "
1530 "addmulti failed for "
1531 "%s on %s (errno=%d)\n",
1532 ip6_sprintf(&mltaddr
.sin6_addr
),
1533 if_name(ifp
), error
);
1540 * join node-local all-nodes address, on loopback.
1541 * XXX: since "node-local" is obsoleted by interface-local,
1542 * we have to join the group on every interface with
1543 * some interface-boundary restriction.
1545 if (ifp
->if_flags
& IFF_LOOPBACK
) {
1546 struct in6_ifaddr
*ia_loop
;
1548 struct in6_addr loop6
= in6addr_loopback
;
1549 ia_loop
= in6ifa_ifpwithaddr(ifp
, &loop6
);
1551 mltaddr
.sin6_addr
= in6addr_nodelocal_allnodes
;
1553 ifnet_lock_shared(ifp
);
1554 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1555 ifnet_lock_done(ifp
);
1556 if (in6m
== NULL
&& ia_loop
!= NULL
) {
1558 (struct sockaddr
*)&mltaddr
,
1559 (struct sockaddr
*)&ia_loop
->ia_addr
,
1560 (struct sockaddr
*)&mltmask
,
1562 (struct rtentry
**)0);
1563 (void)in6_addmulti(&mltaddr
.sin6_addr
, ifp
,
1566 log(LOG_WARNING
, "in6_update_ifa: "
1567 "addmulti failed for %s on %s "
1569 ip6_sprintf(&mltaddr
.sin6_addr
),
1570 if_name(ifp
), error
);
1573 if (ia_loop
!= NULL
)
1574 ifafree(&ia_loop
->ia_ifa
);
1578 ia
->ia6_flags
= ifra
->ifra_flags
;
1579 ia
->ia6_flags
&= ~IN6_IFF_DUPLICATED
; /*safety*/
1580 ia
->ia6_flags
&= ~IN6_IFF_NODAD
; /* Mobile IPv6 */
1582 ia
->ia6_lifetime
= ifra
->ifra_lifetime
;
1584 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
1585 ia
->ia6_lifetime
.ia6t_expire
=
1586 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_vltime
;
1588 ia
->ia6_lifetime
.ia6t_expire
= 0;
1589 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
1590 ia
->ia6_lifetime
.ia6t_preferred
=
1591 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_pltime
;
1593 ia
->ia6_lifetime
.ia6t_preferred
= 0;
1596 * make sure to initialize ND6 information. this is to workaround
1597 * issues with interfaces with IPv6 addresses, which have never brought
1598 * up. We are assuming that it is safe to nd6_ifattach multiple times.
1600 if ((error
= nd6_ifattach(ifp
)) != 0)
1604 * Perform DAD, if needed.
1605 * XXX It may be of use, if we can administratively
1608 if (in6if_do_dad(ifp
) && (ifra
->ifra_flags
& IN6_IFF_NODAD
) == 0) {
1609 ia
->ia6_flags
|= IN6_IFF_TENTATIVE
;
1610 nd6_dad_start((struct ifaddr
*)ia
, NULL
);
1617 * XXX: if a change of an existing address failed, keep the entry
1621 in6_unlink_ifa(ia
, ifp
, 0);
1627 struct ifaddr
*ifa
, int nd6_locked
)
1629 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1630 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*) ifa
;
1632 /* stop DAD processing */
1636 * delete route to the destination of the address being purged.
1637 * The interface must be p2p or loopback in this case.
1639 if ((ia
->ia_flags
& IFA_ROUTE
) != 0 && ia
->ia_dstaddr
.sin6_len
!= 0) {
1642 if ((e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
))
1644 log(LOG_ERR
, "in6_purgeaddr: failed to remove "
1645 "a route to the p2p destination: %s on %s, "
1647 ip6_sprintf(&ia
->ia_addr
.sin6_addr
), if_name(ifp
),
1649 /* proceed anyway... */
1652 ia
->ia_flags
&= ~IFA_ROUTE
;
1655 /* Remove ownaddr's loopback rtentry, if it exists. */
1656 in6_ifremloop(&(ia
->ia_ifa
), nd6_locked
);
1658 if (ifp
->if_flags
& IFF_MULTICAST
) {
1660 * delete solicited multicast addr for deleting host id
1662 struct in6_multi
*in6m
;
1663 struct in6_addr llsol
;
1664 bzero(&llsol
, sizeof(struct in6_addr
));
1665 llsol
.s6_addr16
[0] = htons(0xff02);
1666 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1667 llsol
.s6_addr32
[1] = 0;
1668 llsol
.s6_addr32
[2] = htonl(1);
1669 llsol
.s6_addr32
[3] =
1670 ia
->ia_addr
.sin6_addr
.s6_addr32
[3];
1671 llsol
.s6_addr8
[12] = 0xff;
1673 ifnet_lock_shared(ifp
);
1674 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1675 ifnet_lock_done(ifp
);
1677 in6_delmulti(in6m
, nd6_locked
);
1680 in6_unlink_ifa(ia
, ifp
, nd6_locked
);
1681 in6_post_msg(ifp
, KEV_INET6_ADDR_DELETED
, ia
);
1685 in6_unlink_ifa(ia
, ifp
, nd6_locked
)
1686 struct in6_ifaddr
*ia
;
1691 struct in6_ifaddr
*oia
;
1693 ifnet_lock_exclusive(ifp
);
1694 if_detach_ifa(ifp
, &ia
->ia_ifa
);
1695 ifnet_lock_done(ifp
);
1698 lck_mtx_lock(nd6_mutex
);
1700 if (oia
== (ia
= in6_ifaddrs
))
1701 in6_ifaddrs
= ia
->ia_next
;
1703 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
1706 ia
->ia_next
= oia
->ia_next
;
1709 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1712 if (oia
->ia6_ifpr
) { /* check for safety */
1713 plen
= in6_mask2len(&oia
->ia_prefixmask
.sin6_addr
, NULL
);
1714 iilen
= (sizeof(oia
->ia_prefixmask
.sin6_addr
) << 3) - plen
;
1715 in6_prefix_remove_ifid(iilen
, oia
);
1719 * When an autoconfigured address is being removed, release the
1720 * reference to the base prefix. Also, since the release might
1721 * affect the status of other (detached) addresses, call
1722 * pfxlist_onlink_check().
1724 if ((oia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0) {
1725 if (oia
->ia6_ndpr
== NULL
) {
1726 log(LOG_NOTICE
, "in6_unlink_ifa: autoconf'ed address "
1727 "%p has no prefix\n", oia
);
1729 oia
->ia6_ndpr
->ndpr_refcnt
--;
1730 oia
->ia6_flags
&= ~IN6_IFF_AUTOCONF
;
1731 oia
->ia6_ndpr
= NULL
;
1734 pfxlist_onlink_check(1);
1737 lck_mtx_unlock(nd6_mutex
);
1741 * release another refcnt for the link from in6_ifaddrs.
1742 * Note that we should decrement the refcnt at least once for all *BSD.
1744 ifafree(&oia
->ia_ifa
);
1752 struct in6_ifaddr
*ia
, *nia
= NULL
;
1754 if (ifp
== NULL
|| &ifp
->if_addrlist
== NULL
)
1757 lck_mtx_lock(nd6_mutex
);
1758 for (ia
= in6_ifaddrs
; ia
!= NULL
; ia
= nia
)
1761 if (ia
->ia_ifa
.ifa_ifp
!= ifp
)
1763 in6_purgeaddr(&ia
->ia_ifa
, 1);
1765 lck_mtx_unlock(nd6_mutex
);
1772 * SIOCGLIFADDR: get first address. (?)
1773 * SIOCGLIFADDR with IFLR_PREFIX:
1774 * get first address that matches the specified prefix.
1775 * SIOCALIFADDR: add the specified address.
1776 * SIOCALIFADDR with IFLR_PREFIX:
1777 * add the specified prefix, filling hostaddr part from
1778 * the first link-local address. prefixlen must be <= 64.
1779 * SIOCDLIFADDR: delete the specified address.
1780 * SIOCDLIFADDR with IFLR_PREFIX:
1781 * delete the first address that matches the specified prefix.
1783 * EINVAL on invalid parameters
1784 * EADDRNOTAVAIL on prefix match failed/specified address not found
1785 * other values may be returned from in6_ioctl()
1787 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1788 * this is to accomodate address naming scheme other than RFC2374,
1790 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1791 * address encoding scheme. (see figure on page 8)
1794 in6_lifaddr_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
1795 struct ifnet
*ifp
, struct proc
*p
)
1797 struct if_laddrreq
*iflr
= (struct if_laddrreq
*)data
;
1798 struct ifaddr
*ifa
= NULL
;
1799 struct sockaddr
*sa
;
1800 int p64
= proc_is64bit(p
);
1803 if (!data
|| !ifp
) {
1804 panic("invalid argument to in6_lifaddr_ioctl");
1810 /* address must be specified on GET with IFLR_PREFIX */
1811 if ((iflr
->flags
& IFLR_PREFIX
) == 0)
1816 /* address must be specified on ADD and DELETE */
1817 sa
= (struct sockaddr
*)&iflr
->addr
;
1818 if (sa
->sa_family
!= AF_INET6
)
1820 if (sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1822 /* XXX need improvement */
1823 sa
= (struct sockaddr
*)&iflr
->dstaddr
;
1824 if (sa
->sa_family
&& sa
->sa_family
!= AF_INET6
)
1826 if (sa
->sa_len
&& sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1829 default: /* shouldn't happen */
1831 panic("invalid cmd to in6_lifaddr_ioctl");
1837 if (sizeof(struct in6_addr
) * 8 < iflr
->prefixlen
)
1843 struct in6_aliasreq ifra
;
1844 struct in6_addr hostaddr
;
1846 int hostid_found
= 0;
1848 if ((iflr
->flags
& IFLR_PREFIX
) != 0) {
1849 struct sockaddr_in6
*sin6
;
1852 * hostaddr is to fill in the hostaddr part of the
1853 * address. hostaddr points to the first link-local
1854 * address attached to the interface.
1856 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
, 0);
1858 return EADDRNOTAVAIL
;
1859 hostaddr
= *IFA_IN6(ifa
);
1864 /* prefixlen must be <= 64. */
1865 if (64 < iflr
->prefixlen
)
1867 prefixlen
= iflr
->prefixlen
;
1869 /* hostaddr part must be zero. */
1870 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1871 if (sin6
->sin6_addr
.s6_addr32
[2] != 0
1872 || sin6
->sin6_addr
.s6_addr32
[3] != 0) {
1876 prefixlen
= iflr
->prefixlen
;
1878 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1879 bzero(&ifra
, sizeof(ifra
));
1880 bcopy(iflr
->iflr_name
, ifra
.ifra_name
, sizeof(ifra
.ifra_name
));
1882 bcopy(&iflr
->addr
, &ifra
.ifra_addr
,
1883 ((struct sockaddr
*)&iflr
->addr
)->sa_len
);
1885 /* fill in hostaddr part */
1886 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] =
1887 hostaddr
.s6_addr32
[2];
1888 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] =
1889 hostaddr
.s6_addr32
[3];
1892 if (((struct sockaddr
*)&iflr
->dstaddr
)->sa_family
) { /*XXX*/
1893 bcopy(&iflr
->dstaddr
, &ifra
.ifra_dstaddr
,
1894 ((struct sockaddr
*)&iflr
->dstaddr
)->sa_len
);
1896 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[2] =
1897 hostaddr
.s6_addr32
[2];
1898 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[3] =
1899 hostaddr
.s6_addr32
[3];
1903 ifra
.ifra_prefixmask
.sin6_len
= sizeof(struct sockaddr_in6
);
1904 in6_len2mask(&ifra
.ifra_prefixmask
.sin6_addr
, prefixlen
);
1906 ifra
.ifra_flags
= iflr
->flags
& ~IFLR_PREFIX
;
1908 #if defined(__LP64__)
1909 struct in6_aliasreq_32 ifra_32
;
1911 * Use 32-bit ioctl and structure for 32-bit process.
1913 in6_aliasreq_64_to_32((struct in6_aliasreq_64
*)&ifra
,
1915 return (in6_control(so
, SIOCAIFADDR_IN6_32
,
1916 (caddr_t
)&ifra_32
, ifp
, p
));
1918 return (in6_control(so
, SIOCAIFADDR_IN6
,
1919 (caddr_t
)&ifra
, ifp
, p
));
1920 #endif /* __LP64__ */
1922 #if defined(__LP64__)
1923 return (in6_control(so
, SIOCAIFADDR_IN6
,
1924 (caddr_t
)&ifra
, ifp
, p
));
1926 struct in6_aliasreq_64 ifra_64
;
1928 * Use 64-bit ioctl and structure for 64-bit process.
1930 in6_aliasreq_32_to_64((struct in6_aliasreq_32
*)&ifra
,
1932 return (in6_control(so
, SIOCAIFADDR_IN6_64
,
1933 (caddr_t
)&ifra_64
, ifp
, p
));
1934 #endif /* __LP64__ */
1941 struct in6_ifaddr
*ia
;
1942 struct in6_addr mask
, candidate
, match
;
1943 struct sockaddr_in6
*sin6
;
1946 bzero(&mask
, sizeof(mask
));
1947 if (iflr
->flags
& IFLR_PREFIX
) {
1948 /* lookup a prefix rather than address. */
1949 in6_len2mask(&mask
, iflr
->prefixlen
);
1951 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1952 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
1953 match
.s6_addr32
[0] &= mask
.s6_addr32
[0];
1954 match
.s6_addr32
[1] &= mask
.s6_addr32
[1];
1955 match
.s6_addr32
[2] &= mask
.s6_addr32
[2];
1956 match
.s6_addr32
[3] &= mask
.s6_addr32
[3];
1958 /* if you set extra bits, that's wrong */
1959 if (bcmp(&match
, &sin6
->sin6_addr
, sizeof(match
)))
1964 if (cmd
== SIOCGLIFADDR
) {
1965 /* on getting an address, take the 1st match */
1968 /* on deleting an address, do exact match */
1969 in6_len2mask(&mask
, 128);
1970 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1971 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
1977 ifnet_lock_shared(ifp
);
1978 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1980 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1985 bcopy(IFA_IN6(ifa
), &candidate
, sizeof(candidate
));
1986 #ifndef SCOPEDROUTING
1988 * XXX: this is adhoc, but is necessary to allow
1989 * a user to specify fe80::/64 (not /10) for a
1990 * link-local address.
1992 if (IN6_IS_ADDR_LINKLOCAL(&candidate
))
1993 candidate
.s6_addr16
[1] = 0;
1995 candidate
.s6_addr32
[0] &= mask
.s6_addr32
[0];
1996 candidate
.s6_addr32
[1] &= mask
.s6_addr32
[1];
1997 candidate
.s6_addr32
[2] &= mask
.s6_addr32
[2];
1998 candidate
.s6_addr32
[3] &= mask
.s6_addr32
[3];
1999 if (IN6_ARE_ADDR_EQUAL(&candidate
, &match
))
2002 ifnet_lock_done(ifp
);
2004 return EADDRNOTAVAIL
;
2007 if (cmd
== SIOCGLIFADDR
) {
2008 #ifndef SCOPEDROUTING
2009 struct sockaddr_in6
*s6
;
2012 /* fill in the if_laddrreq structure */
2013 bcopy(&ia
->ia_addr
, &iflr
->addr
, ia
->ia_addr
.sin6_len
);
2014 #ifndef SCOPEDROUTING /* XXX see above */
2015 s6
= (struct sockaddr_in6
*)&iflr
->addr
;
2016 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
2017 s6
->sin6_addr
.s6_addr16
[1] = 0;
2019 in6_addr2scopeid(ifp
, &s6
->sin6_addr
);
2022 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
2023 bcopy(&ia
->ia_dstaddr
, &iflr
->dstaddr
,
2024 ia
->ia_dstaddr
.sin6_len
);
2025 #ifndef SCOPEDROUTING /* XXX see above */
2026 s6
= (struct sockaddr_in6
*)&iflr
->dstaddr
;
2027 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
2028 s6
->sin6_addr
.s6_addr16
[1] = 0;
2030 in6_addr2scopeid(ifp
,
2035 bzero(&iflr
->dstaddr
, sizeof(iflr
->dstaddr
));
2038 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
2041 iflr
->flags
= ia
->ia6_flags
; /* XXX */
2045 struct in6_aliasreq ifra
;
2047 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
2048 bzero(&ifra
, sizeof(ifra
));
2049 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
2050 sizeof(ifra
.ifra_name
));
2052 bcopy(&ia
->ia_addr
, &ifra
.ifra_addr
,
2053 ia
->ia_addr
.sin6_len
);
2054 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
2055 bcopy(&ia
->ia_dstaddr
, &ifra
.ifra_dstaddr
,
2056 ia
->ia_dstaddr
.sin6_len
);
2058 bzero(&ifra
.ifra_dstaddr
,
2059 sizeof(ifra
.ifra_dstaddr
));
2061 bcopy(&ia
->ia_prefixmask
, &ifra
.ifra_dstaddr
,
2062 ia
->ia_prefixmask
.sin6_len
);
2064 ifra
.ifra_flags
= ia
->ia6_flags
;
2066 #if defined(__LP64__)
2067 struct in6_aliasreq_32 ifra_32
;
2069 * Use 32-bit structure for 32-bit process.
2070 * SIOCDIFADDR_IN6 is encoded with in6_ifreq,
2071 * so it stays the same since the size does
2072 * not change. The data part of the ioctl,
2073 * however, is of a different structure, i.e.
2076 in6_aliasreq_64_to_32(
2077 (struct in6_aliasreq_64
*)&ifra
, &ifra_32
);
2078 return (in6_control(so
, SIOCDIFADDR_IN6
,
2079 (caddr_t
)&ifra_32
, ifp
, p
));
2081 return (in6_control(so
, SIOCDIFADDR_IN6
,
2082 (caddr_t
)&ifra
, ifp
, p
));
2083 #endif /* __LP64__ */
2085 #if defined(__LP64__)
2086 return (in6_control(so
, SIOCDIFADDR_IN6
,
2087 (caddr_t
)&ifra
, ifp
, p
));
2089 struct in6_aliasreq_64 ifra_64
;
2091 * Use 64-bit structure for 64-bit process.
2092 * SIOCDIFADDR_IN6 is encoded with in6_ifreq,
2093 * so it stays the same since the size does
2094 * not change. The data part of the ioctl,
2095 * however, is of a different structure, i.e.
2098 in6_aliasreq_32_to_64(
2099 (struct in6_aliasreq_32
*)&ifra
, &ifra_64
);
2100 return (in6_control(so
, SIOCDIFADDR_IN6
,
2101 (caddr_t
)&ifra_64
, ifp
, p
));
2102 #endif /* __LP64__ */
2109 return EOPNOTSUPP
; /* just for safety */
2113 * Initialize an interface's intetnet6 address
2114 * and routing table entry.
2117 in6_ifinit(ifp
, ia
, sin6
, newhost
)
2119 struct in6_ifaddr
*ia
;
2120 struct sockaddr_in6
*sin6
;
2123 int error
= 0, plen
, ifacount
= 0;
2127 * Give the interface a chance to initialize
2128 * if this is its first address,
2129 * and to validate the address if necessary.
2131 ifnet_lock_shared(ifp
);
2132 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2134 if (ifa
->ifa_addr
== NULL
)
2135 continue; /* just for safety */
2136 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2140 ifnet_lock_done(ifp
);
2142 ia
->ia_addr
= *sin6
;
2145 if (ifacount
<= 1 &&
2146 (error
= ifnet_ioctl(ifp
, PF_INET6
, SIOCSIFADDR
, ia
))) {
2152 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
2154 /* we could do in(6)_socktrim here, but just omit it at this moment. */
2158 * If the destination address is specified for a point-to-point
2159 * interface, install a route to the destination as an interface
2162 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
); /* XXX */
2163 if (plen
== 128 && ia
->ia_dstaddr
.sin6_family
== AF_INET6
) {
2164 if ((error
= rtinit(&(ia
->ia_ifa
), (int)RTM_ADD
,
2165 RTF_UP
| RTF_HOST
)) != 0)
2167 ia
->ia_flags
|= IFA_ROUTE
;
2171 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
2173 ia
->ia_ifa
.ifa_flags
|= RTF_CLONING
;
2176 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
2178 /* set the rtrequest function to create llinfo */
2179 ia
->ia_ifa
.ifa_rtrequest
= nd6_rtrequest
;
2180 in6_ifaddloop(&(ia
->ia_ifa
));
2187 * Add an address to the list of IP6 multicast addresses for a
2191 in6_addmulti(maddr6
, ifp
, errorp
, nd6_locked
)
2192 struct in6_addr
*maddr6
;
2197 struct in6_multi
*in6m
;
2198 struct sockaddr_in6 sin6
;
2199 struct ifmultiaddr
*ifma
;
2204 * Call generic routine to add membership or increment
2205 * refcount. It wants addresses in the form of a sockaddr,
2206 * so we build one here (being careful to zero the unused bytes).
2208 bzero(&sin6
, sizeof sin6
);
2209 sin6
.sin6_family
= AF_INET6
;
2210 sin6
.sin6_len
= sizeof sin6
;
2211 sin6
.sin6_addr
= *maddr6
;
2212 *errorp
= if_addmulti(ifp
, (struct sockaddr
*)&sin6
, &ifma
);
2218 * If ifma->ifma_protospec is null, then if_addmulti() created
2219 * a new record. Otherwise, we are done.
2221 if (ifma
->ifma_protospec
!= 0)
2222 return ifma
->ifma_protospec
;
2224 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
2225 at interrupt time? If so, need to fix if_addmulti. XXX */
2226 in6m
= (struct in6_multi
*)_MALLOC(sizeof(*in6m
), M_IPMADDR
, M_NOWAIT
);
2231 bzero(in6m
, sizeof *in6m
);
2232 in6m
->in6m_addr
= *maddr6
;
2233 in6m
->in6m_ifp
= ifp
;
2234 in6m
->in6m_ifma
= ifma
;
2235 ifma
->ifma_protospec
= in6m
;
2236 if (nd6_locked
== 0)
2237 lck_mtx_lock(nd6_mutex
);
2238 LIST_INSERT_HEAD(&in6_multihead
, in6m
, in6m_entry
);
2239 if (nd6_locked
== 0)
2240 lck_mtx_unlock(nd6_mutex
);
2243 * Let MLD6 know that we have joined a new IP6 multicast
2246 mld6_start_listening(in6m
);
2251 * Delete a multicast address record.
2255 struct in6_multi
*in6m
, int nd6locked
)
2257 struct ifmultiaddr
*ifma
= in6m
->in6m_ifma
;
2259 if (ifma
&& ifma
->ifma_usecount
== 1) {
2261 * No remaining claims to this record; let MLD6 know
2262 * that we are leaving the multicast group.
2264 mld6_stop_listening(in6m
);
2265 ifma
->ifma_protospec
= 0;
2267 lck_mtx_lock(nd6_mutex
);
2268 LIST_REMOVE(in6m
, in6m_entry
);
2270 lck_mtx_unlock(nd6_mutex
);
2271 FREE(in6m
, M_IPMADDR
);
2273 /* XXX - should be separate API for when we have an ifma? */
2275 if_delmultiaddr(ifma
, 0);
2281 * Find an IPv6 interface link-local address specific to an interface.
2284 in6ifa_ifpforlinklocal(ifp
, ignoreflags
)
2290 ifnet_lock_shared(ifp
);
2291 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2293 if (ifa
->ifa_addr
== NULL
)
2294 continue; /* just for safety */
2295 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2297 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
))) {
2298 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2306 ifnet_lock_done(ifp
);
2308 return((struct in6_ifaddr
*)ifa
);
2312 * find the internet address corresponding to a given interface and address.
2315 in6ifa_ifpwithaddr(ifp
, addr
)
2317 struct in6_addr
*addr
;
2321 ifnet_lock_shared(ifp
);
2322 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2324 if (ifa
->ifa_addr
== NULL
)
2325 continue; /* just for safety */
2326 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2328 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(ifa
)))
2333 ifnet_lock_done(ifp
);
2335 return((struct in6_ifaddr
*)ifa
);
2339 * Convert IP6 address to printable (loggable) representation.
2341 static char digits
[] = "0123456789abcdef";
2342 static int ip6round
= 0;
2345 const struct in6_addr
*addr
;
2347 static char ip6buf
[8][48];
2350 const u_short
*a
= (const u_short
*)addr
;
2354 ip6round
= (ip6round
+ 1) & 7;
2355 cp
= ip6buf
[ip6round
];
2357 for (i
= 0; i
< 8; i
++) {
2368 if (dcolon
== 0 && *(a
+ 1) == 0) {
2380 d
= (const u_char
*)a
;
2381 *cp
++ = digits
[*d
>> 4];
2382 *cp
++ = digits
[*d
++ & 0xf];
2383 *cp
++ = digits
[*d
>> 4];
2384 *cp
++ = digits
[*d
& 0xf];
2389 return(ip6buf
[ip6round
]);
2393 in6addr_local(struct in6_addr
*in6
)
2396 struct sockaddr_in6 sin6
;
2399 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
))
2402 sin6
.sin6_family
= AF_INET6
;
2403 sin6
.sin6_len
= sizeof (sin6
);
2404 bcopy(in6
, &sin6
.sin6_addr
, sizeof (*in6
));
2405 rt
= rtalloc1((struct sockaddr
*)&sin6
, 0, 0);
2409 if (rt
->rt_gateway
->sa_family
== AF_LINK
)
2414 local
= in6_localaddr(in6
);
2421 struct in6_addr
*in6
;
2423 struct in6_ifaddr
*ia
;
2425 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
))
2428 lck_mtx_lock(nd6_mutex
);
2429 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
)
2430 if (IN6_ARE_MASKED_ADDR_EQUAL(in6
, &ia
->ia_addr
.sin6_addr
,
2431 &ia
->ia_prefixmask
.sin6_addr
)) {
2432 lck_mtx_unlock(nd6_mutex
);
2436 lck_mtx_unlock(nd6_mutex
);
2441 in6_is_addr_deprecated(sa6
)
2442 struct sockaddr_in6
*sa6
;
2444 struct in6_ifaddr
*ia
;
2446 lck_mtx_lock(nd6_mutex
);
2447 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2448 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
,
2451 ia
->ia_addr
.sin6_scope_id
== sa6
->sin6_scope_id
&&
2453 (ia
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0) {
2454 lck_mtx_unlock(nd6_mutex
);
2455 return(1); /* true */
2458 /* XXX: do we still have to go thru the rest of the list? */
2461 lck_mtx_unlock(nd6_mutex
);
2462 return(0); /* false */
2466 * return length of part which dst and src are equal
2470 in6_matchlen(src
, dst
)
2471 struct in6_addr
*src
, *dst
;
2474 u_char
*s
= (u_char
*)src
, *d
= (u_char
*)dst
;
2475 u_char
*lim
= s
+ 16, r
;
2478 if ((r
= (*d
++ ^ *s
++)) != 0) {
2489 /* XXX: to be scope conscious */
2491 in6_are_prefix_equal(p1
, p2
, len
)
2492 struct in6_addr
*p1
, *p2
;
2495 int bytelen
, bitlen
;
2498 if (0 > len
|| len
> 128) {
2499 log(LOG_ERR
, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2507 if (bcmp(&p1
->s6_addr
, &p2
->s6_addr
, bytelen
))
2510 p1
->s6_addr
[bytelen
] >> (8 - bitlen
) !=
2511 p2
->s6_addr
[bytelen
] >> (8 - bitlen
))
2518 in6_prefixlen2mask(maskp
, len
)
2519 struct in6_addr
*maskp
;
2522 u_char maskarray
[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2523 int bytelen
, bitlen
, i
;
2526 if (0 > len
|| len
> 128) {
2527 log(LOG_ERR
, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2532 bzero(maskp
, sizeof(*maskp
));
2535 for (i
= 0; i
< bytelen
; i
++)
2536 maskp
->s6_addr
[i
] = 0xff;
2538 maskp
->s6_addr
[bytelen
] = maskarray
[bitlen
- 1];
2542 * return the best address out of the same scope
2547 struct in6_addr
*dst
)
2549 int dst_scope
= in6_addrscope(dst
), src_scope
, best_scope
= 0;
2553 struct in6_ifaddr
*ifa_best
= NULL
;
2557 printf("in6_ifawithscope: output interface is not specified\n");
2563 * We search for all addresses on all interfaces from the beginning.
2564 * Comparing an interface with the outgoing interface will be done
2565 * only at the final stage of tiebreaking.
2567 ifnet_head_lock_shared();
2568 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
2570 * We can never take an address that breaks the scope zone
2571 * of the destination.
2573 if (in6_addr2scopeid(ifp
, dst
) != in6_addr2scopeid(oifp
, dst
))
2576 ifnet_lock_shared(ifp
);
2577 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2579 int tlen
= -1, dscopecmp
, bscopecmp
, matchcmp
;
2581 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2584 src_scope
= in6_addrscope(IFA_IN6(ifa
));
2587 * Don't use an address before completing DAD
2588 * nor a duplicated address.
2590 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2594 /* XXX: is there any case to allow anycasts? */
2595 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2599 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2604 * If this is the first address we find,
2607 if (ifa_best
== NULL
)
2611 * ifa_best is never NULL beyond this line except
2612 * within the block labeled "replace".
2616 * If ifa_best has a smaller scope than dst and
2617 * the current address has a larger one than
2618 * (or equal to) dst, always replace ifa_best.
2619 * Also, if the current address has a smaller scope
2620 * than dst, ignore it unless ifa_best also has a
2622 * Consequently, after the two if-clause below,
2623 * the followings must be satisfied:
2624 * (scope(src) < scope(dst) &&
2625 * scope(best) < scope(dst))
2627 * (scope(best) >= scope(dst) &&
2628 * scope(src) >= scope(dst))
2630 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0 &&
2631 IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) >= 0)
2632 goto replace
; /* (A) */
2633 if (IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) < 0 &&
2634 IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) >= 0)
2638 * A deprecated address SHOULD NOT be used in new
2639 * communications if an alternate (non-deprecated)
2640 * address is available and has sufficient scope.
2641 * RFC 2462, Section 5.5.4.
2643 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2644 IN6_IFF_DEPRECATED
) {
2646 * Ignore any deprecated addresses if
2647 * specified by configuration.
2649 if (!ip6_use_deprecated
)
2653 * If we have already found a non-deprecated
2654 * candidate, just ignore deprecated addresses.
2656 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
)
2662 * A non-deprecated address is always preferred
2663 * to a deprecated one regardless of scopes and
2664 * address matching (Note invariants ensured by the
2665 * conditions (A) and (B) above.)
2667 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) &&
2668 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2669 IN6_IFF_DEPRECATED
) == 0)
2673 * When we use temporary addresses described in
2674 * RFC 3041, we prefer temporary addresses to
2675 * public autoconf addresses. Again, note the
2676 * invariants from (A) and (B). Also note that we
2677 * don't have any preference between static addresses
2678 * and autoconf addresses (despite of whether or not
2679 * the latter is temporary or public.)
2681 if (ip6_use_tempaddr
) {
2682 struct in6_ifaddr
*ifat
;
2684 ifat
= (struct in6_ifaddr
*)ifa
;
2685 if ((ifa_best
->ia6_flags
&
2686 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2687 == IN6_IFF_AUTOCONF
&&
2689 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2690 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
)) {
2693 if ((ifa_best
->ia6_flags
&
2694 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2695 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
) &&
2697 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2698 == IN6_IFF_AUTOCONF
) {
2704 * At this point, we have two cases:
2705 * 1. we are looking at a non-deprecated address,
2706 * and ifa_best is also non-deprecated.
2707 * 2. we are looking at a deprecated address,
2708 * and ifa_best is also deprecated.
2709 * Also, we do not have to consider a case where
2710 * the scope of if_best is larger(smaller) than dst and
2711 * the scope of the current address is smaller(larger)
2712 * than dst. Such a case has already been covered.
2713 * Tiebreaking is done according to the following
2715 * - the scope comparison between the address and
2717 * - the scope comparison between the address and
2718 * ifa_best (bscopecmp)
2719 * - if the address match dst longer than ifa_best
2721 * - if the address is on the outgoing I/F (outI/F)
2723 * Roughly speaking, the selection policy is
2724 * - the most important item is scope. The same scope
2725 * is best. Then search for a larger scope.
2726 * Smaller scopes are the last resort.
2727 * - A deprecated address is chosen only when we have
2728 * no address that has an enough scope, but is
2729 * prefered to any addresses of smaller scopes
2730 * (this must be already done above.)
2731 * - addresses on the outgoing I/F are preferred to
2732 * ones on other interfaces if none of above
2733 * tiebreaks. In the table below, the column "bI"
2734 * means if the best_ifa is on the outgoing
2735 * interface, and the column "sI" means if the ifa
2736 * is on the outgoing interface.
2737 * - If there is no other reasons to choose one,
2738 * longest address match against dst is considered.
2740 * The precise decision table is as follows:
2741 * dscopecmp bscopecmp match bI oI | replace?
2742 * N/A equal N/A Y N | No (1)
2743 * N/A equal N/A N Y | Yes (2)
2744 * N/A equal larger N/A | Yes (3)
2745 * N/A equal !larger N/A | No (4)
2746 * larger larger N/A N/A | No (5)
2747 * larger smaller N/A N/A | Yes (6)
2748 * smaller larger N/A N/A | Yes (7)
2749 * smaller smaller N/A N/A | No (8)
2750 * equal smaller N/A N/A | Yes (9)
2751 * equal larger (already done at A above)
2753 dscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
);
2754 bscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, best_scope
);
2756 if (bscopecmp
== 0) {
2757 struct ifnet
*bifp
= ifa_best
->ia_ifp
;
2759 if (bifp
== oifp
&& ifp
!= oifp
) /* (1) */
2761 if (bifp
!= oifp
&& ifp
== oifp
) /* (2) */
2765 * Both bifp and ifp are on the outgoing
2766 * interface, or both two are on a different
2767 * interface from the outgoing I/F.
2768 * now we need address matching against dst
2771 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
2772 matchcmp
= tlen
- blen
;
2773 if (matchcmp
> 0) /* (3) */
2777 if (dscopecmp
> 0) {
2778 if (bscopecmp
> 0) /* (5) */
2780 goto replace
; /* (6) */
2782 if (dscopecmp
< 0) {
2783 if (bscopecmp
> 0) /* (7) */
2788 /* now dscopecmp must be 0 */
2790 goto replace
; /* (9) */
2795 ifafree(&ifa_best
->ia_ifa
);
2796 ifa_best
= (struct in6_ifaddr
*)ifa
;
2797 blen
= tlen
>= 0 ? tlen
:
2798 in6_matchlen(IFA_IN6(ifa
), dst
);
2799 best_scope
= in6_addrscope(&ifa_best
->ia_addr
.sin6_addr
);
2801 ifnet_lock_done(ifp
);
2805 /* count statistics for future improvements */
2806 if (ifa_best
== NULL
)
2807 ip6stat
.ip6s_sources_none
++;
2809 if (oifp
== ifa_best
->ia_ifp
)
2810 ip6stat
.ip6s_sources_sameif
[best_scope
]++;
2812 ip6stat
.ip6s_sources_otherif
[best_scope
]++;
2814 if (best_scope
== dst_scope
)
2815 ip6stat
.ip6s_sources_samescope
[best_scope
]++;
2817 ip6stat
.ip6s_sources_otherscope
[best_scope
]++;
2819 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0)
2820 ip6stat
.ip6s_sources_deprecated
[best_scope
]++;
2827 * return the best address out of the same scope. if no address was
2828 * found, return the first valid address from designated IF.
2833 struct in6_addr
*dst
)
2835 int dst_scope
= in6_addrscope(dst
), blen
= -1, tlen
;
2837 struct in6_ifaddr
*besta
= 0;
2838 struct in6_ifaddr
*dep
[2]; /* last-resort: deprecated */
2840 dep
[0] = dep
[1] = NULL
;
2843 * We first look for addresses in the same scope.
2844 * If there is one, return it.
2845 * If two or more, return one which matches the dst longest.
2846 * If none, return one of global addresses assigned other ifs.
2848 ifnet_lock_shared(ifp
);
2849 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2851 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2853 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
)
2854 continue; /* XXX: is there any case to allow anycast? */
2855 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_NOTREADY
)
2856 continue; /* don't use this interface */
2857 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DETACHED
)
2859 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
2860 if (ip6_use_deprecated
) {
2862 ifafree(&dep
[0]->ia_ifa
);
2863 dep
[0] = (struct in6_ifaddr
*)ifa
;
2869 if (dst_scope
== in6_addrscope(IFA_IN6(ifa
))) {
2871 * call in6_matchlen() as few as possible
2875 blen
= in6_matchlen(&besta
->ia_addr
.sin6_addr
, dst
);
2876 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
2879 besta
= (struct in6_ifaddr
*)ifa
;
2882 besta
= (struct in6_ifaddr
*)ifa
;
2886 ifaref(&besta
->ia_ifa
);
2887 ifnet_lock_done(ifp
);
2889 ifafree(&dep
[0]->ia_ifa
);
2893 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2895 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2897 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
)
2898 continue; /* XXX: is there any case to allow anycast? */
2899 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_NOTREADY
)
2900 continue; /* don't use this interface */
2901 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DETACHED
)
2903 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
2904 if (ip6_use_deprecated
) {
2906 ifafree(&dep
[1]->ia_ifa
);
2907 dep
[1] = (struct in6_ifaddr
*)ifa
;
2914 ifnet_lock_done(ifp
);
2916 ifafree(&dep
[0]->ia_ifa
);
2918 ifafree(&dep
[1]->ia_ifa
);
2919 return (struct in6_ifaddr
*)ifa
;
2921 ifnet_lock_done(ifp
);
2923 /* use the last-resort values, that are, deprecated addresses */
2926 ifafree(&dep
[1]->ia_ifa
);
2936 * perform DAD when interface becomes IFF_UP.
2941 struct in6_aliasreq
*ifra
)
2944 struct in6_ifaddr
*ia
;
2945 int dad_delay
; /* delay ticks before DAD output */
2952 * special cases, like 6to4, are handled in in6_ifattach
2954 error
= in6_ifattach(ifp
, NULL
, ifra
);
2959 ifnet_lock_exclusive(ifp
);
2960 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2962 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2964 ia
= (struct in6_ifaddr
*)ifa
;
2965 if (ia
->ia6_flags
& IN6_IFF_TENTATIVE
)
2966 nd6_dad_start(ifa
, &dad_delay
);
2968 ifnet_lock_done(ifp
);
2977 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0)
2980 switch (ifp
->if_type
) {
2986 * These interfaces do not have the IFF_LOOPBACK flag,
2987 * but loop packets back. We do not have to do DAD on such
2988 * interfaces. We should even omit it, because loop-backed
2989 * NS would confuse the DAD procedure.
2994 * Our DAD routine requires the interface up and running.
2995 * However, some interfaces can be up before the RUNNING
2996 * status. Additionaly, users may try to assign addresses
2997 * before the interface becomes up (or running).
2998 * We simply skip DAD in such a case as a work around.
2999 * XXX: we should rather mark "tentative" on such addresses,
3000 * and do DAD after the interface becomes ready.
3002 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) !=
3003 (IFF_UP
|IFF_RUNNING
))
3011 * Calculate max IPv6 MTU through all the interfaces and store it
3017 u_int32_t maxmtu
= 0;
3020 ifnet_head_lock_shared();
3021 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
3022 lck_rw_lock_shared(nd_if_rwlock
);
3023 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
3024 IN6_LINKMTU(ifp
) > maxmtu
)
3025 maxmtu
= IN6_LINKMTU(ifp
);
3026 lck_rw_done(nd_if_rwlock
);
3029 if (maxmtu
) /* update only when maxmtu is positive */
3030 in6_maxmtu
= maxmtu
;
3034 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
3035 * v4 mapped addr or v4 compat addr
3038 in6_sin6_2_sin(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3040 bzero(sin
, sizeof(*sin
));
3041 sin
->sin_len
= sizeof(struct sockaddr_in
);
3042 sin
->sin_family
= AF_INET
;
3043 sin
->sin_port
= sin6
->sin6_port
;
3044 sin
->sin_addr
.s_addr
= sin6
->sin6_addr
.s6_addr32
[3];
3047 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
3049 in6_sin_2_v4mapsin6(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
3051 bzero(sin6
, sizeof(*sin6
));
3052 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
3053 sin6
->sin6_family
= AF_INET6
;
3054 sin6
->sin6_port
= sin
->sin_port
;
3055 sin6
->sin6_addr
.s6_addr32
[0] = 0;
3056 sin6
->sin6_addr
.s6_addr32
[1] = 0;
3057 sin6
->sin6_addr
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
3058 sin6
->sin6_addr
.s6_addr32
[3] = sin
->sin_addr
.s_addr
;
3061 /* Convert sockaddr_in6 into sockaddr_in. */
3063 in6_sin6_2_sin_in_sock(struct sockaddr
*nam
)
3065 struct sockaddr_in
*sin_p
;
3066 struct sockaddr_in6 sin6
;
3069 * Save original sockaddr_in6 addr and convert it
3072 sin6
= *(struct sockaddr_in6
*)nam
;
3073 sin_p
= (struct sockaddr_in
*)nam
;
3074 in6_sin6_2_sin(sin_p
, &sin6
);
3077 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
3079 in6_sin_2_v4mapsin6_in_sock(struct sockaddr
**nam
)
3081 struct sockaddr_in
*sin_p
;
3082 struct sockaddr_in6
*sin6_p
;
3084 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof *sin6_p
, M_SONAME
,
3088 sin_p
= (struct sockaddr_in
*)*nam
;
3089 in6_sin_2_v4mapsin6(sin_p
, sin6_p
);
3090 FREE(*nam
, M_SONAME
);
3091 *nam
= (struct sockaddr
*)sin6_p
;
3097 * Posts in6_event_data message kernel events.
3099 * To get the same size of kev_in6_data between ILP32 and LP64 data models
3100 * we are using a special version of the in6_addrlifetime structure that
3101 * uses only 32 bits fields to be compatible with Leopard, and that
3102 * are large enough to span 68 years.
3105 in6_post_msg(struct ifnet
*ifp
, u_int32_t event_code
, struct in6_ifaddr
*ifa
)
3107 struct kev_msg ev_msg
;
3108 struct kev_in6_data in6_event_data
;
3110 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
3111 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
3112 ev_msg
.kev_subclass
= KEV_INET6_SUBCLASS
;
3113 ev_msg
.event_code
= event_code
;
3115 in6_event_data
.ia_addr
= ifa
->ia_addr
;
3116 in6_event_data
.ia_net
= ifa
->ia_net
;
3117 in6_event_data
.ia_dstaddr
= ifa
->ia_dstaddr
;
3118 in6_event_data
.ia_prefixmask
= ifa
->ia_prefixmask
;
3119 in6_event_data
.ia_plen
= ifa
->ia_plen
;
3120 in6_event_data
.ia6_flags
= (u_int32_t
)ifa
->ia6_flags
;
3122 in6_event_data
.ia_lifetime
.ia6t_expire
=
3123 ifa
->ia6_lifetime
.ia6t_expire
;
3124 in6_event_data
.ia_lifetime
.ia6t_preferred
=
3125 ifa
->ia6_lifetime
.ia6t_preferred
;
3126 in6_event_data
.ia_lifetime
.ia6t_vltime
=
3127 ifa
->ia6_lifetime
.ia6t_vltime
;
3128 in6_event_data
.ia_lifetime
.ia6t_pltime
=
3129 ifa
->ia6_lifetime
.ia6t_pltime
;
3132 strncpy(&in6_event_data
.link_data
.if_name
[0],
3133 ifp
->if_name
, IFNAMSIZ
);
3134 in6_event_data
.link_data
.if_family
= ifp
->if_family
;
3135 in6_event_data
.link_data
.if_unit
= (u_int32_t
) ifp
->if_unit
;
3138 ev_msg
.dv
[0].data_ptr
= &in6_event_data
;
3139 ev_msg
.dv
[0].data_length
= sizeof (in6_event_data
);
3140 ev_msg
.dv
[1].data_length
= 0;
3142 kev_post_msg(&ev_msg
);
3146 * Called as part of ip6_init
3149 in6_ifaddr_init(void)
3151 PE_parse_boot_argn("ifa_debug", &in6ifa_debug
, sizeof (in6ifa_debug
));
3153 in6ifa_size
= (in6ifa_debug
== 0) ? sizeof (struct in6_ifaddr
) :
3154 sizeof (struct in6_ifaddr_dbg
);
3156 in6ifa_zone
= zinit(in6ifa_size
, IN6IFA_ZONE_MAX
* in6ifa_size
,
3157 0, IN6IFA_ZONE_NAME
);
3158 if (in6ifa_zone
== NULL
)
3159 panic("%s: failed allocating %s", __func__
, IN6IFA_ZONE_NAME
);
3161 zone_change(in6ifa_zone
, Z_EXPAND
, TRUE
);
3164 static struct in6_ifaddr
*
3165 in6_ifaddr_alloc(int how
)
3167 struct in6_ifaddr
*in6ifa
;
3169 in6ifa
= (how
== M_WAITOK
) ? zalloc(in6ifa_zone
) :
3170 zalloc_noblock(in6ifa_zone
);
3171 if (in6ifa
!= NULL
) {
3172 bzero(in6ifa
, in6ifa_size
);
3173 in6ifa
->ia_ifa
.ifa_free
= in6_ifaddr_free
;
3174 in6ifa
->ia_ifa
.ifa_debug
|= IFD_ALLOC
;
3175 if (in6ifa_debug
!= 0) {
3176 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3177 (struct in6_ifaddr_dbg
*)in6ifa
;
3178 in6ifa
->ia_ifa
.ifa_debug
|= IFD_DEBUG
;
3179 in6ifa
->ia_ifa
.ifa_trace
= in6_ifaddr_trace
;
3180 ctrace_record(&in6ifa_dbg
->in6ifa_alloc
);
3187 in6_ifaddr_free(struct ifaddr
*ifa
)
3189 if (ifa
->ifa_refcnt
!= 0)
3190 panic("%s: ifa %p bad ref cnt", __func__
, ifa
);
3191 if (!(ifa
->ifa_debug
& IFD_ALLOC
))
3192 panic("%s: ifa %p cannot be freed", __func__
, ifa
);
3194 if (ifa
->ifa_debug
& IFD_DEBUG
) {
3195 struct in6_ifaddr_dbg
*in6ifa_dbg
=
3196 (struct in6_ifaddr_dbg
*)ifa
;
3197 ctrace_record(&in6ifa_dbg
->in6ifa_free
);
3198 bcopy(&in6ifa_dbg
->in6ifa
, &in6ifa_dbg
->in6ifa_old
,
3199 sizeof (struct in6_ifaddr
));
3201 bzero(ifa
, sizeof (struct in6_ifaddr
));
3202 zfree(in6ifa_zone
, ifa
);
3206 in6_ifaddr_trace(struct ifaddr
*ifa
, int refhold
)
3208 struct in6_ifaddr_dbg
*in6ifa_dbg
= (struct in6_ifaddr_dbg
*)ifa
;
3213 if (!(ifa
->ifa_debug
& IFD_DEBUG
))
3214 panic("%s: ifa %p has no debug structure", __func__
, ifa
);
3217 cnt
= &in6ifa_dbg
->in6ifa_refhold_cnt
;
3218 tr
= in6ifa_dbg
->in6ifa_refhold
;
3220 cnt
= &in6ifa_dbg
->in6ifa_refrele_cnt
;
3221 tr
= in6ifa_dbg
->in6ifa_refrele
;
3224 idx
= OSAddAtomic16(1, (volatile SInt16
*)cnt
) % CTRACE_HIST_SIZE
;
3225 ctrace_record(&tr
[idx
]);