Define some fcns & vars as static...
[metze/wireshark/wip.git] / epan / dissectors / packet-gvrp.c
1 /* packet-gvrp.c
2  * Routines for GVRP (GARP VLAN Registration Protocol) dissection
3  * Copyright 2000, Kevin Shi <techishi@ms22.hinet.net>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdlib.h>
31
32 #include <glib.h>
33
34 #include <epan/packet.h>
35 #include <epan/llcsaps.h>
36
37 /* Initialize the protocol and registered fields */
38 static int proto_gvrp = -1;
39 static int hf_gvrp_proto_id = -1;
40 static int hf_gvrp_attribute_type = -1;
41 static int hf_gvrp_attribute_length = -1;
42 static int hf_gvrp_attribute_event = -1;
43 static int hf_gvrp_attribute_value = -1;
44 /*static int hf_gvrp_end_of_mark = -1;*/
45
46 /* Initialize the subtree pointers */
47 static gint ett_gvrp = -1;
48 /*static gint ett_gvrp_message = -1;
49 static gint ett_gvrp_attribute_list = -1;
50 static gint ett_gvrp_attribute = -1;*/
51
52 static dissector_handle_t data_handle;
53
54 /* Constant definitions */
55 #define GARP_DEFAULT_PROTOCOL_ID        0x0001
56 #define GARP_END_OF_MARK                0x00
57
58 #define GVRP_ATTRIBUTE_TYPE             0x01
59
60 static const value_string attribute_type_vals[] = {
61         { GVRP_ATTRIBUTE_TYPE, "VID" },
62         { 0,                   NULL }
63 };
64
65 /* The length of GVRP LeaveAll attribute should be 2 octets (one for length
66  * and the other for event) */
67 #define GVRP_LENGTH_LEAVEALL            (sizeof(guint8)+sizeof(guint8))
68
69 /* The length of GVRP attribute other than LeaveAll should be 4 octets (one
70  * for length, one for event, and the last two for VID value).
71  */
72 #define GVRP_LENGTH_NON_LEAVEALL        (sizeof(guint8)+sizeof(guint8)+sizeof(guint16))
73
74 /* Packet offset definitions */
75 #define GARP_PROTOCOL_ID                0
76
77 /* Event definitions */
78 #define GVRP_EVENT_LEAVEALL             0
79 #define GVRP_EVENT_JOINEMPTY            1
80 #define GVRP_EVENT_JOININ               2
81 #define GVRP_EVENT_LEAVEEMPTY           3
82 #define GVRP_EVENT_LEAVEIN              4
83 #define GVRP_EVENT_EMPTY                5
84
85 static const value_string event_vals[] = {
86         { GVRP_EVENT_LEAVEALL,   "Leave All" },
87         { GVRP_EVENT_JOINEMPTY,  "Join Empty" },
88         { GVRP_EVENT_JOININ,     "Join In" },
89         { GVRP_EVENT_LEAVEEMPTY, "Leave Empty" },
90         { GVRP_EVENT_LEAVEIN,    "Leave In" },
91         { GVRP_EVENT_EMPTY,      "Empty" },
92         { 0,                     NULL }
93 };
94
95 /* Code to actually dissect the packets */
96 static void
97 dissect_gvrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
98 {
99     proto_item   *ti;
100     proto_tree   *gvrp_tree;
101     guint16       protocol_id;
102     guint8        octet;
103     int           msg_index, attr_index, offset = 0, length = tvb_reported_length(tvb);
104
105     col_set_str(pinfo->cinfo, COL_PROTOCOL, "GVRP");
106
107     col_set_str(pinfo->cinfo, COL_INFO, "GVRP");
108
109     if (tree)
110     {
111         ti = proto_tree_add_item(tree, proto_gvrp, tvb, 0, length, FALSE);
112
113         gvrp_tree = proto_item_add_subtree(ti, ett_gvrp);
114
115         /* Read in GARP protocol ID */
116         protocol_id = tvb_get_ntohs(tvb, GARP_PROTOCOL_ID);
117
118         proto_tree_add_uint_format(gvrp_tree, hf_gvrp_proto_id, tvb,
119                                    GARP_PROTOCOL_ID, sizeof(guint16),
120                                    protocol_id,
121                                    "Protocol Identifier: 0x%04x (%s)",
122                                    protocol_id,
123                                    protocol_id == GARP_DEFAULT_PROTOCOL_ID ?
124                                      "GARP VLAN Registration Protocol" :
125                                      "Unknown Protocol");
126
127         /* Currently only one protocol ID is supported */
128         if (protocol_id != GARP_DEFAULT_PROTOCOL_ID)
129         {
130             proto_tree_add_text(gvrp_tree, tvb, GARP_PROTOCOL_ID, sizeof(guint16),
131  "   (Warning: this version of Wireshark only knows about protocol id = 1)");
132             call_dissector(data_handle,
133                 tvb_new_subset(tvb, GARP_PROTOCOL_ID + sizeof(guint16), -1, -1),
134                 pinfo, tree);
135             return;
136         }
137
138         offset += sizeof(guint16);
139         length -= sizeof(guint16);
140
141         msg_index = 0;
142
143         /* Begin to parse GARP messages */
144         while (length)
145         {
146             proto_item   *msg_item;
147             int           msg_start = offset;
148
149             /* Read in attribute type. */
150             octet = tvb_get_guint8(tvb, offset);
151
152             /* Check for end of mark */
153             if (octet == GARP_END_OF_MARK)
154             {
155                 /* End of GARP PDU */
156                 if (msg_index)
157                 {
158                     proto_tree_add_text(gvrp_tree, tvb, offset, sizeof(guint8),
159                                         "End of mark");
160                     break;
161                 }
162                 else
163                 {
164                     call_dissector(data_handle,
165                         tvb_new_subset_remaining(tvb, offset), pinfo, tree);
166                     return;
167                 }
168             }
169
170             offset += sizeof(guint8);
171             length -= sizeof(guint8);
172
173             msg_item = proto_tree_add_text(gvrp_tree, tvb, msg_start, -1,
174                                            "Message %d", msg_index + 1);
175
176             proto_tree_add_uint(gvrp_tree, hf_gvrp_attribute_type, tvb,
177                                 msg_start, sizeof(guint8), octet);
178
179             /* GVRP only supports one attribute type. */
180             if (octet != GVRP_ATTRIBUTE_TYPE)
181             {
182                 call_dissector(data_handle, tvb_new_subset_remaining(tvb, offset),
183                     pinfo, tree);
184                 return;
185             }
186
187             attr_index = 0;
188
189             while (length)
190             {
191                 int          attr_start = offset;
192                 proto_item   *attr_item;
193
194                 /* Read in attribute length. */
195                 octet = tvb_get_guint8(tvb, offset);
196
197                 /* Check for end of mark */
198                 if (octet == GARP_END_OF_MARK)
199                 {
200                     /* If at least one message has been already read,
201                      * check for another end of mark.
202                      */
203                     if (attr_index)
204                     {
205                         proto_tree_add_text(gvrp_tree, tvb, offset,
206                                             sizeof(guint8), "  End of mark");
207
208                         offset += sizeof(guint8);
209                         length -= sizeof(guint8);
210
211                         proto_item_set_len(msg_item, offset - msg_start);
212                         break;
213                     }
214                     else
215                     {
216                         call_dissector(data_handle,
217                             tvb_new_subset_remaining(tvb, offset), pinfo, tree);
218                         return;
219                     }
220                 }
221                 else
222                 {
223                     guint8   event;
224
225                     offset += sizeof(guint8);
226                     length -= sizeof(guint8);
227
228                     attr_item = proto_tree_add_text(gvrp_tree, tvb,
229                          attr_start, -1, "  Attribute %d", attr_index + 1);
230
231                     proto_tree_add_uint(gvrp_tree, hf_gvrp_attribute_length,
232                          tvb, attr_start, sizeof(guint8), octet);
233
234                     /* Read in attribute event */
235                     event = tvb_get_guint8(tvb, offset);
236
237                     proto_tree_add_uint(gvrp_tree, hf_gvrp_attribute_event,
238                          tvb, offset, sizeof(guint8), event);
239
240                     offset += sizeof(guint8);
241                     length -= sizeof(guint8);
242
243                     switch (event) {
244
245                     case GVRP_EVENT_LEAVEALL:
246                         if (octet != GVRP_LENGTH_LEAVEALL)
247                         {
248                             call_dissector(data_handle,
249                                 tvb_new_subset_remaining(tvb, offset), pinfo,
250                                 tree);
251                             return;
252                         }
253                         break;
254
255                      case GVRP_EVENT_JOINEMPTY:
256                      case GVRP_EVENT_JOININ:
257                      case GVRP_EVENT_LEAVEEMPTY:
258                      case GVRP_EVENT_LEAVEIN:
259                      case GVRP_EVENT_EMPTY:
260                         if (octet != GVRP_LENGTH_NON_LEAVEALL)
261                         {
262                             call_dissector(data_handle,
263                                 tvb_new_subset_remaining(tvb, offset),pinfo,
264                                 tree);
265                             return;
266                         }
267
268                         /* Show attribute value */
269                         proto_tree_add_item(gvrp_tree, hf_gvrp_attribute_value,
270                             tvb, offset, sizeof(guint16), FALSE);
271
272                         offset += sizeof(guint16);
273                         length -= sizeof(guint16);
274                         break;
275
276                      default:
277                         call_dissector(data_handle,
278                             tvb_new_subset_remaining(tvb, offset), pinfo, tree);
279                         return;
280                     }
281                 }
282
283                 proto_item_set_len(attr_item, offset - attr_start);
284
285                 attr_index++;
286             }
287
288             msg_index++;
289         }
290     }
291 }
292
293
294 /* Register the protocol with Wireshark */
295 void
296 proto_register_gvrp(void)
297 {
298     static hf_register_info hf[] = {
299         { &hf_gvrp_proto_id,
300             { "Protocol ID", "gvrp.protocol_id",
301             FT_UINT16,      BASE_HEX,      NULL,  0x0,
302             NULL, HFILL }
303         },
304         { &hf_gvrp_attribute_type,
305             { "Type",        "gvrp.attribute_type",
306             FT_UINT8,        BASE_HEX,      VALS(attribute_type_vals),  0x0,
307             NULL, HFILL }
308         },
309         { &hf_gvrp_attribute_length,
310             { "Length",      "gvrp.attribute_length",
311             FT_UINT8,        BASE_DEC,      NULL,  0x0,
312             NULL, HFILL }
313         },
314         { &hf_gvrp_attribute_event,
315             { "Event",       "gvrp.attribute_event",
316             FT_UINT8,        BASE_DEC,      VALS(event_vals),  0x0,
317             NULL, HFILL }
318         },
319         { &hf_gvrp_attribute_value,
320             { "Value",       "gvrp.attribute_value",
321             FT_UINT16,       BASE_DEC,      NULL,  0x0,
322             NULL, HFILL }
323         }
324     };
325
326     static gint *ett[] = {
327         &ett_gvrp
328     };
329
330     /* Register the protocol name and description for GVRP */
331     proto_gvrp = proto_register_protocol("GARP VLAN Registration Protocol",
332                                          "GVRP", "gvrp");
333
334     /* Required function calls to register the header fields and subtrees
335      * used by GVRP */
336     proto_register_field_array(proto_gvrp, hf, array_length(hf));
337     proto_register_subtree_array(ett, array_length(ett));
338
339     register_dissector("gvrp", dissect_gvrp, proto_gvrp);
340 }
341
342 void
343 proto_reg_handoff_gvrp(void){
344   data_handle = find_dissector("data");
345 }