Membership.cpp 10 KB

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