Membership.hpp 8.8 KB

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