Include files from the "epan" directory and subdirectories thereof with
[obnox/wireshark/wip.git] / packet-esis.c
1 /* packet-esis.c
2  * Routines for ISO/OSI End System to Intermediate System  
3  * Routing Exchange Protocol ISO 9542.
4  *
5  * $Id: packet-esis.c,v 1.20 2002/01/21 07:36:34 guy Exp $
6  * Ralf Schneider <Ralf.Schneider@t-online.de>
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <stdio.h>
36 #include <string.h>
37 #include <glib.h>
38 #include <epan/packet.h>
39 #include "nlpid.h"
40 #include "packet-osi.h"
41 #include "packet-osi-options.h"
42 #include "packet-esis.h"
43
44
45 /* esis base header */
46 static int  proto_esis        = -1;
47
48 static int  hf_esis_nlpi      = -1;
49 static int  hf_esis_length    = -1;
50 static int  hf_esis_version   = -1;
51 static int  hf_esis_reserved  = -1;
52 static int  hf_esis_type      = -1;
53 static int  hf_esis_holdtime  = -1;
54 static int  hf_esis_checksum  = -1;
55
56 static gint ett_esis              = -1;
57 static gint ett_esis_area_addr    = -1;
58
59 static const value_string esis_vals[] = {
60   { ESIS_ESH_PDU, "ES HELLO"},
61   { ESIS_ISH_PDU, "IS HELLO"},
62   { ESIS_RD_PDU,  "RD REQUEST"},
63   { 0,             NULL} };
64
65 /* internal prototypes */
66
67 static void esis_dissect_esh_pdu( u_char len, tvbuff_t *tvb,
68                            packet_info *pinfo, proto_tree *treepd);
69 static void esis_dissect_ish_pdu( u_char len, tvbuff_t *tvb,
70                            packet_info *pinfo, proto_tree *tree);
71 static void esis_dissect_redirect_pdu( u_char len, tvbuff_t *tvb,
72                            packet_info *pinfo, proto_tree *tree);
73
74 /* ################## Descriptions ###########################################*/
75 /* Parameters for the ESH PDU
76  * Source Address Parameter:
77  *
78  * Octet:    Length:   Parameter Type:
79  *     10          1   Number of Source Adresses ( NSAPs served by this Network
80  *     11          1   Source Address Length Indicator ( SAL )     #    Entity )
81  * 12-m-1   variable   Source Address ( NSAP )
82  *      m              Options, dissected in osi.c
83  *
84  *
85  * Parameter for the ISH PDU:
86  * Network Entity Title Parameter:
87  *
88  * Octet:    Length:   Parameter Type:
89  *     10          1   Network Entity Title Length Indicator ( NETL )
90  * 11-m-1   variable   Network Entity Title ( NET )
91  *      m              Options, dissected in osi.c
92  *
93  *
94  * Parameter for the RD PDU:
95  * When re-directed to an IS:
96  *
97  *  Octet:   Length:   Parameter Type:
98  *      10         1   Destination Address Length Indicator ( DAL )
99  *  11>m-1  variable   Destination Address ( DA )
100  *       m         1   Subnetwork Address Length Indicator ( BSNPAL )
101  * m+1>n-1  variable   Subnetwork Address ( BSNPA )
102  *       n         1   Network Entity Title Length Indicator ( NETL )
103  * n+1>p-1  variable   Network Entity Title ( NET )
104  *       p             Options, dissected in osi.c
105  *
106  *
107  * Parameter for the RD PDU:
108  * When re-directed to an ES:
109  *
110  *  Octet:   Length:   Parameter Type:
111  *      10         1   Destination Address Length Indicator ( DAL )
112  *  11>m-1  variable   Destination Address ( DA )
113  *       m         1   Subnetwork Address Length Indicator ( BSNPAL )
114  * m+1>n-1  variable   Subnetwork Address ( BSNPA )
115  *       n         1   Network Entity Title Length Indicator ( NETL ) == 0
116  *     n+1             Options, dissected in osi.c
117  *
118  */
119
120 /* ############################ Tool Functions ############################## */
121
122
123 /* ############################## Dissection Functions ###################### */
124 /*
125  * Name: dissect_esis_unknown()
126  *
127  * Description:
128  *   There was some error in the protocol and we are in unknown space
129  *   here.  Add a tree item to cover the error and go on.  Note
130  *   that we make sure we don't go off the end of the bleedin packet here!
131  *
132  *   This is just a copy of isis.c and isis.h, so I keep the stuff also
133  *   and adapt the names to cover possible protocol errors! Ive really no
134  *   idea wether I need this or not.
135  *  
136  * Input
137  *   int offset      : Current offset into packet data.
138  *   int len         : length of to dump.
139  *   proto_tree *    : tree of display data.  May be NULL.
140  *   frame_data * fd : frame data
141  *   char *          : format text
142  *
143  * Output:
144  *   void (may modify proto tree)
145  */
146 static void
147 esis_dissect_unknown( tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
148                       char *fmat, ...){
149   va_list ap;
150
151   va_start(ap, fmat);
152   proto_tree_add_text_valist(tree, tvb, 0, tvb_length(tvb), fmat, ap);
153   va_end(ap);
154 }
155
156
157 static void
158 esis_dissect_esh_pdu( u_char len, tvbuff_t *tvb, packet_info *pinfo,
159                       proto_tree *tree) {
160   proto_tree *esis_area_tree;
161   int         offset  = 0;
162   int         no_sa   = 0;
163   int         sal     = 0;
164   
165   proto_item  *ti;
166   
167   if (tree) {
168     offset += ESIS_HDR_FIXED_LENGTH;
169
170     no_sa  = tvb_get_guint8(tvb, offset);
171     len   -= 1;
172
173     ti = proto_tree_add_text( tree, tvb, offset, tvb_length_remaining(tvb, offset),
174             "Number of Source Addresses (SA, Format: NSAP) : %u", no_sa );
175     offset++;
176     
177     esis_area_tree = proto_item_add_subtree( ti, ett_esis_area_addr );
178     while ( no_sa-- > 0 ) {
179        sal = (int) tvb_get_guint8(tvb, offset);
180        offset++;
181        proto_tree_add_text(esis_area_tree, tvb, offset, 1, "SAL: %2u Octets", sal);
182        proto_tree_add_text(esis_area_tree, tvb, offset + 1, sal,
183                            " SA: %s",
184                            print_nsap_net( tvb_get_ptr(tvb, offset, sal), sal ) );
185        offset += sal;
186        len    -= ( sal + 1 );
187     }
188     dissect_osi_options( PDU_TYPE_ESIS_ESH, len, tvb, offset, pinfo, tree );
189   }  
190 } /* esis_dissect_esh_pdu */ ;
191
192 static void
193 esis_dissect_ish_pdu( u_char len, tvbuff_t *tvb, packet_info *pinfo,
194                       proto_tree *tree) {
195   
196   int   offset  = 0;
197   int   netl    = 0;
198
199   if (tree) {
200     offset += ESIS_HDR_FIXED_LENGTH;
201
202     netl = (int) tvb_get_guint8(tvb, offset);
203     proto_tree_add_text( tree, tvb, offset, netl + 1, 
204                          "### Network Entity Titel Section ###");
205     proto_tree_add_text( tree, tvb, offset++, 1, "NETL: %2u Octets", netl);
206     proto_tree_add_text( tree, tvb, offset, netl,
207                            " NET: %s",
208                            print_nsap_net( tvb_get_ptr(tvb, offset, netl), netl ) );
209     offset += netl;
210     len    -= ( netl + 1 );
211
212     dissect_osi_options( PDU_TYPE_ESIS_ISH, len, tvb, offset, pinfo, tree );
213   }
214 };
215
216 static void
217 esis_dissect_redirect_pdu( u_char len, tvbuff_t *tvb, packet_info *pinfo,
218                            proto_tree *tree) {
219
220   int   offset  = 0;
221   int   tmpl    = 0;
222
223   if (tree) {
224     offset += ESIS_HDR_FIXED_LENGTH;
225
226     tmpl = (int) tvb_get_guint8(tvb, offset);
227     proto_tree_add_text( tree, tvb, offset, tmpl + 1, 
228                          "### Destination Address Section ###" );
229     proto_tree_add_text( tree, tvb, offset++, 1, "DAL: %2u Octets", tmpl);
230     proto_tree_add_text( tree, tvb, offset, tmpl,
231                          " DA : %s",
232                          print_nsap_net( tvb_get_ptr(tvb, offset, tmpl), tmpl ) );
233     offset += tmpl;
234     len    -= ( tmpl + 1 );
235     tmpl    = (int) tvb_get_guint8(tvb, offset);
236
237     proto_tree_add_text( tree, tvb, offset, tmpl + 1, 
238                          "###  Subnetwork Address Section ###");
239     proto_tree_add_text( tree, tvb, offset++, 1, "BSNPAL: %2u Octets", tmpl);
240     proto_tree_add_text( tree, tvb, offset, tmpl,
241                            " BSNPA: %s",
242                            print_system_id( tvb_get_ptr(tvb, offset, tmpl), tmpl ) );
243     offset += tmpl;
244     len    -= ( tmpl + 1 );
245     tmpl    = (int) tvb_get_guint8(tvb, offset);
246
247     if ( 0 == tmpl ) {
248       proto_tree_add_text( tree, tvb, offset, 1, 
249                            "### No Network Entity Title Section ###" );
250       offset++;
251       len--;
252     }
253     else {
254       proto_tree_add_text( tree, tvb, offset, 1,
255                            "### Network Entity Title Section ###" );
256       proto_tree_add_text( tree, tvb, offset++, 1, "NETL: %2u Octets", tmpl );
257       proto_tree_add_text( tree, tvb, offset, tmpl,
258                            " NET: %s",
259                            print_nsap_net( tvb_get_ptr(tvb, offset, tmpl), tmpl ) );
260       offset += tmpl;
261       len    -= ( tmpl + 1 );
262     }
263     dissect_osi_options( PDU_TYPE_ESIS_RD, len, tvb, offset, pinfo, tree );
264   }
265 }
266
267
268 /*
269  * Name: dissect_esis()
270  * 
271  * Description:
272  *   Main entry area for esis de-mangling.  This will build the
273  *   main esis tree data and call the sub-protocols as needed.
274  *
275  * Input:
276  *   tvbuff *     : tvbuff referring to packet data
277  *   frame_data * : frame data (whole packet with extra info)
278  *   proto_tree * : tree of display data.  May be NULL.
279  *
280  * Output:
281  *   void, but we will add to the proto_tree if it is not NULL.
282  */
283 static void
284 dissect_esis(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
285    char       *pdu_type_string        = NULL;
286    char       *pdu_type_format_string = "PDU Type      : %s (R:%s%s%s)";   
287    esis_hdr_t  ehdr;
288    proto_item *ti;
289    proto_tree *esis_tree    = NULL;
290    int         variable_len = 0;
291    u_int       tmp_uint     = 0;
292    char       *cksum_status;
293
294    if (check_col(pinfo->cinfo, COL_PROTOCOL))
295      col_set_str(pinfo->cinfo, COL_PROTOCOL, "ESIS");
296    if (check_col(pinfo->cinfo, COL_INFO))
297      col_clear(pinfo->cinfo, COL_INFO);
298
299    tvb_memcpy(tvb, (guint8 *)&ehdr, 0, sizeof ehdr);
300    
301    if (tree) {
302      ti = proto_tree_add_item(tree, proto_esis, tvb, 0, tvb_length(tvb), FALSE);
303      esis_tree = proto_item_add_subtree(ti, ett_esis);
304
305      if (ehdr.esis_version != ESIS_REQUIRED_VERSION){
306        esis_dissect_unknown(tvb, pinfo, esis_tree,
307                           "Unknown ESIS version (%u vs %u)",
308                            ehdr.esis_version, ESIS_REQUIRED_VERSION );
309        return;
310      }
311
312      proto_tree_add_uint( esis_tree, hf_esis_nlpi, tvb, 0, 1, ehdr.esis_nlpi );
313      proto_tree_add_uint( esis_tree, hf_esis_length, tvb,
314                           1, 1, ehdr.esis_length );
315      proto_tree_add_uint( esis_tree, hf_esis_version, tvb, 2, 1, 
316                           ehdr.esis_version );
317      proto_tree_add_uint( esis_tree, hf_esis_reserved, tvb, 3, 1, 
318                           ehdr.esis_reserved );
319
320      pdu_type_string = val_to_str(ehdr.esis_type&OSI_PDU_TYPE_MASK,
321                                   esis_vals, "Unknown (0x%x)");
322
323      proto_tree_add_uint_format( esis_tree, hf_esis_type, tvb, 4, 1, 
324                                  ehdr.esis_type, 
325                                  pdu_type_format_string,
326                                  pdu_type_string,
327                                  (ehdr.esis_type&BIT_8) ? "1" : "0",
328                                  (ehdr.esis_type&BIT_7) ? "1" : "0",
329                                  (ehdr.esis_type&BIT_6) ? "1" : "0");
330
331      tmp_uint = pntohs( ehdr.esis_holdtime );
332      proto_tree_add_uint_format(esis_tree, hf_esis_holdtime, tvb, 5, 2, 
333                                 tmp_uint, "Holding Time  : %u seconds",
334                                 tmp_uint );
335
336      tmp_uint = pntohs( ehdr.esis_checksum );
337      
338      switch (calc_checksum( tvb, 0, ehdr.esis_length, tmp_uint )) {
339
340      case NO_CKSUM:
341         cksum_status = "Not Used";
342         break;
343
344      case DATA_MISSING:
345         cksum_status = "Not checkable - not all of packet was captured";
346         break;
347
348      case CKSUM_OK:
349         cksum_status = "Is good";
350         break;
351
352      case CKSUM_NOT_OK:
353         cksum_status = "Is wrong";
354         break;
355
356      default:
357         cksum_status = NULL;
358         g_assert_not_reached();
359      }
360      proto_tree_add_uint_format( esis_tree, hf_esis_checksum, tvb, 7, 2,
361                                  tmp_uint, "Checksum      : 0x%x ( %s )", 
362                                  tmp_uint, cksum_status );
363    }
364
365
366    /*
367     * Let us make sure we use the same names for all our decodes
368     * here.  First, dump the name into info column, and THEN
369     * dispatch the sub-type.
370     */
371    if (check_col(pinfo->cinfo, COL_INFO)) {
372      col_add_str(pinfo->cinfo, COL_INFO, 
373                  val_to_str( ehdr.esis_type&OSI_PDU_TYPE_MASK, esis_vals,
374                              "Unknown (0x%x)" ) );
375    } 
376
377    variable_len = ehdr.esis_length - ESIS_HDR_FIXED_LENGTH;
378
379    switch (ehdr.esis_type & OSI_PDU_TYPE_MASK) {
380      case ESIS_ESH_PDU:
381           esis_dissect_esh_pdu( variable_len, tvb, pinfo, esis_tree);
382      break;
383      case ESIS_ISH_PDU:
384           esis_dissect_ish_pdu( variable_len, tvb, pinfo, esis_tree);
385      break;
386      case ESIS_RD_PDU:
387           esis_dissect_redirect_pdu( variable_len, tvb, pinfo, 
388                                      esis_tree);
389      break;
390      default:
391          esis_dissect_unknown(tvb, pinfo, esis_tree,
392                               "Unknown ESIS packet type 0x%x",
393                               ehdr.esis_type & OSI_PDU_TYPE_MASK );
394    }
395 } /* dissect_esis */
396
397
398 /*
399  * Name: proto_register_esis()
400  *
401  * Description:
402  *      main register for esis protocol set.  We register some display
403  *      formats and the protocol module variables.
404  *
405  *      NOTE: this procedure to autolinked by the makefile process that
406  *      builds register.c
407  *
408  * Input: 
409  *      void
410  *
411  * Output:
412  *      void
413  */
414 void 
415 proto_register_esis(void) {
416   static hf_register_info hf[] = {
417     { &hf_esis_nlpi,
418       { "Network Layer Protocol Identifier", "esis.nlpi",       
419         FT_UINT8, BASE_HEX, VALS(nlpid_vals), 0x0, "", HFILL }},
420     { &hf_esis_length,
421       { "PDU Length    ", "esis.length", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
422     { &hf_esis_version,
423       { "Version (==1) ", "esis.ver",    FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
424     { &hf_esis_reserved,
425       { "Reserved(==0) ", "esis.res",    FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
426     { &hf_esis_type,
427       { "PDU Type      ", "esis.type",   FT_UINT8, BASE_DEC, VALS(esis_vals),
428          0xff, "", HFILL }},
429     { &hf_esis_holdtime,
430       { "Holding Time  ", "esis.htime",  FT_UINT16, BASE_DEC, NULL, 0x0, " s", HFILL }},
431     { &hf_esis_checksum,
432       { "Checksum      ", "esis.chksum", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL }}
433   };
434   /*
435    * 
436    * 
437    */
438   static gint *ett[] = {
439     &ett_esis,
440     &ett_esis_area_addr,
441   };
442
443   proto_esis = proto_register_protocol( PROTO_STRING_ESIS, "ESIS", "esis");
444   proto_register_field_array(proto_esis, hf, array_length(hf));
445   proto_register_subtree_array(ett, array_length(ett));
446 };
447
448 void
449 proto_reg_handoff_esis(void)
450 {
451   dissector_handle_t esis_handle;
452
453   esis_handle = create_dissector_handle(dissect_esis, proto_esis);
454   dissector_add("osinl", NLPID_ISO9542_ESIS, esis_handle);
455 }