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.20 2000/12/24 20:33:04 guy Exp $
10 * Ethereal - Network traffic analyzer
11 * By Gerald Combs <gerald@zing.org>
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 #define UDP_PORT_TFTP 69
67 static const value_string tftp_opcode_vals[] = {
68 { RRQ, "Read Request" },
69 { WRQ, "Write Request" },
70 { DATA, "Data Packet" },
71 { ACK, "Acknowledgement" },
72 { ERROR, "Error Code" },
73 { OACK, "Option Acknowledgement" },
77 static const value_string tftp_error_code_vals[] = {
79 { 1, "File not found" },
80 { 2, "Access violation" },
81 { 3, "Disk full or allocation exceeded" },
82 { 4, "Illegal TFTP Operation" },
83 { 5, "Unknown transfer ID" },
84 { 6, "File already exists" },
85 { 7, "No such user" },
89 static void tftp_dissect_options(tvbuff_t *tvb, int offset, proto_tree *tree);
90 static gint tftp_strnlen(tvbuff_t *tvb, gint offset);
93 dissect_tftp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
95 proto_tree *tftp_tree;
97 conversation_t *conversation;
102 CHECK_DISPLAY_AS_DATA(proto_tftp, tvb, pinfo, tree);
104 pinfo->current_proto = "TFTP";
107 * The first TFTP packet goes to the TFTP port; the second one
108 * comes from some *other* port, but goes back to the same
109 * IP address and port as the ones from which the first packet
110 * came; all subsequent packets go between those two IP addresses
113 * If this packet went to the TFTP port, we check to see if
114 * there's already a conversation with the source IP address
115 * and port of this packet, the destination IP address of this
116 * packet, and any destination UDP port. If not, we create
117 * one, with a wildcard UDP port, and give it the TFTP dissector
120 if (pinfo->destport == UDP_PORT_TFTP) {
121 conversation = find_conversation(&pinfo->src, &pinfo->dst, PT_UDP,
122 pinfo->srcport, 0, NO_DST_PORT);
123 if (conversation == NULL) {
124 conversation = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP,
125 pinfo->srcport, 0, NULL,
127 conversation_set_dissector(conversation, dissect_tftp);
131 if (check_col(pinfo->fd, COL_PROTOCOL))
132 col_set_str(pinfo->fd, COL_PROTOCOL, "TFTP");
134 opcode = tvb_get_ntohs(tvb, offset);
136 if (check_col(pinfo->fd, COL_INFO)) {
138 col_add_fstr(pinfo->fd, COL_INFO, "TFTP %s",
139 val_to_str(opcode, tftp_opcode_vals, "Unknown (0x%04x)"));
145 ti = proto_tree_add_item(tree, proto_tftp, tvb, offset,
146 tvb_length_remaining(tvb, offset), FALSE);
147 tftp_tree = proto_item_add_subtree(ti, ett_tftp);
149 proto_tree_add_uint(tftp_tree, hf_tftp_opcode, tvb,
155 i1 = tftp_strnlen(tvb, offset);
156 proto_tree_add_item(tftp_tree, hf_tftp_source_file,
157 tvb, offset, i1, FALSE);
160 i1 = tftp_strnlen(tvb, offset);
161 ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
162 tvb, offset, i1, FALSE);
165 tftp_dissect_options(tvb, offset, tftp_tree);
168 i1 = tftp_strnlen(tvb, offset);
169 proto_tree_add_item(tftp_tree, hf_tftp_destination_file,
170 tvb, offset, i1, FALSE);
173 i1 = tftp_strnlen(tvb, offset);
174 ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
175 tvb, offset, i1, FALSE);
178 tftp_dissect_options(tvb, offset, tftp_tree);
181 proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
185 proto_tree_add_text(tftp_tree, tvb, offset, tvb_length_remaining(tvb, offset),
186 "Data (%d bytes)", tvb_length_remaining(tvb, offset));
189 proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
193 proto_tree_add_item(tftp_tree, hf_tftp_error_code, tvb, offset, 2,
197 i1 = tftp_strnlen(tvb, offset);
198 proto_tree_add_item(tftp_tree, hf_tftp_error_string, tvb, offset,
202 tftp_dissect_options(tvb, offset, tftp_tree);
205 proto_tree_add_text(tftp_tree, tvb, offset, tvb_length_remaining(tvb, offset),
206 "Data (%d bytes)", tvb_length_remaining(tvb, offset));
214 tftp_dissect_options(tvbuff_t *tvb, int offset, proto_tree *tree)
216 int option_len, value_len;
219 while (tvb_offset_exists(tvb, offset)) {
220 option_len = tftp_strnlen(tvb, offset); /* length of option */
221 value_offset = offset + option_len;
222 value_len = tftp_strnlen(tvb, value_offset); /* length of value */
223 proto_tree_add_text(tree, tvb, offset, option_len+value_len,
224 "Option: %.*s = %.*s",
225 option_len - 1, tvb_get_ptr(tvb, offset, option_len - 1),
226 value_len - 1, tvb_get_ptr(tvb, value_offset, value_len - 1));
227 offset += option_len + value_len;
232 * Find the length of a null-terminated string in a TFTP packet; if we
233 * don't find the '\0', throw an exception, as it means that either
234 * we didn't capture enough of the frame, or the frame is malformed.
236 * XXX - we'd like to know *which* exception to throw....
238 * XXX - this should probably be a standard tvbuff accessor.
240 * Add 1 to the resulting length, so that it includes the '\0'.
243 tftp_strnlen(tvbuff_t *tvb, gint offset)
247 len = tvb_strnlen(tvb, offset, -1);
250 * No '\0' found before the end of the tvbuff; throw an
259 proto_register_tftp(void)
261 static hf_register_info hf[] = {
263 { "Opcode", "tftp.opcode",
264 FT_UINT16, BASE_DEC, VALS(tftp_opcode_vals), 0x0,
265 "TFTP message type" }},
267 { &hf_tftp_source_file,
268 { "Source File", "tftp.source_file",
269 FT_STRINGZ, BASE_DEC, NULL, 0x0,
270 "TFTP source file name" }},
272 { &hf_tftp_destination_file,
273 { "DESTINATION File", "tftp.destination_file",
274 FT_STRINGZ, BASE_DEC, NULL, 0x0,
275 "TFTP source file name" }},
277 { &hf_tftp_transfer_type,
278 { "Type", "tftp.type",
279 FT_STRINGZ, BASE_DEC, NULL, 0x0,
280 "TFTP transfer type" }},
283 { "Block", "tftp.block",
284 FT_UINT16, BASE_DEC, NULL, 0x0,
287 { &hf_tftp_error_code,
288 { "Error code", "tftp.error.code",
289 FT_UINT16, BASE_DEC, VALS(tftp_error_code_vals), 0x0,
290 "Error code in case of TFTP error message" }},
292 { &hf_tftp_error_string,
293 { "Error message", "tftp.error.message",
294 FT_STRINGZ, BASE_DEC, NULL, 0x0,
295 "Error string in case of TFTP error message" }}
297 static gint *ett[] = {
301 proto_tftp = proto_register_protocol("Trivial File Transfer Protocol", "tftp");
302 proto_register_field_array(proto_tftp, hf, array_length(hf));
303 proto_register_subtree_array(ett, array_length(ett));
307 proto_reg_handoff_tftp(void)
309 dissector_add("udp.port", UDP_PORT_TFTP, dissect_tftp);