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>
83 #include <net/if_types.h>
84 #include <net/route.h>
85 #include <net/if_dl.h>
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/if_ether.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/in_pcb.h>
96 #include <netinet6/nd6.h>
97 #include <netinet/ip6.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet6/mld6_var.h>
100 #include <netinet6/ip6_mroute.h>
101 #include <netinet6/in6_ifattach.h>
102 #include <netinet6/scope6_var.h>
103 #ifndef SCOPEDROUTING
104 #include <netinet6/in6_pcb.h>
107 #include <net/net_osdep.h>
110 MALLOC_DEFINE(M_IPMADDR
, "in6_multi", "internet multicast address");
113 * Definitions of some costant IP6 addresses.
115 const struct in6_addr in6addr_any
= IN6ADDR_ANY_INIT
;
116 const struct in6_addr in6addr_loopback
= IN6ADDR_LOOPBACK_INIT
;
117 const struct in6_addr in6addr_nodelocal_allnodes
=
118 IN6ADDR_NODELOCAL_ALLNODES_INIT
;
119 const struct in6_addr in6addr_linklocal_allnodes
=
120 IN6ADDR_LINKLOCAL_ALLNODES_INIT
;
121 const struct in6_addr in6addr_linklocal_allrouters
=
122 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT
;
124 const struct in6_addr in6mask0
= IN6MASK0
;
125 const struct in6_addr in6mask32
= IN6MASK32
;
126 const struct in6_addr in6mask64
= IN6MASK64
;
127 const struct in6_addr in6mask96
= IN6MASK96
;
128 const struct in6_addr in6mask128
= IN6MASK128
;
130 const struct sockaddr_in6 sa6_any
= {sizeof(sa6_any
), AF_INET6
,
131 0, 0, IN6ADDR_ANY_INIT
, 0};
133 static int in6_lifaddr_ioctl
__P((struct socket
*, u_long
, caddr_t
,
134 struct ifnet
*, struct proc
*));
135 static int in6_ifinit
__P((struct ifnet
*, struct in6_ifaddr
*,
136 struct sockaddr_in6
*, int));
137 static void in6_unlink_ifa
__P((struct in6_ifaddr
*, struct ifnet
*));
139 struct in6_multihead in6_multihead
; /* XXX BSS initialization */
142 * Subroutine for in6_ifaddloop() and in6_ifremloop().
143 * This routine does actual work.
146 in6_ifloop_request(int cmd
, struct ifaddr
*ifa
)
148 struct sockaddr_in6 all1_sa
;
149 struct rtentry
*nrt
= NULL
;
152 bzero(&all1_sa
, sizeof(all1_sa
));
153 all1_sa
.sin6_family
= AF_INET6
;
154 all1_sa
.sin6_len
= sizeof(struct sockaddr_in6
);
155 all1_sa
.sin6_addr
= in6mask128
;
158 * We specify the address itself as the gateway, and set the
159 * RTF_LLINFO flag, so that the corresponding host route would have
160 * the flag, and thus applications that assume traditional behavior
161 * would be happy. Note that we assume the caller of the function
162 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
163 * which changes the outgoing interface to the loopback interface.
165 e
= rtrequest(cmd
, ifa
->ifa_addr
, ifa
->ifa_addr
,
166 (struct sockaddr
*)&all1_sa
,
167 RTF_UP
|RTF_HOST
|RTF_LLINFO
, &nrt
);
169 log(LOG_ERR
, "in6_ifloop_request: "
170 "%s operation failed for %s (errno=%d)\n",
171 cmd
== RTM_ADD
? "ADD" : "DELETE",
172 ip6_sprintf(&((struct in6_ifaddr
*)ifa
)->ia_addr
.sin6_addr
),
177 * Make sure rt_ifa be equal to IFA, the second argument of the
179 * We need this because when we refer to rt_ifa->ia6_flags in
180 * ip6_input, we assume that the rt_ifa points to the address instead
181 * of the loopback address.
183 if (cmd
== RTM_ADD
&& nrt
&& ifa
!= nrt
->rt_ifa
) {
185 nrt
->rt_dlt
= ifa
->ifa_dlt
;
189 * Report the addition/removal of the address to the routing socket.
190 * XXX: since we called rtinit for a p2p interface with a destination,
191 * we end up reporting twice in such a case. Should we rather
192 * omit the second report?
195 rt_newaddrmsg(cmd
, ifa
, e
, nrt
);
196 if (cmd
== RTM_DELETE
) {
197 if (nrt
->rt_refcnt
<= 0) {
198 /* XXX: we should free the entry ourselves. */
203 /* the cmd must be RTM_ADD here */
210 * Add ownaddr as loopback rtentry. We previously add the route only if
211 * necessary (ex. on a p2p link). However, since we now manage addresses
212 * separately from prefixes, we should always add the route. We can't
213 * rely on the cloning mechanism from the corresponding interface route
217 in6_ifaddloop(struct ifaddr
*ifa
)
221 /* If there is no loopback entry, allocate one. */
222 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
223 if (rt
== NULL
|| (rt
->rt_flags
& RTF_HOST
) == 0 ||
224 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) == 0)
225 in6_ifloop_request(RTM_ADD
, ifa
);
231 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
235 in6_ifremloop(struct ifaddr
*ifa
)
237 struct in6_ifaddr
*ia
;
242 * Some of BSD variants do not remove cloned routes
243 * from an interface direct route, when removing the direct route
244 * (see comments in net/net_osdep.h). Even for variants that do remove
245 * cloned routes, they could fail to remove the cloned routes when
246 * we handle multple addresses that share a common prefix.
247 * So, we should remove the route corresponding to the deleted address
248 * regardless of the result of in6_is_ifloop_auto().
252 * Delete the entry only if exact one ifa exists. More than one ifa
253 * can exist if we assign a same single address to multiple
254 * (probably p2p) interfaces.
255 * XXX: we should avoid such a configuration in IPv6...
257 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
) {
258 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa
), &ia
->ia_addr
.sin6_addr
)) {
267 * Before deleting, check if a corresponding loopbacked host
268 * route surely exists. With this check, we can avoid to
269 * delete an interface direct route whose destination is same
270 * as the address being removed. This can happen when remofing
271 * a subnet-router anycast address on an interface attahced
272 * to a shared medium.
274 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
275 if (rt
!= NULL
&& (rt
->rt_flags
& RTF_HOST
) != 0 &&
276 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
278 in6_ifloop_request(RTM_DELETE
, ifa
);
284 in6_ifindex2scopeid(idx
)
289 struct sockaddr_in6
*sin6
;
291 if (idx
< 0 || if_index
< idx
)
293 ifp
= ifindex2ifnet
[idx
];
295 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
297 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
299 sin6
= (struct sockaddr_in6
*)ifa
->ifa_addr
;
300 if (IN6_IS_ADDR_SITELOCAL(&sin6
->sin6_addr
))
301 return sin6
->sin6_scope_id
& 0xffff;
308 in6_mask2len(mask
, lim0
)
309 struct in6_addr
*mask
;
313 u_char
*lim
= lim0
, *p
;
316 lim0
- (u_char
*)mask
> sizeof(*mask
)) /* ignore the scope_id part */
317 lim
= (u_char
*)mask
+ sizeof(*mask
);
318 for (p
= (u_char
*)mask
; p
< lim
; x
++, p
++) {
324 for (y
= 0; y
< 8; y
++) {
325 if ((*p
& (0x80 >> y
)) == 0)
331 * when the limit pointer is given, do a stricter check on the
335 if (y
!= 0 && (*p
& (0x00ff >> y
)) != 0)
337 for (p
= p
+ 1; p
< lim
; p
++)
346 in6_len2mask(mask
, len
)
347 struct in6_addr
*mask
;
352 bzero(mask
, sizeof(*mask
));
353 for (i
= 0; i
< len
/ 8; i
++)
354 mask
->s6_addr8
[i
] = 0xff;
356 mask
->s6_addr8
[i
] = (0xff00 >> (len
% 8)) & 0xff;
359 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
360 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
363 in6_control(so
, cmd
, data
, ifp
, p
)
370 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)data
;
371 struct in6_ifaddr
*ia
= NULL
;
372 struct in6_aliasreq
*ifra
= (struct in6_aliasreq
*)data
;
373 int privileged
, error
= 0;
378 if (p
== NULL
|| !suser(p
->p_ucred
, &p
->p_acflag
))
380 if (p
== NULL
|| !suser(p
))
385 case SIOCGETSGCNT_IN6
:
386 case SIOCGETMIFCNT_IN6
:
387 return (mrt6_ioctl(cmd
, data
));
394 case SIOCSNDFLUSH_IN6
:
395 case SIOCSPFXFLUSH_IN6
:
396 case SIOCSRTRFLUSH_IN6
:
397 case SIOCSDEFIFACE_IN6
:
398 case SIOCSIFINFO_FLAGS
:
402 case OSIOCGIFINFO_IN6
:
403 case SIOCGIFINFO_IN6
:
406 case SIOCGNBRINFO_IN6
:
407 case SIOCGDEFIFACE_IN6
:
408 return(nd6_ioctl(cmd
, data
, ifp
));
412 case SIOCSIFPREFIX_IN6
:
413 case SIOCDIFPREFIX_IN6
:
414 case SIOCAIFPREFIX_IN6
:
415 case SIOCCIFPREFIX_IN6
:
416 case SIOCSGIFPREFIX_IN6
:
417 case SIOCGIFPREFIX_IN6
:
419 "prefix ioctls are now invalidated. "
420 "please use ifconfig.\n");
428 return(scope6_set(ifp
, ifr
->ifr_ifru
.ifru_scope_id
));
431 return(scope6_get(ifp
, ifr
->ifr_ifru
.ifru_scope_id
));
434 return(scope6_get_default(ifr
->ifr_ifru
.ifru_scope_id
));
445 return in6_lifaddr_ioctl(so
, cmd
, data
, ifp
, p
);
452 case SIOCPROTOATTACH
:
455 case SIOCPROTODETACH
:
457 switch (ifp
->if_type
) {
459 error
= ether_detach_inet6(ifp
);
462 error
= gif_detach_proto_family(ifp
, PF_INET6
);
465 error
= stf_detach_inet6(ifp
);
467 case IFT_LOOP
: /* do not detach loopback */
470 printf("SIOCPROTODETACH: %s%d unknown type, can't detach\n",
471 ifp
->if_name
, ifp
->if_unit
);
476 printf("SIOCPROTODETACH: %s%d ether_detach_inet6 error=%x\n",
477 ifp
->if_name
, ifp
->if_unit
, error
);
485 * Find address for this interface, if it exists.
487 if (ifra
->ifra_addr
.sin6_family
== AF_INET6
) { /* XXX */
488 struct sockaddr_in6
*sa6
=
489 (struct sockaddr_in6
*)&ifra
->ifra_addr
;
491 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
492 if (sa6
->sin6_addr
.s6_addr16
[1] == 0) {
493 /* link ID is not embedded by the user */
494 sa6
->sin6_addr
.s6_addr16
[1] =
495 htons(ifp
->if_index
);
496 } else if (sa6
->sin6_addr
.s6_addr16
[1] !=
497 htons(ifp
->if_index
)) {
498 return(EINVAL
); /* link ID contradicts */
500 if (sa6
->sin6_scope_id
) {
501 if (sa6
->sin6_scope_id
!=
502 (u_int32_t
)ifp
->if_index
)
504 sa6
->sin6_scope_id
= 0; /* XXX: good way? */
507 ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
);
511 case SIOCSIFADDR_IN6
:
512 case SIOCSIFDSTADDR_IN6
:
513 case SIOCSIFNETMASK_IN6
:
515 * Since IPv6 allows a node to assign multiple addresses
516 * on a single interface, SIOCSIFxxx ioctls are not suitable
517 * and should be unused.
519 /* we decided to obsolete this command (20000704) */
522 case SIOCDIFADDR_IN6
:
524 * for IPv4, we look for existing in_ifaddr here to allow
525 * "ifconfig if0 delete" to remove first IPv4 address on the
526 * interface. For IPv6, as the spec allow multiple interface
527 * address from the day one, we consider "remove the first one"
528 * semantics to be not preferable.
531 return(EADDRNOTAVAIL
);
533 case SIOCAIFADDR_IN6
:
535 * We always require users to specify a valid IPv6 address for
536 * the corresponding operation.
538 if (ifra
->ifra_addr
.sin6_family
!= AF_INET6
||
539 ifra
->ifra_addr
.sin6_len
!= sizeof(struct sockaddr_in6
))
540 return(EAFNOSUPPORT
);
546 case SIOCGIFADDR_IN6
:
547 /* This interface is basically deprecated. use SIOCGIFCONF. */
549 case SIOCGIFAFLAG_IN6
:
550 case SIOCGIFNETMASK_IN6
:
551 case SIOCGIFDSTADDR_IN6
:
552 case SIOCGIFALIFETIME_IN6
:
553 /* must think again about its semantics */
555 return(EADDRNOTAVAIL
);
557 case SIOCSIFALIFETIME_IN6
:
559 struct in6_addrlifetime
*lt
;
564 return(EADDRNOTAVAIL
);
565 /* sanity for overflow - beware unsigned */
566 lt
= &ifr
->ifr_ifru
.ifru_lifetime
;
567 if (lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
568 && lt
->ia6t_vltime
+ time_second
< time_second
) {
571 if (lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
572 && lt
->ia6t_pltime
+ time_second
< time_second
) {
581 case SIOCGIFADDR_IN6
:
582 ifr
->ifr_addr
= ia
->ia_addr
;
585 case SIOCGIFDSTADDR_IN6
:
586 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
589 * XXX: should we check if ifa_dstaddr is NULL and return
592 ifr
->ifr_dstaddr
= ia
->ia_dstaddr
;
595 case SIOCGIFNETMASK_IN6
:
596 ifr
->ifr_addr
= ia
->ia_prefixmask
;
599 case SIOCGIFAFLAG_IN6
:
600 ifr
->ifr_ifru
.ifru_flags6
= ia
->ia6_flags
;
603 case SIOCGIFSTAT_IN6
:
606 if (in6_ifstat
== NULL
|| ifp
->if_index
>= in6_ifstatmax
607 || in6_ifstat
[ifp
->if_index
] == NULL
) {
608 /* return EAFNOSUPPORT? */
609 bzero(&ifr
->ifr_ifru
.ifru_stat
,
610 sizeof(ifr
->ifr_ifru
.ifru_stat
));
612 ifr
->ifr_ifru
.ifru_stat
= *in6_ifstat
[ifp
->if_index
];
615 case SIOCGIFSTAT_ICMP6
:
618 if (icmp6_ifstat
== NULL
|| ifp
->if_index
>= icmp6_ifstatmax
||
619 icmp6_ifstat
[ifp
->if_index
] == NULL
) {
620 /* return EAFNOSUPPORT? */
621 bzero(&ifr
->ifr_ifru
.ifru_stat
,
622 sizeof(ifr
->ifr_ifru
.ifru_icmp6stat
));
624 ifr
->ifr_ifru
.ifru_icmp6stat
=
625 *icmp6_ifstat
[ifp
->if_index
];
628 case SIOCGIFALIFETIME_IN6
:
629 ifr
->ifr_ifru
.ifru_lifetime
= ia
->ia6_lifetime
;
632 case SIOCSIFALIFETIME_IN6
:
633 ia
->ia6_lifetime
= ifr
->ifr_ifru
.ifru_lifetime
;
635 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
636 ia
->ia6_lifetime
.ia6t_expire
=
637 time_second
+ ia
->ia6_lifetime
.ia6t_vltime
;
639 ia
->ia6_lifetime
.ia6t_expire
= 0;
640 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
641 ia
->ia6_lifetime
.ia6t_preferred
=
642 time_second
+ ia
->ia6_lifetime
.ia6t_pltime
;
644 ia
->ia6_lifetime
.ia6t_preferred
= 0;
647 case SIOCAIFADDR_IN6
:
650 struct nd_prefix pr0
, *pr
;
652 if (dlil_find_dltag(ifp
->if_family
, ifp
->if_unit
, PF_INET6
, &dl_tag
) == EPROTONOSUPPORT
) {
653 in6_if_up(ifp
); /* no dl_tag, the interface is not "up" for IPv6 yet */
657 * first, make or update the interface address structure,
658 * and link it to the list.
660 if ((error
= in6_update_ifa(ifp
, ifra
, ia
)) != 0)
664 * then, make the prefix on-link on the interface.
665 * XXX: we'd rather create the prefix before the address, but
666 * we need at least one address to install the corresponding
667 * interface route, so we configure the address first.
671 * convert mask to prefix length (prefixmask has already
672 * been validated in in6_update_ifa().
674 bzero(&pr0
, sizeof(pr0
));
676 pr0
.ndpr_plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
678 if (pr0
.ndpr_plen
== 128)
679 break; /* we don't need to install a host route. */
680 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
681 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
682 /* apply the mask for safety. */
683 for (i
= 0; i
< 4; i
++) {
684 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
685 ifra
->ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
688 * XXX: since we don't have enough APIs, we just set inifinity
689 * to lifetimes. They can be overridden by later advertised
690 * RAs (when accept_rtadv is non 0), but we'd rather intend
693 pr0
.ndpr_raf_onlink
= 1; /* should be configurable? */
695 ((ifra
->ifra_flags
& IN6_IFF_AUTOCONF
) != 0);
696 pr0
.ndpr_vltime
= ifra
->ifra_lifetime
.ia6t_vltime
;
697 pr0
.ndpr_pltime
= ifra
->ifra_lifetime
.ia6t_pltime
;
699 /* add the prefix if there's one. */
700 if ((pr
= nd6_prefix_lookup(&pr0
)) == NULL
) {
702 * nd6_prelist_add will install the corresponding
705 if ((error
= nd6_prelist_add(&pr0
, NULL
, &pr
)) != 0)
708 log(LOG_ERR
, "nd6_prelist_add succedded but "
710 return(EINVAL
); /* XXX panic here? */
713 if ((ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
))
715 /* XXX: this should not happen! */
716 log(LOG_ERR
, "in6_control: addition succeeded, but"
719 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
720 ia
->ia6_ndpr
== NULL
) { /* new autoconfed addr */
725 * If this is the first autoconf address from
726 * the prefix, create a temporary address
727 * as well (when specified).
729 if (ip6_use_tempaddr
&&
730 pr
->ndpr_refcnt
== 1) {
732 if ((e
= in6_tmpifadd(ia
, 1)) != 0) {
733 log(LOG_NOTICE
, "in6_control: "
734 "failed to create a "
735 "temporary address, "
743 * this might affect the status of autoconfigured
744 * addresses, that is, this address might make
745 * other addresses detached.
747 pfxlist_onlink_check();
750 dlil_find_dltag(ifp
->if_family
, ifp
->if_unit
, PF_INET6
, &dl_tag
);
751 ia
->ia_ifa
.ifa_dlt
= dl_tag
;
753 in6_post_msg(ifp
, KEV_INET6_NEW_USER_ADDR
, ia
);
757 case SIOCDIFADDR_IN6
:
760 struct nd_prefix pr0
, *pr
;
763 * If the address being deleted is the only one that owns
764 * the corresponding prefix, expire the prefix as well.
765 * XXX: theoretically, we don't have to warry about such
766 * relationship, since we separate the address management
767 * and the prefix management. We do this, however, to provide
768 * as much backward compatibility as possible in terms of
769 * the ioctl operation.
771 bzero(&pr0
, sizeof(pr0
));
773 pr0
.ndpr_plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
775 if (pr0
.ndpr_plen
== 128)
777 pr0
.ndpr_prefix
= ia
->ia_addr
;
778 pr0
.ndpr_mask
= ia
->ia_prefixmask
.sin6_addr
;
779 for (i
= 0; i
< 4; i
++) {
780 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
781 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
];
784 * The logic of the following condition is a bit complicated.
785 * We expire the prefix when
786 * 1. the address obeys autoconfiguration and it is the
787 * only owner of the associated prefix, or
788 * 2. the address does not obey autoconf and there is no
789 * other owner of the prefix.
791 if ((pr
= nd6_prefix_lookup(&pr0
)) != NULL
&&
792 (((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
793 pr
->ndpr_refcnt
== 1) ||
794 ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 &&
795 pr
->ndpr_refcnt
== 0))) {
796 pr
->ndpr_expire
= 1; /* XXX: just for expiration */
800 in6_purgeaddr(&ia
->ia_ifa
);
806 error
= dlil_ioctl(0, ifp
, cmd
, (caddr_t
)data
);
807 if (error
== EOPNOTSUPP
)
812 if (ifp
== NULL
|| ifp
->if_ioctl
== 0)
814 return((*ifp
->if_ioctl
)(ifp
, cmd
, data
));
822 * Update parameters of an IPv6 interface address.
823 * If necessary, a new entry is created and linked into address chains.
824 * This function is separated from in6_control().
825 * XXX: should this be performed under splnet()?
828 in6_update_ifa(ifp
, ifra
, ia
)
830 struct in6_aliasreq
*ifra
;
831 struct in6_ifaddr
*ia
;
833 int error
= 0, hostIsNew
= 0, plen
= -1;
834 struct in6_ifaddr
*oia
;
835 struct sockaddr_in6 dst6
;
836 struct in6_addrlifetime
*lt
;
838 /* Validate parameters */
839 if (ifp
== NULL
|| ifra
== NULL
) /* this maybe redundant */
843 * The destination address for a p2p link must have a family
844 * of AF_UNSPEC or AF_INET6.
846 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
847 ifra
->ifra_dstaddr
.sin6_family
!= AF_INET6
&&
848 ifra
->ifra_dstaddr
.sin6_family
!= AF_UNSPEC
)
849 return(EAFNOSUPPORT
);
851 * validate ifra_prefixmask. don't check sin6_family, netmask
852 * does not carry fields other than sin6_len.
854 if (ifra
->ifra_prefixmask
.sin6_len
> sizeof(struct sockaddr_in6
))
857 * Because the IPv6 address architecture is classless, we require
858 * users to specify a (non 0) prefix length (mask) for a new address.
859 * We also require the prefix (when specified) mask is valid, and thus
860 * reject a non-consecutive mask.
862 if (ia
== NULL
&& ifra
->ifra_prefixmask
.sin6_len
== 0)
864 if (ifra
->ifra_prefixmask
.sin6_len
!= 0) {
865 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
866 (u_char
*)&ifra
->ifra_prefixmask
+
867 ifra
->ifra_prefixmask
.sin6_len
);
873 * In this case, ia must not be NULL. We just use its prefix
876 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
);
879 * If the destination address on a p2p interface is specified,
880 * and the address is a scoped one, validate/set the scope
883 dst6
= ifra
->ifra_dstaddr
;
884 if ((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) &&
885 (dst6
.sin6_family
== AF_INET6
)) {
888 #ifndef SCOPEDROUTING
889 if ((error
= in6_recoverscope(&dst6
,
890 &ifra
->ifra_dstaddr
.sin6_addr
,
894 scopeid
= in6_addr2scopeid(ifp
, &dst6
.sin6_addr
);
895 if (dst6
.sin6_scope_id
== 0) /* user omit to specify the ID. */
896 dst6
.sin6_scope_id
= scopeid
;
897 else if (dst6
.sin6_scope_id
!= scopeid
)
898 return(EINVAL
); /* scope ID mismatch. */
899 #ifndef SCOPEDROUTING
900 if ((error
= in6_embedscope(&dst6
.sin6_addr
, &dst6
, NULL
, NULL
))
903 dst6
.sin6_scope_id
= 0; /* XXX */
907 * The destination address can be specified only for a p2p or a
908 * loopback interface. If specified, the corresponding prefix length
911 if (ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
) {
912 if ((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) == 0) {
913 /* XXX: noisy message */
914 log(LOG_INFO
, "in6_update_ifa: a destination can be "
915 "specified for a p2p or a loopback IF only\n");
920 * The following message seems noisy, but we dare to
921 * add it for diagnosis.
923 log(LOG_INFO
, "in6_update_ifa: prefixlen must be 128 "
924 "when dstaddr is specified\n");
928 /* lifetime consistency check */
929 lt
= &ifra
->ifra_lifetime
;
930 if (lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
931 && lt
->ia6t_vltime
+ time_second
< time_second
) {
934 if (lt
->ia6t_vltime
== 0) {
936 * the following log might be noisy, but this is a typical
937 * configuration mistake or a tool's bug.
940 "in6_update_ifa: valid lifetime is 0 for %s\n",
941 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
943 if (lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
944 && lt
->ia6t_pltime
+ time_second
< time_second
) {
949 * If this is a new address, allocate a new ifaddr and link it
955 * When in6_update_ifa() is called in a process of a received
956 * RA, it is called under splnet(). So, we should call malloc
959 ia
= (struct in6_ifaddr
*)
960 _MALLOC(sizeof(*ia
), M_IFADDR
, M_NOWAIT
);
963 bzero((caddr_t
)ia
, sizeof(*ia
));
964 /* Initialize the address and masks */
965 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
966 ia
->ia_addr
.sin6_family
= AF_INET6
;
967 ia
->ia_addr
.sin6_len
= sizeof(ia
->ia_addr
);
968 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) != 0) {
970 * XXX: some functions expect that ifa_dstaddr is not
971 * NULL for p2p interfaces.
973 ia
->ia_ifa
.ifa_dstaddr
974 = (struct sockaddr
*)&ia
->ia_dstaddr
;
976 ia
->ia_ifa
.ifa_dstaddr
= NULL
;
978 ia
->ia_ifa
.ifa_netmask
979 = (struct sockaddr
*)&ia
->ia_prefixmask
;
982 if ((oia
= in6_ifaddr
) != NULL
) {
983 for ( ; oia
->ia_next
; oia
= oia
->ia_next
)
989 TAILQ_INSERT_TAIL(&ifp
->if_addrlist
, &ia
->ia_ifa
,
993 /* set prefix mask */
994 if (ifra
->ifra_prefixmask
.sin6_len
) {
996 * We prohibit changing the prefix length of an existing
998 * + such an operation should be rare in IPv6, and
999 * + the operation would confuse prefix management.
1001 if (ia
->ia_prefixmask
.sin6_len
&&
1002 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
) != plen
) {
1003 log(LOG_INFO
, "in6_update_ifa: the prefix length of an"
1004 " existing (%s) address should not be changed\n",
1005 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1009 ia
->ia_prefixmask
= ifra
->ifra_prefixmask
;
1013 * If a new destination address is specified, scrub the old one and
1014 * install the new destination. Note that the interface must be
1015 * p2p or loopback (see the check above.)
1017 if (dst6
.sin6_family
== AF_INET6
&&
1018 !IN6_ARE_ADDR_EQUAL(&dst6
.sin6_addr
,
1019 &ia
->ia_dstaddr
.sin6_addr
)) {
1022 if ((ia
->ia_flags
& IFA_ROUTE
) != 0 &&
1023 (e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
))
1025 log(LOG_ERR
, "in6_update_ifa: failed to remove "
1026 "a route to the old destination: %s\n",
1027 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1028 /* proceed anyway... */
1031 ia
->ia_flags
&= ~IFA_ROUTE
;
1032 ia
->ia_dstaddr
= dst6
;
1035 /* reset the interface and routing table appropriately. */
1036 if ((error
= in6_ifinit(ifp
, ia
, &ifra
->ifra_addr
, hostIsNew
)) != 0)
1040 * Beyond this point, we should call in6_purgeaddr upon an error,
1041 * not just go to unlink.
1044 #if 0 /* disable this mechanism for now */
1045 /* update prefix list */
1047 (ifra
->ifra_flags
& IN6_IFF_NOPFX
) == 0) { /* XXX */
1050 iilen
= (sizeof(ia
->ia_prefixmask
.sin6_addr
) << 3) - plen
;
1051 if ((error
= in6_prefix_add_ifid(iilen
, ia
)) != 0) {
1052 in6_purgeaddr((struct ifaddr
*)ia
);
1058 if ((ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1059 struct sockaddr_in6 mltaddr
, mltmask
;
1060 struct in6_multi
*in6m
;
1064 * join solicited multicast addr for new host id
1066 struct in6_addr llsol
;
1067 bzero(&llsol
, sizeof(struct in6_addr
));
1068 llsol
.s6_addr16
[0] = htons(0xff02);
1069 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1070 llsol
.s6_addr32
[1] = 0;
1071 llsol
.s6_addr32
[2] = htonl(1);
1072 llsol
.s6_addr32
[3] =
1073 ifra
->ifra_addr
.sin6_addr
.s6_addr32
[3];
1074 llsol
.s6_addr8
[12] = 0xff;
1075 (void)in6_addmulti(&llsol
, ifp
, &error
);
1078 "in6_update_ifa: addmulti failed for "
1079 "%s on %s (errno=%d)\n",
1080 ip6_sprintf(&llsol
), if_name(ifp
),
1082 in6_purgeaddr((struct ifaddr
*)ia
);
1087 bzero(&mltmask
, sizeof(mltmask
));
1088 mltmask
.sin6_len
= sizeof(struct sockaddr_in6
);
1089 mltmask
.sin6_family
= AF_INET6
;
1090 mltmask
.sin6_addr
= in6mask32
;
1093 * join link-local all-nodes address
1095 bzero(&mltaddr
, sizeof(mltaddr
));
1096 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
1097 mltaddr
.sin6_family
= AF_INET6
;
1098 mltaddr
.sin6_addr
= in6addr_linklocal_allnodes
;
1099 mltaddr
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
1101 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1104 (struct sockaddr
*)&mltaddr
,
1105 (struct sockaddr
*)&ia
->ia_addr
,
1106 (struct sockaddr
*)&mltmask
,
1107 RTF_UP
|RTF_CLONING
, /* xxx */
1108 (struct rtentry
**)0);
1109 (void)in6_addmulti(&mltaddr
.sin6_addr
, ifp
, &error
);
1112 "in6_update_ifa: addmulti failed for "
1113 "%s on %s (errno=%d)\n",
1114 ip6_sprintf(&mltaddr
.sin6_addr
),
1115 if_name(ifp
), error
);
1120 * join node information group address
1122 #define hostnamelen strlen(hostname)
1123 if (in6_nigroup(ifp
, hostname
, hostnamelen
, &mltaddr
.sin6_addr
)
1125 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1126 if (in6m
== NULL
&& ia
!= NULL
) {
1127 (void)in6_addmulti(&mltaddr
.sin6_addr
,
1130 log(LOG_WARNING
, "in6_update_ifa: "
1131 "addmulti failed for "
1132 "%s on %s (errno=%d)\n",
1133 ip6_sprintf(&mltaddr
.sin6_addr
),
1134 if_name(ifp
), error
);
1141 * join node-local all-nodes address, on loopback.
1142 * XXX: since "node-local" is obsoleted by interface-local,
1143 * we have to join the group on every interface with
1144 * some interface-boundary restriction.
1146 if (ifp
->if_flags
& IFF_LOOPBACK
) {
1147 struct in6_ifaddr
*ia_loop
;
1149 struct in6_addr loop6
= in6addr_loopback
;
1150 ia_loop
= in6ifa_ifpwithaddr(ifp
, &loop6
);
1152 mltaddr
.sin6_addr
= in6addr_nodelocal_allnodes
;
1154 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1155 if (in6m
== NULL
&& ia_loop
!= NULL
) {
1157 (struct sockaddr
*)&mltaddr
,
1158 (struct sockaddr
*)&ia_loop
->ia_addr
,
1159 (struct sockaddr
*)&mltmask
,
1161 (struct rtentry
**)0);
1162 (void)in6_addmulti(&mltaddr
.sin6_addr
, ifp
,
1165 log(LOG_WARNING
, "in6_update_ifa: "
1166 "addmulti failed for %s on %s "
1168 ip6_sprintf(&mltaddr
.sin6_addr
),
1169 if_name(ifp
), error
);
1175 ia
->ia6_flags
= ifra
->ifra_flags
;
1176 ia
->ia6_flags
&= ~IN6_IFF_DUPLICATED
; /*safety*/
1177 ia
->ia6_flags
&= ~IN6_IFF_NODAD
; /* Mobile IPv6 */
1179 ia
->ia6_lifetime
= ifra
->ifra_lifetime
;
1181 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
1182 ia
->ia6_lifetime
.ia6t_expire
=
1183 time_second
+ ia
->ia6_lifetime
.ia6t_vltime
;
1185 ia
->ia6_lifetime
.ia6t_expire
= 0;
1186 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
1187 ia
->ia6_lifetime
.ia6t_preferred
=
1188 time_second
+ ia
->ia6_lifetime
.ia6t_pltime
;
1190 ia
->ia6_lifetime
.ia6t_preferred
= 0;
1193 * make sure to initialize ND6 information. this is to workaround
1194 * issues with interfaces with IPv6 addresses, which have never brought
1195 * up. We are assuming that it is safe to nd6_ifattach multiple times.
1200 * Perform DAD, if needed.
1201 * XXX It may be of use, if we can administratively
1204 if (in6if_do_dad(ifp
) && (ifra
->ifra_flags
& IN6_IFF_NODAD
) == 0) {
1205 ia
->ia6_flags
|= IN6_IFF_TENTATIVE
;
1206 nd6_dad_start((struct ifaddr
*)ia
, NULL
);
1213 * XXX: if a change of an existing address failed, keep the entry
1217 in6_unlink_ifa(ia
, ifp
);
1225 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1226 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*) ifa
;
1228 /* stop DAD processing */
1229 nd6_dad_stoptimer(ifa
);
1232 * delete route to the destination of the address being purged.
1233 * The interface must be p2p or loopback in this case.
1235 if ((ia
->ia_flags
& IFA_ROUTE
) != 0 && ia
->ia_dstaddr
.sin6_len
!= 0) {
1238 if ((e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
))
1240 log(LOG_ERR
, "in6_purgeaddr: failed to remove "
1241 "a route to the p2p destination: %s on %s, "
1243 ip6_sprintf(&ia
->ia_addr
.sin6_addr
), if_name(ifp
),
1245 /* proceed anyway... */
1248 ia
->ia_flags
&= ~IFA_ROUTE
;
1251 /* Remove ownaddr's loopback rtentry, if it exists. */
1252 in6_ifremloop(&(ia
->ia_ifa
));
1254 if (ifp
->if_flags
& IFF_MULTICAST
) {
1256 * delete solicited multicast addr for deleting host id
1258 struct in6_multi
*in6m
;
1259 struct in6_addr llsol
;
1260 bzero(&llsol
, sizeof(struct in6_addr
));
1261 llsol
.s6_addr16
[0] = htons(0xff02);
1262 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1263 llsol
.s6_addr32
[1] = 0;
1264 llsol
.s6_addr32
[2] = htonl(1);
1265 llsol
.s6_addr32
[3] =
1266 ia
->ia_addr
.sin6_addr
.s6_addr32
[3];
1267 llsol
.s6_addr8
[12] = 0xff;
1269 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1274 in6_post_msg(ifp
, KEV_INET6_ADDR_DELETED
, ia
);
1275 in6_unlink_ifa(ia
, ifp
);
1279 in6_unlink_ifa(ia
, ifp
)
1280 struct in6_ifaddr
*ia
;
1284 struct in6_ifaddr
*oia
;
1287 TAILQ_REMOVE(&ifp
->if_addrlist
, &ia
->ia_ifa
, ifa_list
);
1290 if (oia
== (ia
= in6_ifaddr
))
1291 in6_ifaddr
= ia
->ia_next
;
1293 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
1296 ia
->ia_next
= oia
->ia_next
;
1299 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1303 if (oia
->ia6_ifpr
) { /* check for safety */
1304 plen
= in6_mask2len(&oia
->ia_prefixmask
.sin6_addr
, NULL
);
1305 iilen
= (sizeof(oia
->ia_prefixmask
.sin6_addr
) << 3) - plen
;
1306 in6_prefix_remove_ifid(iilen
, oia
);
1310 * When an autoconfigured address is being removed, release the
1311 * reference to the base prefix. Also, since the release might
1312 * affect the status of other (detached) addresses, call
1313 * pfxlist_onlink_check().
1315 if ((oia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0) {
1316 if (oia
->ia6_ndpr
== NULL
) {
1317 log(LOG_NOTICE
, "in6_unlink_ifa: autoconf'ed address "
1318 "%p has no prefix\n", oia
);
1320 oia
->ia6_ndpr
->ndpr_refcnt
--;
1321 oia
->ia6_flags
&= ~IN6_IFF_AUTOCONF
;
1322 oia
->ia6_ndpr
= NULL
;
1325 pfxlist_onlink_check();
1329 * release another refcnt for the link from in6_ifaddr.
1330 * Note that we should decrement the refcnt at least once for all *BSD.
1332 ifafree(&oia
->ia_ifa
);
1341 struct ifaddr
*ifa
, *nifa
= NULL
;
1343 if (ifp
== NULL
|| &ifp
->if_addrlist
== NULL
)
1346 for (ifa
= TAILQ_FIRST(&ifp
->if_addrlist
); ifa
!= NULL
; ifa
= nifa
)
1348 nifa
= TAILQ_NEXT(ifa
, ifa_list
);
1349 if (ifa
->ifa_addr
== NULL
)
1351 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1361 * SIOCGLIFADDR: get first address. (?)
1362 * SIOCGLIFADDR with IFLR_PREFIX:
1363 * get first address that matches the specified prefix.
1364 * SIOCALIFADDR: add the specified address.
1365 * SIOCALIFADDR with IFLR_PREFIX:
1366 * add the specified prefix, filling hostid part from
1367 * the first link-local address. prefixlen must be <= 64.
1368 * SIOCDLIFADDR: delete the specified address.
1369 * SIOCDLIFADDR with IFLR_PREFIX:
1370 * delete the first address that matches the specified prefix.
1372 * EINVAL on invalid parameters
1373 * EADDRNOTAVAIL on prefix match failed/specified address not found
1374 * other values may be returned from in6_ioctl()
1376 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1377 * this is to accomodate address naming scheme other than RFC2374,
1379 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1380 * address encoding scheme. (see figure on page 8)
1383 in6_lifaddr_ioctl(so
, cmd
, data
, ifp
, p
)
1390 struct if_laddrreq
*iflr
= (struct if_laddrreq
*)data
;
1392 struct sockaddr
*sa
;
1395 if (!data
|| !ifp
) {
1396 panic("invalid argument to in6_lifaddr_ioctl");
1402 /* address must be specified on GET with IFLR_PREFIX */
1403 if ((iflr
->flags
& IFLR_PREFIX
) == 0)
1408 /* address must be specified on ADD and DELETE */
1409 sa
= (struct sockaddr
*)&iflr
->addr
;
1410 if (sa
->sa_family
!= AF_INET6
)
1412 if (sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1414 /* XXX need improvement */
1415 sa
= (struct sockaddr
*)&iflr
->dstaddr
;
1416 if (sa
->sa_family
&& sa
->sa_family
!= AF_INET6
)
1418 if (sa
->sa_len
&& sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1421 default: /*shouldn't happen*/
1423 panic("invalid cmd to in6_lifaddr_ioctl");
1429 if (sizeof(struct in6_addr
) * 8 < iflr
->prefixlen
)
1435 struct in6_aliasreq ifra
;
1436 struct in6_addr
*hostid
= NULL
;
1439 if ((iflr
->flags
& IFLR_PREFIX
) != 0) {
1440 struct sockaddr_in6
*sin6
;
1443 * hostid is to fill in the hostid part of the
1444 * address. hostid points to the first link-local
1445 * address attached to the interface.
1447 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
, 0);
1449 return EADDRNOTAVAIL
;
1450 hostid
= IFA_IN6(ifa
);
1452 /* prefixlen must be <= 64. */
1453 if (64 < iflr
->prefixlen
)
1455 prefixlen
= iflr
->prefixlen
;
1457 /* hostid part must be zero. */
1458 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1459 if (sin6
->sin6_addr
.s6_addr32
[2] != 0
1460 || sin6
->sin6_addr
.s6_addr32
[3] != 0) {
1464 prefixlen
= iflr
->prefixlen
;
1466 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1467 bzero(&ifra
, sizeof(ifra
));
1468 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
1469 sizeof(ifra
.ifra_name
));
1471 bcopy(&iflr
->addr
, &ifra
.ifra_addr
,
1472 ((struct sockaddr
*)&iflr
->addr
)->sa_len
);
1474 /* fill in hostid part */
1475 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] =
1476 hostid
->s6_addr32
[2];
1477 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] =
1478 hostid
->s6_addr32
[3];
1481 if (((struct sockaddr
*)&iflr
->dstaddr
)->sa_family
) { /*XXX*/
1482 bcopy(&iflr
->dstaddr
, &ifra
.ifra_dstaddr
,
1483 ((struct sockaddr
*)&iflr
->dstaddr
)->sa_len
);
1485 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[2] =
1486 hostid
->s6_addr32
[2];
1487 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[3] =
1488 hostid
->s6_addr32
[3];
1492 ifra
.ifra_prefixmask
.sin6_len
= sizeof(struct sockaddr_in6
);
1493 in6_len2mask(&ifra
.ifra_prefixmask
.sin6_addr
, prefixlen
);
1495 ifra
.ifra_flags
= iflr
->flags
& ~IFLR_PREFIX
;
1496 return in6_control(so
, SIOCAIFADDR_IN6
, (caddr_t
)&ifra
, ifp
, p
);
1501 struct in6_ifaddr
*ia
;
1502 struct in6_addr mask
, candidate
, match
;
1503 struct sockaddr_in6
*sin6
;
1506 bzero(&mask
, sizeof(mask
));
1507 if (iflr
->flags
& IFLR_PREFIX
) {
1508 /* lookup a prefix rather than address. */
1509 in6_len2mask(&mask
, iflr
->prefixlen
);
1511 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1512 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
1513 match
.s6_addr32
[0] &= mask
.s6_addr32
[0];
1514 match
.s6_addr32
[1] &= mask
.s6_addr32
[1];
1515 match
.s6_addr32
[2] &= mask
.s6_addr32
[2];
1516 match
.s6_addr32
[3] &= mask
.s6_addr32
[3];
1518 /* if you set extra bits, that's wrong */
1519 if (bcmp(&match
, &sin6
->sin6_addr
, sizeof(match
)))
1524 if (cmd
== SIOCGLIFADDR
) {
1525 /* on getting an address, take the 1st match */
1528 /* on deleting an address, do exact match */
1529 in6_len2mask(&mask
, 128);
1530 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1531 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
1537 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1539 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1544 bcopy(IFA_IN6(ifa
), &candidate
, sizeof(candidate
));
1545 #ifndef SCOPEDROUTING
1547 * XXX: this is adhoc, but is necessary to allow
1548 * a user to specify fe80::/64 (not /10) for a
1549 * link-local address.
1551 if (IN6_IS_ADDR_LINKLOCAL(&candidate
))
1552 candidate
.s6_addr16
[1] = 0;
1554 candidate
.s6_addr32
[0] &= mask
.s6_addr32
[0];
1555 candidate
.s6_addr32
[1] &= mask
.s6_addr32
[1];
1556 candidate
.s6_addr32
[2] &= mask
.s6_addr32
[2];
1557 candidate
.s6_addr32
[3] &= mask
.s6_addr32
[3];
1558 if (IN6_ARE_ADDR_EQUAL(&candidate
, &match
))
1562 return EADDRNOTAVAIL
;
1565 if (cmd
== SIOCGLIFADDR
) {
1566 #ifndef SCOPEDROUTING
1567 struct sockaddr_in6
*s6
;
1570 /* fill in the if_laddrreq structure */
1571 bcopy(&ia
->ia_addr
, &iflr
->addr
, ia
->ia_addr
.sin6_len
);
1572 #ifndef SCOPEDROUTING /* XXX see above */
1573 s6
= (struct sockaddr_in6
*)&iflr
->addr
;
1574 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
1575 s6
->sin6_addr
.s6_addr16
[1] = 0;
1577 in6_addr2scopeid(ifp
, &s6
->sin6_addr
);
1580 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
1581 bcopy(&ia
->ia_dstaddr
, &iflr
->dstaddr
,
1582 ia
->ia_dstaddr
.sin6_len
);
1583 #ifndef SCOPEDROUTING /* XXX see above */
1584 s6
= (struct sockaddr_in6
*)&iflr
->dstaddr
;
1585 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
1586 s6
->sin6_addr
.s6_addr16
[1] = 0;
1588 in6_addr2scopeid(ifp
,
1593 bzero(&iflr
->dstaddr
, sizeof(iflr
->dstaddr
));
1596 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
1599 iflr
->flags
= ia
->ia6_flags
; /*XXX*/
1603 struct in6_aliasreq ifra
;
1605 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1606 bzero(&ifra
, sizeof(ifra
));
1607 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
1608 sizeof(ifra
.ifra_name
));
1610 bcopy(&ia
->ia_addr
, &ifra
.ifra_addr
,
1611 ia
->ia_addr
.sin6_len
);
1612 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
1613 bcopy(&ia
->ia_dstaddr
, &ifra
.ifra_dstaddr
,
1614 ia
->ia_dstaddr
.sin6_len
);
1616 bzero(&ifra
.ifra_dstaddr
,
1617 sizeof(ifra
.ifra_dstaddr
));
1619 bcopy(&ia
->ia_prefixmask
, &ifra
.ifra_dstaddr
,
1620 ia
->ia_prefixmask
.sin6_len
);
1622 ifra
.ifra_flags
= ia
->ia6_flags
;
1623 return in6_control(so
, SIOCDIFADDR_IN6
, (caddr_t
)&ifra
,
1629 return EOPNOTSUPP
; /*just for safety*/
1633 * Initialize an interface's intetnet6 address
1634 * and routing table entry.
1637 in6_ifinit(ifp
, ia
, sin6
, newhost
)
1639 struct in6_ifaddr
*ia
;
1640 struct sockaddr_in6
*sin6
;
1643 int error
= 0, plen
, ifacount
= 0;
1648 * Give the interface a chance to initialize
1649 * if this is its first address,
1650 * and to validate the address if necessary.
1652 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1654 if (ifa
->ifa_addr
== NULL
)
1655 continue; /* just for safety */
1656 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1661 ia
->ia_addr
= *sin6
;
1664 if (ifacount
<= 1 &&
1666 (error
= dlil_ioctl(0, ifp
, SIOCSIFADDR
, (caddr_t
)ia
))) {
1667 if (error
== EOPNOTSUPP
)
1675 ifp
->if_ioctl
&& (error
= (*ifp
->if_ioctl
)(ifp
, SIOCSIFADDR
, (caddr_t
)ia
))) {
1682 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
1684 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1688 * If the destination address is specified for a point-to-point
1689 * interface, install a route to the destination as an interface
1692 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
); /* XXX */
1693 if (plen
== 128 && ia
->ia_dstaddr
.sin6_family
== AF_INET6
) {
1694 if ((error
= rtinit(&(ia
->ia_ifa
), (int)RTM_ADD
,
1695 RTF_UP
| RTF_HOST
)) != 0)
1697 ia
->ia_flags
|= IFA_ROUTE
;
1701 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1703 ia
->ia_ifa
.ifa_flags
|= RTF_CLONING
;
1706 /* Add ownaddr as loopback rtentry, if necessary(ex. on p2p link). */
1708 /* set the rtrequest function to create llinfo */
1709 ia
->ia_ifa
.ifa_rtrequest
= nd6_rtrequest
;
1710 in6_ifaddloop(&(ia
->ia_ifa
));
1717 * Add an address to the list of IP6 multicast addresses for a
1721 in6_addmulti(maddr6
, ifp
, errorp
)
1722 struct in6_addr
*maddr6
;
1726 struct in6_multi
*in6m
;
1727 struct sockaddr_in6 sin6
;
1728 struct ifmultiaddr
*ifma
;
1734 * Call generic routine to add membership or increment
1735 * refcount. It wants addresses in the form of a sockaddr,
1736 * so we build one here (being careful to zero the unused bytes).
1738 bzero(&sin6
, sizeof sin6
);
1739 sin6
.sin6_family
= AF_INET6
;
1740 sin6
.sin6_len
= sizeof sin6
;
1741 sin6
.sin6_addr
= *maddr6
;
1742 *errorp
= if_addmulti(ifp
, (struct sockaddr
*)&sin6
, &ifma
);
1749 * If ifma->ifma_protospec is null, then if_addmulti() created
1750 * a new record. Otherwise, we are done.
1752 if (ifma
->ifma_protospec
!= 0)
1753 return ifma
->ifma_protospec
;
1755 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1756 at interrupt time? If so, need to fix if_addmulti. XXX */
1757 in6m
= (struct in6_multi
*)_MALLOC(sizeof(*in6m
), M_IPMADDR
, M_NOWAIT
);
1763 bzero(in6m
, sizeof *in6m
);
1764 in6m
->in6m_addr
= *maddr6
;
1765 in6m
->in6m_ifp
= ifp
;
1766 in6m
->in6m_ifma
= ifma
;
1767 ifma
->ifma_protospec
= in6m
;
1768 LIST_INSERT_HEAD(&in6_multihead
, in6m
, in6m_entry
);
1771 * Let MLD6 know that we have joined a new IP6 multicast
1774 mld6_start_listening(in6m
);
1780 * Delete a multicast address record.
1784 struct in6_multi
*in6m
;
1786 struct ifmultiaddr
*ifma
= in6m
->in6m_ifma
;
1789 if (ifma
->ifma_refcount
== 1) {
1791 * No remaining claims to this record; let MLD6 know
1792 * that we are leaving the multicast group.
1794 mld6_stop_listening(in6m
);
1795 ifma
->ifma_protospec
= 0;
1796 LIST_REMOVE(in6m
, in6m_entry
);
1797 FREE(in6m
, M_IPMADDR
);
1799 /* XXX - should be separate API for when we have an ifma? */
1800 if_delmulti(ifma
->ifma_ifp
, ifma
->ifma_addr
);
1805 * Find an IPv6 interface link-local address specific to an interface.
1808 in6ifa_ifpforlinklocal(ifp
, ignoreflags
)
1814 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1816 if (ifa
->ifa_addr
== NULL
)
1817 continue; /* just for safety */
1818 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1820 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
))) {
1821 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
&
1828 return((struct in6_ifaddr
*)ifa
);
1833 * find the internet address corresponding to a given interface and address.
1836 in6ifa_ifpwithaddr(ifp
, addr
)
1838 struct in6_addr
*addr
;
1842 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1844 if (ifa
->ifa_addr
== NULL
)
1845 continue; /* just for safety */
1846 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1848 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(ifa
)))
1852 return((struct in6_ifaddr
*)ifa
);
1856 * Convert IP6 address to printable (loggable) representation.
1858 static char digits
[] = "0123456789abcdef";
1859 static int ip6round
= 0;
1862 const struct in6_addr
*addr
;
1864 static char ip6buf
[8][48];
1867 u_short
*a
= (u_short
*)addr
;
1871 ip6round
= (ip6round
+ 1) & 7;
1872 cp
= ip6buf
[ip6round
];
1874 for (i
= 0; i
< 8; i
++) {
1885 if (dcolon
== 0 && *(a
+ 1) == 0) {
1898 *cp
++ = digits
[*d
>> 4];
1899 *cp
++ = digits
[*d
++ & 0xf];
1900 *cp
++ = digits
[*d
>> 4];
1901 *cp
++ = digits
[*d
& 0xf];
1906 return(ip6buf
[ip6round
]);
1911 struct in6_addr
*in6
;
1913 struct in6_ifaddr
*ia
;
1915 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
))
1918 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
)
1919 if (IN6_ARE_MASKED_ADDR_EQUAL(in6
, &ia
->ia_addr
.sin6_addr
,
1920 &ia
->ia_prefixmask
.sin6_addr
))
1927 in6_is_addr_deprecated(sa6
)
1928 struct sockaddr_in6
*sa6
;
1930 struct in6_ifaddr
*ia
;
1932 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
) {
1933 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
,
1936 ia
->ia_addr
.sin6_scope_id
== sa6
->sin6_scope_id
&&
1938 (ia
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0)
1939 return(1); /* true */
1941 /* XXX: do we still have to go thru the rest of the list? */
1944 return(0); /* false */
1948 * return length of part which dst and src are equal
1952 in6_matchlen(src
, dst
)
1953 struct in6_addr
*src
, *dst
;
1956 u_char
*s
= (u_char
*)src
, *d
= (u_char
*)dst
;
1957 u_char
*lim
= s
+ 16, r
;
1960 if ((r
= (*d
++ ^ *s
++)) != 0) {
1971 /* XXX: to be scope conscious */
1973 in6_are_prefix_equal(p1
, p2
, len
)
1974 struct in6_addr
*p1
, *p2
;
1977 int bytelen
, bitlen
;
1980 if (0 > len
|| len
> 128) {
1981 log(LOG_ERR
, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1989 if (bcmp(&p1
->s6_addr
, &p2
->s6_addr
, bytelen
))
1991 if (p1
->s6_addr
[bytelen
] >> (8 - bitlen
) !=
1992 p2
->s6_addr
[bytelen
] >> (8 - bitlen
))
1999 in6_prefixlen2mask(maskp
, len
)
2000 struct in6_addr
*maskp
;
2003 u_char maskarray
[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2004 int bytelen
, bitlen
, i
;
2007 if (0 > len
|| len
> 128) {
2008 log(LOG_ERR
, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2013 bzero(maskp
, sizeof(*maskp
));
2016 for (i
= 0; i
< bytelen
; i
++)
2017 maskp
->s6_addr
[i
] = 0xff;
2019 maskp
->s6_addr
[bytelen
] = maskarray
[bitlen
- 1];
2023 * return the best address out of the same scope
2026 in6_ifawithscope(oifp
, dst
)
2028 struct in6_addr
*dst
;
2030 int dst_scope
= in6_addrscope(dst
), src_scope
, best_scope
= 0;
2034 struct in6_ifaddr
*ifa_best
= NULL
;
2038 printf("in6_ifawithscope: output interface is not specified\n");
2044 * We search for all addresses on all interfaces from the beginning.
2045 * Comparing an interface with the outgoing interface will be done
2046 * only at the final stage of tiebreaking.
2048 for (ifp
= TAILQ_FIRST(&ifnet
); ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
))
2051 * We can never take an address that breaks the scope zone
2052 * of the destination.
2054 if (in6_addr2scopeid(ifp
, dst
) != in6_addr2scopeid(oifp
, dst
))
2057 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2059 int tlen
= -1, dscopecmp
, bscopecmp
, matchcmp
;
2061 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2064 src_scope
= in6_addrscope(IFA_IN6(ifa
));
2067 * Don't use an address before completing DAD
2068 * nor a duplicated address.
2070 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2074 /* XXX: is there any case to allow anycasts? */
2075 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2079 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2084 * If this is the first address we find,
2087 if (ifa_best
== NULL
)
2091 * ifa_best is never NULL beyond this line except
2092 * within the block labeled "replace".
2096 * If ifa_best has a smaller scope than dst and
2097 * the current address has a larger one than
2098 * (or equal to) dst, always replace ifa_best.
2099 * Also, if the current address has a smaller scope
2100 * than dst, ignore it unless ifa_best also has a
2102 * Consequently, after the two if-clause below,
2103 * the followings must be satisfied:
2104 * (scope(src) < scope(dst) &&
2105 * scope(best) < scope(dst))
2107 * (scope(best) >= scope(dst) &&
2108 * scope(src) >= scope(dst))
2110 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0 &&
2111 IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) >= 0)
2112 goto replace
; /* (A) */
2113 if (IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) < 0 &&
2114 IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) >= 0)
2118 * A deprecated address SHOULD NOT be used in new
2119 * communications if an alternate (non-deprecated)
2120 * address is available and has sufficient scope.
2121 * RFC 2462, Section 5.5.4.
2123 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2124 IN6_IFF_DEPRECATED
) {
2126 * Ignore any deprecated addresses if
2127 * specified by configuration.
2129 if (!ip6_use_deprecated
)
2133 * If we have already found a non-deprecated
2134 * candidate, just ignore deprecated addresses.
2136 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
)
2142 * A non-deprecated address is always preferred
2143 * to a deprecated one regardless of scopes and
2144 * address matching (Note invariants ensured by the
2145 * conditions (A) and (B) above.)
2147 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) &&
2148 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2149 IN6_IFF_DEPRECATED
) == 0)
2153 * When we use temporary addresses described in
2154 * RFC 3041, we prefer temporary addresses to
2155 * public autoconf addresses. Again, note the
2156 * invariants from (A) and (B). Also note that we
2157 * don't have any preference between static addresses
2158 * and autoconf addresses (despite of whether or not
2159 * the latter is temporary or public.)
2161 if (ip6_use_tempaddr
) {
2162 struct in6_ifaddr
*ifat
;
2164 ifat
= (struct in6_ifaddr
*)ifa
;
2165 if ((ifa_best
->ia6_flags
&
2166 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2167 == IN6_IFF_AUTOCONF
&&
2169 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2170 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
)) {
2173 if ((ifa_best
->ia6_flags
&
2174 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2175 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
) &&
2177 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2178 == IN6_IFF_AUTOCONF
) {
2184 * At this point, we have two cases:
2185 * 1. we are looking at a non-deprecated address,
2186 * and ifa_best is also non-deprecated.
2187 * 2. we are looking at a deprecated address,
2188 * and ifa_best is also deprecated.
2189 * Also, we do not have to consider a case where
2190 * the scope of if_best is larger(smaller) than dst and
2191 * the scope of the current address is smaller(larger)
2192 * than dst. Such a case has already been covered.
2193 * Tiebreaking is done according to the following
2195 * - the scope comparison between the address and
2197 * - the scope comparison between the address and
2198 * ifa_best (bscopecmp)
2199 * - if the address match dst longer than ifa_best
2201 * - if the address is on the outgoing I/F (outI/F)
2203 * Roughly speaking, the selection policy is
2204 * - the most important item is scope. The same scope
2205 * is best. Then search for a larger scope.
2206 * Smaller scopes are the last resort.
2207 * - A deprecated address is chosen only when we have
2208 * no address that has an enough scope, but is
2209 * prefered to any addresses of smaller scopes
2210 * (this must be already done above.)
2211 * - addresses on the outgoing I/F are preferred to
2212 * ones on other interfaces if none of above
2213 * tiebreaks. In the table below, the column "bI"
2214 * means if the best_ifa is on the outgoing
2215 * interface, and the column "sI" means if the ifa
2216 * is on the outgoing interface.
2217 * - If there is no other reasons to choose one,
2218 * longest address match against dst is considered.
2220 * The precise decision table is as follows:
2221 * dscopecmp bscopecmp match bI oI | replace?
2222 * N/A equal N/A Y N | No (1)
2223 * N/A equal N/A N Y | Yes (2)
2224 * N/A equal larger N/A | Yes (3)
2225 * N/A equal !larger N/A | No (4)
2226 * larger larger N/A N/A | No (5)
2227 * larger smaller N/A N/A | Yes (6)
2228 * smaller larger N/A N/A | Yes (7)
2229 * smaller smaller N/A N/A | No (8)
2230 * equal smaller N/A N/A | Yes (9)
2231 * equal larger (already done at A above)
2233 dscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
);
2234 bscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, best_scope
);
2236 if (bscopecmp
== 0) {
2237 struct ifnet
*bifp
= ifa_best
->ia_ifp
;
2239 if (bifp
== oifp
&& ifp
!= oifp
) /* (1) */
2241 if (bifp
!= oifp
&& ifp
== oifp
) /* (2) */
2245 * Both bifp and ifp are on the outgoing
2246 * interface, or both two are on a different
2247 * interface from the outgoing I/F.
2248 * now we need address matching against dst
2251 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
2252 matchcmp
= tlen
- blen
;
2253 if (matchcmp
> 0) /* (3) */
2257 if (dscopecmp
> 0) {
2258 if (bscopecmp
> 0) /* (5) */
2260 goto replace
; /* (6) */
2262 if (dscopecmp
< 0) {
2263 if (bscopecmp
> 0) /* (7) */
2268 /* now dscopecmp must be 0 */
2270 goto replace
; /* (9) */
2273 ifa_best
= (struct in6_ifaddr
*)ifa
;
2274 blen
= tlen
>= 0 ? tlen
:
2275 in6_matchlen(IFA_IN6(ifa
), dst
);
2276 best_scope
= in6_addrscope(&ifa_best
->ia_addr
.sin6_addr
);
2280 /* count statistics for future improvements */
2281 if (ifa_best
== NULL
)
2282 ip6stat
.ip6s_sources_none
++;
2284 if (oifp
== ifa_best
->ia_ifp
)
2285 ip6stat
.ip6s_sources_sameif
[best_scope
]++;
2287 ip6stat
.ip6s_sources_otherif
[best_scope
]++;
2289 if (best_scope
== dst_scope
)
2290 ip6stat
.ip6s_sources_samescope
[best_scope
]++;
2292 ip6stat
.ip6s_sources_otherscope
[best_scope
]++;
2294 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0)
2295 ip6stat
.ip6s_sources_deprecated
[best_scope
]++;
2302 * return the best address out of the same scope. if no address was
2303 * found, return the first valid address from designated IF.
2306 in6_ifawithifp(ifp
, dst
)
2308 struct in6_addr
*dst
;
2310 int dst_scope
= in6_addrscope(dst
), blen
= -1, tlen
;
2312 struct in6_ifaddr
*besta
= 0;
2313 struct in6_ifaddr
*dep
[2]; /*last-resort: deprecated*/
2315 dep
[0] = dep
[1] = NULL
;
2318 * We first look for addresses in the same scope.
2319 * If there is one, return it.
2320 * If two or more, return one which matches the dst longest.
2321 * If none, return one of global addresses assigned other ifs.
2323 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2325 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2327 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
)
2328 continue; /* XXX: is there any case to allow anycast? */
2329 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_NOTREADY
)
2330 continue; /* don't use this interface */
2331 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DETACHED
)
2333 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
2334 if (ip6_use_deprecated
)
2335 dep
[0] = (struct in6_ifaddr
*)ifa
;
2339 if (dst_scope
== in6_addrscope(IFA_IN6(ifa
))) {
2341 * call in6_matchlen() as few as possible
2345 blen
= in6_matchlen(&besta
->ia_addr
.sin6_addr
, dst
);
2346 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
2349 besta
= (struct in6_ifaddr
*)ifa
;
2352 besta
= (struct in6_ifaddr
*)ifa
;
2358 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2360 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2362 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
)
2363 continue; /* XXX: is there any case to allow anycast? */
2364 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_NOTREADY
)
2365 continue; /* don't use this interface */
2366 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DETACHED
)
2368 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
2369 if (ip6_use_deprecated
)
2370 dep
[1] = (struct in6_ifaddr
*)ifa
;
2374 return (struct in6_ifaddr
*)ifa
;
2377 /* use the last-resort values, that are, deprecated addresses */
2386 extern int in6_init2done
;
2389 * perform DAD when interface becomes IFF_UP.
2396 struct in6_ifaddr
*ia
;
2397 int dad_delay
; /* delay ticks before DAD output */
2403 * special cases, like 6to4, are handled in in6_ifattach
2405 in6_ifattach(ifp
, NULL
);
2408 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2410 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2412 ia
= (struct in6_ifaddr
*)ifa
;
2413 if (ia
->ia6_flags
& IN6_IFF_TENTATIVE
)
2414 nd6_dad_start(ifa
, &dad_delay
);
2422 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0)
2425 switch (ifp
->if_type
) {
2431 * These interfaces do not have the IFF_LOOPBACK flag,
2432 * but loop packets back. We do not have to do DAD on such
2433 * interfaces. We should even omit it, because loop-backed
2434 * NS would confuse the DAD procedure.
2439 * Our DAD routine requires the interface up and running.
2440 * However, some interfaces can be up before the RUNNING
2441 * status. Additionaly, users may try to assign addresses
2442 * before the interface becomes up (or running).
2443 * We simply skip DAD in such a case as a work around.
2444 * XXX: we should rather mark "tentative" on such addresses,
2445 * and do DAD after the interface becomes ready.
2447 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) !=
2448 (IFF_UP
|IFF_RUNNING
))
2456 * Calculate max IPv6 MTU through all the interfaces and store it
2462 unsigned long maxmtu
= 0;
2465 for (ifp
= TAILQ_FIRST(&ifnet
); ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
))
2467 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
2468 nd_ifinfo
[ifp
->if_index
].linkmtu
> maxmtu
)
2469 maxmtu
= nd_ifinfo
[ifp
->if_index
].linkmtu
;
2471 if (maxmtu
) /* update only when maxmtu is positive */
2472 in6_maxmtu
= maxmtu
;
2476 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2477 * v4 mapped addr or v4 compat addr
2480 in6_sin6_2_sin(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
2482 bzero(sin
, sizeof(*sin
));
2483 sin
->sin_len
= sizeof(struct sockaddr_in
);
2484 sin
->sin_family
= AF_INET
;
2485 sin
->sin_port
= sin6
->sin6_port
;
2486 sin
->sin_addr
.s_addr
= sin6
->sin6_addr
.s6_addr32
[3];
2489 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2491 in6_sin_2_v4mapsin6(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
2493 bzero(sin6
, sizeof(*sin6
));
2494 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2495 sin6
->sin6_family
= AF_INET6
;
2496 sin6
->sin6_port
= sin
->sin_port
;
2497 sin6
->sin6_addr
.s6_addr32
[0] = 0;
2498 sin6
->sin6_addr
.s6_addr32
[1] = 0;
2499 sin6
->sin6_addr
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
2500 sin6
->sin6_addr
.s6_addr32
[3] = sin
->sin_addr
.s_addr
;
2503 /* Convert sockaddr_in6 into sockaddr_in. */
2505 in6_sin6_2_sin_in_sock(struct sockaddr
*nam
)
2507 struct sockaddr_in
*sin_p
;
2508 struct sockaddr_in6 sin6
;
2511 * Save original sockaddr_in6 addr and convert it
2514 sin6
= *(struct sockaddr_in6
*)nam
;
2515 sin_p
= (struct sockaddr_in
*)nam
;
2516 in6_sin6_2_sin(sin_p
, &sin6
);
2519 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2521 in6_sin_2_v4mapsin6_in_sock(struct sockaddr
**nam
)
2523 struct sockaddr_in
*sin_p
;
2524 struct sockaddr_in6
*sin6_p
;
2526 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof *sin6_p
, M_SONAME
,
2528 sin_p
= (struct sockaddr_in
*)*nam
;
2529 in6_sin_2_v4mapsin6(sin_p
, sin6_p
);
2530 FREE(*nam
, M_SONAME
);
2531 *nam
= (struct sockaddr
*)sin6_p
;
2534 /* Posts in6_event_data message kernel events */
2536 in6_post_msg(struct ifnet
*ifp
, u_long event_code
, struct in6_ifaddr
*ifa
)
2538 struct kev_msg ev_msg
;
2539 struct kev_in6_data in6_event_data
;
2541 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2542 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2543 ev_msg
.kev_subclass
= KEV_INET6_SUBCLASS
;
2544 ev_msg
.event_code
= event_code
;
2546 in6_event_data
.ia_addr
= ifa
->ia_addr
;
2547 in6_event_data
.ia_net
= ifa
->ia_net
;
2548 in6_event_data
.ia_dstaddr
= ifa
->ia_dstaddr
;
2549 in6_event_data
.ia_prefixmask
= ifa
->ia_prefixmask
;
2550 in6_event_data
.ia_plen
= ifa
->ia_plen
;
2551 in6_event_data
.ia6_flags
= (u_int32_t
)ifa
->ia6_flags
;
2552 in6_event_data
.ia_lifetime
= ifa
->ia6_lifetime
;
2555 strncpy(&in6_event_data
.link_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2556 in6_event_data
.link_data
.if_family
= ifp
->if_family
;
2557 in6_event_data
.link_data
.if_unit
= (unsigned long) ifp
->if_unit
;
2560 ev_msg
.dv
[0].data_ptr
= &in6_event_data
;
2561 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in6_data
);
2562 ev_msg
.dv
[1].data_length
= 0;
2564 kev_post_msg(&ev_msg
);