Membership.cpp 10 KB

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