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.
37 #include <epan/packet.h>
38 #include "packet-tcp.h"
39 #include <epan/prefs.h>
41 #define TCP_PORT_XOT 1998
42 #define XOT_HEADER_LENGTH 4
44 #define XOT_PVC_SETUP 0xF5
46 /* Some X25 macros from packet-x25.c - some adapted code as well below */
47 #define X25_MIN_HEADER_LENGTH 3
48 #define X25_MIN_M128_HEADER_LENGTH 4
49 #define X25_NONDATA_BIT 0x01
50 #define PACKET_IS_DATA(type) (!(type & X25_NONDATA_BIT))
51 #define X25_MBIT_MOD8 0x10
52 #define X25_MBIT_MOD128 0x01
54 static const value_string vals_x25_type[] = {
55 { XOT_PVC_SETUP, "PVC Setup" },
59 static const value_string xot_pvc_status_vals[] = {
60 { 0x00, "Waiting to connect" },
62 { 0x08, "Destination disconnected" },
63 { 0x09, "PVC/TCP connection refused" },
64 { 0x0A, "PVC/TCP routing error" },
65 { 0x0B, "PVC/TCP connect timed out" },
67 { 0x10, "Trying to connect via TCP" },
68 { 0x11, "Awaiting PVC-SETUP reply" },
69 { 0x12, "Connected" },
70 { 0x13, "No such destination interface" },
71 { 0x14, "Destination interface is not up" },
72 { 0x15, "Non-X.25 destination interface" },
73 { 0x16, "No such destination PVC" },
74 { 0x17, "Destination PVC configuration mismatch" },
75 { 0x18, "Mismatched flow control values" },
76 { 0x19, "Can't support flow control values" },
77 { 0x1A, "PVC setup protocol error" },
82 static dissector_handle_t x25_handle;
84 static gint proto_xot = -1;
85 static gint ett_xot = -1;
86 static gint hf_xot_version = -1;
87 static gint hf_xot_length = -1;
89 static gint hf_x25_gfi = -1;
90 static gint hf_x25_lcn = -1;
91 static gint hf_x25_type = -1;
93 static gint hf_xot_pvc_version = -1;
94 static gint hf_xot_pvc_status = -1;
95 static gint hf_xot_pvc_init_itf_name_len = -1;
96 static gint hf_xot_pvc_init_lcn = -1;
97 static gint hf_xot_pvc_resp_itf_name_len = -1;
98 static gint hf_xot_pvc_resp_lcn = -1;
99 static gint hf_xot_pvc_send_inc_window = -1;
100 static gint hf_xot_pvc_send_out_window = -1;
101 static gint hf_xot_pvc_send_inc_pkt_size = -1;
102 static gint hf_xot_pvc_send_out_pkt_size = -1;
103 static gint hf_xot_pvc_init_itf_name = -1;
104 static gint hf_xot_pvc_resp_itf_name = -1;
106 /* desegmentation of X.25 over multiple TCP */
107 static gboolean xot_desegment = TRUE;
108 /* desegmentation of X.25 packet sequences */
109 static gboolean x25_desegment = FALSE;
111 static guint get_xot_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
114 int remain = tvb_length_remaining(tvb, offset);
115 if ( remain < XOT_HEADER_LENGTH){
116 /* We did not get the data we asked for, use up what we can */
121 * Get the length of the X.25-over-TCP packet.
123 plen = tvb_get_ntohs(tvb, offset + 2);
124 return XOT_HEADER_LENGTH + plen;
127 static guint get_xot_pdu_len_mult(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
129 int offset_before = offset; /* offset where we start this test */
130 int offset_next = offset + XOT_HEADER_LENGTH + X25_MIN_HEADER_LENGTH;
133 while (tvb_len = tvb_length_remaining(tvb, offset), tvb_len>0){
139 int offset_x25 = offset + XOT_HEADER_LENGTH;
141 /* Minimum where next starts */
142 offset_next = offset_x25 + X25_MIN_HEADER_LENGTH;
144 if (tvb_len < XOT_HEADER_LENGTH) {
145 return offset_next-offset_before;
149 * Get the length of the current X.25-over-TCP packet.
151 plen = get_xot_pdu_len(pinfo, tvb, offset);
152 offset_next = offset + plen;
154 /* Make sure we have enough data */
156 return offset_next-offset_before;
159 /*Some minor code copied from packet-x25.c */
160 bytes0_1 = tvb_get_ntohs(tvb, offset_x25+0);
161 pkt_type = tvb_get_guint8(tvb, offset_x25+2);
163 /* If this is the first packet and it is not data, no sequence needed */
164 if (offset == offset_before && !PACKET_IS_DATA(pkt_type)) {
165 return offset_next-offset_before;
168 /* Check for data, there can be X25 control packets in the X25 data */
169 if (PACKET_IS_DATA(pkt_type)){
170 modulo = ((bytes0_1 & 0x2000) ? 128 : 8);
172 m_bit_set = pkt_type & X25_MBIT_MOD8;
174 m_bit_set = tvb_get_guint8(tvb, offset_x25+3) & X25_MBIT_MOD128;
178 /* We are done with this sequence when the mbit is no longer set */
179 return offset_next-offset_before;
182 offset = offset_next;
183 offset_next += XOT_HEADER_LENGTH + X25_MIN_HEADER_LENGTH;
186 /* not enough data */
187 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
188 return offset_next - offset_before;
191 static void dissect_xot_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
197 proto_item *ti = NULL;
198 proto_tree *xot_tree = NULL;
202 * Dissect the X.25-over-TCP packet.
204 col_set_str(pinfo->cinfo, COL_PROTOCOL, "XOT");
205 version = tvb_get_ntohs(tvb, offset + 0);
206 plen = tvb_get_ntohs(tvb, offset + 2);
207 if (check_col(pinfo->cinfo, COL_INFO))
208 col_add_fstr(pinfo->cinfo, COL_INFO, "XOT Version = %u, size = %u",
210 if (check_col(pinfo->cinfo, COL_INFO) && offset == 0 &&
211 tvb_length_remaining(tvb, offset) > XOT_HEADER_LENGTH + plen )
212 col_append_fstr(pinfo->cinfo, COL_INFO, " TotX25: %d",
213 tvb_length_remaining(tvb, offset));
216 ti = proto_tree_add_protocol_format(tree, proto_xot, tvb, offset, XOT_HEADER_LENGTH,
218 xot_tree = proto_item_add_subtree(ti, ett_xot);
220 proto_tree_add_uint(xot_tree, hf_xot_version, tvb, offset, 2, version);
221 proto_tree_add_uint(xot_tree, hf_xot_length, tvb, offset + 2, 2, plen);
224 offset += XOT_HEADER_LENGTH;
226 * Construct a tvbuff containing the amount of the payload we have
227 * available. Make its reported length the amount of data in the
228 * X.25-over-TCP packet.
230 if (plen >= X25_MIN_HEADER_LENGTH) {
231 pkt_type = tvb_get_guint8(tvb, offset + 2);
232 if (pkt_type == XOT_PVC_SETUP) {
233 guint init_itf_name_len, resp_itf_name_len, pkt_size;
234 gint hdr_offset = offset;
236 if (check_col(pinfo->cinfo, COL_INFO))
237 col_set_str(pinfo->cinfo, COL_INFO, "XOT PVC Setup");
238 proto_item_set_len(ti, XOT_HEADER_LENGTH + plen);
240 /* These fields are in overlay with packet-x25.c */
241 proto_tree_add_item(xot_tree, hf_x25_gfi, tvb, hdr_offset, 2, FALSE);
242 proto_tree_add_item(xot_tree, hf_x25_lcn, tvb, hdr_offset, 2, FALSE);
244 proto_tree_add_item(xot_tree, hf_x25_type, tvb, hdr_offset, 1, FALSE);
247 proto_tree_add_item(xot_tree, hf_xot_pvc_version, tvb, hdr_offset, 1, FALSE);
249 proto_tree_add_item(xot_tree, hf_xot_pvc_status, tvb, hdr_offset, 1, FALSE);
251 proto_tree_add_item(xot_tree, hf_xot_pvc_init_itf_name_len, tvb, hdr_offset, 1, FALSE);
252 init_itf_name_len = tvb_get_guint8(tvb, hdr_offset);
254 proto_tree_add_item(xot_tree, hf_xot_pvc_init_lcn, tvb, hdr_offset, 2, FALSE);
256 proto_tree_add_item(xot_tree, hf_xot_pvc_resp_itf_name_len, tvb, hdr_offset, 1, FALSE);
257 resp_itf_name_len = tvb_get_guint8(tvb, hdr_offset);
259 proto_tree_add_item(xot_tree, hf_xot_pvc_resp_lcn, tvb, hdr_offset, 2, FALSE);
261 proto_tree_add_item(xot_tree, hf_xot_pvc_send_inc_window, tvb, hdr_offset, 1, FALSE);
263 proto_tree_add_item(xot_tree, hf_xot_pvc_send_out_window, tvb, hdr_offset, 1, FALSE);
265 pkt_size = 1 << tvb_get_guint8(tvb, hdr_offset);
266 proto_tree_add_uint(xot_tree, hf_xot_pvc_send_inc_pkt_size, tvb, hdr_offset, 1, pkt_size);
268 pkt_size = 1 << tvb_get_guint8(tvb, hdr_offset);
269 proto_tree_add_uint(xot_tree, hf_xot_pvc_send_out_pkt_size, tvb, hdr_offset, 1, pkt_size);
271 proto_tree_add_item(xot_tree, hf_xot_pvc_init_itf_name, tvb, hdr_offset, init_itf_name_len, FALSE);
272 hdr_offset += init_itf_name_len;
273 proto_tree_add_item(xot_tree, hf_xot_pvc_resp_itf_name, tvb, hdr_offset, resp_itf_name_len, FALSE);
275 next_tvb = tvb_new_subset(tvb, offset,
276 MIN(plen, tvb_length_remaining(tvb, offset)), plen);
277 call_dissector(x25_handle, next_tvb, pinfo, tree);
283 static void dissect_xot_mult(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
286 int len = get_xot_pdu_len_mult(pinfo, tvb, offset);
288 int offset_max = offset+MIN(len,tvb_length_remaining(tvb, offset));
290 proto_tree *xot_tree;
293 /* Special header to show segments */
294 ti = proto_tree_add_protocol_format(tree, proto_xot, tvb, offset, offset_max-offset,
295 "X.25 over TCP - X.25 Sequence");
296 xot_tree = proto_item_add_subtree(ti, ett_xot);
297 proto_tree_add_uint(xot_tree, hf_xot_length, tvb, offset, offset_max, len);
300 while (offset <= offset_max - XOT_HEADER_LENGTH){
301 int plen = get_xot_pdu_len(pinfo, tvb, offset);
302 next_tvb = tvb_new_subset(tvb, offset,plen, plen);
303 /*MIN(plen,tvb_length_remaining(tvb, offset)),plen*/
305 dissect_xot_pdu(next_tvb, pinfo, tree);
309 static int dissect_xot_tcp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
311 int tvb_len = tvb_length(tvb);
314 if (tvb_len >= 2 && tvb_get_ntohs(tvb,0) != XOT_VERSION) {
318 if (!x25_desegment || !xot_desegment){
319 tcp_dissect_pdus(tvb, pinfo, tree, xot_desegment,
323 len=get_xot_pdu_len(pinfo, tvb, 0);
325 /* Use length version that "peeks" into X25, possibly several XOT packets */
326 tcp_dissect_pdus(tvb, pinfo, tree, xot_desegment,
328 get_xot_pdu_len_mult,
330 len=get_xot_pdu_len_mult(pinfo, tvb, 0);
332 /*As tcp_dissect_pdus will not report the success/failure, we have to compute
334 if (len < XOT_HEADER_LENGTH) {
335 /* TCP has reported bounds error */
337 } else if (tvb_len < XOT_HEADER_LENGTH) {
338 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
339 len=tvb_len - XOT_HEADER_LENGTH; /* bytes missing */
340 } else if (tvb_len < len) {
342 /* As the "fixed_len" is not fixed here, just request new segments */
343 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
345 pinfo->desegment_len = len - tvb_len;
347 len=tvb_len - len; /* bytes missing */
352 /* Register the protocol with Wireshark */
354 proto_register_xot(void)
356 static hf_register_info hf[] = {
358 { "Version", "xot.version", FT_UINT16, BASE_DEC,
359 NULL, 0, "Version of X.25 over TCP protocol", HFILL }},
362 { "Length", "xot.length", FT_UINT16, BASE_DEC,
363 NULL, 0, "Length of X.25 over TCP packet", HFILL }},
364 /* These fields are in overlay with packet-x25.c */
366 { "GFI", "x.25.gfi", FT_UINT16, BASE_DEC,
367 NULL, 0xF000, "General Format Identifier", HFILL }},
370 { "Logical Channel", "x.25.lcn", FT_UINT16, BASE_DEC,
371 NULL, 0x0FFF, "Logical Channel Number", HFILL }},
374 { "Packet Type", "x.25.type", FT_UINT8, BASE_HEX,
375 VALS(vals_x25_type), 0x0, NULL, HFILL }},
377 { &hf_xot_pvc_version,
378 { "Version", "xot.pvc.version", FT_UINT8, BASE_HEX,
379 NULL, 0, NULL, HFILL }},
381 { &hf_xot_pvc_status,
382 { "Status", "xot.pvc.status", FT_UINT8, BASE_HEX,
383 VALS(xot_pvc_status_vals), 0, NULL, HFILL }},
385 { &hf_xot_pvc_init_itf_name_len,
386 { "Initiator interface name length", "xot.pvc.init_itf_name_len", FT_UINT8, BASE_DEC,
387 NULL, 0, NULL, HFILL }},
389 { &hf_xot_pvc_init_lcn,
390 { "Initiator LCN", "xot.pvc.init_lcn", FT_UINT8, BASE_DEC,
391 NULL, 0, "Initiator Logical Channel Number", HFILL }},
393 { &hf_xot_pvc_resp_itf_name_len,
394 { "Responder interface name length", "xot.pvc.resp_itf_name_len", FT_UINT8, BASE_DEC,
395 NULL, 0, NULL, HFILL }},
397 { &hf_xot_pvc_resp_lcn,
398 { "Responder LCN", "xot.pvc.resp_lcn", FT_UINT16, BASE_DEC,
399 NULL, 0, "Responder Logical Channel Number", HFILL }},
401 { &hf_xot_pvc_send_inc_window,
402 { "Sender incoming window", "xot.pvc.send_inc_window", FT_UINT8, BASE_DEC,
403 NULL, 0, NULL, HFILL }},
405 { &hf_xot_pvc_send_out_window,
406 { "Sender outgoing window", "xot.pvc.send_out_window", FT_UINT8, BASE_DEC,
407 NULL, 0, NULL, HFILL }},
409 { &hf_xot_pvc_send_inc_pkt_size,
410 { "Sender incoming packet size", "xot.pvc.send_inc_pkt_size", FT_UINT8, BASE_DEC,
411 NULL, 0, NULL, HFILL }},
413 { &hf_xot_pvc_send_out_pkt_size,
414 { "Sender outgoing packet size", "xot.pvc.send_out_pkt_size", FT_UINT8, BASE_DEC,
415 NULL, 0, NULL, HFILL }},
417 { &hf_xot_pvc_init_itf_name,
418 { "Initiator interface name", "xot.pvc.init_itf_name", FT_STRING, BASE_NONE,
419 NULL, 0, NULL, HFILL }},
421 { &hf_xot_pvc_resp_itf_name,
422 { "Responder interface name", "xot.pvc.resp_itf_name", FT_STRING, BASE_NONE,
423 NULL, 0, NULL, HFILL }}
426 static gint *ett[] = {
429 module_t *xot_module;
431 proto_xot = proto_register_protocol("X.25 over TCP", "XOT", "xot");
432 proto_register_field_array(proto_xot, hf, array_length(hf));
433 proto_register_subtree_array(ett, array_length(ett));
434 new_register_dissector("xot", dissect_xot_tcp_heur, proto_xot);
435 xot_module = prefs_register_protocol(proto_xot, NULL);
437 prefs_register_bool_preference(xot_module, "desegment",
438 "Reassemble X.25-over-TCP messages spanning multiple TCP segments",
439 "Whether the X.25-over-TCP dissector should reassemble messages spanning multiple TCP segments. "
440 "To use this option, you must also enable \"Allow subdissectors to reassemble TCP streams\" in the TCP protocol settings",
442 prefs_register_bool_preference(xot_module, "x25_desegment",
443 "Reassemble X.25 packets with More flag to enable safe X.25 reassembly",
444 "Whether the X.25-over-TCP dissector should reassemble all X.25 packets before calling the X25 dissector. "
445 "If the TCP packets arrive out-of-order, the X.25 reassembly can otherwise fail. "
446 "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.",
452 proto_reg_handoff_xot(void)
454 dissector_handle_t xot_handle;
456 xot_handle = find_dissector("xot");
457 dissector_add("tcp.port", TCP_PORT_XOT, xot_handle);
459 x25_handle = find_dissector("x.25");