aboutsummaryrefslogtreecommitdiffstats
path: root/packet-cdp.c
blob: 4414ed66941c03dfa4f91150ac30553104dd2089 (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
/* packet-cdp.c
 * Routines for the disassembly of the "Cisco Discovery Protocol"
 * (c) Copyright Hannes R. Boehm <hannes@boehm.org>
 *
 * $Id: packet-cdp.c,v 1.49 2002/08/28 21:00:08 jmayer Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * 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.
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>

#include <glib.h>
#include <epan/packet.h>
#include <epan/strutil.h>
#include "nlpid.h"

/*
 * See
 *
 *	http://www.cisco.com/univercd/cc/td/doc/product/lan/trsrb/frames.htm
 *
 * for some information on CDP.
 */

/* 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_ttl = -1;
static int hf_cdp_tlvtype = -1;
static int hf_cdp_tlvlength = -1;

static gint ett_cdp = -1;
static gint ett_cdp_tlv = -1;
static gint ett_cdp_address = -1;
static gint ett_cdp_capabilities = -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
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_VTP_MGMT_DOMAIN    0x0009 /* Guessed, from tcpdump */
#define TYPE_NATIVE_VLAN        0x000a /* Guessed, from tcpdump */
#define TYPE_DUPLEX             0x000b /* Guessed, from tcpdump */

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 (used for ODR)" },
	{ TYPE_VTP_MGMT_DOMAIN, "VTP Management Domain" },
	{ TYPE_NATIVE_VLAN,     "Native VLAN" },
	{ TYPE_DUPLEX,          "Duplex" },
	{ 0,                    NULL },
};

static void
dissect_cdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    proto_item *ti;
    proto_tree *cdp_tree = NULL;
    int offset = 0;
    guint16 type;
    guint16 length;
    proto_item *tlvi;
    proto_tree *tlv_tree;
    int real_length;
    guint32 naddresses;
    int addr_length;

    if (check_col(pinfo->cinfo, COL_PROTOCOL))
        col_set_str(pinfo->cinfo, COL_PROTOCOL, "CDP");
    if (check_col(pinfo->cinfo, COL_INFO))
        col_set_str(pinfo->cinfo, COL_INFO, "Cisco Discovery Protocol");

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

	/* CDP header */
	proto_tree_add_item(cdp_tree, hf_cdp_version, tvb, offset, 1, FALSE);
	offset += 1;
	proto_tree_add_uint_format(cdp_tree, hf_cdp_ttl, tvb, offset, 1,
				   tvb_get_guint8(tvb, offset),
				   "TTL: %u seconds",
				   tvb_get_guint8(tvb, offset));
	offset += 1;
	proto_tree_add_item(cdp_tree, hf_cdp_checksum, tvb, offset, 2, FALSE);
	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) {
		tlvi = proto_tree_add_text(cdp_tree, tvb, offset, 4,
		    "TLV with invalid length %u (< 4)",
		    length);
		tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
			    offset + TLV_TYPE, 2, type);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
			    offset + TLV_LENGTH, 2, length);
		offset += 4;
		break;
	    }

	    switch (type) {

	    case TYPE_DEVICE_ID:
		/* Device ID */
		tlvi = proto_tree_add_text(cdp_tree, tvb, offset,
			    length, "Device ID: %.*s",
			    length - 4,
			    tvb_get_ptr(tvb, offset + 4, length - 4));
		tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
			    offset + TLV_TYPE, 2, type);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
			    offset + TLV_LENGTH, 2, length);
		proto_tree_add_text(tlv_tree, tvb, offset + 4,
			    length - 4, "Device ID: %.*s",
			    length - 4,
			    tvb_get_ptr(tvb, offset + 4, length - 4));
		offset += length;
		break;

	    case TYPE_ADDRESS:
		/* Addresses */
		tlvi = proto_tree_add_text(cdp_tree, tvb, offset,
			    length, "Addresses");
		tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
			    offset + TLV_TYPE, 2, type);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
			    offset + TLV_LENGTH, 2, length);
		offset += 4;
		length -= 4;
		naddresses = tvb_get_ntohl(tvb, offset);
		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_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;
		}
		tlvi = proto_tree_add_text(cdp_tree, tvb, offset,
			    real_length, "Port ID: %.*s",
			    real_length - 4,
			    tvb_get_ptr(tvb, offset + 4, real_length - 4));
		tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
			    offset + TLV_TYPE, 2, type);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
			    offset + TLV_LENGTH, 2, length);
		proto_tree_add_text(tlv_tree, tvb, offset + 4,
			    real_length - 4,
			    "Sent through Interface: %.*s",
			    real_length - 4,
			    tvb_get_ptr(tvb, offset + 4, real_length - 4));
		offset += real_length;
		break;

	    case TYPE_CAPABILITIES:
		tlvi = proto_tree_add_text(cdp_tree, tvb, offset,
			    length, "Capabilities");
		tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
			    offset + TLV_TYPE, 2, type);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
			    offset + TLV_LENGTH, 2, length);
		offset += 4;
		length -= 4;
		dissect_capabilities(tvb, offset, length, tlv_tree);
		offset += length;
		break;

	    case TYPE_IOS_VERSION:
		tlvi = proto_tree_add_text(cdp_tree, tvb, offset,
			    length, "Software Version");
		tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
			    offset + TLV_TYPE, 2, type);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
			    offset + TLV_LENGTH, 2, length);
		add_multi_line_string_to_tree(tlv_tree, tvb, offset + 4,
				length - 4, "Software Version: ");
		offset += length;
		break;

	    case TYPE_PLATFORM:
		/* ??? platform */
		tlvi = proto_tree_add_text(cdp_tree, tvb,
			    offset, length, "Platform: %.*s",
			    length - 4,
			    tvb_get_ptr(tvb, offset + 4, length - 4));
		tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
			    offset + TLV_TYPE, 2, type);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
			    offset + TLV_LENGTH, 2, length);
		proto_tree_add_text(tlv_tree, tvb, offset + 4,
			    length - 4, "Platform: %.*s",
			    length - 4,
			    tvb_get_ptr(tvb, offset + 4, length - 4));
		offset += length;
		break;
            case TYPE_IP_PREFIX:
		tlvi = proto_tree_add_text(cdp_tree, tvb, offset,
			    length, "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  :)  */

		tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
			    offset + TLV_TYPE, 2, type);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
			    offset + TLV_LENGTH, 2, length);
		offset += 4;
		length -= 4;
		while (length > 0) {
			proto_tree_add_text(tlv_tree, tvb, offset, 5,
				"IP Prefix = %s/%u",
				ip_to_str(tvb_get_ptr(tvb, offset, 4)),
				tvb_get_guint8(tvb,offset+4));
			offset += 5;
			length -= 5;
		}
		break;
	    case TYPE_VTP_MGMT_DOMAIN:
		tlvi = proto_tree_add_text(cdp_tree, tvb,
			    offset, length, "VTP Management Domain: %.*s",
			    length - 4,
			    tvb_get_ptr(tvb, offset + 4, length - 4));
		tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
			    offset + TLV_TYPE, 2, type);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
			    offset + TLV_LENGTH, 2, length);
		proto_tree_add_text(tlv_tree, tvb, offset + 4,
			    length - 4, "VTP Management Domain: %.*s",
			    length - 4,
			    tvb_get_ptr(tvb, offset + 4, length - 4));
		offset += length;
		break;
	    case TYPE_NATIVE_VLAN:
		tlvi = proto_tree_add_text(cdp_tree, tvb,
			    offset, length, "Native VLAN: %u",
					   tvb_get_ntohs(tvb, offset + 4));
		tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
			    offset + TLV_TYPE, 2, type);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
			    offset + TLV_LENGTH, 2, length);
		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:
		tlvi = proto_tree_add_text(cdp_tree, tvb,
			    offset, length, "Duplex: %s",
					   tvb_get_guint8(tvb, offset + 4) ?
					   "Full" : "Half" );
		tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
			    offset + TLV_TYPE, 2, type);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
			    offset + TLV_LENGTH, 2, length);
		proto_tree_add_text(tlv_tree, tvb, offset + 4,
			    length - 4, "Duplex: %s",
				    tvb_get_guint8(tvb, offset + 4) ?
				    "Full" : "Half" );
		offset += length;
		break;
	    default:
		tlvi = proto_tree_add_text(cdp_tree, tvb, offset,
			    length, "Type: %s, length: %u",
			    val_to_str(type, type_vals, "Unknown (0x%04x)"),
			    length);
		tlv_tree = proto_item_add_subtree(tlvi, ett_cdp_tlv);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvtype, tvb,
			    offset + TLV_TYPE, 2, type);
		proto_tree_add_uint(tlv_tree, hf_cdp_tlvlength, tvb,
			    offset + TLV_LENGTH, 2, length);
		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(tvb, offset, -1, -1), 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;
    char *protocol_str;
    guint16 address_length;
    char *address_type_str;
    char *address_str;

    if (length < 1)
        return -1;
    ti = proto_tree_add_text(tree, tvb, offset, length, "Truncated address");
    address_tree = proto_item_add_subtree(ti, ett_cdp_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_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_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_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. */
    length = 2 + protocol_length + 2 + address_length;
    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 = ip_to_str(tvb_get_ptr(tvb, offset, 4));
            }
            break;
        }
    }
    if (address_type_str == NULL)
        address_type_str = "Address";
    if (address_str == NULL) {
        address_str = tvb_bytes_to_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;
    guint32 capabilities;

    if (length < 4)
        return;
    capabilities = tvb_get_ntohl(tvb, offset);
    ti = proto_tree_add_text(tree, tvb, offset, length, "Capabilities: 0x%08x",
        capabilities);
    capabilities_tree = proto_item_add_subtree(ti, ett_cdp_capabilities);
    proto_tree_add_text(capabilities_tree, tvb, offset, 4,
	decode_boolean_bitfield(capabilities, 0x01, 4*8,
	    "Performs level 3 routing",
	    "Doesn't perform level 3 routing"));
    proto_tree_add_text(capabilities_tree, tvb, offset, 4,
	decode_boolean_bitfield(capabilities, 0x02, 4*8,
	    "Performs level 2 transparent bridging",
	    "Doesn't perform level 2 transparent bridging"));
    proto_tree_add_text(capabilities_tree, tvb, offset, 4,
	decode_boolean_bitfield(capabilities, 0x04, 4*8,
	    "Performs level 2 source-route bridging",
	    "Doesn't perform level 2 source-route bridging"));
    proto_tree_add_text(capabilities_tree, tvb, offset, 4,
	decode_boolean_bitfield(capabilities, 0x08, 4*8,
	    "Performs level 2 switching",
	    "Doesn't perform level 2 switching"));
    proto_tree_add_text(capabilities_tree, tvb, offset, 4,
	decode_boolean_bitfield(capabilities, 0x10, 4*8,
	    "Sends and receives packets for network-layer protocols",
	    "Doesn't send or receive packets for network-layer protocols"));
    proto_tree_add_text(capabilities_tree, tvb, offset, 4,
	decode_boolean_bitfield(capabilities, 0x20, 4*8,
	    "Doesn't forward IGMP Report packets on nonrouter ports",
	    "Forwards IGMP Report packets on nonrouter ports"));
    proto_tree_add_text(capabilities_tree, tvb, offset, 4,
	decode_boolean_bitfield(capabilities, 0x40, 4*8,
	    "Provides level 1 functionality",
	    "Doesn't provide level 1 functionality"));
}

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 = 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,
	   line_len, tvb_get_ptr(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,
	  "", HFILL }},

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

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

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

	{ &hf_cdp_tlvlength,
	{ "Length",		"cdp.tlv.len", FT_UINT16, BASE_DEC, NULL, 0x0,
	  "", HFILL }},
    };
    static gint *ett[] = {
	&ett_cdp,
	&ett_cdp_tlv,
	&ett_cdp_address,
	&ett_cdp_capabilities,
    };

    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("llc.cisco_pid", 0x2000, cdp_handle);
    dissector_add("chdlctype", 0x2000, cdp_handle);
    dissector_add("ppp.protocol", 0x0207, cdp_handle);
}