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.41 2003/12/24 10:48:13 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.
36 #include <epan/packet.h>
37 #include <epan/conversation.h>
39 static int proto_tftp = -1;
40 static int hf_tftp_opcode = -1;
41 static int hf_tftp_source_file = -1;
42 static int hf_tftp_destination_file = -1;
43 static int hf_tftp_transfer_type = -1;
44 static int hf_tftp_blocknum = -1;
45 static int hf_tftp_error_code = -1;
46 static int hf_tftp_error_string = -1;
48 static gint ett_tftp = -1;
50 static dissector_handle_t tftp_handle;
52 #define UDP_PORT_TFTP 69
61 static const value_string tftp_opcode_vals[] = {
62 { TFTP_RRQ, "Read Request" },
63 { TFTP_WRQ, "Write Request" },
64 { TFTP_DATA, "Data Packet" },
65 { TFTP_ACK, "Acknowledgement" },
66 { TFTP_ERROR, "Error Code" },
67 { TFTP_OACK, "Option Acknowledgement" },
71 static const value_string tftp_error_code_vals[] = {
73 { 1, "File not found" },
74 { 2, "Access violation" },
75 { 3, "Disk full or allocation exceeded" },
76 { 4, "Illegal TFTP Operation" },
77 { 5, "Unknown transfer ID" },
78 { 6, "File already exists" },
79 { 7, "No such user" },
80 { 8, "Option negotiation failed" },
84 static void tftp_dissect_options(tvbuff_t *tvb, int offset, proto_tree *tree);
87 dissect_tftp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
89 proto_tree *tftp_tree = NULL;
91 conversation_t *conversation;
100 * The first TFTP packet goes to the TFTP port; the second one
101 * comes from some *other* port, but goes back to the same
102 * IP address and port as the ones from which the first packet
103 * came; all subsequent packets go between those two IP addresses
106 * If this packet went to the TFTP port, we check to see if
107 * there's already a conversation with one address/port pair
108 * matching the source IP address and port of this packet,
109 * the other address matching the destination IP address of this
110 * packet, and any destination port.
112 * If not, we create one, with its address 1/port 1 pair being
113 * the source address/port of this packet, its address 2 being
114 * the destination address of this packet, and its port 2 being
115 * wildcarded, and give it the TFTP dissector as a dissector.
117 if (pinfo->destport == UDP_PORT_TFTP) {
118 conversation = find_conversation(&pinfo->src, &pinfo->dst, PT_UDP,
119 pinfo->srcport, 0, NO_PORT_B);
120 if (conversation == NULL) {
121 conversation = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP,
122 pinfo->srcport, 0, NO_PORT2);
123 conversation_set_dissector(conversation, tftp_handle);
127 if (check_col(pinfo->cinfo, COL_PROTOCOL))
128 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TFTP");
130 opcode = tvb_get_ntohs(tvb, offset);
132 if (check_col(pinfo->cinfo, COL_INFO)) {
134 col_add_fstr(pinfo->cinfo, COL_INFO, "%s",
135 val_to_str(opcode, tftp_opcode_vals, "Unknown (0x%04x)"));
141 ti = proto_tree_add_item(tree, proto_tftp, tvb, offset, -1, FALSE);
142 tftp_tree = proto_item_add_subtree(ti, ett_tftp);
144 proto_tree_add_uint(tftp_tree, hf_tftp_opcode, tvb,
152 i1 = tvb_strsize(tvb, offset);
154 proto_tree_add_item(tftp_tree, hf_tftp_source_file,
155 tvb, offset, i1, FALSE);
157 if (check_col(pinfo->cinfo, COL_INFO)) {
158 col_append_fstr(pinfo->cinfo, COL_INFO, ", File: %s",
159 tvb_get_ptr(tvb, offset, i1));
163 i1 = tvb_strsize(tvb, offset);
165 ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
166 tvb, offset, i1, FALSE);
168 if (check_col(pinfo->cinfo, COL_INFO)) {
169 col_append_fstr(pinfo->cinfo, COL_INFO, ", Transfer type: %s",
170 tvb_get_ptr(tvb, offset, i1));
175 tftp_dissect_options(tvb, offset, tftp_tree);
179 i1 = tvb_strsize(tvb, offset);
181 proto_tree_add_item(tftp_tree, hf_tftp_destination_file,
182 tvb, offset, i1, FALSE);
184 if (check_col(pinfo->cinfo, COL_INFO)) {
185 col_append_fstr(pinfo->cinfo, COL_INFO, ", File: %s",
186 tvb_get_ptr(tvb, offset, i1));
190 i1 = tvb_strsize(tvb, offset);
192 ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
193 tvb, offset, i1, FALSE);
195 if (check_col(pinfo->cinfo, COL_INFO)) {
196 col_append_fstr(pinfo->cinfo, COL_INFO, ", Transfer type: %s",
197 tvb_get_ptr(tvb, offset, i1));
202 tftp_dissect_options(tvb, offset, tftp_tree);
206 blocknum = tvb_get_ntohs(tvb, offset);
208 proto_tree_add_uint(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
213 bytes = tvb_reported_length_remaining(tvb, offset);
215 if (check_col(pinfo->cinfo, COL_INFO)) {
216 col_append_fstr(pinfo->cinfo, COL_INFO, ", Block: %i%s",
218 (bytes < 512)?" (last)":"" );
223 proto_tree_add_text(tftp_tree, tvb, offset, -1,
224 "Data (%d bytes)", bytes);
230 blocknum = tvb_get_ntohs(tvb, offset);
232 proto_tree_add_uint(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
235 if (check_col(pinfo->cinfo, COL_INFO)) {
236 col_append_fstr(pinfo->cinfo, COL_INFO, ", Block: %i",
242 error = tvb_get_ntohs(tvb, offset);
244 proto_tree_add_uint(tftp_tree, hf_tftp_error_code, tvb, offset, 2,
247 if (check_col(pinfo->cinfo, COL_INFO)) {
248 col_append_fstr(pinfo->cinfo, COL_INFO, ", Code: %s",
249 val_to_str(error, tftp_error_code_vals, "Unknown (%u)"));
253 i1 = tvb_strsize(tvb, offset);
255 proto_tree_add_item(tftp_tree, hf_tftp_error_string, tvb, offset,
258 if (check_col(pinfo->cinfo, COL_INFO)) {
259 col_append_fstr(pinfo->cinfo, COL_INFO, ", Message: %s",
260 tvb_get_ptr(tvb, offset, i1));
266 tftp_dissect_options(tvb, offset, tftp_tree);
271 proto_tree_add_text(tftp_tree, tvb, offset, -1,
272 "Data (%d bytes)", tvb_reported_length_remaining(tvb, offset));
280 tftp_dissect_options(tvbuff_t *tvb, int offset, proto_tree *tree)
282 int option_len, value_len;
285 while (tvb_offset_exists(tvb, offset)) {
286 option_len = tvb_strsize(tvb, offset); /* length of option */
287 value_offset = offset + option_len;
288 value_len = tvb_strsize(tvb, value_offset); /* length of value */
289 proto_tree_add_text(tree, tvb, offset, option_len+value_len,
291 tvb_get_ptr(tvb, offset, option_len),
292 tvb_get_ptr(tvb, value_offset, value_len));
293 offset += option_len + value_len;
298 proto_register_tftp(void)
300 static hf_register_info hf[] = {
302 { "Opcode", "tftp.opcode",
303 FT_UINT16, BASE_DEC, VALS(tftp_opcode_vals), 0x0,
304 "TFTP message type", HFILL }},
306 { &hf_tftp_source_file,
307 { "Source File", "tftp.source_file",
308 FT_STRINGZ, BASE_DEC, NULL, 0x0,
309 "TFTP source file name", HFILL }},
311 { &hf_tftp_destination_file,
312 { "DESTINATION File", "tftp.destination_file",
313 FT_STRINGZ, BASE_DEC, NULL, 0x0,
314 "TFTP source file name", HFILL }},
316 { &hf_tftp_transfer_type,
317 { "Type", "tftp.type",
318 FT_STRINGZ, BASE_DEC, NULL, 0x0,
319 "TFTP transfer type", HFILL }},
322 { "Block", "tftp.block",
323 FT_UINT16, BASE_DEC, NULL, 0x0,
324 "Block number", HFILL }},
326 { &hf_tftp_error_code,
327 { "Error code", "tftp.error.code",
328 FT_UINT16, BASE_DEC, VALS(tftp_error_code_vals), 0x0,
329 "Error code in case of TFTP error message", HFILL }},
331 { &hf_tftp_error_string,
332 { "Error message", "tftp.error.message",
333 FT_STRINGZ, BASE_DEC, NULL, 0x0,
334 "Error string in case of TFTP error message", HFILL }}
336 static gint *ett[] = {
340 proto_tftp = proto_register_protocol("Trivial File Transfer Protocol",
342 proto_register_field_array(proto_tftp, hf, array_length(hf));
343 proto_register_subtree_array(ett, array_length(ett));
345 tftp_handle = create_dissector_handle(dissect_tftp, proto_tftp);
349 proto_reg_handoff_tftp(void)
351 dissector_add("udp.port", UDP_PORT_TFTP, tftp_handle);