76b9fd967d29fc19f8e0e954f927d4d29c989d62
[obnox/wireshark/wip.git] / packet-udp.c
1 /* packet-udp.c
2  * Routines for UDP packet disassembly
3  *
4  * $Id: packet-udp.c,v 1.40 1999/12/05 02:32:39 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 "packet.h"
45 #include "resolv.h"
46
47 static int proto_udp = -1;              
48 static int hf_udp_srcport = -1;
49 static int hf_udp_dstport = -1;
50 static int hf_udp_port = -1;
51 static int hf_udp_length = -1;
52 static int hf_udp_checksum = -1;
53
54 static gint ett_udp = -1;
55
56 /* UDP structs and definitions */
57
58 typedef struct _e_udphdr {
59   guint16 uh_sport;
60   guint16 uh_dport;
61   guint16 uh_ulen;
62   guint16 uh_sum;
63 } e_udphdr;
64
65 /* UDP Ports -> should go in packet-udp.h */
66
67 #define UDP_PORT_TACACS  49
68 #define UDP_PORT_DNS     53
69 #define UDP_PORT_BOOTPS  67
70 #define UDP_PORT_TFTP    69
71 #define UDP_PORT_IPX    213
72 #define UDP_PORT_NTP    123
73 #define UDP_PORT_NBNS   137
74 #define UDP_PORT_NBDGM  138
75 #define UDP_PORT_SNMP   161
76 #define UDP_PORT_PIM_RP_DISC 496
77 #define UDP_PORT_ISAKMP 500
78 #define UDP_PORT_RIP    520
79 #define UDP_PORT_RIPNG  521
80 #define UDP_PORT_VINES  573
81 #define UDP_PORT_RADIUS 1645
82 #define UDP_PORT_RADIUS_NEW 1812
83 #define UDP_PORT_RADACCT 1646
84 #define UDP_PORT_RADACCT_NEW 1813
85 #define UDP_PORT_HSRP   1985
86 #define UDP_PORT_ICP    3130
87 #define UDP_PORT_ICQ    4000
88 #define UDP_PORT_SAP    9875
89 #define UDP_PORT_RX_LOW 7000
90 #define UDP_PORT_RX_HIGH 7009
91 #define UDP_PORT_RX_AFS_BACKUPS 7021
92
93 struct hash_struct {
94   guint16 proto;
95   void (*dissect)(const u_char *, int, frame_data *, proto_tree *);
96   struct hash_struct *next;
97 };
98
99 static struct hash_struct *hash_table[256];
100
101 /*
102  * These routines are for UDP, will be generalized soon: RJS
103  *
104  * XXX - note that they should probably check the IP address as well as
105  * the port number, so that we don't mistakenly identify packets as, say,
106  * TFTP, merely because they have a source or destination port number
107  * equal to the port being used by a TFTP daemon on some machine other
108  * than the one they're going to or from.
109  */
110
111 struct hash_struct *udp_find_hash_ent(guint16 proto) {
112
113   int idx = proto % 256;
114   struct hash_struct *hash_ent = hash_table[idx];
115
116   while (hash_ent != NULL) {
117
118     if (hash_ent -> proto == proto)
119       return hash_ent;
120   
121     hash_ent = hash_ent -> next;
122
123   }
124
125   return NULL;
126
127 }
128
129 void udp_hash_add(guint16 proto,
130         void (*dissect)(const u_char *, int, frame_data *, proto_tree *)) {
131
132   int idx = proto % 256;   /* Simply take the remainder, hope for no collisions */
133   struct hash_struct *hash_ent = (struct hash_struct *)malloc(sizeof(struct hash_struct));
134   struct hash_struct *hash_ent2;
135   
136   hash_ent -> proto = proto;
137   hash_ent -> dissect = dissect;
138   hash_ent -> next = NULL;
139
140   if (hash_ent == NULL) {
141
142     fprintf(stderr, "Could not allocate space for hash structure in dissect_udp\n");
143     exit(1);
144   }
145
146   if (hash_table[idx]) {  /* Something, add on end */
147
148     hash_ent2 = hash_table[idx];
149
150     while (hash_ent2 -> next != NULL)
151       hash_ent2 = hash_ent2 -> next;
152
153     hash_ent2 -> next = hash_ent;     /* Bad in pathalogical cases */
154
155   }
156   else {
157
158     hash_table[idx] = hash_ent;
159
160   }
161
162 }
163
164 void init_dissect_udp(void) {
165
166   int i;
167
168   for (i = 0; i < 256; i++) {
169
170     hash_table[i] = NULL;
171
172   }
173
174   /* Now add the protocols we know about */
175
176   udp_hash_add(UDP_PORT_BOOTPS, dissect_bootp);
177   udp_hash_add(UDP_PORT_TFTP, dissect_tftp);
178   udp_hash_add(UDP_PORT_SAP, dissect_sap);
179   udp_hash_add(UDP_PORT_HSRP, dissect_hsrp);
180   udp_hash_add(UDP_PORT_PIM_RP_DISC, dissect_auto_rp);
181   udp_hash_add(UDP_PORT_TACACS, dissect_tacacs);
182 }
183
184 void
185 dissect_udp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
186   e_udphdr  uh;
187   guint16    uh_sport, uh_dport, uh_ulen, uh_sum;
188   struct hash_struct *dissect_routine = NULL;
189   proto_tree *udp_tree;
190   proto_item *ti;
191
192   if (!BYTES_ARE_IN_FRAME(offset, sizeof(e_udphdr))) {
193     dissect_data(pd, offset, fd, tree);
194     return;
195   }
196
197   /* Avoids alignment problems on many architectures. */
198   memcpy(&uh, &pd[offset], sizeof(e_udphdr));
199   uh_sport = ntohs(uh.uh_sport);
200   uh_dport = ntohs(uh.uh_dport);
201   uh_ulen  = ntohs(uh.uh_ulen);
202   uh_sum   = ntohs(uh.uh_sum);
203   
204   if (check_col(fd, COL_PROTOCOL))
205     col_add_str(fd, COL_PROTOCOL, "UDP");
206   if (check_col(fd, COL_INFO))
207     col_add_fstr(fd, COL_INFO, "Source port: %s  Destination port: %s",
208             get_udp_port(uh_sport), get_udp_port(uh_dport));
209     
210   if (tree) {
211     ti = proto_tree_add_item(tree, proto_udp, offset, 8);
212     udp_tree = proto_item_add_subtree(ti, ett_udp);
213
214     proto_tree_add_item_format(udp_tree, hf_udp_srcport, offset, 2, uh_sport,
215         "Source port: %s (%u)", get_udp_port(uh_sport), uh_sport);
216     proto_tree_add_item_format(udp_tree, hf_udp_dstport, offset + 2, 2, uh_dport,
217         "Destination port: %s (%u)", get_udp_port(uh_dport), uh_dport);
218
219     proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset, 2, uh_sport);
220     proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset+2, 2, uh_dport);
221
222     proto_tree_add_item(udp_tree, hf_udp_length, offset + 4, 2,  uh_ulen);
223     proto_tree_add_item_format(udp_tree, hf_udp_checksum, offset + 6, 2, uh_sum,
224         "Checksum: 0x%04x", uh_sum);
225   }
226
227   /* Skip over header */
228   offset += 8;
229
230   pi.ptype = PT_UDP;
231   pi.srcport = uh_sport;
232   pi.destport = uh_dport;
233
234   /* ONC RPC.  We can't base this on anything in the UDP header; we have
235      to look at the payload.  If "dissect_rpc()" returns TRUE, it was
236      an RPC packet, otherwise it's some other type of packet. */
237   if (dissect_rpc(pd, offset, fd, tree))
238     return;
239
240   /* XXX - we should do all of this through the table of ports. */
241 #define PORT_IS(port)   (uh_sport == port || uh_dport == port)
242   if (PORT_IS(UDP_PORT_BOOTPS))
243       dissect_bootp(pd, offset, fd, tree);
244   else if (PORT_IS(UDP_PORT_DNS))
245       dissect_dns(pd, offset, fd, tree);
246   else if (PORT_IS(UDP_PORT_ISAKMP))
247       dissect_isakmp(pd, offset, fd, tree);
248   else if (PORT_IS(UDP_PORT_RIP)) {
249       /* we should check the source port too (RIP: UDP src and dst port 520) */
250       dissect_rip(pd, offset, fd, tree);
251   } else if (PORT_IS(UDP_PORT_RIPNG))
252       dissect_ripng(pd, offset, fd, tree);
253   else if (PORT_IS(UDP_PORT_NBNS))
254       dissect_nbns(pd, offset, fd, tree);
255   else if (PORT_IS(UDP_PORT_NBDGM))
256       dissect_nbdgm(pd, offset, fd, tree);
257   else if (PORT_IS(UDP_PORT_NTP))
258       dissect_ntp(pd, offset, fd, tree);
259   else if (PORT_IS(UDP_PORT_IPX)) /* RFC 1234 */
260       dissect_ipx(pd, offset, fd, tree);
261   else if ((uh_sport >= UDP_PORT_RX_LOW && uh_sport <= UDP_PORT_RX_HIGH) ||
262         (uh_dport >= UDP_PORT_RX_LOW && uh_dport <= UDP_PORT_RX_HIGH) ||
263         PORT_IS(UDP_PORT_RX_AFS_BACKUPS)) 
264       dissect_rx(pd, offset, fd, tree); /* transarc AFS's RX protocol */
265   else if (PORT_IS(UDP_PORT_SNMP))
266       dissect_snmp(pd, offset, fd, tree);
267   else if (PORT_IS(UDP_PORT_VINES)) {
268       /* FIXME: AFAIK, src and dst port must be the same */
269       dissect_vines_frp(pd, offset, fd, tree);
270   } else if (PORT_IS(UDP_PORT_TFTP)) {
271       /* This is the first point of call, but it adds a dynamic call */
272       udp_hash_add(MAX(uh_sport, uh_dport), dissect_tftp);  /* Add to table */
273       dissect_tftp(pd, offset, fd, tree);
274   } else if (PORT_IS(UDP_PORT_RADIUS) ||
275                 PORT_IS(UDP_PORT_RADACCT) ||
276                 PORT_IS(UDP_PORT_RADIUS_NEW) ||
277                 PORT_IS(UDP_PORT_RADACCT_NEW) ) {
278       dissect_radius(pd, offset, fd, tree);
279   } else if ( PORT_IS(UDP_PORT_ICP)) {
280         dissect_icp(pd,offset,fd,tree);
281  } else if ( PORT_IS(UDP_PORT_ICQ)) {
282         dissect_icq(pd,offset,fd,tree);
283  } else {
284       /* OK, find a routine in the table, else use the default */
285
286       if ((dissect_routine = udp_find_hash_ent(uh_sport))) {
287
288         struct hash_struct *dr2 = udp_find_hash_ent(uh_dport);
289
290         if (dr2 == NULL) {  /* Not in the table, add */
291
292           udp_hash_add(uh_dport, dissect_tftp);
293
294         }
295
296         dissect_routine -> dissect(pd, offset, fd, tree);
297       }
298       else if ((dissect_routine = udp_find_hash_ent(uh_dport))) {
299
300         dissect_routine -> dissect(pd, offset, fd, tree);
301
302       }
303       else {
304
305         dissect_data(pd, offset, fd, tree);
306       }
307   }
308 }
309
310 void
311 proto_register_udp(void)
312 {
313         static hf_register_info hf[] = {
314                 { &hf_udp_srcport,
315                 { "Source Port",        "udp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
316                         "" }},
317
318                 { &hf_udp_dstport,
319                 { "Destination Port",   "udp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
320                         "" }},
321
322                 { &hf_udp_port,
323                 { "Source or Destination Port", "udp.port", FT_UINT16, BASE_DEC,  NULL, 0x0,
324                         "" }},
325
326                 { &hf_udp_length,
327                 { "Length",             "udp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
328                         "" }},
329
330                 { &hf_udp_checksum,
331                 { "Checksum",           "udp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
332                         "" }},
333         };
334         static gint *ett[] = {
335                 &ett_udp,
336         };
337
338         proto_udp = proto_register_protocol("User Datagram Protocol", "udp");
339         proto_register_field_array(proto_udp, hf, array_length(hf));
340         proto_register_subtree_array(ett, array_length(ett));
341 }