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.16 2005/11/28 17:13:46 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 in6addr_any = IN6ADDR_ANY_INIT;
114 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
115 const struct in6_addr in6addr_nodelocal_allnodes =
116 IN6ADDR_NODELOCAL_ALLNODES_INIT;
117 const struct in6_addr in6addr_linklocal_allnodes =
118 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
119 const struct in6_addr in6addr_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 *);
137 struct in6_multihead in6_multihead; /* XXX BSS initialization */
139 int (*faithprefix_p)(struct in6_addr *);
142 * Subroutine for in6_ifaddloop() and in6_ifremloop().
143 * This routine does actual work.
146 in6_ifloop_request(int cmd, struct ifaddr *ifa)
148 struct sockaddr_in6 all1_sa;
149 struct rtentry *nrt = NULL;
152 bzero(&all1_sa, sizeof(all1_sa));
153 all1_sa.sin6_family = AF_INET6;
154 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
155 all1_sa.sin6_addr = in6mask128;
158 * We specify the address itself as the gateway, and set the
159 * RTF_LLINFO flag, so that the corresponding host route would have
160 * the flag, and thus applications that assume traditional behavior
161 * would be happy. Note that we assume the caller of the function
162 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
163 * which changes the outgoing interface to the loopback interface.
165 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
166 (struct sockaddr *)&all1_sa,
167 RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
169 log(LOG_ERR, "in6_ifloop_request: "
170 "%s operation failed for %s (errno=%d)\n",
171 cmd == RTM_ADD ? "ADD" : "DELETE",
172 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
177 * Make sure rt_ifa be equal to IFA, the second argument of the
179 * We need this because when we refer to rt_ifa->ia6_flags in
180 * ip6_input, we assume that the rt_ifa points to the address instead
181 * of the loopback address.
183 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
184 IFAFREE(nrt->rt_ifa);
190 * Report the addition/removal of the address to the routing socket.
191 * XXX: since we called rtinit for a p2p interface with a destination,
192 * we end up reporting twice in such a case. Should we rather
193 * omit the second report?
196 rt_newaddrmsg(cmd, ifa, e, nrt);
197 if (cmd == RTM_DELETE) {
198 if (nrt->rt_refcnt <= 0) {
199 /* XXX: we should free the entry ourselves. */
204 /* the cmd must be RTM_ADD here */
211 * Add ownaddr as loopback rtentry. We previously add the route only if
212 * necessary (ex. on a p2p link). However, since we now manage addresses
213 * separately from prefixes, we should always add the route. We can't
214 * rely on the cloning mechanism from the corresponding interface route
218 in6_ifaddloop(struct ifaddr *ifa)
222 /* If there is no loopback entry, allocate one. */
223 rt = rtpurelookup(ifa->ifa_addr);
224 if (rt == NULL || !(rt->rt_flags & RTF_HOST) ||
225 !(rt->rt_ifp->if_flags & IFF_LOOPBACK))
226 in6_ifloop_request(RTM_ADD, ifa);
232 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
236 in6_ifremloop(struct ifaddr *ifa)
238 struct in6_ifaddr *ia;
243 * Some of BSD variants do not remove cloned routes
244 * from an interface direct route, when removing the direct route
245 * (see comments in net/net_osdep.h). Even for variants that do remove
246 * cloned routes, they could fail to remove the cloned routes when
247 * we handle multple addresses that share a common prefix.
248 * So, we should remove the route corresponding to the deleted address
249 * regardless of the result of in6_is_ifloop_auto().
253 * Delete the entry only if exact one ifa exists. More than one ifa
254 * can exist if we assign a same single address to multiple
255 * (probably p2p) interfaces.
256 * XXX: we should avoid such a configuration in IPv6...
258 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
259 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
268 * Before deleting, check if a corresponding loopbacked host
269 * route surely exists. With this check, we can avoid to
270 * delete an interface direct route whose destination is same
271 * as the address being removed. This can happen when remofing
272 * a subnet-router anycast address on an interface attahced
273 * to a shared medium.
275 rt = rtpurelookup(ifa->ifa_addr);
276 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
277 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
279 in6_ifloop_request(RTM_DELETE, ifa);
285 in6_ifindex2scopeid(int idx)
289 struct sockaddr_in6 *sin6;
291 if (idx < 0 || if_index < idx)
293 ifp = ifindex2ifnet[idx];
295 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
297 if (ifa->ifa_addr->sa_family != AF_INET6)
299 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
300 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
301 return sin6->sin6_scope_id & 0xffff;
308 in6_mask2len(struct in6_addr *mask, u_char *lim0)
311 u_char *lim = lim0, *p;
314 lim0 - (u_char *)mask > sizeof(*mask)) /* ignore the scope_id part */
315 lim = (u_char *)mask + sizeof(*mask);
316 for (p = (u_char *)mask; p < lim; x++, p++) {
322 for (y = 0; y < 8; y++) {
323 if ((*p & (0x80 >> y)) == 0)
329 * when the limit pointer is given, do a stricter check on the
333 if (y != 0 && (*p & (0x00ff >> y)) != 0)
335 for (p = p + 1; p < lim; p++)
344 in6_len2mask(struct in6_addr *mask, int len)
348 bzero(mask, sizeof(*mask));
349 for (i = 0; i < len / 8; i++)
350 mask->s6_addr8[i] = 0xff;
352 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
355 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
356 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
359 in6_control(struct socket *so, u_long cmd, caddr_t data,
360 struct ifnet *ifp, struct thread *td)
362 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
363 struct in6_ifaddr *ia = NULL;
364 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
373 case SIOCGETSGCNT_IN6:
374 case SIOCGETMIFCNT_IN6:
375 return (mrt6_ioctl(cmd, data));
382 case SIOCSNDFLUSH_IN6:
383 case SIOCSPFXFLUSH_IN6:
384 case SIOCSRTRFLUSH_IN6:
385 case SIOCSDEFIFACE_IN6:
386 case SIOCSIFINFO_FLAGS:
390 case OSIOCGIFINFO_IN6:
391 case SIOCGIFINFO_IN6:
394 case SIOCGNBRINFO_IN6:
395 case SIOCGDEFIFACE_IN6:
396 return(nd6_ioctl(cmd, data, ifp));
400 case SIOCSIFPREFIX_IN6:
401 case SIOCDIFPREFIX_IN6:
402 case SIOCAIFPREFIX_IN6:
403 case SIOCCIFPREFIX_IN6:
404 case SIOCSGIFPREFIX_IN6:
405 case SIOCGIFPREFIX_IN6:
407 "prefix ioctls are now invalidated. "
408 "please use ifconfig.\n");
416 return(scope6_set(ifp,
417 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
420 return(scope6_get(ifp,
421 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
424 return(scope6_get_default((struct scope6_id *)
425 ifr->ifr_ifru.ifru_scope_id));
436 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
440 * Find address for this interface, if it exists.
442 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
443 struct sockaddr_in6 *sa6 =
444 (struct sockaddr_in6 *)&ifra->ifra_addr;
446 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
447 if (sa6->sin6_addr.s6_addr16[1] == 0) {
448 /* link ID is not embedded by the user */
449 sa6->sin6_addr.s6_addr16[1] =
450 htons(ifp->if_index);
451 } else if (sa6->sin6_addr.s6_addr16[1] !=
452 htons(ifp->if_index)) {
453 return(EINVAL); /* link ID contradicts */
455 if (sa6->sin6_scope_id) {
456 if (sa6->sin6_scope_id !=
457 (u_int32_t)ifp->if_index)
459 sa6->sin6_scope_id = 0; /* XXX: good way? */
462 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
466 case SIOCSIFADDR_IN6:
467 case SIOCSIFDSTADDR_IN6:
468 case SIOCSIFNETMASK_IN6:
470 * Since IPv6 allows a node to assign multiple addresses
471 * on a single interface, SIOCSIFxxx ioctls are not suitable
472 * and should be unused.
474 /* we decided to obsolete this command (20000704) */
477 case SIOCDIFADDR_IN6:
479 * for IPv4, we look for existing in_ifaddr here to allow
480 * "ifconfig if0 delete" to remove first IPv4 address on the
481 * interface. For IPv6, as the spec allow multiple interface
482 * address from the day one, we consider "remove the first one"
483 * semantics to be not preferable.
486 return(EADDRNOTAVAIL);
488 case SIOCAIFADDR_IN6:
490 * We always require users to specify a valid IPv6 address for
491 * the corresponding operation.
493 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
494 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
495 return(EAFNOSUPPORT);
501 case SIOCGIFADDR_IN6:
502 /* This interface is basically deprecated. use SIOCGIFCONF. */
504 case SIOCGIFAFLAG_IN6:
505 case SIOCGIFNETMASK_IN6:
506 case SIOCGIFDSTADDR_IN6:
507 case SIOCGIFALIFETIME_IN6:
508 /* must think again about its semantics */
510 return(EADDRNOTAVAIL);
512 case SIOCSIFALIFETIME_IN6:
514 struct in6_addrlifetime *lt;
519 return(EADDRNOTAVAIL);
520 /* sanity for overflow - beware unsigned */
521 lt = &ifr->ifr_ifru.ifru_lifetime;
522 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
523 && lt->ia6t_vltime + time_second < time_second) {
526 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
527 && lt->ia6t_pltime + time_second < time_second) {
536 case SIOCGIFADDR_IN6:
537 ifr->ifr_addr = ia->ia_addr;
540 case SIOCGIFDSTADDR_IN6:
541 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
544 * XXX: should we check if ifa_dstaddr is NULL and return
547 ifr->ifr_dstaddr = ia->ia_dstaddr;
550 case SIOCGIFNETMASK_IN6:
551 ifr->ifr_addr = ia->ia_prefixmask;
554 case SIOCGIFAFLAG_IN6:
555 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
558 case SIOCGIFSTAT_IN6:
561 bzero(&ifr->ifr_ifru.ifru_stat,
562 sizeof(ifr->ifr_ifru.ifru_stat));
563 ifr->ifr_ifru.ifru_stat =
564 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
567 case SIOCGIFSTAT_ICMP6:
568 bzero(&ifr->ifr_ifru.ifru_stat,
569 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
570 ifr->ifr_ifru.ifru_icmp6stat =
571 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
574 case SIOCGIFALIFETIME_IN6:
575 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
578 case SIOCSIFALIFETIME_IN6:
579 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
581 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
582 ia->ia6_lifetime.ia6t_expire =
583 time_second + ia->ia6_lifetime.ia6t_vltime;
585 ia->ia6_lifetime.ia6t_expire = 0;
586 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
587 ia->ia6_lifetime.ia6t_preferred =
588 time_second + ia->ia6_lifetime.ia6t_pltime;
590 ia->ia6_lifetime.ia6t_preferred = 0;
593 case SIOCAIFADDR_IN6:
596 struct nd_prefix pr0, *pr;
599 * first, make or update the interface address structure,
600 * and link it to the list.
602 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
606 * then, make the prefix on-link on the interface.
607 * XXX: we'd rather create the prefix before the address, but
608 * we need at least one address to install the corresponding
609 * interface route, so we configure the address first.
613 * convert mask to prefix length (prefixmask has already
614 * been validated in in6_update_ifa().
616 bzero(&pr0, sizeof(pr0));
618 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
620 if (pr0.ndpr_plen == 128)
621 break; /* we don't need to install a host route. */
622 pr0.ndpr_prefix = ifra->ifra_addr;
623 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
624 /* apply the mask for safety. */
625 for (i = 0; i < 4; i++) {
626 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
627 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
630 * XXX: since we don't have an API to set prefix (not address)
631 * lifetimes, we just use the same lifetimes as addresses.
632 * The (temporarily) installed lifetimes can be overridden by
633 * later advertised RAs (when accept_rtadv is non 0), which is
634 * an intended behavior.
636 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
638 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
639 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
640 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
642 /* add the prefix if there's one. */
643 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
645 * nd6_prelist_add will install the corresponding
648 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
651 log(LOG_ERR, "nd6_prelist_add succedded but "
653 return(EINVAL); /* XXX panic here? */
656 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
658 /* XXX: this should not happen! */
659 log(LOG_ERR, "in6_control: addition succeeded, but"
662 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
663 ia->ia6_ndpr == NULL) { /* new autoconfed addr */
668 * If this is the first autoconf address from
669 * the prefix, create a temporary address
670 * as well (when specified).
672 if (ip6_use_tempaddr &&
673 pr->ndpr_refcnt == 1) {
675 if ((e = in6_tmpifadd(ia, 1)) != 0) {
676 log(LOG_NOTICE, "in6_control: "
677 "failed to create a "
678 "temporary address, "
686 * this might affect the status of autoconfigured
687 * addresses, that is, this address might make
688 * other addresses detached.
690 pfxlist_onlink_check();
692 if (error == 0 && ia)
693 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
697 case SIOCDIFADDR_IN6:
700 struct nd_prefix pr0, *pr;
703 * If the address being deleted is the only one that owns
704 * the corresponding prefix, expire the prefix as well.
705 * XXX: theoretically, we don't have to warry about such
706 * relationship, since we separate the address management
707 * and the prefix management. We do this, however, to provide
708 * as much backward compatibility as possible in terms of
709 * the ioctl operation.
711 bzero(&pr0, sizeof(pr0));
713 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
715 if (pr0.ndpr_plen == 128)
717 pr0.ndpr_prefix = ia->ia_addr;
718 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
719 for (i = 0; i < 4; i++) {
720 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
721 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
724 * The logic of the following condition is a bit complicated.
725 * We expire the prefix when
726 * 1. the address obeys autoconfiguration and it is the
727 * only owner of the associated prefix, or
728 * 2. the address does not obey autoconf and there is no
729 * other owner of the prefix.
731 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
732 (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
733 pr->ndpr_refcnt == 1) ||
734 ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
735 pr->ndpr_refcnt == 0))) {
736 pr->ndpr_expire = 1; /* XXX: just for expiration */
740 in6_purgeaddr(&ia->ia_ifa);
741 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
746 if (ifp == NULL || ifp->if_ioctl == 0)
748 lwkt_serialize_enter(ifp->if_serializer);
749 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
750 lwkt_serialize_exit(ifp->if_serializer);
758 * Update parameters of an IPv6 interface address.
759 * If necessary, a new entry is created and linked into address chains.
760 * This function is separated from in6_control().
761 * XXX: should this be performed under splnet()?
764 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
765 struct in6_ifaddr *ia)
767 int error = 0, hostIsNew = 0, plen = -1;
768 struct in6_ifaddr *oia;
769 struct sockaddr_in6 dst6;
770 struct in6_addrlifetime *lt;
772 /* Validate parameters */
773 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
777 * The destination address for a p2p link must have a family
778 * of AF_UNSPEC or AF_INET6.
780 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
781 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
782 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
783 return(EAFNOSUPPORT);
785 * validate ifra_prefixmask. don't check sin6_family, netmask
786 * does not carry fields other than sin6_len.
788 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
791 * Because the IPv6 address architecture is classless, we require
792 * users to specify a (non 0) prefix length (mask) for a new address.
793 * We also require the prefix (when specified) mask is valid, and thus
794 * reject a non-consecutive mask.
796 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
798 if (ifra->ifra_prefixmask.sin6_len != 0) {
799 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
800 (u_char *)&ifra->ifra_prefixmask +
801 ifra->ifra_prefixmask.sin6_len);
807 * In this case, ia must not be NULL. We just use its prefix
810 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
813 * If the destination address on a p2p interface is specified,
814 * and the address is a scoped one, validate/set the scope
817 dst6 = ifra->ifra_dstaddr;
818 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
819 (dst6.sin6_family == AF_INET6)) {
822 if ((error = in6_recoverscope(&dst6,
823 &ifra->ifra_dstaddr.sin6_addr,
826 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
827 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
828 dst6.sin6_scope_id = scopeid;
829 else if (dst6.sin6_scope_id != scopeid)
830 return(EINVAL); /* scope ID mismatch. */
831 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
834 dst6.sin6_scope_id = 0; /* XXX */
837 * The destination address can be specified only for a p2p or a
838 * loopback interface. If specified, the corresponding prefix length
841 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
842 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
843 /* XXX: noisy message */
844 log(LOG_INFO, "in6_update_ifa: a destination can be "
845 "specified for a p2p or a loopback IF only\n");
850 * The following message seems noisy, but we dare to
851 * add it for diagnosis.
853 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
854 "when dstaddr is specified\n");
858 /* lifetime consistency check */
859 lt = &ifra->ifra_lifetime;
860 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
861 && lt->ia6t_vltime + time_second < time_second) {
864 if (lt->ia6t_vltime == 0) {
866 * the following log might be noisy, but this is a typical
867 * configuration mistake or a tool's bug.
870 "in6_update_ifa: valid lifetime is 0 for %s\n",
871 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
873 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
874 && lt->ia6t_pltime + time_second < time_second) {
879 * If this is a new address, allocate a new ifaddr and link it
885 * When in6_update_ifa() is called in a process of a received
886 * RA, it is called under splnet(). So, we should call malloc
889 ia = (struct in6_ifaddr *)
890 malloc(sizeof(*ia), M_IFADDR, M_NOWAIT);
893 bzero((caddr_t)ia, sizeof(*ia));
894 /* Initialize the address and masks */
895 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
896 ia->ia_addr.sin6_family = AF_INET6;
897 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
898 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
900 * XXX: some functions expect that ifa_dstaddr is not
901 * NULL for p2p interfaces.
903 ia->ia_ifa.ifa_dstaddr
904 = (struct sockaddr *)&ia->ia_dstaddr;
906 ia->ia_ifa.ifa_dstaddr = NULL;
908 ia->ia_ifa.ifa_netmask
909 = (struct sockaddr *)&ia->ia_prefixmask;
912 if ((oia = in6_ifaddr) != NULL) {
913 for ( ; oia->ia_next; oia = oia->ia_next)
919 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
923 /* set prefix mask */
924 if (ifra->ifra_prefixmask.sin6_len) {
926 * We prohibit changing the prefix length of an existing
928 * + such an operation should be rare in IPv6, and
929 * + the operation would confuse prefix management.
931 if (ia->ia_prefixmask.sin6_len &&
932 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
933 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
934 " existing (%s) address should not be changed\n",
935 ip6_sprintf(&ia->ia_addr.sin6_addr));
939 ia->ia_prefixmask = ifra->ifra_prefixmask;
943 * If a new destination address is specified, scrub the old one and
944 * install the new destination. Note that the interface must be
945 * p2p or loopback (see the check above.)
947 if (dst6.sin6_family == AF_INET6 &&
948 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
949 &ia->ia_dstaddr.sin6_addr)) {
952 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
953 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
955 log(LOG_ERR, "in6_update_ifa: failed to remove "
956 "a route to the old destination: %s\n",
957 ip6_sprintf(&ia->ia_addr.sin6_addr));
958 /* proceed anyway... */
961 ia->ia_flags &= ~IFA_ROUTE;
962 ia->ia_dstaddr = dst6;
965 /* reset the interface and routing table appropriately. */
966 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
970 * Beyond this point, we should call in6_purgeaddr upon an error,
971 * not just go to unlink.
974 #if 0 /* disable this mechanism for now */
975 /* update prefix list */
977 (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
980 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
981 if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
982 in6_purgeaddr((struct ifaddr *)ia);
988 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
989 struct sockaddr_in6 mltaddr, mltmask;
990 struct in6_multi *in6m;
994 * join solicited multicast addr for new host id
996 struct in6_addr llsol;
997 bzero(&llsol, sizeof(struct in6_addr));
998 llsol.s6_addr16[0] = htons(0xff02);
999 llsol.s6_addr16[1] = htons(ifp->if_index);
1000 llsol.s6_addr32[1] = 0;
1001 llsol.s6_addr32[2] = htonl(1);
1002 llsol.s6_addr32[3] =
1003 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1004 llsol.s6_addr8[12] = 0xff;
1005 (void)in6_addmulti(&llsol, ifp, &error);
1008 "in6_update_ifa: addmulti failed for "
1009 "%s on %s (errno=%d)\n",
1010 ip6_sprintf(&llsol), if_name(ifp),
1012 in6_purgeaddr((struct ifaddr *)ia);
1017 bzero(&mltmask, sizeof(mltmask));
1018 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1019 mltmask.sin6_family = AF_INET6;
1020 mltmask.sin6_addr = in6mask32;
1023 * join link-local all-nodes address
1025 bzero(&mltaddr, sizeof(mltaddr));
1026 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1027 mltaddr.sin6_family = AF_INET6;
1028 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1029 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1031 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1034 (struct sockaddr *)&mltaddr,
1035 (struct sockaddr *)&ia->ia_addr,
1036 (struct sockaddr *)&mltmask,
1037 RTF_UP|RTF_CLONING, /* xxx */
1038 (struct rtentry **)0);
1039 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1042 "in6_update_ifa: addmulti failed for "
1043 "%s on %s (errno=%d)\n",
1044 ip6_sprintf(&mltaddr.sin6_addr),
1045 if_name(ifp), error);
1050 * join node information group address
1052 #define hostnamelen strlen(hostname)
1053 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1055 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1056 if (in6m == NULL && ia != NULL) {
1057 (void)in6_addmulti(&mltaddr.sin6_addr,
1060 log(LOG_WARNING, "in6_update_ifa: "
1061 "addmulti failed for "
1062 "%s on %s (errno=%d)\n",
1063 ip6_sprintf(&mltaddr.sin6_addr),
1064 if_name(ifp), error);
1071 * join node-local all-nodes address, on loopback.
1072 * XXX: since "node-local" is obsoleted by interface-local,
1073 * we have to join the group on every interface with
1074 * some interface-boundary restriction.
1076 if (ifp->if_flags & IFF_LOOPBACK) {
1077 struct in6_ifaddr *ia_loop;
1079 struct in6_addr loop6 = in6addr_loopback;
1080 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1082 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1084 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1085 if (in6m == NULL && ia_loop != NULL) {
1087 (struct sockaddr *)&mltaddr,
1088 (struct sockaddr *)&ia_loop->ia_addr,
1089 (struct sockaddr *)&mltmask,
1091 (struct rtentry **)0);
1092 (void)in6_addmulti(&mltaddr.sin6_addr, ifp,
1095 log(LOG_WARNING, "in6_update_ifa: "
1096 "addmulti failed for %s on %s "
1098 ip6_sprintf(&mltaddr.sin6_addr),
1099 if_name(ifp), error);
1105 ia->ia6_flags = ifra->ifra_flags;
1106 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1107 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1109 ia->ia6_lifetime = ifra->ifra_lifetime;
1111 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1112 ia->ia6_lifetime.ia6t_expire =
1113 time_second + ia->ia6_lifetime.ia6t_vltime;
1115 ia->ia6_lifetime.ia6t_expire = 0;
1116 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1117 ia->ia6_lifetime.ia6t_preferred =
1118 time_second + ia->ia6_lifetime.ia6t_pltime;
1120 ia->ia6_lifetime.ia6t_preferred = 0;
1123 * Perform DAD, if needed.
1124 * XXX It may be of use, if we can administratively
1127 if (in6if_do_dad(ifp) && (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
1128 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1129 nd6_dad_start((struct ifaddr *)ia, NULL);
1136 * XXX: if a change of an existing address failed, keep the entry
1140 in6_unlink_ifa(ia, ifp);
1145 in6_purgeaddr(struct ifaddr *ifa)
1147 struct ifnet *ifp = ifa->ifa_ifp;
1148 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1150 /* stop DAD processing */
1154 * delete route to the destination of the address being purged.
1155 * The interface must be p2p or loopback in this case.
1157 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1160 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1162 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1163 "a route to the p2p destination: %s on %s, "
1165 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1167 /* proceed anyway... */
1170 ia->ia_flags &= ~IFA_ROUTE;
1173 /* Remove ownaddr's loopback rtentry, if it exists. */
1174 in6_ifremloop(&(ia->ia_ifa));
1176 if (ifp->if_flags & IFF_MULTICAST) {
1178 * delete solicited multicast addr for deleting host id
1180 struct in6_multi *in6m;
1181 struct in6_addr llsol;
1182 bzero(&llsol, sizeof(struct in6_addr));
1183 llsol.s6_addr16[0] = htons(0xff02);
1184 llsol.s6_addr16[1] = htons(ifp->if_index);
1185 llsol.s6_addr32[1] = 0;
1186 llsol.s6_addr32[2] = htonl(1);
1187 llsol.s6_addr32[3] =
1188 ia->ia_addr.sin6_addr.s6_addr32[3];
1189 llsol.s6_addr8[12] = 0xff;
1191 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1196 in6_unlink_ifa(ia, ifp);
1200 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1203 struct in6_ifaddr *oia;
1207 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1210 if (oia == (ia = in6_ifaddr))
1211 in6_ifaddr = ia->ia_next;
1213 while (ia->ia_next && (ia->ia_next != oia))
1216 ia->ia_next = oia->ia_next;
1219 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1223 if (oia->ia6_ifpr) { /* check for safety */
1224 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1225 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1226 in6_prefix_remove_ifid(iilen, oia);
1230 * When an autoconfigured address is being removed, release the
1231 * reference to the base prefix. Also, since the release might
1232 * affect the status of other (detached) addresses, call
1233 * pfxlist_onlink_check().
1235 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1236 if (oia->ia6_ndpr == NULL) {
1237 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1238 "%p has no prefix\n", oia);
1240 oia->ia6_ndpr->ndpr_refcnt--;
1241 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1242 oia->ia6_ndpr = NULL;
1245 pfxlist_onlink_check();
1249 * release another refcnt for the link from in6_ifaddr.
1250 * Note that we should decrement the refcnt at least once for all *BSD.
1252 IFAFREE(&oia->ia_ifa);
1258 in6_purgeif(struct ifnet *ifp)
1260 struct ifaddr *ifa, *nifa;
1262 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa)
1264 nifa = TAILQ_NEXT(ifa, ifa_list);
1265 if (ifa->ifa_addr->sa_family != AF_INET6)
1275 * SIOCGLIFADDR: get first address. (?)
1276 * SIOCGLIFADDR with IFLR_PREFIX:
1277 * get first address that matches the specified prefix.
1278 * SIOCALIFADDR: add the specified address.
1279 * SIOCALIFADDR with IFLR_PREFIX:
1280 * add the specified prefix, filling hostid part from
1281 * the first link-local address. prefixlen must be <= 64.
1282 * SIOCDLIFADDR: delete the specified address.
1283 * SIOCDLIFADDR with IFLR_PREFIX:
1284 * delete the first address that matches the specified prefix.
1286 * EINVAL on invalid parameters
1287 * EADDRNOTAVAIL on prefix match failed/specified address not found
1288 * other values may be returned from in6_ioctl()
1290 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1291 * this is to accomodate address naming scheme other than RFC2374,
1293 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1294 * address encoding scheme. (see figure on page 8)
1297 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1298 struct ifnet *ifp, struct thread *td)
1300 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1302 struct sockaddr *sa;
1305 if (!data || !ifp) {
1306 panic("invalid argument to in6_lifaddr_ioctl");
1312 /* address must be specified on GET with IFLR_PREFIX */
1313 if ((iflr->flags & IFLR_PREFIX) == 0)
1318 /* address must be specified on ADD and DELETE */
1319 sa = (struct sockaddr *)&iflr->addr;
1320 if (sa->sa_family != AF_INET6)
1322 if (sa->sa_len != sizeof(struct sockaddr_in6))
1324 /* XXX need improvement */
1325 sa = (struct sockaddr *)&iflr->dstaddr;
1326 if (sa->sa_family && sa->sa_family != AF_INET6)
1328 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1331 default: /* shouldn't happen */
1333 panic("invalid cmd to in6_lifaddr_ioctl");
1339 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1345 struct in6_aliasreq ifra;
1346 struct in6_addr *hostid = NULL;
1349 if ((iflr->flags & IFLR_PREFIX) != 0) {
1350 struct sockaddr_in6 *sin6;
1353 * hostid is to fill in the hostid part of the
1354 * address. hostid points to the first link-local
1355 * address attached to the interface.
1357 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1359 return EADDRNOTAVAIL;
1360 hostid = IFA_IN6(ifa);
1362 /* prefixlen must be <= 64. */
1363 if (64 < iflr->prefixlen)
1365 prefixlen = iflr->prefixlen;
1367 /* hostid part must be zero. */
1368 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1369 if (sin6->sin6_addr.s6_addr32[2] != 0
1370 || sin6->sin6_addr.s6_addr32[3] != 0) {
1374 prefixlen = iflr->prefixlen;
1376 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1377 bzero(&ifra, sizeof(ifra));
1378 bcopy(iflr->iflr_name, ifra.ifra_name,
1379 sizeof(ifra.ifra_name));
1381 bcopy(&iflr->addr, &ifra.ifra_addr,
1382 ((struct sockaddr *)&iflr->addr)->sa_len);
1384 /* fill in hostid part */
1385 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1386 hostid->s6_addr32[2];
1387 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1388 hostid->s6_addr32[3];
1391 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1392 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1393 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1395 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1396 hostid->s6_addr32[2];
1397 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1398 hostid->s6_addr32[3];
1402 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1403 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1405 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1406 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1411 struct in6_ifaddr *ia;
1412 struct in6_addr mask, candidate, match;
1413 struct sockaddr_in6 *sin6;
1416 bzero(&mask, sizeof(mask));
1417 if (iflr->flags & IFLR_PREFIX) {
1418 /* lookup a prefix rather than address. */
1419 in6_len2mask(&mask, iflr->prefixlen);
1421 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1422 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1423 match.s6_addr32[0] &= mask.s6_addr32[0];
1424 match.s6_addr32[1] &= mask.s6_addr32[1];
1425 match.s6_addr32[2] &= mask.s6_addr32[2];
1426 match.s6_addr32[3] &= mask.s6_addr32[3];
1428 /* if you set extra bits, that's wrong */
1429 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1434 if (cmd == SIOCGLIFADDR) {
1435 /* on getting an address, take the 1st match */
1438 /* on deleting an address, do exact match */
1439 in6_len2mask(&mask, 128);
1440 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1441 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1447 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1449 if (ifa->ifa_addr->sa_family != AF_INET6)
1454 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1456 * XXX: this is adhoc, but is necessary to allow
1457 * a user to specify fe80::/64 (not /10) for a
1458 * link-local address.
1460 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1461 candidate.s6_addr16[1] = 0;
1462 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1463 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1464 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1465 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1466 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1470 return EADDRNOTAVAIL;
1473 if (cmd == SIOCGLIFADDR) {
1474 struct sockaddr_in6 *s6;
1476 /* fill in the if_laddrreq structure */
1477 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1478 s6 = (struct sockaddr_in6 *)&iflr->addr;
1479 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1480 s6->sin6_addr.s6_addr16[1] = 0;
1482 in6_addr2scopeid(ifp, &s6->sin6_addr);
1484 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1485 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1486 ia->ia_dstaddr.sin6_len);
1487 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1488 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1489 s6->sin6_addr.s6_addr16[1] = 0;
1491 in6_addr2scopeid(ifp,
1495 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1498 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1501 iflr->flags = ia->ia6_flags; /* XXX */
1505 struct in6_aliasreq ifra;
1507 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1508 bzero(&ifra, sizeof(ifra));
1509 bcopy(iflr->iflr_name, ifra.ifra_name,
1510 sizeof(ifra.ifra_name));
1512 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1513 ia->ia_addr.sin6_len);
1514 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1515 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1516 ia->ia_dstaddr.sin6_len);
1518 bzero(&ifra.ifra_dstaddr,
1519 sizeof(ifra.ifra_dstaddr));
1521 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1522 ia->ia_prefixmask.sin6_len);
1524 ifra.ifra_flags = ia->ia6_flags;
1525 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1531 return EOPNOTSUPP; /* just for safety */
1535 * Initialize an interface's intetnet6 address
1536 * and routing table entry.
1539 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6,
1542 int error = 0, plen, ifacount = 0;
1545 lwkt_serialize_enter(ifp->if_serializer);
1548 * Give the interface a chance to initialize
1549 * if this is its first address,
1550 * and to validate the address if necessary.
1552 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1554 if (ifa->ifa_addr == NULL)
1555 continue; /* just for safety */
1556 if (ifa->ifa_addr->sa_family != AF_INET6)
1561 ia->ia_addr = *sin6;
1563 if (ifacount <= 1 && ifp->if_ioctl &&
1564 (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia,
1565 (struct ucred *)NULL))) {
1566 lwkt_serialize_exit(ifp->if_serializer);
1569 lwkt_serialize_exit(ifp->if_serializer);
1571 ia->ia_ifa.ifa_metric = ifp->if_metric;
1573 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1577 * If the destination address is specified for a point-to-point
1578 * interface, install a route to the destination as an interface
1581 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1582 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1583 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1584 RTF_UP | RTF_HOST)) != 0)
1586 ia->ia_flags |= IFA_ROUTE;
1590 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1592 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1595 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1597 /* set the rtrequest function to create llinfo */
1598 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1599 in6_ifaddloop(&(ia->ia_ifa));
1606 * Add an address to the list of IP6 multicast addresses for a
1610 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
1612 struct in6_multi *in6m;
1613 struct sockaddr_in6 sin6;
1614 struct ifmultiaddr *ifma;
1621 * Call generic routine to add membership or increment
1622 * refcount. It wants addresses in the form of a sockaddr,
1623 * so we build one here (being careful to zero the unused bytes).
1625 bzero(&sin6, sizeof sin6);
1626 sin6.sin6_family = AF_INET6;
1627 sin6.sin6_len = sizeof sin6;
1628 sin6.sin6_addr = *maddr6;
1629 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1636 * If ifma->ifma_protospec is null, then if_addmulti() created
1637 * a new record. Otherwise, we are done.
1639 if (ifma->ifma_protospec != 0) {
1641 return ifma->ifma_protospec;
1644 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1645 at interrupt time? If so, need to fix if_addmulti. XXX */
1646 in6m = (struct in6_multi *)malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1652 bzero(in6m, sizeof *in6m);
1653 in6m->in6m_addr = *maddr6;
1654 in6m->in6m_ifp = ifp;
1655 in6m->in6m_ifma = ifma;
1656 ifma->ifma_protospec = in6m;
1657 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1660 * Let MLD6 know that we have joined a new IP6 multicast
1663 mld6_start_listening(in6m);
1669 * Delete a multicast address record.
1672 in6_delmulti(struct in6_multi *in6m)
1674 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1678 if (ifma->ifma_refcount == 1) {
1680 * No remaining claims to this record; let MLD6 know
1681 * that we are leaving the multicast group.
1683 mld6_stop_listening(in6m);
1684 ifma->ifma_protospec = 0;
1685 LIST_REMOVE(in6m, in6m_entry);
1686 free(in6m, M_IPMADDR);
1688 /* XXX - should be separate API for when we have an ifma? */
1689 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1694 * Find an IPv6 interface link-local address specific to an interface.
1697 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1701 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1703 if (ifa->ifa_addr == NULL)
1704 continue; /* just for safety */
1705 if (ifa->ifa_addr->sa_family != AF_INET6)
1707 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1708 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1715 return((struct in6_ifaddr *)ifa);
1720 * find the internet address corresponding to a given interface and address.
1723 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1727 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1729 if (ifa->ifa_addr == NULL)
1730 continue; /* just for safety */
1731 if (ifa->ifa_addr->sa_family != AF_INET6)
1733 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1737 return((struct in6_ifaddr *)ifa);
1741 * Convert IP6 address to printable (loggable) representation.
1743 static char digits[] = "0123456789abcdef";
1744 static int ip6round = 0;
1746 ip6_sprintf(const struct in6_addr *addr)
1748 static char ip6buf[8][48];
1751 const u_short *a = (const u_short *)addr;
1755 ip6round = (ip6round + 1) & 7;
1756 cp = ip6buf[ip6round];
1758 for (i = 0; i < 8; i++) {
1769 if (dcolon == 0 && *(a + 1) == 0) {
1781 d = (const u_char *)a;
1782 *cp++ = digits[*d >> 4];
1783 *cp++ = digits[*d++ & 0xf];
1784 *cp++ = digits[*d >> 4];
1785 *cp++ = digits[*d & 0xf];
1790 return(ip6buf[ip6round]);
1794 in6_localaddr(struct in6_addr *in6)
1796 struct in6_ifaddr *ia;
1798 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1801 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1802 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1803 &ia->ia_prefixmask.sin6_addr))
1810 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1812 struct in6_ifaddr *ia;
1814 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1815 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1817 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1818 return(1); /* true */
1820 /* XXX: do we still have to go thru the rest of the list? */
1823 return(0); /* false */
1827 * return length of part which dst and src are equal
1831 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1834 u_char *s = (u_char *)src, *d = (u_char *)dst;
1835 u_char *lim = s + 16, r;
1838 if ((r = (*d++ ^ *s++)) != 0) {
1849 /* XXX: to be scope conscious */
1851 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1853 int bytelen, bitlen;
1856 if (0 > len || len > 128) {
1857 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1865 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1867 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1868 p2->s6_addr[bytelen] >> (8 - bitlen))
1875 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1877 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1878 int bytelen, bitlen, i;
1881 if (0 > len || len > 128) {
1882 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1887 bzero(maskp, sizeof(*maskp));
1890 for (i = 0; i < bytelen; i++)
1891 maskp->s6_addr[i] = 0xff;
1893 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1897 * return the best address out of the same scope
1900 in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst)
1902 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
1906 struct in6_ifaddr *ifa_best = NULL;
1910 printf("in6_ifawithscope: output interface is not specified\n");
1916 * We search for all addresses on all interfaces from the beginning.
1917 * Comparing an interface with the outgoing interface will be done
1918 * only at the final stage of tiebreaking.
1920 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1923 * We can never take an address that breaks the scope zone
1924 * of the destination.
1926 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
1929 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1931 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
1933 if (ifa->ifa_addr->sa_family != AF_INET6)
1936 src_scope = in6_addrscope(IFA_IN6(ifa));
1939 * Don't use an address before completing DAD
1940 * nor a duplicated address.
1942 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1946 /* XXX: is there any case to allow anycasts? */
1947 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1951 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1956 * If this is the first address we find,
1959 if (ifa_best == NULL)
1963 * ifa_best is never NULL beyond this line except
1964 * within the block labeled "replace".
1968 * If ifa_best has a smaller scope than dst and
1969 * the current address has a larger one than
1970 * (or equal to) dst, always replace ifa_best.
1971 * Also, if the current address has a smaller scope
1972 * than dst, ignore it unless ifa_best also has a
1974 * Consequently, after the two if-clause below,
1975 * the followings must be satisfied:
1976 * (scope(src) < scope(dst) &&
1977 * scope(best) < scope(dst))
1979 * (scope(best) >= scope(dst) &&
1980 * scope(src) >= scope(dst))
1982 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
1983 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
1984 goto replace; /* (A) */
1985 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
1986 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
1990 * A deprecated address SHOULD NOT be used in new
1991 * communications if an alternate (non-deprecated)
1992 * address is available and has sufficient scope.
1993 * RFC 2462, Section 5.5.4.
1995 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1996 IN6_IFF_DEPRECATED) {
1998 * Ignore any deprecated addresses if
1999 * specified by configuration.
2001 if (!ip6_use_deprecated)
2005 * If we have already found a non-deprecated
2006 * candidate, just ignore deprecated addresses.
2008 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2014 * A non-deprecated address is always preferred
2015 * to a deprecated one regardless of scopes and
2016 * address matching (Note invariants ensured by the
2017 * conditions (A) and (B) above.)
2019 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2020 (((struct in6_ifaddr *)ifa)->ia6_flags &
2021 IN6_IFF_DEPRECATED) == 0)
2025 * When we use temporary addresses described in
2026 * RFC 3041, we prefer temporary addresses to
2027 * public autoconf addresses. Again, note the
2028 * invariants from (A) and (B). Also note that we
2029 * don't have any preference between static addresses
2030 * and autoconf addresses (despite of whether or not
2031 * the latter is temporary or public.)
2033 if (ip6_use_tempaddr) {
2034 struct in6_ifaddr *ifat;
2036 ifat = (struct in6_ifaddr *)ifa;
2037 if ((ifa_best->ia6_flags &
2038 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2039 == IN6_IFF_AUTOCONF &&
2041 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2042 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2045 if ((ifa_best->ia6_flags &
2046 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2047 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2049 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2050 == IN6_IFF_AUTOCONF) {
2056 * At this point, we have two cases:
2057 * 1. we are looking at a non-deprecated address,
2058 * and ifa_best is also non-deprecated.
2059 * 2. we are looking at a deprecated address,
2060 * and ifa_best is also deprecated.
2061 * Also, we do not have to consider a case where
2062 * the scope of if_best is larger(smaller) than dst and
2063 * the scope of the current address is smaller(larger)
2064 * than dst. Such a case has already been covered.
2065 * Tiebreaking is done according to the following
2067 * - the scope comparison between the address and
2069 * - the scope comparison between the address and
2070 * ifa_best (bscopecmp)
2071 * - if the address match dst longer than ifa_best
2073 * - if the address is on the outgoing I/F (outI/F)
2075 * Roughly speaking, the selection policy is
2076 * - the most important item is scope. The same scope
2077 * is best. Then search for a larger scope.
2078 * Smaller scopes are the last resort.
2079 * - A deprecated address is chosen only when we have
2080 * no address that has an enough scope, but is
2081 * prefered to any addresses of smaller scopes
2082 * (this must be already done above.)
2083 * - addresses on the outgoing I/F are preferred to
2084 * ones on other interfaces if none of above
2085 * tiebreaks. In the table below, the column "bI"
2086 * means if the best_ifa is on the outgoing
2087 * interface, and the column "sI" means if the ifa
2088 * is on the outgoing interface.
2089 * - If there is no other reasons to choose one,
2090 * longest address match against dst is considered.
2092 * The precise decision table is as follows:
2093 * dscopecmp bscopecmp match bI oI | replace?
2094 * N/A equal N/A Y N | No (1)
2095 * N/A equal N/A N Y | Yes (2)
2096 * N/A equal larger N/A | Yes (3)
2097 * N/A equal !larger N/A | No (4)
2098 * larger larger N/A N/A | No (5)
2099 * larger smaller N/A N/A | Yes (6)
2100 * smaller larger N/A N/A | Yes (7)
2101 * smaller smaller N/A N/A | No (8)
2102 * equal smaller N/A N/A | Yes (9)
2103 * equal larger (already done at A above)
2105 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2106 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2108 if (bscopecmp == 0) {
2109 struct ifnet *bifp = ifa_best->ia_ifp;
2111 if (bifp == oifp && ifp != oifp) /* (1) */
2113 if (bifp != oifp && ifp == oifp) /* (2) */
2117 * Both bifp and ifp are on the outgoing
2118 * interface, or both two are on a different
2119 * interface from the outgoing I/F.
2120 * now we need address matching against dst
2123 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2124 matchcmp = tlen - blen;
2125 if (matchcmp > 0) /* (3) */
2129 if (dscopecmp > 0) {
2130 if (bscopecmp > 0) /* (5) */
2132 goto replace; /* (6) */
2134 if (dscopecmp < 0) {
2135 if (bscopecmp > 0) /* (7) */
2140 /* now dscopecmp must be 0 */
2142 goto replace; /* (9) */
2145 ifa_best = (struct in6_ifaddr *)ifa;
2146 blen = tlen >= 0 ? tlen :
2147 in6_matchlen(IFA_IN6(ifa), dst);
2148 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2152 /* count statistics for future improvements */
2153 if (ifa_best == NULL)
2154 ip6stat.ip6s_sources_none++;
2156 if (oifp == ifa_best->ia_ifp)
2157 ip6stat.ip6s_sources_sameif[best_scope]++;
2159 ip6stat.ip6s_sources_otherif[best_scope]++;
2161 if (best_scope == dst_scope)
2162 ip6stat.ip6s_sources_samescope[best_scope]++;
2164 ip6stat.ip6s_sources_otherscope[best_scope]++;
2166 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2167 ip6stat.ip6s_sources_deprecated[best_scope]++;
2174 * return the best address out of the same scope. if no address was
2175 * found, return the first valid address from designated IF.
2178 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2180 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2182 struct in6_ifaddr *besta = 0;
2183 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2185 dep[0] = dep[1] = NULL;
2188 * We first look for addresses in the same scope.
2189 * If there is one, return it.
2190 * If two or more, return one which matches the dst longest.
2191 * If none, return one of global addresses assigned other ifs.
2193 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2195 if (ifa->ifa_addr->sa_family != AF_INET6)
2197 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2198 continue; /* XXX: is there any case to allow anycast? */
2199 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2200 continue; /* don't use this interface */
2201 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2203 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2204 if (ip6_use_deprecated)
2205 dep[0] = (struct in6_ifaddr *)ifa;
2209 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2211 * call in6_matchlen() as few as possible
2215 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2216 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2219 besta = (struct in6_ifaddr *)ifa;
2222 besta = (struct in6_ifaddr *)ifa;
2228 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2230 if (ifa->ifa_addr->sa_family != AF_INET6)
2232 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2233 continue; /* XXX: is there any case to allow anycast? */
2234 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2235 continue; /* don't use this interface */
2236 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2238 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2239 if (ip6_use_deprecated)
2240 dep[1] = (struct in6_ifaddr *)ifa;
2244 return (struct in6_ifaddr *)ifa;
2247 /* use the last-resort values, that are, deprecated addresses */
2257 * perform DAD when interface becomes IFF_UP.
2260 in6_if_up(struct ifnet *ifp)
2263 struct in6_ifaddr *ia;
2264 int dad_delay; /* delay ticks before DAD output */
2267 * special cases, like 6to4, are handled in in6_ifattach
2269 in6_ifattach(ifp, NULL);
2272 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2274 if (ifa->ifa_addr->sa_family != AF_INET6)
2276 ia = (struct in6_ifaddr *)ifa;
2277 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2278 nd6_dad_start(ifa, &dad_delay);
2283 in6if_do_dad(struct ifnet *ifp)
2285 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2288 switch (ifp->if_type) {
2294 * These interfaces do not have the IFF_LOOPBACK flag,
2295 * but loop packets back. We do not have to do DAD on such
2296 * interfaces. We should even omit it, because loop-backed
2297 * NS would confuse the DAD procedure.
2302 * Our DAD routine requires the interface up and running.
2303 * However, some interfaces can be up before the RUNNING
2304 * status. Additionaly, users may try to assign addresses
2305 * before the interface becomes up (or running).
2306 * We simply skip DAD in such a case as a work around.
2307 * XXX: we should rather mark "tentative" on such addresses,
2308 * and do DAD after the interface becomes ready.
2310 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2311 (IFF_UP|IFF_RUNNING))
2319 * Calculate max IPv6 MTU through all the interfaces and store it
2325 unsigned long maxmtu = 0;
2328 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2330 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2331 ND_IFINFO(ifp)->linkmtu > maxmtu)
2332 maxmtu = ND_IFINFO(ifp)->linkmtu;
2334 if (maxmtu) /* update only when maxmtu is positive */
2335 in6_maxmtu = maxmtu;
2339 in6_domifattach(struct ifnet *ifp)
2341 struct in6_ifextra *ext;
2343 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2344 bzero(ext, sizeof(*ext));
2346 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2347 M_IFADDR, M_WAITOK);
2348 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2351 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2352 M_IFADDR, M_WAITOK);
2353 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2355 ext->nd_ifinfo = nd6_ifattach(ifp);
2356 ext->scope6_id = scope6_ifattach(ifp);
2361 in6_domifdetach(struct ifnet *ifp, void *aux)
2363 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2364 scope6_ifdetach(ext->scope6_id);
2365 nd6_ifdetach(ext->nd_ifinfo);
2366 free(ext->in6_ifstat, M_IFADDR);
2367 free(ext->icmp6_ifstat, M_IFADDR);
2368 free(ext, M_IFADDR);
2372 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2373 * v4 mapped addr or v4 compat addr
2376 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2378 bzero(sin, sizeof(*sin));
2379 sin->sin_len = sizeof(struct sockaddr_in);
2380 sin->sin_family = AF_INET;
2381 sin->sin_port = sin6->sin6_port;
2382 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2385 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2387 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2389 bzero(sin6, sizeof(*sin6));
2390 sin6->sin6_len = sizeof(struct sockaddr_in6);
2391 sin6->sin6_family = AF_INET6;
2392 sin6->sin6_port = sin->sin_port;
2393 sin6->sin6_addr.s6_addr32[0] = 0;
2394 sin6->sin6_addr.s6_addr32[1] = 0;
2395 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2396 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2399 /* Convert sockaddr_in6 into sockaddr_in. */
2401 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2403 struct sockaddr_in *sin_p;
2404 struct sockaddr_in6 sin6;
2407 * Save original sockaddr_in6 addr and convert it
2410 sin6 = *(struct sockaddr_in6 *)nam;
2411 sin_p = (struct sockaddr_in *)nam;
2412 in6_sin6_2_sin(sin_p, &sin6);
2415 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2417 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2419 struct sockaddr_in *sin_p;
2420 struct sockaddr_in6 *sin6_p;
2422 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2424 sin_p = (struct sockaddr_in *)*nam;
2425 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2426 FREE(*nam, M_SONAME);
2427 *nam = (struct sockaddr *)sin6_p;