aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-log3gpp.c
blob: 5404c4f00b86beddfff7f3da5c5c5668c79fc949 (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
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
/* packet-log3gpp.c
 * Routines for dissecting phone logs containing 3GPP protocol messages.
 * Copyright 2008, Vincent Helfre
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>

#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/proto_data.h>

#include <wsutil/strtoi.h>

#include "packet-mac-lte.h"
#include "packet-pdcp-lte.h"
#include "packet-rlc-lte.h"

#define FD1 0
#define REL8 1

void proto_register_log3gpp(void);
void proto_reg_handoff_log3gpp(void);

/* Protocol and registered fields. */
static int proto_log3gpp = -1;


static int hf_log3gpp_timestamp = -1;
static int hf_log3gpp_protocol = -1;
static int hf_log3gpp_direction = -1;
static int hf_log3gpp_dissector_option = -1;
static int hf_log3gpp_unparsed_data = -1;
static int hf_log3gpp_dissected_length = -1;

/* Protocol subtree. */
static int ett_log3gpp = -1;

/* Variables used to select a version for RRC and NAS */
static int lte_rrc_prot_version = REL8;
static int nas_eps_prot_version = REL8;

static const enum_val_t lte_rrc_dissector_version[] = {
  {"FD1", "FD1", FD1},
  {"Rel8 dec 2008", "Rel8 dec 2008", REL8}, /* Add new dissector version after */
  {NULL, NULL, -1}
};

static const enum_val_t nas_eps_dissector_version[] = {
  {"FD1", "FD1", FD1},
  {"Rel8 dec 2008", "Rel8 dec 2008", REL8}, /* Add new dissector version after */
  {NULL, NULL, -1}
};

typedef enum packet_direction_t
{
    UPLINK,
    DOWNLINK
} packet_direction_t;

static const value_string direction_vals[] = {
    { 0,   "Uplink" },
    { 1,   "Downlink" },
    { 0,   NULL },
};
/* Pseudo header functions*/
typedef gboolean (*pseudo_hdr_func_ptr_t) (char *, packet_info *pinfo, guint16, packet_direction_t);

static gboolean lte_mac_pseudo_hdr(char *, packet_info *pinfo, guint16, packet_direction_t);
static gboolean lte_rlc_pseudo_hdr(char *, packet_info *pinfo, guint16, packet_direction_t);
static gboolean lte_pdcp_pseudo_hdr(char *, packet_info *pinfo, guint16, packet_direction_t);

typedef struct
{
  const char * protocol_name;
  const char * ul_dissector_name;
  const char * dl_dissector_name;
  dissector_handle_t ul_dissector_handle;
  dissector_handle_t dl_dissector_handle;
  pseudo_hdr_func_ptr_t hdr_process;
} lookup_dissector_element_t;

/* Look up table for protocol name /dissector: should be in alphabetic order!!!
   the purpose is to match a protocol name with a dissector,
   and to store the dissector handle the first time to avoid looking it up every time.
   This table should contain all 3GPP specified protocols */
lookup_dissector_element_t dissector_lookup_table[] = {
  {"DATA","data","data",0,0,NULL},
  {"GAN.TCP","umatcp","umatcp",0,0,NULL},
  {"GAN.UDP","umaudp","umaudp",0,0,NULL},
  {"GSM.CCCH","gsm_a_ccch","gsm_a_ccch",0,0,NULL},
  {"GSM.SACCH","gsm_a_sacch","gsm_a_sacch",0,0,NULL},
  {"GTP","gtp","gtp",0,0,NULL},
  {"LLC","llcgprs","llcgprs",0,0,NULL},
  {"LTE-MAC","mac-lte","mac-lte",0,0, lte_mac_pseudo_hdr},
  {"LTE-PDCP","pdcp-lte","pdcp-lte",0,0, lte_pdcp_pseudo_hdr},
  {"LTE-RLC","rlc-lte","rlc-lte",0,0, lte_rlc_pseudo_hdr},
  {"LTE-RRC.BCCH.BCH",0,0,0,0,NULL}, /* Dissector set according to preferences (depending on the release) */
  {"LTE-RRC.BCCH.DL.SCH",0,0,0,0,NULL}, /* Dissector set according to preferences (depending on the release) */
  {"LTE-RRC.CCCH",0,0,0,0,NULL}, /* Dissector set according to preferences (depending on the release) */
  {"LTE-RRC.DCCH",0,0,0,0,NULL}, /* Dissector set according to preferences (depending on the release) */
  {"LTE-RRC.PCCH",0,0,0,0,NULL}, /* Dissector set according to preferences (depending on the release) */
  {"NAS","gsm_a_dtap","gsm_a_dtap",0,0,NULL},
  {"NAS-EPS",0,0,0,0,NULL}, /* Dissector set according to preferences (depending on the release) */
  {"RR","gsm_a_dtap","gsm_a_dtap",0,0,NULL},
  {"RRC.BCCH.BCH","rrc.bcch.bch","rrc.bcch.bch",0,0,NULL},
  {"RRC.BCCH.FACH","rrc.bcch.fach","rrc.bcch.fach",0,0,NULL},
  {"RRC.CCCH","rrc.ul.ccch","rrc.dl.ccch",0,0,NULL},
  {"RRC.DCCH","rrc.ul.dcch","rrc.dl.dcch",0,0,NULL},
  {"RRC.MCCH","rrc.mcch","rrc.mcch",0,0,NULL},
  {"RRC.MSCH","rrc.msch","rrc.msch",0,0,NULL},
  {"RRC.PCCH","rrc.pcch","rrc.pcch",0,0,NULL},
  {"RRC.SHCCH","rrc.ul.shcch","rrc.dl.shcch",0,0,NULL},
  {"RRC.SI.MIB","rrc.si.mib","rrc.si.mib",0,0,NULL},
  {"RRC.SI.SB1","rrc.sb1","rrc.sb1",0,0,NULL},
  {"RRC.SI.SB2","rrc.sb2","rrc.sb2",0,0,NULL},
  {"RRC.SI.SIB1","rrc.si.sib1","rrc.si.sib1",0,0,NULL},
  {"RRC.SI.SIB10","rrc.si.sib10","rrc.si.sib10",0,0,NULL},
  {"RRC.SI.SIB11","rrc.si.sib11","rrc.si.sib11",0,0,NULL},
  {"RRC.SI.SIB11bis","rrc.si.sib11bis","rrc.si.sib11bis",0,0,NULL},
  {"RRC.SI.SIB12","rrc.si.sib12","rrc.si.sib12",0,0,NULL},
  {"RRC.SI.SIB13","rrc.si.sib13","rrc.si.sib13",0,0,NULL },
  {"RRC.SI.SIB13-1","rrc.si.sib13-1","rrc.si.sib13-1",0,0,NULL},
  {"RRC.SI.SIB13-2","rrc.si.sib13-2","rrc.si.sib13-2",0,0,NULL},
  {"RRC.SI.SIB13-3","rrc.si.sib13-3","rrc.si.sib13-3",0,0,NULL },
  {"RRC.SI.SIB13-4","rrc.si.sib13-4","rrc.si.sib13-4",0,0,NULL},
  {"RRC.SI.SIB14","rrc.si.sib14","rrc.si.sib14",0,0,NULL},
  {"RRC.SI.SIB15","rrc.si.sib15","rrc.si.sib15",0,0,NULL},
  {"RRC.SI.SIB15bis","rrc.si.sib15bis","rrc.si.sib15bis",0,0,NULL},
  {"RRC.SI.SIB15-1","rrc.si.sib15-1","rrc.si.sib15-1",0,0,NULL},
  {"RRC.SI.SIB15-1bis","rrc.si.sib15-1bis","rrc.si.sib15-1bis",0,0,NULL },
  {"RRC.SI.SIB15-2","rrc.si.sib15-2","rrc.si.sib15-2",0,0,NULL},
  {"RRC.SI.SIB15-2bis","rrc.si.sib15-2bis","rrc.si.sib15-2bis",0,0,NULL},
  {"RRC.SI.SIB15-3","rrc.si.sib15-3","rrc.si.sib15-3",0,0,NULL},
  {"RRC.SI.SIB15-3bis","rrc.si.sib15-3bis","rrc.si.sib15-3bis",0,0,NULL},
  {"RRC.SI.SIB15-4","rrc.si.sib15-4","rrc.si.sib15-4",0,0,NULL},
  {"RRC.SI.SIB15-5","rrc.si.sib15-5","rrc.si.sib15-5",0,0,NULL},
  {"RRC.SI.SIB15-6","rrc.si.sib15-6","rrc.si.sib15-6",0,0,NULL},
  {"RRC.SI.SIB15-7","rrc.si.sib15-7","rrc.si.sib15-7",0,0,NULL},
  {"RRC.SI.SIB15-8","rrc.si.sib15-8","rrc.si.sib15-8",0,0,NULL},
  {"RRC.SI.SIB18","rrc.si.sib18","rrc.si.sib18",0,0,NULL},
  {"RRC.SI.SIB17","rrc.si.sib17","rrc.si.sib17",0,0,NULL},
  {"RRC.SI.SIB18","rrc.si.sib18","rrc.si.sib18",0,0,NULL},
  {"RRC.SI.SIB2","rrc.si.sib2","rrc.si.sib2",0,0,NULL},
  {"RRC.SI.SIB3","rrc.si.sib3","rrc.si.sib3",0,0,NULL},
  {"RRC.SI.SIB4","rrc.si.sib4","rrc.si.sib4",0,0,NULL},
  {"RRC.SI.SIB5","rrc.si.sib5","rrc.si.sib5",0,0,NULL},
  {"RRC.SI.SIB5bis","rrc.si.sib5bis","rrc.si.sib5bis",0,0,NULL},
  {"RRC.SI.SIB6","rrc.si.sib6","rrc.si.sib6",0,0,NULL},
  {"RRC.SI.SIB7","rrc.si.sib7","rrc.si.sib7",0,0,NULL},
  {"RRC.SI.SIB8","rrc.si.sib8","rrc.si.sib8",0,0,NULL},
  {"RRC.SI.SIB9","rrc.si.sib9","rrc.si.sib9",0,0,NULL},
  {"SNDCP","sndcp","sndcp",0,0,NULL},
  {"SNDCPXID","sndcpxid","sndcpxid",0,0,NULL}
};


static int
dissector_element_compare(const void *protocol_name, const void *element)
{
  return strcmp((const char *)protocol_name, ((const lookup_dissector_element_t *) element)->protocol_name);
}

static dissector_handle_t
look_for_dissector(char* protocol_name, packet_direction_t direction, pseudo_hdr_func_ptr_t* func_ptr _U_)
{
    lookup_dissector_element_t* element_ptr;
    dissector_handle_t dissector_handle = NULL;

    element_ptr = (lookup_dissector_element_t*)bsearch((void*)protocol_name,
        (void*)dissector_lookup_table,
        sizeof(dissector_lookup_table) / sizeof(lookup_dissector_element_t),
        sizeof(lookup_dissector_element_t),
        dissector_element_compare);
    if (element_ptr != NULL) {
        if (direction == UPLINK)
        {
            if (element_ptr->ul_dissector_handle == 0)
            {
                element_ptr->ul_dissector_handle = find_dissector(element_ptr->ul_dissector_name);
            }
            dissector_handle = element_ptr->ul_dissector_handle;
        }
        else
        {
            if (element_ptr->dl_dissector_handle == 0)
            {
                element_ptr->dl_dissector_handle = find_dissector(element_ptr->dl_dissector_name);
            }
            dissector_handle = element_ptr->dl_dissector_handle;
        }

    }

    return dissector_handle;
}

static void
update_dissector_name(const char* protocol_name, packet_direction_t direction, const char* dissector_name)
{
    lookup_dissector_element_t* element_ptr;

    element_ptr = (lookup_dissector_element_t*)bsearch((void*)protocol_name,
        (void*)dissector_lookup_table,
        sizeof(dissector_lookup_table) / sizeof(lookup_dissector_element_t),
        sizeof(lookup_dissector_element_t),
        dissector_element_compare);
    if (element_ptr != NULL) {
        if (direction == UPLINK)
        {
            element_ptr->ul_dissector_handle = 0;
            element_ptr->ul_dissector_name = dissector_name;
        }
        else
        {
            element_ptr->dl_dissector_handle = 0;
            element_ptr->dl_dissector_name = dissector_name;
        }

    }
}

/******************************************************************************/
/* pseudo header functions: used for the dissectors that needs pseudo headers */
/******************************************************************************/


/* In the optional string, MAC info should be set as follow (M = mandatory, O = optional):
 * Radio type (M):  "FDD" or "TDD"
 * RNTI type (M): "NO_RNTI" or "P_RNTI" or "RA_RNTI" or "C_RNTI" or "SI_RNT" followed by rnti value in decimal format
 * subframe number (M): "SFN" followed by the subframe number in decimal format
 */
static gboolean
lte_mac_pseudo_hdr(char* option_str, packet_info* pinfo, guint16 length, packet_direction_t direction)
{
    struct mac_lte_info* p_mac_lte_info;
    char* par_opt_field;
    char option[30];
    static int proto_mac_lte = 0;

    /* look up for protocol handle */
    if (proto_mac_lte == 0)
    {
        proto_mac_lte = proto_get_id_by_filter_name("mac-lte");
    }

    /* Need to copy the string in a local buffer since strtok will modify it */
    g_strlcpy(option, option_str, 30);

    /* Only need to set info once per session. */
    p_mac_lte_info = (struct mac_lte_info*)p_get_proto_data(wmem_file_scope(), pinfo, proto_mac_lte, 0);
    if (p_mac_lte_info != NULL)
    {
        return 1;
    }

    /* Allocate & zero struct */
    p_mac_lte_info = (struct mac_lte_info*) wmem_new0(wmem_packet_scope(), mac_lte_info);

    /* First mandatory parameter */
    par_opt_field = strtok(option, " ");
    if (par_opt_field == NULL)
    {
        return 0;
    }
    if (strcmp(par_opt_field, "FDD") == 0)
    {
        p_mac_lte_info->radioType = FDD_RADIO;
    }
    else if (strcmp(par_opt_field, "TDD") == 0)
    {
        p_mac_lte_info->radioType = TDD_RADIO;
    }
    else
    {
        return 0;
    }

    /* Second mandatory parameter */
    par_opt_field = strtok(NULL, " ");
    if (par_opt_field == NULL)
    {
        return 0;
    }
    if (strcmp(par_opt_field, "NO_RNTI") == 0)
    {
        p_mac_lte_info->rntiType = NO_RNTI;
    }
    else if (strcmp(par_opt_field, "P_RNTI") == 0)
    {
        p_mac_lte_info->rntiType = P_RNTI;
    }
    else if (strcmp(par_opt_field, "RA_RNTI") == 0)
    {
        p_mac_lte_info->rntiType = RA_RNTI;
    }
    else if (strcmp(par_opt_field, "C_RNTI") == 0)
    {
        p_mac_lte_info->rntiType = C_RNTI;
    }
    else if (strcmp(par_opt_field, "SI_RNTI") == 0)
    {
        p_mac_lte_info->rntiType = SI_RNTI;
    }
    else
    {
        return 0;
    }
    /* Get the associated rnti value */
    par_opt_field = strtok(NULL, " ");
    if (par_opt_field)
    {
        ws_strtoi16(par_opt_field, NULL, &p_mac_lte_info->rnti);
    }
    else
    {
        return 0;
    }

    /* First optional parameter */
    p_mac_lte_info->subframeNumber = 0;
    par_opt_field = strtok(NULL, " ");
    if (par_opt_field == NULL)
    {
        return 0;
    }
    if (strcmp(par_opt_field, "SFN") == 0)
    {
        par_opt_field = strtok(NULL, " ");
        if (par_opt_field != NULL)
        {
            ws_strtoi16(par_opt_field, NULL, &p_mac_lte_info->subframeNumber);
        }
    }
    p_mac_lte_info->direction = (direction == UPLINK) ? DIRECTION_UPLINK : DIRECTION_DOWNLINK;
    p_mac_lte_info->length = length;

    /* Store info in packet */
    p_add_proto_data(wmem_file_scope(), pinfo, proto_mac_lte, 0, p_mac_lte_info);

    return 1;
}

/* In the optional string, RLC info should be set as follow (M = mandatory, O = optional):
 * Channel type (M): "SRB" or "DRB" followed by the RB ID
 * RLC mode (M): "TM" or  "UM" or "AM" or "NA"
 * UM Sequence nb length (O): "SN_5b" or "SN_10b"
 */

static gboolean
lte_rlc_pseudo_hdr(char* option_str, packet_info* pinfo, guint16 length, packet_direction_t direction)
{
    struct rlc_lte_info* p_rlc_lte_info;
    char* par_opt_field;
    char option[30];
    static int proto_rlc_lte = 0;

    /* look up for protocol handle */
    if (proto_rlc_lte == 0)
    {
        proto_rlc_lte = proto_get_id_by_filter_name("rlc-lte");
    }
    g_strlcpy(option, option_str, 30);

    /* Only need to set info once per session. */
    p_rlc_lte_info = (struct rlc_lte_info*)p_get_proto_data(wmem_file_scope(), pinfo, proto_rlc_lte, 0);
    if (p_rlc_lte_info != NULL)
    {
        return 1;
    }

    /* Allocate & zero struct */
    p_rlc_lte_info = (struct rlc_lte_info*) wmem_new0(wmem_packet_scope(), rlc_lte_info);
    /* First mandatory parameter */
    par_opt_field = strtok(option, " ");
    if (par_opt_field == NULL)
    {
        return 0;
    }
    if (strcmp(par_opt_field, "SRB") == 0)
    {
        p_rlc_lte_info->channelType = CHANNEL_TYPE_SRB;
    }
    else if (strcmp(par_opt_field, "DRB") == 0)
    {
        p_rlc_lte_info->channelType = CHANNEL_TYPE_DRB;
    }
    else
    {
        return 0;
    }
    /* Fill in the RB ID */
    par_opt_field = strtok(NULL, " ");
    if (par_opt_field == NULL)
    {
        return 0;
    }
    ws_strtou16(par_opt_field, NULL, &p_rlc_lte_info->channelId);

    /* Second mandatory parameter */
    par_opt_field = strtok(NULL, " ");
    if (par_opt_field == NULL)
    {
        return 0;
    }
    if (strcmp(par_opt_field, "TM") == 0)
    {
        p_rlc_lte_info->rlcMode = RLC_TM_MODE;
    }
    else if (strcmp(par_opt_field, "UM") == 0)
    {
        p_rlc_lte_info->rlcMode = RLC_UM_MODE;
    }
    else if (strcmp(par_opt_field, "AM") == 0)
    {
        p_rlc_lte_info->rlcMode = RLC_AM_MODE;
    }
    else if (strcmp(par_opt_field, "NA") == 0)
    {
        p_rlc_lte_info->rlcMode = RLC_PREDEF;
    }
    else
    {
        return 0;
    }

    /* First optional parameter */
    par_opt_field = strtok(NULL, " ");
    if (par_opt_field != NULL)
    {
        if (strcmp(par_opt_field, "SN_5b") == 0)
        {
            p_rlc_lte_info->sequenceNumberLength = UM_SN_LENGTH_5_BITS;
        }
        else if (strcmp(par_opt_field, "SN_10b") == 0)
        {
            p_rlc_lte_info->sequenceNumberLength = UM_SN_LENGTH_10_BITS;
        }
    }
    p_rlc_lte_info->direction = (direction == UPLINK) ? DIRECTION_UPLINK : DIRECTION_DOWNLINK;
    p_rlc_lte_info->priority = 0;
    p_rlc_lte_info->ueid = 0;
    p_rlc_lte_info->pduLength = length;

    /* Store info in packet */
    p_add_proto_data(wmem_file_scope(), pinfo, proto_rlc_lte, 0, p_rlc_lte_info);

    return (1);
}

/* In the optional string, PDCP info should be set as follow (M = mandatory, O = optional):
 * Plane: "SRB" or "DRB"
 * Sequence number length: "SN_7b" or "SN_12b"
 */
static gboolean
lte_pdcp_pseudo_hdr(char* option_str, packet_info* pinfo, guint16 length _U_, packet_direction_t direction)
{
    struct pdcp_lte_info* p_pdcp_lte_info;
    char* par_opt_field;
    char option[30];
    static int proto_pdcp_lte = 0;

    /* look up for protocol handle */
    if (proto_pdcp_lte == 0)
    {
        proto_pdcp_lte = proto_get_id_by_filter_name("pdcp-lte");
    }
    g_strlcpy(option, option_str, 30);

    /* Only need to set info once per session. */
    p_pdcp_lte_info = (struct pdcp_lte_info*)p_get_proto_data(wmem_file_scope(), pinfo, proto_pdcp_lte, 0);
    if (p_pdcp_lte_info != NULL)
    {
        return 1;
    }

    /* Allocate & zero struct */
    p_pdcp_lte_info = (struct pdcp_lte_info*) wmem_new0(wmem_packet_scope(), pdcp_lte_info);
    /* First mandatory parameter */
    par_opt_field = strtok(option, " ");
    if (par_opt_field == NULL)
    {
        return 0;
    }
    if (strcmp(par_opt_field, "SRB") == 0)
    {
        p_pdcp_lte_info->plane = SIGNALING_PLANE;
    }
    else if (strcmp(par_opt_field, "DRB") == 0)
    {
        p_pdcp_lte_info->plane = USER_PLANE;
    }
    else
    {
        return 0;
    }
    /* Second mandatory parameter */
    par_opt_field = strtok(NULL, " ");
    if (par_opt_field == NULL)
    {
        return 0;
    }
    if (strcmp(par_opt_field, "SN_7b") == 0)
    {
        p_pdcp_lte_info->seqnum_length = PDCP_SN_LENGTH_7_BITS;
    }
    else if (strcmp(par_opt_field, "SN_12b") == 0)
    {
        p_pdcp_lte_info->seqnum_length = PDCP_SN_LENGTH_12_BITS;
    }
    else
    {
        return 0;
    }
    p_pdcp_lte_info->no_header_pdu = 0;
    p_pdcp_lte_info->rohc.rohc_compression = 0;
    p_pdcp_lte_info->direction = (direction == UPLINK) ? DIRECTION_UPLINK : DIRECTION_DOWNLINK;

    /* Store info in packet */
    p_add_proto_data(wmem_file_scope(), pinfo, proto_pdcp_lte, 0, p_pdcp_lte_info);

    return (1);
}


/*****************************************/
/* Main dissection function.             */
/*****************************************/
static int
dissect_log3gpp(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree, void* data _U_)
{
    proto_tree* prot3gpp_tree = NULL;
    proto_item* ti = NULL;
    gint        offset = 0;
    gint        protocol_name_start;
    gint        protocol_name_length;
    gint        protocol_option_start;
    gint        protocol_option_length;
    gint        timestamp_start;
    gint        timestamp_length;
    packet_direction_t      direction;
    tvbuff_t* next_tvb;
    dissector_handle_t protocol_handle = 0;
    int sub_dissector_result = 0;
    char* protocol_name;
    char* protocol_option;
    gboolean is_hex_data;

    /* Clear Info */
    col_clear(pinfo->cinfo, COL_INFO);

    /* Create root (protocol) tree. */
    ti = proto_tree_add_item(tree, proto_log3gpp, tvb, offset, -1, FALSE);
    prot3gpp_tree = proto_item_add_subtree(ti, ett_log3gpp);

    /*********************************************************************/
    /* Note that these are the fields of the stub header as written out  */
    /* by the wiretap module                                             */

    /* Timestamp in file */
    timestamp_start = offset;
    timestamp_length = tvb_strsize(tvb, offset);
    if (prot3gpp_tree) {
        proto_tree_add_double_format_value(prot3gpp_tree, hf_log3gpp_timestamp, tvb,
            offset, timestamp_length,
            g_ascii_strtod(tvb_format_text(tvb, offset, timestamp_length), NULL),
            "%s", tvb_format_text(tvb, offset, timestamp_length - 1));
    }
    offset += timestamp_length;


    /* protocol name */
    protocol_name_start = offset;
    protocol_name_length = tvb_strsize(tvb, offset);
    if (prot3gpp_tree) {
        proto_tree_add_item(prot3gpp_tree, hf_log3gpp_protocol, tvb, offset, protocol_name_length, ENC_ASCII | ENC_NA);
    }
    offset += protocol_name_length;

    /* Direction */
    direction = (packet_direction_t)tvb_get_guint8(tvb, offset);
    if (prot3gpp_tree) {
        proto_tree_add_item(prot3gpp_tree, hf_log3gpp_direction, tvb, offset, 1, ENC_BIG_ENDIAN);
    }
    offset++;

    /* protocol option */
    protocol_option_start = offset;
    protocol_option_length = tvb_strsize(tvb, offset);
    if (prot3gpp_tree) {
        proto_tree_add_item(prot3gpp_tree, hf_log3gpp_dissector_option, tvb, offset, protocol_option_length, ENC_ASCII | ENC_NA);
    }
    offset += protocol_option_length;

    if (prot3gpp_tree)
    {
        /* Set selection length of prot3gpp tree */
        proto_item_set_len(prot3gpp_tree, offset);
    }

    /* Add useful details to protocol tree label */
    protocol_name = (char*)tvb_get_string_enc(pinfo->pool, tvb, protocol_name_start, protocol_name_length, ENC_UTF_8 | ENC_NA);
    /* Set Protocol */
    col_set_str(pinfo->cinfo, COL_PROTOCOL, protocol_name);
    /* To know whether the data following is row byte stream or text data */
    is_hex_data = strcmp(protocol_name, "TXT");

    proto_item_append_text(ti, " t=%s   %c   prot=%s",
        tvb_get_string_enc(wmem_packet_scope(), tvb, timestamp_start, timestamp_length, ENC_UTF_8 | ENC_NA),
        (direction == 0) ? 'U' : 'D',
        protocol_name);

    if (is_hex_data)
    {
        /* We might need to prepend pseudo header for the dissector */
        pseudo_hdr_func_ptr_t func_ptr = NULL;

        /* Look up for the optional information */
        protocol_option = (char*)tvb_get_string_enc(wmem_packet_scope(), tvb, protocol_option_start, protocol_option_length, ENC_UTF_8 | ENC_NA);

        /* look up for the right dissector handle */
        protocol_handle = look_for_dissector(protocol_name, direction, &func_ptr);

        /***********************************************************************/
        /* Now hand off to the dissector of intended packet encapsulation type */

        /* Try appropriate dissector, if one has been selected */
        if (protocol_handle != 0)
        {
            /* Dissect the remainder of the frame using chosen protocol handle */
            next_tvb = tvb_new_subset_length_caplen(tvb, offset, -1, tvb_reported_length(tvb) - offset);

            /* This part is optional, only for dissector that need pseudo header information */
            if (func_ptr != NULL && strlen(protocol_option) != 0)
            {
                if (func_ptr(protocol_option, pinfo, offset, direction) == 0)
                {
                    /* There was an error, return */
                    return tvb_reported_length(tvb);
                }
            }
            sub_dissector_result = call_dissector(protocol_handle, next_tvb, pinfo, tree);
        }
    }

    if (protocol_handle == 0 || sub_dissector_result == 0)
    {
        /* Could get here because:
          - desired protocol is unavailable (probably disabled), OR
          - protocol rejected our data
          Show remaining bytes as unparsed data */
        proto_tree_add_item(prot3gpp_tree, hf_log3gpp_unparsed_data, tvb, offset, -1, ENC_NA);

        if (!is_hex_data)
        {
            col_add_fstr(pinfo->cinfo, COL_INFO,
                "%s",
                tvb_get_string_enc(wmem_packet_scope(), tvb, offset, tvb_reported_length(tvb) - offset, ENC_UTF_8 | ENC_NA));
        }
        else
        {
            col_add_fstr(pinfo->cinfo, COL_INFO,
                "Not dissected  ( t=%s   %c   prot=%s)",
                tvb_get_string_enc(wmem_packet_scope(), tvb, timestamp_start, timestamp_length, ENC_UTF_8 | ENC_NA),
                (direction == 0) ? 'U' : 'D',
                tvb_get_string_enc(wmem_packet_scope(), tvb, protocol_name_start, protocol_name_length, ENC_UTF_8 | ENC_NA));
        }
    }
    else
    {
        /* Show number of dissected bytes */
        proto_item* ti_local = proto_tree_add_uint(prot3gpp_tree,
            hf_log3gpp_dissected_length,
            tvb, 0, 0, tvb_reported_length(tvb) - offset);
        PROTO_ITEM_SET_GENERATED(ti_local);
    }
    return tvb_reported_length(tvb);
}

/******************************************************************************/
/* Associate this protocol with the log3gpp file encapsulation type. */
/******************************************************************************/
void proto_reg_handoff_log3gpp(void)
{
    static gboolean init = FALSE;

    if (init == FALSE)
    {
        dissector_handle_t log3gpp_handle;

        log3gpp_handle = find_dissector("prot3gpp");
        dissector_add_uint("wtap_encap", WTAP_ENCAP_LOG_3GPP, log3gpp_handle);
        init = TRUE;
    }
    if (lte_rrc_prot_version == REL8)
    {
        update_dissector_name("LTE-RRC.BCCH.BCH", UPLINK, "lte-rrc.bcch.bch");
        update_dissector_name("LTE-RRC.BCCH.BCH", DOWNLINK, "lte-rrc.bcch.bch");
        update_dissector_name("LTE-RRC.BCCH.DL.SCH", UPLINK, "lte-rrc.bcch.dl.sch");
        update_dissector_name("LTE-RRC.BCCH.DL.SCH", DOWNLINK, "lte-rrc.bcch.dl.sch");
        update_dissector_name("LTE-RRC.CCCH", UPLINK, "lte-rrc.ul.ccch");
        update_dissector_name("LTE-RRC.CCCH", DOWNLINK, "lte-rrc.dl.ccch");
        update_dissector_name("LTE-RRC.DCCH", UPLINK, "lte-rrc.ul.dcch");
        update_dissector_name("LTE-RRC.DCCH", DOWNLINK, "lte-rrc.dl.dcch");
        update_dissector_name("LTE-RRC.PCCH", UPLINK, "lte-rrc.pcch");
        update_dissector_name("LTE-RRC.PCCH", DOWNLINK, "lte-rrc.pcch");
    }
    else if (lte_rrc_prot_version == FD1)
    {
        update_dissector_name("LTE-RRC.BCCH.BCH", UPLINK, "lte-rrc-fd1.bcch.bch");
        update_dissector_name("LTE-RRC.BCCH.BCH", DOWNLINK, "lte-rrc-fd1.bcch.bch");
        update_dissector_name("LTE-RRC.BCCH.DL.SCH", UPLINK, "lte-rrc-fd1.bcch.dl.sch");
        update_dissector_name("LTE-RRC.BCCH.DL.SCH", DOWNLINK, "lte-rrc-fd1.bcch.dl.sch");
        update_dissector_name("LTE-RRC.CCCH", UPLINK, "lte-rrc-fd1.ul.ccch");
        update_dissector_name("LTE-RRC.CCCH", DOWNLINK, "lte-rrc-fd1.dl.ccch");
        update_dissector_name("LTE-RRC.DCCH", UPLINK, "lte-rrc-fd1.ul.dcch");
        update_dissector_name("LTE-RRC.DCCH", DOWNLINK, "lte-rrc-fd1.dl.dcch");
        update_dissector_name("LTE-RRC.PCCH", UPLINK, "lte-rrc-fd1.pcch");
        update_dissector_name("LTE-RRC.PCCH", DOWNLINK, "lte-rrc-fd1.pcch");
    }
    if (nas_eps_prot_version == REL8)
    {
        update_dissector_name("NAS-EPS", UPLINK, "nas-eps");
        update_dissector_name("NAS-EPS", DOWNLINK, "nas-eps");
    }
    else if (nas_eps_prot_version == FD1)
    {
        update_dissector_name("NAS-EPS", UPLINK, "nas-eps-fd1");
        update_dissector_name("NAS-EPS", DOWNLINK, "nas-eps-fd1");
    }
}

/****************************************/
/* Register the protocol                */
/****************************************/
void proto_register_log3gpp(void)
{
  module_t *log3gpp_module;
    static hf_register_info hf[] =
    {
        { &hf_log3gpp_timestamp,
            { "Timestamp",
              "log3gpp.timestamp", FT_DOUBLE, BASE_NONE, NULL, 0x0,
              "File timestamp", HFILL
            }
        },
        { &hf_log3gpp_protocol,
            { "3GPP protocol",
              "log3gpp.protocol", FT_STRING, BASE_NONE, NULL, 0x0,
              "Original 3GPP protocol name", HFILL
            }
        },
        { &hf_log3gpp_dissector_option,
            { "option",
              "log3gpp.option", FT_STRING, BASE_NONE, NULL, 0x0,
              "Protocol option", HFILL
            }
        },
        { &hf_log3gpp_direction,
            { "Direction",
              "log3gpp.direction", FT_UINT8, BASE_DEC, VALS(direction_vals), 0x0,
              "Frame direction (Uplink or Downlink)", HFILL
            }
        },
        { &hf_log3gpp_unparsed_data,
            { "Unparsed protocol data",
              "log3gpp.unparsed_data", FT_BYTES, BASE_NONE, NULL, 0x0,
              "Unparsed 3GPP protocol data", HFILL
            }
        },
        { &hf_log3gpp_dissected_length,
            { "Dissected length",
              "log3gpp.dissected-length", FT_UINT16, BASE_DEC, NULL, 0x0,
              "Number of bytes dissected by subdissector(s)", HFILL
            }
        },
    };

    static gint *ett[] =
    {
        &ett_log3gpp
    };

    /* Register protocol. */
    proto_log3gpp = proto_register_protocol("3GPP log packet",
                                            "LOG3GPP",
                                            "log3gpp");
    proto_register_field_array(proto_log3gpp, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    log3gpp_module = prefs_register_protocol(proto_log3gpp, proto_reg_handoff_log3gpp);
    prefs_register_enum_preference(log3gpp_module,
                                   "rrc_release_version",
                                   "Select the release version of LTE RRC protocol",
                                   "There might be plugins corresponding to different version of the specification "
                                   "If they are present they should be listed here.",
                                   &lte_rrc_prot_version,
                                   lte_rrc_dissector_version,
                                   FALSE);

    prefs_register_enum_preference(log3gpp_module,
                                   "nas_eps_release_version",
                                   "Select the release version of NAS EPS protocol",
                                   "There might be plugins corresponding to different version of the specification "
                                   "If they are present they should be listed here.",
                                   &nas_eps_prot_version,
                                   nas_eps_dissector_version,
                                   FALSE);

    /* Allow dissector to find be found by name. */
    register_dissector("prot3gpp", dissect_log3gpp, proto_log3gpp);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */