Membership.cpp 10 KB

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