2 * Routines for UDP packet disassembly
4 * $Id: packet-udp.c,v 1.81 2001/01/03 06:55:34 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 /* UDP Ports -> should go in packet-udp.h */
76 #define UDP_PORT_VINES 573
78 static dissector_table_t udp_dissector_table;
79 static heur_dissector_list_t heur_subdissector_list;
81 /* Determine if there is a sub-dissector and call it. This has been */
82 /* separated into a stand alone routine to other protocol dissectors */
83 /* can call to it, ie. socks */
86 decode_udp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
87 proto_tree *tree, int uh_sport, int uh_dport)
90 const u_char *next_pd;
93 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
95 /* determine if this packet is part of a conversation and call dissector */
96 /* for the conversation if available */
98 if (try_conversation_dissector(&pinfo->src, &pinfo->dst, PT_UDP,
99 uh_sport, uh_dport, next_tvb, pinfo, tree))
102 /* try to apply the plugins */
105 plugin *pt_plug = plugin_list;
107 if (enabled_plugins_number > 0) {
108 tvb_compat(next_tvb, &next_pd, &next_offset);
110 if (pt_plug->enabled && strstr(pt_plug->protocol, "udp") &&
111 tree && dfilter_apply(pt_plug->filter, tree, next_pd, pinfo->fd->cap_len)) {
112 pt_plug->dissector(next_pd, next_offset, pinfo->fd, tree);
115 pt_plug = pt_plug->next;
121 /* do lookup with the subdissector table */
122 if (dissector_try_port(udp_dissector_table, uh_sport, next_tvb, pinfo, tree) ||
123 dissector_try_port(udp_dissector_table, uh_dport, next_tvb, pinfo, tree))
126 /* do lookup with the heuristic subdissector table */
127 if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
130 /* XXX - we should do these with the subdissector table as well. */
131 #define PORT_IS(port) (uh_sport == port || uh_dport == port)
132 if (PORT_IS(UDP_PORT_VINES)) {
133 /* FIXME: AFAIK, src and dst port must be the same */
134 tvb_compat(next_tvb, &next_pd, &next_offset);
135 dissect_vines_frp(next_pd, next_offset, pinfo->fd, tree);
137 dissect_data(next_tvb, 0, pinfo, tree);
142 dissect_udp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
145 guint16 uh_sport, uh_dport, uh_ulen, uh_sum;
146 proto_tree *udp_tree;
152 guint16 computed_cksum;
155 CHECK_DISPLAY_AS_DATA(proto_udp, tvb, pinfo, tree);
157 pinfo->current_proto = "UDP";
159 /* Avoids alignment problems on many architectures. */
160 tvb_memcpy(tvb, (guint8 *)&uh, offset, sizeof(e_udphdr));
161 uh_sport = ntohs(uh.uh_sport);
162 uh_dport = ntohs(uh.uh_dport);
163 uh_ulen = ntohs(uh.uh_ulen);
164 uh_sum = ntohs(uh.uh_sum);
166 if (check_col(pinfo->fd, COL_PROTOCOL))
167 col_set_str(pinfo->fd, COL_PROTOCOL, "UDP");
168 if (check_col(pinfo->fd, COL_INFO))
169 col_add_fstr(pinfo->fd, COL_INFO, "Source port: %s Destination port: %s",
170 get_udp_port(uh_sport), get_udp_port(uh_dport));
173 ti = proto_tree_add_item(tree, proto_udp, tvb, offset, 8, FALSE);
174 udp_tree = proto_item_add_subtree(ti, ett_udp);
176 proto_tree_add_uint_format(udp_tree, hf_udp_srcport, tvb, offset, 2, uh_sport,
177 "Source port: %s (%u)", get_udp_port(uh_sport), uh_sport);
178 proto_tree_add_uint_format(udp_tree, hf_udp_dstport, tvb, offset + 2, 2, uh_dport,
179 "Destination port: %s (%u)", get_udp_port(uh_dport), uh_dport);
181 proto_tree_add_uint_hidden(udp_tree, hf_udp_port, tvb, offset, 2, uh_sport);
182 proto_tree_add_uint_hidden(udp_tree, hf_udp_port, tvb, offset+2, 2, uh_dport);
184 proto_tree_add_uint(udp_tree, hf_udp_length, tvb, offset + 4, 2, uh_ulen);
185 reported_len = tvb_reported_length(tvb);
186 len = tvb_length(tvb);
188 /* No checksum supplied in the packet. */
189 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
190 offset + 6, 2, uh_sum, "Checksum: 0x%04x (none)", uh_sum);
191 } else if (!pinfo->fragmented && len >= reported_len) {
192 /* The packet isn't part of a fragmented datagram and isn't
193 truncated, so we can checksum it.
194 XXX - make a bigger scatter-gather list once we do fragment
197 /* Set up the fields of the pseudo-header. */
198 cksum_vec[0].ptr = pinfo->src.data;
199 cksum_vec[0].len = pinfo->src.len;
200 cksum_vec[1].ptr = pinfo->dst.data;
201 cksum_vec[1].len = pinfo->dst.len;
202 cksum_vec[2].ptr = (const guint8 *)&phdr;
203 switch (pinfo->src.type) {
206 phdr[0] = htonl((IP_PROTO_UDP<<16) + reported_len);
207 cksum_vec[2].len = 4;
211 phdr[0] = htonl(reported_len);
212 phdr[1] = htonl(IP_PROTO_UDP);
213 cksum_vec[2].len = 8;
217 /* TCP runs only atop IPv4 and IPv6.... */
218 g_assert_not_reached();
221 cksum_vec[3].ptr = tvb_get_ptr(tvb, offset, len);
222 cksum_vec[3].len = reported_len;
223 computed_cksum = in_cksum(&cksum_vec[0], 4);
224 if (computed_cksum == 0) {
225 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
226 offset + 6, 2, uh_sum, "Checksum: 0x%04x (correct)", uh_sum);
228 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
229 offset + 6, 2, uh_sum,
230 "Checksum: 0x%04x (incorrect, should be 0x%04x)", uh_sum,
231 in_cksum_shouldbe(uh_sum, computed_cksum));
234 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, tvb,
235 offset + 6, 2, uh_sum, "Checksum: 0x%04x", uh_sum);
239 /* Skip over header */
242 pinfo->ptype = PT_UDP;
243 pinfo->srcport = uh_sport;
244 pinfo->destport = uh_dport;
246 /* call sub-dissectors */
247 decode_udp_ports( tvb, offset, pinfo, tree, uh_sport, uh_dport);
252 proto_register_udp(void)
254 static hf_register_info hf[] = {
256 { "Source Port", "udp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
260 { "Destination Port", "udp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
264 { "Source or Destination Port", "udp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
268 { "Length", "udp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
272 { "Checksum", "udp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
275 static gint *ett[] = {
279 proto_udp = proto_register_protocol("User Datagram Protocol",
281 proto_register_field_array(proto_udp, hf, array_length(hf));
282 proto_register_subtree_array(ett, array_length(ett));
284 /* subdissector code */
285 udp_dissector_table = register_dissector_table("udp.port");
286 register_heur_dissector_list("udp", &heur_subdissector_list);
290 proto_reg_handoff_udp(void)
292 dissector_add("ip.proto", IP_PROTO_UDP, dissect_udp);