-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpacket-smf.c
More file actions
3366 lines (2886 loc) · 123 KB
/
packet-smf.c
File metadata and controls
3366 lines (2886 loc) · 123 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-smf.c
* Routines for Solace Message Format dissection
* Copyright 2007, Solace Corporation
*
* $Id: packet-smf.c 657 2007-07-31 20:42:07Z $
*
* 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.
*/
// NOTE: See the README.developer file for instructions on modifying the
// dissector code.
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <epan/packet.h>
#include <epan/tvbuff.h>
#include <epan/dissectors/packet-tcp.h>
#include <epan/conversation.h>
#include <epan/wmem_scopes.h>
#include <epan/ipproto.h>
#include <epan/addr_resolv.h>
#include <epan/prefs.h>
#include <epan/strutil.h>
#include <epan/reassemble.h>
#include <epan/conversation.h>
#include <epan/exceptions.h>
#include <wsutil/pint.h>
#include "packet-smf.h"
#include "smf-analysis.h"
#include "sdt-decoder.h"
#include <epan/column-info.h>
#include <epan/decode_as.h>
#include <epan/proto_data.h>
#include <epan/prefs.h>
#include <epan/uat.h>
#include <stdbool.h>
#include <epan/uat-int.h>
//#include <epan/uat_load.l>
/*
IF PROTO exposes code to other dissectors, then it must be exported
in a header file. If not, a header file is not needed at all. */
/* #include "packet-smf.h" */
/* Forward declaration we need below */
void proto_reg_handoff_smf(void);
static void smf_proto_init(void);
static void try_load_smf_subdissection_uat(void);
/* Initialize the protocol and registered fields */
static int proto_smf = -1;
static int global_smf_port = 55555;
static int global_smf_rtg_port = 55556;
static bool scan_smf_in_stream = true;
/* Header v3 */
static int hf_smf_di = -1;
static int hf_smf_ee = -1;
static int hf_smf_dto = -1;
static int hf_smf_adf = -1;
static int hf_smf_dmqe = -1;
static int hf_smf_version = -1;
static int hf_smf_uh = -1;
static int hf_smf_encap_protocol = -1;
static int hf_smf_priority = -1;
static int hf_smf_retain = -1;
static int hf_smf_acf = -1;
static int hf_smf_sni = -1;
static int hf_smf_ttl = -1;
static int hf_smf_header_len_v3 = -1;
static int hf_smf_msg_len_v3 = -1;
static int hf_smf_attachment_len = -1;
/* Header v2 */
static int hf_smf_rfu = -1;
static int hf_smf_header_len = -1;
static int hf_smf_msg_len = -1;
/* Standard Parameters */
static int hf_smf_publisherid_param = -1;
static int hf_smf_publisher_msgid_param = -1;
static int hf_smf_message_prio_param = -1;
static int hf_smf_userdata_param = -1;
static int hf_smf_message_id_param = -1;
static int hf_smf_username_param = -1;
static int hf_smf_password_param = -1;
static int hf_smf_response_param = -1;
static int hf_smf_entitlements_param = -1;
static int hf_smf_subscriber_ids_param = -1;
static int hf_smf_generic_attachment_param = -1;
static int hf_smf_binary_attachment_param = -1;
static int hf_smf_originator_address_param = -1;
static int hf_smf_delivery_mode_param = -1;
static int hf_smf_ad_msg_id_param = -1;
static int hf_smf_ad_prev_msg_id_param = -1;
static int hf_smf_ad_redelivered_param = -1;
static int hf_smf_ad_ttl_deprecated_param = -1;
static int hf_smf_ad_ttl_param = -1;
static int hf_smf_retryable_error = -1;
static int hf_smf_message_contents_summary_param = -1;
static int hf_smf_ad_flow_id_param = -1;
static int hf_assuredctrl_flow_id_hidden_param = -1;
static int hf_smf_topic_name_param = -1;
static int hf_smf_ad_flow_redelivered_flag = -1;
static int hf_smf_point_of_entry_unique_id_client_id = -1;
static int hf_smf_point_of_entry_unique_id_ingress_vrid_hash = -1;
static int hf_smf_point_of_entry_unique_id_vpn_name_hash = -1;
static int hf_smf_deliver_always_only = -1;
static int hf_smf_sequence_number_param = -1;
/* Lightweight Parameters */
static int hf_smf_correlation_tag_param = -1;
static int hf_smf_topic_name_offset_param = -1;
static int hf_smf_queue_name_offset_param = -1;
static int hf_smf_ack_immediately_tag_param = -1;
static int hf_smf_header_extension_param = -1;
static int hf_smf_header_extension_param_odf = -1;
static int hf_smf_header_extension_param_fac = -1;
static int hf_smf_header_extension_param_rffu = -1;
/* Extended Parameters */
static int hf_smf_gss_api_token_param = -1;
static int hf_smf_assured_delivery_ack_message_id_param = -1;
static int hf_smf_assured_delivery_trans_id_param = -1;
static int hf_smf_assured_delivery_trans_sr_flag = -1;
static int hf_smf_assured_delivery_trans_pr_flag = -1;
static int hf_smf_assured_delivery_spooler_unique_id = -1;
static int hf_smf_assured_delivery_rep_mate_ack_id = -1;
static int hf_smf_assured_delivery_redelivery_count = -1;
static int hf_smf_assured_delivery_eligible_time = -1;
static int hf_smf_assured_delivery_queued_for_redelivery = -1;
static int hf_smf_oauth_issuer_identifier = -1;
static int hf_smf_openid_connect_id_token = -1;
static int hf_smf_oauth_access_token = -1;
static int hf_smf_trace_span_transport_context_param = -1;
/* Trace Span Context Internal Structure */
static int hf_smf_trace_span_context_struct_version = -1;
static int hf_smf_trace_span_context_struct_sampled_flag = -1;
static int hf_smf_trace_span_context_struct_rfu_flag = -1;
static int hf_smf_trace_span_context_struct_trace_id = -1;
static int hf_smf_trace_span_context_struct_span_id = -1;
static int hf_smf_trace_span_context_struct_injection_standard = -1;
static int hf_smf_trace_span_context_struct_rfu = -1;
static int hf_smf_trace_span_context_struct_trace_state_length = -1;
static int hf_smf_trace_span_context_struct_trace_state = -1;
/* Message Contents Summary Elements */
static int hf_smf_binary_metadata_param = -1;
/* Fragments, Reassembly, Perf Tool */
static int hf_smf_attachment_tooldata = -1;
static int hf_smf_fragment_count = -1;
static int hf_smf_reassembled_length = -1;
static int hf_smf_reassembled_data = -1;
static int hf_smf_fragments = -1;
static int hf_smf_fragment = -1;
static int hf_smf_fragment_overlap = -1;
static int hf_smf_fragment_overlap_conflict = -1;
static int hf_smf_fragment_multiple_tails = -1;
static int hf_smf_fragment_too_long_fragment = -1;
static int hf_smf_fragment_error = -1;
static int hf_smf_reassembled_in = -1;
/*Miscellaneous*/
static int hf_smf_unknown_param = -1;
static int hf_smf_payload = -1;
static int hf_smf_attachment = -1;
static int hf_smf_attachment_sdt = -1;
static int hf_smf_binary_metadata = -1;
static int hf_smf_pad_byte = -1;
static int hf_smf_orig_router_port_param = -1;
static int hf_smf_dest_router_port_param = -1;
static int hf_smf_cidlist_param = -1;
static int hf_smf_originator_port_param = -1;
static int hf_smf_destination_address_param = -1;
static int hf_smf_destination_port_param = -1;
static int hf_smf_topic_name_length_param = -1;
static int hf_smf_queue_name_length_param = -1;
static int hf_smf_metadata_param = -1;
static int hf_smf_xml_payload_param = -1;
static int hf_smf_cid_param = -1;
static int hf_smf_cidlist_size_param = -1;
/* Expert info */
static expert_field ei_trace_span_unsupported_version = EI_INIT;
static expert_field ei_trace_span_invalid_length = EI_INIT;
/* desegmentation of SMF over TCP */
static bool smf_desegment = true;
static bool extractAdMsgId = false;
#if 0
/* Global sample preference ("controls" display of numbers) */
static bool gPREF_HEX = false;
#endif
/* Initialize the subtree pointers */
static int ett_smf = -1;
static int ett_message_contents_summary = -1;
static int ett_consumer_id_list = -1;
static int ett_smf_fragments = -1;
static int ett_smf_fragment = -1;
static int ett_attachment_sdt = -1;
static int ett_trace_span_transport_context = -1;
static const fragment_items smf_frag_items =
{
&ett_smf_fragment,
&ett_smf_fragments,
&hf_smf_fragments,
&hf_smf_fragment,
&hf_smf_fragment_overlap,
&hf_smf_fragment_overlap_conflict,
&hf_smf_fragment_multiple_tails,
&hf_smf_fragment_too_long_fragment,
&hf_smf_fragment_error,
&hf_smf_fragment_count,
&hf_smf_reassembled_in,
&hf_smf_reassembled_length,
&hf_smf_reassembled_data,
"fragments"
};
/* Value to string conversion tables */
static const value_string uhnames[] =
{
{ 0, "Ignore and discard message" },
{ 1, "Ignore and discard message" },
{ 2, "Ignore and return error" },
{ 3, "Ignore and return error" },
{ 0, NULL } };
/* Minimum SMF Header length */
#define MIN_SMF_HEADER_LEN 12
#define MAX_SMF_HEADER_LEN 100000 // There is no such thing as max header length. But try to reduce error.
/* Maximum SMF Message length */
#define MAX_SMF_MESSAGE_BODY_LEN 64000000
/* Length Check */
#define SMF_LENGTH_CHECK 1
#define SMF_LENGTH_CHECK_NO 0
/* All the SMF-encapsulated protocols we support */
#define SMF_CSPF 0x01
#define SMF_CSMP 0x02
#define SMF_PUBMSG 0x03
#define SMF_XMLLINK 0x04
#define SMF_WSE 0x05
#define SMF_SEMP 0x06
#define SMF_SUBCTRL 0x07
#define SMF_PUBCTRL 0x08
#define SMF_ASSUREDCTRL 0x09
#define SMF_KEEPALIVE 0x0a
#define SMF_KEEPALIVE_V2 0x0b
#define SMF_CLIENTCTRL 0x0c
#define SMF_TRMSG 0x0d
#define SMF_JNDI 0x0e
#define SMF_SMP 0x0f
#define SMF_SMRP 0x10
#define SMF_ENCAP_SMF 0x11
#define SMF_ENCAP_RV 0x12
#define SMF_ASSUREDCTRL_PASSTHRU 0x13
/* Please set the last known protocol to SMF_PROTOCOL_MAX for validation purposes */
#define SMF_PROTOCOL_MAX SMF_ASSUREDCTRL_PASSTHRU
/* Standard Parameter Types */
#define STANDARD_PARAM_PUBLISHER_ID 0x01
#define STANDARD_PARAM_PUBLISHER_MSG_ID 0x02
#define STANDARD_PARAM_MESSAGE_PRIORITY 0x03
#define STANDARD_PARAM_USER_DATA 0x04
#define STANDARD_PARAM_MESSAGE_ID 0x05
#define STANDARD_PARAM_USERNAME 0x06
#define STANDARD_PARAM_PASSWORD 0x07
#define STANDARD_PARAM_RESPONSE 0x08
#define STANDARD_PARAM_ENTITLEMENT_LIST 0x09
#define STANDARD_PARAM_SUBSCRIBER_ID_LIST 0x0a
#define STANDARD_PARAM_GENERIC_ATTACHMENT 0x0b
#define STANDARD_PARAM_BINARY_ATTACHMENT 0x0c
#define STANDARD_PARAM_ORIGINATOR_ADDRESS 0x0d
#define STANDARD_PARAM_DESTINATION_ADDRESS_LIST 0x0e
#define STANDARD_PARAM_DESTINATION_ADDRESS_AND_PORT_LIST 0x0f
#define STANDARD_PARAM_DEELIVERY_MODE 0x10
#define STANDARD_PARAM_AD_MESSAGE_ID 0x11
#define STANDARD_PARAM_AD_PREV_MESSAGE_ID 0x12
#define STANDARD_PARAM_AD_REDELIVERED_FLAG 0x13
#define STANDARD_PARAM_AD_TTL_DEPRICATED 0x14
#define STANDARD_PARAM_AD_DESTINATION_SET 0x15
#define STANDARD_PARAM_MESSAGE_CONTENTS_SUMMARY 0x16
#define STANDARD_PARAM_AD_FLOW_ID 0x17
#define STANDARD_PARAM_TOPIC_NAME 0x18
#define STANDARD_PARAM_AD_FLOW_REDELIVERED_FLAG 0x19
#define STANDARD_PARAM_POINT_OF_ENTRY_UNIQUE_ID 0x1a
#define STANDARD_PARAM_DELIVERY_ALWAYS_ONLY 0x1b
#define STANDARD_PARAM_AD_TTL 0x1c
#define STANDARD_PARAM_RETRYABLE_ERROR 0x1d
#define STANDARD_PARAM_SEQUENCE_NUMBER 0x1e
#define STANDARD_PARAM_EXTENDED_TYPE_STREAM 0x1f
/* Lightweight Parameter Types */
#define LIGHTWEIGHT_PARAM_CORRELATION_TAG 0x00
#define LIGHTWEIGHT_PARAM_TOPIC_NAME_OFFSET 0x01
#define LIGHTWEIGHT_PARAM_QUEUE_NAME_OFFSET 0x02
#define LIGHTWEIGHT_PARAM_ACK_IMMEDIATELY 0x03
#define LIGHTWEIGHT_PARAM_HEADER_EXTENSION 0x04
/* Extended Parameters */
#define SMF_EXTENDED_PARAM_GSS_API_TOKEN 0x28
#define SMF_EXTENDED_PARAM_ASSURED_DELIVERY_ACK_MESSAGE_ID 0x29
#define SMF_EXTENDED_PARAM_ASSURED_DELIVERY_TRANSACTION_ID 0x2a
#define SMF_EXTENDED_PARAM_ASSURED_DELIVERY_TRANSACTION_FLAGS 0x2b
#define SMF_EXTENDED_PARAM_ASSURED_DELIVERY_SPOOLER_UNIQUE_ID 0x2c
#define SMF_EXTENDED_PARAM_ASSURED_DELIVERY_REP_MATE_ACK_MES_ID 0x2d
#define SMF_EXTENDED_PARAM_ASSURED_DELIVERY_REDELIVERY_COUNT 0x2e
#define SMF_EXTENDED_PARAM_ASSURED_DELIVERY_ELIGIBLE_TIME 0x32
#define SMF_EXTENDED_PARAM_ASSURED_DELIVERY_QUEUED_FOR_REDELIVERY 0x33
#define SMF_EXTENDED_PARAM_OAUTH_ISSUE_IDENTIFIER 0X2f
#define SMF_EXTENDED_PARAM_OPENID_CONNECT_ID_TOKEN 0x30
#define SMF_EXTENDED_PARAM_OAUTH_ACCESS_TOKEN 0x31
#define SMF_EXTENDED_PARAM_TRACE_SPAN_TRANSPORT_CONTEXT 0x36
#define CSV_WRITE_LONG_VALUE(condition, eol, fileName, tvb, offset) \
if (condition) {\
FILE* output = fopen(fileName, "a+");\
fprintf(output, "%d%s", tvb_get_ntohl(tvb, offset), eol? "\n":",");\
fclose(output);\
}
#define CSV_WRITE_SHORT_VALUE(condition, eol, fileName, tvb, offset) \
if (condition) {\
FILE* output = fopen(fileName, "a+");\
fprintf(output, "%d%s", tvb_get_ntohs(tvb, offset), eol? "\n":",");\
fclose(output);\
}
static const char* default_subdissector_uat_topic = "_telemetry/broker/trace/receive/v1";
static const char* default_subdissector_uat_protocol = "protobuf";
static const char* default_subdissector_uat_extra_data = "message,solace.messaging.proto.broker.trace.receive.v1.SpanData";
/* Maps protocol numbers to protocol names */
static const value_string protocolnames[] =
{
{ SMF_CSPF, "CSPF" },
{ SMF_CSMP, "CSMP" },
{ SMF_PUBMSG, "PubMsg" },
{ SMF_XMLLINK, "XmlLink" },
{ SMF_WSE, "WSE" },
{ SMF_SEMP, "SEMP" },
{ SMF_SUBCTRL, "SubCtrl" },
{ SMF_PUBCTRL, "PubCtrl" },
{ SMF_ASSUREDCTRL, "AssuredCtrl" },
{ SMF_KEEPALIVE, "KeepAlive" },
{ SMF_KEEPALIVE_V2, "KeepAliveV2" },
{ SMF_CLIENTCTRL, "ClientCtrl" },
{ SMF_TRMSG, "TrMsg" },
{ SMF_JNDI, "JNDI" },
{ SMF_SMP, "SMP" },
{ SMF_SMRP, "SMRP" },
{ SMF_ENCAP_SMF, "SMF-Encap SMF: " },
{ SMF_ENCAP_RV, "SMF-Encap RV" },
{ SMF_ASSUREDCTRL_PASSTHRU, "AssuredCtrl PassThru" },
{ 0, NULL } };
/* Maps delivery mode values to names */
static const value_string deliverymodenames[] =
{
{ 0, "non-persistent" },
{ 1, "persistent" },
{ 2, "direct" },
{ 3, "express" },
{ 0, NULL } };
static const value_string trace_span_context_sampled_flag_names[] =
{
{ 0, "Not Sampled" },
{ 1, "Sampled" },
{ 2, "Debug" },
{ 0, NULL } };
static const value_string trace_span_context_injection_standard_names[] =
{
{ 0, "SMF" },
{ 1, "W3C" },
{ 0, NULL } };
/* A structure that keeps track of information parsed from SMF parameters */
struct param_info_t
{
bool is_response;
int metadata_start;
int metadata_length;
int xml_payload_start;
int xml_payload_length;
int binary_payload_start;
int binary_payload_length;
int attachment_start;
int attachment_length;
int cidlist_start;
int cidlist_length;
int binary_metadata_start;
int binary_metadata_length;
int32_t correlation_tag;
bool ack_immediately_tag;
};
/* Dissector handles for external dissectors */
static dissector_handle_t xml_handle;
static dissector_handle_t pubctrl_handle;
static dissector_handle_t subctrl_handle;
static dissector_handle_t xmllink_handle;
static dissector_handle_t assuredctrl_handle;
static dissector_handle_t smp_handle;
static dissector_handle_t smrp_handle;
static dissector_handle_t clientctrl_handle;
static dissector_handle_t bm_handle;
static dissector_handle_t mama_payload_handle;
static dissector_handle_t protobuf_handle;
static dissector_handle_t smf_handle;
/* General Reassembly */
static reassembly_table smf_gen_reassembly_table;
static int hf_smf_gen_fragments = -1;
static int hf_smf_gen_fragment = -1;
static int hf_smf_gen_fragment_overlap = -1;
static int hf_smf_gen_fragment_overlap_conflicts = -1;
static int hf_smf_gen_fragment_multiple_tails = -1;
static int hf_smf_gen_fragment_too_long_fragment = -1;
static int hf_smf_gen_fragment_error = -1;
static int hf_smf_gen_fragment_count = -1;
static int hf_smf_gen_reassembled_in = -1;
static int hf_smf_gen_reassembled_length = -1;
//static int hf_smf_gen_segment_data = -1;
static int ett_smf_gen_fragment = -1;
static int ett_smf_gen_fragments = -1;
//static bool isFragmented = false;
//static fragment_head* fd_head = NULL;
//static bool more_frags = false;
//static int total_msg_length = -1;
static const fragment_items smf_gen_frag_items = {
/* Fragment subtrees */
&ett_smf_gen_fragment,
&ett_smf_gen_fragments,
/* Fragment fields */
&hf_smf_gen_fragments,
&hf_smf_gen_fragment,
&hf_smf_gen_fragment_overlap,
&hf_smf_gen_fragment_overlap_conflicts,
&hf_smf_gen_fragment_multiple_tails,
&hf_smf_gen_fragment_too_long_fragment,
&hf_smf_gen_fragment_error,
&hf_smf_gen_fragment_count,
/* Reassembled in field */
&hf_smf_gen_reassembled_in,
/* Reassembled length field */
&hf_smf_gen_reassembled_length,
/* Reassembled data field */
NULL,
/* Tag */
"SMF Gen fragments"
};
struct bd_reas_info_t
{
bool inProgress;
int seqNum;
int pduLength;
int curLength;
};
typedef enum _smf_attachment_type {
_smf_attachment_type_none = 0,
_smf_attachment_type_sdt,
_smf_attachment_type_openmama_payload,
} _smf_attachment_type_t;
static struct bd_reas_info_t bdReasInfo[MAX_BD_CHANNEL + 1];
/* User Access Table for subdissection based off topic */
static char* topic_name;
typedef enum {
MATCH_CRITERIA_EQUAL,
MATCH_CRITERIA_CONTAINS,
MATCH_CRITERIA_STARTS_WITH,
MATCH_CRITERIA_ENDS_WITH,
MATCH_CRITERIA_REGEX
} smf_subdissection_match_criteria_t;
static const value_string smf_subdissection_match_criteria[] = {
{ MATCH_CRITERIA_EQUAL, "Equal to" },
{ MATCH_CRITERIA_CONTAINS, "Contains" },
{ MATCH_CRITERIA_STARTS_WITH, "Starts with" },
{ MATCH_CRITERIA_ENDS_WITH, "Ends with" },
{ MATCH_CRITERIA_REGEX, "Regular Expression" },
{ 0, NULL }
};
typedef struct {
smf_subdissection_match_criteria_t match_criteria;
char* topic_pattern;
GRegex* topic_regex;
char* payload_proto_name;
dissector_handle_t payload_proto;
char* proto_more_info;
} smf_subdissection_uat_entry_t;
static smf_subdissection_uat_entry_t* smf_subdissection_uat_entries = NULL;
static unsigned int num_smf_subdissection_uat_entries;
static bool smf_subdissection_uat_loaded = 0;
static uat_t* smf_subdissection_uat = NULL;
static void* smf_subdissection_uat_entry_copy_cb(void* dest, const void* orig, size_t len _U_)
{
const smf_subdissection_uat_entry_t* o = (const smf_subdissection_uat_entry_t*)orig;
smf_subdissection_uat_entry_t* d = (smf_subdissection_uat_entry_t*)dest;
d->match_criteria = o->match_criteria;
d->topic_pattern = g_strdup(o->topic_pattern);
d->payload_proto_name = g_strdup(o->payload_proto_name);
d->payload_proto = o->payload_proto;
d->proto_more_info = g_strdup(o->proto_more_info);
return d;
}
static bool smf_subdissection_uat_entry_update_cb(void* record, char** error)
{
smf_subdissection_uat_entry_t* u = (smf_subdissection_uat_entry_t*)record;
if (u->topic_pattern == NULL || strlen(u->topic_pattern) == 0) {
*error = g_strdup("Missing topic pattern");
return false;
}
if (u->payload_proto_name == NULL || strlen(u->payload_proto_name) == 0) {
*error = g_strdup("Missing payload protocol");
return false;
}
if (u->match_criteria == MATCH_CRITERIA_REGEX) {
u->topic_regex = g_regex_new(u->topic_pattern, (GRegexCompileFlags)G_REGEX_OPTIMIZE, (GRegexMatchFlags)0, NULL);
if (!u->topic_regex) {
*error = g_strdup_printf("Invalid regex: %s", u->topic_pattern);
return false;
}
}
return true;
}
static void smf_subdissection_uat_entry_free_cb(void* record)
{
smf_subdissection_uat_entry_t* u = (smf_subdissection_uat_entry_t*)record;
g_free(u->topic_pattern);
if (u->topic_regex) {
g_regex_unref(u->topic_regex);
}
g_free(u->payload_proto_name);
g_free(u->proto_more_info);
}
static const smf_subdissection_uat_entry_t*
get_subdissector_from_uat(const char* topic)
{
smf_subdissection_uat_entry_t* uat_entry = NULL;
size_t topic_str_len;
size_t topic_pattern_len;
bool match_found = false;
for (unsigned int i = 0; i < num_smf_subdissection_uat_entries; i++) {
uat_entry = &smf_subdissection_uat_entries[i];
switch (uat_entry->match_criteria) {
case MATCH_CRITERIA_EQUAL:
match_found = (strcmp(topic, uat_entry->topic_pattern) == 0);
break;
case MATCH_CRITERIA_CONTAINS:
match_found = (strstr(topic, uat_entry->topic_pattern) != NULL);
break;
case MATCH_CRITERIA_STARTS_WITH:
topic_str_len = strlen(topic);
topic_pattern_len = strlen(uat_entry->topic_pattern);
match_found = ((topic_str_len >= topic_pattern_len) &&
(strncmp(topic, uat_entry->topic_pattern, topic_pattern_len) == 0));
break;
case MATCH_CRITERIA_ENDS_WITH:
topic_str_len = strlen(topic);
topic_pattern_len = strlen(uat_entry->topic_pattern);
match_found = ((topic_str_len >= topic_pattern_len) &&
(strcmp(topic + (topic_str_len - topic_pattern_len), uat_entry->topic_pattern) == 0));
break;
case MATCH_CRITERIA_REGEX:
if (uat_entry->topic_regex) {
GMatchInfo* match_info = NULL;
g_regex_match(uat_entry->topic_regex, topic, (GRegexMatchFlags)0, &match_info);
match_found = g_match_info_matches(match_info);
g_match_info_free(match_info);
}
break;
default:
/* Unknown match criteria */
break;
}
if (match_found) {
return uat_entry;
}
}
return NULL;
}
static int
smf_call_subdissector(const smf_subdissection_uat_entry_t* subdissector, tvbuff_t* next_tvb, packet_info* pinfo, proto_tree* tree)
{
return call_dissector_only(subdissector->payload_proto, next_tvb, pinfo, tree, subdissector->proto_more_info);
}
UAT_VS_DEF(smf_subdissection, match_criteria, smf_subdissection_uat_entry_t, smf_subdissection_match_criteria_t, MATCH_CRITERIA_EQUAL, "Equal to")
UAT_CSTRING_CB_DEF(smf_subdissection, topic_pattern, smf_subdissection_uat_entry_t)
UAT_DISSECTOR_DEF(smf_subdissection, payload_proto, payload_proto, payload_proto_name, smf_subdissection_uat_entry_t)
UAT_CSTRING_CB_DEF(smf_subdissection, proto_more_info, smf_subdissection_uat_entry_t)
/* reassembly table used for calls into reassemble.h */
static reassembly_table reasTable;
static dissector_table_t smf_payload_dissector_table;
/* This is a helper function to prevent the sub-dissector from change to the protocol_column caused by the dissector. */
static int call_dissector_no_protocol_change(dissector_handle_t handle, tvbuff_t *tvb,
packet_info *pinfo, proto_tree *tree) {
int rc;
col_set_writable(pinfo->cinfo, COL_PROTOCOL, false);
rc = call_dissector(handle, tvb, pinfo, tree);
col_set_writable(pinfo->cinfo, COL_PROTOCOL, true);
return rc;
}
/* Initialise the reassembly table */
static void smf_reas_init(void)
{
int i;
reassembly_table_init(&reasTable,
&addresses_reassembly_table_functions);
for (i = 0; i <= MAX_BD_CHANNEL; i++)
{
bdReasInfo[i].inProgress = false;
bdReasInfo[i].seqNum = 0;
bdReasInfo[i].pduLength = 0;
bdReasInfo[i].curLength = 0;
}
}
/*
static bool smf_reas_in_progress(int bdChannel)
{
if (bdChannel > MAX_BD_CHANNEL)
return false;
return bdReasInfo[bdChannel].inProgress;
}
*/
static void stop_smf_reas(int bdChannel)
{
if (bdChannel > MAX_BD_CHANNEL)
return;
bdReasInfo[bdChannel].inProgress = false;
bdReasInfo[bdChannel].seqNum = 0;
return;
}
static void start_smf_reas(tvbuff_t *tvb, packet_info *pinfo _U_, int bdChannel,
int pduLength)
{
if (bdChannel > MAX_BD_CHANNEL)
return;
// if (!tvb || !pinfo) return; // get rid of warning...
bdReasInfo[bdChannel].inProgress = true;
bdReasInfo[bdChannel].seqNum = 0;
bdReasInfo[bdChannel].curLength = tvb_reported_length_remaining(tvb, 0);
bdReasInfo[bdChannel].pduLength = pduLength;
return;
}
static bool smf_reas_add_fragment(int bdChannel, tvbuff_t *tvb,
packet_info *pinfo _U_, int * seqNum)
{
if (bdChannel > MAX_BD_CHANNEL)
return false;
bdReasInfo[bdChannel].seqNum += 1;
*seqNum = bdReasInfo[bdChannel].seqNum;
bdReasInfo[bdChannel].curLength += tvb_reported_length_remaining(tvb, 0);
return (bdReasInfo[bdChannel].curLength < bdReasInfo[bdChannel].pduLength);
}
static int get_smf_bd_channel(packet_info *pinfo)
{
uint16_t bdchannel;
uint8_t direction;
void *bdchannelPtr;
if (!pinfo)
return -1;
if (pinfo->dl_src.len != 6)
return -1;
/* Channel number is least significant 10 bits of Ethernet source address */
// return ((uint8_t*)(pinfo->dl_src.data)[5]) + ((uint8_t*)((pinfo->dl_src.data)[4] & 0x3) << 8);
bdchannelPtr = &bdchannel;
memcpy(bdchannelPtr, (char*) (pinfo->dl_src.data) + 4, 2);
bdchannel = pntohu16(&bdchannel);
bdchannel &= 0x3ff;
memcpy(&direction, (char*) (pinfo->dl_src.data) + 3, 1);
return (direction == 0xcc) ? bdchannel : bdchannel | 0x400;
}
/* if we return 0, it means the current PDU is deferred until we
* get the next packet.
* If we return 1 (or anything less then the fix offset,
* it means the current PDU is an error and we will be marked as error
* and we move on to the next packet.
* If everything is OK return the length of the smf message
*/
static uint32_t test_smf(tvbuff_t *tvb, packet_info* pinfo, int offset)
{
// If the remaining length is less then MIN_SMF_HEADER_LEN, we do not have enough to test
int remainingLength = tvb_captured_length(tvb) - offset;
if (remainingLength < MIN_SMF_HEADER_LEN)
{
if (pinfo->can_desegment) {
return 0;
} else {
// Less than MIN_SMF_HEADER_LEN bytes of data, just junk it
return 1;
}
}
// Check SMF version
uint8_t firstByte = tvb_get_uint8(tvb, offset);
if ((firstByte & 0x07) != 0x03)
{
return 1;
}
// Check protocol
uint8_t secondByte = tvb_get_uint8(tvb, offset+1);
uint8_t smfProtocol = secondByte & 0x3f;
if (smfProtocol > SMF_PROTOCOL_MAX)
{
return 1;
}
// Detect obsolete protocols
switch (smfProtocol) {
case SMF_CSMP:
case SMF_PUBMSG:
case SMF_XMLLINK:
case SMF_WSE:
case SMF_SUBCTRL:
case SMF_PUBCTRL:
case SMF_KEEPALIVE:
return 1;
default:
break;
}
uint32_t headerlen = tvb_get_ntohl(tvb, offset + 4);
uint32_t msglen = tvb_get_ntohl(tvb, offset + 8);
if (headerlen < MIN_SMF_HEADER_LEN) {
return 1;
}
if (headerlen > MAX_SMF_HEADER_LEN) {
return 1;
}
if (headerlen > MAX_SMF_MESSAGE_BODY_LEN) {
return 1;
}
if (headerlen > msglen) {
// Something is definitely wrong with the message
return 1;
}
if ((msglen - headerlen) > MAX_SMF_MESSAGE_BODY_LEN) {
// msgLen - headerlen is the size of the message body.
// The message is too big.
return 1;
}
return msglen;
}
/* Determine the total length of an SMF packet, given the first 8 or 12 bytes */
static unsigned int get_smf_pdu_len(packet_info* inf, tvbuff_t *tvb, int offset, void *randomPointer _U_)
{
/* msglen initialze to 1 because
* if we return 0, it means the current PDU is deferred until we
* get the next packet.
* If we return 1 (or anything less then the fix offset,
* it means the current PDU is an error and we will be marked as error
* and we move on to the next packet.
*/
uint32_t msglen = test_smf(tvb, inf, offset);
if (msglen == 1) {
// Packet is not valid. One possibility is that the packet capture not done at from the beginning.
// As SMF could start somewhere inside a TCP packet, we look for the next potential starting point
if (scan_smf_in_stream) {
unsigned int captured_length_remaining = tvb_ensure_captured_length_remaining(tvb, offset);
// Start from the MIN_SMF_HEADER_LEN byte. Returning anything less than MIN_SMF_HEADER_LEN is an error
uint32_t index = offset + MIN_SMF_HEADER_LEN;
uint32_t found = 0;
// The reason for not checking the last 12 bytes of the packet is because it would trigger some
// other errors in decoding. The exact reason has not been studied.
while (!found && ((index + MIN_SMF_HEADER_LEN) < captured_length_remaining) ) {
if (test_smf(tvb, inf, index) != 1) {
// Found a good starting point. Indicate that our current smf message ends there.
msglen = index;
found = 1;
break;
}
index++;
}
}
}
/* Eliminate compiler warning */
(void) &inf;
return msglen;
}
/* Add a base-64 encoded string to the tree */
static void smf_proto_add_base64_string(proto_tree *tree, packet_info *pinfo, int id, tvbuff_t *tvb,
int offset, int size)
{
char* str;
str = tvb_get_string_enc(NULL, tvb, offset, size, ENC_ASCII);
if (size > 1) {
if (strlen(str) > 1) {
gsize len = size; // This is for type conversion, g_base64_decode_inplace wants gsize for len
g_base64_decode_inplace(str, &len);
str[len] = '\0'; // We now have a base64 decode string, let us null terminate it.
size = len;
} else {
g_print("invalid base64 string found in packet %d, strlen is < 1", pinfo->fd->num);
}
}
proto_tree_add_string(tree, id, tvb, offset, size, str);
}
/* Add an SMF username to the tree */
static void smf_proto_add_username_item(proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb,
int offset, int size)
{
smf_proto_add_base64_string(tree, pinfo, hf_smf_username_param, tvb, offset, size);
}
/* Add an SMF password to the tree */
static void smf_proto_add_password_item(proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb,
int offset, int size)
{
smf_proto_add_base64_string(tree, pinfo, hf_smf_password_param, tvb, offset, size);
}
/* Add an SMF response to the tree */
static void smf_proto_add_response_item(proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb,
int offset, int size)
{
char* buffer;
uint32_t code;
char* str;
/* Get the response code and string */
code = tvb_get_ntohl(tvb, offset);
str = tvb_get_string_enc(NULL, tvb, offset + 4, size - 4, ENC_ASCII);
/* Format it like "200 OK" */
buffer = (char*)wmem_alloc(pinfo->pool, 300);
g_snprintf(buffer, 300, "%d %s", code, str);
/* Add the string to the tree */
proto_tree_add_string(tree, hf_smf_response_param, tvb, offset, size,
buffer);
}
/* Add a router id (addr, port) to the tree */
/*
static void smf_proto_add_router_id_item(proto_tree *tree, int id1, int id2,
tvbuff_t *tvb, int offset)
{
proto_tree_add_item(tree, id1, tvb, offset + 3, 4, false);
proto_tree_add_item(tree, id2, tvb, offset + 7, 2, false);
}
*/
/* Add an list of UINT16s to the tree */
static void smf_proto_add_uint16_list_item(proto_tree *tree, packet_info *pinfo,
int id, tvbuff_t *tvb, int offset, int size)
{
int i;
char* buffer;
uint16_t list_item;
buffer = (char*)wmem_alloc(pinfo->pool, 510);
buffer[0] = '\0';
for (i = 0; i < size; i += 2)
{
list_item = tvb_get_ntohs(tvb, offset + i);
g_snprintf(buffer, 510, "%s %d", buffer, list_item);
}
proto_tree_add_string(tree, id, tvb, offset, size, buffer);
}
/* Add an entitlement list to the tree */
static void smf_proto_add_enttl_list_item(proto_tree *tree, packet_info *pinfo,
tvbuff_t *tvb, int offset, int size)
{
smf_proto_add_uint16_list_item(tree, pinfo, hf_smf_entitlements_param, tvb, offset,
size);
}
/* Add a subscriber id list to the tree */
static void smf_proto_add_subid_list_item(proto_tree *tree, packet_info *pinfo,
tvbuff_t *tvb, int offset, int size)
{
smf_proto_add_uint16_list_item(tree, pinfo, hf_smf_subscriber_ids_param, tvb,
offset, size);
}
/* Add a list of 32-bit items to the tree */
static void smf_proto_add_uint32_list_item(proto_tree *tree, int id,
tvbuff_t * tvb, int offset, int size)
{
int i;
for (i = 0; i < size; i += 4)
{
proto_tree_add_item(tree, id, tvb, offset + i, 4, false);
}
}
static void smf_proto_add_destination_list_item(proto_tree *tree, tvbuff_t *tvb,
int offset, int size)
{
smf_proto_add_uint32_list_item(tree, hf_smf_destination_address_param, tvb,
offset, size);
}
static void smf_proto_add_cid_list_item(proto_tree *tree, tvbuff_t *tvb,
int offset, int size)
{
proto_tree* sub_tree;
proto_item* item;
item = proto_tree_add_item(tree, hf_smf_cidlist_param, tvb, offset, size,
false);