Membership.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. public:
  44. enum AddCredentialResult
  45. {
  46. ADD_REJECTED,
  47. ADD_ACCEPTED_NEW,
  48. ADD_ACCEPTED_REDUNDANT,
  49. ADD_DEFERRED_FOR_WHOIS
  50. };
  51. Membership();
  52. /**
  53. * Send COM and other credentials to this peer if needed
  54. *
  55. * This checks last pushed times for our COM and for other credentials and
  56. * sends VERB_NETWORK_CREDENTIALS if the recipient might need them.
  57. *
  58. * @param RR Runtime environment
  59. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  60. * @param now Current time
  61. * @param peerAddress Address of member peer (the one that this Membership describes)
  62. * @param nconf My network config
  63. * @param localCapabilityIndex Index of local capability to include (in nconf.capabilities[]) or -1 if none
  64. * @param force If true, send objects regardless of last push time
  65. */
  66. void pushCredentials(const RuntimeEnvironment *RR,void *tPtr,const uint64_t now,const Address &peerAddress,const NetworkConfig &nconf,int localCapabilityIndex,const bool force);
  67. /**
  68. * Check whether we should push MULTICAST_LIKEs to this peer, and update last sent time if true
  69. *
  70. * @param now Current time
  71. * @return True if we should update multicasts
  72. */
  73. inline bool multicastLikeGate(const uint64_t now)
  74. {
  75. if ((now - _lastUpdatedMulticast) >= ZT_MULTICAST_ANNOUNCE_PERIOD) {
  76. _lastUpdatedMulticast = now;
  77. return true;
  78. }
  79. return false;
  80. }
  81. /**
  82. * Check whether the peer represented by this Membership should be allowed on this network at all
  83. *
  84. * @param nconf Our network config
  85. * @return True if this peer is allowed on this network at all
  86. */
  87. inline bool isAllowedOnNetwork(const NetworkConfig &nconf) const
  88. {
  89. if (nconf.isPublic()) return true;
  90. if (_com.timestamp() <= _comRevocationThreshold) return false;
  91. return nconf.com.agreesWith(_com);
  92. }
  93. /**
  94. * Check whether the peer represented by this Membership owns a given resource
  95. *
  96. * @tparam Type of resource: InetAddress or MAC
  97. * @param nconf Our network config
  98. * @param r Resource to check
  99. * @return True if this peer has a certificate of ownership for the given resource
  100. */
  101. template<typename T>
  102. inline bool hasCertificateOfOwnershipFor(const NetworkConfig &nconf,const T &r) const
  103. {
  104. uint32_t *k = (uint32_t *)0;
  105. CertificateOfOwnership *v = (CertificateOfOwnership *)0;
  106. Hashtable< uint32_t,CertificateOfOwnership >::Iterator i(*(const_cast< Hashtable< uint32_t,CertificateOfOwnership> *>(&_remoteCoos)));
  107. while (i.next(k,v)) {
  108. if (_isCredentialTimestampValid(nconf,*v)&&(v->owns(r)))
  109. return true;
  110. }
  111. return false;
  112. }
  113. /**
  114. * Get a remote member's tag (if we have it)
  115. *
  116. * @param nconf Network configuration
  117. * @param id Tag ID
  118. * @return Pointer to tag or NULL if not found
  119. */
  120. inline const Tag *getTag(const NetworkConfig &nconf,const uint32_t id) const
  121. {
  122. const Tag *const t = _remoteTags.get(id);
  123. return (((t)&&(_isCredentialTimestampValid(nconf,*t))) ? t : (Tag *)0);
  124. }
  125. /**
  126. * Validate and add a credential if signature is okay and it's otherwise good
  127. */
  128. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfMembership &com);
  129. /**
  130. * Validate and add a credential if signature is okay and it's otherwise good
  131. */
  132. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Tag &tag);
  133. /**
  134. * Validate and add a credential if signature is okay and it's otherwise good
  135. */
  136. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Capability &cap);
  137. /**
  138. * Validate and add a credential if signature is okay and it's otherwise good
  139. */
  140. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfOwnership &coo);
  141. /**
  142. * Validate and add a credential if signature is okay and it's otherwise good
  143. */
  144. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Revocation &rev);
  145. /**
  146. * Clean internal databases of stale entries
  147. *
  148. * @param now Current time
  149. * @param nconf Current network configuration
  150. */
  151. void clean(const uint64_t now,const NetworkConfig &nconf);
  152. /**
  153. * Reset last pushed time for local credentials
  154. *
  155. * This is done when we update our network configuration and our credentials have changed
  156. */
  157. inline void resetPushState()
  158. {
  159. _lastPushedCom = 0;
  160. memset(&_localCredLastPushed,0,sizeof(_localCredLastPushed));
  161. }
  162. /**
  163. * Generates a key for the internal use in indexing credentials by type and credential ID
  164. */
  165. static uint64_t credentialKey(const Credential::Type &t,const uint32_t i) { return (((uint64_t)t << 32) | (uint64_t)i); }
  166. private:
  167. template<typename C>
  168. inline bool _isCredentialTimestampValid(const NetworkConfig &nconf,const C &remoteCredential) const
  169. {
  170. const uint64_t ts = remoteCredential.timestamp();
  171. if (((ts >= nconf.timestamp) ? (ts - nconf.timestamp) : (nconf.timestamp - ts)) <= nconf.credentialTimeMaxDelta) {
  172. const uint64_t *threshold = _revocations.get(credentialKey(C::credentialType(),remoteCredential.id()));
  173. return ((!threshold)||(ts > *threshold));
  174. }
  175. return false;
  176. }
  177. template<typename C>
  178. void _cleanCredImpl(const NetworkConfig &nconf,Hashtable<uint32_t,C> &remoteCreds)
  179. {
  180. uint32_t *k = (uint32_t *)0;
  181. C *v = (C *)0;
  182. typename Hashtable<uint32_t,C>::Iterator i(remoteCreds);
  183. while (i.next(k,v)) {
  184. if (!_isCredentialTimestampValid(nconf,*v))
  185. remoteCreds.erase(*k);
  186. }
  187. }
  188. // Last time we pushed MULTICAST_LIKE(s)
  189. uint64_t _lastUpdatedMulticast;
  190. // Last time we pushed our COM to this peer
  191. uint64_t _lastPushedCom;
  192. // Revocation threshold for COM or 0 if none
  193. uint64_t _comRevocationThreshold;
  194. // Remote member's latest network COM
  195. CertificateOfMembership _com;
  196. // Revocations by credentialKey()
  197. Hashtable< uint64_t,uint64_t > _revocations;
  198. // Remote credentials that we have received from this member (and that are valid)
  199. Hashtable< uint32_t,Tag > _remoteTags;
  200. Hashtable< uint32_t,Capability > _remoteCaps;
  201. Hashtable< uint32_t,CertificateOfOwnership > _remoteCoos;
  202. // Time we last pushed our local credentials to this member
  203. struct {
  204. uint64_t tag[ZT_MAX_NETWORK_TAGS];
  205. uint64_t cap[ZT_MAX_NETWORK_CAPABILITIES];
  206. uint64_t coo[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  207. } _localCredLastPushed;
  208. public:
  209. class CapabilityIterator
  210. {
  211. public:
  212. CapabilityIterator(Membership &m,const NetworkConfig &nconf) :
  213. _hti(m._remoteCaps),
  214. _k((uint32_t *)0),
  215. _c((Capability *)0),
  216. _nconf(nconf)
  217. {
  218. }
  219. inline Capability *next()
  220. {
  221. if (_hti.next(_k,_c))
  222. return _c;
  223. else return (Capability *)0;
  224. }
  225. private:
  226. Hashtable< uint32_t,Capability >::Iterator _hti;
  227. uint32_t *_k;
  228. Capability *_c;
  229. const NetworkConfig &_nconf;
  230. };
  231. };
  232. } // namespace ZeroTier
  233. #endif