aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-tftp.c
blob: 2517984abb1e1947a732592dd6a7b5c65d7562e7 (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/* packet-tftp.c
 * Routines for tftp packet dissection
 *
 * Richard Sharpe <rsharpe@ns.aus.com>
 * Craig Newell <CraigN@cheque.uq.edu.au>
 *      RFC2347 TFTP Option Extension
 * Joerg Mayer (see AUTHORS file)
 *      RFC2348 TFTP Blocksize Option
 *
 * $Id$
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * Copied from packet-bootp.c
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* Documentation:
 * RFC 1350: THE TFTP PROTOCOL (REVISION 2)
 * RFC 2090: TFTP Multicast Option
 *           (not yet implemented)
 * RFC 2347: TFTP Option Extension
 * RFC 2348: TFTP Blocksize Option
 * RFC 2349: TFTP Timeout Interval and Transfer Size Options
 *           (not yet implemented)
 */

#include "config.h"

#include <glib.h>
#include <stdlib.h>
#include <epan/packet.h>
#include <epan/conversation.h>
#include <epan/wmem/wmem.h>
#include <epan/expert.h>
#include <epan/range.h>
#include <epan/prefs.h>

void proto_register_tftp(void);

/* Things we may want to remember for a whole conversation */
typedef struct _tftp_conv_info_t {
	guint16 blocksize;
	gchar *source_file, *destination_file;
} tftp_conv_info_t;


static int proto_tftp = -1;
static int hf_tftp_opcode = -1;
static int hf_tftp_source_file = -1;
static int hf_tftp_destination_file = -1;
static int hf_tftp_transfer_type = -1;
static int hf_tftp_blocknum = -1;
static int hf_tftp_error_code = -1;
static int hf_tftp_error_string = -1;
static int hf_tftp_option_name = -1;
static int hf_tftp_option_value = -1;

static gint ett_tftp = -1;
static gint ett_tftp_option = -1;

static expert_field ei_tftp_blocksize_range = EI_INIT;

static dissector_handle_t tftp_handle;
static dissector_handle_t data_handle;

#define UDP_PORT_TFTP_RANGE    "69"

void proto_reg_handoff_tftp (void);

/* User definable values */
static range_t *global_tftp_port_range;

#define	TFTP_RRQ	1
#define	TFTP_WRQ	2
#define	TFTP_DATA	3
#define	TFTP_ACK	4
#define	TFTP_ERROR	5
#define	TFTP_OACK	6
#define	TFTP_INFO	255

static const value_string tftp_opcode_vals[] = {
  { TFTP_RRQ,   "Read Request" },
  { TFTP_WRQ,   "Write Request" },
  { TFTP_DATA,  "Data Packet" },
  { TFTP_ACK,   "Acknowledgement" },
  { TFTP_ERROR, "Error Code" },
  { TFTP_OACK,  "Option Acknowledgement" },
  { TFTP_INFO,  "Information (MSDP)" },
  { 0,          NULL }
};

static const value_string tftp_error_code_vals[] = {
  { 0, "Not defined" },
  { 1, "File not found" },
  { 2, "Access violation" },
  { 3, "Disk full or allocation exceeded" },
  { 4, "Illegal TFTP Operation" },
  { 5, "Unknown transfer ID" },		/* Does not cause termination */
  { 6, "File already exists" },
  { 7, "No such user" },
  { 8, "Option negotiation failed" },
  { 0, NULL }
};

static void
tftp_dissect_options(tvbuff_t *tvb, packet_info *pinfo, int offset,
	proto_tree *tree, guint16 opcode, tftp_conv_info_t *tftp_info)
{
	int option_len, value_len;
	int value_offset;
	const char *optionname;
	const char *optionvalue;
	proto_item *opt_item;
	proto_tree *opt_tree;

	while (tvb_offset_exists(tvb, offset)) {
	  option_len = tvb_strsize(tvb, offset);	/* length of option */
	  value_offset = offset + option_len;
	  value_len = tvb_strsize(tvb, value_offset);	/* length of value */
	  optionname = tvb_format_text(tvb, offset, option_len);
	  optionvalue = tvb_format_text(tvb, value_offset, value_len);
	  opt_item = proto_tree_add_text(tree, tvb, offset, option_len+value_len,
	          "Option: %s = %s", optionname, optionvalue);

	  opt_tree = proto_item_add_subtree(opt_item, ett_tftp_option);
	  proto_tree_add_item(opt_tree, hf_tftp_option_name, tvb, offset,
		option_len, ENC_ASCII|ENC_NA);
	  proto_tree_add_item(opt_tree, hf_tftp_option_value, tvb, value_offset,
		value_len, ENC_ASCII|ENC_NA);

	  offset += option_len + value_len;

	  col_append_fstr(pinfo->cinfo, COL_INFO, ", %s=%s",
			  optionname, optionvalue);

	  /* Special code to handle individual options */
	  if (!g_ascii_strcasecmp((const char *)optionname, "blksize") &&
	      opcode == TFTP_OACK) {
		gint blocksize = (gint)strtol((const char *)optionvalue, NULL, 10);
		if (blocksize < 8 || blocksize > 65464) {
			expert_add_info(pinfo, NULL, &ei_tftp_blocksize_range);
		} else {
			tftp_info->blocksize = blocksize;
		}
	  }
	}
}

static void dissect_tftp_message(tftp_conv_info_t *tftp_info,
				 tvbuff_t *tvb, packet_info *pinfo,
				 proto_tree *tree)
{
	proto_tree	 *tftp_tree = NULL;
	proto_item	 *ti;
	gint		 offset = 0;
	guint16		 opcode;
	guint16		 bytes;
	guint16		 blocknum;
	guint		 i1;
	guint16	         error;

	col_set_str(pinfo->cinfo, COL_PROTOCOL, "TFTP");

	opcode = tvb_get_ntohs(tvb, offset);

	col_add_str(pinfo->cinfo, COL_INFO,
		    val_to_str(opcode, tftp_opcode_vals, "Unknown (0x%04x)"));

	if (tree) {
	  ti = proto_tree_add_item(tree, proto_tftp, tvb, offset, -1, ENC_NA);
	  tftp_tree = proto_item_add_subtree(ti, ett_tftp);

	  if (tftp_info->source_file) {
	    ti = proto_tree_add_string(tftp_tree, hf_tftp_source_file, tvb,
				       0, 0, tftp_info->source_file);
	    PROTO_ITEM_SET_GENERATED(ti);
	  }

	  if (tftp_info->destination_file) {
	    ti = proto_tree_add_string(tftp_tree, hf_tftp_destination_file, tvb,
				       0, 0, tftp_info->destination_file);
	    PROTO_ITEM_SET_GENERATED(ti);
	  }

	  proto_tree_add_uint(tftp_tree, hf_tftp_opcode, tvb,
			      offset, 2, opcode);
	}
	offset += 2;

	switch (opcode) {

	case TFTP_RRQ:
	  i1 = tvb_strsize(tvb, offset);
	  proto_tree_add_item(tftp_tree, hf_tftp_source_file,
			      tvb, offset, i1, ENC_ASCII|ENC_NA);

	  tftp_info->source_file = tvb_get_string(wmem_file_scope(), tvb, offset, i1);

	  col_append_fstr(pinfo->cinfo, COL_INFO, ", File: %s",
			  tvb_format_stringzpad(tvb, offset, i1));

	  offset += i1;

	  i1 = tvb_strsize(tvb, offset);
	  proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
			      tvb, offset, i1, ENC_ASCII|ENC_NA);

	  col_append_fstr(pinfo->cinfo, COL_INFO, ", Transfer type: %s",
			  tvb_format_stringzpad(tvb, offset, i1));

	  offset += i1;

	  tftp_dissect_options(tvb, pinfo,  offset, tftp_tree,
			       opcode, tftp_info);
	  break;

	case TFTP_WRQ:
	  i1 = tvb_strsize(tvb, offset);
	  proto_tree_add_item(tftp_tree, hf_tftp_destination_file,
			      tvb, offset, i1, ENC_ASCII|ENC_NA);

	  tftp_info->destination_file =
	    tvb_get_string(wmem_file_scope(), tvb, offset, i1);

	  col_append_fstr(pinfo->cinfo, COL_INFO, ", File: %s",
			  tvb_format_stringzpad(tvb, offset, i1));

	  offset += i1;

	  i1 = tvb_strsize(tvb, offset);
	  proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
			           tvb, offset, i1, ENC_ASCII|ENC_NA);

	  col_append_fstr(pinfo->cinfo, COL_INFO, ", Transfer type: %s",
			  tvb_format_stringzpad(tvb, offset, i1));

	  offset += i1;

	  tftp_dissect_options(tvb, pinfo, offset, tftp_tree,
			       opcode,  tftp_info);
	  break;

	case TFTP_INFO:
	  tftp_dissect_options(tvb, pinfo, offset, tftp_tree,
			       opcode,  tftp_info);
	  break;

	case TFTP_DATA:
	  blocknum = tvb_get_ntohs(tvb, offset);
	  proto_tree_add_uint(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
			      blocknum);

	  offset += 2;

	  bytes = tvb_reported_length_remaining(tvb, offset);

	  col_append_fstr(pinfo->cinfo, COL_INFO, ", Block: %i%s",
			  blocknum,
			  (bytes < tftp_info->blocksize)?" (last)":"" );

	  if (bytes != 0) {
	    tvbuff_t *data_tvb = tvb_new_subset(tvb, offset, -1, bytes);
	    call_dissector(data_handle, data_tvb, pinfo, tree);
	  }
	  break;

	case TFTP_ACK:
	  blocknum = tvb_get_ntohs(tvb, offset);
	  proto_tree_add_uint(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
			      blocknum);

	  col_append_fstr(pinfo->cinfo, COL_INFO, ", Block: %i",
			  blocknum);
	  break;

	case TFTP_ERROR:
	  error = tvb_get_ntohs(tvb, offset);
	  proto_tree_add_uint(tftp_tree, hf_tftp_error_code, tvb, offset, 2,
			      error);

	  col_append_fstr(pinfo->cinfo, COL_INFO, ", Code: %s",
			  val_to_str(error, tftp_error_code_vals, "Unknown (%u)"));

	  offset += 2;

	  i1 = tvb_strsize(tvb, offset);
	  proto_tree_add_item(tftp_tree, hf_tftp_error_string, tvb, offset,
			      i1, ENC_ASCII|ENC_NA);

	  col_append_fstr(pinfo->cinfo, COL_INFO, ", Message: %s",
			  tvb_format_stringzpad(tvb, offset, i1));

	  expert_add_info(pinfo, NULL, &ei_tftp_blocksize_range);
	  break;

	case TFTP_OACK:
	  tftp_dissect_options(tvb, pinfo, offset, tftp_tree,
			       opcode, tftp_info);
	  break;

	default:
	  proto_tree_add_text(tftp_tree, tvb, offset, -1,
			      "Data (%d bytes)", tvb_reported_length_remaining(tvb, offset));
	  break;

	}

  return;
}

static gboolean
dissect_embeddedtftp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
  /* Used to dissect TFTP packets where one can not assume
     that the TFTP is the only protocol used by that port, and
     that TFTP may not be carried by UDP */
  conversation_t   *conversation = NULL;
  guint16           opcode;
  tftp_conv_info_t *tftp_info;

  conversation = find_or_create_conversation(pinfo);

  tftp_info = (tftp_conv_info_t *)conversation_get_proto_data(conversation, proto_tftp);
  if (!tftp_info) {
    tftp_info = wmem_new(wmem_file_scope(), tftp_conv_info_t);
    tftp_info->blocksize = 512; /* TFTP default block size */
    tftp_info->source_file = NULL;
    tftp_info->destination_file = NULL;
    conversation_add_proto_data(conversation, proto_tftp, tftp_info);
  }

  opcode = tvb_get_ntohs(tvb, 0);

  if ((opcode == TFTP_RRQ) ||
      (opcode == TFTP_WRQ) ||
      (opcode == TFTP_DATA) ||
      (opcode == TFTP_ACK) ||
      (opcode == TFTP_ERROR) ||
      (opcode == TFTP_INFO) ||
      (opcode == TFTP_OACK)) {
    dissect_tftp_message(tftp_info, tvb, pinfo, tree);
    return TRUE;
  }
  else {
    return FALSE;
  }
}

static void
dissect_tftp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	conversation_t   *conversation = NULL;
	tftp_conv_info_t *tftp_info;

	/*
	 * The first TFTP packet goes to the TFTP port; the second one
	 * comes from some *other* port, but goes back to the same
	 * IP address and port as the ones from which the first packet
	 * came; all subsequent packets go between those two IP addresses
	 * and ports.
	 *
	 * If this packet went to the TFTP port, we check to see if
	 * there's already a conversation with one address/port pair
	 * matching the source IP address and port of this packet,
	 * the other address matching the destination IP address of this
	 * packet, and any destination port.
	 *
	 * If not, we create one, with its address 1/port 1 pair being
	 * the source address/port of this packet, its address 2 being
	 * the destination address of this packet, and its port 2 being
	 * wildcarded, and give it the TFTP dissector as a dissector.
	 */
	if (value_is_in_range(global_tftp_port_range, pinfo->destport)) {
	  conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst, PT_UDP,
					   pinfo->srcport, 0, NO_PORT_B);
	  if( (conversation == NULL) || (conversation->dissector_handle != tftp_handle) ){
	    conversation = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, PT_UDP,
					    pinfo->srcport, 0, NO_PORT2);
		conversation_set_dissector(conversation, tftp_handle);
	  }
	} else {
	  conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
					   pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
	  if( (conversation == NULL) || (conversation->dissector_handle != tftp_handle) ){
	    conversation = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, PT_UDP,
					    pinfo->destport, pinfo->srcport, 0);
		conversation_set_dissector(conversation, tftp_handle);
	  } else if (conversation->options & NO_PORT_B) {
		if (pinfo->destport == conversation->key_ptr->port1)
		  conversation_set_port2(conversation, pinfo->srcport);
		else
		  return;
	  }
	}
	tftp_info = (tftp_conv_info_t *)conversation_get_proto_data(conversation, proto_tftp);
        if (!tftp_info) {
	  tftp_info = wmem_new(wmem_file_scope(), tftp_conv_info_t);
	  tftp_info->blocksize = 512; /* TFTP default block size */
	  tftp_info->source_file = NULL;
	  tftp_info->destination_file = NULL;
	  conversation_add_proto_data(conversation, proto_tftp, tftp_info);
	}

	dissect_tftp_message(tftp_info, tvb, pinfo, tree);

	return;
}


void
proto_register_tftp(void)
{
  static hf_register_info hf[] = {
    { &hf_tftp_opcode,
      { "Opcode",	      "tftp.opcode",
	FT_UINT16, BASE_DEC, VALS(tftp_opcode_vals), 0x0,
	"TFTP message type", HFILL }},

    { &hf_tftp_source_file,
      { "Source File",	      "tftp.source_file",
	FT_STRINGZ, BASE_NONE, NULL, 0x0,
	"TFTP source file name", HFILL }},

    { &hf_tftp_destination_file,
      { "DESTINATION File",   "tftp.destination_file",
	FT_STRINGZ, BASE_NONE, NULL, 0x0,
	"TFTP source file name", HFILL }},

    { &hf_tftp_transfer_type,
      { "Type",	              "tftp.type",
	FT_STRINGZ, BASE_NONE, NULL, 0x0,
	"TFTP transfer type", HFILL }},

    { &hf_tftp_blocknum,
      { "Block",              "tftp.block",
	FT_UINT16, BASE_DEC, NULL, 0x0,
	"Block number", HFILL }},

    { &hf_tftp_error_code,
      { "Error code",         "tftp.error.code",
	FT_UINT16, BASE_DEC, VALS(tftp_error_code_vals), 0x0,
	"Error code in case of TFTP error message", HFILL }},

    { &hf_tftp_error_string,
      { "Error message",      "tftp.error.message",
	FT_STRINGZ, BASE_NONE, NULL, 0x0,
	"Error string in case of TFTP error message", HFILL }},

    { &hf_tftp_option_name,
      { "Option name",        "tftp.option.name",
	FT_STRINGZ, BASE_NONE, NULL, 0x0,
	NULL, HFILL }},

    { &hf_tftp_option_value,
      { "Option value",       "tftp.option.value",
	FT_STRINGZ, BASE_NONE, NULL, 0x0,
	NULL, HFILL }},

  };
  static gint *ett[] = {
    &ett_tftp,
    &ett_tftp_option,
  };

  static ei_register_info ei[] = {
     { &ei_tftp_blocksize_range, { "tftp.blocksize_range", PI_RESPONSE_CODE, PI_WARN, "TFTP blocksize out of range", EXPFILL }},
  };

  module_t *tftp_module;
  expert_module_t* expert_tftp;

  proto_tftp = proto_register_protocol("Trivial File Transfer Protocol",
				       "TFTP", "tftp");
  proto_register_field_array(proto_tftp, hf, array_length(hf));
  proto_register_subtree_array(ett, array_length(ett));
  expert_tftp = expert_register_protocol(proto_tftp);
  expert_register_field_array(expert_tftp, ei, array_length(ei));

  register_dissector("tftp", dissect_tftp, proto_tftp);

  /* Set default UDP ports */
  range_convert_str(&global_tftp_port_range, UDP_PORT_TFTP_RANGE, MAX_UDP_PORT);

  tftp_module = prefs_register_protocol(proto_tftp, proto_reg_handoff_tftp);
  prefs_register_range_preference(tftp_module, "udp_ports",
				  "TFTP port numbers",
				  "Port numbers used for TFTP traffic "
				  "(default " UDP_PORT_TFTP_RANGE ")",
				  &global_tftp_port_range, MAX_UDP_PORT);
}

void
proto_reg_handoff_tftp(void)
{
  static range_t *tftp_port_range;
  static gboolean tftp_initialized = FALSE;

  if (!tftp_initialized) {
    tftp_handle = find_dissector("tftp");
    data_handle = find_dissector("data");
    heur_dissector_add("stun", dissect_embeddedtftp_heur, proto_tftp);
    tftp_initialized = TRUE;
  } else {
    dissector_add_uint_range("udp.port", tftp_port_range, tftp_handle);
    g_free(tftp_port_range);
  }

  tftp_port_range = range_copy(global_tftp_port_range);
  dissector_add_uint_range("udp.port", tftp_port_range, tftp_handle);
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 2
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=2 tabstop=8 expandtab:
 * :indentSize=2:tabSize=8:noTabs=true:
 */