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.8 2004/05/20 18:30:36 cpressey 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();
703 case SIOCDIFADDR_IN6:
706 struct nd_prefix pr0, *pr;
709 * If the address being deleted is the only one that owns
710 * the corresponding prefix, expire the prefix as well.
711 * XXX: theoretically, we don't have to warry about such
712 * relationship, since we separate the address management
713 * and the prefix management. We do this, however, to provide
714 * as much backward compatibility as possible in terms of
715 * the ioctl operation.
717 bzero(&pr0, sizeof(pr0));
719 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
721 if (pr0.ndpr_plen == 128)
723 pr0.ndpr_prefix = ia->ia_addr;
724 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
725 for (i = 0; i < 4; i++) {
726 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
727 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
730 * The logic of the following condition is a bit complicated.
731 * We expire the prefix when
732 * 1. the address obeys autoconfiguration and it is the
733 * only owner of the associated prefix, or
734 * 2. the address does not obey autoconf and there is no
735 * other owner of the prefix.
737 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
738 (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
739 pr->ndpr_refcnt == 1) ||
740 ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
741 pr->ndpr_refcnt == 0))) {
742 pr->ndpr_expire = 1; /* XXX: just for expiration */
746 in6_purgeaddr(&ia->ia_ifa);
751 if (ifp == NULL || ifp->if_ioctl == 0)
753 return((*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred));
760 * Update parameters of an IPv6 interface address.
761 * If necessary, a new entry is created and linked into address chains.
762 * This function is separated from in6_control().
763 * XXX: should this be performed under splnet()?
766 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
767 struct in6_ifaddr *ia)
769 int error = 0, hostIsNew = 0, plen = -1;
770 struct in6_ifaddr *oia;
771 struct sockaddr_in6 dst6;
772 struct in6_addrlifetime *lt;
774 /* Validate parameters */
775 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
779 * The destination address for a p2p link must have a family
780 * of AF_UNSPEC or AF_INET6.
782 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
783 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
784 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
785 return(EAFNOSUPPORT);
787 * validate ifra_prefixmask. don't check sin6_family, netmask
788 * does not carry fields other than sin6_len.
790 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
793 * Because the IPv6 address architecture is classless, we require
794 * users to specify a (non 0) prefix length (mask) for a new address.
795 * We also require the prefix (when specified) mask is valid, and thus
796 * reject a non-consecutive mask.
798 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
800 if (ifra->ifra_prefixmask.sin6_len != 0) {
801 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
802 (u_char *)&ifra->ifra_prefixmask +
803 ifra->ifra_prefixmask.sin6_len);
809 * In this case, ia must not be NULL. We just use its prefix
812 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
815 * If the destination address on a p2p interface is specified,
816 * and the address is a scoped one, validate/set the scope
819 dst6 = ifra->ifra_dstaddr;
820 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
821 (dst6.sin6_family == AF_INET6)) {
824 #ifndef SCOPEDROUTING
825 if ((error = in6_recoverscope(&dst6,
826 &ifra->ifra_dstaddr.sin6_addr,
830 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
831 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
832 dst6.sin6_scope_id = scopeid;
833 else if (dst6.sin6_scope_id != scopeid)
834 return(EINVAL); /* scope ID mismatch. */
835 #ifndef SCOPEDROUTING
836 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
839 dst6.sin6_scope_id = 0; /* XXX */
843 * The destination address can be specified only for a p2p or a
844 * loopback interface. If specified, the corresponding prefix length
847 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
848 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
849 /* XXX: noisy message */
850 log(LOG_INFO, "in6_update_ifa: a destination can be "
851 "specified for a p2p or a loopback IF only\n");
856 * The following message seems noisy, but we dare to
857 * add it for diagnosis.
859 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
860 "when dstaddr is specified\n");
864 /* lifetime consistency check */
865 lt = &ifra->ifra_lifetime;
866 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
867 && lt->ia6t_vltime + time_second < time_second) {
870 if (lt->ia6t_vltime == 0) {
872 * the following log might be noisy, but this is a typical
873 * configuration mistake or a tool's bug.
876 "in6_update_ifa: valid lifetime is 0 for %s\n",
877 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
879 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
880 && lt->ia6t_pltime + time_second < time_second) {
885 * If this is a new address, allocate a new ifaddr and link it
891 * When in6_update_ifa() is called in a process of a received
892 * RA, it is called under splnet(). So, we should call malloc
895 ia = (struct in6_ifaddr *)
896 malloc(sizeof(*ia), M_IFADDR, M_NOWAIT);
899 bzero((caddr_t)ia, sizeof(*ia));
900 /* Initialize the address and masks */
901 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
902 ia->ia_addr.sin6_family = AF_INET6;
903 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
904 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
906 * XXX: some functions expect that ifa_dstaddr is not
907 * NULL for p2p interfaces.
909 ia->ia_ifa.ifa_dstaddr
910 = (struct sockaddr *)&ia->ia_dstaddr;
912 ia->ia_ifa.ifa_dstaddr = NULL;
914 ia->ia_ifa.ifa_netmask
915 = (struct sockaddr *)&ia->ia_prefixmask;
918 if ((oia = in6_ifaddr) != NULL) {
919 for ( ; oia->ia_next; oia = oia->ia_next)
925 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
929 /* set prefix mask */
930 if (ifra->ifra_prefixmask.sin6_len) {
932 * We prohibit changing the prefix length of an existing
934 * + such an operation should be rare in IPv6, and
935 * + the operation would confuse prefix management.
937 if (ia->ia_prefixmask.sin6_len &&
938 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
939 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
940 " existing (%s) address should not be changed\n",
941 ip6_sprintf(&ia->ia_addr.sin6_addr));
945 ia->ia_prefixmask = ifra->ifra_prefixmask;
949 * If a new destination address is specified, scrub the old one and
950 * install the new destination. Note that the interface must be
951 * p2p or loopback (see the check above.)
953 if (dst6.sin6_family == AF_INET6 &&
954 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
955 &ia->ia_dstaddr.sin6_addr)) {
958 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
959 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
961 log(LOG_ERR, "in6_update_ifa: failed to remove "
962 "a route to the old destination: %s\n",
963 ip6_sprintf(&ia->ia_addr.sin6_addr));
964 /* proceed anyway... */
967 ia->ia_flags &= ~IFA_ROUTE;
968 ia->ia_dstaddr = dst6;
971 /* reset the interface and routing table appropriately. */
972 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
976 * Beyond this point, we should call in6_purgeaddr upon an error,
977 * not just go to unlink.
980 #if 0 /* disable this mechanism for now */
981 /* update prefix list */
983 (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
986 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
987 if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
988 in6_purgeaddr((struct ifaddr *)ia);
994 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
995 struct sockaddr_in6 mltaddr, mltmask;
996 struct in6_multi *in6m;
1000 * join solicited multicast addr for new host id
1002 struct in6_addr llsol;
1003 bzero(&llsol, sizeof(struct in6_addr));
1004 llsol.s6_addr16[0] = htons(0xff02);
1005 llsol.s6_addr16[1] = htons(ifp->if_index);
1006 llsol.s6_addr32[1] = 0;
1007 llsol.s6_addr32[2] = htonl(1);
1008 llsol.s6_addr32[3] =
1009 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1010 llsol.s6_addr8[12] = 0xff;
1011 (void)in6_addmulti(&llsol, ifp, &error);
1014 "in6_update_ifa: addmulti failed for "
1015 "%s on %s (errno=%d)\n",
1016 ip6_sprintf(&llsol), if_name(ifp),
1018 in6_purgeaddr((struct ifaddr *)ia);
1023 bzero(&mltmask, sizeof(mltmask));
1024 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1025 mltmask.sin6_family = AF_INET6;
1026 mltmask.sin6_addr = in6mask32;
1029 * join link-local all-nodes address
1031 bzero(&mltaddr, sizeof(mltaddr));
1032 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1033 mltaddr.sin6_family = AF_INET6;
1034 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1035 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1037 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1040 (struct sockaddr *)&mltaddr,
1041 (struct sockaddr *)&ia->ia_addr,
1042 (struct sockaddr *)&mltmask,
1043 RTF_UP|RTF_CLONING, /* xxx */
1044 (struct rtentry **)0);
1045 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1048 "in6_update_ifa: addmulti failed for "
1049 "%s on %s (errno=%d)\n",
1050 ip6_sprintf(&mltaddr.sin6_addr),
1051 if_name(ifp), error);
1056 * join node information group address
1058 #define hostnamelen strlen(hostname)
1059 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1061 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1062 if (in6m == NULL && ia != NULL) {
1063 (void)in6_addmulti(&mltaddr.sin6_addr,
1066 log(LOG_WARNING, "in6_update_ifa: "
1067 "addmulti failed for "
1068 "%s on %s (errno=%d)\n",
1069 ip6_sprintf(&mltaddr.sin6_addr),
1070 if_name(ifp), error);
1077 * join node-local all-nodes address, on loopback.
1078 * XXX: since "node-local" is obsoleted by interface-local,
1079 * we have to join the group on every interface with
1080 * some interface-boundary restriction.
1082 if (ifp->if_flags & IFF_LOOPBACK) {
1083 struct in6_ifaddr *ia_loop;
1085 struct in6_addr loop6 = in6addr_loopback;
1086 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1088 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1090 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1091 if (in6m == NULL && ia_loop != NULL) {
1093 (struct sockaddr *)&mltaddr,
1094 (struct sockaddr *)&ia_loop->ia_addr,
1095 (struct sockaddr *)&mltmask,
1097 (struct rtentry **)0);
1098 (void)in6_addmulti(&mltaddr.sin6_addr, ifp,
1101 log(LOG_WARNING, "in6_update_ifa: "
1102 "addmulti failed for %s on %s "
1104 ip6_sprintf(&mltaddr.sin6_addr),
1105 if_name(ifp), error);
1111 ia->ia6_flags = ifra->ifra_flags;
1112 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1113 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1115 ia->ia6_lifetime = ifra->ifra_lifetime;
1117 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1118 ia->ia6_lifetime.ia6t_expire =
1119 time_second + ia->ia6_lifetime.ia6t_vltime;
1121 ia->ia6_lifetime.ia6t_expire = 0;
1122 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1123 ia->ia6_lifetime.ia6t_preferred =
1124 time_second + ia->ia6_lifetime.ia6t_pltime;
1126 ia->ia6_lifetime.ia6t_preferred = 0;
1129 * make sure to initialize ND6 information. this is to workaround
1130 * issues with interfaces with IPv6 addresses, which have never brought
1131 * up. We are assuming that it is safe to nd6_ifattach multiple times.
1136 * Perform DAD, if needed.
1137 * XXX It may be of use, if we can administratively
1140 if (in6if_do_dad(ifp) && (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
1141 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1142 nd6_dad_start((struct ifaddr *)ia, NULL);
1149 * XXX: if a change of an existing address failed, keep the entry
1153 in6_unlink_ifa(ia, ifp);
1158 in6_purgeaddr(struct ifaddr *ifa)
1160 struct ifnet *ifp = ifa->ifa_ifp;
1161 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1163 /* stop DAD processing */
1167 * delete route to the destination of the address being purged.
1168 * The interface must be p2p or loopback in this case.
1170 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1173 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1175 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1176 "a route to the p2p destination: %s on %s, "
1178 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1180 /* proceed anyway... */
1183 ia->ia_flags &= ~IFA_ROUTE;
1186 /* Remove ownaddr's loopback rtentry, if it exists. */
1187 in6_ifremloop(&(ia->ia_ifa));
1189 if (ifp->if_flags & IFF_MULTICAST) {
1191 * delete solicited multicast addr for deleting host id
1193 struct in6_multi *in6m;
1194 struct in6_addr llsol;
1195 bzero(&llsol, sizeof(struct in6_addr));
1196 llsol.s6_addr16[0] = htons(0xff02);
1197 llsol.s6_addr16[1] = htons(ifp->if_index);
1198 llsol.s6_addr32[1] = 0;
1199 llsol.s6_addr32[2] = htonl(1);
1200 llsol.s6_addr32[3] =
1201 ia->ia_addr.sin6_addr.s6_addr32[3];
1202 llsol.s6_addr8[12] = 0xff;
1204 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1209 in6_unlink_ifa(ia, ifp);
1213 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1216 struct in6_ifaddr *oia;
1219 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1222 if (oia == (ia = in6_ifaddr))
1223 in6_ifaddr = ia->ia_next;
1225 while (ia->ia_next && (ia->ia_next != oia))
1228 ia->ia_next = oia->ia_next;
1231 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1235 if (oia->ia6_ifpr) { /* check for safety */
1236 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1237 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1238 in6_prefix_remove_ifid(iilen, oia);
1242 * When an autoconfigured address is being removed, release the
1243 * reference to the base prefix. Also, since the release might
1244 * affect the status of other (detached) addresses, call
1245 * pfxlist_onlink_check().
1247 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1248 if (oia->ia6_ndpr == NULL) {
1249 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1250 "%p has no prefix\n", oia);
1252 oia->ia6_ndpr->ndpr_refcnt--;
1253 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1254 oia->ia6_ndpr = NULL;
1257 pfxlist_onlink_check();
1261 * release another refcnt for the link from in6_ifaddr.
1262 * Note that we should decrement the refcnt at least once for all *BSD.
1264 IFAFREE(&oia->ia_ifa);
1270 in6_purgeif(struct ifnet *ifp)
1272 struct ifaddr *ifa, *nifa;
1274 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa)
1276 nifa = TAILQ_NEXT(ifa, ifa_list);
1277 if (ifa->ifa_addr->sa_family != AF_INET6)
1287 * SIOCGLIFADDR: get first address. (?)
1288 * SIOCGLIFADDR with IFLR_PREFIX:
1289 * get first address that matches the specified prefix.
1290 * SIOCALIFADDR: add the specified address.
1291 * SIOCALIFADDR with IFLR_PREFIX:
1292 * add the specified prefix, filling hostid part from
1293 * the first link-local address. prefixlen must be <= 64.
1294 * SIOCDLIFADDR: delete the specified address.
1295 * SIOCDLIFADDR with IFLR_PREFIX:
1296 * delete the first address that matches the specified prefix.
1298 * EINVAL on invalid parameters
1299 * EADDRNOTAVAIL on prefix match failed/specified address not found
1300 * other values may be returned from in6_ioctl()
1302 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1303 * this is to accomodate address naming scheme other than RFC2374,
1305 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1306 * address encoding scheme. (see figure on page 8)
1309 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1310 struct ifnet *ifp, struct thread *td)
1312 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1314 struct sockaddr *sa;
1317 if (!data || !ifp) {
1318 panic("invalid argument to in6_lifaddr_ioctl");
1324 /* address must be specified on GET with IFLR_PREFIX */
1325 if ((iflr->flags & IFLR_PREFIX) == 0)
1330 /* address must be specified on ADD and DELETE */
1331 sa = (struct sockaddr *)&iflr->addr;
1332 if (sa->sa_family != AF_INET6)
1334 if (sa->sa_len != sizeof(struct sockaddr_in6))
1336 /* XXX need improvement */
1337 sa = (struct sockaddr *)&iflr->dstaddr;
1338 if (sa->sa_family && sa->sa_family != AF_INET6)
1340 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1343 default: /* shouldn't happen */
1345 panic("invalid cmd to in6_lifaddr_ioctl");
1351 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1357 struct in6_aliasreq ifra;
1358 struct in6_addr *hostid = NULL;
1361 if ((iflr->flags & IFLR_PREFIX) != 0) {
1362 struct sockaddr_in6 *sin6;
1365 * hostid is to fill in the hostid part of the
1366 * address. hostid points to the first link-local
1367 * address attached to the interface.
1369 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1371 return EADDRNOTAVAIL;
1372 hostid = IFA_IN6(ifa);
1374 /* prefixlen must be <= 64. */
1375 if (64 < iflr->prefixlen)
1377 prefixlen = iflr->prefixlen;
1379 /* hostid part must be zero. */
1380 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1381 if (sin6->sin6_addr.s6_addr32[2] != 0
1382 || sin6->sin6_addr.s6_addr32[3] != 0) {
1386 prefixlen = iflr->prefixlen;
1388 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1389 bzero(&ifra, sizeof(ifra));
1390 bcopy(iflr->iflr_name, ifra.ifra_name,
1391 sizeof(ifra.ifra_name));
1393 bcopy(&iflr->addr, &ifra.ifra_addr,
1394 ((struct sockaddr *)&iflr->addr)->sa_len);
1396 /* fill in hostid part */
1397 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1398 hostid->s6_addr32[2];
1399 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1400 hostid->s6_addr32[3];
1403 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1404 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1405 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1407 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1408 hostid->s6_addr32[2];
1409 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1410 hostid->s6_addr32[3];
1414 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1415 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1417 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1418 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1423 struct in6_ifaddr *ia;
1424 struct in6_addr mask, candidate, match;
1425 struct sockaddr_in6 *sin6;
1428 bzero(&mask, sizeof(mask));
1429 if (iflr->flags & IFLR_PREFIX) {
1430 /* lookup a prefix rather than address. */
1431 in6_len2mask(&mask, iflr->prefixlen);
1433 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1434 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1435 match.s6_addr32[0] &= mask.s6_addr32[0];
1436 match.s6_addr32[1] &= mask.s6_addr32[1];
1437 match.s6_addr32[2] &= mask.s6_addr32[2];
1438 match.s6_addr32[3] &= mask.s6_addr32[3];
1440 /* if you set extra bits, that's wrong */
1441 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1446 if (cmd == SIOCGLIFADDR) {
1447 /* on getting an address, take the 1st match */
1450 /* on deleting an address, do exact match */
1451 in6_len2mask(&mask, 128);
1452 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1453 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1459 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1461 if (ifa->ifa_addr->sa_family != AF_INET6)
1466 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1467 #ifndef SCOPEDROUTING
1469 * XXX: this is adhoc, but is necessary to allow
1470 * a user to specify fe80::/64 (not /10) for a
1471 * link-local address.
1473 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1474 candidate.s6_addr16[1] = 0;
1476 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1477 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1478 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1479 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1480 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1484 return EADDRNOTAVAIL;
1487 if (cmd == SIOCGLIFADDR) {
1488 #ifndef SCOPEDROUTING
1489 struct sockaddr_in6 *s6;
1492 /* fill in the if_laddrreq structure */
1493 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1494 #ifndef SCOPEDROUTING /* XXX see above */
1495 s6 = (struct sockaddr_in6 *)&iflr->addr;
1496 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1497 s6->sin6_addr.s6_addr16[1] = 0;
1499 in6_addr2scopeid(ifp, &s6->sin6_addr);
1502 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1503 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1504 ia->ia_dstaddr.sin6_len);
1505 #ifndef SCOPEDROUTING /* XXX see above */
1506 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1507 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1508 s6->sin6_addr.s6_addr16[1] = 0;
1510 in6_addr2scopeid(ifp,
1515 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1518 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1521 iflr->flags = ia->ia6_flags; /* XXX */
1525 struct in6_aliasreq ifra;
1527 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1528 bzero(&ifra, sizeof(ifra));
1529 bcopy(iflr->iflr_name, ifra.ifra_name,
1530 sizeof(ifra.ifra_name));
1532 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1533 ia->ia_addr.sin6_len);
1534 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1535 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1536 ia->ia_dstaddr.sin6_len);
1538 bzero(&ifra.ifra_dstaddr,
1539 sizeof(ifra.ifra_dstaddr));
1541 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1542 ia->ia_prefixmask.sin6_len);
1544 ifra.ifra_flags = ia->ia6_flags;
1545 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1551 return EOPNOTSUPP; /* just for safety */
1555 * Initialize an interface's intetnet6 address
1556 * and routing table entry.
1559 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6,
1562 int error = 0, plen, ifacount = 0;
1567 * Give the interface a chance to initialize
1568 * if this is its first address,
1569 * and to validate the address if necessary.
1571 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1573 if (ifa->ifa_addr == NULL)
1574 continue; /* just for safety */
1575 if (ifa->ifa_addr->sa_family != AF_INET6)
1580 ia->ia_addr = *sin6;
1582 if (ifacount <= 1 && ifp->if_ioctl &&
1583 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia,
1584 (struct ucred *)NULL))) {
1590 ia->ia_ifa.ifa_metric = ifp->if_metric;
1592 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1596 * If the destination address is specified for a point-to-point
1597 * interface, install a route to the destination as an interface
1600 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1601 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1602 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1603 RTF_UP | RTF_HOST)) != 0)
1605 ia->ia_flags |= IFA_ROUTE;
1609 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1611 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1614 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1616 /* set the rtrequest function to create llinfo */
1617 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1618 in6_ifaddloop(&(ia->ia_ifa));
1625 * Add an address to the list of IP6 multicast addresses for a
1629 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
1631 struct in6_multi *in6m;
1632 struct sockaddr_in6 sin6;
1633 struct ifmultiaddr *ifma;
1639 * Call generic routine to add membership or increment
1640 * refcount. It wants addresses in the form of a sockaddr,
1641 * so we build one here (being careful to zero the unused bytes).
1643 bzero(&sin6, sizeof sin6);
1644 sin6.sin6_family = AF_INET6;
1645 sin6.sin6_len = sizeof sin6;
1646 sin6.sin6_addr = *maddr6;
1647 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1654 * If ifma->ifma_protospec is null, then if_addmulti() created
1655 * a new record. Otherwise, we are done.
1657 if (ifma->ifma_protospec != 0)
1658 return ifma->ifma_protospec;
1660 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1661 at interrupt time? If so, need to fix if_addmulti. XXX */
1662 in6m = (struct in6_multi *)malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1668 bzero(in6m, sizeof *in6m);
1669 in6m->in6m_addr = *maddr6;
1670 in6m->in6m_ifp = ifp;
1671 in6m->in6m_ifma = ifma;
1672 ifma->ifma_protospec = in6m;
1673 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1676 * Let MLD6 know that we have joined a new IP6 multicast
1679 mld6_start_listening(in6m);
1685 * Delete a multicast address record.
1688 in6_delmulti(struct in6_multi *in6m)
1690 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1693 if (ifma->ifma_refcount == 1) {
1695 * No remaining claims to this record; let MLD6 know
1696 * that we are leaving the multicast group.
1698 mld6_stop_listening(in6m);
1699 ifma->ifma_protospec = 0;
1700 LIST_REMOVE(in6m, in6m_entry);
1701 free(in6m, M_IPMADDR);
1703 /* XXX - should be separate API for when we have an ifma? */
1704 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1709 * Find an IPv6 interface link-local address specific to an interface.
1712 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1716 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1718 if (ifa->ifa_addr == NULL)
1719 continue; /* just for safety */
1720 if (ifa->ifa_addr->sa_family != AF_INET6)
1722 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1723 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1730 return((struct in6_ifaddr *)ifa);
1735 * find the internet address corresponding to a given interface and address.
1738 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1742 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1744 if (ifa->ifa_addr == NULL)
1745 continue; /* just for safety */
1746 if (ifa->ifa_addr->sa_family != AF_INET6)
1748 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1752 return((struct in6_ifaddr *)ifa);
1756 * Convert IP6 address to printable (loggable) representation.
1758 static char digits[] = "0123456789abcdef";
1759 static int ip6round = 0;
1761 ip6_sprintf(const struct in6_addr *addr)
1763 static char ip6buf[8][48];
1766 const u_short *a = (const u_short *)addr;
1770 ip6round = (ip6round + 1) & 7;
1771 cp = ip6buf[ip6round];
1773 for (i = 0; i < 8; i++) {
1784 if (dcolon == 0 && *(a + 1) == 0) {
1796 d = (const u_char *)a;
1797 *cp++ = digits[*d >> 4];
1798 *cp++ = digits[*d++ & 0xf];
1799 *cp++ = digits[*d >> 4];
1800 *cp++ = digits[*d & 0xf];
1805 return(ip6buf[ip6round]);
1809 in6_localaddr(struct in6_addr *in6)
1811 struct in6_ifaddr *ia;
1813 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1816 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1817 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1818 &ia->ia_prefixmask.sin6_addr))
1825 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1827 struct in6_ifaddr *ia;
1829 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1830 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1832 #ifdef SCOPEDROUTING
1833 ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id &&
1835 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1836 return(1); /* true */
1838 /* XXX: do we still have to go thru the rest of the list? */
1841 return(0); /* false */
1845 * return length of part which dst and src are equal
1849 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1852 u_char *s = (u_char *)src, *d = (u_char *)dst;
1853 u_char *lim = s + 16, r;
1856 if ((r = (*d++ ^ *s++)) != 0) {
1867 /* XXX: to be scope conscious */
1869 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1871 int bytelen, bitlen;
1874 if (0 > len || len > 128) {
1875 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1883 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1885 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1886 p2->s6_addr[bytelen] >> (8 - bitlen))
1893 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1895 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1896 int bytelen, bitlen, i;
1899 if (0 > len || len > 128) {
1900 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1905 bzero(maskp, sizeof(*maskp));
1908 for (i = 0; i < bytelen; i++)
1909 maskp->s6_addr[i] = 0xff;
1911 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1915 * return the best address out of the same scope
1918 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst)
1920 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
1924 struct in6_ifaddr *ifa_best = NULL;
1928 printf("in6_ifawithscope: output interface is not specified\n");
1934 * We search for all addresses on all interfaces from the beginning.
1935 * Comparing an interface with the outgoing interface will be done
1936 * only at the final stage of tiebreaking.
1938 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1941 * We can never take an address that breaks the scope zone
1942 * of the destination.
1944 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
1947 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1949 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
1951 if (ifa->ifa_addr->sa_family != AF_INET6)
1954 src_scope = in6_addrscope(IFA_IN6(ifa));
1957 * Don't use an address before completing DAD
1958 * nor a duplicated address.
1960 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1964 /* XXX: is there any case to allow anycasts? */
1965 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1969 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1974 * If this is the first address we find,
1977 if (ifa_best == NULL)
1981 * ifa_best is never NULL beyond this line except
1982 * within the block labeled "replace".
1986 * If ifa_best has a smaller scope than dst and
1987 * the current address has a larger one than
1988 * (or equal to) dst, always replace ifa_best.
1989 * Also, if the current address has a smaller scope
1990 * than dst, ignore it unless ifa_best also has a
1992 * Consequently, after the two if-clause below,
1993 * the followings must be satisfied:
1994 * (scope(src) < scope(dst) &&
1995 * scope(best) < scope(dst))
1997 * (scope(best) >= scope(dst) &&
1998 * scope(src) >= scope(dst))
2000 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2001 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2002 goto replace; /* (A) */
2003 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2004 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2008 * A deprecated address SHOULD NOT be used in new
2009 * communications if an alternate (non-deprecated)
2010 * address is available and has sufficient scope.
2011 * RFC 2462, Section 5.5.4.
2013 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2014 IN6_IFF_DEPRECATED) {
2016 * Ignore any deprecated addresses if
2017 * specified by configuration.
2019 if (!ip6_use_deprecated)
2023 * If we have already found a non-deprecated
2024 * candidate, just ignore deprecated addresses.
2026 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2032 * A non-deprecated address is always preferred
2033 * to a deprecated one regardless of scopes and
2034 * address matching (Note invariants ensured by the
2035 * conditions (A) and (B) above.)
2037 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2038 (((struct in6_ifaddr *)ifa)->ia6_flags &
2039 IN6_IFF_DEPRECATED) == 0)
2043 * When we use temporary addresses described in
2044 * RFC 3041, we prefer temporary addresses to
2045 * public autoconf addresses. Again, note the
2046 * invariants from (A) and (B). Also note that we
2047 * don't have any preference between static addresses
2048 * and autoconf addresses (despite of whether or not
2049 * the latter is temporary or public.)
2051 if (ip6_use_tempaddr) {
2052 struct in6_ifaddr *ifat;
2054 ifat = (struct in6_ifaddr *)ifa;
2055 if ((ifa_best->ia6_flags &
2056 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2057 == IN6_IFF_AUTOCONF &&
2059 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2060 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2063 if ((ifa_best->ia6_flags &
2064 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2065 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2067 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2068 == IN6_IFF_AUTOCONF) {
2074 * At this point, we have two cases:
2075 * 1. we are looking at a non-deprecated address,
2076 * and ifa_best is also non-deprecated.
2077 * 2. we are looking at a deprecated address,
2078 * and ifa_best is also deprecated.
2079 * Also, we do not have to consider a case where
2080 * the scope of if_best is larger(smaller) than dst and
2081 * the scope of the current address is smaller(larger)
2082 * than dst. Such a case has already been covered.
2083 * Tiebreaking is done according to the following
2085 * - the scope comparison between the address and
2087 * - the scope comparison between the address and
2088 * ifa_best (bscopecmp)
2089 * - if the address match dst longer than ifa_best
2091 * - if the address is on the outgoing I/F (outI/F)
2093 * Roughly speaking, the selection policy is
2094 * - the most important item is scope. The same scope
2095 * is best. Then search for a larger scope.
2096 * Smaller scopes are the last resort.
2097 * - A deprecated address is chosen only when we have
2098 * no address that has an enough scope, but is
2099 * prefered to any addresses of smaller scopes
2100 * (this must be already done above.)
2101 * - addresses on the outgoing I/F are preferred to
2102 * ones on other interfaces if none of above
2103 * tiebreaks. In the table below, the column "bI"
2104 * means if the best_ifa is on the outgoing
2105 * interface, and the column "sI" means if the ifa
2106 * is on the outgoing interface.
2107 * - If there is no other reasons to choose one,
2108 * longest address match against dst is considered.
2110 * The precise decision table is as follows:
2111 * dscopecmp bscopecmp match bI oI | replace?
2112 * N/A equal N/A Y N | No (1)
2113 * N/A equal N/A N Y | Yes (2)
2114 * N/A equal larger N/A | Yes (3)
2115 * N/A equal !larger N/A | No (4)
2116 * larger larger N/A N/A | No (5)
2117 * larger smaller N/A N/A | Yes (6)
2118 * smaller larger N/A N/A | Yes (7)
2119 * smaller smaller N/A N/A | No (8)
2120 * equal smaller N/A N/A | Yes (9)
2121 * equal larger (already done at A above)
2123 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2124 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2126 if (bscopecmp == 0) {
2127 struct ifnet *bifp = ifa_best->ia_ifp;
2129 if (bifp == oifp && ifp != oifp) /* (1) */
2131 if (bifp != oifp && ifp == oifp) /* (2) */
2135 * Both bifp and ifp are on the outgoing
2136 * interface, or both two are on a different
2137 * interface from the outgoing I/F.
2138 * now we need address matching against dst
2141 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2142 matchcmp = tlen - blen;
2143 if (matchcmp > 0) /* (3) */
2147 if (dscopecmp > 0) {
2148 if (bscopecmp > 0) /* (5) */
2150 goto replace; /* (6) */
2152 if (dscopecmp < 0) {
2153 if (bscopecmp > 0) /* (7) */
2158 /* now dscopecmp must be 0 */
2160 goto replace; /* (9) */
2163 ifa_best = (struct in6_ifaddr *)ifa;
2164 blen = tlen >= 0 ? tlen :
2165 in6_matchlen(IFA_IN6(ifa), dst);
2166 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2170 /* count statistics for future improvements */
2171 if (ifa_best == NULL)
2172 ip6stat.ip6s_sources_none++;
2174 if (oifp == ifa_best->ia_ifp)
2175 ip6stat.ip6s_sources_sameif[best_scope]++;
2177 ip6stat.ip6s_sources_otherif[best_scope]++;
2179 if (best_scope == dst_scope)
2180 ip6stat.ip6s_sources_samescope[best_scope]++;
2182 ip6stat.ip6s_sources_otherscope[best_scope]++;
2184 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2185 ip6stat.ip6s_sources_deprecated[best_scope]++;
2192 * return the best address out of the same scope. if no address was
2193 * found, return the first valid address from designated IF.
2196 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2198 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2200 struct in6_ifaddr *besta = 0;
2201 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2203 dep[0] = dep[1] = NULL;
2206 * We first look for addresses in the same scope.
2207 * If there is one, return it.
2208 * If two or more, return one which matches the dst longest.
2209 * If none, return one of global addresses assigned other ifs.
2211 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2213 if (ifa->ifa_addr->sa_family != AF_INET6)
2215 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2216 continue; /* XXX: is there any case to allow anycast? */
2217 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2218 continue; /* don't use this interface */
2219 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2221 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2222 if (ip6_use_deprecated)
2223 dep[0] = (struct in6_ifaddr *)ifa;
2227 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2229 * call in6_matchlen() as few as possible
2233 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2234 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2237 besta = (struct in6_ifaddr *)ifa;
2240 besta = (struct in6_ifaddr *)ifa;
2246 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2248 if (ifa->ifa_addr->sa_family != AF_INET6)
2250 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2251 continue; /* XXX: is there any case to allow anycast? */
2252 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2253 continue; /* don't use this interface */
2254 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2256 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2257 if (ip6_use_deprecated)
2258 dep[1] = (struct in6_ifaddr *)ifa;
2262 return (struct in6_ifaddr *)ifa;
2265 /* use the last-resort values, that are, deprecated addresses */
2275 * perform DAD when interface becomes IFF_UP.
2278 in6_if_up(struct ifnet *ifp)
2281 struct in6_ifaddr *ia;
2282 int dad_delay; /* delay ticks before DAD output */
2285 * special cases, like 6to4, are handled in in6_ifattach
2287 in6_ifattach(ifp, NULL);
2290 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2292 if (ifa->ifa_addr->sa_family != AF_INET6)
2294 ia = (struct in6_ifaddr *)ifa;
2295 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2296 nd6_dad_start(ifa, &dad_delay);
2301 in6if_do_dad(struct ifnet *ifp)
2303 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2306 switch (ifp->if_type) {
2312 * These interfaces do not have the IFF_LOOPBACK flag,
2313 * but loop packets back. We do not have to do DAD on such
2314 * interfaces. We should even omit it, because loop-backed
2315 * NS would confuse the DAD procedure.
2320 * Our DAD routine requires the interface up and running.
2321 * However, some interfaces can be up before the RUNNING
2322 * status. Additionaly, users may try to assign addresses
2323 * before the interface becomes up (or running).
2324 * We simply skip DAD in such a case as a work around.
2325 * XXX: we should rather mark "tentative" on such addresses,
2326 * and do DAD after the interface becomes ready.
2328 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2329 (IFF_UP|IFF_RUNNING))
2337 * Calculate max IPv6 MTU through all the interfaces and store it
2343 unsigned long maxmtu = 0;
2346 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2348 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2349 nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
2350 maxmtu = nd_ifinfo[ifp->if_index].linkmtu;
2352 if (maxmtu) /* update only when maxmtu is positive */
2353 in6_maxmtu = maxmtu;
2357 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2358 * v4 mapped addr or v4 compat addr
2361 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2363 bzero(sin, sizeof(*sin));
2364 sin->sin_len = sizeof(struct sockaddr_in);
2365 sin->sin_family = AF_INET;
2366 sin->sin_port = sin6->sin6_port;
2367 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2370 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2372 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2374 bzero(sin6, sizeof(*sin6));
2375 sin6->sin6_len = sizeof(struct sockaddr_in6);
2376 sin6->sin6_family = AF_INET6;
2377 sin6->sin6_port = sin->sin_port;
2378 sin6->sin6_addr.s6_addr32[0] = 0;
2379 sin6->sin6_addr.s6_addr32[1] = 0;
2380 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2381 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2384 /* Convert sockaddr_in6 into sockaddr_in. */
2386 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2388 struct sockaddr_in *sin_p;
2389 struct sockaddr_in6 sin6;
2392 * Save original sockaddr_in6 addr and convert it
2395 sin6 = *(struct sockaddr_in6 *)nam;
2396 sin_p = (struct sockaddr_in *)nam;
2397 in6_sin6_2_sin(sin_p, &sin6);
2400 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2402 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2404 struct sockaddr_in *sin_p;
2405 struct sockaddr_in6 *sin6_p;
2407 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2409 sin_p = (struct sockaddr_in *)*nam;
2410 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2411 FREE(*nam, M_SONAME);
2412 *nam = (struct sockaddr *)sin6_p;