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>
98 #include <netinet/ip_carp.h>
100 #include <netinet/ip6.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/nd6.h>
103 #include <netinet6/mld6_var.h>
104 #include <netinet6/ip6_mroute.h>
105 #include <netinet6/in6_ifattach.h>
106 #include <netinet6/scope6_var.h>
107 #include <netinet6/in6_pcb.h>
109 VNET_DECLARE(int, icmp6_nodeinfo_oldmcprefix);
110 #define V_icmp6_nodeinfo_oldmcprefix VNET(icmp6_nodeinfo_oldmcprefix)
113 * Definitions of some costant IP6 addresses.
115 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
116 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
117 const struct in6_addr in6addr_nodelocal_allnodes =
118 IN6ADDR_NODELOCAL_ALLNODES_INIT;
119 const struct in6_addr in6addr_linklocal_allnodes =
120 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
121 const struct in6_addr in6addr_linklocal_allrouters =
122 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
123 const struct in6_addr in6addr_linklocal_allv2routers =
124 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
126 const struct in6_addr in6mask0 = IN6MASK0;
127 const struct in6_addr in6mask32 = IN6MASK32;
128 const struct in6_addr in6mask64 = IN6MASK64;
129 const struct in6_addr in6mask96 = IN6MASK96;
130 const struct in6_addr in6mask128 = IN6MASK128;
132 const struct sockaddr_in6 sa6_any =
133 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
135 static int in6_lifaddr_ioctl(struct socket *, u_long, caddr_t,
136 struct ifnet *, struct thread *);
137 static int in6_ifinit(struct ifnet *, struct in6_ifaddr *,
138 struct sockaddr_in6 *, int);
139 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
141 int (*faithprefix_p)(struct in6_addr *);
143 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
144 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
147 in6_ifaddloop(struct ifaddr *ifa)
149 struct sockaddr_dl gateway;
150 struct sockaddr_in6 mask, addr;
152 struct in6_ifaddr *ia;
159 ifa->ifa_rtrequest = nd6_rtrequest;
160 ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR |
161 LLE_EXCLUSIVE), (struct sockaddr *)&ia->ia_addr);
162 IF_AFDATA_UNLOCK(ifp);
164 ln->la_expire = 0; /* for IPv6 this means permanent */
165 ln->ln_state = ND6_LLINFO_REACHABLE;
167 * initialize for rtmsg generation
169 bzero(&gateway, sizeof(gateway));
170 gateway.sdl_len = sizeof(gateway);
171 gateway.sdl_family = AF_LINK;
172 gateway.sdl_nlen = 0;
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;
275 int carp_attached = 0;
280 * Compat to make pre-10.x ifconfig(8) operable.
282 if (cmd == OSIOCAIFADDR_IN6)
283 cmd = SIOCAIFADDR_IN6;
286 case SIOCGETSGCNT_IN6:
287 case SIOCGETMIFCNT_IN6:
289 * XXX mrt_ioctl has a 3rd, unused, FIB argument in route.c.
290 * We cannot see how that would be needed, so do not adjust the
291 * KPI blindly; more likely should clean up the IPv4 variant.
293 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
297 case SIOCAADDRCTL_POLICY:
298 case SIOCDADDRCTL_POLICY:
300 error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
304 return (in6_src_ioctl(cmd, data));
311 case SIOCSNDFLUSH_IN6:
312 case SIOCSPFXFLUSH_IN6:
313 case SIOCSRTRFLUSH_IN6:
314 case SIOCSDEFIFACE_IN6:
315 case SIOCSIFINFO_FLAGS:
316 case SIOCSIFINFO_IN6:
318 error = priv_check(td, PRIV_NETINET_ND6);
323 case OSIOCGIFINFO_IN6:
324 case SIOCGIFINFO_IN6:
327 case SIOCGNBRINFO_IN6:
328 case SIOCGDEFIFACE_IN6:
329 return (nd6_ioctl(cmd, data, ifp));
331 #ifdef COMPAT_FREEBSD32
332 case SIOCGDEFIFACE32_IN6:
334 struct in6_ndifreq ndif;
335 struct in6_ndifreq32 *ndif32;
337 error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif,
341 ndif32 = (struct in6_ndifreq32 *)data;
342 ndif32->ifindex = ndif.ifindex;
349 case SIOCSIFPREFIX_IN6:
350 case SIOCDIFPREFIX_IN6:
351 case SIOCAIFPREFIX_IN6:
352 case SIOCCIFPREFIX_IN6:
353 case SIOCSGIFPREFIX_IN6:
354 case SIOCGIFPREFIX_IN6:
356 "prefix ioctls are now invalidated. "
357 "please use ifconfig.\n");
364 error = priv_check(td, PRIV_NETINET_SCOPE6);
368 return (scope6_set(ifp,
369 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
371 return (scope6_get(ifp,
372 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
374 return (scope6_get_default((struct scope6_id *)
375 ifr->ifr_ifru.ifru_scope_id));
381 error = priv_check(td, PRIV_NET_ADDIFADDR);
385 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
389 error = priv_check(td, PRIV_NET_DELIFADDR);
395 return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
399 * Find address for this interface, if it exists.
401 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
402 * only, and used the first interface address as the target of other
403 * operations (without checking ifra_addr). This was because netinet
404 * code/API assumed at most 1 interface address per interface.
405 * Since IPv6 allows a node to assign multiple addresses
406 * on a single interface, we almost always look and check the
407 * presence of ifra_addr, and reject invalid ones here.
408 * It also decreases duplicated code among SIOC*_IN6 operations.
411 case SIOCAIFADDR_IN6:
412 case SIOCSIFPHYADDR_IN6:
413 sa6 = &ifra->ifra_addr;
415 case SIOCSIFADDR_IN6:
416 case SIOCGIFADDR_IN6:
417 case SIOCSIFDSTADDR_IN6:
418 case SIOCSIFNETMASK_IN6:
419 case SIOCGIFDSTADDR_IN6:
420 case SIOCGIFNETMASK_IN6:
421 case SIOCDIFADDR_IN6:
422 case SIOCGIFPSRCADDR_IN6:
423 case SIOCGIFPDSTADDR_IN6:
424 case SIOCGIFAFLAG_IN6:
425 case SIOCSNDFLUSH_IN6:
426 case SIOCSPFXFLUSH_IN6:
427 case SIOCSRTRFLUSH_IN6:
428 case SIOCGIFALIFETIME_IN6:
429 case SIOCSIFALIFETIME_IN6:
430 case SIOCGIFSTAT_IN6:
431 case SIOCGIFSTAT_ICMP6:
432 sa6 = &ifr->ifr_addr;
439 * Although we should pass any non-INET6 ioctl requests
440 * down to driver, we filter some legacy INET requests.
441 * Drivers trust SIOCSIFADDR et al to come from an already
442 * privileged layer, and do not perform any credentials
443 * checks or input validation.
450 if (sa6 && sa6->sin6_family == AF_INET6) {
451 if (sa6->sin6_scope_id != 0)
452 error = sa6_embedscope(sa6, 0);
454 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
457 if (td != NULL && (error = prison_check_ip6(td->td_ucred,
458 &sa6->sin6_addr)) != 0)
460 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
465 case SIOCSIFADDR_IN6:
466 case SIOCSIFDSTADDR_IN6:
467 case SIOCSIFNETMASK_IN6:
469 * Since IPv6 allows a node to assign multiple addresses
470 * on a single interface, SIOCSIFxxx ioctls are deprecated.
472 /* we decided to obsolete this command (20000704) */
476 case SIOCDIFADDR_IN6:
478 * for IPv4, we look for existing in_ifaddr here to allow
479 * "ifconfig if0 delete" to remove the first IPv4 address on
480 * the interface. For IPv6, as the spec allows multiple
481 * interface address from the day one, we consider "remove the
482 * first one" semantics to be not preferable.
485 error = EADDRNOTAVAIL;
489 case SIOCAIFADDR_IN6:
491 * We always require users to specify a valid IPv6 address for
492 * the corresponding operation.
494 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
495 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
496 error = EAFNOSUPPORT;
501 error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
502 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
508 case SIOCGIFADDR_IN6:
509 /* This interface is basically deprecated. use SIOCGIFCONF. */
511 case SIOCGIFAFLAG_IN6:
512 case SIOCGIFNETMASK_IN6:
513 case SIOCGIFDSTADDR_IN6:
514 case SIOCGIFALIFETIME_IN6:
515 /* must think again about its semantics */
517 error = EADDRNOTAVAIL;
522 case SIOCSIFALIFETIME_IN6:
524 struct in6_addrlifetime *lt;
527 error = priv_check(td, PRIV_NETINET_ALIFETIME6);
532 error = EADDRNOTAVAIL;
535 /* sanity for overflow - beware unsigned */
536 lt = &ifr->ifr_ifru.ifru_lifetime;
537 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
538 lt->ia6t_vltime + time_uptime < time_uptime) {
542 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
543 lt->ia6t_pltime + time_uptime < time_uptime) {
552 case SIOCGIFADDR_IN6:
553 ifr->ifr_addr = ia->ia_addr;
554 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
558 case SIOCGIFDSTADDR_IN6:
559 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
564 * XXX: should we check if ifa_dstaddr is NULL and return
567 ifr->ifr_dstaddr = ia->ia_dstaddr;
568 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
572 case SIOCGIFNETMASK_IN6:
573 ifr->ifr_addr = ia->ia_prefixmask;
576 case SIOCGIFAFLAG_IN6:
577 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
580 case SIOCGIFSTAT_IN6:
585 COUNTER_ARRAY_COPY(((struct in6_ifextra *)
586 ifp->if_afdata[AF_INET6])->in6_ifstat,
587 &ifr->ifr_ifru.ifru_stat,
588 sizeof(struct in6_ifstat) / sizeof(uint64_t));
591 case SIOCGIFSTAT_ICMP6:
596 COUNTER_ARRAY_COPY(((struct in6_ifextra *)
597 ifp->if_afdata[AF_INET6])->icmp6_ifstat,
598 &ifr->ifr_ifru.ifru_icmp6stat,
599 sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
602 case SIOCGIFALIFETIME_IN6:
603 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
604 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
606 struct in6_addrlifetime *retlt =
607 &ifr->ifr_ifru.ifru_lifetime;
610 * XXX: adjust expiration time assuming time_t is
614 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
615 if (ia->ia6_lifetime.ia6t_vltime <
616 maxexpire - ia->ia6_updatetime) {
617 retlt->ia6t_expire = ia->ia6_updatetime +
618 ia->ia6_lifetime.ia6t_vltime;
620 retlt->ia6t_expire = maxexpire;
622 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
624 struct in6_addrlifetime *retlt =
625 &ifr->ifr_ifru.ifru_lifetime;
628 * XXX: adjust expiration time assuming time_t is
632 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
633 if (ia->ia6_lifetime.ia6t_pltime <
634 maxexpire - ia->ia6_updatetime) {
635 retlt->ia6t_preferred = ia->ia6_updatetime +
636 ia->ia6_lifetime.ia6t_pltime;
638 retlt->ia6t_preferred = maxexpire;
642 case SIOCSIFALIFETIME_IN6:
643 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
645 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
646 ia->ia6_lifetime.ia6t_expire =
647 time_uptime + ia->ia6_lifetime.ia6t_vltime;
649 ia->ia6_lifetime.ia6t_expire = 0;
650 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
651 ia->ia6_lifetime.ia6t_preferred =
652 time_uptime + ia->ia6_lifetime.ia6t_pltime;
654 ia->ia6_lifetime.ia6t_preferred = 0;
657 case SIOCAIFADDR_IN6:
660 struct nd_prefixctl pr0;
661 struct nd_prefix *pr;
664 * first, make or update the interface address structure,
665 * and link it to the list.
667 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
670 ifa_free(&ia->ia_ifa);
671 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
674 * this can happen when the user specify the 0 valid
680 if (cmd == ocmd && ifra->ifra_vhid > 0) {
681 if (carp_attach_p != NULL)
682 error = (*carp_attach_p)(&ia->ia_ifa,
685 error = EPROTONOSUPPORT;
693 * then, make the prefix on-link on the interface.
694 * XXX: we'd rather create the prefix before the address, but
695 * we need at least one address to install the corresponding
696 * interface route, so we configure the address first.
700 * convert mask to prefix length (prefixmask has already
701 * been validated in in6_update_ifa().
703 bzero(&pr0, sizeof(pr0));
705 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
707 if (pr0.ndpr_plen == 128) {
708 break; /* we don't need to install a host route. */
710 pr0.ndpr_prefix = ifra->ifra_addr;
711 /* apply the mask for safety. */
712 for (i = 0; i < 4; i++) {
713 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
714 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
717 * XXX: since we don't have an API to set prefix (not address)
718 * lifetimes, we just use the same lifetimes as addresses.
719 * The (temporarily) installed lifetimes can be overridden by
720 * later advertised RAs (when accept_rtadv is non 0), which is
721 * an intended behavior.
723 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
725 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
726 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
727 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
729 /* add the prefix if not yet. */
730 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
732 * nd6_prelist_add will install the corresponding
735 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) {
737 (*carp_detach_p)(&ia->ia_ifa);
742 (*carp_detach_p)(&ia->ia_ifa);
743 log(LOG_ERR, "nd6_prelist_add succeeded but "
750 /* relate the address to the prefix */
751 if (ia->ia6_ndpr == NULL) {
756 * If this is the first autoconf address from the
757 * prefix, create a temporary address as well
760 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
761 V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
763 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
764 log(LOG_NOTICE, "in6_control: failed "
765 "to create a temporary address, "
772 * this might affect the status of autoconfigured addresses,
773 * that is, this address might make other addresses detached.
775 pfxlist_onlink_check();
776 if (error == 0 && ia) {
777 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
779 * Try to clear the flag when a new
780 * IPv6 address is added onto an
781 * IFDISABLED interface and it
784 struct in6_ndireq nd;
786 memset(&nd, 0, sizeof(nd));
787 nd.ndi.flags = ND_IFINFO(ifp)->flags;
788 nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
789 if (nd6_ioctl(SIOCSIFINFO_FLAGS,
790 (caddr_t)&nd, ifp) < 0)
791 log(LOG_NOTICE, "SIOCAIFADDR_IN6: "
792 "SIOCSIFINFO_FLAGS for -ifdisabled "
795 * Ignore failure of clearing the flag
796 * intentionally. The failure means
797 * address duplication was detected.
800 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
805 case SIOCDIFADDR_IN6:
807 struct nd_prefix *pr;
810 * If the address being deleted is the only one that owns
811 * the corresponding prefix, expire the prefix as well.
812 * XXX: theoretically, we don't have to worry about such
813 * relationship, since we separate the address management
814 * and the prefix management. We do this, however, to provide
815 * as much backward compatibility as possible in terms of
816 * the ioctl operation.
817 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
820 in6_purgeaddr(&ia->ia_ifa);
821 if (pr && pr->ndpr_refcnt == 0)
823 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
828 if (ifp == NULL || ifp->if_ioctl == 0) {
832 error = (*ifp->if_ioctl)(ifp, cmd, data);
839 ifa_free(&ia->ia_ifa);
845 * Join necessary multicast groups. Factored out from in6_update_ifa().
846 * This entire work should only be done once, for the default FIB.
849 in6_update_ifa_join_mc(struct ifnet *ifp, struct in6_aliasreq *ifra,
850 struct in6_ifaddr *ia, int flags, struct in6_multi **in6m_sol)
852 char ip6buf[INET6_ADDRSTRLEN];
853 struct sockaddr_in6 mltaddr, mltmask;
854 struct in6_addr llsol;
855 struct in6_multi_mship *imm;
859 KASSERT(in6m_sol != NULL, ("%s: in6m_sol is NULL", __func__));
861 /* Join solicited multicast addr for new host id. */
862 bzero(&llsol, sizeof(struct in6_addr));
863 llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
864 llsol.s6_addr32[1] = 0;
865 llsol.s6_addr32[2] = htonl(1);
866 llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
867 llsol.s6_addr8[12] = 0xff;
868 if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
869 /* XXX: should not happen */
870 log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
874 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
876 * We need a random delay for DAD on the address being
877 * configured. It also means delaying transmission of the
878 * corresponding MLD report to avoid report collision.
879 * [RFC 4861, Section 6.3.7]
881 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
883 imm = in6_joingroup(ifp, &llsol, &error, delay);
885 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
886 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &llsol),
887 if_name(ifp), error));
890 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
891 *in6m_sol = imm->i6mm_maddr;
893 bzero(&mltmask, sizeof(mltmask));
894 mltmask.sin6_len = sizeof(struct sockaddr_in6);
895 mltmask.sin6_family = AF_INET6;
896 mltmask.sin6_addr = in6mask32;
897 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */
900 * Join link-local all-nodes address.
902 bzero(&mltaddr, sizeof(mltaddr));
903 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
904 mltaddr.sin6_family = AF_INET6;
905 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
906 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
907 goto cleanup; /* XXX: should not fail */
910 * XXX: do we really need this automatic routes? We should probably
911 * reconsider this stuff. Most applications actually do not need the
912 * routes, since they usually specify the outgoing interface.
914 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
916 /* XXX: only works in !SCOPEDROUTING case. */
917 if (memcmp(&mltaddr.sin6_addr,
918 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
925 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
926 (struct sockaddr *)&ia->ia_addr,
927 (struct sockaddr *)&mltmask, RTF_UP,
928 (struct rtentry **)0, RT_DEFAULT_FIB);
934 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
936 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
937 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
938 &mltaddr.sin6_addr), if_name(ifp), error));
941 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
944 * Join node information group address.
947 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
949 * The spec does not say anything about delay for this group,
950 * but the same logic should apply.
952 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
954 if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
956 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, delay);
958 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
959 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
960 &mltaddr.sin6_addr), if_name(ifp), error));
961 /* XXX not very fatal, go on... */
963 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
965 if (V_icmp6_nodeinfo_oldmcprefix &&
966 in6_nigroup_oldmcprefix(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
967 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, delay);
969 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
970 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
971 &mltaddr.sin6_addr), if_name(ifp), error));
972 /* XXX not very fatal, go on... */
974 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
978 * Join interface-local all-nodes address.
979 * (ff01::1%ifN, and ff01::%ifN/32)
981 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
982 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
983 goto cleanup; /* XXX: should not fail */
984 /* XXX: again, do we really need the route? */
985 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
987 if (memcmp(&mltaddr.sin6_addr,
988 &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
995 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
996 (struct sockaddr *)&ia->ia_addr,
997 (struct sockaddr *)&mltmask, RTF_UP,
998 (struct rtentry **)0, RT_DEFAULT_FIB);
1004 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1006 nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
1007 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
1008 &mltaddr.sin6_addr), if_name(ifp), error));
1011 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1019 * Update parameters of an IPv6 interface address.
1020 * If necessary, a new entry is created and linked into address chains.
1021 * This function is separated from in6_control().
1024 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
1025 struct in6_ifaddr *ia, int flags)
1027 int error = 0, hostIsNew = 0, plen = -1;
1028 struct sockaddr_in6 dst6;
1029 struct in6_addrlifetime *lt;
1030 struct in6_multi *in6m_sol;
1032 char ip6buf[INET6_ADDRSTRLEN];
1034 /* Validate parameters */
1035 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
1039 * The destination address for a p2p link must have a family
1040 * of AF_UNSPEC or AF_INET6.
1042 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1043 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
1044 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
1045 return (EAFNOSUPPORT);
1047 * validate ifra_prefixmask. don't check sin6_family, netmask
1048 * does not carry fields other than sin6_len.
1050 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
1053 * Because the IPv6 address architecture is classless, we require
1054 * users to specify a (non 0) prefix length (mask) for a new address.
1055 * We also require the prefix (when specified) mask is valid, and thus
1056 * reject a non-consecutive mask.
1058 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
1060 if (ifra->ifra_prefixmask.sin6_len != 0) {
1061 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
1062 (u_char *)&ifra->ifra_prefixmask +
1063 ifra->ifra_prefixmask.sin6_len);
1068 * In this case, ia must not be NULL. We just use its prefix
1071 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1074 * If the destination address on a p2p interface is specified,
1075 * and the address is a scoped one, validate/set the scope
1078 dst6 = ifra->ifra_dstaddr;
1079 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
1080 (dst6.sin6_family == AF_INET6)) {
1081 struct in6_addr in6_tmp;
1084 in6_tmp = dst6.sin6_addr;
1085 if (in6_setscope(&in6_tmp, ifp, &zoneid))
1086 return (EINVAL); /* XXX: should be impossible */
1088 if (dst6.sin6_scope_id != 0) {
1089 if (dst6.sin6_scope_id != zoneid)
1091 } else /* user omit to specify the ID. */
1092 dst6.sin6_scope_id = zoneid;
1094 /* convert into the internal form */
1095 if (sa6_embedscope(&dst6, 0))
1096 return (EINVAL); /* XXX: should be impossible */
1099 * The destination address can be specified only for a p2p or a
1100 * loopback interface. If specified, the corresponding prefix length
1103 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
1104 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
1105 /* XXX: noisy message */
1106 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
1107 "be specified for a p2p or a loopback IF only\n"));
1111 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
1112 "be 128 when dstaddr is specified\n"));
1116 /* lifetime consistency check */
1117 lt = &ifra->ifra_lifetime;
1118 if (lt->ia6t_pltime > lt->ia6t_vltime)
1120 if (lt->ia6t_vltime == 0) {
1122 * the following log might be noisy, but this is a typical
1123 * configuration mistake or a tool's bug.
1126 "in6_update_ifa: valid lifetime is 0 for %s\n",
1127 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
1130 return (0); /* there's nothing to do */
1134 * If this is a new address, allocate a new ifaddr and link it
1140 * When in6_update_ifa() is called in a process of a received
1141 * RA, it is called under an interrupt context. So, we should
1142 * call malloc with M_NOWAIT.
1144 ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
1148 bzero((caddr_t)ia, sizeof(*ia));
1149 ifa_init(&ia->ia_ifa);
1150 LIST_INIT(&ia->ia6_memberships);
1151 /* Initialize the address and masks, and put time stamp */
1152 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1153 ia->ia_addr.sin6_family = AF_INET6;
1154 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
1155 ia->ia6_createtime = time_uptime;
1156 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
1158 * XXX: some functions expect that ifa_dstaddr is not
1159 * NULL for p2p interfaces.
1161 ia->ia_ifa.ifa_dstaddr =
1162 (struct sockaddr *)&ia->ia_dstaddr;
1164 ia->ia_ifa.ifa_dstaddr = NULL;
1166 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
1168 ifa_ref(&ia->ia_ifa); /* if_addrhead */
1170 TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1171 IF_ADDR_WUNLOCK(ifp);
1173 ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */
1175 TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
1176 LIST_INSERT_HEAD(IN6ADDR_HASH(&ifra->ifra_addr.sin6_addr),
1178 IN6_IFADDR_WUNLOCK();
1181 /* update timestamp */
1182 ia->ia6_updatetime = time_uptime;
1184 /* set prefix mask */
1185 if (ifra->ifra_prefixmask.sin6_len) {
1187 * We prohibit changing the prefix length of an existing
1189 * + such an operation should be rare in IPv6, and
1190 * + the operation would confuse prefix management.
1192 if (ia->ia_prefixmask.sin6_len &&
1193 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1194 nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
1195 " existing (%s) address should not be changed\n",
1196 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1200 ia->ia_prefixmask = ifra->ifra_prefixmask;
1201 ia->ia_prefixmask.sin6_family = AF_INET6;
1205 * If a new destination address is specified, scrub the old one and
1206 * install the new destination. Note that the interface must be
1207 * p2p or loopback (see the check above.)
1209 if (dst6.sin6_family == AF_INET6 &&
1210 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
1213 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1214 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
1215 nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
1216 "a route to the old destination: %s\n",
1217 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1218 /* proceed anyway... */
1220 ia->ia_flags &= ~IFA_ROUTE;
1221 ia->ia_dstaddr = dst6;
1225 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1226 * to see if the address is deprecated or invalidated, but initialize
1227 * these members for applications.
1229 ia->ia6_lifetime = ifra->ifra_lifetime;
1230 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1231 ia->ia6_lifetime.ia6t_expire =
1232 time_uptime + ia->ia6_lifetime.ia6t_vltime;
1234 ia->ia6_lifetime.ia6t_expire = 0;
1235 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1236 ia->ia6_lifetime.ia6t_preferred =
1237 time_uptime + ia->ia6_lifetime.ia6t_pltime;
1239 ia->ia6_lifetime.ia6t_preferred = 0;
1241 /* reset the interface and routing table appropriately. */
1242 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1246 * configure address flags.
1248 ia->ia6_flags = ifra->ifra_flags;
1250 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1251 * userland, make it deprecated.
1253 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1254 ia->ia6_lifetime.ia6t_pltime = 0;
1255 ia->ia6_lifetime.ia6t_preferred = time_uptime;
1258 * Make the address tentative before joining multicast addresses,
1259 * so that corresponding MLD responses would not have a tentative
1262 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1263 if (hostIsNew && in6if_do_dad(ifp))
1264 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1266 /* DAD should be performed after ND6_IFF_IFDISABLED is cleared. */
1267 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1268 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1271 * We are done if we have simply modified an existing address.
1277 * Beyond this point, we should call in6_purgeaddr upon an error,
1278 * not just go to unlink.
1281 /* Join necessary multicast groups. */
1283 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1284 error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol);
1290 * Perform DAD, if needed.
1291 * XXX It may be of use, if we can administratively disable DAD.
1293 if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1294 (ia->ia6_flags & IN6_IFF_TENTATIVE))
1296 int mindelay, maxdelay;
1299 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1301 * We need to impose a delay before sending an NS
1302 * for DAD. Check if we also needed a delay for the
1303 * corresponding MLD message. If we did, the delay
1304 * should be larger than the MLD delay (this could be
1305 * relaxed a bit, but this simple logic is at least
1307 * XXX: Break data hiding guidelines and look at
1308 * state for the solicited multicast group.
1311 if (in6m_sol != NULL &&
1312 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1313 mindelay = in6m_sol->in6m_timer;
1315 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1316 if (maxdelay - mindelay == 0)
1320 (arc4random() % (maxdelay - mindelay)) +
1324 nd6_dad_start((struct ifaddr *)ia, delay);
1327 KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew"));
1328 ifa_free(&ia->ia_ifa);
1333 * XXX: if a change of an existing address failed, keep the entry
1337 in6_unlink_ifa(ia, ifp);
1338 ifa_free(&ia->ia_ifa);
1343 KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew"));
1344 ifa_free(&ia->ia_ifa);
1345 in6_purgeaddr(&ia->ia_ifa);
1350 * Leave multicast groups. Factored out from in6_purgeaddr().
1351 * This entire work should only be done once, for the default FIB.
1354 in6_purgeaddr_mc(struct ifnet *ifp, struct in6_ifaddr *ia, struct ifaddr *ifa0)
1356 struct sockaddr_in6 mltaddr, mltmask;
1357 struct in6_multi_mship *imm;
1359 struct sockaddr_in6 sin6;
1363 * Leave from multicast groups we have joined for the interface.
1365 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1366 LIST_REMOVE(imm, i6mm_chain);
1367 in6_leavegroup(imm);
1371 * Remove the link-local all-nodes address.
1373 bzero(&mltmask, sizeof(mltmask));
1374 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1375 mltmask.sin6_family = AF_INET6;
1376 mltmask.sin6_addr = in6mask32;
1378 bzero(&mltaddr, sizeof(mltaddr));
1379 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1380 mltaddr.sin6_family = AF_INET6;
1381 mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1383 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1387 * As for the mltaddr above, proactively prepare the sin6 to avoid
1388 * rtentry un- and re-locking.
1391 bzero(&sin6, sizeof(sin6));
1392 sin6.sin6_len = sizeof(sin6);
1393 sin6.sin6_family = AF_INET6;
1394 memcpy(&sin6.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1395 sizeof(sin6.sin6_addr));
1396 error = in6_setscope(&sin6.sin6_addr, ifa0->ifa_ifp, NULL);
1401 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
1402 if (rt != NULL && rt->rt_gateway != NULL &&
1403 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1404 &ia->ia_addr.sin6_addr,
1405 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1407 * If no more IPv6 address exists on this interface then
1408 * remove the multicast address route.
1411 memcpy(&mltaddr.sin6_addr,
1412 &satosin6(rt_key(rt))->sin6_addr,
1413 sizeof(mltaddr.sin6_addr));
1415 error = in6_rtrequest(RTM_DELETE,
1416 (struct sockaddr *)&mltaddr,
1417 (struct sockaddr *)&ia->ia_addr,
1418 (struct sockaddr *)&mltmask, RTF_UP,
1419 (struct rtentry **)0, RT_DEFAULT_FIB);
1421 log(LOG_INFO, "%s: link-local all-nodes "
1422 "multicast address deletion error\n",
1426 * Replace the gateway of the route.
1428 memcpy(rt->rt_gateway, &sin6, sizeof(sin6));
1437 * Remove the node-local all-nodes address.
1439 mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1440 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1443 rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
1444 if (rt != NULL && rt->rt_gateway != NULL &&
1445 (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1446 &ia->ia_addr.sin6_addr,
1447 sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1449 * If no more IPv6 address exists on this interface then
1450 * remove the multicast address route.
1453 memcpy(&mltaddr.sin6_addr,
1454 &satosin6(rt_key(rt))->sin6_addr,
1455 sizeof(mltaddr.sin6_addr));
1458 error = in6_rtrequest(RTM_DELETE,
1459 (struct sockaddr *)&mltaddr,
1460 (struct sockaddr *)&ia->ia_addr,
1461 (struct sockaddr *)&mltmask, RTF_UP,
1462 (struct rtentry **)0, RT_DEFAULT_FIB);
1464 log(LOG_INFO, "%s: node-local all-nodes"
1465 "multicast address deletion error\n",
1469 * Replace the gateway of the route.
1471 memcpy(rt->rt_gateway, &sin6, sizeof(sin6));
1483 in6_purgeaddr(struct ifaddr *ifa)
1485 struct ifnet *ifp = ifa->ifa_ifp;
1486 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1488 struct ifaddr *ifa0;
1491 (*carp_detach_p)(ifa);
1494 * find another IPv6 address as the gateway for the
1495 * link-local and node-local all-nodes multicast
1499 TAILQ_FOREACH(ifa0, &ifp->if_addrhead, ifa_link) {
1500 if ((ifa0->ifa_addr->sa_family != AF_INET6) ||
1501 memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr,
1502 &ia->ia_addr.sin6_addr, sizeof(struct in6_addr)) == 0)
1509 IF_ADDR_RUNLOCK(ifp);
1512 * Remove the loopback route to the interface address.
1513 * The check for the current setting of "nd6_useloopback"
1516 if (ia->ia_flags & IFA_RTSELF) {
1517 error = ifa_del_loopback_route((struct ifaddr *)ia,
1518 (struct sockaddr *)&ia->ia_addr);
1520 ia->ia_flags &= ~IFA_RTSELF;
1523 /* stop DAD processing */
1526 /* Remove local address entry from lltable. */
1529 /* Leave multicast groups. */
1530 error = in6_purgeaddr_mc(ifp, ia, ifa0);
1535 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1536 if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1537 error = rtinit(&(ia->ia_ifa), RTM_DELETE, ia->ia_flags |
1538 (ia->ia_dstaddr.sin6_family == AF_INET6) ? RTF_HOST : 0);
1540 log(LOG_INFO, "%s: err=%d, destination address delete "
1541 "failed\n", __func__, error);
1542 ia->ia_flags &= ~IFA_ROUTE;
1545 in6_unlink_ifa(ia, ifp);
1549 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1553 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1554 IF_ADDR_WUNLOCK(ifp);
1555 ifa_free(&ia->ia_ifa); /* if_addrhead */
1558 * Defer the release of what might be the last reference to the
1559 * in6_ifaddr so that it can't be freed before the remainder of the
1563 TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1564 LIST_REMOVE(ia, ia6_hash);
1565 IN6_IFADDR_WUNLOCK();
1568 * Release the reference to the base prefix. There should be a
1569 * positive reference.
1571 if (ia->ia6_ndpr == NULL) {
1573 "in6_unlink_ifa: autoconf'ed address "
1574 "%p has no prefix\n", ia));
1576 ia->ia6_ndpr->ndpr_refcnt--;
1577 ia->ia6_ndpr = NULL;
1581 * Also, if the address being removed is autoconf'ed, call
1582 * pfxlist_onlink_check() since the release might affect the status of
1583 * other (detached) addresses.
1585 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1586 pfxlist_onlink_check();
1588 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */
1592 in6_purgeif(struct ifnet *ifp)
1594 struct ifaddr *ifa, *nifa;
1596 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1597 if (ifa->ifa_addr->sa_family != AF_INET6)
1607 * SIOCGLIFADDR: get first address. (?)
1608 * SIOCGLIFADDR with IFLR_PREFIX:
1609 * get first address that matches the specified prefix.
1610 * SIOCALIFADDR: add the specified address.
1611 * SIOCALIFADDR with IFLR_PREFIX:
1612 * add the specified prefix, filling hostid part from
1613 * the first link-local address. prefixlen must be <= 64.
1614 * SIOCDLIFADDR: delete the specified address.
1615 * SIOCDLIFADDR with IFLR_PREFIX:
1616 * delete the first address that matches the specified prefix.
1618 * EINVAL on invalid parameters
1619 * EADDRNOTAVAIL on prefix match failed/specified address not found
1620 * other values may be returned from in6_ioctl()
1622 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1623 * this is to accomodate address naming scheme other than RFC2374,
1625 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1626 * address encoding scheme. (see figure on page 8)
1629 in6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1630 struct ifnet *ifp, struct thread *td)
1632 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1634 struct sockaddr *sa;
1637 if (!data || !ifp) {
1638 panic("invalid argument to in6_lifaddr_ioctl");
1644 /* address must be specified on GET with IFLR_PREFIX */
1645 if ((iflr->flags & IFLR_PREFIX) == 0)
1650 /* address must be specified on ADD and DELETE */
1651 sa = (struct sockaddr *)&iflr->addr;
1652 if (sa->sa_family != AF_INET6)
1654 if (sa->sa_len != sizeof(struct sockaddr_in6))
1656 /* XXX need improvement */
1657 sa = (struct sockaddr *)&iflr->dstaddr;
1658 if (sa->sa_family && sa->sa_family != AF_INET6)
1660 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1663 default: /* shouldn't happen */
1665 panic("invalid cmd to in6_lifaddr_ioctl");
1671 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1677 struct in6_aliasreq ifra;
1678 struct in6_addr *hostid = NULL;
1682 if ((iflr->flags & IFLR_PREFIX) != 0) {
1683 struct sockaddr_in6 *sin6;
1686 * hostid is to fill in the hostid part of the
1687 * address. hostid points to the first link-local
1688 * address attached to the interface.
1690 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1692 return EADDRNOTAVAIL;
1693 hostid = IFA_IN6(ifa);
1695 /* prefixlen must be <= 64. */
1696 if (64 < iflr->prefixlen) {
1701 prefixlen = iflr->prefixlen;
1703 /* hostid part must be zero. */
1704 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1705 if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1706 sin6->sin6_addr.s6_addr32[3] != 0) {
1712 prefixlen = iflr->prefixlen;
1714 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1715 bzero(&ifra, sizeof(ifra));
1716 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1718 bcopy(&iflr->addr, &ifra.ifra_addr,
1719 ((struct sockaddr *)&iflr->addr)->sa_len);
1721 /* fill in hostid part */
1722 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1723 hostid->s6_addr32[2];
1724 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1725 hostid->s6_addr32[3];
1728 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1729 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1730 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1732 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1733 hostid->s6_addr32[2];
1734 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1735 hostid->s6_addr32[3];
1741 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1742 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1744 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1745 return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1750 struct in6_ifaddr *ia;
1751 struct in6_addr mask, candidate, match;
1752 struct sockaddr_in6 *sin6;
1755 bzero(&mask, sizeof(mask));
1756 if (iflr->flags & IFLR_PREFIX) {
1757 /* lookup a prefix rather than address. */
1758 in6_prefixlen2mask(&mask, iflr->prefixlen);
1760 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1761 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1762 match.s6_addr32[0] &= mask.s6_addr32[0];
1763 match.s6_addr32[1] &= mask.s6_addr32[1];
1764 match.s6_addr32[2] &= mask.s6_addr32[2];
1765 match.s6_addr32[3] &= mask.s6_addr32[3];
1767 /* if you set extra bits, that's wrong */
1768 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1773 if (cmd == SIOCGLIFADDR) {
1774 /* on getting an address, take the 1st match */
1777 /* on deleting an address, do exact match */
1778 in6_prefixlen2mask(&mask, 128);
1779 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1780 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1787 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1788 if (ifa->ifa_addr->sa_family != AF_INET6)
1794 * XXX: this is adhoc, but is necessary to allow
1795 * a user to specify fe80::/64 (not /10) for a
1796 * link-local address.
1798 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1799 in6_clearscope(&candidate);
1800 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1801 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1802 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1803 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1804 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1809 IF_ADDR_RUNLOCK(ifp);
1811 return EADDRNOTAVAIL;
1814 if (cmd == SIOCGLIFADDR) {
1817 /* fill in the if_laddrreq structure */
1818 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1819 error = sa6_recoverscope(
1820 (struct sockaddr_in6 *)&iflr->addr);
1826 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1827 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1828 ia->ia_dstaddr.sin6_len);
1829 error = sa6_recoverscope(
1830 (struct sockaddr_in6 *)&iflr->dstaddr);
1836 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1839 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1841 iflr->flags = ia->ia6_flags; /* XXX */
1846 struct in6_aliasreq ifra;
1848 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1849 bzero(&ifra, sizeof(ifra));
1850 bcopy(iflr->iflr_name, ifra.ifra_name,
1851 sizeof(ifra.ifra_name));
1853 bcopy(&ia->ia_addr, &ifra.ifra_addr,
1854 ia->ia_addr.sin6_len);
1855 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1856 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1857 ia->ia_dstaddr.sin6_len);
1859 bzero(&ifra.ifra_dstaddr,
1860 sizeof(ifra.ifra_dstaddr));
1862 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1863 ia->ia_prefixmask.sin6_len);
1865 ifra.ifra_flags = ia->ia6_flags;
1867 return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1873 return EOPNOTSUPP; /* just for safety */
1877 * Initialize an interface's IPv6 address and routing table entry.
1880 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1881 struct sockaddr_in6 *sin6, int newhost)
1883 int error = 0, plen, ifacount = 0;
1887 * Give the interface a chance to initialize
1888 * if this is its first address,
1889 * and to validate the address if necessary.
1892 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1893 if (ifa->ifa_addr->sa_family != AF_INET6)
1897 IF_ADDR_RUNLOCK(ifp);
1899 ia->ia_addr = *sin6;
1901 if (ifacount <= 1 && ifp->if_ioctl) {
1902 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1907 ia->ia_ifa.ifa_metric = ifp->if_metric;
1909 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1913 * If a new destination address is specified for a point-to-point
1914 * interface, install a route to the destination as an interface
1916 * XXX: the logic below rejects assigning multiple addresses on a p2p
1917 * interface that share the same destination.
1919 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1920 if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1921 ia->ia_dstaddr.sin6_family == AF_INET6) {
1922 int rtflags = RTF_UP | RTF_HOST;
1923 error = rtinit(&ia->ia_ifa, RTM_ADD, ia->ia_flags | rtflags);
1926 ia->ia_flags |= IFA_ROUTE;
1928 * Handle the case for ::1 .
1930 if (ifp->if_flags & IFF_LOOPBACK)
1931 ia->ia_flags |= IFA_RTSELF;
1935 * add a loopback route to self
1937 if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
1938 error = ifa_add_loopback_route((struct ifaddr *)ia,
1939 (struct sockaddr *)&ia->ia_addr);
1941 ia->ia_flags |= IFA_RTSELF;
1944 /* Add local address to lltable, if necessary (ex. on p2p link). */
1946 in6_ifaddloop(&(ia->ia_ifa));
1952 * Find an IPv6 interface link-local address specific to an interface.
1953 * ifaddr is returned referenced.
1956 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1961 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1962 if (ifa->ifa_addr->sa_family != AF_INET6)
1964 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1965 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1972 IF_ADDR_RUNLOCK(ifp);
1974 return ((struct in6_ifaddr *)ifa);
1979 * find the internet address corresponding to a given interface and address.
1980 * ifaddr is returned referenced.
1983 in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
1988 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1989 if (ifa->ifa_addr->sa_family != AF_INET6)
1991 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1996 IF_ADDR_RUNLOCK(ifp);
1998 return ((struct in6_ifaddr *)ifa);
2002 * Find a link-local scoped address on ifp and return it if any.
2005 in6ifa_llaonifp(struct ifnet *ifp)
2007 struct sockaddr_in6 *sin6;
2010 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
2013 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2014 if (ifa->ifa_addr->sa_family != AF_INET6)
2016 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
2017 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
2018 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
2019 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
2022 if_addr_runlock(ifp);
2024 return ((struct in6_ifaddr *)ifa);
2028 * Convert IP6 address to printable (loggable) representation. Caller
2029 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
2031 static char digits[] = "0123456789abcdef";
2033 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
2035 int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
2037 const u_int16_t *a = (const u_int16_t *)addr;
2039 int dcolon = 0, zero = 0;
2043 for (i = 0; i < 8; i++) {
2044 if (*(a + i) == 0) {
2049 else if (maxcnt < cnt) {
2060 for (i = 0; i < 8; i++) {
2071 if (dcolon == 0 && *(a + 1) == 0 && i == index) {
2083 d = (const u_char *)a;
2084 /* Try to eliminate leading zeros in printout like in :0001. */
2086 *cp = digits[*d >> 4];
2091 *cp = digits[*d++ & 0xf];
2092 if (zero == 0 || (*cp != '0')) {
2096 *cp = digits[*d >> 4];
2097 if (zero == 0 || (*cp != '0')) {
2101 *cp++ = digits[*d & 0xf];
2110 in6_localaddr(struct in6_addr *in6)
2112 struct in6_ifaddr *ia;
2114 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
2118 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
2119 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
2120 &ia->ia_prefixmask.sin6_addr)) {
2121 IN6_IFADDR_RUNLOCK();
2125 IN6_IFADDR_RUNLOCK();
2131 * Return 1 if an internet address is for the local host and configured
2132 * on one of its interfaces.
2135 in6_localip(struct in6_addr *in6)
2137 struct in6_ifaddr *ia;
2140 LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) {
2141 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) {
2142 IN6_IFADDR_RUNLOCK();
2146 IN6_IFADDR_RUNLOCK();
2151 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
2153 struct in6_ifaddr *ia;
2156 LIST_FOREACH(ia, IN6ADDR_HASH(&sa6->sin6_addr), ia6_hash) {
2157 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), &sa6->sin6_addr)) {
2158 if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
2159 IN6_IFADDR_RUNLOCK();
2160 return (1); /* true */
2165 IN6_IFADDR_RUNLOCK();
2167 return (0); /* false */
2171 * return length of part which dst and src are equal
2175 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
2178 u_char *s = (u_char *)src, *d = (u_char *)dst;
2179 u_char *lim = s + 16, r;
2182 if ((r = (*d++ ^ *s++)) != 0) {
2193 /* XXX: to be scope conscious */
2195 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
2197 int bytelen, bitlen;
2200 if (0 > len || len > 128) {
2201 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2209 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2212 p1->s6_addr[bytelen] >> (8 - bitlen) !=
2213 p2->s6_addr[bytelen] >> (8 - bitlen))
2220 in6_prefixlen2mask(struct in6_addr *maskp, int len)
2222 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2223 int bytelen, bitlen, i;
2226 if (0 > len || len > 128) {
2227 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2232 bzero(maskp, sizeof(*maskp));
2235 for (i = 0; i < bytelen; i++)
2236 maskp->s6_addr[i] = 0xff;
2238 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2242 * return the best address out of the same scope. if no address was
2243 * found, return the first valid address from designated IF.
2246 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
2248 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
2250 struct in6_ifaddr *besta = 0;
2251 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2253 dep[0] = dep[1] = NULL;
2256 * We first look for addresses in the same scope.
2257 * If there is one, return it.
2258 * If two or more, return one which matches the dst longest.
2259 * If none, return one of global addresses assigned other ifs.
2262 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2263 if (ifa->ifa_addr->sa_family != AF_INET6)
2265 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2266 continue; /* XXX: is there any case to allow anycast? */
2267 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2268 continue; /* don't use this interface */
2269 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2271 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2272 if (V_ip6_use_deprecated)
2273 dep[0] = (struct in6_ifaddr *)ifa;
2277 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2279 * call in6_matchlen() as few as possible
2283 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2284 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2287 besta = (struct in6_ifaddr *)ifa;
2290 besta = (struct in6_ifaddr *)ifa;
2294 ifa_ref(&besta->ia_ifa);
2295 IF_ADDR_RUNLOCK(ifp);
2299 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2300 if (ifa->ifa_addr->sa_family != AF_INET6)
2302 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2303 continue; /* XXX: is there any case to allow anycast? */
2304 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2305 continue; /* don't use this interface */
2306 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2308 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2309 if (V_ip6_use_deprecated)
2310 dep[1] = (struct in6_ifaddr *)ifa;
2316 IF_ADDR_RUNLOCK(ifp);
2317 return (struct in6_ifaddr *)ifa;
2320 /* use the last-resort values, that are, deprecated addresses */
2322 ifa_ref((struct ifaddr *)dep[0]);
2323 IF_ADDR_RUNLOCK(ifp);
2327 ifa_ref((struct ifaddr *)dep[1]);
2328 IF_ADDR_RUNLOCK(ifp);
2332 IF_ADDR_RUNLOCK(ifp);
2337 * perform DAD when interface becomes IFF_UP.
2340 in6_if_up(struct ifnet *ifp)
2343 struct in6_ifaddr *ia;
2346 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2347 if (ifa->ifa_addr->sa_family != AF_INET6)
2349 ia = (struct in6_ifaddr *)ifa;
2350 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2352 * The TENTATIVE flag was likely set by hand
2353 * beforehand, implicitly indicating the need for DAD.
2354 * We may be able to skip the random delay in this
2355 * case, but we impose delays just in case.
2358 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2361 IF_ADDR_RUNLOCK(ifp);
2364 * special cases, like 6to4, are handled in in6_ifattach
2366 in6_ifattach(ifp, NULL);
2370 in6if_do_dad(struct ifnet *ifp)
2372 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2375 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
2378 switch (ifp->if_type) {
2384 * These interfaces do not have the IFF_LOOPBACK flag,
2385 * but loop packets back. We do not have to do DAD on such
2386 * interfaces. We should even omit it, because loop-backed
2387 * NS would confuse the DAD procedure.
2392 * Our DAD routine requires the interface up and running.
2393 * However, some interfaces can be up before the RUNNING
2394 * status. Additionaly, users may try to assign addresses
2395 * before the interface becomes up (or running).
2396 * We simply skip DAD in such a case as a work around.
2397 * XXX: we should rather mark "tentative" on such addresses,
2398 * and do DAD after the interface becomes ready.
2400 if (!((ifp->if_flags & IFF_UP) &&
2401 (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2409 * Calculate max IPv6 MTU through all the interfaces and store it
2415 unsigned long maxmtu = 0;
2418 IFNET_RLOCK_NOSLEEP();
2419 TAILQ_FOREACH(ifp, &V_ifnet, if_list) {
2420 /* this function can be called during ifnet initialization */
2421 if (!ifp->if_afdata[AF_INET6])
2423 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2424 IN6_LINKMTU(ifp) > maxmtu)
2425 maxmtu = IN6_LINKMTU(ifp);
2427 IFNET_RUNLOCK_NOSLEEP();
2428 if (maxmtu) /* update only when maxmtu is positive */
2429 V_in6_maxmtu = maxmtu;
2433 * Provide the length of interface identifiers to be used for the link attached
2434 * to the given interface. The length should be defined in "IPv6 over
2435 * xxx-link" document. Note that address architecture might also define
2436 * the length for a particular set of address prefixes, regardless of the
2437 * link type. As clarified in rfc2462bis, those two definitions should be
2438 * consistent, and those really are as of August 2004.
2441 in6_if2idlen(struct ifnet *ifp)
2443 switch (ifp->if_type) {
2444 case IFT_ETHER: /* RFC2464 */
2445 #ifdef IFT_PROPVIRTUAL
2446 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2449 case IFT_L2VLAN: /* ditto */
2451 #ifdef IFT_IEEE80211
2452 case IFT_IEEE80211: /* ditto */
2455 case IFT_MIP: /* ditto */
2457 case IFT_INFINIBAND:
2459 case IFT_FDDI: /* RFC2467 */
2461 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */
2463 case IFT_PPP: /* RFC2472 */
2465 case IFT_ARCNET: /* RFC2497 */
2467 case IFT_FRELAY: /* RFC2590 */
2469 case IFT_IEEE1394: /* RFC3146 */
2472 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2474 return (64); /* XXX: is this really correct? */
2477 * Unknown link type:
2478 * It might be controversial to use the today's common constant
2479 * of 64 for these cases unconditionally. For full compliance,
2480 * we should return an error in this case. On the other hand,
2481 * if we simply miss the standard for the link type or a new
2482 * standard is defined for a new link type, the IFID length
2483 * is very likely to be the common constant. As a compromise,
2484 * we always use the constant, but make an explicit notice
2485 * indicating the "unknown" case.
2487 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2492 #include <sys/sysctl.h>
2494 struct in6_llentry {
2495 struct llentry base;
2496 struct sockaddr_in6 l3_addr6;
2500 * Deletes an address from the address table.
2501 * This function is called by the timer functions
2502 * such as arptimer() and nd6_llinfo_timer(), and
2503 * the caller does the locking.
2506 in6_lltable_free(struct lltable *llt, struct llentry *lle)
2509 LLE_LOCK_DESTROY(lle);
2510 free(lle, M_LLTABLE);
2513 static struct llentry *
2514 in6_lltable_new(const struct sockaddr *l3addr, u_int flags)
2516 struct in6_llentry *lle;
2518 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
2519 if (lle == NULL) /* NB: caller generates msg */
2522 lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr;
2523 lle->base.lle_refcnt = 1;
2524 lle->base.lle_free = in6_lltable_free;
2525 LLE_LOCK_INIT(&lle->base);
2526 callout_init_rw(&lle->base.ln_timer_ch, &lle->base.lle_lock,
2527 CALLOUT_RETURNUNLOCKED);
2529 return (&lle->base);
2533 in6_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
2534 const struct sockaddr *mask, u_int flags)
2536 const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
2537 const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
2538 struct llentry *lle, *next;
2542 * (flags & LLE_STATIC) means deleting all entries
2543 * including static ND6 entries.
2545 IF_AFDATA_WLOCK(llt->llt_ifp);
2546 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2547 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
2548 if (IN6_ARE_MASKED_ADDR_EQUAL(
2549 &satosin6(L3_ADDR(lle))->sin6_addr,
2550 &pfx->sin6_addr, &msk->sin6_addr) &&
2551 ((flags & LLE_STATIC) ||
2552 !(lle->la_flags & LLE_STATIC))) {
2554 if (callout_stop(&lle->la_timer))
2560 IF_AFDATA_WUNLOCK(llt->llt_ifp);
2564 in6_lltable_rtcheck(struct ifnet *ifp,
2566 const struct sockaddr *l3addr)
2569 char ip6buf[INET6_ADDRSTRLEN];
2571 KASSERT(l3addr->sa_family == AF_INET6,
2572 ("sin_family %d", l3addr->sa_family));
2574 /* Our local addresses are always only installed on the default FIB. */
2575 /* XXX rtalloc1 should take a const param */
2576 rt = in6_rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0,
2578 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2581 * Create an ND6 cache for an IPv6 neighbor
2582 * that is not covered by our own prefix.
2584 /* XXX ifaof_ifpforaddr should take a const param */
2585 ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp);
2592 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2593 ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr));
2602 static struct llentry *
2603 in6_lltable_lookup(struct lltable *llt, u_int flags,
2604 const struct sockaddr *l3addr)
2606 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2607 struct ifnet *ifp = llt->llt_ifp;
2608 struct llentry *lle;
2609 struct llentries *lleh;
2612 IF_AFDATA_LOCK_ASSERT(ifp);
2613 KASSERT(l3addr->sa_family == AF_INET6,
2614 ("sin_family %d", l3addr->sa_family));
2616 hashkey = sin6->sin6_addr.s6_addr32[3];
2617 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
2618 LIST_FOREACH(lle, lleh, lle_next) {
2619 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle);
2620 if (lle->la_flags & LLE_DELETED)
2622 if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr,
2623 sizeof(struct in6_addr)) == 0)
2628 if (!(flags & LLE_CREATE))
2631 * A route that covers the given address must have
2632 * been installed 1st because we are doing a resolution,
2635 if (!(flags & LLE_IFADDR) &&
2636 in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2639 lle = in6_lltable_new(l3addr, flags);
2641 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2644 lle->la_flags = flags & ~LLE_CREATE;
2645 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
2646 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
2647 lle->la_flags |= (LLE_VALID | LLE_STATIC);
2651 lle->lle_head = lleh;
2652 lle->la_flags |= LLE_LINKED;
2653 LIST_INSERT_HEAD(lleh, lle, lle_next);
2654 } else if (flags & LLE_DELETE) {
2655 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
2657 lle->la_flags |= LLE_DELETED;
2659 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2661 if ((lle->la_flags &
2662 (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
2669 if (LLE_IS_VALID(lle)) {
2670 if (flags & LLE_EXCLUSIVE)
2679 in6_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
2681 struct ifnet *ifp = llt->llt_ifp;
2682 struct llentry *lle;
2685 struct rt_msghdr rtm;
2686 struct sockaddr_in6 sin6;
2688 * ndp.c assumes that sdl is word aligned
2693 struct sockaddr_dl sdl;
2697 if (ifp->if_flags & IFF_LOOPBACK)
2700 LLTABLE_LOCK_ASSERT();
2703 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2704 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
2705 struct sockaddr_dl *sdl;
2707 /* skip deleted or invalid entries */
2708 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
2710 /* Skip if jailed and not a valid IP of the prison. */
2711 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
2714 * produce a msg made of:
2716 * struct sockaddr_in6 (IPv6)
2717 * struct sockaddr_dl;
2719 bzero(&ndpc, sizeof(ndpc));
2720 ndpc.rtm.rtm_msglen = sizeof(ndpc);
2721 ndpc.rtm.rtm_version = RTM_VERSION;
2722 ndpc.rtm.rtm_type = RTM_GET;
2723 ndpc.rtm.rtm_flags = RTF_UP;
2724 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2725 ndpc.sin6.sin6_family = AF_INET6;
2726 ndpc.sin6.sin6_len = sizeof(ndpc.sin6);
2727 bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle));
2728 if (V_deembed_scopeid)
2729 sa6_recoverscope(&ndpc.sin6);
2732 if (lle->la_flags & LLE_PUB)
2733 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2736 sdl->sdl_family = AF_LINK;
2737 sdl->sdl_len = sizeof(*sdl);
2738 sdl->sdl_alen = ifp->if_addrlen;
2739 sdl->sdl_index = ifp->if_index;
2740 sdl->sdl_type = ifp->if_type;
2741 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2742 ndpc.rtm.rtm_rmx.rmx_expire =
2743 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
2744 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2745 if (lle->la_flags & LLE_STATIC)
2746 ndpc.rtm.rtm_flags |= RTF_STATIC;
2747 ndpc.rtm.rtm_index = ifp->if_index;
2748 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2757 in6_domifattach(struct ifnet *ifp)
2759 struct in6_ifextra *ext;
2761 /* There are not IPv6-capable interfaces. */
2762 switch (ifp->if_type) {
2768 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2769 bzero(ext, sizeof(*ext));
2771 ext->in6_ifstat = malloc(sizeof(counter_u64_t) *
2772 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_IFADDR, M_WAITOK);
2773 COUNTER_ARRAY_ALLOC(ext->in6_ifstat,
2774 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_WAITOK);
2776 ext->icmp6_ifstat = malloc(sizeof(counter_u64_t) *
2777 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_IFADDR,
2779 COUNTER_ARRAY_ALLOC(ext->icmp6_ifstat,
2780 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_WAITOK);
2782 ext->nd_ifinfo = nd6_ifattach(ifp);
2783 ext->scope6_id = scope6_ifattach(ifp);
2784 ext->lltable = lltable_init(ifp, AF_INET6);
2785 if (ext->lltable != NULL) {
2786 ext->lltable->llt_prefix_free = in6_lltable_prefix_free;
2787 ext->lltable->llt_lookup = in6_lltable_lookup;
2788 ext->lltable->llt_dump = in6_lltable_dump;
2791 ext->mld_ifinfo = mld_domifattach(ifp);
2797 in6_domifdetach(struct ifnet *ifp, void *aux)
2799 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2801 mld_domifdetach(ifp);
2802 scope6_ifdetach(ext->scope6_id);
2803 nd6_ifdetach(ext->nd_ifinfo);
2804 lltable_free(ext->lltable);
2805 COUNTER_ARRAY_FREE(ext->in6_ifstat,
2806 sizeof(struct in6_ifstat) / sizeof(uint64_t));
2807 free(ext->in6_ifstat, M_IFADDR);
2808 COUNTER_ARRAY_FREE(ext->icmp6_ifstat,
2809 sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
2810 free(ext->icmp6_ifstat, M_IFADDR);
2811 free(ext, M_IFADDR);
2815 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2816 * v4 mapped addr or v4 compat addr
2819 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2822 bzero(sin, sizeof(*sin));
2823 sin->sin_len = sizeof(struct sockaddr_in);
2824 sin->sin_family = AF_INET;
2825 sin->sin_port = sin6->sin6_port;
2826 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2829 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2831 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2833 bzero(sin6, sizeof(*sin6));
2834 sin6->sin6_len = sizeof(struct sockaddr_in6);
2835 sin6->sin6_family = AF_INET6;
2836 sin6->sin6_port = sin->sin_port;
2837 sin6->sin6_addr.s6_addr32[0] = 0;
2838 sin6->sin6_addr.s6_addr32[1] = 0;
2839 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2840 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2843 /* Convert sockaddr_in6 into sockaddr_in. */
2845 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2847 struct sockaddr_in *sin_p;
2848 struct sockaddr_in6 sin6;
2851 * Save original sockaddr_in6 addr and convert it
2854 sin6 = *(struct sockaddr_in6 *)nam;
2855 sin_p = (struct sockaddr_in *)nam;
2856 in6_sin6_2_sin(sin_p, &sin6);
2859 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2861 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2863 struct sockaddr_in *sin_p;
2864 struct sockaddr_in6 *sin6_p;
2866 sin6_p = malloc(sizeof *sin6_p, M_SONAME, M_WAITOK);
2867 sin_p = (struct sockaddr_in *)*nam;
2868 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2869 free(*nam, M_SONAME);
2870 *nam = (struct sockaddr *)sin6_p;