Trace.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c)2019 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: 2026-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. #ifndef ZT_TRACE_HPP
  14. #define ZT_TRACE_HPP
  15. #include "../include/ZeroTierOne.h"
  16. #include "Constants.hpp"
  17. #include "Credential.hpp"
  18. #include "Dictionary.hpp"
  19. #include "Hashtable.hpp"
  20. #include "InetAddress.hpp"
  21. #include "Mutex.hpp"
  22. #include "Packet.hpp"
  23. #include "SharedPtr.hpp"
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. namespace ZeroTier {
  29. class RuntimeEnvironment;
  30. class Address;
  31. class Identity;
  32. class Peer;
  33. class Path;
  34. class Network;
  35. class NetworkConfig;
  36. class MAC;
  37. class CertificateOfMembership;
  38. class CertificateOfOwnership;
  39. class Revocation;
  40. class Tag;
  41. class Capability;
  42. /**
  43. * Remote tracing and trace logging handler
  44. */
  45. class Trace {
  46. public:
  47. /**
  48. * Trace verbosity level
  49. */
  50. enum Level { LEVEL_NORMAL = 0, LEVEL_VERBOSE = 10, LEVEL_RULES = 15, LEVEL_DEBUG = 20, LEVEL_INSANE = 30 };
  51. /**
  52. * Filter rule evaluation result log
  53. *
  54. * Each rule in a rule set gets a four-bit log entry. A log entry
  55. * of zero means not evaluated. Otherwise each four-bit log entry
  56. * contains two two-bit values of 01 for 'false' and 10 for 'true'.
  57. * As with four-bit rules an 00 value here means this was not
  58. * evaluated or was not relevant.
  59. */
  60. class RuleResultLog {
  61. public:
  62. RuleResultLog()
  63. {
  64. }
  65. inline void log(const unsigned int rn, const uint8_t thisRuleMatches, const uint8_t thisSetMatches)
  66. {
  67. _l[rn >> 1] |= (((thisRuleMatches + 1) << 2) | (thisSetMatches + 1)) << ((rn & 1) << 2);
  68. }
  69. inline void logSkipped(const unsigned int rn, const uint8_t thisSetMatches)
  70. {
  71. _l[rn >> 1] |= (thisSetMatches + 1) << ((rn & 1) << 2);
  72. }
  73. inline void clear()
  74. {
  75. memset(_l, 0, sizeof(_l));
  76. }
  77. inline const uint8_t* data() const
  78. {
  79. return _l;
  80. }
  81. inline unsigned int sizeBytes() const
  82. {
  83. return (ZT_MAX_NETWORK_RULES / 2);
  84. }
  85. private:
  86. uint8_t _l[ZT_MAX_NETWORK_RULES / 2];
  87. };
  88. Trace(const RuntimeEnvironment* renv) : RR(renv), _byNet(8)
  89. {
  90. }
  91. void resettingPathsInScope(void* const tPtr, const Address& reporter, const InetAddress& reporterPhysicalAddress, const InetAddress& myPhysicalAddress, const InetAddress::IpScope scope);
  92. void peerConfirmingUnknownPath(void* const tPtr, const uint64_t networkId, Peer& peer, const SharedPtr<Path>& path, const uint64_t packetId, const Packet::Verb verb);
  93. void bondStateMessage(void* const tPtr, char* msg);
  94. void peerLearnedNewPath(void* const tPtr, const uint64_t networkId, Peer& peer, const SharedPtr<Path>& newPath, const uint64_t packetId);
  95. void peerRedirected(void* const tPtr, const uint64_t networkId, Peer& peer, const SharedPtr<Path>& newPath);
  96. void incomingPacketMessageAuthenticationFailure(void* const tPtr, const SharedPtr<Path>& path, const uint64_t packetId, const Address& source, const unsigned int hops, const char* reason);
  97. void incomingPacketInvalid(void* const tPtr, const SharedPtr<Path>& path, const uint64_t packetId, const Address& source, const unsigned int hops, const Packet::Verb verb, const char* reason);
  98. void incomingPacketDroppedHELLO(void* const tPtr, const SharedPtr<Path>& path, const uint64_t packetId, const Address& source, const char* reason);
  99. void outgoingNetworkFrameDropped(void* const tPtr, const SharedPtr<Network>& network, const MAC& sourceMac, const MAC& destMac, const unsigned int etherType, const unsigned int vlanId, const unsigned int frameLen, const char* reason);
  100. void incomingNetworkAccessDenied(
  101. void* const tPtr,
  102. const SharedPtr<Network>& network,
  103. const SharedPtr<Path>& path,
  104. const uint64_t packetId,
  105. const unsigned int packetLength,
  106. const Address& source,
  107. const Packet::Verb verb,
  108. bool credentialsRequested);
  109. void incomingNetworkFrameDropped(
  110. void* const tPtr,
  111. const SharedPtr<Network>& network,
  112. const SharedPtr<Path>& path,
  113. const uint64_t packetId,
  114. const unsigned int packetLength,
  115. const Address& source,
  116. const Packet::Verb verb,
  117. const MAC& sourceMac,
  118. const MAC& destMac,
  119. const char* reason);
  120. void networkConfigRequestSent(void* const tPtr, const Network& network, const Address& controller);
  121. void networkFilter(
  122. void* const tPtr,
  123. const Network& network,
  124. const RuleResultLog& primaryRuleSetLog,
  125. const RuleResultLog* const matchingCapabilityRuleSetLog,
  126. const Capability* const matchingCapability,
  127. const Address& ztSource,
  128. const Address& ztDest,
  129. const MAC& macSource,
  130. const MAC& macDest,
  131. const uint8_t* const frameData,
  132. const unsigned int frameLen,
  133. const unsigned int etherType,
  134. const unsigned int vlanId,
  135. const bool noTee,
  136. const bool inbound,
  137. const int accept);
  138. void credentialRejected(void* const tPtr, const CertificateOfMembership& c, const char* reason);
  139. void credentialRejected(void* const tPtr, const CertificateOfOwnership& c, const char* reason);
  140. void credentialRejected(void* const tPtr, const Capability& c, const char* reason);
  141. void credentialRejected(void* const tPtr, const Tag& c, const char* reason);
  142. void credentialRejected(void* const tPtr, const Revocation& c, const char* reason);
  143. void updateMemoizedSettings();
  144. private:
  145. const RuntimeEnvironment* const RR;
  146. void _send(void* const tPtr, const Dictionary<ZT_MAX_REMOTE_TRACE_SIZE>& d, const Address& dest);
  147. void _spamToAllNetworks(void* const tPtr, const Dictionary<ZT_MAX_REMOTE_TRACE_SIZE>& d, const Level level);
  148. Address _globalTarget;
  149. Trace::Level _globalLevel;
  150. Hashtable<uint64_t, std::pair<Address, Trace::Level> > _byNet;
  151. Mutex _byNet_m;
  152. };
  153. } // namespace ZeroTier
  154. #endif