The function pointer in a "per_choice_t" or a "per_sequence_t" is to a
[obnox/wireshark/wip.git] / 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: packet-osi.c,v 1.63 2003/09/20 03:31:25 guy Exp $
6  * Laurent Deniel <laurent.deniel@free.fr>
7  * Ralf Schneider <Ralf.Schneider@t-online.de>
8  *
9  * Ethereal - Network traffic analyzer
10  * By Gerald Combs <gerald@ethereal.com>
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/packet.h>
37 #include "llcsaps.h"
38 #include "aftypes.h"
39 #include "nlpid.h"
40 #include "ppptypes.h"
41 #include "chdlctypes.h"
42 #include "packet-osi.h"
43 #include "packet-isis.h"
44 #include "packet-esis.h"
45
46
47 cksum_status_t
48 calc_checksum( tvbuff_t *tvb, int offset, guint len, guint checksum) {
49   const gchar *buffer;
50   guint   available_len;
51   const guint8 *p;
52   guint32 c0, c1;
53   guint   seglen;
54   guint   i;
55
56   if ( 0 == checksum )
57     return( NO_CKSUM );
58
59   available_len = tvb_length_remaining( tvb, offset );
60   if ( available_len < len )
61     return( DATA_MISSING );
62
63   buffer = tvb_get_ptr( tvb, offset, len );
64
65   /*
66    * The maximum values of c0 and c1 will occur if all bytes have the
67    * value 255; if so, then c0 will be len*255 and c1 will be
68    * (len*255 + (len-1)*255 + ... + 255), which is
69    * (len + (len - 1) + ... + 1)*255, or 255*(len*(len + 1))/2.
70    * This means it can overflow if "len" is 5804 or greater.
71    *
72    * (A+B) mod 255 = ((A mod 255) + (B mod 255) mod 255, so
73    * we can solve this by taking c0 and c1 mod 255 every
74    * 5803 bytes.
75    */
76   p = buffer;
77   c0 = 0;
78   c1 = 0;
79   while (len != 0) {
80     seglen = len;
81     if (seglen > 5803)
82       seglen = 5803;
83     for (i = 0; i < seglen; i++) {
84       c0 = c0 + *(p++);
85       c1 += c0;
86     }
87
88     c0 = c0 % 255;
89     c1 = c1 % 255;
90
91     len -= seglen;
92   }
93   if (c0 != 0 || c1 != 0)
94     return( CKSUM_NOT_OK );     /* XXX - what should the checksum field be? */
95   else
96     return( CKSUM_OK );
97 }
98
99
100 cksum_status_t
101 check_and_get_checksum( tvbuff_t *tvb, int offset, guint len, guint checksum, int offset_check, guint16* result) {
102   const gchar *buffer;
103   guint   available_len;
104   const guint8 *p;
105   guint8 discard = 0;
106   guint32 c0, c1, factor;
107   guint   seglen, initlen = len;
108   guint   i;
109   int     block, x, y;
110
111   if ( 0 == checksum )
112     return( NO_CKSUM );
113
114   available_len = tvb_length_remaining( tvb, offset );
115   offset_check -= offset;
116   if ( ( available_len < len ) || ( offset_check < 0 ) || ( (guint)(offset_check+2) > len ) )
117     return( DATA_MISSING );
118
119   buffer = tvb_get_ptr( tvb, offset, len );
120   block  = offset_check / 5803;
121
122   /*
123    * The maximum values of c0 and c1 will occur if all bytes have the
124    * value 255; if so, then c0 will be len*255 and c1 will be
125    * (len*255 + (len-1)*255 + ... + 255), which is
126    * (len + (len - 1) + ... + 1)*255, or 255*(len*(len + 1))/2.
127    * This means it can overflow if "len" is 5804 or greater.
128    *
129    * (A+B) mod 255 = ((A mod 255) + (B mod 255) mod 255, so
130    * we can solve this by taking c0 and c1 mod 255 every
131    * 5803 bytes.
132    */
133   p = buffer;
134   c0 = 0;
135   c1 = 0;
136
137   while (len != 0) {
138     seglen = len;
139     if ( block-- == 0 ) {
140       seglen = offset_check % 5803;
141       discard = 1;
142     } else if ( seglen > 5803 )
143       seglen = 5803;
144     for (i = 0; i < seglen; i++) {
145       c0 = c0 + *(p++);
146       c1 += c0;
147     }
148     if ( discard ) {
149       /*
150        * This works even if (offset_check % 5803) == 5802
151        */
152       p += 2;
153       c1 += 2*c0;
154       len -= 2;
155       discard = 0;
156     }
157
158     c0 = c0 % 255;
159     c1 = c1 % 255;
160
161     len -= seglen;
162   }
163
164   factor = ( initlen - offset_check ) * c0;
165   x = factor - c0 - c1;
166   y = c1 - factor - 1;
167
168   /*
169    * This algorithm uses the 8 bits one's complement arithmetic.
170    * Therefore, we must correct an effect produced
171    * by the "standard" arithmetic (two's complement)
172    */
173
174   if (x < 0 ) x--;
175   if (y > 0 ) y++;
176
177   x %= 255;
178   y %= 255;
179
180   if (x == 0) x = 0xFF;
181   if (y == 0) y = 0x01;
182
183   *result = ( x << 8 ) | ( y & 0xFF );
184
185   if (*result != checksum)
186     return( CKSUM_NOT_OK );     /* XXX - what should the checksum field be? */
187   else
188     return( CKSUM_OK );
189 }
190
191
192
193 /* main entry point */
194
195 /*
196  * These assume the NLPID is a secondary protocol identifier, not an
197  * initial protocol identifier.
198  *
199  * This is an issue only if, in any packet where an NLPID appears, it's
200  * an initial protocol identifier *AND* it can have the value 1, which
201  * means T.70 for an IPI and X.29 for an SPI.
202  */
203 const value_string nlpid_vals[] = {
204         { NLPID_NULL,            "NULL" },
205         { NLPID_SPI_X_29,        "X.29" },
206         { NLPID_X_633,           "X.633" },
207         { NLPID_Q_931,           "Q.931" },
208         { NLPID_Q_2931,          "Q.2931" },
209         { NLPID_Q_2119,          "Q.2119" },
210         { NLPID_SNAP,            "SNAP" },
211         { NLPID_ISO8473_CLNP,    "CLNP" },
212         { NLPID_ISO9542_ESIS,    "ESIS" },
213         { NLPID_ISO10589_ISIS,   "ISIS" },
214         { NLPID_ISO10747_IDRP,   "IDRP" },
215         { NLPID_ISO9542X25_ESIS, "ESIS (X.25)" },
216         { NLPID_ISO10030,        "ISO 10030" },
217         { NLPID_ISO11577,        "ISO 11577" },
218         { NLPID_COMPRESSED,      "Data compression protocol" },
219         { NLPID_IP,              "IP" },
220         { NLPID_SNDCF,           "SubNetwork Dependent Convergence Function"},
221         { NLPID_IP6,             "IPv6" },
222         { NLPID_PPP,             "PPP" },
223         { 0,                     NULL },
224 };
225
226 static dissector_table_t osinl_subdissector_table;
227 static dissector_handle_t data_handle, ppp_handle;
228
229 static void dissect_osi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
230 {
231   guint8 nlpid;
232   tvbuff_t *new_tvb;
233
234   pinfo->current_proto = "OSI";
235
236   nlpid = tvb_get_guint8(tvb, 0);
237
238   /* do lookup with the subdissector table */
239   if (dissector_try_port(osinl_subdissector_table, nlpid, tvb, pinfo, tree))
240       return;
241
242   switch (nlpid) {
243
244     /* ESIS (X.25) is not currently decoded */
245
246     case NLPID_ISO9542X25_ESIS:
247       if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
248         col_set_str(pinfo->cinfo, COL_PROTOCOL, "ESIS (X.25)");
249       }
250       call_dissector(data_handle,tvb, pinfo, tree);
251       break;
252     case NLPID_ISO10747_IDRP:
253       if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
254         col_set_str(pinfo->cinfo, COL_PROTOCOL, "IDRP");
255       }
256       call_dissector(data_handle,tvb, pinfo, tree);
257       break;
258     case NLPID_PPP:
259       /* XXX - we should put the NLPID into the protocol tree.
260          We should also probably have a subdissector table for
261          those protocols whose PDUs *aren't* defined to begin
262          with an NLPID. */
263       new_tvb = tvb_new_subset(tvb, 1, -1, -1);
264       call_dissector(ppp_handle, new_tvb, pinfo, tree);
265       break;
266     default:
267       if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
268         col_set_str(pinfo->cinfo, COL_PROTOCOL, "ISO");
269       }
270       if (check_col(pinfo->cinfo, COL_INFO)) {
271         col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown ISO protocol (%02x)", nlpid);
272       }
273       call_dissector(data_handle,tvb, pinfo, tree);
274       break;
275   }
276 } /* dissect_osi */
277
278 void
279 proto_register_osi(void)
280 {
281         /* There's no "OSI" protocol *per se*, but we do register a
282            dissector table so various protocols running at the
283            network layer can register themselves. */
284         osinl_subdissector_table = register_dissector_table("osinl",
285             "OSI NLPID", FT_UINT8, BASE_HEX);
286 }
287
288 void
289 proto_reg_handoff_osi(void)
290 {
291         dissector_handle_t osi_handle;
292
293         osi_handle = create_dissector_handle(dissect_osi, -1);
294         dissector_add("llc.dsap", SAP_OSINL1, osi_handle);
295         dissector_add("llc.dsap", SAP_OSINL2, osi_handle);
296         dissector_add("llc.dsap", SAP_OSINL3, osi_handle);
297         dissector_add("llc.dsap", SAP_OSINL4, osi_handle);
298         dissector_add("llc.dsap", SAP_OSINL5, osi_handle);
299         dissector_add("ppp.protocol", PPP_OSI, osi_handle);
300         dissector_add("chdlctype", CHDLCTYPE_OSI, osi_handle);
301         dissector_add("null.type", BSD_AF_ISO, osi_handle);
302         dissector_add("gre.proto", SAP_OSINL5, osi_handle);
303         data_handle = find_dissector("data");
304         ppp_handle  = find_dissector("ppp");
305 }