Make "make-reg-dotc" generate a "register_all_protocol_handoffs()"
[metze/wireshark/wip.git] / packet-udp.c
1 /* packet-udp.c
2  * Routines for UDP packet disassembly
3  *
4  * $Id: packet-udp.c,v 1.57 2000/04/04 06:17:29 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * Richard Sharpe, 13-Feb-1999, added dispatch table support and 
11  *                              support for tftp.
12  * 
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.
17  * 
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.
22  * 
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.
26  */
27  
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>
38 #endif
39
40 #include <stdio.h>
41 #include <stdlib.h>
42
43 #include <glib.h>
44 #include "globals.h"
45 #include "resolv.h"
46
47 #include "plugins.h"
48
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"
75
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;
82
83 static gint ett_udp = -1;
84
85 /* UDP structs and definitions */
86
87 typedef struct _e_udphdr {
88   guint16 uh_sport;
89   guint16 uh_dport;
90   guint16 uh_ulen;
91   guint16 uh_sum;
92 } e_udphdr;
93
94 /* UDP Ports -> should go in packet-udp.h */
95
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
129
130 static dissector_table_t udp_dissector_table;
131
132 void
133 dissect_udp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
134   e_udphdr  uh;
135   guint16    uh_sport, uh_dport, uh_ulen, uh_sum;
136   proto_tree *udp_tree;
137   proto_item *ti;
138
139   if (!BYTES_ARE_IN_FRAME(offset, sizeof(e_udphdr))) {
140     dissect_data(pd, offset, fd, tree);
141     return;
142   }
143
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);
150   
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));
156     
157   if (tree) {
158     ti = proto_tree_add_item(tree, proto_udp, offset, 8);
159     udp_tree = proto_item_add_subtree(ti, ett_udp);
160
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);
165
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);
168
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);
172   }
173
174   /* Skip over header */
175   offset += 8;
176
177   pi.ptype = PT_UDP;
178   pi.srcport = uh_sport;
179   pi.destport = uh_dport;
180
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))
185     return;
186
187   /* try to apply the plugins */
188 #ifdef HAVE_PLUGINS
189   {
190       plugin *pt_plug = plugin_list;
191
192       if (pt_plug) {
193           while (pt_plug) {
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);
197                   return;
198               }
199               pt_plug = pt_plug->next;
200           }
201       }
202   }
203 #endif
204
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);
223  } else {
224       /* OK, find a routine in the table, else use the default */
225
226       if (!dissector_try_port(udp_dissector_table, uh_sport, pd, offset,
227                                 fd, tree) &&
228           !dissector_try_port(udp_dissector_table, uh_dport, pd, offset,
229                                 fd, tree))
230         dissect_data(pd, offset, fd, tree);
231   }
232 }
233
234 void
235 proto_register_udp(void)
236 {
237         static hf_register_info hf[] = {
238                 { &hf_udp_srcport,
239                 { "Source Port",        "udp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
240                         "" }},
241
242                 { &hf_udp_dstport,
243                 { "Destination Port",   "udp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
244                         "" }},
245
246                 { &hf_udp_port,
247                 { "Source or Destination Port", "udp.port", FT_UINT16, BASE_DEC,  NULL, 0x0,
248                         "" }},
249
250                 { &hf_udp_length,
251                 { "Length",             "udp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
252                         "" }},
253
254                 { &hf_udp_checksum,
255                 { "Checksum",           "udp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
256                         "" }},
257         };
258         static gint *ett[] = {
259                 &ett_udp,
260         };
261
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));
265
266 /* subdissector code */
267         udp_dissector_table = register_dissector_table(hf_udp_port);
268
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
272            "packet-tcp.c". */
273
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);
300 }