Node.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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_NODE_HPP
  14. #define ZT_NODE_HPP
  15. #include <cstdio>
  16. #include <cstdlib>
  17. #include <cstring>
  18. #include <map>
  19. #include <vector>
  20. #include "Constants.hpp"
  21. #include "../include/ZeroTierOne.h"
  22. #include "RuntimeEnvironment.hpp"
  23. #include "InetAddress.hpp"
  24. #include "Mutex.hpp"
  25. #include "MAC.hpp"
  26. #include "Network.hpp"
  27. #include "Path.hpp"
  28. #include "Salsa20.hpp"
  29. #include "NetworkController.hpp"
  30. #include "Hashtable.hpp"
  31. // Bit mask for "expecting reply" hash
  32. #define ZT_EXPECTING_REPLIES_BUCKET_MASK1 255
  33. #define ZT_EXPECTING_REPLIES_BUCKET_MASK2 31
  34. namespace ZeroTier {
  35. class Locator;
  36. /**
  37. * Implementation of Node object as defined in CAPI
  38. *
  39. * The pointer returned by ZT_Node_new() is an instance of this class.
  40. */
  41. class Node : public NetworkController::Sender
  42. {
  43. public:
  44. Node(void *uPtr,void *tPtr,const struct ZT_Node_Callbacks *callbacks,int64_t now);
  45. virtual ~Node();
  46. /**
  47. * Perform any operations that should be done prior to deleting a Node
  48. *
  49. * This is technically optional but recommended.
  50. *
  51. * @param tPtr Thread pointer to pass through to callbacks
  52. */
  53. void shutdown(void *tPtr);
  54. // Get rid of alignment warnings on 32-bit Windows and possibly improve performance
  55. #ifdef __WINDOWS__
  56. void * operator new(size_t i) { return _mm_malloc(i,16); }
  57. void operator delete(void* p) { _mm_free(p); }
  58. #endif
  59. // Public API Functions ----------------------------------------------------
  60. ZT_ResultCode processWirePacket(
  61. void *tptr,
  62. int64_t now,
  63. int64_t localSocket,
  64. const struct sockaddr_storage *remoteAddress,
  65. const void *packetData,
  66. unsigned int packetLength,
  67. volatile int64_t *nextBackgroundTaskDeadline);
  68. ZT_ResultCode processVirtualNetworkFrame(
  69. void *tptr,
  70. int64_t now,
  71. uint64_t nwid,
  72. uint64_t sourceMac,
  73. uint64_t destMac,
  74. unsigned int etherType,
  75. unsigned int vlanId,
  76. const void *frameData,
  77. unsigned int frameLength,
  78. volatile int64_t *nextBackgroundTaskDeadline);
  79. ZT_ResultCode processBackgroundTasks(void *tPtr, int64_t now, volatile int64_t *nextBackgroundTaskDeadline);
  80. ZT_ResultCode join(uint64_t nwid,void *uptr,void *tptr);
  81. ZT_ResultCode leave(uint64_t nwid,void **uptr,void *tptr);
  82. ZT_ResultCode multicastSubscribe(void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  83. ZT_ResultCode multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
  84. ZT_ResultCode addRoot(void *tptr,const ZT_Identity *identity,const sockaddr_storage *bootstrap);
  85. ZT_ResultCode removeRoot(void *tptr,const ZT_Identity *identity);
  86. uint64_t address() const;
  87. void status(ZT_NodeStatus *status) const;
  88. ZT_PeerList *peers() const;
  89. ZT_VirtualNetworkConfig *networkConfig(uint64_t nwid) const;
  90. ZT_VirtualNetworkList *networks() const;
  91. void setNetworkUserPtr(uint64_t nwid,void *ptr);
  92. void freeQueryResult(void *qr);
  93. void setInterfaceAddresses(const ZT_InterfaceAddress *addrs,unsigned int addrCount);
  94. int sendUserMessage(void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len);
  95. void setController(void *networkControllerInstance);
  96. // Internal functions ------------------------------------------------------
  97. /**
  98. * @return Most recent time value supplied to core via API
  99. */
  100. ZT_ALWAYS_INLINE int64_t now() const { return _now; }
  101. /**
  102. * Send packet to to the physical wire via callback
  103. *
  104. * @param tPtr Thread pointer
  105. * @param localSocket Local socket or -1 to use all/any
  106. * @param addr Destination address
  107. * @param data Data to send
  108. * @param len Length in bytes
  109. * @param ttl TTL or 0 for default/max
  110. * @return True if send appears successful
  111. */
  112. ZT_ALWAYS_INLINE bool putPacket(void *tPtr,const int64_t localSocket,const InetAddress &addr,const void *data,unsigned int len,unsigned int ttl = 0)
  113. {
  114. return (_cb.wirePacketSendFunction(
  115. reinterpret_cast<ZT_Node *>(this),
  116. _uPtr,
  117. tPtr,
  118. localSocket,
  119. reinterpret_cast<const struct sockaddr_storage *>(&addr),
  120. data,
  121. len,
  122. ttl) == 0);
  123. }
  124. /**
  125. * Inject frame into virtual Ethernet tap
  126. *
  127. * @param tPtr Thread pointer
  128. * @param nwid Network ID
  129. * @param nuptr Network-associated user pointer
  130. * @param source Source MAC address
  131. * @param dest Destination MAC address
  132. * @param etherType 16-bit Ethernet type
  133. * @param vlanId Ethernet VLAN ID (currently unused)
  134. * @param data Ethernet frame data
  135. * @param len Ethernet frame length in bytes
  136. */
  137. ZT_ALWAYS_INLINE void putFrame(void *tPtr,uint64_t nwid,void **nuptr,const MAC &source,const MAC &dest,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  138. {
  139. _cb.virtualNetworkFrameFunction(
  140. reinterpret_cast<ZT_Node *>(this),
  141. _uPtr,
  142. tPtr,
  143. nwid,
  144. nuptr,
  145. source.toInt(),
  146. dest.toInt(),
  147. etherType,
  148. vlanId,
  149. data,
  150. len);
  151. }
  152. /**
  153. * @param nwid Network ID
  154. * @return Network associated with ID
  155. */
  156. ZT_ALWAYS_INLINE SharedPtr<Network> network(uint64_t nwid) const
  157. {
  158. RWMutex::RLock l(_networks_m);
  159. return _networks[(unsigned long)((nwid + (nwid >> 32U)) & _networksMask)];
  160. }
  161. /**
  162. * @return Known local interface addresses for this node
  163. */
  164. ZT_ALWAYS_INLINE std::vector<ZT_InterfaceAddress> localInterfaceAddresses() const
  165. {
  166. Mutex::Lock _l(_localInterfaceAddresses_m);
  167. return _localInterfaceAddresses;
  168. }
  169. /**
  170. * Post an event via external callback
  171. *
  172. * @param tPtr Thread pointer
  173. * @param ev Event object
  174. * @param md Event data or NULL if none
  175. */
  176. ZT_ALWAYS_INLINE void postEvent(void *tPtr,ZT_Event ev,const void *md = (const void *)0)
  177. {
  178. _cb.eventCallback(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,ev,md);
  179. }
  180. /**
  181. * Post network port configuration via external callback
  182. *
  183. * @param tPtr Thread pointer
  184. * @param nwid Network ID
  185. * @param nuptr Network-associated user pointer
  186. * @param op Config operation or event type
  187. * @param nc Network config info
  188. */
  189. ZT_ALWAYS_INLINE void configureVirtualNetworkPort(void *tPtr,uint64_t nwid,void **nuptr,ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nc)
  190. {
  191. _cb.virtualNetworkConfigFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,nwid,nuptr,op,nc);
  192. }
  193. /**
  194. * @return True if node appears online
  195. */
  196. ZT_ALWAYS_INLINE bool online() const { return _online; }
  197. /**
  198. * Get a state object
  199. *
  200. * @param tPtr Thread pointer
  201. * @param type Object type to get
  202. * @param id Object ID
  203. * @return Vector containing data or empty vector if not found or empty
  204. */
  205. std::vector<uint8_t> stateObjectGet(void *const tPtr,ZT_StateObjectType type,const uint64_t id[2]);
  206. /**
  207. * Store a state object
  208. *
  209. * @param tPtr Thread pointer
  210. * @param type Object type to get
  211. * @param id Object ID
  212. * @param data Data to store
  213. * @param len Length of data
  214. */
  215. ZT_ALWAYS_INLINE void stateObjectPut(void *const tPtr,ZT_StateObjectType type,const uint64_t id[2],const void *const data,const unsigned int len)
  216. {
  217. if (_cb.statePutFunction)
  218. _cb.statePutFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,type,id,data,(int)len);
  219. }
  220. /**
  221. * Delete a state object
  222. *
  223. * @param tPtr Thread pointer
  224. * @param type Object type to delete
  225. * @param id Object ID
  226. */
  227. ZT_ALWAYS_INLINE void stateObjectDelete(void *const tPtr,ZT_StateObjectType type,const uint64_t id[2])
  228. {
  229. if (_cb.statePutFunction)
  230. _cb.statePutFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,type,id,(const void *)0,-1);
  231. }
  232. /**
  233. * Check whether a path should be used for ZeroTier traffic
  234. *
  235. * This performs internal checks and also calls out to an external callback if one is defined.
  236. *
  237. * @param tPtr Thread pointer
  238. * @param id Identity of peer
  239. * @param localSocket Local socket or -1 if unknown
  240. * @param remoteAddress Remote address
  241. * @return True if path should be used
  242. */
  243. bool shouldUsePathForZeroTierTraffic(void *tPtr,const Identity &id,const int64_t localSocket,const InetAddress &remoteAddress);
  244. /**
  245. * Query callback for a physical address for a peer
  246. *
  247. * @param tPtr Thread pointer
  248. * @param id Full identity of ZeroTier node
  249. * @param family Desired address family or -1 for any
  250. * @param addr Buffer to store address (result paramter)
  251. * @return True if addr was filled with something
  252. */
  253. bool externalPathLookup(void *tPtr,const Identity &id,int family,InetAddress &addr);
  254. /**
  255. * Set physical path configuration
  256. *
  257. * @param pathNetwork Physical path network/netmask bits (CIDR notation)
  258. * @param pathConfig Path configuration
  259. * @return Return to pass through to external API
  260. */
  261. ZT_ResultCode setPhysicalPathConfiguration(const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig);
  262. /**
  263. * @return This node's identity
  264. */
  265. ZT_ALWAYS_INLINE const Identity &identity() const { return _RR.identity; }
  266. /**
  267. * Register that we are expecting a reply to a packet ID
  268. *
  269. * This only uses the most significant bits of the packet ID, both to save space
  270. * and to avoid using the higher bits that can be modified during armor() to
  271. * mask against the packet send counter used for QoS detection.
  272. *
  273. * @param packetId Packet ID to expect reply to
  274. */
  275. ZT_ALWAYS_INLINE void expectReplyTo(const uint64_t packetId)
  276. {
  277. const unsigned long pid2 = (unsigned long)(packetId >> 32U);
  278. const unsigned long bucket = (unsigned long)(pid2 & ZT_EXPECTING_REPLIES_BUCKET_MASK1);
  279. _expectingRepliesTo[bucket][_expectingRepliesToBucketPtr[bucket]++ & ZT_EXPECTING_REPLIES_BUCKET_MASK2] = (uint32_t)pid2;
  280. }
  281. /**
  282. * Check whether a given packet ID is something we are expecting a reply to
  283. *
  284. * This only uses the most significant bits of the packet ID, both to save space
  285. * and to avoid using the higher bits that can be modified during armor() to
  286. * mask against the packet send counter used for QoS detection.
  287. *
  288. * @param packetId Packet ID to check
  289. * @return True if we're expecting a reply
  290. */
  291. ZT_ALWAYS_INLINE bool expectingReplyTo(const uint64_t packetId) const
  292. {
  293. const uint32_t pid2 = (uint32_t)(packetId >> 32);
  294. const unsigned long bucket = (unsigned long)(pid2 & ZT_EXPECTING_REPLIES_BUCKET_MASK1);
  295. for(unsigned long i=0;i<=ZT_EXPECTING_REPLIES_BUCKET_MASK2;++i) {
  296. if (_expectingRepliesTo[bucket][i] == pid2)
  297. return true;
  298. }
  299. return false;
  300. }
  301. /**
  302. * Check whether we should do potentially expensive identity verification (rate limit)
  303. *
  304. * @param now Current time
  305. * @param from Source address of packet
  306. * @return True if within rate limits
  307. */
  308. ZT_ALWAYS_INLINE bool rateGateIdentityVerification(const int64_t now,const InetAddress &from)
  309. {
  310. unsigned long iph = from.rateGateHash();
  311. if ((now - _lastIdentityVerification[iph]) >= ZT_IDENTITY_VALIDATION_SOURCE_RATE_LIMIT) {
  312. _lastIdentityVerification[iph] = now;
  313. return true;
  314. }
  315. return false;
  316. }
  317. /**
  318. * Check whether a local controller has authorized a member on a network
  319. *
  320. * This is used by controllers to avoid needless certificate checks when we already
  321. * know if this has occurred. It's a bit of a hack but saves a massive amount of
  322. * controller CPU. It's easiest to put this here, and it imposes no overhead on
  323. * non-controllers.
  324. *
  325. * @param now Current time
  326. * @param nwid Network ID
  327. * @param addr Member address to check
  328. * @return True if member has been authorized
  329. */
  330. bool localControllerHasAuthorized(int64_t now,uint64_t nwid,const Address &addr) const;
  331. // Implementation of NetworkController::Sender interface
  332. virtual void ncSendConfig(uint64_t nwid,uint64_t requestPacketId,const Address &destination,const NetworkConfig &nc,bool sendLegacyFormatConfig);
  333. virtual void ncSendRevocation(const Address &destination,const Revocation &rev);
  334. virtual void ncSendError(uint64_t nwid,uint64_t requestPacketId,const Address &destination,NetworkController::ErrorCode errorCode);
  335. private:
  336. RuntimeEnvironment _RR;
  337. RuntimeEnvironment *RR;
  338. ZT_Node_Callbacks _cb;
  339. void *_uPtr; // _uptr (lower case) is reserved in Visual Studio :P
  340. // For tracking packet IDs to filter out OK/ERROR replies to packets we did not send
  341. volatile uint8_t _expectingRepliesToBucketPtr[ZT_EXPECTING_REPLIES_BUCKET_MASK1 + 1];
  342. volatile uint32_t _expectingRepliesTo[ZT_EXPECTING_REPLIES_BUCKET_MASK1 + 1][ZT_EXPECTING_REPLIES_BUCKET_MASK2 + 1];
  343. // Time of last identity verification indexed by InetAddress.rateGateHash() -- used in IncomingPacket::_doHELLO() via rateGateIdentityVerification()
  344. volatile int64_t _lastIdentityVerification[16384];
  345. /* Map that remembers if we have recently sent a network config to someone
  346. * querying us as a controller. This is an optimization to allow network
  347. * controllers to know whether to treat things like multicast queries the
  348. * way authorized members would be treated without requiring an extra cert
  349. * validation. */
  350. struct _LocalControllerAuth
  351. {
  352. uint64_t nwid,address;
  353. ZT_ALWAYS_INLINE _LocalControllerAuth(const uint64_t nwid_,const Address &address_) : nwid(nwid_),address(address_.toInt()) {}
  354. ZT_ALWAYS_INLINE unsigned long hashCode() const { return (unsigned long)(nwid ^ address); }
  355. ZT_ALWAYS_INLINE bool operator==(const _LocalControllerAuth &a) const { return ((a.nwid == nwid)&&(a.address == address)); }
  356. ZT_ALWAYS_INLINE bool operator!=(const _LocalControllerAuth &a) const { return ((a.nwid != nwid)||(a.address != address)); }
  357. };
  358. Hashtable< _LocalControllerAuth,int64_t > _localControllerAuthorizations;
  359. // Networks are stored in a flat hash table that is resized on any network ID collision. This makes
  360. // network lookup by network ID a few bitwise ops and an array index.
  361. std::vector< SharedPtr<Network> > _networks;
  362. uint64_t _networksMask;
  363. std::vector< ZT_InterfaceAddress > _localInterfaceAddresses;
  364. Mutex _localControllerAuthorizations_m;
  365. RWMutex _networks_m;
  366. Mutex _localInterfaceAddresses_m;
  367. Mutex _backgroundTasksLock;
  368. volatile int64_t _now;
  369. volatile int64_t _lastPing;
  370. volatile int64_t _lastHousekeepingRun;
  371. volatile int64_t _lastNetworkHousekeepingRun;
  372. volatile int64_t _lastPathKeepaliveCheck;
  373. volatile bool _online;
  374. };
  375. } // namespace ZeroTier
  376. #endif