2 * Routines for UDP packet disassembly
4 * $Id: packet-udp.c,v 1.50 2000/02/15 21:03:23 gram 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-dns.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_DNS 53
99 #define UDP_PORT_BOOTPS 67
100 #define UDP_PORT_TFTP 69
101 #define UDP_PORT_IPX 213
102 #define UDP_PORT_NTP 123
103 #define UDP_PORT_NBNS 137
104 #define UDP_PORT_NBDGM 138
105 #define UDP_PORT_SNMP 161
106 #define UDP_PORT_SNMP_TRAP 162
107 #define UDP_PORT_SRVLOC 427
108 #define UDP_PORT_PIM_RP_DISC 496
109 #define UDP_PORT_ISAKMP 500
110 #define UDP_PORT_WHO 513
111 #define UDP_PORT_RIP 520
112 #define UDP_PORT_RIPNG 521
113 #define UDP_PORT_NCP 524
114 #define UDP_PORT_VINES 573
115 #define UDP_PORT_RADIUS 1645
116 #define UDP_PORT_L2TP 1701
117 #define UDP_PORT_RADIUS_NEW 1812
118 #define UDP_PORT_RADACCT 1646
119 #define UDP_PORT_RADACCT_NEW 1813
120 #define UDP_PORT_HSRP 1985
121 #define UDP_PORT_ICP 3130
122 #define UDP_PORT_ICQ 4000
123 #define UDP_PORT_SAP 9875
124 #define UDP_PORT_RX_LOW 7000
125 #define UDP_PORT_RX_HIGH 7009
126 #define UDP_PORT_RX_AFS_BACKUPS 7021
127 #define UDP_PORT_WCCP 2048
131 void (*dissect)(const u_char *, int, frame_data *, proto_tree *);
132 struct hash_struct *next;
135 static struct hash_struct *hash_table[256];
138 * These routines are for UDP, will be generalized soon: RJS
140 * XXX - note that they should probably check the IP address as well as
141 * the port number, so that we don't mistakenly identify packets as, say,
142 * TFTP, merely because they have a source or destination port number
143 * equal to the port being used by a TFTP daemon on some machine other
144 * than the one they're going to or from.
147 struct hash_struct *udp_find_hash_ent(guint16 proto) {
149 int idx = proto % 256;
150 struct hash_struct *hash_ent = hash_table[idx];
152 while (hash_ent != NULL) {
154 if (hash_ent -> proto == proto)
157 hash_ent = hash_ent -> next;
165 void udp_hash_add(guint16 proto,
166 void (*dissect)(const u_char *, int, frame_data *, proto_tree *)) {
168 int idx = proto % 256; /* Simply take the remainder, hope for no collisions */
169 struct hash_struct *hash_ent = (struct hash_struct *)malloc(sizeof(struct hash_struct));
170 struct hash_struct *hash_ent2;
172 hash_ent -> proto = proto;
173 hash_ent -> dissect = dissect;
174 hash_ent -> next = NULL;
176 if (hash_ent == NULL) {
178 fprintf(stderr, "Could not allocate space for hash structure in dissect_udp\n");
182 if (hash_table[idx]) { /* Something, add on end */
184 hash_ent2 = hash_table[idx];
186 while (hash_ent2 -> next != NULL)
187 hash_ent2 = hash_ent2 -> next;
189 hash_ent2 -> next = hash_ent; /* Bad in pathalogical cases */
194 hash_table[idx] = hash_ent;
200 void init_dissect_udp(void) {
204 for (i = 0; i < 256; i++) {
206 hash_table[i] = NULL;
210 /* Now add the protocols we know about */
212 udp_hash_add(UDP_PORT_BOOTPS, dissect_bootp);
213 udp_hash_add(UDP_PORT_TFTP, dissect_tftp);
214 udp_hash_add(UDP_PORT_SAP, dissect_sap);
215 udp_hash_add(UDP_PORT_HSRP, dissect_hsrp);
216 udp_hash_add(UDP_PORT_PIM_RP_DISC, dissect_auto_rp);
217 udp_hash_add(UDP_PORT_TACACS, dissect_tacacs);
221 dissect_udp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
223 guint16 uh_sport, uh_dport, uh_ulen, uh_sum;
224 struct hash_struct *dissect_routine = NULL;
225 proto_tree *udp_tree;
228 if (!BYTES_ARE_IN_FRAME(offset, sizeof(e_udphdr))) {
229 dissect_data(pd, offset, fd, tree);
233 /* Avoids alignment problems on many architectures. */
234 memcpy(&uh, &pd[offset], sizeof(e_udphdr));
235 uh_sport = ntohs(uh.uh_sport);
236 uh_dport = ntohs(uh.uh_dport);
237 uh_ulen = ntohs(uh.uh_ulen);
238 uh_sum = ntohs(uh.uh_sum);
240 if (check_col(fd, COL_PROTOCOL))
241 col_add_str(fd, COL_PROTOCOL, "UDP");
242 if (check_col(fd, COL_INFO))
243 col_add_fstr(fd, COL_INFO, "Source port: %s Destination port: %s",
244 get_udp_port(uh_sport), get_udp_port(uh_dport));
247 ti = proto_tree_add_item(tree, proto_udp, offset, 8);
248 udp_tree = proto_item_add_subtree(ti, ett_udp);
250 proto_tree_add_item_format(udp_tree, hf_udp_srcport, offset, 2, uh_sport,
251 "Source port: %s (%u)", get_udp_port(uh_sport), uh_sport);
252 proto_tree_add_item_format(udp_tree, hf_udp_dstport, offset + 2, 2, uh_dport,
253 "Destination port: %s (%u)", get_udp_port(uh_dport), uh_dport);
255 proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset, 2, uh_sport);
256 proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset+2, 2, uh_dport);
258 proto_tree_add_item(udp_tree, hf_udp_length, offset + 4, 2, uh_ulen);
259 proto_tree_add_item_format(udp_tree, hf_udp_checksum, offset + 6, 2, uh_sum,
260 "Checksum: 0x%04x", uh_sum);
263 /* Skip over header */
267 pi.srcport = uh_sport;
268 pi.destport = uh_dport;
270 /* ONC RPC. We can't base this on anything in the UDP header; we have
271 to look at the payload. If "dissect_rpc()" returns TRUE, it was
272 an RPC packet, otherwise it's some other type of packet. */
273 if (dissect_rpc(pd, offset, fd, tree))
276 /* try to apply the plugins */
279 plugin *pt_plug = plugin_list;
283 if (pt_plug->enabled && !strcmp(pt_plug->protocol, "udp") &&
284 tree && dfilter_apply(pt_plug->filter, tree, pd)) {
285 pt_plug->dissector(pd, offset, fd, tree);
288 pt_plug = pt_plug->next;
294 /* XXX - we should do all of this through the table of ports. */
295 #define PORT_IS(port) (uh_sport == port || uh_dport == port)
296 if (PORT_IS(UDP_PORT_BOOTPS))
297 dissect_bootp(pd, offset, fd, tree);
298 else if (PORT_IS(UDP_PORT_DNS))
299 dissect_dns(pd, offset, fd, tree);
300 else if (PORT_IS(UDP_PORT_SRVLOC))
301 dissect_srvloc(pd, offset, fd, tree);
302 else if (PORT_IS(UDP_PORT_ISAKMP))
303 dissect_isakmp(pd, offset, fd, tree);
304 else if (PORT_IS(UDP_PORT_RIP)) {
305 /* we should check the source port too (RIP: UDP src and dst port 520) */
306 dissect_rip(pd, offset, fd, tree);
307 } else if (PORT_IS(UDP_PORT_RIPNG))
308 dissect_ripng(pd, offset, fd, tree);
309 else if (PORT_IS(UDP_PORT_NCP))
310 dissect_ncp(pd, offset, fd, tree); /* XXX -- need to handle nw_server_address */
311 else if (PORT_IS(UDP_PORT_NBNS))
312 dissect_nbns(pd, offset, fd, tree);
313 else if (PORT_IS(UDP_PORT_NBDGM))
314 dissect_nbdgm(pd, offset, fd, tree);
315 else if (PORT_IS(UDP_PORT_NTP))
316 dissect_ntp(pd, offset, fd, tree);
317 else if (PORT_IS(UDP_PORT_WHO))
318 dissect_who(pd, offset, fd, tree);
319 else if (PORT_IS(UDP_PORT_IPX)) /* RFC 1234 */
320 dissect_ipx(pd, offset, fd, tree);
321 else if ((uh_sport >= UDP_PORT_RX_LOW && uh_sport <= UDP_PORT_RX_HIGH) ||
322 (uh_dport >= UDP_PORT_RX_LOW && uh_dport <= UDP_PORT_RX_HIGH) ||
323 PORT_IS(UDP_PORT_RX_AFS_BACKUPS))
324 dissect_rx(pd, offset, fd, tree); /* transarc AFS's RX protocol */
325 else if (PORT_IS(UDP_PORT_SNMP) || PORT_IS(UDP_PORT_SNMP_TRAP))
326 dissect_snmp(pd, offset, fd, tree);
327 else if (PORT_IS(UDP_PORT_VINES)) {
328 /* FIXME: AFAIK, src and dst port must be the same */
329 dissect_vines_frp(pd, offset, fd, tree);
330 } else if (PORT_IS(UDP_PORT_TFTP)) {
331 /* This is the first point of call, but it adds a dynamic call */
332 udp_hash_add(MAX(uh_sport, uh_dport), dissect_tftp); /* Add to table */
333 dissect_tftp(pd, offset, fd, tree);
334 } else if (PORT_IS(UDP_PORT_TIME)) {
335 dissect_time(pd, offset, fd, tree);
336 } else if (PORT_IS(UDP_PORT_RADIUS) ||
337 PORT_IS(UDP_PORT_RADACCT) ||
338 PORT_IS(UDP_PORT_RADIUS_NEW) ||
339 PORT_IS(UDP_PORT_RADACCT_NEW) ) {
340 dissect_radius(pd, offset, fd, tree);
341 } else if ( PORT_IS(UDP_PORT_L2TP)) {
342 dissect_l2tp(pd,offset,fd,tree);
343 } else if ( PORT_IS(UDP_PORT_ICP)) {
344 dissect_icp(pd,offset,fd,tree);
345 } else if ( PORT_IS(UDP_PORT_ICQ)) {
346 dissect_icq(pd,offset,fd,tree);
347 } else if (PORT_IS(UDP_PORT_WCCP) ) {
348 dissect_wccp(pd, offset, fd, tree);
350 /* OK, find a routine in the table, else use the default */
352 if ((dissect_routine = udp_find_hash_ent(uh_sport))) {
354 struct hash_struct *dr2 = udp_find_hash_ent(uh_dport);
356 if (dr2 == NULL) { /* Not in the table, add */
358 udp_hash_add(uh_dport, dissect_tftp);
362 dissect_routine -> dissect(pd, offset, fd, tree);
364 else if ((dissect_routine = udp_find_hash_ent(uh_dport))) {
366 dissect_routine -> dissect(pd, offset, fd, tree);
371 dissect_data(pd, offset, fd, tree);
377 proto_register_udp(void)
379 static hf_register_info hf[] = {
381 { "Source Port", "udp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
385 { "Destination Port", "udp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
389 { "Source or Destination Port", "udp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
393 { "Length", "udp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
397 { "Checksum", "udp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
400 static gint *ett[] = {
404 proto_udp = proto_register_protocol("User Datagram Protocol", "udp");
405 proto_register_field_array(proto_udp, hf, array_length(hf));
406 proto_register_subtree_array(ett, array_length(ett));