Membership.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. }
  40. const Tag *sendTags[ZT_MAX_NETWORK_TAGS];
  41. unsigned int sendTagCount = 0;
  42. for(unsigned int t=0;t<nconf.tagCount;++t) {
  43. sendTags[sendTagCount++] = &(nconf.tags[t]);
  44. }
  45. const CertificateOfOwnership *sendCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  46. unsigned int sendCooCount = 0;
  47. for(unsigned int c=0;c<nconf.certificateOfOwnershipCount;++c) {
  48. sendCoos[sendCooCount++] = &(nconf.certificatesOfOwnership[c]);
  49. }
  50. unsigned int capPtr = 0;
  51. unsigned int tagPtr = 0;
  52. unsigned int cooPtr = 0;
  53. bool sendCom = (bool)(nconf.com);
  54. while ((capPtr < sendCapCount)||(tagPtr < sendTagCount)||(cooPtr < sendCooCount)||(sendCom)) {
  55. Packet outp(peerAddress,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  56. if (sendCom) {
  57. sendCom = false;
  58. nconf.com.serialize(outp);
  59. }
  60. outp.append((uint8_t)0x00);
  61. const unsigned int capCountAt = outp.size();
  62. outp.addSize(2);
  63. unsigned int thisPacketCapCount = 0;
  64. while ((capPtr < sendCapCount)&&((outp.size() + sizeof(Capability) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  65. sendCaps[capPtr++]->serialize(outp);
  66. ++thisPacketCapCount;
  67. }
  68. outp.setAt(capCountAt,(uint16_t)thisPacketCapCount);
  69. const unsigned int tagCountAt = outp.size();
  70. outp.addSize(2);
  71. unsigned int thisPacketTagCount = 0;
  72. while ((tagPtr < sendTagCount)&&((outp.size() + sizeof(Tag) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  73. sendTags[tagPtr++]->serialize(outp);
  74. ++thisPacketTagCount;
  75. }
  76. outp.setAt(tagCountAt,(uint16_t)thisPacketTagCount);
  77. // No revocations, these propagate differently
  78. outp.append((uint16_t)0);
  79. const unsigned int cooCountAt = outp.size();
  80. outp.addSize(2);
  81. unsigned int thisPacketCooCount = 0;
  82. while ((cooPtr < sendCooCount)&&((outp.size() + sizeof(CertificateOfOwnership) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  83. sendCoos[cooPtr++]->serialize(outp);
  84. ++thisPacketCooCount;
  85. }
  86. outp.setAt(cooCountAt,(uint16_t)thisPacketCooCount);
  87. outp.compress();
  88. RR->sw->send(tPtr,outp,true);
  89. Metrics::pkt_network_credentials_out++;
  90. }
  91. _lastPushedCredentials = now;
  92. }
  93. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfMembership &com)
  94. {
  95. const int64_t newts = com.timestamp();
  96. if (newts <= _comRevocationThreshold) {
  97. RR->t->credentialRejected(tPtr,com,"revoked");
  98. return ADD_REJECTED;
  99. }
  100. const int64_t oldts = _com.timestamp();
  101. if (newts < oldts) {
  102. RR->t->credentialRejected(tPtr,com,"old");
  103. return ADD_REJECTED;
  104. }
  105. if (_com == com) {
  106. return ADD_ACCEPTED_REDUNDANT;
  107. }
  108. switch(com.verify(RR,tPtr)) {
  109. default:
  110. RR->t->credentialRejected(tPtr,com,"invalid");
  111. return ADD_REJECTED;
  112. case 0:
  113. //printf("%.16llx %.10llx replacing COM %lld with %lld\n", com.networkId(), com.issuedTo().toInt(), _com.timestamp(), com.timestamp()); fflush(stdout);
  114. _com = com;
  115. return ADD_ACCEPTED_NEW;
  116. case 1:
  117. return ADD_DEFERRED_FOR_WHOIS;
  118. }
  119. }
  120. // Template out addCredential() for many cred types to avoid copypasta
  121. template<typename C>
  122. 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)
  123. {
  124. C *rc = remoteCreds.get(cred.id());
  125. if (rc) {
  126. if (rc->timestamp() > cred.timestamp()) {
  127. RR->t->credentialRejected(tPtr,cred,"old");
  128. return Membership::ADD_REJECTED;
  129. }
  130. if (*rc == cred) {
  131. return Membership::ADD_ACCEPTED_REDUNDANT;
  132. }
  133. }
  134. const int64_t *const rt = revocations.get(Membership::credentialKey(C::credentialType(),cred.id()));
  135. if ((rt)&&(*rt >= cred.timestamp())) {
  136. RR->t->credentialRejected(tPtr,cred,"revoked");
  137. return Membership::ADD_REJECTED;
  138. }
  139. switch(cred.verify(RR,tPtr)) {
  140. default:
  141. RR->t->credentialRejected(tPtr,cred,"invalid");
  142. return Membership::ADD_REJECTED;
  143. case 0:
  144. if (!rc) {
  145. rc = &(remoteCreds[cred.id()]);
  146. }
  147. *rc = cred;
  148. return Membership::ADD_ACCEPTED_NEW;
  149. case 1:
  150. return Membership::ADD_DEFERRED_FOR_WHOIS;
  151. }
  152. }
  153. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Tag &tag) { return _addCredImpl<Tag>(_remoteTags,_revocations,RR,tPtr,nconf,tag); }
  154. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Capability &cap) { return _addCredImpl<Capability>(_remoteCaps,_revocations,RR,tPtr,nconf,cap); }
  155. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfOwnership &coo) { return _addCredImpl<CertificateOfOwnership>(_remoteCoos,_revocations,RR,tPtr,nconf,coo); }
  156. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Revocation &rev)
  157. {
  158. int64_t *rt;
  159. switch(rev.verify(RR,tPtr)) {
  160. default:
  161. RR->t->credentialRejected(tPtr,rev,"invalid");
  162. return ADD_REJECTED;
  163. case 0: {
  164. const Credential::Type ct = rev.type();
  165. switch(ct) {
  166. case Credential::CREDENTIAL_TYPE_COM:
  167. if (rev.threshold() > _comRevocationThreshold) {
  168. _comRevocationThreshold = rev.threshold();
  169. return ADD_ACCEPTED_NEW;
  170. }
  171. return ADD_ACCEPTED_REDUNDANT;
  172. case Credential::CREDENTIAL_TYPE_CAPABILITY:
  173. case Credential::CREDENTIAL_TYPE_TAG:
  174. case Credential::CREDENTIAL_TYPE_COO:
  175. rt = &(_revocations[credentialKey(ct,rev.credentialId())]);
  176. if (*rt < rev.threshold()) {
  177. *rt = rev.threshold();
  178. _comRevocationThreshold = rev.threshold();
  179. return ADD_ACCEPTED_NEW;
  180. }
  181. return ADD_ACCEPTED_REDUNDANT;
  182. default:
  183. RR->t->credentialRejected(tPtr,rev,"invalid");
  184. return ADD_REJECTED;
  185. }
  186. }
  187. case 1:
  188. return ADD_DEFERRED_FOR_WHOIS;
  189. }
  190. }
  191. void Membership::clean(const int64_t now,const NetworkConfig &nconf)
  192. {
  193. _cleanCredImpl<Tag>(nconf,_remoteTags);
  194. _cleanCredImpl<Capability>(nconf,_remoteCaps);
  195. _cleanCredImpl<CertificateOfOwnership>(nconf,_remoteCoos);
  196. }
  197. } // namespace ZeroTier