2 * Routines for UDP packet disassembly
4 * $Id: packet-udp.c,v 1.82 2001/01/06 08:44:03 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
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>
50 #include "packet-udp.h"
52 #include "packet-ip.h"
53 #include "conversation.h"
54 #include "packet-vines.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;
63 static gint ett_udp = -1;
65 /* UDP structs and definitions */
67 typedef struct _e_udphdr {
74 static dissector_table_t udp_dissector_table;
75 static heur_dissector_list_t heur_subdissector_list;
77 /* Determine if there is a sub-dissector and call it. This has been */
78 /* separated into a stand alone routine to other protocol dissectors */
79 /* can call to it, ie. socks */
82 decode_udp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
83 proto_tree *tree, int uh_sport, int uh_dport)
86 const u_char *next_pd;
89 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
91 /* determine if this packet is part of a conversation and call dissector */
92 /* for the conversation if available */
94 if (try_conversation_dissector(&pinfo->src, &pinfo->dst, PT_UDP,
95 uh_sport, uh_dport, next_tvb, pinfo, tree))
98 /* try to apply the plugins */
101 plugin *pt_plug = plugin_list;
103 if (enabled_plugins_number > 0) {
104 tvb_compat(next_tvb, &next_pd, &next_offset);
106 if (pt_plug->enabled && strstr(pt_plug->protocol, "udp") &&
107 tree && dfilter_apply(pt_plug->filter, tree, next_pd, pinfo->fd->cap_len)) {
108 pt_plug->dissector(next_pd, next_offset, pinfo->fd, tree);
111 pt_plug = pt_plug->next;
117 /* do lookup with the subdissector table */
118 if (dissector_try_port(udp_dissector_table, uh_sport, next_tvb, pinfo, tree) ||
119 dissector_try_port(udp_dissector_table, uh_dport, next_tvb, pinfo, tree))
122 /* do lookup with the heuristic subdissector table */
123 if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
126 dissect_data(next_tvb, 0, pinfo, tree);
131 dissect_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
134 guint16 uh_sport, uh_dport, uh_ulen, uh_sum;
135 proto_tree *udp_tree;
141 guint16 computed_cksum;
144 CHECK_DISPLAY_AS_DATA(proto_udp, tvb, pinfo, tree);
146 pinfo->current_proto = "UDP";
148 /* Avoids alignment problems on many architectures. */
149 tvb_memcpy(tvb, (guint8 *)&uh, offset, sizeof(e_udphdr));
150 uh_sport = ntohs(uh.uh_sport);
151 uh_dport = ntohs(uh.uh_dport);
152 uh_ulen = ntohs(uh.uh_ulen);
153 uh_sum = ntohs(uh.uh_sum);
155 if (check_col(pinfo->fd, COL_PROTOCOL))
156 col_set_str(pinfo->fd, COL_PROTOCOL, "UDP");
157 if (check_col(pinfo->fd, COL_INFO))
158 col_add_fstr(pinfo->fd, COL_INFO, "Source port: %s Destination port: %s",
159 get_udp_port(uh_sport), get_udp_port(uh_dport));
162 ti = proto_tree_add_item(tree, proto_udp, tvb, offset, 8, FALSE);
163 udp_tree = proto_item_add_subtree(ti, ett_udp);
165 proto_tree_add_uint_format(udp_tree, hf_udp_srcport, tvb, offset, 2, uh_sport,
166 "Source port: %s (%u)", get_udp_port(uh_sport), uh_sport);
167 proto_tree_add_uint_format(udp_tree, hf_udp_dstport, tvb, offset + 2, 2, uh_dport,
168 "Destination port: %s (%u)", get_udp_port(uh_dport), uh_dport);
170 proto_tree_add_uint_hidden(udp_tree, hf_udp_port, tvb, offset, 2, uh_sport);
171 proto_tree_add_uint_hidden(udp_tree, hf_udp_port, tvb, offset+2, 2, uh_dport);
173 proto_tree_add_uint(udp_tree, hf_udp_length, tvb, offset + 4, 2, uh_ulen);
174 reported_len = tvb_reported_length(tvb);
175 len = tvb_length(tvb);
177 /* No checksum supplied in the packet. */
178 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
179 offset + 6, 2, uh_sum, "Checksum: 0x%04x (none)", uh_sum);
180 } else if (!pinfo->fragmented && len >= reported_len) {
181 /* The packet isn't part of a fragmented datagram and isn't
182 truncated, so we can checksum it.
183 XXX - make a bigger scatter-gather list once we do fragment
186 /* Set up the fields of the pseudo-header. */
187 cksum_vec[0].ptr = pinfo->src.data;
188 cksum_vec[0].len = pinfo->src.len;
189 cksum_vec[1].ptr = pinfo->dst.data;
190 cksum_vec[1].len = pinfo->dst.len;
191 cksum_vec[2].ptr = (const guint8 *)&phdr;
192 switch (pinfo->src.type) {
195 phdr[0] = htonl((IP_PROTO_UDP<<16) + reported_len);
196 cksum_vec[2].len = 4;
200 phdr[0] = htonl(reported_len);
201 phdr[1] = htonl(IP_PROTO_UDP);
202 cksum_vec[2].len = 8;
206 /* TCP runs only atop IPv4 and IPv6.... */
207 g_assert_not_reached();
210 cksum_vec[3].ptr = tvb_get_ptr(tvb, offset, len);
211 cksum_vec[3].len = reported_len;
212 computed_cksum = in_cksum(&cksum_vec[0], 4);
213 if (computed_cksum == 0) {
214 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
215 offset + 6, 2, uh_sum, "Checksum: 0x%04x (correct)", uh_sum);
217 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
218 offset + 6, 2, uh_sum,
219 "Checksum: 0x%04x (incorrect, should be 0x%04x)", uh_sum,
220 in_cksum_shouldbe(uh_sum, computed_cksum));
223 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
224 offset + 6, 2, uh_sum, "Checksum: 0x%04x", uh_sum);
228 /* Skip over header */
231 pinfo->ptype = PT_UDP;
232 pinfo->srcport = uh_sport;
233 pinfo->destport = uh_dport;
235 /* call sub-dissectors */
236 decode_udp_ports( tvb, offset, pinfo, tree, uh_sport, uh_dport);
241 proto_register_udp(void)
243 static hf_register_info hf[] = {
245 { "Source Port", "udp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
249 { "Destination Port", "udp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
253 { "Source or Destination Port", "udp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
257 { "Length", "udp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
261 { "Checksum", "udp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
264 static gint *ett[] = {
268 proto_udp = proto_register_protocol("User Datagram Protocol",
270 proto_register_field_array(proto_udp, hf, array_length(hf));
271 proto_register_subtree_array(ett, array_length(ett));
273 /* subdissector code */
274 udp_dissector_table = register_dissector_table("udp.port");
275 register_heur_dissector_list("udp", &heur_subdissector_list);
279 proto_reg_handoff_udp(void)
281 dissector_add("ip.proto", IP_PROTO_UDP, dissect_udp);