Mark requests as such in the Info column.
[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.24 2002/04/07 22:00: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                            proto_tree *treepd);
69 static void esis_dissect_ish_pdu( u_char len, tvbuff_t *tvb,
70                            proto_tree *tree);
71 static void esis_dissect_redirect_pdu( u_char len, tvbuff_t *tvb,
72                            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  *   tvbuff_t *      : tvbuff with packet data.
138  *   proto_tree *    : tree of display data.  May be NULL.
139  *   char *          : format text
140  *   subsequent args : arguments to format
141  *
142  * Output:
143  *   void (may modify proto tree)
144  */
145 static void
146 esis_dissect_unknown( tvbuff_t *tvb, proto_tree *tree, char *fmat, ...){
147   va_list ap;
148
149   va_start(ap, fmat);
150   proto_tree_add_text_valist(tree, tvb, 0, -1, fmat, ap);
151   va_end(ap);
152 }
153
154
155 static void
156 esis_dissect_esh_pdu( u_char len, tvbuff_t *tvb, proto_tree *tree) {
157   proto_tree *esis_area_tree;
158   int         offset  = 0;
159   int         no_sa   = 0;
160   int         sal     = 0;
161   
162   proto_item  *ti;
163   
164   if (tree) {
165     offset += ESIS_HDR_FIXED_LENGTH;
166
167     no_sa  = tvb_get_guint8(tvb, offset);
168     len   -= 1;
169
170     ti = proto_tree_add_text( tree, tvb, offset, -1,
171             "Number of Source Addresses (SA, Format: NSAP) : %u", no_sa );
172     offset++;
173     
174     esis_area_tree = proto_item_add_subtree( ti, ett_esis_area_addr );
175     while ( no_sa-- > 0 ) {
176        sal = (int) tvb_get_guint8(tvb, offset);
177        offset++;
178        proto_tree_add_text(esis_area_tree, tvb, offset, 1, "SAL: %2u Octets", sal);
179        proto_tree_add_text(esis_area_tree, tvb, offset + 1, sal,
180                            " SA: %s",
181                            print_nsap_net( tvb_get_ptr(tvb, offset, sal), sal ) );
182        offset += sal;
183        len    -= ( sal + 1 );
184     }
185     dissect_osi_options( PDU_TYPE_ESIS_ESH, len, tvb, offset, tree );
186   }  
187 } /* esis_dissect_esh_pdu */ ;
188
189 static void
190 esis_dissect_ish_pdu( u_char len, tvbuff_t *tvb, proto_tree *tree) {
191   
192   int   offset  = 0;
193   int   netl    = 0;
194
195   if (tree) {
196     offset += ESIS_HDR_FIXED_LENGTH;
197
198     netl = (int) tvb_get_guint8(tvb, offset);
199     proto_tree_add_text( tree, tvb, offset, netl + 1, 
200                          "### Network Entity Title Section ###");
201     proto_tree_add_text( tree, tvb, offset++, 1, "NETL: %2u Octets", netl);
202     proto_tree_add_text( tree, tvb, offset, netl,
203                            " NET: %s",
204                            print_nsap_net( tvb_get_ptr(tvb, offset, netl), netl ) );
205     offset += netl;
206     len    -= ( netl + 1 );
207
208     dissect_osi_options( PDU_TYPE_ESIS_ISH, len, tvb, offset, tree );
209   }
210 };
211
212 static void
213 esis_dissect_redirect_pdu( u_char len, tvbuff_t *tvb, proto_tree *tree) {
214
215   int   offset  = 0;
216   int   tmpl    = 0;
217
218   if (tree) {
219     offset += ESIS_HDR_FIXED_LENGTH;
220
221     tmpl = (int) tvb_get_guint8(tvb, offset);
222     proto_tree_add_text( tree, tvb, offset, tmpl + 1, 
223                          "### Destination Address Section ###" );
224     proto_tree_add_text( tree, tvb, offset++, 1, "DAL: %2u Octets", tmpl);
225     proto_tree_add_text( tree, tvb, offset, tmpl,
226                          " DA : %s",
227                          print_nsap_net( tvb_get_ptr(tvb, offset, tmpl), tmpl ) );
228     offset += tmpl;
229     len    -= ( tmpl + 1 );
230     tmpl    = (int) tvb_get_guint8(tvb, offset);
231
232     proto_tree_add_text( tree, tvb, offset, tmpl + 1, 
233                          "###  Subnetwork Address Section ###");
234     proto_tree_add_text( tree, tvb, offset++, 1, "BSNPAL: %2u Octets", tmpl);
235     proto_tree_add_text( tree, tvb, offset, tmpl,
236                            " BSNPA: %s",
237                            print_system_id( tvb_get_ptr(tvb, offset, tmpl), tmpl ) );
238     offset += tmpl;
239     len    -= ( tmpl + 1 );
240     tmpl    = (int) tvb_get_guint8(tvb, offset);
241
242     if ( 0 == tmpl ) {
243       proto_tree_add_text( tree, tvb, offset, 1, 
244                            "### No Network Entity Title Section ###" );
245       offset++;
246       len--;
247     }
248     else {
249       proto_tree_add_text( tree, tvb, offset, 1,
250                            "### Network Entity Title Section ###" );
251       proto_tree_add_text( tree, tvb, offset++, 1, "NETL: %2u Octets", tmpl );
252       proto_tree_add_text( tree, tvb, offset, tmpl,
253                            " NET: %s",
254                            print_nsap_net( tvb_get_ptr(tvb, offset, tmpl), tmpl ) );
255       offset += tmpl;
256       len    -= ( tmpl + 1 );
257     }
258     dissect_osi_options( PDU_TYPE_ESIS_RD, len, tvb, offset, tree );
259   }
260 }
261
262
263 /*
264  * Name: dissect_esis()
265  * 
266  * Description:
267  *   Main entry area for esis de-mangling.  This will build the
268  *   main esis tree data and call the sub-protocols as needed.
269  *
270  * Input:
271  *   tvbuff *      : tvbuff referring to packet data
272  *   packet_info * : info for current packet
273  *   proto_tree *  : tree of display data.  May be NULL.
274  *
275  * Output:
276  *   void, but we will add to the proto_tree if it is not NULL.
277  */
278 static void
279 dissect_esis(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
280    char       *pdu_type_string        = NULL;
281    char       *pdu_type_format_string = "PDU Type      : %s (R:%s%s%s)";   
282    esis_hdr_t  ehdr;
283    proto_item *ti;
284    proto_tree *esis_tree    = NULL;
285    int         variable_len = 0;
286    u_int       tmp_uint     = 0;
287    char       *cksum_status;
288
289    if (check_col(pinfo->cinfo, COL_PROTOCOL))
290      col_set_str(pinfo->cinfo, COL_PROTOCOL, "ESIS");
291    if (check_col(pinfo->cinfo, COL_INFO))
292      col_clear(pinfo->cinfo, COL_INFO);
293
294    tvb_memcpy(tvb, (guint8 *)&ehdr, 0, sizeof ehdr);
295    
296    if (tree) {
297      ti = proto_tree_add_item(tree, proto_esis, tvb, 0, -1, FALSE);
298      esis_tree = proto_item_add_subtree(ti, ett_esis);
299
300      if (ehdr.esis_version != ESIS_REQUIRED_VERSION){
301        esis_dissect_unknown(tvb, esis_tree,
302                           "Unknown ESIS version (%u vs %u)",
303                            ehdr.esis_version, ESIS_REQUIRED_VERSION );
304        return;
305      }
306
307      proto_tree_add_uint( esis_tree, hf_esis_nlpi, tvb, 0, 1, ehdr.esis_nlpi );
308      proto_tree_add_uint( esis_tree, hf_esis_length, tvb,
309                           1, 1, ehdr.esis_length );
310      proto_tree_add_uint( esis_tree, hf_esis_version, tvb, 2, 1, 
311                           ehdr.esis_version );
312      proto_tree_add_uint( esis_tree, hf_esis_reserved, tvb, 3, 1, 
313                           ehdr.esis_reserved );
314
315      pdu_type_string = val_to_str(ehdr.esis_type&OSI_PDU_TYPE_MASK,
316                                   esis_vals, "Unknown (0x%x)");
317
318      proto_tree_add_uint_format( esis_tree, hf_esis_type, tvb, 4, 1, 
319                                  ehdr.esis_type, 
320                                  pdu_type_format_string,
321                                  pdu_type_string,
322                                  (ehdr.esis_type&BIT_8) ? "1" : "0",
323                                  (ehdr.esis_type&BIT_7) ? "1" : "0",
324                                  (ehdr.esis_type&BIT_6) ? "1" : "0");
325
326      tmp_uint = pntohs( ehdr.esis_holdtime );
327      proto_tree_add_uint_format(esis_tree, hf_esis_holdtime, tvb, 5, 2, 
328                                 tmp_uint, "Holding Time  : %u seconds",
329                                 tmp_uint );
330
331      tmp_uint = pntohs( ehdr.esis_checksum );
332      
333      switch (calc_checksum( tvb, 0, ehdr.esis_length, tmp_uint )) {
334
335      case NO_CKSUM:
336         cksum_status = "Not Used";
337         break;
338
339      case DATA_MISSING:
340         cksum_status = "Not checkable - not all of packet was captured";
341         break;
342
343      case CKSUM_OK:
344         cksum_status = "Is good";
345         break;
346
347      case CKSUM_NOT_OK:
348         cksum_status = "Is wrong";
349         break;
350
351      default:
352         cksum_status = NULL;
353         g_assert_not_reached();
354      }
355      proto_tree_add_uint_format( esis_tree, hf_esis_checksum, tvb, 7, 2,
356                                  tmp_uint, "Checksum      : 0x%x ( %s )", 
357                                  tmp_uint, cksum_status );
358    }
359
360
361    /*
362     * Let us make sure we use the same names for all our decodes
363     * here.  First, dump the name into info column, and THEN
364     * dispatch the sub-type.
365     */
366    if (check_col(pinfo->cinfo, COL_INFO)) {
367      col_add_str(pinfo->cinfo, COL_INFO, 
368                  val_to_str( ehdr.esis_type&OSI_PDU_TYPE_MASK, esis_vals,
369                              "Unknown (0x%x)" ) );
370    } 
371
372    variable_len = ehdr.esis_length - ESIS_HDR_FIXED_LENGTH;
373
374    switch (ehdr.esis_type & OSI_PDU_TYPE_MASK) {
375      case ESIS_ESH_PDU:
376           esis_dissect_esh_pdu( variable_len, tvb, esis_tree);
377      break;
378      case ESIS_ISH_PDU:
379           esis_dissect_ish_pdu( variable_len, tvb, esis_tree);
380      break;
381      case ESIS_RD_PDU:
382           esis_dissect_redirect_pdu( variable_len, tvb, esis_tree);
383      break;
384      default:
385          esis_dissect_unknown(tvb, esis_tree,
386                               "Unknown ESIS packet type 0x%x",
387                               ehdr.esis_type & OSI_PDU_TYPE_MASK );
388    }
389 } /* dissect_esis */
390
391
392 /*
393  * Name: proto_register_esis()
394  *
395  * Description:
396  *      main register for esis protocol set.  We register some display
397  *      formats and the protocol module variables.
398  *
399  *      NOTE: this procedure to autolinked by the makefile process that
400  *      builds register.c
401  *
402  * Input: 
403  *      void
404  *
405  * Output:
406  *      void
407  */
408 void 
409 proto_register_esis(void) {
410   static hf_register_info hf[] = {
411     { &hf_esis_nlpi,
412       { "Network Layer Protocol Identifier", "esis.nlpi",       
413         FT_UINT8, BASE_HEX, VALS(nlpid_vals), 0x0, "", HFILL }},
414     { &hf_esis_length,
415       { "PDU Length    ", "esis.length", FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
416     { &hf_esis_version,
417       { "Version (==1) ", "esis.ver",    FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
418     { &hf_esis_reserved,
419       { "Reserved(==0) ", "esis.res",    FT_UINT8, BASE_DEC, NULL, 0x0, "", HFILL }},
420     { &hf_esis_type,
421       { "PDU Type      ", "esis.type",   FT_UINT8, BASE_DEC, VALS(esis_vals),
422          0xff, "", HFILL }},
423     { &hf_esis_holdtime,
424       { "Holding Time  ", "esis.htime",  FT_UINT16, BASE_DEC, NULL, 0x0, " s", HFILL }},
425     { &hf_esis_checksum,
426       { "Checksum      ", "esis.chksum", FT_UINT16, BASE_HEX, NULL, 0x0, "", HFILL }}
427   };
428   /*
429    * 
430    * 
431    */
432   static gint *ett[] = {
433     &ett_esis,
434     &ett_esis_area_addr,
435   };
436
437   proto_esis = proto_register_protocol( PROTO_STRING_ESIS, "ESIS", "esis");
438   proto_register_field_array(proto_esis, hf, array_length(hf));
439   proto_register_subtree_array(ett, array_length(ett));
440 };
441
442 void
443 proto_reg_handoff_esis(void)
444 {
445   dissector_handle_t esis_handle;
446
447   esis_handle = create_dissector_handle(dissect_esis, proto_esis);
448   dissector_add("osinl", NLPID_ISO9542_ESIS, esis_handle);
449 }