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