Peer.hpp 16 KB

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