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