summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-docsis.c
blob: 3de7f8aa3167beda64149ebaed8310983f187866 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
/* packet-docsis.c
 * Routines for docsis dissection
 * Copyright 2002, Anand V. Narwani <anand[AT]narwani.org>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */


/* This code is based on the DOCSIS 1.1 specification available at:
 * http://www.cablelabs.com/wp-content/uploads/specdocs/CM-SP-RFIv1.1-C01-050907.pdf
 *
 * This code was updated to include DOCSIS 3.1 specification details available at:
 * http://www.cablelabs.com/wp-content/uploads/specdocs/CM-SP-MULPIv3.1-I09-160602.pdf
 *
 * Updates are backward compatible with previous DOCSIS spcifications.
 *
 * DOCSIS Captures can be facilitated using the Cable Monitor Feature
 * available on Cisco Cable Modem Termination Systems:
 * https://www.cisco.com/c/en/us/td/docs/cable/cmts/config_guide/b_cmts_security_and_cable_monitoring_features/b_cmts_security_and_cable_monitoring_features_chapter_010.html
 *
 * This dissector depends on the presence of a DOCSIS enapsulation type.
 * There is no simple way to distinguish DOCSIS Frames from Ethernet frames,
 * since the frames are copied from the RF interface on the CMTS to
 * a Fast Ethernet interface; thus a preference was needed to enable
 * the DOCSIS encapsulation type.
 *
 * Libpcap 0.7 and later allow a link-layer header type to be specified for
 * some interfaces on some platforms; for Ethernet interfaces, they allow
 * DOCSIS to be specified.  If an Ethernet capture is done with a link-layer
 * type of DOCSIS, the file will have a link-layer header type of DLT_DOCSIS;
 * Wireshark will treat the frames in that capture as DOCSIS frames, even
 * if the preference mentioned above isn't enabled.
 */

#include "config.h"

#include <epan/etypes.h>
#include <epan/expert.h>
#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/reassemble.h>
#include <epan/addr_resolv.h>
#include <epan/exceptions.h>
#include <epan/crc16-tvb.h>
#include <epan/crc32-tvb.h>

void proto_register_docsis(void);
void proto_reg_handoff_docsis(void);

/* Assume all packets have an FCS */
static gboolean docsis_check_fcs = TRUE;
static gboolean docsis_dissect_encrypted_frames = FALSE;

#define DOCSIS_MIN_HEADER_LEN   6

#define FCTYPE_PACKET   0x00
#define FCTYPE_RESERVED 0x01
#define FCTYPE_ISOLAT   0x02
#define FCTYPE_MACSPC   0x03

#define FCPARM_TIMING_HDR           0x00
#define FCPARM_MAC_MGMT_HDR         0x01
#define FCPARM_RQST_FRM             0x02
#define FCPARM_FRAG_HDR             0x03
#define FCPARM_QUEUE_DEPTH_REQ_FRM  0x04
#define FCPARM_CONCAT_HDR           0x1C

#define EXT_HDR_OFF     0x00
#define EXT_HDR_ON      0x01

#define FRAG_FCS_LEN    4
#define FRAG_FIRST      0x20
#define FRAG_MIDDLE     0x00
#define FRAG_LAST       0x10

#define EH_NULL_CONFIG      0
#define EH_REQUEST          1
#define EH_ACK_REQ          2
#define EH_BP_UP            3
#define EH_BP_DOWN          4
#define EH_SFLOW_HDR_DOWN   5
#define EH_SFLOW_HDR_UP     6
#define EH_BP_UP2           7
#define EH_DS_SERVICE       8
#define EH_PATH_VERIFY      9
#define EH_RESERVED_10      10
#define EH_RESERVED_11      11
#define EH_RESERVED_12      12
#define EH_RESERVED_13      13
#define EH_RESERVED_14      14
#define EH_EXTENDED         15

/* Initialize the protocol and registered fields */
static int proto_docsis = -1;
static int hf_docsis_fctype = -1;
static int hf_docsis_machdr_fcparm = -1;
static int hf_docsis_fcparm = -1;
static int hf_docsis_exthdr = -1;
static int hf_docsis_concat_cnt = -1;
static int hf_docsis_macparm = -1;
static int hf_docsis_ehdrlen = -1;
static int hf_docsis_len = -1;
static int hf_docsis_eh_type = -1;
static int hf_docsis_eh_len = -1;
static int hf_docsis_eh_val = -1;
static int hf_docsis_ehx_type = -1;
static int hf_docsis_ehx_len = -1;
static int hf_docsis_frag_rsvd = -1;
static int hf_docsis_frag_first = -1;
static int hf_docsis_frag_last = -1;
static int hf_docsis_frag_seq = -1;
static int hf_docsis_sid = -1;
static int hf_docsis_mini_slots = -1;
static int hf_docsis_requested_size = -1;
static int hf_docsis_hcs = -1;
static int hf_docsis_hcs_status = -1;
static int hf_docsis_bpi_en = -1;
static int hf_docsis_toggle_bit = -1;
static int hf_docsis_key_seq = -1;
static int hf_docsis_ehdr_ver = -1;
static int hf_docsis_said = -1;
static int hf_docsis_ehdr_phsi = -1;
static int hf_docsis_ehdr_qind = -1;
static int hf_docsis_ehdr_grants = -1;
static int hf_docsis_reserved = -1;
static int hf_docsis_ehdr_ds_traffic_pri = -1;
static int hf_docsis_ehdr_ds_seq_chg_cnt = -1;
static int hf_docsis_ehdr_ds_dsid = -1;
static int hf_docsis_ehdr_ds_pkt_seq_num = -1;
static int hf_docsis_ehdr_bpup2_bpi_en = -1;
static int hf_docsis_ehdr_bpup2_toggle_bit = -1;
static int hf_docsis_ehdr_bpup2_key_seq = -1;
static int hf_docsis_ehdr_bpup2_ver = -1;
static int hf_docsis_ehdr_bpup2_sid = -1;
static int hf_docsis_ehdr_pv_st_refpt = -1;
static int hf_docsis_ehdr_pv_timestamp = -1;

static int hf_docsis_fragments = -1;
static int hf_docsis_fragment = -1;
static int hf_docsis_fragment_overlap = -1;
static int hf_docsis_fragment_overlap_conflict = -1;
static int hf_docsis_fragment_multiple_tails = -1;
static int hf_docsis_fragment_too_long_fragment = -1;
static int hf_docsis_fragment_error = -1;
static int hf_docsis_fragment_count = -1;
static int hf_docsis_reassembled_in = -1;
static int hf_docsis_reassembled_length = -1;
static int hf_docsis_reassembled_data = -1;
static int hf_docsis_frag_fcs = -1;
static int hf_docsis_frag_fcs_status = -1;

static int hf_docsis_dst = -1;
static int hf_docsis_dst_resolved = -1;
static int hf_docsis_src = -1;
static int hf_docsis_src_resolved = -1;
static int hf_docsis_lg = -1;
static int hf_docsis_ig = -1;
static int hf_docsis_encrypted_payload = -1;

static dissector_handle_t docsis_handle;
static dissector_handle_t eth_withoutfcs_handle;
static dissector_handle_t docsis_mgmt_handle;
#if 0
static dissector_table_t docsis_dissector_table;
#endif

static expert_field ei_docsis_hcs_bad = EI_INIT;
static expert_field ei_docsis_len = EI_INIT;
static expert_field ei_docsis_frag_fcs_bad = EI_INIT;
static expert_field ei_docsis_eh_len = EI_INIT;

/* Initialize the subtree pointers */
static gint ett_docsis = -1;
static gint ett_ehdr = -1;
static gint ett_docsis_fragments = -1;
static gint ett_docsis_fragment = -1;
static gint ett_addr = -1;

static const value_string fctype_vals[] = {
  {FCTYPE_PACKET,   "Packet PDU"},
  {FCTYPE_RESERVED, "Reserved"},
  {FCTYPE_ISOLAT,   "Isolation PDU"},
  {FCTYPE_MACSPC,   "MAC Specific"},
  {0, NULL}
};

static const value_string eh_type_vals[] = {
  {0,                 "NULL Configuration Parameter"},
  {EH_REQUEST,        "Request"},
  {EH_ACK_REQ,        "Acknowledgement Requested"},
  {EH_BP_UP,          "Upstream Privacy Element"},
  {EH_BP_DOWN,        "Downstream  Privacy Element"},
  {EH_SFLOW_HDR_UP,   "Service Flow EH; PHS Header Upstream"},
  {EH_SFLOW_HDR_DOWN, "Service Flow EH; PHS Header Downstream"},
  {EH_BP_UP2,         "Upstream Privacy with Multi Channel"},
  {EH_DS_SERVICE,     "Downstream Service"},
  {EH_PATH_VERIFY,    "Path Verify"},
  {EH_RESERVED_10,    "Reserved"},
  {EH_RESERVED_11,    "Reserved"},
  {EH_RESERVED_12,    "Reserved"},
  {EH_RESERVED_13,    "Reserved"},
  {EH_RESERVED_14,    "Reserved"},
  {EH_EXTENDED,       "Extended"},
  {0, NULL}
};

static const value_string ms_fcparm_vals[] = {
  {FCPARM_TIMING_HDR,           "Timing Header"},
  {FCPARM_MAC_MGMT_HDR,         "MAC Management Message"},
  {FCPARM_RQST_FRM,             "Request Frame"},
  {FCPARM_FRAG_HDR,             "Fragmentation Header"},
  {FCPARM_QUEUE_DEPTH_REQ_FRM,  "Queue Depth-based Request Frame"},
  {FCPARM_CONCAT_HDR,           "Concatenation Header"},
  {0, NULL}
};

static const value_string pkt_fcparm_vals[] = {
  {0x00,           "PDU MAC"},
  {0x01,           "DELAY/DUPLICATE/MULTICAST/BROADCAST"},
  {0, NULL}
};

static const true_false_string exthdr_tfs = {
  "Extended Header Present",
  "Extended Header Absent"
};


static const value_string local_proto_checksum_vals[] = {
  { PROTO_CHECKSUM_E_BAD,        "Bad"  },
  { PROTO_CHECKSUM_E_GOOD,       "Good" },
  { PROTO_CHECKSUM_E_UNVERIFIED, "Unverified" },
  { PROTO_CHECKSUM_E_NOT_PRESENT, "Not present" },

  { 0,        NULL }
};

static const true_false_string qind_tfs = {
  "Rate overrun",
  "Rate non-overrun"
};

static const true_false_string odd_even_tfs = {
  "Odd Key",
  "Even Key",
};

static const value_string unique_no_phs[] = {
  { 0, "No PHS on current packet" },
  { 0, NULL }
};

/* Fragmentation Flags / Sequence */
static guint8 frag_flags;
static guint8 frag_seq;
static guint16 frag_sid;

/*
 * Defragmentation of DOCSIS
 */
static reassembly_table docsis_reassembly_table;


static const fragment_items docsis_frag_items = {
  &ett_docsis_fragment,
  &ett_docsis_fragments,
  &hf_docsis_fragments,
  &hf_docsis_fragment,
  &hf_docsis_fragment_overlap,
  &hf_docsis_fragment_overlap_conflict,
  &hf_docsis_fragment_multiple_tails,
  &hf_docsis_fragment_too_long_fragment,
  &hf_docsis_fragment_error,
  &hf_docsis_fragment_count,
  &hf_docsis_reassembled_in,
  &hf_docsis_reassembled_length,
  &hf_docsis_reassembled_data,
  "DOCSIS fragments"
};

/* Dissection */
/* Code to Dissect the extended header; TLV Formatted headers */
static void
dissect_ehdr (tvbuff_t * tvb, proto_tree * tree, packet_info * pinfo, gboolean *is_encrypted)
{
  proto_tree *ehdr_tree;
  proto_item *eh_length_item;
  gint ehdrlen;
  int pos;
  guint8 type;
  guint8 len;

  ehdrlen = tvb_get_guint8 (tvb, 1);
  pos = 4;

  ehdr_tree = proto_tree_add_subtree(tree, tvb, pos, ehdrlen, ett_ehdr, NULL, "Extended Header");

  while (pos < ehdrlen + 4)
  {
    type = (tvb_get_guint8 (tvb, pos) & 0xF0);
    len = (tvb_get_guint8 (tvb, pos) & 0x0F);
    if ((((type >> 4) & 0x0F)== 6) && (len == 2))
    {
      proto_tree_add_uint_format_value(ehdr_tree, hf_docsis_eh_type, tvb, pos, 1, 0x60, "Unsolicited Grant Sync EHDR Sub-Element");
    }
    else
    {
      proto_tree_add_item (ehdr_tree, hf_docsis_eh_type, tvb, pos, 1, ENC_BIG_ENDIAN);
    }

    eh_length_item = proto_tree_add_item (ehdr_tree, hf_docsis_eh_len, tvb, pos, 1, ENC_BIG_ENDIAN);
    pos++;

    switch ((type >> 4) & 0x0F)
    {
      case EH_REQUEST:
        /* Request: Minislots Requested */
        if (len == 3)
        {
          proto_tree_add_item(ehdr_tree, hf_docsis_mini_slots, tvb, pos, 1, ENC_NA);
          proto_tree_add_item(ehdr_tree, hf_docsis_sid, tvb, pos + 1, 2, ENC_BIG_ENDIAN);
        }
        else
        {
          expert_add_info(pinfo, eh_length_item, &ei_docsis_eh_len);
          return;
        }
        break;
      case EH_ACK_REQ:
        /* Deprecated in DOCSIS 3.1 */
        if (len == 2)
        {
          proto_tree_add_item(ehdr_tree, hf_docsis_sid, tvb, pos, 2, ENC_BIG_ENDIAN);
        }
        else
        {
          expert_add_info(pinfo, eh_length_item, &ei_docsis_eh_len);
          return;
        }
        break;
      case EH_BP_UP:
        /* Upstream Privacy EH Element or Upstream Privacy with fragmentation */
        proto_tree_add_item (ehdr_tree, hf_docsis_key_seq, tvb, pos, 1,
                             ENC_BIG_ENDIAN);
        proto_tree_add_item (ehdr_tree, hf_docsis_ehdr_ver, tvb, pos, 1,
                             ENC_BIG_ENDIAN);
        proto_tree_add_item_ret_boolean (ehdr_tree, hf_docsis_bpi_en, tvb, pos + 1, 1,
                             ENC_BIG_ENDIAN, is_encrypted);
        proto_tree_add_item (ehdr_tree, hf_docsis_toggle_bit, tvb, pos + 1,
                             1, ENC_BIG_ENDIAN);
        proto_tree_add_item (ehdr_tree, hf_docsis_sid, tvb, pos + 1, 2,
                             ENC_BIG_ENDIAN);
        frag_sid = tvb_get_guint8 (tvb, pos+1) & 0xCFFF;
        proto_tree_add_item (ehdr_tree, hf_docsis_mini_slots, tvb, pos + 3,
                             1, ENC_BIG_ENDIAN);
        if (pinfo->fragmented)
        {
          proto_tree_add_item (ehdr_tree, hf_docsis_frag_rsvd, tvb, pos+4,
                               1, ENC_BIG_ENDIAN);
          frag_flags = tvb_get_guint8 (tvb, pos+4) & 0x30;
          proto_tree_add_item (ehdr_tree, hf_docsis_frag_first, tvb, pos+4,
                               1, ENC_BIG_ENDIAN);
          proto_tree_add_item (ehdr_tree, hf_docsis_frag_last, tvb, pos+4,
                               1, ENC_BIG_ENDIAN);
          frag_seq = tvb_get_guint8 (tvb, pos+4) & 0x0F;
          proto_tree_add_item (ehdr_tree, hf_docsis_frag_seq, tvb, pos+4,
                               1, ENC_BIG_ENDIAN);
        }
        break;
      case EH_BP_DOWN:
        /* Downstream Privacy EH Element */
        proto_tree_add_item (ehdr_tree, hf_docsis_key_seq, tvb, pos, 1,
                             ENC_BIG_ENDIAN);
        proto_tree_add_item (ehdr_tree, hf_docsis_ehdr_ver, tvb, pos, 1,
                             ENC_BIG_ENDIAN);
        proto_tree_add_item_ret_boolean (ehdr_tree, hf_docsis_bpi_en, tvb, pos + 1, 1,
                             ENC_BIG_ENDIAN, is_encrypted);
        proto_tree_add_item (ehdr_tree, hf_docsis_toggle_bit, tvb, pos + 1,
                             1, ENC_BIG_ENDIAN);
        proto_tree_add_item (ehdr_tree, hf_docsis_said, tvb, pos + 1, 2,
                             ENC_BIG_ENDIAN);
        proto_tree_add_item (ehdr_tree, hf_docsis_reserved, tvb, pos + 3, 1,
                             ENC_BIG_ENDIAN);
        break;
      case EH_SFLOW_HDR_DOWN:
        /* Deprecated in DOCSIS 3.1, was Downstream Service Flow EH Element in earlier revisions */
      case EH_SFLOW_HDR_UP:
        /* Deprecated in DOCSIS 3.1, was Upstream Service Flow EH Element in earlier revisions */
        proto_tree_add_item(ehdr_tree, hf_docsis_ehdr_phsi, tvb, pos, 1, ENC_BIG_ENDIAN);

        if (len == 2)
        {
          proto_tree_add_item (ehdr_tree, hf_docsis_ehdr_qind, tvb, pos+1, 1, ENC_BIG_ENDIAN);
          proto_tree_add_item (ehdr_tree, hf_docsis_ehdr_grants, tvb, pos+1, 1, ENC_BIG_ENDIAN);
        }
        break;
      case EH_BP_UP2:
        /* Upstream Privacy EH Element, version 2, with no piggyback request */
        proto_tree_add_item (ehdr_tree, hf_docsis_ehdr_bpup2_key_seq, tvb, pos, 1,
                             ENC_BIG_ENDIAN);
        proto_tree_add_item (ehdr_tree, hf_docsis_ehdr_bpup2_ver, tvb, pos, 1,
                             ENC_BIG_ENDIAN);
        proto_tree_add_item_ret_boolean (ehdr_tree, hf_docsis_ehdr_bpup2_bpi_en, tvb, pos + 1, 1,
                             ENC_BIG_ENDIAN, is_encrypted);
        proto_tree_add_item (ehdr_tree, hf_docsis_ehdr_bpup2_toggle_bit, tvb, pos + 1,
                             1, ENC_BIG_ENDIAN);
        proto_tree_add_item (ehdr_tree, hf_docsis_ehdr_bpup2_sid, tvb, pos + 1, 2,
                             ENC_BIG_ENDIAN);
        break;
      case EH_DS_SERVICE:
        /* Downstream Service EH Element */
        proto_tree_add_item(ehdr_tree, hf_docsis_ehdr_ds_traffic_pri, tvb, pos, 1, ENC_BIG_ENDIAN);

        if (len == 3)
        {
          proto_tree_add_item (ehdr_tree, hf_docsis_ehdr_ds_dsid, tvb, pos, 3, ENC_BIG_ENDIAN);
        }

        if (len == 5)
        {
          proto_tree_add_item (ehdr_tree, hf_docsis_ehdr_ds_seq_chg_cnt, tvb, pos, 1, ENC_BIG_ENDIAN);
          proto_tree_add_item (ehdr_tree, hf_docsis_ehdr_ds_dsid, tvb, pos, 3, ENC_BIG_ENDIAN);
          proto_tree_add_item (ehdr_tree, hf_docsis_ehdr_ds_pkt_seq_num, tvb, pos+3, 2, ENC_BIG_ENDIAN);
        }
        break;
      case EH_PATH_VERIFY:
        /* Path Verify EH Element */
        if (len == 5)
        {
          proto_tree_add_item (ehdr_tree, hf_docsis_ehdr_pv_st_refpt, tvb, pos, 1, ENC_BIG_ENDIAN);
          proto_tree_add_item (ehdr_tree, hf_docsis_ehdr_pv_timestamp, tvb, pos+1, 4, ENC_BIG_ENDIAN);
        }
        break;
      case EH_EXTENDED:
        /* Extended EH Element, ignore eh_len */
        proto_tree_add_item(ehdr_tree, hf_docsis_ehx_type, tvb, pos, 1, ENC_NA);
        pos++;
        proto_tree_add_item(ehdr_tree, hf_docsis_ehx_len, tvb, pos, 1, ENC_NA);
        len = tvb_get_guint8(tvb, pos);
        pos++;
        /* FALLTHROUGH */
      default:
        if (len > 0)
          proto_tree_add_item (ehdr_tree, hf_docsis_eh_val, tvb, pos,
                               len, ENC_NA);
    }
    pos += len;
  }

  return;
}

/* Code to Dissect the Header Check Sequence field */
/* Return FALSE in case FCS validation is enabled, but FCS is incorrect */
/* Return TRUE in all other cases */
static gboolean
dissect_hcs_field (tvbuff_t * tvb, packet_info * pinfo, proto_tree * docsis_tree, gint hdrlen)
{
  /* dissect the header check sequence */
  if(docsis_check_fcs){
    /* CRC-CCITT(16+12+5+1) */
    guint16 fcs = g_ntohs(crc16_ccitt_tvb(tvb, (hdrlen - 2)));
    proto_tree_add_checksum(docsis_tree, tvb, (hdrlen - 2), hf_docsis_hcs, hf_docsis_hcs_status, &ei_docsis_hcs_bad, pinfo, fcs, ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY);

    return (tvb_get_ntohs(tvb, (hdrlen - 2)) == fcs) ? TRUE : FALSE;
  }
  else
  {
    proto_tree_add_checksum(docsis_tree, tvb, (hdrlen - 2), hf_docsis_hcs, hf_docsis_hcs_status, &ei_docsis_hcs_bad, pinfo, 0, ENC_BIG_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
  }
  return TRUE;
}

/* Code to Dissect the extended header length / MAC Param field and Length field */
/* The length field may condain a SID, but this logic is not handled here */
static void
dissect_exthdr_length_field (tvbuff_t * tvb, packet_info * pinfo, proto_tree * docsis_tree,
                             guint8 exthdr, guint16 mac_parm, guint16 len_sid, guint16 *payload_length, gboolean *is_encrypted)
{
  proto_item *length_item;
  if (exthdr == EXT_HDR_ON)
  {
    /* Add in Extended Header Length */
    proto_tree_add_item (docsis_tree, hf_docsis_ehdrlen, tvb, 1, 1,  ENC_BIG_ENDIAN);
    length_item = proto_tree_add_item (docsis_tree, hf_docsis_len, tvb, 2, 2, ENC_BIG_ENDIAN);
    /* Validate PDU length */
    if ((len_sid - mac_parm) > *payload_length)
    {
      *payload_length = len_sid;
      expert_add_info(pinfo, length_item, &ei_docsis_len);
    }
    /* Pass off to the Extended Header dissection */
    dissect_ehdr (tvb, docsis_tree, pinfo, is_encrypted);
  }
  else
  {
    /* Add in MAC Parm field only */
    proto_tree_add_item (docsis_tree, hf_docsis_macparm, tvb, 1, 1, ENC_BIG_ENDIAN);
    length_item = proto_tree_add_item (docsis_tree, hf_docsis_len, tvb, 2, 2, ENC_BIG_ENDIAN);
    /* Validate PDU length */
    if (len_sid > *payload_length)
    {
      *payload_length = len_sid;
      expert_add_info(pinfo, length_item, &ei_docsis_len);
    }
  }
  return;
}

/* Code to Dissect Encrypted DOCSIS Frames */
/* Print DST and SRC MACs and do not dissect the payload */
/* Implementation inferred from packet-eth.c */
static void
dissect_encrypted_frame (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree, const guint8 fctype, const guint8 fcparm)
{
  guint32           offset, frame_len;
  const guint8      *src_addr, *dst_addr;
  const char        *src_addr_name, *dst_addr_name;
  proto_item        *addr_item;
  proto_tree        *addr_tree=NULL;
//  tvbuff_t          *next_tvb;

  /* According to CM-SP-SEC-v3.1, PDU regions of the following
   * frames must be encrypted when security is enabled:
   * - Variable-length PDU MAC Frames;
   * - Fragmentation MAC Frames;
   * - Registration Request (REG-REQ-MP) MAC Management Message Frames;
   * - Isolation PDU MAC Frames.
   * There are also other corner cases when MAC Management frames might be encrypted
   * (e.g. EH_TYPE=7 as described in CM-SP-MULPIv3.1-I15-180509 Table 17 and Table 20)
   */
  if (fctype == FCTYPE_MACSPC) {
    if (fcparm == FCPARM_MAC_MGMT_HDR) {
      col_append_str (pinfo->cinfo, COL_INFO, " (Encrypted MMM)");
    } else if (fcparm == FCPARM_FRAG_HDR) {
      col_append_str (pinfo->cinfo, COL_INFO, " (Encrypted Fragmentation MAC Frame)");
    } else {
      col_append_str (pinfo->cinfo, COL_INFO, " (Encrypted)");
    }
  } else {
    col_append_str (pinfo->cinfo, COL_INFO, " (Encrypted)");
  }

  offset = 0;
  frame_len = tvb_captured_length_remaining (tvb, offset);
  switch (fctype)
  {
    case FCTYPE_PACKET:
    case FCTYPE_ISOLAT:
    case FCTYPE_MACSPC:
      dst_addr = tvb_get_ptr(tvb, 0, 6);
      dst_addr_name = get_ether_name(dst_addr);
      src_addr = tvb_get_ptr(tvb, 6, 6);
      src_addr_name = get_ether_name(src_addr);

      addr_item = proto_tree_add_ether(tree, hf_docsis_dst, tvb, 0, 6, dst_addr);
      addr_tree = proto_item_add_subtree(addr_item, ett_addr);
      addr_item=proto_tree_add_string(addr_tree, hf_docsis_dst_resolved, tvb, 0, 6,
          dst_addr_name);
      proto_item_set_generated(addr_item);
      proto_tree_add_item(addr_tree, hf_docsis_lg, tvb, 0, 3, ENC_BIG_ENDIAN);
      proto_tree_add_item(addr_tree, hf_docsis_ig, tvb, 0, 3, ENC_BIG_ENDIAN);

      addr_item = proto_tree_add_ether(tree, hf_docsis_src, tvb, 6, 6, src_addr);
      addr_tree = proto_item_add_subtree(addr_item, ett_addr);
      addr_item=proto_tree_add_string(addr_tree, hf_docsis_src_resolved, tvb, 6, 6,
          src_addr_name);
      proto_item_set_generated(addr_item);
      proto_tree_add_item(addr_tree, hf_docsis_lg, tvb, 6, 3, ENC_BIG_ENDIAN);
      proto_tree_add_item(addr_tree, hf_docsis_ig, tvb, 6, 3, ENC_BIG_ENDIAN);

      offset += 12;
      break;
  }
  proto_tree_add_item(tree, hf_docsis_encrypted_payload, tvb, offset, frame_len - offset, ENC_NA);

  return;
}

/* Main DOCSIS Dissection Entry Point */
/* Code to Dissect the DOCSIS Frames */
static int
dissect_docsis (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree, void* data _U_)
{
  guint8 fc = 0;
  guint8 fctype = 0;
  guint8 fcparm = 0;
  guint8 exthdr = 0;
  guint16 mac_parm = 0;
  guint8 hdrlen = DOCSIS_MIN_HEADER_LEN;
  guint16 len_sid = 0;
  tvbuff_t *next_tvb = NULL;
  tvbuff_t *mgt_tvb = NULL;
  gint pdulen = 0;
  guint16 payload_length = 0;
  /* guint16 framelen = 0; */
  gboolean save_fragmented;
  gboolean is_encrypted = FALSE;
  gboolean fcs_correct;
  proto_item *ti;
  proto_tree *docsis_tree;

  /* Extract Frame Control parts */
  fc = tvb_get_guint8 (tvb, 0); /* Frame Control Byte */
  fctype = (fc >> 6) & 0x03;    /* Frame Control Type:  2 MSB Bits */
  fcparm = (fc >> 1) & 0x1F;    /* Frame Control Parameter: Next 5 Bits */
  exthdr = (fc & 0x01);         /* Extended Header Bit: LSB */

  /* Extract the MAC Parm; MAC Parm and SID offsets; change for a Queue Depth Request */
  if (fcparm == FCPARM_QUEUE_DEPTH_REQ_FRM) {
    mac_parm = tvb_get_ntohs (tvb, 1);
    len_sid = tvb_get_ntohs (tvb, 3);
    hdrlen = DOCSIS_MIN_HEADER_LEN + 1; // 7-byte header for this message type
  } else {
    mac_parm = tvb_get_guint8 (tvb, 1);
    len_sid = tvb_get_ntohs (tvb, 2);
  }

  /* Set Header Length based on presence of Extended header */
  if (exthdr == EXT_HDR_ON) {
    hdrlen += mac_parm;
  }

  /* Captured Payload Length is based on the length of the header */
  payload_length = tvb_reported_length_remaining (tvb, hdrlen);

  /* If this is a Request Frame, then pdulen is 0 and framelen is 6 */
  if ((fctype == FCTYPE_MACSPC) && (fcparm == FCPARM_RQST_FRM || fcparm == FCPARM_QUEUE_DEPTH_REQ_FRM))
  {
    pdulen = 0;
    /*
    if (fcparm == FCPARM_QUEUE_DEPTH_REQ_FRM)
      framelen = DOCSIS_MIN_HEADER_LEN + 1;
    else
      framelen = DOCSIS_MIN_HEADER_LEN;
    */
  } else {
    /* framelen = DOCSIS_MIN_HEADER_LEN + len_sid; */
    pdulen = len_sid - (mac_parm + 2);
  }

  /* Make entries in Protocol column and Info column on summary display */
  col_set_str (pinfo->cinfo, COL_PROTOCOL, "DOCSIS");

  switch (fctype)
  {
    case FCTYPE_PACKET:
      col_set_str (pinfo->cinfo, COL_INFO, "Packet PDU");
      break;
    case FCTYPE_RESERVED:
      col_set_str (pinfo->cinfo, COL_INFO, "Reserved PDU");
      break;
    case FCTYPE_ISOLAT:
      col_set_str (pinfo->cinfo, COL_INFO, "Isolation PDU");
      break;
    case FCTYPE_MACSPC:
      if (fcparm == FCPARM_RQST_FRM)
        col_add_fstr (pinfo->cinfo, COL_INFO,
                      "Request Frame SID=%u Mini Slots=%u", len_sid,
                      mac_parm);
      else if (fcparm == FCPARM_QUEUE_DEPTH_REQ_FRM)
        col_add_fstr (pinfo->cinfo, COL_INFO,
                      "Request Frame SID=%u, Requested Size=%uxN bytes", len_sid,
                      mac_parm);
      else if (fcparm == FCPARM_FRAG_HDR)
        col_set_str (pinfo->cinfo, COL_INFO, "Fragmented Frame");
      else
        col_set_str (pinfo->cinfo, COL_INFO, "MAC Specific");
      break;
  }  /* switch fctype */

  ti = proto_tree_add_item(tree, proto_docsis, tvb, 0, hdrlen, ENC_NA);
  docsis_tree = proto_item_add_subtree (ti, ett_docsis);

  /* add an item to the subtree, see section 1.6 for more information */

  /* Add in FC Byte fields */
  proto_tree_add_item (docsis_tree, hf_docsis_fctype, tvb, 0, 1, ENC_BIG_ENDIAN);

  switch (fctype)
  {
    case FCTYPE_PACKET:
    {
      proto_item_append_text (ti, " Packet PDU");
      proto_tree_add_item (docsis_tree, hf_docsis_fcparm, tvb, 0, 1, ENC_BIG_ENDIAN);
      proto_tree_add_item (docsis_tree, hf_docsis_exthdr, tvb, 0, 1, ENC_BIG_ENDIAN);
      /* Dissect Length field for a PDU */
      dissect_exthdr_length_field (tvb, pinfo, docsis_tree, exthdr, mac_parm, len_sid, &payload_length, &is_encrypted);
      /* Dissect Header Check Sequence field for a PDU */
      fcs_correct = dissect_hcs_field (tvb, pinfo, docsis_tree, hdrlen);
      if (fcs_correct && pdulen > 0)
      {
        next_tvb =  tvb_new_subset_remaining(tvb, hdrlen);
        if(is_encrypted && !docsis_dissect_encrypted_frames)
          dissect_encrypted_frame (next_tvb, pinfo, docsis_tree, fctype, fcparm);
        else
          call_dissector (eth_withoutfcs_handle, next_tvb, pinfo, docsis_tree);
      }
      break;
    }
    case FCTYPE_RESERVED:
    {
      proto_item_append_text (ti, " Reserved PDU");
      proto_tree_add_item (docsis_tree, hf_docsis_fcparm, tvb, 0, 1, ENC_BIG_ENDIAN);
      proto_tree_add_item (docsis_tree, hf_docsis_exthdr, tvb, 0, 1, ENC_BIG_ENDIAN);
      /* Dissect Length field for a PDU */
      dissect_exthdr_length_field (tvb, pinfo, docsis_tree, exthdr, mac_parm, len_sid, &payload_length, &is_encrypted);
      /* Dissect Header Check Sequence field for a PDU */
      fcs_correct = dissect_hcs_field (tvb, pinfo, docsis_tree, hdrlen);
      if (fcs_correct)
      {
        /* Don't do anything for a Reserved Frame */
        next_tvb =  tvb_new_subset_remaining(tvb, hdrlen);
        call_data_dissector(next_tvb, pinfo, tree);
      }
      break;
    }
    case FCTYPE_ISOLAT:
    {
      proto_item_append_text (ti, " Isolation PDU");
      proto_tree_add_item (docsis_tree, hf_docsis_fcparm, tvb, 0, 1, ENC_BIG_ENDIAN);
      proto_tree_add_item (docsis_tree, hf_docsis_exthdr, tvb, 0, 1, ENC_BIG_ENDIAN);
      /* Dissect Length field for a PDU */
      dissect_exthdr_length_field (tvb, pinfo, docsis_tree, exthdr, mac_parm, len_sid, &payload_length, &is_encrypted);
      /* Dissect Header Check Sequence field for a PDU */
      fcs_correct = dissect_hcs_field (tvb, pinfo, docsis_tree, hdrlen);
      if (fcs_correct && pdulen > 0)
      {
        next_tvb =  tvb_new_subset_remaining(tvb, hdrlen);
        if(is_encrypted && !docsis_dissect_encrypted_frames)
          dissect_encrypted_frame (next_tvb, pinfo, docsis_tree, fctype, fcparm);
        else
          call_dissector (eth_withoutfcs_handle, next_tvb, pinfo, docsis_tree);
      }
      break;
    }
    case FCTYPE_MACSPC:
    {
      proto_item_append_text (ti, " MAC-Specific PDU");
      proto_tree_add_item (docsis_tree, hf_docsis_machdr_fcparm, tvb, 0, 1, ENC_BIG_ENDIAN);
      proto_tree_add_item (docsis_tree, hf_docsis_exthdr, tvb, 0, 1, ENC_BIG_ENDIAN);
      switch(fcparm)
      {
        case FCPARM_TIMING_HDR:
          // no break
        case FCPARM_MAC_MGMT_HDR:
        {
          /* Dissect Length field for a PDU */
          dissect_exthdr_length_field (tvb, pinfo, docsis_tree, exthdr, mac_parm, len_sid, &payload_length, &is_encrypted);
          /* Dissect Header Check Sequence field for a PDU */
          fcs_correct = dissect_hcs_field (tvb, pinfo, docsis_tree, hdrlen);
          if (fcs_correct)
          {
            /* Pass off to the DOCSIS Management dissector/s */
            mgt_tvb = tvb_new_subset_remaining(tvb, hdrlen);
            if(is_encrypted && !docsis_dissect_encrypted_frames)
              dissect_encrypted_frame (mgt_tvb, pinfo, docsis_tree, fctype, fcparm);
            else
              call_dissector (docsis_mgmt_handle, mgt_tvb, pinfo, docsis_tree);
          }
          break;
        }
        case FCPARM_RQST_FRM:
        {
          /* Decode for a Request Frame.  No extended header */
          proto_tree_add_uint (docsis_tree, hf_docsis_mini_slots, tvb, 1, 1, mac_parm);
          proto_tree_add_uint (docsis_tree, hf_docsis_sid, tvb, 2, 2, len_sid);
          /* Dissect Header Check Sequence field for a PDU */
          fcs_correct = dissect_hcs_field (tvb, pinfo, docsis_tree, hdrlen);
          if (fcs_correct)
          {
            /* Don't do anything for a Request Frame, there is no data following it */
          }
          break;
        }
        case FCPARM_FRAG_HDR:
        {
          /* Check if this is a fragmentation header */
          save_fragmented = pinfo->fragmented;
          pinfo->fragmented = TRUE;

          /* Dissect Length field for a PDU */
          dissect_exthdr_length_field (tvb, pinfo, docsis_tree, exthdr, mac_parm, len_sid, &payload_length, &is_encrypted);
          /* Dissect Header Check Sequence field for a PDU */
          fcs_correct = dissect_hcs_field (tvb, pinfo, docsis_tree, hdrlen);
          if (fcs_correct)
          {
            /* Grab the Fragment FCS */
            guint32 sent_fcs = tvb_get_ntohl(tvb, (hdrlen + len_sid - 4));
            guint32 fcs = crc32_802_tvb(tvb, tvb_captured_length(tvb) - 4);

            /* Only defragment valid frames with a good FCS */
            if (sent_fcs == fcs)
            {
              fragment_head *frag_msg = NULL;
              frag_msg = fragment_add_seq_check(&docsis_reassembly_table,
                                                tvb, hdrlen, pinfo,
                                                frag_sid, NULL, /* ID for fragments belonging together */
                                                frag_seq, /* Fragment Sequence Number */
                                                (len_sid - 4), /* fragment length - to the end */
                                                !(frag_flags & FRAG_LAST)); /* More fragments? */

              next_tvb = process_reassembled_data(tvb, hdrlen, pinfo,
                                                  "Reassembled Message", frag_msg, &docsis_frag_items,
                                                  NULL, docsis_tree);

              if (frag_flags == FRAG_LAST)
                pinfo->fragmented = FALSE;
              else
                pinfo->fragmented = TRUE;

              if (frag_msg) { /* Reassembled */
                proto_item_append_text (ti, " (Message Reassembled)");
              } else { /* Not last packet of reassembled Short Message */
                proto_item_append_text (ti, " (Message fragment %u)", frag_seq);

              }

              if(next_tvb)
              {
                /* By default assume an Ethernet payload */
              if(is_encrypted && !docsis_dissect_encrypted_frames)
                dissect_encrypted_frame (next_tvb, pinfo, docsis_tree, fctype, fcparm);
              else
                call_dissector (eth_withoutfcs_handle, next_tvb, pinfo, docsis_tree);
              } else {
                /* Otherwise treat as Data */
                tvbuff_t *payload_tvb = tvb_new_subset_length_caplen(tvb, hdrlen, (len_sid - 4), -1);
                call_data_dissector(payload_tvb, pinfo, docsis_tree);
              }
            } else {
              /* Report frames with a bad FCS */
              expert_add_info(pinfo, ti, &ei_docsis_frag_fcs_bad);
            }
            /* Add the Fragment FCS to the end of the parent tree */
            proto_tree_add_checksum(docsis_tree, tvb, (hdrlen + len_sid - 4), hf_docsis_frag_fcs, hf_docsis_frag_fcs_status, &ei_docsis_frag_fcs_bad, pinfo, fcs, ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY);

            pinfo->fragmented = save_fragmented;
          }
          break;
        }
        case FCPARM_QUEUE_DEPTH_REQ_FRM:
        {
          /* Decode for a Queue-depth Based Request */
          proto_tree_add_uint (docsis_tree, hf_docsis_requested_size, tvb, 1, 2, mac_parm);
          proto_tree_add_uint (docsis_tree, hf_docsis_sid, tvb, 3, 2, len_sid);
          /* Dissect Header Check Sequence field for a PDU */
          fcs_correct = dissect_hcs_field (tvb, pinfo, docsis_tree, hdrlen);
          if (fcs_correct)
          {
          /* No PDU Payload for this frame */
          }
          break;
        }
        case FCPARM_CONCAT_HDR:
        {
          /* Decode for a Concatenated Header; ONLY for DOCSIS versions < 3.1.  No Extended Header */
          proto_item_append_text (ti, " (Concatenated Header)");
          proto_tree_add_item (docsis_tree, hf_docsis_concat_cnt, tvb, 1, 1, ENC_BIG_ENDIAN);
          proto_tree_add_item (docsis_tree, hf_docsis_len, tvb, 2, 2, ENC_BIG_ENDIAN);
          /* Dissect Header Check Sequence field for a PDU */
          fcs_correct = dissect_hcs_field (tvb, pinfo, docsis_tree, hdrlen);
          if (fcs_correct)
          {
            // There used to be a section of code here that recursively
            // called dissect_docsis. It has been removed. If you plan on
            // adding concatenated PDU support back you should consider
            // doing something like the following:
            // dissect_docsis(...) {
            //   while(we_have_pdus_remaining) {
            //     int pdu_len = dissect_docsis_pdu(...)
            //     if (pdu_len < 1) {
            //       add_expert...
            //       break;
            //     }
            //   }
            // }
            // Adding back this functionality using recursion might result
            // in this dissector being disabled by default or removed entirely.
          }
          break;
        }
        default:
            /* Unknown parameter, stop dissection */
          break;
      } /* switch fcparm */
      break;
    }
  } /* switch fctype */

  return tvb_captured_length(tvb);
}

/* Register the protocol with Wireshark */
void
proto_register_docsis (void)
{
  static hf_register_info hf[] = {
    {&hf_docsis_fctype,
     {"FCType", "docsis.fctype",
      FT_UINT8, BASE_HEX, VALS (fctype_vals), 0xC0,
      "Frame Control Type", HFILL}
    },
    {&hf_docsis_fcparm,
     {"FCParm", "docsis.fcparm",
      FT_UINT8, BASE_DEC, VALS (pkt_fcparm_vals), 0x3E,
      "Parameter Field", HFILL}
    },
    {&hf_docsis_machdr_fcparm,
     {"FCParm", "docsis.fcparm",
      FT_UINT8, BASE_DEC, VALS (ms_fcparm_vals), 0x3E,
      "Parameter Field", HFILL}
    },
    {&hf_docsis_exthdr,
     {"EXTHDR", "docsis.exthdr",
      FT_BOOLEAN, 8, TFS (&exthdr_tfs), 0x01,
      "Extended Header Presence", HFILL}
    },
    {&hf_docsis_macparm,
     {"MACParm", "docsis.macparm",
      FT_UINT8, BASE_HEX, NULL, 0x0,
      "MAC Parameter Field", HFILL}
    },
    {&hf_docsis_concat_cnt,
     {"Number of Concatenated Frames", "docsis.concat_cnt",
      FT_UINT8, BASE_DEC, NULL, 0x0,
      NULL, HFILL}
    },
    {&hf_docsis_ehdrlen,
     {"Extended Header Length (bytes)", "docsis.ehdrlen",
      FT_UINT8, BASE_DEC, NULL, 0x0,
      NULL, HFILL}
    },
    {&hf_docsis_len,
     {"Length of the MAC frame (bytes)", "docsis.len",
      FT_UINT16, BASE_DEC, NULL, 0x0,
      "Length of the MAC frame, not counting the fixed-length MAC header", HFILL}
    },
    {&hf_docsis_eh_type,
     {"Type", "docsis.ehdr.type",
      FT_UINT8, BASE_DEC, VALS (eh_type_vals), 0xF0,
      "TLV Type", HFILL}
    },
    {&hf_docsis_eh_len,
     {"Length", "docsis.ehdr.len",
      FT_UINT8, BASE_DEC, NULL, 0x0F,
      "TLV Len", HFILL}
    },
    {&hf_docsis_eh_val,
     {"Value", "docsis.ehdr.value",
      FT_BYTES, BASE_NONE, NULL, 0x0,
      "TLV Value", HFILL}
    },
    {&hf_docsis_ehx_type,
     {"Extended Type", "docsis.ehdr.ehx_type",
      FT_UINT8, BASE_DEC, NULL, 0x0,
      "TLV Type", HFILL}
    },
    {&hf_docsis_ehx_len,
     {"Extended Length", "docsis.ehdr.ehx_len",
      FT_UINT8, BASE_DEC, NULL, 0x0,
      "TLV Len", HFILL}
    },
    {&hf_docsis_frag_rsvd,
     {"Reserved", "docsis.frag_rsvd",
      FT_UINT8, BASE_DEC, NULL, 0xC0,
      NULL, HFILL}
    },
    {&hf_docsis_frag_first,
     {"First Frame", "docsis.frag_first",
      FT_BOOLEAN, 8, NULL, 0x20,
      NULL, HFILL}
    },
    {&hf_docsis_frag_last,
     {"Last Frame", "docsis.frag_last",
      FT_BOOLEAN, 8, NULL, 0x10,
      NULL, HFILL}
    },
    {&hf_docsis_frag_seq,
     {"Fragmentation Sequence #", "docsis.frag_seq",
      FT_UINT8, BASE_DEC, NULL, 0x0F,
      "Fragmentation Sequence Number", HFILL}
    },
    {&hf_docsis_sid,
     {"SID", "docsis.ehdr.sid",
      FT_UINT16, BASE_DEC_HEX, NULL, 0x3FFF,
      "Service Identifier", HFILL}
    },
    {&hf_docsis_said,
     {"SAID", "docsis.ehdr.said",
      FT_UINT16, BASE_DEC, NULL, 0x3FFF,
      "Security Association Identifier", HFILL}
    },
    {&hf_docsis_reserved,
     {"Reserved", "docsis.ehdr.rsvd",
      FT_UINT8, BASE_HEX, NULL, 0x0,
      "Reserved Byte", HFILL}
    },
    {&hf_docsis_mini_slots,
     {"MiniSlots", "docsis.ehdr.minislots",
      FT_UINT8, BASE_DEC, NULL, 0x0,
      "Mini Slots Requested", HFILL}
    },
    {&hf_docsis_requested_size,
     {"Requested bytes in units of N bytes, N a parameter of the service flow for which this request is being made", "docsis.ehdr.reqsize",
      FT_UINT16, BASE_DEC, NULL, 0x0,
      NULL, HFILL}
    },
    {&hf_docsis_key_seq,
     {"Key Sequence", "docsis.ehdr.keyseq",
      FT_UINT8, BASE_DEC, NULL, 0xF0,
      NULL, HFILL}
    },
    {&hf_docsis_ehdr_ver,
     {"Version", "docsis.ehdr.ver",
      FT_UINT8, BASE_DEC, NULL, 0x0F,
      NULL, HFILL}
    },
    {&hf_docsis_ehdr_phsi,
     {"Payload Header Suppression Index", "docsis.ehdr.phsi",
      FT_UINT8, BASE_DEC|BASE_SPECIAL_VALS, VALS(unique_no_phs), 0x0,
      NULL, HFILL}
    },
    {&hf_docsis_ehdr_qind,
     {"Queue Indicator", "docsis.ehdr.qind",
      FT_BOOLEAN, 8, TFS(&qind_tfs), 0x80,
      NULL, HFILL}
    },
    {&hf_docsis_ehdr_grants,
     {"Active Grants", "docsis.ehdr.act_grants",
      FT_UINT8, BASE_DEC, NULL, 0x7F,
      NULL, HFILL}
    },
    {&hf_docsis_ehdr_bpup2_key_seq,
     {"Key Sequence", "docsis.ehdr.bpup2_keyseq",
      FT_UINT8, BASE_DEC, NULL, 0xF0,
      NULL, HFILL}
    },
    {&hf_docsis_ehdr_bpup2_ver,
     {"Version", "docsis.ehdr.bpup2_ver",
      FT_UINT8, BASE_DEC, NULL, 0x0F,
      NULL, HFILL}
    },
    {&hf_docsis_ehdr_bpup2_bpi_en,
     {"Encryption", "docsis.ehdr.bpup2_bpi_en",
      FT_BOOLEAN, 8, TFS (&tfs_enabled_disabled), 0x80,
      "BPI Enable", HFILL}
    },
    {&hf_docsis_ehdr_bpup2_toggle_bit,
     {"Toggle", "docsis.ehdr.bpup2_toggle_bit",
      FT_BOOLEAN, 8, TFS (&odd_even_tfs), 0x40,
      NULL, HFILL}
    },
    {&hf_docsis_ehdr_bpup2_sid,
     {"SID", "docsis.ehdr.bpup2_sid",
      FT_UINT16, BASE_DEC, NULL, 0x3FFF,
      "Service Identifier", HFILL}
    },
    { &hf_docsis_ehdr_pv_st_refpt,
     { "Start Reference Point", "docsis.ehdr.pv_start_ref",
       FT_UINT8, BASE_DEC, NULL, 0x0,
       NULL, HFILL}
    },
    { &hf_docsis_ehdr_pv_timestamp,
     { "Timestamp", "docsis.ehdr.pv_timestamp",
       FT_UINT32, BASE_DEC, NULL, 0x0,
       NULL, HFILL}
    },
    {&hf_docsis_ehdr_ds_traffic_pri,
     {"DS Traffic Priority", "docsis.ehdr.ds_traffic_pri",
      FT_UINT8, BASE_DEC, NULL, 0xE0,
      NULL, HFILL}
    },
    {&hf_docsis_ehdr_ds_seq_chg_cnt,
     {"DS Sequence Change Count", "docsis.ehdr.ds_seq_chg_cnt",
      FT_UINT8, BASE_DEC, NULL, 0x10,
      NULL, HFILL}
    },
    {&hf_docsis_ehdr_ds_dsid,
     {"DS DSID", "docsis.ehdr.ds_dsid",
      FT_UINT32, BASE_DEC, NULL, 0x0FFFFF,
      NULL, HFILL}
    },
    {&hf_docsis_ehdr_ds_pkt_seq_num,
     {"DS Packet Sequence Number", "docsis.ehdr.ds_pkt_seq_num",
      FT_UINT16, BASE_DEC, NULL, 0x0,
      NULL, HFILL}
    },
    {&hf_docsis_hcs,
     {"Header check sequence", "docsis.hcs",
      FT_UINT16, BASE_HEX, NULL, 0x0,
      NULL, HFILL}
    },
    { &hf_docsis_hcs_status,
     { "HCS Status", "docsis.hcs.status",
       FT_UINT8, BASE_NONE, VALS(local_proto_checksum_vals), 0x0,
       NULL, HFILL}
    },
    { &hf_docsis_bpi_en,
     { "Encryption", "docsis.bpi_en",
       FT_BOOLEAN, 8, TFS (&tfs_enabled_disabled), 0x80,
       "BPI Enable", HFILL}
    },
    { &hf_docsis_toggle_bit,
     { "Toggle", "docsis.toggle_bit",
       FT_BOOLEAN, 8, TFS (&odd_even_tfs), 0x40,
       NULL, HFILL}
    },
    { &hf_docsis_fragment_overlap,
     { "Fragment overlap", "docsis.fragment.overlap",
       FT_BOOLEAN, BASE_NONE, NULL, 0x0,
       "Fragment overlaps with other fragments", HFILL}
    },
    { &hf_docsis_fragment_overlap_conflict,
     { "Conflicting data in fragment overlap", "docsis.fragment.overlap.conflict",
       FT_BOOLEAN, BASE_NONE, NULL, 0x0,
       "Overlapping fragments contained conflicting data", HFILL}
    },
    { &hf_docsis_fragment_multiple_tails,
     { "Multiple tail fragments found", "docsis.fragment.multipletails",
       FT_BOOLEAN, BASE_NONE, NULL, 0x0,
       "Several tails were found when defragmenting the packet", HFILL}
    },
    { &hf_docsis_fragment_too_long_fragment,
     { "Fragment too long", "docsis.fragment.toolongfragment",
       FT_BOOLEAN, BASE_NONE, NULL, 0x0,
       "Fragment contained data past end of packet", HFILL}
    },
    { &hf_docsis_fragment_error,
     { "Defragmentation error", "docsis.fragment.error",
       FT_FRAMENUM, BASE_NONE, NULL, 0x0,
       "Defragmentation error due to illegal fragments", HFILL}
    },
    { &hf_docsis_fragment_count,
     { "Fragment count", "docsis.fragment.count",
       FT_UINT32, BASE_DEC, NULL, 0x0,
       NULL, HFILL}
    },
    { &hf_docsis_fragment,
     { "DOCSIS Fragment", "docsis.fragment",
       FT_FRAMENUM, BASE_NONE, NULL, 0x0,
       NULL, HFILL}
    },
    { &hf_docsis_fragments,
     { "DOCSIS Fragments", "docsis.fragments",
       FT_BYTES, BASE_NONE, NULL, 0x0,
       NULL, HFILL}
    },
    { &hf_docsis_reassembled_in,
     { "Reassembled DOCSIS in frame", "docsis.reassembled_in",
       FT_FRAMENUM, BASE_NONE, NULL, 0x0,
       "This DOCSIS packet is reassembled in this frame", HFILL}
    },
    { &hf_docsis_reassembled_length,
     { "Reassembled DOCSIS length", "docsis.reassembled.length",
       FT_UINT32, BASE_DEC, NULL, 0x0,
       "The total length of the reassembled payload", HFILL}
    },
    { &hf_docsis_reassembled_data,
     { "Reassembled DOCSIS data", "docsis.reassembled.data",
       FT_BYTES, BASE_NONE, NULL, 0x0,
       "The reassembled payload", HFILL}
    },
    { &hf_docsis_frag_fcs,
     { "Fragment FCS", "docsis.frag.fcs",
       FT_UINT32, BASE_HEX, NULL, 0x0,
       NULL, HFILL}
    },
    { &hf_docsis_frag_fcs_status,
     { "Fragment FCS Status", "docsis.frag.fcs.status",
       FT_UINT8, BASE_NONE, VALS(local_proto_checksum_vals), 0x0,
       NULL, HFILL}
    },
    { &hf_docsis_encrypted_payload,
      { "Encrypted Payload", "docsis.encrypted_payload",
        FT_BYTES, BASE_NONE, NULL, 0x0,
        "Encrypted data", HFILL }
    }
  };

  static ei_register_info ei[] = {
      { &ei_docsis_hcs_bad, { "docsis.hcs_bad", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
      { &ei_docsis_len, { "docsis.len.past_end", PI_MALFORMED, PI_ERROR, "Length field value goes past the end of the payload", EXPFILL }},
      { &ei_docsis_frag_fcs_bad, { "docsis.frag.fcs_bad", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
      { &ei_docsis_eh_len, { "docsis.ehdr.len.past_end", PI_MALFORMED, PI_ERROR, "Extended Header Length Invalid!", EXPFILL }}
  };

  static gint *ett[] = {
      &ett_docsis,
      &ett_ehdr,
      &ett_docsis_fragment,
      &ett_docsis_fragments,
      &ett_addr
  };

  module_t *docsis_module;
  expert_module_t* expert_docsis;

  proto_docsis = proto_register_protocol ("DOCSIS", "DOCSIS", "docsis");
  proto_register_field_array (proto_docsis, hf, array_length (hf));
  proto_register_subtree_array (ett, array_length (ett));

  /* register expert notifications */
  expert_docsis = expert_register_protocol(proto_docsis);
  expert_register_field_array(expert_docsis, ei, array_length(ei));

  /* Register configuration preferences */
  docsis_module = prefs_register_protocol(proto_docsis, NULL);
  prefs_register_bool_preference(docsis_module, "check_fcs",
                                 "Validate the DOCSIS checksum if possible",
                                 "Whether or not to validate the Header Check Sequence",
                                 &docsis_check_fcs);
   prefs_register_bool_preference(docsis_module, "dissect_encrypted_frames",
                                 "Ignore EH 'encrypted' bit",
                                 "Whether or not to attempt to dissect encrypted DOCSIS payload",
                                 &docsis_dissect_encrypted_frames);

#if 0
  docsis_dissector_table = register_dissector_table ("docsis",
                                                     "DOCSIS Encapsulation Type", proto_docsis,
                                                     FT_UINT8, BASE_DEC);
#endif

  docsis_handle = register_dissector ("docsis", dissect_docsis, proto_docsis);
  reassembly_table_register(&docsis_reassembly_table,
                        &addresses_reassembly_table_functions);
}

void
proto_reg_handoff_docsis (void)
{
  dissector_add_uint ("wtap_encap", WTAP_ENCAP_DOCSIS, docsis_handle);

  hf_docsis_dst = proto_registrar_get_id_byname ("eth.dst");
  hf_docsis_dst_resolved = proto_registrar_get_id_byname ("eth.dst_resolved");
  hf_docsis_src = proto_registrar_get_id_byname ("eth.src");
  hf_docsis_src_resolved = proto_registrar_get_id_byname ("eth.src_resolved");
  hf_docsis_lg = proto_registrar_get_id_byname ("eth.lg");
  hf_docsis_ig = proto_registrar_get_id_byname ("eth.ig");

  docsis_mgmt_handle = find_dissector ("docsis_mgmt");
  eth_withoutfcs_handle = find_dissector_add_dependency("eth_withoutfcs", proto_docsis);
}

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