Membership.cpp 12 KB

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