aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-homeplug.c
blob: 76d90cd4695696301ecc3c54c4de0c4782c6200c (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
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
/* packet-homeplug.c
 * Routines for homeplug dissection
 *
 * Copyright 2006, Sebastien Tandel <sebastien[AT]tandel.be>
 * Copyright 2009, Luca Ceresoli <luca[AT]lucaceresoli.net>
 *
 * $Id$
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <glib.h>

#include <epan/packet.h>

#include <epan/etypes.h>

#include <epan/proto.h>
#include <epan/ptvcursor.h>

static int proto_homeplug		= -1;

static int hf_homeplug_mctrl		= -1;
  static int hf_homeplug_mctrl_reserved = -1;
  static int hf_homeplug_mctrl_ne	= -1;
static int hf_homeplug_mehdr		= -1;
  static int hf_homeplug_mehdr_mev	= -1;
  static int hf_homeplug_mehdr_metype	= -1;
static int hf_homeplug_melen		= -1;
static int hf_homeplug_mme		= -1;
  /* Request Channel Estimation */
  static int hf_homeplug_rce		= -1;
    static int hf_homeplug_rce_cev	= -1;
    static int hf_homeplug_rce_rsvd	= -1;
  /* Channel Estimation Response */
  static int hf_homeplug_cer		= -1;
    static int hf_homeplug_cer_cerv	= -1;
    static int hf_homeplug_cer_rsvd1	= -1;
    static int hf_homeplug_cer_rxtmi	= -1;
    static int hf_homeplug_cer_vt	= -1;
    static int hf_homeplug_cer_rate	= -1;
    static int hf_homeplug_cer_bp	= -1;
    static int hf_homeplug_cer_mod	= -1;
    static int hf_homeplug_cer_vt11	= -1;
    static int hf_homeplug_cer_rsvd2	= -1;
    static int hf_homeplug_cer_nbdas	= -1;
    static int hf_homeplug_cer_bda	= -1;
  /* Vendor Specific */
  static int hf_homeplug_vs		= -1;
    static int hf_homeplug_vs_oui	= -1;
    static int hf_homeplug_vs_vd	= -1;
  /* Set Network Encryption Key */
  static int hf_homeplug_snk		= -1;
    static int hf_homeplug_snk_eks	= -1;
    static int hf_homeplug_snk_nek	= -1;
  /* Request Parameters and Statistics */
  static int hf_homeplug_rps		= -1;
  /* Parameters and Statistics Response */
  static int hf_homeplug_psr		= -1;
    static int hf_homeplug_psr_txack	= -1;
    static int hf_homeplug_psr_txnack	= -1;
    static int hf_homeplug_psr_txfail	= -1;
    static int hf_homeplug_psr_txcloss	= -1;
    static int hf_homeplug_psr_txcoll	= -1;
    static int hf_homeplug_psr_txca3lat	= -1;
    static int hf_homeplug_psr_txca2lat = -1;
    static int hf_homeplug_psr_txca1lat	= -1;
    static int hf_homeplug_psr_txca0lat = -1;
    static int hf_homeplug_psr_rxbp40	= -1;
  /* Set Local Parameters */
  static int hf_homeplug_slp		= -1;
    static int hf_homeplug_slp_ma	= -1;
  /* Network Statistics */
  static int hf_homeplug_ns		      = -1;
  static int hf_homeplug_ns_extended	      = -1;
    /* Basic */
    static int hf_homeplug_ns_netw_ctrl_ac    = -1;
    static int hf_homeplug_ns_netw_ctrl_icid  = -1;
    static int hf_homeplug_ns_bytes40_robo    = -1;
    static int hf_homeplug_ns_fails_robo      = -1;
    static int hf_homeplug_ns_drops_robo      = -1;
    static int hf_homeplug_ns_netw_da	      = -1;
    static int hf_homeplug_ns_bytes40	      = -1;
    static int hf_homeplug_ns_fails	      = -1;
    static int hf_homeplug_ns_drops	      = -1;
    /* Extended */
    static int hf_homeplug_ns_tx_bfr_state	= -1;
    static int hf_homeplug_ns_buf_in_use	= -1;
    static int hf_homeplug_ns_prio		= -1;
    static int hf_homeplug_ns_msdu_len		= -1;
    static int hf_homeplug_ns_seqn		= -1;
    static int hf_homeplug_ns_toneidx		= -1;
  /* Bridging Characteristics Network */
  static int hf_homeplug_bcn		= -1;
    static int hf_homeplug_bcn_network	= -1;
    static int hf_homeplug_bcn_return	= -1;
    static int hf_homeplug_bcn_rsvd	= -1;
    static int hf_homeplug_bcn_fbn	= -1;
    static int hf_homeplug_bcn_brda	= -1;
    static int hf_homeplug_bcn_bp_das	= -1;
    static int hf_homeplug_bcn_bp_da	= -1;
  /* Bridging Characteristics Local */
  static int hf_homeplug_bcl		= -1;
    static int hf_homeplug_bcl_network	= -1;
    static int hf_homeplug_bcl_return	= -1;
    static int hf_homeplug_bcl_rsvd	= -1;
    static int hf_homeplug_bcl_hprox_das= -1;
    static int hf_homeplug_bcl_hpbda	= -1;
  /* Set Transmit Characteristics */
  static int hf_homeplug_stc		= -1;
    static int hf_homeplug_stc_lco	= -1;
    static int hf_homeplug_stc_encf	= -1;
    static int hf_homeplug_stc_txprio	= -1;
    static int hf_homeplug_stc_rexp	= -1;
    static int hf_homeplug_stc_txcf	= -1;
    static int hf_homeplug_stc_cftop	= -1;
    static int hf_homeplug_stc_rsvd1	= -1;
    static int hf_homeplug_stc_retry	= -1;
    static int hf_homeplug_stc_rsvd2	= -1;
    static int hf_homeplug_stc_dder	= -1;
    static int hf_homeplug_stc_dur	= -1;
    static int hf_homeplug_stc_ebp	= -1;
    static int hf_homeplug_stc_dees	= -1;
    static int hf_homeplug_stc_txeks	= -1;
static int hf_homeplug_data      = -1;

static gint ett_homeplug		= -1;
static gint ett_homeplug_mctrl		= -1;
static gint ett_homeplug_mehdr		= -1;
static gint ett_homeplug_rce		= -1;
static gint ett_homeplug_cer		= -1;
static gint ett_homeplug_vs		= -1;
static gint ett_homeplug_snk		= -1;
static gint ett_homeplug_rps		= -1;
static gint ett_homeplug_psr		= -1;
static gint ett_homeplug_slp		= -1;
static gint ett_homeplug_ns		= -1;
static gint ett_homeplug_tone		= -1;
static gint ett_homeplug_tx_bfr_state	= -1;
static gint ett_homeplug_bridge		= -1;
static gint ett_homeplug_bcn		= -1;
static gint ett_homeplug_bcl		= -1;
static gint ett_homeplug_stc		= -1;


static guint8 homeplug_ne = 0;
static guint8 homeplug_melen = 0;
static guint8 homeplug_metype = 0;

static guint32	homeplug_offset = 0;


/* METYPE Values */
#define HOMEPLUG_MME_RCE      0x00
#define HOMEPLUG_MME_CER      0x01
#define HOMEPLUG_MME_VS       0x02
#define HOMEPLUG_MME_RBA      0x03
#define HOMEPLUG_MME_SNK      0x04
#define HOMEPLUG_MME_MWR      0x05
#define HOMEPLUG_MME_CNK      0x06
#define HOMEPLUG_MME_RPS      0x07
#define HOMEPLUG_MME_PSR      0x08
#define HOMEPLUG_MME_SLP      0x19
#define HOMEPLUG_MME_NS	      0x1A
#define HOMEPLUG_MME_RES      0x1B
#define HOMEPLUG_MME_PS       0x1C
#define HOMEPLUG_MME_SLO      0x1D
#define HOMEPLUG_MME_BC       0x1E
#define HOMEPLUG_MME_STC      0x1F
static const value_string homeplug_metype_vals[] = {
    { HOMEPLUG_MME_RCE, "Request Channel Estimation" },
    { HOMEPLUG_MME_CER, "Channel Estimation Response" },
    { HOMEPLUG_MME_VS , "Vendor Specific" },
    { HOMEPLUG_MME_RBA, "Replace Bridge Address" },
    { HOMEPLUG_MME_SNK, "Set Network Encryption Key" },
    { HOMEPLUG_MME_MWR, "Multicast With Response" },
    { HOMEPLUG_MME_CNK, "Confirm Network Encryption Key" },
    { HOMEPLUG_MME_RPS, "Request Parameters and Statistics" },
    { HOMEPLUG_MME_PSR, "Parameters and Statistics Response" },
    { HOMEPLUG_MME_SLP, "Set Local Parameters" },
    { HOMEPLUG_MME_NS , "Network Statistics" },
    { HOMEPLUG_MME_RES, "Reserved" },
    { HOMEPLUG_MME_PS , "Performance Statistics" },
    { HOMEPLUG_MME_SLO, "Set Local Overrides" },
    { HOMEPLUG_MME_BC , "Bridging Characteristics" },
    { HOMEPLUG_MME_STC, "Set Transmit Characteristics" },
    { 0, NULL }
};

/* Bit mask Operation */
#define HOMEPLUG_MCTRL_RSVD   0x80
#define HOMEPLUG_MCTRL_NE     0x7F

#define HOMEPLUG_MEHDR_MEV    0xE0
#define HOMEPLUG_MEHDR_METYPE 0x1F

#define HOMEPLUG_NS_AC	      0x80
#define HOMEPLUG_NS_ICID      0x7F
#define HOMEPLUG_NS_BUF_IN_USE 0x80
#define HOMEPLUG_NS_PRIO      0x60
#define HOMEPLUG_NS_MSDU_LEN  0x1F
#define HOMEPLUG_NS_SEQN      0xF0
#define HOMEPLUG_NS_TONEIDX   0x0F

/* string values in function of AC */
static const true_false_string homeplug_ns_ac_vals = {
  "(from host) Return basic network statistics",
  "(from host) Clear basic network statistics"
};

/* string values in function of BUF_IN_USE */
static const true_false_string homeplug_ns_buf_in_use_vals = {
  "Buffer is in use",
  "Buffer is available"
};

#define HOMEPLUG_RCE_CEV      0xF0
#define HOMEPLUG_RCE_RSVD     0x0F

#define HOMEPLUG_CER_CERV     0xF0
#define HOMEPLUG_CER_RSVD     0x0FE0
#define HOMEPLUG_CER_RXTMI    0x1F
#define HOMEPLUG_CER_RATE     0x80
#define HOMEPLUG_CER_BP	      0x40
#define HOMEPLUG_CER_VT11     0x0F
#define HOMEPLUG_CER_RSVD2    0x80
#define HOMEPLUG_CER_NBDAS    0x7F

#define HOMEPLUG_BC_NETWORK   0x80
#define HOMEPLUG_BC_RETURN    0x40
#define HOMEPLUG_BCL_RSVD     0x3F
#define HOMEPLUG_BCN_RSVD     0x30
#define HOMEPLUG_BCN_FBN      0x0F

/* string values in function of BC_NETWORK */
static const true_false_string homeplug_bc_network_vals = {
  "Network bridge information",
  "Local bridge information"
};

/* string values in function of BC_RETURN */
static const true_false_string homeplug_bc_return_vals = {
  "Return bridging characteristics",
  "Set bridging characteristics"
};

#define HOMEPLUG_STC_LCO      0x80
#define HOMEPLUG_STC_ENCF     0x40
#define HOMEPLUG_STC_TXPRIO   0x30
#define HOMEPLUG_STC_REXP     0x08
#define HOMEPLUG_STC_TXCF     0x04
#define HOMEPLUG_STC_CFTOP    0x02
#define HOMEPLUG_STC_RSVD1    0x01
#define HOMEPLUG_STC_RETRY    0xC0
#define HOMEPLUG_STC_RSVD2    0x30
#define HOMEPLUG_STC_DDER     0x08
#define HOMEPLUG_STC_DUR      0x04
#define HOMEPLUG_STC_EBP      0x02
#define HOMEPLUG_STC_DEES     0x01

/* string values in function of CFTOP */
static const true_false_string homeplug_stc_cftop_vals = {
  "CA2",
  "CA3"
};

/* string values in function of RETRY */
static const value_string homeplug_stc_retry_vals[] = {
  { 0, "No retries"},
  { 1, "One retry only"},
  { 2, "Normal retries based on specification"},
  { 3, "Reserved"},
  { 0, NULL}
};

/*  Length of Network Statistics Response defines whether it is the Basic or
 *  the Extended Response */
#define HOMEPLUG_NS_BASIC_LEN 187
#define HOMEPLUG_NS_EXT_LEN   199

/* IC_ID Values */
#define HOMEPLUG_NS_ICID5130A1		0x00
#define HOMEPLUG_NS_ICID51X1USB		0x01
#define HOMEPLUG_NS_ICID51X1PHY		0x02
#define HOMEPLUG_NS_ICID51X1HOST	0x03
#define HOMEPLUG_NS_ICID5130A2		0x04
#define HOMEPLUG_NS_ICID_RSVD_MIN	0x05
#define HOMEPLUG_NS_ICID_RSVD_MAX	0x7F
/* string values in function of IC_ID values */
static const range_string homeplug_ns_icid_vals[] = {
    { HOMEPLUG_NS_ICID5130A1,    HOMEPLUG_NS_ICID5130A1,    "INT5130A1" },
    { HOMEPLUG_NS_ICID51X1USB,   HOMEPLUG_NS_ICID51X1USB,   "INT51X1 (USB Option)" },
    { HOMEPLUG_NS_ICID51X1PHY,   HOMEPLUG_NS_ICID51X1PHY,   "INT51X1 (PHY Option)" },
    { HOMEPLUG_NS_ICID51X1HOST,  HOMEPLUG_NS_ICID51X1HOST,  "INT51X1 (Host/DTE Option)" },
    { HOMEPLUG_NS_ICID5130A2,    HOMEPLUG_NS_ICID5130A2,    "INT5130A2" },
    { HOMEPLUG_NS_ICID_RSVD_MIN, HOMEPLUG_NS_ICID_RSVD_MAX, "Reserved"},
    { 0, 0, NULL }
};

/* Modulation Method Bit Mask */
#define HOMEPLUG_CER_MOD_MASK	        0x30
/* Modulation Method Values */
#define HOMEPLUG_CER_MOD_ROBO		0x00
#define HOMEPLUG_CER_MOD_DBPSK		0x01
#define HOMEPLUG_CER_MOD_DQPSK		0x02
#define	HOMEPLUG_CER_MOD_RSVD		0x03
/* string values in function of Modulation Method Values */
static const value_string homeplug_cer_mod_vals[] = {
  { HOMEPLUG_CER_MOD_ROBO,  "ROBO Modulation"},
  { HOMEPLUG_CER_MOD_DBPSK, "DBPSK Modulation"},
  { HOMEPLUG_CER_MOD_DQPSK, "DQPSK Modulation"},
  { HOMEPLUG_CER_MOD_RSVD,  "Reserved"},
  { 0, NULL}
};

/* Constants used by various MMEs */

#define HOMEPLUG_ADDR_INEXISTANT G_GINT64_CONSTANT(010000000000U)

/* string values in function of TXPRIO */
static const value_string homeplug_txprio_vals[] = {
  { 0, "CA0"},
  { 1, "CA1"},
  { 2, "CA2"},
  { 3, "CA3"},
  { 0, NULL}
};

#define HOMEPLUG_MCTRL_LEN 1
#define HOMEPLUG_MEHDR_LEN 1
#define HOMEPLUG_MELEN_LEN 1

void
proto_register_homeplug(void)
{

  static hf_register_info hf[] = {
    /* MAC Control Field */
    { &hf_homeplug_mctrl,
      { "MAC Control Field", "homeplug.mctrl",
      FT_NONE, BASE_NONE, NULL, 0x0, "MAC Control Field", HFILL }
    },

    { &hf_homeplug_mctrl_reserved,
      { "Reserved", "homeplug.mctrl.rsvd",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_MCTRL_RSVD, "Reserved", HFILL }
    },

    { &hf_homeplug_mctrl_ne,
      { "Number of MAC Data Entries", "homeplug.mctrl.ne",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_MCTRL_NE, "Number of MAC Data Entries", HFILL }
    },

    /* MAC Entry Header */
    { &hf_homeplug_mehdr,
      { "MAC Management Entry Header", "homeplug.mehdr",
      FT_NONE, BASE_NONE, NULL, 0x0, "MAC Management Entry Header", HFILL }
    },

    { &hf_homeplug_mehdr_mev,
      { "MAC Entry Version", "homeplug.mehdr.mev",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_MEHDR_MEV, "MAC Entry Version", HFILL }
    },

    { &hf_homeplug_mehdr_metype,
      { "MAC Entry Type", "homeplug.mehdr.metype",
      FT_UINT8, BASE_HEX, VALS(homeplug_metype_vals), HOMEPLUG_MEHDR_METYPE, "MAC Entry Type", HFILL }
    },

    /* MAC Entry Len */
    { &hf_homeplug_melen,
      { "MAC Management Entry Length", "homeplug.melen",
      FT_UINT8, BASE_DEC, NULL, 0x0, "MAC Management Entry Length", HFILL }
    },

    /* MAC Management Entry */
    { &hf_homeplug_mme,
      { "MAC Management Entry Data", "homeplug.mmentry",
      FT_UINT8, BASE_DEC, NULL, 0x0, "MAC Management Entry Data", HFILL }
    },

    /* Request Channel Estimation */
    { &hf_homeplug_rce,
      { "Request Channel Estimation", "homeplug.rce",
      FT_NONE, BASE_NONE, NULL, 0x0, "Request Channel Estimation", HFILL }
    },

    { &hf_homeplug_rce_cev,
      { "Channel Estimation Version", "homeplug.rce.cev",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_RCE_CEV, "Channel Estimation Version", HFILL }
    },

    { &hf_homeplug_rce_rsvd,
      { "Reserved", "homeplug.rce.rsvd",
      FT_NONE, BASE_NONE, NULL, HOMEPLUG_RCE_RSVD, "Reserved", HFILL }
    },

    /* Channel Estimation Response */
    { &hf_homeplug_cer,
      { "Channel Estimation Response", "homeplug.cer",
      FT_NONE, BASE_NONE, NULL, 0x0, "Channel Estimation Response", HFILL }
    },

    { &hf_homeplug_cer_cerv,
      { "Channel Estimation Response Version", "homeplug.cer.cerv",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_CER_CERV, "Channel Estimation Response Version", HFILL }
    },

    { &hf_homeplug_cer_rsvd1,
      { "Reserved", "homeplug.cer.rsvd1",
      FT_NONE, BASE_NONE, NULL, HOMEPLUG_CER_RSVD, "Reserved", HFILL }
    },

    { &hf_homeplug_cer_rxtmi,
      { "Receive Tone Map Index", "homeplug.cer.rxtmi",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_CER_RXTMI, "Receive Tone Map Index", HFILL }
    },

    /* TODO must append vt[79-0] */

    { &hf_homeplug_cer_vt,
      {"Valid Tone Flags", "homeplug.cer.vt",
      FT_UINT8, BASE_HEX, NULL, 0x0, "Valid Tone Flags", HFILL }
    },

    { &hf_homeplug_cer_rate,
      { "FEC Rate", "homeplug.cer.rate",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_CER_RATE, "FEC Rate", HFILL }
    },

    { &hf_homeplug_cer_bp,
      { "Bridge Proxy", "homeplug.cer.bp",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_CER_BP, "Bridge Proxy", HFILL }
    },

    { &hf_homeplug_cer_mod,
      { "Modulation Method", "homeplug.cer.mod",
      FT_UINT8, BASE_DEC, VALS(homeplug_cer_mod_vals), HOMEPLUG_CER_MOD_MASK,
      "Modulation Method", HFILL }
    },

    { &hf_homeplug_cer_vt11,
      { "Valid Tone Flags [83-80]", "homeplug.cer.vt11",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_CER_VT11, "Valid Tone Flags [83-80]", HFILL }
    },

    { &hf_homeplug_cer_rsvd2,
      { "Reserved", "homeplug.cer.rsvd2",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_CER_RSVD2, "Reserved", HFILL }
    },

    { &hf_homeplug_cer_nbdas,
      { "Number Bridged Destination Addresses", "homeplug.cer.nbdas",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_CER_NBDAS, "Number Bridged Destination Addresses", HFILL }
    },

    { &hf_homeplug_cer_bda,
      { "Bridged Destination Address", "homeplug.cer.bda",
      FT_ETHER, BASE_NONE, NULL, 0x0, "Bridged Destination Address", HFILL }
    },

    /* Vendor Specific */
    { &hf_homeplug_vs,
      { "Vendor Specific", "homeplug.vs",
      FT_NONE, BASE_NONE, NULL, 0x0, "Vendor Specific", HFILL }
    },

    { &hf_homeplug_vs_oui,
      { "OUI", "homeplug.vs.oui",
      FT_BYTES, BASE_NONE, NULL, 0x0,
      "Should be an IEEE assigned Organizationally Unique Identifier", HFILL }
    },

    { &hf_homeplug_vs_vd,
      { "Vendor Defined", "homeplug.vs.vd",
      FT_BYTES, BASE_NONE, NULL, 0x0, "Vendor Defined", HFILL }
    },

    /* Set Network Encryption Key */
    { &hf_homeplug_snk,
      { "Set Network Encryption Key", "homeplug.snk",
      FT_NONE, BASE_NONE, NULL, 0x0, "Set Network Encryption Key", HFILL }
    },

    { &hf_homeplug_snk_eks,
      { "Encryption Key Select", "homeplug.snk.eks",
      FT_UINT8, BASE_DEC, NULL, 0x0, "Encryption Key Select", HFILL }
    },

    { &hf_homeplug_snk_nek,
      { "Network Encryption Key", "homeplug.snk.nek",
      FT_BYTES, BASE_NONE, NULL, 0x0, "Network Encryption Key", HFILL }
    },

    /* Request Parameters and Statistics */
    { &hf_homeplug_rps,
      { "Request Parameters and Statistics", "homeplug.rps",
      FT_NONE, BASE_NONE, NULL, 0x0, "Request Parameters and Statistics", HFILL }
    },

    /* Parameters and Statistics Response */
    { &hf_homeplug_psr,
      { "Parameters and Statistics Response", "homeplug.psr",
      FT_NONE, BASE_NONE, NULL, 0x0, "Parameters and Statistics Response", HFILL }
    },

    { &hf_homeplug_psr_txack,
      { "Transmit ACK Counter", "homeplug.psr.txack",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Transmit ACK Counter", HFILL }
    },

    { &hf_homeplug_psr_txnack,
      { "Transmit NACK Counter", "homeplug.psr.txnack",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Transmit NACK Counter", HFILL }
    },

    { &hf_homeplug_psr_txfail,
      { "Transmit FAIL Counter", "homeplug.psr.txfail",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Transmit FAIL Counter", HFILL }
    },

    { &hf_homeplug_psr_txcloss,
      { "Transmit Contention Loss Counter", "homeplug.psr.txcloss",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Transmit Contention Loss Counter", HFILL }
    },

    { &hf_homeplug_psr_txcoll,
      { "Transmit Collision Counter", "homeplug.psr.txcoll",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Transmit Collision Counter", HFILL }
    },

    { &hf_homeplug_psr_txca3lat,
      { "Transmit CA3 Latency Counter", "homeplug.psr.txca3lat",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Transmit CA3 Latency Counter", HFILL }
    },

    { &hf_homeplug_psr_txca2lat,
      { "Transmit CA2 Latency Counter", "homeplug.psr.txca2lat",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Transmit CA2 Latency Counter", HFILL }
    },
    { &hf_homeplug_psr_txca1lat,
      { "Transmit CA1 Latency Counter", "homeplug.psr.txca1lat",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Transmit CA1 Latency Counter", HFILL }
    },
    { &hf_homeplug_psr_txca0lat,
      { "Transmit CA0 Latency Counter", "homeplug.psr.txca0lat",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Transmit CA0 Latency Counter", HFILL }
    },

    { &hf_homeplug_psr_rxbp40,
      { "Receive Cumulative Bytes per 40-symbol", "homeplug.psr.rxbp40",
      FT_UINT32, BASE_DEC, NULL, 0x0, "Receive Cumulative Bytes per 40-symbol", HFILL }
    },

    /* Set Local Parameters */
    { &hf_homeplug_slp,
      { "Set Local Parameters", "homeplug.slp",
      FT_NONE, BASE_NONE, NULL, 0x0, "Set Local Parameters", HFILL }
    },

    { &hf_homeplug_slp_ma,
      { "MAC Address", "homeplug.slp.ma",
      FT_ETHER, BASE_NONE, NULL, 0x0, "MAC Address", HFILL }
    },

    /* Network Statistics */
    { &hf_homeplug_ns,
      { "Network Statistics", "homeplug.ns",
      FT_NONE, BASE_NONE, NULL, 0x0, "Network Statistics", HFILL }
    },

    { &hf_homeplug_ns_extended,
      { "Network Statistics is Extended", "homeplug.ns.extended",
      FT_BOOLEAN, 8, NULL, 0x0, "Network Statistics is Extended (MELEN >= 199)", HFILL }
    },

    { &hf_homeplug_ns_netw_ctrl_ac,
      { "Action Control", "homeplug.ns.ac",
      FT_BOOLEAN, 8, TFS(&homeplug_ns_ac_vals), HOMEPLUG_NS_AC, "Action Control", HFILL }
    },

    { &hf_homeplug_ns_netw_ctrl_icid,
      { "IC_ID", "homeplug.ns.icid",
      FT_UINT8, BASE_HEX|BASE_RANGE_STRING, RVALS(homeplug_ns_icid_vals), HOMEPLUG_NS_ICID, "IC_ID", HFILL }
    },

    { &hf_homeplug_ns_bytes40_robo,
      { "Bytes in 40 symbols in ROBO", "homeplug.ns.bytes40_robo",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Bytes in 40 symbols in ROBO", HFILL }
    },

    { &hf_homeplug_ns_fails_robo,
      { "Fails Received in ROBO", "homeplug.ns.fails_robo",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Fails Received in ROBO", HFILL }
    },

    { &hf_homeplug_ns_drops_robo,
      { "Frame Drops in ROBO", "homeplug.ns.drops_robo",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Frame Drops in ROBO", HFILL }
    },

    { &hf_homeplug_ns_netw_da,
      { "Address of Network DA", "homeplug.ns.netw_da",
      FT_ETHER, BASE_NONE, NULL, 0x0, "Address of Network DA", HFILL }
    },

    { &hf_homeplug_ns_bytes40,
      { "Bytes in 40 symbols", "homeplug.ns.bytes40",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Bytes in 40 symbols", HFILL }
    },

    { &hf_homeplug_ns_fails,
      { "Fails Received", "homeplug.ns.fails",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Fails Received", HFILL }
    },

    { &hf_homeplug_ns_drops,
      { "Frame Drops", "homeplug.ns.drops",
      FT_UINT16, BASE_DEC, NULL, 0x0, "Frame Drops", HFILL }
    },

    /* ... Extended */
    { &hf_homeplug_ns_tx_bfr_state,
      { "Transmit Buffer State", "homeplug.ns.tx_bfr_state",
      FT_NONE, BASE_NONE, NULL, 0x0, "Transmit Buffer State", HFILL }
    },

    { &hf_homeplug_ns_buf_in_use,
      { "Buffer in use", "homeplug.ns.buf_in_use",
      FT_BOOLEAN, 8, TFS(&homeplug_ns_buf_in_use_vals), HOMEPLUG_NS_BUF_IN_USE,
      "Buffer in use (1) or Available (0)", HFILL }
    },

    { &hf_homeplug_ns_prio,
      { "Priority", "homeplug.ns.prio",
      FT_UINT8, BASE_DEC, VALS(homeplug_txprio_vals), HOMEPLUG_NS_PRIO,
      "Priority", HFILL }
    },

    { &hf_homeplug_ns_msdu_len,
      { "MSDU Length", "homeplug.ns.msdu_len",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_NS_MSDU_LEN, "MSDU Length", HFILL }
    },

    { &hf_homeplug_ns_seqn,
      { "Sequence Number", "homeplug.ns.seqn",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_NS_SEQN, "Sequence Number", HFILL }
    },

    { &hf_homeplug_ns_toneidx,
      { "Transmit tone map index", "homeplug.ns.toneidx",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_NS_TONEIDX,
      "Maps to the 16 statistics occurring earlier in this MME", HFILL }
    },

    /* Bridging Characteristics Network */
    { &hf_homeplug_bcn,
      { "Bridging Characteristics Network", "homeplug.bcn",
      FT_NONE, BASE_NONE, NULL, 0x0, "Bridging Characteristics Network", HFILL }
    },

    { &hf_homeplug_bcn_network,
      { "Network", "homeplug.bcn.network",
      FT_BOOLEAN, 8, TFS(&homeplug_bc_network_vals), HOMEPLUG_BC_NETWORK,
      "Local (0) or Network Bridge (1) Information", HFILL }
    },

    { &hf_homeplug_bcn_return,
      { "Return/Set", "homeplug.bcn.return",
      FT_BOOLEAN, 8, TFS(&homeplug_bc_return_vals), HOMEPLUG_BC_RETURN,
      "From host: Return (1) or set bridging characteristics (0)", HFILL }
    },

    { &hf_homeplug_bcn_rsvd,
      { "Reserved", "homeplug.bcn.rsvd",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_BCN_RSVD, "Reserved", HFILL }
    },

    { &hf_homeplug_bcn_fbn,
      { "First Bridge Number", "homeplug.bcn.fbn",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_BCN_FBN, "First Bridge Number", HFILL }
    },

    { &hf_homeplug_bcn_brda,
      { "Address of Bridge", "homeplug.bcn.brda",
      FT_ETHER, BASE_HEX, NULL, 0x0, "Address of Bridge", HFILL }
    },

    { &hf_homeplug_bcn_bp_das,
      { "Number of bridge proxied DAs", "homeplug.bcn.bp_das",
      FT_UINT8, BASE_DEC, NULL, 0x0,
      "Number of bridge proxied DAs supported", HFILL }
    },

    { &hf_homeplug_bcn_bp_da,
      { "Bridged DA", "homeplug.bcn.bp_da",
      FT_ETHER, BASE_NONE, NULL, 0x0, "Bridged Destination Address", HFILL }
    },

    /* Bridging Characteristics Local */
    { &hf_homeplug_bcl,
      { "Bridging Characteristics Local", "homeplug.bcl",
      FT_NONE, BASE_NONE, NULL, 0x0, "Bridging Characteristics Local", HFILL }
    },

    { &hf_homeplug_bcl_network,
      { "Network/Local", "homeplug.bcl.network",
      FT_BOOLEAN, 8, TFS(&homeplug_bc_network_vals), HOMEPLUG_BC_NETWORK,
      "Local (0) or Network Bridge (1) Information", HFILL }
    },

    { &hf_homeplug_bcl_return,
      { "Return/Set", "homeplug.bcl.return",
      FT_BOOLEAN, 8, TFS(&homeplug_bc_return_vals), HOMEPLUG_BC_RETURN,
      "From host: Return (1) or set bridging characteristics (0)", HFILL }
    },

    { &hf_homeplug_bcl_rsvd,
      { "Reserved", "homeplug.bcl.rsvd",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_BCL_RSVD, "Reserved", HFILL }
    },

    { &hf_homeplug_bcl_hprox_das,
      { "Number of host proxied DAs", "homeplug.bcl.hprox_das",
      FT_UINT8, BASE_DEC, NULL, 0x0,
      "Number of host proxied DAs supported by the bridge application", HFILL }
    },

    { &hf_homeplug_bcl_hpbda,
      { "Host Proxied DA", "homeplug.bcl.hpbda",
      FT_ETHER, BASE_NONE, NULL, 0x0, "Host Proxied Bridged Destination Address", HFILL }
    },

    /* Set Transmit Characteristics */
    { &hf_homeplug_stc,
      { "Set Transmit Characteristics", "homeplug.stc",
      FT_NONE, BASE_NONE, NULL, 0x0, "Set Transmit Characteristics", HFILL }
    },

    { &hf_homeplug_stc_lco,
      { "Local Consumption Only", "homeplug.stc.lco",
      FT_BOOLEAN, 8, NULL, HOMEPLUG_STC_LCO,
      "Do not transmit subsequent frames to medium", HFILL }
    },

    { &hf_homeplug_stc_encf,
      { "Encryption Flag", "homeplug.stc.encf",
      FT_BOOLEAN, 8, NULL, HOMEPLUG_STC_ENCF, "Encrypt subsequent frames", HFILL }
    },

    { &hf_homeplug_stc_txprio,
      { "Transmit Priority", "homeplug.stc.txprio",
      FT_UINT8, BASE_DEC, VALS(homeplug_txprio_vals), HOMEPLUG_STC_TXPRIO,
      "Transmit Priority", HFILL }
    },

    { &hf_homeplug_stc_rexp,
      { "Response Expected", "homeplug.stc.rexp",
      FT_BOOLEAN, 8, NULL, HOMEPLUG_STC_REXP,
      "Mark subsequent frames to receive response", HFILL }
    },

    { &hf_homeplug_stc_txcf,
      { "Transmit Contention Free", "homeplug.stc.txcf",
      FT_BOOLEAN, 8, NULL, HOMEPLUG_STC_TXCF,
      "Mark subsequently transmitted frames as contention free", HFILL }
    },

    { &hf_homeplug_stc_cftop,
      { "Contention Free Transmit Override Priority", "homeplug.stc.cftop",
      FT_BOOLEAN, 8, TFS(&homeplug_stc_cftop_vals), HOMEPLUG_STC_CFTOP,
      "Transmit subsequent contention free frames with CA2/CA3 priority", HFILL }
    },

    { &hf_homeplug_stc_rsvd1,
      { "Reserved", "homeplug.stc.rsvd1",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_STC_RSVD1, "Reserved", HFILL }
    },

    { &hf_homeplug_stc_retry,
      { "Retry Control", "homeplug.stc.retry",
      FT_UINT8, BASE_DEC, VALS(homeplug_stc_retry_vals), HOMEPLUG_STC_RETRY,
      "Retry Control", HFILL }
    },

    { &hf_homeplug_stc_rsvd2,
      { "Reserved", "homeplug.stc.rsvd2",
      FT_UINT8, BASE_DEC, NULL, HOMEPLUG_STC_RSVD2, "Reserved", HFILL }
    },

    { &hf_homeplug_stc_dder,
      { "Disable Default Encryption Receive", "homeplug.stc.dder",
      FT_BOOLEAN, 8, NULL, HOMEPLUG_STC_DDER, "Disable Default Encryption Receive", HFILL }
    },

    { &hf_homeplug_stc_dur,
      { "Disable Unencrypted Receive", "homeplug.stc.dur",
      FT_BOOLEAN, 8, NULL, HOMEPLUG_STC_DUR, "Disable Unencrypted Receive", HFILL }
    },

    { &hf_homeplug_stc_ebp,
      { "INT51X1 (Host/DTE Option) Enable Backpressure", "homeplug.stc.ebp",
      FT_BOOLEAN, 8, NULL, HOMEPLUG_STC_EBP,
      "INT51X1 (Host/DTE Option) Enable Backpressure", HFILL }
    },

    { &hf_homeplug_stc_dees,
      { "Disable EEPROM Save", "homeplug.stc.dees",
      FT_BOOLEAN, 8, NULL, HOMEPLUG_STC_DEES, "Disable EEPROM Save", HFILL }
    },

    { &hf_homeplug_stc_txeks,
      { "EKS to be used for encryption", "homeplug.stc.txeks",
      FT_UINT8, BASE_DEC, NULL, 0x0, "EKS to be used for encryption", HFILL }
    },

    /* Undecoded data */
    { &hf_homeplug_data,
      { "Data", "homeplug.data",
      FT_BYTES, BASE_NONE, NULL, 0x0, "Data", HFILL }
    }
  };

  /* Setup protocol subtree array */
  static gint *ett[] = {
    &ett_homeplug,
    &ett_homeplug_mctrl,
    &ett_homeplug_mehdr,
    &ett_homeplug_rce,
    &ett_homeplug_cer,
    &ett_homeplug_vs,
    &ett_homeplug_snk,
    &ett_homeplug_rps,
    &ett_homeplug_psr,
    &ett_homeplug_slp,
    &ett_homeplug_ns,
    &ett_homeplug_tx_bfr_state,
    &ett_homeplug_tone,
    &ett_homeplug_bcn,
    &ett_homeplug_bridge,
    &ett_homeplug_bcl,
    &ett_homeplug_stc
  };

  proto_homeplug = proto_register_protocol("HomePlug protocol", "HomePlug", "homeplug");

  proto_register_field_array(proto_homeplug, hf, array_length(hf));

  proto_register_subtree_array(ett, array_length(ett));
}

/* Dissection of MCTRL */
static void dissect_homeplug_mctrl(ptvcursor_t * cursor)
{
  proto_item * it = NULL;

  if (!ptvcursor_tree(cursor)) 
    return;

  it = ptvcursor_add_no_advance(cursor, hf_homeplug_mctrl, 1, FALSE);
  /* Extract Number Of MAC Data Entries */
  homeplug_ne = tvb_get_guint8(ptvcursor_tvbuff(cursor), 
      ptvcursor_current_offset(cursor)) & HOMEPLUG_MCTRL_NE;

  ptvcursor_push_subtree(cursor, it, ett_homeplug_mctrl);
    ptvcursor_add_no_advance(cursor, hf_homeplug_mctrl_reserved, 1, FALSE);
    ptvcursor_add(cursor, hf_homeplug_mctrl_ne, 1, FALSE);

  ptvcursor_pop_subtree(cursor);
}

/* Dissection of MEHDR */
static void dissect_homeplug_mehdr(ptvcursor_t * cursor)
{
  proto_item * it = NULL;

  if (!ptvcursor_tree(cursor)) 
    return;

  it = ptvcursor_add_no_advance(cursor, hf_homeplug_mehdr, 1, FALSE);
  homeplug_metype = tvb_get_guint8(ptvcursor_tvbuff(cursor), 
      ptvcursor_current_offset(cursor)) & HOMEPLUG_MEHDR_METYPE;

  ptvcursor_push_subtree(cursor, it, ett_homeplug_mehdr);
    ptvcursor_add_no_advance(cursor, hf_homeplug_mehdr_mev, 1, FALSE);
    ptvcursor_add(cursor, hf_homeplug_mehdr_metype, 1, FALSE);

  ptvcursor_pop_subtree(cursor);
}

/* dissection of MELEN */
static void dissect_homeplug_melen(ptvcursor_t * cursor)
{
  if (!ptvcursor_tree(cursor)) 
    return;

  homeplug_melen = tvb_get_guint8(ptvcursor_tvbuff(cursor), ptvcursor_current_offset(cursor)); 
  ptvcursor_add(cursor, hf_homeplug_melen, 1, FALSE);

}

/* Dissection of Request Channel Estimation MME */
static void dissect_homeplug_rce(ptvcursor_t * cursor)
{
  proto_item * it = NULL;

  if (!ptvcursor_tree(cursor)) 
    return;

  it = ptvcursor_add_no_advance(cursor, hf_homeplug_rce, homeplug_melen, FALSE);

  ptvcursor_push_subtree(cursor, it, ett_homeplug_rce);
    ptvcursor_add_no_advance(cursor, hf_homeplug_rce_cev, 1, FALSE);
    ptvcursor_add(cursor, hf_homeplug_rce_rsvd, 1, FALSE);

  ptvcursor_pop_subtree(cursor);
}

/* Dissection of Channel Estimation Response MME */
static void dissect_homeplug_cer(ptvcursor_t * cursor)
{
  proto_item * it = NULL;
  guint8 iTone = 0;
  guint8 BP = 0;
  guint8 iNBDA = 0;

  if (!ptvcursor_tree(cursor)) 
    return;

  it = ptvcursor_add_no_advance(cursor, hf_homeplug_cer_cerv, homeplug_melen, FALSE);

  ptvcursor_push_subtree(cursor, it, ett_homeplug_cer);
    ptvcursor_add_no_advance(cursor, hf_homeplug_cer_cerv, 1, FALSE);
    ptvcursor_add(cursor, hf_homeplug_cer_rsvd1, 2, FALSE);
    ptvcursor_add(cursor, hf_homeplug_cer_rxtmi, 1, FALSE);

    for (;iTone < 10; iTone++) {
      ptvcursor_add(cursor, hf_homeplug_cer_vt, 1, FALSE);
    }

    ptvcursor_add_no_advance(cursor, hf_homeplug_cer_rate, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_cer_bp, 1, FALSE);
    BP = tvb_get_guint8(ptvcursor_tvbuff(cursor), 
        ptvcursor_current_offset(cursor)) & HOMEPLUG_CER_BP;
    ptvcursor_add_no_advance(cursor, hf_homeplug_cer_mod, 1, FALSE);
    ptvcursor_add(cursor, hf_homeplug_cer_vt11, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_cer_rsvd2, 1, FALSE);

    if (BP) {
      iNBDA = tvb_get_guint8(ptvcursor_tvbuff(cursor), 
          ptvcursor_current_offset(cursor)) & HOMEPLUG_CER_NBDAS;
      ptvcursor_add(cursor, hf_homeplug_cer_nbdas, 1, FALSE);
      /* TODO : Check on iNBDA! INT51X1 up to 16 dba. But up to 32 for INT51X1 (Host/DTE) */
      for (;iNBDA > 0; iNBDA--) {
        ptvcursor_add(cursor, hf_homeplug_cer_bda, 6, FALSE);
      }
    }
  ptvcursor_pop_subtree(cursor);
}

/* Dissection of the Vendor Specific MME */
static void dissect_homeplug_vs(ptvcursor_t * cursor)
{
  proto_item * it = NULL;

  if (!ptvcursor_tree(cursor)) 
    return;

  it = ptvcursor_add_no_advance(cursor, hf_homeplug_vs, homeplug_melen, FALSE);

  ptvcursor_push_subtree(cursor, it, ett_homeplug_vs);
    ptvcursor_add(cursor, hf_homeplug_vs_oui, 3, FALSE);
    if (homeplug_melen > 3)
      ptvcursor_add(cursor, hf_homeplug_vs_vd, homeplug_melen - 3, FALSE);
  ptvcursor_pop_subtree(cursor);
}

/* Dissection of Request Parameters and Statistics MME */
static void dissect_homeplug_rps(ptvcursor_t * cursor) 
{
  if (!ptvcursor_tree(cursor)) 
    return;

  ptvcursor_add(cursor, hf_homeplug_rps, homeplug_melen, FALSE);
}

/* Dissection of Set Network Encryption Key MME */
static void dissect_homeplug_snk(ptvcursor_t * cursor)
{
  proto_item * it = NULL;

  if (!ptvcursor_tree(cursor)) 
    return;

  it = ptvcursor_add_no_advance(cursor, hf_homeplug_snk, homeplug_melen, FALSE);

  ptvcursor_push_subtree(cursor, it, ett_homeplug_snk);
    ptvcursor_add(cursor, hf_homeplug_snk_eks, 1, FALSE);
    ptvcursor_add(cursor, hf_homeplug_snk_nek, 8, FALSE);

  ptvcursor_pop_subtree(cursor);
}

/* Dissection of Parameters and Statistics Response MME */
static void dissect_homeplug_psr(ptvcursor_t * cursor)
{
  proto_item * it = NULL;

  if (!ptvcursor_tree(cursor)) 
    return;

  it = ptvcursor_add_no_advance(cursor, hf_homeplug_psr, homeplug_melen, FALSE);

  ptvcursor_push_subtree(cursor, it, ett_homeplug_psr);
    ptvcursor_add(cursor, hf_homeplug_psr_txack, 2, FALSE);
    ptvcursor_add(cursor, hf_homeplug_psr_txnack, 2, FALSE);
    ptvcursor_add(cursor, hf_homeplug_psr_txfail, 2, FALSE);
    ptvcursor_add(cursor, hf_homeplug_psr_txcloss, 2, FALSE);
    ptvcursor_add(cursor, hf_homeplug_psr_txcoll, 2, FALSE);
    ptvcursor_add(cursor, hf_homeplug_psr_txca3lat, 2, FALSE);
    ptvcursor_add(cursor, hf_homeplug_psr_txca2lat, 2, FALSE);
    ptvcursor_add(cursor, hf_homeplug_psr_txca1lat, 2, FALSE);
    ptvcursor_add(cursor, hf_homeplug_psr_txca0lat, 2, FALSE);
    ptvcursor_add(cursor, hf_homeplug_psr_rxbp40, 4, FALSE);

  ptvcursor_pop_subtree(cursor);
}

/* Dissection of the Set Local Parameters MME */
static void dissect_homeplug_slp(ptvcursor_t * cursor)
{
  proto_item * it = NULL;

  if (!ptvcursor_tree(cursor)) 
    return;

  it = ptvcursor_add_no_advance(cursor, hf_homeplug_slp, homeplug_melen, FALSE);

  ptvcursor_push_subtree(cursor, it, ett_homeplug_slp);
    ptvcursor_add(cursor, hf_homeplug_slp_ma, 6, FALSE);
  ptvcursor_pop_subtree(cursor);
}

/* Dissection of the Network Statistics MME */
static void dissect_homeplug_ns(ptvcursor_t * cursor, packet_info * pinfo)
{
  guint8 iTone = 0;
  guint8 i_buffer = 0;

  guint16 ns_bytes40 = 0;
  guint64 newt_da = 0;
  gboolean extended = (homeplug_melen >= HOMEPLUG_NS_EXT_LEN);
  proto_item * ti;

  /* Append Basic/Extender specifier to info column */
  if (check_col(pinfo->cinfo, COL_INFO))
    col_append_str(pinfo->cinfo, COL_INFO, extended ? " Extended" : " Basic");

  if (!ptvcursor_tree(cursor)) 
    return;

  ptvcursor_add_with_subtree(cursor, hf_homeplug_ns, homeplug_melen, FALSE,
      ett_homeplug_ns);

    ti = proto_tree_add_boolean(ptvcursor_tree(cursor), hf_homeplug_ns_extended,
             ptvcursor_tvbuff(cursor), 0, 0, extended);
    PROTO_ITEM_SET_GENERATED(ti);

    ptvcursor_add_no_advance(cursor, hf_homeplug_ns_netw_ctrl_ac, 1, FALSE);
    ptvcursor_add(cursor, hf_homeplug_ns_netw_ctrl_icid, 1, FALSE);

    ptvcursor_add_no_advance(cursor, hf_homeplug_ns_bytes40_robo, 2, TRUE);
    ns_bytes40 = tvb_get_letohs(ptvcursor_tvbuff(cursor),
        ptvcursor_current_offset(cursor));
    proto_tree_add_text(ptvcursor_tree(cursor), ptvcursor_tvbuff(cursor), 
        ptvcursor_current_offset(cursor), 2, "MHz :  %.3f", (float)(ns_bytes40)/42);
    ptvcursor_advance(cursor, 2);

    ptvcursor_add(cursor, hf_homeplug_ns_fails_robo, 2, TRUE);
    ptvcursor_add(cursor, hf_homeplug_ns_drops_robo, 2, TRUE);

    while (iTone < 15) {
      newt_da = ((gint64)tvb_get_ntoh24(ptvcursor_tvbuff(cursor),
      ptvcursor_current_offset(cursor))) << 24;
      newt_da |= tvb_get_ntoh24(ptvcursor_tvbuff(cursor),
      ptvcursor_current_offset(cursor)+3);

      if (newt_da != HOMEPLUG_ADDR_INEXISTANT) {
        ptvcursor_add_text_with_subtree(cursor, SUBTREE_UNDEFINED_LENGTH,
            ett_homeplug_tone, "Tone Map #%d", iTone+1);

        ptvcursor_add(cursor, hf_homeplug_ns_netw_da, 6, FALSE);

        ptvcursor_add_no_advance(cursor, hf_homeplug_ns_bytes40, 2, TRUE);
        ns_bytes40 = tvb_get_letohs(ptvcursor_tvbuff(cursor),
            ptvcursor_current_offset(cursor));
        proto_tree_add_text(ptvcursor_tree(cursor), ptvcursor_tvbuff(cursor),
            ptvcursor_current_offset(cursor), 2, "MHz :  %.3f", (float)(ns_bytes40)/42);
        ptvcursor_advance(cursor, 2);

        ptvcursor_add(cursor, hf_homeplug_ns_fails, 2, TRUE);
        ptvcursor_add(cursor, hf_homeplug_ns_drops, 2, TRUE);

        ptvcursor_pop_subtree(cursor);
      } else {
        proto_tree_add_text(ptvcursor_tree(cursor), ptvcursor_tvbuff(cursor),
            ptvcursor_current_offset(cursor), 12, "Tone Map #%d does not exist", iTone+1);
      }

      iTone++;
    }
    if (extended) {
      while (i_buffer < 6) {
        ptvcursor_add_text_with_subtree(cursor, SUBTREE_UNDEFINED_LENGTH,
            ett_homeplug_tx_bfr_state, "TX_BFR_%d_STATE", i_buffer);
          ptvcursor_add_no_advance(cursor, hf_homeplug_ns_buf_in_use, 1, FALSE);
          ptvcursor_add_no_advance(cursor, hf_homeplug_ns_prio, 1, FALSE);
          ptvcursor_add(cursor, hf_homeplug_ns_msdu_len, 1, FALSE);
          ptvcursor_add_no_advance(cursor, hf_homeplug_ns_seqn, 1, FALSE);
          ptvcursor_add(cursor, hf_homeplug_ns_toneidx, 1, FALSE);
        ptvcursor_pop_subtree(cursor);
        i_buffer++;
      }
    }
  ptvcursor_pop_subtree(cursor);
}

/* Dissection of the Bridging Characteristics Network MME */
static void dissect_homeplug_bcn(ptvcursor_t * cursor)
{
  proto_item * it = NULL;
  guint8 i_bridge = 0;
  guint8 num_das;
  guint8 i_da;
  guint8 fbn;

  it = ptvcursor_add_no_advance(cursor, hf_homeplug_bcn, homeplug_melen, FALSE);

  ptvcursor_push_subtree(cursor, it, ett_homeplug_bcn);
    ptvcursor_add_no_advance(cursor, hf_homeplug_bcn_network, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_bcn_return, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_bcn_rsvd, 1, FALSE);
    fbn = tvb_get_guint8(ptvcursor_tvbuff(cursor), ptvcursor_current_offset(cursor))
        & HOMEPLUG_BCN_FBN;
    ptvcursor_add(cursor, hf_homeplug_bcn_fbn, 1, FALSE);
    while (i_bridge < 2) {
      ptvcursor_add_text_with_subtree(cursor, SUBTREE_UNDEFINED_LENGTH,
          ett_homeplug_bridge, "Bridge #%d", fbn + i_bridge);
        ptvcursor_add(cursor, hf_homeplug_bcn_brda, 6, FALSE);
        num_das = tvb_get_guint8(ptvcursor_tvbuff(cursor), ptvcursor_current_offset(cursor));
        ptvcursor_add(cursor, hf_homeplug_bcn_bp_das, 1, FALSE);
        for (i_da = 0; i_da < num_das; i_da++) {
          ptvcursor_add(cursor, hf_homeplug_bcn_bp_da, 6, FALSE);
        }
      ptvcursor_pop_subtree(cursor);
      i_bridge++;
    }
  ptvcursor_pop_subtree(cursor);
}

/* Dissection of the Bridging Characteristics Local MME */
static void dissect_homeplug_bcl(ptvcursor_t * cursor)
{
  proto_item * it = NULL;
  guint8 num_das;
  guint8 i_da = 0;

  it = ptvcursor_add_no_advance(cursor, hf_homeplug_bcl, homeplug_melen, FALSE);

  ptvcursor_push_subtree(cursor, it, ett_homeplug_bcl);
    ptvcursor_add_no_advance(cursor, hf_homeplug_bcl_network, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_bcl_return, 1, FALSE);
    ptvcursor_add(cursor, hf_homeplug_bcl_rsvd, 1, FALSE);

    num_das = tvb_get_guint8(ptvcursor_tvbuff(cursor), ptvcursor_current_offset(cursor));
    ptvcursor_add(cursor, hf_homeplug_bcl_hprox_das, 1, FALSE);

    while (i_da < num_das) {
      it = ptvcursor_add(cursor, hf_homeplug_bcl_hpbda, 6, FALSE);
      i_da++;
    }
  ptvcursor_pop_subtree(cursor);
}

/* Dissection of the Bridging Characteristics MME */
static void dissect_homeplug_bc(ptvcursor_t * cursor, packet_info * pinfo)
{
  gboolean network;

  if (!ptvcursor_tree(cursor)) 
    return;

  network = tvb_get_guint8(ptvcursor_tvbuff(cursor), ptvcursor_current_offset(cursor))
          & HOMEPLUG_BC_NETWORK;

  /* Append Network/Local specifier to info column */
  if (check_col(pinfo->cinfo, COL_INFO))
    col_append_str(pinfo->cinfo, COL_INFO, network ? " Network" : " Local");

  /* Call specific dissector */
  if (network)
    dissect_homeplug_bcn(cursor);
  else
    dissect_homeplug_bcl(cursor);
}

/* Dissection of the Set Transmit Characteristics MME */
static void dissect_homeplug_stc(ptvcursor_t * cursor)
{
  proto_item * it = NULL;

  if (!ptvcursor_tree(cursor)) 
    return;

  it = ptvcursor_add_no_advance(cursor, hf_homeplug_stc, homeplug_melen, FALSE);

  ptvcursor_push_subtree(cursor, it, ett_homeplug_stc);
    ptvcursor_add_no_advance(cursor, hf_homeplug_stc_lco, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_stc_encf, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_stc_txprio, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_stc_rexp, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_stc_txcf, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_stc_cftop, 1, FALSE);
    ptvcursor_add(cursor, hf_homeplug_stc_rsvd1, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_stc_retry, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_stc_rsvd2, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_stc_dder, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_stc_dur, 1, FALSE);
    ptvcursor_add_no_advance(cursor, hf_homeplug_stc_ebp, 1, FALSE);
    ptvcursor_add(cursor, hf_homeplug_stc_dees, 1, FALSE);
    ptvcursor_add(cursor, hf_homeplug_stc_txeks, 1, FALSE);

  ptvcursor_pop_subtree(cursor);

  return;
}

/* Dissection of unknown tags */
static void dissect_homeplug_unknown(ptvcursor_t * cursor)
{
  ptvcursor_add(cursor, hf_homeplug_data, homeplug_melen, FALSE);
}

static void dissect_homeplug_mme(ptvcursor_t * cursor, packet_info * pinfo)
{
  if (check_col(pinfo->cinfo, COL_INFO)) {
    col_append_sep_str(pinfo->cinfo, COL_INFO, ", ",
        val_to_str(homeplug_metype, homeplug_metype_vals, "Unknown 0x%x"));
  }

  switch(homeplug_metype) {
    case HOMEPLUG_MME_RCE:
      dissect_homeplug_rce(cursor);
      break;
    case HOMEPLUG_MME_CER:
      dissect_homeplug_cer(cursor);
      break;
    case HOMEPLUG_MME_VS:
      dissect_homeplug_vs(cursor);
      break;
    case HOMEPLUG_MME_SNK:
      dissect_homeplug_snk(cursor);
      break;
    case HOMEPLUG_MME_RPS:
      dissect_homeplug_rps(cursor);
      break;
    case HOMEPLUG_MME_PSR:
      dissect_homeplug_psr(cursor);
      break;
    case HOMEPLUG_MME_SLP:
      dissect_homeplug_slp(cursor);
      break;
    case HOMEPLUG_MME_NS:
      dissect_homeplug_ns(cursor, pinfo);
      break;
    case HOMEPLUG_MME_BC:
      dissect_homeplug_bc(cursor, pinfo);
      break;
    case HOMEPLUG_MME_STC:
      dissect_homeplug_stc(cursor);
      break;
    default:
      dissect_homeplug_unknown(cursor);
      break;
  }
}

#define TVB_LEN_GREATEST  1
#define TVB_LEN_UNDEF     0
#define TVB_LEN_SHORTEST -1
static int check_tvb_length(ptvcursor_t *cursor, const gint length)
{
  if (!cursor)
    return TVB_LEN_UNDEF;

  if (tvb_reported_length_remaining(ptvcursor_tvbuff(cursor),
                                    ptvcursor_current_offset(cursor)) < length)
    return TVB_LEN_SHORTEST;

  return TVB_LEN_GREATEST;
}

static void
dissect_homeplug(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
{
  proto_item * it= NULL;
  proto_tree * homeplug_tree= NULL;
  ptvcursor_t * cursor= NULL;

  if (check_col(pinfo->cinfo, COL_PROTOCOL))
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "HomePlug");
  /* Clear out stuff in the info column */
  if (check_col(pinfo->cinfo, COL_INFO)) {
    col_clear(pinfo->cinfo, COL_INFO);
    col_set_str(pinfo->cinfo, COL_INFO, "MAC Management");
  }

  homeplug_offset = 0;

  if (tree) {
    it = proto_tree_add_item(tree, proto_homeplug, tvb, homeplug_offset, -1, FALSE);
    homeplug_tree = proto_item_add_subtree(it, ett_homeplug);
    cursor = ptvcursor_new(homeplug_tree, tvb, 0);
  }

  /*  We do not have enough data to read mctrl field stop the dissection */
  if (check_tvb_length(cursor, HOMEPLUG_MCTRL_LEN) != TVB_LEN_SHORTEST) {

    dissect_homeplug_mctrl(cursor);

    /** homeplug_ne indicates the number of MME entries. This field is fetched
     *  from MCTRL. 
     */
    if ((homeplug_ne > 0) && check_col(pinfo->cinfo, COL_INFO)) {
      col_clear(pinfo->cinfo, COL_INFO);
    }
    for (; homeplug_ne > 0; homeplug_ne--) {

      /* Check we have enough data in tvb to read MEHDR */
      if (check_tvb_length(cursor, HOMEPLUG_MEHDR_LEN) == TVB_LEN_SHORTEST)
        break;
      dissect_homeplug_mehdr(cursor);

      /* Check we have enough data in tvb to read MELEN */
      if (check_tvb_length(cursor, HOMEPLUG_MELEN_LEN) == TVB_LEN_SHORTEST)
        break;
      dissect_homeplug_melen(cursor);

      dissect_homeplug_mme(cursor, pinfo);
    }
  }

  if (cursor) 
    ptvcursor_free(cursor);
}


void
proto_reg_handoff_homeplug(void)
{
  dissector_handle_t homeplug_handle;

  homeplug_handle = create_dissector_handle(dissect_homeplug, proto_homeplug);
  dissector_add("ethertype", ETHERTYPE_HOMEPLUG, homeplug_handle);
}