Membership.cpp 7.2 KB

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