Membership.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "Switch.hpp"
  19. #include "Node.hpp"
  20. namespace ZeroTier {
  21. Membership::Membership() :
  22. _lastUpdatedMulticast(0),
  23. _comRevocationThreshold(0),
  24. _lastPushedCredentials(0),
  25. _revocations(4),
  26. _remoteTags(4),
  27. _remoteCaps(4),
  28. _remoteCoos(4)
  29. {
  30. }
  31. void Membership::pushCredentials(const RuntimeEnvironment *RR,void *tPtr,const int64_t now,const Address &peerAddress,const NetworkConfig &nconf)
  32. {
  33. const Capability *sendCaps[ZT_MAX_NETWORK_CAPABILITIES];
  34. unsigned int sendCapCount = 0;
  35. for(unsigned int c=0;c<nconf.capabilityCount;++c)
  36. sendCaps[sendCapCount++] = &(nconf.capabilities[c]);
  37. const Tag *sendTags[ZT_MAX_NETWORK_TAGS];
  38. unsigned int sendTagCount = 0;
  39. for(unsigned int t=0;t<nconf.tagCount;++t)
  40. sendTags[sendTagCount++] = &(nconf.tags[t]);
  41. const CertificateOfOwnership *sendCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
  42. unsigned int sendCooCount = 0;
  43. for(unsigned int c=0;c<nconf.certificateOfOwnershipCount;++c)
  44. sendCoos[sendCooCount++] = &(nconf.certificatesOfOwnership[c]);
  45. unsigned int capPtr = 0;
  46. unsigned int tagPtr = 0;
  47. unsigned int cooPtr = 0;
  48. bool sendCom = (bool)(nconf.com);
  49. while ((capPtr < sendCapCount)||(tagPtr < sendTagCount)||(cooPtr < sendCooCount)||(sendCom)) {
  50. Packet outp(peerAddress,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  51. if (sendCom) {
  52. sendCom = false;
  53. nconf.com.serialize(outp);
  54. }
  55. outp.append((uint8_t)0x00);
  56. const unsigned int capCountAt = outp.size();
  57. outp.addSize(2);
  58. unsigned int thisPacketCapCount = 0;
  59. while ((capPtr < sendCapCount)&&((outp.size() + sizeof(Capability) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  60. sendCaps[capPtr++]->serialize(outp);
  61. ++thisPacketCapCount;
  62. }
  63. outp.setAt(capCountAt,(uint16_t)thisPacketCapCount);
  64. const unsigned int tagCountAt = outp.size();
  65. outp.addSize(2);
  66. unsigned int thisPacketTagCount = 0;
  67. while ((tagPtr < sendTagCount)&&((outp.size() + sizeof(Tag) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  68. sendTags[tagPtr++]->serialize(outp);
  69. ++thisPacketTagCount;
  70. }
  71. outp.setAt(tagCountAt,(uint16_t)thisPacketTagCount);
  72. // No revocations, these propagate differently
  73. outp.append((uint16_t)0);
  74. const unsigned int cooCountAt = outp.size();
  75. outp.addSize(2);
  76. unsigned int thisPacketCooCount = 0;
  77. while ((cooPtr < sendCooCount)&&((outp.size() + sizeof(CertificateOfOwnership) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
  78. sendCoos[cooPtr++]->serialize(outp);
  79. ++thisPacketCooCount;
  80. }
  81. outp.setAt(cooCountAt,(uint16_t)thisPacketCooCount);
  82. outp.compress();
  83. RR->sw->send(tPtr,outp,true);
  84. }
  85. _lastPushedCredentials = now;
  86. }
  87. void Membership::clean(const int64_t now,const NetworkConfig &nconf)
  88. {
  89. _cleanCredImpl<Tag>(nconf,_remoteTags);
  90. _cleanCredImpl<Capability>(nconf,_remoteCaps);
  91. _cleanCredImpl<CertificateOfOwnership>(nconf,_remoteCoos);
  92. }
  93. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const CertificateOfMembership &com)
  94. {
  95. const int64_t newts = com.timestamp();
  96. if (newts <= _comRevocationThreshold) {
  97. RR->t->credentialRejected(tPtr,com.networkId(),sourcePeerIdentity.address(),com.id(),com.timestamp(),ZT_CREDENTIAL_TYPE_COM,ZT_TRACE_CREDENTIAL_REJECTION_REASON_REVOKED);
  98. return ADD_REJECTED;
  99. }
  100. const int64_t oldts = _com.timestamp();
  101. if (newts < oldts) {
  102. RR->t->credentialRejected(tPtr,com.networkId(),sourcePeerIdentity.address(),com.id(),com.timestamp(),ZT_CREDENTIAL_TYPE_COM,ZT_TRACE_CREDENTIAL_REJECTION_REASON_OLDER_THAN_LATEST);
  103. return ADD_REJECTED;
  104. }
  105. if ((newts == oldts)&&(_com == com))
  106. return ADD_ACCEPTED_REDUNDANT;
  107. switch(com.verify(RR,tPtr)) {
  108. default:
  109. RR->t->credentialRejected(tPtr,com.networkId(),sourcePeerIdentity.address(),com.id(),com.timestamp(),ZT_CREDENTIAL_TYPE_COM,ZT_TRACE_CREDENTIAL_REJECTION_REASON_INVALID);
  110. return Membership::ADD_REJECTED;
  111. case Credential::VERIFY_OK:
  112. _com = com;
  113. return ADD_ACCEPTED_NEW;
  114. case Credential::VERIFY_BAD_SIGNATURE:
  115. RR->t->credentialRejected(tPtr,com.networkId(),sourcePeerIdentity.address(),com.id(),com.timestamp(),ZT_CREDENTIAL_TYPE_COM,ZT_TRACE_CREDENTIAL_REJECTION_REASON_SIGNATURE_VERIFICATION_FAILED);
  116. return ADD_REJECTED;
  117. case Credential::VERIFY_NEED_IDENTITY:
  118. return ADD_DEFERRED_FOR_WHOIS;
  119. }
  120. }
  121. // 3/5 of the credential types have identical addCredential() code
  122. template<typename C>
  123. static ZT_ALWAYS_INLINE Membership::AddCredentialResult _addCredImpl(
  124. Hashtable<uint32_t,C> &remoteCreds,
  125. const Hashtable<uint64_t,int64_t> &revocations,
  126. const RuntimeEnvironment *const RR,
  127. void *const tPtr,
  128. const Identity &sourcePeerIdentity,
  129. const NetworkConfig &nconf,
  130. const C &cred)
  131. {
  132. C *rc = remoteCreds.get(cred.id());
  133. if (rc) {
  134. if (rc->timestamp() > cred.timestamp()) {
  135. RR->t->credentialRejected(tPtr,nconf.networkId,sourcePeerIdentity.address(),cred.id(),cred.timestamp(),C::credentialType(),ZT_TRACE_CREDENTIAL_REJECTION_REASON_OLDER_THAN_LATEST);
  136. return Membership::ADD_REJECTED;
  137. }
  138. if (*rc == cred)
  139. return Membership::ADD_ACCEPTED_REDUNDANT;
  140. }
  141. const int64_t *const rt = revocations.get(Membership::credentialKey(C::credentialType(),cred.id()));
  142. if ((rt)&&(*rt >= cred.timestamp())) {
  143. RR->t->credentialRejected(tPtr,nconf.networkId,sourcePeerIdentity.address(),cred.id(),cred.timestamp(),C::credentialType(),ZT_TRACE_CREDENTIAL_REJECTION_REASON_REVOKED);
  144. return Membership::ADD_REJECTED;
  145. }
  146. switch(cred.verify(RR,tPtr)) {
  147. default:
  148. RR->t->credentialRejected(tPtr,nconf.networkId,sourcePeerIdentity.address(),cred.id(),cred.timestamp(),C::credentialType(),ZT_TRACE_CREDENTIAL_REJECTION_REASON_INVALID);
  149. return Membership::ADD_REJECTED;
  150. case 0:
  151. if (!rc)
  152. rc = &(remoteCreds[cred.id()]);
  153. *rc = cred;
  154. return Membership::ADD_ACCEPTED_NEW;
  155. case 1:
  156. return Membership::ADD_DEFERRED_FOR_WHOIS;
  157. }
  158. }
  159. 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); }
  160. 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); }
  161. 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); }
  162. Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const Revocation &rev)
  163. {
  164. int64_t *rt;
  165. switch(rev.verify(RR,tPtr)) {
  166. default:
  167. RR->t->credentialRejected(tPtr,nconf.networkId,sourcePeerIdentity.address(),rev.id(),0,ZT_CREDENTIAL_TYPE_REVOCATION,ZT_TRACE_CREDENTIAL_REJECTION_REASON_INVALID);
  168. return ADD_REJECTED;
  169. case 0: {
  170. const ZT_CredentialType ct = rev.typeBeingRevoked();
  171. switch(ct) {
  172. case ZT_CREDENTIAL_TYPE_COM:
  173. if (rev.threshold() > _comRevocationThreshold) {
  174. _comRevocationThreshold = rev.threshold();
  175. return ADD_ACCEPTED_NEW;
  176. }
  177. return ADD_ACCEPTED_REDUNDANT;
  178. case ZT_CREDENTIAL_TYPE_CAPABILITY:
  179. case ZT_CREDENTIAL_TYPE_TAG:
  180. case ZT_CREDENTIAL_TYPE_COO:
  181. rt = &(_revocations[credentialKey(ct,rev.credentialId())]);
  182. if (*rt < rev.threshold()) {
  183. *rt = rev.threshold();
  184. _comRevocationThreshold = rev.threshold();
  185. return ADD_ACCEPTED_NEW;
  186. }
  187. return ADD_ACCEPTED_REDUNDANT;
  188. default:
  189. RR->t->credentialRejected(tPtr,nconf.networkId,sourcePeerIdentity.address(),rev.id(),0,ZT_CREDENTIAL_TYPE_REVOCATION,ZT_TRACE_CREDENTIAL_REJECTION_REASON_INVALID);
  190. return ADD_REJECTED;
  191. }
  192. }
  193. case 1:
  194. return ADD_DEFERRED_FOR_WHOIS;
  195. }
  196. }
  197. bool Membership::_isUnspoofableAddress(const NetworkConfig &nconf,const InetAddress &ip) const
  198. {
  199. if ((ip.isV6())&&(nconf.ndpEmulation())) {
  200. const InetAddress sixpl(InetAddress::makeIpv66plane(nconf.networkId,nconf.issuedTo.toInt()));
  201. for(unsigned int i=0;i<nconf.staticIpCount;++i) {
  202. if (nconf.staticIps[i].ipsEqual(sixpl)) {
  203. bool prefixMatches = true;
  204. for(unsigned int j=0;j<5;++j) { // check for match on /40
  205. if ((((const struct sockaddr_in6 *)&ip)->sin6_addr.s6_addr)[j] != (((const struct sockaddr_in6 *)&sixpl)->sin6_addr.s6_addr)[j]) {
  206. prefixMatches = false;
  207. break;
  208. }
  209. }
  210. if (prefixMatches)
  211. return true;
  212. break;
  213. }
  214. }
  215. const InetAddress rfc4193(InetAddress::makeIpv6rfc4193(nconf.networkId,nconf.issuedTo.toInt()));
  216. for(unsigned int i=0;i<nconf.staticIpCount;++i) {
  217. if (nconf.staticIps[i].ipsEqual(rfc4193)) {
  218. bool prefixMatches = true;
  219. for(unsigned int j=0;j<11;++j) { // check for match on /88
  220. if ((((const struct sockaddr_in6 *)&ip)->sin6_addr.s6_addr)[j] != (((const struct sockaddr_in6 *)&rfc4193)->sin6_addr.s6_addr)[j]) {
  221. prefixMatches = false;
  222. break;
  223. }
  224. }
  225. if (prefixMatches)
  226. return true;
  227. break;
  228. }
  229. }
  230. }
  231. return false;
  232. }
  233. } // namespace ZeroTier