Trace.hpp 5.6 KB

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