Files

222 lines
7.1 KiB
C++
Raw Permalink Normal View History

2025-08-06 12:10:18 -04:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
2016-08-04 09:51:15 -07:00
*
2025-08-06 12:10:18 -04:00
* (c) ZeroTier, Inc.
* https://www.zerotier.com/
2016-08-04 09:51:15 -07:00
*/
#include "Membership.hpp"
2025-07-03 11:26:23 -04:00
#include "Constants.hpp"
2016-08-04 09:51:15 -07:00
#include "Node.hpp"
2025-07-03 11:26:23 -04:00
#include "Packet.hpp"
#include "Peer.hpp"
#include "RuntimeEnvironment.hpp"
#include "Switch.hpp"
#include "Topology.hpp"
#include "Trace.hpp"
2016-08-04 09:51:15 -07:00
2025-07-03 11:26:23 -04:00
#include <algorithm>
2016-08-04 09:51:15 -07:00
namespace ZeroTier {
2025-07-03 11:26:23 -04:00
Membership::Membership() : _lastUpdatedMulticast(0), _comRevocationThreshold(0), _lastPushedCredentials(0), _revocations(4), _remoteTags(4), _remoteCaps(4), _remoteCoos(4)
2016-08-04 09:51:15 -07:00
{
}
2025-07-03 11:26:23 -04:00
void Membership::pushCredentials(const RuntimeEnvironment* RR, void* tPtr, const int64_t now, const Address& peerAddress, const NetworkConfig& nconf)
{
2025-07-03 11:26:23 -04:00
const Capability* sendCaps[ZT_MAX_NETWORK_CAPABILITIES];
unsigned int sendCapCount = 0;
2025-07-03 11:26:23 -04:00
for (unsigned int c = 0; c < nconf.capabilityCount; ++c) {
sendCaps[sendCapCount++] = &(nconf.capabilities[c]);
2023-05-01 14:48:16 -04:00
}
2025-07-03 11:26:23 -04:00
const Tag* sendTags[ZT_MAX_NETWORK_TAGS];
2017-02-06 17:20:22 -08:00
unsigned int sendTagCount = 0;
2025-07-03 11:26:23 -04:00
for (unsigned int t = 0; t < nconf.tagCount; ++t) {
2019-03-19 16:43:43 -07:00
sendTags[sendTagCount++] = &(nconf.tags[t]);
2023-05-01 14:48:16 -04:00
}
2017-02-06 17:20:22 -08:00
2025-07-03 11:26:23 -04:00
const CertificateOfOwnership* sendCoos[ZT_MAX_CERTIFICATES_OF_OWNERSHIP];
unsigned int sendCooCount = 0;
2025-07-03 11:26:23 -04:00
for (unsigned int c = 0; c < nconf.certificateOfOwnershipCount; ++c) {
2019-03-19 16:43:43 -07:00
sendCoos[sendCooCount++] = &(nconf.certificatesOfOwnership[c]);
2023-05-01 14:48:16 -04:00
}
unsigned int capPtr = 0;
unsigned int tagPtr = 0;
unsigned int cooPtr = 0;
2019-03-19 16:43:43 -07:00
bool sendCom = (bool)(nconf.com);
2025-07-03 11:26:23 -04:00
while ((capPtr < sendCapCount) || (tagPtr < sendTagCount) || (cooPtr < sendCooCount) || (sendCom)) {
Packet outp(peerAddress, RR->identity.address(), Packet::VERB_NETWORK_CREDENTIALS);
if (sendCom) {
sendCom = false;
nconf.com.serialize(outp);
}
outp.append((uint8_t)0x00);
const unsigned int capCountAt = outp.size();
outp.addSize(2);
unsigned int thisPacketCapCount = 0;
2025-07-03 11:26:23 -04:00
while ((capPtr < sendCapCount) && ((outp.size() + sizeof(Capability) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
sendCaps[capPtr++]->serialize(outp);
++thisPacketCapCount;
}
2025-07-03 11:26:23 -04:00
outp.setAt(capCountAt, (uint16_t)thisPacketCapCount);
const unsigned int tagCountAt = outp.size();
outp.addSize(2);
unsigned int thisPacketTagCount = 0;
2025-07-03 11:26:23 -04:00
while ((tagPtr < sendTagCount) && ((outp.size() + sizeof(Tag) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
2017-02-07 14:06:40 -08:00
sendTags[tagPtr++]->serialize(outp);
++thisPacketTagCount;
}
2025-07-03 11:26:23 -04:00
outp.setAt(tagCountAt, (uint16_t)thisPacketTagCount);
// No revocations, these propagate differently
outp.append((uint16_t)0);
2016-08-04 09:51:15 -07:00
const unsigned int cooCountAt = outp.size();
outp.addSize(2);
unsigned int thisPacketCooCount = 0;
2025-07-03 11:26:23 -04:00
while ((cooPtr < sendCooCount) && ((outp.size() + sizeof(CertificateOfOwnership) + 16) < ZT_PROTO_MAX_PACKET_LENGTH)) {
sendCoos[cooPtr++]->serialize(outp);
++thisPacketCooCount;
}
2025-07-03 11:26:23 -04:00
outp.setAt(cooCountAt, (uint16_t)thisPacketCooCount);
outp.compress();
RR->sw->send(tPtr, outp, true, nconf.networkId, ZT_QOS_NO_FLOW);
2023-05-02 11:16:55 -07:00
Metrics::pkt_network_credentials_out++;
2016-08-04 09:51:15 -07:00
}
_lastPushedCredentials = now;
2016-08-04 09:51:15 -07:00
}
2025-07-03 11:26:23 -04:00
Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment* RR, void* tPtr, const NetworkConfig& nconf, const CertificateOfMembership& com)
{
2017-10-02 15:52:57 -07:00
const int64_t newts = com.timestamp();
if (newts <= _comRevocationThreshold) {
2025-07-03 11:26:23 -04:00
RR->t->credentialRejected(tPtr, com, "revoked");
return ADD_REJECTED;
2016-08-24 15:45:37 -07:00
}
2017-10-02 15:52:57 -07:00
const int64_t oldts = _com.timestamp();
if (newts < oldts) {
2025-07-03 11:26:23 -04:00
RR->t->credentialRejected(tPtr, com, "old");
return ADD_REJECTED;
}
2023-05-01 14:48:16 -04:00
if (_com == com) {
return ADD_ACCEPTED_REDUNDANT;
2023-05-01 14:48:16 -04:00
}
2025-07-03 11:26:23 -04:00
switch (com.verify(RR, tPtr)) {
default:
2025-07-03 11:26:23 -04:00
RR->t->credentialRejected(tPtr, com, "invalid");
return ADD_REJECTED;
case 0:
2025-07-03 11:26:23 -04:00
// printf("%.16llx %.10llx replacing COM %lld with %lld\n", com.networkId(), com.issuedTo().toInt(), _com.timestamp(), com.timestamp()); fflush(stdout);
2016-08-24 15:45:37 -07:00
_com = com;
return ADD_ACCEPTED_NEW;
case 1:
return ADD_DEFERRED_FOR_WHOIS;
2016-08-24 15:45:37 -07:00
}
2016-08-04 09:51:15 -07:00
}
// Template out addCredential() for many cred types to avoid copypasta
2025-07-03 11:26:23 -04:00
template <typename C>
static Membership::AddCredentialResult _addCredImpl(Hashtable<uint32_t, C>& remoteCreds, const Hashtable<uint64_t, int64_t>& revocations, const RuntimeEnvironment* RR, void* tPtr, const NetworkConfig& nconf, const C& cred)
2016-08-04 09:51:15 -07:00
{
2025-07-03 11:26:23 -04:00
C* rc = remoteCreds.get(cred.id());
2017-04-04 06:47:01 -07:00
if (rc) {
if (rc->timestamp() > cred.timestamp()) {
2025-07-03 11:26:23 -04:00
RR->t->credentialRejected(tPtr, cred, "old");
2017-04-04 06:47:01 -07:00
return Membership::ADD_REJECTED;
}
2023-05-01 14:48:16 -04:00
if (*rc == cred) {
2017-04-04 06:47:01 -07:00
return Membership::ADD_ACCEPTED_REDUNDANT;
2023-05-01 14:48:16 -04:00
}
2016-08-24 15:45:37 -07:00
}
2025-07-03 11:26:23 -04:00
const int64_t* const rt = revocations.get(Membership::credentialKey(C::credentialType(), cred.id()));
if ((rt) && (*rt >= cred.timestamp())) {
RR->t->credentialRejected(tPtr, cred, "revoked");
2017-04-04 06:47:01 -07:00
return Membership::ADD_REJECTED;
2016-08-04 09:51:15 -07:00
}
2017-04-04 06:47:01 -07:00
2025-07-03 11:26:23 -04:00
switch (cred.verify(RR, tPtr)) {
2017-04-04 06:47:01 -07:00
default:
2025-07-03 11:26:23 -04:00
RR->t->credentialRejected(tPtr, cred, "invalid");
2017-04-04 06:47:01 -07:00
return Membership::ADD_REJECTED;
case 0:
2025-07-03 11:26:23 -04:00
if (! rc) {
2017-04-04 06:47:01 -07:00
rc = &(remoteCreds[cred.id()]);
2023-05-01 14:48:16 -04:00
}
2017-04-04 06:47:01 -07:00
*rc = cred;
return Membership::ADD_ACCEPTED_NEW;
case 1:
return Membership::ADD_DEFERRED_FOR_WHOIS;
}
}
2025-07-03 11:26:23 -04:00
Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment* RR, void* tPtr, const NetworkConfig& nconf, const Tag& tag)
{
2025-07-03 11:26:23 -04:00
return _addCredImpl<Tag>(_remoteTags, _revocations, RR, tPtr, nconf, tag);
}
Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment* RR, void* tPtr, const NetworkConfig& nconf, const Capability& cap)
{
return _addCredImpl<Capability>(_remoteCaps, _revocations, RR, tPtr, nconf, cap);
}
Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment* RR, void* tPtr, const NetworkConfig& nconf, const CertificateOfOwnership& coo)
{
return _addCredImpl<CertificateOfOwnership>(_remoteCoos, _revocations, RR, tPtr, nconf, coo);
}
Membership::AddCredentialResult Membership::addCredential(const RuntimeEnvironment* RR, void* tPtr, const NetworkConfig& nconf, const Revocation& rev)
{
int64_t* rt;
switch (rev.verify(RR, tPtr)) {
default:
2025-07-03 11:26:23 -04:00
RR->t->credentialRejected(tPtr, rev, "invalid");
return ADD_REJECTED;
case 0: {
2017-04-04 06:47:01 -07:00
const Credential::Type ct = rev.type();
2025-07-03 11:26:23 -04:00
switch (ct) {
2017-04-04 06:47:01 -07:00
case Credential::CREDENTIAL_TYPE_COM:
if (rev.threshold() > _comRevocationThreshold) {
_comRevocationThreshold = rev.threshold();
return ADD_ACCEPTED_NEW;
}
return ADD_ACCEPTED_REDUNDANT;
case Credential::CREDENTIAL_TYPE_CAPABILITY:
case Credential::CREDENTIAL_TYPE_TAG:
case Credential::CREDENTIAL_TYPE_COO:
2025-07-03 11:26:23 -04:00
rt = &(_revocations[credentialKey(ct, rev.credentialId())]);
2017-04-04 06:47:01 -07:00
if (*rt < rev.threshold()) {
*rt = rev.threshold();
_comRevocationThreshold = rev.threshold();
2017-04-04 06:47:01 -07:00
return ADD_ACCEPTED_NEW;
}
return ADD_ACCEPTED_REDUNDANT;
default:
2025-07-03 11:26:23 -04:00
RR->t->credentialRejected(tPtr, rev, "invalid");
return ADD_REJECTED;
}
}
case 1:
return ADD_DEFERRED_FOR_WHOIS;
}
}
2025-07-03 11:26:23 -04:00
void Membership::clean(const int64_t now, const NetworkConfig& nconf)
{
2025-07-03 11:26:23 -04:00
_cleanCredImpl<Tag>(nconf, _remoteTags);
_cleanCredImpl<Capability>(nconf, _remoteCaps);
_cleanCredImpl<CertificateOfOwnership>(nconf, _remoteCoos);
}
2025-07-03 11:26:23 -04:00
} // namespace ZeroTier