Peer.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. * Log receipt of an authenticated packet
  80. *
  81. * This is called by the decode pipe when a packet is proven to be authentic
  82. * and appears to be valid.
  83. *
  84. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  85. * @param path Path over which packet was received
  86. * @param hops ZeroTier (not IP) hops
  87. * @param packetId Packet ID
  88. * @param verb Packet verb
  89. * @param inReVerb In-reply verb for OK or ERROR verbs
  90. */
  91. void received(
  92. void *tPtr,
  93. const SharedPtr<Path> &path,
  94. unsigned int hops,
  95. uint64_t packetId,
  96. unsigned int payloadLength,
  97. Protocol::Verb verb,
  98. Protocol::Verb inReVerb);
  99. /**
  100. * Log sent data
  101. *
  102. * @param now Current time
  103. * @param bytes Number of bytes written
  104. */
  105. ZT_INLINE void sent(const int64_t now,const unsigned int bytes) noexcept
  106. {
  107. m_lastSend = now;
  108. m_outMeter.log(now, bytes);
  109. }
  110. /**
  111. * Called when traffic destined for a different peer is sent to this one
  112. *
  113. * @param now Current time
  114. * @param bytes Number of bytes relayed
  115. */
  116. ZT_INLINE void relayed(const int64_t now,const unsigned int bytes) noexcept
  117. {
  118. m_relayedMeter.log(now, bytes);
  119. }
  120. /**
  121. * Get the current best direct path or NULL if none
  122. *
  123. * @return Current best path or NULL if there is no direct path
  124. */
  125. ZT_INLINE SharedPtr<Path> path(const int64_t now) noexcept
  126. {
  127. if (likely((now - m_lastPrioritizedPaths) < ZT_PEER_PRIORITIZE_PATHS_INTERVAL)) {
  128. RWMutex::RLock l(m_lock);
  129. if (m_alivePathCount > 0)
  130. return m_paths[0];
  131. } else {
  132. RWMutex::Lock l(m_lock);
  133. m_prioritizePaths(now);
  134. if (m_alivePathCount > 0)
  135. return m_paths[0];
  136. }
  137. return SharedPtr<Path>();
  138. }
  139. /**
  140. * Send data to this peer over a specific path only
  141. *
  142. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  143. * @param now Current time
  144. * @param data Data to send
  145. * @param len Length in bytes
  146. * @param via Path over which to send data (may or may not be an already-learned path for this peer)
  147. */
  148. ZT_INLINE void send(void *tPtr,int64_t now,const void *data,unsigned int len,const SharedPtr<Path> &via) noexcept
  149. {
  150. via->send(RR,tPtr,data,len,now);
  151. sent(now,len);
  152. }
  153. /**
  154. * Send data to this peer over the best available path
  155. *
  156. * If there is a working direct path it will be used. Otherwise the data will be
  157. * sent via a root server.
  158. *
  159. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  160. * @param now Current time
  161. * @param data Data to send
  162. * @param len Length in bytes
  163. */
  164. void send(void *tPtr,int64_t now,const void *data,unsigned int len) noexcept;
  165. /**
  166. * Send a HELLO to this peer at a specified physical address.
  167. *
  168. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  169. * @param localSocket Local source socket
  170. * @param atAddress Destination address
  171. * @param now Current time
  172. * @return Number of bytes sent
  173. */
  174. unsigned int hello(void *tPtr,int64_t localSocket,const InetAddress &atAddress,int64_t now);
  175. /**
  176. * Ping this peer if needed and/or perform other periodic tasks.
  177. *
  178. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  179. * @param now Current time
  180. * @param isRoot True if this peer is a root
  181. */
  182. void pulse(void *tPtr,int64_t now,bool isRoot);
  183. /**
  184. * Attempt to contact this peer at a given endpoint.
  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 ep Endpoint to attempt to contact
  189. * @param bfg1024 Use BFG1024 brute force symmetric NAT busting algorithm if applicable
  190. */
  191. void contact(void *tPtr,int64_t now,const Endpoint &ep,bool breakSymmetricBFG1024);
  192. /**
  193. * Reset paths within a given IP scope and address family
  194. *
  195. * Resetting a path involves sending an ECHO to it and then deactivating
  196. * it until or unless it responds. This is done when we detect a change
  197. * to our external IP or another system change that might invalidate
  198. * many or all current paths.
  199. *
  200. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  201. * @param scope IP scope
  202. * @param inetAddressFamily Family e.g. AF_INET
  203. * @param now Current time
  204. */
  205. void resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddressFamily,int64_t now);
  206. /**
  207. * @return All currently memorized bootstrap endpoints
  208. */
  209. ZT_INLINE FCV<Endpoint,ZT_MAX_PEER_NETWORK_PATHS> bootstrap() const noexcept
  210. {
  211. RWMutex::RLock l(m_lock);
  212. FCV<Endpoint,ZT_MAX_PEER_NETWORK_PATHS> r;
  213. 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)
  214. r.push_back(i->second);
  215. return r;
  216. }
  217. /**
  218. * Set bootstrap endpoint
  219. *
  220. * @param ep Bootstrap endpoint
  221. */
  222. ZT_INLINE void setBootstrap(const Endpoint &ep) noexcept
  223. {
  224. RWMutex::Lock l(m_lock);
  225. m_bootstrap[ep.type()] = ep;
  226. }
  227. /**
  228. * @return Time of last receive of anything, whether direct or relayed
  229. */
  230. ZT_INLINE int64_t lastReceive() const noexcept { 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 { return m_vProto; }
  323. ZT_INLINE unsigned int remoteVersionMajor() const noexcept { return m_vMajor; }
  324. ZT_INLINE unsigned int remoteVersionMinor() const noexcept { return m_vMinor; }
  325. ZT_INLINE unsigned int remoteVersionRevision() const noexcept { return m_vRevision; }
  326. ZT_INLINE bool remoteVersionKnown() const noexcept { return ((m_vMajor > 0) || (m_vMinor > 0) || (m_vRevision > 0)); }
  327. /**
  328. * @return True if there is at least one alive direct path
  329. */
  330. bool directlyConnected(int64_t now);
  331. /**
  332. * Get all paths
  333. *
  334. * @param paths Vector of paths with the first path being the current preferred path
  335. */
  336. void getAllPaths(Vector< SharedPtr<Path> > &paths);
  337. /**
  338. * Save the latest version of this peer to the data store
  339. */
  340. void save(void *tPtr) const;
  341. // NOTE: peer marshal/unmarshal only saves/restores the identity, locator, most
  342. // recent bootstrap address, and version information.
  343. static constexpr int marshalSizeMax() noexcept { return ZT_PEER_MARSHAL_SIZE_MAX; }
  344. int marshal(uint8_t data[ZT_PEER_MARSHAL_SIZE_MAX]) const noexcept;
  345. int unmarshal(const uint8_t *restrict data,int len) noexcept;
  346. /**
  347. * Rate limit gate for inbound WHOIS requests
  348. */
  349. ZT_INLINE bool rateGateInboundWhoisRequest(const int64_t now) noexcept
  350. {
  351. if ((now - m_lastWhoisRequestReceived) >= ZT_PEER_WHOIS_RATE_LIMIT) {
  352. m_lastWhoisRequestReceived = now;
  353. return true;
  354. }
  355. return false;
  356. }
  357. /**
  358. * Rate limit gate for inbound ECHO requests
  359. */
  360. ZT_INLINE bool rateGateEchoRequest(const int64_t now) noexcept
  361. {
  362. if ((now - m_lastEchoRequestReceived) >= ZT_PEER_GENERAL_RATE_LIMIT) {
  363. m_lastEchoRequestReceived = now;
  364. return true;
  365. }
  366. return false;
  367. }
  368. /**
  369. * Rate limit gate for inbound probes
  370. */
  371. ZT_INLINE bool rateGateProbeRequest(const int64_t now) noexcept
  372. {
  373. if((now - m_lastProbeReceived) > ZT_PEER_PROBE_RESPONSE_RATE_LIMIT) {
  374. m_lastProbeReceived = now;
  375. return true;
  376. }
  377. return false;
  378. }
  379. /**
  380. * Packet deduplication filter for incoming packets
  381. *
  382. * This flags a packet ID and returns true if the same packet ID was already
  383. * flagged. This is done in an atomic operation if supported.
  384. *
  385. * @param packetId Packet ID to check/flag
  386. * @return True if this is a duplicate
  387. */
  388. ZT_INLINE bool deduplicateIncomingPacket(const uint64_t packetId) noexcept
  389. {
  390. // TODO: should take instance ID into account too, but this isn't fully wired.
  391. return m_dedup[Utils::hash32((uint32_t)packetId) & ZT_PEER_DEDUP_BUFFER_MASK].exchange(packetId) == packetId;
  392. }
  393. private:
  394. void m_prioritizePaths(int64_t now);
  395. unsigned int m_sendProbe(void *tPtr,int64_t localSocket,const InetAddress &atAddress,const uint16_t *ports,unsigned int numPorts,int64_t now);
  396. void m_deriveSecondaryIdentityKeys() noexcept;
  397. ZT_INLINE SharedPtr<SymmetricKey> m_key() noexcept
  398. {
  399. // assumes m_lock is locked (for read at least)
  400. return (m_ephemeralKeys[0]) ? m_ephemeralKeys[0] : m_identityKey;
  401. }
  402. const RuntimeEnvironment *RR;
  403. // Read/write mutex for non-atomic non-const fields.
  404. RWMutex m_lock;
  405. // Static identity key
  406. SharedPtr<SymmetricKey> m_identityKey;
  407. // Cipher for encrypting or decrypting the encrypted section of HELLO packets.
  408. AES m_helloCipher;
  409. // Key for HELLO HMAC-SHA384
  410. uint8_t m_helloMacKey[ZT_SYMMETRIC_KEY_SIZE];
  411. // Currently active ephemeral public key pair
  412. EphemeralKey m_ephemeralPair;
  413. int64_t m_ephemeralPairTimestamp;
  414. // Current and previous ephemeral key
  415. SharedPtr<SymmetricKey> m_ephemeralKeys[2];
  416. Identity m_id;
  417. Locator m_locator;
  418. // the last time something was sent or received from this peer (direct or indirect).
  419. std::atomic<int64_t> m_lastReceive;
  420. std::atomic<int64_t> m_lastSend;
  421. // The last time we sent a full HELLO to this peer.
  422. int64_t m_lastSentHello; // only checked while locked
  423. // The last time a WHOIS request was received from this peer (anti-DOS / anti-flood).
  424. std::atomic<int64_t> m_lastWhoisRequestReceived;
  425. // The last time an ECHO request was received from this peer (anti-DOS / anti-flood).
  426. std::atomic<int64_t> m_lastEchoRequestReceived;
  427. // The last time we sorted paths in order of preference. (This happens pretty often.)
  428. std::atomic<int64_t> m_lastPrioritizedPaths;
  429. // The last time we got a probe from this peer.
  430. std::atomic<int64_t> m_lastProbeReceived;
  431. // Deduplication buffer
  432. std::atomic<uint64_t> m_dedup[ZT_PEER_DEDUP_BUFFER_SIZE];
  433. // Meters measuring actual bandwidth in, out, and relayed via this peer (mostly if this is a root).
  434. Meter<> m_inMeter;
  435. Meter<> m_outMeter;
  436. Meter<> m_relayedMeter;
  437. // Direct paths sorted in descending order of preference.
  438. SharedPtr<Path> m_paths[ZT_MAX_PEER_NETWORK_PATHS];
  439. // For SharedPtr<>
  440. std::atomic<int> __refCount;
  441. // Number of paths current alive (number of non-NULL entries in _paths).
  442. unsigned int m_alivePathCount;
  443. // Remembered addresses by endpoint type (std::map is smaller for only a few keys).
  444. SortedMap<Endpoint::Type,Endpoint> m_bootstrap;
  445. // Addresses recieved via PUSH_DIRECT_PATHS etc. that we are scheduled to try.
  446. struct p_TryQueueItem
  447. {
  448. ZT_INLINE p_TryQueueItem() : target(), ts(0), breakSymmetricBFG1024(false) {}
  449. ZT_INLINE p_TryQueueItem(const int64_t now, const Endpoint &t, const bool bfg) : target(t), ts(now), breakSymmetricBFG1024(bfg) {}
  450. Endpoint target;
  451. int64_t ts;
  452. bool breakSymmetricBFG1024;
  453. };
  454. List<p_TryQueueItem> m_tryQueue;
  455. List<p_TryQueueItem>::iterator m_tryQueuePtr; // loops over _tryQueue like a circular buffer
  456. uint16_t m_vProto;
  457. uint16_t m_vMajor;
  458. uint16_t m_vMinor;
  459. uint16_t m_vRevision;
  460. };
  461. } // namespace ZeroTier
  462. #endif