aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-cdp.c
blob: 3fb19913d6ca042999a118336b7a3f18d19d7c3b (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
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
/* packet-cdp.c
 * Routines for the disassembly of the "Cisco Discovery Protocol"
 * (c) Copyright Hannes R. Boehm <hannes@boehm.org>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "config.h"

#include <string.h>

#include <glib.h>
#include <epan/packet.h>
#include <epan/to_str.h>
#include <epan/in_cksum.h>
#include <epan/wmem/wmem.h>

#include <epan/oui.h>
#include <epan/nlpid.h>


/*
 * See
 *
 *      http://www.cisco.com/univercd/cc/td/doc/product/lan/trsrb/frames.htm#xtocid12
 *
 * for some information on CDP.
 *
 * See
 *
 *      http://www.cisco.com/en/US/products/hw/switches/ps663/products_tech_note09186a0080094713.shtml#cdp
 *
 * for some more information on CDP version 2.
 */

void proto_register_cdp(void);
void proto_reg_handoff_cdp(void);

/* Offsets in TLV structure. */
#define TLV_TYPE        0
#define TLV_LENGTH      2

static int proto_cdp = -1;
static int hf_cdp_version = -1;
static int hf_cdp_checksum = -1;
static int hf_cdp_checksum_good = -1;
static int hf_cdp_checksum_bad = -1;
static int hf_cdp_ttl = -1;
static int hf_cdp_tlvtype = -1;
static int hf_cdp_tlvlength = -1;
static int hf_cdp_deviceid = -1;
static int hf_cdp_platform = -1;
static int hf_cdp_portid = -1;
static int hf_cdp_capabilities = -1;
static int hf_cdp_capabilities_router = -1;
static int hf_cdp_capabilities_trans_bridge = -1;
static int hf_cdp_capabilities_src_bridge = -1;
static int hf_cdp_capabilities_switch = -1;
static int hf_cdp_capabilities_host = -1;
static int hf_cdp_capabilities_igmp_capable = -1;
static int hf_cdp_capabilities_repeater = -1;
static int hf_cdp_spare_poe_tlv = -1;
static int hf_cdp_spare_poe_tlv_poe = -1;
static int hf_cdp_spare_poe_tlv_spare_pair_arch = -1;
static int hf_cdp_spare_poe_tlv_req_spare_pair_poe = -1;
static int hf_cdp_spare_poe_tlv_pse_spare_pair_poe = -1;

static gint ett_cdp = -1;
static gint ett_cdp_tlv = -1;
static gint ett_cdp_nrgyz_tlv = -1;
static gint ett_cdp_address = -1;
static gint ett_cdp_capabilities = -1;
static gint ett_cdp_spare_poe_tlv = -1;
static gint ett_cdp_checksum = -1;

static dissector_handle_t data_handle;

static int
dissect_address_tlv(tvbuff_t *tvb, int offset, int length, proto_tree *tree);
static void
dissect_capabilities(tvbuff_t *tvb, int offset, int length, proto_tree *tree);
static void
dissect_nrgyz_tlv(tvbuff_t *tvb, int offset, guint16 length, guint16 num,
  proto_tree *tree);
static void
dissect_spare_poe_tlv(tvbuff_t *tvb, int offset, int length, proto_tree *tree);
static void
add_multi_line_string_to_tree(proto_tree *tree, tvbuff_t *tvb, gint start,
  gint len, const gchar *prefix);

#define TYPE_DEVICE_ID          0x0001
#define TYPE_ADDRESS            0x0002
#define TYPE_PORT_ID            0x0003
#define TYPE_CAPABILITIES       0x0004
#define TYPE_IOS_VERSION        0x0005
#define TYPE_PLATFORM           0x0006
#define TYPE_IP_PREFIX          0x0007
#define TYPE_PROTOCOL_HELLO     0x0008 /* Protocol Hello */
#define TYPE_VTP_MGMT_DOMAIN    0x0009 /* VTP Domain, CTPv2 - see second URL */
#define TYPE_NATIVE_VLAN        0x000a /* Native VLAN, CTPv2 - see second URL */
#define TYPE_DUPLEX             0x000b /* Full/Half Duplex - see second URL */
/*                              0x000c */
/*                              0x000d */
#define TYPE_VOIP_VLAN_REPLY    0x000e /* VoIP VLAN reply */
#define TYPE_VOIP_VLAN_QUERY    0x000f /* VoIP VLAN query */
#define TYPE_POWER              0x0010 /* Power consumption */
#define TYPE_MTU                0x0011 /* MTU */
#define TYPE_TRUST_BITMAP       0x0012 /* Trust bitmap */
#define TYPE_UNTRUSTED_COS      0x0013 /* Untrusted port CoS */
#define TYPE_SYSTEM_NAME        0x0014 /* System Name */
#define TYPE_SYSTEM_OID         0x0015 /* System OID */
#define TYPE_MANAGEMENT_ADDR    0x0016 /* Management Address(es) */
#define TYPE_LOCATION           0x0017 /* Location */
#define TYPE_EXT_PORT_ID        0x0018 /* External Port-ID */
#define TYPE_POWER_REQUESTED    0x0019 /* Power Requested */
#define TYPE_POWER_AVAILABLE    0x001a /* Power Available */
#define TYPE_PORT_UNIDIR        0x001b /* Port Unidirectional */
#define TYPE_NRGYZ              0x001d /* EnergyWise over CDP */
#define TYPE_SPARE_POE          0x001f /* Spare Pair PoE */

#define TYPE_HP_BSSID           0x1000 /* BSSID */
#define TYPE_HP_SERIAL          0x1001 /* Serial number */
#define TYPE_HP_SSID            0x1002 /* SSID */
#define TYPE_HP_RADIO1_CH       0x1003 /* Radio1 channel */
/*                              0x1004 */
/*                              0x1005 */
#define TYPE_HP_SNMP_PORT       0x1006 /* SNMP listening UDP port */
#define TYPE_HP_MGMT_PORT       0x1007 /* Web interface TCP port */
#define TYPE_HP_SOURCE_MAC      0x1008 /* Sender MAC address for the AP, bouth wired and wireless */
#define TYPE_HP_RADIO2_CH       0x1009 /* Radio2 channel */
#define TYPE_HP_RADIO1_OMODE    0x100A /* Radio1 Operating mode */
#define TYPE_HP_RADIO2_OMODE    0x100B /* Radio2 Operating mode */
#define TYPE_HP_RADIO1_RMODE    0x100C /* Radio1 Radio mode */
#define TYPE_HP_RADIO2_RMODE    0x100D /* Radio2 Radio mode */

static const value_string type_vals[] = {
    { TYPE_DEVICE_ID,       "Device ID" },
    { TYPE_ADDRESS,         "Addresses" },
    { TYPE_PORT_ID,         "Port ID" },
    { TYPE_CAPABILITIES,    "Capabilities" },
    { TYPE_IOS_VERSION,     "Software version" },
    { TYPE_PLATFORM,        "Platform" },
    { TYPE_IP_PREFIX,       "IP Prefix/Gateway (used for ODR)" },
    { TYPE_PROTOCOL_HELLO,  "Protocol Hello" },
    { TYPE_VTP_MGMT_DOMAIN, "VTP Management Domain" },
    { TYPE_NATIVE_VLAN,     "Native VLAN" },
    { TYPE_DUPLEX,          "Duplex" },
    { TYPE_VOIP_VLAN_REPLY, "VoIP VLAN Reply" },
    { TYPE_VOIP_VLAN_QUERY, "VoIP VLAN Query" },
    { TYPE_POWER,           "Power consumption" },
    { TYPE_MTU,             "MTU"},
    { TYPE_TRUST_BITMAP,    "Trust Bitmap" },
    { TYPE_UNTRUSTED_COS,   "Untrusted Port CoS" },
    { TYPE_SYSTEM_NAME,     "System Name" },
    { TYPE_SYSTEM_OID,      "System Object ID" },
    { TYPE_MANAGEMENT_ADDR, "Management Address" },
    { TYPE_LOCATION,        "Location" },
    { TYPE_EXT_PORT_ID,     "External Port-ID" },
    { TYPE_POWER_REQUESTED, "Power Requested" },
    { TYPE_POWER_AVAILABLE, "Power Available" },
    { TYPE_PORT_UNIDIR,     "Port Unidirectional" },
    { TYPE_NRGYZ,           "EnergyWise" },
    { TYPE_SPARE_POE,       "Spare PoE" },
    { TYPE_HP_BSSID,        "BSSID" },
    { TYPE_HP_SERIAL,       "Serial number" },
    { TYPE_HP_SSID,         "SSID" },
    { TYPE_HP_RADIO1_CH,    "Radio1 channel" },
    { TYPE_HP_SNMP_PORT,    "SNMP UDP port" },
    { TYPE_HP_MGMT_PORT,    "Web TCP port" },
    { TYPE_HP_SOURCE_MAC,   "Source MAC address" },
    { TYPE_HP_RADIO2_CH,    "Radio2 channel" },
    { TYPE_HP_RADIO1_OMODE, "Radio1 Operating mode" },
    { TYPE_HP_RADIO2_OMODE, "Radio2 Operating mode" },
    { TYPE_HP_RADIO1_RMODE, "Radio1 Radio mode" },
    { TYPE_HP_RADIO2_RMODE, "Radio2 Radio mode" },
    { 0, NULL }
};

#define TYPE_HELLO_CLUSTER_MGMT    0x0112

static const value_string type_hello_vals[] = {
    { TYPE_HELLO_CLUSTER_MGMT,   "Cluster Management" },
    { 0, NULL }
};

#define TYPE_NRGYZ_ROLE    0x00000007
#define TYPE_NRGYZ_DOMAIN  0x00000008
#define TYPE_NRGYZ_NAME    0x00000009
#define TYPE_NRGYZ_REPLYTO 0x00000017

static const value_string type_nrgyz_vals[] = {
    { TYPE_NRGYZ_ROLE,    "Role" },
    { TYPE_NRGYZ_DOMAIN,  "Domain" },
    { TYPE_NRGYZ_NAME,    "Name" },
    { TYPE_NRGYZ_REPLYTO, "Reply To" },
    { 0, NULL }
};

static void
dissect_cdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    proto_item *ti, *checksum_item;
    proto_tree *cdp_tree = NULL, *checksum_tree;
    int         offset   = 0;
    guint16     type;
    guint16     length, packet_checksum, computed_checksum, data_length;
    gboolean    checksum_good, checksum_bad;
    proto_item *tlvi     = NULL;
    proto_tree *tlv_tree = NULL;
    int         real_length;
    guint32     naddresses;
    guint32     power_avail_len, power_avail;
    guint32     power_req_len, power_req;
    int         addr_length;
    guint32     ip_addr;
    vec_t       cksum_vec[1];

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "CDP");
    col_clear(pinfo->cinfo, COL_INFO);

    if (tree) {
        ti = proto_tree_add_item(tree, proto_cdp, tvb, offset, -1, ENC_NA);
        cdp_tree = proto_item_add_subtree(ti, ett_cdp);

        /* CDP header */
        proto_tree_add_item(cdp_tree, hf_cdp_version, tvb, offset, 1, ENC_BIG_ENDIAN);
        offset += 1;

        proto_tree_add_uint_format_value(cdp_tree, hf_cdp_ttl, tvb, offset, 1,
                                         tvb_get_guint8(tvb, offset),
                                         "%u seconds",
                                         tvb_get_guint8(tvb, offset));
        offset += 1;
    } else {
        offset += 2; /* The version/ttl fields from above */
    }

    /* Checksum display & verification code */
    packet_checksum = tvb_get_ntohs(tvb, offset);

    data_length = tvb_reported_length(tvb);

    /* CDP doesn't adhere to RFC 1071 section 2. (B). It incorrectly assumes
     * checksums are calculated on a big endian platform, therefore i.s.o.
     * padding odd sized data with a zero byte _at the end_ it sets the last
     * big endian _word_ to contain the last network _octet_. This byteswap
     * has to be done on the last octet of network data before feeding it to
     * the Internet checksum routine.
     * CDP checksumming code has a bug in the addition of this last _word_
     * as a signed number into the long word intermediate checksum. When
     * reducing this long to word size checksum an off-by-one error can be
     * made. This off-by-one error is compensated for in the last _word_ of
     * the network data.
     */
    if (data_length & 1) {
        guint8 *padded_buffer;
        /* Allocate new buffer */
        padded_buffer = (guint8 *)wmem_alloc(wmem_packet_scope(), data_length+1);
        tvb_memcpy(tvb, padded_buffer, 0, data_length);
        /* Swap bytes in last word */
        padded_buffer[data_length] = padded_buffer[data_length-1];
        padded_buffer[data_length-1] = 0;
        /* Compensate off-by-one error */
        if (padded_buffer[data_length] & 0x80) {
          padded_buffer[data_length]--;
          padded_buffer[data_length-1]--;
        }
        /* Setup checksum routine data buffer */
        cksum_vec[0].ptr = padded_buffer;
        cksum_vec[0].len = data_length+1;
    } else {
        /* Setup checksum routine data buffer */
        cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, data_length);
        cksum_vec[0].len = data_length;
    }

    computed_checksum = in_cksum(cksum_vec, 1);
    checksum_good = (computed_checksum == 0);
    checksum_bad = !checksum_good;
    if (checksum_good) {
        checksum_item = proto_tree_add_uint_format_value(cdp_tree,
                                                   hf_cdp_checksum, tvb, offset, 2, packet_checksum,
                                                   "0x%04x [correct]", packet_checksum);
    } else {
        checksum_item = proto_tree_add_uint_format_value(cdp_tree,
                                                   hf_cdp_checksum, tvb, offset, 2, packet_checksum,
                                                   "0x%04x [incorrect, should be 0x%04x]",
                                                   packet_checksum,
                                                   in_cksum_shouldbe(packet_checksum, computed_checksum));
    }

    checksum_tree = proto_item_add_subtree(checksum_item, ett_cdp_checksum);
    checksum_item = proto_tree_add_boolean(checksum_tree, hf_cdp_checksum_good,
                                           tvb, offset, 2, checksum_good);
    PROTO_ITEM_SET_GENERATED(checksum_item);
    checksum_item = proto_tree_add_boolean(checksum_tree, hf_cdp_checksum_bad,
                                           tvb, offset, 2, checksum_bad);
    PROTO_ITEM_SET_GENERATED(checksum_item);

    offset += 2;

    while (tvb_reported_length_remaining(tvb, offset) != 0) {
        type = tvb_get_ntohs(tvb, offset + TLV_TYPE);
        length = tvb_get_ntohs(tvb, offset + TLV_LENGTH);
        if (length < 4) {
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb, offset, 4,
                                           ett_cdp_tlv, NULL, "TLV with invalid length %u (< 4)",
                                           length);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
            }
            offset += 4;
            break;
        }

        switch (type) {

        case TYPE_DEVICE_ID:
            /* Device ID */

            col_append_fstr(pinfo->cinfo, COL_INFO,
                            "Device ID: %s  ",
                            tvb_format_stringzpad(tvb, offset + 4, length - 4));

            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb, offset,
                                           length, ett_cdp_tlv, NULL, "Device ID: %s",
                                           tvb_format_stringzpad(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_deviceid, tvb, offset + 4, length - 4, ENC_ASCII|ENC_NA);
            }
            offset += length;
            break;

        case TYPE_PORT_ID:
            real_length = length;
            if (tvb_get_guint8(tvb, offset + real_length) != 0x00) {
                /* The length in the TLV doesn't appear to be the
                   length of the TLV, as the byte just past it
                   isn't the first byte of a 2-byte big-endian
                   small integer; make the length of the TLV the length
                   in the TLV, plus 4 bytes for the TLV type and length,
                   minus 1 because that's what makes one capture work. */
                real_length = length + 3;
            }

            col_append_fstr(pinfo->cinfo, COL_INFO,
                            "Port ID: %s  ",
                            tvb_format_stringzpad(tvb, offset + 4,
                                                  length - 4));

            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb, offset,
                                           real_length, ett_cdp_tlv, NULL, "Port ID: %s",
                                           tvb_format_text(tvb, offset + 4, real_length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_portid, tvb, offset + 4, real_length - 4, ENC_ASCII|ENC_NA);
            }
            offset += real_length;
            break;

        case TYPE_ADDRESS:
            /* Addresses */
            if (tree) {
                tlv_tree = proto_tree_add_subtree(cdp_tree, tvb, offset,
                                           length, ett_cdp_tlv, NULL, "Addresses");
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
            }
            offset += 4;
            length -= 4;
            naddresses = tvb_get_ntohl(tvb, offset);
            if (tree) {
                proto_tree_add_text(tlv_tree, tvb, offset, 4,
                                    "Number of addresses: %u", naddresses);
            }
            offset += 4;
            length -= 4;
            while (naddresses != 0) {
                addr_length = dissect_address_tlv(tvb, offset, length,
                                                  tlv_tree);
                if (addr_length < 0)
                    break;
                offset += addr_length;
                length -= addr_length;

                naddresses--;
            }
            offset += length;
            break;

        case TYPE_CAPABILITIES:
            if (tree) {
                tlv_tree = proto_tree_add_subtree(cdp_tree, tvb, offset,
                                           length, ett_cdp_tlv, NULL, "Capabilities");
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
            }
            offset += 4;
            length -= 4;
            dissect_capabilities(tvb, offset, length, tlv_tree);
            offset += length;
            break;

        case TYPE_IOS_VERSION:
            if (tree) {
                tlv_tree = proto_tree_add_subtree(cdp_tree, tvb, offset,
                                           length, ett_cdp_tlv, NULL, "Software Version");
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                add_multi_line_string_to_tree(tlv_tree, tvb, offset + 4,
                                              length - 4, "Software Version: ");
            }
            offset += length;
            break;

        case TYPE_PLATFORM:
            /* ??? platform */
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Platform: %s",
                                           tvb_format_text(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_platform, tvb, offset + 4, length - 4, ENC_ASCII|ENC_NA);
            }
            offset += length;
            break;

        case TYPE_IP_PREFIX:
            if (length == 8) {
                /* if length is 8 then this is default gw not prefix */
                if (tree) {
                    tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb, offset,
                                               length, ett_cdp_tlv, NULL, "ODR Default gateway: %s",
                                               tvb_ip_to_str(tvb, offset+4));
                    proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                    proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                    proto_tree_add_text(tlv_tree, tvb, offset+4, 4,
                                        "ODR Default gateway = %s",
                                        tvb_ip_to_str(tvb, offset+4));
                }
                offset += 8;
            } else {
                if (tree) {
                    tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb, offset,
                                               length, ett_cdp_tlv, NULL, "IP Prefixes: %d",length/5);

                    /* the actual number of prefixes is (length-4)/5
                       but if the variable is not a "float" but "integer"
                       then length/5=(length-4)/5  :)  */

                    proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                    proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                }
                offset += 4;
                length -= 4;
                while (length > 0) {
                    if (tree) {
                        proto_tree_add_text(tlv_tree, tvb, offset, 5,
                                            "IP Prefix = %s/%u",
                                            tvb_ip_to_str(tvb, offset),
                                            tvb_get_guint8(tvb,offset+4));
                    }
                    offset += 5;
                    length -= 5;
                }
            }
            break;

        case TYPE_PROTOCOL_HELLO:
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset,length, ett_cdp_tlv, NULL, "Protocol Hello: %s",
                                           val_to_str(tvb_get_ntohs(tvb, offset+7), type_hello_vals, "Unknown (0x%04x)"));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset+4, 3,
                                    "OUI: 0x%06X (%s)",
                                    tvb_get_ntoh24(tvb,offset+4),
                                    val_to_str_const(tvb_get_ntoh24(tvb,offset+4), oui_vals, "Unknown"));
                proto_tree_add_text(tlv_tree, tvb, offset+7, 2,
                                    "Protocol ID: 0x%04X (%s)",
                                    tvb_get_ntohs(tvb, offset+7),
                                    val_to_str_const(tvb_get_ntohs(tvb, offset+7), type_hello_vals, "Unknown"));

                switch(tvb_get_ntohs(tvb, offset+7)) {

                case TYPE_HELLO_CLUSTER_MGMT:
                    /*          proto_tree_add_text(tlv_tree, tvb, offset+9,
                                length - 9, "Cluster Management");
                    */
                    ip_addr = tvb_get_ipv4(tvb, offset+9);
                    proto_tree_add_text(tlv_tree, tvb, offset+9, 4,
                                        "Cluster Master IP: %s",ip_to_str((guint8 *)&ip_addr));
                    ip_addr = tvb_get_ipv4(tvb, offset+13);
                    proto_tree_add_text(tlv_tree, tvb, offset+13, 4,
                                        "UNKNOWN (IP?): 0x%08X (%s)",
                                        ip_addr, ip_to_str((guint8 *)&ip_addr));
                    proto_tree_add_text(tlv_tree, tvb, offset+17, 1,
                                        "Version?: 0x%02X",
                                        tvb_get_guint8(tvb, offset+17));
                    proto_tree_add_text(tlv_tree, tvb, offset+18, 1,
                                        "Sub Version?: 0x%02X",
                                        tvb_get_guint8(tvb, offset+18));
                    proto_tree_add_text(tlv_tree, tvb, offset+19, 1,
                                        "Status?: 0x%02X",
                                        tvb_get_guint8(tvb, offset+19));
                    proto_tree_add_text(tlv_tree, tvb, offset+20, 1,
                                        "UNKNOWN: 0x%02X",
                                        tvb_get_guint8(tvb, offset+20));
                    proto_tree_add_text(tlv_tree, tvb, offset+21, 6,
                                        "Cluster Commander MAC: %s",
                                        tvb_ether_to_str(tvb, offset+21));
                    proto_tree_add_text(tlv_tree, tvb, offset+27, 6,
                                        "Switch's MAC: %s",
                                        tvb_ether_to_str(tvb, offset+27));
                    proto_tree_add_text(tlv_tree, tvb, offset+33, 1,
                                        "UNKNOWN: 0x%02X",
                                        tvb_get_guint8(tvb, offset+33));
                    proto_tree_add_text(tlv_tree, tvb, offset+34, 2,
                                        "Management VLAN: %d",
                                        tvb_get_ntohs(tvb, offset+34));
                    break;
                default:
                    proto_tree_add_text(tlv_tree, tvb, offset + 9,
                                        length - 9, "Unknown");
                    break;
                }
            }
            offset += length;
            break;

        case TYPE_VTP_MGMT_DOMAIN:
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "VTP Management Domain: %s",
                                           tvb_format_text(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    length - 4, "VTP Management Domain: %s",
                                    tvb_format_text(tvb, offset + 4, length - 4));
            }
            offset += length;
            break;

        case TYPE_NATIVE_VLAN:
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Native VLAN: %u",
                                           tvb_get_ntohs(tvb, offset + 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    length - 4, "Native VLAN: %u",
                                    tvb_get_ntohs(tvb, offset + 4));
            }
            offset += length;
            break;

        case TYPE_DUPLEX:
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Duplex: %s",
                                           tvb_get_guint8(tvb, offset + 4) ?
                                           "Full" : "Half" );
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    length - 4, "Duplex: %s",
                                    tvb_get_guint8(tvb, offset + 4) ?
                                    "Full" : "Half" );
            }
            offset += length;
            break;

        case TYPE_VOIP_VLAN_REPLY:
            if (tree) {
                if (length >= 7) {
                    tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb, offset, length, ett_cdp_tlv, NULL,
                                               "VoIP VLAN Reply: %u", tvb_get_ntohs(tvb, offset + 5));
                } else {
                    /*
                     * XXX - what are these?  I've seen them in some captures;
                     * they have a length of 6, and run up to the end of
                     * the packet, so if we try to dissect it the same way
                     * we dissect the 7-byte ones, we report a malformed
                     * frame.
                     */
                    tlv_tree = proto_tree_add_subtree(cdp_tree, tvb,
                                               offset, length, ett_cdp_tlv, NULL, "VoIP VLAN Reply");
                }
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    1, "Data");
                if (length >= 7) {
                    proto_tree_add_text(tlv_tree, tvb, offset + 5,
                                        2, "Voice VLAN: %u",
                                        tvb_get_ntohs(tvb, offset + 5));
                }
            }
            offset += length;
            break;

        case TYPE_VOIP_VLAN_QUERY:
            if (tree) {
                if (length >= 7) {
                    tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb, offset, length,
                                               ett_cdp_tlv, NULL, "VoIP VLAN Query: %u", tvb_get_ntohs(tvb, offset + 5));
                } else {
                    /*
                     * XXX - what are these?  I've seen them in some captures;
                     * they have a length of 6, and run up to the end of
                     * the packet, so if we try to dissect it the same way
                     * we dissect the 7-byte ones, we report a malformed
                     * frame.
                     */
                    tlv_tree = proto_tree_add_subtree(cdp_tree, tvb,
                                               offset, length, ett_cdp_tlv, NULL, "VoIP VLAN Query");
                }
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    1, "Data");
                if (length >= 7) {
                    proto_tree_add_text(tlv_tree, tvb, offset + 5,
                                        2, "Voice VLAN: %u",
                                        tvb_get_ntohs(tvb, offset + 5));
                }
            }
            offset += length;
            break;

        case TYPE_POWER:
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Power Consumption: %u mW",
                                           tvb_get_ntohs(tvb, offset + 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    length - 4, "Power Consumption: %u mW",
                                    tvb_get_ntohs(tvb, offset + 4));
            }
            offset += length;
            break;

        case TYPE_MTU:
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "MTU: %u",
                                           tvb_get_ntohl(tvb,offset + 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    length - 4, "MTU: %u",
                                    tvb_get_ntohl(tvb,offset + 4));
            }
            offset += length;
            break;

        case TYPE_TRUST_BITMAP:
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Trust Bitmap: 0x%02X",
                                           tvb_get_guint8(tvb, offset + 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    length - 4, "Trust Bitmap: %02x",
                                    tvb_get_guint8(tvb, offset + 4));
            }
            offset += length;
            break;

        case TYPE_UNTRUSTED_COS:
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Untrusted port CoS: 0x%02X",
                                           tvb_get_guint8(tvb, offset + 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    length - 4, "Untrusted port CoS: %02x",
                                    tvb_get_guint8(tvb, offset + 4));
            }
            offset += length;
            break;

        case TYPE_SYSTEM_NAME:
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "System Name: %s",
                                           tvb_format_text(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    length - 4, "System Name: %s",
                                    tvb_format_text(tvb, offset + 4, length - 4));
            }
            offset += length;
            break;

        case TYPE_SYSTEM_OID:
            if (tree) {
                tlv_tree = proto_tree_add_subtree(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "System Object Identifier");
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    length - 4, "System Object Identifier: %s",
                                    tvb_bytes_to_ep_str(tvb, offset + 4, length - 4));
            }
            offset += length;
            break;

        case TYPE_MANAGEMENT_ADDR:
            if (tree) {
                tlv_tree = proto_tree_add_subtree(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Management Addresses");
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
            }
            offset += 4;
            length -= 4;
            naddresses = tvb_get_ntohl(tvb, offset);
            if (tree) {
                proto_tree_add_text(tlv_tree, tvb, offset, 4,
                                    "Number of addresses: %u", naddresses);
            }
            offset += 4;
            length -= 4;
            while (naddresses != 0) {
                addr_length = dissect_address_tlv(tvb, offset, length,
                                                  tlv_tree);
                if (addr_length < 0)
                    break;
                offset += addr_length;
                length -= addr_length;

                naddresses--;
            }
            offset += length;
            break;

        case TYPE_LOCATION:
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Location: %s",
                                           tvb_format_text(tvb, offset + 5, length - 5));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    1 , "UNKNOWN: 0x%02X",
                                    tvb_get_guint8(tvb, offset + 4));
                proto_tree_add_text(tlv_tree, tvb, offset + 5,
                                    length - 5, "Location: %s",
                                    tvb_format_text(tvb, offset + 5, length - 5));
            }
            offset += length;
            break;

        case TYPE_POWER_REQUESTED:
            if (tree) {
                tlv_tree = proto_tree_add_subtree(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Power Request: ");
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    2, "Request-ID: %u",
                                    tvb_get_ntohs(tvb, offset + 4));
                proto_tree_add_text(tlv_tree, tvb, offset + 6,
                                    2, "Management-ID: %u",
                                    tvb_get_ntohs(tvb, offset + 6));
            }
            power_req_len = (tvb_get_ntohs(tvb, offset + TLV_LENGTH)) - 8;
            /* Move offset to where the list of Power Request Values Exist */
            offset += 8;
            while(power_req_len) {
                if (power_req_len > 4) {
                    power_req = tvb_get_ntohl(tvb, offset);
                    if (tree) {
                        proto_tree_add_text(tlv_tree, tvb, offset,
                                            4, "Power Requested: %u mW", power_req);
                        proto_item_append_text(tlvi, "%u mW, ", power_req);
                    }
                    power_req_len -= 4;
                    offset += 4;
                } else {
                    if (power_req_len == 4) {
                        power_req = tvb_get_ntohl(tvb, offset);
                        if (tree) {
                            proto_tree_add_text(tlv_tree, tvb, offset,
                                                4, "Power Requested: %u mW", power_req);
                            proto_item_append_text(tlvi, "%u mW", power_req);
                        }
                    }
                    offset += power_req_len;
                    break;
                }
            }
            break;

        case TYPE_POWER_AVAILABLE:
            if (tree) {
                tlv_tree = proto_tree_add_subtree(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Power Available: ");
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    2, "Request-ID: %u",
                                    tvb_get_ntohs(tvb, offset + 4));
                proto_tree_add_text(tlv_tree, tvb, offset + 6,
                                    2, "Management-ID: %u",
                                    tvb_get_ntohs(tvb, offset + 6));
            }
            power_avail_len = (tvb_get_ntohs(tvb, offset + TLV_LENGTH)) - 8;
            /* Move offset to where the list of Power Available Values Exist */
            offset += 8;
            while(power_avail_len) {
                if (power_avail_len >= 4) {
                    power_avail = tvb_get_ntohl(tvb, offset);
                    if (tree) {
                        proto_tree_add_text(tlv_tree, tvb, offset,
                                            4, "Power Available: %u mW", power_avail);
                        proto_item_append_text(tlvi, "%u mW, ", power_avail);
                    }
                    power_avail_len -= 4;
                    offset += 4;
                } else {
                    offset += power_avail_len;
                    break;
                }
            }
            break;

        case TYPE_NRGYZ:
            if (tree) {
                tlv_tree = proto_tree_add_subtree(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "EnergyWise");
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                    20, "Encrypted Data");
                proto_tree_add_text(tlv_tree, tvb, offset + 24,
                                    4, "Unknown (Seen Sequence?): %u",
                                    tvb_get_ntohl(tvb, offset + 24));
                proto_tree_add_text(tlv_tree, tvb, offset + 28,
                                    4, "Sequence Number: %u",
                                    tvb_get_ntohl(tvb, offset + 28));
                proto_tree_add_text(tlv_tree, tvb, offset + 32,
                                    16, "Model Number: %s",
                                    tvb_format_stringzpad(tvb, offset + 32, 16));
                proto_tree_add_text(tlv_tree, tvb, offset + 48,
                                    2, "Unknown Pad: %x",
                                    tvb_get_ntohs(tvb, offset + 48));
                proto_tree_add_text(tlv_tree, tvb, offset + 50,
                                    3, "Hardware Version ID: %s",
                                    tvb_format_stringzpad(tvb, offset + 50, 3));
                proto_tree_add_text(tlv_tree, tvb, offset + 53,
                                    11, "System Serial Number: %s",
                                    tvb_format_stringzpad(tvb, offset + 53, 11));
                proto_tree_add_text(tlv_tree, tvb, offset + 64,
                                    8, "Unknown Values");
                proto_tree_add_text(tlv_tree, tvb, offset + 72,
                                    2, "Length of TLV table: %u",
                                    tvb_get_ntohs(tvb, offset + 72));
                proto_tree_add_text(tlv_tree, tvb, offset + 74,
                                    2, "Number of TLVs in table: %u",
                                    tvb_get_ntohs(tvb, offset + 74));
/*
  proto_tree_add_text(tlv_tree, tvb,
  offset + 76, length - 76,
  "EnergyWise TLV Table");
*/
                dissect_nrgyz_tlv(tvb, offset + 76,
                                  tvb_get_ntohs(tvb, offset + 72),
                                  tvb_get_ntohs(tvb, offset + 74),
                                  tlv_tree);


            }
            offset += length;
            break;

        case TYPE_SPARE_POE:
            if (tree) {
                tlv_tree = proto_tree_add_subtree(cdp_tree, tvb, offset, length,
                                           ett_cdp_tlv, NULL, "Spare Pair PoE");

                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
            }
            offset += 4;
            length -= 4;
            dissect_spare_poe_tlv(tvb, offset, length, tlv_tree);
            offset += length;
            break;

        case TYPE_HP_BSSID:
            /* BSSID */
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "BSSID: %s",
                                           tvb_format_text(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_platform, tvb, offset + 4, length - 4, ENC_ASCII|ENC_NA);
            }
            offset += length;
            break;

        case TYPE_HP_SERIAL:
            /* Serial number */
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Serial: %s",
                                           tvb_format_text(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_platform, tvb, offset + 4, length - 4, ENC_ASCII|ENC_NA);
            }
            offset += length;
            break;

        case TYPE_HP_SSID:
            /* SSID */
            if (tree) {
                if (length == 4) {
                    tlv_tree = proto_tree_add_subtree(cdp_tree, tvb,
                                               offset, length, ett_cdp_tlv, NULL, "SSID: [Empty]");
                    proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                    proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                } else {
                    tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                               offset, length, ett_cdp_tlv, NULL, "SSID: %s",
                                               tvb_format_text(tvb, offset + 4, length - 4));
                    proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                    proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                    proto_tree_add_item(tlv_tree, hf_cdp_platform, tvb, offset + 4, length - 4, ENC_ASCII|ENC_NA);
                }
            }
            offset += length;
            break;

        case TYPE_HP_RADIO1_CH:
            /* Radio1 channel */
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Radio 1 channel: %s",
                                           tvb_format_text(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_platform, tvb, offset + 4, length - 4, ENC_ASCII|ENC_NA);
            }
            offset += length;
            break;

        case TYPE_HP_SNMP_PORT:
            /* SNMP listening UDP port */
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "SNMP port: %s",
                                           tvb_format_text(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_platform, tvb, offset + 4, length - 4, ENC_ASCII|ENC_NA);
            }
            offset += length;
            break;

        case TYPE_HP_MGMT_PORT:
            /* Web interface TCP port */
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Web mgmt port: %s",
                                           tvb_format_text(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_platform, tvb, offset + 4, length - 4, ENC_ASCII|ENC_NA);
            }
            offset += length;
            break;

        case TYPE_HP_SOURCE_MAC:
            /* Sender MAC address for the AP, bouth wired and wireless */
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Source MAC: %s",
                                           tvb_format_text(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_platform, tvb, offset + 4, length - 4, ENC_ASCII|ENC_NA);
            }
            offset += length;
            break;

        case TYPE_HP_RADIO2_CH:
            /* Radio2 channel */
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Radio 2 channel: %s",
                                           tvb_format_text(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_platform, tvb, offset + 4, length - 4, ENC_ASCII|ENC_NA);
            }
            offset += length;
            break;

        case TYPE_HP_RADIO1_OMODE:
            /* Radio1 Operating mode */
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Radio 1 operating mode: %s",
                                           tvb_format_text(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_platform, tvb, offset + 4, length - 4, ENC_ASCII|ENC_NA);
            }
            offset += length;
            break;

        case TYPE_HP_RADIO2_OMODE:
            /* Radio2 Operating mode */
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Radio 2 operating mode: %s",
                                           tvb_format_text(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_platform, tvb, offset + 4, length - 4, ENC_ASCII|ENC_NA);
            }
            offset += length;
            break;

        case TYPE_HP_RADIO1_RMODE:
            /* Radio1 Radio mode */
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Radio 1 radio mode: %s",
                                           tvb_format_text(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_platform, tvb, offset + 4, length - 4, ENC_ASCII|ENC_NA);
            }
            offset += length;
            break;

        case TYPE_HP_RADIO2_RMODE:
            /* Radio2 Radio mode */
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb,
                                           offset, length, ett_cdp_tlv, NULL, "Radio 2 radio mode: %s",
                                           tvb_format_text(tvb, offset + 4, length - 4));
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_platform, tvb, offset + 4, length - 4, ENC_ASCII|ENC_NA);
            }
            offset += length;
            break;

        default:
            if (tree) {
                tlv_tree = proto_tree_add_subtree_format(cdp_tree, tvb, offset,
                                           length, ett_cdp_tlv, NULL, "Type: %s, length: %u",
                                           val_to_str(type, type_vals, "Unknown (0x%04x)"),
                                           length);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvtype, tvb, offset + TLV_TYPE, 2, ENC_BIG_ENDIAN);
                proto_tree_add_item(tlv_tree, hf_cdp_tlvlength, tvb, offset + TLV_LENGTH, 2, ENC_BIG_ENDIAN);
                if (length > 4) {
                    proto_tree_add_text(tlv_tree, tvb, offset + 4,
                                        length - 4, "Data");
                } else {
                    return;
                }
            }
            offset += length;
        }
    }
    call_dissector(data_handle, tvb_new_subset_remaining(tvb, offset), pinfo, cdp_tree);
}

#define PROTO_TYPE_NLPID       1
#define PROTO_TYPE_IEEE_802_2  2

static const value_string proto_type_vals[] = {
    { PROTO_TYPE_NLPID,      "NLPID" },
    { PROTO_TYPE_IEEE_802_2, "802.2" },
    { 0,                     NULL }
};

static int
dissect_address_tlv(tvbuff_t *tvb, int offset, int length, proto_tree *tree)
{
    proto_item *ti;
    proto_tree *address_tree;
    guint8      protocol_type;
    guint8      protocol_length;
    int         nlpid;
    const char *protocol_str;
    guint16     address_length;
    const char *address_type_str;
    const char *address_str;

    if (length < 1)
        return -1;
    address_tree = proto_tree_add_subtree(tree, tvb, offset, length, ett_cdp_address, &ti, "Truncated address");
    protocol_type = tvb_get_guint8(tvb, offset);
    proto_tree_add_text(address_tree, tvb, offset, 1, "Protocol type: %s",
                        val_to_str(protocol_type, proto_type_vals, "Unknown (0x%02x)"));
    offset += 1;
    length -= 1;

    if (length < 1)
        return -1;
    protocol_length = tvb_get_guint8(tvb, offset);
    proto_tree_add_text(address_tree, tvb, offset, 1, "Protocol length: %u",
                        protocol_length);
    offset += 1;
    length -= 1;

    if (length < protocol_length) {
        if (length != 0) {
            proto_tree_add_text(address_tree, tvb, offset, length,
              "Protocol: %s (truncated)",
              tvb_bytes_to_ep_str(tvb, offset, length));
        }
        return -1;
    }
    protocol_str = NULL;
    if ((protocol_type == PROTO_TYPE_NLPID) && (protocol_length == 1)) {
        nlpid = tvb_get_guint8(tvb, offset);
        protocol_str = val_to_str(nlpid, nlpid_vals, "Unknown (0x%02x)");
    } else
        nlpid = -1;
    if (protocol_str == NULL)
        protocol_str = tvb_bytes_to_ep_str(tvb, offset, protocol_length);
    proto_tree_add_text(address_tree, tvb, offset, protocol_length,
                        "Protocol: %s", protocol_str);
    offset += protocol_length;
    length -= protocol_length;

    if (length < 2)
        return -1;
    address_length = tvb_get_ntohs(tvb, offset);
    proto_tree_add_text(address_tree, tvb, offset, 2, "Address length: %u",
                        address_length);
    offset += 2;
    length -= 2;

    if (length < address_length) {
        if (length != 0) {
            proto_tree_add_text(address_tree, tvb, offset, length,
              "Address: %s (truncated)",
              tvb_bytes_to_ep_str(tvb, offset, length));
        }
        return -1;
    }
    /* XXX - the Cisco document seems to be saying that, for 802.2-format
       protocol types, 0xAAAA03 0x000000 0x0800 is IPv6, but 0x0800 is
       the Ethernet protocol type for IPv4. */
    address_type_str = NULL;
    address_str = NULL;
    if ((protocol_type == PROTO_TYPE_NLPID) && (protocol_length == 1)) {
        switch (nlpid) {

        /* XXX - dissect NLPID_ISO8473_CLNP as OSI CLNP address? */

        case NLPID_IP:
            if (address_length == 4) {
                /* The address is an IP address. */
                address_type_str = "IP address";
                address_str = tvb_ip_to_str(tvb, offset);
            }
            break;
        }
    }
    if (address_type_str == NULL)
        address_type_str = "Address";
    if (address_str == NULL) {
        address_str = tvb_bytes_to_ep_str(tvb, offset, address_length);
    }
    proto_item_set_text(ti, "%s: %s", address_type_str, address_str);
    proto_tree_add_text(address_tree, tvb, offset, address_length, "%s: %s",
      address_type_str, address_str);
    return 2 + protocol_length + 2 + address_length;
}

static void
dissect_capabilities(tvbuff_t *tvb, int offset, int length, proto_tree *tree)
{
    proto_item *ti;
    proto_tree *capabilities_tree;

    if (length < 4)
        return;
    ti = proto_tree_add_item(tree, hf_cdp_capabilities, tvb, offset, 4, ENC_BIG_ENDIAN);
    capabilities_tree = proto_item_add_subtree(ti, ett_cdp_capabilities);
    proto_tree_add_item(capabilities_tree, hf_cdp_capabilities_router, tvb, offset, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(capabilities_tree, hf_cdp_capabilities_trans_bridge, tvb, offset, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(capabilities_tree, hf_cdp_capabilities_src_bridge, tvb, offset, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(capabilities_tree, hf_cdp_capabilities_switch, tvb, offset, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(capabilities_tree, hf_cdp_capabilities_host, tvb, offset, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(capabilities_tree, hf_cdp_capabilities_igmp_capable, tvb, offset, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(capabilities_tree, hf_cdp_capabilities_repeater, tvb, offset, 4, ENC_BIG_ENDIAN);
}

static void
dissect_nrgyz_tlv(tvbuff_t *tvb, int offset, guint16 length, guint16 num,
                  proto_tree *tree)
{
    guint32     tlvt, tlvl, ip_addr;
    proto_item *it    = NULL;
    proto_tree *etree = NULL;
    char const *ttext = NULL;

    while (num-- && (length >= 8)) {
        tlvt = tvb_get_ntohl(tvb, offset);
        tlvl = tvb_get_ntohl(tvb, offset + 4);

        if (length < tlvl)
            break;
        length -= tlvl;

        if (tlvl < 8) {
            proto_tree_add_text(tree, tvb, offset, 8,
                                "TLV with invalid length %u (< 8)",
                                tlvl);
            offset += 8;
            break;
        }
        else {
            ttext = val_to_str(tlvt, type_nrgyz_vals, "Unknown (0x%04x)");
            switch (tlvt) {
            case TYPE_NRGYZ_ROLE:
            case TYPE_NRGYZ_DOMAIN:
            case TYPE_NRGYZ_NAME:
                etree  = proto_tree_add_subtree_format(tree, tvb, offset,
                                         tlvl, ett_cdp_nrgyz_tlv, NULL, "EnergyWise %s: %s", ttext,
                                         tvb_format_stringzpad(tvb, offset + 8, tlvl - 8)
                    );
                break;
            case TYPE_NRGYZ_REPLYTO:
                ip_addr = tvb_get_ipv4(tvb, offset + 12);
                etree  = proto_tree_add_subtree_format(tree, tvb, offset,
                                         tlvl, ett_cdp_nrgyz_tlv, NULL, "EnergyWise %s: %s port %u",
                                         ttext,
                                         ip_to_str((guint8 *)&ip_addr),
                                         tvb_get_ntohs(tvb, offset + 10)
                    );
                break;
            default:
                etree  = proto_tree_add_subtree_format(tree, tvb, offset,
                                         tlvl, ett_cdp_nrgyz_tlv, NULL, "EnergyWise %s TLV", ttext);
            }
            proto_tree_add_text(etree, tvb, offset, 4,
                                "TLV Type: %x (%s)", tlvt, ttext);
            proto_tree_add_text(etree, tvb, offset + 4, 4,
                                "TLV Length: %u", tlvl);
            switch (tlvt) {
            case TYPE_NRGYZ_ROLE:
            case TYPE_NRGYZ_DOMAIN:
            case TYPE_NRGYZ_NAME:
                proto_tree_add_text(etree, tvb, offset + 8,
                                    tlvl - 8, "%s %s", ttext,
                                    tvb_format_stringzpad(tvb, offset + 8, tlvl - 8)
                    );
                break;
            case TYPE_NRGYZ_REPLYTO:
                ip_addr = tvb_get_ipv4(tvb, offset + 12);
                proto_tree_add_text(etree, tvb, offset + 8, 2,
                                    "Unknown Field");
                proto_tree_add_text(etree, tvb, offset + 10, 2,
                                    "Port %d",
                                    tvb_get_ntohs(tvb, offset + 10)
                    );
                proto_tree_add_text(etree, tvb, offset + 12, 4,
                                    "IP Address %s",
                                    ip_to_str((guint8 *)&ip_addr)
                    );
                proto_tree_add_text(etree, tvb, offset + 16, 2,
                                    "Unknown Field (Backup server Port?)");
                proto_tree_add_text(etree, tvb, offset + 18, 4,
                                    "Unknown Field (Backup Server IP?)");
                break;
            default:
                if (tlvl > 8) {
                    proto_tree_add_text(etree, tvb, offset + 8,
                                        tlvl - 8, "Data");
                }
            }
            offset += tlvl;
        }
    }
    if (length) {
        proto_tree_add_text(tree, tvb, offset, length,
                            "Invalid garbage at end");
    }
}

static void
dissect_spare_poe_tlv(tvbuff_t *tvb, int offset, int length,
                      proto_tree *tree)
{
    proto_item *ti;
    proto_tree *tlv_tree;

    if (length == 0) {
        return;
    }

    ti = proto_tree_add_item(tree, hf_cdp_spare_poe_tlv, tvb, offset, 1, ENC_BIG_ENDIAN);
    tlv_tree = proto_item_add_subtree(ti, ett_cdp_spare_poe_tlv);
    proto_tree_add_item(tlv_tree, hf_cdp_spare_poe_tlv_poe, tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(tlv_tree, hf_cdp_spare_poe_tlv_spare_pair_arch, tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(tlv_tree, hf_cdp_spare_poe_tlv_req_spare_pair_poe, tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(tlv_tree, hf_cdp_spare_poe_tlv_pse_spare_pair_poe, tvb, offset, 1, ENC_BIG_ENDIAN);
}

static void
add_multi_line_string_to_tree(proto_tree *tree, tvbuff_t *tvb, gint start,
  gint len, const gchar *prefix)
{
    int  prefix_len;
    int  i;
    char blanks[64+1];
    gint next;
    int  line_len;
    int  data_len;

    prefix_len = (int)strlen(prefix);
    if (prefix_len > 64)
        prefix_len = 64;
    for (i = 0; i < prefix_len; i++)
        blanks[i] = ' ';
    blanks[i] = '\0';
    while (len > 0) {
        line_len = tvb_find_line_end(tvb, start, len, &next, FALSE);
        data_len = next - start;
        proto_tree_add_text(tree, tvb, start, data_len, "%s%s", prefix,
           tvb_format_stringzpad(tvb, start, line_len));
        start += data_len;
        len   -= data_len;
        prefix = blanks;
    }
}

void
proto_register_cdp(void)
{
    static hf_register_info hf[] = {
        { &hf_cdp_version,
        { "Version",            "cdp.version",  FT_UINT8, BASE_DEC, NULL, 0x0,
          NULL, HFILL }},

        { &hf_cdp_ttl,
        { "TTL",                "cdp.ttl", FT_UINT16, BASE_DEC, NULL, 0x0,
          NULL, HFILL }},

        { &hf_cdp_checksum,
        { "Checksum",           "cdp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
          NULL, HFILL }},

        { &hf_cdp_checksum_good,
          { "Good",       "cdp.checksum_good", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
            "True: checksum matches packet content; False: doesn't match content or not checked", HFILL }},

        { &hf_cdp_checksum_bad,
          { "Bad",       "cdp.checksum_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
            "True: checksum doesn't match packet content; False: matches content or not checked", HFILL }},

        { &hf_cdp_tlvtype,
        { "Type",               "cdp.tlv.type", FT_UINT16, BASE_HEX, VALS(type_vals), 0x0,
          NULL, HFILL }},

        { &hf_cdp_tlvlength,
        { "Length",             "cdp.tlv.len", FT_UINT16, BASE_DEC, NULL, 0x0,
          NULL, HFILL }},

        { &hf_cdp_deviceid,
        {"Device ID", "cdp.deviceid", FT_STRING, BASE_NONE,
         NULL, 0, NULL, HFILL }},

        { &hf_cdp_platform,
        {"Platform", "cdp.platform", FT_STRING, BASE_NONE,
         NULL, 0, NULL, HFILL }},

        { &hf_cdp_portid,
        {"Sent through Interface", "cdp.portid", FT_STRING, BASE_NONE,
                NULL, 0, NULL, HFILL }},

        { &hf_cdp_capabilities,
        {"Capabilities", "cdp.capabilities", FT_UINT32, BASE_HEX,
                NULL, 0, NULL, HFILL }},

        { &hf_cdp_capabilities_router,
        {"Router", "cdp.capabilities.router", FT_BOOLEAN, 32,
                TFS(&tfs_yes_no), 0x01, NULL, HFILL }},

        { &hf_cdp_capabilities_trans_bridge,
        {"Transparent Bridge", "cdp.capabilities.trans_bridge", FT_BOOLEAN, 32,
                TFS(&tfs_yes_no), 0x02, NULL, HFILL }},

        { &hf_cdp_capabilities_src_bridge,
        {"Source Route Bridge", "cdp.capabilities.src_bridge", FT_BOOLEAN, 32,
                TFS(&tfs_yes_no), 0x04, NULL, HFILL }},

        { &hf_cdp_capabilities_switch,
        {"Switch", "cdp.capabilities.switch", FT_BOOLEAN, 32,
                TFS(&tfs_yes_no), 0x08, NULL, HFILL }},

        { &hf_cdp_capabilities_host,
        {"Host", "cdp.capabilities.host", FT_BOOLEAN, 32,
                TFS(&tfs_yes_no), 0x10, NULL, HFILL }},

        { &hf_cdp_capabilities_igmp_capable,
        {"IGMP capable", "cdp.capabilities.igmp_capable", FT_BOOLEAN, 32,
                TFS(&tfs_yes_no), 0x20, NULL, HFILL }},

        { &hf_cdp_capabilities_repeater,
        {"Repeater", "cdp.capabilities.repeater", FT_BOOLEAN, 32,
                TFS(&tfs_yes_no), 0x40, NULL, HFILL }},

        { &hf_cdp_spare_poe_tlv,
        { "Spare Pair PoE", "cdp.spare_poe_tlv", FT_UINT8, BASE_HEX,
                NULL, 0x0, NULL, HFILL }
        },

        { &hf_cdp_spare_poe_tlv_poe,
        { "PSE Four-Wire PoE", "cdp.spare_poe_tlv.poe", FT_BOOLEAN, 8,
                TFS(&tfs_supported_not_supported), 0x01, NULL, HFILL }
        },

        { &hf_cdp_spare_poe_tlv_spare_pair_arch,
        { "PD Spare Pair Architecture", "cdp.spare_poe_tlv.spare_pair_arch", FT_BOOLEAN, 8,
                TFS(&tfs_shared_independent), 0x02, NULL, HFILL }
        },

        { &hf_cdp_spare_poe_tlv_req_spare_pair_poe,
        { "PD Request Spare Pair PoE", "cdp.spare_poe_tlv.req_spare_pair_poe", FT_BOOLEAN, 8,
                TFS(&tfs_on_off), 0x04, NULL, HFILL }
        },

        { &hf_cdp_spare_poe_tlv_pse_spare_pair_poe,
        { "PSE Spare Pair PoE", "cdp.spare_poe_tlv.pse_spare_pair_poe", FT_BOOLEAN, 8,
                TFS(&tfs_on_off), 0x08, NULL, HFILL }
        },
    };

    static gint *ett[] = {
        &ett_cdp,
        &ett_cdp_tlv,
        &ett_cdp_nrgyz_tlv,
        &ett_cdp_address,
        &ett_cdp_capabilities,
        &ett_cdp_checksum,
        &ett_cdp_spare_poe_tlv
    };

    proto_cdp = proto_register_protocol("Cisco Discovery Protocol",
                                        "CDP", "cdp");
    proto_register_field_array(proto_cdp, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
}

void
proto_reg_handoff_cdp(void)
{
    dissector_handle_t cdp_handle;

    data_handle = find_dissector("data");
    cdp_handle  = create_dissector_handle(dissect_cdp, proto_cdp);
    dissector_add_uint("llc.cisco_pid", 0x2000, cdp_handle);
    dissector_add_uint("chdlc.protocol", 0x2000, cdp_handle);
    dissector_add_uint("ppp.protocol",  0x0207, cdp_handle);
}