summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-mctp.c
blob: 5204f30e25b5fcff467c294eadc179fa2f7b4097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* packet-mctp.c
 * Routines for Management Component Transport Protocol (MCTP) packet
 * disassembly
 * Copyright 2022, Jeremy Kerr <jk@codeconstruct.com.au>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/*
 * MCTP is a datagram-based protocol for intra-platform communication,
 * typically between a management controller and system devices.
 *
 * MCTP is defined by DMTF standard DSP0236: https://www.dmtf.org/dsp/DSP0236
 */

#include <config.h>

#include <epan/packet.h>
#include <epan/reassemble.h>
#include <epan/to_str.h>
#include "packet-mctp.h"
#include "packet-sll.h"

#define MCTP_MIN_LENGTH 5       /* 4-byte header, plus message type */

void proto_register_mctp(void);
void proto_reg_handoff_mctp(void);

static int proto_mctp = -1;

static int hf_mctp_ver = -1;
static int hf_mctp_dst = -1;
static int hf_mctp_src = -1;
static int hf_mctp_flags = -1;
static int hf_mctp_flags_som = -1;
static int hf_mctp_flags_eom = -1;
static int hf_mctp_seq = -1;
static int hf_mctp_tag = -1;
static int hf_mctp_tag_to = -1;
static int hf_mctp_tag_value = -1;
static int hf_mctp_msg_ic = -1;
static int hf_mctp_msg_type = -1;

static gint ett_mctp = -1;
static gint ett_mctp_fst = -1;
static gint ett_mctp_flags = -1;
static gint ett_mctp_tag = -1;
static gint ett_mctp_type = -1;

static const true_false_string tfs_tag_to = { "Sender", "Receiver" };

static int hf_mctp_fragments = -1;
static int hf_mctp_fragment = -1;
static int hf_mctp_fragment_overlap = -1;
static int hf_mctp_fragment_overlap_conflicts = -1;
static int hf_mctp_fragment_multiple_tails = -1;
static int hf_mctp_fragment_too_long_fragment = -1;
static int hf_mctp_fragment_error = -1;
static int hf_mctp_fragment_count = -1;
static int hf_mctp_reassembled_in = -1;
static int hf_mctp_reassembled_length = -1;
static int hf_mctp_reassembled_data = -1;

static gint ett_mctp_fragment = -1;
static gint ett_mctp_fragments = -1;

static const fragment_items mctp_frag_items = {
    /* Fragment subtrees */
    &ett_mctp_fragment,
    &ett_mctp_fragments,
    /* Fragment fields */
    &hf_mctp_fragments,
    &hf_mctp_fragment,
    &hf_mctp_fragment_overlap,
    &hf_mctp_fragment_overlap_conflicts,
    &hf_mctp_fragment_multiple_tails,
    &hf_mctp_fragment_too_long_fragment,
    &hf_mctp_fragment_error,
    &hf_mctp_fragment_count,
    /* "Reassembled in" field */
    &hf_mctp_reassembled_in,
    /* Reassembled length field */
    &hf_mctp_reassembled_length,
    &hf_mctp_reassembled_data,
    /* Tag */
    "Message fragments"
};

static const value_string flag_vals[] = {
    { 0x00, "none" },
    { 0x01, "EOM" },
    { 0x02, "SOM" },
    { 0x03, "SOM|EOM" },
    { 0x00, NULL },
};

static const value_string type_vals[] = {
    { MCTP_TYPE_CONTROL, "MCTP Control Protocol" },
    { MCTP_TYPE_PLDM, "PLDM" },
    { MCTP_TYPE_NCSI, "NC-SI" },
    { MCTP_TYPE_ETHERNET, "Ethernet" },
    { MCTP_TYPE_NVME, "NVMe-MI" },
    { 0, NULL },
};

static dissector_table_t mctp_dissector_table;
static dissector_table_t mctp_encap_dissector_table;
static reassembly_table mctp_reassembly_table;

static int
dissect_mctp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
        void *data _U_)
{
    proto_tree *mctp_tree, *fst_tree;
    guint len, ver, type, seq, fst;
    bool save_fragmented;
    proto_item *ti, *tti;
    tvbuff_t *next_tvb;
    guint8 tag;

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "MCTP");
    col_clear(pinfo->cinfo, COL_INFO);

    /* Check that the packet is long enough for it to belong to us. */
    len = tvb_reported_length(tvb);

    if (len < MCTP_MIN_LENGTH) {
        col_add_fstr(pinfo->cinfo, COL_INFO, "Bogus length %u, minimum %u",
                     len, MCTP_MIN_LENGTH);
        return tvb_captured_length(tvb);
    }

    ver = tvb_get_bits8(tvb, 4, 4);
    if (ver != 1) {
        col_add_fstr(pinfo->cinfo, COL_INFO, "Invalid version %u", ver);
        return tvb_captured_length(tvb);
    }

    /* Top-level protocol item & tree */
    ti = proto_tree_add_item(tree, proto_mctp, tvb, 0, 4, ENC_NA);
    mctp_tree = proto_item_add_subtree(ti, ett_mctp);

    set_address_tvb(&pinfo->dl_dst, AT_MCTP, 1, tvb, 1);
    set_address_tvb(&pinfo->dl_src, AT_MCTP, 1, tvb, 2);
    copy_address_shallow(&pinfo->dst, &pinfo->dl_dst);
    copy_address_shallow(&pinfo->src, &pinfo->dl_src);

    proto_item_append_text(ti, " Dst: %s, Src %s",
            address_to_str(pinfo->pool, &pinfo->dst),
            address_to_str(pinfo->pool, &pinfo->src));

    /* Standard header fields */
    proto_tree_add_item(mctp_tree, hf_mctp_ver, tvb, 0, 1, ENC_NA);
    proto_tree_add_item(mctp_tree, hf_mctp_dst, tvb, 1, 1, ENC_NA);
    proto_tree_add_item(mctp_tree, hf_mctp_src, tvb, 2, 1, ENC_NA);

    static int * const mctp_flags[] = {
        &hf_mctp_flags_som,
        &hf_mctp_flags_eom,
        NULL
    };

    static int * const mctp_tag[] = {
        &hf_mctp_tag_to,
        &hf_mctp_tag_value,
        NULL,
    };

    fst = tvb_get_guint8(tvb, 3);
    tag = fst & 0x0f;
    fst_tree = proto_tree_add_subtree_format(mctp_tree, tvb, 3, 1, ett_mctp_fst,
                                      &tti, "Flags %s, seq %d, tag %s%d",
                                      val_to_str_const(fst >> 6, flag_vals, ""),
                                      fst >> 4 & 0x3,
                                      fst & 0x08 ? "TO:" : "",
                                      fst & 0x7);
    proto_tree_add_bitmask(fst_tree, tvb, 3, hf_mctp_flags,
                           ett_mctp_flags, mctp_flags, ENC_NA);
    proto_tree_add_item_ret_uint(fst_tree, hf_mctp_seq, tvb, 3, 1, ENC_NA, &seq);
    proto_tree_add_bitmask_with_flags(fst_tree, tvb, 3, hf_mctp_tag,
                           ett_mctp_tag, mctp_tag, ENC_NA, BMT_NO_FLAGS);

    /* use the tags as our port numbers */
    pinfo->ptype = PT_MCTP;
    pinfo->srcport = tag;
    pinfo->destport = tag ^ 0x08; /* flip tag-owner bit */

    save_fragmented = pinfo->fragmented;

    col_set_str(pinfo->cinfo, COL_INFO, "MCTP message");

    /* if we're not both the start and end of a message, handle as a
     * fragment */
    if ((fst & 0xc0) != 0xc0) {
        fragment_head *frag_msg = NULL;
        tvbuff_t *new_tvb = NULL;

        pinfo->fragmented = true;
        frag_msg = fragment_add_seq_next(&mctp_reassembly_table,
                                         tvb, 4, pinfo,
                                         fst & 0x7, NULL,
                                         tvb_captured_length_remaining(tvb, 4),
                                         !(fst & 0x40));

        new_tvb = process_reassembled_data(tvb, 4, pinfo,
                                           "reassembled Message",
                                           frag_msg, &mctp_frag_items,
                                           NULL, mctp_tree);

        if (fst & 0x40)
            col_append_str(pinfo->cinfo, COL_INFO, " reassembled");
        else
            col_append_fstr(pinfo->cinfo, COL_INFO, " frag %u", seq);

        next_tvb = new_tvb;
    } else {
        next_tvb = tvb_new_subset_remaining(tvb, 4);
    }

    if (next_tvb) {
        proto_tree *type_tree;
        int rc;

        type = tvb_get_guint8(next_tvb, 0);
        type_tree = proto_tree_add_subtree_format(mctp_tree, next_tvb, 0, 1,
                                                  ett_mctp_type,
                                                  &tti, "Type: %s (0x%x)%s",
                                                  val_to_str_const(type & 0x7f,
                                                                   type_vals,
                                                                   "unknown"),
                                                  type & 0x7f,
                                                  type & 0x80 ? " + IC" : "");

        proto_tree_add_item(type_tree, hf_mctp_msg_type, next_tvb, 0, 1,
                            ENC_NA);
        proto_tree_add_item(type_tree, hf_mctp_msg_ic, next_tvb, 0, 1,
                            ENC_NA);

        rc = dissector_try_uint_new(mctp_dissector_table, type & 0x7f,
                                    next_tvb, pinfo, tree, true, NULL);

        if (!rc && !(type & 0x80)) {
            tvbuff_t *encap_tvb = tvb_new_subset_remaining(next_tvb, 1);
            dissector_try_uint_new(mctp_encap_dissector_table, type,
                                   encap_tvb, pinfo, tree, true, NULL);
        }
    }

    pinfo->fragmented = save_fragmented;

    return tvb_captured_length(tvb);
}

void
proto_register_mctp(void)
{
    /* *INDENT-OFF* */
    /* Field definitions */
    static hf_register_info hf[] = {
        { &hf_mctp_ver,
          { "Version", "mctp.version",
            FT_UINT8, BASE_DEC, NULL, 0x0f,
            NULL, HFILL },
        },
        { &hf_mctp_dst,
          { "Destination", "mctp.dst",
            FT_UINT8, BASE_DEC, NULL, 0x00,
            NULL, HFILL },
        },
        { &hf_mctp_src,
          { "Source", "mctp.src",
            FT_UINT8, BASE_DEC, NULL, 0x00,
            NULL, HFILL },
        },
        { &hf_mctp_flags,
          { "Flags", "mctp.flags",
            FT_UINT8, BASE_HEX, NULL, 0xc0,
            NULL, HFILL },
        },
        { &hf_mctp_flags_som,
          { "Start of message", "mctp.flags.som",
            FT_BOOLEAN, 8, TFS(&tfs_set_notset), 0x80,
            NULL, HFILL },
        },
        { &hf_mctp_flags_eom,
          { "End of message", "mctp.flags.eom",
            FT_BOOLEAN, 8, TFS(&tfs_set_notset), 0x40,
            NULL, HFILL },
        },
        { &hf_mctp_seq,
          { "Sequence", "mctp.seq",
            FT_UINT8, BASE_HEX, NULL, 0x30,
            NULL, HFILL },
        },
        { &hf_mctp_tag,
          { "Tag", "mctp.tag",
            FT_UINT8, BASE_HEX, NULL, 0x0f,
            NULL, HFILL },
        },
        { &hf_mctp_tag_to,
          { "Tag owner", "mctp.tag.to",
            FT_BOOLEAN, 8, TFS(&tfs_tag_to), 0x08,
            NULL, HFILL },
        },
        { &hf_mctp_tag_value,
          { "Tag value", "mctp.tag.value",
            FT_UINT8, BASE_HEX, NULL, 0x07,
            NULL, HFILL },
        },

        /* message header */
        { &hf_mctp_msg_ic,
          { "Integrity check", "mctp.msg.ic",
            FT_BOOLEAN, 8, TFS(&tfs_present_absent), 0x80,
            NULL, HFILL },
        },
        { &hf_mctp_msg_type,
          { "Message type", "mctp.msg.type",
            FT_UINT8, BASE_HEX, VALS(type_vals), 0x7f,
            NULL, HFILL },
        },

        /* generic fragmentation */
        {&hf_mctp_fragments,
            {"Message fragments", "mctp.fragments",
                FT_NONE, BASE_NONE, NULL, 0x00, NULL, HFILL } },
        {&hf_mctp_fragment,
            {"Message fragment", "mctp.fragment",
                FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
        {&hf_mctp_fragment_overlap,
            {"Message fragment overlap", "mctp.fragment.overlap",
                FT_BOOLEAN, 0, NULL, 0x00, NULL, HFILL } },
        {&hf_mctp_fragment_overlap_conflicts,
            {"Message fragment overlapping with conflicting data",
                "mctp.fragment.overlap.conflicts",
                FT_BOOLEAN, 0, NULL, 0x00, NULL, HFILL } },
        {&hf_mctp_fragment_multiple_tails,
            {"Message has multiple tail fragments",
                "mctp.fragment.multiple_tails",
                FT_BOOLEAN, 0, NULL, 0x00, NULL, HFILL } },
        {&hf_mctp_fragment_too_long_fragment,
            {"Message fragment too long", "mctp.fragment.too_long_fragment",
                FT_BOOLEAN, 0, NULL, 0x00, NULL, HFILL } },
        {&hf_mctp_fragment_error,
            {"Message defragmentation error", "mctp.fragment.error",
                FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
        {&hf_mctp_fragment_count,
            {"Message fragment count", "mctp.fragment.count",
                FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL } },
        {&hf_mctp_reassembled_in,
            {"Reassembled in", "mctp.reassembled.in",
                FT_FRAMENUM, BASE_NONE, NULL, 0x00, NULL, HFILL } },
        {&hf_mctp_reassembled_length,
            {"Reassembled length", "mctp.reassembled.length",
                FT_UINT32, BASE_DEC, NULL, 0x00, NULL, HFILL } },
        {&hf_mctp_reassembled_data,
            {"Reassembled data", "mctp.reassembled.data",
                FT_BYTES, SEP_SPACE, NULL, 0x00, NULL, HFILL } },
    };

    /* protocol subtree */
    static gint *ett[] = {
        &ett_mctp,
        &ett_mctp_flags,
        &ett_mctp_fst,
        &ett_mctp_tag,
        &ett_mctp_type,
        &ett_mctp_fragment,
        &ett_mctp_fragments,
    };

    /* Register the protocol name and description */
    proto_mctp = proto_register_protocol("MCTP", "MCTP", "mctp");

    /* Required function calls to register the header fields and subtrees */
    proto_register_field_array(proto_mctp, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    /* We have two dissector tables here, both keyed off the type byte, but
     * with different decode semantics:
     *
     * mctp.type: for protocols that are "MCTP-aware" - they perform their
     *    own decoding of the type byte, including the IC bit, and possibly the
     *    message integrity check (which is type-specific!). For example,
     *    NVMe-MI, which includes the type byte in packet specifications
     *
     * mctp.encap-type: for procotols that are trivially encapsulated in a
     *    MCTP message, and do not handle the type byte themselves. For
     *    example, NC-SI over MCTP, which just wraps a NC-SI packet within
     *    a MCTP message.
     *
     * it doesn't make sense to allow encap-type decoders to also have the IC
     * bit set, as there is no specification for what format the message
     * integrity check is in. So, we disallow the IC bit in the type field
     * for those dissectors.
     */
    mctp_dissector_table = register_dissector_table("mctp.type", "MCTP type",
                                                    proto_mctp, FT_UINT8,
                                                    BASE_HEX);
    mctp_encap_dissector_table = register_dissector_table("mctp.encap-type",
                                                          "MCTP encapsulated type",
                                                          proto_mctp, FT_UINT8,
                                                          BASE_HEX);

    reassembly_table_register(&mctp_reassembly_table,
                              &addresses_reassembly_table_functions);
}

void
proto_reg_handoff_mctp(void)
{
    dissector_handle_t mctp_handle;
    mctp_handle = create_dissector_handle(dissect_mctp, proto_mctp);
    dissector_add_uint("sll.ltype", LINUX_SLL_P_MCTP, mctp_handle);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */