Membership.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "CertificateOfMembership.hpp"
  24. #include "Capability.hpp"
  25. #include "Tag.hpp"
  26. #include "Revocation.hpp"
  27. #include "NetworkConfig.hpp"
  28. #define ZT_MEMBERSHIP_CRED_ID_UNUSED 0xffffffffffffffffULL
  29. namespace ZeroTier {
  30. class RuntimeEnvironment;
  31. class Network;
  32. /**
  33. * A container for certificates of membership and other network credentials
  34. *
  35. * This is essentially a relational join between Peer and Network.
  36. *
  37. * This class is not thread safe. It must be locked externally.
  38. */
  39. class Membership
  40. {
  41. private:
  42. template<typename T>
  43. struct _RemoteCredential
  44. {
  45. _RemoteCredential() : id(ZT_MEMBERSHIP_CRED_ID_UNUSED),lastReceived(0),revocationThreshold(0) {}
  46. uint64_t id;
  47. uint64_t lastReceived; // last time we got this credential
  48. uint64_t revocationThreshold; // credentials before this time are invalid
  49. T credential;
  50. inline bool operator<(const _RemoteCredential &c) const { return (id < c.id); }
  51. };
  52. template<typename T>
  53. struct _RemoteCredentialComp
  54. {
  55. inline bool operator()(const _RemoteCredential<T> *a,const _RemoteCredential<T> *b) const { return (a->id < b->id); }
  56. inline bool operator()(const uint64_t a,const _RemoteCredential<T> *b) const { return (a < b->id); }
  57. inline bool operator()(const _RemoteCredential<T> *a,const uint64_t b) const { return (a->id < b); }
  58. inline bool operator()(const uint64_t a,const uint64_t b) const { return (a < b); }
  59. };
  60. // Used to track push state for network config tags[] and capabilities[] entries
  61. struct _LocalCredentialPushState
  62. {
  63. _LocalCredentialPushState() : lastPushed(0),id(0) {}
  64. uint64_t lastPushed; // last time we sent our own copy of this credential
  65. uint64_t id;
  66. };
  67. public:
  68. enum AddCredentialResult
  69. {
  70. ADD_REJECTED,
  71. ADD_ACCEPTED_NEW,
  72. ADD_ACCEPTED_REDUNDANT,
  73. ADD_DEFERRED_FOR_WHOIS
  74. };
  75. /**
  76. * Iterator to scan forward through capabilities in ascending order of ID
  77. */
  78. class CapabilityIterator
  79. {
  80. public:
  81. CapabilityIterator(const Membership &m,const NetworkConfig &nconf) :
  82. _m(&m),
  83. _c(&nconf),
  84. _i(&(m._remoteCaps[0])) {}
  85. inline const Capability *next()
  86. {
  87. for(;;) {
  88. if ((_i != &(_m->_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]))&&((*_i)->id != ZT_MEMBERSHIP_CRED_ID_UNUSED)) {
  89. const Capability *tmp = &((*_i)->credential);
  90. if (_m->_isCredentialTimestampValid(*_c,*tmp,**_i)) {
  91. ++_i;
  92. return tmp;
  93. } else ++_i;
  94. } else {
  95. return (const Capability *)0;
  96. }
  97. }
  98. }
  99. private:
  100. const Membership *_m;
  101. const NetworkConfig *_c;
  102. const _RemoteCredential<Capability> *const *_i;
  103. };
  104. friend class CapabilityIterator;
  105. /**
  106. * Iterator to scan forward through tags in ascending order of ID
  107. */
  108. class TagIterator
  109. {
  110. public:
  111. TagIterator(const Membership &m,const NetworkConfig &nconf) :
  112. _m(&m),
  113. _c(&nconf),
  114. _i(&(m._remoteTags[0])) {}
  115. inline const Tag *next()
  116. {
  117. for(;;) {
  118. if ((_i != &(_m->_remoteTags[ZT_MAX_NETWORK_TAGS]))&&((*_i)->id != ZT_MEMBERSHIP_CRED_ID_UNUSED)) {
  119. const Tag *tmp = &((*_i)->credential);
  120. if (_m->_isCredentialTimestampValid(*_c,*tmp,**_i)) {
  121. ++_i;
  122. return tmp;
  123. } else ++_i;
  124. } else {
  125. return (const Tag *)0;
  126. }
  127. }
  128. }
  129. private:
  130. const Membership *_m;
  131. const NetworkConfig *_c;
  132. const _RemoteCredential<Tag> *const *_i;
  133. };
  134. friend class TagIterator;
  135. Membership();
  136. /**
  137. * Send COM and other credentials to this peer if needed
  138. *
  139. * This checks last pushed times for our COM and for other credentials and
  140. * sends VERB_NETWORK_CREDENTIALS if the recipient might need them.
  141. *
  142. * @param RR Runtime environment
  143. * @param now Current time
  144. * @param peerAddress Address of member peer (the one that this Membership describes)
  145. * @param nconf My network config
  146. * @param localCapabilityIndex Index of local capability to include (in nconf.capabilities[]) or -1 if none
  147. * @param force If true, send objects regardless of last push time
  148. */
  149. void pushCredentials(const RuntimeEnvironment *RR,const uint64_t now,const Address &peerAddress,const NetworkConfig &nconf,int localCapabilityIndex,const bool force);
  150. /**
  151. * Check whether we should push MULTICAST_LIKEs to this peer
  152. *
  153. * @param now Current time
  154. * @return True if we should update multicasts
  155. */
  156. inline bool shouldLikeMulticasts(const uint64_t now) const { return ((now - _lastUpdatedMulticast) >= ZT_MULTICAST_ANNOUNCE_PERIOD); }
  157. /**
  158. * Set time we last updated multicasts for this peer
  159. *
  160. * @param now Current time
  161. */
  162. inline void likingMulticasts(const uint64_t now) { _lastUpdatedMulticast = now; }
  163. /**
  164. * Check whether the peer represented by this Membership should be allowed on this network at all
  165. *
  166. * @param nconf Our network config
  167. * @return True if this peer is allowed on this network at all
  168. */
  169. inline bool isAllowedOnNetwork(const NetworkConfig &nconf) const
  170. {
  171. if (nconf.isPublic())
  172. return true;
  173. if ((_comRevocationThreshold)&&(_com.timestamp().first <= _comRevocationThreshold))
  174. return false;
  175. return nconf.com.agreesWith(_com);
  176. }
  177. /**
  178. * @param nconf Network configuration
  179. * @param id Capablity ID
  180. * @return Pointer to capability or NULL if not found
  181. */
  182. const Capability *getCapability(const NetworkConfig &nconf,const uint32_t id) const;
  183. /**
  184. * @param nconf Network configuration
  185. * @param id Tag ID
  186. * @return Pointer to tag or NULL if not found
  187. */
  188. const Tag *getTag(const NetworkConfig &nconf,const uint32_t id) const;
  189. /**
  190. * Validate and add a credential if signature is okay and it's otherwise good
  191. */
  192. AddCredentialResult addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const CertificateOfMembership &com);
  193. /**
  194. * Validate and add a credential if signature is okay and it's otherwise good
  195. */
  196. AddCredentialResult addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const Tag &tag);
  197. /**
  198. * Validate and add a credential if signature is okay and it's otherwise good
  199. */
  200. AddCredentialResult addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const Capability &cap);
  201. /**
  202. * Validate and add a credential if signature is okay and it's otherwise good
  203. */
  204. AddCredentialResult addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const Revocation &rev);
  205. /**
  206. * Validate and add a credential if signature is okay and it's otherwise good
  207. */
  208. AddCredentialResult addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const CertificateOfOwnership &coo);
  209. private:
  210. _RemoteCredential<Tag> *_newTag(const uint64_t id);
  211. _RemoteCredential<Capability> *_newCapability(const uint64_t id);
  212. _RemoteCredential<CertificateOfOwnership> *_newCoo(const uint64_t id);
  213. bool _revokeCom(const Revocation &rev);
  214. bool _revokeCap(const Revocation &rev,const uint64_t now);
  215. bool _revokeTag(const Revocation &rev,const uint64_t now);
  216. bool _revokeCoo(const Revocation &rev,const uint64_t now);
  217. template<typename C,typename CS>
  218. inline bool _isCredentialTimestampValid(const NetworkConfig &nconf,const C &cred,const CS &state) const
  219. {
  220. const uint64_t ts = cred.timestamp();
  221. return ( (((ts >= nconf.timestamp) ? (ts - nconf.timestamp) : (nconf.timestamp - ts)) <= nconf.credentialTimeMaxDelta) && (ts > state.revocationThreshold) );
  222. }
  223. // Last time we pushed MULTICAST_LIKE(s)
  224. uint64_t _lastUpdatedMulticast;
  225. // Last time we pushed our COM to this peer
  226. uint64_t _lastPushedCom;
  227. // Revocation threshold for COM or 0 if none
  228. uint64_t _comRevocationThreshold;
  229. // Remote member's latest network COM
  230. CertificateOfMembership _com;
  231. // Sorted (in ascending order of ID) arrays of pointers to remote credentials
  232. _RemoteCredential<Tag> *_remoteTags[ZT_MAX_NETWORK_TAGS];
  233. _RemoteCredential<Capability> *_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES];
  234. _RemoteCredential<CertificateOfOwnership> *_remoteCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  235. // This is the RAM allocated for remote credential cache objects
  236. _RemoteCredential<Tag> _tagMem[ZT_MAX_NETWORK_TAGS];
  237. _RemoteCredential<Capability> _capMem[ZT_MAX_NETWORK_CAPABILITIES];
  238. _RemoteCredential<CertificateOfOwnership> _cooMem[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  239. // Local credential push state tracking
  240. _LocalCredentialPushState _localTags[ZT_MAX_NETWORK_TAGS];
  241. _LocalCredentialPushState _localCaps[ZT_MAX_NETWORK_CAPABILITIES];
  242. _LocalCredentialPushState _localCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  243. };
  244. } // namespace ZeroTier
  245. #endif