Trace.hpp 5.6 KB

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