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