Trace.cpp 11 KB

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