Trace.cpp 8.3 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. // NOTE: packet IDs are always handled in network byte order, so no need to convert them.
  18. namespace ZeroTier {
  19. Trace::Trace(const RuntimeEnvironment *renv) :
  20. RR(renv),
  21. _vl1(false),
  22. _vl2(false),
  23. _vl2Filter(false),
  24. _vl2Multicast(false),
  25. _eventBufSize(0)
  26. {
  27. }
  28. void Trace::_resettingPathsInScope(
  29. void *const tPtr,
  30. const Identity &reporter,
  31. const InetAddress &from,
  32. const InetAddress &oldExternal,
  33. const InetAddress &newExternal,
  34. const InetAddress::IpScope scope)
  35. {
  36. ZT_TraceEvent_VL1_RESETTING_PATHS_IN_SCOPE ev;
  37. ev.evSize = CONST_TO_BE_UINT16(sizeof(ev));
  38. ev.evType = CONST_TO_BE_UINT16(ZT_TRACE_VL1_RESETTING_PATHS_IN_SCOPE);
  39. from.forTrace(ev.from);
  40. oldExternal.forTrace(ev.oldExternal);
  41. newExternal.forTrace(ev.newExternal);
  42. ev.scope = (uint8_t)scope;
  43. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  44. }
  45. void Trace::_tryingNewPath(
  46. void *const tPtr,
  47. const Identity &trying,
  48. const InetAddress &physicalAddress,
  49. const InetAddress &triggerAddress,
  50. uint64_t triggeringPacketId,
  51. uint8_t triggeringPacketVerb,
  52. uint64_t triggeredByAddress,
  53. const uint8_t *triggeredByIdentityHash,
  54. ZT_TraceTryingNewPathReason reason)
  55. {
  56. ZT_TraceEvent_VL1_TRYING_NEW_PATH ev;
  57. ev.evSize = CONST_TO_BE_UINT16(sizeof(ev));
  58. ev.evType = CONST_TO_BE_UINT16(ZT_TRACE_VL1_TRYING_NEW_PATH);
  59. ev.address = Utils::hton(trying.address().toInt());
  60. memcpy(ev.identityHash,trying.hash(),48);
  61. physicalAddress.forTrace(ev.physicalAddress);
  62. triggerAddress.forTrace(ev.triggerAddress);
  63. ev.triggeringPacketId = triggeringPacketId;
  64. ev.triggeringPacketVerb = triggeringPacketVerb;
  65. ev.triggeredByAddress = Utils::hton(triggeredByAddress);
  66. if (triggeredByIdentityHash)
  67. memcpy(ev.triggeredByIdentityHash,triggeredByIdentityHash,48);
  68. else memset(ev.triggeredByIdentityHash,0,48);
  69. ev.reason = (uint8_t)reason;
  70. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  71. }
  72. void Trace::_learnedNewPath(
  73. void *const tPtr,
  74. uint64_t packetId,
  75. const Identity &peerIdentity,
  76. const InetAddress &physicalAddress,
  77. const InetAddress &replaced)
  78. {
  79. ZT_TraceEvent_VL1_LEARNED_NEW_PATH ev;
  80. ev.evSize = CONST_TO_BE_UINT16(sizeof(ev));
  81. ev.evType = CONST_TO_BE_UINT16(ZT_TRACE_VL1_LEARNED_NEW_PATH);
  82. ev.packetId = packetId;
  83. ev.address = Utils::hton(peerIdentity.address().toInt());
  84. memcpy(ev.identityHash,peerIdentity.hash(),48);
  85. physicalAddress.forTrace(ev.physicalAddress);
  86. replaced.forTrace(ev.replaced);
  87. RR->node->postEvent(tPtr,ZT_EVENT_TRACE,&ev);
  88. }
  89. void Trace::_incomingPacketDropped(
  90. void *const tPtr,
  91. uint64_t packetId,
  92. uint64_t networkId,
  93. const Identity &peerIdentity,
  94. const InetAddress &physicalAddress,
  95. uint8_t hops,
  96. uint8_t verb,
  97. ZT_TracePacketDropReason reason)
  98. {
  99. ZT_TraceEvent_VL1_INCOMING_PACKET_DROPPED ev;
  100. ev.evSize = CONST_TO_BE_UINT16(sizeof(ev));
  101. ev.evType = CONST_TO_BE_UINT16(ZT_TRACE_VL1_INCOMING_PACKET_DROPPED);
  102. ev.packetId = packetId;
  103. ev.networkId = Utils::hton(networkId);
  104. if (peerIdentity) {
  105. ev.address = Utils::hton(peerIdentity.address().toInt());
  106. memcpy(ev.identityHash,peerIdentity.hash(),48);
  107. } else {
  108. ev.address = 0;
  109. memset(ev.identityHash,0,48);
  110. }
  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