Membership.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include <algorithm>
  27. #include "Membership.hpp"
  28. #include "RuntimeEnvironment.hpp"
  29. #include "Peer.hpp"
  30. #include "Topology.hpp"
  31. #include "Switch.hpp"
  32. #include "Packet.hpp"
  33. #include "Node.hpp"
  34. #include "Trace.hpp"
  35. namespace ZeroTier {
  36. Membership::Membership() :
  37. _lastUpdatedMulticast(0),
  38. _comRevocationThreshold(0),
  39. _lastPushedCredentials(0),
  40. _revocations(4),
  41. _remoteTags(4),
  42. _remoteCaps(4),
  43. _remoteCoos(4)
  44. {
  45. }
  46. void Membership::pushCredentials(const RuntimeEnvironment *RR,void *tPtr,const int64_t now,const Address &peerAddress,const NetworkConfig &nconf,int localCapabilityIndex)
  47. {
  48. const Capability *sendCap = (localCapabilityIndex >= 0) ? &(nconf.capabilities[localCapabilityIndex]) : (const Capability *)0;
  49. const Tag *sendTags[ZT_MAX_NETWORK_TAGS];
  50. unsigned int sendTagCount = 0;
  51. for(unsigned int t=0;t<nconf.tagCount;++t)
  52. sendTags[sendTagCount++] = &(nconf.tags[t]);
  53. const CertificateOfOwnership *sendCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  54. unsigned int sendCooCount = 0;
  55. for(unsigned int c=0;c<nconf.certificateOfOwnershipCount;++c)
  56. sendCoos[sendCooCount++] = &(nconf.certificatesOfOwnership[c]);
  57. unsigned int tagPtr = 0;
  58. unsigned int cooPtr = 0;
  59. bool sendCom = (bool)(nconf.com);
  60. while ((tagPtr < sendTagCount)||(cooPtr < sendCooCount)||(sendCom)||(sendCap)) {
  61. Packet outp(peerAddress,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  62. if (sendCom) {
  63. sendCom = false;
  64. nconf.com.serialize(outp);
  65. }
  66. outp.append((uint8_t)0x00);
  67. if (sendCap) {
  68. outp.append((uint16_t)1);
  69. sendCap->serialize(outp);
  70. sendCap = (const Capability *)0;
  71. } else outp.append((uint16_t)0);
  72. const unsigned int tagCountAt = outp.size();
  73. outp.addSize(2);
  74. unsigned int thisPacketTagCount = 0;
  75. while ((tagPtr < sendTagCount)&&((outp.size() + sizeof(Tag) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  76. sendTags[tagPtr++]->serialize(outp);
  77. ++thisPacketTagCount;
  78. }
  79. outp.setAt(tagCountAt,(uint16_t)thisPacketTagCount);
  80. // No revocations, these propagate differently
  81. outp.append((uint16_t)0);
  82. const unsigned int cooCountAt = outp.size();
  83. outp.addSize(2);
  84. unsigned int thisPacketCooCount = 0;
  85. while ((cooPtr < sendCooCount)&&((outp.size() + sizeof(CertificateOfOwnership) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  86. sendCoos[cooPtr++]->serialize(outp);
  87. ++thisPacketCooCount;
  88. }
  89. outp.setAt(cooCountAt,(uint16_t)thisPacketCooCount);
  90. outp.compress();
  91. RR->sw->send(tPtr,outp,true);
  92. }
  93. _lastPushedCredentials = now;
  94. }
  95. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfMembership &com)
  96. {
  97. const int64_t newts = com.timestamp();
  98. if (newts <= _comRevocationThreshold) {
  99. RR->t->credentialRejected(tPtr,com,"revoked");
  100. return ADD_REJECTED;
  101. }
  102. const int64_t oldts = _com.timestamp();
  103. if (newts < oldts) {
  104. RR->t->credentialRejected(tPtr,com,"old");
  105. return ADD_REJECTED;
  106. }
  107. if ((newts == oldts)&&(_com == com))
  108. return ADD_ACCEPTED_REDUNDANT;
  109. switch(com.verify(RR,tPtr)) {
  110. default:
  111. RR->t->credentialRejected(tPtr,com,"invalid");
  112. return ADD_REJECTED;
  113. case 0:
  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. const int64_t *const rt = revocations.get(Membership::credentialKey(C::credentialType(),cred.id()));
  134. if ((rt)&&(*rt >= cred.timestamp())) {
  135. RR->t->credentialRejected(tPtr,cred,"revoked");
  136. return Membership::ADD_REJECTED;
  137. }
  138. switch(cred.verify(RR,tPtr)) {
  139. default:
  140. RR->t->credentialRejected(tPtr,cred,"invalid");
  141. return Membership::ADD_REJECTED;
  142. case 0:
  143. if (!rc)
  144. rc = &(remoteCreds[cred.id()]);
  145. *rc = cred;
  146. return Membership::ADD_ACCEPTED_NEW;
  147. case 1:
  148. return Membership::ADD_DEFERRED_FOR_WHOIS;
  149. }
  150. }
  151. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Tag &tag) { return _addCredImpl<Tag>(_remoteTags,_revocations,RR,tPtr,nconf,tag); }
  152. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Capability &cap) { return _addCredImpl<Capability>(_remoteCaps,_revocations,RR,tPtr,nconf,cap); }
  153. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfOwnership &coo) { return _addCredImpl<CertificateOfOwnership>(_remoteCoos,_revocations,RR,tPtr,nconf,coo); }
  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