aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-vsock.c
blob: 689d7cbfe66e0bc5506eedb111738d6caccc913c (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
/* packet-vsock.c
 * Routines for AF_VSOCK dissection
 * Copyright 2016, Gerard Garcia <ggarcia@deic.uab.cat>
 *
 * Header definition:
 * https://github.com/GerardGarcia/linux/blob/vsockmon/include/uapi/linux/vsockmon.h
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/*
 * The AF_VSOCK socket allows zero-configuration communication between guests
 * and hypervisors using the standard socket API.
 */

#include <config.h>
#include <epan/packet.h>
#include <wsutil/pint.h>
#include <epan/address_types.h>
#include <wiretap/wtap.h>

void proto_register_vsock(void);
void proto_reg_handoff_vsock(void);

static int proto_vsock = -1;
static int vsock_address_type = -1;
static dissector_handle_t vsock_handle;

/* Generic header related fields */
static int hf_vsock_src_cid = -1;
static int hf_vsock_src_port = -1;
static int hf_vsock_dst_cid = -1;
static int hf_vsock_dst_port = -1;
static int hf_vsock_op = -1;
static int hf_vsock_t = -1;
static int hf_vsock_t_len = -1;
static int hf_vsock_reserved = -1;
static int hf_vsock_payload = -1;

/* Virtio related fields */
static int hf_virtio_src_cid = -1;
static int hf_virtio_dst_cid = -1;
static int hf_virtio_src_port = -1;
static int hf_virtio_dst_port = -1;
static int hf_virtio_len = -1;
static int hf_virtio_type = -1;
static int hf_virtio_op = -1;
static int hf_virtio_flags = -1;
static int hf_virtio_buf_alloc = -1;
static int hf_virtio_fwd_cnt = -1;

static gint ett_vsock = -1;
static gint ett_virtio = -1;

static const value_string af_vsockmon_op_names[] = {
    { 0, "Unknown" },
    { 1, "Connect" },
    { 2, "Disconnect" },
    { 3, "Control" },
    { 4, "Payload" },
    { 0, NULL }
};

enum af_vsockmon_t {
    AF_VSOCK_T_UNKNOWN = 0,
    AF_VSOCK_T_NO_INFO = 1,
    AF_VSOCK_T_VIRTIO = 2
};

static const value_string af_vsockmon_t_names[] = {
    { 0, "Unknown" },
    { 1, "No info" },
    { 2, "Virtio" },
    { 0 , NULL }
};

static const value_string virtio_vsock_type_names[] = {
    { 1, "Stream"},
    { 0, NULL }
};

static const value_string virtio_vsock_op_names[] = {
    { 0, "Invalid" },
    { 1, "Request" },
    { 2, "Response" },
    { 3, "Rst" },
    { 4, "Shutdown" },
    { 5, "RW" },
    { 6, "Credit update" },
    { 7, "Credit response" },
    { 0 , NULL }
};

#define VSOCK_MIN_LENGTH 32

static int vsock_addr_to_str(const address* addr, gchar *buf, int buf_len)
{
    const guint8 *addrp = (const guint8 *)addr->data;

    if(pletoh64(&addrp[0])==2){
        (void) g_strlcpy(buf, "host", buf_len);
    } else {
        snprintf(buf, buf_len, "%" PRIu64, pletoh64(&addrp[0]));
    }

    return (int)(strlen(buf)+1);
}

static int vsock_addr_str_len(const address* addr _U_)
{
    /* 2^64 unsigned int len */
    return 19;
}

static int
dissect_vsock(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
        void *data _U_)
{
    proto_item *ti, *virtio_ti;
    proto_tree *vsock_tree, *virtio_tree;

    guint32 t_len, payload_len, virtio_buf_alloc, op, type,
            virtio_fwd_cnt, virtio_op, virtio_type;
    guint16 payload_offset = 0, offset = 0;

    if (tvb_reported_length(tvb) < VSOCK_MIN_LENGTH)
        return 0;

    /* Clear column information before start parsing */
    col_clear(pinfo->cinfo, COL_INFO);

    /* Create top tree */
    ti = proto_tree_add_item(tree, proto_vsock, tvb, 0, -1, ENC_NA);
    vsock_tree = proto_item_add_subtree(ti, ett_vsock);

    /* Parse generic header part */
    proto_tree_add_item(vsock_tree, hf_vsock_src_cid, tvb, offset, 8, ENC_LITTLE_ENDIAN);
    set_address_tvb(&pinfo->src, vsock_address_type, 8, tvb, offset);
    offset += 8;

    proto_tree_add_item(vsock_tree, hf_vsock_dst_cid, tvb, offset, 8, ENC_LITTLE_ENDIAN);
    set_address_tvb(&pinfo->dst, vsock_address_type, 8, tvb, offset);
    offset += 8;

    proto_tree_add_item_ret_uint(vsock_tree, hf_vsock_src_port, tvb, offset, 4, ENC_LITTLE_ENDIAN, &pinfo->srcport);
    offset += 4;

    proto_tree_add_item_ret_uint(vsock_tree, hf_vsock_dst_port, tvb, offset, 4, ENC_LITTLE_ENDIAN, &pinfo->destport);
    offset += 4;

    proto_tree_add_item_ret_uint(vsock_tree, hf_vsock_op, tvb, offset, 2, ENC_LITTLE_ENDIAN, &op);
    offset += 2;

    proto_tree_add_item_ret_uint(vsock_tree, hf_vsock_t, tvb, offset, 2, ENC_LITTLE_ENDIAN, &type);
    offset += 2;

    proto_tree_add_item_ret_uint(vsock_tree, hf_vsock_t_len, tvb, offset, 2, ENC_LITTLE_ENDIAN, &t_len);
    offset += 2;

    proto_tree_add_item(vsock_tree, hf_vsock_reserved, tvb, offset, 2, ENC_NA);
    offset += 2;

    payload_offset = offset + t_len;

    /* Append summary information to top tree */
    proto_item_append_text(ti, ", Op: %s, Transport: %s",
            val_to_str(op, af_vsockmon_op_names, "Unknown (%d)"),
            val_to_str(type, af_vsockmon_t_names, "Unknown (%d)"));

    /* Fill columns */
    col_add_fstr(pinfo->cinfo, COL_INFO, "[%s] %s",
            val_to_str(op, af_vsockmon_op_names, "Unknown (%d)"),
            val_to_str(type, af_vsockmon_t_names, "Unknown (%d)"));
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "vSocket");

    /* Create subtree if there is transport information */
    switch (type) {
        case AF_VSOCK_T_UNKNOWN:
        case AF_VSOCK_T_NO_INFO:
            break;
        case AF_VSOCK_T_VIRTIO:
            virtio_tree = proto_tree_add_subtree(vsock_tree, tvb, offset, 44, ett_virtio, &virtio_ti, "Virtio transport header");

            proto_tree_add_item(virtio_tree, hf_virtio_src_cid, tvb, offset, 8, ENC_LITTLE_ENDIAN);
            offset += 8;
            proto_tree_add_item(virtio_tree, hf_virtio_dst_cid, tvb, offset, 8, ENC_LITTLE_ENDIAN);
            offset += 8;
            proto_tree_add_item(virtio_tree, hf_virtio_src_port, tvb, offset, 4, ENC_LITTLE_ENDIAN);
            offset += 4;
            proto_tree_add_item(virtio_tree, hf_virtio_dst_port, tvb, offset, 4, ENC_LITTLE_ENDIAN);
            offset += 4;
            proto_tree_add_item(virtio_tree, hf_virtio_len, tvb, offset, 4, ENC_LITTLE_ENDIAN);
            offset += 4;

            proto_tree_add_item_ret_uint(virtio_tree, hf_virtio_type, tvb, offset, 2, ENC_LITTLE_ENDIAN, &virtio_type);
            offset += 2;

            proto_tree_add_item_ret_uint(virtio_tree, hf_virtio_op, tvb, offset, 2, ENC_LITTLE_ENDIAN, &virtio_op);
            offset += 2;

            proto_tree_add_item(virtio_tree, hf_virtio_flags, tvb, offset, 4, ENC_LITTLE_ENDIAN);
            offset += 4;

            proto_tree_add_item_ret_uint(virtio_tree, hf_virtio_buf_alloc, tvb, offset, 4, ENC_LITTLE_ENDIAN, &virtio_buf_alloc);
            offset += 4;

            proto_tree_add_item_ret_uint(virtio_tree, hf_virtio_fwd_cnt, tvb, offset, 4, ENC_LITTLE_ENDIAN, &virtio_fwd_cnt);
            /*offset += 4;*/

            /* Append virtio information */
            col_append_fstr(pinfo->cinfo, COL_INFO, ": %s, Op: %s, Buf alloc: %u, Fwd cnt: %u",
                    val_to_str(virtio_type, virtio_vsock_type_names, "Unknown (%d)"),
                    val_to_str(virtio_op, virtio_vsock_op_names, "Unknown (%d)"),
                    virtio_buf_alloc, virtio_fwd_cnt);
            break;
    }


    /* Append payload */
    payload_len = tvb_reported_length_remaining(tvb, payload_offset);
    if (payload_len)
        proto_tree_add_bytes_format(vsock_tree, hf_vsock_payload, tvb, payload_offset, payload_len,
                NULL, "Payload (%uB)", payload_len);

    return tvb_reported_length(tvb);
}

void
proto_register_vsock(void)
{
    static hf_register_info hf[] = {
        { &hf_vsock_src_cid,
            {"Source cid", "vsock.src_cid", FT_UINT64, BASE_DEC, NULL,
                0x0, NULL, HFILL }},
        { &hf_vsock_dst_cid,
            {"Destination cid", "vsock.dst_cid", FT_UINT64, BASE_DEC, NULL,
                0x0, NULL, HFILL }},
        { &hf_vsock_src_port,
            {"Source port", "vsock.src_port", FT_UINT32, BASE_DEC, NULL,
                0x0, NULL, HFILL }},
        { &hf_vsock_dst_port,
            {"Destination port", "vsock.dst_port", FT_UINT32, BASE_DEC, NULL,
                0x0, NULL, HFILL }},
        { &hf_vsock_op,
            {"Operation", "vsock.op", FT_UINT16, BASE_DEC, VALS(af_vsockmon_op_names),
                0x0, NULL, HFILL }},
        { &hf_vsock_t,
            {"Transport", "vsock.trans", FT_UINT16, BASE_DEC, VALS(af_vsockmon_t_names),
                0x0, NULL, HFILL }},
        { &hf_vsock_t_len,
            {"Transport length", "vsock.trans_len", FT_UINT16, BASE_DEC, NULL,
                0x0, NULL, HFILL }},
        { &hf_vsock_reserved,
            {"Reserved", "vsock.reserved", FT_BYTES, BASE_NONE, NULL,
                0x0, NULL, HFILL }},
        { &hf_vsock_payload,
            { "Payload", "vsock.payload", FT_BYTES, BASE_NONE, NULL,
                0x0, NULL, HFILL}},
        { &hf_virtio_src_cid,
            {"Source cid", "vsock.virtio.src_cid", FT_UINT64, BASE_DEC, NULL,
                0x0, NULL, HFILL }},
        { &hf_virtio_dst_cid,
            {"Destination cid", "vsock.virtio.dst_cid", FT_UINT64, BASE_DEC, NULL,
                0x0, NULL, HFILL }},
        { &hf_virtio_src_port,
            {"Source port", "vsock.virtio.src_prot", FT_UINT32, BASE_DEC, NULL,
                0x0, NULL, HFILL }},
        { &hf_virtio_dst_port,
            {"Destination port", "vsock.virtio.dst_prot", FT_UINT32, BASE_DEC, NULL,
                0x0, NULL, HFILL }},
        { &hf_virtio_len,
            {"Length", "vsock.virtio.len", FT_UINT32, BASE_DEC, NULL,
                0x0, NULL, HFILL }},
        { &hf_virtio_type,
            {"Type", "vsock.virtio.type", FT_UINT16, BASE_DEC, VALS(virtio_vsock_type_names),
                0x0, NULL, HFILL }},
        { &hf_virtio_op,
            {"Operation", "vsock.virtio.op", FT_UINT16, BASE_DEC, VALS(virtio_vsock_op_names),
                0x0, NULL, HFILL }},
        { &hf_virtio_flags,
            {"Flags", "vsock.virtio.flags", FT_UINT32, BASE_HEX, NULL,
                0x0, NULL, HFILL }},
        { &hf_virtio_buf_alloc,
            {"Buf alloc", "vsock.virtio.buf_alloc", FT_UINT32, BASE_DEC, NULL,
                0x0, NULL, HFILL }},
        { &hf_virtio_fwd_cnt,
            {"Fwd cnt", "vsock.virtio.fwd_cnt", FT_UINT32, BASE_DEC, NULL,
                0x0, NULL, HFILL }}
    };
    static gint *ett[] = {
        &ett_vsock,
        &ett_virtio
    };

    vsock_address_type = address_type_dissector_register("AT_VSOCK", "vSocket Address",
            vsock_addr_to_str, vsock_addr_str_len, NULL, NULL, NULL, NULL, NULL);

    proto_vsock = proto_register_protocol("vSocket", "vsock", "vsock");
    proto_register_field_array(proto_vsock, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    vsock_handle = register_dissector("vsock", dissect_vsock, proto_vsock);
}

void
proto_reg_handoff_vsock(void)
{
    dissector_add_uint("wtap_encap", WTAP_ENCAP_VSOCK, vsock_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:
 */