Style match

This commit is contained in:
Joseph Henry 2025-02-26 11:16:03 -08:00
parent cd191778c2
commit 269c8d415a
No known key found for this signature in database
GPG key ID: 3C2C8A1EB4269827
2 changed files with 437 additions and 328 deletions

View file

@ -1,11 +1,26 @@
#include <sys/times.h> /*
#include <fcntl.h> * Copyright (c)2019 ZeroTier, Inc.
#include <unistd.h> *
#include <iostream> * Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
*
* Change Date: 2026-01-01
*
* 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.
*/
/****/
#include "ExtOsdep.hpp" #include "ExtOsdep.hpp"
#include <list>
#include "../node/AtomicCounter.hpp" #include "../node/AtomicCounter.hpp"
#include <fcntl.h>
#include <iostream>
#include <list>
#include <sys/times.h>
#include <unistd.h>
#define ZT_TAP_BUF_SIZE 16384 #define ZT_TAP_BUF_SIZE 16384
namespace ZeroTier { namespace ZeroTier {
@ -22,18 +37,24 @@ struct EodRoute {
}; };
static std::list<EodRoute> allRoutes; static std::list<EodRoute> allRoutes;
template<typename T> static void __eodSend(const T &t) { template <typename T> static void __eodSend(const T& t)
{
write(eodFd, &t, sizeof(t)); write(eodFd, &t, sizeof(t));
} }
static void strncpyx(char *dest, const char *src, size_t n) { static void strncpyx(char* dest, const char* src, size_t n)
{
strncpy(dest, src, n); strncpy(dest, src, n);
if (n > 1) dest[n - 1] = 0; if (n > 1) {
dest[n - 1] = 0;
}
} }
static int __eodWait(unsigned char msg, unsigned char *d, unsigned l, static int __eodWait(unsigned char msg, unsigned char* d, unsigned l, unsigned maxl = 0, int* recvfd = nullptr)
unsigned maxl = 0, int *recvfd = nullptr) { {
if (!maxl) maxl = l; if (! maxl) {
maxl = l;
}
auto start = times(NULL); auto start = times(NULL);
while (1) { while (1) {
msghdr mh; msghdr mh;
@ -56,10 +77,7 @@ static int __eodWait(unsigned char msg, unsigned char *d, unsigned l,
int r = recvmsg(eodFd, &mh, MSG_TRUNC | MSG_CMSG_CLOEXEC); int r = recvmsg(eodFd, &mh, MSG_TRUNC | MSG_CMSG_CLOEXEC);
if (r > 0) { if (r > 0) {
if (recvfd && mh.msg_controllen >= sizeof(cmsg) if (recvfd && mh.msg_controllen >= sizeof(cmsg) && cmsg.cmsg_len == sizeof(cmsg) && cmsg.cmsg_level == SOL_SOCKET && cmsg.cmsg_type == SCM_RIGHTS) {
&& cmsg.cmsg_len == sizeof(cmsg)
&& cmsg.cmsg_level == SOL_SOCKET
&& cmsg.cmsg_type == SCM_RIGHTS) {
*recvfd = cmsg.fd; *recvfd = cmsg.fd;
fprintf(stderr, "eodWait: received fd %d\n", *recvfd); fprintf(stderr, "eodWait: received fd %d\n", *recvfd);
} }
@ -82,29 +100,34 @@ static int __eodWait(unsigned char msg, unsigned char *d, unsigned l,
} }
} }
template<typename T> static bool __eodWait(unsigned msg, T &t) { template <typename T> static bool __eodWait(unsigned msg, T& t)
return __eodWait(msg, (unsigned char *)&t, sizeof(T)) == (int)sizeof(T); {
return __eodWait(msg, (unsigned char*)&t, sizeof(T)) == (int)sizeof(T);
} }
template<typename M, typename R> static bool __eodXchg(const M &m, unsigned rm, R &r) { template <typename M, typename R> static bool __eodXchg(const M& m, unsigned rm, R& r)
{
__eodSend(m); __eodSend(m);
return __eodWait(rm, r); return __eodWait(rm, r);
} }
template<typename M, typename R> static bool eodXchg(const M &m, unsigned rm, R &r) { template <typename M, typename R> static bool eodXchg(const M& m, unsigned rm, R& r)
{
Mutex::Lock l(eodMutex); Mutex::Lock l(eodMutex);
return __eodXchg(m, rm, r); return __eodXchg(m, rm, r);
} }
void ExtOsdep::init(int fd1, int fd2) { void ExtOsdep::init(int fd1, int fd2)
{
eodFd = fd1; eodFd = fd1;
eodMgmtFd = fd2; eodMgmtFd = fd2;
fcntl(eodMgmtFd,F_SETFL,O_NONBLOCK); fcntl(eodMgmtFd, F_SETFL, O_NONBLOCK);
} }
void ExtOsdep::started(int *f, void **cp) { void ExtOsdep::started(int* f, void** cp)
{
*f = eodMgmtFd; *f = eodMgmtFd;
*cp = (void *)eodMgmtFd; *cp = (void*)eodMgmtFd;
unsigned char msg = ZT_EOD_MSG_STARTED; unsigned char msg = ZT_EOD_MSG_STARTED;
Mutex::Lock l(eodMutex); Mutex::Lock l(eodMutex);
@ -114,66 +137,85 @@ void ExtOsdep::started(int *f, void **cp) {
static std::string mgmtrd; static std::string mgmtrd;
static std::string mgmtwr; static std::string mgmtwr;
bool ExtOsdep::mgmtWritable(void *cookie) { bool ExtOsdep::mgmtWritable(void* cookie)
if (cookie != (void *)eodMgmtFd) return false; {
if (mgmtwr.size() == 0) return true; if (cookie != (void*)eodMgmtFd) {
return false;
}
if (mgmtwr.size() == 0) {
return true;
}
auto sz = write(eodMgmtFd, mgmtwr.data(), mgmtwr.size()); auto sz = write(eodMgmtFd, mgmtwr.data(), mgmtwr.size());
if (sz <= 0) return false; if (sz <= 0) {
return false;
}
mgmtwr.erase(mgmtwr.begin(), mgmtwr.begin() + sz); mgmtwr.erase(mgmtwr.begin(), mgmtwr.begin() + sz);
return mgmtwr.empty(); return mgmtwr.empty();
} }
bool ExtOsdep::mgmtRecv(void *cookie, void *data, unsigned long len, bool ExtOsdep::mgmtRecv(void* cookie, void* data, unsigned long len, std::function<unsigned(unsigned, const std::string&, const std::string&, std::string&)> cb)
std::function<unsigned (unsigned, const std::string &, const std::string &, std::string &)> cb) { {
if (cookie != (void *)eodMgmtFd) return false; if (cookie != (void*)eodMgmtFd) {
mgmtrd.append((char *)data, len); return false;
}
mgmtrd.append((char*)data, len);
while (1) { while (1) {
auto req = (zt_eod_mgmt_req *)mgmtrd.data(); auto req = (zt_eod_mgmt_req*)mgmtrd.data();
if (mgmtrd.size() < sizeof(*req)) break; if (mgmtrd.size() < sizeof(*req)) {
break;
}
unsigned reqsz = sizeof(*req) + req->pathlen + req->datalen; unsigned reqsz = sizeof(*req) + req->pathlen + req->datalen;
if (mgmtrd.size() < reqsz) break; if (mgmtrd.size() < reqsz) {
break;
}
std::string resp; std::string resp;
char *p = (char *)req->data; char* p = (char*)req->data;
zt_eod_mgmt_reply rep; zt_eod_mgmt_reply rep;
rep.scode = cb(req->method, std::string(p, p + req->pathlen), rep.scode = cb(req->method, std::string(p, p + req->pathlen), std::string(p + req->pathlen, p + req->pathlen + req->datalen), resp);
std::string(p + req->pathlen, p + req->pathlen + req->datalen), resp);
rep.datalen = resp.size(); rep.datalen = resp.size();
mgmtrd.erase(mgmtrd.begin(), mgmtrd.begin() + reqsz); mgmtrd.erase(mgmtrd.begin(), mgmtrd.begin() + reqsz);
mgmtwr.append((char *)&rep, sizeof(rep)); mgmtwr.append((char*)&rep, sizeof(rep));
mgmtwr.append(resp); mgmtwr.append(resp);
auto sz = write(eodMgmtFd, mgmtwr.data(), mgmtwr.size()); auto sz = write(eodMgmtFd, mgmtwr.data(), mgmtwr.size());
if (sz > 0) mgmtwr.erase(mgmtwr.begin(), mgmtwr.begin() + sz); if (sz > 0) {
mgmtwr.erase(mgmtwr.begin(), mgmtwr.begin() + sz);
} }
return !mgmtwr.empty(); }
return ! mgmtwr.empty();
} }
void ExtOsdep::routeAddDel(bool add, const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifname) { void ExtOsdep::routeAddDel(bool add, const InetAddress& target, const InetAddress& via, const InetAddress& src, const char* ifname)
{
Mutex::Lock l(eodMutex); Mutex::Lock l(eodMutex);
std::string ifn; std::string ifn;
if (ifname) ifn = ifname; if (ifname) {
ifn = ifname;
}
if (add) { if (add) {
for (auto x = allRoutes.begin(); x != allRoutes.end(); ++x) { for (auto x = allRoutes.begin(); x != allRoutes.end(); ++x) {
if (x->target == target && x->via == via if (x->target == target && x->via == via && x->src == src && x->ifname == ifn) {
&& x->src == src && x->ifname == ifn) return; return;
} }
allRoutes.push_back({target, via, src, ifn}); }
allRoutes.push_back({ target, via, src, ifn });
} }
else { else {
bool found = false; bool found = false;
for (auto x = allRoutes.begin(); x != allRoutes.end(); ++x) { for (auto x = allRoutes.begin(); x != allRoutes.end(); ++x) {
if (x->target == target && x->via == via if (x->target == target && x->via == via && x->src == src && x->ifname == ifn) {
&& x->src == src && x->ifname == ifn) {
allRoutes.erase(x); allRoutes.erase(x);
found = true; found = true;
break; break;
} }
} }
if (!found) return; if (! found) {
return;
}
} }
zt_eod_msg_route req; zt_eod_msg_route req;
@ -182,54 +224,62 @@ void ExtOsdep::routeAddDel(bool add, const InetAddress &target, const InetAddres
req.afi = target.isV4() ? 1 : 2; req.afi = target.isV4() ? 1 : 2;
req.dstlen = target.netmaskBits(); req.dstlen = target.netmaskBits();
memcpy(req.dst, target.rawIpData(), target.isV4() ? 4 : 16); memcpy(req.dst, target.rawIpData(), target.isV4() ? 4 : 16);
if (ifname) strncpyx(req.dev, ifname, sizeof(req.dev)); if (ifname) {
if (via) memcpy(req.gw, via.rawIpData(), target.isV4() ? 4 : 16); strncpyx(req.dev, ifname, sizeof(req.dev));
if (src) memcpy(req.src, src.rawIpData(), target.isV4() ? 4 : 16); }
if (via) {
memcpy(req.gw, via.rawIpData(), target.isV4() ? 4 : 16);
}
if (src) {
memcpy(req.src, src.rawIpData(), target.isV4() ? 4 : 16);
}
unsigned char resp; unsigned char resp;
__eodXchg(req, add ? ZT_EOD_MSG_ADDROUTERESP : ZT_EOD_MSG_DELROUTERESP, resp); __eodXchg(req, add ? ZT_EOD_MSG_ADDROUTERESP : ZT_EOD_MSG_DELROUTERESP, resp);
} }
bool ExtOsdep::getBindAddrs(std::map<InetAddress,std::string> &ret) { bool ExtOsdep::getBindAddrs(std::map<InetAddress, std::string>& ret)
{
Mutex::Lock l(eodMutex); Mutex::Lock l(eodMutex);
unsigned char req = ZT_EOD_MSG_GETBINDADDRS; unsigned char req = ZT_EOD_MSG_GETBINDADDRS;
__eodSend(req); __eodSend(req);
zt_eod_msg_getbindaddrsresp *resp; zt_eod_msg_getbindaddrsresp* resp;
unsigned char buf[ZT_EOD_MAXMSGSIZE]; unsigned char buf[ZT_EOD_MAXMSGSIZE];
int r = __eodWait(ZT_EOD_MSG_GETBINDADDRSRESP, (unsigned char *)buf, sizeof(*resp), sizeof(buf)); int r = __eodWait(ZT_EOD_MSG_GETBINDADDRSRESP, (unsigned char*)buf, sizeof(*resp), sizeof(buf));
if (r < (int)sizeof(*resp)) return false; if (r < (int)sizeof(*resp)) {
return false;
}
int c = (r - (int)sizeof(*resp)) / sizeof(resp->addrs[0]); int c = (r - (int)sizeof(*resp)) / sizeof(resp->addrs[0]);
resp = (zt_eod_msg_getbindaddrsresp *)buf; resp = (zt_eod_msg_getbindaddrsresp*)buf;
for (int i = 0; i < c; ++i) { for (int i = 0; i < c; ++i) {
ret[InetAddress(resp->addrs[i].data, resp->addrs[i].afi == 1 ? 4 : 16, resp->addrs[i].len)] ret[InetAddress(resp->addrs[i].data, resp->addrs[i].afi == 1 ? 4 : 16, resp->addrs[i].len)] = resp->addrs[i].ifname;
= resp->addrs[i].ifname;
} }
return resp->result; return resp->result;
} }
ExtOsdepTap::ExtOsdepTap( ExtOsdepTap::ExtOsdepTap(
const char *homePath, const char* homePath,
const MAC &mac, const MAC& mac,
unsigned int mtu, unsigned int mtu,
unsigned int metric, unsigned int metric,
uint64_t nwid, uint64_t nwid,
const char *friendlyName, const char* friendlyName,
void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int), void (*handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int),
void *arg) : void* arg)
_handler(handler), : _handler(handler)
_arg(arg), , _arg(arg)
_nwid(nwid), , _nwid(nwid)
_mac(mac), , _mac(mac)
_homePath(homePath), , _homePath(homePath)
_mtu(mtu), , _mtu(mtu)
_fd(0), , _fd(0)
_enabled(true), , _enabled(true)
_run(true) , _run(true)
{ {
zt_eod_msg_addtap req; zt_eod_msg_addtap req;
req.cmd = ZT_EOD_MSG_ADDTAP; req.cmd = ZT_EOD_MSG_ADDTAP;
req.nwid = nwid; req.nwid = nwid;
@ -243,43 +293,46 @@ ExtOsdepTap::ExtOsdepTap(
Mutex::Lock l(eodMutex); Mutex::Lock l(eodMutex);
__eodSend(req); __eodSend(req);
_fd = -1; _fd = -1;
if (__eodWait(ZT_EOD_MSG_ADDTAPRESP, (unsigned char *)&resp, sizeof(resp), sizeof(resp), &_fd) != sizeof(resp)) if (__eodWait(ZT_EOD_MSG_ADDTAPRESP, (unsigned char*)&resp, sizeof(resp), sizeof(resp), &_fd) != sizeof(resp)) {
throw std::runtime_error(std::string("could not create TAP")); throw std::runtime_error(std::string("could not create TAP"));
}
_dev = resp.name; _dev = resp.name;
if (_dev.empty() || _fd < 0) if (_dev.empty() || _fd < 0) {
throw std::runtime_error(std::string("could not create TAP")); throw std::runtime_error(std::string("could not create TAP"));
}
fcntl(_fd,F_SETFL,O_NONBLOCK); fcntl(_fd, F_SETFL, O_NONBLOCK);
// processing shamelessly copied from LinuxEthernetTap
(void)::pipe(_shutdownSignalPipe); (void)::pipe(_shutdownSignalPipe);
for(unsigned int t=0;t<2;++t) { for (unsigned int t = 0; t < 2; ++t) {
_tapReaderThread[t] = std::thread([this, t]{ _tapReaderThread[t] = std::thread([this, t] {
fd_set readfds,nullfds; fd_set readfds, nullfds;
int n,nfds,r; int n, nfds, r;
void *buf = nullptr; void* buf = nullptr;
std::vector<void *> buffers; std::vector<void*> buffers;
if (!_run) if (! _run) {
return; return;
}
FD_ZERO(&readfds); FD_ZERO(&readfds);
FD_ZERO(&nullfds); FD_ZERO(&nullfds);
nfds = (int)std::max(_shutdownSignalPipe[0],_fd) + 1; nfds = (int)std::max(_shutdownSignalPipe[0], _fd) + 1;
r = 0; r = 0;
for(;;) { for (;;) {
FD_SET(_shutdownSignalPipe[0],&readfds); FD_SET(_shutdownSignalPipe[0], &readfds);
FD_SET(_fd,&readfds); FD_SET(_fd, &readfds);
select(nfds,&readfds,&nullfds,&nullfds,(struct timeval *)0); select(nfds, &readfds, &nullfds, &nullfds, (struct timeval*)0);
if (FD_ISSET(_shutdownSignalPipe[0],&readfds)) // writes to shutdown pipe terminate thread if (FD_ISSET(_shutdownSignalPipe[0], &readfds)) { // writes to shutdown pipe terminate thread
break; break;
}
if (FD_ISSET(_fd,&readfds)) { if (FD_ISSET(_fd, &readfds)) {
for(;;) { // read until there are no more packets, then return to outer select() loop for (;;) { // read until there are no more packets, then return to outer select() loop
if (!buf) { if (! buf) {
// To reduce use of the mutex, we keep a local buffer vector and // To reduce use of the mutex, we keep a local buffer vector and
// swap (which is a pointer swap) with the global one when it's // swap (which is a pointer swap) with the global one when it's
// empty. This retrieves a batch of buffers to use. // empty. This retrieves a batch of buffers to use.
@ -289,33 +342,37 @@ ExtOsdepTap::ExtOsdepTap(
} }
if (buffers.empty()) { if (buffers.empty()) {
buf = malloc(ZT_TAP_BUF_SIZE); buf = malloc(ZT_TAP_BUF_SIZE);
if (!buf) if (! buf) {
break; break;
} else { }
}
else {
buf = buffers.back(); buf = buffers.back();
buffers.pop_back(); buffers.pop_back();
} }
} }
n = (int)::read(_fd,reinterpret_cast<uint8_t *>(buf) + r,ZT_TAP_BUF_SIZE - r); n = (int)::read(_fd, reinterpret_cast<uint8_t*>(buf) + r, ZT_TAP_BUF_SIZE - r);
if (n > 0) { if (n > 0) {
// Some tap drivers like to send the ethernet frame and the // Some tap drivers like to send the ethernet frame and the
// payload in two chunks, so handle that by accumulating // payload in two chunks, so handle that by accumulating
// data until we have at least a frame. // data until we have at least a frame.
r += n; r += n;
if (r > 14) { if (r > 14) {
if (r > ((int)_mtu + 14)) // sanity check for weird TAP behavior on some platforms if (r > ((int)_mtu + 14)) { // sanity check for weird TAP behavior on some platforms
r = _mtu + 14; r = _mtu + 14;
}
if (_enabled && _tapqsize.load() < 1000) { if (_enabled && _tapqsize.load() < 1000) {
++_tapqsize; ++_tapqsize;
_tapq.post(std::pair<void *,int>(buf,r)); _tapq.post(std::pair<void*, int>(buf, r));
buf = nullptr; buf = nullptr;
} }
r = 0; r = 0;
} }
} else { }
else {
r = 0; r = 0;
break; break;
} }
@ -326,32 +383,39 @@ ExtOsdepTap::ExtOsdepTap(
} }
_tapProcessorThread = std::thread([this] { _tapProcessorThread = std::thread([this] {
MAC to,from; MAC to, from;
std::pair<void *,int> qi; std::pair<void*, int> qi;
while (_tapq.get(qi)) { while (_tapq.get(qi)) {
--_tapqsize; --_tapqsize;
uint8_t *const b = reinterpret_cast<uint8_t *>(qi.first); uint8_t* const b = reinterpret_cast<uint8_t*>(qi.first);
if (b) { if (b) {
to.setTo(b, 6); to.setTo(b, 6);
from.setTo(b + 6, 6); from.setTo(b + 6, 6);
unsigned int etherType = Utils::ntoh(((const uint16_t *)b)[6]); unsigned int etherType = Utils::ntoh(((const uint16_t*)b)[6]);
_handler(_arg, nullptr, _nwid, from, to, etherType, 0, (const void *)(b + 14),(unsigned int)(qi.second - 14)); _handler(_arg, nullptr, _nwid, from, to, etherType, 0, (const void*)(b + 14), (unsigned int)(qi.second - 14));
{ {
std::lock_guard<std::mutex> l(_buffers_l); std::lock_guard<std::mutex> l(_buffers_l);
if (_buffers.size() < 128) if (_buffers.size() < 128) {
_buffers.push_back(qi.first); _buffers.push_back(qi.first);
else free(qi.first);
} }
} else break; else {
free(qi.first);
}
}
}
else {
break;
}
} }
}); });
} }
ExtOsdepTap::~ExtOsdepTap() { ExtOsdepTap::~ExtOsdepTap()
{
_run = false; _run = false;
(void)::write(_shutdownSignalPipe[1],"\0",1); // causes reader thread(s) to exit (void)::write(_shutdownSignalPipe[1], "\0", 1); // causes reader thread(s) to exit
_tapq.post(std::pair<void *,int>(nullptr,0)); // causes processor thread to exit _tapq.post(std::pair<void*, int>(nullptr, 0)); // causes processor thread to exit
_tapReaderThread[0].join(); _tapReaderThread[0].join();
_tapReaderThread[1].join(); _tapReaderThread[1].join();
@ -361,13 +425,15 @@ ExtOsdepTap::~ExtOsdepTap() {
::close(_shutdownSignalPipe[0]); ::close(_shutdownSignalPipe[0]);
::close(_shutdownSignalPipe[1]); ::close(_shutdownSignalPipe[1]);
for(std::vector<void *>::iterator i(_buffers.begin());i!=_buffers.end();++i) for (std::vector<void*>::iterator i(_buffers.begin()); i != _buffers.end(); ++i) {
free(*i); free(*i);
std::vector< std::pair<void *,int> > dv(_tapq.drain()); }
for(std::vector< std::pair<void *,int> >::iterator i(dv.begin());i!=dv.end();++i) { std::vector<std::pair<void*, int> > dv(_tapq.drain());
if (i->first) for (std::vector<std::pair<void*, int> >::iterator i(dv.begin()); i != dv.end(); ++i) {
if (i->first) {
free(i->first); free(i->first);
} }
}
zt_eod_msg_deltap req; zt_eod_msg_deltap req;
req.cmd = ZT_EOD_MSG_DELTAP; req.cmd = ZT_EOD_MSG_DELTAP;
@ -377,15 +443,18 @@ ExtOsdepTap::~ExtOsdepTap() {
eodXchg(req, ZT_EOD_MSG_DELTAPRESP, resp); eodXchg(req, ZT_EOD_MSG_DELTAPRESP, resp);
} }
void ExtOsdepTap::setEnabled(bool en) { void ExtOsdepTap::setEnabled(bool en)
{
_enabled = en; _enabled = en;
} }
bool ExtOsdepTap::enabled() const { bool ExtOsdepTap::enabled() const
{
return _enabled; return _enabled;
} }
void ExtOsdepTap::doRemoveIp(const InetAddress &ip) { void ExtOsdepTap::doRemoveIp(const InetAddress& ip)
{
zt_eod_msg_ip req; zt_eod_msg_ip req;
req.cmd = ZT_EOD_MSG_DELIP; req.cmd = ZT_EOD_MSG_DELIP;
strcpy(req.name, _dev.c_str()); strcpy(req.name, _dev.c_str());
@ -397,12 +466,17 @@ void ExtOsdepTap::doRemoveIp(const InetAddress &ip) {
__eodXchg(req, ZT_EOD_MSG_DELIPRESP, resp); __eodXchg(req, ZT_EOD_MSG_DELIPRESP, resp);
} }
bool ExtOsdepTap::addIp(const InetAddress &ip) { bool ExtOsdepTap::addIp(const InetAddress& ip)
{
Mutex::Lock l(eodMutex); Mutex::Lock l(eodMutex);
for(auto i = allIps.begin();i!=allIps.end();++i) { for (auto i = allIps.begin(); i != allIps.end(); ++i) {
if (*i == ip) return true; if (*i == ip) {
if (i->ipsEqual(ip)) doRemoveIp(*i); return true;
}
if (i->ipsEqual(ip)) {
doRemoveIp(*i);
}
} }
zt_eod_msg_ip req; zt_eod_msg_ip req;
@ -419,12 +493,15 @@ bool ExtOsdepTap::addIp(const InetAddress &ip) {
return true; return true;
} }
bool ExtOsdepTap::addIps(std::vector<InetAddress> ips) { bool ExtOsdepTap::addIps(std::vector<InetAddress> ips)
{
return false; return false;
} }
bool ExtOsdepTap::removeIp(const InetAddress &ip) {
bool ExtOsdepTap::removeIp(const InetAddress& ip)
{
Mutex::Lock l(eodMutex); Mutex::Lock l(eodMutex);
for(auto i = allIps.begin();i!=allIps.end();++i) { for (auto i = allIps.begin(); i != allIps.end(); ++i) {
if (*i == ip) { if (*i == ip) {
doRemoveIp(*i); doRemoveIp(*i);
return true; return true;
@ -432,7 +509,8 @@ bool ExtOsdepTap::removeIp(const InetAddress &ip) {
} }
return false; return false;
} }
std::vector<InetAddress> ExtOsdepTap::ips() const { std::vector<InetAddress> ExtOsdepTap::ips() const
{
std::vector<InetAddress> ret; std::vector<InetAddress> ret;
Mutex::Lock l(eodMutex); Mutex::Lock l(eodMutex);
@ -442,84 +520,100 @@ std::vector<InetAddress> ExtOsdepTap::ips() const {
strcpy(req.name, _dev.c_str()); strcpy(req.name, _dev.c_str());
__eodSend(req); __eodSend(req);
zt_eod_msg_getipsresp *resp; zt_eod_msg_getipsresp* resp;
unsigned char buf[ZT_EOD_MAXMSGSIZE]; unsigned char buf[ZT_EOD_MAXMSGSIZE];
int r = __eodWait(ZT_EOD_MSG_GETIPSRESP, (unsigned char *)buf, sizeof(*resp), sizeof(buf)); int r = __eodWait(ZT_EOD_MSG_GETIPSRESP, (unsigned char*)buf, sizeof(*resp), sizeof(buf));
if (r < (int)sizeof(*resp)) return ret; if (r < (int)sizeof(*resp)) {
return ret;
}
int c = (r - (int)sizeof(*resp)) / sizeof(resp->addrs[0]); int c = (r - (int)sizeof(*resp)) / sizeof(resp->addrs[0]);
resp = (zt_eod_msg_getipsresp *)buf; resp = (zt_eod_msg_getipsresp*)buf;
for (int i = 0; i < c; ++i) { for (int i = 0; i < c; ++i) {
ret.push_back(InetAddress(resp->addrs[i].data, resp->addrs[i].afi == 1 ? 4 : 16, resp->addrs[i].len)); ret.push_back(InetAddress(resp->addrs[i].data, resp->addrs[i].afi == 1 ? 4 : 16, resp->addrs[i].len));
} }
return ret; return ret;
} }
void ExtOsdepTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) { void ExtOsdepTap::put(const MAC& from, const MAC& to, unsigned int etherType, const void* data, unsigned int len)
{
char putBuf[ZT_MAX_MTU + 64]; char putBuf[ZT_MAX_MTU + 64];
if ((_fd > 0)&&(len <= _mtu)&&(_enabled)) { if ((_fd > 0) && (len <= _mtu) && (_enabled)) {
to.copyTo(putBuf,6); to.copyTo(putBuf, 6);
from.copyTo(putBuf + 6,6); from.copyTo(putBuf + 6, 6);
*((uint16_t *)(putBuf + 12)) = htons((uint16_t)etherType); *((uint16_t*)(putBuf + 12)) = htons((uint16_t)etherType);
memcpy(putBuf + 14,data,len); memcpy(putBuf + 14, data, len);
len += 14; len += 14;
(void)::write(_fd,putBuf,len); (void)::write(_fd, putBuf, len);
} }
} }
std::string ExtOsdepTap::deviceName() const { std::string ExtOsdepTap::deviceName() const
{
return _dev; return _dev;
} }
void ExtOsdepTap::setFriendlyName(const char *friendlyName) {} void ExtOsdepTap::setFriendlyName(const char* friendlyName)
{
}
void ExtOsdepTap::scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed) { void ExtOsdepTap::scanMulticastGroups(std::vector<MulticastGroup>& added, std::vector<MulticastGroup>& removed)
char *ptr,*ptr2; {
char *ptr, *ptr2;
unsigned char mac[6]; unsigned char mac[6];
std::vector<MulticastGroup> newGroups; std::vector<MulticastGroup> newGroups;
int fd = ::open("/proc/net/dev_mcast",O_RDONLY); int fd = ::open("/proc/net/dev_mcast", O_RDONLY);
if (fd > 0) { if (fd > 0) {
char buf[131072]; char buf[131072];
int n = (int)::read(fd,buf,sizeof(buf)); int n = (int)::read(fd, buf, sizeof(buf));
if ((n > 0)&&(n < (int)sizeof(buf))) { if ((n > 0) && (n < (int)sizeof(buf))) {
buf[n] = (char)0; buf[n] = (char)0;
for(char *l=strtok_r(buf,"\r\n",&ptr);(l);l=strtok_r((char *)0,"\r\n",&ptr)) { for (char* l = strtok_r(buf, "\r\n", &ptr); (l); l = strtok_r((char*)0, "\r\n", &ptr)) {
int fno = 0; int fno = 0;
char *devname = (char *)0; char* devname = (char*)0;
char *mcastmac = (char *)0; char* mcastmac = (char*)0;
for(char *f=strtok_r(l," \t",&ptr2);(f);f=strtok_r((char *)0," \t",&ptr2)) { for (char* f = strtok_r(l, " \t", &ptr2); (f); f = strtok_r((char*)0, " \t", &ptr2)) {
if (fno == 1) if (fno == 1) {
devname = f; devname = f;
else if (fno == 4) }
else if (fno == 4) {
mcastmac = f; mcastmac = f;
}
++fno; ++fno;
} }
if ((devname)&&(!strcmp(devname,_dev.c_str()))&&(mcastmac)&&(Utils::unhex(mcastmac,mac,6) == 6)) if ((devname) && (! strcmp(devname, _dev.c_str())) && (mcastmac) && (Utils::unhex(mcastmac, mac, 6) == 6)) {
newGroups.push_back(MulticastGroup(MAC(mac,6),0)); newGroups.push_back(MulticastGroup(MAC(mac, 6), 0));
}
} }
} }
::close(fd); ::close(fd);
} }
std::vector<InetAddress> allIps(ips()); std::vector<InetAddress> allIps(ips());
for(std::vector<InetAddress>::iterator ip(allIps.begin());ip!=allIps.end();++ip) for (std::vector<InetAddress>::iterator ip(allIps.begin()); ip != allIps.end(); ++ip) {
newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip)); newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip));
}
std::sort(newGroups.begin(),newGroups.end()); std::sort(newGroups.begin(), newGroups.end());
newGroups.erase(std::unique(newGroups.begin(),newGroups.end()),newGroups.end()); newGroups.erase(std::unique(newGroups.begin(), newGroups.end()), newGroups.end());
for(std::vector<MulticastGroup>::iterator m(newGroups.begin());m!=newGroups.end();++m) { for (std::vector<MulticastGroup>::iterator m(newGroups.begin()); m != newGroups.end(); ++m) {
if (!std::binary_search(_multicastGroups.begin(),_multicastGroups.end(),*m)) if (! std::binary_search(_multicastGroups.begin(), _multicastGroups.end(), *m)) {
added.push_back(*m); added.push_back(*m);
} }
for(std::vector<MulticastGroup>::iterator m(_multicastGroups.begin());m!=_multicastGroups.end();++m) { }
if (!std::binary_search(newGroups.begin(),newGroups.end(),*m)) for (std::vector<MulticastGroup>::iterator m(_multicastGroups.begin()); m != _multicastGroups.end(); ++m) {
if (! std::binary_search(newGroups.begin(), newGroups.end(), *m)) {
removed.push_back(*m); removed.push_back(*m);
} }
}
_multicastGroups.swap(newGroups); _multicastGroups.swap(newGroups);
} }
void ExtOsdepTap::setMtu(unsigned int mtu) { void ExtOsdepTap::setMtu(unsigned int mtu)
if (mtu == _mtu) return; {
if (mtu == _mtu) {
return;
}
_mtu = mtu; _mtu = mtu;
zt_eod_msg_setmtu req; zt_eod_msg_setmtu req;

View file

@ -1,3 +1,16 @@
/*
* Copyright (c)2019 ZeroTier, Inc.
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
*
* Change Date: 2026-01-01
*
* 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.
*/
/****/
#ifndef ZT_EXTOSDEP_HPP #ifndef ZT_EXTOSDEP_HPP
#define ZT_EXTOSDEP_HPP #define ZT_EXTOSDEP_HPP
@ -110,65 +123,67 @@ struct zt_eod_mgmt_reply {
#ifndef ZT_EXTOSDEP_IFACEONLY #ifndef ZT_EXTOSDEP_IFACEONLY
#include "../node/AtomicCounter.hpp"
#include "../node/Hashtable.hpp"
#include "../node/InetAddress.hpp" #include "../node/InetAddress.hpp"
#include "../node/MAC.hpp" #include "../node/MAC.hpp"
#include "Thread.hpp"
#include "../node/Hashtable.hpp"
#include "../node/Mutex.hpp" #include "../node/Mutex.hpp"
#include "../node/AtomicCounter.hpp"
#include "EthernetTap.hpp"
#include "BlockingQueue.hpp" #include "BlockingQueue.hpp"
#include "EthernetTap.hpp"
#include "Thread.hpp"
#include <atomic> #include <atomic>
#include <thread>
#include <mutex>
#include <functional> #include <functional>
#include <mutex>
#include <string> #include <string>
#include <thread>
namespace ZeroTier { namespace ZeroTier {
class ExtOsdep { class ExtOsdep {
public: public:
static void init(int, int); static void init(int, int);
static void started(int *, void **); static void started(int*, void**);
static void routeAddDel(bool, const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName); static void routeAddDel(bool, const InetAddress& target, const InetAddress& via, const InetAddress& src, const char* ifaceName);
static bool getBindAddrs(std::map<InetAddress,std::string> &); static bool getBindAddrs(std::map<InetAddress, std::string>&);
static bool mgmtRecv(void *cookie, void *data, unsigned long len, static bool mgmtRecv(void* cookie, void* data, unsigned long len, std::function<unsigned(unsigned, const std::string&, const std::string&, std::string&)>);
std::function<unsigned (unsigned, const std::string &, const std::string &, std::string &)>); static bool mgmtWritable(void*);
static bool mgmtWritable(void *);
}; };
class ExtOsdepTap : public EthernetTap class ExtOsdepTap : public EthernetTap {
{ public:
public:
ExtOsdepTap( ExtOsdepTap(
const char *homePath, const char* homePath,
const MAC &mac, const MAC& mac,
unsigned int mtu, unsigned int mtu,
unsigned int metric, unsigned int metric,
uint64_t nwid, uint64_t nwid,
const char *friendlyName, const char* friendlyName,
void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int), void (*handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int),
void *arg); void* arg);
virtual ~ExtOsdepTap(); virtual ~ExtOsdepTap();
virtual void setEnabled(bool en); virtual void setEnabled(bool en);
virtual bool enabled() const; virtual bool enabled() const;
virtual bool addIp(const InetAddress &ip); virtual bool addIp(const InetAddress& ip);
virtual bool addIps(std::vector<InetAddress> ips); virtual bool addIps(std::vector<InetAddress> ips);
virtual bool removeIp(const InetAddress &ip); virtual bool removeIp(const InetAddress& ip);
virtual std::vector<InetAddress> ips() const; virtual std::vector<InetAddress> ips() const;
virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len); virtual void put(const MAC& from, const MAC& to, unsigned int etherType, const void* data, unsigned int len);
virtual std::string deviceName() const; virtual std::string deviceName() const;
virtual void setFriendlyName(const char *friendlyName); virtual void setFriendlyName(const char* friendlyName);
virtual void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed); virtual void scanMulticastGroups(std::vector<MulticastGroup>& added, std::vector<MulticastGroup>& removed);
virtual void setMtu(unsigned int mtu); virtual void setMtu(unsigned int mtu);
virtual void setDns(const char *domain, const std::vector<InetAddress> &servers) {} virtual void setDns(const char* domain, const std::vector<InetAddress>& servers)
private: {
void (*_handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int); }
void *_arg;
private:
void (*_handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int);
void* _arg;
uint64_t _nwid; uint64_t _nwid;
MAC _mac; MAC _mac;
std::string _homePath; std::string _homePath;
@ -182,12 +197,12 @@ private:
std::thread _tapReaderThread[2]; std::thread _tapReaderThread[2];
std::thread _tapProcessorThread; std::thread _tapProcessorThread;
std::mutex _buffers_l; std::mutex _buffers_l;
std::vector<void *> _buffers; std::vector<void*> _buffers;
BlockingQueue< std::pair<void *,int> > _tapq; BlockingQueue<std::pair<void*, int> > _tapq;
AtomicCounter _tapqsize; AtomicCounter _tapqsize;
std::vector<InetAddress> allIps; std::vector<InetAddress> allIps;
void doRemoveIp(const InetAddress &); void doRemoveIp(const InetAddress&);
}; };
} // namespace ZeroTier } // namespace ZeroTier