From Jesper Peterson:
[obnox/wireshark/wip.git] / packet-gmrp.c
1 /* packet-gmrp.c
2  * Routines for GMRP (GARP Multicast Registration Protocol) dissection
3  * Copyright 2001, Markus Seehofer <mseehofe@nt.hirschmann.de>
4  *
5  * Based on the code from packet-gvrp.c (GVRP) from
6  * Kevin Shi <techishi@ms22.hinet.net> Copyright 2000
7  *
8  * $Id: packet-gmrp.c,v 1.10 2002/08/28 21:00:13 jmayer Exp $
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@ethereal.com>
12  * Copyright 1998 Gerald Combs
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <glib.h>
38
39 #include <epan/packet.h>
40 #include "llcsaps.h"
41
42 /* Initialize the protocol and registered fields */
43 static int proto_gmrp = -1;
44 static int hf_gmrp_proto_id = -1;
45 static int hf_gmrp_attribute_type = -1;
46 static int hf_gmrp_attribute_length = -1;
47 static int hf_gmrp_attribute_event = -1;
48 static int hf_gmrp_attribute_value_group_membership = -1;
49 static int hf_gmrp_attribute_value_service_requirement = -1;
50 /*static int hf_gmrp_end_of_mark = -1;*/
51
52 /* Initialize the subtree pointers */
53 static gint ett_gmrp = -1;
54 /*static gint ett_gmrp_message = -1;
55 static gint ett_gmrp_attribute_list = -1;
56 static gint ett_gmrp_attribute = -1;*/
57
58 static dissector_handle_t data_handle;
59
60 /* Constant definitions */
61 #define GARP_DEFAULT_PROTOCOL_ID        0x0001
62 #define GARP_END_OF_MARK                        0x00
63
64 #define GMRP_ATTRIBUTE_TYPE_GROUP_MEMBERSHIP    0x01
65 #define GMRP_ATTRIBUTE_TYPE_SERVICE_REQUIREMENT 0x02
66
67 #define GMRP_SERVICE_REQUIREMENT_FORWARD_ALL                            0x00
68 #define GMRP_SERVICE_REQUIREMENT_FORWARD_ALL_UNREGISTERED       0x01
69
70 static const value_string attribute_type_vals[] = {
71         { GMRP_ATTRIBUTE_TYPE_GROUP_MEMBERSHIP    ,"Group Membership" },
72         { GMRP_ATTRIBUTE_TYPE_SERVICE_REQUIREMENT ,"Service Requirement" },
73         { 0,                   NULL }
74 };
75
76 /* The length of GMRP LeaveAll attribute should be 2 octets (one for length
77  * and the other for event) */
78 #define GMRP_LENGTH_LEAVEALL            (sizeof(guint8)+sizeof(guint8))
79
80 /* The length of GMRP attribute other than LeaveAll should be:
81 *
82 *  8 bytes for Group Membership (1 for length, 1 for event and 6 for mac address to register)
83 *  or
84 *  3 bytes for Service Requirement (1 for length, 1 for event, 1 for attribute value)
85 *
86  */
87 #define GMRP_GROUP_MEMBERSHIP_NON_LEAVEALL              (sizeof(guint8)+sizeof(guint8)+(6*sizeof(guint8)))
88 #define GMRP_SERVICE_REQUIREMENT_NON_LEAVEALL   (sizeof(guint8)+sizeof(guint8)+sizeof(guint8))
89
90 /* Packet offset definitions */
91 #define GARP_PROTOCOL_ID                0
92
93 /* Event definitions */
94 #define GMRP_EVENT_LEAVEALL             0
95 #define GMRP_EVENT_JOINEMPTY    1
96 #define GMRP_EVENT_JOININ               2
97 #define GMRP_EVENT_LEAVEEMPTY   3
98 #define GMRP_EVENT_LEAVEIN              4
99 #define GMRP_EVENT_EMPTY                5
100
101 static const value_string event_vals[] = {
102         { GMRP_EVENT_LEAVEALL,   "Leave All" },
103         { GMRP_EVENT_JOINEMPTY,  "Join Empty" },
104         { GMRP_EVENT_JOININ,     "Join In" },
105         { GMRP_EVENT_LEAVEEMPTY, "Leave Empty" },
106         { GMRP_EVENT_LEAVEIN,    "Leave In" },
107         { GMRP_EVENT_EMPTY,      "Empty" },
108         { 0,                     NULL }
109 };
110
111
112 /* Code to actually dissect the packets */
113 static void
114 dissect_gmrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
115 {
116     proto_item   *ti;
117     proto_tree   *gmrp_tree;
118     guint16       protocol_id;
119     guint8        octet;
120     guint8                attribute_type;
121     int           msg_index, attr_index, offset = 0, length = tvb_reported_length(tvb);
122
123     if (check_col(pinfo->cinfo, COL_PROTOCOL))
124         col_set_str(pinfo->cinfo, COL_PROTOCOL, "GMRP");
125
126     if (check_col(pinfo->cinfo, COL_INFO))
127         col_set_str(pinfo->cinfo, COL_INFO, "GMRP");
128
129     if (tree)
130     {
131         ti = proto_tree_add_item(tree, proto_gmrp, tvb, 0, length, FALSE);
132
133         gmrp_tree = proto_item_add_subtree(ti, ett_gmrp);
134
135         /* Read in GARP protocol ID */
136         protocol_id = tvb_get_ntohs(tvb, GARP_PROTOCOL_ID);
137
138         proto_tree_add_uint_format(gmrp_tree, hf_gmrp_proto_id, tvb,
139                                    GARP_PROTOCOL_ID, sizeof(guint16),
140                                    protocol_id,
141                                    "Protocol Identifier: 0x%04x (%s)",
142                                    protocol_id,
143                                    protocol_id == GARP_DEFAULT_PROTOCOL_ID ?
144                                      "GARP Multicast Registration Protocol" :
145                                      "Unknown Protocol");
146
147         /* Currently only one protocol ID is supported */
148         if (protocol_id != GARP_DEFAULT_PROTOCOL_ID)
149         {
150             proto_tree_add_text(gmrp_tree, tvb, GARP_PROTOCOL_ID, sizeof(guint16),
151  "   (Warning: this version of Ethereal only knows about protocol id = 1)");
152             call_dissector(data_handle,
153                 tvb_new_subset(tvb, GARP_PROTOCOL_ID + sizeof(guint16), -1, -1),
154                 pinfo, tree);
155             return;
156         }
157
158         offset += sizeof(guint16);
159         length -= sizeof(guint16);
160
161         msg_index = 0;
162
163         /* Begin to parse GARP messages */
164         while (length)
165         {
166                         proto_item   *msg_item;
167                         int           msg_start = offset;
168
169                         /* Read in attribute type. */
170                         attribute_type = octet = tvb_get_guint8(tvb, offset);
171
172                         /* Check for end of mark */
173                         if (octet == GARP_END_OF_MARK)
174                         {
175                                 /* End of GARP PDU */
176                                 if (msg_index)
177                                 {
178                                         proto_tree_add_text(gmrp_tree, tvb, offset, sizeof(guint8),
179                                                         "End of pdu");
180                                         break;
181                                 }
182                                 else
183                                 {
184                                         call_dissector(data_handle,
185                                             tvb_new_subset(tvb, offset, -1, -1),
186                                             pinfo, tree);
187                                         return;
188                                 }
189                         }
190
191                         offset += sizeof(guint8);
192                         length -= sizeof(guint8);
193
194                         msg_item = proto_tree_add_text(gmrp_tree, tvb, msg_start, -1,
195                                                    "Message %d", msg_index + 1);
196
197                         proto_tree_add_uint(gmrp_tree, hf_gmrp_attribute_type, tvb,
198                                         msg_start, sizeof(guint8), octet);
199
200                         /* GMRP supports Group Membership and Service Requirement as attribute types */
201                         if ( (octet != GMRP_ATTRIBUTE_TYPE_GROUP_MEMBERSHIP) && (octet != GMRP_ATTRIBUTE_TYPE_SERVICE_REQUIREMENT) )
202                         {
203                                 call_dissector(data_handle,
204                                     tvb_new_subset(tvb, offset, -1, -1), pinfo,
205                                     tree);
206                                 return;
207                         }
208
209                         attr_index = 0;
210
211                         while (length)
212                         {
213                                 int          attr_start = offset;
214                                 proto_item   *attr_item;
215
216                                 /* Read in attribute length. */
217                                 octet = tvb_get_guint8(tvb, offset);
218
219                                 /* Check for end of mark */
220                                 if (octet == GARP_END_OF_MARK)
221                                 {
222                                         /* If at least one message has been already read,
223                                          * check for another end of mark.
224                                          */
225                                         if (attr_index)
226                                         {
227                                                 proto_tree_add_text(gmrp_tree, tvb, offset,
228                                                                         sizeof(guint8), "  End of mark");
229
230                                                 offset += sizeof(guint8);
231                                                 length -= sizeof(guint8);
232
233                                                 proto_item_set_len(msg_item, offset - msg_start);
234                                                 break;
235                                         }
236                                         else
237                                         {
238                                                 call_dissector(data_handle,
239                                                     tvb_new_subset(tvb, offset, -1, -1),
240                                                     pinfo, tree);
241                                                 return;
242                                         }
243                                 }
244                                 else
245                                 {
246                                         guint8   event;
247
248                                         offset += sizeof(guint8);
249                                         length -= sizeof(guint8);
250
251                                         attr_item = proto_tree_add_text(gmrp_tree, tvb,
252                                          attr_start, -1, "  Attribute %d", attr_index + 1);
253
254                                         proto_tree_add_uint(gmrp_tree, hf_gmrp_attribute_length,
255                                          tvb, attr_start, sizeof(guint8), octet);
256
257                                         /* Read in attribute event */
258                                         event = tvb_get_guint8(tvb, offset);
259
260                                         proto_tree_add_uint(gmrp_tree, hf_gmrp_attribute_event,
261                                          tvb, offset, sizeof(guint8), event);
262
263                                         offset += sizeof(guint8);
264                                         length -= sizeof(guint8);
265
266                                         switch (event) {
267
268                                         case GMRP_EVENT_LEAVEALL:
269                                                 if (octet != GMRP_LENGTH_LEAVEALL)
270                                                 {
271                                                         call_dissector(data_handle,
272                                                             tvb_new_subset(tvb, offset, -1, -1),
273                                                             pinfo, tree);
274                                                         return;
275                                                 }
276                                                 break;
277
278                                          case GMRP_EVENT_JOINEMPTY:
279                                          case GMRP_EVENT_JOININ:
280                                          case GMRP_EVENT_LEAVEEMPTY:
281                                          case GMRP_EVENT_LEAVEIN:
282                                          case GMRP_EVENT_EMPTY:
283                                                 if ( (octet != GMRP_GROUP_MEMBERSHIP_NON_LEAVEALL) && (octet != GMRP_SERVICE_REQUIREMENT_NON_LEAVEALL) )
284                                                 {
285                                                         call_dissector(data_handle,
286                                                             tvb_new_subset(tvb, offset, -1, -1),
287                                                             pinfo, tree);
288                                                         return;
289                                                 }
290
291                                         /* Show attribute value */
292
293                                         if ( GMRP_ATTRIBUTE_TYPE_GROUP_MEMBERSHIP == attribute_type )
294                                         {
295                                                 /* Group Membership */
296                                                 proto_tree_add_item(gmrp_tree, hf_gmrp_attribute_value_group_membership,
297                                                         tvb, offset, (6*sizeof(guint8)), FALSE);
298
299                                                 offset += 6*sizeof(guint8);
300                                                 length -= 6*sizeof(guint8);
301                                         }
302                                         else
303                                         if ( GMRP_ATTRIBUTE_TYPE_SERVICE_REQUIREMENT == attribute_type )
304                                         {
305                                                 /* Service Requirement */
306                                                 proto_tree_add_item(gmrp_tree, hf_gmrp_attribute_value_service_requirement,
307                                                         tvb, offset, sizeof(guint8), FALSE);
308
309                                                 offset += sizeof(guint8);
310                                                 length -= sizeof(guint8);
311                                         }
312                                         else
313                                         {
314                                                 call_dissector(data_handle,
315                                                     tvb_new_subset(tvb, offset, -1, -1),
316                                                     pinfo, tree);
317                                                 return;
318                                         }
319
320                                         break;
321
322                                          default:
323                                         call_dissector(data_handle,
324                                             tvb_new_subset(tvb, offset, -1, -1),
325                                             pinfo, tree);
326                                         return;
327                                         }
328                                 }
329
330                                 proto_item_set_len(attr_item, offset - attr_start);
331
332                                 attr_index++;
333                         }
334
335                         msg_index++;
336                 }
337     }
338 }
339
340
341
342 /* Register the protocol with Ethereal */
343 void
344 proto_register_gmrp(void)
345 {
346     static hf_register_info hf[] = {
347         { &hf_gmrp_proto_id,
348             { "Protocol ID", "garp.protocol_id",
349             FT_UINT16,      BASE_HEX,      NULL,  0x0,
350             "" , HFILL }
351         },
352         { &hf_gmrp_attribute_type,
353             { "Type",        "garp.attribute_type",
354             FT_UINT8,        BASE_HEX,      VALS(attribute_type_vals),  0x0,
355             "" , HFILL }
356         },
357         { &hf_gmrp_attribute_length,
358             { "Length",      "garp.attribute_length",
359             FT_UINT8,        BASE_DEC,      NULL,  0x0,
360             "" , HFILL }
361         },
362         { &hf_gmrp_attribute_event,
363             { "Event",       "garp.attribute_event",
364             FT_UINT8,        BASE_DEC,      VALS(event_vals),  0x0,
365             "" , HFILL }
366         },
367         { &hf_gmrp_attribute_value_group_membership,
368             { "Value",       "garp.attribute_value_group_membership",
369             FT_ETHER,        BASE_HEX,      NULL,  0x0,
370             "" , HFILL }
371         },
372         { &hf_gmrp_attribute_value_service_requirement,
373             { "Value",       "garp.attribute_value_service_requirement",
374             FT_UINT8,        BASE_HEX,      NULL,  0x0,
375             "" , HFILL }
376         }
377
378     };
379
380     static gint *ett[] = {
381         &ett_gmrp
382     };
383
384     /* Register the protocol name and description for GMRP */
385     proto_gmrp = proto_register_protocol("GARP Multicast Registration Protocol", "GMRP", "gmrp");
386
387     /* Required function calls to register the header fields and subtrees
388      * used by GMRP */
389     proto_register_field_array(proto_gmrp, hf, array_length(hf));
390     proto_register_subtree_array(ett, array_length(ett));
391
392     register_dissector("gmrp", dissect_gmrp, proto_gmrp);
393
394 }
395
396 void
397 proto_reg_handoff_gmrp(void){
398   data_handle = find_dissector("data");
399 }