Membership.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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. #include <algorithm>
  27. #include "Membership.hpp"
  28. #include "RuntimeEnvironment.hpp"
  29. #include "Peer.hpp"
  30. #include "Topology.hpp"
  31. #include "Switch.hpp"
  32. #include "Packet.hpp"
  33. #include "Node.hpp"
  34. #define ZT_CREDENTIAL_PUSH_EVERY (ZT_NETWORK_AUTOCONF_DELAY / 3)
  35. namespace ZeroTier {
  36. Membership::Membership() :
  37. _lastUpdatedMulticast(0),
  38. _lastPushedCom(0),
  39. _comRevocationThreshold(0),
  40. _revocations(4),
  41. _remoteTags(4),
  42. _remoteCaps(4),
  43. _remoteCoos(4)
  44. {
  45. resetPushState();
  46. }
  47. void Membership::pushCredentials(const RuntimeEnvironment *RR,void *tPtr,const uint64_t now,const Address &peerAddress,const NetworkConfig &nconf,int localCapabilityIndex,const bool force)
  48. {
  49. bool sendCom = ( (nconf.com) && ( ((now - _lastPushedCom) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) );
  50. const Capability *sendCap;
  51. if (localCapabilityIndex >= 0) {
  52. sendCap = &(nconf.capabilities[localCapabilityIndex]);
  53. if ( ((now - _localCredLastPushed.cap[localCapabilityIndex]) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) )
  54. _localCredLastPushed.cap[localCapabilityIndex] = now;
  55. else sendCap = (const Capability *)0;
  56. } else sendCap = (const Capability *)0;
  57. const Tag *sendTags[ZT_MAX_NETWORK_TAGS];
  58. unsigned int sendTagCount = 0;
  59. for(unsigned int t=0;t<nconf.tagCount;++t) {
  60. if ( ((now - _localCredLastPushed.tag[t]) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) {
  61. _localCredLastPushed.tag[t] = now;
  62. sendTags[sendTagCount++] = &(nconf.tags[t]);
  63. }
  64. }
  65. const CertificateOfOwnership *sendCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  66. unsigned int sendCooCount = 0;
  67. for(unsigned int c=0;c<nconf.certificateOfOwnershipCount;++c) {
  68. if ( ((now - _localCredLastPushed.coo[c]) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) {
  69. _localCredLastPushed.coo[c] = now;
  70. sendCoos[sendCooCount++] = &(nconf.certificatesOfOwnership[c]);
  71. }
  72. }
  73. unsigned int tagPtr = 0;
  74. unsigned int cooPtr = 0;
  75. while ((tagPtr < sendTagCount)||(cooPtr < sendCooCount)||(sendCom)||(sendCap)) {
  76. Packet outp(peerAddress,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  77. if (sendCom) {
  78. sendCom = false;
  79. nconf.com.serialize(outp);
  80. _lastPushedCom = now;
  81. }
  82. outp.append((uint8_t)0x00);
  83. if (sendCap) {
  84. outp.append((uint16_t)1);
  85. sendCap->serialize(outp);
  86. sendCap = (const Capability *)0;
  87. } else outp.append((uint16_t)0);
  88. const unsigned int tagCountAt = outp.size();
  89. outp.addSize(2);
  90. unsigned int thisPacketTagCount = 0;
  91. while ((tagPtr < sendTagCount)&&((outp.size() + sizeof(Tag) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  92. sendTags[tagPtr++]->serialize(outp);
  93. ++thisPacketTagCount;
  94. }
  95. outp.setAt(tagCountAt,(uint16_t)thisPacketTagCount);
  96. // No revocations, these propagate differently
  97. outp.append((uint16_t)0);
  98. const unsigned int cooCountAt = outp.size();
  99. outp.addSize(2);
  100. unsigned int thisPacketCooCount = 0;
  101. while ((cooPtr < sendCooCount)&&((outp.size() + sizeof(CertificateOfOwnership) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  102. sendCoos[cooPtr++]->serialize(outp);
  103. ++thisPacketCooCount;
  104. }
  105. outp.setAt(cooCountAt,(uint16_t)thisPacketCooCount);
  106. outp.compress();
  107. RR->sw->send(tPtr,outp,true);
  108. }
  109. }
  110. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfMembership &com)
  111. {
  112. const uint64_t newts = com.timestamp();
  113. if (newts <= _comRevocationThreshold) {
  114. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (revoked)",com.issuedTo().toString().c_str(),com.networkId());
  115. return ADD_REJECTED;
  116. }
  117. const uint64_t oldts = _com.timestamp();
  118. if (newts < oldts) {
  119. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (older than current)",com.issuedTo().toString().c_str(),com.networkId());
  120. return ADD_REJECTED;
  121. }
  122. if ((newts == oldts)&&(_com == com)) {
  123. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx ACCEPTED (redundant)",com.issuedTo().toString().c_str(),com.networkId());
  124. return ADD_ACCEPTED_REDUNDANT;
  125. }
  126. switch(com.verify(RR,tPtr)) {
  127. default:
  128. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (invalid signature or object)",com.issuedTo().toString().c_str(),com.networkId());
  129. return ADD_REJECTED;
  130. case 0:
  131. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx ACCEPTED (new)",com.issuedTo().toString().c_str(),com.networkId());
  132. _com = com;
  133. return ADD_ACCEPTED_NEW;
  134. case 1:
  135. return ADD_DEFERRED_FOR_WHOIS;
  136. }
  137. }
  138. // Template out addCredential() for many cred types to avoid copypasta
  139. template<typename C>
  140. static Membership::AddCredentialResult _addCredImpl(Hashtable<uint32_t,C> &remoteCreds,const Hashtable<uint64_t,uint64_t> &revocations,const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const C &cred)
  141. {
  142. C *rc = remoteCreds.get(cred.id());
  143. if (rc) {
  144. if (rc->timestamp() > cred.timestamp()) {
  145. TRACE("addCredential(type==%d) for %s on %.16llx REJECTED (older than credential we have)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  146. return Membership::ADD_REJECTED;
  147. }
  148. if (*rc == cred) {
  149. //TRACE("addCredential(type==%d) for %s on %.16llx ACCEPTED (redundant)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  150. return Membership::ADD_ACCEPTED_REDUNDANT;
  151. }
  152. }
  153. const uint64_t *const rt = revocations.get(Membership::credentialKey(C::credentialType(),cred.id()));
  154. if ((rt)&&(*rt >= cred.timestamp())) {
  155. TRACE("addCredential(type==%d) for %s on %.16llx REJECTED (timestamp below revocation threshold)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  156. return Membership::ADD_REJECTED;
  157. }
  158. switch(cred.verify(RR,tPtr)) {
  159. default:
  160. TRACE("addCredential(type==%d) for %s on %.16llx REJECTED (invalid)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  161. return Membership::ADD_REJECTED;
  162. case 0:
  163. TRACE("addCredential(type==%d) for %s on %.16llx ACCEPTED (new)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  164. if (!rc)
  165. rc = &(remoteCreds[cred.id()]);
  166. *rc = cred;
  167. return Membership::ADD_ACCEPTED_NEW;
  168. case 1:
  169. return Membership::ADD_DEFERRED_FOR_WHOIS;
  170. }
  171. }
  172. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Tag &tag) { return _addCredImpl<Tag>(_remoteTags,_revocations,RR,tPtr,nconf,tag); }
  173. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Capability &cap) { return _addCredImpl<Capability>(_remoteCaps,_revocations,RR,tPtr,nconf,cap); }
  174. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfOwnership &coo) { return _addCredImpl<CertificateOfOwnership>(_remoteCoos,_revocations,RR,tPtr,nconf,coo); }
  175. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Revocation &rev)
  176. {
  177. uint64_t *rt;
  178. switch(rev.verify(RR,tPtr)) {
  179. default:
  180. return ADD_REJECTED;
  181. case 0: {
  182. const Credential::Type ct = rev.type();
  183. switch(ct) {
  184. case Credential::CREDENTIAL_TYPE_COM:
  185. if (rev.threshold() > _comRevocationThreshold) {
  186. _comRevocationThreshold = rev.threshold();
  187. return ADD_ACCEPTED_NEW;
  188. }
  189. return ADD_ACCEPTED_REDUNDANT;
  190. case Credential::CREDENTIAL_TYPE_CAPABILITY:
  191. case Credential::CREDENTIAL_TYPE_TAG:
  192. case Credential::CREDENTIAL_TYPE_COO:
  193. rt = &(_revocations[credentialKey(ct,rev.credentialId())]);
  194. if (*rt < rev.threshold()) {
  195. *rt = rev.threshold();
  196. return ADD_ACCEPTED_NEW;
  197. }
  198. return ADD_ACCEPTED_REDUNDANT;
  199. default:
  200. return ADD_REJECTED;
  201. }
  202. }
  203. case 1:
  204. return ADD_DEFERRED_FOR_WHOIS;
  205. }
  206. }
  207. void Membership::clean(const uint64_t now,const NetworkConfig &nconf)
  208. {
  209. _cleanCredImpl<Tag>(nconf,_remoteTags);
  210. _cleanCredImpl<Capability>(nconf,_remoteCaps);
  211. _cleanCredImpl<CertificateOfOwnership>(nconf,_remoteCoos);
  212. }
  213. } // namespace ZeroTier