2 * Routines for X.25 over TCP dissection (RFC 1613)
4 * Copyright 2000, Paul Ionescu <paul@acorp.ro>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #include <epan/packet.h>
36 #include "packet-tcp.h"
37 #include <epan/prefs.h>
39 #define TCP_PORT_XOT 1998
40 #define XOT_HEADER_LENGTH 4
42 #define XOT_PVC_SETUP 0xF5
44 /* Some X25 macros from packet-x25.c - some adapted code as well below */
45 #define X25_MIN_HEADER_LENGTH 3
46 #define X25_MIN_M128_HEADER_LENGTH 4
47 #define X25_NONDATA_BIT 0x01
48 #define PACKET_IS_DATA(type) (!(type & X25_NONDATA_BIT))
49 #define X25_MBIT_MOD8 0x10
50 #define X25_MBIT_MOD128 0x01
52 static const value_string vals_x25_type[] = {
53 { XOT_PVC_SETUP, "PVC Setup" },
57 static const value_string xot_pvc_status_vals[] = {
58 { 0x00, "Waiting to connect" },
60 { 0x08, "Destination disconnected" },
61 { 0x09, "PVC/TCP connection refused" },
62 { 0x0A, "PVC/TCP routing error" },
63 { 0x0B, "PVC/TCP connect timed out" },
65 { 0x10, "Trying to connect via TCP" },
66 { 0x11, "Awaiting PVC-SETUP reply" },
67 { 0x12, "Connected" },
68 { 0x13, "No such destination interface" },
69 { 0x14, "Destination interface is not up" },
70 { 0x15, "Non-X.25 destination interface" },
71 { 0x16, "No such destination PVC" },
72 { 0x17, "Destination PVC configuration mismatch" },
73 { 0x18, "Mismatched flow control values" },
74 { 0x19, "Can't support flow control values" },
75 { 0x1A, "PVC setup protocol error" },
80 static dissector_handle_t x25_handle;
82 static gint proto_xot = -1;
83 static gint ett_xot = -1;
84 static gint hf_xot_version = -1;
85 static gint hf_xot_length = -1;
87 static gint hf_x25_gfi = -1;
88 static gint hf_x25_lcn = -1;
89 static gint hf_x25_type = -1;
91 static gint hf_xot_pvc_version = -1;
92 static gint hf_xot_pvc_status = -1;
93 static gint hf_xot_pvc_init_itf_name_len = -1;
94 static gint hf_xot_pvc_init_lcn = -1;
95 static gint hf_xot_pvc_resp_itf_name_len = -1;
96 static gint hf_xot_pvc_resp_lcn = -1;
97 static gint hf_xot_pvc_send_inc_window = -1;
98 static gint hf_xot_pvc_send_out_window = -1;
99 static gint hf_xot_pvc_send_inc_pkt_size = -1;
100 static gint hf_xot_pvc_send_out_pkt_size = -1;
101 static gint hf_xot_pvc_init_itf_name = -1;
102 static gint hf_xot_pvc_resp_itf_name = -1;
104 /* desegmentation of X.25 over multiple TCP */
105 static gboolean xot_desegment = TRUE;
106 /* desegmentation of X.25 packet sequences */
107 static gboolean x25_desegment = FALSE;
109 static guint get_xot_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
112 int remain = tvb_length_remaining(tvb, offset);
113 if ( remain < XOT_HEADER_LENGTH){
114 /* We did not get the data we asked for, use up what we can */
119 * Get the length of the X.25-over-TCP packet.
121 plen = tvb_get_ntohs(tvb, offset + 2);
122 return XOT_HEADER_LENGTH + plen;
125 static guint get_xot_pdu_len_mult(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
127 int offset_before = offset; /* offset where we start this test */
128 int offset_next = offset + XOT_HEADER_LENGTH + X25_MIN_HEADER_LENGTH;
131 while (tvb_len = tvb_length_remaining(tvb, offset), tvb_len>0){
137 int offset_x25 = offset + XOT_HEADER_LENGTH;
139 /* Minimum where next starts */
140 offset_next = offset_x25 + X25_MIN_HEADER_LENGTH;
142 if (tvb_len < XOT_HEADER_LENGTH) {
143 return offset_next-offset_before;
147 * Get the length of the current X.25-over-TCP packet.
149 plen = get_xot_pdu_len(pinfo, tvb, offset);
150 offset_next = offset + plen;
152 /* Make sure we have enough data */
154 return offset_next-offset_before;
157 /*Some minor code copied from packet-x25.c */
158 bytes0_1 = tvb_get_ntohs(tvb, offset_x25+0);
159 pkt_type = tvb_get_guint8(tvb, offset_x25+2);
161 /* If this is the first packet and it is not data, no sequence needed */
162 if (offset == offset_before && !PACKET_IS_DATA(pkt_type)) {
163 return offset_next-offset_before;
166 /* Check for data, there can be X25 control packets in the X25 data */
167 if (PACKET_IS_DATA(pkt_type)){
168 modulo = ((bytes0_1 & 0x2000) ? 128 : 8);
170 m_bit_set = pkt_type & X25_MBIT_MOD8;
172 m_bit_set = tvb_get_guint8(tvb, offset_x25+3) & X25_MBIT_MOD128;
176 /* We are done with this sequence when the mbit is no longer set */
177 return offset_next-offset_before;
180 offset = offset_next;
181 offset_next += XOT_HEADER_LENGTH + X25_MIN_HEADER_LENGTH;
184 /* not enough data */
185 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
186 return offset_next - offset_before;
189 static void dissect_xot_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
195 proto_item *ti = NULL;
196 proto_tree *xot_tree = NULL;
200 * Dissect the X.25-over-TCP packet.
202 col_set_str(pinfo->cinfo, COL_PROTOCOL, "XOT");
203 version = tvb_get_ntohs(tvb, offset + 0);
204 plen = tvb_get_ntohs(tvb, offset + 2);
205 if (check_col(pinfo->cinfo, COL_INFO))
206 col_add_fstr(pinfo->cinfo, COL_INFO, "XOT Version = %u, size = %u",
208 if (check_col(pinfo->cinfo, COL_INFO) && offset == 0 &&
209 tvb_length_remaining(tvb, offset) > XOT_HEADER_LENGTH + plen )
210 col_append_fstr(pinfo->cinfo, COL_INFO, " TotX25: %d",
211 tvb_length_remaining(tvb, offset));
214 ti = proto_tree_add_protocol_format(tree, proto_xot, tvb, offset, XOT_HEADER_LENGTH,
216 xot_tree = proto_item_add_subtree(ti, ett_xot);
218 proto_tree_add_uint(xot_tree, hf_xot_version, tvb, offset, 2, version);
219 proto_tree_add_uint(xot_tree, hf_xot_length, tvb, offset + 2, 2, plen);
222 offset += XOT_HEADER_LENGTH;
224 * Construct a tvbuff containing the amount of the payload we have
225 * available. Make its reported length the amount of data in the
226 * X.25-over-TCP packet.
228 if (plen >= X25_MIN_HEADER_LENGTH) {
229 pkt_type = tvb_get_guint8(tvb, offset + 2);
230 if (pkt_type == XOT_PVC_SETUP) {
231 guint init_itf_name_len, resp_itf_name_len, pkt_size;
232 gint hdr_offset = offset;
234 col_set_str(pinfo->cinfo, COL_INFO, "XOT PVC Setup");
235 proto_item_set_len(ti, XOT_HEADER_LENGTH + plen);
237 /* These fields are in overlay with packet-x25.c */
238 proto_tree_add_item(xot_tree, hf_x25_gfi, tvb, hdr_offset, 2, FALSE);
239 proto_tree_add_item(xot_tree, hf_x25_lcn, tvb, hdr_offset, 2, FALSE);
241 proto_tree_add_item(xot_tree, hf_x25_type, tvb, hdr_offset, 1, FALSE);
244 proto_tree_add_item(xot_tree, hf_xot_pvc_version, tvb, hdr_offset, 1, FALSE);
246 proto_tree_add_item(xot_tree, hf_xot_pvc_status, tvb, hdr_offset, 1, FALSE);
248 proto_tree_add_item(xot_tree, hf_xot_pvc_init_itf_name_len, tvb, hdr_offset, 1, FALSE);
249 init_itf_name_len = tvb_get_guint8(tvb, hdr_offset);
251 proto_tree_add_item(xot_tree, hf_xot_pvc_init_lcn, tvb, hdr_offset, 2, FALSE);
253 proto_tree_add_item(xot_tree, hf_xot_pvc_resp_itf_name_len, tvb, hdr_offset, 1, FALSE);
254 resp_itf_name_len = tvb_get_guint8(tvb, hdr_offset);
256 proto_tree_add_item(xot_tree, hf_xot_pvc_resp_lcn, tvb, hdr_offset, 2, FALSE);
258 proto_tree_add_item(xot_tree, hf_xot_pvc_send_inc_window, tvb, hdr_offset, 1, FALSE);
260 proto_tree_add_item(xot_tree, hf_xot_pvc_send_out_window, tvb, hdr_offset, 1, FALSE);
262 pkt_size = 1 << tvb_get_guint8(tvb, hdr_offset);
263 proto_tree_add_uint(xot_tree, hf_xot_pvc_send_inc_pkt_size, tvb, hdr_offset, 1, pkt_size);
265 pkt_size = 1 << tvb_get_guint8(tvb, hdr_offset);
266 proto_tree_add_uint(xot_tree, hf_xot_pvc_send_out_pkt_size, tvb, hdr_offset, 1, pkt_size);
268 proto_tree_add_item(xot_tree, hf_xot_pvc_init_itf_name, tvb, hdr_offset, init_itf_name_len, FALSE);
269 hdr_offset += init_itf_name_len;
270 proto_tree_add_item(xot_tree, hf_xot_pvc_resp_itf_name, tvb, hdr_offset, resp_itf_name_len, FALSE);
272 next_tvb = tvb_new_subset(tvb, offset,
273 MIN(plen, tvb_length_remaining(tvb, offset)), plen);
274 call_dissector(x25_handle, next_tvb, pinfo, tree);
280 static void dissect_xot_mult(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
283 int len = get_xot_pdu_len_mult(pinfo, tvb, offset);
285 int offset_max = offset+MIN(len,tvb_length_remaining(tvb, offset));
287 proto_tree *xot_tree;
290 /* Special header to show segments */
291 ti = proto_tree_add_protocol_format(tree, proto_xot, tvb, offset, offset_max-offset,
292 "X.25 over TCP - X.25 Sequence");
293 xot_tree = proto_item_add_subtree(ti, ett_xot);
294 proto_tree_add_uint(xot_tree, hf_xot_length, tvb, offset, offset_max, len);
297 while (offset <= offset_max - XOT_HEADER_LENGTH){
298 int plen = get_xot_pdu_len(pinfo, tvb, offset);
299 next_tvb = tvb_new_subset(tvb, offset,plen, plen);
300 /*MIN(plen,tvb_length_remaining(tvb, offset)),plen*/
302 dissect_xot_pdu(next_tvb, pinfo, tree);
306 static int dissect_xot_tcp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
308 int tvb_len = tvb_length(tvb);
311 if (tvb_len >= 2 && tvb_get_ntohs(tvb,0) != XOT_VERSION) {
315 if (!x25_desegment || !xot_desegment){
316 tcp_dissect_pdus(tvb, pinfo, tree, xot_desegment,
320 len=get_xot_pdu_len(pinfo, tvb, 0);
322 /* Use length version that "peeks" into X25, possibly several XOT packets */
323 tcp_dissect_pdus(tvb, pinfo, tree, xot_desegment,
325 get_xot_pdu_len_mult,
327 len=get_xot_pdu_len_mult(pinfo, tvb, 0);
329 /*As tcp_dissect_pdus will not report the success/failure, we have to compute
331 if (len < XOT_HEADER_LENGTH) {
332 /* TCP has reported bounds error */
334 } else if (tvb_len < XOT_HEADER_LENGTH) {
335 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
336 len=tvb_len - XOT_HEADER_LENGTH; /* bytes missing */
337 } else if (tvb_len < len) {
339 /* As the "fixed_len" is not fixed here, just request new segments */
340 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
342 pinfo->desegment_len = len - tvb_len;
344 len=tvb_len - len; /* bytes missing */
349 /* Register the protocol with Wireshark */
351 proto_register_xot(void)
353 static hf_register_info hf[] = {
355 { "Version", "xot.version", FT_UINT16, BASE_DEC,
356 NULL, 0, "Version of X.25 over TCP protocol", HFILL }},
359 { "Length", "xot.length", FT_UINT16, BASE_DEC,
360 NULL, 0, "Length of X.25 over TCP packet", HFILL }},
361 /* These fields are in overlay with packet-x25.c */
363 { "GFI", "x.25.gfi", FT_UINT16, BASE_DEC,
364 NULL, 0xF000, "General Format Identifier", HFILL }},
367 { "Logical Channel", "x.25.lcn", FT_UINT16, BASE_DEC,
368 NULL, 0x0FFF, "Logical Channel Number", HFILL }},
371 { "Packet Type", "x.25.type", FT_UINT8, BASE_HEX,
372 VALS(vals_x25_type), 0x0, NULL, HFILL }},
374 { &hf_xot_pvc_version,
375 { "Version", "xot.pvc.version", FT_UINT8, BASE_HEX,
376 NULL, 0, NULL, HFILL }},
378 { &hf_xot_pvc_status,
379 { "Status", "xot.pvc.status", FT_UINT8, BASE_HEX,
380 VALS(xot_pvc_status_vals), 0, NULL, HFILL }},
382 { &hf_xot_pvc_init_itf_name_len,
383 { "Initiator interface name length", "xot.pvc.init_itf_name_len", FT_UINT8, BASE_DEC,
384 NULL, 0, NULL, HFILL }},
386 { &hf_xot_pvc_init_lcn,
387 { "Initiator LCN", "xot.pvc.init_lcn", FT_UINT8, BASE_DEC,
388 NULL, 0, "Initiator Logical Channel Number", HFILL }},
390 { &hf_xot_pvc_resp_itf_name_len,
391 { "Responder interface name length", "xot.pvc.resp_itf_name_len", FT_UINT8, BASE_DEC,
392 NULL, 0, NULL, HFILL }},
394 { &hf_xot_pvc_resp_lcn,
395 { "Responder LCN", "xot.pvc.resp_lcn", FT_UINT16, BASE_DEC,
396 NULL, 0, "Responder Logical Channel Number", HFILL }},
398 { &hf_xot_pvc_send_inc_window,
399 { "Sender incoming window", "xot.pvc.send_inc_window", FT_UINT8, BASE_DEC,
400 NULL, 0, NULL, HFILL }},
402 { &hf_xot_pvc_send_out_window,
403 { "Sender outgoing window", "xot.pvc.send_out_window", FT_UINT8, BASE_DEC,
404 NULL, 0, NULL, HFILL }},
406 { &hf_xot_pvc_send_inc_pkt_size,
407 { "Sender incoming packet size", "xot.pvc.send_inc_pkt_size", FT_UINT8, BASE_DEC,
408 NULL, 0, NULL, HFILL }},
410 { &hf_xot_pvc_send_out_pkt_size,
411 { "Sender outgoing packet size", "xot.pvc.send_out_pkt_size", FT_UINT8, BASE_DEC,
412 NULL, 0, NULL, HFILL }},
414 { &hf_xot_pvc_init_itf_name,
415 { "Initiator interface name", "xot.pvc.init_itf_name", FT_STRING, BASE_NONE,
416 NULL, 0, NULL, HFILL }},
418 { &hf_xot_pvc_resp_itf_name,
419 { "Responder interface name", "xot.pvc.resp_itf_name", FT_STRING, BASE_NONE,
420 NULL, 0, NULL, HFILL }}
423 static gint *ett[] = {
426 module_t *xot_module;
428 proto_xot = proto_register_protocol("X.25 over TCP", "XOT", "xot");
429 proto_register_field_array(proto_xot, hf, array_length(hf));
430 proto_register_subtree_array(ett, array_length(ett));
431 new_register_dissector("xot", dissect_xot_tcp_heur, proto_xot);
432 xot_module = prefs_register_protocol(proto_xot, NULL);
434 prefs_register_bool_preference(xot_module, "desegment",
435 "Reassemble X.25-over-TCP messages spanning multiple TCP segments",
436 "Whether the X.25-over-TCP dissector should reassemble messages spanning multiple TCP segments. "
437 "To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings",
439 prefs_register_bool_preference(xot_module, "x25_desegment",
440 "Reassemble X.25 packets with More flag to enable safe X.25 reassembly",
441 "Whether the X.25-over-TCP dissector should reassemble all X.25 packets before calling the X25 dissector. "
442 "If the TCP packets arrive out-of-order, the X.25 reassembly can otherwise fail. "
443 "To use this option, you should also enable \"Reassemble X.25-over-TCP messages spanning multiple TCP segments\", \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings and \"Reassemble fragmented X.25 packets\" in the X.25 protocol settings.",
449 proto_reg_handoff_xot(void)
451 dissector_handle_t xot_handle;
453 xot_handle = find_dissector("xot");
454 dissector_add("tcp.port", TCP_PORT_XOT, xot_handle);
456 x25_handle = find_dissector("x.25");