Membership.cpp 7.6 KB

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