Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-mrp-mmrp.c
blob75ebb92bd3105b4f3236713ffbdce8dc3c86000c
1 /* packet-mrp_mmrp.c
2 * Routines for MMRP (MRP Multiple Mac Registration Protocol) dissection
3 * Copyright 2011, Johannes Jochen <johannes.jochen[AT]belden.com>
6 * Based on the code from packet-mrp-msrp.c (MSRP) from
7 * Torrey Atcitty <tatcitty[AT]harman.com> and Craig Gunther <craig.gunther[AT]harman.com>
8 * Copyright 2010
10 * Wireshark - Network traffic analyzer
11 * By Gerald Combs <gerald[AT]wireshark.org>
12 * Copyright 1998 Gerald Combs
14 * SPDX-License-Identifier: GPL-2.0-or-later
16 * The MMRP Protocol specification can be found at the following:
17 * http://standards.ieee.org/about/get/802/802.1.html
21 #include "config.h"
23 #include <epan/packet.h>
24 #include <epan/etypes.h>
26 void proto_register_mrp_mmrp(void);
27 void proto_reg_handoff_mrp_mmrp(void);
29 static dissector_handle_t mmrp_handle;
31 /* MMRP End Mark Sequence */
32 #define MMRP_END_MARK 0x0000
34 /**********************************************************/
35 /* Offsets of fields within an MMRP packet */
36 /**********************************************************/
37 #define MMRP_PROTOCOL_VERSION_OFFSET 0
39 /* Next comes the MMRP Message group */
40 #define MMRP_MESSAGE_GROUP_OFFSET (MMRP_PROTOCOL_VERSION_OFFSET + 1) /* Message is a group of fields */
41 #define MMRP_ATTRIBUTE_TYPE_OFFSET (MMRP_MESSAGE_GROUP_OFFSET)
42 #define MMRP_ATTRIBUTE_LENGTH_OFFSET (MMRP_ATTRIBUTE_TYPE_OFFSET + 1)
44 /* Next comes the MMRP AttributeList group */
45 #define MMRP_ATTRIBUTE_LIST_GROUP_OFFSET (MMRP_ATTRIBUTE_LENGTH_OFFSET + 1) /* AttributeList is a group of fields */
47 /* Next comes the MMRP VectorAttribute group */
48 #define MMRP_VECTOR_ATTRIBUTE_GROUP_OFFSET (MMRP_ATTRIBUTE_LIST_GROUP_OFFSET) /* VectorAttribute is a group of fields */
49 #define MMRP_VECTOR_HEADER_OFFSET (MMRP_VECTOR_ATTRIBUTE_GROUP_OFFSET) /* contains the following two fields */
50 #define MMRP_LEAVE_ALL_EVENT_OFFSET (MMRP_VECTOR_HEADER_OFFSET)
51 #define MMRP_LEAVE_ALL_EVENT_MASK 0xE000
52 #define MMRP_NUMBER_OF_VALUES_OFFSET (MMRP_VECTOR_HEADER_OFFSET)
53 #define MMRP_NUMBER_OF_VALUES_MASK 0x1fff
55 /* Next comes the MMRP FirstValue group */
56 #define MMRP_FIRST_VALUE_GROUP_OFFSET (MMRP_VECTOR_HEADER_OFFSET + 2) /* FirstValue is a group of fields */
58 #define MMRP_SERVICE_THREE_PACKED_OFFSET (MMRP_FIRST_VALUE_GROUP_OFFSET + 1)
59 #define MMRP_MAC_THREE_PACKED_OFFSET (MMRP_FIRST_VALUE_GROUP_OFFSET + 6)
61 /**********************************************************/
62 /* Valid field contents */
63 /**********************************************************/
65 /* Attribute Type definitions */
66 #define MMRP_ATTRIBUTE_TYPE_SERVICE 0x01
67 #define MMRP_ATTRIBUTE_TYPE_MAC 0x02
68 static const value_string attribute_type_vals[] = {
69 { MMRP_ATTRIBUTE_TYPE_SERVICE, "Service Requirement" },
70 { MMRP_ATTRIBUTE_TYPE_MAC, "MAC" },
71 { 0, NULL }
74 /* Leave All Event definitions */
75 #define MMRP_NULLLEAVEALL 0
76 #define MMRP_LEAVEALL 1
77 static const value_string leave_all_vals[] = {
78 { MMRP_NULLLEAVEALL, "Null" },
79 { MMRP_LEAVEALL, "Leave All" },
80 { 0, NULL }
83 /* Three Packed Event definitions */
84 static const value_string three_packed_vals[] = {
85 { 0, "New" },
86 { 1, "JoinIn" },
87 { 2, "In" },
88 { 3, "JoinMt" },
89 { 4, "Mt" },
90 { 5, "Lv" },
91 { 0, NULL }
94 /**********************************************************/
95 /* Initialize the protocol and registered fields */
96 /**********************************************************/
97 static int proto_mmrp;
98 static int hf_mmrp_proto_id;
99 static int hf_mmrp_message; /* Message is a group of fields */
100 static int hf_mmrp_attribute_type;
101 static int hf_mmrp_attribute_length;
102 static int hf_mmrp_attribute_list; /* AttributeList is a group of fields */
103 static int hf_mmrp_vector_attribute; /* VectorAttribute is a group of fields */
105 /* The following VectorHeader contains the LeaveAllEvent and NumberOfValues */
106 static int hf_mmrp_vector_header;
107 static int hf_mmrp_leave_all_event;
108 static int hf_mmrp_number_of_values;
109 static int ett_vector_header;
110 static int * const vector_header_fields[] = {
111 &hf_mmrp_leave_all_event,
112 &hf_mmrp_number_of_values,
113 NULL
116 static int hf_mmrp_first_value; /* FirstValue is a group of fields */
118 static int hf_mmrp_mac;
119 static int hf_mmrp_ser_req;
121 static int hf_mmrp_three_packed_event;
123 static int hf_mmrp_end_mark;
125 /* Initialize the subtree pointers */
126 static int ett_mmrp;
127 static int ett_msg;
128 static int ett_attr_list;
129 static int ett_vect_attr;
130 static int ett_first_value;
134 /**********************************************************/
135 /* Dissector starts here */
136 /**********************************************************/
138 /* dissect_mmrp_common1 (called from dissect_mmrp)
140 * dissect the following fields which are common to all MMRP attributes:
141 * Attribute Type
142 * Attribute Length
143 * Attribute List Length
145 static void
146 dissect_mmrp_common1(proto_tree *msg_tree, tvbuff_t *tvb, int msg_offset)
148 proto_tree_add_item(msg_tree, hf_mmrp_attribute_type, tvb,
149 MMRP_ATTRIBUTE_TYPE_OFFSET + msg_offset, 1, ENC_BIG_ENDIAN);
150 proto_tree_add_item(msg_tree, hf_mmrp_attribute_length, tvb,
151 MMRP_ATTRIBUTE_LENGTH_OFFSET + msg_offset, 1, ENC_BIG_ENDIAN);
155 /* dissect_mmrp_common2 (called from dissect_mmrp)
157 * dissect the following fields which are common to all MMRP attributes:
158 * Leave All Event
159 * Number of Values fields
161 static void
162 dissect_mmrp_common2(proto_tree *vect_attr_tree, tvbuff_t *tvb, int msg_offset)
164 proto_tree_add_bitmask(vect_attr_tree, tvb, MMRP_VECTOR_HEADER_OFFSET + msg_offset,
165 hf_mmrp_vector_header, ett_vector_header, vector_header_fields, ENC_BIG_ENDIAN);
168 /* dissect_mmrp_three_packed_event (called from dissect_mmrp)
170 * dissect one or more ThreePackedEvents
172 static unsigned
173 dissect_mmrp_three_packed_event(proto_tree *vect_attr_tree, tvbuff_t *tvb, unsigned offset, uint16_t number_of_values)
175 unsigned counter;
177 for ( counter = 0; counter < number_of_values; ) {
178 uint8_t value;
179 uint8_t three_packed_event[3];
181 value = tvb_get_uint8(tvb, offset);
182 three_packed_event[0] = value / 36;
183 value -= 36 * three_packed_event[0];
184 three_packed_event[1] = value / 6;
185 value -= 6 * three_packed_event[1];
186 three_packed_event[2] = value;
188 proto_tree_add_uint(vect_attr_tree, hf_mmrp_three_packed_event, tvb, offset, sizeof(uint8_t),
189 three_packed_event[0]);
190 counter++;
191 if ( counter < number_of_values ) {
192 proto_tree_add_uint(vect_attr_tree, hf_mmrp_three_packed_event, tvb, offset, sizeof(uint8_t),
193 three_packed_event[1]);
194 counter++;
196 if ( counter < number_of_values ) {
197 proto_tree_add_uint(vect_attr_tree, hf_mmrp_three_packed_event, tvb, offset, sizeof(uint8_t),
198 three_packed_event[2]);
199 counter++;
202 offset++;
204 return offset;
207 /* dissect_main
209 * main dissect function that calls the other functions listed above as necessary
211 static int
212 dissect_mmrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_) {
213 /* Set up structures needed to add the protocol subtrees and manage them */
214 proto_item *ti, *msg_ti, *attr_list_ti, *vect_attr_ti, *first_value_ti;
215 proto_tree *mmrp_tree, *msg_tree, *attr_list_tree, *vect_attr_tree, *first_value_tree;
217 /* Make entries in Protocol column and Info column on summary display */
218 col_set_str(pinfo->cinfo, COL_PROTOCOL, "MRP-MMRP");
220 col_set_str(pinfo->cinfo, COL_INFO, "Multiple Mac Registration Protocol");
222 if (tree) {
223 uint8_t attribute_type;
224 uint8_t attribute_length;
225 uint16_t number_of_values;
226 unsigned offset = 0;
227 int vect_attr_len;
228 int msg_offset; /* Use when handling multiple messages. This points to current msg being decoded. */
229 int vect_offset; /* Use when handling multiple vector attributes. This points to the current vector attribute being decoded. */
231 ti = proto_tree_add_item(tree, proto_mmrp, tvb, 0, -1, ENC_NA);
232 mmrp_tree = proto_item_add_subtree(ti, ett_mmrp);
234 proto_tree_add_item(mmrp_tree, hf_mmrp_proto_id, tvb, MMRP_PROTOCOL_VERSION_OFFSET, 1, ENC_BIG_ENDIAN);
236 /* MMRP supports multiple MRP Messages per frame. Handle those Messages in
237 * the following while() loop. You will know you are at the end of the list
238 * of messages when the EndMark (0x0000) is encountered instead of an
239 * Attribute Type and Attribute Length (guaranteed to not be 0x0000).
241 msg_offset = 0;
242 while (tvb_get_ntohs(tvb, MMRP_ATTRIBUTE_TYPE_OFFSET + msg_offset) != MMRP_END_MARK) {
244 attribute_type = tvb_get_uint8(tvb, MMRP_ATTRIBUTE_TYPE_OFFSET + msg_offset);
245 attribute_length = tvb_get_uint8(tvb, MMRP_ATTRIBUTE_LENGTH_OFFSET + msg_offset);
247 /* MMRP Message is a group of fields
249 * Contains AttributeType (1 byte)
250 * + AttributeLength (1 byte)
251 * + AttributeList (AttributeListLength bytes)
252 * bytes of data
254 msg_ti = proto_tree_add_item(mmrp_tree, hf_mmrp_message, tvb,
255 MMRP_MESSAGE_GROUP_OFFSET + msg_offset,
256 -1, ENC_NA);
257 msg_tree = proto_item_add_subtree(msg_ti, ett_msg);
259 /* Append AttributeType description to the end of the "Message" heading */
260 proto_item_append_text(msg_tree, ": %s (%d)",
261 val_to_str_const(attribute_type, attribute_type_vals, "<Unknown>"),
262 attribute_type);
264 dissect_mmrp_common1(msg_tree, tvb, msg_offset);
266 /* MMRP AttributeList is a group of fields
268 * Contains AttributeListLength bytes of data NOT
270 attr_list_ti = proto_tree_add_item(msg_tree, hf_mmrp_attribute_list, tvb,
271 MMRP_ATTRIBUTE_LIST_GROUP_OFFSET + msg_offset,
272 -1, ENC_NA);
273 attr_list_tree = proto_item_add_subtree(attr_list_ti, ett_attr_list);
276 /* MMRP supports multiple MRP Vector Attributes per Attribute List. Handle those
277 * Vector Attributes in the following while() loop. You will know you are at the
278 * end of the list of Vector Attributes when the EndMark (0x0000) is encountered
279 * instead of a Vector Header (guaranteed to not be 0x0000).
281 vect_offset = 0;
282 while (tvb_get_ntohs(tvb, MMRP_VECTOR_HEADER_OFFSET + msg_offset + vect_offset) != MMRP_END_MARK) {
283 /* MMRP VectorAttribute is a group of fields
285 * Contains VectorHeader (2 bytes)
286 * + FirstValue (AttributeLength bytes)
287 * + VectorThreePacked (NumberOfValues @ 3/vector bytes)
288 * + VectorFourPacked (NumberOfValues @ 4/vector bytes only for Listener attributes)
289 * bytes of data
291 number_of_values = tvb_get_ntohs(tvb, MMRP_NUMBER_OF_VALUES_OFFSET + msg_offset + vect_offset)
292 & MMRP_NUMBER_OF_VALUES_MASK;
294 vect_attr_len = 2 + attribute_length + (number_of_values + 2)/3; /* stores 3 values per byte */
296 vect_attr_ti = proto_tree_add_item(attr_list_tree, hf_mmrp_vector_attribute, tvb,
297 MMRP_VECTOR_ATTRIBUTE_GROUP_OFFSET + msg_offset + vect_offset,
298 vect_attr_len, ENC_NA);
300 vect_attr_tree = proto_item_add_subtree(vect_attr_ti, ett_vect_attr);
302 dissect_mmrp_common2(vect_attr_tree, tvb, msg_offset + vect_offset);
304 if (attribute_type == MMRP_ATTRIBUTE_TYPE_MAC) {
305 /* MMRP FirstValue is a Mac Address*/
306 first_value_ti = proto_tree_add_item(vect_attr_tree, hf_mmrp_first_value, tvb,
307 MMRP_FIRST_VALUE_GROUP_OFFSET + msg_offset + vect_offset,
308 attribute_length, ENC_NA);
309 first_value_tree = proto_item_add_subtree(first_value_ti, ett_first_value);
311 /* Add MAC components to First Value tree */
312 proto_tree_add_item(first_value_tree, hf_mmrp_mac, tvb,
313 MMRP_FIRST_VALUE_GROUP_OFFSET + msg_offset + vect_offset, 6, ENC_NA);
315 /* Decode three packed events. */
316 offset = dissect_mmrp_three_packed_event(vect_attr_tree, tvb,
317 MMRP_MAC_THREE_PACKED_OFFSET + msg_offset + vect_offset,
318 number_of_values);
320 else if (attribute_type == MMRP_ATTRIBUTE_TYPE_SERVICE) {
321 /* MMRP Service Requirement*/
322 first_value_ti = proto_tree_add_item(vect_attr_tree, hf_mmrp_first_value, tvb,
323 MMRP_FIRST_VALUE_GROUP_OFFSET + msg_offset + vect_offset,
324 attribute_length, ENC_NA);
325 first_value_tree = proto_item_add_subtree(first_value_ti, ett_first_value);
327 /* Add ServiceRequirement components to First Value tree */
328 proto_tree_add_item(first_value_tree, hf_mmrp_ser_req, tvb,
329 MMRP_FIRST_VALUE_GROUP_OFFSET + msg_offset + vect_offset, 1, ENC_BIG_ENDIAN);
331 offset = dissect_mmrp_three_packed_event(vect_attr_tree, tvb,
332 MMRP_SERVICE_THREE_PACKED_OFFSET + msg_offset + vect_offset,
333 number_of_values);
336 vect_offset += vect_attr_len; /* Move to next Vector Attribute, if there is one */
337 } /* Multiple VectorAttribute while() */
339 proto_tree_add_item(attr_list_tree, hf_mmrp_end_mark, tvb, offset, 2, ENC_BIG_ENDIAN); /* VectorAttribute EndMark */
341 msg_offset += vect_offset + 2/* + VectorHeader */ + 2/* + endmark */;
343 } /* Multiple Message while() */
345 proto_tree_add_item(mmrp_tree, hf_mmrp_end_mark, tvb, offset+2, 2, ENC_BIG_ENDIAN); /* Message EndMark */
347 return tvb_captured_length(tvb);
351 /* Register the protocol with Wireshark */
352 void
353 proto_register_mrp_mmrp(void)
355 static hf_register_info hf[] = {
356 { &hf_mmrp_proto_id,
357 { "Protocol Version", "mrp-mmrp.protocol_version",
358 FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }
360 { &hf_mmrp_message, /* Message is a group of fields */
361 { "Message", "mrp-mmrp.message",
362 FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
364 { &hf_mmrp_attribute_type,
365 { "Attribute Type", "mrp-mmrp.attribute_type",
366 FT_UINT8, BASE_DEC, VALS(attribute_type_vals), 0x0, NULL, HFILL }
368 { &hf_mmrp_attribute_length,
369 { "Attribute Length", "mrp-mmrp.attribute_length",
370 FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }
372 { &hf_mmrp_attribute_list, /* AttributeList is a group of fields */
373 { "Attribute List", "mrp-mmrp.attribute_list",
374 FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
376 { &hf_mmrp_vector_attribute, /* VectorAttribute is a group of fields */
377 { "Vector Attribute", "mrp-mmrp.vector_attribute",
378 FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
380 { &hf_mmrp_vector_header,
381 { "Vector Header", "mrp-mmrp.vector_header",
382 FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }
384 { &hf_mmrp_leave_all_event,
385 { "Leave All Event", "mrp-mmrp.leave_all_event",
386 FT_UINT16, BASE_DEC, VALS(leave_all_vals), MMRP_LEAVE_ALL_EVENT_MASK, NULL, HFILL }
388 { &hf_mmrp_number_of_values,
389 { "Number of Values", "mrp-mmrp.number_of_values",
390 FT_UINT16, BASE_DEC, NULL, MMRP_NUMBER_OF_VALUES_MASK, NULL, HFILL }
392 { &hf_mmrp_first_value, /* FirstValue is a group of fields */
393 { "First Value", "mrp-mmrp.first_value",
394 FT_NONE, BASE_NONE, NULL, 0x0, NULL, HFILL }
396 { &hf_mmrp_mac,
397 { "MAC", "mrp-mmrp.mac",
398 FT_ETHER, BASE_NONE, NULL, 0x00, NULL, HFILL }
400 { &hf_mmrp_ser_req,
401 { "Service Requirement", "mrp-mmrp.service_requirement",
402 FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }
404 { &hf_mmrp_three_packed_event,
405 { "Attribute Event", "mrp-mmrp.three_packed_event",
406 FT_UINT8, BASE_DEC, VALS(three_packed_vals), 0x0, NULL, HFILL }
408 { &hf_mmrp_end_mark,
409 { "End Mark", "mrp-mmrp.end_mark",
410 FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }
414 /* Setup protocol subtree array */
415 static int *ett[] = {
416 &ett_mmrp,
417 &ett_msg,
418 &ett_attr_list,
419 &ett_vect_attr,
420 &ett_vector_header,
421 &ett_first_value
424 /* Register the protocol name and description */
425 proto_mmrp = proto_register_protocol("Multiple Mac Registration Protocol",
426 "MRP-MMRP", "mrp-mmrp");
428 /* Required function calls to register the header fields and subtrees used */
429 proto_register_field_array(proto_mmrp, hf, array_length(hf));
430 proto_register_subtree_array(ett, array_length(ett));
432 /* Register the dissector */
433 mmrp_handle = register_dissector("mrp-mmrp", dissect_mmrp, proto_mmrp);
436 void
437 proto_reg_handoff_mrp_mmrp(void)
439 dissector_add_uint("ethertype", ETHERTYPE_MMRP, mmrp_handle);
443 * Editor modelines - https://www.wireshark.org/tools/modelines.html
445 * Local variables:
446 * c-basic-offset: 4
447 * tab-width: 8
448 * indent-tabs-mode: nil
449 * End:
451 * vi: set shiftwidth=4 tabstop=8 expandtab:
452 * :indentSize=4:tabSize=8:noTabs=true: