From Didier Gautheron:
[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 <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34
35 #include <string.h>
36 #include <glib.h>
37 #include <epan/packet.h>
38 #include "packet-tcp.h"
39 #include <epan/prefs.h>
40
41 #define TCP_PORT_XOT 1998
42 #define XOT_HEADER_LENGTH 4
43 #define XOT_VERSION 0
44 #define XOT_PVC_SETUP 0xF5
45
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
53
54 static const value_string vals_x25_type[] = {
55    { XOT_PVC_SETUP, "PVC Setup" },
56    { 0,   NULL}
57 };
58
59 static const value_string xot_pvc_status_vals[] = {
60    { 0x00, "Waiting to connect" },
61
62    { 0x08, "Destination disconnected" },
63    { 0x09, "PVC/TCP connection refused" },
64    { 0x0A, "PVC/TCP routing error" },
65    { 0x0B, "PVC/TCP connect timed out" },
66
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" },
78
79    { 0,   NULL}
80 };
81
82 static dissector_handle_t x25_handle;
83
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;
88
89 static gint hf_x25_gfi = -1;
90 static gint hf_x25_lcn = -1;
91 static gint hf_x25_type = -1;
92
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;
105
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;
110
111 static guint get_xot_pdu_len(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
112 {
113    guint16 plen;
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 */
117       return remain;
118    }
119
120    /*
121     * Get the length of the X.25-over-TCP packet.
122     */
123    plen = tvb_get_ntohs(tvb, offset + 2);
124    return XOT_HEADER_LENGTH + plen;
125 }
126
127 static guint get_xot_pdu_len_mult(packet_info *pinfo _U_, tvbuff_t *tvb, int offset)
128 {
129    int offset_before = offset; /* offset where we start this test */
130    int offset_next = offset + XOT_HEADER_LENGTH + X25_MIN_HEADER_LENGTH;
131    int tvb_len;
132
133    while (tvb_len = tvb_length_remaining(tvb, offset), tvb_len>0){
134       guint16 plen = 0;
135       int modulo;
136       guint16 bytes0_1;
137       guint8 pkt_type;
138       gboolean m_bit_set;
139       int offset_x25 = offset + XOT_HEADER_LENGTH;
140
141       /* Minimum where next starts */
142       offset_next = offset_x25 + X25_MIN_HEADER_LENGTH;
143
144       if (tvb_len < XOT_HEADER_LENGTH) {
145          return offset_next-offset_before;
146       }
147
148       /*
149        * Get the length of the current X.25-over-TCP packet.
150        */
151       plen = get_xot_pdu_len(pinfo, tvb, offset);
152       offset_next = offset + plen;
153
154       /* Make sure we have enough data */
155       if (tvb_len < plen){
156          return offset_next-offset_before;
157       }
158
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);
162
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; 
166       }
167
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);
171          if (modulo == 8) {
172             m_bit_set = pkt_type & X25_MBIT_MOD8;
173          } else {
174             m_bit_set = tvb_get_guint8(tvb, offset_x25+3) & X25_MBIT_MOD128;
175          }
176
177          if (!m_bit_set){
178             /* We are done with this sequence when the mbit is no longer set */
179             return offset_next-offset_before;
180          }
181       }
182       offset = offset_next;
183       offset_next += XOT_HEADER_LENGTH + X25_MIN_HEADER_LENGTH;
184   }
185
186   /* not enough data */
187   pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
188   return offset_next - offset_before;
189 }
190
191 static void dissect_xot_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
192 {
193   int offset = 0;
194   guint16 version;
195   guint16 plen;
196   guint8 pkt_type;
197   proto_item *ti = NULL;
198   proto_tree *xot_tree = NULL;
199   tvbuff_t   *next_tvb;
200
201   /*
202    * Dissect the X.25-over-TCP packet.
203    */
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",
209                      version, plen);
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));
214
215      if (tree) {
216         ti = proto_tree_add_protocol_format(tree, proto_xot, tvb, offset, XOT_HEADER_LENGTH,
217                                             "X.25 over TCP");
218         xot_tree = proto_item_add_subtree(ti, ett_xot);
219
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);
222      }
223
224      offset += XOT_HEADER_LENGTH;
225      /*
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.
229       */
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;
235
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);
239
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);
243            hdr_offset += 2;
244            proto_tree_add_item(xot_tree, hf_x25_type, tvb, hdr_offset, 1, FALSE);
245            hdr_offset += 1;
246
247            proto_tree_add_item(xot_tree, hf_xot_pvc_version, tvb, hdr_offset, 1, FALSE);
248            hdr_offset += 1;
249            proto_tree_add_item(xot_tree, hf_xot_pvc_status, tvb, hdr_offset, 1, FALSE);
250            hdr_offset += 1;
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);
253            hdr_offset += 1;
254            proto_tree_add_item(xot_tree, hf_xot_pvc_init_lcn, tvb, hdr_offset, 2, FALSE);
255            hdr_offset += 2;
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);
258            hdr_offset += 1;
259            proto_tree_add_item(xot_tree, hf_xot_pvc_resp_lcn, tvb, hdr_offset, 2, FALSE);
260            hdr_offset += 2;
261            proto_tree_add_item(xot_tree, hf_xot_pvc_send_inc_window, tvb, hdr_offset, 1, FALSE);
262            hdr_offset += 1;
263            proto_tree_add_item(xot_tree, hf_xot_pvc_send_out_window, tvb, hdr_offset, 1, FALSE);
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_inc_pkt_size, tvb, hdr_offset, 1, pkt_size);
267            hdr_offset += 1;
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);
270            hdr_offset += 1;
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);
274         } else {
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);
278         }
279      }
280      offset += plen;
281 }
282
283 static void dissect_xot_mult(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
284 {
285    int offset = 0;
286    int len = get_xot_pdu_len_mult(pinfo, tvb, offset);
287    tvbuff_t   *next_tvb;
288    int offset_max = offset+MIN(len,tvb_length_remaining(tvb, offset));
289    proto_item *ti;
290    proto_tree *xot_tree;
291
292    if (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);
298    }
299
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*/
304
305       dissect_xot_pdu(next_tvb, pinfo, tree);
306       offset += plen;
307    }
308 }
309 static int dissect_xot_tcp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
310 {
311    int tvb_len = tvb_length(tvb);
312    int len = 0;
313
314    if (tvb_len >= 2 && tvb_get_ntohs(tvb,0) != XOT_VERSION) {
315       return 0;
316    }
317
318    if (!x25_desegment || !xot_desegment){
319       tcp_dissect_pdus(tvb, pinfo, tree, xot_desegment,
320                        XOT_HEADER_LENGTH,
321                        get_xot_pdu_len, 
322                        dissect_xot_pdu);
323       len=get_xot_pdu_len(pinfo, tvb, 0);
324    } else {
325       /* Use length version that "peeks" into X25, possibly several XOT packets */
326       tcp_dissect_pdus(tvb, pinfo, tree, xot_desegment,
327                        XOT_HEADER_LENGTH,
328                        get_xot_pdu_len_mult, 
329                        dissect_xot_mult);
330       len=get_xot_pdu_len_mult(pinfo, tvb, 0);
331    }
332    /*As tcp_dissect_pdus will not report the success/failure, we have to compute
333      again */
334    if (len < XOT_HEADER_LENGTH) {
335       /* TCP has reported bounds error */
336       len = 0;
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) {
341       if (x25_desegment){
342          /* As the "fixed_len" is not fixed here, just request new segments */
343          pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
344       } else {
345          pinfo->desegment_len = len - tvb_len;
346       }
347       len=tvb_len - len; /* bytes missing */
348    }
349    return len;
350 }
351
352 /* Register the protocol with Wireshark */
353 void
354 proto_register_xot(void)
355 {
356         static hf_register_info hf[] = {
357                 { &hf_xot_version,
358                         { "Version", "xot.version", FT_UINT16, BASE_DEC,
359                         NULL, 0, "Version of X.25 over TCP protocol", HFILL }},
360
361                 { &hf_xot_length,
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 */
365                 { &hf_x25_gfi,
366                         { "GFI", "x.25.gfi", FT_UINT16, BASE_DEC,
367                         NULL, 0xF000, "General Format Identifier", HFILL }},
368
369                 { &hf_x25_lcn,
370                         { "Logical Channel", "x.25.lcn", FT_UINT16, BASE_DEC,
371                         NULL, 0x0FFF, "Logical Channel Number", HFILL }},
372
373                 { &hf_x25_type,
374                         { "Packet Type", "x.25.type", FT_UINT8, BASE_HEX,
375                         VALS(vals_x25_type), 0x0, NULL, HFILL }},
376
377                 { &hf_xot_pvc_version,
378                         { "Version", "xot.pvc.version", FT_UINT8, BASE_HEX,
379                         NULL, 0, NULL, HFILL }},
380
381                 { &hf_xot_pvc_status,
382                         { "Status", "xot.pvc.status", FT_UINT8, BASE_HEX,
383                         VALS(xot_pvc_status_vals), 0, NULL, HFILL }},
384
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 }},
388
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 }},
392
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 }},
396
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 }},
400
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 }},
404
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 }},
408
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 }},
412
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 }},
416
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 }},
420
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 }}
424         };
425
426         static gint *ett[] = {
427                 &ett_xot
428         };
429         module_t *xot_module;
430
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);
436
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",
441             &xot_desegment);
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.",
447             &x25_desegment);
448
449 }
450
451 void
452 proto_reg_handoff_xot(void)
453 {
454   dissector_handle_t xot_handle;
455
456   xot_handle = find_dissector("xot");
457   dissector_add("tcp.port", TCP_PORT_XOT, xot_handle);
458
459   x25_handle = find_dissector("x.25");
460 }