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.17 2000/11/13 08:58:17 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);
104 * The first TFTP packet goes to the TFTP port; the second one
105 * comes from some *other* port, but goes back to the same
106 * IP address and port as the ones from which the first packet
107 * came; all subsequent packets go between those two IP addresses
110 * If this packet went to the TFTP port, we check to see if
111 * there's already a conversation with the source IP address
112 * and port of this packet, the destination IP address of this
113 * packet, and any destination UDP port. If not, we create
114 * one, with a wildcard UDP port, and give it the TFTP dissector
117 if (pinfo->destport == UDP_PORT_TFTP) {
118 conversation = find_conversation(&pinfo->src, &pinfo->dst, PT_UDP,
119 pinfo->srcport, 0, NO_DST_PORT);
120 if (conversation == NULL) {
121 conversation = conversation_new(&pinfo->src, &pinfo->dst, PT_UDP,
122 pinfo->srcport, 0, NULL,
124 conversation_set_dissector(conversation, dissect_tftp);
128 if (check_col(pinfo->fd, COL_PROTOCOL))
129 col_add_str(pinfo->fd, COL_PROTOCOL, "TFTP");
131 opcode = tvb_get_ntohs(tvb, offset);
133 if (check_col(pinfo->fd, COL_INFO)) {
135 col_add_fstr(pinfo->fd, COL_INFO, "TFTP %s",
136 val_to_str(opcode, tftp_opcode_vals, "Unknown (0x%04x)"));
142 ti = proto_tree_add_item(tree, proto_tftp, tvb, offset,
143 tvb_length_remaining(tvb, offset), FALSE);
144 tftp_tree = proto_item_add_subtree(ti, ett_tftp);
146 proto_tree_add_uint(tftp_tree, hf_tftp_opcode, tvb,
152 i1 = tvb_strnlen(tvb, offset, -1);
153 proto_tree_add_item(tftp_tree, hf_tftp_source_file,
154 tvb, offset, i1 + 1, FALSE);
157 i1 = tvb_strnlen(tvb, offset, -1);
158 ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
159 tvb, offset, i1 + 1, FALSE);
162 tftp_dissect_options(tvb, offset, tftp_tree);
165 i1 = tvb_strnlen(tvb, offset, -1);
166 proto_tree_add_item(tftp_tree, hf_tftp_destination_file,
167 tvb, offset, i1 + 1, FALSE);
170 i1 = tvb_strnlen(tvb, offset, -1);
171 ti = proto_tree_add_item(tftp_tree, hf_tftp_transfer_type,
172 tvb, offset, i1 + 1, FALSE);
175 tftp_dissect_options(tvb, offset, tftp_tree);
178 proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
182 proto_tree_add_text(tftp_tree, tvb, offset, tvb_length_remaining(tvb, offset),
183 "Data (%d bytes)", tvb_length_remaining(tvb, offset));
186 proto_tree_add_item(tftp_tree, hf_tftp_blocknum, tvb, offset, 2,
190 proto_tree_add_item(tftp_tree, hf_tftp_error_code, tvb, offset, 2,
194 i1 = tvb_strnlen(tvb, offset, -1);
195 proto_tree_add_item(tftp_tree, hf_tftp_error_string, tvb, offset,
199 tftp_dissect_options(tvb, offset, tftp_tree);
202 proto_tree_add_text(tftp_tree, tvb, offset, tvb_length_remaining(tvb, offset),
203 "Data (%d bytes)", tvb_length_remaining(tvb, offset));
211 tftp_dissect_options(tvbuff_t *tvb, int offset, proto_tree *tree)
215 while (tvb_offset_exists(tvb, offset)) {
216 i1 = tvb_strnlen(tvb, offset, -1); /* length of option */
217 i2 = tvb_strnlen(tvb, offset+i1+1, -1); /* length of value */
218 proto_tree_add_text(tree, tvb, offset, i1+i2+2,
219 "Option: %.*s = %.*s",
220 i1, tvb_get_ptr(tvb, offset, i1),
221 i2, tvb_get_ptr(tvb, offset+i1+1, i2));
222 offset += i1 + i2 + 2;
227 proto_register_tftp(void)
229 static hf_register_info hf[] = {
231 { "Opcode", "tftp.opcode",
232 FT_UINT16, BASE_DEC, VALS(tftp_opcode_vals), 0x0,
233 "TFTP message type" }},
235 { &hf_tftp_source_file,
236 { "Source File", "tftp.source_file",
237 FT_STRINGZ, BASE_DEC, NULL, 0x0,
238 "TFTP source file name" }},
240 { &hf_tftp_destination_file,
241 { "DESTINATION File", "tftp.destination_file",
242 FT_STRINGZ, BASE_DEC, NULL, 0x0,
243 "TFTP source file name" }},
245 { &hf_tftp_transfer_type,
246 { "Type", "tftp.type",
247 FT_STRINGZ, BASE_DEC, NULL, 0x0,
248 "TFTP transfer type" }},
251 { "Block", "tftp.block",
252 FT_UINT16, BASE_DEC, NULL, 0x0,
255 { &hf_tftp_error_code,
256 { "Error code", "tftp.error.code",
257 FT_UINT16, BASE_DEC, VALS(tftp_error_code_vals), 0x0,
258 "Error code in case of TFTP error message" }},
260 { &hf_tftp_error_string,
261 { "Error message", "tftp.error.message",
262 FT_STRINGZ, BASE_DEC, NULL, 0x0,
263 "Error string in case of TFTP error message" }}
265 static gint *ett[] = {
269 proto_tftp = proto_register_protocol("Trivial File Transfer Protocol", "tftp");
270 proto_register_field_array(proto_tftp, hf, array_length(hf));
271 proto_register_subtree_array(ett, array_length(ett));
275 proto_reg_handoff_tftp(void)
277 dissector_add("udp.port", UDP_PORT_TFTP, dissect_tftp);