Membership.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 Capability *Membership::getCapability(const NetworkConfig &nconf,const uint32_t id) const
  104. {
  105. const _RemoteCredential<Capability> *const *c = std::lower_bound(&(_remoteCaps[0]),&(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]),(uint64_t)id,_RemoteCredentialComp<Capability>());
  106. return ( ((c != &(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]))&&((*c)->id == (uint64_t)id)) ? ((((*c)->lastReceived)&&(_isCredentialTimestampValid(nconf,(*c)->credential,**c))) ? &((*c)->credential) : (const Capability *)0) : (const Capability *)0);
  107. }
  108. const Tag *Membership::getTag(const NetworkConfig &nconf,const uint32_t id) const
  109. {
  110. const _RemoteCredential<Tag> *const *t = std::lower_bound(&(_remoteTags[0]),&(_remoteTags[ZT_MAX_NETWORK_TAGS]),(uint64_t)id,_RemoteCredentialComp<Tag>());
  111. return ( ((t != &(_remoteTags[ZT_MAX_NETWORK_CAPABILITIES]))&&((*t)->id == (uint64_t)id)) ? ((((*t)->lastReceived)&&(_isCredentialTimestampValid(nconf,(*t)->credential,**t))) ? &((*t)->credential) : (const Tag *)0) : (const Tag *)0);
  112. }
  113. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const CertificateOfMembership &com)
  114. {
  115. const uint64_t newts = com.timestamp().first;
  116. if (newts <= _comRevocationThreshold) {
  117. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (revoked)",com.issuedTo().toString().c_str(),com.networkId());
  118. return ADD_REJECTED;
  119. }
  120. const uint64_t oldts = _com.timestamp().first;
  121. if (newts < oldts) {
  122. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (older than current)",com.issuedTo().toString().c_str(),com.networkId());
  123. return ADD_REJECTED;
  124. }
  125. if ((newts == oldts)&&(_com == com)) {
  126. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx ACCEPTED (redundant)",com.issuedTo().toString().c_str(),com.networkId());
  127. return ADD_ACCEPTED_REDUNDANT;
  128. }
  129. switch(com.verify(RR)) {
  130. default:
  131. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (invalid signature or object)",com.issuedTo().toString().c_str(),com.networkId());
  132. return ADD_REJECTED;
  133. case 0:
  134. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx ACCEPTED (new)",com.issuedTo().toString().c_str(),com.networkId());
  135. _com = com;
  136. return ADD_ACCEPTED_NEW;
  137. case 1:
  138. return ADD_DEFERRED_FOR_WHOIS;
  139. }
  140. }
  141. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const Tag &tag)
  142. {
  143. _RemoteCredential<Tag> *const *htmp = std::lower_bound(&(_remoteTags[0]),&(_remoteTags[ZT_MAX_NETWORK_TAGS]),(uint64_t)tag.id(),_RemoteCredentialComp<Tag>());
  144. _RemoteCredential<Tag> *have = ((htmp != &(_remoteTags[ZT_MAX_NETWORK_TAGS]))&&((*htmp)->id == (uint64_t)tag.id())) ? *htmp : (_RemoteCredential<Tag> *)0;
  145. if (have) {
  146. if ( (!_isCredentialTimestampValid(nconf,tag,*have)) || (have->credential.timestamp() > tag.timestamp()) ) {
  147. TRACE("addCredential(Tag) for %s on %.16llx REJECTED (revoked or too old)",tag.issuedTo().toString().c_str(),tag.networkId());
  148. return ADD_REJECTED;
  149. }
  150. if (have->credential == tag) {
  151. TRACE("addCredential(Tag) for %s on %.16llx ACCEPTED (redundant)",tag.issuedTo().toString().c_str(),tag.networkId());
  152. return ADD_ACCEPTED_REDUNDANT;
  153. }
  154. }
  155. switch(tag.verify(RR)) {
  156. default:
  157. TRACE("addCredential(Tag) for %s on %.16llx REJECTED (invalid)",tag.issuedTo().toString().c_str(),tag.networkId());
  158. return ADD_REJECTED;
  159. case 0:
  160. TRACE("addCredential(Tag) for %s on %.16llx ACCEPTED (new)",tag.issuedTo().toString().c_str(),tag.networkId());
  161. if (!have) have = _newTag(tag.id());
  162. have->lastReceived = RR->node->now();
  163. have->credential = tag;
  164. return ADD_ACCEPTED_NEW;
  165. case 1:
  166. return ADD_DEFERRED_FOR_WHOIS;
  167. }
  168. }
  169. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const Capability &cap)
  170. {
  171. _RemoteCredential<Capability> *const *htmp = std::lower_bound(&(_remoteCaps[0]),&(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]),(uint64_t)cap.id(),_RemoteCredentialComp<Capability>());
  172. _RemoteCredential<Capability> *have = ((htmp != &(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]))&&((*htmp)->id == (uint64_t)cap.id())) ? *htmp : (_RemoteCredential<Capability> *)0;
  173. if (have) {
  174. if ( (!_isCredentialTimestampValid(nconf,cap,*have)) || (have->credential.timestamp() > cap.timestamp()) ) {
  175. TRACE("addCredential(Capability) for %s on %.16llx REJECTED (revoked or too old)",cap.issuedTo().toString().c_str(),cap.networkId());
  176. return ADD_REJECTED;
  177. }
  178. if (have->credential == cap) {
  179. TRACE("addCredential(Capability) for %s on %.16llx ACCEPTED (redundant)",cap.issuedTo().toString().c_str(),cap.networkId());
  180. return ADD_ACCEPTED_REDUNDANT;
  181. }
  182. }
  183. switch(cap.verify(RR)) {
  184. default:
  185. TRACE("addCredential(Capability) for %s on %.16llx REJECTED (invalid)",cap.issuedTo().toString().c_str(),cap.networkId());
  186. return ADD_REJECTED;
  187. case 0:
  188. TRACE("addCredential(Capability) for %s on %.16llx ACCEPTED (new)",cap.issuedTo().toString().c_str(),cap.networkId());
  189. if (!have) have = _newCapability(cap.id());
  190. have->lastReceived = RR->node->now();
  191. have->credential = cap;
  192. return ADD_ACCEPTED_NEW;
  193. case 1:
  194. return ADD_DEFERRED_FOR_WHOIS;
  195. }
  196. }
  197. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const Revocation &rev)
  198. {
  199. switch(rev.verify(RR)) {
  200. default:
  201. return ADD_REJECTED;
  202. case 0: {
  203. const uint64_t now = RR->node->now();
  204. switch(rev.type()) {
  205. default:
  206. //case Revocation::CREDENTIAL_TYPE_ALL:
  207. return ( (_revokeCom(rev)||_revokeCap(rev,now)||_revokeTag(rev,now)||_revokeCoo(rev,now)) ? ADD_ACCEPTED_NEW : ADD_ACCEPTED_REDUNDANT );
  208. case Revocation::CREDENTIAL_TYPE_COM:
  209. return (_revokeCom(rev) ? ADD_ACCEPTED_NEW : ADD_ACCEPTED_REDUNDANT);
  210. case Revocation::CREDENTIAL_TYPE_CAPABILITY:
  211. return (_revokeCap(rev,now) ? ADD_ACCEPTED_NEW : ADD_ACCEPTED_REDUNDANT);
  212. case Revocation::CREDENTIAL_TYPE_TAG:
  213. return (_revokeTag(rev,now) ? ADD_ACCEPTED_NEW : ADD_ACCEPTED_REDUNDANT);
  214. case Revocation::CREDENTIAL_TYPE_COO:
  215. return (_revokeCoo(rev,now) ? ADD_ACCEPTED_NEW : ADD_ACCEPTED_REDUNDANT);
  216. }
  217. }
  218. case 1:
  219. return ADD_DEFERRED_FOR_WHOIS;
  220. }
  221. }
  222. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const CertificateOfOwnership &coo)
  223. {
  224. _RemoteCredential<CertificateOfOwnership> *const *htmp = std::lower_bound(&(_remoteCoos[0]),&(_remoteCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP]),(uint64_t)coo.id(),_RemoteCredentialComp<CertificateOfOwnership>());
  225. _RemoteCredential<CertificateOfOwnership> *have = ((htmp != &(_remoteCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP]))&&((*htmp)->id == (uint64_t)coo.id())) ? *htmp : (_RemoteCredential<CertificateOfOwnership> *)0;
  226. if (have) {
  227. if ( (!_isCredentialTimestampValid(nconf,coo,*have)) || (have->credential.timestamp() > coo.timestamp()) ) {
  228. TRACE("addCredential(CertificateOfOwnership) for %s on %.16llx REJECTED (revoked or too old)",cap.issuedTo().toString().c_str(),cap.networkId());
  229. return ADD_REJECTED;
  230. }
  231. if (have->credential == coo) {
  232. TRACE("addCredential(CertificateOfOwnership) for %s on %.16llx ACCEPTED (redundant)",cap.issuedTo().toString().c_str(),cap.networkId());
  233. return ADD_ACCEPTED_REDUNDANT;
  234. }
  235. }
  236. switch(coo.verify(RR)) {
  237. default:
  238. TRACE("addCredential(CertificateOfOwnership) for %s on %.16llx REJECTED (invalid)",cap.issuedTo().toString().c_str(),cap.networkId());
  239. return ADD_REJECTED;
  240. case 0:
  241. TRACE("addCredential(CertificateOfOwnership) for %s on %.16llx ACCEPTED (new)",cap.issuedTo().toString().c_str(),cap.networkId());
  242. if (!have) have = _newCoo(coo.id());
  243. have->lastReceived = RR->node->now();
  244. have->credential = coo;
  245. return ADD_ACCEPTED_NEW;
  246. case 1:
  247. return ADD_DEFERRED_FOR_WHOIS;
  248. }
  249. }
  250. Membership::_RemoteCredential<Tag> *Membership::_newTag(const uint64_t id)
  251. {
  252. _RemoteCredential<Tag> *t = NULL;
  253. uint64_t minlr = 0xffffffffffffffffULL;
  254. for(unsigned int i=0;i<ZT_MAX_NETWORK_TAGS;++i) {
  255. if (_remoteTags[i]->id == ZT_MEMBERSHIP_CRED_ID_UNUSED) {
  256. t = _remoteTags[i];
  257. break;
  258. } else if (_remoteTags[i]->lastReceived <= minlr) {
  259. t = _remoteTags[i];
  260. minlr = _remoteTags[i]->lastReceived;
  261. }
  262. }
  263. if (t) {
  264. t->id = id;
  265. t->lastReceived = 0;
  266. t->revocationThreshold = 0;
  267. t->credential = Tag();
  268. }
  269. std::sort(&(_remoteTags[0]),&(_remoteTags[ZT_MAX_NETWORK_TAGS]));
  270. return t;
  271. }
  272. Membership::_RemoteCredential<Capability> *Membership::_newCapability(const uint64_t id)
  273. {
  274. _RemoteCredential<Capability> *c = NULL;
  275. uint64_t minlr = 0xffffffffffffffffULL;
  276. for(unsigned int i=0;i<ZT_MAX_NETWORK_CAPABILITIES;++i) {
  277. if (_remoteCaps[i]->id == ZT_MEMBERSHIP_CRED_ID_UNUSED) {
  278. c = _remoteCaps[i];
  279. break;
  280. } else if (_remoteCaps[i]->lastReceived <= minlr) {
  281. c = _remoteCaps[i];
  282. minlr = _remoteCaps[i]->lastReceived;
  283. }
  284. }
  285. if (c) {
  286. c->id = id;
  287. c->lastReceived = 0;
  288. c->revocationThreshold = 0;
  289. c->credential = Capability();
  290. }
  291. std::sort(&(_remoteCaps[0]),&(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]));
  292. return c;
  293. }
  294. Membership::_RemoteCredential<CertificateOfOwnership> *Membership::_newCoo(const uint64_t id)
  295. {
  296. _RemoteCredential<CertificateOfOwnership> *c = NULL;
  297. uint64_t minlr = 0xffffffffffffffffULL;
  298. for(unsigned int i=0;i<ZT_MAX_CERTIFICATES_OF_OWNERSHIP;++i) {
  299. if (_remoteCoos[i]->id == ZT_MEMBERSHIP_CRED_ID_UNUSED) {
  300. c = _remoteCoos[i];
  301. break;
  302. } else if (_remoteCoos[i]->lastReceived <= minlr) {
  303. c = _remoteCoos[i];
  304. minlr = _remoteCoos[i]->lastReceived;
  305. }
  306. }
  307. if (c) {
  308. c->id = id;
  309. c->lastReceived = 0;
  310. c->revocationThreshold = 0;
  311. c->credential = CertificateOfOwnership();
  312. }
  313. std::sort(&(_remoteCoos[0]),&(_remoteCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP]));
  314. return c;
  315. }
  316. bool Membership::_revokeCom(const Revocation &rev)
  317. {
  318. if (rev.threshold() > _comRevocationThreshold) {
  319. _comRevocationThreshold = rev.threshold();
  320. return true;
  321. }
  322. return false;
  323. }
  324. bool Membership::_revokeCap(const Revocation &rev,const uint64_t now)
  325. {
  326. _RemoteCredential<Capability> *const *htmp = std::lower_bound(&(_remoteCaps[0]),&(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]),(uint64_t)rev.credentialId(),_RemoteCredentialComp<Capability>());
  327. _RemoteCredential<Capability> *have = ((htmp != &(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]))&&((*htmp)->id == (uint64_t)rev.credentialId())) ? *htmp : (_RemoteCredential<Capability> *)0;
  328. if (!have) have = _newCapability(rev.credentialId());
  329. if (rev.threshold() > have->revocationThreshold) {
  330. have->lastReceived = now;
  331. have->revocationThreshold = rev.threshold();
  332. return true;
  333. }
  334. return false;
  335. }
  336. bool Membership::_revokeTag(const Revocation &rev,const uint64_t now)
  337. {
  338. _RemoteCredential<Tag> *const *htmp = std::lower_bound(&(_remoteTags[0]),&(_remoteTags[ZT_MAX_NETWORK_TAGS]),(uint64_t)rev.credentialId(),_RemoteCredentialComp<Tag>());
  339. _RemoteCredential<Tag> *have = ((htmp != &(_remoteTags[ZT_MAX_NETWORK_TAGS]))&&((*htmp)->id == (uint64_t)rev.credentialId())) ? *htmp : (_RemoteCredential<Tag> *)0;
  340. if (!have) have = _newTag(rev.credentialId());
  341. if (rev.threshold() > have->revocationThreshold) {
  342. have->lastReceived = now;
  343. have->revocationThreshold = rev.threshold();
  344. return true;
  345. }
  346. return false;
  347. }
  348. bool Membership::_revokeCoo(const Revocation &rev,const uint64_t now)
  349. {
  350. _RemoteCredential<CertificateOfOwnership> *const *htmp = std::lower_bound(&(_remoteCoos[0]),&(_remoteCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP]),(uint64_t)rev.credentialId(),_RemoteCredentialComp<CertificateOfOwnership>());
  351. _RemoteCredential<CertificateOfOwnership> *have = ((htmp != &(_remoteCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP]))&&((*htmp)->id == (uint64_t)rev.credentialId())) ? *htmp : (_RemoteCredential<CertificateOfOwnership> *)0;
  352. if (!have) have = _newCoo(rev.credentialId());
  353. if (rev.threshold() > have->revocationThreshold) {
  354. have->lastReceived = now;
  355. have->revocationThreshold = rev.threshold();
  356. return true;
  357. }
  358. return false;
  359. }
  360. } // namespace ZeroTier