Membership.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include <algorithm>
  14. #include "Membership.hpp"
  15. #include "RuntimeEnvironment.hpp"
  16. #include "Peer.hpp"
  17. #include "Topology.hpp"
  18. #include "Node.hpp"
  19. namespace ZeroTier {
  20. Membership::Membership() :
  21. _lastUpdatedMulticast(0),
  22. _comRevocationThreshold(0),
  23. _lastPushedCredentials(0),
  24. _revocations(4),
  25. _remoteTags(4),
  26. _remoteCaps(4),
  27. _remoteCoos(4)
  28. {
  29. }
  30. Membership::~Membership()
  31. {
  32. }
  33. void Membership::pushCredentials(const RuntimeEnvironment *RR,void *tPtr,const int64_t now,const Address &peerAddress,const NetworkConfig &nconf)
  34. {
  35. if (!nconf.com) // sanity check
  36. return;
  37. SharedPtr<Buf> outp(new Buf());
  38. Protocol::Header &ph = outp->as<Protocol::Header>();
  39. unsigned int capPtr = 0,tagPtr = 0,cooPtr = 0;
  40. bool sendCom = true;
  41. bool complete = false;
  42. while (!complete) {
  43. ph.packetId = Protocol::getPacketId();
  44. peerAddress.copyTo(ph.destination);
  45. RR->identity.address().copyTo(ph.source);
  46. ph.flags = 0;
  47. ph.verb = Protocol::VERB_NETWORK_CREDENTIALS;
  48. int outl = sizeof(Protocol::Header);
  49. if (sendCom) {
  50. sendCom = false;
  51. outp->wO(outl,nconf.com);
  52. }
  53. outp->wI8(outl,0);
  54. if ((outl + ZT_CAPABILITY_MARSHAL_SIZE_MAX + 2) < ZT_PROTO_MAX_PACKET_LENGTH) {
  55. void *const capCountAt = outp->unsafeData + outl;
  56. outl += 2;
  57. unsigned int capCount = 0;
  58. while (capPtr < nconf.capabilityCount) {
  59. outp->wO(outl,nconf.capabilities[capPtr++]);
  60. ++capCount;
  61. if ((outl + ZT_CAPABILITY_MARSHAL_SIZE_MAX) >= ZT_PROTO_MAX_PACKET_LENGTH)
  62. break;
  63. }
  64. Utils::storeBigEndian(capCountAt,(uint16_t)capCount);
  65. if ((outl + ZT_TAG_MARSHAL_SIZE_MAX + 4) < ZT_PROTO_MAX_PACKET_LENGTH) {
  66. void *const tagCountAt = outp->unsafeData + outl;
  67. outl += 2;
  68. unsigned int tagCount = 0;
  69. while (tagPtr < nconf.tagCount) {
  70. outp->wO(outl,nconf.tags[tagPtr++]);
  71. ++tagCount;
  72. if ((outl + ZT_TAG_MARSHAL_SIZE_MAX) >= ZT_PROTO_MAX_PACKET_LENGTH)
  73. break;
  74. }
  75. Utils::storeBigEndian(tagCountAt,(uint16_t)tagCount);
  76. outp->wI16(outl,0); // no revocations sent here as these propagate differently
  77. if ((outl + ZT_CERTIFICATEOFOWNERSHIP_MARSHAL_SIZE_MAX + 2) < ZT_PROTO_MAX_PACKET_LENGTH) {
  78. void *const cooCountAt = outp->unsafeData + outl;
  79. outl += 2;
  80. unsigned int cooCount = 0;
  81. while (cooPtr < nconf.certificateOfOwnershipCount) {
  82. outp->wO(outl,nconf.certificatesOfOwnership[cooPtr++]);
  83. ++cooCount;
  84. if ((outl + ZT_CERTIFICATEOFOWNERSHIP_MARSHAL_SIZE_MAX) >= ZT_PROTO_MAX_PACKET_LENGTH)
  85. break;
  86. }
  87. Utils::storeBigEndian(cooCountAt,(uint16_t)cooCount);
  88. complete = true;
  89. } else {
  90. outp->wI16(outl,0);
  91. }
  92. } else {
  93. outp->wI32(outl,0);
  94. outp->wI16(outl,0); // three zero 16-bit integers
  95. }
  96. } else {
  97. outp->wI64(outl,0); // four zero 16-bit integers
  98. }
  99. if (outl > sizeof(Protocol::Header)) {
  100. outl = Protocol::compress(outp,outl);
  101. }
  102. }
  103. _lastPushedCredentials = now;
  104. }
  105. void Membership::clean(const int64_t now,const NetworkConfig &nconf)
  106. {
  107. _cleanCredImpl<Tag>(nconf,_remoteTags);
  108. _cleanCredImpl<Capability>(nconf,_remoteCaps);
  109. _cleanCredImpl<CertificateOfOwnership>(nconf,_remoteCoos);
  110. }
  111. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const CertificateOfMembership &com)
  112. {
  113. const int64_t newts = com.timestamp();
  114. if (newts <= _comRevocationThreshold) {
  115. RR->t->credentialRejected(tPtr,0xd9992121,com.networkId(),sourcePeerIdentity.address(),com.id(),com.timestamp(),ZT_CREDENTIAL_TYPE_COM,ZT_TRACE_CREDENTIAL_REJECTION_REASON_REVOKED);
  116. return ADD_REJECTED;
  117. }
  118. const int64_t oldts = _com.timestamp();
  119. if (newts < oldts) {
  120. RR->t->credentialRejected(tPtr,0xd9928192,com.networkId(),sourcePeerIdentity.address(),com.id(),com.timestamp(),ZT_CREDENTIAL_TYPE_COM,ZT_TRACE_CREDENTIAL_REJECTION_REASON_OLDER_THAN_LATEST);
  121. return ADD_REJECTED;
  122. }
  123. if ((newts == oldts)&&(_com == com))
  124. return ADD_ACCEPTED_REDUNDANT;
  125. switch(com.verify(RR,tPtr)) {
  126. default:
  127. RR->t->credentialRejected(tPtr,0x0f198241,com.networkId(),sourcePeerIdentity.address(),com.id(),com.timestamp(),ZT_CREDENTIAL_TYPE_COM,ZT_TRACE_CREDENTIAL_REJECTION_REASON_INVALID);
  128. return Membership::ADD_REJECTED;
  129. case Credential::VERIFY_OK:
  130. _com = com;
  131. return ADD_ACCEPTED_NEW;
  132. case Credential::VERIFY_BAD_SIGNATURE:
  133. RR->t->credentialRejected(tPtr,0xbaf0aaaa,com.networkId(),sourcePeerIdentity.address(),com.id(),com.timestamp(),ZT_CREDENTIAL_TYPE_COM,ZT_TRACE_CREDENTIAL_REJECTION_REASON_SIGNATURE_VERIFICATION_FAILED);
  134. return ADD_REJECTED;
  135. case Credential::VERIFY_NEED_IDENTITY:
  136. return ADD_DEFERRED_FOR_WHOIS;
  137. }
  138. }
  139. // 3/5 of the credential types have identical addCredential() code
  140. template<typename C>
  141. static ZT_ALWAYS_INLINE Membership::AddCredentialResult _addCredImpl(
  142. Hashtable<uint32_t,C> &remoteCreds,
  143. const Hashtable<uint64_t,int64_t> &revocations,
  144. const RuntimeEnvironment *const RR,
  145. void *const tPtr,
  146. const Identity &sourcePeerIdentity,
  147. const NetworkConfig &nconf,
  148. const C &cred)
  149. {
  150. C *rc = remoteCreds.get(cred.id());
  151. if (rc) {
  152. if (rc->timestamp() > cred.timestamp()) {
  153. RR->t->credentialRejected(tPtr,0x40000001,nconf.networkId,sourcePeerIdentity.address(),cred.id(),cred.timestamp(),C::credentialType(),ZT_TRACE_CREDENTIAL_REJECTION_REASON_OLDER_THAN_LATEST);
  154. return Membership::ADD_REJECTED;
  155. }
  156. if (*rc == cred)
  157. return Membership::ADD_ACCEPTED_REDUNDANT;
  158. }
  159. const int64_t *const rt = revocations.get(Membership::credentialKey(C::credentialType(),cred.id()));
  160. if ((rt)&&(*rt >= cred.timestamp())) {
  161. RR->t->credentialRejected(tPtr,0x24248124,nconf.networkId,sourcePeerIdentity.address(),cred.id(),cred.timestamp(),C::credentialType(),ZT_TRACE_CREDENTIAL_REJECTION_REASON_REVOKED);
  162. return Membership::ADD_REJECTED;
  163. }
  164. switch(cred.verify(RR,tPtr)) {
  165. default:
  166. RR->t->credentialRejected(tPtr,0x01feba012,nconf.networkId,sourcePeerIdentity.address(),cred.id(),cred.timestamp(),C::credentialType(),ZT_TRACE_CREDENTIAL_REJECTION_REASON_INVALID);
  167. return Membership::ADD_REJECTED;
  168. case 0:
  169. if (!rc)
  170. rc = &(remoteCreds[cred.id()]);
  171. *rc = cred;
  172. return Membership::ADD_ACCEPTED_NEW;
  173. case 1:
  174. return Membership::ADD_DEFERRED_FOR_WHOIS;
  175. }
  176. }
  177. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const Tag &tag) { return _addCredImpl<Tag>(_remoteTags,_revocations,RR,tPtr,sourcePeerIdentity,nconf,tag); }
  178. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const Capability &cap) { return _addCredImpl<Capability>(_remoteCaps,_revocations,RR,tPtr,sourcePeerIdentity,nconf,cap); }
  179. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const CertificateOfOwnership &coo) { return _addCredImpl<CertificateOfOwnership>(_remoteCoos,_revocations,RR,tPtr,sourcePeerIdentity,nconf,coo); }
  180. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const Revocation &rev)
  181. {
  182. int64_t *rt;
  183. switch(rev.verify(RR,tPtr)) {
  184. default:
  185. RR->t->credentialRejected(tPtr,0x938fffff,nconf.networkId,sourcePeerIdentity.address(),rev.id(),0,ZT_CREDENTIAL_TYPE_REVOCATION,ZT_TRACE_CREDENTIAL_REJECTION_REASON_INVALID);
  186. return ADD_REJECTED;
  187. case 0: {
  188. const ZT_CredentialType ct = rev.typeBeingRevoked();
  189. switch(ct) {
  190. case ZT_CREDENTIAL_TYPE_COM:
  191. if (rev.threshold() > _comRevocationThreshold) {
  192. _comRevocationThreshold = rev.threshold();
  193. return ADD_ACCEPTED_NEW;
  194. }
  195. return ADD_ACCEPTED_REDUNDANT;
  196. case ZT_CREDENTIAL_TYPE_CAPABILITY:
  197. case ZT_CREDENTIAL_TYPE_TAG:
  198. case ZT_CREDENTIAL_TYPE_COO:
  199. rt = &(_revocations[credentialKey(ct,rev.credentialId())]);
  200. if (*rt < rev.threshold()) {
  201. *rt = rev.threshold();
  202. _comRevocationThreshold = rev.threshold();
  203. return ADD_ACCEPTED_NEW;
  204. }
  205. return ADD_ACCEPTED_REDUNDANT;
  206. default:
  207. RR->t->credentialRejected(tPtr,0x0bbbb1a4,nconf.networkId,sourcePeerIdentity.address(),rev.id(),0,ZT_CREDENTIAL_TYPE_REVOCATION,ZT_TRACE_CREDENTIAL_REJECTION_REASON_INVALID);
  208. return ADD_REJECTED;
  209. }
  210. }
  211. case 1:
  212. return ADD_DEFERRED_FOR_WHOIS;
  213. }
  214. }
  215. bool Membership::_isUnspoofableAddress(const NetworkConfig &nconf,const InetAddress &ip) const noexcept
  216. {
  217. if ((ip.isV6())&&(nconf.ndpEmulation())) {
  218. const InetAddress sixpl(InetAddress::makeIpv66plane(nconf.networkId,nconf.issuedTo.toInt()));
  219. for(unsigned int i=0;i<nconf.staticIpCount;++i) {
  220. if (nconf.staticIps[i].ipsEqual(sixpl)) {
  221. bool prefixMatches = true;
  222. for(unsigned int j=0;j<5;++j) { // check for match on /40
  223. if ((((const struct sockaddr_in6 *)&ip)->sin6_addr.s6_addr)[j] != (((const struct sockaddr_in6 *)&sixpl)->sin6_addr.s6_addr)[j]) {
  224. prefixMatches = false;
  225. break;
  226. }
  227. }
  228. if (prefixMatches)
  229. return true;
  230. break;
  231. }
  232. }
  233. const InetAddress rfc4193(InetAddress::makeIpv6rfc4193(nconf.networkId,nconf.issuedTo.toInt()));
  234. for(unsigned int i=0;i<nconf.staticIpCount;++i) {
  235. if (nconf.staticIps[i].ipsEqual(rfc4193)) {
  236. bool prefixMatches = true;
  237. for(unsigned int j=0;j<11;++j) { // check for match on /88
  238. if ((((const struct sockaddr_in6 *)&ip)->sin6_addr.s6_addr)[j] != (((const struct sockaddr_in6 *)&rfc4193)->sin6_addr.s6_addr)[j]) {
  239. prefixMatches = false;
  240. break;
  241. }
  242. }
  243. if (prefixMatches)
  244. return true;
  245. break;
  246. }
  247. }
  248. }
  249. return false;
  250. }
  251. } // namespace ZeroTier