Peer.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. * @param bfg1024 Use BFG1024 brute force symmetric NAT busting algorithm if applicable
  211. */
  212. void contact(void *tPtr, int64_t now, const Endpoint &ep, bool breakSymmetricBFG1024);
  213. /**
  214. * Reset paths within a given IP scope and address family
  215. *
  216. * Resetting a path involves sending an ECHO to it and then deactivating
  217. * it until or unless it responds. This is done when we detect a change
  218. * to our external IP or another system change that might invalidate
  219. * many or all current paths.
  220. *
  221. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  222. * @param scope IP scope
  223. * @param inetAddressFamily Family e.g. AF_INET
  224. * @param now Current time
  225. */
  226. void resetWithinScope(void *tPtr, InetAddress::IpScope scope, int inetAddressFamily, int64_t now);
  227. /**
  228. * @return Time of last receive of anything, whether direct or relayed
  229. */
  230. ZT_INLINE int64_t lastReceive() const noexcept
  231. { return m_lastReceive; }
  232. /**
  233. * @return Average latency of all direct paths or -1 if no direct paths or unknown
  234. */
  235. ZT_INLINE int latency() const noexcept
  236. {
  237. int ltot = 0;
  238. int lcnt = 0;
  239. RWMutex::RLock l(m_lock);
  240. for (unsigned int i = 0;i < m_alivePathCount;++i) {
  241. int lat = m_paths[i]->latency();
  242. if (lat > 0) {
  243. ltot += lat;
  244. ++lcnt;
  245. }
  246. }
  247. return (ltot > 0) ? (lcnt / ltot) : -1;
  248. }
  249. /**
  250. * @return Cipher suite that should be used to communicate with this peer
  251. */
  252. ZT_INLINE uint8_t cipher() const noexcept
  253. {
  254. //if (m_vProto >= 11)
  255. // return ZT_PROTO_CIPHER_SUITE__AES_GMAC_SIV;
  256. return ZT_PROTO_CIPHER_SUITE__POLY1305_SALSA2012;
  257. }
  258. /**
  259. * @return The permanent shared key for this peer computed by simple identity agreement
  260. */
  261. ZT_INLINE SharedPtr<SymmetricKey> identityKey() noexcept
  262. {
  263. return m_identityKey;
  264. }
  265. /**
  266. * @return AES instance for HELLO dictionary / encrypted section encryption/decryption
  267. */
  268. ZT_INLINE const AES &identityHelloDictionaryEncryptionCipher() noexcept
  269. {
  270. return m_helloCipher;
  271. }
  272. /**
  273. * @return Key for HMAC on HELLOs
  274. */
  275. ZT_INLINE const uint8_t *identityHelloHmacKey() noexcept
  276. {
  277. return m_helloMacKey;
  278. }
  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. {
  306. return (m_identityKey != k);
  307. }
  308. /**
  309. * Set the currently known remote version of this peer's client
  310. *
  311. * @param vproto Protocol version
  312. * @param vmaj Major version
  313. * @param vmin Minor version
  314. * @param vrev Revision
  315. */
  316. ZT_INLINE void setRemoteVersion(unsigned int vproto, unsigned int vmaj, unsigned int vmin, unsigned int vrev) noexcept
  317. {
  318. m_vProto = (uint16_t) vproto;
  319. m_vMajor = (uint16_t) vmaj;
  320. m_vMinor = (uint16_t) vmin;
  321. m_vRevision = (uint16_t) vrev;
  322. }
  323. ZT_INLINE unsigned int remoteVersionProtocol() const noexcept
  324. { return m_vProto; }
  325. ZT_INLINE unsigned int remoteVersionMajor() const noexcept
  326. { return m_vMajor; }
  327. ZT_INLINE unsigned int remoteVersionMinor() const noexcept
  328. { return m_vMinor; }
  329. ZT_INLINE unsigned int remoteVersionRevision() const noexcept
  330. { return m_vRevision; }
  331. ZT_INLINE bool remoteVersionKnown() const noexcept
  332. { return ((m_vMajor > 0) || (m_vMinor > 0) || (m_vRevision > 0)); }
  333. /**
  334. * @return True if there is at least one alive direct path
  335. */
  336. bool directlyConnected(int64_t now);
  337. /**
  338. * Get all paths
  339. *
  340. * @param paths Vector of paths with the first path being the current preferred path
  341. */
  342. void getAllPaths(Vector< SharedPtr<Path> > &paths);
  343. /**
  344. * Save the latest version of this peer to the data store
  345. */
  346. void save(void *tPtr) const;
  347. // NOTE: peer marshal/unmarshal only saves/restores the identity, locator, most
  348. // recent bootstrap address, and version information.
  349. static constexpr int marshalSizeMax() noexcept
  350. { return ZT_PEER_MARSHAL_SIZE_MAX; }
  351. int marshal(uint8_t data[ZT_PEER_MARSHAL_SIZE_MAX]) const noexcept;
  352. int unmarshal(const uint8_t *restrict data, int len) noexcept;
  353. /**
  354. * Rate limit gate for inbound WHOIS requests
  355. */
  356. ZT_INLINE bool rateGateInboundWhoisRequest(const int64_t now) noexcept
  357. {
  358. if ((now - m_lastWhoisRequestReceived) >= ZT_PEER_WHOIS_RATE_LIMIT) {
  359. m_lastWhoisRequestReceived = now;
  360. return true;
  361. }
  362. return false;
  363. }
  364. /**
  365. * Rate limit gate for inbound ECHO requests
  366. */
  367. ZT_INLINE bool rateGateEchoRequest(const int64_t now) noexcept
  368. {
  369. if ((now - m_lastEchoRequestReceived) >= ZT_PEER_GENERAL_RATE_LIMIT) {
  370. m_lastEchoRequestReceived = now;
  371. return true;
  372. }
  373. return false;
  374. }
  375. /**
  376. * Rate limit gate for inbound probes
  377. */
  378. ZT_INLINE bool rateGateProbeRequest(const int64_t now) noexcept
  379. {
  380. if ((now - m_lastProbeReceived) > ZT_PEER_PROBE_RESPONSE_RATE_LIMIT) {
  381. m_lastProbeReceived = now;
  382. return true;
  383. }
  384. return false;
  385. }
  386. /**
  387. * Packet deduplication filter for incoming packets
  388. *
  389. * This flags a packet ID and returns true if the same packet ID was already
  390. * flagged. This is done in an atomic operation if supported.
  391. *
  392. * @param packetId Packet ID to check/flag
  393. * @return True if this is a duplicate
  394. */
  395. ZT_INLINE bool deduplicateIncomingPacket(const uint64_t packetId) noexcept
  396. {
  397. // TODO: should take instance ID into account too, but this isn't fully wired.
  398. return m_dedup[Utils::hash32((uint32_t) packetId) & ZT_PEER_DEDUP_BUFFER_MASK].exchange(packetId) == packetId;
  399. }
  400. private:
  401. void m_prioritizePaths(int64_t now);
  402. unsigned int m_sendProbe(void *tPtr, int64_t localSocket, const InetAddress &atAddress, const uint16_t *ports, unsigned int numPorts, int64_t now);
  403. void m_deriveSecondaryIdentityKeys() noexcept;
  404. ZT_INLINE SharedPtr<SymmetricKey> m_key() noexcept
  405. {
  406. // assumes m_lock is locked (for read at least)
  407. return (m_ephemeralKeys[0]) ? m_ephemeralKeys[0] : m_identityKey;
  408. }
  409. const RuntimeEnvironment *RR;
  410. // Read/write mutex for non-atomic non-const fields.
  411. RWMutex m_lock;
  412. // Static identity key
  413. SharedPtr<SymmetricKey> m_identityKey;
  414. // Cipher for encrypting or decrypting the encrypted section of HELLO packets.
  415. AES m_helloCipher;
  416. // Key for HELLO HMAC-SHA384
  417. uint8_t m_helloMacKey[ZT_SYMMETRIC_KEY_SIZE];
  418. // Currently active ephemeral public key pair
  419. EphemeralKey m_ephemeralPair;
  420. int64_t m_ephemeralPairTimestamp;
  421. // Current and previous ephemeral key
  422. SharedPtr<SymmetricKey> m_ephemeralKeys[2];
  423. Identity m_id;
  424. SharedPtr<const Locator> m_locator;
  425. // the last time something was sent or received from this peer (direct or indirect).
  426. std::atomic<int64_t> m_lastReceive;
  427. std::atomic<int64_t> m_lastSend;
  428. // The last time we sent a full HELLO to this peer.
  429. int64_t m_lastSentHello; // only checked while locked
  430. // The last time a WHOIS request was received from this peer (anti-DOS / anti-flood).
  431. std::atomic<int64_t> m_lastWhoisRequestReceived;
  432. // The last time an ECHO request was received from this peer (anti-DOS / anti-flood).
  433. std::atomic<int64_t> m_lastEchoRequestReceived;
  434. // The last time we sorted paths in order of preference. (This happens pretty often.)
  435. std::atomic<int64_t> m_lastPrioritizedPaths;
  436. // The last time we got a probe from this peer.
  437. std::atomic<int64_t> m_lastProbeReceived;
  438. // Deduplication buffer
  439. std::atomic<uint64_t> m_dedup[ZT_PEER_DEDUP_BUFFER_SIZE];
  440. // Meters measuring actual bandwidth in, out, and relayed via this peer (mostly if this is a root).
  441. Meter<> m_inMeter;
  442. Meter<> m_outMeter;
  443. Meter<> m_relayedMeter;
  444. // Direct paths sorted in descending order of preference.
  445. SharedPtr<Path> m_paths[ZT_MAX_PEER_NETWORK_PATHS];
  446. // Number of paths current alive (number of non-NULL entries in _paths).
  447. unsigned int m_alivePathCount;
  448. // For SharedPtr<>
  449. std::atomic<int> __refCount;
  450. // Addresses recieved via PUSH_DIRECT_PATHS etc. that we are scheduled to try.
  451. struct p_TryQueueItem
  452. {
  453. ZT_INLINE p_TryQueueItem() : ts(0), target(), natMustDie(false)
  454. {}
  455. ZT_INLINE p_TryQueueItem(const int64_t now, const Endpoint &t, const bool nmd) : ts(now), target(t), natMustDie(nmd)
  456. {}
  457. int64_t ts;
  458. Endpoint target;
  459. bool natMustDie;
  460. };
  461. List<p_TryQueueItem> m_tryQueue;
  462. uint16_t m_vProto;
  463. uint16_t m_vMajor;
  464. uint16_t m_vMinor;
  465. uint16_t m_vRevision;
  466. };
  467. } // namespace ZeroTier
  468. #endif