3 * Routine to check for RFC 1006 TPKT header and to dissect TPKT header
4 * Copyright 2000, Philips Electronics N.V.
5 * Andreas Sikkema <andreas.sikkema@philips.com>
7 * Routine to dissect RFC 1006 TPKT packet containing OSI TP PDU
8 * Copyright 2001, Martin Thomas <Martin_A_Thomas@yahoo.com>
10 * $Id: packet-tpkt.c,v 1.17 2002/03/05 22:15:21 guy Exp $
12 * Ethereal - Network traffic analyzer
13 * By Gerald Combs <gerald@ethereal.com>
14 * Copyright 1998 Gerald Combs
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>
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
42 #ifdef HAVE_NETINET_IN_H
43 # include <netinet/in.h>
49 #include "packet-tpkt.h"
50 #include "packet-frame.h"
53 /* TPKT header fields */
54 static int proto_tpkt = -1;
55 static int hf_tpkt_version = -1;
56 static int hf_tpkt_reserved = -1;
57 static int hf_tpkt_length = -1;
59 /* TPKT fields defining a sub tree */
60 static gint ett_tpkt = -1;
62 /* desegmentation of OSI over TPKT over TCP */
63 static gboolean tpkt_desegment = TRUE;
65 #define TCP_PORT_TPKT 102
67 /* find the dissector for OSI TP (aka COTP) */
68 static dissector_handle_t osi_tp_handle;
71 * Check whether this could be a TPKT-encapsulated PDU.
72 * Returns -1 if it's not, and the PDU length from the TPKT header
76 is_tpkt(tvbuff_t *tvb)
79 * If TPKT is disabled, don't dissect it, just return -1, meaning
82 if (!proto_is_protocol_enabled(proto_tpkt))
85 /* There should at least be 4 bytes left in the frame */
86 if (!tvb_bytes_exist(tvb, 0, 4))
87 return -1; /* there aren't */
90 * The first octet should be 3 and the second one should be 0
91 * The H.323 implementers guide suggests that this might not
92 * always be the case....
94 if (!(tvb_get_guint8(tvb, 0) == 3 && tvb_get_guint8(tvb, 1) == 0))
95 return -1; /* They're not */
98 * Return the length from the TPKT header.
100 return tvb_get_ntohs(tvb, 2);
104 * Dissect TPKT-encapsulated data in a TCP stream.
107 dissect_tpkt_encap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
108 gboolean desegment, dissector_handle_t subdissector_handle)
110 proto_item *ti = NULL;
111 proto_tree *tpkt_tree = NULL;
112 volatile int offset = 0;
113 int length_remaining;
117 const char *saved_proto;
119 while (tvb_reported_length_remaining(tvb, offset) != 0) {
120 length_remaining = tvb_length_remaining(tvb, offset);
123 * Can we do reassembly?
125 if (desegment && pinfo->can_desegment) {
127 * Yes - is the TPKT header split across segment
130 if (length_remaining < 4) {
132 * Yes. Tell the TCP dissector where
133 * the data for this message starts in
134 * the data it handed us, and how many
135 * more bytes we need, and return.
137 pinfo->desegment_offset = offset;
138 pinfo->desegment_len = 4 - length_remaining;
144 * Get the length from the TPKT header.
146 data_len = tvb_get_ntohs(tvb, offset + 2);
149 * Can we do reassembly?
151 if (desegment && pinfo->can_desegment) {
153 * Yes - is the payload split across segment
156 if (length_remaining < data_len) {
158 * Yes. Tell the TCP dissector where
159 * the data for this message starts in
160 * the data it handed us, and how many
161 * more bytes we need, and return.
163 pinfo->desegment_offset = offset;
164 pinfo->desegment_len =
165 data_len - length_remaining;
171 * Dissect the TPKT header.
172 * Save and restore "pinfo->current_proto".
174 saved_proto = pinfo->current_proto;
175 pinfo->current_proto = "TPKT";
177 if (check_col(pinfo->cinfo, COL_PROTOCOL))
178 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TPKT");
179 if (check_col(pinfo->cinfo, COL_INFO)) {
180 col_add_fstr(pinfo->cinfo, COL_INFO,
181 "TPKT Data length = %u", data_len);
185 ti = proto_tree_add_item(tree, proto_tpkt, tvb,
187 tpkt_tree = proto_item_add_subtree(ti, ett_tpkt);
190 proto_tree_add_item(tpkt_tree, hf_tpkt_version, tvb,
194 proto_tree_add_item(tpkt_tree, hf_tpkt_reserved, tvb,
195 offset + 1, 1, FALSE);
198 proto_tree_add_uint(tpkt_tree, hf_tpkt_length, tvb,
199 offset + 2, 2, data_len);
201 pinfo->current_proto = saved_proto;
203 /* Skip the TPKT header. */
208 * Construct a tvbuff containing the amount of the payload
209 * we have available. Make its reported length the
210 * amount of data in this TPKT packet.
212 * XXX - if reassembly isn't enabled. the subdissector
213 * will throw a BoundsError exception, rather than a
214 * ReportedBoundsError exception. We really want
215 * a tvbuff where the length is "length", the reported
216 * length is "plen + 2", and the "if the snapshot length
217 * were infinite" length were the minimum of the
218 * reported length of the tvbuff handed to us and "plen+2",
219 * with a new type of exception thrown if the offset is
220 * within the reported length but beyond that third length,
221 * with that exception getting the "Unreassembled Packet"
224 length = length_remaining - 4;
225 if (length > data_len)
227 next_tvb = tvb_new_subset(tvb, offset, length, data_len);
230 * Call the subdissector.
232 * Catch the ReportedBoundsError exception; if this
233 * particular message happens to get a ReportedBoundsError
234 * exception, that doesn't mean that we should stop
235 * dissecting TPKT messages within this frame or chunk
236 * of reassembled data.
238 * If it gets a BoundsError, we can stop, as there's nothing
239 * more to see, so we just re-throw it.
242 call_dissector(subdissector_handle, next_tvb, pinfo,
248 CATCH(ReportedBoundsError) {
249 show_reported_bounds_error(tvb, pinfo, tree);
261 * Dissect RFC 1006 TPKT, which wraps a TPKT header around an OSI TP
265 dissect_tpkt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
267 dissect_tpkt_encap(tvb, pinfo, tree, tpkt_desegment, osi_tp_handle);
271 proto_register_tpkt(void)
273 static hf_register_info hf[] =
317 module_t *tpkt_module;
319 proto_tpkt = proto_register_protocol("TPKT", "TPKT", "tpkt");
320 proto_register_field_array(proto_tpkt, hf, array_length(hf));
321 proto_register_subtree_array(ett, array_length(ett));
323 tpkt_module = prefs_register_protocol(proto_tpkt, NULL);
324 prefs_register_bool_preference(tpkt_module, "desegment",
325 "Desegment all TPKT messages spanning multiple TCP segments",
326 "Whether the TPKT dissector should desegment all messages spanning multiple TCP segments",
331 proto_reg_handoff_tpkt(void)
333 dissector_handle_t tpkt_handle;
335 osi_tp_handle = find_dissector("ositp");
336 tpkt_handle = create_dissector_handle(dissect_tpkt, proto_tpkt);
337 dissector_add("tcp.port", TCP_PORT_TPKT, tpkt_handle);