Merge branch 'rtnetlink' into edge

This commit is contained in:
Grant Limberg 2018-06-04 12:24:58 -07:00
commit b9975845ff
10 changed files with 1399 additions and 96 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
/zerotier-cli
/zerotier-selftest
/zerotier
/nltest
# OS-created garbage files from various platforms
.DS_Store

View File

@ -17,6 +17,10 @@ DESTDIR?=
include objects.mk
ONE_OBJS+=osdep/LinuxEthernetTap.o
ONE_OBJS+=osdep/LinuxNetLink.o
NLTEST_OBJS+=osdep/LinuxNetLink.o node/InetAddress.o node/Utils.o node/Salsa20.o
NLTEST_OBJS+=nltest.o
# Auto-detect miniupnpc and nat-pmp as well and use system libs if present,
# otherwise build into binary as done on Mac and Windows.
@ -58,8 +62,8 @@ ifeq ($(ZT_SANITIZE),1)
SANFLAGS+=-fsanitize=address -DASAN_OPTIONS=symbolize=1
endif
ifeq ($(ZT_DEBUG),1)
override CFLAGS+=-Wall -Wno-deprecated -Werror -g -pthread $(INCLUDES) $(DEFS)
override CXXFLAGS+=-Wall -Wno-deprecated -Werror -g -std=c++11 -pthread $(INCLUDES) $(DEFS)
override CFLAGS+=-Wall -Wno-deprecated -g -pthread $(INCLUDES) $(DEFS)
override CXXFLAGS+=-Wall -Wno-deprecated -g -std=c++11 -pthread $(INCLUDES) $(DEFS)
ZT_TRACE=1
STRIP?=echo
# The following line enables optimization for the crypto code, since
@ -315,6 +319,9 @@ debug: FORCE
make ZT_DEBUG=1 one
make ZT_DEBUG=1 selftest
nltest: $(NLTEST_OBJS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o nltest $(NLTEST_OBJS) $(LDLIBS)
# Note: keep the symlinks in /var/lib/zerotier-one to the binaries since these
# provide backward compatibility with old releases where the binaries actually
# lived here. Folks got scripts.

13
nltest.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "osdep/LinuxNetLink.hpp"
using namespace ZeroTier;
int main(int argc, char **argv)
{
LinuxNetLink &nl = LinuxNetLink::getInstance();
while(true) {
Thread::sleep(1000);
}
}

View File

@ -399,6 +399,10 @@ private:
{
return ((unsigned long)i * (unsigned long)0x9e3779b1);
}
static inline unsigned long _hc(const int i)
{
return ((unsigned long)i * (unsigned long)0x9e3379b1);
}
inline void _grow()
{

View File

@ -56,6 +56,7 @@
#include "../node/Dictionary.hpp"
#include "OSUtils.hpp"
#include "LinuxEthernetTap.hpp"
#include "LinuxNetLink.hpp"
// ff:ff:ff:ff:ff:ff with no ADI
static const ZeroTier::MulticastGroup _blindWildcardMulticastGroup(ZeroTier::MAC(0xff),0);
@ -97,6 +98,9 @@ LinuxEthernetTap::LinuxEthernetTap(
char procpath[128],nwids[32];
struct stat sbuf;
// ensure netlink connection is started
(void)LinuxNetLink::getInstance();
OSUtils::ztsnprintf(nwids,sizeof(nwids),"%.16llx",nwid);
Mutex::Lock _l(__tapCreateLock); // create only one tap at a time, globally
@ -263,18 +267,8 @@ bool LinuxEthernetTap::enabled() const
static bool ___removeIp(const std::string &_dev,const InetAddress &ip)
{
long cpid = (long)vfork();
if (cpid == 0) {
OSUtils::redirectUnixOutputs("/dev/null",(const char *)0);
setenv("PATH", "/sbin:/bin:/usr/sbin:/usr/bin", 1);
char iptmp[128];
::execlp("ip","ip","addr","del",ip.toString(iptmp),"dev",_dev.c_str(),(const char *)0);
::_exit(-1);
} else {
int exitcode = -1;
::waitpid(cpid,&exitcode,0);
return (exitcode == 0);
}
LinuxNetLink::getInstance().removeAddress(ip, _dev.c_str());
return true;
}
#ifdef __SYNOLOGY__
@ -285,12 +279,6 @@ bool LinuxEthernetTap::addIpSyn(std::vector<InetAddress> ips)
std::string cfg_contents = "DEVICE="+_dev+"\nBOOTPROTO=static";
int ip4=0,ip6=0,ip4_tot=0,ip6_tot=0;
long cpid = (long)vfork();
if (cpid == 0) {
OSUtils::redirectUnixOutputs("/dev/null",(const char *)0);
setenv("PATH", "/sbin:/bin:/usr/sbin:/usr/bin", 1);
// We must know if there is at least (one) of each protocol version so we
// can properly enumerate address/netmask combinations in the ifcfg-dev file
for(int i=0; i<(int)ips.size(); i++) {
if (ips[i].isV4())
ip4_tot++;
@ -305,8 +293,7 @@ bool LinuxEthernetTap::addIpSyn(std::vector<InetAddress> ips)
cfg_contents += "\nIPADDR"+numstr4+"="+ips[i].toIpString(iptmp)
+ "\nNETMASK"+numstr4+"="+ips[i].netmask().toIpString(iptmp2)+"\n";
ip4++;
}
else {
} else {
char iptmp[64],iptmp2[64];
std::string numstr6 = ip6_tot > 1 ? std::to_string(ip6) : "";
cfg_contents += "\nIPV6ADDR"+numstr6+"="+ips[i].toIpString(iptmp)
@ -317,17 +304,7 @@ bool LinuxEthernetTap::addIpSyn(std::vector<InetAddress> ips)
OSUtils::writeFile(filepath.c_str(), cfg_contents.c_str(), cfg_contents.length());
// Finaly, add IPs
for(int i=0; i<(int)ips.size(); i++){
char iptmp[128],iptmp2[128];
if (ips[i].isV4())
::execlp("ip","ip","addr","add",ips[i].toString(iptmp),"broadcast",ips[i].broadcast().toIpString(iptmp2),"dev",_dev.c_str(),(const char *)0);
else
::execlp("ip","ip","addr","add",ips[i].toString(iptmp),"dev",_dev.c_str(),(const char *)0);
}
::_exit(-1);
} else if (cpid > 0) {
int exitcode = -1;
::waitpid(cpid,&exitcode,0);
return (exitcode == 0);
LinuxNetLink::getInstance().addAddress(ips[i], _dev.c_str());
}
return true;
}
@ -348,24 +325,9 @@ bool LinuxEthernetTap::addIp(const InetAddress &ip)
___removeIp(_dev,*i);
}
long cpid = (long)vfork();
if (cpid == 0) {
OSUtils::redirectUnixOutputs("/dev/null",(const char *)0);
setenv("PATH", "/sbin:/bin:/usr/sbin:/usr/bin", 1);
char iptmp[128],iptmp2[128];
if (ip.isV4()) {
::execlp("ip","ip","addr","add",ip.toString(iptmp),"broadcast",ip.broadcast().toIpString(iptmp2),"dev",_dev.c_str(),(const char *)0);
} else {
::execlp("ip","ip","addr","add",ip.toString(iptmp),"dev",_dev.c_str(),(const char *)0);
}
::_exit(-1);
} else if (cpid > 0) {
int exitcode = -1;
::waitpid(cpid,&exitcode,0);
return (exitcode == 0);
}
LinuxNetLink::getInstance().addAddress(ip, _dev.c_str());
return false;
return true;
}
bool LinuxEthernetTap::removeIp(const InetAddress &ip)

1058
osdep/LinuxNetLink.cpp Normal file

File diff suppressed because it is too large Load Diff

129
osdep/LinuxNetLink.hpp Normal file
View File

@ -0,0 +1,129 @@
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
#ifndef ZT_LINUX_NETLINK_HPP
#define ZT_LINUX_NETLINK_HPP
#include <vector>
#include <bits/sockaddr.h>
#include <asm/types.h>
#include <linux/rtnetlink.h>
#include <sys/socket.h>
#include <linux/if.h>
#include "../node/InetAddress.hpp"
#include "../node/MAC.hpp"
#include "Thread.hpp"
#include "../node/Hashtable.hpp"
#include "../node/Mutex.hpp"
namespace ZeroTier {
struct route_entry {
InetAddress target;
InetAddress via;
int if_index;
char iface[IFNAMSIZ];
};
typedef std::vector<route_entry> RouteList;
/**
* Interface with Linux's RTNETLINK
*/
class LinuxNetLink
{
private:
LinuxNetLink();
~LinuxNetLink();
public:
static LinuxNetLink& getInstance()
{
static LinuxNetLink instance;
return instance;
}
LinuxNetLink(LinuxNetLink const&) = delete;
void operator=(LinuxNetLink const&) = delete;
void addRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName);
void delRoute(const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *ifaceName);
RouteList getIPV4Routes() const;
RouteList getIPV6Routes() const;
void addAddress(const InetAddress &addr, const char *iface);
void removeAddress(const InetAddress &addr, const char *iface);
void threadMain() throw();
private:
int _doRecv(int fd);
void _processMessage(struct nlmsghdr *nlp, int nll);
void _routeAdded(struct nlmsghdr *nlp);
void _routeDeleted(struct nlmsghdr *nlp);
void _linkAdded(struct nlmsghdr *nlp);
void _linkDeleted(struct nlmsghdr *nlp);
void _ipAddressAdded(struct nlmsghdr *nlp);
void _ipAddressDeleted(struct nlmsghdr *nlp);
void _requestInterfaceList();
void _requestIPv4Routes();
void _requestIPv6Routes();
int _indexForInterface(const char *iface);
void _setSocketTimeout(int fd, int seconds = 1);
Thread _t;
bool _running;
RouteList _routes_ipv4;
Mutex _rv4_m;
RouteList _routes_ipv6;
Mutex _rv6_m;
uint32_t _seq;
struct iface_entry {
int index;
char ifacename[IFNAMSIZ];
char mac[18];
char mac_bin[6];
unsigned int mtu;
};
Hashtable<int, iface_entry> _interfaces;
Mutex _if_m;
// socket communication vars;
int _fd;
struct sockaddr_nl _la;
};
}
#endif // ZT_LINUX_NETLINK_HPPS

View File

@ -48,6 +48,14 @@
#include <arpa/inet.h>
#include <net/route.h>
#include <net/if.h>
#ifdef __LINUX__
#include <sys/ioctl.h>
#include <bits/sockaddr.h>
#include <asm/types.h>
#include <linux/rtnetlink.h>
#include <sys/socket.h>
#include "../osdep/LinuxNetLink.hpp"
#endif
#ifdef __BSD__
#include <net/if_dl.h>
#include <sys/sysctl.h>
@ -277,27 +285,127 @@ static void _routeCmd(const char *op,const InetAddress &target,const InetAddress
#ifdef __LINUX__ // ----------------------------------------------------------
#define ZT_ROUTING_SUPPORT_FOUND 1
static void _routeCmd(const char *op,const InetAddress &target,const InetAddress &via,const char *localInterface)
static void _routeCmd(const char *op, const InetAddress &target, const InetAddress &via, const InetAddress &src, const char *localInterface)
{
long p = (long)fork();
if (p > 0) {
int exitcode = -1;
::waitpid(p,&exitcode,0);
} else if (p == 0) {
::close(STDOUT_FILENO);
::close(STDERR_FILENO);
char ipbuf[64],ipbuf2[64];
char targetStr[64] = {0};
char viaStr[64] = {0};
InetAddress nmsk = target.netmask();
char nmskStr[64] = {0};
fprintf(stderr, "Received Route Cmd: %s target: %s via: %s netmask: %s localInterface: %s\n", op, target.toString(targetStr), via.toString(viaStr), nmsk.toString(nmskStr), localInterface);
if ((strcmp(op, "add") == 0 || strcmp(op, "replace") == 0)) {
LinuxNetLink::getInstance().addRoute(target, via, src, localInterface);
} else if ((strcmp(op, "remove") == 0 || strcmp(op, "del") == 0)) {
LinuxNetLink::getInstance().delRoute(target, via, src, localInterface);
}
return;
int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);;
struct rtentry route = {0};
if (target.ss_family == AF_INET) {
struct sockaddr_in *target_in = (struct sockaddr_in*)&target;
struct sockaddr_in *via_in = (struct sockaddr_in*)&via;
InetAddress netmask = target.netmask();
struct sockaddr_in *netmask_in = (struct sockaddr_in*)&netmask;
struct sockaddr_in *addr = NULL;
// set target
addr = (struct sockaddr_in *)&route.rt_dst;
addr->sin_family = AF_INET;
addr->sin_addr = target_in->sin_addr;
// set netmask
addr = (struct sockaddr_in *)&route.rt_genmask;
addr->sin_family = AF_INET;
addr->sin_addr = netmask_in->sin_addr;
route.rt_dev = const_cast<char*>(localInterface);
if (via) {
::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString(ipbuf),"via",via.toIpString(ipbuf2),(const char *)0);
::execl(ZT_LINUX_IP_COMMAND_2,ZT_LINUX_IP_COMMAND_2,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString(ipbuf),"via",via.toIpString(ipbuf2),(const char *)0);
// set the gateway
addr = (struct sockaddr_in *)&route.rt_gateway;
addr->sin_family = AF_INET;
addr->sin_addr = via_in->sin_addr;
route.rt_flags = RTF_UP | RTF_GATEWAY;
} else if ((localInterface)&&(localInterface[0])) {
::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString(ipbuf),"dev",localInterface,(const char *)0);
::execl(ZT_LINUX_IP_COMMAND_2,ZT_LINUX_IP_COMMAND_2,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString(ipbuf),"dev",localInterface,(const char *)0);
route.rt_flags = RTF_UP;//| RTF_HOST;
}
::_exit(-1);
}
else if (target.ss_family == AF_INET6)
{
struct sockaddr_in6 *addr = NULL;
// set target
addr = (struct sockaddr_in6 *)&route.rt_dst;
addr->sin6_family = AF_INET6;
memcpy(&addr->sin6_addr, &((struct sockaddr_in6*)&target)->sin6_addr, sizeof(struct in6_addr));
//set netmask
addr = (struct sockaddr_in6 *)&route.rt_genmask;
addr->sin6_family = AF_INET6;
InetAddress netmask = target.netmask();
memcpy(&addr->sin6_addr, &((struct sockaddr_in6*)&netmask)->sin6_addr, sizeof(struct in6_addr));
if (via) {
// set the gateway
addr = (struct sockaddr_in6*)&route.rt_gateway;
addr->sin6_family = AF_INET;
memcpy(&addr->sin6_addr, &((struct sockaddr_in6*)&via)->sin6_addr, sizeof(struct in6_addr));
route.rt_flags = RTF_UP | RTF_GATEWAY;
} else if ((localInterface)&&(localInterface[0])) {
route.rt_dev = const_cast<char*>(localInterface);
route.rt_flags = RTF_UP;
}
}
unsigned long ctl = -1;
if (strcmp(op, "add") == 0 || strcmp(op, "replace") == 0) {
ctl = SIOCADDRT;
} else if (strcmp(op, "remove") == 0 || strcmp(op, "del") == 0) {
ctl = SIOCDELRT;
} else {
close(fd);
return;
}
if ( ioctl(fd, ctl, &route)) {
fprintf(stderr, "Error adding route: %s\n", strerror(errno));
close(fd);
::exit(1);
}
close(fd);
}
// static void _routeCmd(const char *op,const InetAddress &target,const InetAddress &via,const char *localInterface)
// {
// // long p = (long)fork();
// // if (p > 0) {
// // int exitcode = -1;
// // ::waitpid(p,&exitcode,0);
// // } else if (p == 0) {
// // ::close(STDOUT_FILENO);
// // ::close(STDERR_FILENO);
// char ipbuf[64],ipbuf2[64];
// if (via) {
// ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString(ipbuf),"via",via.toIpString(ipbuf2),(const char *)0);
// ::execl(ZT_LINUX_IP_COMMAND_2,ZT_LINUX_IP_COMMAND_2,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString(ipbuf),"via",via.toIpString(ipbuf2),(const char *)0);
// } else if ((localInterface)&&(localInterface[0])) {
// ::execl(ZT_LINUX_IP_COMMAND,ZT_LINUX_IP_COMMAND,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString(ipbuf),"dev",localInterface,(const char *)0);
// ::execl(ZT_LINUX_IP_COMMAND_2,ZT_LINUX_IP_COMMAND_2,(target.ss_family == AF_INET6) ? "-6" : "-4","route",op,target.toString(ipbuf),"dev",localInterface,(const char *)0);
// }
// // ::_exit(-1);
// // }
// }
#endif // __LINUX__ ----------------------------------------------------------
#ifdef __WINDOWS__ // --------------------------------------------------------
@ -494,11 +602,11 @@ bool ManagedRoute::sync()
if (!_applied.count(leftt)) {
_applied[leftt] = false; // boolean unused
_routeCmd("replace",leftt,_via,(_via) ? (const char *)0 : _device);
_routeCmd("replace",leftt,_via,_src,_device);
}
if ((rightt)&&(!_applied.count(rightt))) {
_applied[rightt] = false; // boolean unused
_routeCmd("replace",rightt,_via,(_via) ? (const char *)0 : _device);
_routeCmd("replace",rightt,_via,_src,_device);
}
#endif // __LINUX__ ----------------------------------------------------------
@ -545,7 +653,7 @@ void ManagedRoute::remove()
#endif // __BSD__ ------------------------------------------------------------
#ifdef __LINUX__ // ----------------------------------------------------------
_routeCmd("del",r->first,_via,(_via) ? (const char *)0 : _device);
_routeCmd("del",r->first,_via,_src,_device);
#endif // __LINUX__ ----------------------------------------------------------
#ifdef __WINDOWS__ // --------------------------------------------------------

View File

@ -49,14 +49,20 @@ class ManagedRoute
friend class SharedPtr<ManagedRoute>;
public:
ManagedRoute(const InetAddress &target,const InetAddress &via,const char *device)
ManagedRoute(const InetAddress &target,const InetAddress &via,const InetAddress &src,const char *device)
{
_target = target;
_via = via;
_src = src;
if (via.ss_family == AF_INET)
_via.setPort(32);
else if (via.ss_family == AF_INET6)
_via.setPort(128);
if (src.ss_family == AF_INET) {
_src.setPort(32);
} else if (src.ss_family == AF_INET6) {
_src.setPort(128);
}
Utils::scopy(_device,sizeof(_device),device);
_systemDevice[0] = (char)0;
}
@ -87,6 +93,7 @@ public:
inline const InetAddress &target() const { return _target; }
inline const InetAddress &via() const { return _via; }
inline const InetAddress &src() const { return _src; }
inline const char *device() const { return _device; }
private:
@ -95,6 +102,7 @@ private:
InetAddress _target;
InetAddress _via;
InetAddress _src;
InetAddress _systemVia; // for route overrides
std::map<InetAddress,bool> _applied; // routes currently applied
char _device[128];

View File

@ -1710,11 +1710,13 @@ public:
// Nuke applied routes that are no longer in n.config.routes[] and/or are not allowed
for(std::list< SharedPtr<ManagedRoute> >::iterator mr(n.managedRoutes.begin());mr!=n.managedRoutes.end();) {
bool haveRoute = false;
if ( (checkIfManagedIsAllowed(n,(*mr)->target())) && (((*mr)->via().ss_family != (*mr)->target().ss_family)||(!matchIpOnly(myIps,(*mr)->via()))) ) {
for(unsigned int i=0;i<n.config.routeCount;++i) {
const InetAddress *const target = reinterpret_cast<const InetAddress *>(&(n.config.routes[i].target));
const InetAddress *const via = reinterpret_cast<const InetAddress *>(&(n.config.routes[i].via));
if ( ((*mr)->target() == *target) && ( ((via->ss_family == target->ss_family)&&((*mr)->via().ipsEqual(*via))) || (tapdev == (*mr)->device()) ) ) {
if ( ((*mr)->target() == *target) && ( ((via->ss_family == target->ss_family)&&((*mr)->via().ipsEqual(*via))) || (strcmp(tapdev,(*mr)->device())==0) ) ) {
haveRoute = true;
break;
}
@ -1731,6 +1733,17 @@ public:
for(unsigned int i=0;i<n.config.routeCount;++i) {
const InetAddress *const target = reinterpret_cast<const InetAddress *>(&(n.config.routes[i].target));
const InetAddress *const via = reinterpret_cast<const InetAddress *>(&(n.config.routes[i].via));
InetAddress *src = NULL;
for (unsigned int j=0; j<n.config.assignedAddressCount; ++j) {
const InetAddress *const tmp = reinterpret_cast<const InetAddress *>(&(n.config.assignedAddresses[j]));
if (target->isV4() && tmp->isV4()) {
src = reinterpret_cast<InetAddress *>(&(n.config.assignedAddresses[j]));
break;
} else if (target->isV6() && tmp->isV6()) {
src = reinterpret_cast<InetAddress *>(&(n.config.assignedAddresses[j]));
break;
}
}
if ( (!checkIfManagedIsAllowed(n,*target)) || ((via->ss_family == target->ss_family)&&(matchIpOnly(myIps,*via))) )
continue;
@ -1762,7 +1775,7 @@ public:
continue;
// Add and apply new routes
n.managedRoutes.push_back(SharedPtr<ManagedRoute>(new ManagedRoute(*target,*via,tapdev)));
n.managedRoutes.push_back(SharedPtr<ManagedRoute>(new ManagedRoute(*target,*via,*src,tapdev)));
if (!n.managedRoutes.back()->sync())
n.managedRoutes.pop_back();
}