Membership.hpp 8.5 KB

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