Trace.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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;
  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. memset(ev.message,0,sizeof(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;
  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 uint64_t triggeredByAddress,
  102. const uint8_t *triggeredByIdentityHash,
  103. const ZT_TraceTryingNewPathReason reason)
  104. {
  105. ZT_TraceEvent_VL1_TRYING_NEW_PATH ev;
  106. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  107. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL1_TRYING_NEW_PATH);
  108. ev.codeLocation = Utils::hton(codeLocation);
  109. ev.address = Utils::hton(trying.address().toInt());
  110. memcpy(ev.identityHash,trying.fingerprint().data(),48);
  111. physicalAddress.forTrace(ev.physicalAddress);
  112. triggerAddress.forTrace(ev.triggerAddress);
  113. ev.triggeringPacketId = triggeringPacketId;
  114. ev.triggeringPacketVerb = triggeringPacketVerb;
  115. ev.triggeredByAddress = Utils::hton(triggeredByAddress);
  116. if (triggeredByIdentityHash)
  117. memcpy(ev.triggeredByIdentityHash,triggeredByIdentityHash,ZT_IDENTITY_HASH_SIZE);
  118. else memset(ev.triggeredByIdentityHash,0,ZT_IDENTITY_HASH_SIZE);
  119. ev.reason = (uint8_t)reason;
  120. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  121. }
  122. void Trace::_learnedNewPath(
  123. void *const tPtr,
  124. const uint32_t codeLocation,
  125. const uint64_t packetId,
  126. const Identity &peerIdentity,
  127. const InetAddress &physicalAddress,
  128. const InetAddress &replaced)
  129. {
  130. ZT_TraceEvent_VL1_LEARNED_NEW_PATH ev;
  131. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  132. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL1_LEARNED_NEW_PATH);
  133. ev.codeLocation = Utils::hton(codeLocation);
  134. ev.packetId = packetId; // packet IDs are kept in big-endian
  135. ev.address = Utils::hton(peerIdentity.address().toInt());
  136. memcpy(ev.identityHash,peerIdentity.fingerprint().data(),ZT_IDENTITY_HASH_SIZE);
  137. physicalAddress.forTrace(ev.physicalAddress);
  138. replaced.forTrace(ev.replaced);
  139. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  140. }
  141. void Trace::_incomingPacketDropped(
  142. void *const tPtr,
  143. const uint32_t codeLocation,
  144. const uint64_t packetId,
  145. const uint64_t networkId,
  146. const Identity &peerIdentity,
  147. const InetAddress &physicalAddress,
  148. const uint8_t hops,
  149. const uint8_t verb,
  150. const ZT_TracePacketDropReason reason)
  151. {
  152. ZT_TraceEvent_VL1_INCOMING_PACKET_DROPPED ev;
  153. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  154. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL1_INCOMING_PACKET_DROPPED);
  155. ev.codeLocation = Utils::hton(codeLocation);
  156. ev.packetId = packetId; // packet IDs are kept in big-endian
  157. ev.networkId = Utils::hton(networkId);
  158. if (peerIdentity) {
  159. ev.address = Utils::hton(peerIdentity.address().toInt());
  160. memcpy(ev.identityHash,peerIdentity.fingerprint().data(),ZT_IDENTITY_HASH_SIZE);
  161. } else {
  162. ev.address = 0;
  163. memset(ev.identityHash,0,ZT_IDENTITY_HASH_SIZE);
  164. }
  165. physicalAddress.forTrace(ev.physicalAddress);
  166. ev.hops = hops;
  167. ev.verb = verb;
  168. ev.reason = (uint8_t)reason;
  169. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  170. }
  171. void Trace::_outgoingNetworkFrameDropped(
  172. void *const tPtr,
  173. const uint32_t codeLocation,
  174. const uint64_t networkId,
  175. const MAC &sourceMac,
  176. const MAC &destMac,
  177. const uint16_t etherType,
  178. const uint16_t frameLength,
  179. const uint8_t *frameData,
  180. const ZT_TraceFrameDropReason reason)
  181. {
  182. ZT_TraceEvent_VL2_OUTGOING_FRAME_DROPPED ev;
  183. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  184. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL2_OUTGOING_FRAME_DROPPED);
  185. ev.codeLocation = Utils::hton(codeLocation);
  186. ev.networkId = Utils::hton(networkId);
  187. ev.sourceMac = Utils::hton(sourceMac.toInt());
  188. ev.destMac = Utils::hton(destMac.toInt());
  189. ev.etherType = Utils::hton(etherType);
  190. ev.frameLength = Utils::hton(frameLength);
  191. if (frameData) {
  192. unsigned int l = frameLength;
  193. if (l > sizeof(ev.frameHead))
  194. l = sizeof(ev.frameHead);
  195. memcpy(ev.frameHead,frameData,l);
  196. memset(ev.frameHead + l,0,sizeof(ev.frameHead) - l);
  197. }
  198. ev.reason = (uint8_t)reason;
  199. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  200. }
  201. void Trace::_incomingNetworkFrameDropped(
  202. void *const tPtr,
  203. const uint32_t codeLocation,
  204. const uint64_t networkId,
  205. const MAC &sourceMac,
  206. const MAC &destMac,
  207. const Identity &peerIdentity,
  208. const InetAddress &physicalAddress,
  209. const uint8_t hops,
  210. const uint16_t frameLength,
  211. const uint8_t *frameData,
  212. const uint8_t verb,
  213. const bool credentialRequestSent,
  214. const ZT_TraceFrameDropReason reason)
  215. {
  216. ZT_TraceEvent_VL2_INCOMING_FRAME_DROPPED ev;
  217. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  218. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL2_INCOMING_FRAME_DROPPED);
  219. ev.codeLocation = Utils::hton(codeLocation);
  220. ev.networkId = Utils::hton(networkId);
  221. ev.sourceMac = Utils::hton(sourceMac.toInt());
  222. ev.destMac = Utils::hton(destMac.toInt());
  223. ev.address = Utils::hton(peerIdentity.address().toInt());
  224. physicalAddress.forTrace(ev.physicalAddress);
  225. ev.hops = hops;
  226. ev.frameLength = Utils::hton(frameLength);
  227. if (frameData) {
  228. unsigned int l = frameLength;
  229. if (l > sizeof(ev.frameHead))
  230. l = sizeof(ev.frameHead);
  231. memcpy(ev.frameHead,frameData,l);
  232. memset(ev.frameHead + l,0,sizeof(ev.frameHead) - l);
  233. }
  234. ev.verb = verb;
  235. ev.credentialRequestSent = (uint8_t)credentialRequestSent;
  236. ev.reason = (uint8_t)reason;
  237. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  238. }
  239. void Trace::_networkConfigRequestSent(
  240. void *const tPtr,
  241. const uint32_t codeLocation,
  242. const uint64_t networkId)
  243. {
  244. ZT_TraceEvent_VL2_NETWORK_CONFIG_REQUESTED ev;
  245. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  246. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL2_NETWORK_CONFIG_REQUESTED);
  247. ev.codeLocation = Utils::hton(codeLocation);
  248. ev.networkId = Utils::hton(networkId);
  249. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  250. }
  251. void Trace::_networkFilter(
  252. void *const tPtr,
  253. const uint32_t codeLocation,
  254. const uint64_t networkId,
  255. const uint8_t primaryRuleSetLog[512],
  256. const uint8_t matchingCapabilityRuleSetLog[512],
  257. const uint32_t matchingCapabilityId,
  258. const int64_t matchingCapabilityTimestamp,
  259. const Address &source,
  260. const Address &dest,
  261. const MAC &sourceMac,
  262. const MAC &destMac,
  263. const uint16_t frameLength,
  264. const uint8_t *frameData,
  265. const uint16_t etherType,
  266. const uint16_t vlanId,
  267. const bool noTee,
  268. const bool inbound,
  269. const int accept)
  270. {
  271. ZT_TraceEvent_VL2_NETWORK_FILTER ev;
  272. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  273. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL2_NETWORK_FILTER);
  274. ev.codeLocation = Utils::hton(codeLocation);
  275. ev.networkId = Utils::hton(networkId);
  276. memcpy(ev.primaryRuleSetLog,primaryRuleSetLog,sizeof(ev.primaryRuleSetLog));
  277. if (matchingCapabilityRuleSetLog)
  278. memcpy(ev.matchingCapabilityRuleSetLog,matchingCapabilityRuleSetLog,sizeof(ev.matchingCapabilityRuleSetLog));
  279. else memset(ev.matchingCapabilityRuleSetLog,0,sizeof(ev.matchingCapabilityRuleSetLog));
  280. ev.matchingCapabilityId = Utils::hton(matchingCapabilityId);
  281. ev.matchingCapabilityTimestamp = Utils::hton(matchingCapabilityTimestamp);
  282. ev.source = Utils::hton(source.toInt());
  283. ev.dest = Utils::hton(dest.toInt());
  284. ev.sourceMac = Utils::hton(sourceMac.toInt());
  285. ev.destMac = Utils::hton(destMac.toInt());
  286. ev.frameLength = Utils::hton(frameLength);
  287. if (frameData) {
  288. unsigned int l = frameLength;
  289. if (l > sizeof(ev.frameHead))
  290. l = sizeof(ev.frameHead);
  291. memcpy(ev.frameHead,frameData,l);
  292. memset(ev.frameHead + l,0,sizeof(ev.frameHead) - l);
  293. }
  294. ev.etherType = Utils::hton(etherType);
  295. ev.vlanId = Utils::hton(vlanId);
  296. ev.noTee = (uint8_t)noTee;
  297. ev.inbound = (uint8_t)inbound;
  298. ev.accept = (int8_t)accept;
  299. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  300. }
  301. void Trace::_credentialRejected(
  302. void *const tPtr,
  303. const uint32_t codeLocation,
  304. const uint64_t networkId,
  305. const Address &address,
  306. const uint32_t credentialId,
  307. const int64_t credentialTimestamp,
  308. const uint8_t credentialType,
  309. const ZT_TraceCredentialRejectionReason reason)
  310. {
  311. ZT_TraceEvent_VL2_CREDENTIAL_REJECTED ev;
  312. ev.evSize = ZT_CONST_TO_BE_UINT16(sizeof(ev));
  313. ev.evType = ZT_CONST_TO_BE_UINT16(ZT_TRACE_VL2_NETWORK_FILTER);
  314. ev.codeLocation = Utils::hton(codeLocation);
  315. ev.networkId = Utils::hton(networkId);
  316. ev.address = Utils::hton(address.toInt());
  317. ev.credentialId = Utils::hton(credentialId);
  318. ev.credentialTimestamp = Utils::hton(credentialTimestamp);
  319. ev.credentialType = credentialType;
  320. ev.reason = (uint8_t)reason;
  321. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  322. }
  323. } // namespace ZeroTier