2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $
33 * Copyright (c) 1982, 1986, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)in.c 8.2 (Berkeley) 11/15/93
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
69 #include <sys/param.h>
70 #include <sys/errno.h>
71 #include <sys/malloc.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/sockio.h>
75 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/syslog.h>
83 #include <net/if_types.h>
84 #include <net/route.h>
85 #include <net/if_dl.h>
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/if_ether.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/ip.h>
92 #include <netinet/in_pcb.h>
94 #include <netinet/ip6.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/nd6.h>
97 #include <netinet6/mld6_var.h>
98 #include <netinet6/ip6_mroute.h>
99 #include <netinet6/in6_ifattach.h>
100 #include <netinet6/scope6_var.h>
101 #include <netinet6/in6_pcb.h>
103 MALLOC_DEFINE(M_IP6MADDR, "in6_multi", "internet multicast address");
106 * Definitions of some costant IP6 addresses.
108 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
109 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
110 const struct in6_addr in6addr_nodelocal_allnodes =
111 IN6ADDR_NODELOCAL_ALLNODES_INIT;
112 const struct in6_addr in6addr_linklocal_allnodes =
113 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
114 const struct in6_addr in6addr_linklocal_allrouters =
115 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
117 const struct in6_addr in6mask0 = IN6MASK0;
118 const struct in6_addr in6mask32 = IN6MASK32;
119 const struct in6_addr in6mask64 = IN6MASK64;
120 const struct in6_addr in6mask96 = IN6MASK96;
121 const struct in6_addr in6mask128 = IN6MASK128;
123 const struct sockaddr_in6 sa6_any =
124 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
126 static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
127 struct ifnet *, struct thread *));
128 static int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
129 struct sockaddr_in6 *, int));
130 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
132 struct in6_multihead in6_multihead; /* XXX BSS initialization */
133 int (*faithprefix_p)(struct in6_addr *);
136 * Subroutine for in6_ifaddloop() and in6_ifremloop().
137 * This routine does actual work.
140 in6_ifloop_request(int cmd, struct ifaddr *ifa)
142 struct sockaddr_in6 all1_sa;
143 struct rtentry *nrt = NULL;
145 char ip6buf[INET6_ADDRSTRLEN];
147 bzero(&all1_sa, sizeof(all1_sa));
148 all1_sa.sin6_family = AF_INET6;
149 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
150 all1_sa.sin6_addr = in6mask128;
153 * We specify the address itself as the gateway, and set the
154 * RTF_LLINFO flag, so that the corresponding host route would have
155 * the flag, and thus applications that assume traditional behavior
156 * would be happy. Note that we assume the caller of the function
157 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
158 * which changes the outgoing interface to the loopback interface.
160 e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
161 (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
163 /* XXX need more descriptive message */
165 log(LOG_ERR, "in6_ifloop_request: "
166 "%s operation failed for %s (errno=%d)\n",
167 cmd == RTM_ADD ? "ADD" : "DELETE",
169 &((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr), e);
173 * Report the addition/removal of the address to the routing socket.
174 * XXX: since we called rtinit for a p2p interface with a destination,
175 * we end up reporting twice in such a case. Should we rather
176 * omit the second report?
181 * Make sure rt_ifa be equal to IFA, the second argument of
182 * the function. We need this because when we refer to
183 * rt_ifa->ia6_flags in ip6_input, we assume that the rt_ifa
184 * points to the address instead of the loopback address.
186 if (cmd == RTM_ADD && ifa != nrt->rt_ifa) {
187 IFAFREE(nrt->rt_ifa);
192 rt_newaddrmsg(cmd, ifa, e, nrt);
193 if (cmd == RTM_DELETE)
196 /* the cmd must be RTM_ADD here */
204 * Add ownaddr as loopback rtentry. We previously add the route only if
205 * necessary (ex. on a p2p link). However, since we now manage addresses
206 * separately from prefixes, we should always add the route. We can't
207 * rely on the cloning mechanism from the corresponding interface route
211 in6_ifaddloop(struct ifaddr *ifa)
216 /* If there is no loopback entry, allocate one. */
217 rt = rtalloc1(ifa->ifa_addr, 0, 0);
218 need_loop = (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
219 (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0);
223 in6_ifloop_request(RTM_ADD, ifa);
227 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
231 in6_ifremloop(struct ifaddr *ifa)
233 struct in6_ifaddr *ia;
238 * Some of BSD variants do not remove cloned routes
239 * from an interface direct route, when removing the direct route
240 * (see comments in net/net_osdep.h). Even for variants that do remove
241 * cloned routes, they could fail to remove the cloned routes when
242 * we handle multple addresses that share a common prefix.
243 * So, we should remove the route corresponding to the deleted address.
247 * Delete the entry only if exact one ifa exists. More than one ifa
248 * can exist if we assign a same single address to multiple
249 * (probably p2p) interfaces.
250 * XXX: we should avoid such a configuration in IPv6...
252 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
253 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
262 * Before deleting, check if a corresponding loopbacked host
263 * route surely exists. With this check, we can avoid to
264 * delete an interface direct route whose destination is same
265 * as the address being removed. This can happen when removing
266 * a subnet-router anycast address on an interface attahced
267 * to a shared medium.
269 rt = rtalloc1(ifa->ifa_addr, 0, 0);
271 if ((rt->rt_flags & RTF_HOST) != 0 &&
272 (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
274 in6_ifloop_request(RTM_DELETE, ifa);
282 in6_mask2len(struct in6_addr *mask, u_char *lim0)
285 u_char *lim = lim0, *p;
287 /* ignore the scope_id part */
288 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
289 lim = (u_char *)mask + sizeof(*mask);
290 for (p = (u_char *)mask; p < lim; x++, p++) {
296 for (y = 0; y < 8; y++) {
297 if ((*p & (0x80 >> y)) == 0)
303 * when the limit pointer is given, do a stricter check on the
307 if (y != 0 && (*p & (0x00ff >> y)) != 0)
309 for (p = p + 1; p < lim; p++)
317 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
318 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
321 in6_control(struct socket *so, u_long cmd, caddr_t data,
322 struct ifnet *ifp, struct thread *td)
324 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
325 struct in6_ifaddr *ia = NULL;
326 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
327 struct sockaddr_in6 *sa6;
331 case SIOCGETSGCNT_IN6:
332 case SIOCGETMIFCNT_IN6:
333 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
337 case SIOCAADDRCTL_POLICY:
338 case SIOCDADDRCTL_POLICY:
340 error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
344 return (in6_src_ioctl(cmd, data));
351 case SIOCSNDFLUSH_IN6:
352 case SIOCSPFXFLUSH_IN6:
353 case SIOCSRTRFLUSH_IN6:
354 case SIOCSDEFIFACE_IN6:
355 case SIOCSIFINFO_FLAGS:
357 error = priv_check(td, PRIV_NETINET_ND6);
362 case OSIOCGIFINFO_IN6:
363 case SIOCGIFINFO_IN6:
364 case SIOCSIFINFO_IN6:
367 case SIOCGNBRINFO_IN6:
368 case SIOCGDEFIFACE_IN6:
369 return (nd6_ioctl(cmd, data, ifp));
373 case SIOCSIFPREFIX_IN6:
374 case SIOCDIFPREFIX_IN6:
375 case SIOCAIFPREFIX_IN6:
376 case SIOCCIFPREFIX_IN6:
377 case SIOCSGIFPREFIX_IN6:
378 case SIOCGIFPREFIX_IN6:
380 "prefix ioctls are now invalidated. "
381 "please use ifconfig.\n");
388 error = priv_check(td, PRIV_NETINET_SCOPE6);
392 return (scope6_set(ifp,
393 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
395 return (scope6_get(ifp,
396 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
398 return (scope6_get_default((struct scope6_id *)
399 ifr->ifr_ifru.ifru_scope_id));
406 * XXXRW: Is this checked at another layer? What priv to use
416 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
420 * Find address for this interface, if it exists.
422 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
423 * only, and used the first interface address as the target of other
424 * operations (without checking ifra_addr). This was because netinet
425 * code/API assumed at most 1 interface address per interface.
426 * Since IPv6 allows a node to assign multiple addresses
427 * on a single interface, we almost always look and check the
428 * presence of ifra_addr, and reject invalid ones here.
429 * It also decreases duplicated code among SIOC*_IN6 operations.
432 case SIOCAIFADDR_IN6:
433 case SIOCSIFPHYADDR_IN6:
434 sa6 = &ifra->ifra_addr;
436 case SIOCSIFADDR_IN6:
437 case SIOCGIFADDR_IN6:
438 case SIOCSIFDSTADDR_IN6:
439 case SIOCSIFNETMASK_IN6:
440 case SIOCGIFDSTADDR_IN6:
441 case SIOCGIFNETMASK_IN6:
442 case SIOCDIFADDR_IN6:
443 case SIOCGIFPSRCADDR_IN6:
444 case SIOCGIFPDSTADDR_IN6:
445 case SIOCGIFAFLAG_IN6:
446 case SIOCSNDFLUSH_IN6:
447 case SIOCSPFXFLUSH_IN6:
448 case SIOCSRTRFLUSH_IN6:
449 case SIOCGIFALIFETIME_IN6:
450 case SIOCSIFALIFETIME_IN6:
451 case SIOCGIFSTAT_IN6:
452 case SIOCGIFSTAT_ICMP6:
453 sa6 = &ifr->ifr_addr;
459 if (sa6 && sa6->sin6_family == AF_INET6) {
462 if (sa6->sin6_scope_id != 0)
463 error = sa6_embedscope(sa6, 0);
465 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
468 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
473 case SIOCSIFADDR_IN6:
474 case SIOCSIFDSTADDR_IN6:
475 case SIOCSIFNETMASK_IN6:
477 * Since IPv6 allows a node to assign multiple addresses
478 * on a single interface, SIOCSIFxxx ioctls are deprecated.
480 /* we decided to obsolete this command (20000704) */
483 case SIOCDIFADDR_IN6:
485 * for IPv4, we look for existing in_ifaddr here to allow
486 * "ifconfig if0 delete" to remove the first IPv4 address on
487 * the interface. For IPv6, as the spec allows multiple
488 * interface address from the day one, we consider "remove the
489 * first one" semantics to be not preferable.
492 return (EADDRNOTAVAIL);
494 case SIOCAIFADDR_IN6:
496 * We always require users to specify a valid IPv6 address for
497 * the corresponding operation.
499 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
500 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
501 return (EAFNOSUPPORT);
504 * XXXRW: Is this checked at another layer? What priv to use
515 case SIOCGIFADDR_IN6:
516 /* This interface is basically deprecated. use SIOCGIFCONF. */
518 case SIOCGIFAFLAG_IN6:
519 case SIOCGIFNETMASK_IN6:
520 case SIOCGIFDSTADDR_IN6:
521 case SIOCGIFALIFETIME_IN6:
522 /* must think again about its semantics */
524 return (EADDRNOTAVAIL);
526 case SIOCSIFALIFETIME_IN6:
528 struct in6_addrlifetime *lt;
531 error = priv_check(td, PRIV_NETINET_ALIFETIME6);
536 return (EADDRNOTAVAIL);
537 /* sanity for overflow - beware unsigned */
538 lt = &ifr->ifr_ifru.ifru_lifetime;
539 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
540 lt->ia6t_vltime + time_second < time_second) {
543 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
544 lt->ia6t_pltime + time_second < time_second) {
553 case SIOCGIFADDR_IN6:
554 ifr->ifr_addr = ia->ia_addr;
555 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
559 case SIOCGIFDSTADDR_IN6:
560 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
563 * XXX: should we check if ifa_dstaddr is NULL and return
566 ifr->ifr_dstaddr = ia->ia_dstaddr;
567 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
571 case SIOCGIFNETMASK_IN6:
572 ifr->ifr_addr = ia->ia_prefixmask;
575 case SIOCGIFAFLAG_IN6:
576 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
579 case SIOCGIFSTAT_IN6:
582 bzero(&ifr->ifr_ifru.ifru_stat,
583 sizeof(ifr->ifr_ifru.ifru_stat));
584 ifr->ifr_ifru.ifru_stat =
585 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
588 case SIOCGIFSTAT_ICMP6:
591 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
592 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
593 ifr->ifr_ifru.ifru_icmp6stat =
594 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
597 case SIOCGIFALIFETIME_IN6:
598 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
599 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
601 struct in6_addrlifetime *retlt =
602 &ifr->ifr_ifru.ifru_lifetime;
605 * XXX: adjust expiration time assuming time_t is
609 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
610 if (ia->ia6_lifetime.ia6t_vltime <
611 maxexpire - ia->ia6_updatetime) {
612 retlt->ia6t_expire = ia->ia6_updatetime +
613 ia->ia6_lifetime.ia6t_vltime;
615 retlt->ia6t_expire = maxexpire;
617 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
619 struct in6_addrlifetime *retlt =
620 &ifr->ifr_ifru.ifru_lifetime;
623 * XXX: adjust expiration time assuming time_t is
627 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
628 if (ia->ia6_lifetime.ia6t_pltime <
629 maxexpire - ia->ia6_updatetime) {
630 retlt->ia6t_preferred = ia->ia6_updatetime +
631 ia->ia6_lifetime.ia6t_pltime;
633 retlt->ia6t_preferred = maxexpire;
637 case SIOCSIFALIFETIME_IN6:
638 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
640 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
641 ia->ia6_lifetime.ia6t_expire =
642 time_second + ia->ia6_lifetime.ia6t_vltime;
644 ia->ia6_lifetime.ia6t_expire = 0;
645 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
646 ia->ia6_lifetime.ia6t_preferred =
647 time_second + ia->ia6_lifetime.ia6t_pltime;
649 ia->ia6_lifetime.ia6t_preferred = 0;
652 case SIOCAIFADDR_IN6:
655 struct nd_prefixctl pr0;
656 struct nd_prefix *pr;
659 * first, make or update the interface address structure,
660 * and link it to the list.
662 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
664 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
667 * this can happen when the user specify the 0 valid
674 * then, make the prefix on-link on the interface.
675 * XXX: we'd rather create the prefix before the address, but
676 * we need at least one address to install the corresponding
677 * interface route, so we configure the address first.
681 * convert mask to prefix length (prefixmask has already
682 * been validated in in6_update_ifa().
684 bzero(&pr0, sizeof(pr0));
686 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
688 if (pr0.ndpr_plen == 128) {
689 break; /* we don't need to install a host route. */
691 pr0.ndpr_prefix = ifra->ifra_addr;
692 /* apply the mask for safety. */
693 for (i = 0; i < 4; i++) {
694 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
695 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
698 * XXX: since we don't have an API to set prefix (not address)
699 * lifetimes, we just use the same lifetimes as addresses.
700 * The (temporarily) installed lifetimes can be overridden by
701 * later advertised RAs (when accept_rtadv is non 0), which is
702 * an intended behavior.
704 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
706 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
707 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
708 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
710 /* add the prefix if not yet. */
711 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
713 * nd6_prelist_add will install the corresponding
716 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
719 log(LOG_ERR, "nd6_prelist_add succeeded but "
721 return (EINVAL); /* XXX panic here? */
725 /* relate the address to the prefix */
726 if (ia->ia6_ndpr == NULL) {
731 * If this is the first autoconf address from the
732 * prefix, create a temporary address as well
735 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
736 ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
738 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
739 log(LOG_NOTICE, "in6_control: failed "
740 "to create a temporary address, "
747 * this might affect the status of autoconfigured addresses,
748 * that is, this address might make other addresses detached.
750 pfxlist_onlink_check();
751 if (error == 0 && ia)
752 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
756 case SIOCDIFADDR_IN6:
758 struct nd_prefix *pr;
761 * If the address being deleted is the only one that owns
762 * the corresponding prefix, expire the prefix as well.
763 * XXX: theoretically, we don't have to worry about such
764 * relationship, since we separate the address management
765 * and the prefix management. We do this, however, to provide
766 * as much backward compatibility as possible in terms of
767 * the ioctl operation.
768 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
771 in6_purgeaddr(&ia->ia_ifa);
772 if (pr && pr->ndpr_refcnt == 0)
774 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
779 if (ifp == NULL || ifp->if_ioctl == 0)
781 return ((*ifp->if_ioctl)(ifp, cmd, data));
788 * Update parameters of an IPv6 interface address.
789 * If necessary, a new entry is created and linked into address chains.
790 * This function is separated from in6_control().
791 * XXX: should this be performed under splnet()?
794 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
795 struct in6_ifaddr *ia, int flags)
797 int error = 0, hostIsNew = 0, plen = -1;
798 struct in6_ifaddr *oia;
799 struct sockaddr_in6 dst6;
800 struct in6_addrlifetime *lt;
801 struct in6_multi_mship *imm;
802 struct in6_multi *in6m_sol;
805 char ip6buf[INET6_ADDRSTRLEN];
807 /* Validate parameters */
808 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
812 * The destination address for a p2p link must have a family
813 * of AF_UNSPEC or AF_INET6.
815 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
816 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
817 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
818 return (EAFNOSUPPORT);
820 * validate ifra_prefixmask. don't check sin6_family, netmask
821 * does not carry fields other than sin6_len.
823 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
826 * Because the IPv6 address architecture is classless, we require
827 * users to specify a (non 0) prefix length (mask) for a new address.
828 * We also require the prefix (when specified) mask is valid, and thus
829 * reject a non-consecutive mask.
831 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
833 if (ifra->ifra_prefixmask.sin6_len != 0) {
834 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
835 (u_char *)&ifra->ifra_prefixmask +
836 ifra->ifra_prefixmask.sin6_len);
841 * In this case, ia must not be NULL. We just use its prefix
844 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
847 * If the destination address on a p2p interface is specified,
848 * and the address is a scoped one, validate/set the scope
851 dst6 = ifra->ifra_dstaddr;
852 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
853 (dst6.sin6_family == AF_INET6)) {
854 struct in6_addr in6_tmp;
857 in6_tmp = dst6.sin6_addr;
858 if (in6_setscope(&in6_tmp, ifp, &zoneid))
859 return (EINVAL); /* XXX: should be impossible */
861 if (dst6.sin6_scope_id != 0) {
862 if (dst6.sin6_scope_id != zoneid)
864 } else /* user omit to specify the ID. */
865 dst6.sin6_scope_id = zoneid;
867 /* convert into the internal form */
868 if (sa6_embedscope(&dst6, 0))
869 return (EINVAL); /* XXX: should be impossible */
872 * The destination address can be specified only for a p2p or a
873 * loopback interface. If specified, the corresponding prefix length
876 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
877 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
878 /* XXX: noisy message */
879 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
880 "be specified for a p2p or a loopback IF only\n"));
884 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
885 "be 128 when dstaddr is specified\n"));
889 /* lifetime consistency check */
890 lt = &ifra->ifra_lifetime;
891 if (lt->ia6t_pltime > lt->ia6t_vltime)
893 if (lt->ia6t_vltime == 0) {
895 * the following log might be noisy, but this is a typical
896 * configuration mistake or a tool's bug.
899 "in6_update_ifa: valid lifetime is 0 for %s\n",
900 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
903 return (0); /* there's nothing to do */
907 * If this is a new address, allocate a new ifaddr and link it
913 * When in6_update_ifa() is called in a process of a received
914 * RA, it is called under an interrupt context. So, we should
915 * call malloc with M_NOWAIT.
917 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
921 bzero((caddr_t)ia, sizeof(*ia));
922 LIST_INIT(&ia->ia6_memberships);
923 /* Initialize the address and masks, and put time stamp */
924 IFA_LOCK_INIT(&ia->ia_ifa);
925 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
926 ia->ia_addr.sin6_family = AF_INET6;
927 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
928 ia->ia6_createtime = time_second;
929 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
931 * XXX: some functions expect that ifa_dstaddr is not
932 * NULL for p2p interfaces.
934 ia->ia_ifa.ifa_dstaddr =
935 (struct sockaddr *)&ia->ia_dstaddr;
937 ia->ia_ifa.ifa_dstaddr = NULL;
939 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
942 if ((oia = in6_ifaddr) != NULL) {
943 for ( ; oia->ia_next; oia = oia->ia_next)
949 ia->ia_ifa.ifa_refcnt = 1;
950 TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
953 /* update timestamp */
954 ia->ia6_updatetime = time_second;
956 /* set prefix mask */
957 if (ifra->ifra_prefixmask.sin6_len) {
959 * We prohibit changing the prefix length of an existing
961 * + such an operation should be rare in IPv6, and
962 * + the operation would confuse prefix management.
964 if (ia->ia_prefixmask.sin6_len &&
965 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
966 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
967 " existing (%s) address should not be changed\n",
968 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
972 ia->ia_prefixmask = ifra->ifra_prefixmask;
976 * If a new destination address is specified, scrub the old one and
977 * install the new destination. Note that the interface must be
978 * p2p or loopback (see the check above.)
980 if (dst6.sin6_family == AF_INET6 &&
981 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
984 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
985 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
986 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
987 "a route to the old destination: %s\n",
988 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
989 /* proceed anyway... */
991 ia->ia_flags &= ~IFA_ROUTE;
992 ia->ia_dstaddr = dst6;
996 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
997 * to see if the address is deprecated or invalidated, but initialize
998 * these members for applications.
1000 ia->ia6_lifetime = ifra->ifra_lifetime;
1001 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1002 ia->ia6_lifetime.ia6t_expire =
1003 time_second + ia->ia6_lifetime.ia6t_vltime;
1005 ia->ia6_lifetime.ia6t_expire = 0;
1006 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1007 ia->ia6_lifetime.ia6t_preferred =
1008 time_second + ia->ia6_lifetime.ia6t_pltime;
1010 ia->ia6_lifetime.ia6t_preferred = 0;
1012 /* reset the interface and routing table appropriately. */
1013 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1017 * configure address flags.
1019 ia->ia6_flags = ifra->ifra_flags;
1021 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1022 * userland, make it deprecated.
1024 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1025 ia->ia6_lifetime.ia6t_pltime = 0;
1026 ia->ia6_lifetime.ia6t_preferred = time_second;
1029 * Make the address tentative before joining multicast addresses,
1030 * so that corresponding MLD responses would not have a tentative
1033 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1034 if (hostIsNew && in6if_do_dad(ifp))
1035 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1038 * We are done if we have simply modified an existing address.
1044 * Beyond this point, we should call in6_purgeaddr upon an error,
1045 * not just go to unlink.
1048 /* Join necessary multicast groups */
1050 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1051 struct sockaddr_in6 mltaddr, mltmask;
1052 struct in6_addr llsol;
1054 /* join solicited multicast addr for new host id */
1055 bzero(&llsol, sizeof(struct in6_addr));
1056 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1057 llsol.s6_addr32[1] = 0;
1058 llsol.s6_addr32[2] = htonl(1);
1059 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
1060 llsol.s6_addr8[12] = 0xff;
1061 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
1062 /* XXX: should not happen */
1063 log(LOG_ERR, "in6_update_ifa: "
1064 "in6_setscope failed\n");
1068 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1070 * We need a random delay for DAD on the address
1071 * being configured. It also means delaying
1072 * transmission of the corresponding MLD report to
1073 * avoid report collision.
1074 * [draft-ietf-ipv6-rfc2462bis-02.txt]
1076 delay = arc4random() %
1077 (MAX_RTR_SOLICITATION_DELAY * hz);
1079 imm = in6_joingroup(ifp, &llsol, &error, delay);
1081 nd6log((LOG_WARNING,
1082 "in6_update_ifa: addmulti failed for "
1083 "%s on %s (errno=%d)\n",
1084 ip6_sprintf(ip6buf, &llsol), if_name(ifp),
1086 in6_purgeaddr((struct ifaddr *)ia);
1089 LIST_INSERT_HEAD(&ia->ia6_memberships,
1091 in6m_sol = imm->i6mm_maddr;
1093 bzero(&mltmask, sizeof(mltmask));
1094 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1095 mltmask.sin6_family = AF_INET6;
1096 mltmask.sin6_addr = in6mask32;
1097 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
1100 * join link-local all-nodes address
1102 bzero(&mltaddr, sizeof(mltaddr));
1103 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1104 mltaddr.sin6_family = AF_INET6;
1105 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1106 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1108 goto cleanup; /* XXX: should not fail */
1111 * XXX: do we really need this automatic routes?
1112 * We should probably reconsider this stuff. Most applications
1113 * actually do not need the routes, since they usually specify
1114 * the outgoing interface.
1116 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1118 /* XXX: only works in !SCOPEDROUTING case. */
1119 if (memcmp(&mltaddr.sin6_addr,
1120 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1127 /* XXX: we need RTF_CLONING to fake nd6_rtrequest */
1128 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1129 (struct sockaddr *)&ia->ia_addr,
1130 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1131 (struct rtentry **)0);
1138 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1140 nd6log((LOG_WARNING,
1141 "in6_update_ifa: addmulti failed for "
1142 "%s on %s (errno=%d)\n",
1143 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1144 if_name(ifp), error));
1147 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1150 * join node information group address
1152 #define hostnamelen strlen(hostname)
1154 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1156 * The spec doesn't say anything about delay for this
1157 * group, but the same logic should apply.
1159 delay = arc4random() %
1160 (MAX_RTR_SOLICITATION_DELAY * hz);
1162 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1164 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
1165 delay); /* XXX jinmei */
1167 nd6log((LOG_WARNING, "in6_update_ifa: "
1168 "addmulti failed for %s on %s "
1170 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1171 if_name(ifp), error));
1172 /* XXX not very fatal, go on... */
1174 LIST_INSERT_HEAD(&ia->ia6_memberships,
1181 * join interface-local all-nodes address.
1182 * (ff01::1%ifN, and ff01::%ifN/32)
1184 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1185 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL))
1187 goto cleanup; /* XXX: should not fail */
1188 /* XXX: again, do we really need the route? */
1189 rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1191 if (memcmp(&mltaddr.sin6_addr,
1192 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1199 error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1200 (struct sockaddr *)&ia->ia_addr,
1201 (struct sockaddr *)&mltmask, RTF_UP | RTF_CLONING,
1202 (struct rtentry **)0);
1208 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1210 nd6log((LOG_WARNING, "in6_update_ifa: "
1211 "addmulti failed for %s on %s "
1213 ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1214 if_name(ifp), error));
1217 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1222 * Perform DAD, if needed.
1223 * XXX It may be of use, if we can administratively
1226 if (hostIsNew && in6if_do_dad(ifp) &&
1227 ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1228 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1230 int mindelay, maxdelay;
1233 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1235 * We need to impose a delay before sending an NS
1236 * for DAD. Check if we also needed a delay for the
1237 * corresponding MLD message. If we did, the delay
1238 * should be larger than the MLD delay (this could be
1239 * relaxed a bit, but this simple logic is at least
1243 if (in6m_sol != NULL &&
1244 in6m_sol->in6m_state == MLD_REPORTPENDING) {
1245 mindelay = in6m_sol->in6m_timer;
1247 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1248 if (maxdelay - mindelay == 0)
1252 (arc4random() % (maxdelay - mindelay)) +
1256 nd6_dad_start((struct ifaddr *)ia, delay);
1263 * XXX: if a change of an existing address failed, keep the entry
1267 in6_unlink_ifa(ia, ifp);
1271 in6_purgeaddr(&ia->ia_ifa);
1276 in6_purgeaddr(struct ifaddr *ifa)
1278 struct ifnet *ifp = ifa->ifa_ifp;
1279 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1280 char ip6buf[INET6_ADDRSTRLEN];
1281 struct in6_multi_mship *imm;
1283 /* stop DAD processing */
1287 * delete route to the destination of the address being purged.
1288 * The interface must be p2p or loopback in this case.
1290 if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1293 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1295 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1296 "a route to the p2p destination: %s on %s, "
1298 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr),
1300 /* proceed anyway... */
1302 ia->ia_flags &= ~IFA_ROUTE;
1305 /* Remove ownaddr's loopback rtentry, if it exists. */
1306 in6_ifremloop(&(ia->ia_ifa));
1309 * leave from multicast groups we have joined for the interface
1311 while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1312 LIST_REMOVE(imm, i6mm_chain);
1313 in6_leavegroup(imm);
1316 in6_unlink_ifa(ia, ifp);
1320 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1322 struct in6_ifaddr *oia;
1325 TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1328 if (oia == (ia = in6_ifaddr))
1329 in6_ifaddr = ia->ia_next;
1331 while (ia->ia_next && (ia->ia_next != oia))
1334 ia->ia_next = oia->ia_next;
1337 printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1342 * Release the reference to the base prefix. There should be a
1343 * positive reference.
1345 if (oia->ia6_ndpr == NULL) {
1347 "in6_unlink_ifa: autoconf'ed address "
1348 "%p has no prefix\n", oia));
1350 oia->ia6_ndpr->ndpr_refcnt--;
1351 oia->ia6_ndpr = NULL;
1355 * Also, if the address being removed is autoconf'ed, call
1356 * pfxlist_onlink_check() since the release might affect the status of
1357 * other (detached) addresses.
1359 if ((oia->ia6_flags & IN6_IFF_AUTOCONF)) {
1360 pfxlist_onlink_check();
1364 * release another refcnt for the link from in6_ifaddr.
1365 * Note that we should decrement the refcnt at least once for all *BSD.
1367 IFAFREE(&oia->ia_ifa);
1373 in6_purgeif(struct ifnet *ifp)
1375 struct ifaddr *ifa, *nifa;
1377 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
1378 nifa = TAILQ_NEXT(ifa, ifa_list);
1379 if (ifa->ifa_addr->sa_family != AF_INET6)
1389 * SIOCGLIFADDR: get first address. (?)
1390 * SIOCGLIFADDR with IFLR_PREFIX:
1391 * get first address that matches the specified prefix.
1392 * SIOCALIFADDR: add the specified address.
1393 * SIOCALIFADDR with IFLR_PREFIX:
1394 * add the specified prefix, filling hostid part from
1395 * the first link-local address. prefixlen must be <= 64.
1396 * SIOCDLIFADDR: delete the specified address.
1397 * SIOCDLIFADDR with IFLR_PREFIX:
1398 * delete the first address that matches the specified prefix.
1400 * EINVAL on invalid parameters
1401 * EADDRNOTAVAIL on prefix match failed/specified address not found
1402 * other values may be returned from in6_ioctl()
1404 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1405 * this is to accomodate address naming scheme other than RFC2374,
1407 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1408 * address encoding scheme. (see figure on page 8)
1411 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1412 struct ifnet *ifp, struct thread *td)
1414 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1416 struct sockaddr *sa;
1419 if (!data || !ifp) {
1420 panic("invalid argument to in6_lifaddr_ioctl");
1426 /* address must be specified on GET with IFLR_PREFIX */
1427 if ((iflr->flags & IFLR_PREFIX) == 0)
1432 /* address must be specified on ADD and DELETE */
1433 sa = (struct sockaddr *)&iflr->addr;
1434 if (sa->sa_family != AF_INET6)
1436 if (sa->sa_len != sizeof(struct sockaddr_in6))
1438 /* XXX need improvement */
1439 sa = (struct sockaddr *)&iflr->dstaddr;
1440 if (sa->sa_family && sa->sa_family != AF_INET6)
1442 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1445 default: /* shouldn't happen */
1447 panic("invalid cmd to in6_lifaddr_ioctl");
1453 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1459 struct in6_aliasreq ifra;
1460 struct in6_addr *hostid = NULL;
1463 if ((iflr->flags & IFLR_PREFIX) != 0) {
1464 struct sockaddr_in6 *sin6;
1467 * hostid is to fill in the hostid part of the
1468 * address. hostid points to the first link-local
1469 * address attached to the interface.
1471 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1473 return EADDRNOTAVAIL;
1474 hostid = IFA_IN6(ifa);
1476 /* prefixlen must be <= 64. */
1477 if (64 < iflr->prefixlen)
1479 prefixlen = iflr->prefixlen;
1481 /* hostid part must be zero. */
1482 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1483 if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1484 sin6->sin6_addr.s6_addr32[3] != 0) {
1488 prefixlen = iflr->prefixlen;
1490 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1491 bzero(&ifra, sizeof(ifra));
1492 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1494 bcopy(&iflr->addr, &ifra.ifra_addr,
1495 ((struct sockaddr *)&iflr->addr)->sa_len);
1497 /* fill in hostid part */
1498 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1499 hostid->s6_addr32[2];
1500 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1501 hostid->s6_addr32[3];
1504 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1505 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1506 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1508 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1509 hostid->s6_addr32[2];
1510 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1511 hostid->s6_addr32[3];
1515 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1516 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1518 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1519 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1524 struct in6_ifaddr *ia;
1525 struct in6_addr mask, candidate, match;
1526 struct sockaddr_in6 *sin6;
1529 bzero(&mask, sizeof(mask));
1530 if (iflr->flags & IFLR_PREFIX) {
1531 /* lookup a prefix rather than address. */
1532 in6_prefixlen2mask(&mask, iflr->prefixlen);
1534 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1535 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1536 match.s6_addr32[0] &= mask.s6_addr32[0];
1537 match.s6_addr32[1] &= mask.s6_addr32[1];
1538 match.s6_addr32[2] &= mask.s6_addr32[2];
1539 match.s6_addr32[3] &= mask.s6_addr32[3];
1541 /* if you set extra bits, that's wrong */
1542 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1547 if (cmd == SIOCGLIFADDR) {
1548 /* on getting an address, take the 1st match */
1551 /* on deleting an address, do exact match */
1552 in6_prefixlen2mask(&mask, 128);
1553 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1554 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1560 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1561 if (ifa->ifa_addr->sa_family != AF_INET6)
1567 * XXX: this is adhoc, but is necessary to allow
1568 * a user to specify fe80::/64 (not /10) for a
1569 * link-local address.
1571 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1572 in6_clearscope(&candidate);
1573 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1574 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1575 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1576 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1577 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1581 return EADDRNOTAVAIL;
1584 if (cmd == SIOCGLIFADDR) {
1587 /* fill in the if_laddrreq structure */
1588 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1589 error = sa6_recoverscope(
1590 (struct sockaddr_in6 *)&iflr->addr);
1594 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1595 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1596 ia->ia_dstaddr.sin6_len);
1597 error = sa6_recoverscope(
1598 (struct sockaddr_in6 *)&iflr->dstaddr);
1602 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1605 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1607 iflr->flags = ia->ia6_flags; /* XXX */
1611 struct in6_aliasreq ifra;
1613 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1614 bzero(&ifra, sizeof(ifra));
1615 bcopy(iflr->iflr_name, ifra.ifra_name,
1616 sizeof(ifra.ifra_name));
1618 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1619 ia->ia_addr.sin6_len);
1620 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1621 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1622 ia->ia_dstaddr.sin6_len);
1624 bzero(&ifra.ifra_dstaddr,
1625 sizeof(ifra.ifra_dstaddr));
1627 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1628 ia->ia_prefixmask.sin6_len);
1630 ifra.ifra_flags = ia->ia6_flags;
1631 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1637 return EOPNOTSUPP; /* just for safety */
1641 * Initialize an interface's intetnet6 address
1642 * and routing table entry.
1645 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1646 struct sockaddr_in6 *sin6, int newhost)
1648 int error = 0, plen, ifacount = 0;
1653 * Give the interface a chance to initialize
1654 * if this is its first address,
1655 * and to validate the address if necessary.
1657 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1658 if (ifa->ifa_addr->sa_family != AF_INET6)
1663 ia->ia_addr = *sin6;
1665 if (ifacount <= 1 && ifp->if_ioctl) {
1667 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1668 IFF_UNLOCKGIANT(ifp);
1676 ia->ia_ifa.ifa_metric = ifp->if_metric;
1678 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1682 * set the rtrequest function to create llinfo. It also
1683 * adjust outgoing interface of the route for the local
1684 * address when called via in6_ifaddloop() below.
1686 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1691 * If a new destination address is specified for a point-to-point
1692 * interface, install a route to the destination as an interface
1693 * direct route. In addition, if the link is expected to have neighbor
1694 * cache entries, specify RTF_LLINFO so that a cache entry for the
1695 * destination address will be created.
1697 * XXX: the logic below rejects assigning multiple addresses on a p2p
1698 * interface that share the same destination.
1700 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1701 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1702 ia->ia_dstaddr.sin6_family == AF_INET6) {
1703 int rtflags = RTF_UP | RTF_HOST;
1704 struct rtentry *rt = NULL, **rtp = NULL;
1706 if (nd6_need_cache(ifp) != 0) {
1707 rtflags |= RTF_LLINFO;
1711 error = rtrequest(RTM_ADD, (struct sockaddr *)&ia->ia_dstaddr,
1712 (struct sockaddr *)&ia->ia_addr,
1713 (struct sockaddr *)&ia->ia_prefixmask,
1714 ia->ia_flags | rtflags, rtp);
1718 struct llinfo_nd6 *ln;
1721 ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1724 * Set the state to STALE because we don't
1725 * have to perform address resolution on this
1728 ln->ln_state = ND6_LLINFO_STALE;
1733 ia->ia_flags |= IFA_ROUTE;
1737 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1739 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1742 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1744 in6_ifaddloop(&(ia->ia_ifa));
1749 struct in6_multi_mship *
1750 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr,
1751 int *errorp, int delay)
1753 struct in6_multi_mship *imm;
1755 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT);
1760 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, delay);
1761 if (!imm->i6mm_maddr) {
1762 /* *errorp is alrady set */
1763 free(imm, M_IP6MADDR);
1770 in6_leavegroup(struct in6_multi_mship *imm)
1773 if (imm->i6mm_maddr)
1774 in6_delmulti(imm->i6mm_maddr);
1775 free(imm, M_IP6MADDR);
1780 * Find an IPv6 interface link-local address specific to an interface.
1783 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1787 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1788 if (ifa->ifa_addr->sa_family != AF_INET6)
1790 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1791 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1798 return ((struct in6_ifaddr *)ifa);
1803 * find the internet address corresponding to a given interface and address.
1806 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1810 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1811 if (ifa->ifa_addr->sa_family != AF_INET6)
1813 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1817 return ((struct in6_ifaddr *)ifa);
1821 * Convert IP6 address to printable (loggable) representation. Caller
1822 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1824 static char digits[] = "0123456789abcdef";
1826 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1830 const u_int16_t *a = (const u_int16_t *)addr;
1832 int dcolon = 0, zero = 0;
1836 for (i = 0; i < 8; i++) {
1847 if (dcolon == 0 && *(a + 1) == 0) {
1859 d = (const u_char *)a;
1860 /* Try to eliminate leading zeros in printout like in :0001. */
1862 *cp = digits[*d >> 4];
1867 *cp = digits[*d++ & 0xf];
1868 if (zero == 0 || (*cp != '0')) {
1872 *cp = digits[*d >> 4];
1873 if (zero == 0 || (*cp != '0')) {
1877 *cp++ = digits[*d & 0xf];
1886 in6_localaddr(struct in6_addr *in6)
1888 struct in6_ifaddr *ia;
1890 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1893 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1894 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1895 &ia->ia_prefixmask.sin6_addr)) {
1904 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1906 struct in6_ifaddr *ia;
1908 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1909 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1911 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1912 return (1); /* true */
1914 /* XXX: do we still have to go thru the rest of the list? */
1917 return (0); /* false */
1921 * return length of part which dst and src are equal
1925 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1928 u_char *s = (u_char *)src, *d = (u_char *)dst;
1929 u_char *lim = s + 16, r;
1932 if ((r = (*d++ ^ *s++)) != 0) {
1943 /* XXX: to be scope conscious */
1945 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1947 int bytelen, bitlen;
1950 if (0 > len || len > 128) {
1951 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1959 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1962 p1->s6_addr[bytelen] >> (8 - bitlen) !=
1963 p2->s6_addr[bytelen] >> (8 - bitlen))
1970 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1972 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1973 int bytelen, bitlen, i;
1976 if (0 > len || len > 128) {
1977 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1982 bzero(maskp, sizeof(*maskp));
1985 for (i = 0; i < bytelen; i++)
1986 maskp->s6_addr[i] = 0xff;
1988 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1992 * return the best address out of the same scope. if no address was
1993 * found, return the first valid address from designated IF.
1996 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
1998 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2000 struct in6_ifaddr *besta = 0;
2001 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2003 dep[0] = dep[1] = NULL;
2006 * We first look for addresses in the same scope.
2007 * If there is one, return it.
2008 * If two or more, return one which matches the dst longest.
2009 * If none, return one of global addresses assigned other ifs.
2011 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2012 if (ifa->ifa_addr->sa_family != AF_INET6)
2014 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2015 continue; /* XXX: is there any case to allow anycast? */
2016 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2017 continue; /* don't use this interface */
2018 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2020 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2021 if (ip6_use_deprecated)
2022 dep[0] = (struct in6_ifaddr *)ifa;
2026 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2028 * call in6_matchlen() as few as possible
2032 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2033 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2036 besta = (struct in6_ifaddr *)ifa;
2039 besta = (struct in6_ifaddr *)ifa;
2045 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2046 if (ifa->ifa_addr->sa_family != AF_INET6)
2048 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2049 continue; /* XXX: is there any case to allow anycast? */
2050 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2051 continue; /* don't use this interface */
2052 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2054 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2055 if (ip6_use_deprecated)
2056 dep[1] = (struct in6_ifaddr *)ifa;
2060 return (struct in6_ifaddr *)ifa;
2063 /* use the last-resort values, that are, deprecated addresses */
2073 * perform DAD when interface becomes IFF_UP.
2076 in6_if_up(struct ifnet *ifp)
2079 struct in6_ifaddr *ia;
2081 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2082 if (ifa->ifa_addr->sa_family != AF_INET6)
2084 ia = (struct in6_ifaddr *)ifa;
2085 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2087 * The TENTATIVE flag was likely set by hand
2088 * beforehand, implicitly indicating the need for DAD.
2089 * We may be able to skip the random delay in this
2090 * case, but we impose delays just in case.
2093 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2098 * special cases, like 6to4, are handled in in6_ifattach
2100 in6_ifattach(ifp, NULL);
2104 in6if_do_dad(struct ifnet *ifp)
2106 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2109 switch (ifp->if_type) {
2115 * These interfaces do not have the IFF_LOOPBACK flag,
2116 * but loop packets back. We do not have to do DAD on such
2117 * interfaces. We should even omit it, because loop-backed
2118 * NS would confuse the DAD procedure.
2123 * Our DAD routine requires the interface up and running.
2124 * However, some interfaces can be up before the RUNNING
2125 * status. Additionaly, users may try to assign addresses
2126 * before the interface becomes up (or running).
2127 * We simply skip DAD in such a case as a work around.
2128 * XXX: we should rather mark "tentative" on such addresses,
2129 * and do DAD after the interface becomes ready.
2131 if (!((ifp->if_flags & IFF_UP) &&
2132 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2140 * Calculate max IPv6 MTU through all the interfaces and store it
2146 unsigned long maxmtu = 0;
2150 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
2151 /* this function can be called during ifnet initialization */
2152 if (!ifp->if_afdata[AF_INET6])
2154 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2155 IN6_LINKMTU(ifp) > maxmtu)
2156 maxmtu = IN6_LINKMTU(ifp);
2159 if (maxmtu) /* update only when maxmtu is positive */
2160 in6_maxmtu = maxmtu;
2164 * Provide the length of interface identifiers to be used for the link attached
2165 * to the given interface. The length should be defined in "IPv6 over
2166 * xxx-link" document. Note that address architecture might also define
2167 * the length for a particular set of address prefixes, regardless of the
2168 * link type. As clarified in rfc2462bis, those two definitions should be
2169 * consistent, and those really are as of August 2004.
2172 in6_if2idlen(struct ifnet *ifp)
2174 switch (ifp->if_type) {
2175 case IFT_ETHER: /* RFC2464 */
2176 #ifdef IFT_PROPVIRTUAL
2177 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2180 case IFT_L2VLAN: /* ditto */
2182 #ifdef IFT_IEEE80211
2183 case IFT_IEEE80211: /* ditto */
2186 case IFT_MIP: /* ditto */
2189 case IFT_FDDI: /* RFC2467 */
2191 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2193 case IFT_PPP: /* RFC2472 */
2195 case IFT_ARCNET: /* RFC2497 */
2197 case IFT_FRELAY: /* RFC2590 */
2199 case IFT_IEEE1394: /* RFC3146 */
2202 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2204 return (64); /* XXX: is this really correct? */
2207 * Unknown link type:
2208 * It might be controversial to use the today's common constant
2209 * of 64 for these cases unconditionally. For full compliance,
2210 * we should return an error in this case. On the other hand,
2211 * if we simply miss the standard for the link type or a new
2212 * standard is defined for a new link type, the IFID length
2213 * is very likely to be the common constant. As a compromise,
2214 * we always use the constant, but make an explicit notice
2215 * indicating the "unknown" case.
2217 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2223 in6_domifattach(struct ifnet *ifp)
2225 struct in6_ifextra *ext;
2227 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2228 bzero(ext, sizeof(*ext));
2230 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2231 M_IFADDR, M_WAITOK);
2232 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2235 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2236 M_IFADDR, M_WAITOK);
2237 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2239 ext->nd_ifinfo = nd6_ifattach(ifp);
2240 ext->scope6_id = scope6_ifattach(ifp);
2245 in6_domifdetach(struct ifnet *ifp, void *aux)
2247 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2249 scope6_ifdetach(ext->scope6_id);
2250 nd6_ifdetach(ext->nd_ifinfo);
2251 free(ext->in6_ifstat, M_IFADDR);
2252 free(ext->icmp6_ifstat, M_IFADDR);
2253 free(ext, M_IFADDR);
2257 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2258 * v4 mapped addr or v4 compat addr
2261 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2264 bzero(sin, sizeof(*sin));
2265 sin->sin_len = sizeof(struct sockaddr_in);
2266 sin->sin_family = AF_INET;
2267 sin->sin_port = sin6->sin6_port;
2268 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2271 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2273 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2275 bzero(sin6, sizeof(*sin6));
2276 sin6->sin6_len = sizeof(struct sockaddr_in6);
2277 sin6->sin6_family = AF_INET6;
2278 sin6->sin6_port = sin->sin_port;
2279 sin6->sin6_addr.s6_addr32[0] = 0;
2280 sin6->sin6_addr.s6_addr32[1] = 0;
2281 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2282 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2285 /* Convert sockaddr_in6 into sockaddr_in. */
2287 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2289 struct sockaddr_in *sin_p;
2290 struct sockaddr_in6 sin6;
2293 * Save original sockaddr_in6 addr and convert it
2296 sin6 = *(struct sockaddr_in6 *)nam;
2297 sin_p = (struct sockaddr_in *)nam;
2298 in6_sin6_2_sin(sin_p, &sin6);
2301 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2303 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2305 struct sockaddr_in *sin_p;
2306 struct sockaddr_in6 *sin6_p;
2308 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2310 sin_p = (struct sockaddr_in *)*nam;
2311 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2312 FREE(*nam, M_SONAME);
2313 *nam = (struct sockaddr *)sin6_p;