fbc7e29058d5c4631ac3904a631b630d2c0567dd
[obnox/wireshark/wip.git] / epan / dissectors / packet-osi.c
1 /* packet-osi.c
2  * Routines for ISO/OSI network and transport protocol packet disassembly
3  * Main entrance point and common functions
4  *
5  * $Id$
6  * Laurent Deniel <laurent.deniel@free.fr>
7  * Ralf Schneider <Ralf.Schneider@t-online.de>
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <glib.h>
36 #include <epan/prefs.h>
37 #include <epan/packet.h>
38 #include <epan/llcsaps.h>
39 #include <epan/aftypes.h>
40 #include <epan/nlpid.h>
41 #include <epan/ppptypes.h>
42 #include <epan/chdlctypes.h>
43 #include "packet-osi.h"
44 #include "packet-isis.h"
45 #include "packet-esis.h"
46 #include "packet-tpkt.h"
47
48 static int  proto_osi         = -1;
49 static dissector_handle_t osi_handle;
50
51 /* Preferences for OSI over TPKT over TCP */
52 static gboolean tpkt_desegment = FALSE;
53 static guint global_tcp_port_osi_over_tpkt = 0;
54
55 cksum_status_t
56 calc_checksum( tvbuff_t *tvb, int offset, guint len, guint checksum) {
57   const gchar *buffer;
58   guint   available_len;
59   const guint8 *p;
60   guint32 c0, c1;
61   guint   seglen;
62   guint   i;
63
64   if ( 0 == checksum )
65     return( NO_CKSUM );
66
67   available_len = tvb_length_remaining( tvb, offset );
68   if ( available_len < len )
69     return( DATA_MISSING );
70
71   buffer = tvb_get_ptr( tvb, offset, len );
72
73   /*
74    * The maximum values of c0 and c1 will occur if all bytes have the
75    * value 255; if so, then c0 will be len*255 and c1 will be
76    * (len*255 + (len-1)*255 + ... + 255), which is
77    * (len + (len - 1) + ... + 1)*255, or 255*(len*(len + 1))/2.
78    * This means it can overflow if "len" is 5804 or greater.
79    *
80    * (A+B) mod 255 = ((A mod 255) + (B mod 255) mod 255, so
81    * we can solve this by taking c0 and c1 mod 255 every
82    * 5803 bytes.
83    */
84   p = buffer;
85   c0 = 0;
86   c1 = 0;
87   while (len != 0) {
88     seglen = len;
89     if (seglen > 5803)
90       seglen = 5803;
91     for (i = 0; i < seglen; i++) {
92       c0 = c0 + *(p++);
93       c1 += c0;
94     }
95
96     c0 = c0 % 255;
97     c1 = c1 % 255;
98
99     len -= seglen;
100   }
101   if (c0 != 0 || c1 != 0)
102     return( CKSUM_NOT_OK );     /* XXX - what should the checksum field be? */
103   else
104     return( CKSUM_OK );
105 }
106
107
108 cksum_status_t
109 check_and_get_checksum( tvbuff_t *tvb, int offset, guint len, guint checksum, int offset_check, guint16* result) {
110   const gchar *buffer;
111   guint   available_len;
112   const guint8 *p;
113   guint8 discard = 0;
114   guint32 c0, c1, factor;
115   guint   seglen, initlen = len;
116   guint   i;
117   int     block, x, y;
118
119   if ( 0 == checksum )
120     return( NO_CKSUM );
121
122   available_len = tvb_length_remaining( tvb, offset );
123   offset_check -= offset;
124   if ( ( available_len < len ) || ( offset_check < 0 ) || ( (guint)(offset_check+2) > len ) )
125     return( DATA_MISSING );
126
127   buffer = tvb_get_ptr( tvb, offset, len );
128   block  = offset_check / 5803;
129
130   /*
131    * The maximum values of c0 and c1 will occur if all bytes have the
132    * value 255; if so, then c0 will be len*255 and c1 will be
133    * (len*255 + (len-1)*255 + ... + 255), which is
134    * (len + (len - 1) + ... + 1)*255, or 255*(len*(len + 1))/2.
135    * This means it can overflow if "len" is 5804 or greater.
136    *
137    * (A+B) mod 255 = ((A mod 255) + (B mod 255) mod 255, so
138    * we can solve this by taking c0 and c1 mod 255 every
139    * 5803 bytes.
140    */
141   p = buffer;
142   c0 = 0;
143   c1 = 0;
144
145   while (len != 0) {
146     seglen = len;
147     if ( block-- == 0 ) {
148       seglen = offset_check % 5803;
149       discard = 1;
150     } else if ( seglen > 5803 )
151       seglen = 5803;
152     for (i = 0; i < seglen; i++) {
153       c0 = c0 + *(p++);
154       c1 += c0;
155     }
156     if ( discard ) {
157       /*
158        * This works even if (offset_check % 5803) == 5802
159        */
160       p += 2;
161       c1 += 2*c0;
162       len -= 2;
163       discard = 0;
164     }
165
166     c0 = c0 % 255;
167     c1 = c1 % 255;
168
169     len -= seglen;
170   }
171
172   factor = ( initlen - offset_check ) * c0;
173   x = factor - c0 - c1;
174   y = c1 - factor - 1;
175
176   /*
177    * This algorithm uses the 8 bits one's complement arithmetic.
178    * Therefore, we must correct an effect produced
179    * by the "standard" arithmetic (two's complement)
180    */
181
182   if (x < 0 ) x--;
183   if (y > 0 ) y++;
184
185   x %= 255;
186   y %= 255;
187
188   if (x == 0) x = 0xFF;
189   if (y == 0) y = 0x01;
190
191   *result = ( x << 8 ) | ( y & 0xFF );
192
193   if (*result != checksum)
194     return( CKSUM_NOT_OK );     /* XXX - what should the checksum field be? */
195   else
196     return( CKSUM_OK );
197 }
198
199
200
201 /* main entry point */
202
203 /*
204  * These assume the NLPID is a secondary protocol identifier, not an
205  * initial protocol identifier.
206  *
207  * This is an issue only if, in any packet where an NLPID appears, it's
208  * an initial protocol identifier *AND* it can have the value 1, which
209  * means T.70 for an IPI and X.29 for an SPI.
210  */
211 const value_string nlpid_vals[] = {
212         { NLPID_NULL,            "NULL" },
213         { NLPID_SPI_X_29,        "X.29" },
214         { NLPID_X_633,           "X.633" },
215         { NLPID_Q_931,           "Q.931" },
216         { NLPID_Q_2931,          "Q.2931" },
217         { NLPID_Q_2119,          "Q.2119" },
218         { NLPID_SNAP,            "SNAP" },
219         { NLPID_ISO8473_CLNP,    "CLNP" },
220         { NLPID_ISO9542_ESIS,    "ESIS" },
221         { NLPID_ISO10589_ISIS,   "ISIS" },
222         { NLPID_ISO10747_IDRP,   "IDRP" },
223         { NLPID_ISO9542X25_ESIS, "ESIS (X.25)" },
224         { NLPID_ISO10030,        "ISO 10030" },
225         { NLPID_ISO11577,        "ISO 11577" },
226         { NLPID_COMPRESSED,      "Data compression protocol" },
227         { NLPID_IP,              "IP" },
228         { NLPID_SNDCF,           "SubNetwork Dependent Convergence Function"},
229         { NLPID_IP6,             "IPv6" },
230         { NLPID_PPP,             "PPP" },
231         { 0,                     NULL },
232 };
233
234 static dissector_table_t osinl_subdissector_table;
235 static dissector_table_t osinl_excl_subdissector_table;
236 static dissector_handle_t data_handle, ppp_handle;
237
238 /* Dissect OSI over TCP over TPKT */
239 static void
240 dissect_osi_tpkt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
241 {
242   dissect_tpkt_encap(tvb, pinfo, tree, tpkt_desegment, osi_handle);
243 }
244
245 static void dissect_osi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
246 {
247   guint8 nlpid;
248   tvbuff_t *new_tvb;
249
250   pinfo->current_proto = "OSI";
251
252   nlpid = tvb_get_guint8(tvb, 0);
253
254   /* try lookup with the subdissector tables that includes the nlpid */
255   if (dissector_try_port(osinl_subdissector_table, nlpid, tvb, pinfo, tree))
256     return;
257   /* try lookup with the subdissector tables that excludes the nlpid */
258   new_tvb = tvb_new_subset(tvb, 1, -1, -1);
259   if (dissector_try_port(osinl_excl_subdissector_table, nlpid, new_tvb, pinfo, tree))
260     return;
261
262   switch (nlpid) {
263
264     /* ESIS (X.25) is not currently decoded */
265
266     case NLPID_ISO9542X25_ESIS:
267       if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
268         col_set_str(pinfo->cinfo, COL_PROTOCOL, "ESIS (X.25)");
269       }
270       call_dissector(data_handle,tvb, pinfo, tree);
271       break;
272     case NLPID_ISO10747_IDRP:
273       if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
274         col_set_str(pinfo->cinfo, COL_PROTOCOL, "IDRP");
275       }
276       call_dissector(data_handle,tvb, pinfo, tree);
277       break;
278     default:
279       if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
280         col_set_str(pinfo->cinfo, COL_PROTOCOL, "ISO");
281       }
282       if (check_col(pinfo->cinfo, COL_INFO)) {
283         col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown ISO protocol (%02x)", nlpid);
284       }
285       call_dissector(data_handle,tvb, pinfo, tree);
286       break;
287   }
288 } /* dissect_osi */
289
290 void
291 proto_reg_handoff_osi(void)
292 {
293   static gboolean osi_prefs_initialized = FALSE;
294   static dissector_handle_t osi_tpkt_handle;
295   static guint tcp_port_osi_over_tpkt;
296
297   if (!osi_prefs_initialized) {
298     osi_handle = create_dissector_handle(dissect_osi, proto_osi);
299     dissector_add("llc.dsap", SAP_OSINL1, osi_handle);
300     dissector_add("llc.dsap", SAP_OSINL2, osi_handle);
301     dissector_add("llc.dsap", SAP_OSINL3, osi_handle);
302     dissector_add("llc.dsap", SAP_OSINL4, osi_handle);
303     dissector_add("llc.dsap", SAP_OSINL5, osi_handle);
304     dissector_add("ppp.protocol", PPP_OSI, osi_handle);
305     dissector_add("chdlctype", CHDLCTYPE_OSI, osi_handle);
306     dissector_add("null.type", BSD_AF_ISO, osi_handle);
307     dissector_add("gre.proto", SAP_OSINL5, osi_handle);
308     data_handle = find_dissector("data");
309     ppp_handle  = find_dissector("ppp");
310
311     osi_tpkt_handle = create_dissector_handle(dissect_osi_tpkt, proto_osi);
312     dissector_add_handle("tcp.port", osi_tpkt_handle); /* for 'decode-as' */
313     osi_prefs_initialized = TRUE;
314   } else {
315     if (tcp_port_osi_over_tpkt != 0) {
316       dissector_delete("tcp.port", tcp_port_osi_over_tpkt, osi_tpkt_handle);
317     }
318   }
319
320   if (global_tcp_port_osi_over_tpkt != 0) {
321     dissector_add("tcp.port", global_tcp_port_osi_over_tpkt, osi_tpkt_handle);
322   }
323   tcp_port_osi_over_tpkt = global_tcp_port_osi_over_tpkt;
324 }
325
326 void
327 proto_register_osi(void)
328 {
329   module_t *osi_module;
330
331   /* There's no "OSI" protocol *per se*, but we do register a
332      dissector table so various protocols running at the
333      network layer can register themselves.
334      all protocols that require inclusion of the NLPID
335      should register here
336   */
337   osinl_subdissector_table = register_dissector_table("osinl",
338                                                       "OSI incl NLPID", FT_UINT8, BASE_HEX);
339
340   /* This dissector table is for those protocols whose PDUs
341    * aren't* defined to begin with an NLPID.
342    * (typically non OSI protocols like IP,IPv6,PPP */
343   osinl_excl_subdissector_table = register_dissector_table("osinl.excl",
344                                                            "OSI excl NLPID", FT_UINT8, BASE_HEX);
345         
346   proto_osi = proto_register_protocol("OSI", "OSI", "osi");
347   /* Preferences how OSI protocols should be dissected */
348   osi_module = prefs_register_protocol(proto_osi, proto_reg_handoff_osi);
349
350   prefs_register_uint_preference(osi_module, "tpkt_port",
351                                  "TCP port for OSI over TPKT",
352                                  "TCP port for OSI over TPKT",
353                                  10, &global_tcp_port_osi_over_tpkt);
354   prefs_register_bool_preference(osi_module, "tpkt_reassemble",
355                                  "Reassemble segmented TPKT datagrams",
356                                  "Whether segmented TPKT datagrams should be reassembled",
357                                  &tpkt_desegment);
358
359 }
360