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$");
66 #include "opt_compat.h"
68 #include "opt_inet6.h"
70 #include <sys/param.h>
71 #include <sys/errno.h>
73 #include <sys/malloc.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sockio.h>
77 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/syslog.h>
85 #include <net/if_var.h>
86 #include <net/if_types.h>
87 #include <net/route.h>
88 #include <net/if_dl.h>
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #include <net/if_llatbl.h>
94 #include <netinet/if_ether.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/ip.h>
97 #include <netinet/in_pcb.h>
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
101 #include <netinet6/nd6.h>
102 #include <netinet6/mld6_var.h>
103 #include <netinet6/ip6_mroute.h>
104 #include <netinet6/in6_ifattach.h>
105 #include <netinet6/scope6_var.h>
106 #include <netinet6/in6_pcb.h>
108 VNET_DECLARE(int, icmp6_nodeinfo_oldmcprefix);
109 #define V_icmp6_nodeinfo_oldmcprefix VNET(icmp6_nodeinfo_oldmcprefix)
112 * Definitions of some costant IP6 addresses.
114 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
115 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
116 const struct in6_addr in6addr_nodelocal_allnodes =
117 IN6ADDR_NODELOCAL_ALLNODES_INIT;
118 const struct in6_addr in6addr_linklocal_allnodes =
119 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
120 const struct in6_addr in6addr_linklocal_allrouters =
121 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
122 const struct in6_addr in6addr_linklocal_allv2routers =
123 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_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 =
132 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
134 static int in6_lifaddr_ioctl(struct socket *, u_long, caddr_t,
135 struct ifnet *, struct thread *);
136 static int in6_ifinit(struct ifnet *, struct in6_ifaddr *,
137 struct sockaddr_in6 *, int);
138 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
140 int (*faithprefix_p)(struct in6_addr *);
142 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
143 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
146 in6_ifaddloop(struct ifaddr *ifa)
148 struct sockaddr_dl gateway;
149 struct sockaddr_in6 mask, addr;
151 struct in6_ifaddr *ia;
158 * initialize for rtmsg generation
160 bzero(&gateway, sizeof(gateway));
161 gateway.sdl_len = sizeof(gateway);
162 gateway.sdl_family = AF_LINK;
163 if (nd6_need_cache(ifp) != 0) {
165 ifa->ifa_rtrequest = nd6_rtrequest;
166 ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR |
167 LLE_EXCLUSIVE), (struct sockaddr *)&ia->ia_addr);
168 IF_AFDATA_UNLOCK(ifp);
170 ln->la_expire = 0; /* for IPv6 this means permanent */
171 ln->ln_state = ND6_LLINFO_REACHABLE;
173 gateway.sdl_alen = 6;
174 memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned,
175 sizeof(ln->ll_addr));
179 bzero(&rt, sizeof(rt));
180 rt.rt_gateway = (struct sockaddr *)&gateway;
181 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
182 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
183 rt_mask(&rt) = (struct sockaddr *)&mask;
184 rt_key(&rt) = (struct sockaddr *)&addr;
185 rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC;
186 /* Announce arrival of local address to all FIBs. */
187 rt_newaddrmsg(RTM_ADD, ifa, 0, &rt);
191 in6_ifremloop(struct ifaddr *ifa)
193 struct sockaddr_dl gateway;
194 struct sockaddr_in6 mask, addr;
196 struct in6_ifaddr *ia;
201 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
202 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
203 lltable_prefix_free(AF_INET6, (struct sockaddr *)&addr,
204 (struct sockaddr *)&mask, LLE_STATIC);
207 * initialize for rtmsg generation
209 bzero(&gateway, sizeof(gateway));
210 gateway.sdl_len = sizeof(gateway);
211 gateway.sdl_family = AF_LINK;
212 gateway.sdl_nlen = 0;
213 gateway.sdl_alen = ifp->if_addrlen;
214 bzero(&rt0, sizeof(rt0));
215 rt0.rt_gateway = (struct sockaddr *)&gateway;
216 rt_mask(&rt0) = (struct sockaddr *)&mask;
217 rt_key(&rt0) = (struct sockaddr *)&addr;
218 rt0.rt_flags = RTF_HOST | RTF_STATIC;
219 /* Announce removal of local address to all FIBs. */
220 rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0);
224 in6_mask2len(struct in6_addr *mask, u_char *lim0)
227 u_char *lim = lim0, *p;
229 /* ignore the scope_id part */
230 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
231 lim = (u_char *)mask + sizeof(*mask);
232 for (p = (u_char *)mask; p < lim; x++, p++) {
238 for (y = 0; y < 8; y++) {
239 if ((*p & (0x80 >> y)) == 0)
245 * when the limit pointer is given, do a stricter check on the
249 if (y != 0 && (*p & (0x00ff >> y)) != 0)
251 for (p = p + 1; p < lim; p++)
259 #ifdef COMPAT_FREEBSD32
260 struct in6_ndifreq32 {
261 char ifname[IFNAMSIZ];
264 #define SIOCGDEFIFACE32_IN6 _IOWR('i', 86, struct in6_ndifreq32)
268 in6_control(struct socket *so, u_long cmd, caddr_t data,
269 struct ifnet *ifp, struct thread *td)
271 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
272 struct in6_ifaddr *ia = NULL;
273 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
274 struct sockaddr_in6 *sa6;
278 case SIOCGETSGCNT_IN6:
279 case SIOCGETMIFCNT_IN6:
281 * XXX mrt_ioctl has a 3rd, unused, FIB argument in route.c.
282 * We cannot see how that would be needed, so do not adjust the
283 * KPI blindly; more likely should clean up the IPv4 variant.
285 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
289 case SIOCAADDRCTL_POLICY:
290 case SIOCDADDRCTL_POLICY:
292 error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
296 return (in6_src_ioctl(cmd, data));
303 case SIOCSNDFLUSH_IN6:
304 case SIOCSPFXFLUSH_IN6:
305 case SIOCSRTRFLUSH_IN6:
306 case SIOCSDEFIFACE_IN6:
307 case SIOCSIFINFO_FLAGS:
308 case SIOCSIFINFO_IN6:
310 error = priv_check(td, PRIV_NETINET_ND6);
315 case OSIOCGIFINFO_IN6:
316 case SIOCGIFINFO_IN6:
319 case SIOCGNBRINFO_IN6:
320 case SIOCGDEFIFACE_IN6:
321 return (nd6_ioctl(cmd, data, ifp));
323 #ifdef COMPAT_FREEBSD32
324 case SIOCGDEFIFACE32_IN6:
326 struct in6_ndifreq ndif;
327 struct in6_ndifreq32 *ndif32;
329 error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif,
333 ndif32 = (struct in6_ndifreq32 *)data;
334 ndif32->ifindex = ndif.ifindex;
341 case SIOCSIFPREFIX_IN6:
342 case SIOCDIFPREFIX_IN6:
343 case SIOCAIFPREFIX_IN6:
344 case SIOCCIFPREFIX_IN6:
345 case SIOCSGIFPREFIX_IN6:
346 case SIOCGIFPREFIX_IN6:
348 "prefix ioctls are now invalidated. "
349 "please use ifconfig.\n");
356 error = priv_check(td, PRIV_NETINET_SCOPE6);
363 return (scope6_ioctl(cmd, data, ifp));
369 error = priv_check(td, PRIV_NET_ADDIFADDR);
373 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
377 error = priv_check(td, PRIV_NET_DELIFADDR);
383 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
387 * Find address for this interface, if it exists.
389 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
390 * only, and used the first interface address as the target of other
391 * operations (without checking ifra_addr). This was because netinet
392 * code/API assumed at most 1 interface address per interface.
393 * Since IPv6 allows a node to assign multiple addresses
394 * on a single interface, we almost always look and check the
395 * presence of ifra_addr, and reject invalid ones here.
396 * It also decreases duplicated code among SIOC*_IN6 operations.
399 case SIOCAIFADDR_IN6:
400 case SIOCSIFPHYADDR_IN6:
401 sa6 = &ifra->ifra_addr;
403 case SIOCSIFADDR_IN6:
404 case SIOCGIFADDR_IN6:
405 case SIOCSIFDSTADDR_IN6:
406 case SIOCSIFNETMASK_IN6:
407 case SIOCGIFDSTADDR_IN6:
408 case SIOCGIFNETMASK_IN6:
409 case SIOCDIFADDR_IN6:
410 case SIOCGIFPSRCADDR_IN6:
411 case SIOCGIFPDSTADDR_IN6:
412 case SIOCGIFAFLAG_IN6:
413 case SIOCSNDFLUSH_IN6:
414 case SIOCSPFXFLUSH_IN6:
415 case SIOCSRTRFLUSH_IN6:
416 case SIOCGIFALIFETIME_IN6:
417 case SIOCSIFALIFETIME_IN6:
418 case SIOCGIFSTAT_IN6:
419 case SIOCGIFSTAT_ICMP6:
420 sa6 = &ifr->ifr_addr;
427 * Although we should pass any non-INET6 ioctl requests
428 * down to driver, we filter some legacy INET requests.
429 * Drivers trust SIOCSIFADDR et al to come from an already
430 * privileged layer, and do not perform any credentials
431 * checks or input validation.
438 if (sa6 && sa6->sin6_family == AF_INET6) {
439 if (sa6->sin6_scope_id != 0)
440 error = sa6_embedscope(sa6, 0);
442 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
445 if (td != NULL && (error = prison_check_ip6(td->td_ucred,
446 &sa6->sin6_addr)) != 0)
448 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
453 case SIOCSIFADDR_IN6:
454 case SIOCSIFDSTADDR_IN6:
455 case SIOCSIFNETMASK_IN6:
457 * Since IPv6 allows a node to assign multiple addresses
458 * on a single interface, SIOCSIFxxx ioctls are deprecated.
460 /* we decided to obsolete this command (20000704) */
464 case SIOCDIFADDR_IN6:
466 * for IPv4, we look for existing in_ifaddr here to allow
467 * "ifconfig if0 delete" to remove the first IPv4 address on
468 * the interface. For IPv6, as the spec allows multiple
469 * interface address from the day one, we consider "remove the
470 * first one" semantics to be not preferable.
473 error = EADDRNOTAVAIL;
477 case SIOCAIFADDR_IN6:
479 * We always require users to specify a valid IPv6 address for
480 * the corresponding operation.
482 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
483 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
484 error = EAFNOSUPPORT;
489 error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
490 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
495 case SIOCGIFSTAT_IN6:
496 case SIOCGIFSTAT_ICMP6:
497 if (ifp->if_afdata[AF_INET6] == NULL) {
498 error = EPFNOSUPPORT;
503 case SIOCGIFADDR_IN6:
504 /* This interface is basically deprecated. use SIOCGIFCONF. */
506 case SIOCGIFAFLAG_IN6:
507 case SIOCGIFNETMASK_IN6:
508 case SIOCGIFDSTADDR_IN6:
509 case SIOCGIFALIFETIME_IN6:
510 /* must think again about its semantics */
512 error = EADDRNOTAVAIL;
517 case SIOCSIFALIFETIME_IN6:
519 struct in6_addrlifetime *lt;
522 error = priv_check(td, PRIV_NETINET_ALIFETIME6);
527 error = EADDRNOTAVAIL;
530 /* sanity for overflow - beware unsigned */
531 lt = &ifr->ifr_ifru.ifru_lifetime;
532 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
533 lt->ia6t_vltime + time_second < time_second) {
537 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
538 lt->ia6t_pltime + time_second < time_second) {
547 case SIOCGIFADDR_IN6:
548 ifr->ifr_addr = ia->ia_addr;
549 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
553 case SIOCGIFDSTADDR_IN6:
554 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
559 * XXX: should we check if ifa_dstaddr is NULL and return
562 ifr->ifr_dstaddr = ia->ia_dstaddr;
563 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
567 case SIOCGIFNETMASK_IN6:
568 ifr->ifr_addr = ia->ia_prefixmask;
571 case SIOCGIFAFLAG_IN6:
572 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
575 case SIOCGIFSTAT_IN6:
576 bzero(&ifr->ifr_ifru.ifru_stat,
577 sizeof(ifr->ifr_ifru.ifru_stat));
578 ifr->ifr_ifru.ifru_stat =
579 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
582 case SIOCGIFSTAT_ICMP6:
583 bzero(&ifr->ifr_ifru.ifru_icmp6stat,
584 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
585 ifr->ifr_ifru.ifru_icmp6stat =
586 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
589 case SIOCGIFALIFETIME_IN6:
590 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
591 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
593 struct in6_addrlifetime *retlt =
594 &ifr->ifr_ifru.ifru_lifetime;
597 * XXX: adjust expiration time assuming time_t is
601 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
602 if (ia->ia6_lifetime.ia6t_vltime <
603 maxexpire - ia->ia6_updatetime) {
604 retlt->ia6t_expire = ia->ia6_updatetime +
605 ia->ia6_lifetime.ia6t_vltime;
607 retlt->ia6t_expire = maxexpire;
609 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
611 struct in6_addrlifetime *retlt =
612 &ifr->ifr_ifru.ifru_lifetime;
615 * XXX: adjust expiration time assuming time_t is
619 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
620 if (ia->ia6_lifetime.ia6t_pltime <
621 maxexpire - ia->ia6_updatetime) {
622 retlt->ia6t_preferred = ia->ia6_updatetime +
623 ia->ia6_lifetime.ia6t_pltime;
625 retlt->ia6t_preferred = maxexpire;
629 case SIOCSIFALIFETIME_IN6:
630 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
632 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
633 ia->ia6_lifetime.ia6t_expire =
634 time_second + ia->ia6_lifetime.ia6t_vltime;
636 ia->ia6_lifetime.ia6t_expire = 0;
637 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
638 ia->ia6_lifetime.ia6t_preferred =
639 time_second + ia->ia6_lifetime.ia6t_pltime;
641 ia->ia6_lifetime.ia6t_preferred = 0;
644 case SIOCAIFADDR_IN6:
647 struct nd_prefixctl pr0;
648 struct nd_prefix *pr;
651 * first, make or update the interface address structure,
652 * and link it to the list.
654 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
657 ifa_free(&ia->ia_ifa);
658 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
661 * this can happen when the user specify the 0 valid
668 * then, make the prefix on-link on the interface.
669 * XXX: we'd rather create the prefix before the address, but
670 * we need at least one address to install the corresponding
671 * interface route, so we configure the address first.
675 * convert mask to prefix length (prefixmask has already
676 * been validated in in6_update_ifa().
678 bzero(&pr0, sizeof(pr0));
680 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
682 if (pr0.ndpr_plen == 128) {
683 break; /* we don't need to install a host route. */
685 pr0.ndpr_prefix = ifra->ifra_addr;
686 /* apply the mask for safety. */
687 for (i = 0; i < 4; i++) {
688 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
689 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
692 * XXX: since we don't have an API to set prefix (not address)
693 * lifetimes, we just use the same lifetimes as addresses.
694 * The (temporarily) installed lifetimes can be overridden by
695 * later advertised RAs (when accept_rtadv is non 0), which is
696 * an intended behavior.
698 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
700 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
701 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
702 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
704 /* add the prefix if not yet. */
705 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
707 * nd6_prelist_add will install the corresponding
710 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
713 log(LOG_ERR, "nd6_prelist_add succeeded but "
720 /* relate the address to the prefix */
721 if (ia->ia6_ndpr == NULL) {
726 * If this is the first autoconf address from the
727 * prefix, create a temporary address as well
730 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
731 V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
733 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
734 log(LOG_NOTICE, "in6_control: failed "
735 "to create a temporary address, "
742 * this might affect the status of autoconfigured addresses,
743 * that is, this address might make other addresses detached.
745 pfxlist_onlink_check();
746 if (error == 0 && ia) {
747 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
749 * Try to clear the flag when a new
750 * IPv6 address is added onto an
751 * IFDISABLED interface and it
754 struct in6_ndireq nd;
756 memset(&nd, 0, sizeof(nd));
757 nd.ndi.flags = ND_IFINFO(ifp)->flags;
758 nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
759 if (nd6_ioctl(SIOCSIFINFO_FLAGS,
760 (caddr_t)&nd, ifp) < 0)
761 log(LOG_NOTICE, "SIOCAIFADDR_IN6: "
762 "SIOCSIFINFO_FLAGS for -ifdisabled "
765 * Ignore failure of clearing the flag
766 * intentionally. The failure means
767 * address duplication was detected.
770 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
775 case SIOCDIFADDR_IN6:
777 struct nd_prefix *pr;
780 * If the address being deleted is the only one that owns
781 * the corresponding prefix, expire the prefix as well.
782 * XXX: theoretically, we don't have to worry about such
783 * relationship, since we separate the address management
784 * and the prefix management. We do this, however, to provide
785 * as much backward compatibility as possible in terms of
786 * the ioctl operation.
787 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
790 in6_purgeaddr(&ia->ia_ifa);
791 if (pr && pr->ndpr_refcnt == 0)
793 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
798 if (ifp->if_ioctl == NULL) {
802 error = (*ifp->if_ioctl)(ifp, cmd, data);
809 ifa_free(&ia->ia_ifa);
815 * Join necessary multicast groups. Factored out from in6_update_ifa().
816 * This entire work should only be done once, for the default FIB.
819 in6_update_ifa_join_mc(struct ifnet *ifp, struct in6_aliasreq *ifra,
820 struct in6_ifaddr *ia, int flags, struct in6_multi **in6m_sol)
822 char ip6buf[INET6_ADDRSTRLEN];
823 struct sockaddr_in6 mltaddr, mltmask;
824 struct in6_addr llsol;
825 struct in6_multi_mship *imm;
829 KASSERT(in6m_sol != NULL, ("%s: in6m_sol is NULL", __func__));
831 /* Join solicited multicast addr for new host id. */
832 bzero(&llsol, sizeof(struct in6_addr));
833 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
834 llsol.s6_addr32[1] = 0;
835 llsol.s6_addr32[2] = htonl(1);
836 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
837 llsol.s6_addr8[12] = 0xff;
838 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
839 /* XXX: should not happen */
840 log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
844 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
846 * We need a random delay for DAD on the address being
847 * configured. It also means delaying transmission of the
848 * corresponding MLD report to avoid report collision.
849 * [RFC 4861, Section 6.3.7]
851 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
853 imm = in6_joingroup(ifp, &llsol, &error, delay);
855 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
856 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &llsol),
857 if_name(ifp), error));
860 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
861 *in6m_sol = imm->i6mm_maddr;
863 bzero(&mltmask, sizeof(mltmask));
864 mltmask.sin6_len = sizeof(struct sockaddr_in6);
865 mltmask.sin6_family = AF_INET6;
866 mltmask.sin6_addr = in6mask32;
867 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
870 * Join link-local all-nodes address.
872 bzero(&mltaddr, sizeof(mltaddr));
873 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
874 mltaddr.sin6_family = AF_INET6;
875 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
876 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
877 goto cleanup; /* XXX: should not fail */
880 * XXX: do we really need this automatic routes? We should probably
881 * reconsider this stuff. Most applications actually do not need the
882 * routes, since they usually specify the outgoing interface.
884 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
886 /* XXX: only works in !SCOPEDROUTING case. */
887 if (memcmp(&mltaddr.sin6_addr,
888 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
895 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
896 (struct sockaddr *)&ia->ia_addr,
897 (struct sockaddr *)&mltmask, RTF_UP,
898 (struct rtentry **)0, RT_DEFAULT_FIB);
904 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
906 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
907 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
908 &mltaddr.sin6_addr), if_name(ifp), error));
911 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
914 * Join node information group address.
917 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
919 * The spec does not say anything about delay for this group,
920 * but the same logic should apply.
922 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
924 if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
926 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, delay);
928 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
929 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
930 &mltaddr.sin6_addr), if_name(ifp), error));
931 /* XXX not very fatal, go on... */
933 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
935 if (V_icmp6_nodeinfo_oldmcprefix &&
936 in6_nigroup_oldmcprefix(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
937 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, delay);
939 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
940 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
941 &mltaddr.sin6_addr), if_name(ifp), error));
942 /* XXX not very fatal, go on... */
944 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
948 * Join interface-local all-nodes address.
949 * (ff01::1%ifN, and ff01::%ifN/32)
951 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
952 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
953 goto cleanup; /* XXX: should not fail */
954 /* XXX: again, do we really need the route? */
955 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
957 if (memcmp(&mltaddr.sin6_addr,
958 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
965 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
966 (struct sockaddr *)&ia->ia_addr,
967 (struct sockaddr *)&mltmask, RTF_UP,
968 (struct rtentry **)0, RT_DEFAULT_FIB);
974 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
976 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
977 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
978 &mltaddr.sin6_addr), if_name(ifp), error));
981 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
989 * Update parameters of an IPv6 interface address.
990 * If necessary, a new entry is created and linked into address chains.
991 * This function is separated from in6_control().
992 * XXX: should this be performed under splnet()?
995 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
996 struct in6_ifaddr *ia, int flags)
998 int error = 0, hostIsNew = 0, plen = -1;
999 struct sockaddr_in6 dst6;
1000 struct in6_addrlifetime *lt;
1001 struct in6_multi *in6m_sol;
1003 char ip6buf[INET6_ADDRSTRLEN];
1005 /* Validate parameters */
1006 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
1010 * The destination address for a p2p link must have a family
1011 * of AF_UNSPEC or AF_INET6.
1013 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1014 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
1015 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
1016 return (EAFNOSUPPORT);
1018 * validate ifra_prefixmask. don't check sin6_family, netmask
1019 * does not carry fields other than sin6_len.
1021 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
1024 * Because the IPv6 address architecture is classless, we require
1025 * users to specify a (non 0) prefix length (mask) for a new address.
1026 * We also require the prefix (when specified) mask is valid, and thus
1027 * reject a non-consecutive mask.
1029 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
1031 if (ifra->ifra_prefixmask.sin6_len != 0) {
1032 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
1033 (u_char *)&ifra->ifra_prefixmask +
1034 ifra->ifra_prefixmask.sin6_len);
1039 * In this case, ia must not be NULL. We just use its prefix
1042 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1045 * If the destination address on a p2p interface is specified,
1046 * and the address is a scoped one, validate/set the scope
1049 dst6 = ifra->ifra_dstaddr;
1050 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
1051 (dst6.sin6_family == AF_INET6)) {
1052 struct in6_addr in6_tmp;
1055 in6_tmp = dst6.sin6_addr;
1056 if (in6_setscope(&in6_tmp, ifp, &zoneid))
1057 return (EINVAL); /* XXX: should be impossible */
1059 if (dst6.sin6_scope_id != 0) {
1060 if (dst6.sin6_scope_id != zoneid)
1062 } else /* user omit to specify the ID. */
1063 dst6.sin6_scope_id = zoneid;
1065 /* convert into the internal form */
1066 if (sa6_embedscope(&dst6, 0))
1067 return (EINVAL); /* XXX: should be impossible */
1070 * The destination address can be specified only for a p2p or a
1071 * loopback interface. If specified, the corresponding prefix length
1074 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
1075 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
1076 /* XXX: noisy message */
1077 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
1078 "be specified for a p2p or a loopback IF only\n"));
1082 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
1083 "be 128 when dstaddr is specified\n"));
1087 /* lifetime consistency check */
1088 lt = &ifra->ifra_lifetime;
1089 if (lt->ia6t_pltime > lt->ia6t_vltime)
1091 if (lt->ia6t_vltime == 0) {
1093 * the following log might be noisy, but this is a typical
1094 * configuration mistake or a tool's bug.
1097 "in6_update_ifa: valid lifetime is 0 for %s\n",
1098 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
1101 return (0); /* there's nothing to do */
1105 * If this is a new address, allocate a new ifaddr and link it
1111 * When in6_update_ifa() is called in a process of a received
1112 * RA, it is called under an interrupt context. So, we should
1113 * call malloc with M_NOWAIT.
1115 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
1119 bzero((caddr_t)ia, sizeof(*ia));
1120 ifa_init(&ia->ia_ifa);
1121 LIST_INIT(&ia->ia6_memberships);
1122 /* Initialize the address and masks, and put time stamp */
1123 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1124 ia->ia_addr.sin6_family = AF_INET6;
1125 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
1126 ia->ia6_createtime = time_second;
1127 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
1129 * XXX: some functions expect that ifa_dstaddr is not
1130 * NULL for p2p interfaces.
1132 ia->ia_ifa.ifa_dstaddr =
1133 (struct sockaddr *)&ia->ia_dstaddr;
1135 ia->ia_ifa.ifa_dstaddr = NULL;
1137 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
1139 ifa_ref(&ia->ia_ifa); /* if_addrhead */
1141 TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1142 IF_ADDR_WUNLOCK(ifp);
1144 ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */
1146 TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
1147 IN6_IFADDR_WUNLOCK();
1150 /* update timestamp */
1151 ia->ia6_updatetime = time_second;
1153 /* set prefix mask */
1154 if (ifra->ifra_prefixmask.sin6_len) {
1156 * We prohibit changing the prefix length of an existing
1158 * + such an operation should be rare in IPv6, and
1159 * + the operation would confuse prefix management.
1161 if (ia->ia_prefixmask.sin6_len &&
1162 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1163 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
1164 " existing (%s) address should not be changed\n",
1165 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1169 ia->ia_prefixmask = ifra->ifra_prefixmask;
1173 * If a new destination address is specified, scrub the old one and
1174 * install the new destination. Note that the interface must be
1175 * p2p or loopback (see the check above.)
1177 if (dst6.sin6_family == AF_INET6 &&
1178 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
1181 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1182 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
1183 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
1184 "a route to the old destination: %s\n",
1185 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1186 /* proceed anyway... */
1188 ia->ia_flags &= ~IFA_ROUTE;
1189 ia->ia_dstaddr = dst6;
1193 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1194 * to see if the address is deprecated or invalidated, but initialize
1195 * these members for applications.
1197 ia->ia6_lifetime = ifra->ifra_lifetime;
1198 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1199 ia->ia6_lifetime.ia6t_expire =
1200 time_second + ia->ia6_lifetime.ia6t_vltime;
1202 ia->ia6_lifetime.ia6t_expire = 0;
1203 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1204 ia->ia6_lifetime.ia6t_preferred =
1205 time_second + ia->ia6_lifetime.ia6t_pltime;
1207 ia->ia6_lifetime.ia6t_preferred = 0;
1209 /* reset the interface and routing table appropriately. */
1210 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1214 * configure address flags.
1216 ia->ia6_flags = ifra->ifra_flags;
1218 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1219 * userland, make it deprecated.
1221 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1222 ia->ia6_lifetime.ia6t_pltime = 0;
1223 ia->ia6_lifetime.ia6t_preferred = time_second;
1226 * Make the address tentative before joining multicast addresses,
1227 * so that corresponding MLD responses would not have a tentative
1230 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1231 if (hostIsNew && in6if_do_dad(ifp))
1232 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1234 /* DAD should be performed after ND6_IFF_IFDISABLED is cleared. */
1235 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1236 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1239 * We are done if we have simply modified an existing address.
1245 * Beyond this point, we should call in6_purgeaddr upon an error,
1246 * not just go to unlink.
1249 /* Join necessary multicast groups. */
1251 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1252 error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol);
1258 * Perform DAD, if needed.
1259 * XXX It may be of use, if we can administratively disable DAD.
1261 if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1262 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1264 int mindelay, maxdelay;
1267 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1269 * We need to impose a delay before sending an NS
1270 * for DAD. Check if we also needed a delay for the
1271 * corresponding MLD message. If we did, the delay
1272 * should be larger than the MLD delay (this could be
1273 * relaxed a bit, but this simple logic is at least
1275 * XXX: Break data hiding guidelines and look at
1276 * state for the solicited multicast group.
1279 if (in6m_sol != NULL &&
1280 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1281 mindelay = in6m_sol->in6m_timer;
1283 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1284 if (maxdelay - mindelay == 0)
1288 (arc4random() % (maxdelay - mindelay)) +
1292 nd6_dad_start((struct ifaddr *)ia, delay);
1295 KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew"));
1296 ifa_free(&ia->ia_ifa);
1301 * XXX: if a change of an existing address failed, keep the entry
1305 in6_unlink_ifa(ia, ifp);
1306 ifa_free(&ia->ia_ifa);
1311 KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew"));
1312 ifa_free(&ia->ia_ifa);
1313 in6_purgeaddr(&ia->ia_ifa);
1318 * Leave multicast groups. Factored out from in6_purgeaddr().
1319 * This entire work should only be done once, for the default FIB.
1322 in6_purgeaddr_mc(struct ifnet *ifp, struct in6_ifaddr *ia, struct ifaddr *ifa0)
1324 struct sockaddr_in6 mltaddr, mltmask;
1325 struct in6_multi_mship *imm;
1327 struct sockaddr_in6 sin6;
1331 * Leave from multicast groups we have joined for the interface.
1333 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1334 LIST_REMOVE(imm, i6mm_chain);
1335 in6_leavegroup(imm);
1339 * Remove the link-local all-nodes address.
1341 bzero(&mltmask, sizeof(mltmask));
1342 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1343 mltmask.sin6_family = AF_INET6;
1344 mltmask.sin6_addr = in6mask32;
1346 bzero(&mltaddr, sizeof(mltaddr));
1347 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1348 mltaddr.sin6_family = AF_INET6;
1349 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1351 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1355 * As for the mltaddr above, proactively prepare the sin6 to avoid
1356 * rtentry un- and re-locking.
1359 bzero(&sin6, sizeof(sin6));
1360 sin6.sin6_len = sizeof(sin6);
1361 sin6.sin6_family = AF_INET6;
1362 memcpy(&sin6.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1363 sizeof(sin6.sin6_addr));
1364 error = in6_setscope(&sin6.sin6_addr, ifa0->ifa_ifp, NULL);
1369 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
1370 if (rt != NULL && rt->rt_gateway != NULL &&
1371 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1372 &ia->ia_addr.sin6_addr,
1373 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1375 * If no more IPv6 address exists on this interface then
1376 * remove the multicast address route.
1379 memcpy(&mltaddr.sin6_addr,
1380 &satosin6(rt_key(rt))->sin6_addr,
1381 sizeof(mltaddr.sin6_addr));
1383 error = in6_rtrequest(RTM_DELETE,
1384 (struct sockaddr *)&mltaddr,
1385 (struct sockaddr *)&ia->ia_addr,
1386 (struct sockaddr *)&mltmask, RTF_UP,
1387 (struct rtentry **)0, RT_DEFAULT_FIB);
1389 log(LOG_INFO, "%s: link-local all-nodes "
1390 "multicast address deletion error\n",
1394 * Replace the gateway of the route.
1396 memcpy(rt->rt_gateway, &sin6, sizeof(sin6));
1405 * Remove the node-local all-nodes address.
1407 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1408 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1411 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
1412 if (rt != NULL && rt->rt_gateway != NULL &&
1413 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1414 &ia->ia_addr.sin6_addr,
1415 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1417 * If no more IPv6 address exists on this interface then
1418 * remove the multicast address route.
1421 memcpy(&mltaddr.sin6_addr,
1422 &satosin6(rt_key(rt))->sin6_addr,
1423 sizeof(mltaddr.sin6_addr));
1426 error = in6_rtrequest(RTM_DELETE,
1427 (struct sockaddr *)&mltaddr,
1428 (struct sockaddr *)&ia->ia_addr,
1429 (struct sockaddr *)&mltmask, RTF_UP,
1430 (struct rtentry **)0, RT_DEFAULT_FIB);
1432 log(LOG_INFO, "%s: node-local all-nodes"
1433 "multicast address deletion error\n",
1437 * Replace the gateway of the route.
1439 memcpy(rt->rt_gateway, &sin6, sizeof(sin6));
1451 in6_purgeaddr(struct ifaddr *ifa)
1453 struct ifnet *ifp = ifa->ifa_ifp;
1454 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1456 struct ifaddr *ifa0;
1459 * find another IPv6 address as the gateway for the
1460 * link-local and node-local all-nodes multicast
1464 TAILQ_FOREACH(ifa0, &ifp->if_addrhead, ifa_link) {
1465 if ((ifa0->ifa_addr->sa_family != AF_INET6) ||
1466 memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr,
1467 &ia->ia_addr.sin6_addr, sizeof(struct in6_addr)) == 0)
1474 IF_ADDR_RUNLOCK(ifp);
1477 * Remove the loopback route to the interface address.
1478 * The check for the current setting of "nd6_useloopback"
1481 if (ia->ia_flags & IFA_RTSELF) {
1482 error = ifa_del_loopback_route((struct ifaddr *)ia,
1483 (struct sockaddr *)&ia->ia_addr);
1485 ia->ia_flags &= ~IFA_RTSELF;
1488 /* stop DAD processing */
1491 /* Remove local address entry from lltable. */
1494 /* Leave multicast groups. */
1495 error = in6_purgeaddr_mc(ifp, ia, ifa0);
1500 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1501 if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1502 error = rtinit(&(ia->ia_ifa), RTM_DELETE, ia->ia_flags |
1503 (ia->ia_dstaddr.sin6_family == AF_INET6) ? RTF_HOST : 0);
1505 log(LOG_INFO, "%s: err=%d, destination address delete "
1506 "failed\n", __func__, error);
1507 ia->ia_flags &= ~IFA_ROUTE;
1510 in6_unlink_ifa(ia, ifp);
1514 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1519 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1520 IF_ADDR_WUNLOCK(ifp);
1521 ifa_free(&ia->ia_ifa); /* if_addrhead */
1524 * Defer the release of what might be the last reference to the
1525 * in6_ifaddr so that it can't be freed before the remainder of the
1529 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1530 IN6_IFADDR_WUNLOCK();
1533 * Release the reference to the base prefix. There should be a
1534 * positive reference.
1536 if (ia->ia6_ndpr == NULL) {
1538 "in6_unlink_ifa: autoconf'ed address "
1539 "%p has no prefix\n", ia));
1541 ia->ia6_ndpr->ndpr_refcnt--;
1542 ia->ia6_ndpr = NULL;
1546 * Also, if the address being removed is autoconf'ed, call
1547 * pfxlist_onlink_check() since the release might affect the status of
1548 * other (detached) addresses.
1550 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1551 pfxlist_onlink_check();
1553 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */
1558 in6_purgeif(struct ifnet *ifp)
1560 struct ifaddr *ifa, *nifa;
1562 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1563 if (ifa->ifa_addr->sa_family != AF_INET6)
1573 * SIOCGLIFADDR: get first address. (?)
1574 * SIOCGLIFADDR with IFLR_PREFIX:
1575 * get first address that matches the specified prefix.
1576 * SIOCALIFADDR: add the specified address.
1577 * SIOCALIFADDR with IFLR_PREFIX:
1578 * add the specified prefix, filling hostid part from
1579 * the first link-local address. prefixlen must be <= 64.
1580 * SIOCDLIFADDR: delete the specified address.
1581 * SIOCDLIFADDR with IFLR_PREFIX:
1582 * delete the first address that matches the specified prefix.
1584 * EINVAL on invalid parameters
1585 * EADDRNOTAVAIL on prefix match failed/specified address not found
1586 * other values may be returned from in6_ioctl()
1588 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1589 * this is to accomodate address naming scheme other than RFC2374,
1591 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1592 * address encoding scheme. (see figure on page 8)
1595 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1596 struct ifnet *ifp, struct thread *td)
1598 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1600 struct sockaddr *sa;
1603 if (!data || !ifp) {
1604 panic("invalid argument to in6_lifaddr_ioctl");
1610 /* address must be specified on GET with IFLR_PREFIX */
1611 if ((iflr->flags & IFLR_PREFIX) == 0)
1616 /* address must be specified on ADD and DELETE */
1617 sa = (struct sockaddr *)&iflr->addr;
1618 if (sa->sa_family != AF_INET6)
1620 if (sa->sa_len != sizeof(struct sockaddr_in6))
1622 /* XXX need improvement */
1623 sa = (struct sockaddr *)&iflr->dstaddr;
1624 if (sa->sa_family && sa->sa_family != AF_INET6)
1626 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1629 default: /* shouldn't happen */
1631 panic("invalid cmd to in6_lifaddr_ioctl");
1637 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1643 struct in6_aliasreq ifra;
1644 struct in6_addr *hostid = NULL;
1648 if ((iflr->flags & IFLR_PREFIX) != 0) {
1649 struct sockaddr_in6 *sin6;
1652 * hostid is to fill in the hostid part of the
1653 * address. hostid points to the first link-local
1654 * address attached to the interface.
1656 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1658 return EADDRNOTAVAIL;
1659 hostid = IFA_IN6(ifa);
1661 /* prefixlen must be <= 64. */
1662 if (64 < iflr->prefixlen) {
1667 prefixlen = iflr->prefixlen;
1669 /* hostid part must be zero. */
1670 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1671 if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1672 sin6->sin6_addr.s6_addr32[3] != 0) {
1678 prefixlen = iflr->prefixlen;
1680 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1681 bzero(&ifra, sizeof(ifra));
1682 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1684 bcopy(&iflr->addr, &ifra.ifra_addr,
1685 ((struct sockaddr *)&iflr->addr)->sa_len);
1687 /* fill in hostid part */
1688 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1689 hostid->s6_addr32[2];
1690 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1691 hostid->s6_addr32[3];
1694 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1695 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1696 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1698 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1699 hostid->s6_addr32[2];
1700 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1701 hostid->s6_addr32[3];
1707 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1708 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1710 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1711 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1716 struct in6_ifaddr *ia;
1717 struct in6_addr mask, candidate, match;
1718 struct sockaddr_in6 *sin6;
1721 bzero(&mask, sizeof(mask));
1722 if (iflr->flags & IFLR_PREFIX) {
1723 /* lookup a prefix rather than address. */
1724 in6_prefixlen2mask(&mask, iflr->prefixlen);
1726 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1727 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1728 match.s6_addr32[0] &= mask.s6_addr32[0];
1729 match.s6_addr32[1] &= mask.s6_addr32[1];
1730 match.s6_addr32[2] &= mask.s6_addr32[2];
1731 match.s6_addr32[3] &= mask.s6_addr32[3];
1733 /* if you set extra bits, that's wrong */
1734 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1739 if (cmd == SIOCGLIFADDR) {
1740 /* on getting an address, take the 1st match */
1743 /* on deleting an address, do exact match */
1744 in6_prefixlen2mask(&mask, 128);
1745 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1746 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1753 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1754 if (ifa->ifa_addr->sa_family != AF_INET6)
1760 * XXX: this is adhoc, but is necessary to allow
1761 * a user to specify fe80::/64 (not /10) for a
1762 * link-local address.
1764 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1765 in6_clearscope(&candidate);
1766 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1767 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1768 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1769 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1770 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1775 IF_ADDR_RUNLOCK(ifp);
1777 return EADDRNOTAVAIL;
1780 if (cmd == SIOCGLIFADDR) {
1783 /* fill in the if_laddrreq structure */
1784 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1785 error = sa6_recoverscope(
1786 (struct sockaddr_in6 *)&iflr->addr);
1792 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1793 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1794 ia->ia_dstaddr.sin6_len);
1795 error = sa6_recoverscope(
1796 (struct sockaddr_in6 *)&iflr->dstaddr);
1802 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1805 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1807 iflr->flags = ia->ia6_flags; /* XXX */
1812 struct in6_aliasreq ifra;
1814 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1815 bzero(&ifra, sizeof(ifra));
1816 bcopy(iflr->iflr_name, ifra.ifra_name,
1817 sizeof(ifra.ifra_name));
1819 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1820 ia->ia_addr.sin6_len);
1821 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1822 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1823 ia->ia_dstaddr.sin6_len);
1825 bzero(&ifra.ifra_dstaddr,
1826 sizeof(ifra.ifra_dstaddr));
1828 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1829 ia->ia_prefixmask.sin6_len);
1831 ifra.ifra_flags = ia->ia6_flags;
1833 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1839 return EOPNOTSUPP; /* just for safety */
1843 * Initialize an interface's IPv6 address and routing table entry.
1846 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1847 struct sockaddr_in6 *sin6, int newhost)
1849 int error = 0, plen, ifacount = 0;
1854 * Give the interface a chance to initialize
1855 * if this is its first address,
1856 * and to validate the address if necessary.
1859 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1860 if (ifa->ifa_addr->sa_family != AF_INET6)
1864 IF_ADDR_RUNLOCK(ifp);
1866 ia->ia_addr = *sin6;
1868 if (ifacount <= 1 && ifp->if_ioctl) {
1869 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1877 ia->ia_ifa.ifa_metric = ifp->if_metric;
1879 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1883 * If a new destination address is specified for a point-to-point
1884 * interface, install a route to the destination as an interface
1886 * XXX: the logic below rejects assigning multiple addresses on a p2p
1887 * interface that share the same destination.
1889 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1890 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1891 ia->ia_dstaddr.sin6_family == AF_INET6) {
1892 int rtflags = RTF_UP | RTF_HOST;
1893 error = rtinit(&ia->ia_ifa, RTM_ADD, ia->ia_flags | rtflags);
1896 ia->ia_flags |= IFA_ROUTE;
1898 * Handle the case for ::1 .
1900 if (ifp->if_flags & IFF_LOOPBACK)
1901 ia->ia_flags |= IFA_RTSELF;
1905 * add a loopback route to self
1907 if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
1908 error = ifa_add_loopback_route((struct ifaddr *)ia,
1909 (struct sockaddr *)&ia->ia_addr);
1911 ia->ia_flags |= IFA_RTSELF;
1914 /* Add local address to lltable, if necessary (ex. on p2p link). */
1916 in6_ifaddloop(&(ia->ia_ifa));
1922 * Find an IPv6 interface link-local address specific to an interface.
1923 * ifaddr is returned referenced.
1926 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1931 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1932 if (ifa->ifa_addr->sa_family != AF_INET6)
1934 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1935 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1942 IF_ADDR_RUNLOCK(ifp);
1944 return ((struct in6_ifaddr *)ifa);
1949 * find the internet address corresponding to a given interface and address.
1950 * ifaddr is returned referenced.
1953 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1958 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1959 if (ifa->ifa_addr->sa_family != AF_INET6)
1961 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1966 IF_ADDR_RUNLOCK(ifp);
1968 return ((struct in6_ifaddr *)ifa);
1972 * Find a link-local scoped address on ifp and return it if any.
1975 in6ifa_llaonifp(struct ifnet *ifp)
1977 struct sockaddr_in6 *sin6;
1980 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1983 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1984 if (ifa->ifa_addr->sa_family != AF_INET6)
1986 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1987 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
1988 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
1989 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
1992 if_addr_runlock(ifp);
1994 return ((struct in6_ifaddr *)ifa);
1998 * Convert IP6 address to printable (loggable) representation. Caller
1999 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
2001 static char digits[] = "0123456789abcdef";
2003 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
2005 int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
2007 const u_int16_t *a = (const u_int16_t *)addr;
2009 int dcolon = 0, zero = 0;
2013 for (i = 0; i < 8; i++) {
2014 if (*(a + i) == 0) {
2019 else if (maxcnt < cnt) {
2030 for (i = 0; i < 8; i++) {
2041 if (dcolon == 0 && *(a + 1) == 0 && i == index) {
2053 d = (const u_char *)a;
2054 /* Try to eliminate leading zeros in printout like in :0001. */
2056 *cp = digits[*d >> 4];
2061 *cp = digits[*d++ & 0xf];
2062 if (zero == 0 || (*cp != '0')) {
2066 *cp = digits[*d >> 4];
2067 if (zero == 0 || (*cp != '0')) {
2071 *cp++ = digits[*d & 0xf];
2080 in6_localaddr(struct in6_addr *in6)
2082 struct in6_ifaddr *ia;
2084 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
2088 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
2089 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
2090 &ia->ia_prefixmask.sin6_addr)) {
2091 IN6_IFADDR_RUNLOCK();
2095 IN6_IFADDR_RUNLOCK();
2101 * Return 1 if an internet address is for the local host and configured
2102 * on one of its interfaces.
2105 in6_localip(struct in6_addr *in6)
2107 struct in6_ifaddr *ia;
2110 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
2111 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) {
2112 IN6_IFADDR_RUNLOCK();
2116 IN6_IFADDR_RUNLOCK();
2122 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
2124 struct in6_ifaddr *ia;
2127 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
2128 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
2130 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
2131 IN6_IFADDR_RUNLOCK();
2132 return (1); /* true */
2135 /* XXX: do we still have to go thru the rest of the list? */
2137 IN6_IFADDR_RUNLOCK();
2139 return (0); /* false */
2143 * return length of part which dst and src are equal
2147 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
2150 u_char *s = (u_char *)src, *d = (u_char *)dst;
2151 u_char *lim = s + 16, r;
2154 if ((r = (*d++ ^ *s++)) != 0) {
2165 /* XXX: to be scope conscious */
2167 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
2169 int bytelen, bitlen;
2172 if (0 > len || len > 128) {
2173 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2181 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2184 p1->s6_addr[bytelen] >> (8 - bitlen) !=
2185 p2->s6_addr[bytelen] >> (8 - bitlen))
2192 in6_prefixlen2mask(struct in6_addr *maskp, int len)
2194 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2195 int bytelen, bitlen, i;
2198 if (0 > len || len > 128) {
2199 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2204 bzero(maskp, sizeof(*maskp));
2207 for (i = 0; i < bytelen; i++)
2208 maskp->s6_addr[i] = 0xff;
2210 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2214 * return the best address out of the same scope. if no address was
2215 * found, return the first valid address from designated IF.
2218 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2220 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2222 struct in6_ifaddr *besta = 0;
2223 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2225 dep[0] = dep[1] = NULL;
2228 * We first look for addresses in the same scope.
2229 * If there is one, return it.
2230 * If two or more, return one which matches the dst longest.
2231 * If none, return one of global addresses assigned other ifs.
2234 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2235 if (ifa->ifa_addr->sa_family != AF_INET6)
2237 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2238 continue; /* XXX: is there any case to allow anycast? */
2239 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2240 continue; /* don't use this interface */
2241 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2243 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2244 if (V_ip6_use_deprecated)
2245 dep[0] = (struct in6_ifaddr *)ifa;
2249 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2251 * call in6_matchlen() as few as possible
2255 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2256 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2259 besta = (struct in6_ifaddr *)ifa;
2262 besta = (struct in6_ifaddr *)ifa;
2266 ifa_ref(&besta->ia_ifa);
2267 IF_ADDR_RUNLOCK(ifp);
2271 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2272 if (ifa->ifa_addr->sa_family != AF_INET6)
2274 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2275 continue; /* XXX: is there any case to allow anycast? */
2276 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2277 continue; /* don't use this interface */
2278 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2280 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2281 if (V_ip6_use_deprecated)
2282 dep[1] = (struct in6_ifaddr *)ifa;
2288 IF_ADDR_RUNLOCK(ifp);
2289 return (struct in6_ifaddr *)ifa;
2292 /* use the last-resort values, that are, deprecated addresses */
2294 ifa_ref((struct ifaddr *)dep[0]);
2295 IF_ADDR_RUNLOCK(ifp);
2299 ifa_ref((struct ifaddr *)dep[1]);
2300 IF_ADDR_RUNLOCK(ifp);
2304 IF_ADDR_RUNLOCK(ifp);
2309 * perform DAD when interface becomes IFF_UP.
2312 in6_if_up(struct ifnet *ifp)
2315 struct in6_ifaddr *ia;
2318 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2319 if (ifa->ifa_addr->sa_family != AF_INET6)
2321 ia = (struct in6_ifaddr *)ifa;
2322 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2324 * The TENTATIVE flag was likely set by hand
2325 * beforehand, implicitly indicating the need for DAD.
2326 * We may be able to skip the random delay in this
2327 * case, but we impose delays just in case.
2330 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2333 IF_ADDR_RUNLOCK(ifp);
2336 * special cases, like 6to4, are handled in in6_ifattach
2338 in6_ifattach(ifp, NULL);
2342 in6if_do_dad(struct ifnet *ifp)
2344 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2347 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
2350 switch (ifp->if_type) {
2356 * These interfaces do not have the IFF_LOOPBACK flag,
2357 * but loop packets back. We do not have to do DAD on such
2358 * interfaces. We should even omit it, because loop-backed
2359 * NS would confuse the DAD procedure.
2364 * Our DAD routine requires the interface up and running.
2365 * However, some interfaces can be up before the RUNNING
2366 * status. Additionaly, users may try to assign addresses
2367 * before the interface becomes up (or running).
2368 * We simply skip DAD in such a case as a work around.
2369 * XXX: we should rather mark "tentative" on such addresses,
2370 * and do DAD after the interface becomes ready.
2372 if (!((ifp->if_flags & IFF_UP) &&
2373 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2381 * Calculate max IPv6 MTU through all the interfaces and store it
2387 unsigned long maxmtu = 0;
2390 IFNET_RLOCK_NOSLEEP();
2391 TAILQ_FOREACH(ifp, &V_ifnet, if_list) {
2392 /* this function can be called during ifnet initialization */
2393 if (!ifp->if_afdata[AF_INET6])
2395 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2396 IN6_LINKMTU(ifp) > maxmtu)
2397 maxmtu = IN6_LINKMTU(ifp);
2399 IFNET_RUNLOCK_NOSLEEP();
2400 if (maxmtu) /* update only when maxmtu is positive */
2401 V_in6_maxmtu = maxmtu;
2405 * Provide the length of interface identifiers to be used for the link attached
2406 * to the given interface. The length should be defined in "IPv6 over
2407 * xxx-link" document. Note that address architecture might also define
2408 * the length for a particular set of address prefixes, regardless of the
2409 * link type. As clarified in rfc2462bis, those two definitions should be
2410 * consistent, and those really are as of August 2004.
2413 in6_if2idlen(struct ifnet *ifp)
2415 switch (ifp->if_type) {
2416 case IFT_ETHER: /* RFC2464 */
2417 #ifdef IFT_PROPVIRTUAL
2418 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2421 case IFT_L2VLAN: /* ditto */
2423 #ifdef IFT_IEEE80211
2424 case IFT_IEEE80211: /* ditto */
2427 case IFT_MIP: /* ditto */
2429 case IFT_INFINIBAND:
2431 case IFT_FDDI: /* RFC2467 */
2433 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2435 case IFT_PPP: /* RFC2472 */
2437 case IFT_ARCNET: /* RFC2497 */
2439 case IFT_FRELAY: /* RFC2590 */
2441 case IFT_IEEE1394: /* RFC3146 */
2444 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2446 return (64); /* XXX: is this really correct? */
2449 * Unknown link type:
2450 * It might be controversial to use the today's common constant
2451 * of 64 for these cases unconditionally. For full compliance,
2452 * we should return an error in this case. On the other hand,
2453 * if we simply miss the standard for the link type or a new
2454 * standard is defined for a new link type, the IFID length
2455 * is very likely to be the common constant. As a compromise,
2456 * we always use the constant, but make an explicit notice
2457 * indicating the "unknown" case.
2459 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2464 #include <sys/sysctl.h>
2466 struct in6_llentry {
2467 struct llentry base;
2468 struct sockaddr_in6 l3_addr6;
2472 * Deletes an address from the address table.
2473 * This function is called by the timer functions
2474 * such as arptimer() and nd6_llinfo_timer(), and
2475 * the caller does the locking.
2478 in6_lltable_free(struct lltable *llt, struct llentry *lle)
2481 LLE_LOCK_DESTROY(lle);
2482 free(lle, M_LLTABLE);
2485 static struct llentry *
2486 in6_lltable_new(const struct sockaddr *l3addr, u_int flags)
2488 struct in6_llentry *lle;
2490 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
2491 if (lle == NULL) /* NB: caller generates msg */
2494 lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr;
2495 lle->base.lle_refcnt = 1;
2496 lle->base.lle_free = in6_lltable_free;
2497 LLE_LOCK_INIT(&lle->base);
2498 callout_init_rw(&lle->base.ln_timer_ch, &lle->base.lle_lock,
2499 CALLOUT_RETURNUNLOCKED);
2501 return (&lle->base);
2505 in6_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
2506 const struct sockaddr *mask, u_int flags)
2508 const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
2509 const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
2510 struct llentry *lle, *next;
2514 * (flags & LLE_STATIC) means deleting all entries
2515 * including static ND6 entries.
2517 IF_AFDATA_WLOCK(llt->llt_ifp);
2518 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2519 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
2520 if (IN6_ARE_MASKED_ADDR_EQUAL(
2521 &satosin6(L3_ADDR(lle))->sin6_addr,
2522 &pfx->sin6_addr, &msk->sin6_addr) &&
2523 ((flags & LLE_STATIC) ||
2524 !(lle->la_flags & LLE_STATIC))) {
2526 if (callout_stop(&lle->la_timer))
2532 IF_AFDATA_WUNLOCK(llt->llt_ifp);
2536 in6_lltable_rtcheck(struct ifnet *ifp,
2538 const struct sockaddr *l3addr)
2541 char ip6buf[INET6_ADDRSTRLEN];
2543 KASSERT(l3addr->sa_family == AF_INET6,
2544 ("sin_family %d", l3addr->sa_family));
2546 /* Our local addresses are always only installed on the default FIB. */
2547 /* XXX rtalloc1 should take a const param */
2548 rt = in6_rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0,
2550 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2553 * Create an ND6 cache for an IPv6 neighbor
2554 * that is not covered by our own prefix.
2556 /* XXX ifaof_ifpforaddr should take a const param */
2557 ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp);
2564 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2565 ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr));
2574 static struct llentry *
2575 in6_lltable_lookup(struct lltable *llt, u_int flags,
2576 const struct sockaddr *l3addr)
2578 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2579 struct ifnet *ifp = llt->llt_ifp;
2580 struct llentry *lle;
2581 struct llentries *lleh;
2584 IF_AFDATA_LOCK_ASSERT(ifp);
2585 KASSERT(l3addr->sa_family == AF_INET6,
2586 ("sin_family %d", l3addr->sa_family));
2588 hashkey = sin6->sin6_addr.s6_addr32[3];
2589 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
2590 LIST_FOREACH(lle, lleh, lle_next) {
2591 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle);
2592 if (lle->la_flags & LLE_DELETED)
2594 if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr,
2595 sizeof(struct in6_addr)) == 0)
2600 if (!(flags & LLE_CREATE))
2602 IF_AFDATA_WLOCK_ASSERT(ifp);
2604 * A route that covers the given address must have
2605 * been installed 1st because we are doing a resolution,
2608 if (!(flags & LLE_IFADDR) &&
2609 in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2612 lle = in6_lltable_new(l3addr, flags);
2614 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2617 lle->la_flags = flags & ~LLE_CREATE;
2618 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
2619 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
2620 lle->la_flags |= (LLE_VALID | LLE_STATIC);
2624 lle->lle_head = lleh;
2625 lle->la_flags |= LLE_LINKED;
2626 LIST_INSERT_HEAD(lleh, lle, lle_next);
2627 } else if (flags & LLE_DELETE) {
2628 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
2630 lle->la_flags |= LLE_DELETED;
2632 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2634 if ((lle->la_flags &
2635 (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
2642 if (LLE_IS_VALID(lle)) {
2643 if (flags & LLE_EXCLUSIVE)
2652 in6_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
2654 struct ifnet *ifp = llt->llt_ifp;
2655 struct llentry *lle;
2658 struct rt_msghdr rtm;
2659 struct sockaddr_in6 sin6;
2661 * ndp.c assumes that sdl is word aligned
2666 struct sockaddr_dl sdl;
2670 if (ifp->if_flags & IFF_LOOPBACK)
2673 LLTABLE_LOCK_ASSERT();
2676 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2677 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
2678 struct sockaddr_dl *sdl;
2680 /* skip deleted or invalid entries */
2681 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
2683 /* Skip if jailed and not a valid IP of the prison. */
2684 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
2687 * produce a msg made of:
2689 * struct sockaddr_in6 (IPv6)
2690 * struct sockaddr_dl;
2692 bzero(&ndpc, sizeof(ndpc));
2693 ndpc.rtm.rtm_msglen = sizeof(ndpc);
2694 ndpc.rtm.rtm_version = RTM_VERSION;
2695 ndpc.rtm.rtm_type = RTM_GET;
2696 ndpc.rtm.rtm_flags = RTF_UP;
2697 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2698 ndpc.sin6.sin6_family = AF_INET6;
2699 ndpc.sin6.sin6_len = sizeof(ndpc.sin6);
2700 bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle));
2703 if (lle->la_flags & LLE_PUB)
2704 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2707 sdl->sdl_family = AF_LINK;
2708 sdl->sdl_len = sizeof(*sdl);
2709 sdl->sdl_alen = ifp->if_addrlen;
2710 sdl->sdl_index = ifp->if_index;
2711 sdl->sdl_type = ifp->if_type;
2712 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2713 ndpc.rtm.rtm_rmx.rmx_expire =
2714 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
2715 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2716 if (lle->la_flags & LLE_STATIC)
2717 ndpc.rtm.rtm_flags |= RTF_STATIC;
2718 ndpc.rtm.rtm_index = ifp->if_index;
2719 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2728 in6_domifattach(struct ifnet *ifp)
2730 struct in6_ifextra *ext;
2732 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2733 bzero(ext, sizeof(*ext));
2735 ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2736 M_IFADDR, M_WAITOK);
2737 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2740 (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2741 M_IFADDR, M_WAITOK);
2742 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2744 ext->nd_ifinfo = nd6_ifattach(ifp);
2745 ext->scope6_id = scope6_ifattach(ifp);
2746 ext->lltable = lltable_init(ifp, AF_INET6);
2747 if (ext->lltable != NULL) {
2748 ext->lltable->llt_prefix_free = in6_lltable_prefix_free;
2749 ext->lltable->llt_lookup = in6_lltable_lookup;
2750 ext->lltable->llt_dump = in6_lltable_dump;
2753 ext->mld_ifinfo = mld_domifattach(ifp);
2759 in6_domifdetach(struct ifnet *ifp, void *aux)
2761 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2763 mld_domifdetach(ifp);
2764 scope6_ifdetach(ext->scope6_id);
2765 nd6_ifdetach(ext->nd_ifinfo);
2766 lltable_free(ext->lltable);
2767 free(ext->in6_ifstat, M_IFADDR);
2768 free(ext->icmp6_ifstat, M_IFADDR);
2769 free(ext, M_IFADDR);
2773 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2774 * v4 mapped addr or v4 compat addr
2777 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2780 bzero(sin, sizeof(*sin));
2781 sin->sin_len = sizeof(struct sockaddr_in);
2782 sin->sin_family = AF_INET;
2783 sin->sin_port = sin6->sin6_port;
2784 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2787 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2789 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2791 bzero(sin6, sizeof(*sin6));
2792 sin6->sin6_len = sizeof(struct sockaddr_in6);
2793 sin6->sin6_family = AF_INET6;
2794 sin6->sin6_port = sin->sin_port;
2795 sin6->sin6_addr.s6_addr32[0] = 0;
2796 sin6->sin6_addr.s6_addr32[1] = 0;
2797 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2798 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2801 /* Convert sockaddr_in6 into sockaddr_in. */
2803 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2805 struct sockaddr_in *sin_p;
2806 struct sockaddr_in6 sin6;
2809 * Save original sockaddr_in6 addr and convert it
2812 sin6 = *(struct sockaddr_in6 *)nam;
2813 sin_p = (struct sockaddr_in *)nam;
2814 in6_sin6_2_sin(sin_p, &sin6);
2817 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2819 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2821 struct sockaddr_in *sin_p;
2822 struct sockaddr_in6 *sin6_p;
2824 sin6_p = malloc(sizeof *sin6_p, M_SONAME, M_WAITOK);
2825 sin_p = (struct sockaddr_in *)*nam;
2826 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2827 free(*nam, M_SONAME);
2828 *nam = (struct sockaddr *)sin6_p;