Membership.cpp 7.9 KB

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