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_flags = RTF_PINNED | RTF_HOST,
1300 [RTAX_DST] = (struct sockaddr *)&dst,
1301 [RTAX_GATEWAY] = (struct sockaddr *)&sdl,
1304 /* Don't set additional per-gw filters on removal */
1306 NET_EPOCH_ENTER(et);
1307 error = rib_handle_ifaddr_info(ifa->ifa_ifp->if_fib, cmd, &info);
1314 ifa_is_p2p(struct in6_ifaddr *ia)
1318 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1320 if ((plen == 128) && (ia->ia_dstaddr.sin6_family == AF_INET6) &&
1321 !IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &ia->ia_dstaddr.sin6_addr))
1328 in6_purgeaddr(struct ifaddr *ifa)
1330 struct ifnet *ifp = ifa->ifa_ifp;
1331 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1332 struct in6_multi_mship *imm;
1336 (*carp_detach_p)(ifa, false);
1339 * Remove the loopback route to the interface address.
1340 * The check for the current setting of "nd6_useloopback"
1343 if (ia->ia_flags & IFA_RTSELF) {
1344 error = ifa_del_loopback_route((struct ifaddr *)ia,
1345 (struct sockaddr *)&ia->ia_addr);
1347 ia->ia_flags &= ~IFA_RTSELF;
1350 /* stop DAD processing */
1353 /* Leave multicast groups. */
1354 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1355 LIST_REMOVE(imm, i6mm_chain);
1356 if (imm->i6mm_maddr != NULL)
1357 in6_leavegroup(imm->i6mm_maddr, NULL);
1358 free(imm, M_IP6MADDR);
1360 /* Check if we need to remove p2p route */
1361 if ((ia->ia_flags & IFA_ROUTE) && ifa_is_p2p(ia)) {
1362 error = in6_handle_dstaddr_rtrequest(RTM_DELETE, ia);
1364 log(LOG_INFO, "%s: err=%d, destination address delete "
1365 "failed\n", __func__, error);
1366 ia->ia_flags &= ~IFA_ROUTE;
1369 in6_newaddrmsg(ia, RTM_DELETE);
1370 in6_unlink_ifa(ia, ifp);
1374 * Removes @ia from the corresponding interfaces and unlinks corresponding
1375 * prefix if no addresses are using it anymore.
1378 in6_purgeifaddr(struct in6_ifaddr *ia)
1380 struct nd_prefix *pr;
1383 * If the address being deleted is the only one that owns
1384 * the corresponding prefix, expire the prefix as well.
1385 * XXX: theoretically, we don't have to worry about such
1386 * relationship, since we separate the address management
1387 * and the prefix management. We do this, however, to provide
1388 * as much backward compatibility as possible in terms of
1389 * the ioctl operation.
1390 * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
1393 in6_purgeaddr(&ia->ia_ifa);
1394 if (pr != NULL && pr->ndpr_addrcnt == 0) {
1396 nd6_prefix_unlink(pr, NULL);
1404 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1406 char ip6buf[INET6_ADDRSTRLEN];
1410 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
1411 IF_ADDR_WUNLOCK(ifp);
1412 ifa_free(&ia->ia_ifa); /* if_addrhead */
1415 * Defer the release of what might be the last reference to the
1416 * in6_ifaddr so that it can't be freed before the remainder of the
1420 CK_STAILQ_REMOVE(&V_in6_ifaddrhead, ia, in6_ifaddr, ia_link);
1421 CK_LIST_REMOVE(ia, ia6_hash);
1422 IN6_IFADDR_WUNLOCK();
1425 * Release the reference to the base prefix. There should be a
1426 * positive reference.
1429 if (ia->ia6_ndpr == NULL) {
1431 "in6_unlink_ifa: autoconf'ed address "
1432 "%s has no prefix\n", ip6_sprintf(ip6buf, IA6_IN6(ia))));
1434 ia->ia6_ndpr->ndpr_addrcnt--;
1435 /* Do not delete lles within prefix if refcont != 0 */
1436 if (ia->ia6_ndpr->ndpr_addrcnt == 0)
1438 ia->ia6_ndpr = NULL;
1441 nd6_rem_ifa_lle(ia, remove_lle);
1444 * Also, if the address being removed is autoconf'ed, call
1445 * pfxlist_onlink_check() since the release might affect the status of
1446 * other (detached) addresses.
1448 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1449 pfxlist_onlink_check();
1451 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */
1455 * Notifies other subsystems about address change/arrival:
1456 * 1) Notifies device handler on the first IPv6 address assignment
1457 * 2) Handle routing table changes for P2P links and route
1458 * 3) Handle routing table changes for address host route
1461 in6_notify_ifa(struct ifnet *ifp, struct in6_ifaddr *ia,
1462 struct in6_aliasreq *ifra, int hostIsNew)
1464 int error = 0, ifacount = 0;
1466 struct sockaddr_in6 *pdst;
1467 char ip6buf[INET6_ADDRSTRLEN];
1470 * Give the interface a chance to initialize
1471 * if this is its first address,
1473 if (hostIsNew != 0) {
1474 struct epoch_tracker et;
1476 NET_EPOCH_ENTER(et);
1477 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1478 if (ifa->ifa_addr->sa_family != AF_INET6)
1485 if (ifacount <= 1 && ifp->if_ioctl) {
1486 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1492 * If a new destination address is specified, scrub the old one and
1493 * install the new destination. Note that the interface must be
1496 pdst = &ifra->ifra_dstaddr;
1497 if (pdst->sin6_family == AF_INET6 &&
1498 !IN6_ARE_ADDR_EQUAL(&pdst->sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
1499 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1500 (in6_handle_dstaddr_rtrequest(RTM_DELETE, ia) != 0)) {
1501 nd6log((LOG_ERR, "in6_update_ifa_internal: failed to "
1502 "remove a route to the old destination: %s\n",
1503 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1504 /* proceed anyway... */
1506 ia->ia_flags &= ~IFA_ROUTE;
1507 ia->ia_dstaddr = *pdst;
1511 * If a new destination address is specified for a point-to-point
1512 * interface, install a route to the destination as an interface
1514 * XXX: the logic below rejects assigning multiple addresses on a p2p
1515 * interface that share the same destination.
1517 if (!(ia->ia_flags & IFA_ROUTE) && ifa_is_p2p(ia)) {
1518 error = in6_handle_dstaddr_rtrequest(RTM_ADD, ia);
1521 ia->ia_flags |= IFA_ROUTE;
1525 * add a loopback route to self if not exists
1527 if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
1528 error = ifa_add_loopback_route((struct ifaddr *)ia,
1529 (struct sockaddr *)&ia->ia_addr);
1531 ia->ia_flags |= IFA_RTSELF;
1534 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1535 "Invoking IPv6 network device address event may sleep");
1537 ifa_ref(&ia->ia_ifa);
1538 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
1540 ifa_free(&ia->ia_ifa);
1546 * Find an IPv6 interface link-local address specific to an interface.
1547 * ifaddr is returned referenced.
1550 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1556 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1557 if (ifa->ifa_addr->sa_family != AF_INET6)
1559 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1560 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1568 return ((struct in6_ifaddr *)ifa);
1572 * find the interface address corresponding to a given IPv6 address.
1573 * ifaddr is returned referenced if @referenced flag is set.
1576 in6ifa_ifwithaddr(const struct in6_addr *addr, uint32_t zoneid, bool referenced)
1578 struct rm_priotracker in6_ifa_tracker;
1579 struct in6_ifaddr *ia;
1581 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1582 CK_LIST_FOREACH(ia, IN6ADDR_HASH(addr), ia6_hash) {
1583 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), addr)) {
1585 zoneid != ia->ia_addr.sin6_scope_id)
1588 ifa_ref(&ia->ia_ifa);
1592 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1597 * find the internet address corresponding to a given interface and address.
1598 * ifaddr is returned referenced.
1601 in6ifa_ifpwithaddr(struct ifnet *ifp, const struct in6_addr *addr)
1603 struct epoch_tracker et;
1606 NET_EPOCH_ENTER(et);
1607 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1608 if (ifa->ifa_addr->sa_family != AF_INET6)
1610 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1617 return ((struct in6_ifaddr *)ifa);
1621 * Find a link-local scoped address on ifp and return it if any.
1624 in6ifa_llaonifp(struct ifnet *ifp)
1626 struct epoch_tracker et;
1627 struct sockaddr_in6 *sin6;
1630 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1632 NET_EPOCH_ENTER(et);
1633 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1634 if (ifa->ifa_addr->sa_family != AF_INET6)
1636 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1637 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
1638 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
1639 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
1644 return ((struct in6_ifaddr *)ifa);
1648 * Convert IP6 address to printable (loggable) representation. Caller
1649 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1651 static char digits[] = "0123456789abcdef";
1653 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1655 int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
1657 const u_int16_t *a = (const u_int16_t *)addr;
1659 int dcolon = 0, zero = 0;
1663 for (i = 0; i < 8; i++) {
1664 if (*(a + i) == 0) {
1669 else if (maxcnt < cnt) {
1680 for (i = 0; i < 8; i++) {
1691 if (dcolon == 0 && *(a + 1) == 0 && i == index) {
1703 d = (const u_char *)a;
1704 /* Try to eliminate leading zeros in printout like in :0001. */
1706 *cp = digits[*d >> 4];
1711 *cp = digits[*d++ & 0xf];
1712 if (zero == 0 || (*cp != '0')) {
1716 *cp = digits[*d >> 4];
1717 if (zero == 0 || (*cp != '0')) {
1721 *cp++ = digits[*d & 0xf];
1730 in6_localaddr(struct in6_addr *in6)
1732 struct rm_priotracker in6_ifa_tracker;
1733 struct in6_ifaddr *ia;
1735 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1738 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1739 CK_STAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1740 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1741 &ia->ia_prefixmask.sin6_addr)) {
1742 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1746 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1752 * Return 1 if an internet address is for the local host and configured
1753 * on one of its interfaces.
1756 in6_localip(struct in6_addr *in6)
1758 struct rm_priotracker in6_ifa_tracker;
1759 struct in6_ifaddr *ia;
1761 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1762 CK_LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) {
1763 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) {
1764 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1768 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1773 * Like in6_localip(), but FIB-aware.
1776 in6_localip_fib(struct in6_addr *in6, uint16_t fib)
1778 struct rm_priotracker in6_ifa_tracker;
1779 struct in6_ifaddr *ia;
1781 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1782 CK_LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) {
1783 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr) &&
1784 ia->ia_ifa.ifa_ifp->if_fib == fib) {
1785 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1789 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1794 * Return 1 if an internet address is configured on an interface.
1797 in6_ifhasaddr(struct ifnet *ifp, struct in6_addr *addr)
1799 struct in6_addr in6;
1801 struct in6_ifaddr *ia6;
1806 if (in6_clearscope(&in6))
1808 in6_setscope(&in6, ifp, NULL);
1810 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1811 if (ifa->ifa_addr->sa_family != AF_INET6)
1813 ia6 = (struct in6_ifaddr *)ifa;
1814 if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &in6))
1822 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1824 struct rm_priotracker in6_ifa_tracker;
1825 struct in6_ifaddr *ia;
1827 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1828 CK_LIST_FOREACH(ia, IN6ADDR_HASH(&sa6->sin6_addr), ia6_hash) {
1829 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), &sa6->sin6_addr)) {
1830 if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
1831 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1832 return (1); /* true */
1837 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1839 return (0); /* false */
1843 * return length of part which dst and src are equal
1847 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1850 u_char *s = (u_char *)src, *d = (u_char *)dst;
1851 u_char *lim = s + 16, r;
1854 if ((r = (*d++ ^ *s++)) != 0) {
1865 /* XXX: to be scope conscious */
1867 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1869 int bytelen, bitlen;
1872 if (0 > len || len > 128) {
1873 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1881 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1884 p1->s6_addr[bytelen] >> (8 - bitlen) !=
1885 p2->s6_addr[bytelen] >> (8 - bitlen))
1892 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1894 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1895 int bytelen, bitlen, i;
1898 if (0 > len || len > 128) {
1899 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1904 bzero(maskp, sizeof(*maskp));
1907 for (i = 0; i < bytelen; i++)
1908 maskp->s6_addr[i] = 0xff;
1910 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1914 * return the best address out of the same scope. if no address was
1915 * found, return the first valid address from designated IF.
1918 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
1920 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1922 struct in6_ifaddr *besta = NULL;
1923 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
1927 dep[0] = dep[1] = NULL;
1930 * We first look for addresses in the same scope.
1931 * If there is one, return it.
1932 * If two or more, return one which matches the dst longest.
1933 * If none, return one of global addresses assigned other ifs.
1935 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1936 if (ifa->ifa_addr->sa_family != AF_INET6)
1938 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1939 continue; /* XXX: is there any case to allow anycast? */
1940 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1941 continue; /* don't use this interface */
1942 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1944 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1945 if (V_ip6_use_deprecated)
1946 dep[0] = (struct in6_ifaddr *)ifa;
1950 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1952 * call in6_matchlen() as few as possible
1956 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1957 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1960 besta = (struct in6_ifaddr *)ifa;
1963 besta = (struct in6_ifaddr *)ifa;
1969 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1970 if (ifa->ifa_addr->sa_family != AF_INET6)
1972 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1973 continue; /* XXX: is there any case to allow anycast? */
1974 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1975 continue; /* don't use this interface */
1976 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1978 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1979 if (V_ip6_use_deprecated)
1980 dep[1] = (struct in6_ifaddr *)ifa;
1984 return (struct in6_ifaddr *)ifa;
1987 /* use the last-resort values, that are, deprecated addresses */
1997 * perform DAD when interface becomes IFF_UP.
2000 in6_if_up(struct ifnet *ifp)
2002 struct epoch_tracker et;
2004 struct in6_ifaddr *ia;
2006 NET_EPOCH_ENTER(et);
2007 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2008 if (ifa->ifa_addr->sa_family != AF_INET6)
2010 ia = (struct in6_ifaddr *)ifa;
2011 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2013 * The TENTATIVE flag was likely set by hand
2014 * beforehand, implicitly indicating the need for DAD.
2015 * We may be able to skip the random delay in this
2016 * case, but we impose delays just in case.
2019 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2025 * special cases, like 6to4, are handled in in6_ifattach
2027 in6_ifattach(ifp, NULL);
2031 in6if_do_dad(struct ifnet *ifp)
2034 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2036 if ((ifp->if_flags & IFF_MULTICAST) == 0)
2038 if ((ND_IFINFO(ifp)->flags &
2039 (ND6_IFF_IFDISABLED | ND6_IFF_NO_DAD)) != 0)
2045 * Calculate max IPv6 MTU through all the interfaces and store it
2051 struct epoch_tracker et;
2052 unsigned long maxmtu = 0;
2055 NET_EPOCH_ENTER(et);
2056 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2057 /* this function can be called during ifnet initialization */
2058 if (!ifp->if_afdata[AF_INET6])
2060 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2061 IN6_LINKMTU(ifp) > maxmtu)
2062 maxmtu = IN6_LINKMTU(ifp);
2065 if (maxmtu) /* update only when maxmtu is positive */
2066 V_in6_maxmtu = maxmtu;
2070 * Provide the length of interface identifiers to be used for the link attached
2071 * to the given interface. The length should be defined in "IPv6 over
2072 * xxx-link" document. Note that address architecture might also define
2073 * the length for a particular set of address prefixes, regardless of the
2074 * link type. As clarified in rfc2462bis, those two definitions should be
2075 * consistent, and those really are as of August 2004.
2078 in6_if2idlen(struct ifnet *ifp)
2080 switch (ifp->if_type) {
2081 case IFT_ETHER: /* RFC2464 */
2082 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */
2083 case IFT_L2VLAN: /* ditto */
2084 case IFT_BRIDGE: /* bridge(4) only does Ethernet-like links */
2085 case IFT_INFINIBAND:
2087 case IFT_PPP: /* RFC2472 */
2089 case IFT_FRELAY: /* RFC2590 */
2091 case IFT_IEEE1394: /* RFC3146 */
2094 return (64); /* draft-ietf-v6ops-mech-v2-07 */
2096 return (64); /* XXX: is this really correct? */
2099 * Unknown link type:
2100 * It might be controversial to use the today's common constant
2101 * of 64 for these cases unconditionally. For full compliance,
2102 * we should return an error in this case. On the other hand,
2103 * if we simply miss the standard for the link type or a new
2104 * standard is defined for a new link type, the IFID length
2105 * is very likely to be the common constant. As a compromise,
2106 * we always use the constant, but make an explicit notice
2107 * indicating the "unknown" case.
2109 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2114 struct in6_llentry {
2115 struct llentry base;
2118 #define IN6_LLTBL_DEFAULT_HSIZE 32
2119 #define IN6_LLTBL_HASH(k, h) \
2120 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
2123 * Do actual deallocation of @lle.
2126 in6_lltable_destroy_lle_unlocked(epoch_context_t ctx)
2128 struct llentry *lle;
2130 lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
2131 LLE_LOCK_DESTROY(lle);
2132 LLE_REQ_DESTROY(lle);
2133 free(lle, M_LLTABLE);
2137 * Called by LLE_FREE_LOCKED when number of references
2141 in6_lltable_destroy_lle(struct llentry *lle)
2145 NET_EPOCH_CALL(in6_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
2148 static struct llentry *
2149 in6_lltable_new(const struct in6_addr *addr6, u_int flags)
2151 struct in6_llentry *lle;
2153 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
2154 if (lle == NULL) /* NB: caller generates msg */
2157 lle->base.r_l3addr.addr6 = *addr6;
2158 lle->base.lle_refcnt = 1;
2159 lle->base.lle_free = in6_lltable_destroy_lle;
2160 LLE_LOCK_INIT(&lle->base);
2161 LLE_REQ_INIT(&lle->base);
2162 callout_init(&lle->base.lle_timer, 1);
2164 return (&lle->base);
2168 in6_lltable_match_prefix(const struct sockaddr *saddr,
2169 const struct sockaddr *smask, u_int flags, struct llentry *lle)
2171 const struct in6_addr *addr, *mask, *lle_addr;
2173 addr = &((const struct sockaddr_in6 *)saddr)->sin6_addr;
2174 mask = &((const struct sockaddr_in6 *)smask)->sin6_addr;
2175 lle_addr = &lle->r_l3addr.addr6;
2177 if (IN6_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
2180 if (lle->la_flags & LLE_IFADDR) {
2182 * Delete LLE_IFADDR records IFF address & flag matches.
2183 * Note that addr is the interface address within prefix
2186 if (IN6_ARE_ADDR_EQUAL(addr, lle_addr) &&
2187 (flags & LLE_STATIC) != 0)
2192 /* flags & LLE_STATIC means deleting both dynamic and static entries */
2193 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
2200 in6_lltable_free_entry(struct lltable *llt, struct llentry *lle)
2202 struct ifnet *ifp __diagused;
2204 LLE_WLOCK_ASSERT(lle);
2205 KASSERT(llt != NULL, ("lltable is NULL"));
2207 /* Unlink entry from table */
2208 if ((lle->la_flags & LLE_LINKED) != 0) {
2210 IF_AFDATA_WLOCK_ASSERT(ifp);
2211 lltable_unlink_entry(llt, lle);
2218 in6_lltable_rtcheck(struct ifnet *ifp,
2220 const struct sockaddr *l3addr)
2222 const struct sockaddr_in6 *sin6;
2223 struct nhop_object *nh;
2224 struct in6_addr dst;
2226 char ip6buf[INET6_ADDRSTRLEN];
2230 KASSERT(l3addr->sa_family == AF_INET6,
2231 ("sin_family %d", l3addr->sa_family));
2233 sin6 = (const struct sockaddr_in6 *)l3addr;
2234 in6_splitscope(&sin6->sin6_addr, &dst, &scopeid);
2235 fibnum = V_rt_add_addr_allfibs ? RT_DEFAULT_FIB : ifp->if_fib;
2236 nh = fib6_lookup(fibnum, &dst, scopeid, NHR_NONE, 0);
2237 if (nh && ((nh->nh_flags & NHF_GATEWAY) || nh->nh_ifp != ifp)) {
2240 * Create an ND6 cache for an IPv6 neighbor
2241 * that is not covered by our own prefix.
2243 ifa = ifaof_ifpforaddr(l3addr, ifp);
2247 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2248 ip6_sprintf(ip6buf, &sin6->sin6_addr));
2254 static inline uint32_t
2255 in6_lltable_hash_dst(const struct in6_addr *dst, uint32_t hsize)
2258 return (IN6_LLTBL_HASH(dst->s6_addr32[3], hsize));
2262 in6_lltable_hash(const struct llentry *lle, uint32_t hsize)
2265 return (in6_lltable_hash_dst(&lle->r_l3addr.addr6, hsize));
2269 in6_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
2271 struct sockaddr_in6 *sin6;
2273 sin6 = (struct sockaddr_in6 *)sa;
2274 bzero(sin6, sizeof(*sin6));
2275 sin6->sin6_family = AF_INET6;
2276 sin6->sin6_len = sizeof(*sin6);
2277 sin6->sin6_addr = lle->r_l3addr.addr6;
2280 static inline struct llentry *
2281 in6_lltable_find_dst(struct lltable *llt, const struct in6_addr *dst)
2283 struct llentry *lle;
2284 struct llentries *lleh;
2287 hashidx = in6_lltable_hash_dst(dst, llt->llt_hsize);
2288 lleh = &llt->lle_head[hashidx];
2289 CK_LIST_FOREACH(lle, lleh, lle_next) {
2290 if (lle->la_flags & LLE_DELETED)
2292 if (IN6_ARE_ADDR_EQUAL(&lle->r_l3addr.addr6, dst))
2300 in6_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
2303 lle->la_flags |= LLE_DELETED;
2305 /* Leave the solicited multicast group. */
2306 if ((lle->la_flags & LLE_PUB) != 0)
2307 in6_leave_proxy_ndp_mc(llt->llt_ifp, &lle->r_l3addr.addr6);
2308 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
2310 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2315 static struct llentry *
2316 in6_lltable_alloc(struct lltable *llt, u_int flags,
2317 const struct sockaddr *l3addr)
2319 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2320 struct ifnet *ifp = llt->llt_ifp;
2321 struct llentry *lle;
2322 char linkhdr[LLE_MAX_LINKHDR];
2326 KASSERT(l3addr->sa_family == AF_INET6,
2327 ("sin_family %d", l3addr->sa_family));
2330 * A route that covers the given address must have
2331 * been installed 1st because we are doing a resolution,
2334 if (!(flags & LLE_IFADDR) &&
2335 in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2338 lle = in6_lltable_new(&sin6->sin6_addr, flags);
2340 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2343 lle->la_flags = flags;
2344 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
2345 linkhdrsize = LLE_MAX_LINKHDR;
2346 if (lltable_calc_llheader(ifp, AF_INET6, IF_LLADDR(ifp),
2347 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
2348 in6_lltable_free_entry(llt, lle);
2351 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
2353 lle->la_flags |= LLE_STATIC;
2356 if ((lle->la_flags & LLE_STATIC) != 0)
2357 lle->ln_state = ND6_LLINFO_REACHABLE;
2362 static struct llentry *
2363 in6_lltable_lookup(struct lltable *llt, u_int flags,
2364 const struct sockaddr *l3addr)
2366 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2367 int family = flags >> 16;
2368 struct llentry *lle;
2370 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
2371 KASSERT(l3addr->sa_family == AF_INET6,
2372 ("sin_family %d", l3addr->sa_family));
2373 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
2374 (LLE_UNLOCKED | LLE_EXCLUSIVE),
2375 ("wrong lle request flags: %#x", flags));
2377 lle = in6_lltable_find_dst(llt, &sin6->sin6_addr);
2379 if (__predict_false(family != AF_INET6))
2380 lle = llentry_lookup_family(lle, family);
2385 if (flags & LLE_UNLOCKED)
2388 if (flags & LLE_EXCLUSIVE)
2394 * If the afdata lock is not held, the LLE may have been unlinked while
2395 * we were blocked on the LLE lock. Check for this case.
2397 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
2398 if (flags & LLE_EXCLUSIVE)
2408 in6_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
2409 struct sysctl_req *wr)
2411 struct ifnet *ifp = llt->llt_ifp;
2414 struct rt_msghdr rtm;
2415 struct sockaddr_in6 sin6;
2417 * ndp.c assumes that sdl is word aligned
2422 struct sockaddr_dl sdl;
2424 struct sockaddr_dl *sdl;
2427 bzero(&ndpc, sizeof(ndpc));
2428 /* skip deleted entries */
2429 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
2431 /* Skip if jailed and not a valid IP of the prison. */
2432 lltable_fill_sa_entry(lle, (struct sockaddr *)&ndpc.sin6);
2433 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&ndpc.sin6) != 0)
2436 * produce a msg made of:
2438 * struct sockaddr_in6 (IPv6)
2439 * struct sockaddr_dl;
2441 ndpc.rtm.rtm_msglen = sizeof(ndpc);
2442 ndpc.rtm.rtm_version = RTM_VERSION;
2443 ndpc.rtm.rtm_type = RTM_GET;
2444 ndpc.rtm.rtm_flags = RTF_UP;
2445 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2446 sa6_recoverscope(&ndpc.sin6);
2449 if (lle->la_flags & LLE_PUB)
2450 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2453 sdl->sdl_family = AF_LINK;
2454 sdl->sdl_len = sizeof(*sdl);
2455 sdl->sdl_index = ifp->if_index;
2456 sdl->sdl_type = ifp->if_type;
2457 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
2458 sdl->sdl_alen = ifp->if_addrlen;
2459 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2462 bzero(LLADDR(sdl), ifp->if_addrlen);
2464 if (lle->la_expire != 0)
2465 ndpc.rtm.rtm_rmx.rmx_expire = lle->la_expire +
2466 lle->lle_remtime / hz + time_second - time_uptime;
2467 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2468 if (lle->la_flags & LLE_STATIC)
2469 ndpc.rtm.rtm_flags |= RTF_STATIC;
2470 if (lle->la_flags & LLE_IFADDR)
2471 ndpc.rtm.rtm_flags |= RTF_PINNED;
2472 if (lle->ln_router != 0)
2473 ndpc.rtm.rtm_flags |= RTF_GATEWAY;
2474 ndpc.rtm.rtm_rmx.rmx_pksent = lle->la_asked;
2475 /* Store state in rmx_weight value */
2476 ndpc.rtm.rtm_rmx.rmx_state = lle->ln_state;
2477 ndpc.rtm.rtm_index = ifp->if_index;
2478 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2484 in6_lltable_post_resolved(struct lltable *llt, struct llentry *lle)
2486 /* Join the solicited multicast group for dst. */
2487 if ((lle->la_flags & LLE_PUB) == LLE_PUB)
2488 in6_join_proxy_ndp_mc(llt->llt_ifp, &lle->r_l3addr.addr6);
2491 static struct lltable *
2492 in6_lltattach(struct ifnet *ifp)
2494 struct lltable *llt;
2496 llt = lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE);
2497 llt->llt_af = AF_INET6;
2500 llt->llt_lookup = in6_lltable_lookup;
2501 llt->llt_alloc_entry = in6_lltable_alloc;
2502 llt->llt_delete_entry = in6_lltable_delete_entry;
2503 llt->llt_dump_entry = in6_lltable_dump_entry;
2504 llt->llt_hash = in6_lltable_hash;
2505 llt->llt_fill_sa_entry = in6_lltable_fill_sa_entry;
2506 llt->llt_free_entry = in6_lltable_free_entry;
2507 llt->llt_match_prefix = in6_lltable_match_prefix;
2508 llt->llt_mark_used = llentry_mark_used;
2509 llt->llt_post_resolved = in6_lltable_post_resolved;
2516 in6_lltable_get(struct ifnet *ifp)
2518 struct lltable *llt = NULL;
2520 void *afdata_ptr = ifp->if_afdata[AF_INET6];
2521 if (afdata_ptr != NULL)
2522 llt = ((struct in6_ifextra *)afdata_ptr)->lltable;
2527 in6_domifattach(struct ifnet *ifp)
2529 struct in6_ifextra *ext;
2531 /* There are not IPv6-capable interfaces. */
2532 switch (ifp->if_type) {
2538 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2539 bzero(ext, sizeof(*ext));
2541 ext->in6_ifstat = malloc(sizeof(counter_u64_t) *
2542 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_IFADDR, M_WAITOK);
2543 COUNTER_ARRAY_ALLOC(ext->in6_ifstat,
2544 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_WAITOK);
2546 ext->icmp6_ifstat = malloc(sizeof(counter_u64_t) *
2547 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_IFADDR,
2549 COUNTER_ARRAY_ALLOC(ext->icmp6_ifstat,
2550 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_WAITOK);
2552 ext->nd_ifinfo = nd6_ifattach(ifp);
2553 ext->scope6_id = scope6_ifattach(ifp);
2554 ext->lltable = in6_lltattach(ifp);
2556 ext->mld_ifinfo = mld_domifattach(ifp);
2562 in6_domifmtu(struct ifnet *ifp)
2564 if (ifp->if_afdata[AF_INET6] == NULL)
2567 return (IN6_LINKMTU(ifp));
2571 in6_domifdetach(struct ifnet *ifp, void *aux)
2573 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2575 mld_domifdetach(ifp);
2576 scope6_ifdetach(ext->scope6_id);
2577 nd6_ifdetach(ifp, ext->nd_ifinfo);
2578 lltable_free(ext->lltable);
2579 COUNTER_ARRAY_FREE(ext->in6_ifstat,
2580 sizeof(struct in6_ifstat) / sizeof(uint64_t));
2581 free(ext->in6_ifstat, M_IFADDR);
2582 COUNTER_ARRAY_FREE(ext->icmp6_ifstat,
2583 sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
2584 free(ext->icmp6_ifstat, M_IFADDR);
2585 free(ext, M_IFADDR);
2589 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be
2590 * v4 mapped addr or v4 compat addr
2593 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2596 bzero(sin, sizeof(*sin));
2597 sin->sin_len = sizeof(struct sockaddr_in);
2598 sin->sin_family = AF_INET;
2599 sin->sin_port = sin6->sin6_port;
2600 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2603 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2605 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2607 bzero(sin6, sizeof(*sin6));
2608 sin6->sin6_len = sizeof(struct sockaddr_in6);
2609 sin6->sin6_family = AF_INET6;
2610 sin6->sin6_port = sin->sin_port;
2611 sin6->sin6_addr.s6_addr32[0] = 0;
2612 sin6->sin6_addr.s6_addr32[1] = 0;
2613 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2614 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2617 /* Convert sockaddr_in6 into sockaddr_in. */
2619 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2621 struct sockaddr_in *sin_p;
2622 struct sockaddr_in6 sin6;
2625 * Save original sockaddr_in6 addr and convert it
2628 sin6 = *(struct sockaddr_in6 *)nam;
2629 sin_p = (struct sockaddr_in *)nam;
2630 in6_sin6_2_sin(sin_p, &sin6);
2633 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2635 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2637 struct sockaddr_in *sin_p;
2638 struct sockaddr_in6 *sin6_p;
2640 sin6_p = malloc(sizeof *sin6_p, M_SONAME, M_WAITOK);
2641 sin_p = (struct sockaddr_in *)*nam;
2642 in6_sin_2_v4mapsin6(sin_p, sin6_p);
2643 free(*nam, M_SONAME);
2644 *nam = (struct sockaddr *)sin6_p;
2648 * Join/leave the solicited multicast groups for proxy NDP entries.
2651 in6_join_proxy_ndp_mc(struct ifnet *ifp, const struct in6_addr *dst)
2653 struct in6_multi *inm;
2654 struct in6_addr mltaddr;
2655 char ip6buf[INET6_ADDRSTRLEN];
2658 if (in6_solicited_node_maddr(&mltaddr, ifp, dst) != 0)
2659 return; /* error logged in in6_solicited_node_maddr. */
2661 error = in6_joingroup(ifp, &mltaddr, NULL, &inm, 0);
2663 nd6log((LOG_WARNING,
2664 "%s: in6_joingroup failed for %s on %s (errno=%d)\n",
2665 __func__, ip6_sprintf(ip6buf, &mltaddr), if_name(ifp),
2671 in6_leave_proxy_ndp_mc(struct ifnet *ifp, const struct in6_addr *dst)
2673 struct epoch_tracker et;
2674 struct in6_multi *inm;
2675 struct in6_addr mltaddr;
2676 char ip6buf[INET6_ADDRSTRLEN];
2678 if (in6_solicited_node_maddr(&mltaddr, ifp, dst) != 0)
2679 return; /* error logged in in6_solicited_node_maddr. */
2681 NET_EPOCH_ENTER(et);
2682 inm = in6m_lookup(ifp, &mltaddr);
2685 in6_leavegroup(inm, NULL);
2687 nd6log((LOG_WARNING, "%s: in6m_lookup failed for %s on %s\n",
2688 __func__, ip6_sprintf(ip6buf, &mltaddr), if_name(ifp)));
2692 in6_lle_match_pub(struct lltable *llt, struct llentry *lle, void *farg)
2694 return ((lle->la_flags & LLE_PUB) != 0);
2698 in6_purge_proxy_ndp(struct ifnet *ifp)
2700 struct lltable *llt;
2703 if (ifp->if_afdata[AF_INET6] == NULL)
2706 llt = LLTABLE6(ifp);
2707 IF_AFDATA_WLOCK(ifp);
2708 need_purge = ((llt->llt_flags & LLT_ADDEDPROXY) != 0);
2709 IF_AFDATA_WUNLOCK(ifp);
2712 * Ever added proxy ndp entries, leave solicited node multicast
2713 * before deleting the llentry.
2716 lltable_delete_conditional(llt, in6_lle_match_pub, NULL);