aboutsummaryrefslogtreecommitdiffstats
path: root/packet-cdp.c
blob: 5ea46954ec317d0c05efc82976dff8e36c198dd1 (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
/* 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.16 1999/10/16 14:27:00 deniel Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@zing.org>
 * Copyright 1998 Gerald Combs
 * 
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 
#include "config.h"

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

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

#include <glib.h>
#include "packet.h"

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

static void
add_multi_line_string_to_tree(proto_tree *tree, gint start, gint len,
  const gchar *prefix, const gchar *string);

#define TYPE_MGMT_ADDR		0
#define TYPE_CHASSIS_ID		1
#define TYPE_2			2
#define TYPE_PORT		3
#define TYPE_IOS_VERSION	5
#define TYPE_PLATFORM		6
#define TYPE_MGMT_IP_ADDR	0x01cc

static const value_string type_vals[] = {
	{ TYPE_MGMT_ADDR,    "Mgmt addr?" },
	{ TYPE_CHASSIS_ID,   "Chassis ID" },
	{ TYPE_2,            "Unknown" },
	{ TYPE_PORT,         "Port" },
	{ TYPE_IOS_VERSION,  "Software version" },
	{ TYPE_PLATFORM,     "Platform" },
	{ TYPE_MGMT_IP_ADDR, "Mgmt IP" },
	{ 0,                 NULL },
};
	
void 
dissect_cdp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
    proto_item *ti; 
    proto_tree *cdp_tree = NULL;
    guint16 type;
    guint16 length;
    char *type_str;
    char *stringmem;
    proto_item *tlvi;
    proto_tree *tlv_tree;

    if (check_col(fd, COL_PROTOCOL))
        col_add_str(fd, COL_PROTOCOL, "CDP");
    if (check_col(fd, COL_INFO))
        col_add_str(fd, COL_INFO, "Cisco Discovery Protocol"); 

    if(tree){
        ti = proto_tree_add_item(tree, proto_cdp, offset, END_OF_FRAME, NULL);
	cdp_tree = proto_item_add_subtree(ti, ETT_CDP);
	
	/* CDP header */
	proto_tree_add_item(cdp_tree, hf_cdp_version, offset, 1, pd[offset]);
	offset += 1;
	proto_tree_add_item_format(cdp_tree, hf_cdp_flags, offset, 1,
				   pd[offset], 
				   "Flags: %x (unknown)", pd[offset]);
	offset += 1;
	proto_tree_add_item_format(cdp_tree, hf_cdp_ttl, offset, 2, 
				   pntohs(&pd[offset]),
				   "TTL: %u (unknown)", pntohs(&pd[offset]));
	offset += 2;

	while( IS_DATA_IN_FRAME(offset) ){
		type = pntohs(&pd[offset + TLV_TYPE]);
		length = pntohs(&pd[offset + TLV_LENGTH]);
		type_str = val_to_str(type, type_vals,
		    "Unknown (0x%04x)");

		switch( type ){
			case TYPE_MGMT_ADDR:
				/* ??? Mgmt Addr; in this one, the "length"
				   field doesn't include the length of the
				   type and length fields. */
				tlvi = proto_tree_add_text(cdp_tree, offset,
				    length + 4, "Type: %s, length: %u",
				    type_str, length);
				tlv_tree = proto_item_add_subtree(tlvi,
				    ETT_CDP_TLV);
				proto_tree_add_item(tlv_tree, hf_cdp_tlvtype,
				    offset + TLV_TYPE, 2, type);
				proto_tree_add_item(tlv_tree, hf_cdp_tlvlength,
				    offset + TLV_LENGTH, 2, length);
				if (length > 0) {
					proto_tree_add_text(tlv_tree,
					    offset + 4, length, "Data");
				}
				offset+=length + 4;
				break;
			case TYPE_CHASSIS_ID:
				/* ??? Chassis ID */
				tlvi = proto_tree_add_text(cdp_tree, offset,
				    length, "Chassis ID: %s",
				    &pd[offset+4]);
				tlv_tree = proto_item_add_subtree(tlvi,
				    ETT_CDP_TLV);
				proto_tree_add_item(tlv_tree, hf_cdp_tlvtype,
				    offset + TLV_TYPE, 2, type);
				proto_tree_add_item(tlv_tree, hf_cdp_tlvlength,
				    offset + TLV_LENGTH, 2, length);
				proto_tree_add_text(tlv_tree, offset + 4,
				    length - 4, "Chassis ID: %s",
				    &pd[offset+4]);
				offset+=length;
				break;
			case TYPE_2:
				/* this is quite strange: this tlv contains
				   no data itself but two tlvs which
				   calculate the length without the 2 byte
				   type and 2 byte length field */
				tlvi = proto_tree_add_text(cdp_tree, offset,
				    4, "Type: %u (unknown), second field: %u",
				    type, length);
				tlv_tree = proto_item_add_subtree(tlvi,
				    ETT_CDP_TLV);
				proto_tree_add_item(tlv_tree, hf_cdp_tlvtype,
				    offset + TLV_TYPE, 2, type);
				proto_tree_add_text(tlv_tree,
				    offset + TLV_LENGTH, 2, "Second field: %u",
				    length);
				offset+=4;
				break;
			case TYPE_PORT:
				/* ??? Port  */    
				tlvi = proto_tree_add_text(cdp_tree, offset,
				    length, "Sent through Interface: %s",
				    &pd[offset+4]);
				tlv_tree = proto_item_add_subtree(tlvi,
				    ETT_CDP_TLV);
				proto_tree_add_item(tlv_tree, hf_cdp_tlvtype,
				    offset + TLV_TYPE, 2, type);
				proto_tree_add_item(tlv_tree, hf_cdp_tlvlength,
				    offset + TLV_LENGTH, 2, length);
				proto_tree_add_text(tlv_tree, offset + 4,
				    length - 4, "Sent through Interface: %s",
				    &pd[offset+4]);
				offset+=length;
				break;
			case TYPE_IOS_VERSION:
				/* ??? IOS Version */
				add_multi_line_string_to_tree(cdp_tree,
				    offset + 4, length - 4, "Software Version: ",
				    &pd[offset+4] );
				offset+=length;
				break;
			case TYPE_PLATFORM:
				/* ??? platform */
				stringmem = malloc(length);
				memset(stringmem, '\0', length);
				memcpy(stringmem, &pd[offset+4], length - 4 );
				tlvi = proto_tree_add_text(cdp_tree,
				    offset, length, "Platform: %s",
				    stringmem);
				tlv_tree = proto_item_add_subtree(tlvi,
				    ETT_CDP_TLV);
				proto_tree_add_item(tlv_tree, hf_cdp_tlvtype,
				    offset + TLV_TYPE, 2, type);
				proto_tree_add_item(tlv_tree, hf_cdp_tlvlength,
				    offset + TLV_LENGTH, 2, length);
				proto_tree_add_text(tlv_tree, offset + 4,
				    length - 4, "Platform: %s", stringmem);
				free(stringmem);
				offset+=length;
				break;
			case TYPE_MGMT_IP_ADDR:
				/* ??? Mgmt IP Addr; in this one, the "length"
				   field doesn't include the length of the
				   type and length fields. */
				tlvi = proto_tree_add_text(cdp_tree,
				    offset, length + 4, "Mgmt IP: %s",
				    ip_to_str(&pd[offset+4]));
				tlv_tree = proto_item_add_subtree(tlvi,
				    ETT_CDP_TLV);
				proto_tree_add_item(tlv_tree, hf_cdp_tlvtype,
				    offset + TLV_TYPE, 2, type);
				proto_tree_add_item(tlv_tree, hf_cdp_tlvlength,
				    offset + TLV_LENGTH, 2, length);
				proto_tree_add_text(tlv_tree, offset + 4,
				    length, "IP address: %s",
				    ip_to_str(&pd[offset+4]));
				offset+=length + 4;
				break;
			default:
				tlvi = proto_tree_add_text(cdp_tree, offset,
				    length, "Type: %s, length: %u",
				    type_str, length);
				tlv_tree = proto_item_add_subtree(tlvi,
				    ETT_CDP_TLV);
				proto_tree_add_item(tlv_tree, hf_cdp_tlvtype,
				    offset + TLV_TYPE, 2, type);
				proto_tree_add_item(tlv_tree, hf_cdp_tlvlength,
				    offset + TLV_LENGTH, 2, length);
				if (length > 4) {
					proto_tree_add_text(tlv_tree,
					    offset + 4, length - 4, "Data");
				} else
					return;
				offset+=length;
		}
	}
    	dissect_data(pd, offset, fd, cdp_tree);
    }
}

static void
add_multi_line_string_to_tree(proto_tree *tree, gint start, gint len,
  const gchar *prefix, const gchar *string)
{
    int prefix_len;
    int i;
    char blanks[64+1];
    const gchar *p, *q;
    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';
    p = string;
    for (;;) {
	q = strchr(p, '\n');
	if (q != NULL) {
	    line_len = q - p;
	    data_len = line_len + 1;
	} else {
	    line_len = strlen(p);
	    data_len = line_len;
	}
	proto_tree_add_text(tree, start, data_len, "%s%.*s", prefix,
	   line_len, p);
	if (q == NULL)
	    break;
	p += data_len;
	start += 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,
			"" }},

                { &hf_cdp_flags,
                { "Flags",		"cdp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
			"" }},

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

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

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

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