Membership.hpp 9.0 KB

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