1 /* $FreeBSD: src/sys/netinet6/in6.c,v 1.7.2.9 2002/04/28 05:40:26 suz Exp $ */
2 /* $DragonFly: src/sys/netinet6/in6.c,v 1.24 2006/12/22 23:57:53 swildner Exp $ */
3 /* $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)in.c 8.2 (Berkeley) 11/15/93
70 #include "opt_inet6.h"
72 #include <sys/param.h>
73 #include <sys/errno.h>
74 #include <sys/malloc.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/sockio.h>
78 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/syslog.h>
83 #include <sys/thread2.h>
86 #include <net/if_types.h>
87 #include <net/route.h>
88 #include <net/if_dl.h>
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92 #include <netinet/if_ether.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/ip.h>
95 #include <netinet/in_pcb.h>
97 #include <netinet/ip6.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet6/nd6.h>
100 #include <netinet6/mld6_var.h>
101 #include <netinet6/ip6_mroute.h>
102 #include <netinet6/in6_ifattach.h>
103 #include <netinet6/scope6_var.h>
104 #include <netinet6/in6_pcb.h>
105 #include <netinet6/in6_var.h>
107 #include <net/net_osdep.h>
110 * Definitions of some costant IP6 addresses.
112 const struct in6_addr kin6addr_any
= IN6ADDR_ANY_INIT
;
113 const struct in6_addr kin6addr_loopback
= IN6ADDR_LOOPBACK_INIT
;
114 const struct in6_addr kin6addr_nodelocal_allnodes
=
115 IN6ADDR_NODELOCAL_ALLNODES_INIT
;
116 const struct in6_addr kin6addr_linklocal_allnodes
=
117 IN6ADDR_LINKLOCAL_ALLNODES_INIT
;
118 const struct in6_addr kin6addr_linklocal_allrouters
=
119 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT
;
121 const struct in6_addr in6mask0
= IN6MASK0
;
122 const struct in6_addr in6mask32
= IN6MASK32
;
123 const struct in6_addr in6mask64
= IN6MASK64
;
124 const struct in6_addr in6mask96
= IN6MASK96
;
125 const struct in6_addr in6mask128
= IN6MASK128
;
127 const struct sockaddr_in6 sa6_any
= {sizeof(sa6_any
), AF_INET6
,
128 0, 0, IN6ADDR_ANY_INIT
, 0};
130 static int in6_lifaddr_ioctl (struct socket
*, u_long
, caddr_t
,
131 struct ifnet
*, struct thread
*);
132 static int in6_ifinit (struct ifnet
*, struct in6_ifaddr
*,
133 struct sockaddr_in6
*, int);
134 static void in6_unlink_ifa (struct in6_ifaddr
*, struct ifnet
*);
135 static void in6_ifloop_request_callback(int, int, struct rt_addrinfo
*, struct rtentry
*, void *);
137 struct in6_multihead in6_multihead
; /* XXX BSS initialization */
139 int (*faithprefix_p
)(struct in6_addr
*);
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 rt_addrinfo rtinfo
;
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 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
166 rtinfo
.rti_info
[RTAX_DST
] = ifa
->ifa_addr
;
167 rtinfo
.rti_info
[RTAX_GATEWAY
] = ifa
->ifa_addr
;
168 rtinfo
.rti_info
[RTAX_NETMASK
] = (struct sockaddr
*)&all1_sa
;
169 rtinfo
.rti_flags
= RTF_UP
|RTF_HOST
|RTF_LLINFO
;
171 error
= rtrequest1_global(cmd
, &rtinfo
,
172 in6_ifloop_request_callback
, ifa
);
174 log(LOG_ERR
, "in6_ifloop_request: "
175 "%s operation failed for %s (errno=%d)\n",
176 cmd
== RTM_ADD
? "ADD" : "DELETE",
177 ip6_sprintf(&((struct in6_ifaddr
*)ifa
)->ia_addr
.sin6_addr
),
183 in6_ifloop_request_callback(int cmd
, int error
, struct rt_addrinfo
*rtinfo
,
184 struct rtentry
*rt
, void *arg
)
186 struct ifaddr
*ifa
= arg
;
192 * Make sure rt_ifa be equal to IFA, the second argument of the
194 * We need this because when we refer to rt_ifa->ia6_flags in
195 * ip6_input, we assume that the rt_ifa points to the address instead
196 * of the loopback address.
198 if (cmd
== RTM_ADD
&& rt
&& ifa
!= rt
->rt_ifa
) {
207 * Report the addition/removal of the address to the routing socket.
208 * XXX: since we called rtinit for a p2p interface with a destination,
209 * we end up reporting twice in such a case. Should we rather
210 * omit the second report?
214 rt_newaddrmsg(cmd
, ifa
, error
, rt
);
215 if (cmd
== RTM_DELETE
) {
216 if (rt
->rt_refcnt
== 0) {
223 /* no way to return any new error */
228 * Add ownaddr as loopback rtentry. We previously add the route only if
229 * necessary (ex. on a p2p link). However, since we now manage addresses
230 * separately from prefixes, we should always add the route. We can't
231 * rely on the cloning mechanism from the corresponding interface route
235 in6_ifaddloop(struct ifaddr
*ifa
)
239 /* If there is no loopback entry, allocate one. */
240 rt
= rtpurelookup(ifa
->ifa_addr
);
241 if (rt
== NULL
|| !(rt
->rt_flags
& RTF_HOST
) ||
242 !(rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
))
243 in6_ifloop_request(RTM_ADD
, ifa
);
249 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
253 in6_ifremloop(struct ifaddr
*ifa
)
255 struct in6_ifaddr
*ia
;
260 * Some of BSD variants do not remove cloned routes
261 * from an interface direct route, when removing the direct route
262 * (see comments in net/net_osdep.h). Even for variants that do remove
263 * cloned routes, they could fail to remove the cloned routes when
264 * we handle multple addresses that share a common prefix.
265 * So, we should remove the route corresponding to the deleted address
266 * regardless of the result of in6_is_ifloop_auto().
270 * Delete the entry only if exact one ifa exists. More than one ifa
271 * can exist if we assign a same single address to multiple
272 * (probably p2p) interfaces.
273 * XXX: we should avoid such a configuration in IPv6...
275 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
) {
276 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa
), &ia
->ia_addr
.sin6_addr
)) {
285 * Before deleting, check if a corresponding loopbacked host
286 * route surely exists. With this check, we can avoid to
287 * delete an interface direct route whose destination is same
288 * as the address being removed. This can happen when remofing
289 * a subnet-router anycast address on an interface attahced
290 * to a shared medium.
292 rt
= rtpurelookup(ifa
->ifa_addr
);
293 if (rt
!= NULL
&& (rt
->rt_flags
& RTF_HOST
) &&
294 (rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
)) {
296 in6_ifloop_request(RTM_DELETE
, ifa
);
302 in6_ifindex2scopeid(int idx
)
306 struct sockaddr_in6
*sin6
;
308 if (idx
< 0 || if_index
< idx
)
310 ifp
= ifindex2ifnet
[idx
];
312 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
314 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
316 sin6
= (struct sockaddr_in6
*)ifa
->ifa_addr
;
317 if (IN6_IS_ADDR_SITELOCAL(&sin6
->sin6_addr
))
318 return sin6
->sin6_scope_id
& 0xffff;
325 in6_mask2len(struct in6_addr
*mask
, u_char
*lim0
)
328 u_char
*lim
= lim0
, *p
;
331 lim0
- (u_char
*)mask
> sizeof(*mask
)) /* ignore the scope_id part */
332 lim
= (u_char
*)mask
+ sizeof(*mask
);
333 for (p
= (u_char
*)mask
; p
< lim
; x
++, p
++) {
339 for (y
= 0; y
< 8; y
++) {
340 if ((*p
& (0x80 >> y
)) == 0)
346 * when the limit pointer is given, do a stricter check on the
350 if (y
!= 0 && (*p
& (0x00ff >> y
)) != 0)
352 for (p
= p
+ 1; p
< lim
; p
++)
361 in6_len2mask(struct in6_addr
*mask
, int len
)
365 bzero(mask
, sizeof(*mask
));
366 for (i
= 0; i
< len
/ 8; i
++)
367 mask
->s6_addr8
[i
] = 0xff;
369 mask
->s6_addr8
[i
] = (0xff00 >> (len
% 8)) & 0xff;
372 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
373 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
376 in6_control(struct socket
*so
, u_long cmd
, caddr_t data
,
377 struct ifnet
*ifp
, struct thread
*td
)
379 struct in6_ifreq
*ifr
= (struct in6_ifreq
*)data
;
380 struct in6_ifaddr
*ia
= NULL
;
381 struct in6_aliasreq
*ifra
= (struct in6_aliasreq
*)data
;
390 case SIOCGETSGCNT_IN6
:
391 case SIOCGETMIFCNT_IN6
:
392 return (mrt6_ioctl(cmd
, data
));
399 case SIOCSNDFLUSH_IN6
:
400 case SIOCSPFXFLUSH_IN6
:
401 case SIOCSRTRFLUSH_IN6
:
402 case SIOCSDEFIFACE_IN6
:
403 case SIOCSIFINFO_FLAGS
:
407 case OSIOCGIFINFO_IN6
:
408 case SIOCGIFINFO_IN6
:
411 case SIOCGNBRINFO_IN6
:
412 case SIOCGDEFIFACE_IN6
:
413 return (nd6_ioctl(cmd
, data
, ifp
));
417 case SIOCSIFPREFIX_IN6
:
418 case SIOCDIFPREFIX_IN6
:
419 case SIOCAIFPREFIX_IN6
:
420 case SIOCCIFPREFIX_IN6
:
421 case SIOCSGIFPREFIX_IN6
:
422 case SIOCGIFPREFIX_IN6
:
424 "prefix ioctls are now invalidated. "
425 "please use ifconfig.\n");
433 return (scope6_set(ifp
,
434 (struct scope6_id
*)ifr
->ifr_ifru
.ifru_scope_id
));
437 return (scope6_get(ifp
,
438 (struct scope6_id
*)ifr
->ifr_ifru
.ifru_scope_id
));
441 return (scope6_get_default((struct scope6_id
*)
442 ifr
->ifr_ifru
.ifru_scope_id
));
453 return in6_lifaddr_ioctl(so
, cmd
, data
, ifp
, td
);
457 * Find address for this interface, if it exists.
459 if (ifra
->ifra_addr
.sin6_family
== AF_INET6
) { /* XXX */
460 struct sockaddr_in6
*sa6
=
461 (struct sockaddr_in6
*)&ifra
->ifra_addr
;
463 if (IN6_IS_ADDR_LINKLOCAL(&sa6
->sin6_addr
)) {
464 if (sa6
->sin6_addr
.s6_addr16
[1] == 0) {
465 /* link ID is not embedded by the user */
466 sa6
->sin6_addr
.s6_addr16
[1] =
467 htons(ifp
->if_index
);
468 } else if (sa6
->sin6_addr
.s6_addr16
[1] !=
469 htons(ifp
->if_index
)) {
470 return (EINVAL
); /* link ID contradicts */
472 if (sa6
->sin6_scope_id
) {
473 if (sa6
->sin6_scope_id
!=
474 (u_int32_t
)ifp
->if_index
)
476 sa6
->sin6_scope_id
= 0; /* XXX: good way? */
479 ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
);
483 case SIOCSIFADDR_IN6
:
484 case SIOCSIFDSTADDR_IN6
:
485 case SIOCSIFNETMASK_IN6
:
487 * Since IPv6 allows a node to assign multiple addresses
488 * on a single interface, SIOCSIFxxx ioctls are not suitable
489 * and should be unused.
491 /* we decided to obsolete this command (20000704) */
494 case SIOCDIFADDR_IN6
:
496 * for IPv4, we look for existing in_ifaddr here to allow
497 * "ifconfig if0 delete" to remove first IPv4 address on the
498 * interface. For IPv6, as the spec allow multiple interface
499 * address from the day one, we consider "remove the first one"
500 * semantics to be not preferable.
503 return (EADDRNOTAVAIL
);
505 case SIOCAIFADDR_IN6
:
507 * We always require users to specify a valid IPv6 address for
508 * the corresponding operation.
510 if (ifra
->ifra_addr
.sin6_family
!= AF_INET6
||
511 ifra
->ifra_addr
.sin6_len
!= sizeof(struct sockaddr_in6
))
512 return (EAFNOSUPPORT
);
518 case SIOCGIFADDR_IN6
:
519 /* This interface is basically deprecated. use SIOCGIFCONF. */
521 case SIOCGIFAFLAG_IN6
:
522 case SIOCGIFNETMASK_IN6
:
523 case SIOCGIFDSTADDR_IN6
:
524 case SIOCGIFALIFETIME_IN6
:
525 /* must think again about its semantics */
527 return (EADDRNOTAVAIL
);
529 case SIOCSIFALIFETIME_IN6
:
531 struct in6_addrlifetime
*lt
;
536 return (EADDRNOTAVAIL
);
537 /* sanity for overflow - beware unsigned */
538 lt
= &ifr
->ifr_ifru
.ifru_lifetime
;
539 if (lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
540 && lt
->ia6t_vltime
+ time_second
< time_second
) {
543 if (lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
544 && lt
->ia6t_pltime
+ time_second
< time_second
) {
553 case SIOCGIFADDR_IN6
:
554 ifr
->ifr_addr
= ia
->ia_addr
;
557 case SIOCGIFDSTADDR_IN6
:
558 if (!(ifp
->if_flags
& IFF_POINTOPOINT
))
561 * XXX: should we check if ifa_dstaddr is NULL and return
564 ifr
->ifr_dstaddr
= ia
->ia_dstaddr
;
567 case SIOCGIFNETMASK_IN6
:
568 ifr
->ifr_addr
= ia
->ia_prefixmask
;
571 case SIOCGIFAFLAG_IN6
:
572 ifr
->ifr_ifru
.ifru_flags6
= ia
->ia6_flags
;
575 case SIOCGIFSTAT_IN6
:
578 bzero(&ifr
->ifr_ifru
.ifru_stat
,
579 sizeof(ifr
->ifr_ifru
.ifru_stat
));
580 ifr
->ifr_ifru
.ifru_stat
=
581 *((struct in6_ifextra
*)ifp
->if_afdata
[AF_INET6
])->in6_ifstat
;
584 case SIOCGIFSTAT_ICMP6
:
585 bzero(&ifr
->ifr_ifru
.ifru_stat
,
586 sizeof(ifr
->ifr_ifru
.ifru_icmp6stat
));
587 ifr
->ifr_ifru
.ifru_icmp6stat
=
588 *((struct in6_ifextra
*)ifp
->if_afdata
[AF_INET6
])->icmp6_ifstat
;
591 case SIOCGIFALIFETIME_IN6
:
592 ifr
->ifr_ifru
.ifru_lifetime
= ia
->ia6_lifetime
;
595 case SIOCSIFALIFETIME_IN6
:
596 ia
->ia6_lifetime
= ifr
->ifr_ifru
.ifru_lifetime
;
598 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
599 ia
->ia6_lifetime
.ia6t_expire
=
600 time_second
+ ia
->ia6_lifetime
.ia6t_vltime
;
602 ia
->ia6_lifetime
.ia6t_expire
= 0;
603 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
604 ia
->ia6_lifetime
.ia6t_preferred
=
605 time_second
+ ia
->ia6_lifetime
.ia6t_pltime
;
607 ia
->ia6_lifetime
.ia6t_preferred
= 0;
610 case SIOCAIFADDR_IN6
:
613 struct nd_prefix pr0
, *pr
;
616 * first, make or update the interface address structure,
617 * and link it to the list.
619 if ((error
= in6_update_ifa(ifp
, ifra
, ia
)) != 0)
623 * then, make the prefix on-link on the interface.
624 * XXX: we'd rather create the prefix before the address, but
625 * we need at least one address to install the corresponding
626 * interface route, so we configure the address first.
630 * convert mask to prefix length (prefixmask has already
631 * been validated in in6_update_ifa().
633 bzero(&pr0
, sizeof(pr0
));
635 pr0
.ndpr_plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
637 if (pr0
.ndpr_plen
== 128)
638 break; /* we don't need to install a host route. */
639 pr0
.ndpr_prefix
= ifra
->ifra_addr
;
640 pr0
.ndpr_mask
= ifra
->ifra_prefixmask
.sin6_addr
;
641 /* apply the mask for safety. */
642 for (i
= 0; i
< 4; i
++) {
643 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
644 ifra
->ifra_prefixmask
.sin6_addr
.s6_addr32
[i
];
647 * XXX: since we don't have an API to set prefix (not address)
648 * lifetimes, we just use the same lifetimes as addresses.
649 * The (temporarily) installed lifetimes can be overridden by
650 * later advertised RAs (when accept_rtadv is non 0), which is
651 * an intended behavior.
653 pr0
.ndpr_raf_onlink
= 1; /* should be configurable? */
655 ((ifra
->ifra_flags
& IN6_IFF_AUTOCONF
) != 0);
656 pr0
.ndpr_vltime
= ifra
->ifra_lifetime
.ia6t_vltime
;
657 pr0
.ndpr_pltime
= ifra
->ifra_lifetime
.ia6t_pltime
;
659 /* add the prefix if there's one. */
660 if ((pr
= nd6_prefix_lookup(&pr0
)) == NULL
) {
662 * nd6_prelist_add will install the corresponding
665 if ((error
= nd6_prelist_add(&pr0
, NULL
, &pr
)) != 0)
668 log(LOG_ERR
, "nd6_prelist_add succedded but "
670 return (EINVAL
); /* XXX panic here? */
673 if ((ia
= in6ifa_ifpwithaddr(ifp
, &ifra
->ifra_addr
.sin6_addr
))
675 /* XXX: this should not happen! */
676 log(LOG_ERR
, "in6_control: addition succeeded, but"
679 if ((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) &&
680 ia
->ia6_ndpr
== NULL
) { /* new autoconfed addr */
685 * If this is the first autoconf address from
686 * the prefix, create a temporary address
687 * as well (when specified).
689 if (ip6_use_tempaddr
&&
690 pr
->ndpr_refcnt
== 1) {
692 if ((e
= in6_tmpifadd(ia
, 1)) != 0) {
693 log(LOG_NOTICE
, "in6_control: "
694 "failed to create a "
695 "temporary address, "
703 * this might affect the status of autoconfigured
704 * addresses, that is, this address might make
705 * other addresses detached.
707 pfxlist_onlink_check();
709 if (error
== 0 && ia
)
710 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
);
714 case SIOCDIFADDR_IN6
:
717 struct nd_prefix pr0
, *pr
;
720 * If the address being deleted is the only one that owns
721 * the corresponding prefix, expire the prefix as well.
722 * XXX: theoretically, we don't have to warry about such
723 * relationship, since we separate the address management
724 * and the prefix management. We do this, however, to provide
725 * as much backward compatibility as possible in terms of
726 * the ioctl operation.
728 bzero(&pr0
, sizeof(pr0
));
730 pr0
.ndpr_plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
732 if (pr0
.ndpr_plen
== 128)
734 pr0
.ndpr_prefix
= ia
->ia_addr
;
735 pr0
.ndpr_mask
= ia
->ia_prefixmask
.sin6_addr
;
736 for (i
= 0; i
< 4; i
++) {
737 pr0
.ndpr_prefix
.sin6_addr
.s6_addr32
[i
] &=
738 ia
->ia_prefixmask
.sin6_addr
.s6_addr32
[i
];
741 * The logic of the following condition is a bit complicated.
742 * We expire the prefix when
743 * 1. the address obeys autoconfiguration and it is the
744 * only owner of the associated prefix, or
745 * 2. the address does not obey autoconf and there is no
746 * other owner of the prefix.
748 if ((pr
= nd6_prefix_lookup(&pr0
)) != NULL
&&
749 (((ia
->ia6_flags
& IN6_IFF_AUTOCONF
) &&
750 pr
->ndpr_refcnt
== 1) ||
751 (!(ia
->ia6_flags
& IN6_IFF_AUTOCONF
) &&
752 pr
->ndpr_refcnt
== 0))) {
753 pr
->ndpr_expire
= 1; /* XXX: just for expiration */
757 in6_purgeaddr(&ia
->ia_ifa
);
758 EVENTHANDLER_INVOKE(ifaddr_event
, ifp
);
763 if (ifp
== NULL
|| ifp
->if_ioctl
== 0)
765 lwkt_serialize_enter(ifp
->if_serializer
);
766 error
= ifp
->if_ioctl(ifp
, cmd
, data
, td
->td_proc
->p_ucred
);
767 lwkt_serialize_exit(ifp
->if_serializer
);
775 * Update parameters of an IPv6 interface address.
776 * If necessary, a new entry is created and linked into address chains.
777 * This function is separated from in6_control().
778 * XXX: should this be performed under splnet()?
781 in6_update_ifa(struct ifnet
*ifp
, struct in6_aliasreq
*ifra
,
782 struct in6_ifaddr
*ia
)
784 int error
= 0, hostIsNew
= 0, plen
= -1;
785 struct in6_ifaddr
*oia
;
786 struct sockaddr_in6 dst6
;
787 struct in6_addrlifetime
*lt
;
789 /* Validate parameters */
790 if (ifp
== NULL
|| ifra
== NULL
) /* this maybe redundant */
794 * The destination address for a p2p link must have a family
795 * of AF_UNSPEC or AF_INET6.
797 if ((ifp
->if_flags
& IFF_POINTOPOINT
) &&
798 ifra
->ifra_dstaddr
.sin6_family
!= AF_INET6
&&
799 ifra
->ifra_dstaddr
.sin6_family
!= AF_UNSPEC
)
800 return (EAFNOSUPPORT
);
802 * validate ifra_prefixmask. don't check sin6_family, netmask
803 * does not carry fields other than sin6_len.
805 if (ifra
->ifra_prefixmask
.sin6_len
> sizeof(struct sockaddr_in6
))
808 * Because the IPv6 address architecture is classless, we require
809 * users to specify a (non 0) prefix length (mask) for a new address.
810 * We also require the prefix (when specified) mask is valid, and thus
811 * reject a non-consecutive mask.
813 if (ia
== NULL
&& ifra
->ifra_prefixmask
.sin6_len
== 0)
815 if (ifra
->ifra_prefixmask
.sin6_len
!= 0) {
816 plen
= in6_mask2len(&ifra
->ifra_prefixmask
.sin6_addr
,
817 (u_char
*)&ifra
->ifra_prefixmask
+
818 ifra
->ifra_prefixmask
.sin6_len
);
824 * In this case, ia must not be NULL. We just use its prefix
827 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
);
830 * If the destination address on a p2p interface is specified,
831 * and the address is a scoped one, validate/set the scope
834 dst6
= ifra
->ifra_dstaddr
;
835 if ((ifp
->if_flags
& (IFF_POINTOPOINT
|IFF_LOOPBACK
)) &&
836 (dst6
.sin6_family
== AF_INET6
)) {
839 if ((error
= in6_recoverscope(&dst6
,
840 &ifra
->ifra_dstaddr
.sin6_addr
,
843 scopeid
= in6_addr2scopeid(ifp
, &dst6
.sin6_addr
);
844 if (dst6
.sin6_scope_id
== 0) /* user omit to specify the ID. */
845 dst6
.sin6_scope_id
= scopeid
;
846 else if (dst6
.sin6_scope_id
!= scopeid
)
847 return (EINVAL
); /* scope ID mismatch. */
848 if ((error
= in6_embedscope(&dst6
.sin6_addr
, &dst6
, NULL
, NULL
))
851 dst6
.sin6_scope_id
= 0; /* XXX */
854 * The destination address can be specified only for a p2p or a
855 * loopback interface. If specified, the corresponding prefix length
858 if (ifra
->ifra_dstaddr
.sin6_family
== AF_INET6
) {
859 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) == 0) {
860 /* XXX: noisy message */
861 log(LOG_INFO
, "in6_update_ifa: a destination can be "
862 "specified for a p2p or a loopback IF only\n");
867 * The following message seems noisy, but we dare to
868 * add it for diagnosis.
870 log(LOG_INFO
, "in6_update_ifa: prefixlen must be 128 "
871 "when dstaddr is specified\n");
875 /* lifetime consistency check */
876 lt
= &ifra
->ifra_lifetime
;
877 if (lt
->ia6t_vltime
!= ND6_INFINITE_LIFETIME
878 && lt
->ia6t_vltime
+ time_second
< time_second
) {
881 if (lt
->ia6t_vltime
== 0) {
883 * the following log might be noisy, but this is a typical
884 * configuration mistake or a tool's bug.
887 "in6_update_ifa: valid lifetime is 0 for %s\n",
888 ip6_sprintf(&ifra
->ifra_addr
.sin6_addr
));
890 if (lt
->ia6t_pltime
!= ND6_INFINITE_LIFETIME
891 && lt
->ia6t_pltime
+ time_second
< time_second
) {
896 * If this is a new address, allocate a new ifaddr and link it
902 * When in6_update_ifa() is called in a process of a received
903 * RA, it is called under splnet(). So, we should call malloc
906 ia
= (struct in6_ifaddr
*)
907 kmalloc(sizeof(*ia
), M_IFADDR
, M_NOWAIT
);
910 bzero((caddr_t
)ia
, sizeof(*ia
));
911 /* Initialize the address and masks */
912 ia
->ia_ifa
.ifa_addr
= (struct sockaddr
*)&ia
->ia_addr
;
913 ia
->ia_addr
.sin6_family
= AF_INET6
;
914 ia
->ia_addr
.sin6_len
= sizeof(ia
->ia_addr
);
915 if ((ifp
->if_flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) != 0) {
917 * XXX: some functions expect that ifa_dstaddr is not
918 * NULL for p2p interfaces.
920 ia
->ia_ifa
.ifa_dstaddr
921 = (struct sockaddr
*)&ia
->ia_dstaddr
;
923 ia
->ia_ifa
.ifa_dstaddr
= NULL
;
925 ia
->ia_ifa
.ifa_netmask
926 = (struct sockaddr
*)&ia
->ia_prefixmask
;
929 if ((oia
= in6_ifaddr
) != NULL
) {
930 for ( ; oia
->ia_next
; oia
= oia
->ia_next
)
936 TAILQ_INSERT_TAIL(&ifp
->if_addrlist
, &ia
->ia_ifa
,
940 /* set prefix mask */
941 if (ifra
->ifra_prefixmask
.sin6_len
) {
943 * We prohibit changing the prefix length of an existing
945 * + such an operation should be rare in IPv6, and
946 * + the operation would confuse prefix management.
948 if (ia
->ia_prefixmask
.sin6_len
&&
949 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
) != plen
) {
950 log(LOG_INFO
, "in6_update_ifa: the prefix length of an"
951 " existing (%s) address should not be changed\n",
952 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
956 ia
->ia_prefixmask
= ifra
->ifra_prefixmask
;
960 * If a new destination address is specified, scrub the old one and
961 * install the new destination. Note that the interface must be
962 * p2p or loopback (see the check above.)
964 if (dst6
.sin6_family
== AF_INET6
&&
965 !IN6_ARE_ADDR_EQUAL(&dst6
.sin6_addr
,
966 &ia
->ia_dstaddr
.sin6_addr
)) {
969 if ((ia
->ia_flags
& IFA_ROUTE
) &&
970 (e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
))
972 log(LOG_ERR
, "in6_update_ifa: failed to remove "
973 "a route to the old destination: %s\n",
974 ip6_sprintf(&ia
->ia_addr
.sin6_addr
));
975 /* proceed anyway... */
978 ia
->ia_flags
&= ~IFA_ROUTE
;
979 ia
->ia_dstaddr
= dst6
;
982 /* reset the interface and routing table appropriately. */
983 if ((error
= in6_ifinit(ifp
, ia
, &ifra
->ifra_addr
, hostIsNew
)) != 0)
987 * Beyond this point, we should call in6_purgeaddr upon an error,
988 * not just go to unlink.
991 #if 0 /* disable this mechanism for now */
992 /* update prefix list */
994 (ifra
->ifra_flags
& IN6_IFF_NOPFX
) == 0) { /* XXX */
997 iilen
= (sizeof(ia
->ia_prefixmask
.sin6_addr
) << 3) - plen
;
998 if ((error
= in6_prefix_add_ifid(iilen
, ia
)) != 0) {
999 in6_purgeaddr((struct ifaddr
*)ia
);
1005 if (ifp
->if_flags
& IFF_MULTICAST
) {
1006 struct sockaddr_in6 mltaddr
, mltmask
;
1007 struct in6_multi
*in6m
;
1011 * join solicited multicast addr for new host id
1013 struct in6_addr llsol
;
1014 bzero(&llsol
, sizeof(struct in6_addr
));
1015 llsol
.s6_addr16
[0] = htons(0xff02);
1016 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1017 llsol
.s6_addr32
[1] = 0;
1018 llsol
.s6_addr32
[2] = htonl(1);
1019 llsol
.s6_addr32
[3] =
1020 ifra
->ifra_addr
.sin6_addr
.s6_addr32
[3];
1021 llsol
.s6_addr8
[12] = 0xff;
1022 in6_addmulti(&llsol
, ifp
, &error
);
1025 "in6_update_ifa: addmulti failed for "
1026 "%s on %s (errno=%d)\n",
1027 ip6_sprintf(&llsol
), if_name(ifp
),
1029 in6_purgeaddr((struct ifaddr
*)ia
);
1034 bzero(&mltmask
, sizeof(mltmask
));
1035 mltmask
.sin6_len
= sizeof(struct sockaddr_in6
);
1036 mltmask
.sin6_family
= AF_INET6
;
1037 mltmask
.sin6_addr
= in6mask32
;
1040 * join link-local all-nodes address
1042 bzero(&mltaddr
, sizeof(mltaddr
));
1043 mltaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
1044 mltaddr
.sin6_family
= AF_INET6
;
1045 mltaddr
.sin6_addr
= kin6addr_linklocal_allnodes
;
1046 mltaddr
.sin6_addr
.s6_addr16
[1] = htons(ifp
->if_index
);
1048 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1050 rtrequest_global(RTM_ADD
,
1051 (struct sockaddr
*)&mltaddr
,
1052 (struct sockaddr
*)&ia
->ia_addr
,
1053 (struct sockaddr
*)&mltmask
,
1054 RTF_UP
|RTF_CLONING
); /* xxx */
1055 in6_addmulti(&mltaddr
.sin6_addr
, ifp
, &error
);
1058 "in6_update_ifa: addmulti failed for "
1059 "%s on %s (errno=%d)\n",
1060 ip6_sprintf(&mltaddr
.sin6_addr
),
1061 if_name(ifp
), error
);
1066 * join node information group address
1068 #define hostnamelen strlen(hostname)
1069 if (in6_nigroup(ifp
, hostname
, hostnamelen
, &mltaddr
.sin6_addr
)
1071 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1072 if (in6m
== NULL
&& ia
!= NULL
) {
1073 in6_addmulti(&mltaddr
.sin6_addr
, ifp
, &error
);
1075 log(LOG_WARNING
, "in6_update_ifa: "
1076 "addmulti failed for "
1077 "%s on %s (errno=%d)\n",
1078 ip6_sprintf(&mltaddr
.sin6_addr
),
1079 if_name(ifp
), error
);
1086 * join node-local all-nodes address, on loopback.
1087 * XXX: since "node-local" is obsoleted by interface-local,
1088 * we have to join the group on every interface with
1089 * some interface-boundary restriction.
1091 if (ifp
->if_flags
& IFF_LOOPBACK
) {
1092 struct in6_ifaddr
*ia_loop
;
1094 struct in6_addr loop6
= kin6addr_loopback
;
1095 ia_loop
= in6ifa_ifpwithaddr(ifp
, &loop6
);
1097 mltaddr
.sin6_addr
= kin6addr_nodelocal_allnodes
;
1099 IN6_LOOKUP_MULTI(mltaddr
.sin6_addr
, ifp
, in6m
);
1100 if (in6m
== NULL
&& ia_loop
!= NULL
) {
1101 rtrequest_global(RTM_ADD
,
1102 (struct sockaddr
*)&mltaddr
,
1103 (struct sockaddr
*)&ia_loop
->ia_addr
,
1104 (struct sockaddr
*)&mltmask
,
1106 in6_addmulti(&mltaddr
.sin6_addr
, ifp
, &error
);
1108 log(LOG_WARNING
, "in6_update_ifa: "
1109 "addmulti failed for %s on %s "
1111 ip6_sprintf(&mltaddr
.sin6_addr
),
1112 if_name(ifp
), error
);
1118 ia
->ia6_flags
= ifra
->ifra_flags
;
1119 ia
->ia6_flags
&= ~IN6_IFF_DUPLICATED
; /*safety*/
1120 ia
->ia6_flags
&= ~IN6_IFF_NODAD
; /* Mobile IPv6 */
1122 ia
->ia6_lifetime
= ifra
->ifra_lifetime
;
1124 if (ia
->ia6_lifetime
.ia6t_vltime
!= ND6_INFINITE_LIFETIME
) {
1125 ia
->ia6_lifetime
.ia6t_expire
=
1126 time_second
+ ia
->ia6_lifetime
.ia6t_vltime
;
1128 ia
->ia6_lifetime
.ia6t_expire
= 0;
1129 if (ia
->ia6_lifetime
.ia6t_pltime
!= ND6_INFINITE_LIFETIME
) {
1130 ia
->ia6_lifetime
.ia6t_preferred
=
1131 time_second
+ ia
->ia6_lifetime
.ia6t_pltime
;
1133 ia
->ia6_lifetime
.ia6t_preferred
= 0;
1136 * Perform DAD, if needed.
1137 * XXX It may be of use, if we can administratively
1140 if (in6if_do_dad(ifp
) && !(ifra
->ifra_flags
& IN6_IFF_NODAD
)) {
1141 ia
->ia6_flags
|= IN6_IFF_TENTATIVE
;
1142 nd6_dad_start((struct ifaddr
*)ia
, NULL
);
1149 * XXX: if a change of an existing address failed, keep the entry
1153 in6_unlink_ifa(ia
, ifp
);
1158 in6_purgeaddr(struct ifaddr
*ifa
)
1160 struct ifnet
*ifp
= ifa
->ifa_ifp
;
1161 struct in6_ifaddr
*ia
= (struct in6_ifaddr
*) ifa
;
1163 /* stop DAD processing */
1167 * delete route to the destination of the address being purged.
1168 * The interface must be p2p or loopback in this case.
1170 if ((ia
->ia_flags
& IFA_ROUTE
) && ia
->ia_dstaddr
.sin6_len
!= 0) {
1173 if ((e
= rtinit(&(ia
->ia_ifa
), (int)RTM_DELETE
, RTF_HOST
))
1175 log(LOG_ERR
, "in6_purgeaddr: failed to remove "
1176 "a route to the p2p destination: %s on %s, "
1178 ip6_sprintf(&ia
->ia_addr
.sin6_addr
), if_name(ifp
),
1180 /* proceed anyway... */
1183 ia
->ia_flags
&= ~IFA_ROUTE
;
1186 /* Remove ownaddr's loopback rtentry, if it exists. */
1187 in6_ifremloop(&(ia
->ia_ifa
));
1189 if (ifp
->if_flags
& IFF_MULTICAST
) {
1191 * delete solicited multicast addr for deleting host id
1193 struct in6_multi
*in6m
;
1194 struct in6_addr llsol
;
1195 bzero(&llsol
, sizeof(struct in6_addr
));
1196 llsol
.s6_addr16
[0] = htons(0xff02);
1197 llsol
.s6_addr16
[1] = htons(ifp
->if_index
);
1198 llsol
.s6_addr32
[1] = 0;
1199 llsol
.s6_addr32
[2] = htonl(1);
1200 llsol
.s6_addr32
[3] =
1201 ia
->ia_addr
.sin6_addr
.s6_addr32
[3];
1202 llsol
.s6_addr8
[12] = 0xff;
1204 IN6_LOOKUP_MULTI(llsol
, ifp
, in6m
);
1209 in6_unlink_ifa(ia
, ifp
);
1213 in6_unlink_ifa(struct in6_ifaddr
*ia
, struct ifnet
*ifp
)
1216 struct in6_ifaddr
*oia
;
1220 TAILQ_REMOVE(&ifp
->if_addrlist
, &ia
->ia_ifa
, ifa_list
);
1223 if (oia
== (ia
= in6_ifaddr
))
1224 in6_ifaddr
= ia
->ia_next
;
1226 while (ia
->ia_next
&& (ia
->ia_next
!= oia
))
1229 ia
->ia_next
= oia
->ia_next
;
1232 kprintf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1236 if (oia
->ia6_ifpr
) { /* check for safety */
1237 plen
= in6_mask2len(&oia
->ia_prefixmask
.sin6_addr
, NULL
);
1238 iilen
= (sizeof(oia
->ia_prefixmask
.sin6_addr
) << 3) - plen
;
1239 in6_prefix_remove_ifid(iilen
, oia
);
1243 * When an autoconfigured address is being removed, release the
1244 * reference to the base prefix. Also, since the release might
1245 * affect the status of other (detached) addresses, call
1246 * pfxlist_onlink_check().
1248 if (oia
->ia6_flags
& IN6_IFF_AUTOCONF
) {
1249 if (oia
->ia6_ndpr
== NULL
) {
1250 log(LOG_NOTICE
, "in6_unlink_ifa: autoconf'ed address "
1251 "%p has no prefix\n", oia
);
1253 oia
->ia6_ndpr
->ndpr_refcnt
--;
1254 oia
->ia6_flags
&= ~IN6_IFF_AUTOCONF
;
1255 oia
->ia6_ndpr
= NULL
;
1258 pfxlist_onlink_check();
1262 * release another refcnt for the link from in6_ifaddr.
1263 * Note that we should decrement the refcnt at least once for all *BSD.
1265 IFAFREE(&oia
->ia_ifa
);
1271 in6_purgeif(struct ifnet
*ifp
)
1273 struct ifaddr
*ifa
, *nifa
;
1275 for (ifa
= TAILQ_FIRST(&ifp
->if_addrlist
); ifa
!= NULL
; ifa
= nifa
)
1277 nifa
= TAILQ_NEXT(ifa
, ifa_list
);
1278 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1288 * SIOCGLIFADDR: get first address. (?)
1289 * SIOCGLIFADDR with IFLR_PREFIX:
1290 * get first address that matches the specified prefix.
1291 * SIOCALIFADDR: add the specified address.
1292 * SIOCALIFADDR with IFLR_PREFIX:
1293 * add the specified prefix, filling hostid part from
1294 * the first link-local address. prefixlen must be <= 64.
1295 * SIOCDLIFADDR: delete the specified address.
1296 * SIOCDLIFADDR with IFLR_PREFIX:
1297 * delete the first address that matches the specified prefix.
1299 * EINVAL on invalid parameters
1300 * EADDRNOTAVAIL on prefix match failed/specified address not found
1301 * other values may be returned from in6_ioctl()
1303 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1304 * this is to accomodate address naming scheme other than RFC2374,
1306 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1307 * address encoding scheme. (see figure on page 8)
1310 in6_lifaddr_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
1311 struct ifnet
*ifp
, struct thread
*td
)
1313 struct if_laddrreq
*iflr
= (struct if_laddrreq
*)data
;
1315 struct sockaddr
*sa
;
1318 if (!data
|| !ifp
) {
1319 panic("invalid argument to in6_lifaddr_ioctl");
1325 /* address must be specified on GET with IFLR_PREFIX */
1326 if (!(iflr
->flags
& IFLR_PREFIX
))
1331 /* address must be specified on ADD and DELETE */
1332 sa
= (struct sockaddr
*)&iflr
->addr
;
1333 if (sa
->sa_family
!= AF_INET6
)
1335 if (sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1337 /* XXX need improvement */
1338 sa
= (struct sockaddr
*)&iflr
->dstaddr
;
1339 if (sa
->sa_family
&& sa
->sa_family
!= AF_INET6
)
1341 if (sa
->sa_len
&& sa
->sa_len
!= sizeof(struct sockaddr_in6
))
1344 default: /* shouldn't happen */
1346 panic("invalid cmd to in6_lifaddr_ioctl");
1352 if (sizeof(struct in6_addr
) * 8 < iflr
->prefixlen
)
1358 struct in6_aliasreq ifra
;
1359 struct in6_addr
*hostid
= NULL
;
1362 if (iflr
->flags
& IFLR_PREFIX
) {
1363 struct sockaddr_in6
*sin6
;
1366 * hostid is to fill in the hostid part of the
1367 * address. hostid points to the first link-local
1368 * address attached to the interface.
1370 ifa
= (struct ifaddr
*)in6ifa_ifpforlinklocal(ifp
, 0);
1372 return EADDRNOTAVAIL
;
1373 hostid
= IFA_IN6(ifa
);
1375 /* prefixlen must be <= 64. */
1376 if (64 < iflr
->prefixlen
)
1378 prefixlen
= iflr
->prefixlen
;
1380 /* hostid part must be zero. */
1381 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1382 if (sin6
->sin6_addr
.s6_addr32
[2] != 0
1383 || sin6
->sin6_addr
.s6_addr32
[3] != 0) {
1387 prefixlen
= iflr
->prefixlen
;
1389 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1390 bzero(&ifra
, sizeof(ifra
));
1391 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
1392 sizeof(ifra
.ifra_name
));
1394 bcopy(&iflr
->addr
, &ifra
.ifra_addr
,
1395 ((struct sockaddr
*)&iflr
->addr
)->sa_len
);
1397 /* fill in hostid part */
1398 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[2] =
1399 hostid
->s6_addr32
[2];
1400 ifra
.ifra_addr
.sin6_addr
.s6_addr32
[3] =
1401 hostid
->s6_addr32
[3];
1404 if (((struct sockaddr
*)&iflr
->dstaddr
)->sa_family
) { /*XXX*/
1405 bcopy(&iflr
->dstaddr
, &ifra
.ifra_dstaddr
,
1406 ((struct sockaddr
*)&iflr
->dstaddr
)->sa_len
);
1408 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[2] =
1409 hostid
->s6_addr32
[2];
1410 ifra
.ifra_dstaddr
.sin6_addr
.s6_addr32
[3] =
1411 hostid
->s6_addr32
[3];
1415 ifra
.ifra_prefixmask
.sin6_len
= sizeof(struct sockaddr_in6
);
1416 in6_len2mask(&ifra
.ifra_prefixmask
.sin6_addr
, prefixlen
);
1418 ifra
.ifra_flags
= iflr
->flags
& ~IFLR_PREFIX
;
1419 return in6_control(so
, SIOCAIFADDR_IN6
, (caddr_t
)&ifra
, ifp
, td
);
1424 struct in6_ifaddr
*ia
;
1425 struct in6_addr mask
, candidate
, match
;
1426 struct sockaddr_in6
*sin6
;
1429 bzero(&mask
, sizeof(mask
));
1430 if (iflr
->flags
& IFLR_PREFIX
) {
1431 /* lookup a prefix rather than address. */
1432 in6_len2mask(&mask
, iflr
->prefixlen
);
1434 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1435 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
1436 match
.s6_addr32
[0] &= mask
.s6_addr32
[0];
1437 match
.s6_addr32
[1] &= mask
.s6_addr32
[1];
1438 match
.s6_addr32
[2] &= mask
.s6_addr32
[2];
1439 match
.s6_addr32
[3] &= mask
.s6_addr32
[3];
1441 /* if you set extra bits, that's wrong */
1442 if (bcmp(&match
, &sin6
->sin6_addr
, sizeof(match
)))
1447 if (cmd
== SIOCGLIFADDR
) {
1448 /* on getting an address, take the 1st match */
1451 /* on deleting an address, do exact match */
1452 in6_len2mask(&mask
, 128);
1453 sin6
= (struct sockaddr_in6
*)&iflr
->addr
;
1454 bcopy(&sin6
->sin6_addr
, &match
, sizeof(match
));
1460 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1462 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1467 bcopy(IFA_IN6(ifa
), &candidate
, sizeof(candidate
));
1469 * XXX: this is adhoc, but is necessary to allow
1470 * a user to specify fe80::/64 (not /10) for a
1471 * link-local address.
1473 if (IN6_IS_ADDR_LINKLOCAL(&candidate
))
1474 candidate
.s6_addr16
[1] = 0;
1475 candidate
.s6_addr32
[0] &= mask
.s6_addr32
[0];
1476 candidate
.s6_addr32
[1] &= mask
.s6_addr32
[1];
1477 candidate
.s6_addr32
[2] &= mask
.s6_addr32
[2];
1478 candidate
.s6_addr32
[3] &= mask
.s6_addr32
[3];
1479 if (IN6_ARE_ADDR_EQUAL(&candidate
, &match
))
1483 return EADDRNOTAVAIL
;
1486 if (cmd
== SIOCGLIFADDR
) {
1487 struct sockaddr_in6
*s6
;
1489 /* fill in the if_laddrreq structure */
1490 bcopy(&ia
->ia_addr
, &iflr
->addr
, ia
->ia_addr
.sin6_len
);
1491 s6
= (struct sockaddr_in6
*)&iflr
->addr
;
1492 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
1493 s6
->sin6_addr
.s6_addr16
[1] = 0;
1495 in6_addr2scopeid(ifp
, &s6
->sin6_addr
);
1497 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1498 bcopy(&ia
->ia_dstaddr
, &iflr
->dstaddr
,
1499 ia
->ia_dstaddr
.sin6_len
);
1500 s6
= (struct sockaddr_in6
*)&iflr
->dstaddr
;
1501 if (IN6_IS_ADDR_LINKLOCAL(&s6
->sin6_addr
)) {
1502 s6
->sin6_addr
.s6_addr16
[1] = 0;
1504 in6_addr2scopeid(ifp
,
1508 bzero(&iflr
->dstaddr
, sizeof(iflr
->dstaddr
));
1511 in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
,
1514 iflr
->flags
= ia
->ia6_flags
; /* XXX */
1518 struct in6_aliasreq ifra
;
1520 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1521 bzero(&ifra
, sizeof(ifra
));
1522 bcopy(iflr
->iflr_name
, ifra
.ifra_name
,
1523 sizeof(ifra
.ifra_name
));
1525 bcopy(&ia
->ia_addr
, &ifra
.ifra_addr
,
1526 ia
->ia_addr
.sin6_len
);
1527 if (ifp
->if_flags
& IFF_POINTOPOINT
)
1528 bcopy(&ia
->ia_dstaddr
, &ifra
.ifra_dstaddr
,
1529 ia
->ia_dstaddr
.sin6_len
);
1531 bzero(&ifra
.ifra_dstaddr
,
1532 sizeof(ifra
.ifra_dstaddr
));
1533 bcopy(&ia
->ia_prefixmask
, &ifra
.ifra_dstaddr
,
1534 ia
->ia_prefixmask
.sin6_len
);
1536 ifra
.ifra_flags
= ia
->ia6_flags
;
1537 return in6_control(so
, SIOCDIFADDR_IN6
, (caddr_t
)&ifra
,
1543 return EOPNOTSUPP
; /* just for safety */
1547 * Initialize an interface's intetnet6 address
1548 * and routing table entry.
1551 in6_ifinit(struct ifnet
*ifp
, struct in6_ifaddr
*ia
, struct sockaddr_in6
*sin6
,
1554 int error
= 0, plen
, ifacount
= 0;
1557 lwkt_serialize_enter(ifp
->if_serializer
);
1560 * Give the interface a chance to initialize
1561 * if this is its first address,
1562 * and to validate the address if necessary.
1564 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1566 if (ifa
->ifa_addr
== NULL
)
1567 continue; /* just for safety */
1568 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1573 ia
->ia_addr
= *sin6
;
1575 if (ifacount
<= 1 && ifp
->if_ioctl
&&
1576 (error
= ifp
->if_ioctl(ifp
, SIOCSIFADDR
, (caddr_t
)ia
,
1577 (struct ucred
*)NULL
))) {
1578 lwkt_serialize_exit(ifp
->if_serializer
);
1581 lwkt_serialize_exit(ifp
->if_serializer
);
1583 ia
->ia_ifa
.ifa_metric
= ifp
->if_metric
;
1585 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1589 * If the destination address is specified for a point-to-point
1590 * interface, install a route to the destination as an interface
1593 plen
= in6_mask2len(&ia
->ia_prefixmask
.sin6_addr
, NULL
); /* XXX */
1594 if (plen
== 128 && ia
->ia_dstaddr
.sin6_family
== AF_INET6
) {
1595 if ((error
= rtinit(&(ia
->ia_ifa
), (int)RTM_ADD
,
1596 RTF_UP
| RTF_HOST
)) != 0)
1598 ia
->ia_flags
|= IFA_ROUTE
;
1602 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1604 ia
->ia_ifa
.ifa_flags
|= RTF_CLONING
;
1607 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1609 /* set the rtrequest function to create llinfo */
1610 ia
->ia_ifa
.ifa_rtrequest
= nd6_rtrequest
;
1611 in6_ifaddloop(&(ia
->ia_ifa
));
1618 * Add an address to the list of IP6 multicast addresses for a
1622 in6_addmulti(struct in6_addr
*maddr6
, struct ifnet
*ifp
, int *errorp
)
1624 struct in6_multi
*in6m
;
1625 struct sockaddr_in6 sin6
;
1626 struct ifmultiaddr
*ifma
;
1633 * Call generic routine to add membership or increment
1634 * refcount. It wants addresses in the form of a sockaddr,
1635 * so we build one here (being careful to zero the unused bytes).
1637 bzero(&sin6
, sizeof sin6
);
1638 sin6
.sin6_family
= AF_INET6
;
1639 sin6
.sin6_len
= sizeof sin6
;
1640 sin6
.sin6_addr
= *maddr6
;
1641 *errorp
= if_addmulti(ifp
, (struct sockaddr
*)&sin6
, &ifma
);
1648 * If ifma->ifma_protospec is null, then if_addmulti() created
1649 * a new record. Otherwise, we are done.
1651 if (ifma
->ifma_protospec
!= 0) {
1653 return ifma
->ifma_protospec
;
1656 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1657 at interrupt time? If so, need to fix if_addmulti. XXX */
1658 in6m
= (struct in6_multi
*)kmalloc(sizeof(*in6m
), M_IPMADDR
, M_NOWAIT
);
1664 bzero(in6m
, sizeof *in6m
);
1665 in6m
->in6m_addr
= *maddr6
;
1666 in6m
->in6m_ifp
= ifp
;
1667 in6m
->in6m_ifma
= ifma
;
1668 ifma
->ifma_protospec
= in6m
;
1669 LIST_INSERT_HEAD(&in6_multihead
, in6m
, in6m_entry
);
1672 * Let MLD6 know that we have joined a new IP6 multicast
1675 mld6_start_listening(in6m
);
1681 * Delete a multicast address record.
1684 in6_delmulti(struct in6_multi
*in6m
)
1686 struct ifmultiaddr
*ifma
= in6m
->in6m_ifma
;
1690 if (ifma
->ifma_refcount
== 1) {
1692 * No remaining claims to this record; let MLD6 know
1693 * that we are leaving the multicast group.
1695 mld6_stop_listening(in6m
);
1696 ifma
->ifma_protospec
= 0;
1697 LIST_REMOVE(in6m
, in6m_entry
);
1698 kfree(in6m
, M_IPMADDR
);
1700 /* XXX - should be separate API for when we have an ifma? */
1701 if_delmulti(ifma
->ifma_ifp
, ifma
->ifma_addr
);
1706 * Find an IPv6 interface link-local address specific to an interface.
1709 in6ifa_ifpforlinklocal(struct ifnet
*ifp
, int ignoreflags
)
1713 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1715 if (ifa
->ifa_addr
== NULL
)
1716 continue; /* just for safety */
1717 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1719 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa
))) {
1720 if ((((struct in6_ifaddr
*)ifa
)->ia6_flags
&
1727 return ((struct in6_ifaddr
*)ifa
);
1732 * find the internet address corresponding to a given interface and address.
1735 in6ifa_ifpwithaddr(struct ifnet
*ifp
, struct in6_addr
*addr
)
1739 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1741 if (ifa
->ifa_addr
== NULL
)
1742 continue; /* just for safety */
1743 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1745 if (IN6_ARE_ADDR_EQUAL(addr
, IFA_IN6(ifa
)))
1749 return ((struct in6_ifaddr
*)ifa
);
1753 * Convert IP6 address to printable (loggable) representation.
1755 static char digits
[] = "0123456789abcdef";
1756 static int ip6round
= 0;
1758 ip6_sprintf(const struct in6_addr
*addr
)
1760 static char ip6buf
[8][48];
1763 const u_short
*a
= (const u_short
*)addr
;
1767 ip6round
= (ip6round
+ 1) & 7;
1768 cp
= ip6buf
[ip6round
];
1770 for (i
= 0; i
< 8; i
++) {
1781 if (dcolon
== 0 && *(a
+ 1) == 0) {
1793 d
= (const u_char
*)a
;
1794 *cp
++ = digits
[*d
>> 4];
1795 *cp
++ = digits
[*d
++ & 0xf];
1796 *cp
++ = digits
[*d
>> 4];
1797 *cp
++ = digits
[*d
& 0xf];
1802 return (ip6buf
[ip6round
]);
1806 in6_localaddr(struct in6_addr
*in6
)
1808 struct in6_ifaddr
*ia
;
1810 if (IN6_IS_ADDR_LOOPBACK(in6
) || IN6_IS_ADDR_LINKLOCAL(in6
))
1813 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
)
1814 if (IN6_ARE_MASKED_ADDR_EQUAL(in6
, &ia
->ia_addr
.sin6_addr
,
1815 &ia
->ia_prefixmask
.sin6_addr
))
1822 in6_is_addr_deprecated(struct sockaddr_in6
*sa6
)
1824 struct in6_ifaddr
*ia
;
1826 for (ia
= in6_ifaddr
; ia
; ia
= ia
->ia_next
) {
1827 if (IN6_ARE_ADDR_EQUAL(&ia
->ia_addr
.sin6_addr
,
1829 (ia
->ia6_flags
& IN6_IFF_DEPRECATED
))
1830 return (1); /* true */
1832 /* XXX: do we still have to go thru the rest of the list? */
1835 return (0); /* false */
1839 * return length of part which dst and src are equal
1843 in6_matchlen(struct in6_addr
*src
, struct in6_addr
*dst
)
1846 u_char
*s
= (u_char
*)src
, *d
= (u_char
*)dst
;
1847 u_char
*lim
= s
+ 16, r
;
1850 if ((r
= (*d
++ ^ *s
++)) != 0) {
1861 /* XXX: to be scope conscious */
1863 in6_are_prefix_equal(struct in6_addr
*p1
, struct in6_addr
*p2
, int len
)
1865 int bytelen
, bitlen
;
1868 if (0 > len
|| len
> 128) {
1869 log(LOG_ERR
, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1877 if (bcmp(&p1
->s6_addr
, &p2
->s6_addr
, bytelen
))
1879 if (p1
->s6_addr
[bytelen
] >> (8 - bitlen
) !=
1880 p2
->s6_addr
[bytelen
] >> (8 - bitlen
))
1887 in6_prefixlen2mask(struct in6_addr
*maskp
, int len
)
1889 u_char maskarray
[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1890 int bytelen
, bitlen
, i
;
1893 if (0 > len
|| len
> 128) {
1894 log(LOG_ERR
, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1899 bzero(maskp
, sizeof(*maskp
));
1902 for (i
= 0; i
< bytelen
; i
++)
1903 maskp
->s6_addr
[i
] = 0xff;
1905 maskp
->s6_addr
[bytelen
] = maskarray
[bitlen
- 1];
1909 * return the best address out of the same scope
1912 in6_ifawithscope(struct ifnet
*oifp
, struct in6_addr
*dst
)
1914 int dst_scope
= in6_addrscope(dst
), src_scope
, best_scope
= 0;
1918 struct in6_ifaddr
*ifa_best
= NULL
;
1922 kprintf("in6_ifawithscope: output interface is not specified\n");
1928 * We search for all addresses on all interfaces from the beginning.
1929 * Comparing an interface with the outgoing interface will be done
1930 * only at the final stage of tiebreaking.
1932 for (ifp
= TAILQ_FIRST(&ifnet
); ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
))
1935 * We can never take an address that breaks the scope zone
1936 * of the destination.
1938 if (in6_addr2scopeid(ifp
, dst
) != in6_addr2scopeid(oifp
, dst
))
1941 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
1943 int tlen
= -1, dscopecmp
, bscopecmp
, matchcmp
;
1945 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
1948 src_scope
= in6_addrscope(IFA_IN6(ifa
));
1951 * Don't use an address before completing DAD
1952 * nor a duplicated address.
1954 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
1958 /* XXX: is there any case to allow anycasts? */
1959 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
1963 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
1968 * If this is the first address we find,
1971 if (ifa_best
== NULL
)
1975 * ifa_best is never NULL beyond this line except
1976 * within the block labeled "replace".
1980 * If ifa_best has a smaller scope than dst and
1981 * the current address has a larger one than
1982 * (or equal to) dst, always replace ifa_best.
1983 * Also, if the current address has a smaller scope
1984 * than dst, ignore it unless ifa_best also has a
1986 * Consequently, after the two if-clause below,
1987 * the followings must be satisfied:
1988 * (scope(src) < scope(dst) &&
1989 * scope(best) < scope(dst))
1991 * (scope(best) >= scope(dst) &&
1992 * scope(src) >= scope(dst))
1994 if (IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) < 0 &&
1995 IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) >= 0)
1996 goto replace
; /* (A) */
1997 if (IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
) < 0 &&
1998 IN6_ARE_SCOPE_CMP(best_scope
, dst_scope
) >= 0)
2002 * A deprecated address SHOULD NOT be used in new
2003 * communications if an alternate (non-deprecated)
2004 * address is available and has sufficient scope.
2005 * RFC 2462, Section 5.5.4.
2007 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2008 IN6_IFF_DEPRECATED
) {
2010 * Ignore any deprecated addresses if
2011 * specified by configuration.
2013 if (!ip6_use_deprecated
)
2017 * If we have already found a non-deprecated
2018 * candidate, just ignore deprecated addresses.
2020 if (!(ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
))
2025 * A non-deprecated address is always preferred
2026 * to a deprecated one regardless of scopes and
2027 * address matching (Note invariants ensured by the
2028 * conditions (A) and (B) above.)
2030 if ((ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
) &&
2031 !(((struct in6_ifaddr
*)ifa
)->ia6_flags
&
2032 IN6_IFF_DEPRECATED
))
2036 * When we use temporary addresses described in
2037 * RFC 3041, we prefer temporary addresses to
2038 * public autoconf addresses. Again, note the
2039 * invariants from (A) and (B). Also note that we
2040 * don't have any preference between static addresses
2041 * and autoconf addresses (despite of whether or not
2042 * the latter is temporary or public.)
2044 if (ip6_use_tempaddr
) {
2045 struct in6_ifaddr
*ifat
;
2047 ifat
= (struct in6_ifaddr
*)ifa
;
2048 if ((ifa_best
->ia6_flags
&
2049 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2050 == IN6_IFF_AUTOCONF
&&
2052 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2053 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
)) {
2056 if ((ifa_best
->ia6_flags
&
2057 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2058 == (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
) &&
2060 (IN6_IFF_AUTOCONF
|IN6_IFF_TEMPORARY
))
2061 == IN6_IFF_AUTOCONF
) {
2067 * At this point, we have two cases:
2068 * 1. we are looking at a non-deprecated address,
2069 * and ifa_best is also non-deprecated.
2070 * 2. we are looking at a deprecated address,
2071 * and ifa_best is also deprecated.
2072 * Also, we do not have to consider a case where
2073 * the scope of if_best is larger(smaller) than dst and
2074 * the scope of the current address is smaller(larger)
2075 * than dst. Such a case has already been covered.
2076 * Tiebreaking is done according to the following
2078 * - the scope comparison between the address and
2080 * - the scope comparison between the address and
2081 * ifa_best (bscopecmp)
2082 * - if the address match dst longer than ifa_best
2084 * - if the address is on the outgoing I/F (outI/F)
2086 * Roughly speaking, the selection policy is
2087 * - the most important item is scope. The same scope
2088 * is best. Then search for a larger scope.
2089 * Smaller scopes are the last resort.
2090 * - A deprecated address is chosen only when we have
2091 * no address that has an enough scope, but is
2092 * prefered to any addresses of smaller scopes
2093 * (this must be already done above.)
2094 * - addresses on the outgoing I/F are preferred to
2095 * ones on other interfaces if none of above
2096 * tiebreaks. In the table below, the column "bI"
2097 * means if the best_ifa is on the outgoing
2098 * interface, and the column "sI" means if the ifa
2099 * is on the outgoing interface.
2100 * - If there is no other reasons to choose one,
2101 * longest address match against dst is considered.
2103 * The precise decision table is as follows:
2104 * dscopecmp bscopecmp match bI oI | replace?
2105 * N/A equal N/A Y N | No (1)
2106 * N/A equal N/A N Y | Yes (2)
2107 * N/A equal larger N/A | Yes (3)
2108 * N/A equal !larger N/A | No (4)
2109 * larger larger N/A N/A | No (5)
2110 * larger smaller N/A N/A | Yes (6)
2111 * smaller larger N/A N/A | Yes (7)
2112 * smaller smaller N/A N/A | No (8)
2113 * equal smaller N/A N/A | Yes (9)
2114 * equal larger (already done at A above)
2116 dscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, dst_scope
);
2117 bscopecmp
= IN6_ARE_SCOPE_CMP(src_scope
, best_scope
);
2119 if (bscopecmp
== 0) {
2120 struct ifnet
*bifp
= ifa_best
->ia_ifp
;
2122 if (bifp
== oifp
&& ifp
!= oifp
) /* (1) */
2124 if (bifp
!= oifp
&& ifp
== oifp
) /* (2) */
2128 * Both bifp and ifp are on the outgoing
2129 * interface, or both two are on a different
2130 * interface from the outgoing I/F.
2131 * now we need address matching against dst
2134 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
2135 matchcmp
= tlen
- blen
;
2136 if (matchcmp
> 0) /* (3) */
2140 if (dscopecmp
> 0) {
2141 if (bscopecmp
> 0) /* (5) */
2143 goto replace
; /* (6) */
2145 if (dscopecmp
< 0) {
2146 if (bscopecmp
> 0) /* (7) */
2151 /* now dscopecmp must be 0 */
2153 goto replace
; /* (9) */
2156 ifa_best
= (struct in6_ifaddr
*)ifa
;
2157 blen
= tlen
>= 0 ? tlen
:
2158 in6_matchlen(IFA_IN6(ifa
), dst
);
2159 best_scope
= in6_addrscope(&ifa_best
->ia_addr
.sin6_addr
);
2163 /* count statistics for future improvements */
2164 if (ifa_best
== NULL
)
2165 ip6stat
.ip6s_sources_none
++;
2167 if (oifp
== ifa_best
->ia_ifp
)
2168 ip6stat
.ip6s_sources_sameif
[best_scope
]++;
2170 ip6stat
.ip6s_sources_otherif
[best_scope
]++;
2172 if (best_scope
== dst_scope
)
2173 ip6stat
.ip6s_sources_samescope
[best_scope
]++;
2175 ip6stat
.ip6s_sources_otherscope
[best_scope
]++;
2177 if (ifa_best
->ia6_flags
& IN6_IFF_DEPRECATED
)
2178 ip6stat
.ip6s_sources_deprecated
[best_scope
]++;
2185 * return the best address out of the same scope. if no address was
2186 * found, return the first valid address from designated IF.
2189 in6_ifawithifp(struct ifnet
*ifp
, struct in6_addr
*dst
)
2191 int dst_scope
= in6_addrscope(dst
), blen
= -1, tlen
;
2193 struct in6_ifaddr
*besta
= 0;
2194 struct in6_ifaddr
*dep
[2]; /* last-resort: deprecated */
2196 dep
[0] = dep
[1] = NULL
;
2199 * We first look for addresses in the same scope.
2200 * If there is one, return it.
2201 * If two or more, return one which matches the dst longest.
2202 * If none, return one of global addresses assigned other ifs.
2204 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2206 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2208 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
)
2209 continue; /* XXX: is there any case to allow anycast? */
2210 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_NOTREADY
)
2211 continue; /* don't use this interface */
2212 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DETACHED
)
2214 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
2215 if (ip6_use_deprecated
)
2216 dep
[0] = (struct in6_ifaddr
*)ifa
;
2220 if (dst_scope
== in6_addrscope(IFA_IN6(ifa
))) {
2222 * call in6_matchlen() as few as possible
2226 blen
= in6_matchlen(&besta
->ia_addr
.sin6_addr
, dst
);
2227 tlen
= in6_matchlen(IFA_IN6(ifa
), dst
);
2230 besta
= (struct in6_ifaddr
*)ifa
;
2233 besta
= (struct in6_ifaddr
*)ifa
;
2239 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2241 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2243 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_ANYCAST
)
2244 continue; /* XXX: is there any case to allow anycast? */
2245 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_NOTREADY
)
2246 continue; /* don't use this interface */
2247 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DETACHED
)
2249 if (((struct in6_ifaddr
*)ifa
)->ia6_flags
& IN6_IFF_DEPRECATED
) {
2250 if (ip6_use_deprecated
)
2251 dep
[1] = (struct in6_ifaddr
*)ifa
;
2255 return (struct in6_ifaddr
*)ifa
;
2258 /* use the last-resort values, that are, deprecated addresses */
2268 * perform DAD when interface becomes IFF_UP.
2271 in6_if_up(struct ifnet
*ifp
)
2274 struct in6_ifaddr
*ia
;
2275 int dad_delay
; /* delay ticks before DAD output */
2278 * special cases, like 6to4, are handled in in6_ifattach
2280 in6_ifattach(ifp
, NULL
);
2283 TAILQ_FOREACH(ifa
, &ifp
->if_addrlist
, ifa_list
)
2285 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
2287 ia
= (struct in6_ifaddr
*)ifa
;
2288 if (ia
->ia6_flags
& IN6_IFF_TENTATIVE
)
2289 nd6_dad_start(ifa
, &dad_delay
);
2294 in6if_do_dad(struct ifnet
*ifp
)
2296 if (ifp
->if_flags
& IFF_LOOPBACK
)
2299 switch (ifp
->if_type
) {
2305 * These interfaces do not have the IFF_LOOPBACK flag,
2306 * but loop packets back. We do not have to do DAD on such
2307 * interfaces. We should even omit it, because loop-backed
2308 * NS would confuse the DAD procedure.
2313 * Our DAD routine requires the interface up and running.
2314 * However, some interfaces can be up before the RUNNING
2315 * status. Additionaly, users may try to assign addresses
2316 * before the interface becomes up (or running).
2317 * We simply skip DAD in such a case as a work around.
2318 * XXX: we should rather mark "tentative" on such addresses,
2319 * and do DAD after the interface becomes ready.
2321 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) !=
2322 (IFF_UP
|IFF_RUNNING
))
2330 * Calculate max IPv6 MTU through all the interfaces and store it
2336 unsigned long maxmtu
= 0;
2339 for (ifp
= TAILQ_FIRST(&ifnet
); ifp
; ifp
= TAILQ_NEXT(ifp
, if_list
))
2341 if (!(ifp
->if_flags
& IFF_LOOPBACK
) &&
2342 ND_IFINFO(ifp
)->linkmtu
> maxmtu
)
2343 maxmtu
= ND_IFINFO(ifp
)->linkmtu
;
2345 if (maxmtu
) /* update only when maxmtu is positive */
2346 in6_maxmtu
= maxmtu
;
2350 in6_domifattach(struct ifnet
*ifp
)
2352 struct in6_ifextra
*ext
;
2354 ext
= (struct in6_ifextra
*)kmalloc(sizeof(*ext
), M_IFADDR
, M_WAITOK
);
2355 bzero(ext
, sizeof(*ext
));
2357 ext
->in6_ifstat
= (struct in6_ifstat
*)kmalloc(sizeof(struct in6_ifstat
),
2358 M_IFADDR
, M_WAITOK
);
2359 bzero(ext
->in6_ifstat
, sizeof(*ext
->in6_ifstat
));
2362 (struct icmp6_ifstat
*)kmalloc(sizeof(struct icmp6_ifstat
),
2363 M_IFADDR
, M_WAITOK
);
2364 bzero(ext
->icmp6_ifstat
, sizeof(*ext
->icmp6_ifstat
));
2366 ext
->nd_ifinfo
= nd6_ifattach(ifp
);
2367 ext
->scope6_id
= scope6_ifattach(ifp
);
2372 in6_domifdetach(struct ifnet
*ifp
, void *aux
)
2374 struct in6_ifextra
*ext
= (struct in6_ifextra
*)aux
;
2375 scope6_ifdetach(ext
->scope6_id
);
2376 nd6_ifdetach(ext
->nd_ifinfo
);
2377 kfree(ext
->in6_ifstat
, M_IFADDR
);
2378 kfree(ext
->icmp6_ifstat
, M_IFADDR
);
2379 kfree(ext
, M_IFADDR
);
2383 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2384 * v4 mapped addr or v4 compat addr
2387 in6_sin6_2_sin(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
2389 bzero(sin
, sizeof(*sin
));
2390 sin
->sin_len
= sizeof(struct sockaddr_in
);
2391 sin
->sin_family
= AF_INET
;
2392 sin
->sin_port
= sin6
->sin6_port
;
2393 sin
->sin_addr
.s_addr
= sin6
->sin6_addr
.s6_addr32
[3];
2396 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2398 in6_sin_2_v4mapsin6(struct sockaddr_in
*sin
, struct sockaddr_in6
*sin6
)
2400 bzero(sin6
, sizeof(*sin6
));
2401 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2402 sin6
->sin6_family
= AF_INET6
;
2403 sin6
->sin6_port
= sin
->sin_port
;
2404 sin6
->sin6_addr
.s6_addr32
[0] = 0;
2405 sin6
->sin6_addr
.s6_addr32
[1] = 0;
2406 sin6
->sin6_addr
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
2407 sin6
->sin6_addr
.s6_addr32
[3] = sin
->sin_addr
.s_addr
;
2410 /* Convert sockaddr_in6 into sockaddr_in. */
2412 in6_sin6_2_sin_in_sock(struct sockaddr
*nam
)
2414 struct sockaddr_in
*sin_p
;
2415 struct sockaddr_in6 sin6
;
2418 * Save original sockaddr_in6 addr and convert it
2421 sin6
= *(struct sockaddr_in6
*)nam
;
2422 sin_p
= (struct sockaddr_in
*)nam
;
2423 in6_sin6_2_sin(sin_p
, &sin6
);
2426 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2428 in6_sin_2_v4mapsin6_in_sock(struct sockaddr
**nam
)
2430 struct sockaddr_in
*sin_p
;
2431 struct sockaddr_in6
*sin6_p
;
2433 MALLOC(sin6_p
, struct sockaddr_in6
*, sizeof *sin6_p
, M_SONAME
,
2435 sin_p
= (struct sockaddr_in
*)*nam
;
2436 in6_sin_2_v4mapsin6(sin_p
, sin6_p
);
2437 FREE(*nam
, M_SONAME
);
2438 *nam
= (struct sockaddr
*)sin6_p
;