Trace.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #include "Trace.hpp"
  14. #include "RuntimeEnvironment.hpp"
  15. #include "Node.hpp"
  16. #include "Peer.hpp"
  17. #include "Path.hpp"
  18. #include "InetAddress.hpp"
  19. #include <cstdio>
  20. #include <cstdlib>
  21. #include <cstdarg>
  22. // NOTE: packet IDs are always handled in network byte order, so no need to convert them.
  23. namespace ZeroTier {
  24. Trace::Trace(const RuntimeEnvironment *renv) :
  25. RR(renv),
  26. _f(0)
  27. {
  28. }
  29. void Trace::unexpectedError(
  30. void *tPtr,
  31. uint32_t codeLocation,
  32. const char *message,
  33. ...)
  34. {
  35. va_list ap;
  36. ZT_TraceEvent_UNEXPECTED_ERROR ev; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  37. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  38. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_UNEXPECTED_ERROR);
  39. ev.codeLocation = codeLocation;
  40. Utils::zero<sizeof(ev.message)>(ev.message);
  41. va_start(ap,message);
  42. vsnprintf(ev.message,sizeof(ev.message),message,ap);
  43. va_end(ap);
  44. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  45. }
  46. void Trace::_resettingPathsInScope(
  47. void *const tPtr,
  48. const uint32_t codeLocation,
  49. const Identity &reporter,
  50. const InetAddress &from,
  51. const InetAddress &oldExternal,
  52. const InetAddress &newExternal,
  53. const InetAddress::IpScope scope)
  54. {
  55. ZT_TraceEvent_VL1_RESETTING_PATHS_IN_SCOPE ev; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  56. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  57. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL1_RESETTING_PATHS_IN_SCOPE);
  58. ev.codeLocation = Utils::hton(codeLocation);
  59. from.forTrace(ev.from);
  60. oldExternal.forTrace(ev.oldExternal);
  61. newExternal.forTrace(ev.newExternal);
  62. ev.scope = (uint8_t)scope;
  63. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  64. }
  65. void Trace::_tryingNewPath(
  66. void *const tPtr,
  67. const uint32_t codeLocation,
  68. const Identity &trying,
  69. const InetAddress &physicalAddress,
  70. const InetAddress &triggerAddress,
  71. const uint64_t triggeringPacketId,
  72. const uint8_t triggeringPacketVerb,
  73. const Identity &triggeringPeer,
  74. const ZT_TraceTryingNewPathReason reason)
  75. {
  76. ZT_TraceEvent_VL1_TRYING_NEW_PATH ev; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  77. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  78. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL1_TRYING_NEW_PATH);
  79. ev.codeLocation = Utils::hton(codeLocation);
  80. Utils::copy<sizeof(ev.peer)>(&ev.peer,trying.fingerprint().apiFingerprint());
  81. physicalAddress.forTrace(ev.physicalAddress);
  82. triggerAddress.forTrace(ev.triggerAddress);
  83. ev.triggeringPacketId = triggeringPacketId;
  84. ev.triggeringPacketVerb = triggeringPacketVerb;
  85. Utils::copy<sizeof(ev.triggeringPeer)>(&ev.triggeringPeer,triggeringPeer.fingerprint().apiFingerprint());
  86. ev.reason = (uint8_t)reason;
  87. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  88. }
  89. void Trace::_learnedNewPath(
  90. void *const tPtr,
  91. const uint32_t codeLocation,
  92. const uint64_t packetId,
  93. const Identity &peerIdentity,
  94. const InetAddress &physicalAddress,
  95. const InetAddress &replaced)
  96. {
  97. ZT_TraceEvent_VL1_LEARNED_NEW_PATH ev; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  98. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  99. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL1_LEARNED_NEW_PATH);
  100. ev.codeLocation = Utils::hton(codeLocation);
  101. ev.packetId = packetId; // packet IDs are kept in big-endian
  102. Utils::copy<sizeof(ev.peer)>(&ev.peer,peerIdentity.fingerprint().apiFingerprint());
  103. physicalAddress.forTrace(ev.physicalAddress);
  104. replaced.forTrace(ev.replaced);
  105. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  106. }
  107. void Trace::_incomingPacketDropped(
  108. void *const tPtr,
  109. const uint32_t codeLocation,
  110. const uint64_t packetId,
  111. const uint64_t networkId,
  112. const Identity &peerIdentity,
  113. const InetAddress &physicalAddress,
  114. const uint8_t hops,
  115. const uint8_t verb,
  116. const ZT_TracePacketDropReason reason)
  117. {
  118. ZT_TraceEvent_VL1_INCOMING_PACKET_DROPPED ev; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  119. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  120. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL1_INCOMING_PACKET_DROPPED);
  121. ev.codeLocation = Utils::hton(codeLocation);
  122. ev.packetId = packetId; // packet IDs are kept in big-endian
  123. ev.networkId = Utils::hton(networkId);
  124. Utils::copy<sizeof(ev.peer)>(&ev.peer,peerIdentity.fingerprint().apiFingerprint());
  125. physicalAddress.forTrace(ev.physicalAddress);
  126. ev.hops = hops;
  127. ev.verb = verb;
  128. ev.reason = (uint8_t)reason;
  129. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  130. }
  131. void Trace::_outgoingNetworkFrameDropped(
  132. void *const tPtr,
  133. const uint32_t codeLocation,
  134. const uint64_t networkId,
  135. const MAC &sourceMac,
  136. const MAC &destMac,
  137. const uint16_t etherType,
  138. const uint16_t frameLength,
  139. const uint8_t *frameData,
  140. const ZT_TraceFrameDropReason reason)
  141. {
  142. ZT_TraceEvent_VL2_OUTGOING_FRAME_DROPPED ev; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  143. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  144. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL2_OUTGOING_FRAME_DROPPED);
  145. ev.codeLocation = Utils::hton(codeLocation);
  146. ev.networkId = Utils::hton(networkId);
  147. ev.sourceMac = Utils::hton(sourceMac.toInt());
  148. ev.destMac = Utils::hton(destMac.toInt());
  149. ev.etherType = Utils::hton(etherType);
  150. ev.frameLength = Utils::hton(frameLength);
  151. if (frameData) {
  152. unsigned int l = frameLength;
  153. if (l > sizeof(ev.frameHead))
  154. l = sizeof(ev.frameHead);
  155. Utils::copy(ev.frameHead,frameData,l);
  156. Utils::zero(ev.frameHead + l,sizeof(ev.frameHead) - l);
  157. }
  158. ev.reason = (uint8_t)reason;
  159. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  160. }
  161. void Trace::_incomingNetworkFrameDropped(
  162. void *const tPtr,
  163. const uint32_t codeLocation,
  164. const uint64_t networkId,
  165. const MAC &sourceMac,
  166. const MAC &destMac,
  167. const Identity &peerIdentity,
  168. const InetAddress &physicalAddress,
  169. const uint8_t hops,
  170. const uint16_t frameLength,
  171. const uint8_t *frameData,
  172. const uint8_t verb,
  173. const bool credentialRequestSent,
  174. const ZT_TraceFrameDropReason reason)
  175. {
  176. ZT_TraceEvent_VL2_INCOMING_FRAME_DROPPED ev; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  177. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  178. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL2_INCOMING_FRAME_DROPPED);
  179. ev.codeLocation = Utils::hton(codeLocation);
  180. ev.networkId = Utils::hton(networkId);
  181. ev.sourceMac = Utils::hton(sourceMac.toInt());
  182. ev.destMac = Utils::hton(destMac.toInt());
  183. Utils::copy<sizeof(ev.sender)>(&ev.sender,peerIdentity.fingerprint().apiFingerprint());
  184. physicalAddress.forTrace(ev.physicalAddress);
  185. ev.hops = hops;
  186. ev.frameLength = Utils::hton(frameLength);
  187. if (frameData) {
  188. unsigned int l = frameLength;
  189. if (l > sizeof(ev.frameHead))
  190. l = sizeof(ev.frameHead);
  191. Utils::copy(ev.frameHead,frameData,l);
  192. Utils::zero(ev.frameHead + l,sizeof(ev.frameHead) - l);
  193. }
  194. ev.verb = verb;
  195. ev.credentialRequestSent = (uint8_t)credentialRequestSent;
  196. ev.reason = (uint8_t)reason;
  197. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  198. }
  199. void Trace::_networkConfigRequestSent(
  200. void *const tPtr,
  201. const uint32_t codeLocation,
  202. const uint64_t networkId)
  203. {
  204. ZT_TraceEvent_VL2_NETWORK_CONFIG_REQUESTED ev; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  205. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  206. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL2_NETWORK_CONFIG_REQUESTED);
  207. ev.codeLocation = Utils::hton(codeLocation);
  208. ev.networkId = Utils::hton(networkId);
  209. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  210. }
  211. void Trace::_networkFilter(
  212. void *const tPtr,
  213. const uint32_t codeLocation,
  214. const uint64_t networkId,
  215. const uint8_t primaryRuleSetLog[512],
  216. const uint8_t matchingCapabilityRuleSetLog[512],
  217. const uint32_t matchingCapabilityId,
  218. const int64_t matchingCapabilityTimestamp,
  219. const Address &source,
  220. const Address &dest,
  221. const MAC &sourceMac,
  222. const MAC &destMac,
  223. const uint16_t frameLength,
  224. const uint8_t *frameData,
  225. const uint16_t etherType,
  226. const uint16_t vlanId,
  227. const bool noTee,
  228. const bool inbound,
  229. const int accept)
  230. {
  231. ZT_TraceEvent_VL2_NETWORK_FILTER ev; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  232. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  233. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL2_NETWORK_FILTER);
  234. ev.codeLocation = Utils::hton(codeLocation);
  235. ev.networkId = Utils::hton(networkId);
  236. Utils::copy<sizeof(ev.primaryRuleSetLog)>(ev.primaryRuleSetLog,primaryRuleSetLog);
  237. if (matchingCapabilityRuleSetLog)
  238. Utils::copy<sizeof(ev.matchingCapabilityRuleSetLog)>(ev.matchingCapabilityRuleSetLog,matchingCapabilityRuleSetLog);
  239. else Utils::zero<sizeof(ev.matchingCapabilityRuleSetLog)>(ev.matchingCapabilityRuleSetLog);
  240. ev.matchingCapabilityId = Utils::hton(matchingCapabilityId);
  241. ev.matchingCapabilityTimestamp = Utils::hton(matchingCapabilityTimestamp);
  242. ev.source = Utils::hton(source.toInt());
  243. ev.dest = Utils::hton(dest.toInt());
  244. ev.sourceMac = Utils::hton(sourceMac.toInt());
  245. ev.destMac = Utils::hton(destMac.toInt());
  246. ev.frameLength = Utils::hton(frameLength);
  247. if (frameData) {
  248. unsigned int l = frameLength;
  249. if (l > sizeof(ev.frameHead))
  250. l = sizeof(ev.frameHead);
  251. Utils::copy(ev.frameHead,frameData,l);
  252. Utils::zero(ev.frameHead + l,sizeof(ev.frameHead) - l);
  253. }
  254. ev.etherType = Utils::hton(etherType);
  255. ev.vlanId = Utils::hton(vlanId);
  256. ev.noTee = (uint8_t)noTee;
  257. ev.inbound = (uint8_t)inbound;
  258. ev.accept = (int8_t)accept;
  259. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  260. }
  261. void Trace::_credentialRejected(
  262. void *const tPtr,
  263. const uint32_t codeLocation,
  264. const uint64_t networkId,
  265. const Address &address,
  266. const Identity &identity,
  267. const uint32_t credentialId,
  268. const int64_t credentialTimestamp,
  269. const uint8_t credentialType,
  270. const ZT_TraceCredentialRejectionReason reason)
  271. {
  272. ZT_TraceEvent_VL2_CREDENTIAL_REJECTED ev; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
  273. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  274. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL2_NETWORK_FILTER);
  275. ev.codeLocation = Utils::hton(codeLocation);
  276. ev.networkId = Utils::hton(networkId);
  277. if (identity) {
  278. Utils::copy<sizeof(ev.peer)>(&ev.peer,identity.fingerprint().apiFingerprint());
  279. } else {
  280. ev.peer.address = address.toInt();
  281. Utils::zero<sizeof(ev.peer.hash)>(ev.peer.hash);
  282. }
  283. ev.credentialId = Utils::hton(credentialId);
  284. ev.credentialTimestamp = Utils::hton(credentialTimestamp);
  285. ev.credentialType = credentialType;
  286. ev.reason = (uint8_t)reason;
  287. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  288. }
  289. } // namespace ZeroTier