summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-btp-matter.c
blob: 254d00f82f69342d54eedc3d4ff49ad42111f6d0 (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
433
434
435
436
/* packet-btp-matter.c
 * Routines for Matter Bluetooth Transport Protocol (BTP) dissection
 * Copyright 2024, Arkadiusz Bokowy <a.bokowy@samsung.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/*
 * The dissector code is based on Matter Specification Version 1.3, section
 * 4.18. Bluetooth Transport Protocol (BTP).
 *
 * The specification is available at:
 * https://csa-iot.org/wp-content/uploads/2024/05/Matter-1.3-Core-Specification.pdf
 */

#include <config.h>

#include <epan/packet.h>
#include "packet-btatt.h"

void proto_register_btatt_matter(void);
void proto_reg_handoff_btatt_matter(void);

static int proto_matter_btp;
static dissector_handle_t matter_btp_handle;
static dissector_handle_t matter_tlv_handle;

static int hf_matter_btp_flags;
static int hf_matter_btp_flags_handshake;
static int hf_matter_btp_flags_management;
static int hf_matter_btp_flags_acknowledgment;
static int hf_matter_btp_flags_ending;
static int hf_matter_btp_flags_continuing;
static int hf_matter_btp_flags_beginning;
static int hf_matter_btp_opcode;
static int hf_matter_btp_versions;
static int hf_matter_btp_versions_0;
static int hf_matter_btp_versions_1;
static int hf_matter_btp_versions_2;
static int hf_matter_btp_versions_3;
static int hf_matter_btp_versions_4;
static int hf_matter_btp_versions_5;
static int hf_matter_btp_versions_6;
static int hf_matter_btp_versions_7;
static int hf_matter_btp_version;
static int hf_matter_btp_mtu;
static int hf_matter_btp_window_size;
static int hf_matter_btp_ack;
static int hf_matter_btp_seq;
static int hf_matter_btp_length;
static int hf_matter_btp_payload;
static int hf_matter_btp_ad;
static int hf_matter_btp_ad_tlv_tag;

static int ett_matter_btp;
static int ett_matter_btp_flags;
static int ett_matter_btp_versions;
static int ett_matter_btp_ad;

// Section 4.18.2.1
#define MATTER_BTP_FLAGS_HANDSHAKE      0x40
#define MATTER_BTP_FLAGS_MANAGEMENT     0x20
#define MATTER_BTP_FLAGS_ACKNOWLEDGMENT 0x08
#define MATTER_BTP_FLAGS_ENDING         0x04
#define MATTER_BTP_FLAGS_CONTINUING     0x02
#define MATTER_BTP_FLAGS_BEGINNING      0x01

// Section 4.18.3.1
#define MATTER_BTP_OPCODE_HANDSHAKE     0x6C

// Section 5.4.2.4.4
#define MATTER_BTP_AD_TAG_ROTATING_ID   0x00

// Section 4.18.4.2
#define MATTER_GATT_SRV_UUID            0xFFF6
// 18EE2EF5-263D-4559-959F-4F9C429F9D11, Client TX Buffer, Write
#define MATTER_GATT_CHR_TX_UUID_128     "\x18\xee\x2e\xf5\x26\x3d\x45\x59\x95\x9f\x4f\x9c\x42\x9f\x9d\x11"
// 18EE2EF5-263D-4559-959F-4F9C429F9D12, Server RX Buffer, Indication
#define MATTER_GATT_CHR_RX_UUID_128     "\x18\xee\x2e\xf5\x26\x3d\x45\x59\x95\x9f\x4f\x9c\x42\x9f\x9d\x12"
// 64630238-8772-45F2-B87D-748A83218F04, Additional Data, Read
#define MATTER_GATT_CHR_AD_UUID_128     "\x64\x63\x02\x38\x87\x72\x45\xf2\xb8\x7d\x74\x8a\x83\x21\x8f\x04"

static const value_string btp_opcode_vals[] = {
    { MATTER_BTP_OPCODE_HANDSHAKE, "Handshake" },
    { 0, NULL }
};

static const value_string btp_ad_tag_vals[] = {
    { MATTER_BTP_AD_TAG_ROTATING_ID, "Rotating Device Identifier" },
    { 0, NULL }
};

// Dissect the Additional Data characteristic using Matter-defined TLV encoding.
static int
dissect_matter_chr_ad_tlv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int offset, int length)
{

    col_append_str(pinfo->cinfo, COL_INFO, " Additional Data");
    proto_item *item = proto_tree_add_item(tree, hf_matter_btp_ad, tvb, offset, length, ENC_NA);
    proto_tree *subtree = proto_item_add_subtree(item, ett_matter_btp_ad);

    call_dissector_with_data(matter_tlv_handle, tvb, pinfo, subtree, &hf_matter_btp_ad_tlv_tag);

    return length;
}

static int
dissect_matter_btp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *btatt_tree, void *data)
{
    btatt_data_t *att_data = (btatt_data_t *) data;
    uint64_t flags = 0;
    uint32_t opcode = 0;
    int offset = 0;

    DISSECTOR_ASSERT(att_data);
    bluetooth_data_t *bluetooth_data = att_data->bluetooth_data;
    const uint8_t att_opcode = att_data->opcode;
    const uint32_t att_handle = att_data->handle;

    // We are only interested in read, write and indication packets.
    if (att_opcode != ATT_OPCODE_READ_RESPONSE &&
            att_opcode != ATT_OPCODE_WRITE_REQUEST &&
            att_opcode != ATT_OPCODE_HANDLE_VALUE_INDICATION)
        return 0;

    /* Get UUID for current ATT handle. */
    bluetooth_uuid_t uuid = get_gatt_bluetooth_uuid_from_handle(pinfo, att_handle, att_opcode, bluetooth_data);
    /* Verify that the UUID belongs to the Matter GATT service and bail otherwise. */
    if (uuid.size != 16 || (
            memcmp(uuid.data, MATTER_GATT_CHR_TX_UUID_128, 16) != 0 &&
            memcmp(uuid.data, MATTER_GATT_CHR_RX_UUID_128, 16) != 0 &&
            memcmp(uuid.data, MATTER_GATT_CHR_AD_UUID_128, 16) != 0))
        return 0;

    /* Check that the packet is long enough to contain BTP flags byte. */
    if (tvb_reported_length(tvb) < 1)
        return 0;

    col_set_str(pinfo->cinfo, COL_PROTOCOL, "Matter BTP");

    switch (pinfo->p2p_dir) {
        case P2P_DIR_SENT:
            col_set_str(pinfo->cinfo, COL_INFO, "Sent ");
            break;
        case P2P_DIR_RECV:
            col_set_str(pinfo->cinfo, COL_INFO, "Rcvd ");
            break;
        default:
            col_set_str(pinfo->cinfo, COL_INFO, "UnknownDirection ");
            break;
    }

    col_append_str(pinfo->cinfo, COL_INFO, "Matter BTP");
    col_append_fstr(pinfo->cinfo, COL_INFO, " [Handle: 0x%04x]", att_handle);

    /* Add Matter Bluetooth Transport Protocol as a root subtree. */
    proto_item *root = proto_item_get_parent(btatt_tree);
    proto_item *item = proto_tree_add_item(root, proto_matter_btp, tvb, offset, -1, ENC_NA);
    proto_tree *tree = proto_item_add_subtree(item, ett_matter_btp);

    // The payload format of the Additional Data characteristic is different
    // than the standard BTP packet. It uses Matter-defined TLV encoding.
    // Section 5.4.2.4.4
    if (memcmp(uuid.data, MATTER_GATT_CHR_AD_UUID_128, 16) == 0)
        return dissect_matter_chr_ad_tlv(tvb, pinfo, tree, offset, tvb_reported_length(tvb));

    static int * const btp_flags[] = {
        &hf_matter_btp_flags_beginning,
        &hf_matter_btp_flags_continuing,
        &hf_matter_btp_flags_ending,
        &hf_matter_btp_flags_acknowledgment,
        &hf_matter_btp_flags_management,
        &hf_matter_btp_flags_handshake,
        NULL
    };

    static int * const btp_versions[] = {
        &hf_matter_btp_versions_0,
        &hf_matter_btp_versions_1,
        &hf_matter_btp_versions_2,
        &hf_matter_btp_versions_3,
        &hf_matter_btp_versions_4,
        &hf_matter_btp_versions_5,
        &hf_matter_btp_versions_6,
        &hf_matter_btp_versions_7,
        NULL
    };

    proto_tree_add_bitmask_ret_uint64(tree, tvb, offset, hf_matter_btp_flags, ett_matter_btp_flags, btp_flags, ENC_NA, &flags);
    offset += 1;

    if (flags & MATTER_BTP_FLAGS_MANAGEMENT) {
        proto_tree_add_item_ret_uint(tree, hf_matter_btp_opcode, tvb, offset, 1, ENC_NA, &opcode);
        offset += 1;
    }

    // The handshake packet format is different than standard BTP packets.
    // Section 4.18.3
    if (flags & MATTER_BTP_FLAGS_HANDSHAKE) {

        if (opcode & MATTER_BTP_OPCODE_HANDSHAKE) {

            // Section 4.18.3.1. BTP Handshake Request
            if (memcmp(uuid.data, MATTER_GATT_CHR_TX_UUID_128, 16) == 0) {

                proto_tree_add_bitmask(tree, tvb, offset, hf_matter_btp_versions, ett_matter_btp_versions, btp_versions, ENC_NA);
                offset += 4;
                proto_tree_add_item(tree, hf_matter_btp_mtu, tvb, offset, 2, ENC_LITTLE_ENDIAN);
                offset += 2;
                proto_tree_add_item(tree, hf_matter_btp_window_size, tvb, offset, 1, ENC_NA);
                offset += 1;

                col_append_str(pinfo->cinfo, COL_INFO, " Handshake Request");
            }

            // Section 4.18.3.2. BTP Handshake Response
            if (memcmp(uuid.data, MATTER_GATT_CHR_RX_UUID_128, 16) == 0) {

                uint32_t version;
                proto_tree_add_item_ret_uint(tree, hf_matter_btp_version, tvb, offset, 1, ENC_NA, &version);
                offset += 1;

                uint32_t mtu;
                proto_tree_add_item_ret_uint(tree, hf_matter_btp_mtu, tvb, offset, 2, ENC_LITTLE_ENDIAN, &mtu);
                offset += 2;

                uint32_t window_size;
                proto_tree_add_item_ret_uint(tree, hf_matter_btp_window_size, tvb, offset, 1, ENC_NA, &window_size);
                offset += 1;

                col_append_fstr(pinfo->cinfo, COL_INFO, " Handshake Response, Version: %u, MTU: %u, Window Size: %u",
                        version, mtu, window_size);

            }

        }

        return offset;
    }

    // Mark the packet as a segment of a BTP Service Data Unit.
    col_append_str(pinfo->cinfo, COL_INFO, " SDU Segment");

    if (flags & MATTER_BTP_FLAGS_ACKNOWLEDGMENT) {
        uint32_t ack;
        proto_tree_add_item_ret_uint(tree, hf_matter_btp_ack, tvb, offset, 1, ENC_NA, &ack);
        col_append_fstr(pinfo->cinfo, COL_INFO, ", Ack: %u", ack);
        offset += 1;
    }

    // All BTP packets SHALL be sent with sequence numbers.
    // Section 4.18.4.6
    if (flags & (MATTER_BTP_FLAGS_BEGINNING | MATTER_BTP_FLAGS_CONTINUING | MATTER_BTP_FLAGS_ENDING)) {
        uint32_t seq;
        proto_tree_add_item_ret_uint(tree, hf_matter_btp_seq, tvb, offset, 1, ENC_NA, &seq);
        col_append_fstr(pinfo->cinfo, COL_INFO, ", Seq: %u", seq);
        offset += 1;
    }

    // Message length is an optional field present in the Beginning Segment only.
    // Section 4.18.2.4
    if (flags & MATTER_BTP_FLAGS_BEGINNING) {
        uint32_t length;
        proto_tree_add_item_ret_uint(tree, hf_matter_btp_length, tvb, offset, 2, ENC_LITTLE_ENDIAN, &length);
        col_append_fstr(pinfo->cinfo, COL_INFO, ", Length: %u", length);
        offset += 2;
    }

    proto_tree_add_item(tree, hf_matter_btp_payload, tvb, offset, -1, ENC_NA);

    return tvb_captured_length(tvb);
}

void
proto_register_btatt_matter(void)
{
    static hf_register_info hf[] = {
        {&hf_matter_btp_flags,
            {"Flags", "btp-matter.flags",
            FT_UINT8, BASE_HEX, NULL, 0x0,
            "Bluetooth Transport Protocol control flags", HFILL}
        },
        {&hf_matter_btp_flags_handshake,
            {"Handshake", "btp-matter.flags.handshake",
            FT_BOOLEAN, 8, NULL, MATTER_BTP_FLAGS_HANDSHAKE,
            "BTP handshake packet for session establishment", HFILL}
        },
        {&hf_matter_btp_flags_management,
            {"Management", "btp-matter.flags.management",
            FT_BOOLEAN, 8, NULL, MATTER_BTP_FLAGS_MANAGEMENT,
            "Management message with the opcode field", HFILL}
        },
        {&hf_matter_btp_flags_acknowledgment,
            {"Acknowledgment", "btp-matter.flags.ack",
            FT_BOOLEAN, 8, NULL, MATTER_BTP_FLAGS_ACKNOWLEDGMENT,
            "Indicates the presence of the ack number field", HFILL}
        },
        {&hf_matter_btp_flags_ending,
            {"Ending", "btp-matter.flags.ending",
            FT_BOOLEAN, 8, NULL, MATTER_BTP_FLAGS_ENDING,
            "The last segment of a BTP Service Data Unit", HFILL}
        },
        {&hf_matter_btp_flags_continuing,
            {"Continuing", "btp-matter.flags.continuing",
            FT_BOOLEAN, 8, NULL, MATTER_BTP_FLAGS_CONTINUING,
            "The continuation of a BTP Service Data Unit", HFILL}
        },
        {&hf_matter_btp_flags_beginning,
            {"Beginning", "btp-matter.flags.beginning",
            FT_BOOLEAN, 8, NULL, MATTER_BTP_FLAGS_BEGINNING,
            "The first segment of a BTP Service Data Unit", HFILL}
        },
        {&hf_matter_btp_opcode,
            {"Management Opcode", "btp-matter.opcode",
            FT_UINT8, BASE_HEX, VALS(btp_opcode_vals), 0x0,
            NULL, HFILL}
        },
        {&hf_matter_btp_versions,
            {"Supported BTP versions", "btp-matter.versions",
            FT_UINT32, BASE_HEX, NULL, 0x0,
            "The list of BTP versions supported by the client", HFILL}
        },
        {&hf_matter_btp_versions_0,
            {"Version", "btp-matter.versions.0",
            FT_UINT32, BASE_DEC, NULL, 0x0F000000,
            NULL, HFILL}
        },
        {&hf_matter_btp_versions_1,
            {"Version", "btp-matter.versions.1",
            FT_UINT32, BASE_DEC, NULL, 0xF0000000,
            NULL, HFILL}
        },
        {&hf_matter_btp_versions_2,
            {"Version", "btp-matter.versions.2",
            FT_UINT32, BASE_DEC, NULL, 0x000F0000,
            NULL, HFILL}
        },
        {&hf_matter_btp_versions_3,
            {"Version", "btp-matter.versions.3",
            FT_UINT32, BASE_DEC, NULL, 0x00F00000,
            NULL, HFILL}
        },
        {&hf_matter_btp_versions_4,
            {"Version", "btp-matter.versions.4",
            FT_UINT32, BASE_DEC, NULL, 0x00000F00,
            NULL, HFILL}
        },
        {&hf_matter_btp_versions_5,
            {"Version", "btp-matter.versions.5",
            FT_UINT32, BASE_DEC, NULL, 0x0000F000,
            NULL, HFILL}
        },
        {&hf_matter_btp_versions_6,
            {"Version", "btp-matter.versions.6",
            FT_UINT32, BASE_DEC, NULL, 0x0000000F,
            NULL, HFILL}
        },
        {&hf_matter_btp_versions_7,
            {"Version", "btp-matter.versions.7",
            FT_UINT32, BASE_DEC, NULL, 0x000000F0,
            NULL, HFILL}
        },
        {&hf_matter_btp_version,
            {"Version", "btp-matter.version",
            FT_UINT8, BASE_DEC, NULL, 0x0F,
            "The BTP protocol version selected by the server", HFILL}
        },
        {&hf_matter_btp_mtu,
            {"MTU", "btp-matter.mtu",
            FT_UINT16, BASE_DEC, NULL, 0x0,
            "Requested or selected MTU for the connection", HFILL}
        },
        {&hf_matter_btp_window_size,
            {"Window Size", "btp-matter.window",
            FT_UINT8, BASE_DEC, NULL, 0x0,
            "Requested or selected maximum receive window size, in units of BTP packets", HFILL}
        },
        {&hf_matter_btp_ack,
            {"Acknowledgment", "btp-matter.ack",
            FT_UINT8, BASE_DEC, NULL, 0x0,
            "The acknowledgement of the previous sequence number", HFILL}
        },
        {&hf_matter_btp_seq,
            {"Sequence Number", "btp-matter.seq",
            FT_UINT8, BASE_DEC, NULL, 0x0,
            "The monotonically increasing sequence number", HFILL}
        },
        {&hf_matter_btp_length,
            {"Length", "btp-matter.length",
            FT_UINT16, BASE_DEC, NULL, 0x0,
            "The payload length, in bytes", HFILL}
        },
        {&hf_matter_btp_payload,
            {"Payload", "btp-matter.payload",
            FT_BYTES, BASE_NONE, NULL, 0x0,
            "The segment of the Service Data Unit message", HFILL}
        },
        {&hf_matter_btp_ad,
            {"Additional Data", "btp-matter.ad",
            FT_NONE, BASE_NONE, NULL, 0x0,
            "Additional commissioning-related data", HFILL}
        },
        {&hf_matter_btp_ad_tlv_tag,
            {"Tag", "btp-matter.ad.item",
            FT_UINT8, BASE_HEX, VALS(btp_ad_tag_vals), 0x0,
            NULL, HFILL}
        },
    };

    /* Setup protocol subtree array */
    static int *ett[] = {
        &ett_matter_btp,
        &ett_matter_btp_flags,
        &ett_matter_btp_versions,
        &ett_matter_btp_ad,
    };

    /* Register the protocol name and description */
    proto_matter_btp = proto_register_protocol("Matter Bluetooth Transport Protocol", "MatterBTP", "btp-matter");
    matter_btp_handle = register_dissector("btp-matter", dissect_matter_btp, proto_matter_btp);

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

void
proto_reg_handoff_btatt_matter(void)
{
    matter_tlv_handle = find_dissector_add_dependency("matter.tlv", proto_matter_btp);
    dissector_add_uint("btatt.service", MATTER_GATT_SRV_UUID, matter_btp_handle);
}