1 /* $FreeBSD: src/sys/netinet6/in6.c,v 1.7.2.9 2002/04/28 05:40:26 suz Exp $ */
2 /* $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi 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. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)in.c 8.2 (Berkeley) 11/15/93
65 #include "opt_inet6.h"
67 #include <sys/param.h>
68 #include <sys/errno.h>
69 #include <sys/malloc.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/sockio.h>
73 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/syslog.h>
80 #include <sys/thread2.h>
81 #include <sys/msgport2.h>
84 #include <net/if_types.h>
85 #include <net/route.h>
86 #include <net/if_dl.h>
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/if_ether.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/in_pcb.h>
95 #include <netinet/ip6.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet6/nd6.h>
98 #include <netinet6/mld6_var.h>
99 #include <netinet6/ip6_mroute.h>
100 #include <netinet6/in6_ifattach.h>
101 #include <netinet6/scope6_var.h>
102 #include <netinet6/in6_pcb.h>
103 #include <netinet6/in6_var.h>
105 #include <net/net_osdep.h>
108 * Definitions of some costant IP6 addresses.
110 const struct in6_addr kin6addr_any = IN6ADDR_ANY_INIT;
111 const struct in6_addr kin6addr_loopback = IN6ADDR_LOOPBACK_INIT;
112 const struct in6_addr kin6addr_nodelocal_allnodes =
113 IN6ADDR_NODELOCAL_ALLNODES_INIT;
114 const struct in6_addr kin6addr_linklocal_allnodes =
115 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
116 const struct in6_addr kin6addr_linklocal_allrouters =
117 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
119 const struct in6_addr in6mask0 = IN6MASK0;
120 const struct in6_addr in6mask32 = IN6MASK32;
121 const struct in6_addr in6mask64 = IN6MASK64;
122 const struct in6_addr in6mask96 = IN6MASK96;
123 const struct in6_addr in6mask128 = IN6MASK128;
125 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
126 0, 0, IN6ADDR_ANY_INIT, 0};
128 static int in6_lifaddr_ioctl (struct socket *, u_long, caddr_t,
129 struct ifnet *, struct thread *);
130 static int in6_ifinit (struct ifnet *, struct in6_ifaddr *,
131 struct sockaddr_in6 *, int);
132 static void in6_unlink_ifa (struct in6_ifaddr *, struct ifnet *);
133 static void in6_ifloop_request_callback(int, int, struct rt_addrinfo *, struct rtentry *, void *);
135 struct in6_multihead in6_multihead; /* XXX BSS initialization */
137 int (*faithprefix_p)(struct in6_addr *);
140 * Subroutine for in6_ifaddloop() and in6_ifremloop().
141 * This routine does actual work.
144 in6_ifloop_request(int cmd, struct ifaddr *ifa)
146 struct sockaddr_in6 all1_sa;
147 struct rt_addrinfo rtinfo;
150 bzero(&all1_sa, sizeof(all1_sa));
151 all1_sa.sin6_family = AF_INET6;
152 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
153 all1_sa.sin6_addr = in6mask128;
156 * We specify the address itself as the gateway, and set the
157 * RTF_LLINFO flag, so that the corresponding host route would have
158 * the flag, and thus applications that assume traditional behavior
159 * would be happy. Note that we assume the caller of the function
160 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
161 * which changes the outgoing interface to the loopback interface.
163 bzero(&rtinfo, sizeof(struct rt_addrinfo));
164 rtinfo.rti_info[RTAX_DST] = ifa->ifa_addr;
165 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
166 rtinfo.rti_info[RTAX_NETMASK] = (struct sockaddr *)&all1_sa;
167 rtinfo.rti_flags = RTF_UP|RTF_HOST|RTF_LLINFO;
169 error = rtrequest1_global(cmd, &rtinfo,
170 in6_ifloop_request_callback, ifa, RTREQ_PRIO_NORM);
172 log(LOG_ERR, "in6_ifloop_request: "
173 "%s operation failed for %s (errno=%d)\n",
174 cmd == RTM_ADD ? "ADD" : "DELETE",
175 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
181 in6_ifloop_request_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
182 struct rtentry *rt, void *arg)
184 struct ifaddr *ifa = arg;
190 * Make sure rt_ifa be equal to IFA, the second argument of the
192 * We need this because when we refer to rt_ifa->ia6_flags in
193 * ip6_input, we assume that the rt_ifa points to the address instead
194 * of the loopback address.
196 if (cmd == RTM_ADD && rt && ifa != rt->rt_ifa) {
205 * Report the addition/removal of the address to the routing socket.
206 * XXX: since we called rtinit for a p2p interface with a destination,
207 * we end up reporting twice in such a case. Should we rather
208 * omit the second report?
212 rt_newaddrmsg(cmd, ifa, error, rt);
213 if (cmd == RTM_DELETE) {
214 if (rt->rt_refcnt == 0) {
221 /* no way to return any new error */
226 * Add ownaddr as loopback rtentry. We previously add the route only if
227 * necessary (ex. on a p2p link). However, since we now manage addresses
228 * separately from prefixes, we should always add the route. We can't
229 * rely on the cloning mechanism from the corresponding interface route
233 in6_ifaddloop(struct ifaddr *ifa)
237 /* If there is no loopback entry, allocate one. */
238 rt = rtpurelookup(ifa->ifa_addr);
239 if (rt == NULL || !(rt->rt_flags & RTF_HOST) ||
240 !(rt->rt_ifp->if_flags & IFF_LOOPBACK))
241 in6_ifloop_request(RTM_ADD, ifa);
247 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
251 in6_ifremloop(struct ifaddr *ifa)
253 struct in6_ifaddr *ia;
258 * Some of BSD variants do not remove cloned routes
259 * from an interface direct route, when removing the direct route
260 * (see comments in net/net_osdep.h). Even for variants that do remove
261 * cloned routes, they could fail to remove the cloned routes when
262 * we handle multple addresses that share a common prefix.
263 * So, we should remove the route corresponding to the deleted address
264 * regardless of the result of in6_is_ifloop_auto().
268 * Delete the entry only if exact one ifa exists. More than one ifa
269 * can exist if we assign a same single address to multiple
270 * (probably p2p) interfaces.
271 * XXX: we should avoid such a configuration in IPv6...
273 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
274 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
283 * Before deleting, check if a corresponding loopbacked host
284 * route surely exists. With this check, we can avoid to
285 * delete an interface direct route whose destination is same
286 * as the address being removed. This can happen when remofing
287 * a subnet-router anycast address on an interface attahced
288 * to a shared medium.
290 rt = rtpurelookup(ifa->ifa_addr);
291 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
292 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
294 in6_ifloop_request(RTM_DELETE, ifa);
300 in6_ifindex2scopeid(int idx)
303 struct sockaddr_in6 *sin6;
304 struct ifaddr_container *ifac;
306 if (idx < 0 || if_index < idx)
308 ifp = ifindex2ifnet[idx];
310 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link)
312 struct ifaddr *ifa = ifac->ifa;
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_dispatch(netmsg_t msg)
380 error = in6_control(msg->control.base.nm_so,
382 msg->control.nm_data,
385 lwkt_replymsg(&msg->control.base.lmsg, error);
389 in6_control(struct socket *so, u_long cmd, caddr_t data,
390 struct ifnet *ifp, struct thread *td)
392 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
393 struct in6_ifaddr *ia = NULL;
394 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
399 if (priv_check(td, PRIV_ROOT) == 0)
403 case SIOCGETSGCNT_IN6:
404 case SIOCGETMIFCNT_IN6:
405 return (mrt6_ioctl(cmd, data));
409 case SIOCAADDRCTL_POLICY:
410 case SIOCDADDRCTL_POLICY:
413 return (in6_src_ioctl(cmd, data));
420 case SIOCSNDFLUSH_IN6:
421 case SIOCSPFXFLUSH_IN6:
422 case SIOCSRTRFLUSH_IN6:
423 case SIOCSDEFIFACE_IN6:
424 case SIOCSIFINFO_FLAGS:
428 case OSIOCGIFINFO_IN6:
429 case SIOCGIFINFO_IN6:
432 case SIOCGNBRINFO_IN6:
433 case SIOCGDEFIFACE_IN6:
434 return (nd6_ioctl(cmd, data, ifp));
438 case SIOCSIFPREFIX_IN6:
439 case SIOCDIFPREFIX_IN6:
440 case SIOCAIFPREFIX_IN6:
441 case SIOCCIFPREFIX_IN6:
442 case SIOCSGIFPREFIX_IN6:
443 case SIOCGIFPREFIX_IN6:
445 "prefix ioctls are now invalidated. "
446 "please use ifconfig.\n");
454 return (scope6_set(ifp,
455 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
458 return (scope6_get(ifp,
459 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
462 return (scope6_get_default((struct scope6_id *)
463 ifr->ifr_ifru.ifru_scope_id));
474 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
478 * Find address for this interface, if it exists.
480 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
481 struct sockaddr_in6 *sa6 =
482 (struct sockaddr_in6 *)&ifra->ifra_addr;
484 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
485 if (sa6->sin6_addr.s6_addr16[1] == 0) {
486 /* link ID is not embedded by the user */
487 sa6->sin6_addr.s6_addr16[1] =
488 htons(ifp->if_index);
489 } else if (sa6->sin6_addr.s6_addr16[1] !=
490 htons(ifp->if_index)) {
491 return (EINVAL); /* link ID contradicts */
493 if (sa6->sin6_scope_id) {
494 if (sa6->sin6_scope_id !=
495 (u_int32_t)ifp->if_index)
497 sa6->sin6_scope_id = 0; /* XXX: good way? */
500 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
504 case SIOCSIFADDR_IN6:
505 case SIOCSIFDSTADDR_IN6:
506 case SIOCSIFNETMASK_IN6:
508 * Since IPv6 allows a node to assign multiple addresses
509 * on a single interface, SIOCSIFxxx ioctls are not suitable
510 * and should be unused.
512 /* we decided to obsolete this command (20000704) */
515 case SIOCDIFADDR_IN6:
517 * for IPv4, we look for existing in_ifaddr here to allow
518 * "ifconfig if0 delete" to remove first IPv4 address on the
519 * interface. For IPv6, as the spec allow multiple interface
520 * address from the day one, we consider "remove the first one"
521 * semantics to be not preferable.
524 return (EADDRNOTAVAIL);
526 case SIOCAIFADDR_IN6:
528 * We always require users to specify a valid IPv6 address for
529 * the corresponding operation.
531 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
532 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
533 return (EAFNOSUPPORT);
539 case SIOCGIFADDR_IN6:
540 /* This interface is basically deprecated. use SIOCGIFCONF. */
542 case SIOCGIFAFLAG_IN6:
543 case SIOCGIFNETMASK_IN6:
544 case SIOCGIFDSTADDR_IN6:
545 case SIOCGIFALIFETIME_IN6:
546 /* must think again about its semantics */
548 return (EADDRNOTAVAIL);
550 case SIOCSIFALIFETIME_IN6:
552 struct in6_addrlifetime *lt;
557 return (EADDRNOTAVAIL);
558 /* sanity for overflow - beware unsigned */
559 lt = &ifr->ifr_ifru.ifru_lifetime;
560 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
561 && lt->ia6t_vltime + time_uptime < time_uptime) {
564 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
565 && lt->ia6t_pltime + time_uptime < time_uptime) {
574 case SIOCGIFADDR_IN6:
575 ifr->ifr_addr = ia->ia_addr;
578 case SIOCGIFDSTADDR_IN6:
579 if (!(ifp->if_flags & IFF_POINTOPOINT))
582 * XXX: should we check if ifa_dstaddr is NULL and return
585 ifr->ifr_dstaddr = ia->ia_dstaddr;
588 case SIOCGIFNETMASK_IN6:
589 ifr->ifr_addr = ia->ia_prefixmask;
592 case SIOCGIFAFLAG_IN6:
593 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
596 case SIOCGIFSTAT_IN6:
599 bzero(&ifr->ifr_ifru.ifru_stat,
600 sizeof(ifr->ifr_ifru.ifru_stat));
601 ifr->ifr_ifru.ifru_stat =
602 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
605 case SIOCGIFSTAT_ICMP6:
606 bzero(&ifr->ifr_ifru.ifru_stat,
607 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
608 ifr->ifr_ifru.ifru_icmp6stat =
609 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
615 * Do not pass those ioctl to driver handler since they are not
616 * properly setup. Instead just error out.
620 case SIOCGIFALIFETIME_IN6:
621 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
624 case SIOCSIFALIFETIME_IN6:
625 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
627 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
628 ia->ia6_lifetime.ia6t_expire =
629 time_uptime + ia->ia6_lifetime.ia6t_vltime;
631 ia->ia6_lifetime.ia6t_expire = 0;
632 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
633 ia->ia6_lifetime.ia6t_preferred =
634 time_uptime + ia->ia6_lifetime.ia6t_pltime;
636 ia->ia6_lifetime.ia6t_preferred = 0;
639 case SIOCAIFADDR_IN6:
641 int i, error = 0, iaIsNew;
642 struct nd_prefix pr0, *pr;
650 * first, make or update the interface address structure,
651 * and link it to the list.
653 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
657 * then, make the prefix on-link on the interface.
658 * XXX: we'd rather create the prefix before the address, but
659 * we need at least one address to install the corresponding
660 * interface route, so we configure the address first.
664 * convert mask to prefix length (prefixmask has already
665 * been validated in in6_update_ifa().
667 bzero(&pr0, sizeof(pr0));
669 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
671 if (pr0.ndpr_plen == 128)
672 break; /* we don't need to install a host route. */
673 pr0.ndpr_prefix = ifra->ifra_addr;
674 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
675 /* apply the mask for safety. */
676 for (i = 0; i < 4; i++) {
677 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
678 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
681 * XXX: since we don't have an API to set prefix (not address)
682 * lifetimes, we just use the same lifetimes as addresses.
683 * The (temporarily) installed lifetimes can be overridden by
684 * later advertised RAs (when accept_rtadv is non 0), which is
685 * an intended behavior.
687 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
689 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
690 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
691 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
693 /* add the prefix if there's one. */
694 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
696 * nd6_prelist_add will install the corresponding
699 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
702 log(LOG_ERR, "nd6_prelist_add succeeded but "
704 return (EINVAL); /* XXX panic here? */
707 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
709 /* XXX: this should not happen! */
710 log(LOG_ERR, "in6_control: addition succeeded, but"
713 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
714 ia->ia6_ndpr == NULL) { /* new autoconfed addr */
719 * If this is the first autoconf address from
720 * the prefix, create a temporary address
721 * as well (when specified).
723 if (ip6_use_tempaddr &&
724 pr->ndpr_refcnt == 1) {
726 if ((e = in6_tmpifadd(ia, 1)) != 0) {
727 log(LOG_NOTICE, "in6_control: "
728 "failed to create a "
729 "temporary address, "
737 * this might affect the status of autoconfigured
738 * addresses, that is, this address might make
739 * other addresses detached.
741 pfxlist_onlink_check();
743 if (error == 0 && ia) {
744 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
745 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
751 case SIOCDIFADDR_IN6:
754 struct nd_prefix pr0, *pr;
757 * If the address being deleted is the only one that owns
758 * the corresponding prefix, expire the prefix as well.
759 * XXX: theoretically, we don't have to warry about such
760 * relationship, since we separate the address management
761 * and the prefix management. We do this, however, to provide
762 * as much backward compatibility as possible in terms of
763 * the ioctl operation.
765 bzero(&pr0, sizeof(pr0));
767 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
769 if (pr0.ndpr_plen == 128)
771 pr0.ndpr_prefix = ia->ia_addr;
772 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
773 for (i = 0; i < 4; i++) {
774 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
775 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
778 * The logic of the following condition is a bit complicated.
779 * We expire the prefix when
780 * 1. the address obeys autoconfiguration and it is the
781 * only owner of the associated prefix, or
782 * 2. the address does not obey autoconf and there is no
783 * other owner of the prefix.
785 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
786 (((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
787 pr->ndpr_refcnt == 1) ||
788 (!(ia->ia6_flags & IN6_IFF_AUTOCONF) &&
789 pr->ndpr_refcnt == 0))) {
790 pr->ndpr_expire = 1; /* XXX: just for expiration */
794 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
796 in6_purgeaddr(&ia->ia_ifa);
801 if (ifp == NULL || ifp->if_ioctl == NULL)
803 ifnet_serialize_all(ifp);
804 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
805 ifnet_deserialize_all(ifp);
813 * Update parameters of an IPv6 interface address.
814 * If necessary, a new entry is created and linked into address chains.
815 * This function is separated from in6_control().
816 * XXX: should this be performed under splnet()?
819 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
820 struct in6_ifaddr *ia)
822 int error = 0, hostIsNew = 0, plen = -1;
823 struct in6_ifaddr *oia;
824 struct sockaddr_in6 dst6;
825 struct in6_addrlifetime *lt;
827 /* Validate parameters */
828 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
832 * The destination address for a p2p link must have a family
833 * of AF_UNSPEC or AF_INET6.
835 if ((ifp->if_flags & IFF_POINTOPOINT) &&
836 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
837 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
838 return (EAFNOSUPPORT);
840 * validate ifra_prefixmask. don't check sin6_family, netmask
841 * does not carry fields other than sin6_len.
843 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
846 * Because the IPv6 address architecture is classless, we require
847 * users to specify a (non 0) prefix length (mask) for a new address.
848 * We also require the prefix (when specified) mask is valid, and thus
849 * reject a non-consecutive mask.
851 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
853 if (ifra->ifra_prefixmask.sin6_len != 0) {
854 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
855 (u_char *)&ifra->ifra_prefixmask +
856 ifra->ifra_prefixmask.sin6_len);
862 * In this case, ia must not be NULL. We just use its prefix
865 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
868 * If the destination address on a p2p interface is specified,
869 * and the address is a scoped one, validate/set the scope
872 dst6 = ifra->ifra_dstaddr;
873 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
874 (dst6.sin6_family == AF_INET6)) {
877 if ((error = in6_recoverscope(&dst6,
878 &ifra->ifra_dstaddr.sin6_addr,
881 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
882 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
883 dst6.sin6_scope_id = scopeid;
884 else if (dst6.sin6_scope_id != scopeid)
885 return (EINVAL); /* scope ID mismatch. */
886 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
889 dst6.sin6_scope_id = 0; /* XXX */
892 * The destination address can be specified only for a p2p or a
893 * loopback interface. If specified, the corresponding prefix length
896 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
897 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
898 /* XXX: noisy message */
899 log(LOG_INFO, "in6_update_ifa: a destination can be "
900 "specified for a p2p or a loopback IF only\n");
905 * The following message seems noisy, but we dare to
906 * add it for diagnosis.
908 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
909 "when dstaddr is specified\n");
913 /* lifetime consistency check */
914 lt = &ifra->ifra_lifetime;
915 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
916 && lt->ia6t_vltime + time_uptime < time_uptime) {
919 if (lt->ia6t_vltime == 0) {
921 * the following log might be noisy, but this is a typical
922 * configuration mistake or a tool's bug.
925 "in6_update_ifa: valid lifetime is 0 for %s\n",
926 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
928 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
929 && lt->ia6t_pltime + time_uptime < time_uptime) {
934 * If this is a new address, allocate a new ifaddr and link it
940 * When in6_update_ifa() is called in a process of a received
941 * RA, it is called under splnet(). So, we should call malloc
944 ia = ifa_create(sizeof(*ia), M_NOWAIT);
947 /* Initialize the address and masks */
948 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
949 ia->ia_addr.sin6_family = AF_INET6;
950 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
951 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
953 * XXX: some functions expect that ifa_dstaddr is not
954 * NULL for p2p interfaces.
956 ia->ia_ifa.ifa_dstaddr
957 = (struct sockaddr *)&ia->ia_dstaddr;
959 ia->ia_ifa.ifa_dstaddr = NULL;
961 ia->ia_ifa.ifa_netmask
962 = (struct sockaddr *)&ia->ia_prefixmask;
965 if ((oia = in6_ifaddr) != NULL) {
966 for ( ; oia->ia_next; oia = oia->ia_next)
972 ifa_iflink(&ia->ia_ifa, ifp, 1);
975 /* set prefix mask */
976 if (ifra->ifra_prefixmask.sin6_len) {
978 * We prohibit changing the prefix length of an existing
980 * + such an operation should be rare in IPv6, and
981 * + the operation would confuse prefix management.
983 if (ia->ia_prefixmask.sin6_len &&
984 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
985 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
986 " existing (%s) address should not be changed\n",
987 ip6_sprintf(&ia->ia_addr.sin6_addr));
991 ia->ia_prefixmask = ifra->ifra_prefixmask;
995 * If a new destination address is specified, scrub the old one and
996 * install the new destination. Note that the interface must be
997 * p2p or loopback (see the check above.)
999 if (dst6.sin6_family == AF_INET6 &&
1000 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
1001 &ia->ia_dstaddr.sin6_addr)) {
1004 if ((ia->ia_flags & IFA_ROUTE) &&
1005 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1007 log(LOG_ERR, "in6_update_ifa: failed to remove "
1008 "a route to the old destination: %s\n",
1009 ip6_sprintf(&ia->ia_addr.sin6_addr));
1010 /* proceed anyway... */
1013 ia->ia_flags &= ~IFA_ROUTE;
1014 ia->ia_dstaddr = dst6;
1017 /* reset the interface and routing table appropriately. */
1018 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1022 * Beyond this point, we should call in6_purgeaddr upon an error,
1023 * not just go to unlink.
1026 #if 0 /* disable this mechanism for now */
1027 /* update prefix list */
1029 (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
1032 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
1033 if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
1034 in6_purgeaddr((struct ifaddr *)ia);
1040 if (ifp->if_flags & IFF_MULTICAST) {
1041 struct sockaddr_in6 mltaddr, mltmask;
1042 struct in6_multi *in6m;
1046 * join solicited multicast addr for new host id
1048 struct in6_addr llsol;
1049 bzero(&llsol, sizeof(struct in6_addr));
1050 llsol.s6_addr16[0] = htons(0xff02);
1051 llsol.s6_addr16[1] = htons(ifp->if_index);
1052 llsol.s6_addr32[1] = 0;
1053 llsol.s6_addr32[2] = htonl(1);
1054 llsol.s6_addr32[3] =
1055 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1056 llsol.s6_addr8[12] = 0xff;
1057 in6_addmulti(&llsol, ifp, &error);
1060 "in6_update_ifa: addmulti failed for "
1061 "%s on %s (errno=%d)\n",
1062 ip6_sprintf(&llsol), if_name(ifp),
1064 in6_purgeaddr((struct ifaddr *)ia);
1069 bzero(&mltmask, sizeof(mltmask));
1070 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1071 mltmask.sin6_family = AF_INET6;
1072 mltmask.sin6_addr = in6mask32;
1075 * join link-local all-nodes address
1077 bzero(&mltaddr, sizeof(mltaddr));
1078 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1079 mltaddr.sin6_family = AF_INET6;
1080 mltaddr.sin6_addr = kin6addr_linklocal_allnodes;
1081 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1083 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1085 rtrequest_global(RTM_ADD,
1086 (struct sockaddr *)&mltaddr,
1087 (struct sockaddr *)&ia->ia_addr,
1088 (struct sockaddr *)&mltmask,
1089 RTF_UP|RTF_CLONING); /* xxx */
1090 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1093 "in6_update_ifa: addmulti failed for "
1094 "%s on %s (errno=%d)\n",
1095 ip6_sprintf(&mltaddr.sin6_addr),
1096 if_name(ifp), error);
1101 * join node information group address
1103 #define hostnamelen strlen(hostname)
1104 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1106 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1107 if (in6m == NULL && ia != NULL) {
1108 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1110 log(LOG_WARNING, "in6_update_ifa: "
1111 "addmulti failed for "
1112 "%s on %s (errno=%d)\n",
1113 ip6_sprintf(&mltaddr.sin6_addr),
1114 if_name(ifp), error);
1121 * join node-local all-nodes address, on loopback.
1122 * XXX: since "node-local" is obsoleted by interface-local,
1123 * we have to join the group on every interface with
1124 * some interface-boundary restriction.
1126 if (ifp->if_flags & IFF_LOOPBACK) {
1127 struct in6_ifaddr *ia_loop;
1129 struct in6_addr loop6 = kin6addr_loopback;
1130 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1132 mltaddr.sin6_addr = kin6addr_nodelocal_allnodes;
1134 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1135 if (in6m == NULL && ia_loop != NULL) {
1136 rtrequest_global(RTM_ADD,
1137 (struct sockaddr *)&mltaddr,
1138 (struct sockaddr *)&ia_loop->ia_addr,
1139 (struct sockaddr *)&mltmask,
1141 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1143 log(LOG_WARNING, "in6_update_ifa: "
1144 "addmulti failed for %s on %s "
1146 ip6_sprintf(&mltaddr.sin6_addr),
1147 if_name(ifp), error);
1153 ia->ia6_flags = ifra->ifra_flags;
1154 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1155 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1157 ia->ia6_lifetime = ifra->ifra_lifetime;
1159 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1160 ia->ia6_lifetime.ia6t_expire =
1161 time_uptime + ia->ia6_lifetime.ia6t_vltime;
1163 ia->ia6_lifetime.ia6t_expire = 0;
1164 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1165 ia->ia6_lifetime.ia6t_preferred =
1166 time_uptime + ia->ia6_lifetime.ia6t_pltime;
1168 ia->ia6_lifetime.ia6t_preferred = 0;
1171 * Perform DAD, if needed.
1172 * XXX It may be of use, if we can administratively
1175 if (in6if_do_dad(ifp) && !(ifra->ifra_flags & IN6_IFF_NODAD)) {
1176 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1177 nd6_dad_start((struct ifaddr *)ia, NULL);
1184 * XXX: if a change of an existing address failed, keep the entry
1188 in6_unlink_ifa(ia, ifp);
1193 in6_purgeaddr(struct ifaddr *ifa)
1195 struct ifnet *ifp = ifa->ifa_ifp;
1196 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1198 /* stop DAD processing */
1202 * delete route to the destination of the address being purged.
1203 * The interface must be p2p or loopback in this case.
1205 if ((ia->ia_flags & IFA_ROUTE) && ia->ia_dstaddr.sin6_len != 0) {
1208 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1210 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1211 "a route to the p2p destination: %s on %s, "
1213 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1215 /* proceed anyway... */
1218 ia->ia_flags &= ~IFA_ROUTE;
1221 /* Remove ownaddr's loopback rtentry, if it exists. */
1222 in6_ifremloop(&(ia->ia_ifa));
1224 if (ifp->if_flags & IFF_MULTICAST) {
1226 * delete solicited multicast addr for deleting host id
1228 struct in6_multi *in6m;
1229 struct in6_addr llsol;
1230 bzero(&llsol, sizeof(struct in6_addr));
1231 llsol.s6_addr16[0] = htons(0xff02);
1232 llsol.s6_addr16[1] = htons(ifp->if_index);
1233 llsol.s6_addr32[1] = 0;
1234 llsol.s6_addr32[2] = htonl(1);
1235 llsol.s6_addr32[3] =
1236 ia->ia_addr.sin6_addr.s6_addr32[3];
1237 llsol.s6_addr8[12] = 0xff;
1239 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1244 in6_unlink_ifa(ia, ifp);
1248 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1251 struct in6_ifaddr *oia;
1255 ifa_ifunlink(&ia->ia_ifa, ifp);
1258 if (oia == (ia = in6_ifaddr))
1259 in6_ifaddr = ia->ia_next;
1261 while (ia->ia_next && (ia->ia_next != oia))
1264 ia->ia_next = oia->ia_next;
1267 kprintf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1271 if (oia->ia6_ifpr) { /* check for safety */
1272 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1273 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1274 in6_prefix_remove_ifid(iilen, oia);
1278 * When an autoconfigured address is being removed, release the
1279 * reference to the base prefix. Also, since the release might
1280 * affect the status of other (detached) addresses, call
1281 * pfxlist_onlink_check().
1283 if (oia->ia6_flags & IN6_IFF_AUTOCONF) {
1284 if (oia->ia6_ndpr == NULL) {
1285 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1286 "%p has no prefix\n", oia);
1288 oia->ia6_ndpr->ndpr_refcnt--;
1289 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1290 oia->ia6_ndpr = NULL;
1293 pfxlist_onlink_check();
1297 * release another refcnt for the link from in6_ifaddr.
1298 * Note that we should decrement the refcnt at least once for all *BSD.
1300 ifa_destroy(&oia->ia_ifa);
1306 in6_purgeif(struct ifnet *ifp)
1308 struct ifaddr_container *ifac, *next;
1310 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
1312 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
1314 in6_purgeaddr(ifac->ifa);
1322 * SIOCGLIFADDR: get first address. (?)
1323 * SIOCGLIFADDR with IFLR_PREFIX:
1324 * get first address that matches the specified prefix.
1325 * SIOCALIFADDR: add the specified address.
1326 * SIOCALIFADDR with IFLR_PREFIX:
1327 * add the specified prefix, filling hostid part from
1328 * the first link-local address. prefixlen must be <= 64.
1329 * SIOCDLIFADDR: delete the specified address.
1330 * SIOCDLIFADDR with IFLR_PREFIX:
1331 * delete the first address that matches the specified prefix.
1333 * EINVAL on invalid parameters
1334 * EADDRNOTAVAIL on prefix match failed/specified address not found
1335 * other values may be returned from in6_ioctl()
1337 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1338 * this is to accomodate address naming scheme other than RFC2374,
1340 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1341 * address encoding scheme. (see figure on page 8)
1344 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1345 struct ifnet *ifp, struct thread *td)
1347 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1348 struct sockaddr *sa;
1351 if (!data || !ifp) {
1352 panic("invalid argument to in6_lifaddr_ioctl");
1358 /* address must be specified on GET with IFLR_PREFIX */
1359 if (!(iflr->flags & IFLR_PREFIX))
1364 /* address must be specified on ADD and DELETE */
1365 sa = (struct sockaddr *)&iflr->addr;
1366 if (sa->sa_family != AF_INET6)
1368 if (sa->sa_len != sizeof(struct sockaddr_in6))
1370 /* XXX need improvement */
1371 sa = (struct sockaddr *)&iflr->dstaddr;
1372 if (sa->sa_family && sa->sa_family != AF_INET6)
1374 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1377 default: /* shouldn't happen */
1379 panic("invalid cmd to in6_lifaddr_ioctl");
1385 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1391 struct in6_aliasreq ifra;
1392 struct in6_addr *hostid = NULL;
1395 if (iflr->flags & IFLR_PREFIX) {
1397 struct sockaddr_in6 *sin6;
1400 * hostid is to fill in the hostid part of the
1401 * address. hostid points to the first link-local
1402 * address attached to the interface.
1404 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1406 return EADDRNOTAVAIL;
1407 hostid = IFA_IN6(ifa);
1409 /* prefixlen must be <= 64. */
1410 if (64 < iflr->prefixlen)
1412 prefixlen = iflr->prefixlen;
1414 /* hostid part must be zero. */
1415 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1416 if (sin6->sin6_addr.s6_addr32[2] != 0
1417 || sin6->sin6_addr.s6_addr32[3] != 0) {
1421 prefixlen = iflr->prefixlen;
1423 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1424 bzero(&ifra, sizeof(ifra));
1425 bcopy(iflr->iflr_name, ifra.ifra_name,
1426 sizeof(ifra.ifra_name));
1428 bcopy(&iflr->addr, &ifra.ifra_addr,
1429 ((struct sockaddr *)&iflr->addr)->sa_len);
1431 /* fill in hostid part */
1432 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1433 hostid->s6_addr32[2];
1434 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1435 hostid->s6_addr32[3];
1438 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1439 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1440 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1442 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1443 hostid->s6_addr32[2];
1444 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1445 hostid->s6_addr32[3];
1449 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1450 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1452 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1453 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1458 struct ifaddr_container *ifac;
1459 struct in6_ifaddr *ia;
1460 struct in6_addr mask, candidate, match;
1461 struct sockaddr_in6 *sin6;
1464 bzero(&mask, sizeof(mask));
1465 if (iflr->flags & IFLR_PREFIX) {
1466 /* lookup a prefix rather than address. */
1467 in6_len2mask(&mask, iflr->prefixlen);
1469 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1470 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1471 match.s6_addr32[0] &= mask.s6_addr32[0];
1472 match.s6_addr32[1] &= mask.s6_addr32[1];
1473 match.s6_addr32[2] &= mask.s6_addr32[2];
1474 match.s6_addr32[3] &= mask.s6_addr32[3];
1476 /* if you set extra bits, that's wrong */
1477 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1482 if (cmd == SIOCGLIFADDR) {
1483 /* on getting an address, take the 1st match */
1486 /* on deleting an address, do exact match */
1487 in6_len2mask(&mask, 128);
1488 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1489 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1495 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1496 struct ifaddr *ifa = ifac->ifa;
1498 if (ifa->ifa_addr->sa_family != AF_INET6)
1503 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1505 * XXX: this is adhoc, but is necessary to allow
1506 * a user to specify fe80::/64 (not /10) for a
1507 * link-local address.
1509 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1510 candidate.s6_addr16[1] = 0;
1511 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1512 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1513 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1514 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1515 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1519 return EADDRNOTAVAIL;
1520 ia = ifa2ia6(ifac->ifa);
1522 if (cmd == SIOCGLIFADDR) {
1523 struct sockaddr_in6 *s6;
1525 /* fill in the if_laddrreq structure */
1526 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1527 s6 = (struct sockaddr_in6 *)&iflr->addr;
1528 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1529 s6->sin6_addr.s6_addr16[1] = 0;
1531 in6_addr2scopeid(ifp, &s6->sin6_addr);
1533 if (ifp->if_flags & IFF_POINTOPOINT) {
1534 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1535 ia->ia_dstaddr.sin6_len);
1536 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1537 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1538 s6->sin6_addr.s6_addr16[1] = 0;
1540 in6_addr2scopeid(ifp,
1544 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1547 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1550 iflr->flags = ia->ia6_flags; /* XXX */
1554 struct in6_aliasreq ifra;
1556 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1557 bzero(&ifra, sizeof(ifra));
1558 bcopy(iflr->iflr_name, ifra.ifra_name,
1559 sizeof(ifra.ifra_name));
1561 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1562 ia->ia_addr.sin6_len);
1563 if (ifp->if_flags & IFF_POINTOPOINT)
1564 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1565 ia->ia_dstaddr.sin6_len);
1567 bzero(&ifra.ifra_dstaddr,
1568 sizeof(ifra.ifra_dstaddr));
1569 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1570 ia->ia_prefixmask.sin6_len);
1572 ifra.ifra_flags = ia->ia6_flags;
1573 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1579 return EOPNOTSUPP; /* just for safety */
1583 * Initialize an interface's intetnet6 address
1584 * and routing table entry.
1587 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6,
1590 int error = 0, plen, ifacount = 0;
1591 struct ifaddr_container *ifac;
1594 * Give the interface a chance to initialize
1595 * if this is its first address,
1596 * and to validate the address if necessary.
1598 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1599 if (ifac->ifa->ifa_addr == NULL)
1600 continue; /* just for safety */
1601 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
1606 ifnet_serialize_all(ifp);
1608 ia->ia_addr = *sin6;
1610 if (ifacount <= 1 && ifp->if_ioctl &&
1611 (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL))) {
1612 ifnet_deserialize_all(ifp);
1616 ifnet_deserialize_all(ifp);
1618 ia->ia_ifa.ifa_metric = ifp->if_metric;
1620 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1624 * If the destination address is specified for a point-to-point
1625 * interface, install a route to the destination as an interface
1628 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1629 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1630 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1631 RTF_UP | RTF_HOST)) != 0)
1633 ia->ia_flags |= IFA_ROUTE;
1637 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1639 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1642 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1644 /* set the rtrequest function to create llinfo */
1645 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1646 in6_ifaddloop(&(ia->ia_ifa));
1652 struct in6_multi_mship *
1653 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr, int *errorp)
1655 struct in6_multi_mship *imm;
1657 imm = kmalloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
1662 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
1663 if (!imm->i6mm_maddr) {
1664 /* *errorp is alrady set */
1665 kfree(imm, M_IPMADDR);
1672 in6_leavegroup(struct in6_multi_mship *imm)
1675 if (imm->i6mm_maddr)
1676 in6_delmulti(imm->i6mm_maddr);
1677 kfree(imm, M_IPMADDR);
1682 * Add an address to the list of IP6 multicast addresses for a
1686 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
1688 struct in6_multi *in6m;
1689 struct sockaddr_in6 sin6;
1690 struct ifmultiaddr *ifma;
1697 * Call generic routine to add membership or increment
1698 * refcount. It wants addresses in the form of a sockaddr,
1699 * so we build one here (being careful to zero the unused bytes).
1701 bzero(&sin6, sizeof sin6);
1702 sin6.sin6_family = AF_INET6;
1703 sin6.sin6_len = sizeof sin6;
1704 sin6.sin6_addr = *maddr6;
1705 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1712 * If ifma->ifma_protospec is null, then if_addmulti() created
1713 * a new record. Otherwise, we are done.
1715 if (ifma->ifma_protospec != NULL) {
1717 return ifma->ifma_protospec;
1720 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1721 at interrupt time? If so, need to fix if_addmulti. XXX */
1722 in6m = (struct in6_multi *)kmalloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1728 bzero(in6m, sizeof *in6m);
1729 in6m->in6m_addr = *maddr6;
1730 in6m->in6m_ifp = ifp;
1731 in6m->in6m_ifma = ifma;
1732 ifma->ifma_protospec = in6m;
1733 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1736 * Let MLD6 know that we have joined a new IP6 multicast
1739 mld6_start_listening(in6m);
1745 * Delete a multicast address record.
1748 in6_delmulti(struct in6_multi *in6m)
1750 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1754 if (ifma->ifma_refcount == 1) {
1756 * No remaining claims to this record; let MLD6 know
1757 * that we are leaving the multicast group.
1759 mld6_stop_listening(in6m);
1760 ifma->ifma_protospec = NULL;
1761 LIST_REMOVE(in6m, in6m_entry);
1762 kfree(in6m, M_IPMADDR);
1764 /* XXX - should be separate API for when we have an ifma? */
1765 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1770 * Find an IPv6 interface link-local address specific to an interface.
1773 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1775 struct ifaddr_container *ifac;
1777 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1778 struct ifaddr *ifa = ifac->ifa;
1780 if (ifa->ifa_addr == NULL)
1781 continue; /* just for safety */
1782 if (ifa->ifa_addr->sa_family != AF_INET6)
1784 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1785 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1792 return ((struct in6_ifaddr *)(ifac->ifa));
1799 * find the internet address corresponding to a given interface and address.
1802 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1804 struct ifaddr_container *ifac;
1806 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1807 struct ifaddr *ifa = ifac->ifa;
1809 if (ifa->ifa_addr == NULL)
1810 continue; /* just for safety */
1811 if (ifa->ifa_addr->sa_family != AF_INET6)
1813 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1817 return ((struct in6_ifaddr *)(ifac->ifa));
1823 * find the internet address on a given interface corresponding to a neighbor's
1827 in6ifa_ifplocaladdr(const struct ifnet *ifp, const struct in6_addr *addr)
1830 struct in6_ifaddr *ia;
1831 struct ifaddr_container *ifac;
1833 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1836 if (ifa->ifa_addr == NULL)
1837 continue; /* just for safety */
1838 if (ifa->ifa_addr->sa_family != AF_INET6)
1840 ia = (struct in6_ifaddr *)ifa;
1841 if (IN6_ARE_MASKED_ADDR_EQUAL(addr,
1842 &ia->ia_addr.sin6_addr,
1843 &ia->ia_prefixmask.sin6_addr))
1851 * Convert IP6 address to printable (loggable) representation.
1853 static char digits[] = "0123456789abcdef";
1854 static int ip6round = 0;
1856 ip6_sprintf(const struct in6_addr *addr)
1858 static char ip6buf[8][48];
1861 const u_short *a = (const u_short *)addr;
1865 ip6round = (ip6round + 1) & 7;
1866 cp = ip6buf[ip6round];
1868 for (i = 0; i < 8; i++) {
1879 if (dcolon == 0 && *(a + 1) == 0) {
1891 d = (const u_char *)a;
1892 *cp++ = digits[*d >> 4];
1893 *cp++ = digits[*d++ & 0xf];
1894 *cp++ = digits[*d >> 4];
1895 *cp++ = digits[*d & 0xf];
1900 return (ip6buf[ip6round]);
1904 in6_localaddr(struct in6_addr *in6)
1906 struct in6_ifaddr *ia;
1908 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1911 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1912 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1913 &ia->ia_prefixmask.sin6_addr))
1920 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1922 struct in6_ifaddr *ia;
1924 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1925 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1927 (ia->ia6_flags & IN6_IFF_DEPRECATED))
1928 return (1); /* true */
1930 /* XXX: do we still have to go thru the rest of the list? */
1933 return (0); /* false */
1937 * return length of part which dst and src are equal
1941 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1944 u_char *s = (u_char *)src, *d = (u_char *)dst;
1945 u_char *lim = s + 16, r;
1948 if ((r = (*d++ ^ *s++)) != 0) {
1959 /* XXX: to be scope conscious */
1961 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1963 int bytelen, bitlen;
1966 if (0 > len || len > 128) {
1967 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1975 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1977 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1978 p2->s6_addr[bytelen] >> (8 - bitlen))
1985 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1987 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1988 int bytelen, bitlen, i;
1991 if (0 > len || len > 128) {
1992 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1997 bzero(maskp, sizeof(*maskp));
2000 for (i = 0; i < bytelen; i++)
2001 maskp->s6_addr[i] = 0xff;
2003 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2007 * return the best address out of the same scope
2010 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst)
2012 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
2015 struct in6_ifaddr *ifa_best = NULL;
2019 kprintf("in6_ifawithscope: output interface is not specified\n");
2025 * We search for all addresses on all interfaces from the beginning.
2026 * Comparing an interface with the outgoing interface will be done
2027 * only at the final stage of tiebreaking.
2029 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2031 struct ifaddr_container *ifac;
2034 * We can never take an address that breaks the scope zone
2035 * of the destination.
2037 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
2040 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2041 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
2042 struct ifaddr *ifa = ifac->ifa;
2044 if (ifa->ifa_addr->sa_family != AF_INET6)
2047 src_scope = in6_addrscope(IFA_IN6(ifa));
2050 * Don't use an address before completing DAD
2051 * nor a duplicated address.
2053 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2057 /* XXX: is there any case to allow anycasts? */
2058 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2062 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2067 * If this is the first address we find,
2070 if (ifa_best == NULL)
2074 * ifa_best is never NULL beyond this line except
2075 * within the block labeled "replace".
2079 * If ifa_best has a smaller scope than dst and
2080 * the current address has a larger one than
2081 * (or equal to) dst, always replace ifa_best.
2082 * Also, if the current address has a smaller scope
2083 * than dst, ignore it unless ifa_best also has a
2085 * Consequently, after the two if-clause below,
2086 * the followings must be satisfied:
2087 * (scope(src) < scope(dst) &&
2088 * scope(best) < scope(dst))
2090 * (scope(best) >= scope(dst) &&
2091 * scope(src) >= scope(dst))
2093 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2094 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2095 goto replace; /* (A) */
2096 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2097 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2101 * A deprecated address SHOULD NOT be used in new
2102 * communications if an alternate (non-deprecated)
2103 * address is available and has sufficient scope.
2104 * RFC 2462, Section 5.5.4.
2106 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2107 IN6_IFF_DEPRECATED) {
2109 * Ignore any deprecated addresses if
2110 * specified by configuration.
2112 if (!ip6_use_deprecated)
2116 * If we have already found a non-deprecated
2117 * candidate, just ignore deprecated addresses.
2119 if (!(ifa_best->ia6_flags & IN6_IFF_DEPRECATED))
2124 * A non-deprecated address is always preferred
2125 * to a deprecated one regardless of scopes and
2126 * address matching (Note invariants ensured by the
2127 * conditions (A) and (B) above.)
2129 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2130 !(((struct in6_ifaddr *)ifa)->ia6_flags &
2131 IN6_IFF_DEPRECATED))
2135 * When we use temporary addresses described in
2136 * RFC 3041, we prefer temporary addresses to
2137 * public autoconf addresses. Again, note the
2138 * invariants from (A) and (B). Also note that we
2139 * don't have any preference between static addresses
2140 * and autoconf addresses (despite of whether or not
2141 * the latter is temporary or public.)
2143 if (ip6_use_tempaddr) {
2144 struct in6_ifaddr *ifat;
2146 ifat = (struct in6_ifaddr *)ifa;
2147 if ((ifa_best->ia6_flags &
2148 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2149 == IN6_IFF_AUTOCONF &&
2151 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2152 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2155 if ((ifa_best->ia6_flags &
2156 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2157 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2159 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2160 == IN6_IFF_AUTOCONF) {
2166 * At this point, we have two cases:
2167 * 1. we are looking at a non-deprecated address,
2168 * and ifa_best is also non-deprecated.
2169 * 2. we are looking at a deprecated address,
2170 * and ifa_best is also deprecated.
2171 * Also, we do not have to consider a case where
2172 * the scope of if_best is larger(smaller) than dst and
2173 * the scope of the current address is smaller(larger)
2174 * than dst. Such a case has already been covered.
2175 * Tiebreaking is done according to the following
2177 * - the scope comparison between the address and
2179 * - the scope comparison between the address and
2180 * ifa_best (bscopecmp)
2181 * - if the address match dst longer than ifa_best
2183 * - if the address is on the outgoing I/F (outI/F)
2185 * Roughly speaking, the selection policy is
2186 * - the most important item is scope. The same scope
2187 * is best. Then search for a larger scope.
2188 * Smaller scopes are the last resort.
2189 * - A deprecated address is chosen only when we have
2190 * no address that has an enough scope, but is
2191 * prefered to any addresses of smaller scopes
2192 * (this must be already done above.)
2193 * - addresses on the outgoing I/F are preferred to
2194 * ones on other interfaces if none of above
2195 * tiebreaks. In the table below, the column "bI"
2196 * means if the best_ifa is on the outgoing
2197 * interface, and the column "sI" means if the ifa
2198 * is on the outgoing interface.
2199 * - If there is no other reasons to choose one,
2200 * longest address match against dst is considered.
2202 * The precise decision table is as follows:
2203 * dscopecmp bscopecmp match bI oI | replace?
2204 * N/A equal N/A Y N | No (1)
2205 * N/A equal N/A N Y | Yes (2)
2206 * N/A equal larger N/A | Yes (3)
2207 * N/A equal !larger N/A | No (4)
2208 * larger larger N/A N/A | No (5)
2209 * larger smaller N/A N/A | Yes (6)
2210 * smaller larger N/A N/A | Yes (7)
2211 * smaller smaller N/A N/A | No (8)
2212 * equal smaller N/A N/A | Yes (9)
2213 * equal larger (already done at A above)
2215 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2216 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2218 if (bscopecmp == 0) {
2219 struct ifnet *bifp = ifa_best->ia_ifp;
2221 if (bifp == oifp && ifp != oifp) /* (1) */
2223 if (bifp != oifp && ifp == oifp) /* (2) */
2227 * Both bifp and ifp are on the outgoing
2228 * interface, or both two are on a different
2229 * interface from the outgoing I/F.
2230 * now we need address matching against dst
2233 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2234 matchcmp = tlen - blen;
2235 if (matchcmp > 0) /* (3) */
2239 if (dscopecmp > 0) {
2240 if (bscopecmp > 0) /* (5) */
2242 goto replace; /* (6) */
2244 if (dscopecmp < 0) {
2245 if (bscopecmp > 0) /* (7) */
2250 /* now dscopecmp must be 0 */
2252 goto replace; /* (9) */
2255 ifa_best = (struct in6_ifaddr *)ifa;
2256 blen = tlen >= 0 ? tlen :
2257 in6_matchlen(IFA_IN6(ifa), dst);
2258 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2262 /* count statistics for future improvements */
2263 if (ifa_best == NULL)
2264 ip6stat.ip6s_sources_none++;
2266 if (oifp == ifa_best->ia_ifp)
2267 ip6stat.ip6s_sources_sameif[best_scope]++;
2269 ip6stat.ip6s_sources_otherif[best_scope]++;
2271 if (best_scope == dst_scope)
2272 ip6stat.ip6s_sources_samescope[best_scope]++;
2274 ip6stat.ip6s_sources_otherscope[best_scope]++;
2276 if (ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2277 ip6stat.ip6s_sources_deprecated[best_scope]++;
2284 * return the best address out of the same scope. if no address was
2285 * found, return the first valid address from designated IF.
2288 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2290 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2291 struct ifaddr_container *ifac;
2292 struct in6_ifaddr *besta = NULL;
2293 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2295 dep[0] = dep[1] = NULL;
2298 * We first look for addresses in the same scope.
2299 * If there is one, return it.
2300 * If two or more, return one which matches the dst longest.
2301 * If none, return one of global addresses assigned other ifs.
2303 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2304 struct ifaddr *ifa = ifac->ifa;
2306 if (ifa->ifa_addr->sa_family != AF_INET6)
2308 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2309 continue; /* XXX: is there any case to allow anycast? */
2310 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2311 continue; /* don't use this interface */
2312 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2314 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2315 if (ip6_use_deprecated)
2316 dep[0] = (struct in6_ifaddr *)ifa;
2320 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2322 * call in6_matchlen() as few as possible
2326 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2327 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2330 besta = (struct in6_ifaddr *)ifa;
2333 besta = (struct in6_ifaddr *)ifa;
2339 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2340 struct ifaddr *ifa = ifac->ifa;
2342 if (ifa->ifa_addr->sa_family != AF_INET6)
2344 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2345 continue; /* XXX: is there any case to allow anycast? */
2346 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2347 continue; /* don't use this interface */
2348 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2350 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2351 if (ip6_use_deprecated)
2352 dep[1] = (struct in6_ifaddr *)ifa;
2356 return (struct in6_ifaddr *)ifa;
2359 /* use the last-resort values, that are, deprecated addresses */
2369 * perform DAD when interface becomes IFF_UP.
2372 in6_if_up(struct ifnet *ifp)
2374 struct ifaddr_container *ifac;
2375 struct in6_ifaddr *ia;
2376 int dad_delay; /* delay ticks before DAD output */
2379 * special cases, like 6to4, are handled in in6_ifattach
2381 in6_ifattach(ifp, NULL);
2384 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2385 struct ifaddr *ifa = ifac->ifa;
2387 if (ifa->ifa_addr->sa_family != AF_INET6)
2389 ia = (struct in6_ifaddr *)ifa;
2390 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2391 nd6_dad_start(ifa, &dad_delay);
2396 in6if_do_dad(struct ifnet *ifp)
2398 if (ifp->if_flags & IFF_LOOPBACK)
2401 switch (ifp->if_type) {
2407 * These interfaces do not have the IFF_LOOPBACK flag,
2408 * but loop packets back. We do not have to do DAD on such
2409 * interfaces. We should even omit it, because loop-backed
2410 * NS would confuse the DAD procedure.
2415 * Our DAD routine requires the interface up and running.
2416 * However, some interfaces can be up before the RUNNING
2417 * status. Additionaly, users may try to assign addresses
2418 * before the interface becomes up (or running).
2419 * We simply skip DAD in such a case as a work around.
2420 * XXX: we should rather mark "tentative" on such addresses,
2421 * and do DAD after the interface becomes ready.
2423 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2424 (IFF_UP|IFF_RUNNING))
2432 * Calculate max IPv6 MTU through all the interfaces and store it
2438 unsigned long maxmtu = 0;
2441 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2443 /* this function can be called during ifnet initialization */
2444 if (!ifp->if_afdata[AF_INET6])
2446 if (!(ifp->if_flags & IFF_LOOPBACK) &&
2447 ND_IFINFO(ifp)->linkmtu > maxmtu)
2448 maxmtu = ND_IFINFO(ifp)->linkmtu;
2450 if (maxmtu) /* update only when maxmtu is positive */
2451 in6_maxmtu = maxmtu;
2455 in6_domifattach(struct ifnet *ifp)
2457 struct in6_ifextra *ext;
2459 ext = (struct in6_ifextra *)kmalloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2460 bzero(ext, sizeof(*ext));
2462 ext->in6_ifstat = (struct in6_ifstat *)kmalloc(sizeof(struct in6_ifstat),
2463 M_IFADDR, M_WAITOK);
2464 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2467 (struct icmp6_ifstat *)kmalloc(sizeof(struct icmp6_ifstat),
2468 M_IFADDR, M_WAITOK);
2469 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2471 ext->nd_ifinfo = nd6_ifattach(ifp);
2472 ext->scope6_id = scope6_ifattach(ifp);
2477 in6_domifdetach(struct ifnet *ifp, void *aux)
2479 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2480 scope6_ifdetach(ext->scope6_id);
2481 nd6_ifdetach(ext->nd_ifinfo);
2482 kfree(ext->in6_ifstat, M_IFADDR);
2483 kfree(ext->icmp6_ifstat, M_IFADDR);
2484 kfree(ext, M_IFADDR);
2488 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2489 * v4 mapped addr or v4 compat addr
2492 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2494 bzero(sin, sizeof(*sin));
2495 sin->sin_len = sizeof(struct sockaddr_in);
2496 sin->sin_family = AF_INET;
2497 sin->sin_port = sin6->sin6_port;
2498 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2501 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2503 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2505 bzero(sin6, sizeof(*sin6));
2506 sin6->sin6_len = sizeof(struct sockaddr_in6);
2507 sin6->sin6_family = AF_INET6;
2508 sin6->sin6_port = sin->sin_port;
2509 sin6->sin6_addr.s6_addr32[0] = 0;
2510 sin6->sin6_addr.s6_addr32[1] = 0;
2511 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2512 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2515 /* Convert sockaddr_in6 into sockaddr_in. */
2517 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2519 struct sockaddr_in *sin_p;
2520 struct sockaddr_in6 sin6;
2523 * Save original sockaddr_in6 addr and convert it
2526 sin6 = *(struct sockaddr_in6 *)nam;
2527 sin_p = (struct sockaddr_in *)nam;
2528 in6_sin6_2_sin(sin_p, &sin6);
2531 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2533 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2535 struct sockaddr_in *sin_p;
2536 struct sockaddr_in6 *sin6_p;
2538 sin6_p = kmalloc(sizeof *sin6_p, M_SONAME, M_WAITOK);
2539 sin_p = (struct sockaddr_in *)*nam;
2540 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2541 kfree(*nam, M_SONAME);
2542 *nam = (struct sockaddr *)sin6_p;