Membership.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 Membership::ADD_REJECTED;
  120. case Credential::VERIFY_OK:
  121. _com = com;
  122. return ADD_ACCEPTED_NEW;
  123. case Credential::VERIFY_BAD_SIGNATURE:
  124. RR->t->credentialRejected(tPtr,com,"invalid");
  125. return ADD_REJECTED;
  126. case Credential::VERIFY_NEED_IDENTITY:
  127. return ADD_DEFERRED_FOR_WHOIS;
  128. }
  129. }
  130. // Template out addCredential() for many cred types to avoid copypasta
  131. template<typename C>
  132. 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)
  133. {
  134. C *rc = remoteCreds.get(cred.id());
  135. if (rc) {
  136. if (rc->timestamp() > cred.timestamp()) {
  137. RR->t->credentialRejected(tPtr,cred,"old");
  138. return Membership::ADD_REJECTED;
  139. }
  140. if (*rc == cred)
  141. return Membership::ADD_ACCEPTED_REDUNDANT;
  142. }
  143. const int64_t *const rt = revocations.get(Membership::credentialKey(C::credentialType(),cred.id()));
  144. if ((rt)&&(*rt >= cred.timestamp())) {
  145. RR->t->credentialRejected(tPtr,cred,"revoked");
  146. return Membership::ADD_REJECTED;
  147. }
  148. switch(cred.verify(RR,tPtr)) {
  149. default:
  150. RR->t->credentialRejected(tPtr,cred,"invalid");
  151. return Membership::ADD_REJECTED;
  152. case 0:
  153. if (!rc)
  154. rc = &(remoteCreds[cred.id()]);
  155. *rc = cred;
  156. return Membership::ADD_ACCEPTED_NEW;
  157. case 1:
  158. return Membership::ADD_DEFERRED_FOR_WHOIS;
  159. }
  160. }
  161. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Tag &tag) { return _addCredImpl<Tag>(_remoteTags,_revocations,RR,tPtr,nconf,tag); }
  162. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Capability &cap) { return _addCredImpl<Capability>(_remoteCaps,_revocations,RR,tPtr,nconf,cap); }
  163. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfOwnership &coo) { return _addCredImpl<CertificateOfOwnership>(_remoteCoos,_revocations,RR,tPtr,nconf,coo); }
  164. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Revocation &rev)
  165. {
  166. int64_t *rt;
  167. switch(rev.verify(RR,tPtr)) {
  168. default:
  169. RR->t->credentialRejected(tPtr,rev,"invalid");
  170. return ADD_REJECTED;
  171. case 0: {
  172. const Credential::Type ct = rev.type();
  173. switch(ct) {
  174. case Credential::CREDENTIAL_TYPE_COM:
  175. if (rev.threshold() > _comRevocationThreshold) {
  176. _comRevocationThreshold = rev.threshold();
  177. return ADD_ACCEPTED_NEW;
  178. }
  179. return ADD_ACCEPTED_REDUNDANT;
  180. case Credential::CREDENTIAL_TYPE_CAPABILITY:
  181. case Credential::CREDENTIAL_TYPE_TAG:
  182. case Credential::CREDENTIAL_TYPE_COO:
  183. rt = &(_revocations[credentialKey(ct,rev.credentialId())]);
  184. if (*rt < rev.threshold()) {
  185. *rt = rev.threshold();
  186. _comRevocationThreshold = rev.threshold();
  187. return ADD_ACCEPTED_NEW;
  188. }
  189. return ADD_ACCEPTED_REDUNDANT;
  190. default:
  191. RR->t->credentialRejected(tPtr,rev,"invalid");
  192. return ADD_REJECTED;
  193. }
  194. }
  195. case 1:
  196. return ADD_DEFERRED_FOR_WHOIS;
  197. }
  198. }
  199. void Membership::clean(const int64_t now,const NetworkConfig &nconf)
  200. {
  201. _cleanCredImpl<Tag>(nconf,_remoteTags);
  202. _cleanCredImpl<Capability>(nconf,_remoteCaps);
  203. _cleanCredImpl<CertificateOfOwnership>(nconf,_remoteCoos);
  204. }
  205. } // namespace ZeroTier