2 * Routines for UDP packet disassembly
4 * $Id: packet-udp.c,v 1.57 2000/04/04 06:17:29 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-auto_rp.h"
50 #include "packet-bootp.h"
51 #include "packet-dhis.h"
52 #include "packet-hsrp.h"
53 #include "packet-icp.h"
54 #include "packet-icq.h"
55 #include "packet-ipx.h"
56 #include "packet-isakmp.h"
57 #include "packet-l2tp.h"
58 #include "packet-nbns.h"
59 #include "packet-ncp.h"
60 #include "packet-ntp.h"
61 #include "packet-radius.h"
62 #include "packet-rip.h"
63 #include "packet-ripng.h"
64 #include "packet-rpc.h"
65 #include "packet-rx.h"
66 #include "packet-sap.h"
67 #include "packet-snmp.h"
68 #include "packet-srvloc.h"
69 #include "packet-tacacs.h"
70 #include "packet-tftp.h"
71 #include "packet-time.h"
72 #include "packet-vines.h"
73 #include "packet-wccp.h"
74 #include "packet-who.h"
76 static int proto_udp = -1;
77 static int hf_udp_srcport = -1;
78 static int hf_udp_dstport = -1;
79 static int hf_udp_port = -1;
80 static int hf_udp_length = -1;
81 static int hf_udp_checksum = -1;
83 static gint ett_udp = -1;
85 /* UDP structs and definitions */
87 typedef struct _e_udphdr {
94 /* UDP Ports -> should go in packet-udp.h */
96 #define UDP_PORT_TIME 37
97 #define UDP_PORT_TACACS 49
98 #define UDP_PORT_BOOTPS 67
99 #define UDP_PORT_TFTP 69
100 #define UDP_PORT_NTP 123
101 #define UDP_PORT_NBNS 137
102 #define UDP_PORT_NBDGM 138
103 #define UDP_PORT_SNMP 161
104 #define UDP_PORT_SNMP_TRAP 162
105 #define UDP_PORT_IPX 213 /* RFC 1234 */
106 #define UDP_PORT_SRVLOC 427
107 #define UDP_PORT_PIM_RP_DISC 496
108 #define UDP_PORT_ISAKMP 500
109 #define UDP_PORT_WHO 513
110 #define UDP_PORT_RIP 520
111 #define UDP_PORT_RIPNG 521
112 #define UDP_PORT_NCP 524
113 #define UDP_PORT_VINES 573
114 #define UDP_PORT_RADIUS 1645
115 #define UDP_PORT_L2TP 1701
116 #define UDP_PORT_RADIUS_NEW 1812
117 #define UDP_PORT_RADACCT 1646
118 #define UDP_PORT_RADACCT_NEW 1813
119 #define UDP_PORT_HSRP 1985
120 #define UDP_PORT_ICP 3130
121 #define UDP_PORT_ICQ 4000
122 #define UDP_PORT_SAP 9875
123 #define UDP_PORT_RX_LOW 7000
124 #define UDP_PORT_RX_HIGH 7009
125 #define UDP_PORT_RX_AFS_BACKUPS 7021
126 #define UDP_PORT_WCCP 2048
127 #define UDP_PORT_DHIS1 58800
128 #define UDP_PORT_DHIS2 58801
130 static dissector_table_t udp_dissector_table;
133 dissect_udp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
135 guint16 uh_sport, uh_dport, uh_ulen, uh_sum;
136 proto_tree *udp_tree;
139 if (!BYTES_ARE_IN_FRAME(offset, sizeof(e_udphdr))) {
140 dissect_data(pd, offset, fd, tree);
144 /* Avoids alignment problems on many architectures. */
145 memcpy(&uh, &pd[offset], sizeof(e_udphdr));
146 uh_sport = ntohs(uh.uh_sport);
147 uh_dport = ntohs(uh.uh_dport);
148 uh_ulen = ntohs(uh.uh_ulen);
149 uh_sum = ntohs(uh.uh_sum);
151 if (check_col(fd, COL_PROTOCOL))
152 col_add_str(fd, COL_PROTOCOL, "UDP");
153 if (check_col(fd, COL_INFO))
154 col_add_fstr(fd, COL_INFO, "Source port: %s Destination port: %s",
155 get_udp_port(uh_sport), get_udp_port(uh_dport));
158 ti = proto_tree_add_item(tree, proto_udp, offset, 8);
159 udp_tree = proto_item_add_subtree(ti, ett_udp);
161 proto_tree_add_uint_format(udp_tree, hf_udp_srcport, offset, 2, uh_sport,
162 "Source port: %s (%u)", get_udp_port(uh_sport), uh_sport);
163 proto_tree_add_uint_format(udp_tree, hf_udp_dstport, offset + 2, 2, uh_dport,
164 "Destination port: %s (%u)", get_udp_port(uh_dport), uh_dport);
166 proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset, 2, uh_sport);
167 proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset+2, 2, uh_dport);
169 proto_tree_add_item(udp_tree, hf_udp_length, offset + 4, 2, uh_ulen);
170 proto_tree_add_uint_format(udp_tree, hf_udp_checksum, offset + 6, 2, uh_sum,
171 "Checksum: 0x%04x", uh_sum);
174 /* Skip over header */
178 pi.srcport = uh_sport;
179 pi.destport = uh_dport;
181 /* ONC RPC. We can't base this on anything in the UDP header; we have
182 to look at the payload. If "dissect_rpc()" returns TRUE, it was
183 an RPC packet, otherwise it's some other type of packet. */
184 if (dissect_rpc(pd, offset, fd, tree))
187 /* try to apply the plugins */
190 plugin *pt_plug = plugin_list;
194 if (pt_plug->enabled && !strcmp(pt_plug->protocol, "udp") &&
195 tree && dfilter_apply(pt_plug->filter, tree, pd)) {
196 pt_plug->dissector(pd, offset, fd, tree);
199 pt_plug = pt_plug->next;
205 /* XXX - we should do all of this through the table of ports. */
206 #define PORT_IS(port) (uh_sport == port || uh_dport == port)
207 if (PORT_IS(UDP_PORT_RIP)) {
208 /* we should check the source port too (RIP: UDP src and dst port 520) */
209 dissect_rip(pd, offset, fd, tree);
210 } else if (PORT_IS(UDP_PORT_NCP))
211 dissect_ncp(pd, offset, fd, tree); /* XXX -- need to handle nw_server_address */
212 else if ((uh_sport >= UDP_PORT_RX_LOW && uh_sport <= UDP_PORT_RX_HIGH) ||
213 (uh_dport >= UDP_PORT_RX_LOW && uh_dport <= UDP_PORT_RX_HIGH) ||
214 PORT_IS(UDP_PORT_RX_AFS_BACKUPS))
215 dissect_rx(pd, offset, fd, tree); /* transarc AFS's RX protocol */
216 else if (PORT_IS(UDP_PORT_VINES)) {
217 /* FIXME: AFAIK, src and dst port must be the same */
218 dissect_vines_frp(pd, offset, fd, tree);
219 } else if (PORT_IS(UDP_PORT_TFTP)) {
220 /* This is the first point of call, but it adds a dynamic call */
221 dissector_add("udp.port", MAX(uh_sport, uh_dport), dissect_tftp); /* Add to table */
222 dissect_tftp(pd, offset, fd, tree);
224 /* OK, find a routine in the table, else use the default */
226 if (!dissector_try_port(udp_dissector_table, uh_sport, pd, offset,
228 !dissector_try_port(udp_dissector_table, uh_dport, pd, offset,
230 dissect_data(pd, offset, fd, tree);
235 proto_register_udp(void)
237 static hf_register_info hf[] = {
239 { "Source Port", "udp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
243 { "Destination Port", "udp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
247 { "Source or Destination Port", "udp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
251 { "Length", "udp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
255 { "Checksum", "udp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
258 static gint *ett[] = {
262 proto_udp = proto_register_protocol("User Datagram Protocol", "udp");
263 proto_register_field_array(proto_udp, hf, array_length(hf));
264 proto_register_subtree_array(ett, array_length(ett));
266 /* subdissector code */
267 udp_dissector_table = register_dissector_table(hf_udp_port);
269 /* Now add the protocols we know about.
270 XXX - we should add all the UDP ports this way, rather
271 than having the giant "if", just as is now done in
274 dissector_add("udp.port", UDP_PORT_TIME, dissect_time);
275 dissector_add("udp.port", UDP_PORT_TACACS, dissect_tacacs);
276 dissector_add("udp.port", UDP_PORT_BOOTPS, dissect_bootp);
277 dissector_add("udp.port", UDP_PORT_NTP, dissect_ntp);
278 dissector_add("udp.port", UDP_PORT_NBNS, dissect_nbns);
279 dissector_add("udp.port", UDP_PORT_NBDGM, dissect_nbdgm);
280 dissector_add("udp.port", UDP_PORT_SNMP, dissect_snmp);
281 dissector_add("udp.port", UDP_PORT_SNMP_TRAP, dissect_snmp);
282 dissector_add("udp.port", UDP_PORT_IPX, dissect_ipx);
283 dissector_add("udp.port", UDP_PORT_SRVLOC, dissect_srvloc);
284 dissector_add("udp.port", UDP_PORT_PIM_RP_DISC, dissect_auto_rp);
285 dissector_add("udp.port", UDP_PORT_ISAKMP, dissect_isakmp);
286 dissector_add("udp.port", UDP_PORT_WHO, dissect_who);
287 dissector_add("udp.port", UDP_PORT_RIPNG, dissect_ripng);
288 dissector_add("udp.port", UDP_PORT_RADIUS, dissect_radius);
289 dissector_add("udp.port", UDP_PORT_L2TP, dissect_l2tp);
290 dissector_add("udp.port", UDP_PORT_RADIUS_NEW, dissect_radius);
291 dissector_add("udp.port", UDP_PORT_RADACCT, dissect_radius);
292 dissector_add("udp.port", UDP_PORT_RADACCT_NEW, dissect_radius);
293 dissector_add("udp.port", UDP_PORT_HSRP, dissect_hsrp);
294 dissector_add("udp.port", UDP_PORT_ICP, dissect_icp);
295 dissector_add("udp.port", UDP_PORT_ICQ, dissect_icq);
296 dissector_add("udp.port", UDP_PORT_SAP, dissect_sap);
297 dissector_add("udp.port", UDP_PORT_WCCP, dissect_wccp);
298 dissector_add("udp.port", UDP_PORT_DHIS1, dissect_dhis);
299 dissector_add("udp.port", UDP_PORT_DHIS2, dissect_dhis);