summaryrefslogtreecommitdiff
path: root/node/Trace.cpp
blob: 386edaacea6bb74ee1f75b353048cb36aca8a2d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
 * ZeroTier One - Network Virtualization Everywhere
 * Copyright (C) 2011-2018  ZeroTier, Inc.  https://www.zerotier.com/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * --
 *
 * You can be released from the requirements of the license by purchasing
 * a commercial license. Buying such a license is mandatory as soon as you
 * develop commercial closed-source software that incorporates or links
 * directly against ZeroTier software without disclosing the source code
 * of your own application.
 */

//#define ZT_TRACE

#include <stdio.h>
#include <stdarg.h>

#include "Trace.hpp"
#include "RuntimeEnvironment.hpp"
#include "Switch.hpp"
#include "Node.hpp"
#include "Utils.hpp"
#include "Dictionary.hpp"
#include "CertificateOfMembership.hpp"
#include "CertificateOfOwnership.hpp"
#include "Tag.hpp"
#include "Capability.hpp"
#include "Revocation.hpp"

namespace ZeroTier {

#ifdef ZT_TRACE
static void ZT_LOCAL_TRACE(void *const tPtr,const RuntimeEnvironment *const RR,const char *const fmt,...)
{
	char traceMsgBuf[1024];
	va_list ap;
	va_start(ap,fmt);
	vsnprintf(traceMsgBuf,sizeof(traceMsgBuf),fmt,ap);
	va_end(ap);
	traceMsgBuf[sizeof(traceMsgBuf) - 1] = (char)0;
	RR->node->postEvent(tPtr,ZT_EVENT_TRACE,traceMsgBuf);
}
#else
#define ZT_LOCAL_TRACE(...)
#endif

void Trace::resettingPathsInScope(void *const tPtr,const Address &reporter,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,const InetAddress::IpScope scope)
{
	char tmp[128];

	ZT_LOCAL_TRACE(tPtr,RR,"RESET and revalidate paths in scope %d; new phy address %s reported by trusted peer %.10llx",(int)scope,myPhysicalAddress.toIpString(tmp),reporter.toInt());

	Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
	d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__RESETTING_PATHS_IN_SCOPE_S);
	d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_ZTADDR,reporter);
	d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_PHYADDR,reporterPhysicalAddress.toString(tmp));
	d.add(ZT_REMOTE_TRACE_FIELD__LOCAL_PHYADDR,myPhysicalAddress.toString(tmp));
	d.add(ZT_REMOTE_TRACE_FIELD__IP_SCOPE,(uint64_t)scope);

	if (_globalTarget)
		_send(tPtr,d,_globalTarget);
	_spamToAllNetworks(tPtr,d,Trace::LEVEL_NORMAL);
}

void Trace::peerConfirmingUnknownPath(void *const tPtr,const uint64_t networkId,Peer &peer,const SharedPtr<Path> &path,const uint64_t packetId,const Packet::Verb verb)
{
	char tmp[128];
	if (!path) return; // sanity check

	ZT_LOCAL_TRACE(tPtr,RR,"trying unknown path %s to %.10llx (packet %.16llx verb %d local socket %lld network %.16llx)",path->address().toString(tmp),peer.address().toInt(),packetId,(double)verb,path->localSocket(),networkId);

	std::pair<Address,Trace::Level> byn;
	if (networkId) { Mutex::Lock l(_byNet_m); _byNet.get(networkId,byn); }

	if ((_globalTarget)||(byn.first)) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__PEER_CONFIRMING_UNKNOWN_PATH_S);
		d.add(ZT_REMOTE_TRACE_FIELD__PACKET_ID,packetId);
		d.add(ZT_REMOTE_TRACE_FIELD__PACKET_VERB,(uint64_t)verb);
		if (networkId)
			d.add(ZT_REMOTE_TRACE_FIELD__NETWORK_ID,networkId);
		d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_ZTADDR,peer.address());
		if (path) {
			d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_PHYADDR,path->address().toString(tmp));
			d.add(ZT_REMOTE_TRACE_FIELD__LOCAL_SOCKET,path->localSocket());
		}

		if (_globalTarget)
			_send(tPtr,d,_globalTarget);
		if (byn.first)
			_send(tPtr,d,byn.first);
	}
}

void Trace::peerLearnedNewPath(void *const tPtr,const uint64_t networkId,Peer &peer,const SharedPtr<Path> &newPath,const uint64_t packetId)
{
	char tmp[128];
	if (!newPath) return; // sanity check

	ZT_LOCAL_TRACE(tPtr,RR,"learned new path %s to %.10llx (packet %.16llx local socket %lld network %.16llx)",newPath->address().toString(tmp),peer.address().toInt(),packetId,newPath->localSocket(),networkId);

	std::pair<Address,Trace::Level> byn;
	if (networkId) { Mutex::Lock l(_byNet_m); _byNet.get(networkId,byn); }

	if ((_globalTarget)||(byn.first)) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__PEER_LEARNED_NEW_PATH_S);
		d.add(ZT_REMOTE_TRACE_FIELD__PACKET_ID,packetId);
		if (networkId)
			d.add(ZT_REMOTE_TRACE_FIELD__NETWORK_ID,networkId);
		d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_ZTADDR,peer.address());
		d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_PHYADDR,newPath->address().toString(tmp));
		d.add(ZT_REMOTE_TRACE_FIELD__LOCAL_SOCKET,newPath->localSocket());

		if (_globalTarget)
			_send(tPtr,d,_globalTarget);
		if (byn.first)
			_send(tPtr,d,byn.first);
	}
}

void Trace::peerRedirected(void *const tPtr,const uint64_t networkId,Peer &peer,const SharedPtr<Path> &newPath)
{
	char tmp[128];
	if (!newPath) return; // sanity check

	ZT_LOCAL_TRACE(tPtr,RR,"explicit redirect from %.10llx to path %s",peer.address().toInt(),newPath->address().toString(tmp));

	std::pair<Address,Trace::Level> byn;
	if (networkId) { Mutex::Lock l(_byNet_m); _byNet.get(networkId,byn); }

	if ((_globalTarget)||(byn.first)) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__PEER_REDIRECTED_S);
		if (networkId)
			d.add(ZT_REMOTE_TRACE_FIELD__NETWORK_ID,networkId);
		d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_ZTADDR,peer.address());
		d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_PHYADDR,newPath->address().toString(tmp));
		d.add(ZT_REMOTE_TRACE_FIELD__LOCAL_SOCKET,newPath->localSocket());

		if (_globalTarget)
			_send(tPtr,d,_globalTarget);
		if (byn.first)
			_send(tPtr,d,byn.first);
	}
}

void Trace::outgoingNetworkFrameDropped(void *const tPtr,const SharedPtr<Network> &network,const MAC &sourceMac,const MAC &destMac,const unsigned int etherType,const unsigned int vlanId,const unsigned int frameLen,const char *reason)
{
#ifdef ZT_TRACE
	char tmp[128],tmp2[128];
#endif
	if (!network) return; // sanity check

	ZT_LOCAL_TRACE(tPtr,RR,"%.16llx DROP frame %s -> %s etherType %.4x size %u (%s)",network->id(),sourceMac.toString(tmp),destMac.toString(tmp2),etherType,frameLen,(reason) ? reason : "unknown reason");

	std::pair<Address,Trace::Level> byn;
	{ Mutex::Lock l(_byNet_m); _byNet.get(network->id(),byn); }

	if ( ((_globalTarget)&&((int)_globalLevel >= (int)Trace::LEVEL_VERBOSE)) || ((byn.first)&&((int)byn.second >= (int)Trace::LEVEL_VERBOSE)) ) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__OUTGOING_NETWORK_FRAME_DROPPED_S);
		d.add(ZT_REMOTE_TRACE_FIELD__NETWORK_ID,network->id());
		d.add(ZT_REMOTE_TRACE_FIELD__SOURCE_MAC,sourceMac.toInt());
		d.add(ZT_REMOTE_TRACE_FIELD__DEST_MAC,destMac.toInt());
		d.add(ZT_REMOTE_TRACE_FIELD__ETHERTYPE,(uint64_t)etherType);
		d.add(ZT_REMOTE_TRACE_FIELD__VLAN_ID,(uint64_t)vlanId);
		d.add(ZT_REMOTE_TRACE_FIELD__FRAME_LENGTH,(uint64_t)frameLen);
		if (reason)
			d.add(ZT_REMOTE_TRACE_FIELD__REASON,reason);

		if ((_globalTarget)&&((int)_globalLevel >= (int)Trace::LEVEL_VERBOSE))
			_send(tPtr,d,_globalTarget);
		if ((byn.first)&&((int)byn.second >= (int)Trace::LEVEL_VERBOSE))
			_send(tPtr,d,byn.first);
	}
}

void Trace::incomingNetworkAccessDenied(void *const tPtr,const SharedPtr<Network> &network,const SharedPtr<Path> &path,const uint64_t packetId,const unsigned int packetLength,const Address &source,const Packet::Verb verb,bool credentialsRequested)
{
	char tmp[128];
	if (!network) return; // sanity check

	ZT_LOCAL_TRACE(tPtr,RR,"%.16llx DENIED packet from %.10llx(%s) verb %d size %u%s",network->id(),source.toInt(),(path) ? (path->address().toString(tmp)) : "???",(int)verb,packetLength,credentialsRequested ? " (credentials requested)" : " (credentials not requested)");

	std::pair<Address,Trace::Level> byn;
	{ Mutex::Lock l(_byNet_m); _byNet.get(network->id(),byn); }

	if ( ((_globalTarget)&&((int)_globalLevel >= (int)Trace::LEVEL_VERBOSE)) || ((byn.first)&&((int)byn.second >= (int)Trace::LEVEL_VERBOSE)) ) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__INCOMING_NETWORK_ACCESS_DENIED_S);
		d.add(ZT_REMOTE_TRACE_FIELD__PACKET_ID,packetId);
		d.add(ZT_REMOTE_TRACE_FIELD__PACKET_VERB,(uint64_t)verb);
		d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_ZTADDR,source);
		if (path) {
			d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_PHYADDR,path->address().toString(tmp));
			d.add(ZT_REMOTE_TRACE_FIELD__LOCAL_SOCKET,path->localSocket());
		}
		d.add(ZT_REMOTE_TRACE_FIELD__NETWORK_ID,network->id());

		if ((_globalTarget)&&((int)_globalLevel >= (int)Trace::LEVEL_VERBOSE))
			_send(tPtr,d,_globalTarget);
		if ((byn.first)&&((int)byn.second >= (int)Trace::LEVEL_VERBOSE))
			_send(tPtr,d,byn.first);
	}
}

void Trace::incomingNetworkFrameDropped(void *const tPtr,const SharedPtr<Network> &network,const SharedPtr<Path> &path,const uint64_t packetId,const unsigned int packetLength,const Address &source,const Packet::Verb verb,const MAC &sourceMac,const MAC &destMac,const char *reason)
{
	char tmp[128];
	if (!network) return; // sanity check

	ZT_LOCAL_TRACE(tPtr,RR,"%.16llx DROPPED frame from %.10llx(%s) verb %d size %u",network->id(),source.toInt(),(path) ? (path->address().toString(tmp)) : "???",(int)verb,packetLength);

	std::pair<Address,Trace::Level> byn;
	{ Mutex::Lock l(_byNet_m); _byNet.get(network->id(),byn); }

	if ( ((_globalTarget)&&((int)_globalLevel >= (int)Trace::LEVEL_VERBOSE)) || ((byn.first)&&((int)byn.second >= (int)Trace::LEVEL_VERBOSE)) ) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__INCOMING_NETWORK_FRAME_DROPPED_S);
		d.add(ZT_REMOTE_TRACE_FIELD__PACKET_ID,packetId);
		d.add(ZT_REMOTE_TRACE_FIELD__PACKET_VERB,(uint64_t)verb);
		d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_ZTADDR,source);
		if (path) {
			d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_PHYADDR,path->address().toString(tmp));
			d.add(ZT_REMOTE_TRACE_FIELD__LOCAL_SOCKET,path->localSocket());
		}
		d.add(ZT_REMOTE_TRACE_FIELD__NETWORK_ID,network->id());
		d.add(ZT_REMOTE_TRACE_FIELD__SOURCE_MAC,sourceMac.toInt());
		d.add(ZT_REMOTE_TRACE_FIELD__DEST_MAC,destMac.toInt());
		if (reason)
			d.add(ZT_REMOTE_TRACE_FIELD__REASON,reason);

		if ((_globalTarget)&&((int)_globalLevel >= (int)Trace::LEVEL_VERBOSE))
			_send(tPtr,d,_globalTarget);
		if ((byn.first)&&((int)byn.second >= (int)Trace::LEVEL_VERBOSE))
			_send(tPtr,d,byn.first);
	}
}

void Trace::incomingPacketMessageAuthenticationFailure(void *const tPtr,const SharedPtr<Path> &path,const uint64_t packetId,const Address &source,const unsigned int hops,const char *reason)
{
	char tmp[128];

	ZT_LOCAL_TRACE(tPtr,RR,"MAC failed for packet %.16llx from %.10llx(%s)",packetId,source.toInt(),(path) ? path->address().toString(tmp) : "???");

	if ((_globalTarget)&&((int)_globalLevel >= Trace::LEVEL_DEBUG)) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__PACKET_MAC_FAILURE_S);
		d.add(ZT_REMOTE_TRACE_FIELD__PACKET_ID,packetId);
		d.add(ZT_REMOTE_TRACE_FIELD__PACKET_HOPS,(uint64_t)hops);
		d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_ZTADDR,source);
		if (path) {
			d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_PHYADDR,path->address().toString(tmp));
			d.add(ZT_REMOTE_TRACE_FIELD__LOCAL_SOCKET,path->localSocket());
		}
		if (reason)
			d.add(ZT_REMOTE_TRACE_FIELD__REASON,reason);

		_send(tPtr,d,_globalTarget);
	}
}

void Trace::incomingPacketInvalid(void *const tPtr,const SharedPtr<Path> &path,const uint64_t packetId,const Address &source,const unsigned int hops,const Packet::Verb verb,const char *reason)
{
	char tmp[128];

	ZT_LOCAL_TRACE(tPtr,RR,"INVALID packet %.16llx from %.10llx(%s) (%s)",packetId,source.toInt(),(path) ? path->address().toString(tmp) : "???",(reason) ? reason : "unknown reason");

	if ((_globalTarget)&&((int)_globalLevel >= Trace::LEVEL_DEBUG)) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__PACKET_INVALID_S);
		d.add(ZT_REMOTE_TRACE_FIELD__PACKET_ID,packetId);
		d.add(ZT_REMOTE_TRACE_FIELD__PACKET_VERB,(uint64_t)verb);
		d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_ZTADDR,source);
		if (path) {
			d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_PHYADDR,path->address().toString(tmp));
			d.add(ZT_REMOTE_TRACE_FIELD__LOCAL_SOCKET,path->localSocket());
		}
		d.add(ZT_REMOTE_TRACE_FIELD__PACKET_HOPS,(uint64_t)hops);
		if (reason)
			d.add(ZT_REMOTE_TRACE_FIELD__REASON,reason);

		_send(tPtr,d,_globalTarget);
	}
}

void Trace::incomingPacketDroppedHELLO(void *const tPtr,const SharedPtr<Path> &path,const uint64_t packetId,const Address &source,const char *reason)
{
	char tmp[128];

	ZT_LOCAL_TRACE(tPtr,RR,"DROPPED HELLO from %.10llx(%s) (%s)",source.toInt(),(path) ? path->address().toString(tmp) : "???",(reason) ? reason : "???");

	if ((_globalTarget)&&((int)_globalLevel >= Trace::LEVEL_DEBUG)) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__PACKET_INVALID_S);
		d.add(ZT_REMOTE_TRACE_FIELD__PACKET_ID,packetId);
		d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_ZTADDR,source);
		if (path) {
			d.add(ZT_REMOTE_TRACE_FIELD__REMOTE_PHYADDR,path->address().toString(tmp));
			d.add(ZT_REMOTE_TRACE_FIELD__LOCAL_SOCKET,path->localSocket());
		}
		if (reason)
			d.add(ZT_REMOTE_TRACE_FIELD__REASON,reason);

		_send(tPtr,d,_globalTarget);
	}
}

void Trace::networkConfigRequestSent(void *const tPtr,const Network &network,const Address &controller)
{
	ZT_LOCAL_TRACE(tPtr,RR,"requesting configuration for network %.16llx",network.id());
	if ((_globalTarget)&&((int)_globalLevel >= Trace::LEVEL_DEBUG)) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__NETWORK_CONFIG_REQUEST_SENT_S);
		d.add(ZT_REMOTE_TRACE_FIELD__NETWORK_ID,network.id());
		d.add(ZT_REMOTE_TRACE_FIELD__NETWORK_CONTROLLER_ID,controller);
		_send(tPtr,d,_globalTarget);
	}
}

void Trace::networkFilter(
	void *const tPtr,
	const Network &network,
	const RuleResultLog &primaryRuleSetLog,
	const RuleResultLog *const matchingCapabilityRuleSetLog,
	const Capability *const matchingCapability,
	const Address &ztSource,
	const Address &ztDest,
	const MAC &macSource,
	const MAC &macDest,
	const uint8_t *const frameData,
	const unsigned int frameLen,
	const unsigned int etherType,
	const unsigned int vlanId,
	const bool noTee,
	const bool inbound,
	const int accept)
{
	std::pair<Address,Trace::Level> byn;
	{ Mutex::Lock l(_byNet_m); _byNet.get(network.id(),byn); }

	if ( ((_globalTarget)&&((int)_globalLevel >= (int)Trace::LEVEL_RULES)) || ((byn.first)&&((int)byn.second >= (int)Trace::LEVEL_RULES)) ) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__NETWORK_FILTER_TRACE_S);
		d.add(ZT_REMOTE_TRACE_FIELD__NETWORK_ID,network.id());
		d.add(ZT_REMOTE_TRACE_FIELD__SOURCE_ZTADDR,ztSource);
		d.add(ZT_REMOTE_TRACE_FIELD__DEST_ZTADDR,ztDest);
		d.add(ZT_REMOTE_TRACE_FIELD__SOURCE_MAC,macSource.toInt());
		d.add(ZT_REMOTE_TRACE_FIELD__DEST_MAC,macDest.toInt());
		d.add(ZT_REMOTE_TRACE_FIELD__ETHERTYPE,(uint64_t)etherType);
		d.add(ZT_REMOTE_TRACE_FIELD__VLAN_ID,(uint64_t)vlanId);
		d.add(ZT_REMOTE_TRACE_FIELD__FILTER_FLAG_NOTEE,noTee ? "1" : "0");
		d.add(ZT_REMOTE_TRACE_FIELD__FILTER_FLAG_INBOUND,inbound ? "1" : "0");
		d.add(ZT_REMOTE_TRACE_FIELD__FILTER_RESULT,(int64_t)accept);
		d.add(ZT_REMOTE_TRACE_FIELD__FILTER_BASE_RULE_LOG,(const char *)primaryRuleSetLog.data(),(int)primaryRuleSetLog.sizeBytes());
		if (matchingCapabilityRuleSetLog)
			d.add(ZT_REMOTE_TRACE_FIELD__FILTER_CAP_RULE_LOG,(const char *)matchingCapabilityRuleSetLog->data(),(int)matchingCapabilityRuleSetLog->sizeBytes());
		if (matchingCapability)
			d.add(ZT_REMOTE_TRACE_FIELD__FILTER_CAP_ID,(uint64_t)matchingCapability->id());
		d.add(ZT_REMOTE_TRACE_FIELD__FRAME_LENGTH,(uint64_t)frameLen);
		if (frameLen > 0)
			d.add(ZT_REMOTE_TRACE_FIELD__FRAME_DATA,(const char *)frameData,(frameLen > 256) ? (int)256 : (int)frameLen);

		if ((_globalTarget)&&((int)_globalLevel >= (int)Trace::LEVEL_RULES))
			_send(tPtr,d,_globalTarget);
		if ((byn.first)&&((int)byn.second >= (int)Trace::LEVEL_RULES))
			_send(tPtr,d,byn.first);
	}
}

void Trace::credentialRejected(void *const tPtr,const CertificateOfMembership &c,const char *reason)
{
	std::pair<Address,Trace::Level> byn;
	if (c.networkId()) { Mutex::Lock l(_byNet_m); _byNet.get(c.networkId(),byn); }

	if ((_globalTarget)||(byn.first)) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__CREDENTIAL_REJECTED_S);
		d.add(ZT_REMOTE_TRACE_FIELD__NETWORK_ID,c.networkId());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_TYPE,(uint64_t)c.credentialType());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_ID,(uint64_t)c.id());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_TIMESTAMP,c.timestamp());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_ISSUED_TO,c.issuedTo());
		if (reason)
			d.add(ZT_REMOTE_TRACE_FIELD__REASON,reason);

		if (_globalTarget)
			_send(tPtr,d,_globalTarget);
		if (byn.first)
			_send(tPtr,d,byn.first);
	}
}

void Trace::credentialRejected(void *const tPtr,const CertificateOfOwnership &c,const char *reason)
{
	std::pair<Address,Trace::Level> byn;
	if (c.networkId()) { Mutex::Lock l(_byNet_m); _byNet.get(c.networkId(),byn); }

	if ((_globalTarget)||(byn.first)) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__CREDENTIAL_REJECTED_S);
		d.add(ZT_REMOTE_TRACE_FIELD__NETWORK_ID,c.networkId());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_TYPE,(uint64_t)c.credentialType());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_ID,(uint64_t)c.id());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_TIMESTAMP,c.timestamp());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_ISSUED_TO,c.issuedTo());
		if (reason)
			d.add(ZT_REMOTE_TRACE_FIELD__REASON,reason);

		if (_globalTarget)
			_send(tPtr,d,_globalTarget);
		if (byn.first)
			_send(tPtr,d,byn.first);
	}
}

void Trace::credentialRejected(void *const tPtr,const Capability &c,const char *reason)
{
	std::pair<Address,Trace::Level> byn;
	if (c.networkId()) { Mutex::Lock l(_byNet_m); _byNet.get(c.networkId(),byn); }

	if ((_globalTarget)||(byn.first)) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__CREDENTIAL_REJECTED_S);
		d.add(ZT_REMOTE_TRACE_FIELD__NETWORK_ID,c.networkId());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_TYPE,(uint64_t)c.credentialType());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_ID,(uint64_t)c.id());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_TIMESTAMP,c.timestamp());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_ISSUED_TO,c.issuedTo());
		if (reason)
			d.add(ZT_REMOTE_TRACE_FIELD__REASON,reason);

		if (_globalTarget)
			_send(tPtr,d,_globalTarget);
		if (byn.first)
			_send(tPtr,d,byn.first);
	}
}

void Trace::credentialRejected(void *const tPtr,const Tag &c,const char *reason)
{
	std::pair<Address,Trace::Level> byn;
	if (c.networkId()) { Mutex::Lock l(_byNet_m); _byNet.get(c.networkId(),byn); }

	if ((_globalTarget)||(byn.first)) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__CREDENTIAL_REJECTED_S);
		d.add(ZT_REMOTE_TRACE_FIELD__NETWORK_ID,c.networkId());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_TYPE,(uint64_t)c.credentialType());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_ID,(uint64_t)c.id());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_TIMESTAMP,c.timestamp());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_ISSUED_TO,c.issuedTo());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_INFO,(uint64_t)c.value());
		if (reason)
			d.add(ZT_REMOTE_TRACE_FIELD__REASON,reason);

		if (_globalTarget)
			_send(tPtr,d,_globalTarget);
		if (byn.first)
			_send(tPtr,d,byn.first);
	}
}

void Trace::credentialRejected(void *const tPtr,const Revocation &c,const char *reason)
{
	std::pair<Address,Trace::Level> byn;
	if (c.networkId()) { Mutex::Lock l(_byNet_m); _byNet.get(c.networkId(),byn); }

	if ((_globalTarget)||(byn.first)) {
		Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> d;
		d.add(ZT_REMOTE_TRACE_FIELD__EVENT,ZT_REMOTE_TRACE_EVENT__CREDENTIAL_REJECTED_S);
		d.add(ZT_REMOTE_TRACE_FIELD__NETWORK_ID,c.networkId());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_TYPE,(uint64_t)c.credentialType());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_ID,(uint64_t)c.id());
		d.add(ZT_REMOTE_TRACE_FIELD__CREDENTIAL_REVOCATION_TARGET,c.target());
		if (reason)
			d.add(ZT_REMOTE_TRACE_FIELD__REASON,reason);

		if (_globalTarget)
			_send(tPtr,d,_globalTarget);
		if (byn.first)
			_send(tPtr,d,byn.first);
	}
}

void Trace::updateMemoizedSettings()
{
	_globalTarget = RR->node->remoteTraceTarget();
	_globalLevel = RR->node->remoteTraceLevel();
	const std::vector< SharedPtr<Network> > nws(RR->node->allNetworks());
	{
		Mutex::Lock l(_byNet_m);
		_byNet.clear();
		for(std::vector< SharedPtr<Network> >::const_iterator n(nws.begin());n!=nws.end();++n) {
			const Address dest((*n)->config().remoteTraceTarget);
			if (dest) {
				std::pair<Address,Trace::Level> &m = _byNet[(*n)->id()];
				m.first = dest;
				m.second = (*n)->config().remoteTraceLevel;
			}
		}
	}
}

void Trace::_send(void *const tPtr,const Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> &d,const Address &dest)
{
	Packet outp(dest,RR->identity.address(),Packet::VERB_REMOTE_TRACE);
	outp.appendCString(d.data());
	outp.compress();
	RR->sw->send(tPtr,outp,true);
}

void Trace::_spamToAllNetworks(void *const tPtr,const Dictionary<ZT_MAX_REMOTE_TRACE_SIZE> &d,const Level level)
{
	Mutex::Lock l(_byNet_m);
	Hashtable< uint64_t,std::pair< Address,Trace::Level > >::Iterator i(_byNet);
	uint64_t *k = (uint64_t *)0;
	std::pair<Address,Trace::Level> *v = (std::pair<Address,Trace::Level> *)0;
	while (i.next(k,v)) {
		if ((v)&&(v->first)&&((int)v->second >= (int)level))
			_send(tPtr,d,v->first);
	}
}

} // namespace ZeroTier