Membership.cpp 10 KB

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