2 * Routines for tftp packet dissection
4 * Richard Sharpe <rsharpe@ns.aus.com>
5 * Craig Newell <CraigN@cheque.uq.edu.au>
6 * RFC2347 TFTP Option Extension
7 * Joerg Mayer (see AUTHORS file)
8 * RFC2348 TFTP Blocksize Option
12 * Wireshark - Network traffic analyzer
13 * By Gerald Combs <gerald@wireshark.org>
14 * Copyright 1998 Gerald Combs
16 * Copied from packet-bootp.c
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version 2
21 * of the License, or (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 * RFC 1350: THE TFTP PROTOCOL (REVISION 2)
35 * RFC 2090: TFTP Multicast Option
36 * (not yet implemented)
37 * RFC 2347: TFTP Option Extension
38 * RFC 2348: TFTP Blocksize Option
39 * RFC 2349: TFTP Timeout Interval and Transfer Size Options
40 * (not yet implemented)
49 #include <epan/packet.h>
50 #include <epan/conversation.h>
51 #include <epan/emem.h>
52 #include <epan/expert.h>
53 #include <epan/range.h>
54 #include <epan/prefs.h>
56 /* Things we may want to remember for a whole conversation */
57 typedef struct _tftp_conv_info_t {
59 gchar *source_file, *destination_file;
63 static int proto_tftp = -1;
64 static int hf_tftp_opcode = -1;
65 static int hf_tftp_source_file = -1;
66 static int hf_tftp_destination_file = -1;
67 static int hf_tftp_transfer_type = -1;
68 static int hf_tftp_blocknum = -1;
69 static int hf_tftp_error_code = -1;
70 static int hf_tftp_error_string = -1;
71 static int hf_tftp_option_name = -1;
72 static int hf_tftp_option_value = -1;
74 static gint ett_tftp = -1;
75 static gint ett_tftp_option = -1;
77 static dissector_handle_t tftp_handle;
79 #define UDP_PORT_TFTP_RANGE "69"
81 void proto_reg_handoff_tftp (void);
83 /* User definable values */
84 static range_t *global_tftp_port_range;
94 static const value_string tftp_opcode_vals[] = {
95 { TFTP_RRQ, "Read Request" },
96 { TFTP_WRQ, "Write Request" },
97 { TFTP_DATA, "Data Packet" },
98 { TFTP_ACK, "Acknowledgement" },
99 { TFTP_ERROR, "Error Code" },
100 { TFTP_OACK, "Option Acknowledgement" },
101 { TFTP_INFO, "Information (MSDP)" },
105 static const value_string tftp_error_code_vals[] = {
106 { 0, "Not defined" },
107 { 1, "File not found" },
108 { 2, "Access violation" },
109 { 3, "Disk full or allocation exceeded" },
110 { 4, "Illegal TFTP Operation" },
111 { 5, "Unknown transfer ID" }, /* Does not cause termination */
112 { 6, "File already exists" },
113 { 7, "No such user" },
114 { 8, "Option negotiation failed" },
119 tftp_dissect_options(tvbuff_t *tvb, packet_info *pinfo, int offset,
120 proto_tree *tree, guint16 opcode, tftp_conv_info_t *tftp_info)
122 int option_len, value_len;
124 const char *optionname;
125 const char *optionvalue;
126 proto_item *opt_item;
127 proto_tree *opt_tree;
129 while (tvb_offset_exists(tvb, offset)) {
130 option_len = tvb_strsize(tvb, offset); /* length of option */
131 value_offset = offset + option_len;
132 value_len = tvb_strsize(tvb, value_offset); /* length of value */
133 optionname = tvb_format_text(tvb, offset, option_len);
134 optionvalue = tvb_format_text(tvb, value_offset, value_len);
135 opt_item = proto_tree_add_text(tree, tvb, offset, option_len+value_len,
136 "Option: %s = %s", optionname, optionvalue);
138 opt_tree = proto_item_add_subtree(opt_item, ett_tftp_option);
139 proto_tree_add_item(opt_tree, hf_tftp_option_name, tvb, offset,
141 proto_tree_add_item(opt_tree, hf_tftp_option_value, tvb, value_offset,
144 offset += option_len + value_len;
146 if (check_col(pinfo->cinfo, COL_INFO)) {
147 col_append_fstr(pinfo->cinfo, COL_INFO, ", %s=%s",
148 optionname, optionvalue);
151 /* Special code to handle individual options */
152 if (!g_ascii_strcasecmp((const char *)optionname, "blksize") &&
153 opcode == TFTP_OACK) {
154 gint blocksize = strtol((const char *)optionvalue, NULL, 10);
155 if (blocksize < 8 || blocksize > 65464) {
156 expert_add_info_format(pinfo, NULL, PI_RESPONSE_CODE,
157 PI_WARN, "TFTP blocksize out of range");
160 tftp_info->blocksize = blocksize;
166 static void dissect_tftp_message(tftp_conv_info_t *tftp_info,
167 tvbuff_t *tvb, packet_info *pinfo,
170 proto_tree *tftp_tree = NULL;
179 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TFTP");
181 opcode = tvb_get_ntohs(tvb, offset);
183 if (check_col(pinfo->cinfo, COL_INFO)) {
185 col_add_str(pinfo->cinfo, COL_INFO,
186 val_to_str(opcode, tftp_opcode_vals, "Unknown (0x%04x)"));
192 ti = proto_tree_add_item(tree, proto_tftp, tvb, offset, -1, FALSE);
193 tftp_tree = proto_item_add_subtree(ti, ett_tftp);
195 if(tftp_info->source_file) {
196 ti = proto_tree_add_string(tftp_tree, hf_tftp_source_file, tvb,
197 0, 0, tftp_info->source_file);
198 PROTO_ITEM_SET_GENERATED(ti);
201 if(tftp_info->destination_file) {
202 ti = proto_tree_add_string(tftp_tree, hf_tftp_destination_file, tvb,
203 0, 0, tftp_info->destination_file);
204 PROTO_ITEM_SET_GENERATED(ti);
207 proto_tree_add_uint(tftp_tree, hf_tftp_opcode, tvb,
215 i1 = tvb_strsize(tvb, offset);
217 proto_tree_add_item(tftp_tree, hf_tftp_source_file,
218 tvb, offset, i1, FALSE);
221 tftp_info->source_file = tvb_get_seasonal_string(tvb, offset, i1);
223 if (check_col(pinfo->cinfo, COL_INFO)) {
224 col_append_fstr(pinfo->cinfo, COL_INFO, ", File: %s",
225 tvb_format_text(tvb, offset, i1));
229 i1 = tvb_strsize(tvb, offset);
231 ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
232 tvb, offset, i1, FALSE);
234 if (check_col(pinfo->cinfo, COL_INFO)) {
235 col_append_fstr(pinfo->cinfo, COL_INFO, ", Transfer type: %s",
236 tvb_format_text(tvb, offset, i1));
241 tftp_dissect_options(tvb, pinfo, offset, tftp_tree,
246 i1 = tvb_strsize(tvb, offset);
248 proto_tree_add_item(tftp_tree, hf_tftp_destination_file,
249 tvb, offset, i1, FALSE);
252 tftp_info->destination_file =
253 tvb_get_seasonal_string(tvb, offset, i1);
255 if (check_col(pinfo->cinfo, COL_INFO)) {
256 col_append_fstr(pinfo->cinfo, COL_INFO, ", File: %s",
257 tvb_format_text(tvb, offset, i1));
261 i1 = tvb_strsize(tvb, offset);
263 ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
264 tvb, offset, i1, FALSE);
267 if (check_col(pinfo->cinfo, COL_INFO)) {
268 col_append_fstr(pinfo->cinfo, COL_INFO, ", Transfer type: %s",
269 tvb_format_text(tvb, offset, i1));
274 tftp_dissect_options(tvb, pinfo, offset, tftp_tree,
280 tftp_dissect_options(tvb, pinfo, offset, tftp_tree,
285 blocknum = tvb_get_ntohs(tvb, offset);
287 proto_tree_add_uint(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
292 bytes = tvb_reported_length_remaining(tvb, offset);
294 if (check_col(pinfo->cinfo, COL_INFO)) {
295 col_append_fstr(pinfo->cinfo, COL_INFO, ", Block: %i%s",
297 (bytes < tftp_info->blocksize)?" (last)":"" );
302 proto_tree_add_text(tftp_tree, tvb, offset, -1,
303 "Data (%d bytes)", bytes);
309 blocknum = tvb_get_ntohs(tvb, offset);
311 proto_tree_add_uint(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
314 if (check_col(pinfo->cinfo, COL_INFO)) {
315 col_append_fstr(pinfo->cinfo, COL_INFO, ", Block: %i",
321 error = tvb_get_ntohs(tvb, offset);
323 proto_tree_add_uint(tftp_tree, hf_tftp_error_code, tvb, offset, 2,
326 if (check_col(pinfo->cinfo, COL_INFO)) {
327 col_append_fstr(pinfo->cinfo, COL_INFO, ", Code: %s",
328 val_to_str(error, tftp_error_code_vals, "Unknown (%u)"));
332 i1 = tvb_strsize(tvb, offset);
334 proto_tree_add_item(tftp_tree, hf_tftp_error_string, tvb, offset,
337 if (check_col(pinfo->cinfo, COL_INFO)) {
338 col_append_fstr(pinfo->cinfo, COL_INFO, ", Message: %s",
339 tvb_format_text(tvb, offset, i1));
341 expert_add_info_format(pinfo, NULL, PI_RESPONSE_CODE,
342 PI_NOTE, "TFTP blocksize out of range");
347 tftp_dissect_options(tvb, pinfo, offset, tftp_tree,
353 proto_tree_add_text(tftp_tree, tvb, offset, -1,
354 "Data (%d bytes)", tvb_reported_length_remaining(tvb, offset));
363 dissect_embeddedtftp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
365 /* Used to dissect TFTP packets where one can not assume
366 that the TFTP is the only protocol used by that port, and
367 that TFTP may not be carried by UDP */
368 conversation_t *conversation = NULL;
370 tftp_conv_info_t *tftp_info;
372 conversation = find_or_create_conversation(pinfo);
374 tftp_info = conversation_get_proto_data(conversation, proto_tftp);
376 tftp_info = se_alloc(sizeof(tftp_conv_info_t));
377 tftp_info->blocksize = 512; /* TFTP default block size */
378 tftp_info->source_file = NULL;
379 tftp_info->destination_file = NULL;
380 conversation_add_proto_data(conversation, proto_tftp, tftp_info);
383 opcode = tvb_get_ntohs(tvb, 0);
385 if ((opcode == TFTP_RRQ) ||
386 (opcode == TFTP_WRQ) ||
387 (opcode == TFTP_DATA) ||
388 (opcode == TFTP_ACK) ||
389 (opcode == TFTP_ERROR) ||
390 (opcode == TFTP_INFO) ||
391 (opcode == TFTP_OACK)) {
392 dissect_tftp_message(tftp_info, tvb, pinfo, tree);
401 dissect_tftp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
403 conversation_t *conversation = NULL;
404 tftp_conv_info_t *tftp_info;
407 * The first TFTP packet goes to the TFTP port; the second one
408 * comes from some *other* port, but goes back to the same
409 * IP address and port as the ones from which the first packet
410 * came; all subsequent packets go between those two IP addresses
413 * If this packet went to the TFTP port, we check to see if
414 * there's already a conversation with one address/port pair
415 * matching the source IP address and port of this packet,
416 * the other address matching the destination IP address of this
417 * packet, and any destination port.
419 * If not, we create one, with its address 1/port 1 pair being
420 * the source address/port of this packet, its address 2 being
421 * the destination address of this packet, and its port 2 being
422 * wildcarded, and give it the TFTP dissector as a dissector.
424 if (value_is_in_range(global_tftp_port_range, pinfo->destport)) {
425 conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst, PT_UDP,
426 pinfo->srcport, 0, NO_PORT_B);
427 if( (conversation == NULL) || (conversation->dissector_handle!=tftp_handle) ){
428 conversation = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, PT_UDP,
429 pinfo->srcport, 0, NO_PORT2);
430 conversation_set_dissector(conversation, tftp_handle);
433 conversation = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
434 pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
435 if( (conversation == NULL) || (conversation->dissector_handle!=tftp_handle) ){
436 conversation = conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, PT_UDP,
437 pinfo->destport, pinfo->srcport, 0);
438 conversation_set_dissector(conversation, tftp_handle);
441 tftp_info = conversation_get_proto_data(conversation, proto_tftp);
443 tftp_info = se_alloc(sizeof(tftp_conv_info_t));
444 tftp_info->blocksize = 512; /* TFTP default block size */
445 tftp_info->source_file = NULL;
446 tftp_info->destination_file = NULL;
447 conversation_add_proto_data(conversation, proto_tftp, tftp_info);
451 dissect_tftp_message(tftp_info, tvb, pinfo, tree);
458 proto_register_tftp(void)
460 static hf_register_info hf[] = {
462 { "Opcode", "tftp.opcode",
463 FT_UINT16, BASE_DEC, VALS(tftp_opcode_vals), 0x0,
464 "TFTP message type", HFILL }},
466 { &hf_tftp_source_file,
467 { "Source File", "tftp.source_file",
468 FT_STRINGZ, BASE_NONE, NULL, 0x0,
469 "TFTP source file name", HFILL }},
471 { &hf_tftp_destination_file,
472 { "DESTINATION File", "tftp.destination_file",
473 FT_STRINGZ, BASE_NONE, NULL, 0x0,
474 "TFTP source file name", HFILL }},
476 { &hf_tftp_transfer_type,
477 { "Type", "tftp.type",
478 FT_STRINGZ, BASE_NONE, NULL, 0x0,
479 "TFTP transfer type", HFILL }},
482 { "Block", "tftp.block",
483 FT_UINT16, BASE_DEC, NULL, 0x0,
484 "Block number", HFILL }},
486 { &hf_tftp_error_code,
487 { "Error code", "tftp.error.code",
488 FT_UINT16, BASE_DEC, VALS(tftp_error_code_vals), 0x0,
489 "Error code in case of TFTP error message", HFILL }},
491 { &hf_tftp_error_string,
492 { "Error message", "tftp.error.message",
493 FT_STRINGZ, BASE_NONE, NULL, 0x0,
494 "Error string in case of TFTP error message", HFILL }},
496 { &hf_tftp_option_name,
497 { "Option name", "tftp.option.name",
498 FT_STRINGZ, BASE_NONE, NULL, 0x0,
501 { &hf_tftp_option_value,
502 { "Option value", "tftp.option.value",
503 FT_STRINGZ, BASE_NONE, NULL, 0x0,
507 static gint *ett[] = {
512 module_t *tftp_module;
514 proto_tftp = proto_register_protocol("Trivial File Transfer Protocol",
516 proto_register_field_array(proto_tftp, hf, array_length(hf));
517 proto_register_subtree_array(ett, array_length(ett));
519 register_dissector("tftp", dissect_tftp, proto_tftp);
521 /* Set default UDP ports */
522 range_convert_str (&global_tftp_port_range, UDP_PORT_TFTP_RANGE, MAX_UDP_PORT);
524 tftp_module = prefs_register_protocol (proto_tftp, proto_reg_handoff_tftp);
525 prefs_register_range_preference (tftp_module, "udp_ports",
527 "Port numbers used for TFTP traffic "
528 "(default " UDP_PORT_TFTP_RANGE ")",
529 &global_tftp_port_range, MAX_UDP_PORT);
532 static void range_delete_callback (guint32 port)
534 dissector_delete ("udp.port", port, tftp_handle);
537 static void range_add_callback (guint32 port)
539 dissector_add ("udp.port", port, tftp_handle);
543 proto_reg_handoff_tftp(void)
545 static range_t *tftp_port_range;
546 static gboolean tftp_initialized = FALSE;
548 if (!tftp_initialized) {
549 tftp_handle = find_dissector("tftp");
550 heur_dissector_add("stun", dissect_embeddedtftp_heur, proto_tftp);
551 tftp_initialized = TRUE;
553 range_foreach (tftp_port_range, range_delete_callback);
554 g_free (tftp_port_range);
557 tftp_port_range = range_copy (global_tftp_port_range);
558 range_foreach (tftp_port_range, range_add_callback);