Trace.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (c)2013-2020 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 "Constants.hpp"
  16. #include "SharedPtr.hpp"
  17. #include "Mutex.hpp"
  18. #include "InetAddress.hpp"
  19. #include "Address.hpp"
  20. #include "MAC.hpp"
  21. #include "Containers.hpp"
  22. #include "Utils.hpp"
  23. #define ZT_TRACE_F_VL1 0x01U
  24. #define ZT_TRACE_F_VL2 0x02U
  25. #define ZT_TRACE_F_VL2_FILTER 0x04U
  26. #define ZT_TRACE_F_VL2_MULTICAST 0x08U
  27. namespace ZeroTier {
  28. class RuntimeEnvironment;
  29. class Identity;
  30. class Peer;
  31. class Path;
  32. class Network;
  33. class MembershipCredential;
  34. class OwnershipCredential;
  35. class RevocationCredential;
  36. class TagCredential;
  37. class CapabilityCredential;
  38. struct NetworkConfig;
  39. /**
  40. * Remote tracing and trace logging handler
  41. *
  42. * These methods are called when things happen that may be of interested to
  43. * someone debugging ZeroTier or its virtual networks. The codeLocation parameter
  44. * is an arbitrary pseudo-random identifier of the form 0xNNNNNNNN that could be
  45. * easily found by searching the code base. This makes it easy to locate the
  46. * specific line where a trace originated without relying on brittle non-portable
  47. * things like source file and line number. The same identifier should be used
  48. * for the same 'place' in the code across versions. These could eventually be
  49. * turned into constants that are semi-official and stored in a database to
  50. * provide extra debug context.
  51. */
  52. class Trace
  53. {
  54. public:
  55. struct RuleResultLog : public TriviallyCopyable
  56. {
  57. uint8_t l[ZT_MAX_NETWORK_RULES / 2]; // ZT_MAX_NETWORK_RULES 4-bit fields
  58. ZT_INLINE void log(const unsigned int rn,const uint8_t thisRuleMatches,const uint8_t thisSetMatches) noexcept
  59. {
  60. l[rn >> 1U] |= ( ((thisRuleMatches + 1U) << 2U) | (thisSetMatches + 1U) ) << ((rn & 1U) << 2U);
  61. }
  62. ZT_INLINE void logSkipped(const unsigned int rn,const uint8_t thisSetMatches) noexcept
  63. {
  64. l[rn >> 1U] |= (thisSetMatches + 1U) << ((rn & 1U) << 2U);
  65. }
  66. ZT_INLINE void clear() noexcept
  67. {
  68. memoryZero(this);
  69. }
  70. };
  71. explicit Trace(const RuntimeEnvironment *renv);
  72. void unexpectedError(
  73. void *tPtr,
  74. uint32_t codeLocation,
  75. const char *message,
  76. ...);
  77. ZT_INLINE void resettingPathsInScope(
  78. void *const tPtr,
  79. const uint32_t codeLocation,
  80. const Identity &reporter,
  81. const InetAddress &from,
  82. const InetAddress &oldExternal,
  83. const InetAddress &newExternal,
  84. const InetAddress::IpScope scope)
  85. {
  86. if ((_f & ZT_TRACE_F_VL1) != 0)
  87. _resettingPathsInScope(tPtr,codeLocation,reporter,from,oldExternal,newExternal,scope);
  88. }
  89. ZT_INLINE void tryingNewPath(
  90. void *const tPtr,
  91. const uint32_t codeLocation,
  92. const Identity &trying,
  93. const InetAddress &physicalAddress,
  94. const InetAddress &triggerAddress,
  95. uint64_t triggeringPacketId,
  96. uint8_t triggeringPacketVerb,
  97. const Identity &triggeringPeer)
  98. {
  99. if ((_f & ZT_TRACE_F_VL1) != 0)
  100. _tryingNewPath(tPtr,codeLocation,trying,physicalAddress,triggerAddress,triggeringPacketId,triggeringPacketVerb,triggeringPeer);
  101. }
  102. ZT_INLINE void learnedNewPath(
  103. void *const tPtr,
  104. const uint32_t codeLocation,
  105. uint64_t packetId,
  106. const Identity &peerIdentity,
  107. const InetAddress &physicalAddress,
  108. const InetAddress &replaced)
  109. {
  110. if ((_f & ZT_TRACE_F_VL1) != 0)
  111. _learnedNewPath(tPtr,codeLocation,packetId,peerIdentity,physicalAddress,replaced);
  112. }
  113. ZT_INLINE void incomingPacketDropped(
  114. void *const tPtr,
  115. const uint32_t codeLocation,
  116. uint64_t packetId,
  117. uint64_t networkId,
  118. const Identity &peerIdentity,
  119. const InetAddress &physicalAddress,
  120. uint8_t hops,
  121. uint8_t verb,
  122. const ZT_TracePacketDropReason reason)
  123. {
  124. if ((_f & ZT_TRACE_F_VL1) != 0)
  125. _incomingPacketDropped(tPtr,codeLocation,packetId,networkId,peerIdentity,physicalAddress,hops,verb,reason);
  126. }
  127. ZT_INLINE void outgoingNetworkFrameDropped(
  128. void *const tPtr,
  129. const uint32_t codeLocation,
  130. uint64_t networkId,
  131. const MAC &sourceMac,
  132. const MAC &destMac,
  133. uint16_t etherType,
  134. uint16_t frameLength,
  135. const uint8_t *frameData,
  136. ZT_TraceFrameDropReason reason)
  137. {
  138. if ((_f & ZT_TRACE_F_VL2) != 0)
  139. _outgoingNetworkFrameDropped(tPtr,codeLocation,networkId,sourceMac,destMac,etherType,frameLength,frameData,reason);
  140. }
  141. ZT_INLINE void incomingNetworkFrameDropped(
  142. void *const tPtr,
  143. const uint32_t codeLocation,
  144. uint64_t networkId,
  145. const MAC &sourceMac,
  146. const MAC &destMac,
  147. const Identity &peerIdentity,
  148. const InetAddress &physicalAddress,
  149. uint8_t hops,
  150. uint16_t frameLength,
  151. const uint8_t *frameData,
  152. uint8_t verb,
  153. bool credentialRequestSent,
  154. ZT_TraceFrameDropReason reason)
  155. {
  156. if ((_f & ZT_TRACE_F_VL2) != 0)
  157. _incomingNetworkFrameDropped(tPtr,codeLocation,networkId,sourceMac,destMac,peerIdentity,physicalAddress,hops,frameLength,frameData,verb,credentialRequestSent,reason);
  158. }
  159. ZT_INLINE void networkConfigRequestSent(
  160. void *const tPtr,
  161. const uint32_t codeLocation,
  162. uint64_t networkId)
  163. {
  164. if ((_f & ZT_TRACE_F_VL2) != 0)
  165. _networkConfigRequestSent(tPtr,codeLocation,networkId);
  166. }
  167. ZT_INLINE void networkFilter(
  168. void *const tPtr,
  169. const uint32_t codeLocation,
  170. uint64_t networkId,
  171. const uint8_t primaryRuleSetLog[512],
  172. const uint8_t matchingCapabilityRuleSetLog[512],
  173. uint32_t matchingCapabilityId,
  174. int64_t matchingCapabilityTimestamp,
  175. const Address &source,
  176. const Address &dest,
  177. const MAC &sourceMac,
  178. const MAC &destMac,
  179. uint16_t frameLength,
  180. const uint8_t *frameData,
  181. uint16_t etherType,
  182. uint16_t vlanId,
  183. bool noTee,
  184. bool inbound,
  185. int accept)
  186. {
  187. if ((_f & ZT_TRACE_F_VL2_FILTER) != 0) {
  188. _networkFilter(
  189. tPtr,
  190. codeLocation,
  191. networkId,
  192. primaryRuleSetLog,
  193. matchingCapabilityRuleSetLog,
  194. matchingCapabilityId,
  195. matchingCapabilityTimestamp,
  196. source,
  197. dest,
  198. sourceMac,
  199. destMac,
  200. frameLength,
  201. frameData,
  202. etherType,
  203. vlanId,
  204. noTee,
  205. inbound,
  206. accept);
  207. }
  208. }
  209. ZT_INLINE void credentialRejected(
  210. void *const tPtr,
  211. const uint32_t codeLocation,
  212. uint64_t networkId,
  213. const Identity &identity,
  214. uint32_t credentialId,
  215. int64_t credentialTimestamp,
  216. uint8_t credentialType,
  217. ZT_TraceCredentialRejectionReason reason)
  218. {
  219. if ((_f & ZT_TRACE_F_VL2) != 0)
  220. _credentialRejected(tPtr,codeLocation,networkId,identity,credentialId,credentialTimestamp,credentialType,reason);
  221. }
  222. private:
  223. void _resettingPathsInScope(
  224. void *tPtr,
  225. uint32_t codeLocation,
  226. const Identity &reporter,
  227. const InetAddress &from,
  228. const InetAddress &oldExternal,
  229. const InetAddress &newExternal,
  230. InetAddress::IpScope scope);
  231. void _tryingNewPath(
  232. void *tPtr,
  233. uint32_t codeLocation,
  234. const Identity &trying,
  235. const InetAddress &physicalAddress,
  236. const InetAddress &triggerAddress,
  237. uint64_t triggeringPacketId,
  238. uint8_t triggeringPacketVerb,
  239. const Identity &triggeringPeer);
  240. void _learnedNewPath(
  241. void *tPtr,
  242. uint32_t codeLocation,
  243. uint64_t packetId,
  244. const Identity &peerIdentity,
  245. const InetAddress &physicalAddress,
  246. const InetAddress &replaced);
  247. void _incomingPacketDropped(
  248. void *tPtr,
  249. uint32_t codeLocation,
  250. uint64_t packetId,
  251. uint64_t networkId,
  252. const Identity &peerIdentity,
  253. const InetAddress &physicalAddress,
  254. uint8_t hops,
  255. uint8_t verb,
  256. ZT_TracePacketDropReason reason);
  257. void _outgoingNetworkFrameDropped(
  258. void *tPtr,
  259. uint32_t codeLocation,
  260. uint64_t networkId,
  261. const MAC &sourceMac,
  262. const MAC &destMac,
  263. uint16_t etherType,
  264. uint16_t frameLength,
  265. const uint8_t *frameData,
  266. ZT_TraceFrameDropReason reason);
  267. void _incomingNetworkFrameDropped(
  268. void *tPtr,
  269. uint32_t codeLocation,
  270. uint64_t networkId,
  271. const MAC &sourceMac,
  272. const MAC &destMac,
  273. const Identity &peerIdentity,
  274. const InetAddress &physicalAddress,
  275. uint8_t hops,
  276. uint16_t frameLength,
  277. const uint8_t *frameData,
  278. uint8_t verb,
  279. bool credentialRequestSent,
  280. ZT_TraceFrameDropReason reason);
  281. void _networkConfigRequestSent(
  282. void *tPtr,
  283. uint32_t codeLocation,
  284. uint64_t networkId);
  285. void _networkFilter(
  286. void *tPtr,
  287. uint32_t codeLocation,
  288. uint64_t networkId,
  289. const uint8_t primaryRuleSetLog[512],
  290. const uint8_t matchingCapabilityRuleSetLog[512],
  291. uint32_t matchingCapabilityId,
  292. int64_t matchingCapabilityTimestamp,
  293. const Address &source,
  294. const Address &dest,
  295. const MAC &sourceMac,
  296. const MAC &destMac,
  297. uint16_t frameLength,
  298. const uint8_t *frameData,
  299. uint16_t etherType,
  300. uint16_t vlanId,
  301. bool noTee,
  302. bool inbound,
  303. int accept);
  304. void _credentialRejected(
  305. void *tPtr,
  306. uint32_t codeLocation,
  307. uint64_t networkId,
  308. const Identity &identity,
  309. uint32_t credentialId,
  310. int64_t credentialTimestamp,
  311. uint8_t credentialType,
  312. ZT_TraceCredentialRejectionReason reason);
  313. const RuntimeEnvironment *const RR;
  314. volatile unsigned int _f; // faster than atomic, but may not "instantly" change... will after next memory fence
  315. };
  316. } // namespace ZeroTier
  317. #endif