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