Mark requests as such in the Info column.
[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.53 2002/01/21 07:36:38 guy Exp $
6  * Laurent Deniel <deniel@worldnet.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 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <glib.h>
40 #include <epan/packet.h>
41 #include "llcsaps.h"
42 #include "aftypes.h"
43 #include "nlpid.h"
44 #include "ppptypes.h"
45 #include "packet-osi.h"
46 #include "packet-isis.h"
47 #include "packet-esis.h"
48
49
50 cksum_status_t
51 calc_checksum( tvbuff_t *tvb, int offset, u_int len, u_int checksum) {
52   const gchar *buffer;
53   guint   available_len;
54   const guint8 *p;
55   guint32 c0, c1;
56   u_int   seglen;
57   u_int   i;
58
59   if ( 0 == checksum )
60     return( NO_CKSUM );
61
62   available_len = tvb_length_remaining( tvb, offset );
63   if ( available_len < len )
64     return( DATA_MISSING );
65
66   buffer = tvb_get_ptr( tvb, offset, len );
67
68   /*
69    * The maximum values of c0 and c1 will occur if all bytes have the
70    * value 255; if so, then c0 will be len*255 and c1 will be
71    * (len*255 + (len-1)*255 + ... + 255), which is
72    * (len + (len - 1) + ... + 1)*255, or 255*(len*(len + 1))/2.
73    * This means it can overflow if "len" is 5804 or greater.
74    *
75    * (A+B) mod 255 = ((A mod 255) + (B mod 255) mod 255, so
76    * we can solve this by taking c0 and c1 mod 255 every
77    * 5803 bytes.
78    */
79   p = buffer;
80   c0 = 0;
81   c1 = 0;
82   while (len != 0) {
83     seglen = len;
84     if (seglen > 5803)
85       seglen = 5803;
86     for (i = 0; i < seglen; i++) {
87       c0 = c0 + *(p++);
88       c1 += c0;
89     }
90
91     c0 = c0 % 255;
92     c1 = c1 % 255;
93
94     len -= seglen;
95   }
96   if (c0 != 0 || c1 != 0)
97     return( CKSUM_NOT_OK );     /* XXX - what should the checksum be? */
98   else
99     return( CKSUM_OK );
100 }
101
102
103 /* main entry point */
104
105 /*
106  * These assume the NLPID is a secondary protocol identifier, not an
107  * initial protocol identifier.
108  *
109  * This is an issue only if, in any packet where an NLPID appears, it's
110  * an initial protocol identifier *AND* it can have the value 1, which
111  * means T.70 for an IPI and X.29 for an SPI.
112  */
113 const value_string nlpid_vals[] = {
114         { NLPID_NULL,            "NULL" },
115         { NLPID_SPI_X_29,        "X.29" },
116         { NLPID_X_633,           "X.633" },
117         { NLPID_Q_931,           "Q.931" },
118         { NLPID_Q_2931,          "Q.2931" },
119         { NLPID_Q_2119,          "Q.2119" },
120         { NLPID_SNAP,            "SNAP" },
121         { NLPID_ISO8473_CLNP,    "CLNP" },
122         { NLPID_ISO9542_ESIS,    "ESIS" },
123         { NLPID_ISO10589_ISIS,   "ISIS" },
124         { NLPID_ISO10747_IDRP,   "IDRP" },
125         { NLPID_ISO9542X25_ESIS, "ESIS (X.25)" },
126         { NLPID_ISO10030,        "ISO 10030" },
127         { NLPID_ISO11577,        "ISO 11577" },
128         { NLPID_COMPRESSED,      "Data compression protocol" },
129         { NLPID_IP,              "IP" },
130         { NLPID_IP6,             "IPv6" },
131         { NLPID_PPP,             "PPP" },
132         { 0,                     NULL },
133 };
134
135 dissector_table_t osinl_subdissector_table;
136 static dissector_handle_t data_handle;
137
138 static void dissect_osi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) 
139 {
140   guint8 nlpid;
141
142   pinfo->current_proto = "OSI";
143
144   nlpid = tvb_get_guint8(tvb, 0);
145
146   /* do lookup with the subdissector table */
147   if (dissector_try_port(osinl_subdissector_table, nlpid, tvb, pinfo, tree))
148       return;
149
150   switch (nlpid) {
151
152     /* ESIS (X.25) is not currently decoded */
153
154     case NLPID_ISO9542X25_ESIS:
155       if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
156         col_set_str(pinfo->cinfo, COL_PROTOCOL, "ESIS (X.25)");
157       }
158       call_dissector(data_handle,tvb, pinfo, tree);
159       break;
160     case NLPID_ISO10747_IDRP:
161       if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
162         col_set_str(pinfo->cinfo, COL_PROTOCOL, "IDRP");
163       }
164       call_dissector(data_handle,tvb, pinfo, tree);
165       break;
166     default:
167       if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
168         col_set_str(pinfo->cinfo, COL_PROTOCOL, "ISO");
169       }
170       if (check_col(pinfo->cinfo, COL_INFO)) {
171         col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown ISO protocol (%02x)", nlpid);
172       }
173       call_dissector(data_handle,tvb, pinfo, tree);
174       break;
175   }
176 } /* dissect_osi */
177
178 void
179 proto_register_osi(void)
180 {
181         /* There's no "OSI" protocol *per se*, but we do register a
182            dissector table so various protocols running at the
183            network layer can register themselves. */
184         osinl_subdissector_table = register_dissector_table("osinl",
185             "OSI NLPID", FT_UINT8, BASE_HEX);
186 }
187
188 void
189 proto_reg_handoff_osi(void)
190 {
191         dissector_handle_t osi_handle;
192
193         osi_handle = create_dissector_handle(dissect_osi, -1);
194         dissector_add("llc.dsap", SAP_OSINL, osi_handle);
195         dissector_add("ppp.protocol", PPP_OSI, osi_handle);
196         dissector_add("null.type", BSD_AF_ISO, osi_handle);
197         dissector_add("gre.proto", SAP_OSINL, osi_handle);
198         data_handle = find_dissector("data");
199 }