Membership.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef ZT_MEMBERSHIP_HPP
  19. #define ZT_MEMBERSHIP_HPP
  20. #include <stdint.h>
  21. #include "Constants.hpp"
  22. #include "../include/ZeroTierOne.h"
  23. #include "Credential.hpp"
  24. #include "Hashtable.hpp"
  25. #include "CertificateOfMembership.hpp"
  26. #include "Capability.hpp"
  27. #include "Tag.hpp"
  28. #include "Revocation.hpp"
  29. #include "NetworkConfig.hpp"
  30. #define ZT_MEMBERSHIP_CRED_ID_UNUSED 0xffffffffffffffffULL
  31. namespace ZeroTier {
  32. class RuntimeEnvironment;
  33. class Network;
  34. /**
  35. * A container for certificates of membership and other network credentials
  36. *
  37. * This is essentially a relational join between Peer and Network.
  38. *
  39. * This class is not thread safe. It must be locked externally.
  40. */
  41. class Membership
  42. {
  43. private:
  44. template<typename T>
  45. struct _RemoteCredential
  46. {
  47. _RemoteCredential() : lastReceived(0),revocationThreshold(0),credential() {}
  48. uint64_t lastReceived; // last time we got this credential
  49. uint64_t revocationThreshold; // credentials before this time are invalid
  50. T credential;
  51. };
  52. struct _LocalCredentialPushState
  53. {
  54. _LocalCredentialPushState() : lastPushed(0),id(0) {}
  55. uint64_t lastPushed; // last time we sent our own copy of this credential
  56. uint32_t id;
  57. };
  58. public:
  59. enum AddCredentialResult
  60. {
  61. ADD_REJECTED,
  62. ADD_ACCEPTED_NEW,
  63. ADD_ACCEPTED_REDUNDANT,
  64. ADD_DEFERRED_FOR_WHOIS
  65. };
  66. Membership();
  67. /**
  68. * Send COM and other credentials to this peer if needed
  69. *
  70. * This checks last pushed times for our COM and for other credentials and
  71. * sends VERB_NETWORK_CREDENTIALS if the recipient might need them.
  72. *
  73. * @param RR Runtime environment
  74. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  75. * @param now Current time
  76. * @param peerAddress Address of member peer (the one that this Membership describes)
  77. * @param nconf My network config
  78. * @param localCapabilityIndex Index of local capability to include (in nconf.capabilities[]) or -1 if none
  79. * @param force If true, send objects regardless of last push time
  80. */
  81. void pushCredentials(const RuntimeEnvironment *RR,void *tPtr,const uint64_t now,const Address &peerAddress,const NetworkConfig &nconf,int localCapabilityIndex,const bool force);
  82. /**
  83. * Check whether we should push MULTICAST_LIKEs to this peer
  84. *
  85. * @param now Current time
  86. * @return True if we should update multicasts
  87. */
  88. inline bool shouldLikeMulticasts(const uint64_t now) const { return ((now - _lastUpdatedMulticast) >= ZT_MULTICAST_ANNOUNCE_PERIOD); }
  89. /**
  90. * Set time we last updated multicasts for this peer
  91. *
  92. * @param now Current time
  93. */
  94. inline void likingMulticasts(const uint64_t now) { _lastUpdatedMulticast = now; }
  95. /**
  96. * Check whether the peer represented by this Membership should be allowed on this network at all
  97. *
  98. * @param nconf Our network config
  99. * @return True if this peer is allowed on this network at all
  100. */
  101. inline bool isAllowedOnNetwork(const NetworkConfig &nconf) const
  102. {
  103. if (nconf.isPublic()) return true;
  104. if (_com.timestamp() <= _comRevocationThreshold) return false;
  105. return nconf.com.agreesWith(_com);
  106. }
  107. /**
  108. * Check whether the peer represented by this Membership owns a given resource
  109. *
  110. * @tparam Type of resource: InetAddress or MAC
  111. * @param nconf Our network config
  112. * @param r Resource to check
  113. * @return True if this peer has a certificate of ownership for the given resource
  114. */
  115. template<typename T>
  116. inline bool hasCertificateOfOwnershipFor(const NetworkConfig &nconf,const T &r)
  117. {
  118. uint32_t *k = (uint32_t *)0;
  119. CertificateOfOwnership *v = (CertificateOfOwnership *)0;
  120. Hashtable< uint32_t,CertificateOfOwnership >::Iterator i(_remoteCoos);
  121. while (i.next(k,v)) {
  122. if (_isCredentialTimestampValid(nconf,*v)&&(v->owns(r)))
  123. return true;
  124. }
  125. return false;
  126. }
  127. /**
  128. * Get a remote member's tag (if we have it)
  129. *
  130. * @param nconf Network configuration
  131. * @param id Tag ID
  132. * @return Pointer to tag or NULL if not found
  133. */
  134. inline const Tag *getTag(const NetworkConfig &nconf,const uint32_t id) const
  135. {
  136. const Tag *const t = _remoteTags.get(id);
  137. return (((t)&&(_isCredentialTimestampValid(nconf,*t))) ? t : (Tag *)0);
  138. }
  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 CertificateOfMembership &com);
  143. /**
  144. * Validate and add a credential if signature is okay and it's otherwise good
  145. */
  146. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Tag &tag);
  147. /**
  148. * Validate and add a credential if signature is okay and it's otherwise good
  149. */
  150. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Capability &cap);
  151. /**
  152. * Validate and add a credential if signature is okay and it's otherwise good
  153. */
  154. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfOwnership &coo);
  155. /**
  156. * Validate and add a credential if signature is okay and it's otherwise good
  157. */
  158. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Revocation &rev);
  159. /**
  160. * Generates a key for the internal revocation tracking hash table
  161. *
  162. * @param t Credential type
  163. * @param i Credential ID
  164. * @return Key for tracking revocations of this credential
  165. */
  166. static uint64_t revocationKey(const Credential::Type &t,const uint32_t i) { return (((uint64_t)t << 32) | (uint64_t)i); }
  167. private:
  168. template<typename C>
  169. inline bool _isCredentialTimestampValid(const NetworkConfig &nconf,const C &remoteCredential) const
  170. {
  171. const uint64_t ts = remoteCredential.timestamp();
  172. if (((ts >= nconf.timestamp) ? (ts - nconf.timestamp) : (nconf.timestamp - ts)) <= nconf.credentialTimeMaxDelta) {
  173. const uint64_t *threshold = _revocations.get(revocationKey(C::credentialType(),remoteCredential.id()));
  174. return ((!threshold)||(ts > *threshold));
  175. }
  176. return false;
  177. }
  178. // Last time we pushed MULTICAST_LIKE(s)
  179. uint64_t _lastUpdatedMulticast;
  180. // Last time we pushed our COM to this peer
  181. uint64_t _lastPushedCom;
  182. // Revocation threshold for COM or 0 if none
  183. uint64_t _comRevocationThreshold;
  184. // Remote member's latest network COM
  185. CertificateOfMembership _com;
  186. // Revocations
  187. Hashtable< uint64_t,uint64_t > _revocations;
  188. // Remote credentials and credential state
  189. Hashtable< uint32_t,Tag > _remoteTags;
  190. Hashtable< uint32_t,Capability > _remoteCaps;
  191. Hashtable< uint32_t,CertificateOfOwnership > _remoteCoos;
  192. // Local credential push state tracking
  193. _LocalCredentialPushState _localTags[ZT_MAX_NETWORK_TAGS];
  194. _LocalCredentialPushState _localCaps[ZT_MAX_NETWORK_CAPABILITIES];
  195. _LocalCredentialPushState _localCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  196. };
  197. } // namespace ZeroTier
  198. #endif