Coverage Report

Created: 2026-05-14 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wireshark/epan/dissectors/packet-mrp-mvrp.c
Line
Count
Source
1
/* packet-mrp-mvrp.c
2
 * Routines for MVRP (MRP Multiple VLAN Registration Protocol) dissection
3
 * Copyright 2011, Pascal Levesque <plevesque[AT]orthogone.ca>
4
 *
5
 *
6
 * Based on the code from packet-mrp-mmrp.c (MMRP) from
7
 * Johannes Jochen <johannes.jochen[AT]belden.com>
8
 * Copyright 2011
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 MVRP 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_mvrp(void);
27
void proto_reg_handoff_mrp_mvrp(void);
28
29
static dissector_handle_t mvrp_handle;
30
31
/* MVRP End Mark Sequence */
32
76
#define MVRP_END_MARK       0x0000
33
34
/**********************************************************/
35
/* Offsets of fields within an MVRP packet                */
36
/**********************************************************/
37
628
#define MVRP_PROTOCOL_VERSION_OFFSET        0
38
39
/* Next comes the MVRP Message group */
40
595
#define MVRP_MESSAGE_GROUP_OFFSET          (MVRP_PROTOCOL_VERSION_OFFSET + 1) /* Message is a group of fields */
41
565
#define MVRP_ATTRIBUTE_TYPE_OFFSET         (MVRP_MESSAGE_GROUP_OFFSET)
42
376
#define MVRP_ATTRIBUTE_LENGTH_OFFSET       (MVRP_ATTRIBUTE_TYPE_OFFSET + 1)
43
44
/* Next comes the MVRP AttributeList group */
45
316
#define MVRP_ATTRIBUTE_LIST_GROUP_OFFSET   (MVRP_ATTRIBUTE_LENGTH_OFFSET + 1) /* AttributeList is a group of fields */
46
47
/* Next comes the MVRP VectorAttribute group */
48
286
#define MVRP_VECTOR_ATTRIBUTE_GROUP_OFFSET (MVRP_ATTRIBUTE_LIST_GROUP_OFFSET) /* VectorAttribute is a group of fields */
49
250
#define MVRP_VECTOR_HEADER_OFFSET          (MVRP_VECTOR_ATTRIBUTE_GROUP_OFFSET) /* contains the following two fields */
50
#define MVRP_LEAVE_ALL_EVENT_OFFSET        (MVRP_VECTOR_HEADER_OFFSET)
51
15
#define MVRP_LEAVE_ALL_EVENT_MASK           0xE000
52
36
#define MVRP_NUMBER_OF_VALUES_OFFSET       (MVRP_VECTOR_HEADER_OFFSET)
53
51
#define MVRP_NUMBER_OF_VALUES_MASK          0x1fff
54
55
/* Next comes the MVRP FirstValue group */
56
39
#define MVRP_FIRST_VALUE_GROUP_OFFSET      (MVRP_VECTOR_HEADER_OFFSET + 2) /* FirstValue is a group of fields */
57
58
13
#define MVRP_VID_THREE_PACKED_OFFSET       (MVRP_FIRST_VALUE_GROUP_OFFSET + 2)
59
60
/**********************************************************/
61
/* Valid field contents                                   */
62
/**********************************************************/
63
64
/* Attribute Type definitions */
65
36
#define MVRP_ATTRIBUTE_TYPE_VID       0x01
66
static const value_string attribute_type_vals[] = {
67
    { MVRP_ATTRIBUTE_TYPE_VID, "VLAN Identifier" },
68
    { 0,                                    NULL }
69
};
70
71
/* Leave All Event definitions */
72
#define MVRP_NULLLEAVEALL   0
73
#define MVRP_LEAVEALL       1
74
static const value_string leave_all_vals[] = {
75
    { MVRP_NULLLEAVEALL, "Null" },
76
    { MVRP_LEAVEALL,     "Leave All" },
77
    { 0,                 NULL }
78
};
79
80
/* Three Packed Event definitions */
81
static const value_string three_packed_vals[] = {
82
    { 0, "New" },
83
    { 1, "JoinIn" },
84
    { 2, "In" },
85
    { 3, "JoinMt" },
86
    { 4, "Mt" },
87
    { 5, "Lv" },
88
    { 0, NULL }
89
};
90
91
/**********************************************************/
92
/* Initialize the protocol and registered fields          */
93
/**********************************************************/
94
static int proto_mvrp;
95
static int hf_mvrp_proto_id;
96
static int hf_mvrp_message; /* Message is a group of fields */
97
static int hf_mvrp_attribute_type;
98
static int hf_mvrp_attribute_length;
99
static int hf_mvrp_attribute_list; /* AttributeList is a group of fields */
100
static int hf_mvrp_vector_attribute; /* VectorAttribute is a group of fields */
101
102
/* The following VectorHeader contains the LeaveAllEvent and NumberOfValues */
103
static int hf_mvrp_vector_header;
104
static int hf_mvrp_leave_all_event;
105
static int hf_mvrp_number_of_values;
106
static int ett_vector_header;
107
static int * const vector_header_fields[] = {
108
    &hf_mvrp_leave_all_event,
109
    &hf_mvrp_number_of_values,
110
    NULL
111
};
112
113
static int hf_mvrp_first_value; /* FirstValue is a group of fields */
114
static int hf_mvrp_vid;
115
static int hf_mvrp_three_packed_event;
116
117
static int hf_mvrp_end_mark;
118
119
/* Initialize the subtree pointers */
120
static int ett_mvrp;
121
static int ett_msg;
122
static int ett_attr_list;
123
static int ett_vect_attr;
124
static int ett_first_value;
125
126
127
128
/**********************************************************/
129
/* Dissector starts here                                  */
130
/**********************************************************/
131
132
/* dissect_mvrp_common1 (called from dissect_mvrp)
133
 *
134
 * dissect the following fields which are common to all MVRP attributes:
135
 *   Attribute Type
136
 *   Attribute Length
137
 *   Attribute List Length
138
 */
139
static void
140
dissect_mvrp_common1(proto_tree *msg_tree, tvbuff_t *tvb, int msg_offset)
141
30
{
142
30
    proto_tree_add_item(msg_tree, hf_mvrp_attribute_type, tvb,
143
30
                        MVRP_ATTRIBUTE_TYPE_OFFSET + msg_offset, 1, ENC_BIG_ENDIAN);
144
30
    proto_tree_add_item(msg_tree, hf_mvrp_attribute_length, tvb,
145
30
                        MVRP_ATTRIBUTE_LENGTH_OFFSET + msg_offset, 1, ENC_BIG_ENDIAN);
146
30
}
147
148
149
/* dissect_mvrp_common2 (called from dissect_mvrp)
150
 *
151
 * dissect the following fields which are common to all MVRP attributes:
152
 *   Leave All Event
153
 *   Number of Values fields
154
 */
155
static void
156
dissect_mvrp_common2(proto_tree *vect_attr_tree, tvbuff_t *tvb, int msg_offset)
157
36
{
158
36
    proto_tree_add_bitmask(vect_attr_tree, tvb, MVRP_VECTOR_HEADER_OFFSET + msg_offset,
159
36
                           hf_mvrp_vector_header, ett_vector_header, vector_header_fields, ENC_BIG_ENDIAN);
160
36
}
161
162
/* dissect_mvrp_three_packed_event (called from dissect_mvrp)
163
 *
164
 * dissect one or more ThreePackedEvents
165
 */
166
static unsigned
167
dissect_mvrp_three_packed_event(proto_tree *vect_attr_tree, tvbuff_t *tvb, unsigned offset, uint16_t number_of_values)
168
13
{
169
13
    unsigned counter;
170
171
1.04k
    for ( counter = 0; counter < number_of_values; ) {
172
1.03k
        uint8_t value;
173
1.03k
        uint8_t three_packed_event[3];
174
175
1.03k
        value = tvb_get_uint8(tvb, offset);
176
1.03k
        three_packed_event[0] = value / 36;
177
1.03k
        value -= 36 * three_packed_event[0];
178
1.03k
        three_packed_event[1] = value / 6;
179
1.03k
        value -=  6 * three_packed_event[1];
180
1.03k
        three_packed_event[2] = value;
181
182
1.03k
        proto_tree_add_uint(vect_attr_tree, hf_mvrp_three_packed_event, tvb, offset, sizeof(uint8_t),
183
1.03k
                            three_packed_event[0]);
184
1.03k
        counter++;
185
1.03k
        if ( counter < number_of_values ) {
186
1.02k
            proto_tree_add_uint(vect_attr_tree, hf_mvrp_three_packed_event, tvb, offset, sizeof(uint8_t),
187
1.02k
                                three_packed_event[1]);
188
1.02k
            counter++;
189
1.02k
        }
190
1.03k
        if ( counter < number_of_values ) {
191
1.02k
            proto_tree_add_uint(vect_attr_tree, hf_mvrp_three_packed_event, tvb, offset, sizeof(uint8_t),
192
1.02k
                                three_packed_event[2]);
193
1.02k
            counter++;
194
1.02k
        }
195
196
1.03k
        offset++;
197
1.03k
    }
198
13
    return offset;
199
13
}
200
201
/* dissect_main
202
 *
203
 * main dissect function that calls the other functions listed above as necessary
204
 */
205
static int
206
dissect_mvrp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
207
33
{
208
    /* Set up structures needed to add the protocol subtrees and manage them */
209
33
    proto_item *ti, *msg_ti, *attr_list_ti, *vect_attr_ti, *first_value_ti;
210
33
    proto_tree *mvrp_tree, *msg_tree, *attr_list_tree, *vect_attr_tree, *first_value_tree;
211
212
    /* Make entries in Protocol column and Info column on summary display */
213
33
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "MRP-MVRP");
214
215
33
    col_set_str(pinfo->cinfo, COL_INFO, "Multiple VLAN Registration Protocol");
216
217
33
    if (tree) {
218
33
        uint8_t attribute_type;
219
33
        uint8_t attribute_length;
220
33
        uint16_t number_of_values;
221
33
        unsigned offset = 0;
222
33
        unsigned int vect_attr_len;
223
33
        unsigned int msg_offset;  /* Use when handling multiple messages.  This points to current msg being decoded. */
224
33
        unsigned int vect_offset; /* Use when handling multiple vector attributes.  This points to the current vector attribute being decoded. */
225
226
33
        ti = proto_tree_add_item(tree, proto_mvrp, tvb, 0, -1, ENC_NA);
227
33
        mvrp_tree = proto_item_add_subtree(ti, ett_mvrp);
228
229
33
        proto_tree_add_item(mvrp_tree, hf_mvrp_proto_id, tvb, MVRP_PROTOCOL_VERSION_OFFSET, 1, ENC_BIG_ENDIAN);
230
231
        /* MVRP supports multiple MRP Messages per frame.  Handle those Messages in
232
         * the following while() loop. You will know you are at the end of the list
233
         * of messages when the EndMark (0x0000) is encountered instead of an
234
         * Attribute Type and Attribute Length (guaranteed to not be 0x0000).
235
         */
236
33
        msg_offset = 0;
237
63
        while (MVRP_ATTRIBUTE_TYPE_OFFSET + msg_offset < tvb_reported_length(tvb) && tvb_get_ntohs(tvb, MVRP_ATTRIBUTE_TYPE_OFFSET + msg_offset) != MVRP_END_MARK) {
238
239
30
            attribute_type = tvb_get_uint8(tvb, MVRP_ATTRIBUTE_TYPE_OFFSET + msg_offset);
240
30
            attribute_length = tvb_get_uint8(tvb, MVRP_ATTRIBUTE_LENGTH_OFFSET + msg_offset);
241
242
            /* MVRP Message is a group of fields
243
             *
244
             * Contains AttributeType (1 byte)
245
             *        + AttributeLength (1 byte)
246
             *        + AttributeList (AttributeListLength bytes)
247
            *        bytes of data
248
            */
249
30
            msg_ti = proto_tree_add_item(mvrp_tree, hf_mvrp_message, tvb,
250
30
                                         MVRP_MESSAGE_GROUP_OFFSET + msg_offset,
251
30
                                         -1, ENC_NA);
252
30
            msg_tree = proto_item_add_subtree(msg_ti, ett_msg);
253
254
            /* Append AttributeType description to the end of the "Message" heading */
255
30
            proto_item_append_text(msg_tree, ": %s (%d)",
256
30
                                   val_to_str_const(attribute_type, attribute_type_vals, "<Unknown>"),
257
30
                                   attribute_type);
258
259
30
            dissect_mvrp_common1(msg_tree, tvb, msg_offset);
260
261
            /* MVRP AttributeList is a group of fields
262
             *
263
             * Contains AttributeListLength bytes of data NOT
264
             */
265
30
            attr_list_ti = proto_tree_add_item(msg_tree, hf_mvrp_attribute_list, tvb,
266
30
                                               MVRP_ATTRIBUTE_LIST_GROUP_OFFSET + msg_offset,
267
30
                                               -1, ENC_NA);
268
30
            attr_list_tree = proto_item_add_subtree(attr_list_ti, ett_attr_list);
269
270
271
            /* MVRP supports multiple MRP Vector Attributes per Attribute List.  Handle those
272
             * Vector Attributes in the following while() loop. You will know you are at the
273
             * end of the list of Vector Attributes when the EndMark (0x0000) is encountered
274
             * instead of a Vector Header (guaranteed to not be 0x0000).
275
             */
276
30
            vect_offset = 0;
277
66
            while (MVRP_VECTOR_HEADER_OFFSET + msg_offset + vect_offset < tvb_reported_length(tvb) && tvb_get_ntohs(tvb, MVRP_VECTOR_HEADER_OFFSET + msg_offset + vect_offset) != MVRP_END_MARK) {
278
                /* MVRP VectorAttribute is a group of fields
279
                 *
280
                 * Contains VectorHeader (2 bytes)
281
                 *        + FirstValue (AttributeLength bytes)
282
                 *        + VectorThreePacked (NumberOfValues @ 3/vector bytes)
283
                 *        + VectorFourPacked (NumberOfValues @ 4/vector bytes only for Listener attributes)
284
                 *        bytes of data
285
                 */
286
36
                number_of_values = tvb_get_ntohs(tvb, MVRP_NUMBER_OF_VALUES_OFFSET + msg_offset + vect_offset)
287
36
                                   & MVRP_NUMBER_OF_VALUES_MASK;
288
289
36
                vect_attr_len = 2 + attribute_length + (number_of_values + 2)/3; /* stores 3 values per byte */
290
291
36
                vect_attr_ti = proto_tree_add_item(attr_list_tree, hf_mvrp_vector_attribute, tvb,
292
36
                                                   MVRP_VECTOR_ATTRIBUTE_GROUP_OFFSET + msg_offset + vect_offset,
293
36
                                                   vect_attr_len, ENC_NA);
294
295
36
                vect_attr_tree = proto_item_add_subtree(vect_attr_ti, ett_vect_attr);
296
297
36
                dissect_mvrp_common2(vect_attr_tree, tvb, msg_offset + vect_offset);
298
299
36
                if (attribute_type == MVRP_ATTRIBUTE_TYPE_VID) {
300
                    /* MVRP VLAN ID FirstValue is a group of fields
301
                     *
302
                     * Contains VID (2 bytes)
303
                     *        bytes of data
304
                     */
305
13
                    first_value_ti = proto_tree_add_item(vect_attr_tree, hf_mvrp_first_value, tvb,
306
13
                                                         MVRP_FIRST_VALUE_GROUP_OFFSET + msg_offset + vect_offset,
307
13
                                                         attribute_length, ENC_NA);
308
13
                    first_value_tree = proto_item_add_subtree(first_value_ti, ett_first_value);
309
310
                    /* Add VLAN components to First Value tree */
311
13
                    proto_tree_add_item(first_value_tree, hf_mvrp_vid, tvb,
312
13
                                        MVRP_FIRST_VALUE_GROUP_OFFSET + msg_offset + vect_offset, 2, ENC_BIG_ENDIAN);
313
314
                    /* Decode three packed events. */
315
13
                    offset = dissect_mvrp_three_packed_event(vect_attr_tree, tvb,
316
13
                                                             MVRP_VID_THREE_PACKED_OFFSET + msg_offset + vect_offset,
317
13
                                                             number_of_values);
318
319
13
                }
320
321
36
                vect_offset += vect_attr_len; /* Move to next Vector Attribute, if there is one */
322
36
            } /* Multiple VectorAttribute while() */
323
324
30
            if (MVRP_VECTOR_HEADER_OFFSET + msg_offset + vect_offset < tvb_reported_length(tvb)) {
325
7
                proto_tree_add_item(attr_list_tree, hf_mvrp_end_mark, tvb, offset, 2, ENC_BIG_ENDIAN); /* VectorAttribute EndMark */
326
7
            }
327
328
30
            proto_item_set_len(attr_list_ti, vect_offset); /*without an endmark*/
329
30
            msg_offset += vect_offset + 4; /*  + endmark; Move to next Message, if there is one */
330
30
            proto_item_set_len(msg_ti, vect_offset + 2); /*length of message*/
331
332
30
        } /* Multiple Message while() */
333
334
33
        if (MVRP_ATTRIBUTE_TYPE_OFFSET + msg_offset < tvb_reported_length(tvb)) {
335
3
            proto_tree_add_item(mvrp_tree, hf_mvrp_end_mark, tvb, offset+2, 2, ENC_BIG_ENDIAN); /* Message EndMark */
336
3
        }
337
33
    }
338
33
    return tvb_captured_length(tvb);
339
33
}
340
341
342
/* Register the protocol with Wireshark */
343
void
344
proto_register_mrp_mvrp(void)
345
15
{
346
15
    static hf_register_info hf[] = {
347
15
        { &hf_mvrp_proto_id,
348
15
            { "Protocol Version",      "mrp-mvrp.protocol_version",
349
15
              FT_UINT8,  BASE_DEC, NULL, 0x0, NULL, HFILL }
350
15
        },
351
15
        { &hf_mvrp_message, /* Message is a group of fields */
352
15
            { "Message",               "mrp-mvrp.message",
353
15
              FT_NONE,  BASE_NONE, NULL, 0x0, NULL, HFILL }
354
15
        },
355
15
        { &hf_mvrp_attribute_type,
356
15
            { "Attribute Type",        "mrp-mvrp.attribute_type",
357
15
              FT_UINT8,  BASE_DEC, VALS(attribute_type_vals), 0x0, NULL, HFILL }
358
15
        },
359
15
        { &hf_mvrp_attribute_length,
360
15
            { "Attribute Length",      "mrp-mvrp.attribute_length",
361
15
              FT_UINT8,  BASE_DEC, NULL, 0x0, NULL, HFILL }
362
15
        },
363
15
        { &hf_mvrp_attribute_list, /* AttributeList is a group of fields */
364
15
            { "Attribute List",        "mrp-mvrp.attribute_list",
365
15
              FT_NONE,  BASE_NONE, NULL, 0x0, NULL, HFILL }
366
15
        },
367
15
        { &hf_mvrp_vector_attribute, /* VectorAttribute is a group of fields */
368
15
            { "Vector Attribute",      "mrp-mvrp.vector_attribute",
369
15
              FT_NONE,  BASE_NONE, NULL, 0x0, NULL, HFILL }
370
15
        },
371
15
        { &hf_mvrp_vector_header,
372
15
            { "Vector Header",         "mrp-mvrp.vector_header",
373
15
              FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }
374
15
        },
375
15
        { &hf_mvrp_leave_all_event,
376
15
            { "Leave All Event",       "mrp-mvrp.leave_all_event",
377
15
              FT_UINT16, BASE_DEC, VALS(leave_all_vals), MVRP_LEAVE_ALL_EVENT_MASK, NULL, HFILL }
378
15
        },
379
15
        { &hf_mvrp_number_of_values,
380
15
            { "Number of Values",      "mrp-mvrp.number_of_values",
381
15
              FT_UINT16, BASE_DEC, NULL, MVRP_NUMBER_OF_VALUES_MASK, NULL, HFILL }
382
15
        },
383
15
        { &hf_mvrp_first_value, /* FirstValue is a group of fields */
384
15
            { "First Value",           "mrp-mvrp.first_value",
385
15
              FT_NONE,  BASE_NONE, NULL, 0x0, NULL, HFILL }
386
15
        },
387
15
        { &hf_mvrp_vid,
388
15
            { "VLAN ID",               "mrp-mvrp.vid",
389
15
              FT_UINT16, BASE_DEC,  NULL, 0x0, NULL, HFILL }
390
15
        },
391
15
        { &hf_mvrp_three_packed_event,
392
15
            { "Attribute Event",       "mrp-mvrp.three_packed_event",
393
15
              FT_UINT8, BASE_DEC,  VALS(three_packed_vals), 0x0, NULL, HFILL }
394
15
        },
395
15
        { &hf_mvrp_end_mark,
396
15
            { "End Mark",              "mrp-mvrp.end_mark",
397
15
              FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }
398
15
        },
399
15
    };
400
401
    /* Setup protocol subtree array */
402
15
    static int *ett[] = {
403
15
        &ett_mvrp,
404
15
        &ett_msg,
405
15
        &ett_attr_list,
406
15
        &ett_vect_attr,
407
15
        &ett_vector_header,
408
15
        &ett_first_value
409
15
    };
410
411
    /* Register the protocol name and description */
412
15
    proto_mvrp = proto_register_protocol("Multiple VLAN Registration Protocol",
413
15
                                         "MRP-MVRP", "mrp-mvrp");
414
415
    /* Required function calls to register the header fields and subtrees used */
416
15
    proto_register_field_array(proto_mvrp, hf, array_length(hf));
417
15
    proto_register_subtree_array(ett, array_length(ett));
418
419
    /* Register the dissector */
420
15
    mvrp_handle = register_dissector("mrp-mvrp", dissect_mvrp, proto_mvrp);
421
15
}
422
423
void
424
proto_reg_handoff_mrp_mvrp(void)
425
15
{
426
15
    dissector_add_uint("ethertype", ETHERTYPE_MVRP, mvrp_handle);
427
15
}
428
/*
429
* Editor modelines - https://www.wireshark.org/tools/modelines.html
430
*
431
* Local variables:
432
* c-basic-offset: 4
433
* tab-width: 8
434
* indent-tabs-mode: nil
435
* End:
436
*
437
* ex: set shiftwidth=4 tabstop=8 expandtab:
438
* :indentSize=4:tabSize=8:noTabs=true:
439
*/