2019-08-07 23:14:12 +00:00
|
|
|
/*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Copyright (c)2019 ZeroTier, Inc.
|
2019-08-07 23:14:12 +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.
|
2019-08-07 23:14:12 +00:00
|
|
|
*
|
2024-03-19 21:38:48 +00:00
|
|
|
* Change Date: 2026-01-01
|
2019-08-07 23:14:12 +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.
|
2019-08-07 23:14:12 +00:00
|
|
|
*/
|
2019-08-23 16:23:39 +00:00
|
|
|
/****/
|
2019-08-07 23:14:12 +00:00
|
|
|
|
|
|
|
#include "EthernetTap.hpp"
|
|
|
|
#include "OSUtils.hpp"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-08-08 02:47:06 +00:00
|
|
|
#ifdef ZT_SDK
|
|
|
|
|
|
|
|
#include "../controller/EmbeddedNetworkController.hpp"
|
|
|
|
#include "../node/Node.hpp"
|
|
|
|
#include "../include/VirtualTap.hpp"
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2019-08-07 23:14:12 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include "MacEthernetTap.hpp"
|
|
|
|
#include "MacKextEthernetTap.hpp"
|
|
|
|
#endif // __APPLE__
|
|
|
|
|
|
|
|
#ifdef __LINUX__
|
|
|
|
#include "LinuxEthernetTap.hpp"
|
|
|
|
#endif // __LINUX__
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
#include "WindowsEthernetTap.hpp"
|
|
|
|
#endif // __WINDOWS__
|
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include "BSDEthernetTap.hpp"
|
|
|
|
#endif // __FreeBSD__
|
|
|
|
|
|
|
|
#ifdef __NetBSD__
|
|
|
|
#include "NetBSDEthernetTap.hpp"
|
|
|
|
#endif // __NetBSD__
|
|
|
|
|
|
|
|
#ifdef __OpenBSD__
|
|
|
|
#include "BSDEthernetTap.hpp"
|
|
|
|
#endif // __OpenBSD__
|
|
|
|
|
2019-08-07 23:45:04 +00:00
|
|
|
#endif
|
|
|
|
|
2019-08-07 23:14:12 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
std::shared_ptr<EthernetTap> EthernetTap::newInstance(
|
|
|
|
const char *tapDeviceType, // OS-specific, NULL for default
|
2024-08-21 21:06:25 +00:00
|
|
|
unsigned int concurrency,
|
|
|
|
bool pinning,
|
2019-08-07 23:14:12 +00:00
|
|
|
const char *homePath,
|
|
|
|
const MAC &mac,
|
|
|
|
unsigned int mtu,
|
|
|
|
unsigned int metric,
|
|
|
|
uint64_t nwid,
|
|
|
|
const char *friendlyName,
|
|
|
|
void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
|
|
|
|
void *arg)
|
|
|
|
{
|
2019-08-07 23:45:04 +00:00
|
|
|
|
|
|
|
#ifdef ZT_SDK
|
2019-08-08 02:47:06 +00:00
|
|
|
|
2019-08-07 23:45:04 +00:00
|
|
|
return std::shared_ptr<EthernetTap>(new VirtualTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
|
2019-08-08 02:47:06 +00:00
|
|
|
|
2019-08-07 23:45:04 +00:00
|
|
|
#else // not ZT_SDK
|
|
|
|
|
2019-08-07 23:14:12 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
char osrelease[256];
|
|
|
|
size_t size = sizeof(osrelease);
|
|
|
|
if (sysctlbyname("kern.osrelease",osrelease,&size,nullptr,0) == 0) {
|
|
|
|
char *dotAt = strchr(osrelease,'.');
|
|
|
|
if (dotAt) {
|
|
|
|
*dotAt = (char)0;
|
|
|
|
// The "feth" virtual Ethernet device type appeared in Darwin 17.x.x. Older versions
|
|
|
|
// (Sierra and earlier) must use the a kernel extension.
|
|
|
|
if (strtol(osrelease,(char **)0,10) < 17) {
|
2024-07-03 15:49:07 +00:00
|
|
|
return std::shared_ptr<EthernetTap>(new MacKextEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
|
2019-08-07 23:14:12 +00:00
|
|
|
} else {
|
2024-07-03 15:49:07 +00:00
|
|
|
return std::shared_ptr<EthernetTap>(new MacEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
|
2019-08-07 23:14:12 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-21 18:22:07 +00:00
|
|
|
}
|
2019-08-07 23:14:12 +00:00
|
|
|
#endif // __APPLE__
|
|
|
|
|
|
|
|
#ifdef __LINUX__
|
2024-08-21 21:06:25 +00:00
|
|
|
return std::shared_ptr<EthernetTap>(new LinuxEthernetTap(homePath,concurrency,pinning,mac,mtu,metric,nwid,friendlyName,handler,arg));
|
2019-08-07 23:14:12 +00:00
|
|
|
#endif // __LINUX__
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
2020-11-19 22:10:34 +00:00
|
|
|
HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
|
|
|
|
if (FAILED(hres)) {
|
|
|
|
throw std::runtime_error("WinEthernetTap: COM initialization failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool _comInit = false;
|
|
|
|
static Mutex _comInit_m;
|
|
|
|
|
|
|
|
{
|
|
|
|
Mutex::Lock l(_comInit_m);
|
|
|
|
if (!_comInit) {
|
|
|
|
hres = CoInitializeSecurity(
|
|
|
|
NULL,
|
|
|
|
-1,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2020-11-26 04:06:43 +00:00
|
|
|
RPC_C_AUTHN_LEVEL_PKT,
|
2020-11-19 22:10:34 +00:00
|
|
|
RPC_C_IMP_LEVEL_IMPERSONATE,
|
|
|
|
NULL,
|
|
|
|
EOAC_NONE,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
if (FAILED(hres)) {
|
|
|
|
CoUninitialize();
|
2020-11-26 04:06:43 +00:00
|
|
|
fprintf(stderr, "WinEthernetTap: Failed to initialize security");
|
2020-11-19 22:10:34 +00:00
|
|
|
throw std::runtime_error("WinEthernetTap: Failed to initialize security");
|
|
|
|
}
|
|
|
|
_comInit = true;
|
|
|
|
}
|
|
|
|
}
|
2024-08-21 21:06:25 +00:00
|
|
|
return std::shared_ptr<EthernetTap>(new WindowsEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
|
2019-08-07 23:14:12 +00:00
|
|
|
#endif // __WINDOWS__
|
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
2024-08-21 21:06:25 +00:00
|
|
|
return std::shared_ptr<EthernetTap>(new BSDEthernetTap(homePath,concurrency,pinning,mac,mtu,metric,nwid,friendlyName,handler,arg));
|
2019-08-07 23:14:12 +00:00
|
|
|
#endif // __FreeBSD__
|
|
|
|
|
|
|
|
#ifdef __NetBSD__
|
2024-08-21 21:06:25 +00:00
|
|
|
return std::shared_ptr<EthernetTap>(new NetBSDEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
|
2019-08-07 23:14:12 +00:00
|
|
|
#endif // __NetBSD__
|
|
|
|
|
|
|
|
#ifdef __OpenBSD__
|
2024-08-22 19:59:06 +00:00
|
|
|
return std::shared_ptr<EthernetTap>(new BSDEthernetTap(homePath,mac,mtu,metric,nwid,friendlyName,handler,arg));
|
2019-08-07 23:14:12 +00:00
|
|
|
#endif // __OpenBSD__
|
|
|
|
|
2019-08-07 23:45:04 +00:00
|
|
|
#endif // ZT_SDK?
|
|
|
|
|
2019-08-07 23:14:12 +00:00
|
|
|
return std::shared_ptr<EthernetTap>();
|
|
|
|
}
|
|
|
|
|
|
|
|
EthernetTap::EthernetTap() {}
|
|
|
|
EthernetTap::~EthernetTap() {}
|
|
|
|
|
2019-08-28 14:43:18 +00:00
|
|
|
bool EthernetTap::addIps(std::vector<InetAddress> ips)
|
|
|
|
{
|
|
|
|
for(std::vector<InetAddress>::const_iterator i(ips.begin());i!=ips.end();++i) {
|
|
|
|
if (!addIp(*i))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-16 14:12:55 +00:00
|
|
|
std::string EthernetTap::friendlyName() const
|
|
|
|
{
|
|
|
|
// Most platforms do not have this.
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2019-08-07 23:14:12 +00:00
|
|
|
} // namespace ZeroTier
|