Membership.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_MEMBERSHIP_HPP
  27. #define ZT_MEMBERSHIP_HPP
  28. #include <stdint.h>
  29. #include "Constants.hpp"
  30. #include "../include/ZeroTierOne.h"
  31. #include "Credential.hpp"
  32. #include "Hashtable.hpp"
  33. #include "CertificateOfMembership.hpp"
  34. #include "Capability.hpp"
  35. #include "Tag.hpp"
  36. #include "Revocation.hpp"
  37. #include "NetworkConfig.hpp"
  38. #define ZT_MEMBERSHIP_CRED_ID_UNUSED 0xffffffffffffffffULL
  39. namespace ZeroTier {
  40. class RuntimeEnvironment;
  41. class Network;
  42. /**
  43. * A container for certificates of membership and other network credentials
  44. *
  45. * This is essentially a relational join between Peer and Network.
  46. *
  47. * This class is not thread safe. It must be locked externally.
  48. */
  49. class Membership
  50. {
  51. public:
  52. enum AddCredentialResult
  53. {
  54. ADD_REJECTED,
  55. ADD_ACCEPTED_NEW,
  56. ADD_ACCEPTED_REDUNDANT,
  57. ADD_DEFERRED_FOR_WHOIS
  58. };
  59. Membership();
  60. /**
  61. * Send COM and other credentials to this peer
  62. *
  63. * @param RR Runtime environment
  64. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  65. * @param now Current time
  66. * @param peerAddress Address of member peer (the one that this Membership describes)
  67. * @param nconf My network config
  68. */
  69. void pushCredentials(const RuntimeEnvironment *RR,void *tPtr,const int64_t now,const Address &peerAddress,const NetworkConfig &nconf);
  70. /**
  71. * @return True if we haven't pushed credentials in a long time (to cause proactive credential push)
  72. */
  73. inline bool shouldPushCredentials(const int64_t now) const
  74. {
  75. return ((now - _lastPushedCredentials) > ZT_PEER_ACTIVITY_TIMEOUT);
  76. }
  77. /**
  78. * Check whether we should push MULTICAST_LIKEs to this peer, and update last sent time if true
  79. *
  80. * @param now Current time
  81. * @return True if we should update multicasts
  82. */
  83. inline bool multicastLikeGate(const int64_t now)
  84. {
  85. if ((now - _lastUpdatedMulticast) >= ZT_MULTICAST_ANNOUNCE_PERIOD) {
  86. _lastUpdatedMulticast = now;
  87. return true;
  88. }
  89. return false;
  90. }
  91. /**
  92. * Check whether the peer represented by this Membership should be allowed on this network at all
  93. *
  94. * @param nconf Our network config
  95. * @return True if this peer is allowed on this network at all
  96. */
  97. inline bool isAllowedOnNetwork(const NetworkConfig &nconf) const
  98. {
  99. if (nconf.isPublic()) return true;
  100. if (_com.timestamp() <= _comRevocationThreshold) return false;
  101. return nconf.com.agreesWith(_com);
  102. }
  103. inline bool recentlyAssociated(const int64_t now) const
  104. {
  105. return ((_com)&&((now - _com.timestamp()) < ZT_PEER_ACTIVITY_TIMEOUT));
  106. }
  107. /**
  108. * Check whether the peer represented by this Membership owns a given resource
  109. *
  110. * @tparam Type of resource: InetAddress or MAC
  111. * @param nconf Our network config
  112. * @param r Resource to check
  113. * @return True if this peer has a certificate of ownership for the given resource
  114. */
  115. template<typename T>
  116. inline bool hasCertificateOfOwnershipFor(const NetworkConfig &nconf,const T &r) const
  117. {
  118. uint32_t *k = (uint32_t *)0;
  119. CertificateOfOwnership *v = (CertificateOfOwnership *)0;
  120. Hashtable< uint32_t,CertificateOfOwnership >::Iterator i(*(const_cast< Hashtable< uint32_t,CertificateOfOwnership> *>(&_remoteCoos)));
  121. while (i.next(k,v)) {
  122. if (_isCredentialTimestampValid(nconf,*v)&&(v->owns(r)))
  123. return true;
  124. }
  125. return _isV6NDPEmulated(nconf,r);
  126. }
  127. /**
  128. * Get a remote member's tag (if we have it)
  129. *
  130. * @param nconf Network configuration
  131. * @param id Tag ID
  132. * @return Pointer to tag or NULL if not found
  133. */
  134. inline const Tag *getTag(const NetworkConfig &nconf,const uint32_t id) const
  135. {
  136. const Tag *const t = _remoteTags.get(id);
  137. return (((t)&&(_isCredentialTimestampValid(nconf,*t))) ? t : (Tag *)0);
  138. }
  139. /**
  140. * Validate and add a credential if signature is okay and it's otherwise good
  141. */
  142. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfMembership &com);
  143. /**
  144. * Validate and add a credential if signature is okay and it's otherwise good
  145. */
  146. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Tag &tag);
  147. /**
  148. * Validate and add a credential if signature is okay and it's otherwise good
  149. */
  150. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Capability &cap);
  151. /**
  152. * Validate and add a credential if signature is okay and it's otherwise good
  153. */
  154. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfOwnership &coo);
  155. /**
  156. * Validate and add a credential if signature is okay and it's otherwise good
  157. */
  158. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Revocation &rev);
  159. /**
  160. * Clean internal databases of stale entries
  161. *
  162. * @param now Current time
  163. * @param nconf Current network configuration
  164. */
  165. void clean(const int64_t now,const NetworkConfig &nconf);
  166. /**
  167. * Generates a key for the internal use in indexing credentials by type and credential ID
  168. */
  169. static uint64_t credentialKey(const Credential::Type &t,const uint32_t i) { return (((uint64_t)t << 32) | (uint64_t)i); }
  170. private:
  171. inline bool _isV6NDPEmulated(const NetworkConfig &nconf,const MAC &m) const { return false; }
  172. inline bool _isV6NDPEmulated(const NetworkConfig &nconf,const InetAddress &ip) const
  173. {
  174. if ((ip.isV6())&&(nconf.ndpEmulation())&&((InetAddress::makeIpv66plane(nconf.networkId,nconf.issuedTo.toInt()).ipsEqual(ip))||(InetAddress::makeIpv6rfc4193(nconf.networkId,nconf.issuedTo.toInt()).ipsEqual(ip)))) {
  175. return true;
  176. }
  177. return false;
  178. }
  179. template<typename C>
  180. inline bool _isCredentialTimestampValid(const NetworkConfig &nconf,const C &remoteCredential) const
  181. {
  182. const int64_t ts = remoteCredential.timestamp();
  183. if (((ts >= nconf.timestamp) ? (ts - nconf.timestamp) : (nconf.timestamp - ts)) <= nconf.credentialTimeMaxDelta) {
  184. const int64_t *threshold = _revocations.get(credentialKey(C::credentialType(),remoteCredential.id()));
  185. return ((!threshold)||(ts > *threshold));
  186. }
  187. return false;
  188. }
  189. template<typename C>
  190. inline void _cleanCredImpl(const NetworkConfig &nconf,Hashtable<uint32_t,C> &remoteCreds)
  191. {
  192. uint32_t *k = (uint32_t *)0;
  193. C *v = (C *)0;
  194. typename Hashtable<uint32_t,C>::Iterator i(remoteCreds);
  195. while (i.next(k,v)) {
  196. if (!_isCredentialTimestampValid(nconf,*v))
  197. remoteCreds.erase(*k);
  198. }
  199. }
  200. // Last time we pushed MULTICAST_LIKE(s)
  201. int64_t _lastUpdatedMulticast;
  202. // Revocation threshold for COM or 0 if none
  203. int64_t _comRevocationThreshold;
  204. // Time we last pushed credentials
  205. int64_t _lastPushedCredentials;
  206. // Remote member's latest network COM
  207. CertificateOfMembership _com;
  208. // Revocations by credentialKey()
  209. Hashtable< uint64_t,int64_t > _revocations;
  210. // Remote credentials that we have received from this member (and that are valid)
  211. Hashtable< uint32_t,Tag > _remoteTags;
  212. Hashtable< uint32_t,Capability > _remoteCaps;
  213. Hashtable< uint32_t,CertificateOfOwnership > _remoteCoos;
  214. public:
  215. class CapabilityIterator
  216. {
  217. public:
  218. CapabilityIterator(Membership &m,const NetworkConfig &nconf) :
  219. _hti(m._remoteCaps),
  220. _k((uint32_t *)0),
  221. _c((Capability *)0),
  222. _m(m),
  223. _nconf(nconf)
  224. {
  225. }
  226. inline Capability *next()
  227. {
  228. while (_hti.next(_k,_c)) {
  229. if (_m._isCredentialTimestampValid(_nconf,*_c))
  230. return _c;
  231. }
  232. return (Capability *)0;
  233. }
  234. private:
  235. Hashtable< uint32_t,Capability >::Iterator _hti;
  236. uint32_t *_k;
  237. Capability *_c;
  238. Membership &_m;
  239. const NetworkConfig &_nconf;
  240. };
  241. };
  242. } // namespace ZeroTier
  243. #endif