epan/dissectors/pidl/ C99 drsuapi
[wireshark-sm.git] / epan / dissectors / packet-gmrp.c
bloba695209d839a2389234815294863003f7a3a269e
1 /* packet-gmrp.c
2 * Routines for GMRP (GARP Multicast Registration Protocol) dissection
3 * Copyright 2001, Markus Seehofer <mseehofe@nt.hirschmann.de>
5 * Based on the code from packet-gvrp.c (GVRP) from
6 * Kevin Shi <techishi@ms22.hinet.net> Copyright 2000
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * SPDX-License-Identifier: GPL-2.0-or-later
15 #include "config.h"
17 #include <epan/packet.h>
18 #include <epan/expert.h>
19 #include <epan/llcsaps.h>
21 void proto_register_gmrp(void);
23 /* Initialize the protocol and registered fields */
24 static int proto_gmrp;
25 static int hf_gmrp_proto_id;
26 static int hf_gmrp_attribute_type;
27 static int hf_gmrp_attribute_length;
28 static int hf_gmrp_attribute_event;
29 static int hf_gmrp_attribute_value_group_membership;
30 static int hf_gmrp_attribute_value_service_requirement;
31 static int hf_gmrp_end_of_mark;
33 /* Initialize the subtree pointers */
34 static int ett_gmrp;
35 static int ett_gmrp_message;
36 static int ett_gmrp_attribute_list;
37 /*static int ett_gmrp_attribute;*/
39 static expert_field ei_gmrp_proto_id;
42 /* Constant definitions */
43 #define GARP_DEFAULT_PROTOCOL_ID 0x0001
44 #define GARP_END_OF_MARK 0x00
46 #define GMRP_ATTRIBUTE_TYPE_GROUP_MEMBERSHIP 0x01
47 #define GMRP_ATTRIBUTE_TYPE_SERVICE_REQUIREMENT 0x02
49 #define GMRP_SERVICE_REQUIREMENT_FORWARD_ALL 0x00
50 #define GMRP_SERVICE_REQUIREMENT_FORWARD_ALL_UNREGISTERED 0x01
52 static const value_string attribute_type_vals[] = {
53 { GMRP_ATTRIBUTE_TYPE_GROUP_MEMBERSHIP ,"Group Membership" },
54 { GMRP_ATTRIBUTE_TYPE_SERVICE_REQUIREMENT ,"Service Requirement" },
55 { 0, NULL }
58 /* The length of GMRP LeaveAll attribute should be 2 octets (one for length
59 * and the other for event) */
60 #define GMRP_LENGTH_LEAVEALL (int)(sizeof(uint8_t)+sizeof(uint8_t))
62 /* The length of GMRP attribute other than LeaveAll should be:
64 * 8 bytes for Group Membership (1 for length, 1 for event and 6 for mac address to register)
65 * or
66 * 3 bytes for Service Requirement (1 for length, 1 for event, 1 for attribute value)
69 #define GMRP_GROUP_MEMBERSHIP_NON_LEAVEALL (int)(sizeof(uint8_t)+sizeof(uint8_t)+(6*sizeof(uint8_t)))
70 #define GMRP_SERVICE_REQUIREMENT_NON_LEAVEALL (int)(sizeof(uint8_t)+sizeof(uint8_t)+sizeof(uint8_t))
72 /* Packet offset definitions */
73 #define GARP_PROTOCOL_ID 0
75 /* Event definitions */
76 #define GMRP_EVENT_LEAVEALL 0
77 #define GMRP_EVENT_JOINEMPTY 1
78 #define GMRP_EVENT_JOININ 2
79 #define GMRP_EVENT_LEAVEEMPTY 3
80 #define GMRP_EVENT_LEAVEIN 4
81 #define GMRP_EVENT_EMPTY 5
83 static const value_string event_vals[] = {
84 { GMRP_EVENT_LEAVEALL, "Leave All" },
85 { GMRP_EVENT_JOINEMPTY, "Join Empty" },
86 { GMRP_EVENT_JOININ, "Join In" },
87 { GMRP_EVENT_LEAVEEMPTY, "Leave Empty" },
88 { GMRP_EVENT_LEAVEIN, "Leave In" },
89 { GMRP_EVENT_EMPTY, "Empty" },
90 { 0, NULL }
94 /* Code to actually dissect the packets */
95 static int
96 dissect_gmrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
98 proto_item *ti;
99 proto_tree *gmrp_tree, *msg_tree, *attr_tree;
100 uint16_t protocol_id;
101 uint8_t octet;
102 uint8_t attribute_type;
103 int msg_index, attr_index, offset = 0, length = tvb_reported_length(tvb);
105 col_set_str(pinfo->cinfo, COL_PROTOCOL, "GMRP");
107 col_set_str(pinfo->cinfo, COL_INFO, "GMRP");
109 ti = proto_tree_add_item(tree, proto_gmrp, tvb, 0, -1, ENC_NA);
111 gmrp_tree = proto_item_add_subtree(ti, ett_gmrp);
113 /* Read in GARP protocol ID */
114 protocol_id = tvb_get_ntohs(tvb, GARP_PROTOCOL_ID);
116 ti = proto_tree_add_uint_format_value(gmrp_tree, hf_gmrp_proto_id, tvb,
117 GARP_PROTOCOL_ID, 2, protocol_id,
118 "0x%04x (%s)", protocol_id,
119 protocol_id == GARP_DEFAULT_PROTOCOL_ID ?
120 "GARP Multicast Registration Protocol" :
121 "Unknown Protocol");
123 /* Currently only one protocol ID is supported */
124 if (protocol_id != GARP_DEFAULT_PROTOCOL_ID)
126 expert_add_info(pinfo, ti, &ei_gmrp_proto_id);
127 call_data_dissector(tvb_new_subset_remaining(tvb, GARP_PROTOCOL_ID + 2),
128 pinfo, tree);
129 return tvb_captured_length(tvb);
132 offset += 2;
133 length -= 2;
135 msg_index = 0;
137 /* Begin to parse GARP messages */
138 while (length)
140 proto_item *msg_item;
141 int msg_start = offset;
143 /* Read in attribute type. */
144 attribute_type = octet = tvb_get_uint8(tvb, offset);
146 /* Check for end of mark */
147 if (octet == GARP_END_OF_MARK)
149 /* End of GARP PDU */
150 if (msg_index)
152 proto_tree_add_uint_format(gmrp_tree, hf_gmrp_end_of_mark, tvb, offset, 1, octet, "End of pdu");
153 break;
155 else
157 call_data_dissector(tvb_new_subset_remaining(tvb, offset),
158 pinfo, tree);
159 return tvb_captured_length(tvb);
163 offset += 1;
164 length -= 1;
166 msg_tree = proto_tree_add_subtree_format(gmrp_tree, tvb, msg_start, -1,
167 ett_gmrp_message, &msg_item, "Message %d", msg_index + 1);
169 proto_tree_add_uint(msg_tree, hf_gmrp_attribute_type, tvb,
170 msg_start, 1, octet);
172 /* GMRP supports Group Membership and Service Requirement as attribute types */
173 if ( (octet != GMRP_ATTRIBUTE_TYPE_GROUP_MEMBERSHIP) && (octet != GMRP_ATTRIBUTE_TYPE_SERVICE_REQUIREMENT) )
175 call_data_dissector(tvb_new_subset_remaining(tvb, offset), pinfo,
176 tree);
177 return tvb_captured_length(tvb);
180 attr_index = 0;
182 while (length)
184 int attr_start = offset;
185 proto_item *attr_item;
187 /* Read in attribute length. */
188 octet = tvb_get_uint8(tvb, offset);
190 /* Check for end of mark */
191 if (octet == GARP_END_OF_MARK)
193 /* If at least one message has been already read,
194 * check for another end of mark.
196 if (attr_index)
198 proto_tree_add_uint_format(msg_tree, hf_gmrp_end_of_mark, tvb, offset,
199 1, octet, " End of mark");
201 offset += 1;
202 length -= 1;
204 proto_item_set_len(msg_item, offset - msg_start);
205 break;
207 else
209 call_data_dissector(tvb_new_subset_remaining(tvb, offset),
210 pinfo, tree);
211 return tvb_captured_length(tvb);
214 else
216 uint8_t event;
218 offset += 1;
219 length -= 1;
221 attr_tree = proto_tree_add_subtree_format(msg_tree, tvb, attr_start, -1,
222 ett_gmrp_attribute_list, &attr_item, " Attribute %d", attr_index + 1);
224 proto_tree_add_uint(attr_tree, hf_gmrp_attribute_length,
225 tvb, attr_start, 1, octet);
227 /* Read in attribute event */
228 event = tvb_get_uint8(tvb, offset);
230 proto_tree_add_uint(attr_tree, hf_gmrp_attribute_event,
231 tvb, offset, 1, event);
233 offset += 1;
234 length -= 1;
236 switch (event) {
238 case GMRP_EVENT_LEAVEALL:
239 if (octet != GMRP_LENGTH_LEAVEALL)
241 call_data_dissector(tvb_new_subset_remaining(tvb, offset),
242 pinfo, tree);
243 return tvb_captured_length(tvb);
245 break;
247 case GMRP_EVENT_JOINEMPTY:
248 case GMRP_EVENT_JOININ:
249 case GMRP_EVENT_LEAVEEMPTY:
250 case GMRP_EVENT_LEAVEIN:
251 case GMRP_EVENT_EMPTY:
252 if ( (octet != GMRP_GROUP_MEMBERSHIP_NON_LEAVEALL) &&
253 (octet != GMRP_SERVICE_REQUIREMENT_NON_LEAVEALL) )
255 call_data_dissector(tvb_new_subset_remaining(tvb, offset),
256 pinfo, tree);
257 return tvb_captured_length(tvb);
260 /* Show attribute value */
262 if ( GMRP_ATTRIBUTE_TYPE_GROUP_MEMBERSHIP == attribute_type )
264 /* Group Membership */
265 proto_tree_add_item(attr_tree, hf_gmrp_attribute_value_group_membership,
266 tvb, offset, 6, ENC_NA);
268 offset += 6;
269 length -= 6;
271 else
272 if ( GMRP_ATTRIBUTE_TYPE_SERVICE_REQUIREMENT == attribute_type )
274 /* Service Requirement */
275 proto_tree_add_item(attr_tree, hf_gmrp_attribute_value_service_requirement,
276 tvb, offset, 1, ENC_BIG_ENDIAN);
278 offset += 1;
279 length -= 1;
281 else
283 call_data_dissector(tvb_new_subset_remaining(tvb, offset),
284 pinfo, tree);
285 return tvb_captured_length(tvb);
288 break;
290 default:
291 call_data_dissector(tvb_new_subset_remaining(tvb, offset),
292 pinfo, tree);
293 return tvb_captured_length(tvb);
297 proto_item_set_len(attr_item, offset - attr_start);
299 attr_index++;
302 msg_index++;
304 return tvb_captured_length(tvb);
309 /* Register the protocol with Wireshark */
310 void
311 proto_register_gmrp(void)
313 static hf_register_info hf[] = {
314 { &hf_gmrp_proto_id,
315 { "Protocol Identifier", "gmrp.protocol_id",
316 FT_UINT16, BASE_HEX, NULL, 0x0,
317 NULL , HFILL }
319 { &hf_gmrp_attribute_type,
320 { "Type", "gmrp.attribute_type",
321 FT_UINT8, BASE_HEX, VALS(attribute_type_vals), 0x0,
322 NULL , HFILL }
324 { &hf_gmrp_attribute_length,
325 { "Length", "gmrp.attribute_length",
326 FT_UINT8, BASE_DEC, NULL, 0x0,
327 NULL , HFILL }
329 { &hf_gmrp_attribute_event,
330 { "Event", "gmrp.attribute_event",
331 FT_UINT8, BASE_DEC, VALS(event_vals), 0x0,
332 NULL , HFILL }
334 { &hf_gmrp_attribute_value_group_membership,
335 { "Value", "gmrp.attribute_value_group_membership",
336 FT_ETHER, BASE_NONE, NULL, 0x0,
337 NULL , HFILL }
339 { &hf_gmrp_attribute_value_service_requirement,
340 { "Value", "gmrp.attribute_value_service_requirement",
341 FT_UINT8, BASE_HEX, NULL, 0x0,
342 NULL , HFILL }
344 { &hf_gmrp_end_of_mark,
345 { "End of mark", "gmrp.end_of_mark",
346 FT_UINT8, BASE_HEX, NULL, 0x0,
347 NULL , HFILL }
352 static int *ett[] = {
353 &ett_gmrp,
354 &ett_gmrp_message,
355 &ett_gmrp_attribute_list
358 static ei_register_info ei[] = {
359 { &ei_gmrp_proto_id, { "gmrp.protocol_id.not_gmrp", PI_UNDECODED, PI_WARN, "This version of Wireshark only knows about protocol id = 1", EXPFILL }},
362 expert_module_t* expert_gmrp;
364 /* Register the protocol name and description for GMRP */
365 proto_gmrp = proto_register_protocol("GARP Multicast Registration Protocol", "GMRP", "gmrp");
367 /* Required function calls to register the header fields and subtrees
368 * used by GMRP */
369 proto_register_field_array(proto_gmrp, hf, array_length(hf));
370 proto_register_subtree_array(ett, array_length(ett));
371 expert_gmrp = expert_register_protocol(proto_gmrp);
372 expert_register_field_array(expert_gmrp, ei, array_length(ei));
374 register_dissector("gmrp", dissect_gmrp, proto_gmrp);
379 * Editor modelines - https://www.wireshark.org/tools/modelines.html
381 * Local variables:
382 * c-basic-offset: 4
383 * tab-width: 8
384 * indent-tabs-mode: nil
385 * End:
387 * vi: set shiftwidth=4 tabstop=8 expandtab:
388 * :indentSize=4:tabSize=8:noTabs=true: