Membership.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. {
  33. for(unsigned int i=0;i<ZT_MAX_NETWORK_TAGS;++i) _remoteTags[i] = &(_tagMem[i]);
  34. for(unsigned int i=0;i<ZT_MAX_NETWORK_CAPABILITIES;++i) _remoteCaps[i] = &(_capMem[i]);
  35. for(unsigned int i=0;i<ZT_MAX_CERTIFICATES_OF_OWNERSHIP;++i) _remoteCoos[i] = &(_cooMem[i]);
  36. }
  37. void Membership::pushCredentials(const RuntimeEnvironment *RR,const uint64_t now,const Address &peerAddress,const NetworkConfig &nconf,int localCapabilityIndex,const bool force)
  38. {
  39. bool sendCom = ( (nconf.com) && ( ((now - _lastPushedCom) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) );
  40. const Capability *sendCap;
  41. if (localCapabilityIndex >= 0) {
  42. sendCap = &(nconf.capabilities[localCapabilityIndex]);
  43. if ( (_localCaps[localCapabilityIndex].id != sendCap->id()) || ((now - _localCaps[localCapabilityIndex].lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) {
  44. _localCaps[localCapabilityIndex].lastPushed = now;
  45. _localCaps[localCapabilityIndex].id = sendCap->id();
  46. } else sendCap = (const Capability *)0;
  47. } else sendCap = (const Capability *)0;
  48. const Tag *sendTags[ZT_MAX_NETWORK_TAGS];
  49. unsigned int sendTagCount = 0;
  50. for(unsigned int t=0;t<nconf.tagCount;++t) {
  51. if ( (_localTags[t].id != nconf.tags[t].id()) || ((now - _localTags[t].lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) {
  52. _localTags[t].lastPushed = now;
  53. _localTags[t].id = nconf.tags[t].id();
  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 ( (_localCoos[c].id != nconf.certificatesOfOwnership[c].id()) || ((now - _localCoos[c].lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) {
  61. _localCoos[c].lastPushed = now;
  62. _localCoos[c].id = nconf.certificatesOfOwnership[c].id();
  63. sendCoos[sendCooCount++] = &(nconf.certificatesOfOwnership[c]);
  64. }
  65. }
  66. unsigned int tagPtr = 0;
  67. unsigned int cooPtr = 0;
  68. while ((tagPtr < sendTagCount)||(cooPtr < sendCooCount)||(sendCom)||(sendCap)) {
  69. Packet outp(peerAddress,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  70. if (sendCom) {
  71. sendCom = false;
  72. nconf.com.serialize(outp);
  73. _lastPushedCom = now;
  74. }
  75. outp.append((uint8_t)0x00);
  76. if (sendCap) {
  77. outp.append((uint16_t)1);
  78. sendCap->serialize(outp);
  79. sendCap = (const Capability *)0;
  80. } else outp.append((uint16_t)0);
  81. const unsigned int tagCountAt = outp.size();
  82. outp.addSize(2);
  83. unsigned int thisPacketTagCount = 0;
  84. while ((tagPtr < sendTagCount)&&((outp.size() + sizeof(Tag) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  85. sendTags[tagPtr++]->serialize(outp);
  86. ++thisPacketTagCount;
  87. }
  88. outp.setAt(tagCountAt,(uint16_t)thisPacketTagCount);
  89. // No revocations, these propagate differently
  90. outp.append((uint16_t)0);
  91. const unsigned int cooCountAt = outp.size();
  92. outp.addSize(2);
  93. unsigned int thisPacketCooCount = 0;
  94. while ((cooPtr < sendCooCount)&&((outp.size() + sizeof(CertificateOfOwnership) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  95. sendCoos[cooPtr++]->serialize(outp);
  96. ++thisPacketCooCount;
  97. }
  98. outp.setAt(cooCountAt,(uint16_t)thisPacketCooCount);
  99. outp.compress();
  100. RR->sw->send(outp,true);
  101. }
  102. }
  103. const Tag *Membership::getTag(const NetworkConfig &nconf,const uint32_t id) const
  104. {
  105. const _RemoteCredential<Tag> *const *t = std::lower_bound(&(_remoteTags[0]),&(_remoteTags[ZT_MAX_NETWORK_TAGS]),(uint64_t)id,_RemoteCredentialComp<Tag>());
  106. return ( ((t != &(_remoteTags[ZT_MAX_NETWORK_CAPABILITIES]))&&((*t)->id == (uint64_t)id)) ? ((((*t)->lastReceived)&&(_isCredentialTimestampValid(nconf,**t))) ? &((*t)->credential) : (const Tag *)0) : (const Tag *)0);
  107. }
  108. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const CertificateOfMembership &com)
  109. {
  110. const uint64_t newts = com.timestamp().first;
  111. if (newts <= _comRevocationThreshold) {
  112. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (revoked)",com.issuedTo().toString().c_str(),com.networkId());
  113. return ADD_REJECTED;
  114. }
  115. const uint64_t oldts = _com.timestamp().first;
  116. if (newts < oldts) {
  117. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (older than current)",com.issuedTo().toString().c_str(),com.networkId());
  118. return ADD_REJECTED;
  119. }
  120. if ((newts == oldts)&&(_com == com)) {
  121. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx ACCEPTED (redundant)",com.issuedTo().toString().c_str(),com.networkId());
  122. return ADD_ACCEPTED_REDUNDANT;
  123. }
  124. switch(com.verify(RR)) {
  125. default:
  126. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (invalid signature or object)",com.issuedTo().toString().c_str(),com.networkId());
  127. return ADD_REJECTED;
  128. case 0:
  129. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx ACCEPTED (new)",com.issuedTo().toString().c_str(),com.networkId());
  130. _com = com;
  131. return ADD_ACCEPTED_NEW;
  132. case 1:
  133. return ADD_DEFERRED_FOR_WHOIS;
  134. }
  135. }
  136. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const Tag &tag)
  137. {
  138. _RemoteCredential<Tag> *const *htmp = std::lower_bound(&(_remoteTags[0]),&(_remoteTags[ZT_MAX_NETWORK_TAGS]),(uint64_t)tag.id(),_RemoteCredentialComp<Tag>());
  139. _RemoteCredential<Tag> *have = ((htmp != &(_remoteTags[ZT_MAX_NETWORK_TAGS]))&&((*htmp)->id == (uint64_t)tag.id())) ? *htmp : (_RemoteCredential<Tag> *)0;
  140. if (have) {
  141. if ( (!_isCredentialTimestampValid(nconf,*have)) || (have->credential.timestamp() > tag.timestamp()) ) {
  142. TRACE("addCredential(Tag) for %s on %.16llx REJECTED (revoked or too old)",tag.issuedTo().toString().c_str(),tag.networkId());
  143. return ADD_REJECTED;
  144. }
  145. if (have->credential == tag) {
  146. TRACE("addCredential(Tag) for %s on %.16llx ACCEPTED (redundant)",tag.issuedTo().toString().c_str(),tag.networkId());
  147. return ADD_ACCEPTED_REDUNDANT;
  148. }
  149. }
  150. switch(tag.verify(RR)) {
  151. default:
  152. TRACE("addCredential(Tag) for %s on %.16llx REJECTED (invalid)",tag.issuedTo().toString().c_str(),tag.networkId());
  153. return ADD_REJECTED;
  154. case 0:
  155. TRACE("addCredential(Tag) for %s on %.16llx ACCEPTED (new)",tag.issuedTo().toString().c_str(),tag.networkId());
  156. if (!have) have = _newTag(tag.id());
  157. have->lastReceived = RR->node->now();
  158. have->credential = tag;
  159. return ADD_ACCEPTED_NEW;
  160. case 1:
  161. return ADD_DEFERRED_FOR_WHOIS;
  162. }
  163. }
  164. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const Capability &cap)
  165. {
  166. _RemoteCredential<Capability> *const *htmp = std::lower_bound(&(_remoteCaps[0]),&(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]),(uint64_t)cap.id(),_RemoteCredentialComp<Capability>());
  167. _RemoteCredential<Capability> *have = ((htmp != &(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]))&&((*htmp)->id == (uint64_t)cap.id())) ? *htmp : (_RemoteCredential<Capability> *)0;
  168. if (have) {
  169. if ( (!_isCredentialTimestampValid(nconf,*have)) || (have->credential.timestamp() > cap.timestamp()) ) {
  170. TRACE("addCredential(Capability) for %s on %.16llx REJECTED (revoked or too old)",cap.issuedTo().toString().c_str(),cap.networkId());
  171. return ADD_REJECTED;
  172. }
  173. if (have->credential == cap) {
  174. TRACE("addCredential(Capability) for %s on %.16llx ACCEPTED (redundant)",cap.issuedTo().toString().c_str(),cap.networkId());
  175. return ADD_ACCEPTED_REDUNDANT;
  176. }
  177. }
  178. switch(cap.verify(RR)) {
  179. default:
  180. TRACE("addCredential(Capability) for %s on %.16llx REJECTED (invalid)",cap.issuedTo().toString().c_str(),cap.networkId());
  181. return ADD_REJECTED;
  182. case 0:
  183. TRACE("addCredential(Capability) for %s on %.16llx ACCEPTED (new)",cap.issuedTo().toString().c_str(),cap.networkId());
  184. if (!have) have = _newCapability(cap.id());
  185. have->lastReceived = RR->node->now();
  186. have->credential = cap;
  187. return ADD_ACCEPTED_NEW;
  188. case 1:
  189. return ADD_DEFERRED_FOR_WHOIS;
  190. }
  191. }
  192. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const Revocation &rev)
  193. {
  194. switch(rev.verify(RR)) {
  195. default:
  196. return ADD_REJECTED;
  197. case 0: {
  198. const uint64_t now = RR->node->now();
  199. switch(rev.type()) {
  200. default:
  201. return ADD_REJECTED;
  202. case Revocation::CREDENTIAL_TYPE_COM:
  203. return (_revokeCom(rev) ? ADD_ACCEPTED_NEW : ADD_ACCEPTED_REDUNDANT);
  204. case Revocation::CREDENTIAL_TYPE_CAPABILITY:
  205. return (_revokeCap(rev,now) ? ADD_ACCEPTED_NEW : ADD_ACCEPTED_REDUNDANT);
  206. case Revocation::CREDENTIAL_TYPE_TAG:
  207. return (_revokeTag(rev,now) ? ADD_ACCEPTED_NEW : ADD_ACCEPTED_REDUNDANT);
  208. case Revocation::CREDENTIAL_TYPE_COO:
  209. return (_revokeCoo(rev,now) ? ADD_ACCEPTED_NEW : ADD_ACCEPTED_REDUNDANT);
  210. }
  211. }
  212. case 1:
  213. return ADD_DEFERRED_FOR_WHOIS;
  214. }
  215. }
  216. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const CertificateOfOwnership &coo)
  217. {
  218. _RemoteCredential<CertificateOfOwnership> *const *htmp = std::lower_bound(&(_remoteCoos[0]),&(_remoteCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP]),(uint64_t)coo.id(),_RemoteCredentialComp<CertificateOfOwnership>());
  219. _RemoteCredential<CertificateOfOwnership> *have = ((htmp != &(_remoteCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP]))&&((*htmp)->id == (uint64_t)coo.id())) ? *htmp : (_RemoteCredential<CertificateOfOwnership> *)0;
  220. if (have) {
  221. if ( (!_isCredentialTimestampValid(nconf,*have)) || (have->credential.timestamp() > coo.timestamp()) ) {
  222. TRACE("addCredential(CertificateOfOwnership) for %s on %.16llx REJECTED (revoked or too old)",coo.issuedTo().toString().c_str(),coo.networkId());
  223. return ADD_REJECTED;
  224. }
  225. if (have->credential == coo) {
  226. TRACE("addCredential(CertificateOfOwnership) for %s on %.16llx ACCEPTED (redundant)",coo.issuedTo().toString().c_str(),coo.networkId());
  227. return ADD_ACCEPTED_REDUNDANT;
  228. }
  229. }
  230. switch(coo.verify(RR)) {
  231. default:
  232. TRACE("addCredential(CertificateOfOwnership) for %s on %.16llx REJECTED (invalid)",coo.issuedTo().toString().c_str(),coo.networkId());
  233. return ADD_REJECTED;
  234. case 0:
  235. TRACE("addCredential(CertificateOfOwnership) for %s on %.16llx ACCEPTED (new)",coo.issuedTo().toString().c_str(),coo.networkId());
  236. if (!have) have = _newCoo(coo.id());
  237. have->lastReceived = RR->node->now();
  238. have->credential = coo;
  239. return ADD_ACCEPTED_NEW;
  240. case 1:
  241. return ADD_DEFERRED_FOR_WHOIS;
  242. }
  243. }
  244. Membership::_RemoteCredential<Tag> *Membership::_newTag(const uint64_t id)
  245. {
  246. _RemoteCredential<Tag> *t = NULL;
  247. uint64_t minlr = 0xffffffffffffffffULL;
  248. for(unsigned int i=0;i<ZT_MAX_NETWORK_TAGS;++i) {
  249. if (_remoteTags[i]->id == ZT_MEMBERSHIP_CRED_ID_UNUSED) {
  250. t = _remoteTags[i];
  251. break;
  252. } else if (_remoteTags[i]->lastReceived <= minlr) {
  253. t = _remoteTags[i];
  254. minlr = _remoteTags[i]->lastReceived;
  255. }
  256. }
  257. if (t) {
  258. t->id = id;
  259. t->lastReceived = 0;
  260. t->revocationThreshold = 0;
  261. t->credential = Tag();
  262. }
  263. std::sort(&(_remoteTags[0]),&(_remoteTags[ZT_MAX_NETWORK_TAGS]),_RemoteCredentialComp<Tag>());
  264. return t;
  265. }
  266. Membership::_RemoteCredential<Capability> *Membership::_newCapability(const uint64_t id)
  267. {
  268. _RemoteCredential<Capability> *c = NULL;
  269. uint64_t minlr = 0xffffffffffffffffULL;
  270. for(unsigned int i=0;i<ZT_MAX_NETWORK_CAPABILITIES;++i) {
  271. if (_remoteCaps[i]->id == ZT_MEMBERSHIP_CRED_ID_UNUSED) {
  272. c = _remoteCaps[i];
  273. break;
  274. } else if (_remoteCaps[i]->lastReceived <= minlr) {
  275. c = _remoteCaps[i];
  276. minlr = _remoteCaps[i]->lastReceived;
  277. }
  278. }
  279. if (c) {
  280. c->id = id;
  281. c->lastReceived = 0;
  282. c->revocationThreshold = 0;
  283. c->credential = Capability();
  284. }
  285. std::sort(&(_remoteCaps[0]),&(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]),_RemoteCredentialComp<Capability>());
  286. return c;
  287. }
  288. Membership::_RemoteCredential<CertificateOfOwnership> *Membership::_newCoo(const uint64_t id)
  289. {
  290. _RemoteCredential<CertificateOfOwnership> *c = NULL;
  291. uint64_t minlr = 0xffffffffffffffffULL;
  292. for(unsigned int i=0;i<ZT_MAX_CERTIFICATES_OF_OWNERSHIP;++i) {
  293. if (_remoteCoos[i]->id == ZT_MEMBERSHIP_CRED_ID_UNUSED) {
  294. c = _remoteCoos[i];
  295. break;
  296. } else if (_remoteCoos[i]->lastReceived <= minlr) {
  297. c = _remoteCoos[i];
  298. minlr = _remoteCoos[i]->lastReceived;
  299. }
  300. }
  301. if (c) {
  302. c->id = id;
  303. c->lastReceived = 0;
  304. c->revocationThreshold = 0;
  305. c->credential = CertificateOfOwnership();
  306. }
  307. std::sort(&(_remoteCoos[0]),&(_remoteCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP]),_RemoteCredentialComp<CertificateOfOwnership>());
  308. return c;
  309. }
  310. bool Membership::_revokeCom(const Revocation &rev)
  311. {
  312. if (rev.threshold() > _comRevocationThreshold) {
  313. _comRevocationThreshold = rev.threshold();
  314. return true;
  315. }
  316. return false;
  317. }
  318. bool Membership::_revokeCap(const Revocation &rev,const uint64_t now)
  319. {
  320. _RemoteCredential<Capability> *const *htmp = std::lower_bound(&(_remoteCaps[0]),&(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]),(uint64_t)rev.credentialId(),_RemoteCredentialComp<Capability>());
  321. _RemoteCredential<Capability> *have = ((htmp != &(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]))&&((*htmp)->id == (uint64_t)rev.credentialId())) ? *htmp : (_RemoteCredential<Capability> *)0;
  322. if (!have) have = _newCapability(rev.credentialId());
  323. if (rev.threshold() > have->revocationThreshold) {
  324. have->lastReceived = now;
  325. have->revocationThreshold = rev.threshold();
  326. return true;
  327. }
  328. return false;
  329. }
  330. bool Membership::_revokeTag(const Revocation &rev,const uint64_t now)
  331. {
  332. _RemoteCredential<Tag> *const *htmp = std::lower_bound(&(_remoteTags[0]),&(_remoteTags[ZT_MAX_NETWORK_TAGS]),(uint64_t)rev.credentialId(),_RemoteCredentialComp<Tag>());
  333. _RemoteCredential<Tag> *have = ((htmp != &(_remoteTags[ZT_MAX_NETWORK_TAGS]))&&((*htmp)->id == (uint64_t)rev.credentialId())) ? *htmp : (_RemoteCredential<Tag> *)0;
  334. if (!have) have = _newTag(rev.credentialId());
  335. if (rev.threshold() > have->revocationThreshold) {
  336. have->lastReceived = now;
  337. have->revocationThreshold = rev.threshold();
  338. return true;
  339. }
  340. return false;
  341. }
  342. bool Membership::_revokeCoo(const Revocation &rev,const uint64_t now)
  343. {
  344. _RemoteCredential<CertificateOfOwnership> *const *htmp = std::lower_bound(&(_remoteCoos[0]),&(_remoteCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP]),(uint64_t)rev.credentialId(),_RemoteCredentialComp<CertificateOfOwnership>());
  345. _RemoteCredential<CertificateOfOwnership> *have = ((htmp != &(_remoteCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP]))&&((*htmp)->id == (uint64_t)rev.credentialId())) ? *htmp : (_RemoteCredential<CertificateOfOwnership> *)0;
  346. if (!have) have = _newCoo(rev.credentialId());
  347. if (rev.threshold() > have->revocationThreshold) {
  348. have->lastReceived = now;
  349. have->revocationThreshold = rev.threshold();
  350. return true;
  351. }
  352. return false;
  353. }
  354. } // namespace ZeroTier