This repository was archived by the owner on Feb 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpacket-ethereum-disc.c
More file actions
1330 lines (1135 loc) · 50.4 KB
/
packet-ethereum-disc.c
File metadata and controls
1330 lines (1135 loc) · 50.4 KB
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* packet-ethereum-disc.c
* Routines for Ethereum devp2p discovery dissection.
* Copyright 2018, ConsenSys AG.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998, Gerald Combs.
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "packet-ethereum.h"
#include <epan/proto_data.h>
#include <epan/tap.h>
#include <epan/stats_tree.h>
#include <epan/conversation.h>
#include <epan/srt_table.h>
#include <epan/exceptions.h>
#include <epan/show_exception.h>
#include <epan/to_str.h>
#define MIN_ETHDEVP2PDISCO_LEN 98
#define MAX_ETHDEVP2PDISCO_LEN 1280
#define ETHEREUM_DISC_HASH_LEN 32
#define ETHEREUM_DISC_SIGNATURE_LEN 65
#define ETHEREUM_DISC_PACKET_TYPE_IDX 97
#define ETHEREUM_DISC_PACKET_DATA_START 98
#define ETHEREUM_DISCV5_ID_STR "temporary discovery v5"
#define ETHEREUM_DISCV5_PACKET_TYPE_IDX 87 // ETHEREUM_DISC_SIGNATURE_LEN + 22 (strlen(ETHEREUM_DISCV5_ID_STR))
#define ETHEREUM_DISCV5_PACKET_DATA_START 88
// Subtrees.
static int proto_ethereum = -1;
static gint ett_ethereum_disc_toplevel = -1;
static gint ett_ethereum_disc_packetdata = -1;
static gint ett_ethereum_disc_nodes = -1;
static dissector_handle_t ethereum_disc_dtor_handle;
static nstime_t unset_time;
// Ethereum discovery protocol packet types.
typedef enum packet_type {
UNKNOWN = 0x00, // initialization value
PING = 0x01,
PONG = 0x02,
FIND_NODE = 0x03,
NODES = 0x04,
FIND_NODEHASH = 0x05,
TOPIC_REGISTER = 0x06,
TOPIC_QUERY = 0x07,
TOPIC_NODES = 0x08,
} packet_type_e;
// Value strings: packet type <=> string representation.
static const value_string packet_type_names[] = {
{UNKNOWN, "(Unknown)"},
{PING, "PING"},
{PONG, "PONG"},
{FIND_NODE, "FIND_NODE"},
{NODES, "NODES"},
{FIND_NODEHASH, "FIND_NODEHASH"},
{TOPIC_REGISTER, "TOPIC_REGISTER"},
{TOPIC_QUERY, "TOPIC_QUERY"},
{TOPIC_NODES, "TOPIC_NODES"}
};
// Message header/packet fields.
static int hf_ethereum_disc_msg_hash = -1;
static int hf_ethereum_disc_msg_sig = -1;
static int hf_ethereum_disc_packet = -1;
static int hf_ethereum_disc_packet_type = -1;
static int hf_ethereum_disc_seq = -1;
static int hf_ethereum_disc_seqtype = -1;
static int hf_ethereum_disc_req_ref = -1;
static int hf_ethereum_disc_res_ref = -1;
static int hf_ethereum_disc_rt = -1;
// PING packet.
static int hf_ethereum_disc_ping_version = -1;
static int hf_ethereum_disc_ping_sender_ipv4 = -1;
static int hf_ethereum_disc_ping_sender_ipv6 = -1;
static int hf_ethereum_disc_ping_sender_udp_port = -1;
static int hf_ethereum_disc_ping_sender_tcp_port = -1;
static int hf_ethereum_disc_ping_recipient_ipv4 = -1;
static int hf_ethereum_disc_ping_recipient_ipv6 = -1;
static int hf_ethereum_disc_ping_recipient_udp_port = -1;
static int hf_ethereum_disc_ping_recipient_tcp_port = -1;
static int hf_ethereum_disc_ping_expiration = -1;
static int hf_ethereum_disc_ping_topics = -1;
// PONG packet.
static int hf_ethereum_disc_pong_recipient_ipv4 = -1;
static int hf_ethereum_disc_pong_recipient_ipv6 = -1;
static int hf_ethereum_disc_pong_recipient_udp_port = -1;
static int hf_ethereum_disc_pong_recipient_tcp_port = -1;
static int hf_ethereum_disc_pong_ping_hash = -1;
static int hf_ethereum_disc_pong_expiration = -1;
// FIND_NODE packet.
static int hf_ethereum_disc_findnode_target = -1;
static int hf_ethereum_disc_findnode_expiration = -1;
// NODES packet.
static int hf_ethereum_disc_nodes_node = -1;
static int hf_ethereum_disc_nodes_nodes_ipv4 = -1;
static int hf_ethereum_disc_nodes_nodes_ipv6 = -1;
static int hf_ethereum_disc_nodes_nodes_udp_port = -1;
static int hf_ethereum_disc_nodes_nodes_tcp_port = -1;
static int hf_ethereum_disc_nodes_nodes_id = -1;
static int hf_ethereum_disc_nodes_expiration = -1;
static int hf_ethereum_disc_nodes_length = -1;
// TOPIC_NODES packet.
static int hf_ethereum_disc_topic_nodes_echo = -1;
// TOPIC_QUERY packet.
static int hf_ethereum_disc_topic_query_topic = -1;
static int hf_ethereum_disc_topic_query_expiration = -1;
// TOPIC_REGISTER packet.
static int hf_ethereum_disc_topic_register_topic = -1;
static int hf_ethereum_disc_topic_register_idx = -1;
static int hf_ethereum_disc_topic_register_pong = -1;
// For tap.
static int ethereum_tap = -1;
static const gchar *st_str_packets = "Total packets";
static const gchar *st_str_packet_types = "Packet types";
static const gchar *st_str_packet_nodecount = "# of nodes returned in NODES";
// Statistics nodes.
static int st_node_packets = -1;
static int st_node_packet_types = -1;
static int st_node_packet_nodes_count = -1;
// The statistics struct handled by the Ethereum discovery tap.
// Exportable as other dissectors can consume from our tap.
typedef struct _ethereum_disc_stat {
gboolean is_request;
gboolean has_request;
packet_type_e packet_type;
guint node_count;
nstime_t rq_time;
} ethereum_disc_stat_t;
// The struct where we store state concerning a conversation between two parties.
typedef struct _ethereum_disc_conv {
guint32 total_count;
guint32 ping_count;
guint32 pong_count;
guint32 findnode_count;
guint32 topicquery_count;
guint32 nodes_count;
guint32 last_ping_frame;
nstime_t last_ping_time;
guint32 last_findnode_frame;
nstime_t last_findnode_time;
guint32 last_topicquery_frame;
nstime_t last_topicquery_time;
wmem_map_t *corr;
} ethereum_disc_conv_t;
// Enhanced packet data, attached to the pinfo for rendering in header fields.
typedef struct _ethereum_disc_enhanced_data {
guint32 seq;
guint seqtype;
nstime_t rt;
nstime_t rq_time;
} ethereum_disc_enhanced_data_t;
// Represents a peer endpoint parsed from the discovery packets.
typedef struct _endpoint {
guint32 ipv4_addr;
ws_in6_addr *ipv6_addr;
guint16 udp_port;
guint16 tcp_port;
} ethereum_disc_endpoint_t;
// A function that handles a packet.
typedef int(packet_processor)(tvbuff_t *,
proto_tree *,
packet_info *,
rlp_element_t *,
ethereum_disc_stat_t *,
ethereum_disc_conv_t *,
ethereum_disc_enhanced_data_t *);
/**
* Decodes an endpoint from the provided RLP elements and adds protocol tree items into the specified fields.
*
* Fields are specified in this order:
* - IPv4 address, mutually exclusive group A.
* - IPv6 address, mutually exclusive group A.
* - UDP port.
* - TCP port (optional).
*
* @param packet_data The buffer.
* @param disc_packet The tree onto which to add the tree items.
* @param rlp The RLP element pointing to the list representing the endpoint.
* @param fields The fields onto which to output the parsed data.
* @return An endpoint struct.
*/
static ethereum_disc_endpoint_t decode_endpoint(tvbuff_t *packet_data,
proto_tree *disc_packet,
rlp_element_t *rlp,
const int *fields[4]) {
ethereum_disc_endpoint_t ret = { .ipv4_addr = 0, .ipv6_addr = NULL, .tcp_port = 0, .udp_port = 0 };
// IP addr.
rlp_next(packet_data, rlp->data_offset, rlp);
if (rlp->byte_length == 4) {
ret.ipv4_addr = tvb_get_ipv4(packet_data, rlp->data_offset);
proto_tree_add_ipv4(disc_packet, *fields[0], packet_data, rlp->data_offset, rlp->byte_length, ret.ipv4_addr);
} else {
ws_in6_addr *addr = (ws_in6_addr *) wmem_alloc0(wmem_packet_scope(), sizeof(ws_in6_addr));
tvb_get_ipv6(packet_data, rlp->data_offset, addr);
ret.ipv6_addr = addr;
proto_tree_add_ipv6(disc_packet, *fields[1], packet_data, rlp->data_offset, rlp->byte_length, ret.ipv6_addr);
}
// UDP port.
rlp_next(packet_data, rlp->next_offset, rlp);
ret.udp_port = tvb_get_guint16(packet_data, rlp->data_offset, ENC_BIG_ENDIAN);
proto_tree_add_item(disc_packet, *fields[2], packet_data,
rlp->data_offset, rlp->byte_length, ENC_BIG_ENDIAN);
// TCP port.
rlp_next(packet_data, rlp->next_offset, rlp);
if (rlp->byte_length > 0) {
ret.tcp_port = tvb_get_guint16(packet_data, rlp->data_offset, ENC_BIG_ENDIAN);
proto_tree_add_item(disc_packet, *fields[3], packet_data,
rlp->data_offset, rlp->byte_length, ENC_BIG_ENDIAN);
}
return ret;
}
/**
* Processes a PING packet.
*
* @param packet_tvb The buffer representing only the packet payload (excluding the message wrapper).
* @param packet_tree The protocol tree representing the packet.
* @param pinfo Packet
* @param rlp The RLP element pointing to the list representing the packet.
* @param st A ready-to-use statistics struct to populate.
* @param conv A ready-to-use conversation struct (retrieved or initialized).
* @param efdata A ready-to-use enhanced frame data struct.
* @return TRUE if processing was successful; FALSE otherwise.
*/
static int process_ping_msg(tvbuff_t *packet_tvb,
proto_tree *packet_tree,
packet_info *pinfo,
rlp_element_t *rlp,
ethereum_disc_stat_t *st,
ethereum_disc_conv_t *conv,
ethereum_disc_enhanced_data_t *efdata) {
proto_tree *parent;
proto_item *ti;
static const int *sender_endpoint_fields[] = {
&hf_ethereum_disc_ping_sender_ipv4,
&hf_ethereum_disc_ping_sender_ipv6,
&hf_ethereum_disc_ping_sender_udp_port,
&hf_ethereum_disc_ping_sender_tcp_port
};
static const int *recipient_endpoint_fields[] = {
&hf_ethereum_disc_ping_recipient_ipv4,
&hf_ethereum_disc_ping_recipient_ipv6,
&hf_ethereum_disc_ping_recipient_udp_port,
&hf_ethereum_disc_ping_recipient_tcp_port
};
// Version.
rlp_next(packet_tvb, rlp->data_offset, rlp);
proto_tree_add_item(packet_tree, hf_ethereum_disc_ping_version, packet_tvb,
rlp->data_offset, rlp->byte_length, ENC_BIG_ENDIAN);
// Sender endpoint.
rlp_next(packet_tvb, rlp->next_offset, rlp);
decode_endpoint(packet_tvb, packet_tree, rlp, sender_endpoint_fields);
// Recipient endpoint.
rlp_next(packet_tvb, rlp->next_offset, rlp);
decode_endpoint(packet_tvb, packet_tree, rlp, recipient_endpoint_fields);
// Expiration.
rlp_next(packet_tvb, rlp->next_offset, rlp);
proto_tree_add_item(packet_tree, hf_ethereum_disc_ping_expiration, packet_tvb,
rlp->data_offset, rlp->byte_length, ENC_TIME_SECS | ENC_BIG_ENDIAN);
if (!PINFO_FD_VISITED(pinfo)) {
efdata->seqtype = ++conv->ping_count;
conv->last_ping_frame = pinfo->num;
conv->last_ping_time = pinfo->abs_ts;
}
// Update conversation.
parent = proto_tree_get_parent_tree(packet_tree);
ti = proto_tree_add_uint(parent, hf_ethereum_disc_seqtype, packet_tvb, 0, 0, efdata->seqtype);
PROTO_ITEM_SET_GENERATED(ti);
// Link to PONG message.
guint32 pongref = GPOINTER_TO_UINT(wmem_map_lookup(conv->corr, GUINT_TO_POINTER(pinfo->num)));
if (pongref) {
ti = proto_tree_add_uint(parent, hf_ethereum_disc_res_ref, packet_tvb, 0, 0, pongref);
PROTO_ITEM_SET_GENERATED(ti);
}
st->is_request = TRUE;
return TRUE;
}
/**
* Processes a PONG packet.
*
* @param packet_tvb The buffer representing only the packet payload (excluding the message wrapper).
* @param packet_tree The protocol tree representing the packet.
* @param pinfo Packet
* @param rlp The RLP element pointing to the list representing the packet.
* @param st A ready-to-use statistics struct to populate.
* @param conv A ready-to-use conversation struct (retrieved or initialized).
* @param efdata A ready-to-use enhanced frame data struct.
* @return TRUE if processing was successful; FALSE otherwise.
*/
static int process_pong_msg(tvbuff_t *packet_tvb,
proto_tree *packet_tree,
packet_info *pinfo _U_,
rlp_element_t *rlp,
ethereum_disc_stat_t *st _U_,
ethereum_disc_conv_t *conv,
ethereum_disc_enhanced_data_t *efdata) {
proto_tree *parent;
proto_item *ti;
static const int *recipient_endpoint_fields[] = {
&hf_ethereum_disc_pong_recipient_ipv4,
&hf_ethereum_disc_pong_recipient_ipv6,
&hf_ethereum_disc_pong_recipient_udp_port,
&hf_ethereum_disc_pong_recipient_tcp_port
};
// Recipient endpoint.
rlp_next(packet_tvb, rlp->data_offset, rlp);
decode_endpoint(packet_tvb, packet_tree, rlp, recipient_endpoint_fields);
// Ping hash.
rlp_next(packet_tvb, rlp->next_offset, rlp);
proto_tree_add_item(packet_tree, hf_ethereum_disc_pong_ping_hash, packet_tvb,
rlp->data_offset, rlp->byte_length, ENC_BIG_ENDIAN);
// Expiration.
rlp_next(packet_tvb, rlp->next_offset, rlp);
// Expiration on v5 pong is broken: https://github.com/ethereum/go-ethereum/issues/17468
proto_tree_add_item(packet_tree, hf_ethereum_disc_pong_expiration, packet_tvb,
rlp->data_offset, rlp->byte_length, ENC_TIME_SECS | ENC_BIG_ENDIAN);
if (!PINFO_FD_VISITED(pinfo)) {
efdata->seqtype = ++conv->pong_count;
wmem_map_insert(conv->corr, GUINT_TO_POINTER(conv->last_ping_frame), GUINT_TO_POINTER(pinfo->num));
wmem_map_insert(conv->corr, GUINT_TO_POINTER(pinfo->num), GUINT_TO_POINTER(conv->last_ping_frame));
nstime_delta(&efdata->rt, &pinfo->fd->abs_ts, &conv->last_ping_time);
efdata->rq_time = conv->last_ping_time;
}
// Sequence number of the message type.
parent = proto_tree_get_parent_tree(packet_tree);
ti = proto_tree_add_uint(parent, hf_ethereum_disc_seqtype, packet_tvb, 0, 0, efdata->seqtype);
PROTO_ITEM_SET_GENERATED(ti);
// Link the PING request.
guint32 pingref = GPOINTER_TO_UINT(wmem_map_lookup(conv->corr, GUINT_TO_POINTER(pinfo->num)));
if (pingref) {
ti = proto_tree_add_uint(parent, hf_ethereum_disc_req_ref, packet_tvb, 0, 0, pingref);
PROTO_ITEM_SET_GENERATED(ti);
st->has_request = TRUE;
}
// Response time.
if (!nstime_is_unset(&efdata->rt)) {
ti = proto_tree_add_time(parent, hf_ethereum_disc_rt, packet_tvb, 0, 0, &efdata->rt);
PROTO_ITEM_SET_GENERATED(ti);
}
st->is_request = FALSE;
st->rq_time = efdata->rq_time;
return TRUE;
}
static int process_pong_v5_msg(tvbuff_t *packet_tvb,
proto_tree *packet_tree,
packet_info *pinfo _U_,
rlp_element_t *rlp,
ethereum_disc_stat_t *st _U_,
ethereum_disc_conv_t *conv,
ethereum_disc_enhanced_data_t *efdata) {
process_pong_msg(packet_tvb, packet_tree, pinfo, rlp, st, conv, efdata);
// TODO: process v5 fields (TopicHash, TicketSerial and WaitPeriods)
return TRUE;
}
/**
* Processes a FIND_NODE packet.
*
* @param packet_tvb The buffer representing only the packet payload (excluding the message wrapper).
* @param packet_tree The protocol tree representing the packet.
* @param pinfo Packet
* @param rlp The RLP element pointing to the list representing the packet.
* @param st A ready-to-use statistics struct to populate.
* @param conv A ready-to-use conversation struct (retrieved or initialized).
* @param efdata A ready-to-use enhanced frame data struct.
* @return TRUE if processing was successful; FALSE otherwise.
*/
static int process_findnode_msg(tvbuff_t *packet_tvb,
proto_tree *packet_tree,
packet_info *pinfo _U_,
rlp_element_t *rlp, ethereum_disc_stat_t *st _U_,
ethereum_disc_conv_t *conv,
ethereum_disc_enhanced_data_t *efdata _U_) {
proto_tree *parent;
proto_item *ti;
// Target.
rlp_next(packet_tvb, rlp->data_offset, rlp);
proto_tree_add_item(packet_tree, hf_ethereum_disc_findnode_target, packet_tvb,
rlp->data_offset, rlp->byte_length, ENC_BIG_ENDIAN);
// Expiration.
rlp_next(packet_tvb, rlp->next_offset, rlp);
proto_tree_add_item(packet_tree, hf_ethereum_disc_findnode_expiration, packet_tvb,
rlp->data_offset, rlp->byte_length, ENC_TIME_SECS | ENC_BIG_ENDIAN);
// Update conversation and enhanced frame data.
if (!PINFO_FD_VISITED(pinfo)) {
efdata->seqtype = ++conv->findnode_count;
conv->last_findnode_frame = pinfo->num;
conv->last_findnode_time = pinfo->abs_ts;
}
// Sequence number of the message type.
parent = proto_tree_get_parent_tree(packet_tree);
ti = proto_tree_add_uint(parent, hf_ethereum_disc_seqtype, packet_tvb, 0, 0, efdata->seqtype);
PROTO_ITEM_SET_GENERATED(ti);
// Link the NODES response.
parent = proto_tree_get_parent_tree(packet_tree);
guint32 nodesref = GPOINTER_TO_UINT(wmem_map_lookup(conv->corr, GUINT_TO_POINTER(pinfo->num)));
if (nodesref) {
ti = proto_tree_add_uint(parent, hf_ethereum_disc_res_ref, packet_tvb, 0, 0, nodesref);
PROTO_ITEM_SET_GENERATED(ti);
}
st->is_request = TRUE;
return TRUE;
}
static void decode_nodes_list(tvbuff_t *packet_tvb,
proto_tree *packet_tree,
packet_info *pinfo,
rlp_element_t *rlp,
ethereum_disc_stat_t *st,
ethereum_disc_conv_t *conv _U_,
ethereum_disc_enhanced_data_t *efdata _U_) {
proto_item *ti;
static const int *recipient_endpoint_fields[] = {
&hf_ethereum_disc_nodes_nodes_ipv4,
&hf_ethereum_disc_nodes_nodes_ipv6,
&hf_ethereum_disc_nodes_nodes_udp_port,
&hf_ethereum_disc_nodes_nodes_tcp_port
};
guint i = 0;
proto_tree *node_tree;
if (rlp->byte_length > 0) {
// List is not empty, move into the first element.
rlp_next(packet_tvb, rlp->data_offset, rlp);
}
while (rlp->type == LIST && rlp->byte_length > 0) {
i++;
ethereum_disc_endpoint_t ep;
ti = proto_tree_add_string(packet_tree, hf_ethereum_disc_nodes_node, packet_tvb,
rlp->data_offset, rlp->byte_length, "enode://");
node_tree = proto_item_add_subtree(ti, ett_ethereum_disc_nodes);
ep = decode_endpoint(packet_tvb, node_tree, rlp, recipient_endpoint_fields);
// Node ID.
rlp_next(packet_tvb, rlp->next_offset, rlp);
proto_tree_add_item(node_tree, hf_ethereum_disc_nodes_nodes_id, packet_tvb,
rlp->data_offset, rlp->byte_length, ENC_BIG_ENDIAN);
proto_item_append_text(ti, "%s", tvb_bytes_to_str(wmem_packet_scope(), packet_tvb, rlp->data_offset, rlp->byte_length));
proto_item_append_text(ti, "@");
if (ep.ipv6_addr) {
address addr = ADDRESS_INIT(AT_IPv6, 16, &ep.ipv6_addr->bytes);
proto_item_append_text(ti, "%s", address_to_str(wmem_packet_scope(), &addr));
} else {
address addr = ADDRESS_INIT(AT_IPv4, 4, &ep.ipv4_addr);
proto_item_append_text(ti, "%s", address_to_str(wmem_packet_scope(), &addr));
}
proto_item_append_text(ti, ":");
if (ep.tcp_port) {
proto_item_append_text(ti, "%d", ep.tcp_port);
}
if (ep.tcp_port != ep.udp_port) {
proto_item_append_text(ti, "?discport=%d", ep.udp_port);
}
if (rlp->next_offset == 0) {
break;
}
// Onto the next element.
rlp_next(packet_tvb, rlp->next_offset, rlp);
}
// Enhance packet info with # of nodes.
char more_info[64];
g_snprintf(more_info, sizeof(more_info), " (%d nodes)", i);
col_append_str(pinfo->cinfo, COL_INFO, more_info);
// Update stats with node count.
st->node_count = i;
}
/**
* Processes a NODES packet.
*
* @param packet_tvb The buffer representing only the packet payload (excluding the message wrapper).
* @param packet_tree The protocol tree representing the packet.
* @param pinfo Packet
* @param rlp The RLP element pointing to the list representing the packet.
* @param st A ready-to-use statistics struct to populate.
* @param conv A ready-to-use conversation struct (retrieved or initialized).
* @param efdata A ready-to-use enhanced frame data struct.
* @return TRUE if processing was successful; FALSE otherwise.
*/
static int process_nodes_msg(tvbuff_t *packet_tvb,
proto_tree *packet_tree,
packet_info *pinfo,
rlp_element_t *rlp,
ethereum_disc_stat_t *st,
ethereum_disc_conv_t *conv,
ethereum_disc_enhanced_data_t *efdata) {
proto_tree *parent;
proto_item *ti;
// Node list.
rlp_next(packet_tvb, rlp->data_offset, rlp);
decode_nodes_list(packet_tvb, packet_tree, pinfo, rlp, st, conv, efdata);
// Expiration
proto_tree_add_item(packet_tree, hf_ethereum_disc_nodes_expiration, packet_tvb,
rlp->data_offset, rlp->byte_length, ENC_TIME_SECS | ENC_BIG_ENDIAN);
if (!PINFO_FD_VISITED(pinfo)) {
efdata->seqtype = ++conv->nodes_count;
wmem_map_insert(conv->corr, GUINT_TO_POINTER(conv->last_findnode_frame), GUINT_TO_POINTER(pinfo->num));
wmem_map_insert(conv->corr, GUINT_TO_POINTER(pinfo->num), GUINT_TO_POINTER(conv->last_findnode_frame));
nstime_delta(&efdata->rt, &pinfo->fd->abs_ts, &conv->last_findnode_time);
efdata->rq_time = conv->last_findnode_time;
}
// Sequence number of the message type.
parent = proto_tree_get_parent_tree(packet_tree);
ti = proto_tree_add_uint(parent, hf_ethereum_disc_seqtype, packet_tvb, 0, 0, efdata->seqtype);
PROTO_ITEM_SET_GENERATED(ti);
// Link the FIND_NODE request.
guint32 findnodesref = GPOINTER_TO_UINT(wmem_map_lookup(conv->corr, GUINT_TO_POINTER(pinfo->num)));
if (findnodesref) {
ti = proto_tree_add_uint(parent, hf_ethereum_disc_req_ref, packet_tvb, 0, 0, findnodesref);
PROTO_ITEM_SET_GENERATED(ti);
st->has_request = TRUE;
}
// Response time.
if (!nstime_is_unset(&efdata->rt)) {
ti = proto_tree_add_time(parent, hf_ethereum_disc_rt, packet_tvb, 0, 0, &efdata->rt);
PROTO_ITEM_SET_GENERATED(ti);
}
st->is_request = FALSE;
st->rq_time = efdata->rq_time;
return TRUE;
}
static int process_ping_v5_msg(tvbuff_t *packet_tvb,
proto_tree *packet_tree,
packet_info *pinfo,
rlp_element_t *rlp,
ethereum_disc_stat_t *st,
ethereum_disc_conv_t *conv,
ethereum_disc_enhanced_data_t *efdata) {
process_ping_msg(packet_tvb, packet_tree, pinfo, rlp, st, conv, efdata);
// TODO: Read topic list
return TRUE;
}
static int process_topic_query_msg(tvbuff_t *packet_tvb,
proto_tree *packet_tree,
packet_info *pinfo _U_,
rlp_element_t *rlp,
ethereum_disc_stat_t *st _U_,
ethereum_disc_conv_t *conv _U_,
ethereum_disc_enhanced_data_t *efdata _U_) {
proto_tree *parent;
proto_item *ti;
rlp_next(packet_tvb, rlp->data_offset, rlp);
proto_tree_add_item(packet_tree, hf_ethereum_disc_topic_query_topic, packet_tvb,
rlp->data_offset, rlp->byte_length, ENC_ASCII);
// Expiration (optional)
rlp_next(packet_tvb, rlp->next_offset, rlp);
if (rlp->byte_length > 0) {
proto_tree_add_item(packet_tree, hf_ethereum_disc_topic_query_expiration, packet_tvb,
rlp->data_offset, rlp->byte_length, ENC_TIME_SECS | ENC_BIG_ENDIAN);
}
// Update conversation and enhanced frame data.
if (!PINFO_FD_VISITED(pinfo)) {
efdata->seqtype = ++conv->topicquery_count;
conv->last_topicquery_frame = pinfo->num;
conv->last_topicquery_time = pinfo->abs_ts;
}
// Sequence number of the message type.
parent = proto_tree_get_parent_tree(packet_tree);
ti = proto_tree_add_uint(parent, hf_ethereum_disc_seqtype, packet_tvb, 0, 0, efdata->seqtype);
PROTO_ITEM_SET_GENERATED(ti);
// Link the TOPIC_QUERY response.
parent = proto_tree_get_parent_tree(packet_tree);
guint32 queryref = GPOINTER_TO_UINT(wmem_map_lookup(conv->corr, GUINT_TO_POINTER(pinfo->num)));
if (queryref) {
ti = proto_tree_add_uint(parent, hf_ethereum_disc_res_ref, packet_tvb, 0, 0, queryref);
PROTO_ITEM_SET_GENERATED(ti);
}
st->is_request = TRUE;
return TRUE;
}
static int process_topic_nodes_msg(tvbuff_t *packet_tvb,
proto_tree *packet_tree,
packet_info *pinfo,
rlp_element_t *rlp,
ethereum_disc_stat_t *st,
ethereum_disc_conv_t *conv,
ethereum_disc_enhanced_data_t *efdata _U_) {
proto_tree *parent;
proto_item *ti;
rlp_next(packet_tvb, rlp->data_offset, rlp);
proto_tree_add_item(packet_tree, hf_ethereum_disc_topic_nodes_echo, packet_tvb,
rlp->data_offset, rlp->byte_length, ENC_BIG_ENDIAN);
rlp_next(packet_tvb, rlp->next_offset, rlp);
decode_nodes_list(packet_tvb, packet_tree, pinfo, rlp, st, conv, efdata);
if (!PINFO_FD_VISITED(pinfo)) {
efdata->seqtype = ++conv->nodes_count;
wmem_map_insert(conv->corr, GUINT_TO_POINTER(conv->last_topicquery_frame), GUINT_TO_POINTER(pinfo->num));
wmem_map_insert(conv->corr, GUINT_TO_POINTER(pinfo->num), GUINT_TO_POINTER(conv->last_topicquery_frame));
nstime_delta(&efdata->rt, &pinfo->fd->abs_ts, &conv->last_topicquery_time);
efdata->rq_time = conv->last_topicquery_time;
}
// Sequence number of the message type.
parent = proto_tree_get_parent_tree(packet_tree);
ti = proto_tree_add_uint(parent, hf_ethereum_disc_seqtype, packet_tvb, 0, 0, efdata->seqtype);
PROTO_ITEM_SET_GENERATED(ti);
// Link the TOPIC_QUERY request.
guint32 topicqueryref = GPOINTER_TO_UINT(wmem_map_lookup(conv->corr, GUINT_TO_POINTER(pinfo->num)));
if (topicqueryref) {
ti = proto_tree_add_uint(parent, hf_ethereum_disc_req_ref, packet_tvb, 0, 0, topicqueryref);
PROTO_ITEM_SET_GENERATED(ti);
st->has_request = TRUE;
}
// Response time.
if (!nstime_is_unset(&efdata->rt)) {
ti = proto_tree_add_time(parent, hf_ethereum_disc_rt, packet_tvb, 0, 0, &efdata->rt);
PROTO_ITEM_SET_GENERATED(ti);
}
st->is_request = FALSE;
st->rq_time = efdata->rq_time;
return TRUE;
}
static int process_topic_register_msg(tvbuff_t *packet_tvb,
proto_tree *packet_tree,
packet_info *pinfo _U_,
rlp_element_t *rlp,
ethereum_disc_stat_t *st _U_,
ethereum_disc_conv_t *conv _U_,
ethereum_disc_enhanced_data_t *efdata _U_) {
// Move to Topic List
rlp_next(packet_tvb, rlp->data_offset, rlp);
guint i = 0;
guint topic_list_end = rlp->data_offset + rlp->byte_length;
if (rlp->byte_length > 0) {
// List is not empty, move into the first element.
rlp_next(packet_tvb, rlp->data_offset, rlp);
}
while (rlp->data_offset < topic_list_end) {
i++;
proto_tree_add_item(packet_tree, hf_ethereum_disc_topic_register_topic, packet_tvb,
rlp->data_offset, rlp->byte_length, ENC_ASCII);
// Onto the next element.
rlp_next(packet_tvb, rlp->next_offset, rlp);
}
// Idx.
proto_tree_add_item(packet_tree, hf_ethereum_disc_topic_register_idx, packet_tvb,
rlp->data_offset, rlp->byte_length, ENC_BIG_ENDIAN);
// Pong
rlp_next(packet_tvb, rlp->next_offset, rlp);
proto_tree_add_item(packet_tree, hf_ethereum_disc_topic_register_pong, packet_tvb,
rlp->data_offset, rlp->byte_length, ENC_BIG_ENDIAN);
// Enhance packet info with # of topics.
char more_info[64];
g_snprintf(more_info, sizeof(more_info), " (%d topics)", i);
col_append_str(pinfo->cinfo, COL_INFO, more_info);
return TRUE;
}
/**
* Retrieves an existing conversation for this packet, or initialises a new one (and saves it).
*
* @param pinfo The packet.
* @return A ready-to-use conversation struct.
*/
static ethereum_disc_conv_t *get_conversation(packet_info *pinfo) {
conversation_t *conversation;
ethereum_disc_conv_t *ret;
conversation = find_or_create_conversation(pinfo);
ret = (ethereum_disc_conv_t *) conversation_get_proto_data(conversation, proto_ethereum);
if (!ret) {
ret = wmem_new(wmem_file_scope(), ethereum_disc_conv_t);
ret->total_count = 0;
ret->ping_count = 0;
ret->pong_count = 0;
ret->findnode_count = 0;
ret->topicquery_count = 0;
ret->nodes_count = 0;
ret->last_ping_frame = 0;
ret->last_ping_time = unset_time;
ret->last_findnode_frame = 0;
ret->last_findnode_time = unset_time;
ret->last_topicquery_frame = 0;
ret->last_topicquery_time = unset_time;
ret->corr = wmem_map_new(wmem_file_scope(), g_direct_hash, g_direct_equal);
conversation_add_proto_data(conversation, proto_ethereum, ret);
}
return ret;
}
static ethereum_disc_stat_t *init_disc_stat(void) {
ethereum_disc_stat_t *st;
st = wmem_new(wmem_packet_scope(), ethereum_disc_stat_t);
st->has_request = FALSE;
st->is_request = FALSE;
st->packet_type = UNKNOWN;
st->rq_time = unset_time;
st->node_count = 0;
return st;
}
/**
* Performs the dissection of a discovery packet.
*
* @param tvb The buffer containing the UDP datagram.
* @param pinfo The packet info.
* @param tree The protocol tree to populate.
* @param data Extra data.
* @return TRUE if successful, FALSE otherwise.
*/
static int dissect_ethereum(tvbuff_t *tvb,
packet_info *pinfo,
proto_tree *tree,
void *data _U_) {
proto_tree *ethereum_tree, *packet_tree;
proto_item *ti;
tvbuff_t *packet_tvb;
ethereum_disc_stat_t *st;
ethereum_disc_conv_t *conv;
ethereum_disc_enhanced_data_t *efdata;
const gchar *packet_type_desc;
rlp_element_t rlp;
static packet_processor *processors[] = {
[PING] = &process_ping_msg,
[PONG] = &process_pong_msg,
[FIND_NODE] = &process_findnode_msg,
[NODES] = &process_nodes_msg
};
st = init_disc_stat();
conv = get_conversation(pinfo);
col_set_str(pinfo->cinfo, COL_PROTOCOL, "Ethereum");
col_clear(pinfo->cinfo, COL_INFO);
// Build the protocol tree.
tree = proto_tree_add_item(tree, proto_ethereum, tvb, 0, -1, ENC_NA);
ethereum_tree = proto_item_add_subtree(tree, ett_ethereum_disc_toplevel);
// Message hash and signature.
proto_tree_add_item(ethereum_tree, hf_ethereum_disc_msg_hash, tvb, 0, ETHEREUM_DISC_HASH_LEN, ENC_BIG_ENDIAN);
proto_tree_add_item(ethereum_tree, hf_ethereum_disc_msg_sig, tvb, ETHEREUM_DISC_HASH_LEN,
ETHEREUM_DISC_SIGNATURE_LEN, ENC_BIG_ENDIAN);
// Packet type.
guint packet_type = tvb_get_guint8(tvb, ETHEREUM_DISC_PACKET_TYPE_IDX);
proto_tree_add_item(ethereum_tree, hf_ethereum_disc_packet_type, tvb,
ETHEREUM_DISC_PACKET_TYPE_IDX, 1, ENC_BIG_ENDIAN);
st->packet_type = (packet_type_e) packet_type;
// Packet subtree, until the end.
packet_type_desc = val_to_str(packet_type, packet_type_names, "(Unknown packet ID: %d)");
ti = proto_tree_add_string(ethereum_tree, hf_ethereum_disc_packet, tvb,
ETHEREUM_DISC_PACKET_DATA_START, -1, packet_type_desc);
packet_tree = proto_item_add_subtree(ti, ett_ethereum_disc_packetdata);
packet_tvb = tvb_new_subset_remaining(tvb, ETHEREUM_DISC_PACKET_DATA_START);
rlp_next(packet_tvb, 0, &rlp);
// Assert we have a top level RLP list.
if (rlp.type != LIST) {
return FALSE;
}
col_append_str(pinfo->cinfo, COL_INFO, "Discovery v4 message: ");
col_append_str(pinfo->cinfo, COL_INFO, packet_type_desc);
// Sanity check.
if (packet_type >= G_N_ELEMENTS(processors) || processors[packet_type] == NULL) {
return FALSE;
}
// Create a new enhanced frame if it doesn't exist.
efdata = (ethereum_disc_enhanced_data_t *) p_get_proto_data(wmem_file_scope(), pinfo, proto_ethereum, 0);
if (!efdata) {
efdata = wmem_new(wmem_file_scope(), ethereum_disc_enhanced_data_t);
efdata->seq = ++conv->total_count;
efdata->seqtype = 0;
efdata->rt = unset_time;
efdata->rq_time = unset_time;
p_add_proto_data(wmem_file_scope(), pinfo, proto_ethereum, 0, efdata);
}
ti = proto_tree_add_uint(proto_tree_get_parent_tree(packet_tree), hf_ethereum_disc_seq,
packet_tvb, 0, 0, efdata->seq);
PROTO_ITEM_SET_GENERATED(ti);
processors[packet_type](packet_tvb, packet_tree, pinfo, &rlp, st, conv, efdata);
tap_queue_packet(ethereum_tap, pinfo, st);
return TRUE;
}
static int dissect_ethereum_discv5(tvbuff_t *tvb,
packet_info *pinfo,
proto_tree *tree,
void *data _U_) {
proto_tree *ethereum_tree, *packet_tree;
proto_item *ti;
tvbuff_t *packet_tvb;
ethereum_disc_stat_t *st;
ethereum_disc_conv_t *conv;
ethereum_disc_enhanced_data_t *efdata;
const gchar *packet_type_desc;
rlp_element_t rlp;
static packet_processor *processors[] = {
[PING] = &process_ping_v5_msg,
[PONG] = &process_pong_v5_msg,
[FIND_NODE] = &process_findnode_msg,
[NODES] = &process_nodes_msg,
// FIND_NODEHASH is identical to FIND_NODE, except the length of their target field is
// different (64 for the former and 32 for the latter). By using process_findnode_msg for
// both, we avoid duplication and get correctly linking of either of them to the NODES
// response.
[FIND_NODEHASH] = &process_findnode_msg,
[TOPIC_REGISTER] = &process_topic_register_msg,
[TOPIC_QUERY] = &process_topic_query_msg,
[TOPIC_NODES] = &process_topic_nodes_msg,
};
st = init_disc_stat();
conv = get_conversation(pinfo);
col_set_str(pinfo->cinfo, COL_PROTOCOL, "Ethereum");
col_clear(pinfo->cinfo, COL_INFO);
// Build the protocol tree.
tree = proto_tree_add_item(tree, proto_ethereum, tvb, 0, -1, ENC_NA);
ethereum_tree = proto_item_add_subtree(tree, ett_ethereum_disc_toplevel);
// Message hash and signature.
proto_tree_add_item(ethereum_tree, hf_ethereum_disc_msg_sig, tvb, strlen(ETHEREUM_DISCV5_ID_STR),
ETHEREUM_DISC_SIGNATURE_LEN, ENC_BIG_ENDIAN);
// Packet type.
guint packet_type = tvb_get_guint8(tvb, ETHEREUM_DISCV5_PACKET_TYPE_IDX);
proto_tree_add_item(ethereum_tree, hf_ethereum_disc_packet_type, tvb,
ETHEREUM_DISCV5_PACKET_TYPE_IDX, 1, ENC_BIG_ENDIAN);
st->packet_type = (packet_type_e) packet_type;
// Packet subtree, until the end.
packet_type_desc = val_to_str(packet_type, packet_type_names, "(Unknown packet ID: %d)");
ti = proto_tree_add_string(ethereum_tree, hf_ethereum_disc_packet, tvb,
ETHEREUM_DISCV5_PACKET_DATA_START, -1, packet_type_desc);
packet_tree = proto_item_add_subtree(ti, ett_ethereum_disc_packetdata);
packet_tvb = tvb_new_subset_remaining(tvb, ETHEREUM_DISCV5_PACKET_DATA_START);
rlp_next(packet_tvb, 0, &rlp);
// Assert we have a top level RLP list.
if (rlp.type != LIST) {
return FALSE;
}
col_append_str(pinfo->cinfo, COL_INFO, "Discovery v5 message: ");
col_append_str(pinfo->cinfo, COL_INFO, packet_type_desc);
// Sanity check.
if (packet_type >= G_N_ELEMENTS(processors) || processors[packet_type] == NULL) {
return FALSE;
}
// Create a new enhanced frame if it doesn't exist.
efdata = (ethereum_disc_enhanced_data_t *) p_get_proto_data(wmem_file_scope(), pinfo, proto_ethereum, 0);
if (!efdata) {
efdata = wmem_new(wmem_file_scope(), ethereum_disc_enhanced_data_t);
efdata->seq = ++conv->total_count;
efdata->seqtype = 0;
efdata->rt = unset_time;
efdata->rq_time = unset_time;
p_add_proto_data(wmem_file_scope(), pinfo, proto_ethereum, 0, efdata);
}
ti = proto_tree_add_uint(proto_tree_get_parent_tree(packet_tree), hf_ethereum_disc_seq,
packet_tvb, 0, 0, efdata->seq);
PROTO_ITEM_SET_GENERATED(ti);
processors[packet_type](packet_tvb, packet_tree, pinfo, &rlp, st, conv, efdata);
tap_queue_packet(ethereum_tap, pinfo, st);
return TRUE;
}
/**
* Evaluates heuristics on a frame and performs the dissection only if there's a high probability
* that this is an Ethereum discovery message.
*
* @param tvb The buffer containing the UDP datagram.
* @param pinfo The packet info.
* @param tree The protocol tree to populate.
* @param data Extra data.
* @return TRUE if successful, FALSE otherwise.
*/
static gboolean dissect_ethereum_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_) {
// Check length.
if (tvb_captured_length(tvb) < MIN_ETHDEVP2PDISCO_LEN || tvb_captured_length(tvb) > MAX_ETHDEVP2PDISCO_LEN) {
return FALSE;
}
// https://github.com/ethereum/go-ethereum/blob/c4712bf96bc1bae4a5ad4600e9719e4a74bde7d5/p2p/discv5/udp.go#L149
gboolean is_discv5 = FALSE;
const gchar *version_prefix = tvb_get_string_enc(wmem_packet_scope(), tvb, 0, strlen(ETHEREUM_DISCV5_ID_STR), ENC_ASCII);
if (strcmp(version_prefix, ETHEREUM_DISCV5_ID_STR) == 0) {
is_discv5 = TRUE;