Tvbuffify the IP, ICMP, TCP, UDP, OSI CLNP, OSI COTP, OSI CLTP, and OSI
[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.36 2000/11/18 10:38:24 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@zing.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * 
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  * 
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  *
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #include <stdio.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <glib.h>
42 #include "packet.h"
43 #include "llcsaps.h"
44 #include "aftypes.h"
45 #include "nlpid.h"
46 #include "packet-osi.h"
47 #include "packet-clnp.h"
48 #include "packet-isis.h"
49 #include "packet-esis.h"
50
51
52 gchar *print_system_id( const u_char *buffer, int length ) {
53   int           tmp;
54   u_char       *cur; 
55   static gchar  str[MAX_SYSTEMID_LEN * 3 + 5]; /* Don't trust exact matching */  
56  
57   if ( ( length <= 0 ) || ( length > MAX_SYSTEMID_LEN ) ) {
58     sprintf( str, "<Invalid length of SYSTEM ID>");
59     return( str );
60   }  
61  
62   cur = str;
63   if ( ( 6 == length ) || ( 7 == length ) ) { /* Special case, print as MAC */
64     cur += sprintf(str, "[%02x:%02x:%02x_%02x:%02x:%02x]", buffer[0], buffer[1],
65                     buffer[2], buffer[3], buffer[4], buffer[5] );
66     if ( 7 == length ) {
67       sprintf( cur, "-%02x", buffer[6] );
68     }
69   }
70   else {
71     tmp = 0;
72     while ( tmp < length / 4 ) { /* 16 / 4 == 4 > four Octets left to print */
73       cur += sprintf( str, "%02x%02x%02x%02x.", buffer[tmp++], buffer[tmp++],
74                       buffer[tmp++], buffer[tmp++] );
75     }
76     if ( 1 == tmp ) {   /* Special case for Designated IS */
77       sprintf( --cur, "-%02x", buffer[tmp] );
78     }
79     else {
80       for ( ; tmp < length; ) {  /* print the rest without dot */
81         cur += sprintf( cur, "%02x", buffer[tmp++] );
82       }
83     }
84   }
85   return( str );
86 }
87
88 gchar *print_area(const u_char *buffer, int length)
89 {
90   /* to do : all real area decoding now: NET is assumed if id len is 1 more byte
91    * and take away all these stupid resource consuming local statics
92    */
93   
94   static gchar  str[MAX_AREA_LEN * 3 + 20]; /* reserve space for nice layout */
95   gchar *cur;
96   u_int  tmp  = 0;
97
98   cur = str;
99
100   if (length <= 0 || length > MAX_AREA_LEN) {
101     sprintf( str, "<Invalid length of AREA>");
102     return( str );
103   }
104   
105   if ( (  ( NSAP_IDI_ISODCC          == *buffer )      
106        || ( NSAP_IDI_GOSIP2          == *buffer )
107        )
108        && 
109        (  ( RFC1237_FULLAREA_LEN     ==  length ) 
110        || ( RFC1237_FULLAREA_LEN + 1 ==  length )
111        ) 
112      ) {    /* AFI is good and length is long enough  */
113   
114     if ( length > RFC1237_FULLAREA_LEN + 1 ) {  /* Special Case Designated IS */
115       sprintf( str, "<Invalid length of AREA for DCC / GOSIP AFI>");
116       return( str );
117     }
118  
119     cur += sprintf( cur, "[%02x|%02x:%02x][%02x|%02x:%02x:%02x|%02x:%02x]", 
120                     buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], 
121                     buffer[5], buffer[6], buffer[7], buffer[8] );
122     cur += sprintf( cur, "[%02x:%02x|%02x:%02x]",
123                     buffer[9], buffer[10],  buffer[11], buffer[12] );
124     if ( RFC1237_FULLAREA_LEN + 1 == length ) {
125       sprintf( cur, "-[%02x]", buffer[20] );
126     }
127     return str;
128   }
129   else { /* print standard format */
130     if ( 4 < length ) { 
131       while ( tmp < length / 4 ) {      /* 16/4==4  four Octets left to print */
132         cur += sprintf( str, "%02x%02x%02x%02x.", buffer[tmp++], buffer[tmp++],
133                         buffer[tmp++], buffer[tmp++] );
134       }
135       if ( 1 == tmp ) {                     /* Special case for Designated IS */
136         sprintf( --cur, "-%02x", buffer[tmp] );
137       }
138       else {
139         for ( ; tmp < length; ) {  /* print the rest without dot */ 
140           cur += sprintf( cur, "%02x", buffer[tmp++] );
141         }
142       } 
143     }
144     return( str );
145   }
146 } /* print_area */
147
148
149 gchar *print_nsap_net( const u_char *buffer, int length)
150 {
151   /* to do : NSAP / NET decoding */
152
153   static gchar  str[MAX_NSAP_LEN * 3 + 50]; /* reserve space for nice layout */
154   gchar *cur;
155
156   cur = str;
157
158   if ( (length <= 0 ) || ( length > MAX_NSAP_LEN ) ) {
159     sprintf( str, "<Invalid length of NSAP>");
160     return( str );
161   }
162   if ( ( length == RFC1237_NSAP_LEN ) || ( length == RFC1237_NSAP_LEN + 1 ) ) {
163     cur += sprintf( cur, "%s", print_area( buffer, RFC1237_FULLAREA_LEN ) );
164     cur += sprintf( cur, "%s", print_system_id( buffer + RFC1237_FULLAREA_LEN, 
165                     RFC1237_SYSTEMID_LEN ) );
166     cur += sprintf( cur, "[%02x]", 
167                     buffer[ RFC1237_FULLAREA_LEN + RFC1237_SYSTEMID_LEN ] );
168     if ( length == RFC1237_NSAP_LEN + 1 ) {
169       cur += sprintf( cur, "-%02x", buffer[ length -1 ] );
170     }
171     return ( str );
172   }
173   else {    /* probably format as standard */
174     return( print_area( buffer, length ) );
175   }
176 } /* print_nsap */
177
178
179 gchar *calc_checksum( tvbuff_t *tvb, int offset, u_int len, u_int checksum) {
180   u_int   calc_sum = 0;
181   u_int   count    = 0;
182   const gchar *buffer;
183   guint   available_len;
184
185   if ( 0 == checksum )
186     return( "Not Used" );
187
188   available_len = tvb_length_remaining( tvb, offset );
189   if ( available_len < len )
190     return( "Not checkable - not all of packet was captured" );
191
192   buffer = tvb_get_ptr( tvb, offset, len );
193   for ( count = 0; count < len; count++ ) {
194     calc_sum += (u_int) buffer[count];
195   }
196   calc_sum %= 255;  /* modulo 255 divison */
197   
198   if ( 0 == calc_sum )
199     return( "Is good" );
200   else
201     return( "Is wrong" );       /* XXX - what should the checksum be? */
202 }
203
204
205 /* main entry point */
206
207 const value_string nlpid_vals[] = {
208         { NLPID_NULL,            "NULL" },
209         { NLPID_T_70,            "T.70" },
210         { NLPID_X_633,           "X.633" },
211         { NLPID_Q_931,           "Q.931" },
212         { NLPID_Q_2931,          "Q.2931" },
213         { NLPID_Q_2119,          "Q.2119" },
214         { NLPID_SNAP,            "SNAP" },
215         { NLPID_ISO8473_CLNP,    "CLNP" },
216         { NLPID_ISO9542_ESIS,    "ESIS" },
217         { NLPID_ISO10589_ISIS,   "ISIS" },
218         { NLPID_ISO10747_IDRP,   "IDRP" },
219         { NLPID_ISO9542X25_ESIS, "ESIS (X.25)" },
220         { NLPID_ISO10030,        "ISO 10030" },
221         { NLPID_ISO11577,        "ISO 11577" },
222         { NLPID_IP,              "IP" },
223         { NLPID_PPP,             "PPP" },
224         { 0,                     NULL },
225 };
226
227 static dissector_table_t subdissector_table;
228
229 void dissect_osi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) 
230 {
231   guint8 nlpid;
232
233   pinfo->current_proto = "OSI";
234
235   nlpid = tvb_get_guint8(tvb, 0);
236
237   /* do lookup with the subdissector table */
238   if (dissector_try_port(subdissector_table, nlpid, tvb, pinfo, tree))
239       return;
240
241   switch (nlpid) {
242
243     /* ESIS (X.25) is not currently decoded */
244
245     case NLPID_ISO9542X25_ESIS:
246       if (check_col(pinfo->fd, COL_PROTOCOL)) {
247         col_add_str(pinfo->fd, COL_PROTOCOL, "ESIS (X.25)");
248       }
249       dissect_data(tvb, 0, pinfo, tree);
250       break;
251     case NLPID_ISO10747_IDRP:
252       if (check_col(pinfo->fd, COL_PROTOCOL)) {
253         col_add_str(pinfo->fd, COL_PROTOCOL, "IDRP");
254       }
255       dissect_data(tvb, 0, pinfo, tree);
256       break;
257     default:
258       if (check_col(pinfo->fd, COL_PROTOCOL)) {
259         col_add_str(pinfo->fd, COL_PROTOCOL, "ISO");
260       }
261       if (check_col(pinfo->fd, COL_INFO)) {
262         col_add_fstr(pinfo->fd, COL_INFO, "Unknown ISO protocol (%02x)", nlpid);
263       }
264       dissect_data(tvb, 0, pinfo, tree);
265       break;
266   }
267 } /* dissect_osi */
268
269 void
270 proto_register_osi(void)
271 {
272         /* There's no "OSI" protocol *per se*, but we do register a
273            dissector table so various protocols running at the
274            network layer can register themselves. */
275         subdissector_table = register_dissector_table("osinl");
276 }
277
278 void
279 proto_reg_handoff_osi(void)
280 {
281         dissector_add("llc.dsap", SAP_OSINL, dissect_osi);
282         dissector_add("null.type", BSD_AF_ISO, dissect_osi);
283 }