Trace.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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: 2024-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 <cstdint>
  16. #include <cstring>
  17. #include <cstdlib>
  18. #include <vector>
  19. #include "Constants.hpp"
  20. #include "SharedPtr.hpp"
  21. #include "Mutex.hpp"
  22. #include "Packet.hpp"
  23. #include "InetAddress.hpp"
  24. #include "Address.hpp"
  25. #include "MAC.hpp"
  26. namespace ZeroTier {
  27. class RuntimeEnvironment;
  28. class Identity;
  29. class Peer;
  30. class Path;
  31. class Network;
  32. class CertificateOfMembership;
  33. class CertificateOfOwnership;
  34. class Revocation;
  35. class Tag;
  36. class Capability;
  37. struct NetworkConfig;
  38. /**
  39. * Remote tracing and trace logging handler
  40. */
  41. class Trace
  42. {
  43. public:
  44. struct RuleResultLog
  45. {
  46. uint8_t l[ZT_MAX_NETWORK_RULES / 2]; // ZT_MAX_NETWORK_RULES 4-bit fields
  47. ZT_ALWAYS_INLINE void log(const unsigned int rn,const uint8_t thisRuleMatches,const uint8_t thisSetMatches)
  48. {
  49. l[rn >> 1U] |= ( ((thisRuleMatches + 1U) << 2U) | (thisSetMatches + 1U) ) << ((rn & 1U) << 2U);
  50. }
  51. ZT_ALWAYS_INLINE void logSkipped(const unsigned int rn,const uint8_t thisSetMatches)
  52. {
  53. l[rn >> 1U] |= (thisSetMatches + 1U) << ((rn & 1U) << 2U);
  54. }
  55. ZT_ALWAYS_INLINE void clear()
  56. {
  57. memset(l,0,sizeof(l));
  58. }
  59. };
  60. explicit Trace(const RuntimeEnvironment *renv);
  61. ZT_ALWAYS_INLINE void resettingPathsInScope(
  62. void *const tPtr,
  63. const Identity &reporter,
  64. const InetAddress &from,
  65. const InetAddress &oldExternal,
  66. const InetAddress &newExternal,
  67. const InetAddress::IpScope scope)
  68. {
  69. if (_vl1) _resettingPathsInScope(tPtr,reporter,from,oldExternal,newExternal,scope);
  70. }
  71. ZT_ALWAYS_INLINE void tryingNewPath(
  72. void *const tPtr,
  73. const Identity &trying,
  74. const InetAddress &physicalAddress,
  75. const InetAddress &triggerAddress,
  76. uint64_t triggeringPacketId,
  77. uint8_t triggeringPacketVerb,
  78. uint64_t triggeredByAddress,
  79. const uint8_t *triggeredByIdentityHash,
  80. ZT_TraceTryingNewPathReason reason)
  81. {
  82. if (_vl1) _tryingNewPath(tPtr,trying,physicalAddress,triggerAddress,triggeringPacketId,triggeringPacketVerb,triggeredByAddress,triggeredByIdentityHash,reason);
  83. }
  84. ZT_ALWAYS_INLINE void learnedNewPath(
  85. void *const tPtr,
  86. uint64_t packetId,
  87. const Identity &peerIdentity,
  88. const InetAddress &physicalAddress,
  89. const InetAddress &replaced)
  90. {
  91. if (_vl1) _learnedNewPath(tPtr,packetId,peerIdentity,physicalAddress,replaced);
  92. }
  93. ZT_ALWAYS_INLINE void incomingPacketDropped(
  94. void *const tPtr,
  95. uint64_t packetId,
  96. uint64_t networkId,
  97. const Identity &peerIdentity,
  98. const InetAddress &physicalAddress,
  99. uint8_t hops,
  100. uint8_t verb,
  101. const ZT_TracePacketDropReason reason)
  102. {
  103. if (_vl1) _incomingPacketDropped(tPtr,packetId,networkId,peerIdentity,physicalAddress,hops,verb,reason);
  104. }
  105. ZT_ALWAYS_INLINE void outgoingNetworkFrameDropped(
  106. void *const tPtr,
  107. uint64_t networkId,
  108. const MAC &sourceMac,
  109. const MAC &destMac,
  110. uint16_t etherType,
  111. uint16_t frameLength,
  112. const uint8_t *frameData,
  113. ZT_TraceFrameDropReason reason)
  114. {
  115. if (_vl2) _outgoingNetworkFrameDropped(tPtr,networkId,sourceMac,destMac,etherType,frameLength,frameData,reason);
  116. }
  117. ZT_ALWAYS_INLINE void incomingNetworkFrameDropped(
  118. void *const tPtr,
  119. uint64_t networkId,
  120. const MAC &sourceMac,
  121. const MAC &destMac,
  122. const Identity &peerIdentity,
  123. const InetAddress &physicalAddress,
  124. uint8_t hops,
  125. uint16_t frameLength,
  126. const uint8_t *frameData,
  127. uint8_t verb,
  128. bool credentialRequestSent,
  129. ZT_TraceFrameDropReason reason)
  130. {
  131. if (_vl2) _incomingNetworkFrameDropped(tPtr,networkId,sourceMac,destMac,peerIdentity,physicalAddress,hops,frameLength,frameData,verb,credentialRequestSent,reason);
  132. }
  133. ZT_ALWAYS_INLINE void networkConfigRequestSent(
  134. void *const tPtr,
  135. uint64_t networkId)
  136. {
  137. if (_vl2) _networkConfigRequestSent(tPtr,networkId);
  138. }
  139. ZT_ALWAYS_INLINE void networkFilter(
  140. void *const tPtr,
  141. uint64_t networkId,
  142. const uint8_t primaryRuleSetLog[512],
  143. const uint8_t matchingCapabilityRuleSetLog[512],
  144. uint32_t matchingCapabilityId,
  145. int64_t matchingCapabilityTimestamp,
  146. const Address &source,
  147. const Address &dest,
  148. const MAC &sourceMac,
  149. const MAC &destMac,
  150. uint16_t frameLength,
  151. const uint8_t *frameData,
  152. uint16_t etherType,
  153. uint16_t vlanId,
  154. bool noTee,
  155. bool inbound,
  156. int accept)
  157. {
  158. if (_vl2Filter) {
  159. _networkFilter(
  160. tPtr,
  161. networkId,
  162. primaryRuleSetLog,
  163. matchingCapabilityRuleSetLog,
  164. matchingCapabilityId,
  165. matchingCapabilityTimestamp,
  166. source,
  167. dest,
  168. sourceMac,
  169. destMac,
  170. frameLength,
  171. frameData,
  172. etherType,
  173. vlanId,
  174. noTee,
  175. inbound,
  176. accept);
  177. }
  178. }
  179. ZT_ALWAYS_INLINE void credentialRejected(
  180. void *const tPtr,
  181. uint64_t networkId,
  182. const Address &address,
  183. uint32_t credentialId,
  184. int64_t credentialTimestamp,
  185. uint8_t credentialType,
  186. ZT_TraceCredentialRejectionReason reason)
  187. {
  188. if (_vl2) _credentialRejected(tPtr,networkId,address,credentialId,credentialTimestamp,credentialType,reason);
  189. }
  190. private:
  191. void _resettingPathsInScope(
  192. void *tPtr,
  193. const Identity &reporter,
  194. const InetAddress &from,
  195. const InetAddress &oldExternal,
  196. const InetAddress &newExternal,
  197. InetAddress::IpScope scope);
  198. void _tryingNewPath(
  199. void *tPtr,
  200. const Identity &trying,
  201. const InetAddress &physicalAddress,
  202. const InetAddress &triggerAddress,
  203. uint64_t triggeringPacketId,
  204. uint8_t triggeringPacketVerb,
  205. uint64_t triggeredByAddress,
  206. const uint8_t *triggeredByIdentityHash,
  207. ZT_TraceTryingNewPathReason reason);
  208. void _learnedNewPath(
  209. void *tPtr,
  210. uint64_t packetId,
  211. const Identity &peerIdentity,
  212. const InetAddress &physicalAddress,
  213. const InetAddress &replaced);
  214. void _incomingPacketDropped(
  215. void *tPtr,
  216. uint64_t packetId,
  217. uint64_t networkId,
  218. const Identity &peerIdentity,
  219. const InetAddress &physicalAddress,
  220. uint8_t hops,
  221. uint8_t verb,
  222. ZT_TracePacketDropReason reason);
  223. void _outgoingNetworkFrameDropped(
  224. void *tPtr,
  225. uint64_t networkId,
  226. const MAC &sourceMac,
  227. const MAC &destMac,
  228. uint16_t etherType,
  229. uint16_t frameLength,
  230. const uint8_t *frameData,
  231. ZT_TraceFrameDropReason reason);
  232. void _incomingNetworkFrameDropped(
  233. void *const tPtr,
  234. uint64_t networkId,
  235. const MAC &sourceMac,
  236. const MAC &destMac,
  237. const Identity &peerIdentity,
  238. const InetAddress &physicalAddress,
  239. uint8_t hops,
  240. uint16_t frameLength,
  241. const uint8_t *frameData,
  242. uint8_t verb,
  243. bool credentialRequestSent,
  244. ZT_TraceFrameDropReason reason);
  245. void _networkConfigRequestSent(
  246. void *tPtr,
  247. uint64_t networkId);
  248. void _networkFilter(
  249. void *tPtr,
  250. uint64_t networkId,
  251. const uint8_t primaryRuleSetLog[512],
  252. const uint8_t matchingCapabilityRuleSetLog[512],
  253. uint32_t matchingCapabilityId,
  254. int64_t matchingCapabilityTimestamp,
  255. const Address &source,
  256. const Address &dest,
  257. const MAC &sourceMac,
  258. const MAC &destMac,
  259. uint16_t frameLength,
  260. const uint8_t *frameData,
  261. uint16_t etherType,
  262. uint16_t vlanId,
  263. bool noTee,
  264. bool inbound,
  265. int accept);
  266. void _credentialRejected(
  267. void *tPtr,
  268. uint64_t networkId,
  269. const Address &address,
  270. uint32_t credentialId,
  271. int64_t credentialTimestamp,
  272. uint8_t credentialType,
  273. ZT_TraceCredentialRejectionReason reason);
  274. const RuntimeEnvironment *const RR;
  275. volatile bool _vl1,_vl2,_vl2Filter,_vl2Multicast;
  276. struct _MonitoringPeer
  277. {
  278. int64_t _timeSet;
  279. unsigned int _traceTypes;
  280. SharedPtr<Peer> peer;
  281. Mutex lock;
  282. };
  283. uint8_t _eventBuf[8192]; // must be less than ZT_PROTO_MAX_PACKET_LENGTH
  284. unsigned int _eventBufSize;
  285. std::vector<_MonitoringPeer> _monitoringPeers;
  286. RWMutex _monitoringPeers_l;
  287. };
  288. } // namespace ZeroTier
  289. #endif