Membership.cpp 7.2 KB

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