This commit is contained in:
Adam Ierymenko 2021-05-28 17:08:35 -04:00
commit dee3361c1d
No known key found for this signature in database
GPG Key ID: C8877CF2D7A5D7F3

View File

@ -22,37 +22,43 @@
#include <string.h>
#ifdef __WINDOWS__
#include <ShlObj.h>
#include <WinSock2.h>
#include <Windows.h>
#include <ShlObj.h>
#include <netioapi.h>
#include <iphlpapi.h>
#include <netioapi.h>
#else
#include <sys/types.h>
#include <ifaddrs.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <ifaddrs.h>
#ifdef __LINUX__
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <linux/if_addr.h>
#endif
#endif
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <set>
#include <atomic>
#if defined(__APPLE__) && defined(TARGET_OS_MAC)
#include <net/if.h>
#include <netinet6/in6_var.h>
#include <sys/ioctl.h>
#endif
#include "../node/InetAddress.hpp"
#include "../node/Mutex.hpp"
#include "../node/Utils.hpp"
#include "Phy.hpp"
#include "OSUtils.hpp"
#include "Phy.hpp"
#include <algorithm>
#include <atomic>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
// Period between refreshes of bindings
#define ZT_BINDER_REFRESH_PERIOD 30000
@ -73,27 +79,28 @@ namespace ZeroTier {
* On OSes that do not support local port enumeration or where this is not
* meaningful, this degrades to binding to wildcard.
*/
class Binder
{
class Binder {
private:
struct _Binding
struct _Binding {
_Binding() : udpSock((PhySocket*)0), tcpListenSock((PhySocket*)0)
{
_Binding() : udpSock((PhySocket *)0),tcpListenSock((PhySocket *)0) {}
}
PhySocket* udpSock;
PhySocket* tcpListenSock;
InetAddress address;
};
public:
Binder() : _bindingCount(0) {}
Binder() : _bindingCount(0)
{
}
/**
* Close all bound ports, should be called on shutdown
*
* @param phy Physical interface
*/
template<typename PHY_HANDLER_TYPE>
void closeAll(Phy<PHY_HANDLER_TYPE> &phy)
template <typename PHY_HANDLER_TYPE> void closeAll(Phy<PHY_HANDLER_TYPE>& phy)
{
Mutex::Lock _l(_lock);
for (unsigned int b = 0, c = _bindingCount; b < c; ++b) {
@ -117,8 +124,7 @@ public:
* @tparam PHY_HANDLER_TYPE Type for Phy<> template
* @tparam INTERFACE_CHECKER Type for class containing shouldBindInterface() method
*/
template<typename PHY_HANDLER_TYPE,typename INTERFACE_CHECKER>
void refresh(Phy<PHY_HANDLER_TYPE> &phy,unsigned int *ports,unsigned int portCount,const std::vector<InetAddress> explicitBind,INTERFACE_CHECKER &ifChecker)
template <typename PHY_HANDLER_TYPE, typename INTERFACE_CHECKER> void refresh(Phy<PHY_HANDLER_TYPE>& phy, unsigned int* ports, unsigned int portCount, const std::vector<InetAddress> explicitBind, INTERFACE_CHECKER& ifChecker)
{
std::map<InetAddress, std::string> localIfAddrs;
PhySocket *udps, *tcps;
@ -206,23 +212,32 @@ public:
unsigned char ipbits[16];
memset(ipbits, 0, sizeof(ipbits));
char* devname = (char*)0;
int flags = 0;
int n = 0;
for (char* f = Utils::stok(tmp, " \t\r\n", &saveptr); (f); f = Utils::stok((char*)0, " \t\r\n", &saveptr)) {
switch (n++) {
case 0: // IP in hex
Utils::unhex(f, 32, ipbits, 16);
break;
case 4:
flags = atoi(f);
break;
case 5: // device name
devname = f;
break;
}
}
if ( (flags & IFA_F_TEMPORARY) != 0) {
continue;
}
if (devname) {
ifnames.insert(devname);
InetAddress ip(ipbits, 16, 0);
if (ifChecker.shouldBindInterface(devname, ip)) {
switch (ip.ipScope()) {
default: break;
default:
break;
case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
case InetAddress::IP_SCOPE_GLOBAL:
case InetAddress::IP_SCOPE_SHARED:
@ -246,15 +261,19 @@ public:
configuration.ifc_len = 0;
configuration.ifc_buf = nullptr;
if (controlfd < 0) goto ip4_address_error;
if (ioctl(controlfd, SIOCGIFCONF, &configuration) < 0) goto ip4_address_error;
if (controlfd < 0)
goto ip4_address_error;
if (ioctl(controlfd, SIOCGIFCONF, &configuration) < 0)
goto ip4_address_error;
configuration.ifc_buf = (char*)malloc(configuration.ifc_len);
if (ioctl(controlfd, SIOCGIFCONF, &configuration) < 0) goto ip4_address_error;
if (ioctl(controlfd, SIOCGIFCONF, &configuration) < 0)
goto ip4_address_error;
for (int i = 0; i < (int)(configuration.ifc_len / sizeof(ifreq)); i++) {
struct ifreq& request = configuration.ifc_req[i];
struct sockaddr* addr = &request.ifr_ifru.ifru_addr;
if (addr->sa_family != AF_INET) continue;
if (addr->sa_family != AF_INET)
continue;
std::string ifname = request.ifr_ifrn.ifrn_name;
// name can either be just interface name or interface name followed by ':' and arbitrary label
if (ifname.find(':') != std::string::npos)
@ -263,7 +282,8 @@ public:
InetAddress ip(&(((struct sockaddr_in*)addr)->sin_addr), 4, 0);
if (ifChecker.shouldBindInterface(ifname.c_str(), ip)) {
switch (ip.ipScope()) {
default: break;
default:
break;
case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
case InetAddress::IP_SCOPE_GLOBAL:
case InetAddress::IP_SCOPE_SHARED:
@ -279,7 +299,8 @@ public:
ip4_address_error:
free(configuration.ifc_buf);
if (controlfd > 0) close(controlfd);
if (controlfd > 0)
close(controlfd);
}
const bool gotViaProc = (! localIfAddrs.empty());
@ -290,14 +311,45 @@ public:
if (! gotViaProc) {
struct ifaddrs* ifatbl = (struct ifaddrs*)0;
struct ifaddrs* ifa;
#if defined(__APPLE__)
// set up an IPv6 socket so we can check the state of interfaces via SIOCGIFAFLAG_IN6
int infoSock = socket(AF_INET6, SOCK_DGRAM, 0);
#endif
if ((getifaddrs(&ifatbl) == 0) && (ifatbl)) {
ifa = ifatbl;
while (ifa) {
if ((ifa->ifa_name) && (ifa->ifa_addr)) {
InetAddress ip = *(ifa->ifa_addr);
#if defined(__APPLE__) && defined(TARGET_OS_MAC)
// Check if the address is an IPv6 Temporary Address, macOS version
if (ifa->ifa_addr->sa_family == AF_INET6) {
struct sockaddr_in6* sa6 = (struct sockaddr_in6*)ifa->ifa_addr;
struct in6_ifreq ifr6;
memset(&ifr6, 0, sizeof(ifr6));
strcpy(ifr6.ifr_name, ifa->ifa_name);
ifr6.ifr_ifru.ifru_addr = *sa6;
int flags = 0;
if (ioctl(infoSock, SIOCGIFAFLAG_IN6, (unsigned long long)&ifr6) != -1) {
flags = ifr6.ifr_ifru.ifru_flags6;
}
// if this is a temporary IPv6 address, skip to the next address
if (flags & IN6_IFF_TEMPORARY) {
char buf[64];
#ifdef ZT_TRACE
fprintf(stderr, "skip binding to temporary IPv6 address: %s\n", ip.toIpString(buf));
#endif
ifa = ifa->ifa_next;
continue;
}
}
#endif
if (ifChecker.shouldBindInterface(ifa->ifa_name, ip)) {
switch (ip.ipScope()) {
default: break;
default:
break;
case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
case InetAddress::IP_SCOPE_GLOBAL:
case InetAddress::IP_SCOPE_SHARED:
@ -317,11 +369,15 @@ public:
else {
interfacesEnumerated = false;
}
#if defined(__APPLE__)
close(infoSock);
#endif
}
#endif
#endif
} else {
}
else {
for (std::vector<InetAddress>::const_iterator i(explicitBind.begin()); i != explicitBind.end(); ++i)
localIfAddrs.insert(std::pair<InetAddress, std::string>(*i, std::string()));
}
@ -343,7 +399,8 @@ public:
if (_bindingCount != b)
_bindings[(unsigned int)_bindingCount] = _bindings[b];
++_bindingCount;
} else {
}
else {
PhySocket* const udps = _bindings[b].udpSock;
PhySocket* const tcps = _bindings[b].tcpListenSock;
_bindings[b].udpSock = (PhySocket*)0;
@ -406,7 +463,8 @@ public:
phy.setIfName(udps, (char*)ii->second.c_str(), (int)ii->second.length());
++_bindingCount;
}
} else {
}
else {
phy.close(udps, false);
phy.close(tcps, false);
}
@ -429,15 +487,17 @@ public:
/**
* Send from all bound UDP sockets
*/
template<typename PHY_HANDLER_TYPE>
inline bool udpSendAll(Phy<PHY_HANDLER_TYPE> &phy,const struct sockaddr_storage *addr,const void *data,unsigned int len,unsigned int ttl)
template <typename PHY_HANDLER_TYPE> inline bool udpSendAll(Phy<PHY_HANDLER_TYPE>& phy, const struct sockaddr_storage* addr, const void* data, unsigned int len, unsigned int ttl)
{
bool r = false;
Mutex::Lock _l(_lock);
for (unsigned int b = 0, c = _bindingCount; b < c; ++b) {
if (ttl) phy.setIp4UdpTtl(_bindings[b].udpSock,ttl);
if (phy.udpSend(_bindings[b].udpSock,(const struct sockaddr *)addr,data,len)) r = true;
if (ttl) phy.setIp4UdpTtl(_bindings[b].udpSock,255);
if (ttl)
phy.setIp4UdpTtl(_bindings[b].udpSock, ttl);
if (phy.udpSend(_bindings[b].udpSock, (const struct sockaddr*)addr, data, len))
r = true;
if (ttl)
phy.setIp4UdpTtl(_bindings[b].udpSock, 255);
}
return r;
}
@ -478,7 +538,6 @@ public:
}
private:
std::set<std::string> linkIfNames;
_Binding _bindings[ZT_BINDER_MAX_BINDINGS];
std::atomic<unsigned int> _bindingCount;