Trace.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #include "Trace.hpp"
  14. #include "RuntimeEnvironment.hpp"
  15. #include "Node.hpp"
  16. #include "Peer.hpp"
  17. #include "InetAddress.hpp"
  18. #include "FCV.hpp"
  19. // NOTE: packet IDs are always handled in network byte order, so no need to convert them.
  20. namespace ZeroTier {
  21. Trace::Trace(const RuntimeEnvironment *renv) :
  22. RR(renv),
  23. _f(0)
  24. {
  25. }
  26. void Trace::unexpectedError(
  27. void *tPtr,
  28. uint32_t codeLocation,
  29. const char *message,
  30. ...)
  31. {
  32. FCV<uint8_t,4096> buf;
  33. Dictionary::append(buf,ZT_TRACE_FIELD_TYPE,ZT_TRACE_UNEXPECTED_ERROR);
  34. Dictionary::append(buf,ZT_TRACE_FIELD_CODE_LOCATION,codeLocation);
  35. Dictionary::append(buf,ZT_TRACE_FIELD_MESSAGE,message);
  36. buf.push_back(0);
  37. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,buf.data());
  38. }
  39. void Trace::_resettingPathsInScope(
  40. void *const tPtr,
  41. const uint32_t codeLocation,
  42. const Identity &reporter,
  43. const InetAddress &from,
  44. const InetAddress &oldExternal,
  45. const InetAddress &newExternal,
  46. const InetAddress::IpScope scope)
  47. {
  48. FCV<uint8_t,4096> buf;
  49. Dictionary::append(buf,ZT_TRACE_FIELD_TYPE,ZT_TRACE_VL1_RESETTING_PATHS_IN_SCOPE);
  50. Dictionary::append(buf,ZT_TRACE_FIELD_CODE_LOCATION,codeLocation);
  51. if (from)
  52. Dictionary::appendObject(buf,ZT_TRACE_FIELD_ENDPOINT,Endpoint(from));
  53. if (oldExternal)
  54. Dictionary::appendObject(buf,ZT_TRACE_FIELD_OLD_ENDPOINT,Endpoint(oldExternal));
  55. if (newExternal)
  56. Dictionary::appendObject(buf,ZT_TRACE_FIELD_NEW_ENDPOINT,Endpoint(newExternal));
  57. Dictionary::append(buf,ZT_TRACE_FIELD_RESET_ADDRESS_SCOPE,scope);
  58. buf.push_back(0);
  59. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,buf.data());
  60. }
  61. void Trace::_tryingNewPath(
  62. void *const tPtr,
  63. const uint32_t codeLocation,
  64. const Identity &trying,
  65. const InetAddress &physicalAddress,
  66. const InetAddress &triggerAddress,
  67. const uint64_t triggeringPacketId,
  68. const uint8_t triggeringPacketVerb,
  69. const Identity &triggeringPeer)
  70. {
  71. FCV<uint8_t,4096> buf;
  72. Dictionary::append(buf,ZT_TRACE_FIELD_TYPE,ZT_TRACE_VL1_TRYING_NEW_PATH);
  73. Dictionary::append(buf,ZT_TRACE_FIELD_CODE_LOCATION,codeLocation);
  74. Dictionary::append(buf,ZT_TRACE_FIELD_IDENTITY_FINGERPRINT_HASH,trying.fingerprint().hash,ZT_FINGERPRINT_HASH_SIZE);
  75. if (triggerAddress)
  76. Dictionary::appendObject(buf,ZT_TRACE_FIELD_TRIGGER_FROM_ENDPOINT,Endpoint(triggerAddress));
  77. Dictionary::appendPacketId(buf,ZT_TRACE_FIELD_TRIGGER_FROM_PACKET_ID,triggeringPacketId);
  78. Dictionary::append(buf,ZT_TRACE_FIELD_TRIGGER_FROM_PACKET_VERB,triggeringPacketVerb);
  79. if (triggeringPeer)
  80. Dictionary::append(buf,ZT_TRACE_FIELD_TRIGGER_FROM_PEER_FINGERPRINT_HASH,triggeringPeer.fingerprint().hash,ZT_FINGERPRINT_HASH_SIZE);
  81. buf.push_back(0);
  82. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,buf.data());
  83. }
  84. void Trace::_learnedNewPath(
  85. void *const tPtr,
  86. const uint32_t codeLocation,
  87. const uint64_t packetId,
  88. const Identity &peerIdentity,
  89. const InetAddress &physicalAddress,
  90. const InetAddress &replaced)
  91. {
  92. FCV<uint8_t,4096> buf;
  93. Dictionary::append(buf,ZT_TRACE_FIELD_TYPE,ZT_TRACE_VL1_LEARNED_NEW_PATH);
  94. Dictionary::append(buf,ZT_TRACE_FIELD_CODE_LOCATION,codeLocation);
  95. Dictionary::appendPacketId(buf,ZT_TRACE_FIELD_PACKET_ID,packetId);
  96. Dictionary::append(buf,ZT_TRACE_FIELD_IDENTITY_FINGERPRINT_HASH,peerIdentity.fingerprint().hash,ZT_FINGERPRINT_HASH_SIZE);
  97. if (physicalAddress)
  98. Dictionary::appendObject(buf,ZT_TRACE_FIELD_ENDPOINT,Endpoint(physicalAddress));
  99. if (replaced)
  100. Dictionary::appendObject(buf,ZT_TRACE_FIELD_OLD_ENDPOINT,Endpoint(replaced));
  101. buf.push_back(0);
  102. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,buf.data());
  103. }
  104. void Trace::_incomingPacketDropped(
  105. void *const tPtr,
  106. const uint32_t codeLocation,
  107. const uint64_t packetId,
  108. const uint64_t networkId,
  109. const Identity &peerIdentity,
  110. const InetAddress &physicalAddress,
  111. const uint8_t hops,
  112. const uint8_t verb,
  113. const ZT_TracePacketDropReason reason)
  114. {
  115. FCV<uint8_t,4096> buf;
  116. Dictionary::append(buf,ZT_TRACE_FIELD_TYPE,ZT_TRACE_VL1_INCOMING_PACKET_DROPPED);
  117. Dictionary::append(buf,ZT_TRACE_FIELD_CODE_LOCATION,codeLocation);
  118. Dictionary::appendPacketId(buf,ZT_TRACE_FIELD_PACKET_ID,packetId);
  119. Dictionary::append(buf,ZT_TRACE_FIELD_NETWORK_ID,networkId);
  120. Dictionary::append(buf,ZT_TRACE_FIELD_IDENTITY_FINGERPRINT_HASH,peerIdentity.fingerprint().hash,ZT_FINGERPRINT_HASH_SIZE);
  121. if (physicalAddress)
  122. Dictionary::append(buf,ZT_TRACE_FIELD_ENDPOINT,Endpoint(physicalAddress));
  123. Dictionary::append(buf,ZT_TRACE_FIELD_PACKET_HOPS,hops);
  124. Dictionary::append(buf,ZT_TRACE_FIELD_PACKET_VERB,verb);
  125. Dictionary::append(buf,ZT_TRACE_FIELD_REASON,reason);
  126. buf.push_back(0);
  127. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,buf.data());
  128. }
  129. void Trace::_outgoingNetworkFrameDropped(
  130. void *const tPtr,
  131. const uint32_t codeLocation,
  132. const uint64_t networkId,
  133. const MAC &sourceMac,
  134. const MAC &destMac,
  135. const uint16_t etherType,
  136. const uint16_t frameLength,
  137. const uint8_t *frameData,
  138. const ZT_TraceFrameDropReason reason)
  139. {
  140. FCV<uint8_t,4096> buf;
  141. Dictionary::append(buf,ZT_TRACE_FIELD_TYPE,ZT_TRACE_VL1_INCOMING_PACKET_DROPPED);
  142. Dictionary::append(buf,ZT_TRACE_FIELD_CODE_LOCATION,codeLocation);
  143. Dictionary::append(buf,ZT_TRACE_FIELD_SOURCE_MAC,sourceMac.toInt());
  144. Dictionary::append(buf,ZT_TRACE_FIELD_DEST_MAC,destMac.toInt());
  145. Dictionary::append(buf,ZT_TRACE_FIELD_ETHERTYPE,etherType);
  146. Dictionary::append(buf,ZT_TRACE_FIELD_FRAME_LENGTH,frameLength);
  147. if (frameData)
  148. Dictionary::append(buf,ZT_TRACE_FIELD_FRAME_DATA,frameData,std::min((unsigned int)64,(unsigned int)frameLength));
  149. Dictionary::append(buf,ZT_TRACE_FIELD_REASON,reason);
  150. buf.push_back(0);
  151. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,buf.data());
  152. }
  153. void Trace::_incomingNetworkFrameDropped(
  154. void *const tPtr,
  155. const uint32_t codeLocation,
  156. const uint64_t networkId,
  157. const MAC &sourceMac,
  158. const MAC &destMac,
  159. const Identity &peerIdentity,
  160. const InetAddress &physicalAddress,
  161. const uint8_t hops,
  162. const uint16_t frameLength,
  163. const uint8_t *frameData,
  164. const uint8_t verb,
  165. const bool credentialRequestSent,
  166. const ZT_TraceFrameDropReason reason)
  167. {
  168. FCV<uint8_t,4096> buf;
  169. Dictionary::append(buf,ZT_TRACE_FIELD_TYPE,ZT_TRACE_VL2_INCOMING_FRAME_DROPPED);
  170. Dictionary::append(buf,ZT_TRACE_FIELD_CODE_LOCATION,codeLocation);
  171. Dictionary::append(buf,ZT_TRACE_FIELD_SOURCE_MAC,sourceMac.toInt());
  172. Dictionary::append(buf,ZT_TRACE_FIELD_DEST_MAC,destMac.toInt());
  173. Dictionary::append(buf,ZT_TRACE_FIELD_IDENTITY_FINGERPRINT_HASH,peerIdentity.fingerprint().hash,ZT_FINGERPRINT_HASH_SIZE);
  174. if (physicalAddress)
  175. Dictionary::appendObject(buf,ZT_TRACE_FIELD_ENDPOINT,Endpoint(physicalAddress));
  176. Dictionary::append(buf,ZT_TRACE_FIELD_PACKET_HOPS,hops);
  177. Dictionary::append(buf,ZT_TRACE_FIELD_PACKET_VERB,verb);
  178. Dictionary::append(buf,ZT_TRACE_FIELD_FRAME_LENGTH,frameLength);
  179. if (frameData)
  180. Dictionary::append(buf,ZT_TRACE_FIELD_FRAME_DATA,frameData,std::min((unsigned int)64,(unsigned int)frameLength));
  181. Dictionary::append(buf,ZT_TRACE_FIELD_FLAG_CREDENTIAL_REQUEST_SENT,credentialRequestSent);
  182. Dictionary::append(buf,ZT_TRACE_FIELD_REASON,reason);
  183. buf.push_back(0);
  184. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,buf.data());
  185. }
  186. void Trace::_networkConfigRequestSent(
  187. void *const tPtr,
  188. const uint32_t codeLocation,
  189. const uint64_t networkId)
  190. {
  191. FCV<uint8_t,4096> buf;
  192. Dictionary::append(buf,ZT_TRACE_FIELD_TYPE,ZT_TRACE_VL2_NETWORK_CONFIG_REQUESTED);
  193. Dictionary::append(buf,ZT_TRACE_FIELD_CODE_LOCATION,codeLocation);
  194. Dictionary::append(buf,ZT_TRACE_FIELD_NETWORK_ID,networkId);
  195. buf.push_back(0);
  196. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,buf.data());
  197. }
  198. void Trace::_networkFilter(
  199. void *const tPtr,
  200. const uint32_t codeLocation,
  201. const uint64_t networkId,
  202. const uint8_t primaryRuleSetLog[512],
  203. const uint8_t matchingCapabilityRuleSetLog[512],
  204. const uint32_t matchingCapabilityId,
  205. const int64_t matchingCapabilityTimestamp,
  206. const Address &source,
  207. const Address &dest,
  208. const MAC &sourceMac,
  209. const MAC &destMac,
  210. const uint16_t frameLength,
  211. const uint8_t *frameData,
  212. const uint16_t etherType,
  213. const uint16_t vlanId,
  214. const bool noTee,
  215. const bool inbound,
  216. const int accept)
  217. {
  218. FCV<uint8_t,4096> buf;
  219. Dictionary::append(buf,ZT_TRACE_FIELD_TYPE,ZT_TRACE_VL2_NETWORK_FILTER);
  220. Dictionary::append(buf,ZT_TRACE_FIELD_CODE_LOCATION,codeLocation);
  221. Dictionary::append(buf,ZT_TRACE_FIELD_NETWORK_ID,networkId);
  222. if ((primaryRuleSetLog)&&(!Utils::allZero(primaryRuleSetLog,512)))
  223. Dictionary::append(buf,ZT_TRACE_FIELD_PRIMARY_RULE_SET_LOG,primaryRuleSetLog,512);
  224. if ((matchingCapabilityRuleSetLog)&&(!Utils::allZero(matchingCapabilityRuleSetLog,512)))
  225. Dictionary::append(buf,ZT_TRACE_FIELD_MATCHING_CAPABILITY_RULE_SET_LOG,matchingCapabilityRuleSetLog,512);
  226. Dictionary::append(buf,ZT_TRACE_FIELD_MATCHING_CAPABILITY_ID,matchingCapabilityId);
  227. Dictionary::append(buf,ZT_TRACE_FIELD_MATCHING_CAPABILITY_TIMESTAMP,matchingCapabilityTimestamp);
  228. Dictionary::append(buf,ZT_TRACE_FIELD_SOURCE_ZT_ADDRESS,source);
  229. Dictionary::append(buf,ZT_TRACE_FIELD_DEST_ZT_ADDRESS,dest);
  230. Dictionary::append(buf,ZT_TRACE_FIELD_SOURCE_MAC,sourceMac.toInt());
  231. Dictionary::append(buf,ZT_TRACE_FIELD_DEST_MAC,destMac.toInt());
  232. Dictionary::append(buf,ZT_TRACE_FIELD_FRAME_LENGTH,frameLength);
  233. if (frameData)
  234. Dictionary::append(buf,ZT_TRACE_FIELD_FRAME_DATA,frameData,std::min((unsigned int)64,(unsigned int)frameLength));
  235. Dictionary::append(buf,ZT_TRACE_FIELD_ETHERTYPE,etherType);
  236. Dictionary::append(buf,ZT_TRACE_FIELD_VLAN_ID,vlanId);
  237. Dictionary::append(buf,ZT_TRACE_FIELD_RULE_FLAG_NOTEE,noTee);
  238. Dictionary::append(buf,ZT_TRACE_FIELD_RULE_FLAG_INBOUND,inbound);
  239. Dictionary::append(buf,ZT_TRACE_FIELD_RULE_FLAG_ACCEPT,(int32_t)accept);
  240. buf.push_back(0);
  241. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,buf.data());
  242. }
  243. void Trace::_credentialRejected(
  244. void *const tPtr,
  245. const uint32_t codeLocation,
  246. const uint64_t networkId,
  247. const Identity &identity,
  248. const uint32_t credentialId,
  249. const int64_t credentialTimestamp,
  250. const uint8_t credentialType,
  251. const ZT_TraceCredentialRejectionReason reason)
  252. {
  253. FCV<uint8_t,4096> buf;
  254. Dictionary::append(buf,ZT_TRACE_FIELD_TYPE,ZT_TRACE_VL2_NETWORK_FILTER);
  255. Dictionary::append(buf,ZT_TRACE_FIELD_CODE_LOCATION,codeLocation);
  256. Dictionary::append(buf,ZT_TRACE_FIELD_NETWORK_ID,networkId);
  257. Dictionary::append(buf,ZT_TRACE_FIELD_IDENTITY_FINGERPRINT_HASH,identity.fingerprint().hash,ZT_FINGERPRINT_HASH_SIZE);
  258. Dictionary::append(buf,ZT_TRACE_FIELD_CREDENTIAL_ID,credentialId);
  259. Dictionary::append(buf,ZT_TRACE_FIELD_CREDENTIAL_TIMESTAMP,credentialTimestamp);
  260. Dictionary::append(buf,ZT_TRACE_FIELD_CREDENTIAL_TYPE,credentialType);
  261. Dictionary::append(buf,ZT_TRACE_FIELD_REASON,reason);
  262. buf.push_back(0);
  263. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,buf.data());
  264. }
  265. } // namespace ZeroTier