2 * Routines for the disassembly of the "Cisco Discovery Protocol"
3 * (c) Copyright Hannes R. Boehm <hannes@boehm.org>
5 * $Id: packet-cdp.c,v 1.17 1999/11/16 11:42:28 guy Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@zing.org>
9 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
39 /* Offsets in TLV structure. */
43 static int proto_cdp = -1;
44 static int hf_cdp_version = -1;
45 static int hf_cdp_flags = -1;
46 static int hf_cdp_ttl = -1;
47 static int hf_cdp_tlvtype = -1;
48 static int hf_cdp_tlvlength = -1;
50 static gint ett_cdp = -1;
51 static gint ett_cdp_tlv = -1;
54 add_multi_line_string_to_tree(proto_tree *tree, gint start, gint len,
55 const gchar *prefix, const gchar *string);
57 #define TYPE_MGMT_ADDR 0
58 #define TYPE_CHASSIS_ID 1
61 #define TYPE_IOS_VERSION 5
62 #define TYPE_PLATFORM 6
63 #define TYPE_MGMT_IP_ADDR 0x01cc
65 static const value_string type_vals[] = {
66 { TYPE_MGMT_ADDR, "Mgmt addr?" },
67 { TYPE_CHASSIS_ID, "Chassis ID" },
68 { TYPE_2, "Unknown" },
69 { TYPE_PORT, "Port" },
70 { TYPE_IOS_VERSION, "Software version" },
71 { TYPE_PLATFORM, "Platform" },
72 { TYPE_MGMT_IP_ADDR, "Mgmt IP" },
77 dissect_cdp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
79 proto_tree *cdp_tree = NULL;
87 if (check_col(fd, COL_PROTOCOL))
88 col_add_str(fd, COL_PROTOCOL, "CDP");
89 if (check_col(fd, COL_INFO))
90 col_add_str(fd, COL_INFO, "Cisco Discovery Protocol");
93 ti = proto_tree_add_item(tree, proto_cdp, offset, END_OF_FRAME, NULL);
94 cdp_tree = proto_item_add_subtree(ti, ett_cdp);
97 proto_tree_add_item(cdp_tree, hf_cdp_version, offset, 1, pd[offset]);
99 proto_tree_add_item_format(cdp_tree, hf_cdp_flags, offset, 1,
101 "Flags: %x (unknown)", pd[offset]);
103 proto_tree_add_item_format(cdp_tree, hf_cdp_ttl, offset, 2,
105 "TTL: %u (unknown)", pntohs(&pd[offset]));
108 while( IS_DATA_IN_FRAME(offset) ){
109 type = pntohs(&pd[offset + TLV_TYPE]);
110 length = pntohs(&pd[offset + TLV_LENGTH]);
111 type_str = val_to_str(type, type_vals,
116 /* ??? Mgmt Addr; in this one, the "length"
117 field doesn't include the length of the
118 type and length fields. */
119 tlvi = proto_tree_add_text(cdp_tree, offset,
120 length + 4, "Type: %s, length: %u",
122 tlv_tree = proto_item_add_subtree(tlvi,
124 proto_tree_add_item(tlv_tree, hf_cdp_tlvtype,
125 offset + TLV_TYPE, 2, type);
126 proto_tree_add_item(tlv_tree, hf_cdp_tlvlength,
127 offset + TLV_LENGTH, 2, length);
129 proto_tree_add_text(tlv_tree,
130 offset + 4, length, "Data");
134 case TYPE_CHASSIS_ID:
136 tlvi = proto_tree_add_text(cdp_tree, offset,
137 length, "Chassis ID: %s",
139 tlv_tree = proto_item_add_subtree(tlvi,
141 proto_tree_add_item(tlv_tree, hf_cdp_tlvtype,
142 offset + TLV_TYPE, 2, type);
143 proto_tree_add_item(tlv_tree, hf_cdp_tlvlength,
144 offset + TLV_LENGTH, 2, length);
145 proto_tree_add_text(tlv_tree, offset + 4,
146 length - 4, "Chassis ID: %s",
151 /* this is quite strange: this tlv contains
152 no data itself but two tlvs which
153 calculate the length without the 2 byte
154 type and 2 byte length field */
155 tlvi = proto_tree_add_text(cdp_tree, offset,
156 4, "Type: %u (unknown), second field: %u",
158 tlv_tree = proto_item_add_subtree(tlvi,
160 proto_tree_add_item(tlv_tree, hf_cdp_tlvtype,
161 offset + TLV_TYPE, 2, type);
162 proto_tree_add_text(tlv_tree,
163 offset + TLV_LENGTH, 2, "Second field: %u",
169 tlvi = proto_tree_add_text(cdp_tree, offset,
170 length, "Sent through Interface: %s",
172 tlv_tree = proto_item_add_subtree(tlvi,
174 proto_tree_add_item(tlv_tree, hf_cdp_tlvtype,
175 offset + TLV_TYPE, 2, type);
176 proto_tree_add_item(tlv_tree, hf_cdp_tlvlength,
177 offset + TLV_LENGTH, 2, length);
178 proto_tree_add_text(tlv_tree, offset + 4,
179 length - 4, "Sent through Interface: %s",
183 case TYPE_IOS_VERSION:
184 /* ??? IOS Version */
185 add_multi_line_string_to_tree(cdp_tree,
186 offset + 4, length - 4, "Software Version: ",
192 stringmem = malloc(length);
193 memset(stringmem, '\0', length);
194 memcpy(stringmem, &pd[offset+4], length - 4 );
195 tlvi = proto_tree_add_text(cdp_tree,
196 offset, length, "Platform: %s",
198 tlv_tree = proto_item_add_subtree(tlvi,
200 proto_tree_add_item(tlv_tree, hf_cdp_tlvtype,
201 offset + TLV_TYPE, 2, type);
202 proto_tree_add_item(tlv_tree, hf_cdp_tlvlength,
203 offset + TLV_LENGTH, 2, length);
204 proto_tree_add_text(tlv_tree, offset + 4,
205 length - 4, "Platform: %s", stringmem);
209 case TYPE_MGMT_IP_ADDR:
210 /* ??? Mgmt IP Addr; in this one, the "length"
211 field doesn't include the length of the
212 type and length fields. */
213 tlvi = proto_tree_add_text(cdp_tree,
214 offset, length + 4, "Mgmt IP: %s",
215 ip_to_str(&pd[offset+4]));
216 tlv_tree = proto_item_add_subtree(tlvi,
218 proto_tree_add_item(tlv_tree, hf_cdp_tlvtype,
219 offset + TLV_TYPE, 2, type);
220 proto_tree_add_item(tlv_tree, hf_cdp_tlvlength,
221 offset + TLV_LENGTH, 2, length);
222 proto_tree_add_text(tlv_tree, offset + 4,
223 length, "IP address: %s",
224 ip_to_str(&pd[offset+4]));
228 tlvi = proto_tree_add_text(cdp_tree, offset,
229 length, "Type: %s, length: %u",
231 tlv_tree = proto_item_add_subtree(tlvi,
233 proto_tree_add_item(tlv_tree, hf_cdp_tlvtype,
234 offset + TLV_TYPE, 2, type);
235 proto_tree_add_item(tlv_tree, hf_cdp_tlvlength,
236 offset + TLV_LENGTH, 2, length);
238 proto_tree_add_text(tlv_tree,
239 offset + 4, length - 4, "Data");
245 dissect_data(pd, offset, fd, cdp_tree);
250 add_multi_line_string_to_tree(proto_tree *tree, gint start, gint len,
251 const gchar *prefix, const gchar *string)
260 prefix_len = strlen(prefix);
263 for (i = 0; i < prefix_len; i++)
271 data_len = line_len + 1;
273 line_len = strlen(p);
276 proto_tree_add_text(tree, start, data_len, "%s%.*s", prefix,
287 proto_register_cdp(void)
289 static hf_register_info hf[] = {
291 { "Version", "cdp.version", FT_UINT8, BASE_DEC, NULL, 0x0,
295 { "Flags", "cdp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
299 { "TTL", "cdp.ttl", FT_UINT16, BASE_DEC, NULL, 0x0,
303 { "Type", "cdp.tlv.type", FT_UINT16, BASE_HEX, VALS(type_vals), 0x0,
307 { "Length", "cdp.tlv.len", FT_UINT16, BASE_DEC, NULL, 0x0,
310 static gint *ett[] = {
315 proto_cdp = proto_register_protocol("Cisco Discovery Protocol", "cdp");
316 proto_register_field_array(proto_cdp, hf, array_length(hf));
317 proto_register_subtree_array(ett, array_length(ett));