Peer.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_PEER_HPP
  14. #define ZT_PEER_HPP
  15. #include "Constants.hpp"
  16. #include "RuntimeEnvironment.hpp"
  17. #include "Node.hpp"
  18. #include "Path.hpp"
  19. #include "Address.hpp"
  20. #include "Utils.hpp"
  21. #include "Identity.hpp"
  22. #include "InetAddress.hpp"
  23. #include "SharedPtr.hpp"
  24. #include "Mutex.hpp"
  25. #include "Endpoint.hpp"
  26. #include "Locator.hpp"
  27. #include "Protocol.hpp"
  28. #include "AES.hpp"
  29. #include "EphemeralKey.hpp"
  30. #include "SymmetricKey.hpp"
  31. #include "Containers.hpp"
  32. #define ZT_PEER_MARSHAL_SIZE_MAX ( \
  33. 1 + \
  34. ZT_ADDRESS_LENGTH + \
  35. ZT_SYMMETRIC_KEY_SIZE + \
  36. ZT_IDENTITY_MARSHAL_SIZE_MAX + \
  37. 1 + ZT_LOCATOR_MARSHAL_SIZE_MAX + \
  38. 2 + ((8 + ZT_ENDPOINT_MARSHAL_SIZE_MAX) * ZT_PEER_ENDPOINT_CACHE_SIZE) + \
  39. (2 * 4) + \
  40. 2 )
  41. #define ZT_PEER_DEDUP_BUFFER_SIZE 1024
  42. #define ZT_PEER_DEDUP_BUFFER_MASK 1023U
  43. namespace ZeroTier {
  44. class Topology;
  45. /**
  46. * Peer on P2P Network (virtual layer 1)
  47. */
  48. class Peer
  49. {
  50. friend class SharedPtr< Peer >;
  51. friend class Topology;
  52. public:
  53. /**
  54. * Create an uninitialized peer
  55. *
  56. * New peers must be initialized via either init() or unmarshal() prior to
  57. * use or null pointer dereference may occur.
  58. *
  59. * @param renv Runtime environment
  60. */
  61. explicit Peer(const RuntimeEnvironment *renv);
  62. ~Peer();
  63. /**
  64. * Initialize peer with an identity
  65. *
  66. * @param peerIdentity The peer's identity
  67. * @return True if initialization was succcesful
  68. */
  69. bool init(const Identity &peerIdentity);
  70. /**
  71. * @return This peer's ZT address (short for identity().address())
  72. */
  73. ZT_INLINE Address address() const noexcept
  74. { return m_id.address(); }
  75. /**
  76. * @return This peer's identity
  77. */
  78. ZT_INLINE const Identity &identity() const noexcept
  79. { return m_id; }
  80. /**
  81. * @return Current locator or NULL if no locator is known
  82. */
  83. ZT_INLINE const SharedPtr< const Locator > &locator() const noexcept
  84. {
  85. RWMutex::RLock l(m_lock);
  86. return m_locator;
  87. }
  88. /**
  89. * Set or update peer locator
  90. *
  91. * This checks the locator's timestamp against the current locator and
  92. * replace it if newer.
  93. *
  94. * SECURITY: note that this does NOT validate the locator's signature
  95. * or structural validity. This MUST be done before calling this.
  96. *
  97. * @param loc Locator update
  98. * @return New locator or previous if it was not replaced.
  99. */
  100. ZT_INLINE SharedPtr< const Locator > setLocator(const SharedPtr< const Locator > &loc) noexcept
  101. {
  102. RWMutex::Lock l(m_lock);
  103. if ((loc) && ((!m_locator) || (m_locator->timestamp() < loc->timestamp())))
  104. m_locator = loc;
  105. return m_locator;
  106. }
  107. /**
  108. * Log receipt of an authenticated packet
  109. *
  110. * This is called by the decode pipe when a packet is proven to be authentic
  111. * and appears to be valid.
  112. *
  113. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  114. * @param path Path over which packet was received
  115. * @param hops ZeroTier (not IP) hops
  116. * @param packetId Packet ID
  117. * @param verb Packet verb
  118. * @param inReVerb In-reply verb for OK or ERROR verbs
  119. */
  120. void received(
  121. void *tPtr,
  122. const SharedPtr< Path > &path,
  123. unsigned int hops,
  124. uint64_t packetId,
  125. unsigned int payloadLength,
  126. Protocol::Verb verb,
  127. Protocol::Verb inReVerb);
  128. /**
  129. * Log sent data
  130. *
  131. * @param now Current time
  132. * @param bytes Number of bytes written
  133. */
  134. ZT_INLINE void sent(const int64_t now, const unsigned int bytes) noexcept
  135. {
  136. m_lastSend = now;
  137. m_outMeter.log(now, bytes);
  138. }
  139. /**
  140. * Called when traffic destined for a different peer is sent to this one
  141. *
  142. * @param now Current time
  143. * @param bytes Number of bytes relayed
  144. */
  145. ZT_INLINE void relayed(const int64_t now, const unsigned int bytes) noexcept
  146. { m_relayedMeter.log(now, bytes); }
  147. /**
  148. * Get the current best direct path or NULL if none
  149. *
  150. * @return Current best path or NULL if there is no direct path
  151. */
  152. ZT_INLINE SharedPtr< Path > path(const int64_t now) noexcept
  153. {
  154. if (likely((now - m_lastPrioritizedPaths) < ZT_PEER_PRIORITIZE_PATHS_INTERVAL)) {
  155. RWMutex::RLock l(m_lock);
  156. if (m_alivePathCount > 0)
  157. return m_paths[0];
  158. } else {
  159. RWMutex::Lock l(m_lock);
  160. m_prioritizePaths(now);
  161. if (m_alivePathCount > 0)
  162. return m_paths[0];
  163. }
  164. return SharedPtr< Path >();
  165. }
  166. /**
  167. * Send data to this peer over a specific path only
  168. *
  169. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  170. * @param now Current time
  171. * @param data Data to send
  172. * @param len Length in bytes
  173. * @param via Path over which to send data (may or may not be an already-learned path for this peer)
  174. */
  175. ZT_INLINE void send(void *tPtr, int64_t now, const void *data, unsigned int len, const SharedPtr< Path > &via) noexcept
  176. {
  177. via->send(RR, tPtr, data, len, now);
  178. sent(now, len);
  179. }
  180. /**
  181. * Send data to this peer over the best available path
  182. *
  183. * If there is a working direct path it will be used. Otherwise the data will be
  184. * sent via a root server.
  185. *
  186. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  187. * @param now Current time
  188. * @param data Data to send
  189. * @param len Length in bytes
  190. */
  191. void send(void *tPtr, int64_t now, const void *data, unsigned int len) noexcept;
  192. /**
  193. * Send a HELLO to this peer at a specified physical address.
  194. *
  195. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  196. * @param localSocket Local source socket
  197. * @param atAddress Destination address
  198. * @param now Current time
  199. * @return Number of bytes sent
  200. */
  201. unsigned int hello(void *tPtr, int64_t localSocket, const InetAddress &atAddress, int64_t now);
  202. /**
  203. * Ping this peer if needed and/or perform other periodic tasks.
  204. *
  205. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  206. * @param now Current time
  207. * @param isRoot True if this peer is a root
  208. */
  209. void pulse(void *tPtr, int64_t now, bool isRoot);
  210. /**
  211. * Attempt to contact this peer at a given endpoint.
  212. *
  213. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  214. * @param now Current time
  215. * @param ep Endpoint to attempt to contact
  216. * @param tries Number of times to try (default: 1)
  217. */
  218. void contact(void *tPtr, int64_t now, const Endpoint &ep, int tries = 1);
  219. /**
  220. * Reset paths within a given IP scope and address family
  221. *
  222. * Resetting a path involves sending an ECHO to it and then deactivating
  223. * it until or unless it responds. This is done when we detect a change
  224. * to our external IP or another system change that might invalidate
  225. * many or all current paths.
  226. *
  227. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  228. * @param scope IP scope
  229. * @param inetAddressFamily Family e.g. AF_INET
  230. * @param now Current time
  231. */
  232. void resetWithinScope(void *tPtr, InetAddress::IpScope scope, int inetAddressFamily, int64_t now);
  233. /**
  234. * @return Time of last receive of anything, whether direct or relayed
  235. */
  236. ZT_INLINE int64_t lastReceive() const noexcept
  237. { return m_lastReceive; }
  238. /**
  239. * @return Average latency of all direct paths or -1 if no direct paths or unknown
  240. */
  241. ZT_INLINE int latency() const noexcept
  242. {
  243. int ltot = 0;
  244. int lcnt = 0;
  245. RWMutex::RLock l(m_lock);
  246. for (unsigned int i = 0; i < m_alivePathCount; ++i) {
  247. int lat = m_paths[i]->latency();
  248. if (lat > 0) {
  249. ltot += lat;
  250. ++lcnt;
  251. }
  252. }
  253. return (ltot > 0) ? (lcnt / ltot) : -1;
  254. }
  255. /**
  256. * @return Cipher suite that should be used to communicate with this peer
  257. */
  258. ZT_INLINE uint8_t cipher() const noexcept
  259. {
  260. //if (m_vProto >= 11)
  261. // return ZT_PROTO_CIPHER_SUITE__AES_GMAC_SIV;
  262. return ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012;
  263. }
  264. /**
  265. * @return The permanent shared key for this peer computed by simple identity agreement
  266. */
  267. ZT_INLINE SharedPtr< SymmetricKey > identityKey() noexcept
  268. { return m_identityKey; }
  269. /**
  270. * @return AES instance for HELLO dictionary / encrypted section encryption/decryption
  271. */
  272. ZT_INLINE const AES &identityHelloDictionaryEncryptionCipher() noexcept
  273. { return m_helloCipher; }
  274. /**
  275. * @return Key for HMAC on HELLOs
  276. */
  277. ZT_INLINE const uint8_t *identityHelloHmacKey() noexcept
  278. { return m_helloMacKey; }
  279. /**
  280. * @return Raw identity key bytes
  281. */
  282. ZT_INLINE const uint8_t *rawIdentityKey() noexcept
  283. {
  284. RWMutex::RLock l(m_lock);
  285. return m_identityKey->secret;
  286. }
  287. /**
  288. * @return Current best key: either the latest ephemeral or the identity key
  289. */
  290. ZT_INLINE SharedPtr< SymmetricKey > key() noexcept
  291. {
  292. RWMutex::RLock l(m_lock);
  293. return m_key();
  294. }
  295. /**
  296. * Check whether a key is ephemeral
  297. *
  298. * This is used to check whether a packet is received with forward secrecy enabled
  299. * or not.
  300. *
  301. * @param k Key to check
  302. * @return True if this key is ephemeral, false if it's the long-lived identity key
  303. */
  304. ZT_INLINE bool isEphemeral(const SharedPtr< SymmetricKey > &k) const noexcept
  305. { return m_identityKey != k; }
  306. /**
  307. * Set the currently known remote version of this peer's client
  308. *
  309. * @param vproto Protocol version
  310. * @param vmaj Major version
  311. * @param vmin Minor version
  312. * @param vrev Revision
  313. */
  314. ZT_INLINE void setRemoteVersion(unsigned int vproto, unsigned int vmaj, unsigned int vmin, unsigned int vrev) noexcept
  315. {
  316. m_vProto = (uint16_t)vproto;
  317. m_vMajor = (uint16_t)vmaj;
  318. m_vMinor = (uint16_t)vmin;
  319. m_vRevision = (uint16_t)vrev;
  320. }
  321. ZT_INLINE unsigned int remoteVersionProtocol() const noexcept
  322. { return m_vProto; }
  323. ZT_INLINE unsigned int remoteVersionMajor() const noexcept
  324. { return m_vMajor; }
  325. ZT_INLINE unsigned int remoteVersionMinor() const noexcept
  326. { return m_vMinor; }
  327. ZT_INLINE unsigned int remoteVersionRevision() const noexcept
  328. { return m_vRevision; }
  329. ZT_INLINE bool remoteVersionKnown() const noexcept
  330. { return (m_vMajor > 0) || (m_vMinor > 0) || (m_vRevision > 0); }
  331. /**
  332. * @return True if there is at least one alive direct path
  333. */
  334. bool directlyConnected(int64_t now);
  335. /**
  336. * Get all paths
  337. *
  338. * @param paths Vector of paths with the first path being the current preferred path
  339. */
  340. void getAllPaths(Vector< SharedPtr< Path > > &paths);
  341. /**
  342. * Save the latest version of this peer to the data store
  343. */
  344. void save(void *tPtr) const;
  345. // NOTE: peer marshal/unmarshal only saves/restores the identity, locator, most
  346. // recent bootstrap address, and version information.
  347. static constexpr int marshalSizeMax() noexcept
  348. { return ZT_PEER_MARSHAL_SIZE_MAX; }
  349. int marshal(uint8_t data[ZT_PEER_MARSHAL_SIZE_MAX]) const noexcept;
  350. int unmarshal(const uint8_t *restrict data, int len) noexcept;
  351. /**
  352. * Rate limit gate for inbound WHOIS requests
  353. */
  354. ZT_INLINE bool rateGateInboundWhoisRequest(const int64_t now) noexcept
  355. {
  356. if ((now - m_lastWhoisRequestReceived) >= ZT_PEER_WHOIS_RATE_LIMIT) {
  357. m_lastWhoisRequestReceived = now;
  358. return true;
  359. }
  360. return false;
  361. }
  362. /**
  363. * Rate limit gate for inbound ECHO requests
  364. */
  365. ZT_INLINE bool rateGateEchoRequest(const int64_t now) noexcept
  366. {
  367. if ((now - m_lastEchoRequestReceived) >= ZT_PEER_GENERAL_RATE_LIMIT) {
  368. m_lastEchoRequestReceived = now;
  369. return true;
  370. }
  371. return false;
  372. }
  373. /**
  374. * Rate limit gate for inbound probes
  375. */
  376. ZT_INLINE bool rateGateProbeRequest(const int64_t now) noexcept
  377. {
  378. if ((now - m_lastProbeReceived) > ZT_PEER_PROBE_RESPONSE_RATE_LIMIT) {
  379. m_lastProbeReceived = now;
  380. return true;
  381. }
  382. return false;
  383. }
  384. /**
  385. * Packet deduplication filter for incoming packets
  386. *
  387. * This flags a packet ID and returns true if the same packet ID was already
  388. * flagged. This is done in an atomic operation if supported.
  389. *
  390. * @param packetId Packet ID to check/flag
  391. * @return True if this is a duplicate
  392. */
  393. ZT_INLINE bool deduplicateIncomingPacket(const uint64_t packetId) noexcept
  394. {
  395. // TODO: should take instance ID into account too, but this isn't fully wired.
  396. return m_dedup[Utils::hash32((uint32_t)packetId) & ZT_PEER_DEDUP_BUFFER_MASK].exchange(packetId) == packetId;
  397. }
  398. private:
  399. void m_prioritizePaths(int64_t now);
  400. unsigned int m_sendProbe(void *tPtr, int64_t localSocket, const InetAddress &atAddress, const uint16_t *ports, unsigned int numPorts, int64_t now);
  401. void m_deriveSecondaryIdentityKeys() noexcept;
  402. ZT_INLINE SharedPtr< SymmetricKey > m_key() noexcept
  403. {
  404. // assumes m_lock is locked (for read at least)
  405. return (m_ephemeralKeys[0]) ? m_ephemeralKeys[0] : m_identityKey;
  406. }
  407. const RuntimeEnvironment *RR;
  408. // Read/write mutex for non-atomic non-const fields.
  409. RWMutex m_lock;
  410. // Static identity key
  411. SharedPtr< SymmetricKey > m_identityKey;
  412. // Cipher for encrypting or decrypting the encrypted section of HELLO packets.
  413. AES m_helloCipher;
  414. // Key for HELLO HMAC-SHA384
  415. uint8_t m_helloMacKey[ZT_SYMMETRIC_KEY_SIZE];
  416. // Currently active ephemeral public key pair
  417. EphemeralKey m_ephemeralPair;
  418. int64_t m_ephemeralPairTimestamp;
  419. // Current and previous ephemeral key
  420. SharedPtr< SymmetricKey > m_ephemeralKeys[2];
  421. Identity m_id;
  422. SharedPtr< const Locator > m_locator;
  423. // the last time something was sent or received from this peer (direct or indirect).
  424. std::atomic< int64_t > m_lastReceive;
  425. std::atomic< int64_t > m_lastSend;
  426. // The last time we sent a full HELLO to this peer.
  427. int64_t m_lastSentHello; // only checked while locked
  428. // The last time a WHOIS request was received from this peer (anti-DOS / anti-flood).
  429. std::atomic< int64_t > m_lastWhoisRequestReceived;
  430. // The last time an ECHO request was received from this peer (anti-DOS / anti-flood).
  431. std::atomic< int64_t > m_lastEchoRequestReceived;
  432. // The last time we sorted paths in order of preference. (This happens pretty often.)
  433. std::atomic< int64_t > m_lastPrioritizedPaths;
  434. // The last time we got a probe from this peer.
  435. std::atomic< int64_t > m_lastProbeReceived;
  436. // Deduplication buffer
  437. std::atomic< uint64_t > m_dedup[ZT_PEER_DEDUP_BUFFER_SIZE];
  438. // Meters measuring actual bandwidth in, out, and relayed via this peer (mostly if this is a root).
  439. Meter<> m_inMeter;
  440. Meter<> m_outMeter;
  441. Meter<> m_relayedMeter;
  442. // Direct paths sorted in descending order of preference.
  443. SharedPtr< Path > m_paths[ZT_MAX_PEER_NETWORK_PATHS];
  444. // Number of paths current alive (number of non-NULL entries in _paths).
  445. unsigned int m_alivePathCount;
  446. // For SharedPtr<>
  447. std::atomic< int > __refCount;
  448. struct p_EndpointCacheItem
  449. {
  450. Endpoint target;
  451. int64_t lastSeen;
  452. ZT_INLINE bool operator<(const p_EndpointCacheItem &ci) const noexcept
  453. { return lastSeen < ci.lastSeen; }
  454. ZT_INLINE p_EndpointCacheItem() noexcept: target(), lastSeen(0)
  455. {}
  456. };
  457. // Endpoint cache sorted in ascending order of times seen followed by first seen time.
  458. p_EndpointCacheItem m_endpointCache[ZT_PEER_ENDPOINT_CACHE_SIZE];
  459. struct p_TryQueueItem
  460. {
  461. ZT_INLINE p_TryQueueItem() :
  462. target(),
  463. iteration(0)
  464. {}
  465. ZT_INLINE p_TryQueueItem(const Endpoint &t, int iter) :
  466. target(t),
  467. iteration(iter)
  468. {}
  469. Endpoint target;
  470. int iteration;
  471. };
  472. List< p_TryQueueItem > m_tryQueue;
  473. Map< Endpoint, int64_t > m_lastTried;
  474. uint16_t m_vProto;
  475. uint16_t m_vMajor;
  476. uint16_t m_vMinor;
  477. uint16_t m_vRevision;
  478. };
  479. } // namespace ZeroTier
  480. #endif