Fix for bug 5422:
[obnox/wireshark/wip.git] / epan / dissectors / packet-xot.c
1 /* packet-xot.c
2  * Routines for X.25 over TCP dissection (RFC 1613)
3  *
4  * Copyright 2000, Paul Ionescu <paul@acorp.ro>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <ctype.h>
33
34 #include <glib.h>
35 #include <epan/packet.h>
36 #include "packet-tcp.h"
37 #include <epan/prefs.h>
38
39 #define TCP_PORT_XOT 1998
40 #define XOT_HEADER_LENGTH 4
41 #define XOT_VERSION 0
42 #define XOT_PVC_SETUP 0xF5
43
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
51
52 static const value_string vals_x25_type[] = {
53    { XOT_PVC_SETUP, "PVC Setup" },
54    { 0,   NULL}
55 };
56
57 static const value_string xot_pvc_status_vals[] = {
58    { 0x00, "Waiting to connect" },
59
60    { 0x08, "Destination disconnected" },
61    { 0x09, "PVC/TCP connection refused" },
62    { 0x0A, "PVC/TCP routing error" },
63    { 0x0B, "PVC/TCP connect timed out" },
64
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" },
76
77    { 0,   NULL}
78 };
79
80 static dissector_handle_t x25_handle;
81
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;
86
87 static gint hf_x25_gfi = -1;
88 static gint hf_x25_lcn = -1;
89 static gint hf_x25_type = -1;
90
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;
103
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;
108
109 static guint get_xot_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
110 {
111    guint16 plen;
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 */
115       return remain;
116    }
117
118    /*
119     * Get the length of the X.25-over-TCP packet.
120     */
121    plen = tvb_get_ntohs(tvb, offset + 2);
122    return XOT_HEADER_LENGTH + plen;
123 }
124
125 static guint get_xot_pdu_len_mult(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
126 {
127    int offset_before = offset; /* offset where we start this test */
128    int offset_next = offset + XOT_HEADER_LENGTH + X25_MIN_HEADER_LENGTH;
129    int tvb_len;
130
131    while (tvb_len = tvb_length_remaining(tvb, offset), tvb_len>0){
132       guint16 plen = 0;
133       int modulo;
134       guint16 bytes0_1;
135       guint8 pkt_type;
136       gboolean m_bit_set;
137       int offset_x25 = offset + XOT_HEADER_LENGTH;
138
139       /* Minimum where next starts */
140       offset_next = offset_x25 + X25_MIN_HEADER_LENGTH;
141
142       if (tvb_len < XOT_HEADER_LENGTH) {
143          return offset_next-offset_before;
144       }
145
146       /*
147        * Get the length of the current X.25-over-TCP packet.
148        */
149       plen = get_xot_pdu_len(pinfo, tvb, offset);
150       offset_next = offset + plen;
151
152       /* Make sure we have enough data */
153       if (tvb_len < plen){
154          return offset_next-offset_before;
155       }
156
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);
160
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; 
164       }
165
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);
169          if (modulo == 8) {
170             m_bit_set = pkt_type & X25_MBIT_MOD8;
171          } else {
172             m_bit_set = tvb_get_guint8(tvb, offset_x25+3) & X25_MBIT_MOD128;
173          }
174
175          if (!m_bit_set){
176             /* We are done with this sequence when the mbit is no longer set */
177             return offset_next-offset_before;
178          }
179       }
180       offset = offset_next;
181       offset_next += XOT_HEADER_LENGTH + X25_MIN_HEADER_LENGTH;
182   }
183
184   /* not enough data */
185   pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
186   return offset_next - offset_before;
187 }
188
189 static void dissect_xot_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
190 {
191   int offset = 0;
192   guint16 version;
193   guint16 plen;
194   guint8 pkt_type;
195   proto_item *ti = NULL;
196   proto_tree *xot_tree = NULL;
197   tvbuff_t   *next_tvb;
198
199   /*
200    * Dissect the X.25-over-TCP packet.
201    */
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",
207                      version, plen);
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));
212
213      if (tree) {
214         ti = proto_tree_add_protocol_format(tree, proto_xot, tvb, offset, XOT_HEADER_LENGTH,
215                                             "X.25 over TCP");
216         xot_tree = proto_item_add_subtree(ti, ett_xot);
217
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);
220      }
221
222      offset += XOT_HEADER_LENGTH;
223      /*
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.
227       */
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;
233
234            col_set_str(pinfo->cinfo, COL_INFO, "XOT PVC Setup");
235            proto_item_set_len(ti, XOT_HEADER_LENGTH + plen);
236
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);
240            hdr_offset += 2;
241            proto_tree_add_item(xot_tree, hf_x25_type, tvb, hdr_offset, 1, FALSE);
242            hdr_offset += 1;
243
244            proto_tree_add_item(xot_tree, hf_xot_pvc_version, tvb, hdr_offset, 1, FALSE);
245            hdr_offset += 1;
246            proto_tree_add_item(xot_tree, hf_xot_pvc_status, tvb, hdr_offset, 1, FALSE);
247            hdr_offset += 1;
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);
250            hdr_offset += 1;
251            proto_tree_add_item(xot_tree, hf_xot_pvc_init_lcn, tvb, hdr_offset, 2, FALSE);
252            hdr_offset += 2;
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);
255            hdr_offset += 1;
256            proto_tree_add_item(xot_tree, hf_xot_pvc_resp_lcn, tvb, hdr_offset, 2, FALSE);
257            hdr_offset += 2;
258            proto_tree_add_item(xot_tree, hf_xot_pvc_send_inc_window, tvb, hdr_offset, 1, FALSE);
259            hdr_offset += 1;
260            proto_tree_add_item(xot_tree, hf_xot_pvc_send_out_window, tvb, hdr_offset, 1, FALSE);
261            hdr_offset += 1;
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);
264            hdr_offset += 1;
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);
267            hdr_offset += 1;
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);
271         } else {
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);
275         }
276      }
277      offset += plen;
278 }
279
280 static void dissect_xot_mult(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
281 {
282    int offset = 0;
283    int len = get_xot_pdu_len_mult(pinfo, tvb, offset);
284    tvbuff_t   *next_tvb;
285    int offset_max = offset+MIN(len,tvb_length_remaining(tvb, offset));
286    proto_item *ti;
287    proto_tree *xot_tree;
288
289    if (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);
295    }
296
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*/
301
302       dissect_xot_pdu(next_tvb, pinfo, tree);
303       offset += plen;
304    }
305 }
306 static int dissect_xot_tcp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
307 {
308    int tvb_len = tvb_length(tvb);
309    int len = 0;
310
311    if (tvb_len >= 2 && tvb_get_ntohs(tvb,0) != XOT_VERSION) {
312       return 0;
313    }
314
315    if (!x25_desegment || !xot_desegment){
316       tcp_dissect_pdus(tvb, pinfo, tree, xot_desegment,
317                        XOT_HEADER_LENGTH,
318                        get_xot_pdu_len, 
319                        dissect_xot_pdu);
320       len=get_xot_pdu_len(pinfo, tvb, 0);
321    } else {
322       /* Use length version that "peeks" into X25, possibly several XOT packets */
323       tcp_dissect_pdus(tvb, pinfo, tree, xot_desegment,
324                        XOT_HEADER_LENGTH,
325                        get_xot_pdu_len_mult, 
326                        dissect_xot_mult);
327       len=get_xot_pdu_len_mult(pinfo, tvb, 0);
328    }
329    /*As tcp_dissect_pdus will not report the success/failure, we have to compute
330      again */
331    if (len < XOT_HEADER_LENGTH) {
332       /* TCP has reported bounds error */
333       len = 0;
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) {
338       if (x25_desegment){
339          /* As the "fixed_len" is not fixed here, just request new segments */
340          pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
341       } else {
342          pinfo->desegment_len = len - tvb_len;
343       }
344       len=tvb_len - len; /* bytes missing */
345    }
346    return len;
347 }
348
349 /* Register the protocol with Wireshark */
350 void
351 proto_register_xot(void)
352 {
353         static hf_register_info hf[] = {
354                 { &hf_xot_version,
355                         { "Version", "xot.version", FT_UINT16, BASE_DEC,
356                         NULL, 0, "Version of X.25 over TCP protocol", HFILL }},
357
358                 { &hf_xot_length,
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 */
362                 { &hf_x25_gfi,
363                         { "GFI", "x.25.gfi", FT_UINT16, BASE_DEC,
364                         NULL, 0xF000, "General Format Identifier", HFILL }},
365
366                 { &hf_x25_lcn,
367                         { "Logical Channel", "x.25.lcn", FT_UINT16, BASE_DEC,
368                         NULL, 0x0FFF, "Logical Channel Number", HFILL }},
369
370                 { &hf_x25_type,
371                         { "Packet Type", "x.25.type", FT_UINT8, BASE_HEX,
372                         VALS(vals_x25_type), 0x0, NULL, HFILL }},
373
374                 { &hf_xot_pvc_version,
375                         { "Version", "xot.pvc.version", FT_UINT8, BASE_HEX,
376                         NULL, 0, NULL, HFILL }},
377
378                 { &hf_xot_pvc_status,
379                         { "Status", "xot.pvc.status", FT_UINT8, BASE_HEX,
380                         VALS(xot_pvc_status_vals), 0, NULL, HFILL }},
381
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 }},
385
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 }},
389
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 }},
393
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 }},
397
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 }},
401
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 }},
405
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 }},
409
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 }},
413
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 }},
417
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 }}
421         };
422
423         static gint *ett[] = {
424                 &ett_xot
425         };
426         module_t *xot_module;
427
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);
433
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",
438             &xot_desegment);
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.",
444             &x25_desegment);
445
446 }
447
448 void
449 proto_reg_handoff_xot(void)
450 {
451   dissector_handle_t xot_handle;
452
453   xot_handle = find_dissector("xot");
454   dissector_add("tcp.port", TCP_PORT_XOT, xot_handle);
455
456   x25_handle = find_dissector("x.25");
457 }