Membership.cpp 12 KB

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