2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
|
|
|
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
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/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
|
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
*
|
|
|
|
* If you would like to embed ZeroTier into a commercial application or
|
|
|
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
|
|
* LLC. Start here: http://www.zerotier.com/
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
~Switch();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a packet is received from the real network
|
|
|
|
*
|
|
|
|
* @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
|
|
|
*/
|
2015-05-21 22:58:26 +00:00
|
|
|
void onRemotePacket(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
|
|
|
|
*
|
|
|
|
* @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
|
|
|
*/
|
2015-04-08 02:31:11 +00:00
|
|
|
void onLocalEthernet(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
|
|
|
*
|
|
|
|
* The network ID should only be specified for frames and other actual
|
2015-07-07 17:02:48 +00:00
|
|
|
* network traffic. Other traffic such as controller requests and regular
|
|
|
|
* protocol messages should specify zero.
|
2015-07-13 15:33:02 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param packet Packet to send
|
|
|
|
* @param encrypt Encrypt packet payload? (always true except for HELLO)
|
2015-07-07 17:00:34 +00:00
|
|
|
* @param nwid Related network ID or 0 if message is not in-network traffic
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2015-06-02 00:50:44 +00:00
|
|
|
void send(const Packet &packet,bool encrypt,uint64_t nwid);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send RENDEZVOUS to two peers to permit them to directly connect
|
|
|
|
*
|
|
|
|
* This only works if both peers are known, with known working direct
|
|
|
|
* links to this peer. The best link for each peer is sent to the other.
|
|
|
|
*
|
|
|
|
* A rate limiter is in effect via the _lastUniteAttempt map. If force
|
|
|
|
* is true, a unite attempt is made even if one has been made less than
|
|
|
|
* ZT_MIN_UNITE_INTERVAL milliseconds ago.
|
|
|
|
*
|
|
|
|
* @param p1 One of two peers (order doesn't matter)
|
|
|
|
* @param p2 Second of pair
|
|
|
|
* @param force If true, send now regardless of interval
|
|
|
|
*/
|
|
|
|
bool unite(const Address &p1,const Address &p2,bool force);
|
|
|
|
|
2013-07-12 02:06:25 +00:00
|
|
|
/**
|
2015-07-28 18:28:47 +00:00
|
|
|
* Attempt NAT traversal to peer at a given physical address
|
2013-07-12 02:06:25 +00:00
|
|
|
*
|
|
|
|
* @param peer Peer to contact
|
|
|
|
* @param atAddr Address of peer
|
|
|
|
*/
|
2015-07-28 18:28:47 +00:00
|
|
|
void rendezvous(const SharedPtr<Peer> &peer,const InetAddress &atAddr);
|
2013-07-12 02:06:25 +00:00
|
|
|
|
2013-07-11 20:19:06 +00:00
|
|
|
/**
|
|
|
|
* Request WHOIS on a given address
|
|
|
|
*
|
|
|
|
* @param addr Address to look up
|
|
|
|
*/
|
2013-07-11 21:52:04 +00:00
|
|
|
void requestWhois(const Address &addr);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-10-16 21:47:26 +00:00
|
|
|
/**
|
|
|
|
* Cancel WHOIS for an address
|
|
|
|
*
|
|
|
|
* @param addr Address to cancel
|
|
|
|
*/
|
|
|
|
void cancelWhoisRequest(const Address &addr);
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param peer New peer
|
|
|
|
*/
|
|
|
|
void doAnythingWaitingForPeer(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.
|
|
|
|
*
|
|
|
|
* @param now Current time
|
2014-09-24 20:45:58 +00:00
|
|
|
* @return Number of milliseconds until doTimerTasks() should be run again
|
|
|
|
*/
|
2015-04-08 02:31:11 +00:00
|
|
|
unsigned long doTimerTasks(uint64_t now);
|
2014-09-24 20:45:58 +00:00
|
|
|
|
2013-07-12 02:06:25 +00:00
|
|
|
private:
|
2015-05-21 22:58:26 +00:00
|
|
|
void _handleRemotePacketFragment(const InetAddress &fromAddr,const void *data,unsigned int len);
|
|
|
|
void _handleRemotePacketHead(const InetAddress &fromAddr,const void *data,unsigned int len);
|
2015-06-02 00:50:44 +00:00
|
|
|
Address _sendWhoisRequest(const Address &addr,const Address *peersAlreadyConsulted,unsigned int numPeersAlreadyConsulted);
|
|
|
|
bool _trySend(const Packet &packet,bool encrypt,uint64_t nwid);
|
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
|
|
|
|
2014-09-24 20:45:58 +00:00
|
|
|
// Outsanding 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;
|
|
|
|
|
2014-09-24 20:45:58 +00:00
|
|
|
// Packet defragmentation queue -- comes before RX queue in path
|
|
|
|
struct DefragQueueEntry
|
|
|
|
{
|
2015-09-04 21:44:22 +00:00
|
|
|
DefragQueueEntry() : creationTime(0),totalFragments(0),haveFragments(0) {}
|
2014-09-24 20:45:58 +00:00
|
|
|
uint64_t creationTime;
|
|
|
|
SharedPtr<IncomingPacket> frag0;
|
|
|
|
Packet::Fragment frags[ZT_MAX_PACKET_FRAGMENTS - 1];
|
|
|
|
unsigned int totalFragments; // 0 if only frag0 received, waiting for frags
|
|
|
|
uint32_t haveFragments; // bit mask, LSB to MSB
|
|
|
|
};
|
2015-09-04 21:44:22 +00:00
|
|
|
Hashtable< uint64_t,DefragQueueEntry > _defragQueue;
|
2014-09-24 20:45:58 +00:00
|
|
|
Mutex _defragQueue_m;
|
|
|
|
|
|
|
|
// ZeroTier-layer RX queue of incoming packets in the process of being decoded
|
2014-10-04 05:03:19 +00:00
|
|
|
std::list< SharedPtr<IncomingPacket> > _rxQueue;
|
2013-07-11 21:52:04 +00:00
|
|
|
Mutex _rxQueue_m;
|
|
|
|
|
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() {}
|
2015-09-04 21:56:39 +00:00
|
|
|
TXQueueEntry(Address d,uint64_t ct,const Packet &p,bool enc,uint64_t nw) :
|
|
|
|
dest(d),
|
2013-07-11 20:19:06 +00:00
|
|
|
creationTime(ct),
|
2015-06-02 00:50:44 +00:00
|
|
|
nwid(nw),
|
2013-07-11 20:19:06 +00:00
|
|
|
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
|
|
|
uint64_t nwid;
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline unsigned long hashCode() const throw() { return ((unsigned long)x ^ (unsigned long)y); }
|
|
|
|
inline bool operator==(const _LastUniteKey &k) const throw() { return ((x == k.x)&&(y == k.y)); }
|
|
|
|
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;
|
2013-07-12 02:25:12 +00:00
|
|
|
|
2014-09-24 20:45:58 +00:00
|
|
|
// Active attempts to contact remote peers, including state of multi-phase NAT traversal
|
2013-07-12 02:25:12 +00:00
|
|
|
struct ContactQueueEntry
|
|
|
|
{
|
|
|
|
ContactQueueEntry() {}
|
2015-05-21 22:58:26 +00:00
|
|
|
ContactQueueEntry(const SharedPtr<Peer> &p,uint64_t ft,const InetAddress &a) :
|
2013-07-12 02:25:12 +00:00
|
|
|
peer(p),
|
|
|
|
fireAtTime(ft),
|
2015-04-03 23:52:53 +00:00
|
|
|
inaddr(a),
|
2015-05-22 20:11:55 +00:00
|
|
|
strategyIteration(0) {}
|
2013-07-12 02:25:12 +00:00
|
|
|
|
|
|
|
SharedPtr<Peer> peer;
|
|
|
|
uint64_t fireAtTime;
|
|
|
|
InetAddress inaddr;
|
2015-04-03 23:52:53 +00:00
|
|
|
unsigned int strategyIteration;
|
2013-07-12 02:25:12 +00:00
|
|
|
};
|
|
|
|
std::list<ContactQueueEntry> _contactQueue;
|
|
|
|
Mutex _contactQueue_m;
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|