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