1 /* $FreeBSD: src/sys/netinet6/in6.c,v 1.7.2.9 2002/04/28 05:40:26 suz Exp $ */
2 /* $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)in.c 8.2 (Berkeley) 11/15/93
65 #include "opt_inet6.h"
67 #include <sys/param.h>
68 #include <sys/errno.h>
69 #include <sys/malloc.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/sockio.h>
73 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/syslog.h>
81 #include <sys/thread2.h>
82 #include <sys/msgport2.h>
85 #include <net/if_types.h>
86 #include <net/route.h>
87 #include <net/if_dl.h>
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet/if_ether.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/in_pcb.h>
96 #include <netinet/ip6.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet6/nd6.h>
99 #include <netinet6/mld6_var.h>
100 #include <netinet6/ip6_mroute.h>
101 #include <netinet6/in6_ifattach.h>
102 #include <netinet6/scope6_var.h>
103 #include <netinet6/in6_pcb.h>
104 #include <netinet6/in6_var.h>
106 #include <net/net_osdep.h>
109 * Definitions of some costant IP6 addresses.
111 const struct in6_addr kin6addr_any = IN6ADDR_ANY_INIT;
112 const struct in6_addr kin6addr_loopback = IN6ADDR_LOOPBACK_INIT;
113 const struct in6_addr kin6addr_nodelocal_allnodes =
114 IN6ADDR_NODELOCAL_ALLNODES_INIT;
115 const struct in6_addr kin6addr_linklocal_allnodes =
116 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
117 const struct in6_addr kin6addr_linklocal_allrouters =
118 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
120 const struct in6_addr in6mask0 = IN6MASK0;
121 const struct in6_addr in6mask32 = IN6MASK32;
122 const struct in6_addr in6mask64 = IN6MASK64;
123 const struct in6_addr in6mask96 = IN6MASK96;
124 const struct in6_addr in6mask128 = IN6MASK128;
126 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
127 0, 0, IN6ADDR_ANY_INIT, 0};
129 static int in6_lifaddr_ioctl (struct socket *, u_long, caddr_t,
130 struct ifnet *, struct thread *);
131 static int in6_ifinit (struct ifnet *, struct in6_ifaddr *,
132 struct sockaddr_in6 *, int);
133 static void in6_unlink_ifa (struct in6_ifaddr *, struct ifnet *);
134 static void in6_ifloop_request_callback(int, int, struct rt_addrinfo *, struct rtentry *, void *);
136 struct in6_multihead in6_multihead; /* XXX BSS initialization */
138 int (*faithprefix_p)(struct in6_addr *);
141 * Subroutine for in6_ifaddloop() and in6_ifremloop().
142 * This routine does actual work.
145 in6_ifloop_request(int cmd, struct ifaddr *ifa)
147 struct sockaddr_in6 all1_sa;
148 struct rt_addrinfo rtinfo;
151 bzero(&all1_sa, sizeof(all1_sa));
152 all1_sa.sin6_family = AF_INET6;
153 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
154 all1_sa.sin6_addr = in6mask128;
157 * We specify the address itself as the gateway, and set the
158 * RTF_LLINFO flag, so that the corresponding host route would have
159 * the flag, and thus applications that assume traditional behavior
160 * would be happy. Note that we assume the caller of the function
161 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
162 * which changes the outgoing interface to the loopback interface.
164 bzero(&rtinfo, sizeof(struct rt_addrinfo));
165 rtinfo.rti_info[RTAX_DST] = ifa->ifa_addr;
166 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
167 rtinfo.rti_info[RTAX_NETMASK] = (struct sockaddr *)&all1_sa;
168 rtinfo.rti_flags = RTF_UP|RTF_HOST|RTF_LLINFO;
170 error = rtrequest1_global(cmd, &rtinfo,
171 in6_ifloop_request_callback, ifa, RTREQ_PRIO_NORM);
173 log(LOG_ERR, "in6_ifloop_request: "
174 "%s operation failed for %s (errno=%d)\n",
175 cmd == RTM_ADD ? "ADD" : "DELETE",
176 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
182 in6_ifloop_request_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
183 struct rtentry *rt, void *arg)
185 struct ifaddr *ifa = arg;
191 * Make sure rt_ifa be equal to IFA, the second argument of the
193 * We need this because when we refer to rt_ifa->ia6_flags in
194 * ip6_input, we assume that the rt_ifa points to the address instead
195 * of the loopback address.
197 if (cmd == RTM_ADD && rt && ifa != rt->rt_ifa) {
206 * Report the addition/removal of the address to the routing socket.
207 * XXX: since we called rtinit for a p2p interface with a destination,
208 * we end up reporting twice in such a case. Should we rather
209 * omit the second report?
213 rt_newaddrmsg(cmd, ifa, error, rt);
214 if (cmd == RTM_DELETE) {
215 if (rt->rt_refcnt == 0) {
222 /* no way to return any new error */
227 * Add ownaddr as loopback rtentry. We previously add the route only if
228 * necessary (ex. on a p2p link). However, since we now manage addresses
229 * separately from prefixes, we should always add the route. We can't
230 * rely on the cloning mechanism from the corresponding interface route
234 in6_ifaddloop(struct ifaddr *ifa)
238 /* If there is no loopback entry, allocate one. */
239 rt = rtpurelookup(ifa->ifa_addr);
240 if (rt == NULL || !(rt->rt_flags & RTF_HOST) ||
241 !(rt->rt_ifp->if_flags & IFF_LOOPBACK))
242 in6_ifloop_request(RTM_ADD, ifa);
248 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
252 in6_ifremloop(struct ifaddr *ifa)
254 struct in6_ifaddr *ia;
259 * Some of BSD variants do not remove cloned routes
260 * from an interface direct route, when removing the direct route
261 * (see comments in net/net_osdep.h). Even for variants that do remove
262 * cloned routes, they could fail to remove the cloned routes when
263 * we handle multple addresses that share a common prefix.
264 * So, we should remove the route corresponding to the deleted address
265 * regardless of the result of in6_is_ifloop_auto().
269 * Delete the entry only if exact one ifa exists. More than one ifa
270 * can exist if we assign a same single address to multiple
271 * (probably p2p) interfaces.
272 * XXX: we should avoid such a configuration in IPv6...
274 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
275 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
284 * Before deleting, check if a corresponding loopbacked host
285 * route surely exists. With this check, we can avoid to
286 * delete an interface direct route whose destination is same
287 * as the address being removed. This can happen when remofing
288 * a subnet-router anycast address on an interface attahced
289 * to a shared medium.
291 rt = rtpurelookup(ifa->ifa_addr);
292 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
293 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
295 in6_ifloop_request(RTM_DELETE, ifa);
301 in6_ifindex2scopeid(int idx)
304 struct sockaddr_in6 *sin6;
305 struct ifaddr_container *ifac;
307 if (idx < 0 || if_index < idx)
309 ifp = ifindex2ifnet[idx];
311 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link)
313 struct ifaddr *ifa = ifac->ifa;
315 if (ifa->ifa_addr->sa_family != AF_INET6)
317 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
318 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
319 return sin6->sin6_scope_id & 0xffff;
326 in6_mask2len(struct in6_addr *mask, u_char *lim0)
329 u_char *lim = lim0, *p;
332 lim0 - (u_char *)mask > sizeof(*mask)) /* ignore the scope_id part */
333 lim = (u_char *)mask + sizeof(*mask);
334 for (p = (u_char *)mask; p < lim; x++, p++) {
340 for (y = 0; y < 8; y++) {
341 if ((*p & (0x80 >> y)) == 0)
347 * when the limit pointer is given, do a stricter check on the
351 if (y != 0 && (*p & (0x00ff >> y)) != 0)
353 for (p = p + 1; p < lim; p++)
362 in6_len2mask(struct in6_addr *mask, int len)
366 bzero(mask, sizeof(*mask));
367 for (i = 0; i < len / 8; i++)
368 mask->s6_addr8[i] = 0xff;
370 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
373 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
374 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
377 in6_control_dispatch(netmsg_t msg)
381 error = in6_control(msg->control.base.nm_so,
383 msg->control.nm_data,
386 lwkt_replymsg(&msg->control.base.lmsg, error);
390 in6_control(struct socket *so, u_long cmd, caddr_t data,
391 struct ifnet *ifp, struct thread *td)
393 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
394 struct in6_ifaddr *ia = NULL;
395 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
396 struct in6_ifextra *xtra;
401 if (priv_check(td, PRIV_ROOT) == 0)
405 case SIOCGETSGCNT_IN6:
406 case SIOCGETMIFCNT_IN6:
407 return (mrt6_ioctl(cmd, data));
411 case SIOCAADDRCTL_POLICY:
412 case SIOCDADDRCTL_POLICY:
415 return (in6_src_ioctl(cmd, data));
422 case SIOCSNDFLUSH_IN6:
423 case SIOCSPFXFLUSH_IN6:
424 case SIOCSRTRFLUSH_IN6:
425 case SIOCSDEFIFACE_IN6:
426 case SIOCSIFINFO_FLAGS:
430 case OSIOCGIFINFO_IN6:
431 case SIOCGIFINFO_IN6:
434 case SIOCGNBRINFO_IN6:
435 case SIOCGDEFIFACE_IN6:
436 return (nd6_ioctl(cmd, data, ifp));
440 case SIOCSIFPREFIX_IN6:
441 case SIOCDIFPREFIX_IN6:
442 case SIOCAIFPREFIX_IN6:
443 case SIOCCIFPREFIX_IN6:
444 case SIOCSGIFPREFIX_IN6:
445 case SIOCGIFPREFIX_IN6:
447 "prefix ioctls are now invalidated. "
448 "please use ifconfig.\n");
456 return (scope6_set(ifp,
457 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
460 return (scope6_get(ifp,
461 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
464 return (scope6_get_default((struct scope6_id *)
465 ifr->ifr_ifru.ifru_scope_id));
476 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
480 * Find address for this interface, if it exists.
482 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
483 struct sockaddr_in6 *sa6 =
484 (struct sockaddr_in6 *)&ifra->ifra_addr;
486 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
487 if (sa6->sin6_addr.s6_addr16[1] == 0) {
488 /* link ID is not embedded by the user */
489 sa6->sin6_addr.s6_addr16[1] =
490 htons(ifp->if_index);
491 } else if (sa6->sin6_addr.s6_addr16[1] !=
492 htons(ifp->if_index)) {
493 return (EINVAL); /* link ID contradicts */
495 if (sa6->sin6_scope_id) {
496 if (sa6->sin6_scope_id !=
497 (u_int32_t)ifp->if_index)
499 sa6->sin6_scope_id = 0; /* XXX: good way? */
502 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
506 case SIOCSIFADDR_IN6:
507 case SIOCSIFDSTADDR_IN6:
508 case SIOCSIFNETMASK_IN6:
510 * Since IPv6 allows a node to assign multiple addresses
511 * on a single interface, SIOCSIFxxx ioctls are not suitable
512 * and should be unused.
514 /* we decided to obsolete this command (20000704) */
517 case SIOCDIFADDR_IN6:
519 * for IPv4, we look for existing in_ifaddr here to allow
520 * "ifconfig if0 delete" to remove first IPv4 address on the
521 * interface. For IPv6, as the spec allow multiple interface
522 * address from the day one, we consider "remove the first one"
523 * semantics to be not preferable.
526 return (EADDRNOTAVAIL);
528 case SIOCAIFADDR_IN6:
530 * We always require users to specify a valid IPv6 address for
531 * the corresponding operation.
533 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
534 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
535 return (EAFNOSUPPORT);
541 case SIOCGIFADDR_IN6:
542 /* This interface is basically deprecated. use SIOCGIFCONF. */
544 case SIOCGIFAFLAG_IN6:
545 case SIOCGIFNETMASK_IN6:
546 case SIOCGIFDSTADDR_IN6:
547 case SIOCGIFALIFETIME_IN6:
548 /* must think again about its semantics */
550 return (EADDRNOTAVAIL);
552 case SIOCSIFALIFETIME_IN6:
554 struct in6_addrlifetime *lt;
559 return (EADDRNOTAVAIL);
560 /* sanity for overflow - beware unsigned */
561 lt = &ifr->ifr_ifru.ifru_lifetime;
562 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
563 && lt->ia6t_vltime + time_uptime < time_uptime) {
566 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
567 && lt->ia6t_pltime + time_uptime < time_uptime) {
576 case SIOCGIFADDR_IN6:
577 ifr->ifr_addr = ia->ia_addr;
580 case SIOCGIFDSTADDR_IN6:
581 if (!(ifp->if_flags & IFF_POINTOPOINT))
584 * XXX: should we check if ifa_dstaddr is NULL and return
587 ifr->ifr_dstaddr = ia->ia_dstaddr;
590 case SIOCGIFNETMASK_IN6:
591 ifr->ifr_addr = ia->ia_prefixmask;
594 case SIOCGIFAFLAG_IN6:
595 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
598 case SIOCGIFSTAT_IN6:
599 if (ifp == NULL || (xtra = ifp->if_afdata[AF_INET6]) == NULL)
601 bzero(&ifr->ifr_ifru.ifru_stat,
602 sizeof(ifr->ifr_ifru.ifru_stat));
603 ifr->ifr_ifru.ifru_stat = *xtra->in6_ifstat;
606 case SIOCGIFSTAT_ICMP6:
607 if (ifp == NULL || (xtra = ifp->if_afdata[AF_INET6]) == NULL)
609 bzero(&ifr->ifr_ifru.ifru_stat,
610 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
611 ifr->ifr_ifru.ifru_icmp6stat = *xtra->icmp6_ifstat;
619 * Do not pass those ioctl to driver handler since they are not
620 * properly setup. Instead just error out.
624 case SIOCGIFALIFETIME_IN6:
625 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
628 case SIOCSIFALIFETIME_IN6:
629 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
631 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
632 ia->ia6_lifetime.ia6t_expire =
633 time_uptime + ia->ia6_lifetime.ia6t_vltime;
635 ia->ia6_lifetime.ia6t_expire = 0;
636 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
637 ia->ia6_lifetime.ia6t_preferred =
638 time_uptime + ia->ia6_lifetime.ia6t_pltime;
640 ia->ia6_lifetime.ia6t_preferred = 0;
643 case SIOCAIFADDR_IN6:
645 int i, error = 0, iaIsNew;
646 struct nd_prefix pr0, *pr;
654 * first, make or update the interface address structure,
655 * and link it to the list.
657 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
661 * then, make the prefix on-link on the interface.
662 * XXX: we'd rather create the prefix before the address, but
663 * we need at least one address to install the corresponding
664 * interface route, so we configure the address first.
668 * convert mask to prefix length (prefixmask has already
669 * been validated in in6_update_ifa().
671 bzero(&pr0, sizeof(pr0));
673 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
675 if (pr0.ndpr_plen == 128)
676 break; /* we don't need to install a host route. */
677 pr0.ndpr_prefix = ifra->ifra_addr;
678 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
679 /* apply the mask for safety. */
680 for (i = 0; i < 4; i++) {
681 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
682 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
685 * XXX: since we don't have an API to set prefix (not address)
686 * lifetimes, we just use the same lifetimes as addresses.
687 * The (temporarily) installed lifetimes can be overridden by
688 * later advertised RAs (when accept_rtadv is non 0), which is
689 * an intended behavior.
691 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
693 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
694 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
695 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
697 /* add the prefix if there's one. */
698 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
700 * nd6_prelist_add will install the corresponding
703 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
706 log(LOG_ERR, "nd6_prelist_add succeeded but "
708 return (EINVAL); /* XXX panic here? */
711 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
713 /* XXX: this should not happen! */
714 log(LOG_ERR, "in6_control: addition succeeded, but"
717 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
718 ia->ia6_ndpr == NULL) { /* new autoconfed addr */
723 * If this is the first autoconf address from
724 * the prefix, create a temporary address
725 * as well (when specified).
727 if (ip6_use_tempaddr &&
728 pr->ndpr_refcnt == 1) {
730 if ((e = in6_tmpifadd(ia, 1)) != 0) {
731 log(LOG_NOTICE, "in6_control: "
732 "failed to create a "
733 "temporary address, "
741 * this might affect the status of autoconfigured
742 * addresses, that is, this address might make
743 * other addresses detached.
745 pfxlist_onlink_check();
747 if (error == 0 && ia) {
748 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
749 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
755 case SIOCDIFADDR_IN6:
758 struct nd_prefix pr0, *pr;
761 * If the address being deleted is the only one that owns
762 * the corresponding prefix, expire the prefix as well.
763 * XXX: theoretically, we don't have to warry about such
764 * relationship, since we separate the address management
765 * and the prefix management. We do this, however, to provide
766 * as much backward compatibility as possible in terms of
767 * the ioctl operation.
769 bzero(&pr0, sizeof(pr0));
771 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
773 if (pr0.ndpr_plen == 128)
775 pr0.ndpr_prefix = ia->ia_addr;
776 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
777 for (i = 0; i < 4; i++) {
778 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
779 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
782 * The logic of the following condition is a bit complicated.
783 * We expire the prefix when
784 * 1. the address obeys autoconfiguration and it is the
785 * only owner of the associated prefix, or
786 * 2. the address does not obey autoconf and there is no
787 * other owner of the prefix.
789 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
790 (((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
791 pr->ndpr_refcnt == 1) ||
792 (!(ia->ia6_flags & IN6_IFF_AUTOCONF) &&
793 pr->ndpr_refcnt == 0))) {
794 pr->ndpr_expire = 1; /* XXX: just for expiration */
798 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
800 in6_purgeaddr(&ia->ia_ifa);
805 if (ifp == NULL || ifp->if_ioctl == NULL)
807 ifnet_serialize_all(ifp);
808 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
809 ifnet_deserialize_all(ifp);
817 * Update parameters of an IPv6 interface address.
818 * If necessary, a new entry is created and linked into address chains.
819 * This function is separated from in6_control().
820 * XXX: should this be performed under splnet()?
823 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
824 struct in6_ifaddr *ia)
826 int error = 0, hostIsNew = 0, plen = -1;
827 struct in6_ifaddr *oia;
828 struct sockaddr_in6 dst6;
829 struct in6_addrlifetime *lt;
831 /* Validate parameters */
832 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
836 * The destination address for a p2p link must have a family
837 * of AF_UNSPEC or AF_INET6.
839 if ((ifp->if_flags & IFF_POINTOPOINT) &&
840 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
841 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
842 return (EAFNOSUPPORT);
844 * validate ifra_prefixmask. don't check sin6_family, netmask
845 * does not carry fields other than sin6_len.
847 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
850 * Because the IPv6 address architecture is classless, we require
851 * users to specify a (non 0) prefix length (mask) for a new address.
852 * We also require the prefix (when specified) mask is valid, and thus
853 * reject a non-consecutive mask.
855 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
857 if (ifra->ifra_prefixmask.sin6_len != 0) {
858 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
859 (u_char *)&ifra->ifra_prefixmask +
860 ifra->ifra_prefixmask.sin6_len);
866 * In this case, ia must not be NULL. We just use its prefix
869 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
872 * If the destination address on a p2p interface is specified,
873 * and the address is a scoped one, validate/set the scope
876 dst6 = ifra->ifra_dstaddr;
877 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
878 (dst6.sin6_family == AF_INET6)) {
881 if ((error = in6_recoverscope(&dst6,
882 &ifra->ifra_dstaddr.sin6_addr,
885 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
886 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
887 dst6.sin6_scope_id = scopeid;
888 else if (dst6.sin6_scope_id != scopeid)
889 return (EINVAL); /* scope ID mismatch. */
890 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
893 dst6.sin6_scope_id = 0; /* XXX */
896 * The destination address can be specified only for a p2p or a
897 * loopback interface. If specified, the corresponding prefix length
900 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
901 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
902 /* XXX: noisy message */
903 log(LOG_INFO, "in6_update_ifa: a destination can be "
904 "specified for a p2p or a loopback IF only\n");
909 * The following message seems noisy, but we dare to
910 * add it for diagnosis.
912 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
913 "when dstaddr is specified\n");
917 /* lifetime consistency check */
918 lt = &ifra->ifra_lifetime;
919 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
920 && lt->ia6t_vltime + time_uptime < time_uptime) {
923 if (lt->ia6t_vltime == 0) {
925 * the following log might be noisy, but this is a typical
926 * configuration mistake or a tool's bug.
929 "in6_update_ifa: valid lifetime is 0 for %s\n",
930 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
932 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
933 && lt->ia6t_pltime + time_uptime < time_uptime) {
938 * If this is a new address, allocate a new ifaddr and link it
944 * When in6_update_ifa() is called in a process of a received
945 * RA, it is called under splnet(). So, we should call malloc
948 ia = ifa_create(sizeof(*ia), M_NOWAIT);
951 /* Initialize the address and masks */
952 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
953 ia->ia_addr.sin6_family = AF_INET6;
954 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
955 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
957 * XXX: some functions expect that ifa_dstaddr is not
958 * NULL for p2p interfaces.
960 ia->ia_ifa.ifa_dstaddr
961 = (struct sockaddr *)&ia->ia_dstaddr;
963 ia->ia_ifa.ifa_dstaddr = NULL;
965 ia->ia_ifa.ifa_netmask
966 = (struct sockaddr *)&ia->ia_prefixmask;
969 if ((oia = in6_ifaddr) != NULL) {
970 for ( ; oia->ia_next; oia = oia->ia_next)
976 ifa_iflink(&ia->ia_ifa, ifp, 1);
979 /* set prefix mask */
980 if (ifra->ifra_prefixmask.sin6_len) {
982 * We prohibit changing the prefix length of an existing
984 * + such an operation should be rare in IPv6, and
985 * + the operation would confuse prefix management.
987 if (ia->ia_prefixmask.sin6_len &&
988 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
989 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
990 " existing (%s) address should not be changed\n",
991 ip6_sprintf(&ia->ia_addr.sin6_addr));
995 ia->ia_prefixmask = ifra->ifra_prefixmask;
999 * If a new destination address is specified, scrub the old one and
1000 * install the new destination. Note that the interface must be
1001 * p2p or loopback (see the check above.)
1003 if (dst6.sin6_family == AF_INET6 &&
1004 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
1005 &ia->ia_dstaddr.sin6_addr)) {
1008 if ((ia->ia_flags & IFA_ROUTE) &&
1009 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1011 log(LOG_ERR, "in6_update_ifa: failed to remove "
1012 "a route to the old destination: %s\n",
1013 ip6_sprintf(&ia->ia_addr.sin6_addr));
1014 /* proceed anyway... */
1017 ia->ia_flags &= ~IFA_ROUTE;
1018 ia->ia_dstaddr = dst6;
1021 /* reset the interface and routing table appropriately. */
1022 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1026 * Beyond this point, we should call in6_purgeaddr upon an error,
1027 * not just go to unlink.
1030 #if 0 /* disable this mechanism for now */
1031 /* update prefix list */
1033 (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
1036 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
1037 if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
1038 in6_purgeaddr((struct ifaddr *)ia);
1044 if (ifp->if_flags & IFF_MULTICAST) {
1045 struct sockaddr_in6 mltaddr, mltmask;
1046 struct in6_multi *in6m;
1050 * join solicited multicast addr for new host id
1052 struct in6_addr llsol;
1053 bzero(&llsol, sizeof(struct in6_addr));
1054 llsol.s6_addr16[0] = htons(0xff02);
1055 llsol.s6_addr16[1] = htons(ifp->if_index);
1056 llsol.s6_addr32[1] = 0;
1057 llsol.s6_addr32[2] = htonl(1);
1058 llsol.s6_addr32[3] =
1059 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1060 llsol.s6_addr8[12] = 0xff;
1061 in6_addmulti(&llsol, ifp, &error);
1064 "in6_update_ifa: addmulti failed for "
1065 "%s on %s (errno=%d)\n",
1066 ip6_sprintf(&llsol), if_name(ifp),
1068 in6_purgeaddr((struct ifaddr *)ia);
1073 bzero(&mltmask, sizeof(mltmask));
1074 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1075 mltmask.sin6_family = AF_INET6;
1076 mltmask.sin6_addr = in6mask32;
1079 * join link-local all-nodes address
1081 bzero(&mltaddr, sizeof(mltaddr));
1082 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1083 mltaddr.sin6_family = AF_INET6;
1084 mltaddr.sin6_addr = kin6addr_linklocal_allnodes;
1085 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1087 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
1089 rtrequest_global(RTM_ADD,
1090 (struct sockaddr *)&mltaddr,
1091 (struct sockaddr *)&ia->ia_addr,
1092 (struct sockaddr *)&mltmask,
1093 RTF_UP|RTF_CLONING); /* xxx */
1094 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1097 "in6_update_ifa: addmulti failed for "
1098 "%s on %s (errno=%d)\n",
1099 ip6_sprintf(&mltaddr.sin6_addr),
1100 if_name(ifp), error);
1105 * join node information group address
1107 #define hostnamelen strlen(hostname)
1108 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1110 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
1111 if (in6m == NULL && ia != NULL) {
1112 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1114 log(LOG_WARNING, "in6_update_ifa: "
1115 "addmulti failed for "
1116 "%s on %s (errno=%d)\n",
1117 ip6_sprintf(&mltaddr.sin6_addr),
1118 if_name(ifp), error);
1125 * join node-local all-nodes address, on loopback.
1126 * XXX: since "node-local" is obsoleted by interface-local,
1127 * we have to join the group on every interface with
1128 * some interface-boundary restriction.
1130 if (ifp->if_flags & IFF_LOOPBACK) {
1131 struct in6_ifaddr *ia_loop;
1133 struct in6_addr loop6 = kin6addr_loopback;
1134 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1136 mltaddr.sin6_addr = kin6addr_nodelocal_allnodes;
1138 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
1139 if (in6m == NULL && ia_loop != NULL) {
1140 rtrequest_global(RTM_ADD,
1141 (struct sockaddr *)&mltaddr,
1142 (struct sockaddr *)&ia_loop->ia_addr,
1143 (struct sockaddr *)&mltmask,
1145 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1147 log(LOG_WARNING, "in6_update_ifa: "
1148 "addmulti failed for %s on %s "
1150 ip6_sprintf(&mltaddr.sin6_addr),
1151 if_name(ifp), error);
1157 ia->ia6_flags = ifra->ifra_flags;
1158 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1159 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1161 ia->ia6_lifetime = ifra->ifra_lifetime;
1163 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1164 ia->ia6_lifetime.ia6t_expire =
1165 time_uptime + ia->ia6_lifetime.ia6t_vltime;
1167 ia->ia6_lifetime.ia6t_expire = 0;
1168 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1169 ia->ia6_lifetime.ia6t_preferred =
1170 time_uptime + ia->ia6_lifetime.ia6t_pltime;
1172 ia->ia6_lifetime.ia6t_preferred = 0;
1175 * Perform DAD, if needed.
1176 * XXX It may be of use, if we can administratively
1179 if (in6if_do_dad(ifp) && !(ifra->ifra_flags & IN6_IFF_NODAD)) {
1180 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1181 nd6_dad_start((struct ifaddr *)ia, NULL);
1188 * XXX: if a change of an existing address failed, keep the entry
1192 in6_unlink_ifa(ia, ifp);
1197 in6_purgeaddr(struct ifaddr *ifa)
1199 struct ifnet *ifp = ifa->ifa_ifp;
1200 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1202 /* stop DAD processing */
1206 * delete route to the destination of the address being purged.
1207 * The interface must be p2p or loopback in this case.
1209 if ((ia->ia_flags & IFA_ROUTE) && ia->ia_dstaddr.sin6_len != 0) {
1212 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1214 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1215 "a route to the p2p destination: %s on %s, "
1217 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1219 /* proceed anyway... */
1222 ia->ia_flags &= ~IFA_ROUTE;
1225 /* Remove ownaddr's loopback rtentry, if it exists. */
1226 in6_ifremloop(&(ia->ia_ifa));
1228 if (ifp->if_flags & IFF_MULTICAST) {
1230 * delete solicited multicast addr for deleting host id
1232 struct in6_multi *in6m;
1233 struct in6_addr llsol;
1234 bzero(&llsol, sizeof(struct in6_addr));
1235 llsol.s6_addr16[0] = htons(0xff02);
1236 llsol.s6_addr16[1] = htons(ifp->if_index);
1237 llsol.s6_addr32[1] = 0;
1238 llsol.s6_addr32[2] = htonl(1);
1239 llsol.s6_addr32[3] =
1240 ia->ia_addr.sin6_addr.s6_addr32[3];
1241 llsol.s6_addr8[12] = 0xff;
1243 in6m = IN6_LOOKUP_MULTI(&llsol, ifp);
1248 in6_unlink_ifa(ia, ifp);
1252 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1255 struct in6_ifaddr *oia;
1259 ifa_ifunlink(&ia->ia_ifa, ifp);
1262 if (oia == (ia = in6_ifaddr))
1263 in6_ifaddr = ia->ia_next;
1265 while (ia->ia_next && (ia->ia_next != oia))
1268 ia->ia_next = oia->ia_next;
1271 kprintf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1275 if (oia->ia6_ifpr) { /* check for safety */
1276 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1277 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1278 in6_prefix_remove_ifid(iilen, oia);
1282 * When an autoconfigured address is being removed, release the
1283 * reference to the base prefix. Also, since the release might
1284 * affect the status of other (detached) addresses, call
1285 * pfxlist_onlink_check().
1287 if (oia->ia6_flags & IN6_IFF_AUTOCONF) {
1288 if (oia->ia6_ndpr == NULL) {
1289 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1290 "%p has no prefix\n", oia);
1292 oia->ia6_ndpr->ndpr_refcnt--;
1293 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1294 oia->ia6_ndpr = NULL;
1297 pfxlist_onlink_check();
1301 * release another refcnt for the link from in6_ifaddr.
1302 * Note that we should decrement the refcnt at least once for all *BSD.
1304 ifa_destroy(&oia->ia_ifa);
1310 in6_purgeif(struct ifnet *ifp)
1312 struct ifaddr_container *ifac, *next;
1314 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
1316 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
1318 in6_purgeaddr(ifac->ifa);
1326 * SIOCGLIFADDR: get first address. (?)
1327 * SIOCGLIFADDR with IFLR_PREFIX:
1328 * get first address that matches the specified prefix.
1329 * SIOCALIFADDR: add the specified address.
1330 * SIOCALIFADDR with IFLR_PREFIX:
1331 * add the specified prefix, filling hostid part from
1332 * the first link-local address. prefixlen must be <= 64.
1333 * SIOCDLIFADDR: delete the specified address.
1334 * SIOCDLIFADDR with IFLR_PREFIX:
1335 * delete the first address that matches the specified prefix.
1337 * EINVAL on invalid parameters
1338 * EADDRNOTAVAIL on prefix match failed/specified address not found
1339 * other values may be returned from in6_ioctl()
1341 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1342 * this is to accomodate address naming scheme other than RFC2374,
1344 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1345 * address encoding scheme. (see figure on page 8)
1348 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1349 struct ifnet *ifp, struct thread *td)
1351 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1352 struct sockaddr *sa;
1355 if (!data || !ifp) {
1356 panic("invalid argument to in6_lifaddr_ioctl");
1362 /* address must be specified on GET with IFLR_PREFIX */
1363 if (!(iflr->flags & IFLR_PREFIX))
1368 /* address must be specified on ADD and DELETE */
1369 sa = (struct sockaddr *)&iflr->addr;
1370 if (sa->sa_family != AF_INET6)
1372 if (sa->sa_len != sizeof(struct sockaddr_in6))
1374 /* XXX need improvement */
1375 sa = (struct sockaddr *)&iflr->dstaddr;
1376 if (sa->sa_family && sa->sa_family != AF_INET6)
1378 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1381 default: /* shouldn't happen */
1383 panic("invalid cmd to in6_lifaddr_ioctl");
1389 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1395 struct in6_aliasreq ifra;
1396 struct in6_addr *hostid = NULL;
1399 if (iflr->flags & IFLR_PREFIX) {
1401 struct sockaddr_in6 *sin6;
1404 * hostid is to fill in the hostid part of the
1405 * address. hostid points to the first link-local
1406 * address attached to the interface.
1408 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1410 return EADDRNOTAVAIL;
1411 hostid = IFA_IN6(ifa);
1413 /* prefixlen must be <= 64. */
1414 if (64 < iflr->prefixlen)
1416 prefixlen = iflr->prefixlen;
1418 /* hostid part must be zero. */
1419 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1420 if (sin6->sin6_addr.s6_addr32[2] != 0
1421 || sin6->sin6_addr.s6_addr32[3] != 0) {
1425 prefixlen = iflr->prefixlen;
1427 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1428 bzero(&ifra, sizeof(ifra));
1429 bcopy(iflr->iflr_name, ifra.ifra_name,
1430 sizeof(ifra.ifra_name));
1432 bcopy(&iflr->addr, &ifra.ifra_addr,
1433 ((struct sockaddr *)&iflr->addr)->sa_len);
1435 /* fill in hostid part */
1436 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1437 hostid->s6_addr32[2];
1438 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1439 hostid->s6_addr32[3];
1442 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1443 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1444 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1446 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1447 hostid->s6_addr32[2];
1448 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1449 hostid->s6_addr32[3];
1453 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1454 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1456 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1457 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1462 struct ifaddr_container *ifac;
1463 struct in6_ifaddr *ia;
1464 struct in6_addr mask, candidate, match;
1465 struct sockaddr_in6 *sin6;
1468 bzero(&mask, sizeof(mask));
1469 if (iflr->flags & IFLR_PREFIX) {
1470 /* lookup a prefix rather than address. */
1471 in6_len2mask(&mask, iflr->prefixlen);
1473 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1474 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1475 match.s6_addr32[0] &= mask.s6_addr32[0];
1476 match.s6_addr32[1] &= mask.s6_addr32[1];
1477 match.s6_addr32[2] &= mask.s6_addr32[2];
1478 match.s6_addr32[3] &= mask.s6_addr32[3];
1480 /* if you set extra bits, that's wrong */
1481 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1486 if (cmd == SIOCGLIFADDR) {
1487 /* on getting an address, take the 1st match */
1490 /* on deleting an address, do exact match */
1491 in6_len2mask(&mask, 128);
1492 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1493 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1499 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1500 struct ifaddr *ifa = ifac->ifa;
1502 if (ifa->ifa_addr->sa_family != AF_INET6)
1507 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1509 * XXX: this is adhoc, but is necessary to allow
1510 * a user to specify fe80::/64 (not /10) for a
1511 * link-local address.
1513 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1514 candidate.s6_addr16[1] = 0;
1515 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1516 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1517 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1518 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1519 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1523 return EADDRNOTAVAIL;
1524 ia = ifa2ia6(ifac->ifa);
1526 if (cmd == SIOCGLIFADDR) {
1527 struct sockaddr_in6 *s6;
1529 /* fill in the if_laddrreq structure */
1530 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1531 s6 = (struct sockaddr_in6 *)&iflr->addr;
1532 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1533 s6->sin6_addr.s6_addr16[1] = 0;
1535 in6_addr2scopeid(ifp, &s6->sin6_addr);
1537 if (ifp->if_flags & IFF_POINTOPOINT) {
1538 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1539 ia->ia_dstaddr.sin6_len);
1540 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1541 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1542 s6->sin6_addr.s6_addr16[1] = 0;
1544 in6_addr2scopeid(ifp,
1548 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1551 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1554 iflr->flags = ia->ia6_flags; /* XXX */
1558 struct in6_aliasreq ifra;
1560 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1561 bzero(&ifra, sizeof(ifra));
1562 bcopy(iflr->iflr_name, ifra.ifra_name,
1563 sizeof(ifra.ifra_name));
1565 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1566 ia->ia_addr.sin6_len);
1567 if (ifp->if_flags & IFF_POINTOPOINT)
1568 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1569 ia->ia_dstaddr.sin6_len);
1571 bzero(&ifra.ifra_dstaddr,
1572 sizeof(ifra.ifra_dstaddr));
1573 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1574 ia->ia_prefixmask.sin6_len);
1576 ifra.ifra_flags = ia->ia6_flags;
1577 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1583 return EOPNOTSUPP; /* just for safety */
1587 * Initialize an interface's intetnet6 address
1588 * and routing table entry.
1591 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6,
1594 int error = 0, plen, ifacount = 0;
1595 struct ifaddr_container *ifac;
1598 * Give the interface a chance to initialize
1599 * if this is its first address,
1600 * and to validate the address if necessary.
1602 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1603 if (ifac->ifa->ifa_addr == NULL)
1604 continue; /* just for safety */
1605 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
1610 ifnet_serialize_all(ifp);
1612 ia->ia_addr = *sin6;
1614 if (ifacount <= 1 && ifp->if_ioctl &&
1615 (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL))) {
1616 ifnet_deserialize_all(ifp);
1620 ifnet_deserialize_all(ifp);
1622 ia->ia_ifa.ifa_metric = ifp->if_metric;
1624 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1628 * If the destination address is specified for a point-to-point
1629 * interface, install a route to the destination as an interface
1632 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1633 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1634 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1635 RTF_UP | RTF_HOST)) != 0)
1637 ia->ia_flags |= IFA_ROUTE;
1641 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1643 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1646 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1648 /* set the rtrequest function to create llinfo */
1649 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1650 in6_ifaddloop(&(ia->ia_ifa));
1656 struct in6_multi_mship *
1657 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr, int *errorp)
1659 struct in6_multi_mship *imm;
1661 imm = kmalloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
1666 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
1667 if (!imm->i6mm_maddr) {
1668 /* *errorp is alrady set */
1669 kfree(imm, M_IPMADDR);
1676 in6_leavegroup(struct in6_multi_mship *imm)
1679 if (imm->i6mm_maddr)
1680 in6_delmulti(imm->i6mm_maddr);
1681 kfree(imm, M_IPMADDR);
1686 * Add an address to the list of IP6 multicast addresses for a
1690 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
1692 struct in6_multi *in6m;
1693 struct sockaddr_in6 sin6;
1694 struct ifmultiaddr *ifma;
1701 * Call generic routine to add membership or increment
1702 * refcount. It wants addresses in the form of a sockaddr,
1703 * so we build one here (being careful to zero the unused bytes).
1705 bzero(&sin6, sizeof sin6);
1706 sin6.sin6_family = AF_INET6;
1707 sin6.sin6_len = sizeof sin6;
1708 sin6.sin6_addr = *maddr6;
1709 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1716 * If ifma->ifma_protospec is null, then if_addmulti() created
1717 * a new record. Otherwise, we are done.
1719 if (ifma->ifma_protospec != NULL) {
1721 return ifma->ifma_protospec;
1724 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1725 at interrupt time? If so, need to fix if_addmulti. XXX */
1726 in6m = (struct in6_multi *)kmalloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1732 bzero(in6m, sizeof *in6m);
1733 in6m->in6m_addr = *maddr6;
1734 in6m->in6m_ifp = ifp;
1735 in6m->in6m_ifma = ifma;
1736 ifma->ifma_protospec = in6m;
1737 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1740 * Let MLD6 know that we have joined a new IP6 multicast
1743 mld6_start_listening(in6m);
1749 * Delete a multicast address record.
1752 in6_delmulti(struct in6_multi *in6m)
1754 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1758 if (ifma->ifma_refcount == 1) {
1760 * No remaining claims to this record; let MLD6 know
1761 * that we are leaving the multicast group.
1763 mld6_stop_listening(in6m);
1764 ifma->ifma_protospec = NULL;
1765 LIST_REMOVE(in6m, in6m_entry);
1766 kfree(in6m, M_IPMADDR);
1768 /* XXX - should be separate API for when we have an ifma? */
1769 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1774 * Find an IPv6 interface link-local address specific to an interface.
1777 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1779 struct ifaddr_container *ifac;
1781 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1782 struct ifaddr *ifa = ifac->ifa;
1784 if (ifa->ifa_addr == NULL)
1785 continue; /* just for safety */
1786 if (ifa->ifa_addr->sa_family != AF_INET6)
1788 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1789 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1796 return ((struct in6_ifaddr *)(ifac->ifa));
1803 * find the internet address corresponding to a given interface and address.
1806 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1808 struct ifaddr_container *ifac;
1810 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1811 struct ifaddr *ifa = ifac->ifa;
1813 if (ifa->ifa_addr == NULL)
1814 continue; /* just for safety */
1815 if (ifa->ifa_addr->sa_family != AF_INET6)
1817 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1821 return ((struct in6_ifaddr *)(ifac->ifa));
1827 * Find a link-local scoped address on ifp and return it if any.
1830 in6ifa_llaonifp(struct ifnet *ifp)
1832 struct sockaddr_in6 *sin6;
1833 struct ifaddr_container *ifac;
1835 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1836 struct ifaddr *ifa = ifac->ifa;
1838 if (ifa->ifa_addr->sa_family != AF_INET6)
1840 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1841 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
1842 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
1843 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
1847 return ((struct in6_ifaddr *)(ifac->ifa));
1853 * find the internet address on a given interface corresponding to a neighbor's
1857 in6ifa_ifplocaladdr(const struct ifnet *ifp, const struct in6_addr *addr)
1860 struct in6_ifaddr *ia;
1861 struct ifaddr_container *ifac;
1863 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1866 if (ifa->ifa_addr == NULL)
1867 continue; /* just for safety */
1868 if (ifa->ifa_addr->sa_family != AF_INET6)
1870 ia = (struct in6_ifaddr *)ifa;
1871 if (IN6_ARE_MASKED_ADDR_EQUAL(addr,
1872 &ia->ia_addr.sin6_addr,
1873 &ia->ia_prefixmask.sin6_addr))
1881 * Convert IP6 address to printable (loggable) representation.
1883 static char digits[] = "0123456789abcdef";
1884 static int ip6round = 0;
1886 ip6_sprintf(const struct in6_addr *addr)
1888 static char ip6buf[8][48];
1891 const u_short *a = (const u_short *)addr;
1895 ip6round = (ip6round + 1) & 7;
1896 cp = ip6buf[ip6round];
1898 for (i = 0; i < 8; i++) {
1909 if (dcolon == 0 && *(a + 1) == 0) {
1921 d = (const u_char *)a;
1922 *cp++ = digits[*d >> 4];
1923 *cp++ = digits[*d++ & 0xf];
1924 *cp++ = digits[*d >> 4];
1925 *cp++ = digits[*d & 0xf];
1930 return (ip6buf[ip6round]);
1934 in6_localaddr(struct in6_addr *in6)
1936 struct in6_ifaddr *ia;
1938 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1941 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1942 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1943 &ia->ia_prefixmask.sin6_addr))
1950 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1952 struct in6_ifaddr *ia;
1954 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1955 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1957 (ia->ia6_flags & IN6_IFF_DEPRECATED))
1958 return (1); /* true */
1960 /* XXX: do we still have to go thru the rest of the list? */
1963 return (0); /* false */
1967 * return length of part which dst and src are equal
1971 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1974 u_char *s = (u_char *)src, *d = (u_char *)dst;
1975 u_char *lim = s + 16, r;
1978 if ((r = (*d++ ^ *s++)) != 0) {
1989 /* XXX: to be scope conscious */
1991 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1993 int bytelen, bitlen;
1996 if (0 > len || len > 128) {
1997 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2005 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2007 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
2008 p2->s6_addr[bytelen] >> (8 - bitlen))
2015 in6_prefixlen2mask(struct in6_addr *maskp, int len)
2017 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2018 int bytelen, bitlen, i;
2021 if (0 > len || len > 128) {
2022 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2027 bzero(maskp, sizeof(*maskp));
2030 for (i = 0; i < bytelen; i++)
2031 maskp->s6_addr[i] = 0xff;
2033 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2037 * return the best address out of the same scope
2040 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst, struct ucred *cred)
2042 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
2045 struct in6_ifaddr *ifa_best = NULL;
2048 if(cred && cred->cr_prison)
2053 kprintf("in6_ifawithscope: output interface is not specified\n");
2059 * We search for all addresses on all interfaces from the beginning.
2060 * Comparing an interface with the outgoing interface will be done
2061 * only at the final stage of tiebreaking.
2063 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2065 struct ifaddr_container *ifac;
2068 * We can never take an address that breaks the scope zone
2069 * of the destination.
2071 if (ifp->if_afdata[AF_INET6] == NULL)
2073 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
2076 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2077 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
2078 struct ifaddr *ifa = ifac->ifa;
2080 if (ifa->ifa_addr->sa_family != AF_INET6)
2083 src_scope = in6_addrscope(IFA_IN6(ifa));
2086 * Don't use an address before completing DAD
2087 * nor a duplicated address.
2089 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2093 /* XXX: is there any case to allow anycasts? */
2094 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2098 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2102 /* Skip adresses not valid for current jail */
2104 !jailed_ip(cred->cr_prison, (struct sockaddr *)(ifa->ifa_addr)) != 0)
2108 * If this is the first address we find,
2111 if (ifa_best == NULL)
2115 * ifa_best is never NULL beyond this line except
2116 * within the block labeled "replace".
2120 * If ifa_best has a smaller scope than dst and
2121 * the current address has a larger one than
2122 * (or equal to) dst, always replace ifa_best.
2123 * Also, if the current address has a smaller scope
2124 * than dst, ignore it unless ifa_best also has a
2126 * Consequently, after the two if-clause below,
2127 * the followings must be satisfied:
2128 * (scope(src) < scope(dst) &&
2129 * scope(best) < scope(dst))
2131 * (scope(best) >= scope(dst) &&
2132 * scope(src) >= scope(dst))
2134 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2135 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2136 goto replace; /* (A) */
2137 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2138 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2142 * A deprecated address SHOULD NOT be used in new
2143 * communications if an alternate (non-deprecated)
2144 * address is available and has sufficient scope.
2145 * RFC 2462, Section 5.5.4.
2147 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2148 IN6_IFF_DEPRECATED) {
2150 * Ignore any deprecated addresses if
2151 * specified by configuration.
2153 if (!ip6_use_deprecated)
2157 * If we have already found a non-deprecated
2158 * candidate, just ignore deprecated addresses.
2160 if (!(ifa_best->ia6_flags & IN6_IFF_DEPRECATED))
2165 * A non-deprecated address is always preferred
2166 * to a deprecated one regardless of scopes and
2167 * address matching (Note invariants ensured by the
2168 * conditions (A) and (B) above.)
2170 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2171 !(((struct in6_ifaddr *)ifa)->ia6_flags &
2172 IN6_IFF_DEPRECATED))
2176 * When we use temporary addresses described in
2177 * RFC 3041, we prefer temporary addresses to
2178 * public autoconf addresses. Again, note the
2179 * invariants from (A) and (B). Also note that we
2180 * don't have any preference between static addresses
2181 * and autoconf addresses (despite of whether or not
2182 * the latter is temporary or public.)
2184 if (ip6_use_tempaddr) {
2185 struct in6_ifaddr *ifat;
2187 ifat = (struct in6_ifaddr *)ifa;
2188 if ((ifa_best->ia6_flags &
2189 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2190 == IN6_IFF_AUTOCONF &&
2192 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2193 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2196 if ((ifa_best->ia6_flags &
2197 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2198 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2200 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2201 == IN6_IFF_AUTOCONF) {
2207 * At this point, we have two cases:
2208 * 1. we are looking at a non-deprecated address,
2209 * and ifa_best is also non-deprecated.
2210 * 2. we are looking at a deprecated address,
2211 * and ifa_best is also deprecated.
2212 * Also, we do not have to consider a case where
2213 * the scope of if_best is larger(smaller) than dst and
2214 * the scope of the current address is smaller(larger)
2215 * than dst. Such a case has already been covered.
2216 * Tiebreaking is done according to the following
2218 * - the scope comparison between the address and
2220 * - the scope comparison between the address and
2221 * ifa_best (bscopecmp)
2222 * - if the address match dst longer than ifa_best
2224 * - if the address is on the outgoing I/F (outI/F)
2226 * Roughly speaking, the selection policy is
2227 * - the most important item is scope. The same scope
2228 * is best. Then search for a larger scope.
2229 * Smaller scopes are the last resort.
2230 * - A deprecated address is chosen only when we have
2231 * no address that has an enough scope, but is
2232 * prefered to any addresses of smaller scopes
2233 * (this must be already done above.)
2234 * - addresses on the outgoing I/F are preferred to
2235 * ones on other interfaces if none of above
2236 * tiebreaks. In the table below, the column "bI"
2237 * means if the best_ifa is on the outgoing
2238 * interface, and the column "sI" means if the ifa
2239 * is on the outgoing interface.
2240 * - If there is no other reasons to choose one,
2241 * longest address match against dst is considered.
2243 * The precise decision table is as follows:
2244 * dscopecmp bscopecmp match bI oI | replace?
2245 * N/A equal N/A Y N | No (1)
2246 * N/A equal N/A N Y | Yes (2)
2247 * N/A equal larger N/A | Yes (3)
2248 * N/A equal !larger N/A | No (4)
2249 * larger larger N/A N/A | No (5)
2250 * larger smaller N/A N/A | Yes (6)
2251 * smaller larger N/A N/A | Yes (7)
2252 * smaller smaller N/A N/A | No (8)
2253 * equal smaller N/A N/A | Yes (9)
2254 * equal larger (already done at A above)
2256 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2257 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2259 if (bscopecmp == 0) {
2260 struct ifnet *bifp = ifa_best->ia_ifp;
2262 if (bifp == oifp && ifp != oifp) /* (1) */
2264 if (bifp != oifp && ifp == oifp) /* (2) */
2268 * Both bifp and ifp are on the outgoing
2269 * interface, or both two are on a different
2270 * interface from the outgoing I/F.
2271 * now we need address matching against dst
2274 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2275 matchcmp = tlen - blen;
2276 if (matchcmp > 0) /* (3) */
2280 if (dscopecmp > 0) {
2281 if (bscopecmp > 0) /* (5) */
2283 goto replace; /* (6) */
2285 if (dscopecmp < 0) {
2286 if (bscopecmp > 0) /* (7) */
2291 /* now dscopecmp must be 0 */
2293 goto replace; /* (9) */
2296 ifa_best = (struct in6_ifaddr *)ifa;
2297 blen = tlen >= 0 ? tlen :
2298 in6_matchlen(IFA_IN6(ifa), dst);
2299 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2303 /* count statistics for future improvements */
2304 if (ifa_best == NULL)
2305 ip6stat.ip6s_sources_none++;
2307 if (oifp == ifa_best->ia_ifp)
2308 ip6stat.ip6s_sources_sameif[best_scope]++;
2310 ip6stat.ip6s_sources_otherif[best_scope]++;
2312 if (best_scope == dst_scope)
2313 ip6stat.ip6s_sources_samescope[best_scope]++;
2315 ip6stat.ip6s_sources_otherscope[best_scope]++;
2317 if (ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2318 ip6stat.ip6s_sources_deprecated[best_scope]++;
2325 * return the best address out of the same scope. if no address was
2326 * found, return the first valid address from designated IF.
2329 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2331 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2332 struct ifaddr_container *ifac;
2333 struct in6_ifaddr *besta = NULL;
2334 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2336 dep[0] = dep[1] = NULL;
2339 * We first look for addresses in the same scope.
2340 * If there is one, return it.
2341 * If two or more, return one which matches the dst longest.
2342 * If none, return one of global addresses assigned other ifs.
2344 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2345 struct ifaddr *ifa = ifac->ifa;
2347 if (ifa->ifa_addr->sa_family != AF_INET6)
2349 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2350 continue; /* XXX: is there any case to allow anycast? */
2351 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2352 continue; /* don't use this interface */
2353 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2355 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2356 if (ip6_use_deprecated)
2357 dep[0] = (struct in6_ifaddr *)ifa;
2361 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2363 * call in6_matchlen() as few as possible
2367 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2368 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2371 besta = (struct in6_ifaddr *)ifa;
2374 besta = (struct in6_ifaddr *)ifa;
2380 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2381 struct ifaddr *ifa = ifac->ifa;
2383 if (ifa->ifa_addr->sa_family != AF_INET6)
2385 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2386 continue; /* XXX: is there any case to allow anycast? */
2387 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2388 continue; /* don't use this interface */
2389 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2391 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2392 if (ip6_use_deprecated)
2393 dep[1] = (struct in6_ifaddr *)ifa;
2397 return (struct in6_ifaddr *)ifa;
2400 /* use the last-resort values, that are, deprecated addresses */
2410 * perform DAD when interface becomes IFF_UP.
2413 in6_if_up(struct ifnet *ifp)
2415 struct ifaddr_container *ifac;
2416 struct in6_ifaddr *ia;
2417 int dad_delay; /* delay ticks before DAD output */
2420 * special cases, like 6to4, are handled in in6_ifattach
2422 in6_ifattach(ifp, NULL);
2425 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2426 struct ifaddr *ifa = ifac->ifa;
2428 if (ifa->ifa_addr->sa_family != AF_INET6)
2430 ia = (struct in6_ifaddr *)ifa;
2431 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2432 nd6_dad_start(ifa, &dad_delay);
2437 in6if_do_dad(struct ifnet *ifp)
2439 if (ifp->if_flags & IFF_LOOPBACK)
2442 switch (ifp->if_type) {
2448 * These interfaces do not have the IFF_LOOPBACK flag,
2449 * but loop packets back. We do not have to do DAD on such
2450 * interfaces. We should even omit it, because loop-backed
2451 * NS would confuse the DAD procedure.
2456 * Our DAD routine requires the interface up and running.
2457 * However, some interfaces can be up before the RUNNING
2458 * status. Additionaly, users may try to assign addresses
2459 * before the interface becomes up (or running).
2460 * We simply skip DAD in such a case as a work around.
2461 * XXX: we should rather mark "tentative" on such addresses,
2462 * and do DAD after the interface becomes ready.
2464 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2465 (IFF_UP|IFF_RUNNING))
2473 * Calculate max IPv6 MTU through all the interfaces and store it
2479 unsigned long maxmtu = 0;
2482 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2484 /* this function can be called during ifnet initialization */
2485 if (ifp->if_afdata[AF_INET6] == NULL)
2487 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2488 ND_IFINFO(ifp)->linkmtu > maxmtu)
2489 maxmtu = ND_IFINFO(ifp)->linkmtu;
2491 if (maxmtu) /* update only when maxmtu is positive */
2492 in6_maxmtu = maxmtu;
2496 in6_domifattach(struct ifnet *ifp)
2498 struct in6_ifextra *ext;
2500 ext = (struct in6_ifextra *)kmalloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2501 bzero(ext, sizeof(*ext));
2503 ext->in6_ifstat = (struct in6_ifstat *)kmalloc(sizeof(struct in6_ifstat),
2504 M_IFADDR, M_WAITOK);
2505 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2508 (struct icmp6_ifstat *)kmalloc(sizeof(struct icmp6_ifstat),
2509 M_IFADDR, M_WAITOK);
2510 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2512 ext->nd_ifinfo = nd6_ifattach(ifp);
2513 ext->scope6_id = scope6_ifattach(ifp);
2518 in6_domifdetach(struct ifnet *ifp, void *aux)
2520 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2521 scope6_ifdetach(ext->scope6_id);
2522 nd6_ifdetach(ext->nd_ifinfo);
2523 kfree(ext->in6_ifstat, M_IFADDR);
2524 kfree(ext->icmp6_ifstat, M_IFADDR);
2525 kfree(ext, M_IFADDR);
2529 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2530 * v4 mapped addr or v4 compat addr
2533 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2535 bzero(sin, sizeof(*sin));
2536 sin->sin_len = sizeof(struct sockaddr_in);
2537 sin->sin_family = AF_INET;
2538 sin->sin_port = sin6->sin6_port;
2539 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2542 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2544 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2546 bzero(sin6, sizeof(*sin6));
2547 sin6->sin6_len = sizeof(struct sockaddr_in6);
2548 sin6->sin6_family = AF_INET6;
2549 sin6->sin6_port = sin->sin_port;
2550 sin6->sin6_addr.s6_addr32[0] = 0;
2551 sin6->sin6_addr.s6_addr32[1] = 0;
2552 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2553 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2556 /* Convert sockaddr_in6 into sockaddr_in. */
2558 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2560 struct sockaddr_in *sin_p;
2561 struct sockaddr_in6 sin6;
2564 * Save original sockaddr_in6 addr and convert it
2567 sin6 = *(struct sockaddr_in6 *)nam;
2568 sin_p = (struct sockaddr_in *)nam;
2569 in6_sin6_2_sin(sin_p, &sin6);
2572 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2574 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2576 struct sockaddr_in *sin_p;
2577 struct sockaddr_in6 *sin6_p;
2579 sin6_p = kmalloc(sizeof *sin6_p, M_SONAME, M_WAITOK);
2580 sin_p = (struct sockaddr_in *)*nam;
2581 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2582 kfree(*nam, M_SONAME);
2583 *nam = (struct sockaddr *)sin6_p;