Membership.cpp 7.2 KB

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