mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-02-02 01:08:19 +00:00
Merge branch 'adamierymenko-dev' into android-jni
This commit is contained in:
commit
bdc5b3d3a5
@ -416,7 +416,7 @@ typedef struct
|
||||
uint64_t nwid;
|
||||
|
||||
/**
|
||||
* Ethernet MAC (40 bits) that should be assigned to port
|
||||
* Ethernet MAC (48 bits) that should be assigned to port
|
||||
*/
|
||||
uint64_t mac;
|
||||
|
||||
|
@ -273,7 +273,7 @@ bool IncomingPacket::_doHELLO(const RuntimeEnvironment *RR)
|
||||
trusted = true;
|
||||
}
|
||||
if (destAddr)
|
||||
RR->sa->iam(id.address(),_remoteAddress,destAddr,trusted);
|
||||
RR->sa->iam(id.address(),_remoteAddress,destAddr,trusted,RR->node->now());
|
||||
|
||||
Packet outp(id.address(),RR->identity.address(),Packet::VERB_OK);
|
||||
|
||||
@ -358,7 +358,7 @@ bool IncomingPacket::_doOK(const RuntimeEnvironment *RR,const SharedPtr<Peer> &p
|
||||
trusted = true;
|
||||
}
|
||||
if (destAddr)
|
||||
RR->sa->iam(peer->address(),_remoteAddress,destAddr,trusted);
|
||||
RR->sa->iam(peer->address(),_remoteAddress,destAddr,trusted,RR->node->now());
|
||||
} break;
|
||||
|
||||
case Packet::VERB_WHOIS: {
|
||||
|
@ -63,9 +63,6 @@ struct InetAddress : public sockaddr_storage
|
||||
|
||||
/**
|
||||
* IP address scope
|
||||
*
|
||||
* Do not change these numeric index values without taking a look
|
||||
* at SelfAwareness. Values 1-5 are mapped onto an array index.
|
||||
*/
|
||||
enum IpScope
|
||||
{
|
||||
|
@ -264,6 +264,7 @@ ZT1_ResultCode Node::processBackgroundTasks(uint64_t now,volatile uint64_t *next
|
||||
|
||||
try {
|
||||
RR->topology->clean(now);
|
||||
RR->sa->clean(now);
|
||||
} catch ( ... ) {
|
||||
return ZT1_RESULT_FATAL_ERROR_INTERNAL;
|
||||
}
|
||||
|
@ -71,4 +71,90 @@ const char *Packet::errorString(ErrorCode e)
|
||||
return "(unknown)";
|
||||
}
|
||||
|
||||
void Packet::armor(const void *key,bool encryptPayload)
|
||||
{
|
||||
unsigned char mangledKey[32];
|
||||
unsigned char macKey[32];
|
||||
unsigned char mac[16];
|
||||
const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
|
||||
unsigned char *const payload = field(ZT_PACKET_IDX_VERB,payloadLen);
|
||||
|
||||
// Set flag now, since it affects key mangle function
|
||||
setCipher(encryptPayload ? ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012 : ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE);
|
||||
|
||||
_salsa20MangleKey((const unsigned char *)key,mangledKey);
|
||||
Salsa20 s20(mangledKey,256,field(ZT_PACKET_IDX_IV,8),ZT_PROTO_SALSA20_ROUNDS);
|
||||
|
||||
// MAC key is always the first 32 bytes of the Salsa20 key stream
|
||||
// This is the same construction DJB's NaCl library uses
|
||||
s20.encrypt(ZERO_KEY,macKey,sizeof(macKey));
|
||||
|
||||
if (encryptPayload)
|
||||
s20.encrypt(payload,payload,payloadLen);
|
||||
|
||||
Poly1305::compute(mac,payload,payloadLen,macKey);
|
||||
memcpy(field(ZT_PACKET_IDX_MAC,8),mac,8);
|
||||
}
|
||||
|
||||
bool Packet::dearmor(const void *key)
|
||||
{
|
||||
unsigned char mangledKey[32];
|
||||
unsigned char macKey[32];
|
||||
unsigned char mac[16];
|
||||
const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
|
||||
unsigned char *const payload = field(ZT_PACKET_IDX_VERB,payloadLen);
|
||||
unsigned int cs = cipher();
|
||||
|
||||
if ((cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE)||(cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012)) {
|
||||
_salsa20MangleKey((const unsigned char *)key,mangledKey);
|
||||
Salsa20 s20(mangledKey,256,field(ZT_PACKET_IDX_IV,8),ZT_PROTO_SALSA20_ROUNDS);
|
||||
|
||||
s20.encrypt(ZERO_KEY,macKey,sizeof(macKey));
|
||||
Poly1305::compute(mac,payload,payloadLen,macKey);
|
||||
if (!Utils::secureEq(mac,field(ZT_PACKET_IDX_MAC,8),8))
|
||||
return false;
|
||||
|
||||
if (cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012)
|
||||
s20.decrypt(payload,payload,payloadLen);
|
||||
|
||||
return true;
|
||||
} else if (cs == ZT_PROTO_CIPHER_SUITE__C25519_AES256_GCM) {
|
||||
return false; // not implemented yet
|
||||
} else return false; // unrecognized cipher suite
|
||||
}
|
||||
|
||||
bool Packet::compress()
|
||||
{
|
||||
unsigned char buf[ZT_PROTO_MAX_PACKET_LENGTH * 2];
|
||||
if ((!compressed())&&(size() > (ZT_PACKET_IDX_PAYLOAD + 32))) {
|
||||
int pl = (int)(size() - ZT_PACKET_IDX_PAYLOAD);
|
||||
int cl = LZ4_compress((const char *)field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)pl),(char *)buf,pl);
|
||||
if ((cl > 0)&&(cl < pl)) {
|
||||
(*this)[ZT_PACKET_IDX_VERB] |= (char)ZT_PROTO_VERB_FLAG_COMPRESSED;
|
||||
setSize((unsigned int)cl + ZT_PACKET_IDX_PAYLOAD);
|
||||
memcpy(field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)cl),buf,cl);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
(*this)[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Packet::uncompress()
|
||||
{
|
||||
unsigned char buf[ZT_PROTO_MAX_PACKET_LENGTH];
|
||||
if ((compressed())&&(size() >= ZT_PROTO_MIN_PACKET_LENGTH)) {
|
||||
if (size() > ZT_PACKET_IDX_PAYLOAD) {
|
||||
unsigned int compLen = size() - ZT_PACKET_IDX_PAYLOAD;
|
||||
int ucl = LZ4_decompress_safe((const char *)field(ZT_PACKET_IDX_PAYLOAD,compLen),(char *)buf,compLen,sizeof(buf));
|
||||
if ((ucl > 0)&&(ucl <= (int)(capacity() - ZT_PACKET_IDX_PAYLOAD))) {
|
||||
setSize((unsigned int)ucl + ZT_PACKET_IDX_PAYLOAD);
|
||||
memcpy(field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)ucl),buf,ucl);
|
||||
} else return false;
|
||||
}
|
||||
(*this)[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
@ -1019,10 +1019,7 @@ public:
|
||||
/**
|
||||
* @return Raw packet payload
|
||||
*/
|
||||
inline const unsigned char *payload() const
|
||||
{
|
||||
return field(ZT_PACKET_IDX_PAYLOAD,size() - ZT_PACKET_IDX_PAYLOAD);
|
||||
}
|
||||
inline const unsigned char *payload() const { return field(ZT_PACKET_IDX_PAYLOAD,size() - ZT_PACKET_IDX_PAYLOAD); }
|
||||
|
||||
/**
|
||||
* Armor packet for transport
|
||||
@ -1030,30 +1027,7 @@ public:
|
||||
* @param key 32-byte key
|
||||
* @param encryptPayload If true, encrypt packet payload, else just MAC
|
||||
*/
|
||||
inline void armor(const void *key,bool encryptPayload)
|
||||
{
|
||||
unsigned char mangledKey[32];
|
||||
unsigned char macKey[32];
|
||||
unsigned char mac[16];
|
||||
const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
|
||||
unsigned char *const payload = field(ZT_PACKET_IDX_VERB,payloadLen);
|
||||
|
||||
// Set flag now, since it affects key mangle function
|
||||
setCipher(encryptPayload ? ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012 : ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE);
|
||||
|
||||
_salsa20MangleKey((const unsigned char *)key,mangledKey);
|
||||
Salsa20 s20(mangledKey,256,field(ZT_PACKET_IDX_IV,8),ZT_PROTO_SALSA20_ROUNDS);
|
||||
|
||||
// MAC key is always the first 32 bytes of the Salsa20 key stream
|
||||
// This is the same construction DJB's NaCl library uses
|
||||
s20.encrypt(ZERO_KEY,macKey,sizeof(macKey));
|
||||
|
||||
if (encryptPayload)
|
||||
s20.encrypt(payload,payload,payloadLen);
|
||||
|
||||
Poly1305::compute(mac,payload,payloadLen,macKey);
|
||||
memcpy(field(ZT_PACKET_IDX_MAC,8),mac,8);
|
||||
}
|
||||
void armor(const void *key,bool encryptPayload);
|
||||
|
||||
/**
|
||||
* Verify and (if encrypted) decrypt packet
|
||||
@ -1061,32 +1035,7 @@ public:
|
||||
* @param key 32-byte key
|
||||
* @return False if packet is invalid or failed MAC authenticity check
|
||||
*/
|
||||
inline bool dearmor(const void *key)
|
||||
{
|
||||
unsigned char mangledKey[32];
|
||||
unsigned char macKey[32];
|
||||
unsigned char mac[16];
|
||||
const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
|
||||
unsigned char *const payload = field(ZT_PACKET_IDX_VERB,payloadLen);
|
||||
unsigned int cs = cipher();
|
||||
|
||||
if ((cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE)||(cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012)) {
|
||||
_salsa20MangleKey((const unsigned char *)key,mangledKey);
|
||||
Salsa20 s20(mangledKey,256,field(ZT_PACKET_IDX_IV,8),ZT_PROTO_SALSA20_ROUNDS);
|
||||
|
||||
s20.encrypt(ZERO_KEY,macKey,sizeof(macKey));
|
||||
Poly1305::compute(mac,payload,payloadLen,macKey);
|
||||
if (!Utils::secureEq(mac,field(ZT_PACKET_IDX_MAC,8),8))
|
||||
return false;
|
||||
|
||||
if (cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012)
|
||||
s20.decrypt(payload,payload,payloadLen);
|
||||
|
||||
return true;
|
||||
} else if (cs == ZT_PROTO_CIPHER_SUITE__C25519_AES256_GCM) {
|
||||
return false; // not implemented yet
|
||||
} else return false; // unrecognized cipher suite
|
||||
}
|
||||
bool dearmor(const void *key);
|
||||
|
||||
/**
|
||||
* Attempt to compress payload if not already (must be unencrypted)
|
||||
@ -1098,22 +1047,7 @@ public:
|
||||
*
|
||||
* @return True if compression occurred
|
||||
*/
|
||||
inline bool compress()
|
||||
{
|
||||
unsigned char buf[ZT_PROTO_MAX_PACKET_LENGTH * 2];
|
||||
if ((!compressed())&&(size() > (ZT_PACKET_IDX_PAYLOAD + 32))) {
|
||||
int pl = (int)(size() - ZT_PACKET_IDX_PAYLOAD);
|
||||
int cl = LZ4_compress((const char *)field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)pl),(char *)buf,pl);
|
||||
if ((cl > 0)&&(cl < pl)) {
|
||||
(*this)[ZT_PACKET_IDX_VERB] |= (char)ZT_PROTO_VERB_FLAG_COMPRESSED;
|
||||
setSize((unsigned int)cl + ZT_PACKET_IDX_PAYLOAD);
|
||||
memcpy(field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)cl),buf,cl);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
(*this)[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
|
||||
return false;
|
||||
}
|
||||
bool compress();
|
||||
|
||||
/**
|
||||
* Attempt to decompress payload if it is compressed (must be unencrypted)
|
||||
@ -1123,22 +1057,7 @@ public:
|
||||
*
|
||||
* @return True if data is now decompressed and valid, false on error
|
||||
*/
|
||||
inline bool uncompress()
|
||||
{
|
||||
unsigned char buf[ZT_PROTO_MAX_PACKET_LENGTH];
|
||||
if ((compressed())&&(size() >= ZT_PROTO_MIN_PACKET_LENGTH)) {
|
||||
if (size() > ZT_PACKET_IDX_PAYLOAD) {
|
||||
unsigned int compLen = size() - ZT_PACKET_IDX_PAYLOAD;
|
||||
int ucl = LZ4_decompress_safe((const char *)field(ZT_PACKET_IDX_PAYLOAD,compLen),(char *)buf,compLen,sizeof(buf));
|
||||
if ((ucl > 0)&&(ucl <= (int)(capacity() - ZT_PACKET_IDX_PAYLOAD))) {
|
||||
setSize((unsigned int)ucl + ZT_PACKET_IDX_PAYLOAD);
|
||||
memcpy(field(ZT_PACKET_IDX_PAYLOAD,(unsigned int)ucl),buf,ucl);
|
||||
} else return false;
|
||||
}
|
||||
(*this)[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
bool uncompress();
|
||||
|
||||
private:
|
||||
static const unsigned char ZERO_KEY[32];
|
||||
|
@ -37,6 +37,9 @@
|
||||
#include "Packet.hpp"
|
||||
#include "Peer.hpp"
|
||||
|
||||
// Entry timeout -- make it fairly long since this is just to prevent stale buildup
|
||||
#define ZT_SELFAWARENESS_ENTRY_TIMEOUT 3600000
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
class _ResetWithinScope
|
||||
@ -64,35 +67,51 @@ private:
|
||||
SelfAwareness::SelfAwareness(const RuntimeEnvironment *renv) :
|
||||
RR(renv)
|
||||
{
|
||||
memset(_lastPhysicalAddress,0,sizeof(_lastPhysicalAddress));
|
||||
}
|
||||
|
||||
SelfAwareness::~SelfAwareness()
|
||||
{
|
||||
}
|
||||
|
||||
void SelfAwareness::iam(const Address &reporter,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted)
|
||||
void SelfAwareness::iam(const Address &reporter,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,uint64_t now)
|
||||
{
|
||||
// This code depends on the numeric values assigned to scopes in InetAddress.hpp
|
||||
const unsigned int scope = (unsigned int)myPhysicalAddress.ipScope();
|
||||
if ((scope > 0)&&(scope < (unsigned int)InetAddress::IP_SCOPE_LOOPBACK)) {
|
||||
if ( (!trusted) && ((scope == (unsigned int)InetAddress::IP_SCOPE_GLOBAL)||(scope != (unsigned int)reporterPhysicalAddress.ipScope())) ) {
|
||||
/* For now only trusted peers are permitted to inform us of changes to
|
||||
* our global Internet IP or to changes of NATed IPs. We'll let peers on
|
||||
* private, shared, or link-local networks inform us of changes as long
|
||||
* as they too are at the same scope. This discrimination avoids a DoS
|
||||
* attack in which an attacker could force us to reset our connections. */
|
||||
const InetAddress::IpScope scope = myPhysicalAddress.ipScope();
|
||||
|
||||
switch(scope) {
|
||||
case InetAddress::IP_SCOPE_NONE:
|
||||
case InetAddress::IP_SCOPE_LOOPBACK:
|
||||
case InetAddress::IP_SCOPE_MULTICAST:
|
||||
return;
|
||||
} else {
|
||||
Mutex::Lock _l(_lock);
|
||||
InetAddress &lastPhy = _lastPhysicalAddress[scope - 1];
|
||||
if (!lastPhy) {
|
||||
TRACE("learned physical address %s for scope %u from reporter %s(%s) (replaced <null>)",myPhysicalAddress.toString().c_str(),scope,reporter.toString().c_str(),reporterPhysicalAddress.toString().c_str());
|
||||
lastPhy = myPhysicalAddress;
|
||||
} else if (lastPhy != myPhysicalAddress) {
|
||||
TRACE("learned physical address %s for scope %u from reporter %s(%s) (replaced %s, resetting within scope)",myPhysicalAddress.toString().c_str(),scope,reporter.toString().c_str(),reporterPhysicalAddress.toString().c_str(),lastPhy.toString().c_str());
|
||||
lastPhy = myPhysicalAddress;
|
||||
uint64_t now = RR->node->now();
|
||||
case InetAddress::IP_SCOPE_GLOBAL:
|
||||
if ((!trusted)||(scope != reporterPhysicalAddress.ipScope()))
|
||||
return;
|
||||
break;
|
||||
default:
|
||||
if (scope != reporterPhysicalAddress.ipScope())
|
||||
return;
|
||||
break;
|
||||
}
|
||||
|
||||
Mutex::Lock _l(_phy_m);
|
||||
|
||||
PhySurfaceEntry &entry = _phy[PhySurfaceKey(reporter,scope)];
|
||||
|
||||
if ((now - entry.ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT) {
|
||||
entry.mySurface = myPhysicalAddress;
|
||||
entry.ts = now;
|
||||
TRACE("learned physical address %s for scope %u as seen from %s(%s) (replaced <null>)",myPhysicalAddress.toString().c_str(),(unsigned int)scope,reporter.toString().c_str(),reporterPhysicalAddress.toString().c_str());
|
||||
} else if (entry.mySurface != myPhysicalAddress) {
|
||||
entry.mySurface = myPhysicalAddress;
|
||||
entry.ts = now;
|
||||
TRACE("learned physical address %s for scope %u as seen from %s(%s) (replaced %s, resetting all in scope)",myPhysicalAddress.toString().c_str(),(unsigned int)scope,reporter.toString().c_str(),reporterPhysicalAddress.toString().c_str(),entry.mySurface.toString().c_str());
|
||||
|
||||
// Erase all entries (other than this one) for this scope to prevent thrashing
|
||||
// Note: we should probably not use 'entry' after this
|
||||
for(std::map< PhySurfaceKey,PhySurfaceEntry >::iterator p(_phy.begin());p!=_phy.end();) {
|
||||
if ((p->first.reporter != reporter)&&(p->first.scope == scope))
|
||||
_phy.erase(p++);
|
||||
else ++p;
|
||||
}
|
||||
|
||||
_ResetWithinScope rset(RR,now,(InetAddress::IpScope)scope);
|
||||
RR->topology->eachPeer<_ResetWithinScope &>(rset);
|
||||
@ -113,8 +132,18 @@ void SelfAwareness::iam(const Address &reporter,const InetAddress &reporterPhysi
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
entry.ts = now;
|
||||
}
|
||||
}
|
||||
|
||||
void SelfAwareness::clean(uint64_t now)
|
||||
{
|
||||
Mutex::Lock _l(_phy_m);
|
||||
for(std::map< PhySurfaceKey,PhySurfaceEntry >::iterator p(_phy.begin());p!=_phy.end();) {
|
||||
if ((now - p->second.ts) >= ZT_SELFAWARENESS_ENTRY_TIMEOUT)
|
||||
_phy.erase(p++);
|
||||
else ++p;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,6 +28,8 @@
|
||||
#ifndef ZT_SELFAWARENESS_HPP
|
||||
#define ZT_SELFAWARENESS_HPP
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "InetAddress.hpp"
|
||||
#include "Address.hpp"
|
||||
#include "Mutex.hpp"
|
||||
@ -52,13 +54,41 @@ public:
|
||||
* @param reporterPhysicalAddress Physical address that reporting peer seems to have
|
||||
* @param myPhysicalAddress Physical address that peer says we have
|
||||
* @param trusted True if this peer is trusted as an authority to inform us of external address changes
|
||||
* @param now Current time
|
||||
*/
|
||||
void iam(const Address &reporter,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted);
|
||||
void iam(const Address &reporter,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,uint64_t now);
|
||||
|
||||
/**
|
||||
* Clean up database periodically
|
||||
*
|
||||
* @param now Current time
|
||||
*/
|
||||
void clean(uint64_t now);
|
||||
|
||||
private:
|
||||
struct PhySurfaceKey
|
||||
{
|
||||
Address reporter;
|
||||
InetAddress::IpScope scope;
|
||||
|
||||
PhySurfaceKey() : reporter(),scope(InetAddress::IP_SCOPE_NONE) {}
|
||||
PhySurfaceKey(const Address &r,InetAddress::IpScope s) : reporter(r),scope(s) {}
|
||||
inline bool operator<(const PhySurfaceKey &k) const throw() { return ((reporter < k.reporter) ? true : ((reporter == k.reporter) ? ((int)scope < (int)k.scope) : false)); }
|
||||
inline bool operator==(const PhySurfaceKey &k) const throw() { return ((reporter == k.reporter)&&(scope == k.scope)); }
|
||||
};
|
||||
struct PhySurfaceEntry
|
||||
{
|
||||
InetAddress mySurface;
|
||||
uint64_t ts;
|
||||
|
||||
PhySurfaceEntry() : mySurface(),ts(0) {}
|
||||
PhySurfaceEntry(const InetAddress &a,const uint64_t t) : mySurface(a),ts(t) {}
|
||||
};
|
||||
|
||||
const RuntimeEnvironment *RR;
|
||||
Mutex _lock;
|
||||
InetAddress _lastPhysicalAddress[5]; // 5 == the number of address classes we care about, see InetAddress.hpp and SelfAwareness.cpp
|
||||
|
||||
std::map< PhySurfaceKey,PhySurfaceEntry > _phy;
|
||||
Mutex _phy_m;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
Loading…
x
Reference in New Issue
Block a user