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.13 2005/02/01 16:09:37 hrs 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>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/in_pcb.h>
96 #include <netinet/ip6.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet6/nd6.h>
99 #include <netinet6/mld6_var.h>
100 #include <netinet6/ip6_mroute.h>
101 #include <netinet6/in6_ifattach.h>
102 #include <netinet6/scope6_var.h>
103 #include <netinet6/in6_pcb.h>
105 #include <net/net_osdep.h>
107 MALLOC_DEFINE(M_IPMADDR, "in6_multi", "internet multicast address");
110 * Definitions of some costant IP6 addresses.
112 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
113 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
114 const struct in6_addr in6addr_nodelocal_allnodes =
115 IN6ADDR_NODELOCAL_ALLNODES_INIT;
116 const struct in6_addr in6addr_linklocal_allnodes =
117 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
118 const struct in6_addr in6addr_linklocal_allrouters =
119 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
121 const struct in6_addr in6mask0 = IN6MASK0;
122 const struct in6_addr in6mask32 = IN6MASK32;
123 const struct in6_addr in6mask64 = IN6MASK64;
124 const struct in6_addr in6mask96 = IN6MASK96;
125 const struct in6_addr in6mask128 = IN6MASK128;
127 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
128 0, 0, IN6ADDR_ANY_INIT, 0};
130 static int in6_lifaddr_ioctl (struct socket *, u_long, caddr_t,
131 struct ifnet *, struct thread *);
132 static int in6_ifinit (struct ifnet *, struct in6_ifaddr *,
133 struct sockaddr_in6 *, int);
134 static void in6_unlink_ifa (struct in6_ifaddr *, struct ifnet *);
136 struct in6_multihead in6_multihead; /* XXX BSS initialization */
138 int (*faithprefix_p)(struct in6_addr *);
141 * Subroutine for in6_ifaddloop() and in6_ifremloop().
142 * This routine does actual work.
145 in6_ifloop_request(int cmd, struct ifaddr *ifa)
147 struct sockaddr_in6 all1_sa;
148 struct rtentry *nrt = NULL;
151 bzero(&all1_sa, sizeof(all1_sa));
152 all1_sa.sin6_family = AF_INET6;
153 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
154 all1_sa.sin6_addr = in6mask128;
157 * We specify the address itself as the gateway, and set the
158 * RTF_LLINFO flag, so that the corresponding host route would have
159 * the flag, and thus applications that assume traditional behavior
160 * would be happy. Note that we assume the caller of the function
161 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
162 * which changes the outgoing interface to the loopback interface.
164 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
165 (struct sockaddr *)&all1_sa,
166 RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
168 log(LOG_ERR, "in6_ifloop_request: "
169 "%s operation failed for %s (errno=%d)\n",
170 cmd == RTM_ADD ? "ADD" : "DELETE",
171 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
176 * Make sure rt_ifa be equal to IFA, the second argument of the
178 * We need this because when we refer to rt_ifa->ia6_flags in
179 * ip6_input, we assume that the rt_ifa points to the address instead
180 * of the loopback address.
182 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
183 IFAFREE(nrt->rt_ifa);
189 * Report the addition/removal of the address to the routing socket.
190 * XXX: since we called rtinit for a p2p interface with a destination,
191 * we end up reporting twice in such a case. Should we rather
192 * omit the second report?
195 rt_newaddrmsg(cmd, ifa, e, nrt);
196 if (cmd == RTM_DELETE) {
197 if (nrt->rt_refcnt <= 0) {
198 /* XXX: we should free the entry ourselves. */
203 /* the cmd must be RTM_ADD here */
210 * Add ownaddr as loopback rtentry. We previously add the route only if
211 * necessary (ex. on a p2p link). However, since we now manage addresses
212 * separately from prefixes, we should always add the route. We can't
213 * rely on the cloning mechanism from the corresponding interface route
217 in6_ifaddloop(struct ifaddr *ifa)
221 /* If there is no loopback entry, allocate one. */
222 rt = rtpurelookup(ifa->ifa_addr);
223 if (rt == NULL || !(rt->rt_flags & RTF_HOST) ||
224 !(rt->rt_ifp->if_flags & IFF_LOOPBACK))
225 in6_ifloop_request(RTM_ADD, ifa);
231 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
235 in6_ifremloop(struct ifaddr *ifa)
237 struct in6_ifaddr *ia;
242 * Some of BSD variants do not remove cloned routes
243 * from an interface direct route, when removing the direct route
244 * (see comments in net/net_osdep.h). Even for variants that do remove
245 * cloned routes, they could fail to remove the cloned routes when
246 * we handle multple addresses that share a common prefix.
247 * So, we should remove the route corresponding to the deleted address
248 * regardless of the result of in6_is_ifloop_auto().
252 * Delete the entry only if exact one ifa exists. More than one ifa
253 * can exist if we assign a same single address to multiple
254 * (probably p2p) interfaces.
255 * XXX: we should avoid such a configuration in IPv6...
257 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
258 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
267 * Before deleting, check if a corresponding loopbacked host
268 * route surely exists. With this check, we can avoid to
269 * delete an interface direct route whose destination is same
270 * as the address being removed. This can happen when remofing
271 * a subnet-router anycast address on an interface attahced
272 * to a shared medium.
274 rt = rtpurelookup(ifa->ifa_addr);
275 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
276 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
278 in6_ifloop_request(RTM_DELETE, ifa);
284 in6_ifindex2scopeid(int idx)
288 struct sockaddr_in6 *sin6;
290 if (idx < 0 || if_index < idx)
292 ifp = ifindex2ifnet[idx];
294 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
296 if (ifa->ifa_addr->sa_family != AF_INET6)
298 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
299 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
300 return sin6->sin6_scope_id & 0xffff;
307 in6_mask2len(struct in6_addr *mask, u_char *lim0)
310 u_char *lim = lim0, *p;
313 lim0 - (u_char *)mask > sizeof(*mask)) /* ignore the scope_id part */
314 lim = (u_char *)mask + sizeof(*mask);
315 for (p = (u_char *)mask; p < lim; x++, p++) {
321 for (y = 0; y < 8; y++) {
322 if ((*p & (0x80 >> y)) == 0)
328 * when the limit pointer is given, do a stricter check on the
332 if (y != 0 && (*p & (0x00ff >> y)) != 0)
334 for (p = p + 1; p < lim; p++)
343 in6_len2mask(struct in6_addr *mask, int len)
347 bzero(mask, sizeof(*mask));
348 for (i = 0; i < len / 8; i++)
349 mask->s6_addr8[i] = 0xff;
351 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
354 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
355 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
358 in6_control(struct socket *so, u_long cmd, caddr_t data,
359 struct ifnet *ifp, struct thread *td)
361 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
362 struct in6_ifaddr *ia = NULL;
363 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
371 case SIOCGETSGCNT_IN6:
372 case SIOCGETMIFCNT_IN6:
373 return (mrt6_ioctl(cmd, data));
380 case SIOCSNDFLUSH_IN6:
381 case SIOCSPFXFLUSH_IN6:
382 case SIOCSRTRFLUSH_IN6:
383 case SIOCSDEFIFACE_IN6:
384 case SIOCSIFINFO_FLAGS:
388 case OSIOCGIFINFO_IN6:
389 case SIOCGIFINFO_IN6:
392 case SIOCGNBRINFO_IN6:
393 case SIOCGDEFIFACE_IN6:
394 return(nd6_ioctl(cmd, data, ifp));
398 case SIOCSIFPREFIX_IN6:
399 case SIOCDIFPREFIX_IN6:
400 case SIOCAIFPREFIX_IN6:
401 case SIOCCIFPREFIX_IN6:
402 case SIOCSGIFPREFIX_IN6:
403 case SIOCGIFPREFIX_IN6:
405 "prefix ioctls are now invalidated. "
406 "please use ifconfig.\n");
414 return(scope6_set(ifp,
415 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
418 return(scope6_get(ifp,
419 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
422 return(scope6_get_default((struct scope6_id *)
423 ifr->ifr_ifru.ifru_scope_id));
434 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
438 * Find address for this interface, if it exists.
440 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
441 struct sockaddr_in6 *sa6 =
442 (struct sockaddr_in6 *)&ifra->ifra_addr;
444 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
445 if (sa6->sin6_addr.s6_addr16[1] == 0) {
446 /* link ID is not embedded by the user */
447 sa6->sin6_addr.s6_addr16[1] =
448 htons(ifp->if_index);
449 } else if (sa6->sin6_addr.s6_addr16[1] !=
450 htons(ifp->if_index)) {
451 return(EINVAL); /* link ID contradicts */
453 if (sa6->sin6_scope_id) {
454 if (sa6->sin6_scope_id !=
455 (u_int32_t)ifp->if_index)
457 sa6->sin6_scope_id = 0; /* XXX: good way? */
460 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
464 case SIOCSIFADDR_IN6:
465 case SIOCSIFDSTADDR_IN6:
466 case SIOCSIFNETMASK_IN6:
468 * Since IPv6 allows a node to assign multiple addresses
469 * on a single interface, SIOCSIFxxx ioctls are not suitable
470 * and should be unused.
472 /* we decided to obsolete this command (20000704) */
475 case SIOCDIFADDR_IN6:
477 * for IPv4, we look for existing in_ifaddr here to allow
478 * "ifconfig if0 delete" to remove first IPv4 address on the
479 * interface. For IPv6, as the spec allow multiple interface
480 * address from the day one, we consider "remove the first one"
481 * semantics to be not preferable.
484 return(EADDRNOTAVAIL);
486 case SIOCAIFADDR_IN6:
488 * We always require users to specify a valid IPv6 address for
489 * the corresponding operation.
491 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
492 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
493 return(EAFNOSUPPORT);
499 case SIOCGIFADDR_IN6:
500 /* This interface is basically deprecated. use SIOCGIFCONF. */
502 case SIOCGIFAFLAG_IN6:
503 case SIOCGIFNETMASK_IN6:
504 case SIOCGIFDSTADDR_IN6:
505 case SIOCGIFALIFETIME_IN6:
506 /* must think again about its semantics */
508 return(EADDRNOTAVAIL);
510 case SIOCSIFALIFETIME_IN6:
512 struct in6_addrlifetime *lt;
517 return(EADDRNOTAVAIL);
518 /* sanity for overflow - beware unsigned */
519 lt = &ifr->ifr_ifru.ifru_lifetime;
520 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
521 && lt->ia6t_vltime + time_second < time_second) {
524 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
525 && lt->ia6t_pltime + time_second < time_second) {
534 case SIOCGIFADDR_IN6:
535 ifr->ifr_addr = ia->ia_addr;
538 case SIOCGIFDSTADDR_IN6:
539 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
542 * XXX: should we check if ifa_dstaddr is NULL and return
545 ifr->ifr_dstaddr = ia->ia_dstaddr;
548 case SIOCGIFNETMASK_IN6:
549 ifr->ifr_addr = ia->ia_prefixmask;
552 case SIOCGIFAFLAG_IN6:
553 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
556 case SIOCGIFSTAT_IN6:
559 bzero(&ifr->ifr_ifru.ifru_stat,
560 sizeof(ifr->ifr_ifru.ifru_stat));
561 ifr->ifr_ifru.ifru_stat =
562 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
565 case SIOCGIFSTAT_ICMP6:
566 bzero(&ifr->ifr_ifru.ifru_stat,
567 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
568 ifr->ifr_ifru.ifru_icmp6stat =
569 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
572 case SIOCGIFALIFETIME_IN6:
573 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
576 case SIOCSIFALIFETIME_IN6:
577 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
579 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
580 ia->ia6_lifetime.ia6t_expire =
581 time_second + ia->ia6_lifetime.ia6t_vltime;
583 ia->ia6_lifetime.ia6t_expire = 0;
584 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
585 ia->ia6_lifetime.ia6t_preferred =
586 time_second + ia->ia6_lifetime.ia6t_pltime;
588 ia->ia6_lifetime.ia6t_preferred = 0;
591 case SIOCAIFADDR_IN6:
594 struct nd_prefix pr0, *pr;
597 * first, make or update the interface address structure,
598 * and link it to the list.
600 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
604 * then, make the prefix on-link on the interface.
605 * XXX: we'd rather create the prefix before the address, but
606 * we need at least one address to install the corresponding
607 * interface route, so we configure the address first.
611 * convert mask to prefix length (prefixmask has already
612 * been validated in in6_update_ifa().
614 bzero(&pr0, sizeof(pr0));
616 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
618 if (pr0.ndpr_plen == 128)
619 break; /* we don't need to install a host route. */
620 pr0.ndpr_prefix = ifra->ifra_addr;
621 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
622 /* apply the mask for safety. */
623 for (i = 0; i < 4; i++) {
624 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
625 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
628 * XXX: since we don't have an API to set prefix (not address)
629 * lifetimes, we just use the same lifetimes as addresses.
630 * The (temporarily) installed lifetimes can be overridden by
631 * later advertised RAs (when accept_rtadv is non 0), which is
632 * an intended behavior.
634 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
636 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
637 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
638 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
640 /* add the prefix if there's one. */
641 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
643 * nd6_prelist_add will install the corresponding
646 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
649 log(LOG_ERR, "nd6_prelist_add succedded but "
651 return(EINVAL); /* XXX panic here? */
654 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
656 /* XXX: this should not happen! */
657 log(LOG_ERR, "in6_control: addition succeeded, but"
660 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
661 ia->ia6_ndpr == NULL) { /* new autoconfed addr */
666 * If this is the first autoconf address from
667 * the prefix, create a temporary address
668 * as well (when specified).
670 if (ip6_use_tempaddr &&
671 pr->ndpr_refcnt == 1) {
673 if ((e = in6_tmpifadd(ia, 1)) != 0) {
674 log(LOG_NOTICE, "in6_control: "
675 "failed to create a "
676 "temporary address, "
684 * this might affect the status of autoconfigured
685 * addresses, that is, this address might make
686 * other addresses detached.
688 pfxlist_onlink_check();
690 if (error == 0 && ia)
691 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
695 case SIOCDIFADDR_IN6:
698 struct nd_prefix pr0, *pr;
701 * If the address being deleted is the only one that owns
702 * the corresponding prefix, expire the prefix as well.
703 * XXX: theoretically, we don't have to warry about such
704 * relationship, since we separate the address management
705 * and the prefix management. We do this, however, to provide
706 * as much backward compatibility as possible in terms of
707 * the ioctl operation.
709 bzero(&pr0, sizeof(pr0));
711 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
713 if (pr0.ndpr_plen == 128)
715 pr0.ndpr_prefix = ia->ia_addr;
716 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
717 for (i = 0; i < 4; i++) {
718 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
719 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
722 * The logic of the following condition is a bit complicated.
723 * We expire the prefix when
724 * 1. the address obeys autoconfiguration and it is the
725 * only owner of the associated prefix, or
726 * 2. the address does not obey autoconf and there is no
727 * other owner of the prefix.
729 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
730 (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
731 pr->ndpr_refcnt == 1) ||
732 ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
733 pr->ndpr_refcnt == 0))) {
734 pr->ndpr_expire = 1; /* XXX: just for expiration */
738 in6_purgeaddr(&ia->ia_ifa);
739 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
744 if (ifp == NULL || ifp->if_ioctl == 0)
746 return((*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred));
753 * Update parameters of an IPv6 interface address.
754 * If necessary, a new entry is created and linked into address chains.
755 * This function is separated from in6_control().
756 * XXX: should this be performed under splnet()?
759 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
760 struct in6_ifaddr *ia)
762 int error = 0, hostIsNew = 0, plen = -1;
763 struct in6_ifaddr *oia;
764 struct sockaddr_in6 dst6;
765 struct in6_addrlifetime *lt;
767 /* Validate parameters */
768 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
772 * The destination address for a p2p link must have a family
773 * of AF_UNSPEC or AF_INET6.
775 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
776 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
777 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
778 return(EAFNOSUPPORT);
780 * validate ifra_prefixmask. don't check sin6_family, netmask
781 * does not carry fields other than sin6_len.
783 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
786 * Because the IPv6 address architecture is classless, we require
787 * users to specify a (non 0) prefix length (mask) for a new address.
788 * We also require the prefix (when specified) mask is valid, and thus
789 * reject a non-consecutive mask.
791 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
793 if (ifra->ifra_prefixmask.sin6_len != 0) {
794 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
795 (u_char *)&ifra->ifra_prefixmask +
796 ifra->ifra_prefixmask.sin6_len);
802 * In this case, ia must not be NULL. We just use its prefix
805 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
808 * If the destination address on a p2p interface is specified,
809 * and the address is a scoped one, validate/set the scope
812 dst6 = ifra->ifra_dstaddr;
813 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
814 (dst6.sin6_family == AF_INET6)) {
817 if ((error = in6_recoverscope(&dst6,
818 &ifra->ifra_dstaddr.sin6_addr,
821 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
822 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
823 dst6.sin6_scope_id = scopeid;
824 else if (dst6.sin6_scope_id != scopeid)
825 return(EINVAL); /* scope ID mismatch. */
826 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
829 dst6.sin6_scope_id = 0; /* XXX */
832 * The destination address can be specified only for a p2p or a
833 * loopback interface. If specified, the corresponding prefix length
836 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
837 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
838 /* XXX: noisy message */
839 log(LOG_INFO, "in6_update_ifa: a destination can be "
840 "specified for a p2p or a loopback IF only\n");
845 * The following message seems noisy, but we dare to
846 * add it for diagnosis.
848 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
849 "when dstaddr is specified\n");
853 /* lifetime consistency check */
854 lt = &ifra->ifra_lifetime;
855 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
856 && lt->ia6t_vltime + time_second < time_second) {
859 if (lt->ia6t_vltime == 0) {
861 * the following log might be noisy, but this is a typical
862 * configuration mistake or a tool's bug.
865 "in6_update_ifa: valid lifetime is 0 for %s\n",
866 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
868 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
869 && lt->ia6t_pltime + time_second < time_second) {
874 * If this is a new address, allocate a new ifaddr and link it
880 * When in6_update_ifa() is called in a process of a received
881 * RA, it is called under splnet(). So, we should call malloc
884 ia = (struct in6_ifaddr *)
885 malloc(sizeof(*ia), M_IFADDR, M_NOWAIT);
888 bzero((caddr_t)ia, sizeof(*ia));
889 /* Initialize the address and masks */
890 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
891 ia->ia_addr.sin6_family = AF_INET6;
892 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
893 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
895 * XXX: some functions expect that ifa_dstaddr is not
896 * NULL for p2p interfaces.
898 ia->ia_ifa.ifa_dstaddr
899 = (struct sockaddr *)&ia->ia_dstaddr;
901 ia->ia_ifa.ifa_dstaddr = NULL;
903 ia->ia_ifa.ifa_netmask
904 = (struct sockaddr *)&ia->ia_prefixmask;
907 if ((oia = in6_ifaddr) != NULL) {
908 for ( ; oia->ia_next; oia = oia->ia_next)
914 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
918 /* set prefix mask */
919 if (ifra->ifra_prefixmask.sin6_len) {
921 * We prohibit changing the prefix length of an existing
923 * + such an operation should be rare in IPv6, and
924 * + the operation would confuse prefix management.
926 if (ia->ia_prefixmask.sin6_len &&
927 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
928 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
929 " existing (%s) address should not be changed\n",
930 ip6_sprintf(&ia->ia_addr.sin6_addr));
934 ia->ia_prefixmask = ifra->ifra_prefixmask;
938 * If a new destination address is specified, scrub the old one and
939 * install the new destination. Note that the interface must be
940 * p2p or loopback (see the check above.)
942 if (dst6.sin6_family == AF_INET6 &&
943 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
944 &ia->ia_dstaddr.sin6_addr)) {
947 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
948 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
950 log(LOG_ERR, "in6_update_ifa: failed to remove "
951 "a route to the old destination: %s\n",
952 ip6_sprintf(&ia->ia_addr.sin6_addr));
953 /* proceed anyway... */
956 ia->ia_flags &= ~IFA_ROUTE;
957 ia->ia_dstaddr = dst6;
960 /* reset the interface and routing table appropriately. */
961 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
965 * Beyond this point, we should call in6_purgeaddr upon an error,
966 * not just go to unlink.
969 #if 0 /* disable this mechanism for now */
970 /* update prefix list */
972 (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
975 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
976 if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
977 in6_purgeaddr((struct ifaddr *)ia);
983 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
984 struct sockaddr_in6 mltaddr, mltmask;
985 struct in6_multi *in6m;
989 * join solicited multicast addr for new host id
991 struct in6_addr llsol;
992 bzero(&llsol, sizeof(struct in6_addr));
993 llsol.s6_addr16[0] = htons(0xff02);
994 llsol.s6_addr16[1] = htons(ifp->if_index);
995 llsol.s6_addr32[1] = 0;
996 llsol.s6_addr32[2] = htonl(1);
998 ifra->ifra_addr.sin6_addr.s6_addr32[3];
999 llsol.s6_addr8[12] = 0xff;
1000 (void)in6_addmulti(&llsol, ifp, &error);
1003 "in6_update_ifa: addmulti failed for "
1004 "%s on %s (errno=%d)\n",
1005 ip6_sprintf(&llsol), if_name(ifp),
1007 in6_purgeaddr((struct ifaddr *)ia);
1012 bzero(&mltmask, sizeof(mltmask));
1013 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1014 mltmask.sin6_family = AF_INET6;
1015 mltmask.sin6_addr = in6mask32;
1018 * join link-local all-nodes address
1020 bzero(&mltaddr, sizeof(mltaddr));
1021 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1022 mltaddr.sin6_family = AF_INET6;
1023 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1024 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1026 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1029 (struct sockaddr *)&mltaddr,
1030 (struct sockaddr *)&ia->ia_addr,
1031 (struct sockaddr *)&mltmask,
1032 RTF_UP|RTF_CLONING, /* xxx */
1033 (struct rtentry **)0);
1034 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1037 "in6_update_ifa: addmulti failed for "
1038 "%s on %s (errno=%d)\n",
1039 ip6_sprintf(&mltaddr.sin6_addr),
1040 if_name(ifp), error);
1045 * join node information group address
1047 #define hostnamelen strlen(hostname)
1048 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1050 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1051 if (in6m == NULL && ia != NULL) {
1052 (void)in6_addmulti(&mltaddr.sin6_addr,
1055 log(LOG_WARNING, "in6_update_ifa: "
1056 "addmulti failed for "
1057 "%s on %s (errno=%d)\n",
1058 ip6_sprintf(&mltaddr.sin6_addr),
1059 if_name(ifp), error);
1066 * join node-local all-nodes address, on loopback.
1067 * XXX: since "node-local" is obsoleted by interface-local,
1068 * we have to join the group on every interface with
1069 * some interface-boundary restriction.
1071 if (ifp->if_flags & IFF_LOOPBACK) {
1072 struct in6_ifaddr *ia_loop;
1074 struct in6_addr loop6 = in6addr_loopback;
1075 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1077 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1079 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1080 if (in6m == NULL && ia_loop != NULL) {
1082 (struct sockaddr *)&mltaddr,
1083 (struct sockaddr *)&ia_loop->ia_addr,
1084 (struct sockaddr *)&mltmask,
1086 (struct rtentry **)0);
1087 (void)in6_addmulti(&mltaddr.sin6_addr, ifp,
1090 log(LOG_WARNING, "in6_update_ifa: "
1091 "addmulti failed for %s on %s "
1093 ip6_sprintf(&mltaddr.sin6_addr),
1094 if_name(ifp), error);
1100 ia->ia6_flags = ifra->ifra_flags;
1101 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1102 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1104 ia->ia6_lifetime = ifra->ifra_lifetime;
1106 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1107 ia->ia6_lifetime.ia6t_expire =
1108 time_second + ia->ia6_lifetime.ia6t_vltime;
1110 ia->ia6_lifetime.ia6t_expire = 0;
1111 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1112 ia->ia6_lifetime.ia6t_preferred =
1113 time_second + ia->ia6_lifetime.ia6t_pltime;
1115 ia->ia6_lifetime.ia6t_preferred = 0;
1118 * Perform DAD, if needed.
1119 * XXX It may be of use, if we can administratively
1122 if (in6if_do_dad(ifp) && (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
1123 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1124 nd6_dad_start((struct ifaddr *)ia, NULL);
1131 * XXX: if a change of an existing address failed, keep the entry
1135 in6_unlink_ifa(ia, ifp);
1140 in6_purgeaddr(struct ifaddr *ifa)
1142 struct ifnet *ifp = ifa->ifa_ifp;
1143 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1145 /* stop DAD processing */
1149 * delete route to the destination of the address being purged.
1150 * The interface must be p2p or loopback in this case.
1152 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1155 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1157 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1158 "a route to the p2p destination: %s on %s, "
1160 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1162 /* proceed anyway... */
1165 ia->ia_flags &= ~IFA_ROUTE;
1168 /* Remove ownaddr's loopback rtentry, if it exists. */
1169 in6_ifremloop(&(ia->ia_ifa));
1171 if (ifp->if_flags & IFF_MULTICAST) {
1173 * delete solicited multicast addr for deleting host id
1175 struct in6_multi *in6m;
1176 struct in6_addr llsol;
1177 bzero(&llsol, sizeof(struct in6_addr));
1178 llsol.s6_addr16[0] = htons(0xff02);
1179 llsol.s6_addr16[1] = htons(ifp->if_index);
1180 llsol.s6_addr32[1] = 0;
1181 llsol.s6_addr32[2] = htonl(1);
1182 llsol.s6_addr32[3] =
1183 ia->ia_addr.sin6_addr.s6_addr32[3];
1184 llsol.s6_addr8[12] = 0xff;
1186 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1191 in6_unlink_ifa(ia, ifp);
1195 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1198 struct in6_ifaddr *oia;
1201 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1204 if (oia == (ia = in6_ifaddr))
1205 in6_ifaddr = ia->ia_next;
1207 while (ia->ia_next && (ia->ia_next != oia))
1210 ia->ia_next = oia->ia_next;
1213 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1217 if (oia->ia6_ifpr) { /* check for safety */
1218 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1219 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1220 in6_prefix_remove_ifid(iilen, oia);
1224 * When an autoconfigured address is being removed, release the
1225 * reference to the base prefix. Also, since the release might
1226 * affect the status of other (detached) addresses, call
1227 * pfxlist_onlink_check().
1229 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1230 if (oia->ia6_ndpr == NULL) {
1231 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1232 "%p has no prefix\n", oia);
1234 oia->ia6_ndpr->ndpr_refcnt--;
1235 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1236 oia->ia6_ndpr = NULL;
1239 pfxlist_onlink_check();
1243 * release another refcnt for the link from in6_ifaddr.
1244 * Note that we should decrement the refcnt at least once for all *BSD.
1246 IFAFREE(&oia->ia_ifa);
1252 in6_purgeif(struct ifnet *ifp)
1254 struct ifaddr *ifa, *nifa;
1256 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa)
1258 nifa = TAILQ_NEXT(ifa, ifa_list);
1259 if (ifa->ifa_addr->sa_family != AF_INET6)
1269 * SIOCGLIFADDR: get first address. (?)
1270 * SIOCGLIFADDR with IFLR_PREFIX:
1271 * get first address that matches the specified prefix.
1272 * SIOCALIFADDR: add the specified address.
1273 * SIOCALIFADDR with IFLR_PREFIX:
1274 * add the specified prefix, filling hostid part from
1275 * the first link-local address. prefixlen must be <= 64.
1276 * SIOCDLIFADDR: delete the specified address.
1277 * SIOCDLIFADDR with IFLR_PREFIX:
1278 * delete the first address that matches the specified prefix.
1280 * EINVAL on invalid parameters
1281 * EADDRNOTAVAIL on prefix match failed/specified address not found
1282 * other values may be returned from in6_ioctl()
1284 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1285 * this is to accomodate address naming scheme other than RFC2374,
1287 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1288 * address encoding scheme. (see figure on page 8)
1291 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1292 struct ifnet *ifp, struct thread *td)
1294 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1296 struct sockaddr *sa;
1299 if (!data || !ifp) {
1300 panic("invalid argument to in6_lifaddr_ioctl");
1306 /* address must be specified on GET with IFLR_PREFIX */
1307 if ((iflr->flags & IFLR_PREFIX) == 0)
1312 /* address must be specified on ADD and DELETE */
1313 sa = (struct sockaddr *)&iflr->addr;
1314 if (sa->sa_family != AF_INET6)
1316 if (sa->sa_len != sizeof(struct sockaddr_in6))
1318 /* XXX need improvement */
1319 sa = (struct sockaddr *)&iflr->dstaddr;
1320 if (sa->sa_family && sa->sa_family != AF_INET6)
1322 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1325 default: /* shouldn't happen */
1327 panic("invalid cmd to in6_lifaddr_ioctl");
1333 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1339 struct in6_aliasreq ifra;
1340 struct in6_addr *hostid = NULL;
1343 if ((iflr->flags & IFLR_PREFIX) != 0) {
1344 struct sockaddr_in6 *sin6;
1347 * hostid is to fill in the hostid part of the
1348 * address. hostid points to the first link-local
1349 * address attached to the interface.
1351 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1353 return EADDRNOTAVAIL;
1354 hostid = IFA_IN6(ifa);
1356 /* prefixlen must be <= 64. */
1357 if (64 < iflr->prefixlen)
1359 prefixlen = iflr->prefixlen;
1361 /* hostid part must be zero. */
1362 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1363 if (sin6->sin6_addr.s6_addr32[2] != 0
1364 || sin6->sin6_addr.s6_addr32[3] != 0) {
1368 prefixlen = iflr->prefixlen;
1370 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1371 bzero(&ifra, sizeof(ifra));
1372 bcopy(iflr->iflr_name, ifra.ifra_name,
1373 sizeof(ifra.ifra_name));
1375 bcopy(&iflr->addr, &ifra.ifra_addr,
1376 ((struct sockaddr *)&iflr->addr)->sa_len);
1378 /* fill in hostid part */
1379 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1380 hostid->s6_addr32[2];
1381 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1382 hostid->s6_addr32[3];
1385 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1386 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1387 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1389 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1390 hostid->s6_addr32[2];
1391 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1392 hostid->s6_addr32[3];
1396 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1397 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1399 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1400 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1405 struct in6_ifaddr *ia;
1406 struct in6_addr mask, candidate, match;
1407 struct sockaddr_in6 *sin6;
1410 bzero(&mask, sizeof(mask));
1411 if (iflr->flags & IFLR_PREFIX) {
1412 /* lookup a prefix rather than address. */
1413 in6_len2mask(&mask, iflr->prefixlen);
1415 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1416 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1417 match.s6_addr32[0] &= mask.s6_addr32[0];
1418 match.s6_addr32[1] &= mask.s6_addr32[1];
1419 match.s6_addr32[2] &= mask.s6_addr32[2];
1420 match.s6_addr32[3] &= mask.s6_addr32[3];
1422 /* if you set extra bits, that's wrong */
1423 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1428 if (cmd == SIOCGLIFADDR) {
1429 /* on getting an address, take the 1st match */
1432 /* on deleting an address, do exact match */
1433 in6_len2mask(&mask, 128);
1434 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1435 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1441 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1443 if (ifa->ifa_addr->sa_family != AF_INET6)
1448 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1450 * XXX: this is adhoc, but is necessary to allow
1451 * a user to specify fe80::/64 (not /10) for a
1452 * link-local address.
1454 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1455 candidate.s6_addr16[1] = 0;
1456 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1457 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1458 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1459 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1460 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1464 return EADDRNOTAVAIL;
1467 if (cmd == SIOCGLIFADDR) {
1468 struct sockaddr_in6 *s6;
1470 /* fill in the if_laddrreq structure */
1471 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1472 s6 = (struct sockaddr_in6 *)&iflr->addr;
1473 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1474 s6->sin6_addr.s6_addr16[1] = 0;
1476 in6_addr2scopeid(ifp, &s6->sin6_addr);
1478 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1479 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1480 ia->ia_dstaddr.sin6_len);
1481 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1482 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1483 s6->sin6_addr.s6_addr16[1] = 0;
1485 in6_addr2scopeid(ifp,
1489 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1492 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1495 iflr->flags = ia->ia6_flags; /* XXX */
1499 struct in6_aliasreq ifra;
1501 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1502 bzero(&ifra, sizeof(ifra));
1503 bcopy(iflr->iflr_name, ifra.ifra_name,
1504 sizeof(ifra.ifra_name));
1506 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1507 ia->ia_addr.sin6_len);
1508 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1509 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1510 ia->ia_dstaddr.sin6_len);
1512 bzero(&ifra.ifra_dstaddr,
1513 sizeof(ifra.ifra_dstaddr));
1515 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1516 ia->ia_prefixmask.sin6_len);
1518 ifra.ifra_flags = ia->ia6_flags;
1519 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1525 return EOPNOTSUPP; /* just for safety */
1529 * Initialize an interface's intetnet6 address
1530 * and routing table entry.
1533 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6,
1536 int error = 0, plen, ifacount = 0;
1541 * Give the interface a chance to initialize
1542 * if this is its first address,
1543 * and to validate the address if necessary.
1545 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1547 if (ifa->ifa_addr == NULL)
1548 continue; /* just for safety */
1549 if (ifa->ifa_addr->sa_family != AF_INET6)
1554 ia->ia_addr = *sin6;
1556 if (ifacount <= 1 && ifp->if_ioctl &&
1557 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia,
1558 (struct ucred *)NULL))) {
1564 ia->ia_ifa.ifa_metric = ifp->if_metric;
1566 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1570 * If the destination address is specified for a point-to-point
1571 * interface, install a route to the destination as an interface
1574 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1575 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1576 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1577 RTF_UP | RTF_HOST)) != 0)
1579 ia->ia_flags |= IFA_ROUTE;
1583 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1585 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1588 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1590 /* set the rtrequest function to create llinfo */
1591 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1592 in6_ifaddloop(&(ia->ia_ifa));
1599 * Add an address to the list of IP6 multicast addresses for a
1603 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
1605 struct in6_multi *in6m;
1606 struct sockaddr_in6 sin6;
1607 struct ifmultiaddr *ifma;
1613 * Call generic routine to add membership or increment
1614 * refcount. It wants addresses in the form of a sockaddr,
1615 * so we build one here (being careful to zero the unused bytes).
1617 bzero(&sin6, sizeof sin6);
1618 sin6.sin6_family = AF_INET6;
1619 sin6.sin6_len = sizeof sin6;
1620 sin6.sin6_addr = *maddr6;
1621 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1628 * If ifma->ifma_protospec is null, then if_addmulti() created
1629 * a new record. Otherwise, we are done.
1631 if (ifma->ifma_protospec != 0)
1632 return ifma->ifma_protospec;
1634 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1635 at interrupt time? If so, need to fix if_addmulti. XXX */
1636 in6m = (struct in6_multi *)malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1642 bzero(in6m, sizeof *in6m);
1643 in6m->in6m_addr = *maddr6;
1644 in6m->in6m_ifp = ifp;
1645 in6m->in6m_ifma = ifma;
1646 ifma->ifma_protospec = in6m;
1647 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1650 * Let MLD6 know that we have joined a new IP6 multicast
1653 mld6_start_listening(in6m);
1659 * Delete a multicast address record.
1662 in6_delmulti(struct in6_multi *in6m)
1664 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1667 if (ifma->ifma_refcount == 1) {
1669 * No remaining claims to this record; let MLD6 know
1670 * that we are leaving the multicast group.
1672 mld6_stop_listening(in6m);
1673 ifma->ifma_protospec = 0;
1674 LIST_REMOVE(in6m, in6m_entry);
1675 free(in6m, M_IPMADDR);
1677 /* XXX - should be separate API for when we have an ifma? */
1678 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1683 * Find an IPv6 interface link-local address specific to an interface.
1686 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1690 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1692 if (ifa->ifa_addr == NULL)
1693 continue; /* just for safety */
1694 if (ifa->ifa_addr->sa_family != AF_INET6)
1696 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1697 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1704 return((struct in6_ifaddr *)ifa);
1709 * find the internet address corresponding to a given interface and address.
1712 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
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_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1726 return((struct in6_ifaddr *)ifa);
1730 * Convert IP6 address to printable (loggable) representation.
1732 static char digits[] = "0123456789abcdef";
1733 static int ip6round = 0;
1735 ip6_sprintf(const struct in6_addr *addr)
1737 static char ip6buf[8][48];
1740 const u_short *a = (const u_short *)addr;
1744 ip6round = (ip6round + 1) & 7;
1745 cp = ip6buf[ip6round];
1747 for (i = 0; i < 8; i++) {
1758 if (dcolon == 0 && *(a + 1) == 0) {
1770 d = (const u_char *)a;
1771 *cp++ = digits[*d >> 4];
1772 *cp++ = digits[*d++ & 0xf];
1773 *cp++ = digits[*d >> 4];
1774 *cp++ = digits[*d & 0xf];
1779 return(ip6buf[ip6round]);
1783 in6_localaddr(struct in6_addr *in6)
1785 struct in6_ifaddr *ia;
1787 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1790 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1791 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1792 &ia->ia_prefixmask.sin6_addr))
1799 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1801 struct in6_ifaddr *ia;
1803 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1804 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1806 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1807 return(1); /* true */
1809 /* XXX: do we still have to go thru the rest of the list? */
1812 return(0); /* false */
1816 * return length of part which dst and src are equal
1820 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1823 u_char *s = (u_char *)src, *d = (u_char *)dst;
1824 u_char *lim = s + 16, r;
1827 if ((r = (*d++ ^ *s++)) != 0) {
1838 /* XXX: to be scope conscious */
1840 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1842 int bytelen, bitlen;
1845 if (0 > len || len > 128) {
1846 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1854 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1856 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1857 p2->s6_addr[bytelen] >> (8 - bitlen))
1864 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1866 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1867 int bytelen, bitlen, i;
1870 if (0 > len || len > 128) {
1871 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1876 bzero(maskp, sizeof(*maskp));
1879 for (i = 0; i < bytelen; i++)
1880 maskp->s6_addr[i] = 0xff;
1882 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1886 * return the best address out of the same scope
1889 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst)
1891 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
1895 struct in6_ifaddr *ifa_best = NULL;
1899 printf("in6_ifawithscope: output interface is not specified\n");
1905 * We search for all addresses on all interfaces from the beginning.
1906 * Comparing an interface with the outgoing interface will be done
1907 * only at the final stage of tiebreaking.
1909 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1912 * We can never take an address that breaks the scope zone
1913 * of the destination.
1915 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
1918 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1920 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
1922 if (ifa->ifa_addr->sa_family != AF_INET6)
1925 src_scope = in6_addrscope(IFA_IN6(ifa));
1928 * Don't use an address before completing DAD
1929 * nor a duplicated address.
1931 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1935 /* XXX: is there any case to allow anycasts? */
1936 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1940 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1945 * If this is the first address we find,
1948 if (ifa_best == NULL)
1952 * ifa_best is never NULL beyond this line except
1953 * within the block labeled "replace".
1957 * If ifa_best has a smaller scope than dst and
1958 * the current address has a larger one than
1959 * (or equal to) dst, always replace ifa_best.
1960 * Also, if the current address has a smaller scope
1961 * than dst, ignore it unless ifa_best also has a
1963 * Consequently, after the two if-clause below,
1964 * the followings must be satisfied:
1965 * (scope(src) < scope(dst) &&
1966 * scope(best) < scope(dst))
1968 * (scope(best) >= scope(dst) &&
1969 * scope(src) >= scope(dst))
1971 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
1972 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
1973 goto replace; /* (A) */
1974 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
1975 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
1979 * A deprecated address SHOULD NOT be used in new
1980 * communications if an alternate (non-deprecated)
1981 * address is available and has sufficient scope.
1982 * RFC 2462, Section 5.5.4.
1984 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1985 IN6_IFF_DEPRECATED) {
1987 * Ignore any deprecated addresses if
1988 * specified by configuration.
1990 if (!ip6_use_deprecated)
1994 * If we have already found a non-deprecated
1995 * candidate, just ignore deprecated addresses.
1997 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2003 * A non-deprecated address is always preferred
2004 * to a deprecated one regardless of scopes and
2005 * address matching (Note invariants ensured by the
2006 * conditions (A) and (B) above.)
2008 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2009 (((struct in6_ifaddr *)ifa)->ia6_flags &
2010 IN6_IFF_DEPRECATED) == 0)
2014 * When we use temporary addresses described in
2015 * RFC 3041, we prefer temporary addresses to
2016 * public autoconf addresses. Again, note the
2017 * invariants from (A) and (B). Also note that we
2018 * don't have any preference between static addresses
2019 * and autoconf addresses (despite of whether or not
2020 * the latter is temporary or public.)
2022 if (ip6_use_tempaddr) {
2023 struct in6_ifaddr *ifat;
2025 ifat = (struct in6_ifaddr *)ifa;
2026 if ((ifa_best->ia6_flags &
2027 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2028 == IN6_IFF_AUTOCONF &&
2030 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2031 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2034 if ((ifa_best->ia6_flags &
2035 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2036 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2038 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2039 == IN6_IFF_AUTOCONF) {
2045 * At this point, we have two cases:
2046 * 1. we are looking at a non-deprecated address,
2047 * and ifa_best is also non-deprecated.
2048 * 2. we are looking at a deprecated address,
2049 * and ifa_best is also deprecated.
2050 * Also, we do not have to consider a case where
2051 * the scope of if_best is larger(smaller) than dst and
2052 * the scope of the current address is smaller(larger)
2053 * than dst. Such a case has already been covered.
2054 * Tiebreaking is done according to the following
2056 * - the scope comparison between the address and
2058 * - the scope comparison between the address and
2059 * ifa_best (bscopecmp)
2060 * - if the address match dst longer than ifa_best
2062 * - if the address is on the outgoing I/F (outI/F)
2064 * Roughly speaking, the selection policy is
2065 * - the most important item is scope. The same scope
2066 * is best. Then search for a larger scope.
2067 * Smaller scopes are the last resort.
2068 * - A deprecated address is chosen only when we have
2069 * no address that has an enough scope, but is
2070 * prefered to any addresses of smaller scopes
2071 * (this must be already done above.)
2072 * - addresses on the outgoing I/F are preferred to
2073 * ones on other interfaces if none of above
2074 * tiebreaks. In the table below, the column "bI"
2075 * means if the best_ifa is on the outgoing
2076 * interface, and the column "sI" means if the ifa
2077 * is on the outgoing interface.
2078 * - If there is no other reasons to choose one,
2079 * longest address match against dst is considered.
2081 * The precise decision table is as follows:
2082 * dscopecmp bscopecmp match bI oI | replace?
2083 * N/A equal N/A Y N | No (1)
2084 * N/A equal N/A N Y | Yes (2)
2085 * N/A equal larger N/A | Yes (3)
2086 * N/A equal !larger N/A | No (4)
2087 * larger larger N/A N/A | No (5)
2088 * larger smaller N/A N/A | Yes (6)
2089 * smaller larger N/A N/A | Yes (7)
2090 * smaller smaller N/A N/A | No (8)
2091 * equal smaller N/A N/A | Yes (9)
2092 * equal larger (already done at A above)
2094 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2095 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2097 if (bscopecmp == 0) {
2098 struct ifnet *bifp = ifa_best->ia_ifp;
2100 if (bifp == oifp && ifp != oifp) /* (1) */
2102 if (bifp != oifp && ifp == oifp) /* (2) */
2106 * Both bifp and ifp are on the outgoing
2107 * interface, or both two are on a different
2108 * interface from the outgoing I/F.
2109 * now we need address matching against dst
2112 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2113 matchcmp = tlen - blen;
2114 if (matchcmp > 0) /* (3) */
2118 if (dscopecmp > 0) {
2119 if (bscopecmp > 0) /* (5) */
2121 goto replace; /* (6) */
2123 if (dscopecmp < 0) {
2124 if (bscopecmp > 0) /* (7) */
2129 /* now dscopecmp must be 0 */
2131 goto replace; /* (9) */
2134 ifa_best = (struct in6_ifaddr *)ifa;
2135 blen = tlen >= 0 ? tlen :
2136 in6_matchlen(IFA_IN6(ifa), dst);
2137 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2141 /* count statistics for future improvements */
2142 if (ifa_best == NULL)
2143 ip6stat.ip6s_sources_none++;
2145 if (oifp == ifa_best->ia_ifp)
2146 ip6stat.ip6s_sources_sameif[best_scope]++;
2148 ip6stat.ip6s_sources_otherif[best_scope]++;
2150 if (best_scope == dst_scope)
2151 ip6stat.ip6s_sources_samescope[best_scope]++;
2153 ip6stat.ip6s_sources_otherscope[best_scope]++;
2155 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2156 ip6stat.ip6s_sources_deprecated[best_scope]++;
2163 * return the best address out of the same scope. if no address was
2164 * found, return the first valid address from designated IF.
2167 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2169 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2171 struct in6_ifaddr *besta = 0;
2172 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2174 dep[0] = dep[1] = NULL;
2177 * We first look for addresses in the same scope.
2178 * If there is one, return it.
2179 * If two or more, return one which matches the dst longest.
2180 * If none, return one of global addresses assigned other ifs.
2182 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2184 if (ifa->ifa_addr->sa_family != AF_INET6)
2186 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2187 continue; /* XXX: is there any case to allow anycast? */
2188 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2189 continue; /* don't use this interface */
2190 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2192 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2193 if (ip6_use_deprecated)
2194 dep[0] = (struct in6_ifaddr *)ifa;
2198 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2200 * call in6_matchlen() as few as possible
2204 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2205 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2208 besta = (struct in6_ifaddr *)ifa;
2211 besta = (struct in6_ifaddr *)ifa;
2217 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2219 if (ifa->ifa_addr->sa_family != AF_INET6)
2221 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2222 continue; /* XXX: is there any case to allow anycast? */
2223 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2224 continue; /* don't use this interface */
2225 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2227 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2228 if (ip6_use_deprecated)
2229 dep[1] = (struct in6_ifaddr *)ifa;
2233 return (struct in6_ifaddr *)ifa;
2236 /* use the last-resort values, that are, deprecated addresses */
2246 * perform DAD when interface becomes IFF_UP.
2249 in6_if_up(struct ifnet *ifp)
2252 struct in6_ifaddr *ia;
2253 int dad_delay; /* delay ticks before DAD output */
2256 * special cases, like 6to4, are handled in in6_ifattach
2258 in6_ifattach(ifp, NULL);
2261 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2263 if (ifa->ifa_addr->sa_family != AF_INET6)
2265 ia = (struct in6_ifaddr *)ifa;
2266 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2267 nd6_dad_start(ifa, &dad_delay);
2272 in6if_do_dad(struct ifnet *ifp)
2274 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2277 switch (ifp->if_type) {
2283 * These interfaces do not have the IFF_LOOPBACK flag,
2284 * but loop packets back. We do not have to do DAD on such
2285 * interfaces. We should even omit it, because loop-backed
2286 * NS would confuse the DAD procedure.
2291 * Our DAD routine requires the interface up and running.
2292 * However, some interfaces can be up before the RUNNING
2293 * status. Additionaly, users may try to assign addresses
2294 * before the interface becomes up (or running).
2295 * We simply skip DAD in such a case as a work around.
2296 * XXX: we should rather mark "tentative" on such addresses,
2297 * and do DAD after the interface becomes ready.
2299 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2300 (IFF_UP|IFF_RUNNING))
2308 * Calculate max IPv6 MTU through all the interfaces and store it
2314 unsigned long maxmtu = 0;
2317 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2319 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2320 ND_IFINFO(ifp)->linkmtu > maxmtu)
2321 maxmtu = ND_IFINFO(ifp)->linkmtu;
2323 if (maxmtu) /* update only when maxmtu is positive */
2324 in6_maxmtu = maxmtu;
2328 in6_domifattach(struct ifnet *ifp)
2330 struct in6_ifextra *ext;
2332 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2333 bzero(ext, sizeof(*ext));
2335 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2336 M_IFADDR, M_WAITOK);
2337 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2340 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2341 M_IFADDR, M_WAITOK);
2342 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2344 ext->nd_ifinfo = nd6_ifattach(ifp);
2345 ext->scope6_id = scope6_ifattach(ifp);
2350 in6_domifdetach(struct ifnet *ifp, void *aux)
2352 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2353 scope6_ifdetach(ext->scope6_id);
2354 nd6_ifdetach(ext->nd_ifinfo);
2355 free(ext->in6_ifstat, M_IFADDR);
2356 free(ext->icmp6_ifstat, M_IFADDR);
2357 free(ext, M_IFADDR);
2361 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2362 * v4 mapped addr or v4 compat addr
2365 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2367 bzero(sin, sizeof(*sin));
2368 sin->sin_len = sizeof(struct sockaddr_in);
2369 sin->sin_family = AF_INET;
2370 sin->sin_port = sin6->sin6_port;
2371 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2374 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2376 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2378 bzero(sin6, sizeof(*sin6));
2379 sin6->sin6_len = sizeof(struct sockaddr_in6);
2380 sin6->sin6_family = AF_INET6;
2381 sin6->sin6_port = sin->sin_port;
2382 sin6->sin6_addr.s6_addr32[0] = 0;
2383 sin6->sin6_addr.s6_addr32[1] = 0;
2384 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2385 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2388 /* Convert sockaddr_in6 into sockaddr_in. */
2390 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2392 struct sockaddr_in *sin_p;
2393 struct sockaddr_in6 sin6;
2396 * Save original sockaddr_in6 addr and convert it
2399 sin6 = *(struct sockaddr_in6 *)nam;
2400 sin_p = (struct sockaddr_in *)nam;
2401 in6_sin6_2_sin(sin_p, &sin6);
2404 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2406 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2408 struct sockaddr_in *sin_p;
2409 struct sockaddr_in6 *sin6_p;
2411 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2413 sin_p = (struct sockaddr_in *)*nam;
2414 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2415 FREE(*nam, M_SONAME);
2416 *nam = (struct sockaddr *)sin6_p;