160894Smsmith/*- 260894Smsmith * SPDX-License-Identifier: BSD-3-Clause 360894Smsmith * 460894Smsmith * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 560894Smsmith * All rights reserved. 660894Smsmith * 760894Smsmith * Redistribution and use in source and binary forms, with or without 860894Smsmith * modification, are permitted provided that the following conditions 960894Smsmith * are met: 1060894Smsmith * 1. Redistributions of source code must retain the above copyright 1160894Smsmith * notice, this list of conditions and the following disclaimer. 1260894Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1360894Smsmith * notice, this list of conditions and the following disclaimer in the 1460894Smsmith * documentation and/or other materials provided with the distribution. 1560894Smsmith * 3. Neither the name of the project nor the names of its contributors 1660894Smsmith * may be used to endorse or promote products derived from this software 1760894Smsmith * without specific prior written permission. 1860894Smsmith * 1960894Smsmith * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2060894Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2160894Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2260894Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2360894Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2460894Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2560894Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2660894Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2760894Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2860894Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2960894Smsmith * SUCH DAMAGE. 3060894Smsmith * 3160894Smsmith * $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $ 3260894Smsmith */ 3360894Smsmith 3467555Smsmith/*- 3560894Smsmith * Copyright (c) 1982, 1986, 1991, 1993 3667555Smsmith * The Regents of the University of California. All rights reserved. 3760894Smsmith * 3867555Smsmith * Redistribution and use in source and binary forms, with or without 3967555Smsmith * modification, are permitted provided that the following conditions 4060894Smsmith * are met: 4160894Smsmith * 1. Redistributions of source code must retain the above copyright 4260894Smsmith * notice, this list of conditions and the following disclaimer. 4360894Smsmith * 2. Redistributions in binary form must reproduce the above copyright 4467555Smsmith * notice, this list of conditions and the following disclaimer in the 4567555Smsmith * documentation and/or other materials provided with the distribution. 4667555Smsmith * 3. Neither the name of the University nor the names of its contributors 4767555Smsmith * may be used to endorse or promote products derived from this software 4860894Smsmith * without specific prior written permission. 4991449Speter * 5067555Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5191449Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5291449Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5367555Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5467555Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5591449Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5667555Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5767555Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5867555Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5967555Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 6067555Smsmith * SUCH DAMAGE. 6167555Smsmith */ 6267555Smsmith 63118508Sps#include <sys/cdefs.h> 64118508Sps#include "opt_inet.h" 6560894Smsmith#include "opt_inet6.h" 6660894Smsmith 6760894Smsmith#include <sys/param.h> 6860894Smsmith#include <sys/eventhandler.h> 6967555Smsmith#include <sys/errno.h> 7067555Smsmith#include <sys/jail.h> 7167555Smsmith#include <sys/malloc.h> 7267555Smsmith#include <sys/socket.h> 7367555Smsmith#include <sys/socketvar.h> 7467555Smsmith#include <sys/sockio.h> 7567555Smsmith#include <sys/systm.h> 7660894Smsmith#include <sys/priv.h> 7760894Smsmith#include <sys/proc.h> 7860894Smsmith#include <sys/time.h> 7960894Smsmith#include <sys/kernel.h> 8067555Smsmith#include <sys/lock.h> 8167555Smsmith#include <sys/rmlock.h> 8267555Smsmith#include <sys/sysctl.h> 8360894Smsmith#include <sys/syslog.h> 8460894Smsmith 8560894Smsmith#include <net/if.h> 8660894Smsmith#include <net/if_var.h> 8767555Smsmith#include <net/if_private.h> 8867555Smsmith#include <net/if_types.h> 8967555Smsmith#include <net/route.h> 9067555Smsmith#include <net/route/route_ctl.h> 9167555Smsmith#include <net/route/nhop.h> 9267555Smsmith#include <net/if_dl.h> 9360894Smsmith#include <net/vnet.h> 9460894Smsmith 9560894Smsmith#include <netinet/in.h> 9660894Smsmith#include <netinet/in_var.h> 9767555Smsmith#include <net/if_llatbl.h> 9867555Smsmith#include <netinet/if_ether.h> 9960894Smsmith#include <netinet/in_systm.h> 10060894Smsmith#include <netinet/ip.h> 10160894Smsmith#include <netinet/in_pcb.h> 10260894Smsmith#include <netinet/ip_carp.h> 10367555Smsmith#include <netinet/icmp6.h> 10469543Smsmith 10567555Smsmith#include <netinet/ip6.h> 10660894Smsmith#include <netinet6/ip6_var.h> 10760894Smsmith#include <netinet6/nd6.h> 10860894Smsmith#include <netinet6/mld6_var.h> 10960894Smsmith#include <netinet6/ip6_mroute.h> 11060894Smsmith#include <netinet6/in6_ifattach.h> 11160894Smsmith#include <netinet6/scope6_var.h> 11260894Smsmith#include <netinet6/in6_fib.h> 11360894Smsmith#include <netinet6/in6_pcb.h> 11467555Smsmith 11560894Smsmith#ifdef MAC 11667555Smsmith#include <security/mac/mac_framework.h> 11767555Smsmith#endif 11860894Smsmith 11960894Smsmith/* 12091790Smsmith * struct in6_ifreq and struct ifreq must be type punnable for common members 12167555Smsmith * of ifr_ifru to allow accessors to be shared. 12260894Smsmith */ 12360894Smsmith_Static_assert(offsetof(struct in6_ifreq, ifr_ifru) == 12460894Smsmith offsetof(struct ifreq, ifr_ifru), 12560894Smsmith "struct in6_ifreq and struct ifreq are not type punnable"); 12667555Smsmith 12760894SmsmithVNET_DEFINE_STATIC(int, icmp6_nodeinfo_oldmcprefix) = 1; 12867555Smsmith#define V_icmp6_nodeinfo_oldmcprefix VNET(icmp6_nodeinfo_oldmcprefix) 12967555SmsmithSYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_NODEINFO_OLDMCPREFIX, 13067555Smsmith nodeinfo_oldmcprefix, CTLFLAG_VNET | CTLFLAG_RW, 13167555Smsmith &VNET_NAME(icmp6_nodeinfo_oldmcprefix), 0, 13267555Smsmith "Join old IPv6 NI group address in draft-ietf-ipngwg-icmp-name-lookup " 13367555Smsmith "for compatibility with KAME implementation"); 13460894Smsmith 13560894SmsmithVNET_DEFINE_STATIC(int, nd6_useloopback) = 1; 13667555Smsmith#define V_nd6_useloopback VNET(nd6_useloopback) 13760894SmsmithSYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_USELOOPBACK, nd6_useloopback, 13867555Smsmith CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_useloopback), 0, 13967555Smsmith "Create a loopback route when configuring an IPv6 address"); 14067555Smsmith 14167555Smsmith/* 14267555Smsmith * Definitions of some costant IP6 addresses. 14367555Smsmith */ 14467555Smsmithconst struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 14567555Smsmithconst struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 14667555Smsmithconst struct in6_addr in6addr_nodelocal_allnodes = 14760894Smsmith IN6ADDR_NODELOCAL_ALLNODES_INIT; 14867555Smsmithconst struct in6_addr in6addr_linklocal_allnodes = 14967555Smsmith IN6ADDR_LINKLOCAL_ALLNODES_INIT; 15067555Smsmithconst struct in6_addr in6addr_linklocal_allrouters = 15167555Smsmith IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 15260894Smsmithconst struct in6_addr in6addr_linklocal_allv2routers = 15360894Smsmith IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT; 15460894Smsmith 15591790Smsmithconst struct in6_addr in6mask0 = IN6MASK0; 15691790Smsmithconst struct in6_addr in6mask32 = IN6MASK32; 15791790Smsmithconst struct in6_addr in6mask64 = IN6MASK64; 15891790Smsmithconst struct in6_addr in6mask96 = IN6MASK96; 15991790Smsmithconst struct in6_addr in6mask128 = IN6MASK128; 16091790Smsmith 16167555Smsmithconst struct sockaddr_in6 sa6_any = 16260894Smsmith { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 }; 16367555Smsmith 16467555Smsmithstatic int in6_notify_ifa(struct ifnet *, struct in6_ifaddr *, 16560894Smsmith struct in6_aliasreq *, int); 16660894Smsmithstatic void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *); 16760894Smsmith 16860894Smsmithstatic int in6_validate_ifra(struct ifnet *, struct in6_aliasreq *, 16967555Smsmith struct in6_ifaddr *, int); 17060894Smsmithstatic struct in6_ifaddr *in6_alloc_ifa(struct ifnet *, 17167555Smsmith struct in6_aliasreq *, int flags); 17260894Smsmithstatic int in6_update_ifa_internal(struct ifnet *, struct in6_aliasreq *, 17360894Smsmith struct in6_ifaddr *, int, int); 17467555Smsmithstatic int in6_broadcast_ifa(struct ifnet *, struct in6_aliasreq *, 17567555Smsmith struct in6_ifaddr *, int); 17660894Smsmith 17767555Smsmithstatic void in6_join_proxy_ndp_mc(struct ifnet *, const struct in6_addr *); 17860894Smsmithstatic void in6_leave_proxy_ndp_mc(struct ifnet *, const struct in6_addr *); 17967555Smsmith 18067555Smsmith#define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa)) 18167555Smsmith#define ia62ifa(ia6) (&((ia6)->ia_ifa)) 18267555Smsmith 18367555Smsmithstatic struct sx in6_control_sx; 18467555SmsmithSX_SYSINIT(in6_control_sx, &in6_control_sx, "in6_control"); 18567555Smsmith 18667555Smsmithvoid 18767555Smsmithin6_newaddrmsg(struct in6_ifaddr *ia, int cmd) 18860894Smsmith{ 18960894Smsmith struct rt_addrinfo info; 19060894Smsmith struct ifaddr *ifa; 19160894Smsmith struct sockaddr_dl gateway; 19260894Smsmith int fibnum; 19360894Smsmith 194118508Sps ifa = &ia->ia_ifa; 195118508Sps 19660894Smsmith /* 19760894Smsmith * Prepare info data for the host route. 198118508Sps * This code mimics one from ifa_maintain_loopback_route(). 19967555Smsmith */ 200118508Sps bzero(&info, sizeof(struct rt_addrinfo)); 20167555Smsmith info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED; 20260894Smsmith info.rti_info[RTAX_DST] = ifa->ifa_addr; 203118508Sps info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gateway; 204118508Sps link_init_sdl(ifa->ifa_ifp, (struct sockaddr *)&gateway, ifa->ifa_ifp->if_type); 20560894Smsmith if (cmd != RTM_DELETE) 20660894Smsmith info.rti_ifp = V_loif; 20767555Smsmith 20860894Smsmith fibnum = ia62ifa(ia)->ifa_ifp->if_fib; 20967555Smsmith 21067555Smsmith if (cmd == RTM_ADD) { 21167555Smsmith rt_addrmsg(cmd, &ia->ia_ifa, fibnum); 21260894Smsmith rt_routemsg_info(cmd, &info, fibnum); 21360894Smsmith } else if (cmd == RTM_DELETE) { 21460894Smsmith rt_routemsg_info(cmd, &info, fibnum); 215118508Sps rt_addrmsg(cmd, &ia->ia_ifa, fibnum); 216118508Sps } 217118508Sps} 218118508Sps 21960894Smsmithint 220118508Spsin6_mask2len(struct in6_addr *mask, u_char *lim0) 22160894Smsmith{ 222118508Sps int x = 0, y; 223118508Sps u_char *lim = lim0, *p; 224118508Sps 225118508Sps /* ignore the scope_id part */ 226118508Sps if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask)) 227118508Sps lim = (u_char *)mask + sizeof(*mask); 228118508Sps for (p = (u_char *)mask; p < lim; x++, p++) { 229118508Sps if (*p != 0xff) 230118508Sps break; 231118508Sps } 232118508Sps y = 0; 233118508Sps if (p < lim) { 234118508Sps for (y = 0; y < 8; y++) { 235118508Sps if ((*p & (0x80 >> y)) == 0) 236118508Sps break; 237118508Sps } 238118508Sps } 239118508Sps 240118508Sps /* 241118508Sps * when the limit pointer is given, do a stricter check on the 242118508Sps * remaining bits. 243118508Sps */ 244118508Sps if (p < lim) { 245118508Sps if (y != 0 && (*p & (0x00ff >> y)) != 0) 246118508Sps return (-1); 247118508Sps for (p = p + 1; p < lim; p++) 248118508Sps if (*p != 0) 249118508Sps return (-1); 250118508Sps } 251118508Sps 252118508Sps return x * 8 + y; 253118508Sps} 254118508Sps 255118508Sps#ifdef COMPAT_FREEBSD32 25667555Smsmithstruct in6_ndifreq32 { 257118508Sps char ifname[IFNAMSIZ]; 258118508Sps uint32_t ifindex; 259118508Sps}; 26060894Smsmith#define SIOCGDEFIFACE32_IN6 _IOWR('i', 86, struct in6_ndifreq32) 261118508Sps#endif 262118508Sps 263118508Spsint 26460894Smsmithin6_control_ioctl(u_long cmd, void *data, 265118508Sps struct ifnet *ifp, struct ucred *cred) 266118508Sps{ 26760894Smsmith struct in6_ifreq *ifr = (struct in6_ifreq *)data; 268118508Sps struct in6_ifaddr *ia = NULL; 269118508Sps struct in6_aliasreq *ifra = (struct in6_aliasreq *)data; 270118508Sps struct sockaddr_in6 *sa6; 271118508Sps int error; 272118508Sps bool control_locked = false; 273118508Sps 274118508Sps /* 275118508Sps * Compat to make pre-10.x ifconfig(8) operable. 276118508Sps */ 277118508Sps if (cmd == OSIOCAIFADDR_IN6) { 278118508Sps cmd = SIOCAIFADDR_IN6; 27960894Smsmith ifra->ifra_vhid = 0; 280118508Sps } 281118508Sps 282118508Sps switch (cmd) { 283118508Sps case SIOCGETSGCNT_IN6: 284118508Sps case SIOCGETMIFCNT_IN6: 285118508Sps /* 28660894Smsmith * XXX mrt_ioctl has a 3rd, unused, FIB argument in route.c. 28760894Smsmith * We cannot see how that would be needed, so do not adjust the 28867555Smsmith * KPI blindly; more likely should clean up the IPv4 variant. 28960894Smsmith */ 29067555Smsmith return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP); 29167555Smsmith } 29267555Smsmith 29367555Smsmith switch (cmd) { 29467555Smsmith case SIOCAADDRCTL_POLICY: 29567555Smsmith case SIOCDADDRCTL_POLICY: 29667555Smsmith if (cred != NULL) { 29760894Smsmith error = priv_check_cred(cred, PRIV_NETINET_ADDRCTRL6); 29860894Smsmith if (error) 29960894Smsmith return (error); 30060894Smsmith } 30160894Smsmith return (in6_src_ioctl(cmd, data)); 30260894Smsmith } 30367555Smsmith 30460894Smsmith if (ifp == NULL) 30560894Smsmith return (EOPNOTSUPP); 30660894Smsmith 30760894Smsmith switch (cmd) { 30860894Smsmith case SIOCSNDFLUSH_IN6: 30967555Smsmith case SIOCSPFXFLUSH_IN6: 31060894Smsmith case SIOCSRTRFLUSH_IN6: 31167555Smsmith case SIOCSDEFIFACE_IN6: 31267555Smsmith case SIOCSIFINFO_FLAGS: 31360894Smsmith case SIOCSIFINFO_IN6: 31460894Smsmith if (cred != NULL) { 31560894Smsmith error = priv_check_cred(cred, PRIV_NETINET_ND6); 31660894Smsmith if (error) 31760894Smsmith return (error); 31860894Smsmith } 31960894Smsmith /* FALLTHROUGH */ 32067555Smsmith case OSIOCGIFINFO_IN6: 32167555Smsmith case SIOCGIFINFO_IN6: 32267555Smsmith case SIOCGNBRINFO_IN6: 32360894Smsmith case SIOCGDEFIFACE_IN6: 32467555Smsmith return (nd6_ioctl(cmd, data, ifp)); 32567555Smsmith 32660894Smsmith#ifdef COMPAT_FREEBSD32 32760894Smsmith case SIOCGDEFIFACE32_IN6: 32860894Smsmith { 32960894Smsmith struct in6_ndifreq ndif; 33060894Smsmith struct in6_ndifreq32 *ndif32; 33160894Smsmith 33267555Smsmith error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif, 33367555Smsmith ifp); 33460894Smsmith if (error) 33560894Smsmith return (error); 33660894Smsmith ndif32 = (struct in6_ndifreq32 *)data; 33760894Smsmith ndif32->ifindex = ndif.ifindex; 33860894Smsmith return (0); 33960894Smsmith } 34060894Smsmith#endif 34160894Smsmith } 34260894Smsmith 34360894Smsmith switch (cmd) { 34460894Smsmith case SIOCSIFPREFIX_IN6: 34560894Smsmith case SIOCDIFPREFIX_IN6: 34660894Smsmith case SIOCAIFPREFIX_IN6: 34760894Smsmith case SIOCCIFPREFIX_IN6: 34860894Smsmith case SIOCSGIFPREFIX_IN6: 34960894Smsmith case SIOCGIFPREFIX_IN6: 35060894Smsmith log(LOG_NOTICE, 35160894Smsmith "prefix ioctls are now invalidated. " 35260894Smsmith "please use ifconfig.\n"); 35360894Smsmith return (EOPNOTSUPP); 35473104Smsmith } 35560894Smsmith 35660894Smsmith switch (cmd) { 35760894Smsmith case SIOCSSCOPE6: 35869543Smsmith if (cred != NULL) { 35969543Smsmith error = priv_check_cred(cred, PRIV_NETINET_SCOPE6); 36069543Smsmith if (error) 36160894Smsmith return (error); 36269543Smsmith } 36369543Smsmith /* FALLTHROUGH */ 36460894Smsmith case SIOCGSCOPE6: 36569543Smsmith case SIOCGSCOPE6DEF: 36669543Smsmith return (scope6_ioctl(cmd, data, ifp)); 36769543Smsmith } 36869543Smsmith 36969543Smsmith /* 37067555Smsmith * Find address for this interface, if it exists. 37160894Smsmith * 37269543Smsmith * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation 37369543Smsmith * only, and used the first interface address as the target of other 37467555Smsmith * operations (without checking ifra_addr). This was because netinet 37569543Smsmith * code/API assumed at most 1 interface address per interface. 37669543Smsmith * Since IPv6 allows a node to assign multiple addresses 37769543Smsmith * on a single interface, we almost always look and check the 37869543Smsmith * presence of ifra_addr, and reject invalid ones here. 37969543Smsmith * It also decreases duplicated code among SIOC*_IN6 operations. 38069543Smsmith */ 38169543Smsmith switch (cmd) { 38269543Smsmith case SIOCAIFADDR_IN6: 38369543Smsmith case SIOCSIFPHYADDR_IN6: 38469543Smsmith sa6 = &ifra->ifra_addr; 38569543Smsmith break; 38669543Smsmith case SIOCSIFADDR_IN6: 38769543Smsmith case SIOCGIFADDR_IN6: 38869543Smsmith case SIOCSIFDSTADDR_IN6: 38969543Smsmith case SIOCSIFNETMASK_IN6: 39069543Smsmith case SIOCGIFDSTADDR_IN6: 39169543Smsmith case SIOCGIFNETMASK_IN6: 39269543Smsmith case SIOCDIFADDR_IN6: 39369543Smsmith case SIOCGIFPSRCADDR_IN6: 39469543Smsmith case SIOCGIFPDSTADDR_IN6: 39569543Smsmith case SIOCGIFAFLAG_IN6: 39669543Smsmith case SIOCSNDFLUSH_IN6: 39769543Smsmith case SIOCSPFXFLUSH_IN6: 39869543Smsmith case SIOCSRTRFLUSH_IN6: 39969543Smsmith case SIOCGIFALIFETIME_IN6: 40069543Smsmith case SIOCGIFSTAT_IN6: 40169543Smsmith case SIOCGIFSTAT_ICMP6: 40269543Smsmith sa6 = &ifr->ifr_addr; 40369543Smsmith break; 40469543Smsmith case SIOCSIFADDR: 40569543Smsmith case SIOCSIFBRDADDR: 40669543Smsmith case SIOCSIFDSTADDR: 40769543Smsmith case SIOCSIFNETMASK: 40869543Smsmith /* 40969543Smsmith * Although we should pass any non-INET6 ioctl requests 41069543Smsmith * down to driver, we filter some legacy INET requests. 41169543Smsmith * Drivers trust SIOCSIFADDR et al to come from an already 41269543Smsmith * privileged layer, and do not perform any credentials 41369543Smsmith * checks or input validation. 41469543Smsmith */ 41569543Smsmith return (EINVAL); 41669543Smsmith default: 41769543Smsmith sa6 = NULL; 41869543Smsmith break; 41969543Smsmith } 42069543Smsmith if (sa6 && sa6->sin6_family == AF_INET6) { 42169543Smsmith if (sa6->sin6_scope_id != 0) 42269543Smsmith error = sa6_embedscope(sa6, 0); 42369543Smsmith else 42469543Smsmith error = in6_setscope(&sa6->sin6_addr, ifp, NULL); 42569543Smsmith if (error != 0) 42669543Smsmith return (error); 42769543Smsmith if (cred != NULL && (error = prison_check_ip6(cred, 42869543Smsmith &sa6->sin6_addr)) != 0) 42969543Smsmith return (error); 43069543Smsmith sx_xlock(&in6_control_sx); 43160894Smsmith control_locked = true; 43260894Smsmith ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr); 43360894Smsmith } else 43469543Smsmith ia = NULL; 43569543Smsmith 43669543Smsmith switch (cmd) { 43769543Smsmith case SIOCSIFADDR_IN6: 43869543Smsmith case SIOCSIFDSTADDR_IN6: 43969543Smsmith case SIOCSIFNETMASK_IN6: 44069543Smsmith /* 44169543Smsmith * Since IPv6 allows a node to assign multiple addresses 44269543Smsmith * on a single interface, SIOCSIFxxx ioctls are deprecated. 44369543Smsmith */ 44469543Smsmith /* we decided to obsolete this command (20000704) */ 44569543Smsmith error = EINVAL; 44669543Smsmith goto out; 44769543Smsmith 44869543Smsmith case SIOCDIFADDR_IN6: 44969543Smsmith /* 45069543Smsmith * for IPv4, we look for existing in_ifaddr here to allow 45169543Smsmith * "ifconfig if0 delete" to remove the first IPv4 address on 45269543Smsmith * the interface. For IPv6, as the spec allows multiple 45369543Smsmith * interface address from the day one, we consider "remove the 45469543Smsmith * first one" semantics to be not preferable. 45569543Smsmith */ 45669543Smsmith if (ia == NULL) { 45769543Smsmith error = EADDRNOTAVAIL; 45869543Smsmith goto out; 45969543Smsmith } 46069543Smsmith /* FALLTHROUGH */ 46169543Smsmith case SIOCAIFADDR_IN6: 46269543Smsmith /* 46369543Smsmith * We always require users to specify a valid IPv6 address for 46469543Smsmith * the corresponding operation. 46569543Smsmith */ 46669543Smsmith if (ifra->ifra_addr.sin6_family != AF_INET6 || 46769543Smsmith ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) { 46867555Smsmith error = EAFNOSUPPORT; 46960894Smsmith goto out; 47067555Smsmith } 47167555Smsmith 47260894Smsmith if (cred != NULL) { 47367555Smsmith error = priv_check_cred(cred, (cmd == SIOCDIFADDR_IN6) ? 47467555Smsmith PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR); 475118508Sps if (error) 47667555Smsmith goto out; 47767555Smsmith } 47867555Smsmith /* FALLTHROUGH */ 47967555Smsmith case SIOCGIFSTAT_IN6: 48067555Smsmith case SIOCGIFSTAT_ICMP6: 48169543Smsmith if (ifp->if_afdata[AF_INET6] == NULL) { 48267555Smsmith error = EPFNOSUPPORT; 48360894Smsmith goto out; 48467555Smsmith } 48567555Smsmith break; 48667555Smsmith 48767555Smsmith case SIOCGIFADDR_IN6: 48867555Smsmith /* This interface is basically deprecated. use SIOCGIFCONF. */ 489118508Sps /* FALLTHROUGH */ 490118508Sps case SIOCGIFAFLAG_IN6: 49167555Smsmith case SIOCGIFNETMASK_IN6: 49269543Smsmith case SIOCGIFDSTADDR_IN6: 49369543Smsmith case SIOCGIFALIFETIME_IN6: 49469543Smsmith /* must think again about its semantics */ 49569543Smsmith if (ia == NULL) { 49669543Smsmith error = EADDRNOTAVAIL; 49767555Smsmith goto out; 49869543Smsmith } 49967555Smsmith break; 500118508Sps } 501118508Sps 502118508Sps switch (cmd) { 503118508Sps case SIOCGIFADDR_IN6: 504118508Sps ifr->ifr_addr = ia->ia_addr; 50567555Smsmith if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0) 506111119Simp goto out; 50767555Smsmith break; 50867555Smsmith 50967555Smsmith case SIOCGIFDSTADDR_IN6: 510118508Sps if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 51167555Smsmith error = EINVAL; 51267555Smsmith goto out; 51367555Smsmith } 51467555Smsmith ifr->ifr_dstaddr = ia->ia_dstaddr; 51567555Smsmith if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0) 51669543Smsmith goto out; 51767555Smsmith break; 51867555Smsmith 51967555Smsmith case SIOCGIFNETMASK_IN6: 52067555Smsmith ifr->ifr_addr = ia->ia_prefixmask; 52167555Smsmith break; 52267555Smsmith 52367555Smsmith case SIOCGIFAFLAG_IN6: 524118508Sps ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags; 52567555Smsmith break; 52667555Smsmith 52767555Smsmith case SIOCGIFSTAT_IN6: 52867555Smsmith COUNTER_ARRAY_COPY(((struct in6_ifextra *) 52967555Smsmith ifp->if_afdata[AF_INET6])->in6_ifstat, 53067555Smsmith &ifr->ifr_ifru.ifru_stat, 53167555Smsmith sizeof(struct in6_ifstat) / sizeof(uint64_t)); 53267555Smsmith break; 53367555Smsmith 53467555Smsmith case SIOCGIFSTAT_ICMP6: 53567555Smsmith COUNTER_ARRAY_COPY(((struct in6_ifextra *) 53667555Smsmith ifp->if_afdata[AF_INET6])->icmp6_ifstat, 53767555Smsmith &ifr->ifr_ifru.ifru_icmp6stat, 53867555Smsmith sizeof(struct icmp6_ifstat) / sizeof(uint64_t)); 53967555Smsmith break; 54067555Smsmith 54167555Smsmith case SIOCGIFALIFETIME_IN6: 54267555Smsmith ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime; 54367555Smsmith if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 54467555Smsmith time_t maxexpire; 54567555Smsmith struct in6_addrlifetime *retlt = 54667555Smsmith &ifr->ifr_ifru.ifru_lifetime; 54767555Smsmith 54867555Smsmith /* 54967555Smsmith * XXX: adjust expiration time assuming time_t is 55067555Smsmith * signed. 55167555Smsmith */ 55267555Smsmith maxexpire = (-1) & 55367555Smsmith ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 55467555Smsmith if (ia->ia6_lifetime.ia6t_vltime < 55567555Smsmith maxexpire - ia->ia6_updatetime) { 55667555Smsmith retlt->ia6t_expire = ia->ia6_updatetime + 55767555Smsmith ia->ia6_lifetime.ia6t_vltime; 55867555Smsmith } else 55967555Smsmith retlt->ia6t_expire = maxexpire; 56067555Smsmith } 561118508Sps if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 56267555Smsmith time_t maxexpire; 56367555Smsmith struct in6_addrlifetime *retlt = 56467555Smsmith &ifr->ifr_ifru.ifru_lifetime; 56567555Smsmith 56667555Smsmith /* 56767555Smsmith * XXX: adjust expiration time assuming time_t is 56867555Smsmith * signed. 56967555Smsmith */ 57067555Smsmith maxexpire = (-1) & 57167555Smsmith ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 57267555Smsmith if (ia->ia6_lifetime.ia6t_pltime < 57367555Smsmith maxexpire - ia->ia6_updatetime) { 57467555Smsmith retlt->ia6t_preferred = ia->ia6_updatetime + 57567555Smsmith ia->ia6_lifetime.ia6t_pltime; 57667555Smsmith } else 57767555Smsmith retlt->ia6t_preferred = maxexpire; 57867555Smsmith } 57967555Smsmith break; 58067555Smsmith 58167555Smsmith case SIOCAIFADDR_IN6: 58267555Smsmith#ifdef MAC 58367555Smsmith /* Check if a MAC policy disallows setting the IPv6 address. */ 58467555Smsmith error = mac_inet6_check_add_addr(cred, &sa6->sin6_addr, ifp); 58567555Smsmith if (error != 0) 58667555Smsmith goto out; 587111119Simp#endif 58867555Smsmith error = in6_addifaddr(ifp, ifra, ia); 58967555Smsmith ia = NULL; 59067555Smsmith break; 59167555Smsmith 59267555Smsmith case SIOCDIFADDR_IN6: 59367555Smsmith in6_purgeifaddr(ia); 59467555Smsmith EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa, 59567555Smsmith IFADDR_EVENT_DEL); 59667555Smsmith break; 59767555Smsmith 59867555Smsmith default: 59967555Smsmith if (ifp->if_ioctl == NULL) { 60067555Smsmith error = EOPNOTSUPP; 601118508Sps goto out; 602118508Sps } 603118508Sps error = (*ifp->if_ioctl)(ifp, cmd, data); 604118508Sps goto out; 605118508Sps } 606118508Sps 607118508Sps error = 0; 608118508Spsout: 60991790Smsmith if (control_locked) 61091790Smsmith sx_xunlock(&in6_control_sx); 61167555Smsmith 61267555Smsmith if (ia != NULL) 61367555Smsmith ifa_free(&ia->ia_ifa); 61467555Smsmith return (error); 61567555Smsmith} 61667555Smsmith 61760894Smsmithint 61860894Smsmithin6_control(struct socket *so, u_long cmd, void *data, 61960894Smsmith struct ifnet *ifp, struct thread *td) 62067555Smsmith{ 62160894Smsmith return (in6_control_ioctl(cmd, data, ifp, td ? td->td_ucred : NULL)); 62267555Smsmith} 62367555Smsmith 62460894Smsmithstatic struct in6_multi_mship * 62567555Smsmithin6_joingroup_legacy(struct ifnet *ifp, const struct in6_addr *mcaddr, 62667555Smsmith int *errorp, int delay) 62767555Smsmith{ 62867555Smsmith struct in6_multi_mship *imm; 62967555Smsmith int error; 63060894Smsmith 63160894Smsmith imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT); 63260894Smsmith if (imm == NULL) { 63367555Smsmith *errorp = ENOBUFS; 63467555Smsmith return (NULL); 63567555Smsmith } 63667555Smsmith 63767555Smsmith delay = (delay * MLD_FASTHZ) / hz; 63867555Smsmith 63967555Smsmith error = in6_joingroup(ifp, mcaddr, NULL, &imm->i6mm_maddr, delay); 64067555Smsmith if (error) { 64167555Smsmith *errorp = error; 64267555Smsmith free(imm, M_IP6MADDR); 64360894Smsmith return (NULL); 64460894Smsmith } 64560894Smsmith 64660894Smsmith return (imm); 64760894Smsmith} 64867555Smsmith 64967555Smsmithstatic int 65060894Smsmithin6_solicited_node_maddr(struct in6_addr *maddr, 65167555Smsmith struct ifnet *ifp, const struct in6_addr *base) 65267555Smsmith{ 65360894Smsmith int error; 65467555Smsmith 65560894Smsmith bzero(maddr, sizeof(struct in6_addr)); 65667555Smsmith maddr->s6_addr32[0] = IPV6_ADDR_INT32_MLL; 65767555Smsmith maddr->s6_addr32[2] = htonl(1); 65867555Smsmith maddr->s6_addr32[3] = base->s6_addr32[3]; 65967555Smsmith maddr->s6_addr8[12] = 0xff; 66067555Smsmith if ((error = in6_setscope(maddr, ifp, NULL)) != 0) { 66167555Smsmith /* XXX: should not happen */ 66260894Smsmith log(LOG_ERR, "%s: in6_setscope failed\n", __func__); 66367555Smsmith } 66467555Smsmith 66567555Smsmith return error; 66667555Smsmith} 66767555Smsmith 66867555Smsmith/* 66967555Smsmith * Join necessary multicast groups. Factored out from in6_update_ifa(). 67067555Smsmith * This entire work should only be done once, for the default FIB. 67167555Smsmith */ 67260894Smsmithstatic int 67360894Smsmithin6_update_ifa_join_mc(struct ifnet *ifp, struct in6_aliasreq *ifra, 67460894Smsmith struct in6_ifaddr *ia, int flags, struct in6_multi **in6m_sol) 67567555Smsmith{ 67667555Smsmith char ip6buf[INET6_ADDRSTRLEN]; 67767555Smsmith struct in6_addr mltaddr; 67867555Smsmith struct in6_multi_mship *imm; 67967555Smsmith int delay, error; 68067555Smsmith 68167555Smsmith KASSERT(in6m_sol != NULL, ("%s: in6m_sol is NULL", __func__)); 68267555Smsmith 68367555Smsmith /* Join solicited multicast addr for new host id. */ 68467555Smsmith if ((error = in6_solicited_node_maddr(&mltaddr, ifp, 68567555Smsmith &ifra->ifra_addr.sin6_addr)) != 0) 68667555Smsmith goto cleanup; 68760894Smsmith delay = error = 0; 68860894Smsmith if ((flags & IN6_IFAUPDATE_DADDELAY)) { 68960894Smsmith /* 69060894Smsmith * We need a random delay for DAD on the address being 69160894Smsmith * configured. It also means delaying transmission of the 69260894Smsmith * corresponding MLD report to avoid report collision. 69360894Smsmith * [RFC 4861, Section 6.3.7] 69467555Smsmith */ 69567555Smsmith delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz); 69660894Smsmith } 69760894Smsmith imm = in6_joingroup_legacy(ifp, &mltaddr, &error, delay); 69860894Smsmith if (imm == NULL) { 69960894Smsmith nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s " 70060894Smsmith "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &mltaddr), 70160894Smsmith if_name(ifp), error)); 70260894Smsmith goto cleanup; 70360894Smsmith } 70460894Smsmith LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 70560894Smsmith *in6m_sol = imm->i6mm_maddr; 70660894Smsmith 70760894Smsmith /* 70867555Smsmith * Join link-local all-nodes address. 70960894Smsmith */ 71060894Smsmith mltaddr = in6addr_linklocal_allnodes; 71160894Smsmith if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0) 71260894Smsmith goto cleanup; /* XXX: should not fail */ 71360894Smsmith 71460894Smsmith imm = in6_joingroup_legacy(ifp, &mltaddr, &error, 0); 71560894Smsmith if (imm == NULL) { 71660894Smsmith nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s " 71760894Smsmith "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &mltaddr), 71860894Smsmith if_name(ifp), error)); 71960894Smsmith goto cleanup; 72067555Smsmith } 72167555Smsmith LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 72267555Smsmith 72367555Smsmith /* 72460894Smsmith * Join node information group address. 72560894Smsmith */ 72660894Smsmith delay = 0; 72760894Smsmith if ((flags & IN6_IFAUPDATE_DADDELAY)) { 72860894Smsmith /* 72960894Smsmith * The spec does not say anything about delay for this group, 73067555Smsmith * but the same logic should apply. 73167555Smsmith */ 73260894Smsmith delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz); 73360894Smsmith } 73460894Smsmith if (in6_nigroup(ifp, NULL, -1, &mltaddr) == 0) { 73560894Smsmith /* XXX jinmei */ 73660894Smsmith imm = in6_joingroup_legacy(ifp, &mltaddr, &error, delay); 73760894Smsmith if (imm == NULL) 73869543Smsmith nd6log((LOG_WARNING, 73960894Smsmith "%s: in6_joingroup failed for %s on %s " 74060894Smsmith "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, 74167555Smsmith &mltaddr), if_name(ifp), error)); 74267555Smsmith /* XXX not very fatal, go on... */ 74360894Smsmith else 74460894Smsmith LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 74560894Smsmith } 74660894Smsmith if (V_icmp6_nodeinfo_oldmcprefix && 74760894Smsmith in6_nigroup_oldmcprefix(ifp, NULL, -1, &mltaddr) == 0) { 74860894Smsmith imm = in6_joingroup_legacy(ifp, &mltaddr, &error, delay); 74960894Smsmith if (imm == NULL) 75060894Smsmith nd6log((LOG_WARNING, 75160894Smsmith "%s: in6_joingroup failed for %s on %s " 75260894Smsmith "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, 75360894Smsmith &mltaddr), if_name(ifp), error)); 75460894Smsmith /* XXX not very fatal, go on... */ 75560894Smsmith else 75660894Smsmith LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 75760894Smsmith } 75860894Smsmith 75960894Smsmith /* 76060894Smsmith * Join interface-local all-nodes address. 76167555Smsmith * (ff01::1%ifN, and ff01::%ifN/32) 76267555Smsmith */ 76391449Speter mltaddr = in6addr_nodelocal_allnodes; 76467555Smsmith if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0) 76567555Smsmith goto cleanup; /* XXX: should not fail */ 76667555Smsmith 76767555Smsmith imm = in6_joingroup_legacy(ifp, &mltaddr, &error, 0); 76867555Smsmith if (imm == NULL) { 76991449Speter nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s " 77067555Smsmith "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, 77191449Speter &mltaddr), if_name(ifp), error)); 77267555Smsmith goto cleanup; 77367555Smsmith } 77467555Smsmith LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 77567555Smsmith 77667555Smsmithcleanup: 77767555Smsmith return (error); 77867555Smsmith} 77967555Smsmith 78067555Smsmith/* 78167555Smsmith * Update parameters of an IPv6 interface address. 78267555Smsmith * If necessary, a new entry is created and linked into address chains. 78391449Speter * This function is separated from in6_control(). 78467555Smsmith */ 78567555Smsmithint 78667555Smsmithin6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, 78767555Smsmith struct in6_ifaddr *ia, int flags) 78867555Smsmith{ 78967555Smsmith int error, hostIsNew = 0; 79067555Smsmith 79167555Smsmith if ((error = in6_validate_ifra(ifp, ifra, ia, flags)) != 0) 79267555Smsmith return (error); 79367555Smsmith 79467555Smsmith if (ia == NULL) { 79567555Smsmith hostIsNew = 1; 79667555Smsmith if ((ia = in6_alloc_ifa(ifp, ifra, flags)) == NULL) 79767555Smsmith return (ENOBUFS); 79867555Smsmith } 79967555Smsmith 80067555Smsmith error = in6_update_ifa_internal(ifp, ifra, ia, hostIsNew, flags); 80167555Smsmith if (error != 0) { 80267555Smsmith if (hostIsNew != 0) { 80367555Smsmith in6_unlink_ifa(ia, ifp); 80467555Smsmith ifa_free(&ia->ia_ifa); 80567555Smsmith } 80667555Smsmith return (error); 80767555Smsmith } 80867555Smsmith 80967555Smsmith if (hostIsNew) 81067555Smsmith error = in6_broadcast_ifa(ifp, ifra, ia, flags); 81167555Smsmith 81267555Smsmith return (error); 81367555Smsmith} 81467555Smsmith 81567555Smsmith/* 81667555Smsmith * Fill in basic IPv6 address request info. 81767555Smsmith */ 81867555Smsmithvoid 81967555Smsmithin6_prepare_ifra(struct in6_aliasreq *ifra, const struct in6_addr *addr, 82067555Smsmith const struct in6_addr *mask) 82167555Smsmith{ 82267555Smsmith 82367555Smsmith memset(ifra, 0, sizeof(struct in6_aliasreq)); 82467555Smsmith 82567555Smsmith ifra->ifra_addr.sin6_family = AF_INET6; 82667555Smsmith ifra->ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 82767555Smsmith if (addr != NULL) 82867555Smsmith ifra->ifra_addr.sin6_addr = *addr; 82967555Smsmith 83067555Smsmith ifra->ifra_prefixmask.sin6_family = AF_INET6; 83167555Smsmith ifra->ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 83269543Smsmith if (mask != NULL) 83367555Smsmith ifra->ifra_prefixmask.sin6_addr = *mask; 83467555Smsmith} 83567555Smsmith 83667555Smsmithstatic int 83767555Smsmithin6_validate_ifra(struct ifnet *ifp, struct in6_aliasreq *ifra, 83867555Smsmith struct in6_ifaddr *ia, int flags) 83967555Smsmith{ 84067555Smsmith int plen = -1; 84167555Smsmith struct sockaddr_in6 dst6; 84267555Smsmith struct in6_addrlifetime *lt; 84367555Smsmith char ip6buf[INET6_ADDRSTRLEN]; 84467555Smsmith 84560894Smsmith /* Validate parameters */ 84660894Smsmith if (ifp == NULL || ifra == NULL) /* this maybe redundant */ 84760894Smsmith return (EINVAL); 84860894Smsmith 84960894Smsmith /* 85067555Smsmith * The destination address for a p2p link must have a family 85160894Smsmith * of AF_UNSPEC or AF_INET6. 85260894Smsmith */ 85360894Smsmith if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 85460894Smsmith ifra->ifra_dstaddr.sin6_family != AF_INET6 && 85560894Smsmith ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) 85660894Smsmith return (EAFNOSUPPORT); 85760894Smsmith 85860894Smsmith /* 85967555Smsmith * Validate address 860102291Sarchie */ 86160894Smsmith if (ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6) || 86260894Smsmith ifra->ifra_addr.sin6_family != AF_INET6) 86360894Smsmith return (EINVAL); 86467555Smsmith 86567555Smsmith /* 86667555Smsmith * validate ifra_prefixmask. don't check sin6_family, netmask 86767555Smsmith * does not carry fields other than sin6_len. 86867555Smsmith */ 86960894Smsmith if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6)) 87060894Smsmith return (EINVAL); 87160894Smsmith /* 87260894Smsmith * Because the IPv6 address architecture is classless, we require 87360894Smsmith * users to specify a (non 0) prefix length (mask) for a new address. 87460894Smsmith * We also require the prefix (when specified) mask is valid, and thus 87560894Smsmith * reject a non-consecutive mask. 87660894Smsmith */ 87760894Smsmith if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0) 87860894Smsmith return (EINVAL); 87967555Smsmith if (ifra->ifra_prefixmask.sin6_len != 0) { 88067555Smsmith plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 88160894Smsmith (u_char *)&ifra->ifra_prefixmask + 88260894Smsmith ifra->ifra_prefixmask.sin6_len); 88360894Smsmith if (plen <= 0) 88460894Smsmith return (EINVAL); 88560894Smsmith } else { 88660894Smsmith /* 88760894Smsmith * In this case, ia must not be NULL. We just use its prefix 88860894Smsmith * length. 88960894Smsmith */ 89060894Smsmith plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 89160894Smsmith } 89267555Smsmith /* 89360894Smsmith * If the destination address on a p2p interface is specified, 89460894Smsmith * and the address is a scoped one, validate/set the scope 89560894Smsmith * zone identifier. 89667555Smsmith */ 89767555Smsmith dst6 = ifra->ifra_dstaddr; 89867555Smsmith if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 && 89967555Smsmith (dst6.sin6_family == AF_INET6)) { 90060894Smsmith struct in6_addr in6_tmp; 90167555Smsmith u_int32_t zoneid; 90267555Smsmith 90360894Smsmith in6_tmp = dst6.sin6_addr; 90467555Smsmith if (in6_setscope(&in6_tmp, ifp, &zoneid)) 90567555Smsmith return (EINVAL); /* XXX: should be impossible */ 90660894Smsmith 90760894Smsmith if (dst6.sin6_scope_id != 0) { 90860894Smsmith if (dst6.sin6_scope_id != zoneid) 90960894Smsmith return (EINVAL); 91060894Smsmith } else /* user omit to specify the ID. */ 91160894Smsmith dst6.sin6_scope_id = zoneid; 91260894Smsmith 91360894Smsmith /* convert into the internal form */ 91460894Smsmith if (sa6_embedscope(&dst6, 0)) 91560894Smsmith return (EINVAL); /* XXX: should be impossible */ 91660894Smsmith } 91760894Smsmith /* Modify original ifra_dstaddr to reflect changes */ 91860894Smsmith ifra->ifra_dstaddr = dst6; 91960894Smsmith 92060894Smsmith /* 92160894Smsmith * The destination address can be specified only for a p2p or a 92260894Smsmith * loopback interface. If specified, the corresponding prefix length 92360894Smsmith * must be 128. 92460894Smsmith */ 92560894Smsmith if (ifra->ifra_dstaddr.sin6_family == AF_INET6) { 92660894Smsmith if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) { 92760894Smsmith /* XXX: noisy message */ 92860894Smsmith nd6log((LOG_INFO, "in6_update_ifa: a destination can " 92960894Smsmith "be specified for a p2p or a loopback IF only\n")); 93060894Smsmith return (EINVAL); 93160894Smsmith } 93260894Smsmith if (plen != 128) { 93360894Smsmith nd6log((LOG_INFO, "in6_update_ifa: prefixlen should " 93460894Smsmith "be 128 when dstaddr is specified\n")); 93560894Smsmith return (EINVAL); 93660894Smsmith } 93767555Smsmith } 93860894Smsmith /* lifetime consistency check */ 93960894Smsmith lt = &ifra->ifra_lifetime; 94060894Smsmith if (lt->ia6t_pltime > lt->ia6t_vltime) 94160894Smsmith return (EINVAL); 94267555Smsmith if (lt->ia6t_vltime == 0) { 94369543Smsmith /* 94467555Smsmith * the following log might be noisy, but this is a typical 94567555Smsmith * configuration mistake or a tool's bug. 94669543Smsmith */ 94769543Smsmith nd6log((LOG_INFO, 94860894Smsmith "in6_update_ifa: valid lifetime is 0 for %s\n", 94969543Smsmith ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr))); 95069543Smsmith 95160894Smsmith if (ia == NULL) 95260894Smsmith return (0); /* there's nothing to do */ 95360894Smsmith } 95460894Smsmith 95567555Smsmith /* Check prefix mask */ 95667555Smsmith if (ia != NULL && ifra->ifra_prefixmask.sin6_len != 0) { 95767555Smsmith /* 95867555Smsmith * We prohibit changing the prefix length of an existing 95967555Smsmith * address, because 96067555Smsmith * + such an operation should be rare in IPv6, and 96167555Smsmith * + the operation would confuse prefix management. 96267555Smsmith */ 96367555Smsmith if (ia->ia_prefixmask.sin6_len != 0 && 96491790Smsmith in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) { 96591790Smsmith nd6log((LOG_INFO, "in6_validate_ifa: the prefix length " 96691790Smsmith "of an existing %s address should not be changed\n", 96791790Smsmith ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 96867555Smsmith 96967555Smsmith return (EINVAL); 97067555Smsmith } 97167555Smsmith } 97267555Smsmith 97391790Smsmith return (0); 97467555Smsmith} 97567555Smsmith 97667555Smsmith/* 97767555Smsmith * Allocate a new ifaddr and link it into chains. 97867555Smsmith */ 97967555Smsmithstatic struct in6_ifaddr * 98067555Smsmithin6_alloc_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, int flags) 98167555Smsmith{ 98267555Smsmith struct in6_ifaddr *ia; 98367555Smsmith 98467555Smsmith /* 98567555Smsmith * When in6_alloc_ifa() is called in a process of a received 98667555Smsmith * RA, it is called under an interrupt context. So, we should 98767555Smsmith * call malloc with M_NOWAIT. 98867555Smsmith */ 98967555Smsmith ia = (struct in6_ifaddr *)ifa_alloc(sizeof(*ia), M_NOWAIT); 99067555Smsmith if (ia == NULL) 99167555Smsmith return (NULL); 99267555Smsmith LIST_INIT(&ia->ia6_memberships); 99367555Smsmith /* Initialize the address and masks, and put time stamp */ 99467555Smsmith ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 99567555Smsmith ia->ia_addr.sin6_family = AF_INET6; 99667555Smsmith ia->ia_addr.sin6_len = sizeof(ia->ia_addr); 99767555Smsmith /* XXX: Can we assign ,sin6_addr and skip the rest? */ 99867555Smsmith ia->ia_addr = ifra->ifra_addr; 99967555Smsmith ia->ia6_createtime = time_uptime; 100067555Smsmith if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) { 100167555Smsmith /* 100267555Smsmith * Some functions expect that ifa_dstaddr is not 100367555Smsmith * NULL for p2p interfaces. 100467555Smsmith */ 100567555Smsmith ia->ia_ifa.ifa_dstaddr = 100667555Smsmith (struct sockaddr *)&ia->ia_dstaddr; 100767555Smsmith } else { 100867555Smsmith ia->ia_ifa.ifa_dstaddr = NULL; 100967555Smsmith } 101067555Smsmith 101167555Smsmith /* set prefix mask if any */ 101267555Smsmith ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask; 101367555Smsmith if (ifra->ifra_prefixmask.sin6_len != 0) { 101467555Smsmith ia->ia_prefixmask.sin6_family = AF_INET6; 101567555Smsmith ia->ia_prefixmask.sin6_len = ifra->ifra_prefixmask.sin6_len; 101660894Smsmith ia->ia_prefixmask.sin6_addr = ifra->ifra_prefixmask.sin6_addr; 101760894Smsmith } 101860894Smsmith 101960894Smsmith ia->ia_ifp = ifp; 102060894Smsmith ifa_ref(&ia->ia_ifa); /* if_addrhead */ 102160894Smsmith IF_ADDR_WLOCK(ifp); 102260894Smsmith CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 102360894Smsmith IF_ADDR_WUNLOCK(ifp); 102460894Smsmith 102560894Smsmith ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */ 102660894Smsmith IN6_IFADDR_WLOCK(); 102760894Smsmith CK_STAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link); 102860894Smsmith CK_LIST_INSERT_HEAD(IN6ADDR_HASH(&ia->ia_addr.sin6_addr), ia, ia6_hash); 102960894Smsmith IN6_IFADDR_WUNLOCK(); 103060894Smsmith 103160894Smsmith return (ia); 103260894Smsmith} 103360894Smsmith 103460894Smsmith/* 103560894Smsmith * Update/configure interface address parameters: 103660894Smsmith * 103760894Smsmith * 1) Update lifetime 103867555Smsmith * 2) Update interface metric ad flags 103967555Smsmith * 3) Notify other subsystems 104067555Smsmith */ 104167555Smsmithstatic int 104267555Smsmithin6_update_ifa_internal(struct ifnet *ifp, struct in6_aliasreq *ifra, 104367555Smsmith struct in6_ifaddr *ia, int hostIsNew, int flags) 104460894Smsmith{ 104560894Smsmith int error; 104660894Smsmith 104760894Smsmith /* update timestamp */ 104860894Smsmith ia->ia6_updatetime = time_uptime; 104960894Smsmith 105060894Smsmith /* 105160894Smsmith * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred 105260894Smsmith * to see if the address is deprecated or invalidated, but initialize 105360894Smsmith * these members for applications. 105460894Smsmith */ 105567555Smsmith ia->ia6_lifetime = ifra->ifra_lifetime; 105667555Smsmith if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 105760894Smsmith ia->ia6_lifetime.ia6t_expire = 105867555Smsmith time_uptime + ia->ia6_lifetime.ia6t_vltime; 105967555Smsmith } else 106067555Smsmith ia->ia6_lifetime.ia6t_expire = 0; 106160894Smsmith if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 106267555Smsmith ia->ia6_lifetime.ia6t_preferred = 106360894Smsmith time_uptime + ia->ia6_lifetime.ia6t_pltime; 106467555Smsmith } else 106560894Smsmith ia->ia6_lifetime.ia6t_preferred = 0; 106660894Smsmith 106760894Smsmith /* 106860894Smsmith * backward compatibility - if IN6_IFF_DEPRECATED is set from the 106960894Smsmith * userland, make it deprecated. 107060894Smsmith */ 107160894Smsmith if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) { 107260894Smsmith ia->ia6_lifetime.ia6t_pltime = 0; 107360894Smsmith ia->ia6_lifetime.ia6t_preferred = time_uptime; 107460894Smsmith } 107560894Smsmith 107660894Smsmith /* 107760894Smsmith * configure address flags. 107860894Smsmith */ 107960894Smsmith ia->ia6_flags = ifra->ifra_flags; 108060894Smsmith 108160894Smsmith /* 108260894Smsmith * Make the address tentative before joining multicast addresses, 108360894Smsmith * so that corresponding MLD responses would not have a tentative 108460894Smsmith * source address. 108560894Smsmith */ 108660894Smsmith ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */ 108760894Smsmith 108860894Smsmith /* 108960894Smsmith * DAD should be performed for an new address or addresses on 109060894Smsmith * an interface with ND6_IFF_IFDISABLED. 109160894Smsmith */ 109260894Smsmith if (in6if_do_dad(ifp) && 109360894Smsmith (hostIsNew || (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED))) 109460894Smsmith ia->ia6_flags |= IN6_IFF_TENTATIVE; 109560894Smsmith 109660894Smsmith /* notify other subsystems */ 109760894Smsmith error = in6_notify_ifa(ifp, ia, ifra, hostIsNew); 109860894Smsmith 109960894Smsmith return (error); 110060894Smsmith} 110160894Smsmith 110260894Smsmith/* 110360894Smsmith * Do link-level ifa job: 110460894Smsmith * 1) Add lle entry for added address 110560894Smsmith * 2) Notifies routing socket users about new address 110660894Smsmith * 3) join appropriate multicast group 110767555Smsmith * 4) start DAD if enabled 110869543Smsmith */ 110969543Smsmithstatic int 111069543Smsmithin6_broadcast_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, 111169543Smsmith struct in6_ifaddr *ia, int flags) 111269543Smsmith{ 111369543Smsmith struct in6_multi *in6m_sol; 111469543Smsmith int error = 0; 111569543Smsmith 111660894Smsmith /* Add local address to lltable, if necessary (ex. on p2p link). */ 111760894Smsmith if ((error = nd6_add_ifa_lle(ia)) != 0) { 111860894Smsmith in6_purgeaddr(&ia->ia_ifa); 111960894Smsmith ifa_free(&ia->ia_ifa); 112060894Smsmith return (error); 112160894Smsmith } 112260894Smsmith 112360894Smsmith /* Join necessary multicast groups. */ 112460894Smsmith in6m_sol = NULL; 112560894Smsmith if ((ifp->if_flags & IFF_MULTICAST) != 0) { 112660894Smsmith error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol); 112767555Smsmith if (error != 0) { 112860894Smsmith in6_purgeaddr(&ia->ia_ifa); 112960894Smsmith ifa_free(&ia->ia_ifa); 113060894Smsmith return (error); 113160894Smsmith } 113260894Smsmith } 113360894Smsmith 113460894Smsmith /* Perform DAD, if the address is TENTATIVE. */ 113560894Smsmith if ((ia->ia6_flags & IN6_IFF_TENTATIVE)) { 113660894Smsmith int delay, mindelay, maxdelay; 113760894Smsmith 113860894Smsmith delay = 0; 113967555Smsmith if ((flags & IN6_IFAUPDATE_DADDELAY)) { 114060894Smsmith /* 114160894Smsmith * We need to impose a delay before sending an NS 114260894Smsmith * for DAD. Check if we also needed a delay for the 114360894Smsmith * corresponding MLD message. If we did, the delay 114467555Smsmith * should be larger than the MLD delay (this could be 114560894Smsmith * relaxed a bit, but this simple logic is at least 114667555Smsmith * safe). 114760894Smsmith * XXX: Break data hiding guidelines and look at 114867555Smsmith * state for the solicited multicast group. 114967555Smsmith */ 115060894Smsmith mindelay = 0; 115167555Smsmith if (in6m_sol != NULL && 115267555Smsmith in6m_sol->in6m_state == MLD_REPORTING_MEMBER) { 115367555Smsmith mindelay = in6m_sol->in6m_timer; 115467555Smsmith } 115560894Smsmith maxdelay = MAX_RTR_SOLICITATION_DELAY * hz; 115667555Smsmith if (maxdelay - mindelay == 0) 115767555Smsmith delay = 0; 115867555Smsmith else { 115960894Smsmith delay = 116067555Smsmith (arc4random() % (maxdelay - mindelay)) + 116167555Smsmith mindelay; 116260894Smsmith } 116367555Smsmith } 116460894Smsmith nd6_dad_start((struct ifaddr *)ia, delay); 116560894Smsmith } 116660894Smsmith 116760894Smsmith in6_newaddrmsg(ia, RTM_ADD); 116860894Smsmith ifa_free(&ia->ia_ifa); 116960894Smsmith return (error); 117060894Smsmith} 117160894Smsmith 117260894Smsmith/* 117360894Smsmith * Adds or deletes interface route for p2p ifa. 117460894Smsmith * Returns 0 on success or errno. 117560894Smsmith */ 117660894Smsmithstatic int 117760894Smsmithin6_handle_dstaddr_rtrequest(int cmd, struct in6_ifaddr *ia) 117860894Smsmith{ 117960894Smsmith struct epoch_tracker et; 118060894Smsmith struct ifaddr *ifa = &ia->ia_ifa; 118160894Smsmith int error; 118260894Smsmith 118360894Smsmith /* Prepare gateway */ 118460894Smsmith struct sockaddr_dl_short sdl = { 118560894Smsmith .sdl_family = AF_LINK, 118660894Smsmith .sdl_len = sizeof(struct sockaddr_dl_short), 118760894Smsmith .sdl_type = ifa->ifa_ifp->if_type, 118860894Smsmith .sdl_index = ifa->ifa_ifp->if_index, 118960894Smsmith }; 119060894Smsmith 119160894Smsmith struct sockaddr_in6 dst = { 119260894Smsmith .sin6_family = AF_INET6, 119360894Smsmith .sin6_len = sizeof(struct sockaddr_in6), 119460894Smsmith .sin6_addr = ia->ia_dstaddr.sin6_addr, 119560894Smsmith }; 119660894Smsmith 119760894Smsmith struct rt_addrinfo info = { 119860894Smsmith .rti_ifa = ifa, 119960894Smsmith .rti_ifp = ifa->ifa_ifp, 120060894Smsmith .rti_flags = RTF_PINNED | RTF_HOST, 120160894Smsmith .rti_info = { 120260894Smsmith [RTAX_DST] = (struct sockaddr *)&dst, 120360894Smsmith [RTAX_GATEWAY] = (struct sockaddr *)&sdl, 120460894Smsmith }, 120560894Smsmith }; 120660894Smsmith /* Don't set additional per-gw filters on removal */ 120760894Smsmith 120860894Smsmith NET_EPOCH_ENTER(et); 120960894Smsmith error = rib_handle_ifaddr_info(ifa->ifa_ifp->if_fib, cmd, &info); 121060894Smsmith NET_EPOCH_EXIT(et); 121160894Smsmith 121260894Smsmith return (error); 121360894Smsmith} 121460894Smsmith 121560894Smsmithstatic bool 121667555Smsmithifa_is_p2p(struct in6_ifaddr *ia) 121767555Smsmith{ 121867555Smsmith int plen; 121967555Smsmith 122067555Smsmith plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 122167555Smsmith 122267555Smsmith if ((plen == 128) && (ia->ia_dstaddr.sin6_family == AF_INET6) && 122367555Smsmith !IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &ia->ia_dstaddr.sin6_addr)) 122467555Smsmith return (true); 122567555Smsmith 122667555Smsmith return (false); 122791790Smsmith} 122867683Smsmith 122967555Smsmithint 123067555Smsmithin6_addifaddr(struct ifnet *ifp, struct in6_aliasreq *ifra, struct in6_ifaddr *ia) 123191790Smsmith{ 123267555Smsmith struct nd_prefixctl pr0; 123367555Smsmith struct nd_prefix *pr; 123467555Smsmith int carp_attached = 0; 123567555Smsmith int error; 123667555Smsmith 123767555Smsmith /* 123867555Smsmith * first, make or update the interface address structure, 123967555Smsmith * and link it to the list. 124067555Smsmith */ 124167555Smsmith if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0) 124267555Smsmith goto out; 124367555Smsmith if (ia != NULL) { 124467555Smsmith if (ia->ia_ifa.ifa_carp) 124567555Smsmith (*carp_detach_p)(&ia->ia_ifa, true); 124667555Smsmith ifa_free(&ia->ia_ifa); 124767555Smsmith } 124867555Smsmith if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr)) == NULL) { 124967555Smsmith /* 125067555Smsmith * this can happen when the user specify the 0 valid 125167555Smsmith * lifetime. 125267555Smsmith */ 125360894Smsmith return (0); 125460894Smsmith } 125560894Smsmith 125660894Smsmith if (ifra->ifra_vhid > 0) { 125760894Smsmith if (carp_attach_p != NULL) 125860894Smsmith error = (*carp_attach_p)(&ia->ia_ifa, 125960894Smsmith ifra->ifra_vhid); 126060894Smsmith else 126160894Smsmith error = EPROTONOSUPPORT; 126260894Smsmith if (error) 126360894Smsmith goto out; 126460894Smsmith else 126560894Smsmith carp_attached = 1; 126660894Smsmith } 126760894Smsmith 126867555Smsmith /* 126960894Smsmith * then, make the prefix on-link on the interface. 127060894Smsmith * XXX: we'd rather create the prefix before the address, but 127160894Smsmith * we need at least one address to install the corresponding 127260894Smsmith * interface route, so we configure the address first. 127360894Smsmith */ 127460894Smsmith 127560894Smsmith /* 127660894Smsmith * convert mask to prefix length (prefixmask has already 127760894Smsmith * been validated in in6_update_ifa(). 127860894Smsmith */ 127960894Smsmith bzero(&pr0, sizeof(pr0)); 128060894Smsmith pr0.ndpr_ifp = ifp; 128160894Smsmith pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 128260894Smsmith NULL); 128360894Smsmith if (pr0.ndpr_plen == 128) { 128467555Smsmith /* we don't need to install a host route. */ 128560894Smsmith goto aifaddr_out; 128660894Smsmith } 128760894Smsmith pr0.ndpr_prefix = ifra->ifra_addr; 128860894Smsmith /* apply the mask for safety. */ 128960894Smsmith IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr, 129060894Smsmith &ifra->ifra_prefixmask.sin6_addr); 129160894Smsmith 129260894Smsmith /* 129360894Smsmith * XXX: since we don't have an API to set prefix (not address) 129460894Smsmith * lifetimes, we just use the same lifetimes as addresses. 129560894Smsmith * The (temporarily) installed lifetimes can be overridden by 129660894Smsmith * later advertised RAs (when accept_rtadv is non 0), which is 129760894Smsmith * an intended behavior. 129860894Smsmith */ 129960894Smsmith pr0.ndpr_raf_onlink = 1; /* should be configurable? */ 130060894Smsmith pr0.ndpr_raf_auto = 130160894Smsmith ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0); 130260894Smsmith pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime; 130360894Smsmith pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime; 130460894Smsmith 130567555Smsmith /* add the prefix if not yet. */ 130660894Smsmith if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { 130760894Smsmith /* 130860894Smsmith * nd6_prelist_add will install the corresponding 130960894Smsmith * interface route. 131060894Smsmith */ 131160894Smsmith if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) { 131260894Smsmith if (carp_attached) 131360894Smsmith (*carp_detach_p)(&ia->ia_ifa, false); 131460894Smsmith goto out; 131560894Smsmith } 131660894Smsmith } 131760894Smsmith 131860894Smsmith /* relate the address to the prefix */ 131960894Smsmith if (ia->ia6_ndpr == NULL) { 132060894Smsmith ia->ia6_ndpr = pr; 132160894Smsmith pr->ndpr_addrcnt++; 132260894Smsmith 132360894Smsmith /* 132467555Smsmith * If this is the first autoconf address from the 132560894Smsmith * prefix, create a temporary address as well 132660894Smsmith * (when required). 132760894Smsmith */ 132860894Smsmith if ((ia->ia6_flags & IN6_IFF_AUTOCONF) && 132960894Smsmith V_ip6_use_tempaddr && pr->ndpr_addrcnt == 1) { 133060894Smsmith int e; 133160894Smsmith if ((e = in6_tmpifadd(ia, 1, 0)) != 0) { 133260894Smsmith log(LOG_NOTICE, "in6_control: failed " 133360894Smsmith "to create a temporary address, " 133460894Smsmith "errno=%d\n", e); 133560894Smsmith } 133660894Smsmith } 133760894Smsmith } 133860894Smsmith nd6_prefix_rele(pr); 133960894Smsmith 134060894Smsmith /* 134160894Smsmith * this might affect the status of autoconfigured addresses, 134260894Smsmith * that is, this address might make other addresses detached. 134360894Smsmith */ 134460894Smsmith pfxlist_onlink_check(); 134560894Smsmith 134660894Smsmithaifaddr_out: 134760894Smsmith /* 134860894Smsmith * Try to clear the flag when a new IPv6 address is added 134960894Smsmith * onto an IFDISABLED interface and it succeeds. 135060894Smsmith */ 135160894Smsmith if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) { 135260894Smsmith struct in6_ndireq nd; 135360894Smsmith 135460894Smsmith memset(&nd, 0, sizeof(nd)); 135560894Smsmith nd.ndi.flags = ND_IFINFO(ifp)->flags; 135660894Smsmith nd.ndi.flags &= ~ND6_IFF_IFDISABLED; 135760894Smsmith if (nd6_ioctl(SIOCSIFINFO_FLAGS, (caddr_t)&nd, ifp) < 0) 135860894Smsmith log(LOG_NOTICE, "SIOCAIFADDR_IN6: " 135960894Smsmith "SIOCSIFINFO_FLAGS for -ifdisabled " 136060894Smsmith "failed."); 136160894Smsmith /* 136260894Smsmith * Ignore failure of clearing the flag intentionally. 136360894Smsmith * The failure means address duplication was detected. 136467555Smsmith */ 136560894Smsmith } 136660894Smsmith error = 0; 136760894Smsmith 136860894Smsmithout: 136960894Smsmith if (ia != NULL) 137060894Smsmith ifa_free(&ia->ia_ifa); 137160894Smsmith return (error); 137260894Smsmith} 137360894Smsmith 137460894Smsmithvoid 137560894Smsmithin6_purgeaddr(struct ifaddr *ifa) 137660894Smsmith{ 137760894Smsmith struct ifnet *ifp = ifa->ifa_ifp; 137860894Smsmith struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa; 137960894Smsmith struct in6_multi_mship *imm; 138060894Smsmith int error; 138160894Smsmith 138260894Smsmith if (ifa->ifa_carp) 138367555Smsmith (*carp_detach_p)(ifa, false); 138467555Smsmith 138560894Smsmith /* 138660894Smsmith * Remove the loopback route to the interface address. 138760894Smsmith * The check for the current setting of "nd6_useloopback" 138867555Smsmith * is not needed. 138967555Smsmith */ 139060894Smsmith if (ia->ia_flags & IFA_RTSELF) { 139160894Smsmith error = ifa_del_loopback_route((struct ifaddr *)ia, 139260894Smsmith (struct sockaddr *)&ia->ia_addr); 139360894Smsmith if (error == 0) 139467555Smsmith ia->ia_flags &= ~IFA_RTSELF; 139567555Smsmith } 139667555Smsmith 139767555Smsmith /* stop DAD processing */ 139867555Smsmith nd6_dad_stop(ifa); 139967555Smsmith 140067555Smsmith /* Leave multicast groups. */ 140160894Smsmith while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) { 140260894Smsmith LIST_REMOVE(imm, i6mm_chain); 140360894Smsmith if (imm->i6mm_maddr != NULL) 140460894Smsmith in6_leavegroup(imm->i6mm_maddr, NULL); 140560894Smsmith free(imm, M_IP6MADDR); 140667555Smsmith } 140767555Smsmith /* Check if we need to remove p2p route */ 140867555Smsmith if ((ia->ia_flags & IFA_ROUTE) && ifa_is_p2p(ia)) { 140960894Smsmith error = in6_handle_dstaddr_rtrequest(RTM_DELETE, ia); 141060894Smsmith if (error != 0) 141160894Smsmith log(LOG_INFO, "%s: err=%d, destination address delete " 141260894Smsmith "failed\n", __func__, error); 141360894Smsmith ia->ia_flags &= ~IFA_ROUTE; 141460894Smsmith } 141560894Smsmith 141660894Smsmith in6_newaddrmsg(ia, RTM_DELETE); 141760894Smsmith in6_unlink_ifa(ia, ifp); 141860894Smsmith} 141960894Smsmith 142060894Smsmith/* 142160894Smsmith * Removes @ia from the corresponding interfaces and unlinks corresponding 142260894Smsmith * prefix if no addresses are using it anymore. 142360894Smsmith */ 142460894Smsmithvoid 142560894Smsmithin6_purgeifaddr(struct in6_ifaddr *ia) 142660894Smsmith{ 142760894Smsmith struct nd_prefix *pr; 142860894Smsmith 142960894Smsmith /* 1430118508Sps * If the address being deleted is the only one that owns 143160894Smsmith * the corresponding prefix, expire the prefix as well. 143260894Smsmith * XXX: theoretically, we don't have to worry about such 143360894Smsmith * relationship, since we separate the address management 143460894Smsmith * and the prefix management. We do this, however, to provide 143560894Smsmith * as much backward compatibility as possible in terms of 143660894Smsmith * the ioctl operation. 143760894Smsmith * Note that in6_purgeaddr() will decrement ndpr_addrcnt. 143860894Smsmith */ 143960894Smsmith pr = ia->ia6_ndpr; 144060894Smsmith in6_purgeaddr(&ia->ia_ifa); 144160894Smsmith if (pr != NULL && pr->ndpr_addrcnt == 0) { 144260894Smsmith ND6_WLOCK(); 144360894Smsmith nd6_prefix_unlink(pr, NULL); 144460894Smsmith ND6_WUNLOCK(); 144560894Smsmith nd6_prefix_del(pr); 144660894Smsmith } 144760894Smsmith} 144860894Smsmith 144960894Smsmith 145060894Smsmithstatic void 145160894Smsmithin6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp) 145260894Smsmith{ 145360894Smsmith char ip6buf[INET6_ADDRSTRLEN]; 145467555Smsmith int remove_lle; 145560894Smsmith 145660894Smsmith IF_ADDR_WLOCK(ifp); 145760894Smsmith CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link); 145860894Smsmith IF_ADDR_WUNLOCK(ifp); 145960894Smsmith ifa_free(&ia->ia_ifa); /* if_addrhead */ 146060894Smsmith 146160894Smsmith /* 146260894Smsmith * Defer the release of what might be the last reference to the 146360894Smsmith * in6_ifaddr so that it can't be freed before the remainder of the 146460894Smsmith * cleanup. 146560894Smsmith */ 146660894Smsmith IN6_IFADDR_WLOCK(); 146760894Smsmith CK_STAILQ_REMOVE(&V_in6_ifaddrhead, ia, in6_ifaddr, ia_link); 146860894Smsmith CK_LIST_REMOVE(ia, ia6_hash); 146960894Smsmith IN6_IFADDR_WUNLOCK(); 147060894Smsmith 147160894Smsmith /* 147260894Smsmith * Release the reference to the base prefix. There should be a 147360894Smsmith * positive reference. 147460894Smsmith */ 147560894Smsmith remove_lle = 0; 147660894Smsmith if (ia->ia6_ndpr == NULL) { 147760894Smsmith nd6log((LOG_NOTICE, 147860894Smsmith "in6_unlink_ifa: autoconf'ed address " 147960894Smsmith "%s has no prefix\n", ip6_sprintf(ip6buf, IA6_IN6(ia)))); 148060894Smsmith } else { 148160894Smsmith ia->ia6_ndpr->ndpr_addrcnt--; 148260894Smsmith /* Do not delete lles within prefix if refcont != 0 */ 148360894Smsmith if (ia->ia6_ndpr->ndpr_addrcnt == 0) 148460894Smsmith remove_lle = 1; 148560894Smsmith ia->ia6_ndpr = NULL; 148660894Smsmith } 148760894Smsmith 148860894Smsmith nd6_rem_ifa_lle(ia, remove_lle); 148960894Smsmith 149060894Smsmith /* 149160894Smsmith * Also, if the address being removed is autoconf'ed, call 149260894Smsmith * pfxlist_onlink_check() since the release might affect the status of 149360894Smsmith * other (detached) addresses. 149460894Smsmith */ 149560894Smsmith if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) { 149660894Smsmith pfxlist_onlink_check(); 149760894Smsmith } 149860894Smsmith ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */ 149960894Smsmith} 150067555Smsmith 150160894Smsmith/* 150267555Smsmith * Notifies other subsystems about address change/arrival: 150367555Smsmith * 1) Notifies device handler on the first IPv6 address assignment 150460894Smsmith * 2) Handle routing table changes for P2P links and route 150560894Smsmith * 3) Handle routing table changes for address host route 150660894Smsmith */ 150760894Smsmithstatic int 150867555Smsmithin6_notify_ifa(struct ifnet *ifp, struct in6_ifaddr *ia, 150960894Smsmith struct in6_aliasreq *ifra, int hostIsNew) 151067555Smsmith{ 151167555Smsmith int error = 0, ifacount = 0; 151267555Smsmith struct ifaddr *ifa; 151369543Smsmith struct sockaddr_in6 *pdst; 151467555Smsmith char ip6buf[INET6_ADDRSTRLEN]; 151567555Smsmith 151667555Smsmith /* 151767555Smsmith * Give the interface a chance to initialize 151867555Smsmith * if this is its first address, 151960894Smsmith */ 152067555Smsmith if (hostIsNew != 0) { 152160894Smsmith struct epoch_tracker et; 152260894Smsmith 152360894Smsmith NET_EPOCH_ENTER(et); 152467555Smsmith CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 152560894Smsmith if (ifa->ifa_addr->sa_family != AF_INET6) 152660894Smsmith continue; 152760894Smsmith ifacount++; 152860894Smsmith } 152960894Smsmith NET_EPOCH_EXIT(et); 153060894Smsmith } 153160894Smsmith 153269543Smsmith if (ifacount <= 1 && ifp->if_ioctl) { 153369543Smsmith error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 153467555Smsmith if (error) 153560894Smsmith goto done; 153660894Smsmith } 153760894Smsmith 153867555Smsmith /* 153967555Smsmith * If a new destination address is specified, scrub the old one and 154067555Smsmith * install the new destination. Note that the interface must be 154167555Smsmith * p2p or loopback. 154260894Smsmith */ 154360894Smsmith pdst = &ifra->ifra_dstaddr; 154467555Smsmith if (pdst->sin6_family == AF_INET6 && 154560894Smsmith !IN6_ARE_ADDR_EQUAL(&pdst->sin6_addr, &ia->ia_dstaddr.sin6_addr)) { 154667555Smsmith if ((ia->ia_flags & IFA_ROUTE) != 0 && 154767555Smsmith (in6_handle_dstaddr_rtrequest(RTM_DELETE, ia) != 0)) { 154860894Smsmith nd6log((LOG_ERR, "in6_update_ifa_internal: failed to " 154967555Smsmith "remove a route to the old destination: %s\n", 155067555Smsmith ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 155167555Smsmith /* proceed anyway... */ 155260894Smsmith } else 155360894Smsmith ia->ia_flags &= ~IFA_ROUTE; 155467555Smsmith ia->ia_dstaddr = *pdst; 155560894Smsmith } 155667555Smsmith 155767555Smsmith /* 155860894Smsmith * If a new destination address is specified for a point-to-point 155967555Smsmith * interface, install a route to the destination as an interface 156067555Smsmith * direct route. 156167555Smsmith * XXX: the logic below rejects assigning multiple addresses on a p2p 156267555Smsmith * interface that share the same destination. 156367555Smsmith */ 156467555Smsmith if (!(ia->ia_flags & IFA_ROUTE) && ifa_is_p2p(ia)) { 156567555Smsmith error = in6_handle_dstaddr_rtrequest(RTM_ADD, ia); 156660894Smsmith if (error) 156767555Smsmith goto done; 156867555Smsmith ia->ia_flags |= IFA_ROUTE; 156967555Smsmith } 157067555Smsmith 157167555Smsmith /* 157267555Smsmith * add a loopback route to self if not exists 157367555Smsmith */ 157467555Smsmith if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) { 157567555Smsmith error = ifa_add_loopback_route((struct ifaddr *)ia, 157667555Smsmith (struct sockaddr *)&ia->ia_addr); 157760894Smsmith if (error == 0) 157867555Smsmith ia->ia_flags |= IFA_RTSELF; 157967555Smsmith } 158067555Smsmithdone: 158167555Smsmith WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 158267555Smsmith "Invoking IPv6 network device address event may sleep"); 158367555Smsmith 158467555Smsmith ifa_ref(&ia->ia_ifa); 158567555Smsmith EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa, 158667555Smsmith IFADDR_EVENT_ADD); 158767555Smsmith ifa_free(&ia->ia_ifa); 158867555Smsmith 158967555Smsmith return (error); 159067555Smsmith} 159167555Smsmith 159260894Smsmith/* 159367555Smsmith * Find an IPv6 interface link-local address specific to an interface. 159460894Smsmith * ifaddr is returned referenced. 159560894Smsmith */ 159660894Smsmithstruct in6_ifaddr * 159760894Smsmithin6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags) 159860894Smsmith{ 159967555Smsmith struct ifaddr *ifa; 160067555Smsmith 160167555Smsmith NET_EPOCH_ASSERT(); 160260894Smsmith 160360894Smsmith CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 160460894Smsmith if (ifa->ifa_addr->sa_family != AF_INET6) 160560894Smsmith continue; 160667555Smsmith if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 160767555Smsmith if ((((struct in6_ifaddr *)ifa)->ia6_flags & 160860894Smsmith ignoreflags) != 0) 160967555Smsmith continue; 161067555Smsmith ifa_ref(ifa); 161167555Smsmith break; 161267555Smsmith } 161367555Smsmith } 161467555Smsmith 161567555Smsmith return ((struct in6_ifaddr *)ifa); 161667555Smsmith} 161767555Smsmith 161867555Smsmith/* 161960894Smsmith * find the interface address corresponding to a given IPv6 address. 162060894Smsmith * ifaddr is returned referenced if @referenced flag is set. 162167555Smsmith */ 162267555Smsmithstruct in6_ifaddr * 162367555Smsmithin6ifa_ifwithaddr(const struct in6_addr *addr, uint32_t zoneid, bool referenced) 162467555Smsmith{ 162567555Smsmith struct rm_priotracker in6_ifa_tracker; 162660894Smsmith struct in6_ifaddr *ia; 162760894Smsmith 162860894Smsmith IN6_IFADDR_RLOCK(&in6_ifa_tracker); 162960894Smsmith CK_LIST_FOREACH(ia, IN6ADDR_HASH(addr), ia6_hash) { 163067555Smsmith if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), addr)) { 163167555Smsmith if (zoneid != 0 && 163267555Smsmith zoneid != ia->ia_addr.sin6_scope_id) 163367555Smsmith continue; 163467555Smsmith if (referenced) 163560894Smsmith ifa_ref(&ia->ia_ifa); 163691790Smsmith break; 163791790Smsmith } 163891790Smsmith } 163991790Smsmith IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 164091790Smsmith return (ia); 164191790Smsmith} 164291790Smsmith 164391790Smsmith/* 164460894Smsmith * find the internet address corresponding to a given interface and address. 164567555Smsmith * ifaddr is returned referenced. 164660894Smsmith */ 164760894Smsmithstruct in6_ifaddr * 164860894Smsmithin6ifa_ifpwithaddr(struct ifnet *ifp, const struct in6_addr *addr) 164960894Smsmith{ 165067555Smsmith struct epoch_tracker et; 165167555Smsmith struct ifaddr *ifa; 165267555Smsmith 165367555Smsmith NET_EPOCH_ENTER(et); 165467555Smsmith CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 165567555Smsmith if (ifa->ifa_addr->sa_family != AF_INET6) 165667555Smsmith continue; 165760894Smsmith if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) { 165860894Smsmith ifa_ref(ifa); 165960894Smsmith break; 166067555Smsmith } 166160894Smsmith } 166267555Smsmith NET_EPOCH_EXIT(et); 166367555Smsmith 166467555Smsmith return ((struct in6_ifaddr *)ifa); 166560894Smsmith} 166667555Smsmith 166767555Smsmith/* 166860894Smsmith * Find a link-local scoped address on ifp and return it if any. 166967555Smsmith */ 167067555Smsmithstruct in6_ifaddr * 167167555Smsmithin6ifa_llaonifp(struct ifnet *ifp) 167267555Smsmith{ 167367555Smsmith struct epoch_tracker et; 167491790Smsmith struct sockaddr_in6 *sin6; 167567555Smsmith struct ifaddr *ifa; 167667555Smsmith 167767555Smsmith if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) 167867555Smsmith return (NULL); 167967555Smsmith NET_EPOCH_ENTER(et); 168067555Smsmith CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 168167555Smsmith if (ifa->ifa_addr->sa_family != AF_INET6) 168267555Smsmith continue; 168367555Smsmith sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 168467555Smsmith if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) || 168591790Smsmith IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) || 168691790Smsmith IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr)) 168791790Smsmith break; 168891790Smsmith } 168991790Smsmith NET_EPOCH_EXIT(et); 169091790Smsmith 169167555Smsmith return ((struct in6_ifaddr *)ifa); 169267555Smsmith} 169367555Smsmith 169467555Smsmith/* 169567555Smsmith * Convert IP6 address to printable (loggable) representation. Caller 169667555Smsmith * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long. 169760894Smsmith */ 169860894Smsmithstatic char digits[] = "0123456789abcdef"; 169960894Smsmithchar * 170069543Smsmithip6_sprintf(char *ip6buf, const struct in6_addr *addr) 170169543Smsmith{ 170269543Smsmith int i, cnt = 0, maxcnt = 0, idx = 0, index = 0; 170369543Smsmith char *cp; 170467555Smsmith const u_int16_t *a = (const u_int16_t *)addr; 170569543Smsmith const u_int8_t *d; 170667555Smsmith int dcolon = 0, zero = 0; 170769543Smsmith 170869543Smsmith cp = ip6buf; 170976340Smsmith 171060894Smsmith for (i = 0; i < 8; i++) { 171176340Smsmith if (*(a + i) == 0) { 171276340Smsmith cnt++; 171376340Smsmith if (cnt == 1) 171476340Smsmith idx = i; 171576340Smsmith } 171676340Smsmith else if (maxcnt < cnt) { 171776340Smsmith maxcnt = cnt; 171876340Smsmith index = idx; 171976340Smsmith cnt = 0; 172069543Smsmith } 172176340Smsmith } 172269543Smsmith if (maxcnt < cnt) { 172376340Smsmith maxcnt = cnt; 172491790Smsmith index = idx; 172591790Smsmith } 172669543Smsmith 172791790Smsmith for (i = 0; i < 8; i++) { 172891790Smsmith if (dcolon == 1) { 172991790Smsmith if (*a == 0) { 173091790Smsmith if (i == 7) 173191790Smsmith *cp++ = ':'; 173291790Smsmith a++; 173391790Smsmith continue; 173491790Smsmith } else 173591790Smsmith dcolon = 2; 173691790Smsmith } 173791790Smsmith if (*a == 0) { 173891790Smsmith if (dcolon == 0 && *(a + 1) == 0 && i == index) { 173991790Smsmith if (i == 0) 174091790Smsmith *cp++ = ':'; 174191790Smsmith *cp++ = ':'; 174291790Smsmith dcolon = 1; 174376340Smsmith } else { 174476340Smsmith *cp++ = '0'; 174576340Smsmith *cp++ = ':'; 174676340Smsmith } 174791790Smsmith a++; 174891790Smsmith continue; 174991790Smsmith } 175091790Smsmith d = (const u_char *)a; 175176340Smsmith /* Try to eliminate leading zeros in printout like in :0001. */ 175276340Smsmith zero = 1; 175376340Smsmith *cp = digits[*d >> 4]; 175476340Smsmith if (*cp != '0') { 175591790Smsmith zero = 0; 175691790Smsmith cp++; 175791790Smsmith } 175891790Smsmith *cp = digits[*d++ & 0xf]; 175967555Smsmith if (zero == 0 || (*cp != '0')) { 176076340Smsmith zero = 0; 176169543Smsmith cp++; 176267555Smsmith } 176367555Smsmith *cp = digits[*d >> 4]; 176469543Smsmith if (zero == 0 || (*cp != '0')) { 176569543Smsmith zero = 0; 176669543Smsmith cp++; 176767555Smsmith } 176867555Smsmith *cp++ = digits[*d & 0xf]; 176960894Smsmith *cp++ = ':'; 177067555Smsmith a++; 177160894Smsmith } 177267555Smsmith *--cp = '\0'; 177367555Smsmith return (ip6buf); 177469543Smsmith} 177569543Smsmith 177669543Smsmithint 177769543Smsmithin6_localaddr(struct in6_addr *in6) 177869543Smsmith{ 177969543Smsmith struct rm_priotracker in6_ifa_tracker; 178067555Smsmith struct in6_ifaddr *ia; 178167555Smsmith 178260894Smsmith if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 178367555Smsmith return 1; 178467555Smsmith 178567555Smsmith IN6_IFADDR_RLOCK(&in6_ifa_tracker); 178667555Smsmith CK_STAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 178767555Smsmith if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 178867555Smsmith &ia->ia_prefixmask.sin6_addr)) { 178967555Smsmith IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 179067555Smsmith return 1; 179167555Smsmith } 179260894Smsmith } 179360894Smsmith IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 179467555Smsmith 179560894Smsmith return (0); 179660894Smsmith} 179760894Smsmith 179860894Smsmith/* 179960894Smsmith * Return 1 if an internet address is for the local host and configured 180060894Smsmith * on one of its interfaces. 180167555Smsmith */ 180260894Smsmithint 180360894Smsmithin6_localip(struct in6_addr *in6) 180460894Smsmith{ 180567555Smsmith struct rm_priotracker in6_ifa_tracker; 180667555Smsmith struct in6_ifaddr *ia; 180767555Smsmith 180867555Smsmith IN6_IFADDR_RLOCK(&in6_ifa_tracker); 180967555Smsmith CK_LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) { 181067555Smsmith if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) { 181167555Smsmith IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 181267555Smsmith return (1); 181367555Smsmith } 181467555Smsmith } 181567555Smsmith IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 181667555Smsmith return (0); 181767555Smsmith} 181860894Smsmith 181960894Smsmith/* 182067555Smsmith * Like in6_localip(), but FIB-aware and carp(4)-aware. 182167555Smsmith */ 182267555Smsmithbool 182367555Smsmithin6_localip_fib(struct in6_addr *in6, uint16_t fib) 182467555Smsmith{ 182560894Smsmith struct rm_priotracker in6_ifa_tracker; 182660894Smsmith struct in6_ifaddr *ia; 182767555Smsmith 182867555Smsmith IN6_IFADDR_RLOCK(&in6_ifa_tracker); 182967555Smsmith CK_LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) { 183067555Smsmith if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr) && 183167555Smsmith (ia->ia_ifa.ifa_carp == NULL || 183260894Smsmith carp_master_p(&ia->ia_ifa)) && 183367555Smsmith ia->ia_ifa.ifa_ifp->if_fib == fib) { 183460894Smsmith IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 183567555Smsmith return (true); 183667555Smsmith } 183767555Smsmith } 183867555Smsmith IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 183960894Smsmith return (false); 184060894Smsmith} 184160894Smsmith 1842/* 1843 * Return 1 if an internet address is configured on an interface. 1844 */ 1845int 1846in6_ifhasaddr(struct ifnet *ifp, struct in6_addr *addr) 1847{ 1848 struct in6_addr in6; 1849 struct ifaddr *ifa; 1850 struct in6_ifaddr *ia6; 1851 1852 NET_EPOCH_ASSERT(); 1853 1854 in6 = *addr; 1855 if (in6_clearscope(&in6)) 1856 return (0); 1857 in6_setscope(&in6, ifp, NULL); 1858 1859 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1860 if (ifa->ifa_addr->sa_family != AF_INET6) 1861 continue; 1862 ia6 = (struct in6_ifaddr *)ifa; 1863 if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &in6)) 1864 return (1); 1865 } 1866 1867 return (0); 1868} 1869 1870int 1871in6_is_addr_deprecated(struct sockaddr_in6 *sa6) 1872{ 1873 struct rm_priotracker in6_ifa_tracker; 1874 struct in6_ifaddr *ia; 1875 1876 IN6_IFADDR_RLOCK(&in6_ifa_tracker); 1877 CK_LIST_FOREACH(ia, IN6ADDR_HASH(&sa6->sin6_addr), ia6_hash) { 1878 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), &sa6->sin6_addr)) { 1879 if (ia->ia6_flags & IN6_IFF_DEPRECATED) { 1880 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 1881 return (1); /* true */ 1882 } 1883 break; 1884 } 1885 } 1886 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 1887 1888 return (0); /* false */ 1889} 1890 1891/* 1892 * return length of part which dst and src are equal 1893 * hard coding... 1894 */ 1895int 1896in6_matchlen(struct in6_addr *src, struct in6_addr *dst) 1897{ 1898 int match = 0; 1899 u_char *s = (u_char *)src, *d = (u_char *)dst; 1900 u_char *lim = s + 16, r; 1901 1902 while (s < lim) 1903 if ((r = (*d++ ^ *s++)) != 0) { 1904 while (r < 128) { 1905 match++; 1906 r <<= 1; 1907 } 1908 break; 1909 } else 1910 match += 8; 1911 return match; 1912} 1913 1914/* XXX: to be scope conscious */ 1915int 1916in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len) 1917{ 1918 int bytelen, bitlen; 1919 1920 /* sanity check */ 1921 if (0 > len || len > 128) { 1922 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 1923 len); 1924 return (0); 1925 } 1926 1927 bytelen = len / 8; 1928 bitlen = len % 8; 1929 1930 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 1931 return (0); 1932 if (bitlen != 0 && 1933 p1->s6_addr[bytelen] >> (8 - bitlen) != 1934 p2->s6_addr[bytelen] >> (8 - bitlen)) 1935 return (0); 1936 1937 return (1); 1938} 1939 1940void 1941in6_prefixlen2mask(struct in6_addr *maskp, int len) 1942{ 1943 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 1944 int bytelen, bitlen, i; 1945 1946 /* sanity check */ 1947 if (0 > len || len > 128) { 1948 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 1949 len); 1950 return; 1951 } 1952 1953 bzero(maskp, sizeof(*maskp)); 1954 bytelen = len / 8; 1955 bitlen = len % 8; 1956 for (i = 0; i < bytelen; i++) 1957 maskp->s6_addr[i] = 0xff; 1958 if (bitlen) 1959 maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 1960} 1961 1962/* 1963 * return the best address out of the same scope. if no address was 1964 * found, return the first valid address from designated IF. 1965 */ 1966struct in6_ifaddr * 1967in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst) 1968{ 1969 int dst_scope = in6_addrscope(dst), blen = -1, tlen; 1970 struct ifaddr *ifa; 1971 struct in6_ifaddr *besta = NULL; 1972 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 1973 1974 NET_EPOCH_ASSERT(); 1975 1976 dep[0] = dep[1] = NULL; 1977 1978 /* 1979 * We first look for addresses in the same scope. 1980 * If there is one, return it. 1981 * If two or more, return one which matches the dst longest. 1982 * If none, return one of global addresses assigned other ifs. 1983 */ 1984 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1985 if (ifa->ifa_addr->sa_family != AF_INET6) 1986 continue; 1987 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 1988 continue; /* XXX: is there any case to allow anycast? */ 1989 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 1990 continue; /* don't use this interface */ 1991 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 1992 continue; 1993 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 1994 if (V_ip6_use_deprecated) 1995 dep[0] = (struct in6_ifaddr *)ifa; 1996 continue; 1997 } 1998 1999 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 2000 /* 2001 * call in6_matchlen() as few as possible 2002 */ 2003 if (besta) { 2004 if (blen == -1) 2005 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 2006 tlen = in6_matchlen(IFA_IN6(ifa), dst); 2007 if (tlen > blen) { 2008 blen = tlen; 2009 besta = (struct in6_ifaddr *)ifa; 2010 } 2011 } else 2012 besta = (struct in6_ifaddr *)ifa; 2013 } 2014 } 2015 if (besta) 2016 return (besta); 2017 2018 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2019 if (ifa->ifa_addr->sa_family != AF_INET6) 2020 continue; 2021 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2022 continue; /* XXX: is there any case to allow anycast? */ 2023 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2024 continue; /* don't use this interface */ 2025 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2026 continue; 2027 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2028 if (V_ip6_use_deprecated) 2029 dep[1] = (struct in6_ifaddr *)ifa; 2030 continue; 2031 } 2032 2033 return (struct in6_ifaddr *)ifa; 2034 } 2035 2036 /* use the last-resort values, that are, deprecated addresses */ 2037 if (dep[0]) 2038 return dep[0]; 2039 if (dep[1]) 2040 return dep[1]; 2041 2042 return NULL; 2043} 2044 2045/* 2046 * perform DAD when interface becomes IFF_UP. 2047 */ 2048void 2049in6_if_up(struct ifnet *ifp) 2050{ 2051 struct epoch_tracker et; 2052 struct ifaddr *ifa; 2053 struct in6_ifaddr *ia; 2054 2055 NET_EPOCH_ENTER(et); 2056 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2057 if (ifa->ifa_addr->sa_family != AF_INET6) 2058 continue; 2059 ia = (struct in6_ifaddr *)ifa; 2060 if (ia->ia6_flags & IN6_IFF_TENTATIVE) { 2061 /* 2062 * The TENTATIVE flag was likely set by hand 2063 * beforehand, implicitly indicating the need for DAD. 2064 * We may be able to skip the random delay in this 2065 * case, but we impose delays just in case. 2066 */ 2067 nd6_dad_start(ifa, 2068 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz)); 2069 } 2070 } 2071 NET_EPOCH_EXIT(et); 2072 2073 /* 2074 * special cases, like 6to4, are handled in in6_ifattach 2075 */ 2076 in6_ifattach(ifp, NULL); 2077} 2078 2079static void 2080in6_ifevent(void *arg __unused, struct ifnet *ifp, int event) 2081{ 2082 if (event == IFNET_EVENT_UP) 2083 in6_if_up(ifp); 2084} 2085 2086static void 2087in6_init(void *arg __unused) 2088{ 2089 EVENTHANDLER_REGISTER(ifnet_event, in6_ifevent, NULL, EVENTHANDLER_PRI_ANY); 2090} 2091SYSINIT(in6_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, in6_init, NULL); 2092 2093int 2094in6if_do_dad(struct ifnet *ifp) 2095{ 2096 2097 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 2098 return (0); 2099 if ((ifp->if_flags & IFF_MULTICAST) == 0) 2100 return (0); 2101 if ((ND_IFINFO(ifp)->flags & 2102 (ND6_IFF_IFDISABLED | ND6_IFF_NO_DAD)) != 0) 2103 return (0); 2104 return (1); 2105} 2106 2107/* 2108 * Calculate max IPv6 MTU through all the interfaces and store it 2109 * to in6_maxmtu. 2110 */ 2111void 2112in6_setmaxmtu(void) 2113{ 2114 struct epoch_tracker et; 2115 unsigned long maxmtu = 0; 2116 struct ifnet *ifp; 2117 2118 NET_EPOCH_ENTER(et); 2119 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2120 /* this function can be called during ifnet initialization */ 2121 if (!ifp->if_afdata[AF_INET6]) 2122 continue; 2123 if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 2124 IN6_LINKMTU(ifp) > maxmtu) 2125 maxmtu = IN6_LINKMTU(ifp); 2126 } 2127 NET_EPOCH_EXIT(et); 2128 if (maxmtu) /* update only when maxmtu is positive */ 2129 V_in6_maxmtu = maxmtu; 2130} 2131 2132/* 2133 * Provide the length of interface identifiers to be used for the link attached 2134 * to the given interface. The length should be defined in "IPv6 over 2135 * xxx-link" document. Note that address architecture might also define 2136 * the length for a particular set of address prefixes, regardless of the 2137 * link type. As clarified in rfc2462bis, those two definitions should be 2138 * consistent, and those really are as of August 2004. 2139 */ 2140int 2141in6_if2idlen(struct ifnet *ifp) 2142{ 2143 switch (ifp->if_type) { 2144 case IFT_ETHER: /* RFC2464 */ 2145 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */ 2146 case IFT_L2VLAN: /* ditto */ 2147 case IFT_BRIDGE: /* bridge(4) only does Ethernet-like links */ 2148 case IFT_INFINIBAND: 2149 return (64); 2150 case IFT_PPP: /* RFC2472 */ 2151 return (64); 2152 case IFT_FRELAY: /* RFC2590 */ 2153 return (64); 2154 case IFT_IEEE1394: /* RFC3146 */ 2155 return (64); 2156 case IFT_GIF: 2157 return (64); /* draft-ietf-v6ops-mech-v2-07 */ 2158 case IFT_LOOP: 2159 return (64); /* XXX: is this really correct? */ 2160 default: 2161 /* 2162 * Unknown link type: 2163 * It might be controversial to use the today's common constant 2164 * of 64 for these cases unconditionally. For full compliance, 2165 * we should return an error in this case. On the other hand, 2166 * if we simply miss the standard for the link type or a new 2167 * standard is defined for a new link type, the IFID length 2168 * is very likely to be the common constant. As a compromise, 2169 * we always use the constant, but make an explicit notice 2170 * indicating the "unknown" case. 2171 */ 2172 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type); 2173 return (64); 2174 } 2175} 2176 2177struct in6_llentry { 2178 struct llentry base; 2179}; 2180 2181#define IN6_LLTBL_DEFAULT_HSIZE 32 2182#define IN6_LLTBL_HASH(k, h) \ 2183 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1)) 2184 2185/* 2186 * Do actual deallocation of @lle. 2187 */ 2188static void 2189in6_lltable_destroy_lle_unlocked(epoch_context_t ctx) 2190{ 2191 struct llentry *lle; 2192 2193 lle = __containerof(ctx, struct llentry, lle_epoch_ctx); 2194 LLE_LOCK_DESTROY(lle); 2195 LLE_REQ_DESTROY(lle); 2196 free(lle, M_LLTABLE); 2197} 2198 2199/* 2200 * Called by LLE_FREE_LOCKED when number of references 2201 * drops to zero. 2202 */ 2203static void 2204in6_lltable_destroy_lle(struct llentry *lle) 2205{ 2206 2207 LLE_WUNLOCK(lle); 2208 NET_EPOCH_CALL(in6_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx); 2209} 2210 2211static struct llentry * 2212in6_lltable_new(const struct in6_addr *addr6, u_int flags) 2213{ 2214 struct in6_llentry *lle; 2215 2216 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO); 2217 if (lle == NULL) /* NB: caller generates msg */ 2218 return NULL; 2219 2220 lle->base.r_l3addr.addr6 = *addr6; 2221 lle->base.lle_refcnt = 1; 2222 lle->base.lle_free = in6_lltable_destroy_lle; 2223 LLE_LOCK_INIT(&lle->base); 2224 LLE_REQ_INIT(&lle->base); 2225 callout_init(&lle->base.lle_timer, 1); 2226 2227 return (&lle->base); 2228} 2229 2230static int 2231in6_lltable_match_prefix(const struct sockaddr *saddr, 2232 const struct sockaddr *smask, u_int flags, struct llentry *lle) 2233{ 2234 const struct in6_addr *addr, *mask, *lle_addr; 2235 2236 addr = &((const struct sockaddr_in6 *)saddr)->sin6_addr; 2237 mask = &((const struct sockaddr_in6 *)smask)->sin6_addr; 2238 lle_addr = &lle->r_l3addr.addr6; 2239 2240 if (IN6_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0) 2241 return (0); 2242 2243 if (lle->la_flags & LLE_IFADDR) { 2244 /* 2245 * Delete LLE_IFADDR records IFF address & flag matches. 2246 * Note that addr is the interface address within prefix 2247 * being matched. 2248 */ 2249 if (IN6_ARE_ADDR_EQUAL(addr, lle_addr) && 2250 (flags & LLE_STATIC) != 0) 2251 return (1); 2252 return (0); 2253 } 2254 2255 /* flags & LLE_STATIC means deleting both dynamic and static entries */ 2256 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)) 2257 return (1); 2258 2259 return (0); 2260} 2261 2262static void 2263in6_lltable_free_entry(struct lltable *llt, struct llentry *lle) 2264{ 2265 struct ifnet *ifp __diagused; 2266 2267 LLE_WLOCK_ASSERT(lle); 2268 KASSERT(llt != NULL, ("lltable is NULL")); 2269 2270 /* Unlink entry from table */ 2271 if ((lle->la_flags & LLE_LINKED) != 0) { 2272 ifp = llt->llt_ifp; 2273 IF_AFDATA_WLOCK_ASSERT(ifp); 2274 lltable_unlink_entry(llt, lle); 2275 } 2276 2277 llentry_free(lle); 2278} 2279 2280static int 2281in6_lltable_rtcheck(struct ifnet *ifp, 2282 u_int flags, 2283 const struct sockaddr *l3addr) 2284{ 2285 const struct sockaddr_in6 *sin6; 2286 struct nhop_object *nh; 2287 struct in6_addr dst; 2288 uint32_t scopeid; 2289 char ip6buf[INET6_ADDRSTRLEN]; 2290 int fibnum; 2291 2292 NET_EPOCH_ASSERT(); 2293 KASSERT(l3addr->sa_family == AF_INET6, 2294 ("sin_family %d", l3addr->sa_family)); 2295 2296 sin6 = (const struct sockaddr_in6 *)l3addr; 2297 in6_splitscope(&sin6->sin6_addr, &dst, &scopeid); 2298 fibnum = V_rt_add_addr_allfibs ? RT_DEFAULT_FIB : ifp->if_fib; 2299 nh = fib6_lookup(fibnum, &dst, scopeid, NHR_NONE, 0); 2300 if (nh && ((nh->nh_flags & NHF_GATEWAY) || nh->nh_ifp != ifp)) { 2301 struct ifaddr *ifa; 2302 /* 2303 * Create an ND6 cache for an IPv6 neighbor 2304 * that is not covered by our own prefix. 2305 */ 2306 ifa = ifaof_ifpforaddr(l3addr, ifp); 2307 if (ifa != NULL) { 2308 return 0; 2309 } 2310 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n", 2311 ip6_sprintf(ip6buf, &sin6->sin6_addr)); 2312 return EINVAL; 2313 } 2314 return 0; 2315} 2316 2317static inline uint32_t 2318in6_lltable_hash_dst(const struct in6_addr *dst, uint32_t hsize) 2319{ 2320 2321 return (IN6_LLTBL_HASH(dst->s6_addr32[3], hsize)); 2322} 2323 2324static uint32_t 2325in6_lltable_hash(const struct llentry *lle, uint32_t hsize) 2326{ 2327 2328 return (in6_lltable_hash_dst(&lle->r_l3addr.addr6, hsize)); 2329} 2330 2331static void 2332in6_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa) 2333{ 2334 struct sockaddr_in6 *sin6; 2335 2336 sin6 = (struct sockaddr_in6 *)sa; 2337 bzero(sin6, sizeof(*sin6)); 2338 sin6->sin6_family = AF_INET6; 2339 sin6->sin6_len = sizeof(*sin6); 2340 sin6->sin6_addr = lle->r_l3addr.addr6; 2341} 2342 2343static inline struct llentry * 2344in6_lltable_find_dst(struct lltable *llt, const struct in6_addr *dst) 2345{ 2346 struct llentry *lle; 2347 struct llentries *lleh; 2348 u_int hashidx; 2349 2350 hashidx = in6_lltable_hash_dst(dst, llt->llt_hsize); 2351 lleh = &llt->lle_head[hashidx]; 2352 CK_LIST_FOREACH(lle, lleh, lle_next) { 2353 if (lle->la_flags & LLE_DELETED) 2354 continue; 2355 if (IN6_ARE_ADDR_EQUAL(&lle->r_l3addr.addr6, dst)) 2356 break; 2357 } 2358 2359 return (lle); 2360} 2361 2362static void 2363in6_lltable_delete_entry(struct lltable *llt, struct llentry *lle) 2364{ 2365 2366 lle->la_flags |= LLE_DELETED; 2367 2368 /* Leave the solicited multicast group. */ 2369 if ((lle->la_flags & LLE_PUB) != 0) 2370 in6_leave_proxy_ndp_mc(llt->llt_ifp, &lle->r_l3addr.addr6); 2371 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED); 2372#ifdef DIAGNOSTIC 2373 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 2374#endif 2375 llentry_free(lle); 2376} 2377 2378static struct llentry * 2379in6_lltable_alloc(struct lltable *llt, u_int flags, 2380 const struct sockaddr *l3addr) 2381{ 2382 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2383 struct ifnet *ifp = llt->llt_ifp; 2384 struct llentry *lle; 2385 char linkhdr[LLE_MAX_LINKHDR]; 2386 size_t linkhdrsize; 2387 int lladdr_off; 2388 2389 KASSERT(l3addr->sa_family == AF_INET6, 2390 ("sin_family %d", l3addr->sa_family)); 2391 2392 /* 2393 * A route that covers the given address must have 2394 * been installed 1st because we are doing a resolution, 2395 * verify this. 2396 */ 2397 if (!(flags & LLE_IFADDR) && 2398 in6_lltable_rtcheck(ifp, flags, l3addr) != 0) 2399 return (NULL); 2400 2401 lle = in6_lltable_new(&sin6->sin6_addr, flags); 2402 if (lle == NULL) { 2403 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 2404 return (NULL); 2405 } 2406 lle->la_flags = flags; 2407 if ((flags & LLE_IFADDR) == LLE_IFADDR) { 2408 linkhdrsize = LLE_MAX_LINKHDR; 2409 if (lltable_calc_llheader(ifp, AF_INET6, IF_LLADDR(ifp), 2410 linkhdr, &linkhdrsize, &lladdr_off) != 0) { 2411 in6_lltable_free_entry(llt, lle); 2412 return (NULL); 2413 } 2414 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize, 2415 lladdr_off); 2416 lle->la_flags |= LLE_STATIC; 2417 } 2418 2419 if ((lle->la_flags & LLE_STATIC) != 0) 2420 lle->ln_state = ND6_LLINFO_REACHABLE; 2421 2422 return (lle); 2423} 2424 2425static struct llentry * 2426in6_lltable_lookup(struct lltable *llt, u_int flags, 2427 const struct sockaddr *l3addr) 2428{ 2429 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2430 int family = flags >> 16; 2431 struct llentry *lle; 2432 2433 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp); 2434 KASSERT(l3addr->sa_family == AF_INET6, 2435 ("sin_family %d", l3addr->sa_family)); 2436 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) != 2437 (LLE_UNLOCKED | LLE_EXCLUSIVE), 2438 ("wrong lle request flags: %#x", flags)); 2439 2440 lle = in6_lltable_find_dst(llt, &sin6->sin6_addr); 2441 2442 if (__predict_false(family != AF_INET6)) 2443 lle = llentry_lookup_family(lle, family); 2444 2445 if (lle == NULL) 2446 return (NULL); 2447 2448 if (flags & LLE_UNLOCKED) 2449 return (lle); 2450 2451 if (flags & LLE_EXCLUSIVE) 2452 LLE_WLOCK(lle); 2453 else 2454 LLE_RLOCK(lle); 2455 2456 /* 2457 * If the afdata lock is not held, the LLE may have been unlinked while 2458 * we were blocked on the LLE lock. Check for this case. 2459 */ 2460 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) { 2461 if (flags & LLE_EXCLUSIVE) 2462 LLE_WUNLOCK(lle); 2463 else 2464 LLE_RUNLOCK(lle); 2465 return (NULL); 2466 } 2467 return (lle); 2468} 2469 2470static int 2471in6_lltable_dump_entry(struct lltable *llt, struct llentry *lle, 2472 struct sysctl_req *wr) 2473{ 2474 struct ifnet *ifp = llt->llt_ifp; 2475 /* XXX stack use */ 2476 struct { 2477 struct rt_msghdr rtm; 2478 struct sockaddr_in6 sin6; 2479 /* 2480 * ndp.c assumes that sdl is word aligned 2481 */ 2482#ifdef __LP64__ 2483 uint32_t pad; 2484#endif 2485 struct sockaddr_dl sdl; 2486 } ndpc; 2487 struct sockaddr_dl *sdl; 2488 int error; 2489 2490 bzero(&ndpc, sizeof(ndpc)); 2491 /* skip deleted entries */ 2492 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED) 2493 return (0); 2494 /* Skip if jailed and not a valid IP of the prison. */ 2495 lltable_fill_sa_entry(lle, (struct sockaddr *)&ndpc.sin6); 2496 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&ndpc.sin6) != 0) 2497 return (0); 2498 /* 2499 * produce a msg made of: 2500 * struct rt_msghdr; 2501 * struct sockaddr_in6 (IPv6) 2502 * struct sockaddr_dl; 2503 */ 2504 ndpc.rtm.rtm_msglen = sizeof(ndpc); 2505 ndpc.rtm.rtm_version = RTM_VERSION; 2506 ndpc.rtm.rtm_type = RTM_GET; 2507 ndpc.rtm.rtm_flags = RTF_UP; 2508 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 2509 sa6_recoverscope(&ndpc.sin6); 2510 2511 /* publish */ 2512 if (lle->la_flags & LLE_PUB) 2513 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE; 2514 2515 sdl = &ndpc.sdl; 2516 sdl->sdl_family = AF_LINK; 2517 sdl->sdl_len = sizeof(*sdl); 2518 sdl->sdl_index = ifp->if_index; 2519 sdl->sdl_type = ifp->if_type; 2520 if ((lle->la_flags & LLE_VALID) == LLE_VALID) { 2521 sdl->sdl_alen = ifp->if_addrlen; 2522 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 2523 } else { 2524 sdl->sdl_alen = 0; 2525 bzero(LLADDR(sdl), ifp->if_addrlen); 2526 } 2527 if (lle->la_expire != 0) 2528 ndpc.rtm.rtm_rmx.rmx_expire = lle->la_expire + 2529 lle->lle_remtime / hz + time_second - time_uptime; 2530 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 2531 if (lle->la_flags & LLE_STATIC) 2532 ndpc.rtm.rtm_flags |= RTF_STATIC; 2533 if (lle->la_flags & LLE_IFADDR) 2534 ndpc.rtm.rtm_flags |= RTF_PINNED; 2535 if (lle->ln_router != 0) 2536 ndpc.rtm.rtm_flags |= RTF_GATEWAY; 2537 ndpc.rtm.rtm_rmx.rmx_pksent = lle->la_asked; 2538 /* Store state in rmx_weight value */ 2539 ndpc.rtm.rtm_rmx.rmx_state = lle->ln_state; 2540 ndpc.rtm.rtm_index = ifp->if_index; 2541 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc)); 2542 2543 return (error); 2544} 2545 2546static void 2547in6_lltable_post_resolved(struct lltable *llt, struct llentry *lle) 2548{ 2549 /* Join the solicited multicast group for dst. */ 2550 if ((lle->la_flags & LLE_PUB) == LLE_PUB) 2551 in6_join_proxy_ndp_mc(llt->llt_ifp, &lle->r_l3addr.addr6); 2552} 2553 2554static struct lltable * 2555in6_lltattach(struct ifnet *ifp) 2556{ 2557 struct lltable *llt; 2558 2559 llt = lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE); 2560 llt->llt_af = AF_INET6; 2561 llt->llt_ifp = ifp; 2562 2563 llt->llt_lookup = in6_lltable_lookup; 2564 llt->llt_alloc_entry = in6_lltable_alloc; 2565 llt->llt_delete_entry = in6_lltable_delete_entry; 2566 llt->llt_dump_entry = in6_lltable_dump_entry; 2567 llt->llt_hash = in6_lltable_hash; 2568 llt->llt_fill_sa_entry = in6_lltable_fill_sa_entry; 2569 llt->llt_free_entry = in6_lltable_free_entry; 2570 llt->llt_match_prefix = in6_lltable_match_prefix; 2571 llt->llt_mark_used = llentry_mark_used; 2572 llt->llt_post_resolved = in6_lltable_post_resolved; 2573 lltable_link(llt); 2574 2575 return (llt); 2576} 2577 2578struct lltable * 2579in6_lltable_get(struct ifnet *ifp) 2580{ 2581 struct lltable *llt = NULL; 2582 2583 void *afdata_ptr = ifp->if_afdata[AF_INET6]; 2584 if (afdata_ptr != NULL) 2585 llt = ((struct in6_ifextra *)afdata_ptr)->lltable; 2586 return (llt); 2587} 2588 2589void * 2590in6_domifattach(struct ifnet *ifp) 2591{ 2592 struct in6_ifextra *ext; 2593 2594 /* There are not IPv6-capable interfaces. */ 2595 switch (ifp->if_type) { 2596 case IFT_PFLOG: 2597 case IFT_PFSYNC: 2598 case IFT_USB: 2599 return (NULL); 2600 } 2601 ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK); 2602 bzero(ext, sizeof(*ext)); 2603 2604 ext->in6_ifstat = malloc(sizeof(counter_u64_t) * 2605 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_IFADDR, M_WAITOK); 2606 COUNTER_ARRAY_ALLOC(ext->in6_ifstat, 2607 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_WAITOK); 2608 2609 ext->icmp6_ifstat = malloc(sizeof(counter_u64_t) * 2610 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_IFADDR, 2611 M_WAITOK); 2612 COUNTER_ARRAY_ALLOC(ext->icmp6_ifstat, 2613 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_WAITOK); 2614 2615 ext->nd_ifinfo = nd6_ifattach(ifp); 2616 ext->scope6_id = scope6_ifattach(ifp); 2617 ext->lltable = in6_lltattach(ifp); 2618 2619 ext->mld_ifinfo = mld_domifattach(ifp); 2620 2621 return ext; 2622} 2623 2624int 2625in6_domifmtu(struct ifnet *ifp) 2626{ 2627 if (ifp->if_afdata[AF_INET6] == NULL) 2628 return ifp->if_mtu; 2629 2630 return (IN6_LINKMTU(ifp)); 2631} 2632 2633void 2634in6_domifdetach(struct ifnet *ifp, void *aux) 2635{ 2636 struct in6_ifextra *ext = (struct in6_ifextra *)aux; 2637 2638 mld_domifdetach(ifp); 2639 scope6_ifdetach(ext->scope6_id); 2640 nd6_ifdetach(ifp, ext->nd_ifinfo); 2641 lltable_free(ext->lltable); 2642 COUNTER_ARRAY_FREE(ext->in6_ifstat, 2643 sizeof(struct in6_ifstat) / sizeof(uint64_t)); 2644 free(ext->in6_ifstat, M_IFADDR); 2645 COUNTER_ARRAY_FREE(ext->icmp6_ifstat, 2646 sizeof(struct icmp6_ifstat) / sizeof(uint64_t)); 2647 free(ext->icmp6_ifstat, M_IFADDR); 2648 free(ext, M_IFADDR); 2649} 2650 2651/* 2652 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 2653 * v4 mapped addr or v4 compat addr 2654 */ 2655void 2656in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 2657{ 2658 2659 bzero(sin, sizeof(*sin)); 2660 sin->sin_len = sizeof(struct sockaddr_in); 2661 sin->sin_family = AF_INET; 2662 sin->sin_port = sin6->sin6_port; 2663 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 2664} 2665 2666/* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 2667void 2668in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 2669{ 2670 bzero(sin6, sizeof(*sin6)); 2671 sin6->sin6_len = sizeof(struct sockaddr_in6); 2672 sin6->sin6_family = AF_INET6; 2673 sin6->sin6_port = sin->sin_port; 2674 sin6->sin6_addr.s6_addr32[0] = 0; 2675 sin6->sin6_addr.s6_addr32[1] = 0; 2676 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 2677 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; 2678} 2679 2680/* Convert sockaddr_in6 into sockaddr_in. */ 2681void 2682in6_sin6_2_sin_in_sock(struct sockaddr *nam) 2683{ 2684 struct sockaddr_in *sin_p; 2685 struct sockaddr_in6 sin6; 2686 2687 /* 2688 * Save original sockaddr_in6 addr and convert it 2689 * to sockaddr_in. 2690 */ 2691 sin6 = *(struct sockaddr_in6 *)nam; 2692 sin_p = (struct sockaddr_in *)nam; 2693 in6_sin6_2_sin(sin_p, &sin6); 2694} 2695 2696/* 2697 * Join/leave the solicited multicast groups for proxy NDP entries. 2698 */ 2699static void 2700in6_join_proxy_ndp_mc(struct ifnet *ifp, const struct in6_addr *dst) 2701{ 2702 struct in6_multi *inm; 2703 struct in6_addr mltaddr; 2704 char ip6buf[INET6_ADDRSTRLEN]; 2705 int error; 2706 2707 if (in6_solicited_node_maddr(&mltaddr, ifp, dst) != 0) 2708 return; /* error logged in in6_solicited_node_maddr. */ 2709 2710 error = in6_joingroup(ifp, &mltaddr, NULL, &inm, 0); 2711 if (error != 0) { 2712 nd6log((LOG_WARNING, 2713 "%s: in6_joingroup failed for %s on %s (errno=%d)\n", 2714 __func__, ip6_sprintf(ip6buf, &mltaddr), if_name(ifp), 2715 error)); 2716 } 2717} 2718 2719static void 2720in6_leave_proxy_ndp_mc(struct ifnet *ifp, const struct in6_addr *dst) 2721{ 2722 struct epoch_tracker et; 2723 struct in6_multi *inm; 2724 struct in6_addr mltaddr; 2725 char ip6buf[INET6_ADDRSTRLEN]; 2726 2727 if (in6_solicited_node_maddr(&mltaddr, ifp, dst) != 0) 2728 return; /* error logged in in6_solicited_node_maddr. */ 2729 2730 NET_EPOCH_ENTER(et); 2731 inm = in6m_lookup(ifp, &mltaddr); 2732 NET_EPOCH_EXIT(et); 2733 if (inm != NULL) 2734 in6_leavegroup(inm, NULL); 2735 else 2736 nd6log((LOG_WARNING, "%s: in6m_lookup failed for %s on %s\n", 2737 __func__, ip6_sprintf(ip6buf, &mltaddr), if_name(ifp))); 2738} 2739 2740static bool 2741in6_lle_match_pub(struct lltable *llt, struct llentry *lle, void *farg) 2742{ 2743 return ((lle->la_flags & LLE_PUB) != 0); 2744} 2745 2746void 2747in6_purge_proxy_ndp(struct ifnet *ifp) 2748{ 2749 struct lltable *llt; 2750 bool need_purge; 2751 2752 if (ifp->if_afdata[AF_INET6] == NULL) 2753 return; 2754 2755 llt = LLTABLE6(ifp); 2756 IF_AFDATA_WLOCK(ifp); 2757 need_purge = ((llt->llt_flags & LLT_ADDEDPROXY) != 0); 2758 IF_AFDATA_WUNLOCK(ifp); 2759 2760 /* 2761 * Ever added proxy ndp entries, leave solicited node multicast 2762 * before deleting the llentry. 2763 */ 2764 if (need_purge) 2765 lltable_delete_conditional(llt, in6_lle_match_pub, NULL); 2766} 2767