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.2 2003/06/17 04:28:52 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>
85 #include <net/if_types.h>
86 #include <net/route.h>
87 #include <net/if_dl.h>
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet/if_ether.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/ip.h>
95 #include <netinet/in_pcb.h>
98 #include <netinet/ip6.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/nd6.h>
101 #include <netinet6/mld6_var.h>
102 #include <netinet6/ip6_mroute.h>
103 #include <netinet6/in6_ifattach.h>
104 #include <netinet6/scope6_var.h>
105 #ifndef SCOPEDROUTING
106 #include <netinet6/in6_pcb.h>
109 #include <net/net_osdep.h>
111 MALLOC_DEFINE(M_IPMADDR, "in6_multi", "internet multicast address");
114 * Definitions of some costant IP6 addresses.
116 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
117 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
118 const struct in6_addr in6addr_nodelocal_allnodes =
119 IN6ADDR_NODELOCAL_ALLNODES_INIT;
120 const struct in6_addr in6addr_linklocal_allnodes =
121 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
122 const struct in6_addr in6addr_linklocal_allrouters =
123 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
125 const struct in6_addr in6mask0 = IN6MASK0;
126 const struct in6_addr in6mask32 = IN6MASK32;
127 const struct in6_addr in6mask64 = IN6MASK64;
128 const struct in6_addr in6mask96 = IN6MASK96;
129 const struct in6_addr in6mask128 = IN6MASK128;
131 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
132 0, 0, IN6ADDR_ANY_INIT, 0};
134 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
135 struct ifnet *, struct proc *));
136 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
137 struct sockaddr_in6 *, int));
138 static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
140 struct in6_multihead in6_multihead; /* XXX BSS initialization */
142 int (*faithprefix_p)(struct in6_addr *);
145 * Subroutine for in6_ifaddloop() and in6_ifremloop().
146 * This routine does actual work.
149 in6_ifloop_request(int cmd, struct ifaddr *ifa)
151 struct sockaddr_in6 all1_sa;
152 struct rtentry *nrt = NULL;
155 bzero(&all1_sa, sizeof(all1_sa));
156 all1_sa.sin6_family = AF_INET6;
157 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
158 all1_sa.sin6_addr = in6mask128;
161 * We specify the address itself as the gateway, and set the
162 * RTF_LLINFO flag, so that the corresponding host route would have
163 * the flag, and thus applications that assume traditional behavior
164 * would be happy. Note that we assume the caller of the function
165 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
166 * which changes the outgoing interface to the loopback interface.
168 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
169 (struct sockaddr *)&all1_sa,
170 RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
172 log(LOG_ERR, "in6_ifloop_request: "
173 "%s operation failed for %s (errno=%d)\n",
174 cmd == RTM_ADD ? "ADD" : "DELETE",
175 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
180 * Make sure rt_ifa be equal to IFA, the second argument of the
182 * We need this because when we refer to rt_ifa->ia6_flags in
183 * ip6_input, we assume that the rt_ifa points to the address instead
184 * of the loopback address.
186 if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) {
187 IFAFREE(nrt->rt_ifa);
193 * Report the addition/removal of the address to the routing socket.
194 * XXX: since we called rtinit for a p2p interface with a destination,
195 * we end up reporting twice in such a case. Should we rather
196 * omit the second report?
199 rt_newaddrmsg(cmd, ifa, e, nrt);
200 if (cmd == RTM_DELETE) {
201 if (nrt->rt_refcnt <= 0) {
202 /* XXX: we should free the entry ourselves. */
207 /* the cmd must be RTM_ADD here */
214 * Add ownaddr as loopback rtentry. We previously add the route only if
215 * necessary (ex. on a p2p link). However, since we now manage addresses
216 * separately from prefixes, we should always add the route. We can't
217 * rely on the cloning mechanism from the corresponding interface route
221 in6_ifaddloop(struct ifaddr *ifa)
225 /* If there is no loopback entry, allocate one. */
226 rt = rtalloc1(ifa->ifa_addr, 0, 0);
227 if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
228 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
229 in6_ifloop_request(RTM_ADD, ifa);
235 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
239 in6_ifremloop(struct ifaddr *ifa)
241 struct in6_ifaddr *ia;
246 * Some of BSD variants do not remove cloned routes
247 * from an interface direct route, when removing the direct route
248 * (see comments in net/net_osdep.h). Even for variants that do remove
249 * cloned routes, they could fail to remove the cloned routes when
250 * we handle multple addresses that share a common prefix.
251 * So, we should remove the route corresponding to the deleted address
252 * regardless of the result of in6_is_ifloop_auto().
256 * Delete the entry only if exact one ifa exists. More than one ifa
257 * can exist if we assign a same single address to multiple
258 * (probably p2p) interfaces.
259 * XXX: we should avoid such a configuration in IPv6...
261 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
262 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
271 * Before deleting, check if a corresponding loopbacked host
272 * route surely exists. With this check, we can avoid to
273 * delete an interface direct route whose destination is same
274 * as the address being removed. This can happen when remofing
275 * a subnet-router anycast address on an interface attahced
276 * to a shared medium.
278 rt = rtalloc1(ifa->ifa_addr, 0, 0);
279 if (rt != NULL && (rt->rt_flags & RTF_HOST) != 0 &&
280 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
282 in6_ifloop_request(RTM_DELETE, ifa);
288 in6_ifindex2scopeid(idx)
293 struct sockaddr_in6 *sin6;
295 if (idx < 0 || if_index < idx)
297 ifp = ifindex2ifnet[idx];
299 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
301 if (ifa->ifa_addr->sa_family != AF_INET6)
303 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
304 if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
305 return sin6->sin6_scope_id & 0xffff;
312 in6_mask2len(mask, lim0)
313 struct in6_addr *mask;
317 u_char *lim = lim0, *p;
320 lim0 - (u_char *)mask > sizeof(*mask)) /* ignore the scope_id part */
321 lim = (u_char *)mask + sizeof(*mask);
322 for (p = (u_char *)mask; p < lim; x++, p++) {
328 for (y = 0; y < 8; y++) {
329 if ((*p & (0x80 >> y)) == 0)
335 * when the limit pointer is given, do a stricter check on the
339 if (y != 0 && (*p & (0x00ff >> y)) != 0)
341 for (p = p + 1; p < lim; p++)
350 in6_len2mask(mask, len)
351 struct in6_addr *mask;
356 bzero(mask, sizeof(*mask));
357 for (i = 0; i < len / 8; i++)
358 mask->s6_addr8[i] = 0xff;
360 mask->s6_addr8[i] = (0xff00 >> (len % 8)) & 0xff;
363 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
364 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
367 in6_control(so, cmd, data, ifp, p)
374 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
375 struct in6_ifaddr *ia = NULL;
376 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
380 if (p == NULL || !suser(p))
384 case SIOCGETSGCNT_IN6:
385 case SIOCGETMIFCNT_IN6:
386 return (mrt6_ioctl(cmd, data));
393 case SIOCSNDFLUSH_IN6:
394 case SIOCSPFXFLUSH_IN6:
395 case SIOCSRTRFLUSH_IN6:
396 case SIOCSDEFIFACE_IN6:
397 case SIOCSIFINFO_FLAGS:
401 case OSIOCGIFINFO_IN6:
402 case SIOCGIFINFO_IN6:
405 case SIOCGNBRINFO_IN6:
406 case SIOCGDEFIFACE_IN6:
407 return(nd6_ioctl(cmd, data, ifp));
411 case SIOCSIFPREFIX_IN6:
412 case SIOCDIFPREFIX_IN6:
413 case SIOCAIFPREFIX_IN6:
414 case SIOCCIFPREFIX_IN6:
415 case SIOCSGIFPREFIX_IN6:
416 case SIOCGIFPREFIX_IN6:
418 "prefix ioctls are now invalidated. "
419 "please use ifconfig.\n");
427 return(scope6_set(ifp, ifr->ifr_ifru.ifru_scope_id));
430 return(scope6_get(ifp, ifr->ifr_ifru.ifru_scope_id));
433 return(scope6_get_default(ifr->ifr_ifru.ifru_scope_id));
444 return in6_lifaddr_ioctl(so, cmd, data, ifp, p);
448 * Find address for this interface, if it exists.
450 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
451 struct sockaddr_in6 *sa6 =
452 (struct sockaddr_in6 *)&ifra->ifra_addr;
454 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
455 if (sa6->sin6_addr.s6_addr16[1] == 0) {
456 /* link ID is not embedded by the user */
457 sa6->sin6_addr.s6_addr16[1] =
458 htons(ifp->if_index);
459 } else if (sa6->sin6_addr.s6_addr16[1] !=
460 htons(ifp->if_index)) {
461 return(EINVAL); /* link ID contradicts */
463 if (sa6->sin6_scope_id) {
464 if (sa6->sin6_scope_id !=
465 (u_int32_t)ifp->if_index)
467 sa6->sin6_scope_id = 0; /* XXX: good way? */
470 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
474 case SIOCSIFADDR_IN6:
475 case SIOCSIFDSTADDR_IN6:
476 case SIOCSIFNETMASK_IN6:
478 * Since IPv6 allows a node to assign multiple addresses
479 * on a single interface, SIOCSIFxxx ioctls are not suitable
480 * and should be unused.
482 /* we decided to obsolete this command (20000704) */
485 case SIOCDIFADDR_IN6:
487 * for IPv4, we look for existing in_ifaddr here to allow
488 * "ifconfig if0 delete" to remove first IPv4 address on the
489 * interface. For IPv6, as the spec allow multiple interface
490 * address from the day one, we consider "remove the first one"
491 * semantics to be not preferable.
494 return(EADDRNOTAVAIL);
496 case SIOCAIFADDR_IN6:
498 * We always require users to specify a valid IPv6 address for
499 * the corresponding operation.
501 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
502 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
503 return(EAFNOSUPPORT);
509 case SIOCGIFADDR_IN6:
510 /* This interface is basically deprecated. use SIOCGIFCONF. */
512 case SIOCGIFAFLAG_IN6:
513 case SIOCGIFNETMASK_IN6:
514 case SIOCGIFDSTADDR_IN6:
515 case SIOCGIFALIFETIME_IN6:
516 /* must think again about its semantics */
518 return(EADDRNOTAVAIL);
520 case SIOCSIFALIFETIME_IN6:
522 struct in6_addrlifetime *lt;
527 return(EADDRNOTAVAIL);
528 /* sanity for overflow - beware unsigned */
529 lt = &ifr->ifr_ifru.ifru_lifetime;
530 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
531 && lt->ia6t_vltime + time_second < time_second) {
534 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
535 && lt->ia6t_pltime + time_second < time_second) {
544 case SIOCGIFADDR_IN6:
545 ifr->ifr_addr = ia->ia_addr;
548 case SIOCGIFDSTADDR_IN6:
549 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
552 * XXX: should we check if ifa_dstaddr is NULL and return
555 ifr->ifr_dstaddr = ia->ia_dstaddr;
558 case SIOCGIFNETMASK_IN6:
559 ifr->ifr_addr = ia->ia_prefixmask;
562 case SIOCGIFAFLAG_IN6:
563 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
566 case SIOCGIFSTAT_IN6:
569 if (in6_ifstat == NULL || ifp->if_index >= in6_ifstatmax
570 || in6_ifstat[ifp->if_index] == NULL) {
571 /* return EAFNOSUPPORT? */
572 bzero(&ifr->ifr_ifru.ifru_stat,
573 sizeof(ifr->ifr_ifru.ifru_stat));
575 ifr->ifr_ifru.ifru_stat = *in6_ifstat[ifp->if_index];
578 case SIOCGIFSTAT_ICMP6:
581 if (icmp6_ifstat == NULL || ifp->if_index >= icmp6_ifstatmax ||
582 icmp6_ifstat[ifp->if_index] == NULL) {
583 /* return EAFNOSUPPORT? */
584 bzero(&ifr->ifr_ifru.ifru_stat,
585 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
587 ifr->ifr_ifru.ifru_icmp6stat =
588 *icmp6_ifstat[ifp->if_index];
591 case SIOCGIFALIFETIME_IN6:
592 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
595 case SIOCSIFALIFETIME_IN6:
596 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
598 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
599 ia->ia6_lifetime.ia6t_expire =
600 time_second + ia->ia6_lifetime.ia6t_vltime;
602 ia->ia6_lifetime.ia6t_expire = 0;
603 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
604 ia->ia6_lifetime.ia6t_preferred =
605 time_second + ia->ia6_lifetime.ia6t_pltime;
607 ia->ia6_lifetime.ia6t_preferred = 0;
610 case SIOCAIFADDR_IN6:
613 struct nd_prefix pr0, *pr;
616 * first, make or update the interface address structure,
617 * and link it to the list.
619 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
623 * then, make the prefix on-link on the interface.
624 * XXX: we'd rather create the prefix before the address, but
625 * we need at least one address to install the corresponding
626 * interface route, so we configure the address first.
630 * convert mask to prefix length (prefixmask has already
631 * been validated in in6_update_ifa().
633 bzero(&pr0, sizeof(pr0));
635 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
637 if (pr0.ndpr_plen == 128)
638 break; /* we don't need to install a host route. */
639 pr0.ndpr_prefix = ifra->ifra_addr;
640 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
641 /* apply the mask for safety. */
642 for (i = 0; i < 4; i++) {
643 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
644 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
647 * XXX: since we don't have an API to set prefix (not address)
648 * lifetimes, we just use the same lifetimes as addresses.
649 * The (temporarily) installed lifetimes can be overridden by
650 * later advertised RAs (when accept_rtadv is non 0), which is
651 * an intended behavior.
653 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
655 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
656 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
657 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
659 /* add the prefix if there's one. */
660 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
662 * nd6_prelist_add will install the corresponding
665 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
668 log(LOG_ERR, "nd6_prelist_add succedded but "
670 return(EINVAL); /* XXX panic here? */
673 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
675 /* XXX: this should not happen! */
676 log(LOG_ERR, "in6_control: addition succeeded, but"
679 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
680 ia->ia6_ndpr == NULL) { /* new autoconfed addr */
685 * If this is the first autoconf address from
686 * the prefix, create a temporary address
687 * as well (when specified).
689 if (ip6_use_tempaddr &&
690 pr->ndpr_refcnt == 1) {
692 if ((e = in6_tmpifadd(ia, 1)) != 0) {
693 log(LOG_NOTICE, "in6_control: "
694 "failed to create a "
695 "temporary address, "
703 * this might affect the status of autoconfigured
704 * addresses, that is, this address might make
705 * other addresses detached.
707 pfxlist_onlink_check();
712 case SIOCDIFADDR_IN6:
715 struct nd_prefix pr0, *pr;
718 * If the address being deleted is the only one that owns
719 * the corresponding prefix, expire the prefix as well.
720 * XXX: theoretically, we don't have to warry about such
721 * relationship, since we separate the address management
722 * and the prefix management. We do this, however, to provide
723 * as much backward compatibility as possible in terms of
724 * the ioctl operation.
726 bzero(&pr0, sizeof(pr0));
728 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
730 if (pr0.ndpr_plen == 128)
732 pr0.ndpr_prefix = ia->ia_addr;
733 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
734 for (i = 0; i < 4; i++) {
735 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
736 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
739 * The logic of the following condition is a bit complicated.
740 * We expire the prefix when
741 * 1. the address obeys autoconfiguration and it is the
742 * only owner of the associated prefix, or
743 * 2. the address does not obey autoconf and there is no
744 * other owner of the prefix.
746 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
747 (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
748 pr->ndpr_refcnt == 1) ||
749 ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
750 pr->ndpr_refcnt == 0))) {
751 pr->ndpr_expire = 1; /* XXX: just for expiration */
755 in6_purgeaddr(&ia->ia_ifa);
760 if (ifp == NULL || ifp->if_ioctl == 0)
762 return((*ifp->if_ioctl)(ifp, cmd, data));
769 * Update parameters of an IPv6 interface address.
770 * If necessary, a new entry is created and linked into address chains.
771 * This function is separated from in6_control().
772 * XXX: should this be performed under splnet()?
775 in6_update_ifa(ifp, ifra, ia)
777 struct in6_aliasreq *ifra;
778 struct in6_ifaddr *ia;
780 int error = 0, hostIsNew = 0, plen = -1;
781 struct in6_ifaddr *oia;
782 struct sockaddr_in6 dst6;
783 struct in6_addrlifetime *lt;
785 /* Validate parameters */
786 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
790 * The destination address for a p2p link must have a family
791 * of AF_UNSPEC or AF_INET6.
793 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
794 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
795 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
796 return(EAFNOSUPPORT);
798 * validate ifra_prefixmask. don't check sin6_family, netmask
799 * does not carry fields other than sin6_len.
801 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
804 * Because the IPv6 address architecture is classless, we require
805 * users to specify a (non 0) prefix length (mask) for a new address.
806 * We also require the prefix (when specified) mask is valid, and thus
807 * reject a non-consecutive mask.
809 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
811 if (ifra->ifra_prefixmask.sin6_len != 0) {
812 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
813 (u_char *)&ifra->ifra_prefixmask +
814 ifra->ifra_prefixmask.sin6_len);
820 * In this case, ia must not be NULL. We just use its prefix
823 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
826 * If the destination address on a p2p interface is specified,
827 * and the address is a scoped one, validate/set the scope
830 dst6 = ifra->ifra_dstaddr;
831 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
832 (dst6.sin6_family == AF_INET6)) {
835 #ifndef SCOPEDROUTING
836 if ((error = in6_recoverscope(&dst6,
837 &ifra->ifra_dstaddr.sin6_addr,
841 scopeid = in6_addr2scopeid(ifp, &dst6.sin6_addr);
842 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
843 dst6.sin6_scope_id = scopeid;
844 else if (dst6.sin6_scope_id != scopeid)
845 return(EINVAL); /* scope ID mismatch. */
846 #ifndef SCOPEDROUTING
847 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
850 dst6.sin6_scope_id = 0; /* XXX */
854 * The destination address can be specified only for a p2p or a
855 * loopback interface. If specified, the corresponding prefix length
858 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
859 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
860 /* XXX: noisy message */
861 log(LOG_INFO, "in6_update_ifa: a destination can be "
862 "specified for a p2p or a loopback IF only\n");
867 * The following message seems noisy, but we dare to
868 * add it for diagnosis.
870 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
871 "when dstaddr is specified\n");
875 /* lifetime consistency check */
876 lt = &ifra->ifra_lifetime;
877 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
878 && lt->ia6t_vltime + time_second < time_second) {
881 if (lt->ia6t_vltime == 0) {
883 * the following log might be noisy, but this is a typical
884 * configuration mistake or a tool's bug.
887 "in6_update_ifa: valid lifetime is 0 for %s\n",
888 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
890 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
891 && lt->ia6t_pltime + time_second < time_second) {
896 * If this is a new address, allocate a new ifaddr and link it
902 * When in6_update_ifa() is called in a process of a received
903 * RA, it is called under splnet(). So, we should call malloc
906 ia = (struct in6_ifaddr *)
907 malloc(sizeof(*ia), M_IFADDR, M_NOWAIT);
910 bzero((caddr_t)ia, sizeof(*ia));
911 /* Initialize the address and masks */
912 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
913 ia->ia_addr.sin6_family = AF_INET6;
914 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
915 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
917 * XXX: some functions expect that ifa_dstaddr is not
918 * NULL for p2p interfaces.
920 ia->ia_ifa.ifa_dstaddr
921 = (struct sockaddr *)&ia->ia_dstaddr;
923 ia->ia_ifa.ifa_dstaddr = NULL;
925 ia->ia_ifa.ifa_netmask
926 = (struct sockaddr *)&ia->ia_prefixmask;
929 if ((oia = in6_ifaddr) != NULL) {
930 for ( ; oia->ia_next; oia = oia->ia_next)
936 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
940 /* set prefix mask */
941 if (ifra->ifra_prefixmask.sin6_len) {
943 * We prohibit changing the prefix length of an existing
945 * + such an operation should be rare in IPv6, and
946 * + the operation would confuse prefix management.
948 if (ia->ia_prefixmask.sin6_len &&
949 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
950 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
951 " existing (%s) address should not be changed\n",
952 ip6_sprintf(&ia->ia_addr.sin6_addr));
956 ia->ia_prefixmask = ifra->ifra_prefixmask;
960 * If a new destination address is specified, scrub the old one and
961 * install the new destination. Note that the interface must be
962 * p2p or loopback (see the check above.)
964 if (dst6.sin6_family == AF_INET6 &&
965 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
966 &ia->ia_dstaddr.sin6_addr)) {
969 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
970 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
972 log(LOG_ERR, "in6_update_ifa: failed to remove "
973 "a route to the old destination: %s\n",
974 ip6_sprintf(&ia->ia_addr.sin6_addr));
975 /* proceed anyway... */
978 ia->ia_flags &= ~IFA_ROUTE;
979 ia->ia_dstaddr = dst6;
982 /* reset the interface and routing table appropriately. */
983 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
987 * Beyond this point, we should call in6_purgeaddr upon an error,
988 * not just go to unlink.
991 #if 0 /* disable this mechanism for now */
992 /* update prefix list */
994 (ifra->ifra_flags & IN6_IFF_NOPFX) == 0) { /* XXX */
997 iilen = (sizeof(ia->ia_prefixmask.sin6_addr) << 3) - plen;
998 if ((error = in6_prefix_add_ifid(iilen, ia)) != 0) {
999 in6_purgeaddr((struct ifaddr *)ia);
1005 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1006 struct sockaddr_in6 mltaddr, mltmask;
1007 struct in6_multi *in6m;
1011 * join solicited multicast addr for new host id
1013 struct in6_addr llsol;
1014 bzero(&llsol, sizeof(struct in6_addr));
1015 llsol.s6_addr16[0] = htons(0xff02);
1016 llsol.s6_addr16[1] = htons(ifp->if_index);
1017 llsol.s6_addr32[1] = 0;
1018 llsol.s6_addr32[2] = htonl(1);
1019 llsol.s6_addr32[3] =
1020 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1021 llsol.s6_addr8[12] = 0xff;
1022 (void)in6_addmulti(&llsol, ifp, &error);
1025 "in6_update_ifa: addmulti failed for "
1026 "%s on %s (errno=%d)\n",
1027 ip6_sprintf(&llsol), if_name(ifp),
1029 in6_purgeaddr((struct ifaddr *)ia);
1034 bzero(&mltmask, sizeof(mltmask));
1035 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1036 mltmask.sin6_family = AF_INET6;
1037 mltmask.sin6_addr = in6mask32;
1040 * join link-local all-nodes address
1042 bzero(&mltaddr, sizeof(mltaddr));
1043 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1044 mltaddr.sin6_family = AF_INET6;
1045 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1046 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1048 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1051 (struct sockaddr *)&mltaddr,
1052 (struct sockaddr *)&ia->ia_addr,
1053 (struct sockaddr *)&mltmask,
1054 RTF_UP|RTF_CLONING, /* xxx */
1055 (struct rtentry **)0);
1056 (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
1059 "in6_update_ifa: addmulti failed for "
1060 "%s on %s (errno=%d)\n",
1061 ip6_sprintf(&mltaddr.sin6_addr),
1062 if_name(ifp), error);
1067 * join node information group address
1069 #define hostnamelen strlen(hostname)
1070 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1072 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1073 if (in6m == NULL && ia != NULL) {
1074 (void)in6_addmulti(&mltaddr.sin6_addr,
1077 log(LOG_WARNING, "in6_update_ifa: "
1078 "addmulti failed for "
1079 "%s on %s (errno=%d)\n",
1080 ip6_sprintf(&mltaddr.sin6_addr),
1081 if_name(ifp), error);
1088 * join node-local all-nodes address, on loopback.
1089 * XXX: since "node-local" is obsoleted by interface-local,
1090 * we have to join the group on every interface with
1091 * some interface-boundary restriction.
1093 if (ifp->if_flags & IFF_LOOPBACK) {
1094 struct in6_ifaddr *ia_loop;
1096 struct in6_addr loop6 = in6addr_loopback;
1097 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1099 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1101 IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1102 if (in6m == NULL && ia_loop != NULL) {
1104 (struct sockaddr *)&mltaddr,
1105 (struct sockaddr *)&ia_loop->ia_addr,
1106 (struct sockaddr *)&mltmask,
1108 (struct rtentry **)0);
1109 (void)in6_addmulti(&mltaddr.sin6_addr, ifp,
1112 log(LOG_WARNING, "in6_update_ifa: "
1113 "addmulti failed for %s on %s "
1115 ip6_sprintf(&mltaddr.sin6_addr),
1116 if_name(ifp), error);
1122 ia->ia6_flags = ifra->ifra_flags;
1123 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1124 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1126 ia->ia6_lifetime = ifra->ifra_lifetime;
1128 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1129 ia->ia6_lifetime.ia6t_expire =
1130 time_second + ia->ia6_lifetime.ia6t_vltime;
1132 ia->ia6_lifetime.ia6t_expire = 0;
1133 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1134 ia->ia6_lifetime.ia6t_preferred =
1135 time_second + ia->ia6_lifetime.ia6t_pltime;
1137 ia->ia6_lifetime.ia6t_preferred = 0;
1140 * make sure to initialize ND6 information. this is to workaround
1141 * issues with interfaces with IPv6 addresses, which have never brought
1142 * up. We are assuming that it is safe to nd6_ifattach multiple times.
1147 * Perform DAD, if needed.
1148 * XXX It may be of use, if we can administratively
1151 if (in6if_do_dad(ifp) && (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
1152 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1153 nd6_dad_start((struct ifaddr *)ia, NULL);
1160 * XXX: if a change of an existing address failed, keep the entry
1164 in6_unlink_ifa(ia, ifp);
1172 struct ifnet *ifp = ifa->ifa_ifp;
1173 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1175 /* stop DAD processing */
1179 * delete route to the destination of the address being purged.
1180 * The interface must be p2p or loopback in this case.
1182 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1185 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1187 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1188 "a route to the p2p destination: %s on %s, "
1190 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1192 /* proceed anyway... */
1195 ia->ia_flags &= ~IFA_ROUTE;
1198 /* Remove ownaddr's loopback rtentry, if it exists. */
1199 in6_ifremloop(&(ia->ia_ifa));
1201 if (ifp->if_flags & IFF_MULTICAST) {
1203 * delete solicited multicast addr for deleting host id
1205 struct in6_multi *in6m;
1206 struct in6_addr llsol;
1207 bzero(&llsol, sizeof(struct in6_addr));
1208 llsol.s6_addr16[0] = htons(0xff02);
1209 llsol.s6_addr16[1] = htons(ifp->if_index);
1210 llsol.s6_addr32[1] = 0;
1211 llsol.s6_addr32[2] = htonl(1);
1212 llsol.s6_addr32[3] =
1213 ia->ia_addr.sin6_addr.s6_addr32[3];
1214 llsol.s6_addr8[12] = 0xff;
1216 IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1221 in6_unlink_ifa(ia, ifp);
1225 in6_unlink_ifa(ia, ifp)
1226 struct in6_ifaddr *ia;
1230 struct in6_ifaddr *oia;
1233 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1236 if (oia == (ia = in6_ifaddr))
1237 in6_ifaddr = ia->ia_next;
1239 while (ia->ia_next && (ia->ia_next != oia))
1242 ia->ia_next = oia->ia_next;
1245 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1249 if (oia->ia6_ifpr) { /* check for safety */
1250 plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1251 iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1252 in6_prefix_remove_ifid(iilen, oia);
1256 * When an autoconfigured address is being removed, release the
1257 * reference to the base prefix. Also, since the release might
1258 * affect the status of other (detached) addresses, call
1259 * pfxlist_onlink_check().
1261 if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1262 if (oia->ia6_ndpr == NULL) {
1263 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1264 "%p has no prefix\n", oia);
1266 oia->ia6_ndpr->ndpr_refcnt--;
1267 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1268 oia->ia6_ndpr = NULL;
1271 pfxlist_onlink_check();
1275 * release another refcnt for the link from in6_ifaddr.
1276 * Note that we should decrement the refcnt at least once for all *BSD.
1278 IFAFREE(&oia->ia_ifa);
1287 struct ifaddr *ifa, *nifa;
1289 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa)
1291 nifa = TAILQ_NEXT(ifa, ifa_list);
1292 if (ifa->ifa_addr->sa_family != AF_INET6)
1302 * SIOCGLIFADDR: get first address. (?)
1303 * SIOCGLIFADDR with IFLR_PREFIX:
1304 * get first address that matches the specified prefix.
1305 * SIOCALIFADDR: add the specified address.
1306 * SIOCALIFADDR with IFLR_PREFIX:
1307 * add the specified prefix, filling hostid part from
1308 * the first link-local address. prefixlen must be <= 64.
1309 * SIOCDLIFADDR: delete the specified address.
1310 * SIOCDLIFADDR with IFLR_PREFIX:
1311 * delete the first address that matches the specified prefix.
1313 * EINVAL on invalid parameters
1314 * EADDRNOTAVAIL on prefix match failed/specified address not found
1315 * other values may be returned from in6_ioctl()
1317 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1318 * this is to accomodate address naming scheme other than RFC2374,
1320 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1321 * address encoding scheme. (see figure on page 8)
1324 in6_lifaddr_ioctl(so, cmd, data, ifp, p)
1331 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1333 struct sockaddr *sa;
1336 if (!data || !ifp) {
1337 panic("invalid argument to in6_lifaddr_ioctl");
1343 /* address must be specified on GET with IFLR_PREFIX */
1344 if ((iflr->flags & IFLR_PREFIX) == 0)
1349 /* address must be specified on ADD and DELETE */
1350 sa = (struct sockaddr *)&iflr->addr;
1351 if (sa->sa_family != AF_INET6)
1353 if (sa->sa_len != sizeof(struct sockaddr_in6))
1355 /* XXX need improvement */
1356 sa = (struct sockaddr *)&iflr->dstaddr;
1357 if (sa->sa_family && sa->sa_family != AF_INET6)
1359 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1362 default: /* shouldn't happen */
1364 panic("invalid cmd to in6_lifaddr_ioctl");
1370 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1376 struct in6_aliasreq ifra;
1377 struct in6_addr *hostid = NULL;
1380 if ((iflr->flags & IFLR_PREFIX) != 0) {
1381 struct sockaddr_in6 *sin6;
1384 * hostid is to fill in the hostid part of the
1385 * address. hostid points to the first link-local
1386 * address attached to the interface.
1388 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1390 return EADDRNOTAVAIL;
1391 hostid = IFA_IN6(ifa);
1393 /* prefixlen must be <= 64. */
1394 if (64 < iflr->prefixlen)
1396 prefixlen = iflr->prefixlen;
1398 /* hostid part must be zero. */
1399 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1400 if (sin6->sin6_addr.s6_addr32[2] != 0
1401 || sin6->sin6_addr.s6_addr32[3] != 0) {
1405 prefixlen = iflr->prefixlen;
1407 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1408 bzero(&ifra, sizeof(ifra));
1409 bcopy(iflr->iflr_name, ifra.ifra_name,
1410 sizeof(ifra.ifra_name));
1412 bcopy(&iflr->addr, &ifra.ifra_addr,
1413 ((struct sockaddr *)&iflr->addr)->sa_len);
1415 /* fill in hostid part */
1416 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1417 hostid->s6_addr32[2];
1418 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1419 hostid->s6_addr32[3];
1422 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1423 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1424 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1426 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1427 hostid->s6_addr32[2];
1428 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1429 hostid->s6_addr32[3];
1433 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1434 in6_len2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1436 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1437 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, p);
1442 struct in6_ifaddr *ia;
1443 struct in6_addr mask, candidate, match;
1444 struct sockaddr_in6 *sin6;
1447 bzero(&mask, sizeof(mask));
1448 if (iflr->flags & IFLR_PREFIX) {
1449 /* lookup a prefix rather than address. */
1450 in6_len2mask(&mask, iflr->prefixlen);
1452 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1453 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1454 match.s6_addr32[0] &= mask.s6_addr32[0];
1455 match.s6_addr32[1] &= mask.s6_addr32[1];
1456 match.s6_addr32[2] &= mask.s6_addr32[2];
1457 match.s6_addr32[3] &= mask.s6_addr32[3];
1459 /* if you set extra bits, that's wrong */
1460 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1465 if (cmd == SIOCGLIFADDR) {
1466 /* on getting an address, take the 1st match */
1469 /* on deleting an address, do exact match */
1470 in6_len2mask(&mask, 128);
1471 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1472 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1478 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1480 if (ifa->ifa_addr->sa_family != AF_INET6)
1485 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1486 #ifndef SCOPEDROUTING
1488 * XXX: this is adhoc, but is necessary to allow
1489 * a user to specify fe80::/64 (not /10) for a
1490 * link-local address.
1492 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1493 candidate.s6_addr16[1] = 0;
1495 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1496 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1497 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1498 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1499 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1503 return EADDRNOTAVAIL;
1506 if (cmd == SIOCGLIFADDR) {
1507 #ifndef SCOPEDROUTING
1508 struct sockaddr_in6 *s6;
1511 /* fill in the if_laddrreq structure */
1512 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1513 #ifndef SCOPEDROUTING /* XXX see above */
1514 s6 = (struct sockaddr_in6 *)&iflr->addr;
1515 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1516 s6->sin6_addr.s6_addr16[1] = 0;
1518 in6_addr2scopeid(ifp, &s6->sin6_addr);
1521 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1522 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1523 ia->ia_dstaddr.sin6_len);
1524 #ifndef SCOPEDROUTING /* XXX see above */
1525 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1526 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1527 s6->sin6_addr.s6_addr16[1] = 0;
1529 in6_addr2scopeid(ifp,
1534 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1537 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1540 iflr->flags = ia->ia6_flags; /* XXX */
1544 struct in6_aliasreq ifra;
1546 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1547 bzero(&ifra, sizeof(ifra));
1548 bcopy(iflr->iflr_name, ifra.ifra_name,
1549 sizeof(ifra.ifra_name));
1551 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1552 ia->ia_addr.sin6_len);
1553 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1554 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1555 ia->ia_dstaddr.sin6_len);
1557 bzero(&ifra.ifra_dstaddr,
1558 sizeof(ifra.ifra_dstaddr));
1560 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1561 ia->ia_prefixmask.sin6_len);
1563 ifra.ifra_flags = ia->ia6_flags;
1564 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1570 return EOPNOTSUPP; /* just for safety */
1574 * Initialize an interface's intetnet6 address
1575 * and routing table entry.
1578 in6_ifinit(ifp, ia, sin6, newhost)
1580 struct in6_ifaddr *ia;
1581 struct sockaddr_in6 *sin6;
1584 int error = 0, plen, ifacount = 0;
1589 * Give the interface a chance to initialize
1590 * if this is its first address,
1591 * and to validate the address if necessary.
1593 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1595 if (ifa->ifa_addr == NULL)
1596 continue; /* just for safety */
1597 if (ifa->ifa_addr->sa_family != AF_INET6)
1602 ia->ia_addr = *sin6;
1604 if (ifacount <= 1 && ifp->if_ioctl &&
1605 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1611 ia->ia_ifa.ifa_metric = ifp->if_metric;
1613 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1617 * If the destination address is specified for a point-to-point
1618 * interface, install a route to the destination as an interface
1621 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1622 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1623 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1624 RTF_UP | RTF_HOST)) != 0)
1626 ia->ia_flags |= IFA_ROUTE;
1630 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1632 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1635 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1637 /* set the rtrequest function to create llinfo */
1638 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1639 in6_ifaddloop(&(ia->ia_ifa));
1646 * Add an address to the list of IP6 multicast addresses for a
1650 in6_addmulti(maddr6, ifp, errorp)
1651 struct in6_addr *maddr6;
1655 struct in6_multi *in6m;
1656 struct sockaddr_in6 sin6;
1657 struct ifmultiaddr *ifma;
1663 * Call generic routine to add membership or increment
1664 * refcount. It wants addresses in the form of a sockaddr,
1665 * so we build one here (being careful to zero the unused bytes).
1667 bzero(&sin6, sizeof sin6);
1668 sin6.sin6_family = AF_INET6;
1669 sin6.sin6_len = sizeof sin6;
1670 sin6.sin6_addr = *maddr6;
1671 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1678 * If ifma->ifma_protospec is null, then if_addmulti() created
1679 * a new record. Otherwise, we are done.
1681 if (ifma->ifma_protospec != 0)
1682 return ifma->ifma_protospec;
1684 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
1685 at interrupt time? If so, need to fix if_addmulti. XXX */
1686 in6m = (struct in6_multi *)malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1692 bzero(in6m, sizeof *in6m);
1693 in6m->in6m_addr = *maddr6;
1694 in6m->in6m_ifp = ifp;
1695 in6m->in6m_ifma = ifma;
1696 ifma->ifma_protospec = in6m;
1697 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1700 * Let MLD6 know that we have joined a new IP6 multicast
1703 mld6_start_listening(in6m);
1709 * Delete a multicast address record.
1713 struct in6_multi *in6m;
1715 struct ifmultiaddr *ifma = in6m->in6m_ifma;
1718 if (ifma->ifma_refcount == 1) {
1720 * No remaining claims to this record; let MLD6 know
1721 * that we are leaving the multicast group.
1723 mld6_stop_listening(in6m);
1724 ifma->ifma_protospec = 0;
1725 LIST_REMOVE(in6m, in6m_entry);
1726 free(in6m, M_IPMADDR);
1728 /* XXX - should be separate API for when we have an ifma? */
1729 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1734 * Find an IPv6 interface link-local address specific to an interface.
1737 in6ifa_ifpforlinklocal(ifp, ignoreflags)
1743 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1745 if (ifa->ifa_addr == NULL)
1746 continue; /* just for safety */
1747 if (ifa->ifa_addr->sa_family != AF_INET6)
1749 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1750 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1757 return((struct in6_ifaddr *)ifa);
1762 * find the internet address corresponding to a given interface and address.
1765 in6ifa_ifpwithaddr(ifp, addr)
1767 struct in6_addr *addr;
1771 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1773 if (ifa->ifa_addr == NULL)
1774 continue; /* just for safety */
1775 if (ifa->ifa_addr->sa_family != AF_INET6)
1777 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1781 return((struct in6_ifaddr *)ifa);
1785 * Convert IP6 address to printable (loggable) representation.
1787 static char digits[] = "0123456789abcdef";
1788 static int ip6round = 0;
1791 const struct in6_addr *addr;
1793 static char ip6buf[8][48];
1796 u_short *a = (u_short *)addr;
1800 ip6round = (ip6round + 1) & 7;
1801 cp = ip6buf[ip6round];
1803 for (i = 0; i < 8; i++) {
1814 if (dcolon == 0 && *(a + 1) == 0) {
1827 *cp++ = digits[*d >> 4];
1828 *cp++ = digits[*d++ & 0xf];
1829 *cp++ = digits[*d >> 4];
1830 *cp++ = digits[*d & 0xf];
1835 return(ip6buf[ip6round]);
1840 struct in6_addr *in6;
1842 struct in6_ifaddr *ia;
1844 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1847 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1848 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1849 &ia->ia_prefixmask.sin6_addr))
1856 in6_is_addr_deprecated(sa6)
1857 struct sockaddr_in6 *sa6;
1859 struct in6_ifaddr *ia;
1861 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1862 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1864 #ifdef SCOPEDROUTING
1865 ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id &&
1867 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1868 return(1); /* true */
1870 /* XXX: do we still have to go thru the rest of the list? */
1873 return(0); /* false */
1877 * return length of part which dst and src are equal
1881 in6_matchlen(src, dst)
1882 struct in6_addr *src, *dst;
1885 u_char *s = (u_char *)src, *d = (u_char *)dst;
1886 u_char *lim = s + 16, r;
1889 if ((r = (*d++ ^ *s++)) != 0) {
1900 /* XXX: to be scope conscious */
1902 in6_are_prefix_equal(p1, p2, len)
1903 struct in6_addr *p1, *p2;
1906 int bytelen, bitlen;
1909 if (0 > len || len > 128) {
1910 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1918 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1920 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1921 p2->s6_addr[bytelen] >> (8 - bitlen))
1928 in6_prefixlen2mask(maskp, len)
1929 struct in6_addr *maskp;
1932 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1933 int bytelen, bitlen, i;
1936 if (0 > len || len > 128) {
1937 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1942 bzero(maskp, sizeof(*maskp));
1945 for (i = 0; i < bytelen; i++)
1946 maskp->s6_addr[i] = 0xff;
1948 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1952 * return the best address out of the same scope
1955 in6_ifawithscope(oifp, dst)
1957 struct in6_addr *dst;
1959 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
1963 struct in6_ifaddr *ifa_best = NULL;
1967 printf("in6_ifawithscope: output interface is not specified\n");
1973 * We search for all addresses on all interfaces from the beginning.
1974 * Comparing an interface with the outgoing interface will be done
1975 * only at the final stage of tiebreaking.
1977 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1980 * We can never take an address that breaks the scope zone
1981 * of the destination.
1983 if (in6_addr2scopeid(ifp, dst) != in6_addr2scopeid(oifp, dst))
1986 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1988 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
1990 if (ifa->ifa_addr->sa_family != AF_INET6)
1993 src_scope = in6_addrscope(IFA_IN6(ifa));
1996 * Don't use an address before completing DAD
1997 * nor a duplicated address.
1999 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2003 /* XXX: is there any case to allow anycasts? */
2004 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2008 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2013 * If this is the first address we find,
2016 if (ifa_best == NULL)
2020 * ifa_best is never NULL beyond this line except
2021 * within the block labeled "replace".
2025 * If ifa_best has a smaller scope than dst and
2026 * the current address has a larger one than
2027 * (or equal to) dst, always replace ifa_best.
2028 * Also, if the current address has a smaller scope
2029 * than dst, ignore it unless ifa_best also has a
2031 * Consequently, after the two if-clause below,
2032 * the followings must be satisfied:
2033 * (scope(src) < scope(dst) &&
2034 * scope(best) < scope(dst))
2036 * (scope(best) >= scope(dst) &&
2037 * scope(src) >= scope(dst))
2039 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2040 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2041 goto replace; /* (A) */
2042 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2043 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2047 * A deprecated address SHOULD NOT be used in new
2048 * communications if an alternate (non-deprecated)
2049 * address is available and has sufficient scope.
2050 * RFC 2462, Section 5.5.4.
2052 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2053 IN6_IFF_DEPRECATED) {
2055 * Ignore any deprecated addresses if
2056 * specified by configuration.
2058 if (!ip6_use_deprecated)
2062 * If we have already found a non-deprecated
2063 * candidate, just ignore deprecated addresses.
2065 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
2071 * A non-deprecated address is always preferred
2072 * to a deprecated one regardless of scopes and
2073 * address matching (Note invariants ensured by the
2074 * conditions (A) and (B) above.)
2076 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
2077 (((struct in6_ifaddr *)ifa)->ia6_flags &
2078 IN6_IFF_DEPRECATED) == 0)
2082 * When we use temporary addresses described in
2083 * RFC 3041, we prefer temporary addresses to
2084 * public autoconf addresses. Again, note the
2085 * invariants from (A) and (B). Also note that we
2086 * don't have any preference between static addresses
2087 * and autoconf addresses (despite of whether or not
2088 * the latter is temporary or public.)
2090 if (ip6_use_tempaddr) {
2091 struct in6_ifaddr *ifat;
2093 ifat = (struct in6_ifaddr *)ifa;
2094 if ((ifa_best->ia6_flags &
2095 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2096 == IN6_IFF_AUTOCONF &&
2098 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2099 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2102 if ((ifa_best->ia6_flags &
2103 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2104 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2106 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2107 == IN6_IFF_AUTOCONF) {
2113 * At this point, we have two cases:
2114 * 1. we are looking at a non-deprecated address,
2115 * and ifa_best is also non-deprecated.
2116 * 2. we are looking at a deprecated address,
2117 * and ifa_best is also deprecated.
2118 * Also, we do not have to consider a case where
2119 * the scope of if_best is larger(smaller) than dst and
2120 * the scope of the current address is smaller(larger)
2121 * than dst. Such a case has already been covered.
2122 * Tiebreaking is done according to the following
2124 * - the scope comparison between the address and
2126 * - the scope comparison between the address and
2127 * ifa_best (bscopecmp)
2128 * - if the address match dst longer than ifa_best
2130 * - if the address is on the outgoing I/F (outI/F)
2132 * Roughly speaking, the selection policy is
2133 * - the most important item is scope. The same scope
2134 * is best. Then search for a larger scope.
2135 * Smaller scopes are the last resort.
2136 * - A deprecated address is chosen only when we have
2137 * no address that has an enough scope, but is
2138 * prefered to any addresses of smaller scopes
2139 * (this must be already done above.)
2140 * - addresses on the outgoing I/F are preferred to
2141 * ones on other interfaces if none of above
2142 * tiebreaks. In the table below, the column "bI"
2143 * means if the best_ifa is on the outgoing
2144 * interface, and the column "sI" means if the ifa
2145 * is on the outgoing interface.
2146 * - If there is no other reasons to choose one,
2147 * longest address match against dst is considered.
2149 * The precise decision table is as follows:
2150 * dscopecmp bscopecmp match bI oI | replace?
2151 * N/A equal N/A Y N | No (1)
2152 * N/A equal N/A N Y | Yes (2)
2153 * N/A equal larger N/A | Yes (3)
2154 * N/A equal !larger N/A | No (4)
2155 * larger larger N/A N/A | No (5)
2156 * larger smaller N/A N/A | Yes (6)
2157 * smaller larger N/A N/A | Yes (7)
2158 * smaller smaller N/A N/A | No (8)
2159 * equal smaller N/A N/A | Yes (9)
2160 * equal larger (already done at A above)
2162 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2163 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2165 if (bscopecmp == 0) {
2166 struct ifnet *bifp = ifa_best->ia_ifp;
2168 if (bifp == oifp && ifp != oifp) /* (1) */
2170 if (bifp != oifp && ifp == oifp) /* (2) */
2174 * Both bifp and ifp are on the outgoing
2175 * interface, or both two are on a different
2176 * interface from the outgoing I/F.
2177 * now we need address matching against dst
2180 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2181 matchcmp = tlen - blen;
2182 if (matchcmp > 0) /* (3) */
2186 if (dscopecmp > 0) {
2187 if (bscopecmp > 0) /* (5) */
2189 goto replace; /* (6) */
2191 if (dscopecmp < 0) {
2192 if (bscopecmp > 0) /* (7) */
2197 /* now dscopecmp must be 0 */
2199 goto replace; /* (9) */
2202 ifa_best = (struct in6_ifaddr *)ifa;
2203 blen = tlen >= 0 ? tlen :
2204 in6_matchlen(IFA_IN6(ifa), dst);
2205 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2209 /* count statistics for future improvements */
2210 if (ifa_best == NULL)
2211 ip6stat.ip6s_sources_none++;
2213 if (oifp == ifa_best->ia_ifp)
2214 ip6stat.ip6s_sources_sameif[best_scope]++;
2216 ip6stat.ip6s_sources_otherif[best_scope]++;
2218 if (best_scope == dst_scope)
2219 ip6stat.ip6s_sources_samescope[best_scope]++;
2221 ip6stat.ip6s_sources_otherscope[best_scope]++;
2223 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2224 ip6stat.ip6s_sources_deprecated[best_scope]++;
2231 * return the best address out of the same scope. if no address was
2232 * found, return the first valid address from designated IF.
2235 in6_ifawithifp(ifp, dst)
2237 struct in6_addr *dst;
2239 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2241 struct in6_ifaddr *besta = 0;
2242 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2244 dep[0] = dep[1] = NULL;
2247 * We first look for addresses in the same scope.
2248 * If there is one, return it.
2249 * If two or more, return one which matches the dst longest.
2250 * If none, return one of global addresses assigned other ifs.
2252 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2254 if (ifa->ifa_addr->sa_family != AF_INET6)
2256 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2257 continue; /* XXX: is there any case to allow anycast? */
2258 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2259 continue; /* don't use this interface */
2260 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2262 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2263 if (ip6_use_deprecated)
2264 dep[0] = (struct in6_ifaddr *)ifa;
2268 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2270 * call in6_matchlen() as few as possible
2274 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2275 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2278 besta = (struct in6_ifaddr *)ifa;
2281 besta = (struct in6_ifaddr *)ifa;
2287 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2289 if (ifa->ifa_addr->sa_family != AF_INET6)
2291 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2292 continue; /* XXX: is there any case to allow anycast? */
2293 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2294 continue; /* don't use this interface */
2295 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2297 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2298 if (ip6_use_deprecated)
2299 dep[1] = (struct in6_ifaddr *)ifa;
2303 return (struct in6_ifaddr *)ifa;
2306 /* use the last-resort values, that are, deprecated addresses */
2316 * perform DAD when interface becomes IFF_UP.
2323 struct in6_ifaddr *ia;
2324 int dad_delay; /* delay ticks before DAD output */
2327 * special cases, like 6to4, are handled in in6_ifattach
2329 in6_ifattach(ifp, NULL);
2332 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
2334 if (ifa->ifa_addr->sa_family != AF_INET6)
2336 ia = (struct in6_ifaddr *)ifa;
2337 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2338 nd6_dad_start(ifa, &dad_delay);
2346 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2349 switch (ifp->if_type) {
2355 * These interfaces do not have the IFF_LOOPBACK flag,
2356 * but loop packets back. We do not have to do DAD on such
2357 * interfaces. We should even omit it, because loop-backed
2358 * NS would confuse the DAD procedure.
2363 * Our DAD routine requires the interface up and running.
2364 * However, some interfaces can be up before the RUNNING
2365 * status. Additionaly, users may try to assign addresses
2366 * before the interface becomes up (or running).
2367 * We simply skip DAD in such a case as a work around.
2368 * XXX: we should rather mark "tentative" on such addresses,
2369 * and do DAD after the interface becomes ready.
2371 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2372 (IFF_UP|IFF_RUNNING))
2380 * Calculate max IPv6 MTU through all the interfaces and store it
2386 unsigned long maxmtu = 0;
2389 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
2391 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2392 nd_ifinfo[ifp->if_index].linkmtu > maxmtu)
2393 maxmtu = nd_ifinfo[ifp->if_index].linkmtu;
2395 if (maxmtu) /* update only when maxmtu is positive */
2396 in6_maxmtu = maxmtu;
2400 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2401 * v4 mapped addr or v4 compat addr
2404 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2406 bzero(sin, sizeof(*sin));
2407 sin->sin_len = sizeof(struct sockaddr_in);
2408 sin->sin_family = AF_INET;
2409 sin->sin_port = sin6->sin6_port;
2410 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2413 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2415 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2417 bzero(sin6, sizeof(*sin6));
2418 sin6->sin6_len = sizeof(struct sockaddr_in6);
2419 sin6->sin6_family = AF_INET6;
2420 sin6->sin6_port = sin->sin_port;
2421 sin6->sin6_addr.s6_addr32[0] = 0;
2422 sin6->sin6_addr.s6_addr32[1] = 0;
2423 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2424 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2427 /* Convert sockaddr_in6 into sockaddr_in. */
2429 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2431 struct sockaddr_in *sin_p;
2432 struct sockaddr_in6 sin6;
2435 * Save original sockaddr_in6 addr and convert it
2438 sin6 = *(struct sockaddr_in6 *)nam;
2439 sin_p = (struct sockaddr_in *)nam;
2440 in6_sin6_2_sin(sin_p, &sin6);
2443 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2445 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2447 struct sockaddr_in *sin_p;
2448 struct sockaddr_in6 *sin6_p;
2450 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2452 sin_p = (struct sockaddr_in *)*nam;
2453 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2454 FREE(*nam, M_SONAME);
2455 *nam = (struct sockaddr *)sin6_p;