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