Peer.hpp 15 KB

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