1 /* packet-dec-bpdu.c
2  * Routines for DEC BPDU (DEC Spanning Tree Protocol) disassembly
3  *
4  * Copyright 2001 Paul Ionescu <paul@acorp.ro>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12 
13 #include "config.h"
14 
15 #include <epan/packet.h>
16 #include <epan/etypes.h>
17 #include <epan/ppptypes.h>
18 
19 /* Offsets of fields within a BPDU */
20 
21 #define BPDU_DEC_CODE            0
22 #define BPDU_TYPE                1
23 #define BPDU_VERSION             2
24 #define BPDU_FLAGS               3
25 #define BPDU_ROOT_PRI            4
26 #define BPDU_ROOT_MAC            6
27 #define BPDU_ROOT_PATH_COST     12
28 #define BPDU_BRIDGE_PRI         14
29 #define BPDU_BRIDGE_MAC         16
30 #define BPDU_PORT_IDENTIFIER    22
31 #define BPDU_MESSAGE_AGE        23
32 #define BPDU_HELLO_TIME         24
33 #define BPDU_MAX_AGE            25
34 #define BPDU_FORWARD_DELAY      26
35 
36 #define DEC_BPDU_SIZE           27
37 
38 /* Flag bits */
39 
40 #define BPDU_FLAGS_SHORT_TIMERS         0x80
41 #define BPDU_FLAGS_TCACK                0x02
42 #define BPDU_FLAGS_TC                   0x01
43 
44 void proto_register_dec_bpdu(void);
45 void proto_reg_handoff_dec_bpdu(void);
46 
47 static int proto_dec_bpdu = -1;
48 static int hf_dec_bpdu_proto_id = -1;
49 static int hf_dec_bpdu_type = -1;
50 static int hf_dec_bpdu_version_id = -1;
51 static int hf_dec_bpdu_flags = -1;
52 static int hf_dec_bpdu_flags_short_timers = -1;
53 static int hf_dec_bpdu_flags_tcack = -1;
54 static int hf_dec_bpdu_flags_tc = -1;
55 static int hf_dec_bpdu_root_pri = -1;
56 static int hf_dec_bpdu_root_mac = -1;
57 static int hf_dec_bpdu_root_cost = -1;
58 static int hf_dec_bpdu_bridge_pri = -1;
59 static int hf_dec_bpdu_bridge_mac = -1;
60 static int hf_dec_bpdu_port_id = -1;
61 static int hf_dec_bpdu_msg_age = -1;
62 static int hf_dec_bpdu_hello_time = -1;
63 static int hf_dec_bpdu_max_age = -1;
64 static int hf_dec_bpdu_forward_delay = -1;
65 
66 static gint ett_dec_bpdu = -1;
67 static gint ett_dec_bpdu_flags = -1;
68 
69 static const value_string protocol_id_vals[] = {
70     { 0xe1, "DEC Spanning Tree Protocol" },
71     { 0,    NULL }
72 };
73 
74 #define BPDU_TYPE_TOPOLOGY_CHANGE       2
75 #define BPDU_TYPE_HELLO                 25
76 
77 static const value_string bpdu_type_vals[] = {
78     { BPDU_TYPE_TOPOLOGY_CHANGE, "Topology Change Notification" },
79     { BPDU_TYPE_HELLO,           "Hello Packet" },
80     { 0,                         NULL }
81 };
82 
83 static int
dissect_dec_bpdu(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data _U_)84 dissect_dec_bpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
85 {
86     guint8  bpdu_type;
87     proto_tree *bpdu_tree;
88     proto_item *ti;
89     static int * const bpdu_flags[] = {
90         &hf_dec_bpdu_flags_short_timers,
91         &hf_dec_bpdu_flags_tcack,
92         &hf_dec_bpdu_flags_tc,
93         NULL
94     };
95 
96     col_set_str(pinfo->cinfo, COL_PROTOCOL, "DEC_STP");
97     col_clear(pinfo->cinfo, COL_INFO);
98 
99     bpdu_type = tvb_get_guint8(tvb, BPDU_TYPE);
100 
101     col_add_str(pinfo->cinfo, COL_INFO,
102                 val_to_str(bpdu_type, bpdu_type_vals,
103                            "Unknown BPDU type (%u)"));
104 
105     set_actual_length(tvb, DEC_BPDU_SIZE);
106 
107     if (tree) {
108         ti = proto_tree_add_item(tree, proto_dec_bpdu, tvb, 0, DEC_BPDU_SIZE,
109                                  ENC_NA);
110         bpdu_tree = proto_item_add_subtree(ti, ett_dec_bpdu);
111 
112         proto_tree_add_item(bpdu_tree, hf_dec_bpdu_proto_id, tvb,
113                             BPDU_DEC_CODE, 1, ENC_BIG_ENDIAN);
114 
115         proto_tree_add_uint(bpdu_tree, hf_dec_bpdu_type, tvb,
116                             BPDU_TYPE, 1, bpdu_type);
117 
118         proto_tree_add_item(bpdu_tree, hf_dec_bpdu_version_id, tvb,
119                             BPDU_VERSION, 1, ENC_BIG_ENDIAN);
120 
121         proto_tree_add_bitmask_with_flags(bpdu_tree, tvb, BPDU_FLAGS, hf_dec_bpdu_flags, ett_dec_bpdu_flags, bpdu_flags, ENC_NA, BMT_NO_FALSE|BMT_NO_TFS);
122         proto_tree_add_item(bpdu_tree, hf_dec_bpdu_root_pri, tvb,
123                             BPDU_ROOT_PRI, 2, ENC_BIG_ENDIAN);
124         proto_tree_add_item(bpdu_tree, hf_dec_bpdu_root_mac, tvb,
125                             BPDU_ROOT_MAC, 6, ENC_NA);
126         proto_tree_add_item(bpdu_tree, hf_dec_bpdu_root_cost, tvb,
127                             BPDU_ROOT_PATH_COST, 2, ENC_BIG_ENDIAN);
128         proto_tree_add_item(bpdu_tree, hf_dec_bpdu_bridge_pri, tvb,
129                             BPDU_BRIDGE_PRI, 2, ENC_BIG_ENDIAN);
130         proto_tree_add_item(bpdu_tree, hf_dec_bpdu_bridge_mac, tvb,
131                             BPDU_BRIDGE_MAC, 6, ENC_NA);
132         proto_tree_add_item(bpdu_tree, hf_dec_bpdu_port_id, tvb,
133                             BPDU_PORT_IDENTIFIER, 1, ENC_BIG_ENDIAN);
134         proto_tree_add_item(bpdu_tree, hf_dec_bpdu_msg_age, tvb,
135                             BPDU_MESSAGE_AGE, 1, ENC_BIG_ENDIAN);
136         proto_tree_add_item(bpdu_tree, hf_dec_bpdu_hello_time, tvb,
137                             BPDU_HELLO_TIME, 1, ENC_BIG_ENDIAN);
138         proto_tree_add_item(bpdu_tree, hf_dec_bpdu_max_age, tvb,
139                             BPDU_MAX_AGE, 1, ENC_BIG_ENDIAN);
140         proto_tree_add_item(bpdu_tree, hf_dec_bpdu_forward_delay, tvb,
141                             BPDU_FORWARD_DELAY, 1, ENC_BIG_ENDIAN);
142 
143     }
144     return tvb_captured_length(tvb);
145 }
146 
147 void
proto_register_dec_bpdu(void)148 proto_register_dec_bpdu(void)
149 {
150 
151     static hf_register_info hf[] = {
152         { &hf_dec_bpdu_proto_id,
153           { "Protocol Identifier",          "dec_stp.protocol",
154             FT_UINT8,       BASE_HEX,       VALS(protocol_id_vals), 0x0,
155             NULL, HFILL }},
156         { &hf_dec_bpdu_type,
157           { "BPDU Type",                    "dec_stp.type",
158             FT_UINT8,       BASE_DEC,       VALS(bpdu_type_vals),   0x0,
159             NULL, HFILL }},
160         { &hf_dec_bpdu_version_id,
161           { "BPDU Version",                 "dec_stp.version",
162             FT_UINT8,       BASE_DEC,       NULL,   0x0,
163             NULL, HFILL }},
164         { &hf_dec_bpdu_flags,
165           { "BPDU flags",                   "dec_stp.flags",
166             FT_UINT8,       BASE_HEX,       NULL,   0x0,
167             NULL, HFILL }},
168         { &hf_dec_bpdu_flags_short_timers,
169           { "Use short timers",             "dec_stp.flags.short_timers",
170             FT_BOOLEAN,     8,              TFS(&tfs_yes_no),       BPDU_FLAGS_SHORT_TIMERS,
171             NULL, HFILL }},
172         { &hf_dec_bpdu_flags_tcack,
173           { "Topology Change Acknowledgment",  "dec_stp.flags.tcack",
174             FT_BOOLEAN,     8,              TFS(&tfs_yes_no),       BPDU_FLAGS_TCACK,
175             NULL, HFILL }},
176         { &hf_dec_bpdu_flags_tc,
177           { "Topology Change",              "dec_stp.flags.tc",
178             FT_BOOLEAN,     8,              TFS(&tfs_yes_no),       BPDU_FLAGS_TC,
179             NULL, HFILL }},
180         { &hf_dec_bpdu_root_pri,
181           { "Root Priority",                "dec_stp.root.pri",
182             FT_UINT16,      BASE_DEC,       NULL,   0x0,
183             NULL, HFILL }},
184         { &hf_dec_bpdu_root_mac,
185           { "Root MAC",                     "dec_stp.root.mac",
186             FT_ETHER,       BASE_NONE,      NULL,   0x0,
187             NULL, HFILL }},
188         { &hf_dec_bpdu_root_cost,
189           { "Root Path Cost",               "dec_stp.root.cost",
190             FT_UINT16,      BASE_DEC,       NULL,   0x0,
191             NULL, HFILL }},
192         { &hf_dec_bpdu_bridge_pri,
193           { "Bridge Priority",              "dec_stp.bridge.pri",
194             FT_UINT16,      BASE_DEC,       NULL,   0x0,
195             NULL, HFILL }},
196         { &hf_dec_bpdu_bridge_mac,
197           { "Bridge MAC",                   "dec_stp.bridge.mac",
198             FT_ETHER,       BASE_NONE,      NULL,   0x0,
199             NULL, HFILL }},
200         { &hf_dec_bpdu_port_id,
201           { "Port identifier",              "dec_stp.port",
202             FT_UINT8,       BASE_DEC,       NULL,   0x0,
203             NULL, HFILL }},
204         { &hf_dec_bpdu_msg_age,
205           { "Message Age",                  "dec_stp.msg_age",
206             FT_UINT8,       BASE_DEC,       NULL,   0x0,
207             NULL, HFILL }},
208         { &hf_dec_bpdu_hello_time,
209           { "Hello Time",                   "dec_stp.hello",
210             FT_UINT8,       BASE_DEC,       NULL,   0x0,
211             NULL, HFILL }},
212         { &hf_dec_bpdu_max_age,
213           { "Max Age",                      "dec_stp.max_age",
214             FT_UINT8,       BASE_DEC,       NULL,   0x0,
215             NULL, HFILL }},
216         { &hf_dec_bpdu_forward_delay,
217           { "Forward Delay",                "dec_stp.forward",
218             FT_UINT8,       BASE_DEC,       NULL,   0x0,
219             NULL, HFILL }},
220     };
221     static gint *ett[] = {
222         &ett_dec_bpdu,
223         &ett_dec_bpdu_flags,
224     };
225 
226     proto_dec_bpdu = proto_register_protocol("DEC Spanning Tree Protocol",
227                                              "DEC_STP", "dec_stp");
228     proto_register_field_array(proto_dec_bpdu, hf, array_length(hf));
229     proto_register_subtree_array(ett, array_length(ett));
230 }
231 
232 void
proto_reg_handoff_dec_bpdu(void)233 proto_reg_handoff_dec_bpdu(void)
234 {
235     dissector_handle_t dec_bpdu_handle;
236 
237     dec_bpdu_handle = create_dissector_handle(dissect_dec_bpdu,
238                                               proto_dec_bpdu);
239     dissector_add_uint("ethertype", ETHERTYPE_DEC_LB, dec_bpdu_handle);
240     dissector_add_uint("chdlc.protocol", ETHERTYPE_DEC_LB, dec_bpdu_handle);
241     dissector_add_uint("ppp.protocol", PPP_DEC_LB, dec_bpdu_handle);
242 }
243 
244 /*
245  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
246  *
247  * Local variables:
248  * c-basic-offset: 4
249  * tab-width: 8
250  * indent-tabs-mode: nil
251  * End:
252  *
253  * vi: set shiftwidth=4 tabstop=8 expandtab:
254  * :indentSize=4:tabSize=8:noTabs=true:
255  */
256