2016-04-05 01:46:41 +00:00
|
|
|
/*
|
2020-05-12 08:35:48 +00:00
|
|
|
* Copyright (c)2013-2020 ZeroTier, Inc.
|
2016-04-05 01:46:41 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
2016-04-05 01:46:41 +00:00
|
|
|
*
|
2020-08-20 19:51:39 +00:00
|
|
|
* Change Date: 2025-01-01
|
2016-04-05 01:46:41 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* On the date above, in accordance with the Business Source License, use
|
|
|
|
* of this software will be governed by version 2.0 of the Apache License.
|
2016-04-05 01:46:41 +00:00
|
|
|
*/
|
2019-08-23 16:23:39 +00:00
|
|
|
/****/
|
2016-04-05 01:46:41 +00:00
|
|
|
|
|
|
|
#ifndef ZT_BINDER_HPP
|
|
|
|
#define ZT_BINDER_HPP
|
|
|
|
|
|
|
|
#include "../node/Constants.hpp"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
2021-05-26 18:01:44 +00:00
|
|
|
#include <ShlObj.h>
|
2016-04-05 01:46:41 +00:00
|
|
|
#include <WinSock2.h>
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <iphlpapi.h>
|
2021-05-26 18:01:44 +00:00
|
|
|
#include <netioapi.h>
|
2016-04-05 01:46:41 +00:00
|
|
|
#else
|
2021-05-26 18:01:44 +00:00
|
|
|
#include <ifaddrs.h>
|
2016-04-05 01:46:41 +00:00
|
|
|
#include <sys/socket.h>
|
2021-05-26 18:01:44 +00:00
|
|
|
#include <sys/types.h>
|
2016-04-05 01:46:41 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
2016-06-21 19:32:58 +00:00
|
|
|
#ifdef __LINUX__
|
|
|
|
#include <net/if.h>
|
2021-05-26 18:01:44 +00:00
|
|
|
#include <sys/ioctl.h>
|
2021-05-26 22:50:14 +00:00
|
|
|
#include <linux/if_addr.h>
|
2016-06-21 19:32:58 +00:00
|
|
|
#endif
|
2016-04-05 01:46:41 +00:00
|
|
|
#endif
|
|
|
|
|
2021-05-27 20:43:12 +00:00
|
|
|
#if defined(__unix__) && !defined(__LINUX__)
|
2021-05-26 18:01:44 +00:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet6/in6_var.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
2016-04-05 01:46:41 +00:00
|
|
|
|
|
|
|
#include "../node/InetAddress.hpp"
|
|
|
|
#include "../node/Mutex.hpp"
|
2016-12-12 20:53:51 +00:00
|
|
|
#include "../node/Utils.hpp"
|
|
|
|
#include "OSUtils.hpp"
|
2021-05-26 18:01:44 +00:00
|
|
|
#include "Phy.hpp"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2016-04-05 01:46:41 +00:00
|
|
|
|
2017-06-06 23:11:19 +00:00
|
|
|
// Period between refreshes of bindings
|
2017-02-22 01:55:00 +00:00
|
|
|
#define ZT_BINDER_REFRESH_PERIOD 30000
|
|
|
|
|
2017-07-13 22:08:57 +00:00
|
|
|
// Max number of bindings
|
2018-01-09 17:05:22 +00:00
|
|
|
#define ZT_BINDER_MAX_BINDINGS 256
|
2017-07-13 22:08:57 +00:00
|
|
|
|
2016-04-05 01:46:41 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
2016-04-05 18:59:46 +00:00
|
|
|
* Enumerates local devices and binds to all potential ZeroTier path endpoints
|
2016-04-05 01:46:41 +00:00
|
|
|
*
|
|
|
|
* This replaces binding to wildcard (0.0.0.0 and ::0) with explicit binding
|
|
|
|
* as part of the path to default gateway support. Under the hood it uses
|
|
|
|
* different queries on different OSes to enumerate devices, and also exposes
|
|
|
|
* device enumeration and endpoint IP data for use elsewhere.
|
|
|
|
*
|
|
|
|
* On OSes that do not support local port enumeration or where this is not
|
|
|
|
* meaningful, this degrades to binding to wildcard.
|
|
|
|
*/
|
2021-05-26 18:01:44 +00:00
|
|
|
class Binder {
|
|
|
|
private:
|
|
|
|
struct _Binding {
|
|
|
|
_Binding() : udpSock((PhySocket*)0), tcpListenSock((PhySocket*)0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
PhySocket* udpSock;
|
|
|
|
PhySocket* tcpListenSock;
|
2016-04-05 01:46:41 +00:00
|
|
|
InetAddress address;
|
|
|
|
};
|
|
|
|
|
2021-05-26 18:01:44 +00:00
|
|
|
public:
|
|
|
|
Binder() : _bindingCount(0)
|
|
|
|
{
|
|
|
|
}
|
2016-04-05 01:46:41 +00:00
|
|
|
|
|
|
|
/**
|
2017-06-06 23:11:19 +00:00
|
|
|
* Close all bound ports, should be called on shutdown
|
2016-04-05 22:44:08 +00:00
|
|
|
*
|
|
|
|
* @param phy Physical interface
|
2016-04-05 01:46:41 +00:00
|
|
|
*/
|
2021-05-26 18:01:44 +00:00
|
|
|
template <typename PHY_HANDLER_TYPE> void closeAll(Phy<PHY_HANDLER_TYPE>& phy)
|
2016-04-05 01:46:41 +00:00
|
|
|
{
|
2016-04-05 22:44:08 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
2021-05-26 18:01:44 +00:00
|
|
|
for (unsigned int b = 0, c = _bindingCount; b < c; ++b) {
|
|
|
|
phy.close(_bindings[b].udpSock, false);
|
|
|
|
phy.close(_bindings[b].tcpListenSock, false);
|
2016-04-05 18:59:46 +00:00
|
|
|
}
|
2017-07-13 22:08:57 +00:00
|
|
|
_bindingCount = 0;
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scan local devices and addresses and rebind TCP and UDP
|
|
|
|
*
|
|
|
|
* This should be called after wake from sleep, on detected network device
|
|
|
|
* changes, on startup, or periodically (e.g. every 30-60s).
|
|
|
|
*
|
2016-04-05 22:44:08 +00:00
|
|
|
* @param phy Physical interface
|
2017-06-06 23:11:19 +00:00
|
|
|
* @param ports Ports to bind on all interfaces
|
2018-01-25 19:59:11 +00:00
|
|
|
* @param portCount Number of ports
|
2018-01-25 22:27:08 +00:00
|
|
|
* @param explicitBind If present, override interface IP detection and bind to these (if possible)
|
2018-01-25 19:59:11 +00:00
|
|
|
* @param ifChecker Interface checker function to see if an interface should be used
|
2016-04-05 22:44:08 +00:00
|
|
|
* @tparam PHY_HANDLER_TYPE Type for Phy<> template
|
|
|
|
* @tparam INTERFACE_CHECKER Type for class containing shouldBindInterface() method
|
2016-04-05 01:46:41 +00:00
|
|
|
*/
|
2021-05-26 18:01:44 +00:00
|
|
|
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)
|
2016-04-05 01:46:41 +00:00
|
|
|
{
|
2021-05-26 18:01:44 +00:00
|
|
|
std::map<InetAddress, std::string> localIfAddrs;
|
|
|
|
PhySocket *udps, *tcps;
|
2016-04-05 01:46:41 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
2018-01-10 20:41:52 +00:00
|
|
|
bool interfacesEnumerated = true;
|
2016-04-05 01:46:41 +00:00
|
|
|
|
2018-01-25 22:27:08 +00:00
|
|
|
if (explicitBind.empty()) {
|
2016-04-05 01:46:41 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
|
2018-01-25 19:59:11 +00:00
|
|
|
char aabuf[32768];
|
|
|
|
ULONG aalen = sizeof(aabuf);
|
2021-05-26 18:01:44 +00:00
|
|
|
if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER, (void*)0, reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf), &aalen) == NO_ERROR) {
|
2018-01-25 19:59:11 +00:00
|
|
|
PIP_ADAPTER_ADDRESSES a = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf);
|
|
|
|
while (a) {
|
|
|
|
PIP_ADAPTER_UNICAST_ADDRESS ua = a->FirstUnicastAddress;
|
|
|
|
while (ua) {
|
2021-05-26 16:15:24 +00:00
|
|
|
// Don't bind temporary/random IPv6 addresses
|
|
|
|
if (ua->SuffixOrigin != IpSuffixOriginRandom) {
|
|
|
|
InetAddress ip(ua->Address.lpSockaddr);
|
|
|
|
char strBuf[128] = { 0 };
|
|
|
|
wcstombs(strBuf, a->FriendlyName, sizeof(strBuf));
|
|
|
|
if (ifChecker.shouldBindInterface(strBuf, ip)) {
|
|
|
|
switch (ip.ipScope()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
|
|
|
|
case InetAddress::IP_SCOPE_GLOBAL:
|
|
|
|
case InetAddress::IP_SCOPE_SHARED:
|
|
|
|
case InetAddress::IP_SCOPE_PRIVATE:
|
|
|
|
for (int x = 0; x < (int)portCount; ++x) {
|
|
|
|
ip.setPort(ports[x]);
|
|
|
|
localIfAddrs.insert(std::pair<InetAddress, std::string>(ip, std::string()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-01-25 19:59:11 +00:00
|
|
|
}
|
2016-04-05 22:53:19 +00:00
|
|
|
}
|
2018-01-25 19:59:11 +00:00
|
|
|
ua = ua->Next;
|
2016-04-05 22:53:19 +00:00
|
|
|
}
|
2018-01-25 19:59:11 +00:00
|
|
|
a = a->Next;
|
2016-04-05 22:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-25 19:59:11 +00:00
|
|
|
else {
|
|
|
|
interfacesEnumerated = false;
|
|
|
|
}
|
2016-04-05 22:53:19 +00:00
|
|
|
|
2021-05-26 18:01:44 +00:00
|
|
|
#else // not __WINDOWS__
|
2016-04-05 01:46:41 +00:00
|
|
|
|
2018-01-25 19:59:11 +00:00
|
|
|
/* On Linux we use an alternative method if available since getifaddrs()
|
|
|
|
* gets very slow when there are lots of network namespaces. This won't
|
|
|
|
* work unless /proc/PID/net/if_inet6 exists and it may not on some
|
|
|
|
* embedded systems, so revert to getifaddrs() there. */
|
2016-12-12 20:53:51 +00:00
|
|
|
|
|
|
|
#ifdef __LINUX__
|
2021-05-26 18:01:44 +00:00
|
|
|
char fn[256], tmp[256];
|
2018-01-25 19:59:11 +00:00
|
|
|
std::set<std::string> ifnames;
|
|
|
|
const unsigned long pid = (unsigned long)getpid();
|
2016-12-12 20:53:51 +00:00
|
|
|
|
2018-01-25 19:59:11 +00:00
|
|
|
// Get all device names
|
2021-05-26 18:01:44 +00:00
|
|
|
OSUtils::ztsnprintf(fn, sizeof(fn), "/proc/%lu/net/dev", pid);
|
|
|
|
FILE* procf = fopen(fn, "r");
|
2018-01-25 19:59:11 +00:00
|
|
|
if (procf) {
|
2021-05-26 18:01:44 +00:00
|
|
|
while (fgets(tmp, sizeof(tmp), procf)) {
|
2018-01-25 19:59:11 +00:00
|
|
|
tmp[255] = 0;
|
2021-05-26 18:01:44 +00:00
|
|
|
char* saveptr = (char*)0;
|
|
|
|
for (char* f = Utils::stok(tmp, " \t\r\n:|", &saveptr); (f); f = Utils::stok((char*)0, " \t\r\n:|", &saveptr)) {
|
|
|
|
if ((strcmp(f, "Inter-") != 0) && (strcmp(f, "face") != 0) && (f[0] != 0))
|
2018-01-25 19:59:11 +00:00
|
|
|
ifnames.insert(f);
|
2021-05-26 18:01:44 +00:00
|
|
|
break; // we only want the first field
|
2018-01-25 19:59:11 +00:00
|
|
|
}
|
2016-12-12 20:53:51 +00:00
|
|
|
}
|
2018-01-25 19:59:11 +00:00
|
|
|
fclose(procf);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
interfacesEnumerated = false;
|
2016-12-12 20:53:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 19:59:11 +00:00
|
|
|
// Get IPv6 addresses (and any device names we don't already know)
|
2021-05-26 18:01:44 +00:00
|
|
|
OSUtils::ztsnprintf(fn, sizeof(fn), "/proc/%lu/net/if_inet6", pid);
|
|
|
|
procf = fopen(fn, "r");
|
2018-01-25 19:59:11 +00:00
|
|
|
if (procf) {
|
2021-05-26 18:01:44 +00:00
|
|
|
while (fgets(tmp, sizeof(tmp), procf)) {
|
2018-01-25 19:59:11 +00:00
|
|
|
tmp[255] = 0;
|
2021-05-26 18:01:44 +00:00
|
|
|
char* saveptr = (char*)0;
|
2018-01-25 19:59:11 +00:00
|
|
|
unsigned char ipbits[16];
|
2021-05-26 18:01:44 +00:00
|
|
|
memset(ipbits, 0, sizeof(ipbits));
|
|
|
|
char* devname = (char*)0;
|
2021-05-26 22:50:14 +00:00
|
|
|
int flags = 0;
|
2018-01-25 19:59:11 +00:00
|
|
|
int n = 0;
|
2021-05-26 18:01:44 +00:00
|
|
|
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);
|
2018-01-25 19:59:11 +00:00
|
|
|
break;
|
2021-05-26 22:50:14 +00:00
|
|
|
case 4:
|
|
|
|
flags = atoi(f);
|
|
|
|
break;
|
2021-05-26 18:01:44 +00:00
|
|
|
case 5: // device name
|
2018-01-25 19:59:11 +00:00
|
|
|
devname = f;
|
2016-04-05 22:44:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
2021-05-26 22:50:14 +00:00
|
|
|
|
|
|
|
if ( (flags & IFA_F_TEMPORARY) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-25 19:59:11 +00:00
|
|
|
if (devname) {
|
|
|
|
ifnames.insert(devname);
|
2021-05-26 18:01:44 +00:00
|
|
|
InetAddress ip(ipbits, 16, 0);
|
|
|
|
if (ifChecker.shouldBindInterface(devname, ip)) {
|
|
|
|
switch (ip.ipScope()) {
|
|
|
|
default:
|
|
|
|
break;
|
2018-01-25 19:59:11 +00:00
|
|
|
case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
|
|
|
|
case InetAddress::IP_SCOPE_GLOBAL:
|
|
|
|
case InetAddress::IP_SCOPE_SHARED:
|
|
|
|
case InetAddress::IP_SCOPE_PRIVATE:
|
2021-05-26 18:01:44 +00:00
|
|
|
for (int x = 0; x < (int)portCount; ++x) {
|
2018-01-25 19:59:11 +00:00
|
|
|
ip.setPort(ports[x]);
|
2021-05-26 18:01:44 +00:00
|
|
|
localIfAddrs.insert(std::pair<InetAddress, std::string>(ip, std::string(devname)));
|
2018-01-25 19:59:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
2018-01-25 19:59:11 +00:00
|
|
|
fclose(procf);
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
2016-12-12 20:53:51 +00:00
|
|
|
|
2018-01-25 19:59:11 +00:00
|
|
|
// Get IPv4 addresses for each device
|
2021-05-26 18:01:44 +00:00
|
|
|
if (! ifnames.empty()) {
|
|
|
|
const int controlfd = (int)socket(AF_INET, SOCK_DGRAM, 0);
|
2018-01-25 19:59:11 +00:00
|
|
|
struct ifconf configuration;
|
|
|
|
configuration.ifc_len = 0;
|
|
|
|
configuration.ifc_buf = nullptr;
|
2017-01-01 10:49:35 +00:00
|
|
|
|
2021-05-26 18:01:44 +00:00
|
|
|
if (controlfd < 0)
|
|
|
|
goto ip4_address_error;
|
|
|
|
if (ioctl(controlfd, SIOCGIFCONF, &configuration) < 0)
|
|
|
|
goto ip4_address_error;
|
2018-01-25 19:59:11 +00:00
|
|
|
configuration.ifc_buf = (char*)malloc(configuration.ifc_len);
|
2021-05-26 18:01:44 +00:00
|
|
|
if (ioctl(controlfd, SIOCGIFCONF, &configuration) < 0)
|
|
|
|
goto ip4_address_error;
|
2017-01-01 10:49:35 +00:00
|
|
|
|
2021-05-26 18:01:44 +00:00
|
|
|
for (int i = 0; i < (int)(configuration.ifc_len / sizeof(ifreq)); i++) {
|
2018-01-25 19:59:11 +00:00
|
|
|
struct ifreq& request = configuration.ifc_req[i];
|
|
|
|
struct sockaddr* addr = &request.ifr_ifru.ifru_addr;
|
2021-05-26 18:01:44 +00:00
|
|
|
if (addr->sa_family != AF_INET)
|
|
|
|
continue;
|
2018-01-25 19:59:11 +00:00
|
|
|
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)
|
|
|
|
ifname = ifname.substr(0, ifname.find(':'));
|
2017-01-01 10:49:35 +00:00
|
|
|
|
2021-05-26 18:01:44 +00:00
|
|
|
InetAddress ip(&(((struct sockaddr_in*)addr)->sin_addr), 4, 0);
|
2018-01-25 19:59:11 +00:00
|
|
|
if (ifChecker.shouldBindInterface(ifname.c_str(), ip)) {
|
2021-05-26 18:01:44 +00:00
|
|
|
switch (ip.ipScope()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
|
|
|
|
case InetAddress::IP_SCOPE_GLOBAL:
|
|
|
|
case InetAddress::IP_SCOPE_SHARED:
|
|
|
|
case InetAddress::IP_SCOPE_PRIVATE:
|
|
|
|
for (int x = 0; x < (int)portCount; ++x) {
|
|
|
|
ip.setPort(ports[x]);
|
|
|
|
localIfAddrs.insert(std::pair<InetAddress, std::string>(ip, ifname));
|
|
|
|
}
|
|
|
|
break;
|
2017-06-06 23:11:19 +00:00
|
|
|
}
|
2016-12-12 20:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-01 10:49:35 +00:00
|
|
|
|
2018-01-25 19:59:11 +00:00
|
|
|
ip4_address_error:
|
|
|
|
free(configuration.ifc_buf);
|
2021-05-26 18:01:44 +00:00
|
|
|
if (controlfd > 0)
|
|
|
|
close(controlfd);
|
2018-01-25 19:59:11 +00:00
|
|
|
}
|
2016-12-12 20:53:51 +00:00
|
|
|
|
2021-05-26 18:01:44 +00:00
|
|
|
const bool gotViaProc = (! localIfAddrs.empty());
|
2016-12-12 20:53:51 +00:00
|
|
|
#else
|
2018-01-25 19:59:11 +00:00
|
|
|
const bool gotViaProc = false;
|
2016-12-12 20:53:51 +00:00
|
|
|
#endif
|
2021-05-26 18:01:44 +00:00
|
|
|
#if ! defined(ZT_SDK) || ! defined(__ANDROID__) // getifaddrs() freeifaddrs() not available on Android
|
|
|
|
if (! gotViaProc) {
|
|
|
|
struct ifaddrs* ifatbl = (struct ifaddrs*)0;
|
|
|
|
struct ifaddrs* ifa;
|
2021-05-27 20:43:12 +00:00
|
|
|
#if defined(__unix__) && !defined(__LINUX__)
|
2021-05-26 18:01:44 +00:00
|
|
|
// 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)) {
|
2018-01-25 19:59:11 +00:00
|
|
|
ifa = ifatbl;
|
|
|
|
while (ifa) {
|
2021-05-26 18:01:44 +00:00
|
|
|
if ((ifa->ifa_name) && (ifa->ifa_addr)) {
|
2018-01-25 19:59:11 +00:00
|
|
|
InetAddress ip = *(ifa->ifa_addr);
|
2021-10-27 21:47:01 +00:00
|
|
|
#if (defined(__unix__) || defined(__APPLE__)) && !defined(__LINUX__)
|
2021-05-27 20:43:12 +00:00
|
|
|
// Check if the address is an IPv6 Temporary Address, macOS/BSD version
|
2021-05-26 18:01:44 +00:00
|
|
|
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;
|
2018-01-25 19:59:11 +00:00
|
|
|
case InetAddress::IP_SCOPE_PSEUDOPRIVATE:
|
|
|
|
case InetAddress::IP_SCOPE_GLOBAL:
|
|
|
|
case InetAddress::IP_SCOPE_SHARED:
|
|
|
|
case InetAddress::IP_SCOPE_PRIVATE:
|
2021-05-26 18:01:44 +00:00
|
|
|
for (int x = 0; x < (int)portCount; ++x) {
|
2018-01-25 19:59:11 +00:00
|
|
|
ip.setPort(ports[x]);
|
2021-05-26 18:01:44 +00:00
|
|
|
localIfAddrs.insert(std::pair<InetAddress, std::string>(ip, std::string(ifa->ifa_name)));
|
2018-01-25 19:59:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-12-12 20:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-25 19:59:11 +00:00
|
|
|
ifa = ifa->ifa_next;
|
2016-12-12 20:53:51 +00:00
|
|
|
}
|
2018-01-25 19:59:11 +00:00
|
|
|
freeifaddrs(ifatbl);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
interfacesEnumerated = false;
|
2016-12-12 20:53:51 +00:00
|
|
|
}
|
2021-05-27 20:43:12 +00:00
|
|
|
#if defined(__unix__) && !defined(__LINUX__)
|
2021-05-26 18:01:44 +00:00
|
|
|
close(infoSock);
|
|
|
|
#endif
|
2018-01-10 20:41:52 +00:00
|
|
|
}
|
2018-07-20 00:50:10 +00:00
|
|
|
#endif
|
2016-04-05 01:46:41 +00:00
|
|
|
|
|
|
|
#endif
|
2021-05-26 18:01:44 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-06-29 18:22:46 +00:00
|
|
|
for (std::vector<InetAddress>::const_iterator i(explicitBind.begin()); i != explicitBind.end(); ++i) {
|
2020-09-07 23:34:38 +00:00
|
|
|
InetAddress ip = InetAddress(*i);
|
2021-06-29 18:22:46 +00:00
|
|
|
for (int x = 0; x < (int)portCount; ++x) {
|
2020-09-07 23:34:38 +00:00
|
|
|
ip.setPort(ports[x]);
|
2021-06-29 18:22:46 +00:00
|
|
|
localIfAddrs.insert(std::pair<InetAddress, std::string>(ip, std::string()));
|
2020-09-07 23:34:38 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-25 19:59:11 +00:00
|
|
|
}
|
2016-04-05 01:46:41 +00:00
|
|
|
|
2016-04-05 17:47:13 +00:00
|
|
|
// Default to binding to wildcard if we can't enumerate addresses
|
2021-05-26 18:01:44 +00:00
|
|
|
if (! interfacesEnumerated && localIfAddrs.empty()) {
|
|
|
|
for (int x = 0; x < (int)portCount; ++x) {
|
|
|
|
localIfAddrs.insert(std::pair<InetAddress, std::string>(InetAddress((uint32_t)0, ports[x]), std::string()));
|
|
|
|
localIfAddrs.insert(std::pair<InetAddress, std::string>(InetAddress((const void*)"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16, ports[x]), std::string()));
|
2017-06-06 23:11:19 +00:00
|
|
|
}
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 22:08:57 +00:00
|
|
|
const unsigned int oldBindingCount = _bindingCount;
|
|
|
|
_bindingCount = 0;
|
2017-06-06 23:11:19 +00:00
|
|
|
|
|
|
|
// Save bindings that are still valid, close those that are not
|
2021-05-26 18:01:44 +00:00
|
|
|
for (unsigned int b = 0; b < oldBindingCount; ++b) {
|
2017-07-13 22:08:57 +00:00
|
|
|
if (localIfAddrs.find(_bindings[b].address) != localIfAddrs.end()) {
|
|
|
|
if (_bindingCount != b)
|
|
|
|
_bindings[(unsigned int)_bindingCount] = _bindings[b];
|
|
|
|
++_bindingCount;
|
2021-05-26 18:01:44 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PhySocket* const udps = _bindings[b].udpSock;
|
|
|
|
PhySocket* const tcps = _bindings[b].tcpListenSock;
|
|
|
|
_bindings[b].udpSock = (PhySocket*)0;
|
|
|
|
_bindings[b].tcpListenSock = (PhySocket*)0;
|
|
|
|
phy.close(udps, false);
|
|
|
|
phy.close(tcps, false);
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 23:11:19 +00:00
|
|
|
// Create new bindings for those not already bound
|
2021-05-26 18:01:44 +00:00
|
|
|
for (std::map<InetAddress, std::string>::const_iterator ii(localIfAddrs.begin()); ii != localIfAddrs.end(); ++ii) {
|
2017-07-13 22:08:57 +00:00
|
|
|
unsigned int bi = 0;
|
|
|
|
while (bi != _bindingCount) {
|
|
|
|
if (_bindings[bi].address == ii->first)
|
2016-04-05 01:46:41 +00:00
|
|
|
break;
|
2016-06-21 19:32:58 +00:00
|
|
|
++bi;
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
2017-07-13 22:08:57 +00:00
|
|
|
if (bi == _bindingCount) {
|
2021-05-26 18:01:44 +00:00
|
|
|
udps = phy.udpBind(reinterpret_cast<const struct sockaddr*>(&(ii->first)), (void*)0, ZT_UDP_DESIRED_BUF_SIZE);
|
|
|
|
tcps = phy.tcpListen(reinterpret_cast<const struct sockaddr*>(&(ii->first)), (void*)0);
|
|
|
|
if ((udps) && (tcps)) {
|
2016-06-21 19:32:58 +00:00
|
|
|
#ifdef __LINUX__
|
2018-11-11 22:23:24 +00:00
|
|
|
// Bind Linux sockets to their device so routes that we manage do not override physical routes (wish all platforms had this!)
|
2017-06-06 23:11:19 +00:00
|
|
|
if (ii->second.length() > 0) {
|
|
|
|
char tmp[256];
|
2021-05-26 18:01:44 +00:00
|
|
|
Utils::scopy(tmp, sizeof(tmp), ii->second.c_str());
|
2017-06-06 23:11:19 +00:00
|
|
|
int fd = (int)Phy<PHY_HANDLER_TYPE>::getDescriptor(udps);
|
|
|
|
if (fd >= 0)
|
2021-05-26 18:01:44 +00:00
|
|
|
setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, tmp, strlen(tmp));
|
2017-06-06 23:11:19 +00:00
|
|
|
fd = (int)Phy<PHY_HANDLER_TYPE>::getDescriptor(tcps);
|
|
|
|
if (fd >= 0)
|
2021-05-26 18:01:44 +00:00
|
|
|
setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, tmp, strlen(tmp));
|
2017-06-06 23:11:19 +00:00
|
|
|
}
|
2021-05-26 18:01:44 +00:00
|
|
|
#endif // __LINUX__
|
2017-07-13 22:08:57 +00:00
|
|
|
if (_bindingCount < ZT_BINDER_MAX_BINDINGS) {
|
|
|
|
_bindings[_bindingCount].udpSock = udps;
|
|
|
|
_bindings[_bindingCount].tcpListenSock = tcps;
|
|
|
|
_bindings[_bindingCount].address = ii->first;
|
2021-05-26 18:01:44 +00:00
|
|
|
phy.setIfName(udps, (char*)ii->second.c_str(), (int)ii->second.length());
|
2017-07-13 22:08:57 +00:00
|
|
|
++_bindingCount;
|
|
|
|
}
|
2021-05-26 18:01:44 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
phy.close(udps, false);
|
|
|
|
phy.close(tcps, false);
|
2016-04-05 01:46:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 22:53:19 +00:00
|
|
|
/**
|
|
|
|
* @return All currently bound local interface addresses
|
|
|
|
*/
|
2017-06-06 23:11:19 +00:00
|
|
|
inline std::vector<InetAddress> allBoundLocalInterfaceAddresses() const
|
2016-04-05 22:53:19 +00:00
|
|
|
{
|
|
|
|
std::vector<InetAddress> aa;
|
2017-06-05 19:15:28 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
2021-05-26 18:01:44 +00:00
|
|
|
for (unsigned int b = 0, c = _bindingCount; b < c; ++b)
|
2017-07-13 22:08:57 +00:00
|
|
|
aa.push_back(_bindings[b].address);
|
2017-06-06 23:11:19 +00:00
|
|
|
return aa;
|
2017-06-05 19:15:28 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 18:45:22 +00:00
|
|
|
/**
|
|
|
|
* Send from all bound UDP sockets
|
|
|
|
*/
|
2021-05-26 18:01:44 +00:00
|
|
|
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)
|
2017-07-06 18:45:22 +00:00
|
|
|
{
|
|
|
|
bool r = false;
|
|
|
|
Mutex::Lock _l(_lock);
|
2021-05-26 18:01:44 +00:00
|
|
|
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);
|
2017-07-06 18:45:22 +00:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-06-08 00:19:33 +00:00
|
|
|
/**
|
|
|
|
* @param addr Address to check
|
|
|
|
* @return True if this is a bound local interface address
|
|
|
|
*/
|
2021-05-26 18:01:44 +00:00
|
|
|
inline bool isBoundLocalInterfaceAddress(const InetAddress& addr) const
|
2017-06-08 00:19:33 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_lock);
|
2021-05-26 18:01:44 +00:00
|
|
|
for (unsigned int b = 0; b < _bindingCount; ++b) {
|
2017-07-13 22:08:57 +00:00
|
|
|
if (_bindings[b].address == addr)
|
2017-06-08 00:19:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-13 22:08:57 +00:00
|
|
|
/**
|
|
|
|
* Quickly check that a UDP socket is valid
|
|
|
|
*
|
|
|
|
* @param udpSock UDP socket to check
|
|
|
|
* @return True if socket is currently bound/allocated
|
|
|
|
*/
|
2021-05-26 18:01:44 +00:00
|
|
|
inline bool isUdpSocketValid(PhySocket* const udpSock)
|
2017-07-13 22:08:57 +00:00
|
|
|
{
|
2021-05-26 18:01:44 +00:00
|
|
|
for (unsigned int b = 0, c = _bindingCount; b < c; ++b) {
|
2017-07-13 22:08:57 +00:00
|
|
|
if (_bindings[b].udpSock == udpSock)
|
2021-05-26 18:01:44 +00:00
|
|
|
return (b < _bindingCount); // double check atomic which may have changed
|
2017-07-13 22:08:57 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-26 18:01:44 +00:00
|
|
|
private:
|
2017-07-13 22:08:57 +00:00
|
|
|
_Binding _bindings[ZT_BINDER_MAX_BINDINGS];
|
|
|
|
std::atomic<unsigned int> _bindingCount;
|
2016-04-05 01:46:41 +00:00
|
|
|
Mutex _lock;
|
|
|
|
};
|
|
|
|
|
2021-05-26 18:01:44 +00:00
|
|
|
} // namespace ZeroTier
|
2016-04-05 01:46:41 +00:00
|
|
|
|
|
|
|
#endif
|