1 /* $FreeBSD: src/sys/netinet6/in6.c,v 1.7.2.7 2001/08/06 20:26:22 ume Exp $ */
2 /* $KAME: in6.c,v 1.187 2001/05/24 07:43:59 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)in.c 8.2 (Berkeley) 11/15/93
69 #include <sys/param.h>
70 #include <sys/ioctl.h>
71 #include <sys/errno.h>
72 #include <sys/malloc.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/sockio.h>
76 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/syslog.h>
80 #include <sys/kern_event.h>
81 #include <kern/lock.h>
84 #include <net/if_types.h>
85 #include <net/route.h>
86 #include <net/if_dl.h>
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/if_ether.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/in_pcb.h>
97 #include <netinet6/nd6.h>
98 #include <netinet/ip6.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/mld6_var.h>
101 #include <netinet6/ip6_mroute.h>
102 #include <netinet6/in6_ifattach.h>
103 #include <netinet6/scope6_var.h>
104 #ifndef SCOPEDROUTING
105 #include <netinet6/in6_pcb.h>
108 #include <net/net_osdep.h>
111 MALLOC_DEFINE(M_IPMADDR
, "in6_multi", "internet multicast address");
114 * Definitions of some costant IP6 addresses.
116 const struct in6_addr in6addr_any
= IN6ADDR_ANY_INIT
;
117 const struct in6_addr in6addr_loopback
= IN6ADDR_LOOPBACK_INIT
;
118 const struct in6_addr in6addr_nodelocal_allnodes
=
119 IN6ADDR_NODELOCAL_ALLNODES_INIT
;
120 const struct in6_addr in6addr_linklocal_allnodes
=
121 IN6ADDR_LINKLOCAL_ALLNODES_INIT
;
122 const struct in6_addr in6addr_linklocal_allrouters
=
123 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT
;
125 const struct in6_addr in6mask0
= IN6MASK0
;
126 const struct in6_addr in6mask32
= IN6MASK32
;
127 const struct in6_addr in6mask64
= IN6MASK64
;
128 const struct in6_addr in6mask96
= IN6MASK96
;
129 const struct in6_addr in6mask128
= IN6MASK128
;
131 const struct sockaddr_in6 sa6_any
= {sizeof(sa6_any
), AF_INET6
,
132 0, 0, IN6ADDR_ANY_INIT
, 0};
134 static int in6_lifaddr_ioctl(struct socket
*, u_long
, caddr_t
,
135 struct ifnet
*, struct proc
*);
136 static int in6_ifinit(struct ifnet
*, struct in6_ifaddr
*,
137 struct sockaddr_in6
*, int);
138 static void in6_unlink_ifa(struct in6_ifaddr
*, struct ifnet
*, int);
140 struct in6_multihead in6_multihead
; /* XXX BSS initialization */
141 extern struct lck_mtx_t
*nd6_mutex
;
144 * Subroutine for in6_ifaddloop() and in6_ifremloop().
145 * This routine does actual work.
148 in6_ifloop_request(int cmd
, struct ifaddr
*ifa
)
150 struct sockaddr_in6 all1_sa
;
151 struct rtentry
*nrt
= NULL
;
154 bzero(&all1_sa
, sizeof(all1_sa
));
155 all1_sa
.sin6_family
= AF_INET6
;
156 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
157 all1_sa
.sin6_addr
= in6mask128
;
160 * We specify the address itself as the gateway, and set the
161 * RTF_LLINFO flag, so that the corresponding host route would have
162 * the flag, and thus applications that assume traditional behavior
163 * would be happy. Note that we assume the caller of the function
164 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
165 * which changes the outgoing interface to the loopback interface.
167 e
= rtrequest_locked(cmd
, ifa
->ifa_addr
, ifa
->ifa_addr
,
168 (struct sockaddr
*)&all1_sa
,
169 RTF_UP
|RTF_HOST
|RTF_LLINFO
, &nrt
);
171 log(LOG_ERR
, "in6_ifloop_request: "
172 "%s operation failed for %s (errno=%d)\n",
173 cmd
== RTM_ADD
? "ADD" : "DELETE",
174 ip6_sprintf(&((struct in6_ifaddr
*)ifa
)->ia_addr
.sin6_addr
),
179 * Make sure rt_ifa be equal to IFA, the second argument of the
181 * We need this because when we refer to rt_ifa->ia6_flags in
182 * ip6_input, we assume that the rt_ifa points to the address instead
183 * of the loopback address.
185 if (cmd
== RTM_ADD
&& nrt
&& ifa
!= nrt
->rt_ifa
) {
190 * Report the addition/removal of the address to the routing socket.
191 * XXX: since we called rtinit for a p2p interface with a destination,
192 * we end up reporting twice in such a case. Should we rather
193 * omit the second report?
196 rt_newaddrmsg(cmd
, ifa
, e
, nrt
);
197 if (cmd
== RTM_DELETE
) {
198 if (nrt
->rt_refcnt
<= 0) {
199 /* XXX: we should free the entry ourselves. */
204 /* the cmd must be RTM_ADD here */
211 * Add ownaddr as loopback rtentry. We previously add the route only if
212 * necessary (ex. on a p2p link). However, since we now manage addresses
213 * separately from prefixes, we should always add the route. We can't
214 * rely on the cloning mechanism from the corresponding interface route
218 in6_ifaddloop(struct ifaddr
*ifa
)
222 lck_mtx_lock(rt_mtx
);
223 /* If there is no loopback entry, allocate one. */
224 rt
= rtalloc1_locked(ifa
->ifa_addr
, 0, 0);
225 if (rt
== NULL
|| (rt
->rt_flags
& RTF_HOST
) == 0 ||
226 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) == 0)
227 in6_ifloop_request(RTM_ADD
, ifa
);
230 lck_mtx_unlock(rt_mtx
);
234 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
238 in6_ifremloop(struct ifaddr
*ifa
, int locked
)
240 struct in6_ifaddr
*ia
;
245 * Some of BSD variants do not remove cloned routes
246 * from an interface direct route, when removing the direct route
247 * (see comments in net/net_osdep.h). Even for variants that do remove
248 * cloned routes, they could fail to remove the cloned routes when
249 * we handle multple addresses that share a common prefix.
250 * So, we should remove the route corresponding to the deleted address
251 * regardless of the result of in6_is_ifloop_auto().
255 * Delete the entry only if exact one ifa exists. More than one ifa
256 * can exist if we assign a same single address to multiple
257 * (probably p2p) interfaces.
258 * XXX: we should avoid such a configuration in IPv6...
261 lck_mtx_lock(nd6_mutex
);
262 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
263 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa
), &ia
->ia_addr
.sin6_addr
)) {
270 lck_mtx_unlock(nd6_mutex
);
274 * Before deleting, check if a corresponding loopbacked host
275 * route surely exists. With this check, we can avoid to
276 * delete an interface direct route whose destination is same
277 * as the address being removed. This can happen when remofing
278 * a subnet-router anycast address on an interface attahced
279 * to a shared medium.
281 lck_mtx_lock(rt_mtx
);
282 rt
= rtalloc1_locked(ifa
->ifa_addr
, 0, 0);
283 if (rt
!= NULL
&& (rt
->rt_flags
& RTF_HOST
) != 0 &&
284 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
286 in6_ifloop_request(RTM_DELETE
, ifa
);
288 lck_mtx_unlock(rt_mtx
);
293 in6_ifindex2scopeid(idx
)
298 struct sockaddr_in6
*sin6
;
300 if (idx
< 0 || if_index
< idx
)
303 ifnet_head_lock_shared();
304 ifp
= ifindex2ifnet
[idx
];
307 ifnet_lock_shared(ifp
);
308 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
310 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
312 sin6
= (struct sockaddr_in6
*)ifa
->ifa_addr
;
313 if (IN6_IS_ADDR_SITELOCAL(&sin6
->sin6_addr
)) {
314 ifnet_lock_done(ifp
);
315 return sin6
->sin6_scope_id
& 0xffff;
318 ifnet_lock_done(ifp
);
324 in6_mask2len(mask
, lim0
)
325 struct in6_addr
*mask
;
329 u_char
*lim
= lim0
, *p
;
332 lim0
- (u_char
*)mask
> sizeof(*mask
)) /* ignore the scope_id part */
333 lim
= (u_char
*)mask
+ sizeof(*mask
);
334 for (p
= (u_char
*)mask
; p
< lim
; x
++, p
++) {
340 for (y
= 0; y
< 8; y
++) {
341 if ((*p
& (0x80 >> y
)) == 0)
347 * when the limit pointer is given, do a stricter check on the
351 if (y
!= 0 && (*p
& (0x00ff >> y
)) != 0)
353 for (p
= p
+ 1; p
< lim
; p
++)
362 in6_len2mask(mask
, len
)
363 struct in6_addr
*mask
;
368 bzero(mask
, sizeof(*mask
));
369 for (i
= 0; i
< len
/ 8; i
++)
370 mask
->s6_addr8
[i
] = 0xff;
372 mask
->s6_addr8
[i
] = (0xff00 >> (len
% 8)) & 0xff;
375 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
376 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
379 in6_control(so
, cmd
, data
, ifp
, p
)
386 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)data
;
387 struct in6_ifaddr
*ia
= NULL
;
388 struct in6_aliasreq
*ifra
= (struct in6_aliasreq
*)data
;
389 int privileged
, error
= 0;
391 struct timeval timenow
;
393 getmicrotime(&timenow
);
397 if (p
== NULL
|| !proc_suser(p
))
399 if (p
== NULL
|| !suser(p
))
404 case SIOCGETSGCNT_IN6
:
405 case SIOCGETMIFCNT_IN6
:
406 return (mrt6_ioctl(cmd
, data
));
413 case SIOCAUTOCONF_START
:
414 case SIOCAUTOCONF_STOP
:
417 case SIOCPROTOATTACH_IN6
:
418 case SIOCPROTODETACH_IN6
:
422 case SIOCSNDFLUSH_IN6
:
423 case SIOCSPFXFLUSH_IN6
:
424 case SIOCSRTRFLUSH_IN6
:
425 case SIOCSDEFIFACE_IN6
:
426 case SIOCSIFINFO_FLAGS
:
430 case OSIOCGIFINFO_IN6
:
431 case SIOCGIFINFO_IN6
:
434 case SIOCGNBRINFO_IN6
:
435 case SIOCGDEFIFACE_IN6
:
436 return(nd6_ioctl(cmd
, data
, ifp
));
440 case SIOCSIFPREFIX_IN6
:
441 case SIOCDIFPREFIX_IN6
:
442 case SIOCAIFPREFIX_IN6
:
443 case SIOCCIFPREFIX_IN6
:
444 case SIOCSGIFPREFIX_IN6
:
445 case SIOCGIFPREFIX_IN6
:
447 "prefix ioctls are now invalidated. "
448 "please use ifconfig.\n");
456 return(scope6_set(ifp
, ifr
->ifr_ifru
.ifru_scope_id
));
459 return(scope6_get(ifp
, ifr
->ifr_ifru
.ifru_scope_id
));
462 return(scope6_get_default(ifr
->ifr_ifru
.ifru_scope_id
));
473 return in6_lifaddr_ioctl(so
, cmd
, data
, ifp
, p
);
480 case SIOCAUTOCONF_START
:
481 ifnet_lock_exclusive(ifp
);
482 ifp
->if_eflags
|= IFEF_ACCEPT_RTADVD
;
483 ifnet_lock_done(ifp
);
486 case SIOCAUTOCONF_STOP
:
488 struct in6_ifaddr
*ia
, *nia
= NULL
;
490 ifnet_lock_exclusive(ifp
);
491 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADVD
;
492 ifnet_lock_done(ifp
);
494 /* nuke prefix list. this may try to remove some ifaddrs as well */
495 in6_purgeprefix(ifp
);
497 /* removed autoconfigured address from interface */
498 lck_mtx_lock(nd6_mutex
);
499 for (ia
= in6_ifaddrs
; ia
!= NULL
; ia
= nia
) {
501 if (ia
->ia_ifa
.ifa_ifp
!= ifp
)
503 if (ia
->ia6_flags
& IN6_IFF_AUTOCONF
)
504 in6_purgeaddr(&ia
->ia_ifa
, 1);
506 lck_mtx_unlock(nd6_mutex
);
513 /* NOTE: All the interface specific DLIL attachements should be done here
514 * They are currently done in in6_ifattach() for the interfaces that need it
517 if (ifp
->if_type
== IFT_PPP
&& ifra
->ifra_addr
.sin6_family
== AF_INET6
&&
518 ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
)
519 in6_if_up(ifp
, ifra
); /* PPP may provide LinkLocal addresses */
527 struct in6_ifaddr
*ia
, *nia
= NULL
;
529 /* removed link local addresses from interface */
531 lck_mtx_lock(nd6_mutex
);
532 for (ia
= in6_ifaddrs
; ia
!= NULL
; ia
= nia
) {
534 if (ia
->ia_ifa
.ifa_ifp
!= ifp
)
536 if (IN6_IS_ADDR_LINKLOCAL(&ia
->ia_addr
.sin6_addr
))
537 in6_purgeaddr(&ia
->ia_ifa
, 1);
539 lck_mtx_unlock(nd6_mutex
);
544 case SIOCPROTOATTACH_IN6
:
546 switch (ifp
->if_type
) {
547 #if IFT_BRIDGE /*OpenBSD 2.8*/
548 /* some of the interfaces are inherently not IPv6 capable */
554 if (error
= dlil_plumb_protocol(PF_INET6
, ifp
))
555 printf("SIOCPROTOATTACH_IN6: %s error=%d\n",
556 if_name(ifp
), error
);
563 case SIOCPROTODETACH_IN6
:
565 in6_purgeif(ifp
); /* Cleanup interface routes and addresses */
567 if (error
= dlil_unplumb_protocol(PF_INET6
, ifp
))
568 printf("SIOCPROTODETACH_IN6: %s error=%d\n",
569 if_name(ifp
), error
);
575 * Find address for this interface, if it exists.
577 if (ifra
->ifra_addr
.sin6_family
== AF_INET6
) { /* XXX */
578 struct sockaddr_in6
*sa6
=
579 (struct sockaddr_in6
*)&ifra
->ifra_addr
;
581 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
582 if (sa6
->sin6_addr
.s6_addr16
[1] == 0) {
583 /* link ID is not embedded by the user */
584 sa6
->sin6_addr
.s6_addr16
[1] =
585 htons(ifp
->if_index
);
586 } else if (sa6
->sin6_addr
.s6_addr16
[1] !=
587 htons(ifp
->if_index
)) {
588 return(EINVAL
); /* link ID contradicts */
590 if (sa6
->sin6_scope_id
) {
591 if (sa6
->sin6_scope_id
!=
592 (u_int32_t
)ifp
->if_index
)
594 sa6
->sin6_scope_id
= 0; /* XXX: good way? */
597 ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
);
601 case SIOCSIFADDR_IN6
:
602 case SIOCSIFDSTADDR_IN6
:
603 case SIOCSIFNETMASK_IN6
:
605 * Since IPv6 allows a node to assign multiple addresses
606 * on a single interface, SIOCSIFxxx ioctls are not suitable
607 * and should be unused.
609 /* we decided to obsolete this command (20000704) */
613 case SIOCDIFADDR_IN6
:
615 * for IPv4, we look for existing in_ifaddr here to allow
616 * "ifconfig if0 delete" to remove first IPv4 address on the
617 * interface. For IPv6, as the spec allow multiple interface
618 * address from the day one, we consider "remove the first one"
619 * semantics to be not preferable.
622 error
= EADDRNOTAVAIL
;
627 case SIOCAIFADDR_IN6
:
629 * We always require users to specify a valid IPv6 address for
630 * the corresponding operation.
632 if (ifra
->ifra_addr
.sin6_family
!= AF_INET6
||
633 ifra
->ifra_addr
.sin6_len
!= sizeof(struct sockaddr_in6
)) {
634 error
= EAFNOSUPPORT
;
644 case SIOCGIFADDR_IN6
:
645 /* This interface is basically deprecated. use SIOCGIFCONF. */
647 case SIOCGIFAFLAG_IN6
:
648 case SIOCGIFNETMASK_IN6
:
649 case SIOCGIFDSTADDR_IN6
:
650 case SIOCGIFALIFETIME_IN6
:
651 /* must think again about its semantics */
653 error
= EADDRNOTAVAIL
;
657 case SIOCSIFALIFETIME_IN6
:
659 struct in6_addrlifetime
*lt
;
666 error
= EADDRNOTAVAIL
;
669 /* sanity for overflow - beware unsigned */
670 lt
= &ifr
->ifr_ifru
.ifru_lifetime
;
671 if (lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
672 && lt
->ia6t_vltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
676 if (lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
677 && lt
->ia6t_pltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
687 case SIOCGIFADDR_IN6
:
688 ifr
->ifr_addr
= ia
->ia_addr
;
691 case SIOCGIFDSTADDR_IN6
:
692 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0) {
697 * XXX: should we check if ifa_dstaddr is NULL and return
700 ifr
->ifr_dstaddr
= ia
->ia_dstaddr
;
703 case SIOCGIFNETMASK_IN6
:
704 ifr
->ifr_addr
= ia
->ia_prefixmask
;
707 case SIOCGIFAFLAG_IN6
:
708 ifr
->ifr_ifru
.ifru_flags6
= ia
->ia6_flags
;
711 case SIOCGIFSTAT_IN6
:
716 index
= ifp
->if_index
;
717 if (in6_ifstat
== NULL
|| index
>= in6_ifstatmax
718 || in6_ifstat
[index
] == NULL
) {
719 /* return EAFNOSUPPORT? */
720 bzero(&ifr
->ifr_ifru
.ifru_stat
,
721 sizeof(ifr
->ifr_ifru
.ifru_stat
));
723 ifr
->ifr_ifru
.ifru_stat
= *in6_ifstat
[index
];
726 case SIOCGIFSTAT_ICMP6
:
731 index
= ifp
->if_index
;
732 if (icmp6_ifstat
== NULL
|| index
>= icmp6_ifstatmax
||
733 icmp6_ifstat
[index
] == NULL
) {
734 /* return EAFNOSUPPORT? */
735 bzero(&ifr
->ifr_ifru
.ifru_stat
,
736 sizeof(ifr
->ifr_ifru
.ifru_icmp6stat
));
738 ifr
->ifr_ifru
.ifru_icmp6stat
=
739 *icmp6_ifstat
[index
];
742 case SIOCGIFALIFETIME_IN6
:
743 ifr
->ifr_ifru
.ifru_lifetime
= ia
->ia6_lifetime
;
746 case SIOCSIFALIFETIME_IN6
:
747 ia
->ia6_lifetime
= ifr
->ifr_ifru
.ifru_lifetime
;
749 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
750 ia
->ia6_lifetime
.ia6t_expire
=
751 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_vltime
;
753 ia
->ia6_lifetime
.ia6t_expire
= 0;
754 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
755 ia
->ia6_lifetime
.ia6t_preferred
=
756 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_pltime
;
758 ia
->ia6_lifetime
.ia6t_preferred
= 0;
761 case SIOCAIFADDR_IN6
:
764 struct nd_prefix pr0
, *pr
;
766 /* Attempt to attache the protocol, in case it isn't attached */
767 error
= dlil_plumb_protocol(PF_INET6
, ifp
);
769 if (error
!= EEXIST
) {
770 printf("SIOCAIFADDR_IN6: %s can't plumb protocol error=%d\n",
771 if_name(ifp
), error
);
779 /* PF_INET6 wasn't previously attached */
780 in6_if_up(ifp
, NULL
);
784 * first, make or update the interface address structure,
785 * and link it to the list.
787 if ((error
= in6_update_ifa(ifp
, ifra
, ia
)) != 0)
791 * then, make the prefix on-link on the interface.
792 * XXX: we'd rather create the prefix before the address, but
793 * we need at least one address to install the corresponding
794 * interface route, so we configure the address first.
798 * convert mask to prefix length (prefixmask has already
799 * been validated in in6_update_ifa().
801 bzero(&pr0
, sizeof(pr0
));
803 pr0
.ndpr_plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
805 if (pr0
.ndpr_plen
== 128)
806 break; /* we don't need to install a host route. */
807 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
808 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
809 /* apply the mask for safety. */
810 for (i
= 0; i
< 4; i
++) {
811 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
812 ifra
->ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
815 * XXX: since we don't have an API to set prefix (not address)
816 * lifetimes, we just use the same lifetimes as addresses.
817 * The (temporarily) installed lifetimes can be overridden by
818 * later advertised RAs (when accept_rtadv is non 0), which is
819 * an intended behavior.
821 pr0
.ndpr_raf_onlink
= 1; /* should be configurable? */
823 ((ifra
->ifra_flags
& IN6_IFF_AUTOCONF
) != 0);
824 pr0
.ndpr_vltime
= ifra
->ifra_lifetime
.ia6t_vltime
;
825 pr0
.ndpr_pltime
= ifra
->ifra_lifetime
.ia6t_pltime
;
827 /* add the prefix if there's one. */
828 if ((pr
= nd6_prefix_lookup(&pr0
)) == NULL
) {
830 * nd6_prelist_add will install the corresponding
833 if ((error
= nd6_prelist_add(&pr0
, NULL
, &pr
)) != 0)
836 log(LOG_ERR
, "nd6_prelist_add succedded but "
842 if ((ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
))
844 /* XXX: this should not happen! */
845 log(LOG_ERR
, "in6_control: addition succeeded, but"
848 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
849 ia
->ia6_ndpr
== NULL
) { /* new autoconfed addr */
854 * If this is the first autoconf address from
855 * the prefix, create a temporary address
856 * as well (when specified).
858 if (ip6_use_tempaddr
&&
859 pr
->ndpr_refcnt
== 1) {
861 if ((e
= in6_tmpifadd(ia
, 1)) != 0) {
862 log(LOG_NOTICE
, "in6_control: "
863 "failed to create a "
864 "temporary address, "
872 * this might affect the status of autoconfigured
873 * addresses, that is, this address might make
874 * other addresses detached.
876 pfxlist_onlink_check(0);
882 case SIOCDIFADDR_IN6
:
885 struct nd_prefix pr0
, *pr
;
888 * If the address being deleted is the only one that owns
889 * the corresponding prefix, expire the prefix as well.
890 * XXX: theoretically, we don't have to warry about such
891 * relationship, since we separate the address management
892 * and the prefix management. We do this, however, to provide
893 * as much backward compatibility as possible in terms of
894 * the ioctl operation.
896 bzero(&pr0
, sizeof(pr0
));
898 pr0
.ndpr_plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
900 if (pr0
.ndpr_plen
== 128)
902 pr0
.ndpr_prefix
= ia
->ia_addr
;
903 pr0
.ndpr_mask
= ia
->ia_prefixmask
.sin6_addr
;
904 for (i
= 0; i
< 4; i
++) {
905 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
906 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
];
909 * The logic of the following condition is a bit complicated.
910 * We expire the prefix when
911 * 1. the address obeys autoconfiguration and it is the
912 * only owner of the associated prefix, or
913 * 2. the address does not obey autoconf and there is no
914 * other owner of the prefix.
916 if ((pr
= nd6_prefix_lookup(&pr0
)) != NULL
&&
917 (((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
918 pr
->ndpr_refcnt
== 1) ||
919 ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 &&
920 pr
->ndpr_refcnt
== 0))) {
921 pr
->ndpr_expire
= 1; /* XXX: just for expiration */
925 in6_purgeaddr(&ia
->ia_ifa
, 0);
931 error
= dlil_ioctl(PF_INET6
, ifp
, cmd
, (caddr_t
)data
);
934 if (ifp
== NULL
|| ifp
->if_ioctl
== 0)
936 return((*ifp
->if_ioctl
)(ifp
, cmd
, data
));
944 * Update parameters of an IPv6 interface address.
945 * If necessary, a new entry is created and linked into address chains.
946 * This function is separated from in6_control().
947 * XXX: should this be performed under splnet()?
950 in6_update_ifa(ifp
, ifra
, ia
)
952 struct in6_aliasreq
*ifra
;
953 struct in6_ifaddr
*ia
;
955 int error
= 0, hostIsNew
= 0, plen
= -1;
956 struct in6_ifaddr
*oia
;
957 struct sockaddr_in6 dst6
;
958 struct in6_addrlifetime
*lt
;
959 struct timeval timenow
;
962 lck_mtx_assert(nd6_mutex
, LCK_MTX_ASSERT_NOTOWNED
);
963 /* Validate parameters */
964 if (ifp
== NULL
|| ifra
== NULL
) /* this maybe redundant */
968 * The destination address for a p2p link must have a family
969 * of AF_UNSPEC or AF_INET6.
971 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
972 ifra
->ifra_dstaddr
.sin6_family
!= AF_INET6
&&
973 ifra
->ifra_dstaddr
.sin6_family
!= AF_UNSPEC
)
974 return(EAFNOSUPPORT
);
976 * validate ifra_prefixmask. don't check sin6_family, netmask
977 * does not carry fields other than sin6_len.
979 if (ifra
->ifra_prefixmask
.sin6_len
> sizeof(struct sockaddr_in6
))
982 * Set the address family value for the mask if it was not set.
985 if (ifra
->ifra_prefixmask
.sin6_len
== sizeof(struct sockaddr_in6
) &&
986 ifra
->ifra_prefixmask
.sin6_family
== 0) {
987 ifra
->ifra_prefixmask
.sin6_family
= AF_INET6
;
990 * Because the IPv6 address architecture is classless, we require
991 * users to specify a (non 0) prefix length (mask) for a new address.
992 * We also require the prefix (when specified) mask is valid, and thus
993 * reject a non-consecutive mask.
995 if (ia
== NULL
&& ifra
->ifra_prefixmask
.sin6_len
== 0)
997 if (ifra
->ifra_prefixmask
.sin6_len
!= 0) {
998 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
999 (u_char
*)&ifra
->ifra_prefixmask
+
1000 ifra
->ifra_prefixmask
.sin6_len
);
1006 * In this case, ia must not be NULL. We just use its prefix
1009 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
);
1012 * If the destination address on a p2p interface is specified,
1013 * and the address is a scoped one, validate/set the scope
1016 dst6
= ifra
->ifra_dstaddr
;
1017 if ((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) &&
1018 (dst6
.sin6_family
== AF_INET6
)) {
1021 #ifndef SCOPEDROUTING
1022 if ((error
= in6_recoverscope(&dst6
,
1023 &ifra
->ifra_dstaddr
.sin6_addr
,
1027 scopeid
= in6_addr2scopeid(ifp
, &dst6
.sin6_addr
);
1028 if (dst6
.sin6_scope_id
== 0) /* user omit to specify the ID. */
1029 dst6
.sin6_scope_id
= scopeid
;
1030 else if (dst6
.sin6_scope_id
!= scopeid
)
1031 return(EINVAL
); /* scope ID mismatch. */
1032 #ifndef SCOPEDROUTING
1033 if ((error
= in6_embedscope(&dst6
.sin6_addr
, &dst6
, NULL
, NULL
))
1036 dst6
.sin6_scope_id
= 0; /* XXX */
1040 * The destination address can be specified only for a p2p or a
1041 * loopback interface. If specified, the corresponding prefix length
1044 if (ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
) {
1045 if ((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) == 0) {
1046 /* XXX: noisy message */
1047 log(LOG_INFO
, "in6_update_ifa: a destination can be "
1048 "specified for a p2p or a loopback IF only\n");
1053 * The following message seems noisy, but we dare to
1054 * add it for diagnosis.
1056 log(LOG_INFO
, "in6_update_ifa: prefixlen must be 128 "
1057 "when dstaddr is specified\n");
1061 /* lifetime consistency check */
1063 getmicrotime(&timenow
);
1064 lt
= &ifra
->ifra_lifetime
;
1065 if (lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
1066 && lt
->ia6t_vltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
1069 if (lt
->ia6t_vltime
== 0) {
1071 * the following log might be noisy, but this is a typical
1072 * configuration mistake or a tool's bug.
1075 "in6_update_ifa: valid lifetime is 0 for %s\n",
1076 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
1078 if (lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
1079 && lt
->ia6t_pltime
+ timenow
.tv_sec
< timenow
.tv_sec
) {
1084 * If this is a new address, allocate a new ifaddr and link it
1090 * When in6_update_ifa() is called in a process of a received
1091 * RA, it is called under splnet(). So, we should call malloc
1094 ia
= (struct in6_ifaddr
*)
1095 _MALLOC(sizeof(*ia
), M_IFADDR
, M_NOWAIT
);
1098 bzero((caddr_t
)ia
, sizeof(*ia
));
1099 /* Initialize the address and masks */
1100 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
1101 ia
->ia_addr
.sin6_family
= AF_INET6
;
1102 ia
->ia_addr
.sin6_len
= sizeof(ia
->ia_addr
);
1103 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) != 0) {
1105 * XXX: some functions expect that ifa_dstaddr is not
1106 * NULL for p2p interfaces.
1108 ia
->ia_ifa
.ifa_dstaddr
1109 = (struct sockaddr
*)&ia
->ia_dstaddr
;
1111 ia
->ia_ifa
.ifa_dstaddr
= NULL
;
1113 ia
->ia_ifa
.ifa_netmask
1114 = (struct sockaddr
*)&ia
->ia_prefixmask
;
1117 lck_mtx_lock(nd6_mutex
);
1118 if ((oia
= in6_ifaddrs
) != NULL
) {
1119 for ( ; oia
->ia_next
; oia
= oia
->ia_next
)
1124 lck_mtx_unlock(nd6_mutex
);
1126 ifnet_lock_exclusive(ifp
);
1127 if_attach_ifa(ifp
, &ia
->ia_ifa
);
1128 ifnet_lock_done(ifp
);
1131 /* set prefix mask */
1132 if (ifra
->ifra_prefixmask
.sin6_len
) {
1134 * We prohibit changing the prefix length of an existing
1136 * + such an operation should be rare in IPv6, and
1137 * + the operation would confuse prefix management.
1139 if (ia
->ia_prefixmask
.sin6_len
&&
1140 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
) != plen
) {
1141 log(LOG_INFO
, "in6_update_ifa: the prefix length of an"
1142 " existing (%s) address should not be changed\n",
1143 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1147 ia
->ia_prefixmask
= ifra
->ifra_prefixmask
;
1151 * If a new destination address is specified, scrub the old one and
1152 * install the new destination. Note that the interface must be
1153 * p2p or loopback (see the check above.)
1155 if (dst6
.sin6_family
== AF_INET6
&&
1156 !IN6_ARE_ADDR_EQUAL(&dst6
.sin6_addr
,
1157 &ia
->ia_dstaddr
.sin6_addr
)) {
1160 if ((ia
->ia_flags
& IFA_ROUTE
) != 0 &&
1161 (e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
))
1163 log(LOG_ERR
, "in6_update_ifa: failed to remove "
1164 "a route to the old destination: %s\n",
1165 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1166 /* proceed anyway... */
1169 ia
->ia_flags
&= ~IFA_ROUTE
;
1170 ia
->ia_dstaddr
= dst6
;
1173 /* reset the interface and routing table appropriately. */
1174 if ((error
= in6_ifinit(ifp
, ia
, &ifra
->ifra_addr
, hostIsNew
)) != 0)
1178 * Beyond this point, we should call in6_purgeaddr upon an error,
1179 * not just go to unlink.
1182 #if 0 /* disable this mechanism for now */
1183 /* update prefix list */
1185 (ifra
->ifra_flags
& IN6_IFF_NOPFX
) == 0) { /* XXX */
1188 iilen
= (sizeof(ia
->ia_prefixmask
.sin6_addr
) << 3) - plen
;
1189 if ((error
= in6_prefix_add_ifid(iilen
, ia
)) != 0) {
1190 in6_purgeaddr((struct ifaddr
*)ia
, 0);
1196 if ((ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1197 struct sockaddr_in6 mltaddr
, mltmask
;
1198 struct in6_multi
*in6m
;
1202 * join solicited multicast addr for new host id
1204 struct in6_addr llsol
;
1205 bzero(&llsol
, sizeof(struct in6_addr
));
1206 llsol
.s6_addr16
[0] = htons(0xff02);
1207 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1208 llsol
.s6_addr32
[1] = 0;
1209 llsol
.s6_addr32
[2] = htonl(1);
1210 llsol
.s6_addr32
[3] =
1211 ifra
->ifra_addr
.sin6_addr
.s6_addr32
[3];
1212 llsol
.s6_addr8
[12] = 0xff;
1213 (void)in6_addmulti(&llsol
, ifp
, &error
, 0);
1216 "in6_update_ifa: addmulti failed for "
1217 "%s on %s (errno=%d)\n",
1218 ip6_sprintf(&llsol
), if_name(ifp
),
1220 in6_purgeaddr((struct ifaddr
*)ia
, 0);
1225 bzero(&mltmask
, sizeof(mltmask
));
1226 mltmask
.sin6_len
= sizeof(struct sockaddr_in6
);
1227 mltmask
.sin6_family
= AF_INET6
;
1228 mltmask
.sin6_addr
= in6mask32
;
1231 * join link-local all-nodes address
1233 bzero(&mltaddr
, sizeof(mltaddr
));
1234 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
1235 mltaddr
.sin6_family
= AF_INET6
;
1236 mltaddr
.sin6_addr
= in6addr_linklocal_allnodes
;
1237 mltaddr
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
1239 ifnet_lock_shared(ifp
);
1240 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1241 ifnet_lock_done(ifp
);
1244 (struct sockaddr
*)&mltaddr
,
1245 (struct sockaddr
*)&ia
->ia_addr
,
1246 (struct sockaddr
*)&mltmask
,
1247 RTF_UP
|RTF_CLONING
, /* xxx */
1248 (struct rtentry
**)0);
1249 (void)in6_addmulti(&mltaddr
.sin6_addr
, ifp
, &error
, 0);
1252 "in6_update_ifa: addmulti failed for "
1253 "%s on %s (errno=%d)\n",
1254 ip6_sprintf(&mltaddr
.sin6_addr
),
1255 if_name(ifp
), error
);
1260 * join node information group address
1262 #define hostnamelen strlen(hostname)
1263 if (in6_nigroup(ifp
, hostname
, hostnamelen
, &mltaddr
.sin6_addr
)
1265 ifnet_lock_shared(ifp
);
1266 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1267 ifnet_lock_done(ifp
);
1268 if (in6m
== NULL
&& ia
!= NULL
) {
1269 (void)in6_addmulti(&mltaddr
.sin6_addr
,
1272 log(LOG_WARNING
, "in6_update_ifa: "
1273 "addmulti failed for "
1274 "%s on %s (errno=%d)\n",
1275 ip6_sprintf(&mltaddr
.sin6_addr
),
1276 if_name(ifp
), error
);
1283 * join node-local all-nodes address, on loopback.
1284 * XXX: since "node-local" is obsoleted by interface-local,
1285 * we have to join the group on every interface with
1286 * some interface-boundary restriction.
1288 if (ifp
->if_flags
& IFF_LOOPBACK
) {
1289 struct in6_ifaddr
*ia_loop
;
1291 struct in6_addr loop6
= in6addr_loopback
;
1292 ia_loop
= in6ifa_ifpwithaddr(ifp
, &loop6
);
1294 mltaddr
.sin6_addr
= in6addr_nodelocal_allnodes
;
1296 ifnet_lock_shared(ifp
);
1297 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1298 ifnet_lock_done(ifp
);
1299 if (in6m
== NULL
&& ia_loop
!= NULL
) {
1301 (struct sockaddr
*)&mltaddr
,
1302 (struct sockaddr
*)&ia_loop
->ia_addr
,
1303 (struct sockaddr
*)&mltmask
,
1305 (struct rtentry
**)0);
1306 (void)in6_addmulti(&mltaddr
.sin6_addr
, ifp
,
1309 log(LOG_WARNING
, "in6_update_ifa: "
1310 "addmulti failed for %s on %s "
1312 ip6_sprintf(&mltaddr
.sin6_addr
),
1313 if_name(ifp
), error
);
1319 ia
->ia6_flags
= ifra
->ifra_flags
;
1320 ia
->ia6_flags
&= ~IN6_IFF_DUPLICATED
; /*safety*/
1321 ia
->ia6_flags
&= ~IN6_IFF_NODAD
; /* Mobile IPv6 */
1323 ia
->ia6_lifetime
= ifra
->ifra_lifetime
;
1325 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
1326 ia
->ia6_lifetime
.ia6t_expire
=
1327 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_vltime
;
1329 ia
->ia6_lifetime
.ia6t_expire
= 0;
1330 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
1331 ia
->ia6_lifetime
.ia6t_preferred
=
1332 timenow
.tv_sec
+ ia
->ia6_lifetime
.ia6t_pltime
;
1334 ia
->ia6_lifetime
.ia6t_preferred
= 0;
1337 * make sure to initialize ND6 information. this is to workaround
1338 * issues with interfaces with IPv6 addresses, which have never brought
1339 * up. We are assuming that it is safe to nd6_ifattach multiple times.
1344 * Perform DAD, if needed.
1345 * XXX It may be of use, if we can administratively
1348 if (in6if_do_dad(ifp
) && (ifra
->ifra_flags
& IN6_IFF_NODAD
) == 0) {
1349 ia
->ia6_flags
|= IN6_IFF_TENTATIVE
;
1350 nd6_dad_start((struct ifaddr
*)ia
, NULL
);
1357 * XXX: if a change of an existing address failed, keep the entry
1361 in6_unlink_ifa(ia
, ifp
, 0);
1367 struct ifaddr
*ifa
, int nd6_locked
)
1369 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1370 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*) ifa
;
1372 /* stop DAD processing */
1376 * delete route to the destination of the address being purged.
1377 * The interface must be p2p or loopback in this case.
1379 if ((ia
->ia_flags
& IFA_ROUTE
) != 0 && ia
->ia_dstaddr
.sin6_len
!= 0) {
1382 if ((e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
))
1384 log(LOG_ERR
, "in6_purgeaddr: failed to remove "
1385 "a route to the p2p destination: %s on %s, "
1387 ip6_sprintf(&ia
->ia_addr
.sin6_addr
), if_name(ifp
),
1389 /* proceed anyway... */
1392 ia
->ia_flags
&= ~IFA_ROUTE
;
1395 /* Remove ownaddr's loopback rtentry, if it exists. */
1396 in6_ifremloop(&(ia
->ia_ifa
), nd6_locked
);
1398 if (ifp
->if_flags
& IFF_MULTICAST
) {
1400 * delete solicited multicast addr for deleting host id
1402 struct in6_multi
*in6m
;
1403 struct in6_addr llsol
;
1404 bzero(&llsol
, sizeof(struct in6_addr
));
1405 llsol
.s6_addr16
[0] = htons(0xff02);
1406 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1407 llsol
.s6_addr32
[1] = 0;
1408 llsol
.s6_addr32
[2] = htonl(1);
1409 llsol
.s6_addr32
[3] =
1410 ia
->ia_addr
.sin6_addr
.s6_addr32
[3];
1411 llsol
.s6_addr8
[12] = 0xff;
1413 ifnet_lock_shared(ifp
);
1414 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1415 ifnet_lock_done(ifp
);
1417 in6_delmulti(in6m
, nd6_locked
);
1420 in6_unlink_ifa(ia
, ifp
, nd6_locked
);
1421 in6_post_msg(ifp
, KEV_INET6_ADDR_DELETED
, ia
);
1425 in6_unlink_ifa(ia
, ifp
, nd6_locked
)
1426 struct in6_ifaddr
*ia
;
1431 struct in6_ifaddr
*oia
;
1433 ifnet_lock_exclusive(ifp
);
1434 if_detach_ifa(ifp
, &ia
->ia_ifa
);
1435 ifnet_lock_done(ifp
);
1438 lck_mtx_lock(nd6_mutex
);
1440 if (oia
== (ia
= in6_ifaddrs
))
1441 in6_ifaddrs
= ia
->ia_next
;
1443 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
1446 ia
->ia_next
= oia
->ia_next
;
1449 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1452 if (oia
->ia6_ifpr
) { /* check for safety */
1453 plen
= in6_mask2len(&oia
->ia_prefixmask
.sin6_addr
, NULL
);
1454 iilen
= (sizeof(oia
->ia_prefixmask
.sin6_addr
) << 3) - plen
;
1455 in6_prefix_remove_ifid(iilen
, oia
);
1459 * When an autoconfigured address is being removed, release the
1460 * reference to the base prefix. Also, since the release might
1461 * affect the status of other (detached) addresses, call
1462 * pfxlist_onlink_check().
1464 if ((oia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0) {
1465 if (oia
->ia6_ndpr
== NULL
) {
1466 log(LOG_NOTICE
, "in6_unlink_ifa: autoconf'ed address "
1467 "%p has no prefix\n", oia
);
1469 oia
->ia6_ndpr
->ndpr_refcnt
--;
1470 oia
->ia6_flags
&= ~IN6_IFF_AUTOCONF
;
1471 oia
->ia6_ndpr
= NULL
;
1474 pfxlist_onlink_check(1);
1477 lck_mtx_unlock(nd6_mutex
);
1481 * release another refcnt for the link from in6_ifaddrs.
1482 * Note that we should decrement the refcnt at least once for all *BSD.
1484 ifafree(&oia
->ia_ifa
);
1492 struct in6_ifaddr
*ia
, *nia
= NULL
;
1494 if (ifp
== NULL
|| &ifp
->if_addrlist
== NULL
)
1497 lck_mtx_lock(nd6_mutex
);
1498 for (ia
= in6_ifaddrs
; ia
!= NULL
; ia
= nia
)
1501 if (ia
->ia_ifa
.ifa_ifp
!= ifp
)
1503 in6_purgeaddr(&ia
->ia_ifa
, 1);
1505 lck_mtx_unlock(nd6_mutex
);
1512 * SIOCGLIFADDR: get first address. (?)
1513 * SIOCGLIFADDR with IFLR_PREFIX:
1514 * get first address that matches the specified prefix.
1515 * SIOCALIFADDR: add the specified address.
1516 * SIOCALIFADDR with IFLR_PREFIX:
1517 * add the specified prefix, filling hostid part from
1518 * the first link-local address. prefixlen must be <= 64.
1519 * SIOCDLIFADDR: delete the specified address.
1520 * SIOCDLIFADDR with IFLR_PREFIX:
1521 * delete the first address that matches the specified prefix.
1523 * EINVAL on invalid parameters
1524 * EADDRNOTAVAIL on prefix match failed/specified address not found
1525 * other values may be returned from in6_ioctl()
1527 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1528 * this is to accomodate address naming scheme other than RFC2374,
1530 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1531 * address encoding scheme. (see figure on page 8)
1534 in6_lifaddr_ioctl(so
, cmd
, data
, ifp
, p
)
1541 struct if_laddrreq
*iflr
= (struct if_laddrreq
*)data
;
1543 struct sockaddr
*sa
;
1546 if (!data
|| !ifp
) {
1547 panic("invalid argument to in6_lifaddr_ioctl");
1553 /* address must be specified on GET with IFLR_PREFIX */
1554 if ((iflr
->flags
& IFLR_PREFIX
) == 0)
1559 /* address must be specified on ADD and DELETE */
1560 sa
= (struct sockaddr
*)&iflr
->addr
;
1561 if (sa
->sa_family
!= AF_INET6
)
1563 if (sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1565 /* XXX need improvement */
1566 sa
= (struct sockaddr
*)&iflr
->dstaddr
;
1567 if (sa
->sa_family
&& sa
->sa_family
!= AF_INET6
)
1569 if (sa
->sa_len
&& sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1572 default: /* shouldn't happen */
1574 panic("invalid cmd to in6_lifaddr_ioctl");
1580 if (sizeof(struct in6_addr
) * 8 < iflr
->prefixlen
)
1586 struct in6_aliasreq ifra
;
1587 struct in6_addr hostid
;
1589 int hostid_found
= 0;
1591 if ((iflr
->flags
& IFLR_PREFIX
) != 0) {
1592 struct sockaddr_in6
*sin6
;
1595 * hostid is to fill in the hostid part of the
1596 * address. hostid points to the first link-local
1597 * address attached to the interface.
1599 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
, 0);
1601 return EADDRNOTAVAIL
;
1602 hostid
= *IFA_IN6(ifa
);
1605 /* prefixlen must be <= 64. */
1606 if (64 < iflr
->prefixlen
)
1608 prefixlen
= iflr
->prefixlen
;
1610 /* hostid part must be zero. */
1611 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1612 if (sin6
->sin6_addr
.s6_addr32
[2] != 0
1613 || sin6
->sin6_addr
.s6_addr32
[3] != 0) {
1617 prefixlen
= iflr
->prefixlen
;
1619 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1620 bzero(&ifra
, sizeof(ifra
));
1621 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
1622 sizeof(ifra
.ifra_name
));
1624 bcopy(&iflr
->addr
, &ifra
.ifra_addr
,
1625 ((struct sockaddr
*)&iflr
->addr
)->sa_len
);
1627 /* fill in hostid part */
1628 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] =
1629 hostid
.s6_addr32
[2];
1630 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] =
1631 hostid
.s6_addr32
[3];
1634 if (((struct sockaddr
*)&iflr
->dstaddr
)->sa_family
) { /*XXX*/
1635 bcopy(&iflr
->dstaddr
, &ifra
.ifra_dstaddr
,
1636 ((struct sockaddr
*)&iflr
->dstaddr
)->sa_len
);
1638 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[2] =
1639 hostid
.s6_addr32
[2];
1640 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[3] =
1641 hostid
.s6_addr32
[3];
1645 ifra
.ifra_prefixmask
.sin6_len
= sizeof(struct sockaddr_in6
);
1646 in6_len2mask(&ifra
.ifra_prefixmask
.sin6_addr
, prefixlen
);
1648 ifra
.ifra_flags
= iflr
->flags
& ~IFLR_PREFIX
;
1649 return in6_control(so
, SIOCAIFADDR_IN6
, (caddr_t
)&ifra
, ifp
, p
);
1654 struct in6_ifaddr
*ia
;
1655 struct in6_addr mask
, candidate
, match
;
1656 struct sockaddr_in6
*sin6
;
1659 bzero(&mask
, sizeof(mask
));
1660 if (iflr
->flags
& IFLR_PREFIX
) {
1661 /* lookup a prefix rather than address. */
1662 in6_len2mask(&mask
, iflr
->prefixlen
);
1664 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1665 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
1666 match
.s6_addr32
[0] &= mask
.s6_addr32
[0];
1667 match
.s6_addr32
[1] &= mask
.s6_addr32
[1];
1668 match
.s6_addr32
[2] &= mask
.s6_addr32
[2];
1669 match
.s6_addr32
[3] &= mask
.s6_addr32
[3];
1671 /* if you set extra bits, that's wrong */
1672 if (bcmp(&match
, &sin6
->sin6_addr
, sizeof(match
)))
1677 if (cmd
== SIOCGLIFADDR
) {
1678 /* on getting an address, take the 1st match */
1681 /* on deleting an address, do exact match */
1682 in6_len2mask(&mask
, 128);
1683 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1684 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
1690 ifnet_lock_shared(ifp
);
1691 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1693 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1698 bcopy(IFA_IN6(ifa
), &candidate
, sizeof(candidate
));
1699 #ifndef SCOPEDROUTING
1701 * XXX: this is adhoc, but is necessary to allow
1702 * a user to specify fe80::/64 (not /10) for a
1703 * link-local address.
1705 if (IN6_IS_ADDR_LINKLOCAL(&candidate
))
1706 candidate
.s6_addr16
[1] = 0;
1708 candidate
.s6_addr32
[0] &= mask
.s6_addr32
[0];
1709 candidate
.s6_addr32
[1] &= mask
.s6_addr32
[1];
1710 candidate
.s6_addr32
[2] &= mask
.s6_addr32
[2];
1711 candidate
.s6_addr32
[3] &= mask
.s6_addr32
[3];
1712 if (IN6_ARE_ADDR_EQUAL(&candidate
, &match
))
1715 ifnet_lock_done(ifp
);
1717 return EADDRNOTAVAIL
;
1720 if (cmd
== SIOCGLIFADDR
) {
1721 #ifndef SCOPEDROUTING
1722 struct sockaddr_in6
*s6
;
1725 /* fill in the if_laddrreq structure */
1726 bcopy(&ia
->ia_addr
, &iflr
->addr
, ia
->ia_addr
.sin6_len
);
1727 #ifndef SCOPEDROUTING /* XXX see above */
1728 s6
= (struct sockaddr_in6
*)&iflr
->addr
;
1729 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
1730 s6
->sin6_addr
.s6_addr16
[1] = 0;
1732 in6_addr2scopeid(ifp
, &s6
->sin6_addr
);
1735 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
1736 bcopy(&ia
->ia_dstaddr
, &iflr
->dstaddr
,
1737 ia
->ia_dstaddr
.sin6_len
);
1738 #ifndef SCOPEDROUTING /* XXX see above */
1739 s6
= (struct sockaddr_in6
*)&iflr
->dstaddr
;
1740 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
1741 s6
->sin6_addr
.s6_addr16
[1] = 0;
1743 in6_addr2scopeid(ifp
,
1748 bzero(&iflr
->dstaddr
, sizeof(iflr
->dstaddr
));
1751 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
1754 iflr
->flags
= ia
->ia6_flags
; /* XXX */
1758 struct in6_aliasreq ifra
;
1760 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1761 bzero(&ifra
, sizeof(ifra
));
1762 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
1763 sizeof(ifra
.ifra_name
));
1765 bcopy(&ia
->ia_addr
, &ifra
.ifra_addr
,
1766 ia
->ia_addr
.sin6_len
);
1767 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
1768 bcopy(&ia
->ia_dstaddr
, &ifra
.ifra_dstaddr
,
1769 ia
->ia_dstaddr
.sin6_len
);
1771 bzero(&ifra
.ifra_dstaddr
,
1772 sizeof(ifra
.ifra_dstaddr
));
1774 bcopy(&ia
->ia_prefixmask
, &ifra
.ifra_dstaddr
,
1775 ia
->ia_prefixmask
.sin6_len
);
1777 ifra
.ifra_flags
= ia
->ia6_flags
;
1778 return in6_control(so
, SIOCDIFADDR_IN6
, (caddr_t
)&ifra
,
1784 return EOPNOTSUPP
; /* just for safety */
1788 * Initialize an interface's intetnet6 address
1789 * and routing table entry.
1792 in6_ifinit(ifp
, ia
, sin6
, newhost
)
1794 struct in6_ifaddr
*ia
;
1795 struct sockaddr_in6
*sin6
;
1798 int error
= 0, plen
, ifacount
= 0;
1802 * Give the interface a chance to initialize
1803 * if this is its first address,
1804 * and to validate the address if necessary.
1806 ifnet_lock_shared(ifp
);
1807 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1809 if (ifa
->ifa_addr
== NULL
)
1810 continue; /* just for safety */
1811 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1815 ifnet_lock_done(ifp
);
1817 ia
->ia_addr
= *sin6
;
1820 if (ifacount
<= 1 &&
1821 (error
= dlil_ioctl(PF_INET6
, ifp
, SIOCSIFADDR
, (caddr_t
)ia
))) {
1827 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
1829 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1833 * If the destination address is specified for a point-to-point
1834 * interface, install a route to the destination as an interface
1837 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
); /* XXX */
1838 if (plen
== 128 && ia
->ia_dstaddr
.sin6_family
== AF_INET6
) {
1839 if ((error
= rtinit(&(ia
->ia_ifa
), (int)RTM_ADD
,
1840 RTF_UP
| RTF_HOST
)) != 0)
1842 ia
->ia_flags
|= IFA_ROUTE
;
1846 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1848 ia
->ia_ifa
.ifa_flags
|= RTF_CLONING
;
1851 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1853 /* set the rtrequest function to create llinfo */
1854 ia
->ia_ifa
.ifa_rtrequest
= nd6_rtrequest
;
1855 in6_ifaddloop(&(ia
->ia_ifa
));
1862 * Add an address to the list of IP6 multicast addresses for a
1866 in6_addmulti(maddr6
, ifp
, errorp
, nd6_locked
)
1867 struct in6_addr
*maddr6
;
1872 struct in6_multi
*in6m
;
1873 struct sockaddr_in6 sin6
;
1874 struct ifmultiaddr
*ifma
;
1879 * Call generic routine to add membership or increment
1880 * refcount. It wants addresses in the form of a sockaddr,
1881 * so we build one here (being careful to zero the unused bytes).
1883 bzero(&sin6
, sizeof sin6
);
1884 sin6
.sin6_family
= AF_INET6
;
1885 sin6
.sin6_len
= sizeof sin6
;
1886 sin6
.sin6_addr
= *maddr6
;
1887 *errorp
= if_addmulti(ifp
, (struct sockaddr
*)&sin6
, &ifma
);
1893 * If ifma->ifma_protospec is null, then if_addmulti() created
1894 * a new record. Otherwise, we are done.
1896 if (ifma
->ifma_protospec
!= 0)
1897 return ifma
->ifma_protospec
;
1899 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1900 at interrupt time? If so, need to fix if_addmulti. XXX */
1901 in6m
= (struct in6_multi
*)_MALLOC(sizeof(*in6m
), M_IPMADDR
, M_NOWAIT
);
1906 bzero(in6m
, sizeof *in6m
);
1907 in6m
->in6m_addr
= *maddr6
;
1908 in6m
->in6m_ifp
= ifp
;
1909 in6m
->in6m_ifma
= ifma
;
1910 ifma
->ifma_protospec
= in6m
;
1911 if (nd6_locked
== 0)
1912 lck_mtx_lock(nd6_mutex
);
1913 LIST_INSERT_HEAD(&in6_multihead
, in6m
, in6m_entry
);
1914 if (nd6_locked
== 0)
1915 lck_mtx_unlock(nd6_mutex
);
1918 * Let MLD6 know that we have joined a new IP6 multicast
1921 mld6_start_listening(in6m
);
1926 * Delete a multicast address record.
1930 struct in6_multi
*in6m
, int nd6locked
)
1932 struct ifmultiaddr
*ifma
= in6m
->in6m_ifma
;
1934 if (ifma
&& ifma
->ifma_usecount
== 1) {
1936 * No remaining claims to this record; let MLD6 know
1937 * that we are leaving the multicast group.
1939 mld6_stop_listening(in6m
);
1940 ifma
->ifma_protospec
= 0;
1942 lck_mtx_lock(nd6_mutex
);
1943 LIST_REMOVE(in6m
, in6m_entry
);
1945 lck_mtx_unlock(nd6_mutex
);
1946 FREE(in6m
, M_IPMADDR
);
1948 /* XXX - should be separate API for when we have an ifma? */
1950 if_delmultiaddr(ifma
, 0);
1956 * Find an IPv6 interface link-local address specific to an interface.
1959 in6ifa_ifpforlinklocal(ifp
, ignoreflags
)
1965 ifnet_lock_shared(ifp
);
1966 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1968 if (ifa
->ifa_addr
== NULL
)
1969 continue; /* just for safety */
1970 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1972 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
))) {
1973 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
&
1979 ifnet_lock_done(ifp
);
1981 return((struct in6_ifaddr
*)ifa
);
1985 * find the internet address corresponding to a given interface and address.
1988 in6ifa_ifpwithaddr(ifp
, addr
)
1990 struct in6_addr
*addr
;
1994 ifnet_lock_shared(ifp
);
1995 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1997 if (ifa
->ifa_addr
== NULL
)
1998 continue; /* just for safety */
1999 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2001 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(ifa
)))
2004 ifnet_lock_done(ifp
);
2006 return((struct in6_ifaddr
*)ifa
);
2010 * Convert IP6 address to printable (loggable) representation.
2012 static char digits
[] = "0123456789abcdef";
2013 static int ip6round
= 0;
2016 const struct in6_addr
*addr
;
2018 static char ip6buf
[8][48];
2021 const u_short
*a
= (const u_short
*)addr
;
2025 ip6round
= (ip6round
+ 1) & 7;
2026 cp
= ip6buf
[ip6round
];
2028 for (i
= 0; i
< 8; i
++) {
2039 if (dcolon
== 0 && *(a
+ 1) == 0) {
2051 d
= (const u_char
*)a
;
2052 *cp
++ = digits
[*d
>> 4];
2053 *cp
++ = digits
[*d
++ & 0xf];
2054 *cp
++ = digits
[*d
>> 4];
2055 *cp
++ = digits
[*d
& 0xf];
2060 return(ip6buf
[ip6round
]);
2065 struct in6_addr
*in6
;
2067 struct in6_ifaddr
*ia
;
2069 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
))
2072 lck_mtx_lock(nd6_mutex
);
2073 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
)
2074 if (IN6_ARE_MASKED_ADDR_EQUAL(in6
, &ia
->ia_addr
.sin6_addr
,
2075 &ia
->ia_prefixmask
.sin6_addr
)) {
2076 lck_mtx_unlock(nd6_mutex
);
2080 lck_mtx_unlock(nd6_mutex
);
2085 in6_is_addr_deprecated(sa6
)
2086 struct sockaddr_in6
*sa6
;
2088 struct in6_ifaddr
*ia
;
2090 lck_mtx_lock(nd6_mutex
);
2091 for (ia
= in6_ifaddrs
; ia
; ia
= ia
->ia_next
) {
2092 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
,
2095 ia
->ia_addr
.sin6_scope_id
== sa6
->sin6_scope_id
&&
2097 (ia
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0) {
2098 lck_mtx_unlock(nd6_mutex
);
2099 return(1); /* true */
2102 /* XXX: do we still have to go thru the rest of the list? */
2105 lck_mtx_unlock(nd6_mutex
);
2106 return(0); /* false */
2110 * return length of part which dst and src are equal
2114 in6_matchlen(src
, dst
)
2115 struct in6_addr
*src
, *dst
;
2118 u_char
*s
= (u_char
*)src
, *d
= (u_char
*)dst
;
2119 u_char
*lim
= s
+ 16, r
;
2122 if ((r
= (*d
++ ^ *s
++)) != 0) {
2133 /* XXX: to be scope conscious */
2135 in6_are_prefix_equal(p1
, p2
, len
)
2136 struct in6_addr
*p1
, *p2
;
2139 int bytelen
, bitlen
;
2142 if (0 > len
|| len
> 128) {
2143 log(LOG_ERR
, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2151 if (bcmp(&p1
->s6_addr
, &p2
->s6_addr
, bytelen
))
2154 p1
->s6_addr
[bytelen
] >> (8 - bitlen
) !=
2155 p2
->s6_addr
[bytelen
] >> (8 - bitlen
))
2162 in6_prefixlen2mask(maskp
, len
)
2163 struct in6_addr
*maskp
;
2166 u_char maskarray
[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2167 int bytelen
, bitlen
, i
;
2170 if (0 > len
|| len
> 128) {
2171 log(LOG_ERR
, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2176 bzero(maskp
, sizeof(*maskp
));
2179 for (i
= 0; i
< bytelen
; i
++)
2180 maskp
->s6_addr
[i
] = 0xff;
2182 maskp
->s6_addr
[bytelen
] = maskarray
[bitlen
- 1];
2186 * return the best address out of the same scope
2191 struct in6_addr
*dst
)
2193 int dst_scope
= in6_addrscope(dst
), src_scope
, best_scope
= 0;
2197 struct in6_ifaddr
*ifa_best
= NULL
;
2201 printf("in6_ifawithscope: output interface is not specified\n");
2207 * We search for all addresses on all interfaces from the beginning.
2208 * Comparing an interface with the outgoing interface will be done
2209 * only at the final stage of tiebreaking.
2211 ifnet_head_lock_shared();
2212 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
2214 * We can never take an address that breaks the scope zone
2215 * of the destination.
2217 if (in6_addr2scopeid(ifp
, dst
) != in6_addr2scopeid(oifp
, dst
))
2220 ifnet_lock_shared(ifp
);
2221 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2223 int tlen
= -1, dscopecmp
, bscopecmp
, matchcmp
;
2225 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2228 src_scope
= in6_addrscope(IFA_IN6(ifa
));
2231 * Don't use an address before completing DAD
2232 * nor a duplicated address.
2234 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2238 /* XXX: is there any case to allow anycasts? */
2239 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2243 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2248 * If this is the first address we find,
2251 if (ifa_best
== NULL
)
2255 * ifa_best is never NULL beyond this line except
2256 * within the block labeled "replace".
2260 * If ifa_best has a smaller scope than dst and
2261 * the current address has a larger one than
2262 * (or equal to) dst, always replace ifa_best.
2263 * Also, if the current address has a smaller scope
2264 * than dst, ignore it unless ifa_best also has a
2266 * Consequently, after the two if-clause below,
2267 * the followings must be satisfied:
2268 * (scope(src) < scope(dst) &&
2269 * scope(best) < scope(dst))
2271 * (scope(best) >= scope(dst) &&
2272 * scope(src) >= scope(dst))
2274 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0 &&
2275 IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) >= 0)
2276 goto replace
; /* (A) */
2277 if (IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) < 0 &&
2278 IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) >= 0)
2282 * A deprecated address SHOULD NOT be used in new
2283 * communications if an alternate (non-deprecated)
2284 * address is available and has sufficient scope.
2285 * RFC 2462, Section 5.5.4.
2287 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2288 IN6_IFF_DEPRECATED
) {
2290 * Ignore any deprecated addresses if
2291 * specified by configuration.
2293 if (!ip6_use_deprecated
)
2297 * If we have already found a non-deprecated
2298 * candidate, just ignore deprecated addresses.
2300 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
)
2306 * A non-deprecated address is always preferred
2307 * to a deprecated one regardless of scopes and
2308 * address matching (Note invariants ensured by the
2309 * conditions (A) and (B) above.)
2311 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) &&
2312 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2313 IN6_IFF_DEPRECATED
) == 0)
2317 * When we use temporary addresses described in
2318 * RFC 3041, we prefer temporary addresses to
2319 * public autoconf addresses. Again, note the
2320 * invariants from (A) and (B). Also note that we
2321 * don't have any preference between static addresses
2322 * and autoconf addresses (despite of whether or not
2323 * the latter is temporary or public.)
2325 if (ip6_use_tempaddr
) {
2326 struct in6_ifaddr
*ifat
;
2328 ifat
= (struct in6_ifaddr
*)ifa
;
2329 if ((ifa_best
->ia6_flags
&
2330 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2331 == IN6_IFF_AUTOCONF
&&
2333 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2334 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
)) {
2337 if ((ifa_best
->ia6_flags
&
2338 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2339 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
) &&
2341 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2342 == IN6_IFF_AUTOCONF
) {
2348 * At this point, we have two cases:
2349 * 1. we are looking at a non-deprecated address,
2350 * and ifa_best is also non-deprecated.
2351 * 2. we are looking at a deprecated address,
2352 * and ifa_best is also deprecated.
2353 * Also, we do not have to consider a case where
2354 * the scope of if_best is larger(smaller) than dst and
2355 * the scope of the current address is smaller(larger)
2356 * than dst. Such a case has already been covered.
2357 * Tiebreaking is done according to the following
2359 * - the scope comparison between the address and
2361 * - the scope comparison between the address and
2362 * ifa_best (bscopecmp)
2363 * - if the address match dst longer than ifa_best
2365 * - if the address is on the outgoing I/F (outI/F)
2367 * Roughly speaking, the selection policy is
2368 * - the most important item is scope. The same scope
2369 * is best. Then search for a larger scope.
2370 * Smaller scopes are the last resort.
2371 * - A deprecated address is chosen only when we have
2372 * no address that has an enough scope, but is
2373 * prefered to any addresses of smaller scopes
2374 * (this must be already done above.)
2375 * - addresses on the outgoing I/F are preferred to
2376 * ones on other interfaces if none of above
2377 * tiebreaks. In the table below, the column "bI"
2378 * means if the best_ifa is on the outgoing
2379 * interface, and the column "sI" means if the ifa
2380 * is on the outgoing interface.
2381 * - If there is no other reasons to choose one,
2382 * longest address match against dst is considered.
2384 * The precise decision table is as follows:
2385 * dscopecmp bscopecmp match bI oI | replace?
2386 * N/A equal N/A Y N | No (1)
2387 * N/A equal N/A N Y | Yes (2)
2388 * N/A equal larger N/A | Yes (3)
2389 * N/A equal !larger N/A | No (4)
2390 * larger larger N/A N/A | No (5)
2391 * larger smaller N/A N/A | Yes (6)
2392 * smaller larger N/A N/A | Yes (7)
2393 * smaller smaller N/A N/A | No (8)
2394 * equal smaller N/A N/A | Yes (9)
2395 * equal larger (already done at A above)
2397 dscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
);
2398 bscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, best_scope
);
2400 if (bscopecmp
== 0) {
2401 struct ifnet
*bifp
= ifa_best
->ia_ifp
;
2403 if (bifp
== oifp
&& ifp
!= oifp
) /* (1) */
2405 if (bifp
!= oifp
&& ifp
== oifp
) /* (2) */
2409 * Both bifp and ifp are on the outgoing
2410 * interface, or both two are on a different
2411 * interface from the outgoing I/F.
2412 * now we need address matching against dst
2415 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
2416 matchcmp
= tlen
- blen
;
2417 if (matchcmp
> 0) /* (3) */
2421 if (dscopecmp
> 0) {
2422 if (bscopecmp
> 0) /* (5) */
2424 goto replace
; /* (6) */
2426 if (dscopecmp
< 0) {
2427 if (bscopecmp
> 0) /* (7) */
2432 /* now dscopecmp must be 0 */
2434 goto replace
; /* (9) */
2439 ifafree(&ifa_best
->ia_ifa
);
2440 ifa_best
= (struct in6_ifaddr
*)ifa
;
2441 blen
= tlen
>= 0 ? tlen
:
2442 in6_matchlen(IFA_IN6(ifa
), dst
);
2443 best_scope
= in6_addrscope(&ifa_best
->ia_addr
.sin6_addr
);
2445 ifnet_lock_done(ifp
);
2449 /* count statistics for future improvements */
2450 if (ifa_best
== NULL
)
2451 ip6stat
.ip6s_sources_none
++;
2453 if (oifp
== ifa_best
->ia_ifp
)
2454 ip6stat
.ip6s_sources_sameif
[best_scope
]++;
2456 ip6stat
.ip6s_sources_otherif
[best_scope
]++;
2458 if (best_scope
== dst_scope
)
2459 ip6stat
.ip6s_sources_samescope
[best_scope
]++;
2461 ip6stat
.ip6s_sources_otherscope
[best_scope
]++;
2463 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0)
2464 ip6stat
.ip6s_sources_deprecated
[best_scope
]++;
2471 * return the best address out of the same scope. if no address was
2472 * found, return the first valid address from designated IF.
2477 struct in6_addr
*dst
)
2479 int dst_scope
= in6_addrscope(dst
), blen
= -1, tlen
;
2481 struct in6_ifaddr
*besta
= 0;
2482 struct in6_ifaddr
*dep
[2]; /* last-resort: deprecated */
2484 dep
[0] = dep
[1] = NULL
;
2487 * We first look for addresses in the same scope.
2488 * If there is one, return it.
2489 * If two or more, return one which matches the dst longest.
2490 * If none, return one of global addresses assigned other ifs.
2492 ifnet_lock_shared(ifp
);
2493 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2495 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2497 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
)
2498 continue; /* XXX: is there any case to allow anycast? */
2499 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_NOTREADY
)
2500 continue; /* don't use this interface */
2501 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DETACHED
)
2503 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
2504 if (ip6_use_deprecated
)
2505 dep
[0] = (struct in6_ifaddr
*)ifa
;
2509 if (dst_scope
== in6_addrscope(IFA_IN6(ifa
))) {
2511 * call in6_matchlen() as few as possible
2515 blen
= in6_matchlen(&besta
->ia_addr
.sin6_addr
, dst
);
2516 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
2519 besta
= (struct in6_ifaddr
*)ifa
;
2522 besta
= (struct in6_ifaddr
*)ifa
;
2526 ifnet_lock_done(ifp
);
2530 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2532 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2534 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
)
2535 continue; /* XXX: is there any case to allow anycast? */
2536 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_NOTREADY
)
2537 continue; /* don't use this interface */
2538 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DETACHED
)
2540 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
2541 if (ip6_use_deprecated
)
2542 dep
[1] = (struct in6_ifaddr
*)ifa
;
2546 ifnet_lock_done(ifp
);
2547 return (struct in6_ifaddr
*)ifa
;
2549 ifnet_lock_done(ifp
);
2551 /* use the last-resort values, that are, deprecated addresses */
2560 extern int in6_init2done
;
2563 * perform DAD when interface becomes IFF_UP.
2568 struct in6_aliasreq
*ifra
)
2571 struct in6_ifaddr
*ia
;
2572 int dad_delay
; /* delay ticks before DAD output */
2578 * special cases, like 6to4, are handled in in6_ifattach
2580 in6_ifattach(ifp
, NULL
, ifra
);
2583 ifnet_lock_exclusive(ifp
);
2584 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2586 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2588 ia
= (struct in6_ifaddr
*)ifa
;
2589 if (ia
->ia6_flags
& IN6_IFF_TENTATIVE
)
2590 nd6_dad_start(ifa
, &dad_delay
);
2592 ifnet_lock_done(ifp
);
2599 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0)
2602 switch (ifp
->if_type
) {
2608 * These interfaces do not have the IFF_LOOPBACK flag,
2609 * but loop packets back. We do not have to do DAD on such
2610 * interfaces. We should even omit it, because loop-backed
2611 * NS would confuse the DAD procedure.
2616 * Our DAD routine requires the interface up and running.
2617 * However, some interfaces can be up before the RUNNING
2618 * status. Additionaly, users may try to assign addresses
2619 * before the interface becomes up (or running).
2620 * We simply skip DAD in such a case as a work around.
2621 * XXX: we should rather mark "tentative" on such addresses,
2622 * and do DAD after the interface becomes ready.
2624 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) !=
2625 (IFF_UP
|IFF_RUNNING
))
2633 * Calculate max IPv6 MTU through all the interfaces and store it
2639 unsigned long maxmtu
= 0;
2642 ifnet_head_lock_shared();
2643 TAILQ_FOREACH(ifp
, &ifnet_head
, if_list
) {
2644 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
2645 nd_ifinfo
[ifp
->if_index
].linkmtu
> maxmtu
)
2646 maxmtu
= nd_ifinfo
[ifp
->if_index
].linkmtu
;
2649 if (maxmtu
) /* update only when maxmtu is positive */
2650 in6_maxmtu
= maxmtu
;
2654 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2655 * v4 mapped addr or v4 compat addr
2658 in6_sin6_2_sin(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
2660 bzero(sin
, sizeof(*sin
));
2661 sin
->sin_len
= sizeof(struct sockaddr_in
);
2662 sin
->sin_family
= AF_INET
;
2663 sin
->sin_port
= sin6
->sin6_port
;
2664 sin
->sin_addr
.s_addr
= sin6
->sin6_addr
.s6_addr32
[3];
2667 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2669 in6_sin_2_v4mapsin6(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
2671 bzero(sin6
, sizeof(*sin6
));
2672 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2673 sin6
->sin6_family
= AF_INET6
;
2674 sin6
->sin6_port
= sin
->sin_port
;
2675 sin6
->sin6_addr
.s6_addr32
[0] = 0;
2676 sin6
->sin6_addr
.s6_addr32
[1] = 0;
2677 sin6
->sin6_addr
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
2678 sin6
->sin6_addr
.s6_addr32
[3] = sin
->sin_addr
.s_addr
;
2681 /* Convert sockaddr_in6 into sockaddr_in. */
2683 in6_sin6_2_sin_in_sock(struct sockaddr
*nam
)
2685 struct sockaddr_in
*sin_p
;
2686 struct sockaddr_in6 sin6
;
2689 * Save original sockaddr_in6 addr and convert it
2692 sin6
= *(struct sockaddr_in6
*)nam
;
2693 sin_p
= (struct sockaddr_in
*)nam
;
2694 in6_sin6_2_sin(sin_p
, &sin6
);
2697 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2699 in6_sin_2_v4mapsin6_in_sock(struct sockaddr
**nam
)
2701 struct sockaddr_in
*sin_p
;
2702 struct sockaddr_in6
*sin6_p
;
2704 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof *sin6_p
, M_SONAME
,
2706 sin_p
= (struct sockaddr_in
*)*nam
;
2707 in6_sin_2_v4mapsin6(sin_p
, sin6_p
);
2708 FREE(*nam
, M_SONAME
);
2709 *nam
= (struct sockaddr
*)sin6_p
;
2712 /* Posts in6_event_data message kernel events */
2714 in6_post_msg(struct ifnet
*ifp
, u_long event_code
, struct in6_ifaddr
*ifa
)
2716 struct kev_msg ev_msg
;
2717 struct kev_in6_data in6_event_data
;
2719 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2720 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2721 ev_msg
.kev_subclass
= KEV_INET6_SUBCLASS
;
2722 ev_msg
.event_code
= event_code
;
2724 in6_event_data
.ia_addr
= ifa
->ia_addr
;
2725 in6_event_data
.ia_net
= ifa
->ia_net
;
2726 in6_event_data
.ia_dstaddr
= ifa
->ia_dstaddr
;
2727 in6_event_data
.ia_prefixmask
= ifa
->ia_prefixmask
;
2728 in6_event_data
.ia_plen
= ifa
->ia_plen
;
2729 in6_event_data
.ia6_flags
= (u_int32_t
)ifa
->ia6_flags
;
2730 in6_event_data
.ia_lifetime
= ifa
->ia6_lifetime
;
2733 strncpy(&in6_event_data
.link_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2734 in6_event_data
.link_data
.if_family
= ifp
->if_family
;
2735 in6_event_data
.link_data
.if_unit
= (unsigned long) ifp
->if_unit
;
2738 ev_msg
.dv
[0].data_ptr
= &in6_event_data
;
2739 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in6_data
);
2740 ev_msg
.dv
[1].data_length
= 0;
2742 kev_post_msg(&ev_msg
);