123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- /*
- * Copyright (c)2019 ZeroTier, Inc.
- *
- * Use of this software is governed by the Business Source License included
- * in the LICENSE.TXT file in the project's root directory.
- *
- * Change Date: 2023-01-01
- *
- * On the date above, in accordance with the Business Source License, use
- * of this software will be governed by version 2.0 of the Apache License.
- */
- /****/
- #ifndef ZT_NODE_HPP
- #define ZT_NODE_HPP
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <map>
- #include <vector>
- #include "Constants.hpp"
- #include "../include/ZeroTierOne.h"
- #include "RuntimeEnvironment.hpp"
- #include "InetAddress.hpp"
- #include "Mutex.hpp"
- #include "MAC.hpp"
- #include "Network.hpp"
- #include "Path.hpp"
- #include "Salsa20.hpp"
- #include "NetworkController.hpp"
- #include "Hashtable.hpp"
- // Bit mask for "expecting reply" hash
- #define ZT_EXPECTING_REPLIES_BUCKET_MASK1 255
- #define ZT_EXPECTING_REPLIES_BUCKET_MASK2 31
- namespace ZeroTier {
- class Locator;
- /**
- * Implementation of Node object as defined in CAPI
- *
- * The pointer returned by ZT_Node_new() is an instance of this class.
- */
- class Node : public NetworkController::Sender
- {
- public:
- Node(void *uPtr, void *tPtr, const struct ZT_Node_Callbacks *callbacks, int64_t now);
- virtual ~Node();
- // Get rid of alignment warnings on 32-bit Windows and possibly improve performance
- #ifdef __WINDOWS__
- void * operator new(size_t i) { return _mm_malloc(i,16); }
- void operator delete(void* p) { _mm_free(p); }
- #endif
- // Public API Functions ----------------------------------------------------
- ZT_ResultCode processWirePacket(
- void *tptr,
- int64_t now,
- int64_t localSocket,
- const struct sockaddr_storage *remoteAddress,
- const void *packetData,
- unsigned int packetLength,
- volatile int64_t *nextBackgroundTaskDeadline);
- ZT_ResultCode processVirtualNetworkFrame(
- void *tptr,
- int64_t now,
- uint64_t nwid,
- uint64_t sourceMac,
- uint64_t destMac,
- unsigned int etherType,
- unsigned int vlanId,
- const void *frameData,
- unsigned int frameLength,
- volatile int64_t *nextBackgroundTaskDeadline);
- ZT_ResultCode processBackgroundTasks(void *tPtr, int64_t now, volatile int64_t *nextBackgroundTaskDeadline);
- ZT_ResultCode join(uint64_t nwid,void *uptr,void *tptr);
- ZT_ResultCode leave(uint64_t nwid,void **uptr,void *tptr);
- ZT_ResultCode multicastSubscribe(void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
- ZT_ResultCode multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
- ZT_ResultCode addRoot(const char *identity);
- ZT_ResultCode removeRoot(const char *identity);
- uint64_t address() const;
- void status(ZT_NodeStatus *status) const;
- ZT_PeerList *peers() const;
- ZT_VirtualNetworkConfig *networkConfig(uint64_t nwid) const;
- ZT_VirtualNetworkList *networks() const;
- void setNetworkUserPtr(uint64_t nwid,void *ptr);
- void freeQueryResult(void *qr);
- void setInterfaceAddresses(const ZT_InterfaceAddress *addrs,unsigned int addrCount);
- int sendUserMessage(void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len);
- void setController(void *networkControllerInstance);
- // Internal functions ------------------------------------------------------
- ZT_ALWAYS_INLINE int64_t now() const { return _now; }
- ZT_ALWAYS_INLINE bool putPacket(void *tPtr,const int64_t localSocket,const InetAddress &addr,const void *data,unsigned int len,unsigned int ttl = 0)
- {
- return (_cb.wirePacketSendFunction(
- reinterpret_cast<ZT_Node *>(this),
- _uPtr,
- tPtr,
- localSocket,
- reinterpret_cast<const struct sockaddr_storage *>(&addr),
- data,
- len,
- ttl) == 0);
- }
- ZT_ALWAYS_INLINE void putFrame(void *tPtr,uint64_t nwid,void **nuptr,const MAC &source,const MAC &dest,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
- {
- _cb.virtualNetworkFrameFunction(
- reinterpret_cast<ZT_Node *>(this),
- _uPtr,
- tPtr,
- nwid,
- nuptr,
- source.toInt(),
- dest.toInt(),
- etherType,
- vlanId,
- data,
- len);
- }
- ZT_ALWAYS_INLINE SharedPtr<Network> network(uint64_t nwid) const
- {
- Mutex::Lock _l(_networks_m);
- const SharedPtr<Network> *n = _networks.get(nwid);
- if (n)
- return *n;
- return SharedPtr<Network>();
- }
- ZT_ALWAYS_INLINE std::vector<ZT_InterfaceAddress> directPaths() const
- {
- Mutex::Lock _l(_localInterfaceAddresses_m);
- return _localInterfaceAddresses;
- }
- ZT_ALWAYS_INLINE void postEvent(void *tPtr,ZT_Event ev,const void *md = (const void *)0) { _cb.eventCallback(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,ev,md); }
- ZT_ALWAYS_INLINE void configureVirtualNetworkPort(void *tPtr,uint64_t nwid,void **nuptr,ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nc) { _cb.virtualNetworkConfigFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,nwid,nuptr,op,nc); }
- ZT_ALWAYS_INLINE bool online() const { return _online; }
- ZT_ALWAYS_INLINE int stateObjectGet(void *const tPtr,ZT_StateObjectType type,const uint64_t id[2],void *const data,const unsigned int maxlen) { return _cb.stateGetFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,type,id,data,maxlen); }
- ZT_ALWAYS_INLINE void stateObjectPut(void *const tPtr,ZT_StateObjectType type,const uint64_t id[2],const void *const data,const unsigned int len) { _cb.statePutFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,type,id,data,(int)len); }
- ZT_ALWAYS_INLINE void stateObjectDelete(void *const tPtr,ZT_StateObjectType type,const uint64_t id[2]) { _cb.statePutFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,type,id,(const void *)0,-1); }
- bool shouldUsePathForZeroTierTraffic(void *tPtr,const Address &ztaddr,const int64_t localSocket,const InetAddress &remoteAddress);
- bool externalPathLookup(void *tPtr,const Identity &id,int family,InetAddress &addr);
- ZT_ResultCode setPhysicalPathConfiguration(const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig);
- ZT_ALWAYS_INLINE const Identity &identity() const { return _RR.identity; }
- /**
- * Register that we are expecting a reply to a packet ID
- *
- * This only uses the most significant bits of the packet ID, both to save space
- * and to avoid using the higher bits that can be modified during armor() to
- * mask against the packet send counter used for QoS detection.
- *
- * @param packetId Packet ID to expect reply to
- */
- ZT_ALWAYS_INLINE void expectReplyTo(const uint64_t packetId)
- {
- const unsigned long pid2 = (unsigned long)(packetId >> 32U);
- const unsigned long bucket = (unsigned long)(pid2 & ZT_EXPECTING_REPLIES_BUCKET_MASK1);
- _expectingRepliesTo[bucket][_expectingRepliesToBucketPtr[bucket]++ & ZT_EXPECTING_REPLIES_BUCKET_MASK2] = (uint32_t)pid2;
- }
- /**
- * Check whether a given packet ID is something we are expecting a reply to
- *
- * This only uses the most significant bits of the packet ID, both to save space
- * and to avoid using the higher bits that can be modified during armor() to
- * mask against the packet send counter used for QoS detection.
- *
- * @param packetId Packet ID to check
- * @return True if we're expecting a reply
- */
- ZT_ALWAYS_INLINE bool expectingReplyTo(const uint64_t packetId) const
- {
- const uint32_t pid2 = (uint32_t)(packetId >> 32);
- const unsigned long bucket = (unsigned long)(pid2 & ZT_EXPECTING_REPLIES_BUCKET_MASK1);
- for(unsigned long i=0;i<=ZT_EXPECTING_REPLIES_BUCKET_MASK2;++i) {
- if (_expectingRepliesTo[bucket][i] == pid2)
- return true;
- }
- return false;
- }
- /**
- * Check whether we should do potentially expensive identity verification (rate limit)
- *
- * @param now Current time
- * @param from Source address of packet
- * @return True if within rate limits
- */
- ZT_ALWAYS_INLINE bool rateGateIdentityVerification(const int64_t now,const InetAddress &from)
- {
- unsigned long iph = from.rateGateHash();
- if ((now - _lastIdentityVerification[iph]) >= ZT_IDENTITY_VALIDATION_SOURCE_RATE_LIMIT) {
- _lastIdentityVerification[iph] = now;
- return true;
- }
- return false;
- }
- virtual void ncSendConfig(uint64_t nwid,uint64_t requestPacketId,const Address &destination,const NetworkConfig &nc,bool sendLegacyFormatConfig);
- virtual void ncSendRevocation(const Address &destination,const Revocation &rev);
- virtual void ncSendError(uint64_t nwid,uint64_t requestPacketId,const Address &destination,NetworkController::ErrorCode errorCode);
- inline bool localControllerHasAuthorized(const int64_t now,const uint64_t nwid,const Address &addr) const
- {
- _localControllerAuthorizations_m.lock();
- const int64_t *const at = _localControllerAuthorizations.get(_LocalControllerAuth(nwid,addr));
- _localControllerAuthorizations_m.unlock();
- if (at)
- return ((now - *at) < (ZT_NETWORK_AUTOCONF_DELAY * 3));
- return false;
- }
- inline void setMultipathMode(uint8_t mode) { _multipathMode = mode; }
- inline uint8_t getMultipathMode() { return _multipathMode; }
- private:
- RuntimeEnvironment _RR;
- RuntimeEnvironment *RR;
- ZT_Node_Callbacks _cb;
- void *_uPtr; // _uptr (lower case) is reserved in Visual Studio :P
- // For tracking packet IDs to filter out OK/ERROR replies to packets we did not send
- uint8_t _expectingRepliesToBucketPtr[ZT_EXPECTING_REPLIES_BUCKET_MASK1 + 1];
- uint32_t _expectingRepliesTo[ZT_EXPECTING_REPLIES_BUCKET_MASK1 + 1][ZT_EXPECTING_REPLIES_BUCKET_MASK2 + 1];
- // Time of last identity verification indexed by InetAddress.rateGateHash() -- used in IncomingPacket::_doHELLO() via rateGateIdentityVerification()
- int64_t _lastIdentityVerification[16384];
- /* Map that remembers if we have recently sent a network config to someone
- * querying us as a controller. This is an optimization to allow network
- * controllers to know whether to treat things like multicast queries the
- * way authorized members would be treated without requiring an extra cert
- * validation. */
- struct _LocalControllerAuth
- {
- uint64_t nwid,address;
- ZT_ALWAYS_INLINE _LocalControllerAuth(const uint64_t nwid_,const Address &address_) : nwid(nwid_),address(address_.toInt()) {}
- ZT_ALWAYS_INLINE unsigned long hashCode() const { return (unsigned long)(nwid ^ address); }
- ZT_ALWAYS_INLINE bool operator==(const _LocalControllerAuth &a) const { return ((a.nwid == nwid)&&(a.address == address)); }
- ZT_ALWAYS_INLINE bool operator!=(const _LocalControllerAuth &a) const { return ((a.nwid != nwid)||(a.address != address)); }
- };
- Hashtable< _LocalControllerAuth,int64_t > _localControllerAuthorizations;
- Hashtable< uint64_t,SharedPtr<Network> > _networks;
- std::vector< ZT_InterfaceAddress > _localInterfaceAddresses;
- Mutex _localControllerAuthorizations_m;
- Mutex _networks_m;
- Mutex _localInterfaceAddresses_m;
- Mutex _backgroundTasksLock;
- uint8_t _multipathMode;
- volatile int64_t _now;
- int64_t _lastPing;
- int64_t _lastHousekeepingRun;
- int64_t _lastNetworkHousekeepingRun;
- int64_t _lastDynamicRootUpdate;
- bool _online;
- };
- } // namespace ZeroTier
- #endif
|