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.18 2002/03/25 20:17:09 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;
120 * If we're reassembling segmented TPKT PDUs, empty the COL_INFO
121 * column, so subdissectors can append information
122 * without having to worry about emptying the column.
124 * We use "col_add_str()" because the subdissector
125 * might be appending information to the column, in
126 * which case we'd have to zero the buffer out explicitly
129 if (tpkt_desegment && check_col(pinfo->cinfo, COL_INFO))
130 col_add_str(pinfo->cinfo, COL_INFO, "");
132 while (tvb_reported_length_remaining(tvb, offset) != 0) {
133 length_remaining = tvb_length_remaining(tvb, offset);
136 * Can we do reassembly?
138 if (desegment && pinfo->can_desegment) {
140 * Yes - is the TPKT header split across segment
143 if (length_remaining < 4) {
145 * Yes. Tell the TCP dissector where
146 * the data for this message starts in
147 * the data it handed us, and how many
148 * more bytes we need, and return.
150 pinfo->desegment_offset = offset;
151 pinfo->desegment_len = 4 - length_remaining;
157 * Get the length from the TPKT header.
159 data_len = tvb_get_ntohs(tvb, offset + 2);
162 * Can we do reassembly?
164 if (desegment && pinfo->can_desegment) {
166 * Yes - is the payload split across segment
169 if (length_remaining < data_len) {
171 * Yes. Tell the TCP dissector where
172 * the data for this message starts in
173 * the data it handed us, and how many
174 * more bytes we need, and return.
176 pinfo->desegment_offset = offset;
177 pinfo->desegment_len =
178 data_len - length_remaining;
184 * Dissect the TPKT header.
185 * Save and restore "pinfo->current_proto".
187 saved_proto = pinfo->current_proto;
188 pinfo->current_proto = "TPKT";
190 if (check_col(pinfo->cinfo, COL_PROTOCOL))
191 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TPKT");
193 * Don't add the TPKT header information if we're
194 * reassembling segmented TPKT PDUs or if this
195 * PDU isn't reassembled.
197 * XXX - the first is so that subdissectors can append
198 * information without getting TPKT stuff in the middle;
201 if (!tpkt_desegment && !pinfo->fragmented
202 && check_col(pinfo->cinfo, COL_INFO)) {
203 col_add_fstr(pinfo->cinfo, COL_INFO,
204 "TPKT Data length = %u", data_len);
208 ti = proto_tree_add_item(tree, proto_tpkt, tvb,
210 tpkt_tree = proto_item_add_subtree(ti, ett_tpkt);
213 proto_tree_add_item(tpkt_tree, hf_tpkt_version, tvb,
217 proto_tree_add_item(tpkt_tree, hf_tpkt_reserved, tvb,
218 offset + 1, 1, FALSE);
221 proto_tree_add_uint(tpkt_tree, hf_tpkt_length, tvb,
222 offset + 2, 2, data_len);
224 pinfo->current_proto = saved_proto;
226 /* Skip the TPKT header. */
231 * Construct a tvbuff containing the amount of the payload
232 * we have available. Make its reported length the
233 * amount of data in this TPKT packet.
235 * XXX - if reassembly isn't enabled. the subdissector
236 * will throw a BoundsError exception, rather than a
237 * ReportedBoundsError exception. We really want
238 * a tvbuff where the length is "length", the reported
239 * length is "plen + 2", and the "if the snapshot length
240 * were infinite" length were the minimum of the
241 * reported length of the tvbuff handed to us and "plen+2",
242 * with a new type of exception thrown if the offset is
243 * within the reported length but beyond that third length,
244 * with that exception getting the "Unreassembled Packet"
247 length = length_remaining - 4;
248 if (length > data_len)
250 next_tvb = tvb_new_subset(tvb, offset, length, data_len);
253 * Call the subdissector.
255 * Catch the ReportedBoundsError exception; if this
256 * particular message happens to get a ReportedBoundsError
257 * exception, that doesn't mean that we should stop
258 * dissecting TPKT messages within this frame or chunk
259 * of reassembled data.
261 * If it gets a BoundsError, we can stop, as there's nothing
262 * more to see, so we just re-throw it.
265 call_dissector(subdissector_handle, next_tvb, pinfo,
271 CATCH(ReportedBoundsError) {
272 show_reported_bounds_error(tvb, pinfo, tree);
284 * Dissect RFC 1006 TPKT, which wraps a TPKT header around an OSI TP
288 dissect_tpkt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
290 dissect_tpkt_encap(tvb, pinfo, tree, tpkt_desegment, osi_tp_handle);
294 proto_register_tpkt(void)
296 static hf_register_info hf[] =
340 module_t *tpkt_module;
342 proto_tpkt = proto_register_protocol("TPKT", "TPKT", "tpkt");
343 proto_register_field_array(proto_tpkt, hf, array_length(hf));
344 proto_register_subtree_array(ett, array_length(ett));
346 tpkt_module = prefs_register_protocol(proto_tpkt, NULL);
347 prefs_register_bool_preference(tpkt_module, "desegment",
348 "Desegment all TPKT messages spanning multiple TCP segments",
349 "Whether the TPKT dissector should desegment all messages spanning multiple TCP segments",
354 proto_reg_handoff_tpkt(void)
356 dissector_handle_t tpkt_handle;
358 osi_tp_handle = find_dissector("ositp");
359 tpkt_handle = create_dissector_handle(dissect_tpkt, proto_tpkt);
360 dissector_add("tcp.port", TCP_PORT_TPKT, tpkt_handle);