2
0

Membership.cpp 7.1 KB

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