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
) {
188 * Report the addition/removal of the address to the routing socket.
189 * XXX: since we called rtinit for a p2p interface with a destination,
190 * we end up reporting twice in such a case. Should we rather
191 * omit the second report?
194 rt_newaddrmsg(cmd
, ifa
, e
, nrt
);
195 if (cmd
== RTM_DELETE
) {
196 if (nrt
->rt_refcnt
<= 0) {
197 /* XXX: we should free the entry ourselves. */
202 /* the cmd must be RTM_ADD here */
209 * Add ownaddr as loopback rtentry. We previously add the route only if
210 * necessary (ex. on a p2p link). However, since we now manage addresses
211 * separately from prefixes, we should always add the route. We can't
212 * rely on the cloning mechanism from the corresponding interface route
216 in6_ifaddloop(struct ifaddr
*ifa
)
220 /* If there is no loopback entry, allocate one. */
221 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
222 if (rt
== NULL
|| (rt
->rt_flags
& RTF_HOST
) == 0 ||
223 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) == 0)
224 in6_ifloop_request(RTM_ADD
, ifa
);
230 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
234 in6_ifremloop(struct ifaddr
*ifa
)
236 struct in6_ifaddr
*ia
;
241 * Some of BSD variants do not remove cloned routes
242 * from an interface direct route, when removing the direct route
243 * (see comments in net/net_osdep.h). Even for variants that do remove
244 * cloned routes, they could fail to remove the cloned routes when
245 * we handle multple addresses that share a common prefix.
246 * So, we should remove the route corresponding to the deleted address
247 * regardless of the result of in6_is_ifloop_auto().
251 * Delete the entry only if exact one ifa exists. More than one ifa
252 * can exist if we assign a same single address to multiple
253 * (probably p2p) interfaces.
254 * XXX: we should avoid such a configuration in IPv6...
256 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
) {
257 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa
), &ia
->ia_addr
.sin6_addr
)) {
266 * Before deleting, check if a corresponding loopbacked host
267 * route surely exists. With this check, we can avoid to
268 * delete an interface direct route whose destination is same
269 * as the address being removed. This can happen when remofing
270 * a subnet-router anycast address on an interface attahced
271 * to a shared medium.
273 rt
= rtalloc1(ifa
->ifa_addr
, 0, 0);
274 if (rt
!= NULL
&& (rt
->rt_flags
& RTF_HOST
) != 0 &&
275 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
277 in6_ifloop_request(RTM_DELETE
, ifa
);
283 in6_ifindex2scopeid(idx
)
288 struct sockaddr_in6
*sin6
;
290 if (idx
< 0 || if_index
< idx
)
292 ifp
= ifindex2ifnet
[idx
];
294 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
296 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
298 sin6
= (struct sockaddr_in6
*)ifa
->ifa_addr
;
299 if (IN6_IS_ADDR_SITELOCAL(&sin6
->sin6_addr
))
300 return sin6
->sin6_scope_id
& 0xffff;
307 in6_mask2len(mask
, lim0
)
308 struct in6_addr
*mask
;
312 u_char
*lim
= lim0
, *p
;
315 lim0
- (u_char
*)mask
> sizeof(*mask
)) /* ignore the scope_id part */
316 lim
= (u_char
*)mask
+ sizeof(*mask
);
317 for (p
= (u_char
*)mask
; p
< lim
; x
++, p
++) {
323 for (y
= 0; y
< 8; y
++) {
324 if ((*p
& (0x80 >> y
)) == 0)
330 * when the limit pointer is given, do a stricter check on the
334 if (y
!= 0 && (*p
& (0x00ff >> y
)) != 0)
336 for (p
= p
+ 1; p
< lim
; p
++)
345 in6_len2mask(mask
, len
)
346 struct in6_addr
*mask
;
351 bzero(mask
, sizeof(*mask
));
352 for (i
= 0; i
< len
/ 8; i
++)
353 mask
->s6_addr8
[i
] = 0xff;
355 mask
->s6_addr8
[i
] = (0xff00 >> (len
% 8)) & 0xff;
358 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
359 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
362 in6_control(so
, cmd
, data
, ifp
, p
)
369 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)data
;
370 struct in6_ifaddr
*ia
= NULL
;
371 struct in6_aliasreq
*ifra
= (struct in6_aliasreq
*)data
;
372 int privileged
, error
= 0;
377 if (p
== NULL
|| !suser(p
->p_ucred
, &p
->p_acflag
))
379 if (p
== NULL
|| !suser(p
))
384 case SIOCGETSGCNT_IN6
:
385 case SIOCGETMIFCNT_IN6
:
386 return (mrt6_ioctl(cmd
, data
));
393 case SIOCSNDFLUSH_IN6
:
394 case SIOCSPFXFLUSH_IN6
:
395 case SIOCSRTRFLUSH_IN6
:
396 case SIOCSDEFIFACE_IN6
:
397 case SIOCSIFINFO_FLAGS
:
401 case OSIOCGIFINFO_IN6
:
402 case SIOCGIFINFO_IN6
:
405 case SIOCGNBRINFO_IN6
:
406 case SIOCGDEFIFACE_IN6
:
407 return(nd6_ioctl(cmd
, data
, ifp
));
411 case SIOCSIFPREFIX_IN6
:
412 case SIOCDIFPREFIX_IN6
:
413 case SIOCAIFPREFIX_IN6
:
414 case SIOCCIFPREFIX_IN6
:
415 case SIOCSGIFPREFIX_IN6
:
416 case SIOCGIFPREFIX_IN6
:
418 "prefix ioctls are now invalidated. "
419 "please use ifconfig.\n");
427 return(scope6_set(ifp
, ifr
->ifr_ifru
.ifru_scope_id
));
430 return(scope6_get(ifp
, ifr
->ifr_ifru
.ifru_scope_id
));
433 return(scope6_get_default(ifr
->ifr_ifru
.ifru_scope_id
));
444 return in6_lifaddr_ioctl(so
, cmd
, data
, ifp
, p
);
451 case SIOCAUTOCONF_START
:
452 ifp
->if_eflags
|= IFEF_ACCEPT_RTADVD
;
455 case SIOCAUTOCONF_STOP
:
457 struct ifaddr
*ifa
, *nifa
= NULL
;
459 ifp
->if_eflags
&= ~IFEF_ACCEPT_RTADVD
;
461 /* nuke prefix list. this may try to remove some of ifaddrs as well */
462 in6_purgeprefix(ifp
);
464 /* removed autoconfigured address from interface */
466 for (ifa
= TAILQ_FIRST(&ifp
->if_addrlist
); ifa
!= NULL
; ifa
= nifa
)
468 nifa
= TAILQ_NEXT(ifa
, ifa_list
);
469 if (ifa
->ifa_addr
== NULL
|| ifa
->ifa_addr
->sa_family
!= AF_INET6
)
471 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_AUTOCONF
)
480 /* NOTE: All the interface specific DLIL attachements should be done here
481 * They are currently done in in6_ifattach() for the interfaces that need it
484 if (ifp
->if_type
== IFT_PPP
&& ifra
->ifra_addr
.sin6_family
== AF_INET6
&&
485 ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
)
486 in6_if_up(ifp
, ifra
); /* PPP may provide LinkLocal addresses */
494 struct ifaddr
*ifa
, *nifa
= NULL
;
496 /* removed link local addresses from interface */
498 for (ifa
= TAILQ_FIRST(&ifp
->if_addrlist
); ifa
!= NULL
; ifa
= nifa
)
500 nifa
= TAILQ_NEXT(ifa
, ifa_list
);
501 if (ifa
->ifa_addr
== NULL
|| ifa
->ifa_addr
->sa_family
!= AF_INET6
)
503 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
)))
510 case SIOCPROTOATTACH_IN6
:
512 switch (ifp
->if_type
) {
513 #if IFT_BRIDGE /*OpenBSD 2.8*/
514 /* some of the interfaces are inherently not IPv6 capable */
520 if (error
= dlil_plumb_protocol(PF_INET6
, ifp
, &dl_tag
))
521 printf("SIOCPROTOATTACH_IN6: %s error=%d\n",
522 if_name(ifp
), error
);
529 case SIOCPROTODETACH_IN6
:
531 in6_purgeif(ifp
); /* Cleanup interface routes and addresses */
533 if (error
= dlil_unplumb_protocol(PF_INET6
, ifp
))
534 printf("SIOCPROTODETACH_IN6: %s error=%d\n",
535 if_name(ifp
), error
);
541 * Find address for this interface, if it exists.
543 if (ifra
->ifra_addr
.sin6_family
== AF_INET6
) { /* XXX */
544 struct sockaddr_in6
*sa6
=
545 (struct sockaddr_in6
*)&ifra
->ifra_addr
;
547 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
548 if (sa6
->sin6_addr
.s6_addr16
[1] == 0) {
549 /* link ID is not embedded by the user */
550 sa6
->sin6_addr
.s6_addr16
[1] =
551 htons(ifp
->if_index
);
552 } else if (sa6
->sin6_addr
.s6_addr16
[1] !=
553 htons(ifp
->if_index
)) {
554 return(EINVAL
); /* link ID contradicts */
556 if (sa6
->sin6_scope_id
) {
557 if (sa6
->sin6_scope_id
!=
558 (u_int32_t
)ifp
->if_index
)
560 sa6
->sin6_scope_id
= 0; /* XXX: good way? */
563 ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
);
567 case SIOCSIFADDR_IN6
:
568 case SIOCSIFDSTADDR_IN6
:
569 case SIOCSIFNETMASK_IN6
:
571 * Since IPv6 allows a node to assign multiple addresses
572 * on a single interface, SIOCSIFxxx ioctls are not suitable
573 * and should be unused.
575 /* we decided to obsolete this command (20000704) */
578 case SIOCDIFADDR_IN6
:
580 * for IPv4, we look for existing in_ifaddr here to allow
581 * "ifconfig if0 delete" to remove first IPv4 address on the
582 * interface. For IPv6, as the spec allow multiple interface
583 * address from the day one, we consider "remove the first one"
584 * semantics to be not preferable.
587 return(EADDRNOTAVAIL
);
589 case SIOCAIFADDR_IN6
:
591 * We always require users to specify a valid IPv6 address for
592 * the corresponding operation.
594 if (ifra
->ifra_addr
.sin6_family
!= AF_INET6
||
595 ifra
->ifra_addr
.sin6_len
!= sizeof(struct sockaddr_in6
))
596 return(EAFNOSUPPORT
);
602 case SIOCGIFADDR_IN6
:
603 /* This interface is basically deprecated. use SIOCGIFCONF. */
605 case SIOCGIFAFLAG_IN6
:
606 case SIOCGIFNETMASK_IN6
:
607 case SIOCGIFDSTADDR_IN6
:
608 case SIOCGIFALIFETIME_IN6
:
609 /* must think again about its semantics */
611 return(EADDRNOTAVAIL
);
613 case SIOCSIFALIFETIME_IN6
:
615 struct in6_addrlifetime
*lt
;
620 return(EADDRNOTAVAIL
);
621 /* sanity for overflow - beware unsigned */
622 lt
= &ifr
->ifr_ifru
.ifru_lifetime
;
623 if (lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
624 && lt
->ia6t_vltime
+ time_second
< time_second
) {
627 if (lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
628 && lt
->ia6t_pltime
+ time_second
< time_second
) {
637 case SIOCGIFADDR_IN6
:
638 ifr
->ifr_addr
= ia
->ia_addr
;
641 case SIOCGIFDSTADDR_IN6
:
642 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
645 * XXX: should we check if ifa_dstaddr is NULL and return
648 ifr
->ifr_dstaddr
= ia
->ia_dstaddr
;
651 case SIOCGIFNETMASK_IN6
:
652 ifr
->ifr_addr
= ia
->ia_prefixmask
;
655 case SIOCGIFAFLAG_IN6
:
656 ifr
->ifr_ifru
.ifru_flags6
= ia
->ia6_flags
;
659 case SIOCGIFSTAT_IN6
:
662 if (in6_ifstat
== NULL
|| ifp
->if_index
>= in6_ifstatmax
663 || in6_ifstat
[ifp
->if_index
] == NULL
) {
664 /* return EAFNOSUPPORT? */
665 bzero(&ifr
->ifr_ifru
.ifru_stat
,
666 sizeof(ifr
->ifr_ifru
.ifru_stat
));
668 ifr
->ifr_ifru
.ifru_stat
= *in6_ifstat
[ifp
->if_index
];
671 case SIOCGIFSTAT_ICMP6
:
674 if (icmp6_ifstat
== NULL
|| ifp
->if_index
>= icmp6_ifstatmax
||
675 icmp6_ifstat
[ifp
->if_index
] == NULL
) {
676 /* return EAFNOSUPPORT? */
677 bzero(&ifr
->ifr_ifru
.ifru_stat
,
678 sizeof(ifr
->ifr_ifru
.ifru_icmp6stat
));
680 ifr
->ifr_ifru
.ifru_icmp6stat
=
681 *icmp6_ifstat
[ifp
->if_index
];
684 case SIOCGIFALIFETIME_IN6
:
685 ifr
->ifr_ifru
.ifru_lifetime
= ia
->ia6_lifetime
;
688 case SIOCSIFALIFETIME_IN6
:
689 ia
->ia6_lifetime
= ifr
->ifr_ifru
.ifru_lifetime
;
691 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
692 ia
->ia6_lifetime
.ia6t_expire
=
693 time_second
+ ia
->ia6_lifetime
.ia6t_vltime
;
695 ia
->ia6_lifetime
.ia6t_expire
= 0;
696 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
697 ia
->ia6_lifetime
.ia6t_preferred
=
698 time_second
+ ia
->ia6_lifetime
.ia6t_pltime
;
700 ia
->ia6_lifetime
.ia6t_preferred
= 0;
703 case SIOCAIFADDR_IN6
:
706 struct nd_prefix pr0
, *pr
;
708 if (dlil_find_dltag(ifp
->if_family
, ifp
->if_unit
, PF_INET6
, &dl_tag
) == EPROTONOSUPPORT
) {
709 /* Address is added without previous IPv6 configurator support (gif, stf etc...) */
710 if (error
= dlil_plumb_protocol(PF_INET6
, ifp
, &dl_tag
)) {
711 printf("SIOCAIFADDR_IN6: %s can't plumb protocol error=%d\n",
712 if_name(ifp
), error
);
715 in6_if_up(ifp
, NULL
);
720 * first, make or update the interface address structure,
721 * and link it to the list.
723 if ((error
= in6_update_ifa(ifp
, ifra
, ia
)) != 0)
727 * then, make the prefix on-link on the interface.
728 * XXX: we'd rather create the prefix before the address, but
729 * we need at least one address to install the corresponding
730 * interface route, so we configure the address first.
734 * convert mask to prefix length (prefixmask has already
735 * been validated in in6_update_ifa().
737 bzero(&pr0
, sizeof(pr0
));
739 pr0
.ndpr_plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
741 if (pr0
.ndpr_plen
== 128)
742 break; /* we don't need to install a host route. */
743 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
744 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
745 /* apply the mask for safety. */
746 for (i
= 0; i
< 4; i
++) {
747 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
748 ifra
->ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
751 * XXX: since we don't have an API to set prefix (not address)
752 * lifetimes, we just use the same lifetimes as addresses.
753 * The (temporarily) installed lifetimes can be overridden by
754 * later advertised RAs (when accept_rtadv is non 0), which is
755 * an intended behavior.
757 pr0
.ndpr_raf_onlink
= 1; /* should be configurable? */
759 ((ifra
->ifra_flags
& IN6_IFF_AUTOCONF
) != 0);
760 pr0
.ndpr_vltime
= ifra
->ifra_lifetime
.ia6t_vltime
;
761 pr0
.ndpr_pltime
= ifra
->ifra_lifetime
.ia6t_pltime
;
763 /* add the prefix if there's one. */
764 if ((pr
= nd6_prefix_lookup(&pr0
)) == NULL
) {
766 * nd6_prelist_add will install the corresponding
769 if ((error
= nd6_prelist_add(&pr0
, NULL
, &pr
)) != 0)
772 log(LOG_ERR
, "nd6_prelist_add succedded but "
774 return(EINVAL
); /* XXX panic here? */
777 if ((ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
))
779 /* XXX: this should not happen! */
780 log(LOG_ERR
, "in6_control: addition succeeded, but"
783 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
784 ia
->ia6_ndpr
== NULL
) { /* new autoconfed addr */
789 * If this is the first autoconf address from
790 * the prefix, create a temporary address
791 * as well (when specified).
793 if (ip6_use_tempaddr
&&
794 pr
->ndpr_refcnt
== 1) {
796 if ((e
= in6_tmpifadd(ia
, 1)) != 0) {
797 log(LOG_NOTICE
, "in6_control: "
798 "failed to create a "
799 "temporary address, "
807 * this might affect the status of autoconfigured
808 * addresses, that is, this address might make
809 * other addresses detached.
811 pfxlist_onlink_check();
812 in6_post_msg(ifp
, KEV_INET6_NEW_USER_ADDR
, ia
);
818 case SIOCDIFADDR_IN6
:
821 struct nd_prefix pr0
, *pr
;
824 * If the address being deleted is the only one that owns
825 * the corresponding prefix, expire the prefix as well.
826 * XXX: theoretically, we don't have to warry about such
827 * relationship, since we separate the address management
828 * and the prefix management. We do this, however, to provide
829 * as much backward compatibility as possible in terms of
830 * the ioctl operation.
832 bzero(&pr0
, sizeof(pr0
));
834 pr0
.ndpr_plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
836 if (pr0
.ndpr_plen
== 128)
838 pr0
.ndpr_prefix
= ia
->ia_addr
;
839 pr0
.ndpr_mask
= ia
->ia_prefixmask
.sin6_addr
;
840 for (i
= 0; i
< 4; i
++) {
841 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
842 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
];
845 * The logic of the following condition is a bit complicated.
846 * We expire the prefix when
847 * 1. the address obeys autoconfiguration and it is the
848 * only owner of the associated prefix, or
849 * 2. the address does not obey autoconf and there is no
850 * other owner of the prefix.
852 if ((pr
= nd6_prefix_lookup(&pr0
)) != NULL
&&
853 (((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0 &&
854 pr
->ndpr_refcnt
== 1) ||
855 ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) == 0 &&
856 pr
->ndpr_refcnt
== 0))) {
857 pr
->ndpr_expire
= 1; /* XXX: just for expiration */
861 in6_purgeaddr(&ia
->ia_ifa
);
867 error
= dlil_ioctl(PF_INET6
, ifp
, cmd
, (caddr_t
)data
);
871 if (ifp
== NULL
|| ifp
->if_ioctl
== 0)
873 return((*ifp
->if_ioctl
)(ifp
, cmd
, data
));
881 * Update parameters of an IPv6 interface address.
882 * If necessary, a new entry is created and linked into address chains.
883 * This function is separated from in6_control().
884 * XXX: should this be performed under splnet()?
887 in6_update_ifa(ifp
, ifra
, ia
)
889 struct in6_aliasreq
*ifra
;
890 struct in6_ifaddr
*ia
;
892 int error
= 0, hostIsNew
= 0, plen
= -1;
893 struct in6_ifaddr
*oia
;
894 struct sockaddr_in6 dst6
;
895 struct in6_addrlifetime
*lt
;
897 /* Validate parameters */
898 if (ifp
== NULL
|| ifra
== NULL
) /* this maybe redundant */
902 * The destination address for a p2p link must have a family
903 * of AF_UNSPEC or AF_INET6.
905 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0 &&
906 ifra
->ifra_dstaddr
.sin6_family
!= AF_INET6
&&
907 ifra
->ifra_dstaddr
.sin6_family
!= AF_UNSPEC
)
908 return(EAFNOSUPPORT
);
910 * validate ifra_prefixmask. don't check sin6_family, netmask
911 * does not carry fields other than sin6_len.
913 if (ifra
->ifra_prefixmask
.sin6_len
> sizeof(struct sockaddr_in6
))
916 * Because the IPv6 address architecture is classless, we require
917 * users to specify a (non 0) prefix length (mask) for a new address.
918 * We also require the prefix (when specified) mask is valid, and thus
919 * reject a non-consecutive mask.
921 if (ia
== NULL
&& ifra
->ifra_prefixmask
.sin6_len
== 0)
923 if (ifra
->ifra_prefixmask
.sin6_len
!= 0) {
924 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
925 (u_char
*)&ifra
->ifra_prefixmask
+
926 ifra
->ifra_prefixmask
.sin6_len
);
932 * In this case, ia must not be NULL. We just use its prefix
935 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
);
938 * If the destination address on a p2p interface is specified,
939 * and the address is a scoped one, validate/set the scope
942 dst6
= ifra
->ifra_dstaddr
;
943 if ((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) &&
944 (dst6
.sin6_family
== AF_INET6
)) {
947 #ifndef SCOPEDROUTING
948 if ((error
= in6_recoverscope(&dst6
,
949 &ifra
->ifra_dstaddr
.sin6_addr
,
953 scopeid
= in6_addr2scopeid(ifp
, &dst6
.sin6_addr
);
954 if (dst6
.sin6_scope_id
== 0) /* user omit to specify the ID. */
955 dst6
.sin6_scope_id
= scopeid
;
956 else if (dst6
.sin6_scope_id
!= scopeid
)
957 return(EINVAL
); /* scope ID mismatch. */
958 #ifndef SCOPEDROUTING
959 if ((error
= in6_embedscope(&dst6
.sin6_addr
, &dst6
, NULL
, NULL
))
962 dst6
.sin6_scope_id
= 0; /* XXX */
966 * The destination address can be specified only for a p2p or a
967 * loopback interface. If specified, the corresponding prefix length
970 if (ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
) {
971 if ((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) == 0) {
972 /* XXX: noisy message */
973 log(LOG_INFO
, "in6_update_ifa: a destination can be "
974 "specified for a p2p or a loopback IF only\n");
979 * The following message seems noisy, but we dare to
980 * add it for diagnosis.
982 log(LOG_INFO
, "in6_update_ifa: prefixlen must be 128 "
983 "when dstaddr is specified\n");
987 /* lifetime consistency check */
988 lt
= &ifra
->ifra_lifetime
;
989 if (lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
990 && lt
->ia6t_vltime
+ time_second
< time_second
) {
993 if (lt
->ia6t_vltime
== 0) {
995 * the following log might be noisy, but this is a typical
996 * configuration mistake or a tool's bug.
999 "in6_update_ifa: valid lifetime is 0 for %s\n",
1000 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
1002 if (lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
1003 && lt
->ia6t_pltime
+ time_second
< time_second
) {
1008 * If this is a new address, allocate a new ifaddr and link it
1014 * When in6_update_ifa() is called in a process of a received
1015 * RA, it is called under splnet(). So, we should call malloc
1018 ia
= (struct in6_ifaddr
*)
1019 _MALLOC(sizeof(*ia
), M_IFADDR
, M_NOWAIT
);
1022 bzero((caddr_t
)ia
, sizeof(*ia
));
1023 /* Initialize the address and masks */
1024 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
1025 ia
->ia_addr
.sin6_family
= AF_INET6
;
1026 ia
->ia_addr
.sin6_len
= sizeof(ia
->ia_addr
);
1027 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) != 0) {
1029 * XXX: some functions expect that ifa_dstaddr is not
1030 * NULL for p2p interfaces.
1032 ia
->ia_ifa
.ifa_dstaddr
1033 = (struct sockaddr
*)&ia
->ia_dstaddr
;
1035 ia
->ia_ifa
.ifa_dstaddr
= NULL
;
1037 ia
->ia_ifa
.ifa_netmask
1038 = (struct sockaddr
*)&ia
->ia_prefixmask
;
1041 if ((oia
= in6_ifaddr
) != NULL
) {
1042 for ( ; oia
->ia_next
; oia
= oia
->ia_next
)
1048 TAILQ_INSERT_TAIL(&ifp
->if_addrlist
, &ia
->ia_ifa
,
1052 /* set prefix mask */
1053 if (ifra
->ifra_prefixmask
.sin6_len
) {
1055 * We prohibit changing the prefix length of an existing
1057 * + such an operation should be rare in IPv6, and
1058 * + the operation would confuse prefix management.
1060 if (ia
->ia_prefixmask
.sin6_len
&&
1061 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
) != plen
) {
1062 log(LOG_INFO
, "in6_update_ifa: the prefix length of an"
1063 " existing (%s) address should not be changed\n",
1064 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1068 ia
->ia_prefixmask
= ifra
->ifra_prefixmask
;
1072 * If a new destination address is specified, scrub the old one and
1073 * install the new destination. Note that the interface must be
1074 * p2p or loopback (see the check above.)
1076 if (dst6
.sin6_family
== AF_INET6
&&
1077 !IN6_ARE_ADDR_EQUAL(&dst6
.sin6_addr
,
1078 &ia
->ia_dstaddr
.sin6_addr
)) {
1081 if ((ia
->ia_flags
& IFA_ROUTE
) != 0 &&
1082 (e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
))
1084 log(LOG_ERR
, "in6_update_ifa: failed to remove "
1085 "a route to the old destination: %s\n",
1086 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
1087 /* proceed anyway... */
1090 ia
->ia_flags
&= ~IFA_ROUTE
;
1091 ia
->ia_dstaddr
= dst6
;
1094 /* reset the interface and routing table appropriately. */
1095 if ((error
= in6_ifinit(ifp
, ia
, &ifra
->ifra_addr
, hostIsNew
)) != 0)
1099 * Beyond this point, we should call in6_purgeaddr upon an error,
1100 * not just go to unlink.
1103 #if 0 /* disable this mechanism for now */
1104 /* update prefix list */
1106 (ifra
->ifra_flags
& IN6_IFF_NOPFX
) == 0) { /* XXX */
1109 iilen
= (sizeof(ia
->ia_prefixmask
.sin6_addr
) << 3) - plen
;
1110 if ((error
= in6_prefix_add_ifid(iilen
, ia
)) != 0) {
1111 in6_purgeaddr((struct ifaddr
*)ia
);
1117 if ((ifp
->if_flags
& IFF_MULTICAST
) != 0) {
1118 struct sockaddr_in6 mltaddr
, mltmask
;
1119 struct in6_multi
*in6m
;
1123 * join solicited multicast addr for new host id
1125 struct in6_addr llsol
;
1126 bzero(&llsol
, sizeof(struct in6_addr
));
1127 llsol
.s6_addr16
[0] = htons(0xff02);
1128 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1129 llsol
.s6_addr32
[1] = 0;
1130 llsol
.s6_addr32
[2] = htonl(1);
1131 llsol
.s6_addr32
[3] =
1132 ifra
->ifra_addr
.sin6_addr
.s6_addr32
[3];
1133 llsol
.s6_addr8
[12] = 0xff;
1134 (void)in6_addmulti(&llsol
, ifp
, &error
);
1137 "in6_update_ifa: addmulti failed for "
1138 "%s on %s (errno=%d)\n",
1139 ip6_sprintf(&llsol
), if_name(ifp
),
1141 in6_purgeaddr((struct ifaddr
*)ia
);
1146 bzero(&mltmask
, sizeof(mltmask
));
1147 mltmask
.sin6_len
= sizeof(struct sockaddr_in6
);
1148 mltmask
.sin6_family
= AF_INET6
;
1149 mltmask
.sin6_addr
= in6mask32
;
1152 * join link-local all-nodes address
1154 bzero(&mltaddr
, sizeof(mltaddr
));
1155 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
1156 mltaddr
.sin6_family
= AF_INET6
;
1157 mltaddr
.sin6_addr
= in6addr_linklocal_allnodes
;
1158 mltaddr
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
1160 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1163 (struct sockaddr
*)&mltaddr
,
1164 (struct sockaddr
*)&ia
->ia_addr
,
1165 (struct sockaddr
*)&mltmask
,
1166 RTF_UP
|RTF_CLONING
, /* xxx */
1167 (struct rtentry
**)0);
1168 (void)in6_addmulti(&mltaddr
.sin6_addr
, ifp
, &error
);
1171 "in6_update_ifa: addmulti failed for "
1172 "%s on %s (errno=%d)\n",
1173 ip6_sprintf(&mltaddr
.sin6_addr
),
1174 if_name(ifp
), error
);
1179 * join node information group address
1181 #define hostnamelen strlen(hostname)
1182 if (in6_nigroup(ifp
, hostname
, hostnamelen
, &mltaddr
.sin6_addr
)
1184 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1185 if (in6m
== NULL
&& ia
!= NULL
) {
1186 (void)in6_addmulti(&mltaddr
.sin6_addr
,
1189 log(LOG_WARNING
, "in6_update_ifa: "
1190 "addmulti failed for "
1191 "%s on %s (errno=%d)\n",
1192 ip6_sprintf(&mltaddr
.sin6_addr
),
1193 if_name(ifp
), error
);
1200 * join node-local all-nodes address, on loopback.
1201 * XXX: since "node-local" is obsoleted by interface-local,
1202 * we have to join the group on every interface with
1203 * some interface-boundary restriction.
1205 if (ifp
->if_flags
& IFF_LOOPBACK
) {
1206 struct in6_ifaddr
*ia_loop
;
1208 struct in6_addr loop6
= in6addr_loopback
;
1209 ia_loop
= in6ifa_ifpwithaddr(ifp
, &loop6
);
1211 mltaddr
.sin6_addr
= in6addr_nodelocal_allnodes
;
1213 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1214 if (in6m
== NULL
&& ia_loop
!= NULL
) {
1216 (struct sockaddr
*)&mltaddr
,
1217 (struct sockaddr
*)&ia_loop
->ia_addr
,
1218 (struct sockaddr
*)&mltmask
,
1220 (struct rtentry
**)0);
1221 (void)in6_addmulti(&mltaddr
.sin6_addr
, ifp
,
1224 log(LOG_WARNING
, "in6_update_ifa: "
1225 "addmulti failed for %s on %s "
1227 ip6_sprintf(&mltaddr
.sin6_addr
),
1228 if_name(ifp
), error
);
1234 ia
->ia6_flags
= ifra
->ifra_flags
;
1235 ia
->ia6_flags
&= ~IN6_IFF_DUPLICATED
; /*safety*/
1236 ia
->ia6_flags
&= ~IN6_IFF_NODAD
; /* Mobile IPv6 */
1238 ia
->ia6_lifetime
= ifra
->ifra_lifetime
;
1240 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
1241 ia
->ia6_lifetime
.ia6t_expire
=
1242 time_second
+ ia
->ia6_lifetime
.ia6t_vltime
;
1244 ia
->ia6_lifetime
.ia6t_expire
= 0;
1245 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
1246 ia
->ia6_lifetime
.ia6t_preferred
=
1247 time_second
+ ia
->ia6_lifetime
.ia6t_pltime
;
1249 ia
->ia6_lifetime
.ia6t_preferred
= 0;
1252 * make sure to initialize ND6 information. this is to workaround
1253 * issues with interfaces with IPv6 addresses, which have never brought
1254 * up. We are assuming that it is safe to nd6_ifattach multiple times.
1259 * Perform DAD, if needed.
1260 * XXX It may be of use, if we can administratively
1263 if (in6if_do_dad(ifp
) && (ifra
->ifra_flags
& IN6_IFF_NODAD
) == 0) {
1264 ia
->ia6_flags
|= IN6_IFF_TENTATIVE
;
1265 nd6_dad_start((struct ifaddr
*)ia
, NULL
);
1272 * XXX: if a change of an existing address failed, keep the entry
1276 in6_unlink_ifa(ia
, ifp
);
1284 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1285 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*) ifa
;
1287 /* stop DAD processing */
1291 * delete route to the destination of the address being purged.
1292 * The interface must be p2p or loopback in this case.
1294 if ((ia
->ia_flags
& IFA_ROUTE
) != 0 && ia
->ia_dstaddr
.sin6_len
!= 0) {
1297 if ((e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
))
1299 log(LOG_ERR
, "in6_purgeaddr: failed to remove "
1300 "a route to the p2p destination: %s on %s, "
1302 ip6_sprintf(&ia
->ia_addr
.sin6_addr
), if_name(ifp
),
1304 /* proceed anyway... */
1307 ia
->ia_flags
&= ~IFA_ROUTE
;
1310 /* Remove ownaddr's loopback rtentry, if it exists. */
1311 in6_ifremloop(&(ia
->ia_ifa
));
1313 if (ifp
->if_flags
& IFF_MULTICAST
) {
1315 * delete solicited multicast addr for deleting host id
1317 struct in6_multi
*in6m
;
1318 struct in6_addr llsol
;
1319 bzero(&llsol
, sizeof(struct in6_addr
));
1320 llsol
.s6_addr16
[0] = htons(0xff02);
1321 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1322 llsol
.s6_addr32
[1] = 0;
1323 llsol
.s6_addr32
[2] = htonl(1);
1324 llsol
.s6_addr32
[3] =
1325 ia
->ia_addr
.sin6_addr
.s6_addr32
[3];
1326 llsol
.s6_addr8
[12] = 0xff;
1328 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1333 in6_post_msg(ifp
, KEV_INET6_ADDR_DELETED
, ia
);
1334 in6_unlink_ifa(ia
, ifp
);
1338 in6_unlink_ifa(ia
, ifp
)
1339 struct in6_ifaddr
*ia
;
1343 struct in6_ifaddr
*oia
;
1346 TAILQ_REMOVE(&ifp
->if_addrlist
, &ia
->ia_ifa
, ifa_list
);
1349 if (oia
== (ia
= in6_ifaddr
))
1350 in6_ifaddr
= ia
->ia_next
;
1352 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
1355 ia
->ia_next
= oia
->ia_next
;
1358 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1362 if (oia
->ia6_ifpr
) { /* check for safety */
1363 plen
= in6_mask2len(&oia
->ia_prefixmask
.sin6_addr
, NULL
);
1364 iilen
= (sizeof(oia
->ia_prefixmask
.sin6_addr
) << 3) - plen
;
1365 in6_prefix_remove_ifid(iilen
, oia
);
1369 * When an autoconfigured address is being removed, release the
1370 * reference to the base prefix. Also, since the release might
1371 * affect the status of other (detached) addresses, call
1372 * pfxlist_onlink_check().
1374 if ((oia
->ia6_flags
& IN6_IFF_AUTOCONF
) != 0) {
1375 if (oia
->ia6_ndpr
== NULL
) {
1376 log(LOG_NOTICE
, "in6_unlink_ifa: autoconf'ed address "
1377 "%p has no prefix\n", oia
);
1379 oia
->ia6_ndpr
->ndpr_refcnt
--;
1380 oia
->ia6_flags
&= ~IN6_IFF_AUTOCONF
;
1381 oia
->ia6_ndpr
= NULL
;
1384 pfxlist_onlink_check();
1388 * release another refcnt for the link from in6_ifaddr.
1389 * Note that we should decrement the refcnt at least once for all *BSD.
1391 ifafree(&oia
->ia_ifa
);
1400 struct ifaddr
*ifa
, *nifa
= NULL
;
1402 if (ifp
== NULL
|| &ifp
->if_addrlist
== NULL
)
1405 for (ifa
= TAILQ_FIRST(&ifp
->if_addrlist
); ifa
!= NULL
; ifa
= nifa
)
1407 nifa
= TAILQ_NEXT(ifa
, ifa_list
);
1408 if (ifa
->ifa_addr
== NULL
)
1410 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1420 * SIOCGLIFADDR: get first address. (?)
1421 * SIOCGLIFADDR with IFLR_PREFIX:
1422 * get first address that matches the specified prefix.
1423 * SIOCALIFADDR: add the specified address.
1424 * SIOCALIFADDR with IFLR_PREFIX:
1425 * add the specified prefix, filling hostid part from
1426 * the first link-local address. prefixlen must be <= 64.
1427 * SIOCDLIFADDR: delete the specified address.
1428 * SIOCDLIFADDR with IFLR_PREFIX:
1429 * delete the first address that matches the specified prefix.
1431 * EINVAL on invalid parameters
1432 * EADDRNOTAVAIL on prefix match failed/specified address not found
1433 * other values may be returned from in6_ioctl()
1435 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1436 * this is to accomodate address naming scheme other than RFC2374,
1438 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1439 * address encoding scheme. (see figure on page 8)
1442 in6_lifaddr_ioctl(so
, cmd
, data
, ifp
, p
)
1449 struct if_laddrreq
*iflr
= (struct if_laddrreq
*)data
;
1451 struct sockaddr
*sa
;
1454 if (!data
|| !ifp
) {
1455 panic("invalid argument to in6_lifaddr_ioctl");
1461 /* address must be specified on GET with IFLR_PREFIX */
1462 if ((iflr
->flags
& IFLR_PREFIX
) == 0)
1467 /* address must be specified on ADD and DELETE */
1468 sa
= (struct sockaddr
*)&iflr
->addr
;
1469 if (sa
->sa_family
!= AF_INET6
)
1471 if (sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1473 /* XXX need improvement */
1474 sa
= (struct sockaddr
*)&iflr
->dstaddr
;
1475 if (sa
->sa_family
&& sa
->sa_family
!= AF_INET6
)
1477 if (sa
->sa_len
&& sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1480 default: /* shouldn't happen */
1482 panic("invalid cmd to in6_lifaddr_ioctl");
1488 if (sizeof(struct in6_addr
) * 8 < iflr
->prefixlen
)
1494 struct in6_aliasreq ifra
;
1495 struct in6_addr
*hostid
= NULL
;
1498 if ((iflr
->flags
& IFLR_PREFIX
) != 0) {
1499 struct sockaddr_in6
*sin6
;
1502 * hostid is to fill in the hostid part of the
1503 * address. hostid points to the first link-local
1504 * address attached to the interface.
1506 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
, 0);
1508 return EADDRNOTAVAIL
;
1509 hostid
= IFA_IN6(ifa
);
1511 /* prefixlen must be <= 64. */
1512 if (64 < iflr
->prefixlen
)
1514 prefixlen
= iflr
->prefixlen
;
1516 /* hostid part must be zero. */
1517 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1518 if (sin6
->sin6_addr
.s6_addr32
[2] != 0
1519 || sin6
->sin6_addr
.s6_addr32
[3] != 0) {
1523 prefixlen
= iflr
->prefixlen
;
1525 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1526 bzero(&ifra
, sizeof(ifra
));
1527 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
1528 sizeof(ifra
.ifra_name
));
1530 bcopy(&iflr
->addr
, &ifra
.ifra_addr
,
1531 ((struct sockaddr
*)&iflr
->addr
)->sa_len
);
1533 /* fill in hostid part */
1534 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] =
1535 hostid
->s6_addr32
[2];
1536 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] =
1537 hostid
->s6_addr32
[3];
1540 if (((struct sockaddr
*)&iflr
->dstaddr
)->sa_family
) { /*XXX*/
1541 bcopy(&iflr
->dstaddr
, &ifra
.ifra_dstaddr
,
1542 ((struct sockaddr
*)&iflr
->dstaddr
)->sa_len
);
1544 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[2] =
1545 hostid
->s6_addr32
[2];
1546 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[3] =
1547 hostid
->s6_addr32
[3];
1551 ifra
.ifra_prefixmask
.sin6_len
= sizeof(struct sockaddr_in6
);
1552 in6_len2mask(&ifra
.ifra_prefixmask
.sin6_addr
, prefixlen
);
1554 ifra
.ifra_flags
= iflr
->flags
& ~IFLR_PREFIX
;
1555 return in6_control(so
, SIOCAIFADDR_IN6
, (caddr_t
)&ifra
, ifp
, p
);
1560 struct in6_ifaddr
*ia
;
1561 struct in6_addr mask
, candidate
, match
;
1562 struct sockaddr_in6
*sin6
;
1565 bzero(&mask
, sizeof(mask
));
1566 if (iflr
->flags
& IFLR_PREFIX
) {
1567 /* lookup a prefix rather than address. */
1568 in6_len2mask(&mask
, iflr
->prefixlen
);
1570 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1571 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
1572 match
.s6_addr32
[0] &= mask
.s6_addr32
[0];
1573 match
.s6_addr32
[1] &= mask
.s6_addr32
[1];
1574 match
.s6_addr32
[2] &= mask
.s6_addr32
[2];
1575 match
.s6_addr32
[3] &= mask
.s6_addr32
[3];
1577 /* if you set extra bits, that's wrong */
1578 if (bcmp(&match
, &sin6
->sin6_addr
, sizeof(match
)))
1583 if (cmd
== SIOCGLIFADDR
) {
1584 /* on getting an address, take the 1st match */
1587 /* on deleting an address, do exact match */
1588 in6_len2mask(&mask
, 128);
1589 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1590 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
1596 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1598 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1603 bcopy(IFA_IN6(ifa
), &candidate
, sizeof(candidate
));
1604 #ifndef SCOPEDROUTING
1606 * XXX: this is adhoc, but is necessary to allow
1607 * a user to specify fe80::/64 (not /10) for a
1608 * link-local address.
1610 if (IN6_IS_ADDR_LINKLOCAL(&candidate
))
1611 candidate
.s6_addr16
[1] = 0;
1613 candidate
.s6_addr32
[0] &= mask
.s6_addr32
[0];
1614 candidate
.s6_addr32
[1] &= mask
.s6_addr32
[1];
1615 candidate
.s6_addr32
[2] &= mask
.s6_addr32
[2];
1616 candidate
.s6_addr32
[3] &= mask
.s6_addr32
[3];
1617 if (IN6_ARE_ADDR_EQUAL(&candidate
, &match
))
1621 return EADDRNOTAVAIL
;
1624 if (cmd
== SIOCGLIFADDR
) {
1625 #ifndef SCOPEDROUTING
1626 struct sockaddr_in6
*s6
;
1629 /* fill in the if_laddrreq structure */
1630 bcopy(&ia
->ia_addr
, &iflr
->addr
, ia
->ia_addr
.sin6_len
);
1631 #ifndef SCOPEDROUTING /* XXX see above */
1632 s6
= (struct sockaddr_in6
*)&iflr
->addr
;
1633 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
1634 s6
->sin6_addr
.s6_addr16
[1] = 0;
1636 in6_addr2scopeid(ifp
, &s6
->sin6_addr
);
1639 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
1640 bcopy(&ia
->ia_dstaddr
, &iflr
->dstaddr
,
1641 ia
->ia_dstaddr
.sin6_len
);
1642 #ifndef SCOPEDROUTING /* XXX see above */
1643 s6
= (struct sockaddr_in6
*)&iflr
->dstaddr
;
1644 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
1645 s6
->sin6_addr
.s6_addr16
[1] = 0;
1647 in6_addr2scopeid(ifp
,
1652 bzero(&iflr
->dstaddr
, sizeof(iflr
->dstaddr
));
1655 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
1658 iflr
->flags
= ia
->ia6_flags
; /* XXX */
1662 struct in6_aliasreq ifra
;
1664 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1665 bzero(&ifra
, sizeof(ifra
));
1666 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
1667 sizeof(ifra
.ifra_name
));
1669 bcopy(&ia
->ia_addr
, &ifra
.ifra_addr
,
1670 ia
->ia_addr
.sin6_len
);
1671 if ((ifp
->if_flags
& IFF_POINTOPOINT
) != 0) {
1672 bcopy(&ia
->ia_dstaddr
, &ifra
.ifra_dstaddr
,
1673 ia
->ia_dstaddr
.sin6_len
);
1675 bzero(&ifra
.ifra_dstaddr
,
1676 sizeof(ifra
.ifra_dstaddr
));
1678 bcopy(&ia
->ia_prefixmask
, &ifra
.ifra_dstaddr
,
1679 ia
->ia_prefixmask
.sin6_len
);
1681 ifra
.ifra_flags
= ia
->ia6_flags
;
1682 return in6_control(so
, SIOCDIFADDR_IN6
, (caddr_t
)&ifra
,
1688 return EOPNOTSUPP
; /* just for safety */
1692 * Initialize an interface's intetnet6 address
1693 * and routing table entry.
1696 in6_ifinit(ifp
, ia
, sin6
, newhost
)
1698 struct in6_ifaddr
*ia
;
1699 struct sockaddr_in6
*sin6
;
1702 int error
= 0, plen
, ifacount
= 0;
1707 * Give the interface a chance to initialize
1708 * if this is its first address,
1709 * and to validate the address if necessary.
1711 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1713 if (ifa
->ifa_addr
== NULL
)
1714 continue; /* just for safety */
1715 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1720 ia
->ia_addr
= *sin6
;
1723 if (ifacount
<= 1 &&
1725 (error
= dlil_ioctl(PF_INET6
, ifp
, SIOCSIFADDR
, (caddr_t
)ia
))) {
1732 ifp
->if_ioctl
&& (error
= (*ifp
->if_ioctl
)(ifp
, SIOCSIFADDR
, (caddr_t
)ia
))) {
1739 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
1741 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1745 * If the destination address is specified for a point-to-point
1746 * interface, install a route to the destination as an interface
1749 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
); /* XXX */
1750 if (plen
== 128 && ia
->ia_dstaddr
.sin6_family
== AF_INET6
) {
1751 if ((error
= rtinit(&(ia
->ia_ifa
), (int)RTM_ADD
,
1752 RTF_UP
| RTF_HOST
)) != 0)
1754 ia
->ia_flags
|= IFA_ROUTE
;
1758 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1760 ia
->ia_ifa
.ifa_flags
|= RTF_CLONING
;
1763 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1765 /* set the rtrequest function to create llinfo */
1766 ia
->ia_ifa
.ifa_rtrequest
= nd6_rtrequest
;
1767 in6_ifaddloop(&(ia
->ia_ifa
));
1774 * Add an address to the list of IP6 multicast addresses for a
1778 in6_addmulti(maddr6
, ifp
, errorp
)
1779 struct in6_addr
*maddr6
;
1783 struct in6_multi
*in6m
;
1784 struct sockaddr_in6 sin6
;
1785 struct ifmultiaddr
*ifma
;
1791 * Call generic routine to add membership or increment
1792 * refcount. It wants addresses in the form of a sockaddr,
1793 * so we build one here (being careful to zero the unused bytes).
1795 bzero(&sin6
, sizeof sin6
);
1796 sin6
.sin6_family
= AF_INET6
;
1797 sin6
.sin6_len
= sizeof sin6
;
1798 sin6
.sin6_addr
= *maddr6
;
1799 *errorp
= if_addmulti(ifp
, (struct sockaddr
*)&sin6
, &ifma
);
1806 * If ifma->ifma_protospec is null, then if_addmulti() created
1807 * a new record. Otherwise, we are done.
1809 if (ifma
->ifma_protospec
!= 0)
1810 return ifma
->ifma_protospec
;
1812 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1813 at interrupt time? If so, need to fix if_addmulti. XXX */
1814 in6m
= (struct in6_multi
*)_MALLOC(sizeof(*in6m
), M_IPMADDR
, M_NOWAIT
);
1820 bzero(in6m
, sizeof *in6m
);
1821 in6m
->in6m_addr
= *maddr6
;
1822 in6m
->in6m_ifp
= ifp
;
1823 in6m
->in6m_ifma
= ifma
;
1824 ifma
->ifma_protospec
= in6m
;
1825 LIST_INSERT_HEAD(&in6_multihead
, in6m
, in6m_entry
);
1828 * Let MLD6 know that we have joined a new IP6 multicast
1831 mld6_start_listening(in6m
);
1837 * Delete a multicast address record.
1841 struct in6_multi
*in6m
;
1843 struct ifmultiaddr
*ifma
= in6m
->in6m_ifma
;
1846 if (ifma
&& ifma
->ifma_refcount
== 1) {
1848 * No remaining claims to this record; let MLD6 know
1849 * that we are leaving the multicast group.
1851 mld6_stop_listening(in6m
);
1852 ifma
->ifma_protospec
= 0;
1853 LIST_REMOVE(in6m
, in6m_entry
);
1854 FREE(in6m
, M_IPMADDR
);
1856 /* XXX - should be separate API for when we have an ifma? */
1858 if_delmultiaddr(ifma
);
1863 * Find an IPv6 interface link-local address specific to an interface.
1866 in6ifa_ifpforlinklocal(ifp
, ignoreflags
)
1872 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1874 if (ifa
->ifa_addr
== NULL
)
1875 continue; /* just for safety */
1876 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1878 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
))) {
1879 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
&
1886 return((struct in6_ifaddr
*)ifa
);
1891 * find the internet address corresponding to a given interface and address.
1894 in6ifa_ifpwithaddr(ifp
, addr
)
1896 struct in6_addr
*addr
;
1900 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1902 if (ifa
->ifa_addr
== NULL
)
1903 continue; /* just for safety */
1904 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1906 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(ifa
)))
1910 return((struct in6_ifaddr
*)ifa
);
1914 * Convert IP6 address to printable (loggable) representation.
1916 static char digits
[] = "0123456789abcdef";
1917 static int ip6round
= 0;
1920 const struct in6_addr
*addr
;
1922 static char ip6buf
[8][48];
1925 const u_short
*a
= (const u_short
*)addr
;
1929 ip6round
= (ip6round
+ 1) & 7;
1930 cp
= ip6buf
[ip6round
];
1932 for (i
= 0; i
< 8; i
++) {
1943 if (dcolon
== 0 && *(a
+ 1) == 0) {
1955 d
= (const u_char
*)a
;
1956 *cp
++ = digits
[*d
>> 4];
1957 *cp
++ = digits
[*d
++ & 0xf];
1958 *cp
++ = digits
[*d
>> 4];
1959 *cp
++ = digits
[*d
& 0xf];
1964 return(ip6buf
[ip6round
]);
1969 struct in6_addr
*in6
;
1971 struct in6_ifaddr
*ia
;
1973 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
))
1976 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
)
1977 if (IN6_ARE_MASKED_ADDR_EQUAL(in6
, &ia
->ia_addr
.sin6_addr
,
1978 &ia
->ia_prefixmask
.sin6_addr
))
1985 in6_is_addr_deprecated(sa6
)
1986 struct sockaddr_in6
*sa6
;
1988 struct in6_ifaddr
*ia
;
1990 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
) {
1991 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
,
1994 ia
->ia_addr
.sin6_scope_id
== sa6
->sin6_scope_id
&&
1996 (ia
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0)
1997 return(1); /* true */
1999 /* XXX: do we still have to go thru the rest of the list? */
2002 return(0); /* false */
2006 * return length of part which dst and src are equal
2010 in6_matchlen(src
, dst
)
2011 struct in6_addr
*src
, *dst
;
2014 u_char
*s
= (u_char
*)src
, *d
= (u_char
*)dst
;
2015 u_char
*lim
= s
+ 16, r
;
2018 if ((r
= (*d
++ ^ *s
++)) != 0) {
2029 /* XXX: to be scope conscious */
2031 in6_are_prefix_equal(p1
, p2
, len
)
2032 struct in6_addr
*p1
, *p2
;
2035 int bytelen
, bitlen
;
2038 if (0 > len
|| len
> 128) {
2039 log(LOG_ERR
, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2047 if (bcmp(&p1
->s6_addr
, &p2
->s6_addr
, bytelen
))
2049 if (p1
->s6_addr
[bytelen
] >> (8 - bitlen
) !=
2050 p2
->s6_addr
[bytelen
] >> (8 - bitlen
))
2057 in6_prefixlen2mask(maskp
, len
)
2058 struct in6_addr
*maskp
;
2061 u_char maskarray
[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2062 int bytelen
, bitlen
, i
;
2065 if (0 > len
|| len
> 128) {
2066 log(LOG_ERR
, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2071 bzero(maskp
, sizeof(*maskp
));
2074 for (i
= 0; i
< bytelen
; i
++)
2075 maskp
->s6_addr
[i
] = 0xff;
2077 maskp
->s6_addr
[bytelen
] = maskarray
[bitlen
- 1];
2081 * return the best address out of the same scope
2084 in6_ifawithscope(oifp
, dst
)
2086 struct in6_addr
*dst
;
2088 int dst_scope
= in6_addrscope(dst
), src_scope
, best_scope
= 0;
2092 struct in6_ifaddr
*ifa_best
= NULL
;
2096 printf("in6_ifawithscope: output interface is not specified\n");
2102 * We search for all addresses on all interfaces from the beginning.
2103 * Comparing an interface with the outgoing interface will be done
2104 * only at the final stage of tiebreaking.
2106 for (ifp
= TAILQ_FIRST(&ifnet
); ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
))
2109 * We can never take an address that breaks the scope zone
2110 * of the destination.
2112 if (in6_addr2scopeid(ifp
, dst
) != in6_addr2scopeid(oifp
, dst
))
2115 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2117 int tlen
= -1, dscopecmp
, bscopecmp
, matchcmp
;
2119 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2122 src_scope
= in6_addrscope(IFA_IN6(ifa
));
2125 * Don't use an address before completing DAD
2126 * nor a duplicated address.
2128 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2132 /* XXX: is there any case to allow anycasts? */
2133 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2137 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2142 * If this is the first address we find,
2145 if (ifa_best
== NULL
)
2149 * ifa_best is never NULL beyond this line except
2150 * within the block labeled "replace".
2154 * If ifa_best has a smaller scope than dst and
2155 * the current address has a larger one than
2156 * (or equal to) dst, always replace ifa_best.
2157 * Also, if the current address has a smaller scope
2158 * than dst, ignore it unless ifa_best also has a
2160 * Consequently, after the two if-clause below,
2161 * the followings must be satisfied:
2162 * (scope(src) < scope(dst) &&
2163 * scope(best) < scope(dst))
2165 * (scope(best) >= scope(dst) &&
2166 * scope(src) >= scope(dst))
2168 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0 &&
2169 IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) >= 0)
2170 goto replace
; /* (A) */
2171 if (IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) < 0 &&
2172 IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) >= 0)
2176 * A deprecated address SHOULD NOT be used in new
2177 * communications if an alternate (non-deprecated)
2178 * address is available and has sufficient scope.
2179 * RFC 2462, Section 5.5.4.
2181 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2182 IN6_IFF_DEPRECATED
) {
2184 * Ignore any deprecated addresses if
2185 * specified by configuration.
2187 if (!ip6_use_deprecated
)
2191 * If we have already found a non-deprecated
2192 * candidate, just ignore deprecated addresses.
2194 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
)
2200 * A non-deprecated address is always preferred
2201 * to a deprecated one regardless of scopes and
2202 * address matching (Note invariants ensured by the
2203 * conditions (A) and (B) above.)
2205 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) &&
2206 (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2207 IN6_IFF_DEPRECATED
) == 0)
2211 * When we use temporary addresses described in
2212 * RFC 3041, we prefer temporary addresses to
2213 * public autoconf addresses. Again, note the
2214 * invariants from (A) and (B). Also note that we
2215 * don't have any preference between static addresses
2216 * and autoconf addresses (despite of whether or not
2217 * the latter is temporary or public.)
2219 if (ip6_use_tempaddr
) {
2220 struct in6_ifaddr
*ifat
;
2222 ifat
= (struct in6_ifaddr
*)ifa
;
2223 if ((ifa_best
->ia6_flags
&
2224 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2225 == IN6_IFF_AUTOCONF
&&
2227 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2228 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
)) {
2231 if ((ifa_best
->ia6_flags
&
2232 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2233 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
) &&
2235 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2236 == IN6_IFF_AUTOCONF
) {
2242 * At this point, we have two cases:
2243 * 1. we are looking at a non-deprecated address,
2244 * and ifa_best is also non-deprecated.
2245 * 2. we are looking at a deprecated address,
2246 * and ifa_best is also deprecated.
2247 * Also, we do not have to consider a case where
2248 * the scope of if_best is larger(smaller) than dst and
2249 * the scope of the current address is smaller(larger)
2250 * than dst. Such a case has already been covered.
2251 * Tiebreaking is done according to the following
2253 * - the scope comparison between the address and
2255 * - the scope comparison between the address and
2256 * ifa_best (bscopecmp)
2257 * - if the address match dst longer than ifa_best
2259 * - if the address is on the outgoing I/F (outI/F)
2261 * Roughly speaking, the selection policy is
2262 * - the most important item is scope. The same scope
2263 * is best. Then search for a larger scope.
2264 * Smaller scopes are the last resort.
2265 * - A deprecated address is chosen only when we have
2266 * no address that has an enough scope, but is
2267 * prefered to any addresses of smaller scopes
2268 * (this must be already done above.)
2269 * - addresses on the outgoing I/F are preferred to
2270 * ones on other interfaces if none of above
2271 * tiebreaks. In the table below, the column "bI"
2272 * means if the best_ifa is on the outgoing
2273 * interface, and the column "sI" means if the ifa
2274 * is on the outgoing interface.
2275 * - If there is no other reasons to choose one,
2276 * longest address match against dst is considered.
2278 * The precise decision table is as follows:
2279 * dscopecmp bscopecmp match bI oI | replace?
2280 * N/A equal N/A Y N | No (1)
2281 * N/A equal N/A N Y | Yes (2)
2282 * N/A equal larger N/A | Yes (3)
2283 * N/A equal !larger N/A | No (4)
2284 * larger larger N/A N/A | No (5)
2285 * larger smaller N/A N/A | Yes (6)
2286 * smaller larger N/A N/A | Yes (7)
2287 * smaller smaller N/A N/A | No (8)
2288 * equal smaller N/A N/A | Yes (9)
2289 * equal larger (already done at A above)
2291 dscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
);
2292 bscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, best_scope
);
2294 if (bscopecmp
== 0) {
2295 struct ifnet
*bifp
= ifa_best
->ia_ifp
;
2297 if (bifp
== oifp
&& ifp
!= oifp
) /* (1) */
2299 if (bifp
!= oifp
&& ifp
== oifp
) /* (2) */
2303 * Both bifp and ifp are on the outgoing
2304 * interface, or both two are on a different
2305 * interface from the outgoing I/F.
2306 * now we need address matching against dst
2309 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
2310 matchcmp
= tlen
- blen
;
2311 if (matchcmp
> 0) /* (3) */
2315 if (dscopecmp
> 0) {
2316 if (bscopecmp
> 0) /* (5) */
2318 goto replace
; /* (6) */
2320 if (dscopecmp
< 0) {
2321 if (bscopecmp
> 0) /* (7) */
2326 /* now dscopecmp must be 0 */
2328 goto replace
; /* (9) */
2331 ifa_best
= (struct in6_ifaddr
*)ifa
;
2332 blen
= tlen
>= 0 ? tlen
:
2333 in6_matchlen(IFA_IN6(ifa
), dst
);
2334 best_scope
= in6_addrscope(&ifa_best
->ia_addr
.sin6_addr
);
2338 /* count statistics for future improvements */
2339 if (ifa_best
== NULL
)
2340 ip6stat
.ip6s_sources_none
++;
2342 if (oifp
== ifa_best
->ia_ifp
)
2343 ip6stat
.ip6s_sources_sameif
[best_scope
]++;
2345 ip6stat
.ip6s_sources_otherif
[best_scope
]++;
2347 if (best_scope
== dst_scope
)
2348 ip6stat
.ip6s_sources_samescope
[best_scope
]++;
2350 ip6stat
.ip6s_sources_otherscope
[best_scope
]++;
2352 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) != 0)
2353 ip6stat
.ip6s_sources_deprecated
[best_scope
]++;
2360 * return the best address out of the same scope. if no address was
2361 * found, return the first valid address from designated IF.
2364 in6_ifawithifp(ifp
, dst
)
2366 struct in6_addr
*dst
;
2368 int dst_scope
= in6_addrscope(dst
), blen
= -1, tlen
;
2370 struct in6_ifaddr
*besta
= 0;
2371 struct in6_ifaddr
*dep
[2]; /* last-resort: deprecated */
2373 dep
[0] = dep
[1] = NULL
;
2376 * We first look for addresses in the same scope.
2377 * If there is one, return it.
2378 * If two or more, return one which matches the dst longest.
2379 * If none, return one of global addresses assigned other ifs.
2381 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2383 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2385 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
)
2386 continue; /* XXX: is there any case to allow anycast? */
2387 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_NOTREADY
)
2388 continue; /* don't use this interface */
2389 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DETACHED
)
2391 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
2392 if (ip6_use_deprecated
)
2393 dep
[0] = (struct in6_ifaddr
*)ifa
;
2397 if (dst_scope
== in6_addrscope(IFA_IN6(ifa
))) {
2399 * call in6_matchlen() as few as possible
2403 blen
= in6_matchlen(&besta
->ia_addr
.sin6_addr
, dst
);
2404 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
2407 besta
= (struct in6_ifaddr
*)ifa
;
2410 besta
= (struct in6_ifaddr
*)ifa
;
2416 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2418 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2420 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
)
2421 continue; /* XXX: is there any case to allow anycast? */
2422 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_NOTREADY
)
2423 continue; /* don't use this interface */
2424 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DETACHED
)
2426 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
2427 if (ip6_use_deprecated
)
2428 dep
[1] = (struct in6_ifaddr
*)ifa
;
2432 return (struct in6_ifaddr
*)ifa
;
2435 /* use the last-resort values, that are, deprecated addresses */
2444 extern int in6_init2done
;
2447 * perform DAD when interface becomes IFF_UP.
2450 in6_if_up(ifp
, ifra
)
2452 struct in6_aliasreq
*ifra
;
2455 struct in6_ifaddr
*ia
;
2456 int dad_delay
; /* delay ticks before DAD output */
2462 * special cases, like 6to4, are handled in in6_ifattach
2464 in6_ifattach(ifp
, NULL
, ifra
);
2467 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2469 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2471 ia
= (struct in6_ifaddr
*)ifa
;
2472 if (ia
->ia6_flags
& IN6_IFF_TENTATIVE
)
2473 nd6_dad_start(ifa
, &dad_delay
);
2481 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0)
2484 switch (ifp
->if_type
) {
2490 * These interfaces do not have the IFF_LOOPBACK flag,
2491 * but loop packets back. We do not have to do DAD on such
2492 * interfaces. We should even omit it, because loop-backed
2493 * NS would confuse the DAD procedure.
2498 * Our DAD routine requires the interface up and running.
2499 * However, some interfaces can be up before the RUNNING
2500 * status. Additionaly, users may try to assign addresses
2501 * before the interface becomes up (or running).
2502 * We simply skip DAD in such a case as a work around.
2503 * XXX: we should rather mark "tentative" on such addresses,
2504 * and do DAD after the interface becomes ready.
2506 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) !=
2507 (IFF_UP
|IFF_RUNNING
))
2515 * Calculate max IPv6 MTU through all the interfaces and store it
2521 unsigned long maxmtu
= 0;
2524 for (ifp
= TAILQ_FIRST(&ifnet
); ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
))
2526 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
2527 nd_ifinfo
[ifp
->if_index
].linkmtu
> maxmtu
)
2528 maxmtu
= nd_ifinfo
[ifp
->if_index
].linkmtu
;
2530 if (maxmtu
) /* update only when maxmtu is positive */
2531 in6_maxmtu
= maxmtu
;
2535 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2536 * v4 mapped addr or v4 compat addr
2539 in6_sin6_2_sin(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
2541 bzero(sin
, sizeof(*sin
));
2542 sin
->sin_len
= sizeof(struct sockaddr_in
);
2543 sin
->sin_family
= AF_INET
;
2544 sin
->sin_port
= sin6
->sin6_port
;
2545 sin
->sin_addr
.s_addr
= sin6
->sin6_addr
.s6_addr32
[3];
2548 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2550 in6_sin_2_v4mapsin6(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
2552 bzero(sin6
, sizeof(*sin6
));
2553 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2554 sin6
->sin6_family
= AF_INET6
;
2555 sin6
->sin6_port
= sin
->sin_port
;
2556 sin6
->sin6_addr
.s6_addr32
[0] = 0;
2557 sin6
->sin6_addr
.s6_addr32
[1] = 0;
2558 sin6
->sin6_addr
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
2559 sin6
->sin6_addr
.s6_addr32
[3] = sin
->sin_addr
.s_addr
;
2562 /* Convert sockaddr_in6 into sockaddr_in. */
2564 in6_sin6_2_sin_in_sock(struct sockaddr
*nam
)
2566 struct sockaddr_in
*sin_p
;
2567 struct sockaddr_in6 sin6
;
2570 * Save original sockaddr_in6 addr and convert it
2573 sin6
= *(struct sockaddr_in6
*)nam
;
2574 sin_p
= (struct sockaddr_in
*)nam
;
2575 in6_sin6_2_sin(sin_p
, &sin6
);
2578 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2580 in6_sin_2_v4mapsin6_in_sock(struct sockaddr
**nam
)
2582 struct sockaddr_in
*sin_p
;
2583 struct sockaddr_in6
*sin6_p
;
2585 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof *sin6_p
, M_SONAME
,
2587 sin_p
= (struct sockaddr_in
*)*nam
;
2588 in6_sin_2_v4mapsin6(sin_p
, sin6_p
);
2589 FREE(*nam
, M_SONAME
);
2590 *nam
= (struct sockaddr
*)sin6_p
;
2593 /* Posts in6_event_data message kernel events */
2595 in6_post_msg(struct ifnet
*ifp
, u_long event_code
, struct in6_ifaddr
*ifa
)
2597 struct kev_msg ev_msg
;
2598 struct kev_in6_data in6_event_data
;
2600 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
2601 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
2602 ev_msg
.kev_subclass
= KEV_INET6_SUBCLASS
;
2603 ev_msg
.event_code
= event_code
;
2605 in6_event_data
.ia_addr
= ifa
->ia_addr
;
2606 in6_event_data
.ia_net
= ifa
->ia_net
;
2607 in6_event_data
.ia_dstaddr
= ifa
->ia_dstaddr
;
2608 in6_event_data
.ia_prefixmask
= ifa
->ia_prefixmask
;
2609 in6_event_data
.ia_plen
= ifa
->ia_plen
;
2610 in6_event_data
.ia6_flags
= (u_int32_t
)ifa
->ia6_flags
;
2611 in6_event_data
.ia_lifetime
= ifa
->ia6_lifetime
;
2614 strncpy(&in6_event_data
.link_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
2615 in6_event_data
.link_data
.if_family
= ifp
->if_family
;
2616 in6_event_data
.link_data
.if_unit
= (unsigned long) ifp
->if_unit
;
2619 ev_msg
.dv
[0].data_ptr
= &in6_event_data
;
2620 ev_msg
.dv
[0].data_length
= sizeof(struct kev_in6_data
);
2621 ev_msg
.dv
[1].data_length
= 0;
2623 kev_post_msg(&ev_msg
);