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