2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)in.c 8.2 (Berkeley) 11/15/93
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
69 #include "opt_inet6.h"
71 #include <sys/param.h>
72 #include <sys/eventhandler.h>
73 #include <sys/errno.h>
75 #include <sys/malloc.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/sockio.h>
79 #include <sys/systm.h>
82 #include <sys/protosw.h>
84 #include <sys/kernel.h>
86 #include <sys/rmlock.h>
87 #include <sys/sysctl.h>
88 #include <sys/syslog.h>
91 #include <net/if_var.h>
92 #include <net/if_types.h>
93 #include <net/route.h>
94 #include <net/route/route_ctl.h>
95 #include <net/route/nhop.h>
96 #include <net/if_dl.h>
99 #include <netinet/in.h>
100 #include <netinet/in_var.h>
101 #include <net/if_llatbl.h>
102 #include <netinet/if_ether.h>
103 #include <netinet/in_systm.h>
104 #include <netinet/ip.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet/ip_carp.h>
108 #include <netinet/ip6.h>
109 #include <netinet6/ip6_var.h>
110 #include <netinet6/nd6.h>
111 #include <netinet6/mld6_var.h>
112 #include <netinet6/ip6_mroute.h>
113 #include <netinet6/in6_ifattach.h>
114 #include <netinet6/scope6_var.h>
115 #include <netinet6/in6_fib.h>
116 #include <netinet6/in6_pcb.h>
119 * struct in6_ifreq and struct ifreq must be type punnable for common members
120 * of ifr_ifru to allow accessors to be shared.
122 _Static_assert(offsetof(struct in6_ifreq, ifr_ifru) ==
123 offsetof(struct ifreq, ifr_ifru),
124 "struct in6_ifreq and struct ifreq are not type punnable");
126 VNET_DECLARE(int, icmp6_nodeinfo_oldmcprefix);
127 #define V_icmp6_nodeinfo_oldmcprefix VNET(icmp6_nodeinfo_oldmcprefix)
130 * Definitions of some costant IP6 addresses.
132 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
133 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
134 const struct in6_addr in6addr_nodelocal_allnodes =
135 IN6ADDR_NODELOCAL_ALLNODES_INIT;
136 const struct in6_addr in6addr_linklocal_allnodes =
137 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
138 const struct in6_addr in6addr_linklocal_allrouters =
139 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
140 const struct in6_addr in6addr_linklocal_allv2routers =
141 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
143 const struct in6_addr in6mask0 = IN6MASK0;
144 const struct in6_addr in6mask32 = IN6MASK32;
145 const struct in6_addr in6mask64 = IN6MASK64;
146 const struct in6_addr in6mask96 = IN6MASK96;
147 const struct in6_addr in6mask128 = IN6MASK128;
149 const struct sockaddr_in6 sa6_any =
150 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
152 static int in6_notify_ifa(struct ifnet *, struct in6_ifaddr *,
153 struct in6_aliasreq *, int);
154 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
156 static int in6_validate_ifra(struct ifnet *, struct in6_aliasreq *,
157 struct in6_ifaddr *, int);
158 static struct in6_ifaddr *in6_alloc_ifa(struct ifnet *,
159 struct in6_aliasreq *, int flags);
160 static int in6_update_ifa_internal(struct ifnet *, struct in6_aliasreq *,
161 struct in6_ifaddr *, int, int);
162 static int in6_broadcast_ifa(struct ifnet *, struct in6_aliasreq *,
163 struct in6_ifaddr *, int);
165 static void in6_join_proxy_ndp_mc(struct ifnet *, const struct in6_addr *);
166 static void in6_leave_proxy_ndp_mc(struct ifnet *, const struct in6_addr *);
168 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
169 #define ia62ifa(ia6) (&((ia6)->ia_ifa))
172 in6_newaddrmsg(struct in6_ifaddr *ia, int cmd)
174 struct rt_addrinfo info;
176 struct sockaddr_dl gateway;
182 * Prepare info data for the host route.
183 * This code mimics one from ifa_maintain_loopback_route().
185 bzero(&info, sizeof(struct rt_addrinfo));
186 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
187 info.rti_info[RTAX_DST] = ifa->ifa_addr;
188 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gateway;
189 link_init_sdl(ifa->ifa_ifp, (struct sockaddr *)&gateway, ifa->ifa_ifp->if_type);
190 if (cmd != RTM_DELETE)
191 info.rti_ifp = V_loif;
193 fibnum = ia62ifa(ia)->ifa_ifp->if_fib;
195 if (cmd == RTM_ADD) {
196 rt_addrmsg(cmd, &ia->ia_ifa, fibnum);
197 rt_routemsg_info(cmd, &info, fibnum);
198 } else if (cmd == RTM_DELETE) {
199 rt_routemsg_info(cmd, &info, fibnum);
200 rt_addrmsg(cmd, &ia->ia_ifa, fibnum);
205 in6_mask2len(struct in6_addr *mask, u_char *lim0)
208 u_char *lim = lim0, *p;
210 /* ignore the scope_id part */
211 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
212 lim = (u_char *)mask + sizeof(*mask);
213 for (p = (u_char *)mask; p < lim; x++, p++) {
219 for (y = 0; y < 8; y++) {
220 if ((*p & (0x80 >> y)) == 0)
226 * when the limit pointer is given, do a stricter check on the
230 if (y != 0 && (*p & (0x00ff >> y)) != 0)
232 for (p = p + 1; p < lim; p++)
240 #ifdef COMPAT_FREEBSD32
241 struct in6_ndifreq32 {
242 char ifname[IFNAMSIZ];
245 #define SIOCGDEFIFACE32_IN6 _IOWR('i', 86, struct in6_ndifreq32)
249 in6_control(struct socket *so, u_long cmd, caddr_t data,
250 struct ifnet *ifp, struct thread *td)
252 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
253 struct in6_ifaddr *ia = NULL;
254 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
255 struct sockaddr_in6 *sa6;
256 int carp_attached = 0;
261 * Compat to make pre-10.x ifconfig(8) operable.
263 if (cmd == OSIOCAIFADDR_IN6)
264 cmd = SIOCAIFADDR_IN6;
267 case SIOCGETSGCNT_IN6:
268 case SIOCGETMIFCNT_IN6:
270 * XXX mrt_ioctl has a 3rd, unused, FIB argument in route.c.
271 * We cannot see how that would be needed, so do not adjust the
272 * KPI blindly; more likely should clean up the IPv4 variant.
274 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
278 case SIOCAADDRCTL_POLICY:
279 case SIOCDADDRCTL_POLICY:
281 error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
285 return (in6_src_ioctl(cmd, data));
292 case SIOCSNDFLUSH_IN6:
293 case SIOCSPFXFLUSH_IN6:
294 case SIOCSRTRFLUSH_IN6:
295 case SIOCSDEFIFACE_IN6:
296 case SIOCSIFINFO_FLAGS:
297 case SIOCSIFINFO_IN6:
299 error = priv_check(td, PRIV_NETINET_ND6);
304 case OSIOCGIFINFO_IN6:
305 case SIOCGIFINFO_IN6:
306 case SIOCGNBRINFO_IN6:
307 case SIOCGDEFIFACE_IN6:
308 return (nd6_ioctl(cmd, data, ifp));
310 #ifdef COMPAT_FREEBSD32
311 case SIOCGDEFIFACE32_IN6:
313 struct in6_ndifreq ndif;
314 struct in6_ndifreq32 *ndif32;
316 error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif,
320 ndif32 = (struct in6_ndifreq32 *)data;
321 ndif32->ifindex = ndif.ifindex;
328 case SIOCSIFPREFIX_IN6:
329 case SIOCDIFPREFIX_IN6:
330 case SIOCAIFPREFIX_IN6:
331 case SIOCCIFPREFIX_IN6:
332 case SIOCSGIFPREFIX_IN6:
333 case SIOCGIFPREFIX_IN6:
335 "prefix ioctls are now invalidated. "
336 "please use ifconfig.\n");
343 error = priv_check(td, PRIV_NETINET_SCOPE6);
350 return (scope6_ioctl(cmd, data, ifp));
354 * Find address for this interface, if it exists.
356 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
357 * only, and used the first interface address as the target of other
358 * operations (without checking ifra_addr). This was because netinet
359 * code/API assumed at most 1 interface address per interface.
360 * Since IPv6 allows a node to assign multiple addresses
361 * on a single interface, we almost always look and check the
362 * presence of ifra_addr, and reject invalid ones here.
363 * It also decreases duplicated code among SIOC*_IN6 operations.
366 case SIOCAIFADDR_IN6:
367 case SIOCSIFPHYADDR_IN6:
368 sa6 = &ifra->ifra_addr;
370 case SIOCSIFADDR_IN6:
371 case SIOCGIFADDR_IN6:
372 case SIOCSIFDSTADDR_IN6:
373 case SIOCSIFNETMASK_IN6:
374 case SIOCGIFDSTADDR_IN6:
375 case SIOCGIFNETMASK_IN6:
376 case SIOCDIFADDR_IN6:
377 case SIOCGIFPSRCADDR_IN6:
378 case SIOCGIFPDSTADDR_IN6:
379 case SIOCGIFAFLAG_IN6:
380 case SIOCSNDFLUSH_IN6:
381 case SIOCSPFXFLUSH_IN6:
382 case SIOCSRTRFLUSH_IN6:
383 case SIOCGIFALIFETIME_IN6:
384 case SIOCGIFSTAT_IN6:
385 case SIOCGIFSTAT_ICMP6:
386 sa6 = &ifr->ifr_addr;
393 * Although we should pass any non-INET6 ioctl requests
394 * down to driver, we filter some legacy INET requests.
395 * Drivers trust SIOCSIFADDR et al to come from an already
396 * privileged layer, and do not perform any credentials
397 * checks or input validation.
404 if (sa6 && sa6->sin6_family == AF_INET6) {
405 if (sa6->sin6_scope_id != 0)
406 error = sa6_embedscope(sa6, 0);
408 error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
411 if (td != NULL && (error = prison_check_ip6(td->td_ucred,
412 &sa6->sin6_addr)) != 0)
414 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
419 case SIOCSIFADDR_IN6:
420 case SIOCSIFDSTADDR_IN6:
421 case SIOCSIFNETMASK_IN6:
423 * Since IPv6 allows a node to assign multiple addresses
424 * on a single interface, SIOCSIFxxx ioctls are deprecated.
426 /* we decided to obsolete this command (20000704) */
430 case SIOCDIFADDR_IN6:
432 * for IPv4, we look for existing in_ifaddr here to allow
433 * "ifconfig if0 delete" to remove the first IPv4 address on
434 * the interface. For IPv6, as the spec allows multiple
435 * interface address from the day one, we consider "remove the
436 * first one" semantics to be not preferable.
439 error = EADDRNOTAVAIL;
443 case SIOCAIFADDR_IN6:
445 * We always require users to specify a valid IPv6 address for
446 * the corresponding operation.
448 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
449 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
450 error = EAFNOSUPPORT;
455 error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
456 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
461 case SIOCGIFSTAT_IN6:
462 case SIOCGIFSTAT_ICMP6:
463 if (ifp->if_afdata[AF_INET6] == NULL) {
464 error = EPFNOSUPPORT;
469 case SIOCGIFADDR_IN6:
470 /* This interface is basically deprecated. use SIOCGIFCONF. */
472 case SIOCGIFAFLAG_IN6:
473 case SIOCGIFNETMASK_IN6:
474 case SIOCGIFDSTADDR_IN6:
475 case SIOCGIFALIFETIME_IN6:
476 /* must think again about its semantics */
478 error = EADDRNOTAVAIL;
485 case SIOCGIFADDR_IN6:
486 ifr->ifr_addr = ia->ia_addr;
487 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
491 case SIOCGIFDSTADDR_IN6:
492 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
496 ifr->ifr_dstaddr = ia->ia_dstaddr;
497 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
501 case SIOCGIFNETMASK_IN6:
502 ifr->ifr_addr = ia->ia_prefixmask;
505 case SIOCGIFAFLAG_IN6:
506 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
509 case SIOCGIFSTAT_IN6:
510 COUNTER_ARRAY_COPY(((struct in6_ifextra *)
511 ifp->if_afdata[AF_INET6])->in6_ifstat,
512 &ifr->ifr_ifru.ifru_stat,
513 sizeof(struct in6_ifstat) / sizeof(uint64_t));
516 case SIOCGIFSTAT_ICMP6:
517 COUNTER_ARRAY_COPY(((struct in6_ifextra *)
518 ifp->if_afdata[AF_INET6])->icmp6_ifstat,
519 &ifr->ifr_ifru.ifru_icmp6stat,
520 sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
523 case SIOCGIFALIFETIME_IN6:
524 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
525 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
527 struct in6_addrlifetime *retlt =
528 &ifr->ifr_ifru.ifru_lifetime;
531 * XXX: adjust expiration time assuming time_t is
535 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
536 if (ia->ia6_lifetime.ia6t_vltime <
537 maxexpire - ia->ia6_updatetime) {
538 retlt->ia6t_expire = ia->ia6_updatetime +
539 ia->ia6_lifetime.ia6t_vltime;
541 retlt->ia6t_expire = maxexpire;
543 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
545 struct in6_addrlifetime *retlt =
546 &ifr->ifr_ifru.ifru_lifetime;
549 * XXX: adjust expiration time assuming time_t is
553 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
554 if (ia->ia6_lifetime.ia6t_pltime <
555 maxexpire - ia->ia6_updatetime) {
556 retlt->ia6t_preferred = ia->ia6_updatetime +
557 ia->ia6_lifetime.ia6t_pltime;
559 retlt->ia6t_preferred = maxexpire;
563 case SIOCAIFADDR_IN6:
565 struct nd_prefixctl pr0;
566 struct nd_prefix *pr;
569 * first, make or update the interface address structure,
570 * and link it to the list.
572 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
575 if (ia->ia_ifa.ifa_carp)
576 (*carp_detach_p)(&ia->ia_ifa, true);
577 ifa_free(&ia->ia_ifa);
579 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
582 * this can happen when the user specify the 0 valid
588 if (cmd == ocmd && ifra->ifra_vhid > 0) {
589 if (carp_attach_p != NULL)
590 error = (*carp_attach_p)(&ia->ia_ifa,
593 error = EPROTONOSUPPORT;
601 * then, make the prefix on-link on the interface.
602 * XXX: we'd rather create the prefix before the address, but
603 * we need at least one address to install the corresponding
604 * interface route, so we configure the address first.
608 * convert mask to prefix length (prefixmask has already
609 * been validated in in6_update_ifa().
611 bzero(&pr0, sizeof(pr0));
613 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
615 if (pr0.ndpr_plen == 128) {
616 /* we don't need to install a host route. */
619 pr0.ndpr_prefix = ifra->ifra_addr;
620 /* apply the mask for safety. */
621 IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr,
622 &ifra->ifra_prefixmask.sin6_addr);
625 * XXX: since we don't have an API to set prefix (not address)
626 * lifetimes, we just use the same lifetimes as addresses.
627 * The (temporarily) installed lifetimes can be overridden by
628 * later advertised RAs (when accept_rtadv is non 0), which is
629 * an intended behavior.
631 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
633 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
634 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
635 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
637 /* add the prefix if not yet. */
638 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
640 * nd6_prelist_add will install the corresponding
643 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) {
645 (*carp_detach_p)(&ia->ia_ifa, false);
650 /* relate the address to the prefix */
651 if (ia->ia6_ndpr == NULL) {
656 * If this is the first autoconf address from the
657 * prefix, create a temporary address as well
660 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
661 V_ip6_use_tempaddr && pr->ndpr_addrcnt == 1) {
663 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
664 log(LOG_NOTICE, "in6_control: failed "
665 "to create a temporary address, "
673 * this might affect the status of autoconfigured addresses,
674 * that is, this address might make other addresses detached.
676 pfxlist_onlink_check();
680 * Try to clear the flag when a new IPv6 address is added
681 * onto an IFDISABLED interface and it succeeds.
683 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
684 struct in6_ndireq nd;
686 memset(&nd, 0, sizeof(nd));
687 nd.ndi.flags = ND_IFINFO(ifp)->flags;
688 nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
689 if (nd6_ioctl(SIOCSIFINFO_FLAGS, (caddr_t)&nd, ifp) < 0)
690 log(LOG_NOTICE, "SIOCAIFADDR_IN6: "
691 "SIOCSIFINFO_FLAGS for -ifdisabled "
694 * Ignore failure of clearing the flag intentionally.
695 * The failure means address duplication was detected.
701 case SIOCDIFADDR_IN6:
703 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
708 if (ifp->if_ioctl == NULL) {
712 error = (*ifp->if_ioctl)(ifp, cmd, data);
719 ifa_free(&ia->ia_ifa);
723 static struct in6_multi_mship *
724 in6_joingroup_legacy(struct ifnet *ifp, const struct in6_addr *mcaddr,
725 int *errorp, int delay)
727 struct in6_multi_mship *imm;
730 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT);
736 delay = (delay * PR_FASTHZ) / hz;
738 error = in6_joingroup(ifp, mcaddr, NULL, &imm->i6mm_maddr, delay);
741 free(imm, M_IP6MADDR);
749 in6_solicited_node_maddr(struct in6_addr *maddr,
750 struct ifnet *ifp, const struct in6_addr *base)
754 bzero(maddr, sizeof(struct in6_addr));
755 maddr->s6_addr32[0] = IPV6_ADDR_INT32_MLL;
756 maddr->s6_addr32[2] = htonl(1);
757 maddr->s6_addr32[3] = base->s6_addr32[3];
758 maddr->s6_addr8[12] = 0xff;
759 if ((error = in6_setscope(maddr, ifp, NULL)) != 0) {
760 /* XXX: should not happen */
761 log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
768 * Join necessary multicast groups. Factored out from in6_update_ifa().
769 * This entire work should only be done once, for the default FIB.
772 in6_update_ifa_join_mc(struct ifnet *ifp, struct in6_aliasreq *ifra,
773 struct in6_ifaddr *ia, int flags, struct in6_multi **in6m_sol)
775 char ip6buf[INET6_ADDRSTRLEN];
776 struct in6_addr mltaddr;
777 struct in6_multi_mship *imm;
780 KASSERT(in6m_sol != NULL, ("%s: in6m_sol is NULL", __func__));
782 /* Join solicited multicast addr for new host id. */
783 if ((error = in6_solicited_node_maddr(&mltaddr, ifp,
784 &ifra->ifra_addr.sin6_addr)) != 0)
787 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
789 * We need a random delay for DAD on the address being
790 * configured. It also means delaying transmission of the
791 * corresponding MLD report to avoid report collision.
792 * [RFC 4861, Section 6.3.7]
794 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
796 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, delay);
798 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
799 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &mltaddr),
800 if_name(ifp), error));
803 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
804 *in6m_sol = imm->i6mm_maddr;
807 * Join link-local all-nodes address.
809 mltaddr = in6addr_linklocal_allnodes;
810 if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0)
811 goto cleanup; /* XXX: should not fail */
813 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, 0);
815 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
816 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &mltaddr),
817 if_name(ifp), error));
820 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
823 * Join node information group address.
826 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
828 * The spec does not say anything about delay for this group,
829 * but the same logic should apply.
831 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
833 if (in6_nigroup(ifp, NULL, -1, &mltaddr) == 0) {
835 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, delay);
838 "%s: in6_joingroup failed for %s on %s "
839 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
840 &mltaddr), if_name(ifp), error));
841 /* XXX not very fatal, go on... */
843 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
845 if (V_icmp6_nodeinfo_oldmcprefix &&
846 in6_nigroup_oldmcprefix(ifp, NULL, -1, &mltaddr) == 0) {
847 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, delay);
850 "%s: in6_joingroup failed for %s on %s "
851 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
852 &mltaddr), if_name(ifp), error));
853 /* XXX not very fatal, go on... */
855 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
859 * Join interface-local all-nodes address.
860 * (ff01::1%ifN, and ff01::%ifN/32)
862 mltaddr = in6addr_nodelocal_allnodes;
863 if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0)
864 goto cleanup; /* XXX: should not fail */
866 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, 0);
868 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
869 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
870 &mltaddr), if_name(ifp), error));
873 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
880 * Update parameters of an IPv6 interface address.
881 * If necessary, a new entry is created and linked into address chains.
882 * This function is separated from in6_control().
885 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
886 struct in6_ifaddr *ia, int flags)
888 int error, hostIsNew = 0;
890 if ((error = in6_validate_ifra(ifp, ifra, ia, flags)) != 0)
895 if ((ia = in6_alloc_ifa(ifp, ifra, flags)) == NULL)
899 error = in6_update_ifa_internal(ifp, ifra, ia, hostIsNew, flags);
901 if (hostIsNew != 0) {
902 in6_unlink_ifa(ia, ifp);
903 ifa_free(&ia->ia_ifa);
909 error = in6_broadcast_ifa(ifp, ifra, ia, flags);
915 * Fill in basic IPv6 address request info.
918 in6_prepare_ifra(struct in6_aliasreq *ifra, const struct in6_addr *addr,
919 const struct in6_addr *mask)
922 memset(ifra, 0, sizeof(struct in6_aliasreq));
924 ifra->ifra_addr.sin6_family = AF_INET6;
925 ifra->ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
927 ifra->ifra_addr.sin6_addr = *addr;
929 ifra->ifra_prefixmask.sin6_family = AF_INET6;
930 ifra->ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
932 ifra->ifra_prefixmask.sin6_addr = *mask;
936 in6_validate_ifra(struct ifnet *ifp, struct in6_aliasreq *ifra,
937 struct in6_ifaddr *ia, int flags)
940 struct sockaddr_in6 dst6;
941 struct in6_addrlifetime *lt;
942 char ip6buf[INET6_ADDRSTRLEN];
944 /* Validate parameters */
945 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
949 * The destination address for a p2p link must have a family
950 * of AF_UNSPEC or AF_INET6.
952 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
953 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
954 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
955 return (EAFNOSUPPORT);
960 if (ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6) ||
961 ifra->ifra_addr.sin6_family != AF_INET6)
965 * validate ifra_prefixmask. don't check sin6_family, netmask
966 * does not carry fields other than sin6_len.
968 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
971 * Because the IPv6 address architecture is classless, we require
972 * users to specify a (non 0) prefix length (mask) for a new address.
973 * We also require the prefix (when specified) mask is valid, and thus
974 * reject a non-consecutive mask.
976 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
978 if (ifra->ifra_prefixmask.sin6_len != 0) {
979 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
980 (u_char *)&ifra->ifra_prefixmask +
981 ifra->ifra_prefixmask.sin6_len);
986 * In this case, ia must not be NULL. We just use its prefix
989 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
992 * If the destination address on a p2p interface is specified,
993 * and the address is a scoped one, validate/set the scope
996 dst6 = ifra->ifra_dstaddr;
997 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
998 (dst6.sin6_family == AF_INET6)) {
999 struct in6_addr in6_tmp;
1002 in6_tmp = dst6.sin6_addr;
1003 if (in6_setscope(&in6_tmp, ifp, &zoneid))
1004 return (EINVAL); /* XXX: should be impossible */
1006 if (dst6.sin6_scope_id != 0) {
1007 if (dst6.sin6_scope_id != zoneid)
1009 } else /* user omit to specify the ID. */
1010 dst6.sin6_scope_id = zoneid;
1012 /* convert into the internal form */
1013 if (sa6_embedscope(&dst6, 0))
1014 return (EINVAL); /* XXX: should be impossible */
1016 /* Modify original ifra_dstaddr to reflect changes */
1017 ifra->ifra_dstaddr = dst6;
1020 * The destination address can be specified only for a p2p or a
1021 * loopback interface. If specified, the corresponding prefix length
1024 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
1025 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
1026 /* XXX: noisy message */
1027 nd6log((LOG_INFO, "in6_update_ifa: a destination can "
1028 "be specified for a p2p or a loopback IF only\n"));
1032 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
1033 "be 128 when dstaddr is specified\n"));
1037 /* lifetime consistency check */
1038 lt = &ifra->ifra_lifetime;
1039 if (lt->ia6t_pltime > lt->ia6t_vltime)
1041 if (lt->ia6t_vltime == 0) {
1043 * the following log might be noisy, but this is a typical
1044 * configuration mistake or a tool's bug.
1047 "in6_update_ifa: valid lifetime is 0 for %s\n",
1048 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
1051 return (0); /* there's nothing to do */
1054 /* Check prefix mask */
1055 if (ia != NULL && ifra->ifra_prefixmask.sin6_len != 0) {
1057 * We prohibit changing the prefix length of an existing
1059 * + such an operation should be rare in IPv6, and
1060 * + the operation would confuse prefix management.
1062 if (ia->ia_prefixmask.sin6_len != 0 &&
1063 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1064 nd6log((LOG_INFO, "in6_validate_ifa: the prefix length "
1065 "of an existing %s address should not be changed\n",
1066 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1076 * Allocate a new ifaddr and link it into chains.
1078 static struct in6_ifaddr *
1079 in6_alloc_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, int flags)
1081 struct in6_ifaddr *ia;
1084 * When in6_alloc_ifa() is called in a process of a received
1085 * RA, it is called under an interrupt context. So, we should
1086 * call malloc with M_NOWAIT.
1088 ia = (struct in6_ifaddr *)ifa_alloc(sizeof(*ia), M_NOWAIT);
1091 LIST_INIT(&ia->ia6_memberships);
1092 /* Initialize the address and masks, and put time stamp */
1093 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1094 ia->ia_addr.sin6_family = AF_INET6;
1095 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
1096 /* XXX: Can we assign ,sin6_addr and skip the rest? */
1097 ia->ia_addr = ifra->ifra_addr;
1098 ia->ia6_createtime = time_uptime;
1099 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
1101 * Some functions expect that ifa_dstaddr is not
1102 * NULL for p2p interfaces.
1104 ia->ia_ifa.ifa_dstaddr =
1105 (struct sockaddr *)&ia->ia_dstaddr;
1107 ia->ia_ifa.ifa_dstaddr = NULL;
1110 /* set prefix mask if any */
1111 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
1112 if (ifra->ifra_prefixmask.sin6_len != 0) {
1113 ia->ia_prefixmask.sin6_family = AF_INET6;
1114 ia->ia_prefixmask.sin6_len = ifra->ifra_prefixmask.sin6_len;
1115 ia->ia_prefixmask.sin6_addr = ifra->ifra_prefixmask.sin6_addr;
1119 ifa_ref(&ia->ia_ifa); /* if_addrhead */
1121 CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1122 IF_ADDR_WUNLOCK(ifp);
1124 ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */
1126 CK_STAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
1127 CK_LIST_INSERT_HEAD(IN6ADDR_HASH(&ia->ia_addr.sin6_addr), ia, ia6_hash);
1128 IN6_IFADDR_WUNLOCK();
1134 * Update/configure interface address parameters:
1136 * 1) Update lifetime
1137 * 2) Update interface metric ad flags
1138 * 3) Notify other subsystems
1141 in6_update_ifa_internal(struct ifnet *ifp, struct in6_aliasreq *ifra,
1142 struct in6_ifaddr *ia, int hostIsNew, int flags)
1146 /* update timestamp */
1147 ia->ia6_updatetime = time_uptime;
1150 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred
1151 * to see if the address is deprecated or invalidated, but initialize
1152 * these members for applications.
1154 ia->ia6_lifetime = ifra->ifra_lifetime;
1155 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1156 ia->ia6_lifetime.ia6t_expire =
1157 time_uptime + ia->ia6_lifetime.ia6t_vltime;
1159 ia->ia6_lifetime.ia6t_expire = 0;
1160 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1161 ia->ia6_lifetime.ia6t_preferred =
1162 time_uptime + ia->ia6_lifetime.ia6t_pltime;
1164 ia->ia6_lifetime.ia6t_preferred = 0;
1167 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1168 * userland, make it deprecated.
1170 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1171 ia->ia6_lifetime.ia6t_pltime = 0;
1172 ia->ia6_lifetime.ia6t_preferred = time_uptime;
1176 * configure address flags.
1178 ia->ia6_flags = ifra->ifra_flags;
1181 * Make the address tentative before joining multicast addresses,
1182 * so that corresponding MLD responses would not have a tentative
1185 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */
1188 * DAD should be performed for an new address or addresses on
1189 * an interface with ND6_IFF_IFDISABLED.
1191 if (in6if_do_dad(ifp) &&
1192 (hostIsNew || (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)))
1193 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1195 /* notify other subsystems */
1196 error = in6_notify_ifa(ifp, ia, ifra, hostIsNew);
1202 * Do link-level ifa job:
1203 * 1) Add lle entry for added address
1204 * 2) Notifies routing socket users about new address
1205 * 3) join appropriate multicast group
1206 * 4) start DAD if enabled
1209 in6_broadcast_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
1210 struct in6_ifaddr *ia, int flags)
1212 struct in6_multi *in6m_sol;
1215 /* Add local address to lltable, if necessary (ex. on p2p link). */
1216 if ((error = nd6_add_ifa_lle(ia)) != 0) {
1217 in6_purgeaddr(&ia->ia_ifa);
1218 ifa_free(&ia->ia_ifa);
1222 /* Join necessary multicast groups. */
1224 if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1225 error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol);
1227 in6_purgeaddr(&ia->ia_ifa);
1228 ifa_free(&ia->ia_ifa);
1233 /* Perform DAD, if the address is TENTATIVE. */
1234 if ((ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1235 int delay, mindelay, maxdelay;
1238 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1240 * We need to impose a delay before sending an NS
1241 * for DAD. Check if we also needed a delay for the
1242 * corresponding MLD message. If we did, the delay
1243 * should be larger than the MLD delay (this could be
1244 * relaxed a bit, but this simple logic is at least
1246 * XXX: Break data hiding guidelines and look at
1247 * state for the solicited multicast group.
1250 if (in6m_sol != NULL &&
1251 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1252 mindelay = in6m_sol->in6m_timer;
1254 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1255 if (maxdelay - mindelay == 0)
1259 (arc4random() % (maxdelay - mindelay)) +
1263 nd6_dad_start((struct ifaddr *)ia, delay);
1266 in6_newaddrmsg(ia, RTM_ADD);
1267 ifa_free(&ia->ia_ifa);
1272 * Adds or deletes interface route for p2p ifa.
1273 * Returns 0 on success or errno.
1276 in6_handle_dstaddr_rtrequest(int cmd, struct in6_ifaddr *ia)
1278 struct epoch_tracker et;
1279 struct ifaddr *ifa = &ia->ia_ifa;
1282 /* Prepare gateway */
1283 struct sockaddr_dl_short sdl = {
1284 .sdl_family = AF_LINK,
1285 .sdl_len = sizeof(struct sockaddr_dl_short),
1286 .sdl_type = ifa->ifa_ifp->if_type,
1287 .sdl_index = ifa->ifa_ifp->if_index,
1290 struct sockaddr_in6 dst = {
1291 .sin6_family = AF_INET6,
1292 .sin6_len = sizeof(struct sockaddr_in6),
1293 .sin6_addr = ia->ia_dstaddr.sin6_addr,
1296 struct rt_addrinfo info = {
1298 .rti_ifp = ifa->ifa_ifp,
1299 .rti_flags = RTF_PINNED | RTF_HOST,
1301 [RTAX_DST] = (struct sockaddr *)&dst,
1302 [RTAX_GATEWAY] = (struct sockaddr *)&sdl,
1305 /* Don't set additional per-gw filters on removal */
1307 NET_EPOCH_ENTER(et);
1308 error = rib_handle_ifaddr_info(ifa->ifa_ifp->if_fib, cmd, &info);
1315 ifa_is_p2p(struct in6_ifaddr *ia)
1319 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1321 if ((plen == 128) && (ia->ia_dstaddr.sin6_family == AF_INET6) &&
1322 !IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &ia->ia_dstaddr.sin6_addr))
1329 in6_purgeaddr(struct ifaddr *ifa)
1331 struct ifnet *ifp = ifa->ifa_ifp;
1332 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1333 struct in6_multi_mship *imm;
1337 (*carp_detach_p)(ifa, false);
1340 * Remove the loopback route to the interface address.
1341 * The check for the current setting of "nd6_useloopback"
1344 if (ia->ia_flags & IFA_RTSELF) {
1345 error = ifa_del_loopback_route((struct ifaddr *)ia,
1346 (struct sockaddr *)&ia->ia_addr);
1348 ia->ia_flags &= ~IFA_RTSELF;
1351 /* stop DAD processing */
1354 /* Leave multicast groups. */
1355 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1356 LIST_REMOVE(imm, i6mm_chain);
1357 if (imm->i6mm_maddr != NULL)
1358 in6_leavegroup(imm->i6mm_maddr, NULL);
1359 free(imm, M_IP6MADDR);
1361 /* Check if we need to remove p2p route */
1362 if ((ia->ia_flags & IFA_ROUTE) && ifa_is_p2p(ia)) {
1363 error = in6_handle_dstaddr_rtrequest(RTM_DELETE, ia);
1365 log(LOG_INFO, "%s: err=%d, destination address delete "
1366 "failed\n", __func__, error);
1367 ia->ia_flags &= ~IFA_ROUTE;
1370 in6_newaddrmsg(ia, RTM_DELETE);
1371 in6_unlink_ifa(ia, ifp);
1375 * Removes @ia from the corresponding interfaces and unlinks corresponding
1376 * prefix if no addresses are using it anymore.
1379 in6_purgeifaddr(struct in6_ifaddr *ia)
1381 struct nd_prefix *pr;
1384 * If the address being deleted is the only one that owns
1385 * the corresponding prefix, expire the prefix as well.
1386 * XXX: theoretically, we don't have to worry about such
1387 * relationship, since we separate the address management
1388 * and the prefix management. We do this, however, to provide
1389 * as much backward compatibility as possible in terms of
1390 * the ioctl operation.
1391 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
1394 in6_purgeaddr(&ia->ia_ifa);
1395 if (pr != NULL && pr->ndpr_addrcnt == 0) {
1397 nd6_prefix_unlink(pr, NULL);
1405 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1407 char ip6buf[INET6_ADDRSTRLEN];
1411 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
1412 IF_ADDR_WUNLOCK(ifp);
1413 ifa_free(&ia->ia_ifa); /* if_addrhead */
1416 * Defer the release of what might be the last reference to the
1417 * in6_ifaddr so that it can't be freed before the remainder of the
1421 CK_STAILQ_REMOVE(&V_in6_ifaddrhead, ia, in6_ifaddr, ia_link);
1422 CK_LIST_REMOVE(ia, ia6_hash);
1423 IN6_IFADDR_WUNLOCK();
1426 * Release the reference to the base prefix. There should be a
1427 * positive reference.
1430 if (ia->ia6_ndpr == NULL) {
1432 "in6_unlink_ifa: autoconf'ed address "
1433 "%s has no prefix\n", ip6_sprintf(ip6buf, IA6_IN6(ia))));
1435 ia->ia6_ndpr->ndpr_addrcnt--;
1436 /* Do not delete lles within prefix if refcont != 0 */
1437 if (ia->ia6_ndpr->ndpr_addrcnt == 0)
1439 ia->ia6_ndpr = NULL;
1442 nd6_rem_ifa_lle(ia, remove_lle);
1445 * Also, if the address being removed is autoconf'ed, call
1446 * pfxlist_onlink_check() since the release might affect the status of
1447 * other (detached) addresses.
1449 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1450 pfxlist_onlink_check();
1452 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */
1456 * Notifies other subsystems about address change/arrival:
1457 * 1) Notifies device handler on the first IPv6 address assignment
1458 * 2) Handle routing table changes for P2P links and route
1459 * 3) Handle routing table changes for address host route
1462 in6_notify_ifa(struct ifnet *ifp, struct in6_ifaddr *ia,
1463 struct in6_aliasreq *ifra, int hostIsNew)
1465 int error = 0, ifacount = 0;
1467 struct sockaddr_in6 *pdst;
1468 char ip6buf[INET6_ADDRSTRLEN];
1471 * Give the interface a chance to initialize
1472 * if this is its first address,
1474 if (hostIsNew != 0) {
1475 struct epoch_tracker et;
1477 NET_EPOCH_ENTER(et);
1478 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1479 if (ifa->ifa_addr->sa_family != AF_INET6)
1486 if (ifacount <= 1 && ifp->if_ioctl) {
1487 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1493 * If a new destination address is specified, scrub the old one and
1494 * install the new destination. Note that the interface must be
1497 pdst = &ifra->ifra_dstaddr;
1498 if (pdst->sin6_family == AF_INET6 &&
1499 !IN6_ARE_ADDR_EQUAL(&pdst->sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
1500 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1501 (in6_handle_dstaddr_rtrequest(RTM_DELETE, ia) != 0)) {
1502 nd6log((LOG_ERR, "in6_update_ifa_internal: failed to "
1503 "remove a route to the old destination: %s\n",
1504 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1505 /* proceed anyway... */
1507 ia->ia_flags &= ~IFA_ROUTE;
1508 ia->ia_dstaddr = *pdst;
1512 * If a new destination address is specified for a point-to-point
1513 * interface, install a route to the destination as an interface
1515 * XXX: the logic below rejects assigning multiple addresses on a p2p
1516 * interface that share the same destination.
1518 if (!(ia->ia_flags & IFA_ROUTE) && ifa_is_p2p(ia)) {
1519 error = in6_handle_dstaddr_rtrequest(RTM_ADD, ia);
1522 ia->ia_flags |= IFA_ROUTE;
1526 * add a loopback route to self if not exists
1528 if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
1529 error = ifa_add_loopback_route((struct ifaddr *)ia,
1530 (struct sockaddr *)&ia->ia_addr);
1532 ia->ia_flags |= IFA_RTSELF;
1535 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1536 "Invoking IPv6 network device address event may sleep");
1538 ifa_ref(&ia->ia_ifa);
1539 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
1541 ifa_free(&ia->ia_ifa);
1547 * Find an IPv6 interface link-local address specific to an interface.
1548 * ifaddr is returned referenced.
1551 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1557 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1558 if (ifa->ifa_addr->sa_family != AF_INET6)
1560 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1561 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1569 return ((struct in6_ifaddr *)ifa);
1573 * find the interface address corresponding to a given IPv6 address.
1574 * ifaddr is returned referenced if @referenced flag is set.
1577 in6ifa_ifwithaddr(const struct in6_addr *addr, uint32_t zoneid, bool referenced)
1579 struct rm_priotracker in6_ifa_tracker;
1580 struct in6_ifaddr *ia;
1582 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1583 CK_LIST_FOREACH(ia, IN6ADDR_HASH(addr), ia6_hash) {
1584 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), addr)) {
1586 zoneid != ia->ia_addr.sin6_scope_id)
1589 ifa_ref(&ia->ia_ifa);
1593 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1598 * find the internet address corresponding to a given interface and address.
1599 * ifaddr is returned referenced.
1602 in6ifa_ifpwithaddr(struct ifnet *ifp, const struct in6_addr *addr)
1604 struct epoch_tracker et;
1607 NET_EPOCH_ENTER(et);
1608 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1609 if (ifa->ifa_addr->sa_family != AF_INET6)
1611 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1618 return ((struct in6_ifaddr *)ifa);
1622 * Find a link-local scoped address on ifp and return it if any.
1625 in6ifa_llaonifp(struct ifnet *ifp)
1627 struct epoch_tracker et;
1628 struct sockaddr_in6 *sin6;
1631 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1633 NET_EPOCH_ENTER(et);
1634 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1635 if (ifa->ifa_addr->sa_family != AF_INET6)
1637 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1638 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
1639 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
1640 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
1645 return ((struct in6_ifaddr *)ifa);
1649 * Convert IP6 address to printable (loggable) representation. Caller
1650 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1652 static char digits[] = "0123456789abcdef";
1654 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1656 int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
1658 const u_int16_t *a = (const u_int16_t *)addr;
1660 int dcolon = 0, zero = 0;
1664 for (i = 0; i < 8; i++) {
1665 if (*(a + i) == 0) {
1670 else if (maxcnt < cnt) {
1681 for (i = 0; i < 8; i++) {
1692 if (dcolon == 0 && *(a + 1) == 0 && i == index) {
1704 d = (const u_char *)a;
1705 /* Try to eliminate leading zeros in printout like in :0001. */
1707 *cp = digits[*d >> 4];
1712 *cp = digits[*d++ & 0xf];
1713 if (zero == 0 || (*cp != '0')) {
1717 *cp = digits[*d >> 4];
1718 if (zero == 0 || (*cp != '0')) {
1722 *cp++ = digits[*d & 0xf];
1731 in6_localaddr(struct in6_addr *in6)
1733 struct rm_priotracker in6_ifa_tracker;
1734 struct in6_ifaddr *ia;
1736 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1739 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1740 CK_STAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1741 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1742 &ia->ia_prefixmask.sin6_addr)) {
1743 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1747 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1753 * Return 1 if an internet address is for the local host and configured
1754 * on one of its interfaces.
1757 in6_localip(struct in6_addr *in6)
1759 struct rm_priotracker in6_ifa_tracker;
1760 struct in6_ifaddr *ia;
1762 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1763 CK_LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) {
1764 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) {
1765 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1769 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1774 * Like in6_localip(), but FIB-aware.
1777 in6_localip_fib(struct in6_addr *in6, uint16_t fib)
1779 struct rm_priotracker in6_ifa_tracker;
1780 struct in6_ifaddr *ia;
1782 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1783 CK_LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) {
1784 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr) &&
1785 ia->ia_ifa.ifa_ifp->if_fib == fib) {
1786 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1790 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1795 * Return 1 if an internet address is configured on an interface.
1798 in6_ifhasaddr(struct ifnet *ifp, struct in6_addr *addr)
1800 struct in6_addr in6;
1802 struct in6_ifaddr *ia6;
1807 if (in6_clearscope(&in6))
1809 in6_setscope(&in6, ifp, NULL);
1811 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1812 if (ifa->ifa_addr->sa_family != AF_INET6)
1814 ia6 = (struct in6_ifaddr *)ifa;
1815 if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &in6))
1823 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1825 struct rm_priotracker in6_ifa_tracker;
1826 struct in6_ifaddr *ia;
1828 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1829 CK_LIST_FOREACH(ia, IN6ADDR_HASH(&sa6->sin6_addr), ia6_hash) {
1830 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), &sa6->sin6_addr)) {
1831 if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
1832 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1833 return (1); /* true */
1838 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1840 return (0); /* false */
1844 * return length of part which dst and src are equal
1848 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1851 u_char *s = (u_char *)src, *d = (u_char *)dst;
1852 u_char *lim = s + 16, r;
1855 if ((r = (*d++ ^ *s++)) != 0) {
1866 /* XXX: to be scope conscious */
1868 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1870 int bytelen, bitlen;
1873 if (0 > len || len > 128) {
1874 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1882 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1885 p1->s6_addr[bytelen] >> (8 - bitlen) !=
1886 p2->s6_addr[bytelen] >> (8 - bitlen))
1893 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1895 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1896 int bytelen, bitlen, i;
1899 if (0 > len || len > 128) {
1900 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1905 bzero(maskp, sizeof(*maskp));
1908 for (i = 0; i < bytelen; i++)
1909 maskp->s6_addr[i] = 0xff;
1911 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1915 * return the best address out of the same scope. if no address was
1916 * found, return the first valid address from designated IF.
1919 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
1921 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1923 struct in6_ifaddr *besta = NULL;
1924 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
1928 dep[0] = dep[1] = NULL;
1931 * We first look for addresses in the same scope.
1932 * If there is one, return it.
1933 * If two or more, return one which matches the dst longest.
1934 * If none, return one of global addresses assigned other ifs.
1936 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1937 if (ifa->ifa_addr->sa_family != AF_INET6)
1939 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1940 continue; /* XXX: is there any case to allow anycast? */
1941 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1942 continue; /* don't use this interface */
1943 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1945 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1946 if (V_ip6_use_deprecated)
1947 dep[0] = (struct in6_ifaddr *)ifa;
1951 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1953 * call in6_matchlen() as few as possible
1957 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1958 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1961 besta = (struct in6_ifaddr *)ifa;
1964 besta = (struct in6_ifaddr *)ifa;
1970 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1971 if (ifa->ifa_addr->sa_family != AF_INET6)
1973 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1974 continue; /* XXX: is there any case to allow anycast? */
1975 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1976 continue; /* don't use this interface */
1977 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1979 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1980 if (V_ip6_use_deprecated)
1981 dep[1] = (struct in6_ifaddr *)ifa;
1985 return (struct in6_ifaddr *)ifa;
1988 /* use the last-resort values, that are, deprecated addresses */
1998 * perform DAD when interface becomes IFF_UP.
2001 in6_if_up(struct ifnet *ifp)
2003 struct epoch_tracker et;
2005 struct in6_ifaddr *ia;
2007 NET_EPOCH_ENTER(et);
2008 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2009 if (ifa->ifa_addr->sa_family != AF_INET6)
2011 ia = (struct in6_ifaddr *)ifa;
2012 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2014 * The TENTATIVE flag was likely set by hand
2015 * beforehand, implicitly indicating the need for DAD.
2016 * We may be able to skip the random delay in this
2017 * case, but we impose delays just in case.
2020 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2026 * special cases, like 6to4, are handled in in6_ifattach
2028 in6_ifattach(ifp, NULL);
2032 in6if_do_dad(struct ifnet *ifp)
2035 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2037 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2039 if ((ND_IFINFO(ifp)->flags &
2040 (ND6_IFF_IFDISABLED | ND6_IFF_NO_DAD)) != 0)
2046 * Calculate max IPv6 MTU through all the interfaces and store it
2052 struct epoch_tracker et;
2053 unsigned long maxmtu = 0;
2056 NET_EPOCH_ENTER(et);
2057 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2058 /* this function can be called during ifnet initialization */
2059 if (!ifp->if_afdata[AF_INET6])
2061 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2062 IN6_LINKMTU(ifp) > maxmtu)
2063 maxmtu = IN6_LINKMTU(ifp);
2066 if (maxmtu) /* update only when maxmtu is positive */
2067 V_in6_maxmtu = maxmtu;
2071 * Provide the length of interface identifiers to be used for the link attached
2072 * to the given interface. The length should be defined in "IPv6 over
2073 * xxx-link" document. Note that address architecture might also define
2074 * the length for a particular set of address prefixes, regardless of the
2075 * link type. As clarified in rfc2462bis, those two definitions should be
2076 * consistent, and those really are as of August 2004.
2079 in6_if2idlen(struct ifnet *ifp)
2081 switch (ifp->if_type) {
2082 case IFT_ETHER: /* RFC2464 */
2083 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2084 case IFT_L2VLAN: /* ditto */
2085 case IFT_BRIDGE: /* bridge(4) only does Ethernet-like links */
2086 case IFT_INFINIBAND:
2088 case IFT_PPP: /* RFC2472 */
2090 case IFT_FRELAY: /* RFC2590 */
2092 case IFT_IEEE1394: /* RFC3146 */
2095 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2097 return (64); /* XXX: is this really correct? */
2100 * Unknown link type:
2101 * It might be controversial to use the today's common constant
2102 * of 64 for these cases unconditionally. For full compliance,
2103 * we should return an error in this case. On the other hand,
2104 * if we simply miss the standard for the link type or a new
2105 * standard is defined for a new link type, the IFID length
2106 * is very likely to be the common constant. As a compromise,
2107 * we always use the constant, but make an explicit notice
2108 * indicating the "unknown" case.
2110 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2115 struct in6_llentry {
2116 struct llentry base;
2119 #define IN6_LLTBL_DEFAULT_HSIZE 32
2120 #define IN6_LLTBL_HASH(k, h) \
2121 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
2124 * Do actual deallocation of @lle.
2127 in6_lltable_destroy_lle_unlocked(epoch_context_t ctx)
2129 struct llentry *lle;
2131 lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
2132 LLE_LOCK_DESTROY(lle);
2133 LLE_REQ_DESTROY(lle);
2134 free(lle, M_LLTABLE);
2138 * Called by LLE_FREE_LOCKED when number of references
2142 in6_lltable_destroy_lle(struct llentry *lle)
2146 NET_EPOCH_CALL(in6_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
2149 static struct llentry *
2150 in6_lltable_new(const struct in6_addr *addr6, u_int flags)
2152 struct in6_llentry *lle;
2154 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
2155 if (lle == NULL) /* NB: caller generates msg */
2158 lle->base.r_l3addr.addr6 = *addr6;
2159 lle->base.lle_refcnt = 1;
2160 lle->base.lle_free = in6_lltable_destroy_lle;
2161 LLE_LOCK_INIT(&lle->base);
2162 LLE_REQ_INIT(&lle->base);
2163 callout_init(&lle->base.lle_timer, 1);
2165 return (&lle->base);
2169 in6_lltable_match_prefix(const struct sockaddr *saddr,
2170 const struct sockaddr *smask, u_int flags, struct llentry *lle)
2172 const struct in6_addr *addr, *mask, *lle_addr;
2174 addr = &((const struct sockaddr_in6 *)saddr)->sin6_addr;
2175 mask = &((const struct sockaddr_in6 *)smask)->sin6_addr;
2176 lle_addr = &lle->r_l3addr.addr6;
2178 if (IN6_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
2181 if (lle->la_flags & LLE_IFADDR) {
2183 * Delete LLE_IFADDR records IFF address & flag matches.
2184 * Note that addr is the interface address within prefix
2187 if (IN6_ARE_ADDR_EQUAL(addr, lle_addr) &&
2188 (flags & LLE_STATIC) != 0)
2193 /* flags & LLE_STATIC means deleting both dynamic and static entries */
2194 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
2201 in6_lltable_free_entry(struct lltable *llt, struct llentry *lle)
2203 struct ifnet *ifp __diagused;
2205 LLE_WLOCK_ASSERT(lle);
2206 KASSERT(llt != NULL, ("lltable is NULL"));
2208 /* Unlink entry from table */
2209 if ((lle->la_flags & LLE_LINKED) != 0) {
2211 IF_AFDATA_WLOCK_ASSERT(ifp);
2212 lltable_unlink_entry(llt, lle);
2219 in6_lltable_rtcheck(struct ifnet *ifp,
2221 const struct sockaddr *l3addr)
2223 const struct sockaddr_in6 *sin6;
2224 struct nhop_object *nh;
2225 struct in6_addr dst;
2227 char ip6buf[INET6_ADDRSTRLEN];
2231 KASSERT(l3addr->sa_family == AF_INET6,
2232 ("sin_family %d", l3addr->sa_family));
2234 sin6 = (const struct sockaddr_in6 *)l3addr;
2235 in6_splitscope(&sin6->sin6_addr, &dst, &scopeid);
2236 fibnum = V_rt_add_addr_allfibs ? RT_DEFAULT_FIB : ifp->if_fib;
2237 nh = fib6_lookup(fibnum, &dst, scopeid, NHR_NONE, 0);
2238 if (nh && ((nh->nh_flags & NHF_GATEWAY) || nh->nh_ifp != ifp)) {
2241 * Create an ND6 cache for an IPv6 neighbor
2242 * that is not covered by our own prefix.
2244 ifa = ifaof_ifpforaddr(l3addr, ifp);
2248 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2249 ip6_sprintf(ip6buf, &sin6->sin6_addr));
2255 static inline uint32_t
2256 in6_lltable_hash_dst(const struct in6_addr *dst, uint32_t hsize)
2259 return (IN6_LLTBL_HASH(dst->s6_addr32[3], hsize));
2263 in6_lltable_hash(const struct llentry *lle, uint32_t hsize)
2266 return (in6_lltable_hash_dst(&lle->r_l3addr.addr6, hsize));
2270 in6_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
2272 struct sockaddr_in6 *sin6;
2274 sin6 = (struct sockaddr_in6 *)sa;
2275 bzero(sin6, sizeof(*sin6));
2276 sin6->sin6_family = AF_INET6;
2277 sin6->sin6_len = sizeof(*sin6);
2278 sin6->sin6_addr = lle->r_l3addr.addr6;
2281 static inline struct llentry *
2282 in6_lltable_find_dst(struct lltable *llt, const struct in6_addr *dst)
2284 struct llentry *lle;
2285 struct llentries *lleh;
2288 hashidx = in6_lltable_hash_dst(dst, llt->llt_hsize);
2289 lleh = &llt->lle_head[hashidx];
2290 CK_LIST_FOREACH(lle, lleh, lle_next) {
2291 if (lle->la_flags & LLE_DELETED)
2293 if (IN6_ARE_ADDR_EQUAL(&lle->r_l3addr.addr6, dst))
2301 in6_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
2304 lle->la_flags |= LLE_DELETED;
2306 /* Leave the solicited multicast group. */
2307 if ((lle->la_flags & LLE_PUB) != 0)
2308 in6_leave_proxy_ndp_mc(llt->llt_ifp, &lle->r_l3addr.addr6);
2309 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
2311 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2316 static struct llentry *
2317 in6_lltable_alloc(struct lltable *llt, u_int flags,
2318 const struct sockaddr *l3addr)
2320 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2321 struct ifnet *ifp = llt->llt_ifp;
2322 struct llentry *lle;
2323 char linkhdr[LLE_MAX_LINKHDR];
2327 KASSERT(l3addr->sa_family == AF_INET6,
2328 ("sin_family %d", l3addr->sa_family));
2331 * A route that covers the given address must have
2332 * been installed 1st because we are doing a resolution,
2335 if (!(flags & LLE_IFADDR) &&
2336 in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2339 lle = in6_lltable_new(&sin6->sin6_addr, flags);
2341 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2344 lle->la_flags = flags;
2345 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
2346 linkhdrsize = LLE_MAX_LINKHDR;
2347 if (lltable_calc_llheader(ifp, AF_INET6, IF_LLADDR(ifp),
2348 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
2349 in6_lltable_free_entry(llt, lle);
2352 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
2354 lle->la_flags |= LLE_STATIC;
2357 if ((lle->la_flags & LLE_STATIC) != 0)
2358 lle->ln_state = ND6_LLINFO_REACHABLE;
2363 static struct llentry *
2364 in6_lltable_lookup(struct lltable *llt, u_int flags,
2365 const struct sockaddr *l3addr)
2367 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2368 int family = flags >> 16;
2369 struct llentry *lle;
2371 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
2372 KASSERT(l3addr->sa_family == AF_INET6,
2373 ("sin_family %d", l3addr->sa_family));
2374 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
2375 (LLE_UNLOCKED | LLE_EXCLUSIVE),
2376 ("wrong lle request flags: %#x", flags));
2378 lle = in6_lltable_find_dst(llt, &sin6->sin6_addr);
2380 if (__predict_false(family != AF_INET6))
2381 lle = llentry_lookup_family(lle, family);
2386 if (flags & LLE_UNLOCKED)
2389 if (flags & LLE_EXCLUSIVE)
2395 * If the afdata lock is not held, the LLE may have been unlinked while
2396 * we were blocked on the LLE lock. Check for this case.
2398 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
2399 if (flags & LLE_EXCLUSIVE)
2409 in6_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
2410 struct sysctl_req *wr)
2412 struct ifnet *ifp = llt->llt_ifp;
2415 struct rt_msghdr rtm;
2416 struct sockaddr_in6 sin6;
2418 * ndp.c assumes that sdl is word aligned
2423 struct sockaddr_dl sdl;
2425 struct sockaddr_dl *sdl;
2428 bzero(&ndpc, sizeof(ndpc));
2429 /* skip deleted entries */
2430 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
2432 /* Skip if jailed and not a valid IP of the prison. */
2433 lltable_fill_sa_entry(lle, (struct sockaddr *)&ndpc.sin6);
2434 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&ndpc.sin6) != 0)
2437 * produce a msg made of:
2439 * struct sockaddr_in6 (IPv6)
2440 * struct sockaddr_dl;
2442 ndpc.rtm.rtm_msglen = sizeof(ndpc);
2443 ndpc.rtm.rtm_version = RTM_VERSION;
2444 ndpc.rtm.rtm_type = RTM_GET;
2445 ndpc.rtm.rtm_flags = RTF_UP;
2446 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2447 sa6_recoverscope(&ndpc.sin6);
2450 if (lle->la_flags & LLE_PUB)
2451 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2454 sdl->sdl_family = AF_LINK;
2455 sdl->sdl_len = sizeof(*sdl);
2456 sdl->sdl_index = ifp->if_index;
2457 sdl->sdl_type = ifp->if_type;
2458 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
2459 sdl->sdl_alen = ifp->if_addrlen;
2460 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2463 bzero(LLADDR(sdl), ifp->if_addrlen);
2465 if (lle->la_expire != 0)
2466 ndpc.rtm.rtm_rmx.rmx_expire = lle->la_expire +
2467 lle->lle_remtime / hz + time_second - time_uptime;
2468 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2469 if (lle->la_flags & LLE_STATIC)
2470 ndpc.rtm.rtm_flags |= RTF_STATIC;
2471 if (lle->la_flags & LLE_IFADDR)
2472 ndpc.rtm.rtm_flags |= RTF_PINNED;
2473 if (lle->ln_router != 0)
2474 ndpc.rtm.rtm_flags |= RTF_GATEWAY;
2475 ndpc.rtm.rtm_rmx.rmx_pksent = lle->la_asked;
2476 /* Store state in rmx_weight value */
2477 ndpc.rtm.rtm_rmx.rmx_state = lle->ln_state;
2478 ndpc.rtm.rtm_index = ifp->if_index;
2479 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2485 in6_lltable_post_resolved(struct lltable *llt, struct llentry *lle)
2487 /* Join the solicited multicast group for dst. */
2488 if ((lle->la_flags & LLE_PUB) == LLE_PUB)
2489 in6_join_proxy_ndp_mc(llt->llt_ifp, &lle->r_l3addr.addr6);
2492 static struct lltable *
2493 in6_lltattach(struct ifnet *ifp)
2495 struct lltable *llt;
2497 llt = lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE);
2498 llt->llt_af = AF_INET6;
2501 llt->llt_lookup = in6_lltable_lookup;
2502 llt->llt_alloc_entry = in6_lltable_alloc;
2503 llt->llt_delete_entry = in6_lltable_delete_entry;
2504 llt->llt_dump_entry = in6_lltable_dump_entry;
2505 llt->llt_hash = in6_lltable_hash;
2506 llt->llt_fill_sa_entry = in6_lltable_fill_sa_entry;
2507 llt->llt_free_entry = in6_lltable_free_entry;
2508 llt->llt_match_prefix = in6_lltable_match_prefix;
2509 llt->llt_mark_used = llentry_mark_used;
2510 llt->llt_post_resolved = in6_lltable_post_resolved;
2517 in6_lltable_get(struct ifnet *ifp)
2519 struct lltable *llt = NULL;
2521 void *afdata_ptr = ifp->if_afdata[AF_INET6];
2522 if (afdata_ptr != NULL)
2523 llt = ((struct in6_ifextra *)afdata_ptr)->lltable;
2528 in6_domifattach(struct ifnet *ifp)
2530 struct in6_ifextra *ext;
2532 /* There are not IPv6-capable interfaces. */
2533 switch (ifp->if_type) {
2539 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2540 bzero(ext, sizeof(*ext));
2542 ext->in6_ifstat = malloc(sizeof(counter_u64_t) *
2543 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_IFADDR, M_WAITOK);
2544 COUNTER_ARRAY_ALLOC(ext->in6_ifstat,
2545 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_WAITOK);
2547 ext->icmp6_ifstat = malloc(sizeof(counter_u64_t) *
2548 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_IFADDR,
2550 COUNTER_ARRAY_ALLOC(ext->icmp6_ifstat,
2551 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_WAITOK);
2553 ext->nd_ifinfo = nd6_ifattach(ifp);
2554 ext->scope6_id = scope6_ifattach(ifp);
2555 ext->lltable = in6_lltattach(ifp);
2557 ext->mld_ifinfo = mld_domifattach(ifp);
2563 in6_domifmtu(struct ifnet *ifp)
2565 if (ifp->if_afdata[AF_INET6] == NULL)
2568 return (IN6_LINKMTU(ifp));
2572 in6_domifdetach(struct ifnet *ifp, void *aux)
2574 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2576 mld_domifdetach(ifp);
2577 scope6_ifdetach(ext->scope6_id);
2578 nd6_ifdetach(ifp, ext->nd_ifinfo);
2579 lltable_free(ext->lltable);
2580 COUNTER_ARRAY_FREE(ext->in6_ifstat,
2581 sizeof(struct in6_ifstat) / sizeof(uint64_t));
2582 free(ext->in6_ifstat, M_IFADDR);
2583 COUNTER_ARRAY_FREE(ext->icmp6_ifstat,
2584 sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
2585 free(ext->icmp6_ifstat, M_IFADDR);
2586 free(ext, M_IFADDR);
2590 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2591 * v4 mapped addr or v4 compat addr
2594 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2597 bzero(sin, sizeof(*sin));
2598 sin->sin_len = sizeof(struct sockaddr_in);
2599 sin->sin_family = AF_INET;
2600 sin->sin_port = sin6->sin6_port;
2601 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2604 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2606 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2608 bzero(sin6, sizeof(*sin6));
2609 sin6->sin6_len = sizeof(struct sockaddr_in6);
2610 sin6->sin6_family = AF_INET6;
2611 sin6->sin6_port = sin->sin_port;
2612 sin6->sin6_addr.s6_addr32[0] = 0;
2613 sin6->sin6_addr.s6_addr32[1] = 0;
2614 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2615 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2618 /* Convert sockaddr_in6 into sockaddr_in. */
2620 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2622 struct sockaddr_in *sin_p;
2623 struct sockaddr_in6 sin6;
2626 * Save original sockaddr_in6 addr and convert it
2629 sin6 = *(struct sockaddr_in6 *)nam;
2630 sin_p = (struct sockaddr_in *)nam;
2631 in6_sin6_2_sin(sin_p, &sin6);
2634 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2636 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2638 struct sockaddr_in *sin_p;
2639 struct sockaddr_in6 *sin6_p;
2641 sin6_p = malloc(sizeof *sin6_p, M_SONAME, M_WAITOK);
2642 sin_p = (struct sockaddr_in *)*nam;
2643 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2644 free(*nam, M_SONAME);
2645 *nam = (struct sockaddr *)sin6_p;
2649 * Join/leave the solicited multicast groups for proxy NDP entries.
2652 in6_join_proxy_ndp_mc(struct ifnet *ifp, const struct in6_addr *dst)
2654 struct in6_multi *inm;
2655 struct in6_addr mltaddr;
2656 char ip6buf[INET6_ADDRSTRLEN];
2659 if (in6_solicited_node_maddr(&mltaddr, ifp, dst) != 0)
2660 return; /* error logged in in6_solicited_node_maddr. */
2662 error = in6_joingroup(ifp, &mltaddr, NULL, &inm, 0);
2664 nd6log((LOG_WARNING,
2665 "%s: in6_joingroup failed for %s on %s (errno=%d)\n",
2666 __func__, ip6_sprintf(ip6buf, &mltaddr), if_name(ifp),
2672 in6_leave_proxy_ndp_mc(struct ifnet *ifp, const struct in6_addr *dst)
2674 struct epoch_tracker et;
2675 struct in6_multi *inm;
2676 struct in6_addr mltaddr;
2677 char ip6buf[INET6_ADDRSTRLEN];
2679 if (in6_solicited_node_maddr(&mltaddr, ifp, dst) != 0)
2680 return; /* error logged in in6_solicited_node_maddr. */
2682 NET_EPOCH_ENTER(et);
2683 inm = in6m_lookup(ifp, &mltaddr);
2686 in6_leavegroup(inm, NULL);
2688 nd6log((LOG_WARNING, "%s: in6m_lookup failed for %s on %s\n",
2689 __func__, ip6_sprintf(ip6buf, &mltaddr), if_name(ifp)));
2693 in6_lle_match_pub(struct lltable *llt, struct llentry *lle, void *farg)
2695 return ((lle->la_flags & LLE_PUB) != 0);
2699 in6_purge_proxy_ndp(struct ifnet *ifp)
2701 struct lltable *llt;
2704 if (ifp->if_afdata[AF_INET6] == NULL)
2707 llt = LLTABLE6(ifp);
2708 IF_AFDATA_WLOCK(ifp);
2709 need_purge = ((llt->llt_flags & LLT_ADDEDPROXY) != 0);
2710 IF_AFDATA_WUNLOCK(ifp);
2713 * Ever added proxy ndp entries, leave solicited node multicast
2714 * before deleting the llentry.
2717 lltable_delete_conditional(llt, in6_lle_match_pub, NULL);