aboutsummaryrefslogtreecommitdiffstats
path: root/packet-udp.c
blob: 90d3a32585de0120dc6042fe1e62650252f1b7ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* packet-udp.c
 * Routines for UDP packet disassembly
 *
 * $Id: packet-udp.c,v 1.50 2000/02/15 21:03:23 gram Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@zing.org>
 * Copyright 1998 Gerald Combs
 *
 * Richard Sharpe, 13-Feb-1999, added dispatch table support and 
 *                              support for tftp.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
 
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif

#include <stdio.h>
#include <stdlib.h>

#include <glib.h>
#include "globals.h"
#include "resolv.h"

#include "plugins.h"

#include "packet-auto_rp.h"
#include "packet-bootp.h"
#include "packet-dns.h"
#include "packet-hsrp.h"
#include "packet-icp.h"
#include "packet-icq.h"
#include "packet-ipx.h"
#include "packet-isakmp.h"
#include "packet-l2tp.h"
#include "packet-nbns.h"
#include "packet-ncp.h"
#include "packet-ntp.h"
#include "packet-radius.h"
#include "packet-rip.h"
#include "packet-ripng.h"
#include "packet-rpc.h"
#include "packet-rx.h"
#include "packet-sap.h"
#include "packet-snmp.h"
#include "packet-srvloc.h"
#include "packet-tacacs.h"
#include "packet-tftp.h"
#include "packet-time.h"
#include "packet-vines.h"
#include "packet-wccp.h"
#include "packet-who.h"

static int proto_udp = -1;		
static int hf_udp_srcport = -1;
static int hf_udp_dstport = -1;
static int hf_udp_port = -1;
static int hf_udp_length = -1;
static int hf_udp_checksum = -1;

static gint ett_udp = -1;

/* UDP structs and definitions */

typedef struct _e_udphdr {
  guint16 uh_sport;
  guint16 uh_dport;
  guint16 uh_ulen;
  guint16 uh_sum;
} e_udphdr;

/* UDP Ports -> should go in packet-udp.h */

#define UDP_PORT_TIME    37
#define UDP_PORT_TACACS  49
#define UDP_PORT_DNS     53
#define UDP_PORT_BOOTPS  67
#define UDP_PORT_TFTP    69
#define UDP_PORT_IPX    213
#define UDP_PORT_NTP	123
#define UDP_PORT_NBNS	137
#define UDP_PORT_NBDGM	138
#define UDP_PORT_SNMP   161
#define UDP_PORT_SNMP_TRAP 162
#define UDP_PORT_SRVLOC 427
#define UDP_PORT_PIM_RP_DISC 496
#define UDP_PORT_ISAKMP	500
#define UDP_PORT_WHO    513
#define UDP_PORT_RIP    520
#define UDP_PORT_RIPNG  521
#define UDP_PORT_NCP    524
#define UDP_PORT_VINES	573
#define UDP_PORT_RADIUS 1645
#define UDP_PORT_L2TP   1701
#define UDP_PORT_RADIUS_NEW 1812
#define UDP_PORT_RADACCT 1646
#define UDP_PORT_RADACCT_NEW 1813
#define UDP_PORT_HSRP   1985
#define UDP_PORT_ICP    3130
#define UDP_PORT_ICQ	4000
#define UDP_PORT_SAP	9875
#define UDP_PORT_RX_LOW 7000
#define UDP_PORT_RX_HIGH 7009
#define UDP_PORT_RX_AFS_BACKUPS 7021
#define UDP_PORT_WCCP	2048

struct hash_struct {
  guint16 proto;
  void (*dissect)(const u_char *, int, frame_data *, proto_tree *);
  struct hash_struct *next;
};

static struct hash_struct *hash_table[256];

/*
 * These routines are for UDP, will be generalized soon: RJS
 *
 * XXX - note that they should probably check the IP address as well as
 * the port number, so that we don't mistakenly identify packets as, say,
 * TFTP, merely because they have a source or destination port number
 * equal to the port being used by a TFTP daemon on some machine other
 * than the one they're going to or from.
 */

struct hash_struct *udp_find_hash_ent(guint16 proto) {

  int idx = proto % 256;
  struct hash_struct *hash_ent = hash_table[idx];

  while (hash_ent != NULL) {

    if (hash_ent -> proto == proto)
      return hash_ent;
  
    hash_ent = hash_ent -> next;

  }

  return NULL;

}

void udp_hash_add(guint16 proto,
	void (*dissect)(const u_char *, int, frame_data *, proto_tree *)) {

  int idx = proto % 256;   /* Simply take the remainder, hope for no collisions */
  struct hash_struct *hash_ent = (struct hash_struct *)malloc(sizeof(struct hash_struct));
  struct hash_struct *hash_ent2;
  
  hash_ent -> proto = proto;
  hash_ent -> dissect = dissect;
  hash_ent -> next = NULL;

  if (hash_ent == NULL) {

    fprintf(stderr, "Could not allocate space for hash structure in dissect_udp\n");
    exit(1);
  }

  if (hash_table[idx]) {  /* Something, add on end */

    hash_ent2 = hash_table[idx];

    while (hash_ent2 -> next != NULL)
      hash_ent2 = hash_ent2 -> next;

    hash_ent2 -> next = hash_ent;     /* Bad in pathalogical cases */

  }
  else {

    hash_table[idx] = hash_ent;

  }

}

void init_dissect_udp(void) {

  int i;

  for (i = 0; i < 256; i++) {

    hash_table[i] = NULL;

  }

  /* Now add the protocols we know about */

  udp_hash_add(UDP_PORT_BOOTPS, dissect_bootp);
  udp_hash_add(UDP_PORT_TFTP, dissect_tftp);
  udp_hash_add(UDP_PORT_SAP, dissect_sap);
  udp_hash_add(UDP_PORT_HSRP, dissect_hsrp);
  udp_hash_add(UDP_PORT_PIM_RP_DISC, dissect_auto_rp);
  udp_hash_add(UDP_PORT_TACACS, dissect_tacacs);
}

void
dissect_udp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
  e_udphdr  uh;
  guint16    uh_sport, uh_dport, uh_ulen, uh_sum;
  struct hash_struct *dissect_routine = NULL;
  proto_tree *udp_tree;
  proto_item *ti;

  if (!BYTES_ARE_IN_FRAME(offset, sizeof(e_udphdr))) {
    dissect_data(pd, offset, fd, tree);
    return;
  }

  /* Avoids alignment problems on many architectures. */
  memcpy(&uh, &pd[offset], sizeof(e_udphdr));
  uh_sport = ntohs(uh.uh_sport);
  uh_dport = ntohs(uh.uh_dport);
  uh_ulen  = ntohs(uh.uh_ulen);
  uh_sum   = ntohs(uh.uh_sum);
  
  if (check_col(fd, COL_PROTOCOL))
    col_add_str(fd, COL_PROTOCOL, "UDP");
  if (check_col(fd, COL_INFO))
    col_add_fstr(fd, COL_INFO, "Source port: %s  Destination port: %s",
	    get_udp_port(uh_sport), get_udp_port(uh_dport));
    
  if (tree) {
    ti = proto_tree_add_item(tree, proto_udp, offset, 8);
    udp_tree = proto_item_add_subtree(ti, ett_udp);

    proto_tree_add_item_format(udp_tree, hf_udp_srcport, offset, 2, uh_sport,
	"Source port: %s (%u)", get_udp_port(uh_sport), uh_sport);
    proto_tree_add_item_format(udp_tree, hf_udp_dstport, offset + 2, 2, uh_dport,
	"Destination port: %s (%u)", get_udp_port(uh_dport), uh_dport);

    proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset, 2, uh_sport);
    proto_tree_add_item_hidden(udp_tree, hf_udp_port, offset+2, 2, uh_dport);

    proto_tree_add_item(udp_tree, hf_udp_length, offset + 4, 2,  uh_ulen);
    proto_tree_add_item_format(udp_tree, hf_udp_checksum, offset + 6, 2, uh_sum,
	"Checksum: 0x%04x", uh_sum);
  }

  /* Skip over header */
  offset += 8;

  pi.ptype = PT_UDP;
  pi.srcport = uh_sport;
  pi.destport = uh_dport;

  /* ONC RPC.  We can't base this on anything in the UDP header; we have
     to look at the payload.  If "dissect_rpc()" returns TRUE, it was
     an RPC packet, otherwise it's some other type of packet. */
  if (dissect_rpc(pd, offset, fd, tree))
    return;

  /* try to apply the plugins */
#ifdef HAVE_PLUGINS
  {
      plugin *pt_plug = plugin_list;

      if (pt_plug) {
	  while (pt_plug) {
	      if (pt_plug->enabled && !strcmp(pt_plug->protocol, "udp") &&
		  tree && dfilter_apply(pt_plug->filter, tree, pd)) {
		  pt_plug->dissector(pd, offset, fd, tree);
		  return;
	      }
	      pt_plug = pt_plug->next;
	  }
      }
  }
#endif

  /* XXX - we should do all of this through the table of ports. */
#define PORT_IS(port)	(uh_sport == port || uh_dport == port)
  if (PORT_IS(UDP_PORT_BOOTPS))
      dissect_bootp(pd, offset, fd, tree);
  else if (PORT_IS(UDP_PORT_DNS))
      dissect_dns(pd, offset, fd, tree);
  else if (PORT_IS(UDP_PORT_SRVLOC))
      dissect_srvloc(pd, offset, fd, tree);
  else if (PORT_IS(UDP_PORT_ISAKMP))
      dissect_isakmp(pd, offset, fd, tree);
  else if (PORT_IS(UDP_PORT_RIP)) {
      /* we should check the source port too (RIP: UDP src and dst port 520) */
      dissect_rip(pd, offset, fd, tree);
  } else if (PORT_IS(UDP_PORT_RIPNG))
      dissect_ripng(pd, offset, fd, tree);
  else if (PORT_IS(UDP_PORT_NCP))
      dissect_ncp(pd, offset, fd, tree); /* XXX -- need to handle nw_server_address */
  else if (PORT_IS(UDP_PORT_NBNS))
      dissect_nbns(pd, offset, fd, tree);
  else if (PORT_IS(UDP_PORT_NBDGM))
      dissect_nbdgm(pd, offset, fd, tree);
  else if (PORT_IS(UDP_PORT_NTP))
      dissect_ntp(pd, offset, fd, tree);
  else if (PORT_IS(UDP_PORT_WHO))
      dissect_who(pd, offset, fd, tree);
  else if (PORT_IS(UDP_PORT_IPX)) /* RFC 1234 */
      dissect_ipx(pd, offset, fd, tree);
  else if ((uh_sport >= UDP_PORT_RX_LOW && uh_sport <= UDP_PORT_RX_HIGH) ||
	(uh_dport >= UDP_PORT_RX_LOW && uh_dport <= UDP_PORT_RX_HIGH) ||
	PORT_IS(UDP_PORT_RX_AFS_BACKUPS)) 
      dissect_rx(pd, offset, fd, tree); /* transarc AFS's RX protocol */
  else if (PORT_IS(UDP_PORT_SNMP) || PORT_IS(UDP_PORT_SNMP_TRAP))
      dissect_snmp(pd, offset, fd, tree);
  else if (PORT_IS(UDP_PORT_VINES)) {
      /* FIXME: AFAIK, src and dst port must be the same */
      dissect_vines_frp(pd, offset, fd, tree);
  } else if (PORT_IS(UDP_PORT_TFTP)) {
      /* This is the first point of call, but it adds a dynamic call */
      udp_hash_add(MAX(uh_sport, uh_dport), dissect_tftp);  /* Add to table */
      dissect_tftp(pd, offset, fd, tree);
  } else if (PORT_IS(UDP_PORT_TIME)) {
      dissect_time(pd, offset, fd, tree);
  } else if (PORT_IS(UDP_PORT_RADIUS) ||
		PORT_IS(UDP_PORT_RADACCT) ||
		PORT_IS(UDP_PORT_RADIUS_NEW) ||
		PORT_IS(UDP_PORT_RADACCT_NEW) ) {
      dissect_radius(pd, offset, fd, tree);
   } else if ( PORT_IS(UDP_PORT_L2TP)) {
      dissect_l2tp(pd,offset,fd,tree);
  } else if ( PORT_IS(UDP_PORT_ICP)) {
	dissect_icp(pd,offset,fd,tree);
 } else if ( PORT_IS(UDP_PORT_ICQ)) {
        dissect_icq(pd,offset,fd,tree);
 } else if (PORT_IS(UDP_PORT_WCCP) ) {
 	dissect_wccp(pd, offset, fd, tree);
 } else {
      /* OK, find a routine in the table, else use the default */

      if ((dissect_routine = udp_find_hash_ent(uh_sport))) {

	struct hash_struct *dr2 = udp_find_hash_ent(uh_dport);

	if (dr2 == NULL) {  /* Not in the table, add */

	  udp_hash_add(uh_dport, dissect_tftp);

	}

	dissect_routine -> dissect(pd, offset, fd, tree);
      }
      else if ((dissect_routine = udp_find_hash_ent(uh_dport))) {

	dissect_routine -> dissect(pd, offset, fd, tree);

      }
      else {

	dissect_data(pd, offset, fd, tree);
      }
  }
}

void
proto_register_udp(void)
{
	static hf_register_info hf[] = {
		{ &hf_udp_srcport,
		{ "Source Port",	"udp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
			"" }},

		{ &hf_udp_dstport,
		{ "Destination Port",	"udp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
			"" }},

		{ &hf_udp_port,
		{ "Source or Destination Port",	"udp.port", FT_UINT16, BASE_DEC,  NULL, 0x0,
			"" }},

		{ &hf_udp_length,
		{ "Length",		"udp.length", FT_UINT16, BASE_DEC, NULL, 0x0,
			"" }},

		{ &hf_udp_checksum,
		{ "Checksum",		"udp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
			"" }},
	};
	static gint *ett[] = {
		&ett_udp,
	};

	proto_udp = proto_register_protocol("User Datagram Protocol", "udp");
	proto_register_field_array(proto_udp, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
}