Trace.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_TRACE_HPP
  27. #define ZT_TRACE_HPP
  28. #include <stdio.h>
  29. #include <stdint.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include "../include/ZeroTierOne.h"
  33. #include "Constants.hpp"
  34. #include "SharedPtr.hpp"
  35. #include "Packet.hpp"
  36. #include "Credential.hpp"
  37. #include "InetAddress.hpp"
  38. #include "Dictionary.hpp"
  39. #include "Mutex.hpp"
  40. #include "Hashtable.hpp"
  41. namespace ZeroTier {
  42. class RuntimeEnvironment;
  43. class Address;
  44. class Identity;
  45. class Peer;
  46. class Path;
  47. class Network;
  48. class NetworkConfig;
  49. class MAC;
  50. class CertificateOfMembership;
  51. class CertificateOfOwnership;
  52. class Revocation;
  53. class Tag;
  54. class Capability;
  55. /**
  56. * Remote tracing and trace logging handler
  57. */
  58. class Trace
  59. {
  60. public:
  61. /**
  62. * Trace verbosity level
  63. */
  64. enum Level
  65. {
  66. LEVEL_NORMAL = 0,
  67. LEVEL_VERBOSE = 10,
  68. LEVEL_RULES = 15,
  69. LEVEL_DEBUG = 20,
  70. LEVEL_INSANE = 30
  71. };
  72. /**
  73. * Filter rule evaluation result log
  74. *
  75. * Each rule in a rule set gets a four-bit log entry. A log entry
  76. * of zero means not evaluated. Otherwise each four-bit log entry
  77. * contains two two-bit values of 01 for 'false' and 10 for 'true'.
  78. * As with four-bit rules an 00 value here means this was not
  79. * evaluated or was not relevant.
  80. */
  81. class RuleResultLog
  82. {
  83. public:
  84. RuleResultLog() {}
  85. inline void log(const unsigned int rn,const uint8_t thisRuleMatches,const uint8_t thisSetMatches)
  86. {
  87. _l[rn >> 1] |= ( ((thisRuleMatches + 1) << 2) | (thisSetMatches + 1) ) << ((rn & 1) << 2);
  88. }
  89. inline void logSkipped(const unsigned int rn,const uint8_t thisSetMatches)
  90. {
  91. _l[rn >> 1] |= (thisSetMatches + 1) << ((rn & 1) << 2);
  92. }
  93. inline void clear()
  94. {
  95. memset(_l,0,sizeof(_l));
  96. }
  97. inline const uint8_t *data() const { return _l; }
  98. inline unsigned int sizeBytes() const { return (ZT_MAX_NETWORK_RULES / 2); }
  99. private:
  100. uint8_t _l[ZT_MAX_NETWORK_RULES / 2];
  101. };
  102. Trace(const RuntimeEnvironment *renv) :
  103. RR(renv),
  104. _byNet(8)
  105. {
  106. }
  107. void resettingPathsInScope(void *const tPtr,const Address &reporter,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,const InetAddress::IpScope scope);
  108. void peerConfirmingUnknownPath(void *const tPtr,const uint64_t networkId,Peer &peer,const SharedPtr<Path> &path,const uint64_t packetId,const Packet::Verb verb);
  109. void peerLinkNowRedundant(void *const tPtr,Peer &peer);
  110. void peerLinkNoLongerRedundant(void *const tPtr,Peer &peer);
  111. void peerLinkAggregateStatistics(void *const tPtr,Peer &peer);
  112. void peerLearnedNewPath(void *const tPtr,const uint64_t networkId,Peer &peer,const SharedPtr<Path> &newPath,const uint64_t packetId);
  113. void peerRedirected(void *const tPtr,const uint64_t networkId,Peer &peer,const SharedPtr<Path> &newPath);
  114. void incomingPacketMessageAuthenticationFailure(void *const tPtr,const SharedPtr<Path> &path,const uint64_t packetId,const Address &source,const unsigned int hops,const char *reason);
  115. 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);
  116. void incomingPacketDroppedHELLO(void *const tPtr,const SharedPtr<Path> &path,const uint64_t packetId,const Address &source,const char *reason);
  117. 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);
  118. 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);
  119. 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);
  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