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-vj-comp.c
Line
Count
Source
1
/* packet-vj-comp.c
2
 * Routines for decompression of PPP Van Jacobson compression
3
 * RFC 1144
4
 *
5
 * Wireshark - Network traffic analyzer
6
 * By Gerald Combs <gerald@wireshark.org>
7
 *
8
 * SPDX-License-Identifier: GPL-2.0-or-later
9
 */
10
/* The routines in this file were created by reading the description of
11
 * RFC 1144 available here: ftp://ftp.rfc-editor.org/in-notes/rfc1144.pdf
12
 * ONLY the description of the protocol in section 3.2 was used.
13
 * Notably, the sample implementation in Appendix A was NOT read by this file's
14
 * author, due to the questionable legality of using it in Wireshark.
15
 * For details on this issue, see:
16
 * https://gitlab.com/wireshark/wireshark/-/issues/12138
17
 */
18
/* Currently hard-coded to assume TCP over IPv4.
19
 * Nothing in the standard explicitly prevents an IPv6 implementation...
20
 */
21
22
#include "config.h"
23
#define WS_LOG_DOMAIN "packet-vjc-comp"
24
#include <wireshark.h>
25
26
#include <epan/conversation.h>
27
#include <epan/in_cksum.h>
28
#include <epan/expert.h>
29
#include <epan/packet.h>
30
#include <epan/tfs.h>
31
#include <epan/iana-info.h>
32
#include <wsutil/pint.h>
33
#include <wsutil/str_util.h>
34
#include "packet-ppp.h"
35
36
37
/* Store the last connection number we've seen.
38
 * Only used on the first pass, in case the connection number itself
39
 * gets compressed out.
40
 */
41
1.35k
#define CNUM_INVALID UINT16_MAX
42
static uint16_t last_cnum = CNUM_INVALID;
43
44
/* Location in an IPv4 packet of the IP Next Protocol field
45
 * (which VJC replaces with the connection ID in uncompressed packets)
46
 */
47
7.66k
#define VJC_CONNID_OFFSET 9
48
49
/* Minimum TCP header length. We get compression data from the TCP header,
50
 * and also store it for future use.
51
 */
52
1.73k
#define VJC_TCP_HDR_LEN 20
53
54
/* Structure for tracking the changeable parts of a packet header */
55
typedef struct vjc_hdr_s {
56
    uint16_t tcp_chksum;
57
    uint16_t urg;
58
    uint16_t win;
59
    uint32_t seq;
60
    uint32_t ack;
61
    uint32_t ip_id;
62
    bool psh;
63
} vjc_hdr_t;
64
65
/* The structure used in a wireshark "conversation"
66
 * (though it's now in the multimap below)
67
 */
68
typedef struct vjc_conv_s {
69
    uint32_t    last_frame_len; // Length of previous frame (for SAWU/SWU)
70
    uint8_t    *frame_headers;  // Full copy of the IP header
71
    uint8_t     header_len;     // Length of the stored IP header
72
    vjc_hdr_t   vjc_headers;
73
} vjc_conv_t;
74
75
/* Map of frame number and connection ID to vjc_conv_t */
76
static wmem_multimap_t *vjc_conv_table;
77
78
/* Store connection ID for compressed frames that lack it */
79
static wmem_map_t *vjc_conn_id_lookup;
80
81
static dissector_handle_t vjcu_handle;
82
static dissector_handle_t vjcc_handle;
83
static dissector_handle_t ip_handle;
84
85
void proto_register_vjc(void);
86
void proto_reg_handoff_vjc(void);
87
88
static int proto_vjc;
89
90
static int ett_vjc;
91
static int ett_vjc_change_mask;
92
93
static expert_field ei_vjc_sawu;
94
static expert_field ei_vjc_swu;
95
static expert_field ei_vjc_no_cnum;
96
static expert_field ei_vjc_no_conversation;
97
static expert_field ei_vjc_no_direction;
98
static expert_field ei_vjc_no_conv_data;
99
static expert_field ei_vjc_undecoded;
100
static expert_field ei_vjc_bad_data;
101
static expert_field ei_vjc_error;
102
103
15
#define VJC_FLAG_R 0x80
104
2.10k
#define VJC_FLAG_C 0x40
105
1.40k
#define VJC_FLAG_I 0x20
106
127
#define VJC_FLAG_P 0x10
107
1.19k
#define VJC_FLAG_S 0x08
108
1.21k
#define VJC_FLAG_A 0x04
109
1.24k
#define VJC_FLAG_W 0x02
110
1.25k
#define VJC_FLAG_U 0x01
111
112
6.31k
#define VJC_FLAGS_SAWU 0x0F
113
1.91k
#define VJC_FLAGS_SWU 0x0B
114
115
static int hf_vjc_comp;
116
static int hf_vjc_cnum;
117
static int hf_vjc_change_mask;
118
static int hf_vjc_change_mask_r;
119
static int hf_vjc_change_mask_c;
120
static int hf_vjc_change_mask_i;
121
static int hf_vjc_change_mask_p;
122
static int hf_vjc_change_mask_s;
123
static int hf_vjc_change_mask_a;
124
static int hf_vjc_change_mask_w;
125
static int hf_vjc_change_mask_u;
126
static int hf_vjc_chksum;
127
static int hf_vjc_urg;
128
static int hf_vjc_d_win;
129
static int hf_vjc_d_ack;
130
static int hf_vjc_d_seq;
131
static int hf_vjc_d_ipid;
132
static int hf_vjc_tcpdata;
133
134
static int * const vjc_change_mask_fields[] = {
135
    &hf_vjc_change_mask_r,
136
    &hf_vjc_change_mask_c,
137
    &hf_vjc_change_mask_i,
138
    &hf_vjc_change_mask_p,
139
    &hf_vjc_change_mask_s,
140
    &hf_vjc_change_mask_a,
141
    &hf_vjc_change_mask_w,
142
    &hf_vjc_change_mask_u,
143
    NULL
144
};
145
146
/* Initialization routine. Called at start of dissection.
147
 * Registered in proto_register_vjc() below.
148
 */
149
static void
150
vjc_init_protocol(void)
151
15
{
152
15
    last_cnum = CNUM_INVALID;
153
15
}
154
155
/* Cleanup routine. Called at close of file.
156
 * Registered in proto_register_vjc() below.
157
 */
158
static void
159
vjc_cleanup_protocol(void)
160
0
{
161
0
    last_cnum = CNUM_INVALID;
162
0
}
163
164
/* Generate a "conversation" key for a VJC connection.
165
 * Returns NULL if it can't for some reason.
166
 */
167
static void *
168
vjc_get_conv_key(packet_info *pinfo, uint32_t vjc_cnum)
169
768
{
170
768
    if (vjc_cnum == CNUM_INVALID)
171
12
        return NULL;
172
173
    /* PPP gives us almost nothing to hook a conversation on; just whether
174
     * the packet is considered to be P2P_DIR_RECV or P2P_DIR_SENT.
175
     * Ideally we should also be distinguishing conversations based on the
176
     * capture interface, VLAN ID, MPLS tags, etc., etc. but that's beyond
177
     * the scope of this dissector, and a perennial problem in Wireshark anyway.
178
     * See <https://gitlab.com/wireshark/wireshark/-/issues/4561>
179
     */
180
756
    uint16_t ret_val = (uint16_t)vjc_cnum;
181
756
    switch (pinfo->p2p_dir) {
182
198
        case P2P_DIR_RECV:
183
198
            ret_val |= 0x0100;
184
198
            break;
185
20
        case P2P_DIR_SENT:
186
20
            ret_val |= 0x0200;
187
20
            break;
188
538
        default:
189
538
            return NULL;
190
756
    }
191
218
    return GUINT_TO_POINTER(ret_val);
192
756
}
193
194
/* RFC 1144 section 3.2.2 says that "deltas" are sent for many values in the
195
 * header. If the initial byte is 0, that means the following 2 bytes are the
196
 * 16-bit value of the delta. Otherwise, the initial byte is the 8-bit value.
197
 */
198
static uint32_t
199
vjc_delta_uint(proto_tree *tree, int hf, tvbuff_t *tvb, unsigned *offset)
200
692
{
201
692
    uint32_t ret_val;
202
692
    if (0 != tvb_get_uint8(tvb, *offset)) {
203
538
        proto_tree_add_item_ret_uint(tree, hf, tvb, *offset, 1,
204
538
                ENC_BIG_ENDIAN, &ret_val);
205
538
        (*offset)++;
206
538
    }
207
154
    else {
208
154
        (*offset)++;
209
154
        proto_tree_add_item_ret_uint(tree, hf, tvb, *offset, 2,
210
154
                ENC_BIG_ENDIAN, &ret_val);
211
154
        *offset += 2;
212
154
    }
213
692
    return ret_val;
214
692
}
215
216
/* Same thing but signed, since the TCP window delta can be negative */
217
static int32_t
218
vjc_delta_int(proto_tree *tree, int hf, tvbuff_t *tvb, unsigned *offset)
219
185
{
220
185
    int32_t ret_val;
221
185
    if (0 != tvb_get_int8(tvb, *offset)) {
222
118
        proto_tree_add_item_ret_int(tree, hf, tvb, *offset, 1,
223
118
                ENC_BIG_ENDIAN, &ret_val);
224
118
        (*offset)++;
225
118
    }
226
67
    else {
227
67
        (*offset)++;
228
67
        proto_tree_add_item_ret_int(tree, hf, tvb, *offset, 2,
229
67
                ENC_BIG_ENDIAN, &ret_val);
230
67
        *offset += 2;
231
67
    }
232
185
    return ret_val;
233
185
}
234
235
/* Main dissection routine for uncompressed VJC packets.
236
 * Registered in proto_reg_handoff_vjc() below.
237
 */
238
static int
239
dissect_vjc_uncomp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
240
2.45k
{
241
    /* A Van Jacobson uncompressed packet contains a standard TCP/IP header, with
242
     * the IP next protocol ID replaced with the connection number.
243
     * It's meant to signify a new TCP connection, or refresh an existing one,
244
     * which will have subsequent compressed packets.
245
     */
246
2.45k
    proto_tree     *subtree     = NULL;
247
2.45k
    proto_item     *ti          = NULL;
248
2.45k
    uint8_t         ip_ver      = 0;
249
2.45k
    uint8_t         ip_len      = 0;
250
2.45k
    unsigned        tcp_len     = 0;
251
2.45k
    uint32_t        vjc_cnum    = 0;
252
2.45k
    tvbuff_t       *tcpip_tvb   = NULL;
253
2.45k
    tvbuff_t       *sub_tvb     = NULL;
254
2.45k
    void           *conv_id     = NULL;
255
2.45k
    vjc_conv_t     *pkt_data    = NULL;
256
2.45k
    uint8_t        *pdata       = NULL;
257
2.45k
    static uint8_t  real_proto  = IP_PROTO_TCP;
258
259
2.45k
    ti = proto_tree_add_item(tree, proto_vjc, tvb, 0, -1, ENC_NA);
260
2.45k
    subtree = proto_item_add_subtree(ti, ett_vjc);
261
2.45k
    proto_item_set_text(subtree, "PPP Van Jacobson uncompressed TCP/IP");
262
263
    /* Start with some sanity checks */
264
2.45k
    if (tvb_captured_length(tvb) < VJC_CONNID_OFFSET+1) {
265
717
        proto_tree_add_expert_format_remaining(subtree, pinfo, &ei_vjc_bad_data, tvb, 0,
266
717
                "Packet truncated before Connection ID field");
267
717
        return tvb_captured_length(tvb);
268
717
    }
269
1.73k
    ip_ver = (tvb_get_uint8(tvb, 0) & 0xF0) >> 4;
270
1.73k
    ip_len = (tvb_get_uint8(tvb, 0) & 0x0F) << 2;
271
1.73k
    tcp_len = ip_len + VJC_TCP_HDR_LEN;
272
1.73k
    if (ip_ver != 4) {
273
431
        proto_tree_add_expert_format(subtree, pinfo, &ei_vjc_bad_data, tvb, 0, 1,
274
431
                "IPv%d unsupported for VJC compression", ip_ver);
275
431
        return tvb_captured_length(tvb);
276
431
    }
277
278
    /* So far so good, continue the dissection */
279
1.30k
    ti = proto_tree_add_boolean(subtree, hf_vjc_comp, tvb, 0, 0, false);
280
1.30k
    proto_item_set_generated(ti);
281
282
1.30k
    proto_tree_add_item_ret_uint(subtree, hf_vjc_cnum, tvb, VJC_CONNID_OFFSET, 1,
283
1.30k
            ENC_BIG_ENDIAN, &vjc_cnum);
284
285
    /* Build a composite TVB containing the original TCP/IP data.
286
     * This is easy for uncompressed VJC packets because only one byte
287
     * is different from the on-the-wire data.
288
     */
289
1.30k
    sub_tvb = tvb_new_child_real_data(tvb, &real_proto, 1, 1);
290
1.30k
    tvb_set_free_cb(sub_tvb, NULL);
291
292
1.30k
    tcpip_tvb = tvb_new_composite();
293
1.30k
    tvb_composite_append(tcpip_tvb, tvb_new_subset_length(tvb, 0, VJC_CONNID_OFFSET));
294
1.30k
    tvb_composite_append(tcpip_tvb, sub_tvb);
295
1.30k
    if (tvb_captured_length_remaining(tvb, VJC_CONNID_OFFSET+1) > 0) {
296
1.29k
        tvb_composite_append(tcpip_tvb, tvb_new_subset_remaining(tvb, VJC_CONNID_OFFSET+1));
297
1.29k
    }
298
1.30k
    tvb_composite_finalize(tcpip_tvb);
299
300
1.30k
    add_new_data_source(pinfo, tcpip_tvb, "Original TCP/IP data");
301
302
1.30k
    if (!(pinfo->p2p_dir == P2P_DIR_RECV || pinfo->p2p_dir == P2P_DIR_SENT)) {
303
        /* We can't make a proper conversation if we don't know the endpoints */
304
1.20k
        proto_tree_add_expert(subtree, pinfo, &ei_vjc_no_direction, tvb, 0, 0);
305
1.20k
    }
306
100
    else if (tvb_captured_length(tvb) < tcp_len) {
307
        /* Not enough data. We can still pass this packet onward (though probably
308
         * to no benefit), but can't base future decompression off of it.
309
         */
310
18
        proto_tree_add_expert_format_remaining(subtree, pinfo, &ei_vjc_bad_data, tvb, 0,
311
18
                "Packet truncated before end of TCP/IP headers");
312
18
    }
313
82
    else if (!pinfo->fd->visited) {
314
        /* If this is our first time visiting this packet, set things up for
315
         * decompressing future packets.
316
         */
317
82
        last_cnum = vjc_cnum;
318
82
        conv_id = vjc_get_conv_key(pinfo, vjc_cnum);
319
82
        DISSECTOR_ASSERT(conv_id != NULL);
320
321
82
        pkt_data = wmem_new0(wmem_file_scope(), vjc_conv_t);
322
82
        pkt_data->header_len = tcp_len;
323
82
        pdata = // shorthand
324
82
            pkt_data->frame_headers =
325
82
            (uint8_t *)tvb_memdup(wmem_file_scope(), tcpip_tvb, 0, tcp_len);
326
327
        // This value is used for re-calculating seq/ack numbers
328
82
        pkt_data->last_frame_len = tvb_reported_length(tvb) - ip_len;
329
330
82
        pkt_data->vjc_headers.ip_id = pntohu16(pdata + 4);
331
82
        pkt_data->vjc_headers.seq = pntohu32(pdata + ip_len + 4);
332
82
        pkt_data->vjc_headers.ack = pntohu32(pdata + ip_len + 8);
333
82
        pkt_data->vjc_headers.psh = (pdata[ip_len + 13] & 0x08) == 0x08;
334
82
        pkt_data->vjc_headers.win = pntohu16(pdata + ip_len + 14);
335
82
        pkt_data->vjc_headers.tcp_chksum = pntohu16(pdata + ip_len + 16);
336
82
        pkt_data->vjc_headers.urg = pntohu16(pdata + ip_len + 18);
337
338
82
        wmem_multimap_insert32(vjc_conv_table, conv_id, pinfo->num, (void *)pkt_data);
339
82
    }
340
0
    else {
341
        /* We've already visited this packet, we should have all the info we need. */
342
0
    }
343
344
1.30k
    return call_dissector_with_data(ip_handle, tcpip_tvb, pinfo, tree, data);
345
1.73k
}
346
347
/* Main dissection routine for compressed VJC packets.
348
 * Registered in proto_reg_handoff_vjc() below.
349
 */
350
static int
351
dissect_vjc_comp(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, void* data _U_)
352
2.00k
{
353
    /* A Van Jacobson compressed packet contains a change mask, which indicates
354
     * possible fields that may be present.
355
     */
356
2.00k
    proto_tree     *subtree     = NULL;
357
2.00k
    proto_item     *ti          = NULL;
358
2.00k
    unsigned        hdr_len     = 3;    // See below
359
2.00k
    bool            hdr_error   = false;
360
2.00k
    unsigned        ip_len      = 0;
361
2.00k
    unsigned        pkt_len     = 0;
362
2.00k
    unsigned        d_ipid      = 0;
363
2.00k
    unsigned        d_seq       = 0;
364
2.00k
    unsigned        d_ack       = 0;
365
2.00k
    int             d_win       = 0;
366
2.00k
    uint8_t         flags       = 0;
367
2.00k
    unsigned        offset      = 0;
368
2.00k
    uint32_t        urg         = 0;
369
2.00k
    uint32_t        ip_chksum   = 0;
370
2.00k
    uint32_t        tcp_chksum  = 0;
371
2.00k
    uint32_t        vjc_cnum    = 0;
372
2.00k
    void           *conv_id     = NULL;
373
2.00k
    vjc_hdr_t      *this_hdr    = NULL;
374
2.00k
    vjc_hdr_t      *last_hdr    = NULL;
375
2.00k
    vjc_conv_t     *last_data   = NULL;
376
2.00k
    vjc_conv_t     *this_data   = NULL;
377
2.00k
    uint8_t        *pdata       = NULL;
378
2.00k
    tvbuff_t       *tcpip_tvb   = NULL;
379
2.00k
    tvbuff_t       *sub_tvb     = NULL;
380
381
    /* Calculate the length of the VJC header,
382
     * accounting for extensions in the delta fields.
383
     * We start with a value of 3, because we'll always have
384
     * an 8-bit change mask and a 16-bit TCP checksum.
385
     */
386
2.00k
#define TEST_HDR_LEN \
387
3.28k
    if (tvb_captured_length(tvb)< hdr_len) { hdr_error = true; goto done_header_len; }
388
389
2.00k
    TEST_HDR_LEN;
390
778
    flags = tvb_get_uint8(tvb, offset);
391
778
    if (flags & VJC_FLAG_C) {
392
        // have connection number
393
136
        hdr_len++;
394
136
        TEST_HDR_LEN;
395
132
    }
396
774
    if ((flags & VJC_FLAGS_SAWU) == VJC_FLAGS_SAWU) {
397
        /* Special case for "unidirectional data transfer".
398
         * No change to header size; d_ack = 0, and
399
         * we're to calculate d_seq ourselves.
400
         */
401
82
    }
402
692
    else if ((flags & VJC_FLAGS_SAWU) == VJC_FLAGS_SWU) {
403
        /* Special case for "echoed interactive traffic".
404
         * No change to header size; we're to calculate d_seq and d_ack.
405
         */
406
32
    }
407
660
    else {
408
        /* Not a special case, determine the header size by
409
         * testing the SAWU flags individually.
410
         */
411
660
        if (flags & VJC_FLAG_U) {
412
            // have urgent pointer
413
179
            hdr_len += 2;
414
179
            TEST_HDR_LEN;
415
166
        }
416
647
        if (flags & VJC_FLAG_W) {
417
            // have d_win
418
215
            if (0 == tvb_get_int8(tvb, offset + hdr_len))
419
91
                hdr_len += 3;
420
124
            else
421
124
                hdr_len++;
422
215
            TEST_HDR_LEN;
423
191
        }
424
623
        if (flags & VJC_FLAG_A) {
425
            // have d_ack
426
254
            if (0 == tvb_get_uint8(tvb, offset + hdr_len))
427
63
                hdr_len += 3;
428
191
            else
429
191
                hdr_len++;
430
254
            TEST_HDR_LEN;
431
231
        }
432
600
        if (flags & VJC_FLAG_S) {
433
            // have d_seq
434
130
            if (0 == tvb_get_uint8(tvb, offset + hdr_len))
435
57
                hdr_len += 3;
436
73
            else
437
73
                hdr_len++;
438
130
            TEST_HDR_LEN;
439
118
        }
440
600
    }
441
702
    if (flags & VJC_FLAG_I) {
442
        // have IP ID
443
367
        if (0 == tvb_get_uint8(tvb, offset + hdr_len))
444
82
            hdr_len += 3;
445
285
        else
446
285
            hdr_len++;
447
367
        TEST_HDR_LEN;
448
355
    }
449
450
    /* Now that we have the header length, use it when assigning the
451
     * protocol item.
452
     */
453
690
#undef TEST_HDR_LEN
454
1.99k
done_header_len:
455
1.99k
    ti = proto_tree_add_item(tree, proto_vjc, tvb, 0,
456
1.99k
            MIN(hdr_len, tvb_captured_length(tvb)), ENC_NA);
457
1.99k
    subtree = proto_item_add_subtree(ti, ett_vjc);
458
1.99k
    proto_item_set_text(subtree, "PPP Van Jacobson compressed TCP/IP");
459
1.99k
    if (hdr_error) {
460
1.31k
        proto_tree_add_expert_format_remaining(subtree, pinfo, &ei_vjc_bad_data, tvb, 0,
461
1.31k
                "Packet truncated, compression header incomplete");
462
1.31k
        return tvb_captured_length(tvb);
463
1.31k
    }
464
465
686
    ti = proto_tree_add_boolean(subtree, hf_vjc_comp, tvb, 0, 0, true);
466
686
    proto_item_set_generated(ti);
467
468
686
    ti = proto_tree_add_bitmask(subtree, tvb, 0, hf_vjc_change_mask,
469
686
            ett_vjc_change_mask, vjc_change_mask_fields, ENC_NA);
470
686
    if ((flags & VJC_FLAGS_SAWU) == VJC_FLAGS_SAWU) {
471
76
        proto_tree_add_expert(ti, pinfo, &ei_vjc_sawu, tvb, 0, 1);
472
76
    }
473
610
    else if ((flags & VJC_FLAGS_SAWU) == VJC_FLAGS_SWU) {
474
31
        proto_tree_add_expert(ti, pinfo, &ei_vjc_swu, tvb, 0, 1);
475
31
    }
476
477
686
    offset++;
478
479
686
    if (flags & VJC_FLAG_C) {
480
112
        proto_tree_add_item_ret_uint(subtree, hf_vjc_cnum, tvb, offset, 1,
481
112
                ENC_BIG_ENDIAN, &vjc_cnum);
482
112
        last_cnum = vjc_cnum;
483
112
        offset++;
484
112
    }
485
574
    else {
486
        /* "If C is clear, the connection is assumed to be the same as for the
487
         * last compressed or uncompressed packet."
488
         */
489
574
        if (!pinfo->fd->visited) {
490
            /* On the first pass, get it from what we saw last,
491
             * and save it for future passes.
492
             * This can store CNUM_INVALID if that's in last_cnum
493
             * but that's not a problem.
494
             */
495
574
            vjc_cnum = last_cnum;
496
574
            wmem_map_insert(vjc_conn_id_lookup, GUINT_TO_POINTER(pinfo->num), GUINT_TO_POINTER(vjc_cnum));
497
574
        }
498
0
        else {
499
0
            if (wmem_map_contains(vjc_conn_id_lookup, GUINT_TO_POINTER(pinfo->num))) {
500
0
                vjc_cnum = GPOINTER_TO_UINT(wmem_map_lookup(vjc_conn_id_lookup,
501
0
                                                GUINT_TO_POINTER(pinfo->num)));
502
0
            }
503
0
            else {
504
0
                vjc_cnum = CNUM_INVALID;
505
0
            }
506
0
        }
507
574
        if (vjc_cnum != CNUM_INVALID) {
508
562
            ti = proto_tree_add_uint(subtree, hf_vjc_cnum, tvb, offset, 0, vjc_cnum);
509
562
            proto_item_set_generated(ti);
510
562
        }
511
12
        else {
512
12
            proto_tree_add_expert(subtree, pinfo, &ei_vjc_no_cnum, tvb, 0, 0);
513
12
        }
514
574
    }
515
686
    conv_id = vjc_get_conv_key(pinfo, vjc_cnum);
516
686
    if (conv_id != NULL) {
517
        // Get the most recent headers from *before* this packet
518
136
        last_data = (vjc_conv_t *)wmem_multimap_lookup32_le(vjc_conv_table, conv_id, pinfo->num - 1);
519
136
    }
520
686
    if (last_data == NULL) {
521
630
        proto_tree_add_expert(subtree, pinfo, &ei_vjc_no_conversation,
522
630
                tvb, 1, (flags & VJC_FLAG_C) ? 1 : 0);
523
630
    }
524
525
686
    proto_tree_add_item_ret_uint(subtree, hf_vjc_chksum, tvb, offset, 2,
526
686
            ENC_BIG_ENDIAN, &tcp_chksum);
527
686
    offset += 2;
528
529
686
    if ((flags & VJC_FLAGS_SAWU) == VJC_FLAGS_SAWU) {
530
        /* Special case for "unidirectional data transfer".
531
         * d_ack is 0, and d_seq changed by the amount of data in the previous packet.
532
         */
533
76
        flags &= ~VJC_FLAGS_SAWU;
534
76
        d_ack = 0;
535
76
        if (last_data != NULL) {
536
2
            d_seq = last_data->last_frame_len;
537
2
            ti = proto_tree_add_uint(subtree, hf_vjc_d_ack, tvb, offset, 0, d_ack);
538
2
            proto_item_set_generated(ti);
539
2
            ti = proto_tree_add_uint(subtree, hf_vjc_d_seq, tvb, offset, 0, d_seq);
540
2
            proto_item_set_generated(ti);
541
2
        }
542
76
    }
543
610
    else if ((flags & VJC_FLAGS_SAWU) == VJC_FLAGS_SWU) {
544
        /* Special case for "echoed interactive traffic".
545
         * d_seq and d_ack changed by the amount of user data in the
546
         * previous packet.
547
         */
548
31
        flags &= ~VJC_FLAGS_SAWU;
549
31
        if (last_data != NULL) {
550
11
            d_seq = d_ack = last_data->last_frame_len;
551
11
            ti = proto_tree_add_uint(subtree, hf_vjc_d_ack, tvb, offset, 0, d_ack);
552
11
            proto_item_set_generated(ti);
553
11
            ti = proto_tree_add_uint(subtree, hf_vjc_d_seq, tvb, offset, 0, d_seq);
554
11
            proto_item_set_generated(ti);
555
11
        }
556
31
    }
557
579
    else {
558
        /* Not a special case, read the SAWU flags individually */
559
560
579
        if (flags & VJC_FLAG_U) {
561
            /* "The packet’s urgent pointer is sent if URG is set ..."
562
            * I assume that means the full 16-bit value here.
563
            */
564
159
            proto_tree_add_item_ret_uint(subtree, hf_vjc_urg, tvb, offset, 2,
565
159
                    ENC_BIG_ENDIAN, &urg);
566
159
            offset += 2;
567
159
        }
568
420
        else {
569
420
            urg = 0;
570
420
        }
571
572
579
        if (flags & VJC_FLAG_W) {
573
            /* "The number sent for the window is also the difference between the current
574
            * and previous values. However, either positive or negative changes are
575
            * allowed since the window is a 16-bit field."
576
            */
577
185
            d_win = vjc_delta_int(subtree, hf_vjc_d_win, tvb, &offset);
578
185
        }
579
394
        else {
580
394
            d_win = 0;
581
394
        }
582
583
        /* The rest of the deltas can only be positive. */
584
579
        if (flags & VJC_FLAG_A) {
585
224
            d_ack = vjc_delta_uint(subtree, hf_vjc_d_ack, tvb, &offset);
586
224
        }
587
355
        else {
588
355
            d_ack = 0;
589
355
        }
590
591
579
        if (flags & VJC_FLAG_S) {
592
115
            d_seq = vjc_delta_uint(subtree, hf_vjc_d_seq, tvb, &offset);
593
115
        }
594
464
        else {
595
464
            d_seq = 0;
596
464
        }
597
579
    }
598
599
686
    if (flags & VJC_FLAG_I) {
600
353
        d_ipid = vjc_delta_uint(subtree, hf_vjc_d_ipid, tvb, &offset);
601
353
    }
602
333
    else {
603
        /* "However, unlike the rest of the compressed fields, the assumed
604
         * change when I is clear is one, not zero." - section 3.2.2
605
         */
606
333
        d_ipid = 1;
607
333
        ti = proto_tree_add_uint(subtree, hf_vjc_d_ipid, tvb, offset, 0, d_ipid);
608
333
        proto_item_set_generated(ti);
609
333
    }
610
611
686
    if (!(pinfo->p2p_dir == P2P_DIR_RECV || pinfo->p2p_dir == P2P_DIR_SENT)) {
612
        /* We can't make a proper conversation if we don't know the endpoints */
613
550
        proto_tree_add_expert(subtree, pinfo, &ei_vjc_no_direction, tvb, offset,
614
550
                tvb_captured_length_remaining(tvb, offset));
615
550
        return tvb_captured_length(tvb);
616
550
    }
617
136
    if (conv_id == NULL) {
618
0
        proto_tree_add_expert(subtree, pinfo, &ei_vjc_undecoded, tvb, offset,
619
0
                tvb_captured_length_remaining(tvb, offset));
620
0
        return tvb_captured_length(tvb);
621
0
    }
622
136
    if (last_data == NULL) {
623
80
        proto_tree_add_expert(subtree, pinfo, &ei_vjc_no_conv_data, tvb, offset,
624
80
                tvb_captured_length_remaining(tvb, offset));
625
80
        return tvb_captured_length(tvb);
626
80
    }
627
628
56
    if (!pinfo->fd->visited) {
629
        /* We haven't visited this packet before.
630
         * Form its vjc_hdr_t from the deltas and the info from the previous frame.
631
         */
632
56
        last_hdr = &last_data->vjc_headers;
633
634
56
        this_data = wmem_memdup(wmem_file_scope(), last_data, sizeof(vjc_conv_t));
635
56
        this_hdr = &this_data->vjc_headers;
636
56
        this_hdr->tcp_chksum = (uint16_t)tcp_chksum;
637
56
        this_hdr->urg = (uint16_t)urg;
638
56
        this_hdr->win = last_hdr->win + d_win;
639
56
        this_hdr->seq = last_hdr->seq + d_seq;
640
56
        this_hdr->ack = last_hdr->ack + d_ack;
641
56
        this_hdr->ip_id = last_hdr->ip_id + d_ipid;
642
56
        this_hdr->psh = (flags & VJC_FLAG_P) == VJC_FLAG_P;
643
56
        wmem_multimap_insert32(vjc_conv_table, conv_id, pinfo->num, (void *)this_data);
644
645
        // This frame is the next frame's last frame
646
56
        this_data->last_frame_len = tvb_reported_length_remaining(tvb, offset);
647
56
    }
648
0
    else {
649
        /* We have visited this packet before.
650
         * Get the values we saved the first time.
651
         */
652
0
        this_data = (vjc_conv_t *)wmem_multimap_lookup32(vjc_conv_table, conv_id, pinfo->num);
653
0
        DISSECTOR_ASSERT(this_data != NULL);
654
0
        this_hdr = &this_data->vjc_headers;
655
0
    }
656
56
    if (this_hdr != NULL) {
657
        /* this_data->frame_headers is our template packet header data.
658
         * Apply changes to it as needed.
659
         * The changes are intentionally done in the template before copying.
660
         */
661
56
        pkt_len = this_data->header_len + tvb_reported_length_remaining(tvb, offset);
662
663
56
        pdata = this_data->frame_headers; /* shorthand */
664
56
        ip_len = (pdata[0] & 0x0F) << 2;
665
666
        /* IP length */
667
56
        phtonu16(pdata + 2, pkt_len);
668
669
        /* IP ID */
670
56
        phtonu16(pdata + 4, this_hdr->ip_id);
671
672
        /* IP checksum */
673
56
        phtonu16(pdata + 10, 0x0000);
674
56
        ip_chksum = ip_checksum(pdata, ip_len);
675
56
        phtonu16(pdata + 10, g_htons(ip_chksum));
676
677
        /* TCP seq */
678
56
        phtonu32(pdata + ip_len + 4, this_hdr->seq);
679
680
        /* TCP ack */
681
56
        phtonu32(pdata + ip_len + 8, this_hdr->ack);
682
683
        /* TCP window */
684
56
        phtonu16(pdata + ip_len + 14, this_hdr->win);
685
686
        /* TCP push */
687
56
        if (this_hdr->psh) {
688
52
            pdata[ip_len + 13] |= 0x08;
689
52
        }
690
4
        else {
691
4
            pdata[ip_len + 13] &= ~0x08;
692
4
        }
693
694
        /* TCP checksum */
695
56
        phtonu16(pdata + ip_len + 16, this_hdr->tcp_chksum);
696
697
        /* TCP urg */
698
56
        if (this_hdr->urg) {
699
1
            pdata[ip_len + 13] |= 0x20;
700
1
            phtonu16(pdata + ip_len + 18, this_hdr->urg);
701
1
        }
702
55
        else {
703
55
            pdata[ip_len + 13] &= ~0x20;
704
55
            phtonu16(pdata + ip_len + 18, 0x0000);
705
55
        }
706
707
        /* Now that we're done manipulating the packet header, stick it into
708
         * a TVB for sub-dissectors to use.
709
         */
710
56
        sub_tvb = tvb_new_child_real_data(tvb, pdata,
711
56
                this_data->header_len, this_data->header_len);
712
56
        tvb_set_free_cb(sub_tvb, NULL);
713
714
        // Reuse pkt_len
715
56
        pkt_len = tvb_captured_length_remaining(tvb, offset);
716
56
        if (pkt_len > 0) {
717
55
            tcpip_tvb = tvb_new_composite();
718
55
            tvb_composite_append(tcpip_tvb, sub_tvb);
719
55
            tvb_composite_append(tcpip_tvb, tvb_new_subset_remaining(tvb, offset));
720
55
            tvb_composite_finalize(tcpip_tvb);
721
722
55
            ti = proto_tree_add_item(subtree, hf_vjc_tcpdata, tvb, offset, pkt_len, ENC_NA);
723
55
            proto_item_set_text(ti, "TCP data (%d byte%s)", pkt_len, plurality(pkt_len, "", "s"));
724
55
        }
725
1
        else
726
1
        {
727
1
            tcpip_tvb = sub_tvb;
728
1
        }
729
730
56
        add_new_data_source(pinfo, tcpip_tvb, "Decompressed TCP/IP data");
731
56
        return offset + call_dissector_with_data(ip_handle, tcpip_tvb, pinfo, tree, data);
732
56
    }
733
0
    else {
734
0
        proto_tree_add_expert_format(subtree, pinfo, &ei_vjc_error, tvb, 0, 0,
735
0
                "Dissector error: unable to find headers for current frame %d",
736
0
                pinfo->num);
737
0
    }
738
0
    return tvb_captured_length(tvb);
739
56
}
740
741
void
742
proto_register_vjc(void)
743
15
{
744
15
    static hf_register_info hf[] = {
745
15
        { &hf_vjc_comp,
746
15
            { "Is compressed", "vjc.compressed", FT_BOOLEAN, BASE_NONE,
747
15
                NULL, 0x0, NULL, HFILL }},
748
15
        { &hf_vjc_cnum,
749
15
            { "Connection number", "vjc.connection_number", FT_UINT8, BASE_DEC,
750
15
                NULL, 0x0, NULL, HFILL }},
751
15
        { &hf_vjc_change_mask,
752
15
            { "Change mask", "vjc.change_mask", FT_UINT8, BASE_HEX,
753
15
                NULL, 0x0, NULL, HFILL }},
754
15
        { &hf_vjc_change_mask_r,
755
15
            { "Reserved", "vjc.change_mask.reserved", FT_BOOLEAN, 8,
756
15
                TFS(&tfs_set_notset), VJC_FLAG_R, "Undefined bit", HFILL }},
757
15
        { &hf_vjc_change_mask_c,
758
15
            { "Connection number", "vjc.change_mask.connection_number", FT_BOOLEAN, 8,
759
15
                TFS(&tfs_set_notset), VJC_FLAG_C, "Whether connection number is present", HFILL }},
760
15
        { &hf_vjc_change_mask_i,
761
15
            { "IP ID", "vjc.change_mask.ip_id", FT_BOOLEAN, 8,
762
15
                TFS(&tfs_set_notset), VJC_FLAG_I, "Whether IP ID is present", HFILL }},
763
15
        { &hf_vjc_change_mask_p,
764
15
            { "TCP PSH", "vjc.change_mask.psh", FT_BOOLEAN, 8,
765
15
                TFS(&tfs_set_notset), VJC_FLAG_P, "Whether to set TCP PSH", HFILL }},
766
15
        { &hf_vjc_change_mask_s,
767
15
            { "TCP Sequence", "vjc.change_mask.seq", FT_BOOLEAN, 8,
768
15
                TFS(&tfs_set_notset), VJC_FLAG_S, "Whether TCP SEQ is present", HFILL }},
769
15
        { &hf_vjc_change_mask_a,
770
15
            { "TCP Acknowledgement", "vjc.change_mask.ack", FT_BOOLEAN, 8,
771
15
                TFS(&tfs_set_notset), VJC_FLAG_A, "Whether TCP ACK is present", HFILL }},
772
15
        { &hf_vjc_change_mask_w,
773
15
            { "TCP Window", "vjc.change_mask.win", FT_BOOLEAN, 8,
774
15
                TFS(&tfs_set_notset), VJC_FLAG_W, "Whether TCP Window is present", HFILL }},
775
15
        { &hf_vjc_change_mask_u,
776
15
            { "TCP Urgent", "vjc.change_mask.urg", FT_BOOLEAN, 8,
777
15
                TFS(&tfs_set_notset), VJC_FLAG_U, "Whether TCP URG pointer is present", HFILL }},
778
15
        { &hf_vjc_chksum,
779
15
            { "TCP Checksum", "vjc.checksum", FT_UINT16, BASE_HEX,
780
15
                NULL, 0x0, "TCP checksum of original packet", HFILL}},
781
15
        { &hf_vjc_urg,
782
15
            { "Urgent pointer", "vjc.urgent_pointer", FT_UINT16, BASE_DEC,
783
15
                NULL, 0x0, "TCP urgent pointer of original packet", HFILL}},
784
15
        { &hf_vjc_d_win,
785
15
            { "Delta window", "vjc.delta_window", FT_INT16, BASE_DEC,
786
15
                NULL, 0x0, "Change in TCP window size from previous packet", HFILL}},
787
15
        { &hf_vjc_d_ack,
788
15
            { "Delta ack", "vjc.delta_ack", FT_UINT16, BASE_DEC,
789
15
                NULL, 0x0, "Change in TCP acknowledgement number from previous packet", HFILL}},
790
15
        { &hf_vjc_d_seq,
791
15
            { "Delta seq", "vjc.delta_seq", FT_UINT16, BASE_DEC,
792
15
                NULL, 0x0, "Change in TCP sequence number from previous packet", HFILL}},
793
15
        { &hf_vjc_d_ipid,
794
15
            { "Delta IP ID", "vjc.delta_ipid", FT_UINT16, BASE_DEC,
795
15
                NULL, 0x0, "Change in IP Identification number from previous packet", HFILL}},
796
15
        { &hf_vjc_tcpdata,
797
15
            { "TCP data", "vjc.tcp_data", FT_BYTES, BASE_NONE,
798
15
                NULL, 0x0, "Original TCP payload", HFILL}},
799
15
    };
800
801
15
    static int *ett[] = {
802
15
        &ett_vjc,
803
15
        &ett_vjc_change_mask,
804
15
    };
805
806
15
    expert_module_t* expert_vjc;
807
15
    static ei_register_info ei[] = {
808
15
        { &ei_vjc_sawu,
809
15
            { "vjc.special.sawu", PI_PROTOCOL, PI_CHAT,
810
15
                ".... 1111 = special case for \"unidirectional data transfer\"", EXPFILL }},
811
15
        { &ei_vjc_swu,
812
15
            { "vjc.special.swu", PI_PROTOCOL, PI_CHAT,
813
15
                ".... 1011 = special case for \"echoed interactive traffic\"", EXPFILL }},
814
15
        { &ei_vjc_no_cnum,
815
15
            { "vjc.no_connection_id", PI_PROTOCOL, PI_WARN,
816
15
                "No connection ID and no prior connection (common at capture start)", EXPFILL }},
817
15
        { &ei_vjc_no_conversation,
818
15
            { "vjc.no_connection", PI_PROTOCOL, PI_WARN,
819
15
                "No saved connection found (common at capture start)", EXPFILL }},
820
15
        { &ei_vjc_no_direction,
821
15
            { "vjc.no_direction", PI_UNDECODED, PI_WARN,
822
15
                "Connection has no direction info, cannot decompress", EXPFILL }},
823
15
        { &ei_vjc_no_conv_data,
824
15
            { "vjc.no_connection_data", PI_UNDECODED, PI_WARN,
825
15
                "Could not find saved connection data", EXPFILL }},
826
15
        { &ei_vjc_undecoded,
827
15
            { "vjc.no_decompress", PI_UNDECODED, PI_WARN,
828
15
                "Undecoded data (impossible due to missing information)", EXPFILL }},
829
15
        { &ei_vjc_bad_data,
830
15
            { "vjc.bad_data", PI_PROTOCOL, PI_ERROR,
831
15
                "Non-compliant packet data", EXPFILL }},
832
15
        { &ei_vjc_error,
833
15
            { "vjc.error", PI_MALFORMED, PI_ERROR,
834
15
                "Unrecoverable dissector error", EXPFILL }},
835
15
    };
836
837
15
    proto_vjc = proto_register_protocol("Van Jacobson PPP compression", "VJC", "vjc");
838
15
    proto_register_field_array(proto_vjc, hf, array_length(hf));
839
15
    proto_register_subtree_array(ett, array_length(ett));
840
15
    expert_vjc = expert_register_protocol(proto_vjc);
841
15
    expert_register_field_array(expert_vjc, ei, array_length(ei));
842
15
    vjcc_handle = register_dissector("vjc_compressed", dissect_vjc_comp, proto_vjc);
843
15
    vjcu_handle = register_dissector("vjc_uncompressed", dissect_vjc_uncomp, proto_vjc);
844
845
    // TODO: is it possible to postpone allocating these until we actually see VJC?
846
    // It's probably not a common protocol.
847
15
    vjc_conn_id_lookup = wmem_map_new_autoreset(wmem_epan_scope(), wmem_file_scope(),
848
15
                                                    g_direct_hash, g_direct_equal);
849
15
    vjc_conv_table = wmem_multimap_new_autoreset(wmem_epan_scope(), wmem_file_scope(),
850
15
                                                    g_direct_hash, g_direct_equal);
851
852
15
    register_init_routine(&vjc_init_protocol);
853
15
    register_cleanup_routine(&vjc_cleanup_protocol);
854
15
}
855
856
void
857
proto_reg_handoff_vjc(void)
858
15
{
859
15
    ip_handle = find_dissector("ip");
860
861
15
    dissector_add_uint("ppp.protocol", PPP_VJC_COMP, vjcc_handle);
862
15
    dissector_add_uint("ppp.protocol", PPP_VJC_UNCOMP, vjcu_handle);
863
15
}
864
865
/*
866
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
867
 *
868
 * Local variables:
869
 * c-basic-offset: 4
870
 * tab-width: 8
871
 * indent-tabs-mode: nil
872
 * End:
873
 *
874
 * vi: set shiftwidth=4 tabstop=8 expandtab:
875
 * :indentSize=4:tabSize=8:noTabs=true:
876
 */