Coverage Report

Created: 2026-11-12 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wireshark/epan/dissectors/packet-mrp-mmrp.c
Line
Count
Source
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>
4
 *
5
 *
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
9
 *
10
 * Wireshark - Network traffic analyzer
11
 * By Gerald Combs <gerald[AT]wireshark.org>
12
 * Copyright 1998 Gerald Combs
13
 *
14
 * SPDX-License-Identifier: GPL-2.0-or-later
15
 *
16
 * The MMRP Protocol specification can be found at the following:
17
 * http://standards.ieee.org/about/get/802/802.1.html
18
 *
19
 */
20
21
#include "config.h"
22
23
#include <epan/packet.h>
24
#include <epan/etypes.h>
25
26
void proto_register_mrp_mmrp(void);
27
void proto_reg_handoff_mrp_mmrp(void);
28
29
static dissector_handle_t mmrp_handle;
30
31
/* MMRP End Mark Sequence */
32
159
#define MMRP_END_MARK       0x0000
33
34
/**********************************************************/
35
/* Offsets of fields within an MMRP packet                */
36
/**********************************************************/
37
772
#define MMRP_PROTOCOL_VERSION_OFFSET        0
38
39
/* Next comes the MMRP Message group */
40
745
#define MMRP_MESSAGE_GROUP_OFFSET          (MMRP_PROTOCOL_VERSION_OFFSET + 1) /* Message is a group of fields */
41
708
#define MMRP_ATTRIBUTE_TYPE_OFFSET         (MMRP_MESSAGE_GROUP_OFFSET)
42
543
#define MMRP_ATTRIBUTE_LENGTH_OFFSET       (MMRP_ATTRIBUTE_TYPE_OFFSET + 1)
43
44
/* Next comes the MMRP AttributeList group */
45
469
#define MMRP_ATTRIBUTE_LIST_GROUP_OFFSET   (MMRP_ATTRIBUTE_LENGTH_OFFSET + 1) /* AttributeList is a group of fields */
46
47
/* Next comes the MMRP VectorAttribute group */
48
432
#define MMRP_VECTOR_ATTRIBUTE_GROUP_OFFSET (MMRP_ATTRIBUTE_LIST_GROUP_OFFSET) /* VectorAttribute is a group of fields */
49
374
#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
16
#define MMRP_LEAVE_ALL_EVENT_MASK           0xE000
52
58
#define MMRP_NUMBER_OF_VALUES_OFFSET       (MMRP_VECTOR_HEADER_OFFSET)
53
74
#define MMRP_NUMBER_OF_VALUES_MASK          0x1fff
54
55
/* Next comes the MMRP FirstValue group */
56
126
#define MMRP_FIRST_VALUE_GROUP_OFFSET      (MMRP_VECTOR_HEADER_OFFSET + 2) /* FirstValue is a group of fields */
57
58
1
#define MMRP_SERVICE_THREE_PACKED_OFFSET   (MMRP_FIRST_VALUE_GROUP_OFFSET + 1)
59
41
#define MMRP_MAC_THREE_PACKED_OFFSET       (MMRP_FIRST_VALUE_GROUP_OFFSET + 6)
60
61
/**********************************************************/
62
/* Valid field contents                                   */
63
/**********************************************************/
64
65
/* Attribute Type definitions */
66
17
#define MMRP_ATTRIBUTE_TYPE_SERVICE   0x01
67
58
#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 }
72
};
73
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 }
81
};
82
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 }
92
};
93
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 */
104
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
114
};
115
116
static int hf_mmrp_first_value; /* FirstValue is a group of fields */
117
118
static int hf_mmrp_mac;
119
static int hf_mmrp_ser_req;
120
121
static int hf_mmrp_three_packed_event;
122
123
static int hf_mmrp_end_mark;
124
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;
131
132
133
134
/**********************************************************/
135
/* Dissector starts here                                  */
136
/**********************************************************/
137
138
/* dissect_mmrp_common1 (called from dissect_mmrp)
139
 *
140
 * dissect the following fields which are common to all MMRP attributes:
141
 *   Attribute Type
142
 *   Attribute Length
143
 *   Attribute List Length
144
 */
145
static void
146
dissect_mmrp_common1(proto_tree *msg_tree, tvbuff_t *tvb, int msg_offset)
147
37
{
148
37
    proto_tree_add_item(msg_tree, hf_mmrp_attribute_type, tvb,
149
37
                        MMRP_ATTRIBUTE_TYPE_OFFSET + msg_offset, 1, ENC_BIG_ENDIAN);
150
37
    proto_tree_add_item(msg_tree, hf_mmrp_attribute_length, tvb,
151
37
                        MMRP_ATTRIBUTE_LENGTH_OFFSET + msg_offset, 1, ENC_BIG_ENDIAN);
152
37
}
153
154
155
/* dissect_mmrp_common2 (called from dissect_mmrp)
156
 *
157
 * dissect the following fields which are common to all MMRP attributes:
158
 *   Leave All Event
159
 *   Number of Values fields
160
 */
161
static void
162
dissect_mmrp_common2(proto_tree *vect_attr_tree, tvbuff_t *tvb, int msg_offset)
163
58
{
164
58
    proto_tree_add_bitmask(vect_attr_tree, tvb, MMRP_VECTOR_HEADER_OFFSET + msg_offset,
165
58
                           hf_mmrp_vector_header, ett_vector_header, vector_header_fields, ENC_BIG_ENDIAN);
166
58
}
167
168
/* dissect_mmrp_three_packed_event (called from dissect_mmrp)
169
 *
170
 * dissect one or more ThreePackedEvents
171
 */
172
static unsigned
173
dissect_mmrp_three_packed_event(proto_tree *vect_attr_tree, tvbuff_t *tvb, unsigned offset, uint16_t number_of_values)
174
42
{
175
42
    unsigned counter;
176
177
5.47k
    for ( counter = 0; counter < number_of_values; ) {
178
5.43k
        uint8_t value;
179
5.43k
        uint8_t three_packed_event[3];
180
181
5.43k
        value = tvb_get_uint8(tvb, offset);
182
5.43k
        three_packed_event[0] = value / 36;
183
5.43k
        value -= 36 * three_packed_event[0];
184
5.43k
        three_packed_event[1] = value / 6;
185
5.43k
        value -=  6 * three_packed_event[1];
186
5.43k
        three_packed_event[2] = value;
187
188
5.43k
        proto_tree_add_uint(vect_attr_tree, hf_mmrp_three_packed_event, tvb, offset, sizeof(uint8_t),
189
5.43k
                            three_packed_event[0]);
190
5.43k
        counter++;
191
5.43k
        if ( counter < number_of_values ) {
192
5.42k
            proto_tree_add_uint(vect_attr_tree, hf_mmrp_three_packed_event, tvb, offset, sizeof(uint8_t),
193
5.42k
                                three_packed_event[1]);
194
5.42k
            counter++;
195
5.42k
        }
196
5.43k
        if ( counter < number_of_values ) {
197
5.41k
            proto_tree_add_uint(vect_attr_tree, hf_mmrp_three_packed_event, tvb, offset, sizeof(uint8_t),
198
5.41k
                                three_packed_event[2]);
199
5.41k
            counter++;
200
5.41k
        }
201
202
5.43k
        offset++;
203
5.43k
    }
204
42
    return offset;
205
42
}
206
207
/* dissect_main
208
 *
209
 * main dissect function that calls the other functions listed above as necessary
210
 */
211
static int
212
27
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
27
    proto_item *ti, *msg_ti, *attr_list_ti, *vect_attr_ti, *first_value_ti;
215
27
    proto_tree *mmrp_tree, *msg_tree, *attr_list_tree, *vect_attr_tree, *first_value_tree;
216
217
    /* Make entries in Protocol column and Info column on summary display */
218
27
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "MRP-MMRP");
219
220
27
    col_set_str(pinfo->cinfo, COL_INFO, "Multiple Mac Registration Protocol");
221
222
27
    if (tree) {
223
27
        uint8_t attribute_type;
224
27
        uint8_t attribute_length;
225
27
        uint16_t number_of_values;
226
27
        int vect_attr_len;
227
27
        int msg_offset;  /* Use when handling multiple messages.  This points to current msg being decoded. */
228
27
        int vect_offset; /* Use when handling multiple vector attributes.  This points to the current vector attribute being decoded. */
229
230
27
        ti = proto_tree_add_item(tree, proto_mmrp, tvb, 0, -1, ENC_NA);
231
27
        mmrp_tree = proto_item_add_subtree(ti, ett_mmrp);
232
233
27
        proto_tree_add_item(mmrp_tree, hf_mmrp_proto_id, tvb, MMRP_PROTOCOL_VERSION_OFFSET, 1, ENC_BIG_ENDIAN);
234
235
        /* MMRP supports multiple MRP Messages per frame.  Handle those Messages in
236
         * the following while() loop. You will know you are at the end of the list
237
         * of messages when the EndMark (0x0000) is encountered instead of an
238
         * Attribute Type and Attribute Length (guaranteed to not be 0x0000).
239
         */
240
27
        msg_offset = 0;
241
64
        while (tvb_get_ntohs(tvb, MMRP_ATTRIBUTE_TYPE_OFFSET + msg_offset) != MMRP_END_MARK) {
242
243
37
            attribute_type = tvb_get_uint8(tvb, MMRP_ATTRIBUTE_TYPE_OFFSET + msg_offset);
244
37
            attribute_length = tvb_get_uint8(tvb, MMRP_ATTRIBUTE_LENGTH_OFFSET + msg_offset);
245
246
            /* MMRP Message is a group of fields
247
             *
248
             * Contains AttributeType (1 byte)
249
             *        + AttributeLength (1 byte)
250
             *        + AttributeList (AttributeListLength bytes)
251
            *        bytes of data
252
            */
253
37
            msg_ti = proto_tree_add_item(mmrp_tree, hf_mmrp_message, tvb,
254
37
                                         MMRP_MESSAGE_GROUP_OFFSET + msg_offset,
255
37
                                         -1, ENC_NA);
256
37
            msg_tree = proto_item_add_subtree(msg_ti, ett_msg);
257
258
            /* Append AttributeType description to the end of the "Message" heading */
259
37
            proto_item_append_text(msg_tree, ": %s (%d)",
260
37
                                   val_to_str_const(attribute_type, attribute_type_vals, "<Unknown>"),
261
37
                                   attribute_type);
262
263
37
            dissect_mmrp_common1(msg_tree, tvb, msg_offset);
264
265
            /* MMRP AttributeList is a group of fields
266
             *
267
             * Contains AttributeListLength bytes of data NOT
268
             */
269
37
            attr_list_ti = proto_tree_add_item(msg_tree, hf_mmrp_attribute_list, tvb,
270
37
                                               MMRP_ATTRIBUTE_LIST_GROUP_OFFSET + msg_offset,
271
37
                                               -1, ENC_NA);
272
37
            attr_list_tree = proto_item_add_subtree(attr_list_ti, ett_attr_list);
273
274
275
            /* MMRP supports multiple MRP Vector Attributes per Attribute List.  Handle those
276
             * Vector Attributes in the following while() loop. You will know you are at the
277
             * end of the list of Vector Attributes when the EndMark (0x0000) is encountered
278
             * instead of a Vector Header (guaranteed to not be 0x0000).
279
             */
280
37
            vect_offset = 0;
281
95
            while (tvb_get_ntohs(tvb, MMRP_VECTOR_HEADER_OFFSET + msg_offset + vect_offset) != MMRP_END_MARK) {
282
                /* MMRP VectorAttribute is a group of fields
283
                 *
284
                 * Contains VectorHeader (2 bytes)
285
                 *        + FirstValue (AttributeLength bytes)
286
                 *        + VectorThreePacked (NumberOfValues @ 3/vector bytes)
287
                 *        + VectorFourPacked (NumberOfValues @ 4/vector bytes only for Listener attributes)
288
                 *        bytes of data
289
                 */
290
58
                number_of_values = tvb_get_ntohs(tvb, MMRP_NUMBER_OF_VALUES_OFFSET + msg_offset + vect_offset)
291
58
                                   & MMRP_NUMBER_OF_VALUES_MASK;
292
293
58
                vect_attr_len = 2 + attribute_length + (number_of_values + 2)/3; /* stores 3 values per byte */
294
295
58
                vect_attr_ti = proto_tree_add_item(attr_list_tree, hf_mmrp_vector_attribute, tvb,
296
58
                                                   MMRP_VECTOR_ATTRIBUTE_GROUP_OFFSET + msg_offset + vect_offset,
297
58
                                                   vect_attr_len, ENC_NA);
298
299
58
                vect_attr_tree = proto_item_add_subtree(vect_attr_ti, ett_vect_attr);
300
301
58
                dissect_mmrp_common2(vect_attr_tree, tvb, msg_offset + vect_offset);
302
303
58
                if (attribute_type == MMRP_ATTRIBUTE_TYPE_MAC) {
304
                    /* MMRP FirstValue is a Mac Address*/
305
41
                    first_value_ti = proto_tree_add_item(vect_attr_tree, hf_mmrp_first_value, tvb,
306
41
                                        MMRP_FIRST_VALUE_GROUP_OFFSET + msg_offset + vect_offset,
307
41
                                        attribute_length, ENC_NA);
308
41
                    first_value_tree = proto_item_add_subtree(first_value_ti, ett_first_value);
309
310
                    /* Add MAC components to First Value tree */
311
41
                    proto_tree_add_item(first_value_tree, hf_mmrp_mac, tvb,
312
41
                                        MMRP_FIRST_VALUE_GROUP_OFFSET + msg_offset + vect_offset, 6, ENC_NA);
313
314
                    /* Decode three packed events. */
315
41
                    dissect_mmrp_three_packed_event(vect_attr_tree, tvb,
316
41
                                                             MMRP_MAC_THREE_PACKED_OFFSET + msg_offset + vect_offset,
317
41
                                                             number_of_values);
318
41
                }
319
17
                else if (attribute_type == MMRP_ATTRIBUTE_TYPE_SERVICE) {
320
                    /* MMRP Service Requirement*/
321
1
                    first_value_ti = proto_tree_add_item(vect_attr_tree, hf_mmrp_first_value, tvb,
322
1
                                        MMRP_FIRST_VALUE_GROUP_OFFSET + msg_offset + vect_offset,
323
1
                                        attribute_length, ENC_NA);
324
1
                    first_value_tree = proto_item_add_subtree(first_value_ti, ett_first_value);
325
326
                    /* Add ServiceRequirement components to First Value tree */
327
1
                    proto_tree_add_item(first_value_tree, hf_mmrp_ser_req, tvb,
328
1
                                        MMRP_FIRST_VALUE_GROUP_OFFSET + msg_offset + vect_offset, 1, ENC_BIG_ENDIAN);
329
330
1
                    dissect_mmrp_three_packed_event(vect_attr_tree, tvb,
331
1
                                                             MMRP_SERVICE_THREE_PACKED_OFFSET + msg_offset + vect_offset,
332
1
                                                             number_of_values);
333
1
                }
334
335
58
                vect_offset += vect_attr_len; /* Move to next Vector Attribute, if there is one */
336
58
            } /* Multiple VectorAttribute while() */
337
338
37
            proto_tree_add_item(attr_list_tree, hf_mmrp_end_mark, tvb,
339
37
                                MMRP_VECTOR_HEADER_OFFSET + msg_offset + vect_offset, 2, ENC_BIG_ENDIAN); /* VectorAttribute EndMark */
340
341
37
            msg_offset += vect_offset + 2/* + VectorHeader */ + 2/* + endmark */;
342
343
37
        } /* Multiple Message while() */
344
345
27
        proto_tree_add_item(mmrp_tree, hf_mmrp_end_mark, tvb,
346
27
                            MMRP_ATTRIBUTE_TYPE_OFFSET + msg_offset, 2, ENC_BIG_ENDIAN); /* Message EndMark */
347
27
    }
348
27
    return tvb_captured_length(tvb);
349
27
}
350
351
352
/* Register the protocol with Wireshark */
353
void
354
proto_register_mrp_mmrp(void)
355
16
{
356
16
    static hf_register_info hf[] = {
357
16
        { &hf_mmrp_proto_id,
358
16
            { "Protocol Version",      "mrp-mmrp.protocol_version",
359
16
              FT_UINT8,  BASE_DEC, NULL, 0x0, NULL, HFILL }
360
16
        },
361
16
        { &hf_mmrp_message, /* Message is a group of fields */
362
16
            { "Message",               "mrp-mmrp.message",
363
16
              FT_NONE,  BASE_NONE, NULL, 0x0, NULL, HFILL }
364
16
        },
365
16
        { &hf_mmrp_attribute_type,
366
16
            { "Attribute Type",        "mrp-mmrp.attribute_type",
367
16
              FT_UINT8,  BASE_DEC, VALS(attribute_type_vals), 0x0, NULL, HFILL }
368
16
        },
369
16
        { &hf_mmrp_attribute_length,
370
16
            { "Attribute Length",      "mrp-mmrp.attribute_length",
371
16
              FT_UINT8,  BASE_DEC, NULL, 0x0, NULL, HFILL }
372
16
        },
373
16
        { &hf_mmrp_attribute_list, /* AttributeList is a group of fields */
374
16
            { "Attribute List",        "mrp-mmrp.attribute_list",
375
16
              FT_NONE,  BASE_NONE, NULL, 0x0, NULL, HFILL }
376
16
        },
377
16
        { &hf_mmrp_vector_attribute, /* VectorAttribute is a group of fields */
378
16
            { "Vector Attribute",      "mrp-mmrp.vector_attribute",
379
16
              FT_NONE,  BASE_NONE, NULL, 0x0, NULL, HFILL }
380
16
        },
381
16
        { &hf_mmrp_vector_header,
382
16
            { "Vector Header",         "mrp-mmrp.vector_header",
383
16
              FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }
384
16
        },
385
16
        { &hf_mmrp_leave_all_event,
386
16
            { "Leave All Event",       "mrp-mmrp.leave_all_event",
387
16
              FT_UINT16, BASE_DEC, VALS(leave_all_vals), MMRP_LEAVE_ALL_EVENT_MASK, NULL, HFILL }
388
16
        },
389
16
        { &hf_mmrp_number_of_values,
390
16
            { "Number of Values",      "mrp-mmrp.number_of_values",
391
16
              FT_UINT16, BASE_DEC, NULL, MMRP_NUMBER_OF_VALUES_MASK, NULL, HFILL }
392
16
        },
393
16
        { &hf_mmrp_first_value, /* FirstValue is a group of fields */
394
16
            { "First Value",           "mrp-mmrp.first_value",
395
16
              FT_NONE,  BASE_NONE, NULL, 0x0, NULL, HFILL }
396
16
        },
397
16
        { &hf_mmrp_mac,
398
16
            { "MAC",                   "mrp-mmrp.mac",
399
16
              FT_ETHER,  BASE_NONE, NULL, 0x00, NULL, HFILL }
400
16
        },
401
16
        { &hf_mmrp_ser_req,
402
16
            { "Service Requirement",   "mrp-mmrp.service_requirement",
403
16
              FT_UINT8, BASE_DEC,  NULL, 0x0, NULL, HFILL }
404
16
        },
405
16
        { &hf_mmrp_three_packed_event,
406
16
            { "Attribute Event",       "mrp-mmrp.three_packed_event",
407
16
              FT_UINT8, BASE_DEC,  VALS(three_packed_vals), 0x0, NULL, HFILL }
408
16
        },
409
16
        { &hf_mmrp_end_mark,
410
16
            { "End Mark",              "mrp-mmrp.end_mark",
411
16
              FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }
412
16
        },
413
16
    };
414
415
    /* Setup protocol subtree array */
416
16
    static int *ett[] = {
417
16
        &ett_mmrp,
418
16
        &ett_msg,
419
16
        &ett_attr_list,
420
16
        &ett_vect_attr,
421
16
        &ett_vector_header,
422
16
        &ett_first_value
423
16
    };
424
425
    /* Register the protocol name and description */
426
16
    proto_mmrp = proto_register_protocol("Multiple Mac Registration Protocol",
427
16
                                         "MRP-MMRP", "mrp-mmrp");
428
429
    /* Required function calls to register the header fields and subtrees used */
430
16
    proto_register_field_array(proto_mmrp, hf, array_length(hf));
431
16
    proto_register_subtree_array(ett, array_length(ett));
432
433
    /* Register the dissector */
434
16
    mmrp_handle = register_dissector("mrp-mmrp", dissect_mmrp, proto_mmrp);
435
16
}
436
437
void
438
proto_reg_handoff_mrp_mmrp(void)
439
16
{
440
16
    dissector_add_uint("ethertype", ETHERTYPE_MMRP, mmrp_handle);
441
16
}
442
443
/*
444
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
445
 *
446
 * Local variables:
447
 * c-basic-offset: 4
448
 * tab-width: 8
449
 * indent-tabs-mode: nil
450
 * End:
451
 *
452
 * vi: set shiftwidth=4 tabstop=8 expandtab:
453
 * :indentSize=4:tabSize=8:noTabs=true:
454
 */