2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2017-04-28 03:47:25 +00:00
|
|
|
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
2017-04-28 03:47:25 +00:00
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* 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.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
|
2013-12-07 00:49:20 +00:00
|
|
|
#ifndef ZT_N_SWITCH_HPP
|
|
|
|
#define ZT_N_SWITCH_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
2013-07-12 02:06:25 +00:00
|
|
|
#include <list>
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-09-17 19:46:56 +00:00
|
|
|
#include "Constants.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Mutex.hpp"
|
|
|
|
#include "MAC.hpp"
|
|
|
|
#include "NonCopyable.hpp"
|
|
|
|
#include "Packet.hpp"
|
|
|
|
#include "Utils.hpp"
|
|
|
|
#include "InetAddress.hpp"
|
|
|
|
#include "Topology.hpp"
|
|
|
|
#include "Array.hpp"
|
|
|
|
#include "Network.hpp"
|
|
|
|
#include "SharedPtr.hpp"
|
2014-09-24 16:04:09 +00:00
|
|
|
#include "IncomingPacket.hpp"
|
2015-09-04 21:44:22 +00:00
|
|
|
#include "Hashtable.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
class RuntimeEnvironment;
|
|
|
|
class Peer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core of the distributed Ethernet switch and protocol implementation
|
2014-09-24 20:45:58 +00:00
|
|
|
*
|
|
|
|
* This class is perhaps a bit misnamed, but it's basically where everything
|
|
|
|
* meets. Transport-layer ZT packets come in here, as do virtual network
|
|
|
|
* packets from tap devices, and this sends them where they need to go and
|
|
|
|
* wraps/unwraps accordingly. It also handles queues and timeouts and such.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
class Switch : NonCopyable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Switch(const RuntimeEnvironment *renv);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a packet is received from the real network
|
|
|
|
*
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2017-07-06 18:45:22 +00:00
|
|
|
* @param localSocket Local I/O socket as supplied by external code
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param fromAddr Internet IP address of origin
|
|
|
|
* @param data Packet data
|
2015-04-08 02:31:11 +00:00
|
|
|
* @param len Packet length
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2017-07-06 18:45:22 +00:00
|
|
|
void onRemotePacket(void *tPtr,const int64_t localSocket,const InetAddress &fromAddr,const void *data,unsigned int len);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a packet comes from a local Ethernet tap
|
|
|
|
*
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param network Which network's TAP did this packet come from?
|
|
|
|
* @param from Originating MAC address
|
|
|
|
* @param to Destination MAC address
|
|
|
|
* @param etherType Ethernet packet type
|
2015-04-08 02:31:11 +00:00
|
|
|
* @param vlanId VLAN ID or 0 if none
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param data Ethernet payload
|
2015-04-08 02:31:11 +00:00
|
|
|
* @param len Frame length
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2017-03-28 00:03:17 +00:00
|
|
|
void onLocalEthernet(void *tPtr,const SharedPtr<Network> &network,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a packet to a ZeroTier address (destination in packet)
|
2015-07-13 15:33:02 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* The packet must be fully composed with source and destination but not
|
|
|
|
* yet encrypted. If the destination peer is known the packet
|
|
|
|
* is sent immediately. Otherwise it is queued and a WHOIS is dispatched.
|
|
|
|
*
|
|
|
|
* The packet may be compressed. Compression isn't done here.
|
2015-07-13 15:33:02 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* Needless to say, the packet's source must be this node. Otherwise it
|
|
|
|
* won't be encrypted right. (This is not used for relaying.)
|
2015-07-07 17:00:34 +00:00
|
|
|
*
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2017-02-01 20:00:25 +00:00
|
|
|
* @param packet Packet to send (buffer may be modified)
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param encrypt Encrypt packet payload? (always true except for HELLO)
|
|
|
|
*/
|
2017-03-28 00:03:17 +00:00
|
|
|
void send(void *tPtr,Packet &packet,bool encrypt);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-07-11 20:19:06 +00:00
|
|
|
/**
|
|
|
|
* Request WHOIS on a given address
|
|
|
|
*
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2013-07-11 20:19:06 +00:00
|
|
|
* @param addr Address to look up
|
|
|
|
*/
|
2017-03-28 00:03:17 +00:00
|
|
|
void requestWhois(void *tPtr,const Address &addr);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-07-12 02:06:25 +00:00
|
|
|
/**
|
2014-09-24 20:45:58 +00:00
|
|
|
* Run any processes that are waiting for this peer's identity
|
2013-07-12 02:06:25 +00:00
|
|
|
*
|
|
|
|
* Called when we learn of a peer's identity from HELLO, OK(WHOIS), etc.
|
|
|
|
*
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2013-07-12 02:06:25 +00:00
|
|
|
* @param peer New peer
|
|
|
|
*/
|
2017-03-28 00:03:17 +00:00
|
|
|
void doAnythingWaitingForPeer(void *tPtr,const SharedPtr<Peer> &peer);
|
2013-07-11 20:19:06 +00:00
|
|
|
|
2014-09-24 20:45:58 +00:00
|
|
|
/**
|
|
|
|
* Perform retries and other periodic timer tasks
|
2015-07-13 15:33:02 +00:00
|
|
|
*
|
2015-04-08 02:31:11 +00:00
|
|
|
* This can return a very long delay if there are no pending timer
|
|
|
|
* tasks. The caller should cap this comparatively vs. other values.
|
|
|
|
*
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2015-04-08 02:31:11 +00:00
|
|
|
* @param now Current time
|
2014-09-24 20:45:58 +00:00
|
|
|
* @return Number of milliseconds until doTimerTasks() should be run again
|
|
|
|
*/
|
2017-03-28 00:03:17 +00:00
|
|
|
unsigned long doTimerTasks(void *tPtr,uint64_t now);
|
2014-09-24 20:45:58 +00:00
|
|
|
|
2013-07-12 02:06:25 +00:00
|
|
|
private:
|
2017-02-04 08:04:44 +00:00
|
|
|
bool _shouldUnite(const uint64_t now,const Address &source,const Address &destination);
|
2017-03-28 00:03:17 +00:00
|
|
|
Address _sendWhoisRequest(void *tPtr,const Address &addr,const Address *peersAlreadyConsulted,unsigned int numPeersAlreadyConsulted);
|
|
|
|
bool _trySend(void *tPtr,Packet &packet,bool encrypt); // packet is modified if return is true
|
2013-07-11 20:19:06 +00:00
|
|
|
|
2014-10-01 19:41:48 +00:00
|
|
|
const RuntimeEnvironment *const RR;
|
2015-07-13 15:33:02 +00:00
|
|
|
uint64_t _lastBeaconResponse;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2016-01-12 17:33:14 +00:00
|
|
|
// Outstanding WHOIS requests and how many retries they've undergone
|
2013-07-04 20:56:19 +00:00
|
|
|
struct WhoisRequest
|
|
|
|
{
|
2015-09-04 22:35:43 +00:00
|
|
|
WhoisRequest() : lastSent(0),retries(0) {}
|
2013-07-04 20:56:19 +00:00
|
|
|
uint64_t lastSent;
|
|
|
|
Address peersConsulted[ZT_MAX_WHOIS_RETRIES]; // by retry
|
|
|
|
unsigned int retries; // 0..ZT_MAX_WHOIS_RETRIES
|
|
|
|
};
|
2015-09-04 22:35:43 +00:00
|
|
|
Hashtable< Address,WhoisRequest > _outstandingWhoisRequests;
|
2013-07-04 20:56:19 +00:00
|
|
|
Mutex _outstandingWhoisRequests_m;
|
|
|
|
|
2016-03-18 21:16:07 +00:00
|
|
|
// Packets waiting for WHOIS replies or other decode info or missing fragments
|
|
|
|
struct RXQueueEntry
|
2014-09-24 20:45:58 +00:00
|
|
|
{
|
2016-03-18 21:16:07 +00:00
|
|
|
RXQueueEntry() : timestamp(0) {}
|
|
|
|
uint64_t timestamp; // 0 if entry is not in use
|
|
|
|
uint64_t packetId;
|
|
|
|
IncomingPacket frag0; // head of packet
|
|
|
|
Packet::Fragment frags[ZT_MAX_PACKET_FRAGMENTS - 1]; // later fragments (if any)
|
2014-09-24 20:45:58 +00:00
|
|
|
unsigned int totalFragments; // 0 if only frag0 received, waiting for frags
|
|
|
|
uint32_t haveFragments; // bit mask, LSB to MSB
|
2016-03-18 21:16:07 +00:00
|
|
|
bool complete; // if true, packet is complete
|
2014-09-24 20:45:58 +00:00
|
|
|
};
|
2016-03-18 21:16:07 +00:00
|
|
|
RXQueueEntry _rxQueue[ZT_RX_QUEUE_SIZE];
|
2017-08-08 20:21:10 +00:00
|
|
|
AtomicCounter _rxQueuePtr;
|
2013-07-11 21:52:04 +00:00
|
|
|
|
2017-08-08 20:21:10 +00:00
|
|
|
// Returns matching or next available RX queue entry
|
|
|
|
inline RXQueueEntry *_findRXQueueEntry(uint64_t packetId)
|
2016-03-18 21:16:07 +00:00
|
|
|
{
|
2017-08-08 20:21:10 +00:00
|
|
|
unsigned int ptr = static_cast<unsigned int>(_rxQueuePtr.load());
|
|
|
|
for(unsigned int k=0;k<ZT_RX_QUEUE_SIZE;++k) {
|
|
|
|
RXQueueEntry *rq = &(_rxQueue[--ptr % ZT_RX_QUEUE_SIZE]);
|
2016-03-18 21:16:07 +00:00
|
|
|
if ((rq->packetId == packetId)&&(rq->timestamp))
|
|
|
|
return rq;
|
|
|
|
}
|
2017-08-08 20:21:10 +00:00
|
|
|
return &(_rxQueue[static_cast<unsigned int>(++_rxQueuePtr) % ZT_RX_QUEUE_SIZE]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns next RX queue entry in ring buffer and increments ring counter
|
|
|
|
inline RXQueueEntry *_nextRXQueueEntry()
|
|
|
|
{
|
|
|
|
return &(_rxQueue[static_cast<unsigned int>(++_rxQueuePtr) % ZT_RX_QUEUE_SIZE]);
|
2016-03-18 21:16:07 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 21:56:39 +00:00
|
|
|
// ZeroTier-layer TX queue entry
|
2013-07-04 20:56:19 +00:00
|
|
|
struct TXQueueEntry
|
|
|
|
{
|
2013-07-11 20:19:06 +00:00
|
|
|
TXQueueEntry() {}
|
2016-08-09 22:45:26 +00:00
|
|
|
TXQueueEntry(Address d,uint64_t ct,const Packet &p,bool enc) :
|
2015-09-04 21:56:39 +00:00
|
|
|
dest(d),
|
2013-07-11 20:19:06 +00:00
|
|
|
creationTime(ct),
|
|
|
|
packet(p),
|
|
|
|
encrypt(enc) {}
|
|
|
|
|
2015-09-04 21:56:39 +00:00
|
|
|
Address dest;
|
2013-07-04 20:56:19 +00:00
|
|
|
uint64_t creationTime;
|
2015-06-02 00:50:44 +00:00
|
|
|
Packet packet; // unencrypted/unMAC'd packet -- this is done at send time
|
2013-07-04 20:56:19 +00:00
|
|
|
bool encrypt;
|
|
|
|
};
|
2015-09-04 21:56:39 +00:00
|
|
|
std::list< TXQueueEntry > _txQueue;
|
2013-07-04 20:56:19 +00:00
|
|
|
Mutex _txQueue_m;
|
|
|
|
|
2014-09-24 20:45:58 +00:00
|
|
|
// Tracks sending of VERB_RENDEZVOUS to relaying peers
|
2015-09-04 22:21:22 +00:00
|
|
|
struct _LastUniteKey
|
|
|
|
{
|
|
|
|
_LastUniteKey() : x(0),y(0) {}
|
|
|
|
_LastUniteKey(const Address &a1,const Address &a2)
|
|
|
|
{
|
|
|
|
if (a1 > a2) {
|
|
|
|
x = a2.toInt();
|
|
|
|
y = a1.toInt();
|
|
|
|
} else {
|
|
|
|
x = a1.toInt();
|
|
|
|
y = a2.toInt();
|
|
|
|
}
|
|
|
|
}
|
2017-07-17 21:21:09 +00:00
|
|
|
inline unsigned long hashCode() const { return ((unsigned long)x ^ (unsigned long)y); }
|
|
|
|
inline bool operator==(const _LastUniteKey &k) const { return ((x == k.x)&&(y == k.y)); }
|
2015-09-04 22:21:22 +00:00
|
|
|
uint64_t x,y;
|
|
|
|
};
|
|
|
|
Hashtable< _LastUniteKey,uint64_t > _lastUniteAttempt; // key is always sorted in ascending order, for set-like behavior
|
2013-07-04 20:56:19 +00:00
|
|
|
Mutex _lastUniteAttempt_m;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|