2 * Routines for UDP packet disassembly
4 * $Id: packet-udp.c,v 1.89 2001/03/28 21:33:31 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>
49 #include "packet-udp.h"
51 #include "packet-ip.h"
52 #include "conversation.h"
53 #include "packet-vines.h"
55 static int proto_udp = -1;
56 static int hf_udp_srcport = -1;
57 static int hf_udp_dstport = -1;
58 static int hf_udp_port = -1;
59 static int hf_udp_length = -1;
60 static int hf_udp_checksum = -1;
61 static int hf_udp_checksum_bad = -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;
76 static conv_dissector_list_t conv_subdissector_list;
78 /* Determine if there is a sub-dissector and call it. This has been */
79 /* separated into a stand alone routine to other protocol dissectors */
80 /* can call to it, ie. socks */
83 decode_udp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
84 proto_tree *tree, int uh_sport, int uh_dport)
88 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
90 /* determine if this packet is part of a conversation and call dissector */
91 /* for the conversation if available */
93 if (try_conversation_dissector(&pinfo->src, &pinfo->dst, PT_UDP,
94 uh_sport, uh_dport, next_tvb, pinfo, tree))
97 /* do lookup with the subdissector table */
98 if (dissector_try_port(udp_dissector_table, uh_sport, next_tvb, pinfo, tree) ||
99 dissector_try_port(udp_dissector_table, uh_dport, next_tvb, pinfo, tree))
102 /* do lookup with the heuristic subdissector table */
103 if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
106 dissect_data(next_tvb, 0, pinfo, tree);
111 dissect_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
114 guint16 uh_sport, uh_dport, uh_ulen, uh_sum;
115 proto_tree *udp_tree;
121 guint16 computed_cksum;
124 if (check_col(pinfo->fd, COL_PROTOCOL))
125 col_set_str(pinfo->fd, COL_PROTOCOL, "UDP");
126 if (check_col(pinfo->fd, COL_INFO))
127 col_clear(pinfo->fd, COL_INFO);
129 /* Avoids alignment problems on many architectures. */
130 tvb_memcpy(tvb, (guint8 *)&uh, offset, sizeof(e_udphdr));
131 uh_sport = ntohs(uh.uh_sport);
132 uh_dport = ntohs(uh.uh_dport);
133 uh_ulen = ntohs(uh.uh_ulen);
134 uh_sum = ntohs(uh.uh_sum);
136 if (check_col(pinfo->fd, COL_INFO))
137 col_add_fstr(pinfo->fd, COL_INFO, "Source port: %s Destination port: %s",
138 get_udp_port(uh_sport), get_udp_port(uh_dport));
141 ti = proto_tree_add_item(tree, proto_udp, tvb, offset, 8, FALSE);
142 udp_tree = proto_item_add_subtree(ti, ett_udp);
144 proto_tree_add_uint_format(udp_tree, hf_udp_srcport, tvb, offset, 2, uh_sport,
145 "Source port: %s (%u)", get_udp_port(uh_sport), uh_sport);
146 proto_tree_add_uint_format(udp_tree, hf_udp_dstport, tvb, offset + 2, 2, uh_dport,
147 "Destination port: %s (%u)", get_udp_port(uh_dport), uh_dport);
149 proto_tree_add_uint_hidden(udp_tree, hf_udp_port, tvb, offset, 2, uh_sport);
150 proto_tree_add_uint_hidden(udp_tree, hf_udp_port, tvb, offset+2, 2, uh_dport);
152 proto_tree_add_uint(udp_tree, hf_udp_length, tvb, offset + 4, 2, uh_ulen);
153 reported_len = tvb_reported_length(tvb);
154 len = tvb_length(tvb);
156 /* No checksum supplied in the packet. */
157 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
158 offset + 6, 2, uh_sum, "Checksum: 0x%04x (none)", uh_sum);
159 } else if (!pinfo->fragmented && len >= reported_len) {
160 /* The packet isn't part of a fragmented datagram and isn't
161 truncated, so we can checksum it.
162 XXX - make a bigger scatter-gather list once we do fragment
165 /* Set up the fields of the pseudo-header. */
166 cksum_vec[0].ptr = pinfo->src.data;
167 cksum_vec[0].len = pinfo->src.len;
168 cksum_vec[1].ptr = pinfo->dst.data;
169 cksum_vec[1].len = pinfo->dst.len;
170 cksum_vec[2].ptr = (const guint8 *)&phdr;
171 switch (pinfo->src.type) {
174 phdr[0] = htonl((IP_PROTO_UDP<<16) + reported_len);
175 cksum_vec[2].len = 4;
179 phdr[0] = htonl(reported_len);
180 phdr[1] = htonl(IP_PROTO_UDP);
181 cksum_vec[2].len = 8;
185 /* TCP runs only atop IPv4 and IPv6.... */
186 g_assert_not_reached();
189 cksum_vec[3].ptr = tvb_get_ptr(tvb, offset, len);
190 cksum_vec[3].len = reported_len;
191 computed_cksum = in_cksum(&cksum_vec[0], 4);
192 if (computed_cksum == 0) {
193 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
194 offset + 6, 2, uh_sum, "Checksum: 0x%04x (correct)", uh_sum);
196 proto_tree_add_boolean_hidden(udp_tree, hf_udp_checksum_bad, tvb,
197 offset + 6, 2, TRUE);
198 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
199 offset + 6, 2, uh_sum,
200 "Checksum: 0x%04x (incorrect, should be 0x%04x)", uh_sum,
201 in_cksum_shouldbe(uh_sum, computed_cksum));
204 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
205 offset + 6, 2, uh_sum, "Checksum: 0x%04x", uh_sum);
209 /* Skip over header */
212 pinfo->ptype = PT_UDP;
213 pinfo->srcport = uh_sport;
214 pinfo->destport = uh_dport;
216 /* call sub-dissectors */
217 decode_udp_ports( tvb, offset, pinfo, tree, uh_sport, uh_dport);
222 proto_register_udp(void)
224 static hf_register_info hf[] = {
226 { "Source Port", "udp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
230 { "Destination Port", "udp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
234 { "Source or Destination Port", "udp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
238 { "Length", "udp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
241 { &hf_udp_checksum_bad,
242 { "Bad Checksum", "udp.checksum_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
246 { "Checksum", "udp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
249 static gint *ett[] = {
253 proto_udp = proto_register_protocol("User Datagram Protocol",
255 proto_register_field_array(proto_udp, hf, array_length(hf));
256 proto_register_subtree_array(ett, array_length(ett));
258 /* subdissector code */
259 udp_dissector_table = register_dissector_table("udp.port");
260 register_heur_dissector_list("udp", &heur_subdissector_list);
261 register_conv_dissector_list("udp", &conv_subdissector_list);
265 proto_reg_handoff_udp(void)
267 dissector_add("ip.proto", IP_PROTO_UDP, dissect_udp, proto_udp);