Membership.cpp 8.1 KB

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