Coverage Report

Created: 2026-01-02 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wireshark/epan/dissectors/packet-opus.c
Line
Count
Source
1
/* packet-opus.c
2
 * Routines for OPUS dissection
3
 * Copyright 2014, Owen Williams williams.owen@gmail.com
4
 *
5
 * Wireshark - Network traffic analyzer
6
 * By Gerald Combs <gerald@wireshark.org>
7
 * Copyright 1998 Gerald Combs
8
 *
9
 * SPDX-License-Identifier: GPL-2.0-or-later
10
 */
11
12
#include "config.h"
13
#include "tvbuff.h"
14
15
#include <epan/packet.h>
16
#include <epan/prefs.h>
17
#include <epan/expert.h>
18
#include <epan/unit_strings.h>
19
#include <epan/tfs.h>
20
#include <wsutil/array.h>
21
22
#include <ftypes/ftypes.h>
23
#include <proto.h>
24
#include <stdint.h>
25
26
0
#define MAX_FRAMES_COUNT 48
27
28
void proto_reg_handoff_opus(void);
29
void proto_register_opus(void);
30
31
static dissector_handle_t opus_handle;
32
33
/* Initialize the protocol and registered fields */
34
static int proto_opus;
35
static int hf_opus_toc_config;
36
static int hf_opus_toc_s;
37
static int hf_opus_toc_c;
38
static int hf_opus_frame;
39
static int hf_opus_frame_size;
40
static int hf_opus_frame_count_v;
41
static int hf_opus_frame_count_p;
42
static int hf_opus_frame_count_m;
43
static int hf_opus_padding;
44
static int hf_opus_padding_size;
45
46
/* Initialize the subtree pointers */
47
static int ett_opus;
48
49
static expert_field ei_opus_err_r1;
50
static expert_field ei_opus_err_r2;
51
static expert_field ei_opus_err_r3;
52
static expert_field ei_opus_err_r4;
53
static expert_field ei_opus_err_r5;
54
static expert_field ei_opus_err_r6;
55
static expert_field ei_opus_err_r7;
56
static expert_field ei_opus_padding_nonzero;
57
58
/* From RFC6716 chapter 3.1
59
 * The top five bits of the TOC byte, labeled "config", encode one of 32
60
 * possible configurations of operating mode, audio bandwidth, and frame size.
61
 */
62
static const value_string opus_codec_toc_config_request_vals[] = {
63
    {0, "NB, SILK-only ptime=10"},
64
    {1, "NB, SILK-only ptime=20"},
65
    {2, "NB, SILK-only ptime=40"},
66
    {3, "NB, SILK-only ptime=60"},
67
    {4, "MB, SILK-only ptime=10"},
68
    {5, "MB, SILK-only ptime=20"},
69
    {6, "MB, SILK-only ptime=40"},
70
    {7, "MB, SILK-only ptime=60"},
71
    {8, "WB, SILK-only ptime=10"},
72
    {9, "WB, SILK-only ptime=20"},
73
    {10, "WB, SILK-only ptime=40"},
74
    {11, "WB, SILK-only ptime=60"},
75
    {12, "SWB, Hybrid ptime=10"},
76
    {13, "SWB, Hybrid ptime=20"},
77
    {14, "FB, Hybrid ptime=10"},
78
    {15, "FB, Hybrid ptime=20"},
79
    {16, "NB, CELT-only ptime=2.5"},
80
    {17, "NB, CELT-only ptime=5"},
81
    {18, "NB, CELT-only ptime=10"},
82
    {19, "NB, CELT-only ptime=20"},
83
    {20, "WB, CELT-only ptime=2.5"},
84
    {21, "WB, CELT-only ptime=5"},
85
    {22, "WB, CELT-only ptime=10"},
86
    {23, "WB, CELT-only ptime=20"},
87
    {24, "SWB, CELT-only ptime=2.5"},
88
    {25, "SWB, CELT-only ptime=5"},
89
    {26, "SWB, CELT-only ptime=10"},
90
    {27, "SWB, CELT-only ptime=20"},
91
    {28, "FB, CELT-only ptime=2.5"},
92
    {29, "FB, CELT-only ptime=5"},
93
    {30, "FB, CELT-only ptime=10"},
94
    {31, "FB, CELT-only ptime=20"},
95
    {0, NULL}};
96
static value_string_ext opus_codec_toc_config_request_vals_ext
97
    = VALUE_STRING_EXT_INIT(opus_codec_toc_config_request_vals);
98
99
static const true_false_string toc_s_bit_vals = {"stereo", "mono"};
100
static const true_false_string fc_v_bit_vals = {"VBR", "CBR"};
101
static const true_false_string fc_p_bit_vals = {"Padding", "No Padding"};
102
103
static const value_string opus_codec_toc_c_request_vals[]
104
    = {{0, "1 frame in the packet"},
105
       {1, "2 frames in the packet, each with equal compressed size"},
106
       {2, "2 frames in the packet, with different compressed sizes"},
107
       {3, "an arbitrary number of frames in the packet"},
108
       {0, NULL}};
109
static value_string_ext opus_codec_toc_c_request_vals_ext
110
    = VALUE_STRING_EXT_INIT(opus_codec_toc_c_request_vals);
111
112
static int
113
parse_size_field(const unsigned char *ch, int32_t cn, int16_t *size)
114
0
{
115
0
    if (cn < 1) {
116
0
        *size = -1;
117
0
        return -1;
118
0
    }
119
0
    else if (ch[0] < 252) {
120
0
        *size = ch[0];
121
0
        return 1;
122
0
    }
123
0
    else if (cn < 2) {
124
0
        *size = -1;
125
0
        return -1;
126
0
    }
127
0
    else {
128
0
        *size = 4 * ch[1] + ch[0];
129
0
        return 2;
130
0
    }
131
0
}
132
133
static int16_t
134
opus_packet_get_samples_per_frame(const unsigned char *data, uint16_t Fs)
135
0
{
136
0
    int audiosize;
137
0
    if (data[0] & 0x80) {
138
0
        audiosize = ((data[0] >> 3) & 0x3);
139
0
        audiosize = (Fs << audiosize) / 400;
140
0
    }
141
0
    else if ((data[0] & 0x60) == 0x60) {
142
0
        audiosize = (data[0] & 0x08) ? Fs / 50 : Fs / 100;
143
0
    }
144
0
    else {
145
0
        audiosize = ((data[0] >> 3) & 0x3);
146
0
        if (audiosize == 3)
147
0
            audiosize = Fs * 60 / 1000;
148
0
        else
149
0
            audiosize = (Fs << audiosize) / 100;
150
0
    }
151
0
    return audiosize;
152
0
}
153
154
static int
155
dissect_opus(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
156
0
{
157
0
    int idx;
158
159
0
    proto_item *item;
160
0
    proto_tree *opus_tree;
161
162
0
    int pkt_total = 0, offset = 0;
163
0
    unsigned cap_len = 0;
164
0
    uint8_t ch = 0, toc = 0, octet[2] = {0, 0};
165
0
    int octet_cnt = 0;
166
0
    int bytes = 0;
167
0
    int16_t framesize = 0;
168
0
    struct FRAME_T {
169
0
        int16_t begin;
170
0
        int16_t size;
171
0
    } frames[MAX_FRAMES_COUNT] = {{0}};
172
0
    int frame_count = 0;
173
0
    static int *toc_fields[]
174
0
        = {&hf_opus_toc_config, &hf_opus_toc_s, &hf_opus_toc_c, NULL};
175
0
    static int *frame_count_fields[]
176
0
        = {&hf_opus_frame_count_v, &hf_opus_frame_count_p,
177
0
        &hf_opus_frame_count_m, NULL};
178
179
0
    int padding_size = 0;
180
181
0
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "OPUS");
182
183
0
    item = proto_tree_add_item(tree, proto_opus, tvb, 0, -1, ENC_NA);
184
0
    opus_tree = proto_item_add_subtree(item, ett_opus);
185
186
    /*
187
     *  A ToC entry takes the following format, details defined in section-3.1:
188
     *
189
     *    0 1 2 3 4 5 6 7
190
     *   +-+-+-+-+-+-+-+-+
191
     *   | config  |s| c |
192
     *   +-+-+-+-+-+-+-+-+
193
     */
194
0
    proto_tree_add_bitmask_list(opus_tree, tvb, offset, 1, toc_fields, ENC_NA);
195
196
0
    cap_len = tvb_captured_length(tvb);
197
0
    pkt_total = tvb_reported_length_remaining(tvb, offset);
198
199
0
    if (pkt_total <= 0) {
200
0
        expert_add_info(pinfo, opus_tree, &ei_opus_err_r1);
201
0
        return cap_len;
202
0
    }
203
204
0
    toc = tvb_get_uint8(tvb, offset++);
205
206
0
    switch (toc & 0x3) {
207
0
    case 0: /* One frame */
208
0
        frames[0].begin = offset;
209
0
        frames[0].size = pkt_total - offset;
210
0
        frame_count = 1;
211
0
        break;
212
0
    case 1: /* Two CBR frames */
213
0
        if ((pkt_total - offset) & 0x1) {
214
0
            expert_add_info(pinfo, opus_tree, &ei_opus_err_r3);
215
0
            return cap_len;
216
0
        }
217
0
        frames[0].begin = offset;
218
0
        frames[0].size = frames[1].size = (pkt_total - offset) / 2;
219
0
        frames[1].begin = frames[0].begin + frames[0].size;
220
0
        frame_count = 2;
221
0
        break;
222
0
    case 2: /* Two VBR frames */
223
0
        if (offset >= pkt_total) {
224
0
            expert_add_info(pinfo, opus_tree, &ei_opus_err_r4);
225
0
            return cap_len;
226
0
        }
227
        /* offset < pkt_total */
228
0
        octet[octet_cnt++] = tvb_get_uint8(tvb, offset);
229
0
        if (offset + 1 < pkt_total) {
230
0
            octet[octet_cnt++] = tvb_get_uint8(tvb, offset + 1);
231
0
        }
232
0
        bytes = parse_size_field(octet, octet_cnt, &framesize);
233
0
        if (framesize < 0 || framesize > pkt_total) {
234
0
            expert_add_info(pinfo, opus_tree, &ei_opus_err_r1);
235
0
            return cap_len;
236
0
        }
237
0
        proto_tree_add_uint(opus_tree, hf_opus_frame_size, tvb, offset, bytes,
238
0
                            framesize);
239
0
        offset += bytes;
240
        /* frame[0] has size header, frame[1] is remaining */
241
0
        frames[0].begin = offset;
242
0
        frames[0].size = framesize;
243
0
        frames[1].begin = frames[0].begin + framesize;
244
0
        frames[1].size = -1;
245
0
        frame_count = 2;
246
0
        break;
247
    /* Multiple CBR/VBR frames (from 0 to 120 ms) */
248
0
    default: /* case 3:*/
249
0
        if ((pkt_total - offset) < 1) {
250
0
            expert_add_info(pinfo, opus_tree, &ei_opus_err_r6);
251
0
            return cap_len;
252
0
        }
253
0
        proto_tree_add_bitmask_list(opus_tree, tvb, offset, 1,
254
0
                                    frame_count_fields, ENC_NA);
255
        /* Number of frames encoded in bits 0 to 5 */
256
0
        ch = tvb_get_uint8(tvb, offset++);
257
0
        frame_count = ch & 0x3F;
258
0
        framesize = opus_packet_get_samples_per_frame(&toc, 48000U);
259
0
        if (frame_count <= 0
260
0
            || framesize * frame_count > 120 * MAX_FRAMES_COUNT) {
261
0
            expert_add_info(pinfo, opus_tree, &ei_opus_err_r5);
262
0
            return cap_len;
263
0
        }
264
        /* The smallest value the function above can return is 120, so
265
         * the below holds (but make it obvious). */
266
0
        ws_assert(frame_count <= MAX_FRAMES_COUNT);
267
        /* Padding flag (bit 6) used */
268
0
        if (ch & 0x40) {
269
0
            int p;
270
0
            int padding_begin = offset;
271
0
            do {
272
0
                int tmp;
273
0
                if (offset >= pkt_total) {
274
0
                    expert_add_info(pinfo, opus_tree, &ei_opus_err_r7);
275
0
                    return cap_len;
276
0
                }
277
0
                p = tvb_get_uint8(tvb, offset++);
278
0
                tmp = p == 255 ? 254 : p;
279
0
                padding_size += tmp;
280
0
            } while (p == 255);
281
0
            proto_tree_add_uint(opus_tree, hf_opus_padding_size, tvb,
282
0
                                padding_begin, offset - padding_begin,
283
0
                                padding_size);
284
            /* This padding size is the number of padding bytes at the
285
             * end of the packets "in addition to the byte(s) used to
286
             * indicate the size of the padding."
287
             */
288
0
            pkt_total -= padding_size;
289
0
        }
290
        /* VBR flag is bit 7 */
291
0
        if (ch & 0x80) { /* VBR case */
292
0
            for (idx = 0; idx < frame_count - 1; idx++) {
293
0
                if (offset >= pkt_total) {
294
0
                    expert_add_info(pinfo, opus_tree, &ei_opus_err_r7);
295
0
                    return cap_len;
296
0
                }
297
0
                octet_cnt = 0;
298
0
                octet[octet_cnt++] = tvb_get_uint8(tvb, offset);
299
0
                if (offset + 1 < pkt_total) {
300
0
                    octet[octet_cnt++] = tvb_get_uint8(tvb, offset);
301
0
                }
302
0
                bytes = parse_size_field(octet, octet_cnt, &frames[idx].size);
303
0
                if (frames[idx].size < 0
304
0
                    || frames[idx].size > (pkt_total - offset)) {
305
0
                    expert_add_info(pinfo, opus_tree, &ei_opus_err_r1);
306
0
                    return cap_len;
307
0
                }
308
309
0
                proto_tree_add_uint(opus_tree, hf_opus_frame_size, tvb, offset,
310
0
                                    bytes, frames[idx].size);
311
0
                offset += bytes;
312
0
            }
313
0
            for (idx = 0; idx < frame_count - 1; idx++) {
314
0
                frames[idx].begin = offset;
315
0
                offset += frames[idx].size;
316
0
            }
317
0
            if (offset > pkt_total) {
318
0
                expert_add_info(pinfo, opus_tree, &ei_opus_err_r7);
319
0
                return cap_len;
320
0
            }
321
0
            frames[idx].begin = offset;
322
0
            frames[idx].size = pkt_total - offset;
323
0
        }
324
0
        else { /* CBR case */
325
0
            if (offset > pkt_total) {
326
0
                expert_add_info(pinfo, opus_tree, &ei_opus_err_r6);
327
0
                return cap_len;
328
0
            }
329
0
            unsigned frame_size = (pkt_total - offset) / frame_count;
330
0
            if (frame_size * frame_count != (unsigned)(pkt_total - offset)) {
331
0
                expert_add_info(pinfo, opus_tree, &ei_opus_err_r6);
332
0
                return cap_len;
333
0
            }
334
0
            for (idx = 0; idx < frame_count; idx++) {
335
0
                frames[idx].begin = offset + idx * frame_size;
336
0
                frames[idx].size = frame_size;
337
0
            }
338
0
        }
339
0
        break;
340
0
    }
341
342
0
    for (idx = 0; idx < frame_count; idx++) {
343
0
        struct FRAME_T *f = &frames[idx];
344
        /* reject the frame which is larger than 1275. */
345
0
        if (f->size > 1275) {
346
0
            expert_add_info(pinfo, opus_tree, &ei_opus_err_r2);
347
0
            return cap_len;
348
0
        }
349
0
        proto_tree_add_item(opus_tree, hf_opus_frame, tvb, f->begin, f->size,
350
0
                            ENC_NA);
351
0
    }
352
353
0
    if (padding_size) {
354
0
        int padding_begin = tvb_reported_length(tvb) - padding_size;
355
0
        item = proto_tree_add_item(opus_tree, hf_opus_padding, tvb,
356
0
                                   padding_begin, padding_size, ENC_NA);
357
        /* RFC 6716: "The additional padding bytes appear at the end
358
         * of the packet and MUST be set to zero by the encoder to avoid
359
         * creating a covert channel."
360
         */
361
0
        for (int i = 0; i < padding_size; ++i) {
362
0
            if (tvb_get_uint8(tvb, padding_begin + i) != 0) {
363
0
                expert_add_info(pinfo, item, &ei_opus_padding_nonzero);
364
0
                break;
365
0
            }
366
0
        }
367
0
    }
368
369
0
    return cap_len;
370
0
}
371
372
void
373
proto_register_opus(void)
374
14
{
375
14
    module_t *opus_module;
376
14
    expert_module_t* expert_opus;
377
378
14
    static hf_register_info hf[] = {
379
14
        {&hf_opus_toc_config,
380
14
         {"TOC.config", "opus.TOC.config", FT_UINT8, BASE_DEC | BASE_EXT_STRING,
381
14
          &opus_codec_toc_config_request_vals_ext, 0xF8, "Opus TOC config",
382
14
          HFILL}},
383
14
        {&hf_opus_toc_s,
384
14
         {"TOC.S bit", "opus.TOC.s", FT_BOOLEAN, 8, TFS(&toc_s_bit_vals),
385
14
          0x04, NULL, HFILL}},
386
14
        {&hf_opus_toc_c,
387
14
         {"TOC.C bits", "opus.TOC.c", FT_UINT8, BASE_DEC | BASE_EXT_STRING,
388
14
          &opus_codec_toc_c_request_vals_ext, 0x03, "Opus TOC code", HFILL}},
389
14
        {&hf_opus_frame_count_m,
390
14
         {"Frame Count.m", "opus.FC.m", FT_UINT8, BASE_DEC, NULL, 0x3F,
391
14
          "Frame Count", HFILL}},
392
14
        {&hf_opus_frame_count_p,
393
14
         {"Frame Count.p bit", "opus.FC.p", FT_BOOLEAN, 8,
394
14
          TFS(&fc_p_bit_vals), 0x40, NULL, HFILL}},
395
14
        {&hf_opus_frame_count_v,
396
14
         {"Frame Count.v bit", "opus.FC.v", FT_BOOLEAN, 8,
397
14
          TFS(&fc_v_bit_vals), 0x80, NULL, HFILL}},
398
14
        {&hf_opus_frame_size,
399
14
         {"Frame Size", "opus.frame_size", FT_UINT16, BASE_DEC | BASE_UNIT_STRING,
400
14
          UNS(&units_byte_bytes), 0x0, NULL, HFILL}},
401
14
        {&hf_opus_frame,
402
14
         {"Frame Data", "opus.frame_data", FT_BYTES, BASE_NONE | BASE_ALLOW_ZERO,
403
14
          NULL, 0x0, NULL, HFILL}},
404
14
        {&hf_opus_padding,
405
14
         {"Padding", "opus.padding", FT_BYTES, BASE_NONE, NULL, 0x0, NULL,
406
14
          HFILL}},
407
14
        {&hf_opus_padding_size,
408
14
         {"Padding Size", "opus.padding_size", FT_UINT32, BASE_DEC, NULL, 0x0,
409
14
          "Additional padding bytes, not including the bytes indicating the padding size",
410
14
          HFILL}},
411
14
    };
412
413
14
    static int *ett[] = { &ett_opus, };
414
415
14
    static ei_register_info ei[] = {
416
14
        {&ei_opus_err_r1,
417
14
         {"opus.violate_r1", PI_PROTOCOL, PI_ERROR,
418
14
          "Error:[R1] Packets are at least one byte.", EXPFILL}},
419
14
        {&ei_opus_err_r2,
420
14
         {"opus.violate_r2", PI_MALFORMED, PI_ERROR,
421
14
          "Error:[R2] No implicit frame length is larger than 1275 bytes.",
422
14
          EXPFILL}},
423
14
        {&ei_opus_err_r3,
424
14
         {"opus.violate_r3", PI_MALFORMED, PI_ERROR,
425
14
          "Error:[R3] Code 1 packets have an odd total length, N, so that "
426
14
          "(N-1)/2 is an integer.",
427
14
          EXPFILL}},
428
14
        {&ei_opus_err_r4,
429
14
         {"opus.violate_r4", PI_MALFORMED, PI_ERROR,
430
14
          "Error:[R4] Code 2 packets have enough bytes after the TOC for a "
431
14
          "valid frame length, and that length is no larger than the number of"
432
14
          "bytes remaining in the packet.",
433
14
          EXPFILL}},
434
14
        {&ei_opus_err_r5,
435
14
         {"opus.violate_r5", PI_PROTOCOL, PI_ERROR,
436
14
          "Error:[R5] Code 3 packets contain at least one frame, but no more "
437
14
          "than 120 ms of audio total.",
438
14
          EXPFILL}},
439
14
        {&ei_opus_err_r6,
440
14
         {"opus.violate_r6", PI_PROTOCOL, PI_ERROR,
441
14
          "Error:[R6] The length of a CBR code 3 packet, N, is at least two "
442
14
          "bytes, the number of bytes added to indicate the padding size plus "
443
14
          "the trailing padding bytes themselves, P, is no more than N-2, and "
444
14
          "the frame count, M, satisfies the constraint that (N-2-P) is a "
445
14
          "non-negative integer multiple of M.",
446
14
          EXPFILL}},
447
14
        {&ei_opus_err_r7,
448
14
         {"opus.violate_r7", PI_PROTOCOL, PI_ERROR,
449
14
          "Error:[R7] VBR code 3 packets are large enough to contain all the "
450
14
          "header bytes (TOC byte, frame count byte, any padding length bytes, "
451
14
          "and any frame length bytes), plus the length of the first M-1 "
452
14
          "frames, plus any trailing padding bytes.",
453
14
          EXPFILL}},
454
14
        {&ei_opus_padding_nonzero,
455
14
         {"opus.padding.nonzero", PI_PROTOCOL, PI_WARN,
456
14
          "Additional padding bytes MUST be set to zero by the encoder",
457
14
          EXPFILL}},
458
14
    };
459
460
14
    proto_opus = proto_register_protocol("Opus Interactive Audio Codec", "OPUS", "opus"); /* name, short name, abbreviation */
461
462
14
    proto_register_field_array(proto_opus, hf, array_length(hf));
463
14
    proto_register_subtree_array(ett, array_length(ett));
464
465
14
    opus_module = prefs_register_protocol(proto_opus, NULL);
466
467
14
    expert_opus = expert_register_protocol(proto_opus);
468
14
    expert_register_field_array(expert_opus, ei, array_length(ei));
469
470
14
    prefs_register_obsolete_preference(opus_module, "dynamic.payload.type");
471
472
14
    opus_handle = register_dissector("opus", dissect_opus, proto_opus);
473
14
}
474
475
void
476
proto_reg_handoff_opus(void)
477
14
{
478
14
    dissector_add_string("rtp_dyn_payload_type" , "OPUS", opus_handle);
479
480
14
    dissector_add_uint_range_with_preference("rtp.pt", "", opus_handle);
481
14
}
482
483
/*
484
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
485
 *
486
 * Local variables:
487
 * c-basic-offset: 4
488
 * tab-width: 8
489
 * indent-tabs-mode: nil
490
 * End:
491
 *
492
 * vi: set shiftwidth=4 tabstop=8 expandtab:
493
 * :indentSize=4:tabSize=8:noTabs=true:
494
 */