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.30 2008/10/03 07:59:20 hasso 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>
82 #include <sys/kernel.h>
83 #include <sys/syslog.h>
85 #include <sys/thread2.h>
86 #include <sys/msgport2.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
91 #include <net/if_dl.h>
93 #include <netinet/in.h>
94 #include <netinet/in_var.h>
95 #include <netinet/if_ether.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/ip.h>
98 #include <netinet/in_pcb.h>
100 #include <netinet/ip6.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/nd6.h>
103 #include <netinet6/mld6_var.h>
104 #include <netinet6/ip6_mroute.h>
105 #include <netinet6/in6_ifattach.h>
106 #include <netinet6/scope6_var.h>
107 #include <netinet6/in6_pcb.h>
108 #include <netinet6/in6_var.h>
110 #include <net/net_osdep.h>
113 * Definitions of some costant IP6 addresses.
115 const struct in6_addr kin6addr_any = IN6ADDR_ANY_INIT;
116 const struct in6_addr kin6addr_loopback = IN6ADDR_LOOPBACK_INIT;
117 const struct in6_addr kin6addr_nodelocal_allnodes =
118 IN6ADDR_NODELOCAL_ALLNODES_INIT;
119 const struct in6_addr kin6addr_linklocal_allnodes =
120 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
121 const struct in6_addr kin6addr_linklocal_allrouters =
122 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
124 const struct in6_addr in6mask0 = IN6MASK0;
125 const struct in6_addr in6mask32 = IN6MASK32;
126 const struct in6_addr in6mask64 = IN6MASK64;
127 const struct in6_addr in6mask96 = IN6MASK96;
128 const struct in6_addr in6mask128 = IN6MASK128;
130 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
131 0, 0, IN6ADDR_ANY_INIT, 0};
133 static int in6_lifaddr_ioctl (struct socket *, u_long, caddr_t,
134 struct ifnet *, struct thread *);
135 static int in6_ifinit (struct ifnet *, struct in6_ifaddr *,
136 struct sockaddr_in6 *, int);
137 static void in6_unlink_ifa (struct in6_ifaddr *, struct ifnet *);
138 static void in6_ifloop_request_callback(int, int, struct rt_addrinfo *, struct rtentry *, void *);
140 struct in6_multihead in6_multihead; /* XXX BSS initialization */
142 int (*faithprefix_p)(struct in6_addr *);
145 * Subroutine for in6_ifaddloop() and in6_ifremloop().
146 * This routine does actual work.
149 in6_ifloop_request(int cmd, struct ifaddr *ifa)
151 struct sockaddr_in6 all1_sa;
152 struct rt_addrinfo rtinfo;
155 bzero(&all1_sa, sizeof(all1_sa));
156 all1_sa.sin6_family = AF_INET6;
157 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
158 all1_sa.sin6_addr = in6mask128;
161 * We specify the address itself as the gateway, and set the
162 * RTF_LLINFO flag, so that the corresponding host route would have
163 * the flag, and thus applications that assume traditional behavior
164 * would be happy. Note that we assume the caller of the function
165 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
166 * which changes the outgoing interface to the loopback interface.
168 bzero(&rtinfo, sizeof(struct rt_addrinfo));
169 rtinfo.rti_info[RTAX_DST] = ifa->ifa_addr;
170 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
171 rtinfo.rti_info[RTAX_NETMASK] = (struct sockaddr *)&all1_sa;
172 rtinfo.rti_flags = RTF_UP|RTF_HOST|RTF_LLINFO;
174 error = rtrequest1_global(cmd, &rtinfo,
175 in6_ifloop_request_callback, ifa);
177 log(LOG_ERR, "in6_ifloop_request: "
178 "%s operation failed for %s (errno=%d)\n",
179 cmd == RTM_ADD ? "ADD" : "DELETE",
180 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
186 in6_ifloop_request_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
187 struct rtentry *rt, void *arg)
189 struct ifaddr *ifa = arg;
195 * Make sure rt_ifa be equal to IFA, the second argument of the
197 * We need this because when we refer to rt_ifa->ia6_flags in
198 * ip6_input, we assume that the rt_ifa points to the address instead
199 * of the loopback address.
201 if (cmd == RTM_ADD && rt && ifa != rt->rt_ifa) {
210 * Report the addition/removal of the address to the routing socket.
211 * XXX: since we called rtinit for a p2p interface with a destination,
212 * we end up reporting twice in such a case. Should we rather
213 * omit the second report?
217 rt_newaddrmsg(cmd, ifa, error, rt);
218 if (cmd == RTM_DELETE) {
219 if (rt->rt_refcnt == 0) {
226 /* no way to return any new error */
231 * Add ownaddr as loopback rtentry. We previously add the route only if
232 * necessary (ex. on a p2p link). However, since we now manage addresses
233 * separately from prefixes, we should always add the route. We can't
234 * rely on the cloning mechanism from the corresponding interface route
238 in6_ifaddloop(struct ifaddr *ifa)
242 /* If there is no loopback entry, allocate one. */
243 rt = rtpurelookup(ifa->ifa_addr);
244 if (rt == NULL || !(rt->rt_flags & RTF_HOST) ||
245 !(rt->rt_ifp->if_flags & IFF_LOOPBACK))
246 in6_ifloop_request(RTM_ADD, ifa);
252 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
256 in6_ifremloop(struct ifaddr *ifa)
258 struct in6_ifaddr *ia;
263 * Some of BSD variants do not remove cloned routes
264 * from an interface direct route, when removing the direct route
265 * (see comments in net/net_osdep.h). Even for variants that do remove
266 * cloned routes, they could fail to remove the cloned routes when
267 * we handle multple addresses that share a common prefix.
268 * So, we should remove the route corresponding to the deleted address
269 * regardless of the result of in6_is_ifloop_auto().
273 * Delete the entry only if exact one ifa exists. More than one ifa
274 * can exist if we assign a same single address to multiple
275 * (probably p2p) interfaces.
276 * XXX: we should avoid such a configuration in IPv6...
278 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
279 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
288 * Before deleting, check if a corresponding loopbacked host
289 * route surely exists. With this check, we can avoid to
290 * delete an interface direct route whose destination is same
291 * as the address being removed. This can happen when remofing
292 * a subnet-router anycast address on an interface attahced
293 * to a shared medium.
295 rt = rtpurelookup(ifa->ifa_addr);
296 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
297 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
299 in6_ifloop_request(RTM_DELETE, ifa);
305 in6_ifindex2scopeid(int idx)
308 struct sockaddr_in6 *sin6;
309 struct ifaddr_container *ifac;
311 if (idx < 0 || if_index < idx)
313 ifp = ifindex2ifnet[idx];
315 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link)
317 struct ifaddr *ifa = ifac->ifa;
319 if (ifa->ifa_addr->sa_family != AF_INET6)
321 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
322 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
323 return sin6->sin6_scope_id & 0xffff;
330 in6_mask2len(struct in6_addr *mask, u_char *lim0)
333 u_char *lim = lim0, *p;
336 lim0 - (u_char *)mask > sizeof(*mask)) /* ignore the scope_id part */
337 lim = (u_char *)mask + sizeof(*mask);
338 for (p = (u_char *)mask; p < lim; x++, p++) {
344 for (y = 0; y < 8; y++) {
345 if ((*p & (0x80 >> y)) == 0)
351 * when the limit pointer is given, do a stricter check on the
355 if (y != 0 && (*p & (0x00ff >> y)) != 0)
357 for (p = p + 1; p < lim; p++)
366 in6_len2mask(struct in6_addr *mask, int len)
370 bzero(mask, sizeof(*mask));
371 for (i = 0; i < len / 8; i++)
372 mask->s6_addr8[i] = 0xff;
374 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
377 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
378 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
381 in6_control_dispatch(netmsg_t msg)
385 error = in6_control(msg->control.base.nm_so,
387 msg->control.nm_data,
390 lwkt_replymsg(&msg->control.base.lmsg, error);
394 in6_control(struct socket *so, u_long cmd, caddr_t data,
395 struct ifnet *ifp, struct thread *td)
397 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
398 struct in6_ifaddr *ia = NULL;
399 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
404 if (priv_check(td, PRIV_ROOT) == 0)
408 case SIOCGETSGCNT_IN6:
409 case SIOCGETMIFCNT_IN6:
410 return (mrt6_ioctl(cmd, data));
414 case SIOCAADDRCTL_POLICY:
415 case SIOCDADDRCTL_POLICY:
418 return (in6_src_ioctl(cmd, data));
425 case SIOCSNDFLUSH_IN6:
426 case SIOCSPFXFLUSH_IN6:
427 case SIOCSRTRFLUSH_IN6:
428 case SIOCSDEFIFACE_IN6:
429 case SIOCSIFINFO_FLAGS:
433 case OSIOCGIFINFO_IN6:
434 case SIOCGIFINFO_IN6:
437 case SIOCGNBRINFO_IN6:
438 case SIOCGDEFIFACE_IN6:
439 return (nd6_ioctl(cmd, data, ifp));
443 case SIOCSIFPREFIX_IN6:
444 case SIOCDIFPREFIX_IN6:
445 case SIOCAIFPREFIX_IN6:
446 case SIOCCIFPREFIX_IN6:
447 case SIOCSGIFPREFIX_IN6:
448 case SIOCGIFPREFIX_IN6:
450 "prefix ioctls are now invalidated. "
451 "please use ifconfig.\n");
459 return (scope6_set(ifp,
460 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
463 return (scope6_get(ifp,
464 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
467 return (scope6_get_default((struct scope6_id *)
468 ifr->ifr_ifru.ifru_scope_id));
479 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
483 * Find address for this interface, if it exists.
485 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
486 struct sockaddr_in6 *sa6 =
487 (struct sockaddr_in6 *)&ifra->ifra_addr;
489 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
490 if (sa6->sin6_addr.s6_addr16[1] == 0) {
491 /* link ID is not embedded by the user */
492 sa6->sin6_addr.s6_addr16[1] =
493 htons(ifp->if_index);
494 } else if (sa6->sin6_addr.s6_addr16[1] !=
495 htons(ifp->if_index)) {
496 return (EINVAL); /* link ID contradicts */
498 if (sa6->sin6_scope_id) {
499 if (sa6->sin6_scope_id !=
500 (u_int32_t)ifp->if_index)
502 sa6->sin6_scope_id = 0; /* XXX: good way? */
505 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
509 case SIOCSIFADDR_IN6:
510 case SIOCSIFDSTADDR_IN6:
511 case SIOCSIFNETMASK_IN6:
513 * Since IPv6 allows a node to assign multiple addresses
514 * on a single interface, SIOCSIFxxx ioctls are not suitable
515 * and should be unused.
517 /* we decided to obsolete this command (20000704) */
520 case SIOCDIFADDR_IN6:
522 * for IPv4, we look for existing in_ifaddr here to allow
523 * "ifconfig if0 delete" to remove first IPv4 address on the
524 * interface. For IPv6, as the spec allow multiple interface
525 * address from the day one, we consider "remove the first one"
526 * semantics to be not preferable.
529 return (EADDRNOTAVAIL);
531 case SIOCAIFADDR_IN6:
533 * We always require users to specify a valid IPv6 address for
534 * the corresponding operation.
536 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
537 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
538 return (EAFNOSUPPORT);
544 case SIOCGIFADDR_IN6:
545 /* This interface is basically deprecated. use SIOCGIFCONF. */
547 case SIOCGIFAFLAG_IN6:
548 case SIOCGIFNETMASK_IN6:
549 case SIOCGIFDSTADDR_IN6:
550 case SIOCGIFALIFETIME_IN6:
551 /* must think again about its semantics */
553 return (EADDRNOTAVAIL);
555 case SIOCSIFALIFETIME_IN6:
557 struct in6_addrlifetime *lt;
562 return (EADDRNOTAVAIL);
563 /* sanity for overflow - beware unsigned */
564 lt = &ifr->ifr_ifru.ifru_lifetime;
565 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
566 && lt->ia6t_vltime + time_second < time_second) {
569 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
570 && lt->ia6t_pltime + time_second < time_second) {
579 case SIOCGIFADDR_IN6:
580 ifr->ifr_addr = ia->ia_addr;
583 case SIOCGIFDSTADDR_IN6:
584 if (!(ifp->if_flags & IFF_POINTOPOINT))
587 * XXX: should we check if ifa_dstaddr is NULL and return
590 ifr->ifr_dstaddr = ia->ia_dstaddr;
593 case SIOCGIFNETMASK_IN6:
594 ifr->ifr_addr = ia->ia_prefixmask;
597 case SIOCGIFAFLAG_IN6:
598 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
601 case SIOCGIFSTAT_IN6:
604 bzero(&ifr->ifr_ifru.ifru_stat,
605 sizeof(ifr->ifr_ifru.ifru_stat));
606 ifr->ifr_ifru.ifru_stat =
607 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
610 case SIOCGIFSTAT_ICMP6:
611 bzero(&ifr->ifr_ifru.ifru_stat,
612 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
613 ifr->ifr_ifru.ifru_icmp6stat =
614 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
617 case SIOCGIFALIFETIME_IN6:
618 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
621 case SIOCSIFALIFETIME_IN6:
622 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
624 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
625 ia->ia6_lifetime.ia6t_expire =
626 time_second + ia->ia6_lifetime.ia6t_vltime;
628 ia->ia6_lifetime.ia6t_expire = 0;
629 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
630 ia->ia6_lifetime.ia6t_preferred =
631 time_second + ia->ia6_lifetime.ia6t_pltime;
633 ia->ia6_lifetime.ia6t_preferred = 0;
636 case SIOCAIFADDR_IN6:
638 int i, error = 0, iaIsNew;
639 struct nd_prefix pr0, *pr;
647 * first, make or update the interface address structure,
648 * and link it to the list.
650 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
654 * then, make the prefix on-link on the interface.
655 * XXX: we'd rather create the prefix before the address, but
656 * we need at least one address to install the corresponding
657 * interface route, so we configure the address first.
661 * convert mask to prefix length (prefixmask has already
662 * been validated in in6_update_ifa().
664 bzero(&pr0, sizeof(pr0));
666 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
668 if (pr0.ndpr_plen == 128)
669 break; /* we don't need to install a host route. */
670 pr0.ndpr_prefix = ifra->ifra_addr;
671 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
672 /* apply the mask for safety. */
673 for (i = 0; i < 4; i++) {
674 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
675 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
678 * XXX: since we don't have an API to set prefix (not address)
679 * lifetimes, we just use the same lifetimes as addresses.
680 * The (temporarily) installed lifetimes can be overridden by
681 * later advertised RAs (when accept_rtadv is non 0), which is
682 * an intended behavior.
684 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
686 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
687 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
688 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
690 /* add the prefix if there's one. */
691 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
693 * nd6_prelist_add will install the corresponding
696 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
699 log(LOG_ERR, "nd6_prelist_add succeeded but "
701 return (EINVAL); /* XXX panic here? */
704 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
706 /* XXX: this should not happen! */
707 log(LOG_ERR, "in6_control: addition succeeded, but"
710 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
711 ia->ia6_ndpr == NULL) { /* new autoconfed addr */
716 * If this is the first autoconf address from
717 * the prefix, create a temporary address
718 * as well (when specified).
720 if (ip6_use_tempaddr &&
721 pr->ndpr_refcnt == 1) {
723 if ((e = in6_tmpifadd(ia, 1)) != 0) {
724 log(LOG_NOTICE, "in6_control: "
725 "failed to create a "
726 "temporary address, "
734 * this might affect the status of autoconfigured
735 * addresses, that is, this address might make
736 * other addresses detached.
738 pfxlist_onlink_check();
740 if (error == 0 && ia) {
741 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
742 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
748 case SIOCDIFADDR_IN6:
751 struct nd_prefix pr0, *pr;
754 * If the address being deleted is the only one that owns
755 * the corresponding prefix, expire the prefix as well.
756 * XXX: theoretically, we don't have to warry about such
757 * relationship, since we separate the address management
758 * and the prefix management. We do this, however, to provide
759 * as much backward compatibility as possible in terms of
760 * the ioctl operation.
762 bzero(&pr0, sizeof(pr0));
764 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
766 if (pr0.ndpr_plen == 128)
768 pr0.ndpr_prefix = ia->ia_addr;
769 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
770 for (i = 0; i < 4; i++) {
771 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
772 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
775 * The logic of the following condition is a bit complicated.
776 * We expire the prefix when
777 * 1. the address obeys autoconfiguration and it is the
778 * only owner of the associated prefix, or
779 * 2. the address does not obey autoconf and there is no
780 * other owner of the prefix.
782 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
783 (((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
784 pr->ndpr_refcnt == 1) ||
785 (!(ia->ia6_flags & IN6_IFF_AUTOCONF) &&
786 pr->ndpr_refcnt == 0))) {
787 pr->ndpr_expire = 1; /* XXX: just for expiration */
791 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
793 in6_purgeaddr(&ia->ia_ifa);
798 if (ifp == NULL || ifp->if_ioctl == 0)
800 ifnet_serialize_all(ifp);
801 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
802 ifnet_deserialize_all(ifp);
810 * Update parameters of an IPv6 interface address.
811 * If necessary, a new entry is created and linked into address chains.
812 * This function is separated from in6_control().
813 * XXX: should this be performed under splnet()?
816 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
817 struct in6_ifaddr *ia)
819 int error = 0, hostIsNew = 0, plen = -1;
820 struct in6_ifaddr *oia;
821 struct sockaddr_in6 dst6;
822 struct in6_addrlifetime *lt;
824 /* Validate parameters */
825 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
829 * The destination address for a p2p link must have a family
830 * of AF_UNSPEC or AF_INET6.
832 if ((ifp->if_flags & IFF_POINTOPOINT) &&
833 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
834 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
835 return (EAFNOSUPPORT);
837 * validate ifra_prefixmask. don't check sin6_family, netmask
838 * does not carry fields other than sin6_len.
840 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
843 * Because the IPv6 address architecture is classless, we require
844 * users to specify a (non 0) prefix length (mask) for a new address.
845 * We also require the prefix (when specified) mask is valid, and thus
846 * reject a non-consecutive mask.
848 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
850 if (ifra->ifra_prefixmask.sin6_len != 0) {
851 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
852 (u_char *)&ifra->ifra_prefixmask +
853 ifra->ifra_prefixmask.sin6_len);
859 * In this case, ia must not be NULL. We just use its prefix
862 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
865 * If the destination address on a p2p interface is specified,
866 * and the address is a scoped one, validate/set the scope
869 dst6 = ifra->ifra_dstaddr;
870 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
871 (dst6.sin6_family == AF_INET6)) {
874 if ((error = in6_recoverscope(&dst6,
875 &ifra->ifra_dstaddr.sin6_addr,
878 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
879 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
880 dst6.sin6_scope_id = scopeid;
881 else if (dst6.sin6_scope_id != scopeid)
882 return (EINVAL); /* scope ID mismatch. */
883 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
886 dst6.sin6_scope_id = 0; /* XXX */
889 * The destination address can be specified only for a p2p or a
890 * loopback interface. If specified, the corresponding prefix length
893 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
894 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
895 /* XXX: noisy message */
896 log(LOG_INFO, "in6_update_ifa: a destination can be "
897 "specified for a p2p or a loopback IF only\n");
902 * The following message seems noisy, but we dare to
903 * add it for diagnosis.
905 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
906 "when dstaddr is specified\n");
910 /* lifetime consistency check */
911 lt = &ifra->ifra_lifetime;
912 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
913 && lt->ia6t_vltime + time_second < time_second) {
916 if (lt->ia6t_vltime == 0) {
918 * the following log might be noisy, but this is a typical
919 * configuration mistake or a tool's bug.
922 "in6_update_ifa: valid lifetime is 0 for %s\n",
923 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
925 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
926 && lt->ia6t_pltime + time_second < time_second) {
931 * If this is a new address, allocate a new ifaddr and link it
937 * When in6_update_ifa() is called in a process of a received
938 * RA, it is called under splnet(). So, we should call malloc
941 ia = ifa_create(sizeof(*ia), M_NOWAIT);
944 /* Initialize the address and masks */
945 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
946 ia->ia_addr.sin6_family = AF_INET6;
947 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
948 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
950 * XXX: some functions expect that ifa_dstaddr is not
951 * NULL for p2p interfaces.
953 ia->ia_ifa.ifa_dstaddr
954 = (struct sockaddr *)&ia->ia_dstaddr;
956 ia->ia_ifa.ifa_dstaddr = NULL;
958 ia->ia_ifa.ifa_netmask
959 = (struct sockaddr *)&ia->ia_prefixmask;
962 if ((oia = in6_ifaddr) != NULL) {
963 for ( ; oia->ia_next; oia = oia->ia_next)
969 ifa_iflink(&ia->ia_ifa, ifp, 1);
972 /* set prefix mask */
973 if (ifra->ifra_prefixmask.sin6_len) {
975 * We prohibit changing the prefix length of an existing
977 * + such an operation should be rare in IPv6, and
978 * + the operation would confuse prefix management.
980 if (ia->ia_prefixmask.sin6_len &&
981 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
982 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
983 " existing (%s) address should not be changed\n",
984 ip6_sprintf(&ia->ia_addr.sin6_addr));
988 ia->ia_prefixmask = ifra->ifra_prefixmask;
992 * If a new destination address is specified, scrub the old one and
993 * install the new destination. Note that the interface must be
994 * p2p or loopback (see the check above.)
996 if (dst6.sin6_family == AF_INET6 &&
997 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
998 &ia->ia_dstaddr.sin6_addr)) {
1001 if ((ia->ia_flags & IFA_ROUTE) &&
1002 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1004 log(LOG_ERR, "in6_update_ifa: failed to remove "
1005 "a route to the old destination: %s\n",
1006 ip6_sprintf(&ia->ia_addr.sin6_addr));
1007 /* proceed anyway... */
1010 ia->ia_flags &= ~IFA_ROUTE;
1011 ia->ia_dstaddr = dst6;
1014 /* reset the interface and routing table appropriately. */
1015 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1019 * Beyond this point, we should call in6_purgeaddr upon an error,
1020 * not just go to unlink.
1023 #if 0 /* disable this mechanism for now */
1024 /* update prefix list */
1026 (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
1029 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
1030 if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
1031 in6_purgeaddr((struct ifaddr *)ia);
1037 if (ifp->if_flags & IFF_MULTICAST) {
1038 struct sockaddr_in6 mltaddr, mltmask;
1039 struct in6_multi *in6m;
1043 * join solicited multicast addr for new host id
1045 struct in6_addr llsol;
1046 bzero(&llsol, sizeof(struct in6_addr));
1047 llsol.s6_addr16[0] = htons(0xff02);
1048 llsol.s6_addr16[1] = htons(ifp->if_index);
1049 llsol.s6_addr32[1] = 0;
1050 llsol.s6_addr32[2] = htonl(1);
1051 llsol.s6_addr32[3] =
1052 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1053 llsol.s6_addr8[12] = 0xff;
1054 in6_addmulti(&llsol, ifp, &error);
1057 "in6_update_ifa: addmulti failed for "
1058 "%s on %s (errno=%d)\n",
1059 ip6_sprintf(&llsol), if_name(ifp),
1061 in6_purgeaddr((struct ifaddr *)ia);
1066 bzero(&mltmask, sizeof(mltmask));
1067 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1068 mltmask.sin6_family = AF_INET6;
1069 mltmask.sin6_addr = in6mask32;
1072 * join link-local all-nodes address
1074 bzero(&mltaddr, sizeof(mltaddr));
1075 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1076 mltaddr.sin6_family = AF_INET6;
1077 mltaddr.sin6_addr = kin6addr_linklocal_allnodes;
1078 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1080 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1082 rtrequest_global(RTM_ADD,
1083 (struct sockaddr *)&mltaddr,
1084 (struct sockaddr *)&ia->ia_addr,
1085 (struct sockaddr *)&mltmask,
1086 RTF_UP|RTF_CLONING); /* xxx */
1087 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1090 "in6_update_ifa: addmulti failed for "
1091 "%s on %s (errno=%d)\n",
1092 ip6_sprintf(&mltaddr.sin6_addr),
1093 if_name(ifp), error);
1098 * join node information group address
1100 #define hostnamelen strlen(hostname)
1101 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1103 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1104 if (in6m == NULL && ia != NULL) {
1105 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1107 log(LOG_WARNING, "in6_update_ifa: "
1108 "addmulti failed for "
1109 "%s on %s (errno=%d)\n",
1110 ip6_sprintf(&mltaddr.sin6_addr),
1111 if_name(ifp), error);
1118 * join node-local all-nodes address, on loopback.
1119 * XXX: since "node-local" is obsoleted by interface-local,
1120 * we have to join the group on every interface with
1121 * some interface-boundary restriction.
1123 if (ifp->if_flags & IFF_LOOPBACK) {
1124 struct in6_ifaddr *ia_loop;
1126 struct in6_addr loop6 = kin6addr_loopback;
1127 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1129 mltaddr.sin6_addr = kin6addr_nodelocal_allnodes;
1131 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1132 if (in6m == NULL && ia_loop != NULL) {
1133 rtrequest_global(RTM_ADD,
1134 (struct sockaddr *)&mltaddr,
1135 (struct sockaddr *)&ia_loop->ia_addr,
1136 (struct sockaddr *)&mltmask,
1138 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1140 log(LOG_WARNING, "in6_update_ifa: "
1141 "addmulti failed for %s on %s "
1143 ip6_sprintf(&mltaddr.sin6_addr),
1144 if_name(ifp), error);
1150 ia->ia6_flags = ifra->ifra_flags;
1151 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1152 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1154 ia->ia6_lifetime = ifra->ifra_lifetime;
1156 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1157 ia->ia6_lifetime.ia6t_expire =
1158 time_second + ia->ia6_lifetime.ia6t_vltime;
1160 ia->ia6_lifetime.ia6t_expire = 0;
1161 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1162 ia->ia6_lifetime.ia6t_preferred =
1163 time_second + ia->ia6_lifetime.ia6t_pltime;
1165 ia->ia6_lifetime.ia6t_preferred = 0;
1168 * Perform DAD, if needed.
1169 * XXX It may be of use, if we can administratively
1172 if (in6if_do_dad(ifp) && !(ifra->ifra_flags & IN6_IFF_NODAD)) {
1173 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1174 nd6_dad_start((struct ifaddr *)ia, NULL);
1181 * XXX: if a change of an existing address failed, keep the entry
1185 in6_unlink_ifa(ia, ifp);
1190 in6_purgeaddr(struct ifaddr *ifa)
1192 struct ifnet *ifp = ifa->ifa_ifp;
1193 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1195 /* stop DAD processing */
1199 * delete route to the destination of the address being purged.
1200 * The interface must be p2p or loopback in this case.
1202 if ((ia->ia_flags & IFA_ROUTE) && ia->ia_dstaddr.sin6_len != 0) {
1205 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1207 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1208 "a route to the p2p destination: %s on %s, "
1210 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1212 /* proceed anyway... */
1215 ia->ia_flags &= ~IFA_ROUTE;
1218 /* Remove ownaddr's loopback rtentry, if it exists. */
1219 in6_ifremloop(&(ia->ia_ifa));
1221 if (ifp->if_flags & IFF_MULTICAST) {
1223 * delete solicited multicast addr for deleting host id
1225 struct in6_multi *in6m;
1226 struct in6_addr llsol;
1227 bzero(&llsol, sizeof(struct in6_addr));
1228 llsol.s6_addr16[0] = htons(0xff02);
1229 llsol.s6_addr16[1] = htons(ifp->if_index);
1230 llsol.s6_addr32[1] = 0;
1231 llsol.s6_addr32[2] = htonl(1);
1232 llsol.s6_addr32[3] =
1233 ia->ia_addr.sin6_addr.s6_addr32[3];
1234 llsol.s6_addr8[12] = 0xff;
1236 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1241 in6_unlink_ifa(ia, ifp);
1245 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1248 struct in6_ifaddr *oia;
1252 ifa_ifunlink(&ia->ia_ifa, ifp);
1255 if (oia == (ia = in6_ifaddr))
1256 in6_ifaddr = ia->ia_next;
1258 while (ia->ia_next && (ia->ia_next != oia))
1261 ia->ia_next = oia->ia_next;
1264 kprintf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1268 if (oia->ia6_ifpr) { /* check for safety */
1269 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1270 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1271 in6_prefix_remove_ifid(iilen, oia);
1275 * When an autoconfigured address is being removed, release the
1276 * reference to the base prefix. Also, since the release might
1277 * affect the status of other (detached) addresses, call
1278 * pfxlist_onlink_check().
1280 if (oia->ia6_flags & IN6_IFF_AUTOCONF) {
1281 if (oia->ia6_ndpr == NULL) {
1282 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1283 "%p has no prefix\n", oia);
1285 oia->ia6_ndpr->ndpr_refcnt--;
1286 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1287 oia->ia6_ndpr = NULL;
1290 pfxlist_onlink_check();
1294 * release another refcnt for the link from in6_ifaddr.
1295 * Note that we should decrement the refcnt at least once for all *BSD.
1297 ifa_destroy(&oia->ia_ifa);
1303 in6_purgeif(struct ifnet *ifp)
1305 struct ifaddr_container *ifac, *next;
1307 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
1309 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
1311 in6_purgeaddr(ifac->ifa);
1319 * SIOCGLIFADDR: get first address. (?)
1320 * SIOCGLIFADDR with IFLR_PREFIX:
1321 * get first address that matches the specified prefix.
1322 * SIOCALIFADDR: add the specified address.
1323 * SIOCALIFADDR with IFLR_PREFIX:
1324 * add the specified prefix, filling hostid part from
1325 * the first link-local address. prefixlen must be <= 64.
1326 * SIOCDLIFADDR: delete the specified address.
1327 * SIOCDLIFADDR with IFLR_PREFIX:
1328 * delete the first address that matches the specified prefix.
1330 * EINVAL on invalid parameters
1331 * EADDRNOTAVAIL on prefix match failed/specified address not found
1332 * other values may be returned from in6_ioctl()
1334 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1335 * this is to accomodate address naming scheme other than RFC2374,
1337 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1338 * address encoding scheme. (see figure on page 8)
1341 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1342 struct ifnet *ifp, struct thread *td)
1344 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1345 struct sockaddr *sa;
1348 if (!data || !ifp) {
1349 panic("invalid argument to in6_lifaddr_ioctl");
1355 /* address must be specified on GET with IFLR_PREFIX */
1356 if (!(iflr->flags & IFLR_PREFIX))
1361 /* address must be specified on ADD and DELETE */
1362 sa = (struct sockaddr *)&iflr->addr;
1363 if (sa->sa_family != AF_INET6)
1365 if (sa->sa_len != sizeof(struct sockaddr_in6))
1367 /* XXX need improvement */
1368 sa = (struct sockaddr *)&iflr->dstaddr;
1369 if (sa->sa_family && sa->sa_family != AF_INET6)
1371 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1374 default: /* shouldn't happen */
1376 panic("invalid cmd to in6_lifaddr_ioctl");
1382 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1388 struct in6_aliasreq ifra;
1389 struct in6_addr *hostid = NULL;
1392 if (iflr->flags & IFLR_PREFIX) {
1394 struct sockaddr_in6 *sin6;
1397 * hostid is to fill in the hostid part of the
1398 * address. hostid points to the first link-local
1399 * address attached to the interface.
1401 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1403 return EADDRNOTAVAIL;
1404 hostid = IFA_IN6(ifa);
1406 /* prefixlen must be <= 64. */
1407 if (64 < iflr->prefixlen)
1409 prefixlen = iflr->prefixlen;
1411 /* hostid part must be zero. */
1412 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1413 if (sin6->sin6_addr.s6_addr32[2] != 0
1414 || sin6->sin6_addr.s6_addr32[3] != 0) {
1418 prefixlen = iflr->prefixlen;
1420 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1421 bzero(&ifra, sizeof(ifra));
1422 bcopy(iflr->iflr_name, ifra.ifra_name,
1423 sizeof(ifra.ifra_name));
1425 bcopy(&iflr->addr, &ifra.ifra_addr,
1426 ((struct sockaddr *)&iflr->addr)->sa_len);
1428 /* fill in hostid part */
1429 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1430 hostid->s6_addr32[2];
1431 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1432 hostid->s6_addr32[3];
1435 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1436 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1437 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1439 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1440 hostid->s6_addr32[2];
1441 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1442 hostid->s6_addr32[3];
1446 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1447 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1449 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1450 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1455 struct ifaddr_container *ifac;
1456 struct in6_ifaddr *ia;
1457 struct in6_addr mask, candidate, match;
1458 struct sockaddr_in6 *sin6;
1461 bzero(&mask, sizeof(mask));
1462 if (iflr->flags & IFLR_PREFIX) {
1463 /* lookup a prefix rather than address. */
1464 in6_len2mask(&mask, iflr->prefixlen);
1466 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1467 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1468 match.s6_addr32[0] &= mask.s6_addr32[0];
1469 match.s6_addr32[1] &= mask.s6_addr32[1];
1470 match.s6_addr32[2] &= mask.s6_addr32[2];
1471 match.s6_addr32[3] &= mask.s6_addr32[3];
1473 /* if you set extra bits, that's wrong */
1474 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1479 if (cmd == SIOCGLIFADDR) {
1480 /* on getting an address, take the 1st match */
1483 /* on deleting an address, do exact match */
1484 in6_len2mask(&mask, 128);
1485 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1486 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1492 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1493 struct ifaddr *ifa = ifac->ifa;
1495 if (ifa->ifa_addr->sa_family != AF_INET6)
1500 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1502 * XXX: this is adhoc, but is necessary to allow
1503 * a user to specify fe80::/64 (not /10) for a
1504 * link-local address.
1506 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1507 candidate.s6_addr16[1] = 0;
1508 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1509 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1510 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1511 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1512 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1516 return EADDRNOTAVAIL;
1517 ia = ifa2ia6(ifac->ifa);
1519 if (cmd == SIOCGLIFADDR) {
1520 struct sockaddr_in6 *s6;
1522 /* fill in the if_laddrreq structure */
1523 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1524 s6 = (struct sockaddr_in6 *)&iflr->addr;
1525 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1526 s6->sin6_addr.s6_addr16[1] = 0;
1528 in6_addr2scopeid(ifp, &s6->sin6_addr);
1530 if (ifp->if_flags & IFF_POINTOPOINT) {
1531 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1532 ia->ia_dstaddr.sin6_len);
1533 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1534 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1535 s6->sin6_addr.s6_addr16[1] = 0;
1537 in6_addr2scopeid(ifp,
1541 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1544 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1547 iflr->flags = ia->ia6_flags; /* XXX */
1551 struct in6_aliasreq ifra;
1553 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1554 bzero(&ifra, sizeof(ifra));
1555 bcopy(iflr->iflr_name, ifra.ifra_name,
1556 sizeof(ifra.ifra_name));
1558 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1559 ia->ia_addr.sin6_len);
1560 if (ifp->if_flags & IFF_POINTOPOINT)
1561 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1562 ia->ia_dstaddr.sin6_len);
1564 bzero(&ifra.ifra_dstaddr,
1565 sizeof(ifra.ifra_dstaddr));
1566 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1567 ia->ia_prefixmask.sin6_len);
1569 ifra.ifra_flags = ia->ia6_flags;
1570 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1576 return EOPNOTSUPP; /* just for safety */
1580 * Initialize an interface's intetnet6 address
1581 * and routing table entry.
1584 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6,
1587 int error = 0, plen, ifacount = 0;
1588 struct ifaddr_container *ifac;
1591 * Give the interface a chance to initialize
1592 * if this is its first address,
1593 * and to validate the address if necessary.
1595 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1596 if (ifac->ifa->ifa_addr == NULL)
1597 continue; /* just for safety */
1598 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
1603 ifnet_serialize_all(ifp);
1605 ia->ia_addr = *sin6;
1607 if (ifacount <= 1 && ifp->if_ioctl &&
1608 (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL))) {
1609 ifnet_deserialize_all(ifp);
1613 ifnet_deserialize_all(ifp);
1615 ia->ia_ifa.ifa_metric = ifp->if_metric;
1617 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1621 * If the destination address is specified for a point-to-point
1622 * interface, install a route to the destination as an interface
1625 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1626 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1627 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1628 RTF_UP | RTF_HOST)) != 0)
1630 ia->ia_flags |= IFA_ROUTE;
1634 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1636 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1639 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1641 /* set the rtrequest function to create llinfo */
1642 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1643 in6_ifaddloop(&(ia->ia_ifa));
1649 struct in6_multi_mship *
1650 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr, int *errorp)
1652 struct in6_multi_mship *imm;
1654 imm = kmalloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
1659 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
1660 if (!imm->i6mm_maddr) {
1661 /* *errorp is alrady set */
1662 kfree(imm, M_IPMADDR);
1669 in6_leavegroup(struct in6_multi_mship *imm)
1672 if (imm->i6mm_maddr)
1673 in6_delmulti(imm->i6mm_maddr);
1674 kfree(imm, M_IPMADDR);
1679 * Add an address to the list of IP6 multicast addresses for a
1683 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
1685 struct in6_multi *in6m;
1686 struct sockaddr_in6 sin6;
1687 struct ifmultiaddr *ifma;
1694 * Call generic routine to add membership or increment
1695 * refcount. It wants addresses in the form of a sockaddr,
1696 * so we build one here (being careful to zero the unused bytes).
1698 bzero(&sin6, sizeof sin6);
1699 sin6.sin6_family = AF_INET6;
1700 sin6.sin6_len = sizeof sin6;
1701 sin6.sin6_addr = *maddr6;
1702 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1709 * If ifma->ifma_protospec is null, then if_addmulti() created
1710 * a new record. Otherwise, we are done.
1712 if (ifma->ifma_protospec != 0) {
1714 return ifma->ifma_protospec;
1717 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1718 at interrupt time? If so, need to fix if_addmulti. XXX */
1719 in6m = (struct in6_multi *)kmalloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1725 bzero(in6m, sizeof *in6m);
1726 in6m->in6m_addr = *maddr6;
1727 in6m->in6m_ifp = ifp;
1728 in6m->in6m_ifma = ifma;
1729 ifma->ifma_protospec = in6m;
1730 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1733 * Let MLD6 know that we have joined a new IP6 multicast
1736 mld6_start_listening(in6m);
1742 * Delete a multicast address record.
1745 in6_delmulti(struct in6_multi *in6m)
1747 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1751 if (ifma->ifma_refcount == 1) {
1753 * No remaining claims to this record; let MLD6 know
1754 * that we are leaving the multicast group.
1756 mld6_stop_listening(in6m);
1757 ifma->ifma_protospec = 0;
1758 LIST_REMOVE(in6m, in6m_entry);
1759 kfree(in6m, M_IPMADDR);
1761 /* XXX - should be separate API for when we have an ifma? */
1762 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1767 * Find an IPv6 interface link-local address specific to an interface.
1770 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1772 struct ifaddr_container *ifac;
1774 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1775 struct ifaddr *ifa = ifac->ifa;
1777 if (ifa->ifa_addr == NULL)
1778 continue; /* just for safety */
1779 if (ifa->ifa_addr->sa_family != AF_INET6)
1781 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1782 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1789 return ((struct in6_ifaddr *)(ifac->ifa));
1796 * find the internet address corresponding to a given interface and address.
1799 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1801 struct ifaddr_container *ifac;
1803 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1804 struct ifaddr *ifa = ifac->ifa;
1806 if (ifa->ifa_addr == NULL)
1807 continue; /* just for safety */
1808 if (ifa->ifa_addr->sa_family != AF_INET6)
1810 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1814 return ((struct in6_ifaddr *)(ifac->ifa));
1820 * find the internet address on a given interface corresponding to a neighbor's
1824 in6ifa_ifplocaladdr(const struct ifnet *ifp, const struct in6_addr *addr)
1827 struct in6_ifaddr *ia;
1828 struct ifaddr_container *ifac;
1830 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1833 if (ifa->ifa_addr == NULL)
1834 continue; /* just for safety */
1835 if (ifa->ifa_addr->sa_family != AF_INET6)
1837 ia = (struct in6_ifaddr *)ifa;
1838 if (IN6_ARE_MASKED_ADDR_EQUAL(addr,
1839 &ia->ia_addr.sin6_addr,
1840 &ia->ia_prefixmask.sin6_addr))
1848 * Convert IP6 address to printable (loggable) representation.
1850 static char digits[] = "0123456789abcdef";
1851 static int ip6round = 0;
1853 ip6_sprintf(const struct in6_addr *addr)
1855 static char ip6buf[8][48];
1858 const u_short *a = (const u_short *)addr;
1862 ip6round = (ip6round + 1) & 7;
1863 cp = ip6buf[ip6round];
1865 for (i = 0; i < 8; i++) {
1876 if (dcolon == 0 && *(a + 1) == 0) {
1888 d = (const u_char *)a;
1889 *cp++ = digits[*d >> 4];
1890 *cp++ = digits[*d++ & 0xf];
1891 *cp++ = digits[*d >> 4];
1892 *cp++ = digits[*d & 0xf];
1897 return (ip6buf[ip6round]);
1901 in6_localaddr(struct in6_addr *in6)
1903 struct in6_ifaddr *ia;
1905 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1908 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1909 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1910 &ia->ia_prefixmask.sin6_addr))
1917 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1919 struct in6_ifaddr *ia;
1921 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1922 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1924 (ia->ia6_flags & IN6_IFF_DEPRECATED))
1925 return (1); /* true */
1927 /* XXX: do we still have to go thru the rest of the list? */
1930 return (0); /* false */
1934 * return length of part which dst and src are equal
1938 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1941 u_char *s = (u_char *)src, *d = (u_char *)dst;
1942 u_char *lim = s + 16, r;
1945 if ((r = (*d++ ^ *s++)) != 0) {
1956 /* XXX: to be scope conscious */
1958 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1960 int bytelen, bitlen;
1963 if (0 > len || len > 128) {
1964 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1972 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1974 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1975 p2->s6_addr[bytelen] >> (8 - bitlen))
1982 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1984 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1985 int bytelen, bitlen, i;
1988 if (0 > len || len > 128) {
1989 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1994 bzero(maskp, sizeof(*maskp));
1997 for (i = 0; i < bytelen; i++)
1998 maskp->s6_addr[i] = 0xff;
2000 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2004 * return the best address out of the same scope
2007 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst)
2009 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
2012 struct in6_ifaddr *ifa_best = NULL;
2016 kprintf("in6_ifawithscope: output interface is not specified\n");
2022 * We search for all addresses on all interfaces from the beginning.
2023 * Comparing an interface with the outgoing interface will be done
2024 * only at the final stage of tiebreaking.
2026 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2028 struct ifaddr_container *ifac;
2031 * We can never take an address that breaks the scope zone
2032 * of the destination.
2034 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
2037 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2038 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
2039 struct ifaddr *ifa = ifac->ifa;
2041 if (ifa->ifa_addr->sa_family != AF_INET6)
2044 src_scope = in6_addrscope(IFA_IN6(ifa));
2047 * Don't use an address before completing DAD
2048 * nor a duplicated address.
2050 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2054 /* XXX: is there any case to allow anycasts? */
2055 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2059 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2064 * If this is the first address we find,
2067 if (ifa_best == NULL)
2071 * ifa_best is never NULL beyond this line except
2072 * within the block labeled "replace".
2076 * If ifa_best has a smaller scope than dst and
2077 * the current address has a larger one than
2078 * (or equal to) dst, always replace ifa_best.
2079 * Also, if the current address has a smaller scope
2080 * than dst, ignore it unless ifa_best also has a
2082 * Consequently, after the two if-clause below,
2083 * the followings must be satisfied:
2084 * (scope(src) < scope(dst) &&
2085 * scope(best) < scope(dst))
2087 * (scope(best) >= scope(dst) &&
2088 * scope(src) >= scope(dst))
2090 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2091 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2092 goto replace; /* (A) */
2093 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2094 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2098 * A deprecated address SHOULD NOT be used in new
2099 * communications if an alternate (non-deprecated)
2100 * address is available and has sufficient scope.
2101 * RFC 2462, Section 5.5.4.
2103 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2104 IN6_IFF_DEPRECATED) {
2106 * Ignore any deprecated addresses if
2107 * specified by configuration.
2109 if (!ip6_use_deprecated)
2113 * If we have already found a non-deprecated
2114 * candidate, just ignore deprecated addresses.
2116 if (!(ifa_best->ia6_flags & IN6_IFF_DEPRECATED))
2121 * A non-deprecated address is always preferred
2122 * to a deprecated one regardless of scopes and
2123 * address matching (Note invariants ensured by the
2124 * conditions (A) and (B) above.)
2126 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2127 !(((struct in6_ifaddr *)ifa)->ia6_flags &
2128 IN6_IFF_DEPRECATED))
2132 * When we use temporary addresses described in
2133 * RFC 3041, we prefer temporary addresses to
2134 * public autoconf addresses. Again, note the
2135 * invariants from (A) and (B). Also note that we
2136 * don't have any preference between static addresses
2137 * and autoconf addresses (despite of whether or not
2138 * the latter is temporary or public.)
2140 if (ip6_use_tempaddr) {
2141 struct in6_ifaddr *ifat;
2143 ifat = (struct in6_ifaddr *)ifa;
2144 if ((ifa_best->ia6_flags &
2145 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2146 == IN6_IFF_AUTOCONF &&
2148 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2149 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2152 if ((ifa_best->ia6_flags &
2153 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2154 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2156 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2157 == IN6_IFF_AUTOCONF) {
2163 * At this point, we have two cases:
2164 * 1. we are looking at a non-deprecated address,
2165 * and ifa_best is also non-deprecated.
2166 * 2. we are looking at a deprecated address,
2167 * and ifa_best is also deprecated.
2168 * Also, we do not have to consider a case where
2169 * the scope of if_best is larger(smaller) than dst and
2170 * the scope of the current address is smaller(larger)
2171 * than dst. Such a case has already been covered.
2172 * Tiebreaking is done according to the following
2174 * - the scope comparison between the address and
2176 * - the scope comparison between the address and
2177 * ifa_best (bscopecmp)
2178 * - if the address match dst longer than ifa_best
2180 * - if the address is on the outgoing I/F (outI/F)
2182 * Roughly speaking, the selection policy is
2183 * - the most important item is scope. The same scope
2184 * is best. Then search for a larger scope.
2185 * Smaller scopes are the last resort.
2186 * - A deprecated address is chosen only when we have
2187 * no address that has an enough scope, but is
2188 * prefered to any addresses of smaller scopes
2189 * (this must be already done above.)
2190 * - addresses on the outgoing I/F are preferred to
2191 * ones on other interfaces if none of above
2192 * tiebreaks. In the table below, the column "bI"
2193 * means if the best_ifa is on the outgoing
2194 * interface, and the column "sI" means if the ifa
2195 * is on the outgoing interface.
2196 * - If there is no other reasons to choose one,
2197 * longest address match against dst is considered.
2199 * The precise decision table is as follows:
2200 * dscopecmp bscopecmp match bI oI | replace?
2201 * N/A equal N/A Y N | No (1)
2202 * N/A equal N/A N Y | Yes (2)
2203 * N/A equal larger N/A | Yes (3)
2204 * N/A equal !larger N/A | No (4)
2205 * larger larger N/A N/A | No (5)
2206 * larger smaller N/A N/A | Yes (6)
2207 * smaller larger N/A N/A | Yes (7)
2208 * smaller smaller N/A N/A | No (8)
2209 * equal smaller N/A N/A | Yes (9)
2210 * equal larger (already done at A above)
2212 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2213 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2215 if (bscopecmp == 0) {
2216 struct ifnet *bifp = ifa_best->ia_ifp;
2218 if (bifp == oifp && ifp != oifp) /* (1) */
2220 if (bifp != oifp && ifp == oifp) /* (2) */
2224 * Both bifp and ifp are on the outgoing
2225 * interface, or both two are on a different
2226 * interface from the outgoing I/F.
2227 * now we need address matching against dst
2230 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2231 matchcmp = tlen - blen;
2232 if (matchcmp > 0) /* (3) */
2236 if (dscopecmp > 0) {
2237 if (bscopecmp > 0) /* (5) */
2239 goto replace; /* (6) */
2241 if (dscopecmp < 0) {
2242 if (bscopecmp > 0) /* (7) */
2247 /* now dscopecmp must be 0 */
2249 goto replace; /* (9) */
2252 ifa_best = (struct in6_ifaddr *)ifa;
2253 blen = tlen >= 0 ? tlen :
2254 in6_matchlen(IFA_IN6(ifa), dst);
2255 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2259 /* count statistics for future improvements */
2260 if (ifa_best == NULL)
2261 ip6stat.ip6s_sources_none++;
2263 if (oifp == ifa_best->ia_ifp)
2264 ip6stat.ip6s_sources_sameif[best_scope]++;
2266 ip6stat.ip6s_sources_otherif[best_scope]++;
2268 if (best_scope == dst_scope)
2269 ip6stat.ip6s_sources_samescope[best_scope]++;
2271 ip6stat.ip6s_sources_otherscope[best_scope]++;
2273 if (ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2274 ip6stat.ip6s_sources_deprecated[best_scope]++;
2281 * return the best address out of the same scope. if no address was
2282 * found, return the first valid address from designated IF.
2285 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2287 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2288 struct ifaddr_container *ifac;
2289 struct in6_ifaddr *besta = 0;
2290 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2292 dep[0] = dep[1] = NULL;
2295 * We first look for addresses in the same scope.
2296 * If there is one, return it.
2297 * If two or more, return one which matches the dst longest.
2298 * If none, return one of global addresses assigned other ifs.
2300 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2301 struct ifaddr *ifa = ifac->ifa;
2303 if (ifa->ifa_addr->sa_family != AF_INET6)
2305 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2306 continue; /* XXX: is there any case to allow anycast? */
2307 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2308 continue; /* don't use this interface */
2309 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2311 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2312 if (ip6_use_deprecated)
2313 dep[0] = (struct in6_ifaddr *)ifa;
2317 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2319 * call in6_matchlen() as few as possible
2323 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2324 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2327 besta = (struct in6_ifaddr *)ifa;
2330 besta = (struct in6_ifaddr *)ifa;
2336 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2337 struct ifaddr *ifa = ifac->ifa;
2339 if (ifa->ifa_addr->sa_family != AF_INET6)
2341 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2342 continue; /* XXX: is there any case to allow anycast? */
2343 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2344 continue; /* don't use this interface */
2345 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2347 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2348 if (ip6_use_deprecated)
2349 dep[1] = (struct in6_ifaddr *)ifa;
2353 return (struct in6_ifaddr *)ifa;
2356 /* use the last-resort values, that are, deprecated addresses */
2366 * perform DAD when interface becomes IFF_UP.
2369 in6_if_up(struct ifnet *ifp)
2371 struct ifaddr_container *ifac;
2372 struct in6_ifaddr *ia;
2373 int dad_delay; /* delay ticks before DAD output */
2376 * special cases, like 6to4, are handled in in6_ifattach
2378 in6_ifattach(ifp, NULL);
2381 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2382 struct ifaddr *ifa = ifac->ifa;
2384 if (ifa->ifa_addr->sa_family != AF_INET6)
2386 ia = (struct in6_ifaddr *)ifa;
2387 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2388 nd6_dad_start(ifa, &dad_delay);
2393 in6if_do_dad(struct ifnet *ifp)
2395 if (ifp->if_flags & IFF_LOOPBACK)
2398 switch (ifp->if_type) {
2404 * These interfaces do not have the IFF_LOOPBACK flag,
2405 * but loop packets back. We do not have to do DAD on such
2406 * interfaces. We should even omit it, because loop-backed
2407 * NS would confuse the DAD procedure.
2412 * Our DAD routine requires the interface up and running.
2413 * However, some interfaces can be up before the RUNNING
2414 * status. Additionaly, users may try to assign addresses
2415 * before the interface becomes up (or running).
2416 * We simply skip DAD in such a case as a work around.
2417 * XXX: we should rather mark "tentative" on such addresses,
2418 * and do DAD after the interface becomes ready.
2420 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2421 (IFF_UP|IFF_RUNNING))
2429 * Calculate max IPv6 MTU through all the interfaces and store it
2435 unsigned long maxmtu = 0;
2438 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2440 /* this function can be called during ifnet initialization */
2441 if (!ifp->if_afdata[AF_INET6])
2443 if (!(ifp->if_flags & IFF_LOOPBACK) &&
2444 ND_IFINFO(ifp)->linkmtu > maxmtu)
2445 maxmtu = ND_IFINFO(ifp)->linkmtu;
2447 if (maxmtu) /* update only when maxmtu is positive */
2448 in6_maxmtu = maxmtu;
2452 in6_domifattach(struct ifnet *ifp)
2454 struct in6_ifextra *ext;
2456 ext = (struct in6_ifextra *)kmalloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2457 bzero(ext, sizeof(*ext));
2459 ext->in6_ifstat = (struct in6_ifstat *)kmalloc(sizeof(struct in6_ifstat),
2460 M_IFADDR, M_WAITOK);
2461 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2464 (struct icmp6_ifstat *)kmalloc(sizeof(struct icmp6_ifstat),
2465 M_IFADDR, M_WAITOK);
2466 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2468 ext->nd_ifinfo = nd6_ifattach(ifp);
2469 ext->scope6_id = scope6_ifattach(ifp);
2474 in6_domifdetach(struct ifnet *ifp, void *aux)
2476 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2477 scope6_ifdetach(ext->scope6_id);
2478 nd6_ifdetach(ext->nd_ifinfo);
2479 kfree(ext->in6_ifstat, M_IFADDR);
2480 kfree(ext->icmp6_ifstat, M_IFADDR);
2481 kfree(ext, M_IFADDR);
2485 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2486 * v4 mapped addr or v4 compat addr
2489 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2491 bzero(sin, sizeof(*sin));
2492 sin->sin_len = sizeof(struct sockaddr_in);
2493 sin->sin_family = AF_INET;
2494 sin->sin_port = sin6->sin6_port;
2495 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2498 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2500 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2502 bzero(sin6, sizeof(*sin6));
2503 sin6->sin6_len = sizeof(struct sockaddr_in6);
2504 sin6->sin6_family = AF_INET6;
2505 sin6->sin6_port = sin->sin_port;
2506 sin6->sin6_addr.s6_addr32[0] = 0;
2507 sin6->sin6_addr.s6_addr32[1] = 0;
2508 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2509 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2512 /* Convert sockaddr_in6 into sockaddr_in. */
2514 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2516 struct sockaddr_in *sin_p;
2517 struct sockaddr_in6 sin6;
2520 * Save original sockaddr_in6 addr and convert it
2523 sin6 = *(struct sockaddr_in6 *)nam;
2524 sin_p = (struct sockaddr_in *)nam;
2525 in6_sin6_2_sin(sin_p, &sin6);
2528 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2530 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2532 struct sockaddr_in *sin_p;
2533 struct sockaddr_in6 *sin6_p;
2535 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2537 sin_p = (struct sockaddr_in *)*nam;
2538 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2539 FREE(*nam, M_SONAME);
2540 *nam = (struct sockaddr *)sin6_p;