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.21 2006/09/05 03:48:12 dillon 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>
83 #include <sys/thread2.h>
86 #include <net/if_types.h>
87 #include <net/route.h>
88 #include <net/if_dl.h>
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92 #include <netinet/if_ether.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/ip.h>
95 #include <netinet/in_pcb.h>
97 #include <netinet/ip6.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet6/nd6.h>
100 #include <netinet6/mld6_var.h>
101 #include <netinet6/ip6_mroute.h>
102 #include <netinet6/in6_ifattach.h>
103 #include <netinet6/scope6_var.h>
104 #include <netinet6/in6_pcb.h>
106 #include <net/net_osdep.h>
108 MALLOC_DEFINE(M_IPMADDR, "in6_multi", "internet multicast address");
111 * Definitions of some costant IP6 addresses.
113 const struct in6_addr kin6addr_any = IN6ADDR_ANY_INIT;
114 const struct in6_addr kin6addr_loopback = IN6ADDR_LOOPBACK_INIT;
115 const struct in6_addr kin6addr_nodelocal_allnodes =
116 IN6ADDR_NODELOCAL_ALLNODES_INIT;
117 const struct in6_addr kin6addr_linklocal_allnodes =
118 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
119 const struct in6_addr kin6addr_linklocal_allrouters =
120 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
122 const struct in6_addr in6mask0 = IN6MASK0;
123 const struct in6_addr in6mask32 = IN6MASK32;
124 const struct in6_addr in6mask64 = IN6MASK64;
125 const struct in6_addr in6mask96 = IN6MASK96;
126 const struct in6_addr in6mask128 = IN6MASK128;
128 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
129 0, 0, IN6ADDR_ANY_INIT, 0};
131 static int in6_lifaddr_ioctl (struct socket *, u_long, caddr_t,
132 struct ifnet *, struct thread *);
133 static int in6_ifinit (struct ifnet *, struct in6_ifaddr *,
134 struct sockaddr_in6 *, int);
135 static void in6_unlink_ifa (struct in6_ifaddr *, struct ifnet *);
136 static void in6_ifloop_request_callback(int, int, struct rt_addrinfo *, struct rtentry *, void *);
138 struct in6_multihead in6_multihead; /* XXX BSS initialization */
140 int (*faithprefix_p)(struct in6_addr *);
143 * Subroutine for in6_ifaddloop() and in6_ifremloop().
144 * This routine does actual work.
147 in6_ifloop_request(int cmd, struct ifaddr *ifa)
149 struct sockaddr_in6 all1_sa;
150 struct rt_addrinfo rtinfo;
153 bzero(&all1_sa, sizeof(all1_sa));
154 all1_sa.sin6_family = AF_INET6;
155 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
156 all1_sa.sin6_addr = in6mask128;
159 * We specify the address itself as the gateway, and set the
160 * RTF_LLINFO flag, so that the corresponding host route would have
161 * the flag, and thus applications that assume traditional behavior
162 * would be happy. Note that we assume the caller of the function
163 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
164 * which changes the outgoing interface to the loopback interface.
166 bzero(&rtinfo, sizeof(struct rt_addrinfo));
167 rtinfo.rti_info[RTAX_DST] = ifa->ifa_addr;
168 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
169 rtinfo.rti_info[RTAX_NETMASK] = (struct sockaddr *)&all1_sa;
170 rtinfo.rti_flags = RTF_UP|RTF_HOST|RTF_LLINFO;
172 error = rtrequest1_global(cmd, &rtinfo,
173 in6_ifloop_request_callback, ifa);
175 log(LOG_ERR, "in6_ifloop_request: "
176 "%s operation failed for %s (errno=%d)\n",
177 cmd == RTM_ADD ? "ADD" : "DELETE",
178 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
184 in6_ifloop_request_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
185 struct rtentry *rt, void *arg)
187 struct ifaddr *ifa = arg;
193 * Make sure rt_ifa be equal to IFA, the second argument of the
195 * We need this because when we refer to rt_ifa->ia6_flags in
196 * ip6_input, we assume that the rt_ifa points to the address instead
197 * of the loopback address.
199 if (cmd == RTM_ADD && rt && ifa != rt->rt_ifa) {
208 * Report the addition/removal of the address to the routing socket.
209 * XXX: since we called rtinit for a p2p interface with a destination,
210 * we end up reporting twice in such a case. Should we rather
211 * omit the second report?
215 rt_newaddrmsg(cmd, ifa, error, rt);
216 if (cmd == RTM_DELETE) {
217 if (rt->rt_refcnt == 0) {
224 /* no way to return any new error */
229 * Add ownaddr as loopback rtentry. We previously add the route only if
230 * necessary (ex. on a p2p link). However, since we now manage addresses
231 * separately from prefixes, we should always add the route. We can't
232 * rely on the cloning mechanism from the corresponding interface route
236 in6_ifaddloop(struct ifaddr *ifa)
240 /* If there is no loopback entry, allocate one. */
241 rt = rtpurelookup(ifa->ifa_addr);
242 if (rt == NULL || !(rt->rt_flags & RTF_HOST) ||
243 !(rt->rt_ifp->if_flags & IFF_LOOPBACK))
244 in6_ifloop_request(RTM_ADD, ifa);
250 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
254 in6_ifremloop(struct ifaddr *ifa)
256 struct in6_ifaddr *ia;
261 * Some of BSD variants do not remove cloned routes
262 * from an interface direct route, when removing the direct route
263 * (see comments in net/net_osdep.h). Even for variants that do remove
264 * cloned routes, they could fail to remove the cloned routes when
265 * we handle multple addresses that share a common prefix.
266 * So, we should remove the route corresponding to the deleted address
267 * regardless of the result of in6_is_ifloop_auto().
271 * Delete the entry only if exact one ifa exists. More than one ifa
272 * can exist if we assign a same single address to multiple
273 * (probably p2p) interfaces.
274 * XXX: we should avoid such a configuration in IPv6...
276 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
277 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
286 * Before deleting, check if a corresponding loopbacked host
287 * route surely exists. With this check, we can avoid to
288 * delete an interface direct route whose destination is same
289 * as the address being removed. This can happen when remofing
290 * a subnet-router anycast address on an interface attahced
291 * to a shared medium.
293 rt = rtpurelookup(ifa->ifa_addr);
294 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
295 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
297 in6_ifloop_request(RTM_DELETE, ifa);
303 in6_ifindex2scopeid(int idx)
307 struct sockaddr_in6 *sin6;
309 if (idx < 0 || if_index < idx)
311 ifp = ifindex2ifnet[idx];
313 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
315 if (ifa->ifa_addr->sa_family != AF_INET6)
317 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
318 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
319 return sin6->sin6_scope_id & 0xffff;
326 in6_mask2len(struct in6_addr *mask, u_char *lim0)
329 u_char *lim = lim0, *p;
332 lim0 - (u_char *)mask > sizeof(*mask)) /* ignore the scope_id part */
333 lim = (u_char *)mask + sizeof(*mask);
334 for (p = (u_char *)mask; p < lim; x++, p++) {
340 for (y = 0; y < 8; y++) {
341 if ((*p & (0x80 >> y)) == 0)
347 * when the limit pointer is given, do a stricter check on the
351 if (y != 0 && (*p & (0x00ff >> y)) != 0)
353 for (p = p + 1; p < lim; p++)
362 in6_len2mask(struct in6_addr *mask, int len)
366 bzero(mask, sizeof(*mask));
367 for (i = 0; i < len / 8; i++)
368 mask->s6_addr8[i] = 0xff;
370 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
373 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
374 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
377 in6_control(struct socket *so, u_long cmd, caddr_t data,
378 struct ifnet *ifp, struct thread *td)
380 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
381 struct in6_ifaddr *ia = NULL;
382 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
391 case SIOCGETSGCNT_IN6:
392 case SIOCGETMIFCNT_IN6:
393 return (mrt6_ioctl(cmd, data));
400 case SIOCSNDFLUSH_IN6:
401 case SIOCSPFXFLUSH_IN6:
402 case SIOCSRTRFLUSH_IN6:
403 case SIOCSDEFIFACE_IN6:
404 case SIOCSIFINFO_FLAGS:
408 case OSIOCGIFINFO_IN6:
409 case SIOCGIFINFO_IN6:
412 case SIOCGNBRINFO_IN6:
413 case SIOCGDEFIFACE_IN6:
414 return(nd6_ioctl(cmd, data, ifp));
418 case SIOCSIFPREFIX_IN6:
419 case SIOCDIFPREFIX_IN6:
420 case SIOCAIFPREFIX_IN6:
421 case SIOCCIFPREFIX_IN6:
422 case SIOCSGIFPREFIX_IN6:
423 case SIOCGIFPREFIX_IN6:
425 "prefix ioctls are now invalidated. "
426 "please use ifconfig.\n");
434 return(scope6_set(ifp,
435 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
438 return(scope6_get(ifp,
439 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
442 return(scope6_get_default((struct scope6_id *)
443 ifr->ifr_ifru.ifru_scope_id));
454 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
458 * Find address for this interface, if it exists.
460 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
461 struct sockaddr_in6 *sa6 =
462 (struct sockaddr_in6 *)&ifra->ifra_addr;
464 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
465 if (sa6->sin6_addr.s6_addr16[1] == 0) {
466 /* link ID is not embedded by the user */
467 sa6->sin6_addr.s6_addr16[1] =
468 htons(ifp->if_index);
469 } else if (sa6->sin6_addr.s6_addr16[1] !=
470 htons(ifp->if_index)) {
471 return(EINVAL); /* link ID contradicts */
473 if (sa6->sin6_scope_id) {
474 if (sa6->sin6_scope_id !=
475 (u_int32_t)ifp->if_index)
477 sa6->sin6_scope_id = 0; /* XXX: good way? */
480 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
484 case SIOCSIFADDR_IN6:
485 case SIOCSIFDSTADDR_IN6:
486 case SIOCSIFNETMASK_IN6:
488 * Since IPv6 allows a node to assign multiple addresses
489 * on a single interface, SIOCSIFxxx ioctls are not suitable
490 * and should be unused.
492 /* we decided to obsolete this command (20000704) */
495 case SIOCDIFADDR_IN6:
497 * for IPv4, we look for existing in_ifaddr here to allow
498 * "ifconfig if0 delete" to remove first IPv4 address on the
499 * interface. For IPv6, as the spec allow multiple interface
500 * address from the day one, we consider "remove the first one"
501 * semantics to be not preferable.
504 return(EADDRNOTAVAIL);
506 case SIOCAIFADDR_IN6:
508 * We always require users to specify a valid IPv6 address for
509 * the corresponding operation.
511 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
512 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
513 return(EAFNOSUPPORT);
519 case SIOCGIFADDR_IN6:
520 /* This interface is basically deprecated. use SIOCGIFCONF. */
522 case SIOCGIFAFLAG_IN6:
523 case SIOCGIFNETMASK_IN6:
524 case SIOCGIFDSTADDR_IN6:
525 case SIOCGIFALIFETIME_IN6:
526 /* must think again about its semantics */
528 return(EADDRNOTAVAIL);
530 case SIOCSIFALIFETIME_IN6:
532 struct in6_addrlifetime *lt;
537 return(EADDRNOTAVAIL);
538 /* sanity for overflow - beware unsigned */
539 lt = &ifr->ifr_ifru.ifru_lifetime;
540 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
541 && lt->ia6t_vltime + time_second < time_second) {
544 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
545 && lt->ia6t_pltime + time_second < time_second) {
554 case SIOCGIFADDR_IN6:
555 ifr->ifr_addr = ia->ia_addr;
558 case SIOCGIFDSTADDR_IN6:
559 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
562 * XXX: should we check if ifa_dstaddr is NULL and return
565 ifr->ifr_dstaddr = ia->ia_dstaddr;
568 case SIOCGIFNETMASK_IN6:
569 ifr->ifr_addr = ia->ia_prefixmask;
572 case SIOCGIFAFLAG_IN6:
573 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
576 case SIOCGIFSTAT_IN6:
579 bzero(&ifr->ifr_ifru.ifru_stat,
580 sizeof(ifr->ifr_ifru.ifru_stat));
581 ifr->ifr_ifru.ifru_stat =
582 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
585 case SIOCGIFSTAT_ICMP6:
586 bzero(&ifr->ifr_ifru.ifru_stat,
587 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
588 ifr->ifr_ifru.ifru_icmp6stat =
589 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
592 case SIOCGIFALIFETIME_IN6:
593 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
596 case SIOCSIFALIFETIME_IN6:
597 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
599 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
600 ia->ia6_lifetime.ia6t_expire =
601 time_second + ia->ia6_lifetime.ia6t_vltime;
603 ia->ia6_lifetime.ia6t_expire = 0;
604 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
605 ia->ia6_lifetime.ia6t_preferred =
606 time_second + ia->ia6_lifetime.ia6t_pltime;
608 ia->ia6_lifetime.ia6t_preferred = 0;
611 case SIOCAIFADDR_IN6:
614 struct nd_prefix pr0, *pr;
617 * first, make or update the interface address structure,
618 * and link it to the list.
620 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
624 * then, make the prefix on-link on the interface.
625 * XXX: we'd rather create the prefix before the address, but
626 * we need at least one address to install the corresponding
627 * interface route, so we configure the address first.
631 * convert mask to prefix length (prefixmask has already
632 * been validated in in6_update_ifa().
634 bzero(&pr0, sizeof(pr0));
636 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
638 if (pr0.ndpr_plen == 128)
639 break; /* we don't need to install a host route. */
640 pr0.ndpr_prefix = ifra->ifra_addr;
641 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
642 /* apply the mask for safety. */
643 for (i = 0; i < 4; i++) {
644 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
645 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
648 * XXX: since we don't have an API to set prefix (not address)
649 * lifetimes, we just use the same lifetimes as addresses.
650 * The (temporarily) installed lifetimes can be overridden by
651 * later advertised RAs (when accept_rtadv is non 0), which is
652 * an intended behavior.
654 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
656 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
657 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
658 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
660 /* add the prefix if there's one. */
661 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
663 * nd6_prelist_add will install the corresponding
666 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
669 log(LOG_ERR, "nd6_prelist_add succedded but "
671 return(EINVAL); /* XXX panic here? */
674 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
676 /* XXX: this should not happen! */
677 log(LOG_ERR, "in6_control: addition succeeded, but"
680 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
681 ia->ia6_ndpr == NULL) { /* new autoconfed addr */
686 * If this is the first autoconf address from
687 * the prefix, create a temporary address
688 * as well (when specified).
690 if (ip6_use_tempaddr &&
691 pr->ndpr_refcnt == 1) {
693 if ((e = in6_tmpifadd(ia, 1)) != 0) {
694 log(LOG_NOTICE, "in6_control: "
695 "failed to create a "
696 "temporary address, "
704 * this might affect the status of autoconfigured
705 * addresses, that is, this address might make
706 * other addresses detached.
708 pfxlist_onlink_check();
710 if (error == 0 && ia)
711 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
715 case SIOCDIFADDR_IN6:
718 struct nd_prefix pr0, *pr;
721 * If the address being deleted is the only one that owns
722 * the corresponding prefix, expire the prefix as well.
723 * XXX: theoretically, we don't have to warry about such
724 * relationship, since we separate the address management
725 * and the prefix management. We do this, however, to provide
726 * as much backward compatibility as possible in terms of
727 * the ioctl operation.
729 bzero(&pr0, sizeof(pr0));
731 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
733 if (pr0.ndpr_plen == 128)
735 pr0.ndpr_prefix = ia->ia_addr;
736 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
737 for (i = 0; i < 4; i++) {
738 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
739 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
742 * The logic of the following condition is a bit complicated.
743 * We expire the prefix when
744 * 1. the address obeys autoconfiguration and it is the
745 * only owner of the associated prefix, or
746 * 2. the address does not obey autoconf and there is no
747 * other owner of the prefix.
749 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
750 (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
751 pr->ndpr_refcnt == 1) ||
752 ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
753 pr->ndpr_refcnt == 0))) {
754 pr->ndpr_expire = 1; /* XXX: just for expiration */
758 in6_purgeaddr(&ia->ia_ifa);
759 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
764 if (ifp == NULL || ifp->if_ioctl == 0)
766 lwkt_serialize_enter(ifp->if_serializer);
767 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
768 lwkt_serialize_exit(ifp->if_serializer);
776 * Update parameters of an IPv6 interface address.
777 * If necessary, a new entry is created and linked into address chains.
778 * This function is separated from in6_control().
779 * XXX: should this be performed under splnet()?
782 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
783 struct in6_ifaddr *ia)
785 int error = 0, hostIsNew = 0, plen = -1;
786 struct in6_ifaddr *oia;
787 struct sockaddr_in6 dst6;
788 struct in6_addrlifetime *lt;
790 /* Validate parameters */
791 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
795 * The destination address for a p2p link must have a family
796 * of AF_UNSPEC or AF_INET6.
798 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
799 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
800 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
801 return(EAFNOSUPPORT);
803 * validate ifra_prefixmask. don't check sin6_family, netmask
804 * does not carry fields other than sin6_len.
806 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
809 * Because the IPv6 address architecture is classless, we require
810 * users to specify a (non 0) prefix length (mask) for a new address.
811 * We also require the prefix (when specified) mask is valid, and thus
812 * reject a non-consecutive mask.
814 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
816 if (ifra->ifra_prefixmask.sin6_len != 0) {
817 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
818 (u_char *)&ifra->ifra_prefixmask +
819 ifra->ifra_prefixmask.sin6_len);
825 * In this case, ia must not be NULL. We just use its prefix
828 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
831 * If the destination address on a p2p interface is specified,
832 * and the address is a scoped one, validate/set the scope
835 dst6 = ifra->ifra_dstaddr;
836 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
837 (dst6.sin6_family == AF_INET6)) {
840 if ((error = in6_recoverscope(&dst6,
841 &ifra->ifra_dstaddr.sin6_addr,
844 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
845 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
846 dst6.sin6_scope_id = scopeid;
847 else if (dst6.sin6_scope_id != scopeid)
848 return(EINVAL); /* scope ID mismatch. */
849 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
852 dst6.sin6_scope_id = 0; /* XXX */
855 * The destination address can be specified only for a p2p or a
856 * loopback interface. If specified, the corresponding prefix length
859 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
860 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
861 /* XXX: noisy message */
862 log(LOG_INFO, "in6_update_ifa: a destination can be "
863 "specified for a p2p or a loopback IF only\n");
868 * The following message seems noisy, but we dare to
869 * add it for diagnosis.
871 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
872 "when dstaddr is specified\n");
876 /* lifetime consistency check */
877 lt = &ifra->ifra_lifetime;
878 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
879 && lt->ia6t_vltime + time_second < time_second) {
882 if (lt->ia6t_vltime == 0) {
884 * the following log might be noisy, but this is a typical
885 * configuration mistake or a tool's bug.
888 "in6_update_ifa: valid lifetime is 0 for %s\n",
889 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
891 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
892 && lt->ia6t_pltime + time_second < time_second) {
897 * If this is a new address, allocate a new ifaddr and link it
903 * When in6_update_ifa() is called in a process of a received
904 * RA, it is called under splnet(). So, we should call malloc
907 ia = (struct in6_ifaddr *)
908 kmalloc(sizeof(*ia), M_IFADDR, M_NOWAIT);
911 bzero((caddr_t)ia, sizeof(*ia));
912 /* Initialize the address and masks */
913 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
914 ia->ia_addr.sin6_family = AF_INET6;
915 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
916 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
918 * XXX: some functions expect that ifa_dstaddr is not
919 * NULL for p2p interfaces.
921 ia->ia_ifa.ifa_dstaddr
922 = (struct sockaddr *)&ia->ia_dstaddr;
924 ia->ia_ifa.ifa_dstaddr = NULL;
926 ia->ia_ifa.ifa_netmask
927 = (struct sockaddr *)&ia->ia_prefixmask;
930 if ((oia = in6_ifaddr) != NULL) {
931 for ( ; oia->ia_next; oia = oia->ia_next)
937 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
941 /* set prefix mask */
942 if (ifra->ifra_prefixmask.sin6_len) {
944 * We prohibit changing the prefix length of an existing
946 * + such an operation should be rare in IPv6, and
947 * + the operation would confuse prefix management.
949 if (ia->ia_prefixmask.sin6_len &&
950 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
951 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
952 " existing (%s) address should not be changed\n",
953 ip6_sprintf(&ia->ia_addr.sin6_addr));
957 ia->ia_prefixmask = ifra->ifra_prefixmask;
961 * If a new destination address is specified, scrub the old one and
962 * install the new destination. Note that the interface must be
963 * p2p or loopback (see the check above.)
965 if (dst6.sin6_family == AF_INET6 &&
966 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
967 &ia->ia_dstaddr.sin6_addr)) {
970 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
971 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
973 log(LOG_ERR, "in6_update_ifa: failed to remove "
974 "a route to the old destination: %s\n",
975 ip6_sprintf(&ia->ia_addr.sin6_addr));
976 /* proceed anyway... */
979 ia->ia_flags &= ~IFA_ROUTE;
980 ia->ia_dstaddr = dst6;
983 /* reset the interface and routing table appropriately. */
984 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
988 * Beyond this point, we should call in6_purgeaddr upon an error,
989 * not just go to unlink.
992 #if 0 /* disable this mechanism for now */
993 /* update prefix list */
995 (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
998 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
999 if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
1000 in6_purgeaddr((struct ifaddr *)ia);
1006 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1007 struct sockaddr_in6 mltaddr, mltmask;
1008 struct in6_multi *in6m;
1012 * join solicited multicast addr for new host id
1014 struct in6_addr llsol;
1015 bzero(&llsol, sizeof(struct in6_addr));
1016 llsol.s6_addr16[0] = htons(0xff02);
1017 llsol.s6_addr16[1] = htons(ifp->if_index);
1018 llsol.s6_addr32[1] = 0;
1019 llsol.s6_addr32[2] = htonl(1);
1020 llsol.s6_addr32[3] =
1021 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1022 llsol.s6_addr8[12] = 0xff;
1023 in6_addmulti(&llsol, ifp, &error);
1026 "in6_update_ifa: addmulti failed for "
1027 "%s on %s (errno=%d)\n",
1028 ip6_sprintf(&llsol), if_name(ifp),
1030 in6_purgeaddr((struct ifaddr *)ia);
1035 bzero(&mltmask, sizeof(mltmask));
1036 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1037 mltmask.sin6_family = AF_INET6;
1038 mltmask.sin6_addr = in6mask32;
1041 * join link-local all-nodes address
1043 bzero(&mltaddr, sizeof(mltaddr));
1044 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1045 mltaddr.sin6_family = AF_INET6;
1046 mltaddr.sin6_addr = kin6addr_linklocal_allnodes;
1047 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1049 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1051 rtrequest_global(RTM_ADD,
1052 (struct sockaddr *)&mltaddr,
1053 (struct sockaddr *)&ia->ia_addr,
1054 (struct sockaddr *)&mltmask,
1055 RTF_UP|RTF_CLONING); /* xxx */
1056 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1059 "in6_update_ifa: addmulti failed for "
1060 "%s on %s (errno=%d)\n",
1061 ip6_sprintf(&mltaddr.sin6_addr),
1062 if_name(ifp), error);
1067 * join node information group address
1069 #define hostnamelen strlen(hostname)
1070 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1072 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1073 if (in6m == NULL && ia != NULL) {
1074 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1076 log(LOG_WARNING, "in6_update_ifa: "
1077 "addmulti failed for "
1078 "%s on %s (errno=%d)\n",
1079 ip6_sprintf(&mltaddr.sin6_addr),
1080 if_name(ifp), error);
1087 * join node-local all-nodes address, on loopback.
1088 * XXX: since "node-local" is obsoleted by interface-local,
1089 * we have to join the group on every interface with
1090 * some interface-boundary restriction.
1092 if (ifp->if_flags & IFF_LOOPBACK) {
1093 struct in6_ifaddr *ia_loop;
1095 struct in6_addr loop6 = kin6addr_loopback;
1096 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1098 mltaddr.sin6_addr = kin6addr_nodelocal_allnodes;
1100 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1101 if (in6m == NULL && ia_loop != NULL) {
1102 rtrequest_global(RTM_ADD,
1103 (struct sockaddr *)&mltaddr,
1104 (struct sockaddr *)&ia_loop->ia_addr,
1105 (struct sockaddr *)&mltmask,
1107 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1109 log(LOG_WARNING, "in6_update_ifa: "
1110 "addmulti failed for %s on %s "
1112 ip6_sprintf(&mltaddr.sin6_addr),
1113 if_name(ifp), error);
1119 ia->ia6_flags = ifra->ifra_flags;
1120 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1121 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1123 ia->ia6_lifetime = ifra->ifra_lifetime;
1125 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1126 ia->ia6_lifetime.ia6t_expire =
1127 time_second + ia->ia6_lifetime.ia6t_vltime;
1129 ia->ia6_lifetime.ia6t_expire = 0;
1130 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1131 ia->ia6_lifetime.ia6t_preferred =
1132 time_second + ia->ia6_lifetime.ia6t_pltime;
1134 ia->ia6_lifetime.ia6t_preferred = 0;
1137 * Perform DAD, if needed.
1138 * XXX It may be of use, if we can administratively
1141 if (in6if_do_dad(ifp) && (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
1142 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1143 nd6_dad_start((struct ifaddr *)ia, NULL);
1150 * XXX: if a change of an existing address failed, keep the entry
1154 in6_unlink_ifa(ia, ifp);
1159 in6_purgeaddr(struct ifaddr *ifa)
1161 struct ifnet *ifp = ifa->ifa_ifp;
1162 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1164 /* stop DAD processing */
1168 * delete route to the destination of the address being purged.
1169 * The interface must be p2p or loopback in this case.
1171 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1174 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1176 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1177 "a route to the p2p destination: %s on %s, "
1179 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1181 /* proceed anyway... */
1184 ia->ia_flags &= ~IFA_ROUTE;
1187 /* Remove ownaddr's loopback rtentry, if it exists. */
1188 in6_ifremloop(&(ia->ia_ifa));
1190 if (ifp->if_flags & IFF_MULTICAST) {
1192 * delete solicited multicast addr for deleting host id
1194 struct in6_multi *in6m;
1195 struct in6_addr llsol;
1196 bzero(&llsol, sizeof(struct in6_addr));
1197 llsol.s6_addr16[0] = htons(0xff02);
1198 llsol.s6_addr16[1] = htons(ifp->if_index);
1199 llsol.s6_addr32[1] = 0;
1200 llsol.s6_addr32[2] = htonl(1);
1201 llsol.s6_addr32[3] =
1202 ia->ia_addr.sin6_addr.s6_addr32[3];
1203 llsol.s6_addr8[12] = 0xff;
1205 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1210 in6_unlink_ifa(ia, ifp);
1214 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1217 struct in6_ifaddr *oia;
1221 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1224 if (oia == (ia = in6_ifaddr))
1225 in6_ifaddr = ia->ia_next;
1227 while (ia->ia_next && (ia->ia_next != oia))
1230 ia->ia_next = oia->ia_next;
1233 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1237 if (oia->ia6_ifpr) { /* check for safety */
1238 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1239 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1240 in6_prefix_remove_ifid(iilen, oia);
1244 * When an autoconfigured address is being removed, release the
1245 * reference to the base prefix. Also, since the release might
1246 * affect the status of other (detached) addresses, call
1247 * pfxlist_onlink_check().
1249 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1250 if (oia->ia6_ndpr == NULL) {
1251 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1252 "%p has no prefix\n", oia);
1254 oia->ia6_ndpr->ndpr_refcnt--;
1255 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1256 oia->ia6_ndpr = NULL;
1259 pfxlist_onlink_check();
1263 * release another refcnt for the link from in6_ifaddr.
1264 * Note that we should decrement the refcnt at least once for all *BSD.
1266 IFAFREE(&oia->ia_ifa);
1272 in6_purgeif(struct ifnet *ifp)
1274 struct ifaddr *ifa, *nifa;
1276 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa)
1278 nifa = TAILQ_NEXT(ifa, ifa_list);
1279 if (ifa->ifa_addr->sa_family != AF_INET6)
1289 * SIOCGLIFADDR: get first address. (?)
1290 * SIOCGLIFADDR with IFLR_PREFIX:
1291 * get first address that matches the specified prefix.
1292 * SIOCALIFADDR: add the specified address.
1293 * SIOCALIFADDR with IFLR_PREFIX:
1294 * add the specified prefix, filling hostid part from
1295 * the first link-local address. prefixlen must be <= 64.
1296 * SIOCDLIFADDR: delete the specified address.
1297 * SIOCDLIFADDR with IFLR_PREFIX:
1298 * delete the first address that matches the specified prefix.
1300 * EINVAL on invalid parameters
1301 * EADDRNOTAVAIL on prefix match failed/specified address not found
1302 * other values may be returned from in6_ioctl()
1304 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1305 * this is to accomodate address naming scheme other than RFC2374,
1307 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1308 * address encoding scheme. (see figure on page 8)
1311 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1312 struct ifnet *ifp, struct thread *td)
1314 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1316 struct sockaddr *sa;
1319 if (!data || !ifp) {
1320 panic("invalid argument to in6_lifaddr_ioctl");
1326 /* address must be specified on GET with IFLR_PREFIX */
1327 if ((iflr->flags & IFLR_PREFIX) == 0)
1332 /* address must be specified on ADD and DELETE */
1333 sa = (struct sockaddr *)&iflr->addr;
1334 if (sa->sa_family != AF_INET6)
1336 if (sa->sa_len != sizeof(struct sockaddr_in6))
1338 /* XXX need improvement */
1339 sa = (struct sockaddr *)&iflr->dstaddr;
1340 if (sa->sa_family && sa->sa_family != AF_INET6)
1342 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1345 default: /* shouldn't happen */
1347 panic("invalid cmd to in6_lifaddr_ioctl");
1353 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1359 struct in6_aliasreq ifra;
1360 struct in6_addr *hostid = NULL;
1363 if ((iflr->flags & IFLR_PREFIX) != 0) {
1364 struct sockaddr_in6 *sin6;
1367 * hostid is to fill in the hostid part of the
1368 * address. hostid points to the first link-local
1369 * address attached to the interface.
1371 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1373 return EADDRNOTAVAIL;
1374 hostid = IFA_IN6(ifa);
1376 /* prefixlen must be <= 64. */
1377 if (64 < iflr->prefixlen)
1379 prefixlen = iflr->prefixlen;
1381 /* hostid part must be zero. */
1382 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1383 if (sin6->sin6_addr.s6_addr32[2] != 0
1384 || sin6->sin6_addr.s6_addr32[3] != 0) {
1388 prefixlen = iflr->prefixlen;
1390 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1391 bzero(&ifra, sizeof(ifra));
1392 bcopy(iflr->iflr_name, ifra.ifra_name,
1393 sizeof(ifra.ifra_name));
1395 bcopy(&iflr->addr, &ifra.ifra_addr,
1396 ((struct sockaddr *)&iflr->addr)->sa_len);
1398 /* fill in hostid part */
1399 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1400 hostid->s6_addr32[2];
1401 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1402 hostid->s6_addr32[3];
1405 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1406 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1407 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1409 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1410 hostid->s6_addr32[2];
1411 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1412 hostid->s6_addr32[3];
1416 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1417 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1419 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1420 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1425 struct in6_ifaddr *ia;
1426 struct in6_addr mask, candidate, match;
1427 struct sockaddr_in6 *sin6;
1430 bzero(&mask, sizeof(mask));
1431 if (iflr->flags & IFLR_PREFIX) {
1432 /* lookup a prefix rather than address. */
1433 in6_len2mask(&mask, iflr->prefixlen);
1435 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1436 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1437 match.s6_addr32[0] &= mask.s6_addr32[0];
1438 match.s6_addr32[1] &= mask.s6_addr32[1];
1439 match.s6_addr32[2] &= mask.s6_addr32[2];
1440 match.s6_addr32[3] &= mask.s6_addr32[3];
1442 /* if you set extra bits, that's wrong */
1443 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1448 if (cmd == SIOCGLIFADDR) {
1449 /* on getting an address, take the 1st match */
1452 /* on deleting an address, do exact match */
1453 in6_len2mask(&mask, 128);
1454 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1455 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1461 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1463 if (ifa->ifa_addr->sa_family != AF_INET6)
1468 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1470 * XXX: this is adhoc, but is necessary to allow
1471 * a user to specify fe80::/64 (not /10) for a
1472 * link-local address.
1474 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1475 candidate.s6_addr16[1] = 0;
1476 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1477 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1478 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1479 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1480 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1484 return EADDRNOTAVAIL;
1487 if (cmd == SIOCGLIFADDR) {
1488 struct sockaddr_in6 *s6;
1490 /* fill in the if_laddrreq structure */
1491 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1492 s6 = (struct sockaddr_in6 *)&iflr->addr;
1493 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1494 s6->sin6_addr.s6_addr16[1] = 0;
1496 in6_addr2scopeid(ifp, &s6->sin6_addr);
1498 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1499 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1500 ia->ia_dstaddr.sin6_len);
1501 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1502 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1503 s6->sin6_addr.s6_addr16[1] = 0;
1505 in6_addr2scopeid(ifp,
1509 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1512 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1515 iflr->flags = ia->ia6_flags; /* XXX */
1519 struct in6_aliasreq ifra;
1521 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1522 bzero(&ifra, sizeof(ifra));
1523 bcopy(iflr->iflr_name, ifra.ifra_name,
1524 sizeof(ifra.ifra_name));
1526 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1527 ia->ia_addr.sin6_len);
1528 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1529 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1530 ia->ia_dstaddr.sin6_len);
1532 bzero(&ifra.ifra_dstaddr,
1533 sizeof(ifra.ifra_dstaddr));
1535 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1536 ia->ia_prefixmask.sin6_len);
1538 ifra.ifra_flags = ia->ia6_flags;
1539 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1545 return EOPNOTSUPP; /* just for safety */
1549 * Initialize an interface's intetnet6 address
1550 * and routing table entry.
1553 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6,
1556 int error = 0, plen, ifacount = 0;
1559 lwkt_serialize_enter(ifp->if_serializer);
1562 * Give the interface a chance to initialize
1563 * if this is its first address,
1564 * and to validate the address if necessary.
1566 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1568 if (ifa->ifa_addr == NULL)
1569 continue; /* just for safety */
1570 if (ifa->ifa_addr->sa_family != AF_INET6)
1575 ia->ia_addr = *sin6;
1577 if (ifacount <= 1 && ifp->if_ioctl &&
1578 (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia,
1579 (struct ucred *)NULL))) {
1580 lwkt_serialize_exit(ifp->if_serializer);
1583 lwkt_serialize_exit(ifp->if_serializer);
1585 ia->ia_ifa.ifa_metric = ifp->if_metric;
1587 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1591 * If the destination address is specified for a point-to-point
1592 * interface, install a route to the destination as an interface
1595 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1596 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1597 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1598 RTF_UP | RTF_HOST)) != 0)
1600 ia->ia_flags |= IFA_ROUTE;
1604 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1606 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1609 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1611 /* set the rtrequest function to create llinfo */
1612 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1613 in6_ifaddloop(&(ia->ia_ifa));
1620 * Add an address to the list of IP6 multicast addresses for a
1624 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
1626 struct in6_multi *in6m;
1627 struct sockaddr_in6 sin6;
1628 struct ifmultiaddr *ifma;
1635 * Call generic routine to add membership or increment
1636 * refcount. It wants addresses in the form of a sockaddr,
1637 * so we build one here (being careful to zero the unused bytes).
1639 bzero(&sin6, sizeof sin6);
1640 sin6.sin6_family = AF_INET6;
1641 sin6.sin6_len = sizeof sin6;
1642 sin6.sin6_addr = *maddr6;
1643 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1650 * If ifma->ifma_protospec is null, then if_addmulti() created
1651 * a new record. Otherwise, we are done.
1653 if (ifma->ifma_protospec != 0) {
1655 return ifma->ifma_protospec;
1658 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1659 at interrupt time? If so, need to fix if_addmulti. XXX */
1660 in6m = (struct in6_multi *)kmalloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1666 bzero(in6m, sizeof *in6m);
1667 in6m->in6m_addr = *maddr6;
1668 in6m->in6m_ifp = ifp;
1669 in6m->in6m_ifma = ifma;
1670 ifma->ifma_protospec = in6m;
1671 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1674 * Let MLD6 know that we have joined a new IP6 multicast
1677 mld6_start_listening(in6m);
1683 * Delete a multicast address record.
1686 in6_delmulti(struct in6_multi *in6m)
1688 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1692 if (ifma->ifma_refcount == 1) {
1694 * No remaining claims to this record; let MLD6 know
1695 * that we are leaving the multicast group.
1697 mld6_stop_listening(in6m);
1698 ifma->ifma_protospec = 0;
1699 LIST_REMOVE(in6m, in6m_entry);
1700 kfree(in6m, M_IPMADDR);
1702 /* XXX - should be separate API for when we have an ifma? */
1703 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1708 * Find an IPv6 interface link-local address specific to an interface.
1711 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1715 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1717 if (ifa->ifa_addr == NULL)
1718 continue; /* just for safety */
1719 if (ifa->ifa_addr->sa_family != AF_INET6)
1721 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1722 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1729 return((struct in6_ifaddr *)ifa);
1734 * find the internet address corresponding to a given interface and address.
1737 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1741 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1743 if (ifa->ifa_addr == NULL)
1744 continue; /* just for safety */
1745 if (ifa->ifa_addr->sa_family != AF_INET6)
1747 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1751 return((struct in6_ifaddr *)ifa);
1755 * Convert IP6 address to printable (loggable) representation.
1757 static char digits[] = "0123456789abcdef";
1758 static int ip6round = 0;
1760 ip6_sprintf(const struct in6_addr *addr)
1762 static char ip6buf[8][48];
1765 const u_short *a = (const u_short *)addr;
1769 ip6round = (ip6round + 1) & 7;
1770 cp = ip6buf[ip6round];
1772 for (i = 0; i < 8; i++) {
1783 if (dcolon == 0 && *(a + 1) == 0) {
1795 d = (const u_char *)a;
1796 *cp++ = digits[*d >> 4];
1797 *cp++ = digits[*d++ & 0xf];
1798 *cp++ = digits[*d >> 4];
1799 *cp++ = digits[*d & 0xf];
1804 return(ip6buf[ip6round]);
1808 in6_localaddr(struct in6_addr *in6)
1810 struct in6_ifaddr *ia;
1812 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1815 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1816 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1817 &ia->ia_prefixmask.sin6_addr))
1824 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1826 struct in6_ifaddr *ia;
1828 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1829 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1831 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1832 return(1); /* true */
1834 /* XXX: do we still have to go thru the rest of the list? */
1837 return(0); /* false */
1841 * return length of part which dst and src are equal
1845 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1848 u_char *s = (u_char *)src, *d = (u_char *)dst;
1849 u_char *lim = s + 16, r;
1852 if ((r = (*d++ ^ *s++)) != 0) {
1863 /* XXX: to be scope conscious */
1865 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1867 int bytelen, bitlen;
1870 if (0 > len || len > 128) {
1871 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1879 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1881 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1882 p2->s6_addr[bytelen] >> (8 - bitlen))
1889 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1891 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1892 int bytelen, bitlen, i;
1895 if (0 > len || len > 128) {
1896 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1901 bzero(maskp, sizeof(*maskp));
1904 for (i = 0; i < bytelen; i++)
1905 maskp->s6_addr[i] = 0xff;
1907 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1911 * return the best address out of the same scope
1914 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst)
1916 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
1920 struct in6_ifaddr *ifa_best = NULL;
1924 printf("in6_ifawithscope: output interface is not specified\n");
1930 * We search for all addresses on all interfaces from the beginning.
1931 * Comparing an interface with the outgoing interface will be done
1932 * only at the final stage of tiebreaking.
1934 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1937 * We can never take an address that breaks the scope zone
1938 * of the destination.
1940 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
1943 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1945 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
1947 if (ifa->ifa_addr->sa_family != AF_INET6)
1950 src_scope = in6_addrscope(IFA_IN6(ifa));
1953 * Don't use an address before completing DAD
1954 * nor a duplicated address.
1956 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1960 /* XXX: is there any case to allow anycasts? */
1961 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1965 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1970 * If this is the first address we find,
1973 if (ifa_best == NULL)
1977 * ifa_best is never NULL beyond this line except
1978 * within the block labeled "replace".
1982 * If ifa_best has a smaller scope than dst and
1983 * the current address has a larger one than
1984 * (or equal to) dst, always replace ifa_best.
1985 * Also, if the current address has a smaller scope
1986 * than dst, ignore it unless ifa_best also has a
1988 * Consequently, after the two if-clause below,
1989 * the followings must be satisfied:
1990 * (scope(src) < scope(dst) &&
1991 * scope(best) < scope(dst))
1993 * (scope(best) >= scope(dst) &&
1994 * scope(src) >= scope(dst))
1996 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
1997 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
1998 goto replace; /* (A) */
1999 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2000 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2004 * A deprecated address SHOULD NOT be used in new
2005 * communications if an alternate (non-deprecated)
2006 * address is available and has sufficient scope.
2007 * RFC 2462, Section 5.5.4.
2009 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2010 IN6_IFF_DEPRECATED) {
2012 * Ignore any deprecated addresses if
2013 * specified by configuration.
2015 if (!ip6_use_deprecated)
2019 * If we have already found a non-deprecated
2020 * candidate, just ignore deprecated addresses.
2022 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2028 * A non-deprecated address is always preferred
2029 * to a deprecated one regardless of scopes and
2030 * address matching (Note invariants ensured by the
2031 * conditions (A) and (B) above.)
2033 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2034 (((struct in6_ifaddr *)ifa)->ia6_flags &
2035 IN6_IFF_DEPRECATED) == 0)
2039 * When we use temporary addresses described in
2040 * RFC 3041, we prefer temporary addresses to
2041 * public autoconf addresses. Again, note the
2042 * invariants from (A) and (B). Also note that we
2043 * don't have any preference between static addresses
2044 * and autoconf addresses (despite of whether or not
2045 * the latter is temporary or public.)
2047 if (ip6_use_tempaddr) {
2048 struct in6_ifaddr *ifat;
2050 ifat = (struct in6_ifaddr *)ifa;
2051 if ((ifa_best->ia6_flags &
2052 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2053 == IN6_IFF_AUTOCONF &&
2055 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2056 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2059 if ((ifa_best->ia6_flags &
2060 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2061 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2063 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2064 == IN6_IFF_AUTOCONF) {
2070 * At this point, we have two cases:
2071 * 1. we are looking at a non-deprecated address,
2072 * and ifa_best is also non-deprecated.
2073 * 2. we are looking at a deprecated address,
2074 * and ifa_best is also deprecated.
2075 * Also, we do not have to consider a case where
2076 * the scope of if_best is larger(smaller) than dst and
2077 * the scope of the current address is smaller(larger)
2078 * than dst. Such a case has already been covered.
2079 * Tiebreaking is done according to the following
2081 * - the scope comparison between the address and
2083 * - the scope comparison between the address and
2084 * ifa_best (bscopecmp)
2085 * - if the address match dst longer than ifa_best
2087 * - if the address is on the outgoing I/F (outI/F)
2089 * Roughly speaking, the selection policy is
2090 * - the most important item is scope. The same scope
2091 * is best. Then search for a larger scope.
2092 * Smaller scopes are the last resort.
2093 * - A deprecated address is chosen only when we have
2094 * no address that has an enough scope, but is
2095 * prefered to any addresses of smaller scopes
2096 * (this must be already done above.)
2097 * - addresses on the outgoing I/F are preferred to
2098 * ones on other interfaces if none of above
2099 * tiebreaks. In the table below, the column "bI"
2100 * means if the best_ifa is on the outgoing
2101 * interface, and the column "sI" means if the ifa
2102 * is on the outgoing interface.
2103 * - If there is no other reasons to choose one,
2104 * longest address match against dst is considered.
2106 * The precise decision table is as follows:
2107 * dscopecmp bscopecmp match bI oI | replace?
2108 * N/A equal N/A Y N | No (1)
2109 * N/A equal N/A N Y | Yes (2)
2110 * N/A equal larger N/A | Yes (3)
2111 * N/A equal !larger N/A | No (4)
2112 * larger larger N/A N/A | No (5)
2113 * larger smaller N/A N/A | Yes (6)
2114 * smaller larger N/A N/A | Yes (7)
2115 * smaller smaller N/A N/A | No (8)
2116 * equal smaller N/A N/A | Yes (9)
2117 * equal larger (already done at A above)
2119 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2120 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2122 if (bscopecmp == 0) {
2123 struct ifnet *bifp = ifa_best->ia_ifp;
2125 if (bifp == oifp && ifp != oifp) /* (1) */
2127 if (bifp != oifp && ifp == oifp) /* (2) */
2131 * Both bifp and ifp are on the outgoing
2132 * interface, or both two are on a different
2133 * interface from the outgoing I/F.
2134 * now we need address matching against dst
2137 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2138 matchcmp = tlen - blen;
2139 if (matchcmp > 0) /* (3) */
2143 if (dscopecmp > 0) {
2144 if (bscopecmp > 0) /* (5) */
2146 goto replace; /* (6) */
2148 if (dscopecmp < 0) {
2149 if (bscopecmp > 0) /* (7) */
2154 /* now dscopecmp must be 0 */
2156 goto replace; /* (9) */
2159 ifa_best = (struct in6_ifaddr *)ifa;
2160 blen = tlen >= 0 ? tlen :
2161 in6_matchlen(IFA_IN6(ifa), dst);
2162 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2166 /* count statistics for future improvements */
2167 if (ifa_best == NULL)
2168 ip6stat.ip6s_sources_none++;
2170 if (oifp == ifa_best->ia_ifp)
2171 ip6stat.ip6s_sources_sameif[best_scope]++;
2173 ip6stat.ip6s_sources_otherif[best_scope]++;
2175 if (best_scope == dst_scope)
2176 ip6stat.ip6s_sources_samescope[best_scope]++;
2178 ip6stat.ip6s_sources_otherscope[best_scope]++;
2180 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2181 ip6stat.ip6s_sources_deprecated[best_scope]++;
2188 * return the best address out of the same scope. if no address was
2189 * found, return the first valid address from designated IF.
2192 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2194 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2196 struct in6_ifaddr *besta = 0;
2197 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2199 dep[0] = dep[1] = NULL;
2202 * We first look for addresses in the same scope.
2203 * If there is one, return it.
2204 * If two or more, return one which matches the dst longest.
2205 * If none, return one of global addresses assigned other ifs.
2207 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2209 if (ifa->ifa_addr->sa_family != AF_INET6)
2211 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2212 continue; /* XXX: is there any case to allow anycast? */
2213 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2214 continue; /* don't use this interface */
2215 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2217 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2218 if (ip6_use_deprecated)
2219 dep[0] = (struct in6_ifaddr *)ifa;
2223 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2225 * call in6_matchlen() as few as possible
2229 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2230 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2233 besta = (struct in6_ifaddr *)ifa;
2236 besta = (struct in6_ifaddr *)ifa;
2242 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2244 if (ifa->ifa_addr->sa_family != AF_INET6)
2246 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2247 continue; /* XXX: is there any case to allow anycast? */
2248 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2249 continue; /* don't use this interface */
2250 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2252 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2253 if (ip6_use_deprecated)
2254 dep[1] = (struct in6_ifaddr *)ifa;
2258 return (struct in6_ifaddr *)ifa;
2261 /* use the last-resort values, that are, deprecated addresses */
2271 * perform DAD when interface becomes IFF_UP.
2274 in6_if_up(struct ifnet *ifp)
2277 struct in6_ifaddr *ia;
2278 int dad_delay; /* delay ticks before DAD output */
2281 * special cases, like 6to4, are handled in in6_ifattach
2283 in6_ifattach(ifp, NULL);
2286 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2288 if (ifa->ifa_addr->sa_family != AF_INET6)
2290 ia = (struct in6_ifaddr *)ifa;
2291 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2292 nd6_dad_start(ifa, &dad_delay);
2297 in6if_do_dad(struct ifnet *ifp)
2299 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2302 switch (ifp->if_type) {
2308 * These interfaces do not have the IFF_LOOPBACK flag,
2309 * but loop packets back. We do not have to do DAD on such
2310 * interfaces. We should even omit it, because loop-backed
2311 * NS would confuse the DAD procedure.
2316 * Our DAD routine requires the interface up and running.
2317 * However, some interfaces can be up before the RUNNING
2318 * status. Additionaly, users may try to assign addresses
2319 * before the interface becomes up (or running).
2320 * We simply skip DAD in such a case as a work around.
2321 * XXX: we should rather mark "tentative" on such addresses,
2322 * and do DAD after the interface becomes ready.
2324 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2325 (IFF_UP|IFF_RUNNING))
2333 * Calculate max IPv6 MTU through all the interfaces and store it
2339 unsigned long maxmtu = 0;
2342 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2344 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2345 ND_IFINFO(ifp)->linkmtu > maxmtu)
2346 maxmtu = ND_IFINFO(ifp)->linkmtu;
2348 if (maxmtu) /* update only when maxmtu is positive */
2349 in6_maxmtu = maxmtu;
2353 in6_domifattach(struct ifnet *ifp)
2355 struct in6_ifextra *ext;
2357 ext = (struct in6_ifextra *)kmalloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2358 bzero(ext, sizeof(*ext));
2360 ext->in6_ifstat = (struct in6_ifstat *)kmalloc(sizeof(struct in6_ifstat),
2361 M_IFADDR, M_WAITOK);
2362 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2365 (struct icmp6_ifstat *)kmalloc(sizeof(struct icmp6_ifstat),
2366 M_IFADDR, M_WAITOK);
2367 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2369 ext->nd_ifinfo = nd6_ifattach(ifp);
2370 ext->scope6_id = scope6_ifattach(ifp);
2375 in6_domifdetach(struct ifnet *ifp, void *aux)
2377 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2378 scope6_ifdetach(ext->scope6_id);
2379 nd6_ifdetach(ext->nd_ifinfo);
2380 kfree(ext->in6_ifstat, M_IFADDR);
2381 kfree(ext->icmp6_ifstat, M_IFADDR);
2382 kfree(ext, M_IFADDR);
2386 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2387 * v4 mapped addr or v4 compat addr
2390 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2392 bzero(sin, sizeof(*sin));
2393 sin->sin_len = sizeof(struct sockaddr_in);
2394 sin->sin_family = AF_INET;
2395 sin->sin_port = sin6->sin6_port;
2396 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2399 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2401 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2403 bzero(sin6, sizeof(*sin6));
2404 sin6->sin6_len = sizeof(struct sockaddr_in6);
2405 sin6->sin6_family = AF_INET6;
2406 sin6->sin6_port = sin->sin_port;
2407 sin6->sin6_addr.s6_addr32[0] = 0;
2408 sin6->sin6_addr.s6_addr32[1] = 0;
2409 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2410 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2413 /* Convert sockaddr_in6 into sockaddr_in. */
2415 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2417 struct sockaddr_in *sin_p;
2418 struct sockaddr_in6 sin6;
2421 * Save original sockaddr_in6 addr and convert it
2424 sin6 = *(struct sockaddr_in6 *)nam;
2425 sin_p = (struct sockaddr_in *)nam;
2426 in6_sin6_2_sin(sin_p, &sin6);
2429 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2431 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2433 struct sockaddr_in *sin_p;
2434 struct sockaddr_in6 *sin6_p;
2436 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2438 sin_p = (struct sockaddr_in *)*nam;
2439 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2440 FREE(*nam, M_SONAME);
2441 *nam = (struct sockaddr *)sin6_p;