Membership.cpp 9.6 KB

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