Membership.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_MEMBERSHIP_HPP
  14. #define ZT_MEMBERSHIP_HPP
  15. #include <cstdint>
  16. #include "Constants.hpp"
  17. #include "Credential.hpp"
  18. #include "Hashtable.hpp"
  19. #include "CertificateOfMembership.hpp"
  20. #include "Capability.hpp"
  21. #include "Tag.hpp"
  22. #include "Revocation.hpp"
  23. #include "NetworkConfig.hpp"
  24. namespace ZeroTier {
  25. class RuntimeEnvironment;
  26. class Network;
  27. /**
  28. * A container for certificates of membership and other network credentials
  29. *
  30. * This is essentially a relational join between Peer and Network.
  31. *
  32. * This class is not thread safe. It must be locked externally.
  33. */
  34. class Membership
  35. {
  36. public:
  37. enum AddCredentialResult
  38. {
  39. ADD_REJECTED,
  40. ADD_ACCEPTED_NEW,
  41. ADD_ACCEPTED_REDUNDANT,
  42. ADD_DEFERRED_FOR_WHOIS
  43. };
  44. Membership();
  45. /**
  46. * Send COM and other credentials to this peer
  47. *
  48. * @param RR Runtime environment
  49. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  50. * @param now Current time
  51. * @param peerAddress Address of member peer (the one that this Membership describes)
  52. * @param nconf My network config
  53. */
  54. void pushCredentials(const RuntimeEnvironment *RR,void *tPtr,int64_t now,const Address &peerAddress,const NetworkConfig &nconf);
  55. /**
  56. * @return Time we last pushed credentials to this member
  57. */
  58. ZT_ALWAYS_INLINE int64_t lastPushedCredentials() const { return _lastPushedCredentials; }
  59. /**
  60. * Check whether the peer represented by this Membership should be allowed on this network at all
  61. *
  62. * @param nconf Our network config
  63. * @return True if this peer is allowed on this network at all
  64. */
  65. ZT_ALWAYS_INLINE bool isAllowedOnNetwork(const NetworkConfig &nconf) const
  66. {
  67. if (nconf.isPublic()) return true; // public network
  68. if (_com.timestamp() <= _comRevocationThreshold) return false; // COM has been revoked
  69. return nconf.com.agreesWith(_com); // check timestamp agreement window
  70. }
  71. /**
  72. * Check whether the peer represented by this Membership owns a given address
  73. *
  74. * @tparam Type of resource: InetAddress or MAC
  75. * @param nconf Our network config
  76. * @param r Resource to check
  77. * @return True if this peer has a certificate of ownership for the given resource
  78. */
  79. template<typename T>
  80. ZT_ALWAYS_INLINE bool peerOwnsAddress(const NetworkConfig &nconf,const T &r) const
  81. {
  82. if (_isUnspoofableAddress(nconf,r))
  83. return true;
  84. uint32_t *k = nullptr;
  85. CertificateOfOwnership *v = nullptr;
  86. Hashtable< uint32_t,CertificateOfOwnership >::Iterator i(*(const_cast< Hashtable< uint32_t,CertificateOfOwnership> *>(&_remoteCoos)));
  87. while (i.next(k,v)) {
  88. if (_isCredentialTimestampValid(nconf,*v)&&(v->owns(r)))
  89. return true;
  90. }
  91. return false;
  92. }
  93. /**
  94. * Get a remote member's tag (if we have it)
  95. *
  96. * @param nconf Network configuration
  97. * @param id Tag ID
  98. * @return Pointer to tag or NULL if not found
  99. */
  100. ZT_ALWAYS_INLINE const Tag *getTag(const NetworkConfig &nconf,const uint32_t id) const
  101. {
  102. const Tag *const t = _remoteTags.get(id);
  103. return (((t)&&(_isCredentialTimestampValid(nconf,*t))) ? t : (Tag *)0);
  104. }
  105. /**
  106. * Clean internal databases of stale entries
  107. *
  108. * @param now Current time
  109. * @param nconf Current network configuration
  110. */
  111. void clean(int64_t now,const NetworkConfig &nconf);
  112. /**
  113. * Generates a key for internal use in indexing credentials by type and credential ID
  114. */
  115. static ZT_ALWAYS_INLINE uint64_t credentialKey(const ZT_CredentialType &t,const uint32_t i) { return (((uint64_t)t << 32U) | (uint64_t)i); }
  116. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const CertificateOfMembership &com);
  117. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const Tag &tag);
  118. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const Capability &cap);
  119. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const CertificateOfOwnership &coo);
  120. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const Revocation &rev);
  121. private:
  122. // This returns true if a resource is an IPv6 NDP-emulated address. These embed the ZT
  123. // address of the peer and therefore cannot be spoofed, causing peerOwnsAddress() to
  124. // always return true for them. A certificate is not required for these.
  125. ZT_ALWAYS_INLINE bool _isUnspoofableAddress(const NetworkConfig &nconf,const MAC &m) const { return false; }
  126. bool _isUnspoofableAddress(const NetworkConfig &nconf,const InetAddress &ip) const;
  127. // This compares the remote credential's timestamp to the timestamp in our network config
  128. // plus or minus the permitted maximum timestamp delta.
  129. template<typename C>
  130. ZT_ALWAYS_INLINE bool _isCredentialTimestampValid(const NetworkConfig &nconf,const C &remoteCredential) const
  131. {
  132. const int64_t ts = remoteCredential.timestamp();
  133. if (((ts >= nconf.timestamp) ? (ts - nconf.timestamp) : (nconf.timestamp - ts)) <= nconf.credentialTimeMaxDelta) {
  134. const int64_t *threshold = _revocations.get(credentialKey(C::credentialType(),remoteCredential.id()));
  135. return ((!threshold)||(ts > *threshold));
  136. }
  137. return false;
  138. }
  139. template<typename C>
  140. ZT_ALWAYS_INLINE void _cleanCredImpl(const NetworkConfig &nconf,Hashtable<uint32_t,C> &remoteCreds)
  141. {
  142. uint32_t *k = nullptr;
  143. C *v = nullptr;
  144. typename Hashtable<uint32_t,C>::Iterator i(remoteCreds);
  145. while (i.next(k,v)) {
  146. if (!_isCredentialTimestampValid(nconf,*v))
  147. remoteCreds.erase(*k);
  148. }
  149. }
  150. // Last time we pushed MULTICAST_LIKE(s)
  151. int64_t _lastUpdatedMulticast;
  152. // Revocation threshold for COM or 0 if none
  153. int64_t _comRevocationThreshold;
  154. // Time we last pushed credentials
  155. int64_t _lastPushedCredentials;
  156. // Remote member's latest network COM
  157. CertificateOfMembership _com;
  158. // Revocations by credentialKey()
  159. Hashtable< uint64_t,int64_t > _revocations;
  160. // Remote credentials that we have received from this member (and that are valid)
  161. Hashtable< uint32_t,Tag > _remoteTags;
  162. Hashtable< uint32_t,Capability > _remoteCaps;
  163. Hashtable< uint32_t,CertificateOfOwnership > _remoteCoos;
  164. public:
  165. class CapabilityIterator
  166. {
  167. public:
  168. ZT_ALWAYS_INLINE CapabilityIterator(Membership &m,const NetworkConfig &nconf) :
  169. _hti(m._remoteCaps),
  170. _k(nullptr),
  171. _c(nullptr),
  172. _m(m),
  173. _nconf(nconf)
  174. {
  175. }
  176. ZT_ALWAYS_INLINE Capability *next()
  177. {
  178. while (_hti.next(_k,_c)) {
  179. if (_m._isCredentialTimestampValid(_nconf,*_c))
  180. return _c;
  181. }
  182. return nullptr;
  183. }
  184. private:
  185. Hashtable< uint32_t,Capability >::Iterator _hti;
  186. uint32_t *_k;
  187. Capability *_c;
  188. Membership &_m;
  189. const NetworkConfig &_nconf;
  190. };
  191. };
  192. } // namespace ZeroTier
  193. #endif