2 * Routines for UDP packet disassembly
4 * $Id: packet-udp.c,v 1.60 2000/04/12 22:53:16 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-ncp.h"
50 #include "packet-rip.h"
51 #include "packet-rpc.h"
52 #include "packet-rx.h"
53 #include "packet-tftp.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_TFTP 69
77 #define UDP_PORT_RIP 520
78 #define UDP_PORT_NCP 524
79 #define UDP_PORT_VINES 573
80 #define UDP_PORT_RX_LOW 7000
81 #define UDP_PORT_RX_HIGH 7009
82 #define UDP_PORT_RX_AFS_BACKUPS 7021
84 static dissector_table_t udp_dissector_table;
88 decode_udp_ports( const u_char *pd, int offset, frame_data *fd,
89 proto_tree *tree, int uh_sport, int uh_dport){
91 /* Determine if there is a sub-dissector and call it. This has been */
92 /* separated into a stand alone routine to other protocol dissectors */
93 /* can call to it, ie. socks */
95 dissector_t sub_dissector;
98 /* determine if this packet is part of a conversation and call dissector */
99 /* for the conversation if available */
101 sub_dissector = find_conversation_dissector( &pi.src, &pi.dst, PT_UDP,
104 (sub_dissector)(pd, offset, fd, tree);
108 /* ONC RPC. We can't base this on anything in the UDP header; we have
109 to look at the payload. If "dissect_rpc()" returns TRUE, it was
110 an RPC packet, otherwise it's some other type of packet. */
111 if (dissect_rpc(pd, offset, fd, tree))
114 /* try to apply the plugins */
117 plugin *pt_plug = plugin_list;
119 if (enabled_plugins_number > 0) {
121 if (pt_plug->enabled && !strcmp(pt_plug->protocol, "udp") &&
122 tree && dfilter_apply(pt_plug->filter, tree, pd)) {
123 pt_plug->dissector(pd, offset, fd, tree);
126 pt_plug = pt_plug->next;
132 /* XXX - we should do all of this through the table of ports. */
133 #define PORT_IS(port) (uh_sport == port || uh_dport == port)
134 if (PORT_IS(UDP_PORT_RIP)) {
135 /* we should check the source port too (RIP: UDP src and dst port 520) */
136 dissect_rip(pd, offset, fd, tree);
137 } else if (PORT_IS(UDP_PORT_NCP))
138 dissect_ncp(pd, offset, fd, tree); /* XXX -- need to handle nw_server_address */
139 else if ((uh_sport >= UDP_PORT_RX_LOW && uh_sport <= UDP_PORT_RX_HIGH) ||
140 (uh_dport >= UDP_PORT_RX_LOW && uh_dport <= UDP_PORT_RX_HIGH) ||
141 PORT_IS(UDP_PORT_RX_AFS_BACKUPS))
142 dissect_rx(pd, offset, fd, tree); /* transarc AFS's RX protocol */
143 else if (PORT_IS(UDP_PORT_VINES)) {
144 /* FIXME: AFAIK, src and dst port must be the same */
145 dissect_vines_frp(pd, offset, fd, tree);
146 } else if (PORT_IS(UDP_PORT_TFTP)) {
147 /* This is the first point of call, but it adds a dynamic call */
148 dissector_add("udp.port", MAX(uh_sport, uh_dport), dissect_tftp); /* Add to table */
149 dissect_tftp(pd, offset, fd, tree);
151 /* OK, find a routine in the table, else use the default */
153 if (!dissector_try_port(udp_dissector_table, uh_sport, pd, offset,
155 !dissector_try_port(udp_dissector_table, uh_dport, pd, offset,
157 dissect_data(pd, offset, fd, tree);
163 dissect_udp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
165 guint16 uh_sport, uh_dport, uh_ulen, uh_sum;
166 proto_tree *udp_tree;
169 if (!BYTES_ARE_IN_FRAME(offset, sizeof(e_udphdr))) {
170 dissect_data(pd, offset, fd, tree);
174 /* Avoids alignment problems on many architectures. */
175 memcpy(&uh, &pd[offset], sizeof(e_udphdr));
176 uh_sport = ntohs(uh.uh_sport);
177 uh_dport = ntohs(uh.uh_dport);
178 uh_ulen = ntohs(uh.uh_ulen);
179 uh_sum = ntohs(uh.uh_sum);
181 if (check_col(fd, COL_PROTOCOL))
182 col_add_str(fd, COL_PROTOCOL, "UDP");
183 if (check_col(fd, COL_INFO))
184 col_add_fstr(fd, COL_INFO, "Source port: %s Destination port: %s",
185 get_udp_port(uh_sport), get_udp_port(uh_dport));
188 ti = proto_tree_add_item(tree, proto_udp, offset, 8);
189 udp_tree = proto_item_add_subtree(ti, ett_udp);
191 proto_tree_add_uint_format(udp_tree, hf_udp_srcport, offset, 2, uh_sport,
192 "Source port: %s (%u)", get_udp_port(uh_sport), uh_sport);
193 proto_tree_add_uint_format(udp_tree, hf_udp_dstport, offset + 2, 2, uh_dport,
194 "Destination port: %s (%u)", get_udp_port(uh_dport), uh_dport);
196 proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset, 2, uh_sport);
197 proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset+2, 2, uh_dport);
199 proto_tree_add_item(udp_tree, hf_udp_length, offset + 4, 2, uh_ulen);
200 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, offset + 6, 2, uh_sum,
201 "Checksum: 0x%04x", uh_sum);
204 /* Skip over header */
208 pi.srcport = uh_sport;
209 pi.destport = uh_dport;
211 /* call sub-dissectors */
212 decode_udp_ports( pd, offset, fd, tree, uh_sport, uh_dport);
217 proto_register_udp(void)
219 static hf_register_info hf[] = {
221 { "Source Port", "udp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
225 { "Destination Port", "udp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
229 { "Source or Destination Port", "udp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
233 { "Length", "udp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
237 { "Checksum", "udp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
240 static gint *ett[] = {
244 proto_udp = proto_register_protocol("User Datagram Protocol", "udp");
245 proto_register_field_array(proto_udp, hf, array_length(hf));
246 proto_register_subtree_array(ett, array_length(ett));
248 /* subdissector code */
249 udp_dissector_table = register_dissector_table(hf_udp_port);