Coverage Report

Created: 2026-11-12 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wireshark/epan/dissectors/packet-classicstun.c
Line
Count
Source
1
/* packet-classicstun.c
2
 * Routines for Simple Traversal of UDP Through NAT dissection
3
 * Copyright 2003, Shiang-Ming Huang <smhuang@pcs.csie.nctu.edu.tw>
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
 * Please refer to RFC 3489 for protocol detail.
12
 * (supports extra message attributes described in draft-ietf-behave-rfc3489bis-00)
13
 */
14
15
#include "config.h"
16
17
#include <epan/packet.h>
18
#include <epan/expert.h>
19
#include <epan/tfs.h>
20
#include <epan/conversation.h>
21
#include <wsutil/ws_padding_to.h>
22
23
void proto_register_classicstun(void);
24
void proto_reg_handoff_classicstun(void);
25
26
/* heuristic subdissectors */
27
static heur_dissector_list_t heur_subdissector_list;
28
29
static dissector_handle_t data_handle;
30
31
/* Initialize the protocol and registered fields */
32
static int proto_classicstun;
33
34
static int hf_classicstun_type; /* CLASSIC-STUN message header */
35
static int hf_classicstun_length;
36
static int hf_classicstun_id;
37
static int hf_classicstun_att;
38
static int hf_classicstun_response_in;
39
static int hf_classicstun_response_to;
40
static int hf_classicstun_time;
41
42
43
static int hf_classicstun_att_type; /* CLASSIC-STUN attribute fields */
44
static int hf_classicstun_att_length;
45
static int hf_classicstun_att_value;
46
static int hf_classicstun_att_padding;
47
static int hf_classicstun_att_family;
48
static int hf_classicstun_att_ipv4;
49
static int hf_classicstun_att_ipv6;
50
static int hf_classicstun_att_port;
51
static int hf_classicstun_att_change_ip;
52
static int hf_classicstun_att_change_port;
53
static int hf_classicstun_att_unknown;
54
static int hf_classicstun_att_error_class;
55
static int hf_classicstun_att_error_number;
56
static int hf_classicstun_att_error_reason;
57
static int hf_classicstun_att_server_string;
58
static int hf_classicstun_att_xor_ipv4;
59
static int hf_classicstun_att_xor_ipv6;
60
static int hf_classicstun_att_xor_port;
61
static int hf_classicstun_att_lifetime;
62
static int hf_classicstun_att_magic_cookie;
63
static int hf_classicstun_att_bandwidth;
64
static int hf_classicstun_att_data;
65
static int hf_classicstun_att_connection_request_binding;
66
67
static expert_field ei_classicstun_att_padding;
68
69
/* Structure containing transaction specific information */
70
typedef struct _classicstun_transaction_t {
71
    uint32_t req_frame;
72
    uint32_t rep_frame;
73
    nstime_t req_time;
74
} classicstun_transaction_t;
75
76
/* Structure containing conversation specific information */
77
typedef struct _classicstun_conv_info_t {
78
    wmem_tree_t *pdus;
79
} classicstun_conv_info_t;
80
81
82
/* Message Types */
83
#define BINDING_REQUEST                       0x0001
84
#define BINDING_RESPONSE                      0x0101
85
#define BINDING_ERROR_RESPONSE                0x0111
86
#define SHARED_SECRET_REQUEST                 0x0002
87
#define SHARED_SECRET_RESPONSE                0x0102
88
#define SHARED_SECRET_ERROR_RESPONSE          0x1112
89
#define ALLOCATE_REQUEST                      0x0003
90
#define ALLOCATE_RESPONSE                     0x0103
91
#define ALLOCATE_ERROR_RESPONSE               0x0113
92
#define SEND_REQUEST                          0x0004
93
#define SEND_RESPONSE                         0x0104
94
#define SEND_ERROR_RESPONSE                   0x0114
95
#define DATA_INDICATION                       0x0115
96
#define SET_ACTIVE_DESTINATION_REQUEST        0x0006
97
#define SET_ACTIVE_DESTINATION_RESPONSE       0x0106
98
#define SET_ACTIVE_DESTINATION_ERROR_RESPONSE 0x0116
99
100
101
/* Message classes */
102
31
#define CLASS_MASK                            0xC110
103
24
#define REQUEST                               0x0000
104
#define INDICATION                            0x0001
105
7
#define RESPONSE                              0x0010
106
0
#define ERROR_RESPONSE                        0x0011
107
108
/* Attribute Types */
109
1
#define MAPPED_ADDRESS                        0x0001
110
1
#define RESPONSE_ADDRESS                      0x0002
111
0
#define CHANGE_REQUEST                        0x0003
112
1
#define SOURCE_ADDRESS                        0x0004
113
1
#define CHANGED_ADDRESS                       0x0005
114
0
#define USERNAME                              0x0006
115
0
#define PASSWORD                              0x0007
116
0
#define MESSAGE_INTEGRITY                     0x0008
117
0
#define ERROR_CODE                            0x0009
118
0
#define UNKNOWN_ATTRIBUTES                    0x000a
119
1
#define REFLECTED_FROM                        0x000b
120
0
#define LIFETIME                              0x000d
121
1
#define ALTERNATE_SERVER                      0x000e
122
0
#define MAGIC_COOKIE                          0x000f
123
0
#define BANDWIDTH                             0x0010
124
1
#define DESTINATION_ADDRESS                   0x0011
125
1
#define REMOTE_ADDRESS                        0x0012
126
0
#define DATA                                  0x0013
127
0
#define NONCE                                 0x0014
128
0
#define REALM                                 0x0015
129
0
#define REQUESTED_ADDRESS_TYPE                0x0016
130
0
#define XOR_MAPPED_ADDRESS                    0x8020
131
#define XOR_ONLY                              0x0021
132
0
#define SERVER                                0x8022
133
0
#define CONNECTION_REQUEST_BINDING            0xc001
134
#define BINDING_CHANGE                        0xc002
135
136
137
138
/* Initialize the subtree pointers */
139
static int ett_classicstun;
140
static int ett_classicstun_att_type;
141
static int ett_classicstun_att;
142
143
144
#define UDP_PORT_STUN   3478
145
#define TCP_PORT_STUN   3478
146
147
148
2.48k
#define CLASSICSTUN_HDR_LEN ((unsigned)20) /* CLASSIC-STUN message header length */
149
122
#define ATTR_HDR_LEN                 4  /* CLASSIC-STUN attribute header length */
150
151
152
static const value_string messages[] = {
153
    {BINDING_REQUEST                       , "Binding Request"},
154
    {BINDING_RESPONSE                      , "Binding Response"},
155
    {BINDING_ERROR_RESPONSE                , "Binding Error Response"},
156
    {SHARED_SECRET_REQUEST                 , "Shared Secret Request"},
157
    {SHARED_SECRET_RESPONSE                , "Shared Secret Response"},
158
    {SHARED_SECRET_ERROR_RESPONSE          , "Shared Secret Error Response"},
159
    {ALLOCATE_REQUEST                      , "Allocate Request"},
160
    {ALLOCATE_RESPONSE                     , "Allocate Response"},
161
    {ALLOCATE_ERROR_RESPONSE               , "Allocate Error Response"},
162
    {SEND_REQUEST                          , "Send Request"},
163
    {SEND_RESPONSE                         , "Send Response"},
164
    {SEND_ERROR_RESPONSE                   , "Send Error Response"},
165
    {DATA_INDICATION                       , "Data Indication"},
166
    {SET_ACTIVE_DESTINATION_REQUEST        , "Set Active Destination Request"},
167
    {SET_ACTIVE_DESTINATION_RESPONSE       , "Set Active Destination Response"},
168
    {SET_ACTIVE_DESTINATION_ERROR_RESPONSE , "Set Active Destination Error Response"},
169
    {0x00                                  , NULL}
170
};
171
172
static const value_string attributes[] = {
173
    {MAPPED_ADDRESS                        , "MAPPED-ADDRESS"},
174
    {RESPONSE_ADDRESS                      , "RESPONSE-ADDRESS"},
175
    {CHANGE_REQUEST                        , "CHANGE-REQUEST"},
176
    {SOURCE_ADDRESS                        , "SOURCE-ADDRESS"},
177
    {CHANGED_ADDRESS                       , "CHANGED-ADDRESS"},
178
    {USERNAME                              , "USERNAME"},
179
    {PASSWORD                              , "PASSWORD"},
180
    {MESSAGE_INTEGRITY                     , "MESSAGE-INTEGRITY"},
181
    {ERROR_CODE                            , "ERROR-CODE"},
182
    {REFLECTED_FROM                        , "REFLECTED-FROM"},
183
    {LIFETIME                              , "LIFETIME"},
184
    {ALTERNATE_SERVER                      , "ALTERNATE_SERVER"},
185
    {MAGIC_COOKIE                          , "MAGIC_COOKIE"},
186
    {BANDWIDTH                             , "BANDWIDTH"},
187
    {DESTINATION_ADDRESS                   , "DESTINATION_ADDRESS"},
188
    {REMOTE_ADDRESS                        , "REMOTE_ADDRESS"},
189
    {DATA                                  , "DATA"},
190
    {NONCE                                 , "NONCE"},
191
    {REALM                                 , "REALM"},
192
    {REQUESTED_ADDRESS_TYPE                , "REQUESTED_ADDRESS_TYPE"},
193
    {XOR_MAPPED_ADDRESS                    , "XOR_MAPPED_ADDRESS"},
194
    {XOR_ONLY                              , "XOR_ONLY"},
195
    {SERVER                                , "SERVER"},
196
    {CONNECTION_REQUEST_BINDING            , "CONNECTION-REQUEST-BINDING"},
197
    {BINDING_CHANGE                        , "BINDING-CHANGE"},
198
    {0x00                                  , NULL}
199
};
200
201
static const value_string attributes_family[] = {
202
    {0x0001                                , "IPv4"},
203
    {0x0002                                , "IPv6"},
204
    {0x00                                  , NULL}
205
};
206
207
static int
208
dissect_classicstun(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
209
2.41k
{
210
211
2.41k
    proto_item                *ti;
212
2.41k
    proto_item                *ta;
213
2.41k
    proto_item                *length_item;
214
2.41k
    proto_tree                *classicstun_tree;
215
2.41k
    proto_tree                *att_type_tree;
216
2.41k
    proto_tree                *att_tree;
217
2.41k
    uint16_t                   msg_type;
218
2.41k
    uint16_t                   msg_length;
219
2.41k
    const char                *msg_type_str;
220
2.41k
    uint16_t                   att_type;
221
2.41k
    uint16_t                   att_length, clear_port;
222
2.41k
    uint32_t                   clear_ip;
223
2.41k
    uint16_t                   offset;
224
2.41k
    unsigned                   len;
225
2.41k
    unsigned                   i;
226
2.41k
    conversation_t            *conversation;
227
2.41k
    classicstun_conv_info_t   *classicstun_info;
228
2.41k
    classicstun_transaction_t *classicstun_trans;
229
2.41k
    wmem_tree_key_t            transaction_id_key[2];
230
2.41k
    uint32_t                   transaction_id[4];
231
232
233
    /*
234
     * First check if the frame is really meant for us.
235
     */
236
2.41k
    len = tvb_captured_length(tvb);
237
    /* First, make sure we have enough data to do the check. */
238
2.41k
    if (len < CLASSICSTUN_HDR_LEN)
239
761
        return 0;
240
241
1.65k
    msg_type = tvb_get_ntohs(tvb, 0);
242
243
1.65k
    if (msg_type & 0xC000 || tvb_get_ntohl(tvb, 4) == 0x2112a442 /* New STUN */
244
967
        || tvb_get_ntohl(tvb, 4) == 0x7f5a9bc7) /* XMCP */
245
692
        return 0;
246
247
    /* check if message type is correct */
248
962
    msg_type_str = try_val_to_str(msg_type, messages);
249
962
    if (msg_type_str == NULL)
250
910
        return 0;
251
252
52
    msg_length = tvb_get_ntohs(tvb, 2);
253
254
    /* check if payload enough */
255
52
    if (len != CLASSICSTUN_HDR_LEN+msg_length)
256
40
        return 0;
257
258
    /* The message seems to be a valid CLASSIC-STUN message! */
259
260
    /* Create the transaction key which may be used
261
       to track the conversation */
262
12
    transaction_id[0] = tvb_get_ntohl(tvb, 4);
263
12
    transaction_id[1] = tvb_get_ntohl(tvb, 8);
264
12
    transaction_id[2] = tvb_get_ntohl(tvb, 12);
265
12
    transaction_id[3] = tvb_get_ntohl(tvb, 16);
266
267
12
    transaction_id_key[0].length = 4;
268
12
    transaction_id_key[0].key    = transaction_id;
269
12
    transaction_id_key[1].length = 0;
270
12
    transaction_id_key[1].key    = NULL;
271
272
    /*
273
     * Do we have a conversation for this connection?
274
     */
275
12
    conversation = find_or_create_conversation(pinfo);
276
277
    /*
278
     * Do we already have a state structure for this conv
279
     */
280
12
    classicstun_info = (classicstun_conv_info_t *)conversation_get_proto_data(conversation, proto_classicstun);
281
12
    if (!classicstun_info) {
282
        /* No.  Attach that information to the conversation, and add
283
         * it to the list of information structures.
284
         */
285
7
        classicstun_info = wmem_new(wmem_file_scope(), classicstun_conv_info_t);
286
7
        classicstun_info->pdus=wmem_tree_new(wmem_file_scope());
287
7
        conversation_add_proto_data(conversation, proto_classicstun, classicstun_info);
288
7
    }
289
290
12
    if(!pinfo->fd->visited){
291
12
        if (((msg_type & CLASS_MASK) >> 4) == REQUEST) {
292
            /* This is a request */
293
5
            classicstun_trans=wmem_new(wmem_file_scope(), classicstun_transaction_t);
294
5
            classicstun_trans->req_frame=pinfo->num;
295
5
            classicstun_trans->rep_frame=0;
296
5
            classicstun_trans->req_time=pinfo->abs_ts;
297
5
            wmem_tree_insert32_array(classicstun_info->pdus, transaction_id_key,
298
5
                           (void *)classicstun_trans);
299
7
        } else {
300
7
            classicstun_trans=(classicstun_transaction_t *)wmem_tree_lookup32_array(classicstun_info->pdus,
301
7
                                 transaction_id_key);
302
7
            if(classicstun_trans){
303
0
                classicstun_trans->rep_frame=pinfo->num;
304
0
            }
305
7
        }
306
12
    } else {
307
0
        classicstun_trans=(classicstun_transaction_t *)wmem_tree_lookup32_array(classicstun_info->pdus, transaction_id_key);
308
0
    }
309
12
    if(!classicstun_trans){
310
        /* create a "fake" pana_trans structure */
311
7
        classicstun_trans=wmem_new(pinfo->pool, classicstun_transaction_t);
312
7
        classicstun_trans->req_frame=0;
313
7
        classicstun_trans->rep_frame=0;
314
7
        classicstun_trans->req_time=pinfo->abs_ts;
315
7
    }
316
317
318
319
12
    col_set_str(pinfo->cinfo, COL_PROTOCOL, "CLASSIC-STUN");
320
321
12
    col_add_fstr(pinfo->cinfo, COL_INFO, "Message: %s",
322
12
             msg_type_str);
323
324
12
    unsigned transaction_id_first_word;
325
326
12
    ti = proto_tree_add_item(tree, proto_classicstun, tvb, 0, -1, ENC_NA);
327
328
12
    classicstun_tree = proto_item_add_subtree(ti, ett_classicstun);
329
330
12
    if (((msg_type & CLASS_MASK) >> 4) == REQUEST) {
331
5
        if (classicstun_trans->rep_frame) {
332
0
            proto_item *it;
333
0
            it=proto_tree_add_uint(classicstun_tree, hf_classicstun_response_in,
334
0
                           tvb, 0, 0,
335
0
                           classicstun_trans->rep_frame);
336
0
            proto_item_set_generated(it);
337
0
        }
338
5
    }
339
7
    else if ((((msg_type & CLASS_MASK) >> 4) == RESPONSE) ||
340
7
         (((msg_type & CLASS_MASK) >> 4) == ERROR_RESPONSE)) {
341
        /* This is a response */
342
7
        if(classicstun_trans->req_frame){
343
0
            proto_item *it;
344
0
            nstime_t ns;
345
346
0
            it=proto_tree_add_uint(classicstun_tree, hf_classicstun_response_to, tvb, 0, 0, classicstun_trans->req_frame);
347
0
            proto_item_set_generated(it);
348
349
0
            nstime_delta(&ns, &pinfo->abs_ts, &classicstun_trans->req_time);
350
0
            it=proto_tree_add_time(classicstun_tree, hf_classicstun_time, tvb, 0, 0, &ns);
351
0
            proto_item_set_generated(it);
352
0
        }
353
354
7
    }
355
356
12
    proto_tree_add_uint(classicstun_tree, hf_classicstun_type, tvb, 0, 2, msg_type);
357
12
    proto_tree_add_uint(classicstun_tree, hf_classicstun_length, tvb, 2, 2, msg_length);
358
12
    proto_tree_add_item(classicstun_tree, hf_classicstun_id, tvb, 4, 16, ENC_NA);
359
360
    /* Remember this (in host order) so we can show clear xor'd addresses */
361
12
    transaction_id_first_word = tvb_get_ntohl(tvb, 4);
362
363
12
    if (msg_length > 0) {
364
7
        ta = proto_tree_add_item(classicstun_tree, hf_classicstun_att, tvb, CLASSICSTUN_HDR_LEN, msg_length, ENC_NA);
365
7
        att_type_tree = proto_item_add_subtree(ta, ett_classicstun_att_type);
366
367
7
        offset = CLASSICSTUN_HDR_LEN;
368
369
7
        unsigned att_length_pad;
370
371
42
        while( msg_length > 0) {
372
42
            att_type = tvb_get_ntohs(tvb, offset); /* Type field in attribute header */
373
42
            att_length = tvb_get_ntohs(tvb, offset+2); /* Length field in attribute header */
374
375
42
            att_tree = proto_tree_add_subtree_format(att_type_tree, tvb, offset,
376
42
                         ATTR_HDR_LEN+att_length, ett_classicstun_att, NULL,
377
42
                         "Attribute: %s",
378
42
                         val_to_str(pinfo->pool, att_type, attributes, "Unknown (0x%04x)"));
379
380
42
            proto_tree_add_uint(att_tree, hf_classicstun_att_type, tvb,
381
42
                        offset, 2, att_type);
382
42
            offset += 2;
383
42
            if (ATTR_HDR_LEN+att_length > msg_length) {
384
6
                proto_tree_add_uint_format_value(att_tree,
385
6
                               hf_classicstun_att_length, tvb, offset, 2,
386
6
                               att_length,
387
6
                               "%u (bogus, goes past the end of the message)",
388
6
                               att_length);
389
6
                break;
390
6
            }
391
36
            length_item = proto_tree_add_uint(att_tree, hf_classicstun_att_length, tvb,
392
36
                        offset, 2, att_length);
393
36
            offset += 2;
394
36
            switch( att_type ){
395
1
                case MAPPED_ADDRESS:
396
1
                case RESPONSE_ADDRESS:
397
1
                case SOURCE_ADDRESS:
398
1
                case CHANGED_ADDRESS:
399
1
                case REFLECTED_FROM:
400
1
                case ALTERNATE_SERVER:
401
1
                case DESTINATION_ADDRESS:
402
1
                case REMOTE_ADDRESS:
403
1
                    if (att_length < 2)
404
1
                        break;
405
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_family, tvb, offset+1, 1, ENC_BIG_ENDIAN);
406
0
                    if (att_length < 4)
407
0
                        break;
408
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_port, tvb, offset+2, 2, ENC_BIG_ENDIAN);
409
0
                    switch( tvb_get_uint8(tvb, offset+1) ){
410
0
                        case 1:
411
0
                            if (att_length < 8)
412
0
                                break;
413
0
                            proto_tree_add_item(att_tree, hf_classicstun_att_ipv4, tvb, offset+4, 4, ENC_BIG_ENDIAN);
414
0
                            break;
415
416
0
                        case 2:
417
0
                            if (att_length < 20)
418
0
                                break;
419
0
                            proto_tree_add_item(att_tree, hf_classicstun_att_ipv6, tvb, offset+4, 16, ENC_NA);
420
0
                            break;
421
0
                    }
422
0
                    break;
423
424
0
                case CHANGE_REQUEST:
425
0
                    if (att_length < 4)
426
0
                        break;
427
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_change_ip, tvb, offset, 4, ENC_BIG_ENDIAN);
428
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_change_port, tvb, offset, 4, ENC_BIG_ENDIAN);
429
0
                    break;
430
431
0
                case USERNAME:
432
0
                case PASSWORD:
433
0
                case MESSAGE_INTEGRITY:
434
0
                case NONCE:
435
0
                case REALM:
436
0
                    if (att_length < 1)
437
0
                        break;
438
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_value, tvb, offset, att_length, ENC_NA);
439
0
                    break;
440
441
0
                case ERROR_CODE:
442
0
                    if (att_length < 3)
443
0
                        break;
444
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_error_class, tvb, offset+2, 1, ENC_BIG_ENDIAN);
445
0
                    if (att_length < 4)
446
0
                        break;
447
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_error_number, tvb, offset+3, 1, ENC_BIG_ENDIAN);
448
0
                    if (att_length < 5)
449
0
                        break;
450
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_error_reason, tvb, offset+4, (att_length-4), ENC_UTF_8);
451
0
                    break;
452
453
0
                case LIFETIME:
454
0
                    if (att_length < 4)
455
0
                        break;
456
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_lifetime, tvb, offset, 4, ENC_BIG_ENDIAN);
457
0
                    break;
458
459
0
                case MAGIC_COOKIE:
460
0
                    if (att_length < 4)
461
0
                        break;
462
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_magic_cookie, tvb, offset, 4, ENC_BIG_ENDIAN);
463
0
                    break;
464
465
0
                case BANDWIDTH:
466
0
                    if (att_length < 4)
467
0
                        break;
468
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_bandwidth, tvb, offset, 4, ENC_BIG_ENDIAN);
469
0
                    break;
470
471
0
                case DATA:
472
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_data, tvb, offset, att_length, ENC_NA);
473
474
0
                    tvbuff_t *next_tvb;
475
0
                    heur_dtbl_entry_t *hdtbl_entry;
476
0
                    next_tvb = tvb_new_subset_length(tvb, offset, att_length);
477
478
0
                    if (!dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, att_tree, &hdtbl_entry, NULL)) {
479
0
                        call_dissector_only(data_handle, next_tvb, pinfo, att_tree, NULL);
480
0
                    }
481
482
0
                    break;
483
484
0
                case UNKNOWN_ATTRIBUTES:
485
0
                    for (i = 0; i < att_length; i += 4) {
486
0
                        proto_tree_add_item(att_tree, hf_classicstun_att_unknown, tvb, offset+i, 2, ENC_BIG_ENDIAN);
487
0
                        proto_tree_add_item(att_tree, hf_classicstun_att_unknown, tvb, offset+i+2, 2, ENC_BIG_ENDIAN);
488
0
                    }
489
0
                    break;
490
491
0
                case SERVER:
492
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_server_string, tvb, offset, att_length, ENC_UTF_8);
493
0
                    break;
494
495
0
                case XOR_MAPPED_ADDRESS:
496
0
                    if (att_length < 2)
497
0
                        break;
498
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_family, tvb, offset+1, 1, ENC_BIG_ENDIAN);
499
0
                    if (att_length < 4)
500
0
                        break;
501
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_xor_port, tvb, offset+2, 2, ENC_BIG_ENDIAN);
502
503
                    /* Show the port 'in the clear'
504
                       XOR (host order) transid with (host order) xor-port.
505
                       Add host-order port into tree. */
506
0
                    clear_port = tvb_get_ntohs(tvb, offset+2) ^ (transaction_id_first_word >> 16);
507
0
                    ti = proto_tree_add_uint(att_tree, hf_classicstun_att_port, tvb, offset+2, 2, clear_port);
508
0
                    proto_item_set_generated(ti);
509
510
0
                    switch( tvb_get_uint8(tvb, offset+1) ){
511
0
                        case 1:
512
0
                            if (att_length < 8)
513
0
                                break;
514
0
                            proto_tree_add_item(att_tree, hf_classicstun_att_xor_ipv4, tvb, offset+4, 4, ENC_BIG_ENDIAN);
515
516
                            /* Show the address 'in the clear'.
517
                               XOR (host order) transid with (host order) xor-address.
518
                               Add in network order tree. */
519
0
                            clear_ip = tvb_get_ipv4(tvb, offset+4) ^ g_htonl(transaction_id_first_word);
520
0
                            ti = proto_tree_add_ipv4(att_tree, hf_classicstun_att_ipv4, tvb, offset+4, 4, clear_ip);
521
0
                            proto_item_set_generated(ti);
522
0
                            break;
523
524
0
                        case 2:
525
0
                            if (att_length < 20)
526
0
                                break;
527
0
                            proto_tree_add_item(att_tree, hf_classicstun_att_xor_ipv6, tvb, offset+4, 16, ENC_NA);
528
0
                            break;
529
0
                    }
530
0
                    break;
531
532
0
                case REQUESTED_ADDRESS_TYPE:
533
0
                    if (att_length < 2)
534
0
                        break;
535
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_family, tvb, offset+1, 1, ENC_BIG_ENDIAN);
536
0
                    break;
537
538
0
                case CONNECTION_REQUEST_BINDING:
539
0
                    proto_tree_add_item(att_tree, hf_classicstun_att_connection_request_binding, tvb, offset, att_length, ENC_UTF_8);
540
0
                    break;
541
542
34
                default:
543
34
                    break;
544
36
            }
545
35
            offset += att_length;
546
35
            msg_length -= ATTR_HDR_LEN+att_length;
547
            /* RFC 3489 aligns the attributes it defines on 32-bit boundaries
548
             * by padding them internally, such that att_length MUST always
549
             * be a multiple of 4. As RFC 5389 notes, this mechanism "never
550
             * worked for the few attributes that weren't aligned naturally
551
             * on 32 bit boundaries," such as USERNAME and PASSWORD. Note that
552
             * this dissector dissects some attribute types which were added
553
             * (and some later withdrawn) in draft-ietf-behave-rfc3489bis-00;
554
             * those also MUST be aligned.
555
             *
556
             * There exist widely used classic STUN implementations that fail
557
             * to pad attributes; some (but not all?) of those use the RFC 5389
558
             * padding (introduced in draft-ietf-behave-rfc3489bis-04) whereby
559
             * att_length has the attribute length prior to padding and padding
560
             * up to 32 bit boundaries is added outside the attribute, despite
561
             * not using the Magic Cookie introduced in rfc3489bis-03 that would
562
             * cause the RFC 5389 and later dissector to handle the frame.
563
             */
564
35
            att_length_pad = WS_PADDING_TO_4(att_length);
565
35
            if (att_length_pad && msg_length) {
566
                /* Possible padding. Apply heuristics. */
567
3
                if (msg_length == att_length_pad) {
568
                    /* There is exactly enough room for padding, and not enough
569
                     * for another attribute (ATTR_HDR_LEN is 4), so assume
570
                     * message ending padding. */
571
0
                    expert_add_info(pinfo, length_item, &ei_classicstun_att_padding);
572
0
                    proto_tree_add_uint(att_tree, hf_classicstun_att_padding, tvb, offset, att_length_pad, att_length_pad);
573
0
                    offset += att_length_pad;
574
0
                    msg_length -= att_length_pad;
575
3
                } else if (msg_length >= att_length_pad + ATTR_HDR_LEN) {
576
                    /* There is enough room for a next attribute either
577
                     * with or without padding. */
578
3
                    if (try_val_to_str(tvb_get_ntohs(tvb, offset), attributes) == NULL &&
579
3
                        try_val_to_str(tvb_get_ntohs(tvb, offset + att_length_pad), attributes)) {
580
581
                        /* It looks like a valid attribute assuming padding
582
                         * but not without assuming padding. Assume padding. */
583
0
                        expert_add_info(pinfo, length_item, &ei_classicstun_att_padding);
584
0
                        proto_tree_add_uint(att_tree, hf_classicstun_att_padding, tvb, offset, att_length_pad, att_length_pad);
585
0
                        offset += att_length_pad;
586
0
                        msg_length -= att_length_pad;
587
0
                    }
588
3
                }
589
3
            }
590
35
        }
591
7
    }
592
11
    return tvb_reported_length(tvb);
593
12
}
594
595
static bool
596
dissect_classicstun_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
597
2.41k
{
598
2.41k
    return dissect_classicstun(tvb, pinfo, tree, data) > 0;
599
2.41k
}
600
601
602
603
void
604
proto_register_classicstun(void)
605
16
{
606
16
    expert_module_t *expert_classicstun;
607
608
16
    static hf_register_info hf[] = {
609
16
        { &hf_classicstun_type,
610
16
            { "Message Type",   "classicstun.type",     FT_UINT16,
611
16
            BASE_HEX,   VALS(messages), 0x0,    NULL,   HFILL }
612
16
        },
613
16
        { &hf_classicstun_length,
614
16
            { "Message Length", "classicstun.length",   FT_UINT16,
615
16
            BASE_HEX,   NULL,   0x0,    NULL,   HFILL }
616
16
        },
617
16
        { &hf_classicstun_id,
618
16
            { "Message Transaction ID", "classicstun.id",   FT_BYTES,
619
16
            BASE_NONE,  NULL,   0x0,    NULL,   HFILL }
620
16
        },
621
16
        { &hf_classicstun_att,
622
16
            { "Attributes",     "classicstun.att",  FT_NONE,
623
16
            BASE_NONE,      NULL,   0x0,    NULL,   HFILL }
624
16
        },
625
16
        { &hf_classicstun_response_in,
626
16
            { "Response In", "classicstun.response_in",
627
16
            FT_FRAMENUM, BASE_NONE, FRAMENUM_TYPE(FT_FRAMENUM_RESPONSE), 0x0,
628
16
            "The response to this CLASSICSTUN query is in this frame", HFILL }},
629
16
        { &hf_classicstun_response_to,
630
16
            { "Request In", "classicstun.response_to",
631
16
            FT_FRAMENUM, BASE_NONE, FRAMENUM_TYPE(FT_FRAMENUM_REQUEST), 0x0,
632
16
            "This is a response to the CLASSICSTUN Request in this frame", HFILL }},
633
16
        { &hf_classicstun_time,
634
16
            { "Time", "classicstun.time",
635
16
            FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
636
16
            "The time between the Request and the Response", HFILL }},
637
638
        /* ////////////////////////////////////// */
639
16
        { &hf_classicstun_att_type,
640
16
            { "Attribute Type", "classicstun.att.type", FT_UINT16,
641
16
            BASE_HEX,   VALS(attributes),   0x0,    NULL,   HFILL }
642
16
        },
643
16
        { &hf_classicstun_att_length,
644
16
            { "Attribute Length",   "classicstun.att.length",   FT_UINT16,
645
16
            BASE_DEC,   NULL,   0x0,    NULL,   HFILL }
646
16
        },
647
16
        { &hf_classicstun_att_value,
648
16
            { "Value",  "classicstun.att.value",    FT_BYTES,
649
16
            BASE_NONE,  NULL,   0x0,    NULL,   HFILL }
650
16
        },
651
16
        { &hf_classicstun_att_padding,
652
16
            { "Padding",  "classicstun.att.padding",    FT_UINT16,
653
16
            BASE_DEC,   NULL,   0x0,    NULL,   HFILL }
654
16
        },
655
16
        { &hf_classicstun_att_family,
656
16
            { "Protocol Family",    "classicstun.att.family",   FT_UINT16,
657
16
            BASE_HEX,   VALS(attributes_family),    0x0,    NULL,   HFILL }
658
16
        },
659
16
        { &hf_classicstun_att_ipv4,
660
16
            { "IP",     "classicstun.att.ipv4", FT_IPv4,
661
16
            BASE_NONE,  NULL,   0x0,    NULL,   HFILL }
662
16
        },
663
16
        { &hf_classicstun_att_ipv6,
664
16
            { "IP",     "classicstun.att.ipv6", FT_IPv6,
665
16
            BASE_NONE,  NULL,   0x0,    NULL,   HFILL }
666
16
        },
667
16
        { &hf_classicstun_att_port,
668
16
            { "Port",   "classicstun.att.port", FT_UINT16,
669
16
            BASE_DEC,   NULL,   0x0,    NULL,   HFILL }
670
16
        },
671
16
        { &hf_classicstun_att_change_ip,
672
16
            { "Change IP","classicstun.att.change.ip",  FT_BOOLEAN,
673
16
            16,     TFS(&tfs_set_notset),   0x0004, NULL,   HFILL}
674
16
        },
675
16
        { &hf_classicstun_att_change_port,
676
16
            { "Change Port","classicstun.att.change.port",  FT_BOOLEAN,
677
16
            16,     TFS(&tfs_set_notset),   0x0002, NULL,   HFILL}
678
16
        },
679
16
        { &hf_classicstun_att_unknown,
680
16
            { "Unknown Attribute","classicstun.att.unknown",    FT_UINT16,
681
16
            BASE_HEX,   NULL,   0x0,    NULL,   HFILL}
682
16
        },
683
16
        { &hf_classicstun_att_error_class,
684
16
            { "Error Class","classicstun.att.error.class",  FT_UINT8,
685
16
            BASE_DEC,   NULL,   0x07,   NULL,   HFILL}
686
16
        },
687
16
        { &hf_classicstun_att_error_number,
688
16
            { "Error Code","classicstun.att.error", FT_UINT8,
689
16
            BASE_DEC,   NULL,   0x0,    NULL,   HFILL}
690
16
        },
691
16
        { &hf_classicstun_att_error_reason,
692
16
            { "Error Reason Phase","classicstun.att.error.reason",  FT_STRING,
693
16
            BASE_NONE,  NULL,   0x0,    NULL,   HFILL}
694
16
        },
695
16
        { &hf_classicstun_att_xor_ipv4,
696
16
            { "IP (XOR-d)",     "classicstun.att.ipv4-xord",    FT_IPv4,
697
16
            BASE_NONE,  NULL,   0x0,    NULL,   HFILL }
698
16
        },
699
16
        { &hf_classicstun_att_xor_ipv6,
700
16
            { "IP (XOR-d)",     "classicstun.att.ipv6-xord",    FT_IPv6,
701
16
            BASE_NONE,  NULL,   0x0,    NULL,   HFILL }
702
16
        },
703
16
        { &hf_classicstun_att_xor_port,
704
16
            { "Port (XOR-d)",   "classicstun.att.port-xord",    FT_UINT16,
705
16
            BASE_DEC,   NULL,   0x0,    NULL,   HFILL }
706
16
        },
707
16
        { &hf_classicstun_att_server_string,
708
16
            { "Server version","classicstun.att.server",    FT_STRING,
709
16
            BASE_NONE,  NULL,   0x0,    NULL,   HFILL}
710
16
        },
711
16
        { &hf_classicstun_att_lifetime,
712
16
            { "Lifetime",   "classicstun.att.lifetime", FT_UINT32,
713
16
            BASE_DEC,   NULL,   0x0,    NULL,   HFILL }
714
16
        },
715
16
        { &hf_classicstun_att_magic_cookie,
716
16
            { "Magic Cookie",   "classicstun.att.magic.cookie", FT_UINT32,
717
16
            BASE_HEX,   NULL,   0x0,    NULL,   HFILL }
718
16
        },
719
16
        { &hf_classicstun_att_bandwidth,
720
16
            { "Bandwidth",  "classicstun.att.bandwidth",    FT_UINT32,
721
16
            BASE_DEC,   NULL,   0x0,    NULL,   HFILL }
722
16
        },
723
16
        { &hf_classicstun_att_data,
724
16
            { "Data",   "classicstun.att.data", FT_BYTES,
725
16
            BASE_NONE,  NULL,   0x0,    NULL,   HFILL }
726
16
        },
727
16
        { &hf_classicstun_att_connection_request_binding,
728
16
            { "Connection Request Binding", "classicstun.att.connection_request_binding", FT_STRING,
729
16
            BASE_NONE,  NULL, 0x0,  NULL,   HFILL }
730
16
        },
731
16
    };
732
733
/* Setup protocol subtree array */
734
16
    static int *ett[] = {
735
16
        &ett_classicstun,
736
16
        &ett_classicstun_att_type,
737
16
        &ett_classicstun_att,
738
16
    };
739
740
16
    static ei_register_info ei[] = {
741
16
        { &ei_classicstun_att_padding,
742
16
            { "classicstun.att.padding.assumed", PI_ASSUMPTION, PI_NOTE, "RFC 3489 STUN MUST align attribute lengths to a multiple of 4; assuming RFC 5389 style padding", EXPFILL}
743
16
        }
744
16
    };
745
746
/* Register the protocol name and description */
747
16
    proto_classicstun = proto_register_protocol("Simple Traversal of UDP Through NAT",
748
16
                            "CLASSICSTUN", "classicstun");
749
750
/* Required function calls to register the header fields and subtrees used */
751
16
    proto_register_field_array(proto_classicstun, hf, array_length(hf));
752
16
    proto_register_subtree_array(ett, array_length(ett));
753
16
    expert_classicstun = expert_register_protocol(proto_classicstun);
754
16
    expert_register_field_array(expert_classicstun, ei, array_length(ei));
755
756
    /* heuristic subdissectors (used for the DATA field) */
757
16
    heur_subdissector_list = register_heur_dissector_list_with_description("classicstun", "CLASSICSTUN DATA payload", proto_classicstun);
758
759
16
    register_dissector("classicstun", dissect_classicstun, proto_classicstun);
760
16
}
761
762
763
void
764
proto_reg_handoff_classicstun(void)
765
16
{
766
#if 0 /* The stun dissector registers on these ports */
767
    dissector_handle_t classicstun_handle;
768
769
    classicstun_handle = find_dissector("classicstun");
770
771
    dissector_add_uint_with_preference("tcp.port", TCP_PORT_STUN, classicstun_handle);
772
    dissector_add_uint_with_preference("udp.port", UDP_PORT_STUN, classicstun_handle);
773
#endif
774
16
    heur_dissector_add("udp", dissect_classicstun_heur, "Classic STUN over UDP", "classicstun_udp", proto_classicstun, HEURISTIC_ENABLE);
775
16
    heur_dissector_add("tcp", dissect_classicstun_heur, "Classic STUN over TCP", "classicstun_tcp", proto_classicstun, HEURISTIC_ENABLE);
776
777
16
    data_handle = find_dissector("data");
778
16
}
779
780
/*
781
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
782
 *
783
 * Local variables:
784
 * c-basic-offset: 4
785
 * tab-width: 8
786
 * indent-tabs-mode: nil
787
 * End:
788
 *
789
 * vi: set shiftwidth=4 tabstop=8 expandtab:
790
 * :indentSize=4:tabSize=8:noTabs=true:
791
 */