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-udp.c
Line
Count
Source
1
/* packet-udp.c
2
 * Routines for UDP/UDP-Lite packet disassembly
3
 *
4
 * Wireshark - Network traffic analyzer
5
 * By Gerald Combs <gerald@wireshark.org>
6
 * Copyright 1998 Gerald Combs
7
 *
8
 * Richard Sharpe, 13-Feb-1999, added dispatch table support and
9
 *                              support for tftp.
10
 *
11
 * SPDX-License-Identifier: GPL-2.0-or-later
12
 */
13
#include "config.h"
14
#include "packet-udp.h"
15
16
#include <epan/packet.h>
17
#include <epan/capture_dissectors.h>
18
#include <epan/addr_resolv.h>
19
#include <epan/in_cksum.h>
20
#include <epan/prefs.h>
21
#include <epan/follow.h>
22
#include <epan/expert.h>
23
#include <epan/exceptions.h>
24
#include <epan/show_exception.h>
25
#include <epan/proto_data.h>
26
27
#include <wsutil/utf8_entities.h>
28
#include <wsutil/pint.h>
29
#include <wsutil/str_util.h>
30
31
#include <epan/conversation.h>
32
#include <epan/conversation_table.h>
33
#include <epan/conversation_filter.h>
34
#include <epan/exported_pdu.h>
35
#include <epan/decode_as.h>
36
#include <epan/iana-info.h>
37
38
void proto_register_udp(void);
39
void proto_reg_handoff_udp(void);
40
41
static dissector_handle_t udp_handle;
42
static dissector_handle_t udplite_handle;
43
static capture_dissector_handle_t udp_cap_handle;
44
static capture_dissector_handle_t udplite_cap_handle;
45
46
static int udp_tap;
47
static int udp_follow_tap;
48
static int exported_pdu_tap;
49
50
static int proto_udp;
51
static int proto_udplite;
52
53
static int hf_udp_checksum;
54
static int hf_udp_checksum_calculated;
55
static int hf_udp_checksum_status;
56
static int hf_udp_dstport;
57
static int hf_udp_length;
58
static int hf_udp_payload;
59
static int hf_udp_pdu_size;
60
static int hf_udp_port;
61
static int hf_udp_proc_dst_cmd;
62
static int hf_udp_proc_dst_pid;
63
static int hf_udp_proc_dst_uid;
64
static int hf_udp_proc_dst_uname;
65
static int hf_udp_proc_src_cmd;
66
static int hf_udp_proc_src_pid;
67
static int hf_udp_proc_src_uid;
68
static int hf_udp_proc_src_uname;
69
static int hf_udp_srcport;
70
static int hf_udp_stream;
71
static int hf_udp_stream_pnum;
72
static int hf_udp_ts_delta;
73
static int hf_udp_ts_relative;
74
static int hf_udplite_checksum_coverage;
75
76
static int ett_udp;
77
static int ett_udp_checksum;
78
static int ett_udp_process_info;
79
static int ett_udp_timestamps;
80
81
static expert_field ei_udp_possible_traceroute;
82
static expert_field ei_udp_length_bad;
83
static expert_field ei_udplite_checksum_coverage_bad;
84
static expert_field ei_udp_checksum_zero;
85
static expert_field ei_udp_checksum_partial;
86
static expert_field ei_udp_checksum_bad;
87
static expert_field ei_udp_length_bad_zero;
88
89
/* Preferences */
90
91
/* Place UDP summary in proto tree */
92
static bool udp_summary_in_tree = true;
93
94
/* Check UDP checksums */
95
static bool udp_check_checksum;
96
97
/* Ignore zero-value UDP checksums over IPv6 */
98
static bool udp_ignore_ipv6_zero_checksum;
99
100
/* Collect IPFIX process flow information */
101
static bool udp_process_info;
102
103
/* Ignore an invalid checksum coverage field for UDP-Lite */
104
static bool udplite_ignore_checksum_coverage = true;
105
106
/* Check UDP-Lite checksums */
107
static bool udplite_check_checksum;
108
109
static dissector_table_t udp_dissector_table;
110
static heur_dissector_list_t heur_subdissector_list;
111
static uint32_t udp_stream_count;
112
113
/* Determine if there is a sub-dissector and call it.  This has been */
114
/* separated into a stand alone routine so other protocol dissectors */
115
/* can call to it, ie. socks */
116
117
static bool try_heuristic_first;
118
119
static bool udp_calculate_ts = true;
120
static bool udplite_calculate_ts = true;
121
122
/* Per-packet-info for UDP */
123
typedef struct {
124
    heur_dtbl_entry_t *heur_dtbl_entry;
125
    nstime_t ts_delta;
126
    bool ts_delta_valid;
127
    uint32_t pnum;
128
} udp_p_info_t;
129
130
static void
131
udp_src_prompt(packet_info *pinfo, char *result)
132
0
{
133
0
    uint32_t port = GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo,
134
0
                                        hf_udp_srcport, pinfo->curr_layer_num));
135
136
0
    snprintf(result, MAX_DECODE_AS_PROMPT_LEN, "source (%u%s)", port, UTF8_RIGHTWARDS_ARROW);
137
0
}
138
139
static void *
140
udp_src_value(packet_info *pinfo)
141
0
{
142
0
    return p_get_proto_data(pinfo->pool, pinfo, hf_udp_srcport, pinfo->curr_layer_num);
143
0
}
144
145
static void
146
udp_dst_prompt(packet_info *pinfo, char *result)
147
0
{
148
0
    uint32_t port = GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo,
149
0
                                        hf_udp_dstport, pinfo->curr_layer_num));
150
151
0
    snprintf(result, MAX_DECODE_AS_PROMPT_LEN, "destination (%s%u)", UTF8_RIGHTWARDS_ARROW, port);
152
0
}
153
154
static void *
155
udp_dst_value(packet_info *pinfo)
156
0
{
157
0
    return p_get_proto_data(pinfo->pool, pinfo, hf_udp_dstport, pinfo->curr_layer_num);
158
0
}
159
160
static void
161
udp_both_prompt(packet_info *pinfo, char *result)
162
0
{
163
0
    uint32_t srcport = GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo,
164
0
                                        hf_udp_srcport, pinfo->curr_layer_num));
165
0
    uint32_t dstport = GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo,
166
0
                                        hf_udp_dstport, pinfo->curr_layer_num));
167
0
    snprintf(result, MAX_DECODE_AS_PROMPT_LEN, "Both (%u%s%u)", srcport, UTF8_LEFT_RIGHT_ARROW, dstport);
168
0
}
169
170
/* Conversation and process code originally copied from packet-tcp.c */
171
static struct udp_analysis *
172
init_udp_conversation_data(packet_info *pinfo)
173
1.04k
{
174
1.04k
    struct udp_analysis *udpd;
175
176
    /* Initialize the udp protocol data structure to add to the udp conversation */
177
1.04k
    udpd = wmem_new0(wmem_file_scope(), struct udp_analysis);
178
    /*
179
    udpd->flow1.username = NULL;
180
    udpd->flow1.command = NULL;
181
    udpd->flow2.username = NULL;
182
    udpd->flow2.command = NULL;
183
    */
184
185
1.04k
    udpd->stream = udp_stream_count++;
186
1.04k
    udpd->ts_first = pinfo->abs_ts;
187
1.04k
    udpd->ts_prev = pinfo->abs_ts;
188
189
1.04k
    return udpd;
190
1.04k
}
191
192
struct udp_analysis *
193
get_udp_conversation_data(conversation_t *conv, packet_info *pinfo)
194
4.90k
{
195
4.90k
    int direction;
196
4.90k
    struct udp_analysis *udpd = NULL;
197
198
    /* Did the caller supply the conversation pointer? */
199
4.90k
    if (conv == NULL)
200
0
        conv = find_or_create_conversation_strat(pinfo);
201
202
    /* Get the data for this conversation */
203
4.90k
    udpd = conversation_get_proto_data(conv, proto_udp);
204
205
    /* If the conversation was just created or it matched a
206
     * conversation with template options, udpd will not
207
     * have been initialized. So, initialize
208
     * a new udpd structure for the conversation.
209
     */
210
4.90k
    if (!udpd) {
211
1.04k
        udpd = init_udp_conversation_data(pinfo);
212
1.04k
        conversation_add_proto_data(conv, proto_udp, udpd);
213
1.04k
    }
214
215
4.90k
    if (!udpd) {
216
0
        return NULL;
217
0
    }
218
219
    /* check direction and get ua lists */
220
4.90k
    direction = cmp_address(&pinfo->src, &pinfo->dst);
221
    /* if the addresses are equal, match the ports instead */
222
4.90k
    if (direction == 0) {
223
4.76k
        direction = (pinfo->srcport > pinfo->destport) ? 1 : -1;
224
4.76k
    }
225
4.90k
    if (direction >= 0) {
226
1.28k
        udpd->fwd = &(udpd->flow1);
227
1.28k
        udpd->rev = &(udpd->flow2);
228
1.28k
    }
229
3.62k
    else {
230
3.62k
        udpd->fwd = &(udpd->flow2);
231
3.62k
        udpd->rev = &(udpd->flow1);
232
3.62k
    }
233
234
4.90k
    return udpd;
235
4.90k
}
236
237
static const char* udp_conv_get_filter_type(conv_item_t* conv, conv_filter_type_e filter)
238
0
{
239
0
    if (filter == CONV_FT_SRC_PORT)
240
0
        return "udp.srcport";
241
242
0
    if (filter == CONV_FT_DST_PORT)
243
0
        return "udp.dstport";
244
245
0
    if (filter == CONV_FT_ANY_PORT)
246
0
        return "udp.port";
247
248
0
    if(!conv) {
249
0
        return CONV_FILTER_INVALID;
250
0
    }
251
252
0
    if (filter == CONV_FT_SRC_ADDRESS) {
253
0
        if (conv->src_address.type == AT_IPv4)
254
0
            return "ip.src";
255
0
        if (conv->src_address.type == AT_IPv6)
256
0
            return "ipv6.src";
257
0
    }
258
259
0
    if (filter == CONV_FT_DST_ADDRESS) {
260
0
        if (conv->dst_address.type == AT_IPv4)
261
0
            return "ip.dst";
262
0
        if (conv->dst_address.type == AT_IPv6)
263
0
            return "ipv6.dst";
264
0
    }
265
266
0
    if (filter == CONV_FT_ANY_ADDRESS) {
267
0
        if (conv->src_address.type == AT_IPv4)
268
0
            return "ip.addr";
269
0
        if (conv->src_address.type == AT_IPv6)
270
0
            return "ipv6.addr";
271
0
    }
272
273
0
    return CONV_FILTER_INVALID;
274
0
}
275
276
static ct_dissector_info_t udp_ct_dissector_info = {&udp_conv_get_filter_type};
277
278
static tap_packet_status
279
udpip_conversation_packet(void *pct, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip, tap_flags_t flags)
280
0
{
281
0
    conv_hash_t *hash = (conv_hash_t*) pct;
282
0
    hash->flags = flags;
283
284
0
    const e_udphdr *udphdr=(const e_udphdr *)vip;
285
286
0
    add_conversation_table_data_with_conv_id(hash,
287
0
                &udphdr->ip_src, &udphdr->ip_dst, udphdr->uh_sport, udphdr->uh_dport,
288
0
                (conv_id_t) udphdr->uh_stream, 1, pinfo->fd->pkt_len, &pinfo->rel_ts, &pinfo->abs_ts,
289
0
                &udp_ct_dissector_info, CONVERSATION_UDP);
290
291
0
    return TAP_PACKET_REDRAW;
292
0
}
293
294
static const char* udp_endpoint_get_filter_type(endpoint_item_t* endpoint, conv_filter_type_e filter)
295
0
{
296
297
0
    if (filter == CONV_FT_SRC_PORT)
298
0
        return "udp.srcport";
299
300
0
    if (filter == CONV_FT_DST_PORT)
301
0
        return "udp.dstport";
302
303
0
    if (filter == CONV_FT_ANY_PORT)
304
0
        return "udp.port";
305
306
0
    if(!endpoint) {
307
0
        return CONV_FILTER_INVALID;
308
0
    }
309
310
311
0
    if (filter == CONV_FT_SRC_ADDRESS) {
312
0
        if (endpoint->myaddress.type == AT_IPv4)
313
0
            return "ip.src";
314
0
        if (endpoint->myaddress.type == AT_IPv6)
315
0
            return "ipv6.src";
316
0
    }
317
318
0
    if (filter == CONV_FT_DST_ADDRESS) {
319
0
        if (endpoint->myaddress.type == AT_IPv4)
320
0
            return "ip.dst";
321
0
        if (endpoint->myaddress.type == AT_IPv6)
322
0
            return "ipv6.dst";
323
0
    }
324
325
0
    if (filter == CONV_FT_ANY_ADDRESS) {
326
0
        if (endpoint->myaddress.type == AT_IPv4)
327
0
            return "ip.addr";
328
0
        if (endpoint->myaddress.type == AT_IPv6)
329
0
            return "ipv6.addr";
330
0
    }
331
332
0
    return CONV_FILTER_INVALID;
333
0
}
334
335
static et_dissector_info_t udp_endpoint_dissector_info = {&udp_endpoint_get_filter_type};
336
337
static tap_packet_status
338
udpip_endpoint_packet(void *pit, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip, tap_flags_t flags)
339
0
{
340
0
    conv_hash_t *hash = (conv_hash_t*) pit;
341
0
    hash->flags = flags;
342
343
0
    const e_udphdr *udphdr=(const e_udphdr *)vip;
344
345
    /* Take two "add" passes per packet, adding for each direction, ensures that all
346
    packets are counted properly (even if address is sending to itself)
347
    XXX - this could probably be done more efficiently inside endpoint_table */
348
0
    add_endpoint_table_data(hash, &udphdr->ip_src, udphdr->uh_sport, true, 1, pinfo->fd->pkt_len, &udp_endpoint_dissector_info, ENDPOINT_UDP);
349
0
    add_endpoint_table_data(hash, &udphdr->ip_dst, udphdr->uh_dport, false, 1, pinfo->fd->pkt_len, &udp_endpoint_dissector_info, ENDPOINT_UDP);
350
351
0
    return TAP_PACKET_REDRAW;
352
0
}
353
354
static bool
355
udp_filter_valid(packet_info *pinfo, void *user_data _U_)
356
0
{
357
0
    return proto_is_frame_protocol(pinfo->layers, "udp");
358
0
}
359
360
static char*
361
udp_build_filter_by_id(packet_info *pinfo, void *user_data _U_)
362
0
{
363
0
        return ws_strdup_printf("udp.stream eq %d", pinfo->stream_id);
364
0
}
365
366
367
static char *udp_follow_conv_filter(epan_dissect_t *edt _U_, packet_info *pinfo, unsigned *stream, unsigned *sub_stream _U_)
368
0
{
369
0
    uint8_t max_layer_num = proto_get_layer_num(pinfo, proto_udp);
370
371
0
    for (uint8_t curr_layer_num = max_layer_num; curr_layer_num; --curr_layer_num) {
372
0
        *stream = GPOINTER_TO_UINT(p_get_proto_data(pinfo->pool, pinfo, hf_udp_stream, curr_layer_num));
373
0
        if (*stream) {
374
0
            return ws_strdup_printf("udp.stream eq %u", --*stream);
375
0
        }
376
0
    }
377
378
0
    return NULL;
379
0
}
380
381
static char *udp_follow_index_filter(unsigned stream, unsigned sub_stream _U_)
382
0
{
383
0
    return ws_strdup_printf("udp.stream eq %u", stream);
384
0
}
385
386
char *udp_follow_address_filter(address *src_addr, address *dst_addr, int src_port, int dst_port)
387
0
{
388
0
    const char   *ip_version = src_addr->type == AT_IPv6 ? "v6" : "";
389
0
    char          src_addr_str[WS_INET6_ADDRSTRLEN];
390
0
    char          dst_addr_str[WS_INET6_ADDRSTRLEN];
391
392
0
    address_to_str_buf(src_addr, src_addr_str, sizeof(src_addr_str));
393
0
    address_to_str_buf(dst_addr, dst_addr_str, sizeof(dst_addr_str));
394
395
0
    return ws_strdup_printf("((ip%s.src eq %s and udp.srcport eq %d) and "
396
0
                     "(ip%s.dst eq %s and udp.dstport eq %d))"
397
0
                     " or "
398
0
                     "((ip%s.src eq %s and udp.srcport eq %d) and "
399
0
                     "(ip%s.dst eq %s and udp.dstport eq %d))",
400
0
                     ip_version, src_addr_str, src_port,
401
0
                     ip_version, dst_addr_str, dst_port,
402
0
                     ip_version, dst_addr_str, dst_port,
403
0
                     ip_version, src_addr_str, src_port);
404
0
}
405
406
407
/* Attach process info to a flow */
408
/* XXX - We depend on the UDP dissector finding the conversation first */
409
void
410
add_udp_process_info(uint32_t frame_num, address *local_addr, address *remote_addr,
411
                        uint16_t local_port, uint16_t remote_port, uint32_t uid, uint32_t pid,
412
                        char *username, char *command)
413
0
{
414
0
    conversation_t *conv;
415
0
    struct udp_analysis *udpd;
416
0
    udp_flow_t *flow = NULL;
417
418
0
    if (!udp_process_info) {
419
0
        return;
420
0
    }
421
422
0
    conv = find_conversation(frame_num, local_addr, remote_addr, CONVERSATION_UDP, local_port, remote_port, 0);
423
0
    if (!conv) {
424
0
        return;
425
0
    }
426
427
0
    udpd = (struct udp_analysis *)conversation_get_proto_data(conv, proto_udp);
428
0
    if (!udpd) {
429
0
        return;
430
0
    }
431
432
0
    if ((cmp_address(local_addr, conversation_key_addr1(conv->key_ptr)) == 0) && (local_port == conversation_key_port1(conv->key_ptr))) {
433
0
        flow = &udpd->flow1;
434
0
    }
435
0
    else if ((cmp_address(remote_addr, conversation_key_addr1(conv->key_ptr)) == 0) && (remote_port == conversation_key_port1(conv->key_ptr))) {
436
0
        flow = &udpd->flow2;
437
0
    }
438
0
    if (!flow || flow->command) {
439
0
        return;
440
0
    }
441
442
0
    flow->process_uid = uid;
443
0
    flow->process_pid = pid;
444
0
    flow->username = wmem_strdup(wmem_file_scope(), username);
445
0
    flow->command = wmem_strdup(wmem_file_scope(), command);
446
0
}
447
448
449
/* Return the current stream count */
450
uint32_t get_udp_stream_count(void)
451
0
{
452
0
    return udp_stream_count;
453
0
}
454
455
static void
456
handle_export_pdu_dissection_table(packet_info *pinfo, tvbuff_t *tvb, uint32_t port)
457
1.74k
{
458
1.74k
    if (have_tap_listener(exported_pdu_tap)) {
459
0
        exp_pdu_data_item_t exp_pdu_data_table_value = {exp_pdu_data_dissector_table_num_value_size, exp_pdu_data_dissector_table_num_value_populate_data, NULL};
460
461
0
        const exp_pdu_data_item_t *udp_exp_pdu_items[] = {
462
0
            &exp_pdu_data_src_ip,
463
0
            &exp_pdu_data_dst_ip,
464
0
            &exp_pdu_data_port_type,
465
0
            &exp_pdu_data_src_port,
466
0
            &exp_pdu_data_dst_port,
467
0
            &exp_pdu_data_orig_frame_num,
468
0
            &exp_pdu_data_table_value,
469
0
            NULL
470
0
        };
471
472
0
        exp_pdu_data_t *exp_pdu_data;
473
474
0
        exp_pdu_data_table_value.data = GUINT_TO_POINTER(port);
475
476
0
        exp_pdu_data = export_pdu_create_tags(pinfo, "udp.port", EXP_PDU_TAG_DISSECTOR_TABLE_NAME, udp_exp_pdu_items);
477
0
        exp_pdu_data->tvb_captured_length = tvb_captured_length(tvb);
478
0
        exp_pdu_data->tvb_reported_length = tvb_reported_length(tvb);
479
0
        exp_pdu_data->pdu_tvb = tvb;
480
481
0
        tap_queue_packet(exported_pdu_tap, pinfo, exp_pdu_data);
482
0
    }
483
1.74k
}
484
485
static void
486
handle_export_pdu_heuristic(packet_info *pinfo, tvbuff_t *tvb, heur_dtbl_entry_t *hdtbl_entry)
487
128
{
488
128
    exp_pdu_data_t *exp_pdu_data = NULL;
489
490
128
    if (have_tap_listener(exported_pdu_tap)) {
491
0
        if ((!hdtbl_entry->enabled) ||
492
0
                (hdtbl_entry->protocol != NULL && !proto_is_protocol_enabled(hdtbl_entry->protocol))) {
493
0
            exp_pdu_data = export_pdu_create_common_tags(pinfo, "data", EXP_PDU_TAG_DISSECTOR_NAME);
494
0
        }
495
0
        else if (hdtbl_entry->protocol != NULL) {
496
0
            exp_pdu_data = export_pdu_create_common_tags(pinfo, hdtbl_entry->short_name, EXP_PDU_TAG_HEUR_DISSECTOR_NAME);
497
0
        }
498
499
0
        if (exp_pdu_data != NULL) {
500
0
            exp_pdu_data->tvb_captured_length = tvb_captured_length(tvb);
501
0
            exp_pdu_data->tvb_reported_length = tvb_reported_length(tvb);
502
0
            exp_pdu_data->pdu_tvb = tvb;
503
504
0
            tap_queue_packet(exported_pdu_tap, pinfo, exp_pdu_data);
505
0
        }
506
0
    }
507
128
}
508
509
static void
510
handle_export_pdu_conversation(packet_info *pinfo, tvbuff_t *tvb, int uh_dport, int uh_sport)
511
11
{
512
11
    if (have_tap_listener(exported_pdu_tap)) {
513
0
        conversation_t *conversation = find_conversation(pinfo->num, &pinfo->dst, &pinfo->src, CONVERSATION_UDP, uh_dport, uh_sport, 0);
514
0
        if (conversation != NULL) {
515
0
            dissector_handle_t handle = (dissector_handle_t)wmem_tree_lookup32_le(conversation->dissector_tree, pinfo->num);
516
0
            if (handle != NULL) {
517
0
                exp_pdu_data_t *exp_pdu_data = export_pdu_create_common_tags(pinfo, dissector_handle_get_dissector_name(handle), EXP_PDU_TAG_DISSECTOR_NAME);
518
0
                exp_pdu_data->tvb_captured_length = tvb_captured_length(tvb);
519
0
                exp_pdu_data->tvb_reported_length = tvb_reported_length(tvb);
520
0
                exp_pdu_data->pdu_tvb = tvb;
521
522
0
                tap_queue_packet(exported_pdu_tap, pinfo, exp_pdu_data);
523
0
            }
524
0
        }
525
0
    }
526
11
}
527
528
void
529
decode_udp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
530
             proto_tree *udp_tree, int uh_sport, int uh_dport, int uh_ulen)
531
4.90k
{
532
4.90k
    tvbuff_t *next_tvb;
533
4.90k
    int low_port, high_port;
534
4.90k
    bool try_low_port, try_high_port;
535
4.90k
    int len, reported_len;
536
4.90k
    udp_p_info_t *udp_p_info;
537
    /* Save curr_layer_num as it might be changed by subdissector */
538
4.90k
    uint8_t curr_layer_num = pinfo->curr_layer_num;
539
4.90k
    heur_dtbl_entry_t *hdtbl_entry;
540
4.90k
    exp_pdu_data_t *exp_pdu_data;
541
4.90k
    proto_tree* tree = proto_tree_get_parent_tree(udp_tree);
542
543
    /* populate per packet data variable */
544
4.90k
    udp_p_info = (udp_p_info_t*)p_get_proto_data(wmem_file_scope(), pinfo, proto_udp, pinfo->curr_layer_num);
545
546
4.90k
    len = tvb_captured_length_remaining(tvb, offset);
547
4.90k
    reported_len = tvb_reported_length_remaining(tvb, offset);
548
4.90k
    if (uh_ulen != -1) {
549
        /* This is the length from the UDP header; the payload should be cut
550
             off at that length.  (If our caller passed a value here, they
551
             are assumed to have checked that it's >= 8, and hence >= offset.)
552
553
             XXX - what if it's *greater* than the reported length? */
554
4.90k
        if ((uh_ulen - offset) < reported_len)
555
57
            reported_len = uh_ulen - offset;
556
4.90k
        if (len > reported_len)
557
57
            len = reported_len;
558
4.90k
    }
559
560
4.90k
    proto_tree_add_bytes_format(udp_tree, hf_udp_payload, tvb, offset,
561
4.90k
            -1, NULL, "UDP payload (%u byte%s)", len,
562
4.90k
            plurality(len, "", "s"));
563
564
4.90k
    next_tvb = tvb_new_subset_length_caplen(tvb, offset, len, reported_len);
565
566
4.90k
    if (PINFO_FD_VISITED(pinfo)) {
567
0
        if (udp_p_info && udp_p_info->heur_dtbl_entry != NULL) {
568
0
            call_heur_dissector_direct(udp_p_info->heur_dtbl_entry, next_tvb, pinfo, tree, NULL);
569
0
            handle_export_pdu_heuristic(pinfo, next_tvb, udp_p_info->heur_dtbl_entry);
570
0
            return;
571
0
        }
572
0
    }
573
574
    /* Determine if this packet is part of a conversation and call dissector
575
     * for the conversation if available.
576
     * This search is done on the 'other' direction */
577
4.90k
    if (try_conversation_dissector_strat(pinfo, CONVERSATION_UDP,
578
4.90k
             next_tvb, tree, NULL, NO_ADDR_B|NO_PORT_B, true)) {
579
11
        handle_export_pdu_conversation(pinfo, next_tvb, uh_dport, uh_sport);
580
11
        return;
581
11
    }
582
583
    /* XXX - we ignore port numbers of 0, as some dissectors use a port
584
         number of 0 to disable the port, and as RFC 768 says that the source
585
         port in UDP datagrams is optional and is 0 if not used. */
586
587
4.89k
    if (uh_sport > uh_dport) {
588
1.26k
        low_port  = uh_dport;
589
1.26k
        high_port = uh_sport;
590
1.26k
    }
591
3.63k
    else {
592
3.63k
        low_port  = uh_sport;
593
3.63k
        high_port = uh_dport;
594
3.63k
    }
595
596
4.89k
    try_low_port = false;
597
4.89k
    if (low_port != 0) {
598
4.83k
        if (dissector_is_uint_changed(udp_dissector_table, low_port)) {
599
0
            if (dissector_try_uint(udp_dissector_table, low_port, next_tvb, pinfo, tree)) {
600
0
                handle_export_pdu_dissection_table(pinfo, next_tvb, low_port);
601
0
                return;
602
0
            }
603
0
        }
604
4.83k
        else {
605
            /* The default; try it later */
606
4.83k
            try_low_port = true;
607
4.83k
        }
608
4.83k
    }
609
610
4.89k
    try_high_port = false;
611
4.89k
    if (high_port != 0) {
612
4.85k
        if (dissector_is_uint_changed(udp_dissector_table, high_port)) {
613
0
            if (dissector_try_uint(udp_dissector_table, high_port, next_tvb, pinfo, tree)) {
614
0
                handle_export_pdu_dissection_table(pinfo, next_tvb, high_port);
615
0
                return;
616
0
            }
617
0
        }
618
4.85k
        else {
619
            /* The default; try it later */
620
4.85k
            try_high_port = true;
621
4.85k
        }
622
4.85k
    }
623
624
4.89k
    if (try_heuristic_first) {
625
        /* Do lookup with the heuristic subdissector table */
626
0
        if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree, &hdtbl_entry, NULL)) {
627
0
            if (!udp_p_info) {
628
0
                udp_p_info = wmem_new0(wmem_file_scope(), udp_p_info_t);
629
0
                p_add_proto_data(wmem_file_scope(), pinfo, proto_udp, curr_layer_num, udp_p_info);
630
0
            }
631
632
0
            udp_p_info->heur_dtbl_entry = hdtbl_entry;
633
634
0
            handle_export_pdu_heuristic(pinfo, next_tvb, udp_p_info->heur_dtbl_entry);
635
0
            return;
636
0
        }
637
0
    }
638
639
    /* Do lookups with the subdissector table.
640
         We try the port number with the lower value first, followed by the
641
         port number with the higher value.  This means that, for packets
642
         where a dissector is registered for *both* port numbers:
643
644
                1) we pick the same dissector for traffic going in both directions;
645
646
                2) we prefer the port number that's more likely to be the right
647
                     one (as that prefers well-known ports to reserved ports);
648
649
         although there is, of course, no guarantee that any such strategy
650
         will always pick the right port number.
651
     */
652
653
4.89k
    if ((try_low_port) && dissector_try_uint(udp_dissector_table, low_port, next_tvb, pinfo, tree)) {
654
1.43k
        handle_export_pdu_dissection_table(pinfo, next_tvb, low_port);
655
1.43k
        return;
656
1.43k
    }
657
3.46k
    if ((try_high_port) && dissector_try_uint(udp_dissector_table, high_port, next_tvb, pinfo, tree)) {
658
316
        handle_export_pdu_dissection_table(pinfo, next_tvb, high_port);
659
316
        return;
660
316
    }
661
662
3.14k
    if (!try_heuristic_first) {
663
        /* Do lookup with the heuristic subdissector table */
664
559
        if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree, &hdtbl_entry, NULL)) {
665
128
            if (!udp_p_info) {
666
1
                udp_p_info = wmem_new0(wmem_file_scope(), udp_p_info_t);
667
1
                p_add_proto_data(wmem_file_scope(), pinfo, proto_udp, curr_layer_num, udp_p_info);
668
1
            }
669
670
128
            udp_p_info->heur_dtbl_entry = hdtbl_entry;
671
672
128
            handle_export_pdu_heuristic(pinfo, next_tvb, udp_p_info->heur_dtbl_entry);
673
128
            return;
674
128
        }
675
559
    }
676
677
3.01k
    call_data_dissector(next_tvb, pinfo, tree);
678
679
3.01k
    if (have_tap_listener(exported_pdu_tap)) {
680
0
        exp_pdu_data = export_pdu_create_common_tags(pinfo, "data", EXP_PDU_TAG_DISSECTOR_NAME);
681
0
        exp_pdu_data->tvb_captured_length = tvb_captured_length(next_tvb);
682
0
        exp_pdu_data->tvb_reported_length = tvb_reported_length(next_tvb);
683
0
        exp_pdu_data->pdu_tvb = next_tvb;
684
685
0
        tap_queue_packet(exported_pdu_tap, pinfo, exp_pdu_data);
686
0
    }
687
3.01k
}
688
689
int
690
udp_dissect_pdus(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
691
         unsigned fixed_len,  bool (*heuristic_check)(packet_info *, tvbuff_t *, int, void*),
692
         unsigned (*get_pdu_len)(packet_info *, tvbuff_t *, int, void*),
693
         dissector_t dissect_pdu, void* dissector_data)
694
370
{
695
370
    volatile int offset = 0;
696
370
    int offset_before;
697
370
    unsigned captured_length_remaining;
698
370
    volatile unsigned plen;
699
370
    unsigned length;
700
370
    tvbuff_t *next_tvb;
701
370
    proto_item *item=NULL;
702
370
    const char *saved_proto;
703
370
    uint8_t curr_layer_num;
704
370
    wmem_list_frame_t *frame;
705
706
389
    while (tvb_reported_length_remaining(tvb, offset) > 0) {
707
        /*
708
        * We use "tvb_ensure_captured_length_remaining()" to make
709
        * sure there actually *is* data remaining.  The protocol
710
        * we're handling could conceivably consists of a sequence of
711
        * fixed-length PDUs, and therefore the "get_pdu_len" routine
712
        * might not actually fetch anything from the tvbuff, and thus
713
        * might not cause an exception to be thrown if we've run past
714
        * the end of the tvbuff.
715
        *
716
        * This means we're guaranteed that "captured_length_remaining" is positive.
717
        */
718
372
        captured_length_remaining = tvb_ensure_captured_length_remaining(tvb, offset);
719
720
        /*
721
        * If there is a heuristic function, check it
722
        */
723
372
        if ((heuristic_check != NULL) &&
724
353
                ((*heuristic_check)(pinfo, tvb, offset, dissector_data) == false)) {
725
353
            return offset;
726
353
         }
727
728
        /*
729
        * Get the length of the PDU.
730
        */
731
19
        plen = (*get_pdu_len)(pinfo, tvb, offset, dissector_data);
732
19
        if (plen == 0) {
733
            /*
734
             * Either protocol has variable length (which isn't supposed by UDP)
735
             * or packet doesn't belong to protocol
736
             */
737
0
            return offset;
738
0
         }
739
740
19
        if (plen < fixed_len) {
741
            /*
742
            * Either:
743
            *
744
            *  1) the length value extracted from the fixed-length portion
745
            *     doesn't include the fixed-length portion's length, and
746
            *     was so large that, when the fixed-length portion's
747
            *     length was added to it, the total length overflowed;
748
            *
749
            *  2) the length value extracted from the fixed-length portion
750
            *     includes the fixed-length portion's length, and the value
751
            *     was less than the fixed-length portion's length, i.e. it
752
            *     was bogus.
753
            *
754
            * Report this as a bounds error.
755
            */
756
0
            show_reported_bounds_error(tvb, pinfo, tree);
757
0
            return offset;
758
0
        }
759
760
19
        curr_layer_num = pinfo->curr_layer_num-1;
761
19
        frame = wmem_list_frame_prev(wmem_list_tail(pinfo->layers));
762
19
        while (frame && (proto_udp != (int) GPOINTER_TO_UINT(wmem_list_frame_data(frame)))) {
763
0
            frame = wmem_list_frame_prev(frame);
764
0
            curr_layer_num--;
765
0
        }
766
767
         /*
768
            * Display the PDU length as a field
769
            */
770
19
        item = proto_tree_add_uint((proto_tree *)p_get_proto_data(pinfo->pool, pinfo, proto_udp, curr_layer_num),
771
19
                                    hf_udp_pdu_size, tvb, offset, plen, plen);
772
19
        proto_item_set_generated(item);
773
774
        /*
775
        * Construct a tvbuff containing the amount of the payload we have
776
        * available.  Make its reported length the amount of data in the PDU.
777
        */
778
19
        length = captured_length_remaining;
779
19
        if (length > plen)
780
2
            length = plen;
781
19
        next_tvb = tvb_new_subset_length_caplen(tvb, offset, length, plen);
782
783
        /*
784
        * Dissect the PDU.
785
        *
786
        * If it gets an error that means there's no point in
787
        * dissecting any more PDUs, rethrow the exception in
788
        * question.
789
        *
790
        * If it gets any other error, report it and continue, as that
791
        * means that PDU got an error, but that doesn't mean we should
792
        * stop dissecting PDUs within this frame or chunk of reassembled
793
        * data.
794
        */
795
19
        saved_proto = pinfo->current_proto;
796
19
        TRY {
797
19
            (*dissect_pdu)(next_tvb, pinfo, tree, dissector_data);
798
19
        }
799
19
        CATCH_NONFATAL_ERRORS {
800
            /*  Restore the private_data structure in case one of the
801
             *  called dissectors modified it (and, due to the exception,
802
             *  was unable to restore it).
803
             */
804
0
            show_exception(tvb, pinfo, tree, EXCEPT_CODE, GET_MESSAGE);
805
806
            /*
807
             * Restore the saved protocol as well; we do this after
808
             * show_exception(), so that the "Malformed packet" indication
809
             * shows the protocol for which dissection failed.
810
             */
811
0
            pinfo->current_proto = saved_proto;
812
0
        }
813
19
        ENDTRY;
814
815
        /*
816
         * Step to the next PDU.
817
         * Make sure we don't overflow.
818
         */
819
19
        offset_before = offset;
820
19
        offset += plen;
821
19
        if (offset <= offset_before)
822
0
            break;
823
19
    }
824
825
17
    return offset;
826
370
}
827
828
static bool
829
capture_udp(const unsigned char *pd _U_, int offset _U_, int len _U_, capture_packet_info_t *cpinfo, const union wtap_pseudo_header *pseudo_header _U_)
830
0
{
831
0
    uint16_t src_port, dst_port, low_port, high_port;
832
833
0
    if (!BYTES_ARE_IN_FRAME(offset, len, 4))
834
0
        return false;
835
836
0
    capture_dissector_increment_count(cpinfo, proto_udp);
837
838
0
    src_port = pntohu16(&pd[offset]);
839
0
    dst_port = pntohu16(&pd[offset+2]);
840
841
0
    if (src_port > dst_port) {
842
0
        low_port = dst_port;
843
0
        high_port = src_port;
844
0
    }
845
0
    else {
846
0
        low_port = src_port;
847
0
        high_port = dst_port;
848
0
    }
849
850
0
    if (low_port != 0 && try_capture_dissector("udp.port", low_port, pd, offset+20, len, cpinfo, pseudo_header))
851
0
        return true;
852
853
0
    if (high_port != 0 && try_capture_dissector("udp.port", high_port, pd, offset+20, len, cpinfo, pseudo_header))
854
0
        return true;
855
856
    /* We've at least identified one type of packet, so this shouldn't be "other" */
857
0
    return true;
858
0
}
859
860
/* Calculate the timestamps relative to this conversation */
861
static void
862
udp_compute_timestamps(packet_info *pinfo, struct udp_analysis *udp_data, int proto)
863
4.90k
{
864
4.90k
    if (!udp_data)
865
0
        return;
866
867
    /* get per packet date for UDP/UDP-Lite based on protocol id */
868
4.90k
    udp_p_info_t *udp_per_packet_data = (udp_p_info_t *)p_get_proto_data(wmem_file_scope(), pinfo, proto, pinfo->curr_layer_num);
869
870
4.90k
    if(!udp_per_packet_data) {
871
4.90k
        udp_per_packet_data = wmem_new0(wmem_file_scope(), udp_p_info_t);
872
4.90k
        p_add_proto_data(wmem_file_scope(), pinfo, proto, pinfo->curr_layer_num, udp_per_packet_data);
873
4.90k
    }
874
875
    /* pre-increment so packet numbers start at 1 */
876
4.90k
    udp_per_packet_data->pnum = ++udp_data->pnum;
877
878
4.90k
    nstime_delta(&udp_per_packet_data->ts_delta, &pinfo->abs_ts, &udp_data->ts_prev);
879
4.90k
    udp_per_packet_data->ts_delta_valid = true;
880
881
4.90k
    udp_data->ts_prev = pinfo->abs_ts;
882
4.90k
}
883
884
/* Add a subtree with the timestamps relative to this conversation */
885
static void
886
udp_print_timestamps(packet_info *pinfo, tvbuff_t *tvb, proto_tree *parent_tree, struct udp_analysis *udp_data, int proto)
887
4.90k
{
888
4.90k
    proto_item  *item;
889
4.90k
    proto_tree  *tree;
890
4.90k
    nstime_t    ts;
891
892
4.90k
    if (!udp_data)
893
0
        return;
894
895
    /* get per packet date for UDP/UDP-Lite based on protocol id */
896
4.90k
    udp_p_info_t *udp_per_packet_data = (udp_p_info_t *)p_get_proto_data(wmem_file_scope(), pinfo, proto, pinfo->curr_layer_num);
897
898
4.90k
    if (udp_per_packet_data) {
899
4.90k
        item = proto_tree_add_uint(parent_tree, hf_udp_stream_pnum, tvb, 0, 0,
900
4.90k
                    udp_per_packet_data->pnum);
901
4.90k
        proto_item_set_generated(item);
902
4.90k
    }
903
904
4.90k
    tree = proto_tree_add_subtree(parent_tree, tvb, 0, 0, ett_udp_timestamps, &item, "Timestamps");
905
4.90k
    proto_item_set_generated(item);
906
907
4.90k
    nstime_delta(&ts, &pinfo->abs_ts, &udp_data->ts_first);
908
4.90k
    item = proto_tree_add_time(tree, hf_udp_ts_relative, tvb, 0, 0, &ts);
909
4.90k
    proto_item_set_generated(item);
910
911
4.90k
    if (udp_per_packet_data && udp_per_packet_data->ts_delta_valid) {
912
4.90k
        item = proto_tree_add_time(tree, hf_udp_ts_delta, tvb, 0, 0,
913
4.90k
                    &udp_per_packet_data->ts_delta);
914
4.90k
        proto_item_set_generated(item);
915
4.90k
    }
916
4.90k
}
917
918
static void
919
udp_handle_timestamps(packet_info *pinfo, tvbuff_t *tvb, proto_tree *tree, struct udp_analysis *udp_data, uint32_t ip_proto)
920
4.90k
{
921
4.90k
    int proto_id = (ip_proto == IP_PROTO_UDP ? proto_udp : proto_udplite);
922
923
    /*
924
     * Calculate the timestamps relative to this conversation (but only on the
925
     * first run when frames are accessed sequentially)
926
     */
927
4.90k
    if (!PINFO_FD_VISITED(pinfo))
928
4.90k
        udp_compute_timestamps(pinfo, udp_data, proto_id);
929
930
    /* handle conversation timestamps */
931
4.90k
    udp_print_timestamps(pinfo, tvb, tree, udp_data, proto_id);
932
4.90k
}
933
934
static void
935
dissect(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, uint32_t ip_proto)
936
4.90k
{
937
4.90k
    proto_tree *udp_tree = NULL;
938
4.90k
    proto_item *ti, *item, *hidden_item, *calc_item;
939
4.90k
    proto_item *src_port_item, *dst_port_item, *len_cov_item;
940
4.90k
    unsigned    len;
941
4.90k
    unsigned    reported_len;
942
4.90k
    vec_t       cksum_vec[4];
943
4.90k
    uint32_t    phdr[2];
944
4.90k
    uint16_t    computed_cksum;
945
4.90k
    int         offset = 0;
946
4.90k
    e_udphdr   *udph;
947
4.90k
    proto_tree *checksum_tree;
948
4.90k
    conversation_t *conv = NULL;
949
4.90k
    struct udp_analysis *udpd = NULL;
950
4.90k
    proto_tree *process_tree;
951
4.90k
    bool        udp_jumbogram = false;
952
953
4.90k
    udph = wmem_new0(pinfo->pool, e_udphdr);
954
4.90k
    udph->uh_sport = tvb_get_ntohs(tvb, offset);
955
4.90k
    udph->uh_dport = tvb_get_ntohs(tvb, offset + 2);
956
4.90k
    copy_address_shallow(&udph->ip_src, &pinfo->src);
957
4.90k
    copy_address_shallow(&udph->ip_dst, &pinfo->dst);
958
959
4.90k
    col_set_str(pinfo->cinfo, COL_PROTOCOL, (ip_proto == IP_PROTO_UDP) ? "UDP" : "UDP-Lite");
960
4.90k
    col_clear(pinfo->cinfo, COL_INFO);
961
4.90k
    col_append_ports(pinfo->cinfo, COL_INFO, PT_UDP, udph->uh_sport, udph->uh_dport);
962
963
4.90k
    reported_len = tvb_reported_length(tvb);
964
4.90k
    len = tvb_captured_length(tvb);
965
966
4.90k
    ti = proto_tree_add_item(tree, (ip_proto == IP_PROTO_UDP) ? proto_udp : proto_udplite, tvb, offset, 8, ENC_NA);
967
4.90k
    if (udp_summary_in_tree && tree) {
968
4.90k
        proto_item_append_text(ti, ", Src Port: %s, Dst Port: %s",
969
4.90k
                     port_with_resolution_to_str(pinfo->pool, PT_UDP, udph->uh_sport),
970
4.90k
                     port_with_resolution_to_str(pinfo->pool, PT_UDP, udph->uh_dport));
971
4.90k
    }
972
4.90k
    udp_tree = proto_item_add_subtree(ti, ett_udp);
973
4.90k
    p_add_proto_data(pinfo->pool, pinfo, proto_udp, pinfo->curr_layer_num, udp_tree);
974
975
4.90k
    src_port_item = proto_tree_add_item(udp_tree, hf_udp_srcport, tvb, offset, 2, ENC_BIG_ENDIAN);
976
4.90k
    dst_port_item = proto_tree_add_item(udp_tree, hf_udp_dstport, tvb, offset + 2, 2, ENC_BIG_ENDIAN);
977
978
4.90k
    p_add_proto_data(pinfo->pool, pinfo, hf_udp_srcport, pinfo->curr_layer_num, GUINT_TO_POINTER(udph->uh_sport));
979
4.90k
    p_add_proto_data(pinfo->pool, pinfo, hf_udp_dstport, pinfo->curr_layer_num, GUINT_TO_POINTER(udph->uh_dport));
980
981
4.90k
    hidden_item = proto_tree_add_item(udp_tree, hf_udp_port, tvb, offset, 2, ENC_BIG_ENDIAN);
982
4.90k
    proto_item_set_hidden(hidden_item);
983
4.90k
    hidden_item = proto_tree_add_item(udp_tree, hf_udp_port, tvb, offset + 2, 2, ENC_BIG_ENDIAN);
984
4.90k
    proto_item_set_hidden(hidden_item);
985
986
    /* The beginning port number, 32768 + 666 (33434), is from LBL's traceroute.c source code and this code
987
     * further assumes that 3 attempts are made per hop */
988
4.90k
    if ((udph->uh_sport > (32768 + 666)) && (udph->uh_sport <= (32768 + 666 + 30))) {
989
0
        expert_add_info_format(pinfo, src_port_item, &ei_udp_possible_traceroute, "Possible traceroute: hop #%u, attempt #%u",
990
0
                     ((udph->uh_sport - 32768 - 666 - 1) / 3) + 1,
991
0
                     ((udph->uh_sport - 32768 - 666 - 1) % 3) + 1);
992
0
    }
993
4.90k
    if ((udph->uh_dport > (32768 + 666)) && (udph->uh_dport <= (32768 + 666 + 30))) {
994
2
        expert_add_info_format(pinfo, dst_port_item, &ei_udp_possible_traceroute, "Possible traceroute: hop #%u, attempt #%u",
995
2
                         ((udph->uh_dport - 32768 - 666 - 1) / 3) + 1,
996
2
                         ((udph->uh_dport - 32768 - 666 - 1) % 3) + 1);
997
2
    }
998
999
4.90k
    udph->uh_ulen = udph->uh_sum_cov = tvb_get_ntohs(tvb, offset + 4);
1000
4.90k
    if (ip_proto == IP_PROTO_UDP) {
1001
4.90k
        len_cov_item = proto_tree_add_item(udp_tree, hf_udp_length, tvb, offset + 4, 2, ENC_BIG_ENDIAN);
1002
4.90k
        if (udph->uh_ulen == 0 && pinfo->src.type == AT_IPv6) {
1003
            /* RFC 2675 (section 4) - UDP Jumbograms */
1004
24
            udph->uh_ulen = udph->uh_sum_cov = reported_len;
1005
24
            udp_jumbogram = true;
1006
24
        }
1007
4.90k
        if (udph->uh_ulen < 8) {
1008
            /* Bogus length - it includes the header, so it must be >= 8. */
1009
0
            proto_item_append_text(len_cov_item, " (bogus, must be >= 8)");
1010
0
            expert_add_info_format(pinfo, len_cov_item, &ei_udp_length_bad, "Bad length value %u < 8", udph->uh_ulen);
1011
0
            col_append_fstr(pinfo->cinfo, COL_INFO, " [BAD UDP LENGTH %u < 8]", udph->uh_ulen);
1012
0
            return;
1013
0
        }
1014
4.90k
        if ((udph->uh_ulen > reported_len) && (!pinfo->fragmented) && (!pinfo->flags.in_error_pkt)) {
1015
            /* Bogus length - it goes past the end of the IP payload */
1016
4.78k
            proto_item_append_text(len_cov_item, " (bogus, payload length %u)", reported_len);
1017
4.78k
            expert_add_info_format(pinfo, len_cov_item, &ei_udp_length_bad, "Bad length value %u > IP payload length", udph->uh_ulen);
1018
4.78k
            col_append_fstr(pinfo->cinfo, COL_INFO, " [BAD UDP LENGTH %u > IP PAYLOAD LENGTH]", udph->uh_ulen);
1019
            /*return;*/
1020
4.78k
        }
1021
4.90k
        if (udp_jumbogram && (udph->uh_ulen < 65536)) {
1022
24
            expert_add_info(pinfo, len_cov_item, &ei_udp_length_bad_zero);
1023
24
        }
1024
4.90k
    }
1025
0
    else {
1026
0
        len_cov_item = proto_tree_add_item(udp_tree, hf_udplite_checksum_coverage, tvb, offset + 4, 2, ENC_BIG_ENDIAN);
1027
0
        udph->uh_ulen = reported_len;
1028
0
        if (udph->uh_sum_cov == 0) {
1029
0
            udph->uh_sum_cov = reported_len;
1030
0
        }
1031
0
        item = proto_tree_add_uint(udp_tree, hf_udp_length, tvb, offset + 4, 0, udph->uh_ulen);
1032
0
        proto_item_set_generated(item);
1033
0
        if ((udph->uh_sum_cov < 8) || (udph->uh_sum_cov > udph->uh_ulen)) {
1034
            /* Bogus coverage - it includes the header, so it must be >= 8, and no larger than the IP payload size. */
1035
0
            proto_item_append_text(len_cov_item, " (bogus, must be >= 8 and <= %u)", udph->uh_ulen);
1036
0
            expert_add_info_format(pinfo, len_cov_item, &ei_udplite_checksum_coverage_bad, "Bad checksum coverage length value %u < 8 or > %u",
1037
0
                         udph->uh_sum_cov, udph->uh_ulen);
1038
0
            col_append_fstr(pinfo->cinfo, COL_INFO, " [BAD LIGHTWEIGHT UDP CHECKSUM COVERAGE LENGTH %u < 8 or > %u]",
1039
0
                        udph->uh_sum_cov, udph->uh_ulen);
1040
0
            if (!udplite_ignore_checksum_coverage) {
1041
0
                return;
1042
0
            }
1043
0
        }
1044
0
    }
1045
1046
4.90k
    col_append_str_uint(pinfo->cinfo, COL_INFO, "Len", udph->uh_ulen - 8, " "); /* Payload length */
1047
4.90k
    if (udp_jumbogram)
1048
24
        col_append_str(pinfo->cinfo, COL_INFO, " [Jumbogram]");
1049
1050
4.90k
    udph->uh_sum = tvb_get_ntohs(tvb, offset + 6);
1051
4.90k
    if (udph->uh_sum == 0) {
1052
        /* No checksum supplied in the packet. */
1053
1054
206
        bool ignore_zero_checksum = (ip_proto == IP_PROTO_UDP) &&
1055
206
            ((pinfo->src.type == AT_IPv4) ||
1056
206
             (pinfo->src.type == AT_NONE) ||
1057
27
             ((pinfo->src.type == AT_IPv6) && udp_ignore_ipv6_zero_checksum));
1058
206
        proto_checksum_enum_e checksum_status;
1059
1060
206
        item = proto_tree_add_item(udp_tree, hf_udp_checksum, tvb, offset + 6, 2, ENC_BIG_ENDIAN);
1061
206
        if (ignore_zero_checksum || pinfo->flags.in_error_pkt) {
1062
179
            proto_item_append_text(item, " [zero-value ignored]");
1063
179
            checksum_status = PROTO_CHECKSUM_E_NOT_PRESENT;
1064
179
        }
1065
27
        else {
1066
27
            proto_item_append_text(item, " [zero-value illegal]");
1067
27
            checksum_status = PROTO_CHECKSUM_E_ILLEGAL;
1068
27
            expert_add_info(pinfo, item, &ei_udp_checksum_zero);
1069
27
            col_append_str(pinfo->cinfo, COL_INFO, " [ILLEGAL CHECKSUM (0)]");
1070
27
        }
1071
206
        checksum_tree = proto_item_add_subtree(item, ett_udp_checksum);
1072
206
        item = proto_tree_add_uint(checksum_tree, hf_udp_checksum_status, tvb, offset + 6, 2, checksum_status);
1073
206
        proto_item_set_generated(item);
1074
206
    }
1075
4.70k
    else if (!pinfo->fragmented && (len >= reported_len) &&
1076
4.66k
                         (len >= udph->uh_sum_cov) && (reported_len >= udph->uh_sum_cov) &&
1077
74
                         (udph->uh_sum_cov >= 8)) {
1078
        /* The packet isn't part of a fragmented datagram and isn't
1079
             truncated, so we can checksum it.
1080
             XXX - make a bigger scatter-gather list once we do fragment
1081
             reassembly? */
1082
1083
74
        if (((ip_proto == IP_PROTO_UDP) && udp_check_checksum) ||
1084
74
                ((ip_proto == IP_PROTO_UDPLITE) && udplite_check_checksum)) {
1085
            /* Set up the fields of the pseudo-header. */
1086
0
            SET_CKSUM_VEC_PTR(cksum_vec[0], (const uint8_t *)pinfo->src.data, pinfo->src.len);
1087
0
            SET_CKSUM_VEC_PTR(cksum_vec[1], (const uint8_t *)pinfo->dst.data, pinfo->dst.len);
1088
0
            switch (pinfo->src.type) {
1089
1090
0
            case AT_IPv4:
1091
0
                if (ip_proto == IP_PROTO_UDP)
1092
0
                    phdr[0] = g_htonl((ip_proto<<16) | udph->uh_ulen);
1093
0
                else
1094
0
                    phdr[0] = g_htonl((ip_proto<<16) | reported_len);
1095
0
                SET_CKSUM_VEC_PTR(cksum_vec[2], (const uint8_t *)&phdr, 4);
1096
0
                break;
1097
1098
0
            case AT_IPv6:
1099
0
                if (ip_proto == IP_PROTO_UDP)
1100
0
                    phdr[0] = g_htonl(udph->uh_ulen);
1101
0
                else
1102
0
                    phdr[0] = g_htonl(reported_len);
1103
0
                phdr[1] = g_htonl(ip_proto);
1104
0
                SET_CKSUM_VEC_PTR(cksum_vec[2], (const uint8_t *)&phdr, 8);
1105
0
                break;
1106
1107
0
            case AT_ILNP_NID:
1108
0
                if (ip_proto == IP_PROTO_UDP)
1109
0
                    phdr[0] = g_htonl(udph->uh_ulen);
1110
0
                else
1111
0
                    phdr[0] = g_htonl(reported_len);
1112
0
                phdr[1] = g_htonl(ip_proto);
1113
0
                SET_CKSUM_VEC_PTR(cksum_vec[2], (const uint8_t *)&phdr, 8);
1114
0
                break;
1115
1116
0
            default:
1117
                /* UDP runs only atop IPv4 and IPv6.... */
1118
0
                DISSECTOR_ASSERT_NOT_REACHED();
1119
0
                break;
1120
0
            }
1121
            /*
1122
             * in_cksum() should never return 0xFFFF here, because, to quote
1123
             * RFC 1624 section 3 "Discussion":
1124
             *
1125
             *     In one's complement, there are two representations of
1126
             *     zero: the all zero and the all one bit values, often
1127
             *     referred to as +0 and -0.  One's complement addition
1128
             *     of non-zero inputs can produce -0 as a result, but
1129
             *     never +0.  Since there is guaranteed to be at least
1130
             *     one non-zero field in the IP header, and the checksum
1131
             *     field in the protocol header is the complement of the
1132
             *     sum, the checksum field can never contain ~(+0), which
1133
             *     is -0 (0xFFFF).  It can, however, contain ~(-0), which
1134
             *     is +0 (0x0000).
1135
             *
1136
             * RFC 1624 is discussing the checksum of the *IPv4* header,
1137
             * where the "version" field is 4, ensuring that, in a valid
1138
             * IPv4 header, there is at least one non-zero field, but it
1139
             * also applies to a UDP datagram, because the length includes
1140
             * the length of the UDP header, so at least one field in a UDP
1141
             * datagram is non-zero.
1142
             *
1143
             * in_cksum() returns the negation of the one's-complement
1144
             * sum of all the data handed to it, and that data won't be
1145
             * all zero, so the sum won't be 0 (+0), and thus the negation
1146
             * won't be -0, i.e. won't be 0xFFFF.
1147
             */
1148
            /*
1149
             * Linux and Windows, at least, when performing Local Checksum
1150
             * Offload (during Generic Segmentation Offload or at other
1151
             * times), place the one's complement sum of the pseudo header
1152
             * in the checksum fields initially, which provides the necessary
1153
             * correction when a device (or driver) computes the one's
1154
             * complement checksum of each buffer in the skbuff. Since it is
1155
             * not inverted, the partial_cksum will never be 0x0000 by the
1156
             * same argument as above.
1157
             */
1158
0
            uint16_t partial_cksum;
1159
0
            SET_CKSUM_VEC_TVB(cksum_vec[3], tvb, offset, udph->uh_sum_cov);
1160
0
            computed_cksum = in_cksum_ret_partial(&cksum_vec[0], 4, &partial_cksum);
1161
0
            uint16_t shouldbe_cksum = in_cksum_shouldbe(udph->uh_sum, computed_cksum);
1162
0
            if (computed_cksum != 0 && udph->uh_sum == g_htons(partial_cksum)) {
1163
                /* Don't use PROTO_CHECKSUM_IN_CKSUM because we expect the value
1164
                 * to match what we pass in. */
1165
0
                item = proto_tree_add_checksum(udp_tree, tvb, offset + 6, hf_udp_checksum, hf_udp_checksum_status, &ei_udp_checksum_bad,
1166
0
                                                                                    pinfo, g_htons(partial_cksum), ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY);
1167
0
                proto_item_append_text(item, " (matches partial checksum, not 0x%04x, likely caused by \"UDP checksum offload\")", shouldbe_cksum);
1168
0
                expert_add_info(pinfo, item, &ei_udp_checksum_partial);
1169
0
                computed_cksum = 0;
1170
                /* XXX: Find some way hint to QUIC (or other dissectors) that
1171
                 * GSO is a possibility so that extra effort can be made to
1172
                 * recover coalesced PDUs? E.g., by searching heuristically
1173
                 * through the payload for the DCID bytes if they're non-zero?
1174
                 * Add a new status, e.g. PROTO_CHECKSUM_E_PARTIAL?
1175
                 */
1176
0
            } else {
1177
0
                item = proto_tree_add_checksum(udp_tree, tvb, offset + 6, hf_udp_checksum, hf_udp_checksum_status, &ei_udp_checksum_bad,
1178
0
                                                                                    pinfo, computed_cksum, ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY|PROTO_CHECKSUM_IN_CKSUM);
1179
0
            }
1180
0
            checksum_tree = proto_item_add_subtree(item, ett_udp_checksum);
1181
1182
0
            if (computed_cksum != 0) {
1183
0
                proto_item_append_text(item, " (maybe caused by \"UDP checksum offload\"?)");
1184
0
                col_append_str(pinfo->cinfo, COL_INFO, " [UDP CHECKSUM INCORRECT]");
1185
0
                calc_item = proto_tree_add_uint(checksum_tree, hf_udp_checksum_calculated,
1186
0
                        tvb, offset + 6, 2, in_cksum_shouldbe(udph->uh_sum, computed_cksum));
1187
0
            }
1188
0
            else {
1189
0
                calc_item = proto_tree_add_uint(checksum_tree, hf_udp_checksum_calculated,
1190
0
                        tvb, offset + 6, 2, shouldbe_cksum);
1191
0
            }
1192
0
            proto_item_set_generated(calc_item);
1193
1194
0
        }
1195
74
        else {
1196
74
            proto_tree_add_checksum(udp_tree, tvb, offset + 6, hf_udp_checksum, hf_udp_checksum_status, &ei_udp_checksum_bad, pinfo, 0, ENC_BIG_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
1197
74
        }
1198
74
    }
1199
4.62k
    else {
1200
4.62k
        proto_tree_add_checksum(udp_tree, tvb, offset + 6, hf_udp_checksum, hf_udp_checksum_status, &ei_udp_checksum_bad, pinfo, 0, ENC_BIG_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
1201
4.62k
    }
1202
1203
    /* Skip over header */
1204
4.90k
    offset += 8;
1205
1206
4.90k
    pinfo->ptype = PT_UDP;
1207
4.90k
    pinfo->srcport = udph->uh_sport;
1208
4.90k
    pinfo->destport = udph->uh_dport;
1209
1210
    /* Find (and extend) an existing conversation, or create a new one.
1211
     * Don't try finding at all costs (NO_GREEDY)
1212
     */
1213
4.90k
    conv = find_conversation_strat(pinfo, CONVERSATION_UDP, NO_GREEDY, false);
1214
4.90k
    if(!conv) {
1215
1.04k
        conv=conversation_new_strat(pinfo, CONVERSATION_UDP, 0);
1216
1.04k
    }
1217
3.86k
    else {
1218
        /* Explicitly and immediately move forward the conversation last_frame */
1219
3.86k
        if (!(pinfo->fd->visited) && (pinfo->num > conv->last_frame)) {
1220
3.83k
            conv->last_frame = pinfo->num;
1221
3.83k
        }
1222
3.86k
    }
1223
1224
4.90k
    udpd = get_udp_conversation_data(conv, pinfo);
1225
4.90k
    if (udpd) {
1226
4.90k
        item = proto_tree_add_uint(udp_tree, hf_udp_stream, tvb, offset, 0, udpd->stream);
1227
4.90k
        proto_item_set_generated(item);
1228
4.90k
        p_add_proto_data(pinfo->pool, pinfo, hf_udp_stream, pinfo->curr_proto_layer_num, GUINT_TO_POINTER(udpd->stream + 1)); // Add 1 to distinguish stream 0 from NULL
1229
1230
        /* Copy the stream index into the header as well to make it available
1231
        * to tap listeners.
1232
        */
1233
4.90k
        udph->uh_stream = udpd->stream;
1234
1235
        /* Copy the stream index into pinfo as well to make it available
1236
         * to callback functions (essentially conversation following events in GUI)
1237
         */
1238
4.90k
        pinfo->stream_id = udpd->stream;
1239
1240
        /* Follow-up of the conversation over ICMP errors.
1241
         * When coming over an error packet (typically ICMP), we want to save the
1242
         * conversation type, and have a specific tracking then we can match with
1243
         * ordinary, non error packets.
1244
         */
1245
4.90k
        if (pinfo->flags.in_error_pkt) {
1246
            /* Save the conversation type */
1247
0
            pinfo->track_ctype = CONVERSATION_UDP;
1248
1249
0
            conversation_t *err_conv = NULL;
1250
0
            err_conv = find_conversation_err_pkts(pinfo->num, CONVERSATION_UDP, udpd->stream, conv->conv_index);
1251
0
            if(!err_conv) {
1252
                /* Create the conversation tracking the ordinary UDP conversation */
1253
0
                err_conv = conversation_new_err_pkts(pinfo->num, CONVERSATION_UDP, udpd->stream, conv->conv_index);
1254
1255
                /* Align the setup_frame with the UDP conversation's one */
1256
0
                err_conv->setup_frame = conv->setup_frame;
1257
0
            }
1258
0
            else if (pinfo->num > err_conv->last_frame) {
1259
                /* If we have multiple error packets related to this same UDP conversation,
1260
                 * extend the tracker conversation.
1261
                 */
1262
0
                err_conv->last_frame = pinfo->num;
1263
0
            }
1264
0
        }
1265
4.90k
    }
1266
1267
4.90k
    tap_queue_packet(udp_tap, pinfo, udph);
1268
1269
4.90k
    if (udpd && ((udpd->fwd && udpd->fwd->command) || (udpd->rev && udpd->rev->command))) {
1270
0
        process_tree = proto_tree_add_subtree(udp_tree, tvb, offset, 0, ett_udp_process_info, &ti, "Process Information");
1271
0
        proto_item_set_generated(ti);
1272
0
        if (udpd->fwd && udpd->fwd->command) {
1273
0
            proto_tree_add_uint(process_tree, hf_udp_proc_dst_uid, tvb, 0, 0, udpd->fwd->process_uid);
1274
0
            proto_tree_add_uint(process_tree, hf_udp_proc_dst_pid, tvb, 0, 0, udpd->fwd->process_pid);
1275
0
            proto_tree_add_string(process_tree, hf_udp_proc_dst_uname, tvb, 0, 0, udpd->fwd->username);
1276
0
            proto_tree_add_string(process_tree, hf_udp_proc_dst_cmd, tvb, 0, 0, udpd->fwd->command);
1277
0
        }
1278
0
        if (udpd->rev->command) {
1279
0
            proto_tree_add_uint(process_tree, hf_udp_proc_src_uid, tvb, 0, 0, udpd->rev->process_uid);
1280
0
            proto_tree_add_uint(process_tree, hf_udp_proc_src_pid, tvb, 0, 0, udpd->rev->process_pid);
1281
0
            proto_tree_add_string(process_tree, hf_udp_proc_src_uname, tvb, 0, 0, udpd->rev->username);
1282
0
            proto_tree_add_string(process_tree, hf_udp_proc_src_cmd, tvb, 0, 0, udpd->rev->command);
1283
0
        }
1284
0
    }
1285
1286
    /* Do we need to calculate timestamps relative to the udp-stream? */
1287
    /* Different boolean preferences have to be checked. */
1288
    /* If the protocol is UDP then the UDP preference */
1289
4.90k
    if (!pinfo->flags.in_error_pkt &&
1290
4.90k
            ((ip_proto == IP_PROTO_UDP && udp_calculate_ts)
1291
             /* Otherwise the UDP-Lite preference */
1292
4.90k
             || (ip_proto == IP_PROTO_UDPLITE && udplite_calculate_ts))) {
1293
4.90k
        udp_handle_timestamps(pinfo, tvb, udp_tree, udpd, ip_proto);
1294
4.90k
    }
1295
1296
4.90k
    if (udph->uh_ulen == 8) {
1297
        /* Empty UDP payload, nothing left to do. */
1298
5
        return;
1299
5
    }
1300
1301
    /*
1302
     * Call sub-dissectors.
1303
     *
1304
     * XXX - should we do this if this is included in an error packet?
1305
     * It might be nice to see the details of the packet that caused the
1306
     * ICMP error, but it might not be nice to have the dissector update
1307
     * state based on it.
1308
     * Also, we probably don't want to run UDP taps on those packets.
1309
     *
1310
     * We definitely don't want to do it for an error packet if there's
1311
     * nothing left in the packet.
1312
     */
1313
4.90k
    if (!pinfo->flags.in_error_pkt || (tvb_captured_length_remaining(tvb, offset) > 0)) {
1314
        /* If the user has a "Follow UDP Stream" window loading, pass a pointer
1315
         * to the payload tvb through the tap system. */
1316
4.90k
        if (have_tap_listener(udp_follow_tap)) {
1317
0
            follow_stream_tap_data_t *follow_data = wmem_new0(pinfo->pool, follow_stream_tap_data_t);
1318
0
            follow_data->tvb = tvb_new_subset_length(tvb, offset, udph->uh_ulen - 8);
1319
0
            follow_data->stream_id = udph->uh_stream;
1320
0
            follow_data->substream_id = SUBSTREAM_UNUSED;
1321
0
            copy_address_shallow(&follow_data->src, &udph->ip_src);
1322
0
            copy_address_shallow(&follow_data->dst, &udph->ip_dst);
1323
0
            follow_data->ptype = PT_UDP;
1324
0
            follow_data->srcport = udph->uh_sport;
1325
0
            follow_data->destport = udph->uh_dport;
1326
1327
0
            tap_queue_packet(udp_follow_tap, pinfo, follow_data);
1328
0
        }
1329
1330
4.90k
        decode_udp_ports(tvb, offset, pinfo, udp_tree, udph->uh_sport, udph->uh_dport, udph->uh_ulen);
1331
4.90k
    }
1332
4.90k
}
1333
1334
static int
1335
dissect_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
1336
4.90k
{
1337
4.90k
    dissect(tvb, pinfo, tree, IP_PROTO_UDP);
1338
4.90k
    return tvb_captured_length(tvb);
1339
4.90k
}
1340
1341
static int
1342
dissect_udplite(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
1343
0
{
1344
0
    dissect(tvb, pinfo, tree, IP_PROTO_UDPLITE);
1345
0
    return tvb_captured_length(tvb);
1346
0
}
1347
1348
static void
1349
udp_init(void)
1350
16
{
1351
16
    udp_stream_count = 0;
1352
16
}
1353
1354
void
1355
proto_register_udp(void)
1356
16
{
1357
16
    static hf_register_info hf_udp[] = {
1358
16
        { &hf_udp_srcport,
1359
16
            { "Source Port", "udp.srcport",
1360
16
              FT_UINT16, BASE_PT_UDP, NULL, 0x0,
1361
16
              NULL, HFILL }
1362
16
        },
1363
16
        { &hf_udp_dstport,
1364
16
            { "Destination Port", "udp.dstport",
1365
16
              FT_UINT16, BASE_PT_UDP, NULL, 0x0,
1366
16
              NULL, HFILL }
1367
16
        },
1368
16
        { &hf_udp_port,
1369
16
            { "Source or Destination Port", "udp.port",
1370
16
              FT_UINT16, BASE_PT_UDP, NULL, 0x0,
1371
16
              NULL, HFILL }
1372
16
        },
1373
16
        { &hf_udp_stream,
1374
16
            { "Stream index", "udp.stream",
1375
16
              FT_UINT32, BASE_DEC, NULL, 0x0,
1376
16
              NULL, HFILL }
1377
16
        },
1378
16
        { &hf_udp_stream_pnum,
1379
16
            { "Stream Packet Number", "udp.stream.pnum",
1380
16
              FT_UINT32, BASE_DEC, NULL, 0x0,
1381
16
              "Relative packet number in this UDP stream", HFILL }
1382
16
        },
1383
16
        { &hf_udp_length,
1384
16
            { "Length", "udp.length",
1385
16
              FT_UINT16, BASE_DEC, NULL, 0x0,
1386
16
              "Length in octets including this header and the data", HFILL }
1387
16
        },
1388
16
        { &hf_udp_checksum,
1389
16
            { "Checksum", "udp.checksum",
1390
16
              FT_UINT16, BASE_HEX, NULL, 0x0,
1391
16
              "Details at: https://www.wireshark.org/docs/wsug_html_chunked/ChAdvChecksums.html", HFILL }
1392
16
        },
1393
16
        { &hf_udp_checksum_calculated,
1394
16
            { "Calculated Checksum", "udp.checksum_calculated",
1395
16
              FT_UINT16, BASE_HEX, NULL, 0x0,
1396
16
              "The expected UDP checksum field as calculated from the UDP packet", HFILL }
1397
16
        },
1398
16
        { &hf_udp_checksum_status,
1399
16
            { "Checksum Status", "udp.checksum.status",
1400
16
              FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x0,
1401
16
              NULL, HFILL }
1402
16
        },
1403
16
        { &hf_udp_proc_src_uid,
1404
16
            { "Source process user ID", "udp.proc.srcuid",
1405
16
              FT_UINT32, BASE_DEC, NULL, 0x0,
1406
16
              NULL, HFILL }
1407
16
        },
1408
16
        { &hf_udp_proc_src_pid,
1409
16
            { "Source process ID", "udp.proc.srcpid",
1410
16
              FT_UINT32, BASE_DEC, NULL, 0x0,
1411
16
              NULL, HFILL }
1412
16
        },
1413
16
        { &hf_udp_proc_src_uname,
1414
16
            { "Source process user name", "udp.proc.srcuname",
1415
16
              FT_STRING, BASE_NONE, NULL, 0x0,
1416
16
              NULL, HFILL }
1417
16
        },
1418
16
        { &hf_udp_proc_src_cmd,
1419
16
            { "Source process name", "udp.proc.srccmd",
1420
16
              FT_STRING, BASE_NONE, NULL, 0x0,
1421
16
              "Source process command name", HFILL }
1422
16
        },
1423
16
        { &hf_udp_proc_dst_uid,
1424
16
            { "Destination process user ID", "udp.proc.dstuid",
1425
16
              FT_UINT32, BASE_DEC, NULL, 0x0,
1426
16
              NULL, HFILL }
1427
16
        },
1428
16
        { &hf_udp_proc_dst_pid,
1429
16
            { "Destination process ID", "udp.proc.dstpid",
1430
16
              FT_UINT32, BASE_DEC, NULL, 0x0,
1431
16
              NULL, HFILL }
1432
16
        },
1433
16
        { &hf_udp_proc_dst_uname,
1434
16
            { "Destination process user name", "udp.proc.dstuname",
1435
16
              FT_STRING, BASE_NONE, NULL, 0x0,
1436
16
              NULL, HFILL }
1437
16
        },
1438
16
        { &hf_udp_proc_dst_cmd,
1439
16
            { "Destination process name", "udp.proc.dstcmd",
1440
16
              FT_STRING, BASE_NONE, NULL, 0x0,
1441
16
              "Destination process command name", HFILL }
1442
16
        },
1443
16
        { &hf_udp_pdu_size,
1444
16
            { "PDU Size", "udp.pdu.size",
1445
16
              FT_UINT32, BASE_DEC, NULL, 0x0,
1446
16
              "The size of this PDU", HFILL }
1447
16
        },
1448
16
        { &hf_udp_ts_relative,
1449
16
            { "Time since first frame", "udp.time_relative",
1450
16
              FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
1451
16
              "Time relative to first frame in this UDP stream", HFILL }
1452
16
        },
1453
16
        { &hf_udp_ts_delta,
1454
16
            { "Time since previous frame", "udp.time_delta",
1455
16
              FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
1456
16
              "Time delta from previous frame in this UDP stream", HFILL }
1457
16
        },
1458
16
        { &hf_udp_payload,
1459
16
            { "Payload", "udp.payload",
1460
16
              FT_BYTES, BASE_NONE, NULL, 0x0,
1461
16
              NULL, HFILL }
1462
16
        },
1463
16
    };
1464
1465
16
    static hf_register_info hf_udplite[] = {
1466
16
        { &hf_udplite_checksum_coverage,
1467
16
            { "Checksum coverage", "udp.checksum_coverage",
1468
16
              FT_UINT16, BASE_DEC, NULL, 0x0,
1469
16
              NULL, HFILL }
1470
16
        },
1471
16
    };
1472
1473
16
    static int *ett[] = {
1474
16
        &ett_udp,
1475
16
        &ett_udp_checksum,
1476
16
        &ett_udp_process_info,
1477
16
        &ett_udp_timestamps
1478
16
    };
1479
1480
16
    static ei_register_info ei[] = {
1481
16
        { &ei_udp_possible_traceroute, { "udp.possible_traceroute", PI_SEQUENCE, PI_CHAT, "Possible traceroute", EXPFILL }},
1482
16
        { &ei_udp_length_bad, { "udp.length.bad", PI_MALFORMED, PI_ERROR, "Bad length value", EXPFILL }},
1483
16
        { &ei_udplite_checksum_coverage_bad, { "udplite.checksum_coverage.bad", PI_MALFORMED, PI_ERROR, "Bad checksum coverage length value", EXPFILL }},
1484
16
        { &ei_udp_checksum_zero, { "udp.checksum.zero", PI_CHECKSUM, PI_ERROR, "Illegal checksum value (0)", EXPFILL }},
1485
16
        { &ei_udp_checksum_partial, { "udp.checksum.partial", PI_CHECKSUM, PI_NOTE, "Partial (pseudo header) checksum (likely caused by \"UDP checksum offload\")", EXPFILL }},
1486
16
        { &ei_udp_checksum_bad, { "udp.checksum.bad", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
1487
16
        { &ei_udp_length_bad_zero, { "udp.length.bad_zero", PI_PROTOCOL, PI_WARN, "Length is zero but payload < 65536", EXPFILL }},
1488
16
    };
1489
1490
16
    static build_valid_func udp_da_src_values[1] = {udp_src_value};
1491
16
    static build_valid_func udp_da_dst_values[1] = {udp_dst_value};
1492
16
    static build_valid_func udp_da_both_values[2] = {udp_src_value, udp_dst_value};
1493
16
    static decode_as_value_t udp_da_values[3] = {{udp_src_prompt, 1, udp_da_src_values}, {udp_dst_prompt, 1, udp_da_dst_values}, {udp_both_prompt, 2, udp_da_both_values}};
1494
16
    static decode_as_t udp_da = {"udp", "udp.port", 3, 2, udp_da_values, "UDP", "port(s) as",
1495
16
                     decode_as_default_populate_list, decode_as_default_reset, decode_as_default_change, NULL, NULL, NULL };
1496
1497
16
    module_t *udp_module;
1498
16
    module_t *udplite_module;
1499
16
    expert_module_t* expert_udp;
1500
1501
16
    proto_udp = proto_register_protocol("User Datagram Protocol", "UDP", "udp");
1502
16
    proto_register_field_array(proto_udp, hf_udp, array_length(hf_udp));
1503
16
    udp_handle = register_dissector("udp", dissect_udp, proto_udp);
1504
16
    udp_cap_handle = register_capture_dissector("udp", capture_udp, proto_udp);
1505
16
    expert_udp = expert_register_protocol(proto_udp);
1506
1507
16
    proto_udplite = proto_register_protocol("Lightweight User Datagram Protocol", "UDP-Lite", "udplite");
1508
16
    proto_register_field_array(proto_udplite, hf_udplite, array_length(hf_udplite));
1509
16
    udplite_handle = register_dissector("udplite", dissect_udplite, proto_udplite);
1510
16
    udplite_cap_handle = register_capture_dissector("udplite", capture_udp, proto_udplite);
1511
1512
16
    proto_register_subtree_array(ett, array_length(ett));
1513
16
    expert_register_field_array(expert_udp, ei, array_length(ei));
1514
1515
    /* subdissector code */
1516
16
    udp_dissector_table = register_dissector_table("udp.port", "UDP port", proto_udp, FT_UINT16, BASE_DEC);
1517
16
    heur_subdissector_list = register_heur_dissector_list_with_description("udp", "UDP heuristic", proto_udp);
1518
1519
16
    register_capture_dissector_table("udp.port", "UDP");
1520
1521
    /* Register configuration preferences */
1522
16
    udp_module = prefs_register_protocol(proto_udp, NULL);
1523
16
    prefs_register_bool_preference(udp_module, "summary_in_tree",
1524
16
                         "Show UDP summary in protocol tree",
1525
16
                         "Whether the UDP summary line should be shown in the protocol tree",
1526
16
                         &udp_summary_in_tree);
1527
16
    prefs_register_bool_preference(udp_module, "try_heuristic_first",
1528
16
                         "Try heuristic sub-dissectors first",
1529
16
                         "Try to decode a packet using an heuristic sub-dissector"
1530
16
                         " before using a sub-dissector registered to a specific port",
1531
16
                         &try_heuristic_first);
1532
16
    prefs_register_bool_preference(udp_module, "check_checksum",
1533
16
                         "Validate the UDP checksum if possible",
1534
16
                         "Whether to validate the UDP checksum",
1535
16
                         &udp_check_checksum);
1536
16
    prefs_register_bool_preference(udp_module, "ignore_ipv6_zero_checksum",
1537
16
                         "Ignore zero-value UDP checksums over IPv6",
1538
16
                         "Whether to ignore zero-value UDP checksums over IPv6",
1539
16
                         &udp_ignore_ipv6_zero_checksum);
1540
16
    prefs_register_bool_preference(udp_module, "process_info",
1541
16
                         "Collect process flow information",
1542
16
                         "Collect process flow information from IPFIX",
1543
16
                         &udp_process_info);
1544
16
    prefs_register_bool_preference(udp_module, "calculate_timestamps",
1545
16
                         "Calculate stream packet number and timestamps",
1546
16
                         "Calculate relative packet number and timestamps relative to the first frame and the previous frame in the udp conversation",
1547
16
                         &udp_calculate_ts);
1548
1549
16
    udplite_module = prefs_register_protocol(proto_udplite, NULL);
1550
16
    prefs_register_bool_preference(udplite_module, "ignore_checksum_coverage",
1551
16
                         "Ignore UDP-Lite checksum coverage",
1552
16
                         "Ignore an invalid checksum coverage field and continue dissection",
1553
16
                         &udplite_ignore_checksum_coverage);
1554
16
    prefs_register_bool_preference(udplite_module, "check_checksum",
1555
16
                         "Validate the UDP-Lite checksum if possible",
1556
16
                         "Whether to validate the UDP-Lite checksum",
1557
16
                         &udplite_check_checksum);
1558
16
    prefs_register_bool_preference(udplite_module, "calculate_timestamps",
1559
16
                         "Calculate stream packet number and timestamps",
1560
16
                         "Calculate relative packet number and timestamps relative to the first frame and the previous frame in the udp-lite conversation",
1561
16
                         &udplite_calculate_ts);
1562
1563
16
    register_decode_as(&udp_da);
1564
16
    register_conversation_table(proto_udp, false, udpip_conversation_packet, udpip_endpoint_packet);
1565
16
    register_conversation_filter("udp", "UDP", udp_filter_valid, udp_build_filter_by_id, NULL);
1566
16
    register_follow_stream(proto_udp, "udp_follow", udp_follow_conv_filter, udp_follow_index_filter, udp_follow_address_filter,
1567
16
                        udp_port_to_display, follow_stream_tap_listener, get_udp_stream_count, NULL);
1568
1569
16
    register_init_routine(udp_init);
1570
1571
16
    udp_tap = register_tap("udp");
1572
16
    udp_follow_tap = register_tap("udp_follow");
1573
16
}
1574
1575
void
1576
proto_reg_handoff_udp(void)
1577
16
{
1578
1579
16
    dissector_add_uint("ip.proto", IP_PROTO_UDP, udp_handle);
1580
16
    dissector_add_uint("ip.proto", IP_PROTO_UDPLITE, udplite_handle);
1581
1582
16
    capture_dissector_add_uint("ip.proto", IP_PROTO_UDP, udp_cap_handle);
1583
16
    capture_dissector_add_uint("ip.proto", IP_PROTO_UDPLITE, udplite_cap_handle);
1584
1585
16
    exported_pdu_tap = find_tap_id(EXPORT_PDU_TAP_NAME_LAYER_4);
1586
16
}
1587
1588
/*
1589
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
1590
 *
1591
 * Local variables:
1592
 * c-basic-offset: 4
1593
 * tab-width: 8
1594
 * indent-tabs-mode: nil
1595
 * End:
1596
 *
1597
 * vi: set shiftwidth=4 tabstop=8 expandtab:
1598
 * :indentSize=4:tabSize=8:noTabs=true:
1599
 */