2017-11-06 13:50:20 -08:00
|
|
|
/*
|
2020-04-13 23:38:06 -07:00
|
|
|
* Copyright (c)2013-2020 ZeroTier, Inc.
|
2017-11-06 13:50:20 -08:00
|
|
|
*
|
2020-04-13 23:38:06 -07:00
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
2017-11-06 13:50:20 -08:00
|
|
|
*
|
2020-04-13 23:38:06 -07:00
|
|
|
* Change Date: 2024-01-01
|
2017-11-06 13:50:20 -08:00
|
|
|
*
|
2020-04-13 23:38:06 -07: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.
|
2017-11-06 13:50:20 -08:00
|
|
|
*/
|
2020-04-13 23:38:06 -07:00
|
|
|
/****/
|
2017-11-06 13:50:20 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
*
|
|
|
|
|
* Virtual Ethernet tap device
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
#ifndef LIBZT_VIRTUALTAP_HPP
|
|
|
|
|
#define LIBZT_VIRTUALTAP_HPP
|
2017-11-06 13:50:20 -08:00
|
|
|
|
2018-07-27 09:53:08 -07:00
|
|
|
#ifndef _MSC_VER
|
2017-11-21 15:53:31 -08:00
|
|
|
extern int errno;
|
2018-07-27 09:53:08 -07:00
|
|
|
#endif
|
2017-11-21 15:53:31 -08:00
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
#include "Phy.hpp"
|
2019-01-14 12:01:29 -08:00
|
|
|
#include "Thread.hpp"
|
|
|
|
|
#include "InetAddress.hpp"
|
|
|
|
|
#include "MulticastGroup.hpp"
|
|
|
|
|
#include "Mutex.hpp"
|
2017-11-06 13:50:20 -08:00
|
|
|
|
2019-02-06 22:00:39 -08:00
|
|
|
#include "Options.h"
|
2017-11-06 13:50:20 -08:00
|
|
|
|
2017-12-19 16:23:52 -08:00
|
|
|
#if defined(_WIN32)
|
2017-11-06 13:50:20 -08:00
|
|
|
#include <WinSock2.h>
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
#include <IPHlpApi.h>
|
|
|
|
|
#include <Ifdef.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
namespace ZeroTier {
|
2017-11-06 13:50:20 -08:00
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
class Mutex;
|
2017-11-06 13:50:20 -08:00
|
|
|
|
|
|
|
|
/**
|
2019-01-27 22:43:42 -08:00
|
|
|
* A virtual tap device. The ZeroTier core service creates one of these for each
|
|
|
|
|
* virtual network joined. It will be destroyed upon leave().
|
2017-11-06 13:50:20 -08:00
|
|
|
*/
|
|
|
|
|
class VirtualTap
|
|
|
|
|
{
|
|
|
|
|
friend class Phy<VirtualTap *>;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
VirtualTap(
|
|
|
|
|
const char *homePath,
|
2019-01-14 12:01:29 -08:00
|
|
|
const MAC &mac,
|
2017-11-06 13:50:20 -08:00
|
|
|
unsigned int mtu,
|
|
|
|
|
unsigned int metric,
|
|
|
|
|
uint64_t nwid,
|
|
|
|
|
const char *friendlyName,
|
2019-01-14 12:01:29 -08:00
|
|
|
void (*handler)(void *, void *, uint64_t, const MAC &,
|
|
|
|
|
const MAC &, unsigned int, unsigned int, const void *, unsigned int),
|
2017-11-06 13:50:20 -08:00
|
|
|
void *arg);
|
|
|
|
|
|
|
|
|
|
~VirtualTap();
|
|
|
|
|
|
|
|
|
|
void setEnabled(bool en);
|
|
|
|
|
bool enabled() const;
|
|
|
|
|
|
2019-02-14 17:27:16 -08:00
|
|
|
/**
|
|
|
|
|
* Mutex for protecting IP address container for this tap.
|
|
|
|
|
*/
|
|
|
|
|
Mutex _ips_m; // Public because we want it accessible by the driver layer
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return whether this tap has been assigned an IPv4 address.
|
|
|
|
|
*/
|
|
|
|
|
bool hasIpv4Addr();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return whether this tap has been assigned an IPv6 address.
|
|
|
|
|
*/
|
|
|
|
|
bool hasIpv6Addr();
|
|
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
/**
|
|
|
|
|
* Adds an address to the userspace stack interface associated with this VirtualTap
|
|
|
|
|
* - Starts VirtualTap main thread ONLY if successful
|
|
|
|
|
*/
|
2019-01-14 12:01:29 -08:00
|
|
|
bool addIp(const InetAddress &ip);
|
2017-11-06 13:50:20 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes an address from the userspace stack interface associated with this VirtualTap
|
|
|
|
|
*/
|
2019-01-14 12:01:29 -08:00
|
|
|
bool removeIp(const InetAddress &ip);
|
2017-11-06 13:50:20 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Presents data to the userspace stack
|
|
|
|
|
*/
|
2019-01-14 12:01:29 -08:00
|
|
|
void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,
|
2017-11-06 13:50:20 -08:00
|
|
|
unsigned int len);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-01-14 12:01:29 -08:00
|
|
|
* Get VirtualTap device name (e.g. 'libzt17d72843bc2c5760')
|
2017-11-06 13:50:20 -08:00
|
|
|
*/
|
|
|
|
|
std::string deviceName() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Node ID (ZT address)
|
|
|
|
|
*/
|
|
|
|
|
std::string nodeId() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set friendly name
|
|
|
|
|
*/
|
|
|
|
|
void setFriendlyName(const char *friendlyName);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scan multicast groups
|
|
|
|
|
*/
|
2019-01-14 12:01:29 -08:00
|
|
|
void scanMulticastGroups(std::vector<MulticastGroup> &added,
|
|
|
|
|
std::vector<MulticastGroup> &removed);
|
2017-11-06 13:50:20 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set MTU
|
|
|
|
|
*/
|
|
|
|
|
void setMtu(unsigned int mtu);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calls main network stack loops
|
|
|
|
|
*/
|
|
|
|
|
void threadMain()
|
|
|
|
|
throw();
|
|
|
|
|
|
|
|
|
|
#if defined(__MINGW32__)
|
|
|
|
|
/* The following is merely to make ZeroTier's OneService happy while building on Windows.
|
|
|
|
|
we won't use these in libzt */
|
|
|
|
|
NET_LUID _deviceLuid;
|
|
|
|
|
std::string _deviceInstanceId;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether the VirtualTap interface has been initialized
|
|
|
|
|
*/
|
|
|
|
|
bool isInitialized() const { return _initialized; };
|
|
|
|
|
|
|
|
|
|
inline const NET_LUID &luid() const { return _deviceLuid; }
|
|
|
|
|
inline const std::string &instanceId() const { return _deviceInstanceId; }
|
|
|
|
|
#endif
|
|
|
|
|
/**
|
|
|
|
|
* For moving data onto the ZeroTier virtual wire
|
|
|
|
|
*/
|
2019-01-14 12:01:29 -08:00
|
|
|
void (*_handler)(void *, void *, uint64_t, const MAC &, const MAC &, unsigned int, unsigned int,
|
2017-11-06 13:50:20 -08:00
|
|
|
const void *, unsigned int);
|
|
|
|
|
|
2019-01-31 03:08:48 -08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Lower-level lwIP netif handling and traffic handling readiness //
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2020-04-13 23:38:06 -07:00
|
|
|
void *netif4 = NULL;
|
|
|
|
|
void *netif6 = NULL;
|
2019-01-31 03:08:48 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The last time that this virtual tap received a network config update from the core
|
|
|
|
|
*/
|
|
|
|
|
uint64_t _lastConfigUpdateTime = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The last time that a callback notification was sent to the user application signalling
|
|
|
|
|
* that this interface is ready to process traffic.
|
|
|
|
|
*/
|
|
|
|
|
uint64_t _lastReadyReportTime = 0;
|
|
|
|
|
|
|
|
|
|
void lastConfigUpdate(uint64_t lastConfigUpdateTime);
|
|
|
|
|
|
|
|
|
|
int _networkStatus = 0;
|
|
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Vars //
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2017-11-06 13:50:20 -08:00
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
std::vector<std::pair<InetAddress, InetAddress> > routes;
|
2017-11-06 13:50:20 -08:00
|
|
|
|
|
|
|
|
char vtap_full_name[64];
|
|
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
std::vector<InetAddress> ips() const;
|
|
|
|
|
std::vector<InetAddress> _ips;
|
2017-11-06 13:50:20 -08:00
|
|
|
|
|
|
|
|
std::string _homePath;
|
|
|
|
|
void *_arg;
|
|
|
|
|
volatile bool _initialized;
|
|
|
|
|
volatile bool _enabled;
|
|
|
|
|
volatile bool _run;
|
2019-01-14 12:01:29 -08:00
|
|
|
MAC _mac;
|
2017-11-06 13:50:20 -08:00
|
|
|
unsigned int _mtu;
|
|
|
|
|
uint64_t _nwid;
|
2019-01-14 12:01:29 -08:00
|
|
|
PhySocket *_unixListenSocket;
|
|
|
|
|
Phy<VirtualTap *> _phy;
|
2017-11-06 13:50:20 -08:00
|
|
|
|
|
|
|
|
Thread _thread;
|
2019-01-14 12:01:29 -08:00
|
|
|
|
|
|
|
|
int _shutdownSignalPipe[2];
|
|
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
std::string _dev; // path to Unix domain socket
|
|
|
|
|
|
|
|
|
|
std::vector<MulticastGroup> _multicastGroups;
|
|
|
|
|
Mutex _multicastGroups_m;
|
2019-01-14 12:01:29 -08:00
|
|
|
|
2017-11-06 13:50:20 -08:00
|
|
|
/*
|
|
|
|
|
* Timestamp of last run of housekeeping
|
2019-03-04 18:04:37 -08:00
|
|
|
* SEE: ZT_HOUSEKEEPING_INTERVAL in ZeroTier.h
|
2017-11-06 13:50:20 -08:00
|
|
|
*/
|
|
|
|
|
uint64_t last_housekeeping_ts = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
2019-01-14 12:01:29 -08:00
|
|
|
* Performs miscellaneous background tasks
|
2017-11-06 13:50:20 -08:00
|
|
|
*/
|
|
|
|
|
void Housekeeping();
|
|
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Not used in this implementation //
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2017-11-06 13:50:20 -08:00
|
|
|
|
|
|
|
|
void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *local_address,
|
|
|
|
|
const struct sockaddr *from,void *data,unsigned long len);
|
|
|
|
|
void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success);
|
|
|
|
|
void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,
|
|
|
|
|
const struct sockaddr *from);
|
|
|
|
|
void phyOnTcpClose(PhySocket *sock,void **uptr);
|
|
|
|
|
void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len);
|
|
|
|
|
void phyOnTcpWritable(PhySocket *sock,void **uptr);
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-14 12:01:29 -08:00
|
|
|
} // namespace ZeroTier
|
2017-11-06 13:50:20 -08:00
|
|
|
|
|
|
|
|
#endif // _H
|