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>
84 #include <sys/thread2.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
89 #include <net/if_dl.h>
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #include <netinet/if_ether.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/ip.h>
96 #include <netinet/in_pcb.h>
98 #include <netinet/ip6.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/nd6.h>
101 #include <netinet6/mld6_var.h>
102 #include <netinet6/ip6_mroute.h>
103 #include <netinet6/in6_ifattach.h>
104 #include <netinet6/scope6_var.h>
105 #include <netinet6/in6_pcb.h>
106 #include <netinet6/in6_var.h>
108 #include <net/net_osdep.h>
111 * Definitions of some costant IP6 addresses.
113 const struct in6_addr kin6addr_any = IN6ADDR_ANY_INIT;
114 const struct in6_addr kin6addr_loopback = IN6ADDR_LOOPBACK_INIT;
115 const struct in6_addr kin6addr_nodelocal_allnodes =
116 IN6ADDR_NODELOCAL_ALLNODES_INIT;
117 const struct in6_addr kin6addr_linklocal_allnodes =
118 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
119 const struct in6_addr kin6addr_linklocal_allrouters =
120 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
122 const struct in6_addr in6mask0 = IN6MASK0;
123 const struct in6_addr in6mask32 = IN6MASK32;
124 const struct in6_addr in6mask64 = IN6MASK64;
125 const struct in6_addr in6mask96 = IN6MASK96;
126 const struct in6_addr in6mask128 = IN6MASK128;
128 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
129 0, 0, IN6ADDR_ANY_INIT, 0};
131 static int in6_lifaddr_ioctl (struct socket *, u_long, caddr_t,
132 struct ifnet *, struct thread *);
133 static int in6_ifinit (struct ifnet *, struct in6_ifaddr *,
134 struct sockaddr_in6 *, int);
135 static void in6_unlink_ifa (struct in6_ifaddr *, struct ifnet *);
136 static void in6_ifloop_request_callback(int, int, struct rt_addrinfo *, struct rtentry *, void *);
138 struct in6_multihead in6_multihead; /* XXX BSS initialization */
140 int (*faithprefix_p)(struct in6_addr *);
143 * Subroutine for in6_ifaddloop() and in6_ifremloop().
144 * This routine does actual work.
147 in6_ifloop_request(int cmd, struct ifaddr *ifa)
149 struct sockaddr_in6 all1_sa;
150 struct rt_addrinfo rtinfo;
153 bzero(&all1_sa, sizeof(all1_sa));
154 all1_sa.sin6_family = AF_INET6;
155 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
156 all1_sa.sin6_addr = in6mask128;
159 * We specify the address itself as the gateway, and set the
160 * RTF_LLINFO flag, so that the corresponding host route would have
161 * the flag, and thus applications that assume traditional behavior
162 * would be happy. Note that we assume the caller of the function
163 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
164 * which changes the outgoing interface to the loopback interface.
166 bzero(&rtinfo, sizeof(struct rt_addrinfo));
167 rtinfo.rti_info[RTAX_DST] = ifa->ifa_addr;
168 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
169 rtinfo.rti_info[RTAX_NETMASK] = (struct sockaddr *)&all1_sa;
170 rtinfo.rti_flags = RTF_UP|RTF_HOST|RTF_LLINFO;
172 error = rtrequest1_global(cmd, &rtinfo,
173 in6_ifloop_request_callback, ifa);
175 log(LOG_ERR, "in6_ifloop_request: "
176 "%s operation failed for %s (errno=%d)\n",
177 cmd == RTM_ADD ? "ADD" : "DELETE",
178 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
184 in6_ifloop_request_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
185 struct rtentry *rt, void *arg)
187 struct ifaddr *ifa = arg;
193 * Make sure rt_ifa be equal to IFA, the second argument of the
195 * We need this because when we refer to rt_ifa->ia6_flags in
196 * ip6_input, we assume that the rt_ifa points to the address instead
197 * of the loopback address.
199 if (cmd == RTM_ADD && rt && ifa != rt->rt_ifa) {
208 * Report the addition/removal of the address to the routing socket.
209 * XXX: since we called rtinit for a p2p interface with a destination,
210 * we end up reporting twice in such a case. Should we rather
211 * omit the second report?
215 rt_newaddrmsg(cmd, ifa, error, rt);
216 if (cmd == RTM_DELETE) {
217 if (rt->rt_refcnt == 0) {
224 /* no way to return any new error */
229 * Add ownaddr as loopback rtentry. We previously add the route only if
230 * necessary (ex. on a p2p link). However, since we now manage addresses
231 * separately from prefixes, we should always add the route. We can't
232 * rely on the cloning mechanism from the corresponding interface route
236 in6_ifaddloop(struct ifaddr *ifa)
240 /* If there is no loopback entry, allocate one. */
241 rt = rtpurelookup(ifa->ifa_addr);
242 if (rt == NULL || !(rt->rt_flags & RTF_HOST) ||
243 !(rt->rt_ifp->if_flags & IFF_LOOPBACK))
244 in6_ifloop_request(RTM_ADD, ifa);
250 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
254 in6_ifremloop(struct ifaddr *ifa)
256 struct in6_ifaddr *ia;
261 * Some of BSD variants do not remove cloned routes
262 * from an interface direct route, when removing the direct route
263 * (see comments in net/net_osdep.h). Even for variants that do remove
264 * cloned routes, they could fail to remove the cloned routes when
265 * we handle multple addresses that share a common prefix.
266 * So, we should remove the route corresponding to the deleted address
267 * regardless of the result of in6_is_ifloop_auto().
271 * Delete the entry only if exact one ifa exists. More than one ifa
272 * can exist if we assign a same single address to multiple
273 * (probably p2p) interfaces.
274 * XXX: we should avoid such a configuration in IPv6...
276 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
277 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
286 * Before deleting, check if a corresponding loopbacked host
287 * route surely exists. With this check, we can avoid to
288 * delete an interface direct route whose destination is same
289 * as the address being removed. This can happen when remofing
290 * a subnet-router anycast address on an interface attahced
291 * to a shared medium.
293 rt = rtpurelookup(ifa->ifa_addr);
294 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
295 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
297 in6_ifloop_request(RTM_DELETE, ifa);
303 in6_ifindex2scopeid(int idx)
306 struct sockaddr_in6 *sin6;
307 struct ifaddr_container *ifac;
309 if (idx < 0 || if_index < idx)
311 ifp = ifindex2ifnet[idx];
313 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link)
315 struct ifaddr *ifa = ifac->ifa;
317 if (ifa->ifa_addr->sa_family != AF_INET6)
319 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
320 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
321 return sin6->sin6_scope_id & 0xffff;
328 in6_mask2len(struct in6_addr *mask, u_char *lim0)
331 u_char *lim = lim0, *p;
334 lim0 - (u_char *)mask > sizeof(*mask)) /* ignore the scope_id part */
335 lim = (u_char *)mask + sizeof(*mask);
336 for (p = (u_char *)mask; p < lim; x++, p++) {
342 for (y = 0; y < 8; y++) {
343 if ((*p & (0x80 >> y)) == 0)
349 * when the limit pointer is given, do a stricter check on the
353 if (y != 0 && (*p & (0x00ff >> y)) != 0)
355 for (p = p + 1; p < lim; p++)
364 in6_len2mask(struct in6_addr *mask, int len)
368 bzero(mask, sizeof(*mask));
369 for (i = 0; i < len / 8; i++)
370 mask->s6_addr8[i] = 0xff;
372 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
375 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
376 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
379 in6_control(struct socket *so, u_long cmd, caddr_t data,
380 struct ifnet *ifp, struct thread *td)
382 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
383 struct in6_ifaddr *ia = NULL;
384 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
389 if (priv_check(td, PRIV_ROOT) == 0)
393 case SIOCGETSGCNT_IN6:
394 case SIOCGETMIFCNT_IN6:
395 return (mrt6_ioctl(cmd, data));
402 case SIOCSNDFLUSH_IN6:
403 case SIOCSPFXFLUSH_IN6:
404 case SIOCSRTRFLUSH_IN6:
405 case SIOCSDEFIFACE_IN6:
406 case SIOCSIFINFO_FLAGS:
410 case OSIOCGIFINFO_IN6:
411 case SIOCGIFINFO_IN6:
414 case SIOCGNBRINFO_IN6:
415 case SIOCGDEFIFACE_IN6:
416 return (nd6_ioctl(cmd, data, ifp));
420 case SIOCSIFPREFIX_IN6:
421 case SIOCDIFPREFIX_IN6:
422 case SIOCAIFPREFIX_IN6:
423 case SIOCCIFPREFIX_IN6:
424 case SIOCSGIFPREFIX_IN6:
425 case SIOCGIFPREFIX_IN6:
427 "prefix ioctls are now invalidated. "
428 "please use ifconfig.\n");
436 return (scope6_set(ifp,
437 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
440 return (scope6_get(ifp,
441 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
444 return (scope6_get_default((struct scope6_id *)
445 ifr->ifr_ifru.ifru_scope_id));
456 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
460 * Find address for this interface, if it exists.
462 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
463 struct sockaddr_in6 *sa6 =
464 (struct sockaddr_in6 *)&ifra->ifra_addr;
466 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
467 if (sa6->sin6_addr.s6_addr16[1] == 0) {
468 /* link ID is not embedded by the user */
469 sa6->sin6_addr.s6_addr16[1] =
470 htons(ifp->if_index);
471 } else if (sa6->sin6_addr.s6_addr16[1] !=
472 htons(ifp->if_index)) {
473 return (EINVAL); /* link ID contradicts */
475 if (sa6->sin6_scope_id) {
476 if (sa6->sin6_scope_id !=
477 (u_int32_t)ifp->if_index)
479 sa6->sin6_scope_id = 0; /* XXX: good way? */
482 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
486 case SIOCSIFADDR_IN6:
487 case SIOCSIFDSTADDR_IN6:
488 case SIOCSIFNETMASK_IN6:
490 * Since IPv6 allows a node to assign multiple addresses
491 * on a single interface, SIOCSIFxxx ioctls are not suitable
492 * and should be unused.
494 /* we decided to obsolete this command (20000704) */
497 case SIOCDIFADDR_IN6:
499 * for IPv4, we look for existing in_ifaddr here to allow
500 * "ifconfig if0 delete" to remove first IPv4 address on the
501 * interface. For IPv6, as the spec allow multiple interface
502 * address from the day one, we consider "remove the first one"
503 * semantics to be not preferable.
506 return (EADDRNOTAVAIL);
508 case SIOCAIFADDR_IN6:
510 * We always require users to specify a valid IPv6 address for
511 * the corresponding operation.
513 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
514 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
515 return (EAFNOSUPPORT);
521 case SIOCGIFADDR_IN6:
522 /* This interface is basically deprecated. use SIOCGIFCONF. */
524 case SIOCGIFAFLAG_IN6:
525 case SIOCGIFNETMASK_IN6:
526 case SIOCGIFDSTADDR_IN6:
527 case SIOCGIFALIFETIME_IN6:
528 /* must think again about its semantics */
530 return (EADDRNOTAVAIL);
532 case SIOCSIFALIFETIME_IN6:
534 struct in6_addrlifetime *lt;
539 return (EADDRNOTAVAIL);
540 /* sanity for overflow - beware unsigned */
541 lt = &ifr->ifr_ifru.ifru_lifetime;
542 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
543 && lt->ia6t_vltime + time_second < time_second) {
546 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
547 && lt->ia6t_pltime + time_second < time_second) {
556 case SIOCGIFADDR_IN6:
557 ifr->ifr_addr = ia->ia_addr;
560 case SIOCGIFDSTADDR_IN6:
561 if (!(ifp->if_flags & IFF_POINTOPOINT))
564 * XXX: should we check if ifa_dstaddr is NULL and return
567 ifr->ifr_dstaddr = ia->ia_dstaddr;
570 case SIOCGIFNETMASK_IN6:
571 ifr->ifr_addr = ia->ia_prefixmask;
574 case SIOCGIFAFLAG_IN6:
575 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
578 case SIOCGIFSTAT_IN6:
581 bzero(&ifr->ifr_ifru.ifru_stat,
582 sizeof(ifr->ifr_ifru.ifru_stat));
583 ifr->ifr_ifru.ifru_stat =
584 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
587 case SIOCGIFSTAT_ICMP6:
588 bzero(&ifr->ifr_ifru.ifru_stat,
589 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
590 ifr->ifr_ifru.ifru_icmp6stat =
591 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
594 case SIOCGIFALIFETIME_IN6:
595 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
598 case SIOCSIFALIFETIME_IN6:
599 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
601 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
602 ia->ia6_lifetime.ia6t_expire =
603 time_second + ia->ia6_lifetime.ia6t_vltime;
605 ia->ia6_lifetime.ia6t_expire = 0;
606 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
607 ia->ia6_lifetime.ia6t_preferred =
608 time_second + ia->ia6_lifetime.ia6t_pltime;
610 ia->ia6_lifetime.ia6t_preferred = 0;
613 case SIOCAIFADDR_IN6:
616 struct nd_prefix pr0, *pr;
619 * first, make or update the interface address structure,
620 * and link it to the list.
622 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
626 * then, make the prefix on-link on the interface.
627 * XXX: we'd rather create the prefix before the address, but
628 * we need at least one address to install the corresponding
629 * interface route, so we configure the address first.
633 * convert mask to prefix length (prefixmask has already
634 * been validated in in6_update_ifa().
636 bzero(&pr0, sizeof(pr0));
638 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
640 if (pr0.ndpr_plen == 128)
641 break; /* we don't need to install a host route. */
642 pr0.ndpr_prefix = ifra->ifra_addr;
643 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
644 /* apply the mask for safety. */
645 for (i = 0; i < 4; i++) {
646 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
647 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
650 * XXX: since we don't have an API to set prefix (not address)
651 * lifetimes, we just use the same lifetimes as addresses.
652 * The (temporarily) installed lifetimes can be overridden by
653 * later advertised RAs (when accept_rtadv is non 0), which is
654 * an intended behavior.
656 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
658 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
659 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
660 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
662 /* add the prefix if there's one. */
663 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
665 * nd6_prelist_add will install the corresponding
668 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
671 log(LOG_ERR, "nd6_prelist_add succeeded but "
673 return (EINVAL); /* XXX panic here? */
676 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
678 /* XXX: this should not happen! */
679 log(LOG_ERR, "in6_control: addition succeeded, but"
682 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
683 ia->ia6_ndpr == NULL) { /* new autoconfed addr */
688 * If this is the first autoconf address from
689 * the prefix, create a temporary address
690 * as well (when specified).
692 if (ip6_use_tempaddr &&
693 pr->ndpr_refcnt == 1) {
695 if ((e = in6_tmpifadd(ia, 1)) != 0) {
696 log(LOG_NOTICE, "in6_control: "
697 "failed to create a "
698 "temporary address, "
706 * this might affect the status of autoconfigured
707 * addresses, that is, this address might make
708 * other addresses detached.
710 pfxlist_onlink_check();
712 if (error == 0 && ia)
713 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
717 case SIOCDIFADDR_IN6:
720 struct nd_prefix pr0, *pr;
723 * If the address being deleted is the only one that owns
724 * the corresponding prefix, expire the prefix as well.
725 * XXX: theoretically, we don't have to warry about such
726 * relationship, since we separate the address management
727 * and the prefix management. We do this, however, to provide
728 * as much backward compatibility as possible in terms of
729 * the ioctl operation.
731 bzero(&pr0, sizeof(pr0));
733 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
735 if (pr0.ndpr_plen == 128)
737 pr0.ndpr_prefix = ia->ia_addr;
738 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
739 for (i = 0; i < 4; i++) {
740 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
741 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
744 * The logic of the following condition is a bit complicated.
745 * We expire the prefix when
746 * 1. the address obeys autoconfiguration and it is the
747 * only owner of the associated prefix, or
748 * 2. the address does not obey autoconf and there is no
749 * other owner of the prefix.
751 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
752 (((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
753 pr->ndpr_refcnt == 1) ||
754 (!(ia->ia6_flags & IN6_IFF_AUTOCONF) &&
755 pr->ndpr_refcnt == 0))) {
756 pr->ndpr_expire = 1; /* XXX: just for expiration */
760 in6_purgeaddr(&ia->ia_ifa);
761 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
766 if (ifp == NULL || ifp->if_ioctl == 0)
768 lwkt_serialize_enter(ifp->if_serializer);
769 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
770 lwkt_serialize_exit(ifp->if_serializer);
778 * Update parameters of an IPv6 interface address.
779 * If necessary, a new entry is created and linked into address chains.
780 * This function is separated from in6_control().
781 * XXX: should this be performed under splnet()?
784 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
785 struct in6_ifaddr *ia)
787 int error = 0, hostIsNew = 0, plen = -1;
788 struct in6_ifaddr *oia;
789 struct sockaddr_in6 dst6;
790 struct in6_addrlifetime *lt;
792 /* Validate parameters */
793 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
797 * The destination address for a p2p link must have a family
798 * of AF_UNSPEC or AF_INET6.
800 if ((ifp->if_flags & IFF_POINTOPOINT) &&
801 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
802 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
803 return (EAFNOSUPPORT);
805 * validate ifra_prefixmask. don't check sin6_family, netmask
806 * does not carry fields other than sin6_len.
808 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
811 * Because the IPv6 address architecture is classless, we require
812 * users to specify a (non 0) prefix length (mask) for a new address.
813 * We also require the prefix (when specified) mask is valid, and thus
814 * reject a non-consecutive mask.
816 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
818 if (ifra->ifra_prefixmask.sin6_len != 0) {
819 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
820 (u_char *)&ifra->ifra_prefixmask +
821 ifra->ifra_prefixmask.sin6_len);
827 * In this case, ia must not be NULL. We just use its prefix
830 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
833 * If the destination address on a p2p interface is specified,
834 * and the address is a scoped one, validate/set the scope
837 dst6 = ifra->ifra_dstaddr;
838 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
839 (dst6.sin6_family == AF_INET6)) {
842 if ((error = in6_recoverscope(&dst6,
843 &ifra->ifra_dstaddr.sin6_addr,
846 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
847 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
848 dst6.sin6_scope_id = scopeid;
849 else if (dst6.sin6_scope_id != scopeid)
850 return (EINVAL); /* scope ID mismatch. */
851 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
854 dst6.sin6_scope_id = 0; /* XXX */
857 * The destination address can be specified only for a p2p or a
858 * loopback interface. If specified, the corresponding prefix length
861 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
862 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
863 /* XXX: noisy message */
864 log(LOG_INFO, "in6_update_ifa: a destination can be "
865 "specified for a p2p or a loopback IF only\n");
870 * The following message seems noisy, but we dare to
871 * add it for diagnosis.
873 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
874 "when dstaddr is specified\n");
878 /* lifetime consistency check */
879 lt = &ifra->ifra_lifetime;
880 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
881 && lt->ia6t_vltime + time_second < time_second) {
884 if (lt->ia6t_vltime == 0) {
886 * the following log might be noisy, but this is a typical
887 * configuration mistake or a tool's bug.
890 "in6_update_ifa: valid lifetime is 0 for %s\n",
891 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
893 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
894 && lt->ia6t_pltime + time_second < time_second) {
899 * If this is a new address, allocate a new ifaddr and link it
905 * When in6_update_ifa() is called in a process of a received
906 * RA, it is called under splnet(). So, we should call malloc
909 ia = ifa_create(sizeof(*ia), M_NOWAIT);
912 /* Initialize the address and masks */
913 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
914 ia->ia_addr.sin6_family = AF_INET6;
915 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
916 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
918 * XXX: some functions expect that ifa_dstaddr is not
919 * NULL for p2p interfaces.
921 ia->ia_ifa.ifa_dstaddr
922 = (struct sockaddr *)&ia->ia_dstaddr;
924 ia->ia_ifa.ifa_dstaddr = NULL;
926 ia->ia_ifa.ifa_netmask
927 = (struct sockaddr *)&ia->ia_prefixmask;
930 if ((oia = in6_ifaddr) != NULL) {
931 for ( ; oia->ia_next; oia = oia->ia_next)
937 ifa_iflink(&ia->ia_ifa, ifp, 1);
940 /* set prefix mask */
941 if (ifra->ifra_prefixmask.sin6_len) {
943 * We prohibit changing the prefix length of an existing
945 * + such an operation should be rare in IPv6, and
946 * + the operation would confuse prefix management.
948 if (ia->ia_prefixmask.sin6_len &&
949 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
950 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
951 " existing (%s) address should not be changed\n",
952 ip6_sprintf(&ia->ia_addr.sin6_addr));
956 ia->ia_prefixmask = ifra->ifra_prefixmask;
960 * If a new destination address is specified, scrub the old one and
961 * install the new destination. Note that the interface must be
962 * p2p or loopback (see the check above.)
964 if (dst6.sin6_family == AF_INET6 &&
965 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
966 &ia->ia_dstaddr.sin6_addr)) {
969 if ((ia->ia_flags & IFA_ROUTE) &&
970 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
972 log(LOG_ERR, "in6_update_ifa: failed to remove "
973 "a route to the old destination: %s\n",
974 ip6_sprintf(&ia->ia_addr.sin6_addr));
975 /* proceed anyway... */
978 ia->ia_flags &= ~IFA_ROUTE;
979 ia->ia_dstaddr = dst6;
982 /* reset the interface and routing table appropriately. */
983 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
987 * Beyond this point, we should call in6_purgeaddr upon an error,
988 * not just go to unlink.
991 #if 0 /* disable this mechanism for now */
992 /* update prefix list */
994 (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
997 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
998 if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
999 in6_purgeaddr((struct ifaddr *)ia);
1005 if (ifp->if_flags & IFF_MULTICAST) {
1006 struct sockaddr_in6 mltaddr, mltmask;
1007 struct in6_multi *in6m;
1011 * join solicited multicast addr for new host id
1013 struct in6_addr llsol;
1014 bzero(&llsol, sizeof(struct in6_addr));
1015 llsol.s6_addr16[0] = htons(0xff02);
1016 llsol.s6_addr16[1] = htons(ifp->if_index);
1017 llsol.s6_addr32[1] = 0;
1018 llsol.s6_addr32[2] = htonl(1);
1019 llsol.s6_addr32[3] =
1020 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1021 llsol.s6_addr8[12] = 0xff;
1022 in6_addmulti(&llsol, ifp, &error);
1025 "in6_update_ifa: addmulti failed for "
1026 "%s on %s (errno=%d)\n",
1027 ip6_sprintf(&llsol), if_name(ifp),
1029 in6_purgeaddr((struct ifaddr *)ia);
1034 bzero(&mltmask, sizeof(mltmask));
1035 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1036 mltmask.sin6_family = AF_INET6;
1037 mltmask.sin6_addr = in6mask32;
1040 * join link-local all-nodes address
1042 bzero(&mltaddr, sizeof(mltaddr));
1043 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1044 mltaddr.sin6_family = AF_INET6;
1045 mltaddr.sin6_addr = kin6addr_linklocal_allnodes;
1046 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1048 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1050 rtrequest_global(RTM_ADD,
1051 (struct sockaddr *)&mltaddr,
1052 (struct sockaddr *)&ia->ia_addr,
1053 (struct sockaddr *)&mltmask,
1054 RTF_UP|RTF_CLONING); /* xxx */
1055 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1058 "in6_update_ifa: addmulti failed for "
1059 "%s on %s (errno=%d)\n",
1060 ip6_sprintf(&mltaddr.sin6_addr),
1061 if_name(ifp), error);
1066 * join node information group address
1068 #define hostnamelen strlen(hostname)
1069 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1071 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1072 if (in6m == NULL && ia != NULL) {
1073 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1075 log(LOG_WARNING, "in6_update_ifa: "
1076 "addmulti failed for "
1077 "%s on %s (errno=%d)\n",
1078 ip6_sprintf(&mltaddr.sin6_addr),
1079 if_name(ifp), error);
1086 * join node-local all-nodes address, on loopback.
1087 * XXX: since "node-local" is obsoleted by interface-local,
1088 * we have to join the group on every interface with
1089 * some interface-boundary restriction.
1091 if (ifp->if_flags & IFF_LOOPBACK) {
1092 struct in6_ifaddr *ia_loop;
1094 struct in6_addr loop6 = kin6addr_loopback;
1095 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1097 mltaddr.sin6_addr = kin6addr_nodelocal_allnodes;
1099 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1100 if (in6m == NULL && ia_loop != NULL) {
1101 rtrequest_global(RTM_ADD,
1102 (struct sockaddr *)&mltaddr,
1103 (struct sockaddr *)&ia_loop->ia_addr,
1104 (struct sockaddr *)&mltmask,
1106 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1108 log(LOG_WARNING, "in6_update_ifa: "
1109 "addmulti failed for %s on %s "
1111 ip6_sprintf(&mltaddr.sin6_addr),
1112 if_name(ifp), error);
1118 ia->ia6_flags = ifra->ifra_flags;
1119 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1120 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1122 ia->ia6_lifetime = ifra->ifra_lifetime;
1124 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1125 ia->ia6_lifetime.ia6t_expire =
1126 time_second + ia->ia6_lifetime.ia6t_vltime;
1128 ia->ia6_lifetime.ia6t_expire = 0;
1129 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1130 ia->ia6_lifetime.ia6t_preferred =
1131 time_second + ia->ia6_lifetime.ia6t_pltime;
1133 ia->ia6_lifetime.ia6t_preferred = 0;
1136 * Perform DAD, if needed.
1137 * XXX It may be of use, if we can administratively
1140 if (in6if_do_dad(ifp) && !(ifra->ifra_flags & IN6_IFF_NODAD)) {
1141 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1142 nd6_dad_start((struct ifaddr *)ia, NULL);
1149 * XXX: if a change of an existing address failed, keep the entry
1153 in6_unlink_ifa(ia, ifp);
1158 in6_purgeaddr(struct ifaddr *ifa)
1160 struct ifnet *ifp = ifa->ifa_ifp;
1161 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1163 /* stop DAD processing */
1167 * delete route to the destination of the address being purged.
1168 * The interface must be p2p or loopback in this case.
1170 if ((ia->ia_flags & IFA_ROUTE) && ia->ia_dstaddr.sin6_len != 0) {
1173 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1175 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1176 "a route to the p2p destination: %s on %s, "
1178 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1180 /* proceed anyway... */
1183 ia->ia_flags &= ~IFA_ROUTE;
1186 /* Remove ownaddr's loopback rtentry, if it exists. */
1187 in6_ifremloop(&(ia->ia_ifa));
1189 if (ifp->if_flags & IFF_MULTICAST) {
1191 * delete solicited multicast addr for deleting host id
1193 struct in6_multi *in6m;
1194 struct in6_addr llsol;
1195 bzero(&llsol, sizeof(struct in6_addr));
1196 llsol.s6_addr16[0] = htons(0xff02);
1197 llsol.s6_addr16[1] = htons(ifp->if_index);
1198 llsol.s6_addr32[1] = 0;
1199 llsol.s6_addr32[2] = htonl(1);
1200 llsol.s6_addr32[3] =
1201 ia->ia_addr.sin6_addr.s6_addr32[3];
1202 llsol.s6_addr8[12] = 0xff;
1204 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1209 in6_unlink_ifa(ia, ifp);
1213 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1216 struct in6_ifaddr *oia;
1220 ifa_ifunlink(&ia->ia_ifa, ifp);
1223 if (oia == (ia = in6_ifaddr))
1224 in6_ifaddr = ia->ia_next;
1226 while (ia->ia_next && (ia->ia_next != oia))
1229 ia->ia_next = oia->ia_next;
1232 kprintf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1236 if (oia->ia6_ifpr) { /* check for safety */
1237 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1238 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1239 in6_prefix_remove_ifid(iilen, oia);
1243 * When an autoconfigured address is being removed, release the
1244 * reference to the base prefix. Also, since the release might
1245 * affect the status of other (detached) addresses, call
1246 * pfxlist_onlink_check().
1248 if (oia->ia6_flags & IN6_IFF_AUTOCONF) {
1249 if (oia->ia6_ndpr == NULL) {
1250 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1251 "%p has no prefix\n", oia);
1253 oia->ia6_ndpr->ndpr_refcnt--;
1254 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1255 oia->ia6_ndpr = NULL;
1258 pfxlist_onlink_check();
1262 * release another refcnt for the link from in6_ifaddr.
1263 * Note that we should decrement the refcnt at least once for all *BSD.
1265 ifa_destroy(&oia->ia_ifa);
1271 in6_purgeif(struct ifnet *ifp)
1273 struct ifaddr_container *ifac, *next;
1275 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
1277 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
1279 in6_purgeaddr(ifac->ifa);
1287 * SIOCGLIFADDR: get first address. (?)
1288 * SIOCGLIFADDR with IFLR_PREFIX:
1289 * get first address that matches the specified prefix.
1290 * SIOCALIFADDR: add the specified address.
1291 * SIOCALIFADDR with IFLR_PREFIX:
1292 * add the specified prefix, filling hostid part from
1293 * the first link-local address. prefixlen must be <= 64.
1294 * SIOCDLIFADDR: delete the specified address.
1295 * SIOCDLIFADDR with IFLR_PREFIX:
1296 * delete the first address that matches the specified prefix.
1298 * EINVAL on invalid parameters
1299 * EADDRNOTAVAIL on prefix match failed/specified address not found
1300 * other values may be returned from in6_ioctl()
1302 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1303 * this is to accomodate address naming scheme other than RFC2374,
1305 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1306 * address encoding scheme. (see figure on page 8)
1309 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1310 struct ifnet *ifp, struct thread *td)
1312 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1313 struct sockaddr *sa;
1316 if (!data || !ifp) {
1317 panic("invalid argument to in6_lifaddr_ioctl");
1323 /* address must be specified on GET with IFLR_PREFIX */
1324 if (!(iflr->flags & IFLR_PREFIX))
1329 /* address must be specified on ADD and DELETE */
1330 sa = (struct sockaddr *)&iflr->addr;
1331 if (sa->sa_family != AF_INET6)
1333 if (sa->sa_len != sizeof(struct sockaddr_in6))
1335 /* XXX need improvement */
1336 sa = (struct sockaddr *)&iflr->dstaddr;
1337 if (sa->sa_family && sa->sa_family != AF_INET6)
1339 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1342 default: /* shouldn't happen */
1344 panic("invalid cmd to in6_lifaddr_ioctl");
1350 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1356 struct in6_aliasreq ifra;
1357 struct in6_addr *hostid = NULL;
1360 if (iflr->flags & IFLR_PREFIX) {
1362 struct sockaddr_in6 *sin6;
1365 * hostid is to fill in the hostid part of the
1366 * address. hostid points to the first link-local
1367 * address attached to the interface.
1369 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1371 return EADDRNOTAVAIL;
1372 hostid = IFA_IN6(ifa);
1374 /* prefixlen must be <= 64. */
1375 if (64 < iflr->prefixlen)
1377 prefixlen = iflr->prefixlen;
1379 /* hostid part must be zero. */
1380 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1381 if (sin6->sin6_addr.s6_addr32[2] != 0
1382 || sin6->sin6_addr.s6_addr32[3] != 0) {
1386 prefixlen = iflr->prefixlen;
1388 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1389 bzero(&ifra, sizeof(ifra));
1390 bcopy(iflr->iflr_name, ifra.ifra_name,
1391 sizeof(ifra.ifra_name));
1393 bcopy(&iflr->addr, &ifra.ifra_addr,
1394 ((struct sockaddr *)&iflr->addr)->sa_len);
1396 /* fill in hostid part */
1397 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1398 hostid->s6_addr32[2];
1399 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1400 hostid->s6_addr32[3];
1403 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1404 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1405 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1407 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1408 hostid->s6_addr32[2];
1409 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1410 hostid->s6_addr32[3];
1414 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1415 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1417 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1418 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1423 struct ifaddr_container *ifac;
1424 struct in6_ifaddr *ia;
1425 struct in6_addr mask, candidate, match;
1426 struct sockaddr_in6 *sin6;
1429 bzero(&mask, sizeof(mask));
1430 if (iflr->flags & IFLR_PREFIX) {
1431 /* lookup a prefix rather than address. */
1432 in6_len2mask(&mask, iflr->prefixlen);
1434 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1435 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1436 match.s6_addr32[0] &= mask.s6_addr32[0];
1437 match.s6_addr32[1] &= mask.s6_addr32[1];
1438 match.s6_addr32[2] &= mask.s6_addr32[2];
1439 match.s6_addr32[3] &= mask.s6_addr32[3];
1441 /* if you set extra bits, that's wrong */
1442 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1447 if (cmd == SIOCGLIFADDR) {
1448 /* on getting an address, take the 1st match */
1451 /* on deleting an address, do exact match */
1452 in6_len2mask(&mask, 128);
1453 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1454 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1460 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1461 struct ifaddr *ifa = ifac->ifa;
1463 if (ifa->ifa_addr->sa_family != AF_INET6)
1468 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1470 * XXX: this is adhoc, but is necessary to allow
1471 * a user to specify fe80::/64 (not /10) for a
1472 * link-local address.
1474 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1475 candidate.s6_addr16[1] = 0;
1476 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1477 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1478 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1479 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1480 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1484 return EADDRNOTAVAIL;
1485 ia = ifa2ia6(ifac->ifa);
1487 if (cmd == SIOCGLIFADDR) {
1488 struct sockaddr_in6 *s6;
1490 /* fill in the if_laddrreq structure */
1491 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1492 s6 = (struct sockaddr_in6 *)&iflr->addr;
1493 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1494 s6->sin6_addr.s6_addr16[1] = 0;
1496 in6_addr2scopeid(ifp, &s6->sin6_addr);
1498 if (ifp->if_flags & IFF_POINTOPOINT) {
1499 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1500 ia->ia_dstaddr.sin6_len);
1501 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1502 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1503 s6->sin6_addr.s6_addr16[1] = 0;
1505 in6_addr2scopeid(ifp,
1509 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1512 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1515 iflr->flags = ia->ia6_flags; /* XXX */
1519 struct in6_aliasreq ifra;
1521 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1522 bzero(&ifra, sizeof(ifra));
1523 bcopy(iflr->iflr_name, ifra.ifra_name,
1524 sizeof(ifra.ifra_name));
1526 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1527 ia->ia_addr.sin6_len);
1528 if (ifp->if_flags & IFF_POINTOPOINT)
1529 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1530 ia->ia_dstaddr.sin6_len);
1532 bzero(&ifra.ifra_dstaddr,
1533 sizeof(ifra.ifra_dstaddr));
1534 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1535 ia->ia_prefixmask.sin6_len);
1537 ifra.ifra_flags = ia->ia6_flags;
1538 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1544 return EOPNOTSUPP; /* just for safety */
1548 * Initialize an interface's intetnet6 address
1549 * and routing table entry.
1552 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6,
1555 int error = 0, plen, ifacount = 0;
1556 struct ifaddr_container *ifac;
1559 * Give the interface a chance to initialize
1560 * if this is its first address,
1561 * and to validate the address if necessary.
1563 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1564 if (ifac->ifa->ifa_addr == NULL)
1565 continue; /* just for safety */
1566 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
1571 lwkt_serialize_enter(ifp->if_serializer);
1573 ia->ia_addr = *sin6;
1575 if (ifacount <= 1 && ifp->if_ioctl &&
1576 (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia,
1577 (struct ucred *)NULL))) {
1578 lwkt_serialize_exit(ifp->if_serializer);
1581 lwkt_serialize_exit(ifp->if_serializer);
1583 ia->ia_ifa.ifa_metric = ifp->if_metric;
1585 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1589 * If the destination address is specified for a point-to-point
1590 * interface, install a route to the destination as an interface
1593 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1594 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1595 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1596 RTF_UP | RTF_HOST)) != 0)
1598 ia->ia_flags |= IFA_ROUTE;
1602 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1604 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1607 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1609 /* set the rtrequest function to create llinfo */
1610 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1611 in6_ifaddloop(&(ia->ia_ifa));
1617 struct in6_multi_mship *
1618 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr, int *errorp)
1620 struct in6_multi_mship *imm;
1622 imm = kmalloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
1627 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
1628 if (!imm->i6mm_maddr) {
1629 /* *errorp is alrady set */
1630 kfree(imm, M_IPMADDR);
1637 in6_leavegroup(struct in6_multi_mship *imm)
1640 if (imm->i6mm_maddr)
1641 in6_delmulti(imm->i6mm_maddr);
1642 kfree(imm, M_IPMADDR);
1647 * Add an address to the list of IP6 multicast addresses for a
1651 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
1653 struct in6_multi *in6m;
1654 struct sockaddr_in6 sin6;
1655 struct ifmultiaddr *ifma;
1662 * Call generic routine to add membership or increment
1663 * refcount. It wants addresses in the form of a sockaddr,
1664 * so we build one here (being careful to zero the unused bytes).
1666 bzero(&sin6, sizeof sin6);
1667 sin6.sin6_family = AF_INET6;
1668 sin6.sin6_len = sizeof sin6;
1669 sin6.sin6_addr = *maddr6;
1670 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1677 * If ifma->ifma_protospec is null, then if_addmulti() created
1678 * a new record. Otherwise, we are done.
1680 if (ifma->ifma_protospec != 0) {
1682 return ifma->ifma_protospec;
1685 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1686 at interrupt time? If so, need to fix if_addmulti. XXX */
1687 in6m = (struct in6_multi *)kmalloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1693 bzero(in6m, sizeof *in6m);
1694 in6m->in6m_addr = *maddr6;
1695 in6m->in6m_ifp = ifp;
1696 in6m->in6m_ifma = ifma;
1697 ifma->ifma_protospec = in6m;
1698 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1701 * Let MLD6 know that we have joined a new IP6 multicast
1704 mld6_start_listening(in6m);
1710 * Delete a multicast address record.
1713 in6_delmulti(struct in6_multi *in6m)
1715 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1719 if (ifma->ifma_refcount == 1) {
1721 * No remaining claims to this record; let MLD6 know
1722 * that we are leaving the multicast group.
1724 mld6_stop_listening(in6m);
1725 ifma->ifma_protospec = 0;
1726 LIST_REMOVE(in6m, in6m_entry);
1727 kfree(in6m, M_IPMADDR);
1729 /* XXX - should be separate API for when we have an ifma? */
1730 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1735 * Find an IPv6 interface link-local address specific to an interface.
1738 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1740 struct ifaddr_container *ifac;
1742 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1743 struct ifaddr *ifa = ifac->ifa;
1745 if (ifa->ifa_addr == NULL)
1746 continue; /* just for safety */
1747 if (ifa->ifa_addr->sa_family != AF_INET6)
1749 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1750 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1757 return ((struct in6_ifaddr *)(ifac->ifa));
1764 * find the internet address corresponding to a given interface and address.
1767 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1769 struct ifaddr_container *ifac;
1771 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1772 struct ifaddr *ifa = ifac->ifa;
1774 if (ifa->ifa_addr == NULL)
1775 continue; /* just for safety */
1776 if (ifa->ifa_addr->sa_family != AF_INET6)
1778 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1782 return ((struct in6_ifaddr *)(ifac->ifa));
1788 * find the internet address on a given interface corresponding to a neighbor's
1792 in6ifa_ifplocaladdr(const struct ifnet *ifp, const struct in6_addr *addr)
1795 struct in6_ifaddr *ia;
1796 struct ifaddr_container *ifac;
1798 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1801 if (ifa->ifa_addr == NULL)
1802 continue; /* just for safety */
1803 if (ifa->ifa_addr->sa_family != AF_INET6)
1805 ia = (struct in6_ifaddr *)ifa;
1806 if (IN6_ARE_MASKED_ADDR_EQUAL(addr,
1807 &ia->ia_addr.sin6_addr,
1808 &ia->ia_prefixmask.sin6_addr))
1816 * Convert IP6 address to printable (loggable) representation.
1818 static char digits[] = "0123456789abcdef";
1819 static int ip6round = 0;
1821 ip6_sprintf(const struct in6_addr *addr)
1823 static char ip6buf[8][48];
1826 const u_short *a = (const u_short *)addr;
1830 ip6round = (ip6round + 1) & 7;
1831 cp = ip6buf[ip6round];
1833 for (i = 0; i < 8; i++) {
1844 if (dcolon == 0 && *(a + 1) == 0) {
1856 d = (const u_char *)a;
1857 *cp++ = digits[*d >> 4];
1858 *cp++ = digits[*d++ & 0xf];
1859 *cp++ = digits[*d >> 4];
1860 *cp++ = digits[*d & 0xf];
1865 return (ip6buf[ip6round]);
1869 in6_localaddr(struct in6_addr *in6)
1871 struct in6_ifaddr *ia;
1873 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1876 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1877 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1878 &ia->ia_prefixmask.sin6_addr))
1885 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1887 struct in6_ifaddr *ia;
1889 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1890 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1892 (ia->ia6_flags & IN6_IFF_DEPRECATED))
1893 return (1); /* true */
1895 /* XXX: do we still have to go thru the rest of the list? */
1898 return (0); /* false */
1902 * return length of part which dst and src are equal
1906 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1909 u_char *s = (u_char *)src, *d = (u_char *)dst;
1910 u_char *lim = s + 16, r;
1913 if ((r = (*d++ ^ *s++)) != 0) {
1924 /* XXX: to be scope conscious */
1926 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1928 int bytelen, bitlen;
1931 if (0 > len || len > 128) {
1932 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1940 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1942 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1943 p2->s6_addr[bytelen] >> (8 - bitlen))
1950 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1952 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1953 int bytelen, bitlen, i;
1956 if (0 > len || len > 128) {
1957 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1962 bzero(maskp, sizeof(*maskp));
1965 for (i = 0; i < bytelen; i++)
1966 maskp->s6_addr[i] = 0xff;
1968 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1972 * return the best address out of the same scope
1975 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst)
1977 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
1980 struct in6_ifaddr *ifa_best = NULL;
1984 kprintf("in6_ifawithscope: output interface is not specified\n");
1990 * We search for all addresses on all interfaces from the beginning.
1991 * Comparing an interface with the outgoing interface will be done
1992 * only at the final stage of tiebreaking.
1994 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1996 struct ifaddr_container *ifac;
1999 * We can never take an address that breaks the scope zone
2000 * of the destination.
2002 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
2005 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2006 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
2007 struct ifaddr *ifa = ifac->ifa;
2009 if (ifa->ifa_addr->sa_family != AF_INET6)
2012 src_scope = in6_addrscope(IFA_IN6(ifa));
2015 * Don't use an address before completing DAD
2016 * nor a duplicated address.
2018 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2022 /* XXX: is there any case to allow anycasts? */
2023 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2027 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2032 * If this is the first address we find,
2035 if (ifa_best == NULL)
2039 * ifa_best is never NULL beyond this line except
2040 * within the block labeled "replace".
2044 * If ifa_best has a smaller scope than dst and
2045 * the current address has a larger one than
2046 * (or equal to) dst, always replace ifa_best.
2047 * Also, if the current address has a smaller scope
2048 * than dst, ignore it unless ifa_best also has a
2050 * Consequently, after the two if-clause below,
2051 * the followings must be satisfied:
2052 * (scope(src) < scope(dst) &&
2053 * scope(best) < scope(dst))
2055 * (scope(best) >= scope(dst) &&
2056 * scope(src) >= scope(dst))
2058 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2059 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2060 goto replace; /* (A) */
2061 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2062 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2066 * A deprecated address SHOULD NOT be used in new
2067 * communications if an alternate (non-deprecated)
2068 * address is available and has sufficient scope.
2069 * RFC 2462, Section 5.5.4.
2071 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2072 IN6_IFF_DEPRECATED) {
2074 * Ignore any deprecated addresses if
2075 * specified by configuration.
2077 if (!ip6_use_deprecated)
2081 * If we have already found a non-deprecated
2082 * candidate, just ignore deprecated addresses.
2084 if (!(ifa_best->ia6_flags & IN6_IFF_DEPRECATED))
2089 * A non-deprecated address is always preferred
2090 * to a deprecated one regardless of scopes and
2091 * address matching (Note invariants ensured by the
2092 * conditions (A) and (B) above.)
2094 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2095 !(((struct in6_ifaddr *)ifa)->ia6_flags &
2096 IN6_IFF_DEPRECATED))
2100 * When we use temporary addresses described in
2101 * RFC 3041, we prefer temporary addresses to
2102 * public autoconf addresses. Again, note the
2103 * invariants from (A) and (B). Also note that we
2104 * don't have any preference between static addresses
2105 * and autoconf addresses (despite of whether or not
2106 * the latter is temporary or public.)
2108 if (ip6_use_tempaddr) {
2109 struct in6_ifaddr *ifat;
2111 ifat = (struct in6_ifaddr *)ifa;
2112 if ((ifa_best->ia6_flags &
2113 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2114 == IN6_IFF_AUTOCONF &&
2116 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2117 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2120 if ((ifa_best->ia6_flags &
2121 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2122 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2124 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2125 == IN6_IFF_AUTOCONF) {
2131 * At this point, we have two cases:
2132 * 1. we are looking at a non-deprecated address,
2133 * and ifa_best is also non-deprecated.
2134 * 2. we are looking at a deprecated address,
2135 * and ifa_best is also deprecated.
2136 * Also, we do not have to consider a case where
2137 * the scope of if_best is larger(smaller) than dst and
2138 * the scope of the current address is smaller(larger)
2139 * than dst. Such a case has already been covered.
2140 * Tiebreaking is done according to the following
2142 * - the scope comparison between the address and
2144 * - the scope comparison between the address and
2145 * ifa_best (bscopecmp)
2146 * - if the address match dst longer than ifa_best
2148 * - if the address is on the outgoing I/F (outI/F)
2150 * Roughly speaking, the selection policy is
2151 * - the most important item is scope. The same scope
2152 * is best. Then search for a larger scope.
2153 * Smaller scopes are the last resort.
2154 * - A deprecated address is chosen only when we have
2155 * no address that has an enough scope, but is
2156 * prefered to any addresses of smaller scopes
2157 * (this must be already done above.)
2158 * - addresses on the outgoing I/F are preferred to
2159 * ones on other interfaces if none of above
2160 * tiebreaks. In the table below, the column "bI"
2161 * means if the best_ifa is on the outgoing
2162 * interface, and the column "sI" means if the ifa
2163 * is on the outgoing interface.
2164 * - If there is no other reasons to choose one,
2165 * longest address match against dst is considered.
2167 * The precise decision table is as follows:
2168 * dscopecmp bscopecmp match bI oI | replace?
2169 * N/A equal N/A Y N | No (1)
2170 * N/A equal N/A N Y | Yes (2)
2171 * N/A equal larger N/A | Yes (3)
2172 * N/A equal !larger N/A | No (4)
2173 * larger larger N/A N/A | No (5)
2174 * larger smaller N/A N/A | Yes (6)
2175 * smaller larger N/A N/A | Yes (7)
2176 * smaller smaller N/A N/A | No (8)
2177 * equal smaller N/A N/A | Yes (9)
2178 * equal larger (already done at A above)
2180 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2181 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2183 if (bscopecmp == 0) {
2184 struct ifnet *bifp = ifa_best->ia_ifp;
2186 if (bifp == oifp && ifp != oifp) /* (1) */
2188 if (bifp != oifp && ifp == oifp) /* (2) */
2192 * Both bifp and ifp are on the outgoing
2193 * interface, or both two are on a different
2194 * interface from the outgoing I/F.
2195 * now we need address matching against dst
2198 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2199 matchcmp = tlen - blen;
2200 if (matchcmp > 0) /* (3) */
2204 if (dscopecmp > 0) {
2205 if (bscopecmp > 0) /* (5) */
2207 goto replace; /* (6) */
2209 if (dscopecmp < 0) {
2210 if (bscopecmp > 0) /* (7) */
2215 /* now dscopecmp must be 0 */
2217 goto replace; /* (9) */
2220 ifa_best = (struct in6_ifaddr *)ifa;
2221 blen = tlen >= 0 ? tlen :
2222 in6_matchlen(IFA_IN6(ifa), dst);
2223 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2227 /* count statistics for future improvements */
2228 if (ifa_best == NULL)
2229 ip6stat.ip6s_sources_none++;
2231 if (oifp == ifa_best->ia_ifp)
2232 ip6stat.ip6s_sources_sameif[best_scope]++;
2234 ip6stat.ip6s_sources_otherif[best_scope]++;
2236 if (best_scope == dst_scope)
2237 ip6stat.ip6s_sources_samescope[best_scope]++;
2239 ip6stat.ip6s_sources_otherscope[best_scope]++;
2241 if (ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2242 ip6stat.ip6s_sources_deprecated[best_scope]++;
2249 * return the best address out of the same scope. if no address was
2250 * found, return the first valid address from designated IF.
2253 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2255 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2256 struct ifaddr_container *ifac;
2257 struct in6_ifaddr *besta = 0;
2258 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2260 dep[0] = dep[1] = NULL;
2263 * We first look for addresses in the same scope.
2264 * If there is one, return it.
2265 * If two or more, return one which matches the dst longest.
2266 * If none, return one of global addresses assigned other ifs.
2268 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2269 struct ifaddr *ifa = ifac->ifa;
2271 if (ifa->ifa_addr->sa_family != AF_INET6)
2273 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2274 continue; /* XXX: is there any case to allow anycast? */
2275 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2276 continue; /* don't use this interface */
2277 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2279 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2280 if (ip6_use_deprecated)
2281 dep[0] = (struct in6_ifaddr *)ifa;
2285 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2287 * call in6_matchlen() as few as possible
2291 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2292 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2295 besta = (struct in6_ifaddr *)ifa;
2298 besta = (struct in6_ifaddr *)ifa;
2304 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2305 struct ifaddr *ifa = ifac->ifa;
2307 if (ifa->ifa_addr->sa_family != AF_INET6)
2309 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2310 continue; /* XXX: is there any case to allow anycast? */
2311 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2312 continue; /* don't use this interface */
2313 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2315 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2316 if (ip6_use_deprecated)
2317 dep[1] = (struct in6_ifaddr *)ifa;
2321 return (struct in6_ifaddr *)ifa;
2324 /* use the last-resort values, that are, deprecated addresses */
2334 * perform DAD when interface becomes IFF_UP.
2337 in6_if_up(struct ifnet *ifp)
2339 struct ifaddr_container *ifac;
2340 struct in6_ifaddr *ia;
2341 int dad_delay; /* delay ticks before DAD output */
2344 * special cases, like 6to4, are handled in in6_ifattach
2346 in6_ifattach(ifp, NULL);
2349 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2350 struct ifaddr *ifa = ifac->ifa;
2352 if (ifa->ifa_addr->sa_family != AF_INET6)
2354 ia = (struct in6_ifaddr *)ifa;
2355 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2356 nd6_dad_start(ifa, &dad_delay);
2361 in6if_do_dad(struct ifnet *ifp)
2363 if (ifp->if_flags & IFF_LOOPBACK)
2366 switch (ifp->if_type) {
2372 * These interfaces do not have the IFF_LOOPBACK flag,
2373 * but loop packets back. We do not have to do DAD on such
2374 * interfaces. We should even omit it, because loop-backed
2375 * NS would confuse the DAD procedure.
2380 * Our DAD routine requires the interface up and running.
2381 * However, some interfaces can be up before the RUNNING
2382 * status. Additionaly, users may try to assign addresses
2383 * before the interface becomes up (or running).
2384 * We simply skip DAD in such a case as a work around.
2385 * XXX: we should rather mark "tentative" on such addresses,
2386 * and do DAD after the interface becomes ready.
2388 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2389 (IFF_UP|IFF_RUNNING))
2397 * Calculate max IPv6 MTU through all the interfaces and store it
2403 unsigned long maxmtu = 0;
2406 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2408 if (!(ifp->if_flags & IFF_LOOPBACK) &&
2409 ND_IFINFO(ifp)->linkmtu > maxmtu)
2410 maxmtu = ND_IFINFO(ifp)->linkmtu;
2412 if (maxmtu) /* update only when maxmtu is positive */
2413 in6_maxmtu = maxmtu;
2417 in6_domifattach(struct ifnet *ifp)
2419 struct in6_ifextra *ext;
2421 ext = (struct in6_ifextra *)kmalloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2422 bzero(ext, sizeof(*ext));
2424 ext->in6_ifstat = (struct in6_ifstat *)kmalloc(sizeof(struct in6_ifstat),
2425 M_IFADDR, M_WAITOK);
2426 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2429 (struct icmp6_ifstat *)kmalloc(sizeof(struct icmp6_ifstat),
2430 M_IFADDR, M_WAITOK);
2431 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2433 ext->nd_ifinfo = nd6_ifattach(ifp);
2434 ext->scope6_id = scope6_ifattach(ifp);
2439 in6_domifdetach(struct ifnet *ifp, void *aux)
2441 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2442 scope6_ifdetach(ext->scope6_id);
2443 nd6_ifdetach(ext->nd_ifinfo);
2444 kfree(ext->in6_ifstat, M_IFADDR);
2445 kfree(ext->icmp6_ifstat, M_IFADDR);
2446 kfree(ext, M_IFADDR);
2450 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2451 * v4 mapped addr or v4 compat addr
2454 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2456 bzero(sin, sizeof(*sin));
2457 sin->sin_len = sizeof(struct sockaddr_in);
2458 sin->sin_family = AF_INET;
2459 sin->sin_port = sin6->sin6_port;
2460 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2463 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2465 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2467 bzero(sin6, sizeof(*sin6));
2468 sin6->sin6_len = sizeof(struct sockaddr_in6);
2469 sin6->sin6_family = AF_INET6;
2470 sin6->sin6_port = sin->sin_port;
2471 sin6->sin6_addr.s6_addr32[0] = 0;
2472 sin6->sin6_addr.s6_addr32[1] = 0;
2473 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2474 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2477 /* Convert sockaddr_in6 into sockaddr_in. */
2479 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2481 struct sockaddr_in *sin_p;
2482 struct sockaddr_in6 sin6;
2485 * Save original sockaddr_in6 addr and convert it
2488 sin6 = *(struct sockaddr_in6 *)nam;
2489 sin_p = (struct sockaddr_in *)nam;
2490 in6_sin6_2_sin(sin_p, &sin6);
2493 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2495 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2497 struct sockaddr_in *sin_p;
2498 struct sockaddr_in6 *sin6_p;
2500 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2502 sin_p = (struct sockaddr_in *)*nam;
2503 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2504 FREE(*nam, M_SONAME);
2505 *nam = (struct sockaddr *)sin6_p;