Membership.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. _lastPushAttempt(0),
  31. _lastPushedCom(0),
  32. _comRevocationThreshold(0)
  33. {
  34. for(unsigned int i=0;i<ZT_MAX_NETWORK_TAGS;++i) _remoteTags[i] = &(_tagMem[i]);
  35. for(unsigned int i=0;i<ZT_MAX_NETWORK_CAPABILITIES;++i) _remoteCaps[i] = &(_capMem[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. // This limits how often we go through this logic, which prevents us from
  40. // doing all this for every single packet or other event.
  41. if ( ((now - _lastPushAttempt) < 1000ULL) && (!force) )
  42. return;
  43. _lastPushAttempt = now;
  44. try {
  45. unsigned int localTagPtr = 0;
  46. bool needCom = ( (nconf.com) && ( ((now - _lastPushedCom) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) );
  47. do {
  48. Buffer<ZT_PROTO_MAX_PACKET_LENGTH> capsAndTags;
  49. unsigned int appendedCaps = 0;
  50. if (localCapabilityIndex >= 0) {
  51. capsAndTags.addSize(2);
  52. if ( (_localCaps[localCapabilityIndex].id != nconf.capabilities[localCapabilityIndex].id()) || ((now - _localCaps[localCapabilityIndex].lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) {
  53. _localCaps[localCapabilityIndex].lastPushed = now;
  54. _localCaps[localCapabilityIndex].id = nconf.capabilities[localCapabilityIndex].id();
  55. nconf.capabilities[localCapabilityIndex].serialize(capsAndTags);
  56. ++appendedCaps;
  57. }
  58. capsAndTags.setAt<uint16_t>(0,(uint16_t)appendedCaps);
  59. localCapabilityIndex = -1; // don't send this cap again on subsequent loops if force is true
  60. } else {
  61. capsAndTags.append((uint16_t)0);
  62. }
  63. unsigned int appendedTags = 0;
  64. const unsigned int tagCountPos = capsAndTags.size();
  65. capsAndTags.addSize(2);
  66. for(;localTagPtr<nconf.tagCount;++localTagPtr) {
  67. if ( (_localTags[localTagPtr].id != nconf.tags[localTagPtr].id()) || ((now - _localTags[localTagPtr].lastPushed) >= ZT_CREDENTIAL_PUSH_EVERY) || (force) ) {
  68. if ((capsAndTags.size() + sizeof(Tag)) >= (ZT_PROTO_MAX_PACKET_LENGTH - sizeof(CertificateOfMembership)))
  69. break;
  70. nconf.tags[localTagPtr].serialize(capsAndTags);
  71. ++appendedTags;
  72. }
  73. }
  74. capsAndTags.setAt<uint16_t>(tagCountPos,(uint16_t)appendedTags);
  75. if (needCom||appendedCaps||appendedTags) {
  76. Packet outp(peerAddress,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  77. if (needCom) {
  78. nconf.com.serialize(outp);
  79. _lastPushedCom = now;
  80. }
  81. outp.append((uint8_t)0x00);
  82. outp.append(capsAndTags.data(),capsAndTags.size());
  83. outp.append((uint16_t)0); // no revocations, these propagate differently
  84. outp.compress();
  85. RR->sw->send(outp,true);
  86. needCom = false; // don't send COM again on subsequent loops if force is true
  87. }
  88. } while (localTagPtr < nconf.tagCount);
  89. } catch ( ... ) {
  90. TRACE("unable to send credentials due to unexpected exception");
  91. }
  92. }
  93. const Capability *Membership::getCapability(const NetworkConfig &nconf,const uint32_t id) const
  94. {
  95. const _RemoteCapability *const *c = std::lower_bound(&(_remoteCaps[0]),&(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]),(uint64_t)id,_RemoteCredentialSorter<_RemoteCapability>());
  96. return ( ((c != &(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]))&&((*c)->id == (uint64_t)id)) ? ((((*c)->lastReceived)&&(_isCredentialTimestampValid(nconf,(*c)->cap,**c))) ? &((*c)->cap) : (const Capability *)0) : (const Capability *)0);
  97. }
  98. const Tag *Membership::getTag(const NetworkConfig &nconf,const uint32_t id) const
  99. {
  100. const _RemoteTag *const *t = std::lower_bound(&(_remoteTags[0]),&(_remoteTags[ZT_MAX_NETWORK_TAGS]),(uint64_t)id,_RemoteCredentialSorter<_RemoteTag>());
  101. return ( ((t != &(_remoteTags[ZT_MAX_NETWORK_CAPABILITIES]))&&((*t)->id == (uint64_t)id)) ? ((((*t)->lastReceived)&&(_isCredentialTimestampValid(nconf,(*t)->tag,**t))) ? &((*t)->tag) : (const Tag *)0) : (const Tag *)0);
  102. }
  103. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const CertificateOfMembership &com)
  104. {
  105. const uint64_t newts = com.timestamp().first;
  106. if (newts <= _comRevocationThreshold) {
  107. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (revoked)",com.issuedTo().toString().c_str(),com.networkId());
  108. return ADD_REJECTED;
  109. }
  110. const uint64_t oldts = _com.timestamp().first;
  111. if (newts < oldts) {
  112. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (older than current)",com.issuedTo().toString().c_str(),com.networkId());
  113. return ADD_REJECTED;
  114. }
  115. if ((newts == oldts)&&(_com == com)) {
  116. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx ACCEPTED (redundant)",com.issuedTo().toString().c_str(),com.networkId());
  117. return ADD_ACCEPTED_REDUNDANT;
  118. }
  119. switch(com.verify(RR)) {
  120. default:
  121. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx REJECTED (invalid signature or object)",com.issuedTo().toString().c_str(),com.networkId());
  122. return ADD_REJECTED;
  123. case 0:
  124. TRACE("addCredential(CertificateOfMembership) for %s on %.16llx ACCEPTED (new)",com.issuedTo().toString().c_str(),com.networkId());
  125. _com = com;
  126. return ADD_ACCEPTED_NEW;
  127. case 1:
  128. return ADD_DEFERRED_FOR_WHOIS;
  129. }
  130. }
  131. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const Tag &tag)
  132. {
  133. _RemoteTag *const *htmp = std::lower_bound(&(_remoteTags[0]),&(_remoteTags[ZT_MAX_NETWORK_TAGS]),(uint64_t)tag.id(),_RemoteCredentialSorter<_RemoteTag>());
  134. _RemoteTag *have = ((htmp != &(_remoteTags[ZT_MAX_NETWORK_TAGS]))&&((*htmp)->id == (uint64_t)tag.id())) ? *htmp : (_RemoteTag *)0;
  135. if (have) {
  136. if ( (!_isCredentialTimestampValid(nconf,tag,*have)) || (have->tag.timestamp() > tag.timestamp()) ) {
  137. TRACE("addCredential(Tag) for %s on %.16llx REJECTED (revoked or too old)",tag.issuedTo().toString().c_str(),tag.networkId());
  138. return ADD_REJECTED;
  139. }
  140. if (have->tag == tag) {
  141. TRACE("addCredential(Tag) for %s on %.16llx ACCEPTED (redundant)",tag.issuedTo().toString().c_str(),tag.networkId());
  142. return ADD_ACCEPTED_REDUNDANT;
  143. }
  144. }
  145. switch(tag.verify(RR)) {
  146. default:
  147. TRACE("addCredential(Tag) for %s on %.16llx REJECTED (invalid)",tag.issuedTo().toString().c_str(),tag.networkId());
  148. return ADD_REJECTED;
  149. case 0:
  150. TRACE("addCredential(Tag) for %s on %.16llx ACCEPTED (new)",tag.issuedTo().toString().c_str(),tag.networkId());
  151. if (!have) have = _newTag(tag.id());
  152. have->lastReceived = RR->node->now();
  153. have->tag = tag;
  154. return ADD_ACCEPTED_NEW;
  155. case 1:
  156. return ADD_DEFERRED_FOR_WHOIS;
  157. }
  158. }
  159. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const Capability &cap)
  160. {
  161. _RemoteCapability *const *htmp = std::lower_bound(&(_remoteCaps[0]),&(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]),(uint64_t)cap.id(),_RemoteCredentialSorter<_RemoteCapability>());
  162. _RemoteCapability *have = ((htmp != &(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]))&&((*htmp)->id == (uint64_t)cap.id())) ? *htmp : (_RemoteCapability *)0;
  163. if (have) {
  164. if ( (!_isCredentialTimestampValid(nconf,cap,*have)) || (have->cap.timestamp() > cap.timestamp()) ) {
  165. TRACE("addCredential(Tag) for %s on %.16llx REJECTED (revoked or too old)",cap.issuedTo().toString().c_str(),cap.networkId());
  166. return ADD_REJECTED;
  167. }
  168. if (have->cap == cap) {
  169. TRACE("addCredential(Tag) for %s on %.16llx ACCEPTED (redundant)",cap.issuedTo().toString().c_str(),cap.networkId());
  170. return ADD_ACCEPTED_REDUNDANT;
  171. }
  172. }
  173. switch(cap.verify(RR)) {
  174. default:
  175. TRACE("addCredential(Tag) for %s on %.16llx REJECTED (invalid)",cap.issuedTo().toString().c_str(),cap.networkId());
  176. return ADD_REJECTED;
  177. case 0:
  178. TRACE("addCredential(Tag) for %s on %.16llx ACCEPTED (new)",cap.issuedTo().toString().c_str(),cap.networkId());
  179. if (!have) have = _newCapability(cap.id());
  180. have->lastReceived = RR->node->now();
  181. have->cap = cap;
  182. return ADD_ACCEPTED_NEW;
  183. case 1:
  184. return ADD_DEFERRED_FOR_WHOIS;
  185. }
  186. }
  187. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,const NetworkConfig &nconf,const Revocation &rev)
  188. {
  189. switch(rev.verify(RR)) {
  190. default:
  191. return ADD_REJECTED;
  192. case 0: {
  193. const uint64_t now = RR->node->now();
  194. switch(rev.type()) {
  195. default:
  196. //case Revocation::CREDENTIAL_TYPE_ALL:
  197. return ( (_revokeCom(rev)||_revokeCap(rev,now)||_revokeTag(rev,now)) ? ADD_ACCEPTED_NEW : ADD_ACCEPTED_REDUNDANT );
  198. case Revocation::CREDENTIAL_TYPE_COM:
  199. return (_revokeCom(rev) ? ADD_ACCEPTED_NEW : ADD_ACCEPTED_REDUNDANT);
  200. case Revocation::CREDENTIAL_TYPE_CAPABILITY:
  201. return (_revokeCap(rev,now) ? ADD_ACCEPTED_NEW : ADD_ACCEPTED_REDUNDANT);
  202. case Revocation::CREDENTIAL_TYPE_TAG:
  203. return (_revokeTag(rev,now) ? ADD_ACCEPTED_NEW : ADD_ACCEPTED_REDUNDANT);
  204. }
  205. }
  206. case 1:
  207. return ADD_DEFERRED_FOR_WHOIS;
  208. }
  209. }
  210. Membership::_RemoteTag *Membership::_newTag(const uint64_t id)
  211. {
  212. _RemoteTag *t = NULL;
  213. uint64_t minlr = 0xffffffffffffffffULL;
  214. for(unsigned int i=0;i<ZT_MAX_NETWORK_TAGS;++i) {
  215. if (_remoteTags[i]->id == ZT_MEMBERSHIP_CRED_ID_UNUSED) {
  216. t = _remoteTags[i];
  217. break;
  218. } else if (_remoteTags[i]->lastReceived <= minlr) {
  219. t = _remoteTags[i];
  220. minlr = _remoteTags[i]->lastReceived;
  221. }
  222. }
  223. if (t) {
  224. t->id = id;
  225. t->lastReceived = 0;
  226. t->revocationThreshold = 0;
  227. t->tag = Tag();
  228. }
  229. std::sort(&(_remoteTags[0]),&(_remoteTags[ZT_MAX_NETWORK_TAGS]),_RemoteCredentialSorter<_RemoteTag>());
  230. return t;
  231. }
  232. Membership::_RemoteCapability *Membership::_newCapability(const uint64_t id)
  233. {
  234. _RemoteCapability *c = NULL;
  235. uint64_t minlr = 0xffffffffffffffffULL;
  236. for(unsigned int i=0;i<ZT_MAX_NETWORK_CAPABILITIES;++i) {
  237. if (_remoteCaps[i]->id == ZT_MEMBERSHIP_CRED_ID_UNUSED) {
  238. c = _remoteCaps[i];
  239. break;
  240. } else if (_remoteCaps[i]->lastReceived <= minlr) {
  241. c = _remoteCaps[i];
  242. minlr = _remoteCaps[i]->lastReceived;
  243. }
  244. }
  245. if (c) {
  246. c->id = id;
  247. c->lastReceived = 0;
  248. c->revocationThreshold = 0;
  249. c->cap = Capability();
  250. }
  251. std::sort(&(_remoteCaps[0]),&(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]),_RemoteCredentialSorter<_RemoteCapability>());
  252. return c;
  253. }
  254. bool Membership::_revokeCom(const Revocation &rev)
  255. {
  256. if (rev.threshold() > _comRevocationThreshold) {
  257. _comRevocationThreshold = rev.threshold();
  258. return true;
  259. }
  260. return false;
  261. }
  262. bool Membership::_revokeCap(const Revocation &rev,const uint64_t now)
  263. {
  264. _RemoteCapability *const *htmp = std::lower_bound(&(_remoteCaps[0]),&(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]),(uint64_t)rev.credentialId(),_RemoteCredentialSorter<_RemoteCapability>());
  265. _RemoteCapability *have = ((htmp != &(_remoteCaps[ZT_MAX_NETWORK_CAPABILITIES]))&&((*htmp)->id == (uint64_t)rev.credentialId())) ? *htmp : (_RemoteCapability *)0;
  266. if (!have) have = _newCapability(rev.credentialId());
  267. if (rev.threshold() > have->revocationThreshold) {
  268. have->lastReceived = now;
  269. have->revocationThreshold = rev.threshold();
  270. return true;
  271. }
  272. return false;
  273. }
  274. bool Membership::_revokeTag(const Revocation &rev,const uint64_t now)
  275. {
  276. _RemoteTag *const *htmp = std::lower_bound(&(_remoteTags[0]),&(_remoteTags[ZT_MAX_NETWORK_TAGS]),(uint64_t)rev.credentialId(),_RemoteCredentialSorter<_RemoteTag>());
  277. _RemoteTag *have = ((htmp != &(_remoteTags[ZT_MAX_NETWORK_TAGS]))&&((*htmp)->id == (uint64_t)rev.credentialId())) ? *htmp : (_RemoteTag *)0;
  278. if (!have) have = _newTag(rev.credentialId());
  279. if (rev.threshold() > have->revocationThreshold) {
  280. have->lastReceived = now;
  281. have->revocationThreshold = rev.threshold();
  282. return true;
  283. }
  284. return false;
  285. }
  286. } // namespace ZeroTier