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