Membership.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 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. #include <algorithm>
  19. #include "Membership.hpp"
  20. #include "RuntimeEnvironment.hpp"
  21. #include "Peer.hpp"
  22. #include "Topology.hpp"
  23. #include "Switch.hpp"
  24. #include "Packet.hpp"
  25. #include "Node.hpp"
  26. #define ZT_CREDENTIAL_PUSH_EVERY (ZT_NETWORK_AUTOCONF_DELAY / 3)
  27. namespace ZeroTier {
  28. Membership::Membership() :
  29. _lastUpdatedMulticast(0),
  30. _lastPushedCom(0),
  31. _comRevocationThreshold(0),
  32. _revocations(4),
  33. _remoteTags(4),
  34. _remoteCaps(4),
  35. _remoteCoos(4)
  36. {
  37. resetPushState();
  38. }
  39. void Membership::pushCredentials(const RuntimeEnvironment *RR,void *tPtr,const uint64_t now,const Address &peerAddress,const NetworkConfig &nconf,int localCapabilityIndex,const bool force)
  40. {
  41. bool sendCom = ( (nconf.com) && ( ((now - _lastPushedCom) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) );
  42. const Capability *sendCap;
  43. if (localCapabilityIndex >= 0) {
  44. sendCap = &(nconf.capabilities[localCapabilityIndex]);
  45. if ( ((now - _localCredLastPushed.cap[localCapabilityIndex]) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) )
  46. _localCredLastPushed.cap[localCapabilityIndex] = now;
  47. else sendCap = (const Capability *)0;
  48. } else sendCap = (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. if ( ((now - _localCredLastPushed.tag[t]) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) {
  53. _localCredLastPushed.tag[t] = now;
  54. sendTags[sendTagCount++] = &(nconf.tags[t]);
  55. }
  56. }
  57. const CertificateOfOwnership *sendCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  58. unsigned int sendCooCount = 0;
  59. for(unsigned int c=0;c<nconf.certificateOfOwnershipCount;++c) {
  60. if ( ((now - _localCredLastPushed.coo[c]) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) {
  61. _localCredLastPushed.coo[c] = now;
  62. sendCoos[sendCooCount++] = &(nconf.certificatesOfOwnership[c]);
  63. }
  64. }
  65. unsigned int tagPtr = 0;
  66. unsigned int cooPtr = 0;
  67. while ((tagPtr < sendTagCount)||(cooPtr < sendCooCount)||(sendCom)||(sendCap)) {
  68. Packet outp(peerAddress,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  69. if (sendCom) {
  70. sendCom = false;
  71. nconf.com.serialize(outp);
  72. _lastPushedCom = now;
  73. }
  74. outp.append((uint8_t)0x00);
  75. if (sendCap) {
  76. outp.append((uint16_t)1);
  77. sendCap->serialize(outp);
  78. sendCap = (const Capability *)0;
  79. } else outp.append((uint16_t)0);
  80. const unsigned int tagCountAt = outp.size();
  81. outp.addSize(2);
  82. unsigned int thisPacketTagCount = 0;
  83. while ((tagPtr < sendTagCount)&&((outp.size() + sizeof(Tag) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  84. sendTags[tagPtr++]->serialize(outp);
  85. ++thisPacketTagCount;
  86. }
  87. outp.setAt(tagCountAt,(uint16_t)thisPacketTagCount);
  88. // No revocations, these propagate differently
  89. outp.append((uint16_t)0);
  90. const unsigned int cooCountAt = outp.size();
  91. outp.addSize(2);
  92. unsigned int thisPacketCooCount = 0;
  93. while ((cooPtr < sendCooCount)&&((outp.size() + sizeof(CertificateOfOwnership) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  94. sendCoos[cooPtr++]->serialize(outp);
  95. ++thisPacketCooCount;
  96. }
  97. outp.setAt(cooCountAt,(uint16_t)thisPacketCooCount);
  98. outp.compress();
  99. RR->sw->send(tPtr,outp,true);
  100. }
  101. }
  102. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfMembership &com)
  103. {
  104. const uint64_t newts = com.timestamp();
  105. if (newts <= _comRevocationThreshold) {
  106. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (revoked)",com.issuedTo().toString().c_str(),com.networkId());
  107. return ADD_REJECTED;
  108. }
  109. const uint64_t oldts = _com.timestamp();
  110. if (newts < oldts) {
  111. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (older than current)",com.issuedTo().toString().c_str(),com.networkId());
  112. return ADD_REJECTED;
  113. }
  114. if ((newts == oldts)&&(_com == com)) {
  115. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx ACCEPTED (redundant)",com.issuedTo().toString().c_str(),com.networkId());
  116. return ADD_ACCEPTED_REDUNDANT;
  117. }
  118. switch(com.verify(RR,tPtr)) {
  119. default:
  120. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (invalid signature or object)",com.issuedTo().toString().c_str(),com.networkId());
  121. return ADD_REJECTED;
  122. case 0:
  123. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx ACCEPTED (new)",com.issuedTo().toString().c_str(),com.networkId());
  124. _com = com;
  125. return ADD_ACCEPTED_NEW;
  126. case 1:
  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,uint64_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. TRACE("addCredential(type==%d) for %s on %.16llx REJECTED (older than credential we have)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  138. return Membership::ADD_REJECTED;
  139. }
  140. if (*rc == cred) {
  141. //TRACE("addCredential(type==%d) for %s on %.16llx ACCEPTED (redundant)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  142. return Membership::ADD_ACCEPTED_REDUNDANT;
  143. }
  144. }
  145. const uint64_t *const rt = revocations.get(Membership::credentialKey(C::credentialType(),cred.id()));
  146. if ((rt)&&(*rt >= cred.timestamp())) {
  147. TRACE("addCredential(type==%d) for %s on %.16llx REJECTED (timestamp below revocation threshold)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  148. return Membership::ADD_REJECTED;
  149. }
  150. switch(cred.verify(RR,tPtr)) {
  151. default:
  152. TRACE("addCredential(type==%d) for %s on %.16llx REJECTED (invalid)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  153. return Membership::ADD_REJECTED;
  154. case 0:
  155. TRACE("addCredential(type==%d) for %s on %.16llx ACCEPTED (new)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  156. if (!rc)
  157. rc = &(remoteCreds[cred.id()]);
  158. *rc = cred;
  159. return Membership::ADD_ACCEPTED_NEW;
  160. case 1:
  161. return Membership::ADD_DEFERRED_FOR_WHOIS;
  162. }
  163. }
  164. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Tag &tag) { return _addCredImpl<Tag>(_remoteTags,_revocations,RR,tPtr,nconf,tag); }
  165. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Capability &cap) { return _addCredImpl<Capability>(_remoteCaps,_revocations,RR,tPtr,nconf,cap); }
  166. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfOwnership &coo) { return _addCredImpl<CertificateOfOwnership>(_remoteCoos,_revocations,RR,tPtr,nconf,coo); }
  167. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Revocation &rev)
  168. {
  169. uint64_t *rt;
  170. switch(rev.verify(RR,tPtr)) {
  171. default:
  172. return ADD_REJECTED;
  173. case 0: {
  174. const Credential::Type ct = rev.type();
  175. switch(ct) {
  176. case Credential::CREDENTIAL_TYPE_COM:
  177. if (rev.threshold() > _comRevocationThreshold) {
  178. _comRevocationThreshold = rev.threshold();
  179. return ADD_ACCEPTED_NEW;
  180. }
  181. return ADD_ACCEPTED_REDUNDANT;
  182. case Credential::CREDENTIAL_TYPE_CAPABILITY:
  183. case Credential::CREDENTIAL_TYPE_TAG:
  184. case Credential::CREDENTIAL_TYPE_COO:
  185. rt = &(_revocations[credentialKey(ct,rev.credentialId())]);
  186. if (*rt < rev.threshold()) {
  187. *rt = rev.threshold();
  188. return ADD_ACCEPTED_NEW;
  189. }
  190. return ADD_ACCEPTED_REDUNDANT;
  191. default:
  192. return ADD_REJECTED;
  193. }
  194. }
  195. case 1:
  196. return ADD_DEFERRED_FOR_WHOIS;
  197. }
  198. }
  199. void Membership::clean(const uint64_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