- some radcom files seem to have a different magic key than the one we
[obnox/wireshark/wip.git] / packet-tpkt.c
1 /* packet-tpkt.c
2  *
3  * Routine to check for RFC 1006 TPKT header and to dissect TPKT header
4  * Copyright 2000, Philips Electronics N.V.
5  * Andreas Sikkema <andreas.sikkema@philips.com>
6  *
7  * Routine to dissect RFC 1006 TPKT packet containing OSI TP PDU
8  * Copyright 2001, Martin Thomas <Martin_A_Thomas@yahoo.com>
9  *
10  * $Id: packet-tpkt.c,v 1.22 2002/08/28 21:00:36 jmayer Exp $
11  *
12  * Ethereal - Network traffic analyzer
13  * By Gerald Combs <gerald@ethereal.com>
14  * Copyright 1998 Gerald Combs
15  *
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.
20  *
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.
25  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <glib.h>
36 #include <epan/packet.h>
37
38 #include <stdio.h>
39 #include <string.h>
40
41 #include "packet-tpkt.h"
42 #include "packet-frame.h"
43 #include "prefs.h"
44
45 /* TPKT header fields             */
46 static int proto_tpkt          = -1;
47 static int hf_tpkt_version     = -1;
48 static int hf_tpkt_reserved    = -1;
49 static int hf_tpkt_length      = -1;
50
51 /* TPKT fields defining a sub tree */
52 static gint ett_tpkt           = -1;
53
54 /* desegmentation of OSI over TPKT over TCP */
55 static gboolean tpkt_desegment = TRUE;
56
57 #define TCP_PORT_TPKT   102
58
59 /* find the dissector for OSI TP (aka COTP) */
60 static dissector_handle_t osi_tp_handle;
61
62 /*
63  * Check whether this could be a TPKT-encapsulated PDU.
64  * Returns -1 if it's not, and the PDU length from the TPKT header
65  * if it is.
66  *
67  * "min_len" is the minimum length of the PDU; the length field in the
68  * TPKT header must be at least "4+min_len" in order for this to be a
69  * valid TPKT PDU for the protocol in question.
70  */
71 int
72 is_tpkt(tvbuff_t *tvb, int min_len)
73 {
74         guint16 pkt_len;
75
76         /*
77          * If TPKT is disabled, don't dissect it, just return -1, meaning
78          * "this isn't TPKT".
79          */
80         if (!proto_is_protocol_enabled(proto_tpkt))
81                 return -1;
82
83         /* There should at least be 4 bytes left in the frame */
84         if (!tvb_bytes_exist(tvb, 0, 4))
85                 return -1;      /* there aren't */
86
87         /*
88          * The first octet should be 3 and the second one should be 0
89          * The H.323 implementers guide suggests that this might not
90          * always be the case....
91          */
92         if (!(tvb_get_guint8(tvb, 0) == 3 && tvb_get_guint8(tvb, 1) == 0))
93                 return -1;      /* they're not */
94
95         /*
96          * Get the length from the TPKT header.  Make sure it's large
97          * enough.
98          */
99         pkt_len = tvb_get_ntohs(tvb, 2);
100         if (pkt_len < 4 + min_len)
101                 return -1;      /* it's not */
102
103         /*
104          * Return the length from the header.
105          */
106         return pkt_len;
107 }
108
109 /*
110  * Dissect TPKT-encapsulated data in a TCP stream.
111  */
112 void
113 dissect_tpkt_encap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
114     gboolean desegment, dissector_handle_t subdissector_handle)
115 {
116         proto_item *ti = NULL;
117         proto_tree *tpkt_tree = NULL;
118         volatile int offset = 0;
119         int length_remaining;
120         int data_len;
121         volatile int length;
122         tvbuff_t *next_tvb;
123         const char *saved_proto;
124
125         /*
126          * If we're reassembling segmented TPKT PDUs, empty the COL_INFO
127          * column, so subdissectors can append information
128          * without having to worry about emptying the column.
129          *
130          * We use "col_add_str()" because the subdissector
131          * might be appending information to the column, in
132          * which case we'd have to zero the buffer out explicitly
133          * anyway.
134          */
135         if (tpkt_desegment && check_col(pinfo->cinfo, COL_INFO))
136                 col_add_str(pinfo->cinfo, COL_INFO, "");
137
138         while (tvb_reported_length_remaining(tvb, offset) != 0) {
139                 /*
140                  * Is the first byte of this putative TPKT header
141                  * a valid TPKT version number, i.e. 3?
142                  */
143                 if (tvb_get_guint8(tvb, offset) != 3) {
144                         /*
145                          * No, so don't assume this is a TPKT header;
146                          * we might be in the middle of TPKT data,
147                          * so don't get the length and don't try to
148                          * do reassembly.
149                          */
150                         if (check_col(pinfo->cinfo, COL_PROTOCOL))
151                                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TPKT");
152                         if (check_col(pinfo->cinfo, COL_INFO))
153                                 col_set_str(pinfo->cinfo, COL_INFO, "Continuation");
154                         if (tree) {
155                                 ti = proto_tree_add_item(tree, proto_tpkt, tvb,
156                                     offset, -1, FALSE);
157                                 tpkt_tree = proto_item_add_subtree(ti, ett_tpkt);
158
159                                 proto_tree_add_text(tpkt_tree, tvb, offset, -1,
160                                     "Continuation data");
161                         }
162                         return;
163                 }
164
165                 length_remaining = tvb_length_remaining(tvb, offset);
166
167                 /*
168                  * Can we do reassembly?
169                  */
170                 if (desegment && pinfo->can_desegment) {
171                         /*
172                          * Yes - is the TPKT header split across segment
173                          * boundaries?
174                          */
175                         if (length_remaining < 4) {
176                                 /*
177                                  * Yes.  Tell the TCP dissector where
178                                  * the data for this message starts in
179                                  * the data it handed us, and how many
180                                  * more bytes we need, and return.
181                                  */
182                                 pinfo->desegment_offset = offset;
183                                 pinfo->desegment_len = 4 - length_remaining;
184                                 return;
185                         }
186                 }
187
188                 /*
189                  * Get the length from the TPKT header.
190                  */
191                 data_len = tvb_get_ntohs(tvb, offset + 2);
192
193                 /*
194                  * Can we do reassembly?
195                  */
196                 if (desegment && pinfo->can_desegment) {
197                         /*
198                          * Yes - is the payload split across segment
199                          * boundaries?
200                          */
201                         if (length_remaining < data_len) {
202                                 /*
203                                  * Yes.  Tell the TCP dissector where
204                                  * the data for this message starts in
205                                  * the data it handed us, and how many
206                                  * more bytes we need, and return.
207                                  */
208                                 pinfo->desegment_offset = offset;
209                                 pinfo->desegment_len =
210                                     data_len - length_remaining;
211                                 return;
212                         }
213                 }
214
215                 /*
216                  * Dissect the TPKT header.
217                  * Save and restore "pinfo->current_proto".
218                  */
219                 saved_proto = pinfo->current_proto;
220                 pinfo->current_proto = "TPKT";
221
222                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
223                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "TPKT");
224                 /*
225                  * Don't add the TPKT header information if we're
226                  * reassembling segmented TPKT PDUs or if this
227                  * PDU isn't reassembled.
228                  *
229                  * XXX - the first is so that subdissectors can append
230                  * information without getting TPKT stuff in the middle;
231                  * why the second?
232                  */
233                 if (!tpkt_desegment && !pinfo->fragmented
234                     && check_col(pinfo->cinfo, COL_INFO)) {
235                         col_add_fstr(pinfo->cinfo, COL_INFO,
236                             "TPKT Data length = %u", data_len);
237                 }
238
239                 if (tree) {
240                         ti = proto_tree_add_item(tree, proto_tpkt, tvb,
241                             offset, 4, FALSE);
242                         tpkt_tree = proto_item_add_subtree(ti, ett_tpkt);
243
244                         /* Version */
245                         proto_tree_add_item(tpkt_tree, hf_tpkt_version, tvb,
246                             offset, 1, FALSE);
247
248                         /* Reserved octet*/
249                         proto_tree_add_item(tpkt_tree, hf_tpkt_reserved, tvb,
250                             offset + 1, 1, FALSE);
251
252                         /* Length */
253                         proto_tree_add_uint(tpkt_tree, hf_tpkt_length, tvb,
254                             offset + 2, 2, data_len);
255                 }
256                 pinfo->current_proto = saved_proto;
257
258                 /* Skip the TPKT header. */
259                 offset += 4;
260                 data_len -= 4;
261
262                 /*
263                  * Construct a tvbuff containing the amount of the payload
264                  * we have available.  Make its reported length the
265                  * amount of data in this TPKT packet.
266                  *
267                  * XXX - if reassembly isn't enabled. the subdissector
268                  * will throw a BoundsError exception, rather than a
269                  * ReportedBoundsError exception.  We really want
270                  * a tvbuff where the length is "length", the reported
271                  * length is "plen + 2", and the "if the snapshot length
272                  * were infinite" length were the minimum of the
273                  * reported length of the tvbuff handed to us and "plen+2",
274                  * with a new type of exception thrown if the offset is
275                  * within the reported length but beyond that third length,
276                  * with that exception getting the "Unreassembled Packet"
277                  * error.
278                  */
279                 length = length_remaining - 4;
280                 if (length > data_len)
281                         length = data_len;
282                 next_tvb = tvb_new_subset(tvb, offset, length, data_len);
283
284                 /*
285                  * Call the subdissector.
286                  *
287                  * Catch the ReportedBoundsError exception; if this
288                  * particular message happens to get a ReportedBoundsError
289                  * exception, that doesn't mean that we should stop
290                  * dissecting TPKT messages within this frame or chunk
291                  * of reassembled data.
292                  *
293                  * If it gets a BoundsError, we can stop, as there's nothing
294                  * more to see, so we just re-throw it.
295                  */
296                 TRY {
297                         call_dissector(subdissector_handle, next_tvb, pinfo,
298                             tree);
299                 }
300                 CATCH(BoundsError) {
301                         RETHROW;
302                 }
303                 CATCH(ReportedBoundsError) {
304                         show_reported_bounds_error(tvb, pinfo, tree);
305                 }
306                 ENDTRY;
307
308                 /*
309                  * Skip the payload.
310                  */
311                 offset += length;
312         }
313 }
314
315 /*
316  * Dissect RFC 1006 TPKT, which wraps a TPKT header around an OSI TP
317  * PDU.
318  */
319 static void
320 dissect_tpkt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
321 {
322         dissect_tpkt_encap(tvb, pinfo, tree, tpkt_desegment, osi_tp_handle);
323 }
324
325 void
326 proto_register_tpkt(void)
327 {
328         static hf_register_info hf[] =
329         {
330                 {
331                         &hf_tpkt_version,
332                         {
333                                 "Version",
334                                 "tpkt.version",
335                                 FT_UINT8,
336                                 BASE_DEC,
337                                 NULL,
338                                 0x0,
339                                 "", HFILL
340                         }
341                 },
342                 {
343                         &hf_tpkt_reserved,
344                         {
345                                 "Reserved",
346                                 "tpkt.reserved",
347                                 FT_UINT8,
348                                 BASE_DEC,
349                                 NULL,
350                                 0x0,
351                                 "", HFILL
352                         }
353                 },
354                 {
355                         &hf_tpkt_length,
356                         {
357                                 "Length",
358                                 "tpkt.length",
359                                 FT_UINT16,
360                                 BASE_DEC,
361                                 NULL,
362                                 0x0,
363                                 "", HFILL
364                         }
365                 },
366         };
367
368         static gint *ett[] =
369         {
370                 &ett_tpkt,
371         };
372         module_t *tpkt_module;
373
374         proto_tpkt = proto_register_protocol("TPKT", "TPKT", "tpkt");
375         proto_register_field_array(proto_tpkt, hf, array_length(hf));
376         proto_register_subtree_array(ett, array_length(ett));
377
378         tpkt_module = prefs_register_protocol(proto_tpkt, NULL);
379         prefs_register_bool_preference(tpkt_module, "desegment",
380             "Desegment all TPKT messages spanning multiple TCP segments",
381             "Whether the TPKT dissector should desegment all messages spanning multiple TCP segments",
382             &tpkt_desegment);
383 }
384
385 void
386 proto_reg_handoff_tpkt(void)
387 {
388         dissector_handle_t tpkt_handle;
389
390         osi_tp_handle = find_dissector("ositp");
391         tpkt_handle = create_dissector_handle(dissect_tpkt, proto_tpkt);
392         dissector_add("tcp.port", TCP_PORT_TPKT, tpkt_handle);
393 }