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>
88 #include <net/netmsg2.h>
89 #include <net/netisr2.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 static void in6_control_internal_dispatch(netmsg_t);
139 static int in6_control_internal(u_long, caddr_t, struct ifnet *,
142 struct in6_multihead in6_multihead; /* XXX BSS initialization */
144 int (*faithprefix_p)(struct in6_addr *);
147 * Subroutine for in6_ifaddloop() and in6_ifremloop().
148 * This routine does actual work.
151 in6_ifloop_request(int cmd, struct ifaddr *ifa)
153 struct sockaddr_in6 all1_sa;
154 struct rt_addrinfo rtinfo;
157 bzero(&all1_sa, sizeof(all1_sa));
158 all1_sa.sin6_family = AF_INET6;
159 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
160 all1_sa.sin6_addr = in6mask128;
163 * We specify the address itself as the gateway, and set the
164 * RTF_LLINFO flag, so that the corresponding host route would have
165 * the flag, and thus applications that assume traditional behavior
166 * would be happy. Note that we assume the caller of the function
167 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
168 * which changes the outgoing interface to the loopback interface.
170 bzero(&rtinfo, sizeof(struct rt_addrinfo));
171 rtinfo.rti_info[RTAX_DST] = ifa->ifa_addr;
172 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
173 rtinfo.rti_info[RTAX_NETMASK] = (struct sockaddr *)&all1_sa;
174 rtinfo.rti_flags = RTF_UP|RTF_HOST|RTF_LLINFO;
176 error = rtrequest1_global(cmd, &rtinfo,
177 in6_ifloop_request_callback, ifa, RTREQ_PRIO_NORM);
179 log(LOG_ERR, "in6_ifloop_request: "
180 "%s operation failed for %s (errno=%d)\n",
181 cmd == RTM_ADD ? "ADD" : "DELETE",
182 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
188 in6_ifloop_request_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
189 struct rtentry *rt, void *arg)
191 struct ifaddr *ifa = arg;
197 * Make sure rt_ifa be equal to IFA, the second argument of the
199 * We need this because when we refer to rt_ifa->ia6_flags in
200 * ip6_input, we assume that the rt_ifa points to the address instead
201 * of the loopback address.
203 if (cmd == RTM_ADD && rt && ifa != rt->rt_ifa) {
212 * Report the addition/removal of the address to the routing socket.
213 * XXX: since we called rtinit for a p2p interface with a destination,
214 * we end up reporting twice in such a case. Should we rather
215 * omit the second report?
219 rt_newaddrmsg(cmd, ifa, error, rt);
220 if (cmd == RTM_DELETE) {
221 if (rt->rt_refcnt == 0) {
228 /* no way to return any new error */
233 * Add ownaddr as loopback rtentry. We previously add the route only if
234 * necessary (ex. on a p2p link). However, since we now manage addresses
235 * separately from prefixes, we should always add the route. We can't
236 * rely on the cloning mechanism from the corresponding interface route
240 in6_ifaddloop(struct ifaddr *ifa)
244 /* If there is no loopback entry, allocate one. */
245 rt = rtpurelookup(ifa->ifa_addr);
246 if (rt == NULL || !(rt->rt_flags & RTF_HOST) ||
247 !(rt->rt_ifp->if_flags & IFF_LOOPBACK))
248 in6_ifloop_request(RTM_ADD, ifa);
254 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
258 in6_ifremloop(struct ifaddr *ifa)
260 struct in6_ifaddr *ia;
265 * Some of BSD variants do not remove cloned routes
266 * from an interface direct route, when removing the direct route
267 * (see comments in net/net_osdep.h). Even for variants that do remove
268 * cloned routes, they could fail to remove the cloned routes when
269 * we handle multple addresses that share a common prefix.
270 * So, we should remove the route corresponding to the deleted address
271 * regardless of the result of in6_is_ifloop_auto().
275 * Delete the entry only if exact one ifa exists. More than one ifa
276 * can exist if we assign a same single address to multiple
277 * (probably p2p) interfaces.
278 * XXX: we should avoid such a configuration in IPv6...
280 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
281 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
290 * Before deleting, check if a corresponding loopbacked host
291 * route surely exists. With this check, we can avoid to
292 * delete an interface direct route whose destination is same
293 * as the address being removed. This can happen when remofing
294 * a subnet-router anycast address on an interface attahced
295 * to a shared medium.
297 rt = rtpurelookup(ifa->ifa_addr);
298 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
299 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
301 in6_ifloop_request(RTM_DELETE, ifa);
307 in6_ifindex2scopeid(int idx)
310 struct sockaddr_in6 *sin6;
311 struct ifaddr_container *ifac;
313 if (idx < 0 || if_index < idx)
315 ifp = ifindex2ifnet[idx];
317 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link)
319 struct ifaddr *ifa = ifac->ifa;
321 if (ifa->ifa_addr->sa_family != AF_INET6)
323 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
324 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
325 return sin6->sin6_scope_id & 0xffff;
332 in6_mask2len(const struct in6_addr *mask, const u_char *lim0)
335 const u_char *lim = lim0, *p;
338 lim0 - (const u_char *)mask > sizeof(*mask)) {
339 /* Ignore the scope_id part */
340 lim = (const u_char *)mask + sizeof(*mask);
342 for (p = (const u_char *)mask; p < lim; x++, p++) {
348 for (y = 0; y < 8; y++) {
349 if ((*p & (0x80 >> y)) == 0)
355 * When the limit pointer is given, do a stricter check on the
359 if (y != 0 && (*p & (0x00ff >> y)) != 0)
361 for (p = p + 1; p < lim; p++)
370 in6_len2mask(struct in6_addr *mask, int len)
374 bzero(mask, sizeof(*mask));
375 for (i = 0; i < len / 8; i++)
376 mask->s6_addr8[i] = 0xff;
378 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
381 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
382 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
385 in6_control_dispatch(netmsg_t msg)
389 error = in6_control(msg->control.base.nm_so,
391 msg->control.nm_data,
394 lwkt_replymsg(&msg->control.base.lmsg, error);
398 in6_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
401 struct netmsg_pru_control msg;
405 case SIOCSIFPREFIX_IN6:
406 case SIOCDIFPREFIX_IN6:
407 case SIOCAIFPREFIX_IN6:
408 case SIOCCIFPREFIX_IN6:
409 case SIOCSGIFPREFIX_IN6:
410 case SIOCGIFPREFIX_IN6:
411 log(LOG_NOTICE, "prefix ioctls are now invalidated. "
412 "please use ifconfig.\n");
415 case SIOCSIFADDR_IN6:
416 case SIOCSIFDSTADDR_IN6:
417 case SIOCSIFNETMASK_IN6:
419 * Since IPv6 allows a node to assign multiple addresses
420 * on a single interface, SIOCSIFxxx ioctls are not suitable
421 * and should be unused.
423 /* We decided to obsolete this command (20000704) */
431 * Do not pass those ioctl to driver handler since they are not
432 * properly setup. Instead just error out.
438 if ((error = priv_check(td, PRIV_ROOT)) != 0)
444 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
447 case SIOCGETSGCNT_IN6:
448 case SIOCGETMIFCNT_IN6:
450 case SIOCAADDRCTL_POLICY:
451 case SIOCDADDRCTL_POLICY:
453 case SIOCSNDFLUSH_IN6:
454 case SIOCSPFXFLUSH_IN6:
455 case SIOCSRTRFLUSH_IN6:
456 case SIOCSDEFIFACE_IN6:
457 case SIOCSIFINFO_FLAGS:
458 case OSIOCGIFINFO_IN6:
459 case SIOCGIFINFO_IN6:
462 case SIOCGNBRINFO_IN6:
463 case SIOCGDEFIFACE_IN6:
469 case SIOCSIFALIFETIME_IN6:
470 case SIOCAIFADDR_IN6:
471 case SIOCDIFADDR_IN6:
473 * Dispatch these SIOCs to netisr0.
475 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
476 in6_control_internal_dispatch);
481 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
482 return msg.base.lmsg.ms_error;
485 return in6_control_internal(cmd, data, ifp, td);
490 in6_control_internal_dispatch(netmsg_t msg)
494 error = in6_control_internal(msg->control.nm_cmd, msg->control.nm_data,
495 msg->control.nm_ifp, msg->control.nm_td);
496 lwkt_replymsg(&msg->lmsg, error);
500 in6_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
503 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
504 struct in6_ifaddr *ia = NULL;
505 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
506 struct in6_ifextra *xtra;
507 boolean_t privileged;
511 if (priv_check(td, PRIV_ROOT) == 0)
515 case SIOCGETSGCNT_IN6:
516 case SIOCGETMIFCNT_IN6:
517 return (mrt6_ioctl(cmd, data));
521 case SIOCAADDRCTL_POLICY:
522 case SIOCDADDRCTL_POLICY:
525 return (in6_src_ioctl(cmd, data));
532 case SIOCSNDFLUSH_IN6:
533 case SIOCSPFXFLUSH_IN6:
534 case SIOCSRTRFLUSH_IN6:
535 case SIOCSDEFIFACE_IN6:
536 case SIOCSIFINFO_FLAGS:
540 case OSIOCGIFINFO_IN6:
541 case SIOCGIFINFO_IN6:
544 case SIOCGNBRINFO_IN6:
545 case SIOCGDEFIFACE_IN6:
546 return (nd6_ioctl(cmd, data, ifp));
553 return (scope6_set(ifp,
554 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
557 return (scope6_get(ifp,
558 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
561 return (scope6_get_default((struct scope6_id *)
562 ifr->ifr_ifru.ifru_scope_id));
566 * Find address for this interface, if it exists.
568 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
569 struct sockaddr_in6 *sa6 =
570 (struct sockaddr_in6 *)&ifra->ifra_addr;
572 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
573 if (sa6->sin6_addr.s6_addr16[1] == 0) {
574 /* Link ID is not embedded by the user */
575 sa6->sin6_addr.s6_addr16[1] =
576 htons(ifp->if_index);
577 } else if (sa6->sin6_addr.s6_addr16[1] !=
578 htons(ifp->if_index)) {
579 /* Link ID contradicts */
582 if (sa6->sin6_scope_id) {
583 if (sa6->sin6_scope_id !=
584 (u_int32_t)ifp->if_index)
586 sa6->sin6_scope_id = 0; /* XXX: good way? */
589 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
593 case SIOCDIFADDR_IN6:
595 * For IPv4, we look for existing in_ifaddr here to allow
596 * "ifconfig if0 delete" to remove first IPv4 address on the
597 * interface. For IPv6, as the spec allow multiple interface
598 * address from the day one, we consider "remove the first one"
599 * semantics to be not preferable.
602 return (EADDRNOTAVAIL);
604 case SIOCAIFADDR_IN6:
606 * We always require users to specify a valid IPv6 address for
607 * the corresponding operation.
609 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
610 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
611 return (EAFNOSUPPORT);
616 case SIOCGIFADDR_IN6:
617 /* This interface is basically deprecated. Use SIOCGIFCONF. */
619 case SIOCGIFAFLAG_IN6:
620 case SIOCGIFNETMASK_IN6:
621 case SIOCGIFDSTADDR_IN6:
622 case SIOCGIFALIFETIME_IN6:
623 /* Must think again about its semantics */
625 return (EADDRNOTAVAIL);
628 case SIOCSIFALIFETIME_IN6:
630 const struct in6_addrlifetime *lt;
635 return (EADDRNOTAVAIL);
636 /* Sanity for overflow - beware unsigned */
637 lt = &ifr->ifr_ifru.ifru_lifetime;
638 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
639 lt->ia6t_vltime + time_uptime < time_uptime)
641 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
642 lt->ia6t_pltime + time_uptime < time_uptime)
649 case SIOCGIFADDR_IN6:
650 ifr->ifr_addr = ia->ia_addr;
653 case SIOCGIFDSTADDR_IN6:
654 if (!(ifp->if_flags & IFF_POINTOPOINT))
657 * XXX: Should we check if ifa_dstaddr is NULL and return
660 ifr->ifr_dstaddr = ia->ia_dstaddr;
663 case SIOCGIFNETMASK_IN6:
664 ifr->ifr_addr = ia->ia_prefixmask;
667 case SIOCGIFAFLAG_IN6:
668 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
671 case SIOCGIFSTAT_IN6:
672 if ((xtra = ifp->if_afdata[AF_INET6]) == NULL)
674 bzero(&ifr->ifr_ifru.ifru_stat,
675 sizeof(ifr->ifr_ifru.ifru_stat));
676 ifr->ifr_ifru.ifru_stat = *xtra->in6_ifstat;
679 case SIOCGIFSTAT_ICMP6:
680 if ((xtra = ifp->if_afdata[AF_INET6]) == NULL)
682 bzero(&ifr->ifr_ifru.ifru_stat,
683 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
684 ifr->ifr_ifru.ifru_icmp6stat = *xtra->icmp6_ifstat;
687 case SIOCGIFALIFETIME_IN6:
688 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
691 case SIOCSIFALIFETIME_IN6:
692 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
693 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
694 ia->ia6_lifetime.ia6t_expire =
695 time_uptime + ia->ia6_lifetime.ia6t_vltime;
697 ia->ia6_lifetime.ia6t_expire = 0;
699 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
700 ia->ia6_lifetime.ia6t_preferred =
701 time_uptime + ia->ia6_lifetime.ia6t_pltime;
703 ia->ia6_lifetime.ia6t_preferred = 0;
707 case SIOCAIFADDR_IN6:
709 int i, error = 0, iaIsNew;
710 struct nd_prefix pr0, *pr;
718 * First, make or update the interface address structure,
719 * and link it to the list.
721 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
725 * Then, make the prefix on-link on the interface.
726 * XXX: We'd rather create the prefix before the address, but
727 * we need at least one address to install the corresponding
728 * interface route, so we configure the address first.
732 * Convert mask to prefix length (prefixmask has already
733 * been validated in in6_update_ifa().
735 bzero(&pr0, sizeof(pr0));
737 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
739 if (pr0.ndpr_plen == 128)
740 break; /* no need to install a host route. */
741 pr0.ndpr_prefix = ifra->ifra_addr;
742 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
743 /* Apply the mask for safety. */
744 for (i = 0; i < 4; i++) {
745 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
746 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
749 * XXX: Since we don't have an API to set prefix (not address)
750 * lifetimes, we just use the same lifetimes as addresses.
751 * The (temporarily) installed lifetimes can be overridden by
752 * later advertised RAs (when accept_rtadv is non 0), which is
753 * an intended behavior.
755 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
757 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
758 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
759 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
761 /* Add the prefix if there's one. */
762 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
764 * nd6_prelist_add will install the corresponding
767 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
770 log(LOG_ERR, "nd6_prelist_add succeeded but "
772 return (EINVAL); /* XXX panic here? */
776 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
778 /* XXX: This should not happen! */
779 log(LOG_ERR, "in6_control: addition succeeded, but"
782 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
783 ia->ia6_ndpr == NULL) {
785 * New autoconf address
791 * If this is the first autoconf address from
792 * the prefix, create a temporary address
793 * as well (when specified).
795 if (ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
798 if ((e = in6_tmpifadd(ia, 1)) != 0) {
799 log(LOG_NOTICE, "in6_control: "
800 "failed to create a "
801 "temporary address, "
808 * This might affect the status of autoconfigured
809 * addresses, that is, this address might make
810 * other addresses detached.
812 pfxlist_onlink_check();
814 if (error == 0 && ia) {
815 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
816 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
822 case SIOCDIFADDR_IN6:
825 struct nd_prefix pr0, *pr;
828 * If the address being deleted is the only one that owns
829 * the corresponding prefix, expire the prefix as well.
830 * XXX: Theoretically, we don't have to warry about such
831 * relationship, since we separate the address management
832 * and the prefix management. We do this, however, to provide
833 * as much backward compatibility as possible in terms of
834 * the ioctl operation.
836 bzero(&pr0, sizeof(pr0));
838 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
840 if (pr0.ndpr_plen == 128)
842 pr0.ndpr_prefix = ia->ia_addr;
843 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
844 for (i = 0; i < 4; i++) {
845 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
846 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
849 * The logic of the following condition is a bit complicated.
850 * We expire the prefix when
851 * 1. The address obeys autoconfiguration and it is the
852 * only owner of the associated prefix, or
853 * 2. The address does not obey autoconf and there is no
854 * other owner of the prefix.
856 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
857 (((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
858 pr->ndpr_refcnt == 1) ||
859 (!(ia->ia6_flags & IN6_IFF_AUTOCONF) &&
860 pr->ndpr_refcnt == 0)))
861 pr->ndpr_expire = 1; /* XXX: just for expiration */
864 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
866 in6_purgeaddr(&ia->ia_ifa);
871 if (ifp->if_ioctl == NULL)
873 ifnet_serialize_all(ifp);
874 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
875 ifnet_deserialize_all(ifp);
883 * Update parameters of an IPv6 interface address.
884 * If necessary, a new entry is created and linked into address chains.
885 * This function is separated from in6_control().
886 * XXX: should this be performed under splnet()?
889 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
890 struct in6_ifaddr *ia)
892 int error = 0, hostIsNew = 0, plen = -1;
893 struct in6_ifaddr *oia;
894 struct sockaddr_in6 dst6;
895 struct in6_addrlifetime *lt;
897 /* Validate parameters */
898 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
902 * The destination address for a p2p link must have a family
903 * of AF_UNSPEC or AF_INET6.
905 if ((ifp->if_flags & IFF_POINTOPOINT) &&
906 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
907 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
908 return (EAFNOSUPPORT);
910 * validate ifra_prefixmask. don't check sin6_family, netmask
911 * does not carry fields other than sin6_len.
913 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
916 * Because the IPv6 address architecture is classless, we require
917 * users to specify a (non 0) prefix length (mask) for a new address.
918 * We also require the prefix (when specified) mask is valid, and thus
919 * reject a non-consecutive mask.
921 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
923 if (ifra->ifra_prefixmask.sin6_len != 0) {
924 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
925 (u_char *)&ifra->ifra_prefixmask +
926 ifra->ifra_prefixmask.sin6_len);
932 * In this case, ia must not be NULL. We just use its prefix
935 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
938 * If the destination address on a p2p interface is specified,
939 * and the address is a scoped one, validate/set the scope
942 dst6 = ifra->ifra_dstaddr;
943 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
944 (dst6.sin6_family == AF_INET6)) {
947 if ((error = in6_recoverscope(&dst6,
948 &ifra->ifra_dstaddr.sin6_addr,
951 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
952 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
953 dst6.sin6_scope_id = scopeid;
954 else if (dst6.sin6_scope_id != scopeid)
955 return (EINVAL); /* scope ID mismatch. */
956 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
959 dst6.sin6_scope_id = 0; /* XXX */
962 * The destination address can be specified only for a p2p or a
963 * loopback interface. If specified, the corresponding prefix length
966 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
967 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
968 /* XXX: noisy message */
969 log(LOG_INFO, "in6_update_ifa: a destination can be "
970 "specified for a p2p or a loopback IF only\n");
975 * The following message seems noisy, but we dare to
976 * add it for diagnosis.
978 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
979 "when dstaddr is specified\n");
983 /* lifetime consistency check */
984 lt = &ifra->ifra_lifetime;
985 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
986 && lt->ia6t_vltime + time_uptime < time_uptime) {
989 if (lt->ia6t_vltime == 0) {
991 * the following log might be noisy, but this is a typical
992 * configuration mistake or a tool's bug.
995 "in6_update_ifa: valid lifetime is 0 for %s\n",
996 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
998 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
999 && lt->ia6t_pltime + time_uptime < time_uptime) {
1004 * If this is a new address, allocate a new ifaddr and link it
1010 * When in6_update_ifa() is called in a process of a received
1011 * RA, it is called under splnet(). So, we should call malloc
1014 ia = ifa_create(sizeof(*ia), M_NOWAIT);
1017 /* Initialize the address and masks */
1018 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1019 ia->ia_addr.sin6_family = AF_INET6;
1020 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
1021 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
1023 * XXX: some functions expect that ifa_dstaddr is not
1024 * NULL for p2p interfaces.
1026 ia->ia_ifa.ifa_dstaddr
1027 = (struct sockaddr *)&ia->ia_dstaddr;
1029 ia->ia_ifa.ifa_dstaddr = NULL;
1031 ia->ia_ifa.ifa_netmask
1032 = (struct sockaddr *)&ia->ia_prefixmask;
1035 if ((oia = in6_ifaddr) != NULL) {
1036 for ( ; oia->ia_next; oia = oia->ia_next)
1042 ifa_iflink(&ia->ia_ifa, ifp, 1);
1045 /* set prefix mask */
1046 if (ifra->ifra_prefixmask.sin6_len) {
1048 * We prohibit changing the prefix length of an existing
1050 * + such an operation should be rare in IPv6, and
1051 * + the operation would confuse prefix management.
1053 if (ia->ia_prefixmask.sin6_len &&
1054 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1055 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
1056 " existing (%s) address should not be changed\n",
1057 ip6_sprintf(&ia->ia_addr.sin6_addr));
1061 ia->ia_prefixmask = ifra->ifra_prefixmask;
1065 * If a new destination address is specified, scrub the old one and
1066 * install the new destination. Note that the interface must be
1067 * p2p or loopback (see the check above.)
1069 if (dst6.sin6_family == AF_INET6 &&
1070 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
1071 &ia->ia_dstaddr.sin6_addr)) {
1074 if ((ia->ia_flags & IFA_ROUTE) &&
1075 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1077 log(LOG_ERR, "in6_update_ifa: failed to remove "
1078 "a route to the old destination: %s\n",
1079 ip6_sprintf(&ia->ia_addr.sin6_addr));
1080 /* proceed anyway... */
1083 ia->ia_flags &= ~IFA_ROUTE;
1084 ia->ia_dstaddr = dst6;
1087 /* reset the interface and routing table appropriately. */
1088 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1092 * Beyond this point, we should call in6_purgeaddr upon an error,
1093 * not just go to unlink.
1096 if (ifp->if_flags & IFF_MULTICAST) {
1097 struct sockaddr_in6 mltaddr, mltmask;
1098 struct in6_multi *in6m;
1102 * join solicited multicast addr for new host id
1104 struct in6_addr llsol;
1105 bzero(&llsol, sizeof(struct in6_addr));
1106 llsol.s6_addr16[0] = htons(0xff02);
1107 llsol.s6_addr16[1] = htons(ifp->if_index);
1108 llsol.s6_addr32[1] = 0;
1109 llsol.s6_addr32[2] = htonl(1);
1110 llsol.s6_addr32[3] =
1111 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1112 llsol.s6_addr8[12] = 0xff;
1113 in6_addmulti(&llsol, ifp, &error);
1116 "in6_update_ifa: addmulti failed for "
1117 "%s on %s (errno=%d)\n",
1118 ip6_sprintf(&llsol), if_name(ifp),
1120 in6_purgeaddr((struct ifaddr *)ia);
1125 bzero(&mltmask, sizeof(mltmask));
1126 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1127 mltmask.sin6_family = AF_INET6;
1128 mltmask.sin6_addr = in6mask32;
1131 * join link-local all-nodes address
1133 bzero(&mltaddr, sizeof(mltaddr));
1134 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1135 mltaddr.sin6_family = AF_INET6;
1136 mltaddr.sin6_addr = kin6addr_linklocal_allnodes;
1137 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1139 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
1141 rtrequest_global(RTM_ADD,
1142 (struct sockaddr *)&mltaddr,
1143 (struct sockaddr *)&ia->ia_addr,
1144 (struct sockaddr *)&mltmask,
1145 RTF_UP|RTF_CLONING); /* xxx */
1146 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1149 "in6_update_ifa: addmulti failed for "
1150 "%s on %s (errno=%d)\n",
1151 ip6_sprintf(&mltaddr.sin6_addr),
1152 if_name(ifp), error);
1157 * join node information group address
1159 #define hostnamelen strlen(hostname)
1160 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1162 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
1163 if (in6m == NULL && ia != NULL) {
1164 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1166 log(LOG_WARNING, "in6_update_ifa: "
1167 "addmulti failed for "
1168 "%s on %s (errno=%d)\n",
1169 ip6_sprintf(&mltaddr.sin6_addr),
1170 if_name(ifp), error);
1177 * join node-local all-nodes address, on loopback.
1178 * XXX: since "node-local" is obsoleted by interface-local,
1179 * we have to join the group on every interface with
1180 * some interface-boundary restriction.
1182 if (ifp->if_flags & IFF_LOOPBACK) {
1183 struct in6_ifaddr *ia_loop;
1185 struct in6_addr loop6 = kin6addr_loopback;
1186 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1188 mltaddr.sin6_addr = kin6addr_nodelocal_allnodes;
1190 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
1191 if (in6m == NULL && ia_loop != NULL) {
1192 rtrequest_global(RTM_ADD,
1193 (struct sockaddr *)&mltaddr,
1194 (struct sockaddr *)&ia_loop->ia_addr,
1195 (struct sockaddr *)&mltmask,
1197 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1199 log(LOG_WARNING, "in6_update_ifa: "
1200 "addmulti failed for %s on %s "
1202 ip6_sprintf(&mltaddr.sin6_addr),
1203 if_name(ifp), error);
1209 ia->ia6_flags = ifra->ifra_flags;
1210 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1211 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1213 ia->ia6_lifetime = ifra->ifra_lifetime;
1215 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1216 ia->ia6_lifetime.ia6t_expire =
1217 time_uptime + ia->ia6_lifetime.ia6t_vltime;
1219 ia->ia6_lifetime.ia6t_expire = 0;
1220 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1221 ia->ia6_lifetime.ia6t_preferred =
1222 time_uptime + ia->ia6_lifetime.ia6t_pltime;
1224 ia->ia6_lifetime.ia6t_preferred = 0;
1227 * Perform DAD, if needed.
1228 * XXX It may be of use, if we can administratively
1231 if (in6if_do_dad(ifp) && !(ifra->ifra_flags & IN6_IFF_NODAD)) {
1232 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1233 nd6_dad_start((struct ifaddr *)ia, NULL);
1240 * XXX: if a change of an existing address failed, keep the entry
1244 in6_unlink_ifa(ia, ifp);
1249 in6_purgeaddr(struct ifaddr *ifa)
1251 struct ifnet *ifp = ifa->ifa_ifp;
1252 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1254 /* stop DAD processing */
1258 * delete route to the destination of the address being purged.
1259 * The interface must be p2p or loopback in this case.
1261 if ((ia->ia_flags & IFA_ROUTE) && ia->ia_dstaddr.sin6_len != 0) {
1264 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1266 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1267 "a route to the p2p destination: %s on %s, "
1269 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1271 /* proceed anyway... */
1274 ia->ia_flags &= ~IFA_ROUTE;
1277 /* Remove ownaddr's loopback rtentry, if it exists. */
1278 in6_ifremloop(&(ia->ia_ifa));
1280 if (ifp->if_flags & IFF_MULTICAST) {
1282 * delete solicited multicast addr for deleting host id
1284 struct in6_multi *in6m;
1285 struct in6_addr llsol;
1286 bzero(&llsol, sizeof(struct in6_addr));
1287 llsol.s6_addr16[0] = htons(0xff02);
1288 llsol.s6_addr16[1] = htons(ifp->if_index);
1289 llsol.s6_addr32[1] = 0;
1290 llsol.s6_addr32[2] = htonl(1);
1291 llsol.s6_addr32[3] =
1292 ia->ia_addr.sin6_addr.s6_addr32[3];
1293 llsol.s6_addr8[12] = 0xff;
1295 in6m = IN6_LOOKUP_MULTI(&llsol, ifp);
1300 in6_unlink_ifa(ia, ifp);
1304 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1306 struct in6_ifaddr *oia;
1310 ifa_ifunlink(&ia->ia_ifa, ifp);
1313 if (oia == (ia = in6_ifaddr))
1314 in6_ifaddr = ia->ia_next;
1316 while (ia->ia_next && (ia->ia_next != oia))
1319 ia->ia_next = oia->ia_next;
1322 kprintf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1327 * When an autoconfigured address is being removed, release the
1328 * reference to the base prefix. Also, since the release might
1329 * affect the status of other (detached) addresses, call
1330 * pfxlist_onlink_check().
1332 if (oia->ia6_flags & IN6_IFF_AUTOCONF) {
1333 if (oia->ia6_ndpr == NULL) {
1334 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1335 "%p has no prefix\n", oia);
1337 oia->ia6_ndpr->ndpr_refcnt--;
1338 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1339 oia->ia6_ndpr = NULL;
1342 pfxlist_onlink_check();
1346 * release another refcnt for the link from in6_ifaddr.
1347 * Note that we should decrement the refcnt at least once for all *BSD.
1349 ifa_destroy(&oia->ia_ifa);
1355 in6_purgeif(struct ifnet *ifp)
1357 struct ifaddr_container *ifac, *next;
1359 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
1361 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
1363 in6_purgeaddr(ifac->ifa);
1371 * SIOCGLIFADDR: get first address. (?)
1372 * SIOCGLIFADDR with IFLR_PREFIX:
1373 * get first address that matches the specified prefix.
1374 * SIOCALIFADDR: add the specified address.
1375 * SIOCALIFADDR with IFLR_PREFIX:
1376 * add the specified prefix, filling hostid part from
1377 * the first link-local address. prefixlen must be <= 64.
1378 * SIOCDLIFADDR: delete the specified address.
1379 * SIOCDLIFADDR with IFLR_PREFIX:
1380 * delete the first address that matches the specified prefix.
1382 * EINVAL on invalid parameters
1383 * EADDRNOTAVAIL on prefix match failed/specified address not found
1384 * other values may be returned from in6_ioctl()
1386 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1387 * this is to accomodate address naming scheme other than RFC2374,
1389 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1390 * address encoding scheme. (see figure on page 8)
1393 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1394 struct ifnet *ifp, struct thread *td)
1396 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1397 struct sockaddr *sa;
1400 if (!data || !ifp) {
1401 panic("invalid argument to in6_lifaddr_ioctl");
1407 /* address must be specified on GET with IFLR_PREFIX */
1408 if (!(iflr->flags & IFLR_PREFIX))
1413 /* address must be specified on ADD and DELETE */
1414 sa = (struct sockaddr *)&iflr->addr;
1415 if (sa->sa_family != AF_INET6)
1417 if (sa->sa_len != sizeof(struct sockaddr_in6))
1419 /* XXX need improvement */
1420 sa = (struct sockaddr *)&iflr->dstaddr;
1421 if (sa->sa_family && sa->sa_family != AF_INET6)
1423 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1426 default: /* shouldn't happen */
1428 panic("invalid cmd to in6_lifaddr_ioctl");
1434 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1440 struct in6_aliasreq ifra;
1441 struct in6_addr *hostid = NULL;
1444 if (iflr->flags & IFLR_PREFIX) {
1446 struct sockaddr_in6 *sin6;
1449 * hostid is to fill in the hostid part of the
1450 * address. hostid points to the first link-local
1451 * address attached to the interface.
1453 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1455 return EADDRNOTAVAIL;
1456 hostid = IFA_IN6(ifa);
1458 /* prefixlen must be <= 64. */
1459 if (64 < iflr->prefixlen)
1461 prefixlen = iflr->prefixlen;
1463 /* hostid part must be zero. */
1464 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1465 if (sin6->sin6_addr.s6_addr32[2] != 0
1466 || sin6->sin6_addr.s6_addr32[3] != 0) {
1470 prefixlen = iflr->prefixlen;
1472 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1473 bzero(&ifra, sizeof(ifra));
1474 bcopy(iflr->iflr_name, ifra.ifra_name,
1475 sizeof(ifra.ifra_name));
1477 bcopy(&iflr->addr, &ifra.ifra_addr,
1478 ((struct sockaddr *)&iflr->addr)->sa_len);
1480 /* fill in hostid part */
1481 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1482 hostid->s6_addr32[2];
1483 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1484 hostid->s6_addr32[3];
1487 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1488 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1489 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1491 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1492 hostid->s6_addr32[2];
1493 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1494 hostid->s6_addr32[3];
1498 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1499 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1501 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1502 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1507 struct ifaddr_container *ifac;
1508 struct in6_ifaddr *ia;
1509 struct in6_addr mask, candidate, match;
1510 struct sockaddr_in6 *sin6;
1513 bzero(&mask, sizeof(mask));
1514 if (iflr->flags & IFLR_PREFIX) {
1515 /* lookup a prefix rather than address. */
1516 in6_len2mask(&mask, iflr->prefixlen);
1518 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1519 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1520 match.s6_addr32[0] &= mask.s6_addr32[0];
1521 match.s6_addr32[1] &= mask.s6_addr32[1];
1522 match.s6_addr32[2] &= mask.s6_addr32[2];
1523 match.s6_addr32[3] &= mask.s6_addr32[3];
1525 /* if you set extra bits, that's wrong */
1526 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1531 if (cmd == SIOCGLIFADDR) {
1532 /* on getting an address, take the 1st match */
1535 /* on deleting an address, do exact match */
1536 in6_len2mask(&mask, 128);
1537 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1538 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1544 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1545 struct ifaddr *ifa = ifac->ifa;
1547 if (ifa->ifa_addr->sa_family != AF_INET6)
1552 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1554 * XXX: this is adhoc, but is necessary to allow
1555 * a user to specify fe80::/64 (not /10) for a
1556 * link-local address.
1558 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1559 candidate.s6_addr16[1] = 0;
1560 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1561 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1562 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1563 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1564 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1568 return EADDRNOTAVAIL;
1569 ia = ifa2ia6(ifac->ifa);
1571 if (cmd == SIOCGLIFADDR) {
1572 struct sockaddr_in6 *s6;
1574 /* fill in the if_laddrreq structure */
1575 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1576 s6 = (struct sockaddr_in6 *)&iflr->addr;
1577 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1578 s6->sin6_addr.s6_addr16[1] = 0;
1580 in6_addr2scopeid(ifp, &s6->sin6_addr);
1582 if (ifp->if_flags & IFF_POINTOPOINT) {
1583 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1584 ia->ia_dstaddr.sin6_len);
1585 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1586 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1587 s6->sin6_addr.s6_addr16[1] = 0;
1589 in6_addr2scopeid(ifp,
1593 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1596 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1599 iflr->flags = ia->ia6_flags; /* XXX */
1603 struct in6_aliasreq ifra;
1605 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1606 bzero(&ifra, sizeof(ifra));
1607 bcopy(iflr->iflr_name, ifra.ifra_name,
1608 sizeof(ifra.ifra_name));
1610 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1611 ia->ia_addr.sin6_len);
1612 if (ifp->if_flags & IFF_POINTOPOINT)
1613 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1614 ia->ia_dstaddr.sin6_len);
1616 bzero(&ifra.ifra_dstaddr,
1617 sizeof(ifra.ifra_dstaddr));
1618 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1619 ia->ia_prefixmask.sin6_len);
1621 ifra.ifra_flags = ia->ia6_flags;
1622 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1628 return EOPNOTSUPP; /* just for safety */
1632 * Initialize an interface's intetnet6 address
1633 * and routing table entry.
1636 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6,
1639 int error = 0, plen, ifacount = 0;
1640 struct ifaddr_container *ifac;
1643 * Give the interface a chance to initialize
1644 * if this is its first address,
1645 * and to validate the address if necessary.
1647 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1648 if (ifac->ifa->ifa_addr == NULL)
1649 continue; /* just for safety */
1650 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
1655 ifnet_serialize_all(ifp);
1657 ia->ia_addr = *sin6;
1659 if (ifacount <= 1 && ifp->if_ioctl &&
1660 (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL))) {
1661 ifnet_deserialize_all(ifp);
1665 ifnet_deserialize_all(ifp);
1667 ia->ia_ifa.ifa_metric = ifp->if_metric;
1669 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1673 * If the destination address is specified for a point-to-point
1674 * interface, install a route to the destination as an interface
1677 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1678 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1679 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1680 RTF_UP | RTF_HOST)) != 0)
1682 ia->ia_flags |= IFA_ROUTE;
1686 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1688 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1691 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1693 /* set the rtrequest function to create llinfo */
1694 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1695 in6_ifaddloop(&(ia->ia_ifa));
1701 struct in6_multi_mship *
1702 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr, int *errorp)
1704 struct in6_multi_mship *imm;
1706 imm = kmalloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
1711 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
1712 if (!imm->i6mm_maddr) {
1713 /* *errorp is alrady set */
1714 kfree(imm, M_IPMADDR);
1721 in6_leavegroup(struct in6_multi_mship *imm)
1724 if (imm->i6mm_maddr)
1725 in6_delmulti(imm->i6mm_maddr);
1726 kfree(imm, M_IPMADDR);
1731 * Add an address to the list of IP6 multicast addresses for a
1735 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
1737 struct in6_multi *in6m;
1738 struct sockaddr_in6 sin6;
1739 struct ifmultiaddr *ifma;
1746 * Call generic routine to add membership or increment
1747 * refcount. It wants addresses in the form of a sockaddr,
1748 * so we build one here (being careful to zero the unused bytes).
1750 bzero(&sin6, sizeof sin6);
1751 sin6.sin6_family = AF_INET6;
1752 sin6.sin6_len = sizeof sin6;
1753 sin6.sin6_addr = *maddr6;
1754 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1761 * If ifma->ifma_protospec is null, then if_addmulti() created
1762 * a new record. Otherwise, we are done.
1764 if (ifma->ifma_protospec != NULL) {
1766 return ifma->ifma_protospec;
1769 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1770 at interrupt time? If so, need to fix if_addmulti. XXX */
1771 in6m = (struct in6_multi *)kmalloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1777 bzero(in6m, sizeof *in6m);
1778 in6m->in6m_addr = *maddr6;
1779 in6m->in6m_ifp = ifp;
1780 in6m->in6m_ifma = ifma;
1781 ifma->ifma_protospec = in6m;
1782 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1785 * Let MLD6 know that we have joined a new IP6 multicast
1788 mld6_start_listening(in6m);
1794 * Delete a multicast address record.
1797 in6_delmulti(struct in6_multi *in6m)
1799 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1803 if (ifma->ifma_refcount == 1) {
1805 * No remaining claims to this record; let MLD6 know
1806 * that we are leaving the multicast group.
1808 mld6_stop_listening(in6m);
1809 ifma->ifma_protospec = NULL;
1810 LIST_REMOVE(in6m, in6m_entry);
1811 kfree(in6m, M_IPMADDR);
1813 /* XXX - should be separate API for when we have an ifma? */
1814 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1819 * Find an IPv6 interface link-local address specific to an interface.
1822 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1824 const struct ifaddr_container *ifac;
1826 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1827 struct ifaddr *ifa = ifac->ifa;
1829 if (ifa->ifa_addr == NULL)
1830 continue; /* just for safety */
1831 if (ifa->ifa_addr->sa_family != AF_INET6)
1833 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1834 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1837 return (struct in6_ifaddr *)ifa;
1845 * find the internet address corresponding to a given interface and address.
1848 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1850 const struct ifaddr_container *ifac;
1852 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1853 struct ifaddr *ifa = ifac->ifa;
1855 if (ifa->ifa_addr == NULL)
1856 continue; /* just for safety */
1857 if (ifa->ifa_addr->sa_family != AF_INET6)
1859 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1860 return (struct in6_ifaddr *)ifa;
1866 * Find a link-local scoped address on ifp and return it if any.
1869 in6ifa_llaonifp(struct ifnet *ifp)
1871 const struct ifaddr_container *ifac;
1873 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1874 const struct sockaddr_in6 *sin6;
1875 struct ifaddr *ifa = ifac->ifa;
1877 if (ifa->ifa_addr->sa_family != AF_INET6)
1879 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
1880 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
1881 /* XXX why are mcast addresses ifp address list? */
1882 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
1883 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
1884 return (struct in6_ifaddr *)ifa;
1890 * find the internet address on a given interface corresponding to a neighbor's
1894 in6ifa_ifplocaladdr(const struct ifnet *ifp, const struct in6_addr *addr)
1897 struct in6_ifaddr *ia;
1898 struct ifaddr_container *ifac;
1900 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1903 if (ifa->ifa_addr == NULL)
1904 continue; /* just for safety */
1905 if (ifa->ifa_addr->sa_family != AF_INET6)
1907 ia = (struct in6_ifaddr *)ifa;
1908 if (IN6_ARE_MASKED_ADDR_EQUAL(addr,
1909 &ia->ia_addr.sin6_addr,
1910 &ia->ia_prefixmask.sin6_addr))
1918 * Convert IP6 address to printable (loggable) representation.
1920 static char digits[] = "0123456789abcdef";
1921 static int ip6round = 0;
1923 ip6_sprintf(const struct in6_addr *addr)
1925 static char ip6buf[8][48];
1928 const u_short *a = (const u_short *)addr;
1932 ip6round = (ip6round + 1) & 7;
1933 cp = ip6buf[ip6round];
1935 for (i = 0; i < 8; i++) {
1946 if (dcolon == 0 && *(a + 1) == 0) {
1958 d = (const u_char *)a;
1959 *cp++ = digits[*d >> 4];
1960 *cp++ = digits[*d++ & 0xf];
1961 *cp++ = digits[*d >> 4];
1962 *cp++ = digits[*d & 0xf];
1967 return (ip6buf[ip6round]);
1971 in6_localaddr(struct in6_addr *in6)
1973 struct in6_ifaddr *ia;
1975 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1978 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1979 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1980 &ia->ia_prefixmask.sin6_addr))
1987 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1989 struct in6_ifaddr *ia;
1991 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1992 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1994 (ia->ia6_flags & IN6_IFF_DEPRECATED))
1995 return (1); /* true */
1997 /* XXX: do we still have to go thru the rest of the list? */
2000 return (0); /* false */
2004 * return length of part which dst and src are equal
2008 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
2011 u_char *s = (u_char *)src, *d = (u_char *)dst;
2012 u_char *lim = s + 16, r;
2015 if ((r = (*d++ ^ *s++)) != 0) {
2026 /* XXX: to be scope conscious */
2028 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
2030 int bytelen, bitlen;
2033 if (0 > len || len > 128) {
2034 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2042 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2044 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
2045 p2->s6_addr[bytelen] >> (8 - bitlen))
2052 in6_prefixlen2mask(struct in6_addr *maskp, int len)
2054 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2055 int bytelen, bitlen, i;
2058 if (0 > len || len > 128) {
2059 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2064 bzero(maskp, sizeof(*maskp));
2067 for (i = 0; i < bytelen; i++)
2068 maskp->s6_addr[i] = 0xff;
2070 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2074 * return the best address out of the same scope
2077 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst, struct ucred *cred)
2079 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
2082 struct in6_ifaddr *ifa_best = NULL;
2085 if(cred && cred->cr_prison)
2090 kprintf("in6_ifawithscope: output interface is not specified\n");
2096 * We search for all addresses on all interfaces from the beginning.
2097 * Comparing an interface with the outgoing interface will be done
2098 * only at the final stage of tiebreaking.
2100 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2102 struct ifaddr_container *ifac;
2105 * We can never take an address that breaks the scope zone
2106 * of the destination.
2108 if (ifp->if_afdata[AF_INET6] == NULL)
2110 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
2113 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2114 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
2115 struct ifaddr *ifa = ifac->ifa;
2117 if (ifa->ifa_addr->sa_family != AF_INET6)
2120 src_scope = in6_addrscope(IFA_IN6(ifa));
2123 * Don't use an address before completing DAD
2124 * nor a duplicated address.
2126 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2130 /* XXX: is there any case to allow anycasts? */
2131 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2135 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2139 /* Skip adresses not valid for current jail */
2141 !jailed_ip(cred->cr_prison, (struct sockaddr *)(ifa->ifa_addr)) != 0)
2145 * If this is the first address we find,
2148 if (ifa_best == NULL)
2152 * ifa_best is never NULL beyond this line except
2153 * within the block labeled "replace".
2157 * If ifa_best has a smaller scope than dst and
2158 * the current address has a larger one than
2159 * (or equal to) dst, always replace ifa_best.
2160 * Also, if the current address has a smaller scope
2161 * than dst, ignore it unless ifa_best also has a
2163 * Consequently, after the two if-clause below,
2164 * the followings must be satisfied:
2165 * (scope(src) < scope(dst) &&
2166 * scope(best) < scope(dst))
2168 * (scope(best) >= scope(dst) &&
2169 * scope(src) >= scope(dst))
2171 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2172 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2173 goto replace; /* (A) */
2174 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2175 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2179 * A deprecated address SHOULD NOT be used in new
2180 * communications if an alternate (non-deprecated)
2181 * address is available and has sufficient scope.
2182 * RFC 2462, Section 5.5.4.
2184 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2185 IN6_IFF_DEPRECATED) {
2187 * Ignore any deprecated addresses if
2188 * specified by configuration.
2190 if (!ip6_use_deprecated)
2194 * If we have already found a non-deprecated
2195 * candidate, just ignore deprecated addresses.
2197 if (!(ifa_best->ia6_flags & IN6_IFF_DEPRECATED))
2202 * A non-deprecated address is always preferred
2203 * to a deprecated one regardless of scopes and
2204 * address matching (Note invariants ensured by the
2205 * conditions (A) and (B) above.)
2207 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2208 !(((struct in6_ifaddr *)ifa)->ia6_flags &
2209 IN6_IFF_DEPRECATED))
2213 * When we use temporary addresses described in
2214 * RFC 3041, we prefer temporary addresses to
2215 * public autoconf addresses. Again, note the
2216 * invariants from (A) and (B). Also note that we
2217 * don't have any preference between static addresses
2218 * and autoconf addresses (despite of whether or not
2219 * the latter is temporary or public.)
2221 if (ip6_use_tempaddr) {
2222 struct in6_ifaddr *ifat;
2224 ifat = (struct in6_ifaddr *)ifa;
2225 if ((ifa_best->ia6_flags &
2226 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2227 == IN6_IFF_AUTOCONF &&
2229 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2230 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2233 if ((ifa_best->ia6_flags &
2234 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2235 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2237 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2238 == IN6_IFF_AUTOCONF) {
2244 * At this point, we have two cases:
2245 * 1. we are looking at a non-deprecated address,
2246 * and ifa_best is also non-deprecated.
2247 * 2. we are looking at a deprecated address,
2248 * and ifa_best is also deprecated.
2249 * Also, we do not have to consider a case where
2250 * the scope of if_best is larger(smaller) than dst and
2251 * the scope of the current address is smaller(larger)
2252 * than dst. Such a case has already been covered.
2253 * Tiebreaking is done according to the following
2255 * - the scope comparison between the address and
2257 * - the scope comparison between the address and
2258 * ifa_best (bscopecmp)
2259 * - if the address match dst longer than ifa_best
2261 * - if the address is on the outgoing I/F (outI/F)
2263 * Roughly speaking, the selection policy is
2264 * - the most important item is scope. The same scope
2265 * is best. Then search for a larger scope.
2266 * Smaller scopes are the last resort.
2267 * - A deprecated address is chosen only when we have
2268 * no address that has an enough scope, but is
2269 * prefered to any addresses of smaller scopes
2270 * (this must be already done above.)
2271 * - addresses on the outgoing I/F are preferred to
2272 * ones on other interfaces if none of above
2273 * tiebreaks. In the table below, the column "bI"
2274 * means if the best_ifa is on the outgoing
2275 * interface, and the column "sI" means if the ifa
2276 * is on the outgoing interface.
2277 * - If there is no other reasons to choose one,
2278 * longest address match against dst is considered.
2280 * The precise decision table is as follows:
2281 * dscopecmp bscopecmp match bI oI | replace?
2282 * N/A equal N/A Y N | No (1)
2283 * N/A equal N/A N Y | Yes (2)
2284 * N/A equal larger N/A | Yes (3)
2285 * N/A equal !larger N/A | No (4)
2286 * larger larger N/A N/A | No (5)
2287 * larger smaller N/A N/A | Yes (6)
2288 * smaller larger N/A N/A | Yes (7)
2289 * smaller smaller N/A N/A | No (8)
2290 * equal smaller N/A N/A | Yes (9)
2291 * equal larger (already done at A above)
2293 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2294 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2296 if (bscopecmp == 0) {
2297 struct ifnet *bifp = ifa_best->ia_ifp;
2299 if (bifp == oifp && ifp != oifp) /* (1) */
2301 if (bifp != oifp && ifp == oifp) /* (2) */
2305 * Both bifp and ifp are on the outgoing
2306 * interface, or both two are on a different
2307 * interface from the outgoing I/F.
2308 * now we need address matching against dst
2311 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2312 matchcmp = tlen - blen;
2313 if (matchcmp > 0) /* (3) */
2317 if (dscopecmp > 0) {
2318 if (bscopecmp > 0) /* (5) */
2320 goto replace; /* (6) */
2322 if (dscopecmp < 0) {
2323 if (bscopecmp > 0) /* (7) */
2328 /* now dscopecmp must be 0 */
2330 goto replace; /* (9) */
2333 ifa_best = (struct in6_ifaddr *)ifa;
2334 blen = tlen >= 0 ? tlen :
2335 in6_matchlen(IFA_IN6(ifa), dst);
2336 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2340 /* count statistics for future improvements */
2341 if (ifa_best == NULL)
2342 ip6stat.ip6s_sources_none++;
2344 if (oifp == ifa_best->ia_ifp)
2345 ip6stat.ip6s_sources_sameif[best_scope]++;
2347 ip6stat.ip6s_sources_otherif[best_scope]++;
2349 if (best_scope == dst_scope)
2350 ip6stat.ip6s_sources_samescope[best_scope]++;
2352 ip6stat.ip6s_sources_otherscope[best_scope]++;
2354 if (ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2355 ip6stat.ip6s_sources_deprecated[best_scope]++;
2362 * return the best address out of the same scope. if no address was
2363 * found, return the first valid address from designated IF.
2366 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2368 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2369 struct ifaddr_container *ifac;
2370 struct in6_ifaddr *besta = NULL;
2371 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2373 dep[0] = dep[1] = NULL;
2376 * We first look for addresses in the same scope.
2377 * If there is one, return it.
2378 * If two or more, return one which matches the dst longest.
2379 * If none, return one of global addresses assigned other ifs.
2381 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2382 struct ifaddr *ifa = ifac->ifa;
2384 if (ifa->ifa_addr->sa_family != AF_INET6)
2386 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2387 continue; /* XXX: is there any case to allow anycast? */
2388 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2389 continue; /* don't use this interface */
2390 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2392 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2393 if (ip6_use_deprecated)
2394 dep[0] = (struct in6_ifaddr *)ifa;
2398 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2400 * call in6_matchlen() as few as possible
2404 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2405 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2408 besta = (struct in6_ifaddr *)ifa;
2411 besta = (struct in6_ifaddr *)ifa;
2417 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2418 struct ifaddr *ifa = ifac->ifa;
2420 if (ifa->ifa_addr->sa_family != AF_INET6)
2422 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2423 continue; /* XXX: is there any case to allow anycast? */
2424 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2425 continue; /* don't use this interface */
2426 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2428 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2429 if (ip6_use_deprecated)
2430 dep[1] = (struct in6_ifaddr *)ifa;
2434 return (struct in6_ifaddr *)ifa;
2437 /* use the last-resort values, that are, deprecated addresses */
2447 * perform DAD when interface becomes IFF_UP.
2450 in6_if_up(struct ifnet *ifp)
2452 struct ifaddr_container *ifac;
2453 struct in6_ifaddr *ia;
2454 int dad_delay; /* delay ticks before DAD output */
2457 * special cases, like 6to4, are handled in in6_ifattach
2459 in6_ifattach(ifp, NULL);
2462 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2463 struct ifaddr *ifa = ifac->ifa;
2465 if (ifa->ifa_addr->sa_family != AF_INET6)
2467 ia = (struct in6_ifaddr *)ifa;
2468 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2469 nd6_dad_start(ifa, &dad_delay);
2474 in6if_do_dad(struct ifnet *ifp)
2476 if (ifp->if_flags & IFF_LOOPBACK)
2479 switch (ifp->if_type) {
2485 * These interfaces do not have the IFF_LOOPBACK flag,
2486 * but loop packets back. We do not have to do DAD on such
2487 * interfaces. We should even omit it, because loop-backed
2488 * NS would confuse the DAD procedure.
2493 * Our DAD routine requires the interface up and running.
2494 * However, some interfaces can be up before the RUNNING
2495 * status. Additionaly, users may try to assign addresses
2496 * before the interface becomes up (or running).
2497 * We simply skip DAD in such a case as a work around.
2498 * XXX: we should rather mark "tentative" on such addresses,
2499 * and do DAD after the interface becomes ready.
2501 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2502 (IFF_UP|IFF_RUNNING))
2510 * Calculate max IPv6 MTU through all the interfaces and store it
2516 unsigned long maxmtu = 0;
2519 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2521 /* this function can be called during ifnet initialization */
2522 if (ifp->if_afdata[AF_INET6] == NULL)
2524 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2525 ND_IFINFO(ifp)->linkmtu > maxmtu)
2526 maxmtu = ND_IFINFO(ifp)->linkmtu;
2528 if (maxmtu) /* update only when maxmtu is positive */
2529 in6_maxmtu = maxmtu;
2533 in6_domifattach(struct ifnet *ifp)
2535 struct in6_ifextra *ext;
2537 ext = (struct in6_ifextra *)kmalloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2538 bzero(ext, sizeof(*ext));
2540 ext->in6_ifstat = (struct in6_ifstat *)kmalloc(sizeof(struct in6_ifstat),
2541 M_IFADDR, M_WAITOK);
2542 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2545 (struct icmp6_ifstat *)kmalloc(sizeof(struct icmp6_ifstat),
2546 M_IFADDR, M_WAITOK);
2547 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2549 ext->nd_ifinfo = nd6_ifattach(ifp);
2550 ext->scope6_id = scope6_ifattach(ifp);
2555 in6_domifdetach(struct ifnet *ifp, void *aux)
2557 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2558 scope6_ifdetach(ext->scope6_id);
2559 nd6_ifdetach(ext->nd_ifinfo);
2560 kfree(ext->in6_ifstat, M_IFADDR);
2561 kfree(ext->icmp6_ifstat, M_IFADDR);
2562 kfree(ext, M_IFADDR);