Membership.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include <algorithm>
  14. #include "Membership.hpp"
  15. #include "RuntimeEnvironment.hpp"
  16. #include "Peer.hpp"
  17. #include "Topology.hpp"
  18. #include "Switch.hpp"
  19. #include "Packet.hpp"
  20. #include "Node.hpp"
  21. #include "Trace.hpp"
  22. namespace ZeroTier {
  23. Membership::Membership() :
  24. _lastUpdatedMulticast(0),
  25. _comRevocationThreshold(0),
  26. _lastPushedCredentials(0),
  27. _revocations(4),
  28. _remoteTags(4),
  29. _remoteCaps(4),
  30. _remoteCoos(4)
  31. {
  32. }
  33. void Membership::pushCredentials(const RuntimeEnvironment *RR,void *tPtr,const int64_t now,const Address &peerAddress,const NetworkConfig &nconf)
  34. {
  35. const Capability *sendCaps[ZT_MAX_NETWORK_CAPABILITIES];
  36. unsigned int sendCapCount = 0;
  37. for(unsigned int c=0;c<nconf.capabilityCount;++c)
  38. sendCaps[sendCapCount++] = &(nconf.capabilities[c]);
  39. const Tag *sendTags[ZT_MAX_NETWORK_TAGS];
  40. unsigned int sendTagCount = 0;
  41. for(unsigned int t=0;t<nconf.tagCount;++t)
  42. sendTags[sendTagCount++] = &(nconf.tags[t]);
  43. const CertificateOfOwnership *sendCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  44. unsigned int sendCooCount = 0;
  45. for(unsigned int c=0;c<nconf.certificateOfOwnershipCount;++c)
  46. sendCoos[sendCooCount++] = &(nconf.certificatesOfOwnership[c]);
  47. unsigned int capPtr = 0;
  48. unsigned int tagPtr = 0;
  49. unsigned int cooPtr = 0;
  50. bool sendCom = (bool)(nconf.com);
  51. while ((capPtr < sendCapCount)||(tagPtr < sendTagCount)||(cooPtr < sendCooCount)||(sendCom)) {
  52. Packet outp(peerAddress,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  53. if (sendCom) {
  54. sendCom = false;
  55. nconf.com.serialize(outp);
  56. }
  57. outp.append((uint8_t)0x00);
  58. const unsigned int capCountAt = outp.size();
  59. outp.addSize(2);
  60. unsigned int thisPacketCapCount = 0;
  61. while ((capPtr < sendCapCount)&&((outp.size() + sizeof(Capability) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  62. sendCaps[capPtr++]->serialize(outp);
  63. ++thisPacketCapCount;
  64. }
  65. outp.setAt(capCountAt,(uint16_t)thisPacketCapCount);
  66. const unsigned int tagCountAt = outp.size();
  67. outp.addSize(2);
  68. unsigned int thisPacketTagCount = 0;
  69. while ((tagPtr < sendTagCount)&&((outp.size() + sizeof(Tag) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  70. sendTags[tagPtr++]->serialize(outp);
  71. ++thisPacketTagCount;
  72. }
  73. outp.setAt(tagCountAt,(uint16_t)thisPacketTagCount);
  74. // No revocations, these propagate differently
  75. outp.append((uint16_t)0);
  76. const unsigned int cooCountAt = outp.size();
  77. outp.addSize(2);
  78. unsigned int thisPacketCooCount = 0;
  79. while ((cooPtr < sendCooCount)&&((outp.size() + sizeof(CertificateOfOwnership) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  80. sendCoos[cooPtr++]->serialize(outp);
  81. ++thisPacketCooCount;
  82. }
  83. outp.setAt(cooCountAt,(uint16_t)thisPacketCooCount);
  84. outp.compress();
  85. RR->sw->send(tPtr,outp,true);
  86. }
  87. _lastPushedCredentials = now;
  88. }
  89. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfMembership &com)
  90. {
  91. const int64_t newts = com.timestamp();
  92. if (newts <= _comRevocationThreshold) {
  93. RR->t->credentialRejected(tPtr,com,"revoked");
  94. return ADD_REJECTED;
  95. }
  96. const int64_t oldts = _com.timestamp();
  97. if (newts < oldts) {
  98. RR->t->credentialRejected(tPtr,com,"old");
  99. return ADD_REJECTED;
  100. }
  101. if (_com == com)
  102. return ADD_ACCEPTED_REDUNDANT;
  103. switch(com.verify(RR,tPtr)) {
  104. default:
  105. RR->t->credentialRejected(tPtr,com,"invalid");
  106. return ADD_REJECTED;
  107. case 0:
  108. //printf("%.16llx %.10llx replacing COM %lld with %lld\n", com.networkId(), com.issuedTo().toInt(), _com.timestamp(), com.timestamp()); fflush(stdout);
  109. _com = com;
  110. return ADD_ACCEPTED_NEW;
  111. case 1:
  112. return ADD_DEFERRED_FOR_WHOIS;
  113. }
  114. }
  115. // Template out addCredential() for many cred types to avoid copypasta
  116. template<typename C>
  117. static Membership::AddCredentialResult _addCredImpl(Hashtable<uint32_t,C> &remoteCreds,const Hashtable<uint64_t,int64_t> &revocations,const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const C &cred)
  118. {
  119. C *rc = remoteCreds.get(cred.id());
  120. if (rc) {
  121. if (rc->timestamp() > cred.timestamp()) {
  122. RR->t->credentialRejected(tPtr,cred,"old");
  123. return Membership::ADD_REJECTED;
  124. }
  125. if (*rc == cred)
  126. return Membership::ADD_ACCEPTED_REDUNDANT;
  127. }
  128. const int64_t *const rt = revocations.get(Membership::credentialKey(C::credentialType(),cred.id()));
  129. if ((rt)&&(*rt >= cred.timestamp())) {
  130. RR->t->credentialRejected(tPtr,cred,"revoked");
  131. return Membership::ADD_REJECTED;
  132. }
  133. switch(cred.verify(RR,tPtr)) {
  134. default:
  135. RR->t->credentialRejected(tPtr,cred,"invalid");
  136. return Membership::ADD_REJECTED;
  137. case 0:
  138. if (!rc)
  139. rc = &(remoteCreds[cred.id()]);
  140. *rc = cred;
  141. return Membership::ADD_ACCEPTED_NEW;
  142. case 1:
  143. return Membership::ADD_DEFERRED_FOR_WHOIS;
  144. }
  145. }
  146. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Tag &tag) { return _addCredImpl<Tag>(_remoteTags,_revocations,RR,tPtr,nconf,tag); }
  147. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Capability &cap) { return _addCredImpl<Capability>(_remoteCaps,_revocations,RR,tPtr,nconf,cap); }
  148. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfOwnership &coo) { return _addCredImpl<CertificateOfOwnership>(_remoteCoos,_revocations,RR,tPtr,nconf,coo); }
  149. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Revocation &rev)
  150. {
  151. int64_t *rt;
  152. switch(rev.verify(RR,tPtr)) {
  153. default:
  154. RR->t->credentialRejected(tPtr,rev,"invalid");
  155. return ADD_REJECTED;
  156. case 0: {
  157. const Credential::Type ct = rev.type();
  158. switch(ct) {
  159. case Credential::CREDENTIAL_TYPE_COM:
  160. if (rev.threshold() > _comRevocationThreshold) {
  161. _comRevocationThreshold = rev.threshold();
  162. return ADD_ACCEPTED_NEW;
  163. }
  164. return ADD_ACCEPTED_REDUNDANT;
  165. case Credential::CREDENTIAL_TYPE_CAPABILITY:
  166. case Credential::CREDENTIAL_TYPE_TAG:
  167. case Credential::CREDENTIAL_TYPE_COO:
  168. rt = &(_revocations[credentialKey(ct,rev.credentialId())]);
  169. if (*rt < rev.threshold()) {
  170. *rt = rev.threshold();
  171. _comRevocationThreshold = rev.threshold();
  172. return ADD_ACCEPTED_NEW;
  173. }
  174. return ADD_ACCEPTED_REDUNDANT;
  175. default:
  176. RR->t->credentialRejected(tPtr,rev,"invalid");
  177. return ADD_REJECTED;
  178. }
  179. }
  180. case 1:
  181. return ADD_DEFERRED_FOR_WHOIS;
  182. }
  183. }
  184. void Membership::clean(const int64_t now,const NetworkConfig &nconf)
  185. {
  186. _cleanCredImpl<Tag>(nconf,_remoteTags);
  187. _cleanCredImpl<Capability>(nconf,_remoteCaps);
  188. _cleanCredImpl<CertificateOfOwnership>(nconf,_remoteCoos);
  189. }
  190. } // namespace ZeroTier