Removed trailing whitespaces from .h and .c files using the
[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.37 2002/08/02 23:35:47 jmayer Exp $
5  *
6  * Copyright 1999 Christophe Tronche <ch.tronche@computer.org>
7  * 
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  * 
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.
16  * 
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.
21  * 
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.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include "llcsaps.h"
36 #include "ppptypes.h"
37 #include <epan/resolv.h>
38
39 /* Offsets of fields within a BPDU */
40
41 #define BPDU_IDENTIFIER          0
42 #define BPDU_VERSION_IDENTIFIER  2
43 #define BPDU_TYPE                3
44 #define BPDU_FLAGS               4
45 #define BPDU_ROOT_IDENTIFIER     5
46 #define BPDU_ROOT_PATH_COST     13
47 #define BPDU_BRIDGE_IDENTIFIER  17
48 #define BPDU_PORT_IDENTIFIER    25
49 #define BPDU_MESSAGE_AGE        27
50 #define BPDU_MAX_AGE            29
51 #define BPDU_HELLO_TIME         31
52 #define BPDU_FORWARD_DELAY      33
53 #define BPDU_VERSION_1_LENGTH   35
54
55 #define CONF_BPDU_SIZE          35
56 #define TC_BPDU_SIZE            4
57 #define RST_BPDU_SIZE           36
58
59 /* Flag bits */
60
61 #define BPDU_FLAGS_TCACK                0x80
62 #define BPDU_FLAGS_AGREEMENT            0x40
63 #define BPDU_FLAGS_FORWARDING           0x20
64 #define BPDU_FLAGS_LEARNING             0x10
65 #define BPDU_FLAGS_PORT_ROLE_MASK       0x0C
66 #define BPDU_FLAGS_PORT_ROLE_SHIFT      2
67 #define BPDU_FLAGS_PROPOSAL             0x02
68 #define BPDU_FLAGS_TC                   0x01
69
70 static int proto_bpdu = -1;
71 static int hf_bpdu_proto_id = -1;
72 static int hf_bpdu_version_id = -1;
73 static int hf_bpdu_type = -1;
74 static int hf_bpdu_flags = -1;
75 static int hf_bpdu_flags_tcack = -1;
76 static int hf_bpdu_flags_agreement = -1;
77 static int hf_bpdu_flags_forwarding = -1;
78 static int hf_bpdu_flags_learning = -1;
79 static int hf_bpdu_flags_port_role = -1;
80 static int hf_bpdu_flags_proposal = -1;
81 static int hf_bpdu_flags_tc = -1;
82 static int hf_bpdu_root_mac = -1;
83 static int hf_bpdu_root_cost = -1;
84 static int hf_bpdu_bridge_mac = -1;
85 static int hf_bpdu_port_id = -1;
86 static int hf_bpdu_msg_age = -1;
87 static int hf_bpdu_max_age = -1;
88 static int hf_bpdu_hello_time = -1;
89 static int hf_bpdu_forward_delay = -1;
90 static int hf_bpdu_version_1_length = -1;
91
92 static gint ett_bpdu = -1;
93 static gint ett_bpdu_flags = -1;
94
95 static dissector_handle_t gvrp_handle;
96 static dissector_handle_t gmrp_handle;
97 static dissector_handle_t data_handle;
98
99 static const value_string protocol_id_vals[] = {
100         { 0, "Spanning Tree Protocol" },
101         { 0, NULL }
102 };
103
104 #define BPDU_TYPE_CONF                  0x00
105 #define BPDU_TYPE_RST                   0x02
106 #define BPDU_TYPE_TOPOLOGY_CHANGE       0x80
107
108 static const value_string bpdu_type_vals[] = {
109         { BPDU_TYPE_CONF,            "Configuration" },
110         { BPDU_TYPE_RST,             "Rapid Spanning Tree" },
111         { BPDU_TYPE_TOPOLOGY_CHANGE, "Topology Change Notification" },
112         { 0,                         NULL }
113 };
114
115 static const value_string role_vals[] = {
116         { 1, "Alternate or Backup" },
117         { 2, "Root" },
118         { 3, "Designated" },
119         { 0, NULL }
120 };
121
122 static const char initial_sep[] = " (";
123 static const char cont_sep[] = ", ";
124
125 #define APPEND_BOOLEAN_FLAG(flag, item, string) \
126         if(flag){                                                       \
127                 if(item)                                                \
128                         proto_item_append_text(item, string, sep);      \
129                 sep = cont_sep;                                         \
130         }
131
132 static void
133 dissect_bpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
134 {
135       guint16 protocol_identifier;
136       guint8  protocol_version_identifier;
137       guint8  bpdu_type;
138       guint8  flags;
139       guint16 root_identifier_bridge_priority;
140       const guint8  *root_identifier_mac;
141       gchar   *root_identifier_mac_str;
142       guint32 root_path_cost;
143       guint16 bridge_identifier_bridge_priority;
144       const guint8  *bridge_identifier_mac;
145       gchar   *bridge_identifier_mac_str;
146       guint16 port_identifier;
147       double message_age;
148       double max_age;
149       double hello_time;
150       double forward_delay;
151       
152       proto_tree *bpdu_tree;
153       proto_item *bpdu_item;
154       proto_tree *flags_tree;
155       proto_item *flags_item;
156       guint8    rstp_bpdu;
157       const char *sep;
158
159       /* GARP application frames require special interpretation of the
160          destination address field; otherwise, they will be mistaken as
161          BPDU frames.  
162          Fortunately, they can be recognized by checking the first 6 octets
163          of the destination address, which are in the range from
164          01-80-C2-00-00-20 to 01-80-C2-00-00-2F.
165
166          Yes - we *do* need to check the destination address type;
167          on Linux cooked captures, there *is* no destination address,
168          so it's AT_NONE. */
169       if (pinfo->dl_dst.type == AT_ETHER &&
170           pinfo->dl_dst.data[0] == 0x01 && pinfo->dl_dst.data[1] == 0x80 &&
171           pinfo->dl_dst.data[2] == 0xC2 && pinfo->dl_dst.data[3] == 0x00 &&
172           pinfo->dl_dst.data[4] == 0x00 && ((pinfo->dl_dst.data[5] & 0x20) == 0x20)) {
173
174             protocol_identifier = tvb_get_ntohs(tvb, BPDU_IDENTIFIER);
175
176             switch (pinfo->dl_dst.data[5]) {
177
178             case 0x20:
179                    /* for GMRP */
180                   call_dissector(gmrp_handle, tvb, pinfo, tree);
181                   return;
182
183             case 0x21:
184                   /* for GVRP */
185                   call_dissector(gvrp_handle, tvb, pinfo, tree);
186                   return;
187             }
188
189             pinfo->current_proto = "GARP";
190
191             if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
192                     col_set_str(pinfo->cinfo, COL_PROTOCOL, "GARP");
193                     /* Generic Attribute Registration Protocol */
194             }
195
196             if (check_col(pinfo->cinfo, COL_INFO)) {
197                     col_add_fstr(pinfo->cinfo, COL_INFO,
198                         "Unknown GARP application (0x%02X)",
199                         pinfo->dl_dst.data[5]);
200             }
201
202             return;
203       }
204
205       if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
206             col_set_str(pinfo->cinfo, COL_PROTOCOL, "STP"); /* Spanning Tree Protocol */
207       }
208       if (check_col(pinfo->cinfo, COL_INFO)) {
209             col_clear(pinfo->cinfo, COL_INFO);
210       }
211
212       bpdu_type = tvb_get_guint8(tvb, BPDU_TYPE);
213       switch (bpdu_type) {
214
215       case BPDU_TYPE_CONF:
216       case BPDU_TYPE_RST:
217             flags = tvb_get_guint8(tvb, BPDU_FLAGS);
218             root_identifier_bridge_priority = tvb_get_ntohs(tvb,
219                 BPDU_ROOT_IDENTIFIER);
220             root_identifier_mac = tvb_get_ptr(tvb, BPDU_ROOT_IDENTIFIER + 2, 6);
221             root_identifier_mac_str = ether_to_str(root_identifier_mac);
222             root_path_cost = tvb_get_ntohl(tvb, BPDU_ROOT_PATH_COST);
223             port_identifier = tvb_get_ntohs(tvb, BPDU_PORT_IDENTIFIER);
224             break;
225
226       default:
227             /* Squelch GCC complaints. */
228             flags = 0;
229             root_identifier_bridge_priority = 0;
230             root_identifier_mac = NULL;
231             root_identifier_mac_str = NULL;
232             root_path_cost = 0;
233             port_identifier = 0;
234             break;
235       }
236
237       if (check_col(pinfo->cinfo, COL_INFO)) {
238             switch (bpdu_type) {
239
240             case BPDU_TYPE_CONF:
241                   col_add_fstr(pinfo->cinfo, COL_INFO, "Conf. %sRoot = %d/%s  Cost = %d  Port = 0x%04x", 
242                                flags & 0x1 ? "TC + " : "",
243                                root_identifier_bridge_priority, root_identifier_mac_str, root_path_cost,
244                                port_identifier);
245                   break;
246
247             case BPDU_TYPE_TOPOLOGY_CHANGE:
248                   col_add_fstr(pinfo->cinfo, COL_INFO, "Topology Change Notification");
249                   break;
250
251             case BPDU_TYPE_RST:
252                   col_add_fstr(pinfo->cinfo, COL_INFO, "RST. %sRoot = %d/%s  Cost = %d  Port = 0x%04x", 
253                                flags & 0x1 ? "TC + " : "",
254                                root_identifier_bridge_priority, root_identifier_mac_str, root_path_cost,
255                                port_identifier);
256                   break;
257
258             default:
259                   col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown BPDU type (%u)",
260                                bpdu_type);
261                   break;
262             }
263       }
264
265       switch (bpdu_type) {
266
267       case BPDU_TYPE_CONF:
268         set_actual_length(tvb, CONF_BPDU_SIZE);
269         break;
270
271       case BPDU_TYPE_TOPOLOGY_CHANGE:
272         set_actual_length(tvb, TC_BPDU_SIZE);
273         break;
274
275       case BPDU_TYPE_RST:
276         set_actual_length(tvb, RST_BPDU_SIZE);
277         break;
278       }
279
280       if (tree) {
281             bpdu_item = proto_tree_add_protocol_format(tree, proto_bpdu, tvb,
282                                 0, -1, "Spanning Tree Protocol");
283             bpdu_tree = proto_item_add_subtree(bpdu_item, ett_bpdu);
284
285             protocol_identifier = tvb_get_ntohs(tvb, BPDU_IDENTIFIER);
286             proto_tree_add_uint(bpdu_tree, hf_bpdu_proto_id, tvb,
287                                 BPDU_IDENTIFIER, 2, protocol_identifier);
288
289             protocol_version_identifier = tvb_get_guint8(tvb, BPDU_VERSION_IDENTIFIER);
290             proto_tree_add_uint(bpdu_tree, hf_bpdu_version_id, tvb, 
291                                 BPDU_VERSION_IDENTIFIER, 1, 
292                                 protocol_version_identifier);
293             switch (protocol_version_identifier) {
294               case 0:
295                 break;
296               case 2:
297                 break;
298               default:
299                   proto_tree_add_text(bpdu_tree, tvb, BPDU_VERSION_IDENTIFIER, 1,
300                   "   (Warning: this version of Ethereal only knows about versions 0 & 2)");
301                 break;
302             }
303             proto_tree_add_uint(bpdu_tree, hf_bpdu_type, tvb,
304                                        BPDU_TYPE, 1, 
305                                        bpdu_type);
306
307             if (bpdu_type != BPDU_TYPE_CONF && bpdu_type != BPDU_TYPE_RST) {
308               call_dissector(data_handle,
309                              tvb_new_subset(tvb, BPDU_TYPE + 1, -1, -1),
310                              pinfo, tree);
311               return;
312             }
313
314             rstp_bpdu = (bpdu_type == BPDU_TYPE_RST);
315             bridge_identifier_bridge_priority = tvb_get_ntohs(tvb, BPDU_BRIDGE_IDENTIFIER);
316             bridge_identifier_mac = tvb_get_ptr(tvb, BPDU_BRIDGE_IDENTIFIER + 2, 6);
317             bridge_identifier_mac_str = ether_to_str(bridge_identifier_mac);
318
319             flags_item = proto_tree_add_uint(bpdu_tree, hf_bpdu_flags, tvb, 
320                                 BPDU_FLAGS, 1, flags);
321             flags_tree = proto_item_add_subtree(flags_item, ett_bpdu_flags);
322             sep = initial_sep;
323             APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_TCACK, flags_item,
324                                 "%sTopology Change Acknowledgment");
325             proto_tree_add_boolean(flags_tree, hf_bpdu_flags_tcack, tvb,
326                                 BPDU_FLAGS, 1, flags);
327             if (rstp_bpdu) {
328               APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_AGREEMENT, flags_item,
329                                   "%sAgreement");
330               proto_tree_add_boolean(flags_tree, hf_bpdu_flags_agreement, tvb,
331                                   BPDU_FLAGS, 1, flags);
332               APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_FORWARDING, flags_item,
333                                   "%sForwarding");
334               proto_tree_add_boolean(flags_tree, hf_bpdu_flags_forwarding, tvb,
335                                   BPDU_FLAGS, 1, flags);
336               APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_LEARNING, flags_item,
337                                   "%sLearning");
338               proto_tree_add_boolean(flags_tree, hf_bpdu_flags_learning, tvb,
339                                   BPDU_FLAGS, 1, flags);
340               if (flags_item) {
341                 guint8 port_role;
342
343                 port_role = (flags & BPDU_FLAGS_PORT_ROLE_MASK) >> BPDU_FLAGS_PORT_ROLE_SHIFT;
344                 proto_item_append_text(flags_item, "%sPort Role: %s", sep,
345                                        val_to_str(port_role, role_vals,
346                                                   "Unknown (%u)"));
347               }
348               sep = cont_sep;
349               proto_tree_add_uint(flags_tree, hf_bpdu_flags_port_role, tvb,
350                                   BPDU_FLAGS, 1, flags);
351               APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_PROPOSAL, flags_item,
352                                   "%sProposal");
353               proto_tree_add_boolean(flags_tree, hf_bpdu_flags_proposal, tvb,
354                                   BPDU_FLAGS, 1, flags);
355             }
356             APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_TC, flags_item,
357                                 "%sTopology Change");
358             proto_tree_add_boolean(flags_tree, hf_bpdu_flags_tc, tvb,
359                                 BPDU_FLAGS, 1, flags);
360             if (sep != initial_sep) {
361               /* We put something in; put in the terminating ")" */
362               proto_item_append_text(flags_item, ")");
363             }
364
365             proto_tree_add_ether_hidden(bpdu_tree, hf_bpdu_root_mac, tvb,
366                                        BPDU_ROOT_IDENTIFIER + 2, 6,
367                                        root_identifier_mac);
368             proto_tree_add_text(bpdu_tree, tvb, 
369                                 BPDU_ROOT_IDENTIFIER, 8, 
370                                 "Root Identifier: %d / %s", 
371                                 root_identifier_bridge_priority, 
372                                 root_identifier_mac_str);
373             proto_tree_add_uint(bpdu_tree, hf_bpdu_root_cost, tvb, 
374                                 BPDU_ROOT_PATH_COST, 4, 
375                                 root_path_cost);
376             proto_tree_add_text(bpdu_tree, tvb, 
377                                 BPDU_BRIDGE_IDENTIFIER, 8, 
378                                 "Bridge Identifier: %d / %s", 
379                                 bridge_identifier_bridge_priority, 
380                                 bridge_identifier_mac_str);
381             proto_tree_add_ether_hidden(bpdu_tree, hf_bpdu_bridge_mac, tvb,
382                                        BPDU_BRIDGE_IDENTIFIER + 2, 6,
383                                        bridge_identifier_mac);
384             proto_tree_add_uint(bpdu_tree, hf_bpdu_port_id, tvb,
385                                 BPDU_PORT_IDENTIFIER, 2, 
386                                 port_identifier);
387             message_age = tvb_get_ntohs(tvb, BPDU_MESSAGE_AGE) / 256.0;
388             proto_tree_add_double(bpdu_tree, hf_bpdu_msg_age, tvb,
389                                 BPDU_MESSAGE_AGE, 2, 
390                                 message_age);
391             max_age = tvb_get_ntohs(tvb, BPDU_MAX_AGE) / 256.0;
392             proto_tree_add_double(bpdu_tree, hf_bpdu_max_age, tvb,
393                                 BPDU_MAX_AGE, 2, 
394                                 max_age);
395             hello_time = tvb_get_ntohs(tvb, BPDU_HELLO_TIME) / 256.0;
396             proto_tree_add_double(bpdu_tree, hf_bpdu_hello_time, tvb,
397                                 BPDU_HELLO_TIME, 2, 
398                                 hello_time);
399             forward_delay = tvb_get_ntohs(tvb, BPDU_FORWARD_DELAY) / 256.0;
400             proto_tree_add_double(bpdu_tree, hf_bpdu_forward_delay, tvb,
401                                 BPDU_FORWARD_DELAY, 2, 
402                                 forward_delay);
403             if (rstp_bpdu) {
404               proto_tree_add_item(bpdu_tree, hf_bpdu_version_1_length, tvb,
405                                 BPDU_VERSION_1_LENGTH, 1, FALSE);
406             }
407       }
408 }
409
410 static const true_false_string yesno = {  
411         "Yes",
412         "No"
413 };
414
415 void
416 proto_register_bpdu(void)
417 {
418
419   static hf_register_info hf[] = {
420     { &hf_bpdu_proto_id,
421       { "Protocol Identifier",          "stp.protocol",
422         FT_UINT16,      BASE_HEX,       VALS(&protocol_id_vals), 0x0,
423         "", HFILL }},
424     { &hf_bpdu_version_id,
425       { "Protocol Version Identifier",  "stp.version",
426         FT_UINT8,       BASE_DEC,       NULL,   0x0,
427         "", HFILL }},
428     { &hf_bpdu_type,
429       { "BPDU Type",                    "stp.type",
430         FT_UINT8,       BASE_HEX,       VALS(&bpdu_type_vals),  0x0,
431         "", HFILL }},
432     { &hf_bpdu_flags,
433       { "BPDU flags",                   "stp.flags",
434         FT_UINT8,       BASE_HEX,       NULL,   0x0,
435         "", HFILL }},
436     { &hf_bpdu_flags_tcack,
437       { "Topology Change Acknowledgment",  "stp.flags.tcack",
438         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_TCACK,
439         "", HFILL }},
440     { &hf_bpdu_flags_agreement,
441       { "Agreement",                    "stp.flags.agreement",
442         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_AGREEMENT,
443         "", HFILL }},
444     { &hf_bpdu_flags_forwarding,
445       { "Forwarding",                   "stp.flags.forwarding",
446         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_FORWARDING,
447         "", HFILL }},
448     { &hf_bpdu_flags_learning,
449       { "Learning",                     "stp.flags.learning",
450         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_LEARNING,
451         "", HFILL }},
452     { &hf_bpdu_flags_port_role,
453       { "Port Role",                    "stp.flags.port_role",
454         FT_UINT8,       BASE_DEC,       VALS(role_vals),        BPDU_FLAGS_PORT_ROLE_MASK,
455         "", HFILL }},
456     { &hf_bpdu_flags_proposal,
457       { "Proposal",                     "stp.flags.proposal",
458         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_PROPOSAL,
459         "", HFILL }},
460     { &hf_bpdu_flags_tc,
461       { "Topology Change",              "stp.flags.tc",
462         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_TC,
463         "", HFILL }},
464     { &hf_bpdu_root_mac,
465       { "Root Identifier",              "stp.root.hw",
466         FT_ETHER,       BASE_NONE,      NULL,   0x0,
467         "", HFILL }},
468     { &hf_bpdu_root_cost,
469       { "Root Path Cost",               "stp.root.cost",
470         FT_UINT32,      BASE_DEC,       NULL,   0x0,
471         "", HFILL }},
472     { &hf_bpdu_bridge_mac,
473       { "Bridge Identifier",            "stp.bridge.hw",
474         FT_ETHER,       BASE_NONE,      NULL,   0x0,
475         "", HFILL }},
476     { &hf_bpdu_port_id,
477       { "Port identifier",              "stp.port",
478         FT_UINT16,      BASE_HEX,       NULL,   0x0,
479         "", HFILL }},
480     { &hf_bpdu_msg_age,
481       { "Message Age",                  "stp.msg_age",
482         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
483         "", HFILL }},
484     { &hf_bpdu_max_age,
485       { "Max Age",                      "stp.max_age",
486         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
487         "", HFILL }},
488     { &hf_bpdu_hello_time,
489       { "Hello Time",                   "stp.hello",
490         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
491         "", HFILL }},
492     { &hf_bpdu_forward_delay,
493       { "Forward Delay",                "stp.forward",
494         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
495         "", HFILL }},
496     { &hf_bpdu_version_1_length,
497       { "Version 1 Length",             "stp.version_1_length",
498         FT_UINT8,       BASE_DEC,       NULL,   0x0,
499         "", HFILL }},
500   };
501   static gint *ett[] = {
502     &ett_bpdu,
503     &ett_bpdu_flags,
504   };
505
506   proto_bpdu = proto_register_protocol("Spanning Tree Protocol", "STP", "stp");
507   proto_register_field_array(proto_bpdu, hf, array_length(hf));
508   proto_register_subtree_array(ett, array_length(ett));
509
510   register_dissector("bpdu", dissect_bpdu, proto_bpdu);
511 }
512
513 void
514 proto_reg_handoff_bpdu(void)
515 {
516   dissector_handle_t bpdu_handle;
517
518   /*
519    * Get handle for the GVRP dissector.
520    */
521   gvrp_handle = find_dissector("gvrp");
522   
523   /*
524    * Get handle for the GMRP dissector.
525    */
526   gmrp_handle = find_dissector("gmrp");
527   data_handle = find_dissector("data");
528
529   bpdu_handle = find_dissector("bpdu");
530   dissector_add("llc.dsap", SAP_BPDU, bpdu_handle);
531   dissector_add("ppp.protocol", PPP_BPDU, bpdu_handle);
532 }