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