2 * Routines for UDP packet disassembly
4 * $Id: packet-udp.c,v 1.102 2002/01/22 15:05:43 nneul Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * Richard Sharpe, 13-Feb-1999, added dispatch table support and
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>
45 #include <epan/packet.h>
46 #include <epan/resolv.h>
51 #include "packet-udp.h"
53 #include "packet-ip.h"
54 #include <epan/conversation.h>
56 static int proto_udp = -1;
57 static int hf_udp_srcport = -1;
58 static int hf_udp_dstport = -1;
59 static int hf_udp_port = -1;
60 static int hf_udp_length = -1;
61 static int hf_udp_checksum = -1;
62 static int hf_udp_checksum_bad = -1;
64 static gint ett_udp = -1;
66 /* Place UDP summary in proto tree */
67 static gboolean udp_summary_in_tree = TRUE;
69 /* UDP structs and definitions */
71 typedef struct _e_udphdr {
78 static dissector_table_t udp_dissector_table;
79 static heur_dissector_list_t heur_subdissector_list;
80 static dissector_handle_t data_handle;
82 /* Determine if there is a sub-dissector and call it. This has been */
83 /* separated into a stand alone routine to other protocol dissectors */
84 /* can call to it, ie. socks */
87 decode_udp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
88 proto_tree *tree, int uh_sport, int uh_dport)
92 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
94 /* determine if this packet is part of a conversation and call dissector */
95 /* for the conversation if available */
97 if (try_conversation_dissector(&pinfo->src, &pinfo->dst, PT_UDP,
98 uh_sport, uh_dport, next_tvb, pinfo, tree))
101 /* do lookup with the subdissector table */
102 if (dissector_try_port(udp_dissector_table, uh_sport, next_tvb, pinfo, tree) ||
103 dissector_try_port(udp_dissector_table, uh_dport, next_tvb, pinfo, tree))
106 /* do lookup with the heuristic subdissector table */
107 if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
110 call_dissector(data_handle,next_tvb, pinfo, tree);
115 dissect_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
118 guint16 uh_sport, uh_dport, uh_ulen, uh_sum;
119 proto_tree *udp_tree;
125 guint16 computed_cksum;
128 if (check_col(pinfo->cinfo, COL_PROTOCOL))
129 col_set_str(pinfo->cinfo, COL_PROTOCOL, "UDP");
130 if (check_col(pinfo->cinfo, COL_INFO))
131 col_clear(pinfo->cinfo, COL_INFO);
133 /* Avoids alignment problems on many architectures. */
134 tvb_memcpy(tvb, (guint8 *)&uh, offset, sizeof(e_udphdr));
135 uh_sport = ntohs(uh.uh_sport);
136 uh_dport = ntohs(uh.uh_dport);
137 uh_ulen = ntohs(uh.uh_ulen);
138 uh_sum = ntohs(uh.uh_sum);
140 if (check_col(pinfo->cinfo, COL_INFO))
141 col_add_fstr(pinfo->cinfo, COL_INFO, "Source port: %s Destination port: %s",
142 get_udp_port(uh_sport), get_udp_port(uh_dport));
145 if (udp_summary_in_tree) {
146 ti = proto_tree_add_protocol_format(tree, proto_udp, tvb, offset, 8,
147 "User Datagram Protocol, Src Port: %s (%u), Dst Port: %s (%u)",
148 get_udp_port(uh_sport), uh_sport, get_udp_port(uh_dport), uh_dport);
150 ti = proto_tree_add_item(tree, proto_udp, tvb, offset, 8, FALSE);
152 udp_tree = proto_item_add_subtree(ti, ett_udp);
154 proto_tree_add_uint_format(udp_tree, hf_udp_srcport, tvb, offset, 2, uh_sport,
155 "Source port: %s (%u)", get_udp_port(uh_sport), uh_sport);
156 proto_tree_add_uint_format(udp_tree, hf_udp_dstport, tvb, offset + 2, 2, uh_dport,
157 "Destination port: %s (%u)", get_udp_port(uh_dport), uh_dport);
159 proto_tree_add_uint_hidden(udp_tree, hf_udp_port, tvb, offset, 2, uh_sport);
160 proto_tree_add_uint_hidden(udp_tree, hf_udp_port, tvb, offset+2, 2, uh_dport);
162 proto_tree_add_uint(udp_tree, hf_udp_length, tvb, offset + 4, 2, uh_ulen);
163 reported_len = tvb_reported_length(tvb);
164 len = tvb_length(tvb);
166 /* No checksum supplied in the packet. */
167 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
168 offset + 6, 2, uh_sum, "Checksum: 0x%04x (none)", uh_sum);
169 } else if (!pinfo->fragmented && len >= reported_len && len >= uh_ulen) {
170 /* The packet isn't part of a fragmented datagram and isn't
171 truncated, so we can checksum it.
172 XXX - make a bigger scatter-gather list once we do fragment
175 /* Set up the fields of the pseudo-header. */
176 cksum_vec[0].ptr = pinfo->src.data;
177 cksum_vec[0].len = pinfo->src.len;
178 cksum_vec[1].ptr = pinfo->dst.data;
179 cksum_vec[1].len = pinfo->dst.len;
180 cksum_vec[2].ptr = (const guint8 *)&phdr;
181 switch (pinfo->src.type) {
184 phdr[0] = htonl((IP_PROTO_UDP<<16) + reported_len);
185 cksum_vec[2].len = 4;
189 phdr[0] = htonl(reported_len);
190 phdr[1] = htonl(IP_PROTO_UDP);
191 cksum_vec[2].len = 8;
195 /* TCP runs only atop IPv4 and IPv6.... */
196 g_assert_not_reached();
199 cksum_vec[3].ptr = tvb_get_ptr(tvb, offset, len);
200 cksum_vec[3].len = reported_len;
201 computed_cksum = in_cksum(&cksum_vec[0], 4);
202 if (computed_cksum == 0) {
203 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
204 offset + 6, 2, uh_sum, "Checksum: 0x%04x (correct)", uh_sum);
206 proto_tree_add_boolean_hidden(udp_tree, hf_udp_checksum_bad, tvb,
207 offset + 6, 2, TRUE);
208 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
209 offset + 6, 2, uh_sum,
210 "Checksum: 0x%04x (incorrect, should be 0x%04x)", uh_sum,
211 in_cksum_shouldbe(uh_sum, computed_cksum));
214 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
215 offset + 6, 2, uh_sum, "Checksum: 0x%04x", uh_sum);
219 /* Skip over header */
222 pinfo->ptype = PT_UDP;
223 pinfo->srcport = uh_sport;
224 pinfo->destport = uh_dport;
226 /* call sub-dissectors */
227 decode_udp_ports( tvb, offset, pinfo, tree, uh_sport, uh_dport);
232 proto_register_udp(void)
234 module_t *udp_module;
235 static hf_register_info hf[] = {
237 { "Source Port", "udp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
241 { "Destination Port", "udp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
245 { "Source or Destination Port", "udp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
249 { "Length", "udp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
252 { &hf_udp_checksum_bad,
253 { "Bad Checksum", "udp.checksum_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
257 { "Checksum", "udp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
260 static gint *ett[] = {
264 proto_udp = proto_register_protocol("User Datagram Protocol",
266 proto_register_field_array(proto_udp, hf, array_length(hf));
267 proto_register_subtree_array(ett, array_length(ett));
269 /* subdissector code */
270 udp_dissector_table = register_dissector_table("udp.port",
271 "UDP port", FT_UINT16, BASE_DEC);
272 register_heur_dissector_list("udp", &heur_subdissector_list);
274 /* Register configuration preferences */
275 udp_module = prefs_register_protocol(proto_udp, NULL);
276 prefs_register_bool_preference(udp_module, "udp_summary_in_tree",
277 "Show UDP summary in protocol tree",
278 "Whether the UDP summary line should be shown in the protocol tree",
279 &udp_summary_in_tree);
283 proto_reg_handoff_udp(void)
285 dissector_handle_t udp_handle;
287 udp_handle = create_dissector_handle(dissect_udp, proto_udp);
288 dissector_add("ip.proto", IP_PROTO_UDP, udp_handle);
289 data_handle = find_dissector("data");