summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-msrcp.c
blob: 3b5b58e480895580a39bb2611ee05f0fad0a7b6d (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
/* packet-msrcp.c
 * Routines for decoding Microsoft Cluster Route Control Protocol (MSRCP)
 * Copyright 2022, Will Aftring <william.aftring@outlook.com>
 *
 * SPDX-License-Identifier: MIT
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 */


#include "config.h"
#include <epan/packet.h>
#include <epan/expert.h>
#include <epan/conversation.h>

void proto_register_msrcp(void);
void proto_reg_handoff_msrcp(void);

static dissector_handle_t msrcp_handle;

#define MSRCP_PORT 3343
#define MSRCP_REQUEST 0
#define MSRCP_RESPONSE 1
#define MSRCP_OFFSET_TYPE 6
#define MSRCP_OFFSET_SEQ 12


static const value_string packettypenames[] = {
    {0, "REQUEST" },
    {1, "RESPONSE" },
    {0, NULL}
};

static const value_string headertypenames[] = {
    {0, "MSRCP EXTENSION NONE",},
    {1, "MSRCP IPv4 Pair"},
    {2, "MSRCP IPv6 Pair"},
    {3, "MSRCP Signature"},
    {4, "MSRCP Maximum"},
    {0, NULL}
};

typedef struct _msrcp_conv_info_t {
    wmem_tree_t* pdus;
} msrcp_conv_info_t;

typedef struct _msrcp_transaction_t {
    guint32 req_frame;
    guint32 rep_frame;
    nstime_t req_time;
    guint32 seq;
    gboolean matched;
} msrcp_transaction_t;

static int proto_msrcp = -1;
static int hf_msrcp_id = -1;
static int hf_msrcp_type = -1;
static int hf_msrcp_vers = -1;
static int hf_msrcp_reserved = -1;
static int hf_msrcp_next_header = -1;
static int hf_msrcp_len = -1;
static int hf_msrcp_seq = -1;
static int hf_msrcp_response_in = -1;
static int hf_msrcp_response_to = -1;
static int hf_msrcp_ext_header = -1;
static int hf_msrcp_ext_next_header = -1;
static int hf_msrcp_ext_len = -1;
static int hf_msrcp_ext_res = -1;

static gint ett_msrcp = -1;
static gint ett_msrcp_nxt = -1;

static expert_field ei_msrcp_no_resp = EI_INIT;

// Handles for subparsing
static dissector_handle_t eth_handle;

static int
dissect_msrcp(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree, void* data _U_)
{

    /*
        Rough Packet layout:
        Identifier:         4 bytes
        Version:            1 byte
        Reserved:           1 byte
        Type:               2 bytes
        NextHeader:         2 bytes
        Total Length:       2 bytes
        SeqNum:             4 bytes
        ExtHeader:          40 bytes
            NextHeader:     2 bytes
            Length:         2 bytes
            Reserved:       4 bytes
            SrcAddr:        16 bytes
            DstAddr:        16 bytes
    */
    guint tree_offset = 0;

    proto_tree* msrcp_tree, * nxt_tree;
    proto_item* ti, * nxt_ti;
    tvbuff_t* next_tvb;
    guint32         seq;
    guint16         type;

    // variables for our expert analysis
    conversation_t* conv = NULL;
    msrcp_conv_info_t* msrcp_info = NULL;
    msrcp_transaction_t* msrcp_trans = NULL;
    wmem_tree_key_t  key[3];

    type = tvb_get_guint8(tvb, MSRCP_OFFSET_TYPE);
    seq = tvb_get_guint32(tvb, MSRCP_OFFSET_SEQ, ENC_LITTLE_ENDIAN);

    conv = find_or_create_conversation(pinfo);
    msrcp_info = (msrcp_conv_info_t*)conversation_get_proto_data(conv, proto_msrcp);
    if (!msrcp_info)
    {
        msrcp_info = wmem_new(wmem_file_scope(), msrcp_conv_info_t);
        msrcp_info->pdus = wmem_tree_new(wmem_file_scope());
        conversation_add_proto_data(conv, proto_msrcp, msrcp_info);
    }

    key[0].length = 1;
    key[0].key = &seq;
    key[1].length = 1;
    key[1].key = &pinfo->num;
    key[2].length = 0;
    key[2].key = NULL;
    if ((type == MSRCP_REQUEST) || (type == MSRCP_RESPONSE))
    {
        if (!pinfo->fd->visited)
        {
            if (type == MSRCP_REQUEST)
            {
                msrcp_trans = wmem_new(wmem_file_scope(), msrcp_transaction_t);
                msrcp_trans->req_frame = pinfo->num;
                msrcp_trans->rep_frame = 0;
                msrcp_trans->req_time = pinfo->abs_ts;
                msrcp_trans->seq = seq;
                msrcp_trans->matched = FALSE;
                wmem_tree_insert32_array(msrcp_info->pdus, key, (void*)msrcp_trans);
            }
            else
            {
                msrcp_trans = (msrcp_transaction_t*)wmem_tree_lookup32_array_le(msrcp_info->pdus, key);
                if (msrcp_trans)
                {
                    if (msrcp_trans->seq != seq)
                    {
                        msrcp_trans = NULL;
                    }
                    else if (msrcp_trans->rep_frame == 0)
                    {
                        msrcp_trans->rep_frame = pinfo->num;
                        msrcp_trans->matched = TRUE;
                    }
                }
            }
        }
        else
        {
            msrcp_trans = (msrcp_transaction_t*)wmem_tree_lookup32_array_le(msrcp_info->pdus, key);
            if (msrcp_trans)
            {
                if (msrcp_trans->seq != seq)
                {
                    msrcp_trans = NULL;
                }
                else if ((!(type == MSRCP_RESPONSE)) && (msrcp_trans->req_frame != pinfo->num))
                {
                    msrcp_transaction_t* retrans_msrcp = wmem_new(pinfo->pool, msrcp_transaction_t);
                    retrans_msrcp->req_frame = msrcp_trans->req_frame;
                    retrans_msrcp->rep_frame = 0;
                    retrans_msrcp->req_time = pinfo->abs_ts;
                    msrcp_trans = retrans_msrcp;
                }
            }
        }
        if (!msrcp_trans)
        {
            msrcp_trans = wmem_new(pinfo->pool, msrcp_transaction_t);
            msrcp_trans->req_frame = 0;
            msrcp_trans->rep_frame = 0;
            msrcp_trans->req_time = pinfo->abs_ts;
            msrcp_trans->matched = FALSE;
        }
    }


    col_set_str(pinfo->cinfo, COL_PROTOCOL, "MSRCP");
    col_clear(pinfo->cinfo, COL_INFO);
    col_add_fstr(pinfo->cinfo, COL_INFO, "%s ID %d (0x%X)",
        val_to_str_const(type, packettypenames, "MSRCP"), seq, seq);


    ti = proto_tree_add_item(tree, proto_msrcp, tvb, 0, -1, ENC_BIG_ENDIAN);
    proto_item_append_text(ti, "Type %s",
        val_to_str_const(type, packettypenames, "MSRCP"));
    msrcp_tree = proto_item_add_subtree(ti, ett_msrcp);

    if (type == MSRCP_REQUEST || type == MSRCP_RESPONSE)
    {
        proto_item* it;
        proto_tree_add_item(msrcp_tree, hf_msrcp_id, tvb, 0, 4, ENC_BIG_ENDIAN);
        tree_offset += 4;
        proto_tree_add_item(msrcp_tree, hf_msrcp_vers, tvb, tree_offset, 1, ENC_LITTLE_ENDIAN);
        tree_offset += 1;
        proto_tree_add_item(msrcp_tree, hf_msrcp_reserved, tvb, tree_offset, 1, ENC_LITTLE_ENDIAN);
        tree_offset += 1;
        proto_tree_add_item(msrcp_tree, hf_msrcp_type, tvb, tree_offset, 2, ENC_LITTLE_ENDIAN);
        tree_offset += 2;
        proto_tree_add_item(msrcp_tree, hf_msrcp_next_header, tvb, tree_offset, 2, ENC_LITTLE_ENDIAN);
        tree_offset += 2;
        proto_tree_add_item(msrcp_tree, hf_msrcp_len, tvb, tree_offset, 2, ENC_LITTLE_ENDIAN);
        tree_offset += 2;
        it = proto_tree_add_item(msrcp_tree, hf_msrcp_seq, tvb, tree_offset, 4, ENC_LITTLE_ENDIAN);
        tree_offset += 4;

        if (msrcp_trans->matched)
        {
            if ((msrcp_trans->req_frame) && (type == MSRCP_RESPONSE))
            {
                it = proto_tree_add_uint(msrcp_tree, hf_msrcp_response_to, tvb, 0, 0, msrcp_trans->req_frame);
                proto_item_set_generated(it);

            }
            else if ((msrcp_trans->rep_frame) && (type == MSRCP_REQUEST))
            {
                it = proto_tree_add_uint(msrcp_tree, hf_msrcp_response_in, tvb, 0, 0, msrcp_trans->rep_frame);
                proto_item_set_generated(it);
            }
        }
        else
        {
            expert_add_info(pinfo, it, &ei_msrcp_no_resp);
            col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[Missing MSRCP Response]");
        }

        nxt_ti = proto_tree_add_item(msrcp_tree, hf_msrcp_ext_header, tvb, 0, 0, ENC_ASCII);
        nxt_tree = proto_item_add_subtree(nxt_ti, ett_msrcp_nxt);
        proto_tree_add_item(nxt_tree, hf_msrcp_ext_next_header, tvb, tree_offset, 2, ENC_LITTLE_ENDIAN);
        tree_offset += 2;
        proto_tree_add_item(nxt_tree, hf_msrcp_ext_len, tvb, tree_offset, 2, ENC_LITTLE_ENDIAN);
        tree_offset += 2;
        proto_tree_add_item(nxt_tree, hf_msrcp_ext_res, tvb, tree_offset, 4, ENC_LITTLE_ENDIAN);

    }
    else
    {
        next_tvb = tvb_new_subset_remaining(tvb, 0);
        call_dissector(eth_handle, next_tvb, pinfo, msrcp_tree);
    }

    return tvb_captured_length(tvb);
}


void
proto_register_msrcp(void)
{
    expert_module_t* expert_msrcp;

    static hf_register_info hf[] = {
    { &hf_msrcp_id,
        { "MSRCP ID", "msrcp.id",
        FT_UINT32, BASE_DEC_HEX,
        NULL, 0x0,
        NULL, HFILL},
    },
    { &hf_msrcp_vers,
        { "Version", "msrcp.vers",
        FT_UINT8, BASE_DEC,
        NULL, 0x0,
        NULL, HFILL}
    },
    { &hf_msrcp_reserved,
        { "Reserved", "msrcp.reserved",
        FT_UINT8, BASE_DEC,
        NULL, 0x0,
        NULL, HFILL}
    },
    { &hf_msrcp_type,
        { "MSRCP Type", "msrcp.type",
        FT_UINT16, BASE_DEC,
        VALS(packettypenames), 0x0,
        NULL, HFILL}
    },
    { &hf_msrcp_next_header,
        { "Next Header", "msrcp.nxt_header",
        FT_UINT16, BASE_DEC,
        VALS(headertypenames), 0x0,
        NULL, HFILL}
    },
    { &hf_msrcp_len,
        { "Total Length", "msrcp.len",
        FT_UINT16, BASE_DEC,
        NULL, 0x0,
        NULL, HFILL}
    },
    { &hf_msrcp_seq,
        { "Sequence Number", "msrcp.seq",
        FT_UINT32, BASE_DEC_HEX,
        NULL, 0x0,
        NULL, HFILL}
    },
    { &hf_msrcp_response_in,
        { "Response In", "msrcp.response_in",
        FT_FRAMENUM, BASE_NONE, FRAMENUM_TYPE(FT_FRAMENUM_RESPONSE), 0x0,
        "The response to this MSRCP request is in frame", HFILL}
    },
    { &hf_msrcp_response_to,
        { "Request In", "msrcp.response_to",
        FT_FRAMENUM,BASE_NONE, FRAMENUM_TYPE(FT_FRAMENUM_REQUEST), 0x0,
        "This is a response to an MSRCP request in frame", HFILL}
    },
    { &hf_msrcp_ext_header,
        { "Extension Header", "msrcp.ext",
        FT_NONE, BASE_NONE,
        NULL, 0x0,
        NULL, HFILL}
    },
    { &hf_msrcp_ext_next_header,
        { "Next Header", "msrcp.ext_nxt_header",
        FT_UINT16, BASE_DEC,
        VALS(headertypenames), 0x0, NULL, HFILL}
    },
    { &hf_msrcp_ext_len,
        { "Length", "msrcp.ext_len",
        FT_UINT16, BASE_DEC,
        NULL, 0x0, NULL, HFILL}
    },
    { &hf_msrcp_ext_res,
        { "Reserved", "msrcp.nxt_res",
        FT_UINT32, BASE_HEX,
        NULL, 0x0,
        NULL, HFILL}
    },
    };

    static gint* ett[] = {
            &ett_msrcp,
            &ett_msrcp_nxt
    };

    static ei_register_info ei[] = {
        {
            &ei_msrcp_no_resp,
            { "msrcp.no_resp", PI_SEQUENCE, PI_WARN,
              "MSRCP Response not found", EXPFILL }
        }
    };

    proto_msrcp = proto_register_protocol("MSRCP Protocol", "MSRCP", "msrcp");

    proto_register_field_array(proto_msrcp, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
    expert_msrcp = expert_register_protocol(proto_msrcp);
    expert_register_field_array(expert_msrcp, ei, array_length(ei));

    msrcp_handle = register_dissector("msrcp", dissect_msrcp, proto_msrcp);
}

void
proto_reg_handoff_msrcp(void)
{
    eth_handle = find_dissector_add_dependency("eth_withoutfcs", proto_msrcp);
    dissector_add_uint("udp.port", MSRCP_PORT, msrcp_handle);
}