Christophe Tronche's BPDU dissector.
[obnox/wireshark/wip.git] / packet-bpdu.c
1 /* packet-bpdu.c
2  * Routines for BPDU (Spanning Tree Protocol) disassembly
3  *
4  * $Id: packet-bpdu.c,v 1.1 1999/10/08 20:50:38 guy Exp $
5  *
6  * Copyright 1999 Christophe Tronche <ch.tronche@computer.org>
7  * 
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>
38 #endif
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <glib.h>
43 #include "packet.h"
44 #include "resolv.h"
45 #include "util.h"
46
47 /*  #ifndef __PACKET_IP_H__ */
48 /*  #include "packet-ip.h" */
49 /*  #endif */
50
51 /* Offsets of fields within a BPDU */
52
53 #define BPDU_IDENTIFIER          0
54 #define BPDU_VERSION_IDENTIFIER  2
55 #define BPDU_TYPE                3
56 #define BPDU_FLAGS               4
57 #define BPDU_ROOT_IDENTIFIER     5
58 #define BPDU_ROOT_PATH_COST     13
59 #define BPDU_BRIDGE_IDENTIFIER  17
60 #define BPDU_PORT_IDENTIFIER    25
61 #define BPDU_MESSAGE_AGE        27
62 #define BPDU_MAX_AGE            29
63 #define BPDU_HELLO_TIME         31
64 #define BPDU_FORWARD_DELAY      33
65
66 static int proto_bpdu = -1;
67
68 void dissect_bpdu(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
69       guint16 protocol_identifier;
70       guint8  protocol_version_identifier;
71       guint8  bpdu_type;
72       guint8  flags;
73       guint16 root_identifier_bridge_priority;
74       gchar   *root_identifier_mac;
75       guint32 root_path_cost;
76       guint16 bridge_identifier_bridge_priority;
77       gchar   *bridge_identifier_mac;
78       guint16 port_identifier;
79       double message_age;
80       double max_age;
81       double hello_time;
82       double forward_delay;
83       
84       proto_tree *bpdu_tree;
85       proto_item *ti;
86       const u_char *bpdu;
87
88       bpdu = pd + offset;
89       bpdu_type = (guint8) bpdu[BPDU_TYPE];
90       flags = (guint8) bpdu[BPDU_FLAGS];
91       root_identifier_bridge_priority = pntohs(bpdu + BPDU_ROOT_IDENTIFIER);
92       root_identifier_mac = ether_to_str(bpdu + BPDU_ROOT_IDENTIFIER + 2);
93       root_path_cost = pntohl(bpdu + BPDU_ROOT_PATH_COST);
94       port_identifier = pntohs(bpdu + BPDU_PORT_IDENTIFIER);
95
96       if (check_col(fd, COL_PROTOCOL)) {
97             col_add_str(fd, COL_PROTOCOL, "STP"); /* Spanning Tree Protocol */
98       }
99
100       if (check_col(fd, COL_INFO)) {
101             if (bpdu_type == 0)
102                   col_add_fstr(fd, COL_INFO, "Conf. %sRoot = %d/%s  Cost = %d  Port = 0x%04x", 
103                                flags & 0x1 ? "TC + " : "",
104                                root_identifier_bridge_priority, root_identifier_mac, root_path_cost,
105                                port_identifier);
106             else if (bpdu_type == 0x80)
107                   col_add_fstr(fd, COL_INFO, "Topology Change Notification");
108       }
109
110       if (tree) {
111             protocol_identifier = pntohs(bpdu + BPDU_IDENTIFIER);
112             protocol_version_identifier = (guint8) bpdu[BPDU_VERSION_IDENTIFIER];
113
114             ti = proto_tree_add_item_format(tree, proto_bpdu, offset, 35, NULL, "Spanning Tree Protocol");
115             bpdu_tree = proto_item_add_subtree(ti, ETT_BPDU);
116             proto_tree_add_text(bpdu_tree, offset + BPDU_IDENTIFIER, 2, "Protocol Identifier: 0x%04x (%s)", 
117                                 protocol_identifier,
118                                 protocol_identifier == 0 ? "Spanning Tree" : "Unknown Protocol");
119
120             proto_tree_add_text(bpdu_tree, offset + BPDU_VERSION_IDENTIFIER, 1, "Protocol Version Identifier: %d", protocol_version_identifier);
121             if (protocol_version_identifier != 0)
122                   proto_tree_add_text(bpdu_tree, offset + BPDU_VERSION_IDENTIFIER, 1, "   (Warning: this version of packet-bpdu only knows about version = 0)");
123             proto_tree_add_text(bpdu_tree, offset + BPDU_TYPE, 1, "BPDU Type: 0x%02x (%s)", 
124                                 bpdu_type,
125                                 bpdu_type == 0 ? "Configuration" :
126                                 bpdu_type == 0x80 ? "Topology Change Notification" : "Unknown");
127
128             if (bpdu_type != 0) return;
129
130             bridge_identifier_bridge_priority = pntohs(bpdu + BPDU_BRIDGE_IDENTIFIER);
131             bridge_identifier_mac = ether_to_str(bpdu + BPDU_BRIDGE_IDENTIFIER + 2);
132             message_age = pntohs(bpdu + BPDU_MESSAGE_AGE) / 256.0;
133             max_age = pntohs(bpdu + BPDU_MAX_AGE) / 256.0;
134             hello_time = pntohs(bpdu + BPDU_HELLO_TIME) / 256.0;
135             forward_delay = pntohs(bpdu + BPDU_FORWARD_DELAY) / 256.0;
136
137             proto_tree_add_text(bpdu_tree, offset + BPDU_FLAGS, 1, "Flags: 0x%02x", flags);
138             if (flags & 0x80)
139                   proto_tree_add_text(bpdu_tree, offset + BPDU_FLAGS, 1, "   1... ....  Topology Change Acknowledgment");
140             if (flags & 0x01)
141                   proto_tree_add_text(bpdu_tree, offset + BPDU_FLAGS, 1, "   .... ...1  Topology Change");
142
143             proto_tree_add_text(bpdu_tree, offset + BPDU_ROOT_IDENTIFIER, 8, "Root Identifier: %d / %s", root_identifier_bridge_priority, root_identifier_mac);
144             proto_tree_add_text(bpdu_tree, offset + BPDU_ROOT_PATH_COST, 4, "Root Path Cost: %d", root_path_cost);
145             proto_tree_add_text(bpdu_tree, offset + BPDU_BRIDGE_IDENTIFIER, 8, "Bridge Identifier: %d / %s", 
146                                 bridge_identifier_bridge_priority, 
147                                 bridge_identifier_mac);
148             proto_tree_add_text(bpdu_tree, offset + BPDU_PORT_IDENTIFIER, 2, "Port identifier: 0x%04x", port_identifier);
149             proto_tree_add_text(bpdu_tree, offset + BPDU_MESSAGE_AGE, 2, "Message Age: %f", message_age);
150             proto_tree_add_text(bpdu_tree, offset + BPDU_MAX_AGE, 2, "Max Age: %f", max_age);
151             proto_tree_add_text(bpdu_tree, offset + BPDU_HELLO_TIME, 2, "Hello Time: %f", hello_time);
152             proto_tree_add_text(bpdu_tree, offset + BPDU_FORWARD_DELAY, 2, "Forward Delay: %f", forward_delay);
153       }
154 }
155
156 void
157 proto_register_bpdu(void)
158 {
159         proto_bpdu = proto_register_protocol("Spanning Tree Protocol", "STP");
160 }