3 * Routine to check for RFC 1006 TPKT header and to dissect TPKT header
4 * Copyright 2000, Philips Electronics N.V.
5 * Andreas Sikkema <h323@ramdyne.nl>
7 * Routine to dissect RFC 1006 TPKT packet containing OSI TP PDU
8 * Copyright 2001, Martin Thomas <Martin_A_Thomas@yahoo.com>
12 * Wireshark - Network traffic analyzer
13 * By Gerald Combs <gerald@wireshark.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37 #include <epan/packet.h>
38 #include <epan/exceptions.h>
39 #include <epan/prefs.h>
40 #include <epan/show_exception.h>
42 #include "packet-tpkt.h"
44 /* TPKT header fields */
45 static int proto_tpkt
= -1;
46 static protocol_t
*proto_tpkt_ptr
;
47 static int hf_tpkt_version
= -1;
48 static int hf_tpkt_reserved
= -1;
49 static int hf_tpkt_length
= -1;
52 /* TPKT fields defining a sub tree */
53 static gint ett_tpkt
= -1;
55 /* desegmentation of OSI over TPKT over TCP */
56 static gboolean tpkt_desegment
= TRUE
;
58 #define TCP_PORT_TPKT 102
60 /* find the dissector for OSI TP (aka COTP) */
61 static dissector_handle_t osi_tp_handle
;
64 * Check whether this could be a TPKT-encapsulated PDU.
65 * Returns -1 if it's not, and the PDU length from the TPKT header
68 * "min_len" is the minimum length of the PDU; the length field in the
69 * TPKT header must be at least "4+min_len" in order for this to be a
70 * valid TPKT PDU for the protocol in question.
73 is_tpkt(tvbuff_t
*tvb
, int min_len
)
78 * If TPKT is disabled, don't dissect it, just return -1, meaning
81 if (!proto_is_protocol_enabled(proto_tpkt_ptr
))
84 /* There should at least be 4 bytes left in the frame */
85 if (tvb_length(tvb
) < 4)
86 return -1; /* there aren't */
89 * The first octet should be 3 and the second one should be 0
90 * The H.323 implementers guide suggests that this might not
91 * always be the case....
93 if (!(tvb_get_guint8(tvb
, 0) == 3 && tvb_get_guint8(tvb
, 1) == 0))
94 return -1; /* they're not */
97 * Get the length from the TPKT header. Make sure it's large
100 pkt_len
= tvb_get_ntohs(tvb
, 2);
101 if (pkt_len
< 4 + min_len
)
102 return -1; /* it's not */
105 * Return the length from the header.
110 is_asciitpkt(tvbuff_t
*tvb
)
114 * If TPKT is disabled, don't dissect it, just return -1, meaning
117 if (!proto_is_protocol_enabled(proto_tpkt_ptr
))
120 /* There should at least be 8 bytes left in the frame */
121 if (!tvb_bytes_exist(tvb
, 0, 8))
122 return -1; /* there aren't */
125 * The first four octets should be ASCII
127 for (count
= 0; count
<=7 ; count
++)
129 if(!isalnum(tvb_get_guint8(tvb
,count
)))
139 parseLengthText ( guint8
* pTpktData
)
142 const guint8
* pData
= pTpktData
;
143 int bitvalue
= 0, count1
= 3;
145 for (count
= 0; count
<= 3; count
++)
147 if (('0' <= *(pData
+ count
)) && (*(pData
+ count
) <= '9'))
148 bitvalue
= *(pData
+ count
) - 48;
149 else if (('a' <= *(pData
+ count
)) && (*(pData
+ count
) <= 'f' ))
150 bitvalue
= *(pData
+ count
) - 87;
151 else if (('A' <= *(pData
+ count
)) && (*(pData
+ count
) <= 'F' ))
152 bitvalue
= *(pData
+ count
) - 55;
154 value
+= bitvalue
<< (4*count1
);
160 parseVersionText ( guint8
* pTpktData
)
163 guint8
* pData
= pTpktData
;
164 int bitvalue
= 0, count1
= 1;
166 for (count
= 0; count
<= 1; count
++)
168 if (('0' <= *(pData
+ count
)) && (*(pData
+ count
) <= '9'))
169 bitvalue
= *(pData
+ count
) - 48;
170 else if (('a' <= *(pData
+ count
)) && (*(pData
+ count
) <= 'f' ))
171 bitvalue
= *(pData
+ count
) - 87;
172 else if (('A' <= *(pData
+ count
)) && (*(pData
+ count
) <= 'F' ))
173 bitvalue
= *(pData
+ count
) - 55;
175 value
+= bitvalue
<< (4*count1
);
182 parseReservedText ( guint8
* pTpktData
)
185 guint8
* pData
= pTpktData
;
186 int bitvalue
= 0, count1
= 1;
188 for (count
= 0; count
<= 1; count
++)
190 if (('0' <= *(pData
+ count
)) && (*(pData
+ count
) <= '9'))
191 bitvalue
= *(pData
+ count
) - 48;
192 else if (('a' <= *(pData
+ count
)) && (*(pData
+ count
) <= 'f' ))
193 bitvalue
= *(pData
+ count
) - 87;
194 else if (('A' <= *(pData
+ count
)) && (*(pData
+ count
) <= 'F' ))
195 bitvalue
= *(pData
+ count
) - 55;
197 value
+= bitvalue
<< (4*count1
);
204 * Dissect ASCII TPKT-encapsulated data in a TCP stream.
207 dissect_asciitpkt(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
,
208 dissector_handle_t subdissector_handle
)
210 proto_item
*ti
= NULL
;
211 proto_tree
*tpkt_tree
= NULL
;
212 volatile int offset
= 0;
213 int length_remaining
;
215 volatile int mgcp_packet_len
= 0;
216 int mgcp_version
= 0;
217 int mgcp_reserved
= 0;
219 tvbuff_t
*volatile next_tvb
;
220 const char *saved_proto
;
225 * If we're reassembling segmented TPKT PDUs, empty the COL_INFO
226 * column, so subdissectors can append information
227 * without having to worry about emptying the column.
229 * We use "col_add_str()" because the subdissector
230 * might be appending information to the column, in
231 * which case we'd have to zero the buffer out explicitly
235 col_set_str(pinfo
->cinfo
, COL_INFO
, "");
237 while (tvb_reported_length_remaining(tvb
, offset
) != 0) {
239 * Is the first byte of this putative TPKT header
240 * a valid TPKT version number, i.e. 3?
242 if (tvb_get_guint8(tvb
, offset
) != 48) {
244 * No, so don't assume this is a TPKT header;
245 * we might be in the middle of TPKT data,
246 * so don't get the length and don't try to
249 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "TPKT");
250 col_set_str(pinfo
->cinfo
, COL_INFO
, "Continuation");
252 ti
= proto_tree_add_item(tree
, proto_tpkt
, tvb
,
254 tpkt_tree
= proto_item_add_subtree(ti
, ett_tpkt
);
255 proto_item_set_text(ti
, "TPKT");
257 proto_tree_add_text(tpkt_tree
, tvb
, offset
, -1,
258 "Continuation data");
263 length_remaining
= tvb_length_remaining(tvb
, offset
);
266 * Get the length from the TPKT header.
269 tvb_memcpy(tvb
, (guint8
*)string
, offset
, 2);
270 mgcp_version
= parseVersionText(string
);
271 tvb_memcpy(tvb
, (guint8
*)string
, offset
+2, 2);
272 mgcp_reserved
= parseReservedText(string
);
273 tvb_memcpy(tvb
, (guint8
*)string
, offset
+ 4, 4);
274 mgcp_packet_len
= parseLengthText(string
);
275 data_len
= mgcp_packet_len
;
278 * Dissect the TPKT header.
279 * Save and restore "pinfo->current_proto".
281 saved_proto
= pinfo
->current_proto
;
282 pinfo
->current_proto
= "TPKT";
284 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "TPKT");
286 * Don't add the TPKT header information if we're
287 * reassembling segmented TPKT PDUs or if this
288 * PDU isn't reassembled.
290 * XXX - the first is so that subdissectors can append
291 * information without getting TPKT stuff in the middle;
294 if (!tpkt_desegment
&& !pinfo
->fragmented
) {
295 col_add_fstr(pinfo
->cinfo
, COL_INFO
,
296 "TPKT Data length = %u", data_len
);
300 ti
= proto_tree_add_item(tree
, proto_tpkt
, tvb
,
302 tpkt_tree
= proto_item_add_subtree(ti
, ett_tpkt
);
303 proto_item_set_text(ti
, "TPKT");
306 proto_tree_add_uint(tpkt_tree
, hf_tpkt_version
, tvb
,
307 offset
, 2, mgcp_version
);
310 proto_tree_add_uint(tpkt_tree
, hf_tpkt_reserved
, tvb
,
311 offset
+ 2, 2, mgcp_reserved
);
314 proto_tree_add_uint(tpkt_tree
, hf_tpkt_length
, tvb
,
315 offset
+ 4, 4, mgcp_packet_len
);
317 pinfo
->current_proto
= saved_proto
;
319 /* Skip the TPKT header. */
320 offset
+= TEXT_LAYER_LENGTH
;
321 length
= length_remaining
- TEXT_LAYER_LENGTH
;
322 if (length
> data_len
)
325 next_tvb
= tvb_new_subset(tvb
, offset
,length
, data_len
);
328 * Call the subdissector.
330 * If it gets an error that means there's no point in
331 * dissecting any more TPKT messages, rethrow the
332 * exception in question.
334 * If it gets any other error, report it and continue, as that
335 * means that TPKT message got an error, but that doesn't mean
336 * we should stop dissecting TPKT messages within this frame
337 * or chunk of reassembled data.
339 pd_save
= pinfo
->private_data
;
341 call_dissector(subdissector_handle
, next_tvb
, pinfo
,
344 CATCH_NONFATAL_ERRORS
{
345 /* Restore the private_data structure in case one of the
346 * called dissectors modified it (and, due to the exception,
347 * was unable to restore it).
349 pinfo
->private_data
= pd_save
;
351 show_exception(tvb
, pinfo
, tree
, EXCEPT_CODE
, GET_MESSAGE
);
363 * Dissect TPKT-encapsulated data in a TCP stream.
366 dissect_tpkt_encap(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
,
367 gboolean desegment
, dissector_handle_t subdissector_handle
)
369 proto_item
*ti
= NULL
;
370 proto_tree
*tpkt_tree
= NULL
;
371 volatile int offset
= 0;
372 int length_remaining
;
375 tvbuff_t
*volatile next_tvb
;
376 const char *saved_proto
;
380 * If we're reassembling segmented TPKT PDUs, empty the COL_INFO
381 * column, so subdissectors can append information
382 * without having to worry about emptying the column.
384 * We use "col_add_str()" because the subdissector
385 * might be appending information to the column, in
386 * which case we'd have to zero the buffer out explicitly
390 col_set_str(pinfo
->cinfo
, COL_INFO
, "");
392 while (tvb_reported_length_remaining(tvb
, offset
) != 0) {
394 * Is the first byte of this putative TPKT header
395 * a valid TPKT version number, i.e. 3?
397 if (tvb_get_guint8(tvb
, offset
) != 3) {
399 * No, so don't assume this is a TPKT header;
400 * we might be in the middle of TPKT data,
401 * so don't get the length and don't try to
404 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "TPKT");
405 col_set_str(pinfo
->cinfo
, COL_INFO
, "Continuation");
407 ti
= proto_tree_add_item(tree
, proto_tpkt
, tvb
,
409 tpkt_tree
= proto_item_add_subtree(ti
, ett_tpkt
);
410 proto_item_set_text(ti
, "TPKT");
412 proto_tree_add_text(tpkt_tree
, tvb
, offset
, -1,
413 "Continuation data");
418 length_remaining
= tvb_length_remaining(tvb
, offset
);
421 * Can we do reassembly?
423 if (desegment
&& pinfo
->can_desegment
) {
425 * Yes - is the TPKT header split across segment
428 if (length_remaining
< 4) {
430 * Yes. Tell the TCP dissector where the data
431 * for this message starts in the data it
432 * handed us and that we need "some more data."
433 * Don't tell it exactly how many bytes we need
434 * because if/when we ask for even more (after
435 * the header) that will break reassembly.
437 pinfo
->desegment_offset
= offset
;
438 pinfo
->desegment_len
= DESEGMENT_ONE_MORE_SEGMENT
;
444 * Get the length from the TPKT header.
446 data_len
= tvb_get_ntohs(tvb
, offset
+ 2);
449 * Can we do reassembly?
451 if (desegment
&& pinfo
->can_desegment
) {
453 * Yes - is the payload split across segment
456 if (length_remaining
< data_len
) {
458 * Yes. Tell the TCP dissector where
459 * the data for this message starts in
460 * the data it handed us, and how many
461 * more bytes we need, and return.
463 pinfo
->desegment_offset
= offset
;
464 pinfo
->desegment_len
=
465 data_len
- length_remaining
;
471 * Dissect the TPKT header.
472 * Save and restore "pinfo->current_proto".
474 saved_proto
= pinfo
->current_proto
;
475 pinfo
->current_proto
= "TPKT";
477 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "TPKT");
479 * Don't add the TPKT header information if we're
480 * reassembling segmented TPKT PDUs or if this
481 * PDU isn't reassembled.
483 * XXX - the first is so that subdissectors can append
484 * information without getting TPKT stuff in the middle;
487 if (!desegment
&& !pinfo
->fragmented
) {
488 col_add_fstr(pinfo
->cinfo
, COL_INFO
,
489 "TPKT Data length = %u", data_len
);
493 ti
= proto_tree_add_item(tree
, proto_tpkt
, tvb
,
495 tpkt_tree
= proto_item_add_subtree(ti
, ett_tpkt
);
496 proto_item_set_text(ti
, "TPKT");
499 proto_tree_add_item(tpkt_tree
, hf_tpkt_version
, tvb
,
500 offset
, 1, ENC_BIG_ENDIAN
);
501 proto_item_append_text(ti
, ", Version: 3");
504 proto_tree_add_item(tpkt_tree
, hf_tpkt_reserved
, tvb
,
505 offset
+ 1, 1, ENC_BIG_ENDIAN
);
508 proto_tree_add_uint(tpkt_tree
, hf_tpkt_length
, tvb
,
509 offset
+ 2, 2, data_len
);
510 proto_item_append_text(ti
, ", Length: %u", data_len
);
512 pinfo
->current_proto
= saved_proto
;
514 /* Skip the TPKT header. */
519 * Construct a tvbuff containing the amount of the payload
520 * we have available. Make its reported length the
521 * amount of data in this TPKT packet.
523 * XXX - if reassembly isn't enabled. the subdissector
524 * will throw a BoundsError exception, rather than a
525 * ReportedBoundsError exception. We really want
526 * a tvbuff where the length is "length", the reported
527 * length is "plen + 2", and the "if the snapshot length
528 * were infinite" length were the minimum of the
529 * reported length of the tvbuff handed to us and "plen+2",
530 * with a new type of exception thrown if the offset is
531 * within the reported length but beyond that third length,
532 * with that exception getting the "Unreassembled Packet"
535 length
= length_remaining
- 4;
536 if (length
> data_len
)
538 next_tvb
= tvb_new_subset(tvb
, offset
, length
, data_len
);
541 * Call the subdissector.
543 * If it gets an error that means there's no point in
544 * dissecting any more TPKT messages, rethrow the
545 * exception in question.
547 * If it gets any other error, report it and continue,
548 * as that means that TPKT message got an error, but
549 * that doesn't mean we should stop dissecting TPKT
550 * messages within this frame or chunk of reassembled
553 pd_save
= pinfo
->private_data
;
555 call_dissector(subdissector_handle
, next_tvb
, pinfo
,
558 CATCH_NONFATAL_ERRORS
{
559 /* Restore the private_data structure in case one of the
560 * called dissectors modified it (and, due to the exception,
561 * was unable to restore it).
563 pinfo
->private_data
= pd_save
;
565 show_exception(tvb
, pinfo
, tree
, EXCEPT_CODE
, GET_MESSAGE
);
577 * Dissect RFC 1006 TPKT, which wraps a TPKT header around an OSI TP
581 dissect_tpkt(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
)
583 dissect_tpkt_encap(tvb
, pinfo
, tree
, tpkt_desegment
, osi_tp_handle
);
587 * Dissect ASCII TPKT, which wraps a ASCII TPKT header around an OSI TP
592 dissect_ascii_tpkt(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
)
594 dissect_asciitpkt(tvb
, pinfo
, tree
, osi_tp_handle
);
599 proto_register_tpkt(void)
601 static hf_register_info hf
[] =
612 "Version, only version 3 is defined", HFILL
624 "Reserved, should be 0", HFILL
636 "Length of data unit, including this header", HFILL
645 module_t
*tpkt_module
;
647 proto_tpkt
= proto_register_protocol("TPKT - ISO on TCP - RFC1006", "TPKT", "tpkt");
648 proto_tpkt_ptr
= find_protocol_by_id(proto_tpkt
);
649 proto_register_field_array(proto_tpkt
, hf
, array_length(hf
));
650 proto_register_subtree_array(ett
, array_length(ett
));
651 register_dissector("tpkt", dissect_tpkt
, proto_tpkt
);
653 tpkt_module
= prefs_register_protocol(proto_tpkt
, NULL
);
654 prefs_register_bool_preference(tpkt_module
, "desegment",
655 "Reassemble TPKT messages spanning multiple TCP segments",
656 "Whether the TPKT dissector should reassemble messages spanning multiple TCP segments. "
657 "To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings.",
662 proto_reg_handoff_tpkt(void)
664 dissector_handle_t tpkt_handle
;
666 osi_tp_handle
= find_dissector("ositp");
667 tpkt_handle
= find_dissector("tpkt");
668 dissector_add_uint("tcp.port", TCP_PORT_TPKT
, tpkt_handle
);
671 tpkt_ascii_handle = create_dissector_handle(dissect_ascii_tpkt, proto_tpkt);
672 dissector_add_uint("tcp.port", TCP_PORT_TPKT, tpkt_ascii_handle);