Membership.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. }
  38. void Membership::pushCredentials(const RuntimeEnvironment *RR,void *tPtr,const uint64_t now,const Address &peerAddress,const NetworkConfig &nconf,int localCapabilityIndex,const bool force)
  39. {
  40. bool sendCom = ( (nconf.com) && ( ((now - _lastPushedCom) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) );
  41. const Capability *sendCap;
  42. if (localCapabilityIndex >= 0) {
  43. sendCap = &(nconf.capabilities[localCapabilityIndex]);
  44. if ( (_localCaps[localCapabilityIndex].id != sendCap->id()) || ((now - _localCaps[localCapabilityIndex].lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) {
  45. _localCaps[localCapabilityIndex].lastPushed = now;
  46. _localCaps[localCapabilityIndex].id = sendCap->id();
  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 ( (_localTags[t].id != nconf.tags[t].id()) || ((now - _localTags[t].lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) {
  53. _localTags[t].lastPushed = now;
  54. _localTags[t].id = nconf.tags[t].id();
  55. sendTags[sendTagCount++] = &(nconf.tags[t]);
  56. }
  57. }
  58. const CertificateOfOwnership *sendCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  59. unsigned int sendCooCount = 0;
  60. for(unsigned int c=0;c<nconf.certificateOfOwnershipCount;++c) {
  61. if ( (_localCoos[c].id != nconf.certificatesOfOwnership[c].id()) || ((now - _localCoos[c].lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) {
  62. _localCoos[c].lastPushed = now;
  63. _localCoos[c].id = nconf.certificatesOfOwnership[c].id();
  64. sendCoos[sendCooCount++] = &(nconf.certificatesOfOwnership[c]);
  65. }
  66. }
  67. unsigned int tagPtr = 0;
  68. unsigned int cooPtr = 0;
  69. while ((tagPtr < sendTagCount)||(cooPtr < sendCooCount)||(sendCom)||(sendCap)) {
  70. Packet outp(peerAddress,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  71. if (sendCom) {
  72. sendCom = false;
  73. nconf.com.serialize(outp);
  74. _lastPushedCom = now;
  75. }
  76. outp.append((uint8_t)0x00);
  77. if (sendCap) {
  78. outp.append((uint16_t)1);
  79. sendCap->serialize(outp);
  80. sendCap = (const Capability *)0;
  81. } else outp.append((uint16_t)0);
  82. const unsigned int tagCountAt = outp.size();
  83. outp.addSize(2);
  84. unsigned int thisPacketTagCount = 0;
  85. while ((tagPtr < sendTagCount)&&((outp.size() + sizeof(Tag) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  86. sendTags[tagPtr++]->serialize(outp);
  87. ++thisPacketTagCount;
  88. }
  89. outp.setAt(tagCountAt,(uint16_t)thisPacketTagCount);
  90. // No revocations, these propagate differently
  91. outp.append((uint16_t)0);
  92. const unsigned int cooCountAt = outp.size();
  93. outp.addSize(2);
  94. unsigned int thisPacketCooCount = 0;
  95. while ((cooPtr < sendCooCount)&&((outp.size() + sizeof(CertificateOfOwnership) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  96. sendCoos[cooPtr++]->serialize(outp);
  97. ++thisPacketCooCount;
  98. }
  99. outp.setAt(cooCountAt,(uint16_t)thisPacketCooCount);
  100. outp.compress();
  101. RR->sw->send(tPtr,outp,true);
  102. }
  103. }
  104. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfMembership &com)
  105. {
  106. const uint64_t newts = com.timestamp();
  107. if (newts <= _comRevocationThreshold) {
  108. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (revoked)",com.issuedTo().toString().c_str(),com.networkId());
  109. return ADD_REJECTED;
  110. }
  111. const uint64_t oldts = _com.timestamp();
  112. if (newts < oldts) {
  113. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (older than current)",com.issuedTo().toString().c_str(),com.networkId());
  114. return ADD_REJECTED;
  115. }
  116. if ((newts == oldts)&&(_com == com)) {
  117. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx ACCEPTED (redundant)",com.issuedTo().toString().c_str(),com.networkId());
  118. return ADD_ACCEPTED_REDUNDANT;
  119. }
  120. switch(com.verify(RR,tPtr)) {
  121. default:
  122. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (invalid signature or object)",com.issuedTo().toString().c_str(),com.networkId());
  123. return ADD_REJECTED;
  124. case 0:
  125. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx ACCEPTED (new)",com.issuedTo().toString().c_str(),com.networkId());
  126. _com = com;
  127. return ADD_ACCEPTED_NEW;
  128. case 1:
  129. return ADD_DEFERRED_FOR_WHOIS;
  130. }
  131. }
  132. // Template out addCredential() for most cred types to avoid copypasta
  133. template<typename C>
  134. 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)
  135. {
  136. C *rc = remoteCreds.get(cred.id());
  137. if (rc) {
  138. if (rc->timestamp() >= cred.timestamp()) {
  139. TRACE("addCredential(type==%d) for %s on %.16llx REJECTED (older than credential we have)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  140. return Membership::ADD_REJECTED;
  141. }
  142. if (*rc == cred) {
  143. TRACE("addCredential(type==%d) for %s on %.16llx ACCEPTED (redundant)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  144. return Membership::ADD_ACCEPTED_REDUNDANT;
  145. }
  146. }
  147. const uint64_t *rt = revocations.get(Membership::revocationKey(C::credentialType(),cred.id()));
  148. if ((rt)&&(*rt >= cred.timestamp())) {
  149. TRACE("addCredential(type==%d) for %s on %.16llx REJECTED (timestamp below revocation threshold)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  150. return Membership::ADD_REJECTED;
  151. }
  152. switch(cred.verify(RR,tPtr)) {
  153. default:
  154. TRACE("addCredential(type==%d) for %s on %.16llx REJECTED (invalid)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  155. return Membership::ADD_REJECTED;
  156. case 0:
  157. TRACE("addCredential(type==%d) for %s on %.16llx ACCEPTED (new)",(int)C::credentialType(),cred.issuedTo().toString().c_str(),cred.networkId());
  158. if (!rc)
  159. rc = &(remoteCreds[cred.id()]);
  160. *rc = cred;
  161. return Membership::ADD_ACCEPTED_NEW;
  162. case 1:
  163. return Membership::ADD_DEFERRED_FOR_WHOIS;
  164. }
  165. }
  166. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Tag &tag)
  167. {
  168. return _addCredImpl<Tag>(_remoteTags,_revocations,RR,tPtr,nconf,tag);
  169. }
  170. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Capability &cap)
  171. {
  172. return _addCredImpl<Capability>(_remoteCaps,_revocations,RR,tPtr,nconf,cap);
  173. }
  174. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const CertificateOfOwnership &coo)
  175. {
  176. return _addCredImpl<CertificateOfOwnership>(_remoteCoos,_revocations,RR,tPtr,nconf,coo);
  177. }
  178. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const NetworkConfig &nconf,const Revocation &rev)
  179. {
  180. uint64_t *rt;
  181. switch(rev.verify(RR,tPtr)) {
  182. default:
  183. return ADD_REJECTED;
  184. case 0: {
  185. const Credential::Type ct = rev.type();
  186. switch(ct) {
  187. case Credential::CREDENTIAL_TYPE_COM:
  188. if (rev.threshold() > _comRevocationThreshold) {
  189. _comRevocationThreshold = rev.threshold();
  190. return ADD_ACCEPTED_NEW;
  191. }
  192. return ADD_ACCEPTED_REDUNDANT;
  193. case Credential::CREDENTIAL_TYPE_CAPABILITY:
  194. case Credential::CREDENTIAL_TYPE_TAG:
  195. case Credential::CREDENTIAL_TYPE_COO:
  196. rt = &(_revocations[revocationKey(ct,rev.credentialId())]);
  197. if (*rt < rev.threshold()) {
  198. *rt = rev.threshold();
  199. return ADD_ACCEPTED_NEW;
  200. }
  201. return ADD_ACCEPTED_REDUNDANT;
  202. default:
  203. return ADD_REJECTED;
  204. }
  205. }
  206. case 1:
  207. return ADD_DEFERRED_FOR_WHOIS;
  208. }
  209. }
  210. } // namespace ZeroTier