Membership.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c)2019 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: 2026-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_MEMBERSHIP_HPP
  14. #define ZT_MEMBERSHIP_HPP
  15. #include "../include/ZeroTierOne.h"
  16. #include "Capability.hpp"
  17. #include "CertificateOfMembership.hpp"
  18. #include "Constants.hpp"
  19. #include "Credential.hpp"
  20. #include "Hashtable.hpp"
  21. #include "NetworkConfig.hpp"
  22. #include "Revocation.hpp"
  23. #include "Tag.hpp"
  24. #include <stdint.h>
  25. #define ZT_MEMBERSHIP_CRED_ID_UNUSED 0xffffffffffffffffULL
  26. namespace ZeroTier {
  27. class RuntimeEnvironment;
  28. class Network;
  29. /**
  30. * A container for certificates of membership and other network credentials
  31. *
  32. * This is essentially a relational join between Peer and Network.
  33. *
  34. * This class is not thread safe. It must be locked externally.
  35. */
  36. class Membership {
  37. public:
  38. enum AddCredentialResult { ADD_REJECTED, ADD_ACCEPTED_NEW, ADD_ACCEPTED_REDUNDANT, ADD_DEFERRED_FOR_WHOIS };
  39. Membership();
  40. /**
  41. * Send COM and other credentials to this peer
  42. *
  43. * @param RR Runtime environment
  44. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  45. * @param now Current time
  46. * @param peerAddress Address of member peer (the one that this Membership describes)
  47. * @param nconf My network config
  48. */
  49. void pushCredentials(const RuntimeEnvironment* RR, void* tPtr, const int64_t now, const Address& peerAddress, const NetworkConfig& nconf);
  50. inline int64_t lastPushedCredentials()
  51. {
  52. return _lastPushedCredentials;
  53. }
  54. inline int64_t comTimestamp()
  55. {
  56. return _com.timestamp();
  57. }
  58. inline int64_t comRevocationThreshold()
  59. {
  60. return _comRevocationThreshold;
  61. }
  62. /**
  63. * Check whether we should push MULTICAST_LIKEs to this peer, and update last sent time if true
  64. *
  65. * @param now Current time
  66. * @return True if we should update multicasts
  67. */
  68. inline bool multicastLikeGate(const int64_t now)
  69. {
  70. if ((now - _lastUpdatedMulticast) >= ZT_MULTICAST_ANNOUNCE_PERIOD) {
  71. _lastUpdatedMulticast = now;
  72. return true;
  73. }
  74. return false;
  75. }
  76. /**
  77. * Check whether the peer represented by this Membership should be allowed on this network at all
  78. *
  79. * @param nconf Our network config
  80. * @param otherNodeIdentity Identity of remote node
  81. * @return True if this peer is allowed on this network at all
  82. */
  83. inline bool isAllowedOnNetwork(const NetworkConfig& thisNodeNetworkConfig, const Identity& otherNodeIdentity) const
  84. {
  85. return thisNodeNetworkConfig.isPublic() || (((_com.timestamp() > _comRevocationThreshold) && (thisNodeNetworkConfig.com.agreesWith(_com, otherNodeIdentity))));
  86. }
  87. inline bool recentlyAssociated(const int64_t now) const
  88. {
  89. return ((_com) && ((now - _com.timestamp()) < ZT_PEER_ACTIVITY_TIMEOUT));
  90. }
  91. /**
  92. * Check whether the peer represented by this Membership owns a given resource
  93. *
  94. * @tparam Type of resource: InetAddress or MAC
  95. * @param nconf Our network config
  96. * @param r Resource to check
  97. * @return True if this peer has a certificate of ownership for the given resource
  98. */
  99. template <typename T> inline bool hasCertificateOfOwnershipFor(const NetworkConfig& nconf, const T& r) const
  100. {
  101. uint32_t* k = (uint32_t*)0;
  102. CertificateOfOwnership* v = (CertificateOfOwnership*)0;
  103. Hashtable<uint32_t, CertificateOfOwnership>::Iterator i(*(const_cast<Hashtable<uint32_t, CertificateOfOwnership>*>(&_remoteCoos)));
  104. while (i.next(k, v)) {
  105. if (_isCredentialTimestampValid(nconf, *v) && (v->owns(r))) {
  106. return true;
  107. }
  108. }
  109. return _isV6NDPEmulated(nconf, r);
  110. }
  111. /**
  112. * Get a remote member's tag (if we have it)
  113. *
  114. * @param nconf Network configuration
  115. * @param id Tag ID
  116. * @return Pointer to tag or NULL if not found
  117. */
  118. inline const Tag* getTag(const NetworkConfig& nconf, const uint32_t id) const
  119. {
  120. const Tag* const t = _remoteTags.get(id);
  121. return (((t) && (_isCredentialTimestampValid(nconf, *t))) ? t : (Tag*)0);
  122. }
  123. /**
  124. * Validate and add a credential if signature is okay and it's otherwise good
  125. */
  126. AddCredentialResult addCredential(const RuntimeEnvironment* RR, void* tPtr, const NetworkConfig& nconf, const CertificateOfMembership& com);
  127. /**
  128. * Validate and add a credential if signature is okay and it's otherwise good
  129. */
  130. AddCredentialResult addCredential(const RuntimeEnvironment* RR, void* tPtr, const NetworkConfig& nconf, const Tag& tag);
  131. /**
  132. * Validate and add a credential if signature is okay and it's otherwise good
  133. */
  134. AddCredentialResult addCredential(const RuntimeEnvironment* RR, void* tPtr, const NetworkConfig& nconf, const Capability& cap);
  135. /**
  136. * Validate and add a credential if signature is okay and it's otherwise good
  137. */
  138. AddCredentialResult addCredential(const RuntimeEnvironment* RR, void* tPtr, const NetworkConfig& nconf, const CertificateOfOwnership& coo);
  139. /**
  140. * Validate and add a credential if signature is okay and it's otherwise good
  141. */
  142. AddCredentialResult addCredential(const RuntimeEnvironment* RR, void* tPtr, const NetworkConfig& nconf, const Revocation& rev);
  143. /**
  144. * Clean internal databases of stale entries
  145. *
  146. * @param now Current time
  147. * @param nconf Current network configuration
  148. */
  149. void clean(const int64_t now, const NetworkConfig& nconf);
  150. /**
  151. * Generates a key for the internal use in indexing credentials by type and credential ID
  152. */
  153. static uint64_t credentialKey(const Credential::Type& t, const uint32_t i)
  154. {
  155. return (((uint64_t)t << 32) | (uint64_t)i);
  156. }
  157. private:
  158. inline bool _isV6NDPEmulated(const NetworkConfig& nconf, const MAC& m) const
  159. {
  160. return false;
  161. }
  162. inline bool _isV6NDPEmulated(const NetworkConfig& nconf, const InetAddress& ip) const
  163. {
  164. if ((ip.isV6()) && (nconf.ndpEmulation())) {
  165. const InetAddress sixpl(InetAddress::makeIpv66plane(nconf.networkId, nconf.issuedTo.toInt()));
  166. for (unsigned int i = 0; i < nconf.staticIpCount; ++i) {
  167. if (nconf.staticIps[i].ipsEqual(sixpl)) {
  168. bool prefixMatches = true;
  169. for (unsigned int j = 0; j < 5; ++j) { // check for match on /40
  170. if ((((const struct sockaddr_in6*)&ip)->sin6_addr.s6_addr)[j] != (((const struct sockaddr_in6*)&sixpl)->sin6_addr.s6_addr)[j]) {
  171. prefixMatches = false;
  172. break;
  173. }
  174. }
  175. if (prefixMatches) {
  176. return true;
  177. }
  178. break;
  179. }
  180. }
  181. const InetAddress rfc4193(InetAddress::makeIpv6rfc4193(nconf.networkId, nconf.issuedTo.toInt()));
  182. for (unsigned int i = 0; i < nconf.staticIpCount; ++i) {
  183. if (nconf.staticIps[i].ipsEqual(rfc4193)) {
  184. bool prefixMatches = true;
  185. for (unsigned int j = 0; j < 11; ++j) { // check for match on /88
  186. if ((((const struct sockaddr_in6*)&ip)->sin6_addr.s6_addr)[j] != (((const struct sockaddr_in6*)&rfc4193)->sin6_addr.s6_addr)[j]) {
  187. prefixMatches = false;
  188. break;
  189. }
  190. }
  191. if (prefixMatches) {
  192. return true;
  193. }
  194. break;
  195. }
  196. }
  197. }
  198. return false;
  199. }
  200. template <typename C> inline bool _isCredentialTimestampValid(const NetworkConfig& nconf, const C& remoteCredential) const
  201. {
  202. const int64_t ts = remoteCredential.timestamp();
  203. if (((ts >= nconf.timestamp) ? (ts - nconf.timestamp) : (nconf.timestamp - ts)) <= nconf.credentialTimeMaxDelta) {
  204. const int64_t* threshold = _revocations.get(credentialKey(C::credentialType(), remoteCredential.id()));
  205. return ((! threshold) || (ts > *threshold));
  206. }
  207. return false;
  208. }
  209. template <typename C> inline void _cleanCredImpl(const NetworkConfig& nconf, Hashtable<uint32_t, C>& remoteCreds)
  210. {
  211. uint32_t* k = (uint32_t*)0;
  212. C* v = (C*)0;
  213. typename Hashtable<uint32_t, C>::Iterator i(remoteCreds);
  214. while (i.next(k, v)) {
  215. if (! _isCredentialTimestampValid(nconf, *v)) {
  216. remoteCreds.erase(*k);
  217. }
  218. }
  219. }
  220. // Last time we pushed MULTICAST_LIKE(s)
  221. int64_t _lastUpdatedMulticast;
  222. // Revocation threshold for COM or 0 if none
  223. int64_t _comRevocationThreshold;
  224. // Time we last pushed credentials
  225. int64_t _lastPushedCredentials;
  226. // Remote member's latest network COM
  227. CertificateOfMembership _com;
  228. // Revocations by credentialKey()
  229. Hashtable<uint64_t, int64_t> _revocations;
  230. // Remote credentials that we have received from this member (and that are valid)
  231. Hashtable<uint32_t, Tag> _remoteTags;
  232. Hashtable<uint32_t, Capability> _remoteCaps;
  233. Hashtable<uint32_t, CertificateOfOwnership> _remoteCoos;
  234. public:
  235. class CapabilityIterator {
  236. public:
  237. CapabilityIterator(Membership& m, const NetworkConfig& nconf) : _hti(m._remoteCaps), _k((uint32_t*)0), _c((Capability*)0), _m(m), _nconf(nconf)
  238. {
  239. }
  240. inline Capability* next()
  241. {
  242. while (_hti.next(_k, _c)) {
  243. if (_m._isCredentialTimestampValid(_nconf, *_c)) {
  244. return _c;
  245. }
  246. }
  247. return (Capability*)0;
  248. }
  249. private:
  250. Hashtable<uint32_t, Capability>::Iterator _hti;
  251. uint32_t* _k;
  252. Capability* _c;
  253. Membership& _m;
  254. const NetworkConfig& _nconf;
  255. };
  256. };
  257. } // namespace ZeroTier
  258. #endif