Membership.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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,**_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,**_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 (_com.timestamp().first <= _comRevocationThreshold)
  174. return false;
  175. return nconf.com.agreesWith(_com);
  176. }
  177. /**
  178. * Check whether the peer represented by this Membership owns a given resource
  179. *
  180. * @tparam Type of resource: InetAddress or MAC
  181. * @param nconf Our network config
  182. * @param r Resource to check
  183. * @return True if this peer has a certificate of ownership for the given resource
  184. */
  185. template<typename T>
  186. inline bool hasCertificateOfOwnershipFor(const NetworkConfig &nconf,const T &r) const
  187. {
  188. for(unsigned int i=0;i<ZT_MAX_CERTIFICATES_OF_OWNERSHIP;++i) {
  189. if (_remoteCoos[i]->id == ZT_MEMBERSHIP_CRED_ID_UNUSED)
  190. break;
  191. if ((_isCredentialTimestampValid(nconf,*_remoteCoos[i]))&&(_remoteCoos[i]->credential.owns(r)))
  192. return true;
  193. }
  194. return false;
  195. }
  196. /**
  197. * @param nconf Network configuration
  198. * @param id Tag ID
  199. * @return Pointer to tag or NULL if not found
  200. */
  201. const Tag *getTag(const NetworkConfig &nconf,const uint32_t id) const;
  202. /**
  203. * Validate and add a credential if signature is okay and it's otherwise good
  204. */
  205. AddCredentialResult addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const CertificateOfMembership &com);
  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 Tag &tag);
  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 Capability &cap);
  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 Revocation &rev);
  218. /**
  219. * Validate and add a credential if signature is okay and it's otherwise good
  220. */
  221. AddCredentialResult addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const CertificateOfOwnership &coo);
  222. private:
  223. _RemoteCredential<Tag> *_newTag(const uint64_t id);
  224. _RemoteCredential<Capability> *_newCapability(const uint64_t id);
  225. _RemoteCredential<CertificateOfOwnership> *_newCoo(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. bool _revokeCoo(const Revocation &rev,const uint64_t now);
  230. template<typename C>
  231. inline bool _isCredentialTimestampValid(const NetworkConfig &nconf,const _RemoteCredential<C> &remoteCredential) const
  232. {
  233. if (!remoteCredential.lastReceived)
  234. return false;
  235. const uint64_t ts = remoteCredential.credential.timestamp();
  236. return ( (((ts >= nconf.timestamp) ? (ts - nconf.timestamp) : (nconf.timestamp - ts)) <= nconf.credentialTimeMaxDelta) && (ts > remoteCredential.revocationThreshold) );
  237. }
  238. // Last time we pushed MULTICAST_LIKE(s)
  239. uint64_t _lastUpdatedMulticast;
  240. // Last time we pushed our COM to this peer
  241. uint64_t _lastPushedCom;
  242. // Revocation threshold for COM or 0 if none
  243. uint64_t _comRevocationThreshold;
  244. // Remote member's latest network COM
  245. CertificateOfMembership _com;
  246. // Sorted (in ascending order of ID) arrays of pointers to remote credentials
  247. _RemoteCredential<Tag> *_remoteTags[ZT_MAX_NETWORK_TAGS];
  248. _RemoteCredential<Capability> *_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES];
  249. _RemoteCredential<CertificateOfOwnership> *_remoteCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  250. // This is the RAM allocated for remote credential cache objects
  251. _RemoteCredential<Tag> _tagMem[ZT_MAX_NETWORK_TAGS];
  252. _RemoteCredential<Capability> _capMem[ZT_MAX_NETWORK_CAPABILITIES];
  253. _RemoteCredential<CertificateOfOwnership> _cooMem[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  254. // Local credential push state tracking
  255. _LocalCredentialPushState _localTags[ZT_MAX_NETWORK_TAGS];
  256. _LocalCredentialPushState _localCaps[ZT_MAX_NETWORK_CAPABILITIES];
  257. _LocalCredentialPushState _localCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  258. };
  259. } // namespace ZeroTier
  260. #endif