1 /* $FreeBSD: src/sys/netinet6/in6.c,v 1.7.2.9 2002/04/28 05:40:26 suz Exp $ */
2 /* $DragonFly: src/sys/netinet6/in6.c,v 1.9 2004/09/10 14:02:01 joerg Exp $ */
3 /* $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)in.c 8.2 (Berkeley) 11/15/93
70 #include "opt_inet6.h"
72 #include <sys/param.h>
73 #include <sys/errno.h>
74 #include <sys/malloc.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/sockio.h>
78 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/syslog.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>
93 #include <netinet/in_systm.h>
94 #include <netinet/ip.h>
95 #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 #ifndef SCOPEDROUTING
106 #include <netinet6/in6_pcb.h>
109 #include <net/net_osdep.h>
111 MALLOC_DEFINE(M_IPMADDR, "in6_multi", "internet multicast address");
114 * Definitions of some costant IP6 addresses.
116 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
117 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
118 const struct in6_addr in6addr_nodelocal_allnodes =
119 IN6ADDR_NODELOCAL_ALLNODES_INIT;
120 const struct in6_addr in6addr_linklocal_allnodes =
121 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
122 const struct in6_addr in6addr_linklocal_allrouters =
123 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
125 const struct in6_addr in6mask0 = IN6MASK0;
126 const struct in6_addr in6mask32 = IN6MASK32;
127 const struct in6_addr in6mask64 = IN6MASK64;
128 const struct in6_addr in6mask96 = IN6MASK96;
129 const struct in6_addr in6mask128 = IN6MASK128;
131 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
132 0, 0, IN6ADDR_ANY_INIT, 0};
134 static int in6_lifaddr_ioctl (struct socket *, u_long, caddr_t,
135 struct ifnet *, struct thread *);
136 static int in6_ifinit (struct ifnet *, struct in6_ifaddr *,
137 struct sockaddr_in6 *, int);
138 static void in6_unlink_ifa (struct in6_ifaddr *, struct ifnet *);
140 struct in6_multihead in6_multihead; /* XXX BSS initialization */
142 int (*faithprefix_p)(struct in6_addr *);
145 * Subroutine for in6_ifaddloop() and in6_ifremloop().
146 * This routine does actual work.
149 in6_ifloop_request(int cmd, struct ifaddr *ifa)
151 struct sockaddr_in6 all1_sa;
152 struct rtentry *nrt = NULL;
155 bzero(&all1_sa, sizeof(all1_sa));
156 all1_sa.sin6_family = AF_INET6;
157 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
158 all1_sa.sin6_addr = in6mask128;
161 * We specify the address itself as the gateway, and set the
162 * RTF_LLINFO flag, so that the corresponding host route would have
163 * the flag, and thus applications that assume traditional behavior
164 * would be happy. Note that we assume the caller of the function
165 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
166 * which changes the outgoing interface to the loopback interface.
168 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
169 (struct sockaddr *)&all1_sa,
170 RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
172 log(LOG_ERR, "in6_ifloop_request: "
173 "%s operation failed for %s (errno=%d)\n",
174 cmd == RTM_ADD ? "ADD" : "DELETE",
175 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
180 * Make sure rt_ifa be equal to IFA, the second argument of the
182 * We need this because when we refer to rt_ifa->ia6_flags in
183 * ip6_input, we assume that the rt_ifa points to the address instead
184 * of the loopback address.
186 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
187 IFAFREE(nrt->rt_ifa);
193 * Report the addition/removal of the address to the routing socket.
194 * XXX: since we called rtinit for a p2p interface with a destination,
195 * we end up reporting twice in such a case. Should we rather
196 * omit the second report?
199 rt_newaddrmsg(cmd, ifa, e, nrt);
200 if (cmd == RTM_DELETE) {
201 if (nrt->rt_refcnt <= 0) {
202 /* XXX: we should free the entry ourselves. */
207 /* the cmd must be RTM_ADD here */
214 * Add ownaddr as loopback rtentry. We previously add the route only if
215 * necessary (ex. on a p2p link). However, since we now manage addresses
216 * separately from prefixes, we should always add the route. We can't
217 * rely on the cloning mechanism from the corresponding interface route
221 in6_ifaddloop(struct ifaddr *ifa)
225 /* If there is no loopback entry, allocate one. */
226 rt = rtalloc1(ifa->ifa_addr, 0, 0);
227 if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
228 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
229 in6_ifloop_request(RTM_ADD, ifa);
235 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
239 in6_ifremloop(struct ifaddr *ifa)
241 struct in6_ifaddr *ia;
246 * Some of BSD variants do not remove cloned routes
247 * from an interface direct route, when removing the direct route
248 * (see comments in net/net_osdep.h). Even for variants that do remove
249 * cloned routes, they could fail to remove the cloned routes when
250 * we handle multple addresses that share a common prefix.
251 * So, we should remove the route corresponding to the deleted address
252 * regardless of the result of in6_is_ifloop_auto().
256 * Delete the entry only if exact one ifa exists. More than one ifa
257 * can exist if we assign a same single address to multiple
258 * (probably p2p) interfaces.
259 * XXX: we should avoid such a configuration in IPv6...
261 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
262 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
271 * Before deleting, check if a corresponding loopbacked host
272 * route surely exists. With this check, we can avoid to
273 * delete an interface direct route whose destination is same
274 * as the address being removed. This can happen when remofing
275 * a subnet-router anycast address on an interface attahced
276 * to a shared medium.
278 rt = rtalloc1(ifa->ifa_addr, 0, 0);
279 if (rt != NULL && (rt->rt_flags & RTF_HOST) != 0 &&
280 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
282 in6_ifloop_request(RTM_DELETE, ifa);
288 in6_ifindex2scopeid(int idx)
292 struct sockaddr_in6 *sin6;
294 if (idx < 0 || if_index < idx)
296 ifp = ifindex2ifnet[idx];
298 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
300 if (ifa->ifa_addr->sa_family != AF_INET6)
302 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
303 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
304 return sin6->sin6_scope_id & 0xffff;
311 in6_mask2len(struct in6_addr *mask, u_char *lim0)
314 u_char *lim = lim0, *p;
317 lim0 - (u_char *)mask > sizeof(*mask)) /* ignore the scope_id part */
318 lim = (u_char *)mask + sizeof(*mask);
319 for (p = (u_char *)mask; p < lim; x++, p++) {
325 for (y = 0; y < 8; y++) {
326 if ((*p & (0x80 >> y)) == 0)
332 * when the limit pointer is given, do a stricter check on the
336 if (y != 0 && (*p & (0x00ff >> y)) != 0)
338 for (p = p + 1; p < lim; p++)
347 in6_len2mask(struct in6_addr *mask, int len)
351 bzero(mask, sizeof(*mask));
352 for (i = 0; i < len / 8; i++)
353 mask->s6_addr8[i] = 0xff;
355 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
358 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
359 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
362 in6_control(struct socket *so, u_long cmd, caddr_t data,
363 struct ifnet *ifp, struct thread *td)
365 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
366 struct in6_ifaddr *ia = NULL;
367 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
375 case SIOCGETSGCNT_IN6:
376 case SIOCGETMIFCNT_IN6:
377 return (mrt6_ioctl(cmd, data));
384 case SIOCSNDFLUSH_IN6:
385 case SIOCSPFXFLUSH_IN6:
386 case SIOCSRTRFLUSH_IN6:
387 case SIOCSDEFIFACE_IN6:
388 case SIOCSIFINFO_FLAGS:
392 case OSIOCGIFINFO_IN6:
393 case SIOCGIFINFO_IN6:
396 case SIOCGNBRINFO_IN6:
397 case SIOCGDEFIFACE_IN6:
398 return(nd6_ioctl(cmd, data, ifp));
402 case SIOCSIFPREFIX_IN6:
403 case SIOCDIFPREFIX_IN6:
404 case SIOCAIFPREFIX_IN6:
405 case SIOCCIFPREFIX_IN6:
406 case SIOCSGIFPREFIX_IN6:
407 case SIOCGIFPREFIX_IN6:
409 "prefix ioctls are now invalidated. "
410 "please use ifconfig.\n");
418 return(scope6_set(ifp, ifr->ifr_ifru.ifru_scope_id));
421 return(scope6_get(ifp, ifr->ifr_ifru.ifru_scope_id));
424 return(scope6_get_default(ifr->ifr_ifru.ifru_scope_id));
435 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
439 * Find address for this interface, if it exists.
441 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
442 struct sockaddr_in6 *sa6 =
443 (struct sockaddr_in6 *)&ifra->ifra_addr;
445 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
446 if (sa6->sin6_addr.s6_addr16[1] == 0) {
447 /* link ID is not embedded by the user */
448 sa6->sin6_addr.s6_addr16[1] =
449 htons(ifp->if_index);
450 } else if (sa6->sin6_addr.s6_addr16[1] !=
451 htons(ifp->if_index)) {
452 return(EINVAL); /* link ID contradicts */
454 if (sa6->sin6_scope_id) {
455 if (sa6->sin6_scope_id !=
456 (u_int32_t)ifp->if_index)
458 sa6->sin6_scope_id = 0; /* XXX: good way? */
461 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
465 case SIOCSIFADDR_IN6:
466 case SIOCSIFDSTADDR_IN6:
467 case SIOCSIFNETMASK_IN6:
469 * Since IPv6 allows a node to assign multiple addresses
470 * on a single interface, SIOCSIFxxx ioctls are not suitable
471 * and should be unused.
473 /* we decided to obsolete this command (20000704) */
476 case SIOCDIFADDR_IN6:
478 * for IPv4, we look for existing in_ifaddr here to allow
479 * "ifconfig if0 delete" to remove first IPv4 address on the
480 * interface. For IPv6, as the spec allow multiple interface
481 * address from the day one, we consider "remove the first one"
482 * semantics to be not preferable.
485 return(EADDRNOTAVAIL);
487 case SIOCAIFADDR_IN6:
489 * We always require users to specify a valid IPv6 address for
490 * the corresponding operation.
492 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
493 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
494 return(EAFNOSUPPORT);
500 case SIOCGIFADDR_IN6:
501 /* This interface is basically deprecated. use SIOCGIFCONF. */
503 case SIOCGIFAFLAG_IN6:
504 case SIOCGIFNETMASK_IN6:
505 case SIOCGIFDSTADDR_IN6:
506 case SIOCGIFALIFETIME_IN6:
507 /* must think again about its semantics */
509 return(EADDRNOTAVAIL);
511 case SIOCSIFALIFETIME_IN6:
513 struct in6_addrlifetime *lt;
518 return(EADDRNOTAVAIL);
519 /* sanity for overflow - beware unsigned */
520 lt = &ifr->ifr_ifru.ifru_lifetime;
521 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
522 && lt->ia6t_vltime + time_second < time_second) {
525 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
526 && lt->ia6t_pltime + time_second < time_second) {
535 case SIOCGIFADDR_IN6:
536 ifr->ifr_addr = ia->ia_addr;
539 case SIOCGIFDSTADDR_IN6:
540 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
543 * XXX: should we check if ifa_dstaddr is NULL and return
546 ifr->ifr_dstaddr = ia->ia_dstaddr;
549 case SIOCGIFNETMASK_IN6:
550 ifr->ifr_addr = ia->ia_prefixmask;
553 case SIOCGIFAFLAG_IN6:
554 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
557 case SIOCGIFSTAT_IN6:
560 if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax
561 || in6_ifstat[ifp->if_index] == NULL) {
562 /* return EAFNOSUPPORT? */
563 bzero(&ifr->ifr_ifru.ifru_stat,
564 sizeof(ifr->ifr_ifru.ifru_stat));
566 ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index];
569 case SIOCGIFSTAT_ICMP6:
572 if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax ||
573 icmp6_ifstat[ifp->if_index] == NULL) {
574 /* return EAFNOSUPPORT? */
575 bzero(&ifr->ifr_ifru.ifru_stat,
576 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
578 ifr->ifr_ifru.ifru_icmp6stat =
579 *icmp6_ifstat[ifp->if_index];
582 case SIOCGIFALIFETIME_IN6:
583 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
586 case SIOCSIFALIFETIME_IN6:
587 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
589 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
590 ia->ia6_lifetime.ia6t_expire =
591 time_second + ia->ia6_lifetime.ia6t_vltime;
593 ia->ia6_lifetime.ia6t_expire = 0;
594 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
595 ia->ia6_lifetime.ia6t_preferred =
596 time_second + ia->ia6_lifetime.ia6t_pltime;
598 ia->ia6_lifetime.ia6t_preferred = 0;
601 case SIOCAIFADDR_IN6:
604 struct nd_prefix pr0, *pr;
607 * first, make or update the interface address structure,
608 * and link it to the list.
610 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
614 * then, make the prefix on-link on the interface.
615 * XXX: we'd rather create the prefix before the address, but
616 * we need at least one address to install the corresponding
617 * interface route, so we configure the address first.
621 * convert mask to prefix length (prefixmask has already
622 * been validated in in6_update_ifa().
624 bzero(&pr0, sizeof(pr0));
626 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
628 if (pr0.ndpr_plen == 128)
629 break; /* we don't need to install a host route. */
630 pr0.ndpr_prefix = ifra->ifra_addr;
631 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
632 /* apply the mask for safety. */
633 for (i = 0; i < 4; i++) {
634 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
635 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
638 * XXX: since we don't have an API to set prefix (not address)
639 * lifetimes, we just use the same lifetimes as addresses.
640 * The (temporarily) installed lifetimes can be overridden by
641 * later advertised RAs (when accept_rtadv is non 0), which is
642 * an intended behavior.
644 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
646 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
647 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
648 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
650 /* add the prefix if there's one. */
651 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
653 * nd6_prelist_add will install the corresponding
656 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
659 log(LOG_ERR, "nd6_prelist_add succedded but "
661 return(EINVAL); /* XXX panic here? */
664 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
666 /* XXX: this should not happen! */
667 log(LOG_ERR, "in6_control: addition succeeded, but"
670 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
671 ia->ia6_ndpr == NULL) { /* new autoconfed addr */
676 * If this is the first autoconf address from
677 * the prefix, create a temporary address
678 * as well (when specified).
680 if (ip6_use_tempaddr &&
681 pr->ndpr_refcnt == 1) {
683 if ((e = in6_tmpifadd(ia, 1)) != 0) {
684 log(LOG_NOTICE, "in6_control: "
685 "failed to create a "
686 "temporary address, "
694 * this might affect the status of autoconfigured
695 * addresses, that is, this address might make
696 * other addresses detached.
698 pfxlist_onlink_check();
700 if (error == 0 && ia)
701 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
705 case SIOCDIFADDR_IN6:
708 struct nd_prefix pr0, *pr;
711 * If the address being deleted is the only one that owns
712 * the corresponding prefix, expire the prefix as well.
713 * XXX: theoretically, we don't have to warry about such
714 * relationship, since we separate the address management
715 * and the prefix management. We do this, however, to provide
716 * as much backward compatibility as possible in terms of
717 * the ioctl operation.
719 bzero(&pr0, sizeof(pr0));
721 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
723 if (pr0.ndpr_plen == 128)
725 pr0.ndpr_prefix = ia->ia_addr;
726 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
727 for (i = 0; i < 4; i++) {
728 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
729 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
732 * The logic of the following condition is a bit complicated.
733 * We expire the prefix when
734 * 1. the address obeys autoconfiguration and it is the
735 * only owner of the associated prefix, or
736 * 2. the address does not obey autoconf and there is no
737 * other owner of the prefix.
739 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
740 (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
741 pr->ndpr_refcnt == 1) ||
742 ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
743 pr->ndpr_refcnt == 0))) {
744 pr->ndpr_expire = 1; /* XXX: just for expiration */
748 in6_purgeaddr(&ia->ia_ifa);
749 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
754 if (ifp == NULL || ifp->if_ioctl == 0)
756 return((*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred));
763 * Update parameters of an IPv6 interface address.
764 * If necessary, a new entry is created and linked into address chains.
765 * This function is separated from in6_control().
766 * XXX: should this be performed under splnet()?
769 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
770 struct in6_ifaddr *ia)
772 int error = 0, hostIsNew = 0, plen = -1;
773 struct in6_ifaddr *oia;
774 struct sockaddr_in6 dst6;
775 struct in6_addrlifetime *lt;
777 /* Validate parameters */
778 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
782 * The destination address for a p2p link must have a family
783 * of AF_UNSPEC or AF_INET6.
785 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
786 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
787 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
788 return(EAFNOSUPPORT);
790 * validate ifra_prefixmask. don't check sin6_family, netmask
791 * does not carry fields other than sin6_len.
793 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
796 * Because the IPv6 address architecture is classless, we require
797 * users to specify a (non 0) prefix length (mask) for a new address.
798 * We also require the prefix (when specified) mask is valid, and thus
799 * reject a non-consecutive mask.
801 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
803 if (ifra->ifra_prefixmask.sin6_len != 0) {
804 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
805 (u_char *)&ifra->ifra_prefixmask +
806 ifra->ifra_prefixmask.sin6_len);
812 * In this case, ia must not be NULL. We just use its prefix
815 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
818 * If the destination address on a p2p interface is specified,
819 * and the address is a scoped one, validate/set the scope
822 dst6 = ifra->ifra_dstaddr;
823 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
824 (dst6.sin6_family == AF_INET6)) {
827 #ifndef SCOPEDROUTING
828 if ((error = in6_recoverscope(&dst6,
829 &ifra->ifra_dstaddr.sin6_addr,
833 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
834 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
835 dst6.sin6_scope_id = scopeid;
836 else if (dst6.sin6_scope_id != scopeid)
837 return(EINVAL); /* scope ID mismatch. */
838 #ifndef SCOPEDROUTING
839 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
842 dst6.sin6_scope_id = 0; /* XXX */
846 * The destination address can be specified only for a p2p or a
847 * loopback interface. If specified, the corresponding prefix length
850 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
851 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
852 /* XXX: noisy message */
853 log(LOG_INFO, "in6_update_ifa: a destination can be "
854 "specified for a p2p or a loopback IF only\n");
859 * The following message seems noisy, but we dare to
860 * add it for diagnosis.
862 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
863 "when dstaddr is specified\n");
867 /* lifetime consistency check */
868 lt = &ifra->ifra_lifetime;
869 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
870 && lt->ia6t_vltime + time_second < time_second) {
873 if (lt->ia6t_vltime == 0) {
875 * the following log might be noisy, but this is a typical
876 * configuration mistake or a tool's bug.
879 "in6_update_ifa: valid lifetime is 0 for %s\n",
880 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
882 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
883 && lt->ia6t_pltime + time_second < time_second) {
888 * If this is a new address, allocate a new ifaddr and link it
894 * When in6_update_ifa() is called in a process of a received
895 * RA, it is called under splnet(). So, we should call malloc
898 ia = (struct in6_ifaddr *)
899 malloc(sizeof(*ia), M_IFADDR, M_NOWAIT);
902 bzero((caddr_t)ia, sizeof(*ia));
903 /* Initialize the address and masks */
904 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
905 ia->ia_addr.sin6_family = AF_INET6;
906 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
907 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
909 * XXX: some functions expect that ifa_dstaddr is not
910 * NULL for p2p interfaces.
912 ia->ia_ifa.ifa_dstaddr
913 = (struct sockaddr *)&ia->ia_dstaddr;
915 ia->ia_ifa.ifa_dstaddr = NULL;
917 ia->ia_ifa.ifa_netmask
918 = (struct sockaddr *)&ia->ia_prefixmask;
921 if ((oia = in6_ifaddr) != NULL) {
922 for ( ; oia->ia_next; oia = oia->ia_next)
928 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
932 /* set prefix mask */
933 if (ifra->ifra_prefixmask.sin6_len) {
935 * We prohibit changing the prefix length of an existing
937 * + such an operation should be rare in IPv6, and
938 * + the operation would confuse prefix management.
940 if (ia->ia_prefixmask.sin6_len &&
941 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
942 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
943 " existing (%s) address should not be changed\n",
944 ip6_sprintf(&ia->ia_addr.sin6_addr));
948 ia->ia_prefixmask = ifra->ifra_prefixmask;
952 * If a new destination address is specified, scrub the old one and
953 * install the new destination. Note that the interface must be
954 * p2p or loopback (see the check above.)
956 if (dst6.sin6_family == AF_INET6 &&
957 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
958 &ia->ia_dstaddr.sin6_addr)) {
961 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
962 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
964 log(LOG_ERR, "in6_update_ifa: failed to remove "
965 "a route to the old destination: %s\n",
966 ip6_sprintf(&ia->ia_addr.sin6_addr));
967 /* proceed anyway... */
970 ia->ia_flags &= ~IFA_ROUTE;
971 ia->ia_dstaddr = dst6;
974 /* reset the interface and routing table appropriately. */
975 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
979 * Beyond this point, we should call in6_purgeaddr upon an error,
980 * not just go to unlink.
983 #if 0 /* disable this mechanism for now */
984 /* update prefix list */
986 (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
989 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
990 if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
991 in6_purgeaddr((struct ifaddr *)ia);
997 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
998 struct sockaddr_in6 mltaddr, mltmask;
999 struct in6_multi *in6m;
1003 * join solicited multicast addr for new host id
1005 struct in6_addr llsol;
1006 bzero(&llsol, sizeof(struct in6_addr));
1007 llsol.s6_addr16[0] = htons(0xff02);
1008 llsol.s6_addr16[1] = htons(ifp->if_index);
1009 llsol.s6_addr32[1] = 0;
1010 llsol.s6_addr32[2] = htonl(1);
1011 llsol.s6_addr32[3] =
1012 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1013 llsol.s6_addr8[12] = 0xff;
1014 (void)in6_addmulti(&llsol, ifp, &error);
1017 "in6_update_ifa: addmulti failed for "
1018 "%s on %s (errno=%d)\n",
1019 ip6_sprintf(&llsol), if_name(ifp),
1021 in6_purgeaddr((struct ifaddr *)ia);
1026 bzero(&mltmask, sizeof(mltmask));
1027 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1028 mltmask.sin6_family = AF_INET6;
1029 mltmask.sin6_addr = in6mask32;
1032 * join link-local all-nodes address
1034 bzero(&mltaddr, sizeof(mltaddr));
1035 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1036 mltaddr.sin6_family = AF_INET6;
1037 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1038 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1040 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1043 (struct sockaddr *)&mltaddr,
1044 (struct sockaddr *)&ia->ia_addr,
1045 (struct sockaddr *)&mltmask,
1046 RTF_UP|RTF_CLONING, /* xxx */
1047 (struct rtentry **)0);
1048 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1051 "in6_update_ifa: addmulti failed for "
1052 "%s on %s (errno=%d)\n",
1053 ip6_sprintf(&mltaddr.sin6_addr),
1054 if_name(ifp), error);
1059 * join node information group address
1061 #define hostnamelen strlen(hostname)
1062 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1064 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1065 if (in6m == NULL && ia != NULL) {
1066 (void)in6_addmulti(&mltaddr.sin6_addr,
1069 log(LOG_WARNING, "in6_update_ifa: "
1070 "addmulti failed for "
1071 "%s on %s (errno=%d)\n",
1072 ip6_sprintf(&mltaddr.sin6_addr),
1073 if_name(ifp), error);
1080 * join node-local all-nodes address, on loopback.
1081 * XXX: since "node-local" is obsoleted by interface-local,
1082 * we have to join the group on every interface with
1083 * some interface-boundary restriction.
1085 if (ifp->if_flags & IFF_LOOPBACK) {
1086 struct in6_ifaddr *ia_loop;
1088 struct in6_addr loop6 = in6addr_loopback;
1089 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1091 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1093 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1094 if (in6m == NULL && ia_loop != NULL) {
1096 (struct sockaddr *)&mltaddr,
1097 (struct sockaddr *)&ia_loop->ia_addr,
1098 (struct sockaddr *)&mltmask,
1100 (struct rtentry **)0);
1101 (void)in6_addmulti(&mltaddr.sin6_addr, ifp,
1104 log(LOG_WARNING, "in6_update_ifa: "
1105 "addmulti failed for %s on %s "
1107 ip6_sprintf(&mltaddr.sin6_addr),
1108 if_name(ifp), error);
1114 ia->ia6_flags = ifra->ifra_flags;
1115 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1116 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1118 ia->ia6_lifetime = ifra->ifra_lifetime;
1120 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1121 ia->ia6_lifetime.ia6t_expire =
1122 time_second + ia->ia6_lifetime.ia6t_vltime;
1124 ia->ia6_lifetime.ia6t_expire = 0;
1125 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1126 ia->ia6_lifetime.ia6t_preferred =
1127 time_second + ia->ia6_lifetime.ia6t_pltime;
1129 ia->ia6_lifetime.ia6t_preferred = 0;
1132 * make sure to initialize ND6 information. this is to workaround
1133 * issues with interfaces with IPv6 addresses, which have never brought
1134 * up. We are assuming that it is safe to nd6_ifattach multiple times.
1139 * Perform DAD, if needed.
1140 * XXX It may be of use, if we can administratively
1143 if (in6if_do_dad(ifp) && (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
1144 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1145 nd6_dad_start((struct ifaddr *)ia, NULL);
1152 * XXX: if a change of an existing address failed, keep the entry
1156 in6_unlink_ifa(ia, ifp);
1161 in6_purgeaddr(struct ifaddr *ifa)
1163 struct ifnet *ifp = ifa->ifa_ifp;
1164 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1166 /* stop DAD processing */
1170 * delete route to the destination of the address being purged.
1171 * The interface must be p2p or loopback in this case.
1173 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1176 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1178 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1179 "a route to the p2p destination: %s on %s, "
1181 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1183 /* proceed anyway... */
1186 ia->ia_flags &= ~IFA_ROUTE;
1189 /* Remove ownaddr's loopback rtentry, if it exists. */
1190 in6_ifremloop(&(ia->ia_ifa));
1192 if (ifp->if_flags & IFF_MULTICAST) {
1194 * delete solicited multicast addr for deleting host id
1196 struct in6_multi *in6m;
1197 struct in6_addr llsol;
1198 bzero(&llsol, sizeof(struct in6_addr));
1199 llsol.s6_addr16[0] = htons(0xff02);
1200 llsol.s6_addr16[1] = htons(ifp->if_index);
1201 llsol.s6_addr32[1] = 0;
1202 llsol.s6_addr32[2] = htonl(1);
1203 llsol.s6_addr32[3] =
1204 ia->ia_addr.sin6_addr.s6_addr32[3];
1205 llsol.s6_addr8[12] = 0xff;
1207 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1212 in6_unlink_ifa(ia, ifp);
1216 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1219 struct in6_ifaddr *oia;
1222 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1225 if (oia == (ia = in6_ifaddr))
1226 in6_ifaddr = ia->ia_next;
1228 while (ia->ia_next && (ia->ia_next != oia))
1231 ia->ia_next = oia->ia_next;
1234 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1238 if (oia->ia6_ifpr) { /* check for safety */
1239 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1240 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1241 in6_prefix_remove_ifid(iilen, oia);
1245 * When an autoconfigured address is being removed, release the
1246 * reference to the base prefix. Also, since the release might
1247 * affect the status of other (detached) addresses, call
1248 * pfxlist_onlink_check().
1250 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1251 if (oia->ia6_ndpr == NULL) {
1252 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1253 "%p has no prefix\n", oia);
1255 oia->ia6_ndpr->ndpr_refcnt--;
1256 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1257 oia->ia6_ndpr = NULL;
1260 pfxlist_onlink_check();
1264 * release another refcnt for the link from in6_ifaddr.
1265 * Note that we should decrement the refcnt at least once for all *BSD.
1267 IFAFREE(&oia->ia_ifa);
1273 in6_purgeif(struct ifnet *ifp)
1275 struct ifaddr *ifa, *nifa;
1277 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa)
1279 nifa = TAILQ_NEXT(ifa, ifa_list);
1280 if (ifa->ifa_addr->sa_family != AF_INET6)
1290 * SIOCGLIFADDR: get first address. (?)
1291 * SIOCGLIFADDR with IFLR_PREFIX:
1292 * get first address that matches the specified prefix.
1293 * SIOCALIFADDR: add the specified address.
1294 * SIOCALIFADDR with IFLR_PREFIX:
1295 * add the specified prefix, filling hostid part from
1296 * the first link-local address. prefixlen must be <= 64.
1297 * SIOCDLIFADDR: delete the specified address.
1298 * SIOCDLIFADDR with IFLR_PREFIX:
1299 * delete the first address that matches the specified prefix.
1301 * EINVAL on invalid parameters
1302 * EADDRNOTAVAIL on prefix match failed/specified address not found
1303 * other values may be returned from in6_ioctl()
1305 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1306 * this is to accomodate address naming scheme other than RFC2374,
1308 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1309 * address encoding scheme. (see figure on page 8)
1312 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1313 struct ifnet *ifp, struct thread *td)
1315 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1317 struct sockaddr *sa;
1320 if (!data || !ifp) {
1321 panic("invalid argument to in6_lifaddr_ioctl");
1327 /* address must be specified on GET with IFLR_PREFIX */
1328 if ((iflr->flags & IFLR_PREFIX) == 0)
1333 /* address must be specified on ADD and DELETE */
1334 sa = (struct sockaddr *)&iflr->addr;
1335 if (sa->sa_family != AF_INET6)
1337 if (sa->sa_len != sizeof(struct sockaddr_in6))
1339 /* XXX need improvement */
1340 sa = (struct sockaddr *)&iflr->dstaddr;
1341 if (sa->sa_family && sa->sa_family != AF_INET6)
1343 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1346 default: /* shouldn't happen */
1348 panic("invalid cmd to in6_lifaddr_ioctl");
1354 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1360 struct in6_aliasreq ifra;
1361 struct in6_addr *hostid = NULL;
1364 if ((iflr->flags & IFLR_PREFIX) != 0) {
1365 struct sockaddr_in6 *sin6;
1368 * hostid is to fill in the hostid part of the
1369 * address. hostid points to the first link-local
1370 * address attached to the interface.
1372 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1374 return EADDRNOTAVAIL;
1375 hostid = IFA_IN6(ifa);
1377 /* prefixlen must be <= 64. */
1378 if (64 < iflr->prefixlen)
1380 prefixlen = iflr->prefixlen;
1382 /* hostid part must be zero. */
1383 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1384 if (sin6->sin6_addr.s6_addr32[2] != 0
1385 || sin6->sin6_addr.s6_addr32[3] != 0) {
1389 prefixlen = iflr->prefixlen;
1391 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1392 bzero(&ifra, sizeof(ifra));
1393 bcopy(iflr->iflr_name, ifra.ifra_name,
1394 sizeof(ifra.ifra_name));
1396 bcopy(&iflr->addr, &ifra.ifra_addr,
1397 ((struct sockaddr *)&iflr->addr)->sa_len);
1399 /* fill in hostid part */
1400 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1401 hostid->s6_addr32[2];
1402 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1403 hostid->s6_addr32[3];
1406 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1407 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1408 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1410 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1411 hostid->s6_addr32[2];
1412 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1413 hostid->s6_addr32[3];
1417 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1418 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1420 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1421 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1426 struct in6_ifaddr *ia;
1427 struct in6_addr mask, candidate, match;
1428 struct sockaddr_in6 *sin6;
1431 bzero(&mask, sizeof(mask));
1432 if (iflr->flags & IFLR_PREFIX) {
1433 /* lookup a prefix rather than address. */
1434 in6_len2mask(&mask, iflr->prefixlen);
1436 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1437 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1438 match.s6_addr32[0] &= mask.s6_addr32[0];
1439 match.s6_addr32[1] &= mask.s6_addr32[1];
1440 match.s6_addr32[2] &= mask.s6_addr32[2];
1441 match.s6_addr32[3] &= mask.s6_addr32[3];
1443 /* if you set extra bits, that's wrong */
1444 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1449 if (cmd == SIOCGLIFADDR) {
1450 /* on getting an address, take the 1st match */
1453 /* on deleting an address, do exact match */
1454 in6_len2mask(&mask, 128);
1455 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1456 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1462 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1464 if (ifa->ifa_addr->sa_family != AF_INET6)
1469 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1470 #ifndef SCOPEDROUTING
1472 * XXX: this is adhoc, but is necessary to allow
1473 * a user to specify fe80::/64 (not /10) for a
1474 * link-local address.
1476 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1477 candidate.s6_addr16[1] = 0;
1479 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1480 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1481 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1482 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1483 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1487 return EADDRNOTAVAIL;
1490 if (cmd == SIOCGLIFADDR) {
1491 #ifndef SCOPEDROUTING
1492 struct sockaddr_in6 *s6;
1495 /* fill in the if_laddrreq structure */
1496 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1497 #ifndef SCOPEDROUTING /* XXX see above */
1498 s6 = (struct sockaddr_in6 *)&iflr->addr;
1499 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1500 s6->sin6_addr.s6_addr16[1] = 0;
1502 in6_addr2scopeid(ifp, &s6->sin6_addr);
1505 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1506 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1507 ia->ia_dstaddr.sin6_len);
1508 #ifndef SCOPEDROUTING /* XXX see above */
1509 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1510 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1511 s6->sin6_addr.s6_addr16[1] = 0;
1513 in6_addr2scopeid(ifp,
1518 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1521 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1524 iflr->flags = ia->ia6_flags; /* XXX */
1528 struct in6_aliasreq ifra;
1530 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1531 bzero(&ifra, sizeof(ifra));
1532 bcopy(iflr->iflr_name, ifra.ifra_name,
1533 sizeof(ifra.ifra_name));
1535 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1536 ia->ia_addr.sin6_len);
1537 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1538 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1539 ia->ia_dstaddr.sin6_len);
1541 bzero(&ifra.ifra_dstaddr,
1542 sizeof(ifra.ifra_dstaddr));
1544 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1545 ia->ia_prefixmask.sin6_len);
1547 ifra.ifra_flags = ia->ia6_flags;
1548 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1554 return EOPNOTSUPP; /* just for safety */
1558 * Initialize an interface's intetnet6 address
1559 * and routing table entry.
1562 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6,
1565 int error = 0, plen, ifacount = 0;
1570 * Give the interface a chance to initialize
1571 * if this is its first address,
1572 * and to validate the address if necessary.
1574 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1576 if (ifa->ifa_addr == NULL)
1577 continue; /* just for safety */
1578 if (ifa->ifa_addr->sa_family != AF_INET6)
1583 ia->ia_addr = *sin6;
1585 if (ifacount <= 1 && ifp->if_ioctl &&
1586 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia,
1587 (struct ucred *)NULL))) {
1593 ia->ia_ifa.ifa_metric = ifp->if_metric;
1595 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1599 * If the destination address is specified for a point-to-point
1600 * interface, install a route to the destination as an interface
1603 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1604 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1605 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1606 RTF_UP | RTF_HOST)) != 0)
1608 ia->ia_flags |= IFA_ROUTE;
1612 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1614 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1617 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1619 /* set the rtrequest function to create llinfo */
1620 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1621 in6_ifaddloop(&(ia->ia_ifa));
1628 * Add an address to the list of IP6 multicast addresses for a
1632 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
1634 struct in6_multi *in6m;
1635 struct sockaddr_in6 sin6;
1636 struct ifmultiaddr *ifma;
1642 * Call generic routine to add membership or increment
1643 * refcount. It wants addresses in the form of a sockaddr,
1644 * so we build one here (being careful to zero the unused bytes).
1646 bzero(&sin6, sizeof sin6);
1647 sin6.sin6_family = AF_INET6;
1648 sin6.sin6_len = sizeof sin6;
1649 sin6.sin6_addr = *maddr6;
1650 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1657 * If ifma->ifma_protospec is null, then if_addmulti() created
1658 * a new record. Otherwise, we are done.
1660 if (ifma->ifma_protospec != 0)
1661 return ifma->ifma_protospec;
1663 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1664 at interrupt time? If so, need to fix if_addmulti. XXX */
1665 in6m = (struct in6_multi *)malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1671 bzero(in6m, sizeof *in6m);
1672 in6m->in6m_addr = *maddr6;
1673 in6m->in6m_ifp = ifp;
1674 in6m->in6m_ifma = ifma;
1675 ifma->ifma_protospec = in6m;
1676 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1679 * Let MLD6 know that we have joined a new IP6 multicast
1682 mld6_start_listening(in6m);
1688 * Delete a multicast address record.
1691 in6_delmulti(struct in6_multi *in6m)
1693 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1696 if (ifma->ifma_refcount == 1) {
1698 * No remaining claims to this record; let MLD6 know
1699 * that we are leaving the multicast group.
1701 mld6_stop_listening(in6m);
1702 ifma->ifma_protospec = 0;
1703 LIST_REMOVE(in6m, in6m_entry);
1704 free(in6m, M_IPMADDR);
1706 /* XXX - should be separate API for when we have an ifma? */
1707 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1712 * Find an IPv6 interface link-local address specific to an interface.
1715 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1719 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1721 if (ifa->ifa_addr == NULL)
1722 continue; /* just for safety */
1723 if (ifa->ifa_addr->sa_family != AF_INET6)
1725 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1726 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1733 return((struct in6_ifaddr *)ifa);
1738 * find the internet address corresponding to a given interface and address.
1741 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1745 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1747 if (ifa->ifa_addr == NULL)
1748 continue; /* just for safety */
1749 if (ifa->ifa_addr->sa_family != AF_INET6)
1751 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1755 return((struct in6_ifaddr *)ifa);
1759 * Convert IP6 address to printable (loggable) representation.
1761 static char digits[] = "0123456789abcdef";
1762 static int ip6round = 0;
1764 ip6_sprintf(const struct in6_addr *addr)
1766 static char ip6buf[8][48];
1769 const u_short *a = (const u_short *)addr;
1773 ip6round = (ip6round + 1) & 7;
1774 cp = ip6buf[ip6round];
1776 for (i = 0; i < 8; i++) {
1787 if (dcolon == 0 && *(a + 1) == 0) {
1799 d = (const u_char *)a;
1800 *cp++ = digits[*d >> 4];
1801 *cp++ = digits[*d++ & 0xf];
1802 *cp++ = digits[*d >> 4];
1803 *cp++ = digits[*d & 0xf];
1808 return(ip6buf[ip6round]);
1812 in6_localaddr(struct in6_addr *in6)
1814 struct in6_ifaddr *ia;
1816 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1819 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1820 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1821 &ia->ia_prefixmask.sin6_addr))
1828 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1830 struct in6_ifaddr *ia;
1832 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1833 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1835 #ifdef SCOPEDROUTING
1836 ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id &&
1838 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1839 return(1); /* true */
1841 /* XXX: do we still have to go thru the rest of the list? */
1844 return(0); /* false */
1848 * return length of part which dst and src are equal
1852 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1855 u_char *s = (u_char *)src, *d = (u_char *)dst;
1856 u_char *lim = s + 16, r;
1859 if ((r = (*d++ ^ *s++)) != 0) {
1870 /* XXX: to be scope conscious */
1872 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1874 int bytelen, bitlen;
1877 if (0 > len || len > 128) {
1878 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1886 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1888 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1889 p2->s6_addr[bytelen] >> (8 - bitlen))
1896 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1898 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1899 int bytelen, bitlen, i;
1902 if (0 > len || len > 128) {
1903 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1908 bzero(maskp, sizeof(*maskp));
1911 for (i = 0; i < bytelen; i++)
1912 maskp->s6_addr[i] = 0xff;
1914 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1918 * return the best address out of the same scope
1921 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst)
1923 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
1927 struct in6_ifaddr *ifa_best = NULL;
1931 printf("in6_ifawithscope: output interface is not specified\n");
1937 * We search for all addresses on all interfaces from the beginning.
1938 * Comparing an interface with the outgoing interface will be done
1939 * only at the final stage of tiebreaking.
1941 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1944 * We can never take an address that breaks the scope zone
1945 * of the destination.
1947 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
1950 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1952 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
1954 if (ifa->ifa_addr->sa_family != AF_INET6)
1957 src_scope = in6_addrscope(IFA_IN6(ifa));
1960 * Don't use an address before completing DAD
1961 * nor a duplicated address.
1963 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1967 /* XXX: is there any case to allow anycasts? */
1968 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1972 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1977 * If this is the first address we find,
1980 if (ifa_best == NULL)
1984 * ifa_best is never NULL beyond this line except
1985 * within the block labeled "replace".
1989 * If ifa_best has a smaller scope than dst and
1990 * the current address has a larger one than
1991 * (or equal to) dst, always replace ifa_best.
1992 * Also, if the current address has a smaller scope
1993 * than dst, ignore it unless ifa_best also has a
1995 * Consequently, after the two if-clause below,
1996 * the followings must be satisfied:
1997 * (scope(src) < scope(dst) &&
1998 * scope(best) < scope(dst))
2000 * (scope(best) >= scope(dst) &&
2001 * scope(src) >= scope(dst))
2003 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2004 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2005 goto replace; /* (A) */
2006 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2007 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2011 * A deprecated address SHOULD NOT be used in new
2012 * communications if an alternate (non-deprecated)
2013 * address is available and has sufficient scope.
2014 * RFC 2462, Section 5.5.4.
2016 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2017 IN6_IFF_DEPRECATED) {
2019 * Ignore any deprecated addresses if
2020 * specified by configuration.
2022 if (!ip6_use_deprecated)
2026 * If we have already found a non-deprecated
2027 * candidate, just ignore deprecated addresses.
2029 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2035 * A non-deprecated address is always preferred
2036 * to a deprecated one regardless of scopes and
2037 * address matching (Note invariants ensured by the
2038 * conditions (A) and (B) above.)
2040 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2041 (((struct in6_ifaddr *)ifa)->ia6_flags &
2042 IN6_IFF_DEPRECATED) == 0)
2046 * When we use temporary addresses described in
2047 * RFC 3041, we prefer temporary addresses to
2048 * public autoconf addresses. Again, note the
2049 * invariants from (A) and (B). Also note that we
2050 * don't have any preference between static addresses
2051 * and autoconf addresses (despite of whether or not
2052 * the latter is temporary or public.)
2054 if (ip6_use_tempaddr) {
2055 struct in6_ifaddr *ifat;
2057 ifat = (struct in6_ifaddr *)ifa;
2058 if ((ifa_best->ia6_flags &
2059 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2060 == IN6_IFF_AUTOCONF &&
2062 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2063 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2066 if ((ifa_best->ia6_flags &
2067 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2068 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2070 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2071 == IN6_IFF_AUTOCONF) {
2077 * At this point, we have two cases:
2078 * 1. we are looking at a non-deprecated address,
2079 * and ifa_best is also non-deprecated.
2080 * 2. we are looking at a deprecated address,
2081 * and ifa_best is also deprecated.
2082 * Also, we do not have to consider a case where
2083 * the scope of if_best is larger(smaller) than dst and
2084 * the scope of the current address is smaller(larger)
2085 * than dst. Such a case has already been covered.
2086 * Tiebreaking is done according to the following
2088 * - the scope comparison between the address and
2090 * - the scope comparison between the address and
2091 * ifa_best (bscopecmp)
2092 * - if the address match dst longer than ifa_best
2094 * - if the address is on the outgoing I/F (outI/F)
2096 * Roughly speaking, the selection policy is
2097 * - the most important item is scope. The same scope
2098 * is best. Then search for a larger scope.
2099 * Smaller scopes are the last resort.
2100 * - A deprecated address is chosen only when we have
2101 * no address that has an enough scope, but is
2102 * prefered to any addresses of smaller scopes
2103 * (this must be already done above.)
2104 * - addresses on the outgoing I/F are preferred to
2105 * ones on other interfaces if none of above
2106 * tiebreaks. In the table below, the column "bI"
2107 * means if the best_ifa is on the outgoing
2108 * interface, and the column "sI" means if the ifa
2109 * is on the outgoing interface.
2110 * - If there is no other reasons to choose one,
2111 * longest address match against dst is considered.
2113 * The precise decision table is as follows:
2114 * dscopecmp bscopecmp match bI oI | replace?
2115 * N/A equal N/A Y N | No (1)
2116 * N/A equal N/A N Y | Yes (2)
2117 * N/A equal larger N/A | Yes (3)
2118 * N/A equal !larger N/A | No (4)
2119 * larger larger N/A N/A | No (5)
2120 * larger smaller N/A N/A | Yes (6)
2121 * smaller larger N/A N/A | Yes (7)
2122 * smaller smaller N/A N/A | No (8)
2123 * equal smaller N/A N/A | Yes (9)
2124 * equal larger (already done at A above)
2126 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2127 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2129 if (bscopecmp == 0) {
2130 struct ifnet *bifp = ifa_best->ia_ifp;
2132 if (bifp == oifp && ifp != oifp) /* (1) */
2134 if (bifp != oifp && ifp == oifp) /* (2) */
2138 * Both bifp and ifp are on the outgoing
2139 * interface, or both two are on a different
2140 * interface from the outgoing I/F.
2141 * now we need address matching against dst
2144 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2145 matchcmp = tlen - blen;
2146 if (matchcmp > 0) /* (3) */
2150 if (dscopecmp > 0) {
2151 if (bscopecmp > 0) /* (5) */
2153 goto replace; /* (6) */
2155 if (dscopecmp < 0) {
2156 if (bscopecmp > 0) /* (7) */
2161 /* now dscopecmp must be 0 */
2163 goto replace; /* (9) */
2166 ifa_best = (struct in6_ifaddr *)ifa;
2167 blen = tlen >= 0 ? tlen :
2168 in6_matchlen(IFA_IN6(ifa), dst);
2169 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2173 /* count statistics for future improvements */
2174 if (ifa_best == NULL)
2175 ip6stat.ip6s_sources_none++;
2177 if (oifp == ifa_best->ia_ifp)
2178 ip6stat.ip6s_sources_sameif[best_scope]++;
2180 ip6stat.ip6s_sources_otherif[best_scope]++;
2182 if (best_scope == dst_scope)
2183 ip6stat.ip6s_sources_samescope[best_scope]++;
2185 ip6stat.ip6s_sources_otherscope[best_scope]++;
2187 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2188 ip6stat.ip6s_sources_deprecated[best_scope]++;
2195 * return the best address out of the same scope. if no address was
2196 * found, return the first valid address from designated IF.
2199 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2201 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2203 struct in6_ifaddr *besta = 0;
2204 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2206 dep[0] = dep[1] = NULL;
2209 * We first look for addresses in the same scope.
2210 * If there is one, return it.
2211 * If two or more, return one which matches the dst longest.
2212 * If none, return one of global addresses assigned other ifs.
2214 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2216 if (ifa->ifa_addr->sa_family != AF_INET6)
2218 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2219 continue; /* XXX: is there any case to allow anycast? */
2220 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2221 continue; /* don't use this interface */
2222 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2224 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2225 if (ip6_use_deprecated)
2226 dep[0] = (struct in6_ifaddr *)ifa;
2230 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2232 * call in6_matchlen() as few as possible
2236 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2237 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2240 besta = (struct in6_ifaddr *)ifa;
2243 besta = (struct in6_ifaddr *)ifa;
2249 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2251 if (ifa->ifa_addr->sa_family != AF_INET6)
2253 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2254 continue; /* XXX: is there any case to allow anycast? */
2255 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2256 continue; /* don't use this interface */
2257 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2259 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2260 if (ip6_use_deprecated)
2261 dep[1] = (struct in6_ifaddr *)ifa;
2265 return (struct in6_ifaddr *)ifa;
2268 /* use the last-resort values, that are, deprecated addresses */
2278 * perform DAD when interface becomes IFF_UP.
2281 in6_if_up(struct ifnet *ifp)
2284 struct in6_ifaddr *ia;
2285 int dad_delay; /* delay ticks before DAD output */
2288 * special cases, like 6to4, are handled in in6_ifattach
2290 in6_ifattach(ifp, NULL);
2293 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2295 if (ifa->ifa_addr->sa_family != AF_INET6)
2297 ia = (struct in6_ifaddr *)ifa;
2298 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2299 nd6_dad_start(ifa, &dad_delay);
2304 in6if_do_dad(struct ifnet *ifp)
2306 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2309 switch (ifp->if_type) {
2315 * These interfaces do not have the IFF_LOOPBACK flag,
2316 * but loop packets back. We do not have to do DAD on such
2317 * interfaces. We should even omit it, because loop-backed
2318 * NS would confuse the DAD procedure.
2323 * Our DAD routine requires the interface up and running.
2324 * However, some interfaces can be up before the RUNNING
2325 * status. Additionaly, users may try to assign addresses
2326 * before the interface becomes up (or running).
2327 * We simply skip DAD in such a case as a work around.
2328 * XXX: we should rather mark "tentative" on such addresses,
2329 * and do DAD after the interface becomes ready.
2331 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2332 (IFF_UP|IFF_RUNNING))
2340 * Calculate max IPv6 MTU through all the interfaces and store it
2346 unsigned long maxmtu = 0;
2349 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2351 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2352 nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
2353 maxmtu = nd_ifinfo[ifp->if_index].linkmtu;
2355 if (maxmtu) /* update only when maxmtu is positive */
2356 in6_maxmtu = maxmtu;
2360 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2361 * v4 mapped addr or v4 compat addr
2364 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2366 bzero(sin, sizeof(*sin));
2367 sin->sin_len = sizeof(struct sockaddr_in);
2368 sin->sin_family = AF_INET;
2369 sin->sin_port = sin6->sin6_port;
2370 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2373 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2375 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2377 bzero(sin6, sizeof(*sin6));
2378 sin6->sin6_len = sizeof(struct sockaddr_in6);
2379 sin6->sin6_family = AF_INET6;
2380 sin6->sin6_port = sin->sin_port;
2381 sin6->sin6_addr.s6_addr32[0] = 0;
2382 sin6->sin6_addr.s6_addr32[1] = 0;
2383 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2384 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2387 /* Convert sockaddr_in6 into sockaddr_in. */
2389 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2391 struct sockaddr_in *sin_p;
2392 struct sockaddr_in6 sin6;
2395 * Save original sockaddr_in6 addr and convert it
2398 sin6 = *(struct sockaddr_in6 *)nam;
2399 sin_p = (struct sockaddr_in *)nam;
2400 in6_sin6_2_sin(sin_p, &sin6);
2403 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2405 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2407 struct sockaddr_in *sin_p;
2408 struct sockaddr_in6 *sin6_p;
2410 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2412 sin_p = (struct sockaddr_in *)*nam;
2413 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2414 FREE(*nam, M_SONAME);
2415 *nam = (struct sockaddr *)sin6_p;