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
8 * $Id: packet-tftp.c,v 1.34 2001/12/10 00:25:40 guy Exp $
10 * Ethereal - Network traffic analyzer
11 * By Gerald Combs <gerald@ethereal.com>
12 * Copyright 1998 Gerald Combs
14 * Copied from packet-bootp.c
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
39 #ifdef HAVE_NETINET_IN_H
40 # include <netinet/in.h>
45 #include "conversation.h"
47 static int proto_tftp = -1;
48 static int hf_tftp_opcode = -1;
49 static int hf_tftp_source_file = -1;
50 static int hf_tftp_destination_file = -1;
51 static int hf_tftp_transfer_type = -1;
52 static int hf_tftp_blocknum = -1;
53 static int hf_tftp_error_code = -1;
54 static int hf_tftp_error_string = -1;
56 static gint ett_tftp = -1;
58 static dissector_handle_t tftp_handle;
60 #define UDP_PORT_TFTP 69
69 static const value_string tftp_opcode_vals[] = {
70 { TFTP_RRQ, "Read Request" },
71 { TFTP_WRQ, "Write Request" },
72 { TFTP_DATA, "Data Packet" },
73 { TFTP_ACK, "Acknowledgement" },
74 { TFTP_ERROR, "Error Code" },
75 { TFTP_OACK, "Option Acknowledgement" },
79 static const value_string tftp_error_code_vals[] = {
81 { 1, "File not found" },
82 { 2, "Access violation" },
83 { 3, "Disk full or allocation exceeded" },
84 { 4, "Illegal TFTP Operation" },
85 { 5, "Unknown transfer ID" },
86 { 6, "File already exists" },
87 { 7, "No such user" },
91 static void tftp_dissect_options(tvbuff_t *tvb, int offset, proto_tree *tree);
94 dissect_tftp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
96 proto_tree *tftp_tree = NULL;
98 conversation_t *conversation;
105 * The first TFTP packet goes to the TFTP port; the second one
106 * comes from some *other* port, but goes back to the same
107 * IP address and port as the ones from which the first packet
108 * came; all subsequent packets go between those two IP addresses
111 * If this packet went to the TFTP port, we check to see if
112 * there's already a conversation with one address/port pair
113 * matching the source IP address and port of this packet,
114 * the other address matching the destination IP address of this
115 * packet, and any destination port.
117 * If not, we create one, with its address 1/port 1 pair being
118 * the source address/port of this packet, its address 2 being
119 * the destination address of this packet, and its port 2 being
120 * wildcarded, and give it the TFTP dissector as a dissector.
122 if (pinfo->destport == UDP_PORT_TFTP) {
123 conversation = find_conversation(&pinfo->src, &pinfo->dst, PT_UDP,
124 pinfo->srcport, 0, NO_PORT_B);
125 if (conversation == NULL) {
126 conversation = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP,
127 pinfo->srcport, 0, NO_PORT2);
128 conversation_set_dissector(conversation, tftp_handle);
132 if (check_col(pinfo->cinfo, COL_PROTOCOL))
133 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TFTP");
135 opcode = tvb_get_ntohs(tvb, offset);
137 if (check_col(pinfo->cinfo, COL_INFO)) {
139 col_add_fstr(pinfo->cinfo, COL_INFO, "%s",
140 val_to_str(opcode, tftp_opcode_vals, "Unknown (0x%04x)"));
146 ti = proto_tree_add_item(tree, proto_tftp, tvb, offset,
147 tvb_length_remaining(tvb, offset), FALSE);
148 tftp_tree = proto_item_add_subtree(ti, ett_tftp);
150 proto_tree_add_uint(tftp_tree, hf_tftp_opcode, tvb,
158 i1 = tvb_strsize(tvb, offset);
160 proto_tree_add_item(tftp_tree, hf_tftp_source_file,
161 tvb, offset, i1, FALSE);
163 if (check_col(pinfo->cinfo, COL_INFO)) {
164 col_append_fstr(pinfo->cinfo, COL_INFO, ", File: %s",
165 tvb_get_ptr(tvb, offset, i1));
169 i1 = tvb_strsize(tvb, offset);
171 ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
172 tvb, offset, i1, FALSE);
174 if (check_col(pinfo->cinfo, COL_INFO)) {
175 col_append_fstr(pinfo->cinfo, COL_INFO, ", Transfer type: %s",
176 tvb_get_ptr(tvb, offset, i1));
181 tftp_dissect_options(tvb, offset, tftp_tree);
185 i1 = tvb_strsize(tvb, offset);
187 proto_tree_add_item(tftp_tree, hf_tftp_destination_file,
188 tvb, offset, i1, FALSE);
190 if (check_col(pinfo->cinfo, COL_INFO)) {
191 col_append_fstr(pinfo->cinfo, COL_INFO, ", File: %s",
192 tvb_get_ptr(tvb, offset, i1));
196 i1 = tvb_strsize(tvb, offset);
198 ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
199 tvb, offset, i1, FALSE);
201 if (check_col(pinfo->cinfo, COL_INFO)) {
202 col_append_fstr(pinfo->cinfo, COL_INFO, ", Transfer type: %s",
203 tvb_get_ptr(tvb, offset, i1));
208 tftp_dissect_options(tvb, offset, tftp_tree);
213 proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
216 if (check_col(pinfo->cinfo, COL_INFO)) {
217 col_append_fstr(pinfo->cinfo, COL_INFO, ", Block: %i",
218 tvb_get_ntohs(tvb, offset));
223 proto_tree_add_text(tftp_tree, tvb, offset, tvb_length_remaining(tvb, offset),
224 "Data (%d bytes)", tvb_length_remaining(tvb, offset));
230 proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
233 if (check_col(pinfo->cinfo, COL_INFO)) {
234 col_append_fstr(pinfo->cinfo, COL_INFO, ", Block: %i",
235 tvb_get_ntohs(tvb, offset));
240 error = tvb_get_ntohs(tvb, offset);
242 proto_tree_add_uint(tftp_tree, hf_tftp_error_code, tvb, offset, 2,
245 if (check_col(pinfo->cinfo, COL_INFO)) {
246 col_append_fstr(pinfo->cinfo, COL_INFO, ", Code: %s",
247 val_to_str(error, tftp_error_code_vals, "Unknown (%u)"));
251 i1 = tvb_strsize(tvb, offset);
253 proto_tree_add_item(tftp_tree, hf_tftp_error_string, tvb, offset,
256 if (check_col(pinfo->cinfo, COL_INFO)) {
257 col_append_fstr(pinfo->cinfo, COL_INFO, ", Message: %s",
258 tvb_get_ptr(tvb, offset, i1));
264 tftp_dissect_options(tvb, offset, tftp_tree);
269 proto_tree_add_text(tftp_tree, tvb, offset, tvb_length_remaining(tvb, offset),
270 "Data (%d bytes)", tvb_length_remaining(tvb, offset));
278 tftp_dissect_options(tvbuff_t *tvb, int offset, proto_tree *tree)
280 int option_len, value_len;
283 while (tvb_offset_exists(tvb, offset)) {
284 option_len = tvb_strsize(tvb, offset); /* length of option */
285 value_offset = offset + option_len;
286 value_len = tvb_strsize(tvb, value_offset); /* length of value */
287 proto_tree_add_text(tree, tvb, offset, option_len+value_len,
289 tvb_get_ptr(tvb, offset, option_len),
290 tvb_get_ptr(tvb, value_offset, value_len));
291 offset += option_len + value_len;
296 proto_register_tftp(void)
298 static hf_register_info hf[] = {
300 { "Opcode", "tftp.opcode",
301 FT_UINT16, BASE_DEC, VALS(tftp_opcode_vals), 0x0,
302 "TFTP message type", HFILL }},
304 { &hf_tftp_source_file,
305 { "Source File", "tftp.source_file",
306 FT_STRINGZ, BASE_DEC, NULL, 0x0,
307 "TFTP source file name", HFILL }},
309 { &hf_tftp_destination_file,
310 { "DESTINATION File", "tftp.destination_file",
311 FT_STRINGZ, BASE_DEC, NULL, 0x0,
312 "TFTP source file name", HFILL }},
314 { &hf_tftp_transfer_type,
315 { "Type", "tftp.type",
316 FT_STRINGZ, BASE_DEC, NULL, 0x0,
317 "TFTP transfer type", HFILL }},
320 { "Block", "tftp.block",
321 FT_UINT16, BASE_DEC, NULL, 0x0,
322 "Block number", HFILL }},
324 { &hf_tftp_error_code,
325 { "Error code", "tftp.error.code",
326 FT_UINT16, BASE_DEC, VALS(tftp_error_code_vals), 0x0,
327 "Error code in case of TFTP error message", HFILL }},
329 { &hf_tftp_error_string,
330 { "Error message", "tftp.error.message",
331 FT_STRINGZ, BASE_DEC, NULL, 0x0,
332 "Error string in case of TFTP error message", HFILL }}
334 static gint *ett[] = {
338 proto_tftp = proto_register_protocol("Trivial File Transfer Protocol",
340 proto_register_field_array(proto_tftp, hf, array_length(hf));
341 proto_register_subtree_array(ett, array_length(ett));
343 tftp_handle = create_dissector_handle(dissect_tftp, proto_tftp);
347 proto_reg_handoff_tftp(void)
349 dissector_add("udp.port", UDP_PORT_TFTP, tftp_handle);