Trace.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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: 2023-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 MAC;
  36. class CertificateOfMembership;
  37. class CertificateOfOwnership;
  38. class Revocation;
  39. class Tag;
  40. class Capability;
  41. struct NetworkConfig;
  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. };
  58. /**
  59. * Filter rule evaluation result log
  60. *
  61. * Each rule in a rule set gets a four-bit log entry. A log entry
  62. * of zero means not evaluated. Otherwise each four-bit log entry
  63. * contains two two-bit values of 01 for 'false' and 10 for 'true'.
  64. * As with four-bit rules an 00 value here means this was not
  65. * evaluated or was not relevant.
  66. */
  67. class RuleResultLog
  68. {
  69. public:
  70. inline RuleResultLog() {}
  71. inline void log(const unsigned int rn,const uint8_t thisRuleMatches,const uint8_t thisSetMatches)
  72. {
  73. _l[rn >> 1] |= ( ((thisRuleMatches + 1) << 2) | (thisSetMatches + 1) ) << ((rn & 1) << 2);
  74. }
  75. inline void logSkipped(const unsigned int rn,const uint8_t thisSetMatches)
  76. {
  77. _l[rn >> 1] |= (thisSetMatches + 1) << ((rn & 1) << 2);
  78. }
  79. inline void clear()
  80. {
  81. memset(_l,0,sizeof(_l));
  82. }
  83. inline const uint8_t *data() const { return _l; }
  84. inline unsigned int sizeBytes() const { return (ZT_MAX_NETWORK_RULES / 2); }
  85. private:
  86. uint8_t _l[ZT_MAX_NETWORK_RULES / 2];
  87. };
  88. inline Trace(const RuntimeEnvironment *renv) :
  89. RR(renv),
  90. _byNet(8)
  91. {
  92. }
  93. void resettingPathsInScope(void *const tPtr,const Address &reporter,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,const InetAddress::IpScope scope);
  94. void peerConfirmingUnknownPath(void *const tPtr,const uint64_t networkId,Peer &peer,const SharedPtr<Path> &path,const uint64_t packetId,const Packet::Verb verb);
  95. void peerLinkNowRedundant(void *const tPtr,Peer &peer);
  96. void peerLinkNoLongerRedundant(void *const tPtr,Peer &peer);
  97. void peerLinkAggregateStatistics(void *const tPtr,Peer &peer);
  98. void peerLearnedNewPath(void *const tPtr,const uint64_t networkId,Peer &peer,const SharedPtr<Path> &newPath,const uint64_t packetId);
  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. Hashtable< uint64_t,std::pair< Address,Trace::Level > > _byNet;
  134. Mutex _byNet_m;
  135. };
  136. } // namespace ZeroTier
  137. #endif