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-mactelnet.c
Line
Count
Source
1
/* packet-mactelnet.c
2
 * Routines for MAC-Telnet dissection
3
 * Copyright 2010, Haakon Nessjoen <haakon.nessjoen@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
/*
13
 * Thanks to "omniflux" for dissecting the protocol by hand before me.
14
 * http://www.omniflux.com/devel/mikrotik/Mikrotik_MAC_Telnet_Procotol.txt
15
 */
16
17
#include "config.h"
18
19
#include <epan/packet.h>
20
#include <epan/to_str.h>
21
22
void proto_register_mactelnet(void);
23
void proto_reg_handoff_mactelnet(void);
24
25
/* Initialize the protocol and registered fields */
26
static int proto_mactelnet;
27
static int hf_mactelnet_control_packet;
28
static int hf_mactelnet_type;
29
static int hf_mactelnet_protocolver;
30
static int hf_mactelnet_source_mac;
31
static int hf_mactelnet_destination_mac;
32
static int hf_mactelnet_session_id;
33
static int hf_mactelnet_client_type;
34
static int hf_mactelnet_databytes;
35
static int hf_mactelnet_datatype;
36
static int hf_mactelnet_control;
37
static int hf_mactelnet_control_length;
38
static int hf_mactelnet_control_encryption_key;
39
static int hf_mactelnet_control_password;
40
static int hf_mactelnet_control_username;
41
static int hf_mactelnet_control_terminal;
42
static int hf_mactelnet_control_width;
43
static int hf_mactelnet_control_height;
44
45
15
#define MACTELNET_UDP_PORT      20561 /* Not IANA registered */
46
47
/* Control packet definition */
48
static const uint32_t control_packet = 0x563412FF;
49
50
/* Initialize the subtree pointers */
51
static int ett_mactelnet;
52
static int ett_mactelnet_control;
53
54
/* Packet types */
55
static const value_string packettypenames[] = {
56
    {   0, "Start session" },
57
    {   1, "Data" },
58
    {   2, "Acknowledge" },
59
    {   4, "Ping request" },
60
    {   5, "Ping response" },
61
    { 255, "End session" },
62
    { 0, NULL }
63
};
64
65
/* Known client types */
66
static const value_string clienttypenames[] = {
67
    { 0x0015, "MAC Telnet" },
68
    { 0x0f90, "Winbox" },
69
    { 0, NULL }
70
};
71
72
/* Known control-packet types */
73
static const value_string controlpackettypenames[] = {
74
    { 0, "Begin authentication" },
75
    { 1, "Encryption key" },
76
    { 2, "Password" },
77
    { 3, "Username" },
78
    { 4, "Terminal type" },
79
    { 5, "Terminal width" },
80
    { 6, "Terminal height" },
81
    { 9, "End authentication" },
82
    { 0, NULL }
83
};
84
85
86
static int
87
dissect_mactelnet(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
88
2
{
89
2
    proto_item *mactelnet_item;
90
2
    proto_tree *mactelnet_tree;
91
2
    proto_item *mactelnet_control_item;
92
2
    proto_tree *mactelnet_control_tree;
93
2
    int         foundping   = -1;
94
2
    int         foundclient = -1;
95
2
    int         foundserver = -1;
96
2
    uint16_t    type;
97
98
    /* Check that there's enough data */
99
2
    if (tvb_captured_length(tvb) < 18)
100
0
        return 0;
101
102
    /*  Get the type byte */
103
2
    type = tvb_get_uint8(tvb, 1);
104
105
2
    if ((type == 4) || (type == 5)) { /* Ping */
106
0
        foundping = 1;
107
2
    } else {
108
2
        int i = 0;
109
6
        while (clienttypenames[i].strptr != NULL) {
110
4
            if (tvb_get_ntohs(tvb, 14) == clienttypenames[i].value) {
111
0
                foundserver = i;
112
0
                break;
113
0
            }
114
4
            if (tvb_get_ntohs(tvb, 16) == clienttypenames[i].value) {
115
0
                foundclient = i;
116
0
                break;
117
0
            }
118
4
            i++;
119
4
        }
120
2
    }
121
122
    /* Not a mactelnet packet */
123
2
    if ((foundping < 0) && (foundclient < 0) && (foundserver < 0)) {
124
2
        return 0;
125
2
    }
126
127
    /* Make entries in Protocol column and Info column on summary display */
128
0
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "MAC-Telnet");
129
130
0
    col_add_fstr(pinfo->cinfo, COL_INFO, "%s > %s Direction: %s Type: %s",
131
0
                    tvb_ether_to_str(pinfo->pool, tvb, 2),
132
0
                    tvb_ether_to_str(pinfo->pool, tvb, 8),
133
0
                    ((foundclient >= 0) || (type == 4) ? "Client->Server" : "Server->Client" ),
134
0
                    val_to_str(pinfo->pool, type, packettypenames, "Unknown Type:0x%02x")
135
0
        );
136
137
0
    if (tree) {
138
0
        uint32_t offset = 0;
139
140
        /* create display subtree for the protocol */
141
0
        mactelnet_item = proto_tree_add_item(tree, proto_mactelnet, tvb, 0, -1, ENC_NA);
142
0
        mactelnet_tree = proto_item_add_subtree(mactelnet_item, ett_mactelnet);
143
144
        /* ver(1) */
145
0
        proto_tree_add_item(mactelnet_tree, hf_mactelnet_protocolver, tvb, offset, 1, ENC_NA);
146
0
        offset += 1;
147
148
        /* ptype(1) */
149
0
        proto_tree_add_item(mactelnet_tree, hf_mactelnet_type, tvb, offset, 1, ENC_NA);
150
0
        offset += 1;
151
152
        /* saddr(6) */
153
0
        proto_tree_add_item(mactelnet_tree, hf_mactelnet_source_mac, tvb, offset, 6, ENC_NA);
154
0
        offset += 6;
155
156
        /* dstaddr(6) */
157
0
        proto_tree_add_item(mactelnet_tree, hf_mactelnet_destination_mac, tvb, offset, 6, ENC_NA);
158
0
        offset += 6;
159
160
0
        if (foundserver >= 0) {
161
            /* Server to client */
162
163
            /* sessionid(2) */
164
0
            proto_tree_add_item(mactelnet_tree, hf_mactelnet_session_id, tvb, offset+2, 2, ENC_BIG_ENDIAN);
165
0
            offset += 2;
166
167
            /* clienttype(2) */
168
0
            proto_tree_add_item(mactelnet_tree, hf_mactelnet_client_type, tvb, offset-2, 2, ENC_BIG_ENDIAN);
169
0
            offset += 2;
170
0
        } else if (foundclient >= 0) {
171
            /* Client to server */
172
173
            /* sessionid(2) */
174
0
            proto_tree_add_item(mactelnet_tree, hf_mactelnet_session_id, tvb, offset, 2, ENC_BIG_ENDIAN);
175
0
            offset += 2;
176
177
            /* clienttype(2) */
178
0
            proto_tree_add_item(mactelnet_tree, hf_mactelnet_client_type, tvb, offset, 2, ENC_BIG_ENDIAN);
179
0
            offset += 2;
180
0
        } else if (foundping >= 0) {
181
            /* Skip empty data */
182
0
            offset += 4;
183
0
        }
184
185
0
        if (foundping < 0) {
186
            /* counter(4) */
187
0
            proto_tree_add_item(mactelnet_tree, hf_mactelnet_databytes, tvb, offset, 4, ENC_BIG_ENDIAN);
188
0
            offset += 4;
189
0
        }
190
191
        /* Data packets only */
192
0
        if (type == 1) {
193
0
            while(tvb_reported_length_remaining(tvb, offset) > 0) {
194
0
                if ((tvb_reported_length_remaining(tvb, offset) > 4) && (tvb_get_ntohl(tvb, offset) == control_packet)) {
195
0
                    uint8_t datatype;
196
0
                    uint32_t datalength;
197
198
                    /* Add subtree for control packet */
199
0
                    mactelnet_control_item = proto_tree_add_item(mactelnet_tree, hf_mactelnet_control, tvb, offset, -1, ENC_NA);
200
0
                    mactelnet_control_tree = proto_item_add_subtree(mactelnet_control_item, ett_mactelnet);
201
                    /* Control packet magic number (4) */
202
0
                    proto_tree_add_item(mactelnet_control_tree, hf_mactelnet_control_packet, tvb, offset, 4, ENC_BIG_ENDIAN);
203
0
                    offset += 4;
204
205
                    /* Control packet type (1) */
206
0
                    proto_tree_add_item_ret_uint8(mactelnet_control_tree, hf_mactelnet_datatype, tvb, offset, 1, ENC_NA, &datatype);
207
0
                    offset += 1;
208
209
                    /* Control packet length (4) */
210
0
                    proto_tree_add_item_ret_uint(mactelnet_control_tree, hf_mactelnet_control_length, tvb, offset, 4, ENC_BIG_ENDIAN, &datalength);
211
0
                    offset += 4;
212
213
0
                    switch (datatype) {
214
0
                        case 1: /* Encryption Key */
215
0
                            proto_tree_add_item(mactelnet_control_tree, hf_mactelnet_control_encryption_key, tvb, offset, datalength, ENC_NA);
216
0
                            break;
217
218
0
                        case 2: /* Password */
219
0
                            proto_tree_add_item(mactelnet_control_tree, hf_mactelnet_control_password, tvb, offset, datalength, ENC_NA);
220
0
                            break;
221
222
0
                        case 3: /* Username */
223
0
                            proto_tree_add_item(mactelnet_control_tree, hf_mactelnet_control_username, tvb, offset, datalength, ENC_ASCII);
224
0
                            break;
225
226
0
                        case 4: /* Terminal type */
227
0
                            proto_tree_add_item(mactelnet_control_tree, hf_mactelnet_control_terminal, tvb, offset, datalength, ENC_ASCII);
228
0
                            break;
229
230
0
                        case 5: /* Terminal width */
231
0
                            proto_tree_add_item(mactelnet_control_tree, hf_mactelnet_control_width, tvb, offset, 2, ENC_LITTLE_ENDIAN);
232
0
                            break;
233
234
0
                        case 6: /* Terminal height */
235
0
                            proto_tree_add_item(mactelnet_control_tree, hf_mactelnet_control_height, tvb, offset, 2, ENC_LITTLE_ENDIAN);
236
0
                            break;
237
238
0
                        case 9: /* End authentication (no data) */
239
0
                            break;
240
0
                    }
241
0
                    proto_item_set_len (mactelnet_control_item, datalength + 9);
242
0
                    offset += datalength;
243
0
                } else {
244
                    /* Data packet, let wireshark handle it */
245
0
                    tvbuff_t *next_client = tvb_new_subset_remaining(tvb, offset);
246
0
                    return call_data_dissector(next_client, pinfo, mactelnet_tree);
247
0
                }
248
0
            }
249
0
        } else if ((type == 4) || (type == 5)) {
250
            /* Data packet, let wireshark handle it */
251
0
            tvbuff_t *next_client = tvb_new_subset_remaining(tvb, offset);
252
0
            return call_data_dissector(next_client, pinfo, mactelnet_tree);
253
0
        }
254
255
256
0
    }
257
0
    return tvb_reported_length(tvb);
258
0
}
259
260
261
void
262
proto_register_mactelnet(void)
263
15
{
264
15
    static hf_register_info hf[] = {
265
15
        { &hf_mactelnet_control_packet,
266
15
          { "Control Packet Magic Number", "mactelnet.control_packet",
267
15
            FT_UINT32, BASE_HEX, NULL, 0x0,
268
15
            NULL, HFILL }
269
15
        },
270
15
        { &hf_mactelnet_type,
271
15
          { "Type", "mactelnet.type",
272
15
            FT_UINT8, BASE_DEC, VALS(packettypenames), 0x0,
273
15
            "Packet Type", HFILL }
274
15
        },
275
15
        { &hf_mactelnet_protocolver,
276
15
          { "Protocol Version", "mactelnet.protocol_version",
277
15
            FT_UINT8, BASE_DEC, NULL, 0x0,
278
15
            NULL, HFILL }
279
15
        },
280
15
        { &hf_mactelnet_source_mac,
281
15
          { "Source MAC", "mactelnet.source_mac",
282
15
            FT_ETHER, BASE_NONE, NULL , 0x0,
283
15
            NULL, HFILL }
284
15
        },
285
15
        { &hf_mactelnet_destination_mac,
286
15
          { "Destination MAC", "mactelnet.destination_mac",
287
15
            FT_ETHER, BASE_NONE, NULL , 0x0,
288
15
            NULL, HFILL }
289
15
        },
290
15
        { &hf_mactelnet_session_id,
291
15
          { "Session ID", "mactelnet.session_id",
292
15
            FT_UINT16, BASE_HEX, NULL , 0x0,
293
15
            "Session ID for this connection", HFILL }
294
15
        },
295
15
        { &hf_mactelnet_client_type,
296
15
          { "Client Type", "mactelnet.client_type",
297
15
            FT_UINT16, BASE_HEX, VALS(clienttypenames) , 0x0,
298
15
            NULL, HFILL }
299
15
        },
300
15
        { &hf_mactelnet_databytes,
301
15
          { "Session Data Bytes", "mactelnet.session_bytes",
302
15
            FT_UINT32, BASE_DEC, NULL , 0x0,
303
15
            "Session data bytes received", HFILL }
304
15
        },
305
15
        { &hf_mactelnet_datatype,
306
15
          { "Data Packet Type", "mactelnet.data_type",
307
15
            FT_UINT8, BASE_HEX, VALS(controlpackettypenames) , 0x0,
308
15
            NULL, HFILL }
309
15
        },
310
15
        { &hf_mactelnet_control,
311
15
          { "Control Packet", "mactelnet.control",
312
15
            FT_NONE, BASE_NONE, NULL , 0x0,
313
15
            NULL, HFILL }
314
15
        },
315
15
        { &hf_mactelnet_control_length,
316
15
          { "Control Data Length", "mactelnet.control_length",
317
15
            FT_UINT32, BASE_DEC, NULL , 0x0,
318
15
            "Control packet length", HFILL }
319
15
        },
320
15
        { &hf_mactelnet_control_encryption_key,
321
15
          { "Encryption Key", "mactelnet.control_encryptionkey",
322
15
            FT_BYTES, BASE_NONE, NULL , 0x0,
323
15
            "Login encryption key", HFILL }
324
15
        },
325
15
        { &hf_mactelnet_control_password,
326
15
          { "Password MD5", "mactelnet.control_password",
327
15
            FT_BYTES, BASE_NONE, NULL , 0x0,
328
15
            "Null padded MD5 password", HFILL }
329
15
        },
330
15
        { &hf_mactelnet_control_username,
331
15
          { "Username", "mactelnet.control_username",
332
15
            FT_STRING, BASE_NONE, NULL , 0x0,
333
15
            NULL, HFILL }
334
15
        },
335
15
        { &hf_mactelnet_control_terminal,
336
15
          { "Terminal Type", "mactelnet.control_terminaltype",
337
15
            FT_STRING, BASE_NONE, NULL , 0x0,
338
15
            NULL, HFILL }
339
15
        },
340
15
        { &hf_mactelnet_control_width,
341
15
          { "Terminal Width", "mactelnet.control_width",
342
15
            FT_UINT16, BASE_DEC, NULL , 0x0,
343
15
            NULL, HFILL }
344
15
        },
345
15
        { &hf_mactelnet_control_height,
346
15
          { "Terminal Height", "mactelnet.control_height",
347
15
            FT_UINT16, BASE_DEC, NULL , 0x0,
348
15
            NULL, HFILL }
349
15
        }
350
15
    };
351
352
    /* Setup protocol subtree array */
353
15
    static int *ett[] = {
354
15
        &ett_mactelnet,
355
15
        &ett_mactelnet_control,
356
15
    };
357
358
    /* Register the protocol name and description */
359
15
    proto_mactelnet = proto_register_protocol ("MikroTik MAC-Telnet Protocol", "MAC-Telnet", "mactelnet");
360
15
    register_dissector("mactelnet", dissect_mactelnet, proto_mactelnet);
361
362
    /* Required function calls to register the header fields and subtrees used */
363
15
    proto_register_field_array (proto_mactelnet, hf, array_length (hf));
364
15
    proto_register_subtree_array (ett, array_length (ett));
365
15
}
366
367
void
368
proto_reg_handoff_mactelnet(void)
369
15
{
370
15
    dissector_add_uint_with_preference("udp.port", MACTELNET_UDP_PORT, find_dissector("mactelnet"));
371
15
}
372
373
/*
374
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
375
 *
376
 * Local variables:
377
 * c-basic-offset: 4
378
 * tab-width: 8
379
 * indent-tabs-mode: nil
380
 * End:
381
 *
382
 * vi: set shiftwidth=4 tabstop=8 expandtab:
383
 * :indentSize=4:tabSize=8:noTabs=true:
384
 */