aboutsummaryrefslogtreecommitdiffstats
path: root/packet-osi.c
blob: ab3ab43f59fe8b04514982fdf3dd4ea9be33b2f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* packet-osi.c
 * Routines for ISO/OSI network and transport protocol packet disassembly
 * Main entrance point and common functions
 *
 * $Id: packet-osi.c,v 1.34 2000/08/07 03:20:58 guy Exp $
 * Laurent Deniel <deniel@worldnet.fr>
 * Ralf Schneider <Ralf.Schneider@t-online.de>
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@zing.org>
 * Copyright 1998 Gerald Combs
 *
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <glib.h>
#include "packet.h"
#include "llcsaps.h"
#include "nlpid.h"
#include "packet-osi.h"
#include "packet-clnp.h"
#include "packet-isis.h"
#include "packet-esis.h"


gchar *print_system_id( const u_char *buffer, int length ) {
  int           tmp;
  u_char       *cur; 
  static gchar  str[MAX_SYSTEMID_LEN * 3 + 5]; /* Don't trust exact matching */  
 
  if ( ( length <= 0 ) || ( length > MAX_SYSTEMID_LEN ) ) {
    sprintf( str, "<Invalid length of SYSTEM ID>");
    return( str );
  }  
 
  cur = str;
  if ( ( 6 == length ) || ( 7 == length ) ) { /* Special case, print as MAC */
    cur += sprintf(str, "[%02x:%02x:%02x_%02x:%02x:%02x]", buffer[0], buffer[1],
                    buffer[2], buffer[3], buffer[4], buffer[5] );
    if ( 7 == length ) {
      sprintf( cur, "-%02x", buffer[6] );
    }
  }
  else {
    tmp = 0;
    while ( tmp < length / 4 ) { /* 16 / 4 == 4 > four Octets left to print */
      cur += sprintf( str, "%02x%02x%02x%02x.", buffer[tmp++], buffer[tmp++],
                      buffer[tmp++], buffer[tmp++] );
    }
    if ( 1 == tmp ) {   /* Special case for Designated IS */
      sprintf( --cur, "-%02x", buffer[tmp] );
    }
    else {
      for ( ; tmp < length; ) {  /* print the rest without dot */
        cur += sprintf( cur, "%02x", buffer[tmp++] );
      }
    }
  }
  return( str );
}

gchar *print_area(const u_char *buffer, int length)
{
  /* to do : all real area decoding now: NET is assumed if id len is 1 more byte
   * and take away all these stupid resource consuming local statics
   */
  
  static gchar  str[MAX_AREA_LEN * 3 + 20]; /* reserve space for nice layout */
  gchar *cur;
  u_int  tmp  = 0;

  cur = str;

  if (length <= 0 || length > MAX_AREA_LEN) {
    sprintf( str, "<Invalid length of AREA>");
    return( str );
  }
  
  if ( (  ( NSAP_IDI_ISODCC          == *buffer )      
       || ( NSAP_IDI_GOSIP2          == *buffer )
       )
       && 
       (  ( RFC1237_FULLAREA_LEN     ==  length ) 
       || ( RFC1237_FULLAREA_LEN + 1 ==  length )
       ) 
     ) {    /* AFI is good and length is long enough  */
  
    if ( length > RFC1237_FULLAREA_LEN + 1 ) {  /* Special Case Designated IS */
      sprintf( str, "<Invalid length of AREA for DCC / GOSIP AFI>");
      return( str );
    }
 
    cur += sprintf( cur, "[%02x|%02x:%02x][%02x|%02x:%02x:%02x|%02x:%02x]", 
                    buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], 
                    buffer[5], buffer[6], buffer[7], buffer[8] );
    cur += sprintf( cur, "[%02x:%02x|%02x:%02x]",
                    buffer[9], buffer[10],  buffer[11], buffer[12] );
    if ( RFC1237_FULLAREA_LEN + 1 == length ) {
      sprintf( cur, "-[%02x]", buffer[20] );
    }
    return str;
  }
  else { /* print standard format */
    if ( 4 < length ) { 
      while ( tmp < length / 4 ) {      /* 16/4==4  four Octets left to print */
        cur += sprintf( str, "%02x%02x%02x%02x.", buffer[tmp++], buffer[tmp++],
                        buffer[tmp++], buffer[tmp++] );
      }
      if ( 1 == tmp ) {                     /* Special case for Designated IS */
        sprintf( --cur, "-%02x", buffer[tmp] );
      }
      else {
        for ( ; tmp < length; ) {  /* print the rest without dot */ 
          cur += sprintf( cur, "%02x", buffer[tmp++] );
        }
      } 
    }
    return( str );
  }
} /* print_area */


gchar *print_nsap_net( const u_char *buffer, int length)
{
  /* to do : NSAP / NET decoding */

  static gchar  str[MAX_NSAP_LEN * 3 + 50]; /* reserve space for nice layout */
  gchar *cur;

  cur = str;

  if ( (length <= 0 ) || ( length > MAX_NSAP_LEN ) ) {
    sprintf( str, "<Invalid length of NSAP>");
    return( str );
  }
  if ( ( length == RFC1237_NSAP_LEN ) || ( length == RFC1237_NSAP_LEN + 1 ) ) {
    cur += sprintf( cur, "%s", print_area( buffer, RFC1237_FULLAREA_LEN ) );
    cur += sprintf( cur, "%s", print_system_id( buffer + RFC1237_FULLAREA_LEN, 
                    RFC1237_SYSTEMID_LEN ) );
    cur += sprintf( cur, "[%02x]", 
                    buffer[ RFC1237_FULLAREA_LEN + RFC1237_SYSTEMID_LEN ] );
    if ( length == RFC1237_NSAP_LEN + 1 ) {
      cur += sprintf( cur, "-%02x", buffer[ length -1 ] );
    }
    return ( str );
  }
  else {    /* probably format as standard */
    return( print_area( buffer, length ) );
  }
} /* print_nsap */


gchar *calc_checksum( const u_char *buffer, u_int len, u_int checksum) {
  u_int   calc_sum = 0;
  u_int   count    = 0;

  static char *checksum_string[] = { "Not Used", 
                                     "Is good",
                                     "Is wrong" }; 
  if ( 0 == checksum )
    return( checksum_string[0] );

  for ( count = 0; count < len; count++ ) {
    calc_sum += (u_int) buffer[count];
  }
  calc_sum %= 255;  /* modulo 255 divison */
  
  if ( 0 == calc_sum )
    return( checksum_string[1] );
  else
    return( checksum_string[2] );
}


/* main entry point */

const value_string nlpid_vals[] = {
	{ NLPID_NULL,            "NULL" },
	{ NLPID_T_70,            "T.70" },
	{ NLPID_X_633,           "X.633" },
	{ NLPID_Q_931,           "Q.931" },
	{ NLPID_Q_2931,          "Q.2931" },
	{ NLPID_Q_2119,          "Q.2119" },
	{ NLPID_SNAP,            "SNAP" },
	{ NLPID_ISO8473_CLNP,    "CLNP" },
	{ NLPID_ISO9542_ESIS,    "ESIS" },
	{ NLPID_ISO10589_ISIS,   "ISIS" },
	{ NLPID_ISO10747_IDRP,   "IDRP" },
	{ NLPID_ISO9542X25_ESIS, "ESIS (X.25)" },
	{ NLPID_ISO10030,        "ISO 10030" },
	{ NLPID_ISO11577,        "ISO 11577" },
	{ NLPID_IP,              "IP" },
	{ NLPID_PPP,             "PPP" },
	{ 0,                     NULL },
};

static dissector_table_t subdissector_table;

void dissect_osi(const u_char *pd, int offset, frame_data *fd, 
		 proto_tree *tree) 
{
  /* do lookup with the subdissector table */
  if (old_dissector_try_port(subdissector_table, pd[offset], pd, offset, fd, tree))
      return;

  switch (pd[offset]) {

    /* ESIS (X.25) is not currently decoded */

    case NLPID_ISO9542X25_ESIS:
      if (check_col(fd, COL_PROTOCOL)) {
	col_add_str(fd, COL_PROTOCOL, "ESIS (X.25)");
      }
      old_dissect_data(pd, offset, fd, tree);
      break;
    case NLPID_ISO10747_IDRP:
      if (check_col(fd, COL_PROTOCOL)) {
        col_add_str(fd, COL_PROTOCOL, "IDRP");
      }
      old_dissect_data(pd, offset, fd, tree);
      break;
    default:
      if (check_col(fd, COL_PROTOCOL)) {
	col_add_str(fd, COL_PROTOCOL, "ISO");
      }
      if (check_col(fd, COL_INFO)) {
	col_add_fstr(fd, COL_INFO, "Unknown ISO protocol (%02x)", pd[offset]);
      }
      old_dissect_data(pd, offset, fd, tree);
      break;
  }
} /* dissect_osi */

void
proto_register_osi(void)
{
	/* There's no "OSI" protocol *per se*, but we do register a
	   dissector table so various protocols running at the
	   network layer can register themselves. */
	subdissector_table = register_dissector_table("osinl");
}

void
proto_reg_handoff_osi(void)
{
	old_dissector_add("llc.dsap", SAP_OSINL, dissect_osi);
}