Trace.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. // Macro to avoid calling hton() on values known at compile time.
  18. #if __BYTE_ORDER == __LITTLE_ENDIAN
  19. #define CONST_TO_BE_UINT16(x) ((uint16_t)((uint16_t)((uint16_t)(x) << 8U) | (uint16_t)((uint16_t)(x) >> 8U)))
  20. #else
  21. #define CONST_TO_BE_UINT16(x) ((uint16_t)(x))
  22. #endif
  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. _eventBufSize(0)
  31. {
  32. }
  33. void Trace::_resettingPathsInScope(
  34. void *const tPtr,
  35. const Identity &reporter,
  36. const InetAddress &from,
  37. const InetAddress &oldExternal,
  38. const InetAddress &newExternal,
  39. const InetAddress::IpScope scope)
  40. {
  41. ZT_TraceEvent_VL1_RESETTING_PATHS_IN_SCOPE ev;
  42. ev.evSize = CONST_TO_BE_UINT16(sizeof(ev));
  43. ev.evType = CONST_TO_BE_UINT16(ZT_TRACE_VL1_RESETTING_PATHS_IN_SCOPE);
  44. from.forTrace(ev.from);
  45. oldExternal.forTrace(ev.oldExternal);
  46. newExternal.forTrace(ev.newExternal);
  47. ev.scope = (uint8_t)scope;
  48. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  49. }
  50. void Trace::_tryingNewPath(
  51. void *const tPtr,
  52. const Identity &trying,
  53. const InetAddress &physicalAddress,
  54. const InetAddress &triggerAddress,
  55. uint64_t triggeringPacketId,
  56. uint8_t triggeringPacketVerb,
  57. uint64_t triggeredByAddress,
  58. const uint8_t *triggeredByIdentityHash,
  59. ZT_TraceTryingNewPathReason reason)
  60. {
  61. ZT_TraceEvent_VL1_TRYING_NEW_PATH ev;
  62. ev.evSize = CONST_TO_BE_UINT16(sizeof(ev));
  63. ev.evType = CONST_TO_BE_UINT16(ZT_TRACE_VL1_TRYING_NEW_PATH);
  64. ev.address = Utils::hton(trying.address().toInt());
  65. memcpy(ev.identityHash,trying.hash(),48);
  66. physicalAddress.forTrace(ev.physicalAddress);
  67. triggerAddress.forTrace(ev.triggerAddress);
  68. ev.triggeringPacketId = Utils::hton(triggeringPacketId);
  69. ev.triggeringPacketVerb = triggeringPacketVerb;
  70. ev.triggeredByAddress = Utils::hton(triggeredByAddress);
  71. if (triggeredByIdentityHash)
  72. memcpy(ev.triggeredByIdentityHash,triggeredByIdentityHash,48);
  73. else memset(ev.triggeredByIdentityHash,0,48);
  74. ev.reason = (uint8_t)reason;
  75. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  76. }
  77. void Trace::_learnedNewPath(
  78. void *const tPtr,
  79. uint64_t packetId,
  80. const Identity &peerIdentity,
  81. const InetAddress &physicalAddress,
  82. const InetAddress &replaced)
  83. {
  84. ZT_TraceEvent_VL1_LEARNED_NEW_PATH ev;
  85. ev.evSize = CONST_TO_BE_UINT16(sizeof(ev));
  86. ev.evType = CONST_TO_BE_UINT16(ZT_TRACE_VL1_LEARNED_NEW_PATH);
  87. ev.packetId = Utils::hton(packetId);
  88. ev.address = Utils::hton(peerIdentity.address().toInt());
  89. memcpy(ev.identityHash,peerIdentity.hash(),48);
  90. physicalAddress.forTrace(ev.physicalAddress);
  91. replaced.forTrace(ev.replaced);
  92. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  93. }
  94. void Trace::_incomingPacketDropped(
  95. void *const tPtr,
  96. uint64_t packetId,
  97. uint64_t networkId,
  98. const Identity &peerIdentity,
  99. const InetAddress &physicalAddress,
  100. uint8_t hops,
  101. uint8_t verb,
  102. ZT_TracePacketDropReason reason)
  103. {
  104. ZT_TraceEvent_VL1_INCOMING_PACKET_DROPPED ev;
  105. ev.evSize = CONST_TO_BE_UINT16(sizeof(ev));
  106. ev.evType = CONST_TO_BE_UINT16(ZT_TRACE_VL1_INCOMING_PACKET_DROPPED);
  107. ev.packetId = Utils::hton(packetId);
  108. ev.networkId = Utils::hton(networkId);
  109. ev.address = Utils::hton(peerIdentity.address().toInt());
  110. memcpy(ev.identityHash,peerIdentity.hash(),48);
  111. physicalAddress.forTrace(ev.physicalAddress);
  112. ev.hops = hops;
  113. ev.verb = verb;
  114. ev.reason = (uint8_t)reason;
  115. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  116. }
  117. void Trace::_outgoingNetworkFrameDropped(
  118. void *const tPtr,
  119. uint64_t networkId,
  120. const MAC &sourceMac,
  121. const MAC &destMac,
  122. uint16_t etherType,
  123. uint16_t frameLength,
  124. const uint8_t *frameData,
  125. ZT_TraceFrameDropReason reason)
  126. {
  127. ZT_TraceEvent_VL2_OUTGOING_FRAME_DROPPED ev;
  128. ev.evSize = CONST_TO_BE_UINT16(sizeof(ev));
  129. ev.evType = CONST_TO_BE_UINT16(ZT_TRACE_VL2_OUTGOING_FRAME_DROPPED);
  130. ev.networkId = Utils::hton(networkId);
  131. ev.sourceMac = Utils::hton(sourceMac.toInt());
  132. ev.destMac = Utils::hton(destMac.toInt());
  133. ev.etherType = Utils::hton(etherType);
  134. ev.frameLength = Utils::hton(frameLength);
  135. if (frameData) {
  136. unsigned int l = frameLength;
  137. if (l > sizeof(ev.frameHead))
  138. l = sizeof(ev.frameHead);
  139. memcpy(ev.frameHead,frameData,l);
  140. memset(ev.frameHead + l,0,sizeof(ev.frameHead) - l);
  141. }
  142. ev.reason = (uint8_t)reason;
  143. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  144. }
  145. void Trace::_incomingNetworkFrameDropped(
  146. void *const tPtr,
  147. uint64_t networkId,
  148. const MAC &sourceMac,
  149. const MAC &destMac,
  150. const Identity &peerIdentity,
  151. const InetAddress &physicalAddress,
  152. uint8_t hops,
  153. uint16_t frameLength,
  154. const uint8_t *frameData,
  155. uint8_t verb,
  156. bool credentialRequestSent,
  157. ZT_TraceFrameDropReason reason)
  158. {
  159. ZT_TraceEvent_VL2_INCOMING_FRAME_DROPPED ev;
  160. ev.evSize = CONST_TO_BE_UINT16(sizeof(ev));
  161. ev.evType = CONST_TO_BE_UINT16(ZT_TRACE_VL2_INCOMING_FRAME_DROPPED);
  162. ev.networkId = Utils::hton(networkId);
  163. ev.sourceMac = Utils::hton(sourceMac.toInt());
  164. ev.destMac = Utils::hton(destMac.toInt());
  165. ev.address = Utils::hton(peerIdentity.address().toInt());
  166. physicalAddress.forTrace(ev.physicalAddress);
  167. ev.hops = hops;
  168. ev.frameLength = Utils::hton(frameLength);
  169. if (frameData) {
  170. unsigned int l = frameLength;
  171. if (l > sizeof(ev.frameHead))
  172. l = sizeof(ev.frameHead);
  173. memcpy(ev.frameHead,frameData,l);
  174. memset(ev.frameHead + l,0,sizeof(ev.frameHead) - l);
  175. }
  176. ev.verb = verb;
  177. ev.credentialRequestSent = (uint8_t)credentialRequestSent;
  178. ev.reason = (uint8_t)reason;
  179. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  180. }
  181. void Trace::_networkConfigRequestSent(
  182. void *const tPtr,
  183. uint64_t networkId)
  184. {
  185. ZT_TraceEvent_VL2_NETWORK_CONFIG_REQUESTED ev;
  186. ev.evSize = CONST_TO_BE_UINT16(sizeof(ev));
  187. ev.evType = CONST_TO_BE_UINT16(ZT_TRACE_VL2_NETWORK_CONFIG_REQUESTED);
  188. ev.networkId = Utils::hton(networkId);
  189. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  190. }
  191. void Trace::_networkFilter(
  192. void *const tPtr,
  193. uint64_t networkId,
  194. const uint8_t primaryRuleSetLog[512],
  195. const uint8_t matchingCapabilityRuleSetLog[512],
  196. uint32_t matchingCapabilityId,
  197. int64_t matchingCapabilityTimestamp,
  198. const Address &source,
  199. const Address &dest,
  200. const MAC &sourceMac,
  201. const MAC &destMac,
  202. uint16_t frameLength,
  203. const uint8_t *frameData,
  204. uint16_t etherType,
  205. uint16_t vlanId,
  206. bool noTee,
  207. bool inbound,
  208. int accept)
  209. {
  210. ZT_TraceEvent_VL2_NETWORK_FILTER ev;
  211. ev.evSize = CONST_TO_BE_UINT16(sizeof(ev));
  212. ev.evType = CONST_TO_BE_UINT16(ZT_TRACE_VL2_NETWORK_FILTER);
  213. ev.networkId = Utils::hton(networkId);
  214. memcpy(ev.primaryRuleSetLog,primaryRuleSetLog,sizeof(ev.primaryRuleSetLog));
  215. if (matchingCapabilityRuleSetLog)
  216. memcpy(ev.matchingCapabilityRuleSetLog,matchingCapabilityRuleSetLog,sizeof(ev.matchingCapabilityRuleSetLog));
  217. else memset(ev.matchingCapabilityRuleSetLog,0,sizeof(ev.matchingCapabilityRuleSetLog));
  218. ev.matchingCapabilityId = Utils::hton(matchingCapabilityId);
  219. ev.matchingCapabilityTimestamp = Utils::hton(matchingCapabilityTimestamp);
  220. ev.source = Utils::hton(source.toInt());
  221. ev.dest = Utils::hton(dest.toInt());
  222. ev.sourceMac = Utils::hton(sourceMac.toInt());
  223. ev.destMac = Utils::hton(destMac.toInt());
  224. ev.frameLength = Utils::hton(frameLength);
  225. if (frameData) {
  226. unsigned int l = frameLength;
  227. if (l > sizeof(ev.frameHead))
  228. l = sizeof(ev.frameHead);
  229. memcpy(ev.frameHead,frameData,l);
  230. memset(ev.frameHead + l,0,sizeof(ev.frameHead) - l);
  231. }
  232. ev.etherType = Utils::hton(etherType);
  233. ev.vlanId = Utils::hton(vlanId);
  234. ev.noTee = (uint8_t)noTee;
  235. ev.inbound = (uint8_t)inbound;
  236. ev.accept = (int8_t)accept;
  237. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  238. }
  239. void Trace::_credentialRejected(
  240. void *const tPtr,
  241. uint64_t networkId,
  242. const Address &address,
  243. uint32_t credentialId,
  244. int64_t credentialTimestamp,
  245. uint8_t credentialType,
  246. ZT_TraceCredentialRejectionReason reason)
  247. {
  248. ZT_TraceEvent_VL2_CREDENTIAL_REJECTED ev;
  249. ev.evSize = CONST_TO_BE_UINT16(sizeof(ev));
  250. ev.evType = CONST_TO_BE_UINT16(ZT_TRACE_VL2_NETWORK_FILTER);
  251. ev.networkId = Utils::hton(networkId);
  252. ev.address = Utils::hton(address.toInt());
  253. ev.credentialId = Utils::hton(credentialId);
  254. ev.credentialTimestamp = Utils::hton(credentialTimestamp);
  255. ev.credentialType = credentialType;
  256. ev.reason = (uint8_t)reason;
  257. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  258. }
  259. } // namespace ZeroTier