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.21 2000/12/25 23:48:14 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);
92 dissect_tftp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
94 proto_tree *tftp_tree;
96 conversation_t *conversation;
101 CHECK_DISPLAY_AS_DATA(proto_tftp, tvb, pinfo, tree);
103 pinfo->current_proto = "TFTP";
106 * The first TFTP packet goes to the TFTP port; the second one
107 * comes from some *other* port, but goes back to the same
108 * IP address and port as the ones from which the first packet
109 * came; all subsequent packets go between those two IP addresses
112 * If this packet went to the TFTP port, we check to see if
113 * there's already a conversation with the source IP address
114 * and port of this packet, the destination IP address of this
115 * packet, and any destination UDP port. If not, we create
116 * one, with a wildcard UDP port, and give it the TFTP dissector
119 if (pinfo->destport == UDP_PORT_TFTP) {
120 conversation = find_conversation(&pinfo->src, &pinfo->dst, PT_UDP,
121 pinfo->srcport, 0, NO_DST_PORT);
122 if (conversation == NULL) {
123 conversation = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP,
124 pinfo->srcport, 0, NULL,
126 conversation_set_dissector(conversation, dissect_tftp);
130 if (check_col(pinfo->fd, COL_PROTOCOL))
131 col_set_str(pinfo->fd, COL_PROTOCOL, "TFTP");
133 opcode = tvb_get_ntohs(tvb, offset);
135 if (check_col(pinfo->fd, COL_INFO)) {
137 col_add_fstr(pinfo->fd, COL_INFO, "TFTP %s",
138 val_to_str(opcode, tftp_opcode_vals, "Unknown (0x%04x)"));
144 ti = proto_tree_add_item(tree, proto_tftp, tvb, offset,
145 tvb_length_remaining(tvb, offset), FALSE);
146 tftp_tree = proto_item_add_subtree(ti, ett_tftp);
148 proto_tree_add_uint(tftp_tree, hf_tftp_opcode, tvb,
154 i1 = tvb_strsize(tvb, offset);
155 proto_tree_add_item(tftp_tree, hf_tftp_source_file,
156 tvb, offset, i1, FALSE);
159 i1 = tvb_strsize(tvb, offset);
160 ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
161 tvb, offset, i1, FALSE);
164 tftp_dissect_options(tvb, offset, tftp_tree);
167 i1 = tvb_strsize(tvb, offset);
168 proto_tree_add_item(tftp_tree, hf_tftp_destination_file,
169 tvb, offset, i1, FALSE);
172 i1 = tvb_strsize(tvb, offset);
173 ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
174 tvb, offset, i1, FALSE);
177 tftp_dissect_options(tvb, offset, tftp_tree);
180 proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
184 proto_tree_add_text(tftp_tree, tvb, offset, tvb_length_remaining(tvb, offset),
185 "Data (%d bytes)", tvb_length_remaining(tvb, offset));
188 proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
192 proto_tree_add_item(tftp_tree, hf_tftp_error_code, tvb, offset, 2,
196 i1 = tvb_strsize(tvb, offset);
197 proto_tree_add_item(tftp_tree, hf_tftp_error_string, tvb, offset,
201 tftp_dissect_options(tvb, offset, tftp_tree);
204 proto_tree_add_text(tftp_tree, tvb, offset, tvb_length_remaining(tvb, offset),
205 "Data (%d bytes)", tvb_length_remaining(tvb, offset));
213 tftp_dissect_options(tvbuff_t *tvb, int offset, proto_tree *tree)
215 int option_len, value_len;
218 while (tvb_offset_exists(tvb, offset)) {
219 option_len = tvb_strsize(tvb, offset); /* length of option */
220 value_offset = offset + option_len;
221 value_len = tvb_strsize(tvb, value_offset); /* length of value */
222 proto_tree_add_text(tree, tvb, offset, option_len+value_len,
224 tvb_get_ptr(tvb, offset, option_len),
225 tvb_get_ptr(tvb, value_offset, value_len));
226 offset += option_len + value_len;
231 proto_register_tftp(void)
233 static hf_register_info hf[] = {
235 { "Opcode", "tftp.opcode",
236 FT_UINT16, BASE_DEC, VALS(tftp_opcode_vals), 0x0,
237 "TFTP message type" }},
239 { &hf_tftp_source_file,
240 { "Source File", "tftp.source_file",
241 FT_STRINGZ, BASE_DEC, NULL, 0x0,
242 "TFTP source file name" }},
244 { &hf_tftp_destination_file,
245 { "DESTINATION File", "tftp.destination_file",
246 FT_STRINGZ, BASE_DEC, NULL, 0x0,
247 "TFTP source file name" }},
249 { &hf_tftp_transfer_type,
250 { "Type", "tftp.type",
251 FT_STRINGZ, BASE_DEC, NULL, 0x0,
252 "TFTP transfer type" }},
255 { "Block", "tftp.block",
256 FT_UINT16, BASE_DEC, NULL, 0x0,
259 { &hf_tftp_error_code,
260 { "Error code", "tftp.error.code",
261 FT_UINT16, BASE_DEC, VALS(tftp_error_code_vals), 0x0,
262 "Error code in case of TFTP error message" }},
264 { &hf_tftp_error_string,
265 { "Error message", "tftp.error.message",
266 FT_STRINGZ, BASE_DEC, NULL, 0x0,
267 "Error string in case of TFTP error message" }}
269 static gint *ett[] = {
273 proto_tftp = proto_register_protocol("Trivial File Transfer Protocol", "tftp");
274 proto_register_field_array(proto_tftp, hf, array_length(hf));
275 proto_register_subtree_array(ett, array_length(ett));
279 proto_reg_handoff_tftp(void)
281 dissector_add("udp.port", UDP_PORT_TFTP, dissect_tftp);