First batch of unincludes. Last attempt...
[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  * Ralf Schneider <Ralf.Schneider@t-online.de>
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27
28 #include <epan/packet.h>
29 #include <epan/expert.h>
30 #include <epan/nlpid.h>
31 #include "packet-osi.h"
32 #include "packet-osi-options.h"
33
34 /* The version we support is 1 */
35 #define ESIS_REQUIRED_VERSION    1
36
37 /* ESIS PDU types */
38 #define ESIS_ESH_PDU    02
39 #define ESIS_ISH_PDU    04
40 #define ESIS_RD_PDU     06
41
42 /* The length of the fixed part */
43 #define ESIS_HDR_FIXED_LENGTH 9
44
45 void proto_register_esis(void);
46 void proto_reg_handoff_esis(void);
47
48 /* esis base header */
49 static int  proto_esis        = -1;
50
51 static int  hf_esis_nlpi      = -1;
52 static int  hf_esis_length    = -1;
53 static int  hf_esis_version   = -1;
54 static int  hf_esis_reserved  = -1;
55 static int  hf_esis_type      = -1;
56 static int  hf_esis_holdtime  = -1;
57 static int  hf_esis_checksum  = -1;
58 /* Generated from convert_proto_tree_add_text.pl */
59 static int hf_esis_dal = -1;
60 static int hf_esis_number_of_source_addresses = -1;
61 static int hf_esis_netl = -1;
62 static int hf_esis_sal = -1;
63 static int hf_esis_bsnpal = -1;
64 static int hf_esis_net = -1;
65 static int hf_esis_da = -1;
66 static int hf_esis_bsnpa = -1;
67
68 static gint ett_esis              = -1;
69 static gint ett_esis_area_addr    = -1;
70
71 static expert_field ei_esis_version = EI_INIT;
72 static expert_field ei_esis_length = EI_INIT;
73 static expert_field ei_esis_type = EI_INIT;
74
75 static const value_string esis_vals[] = {
76   { ESIS_ESH_PDU, "ES HELLO"},
77   { ESIS_ISH_PDU, "IS HELLO"},
78   { ESIS_RD_PDU,  "RD REQUEST"},
79   { 0,             NULL} };
80
81 /* ################## Descriptions ###########################################*/
82 /* Parameters for the ESH PDU
83  * Source Address Parameter:
84  *
85  * Octet:    Length:   Parameter Type:
86  *     10          1   Number of Source Addresses ( NSAPs served by this Network
87  *     11          1   Source Address Length Indicator ( SAL )     #    Entity )
88  * 12-m-1   variable   Source Address ( NSAP )
89  *      m              Options, dissected in osi.c
90  *
91  *
92  * Parameter for the ISH PDU:
93  * Network Entity Title Parameter:
94  *
95  * Octet:    Length:   Parameter Type:
96  *     10          1   Network Entity Title Length Indicator ( NETL )
97  * 11-m-1   variable   Network Entity Title ( NET )
98  *      m              Options, dissected in osi.c
99  *
100  *
101  * Parameter for the RD PDU:
102  * When re-directed to an IS:
103  *
104  *  Octet:   Length:   Parameter Type:
105  *      10         1   Destination Address Length Indicator ( DAL )
106  *  11>m-1  variable   Destination Address ( DA )
107  *       m         1   Subnetwork Address Length Indicator ( BSNPAL )
108  * m+1>n-1  variable   Subnetwork Address ( BSNPA )
109  *       n         1   Network Entity Title Length Indicator ( NETL )
110  * n+1>p-1  variable   Network Entity Title ( NET )
111  *       p             Options, dissected in osi.c
112  *
113  *
114  * Parameter for the RD PDU:
115  * When re-directed to an ES:
116  *
117  *  Octet:   Length:   Parameter Type:
118  *      10         1   Destination Address Length Indicator ( DAL )
119  *  11>m-1  variable   Destination Address ( DA )
120  *       m         1   Subnetwork Address Length Indicator ( BSNPAL )
121  * m+1>n-1  variable   Subnetwork Address ( BSNPA )
122  *       n         1   Network Entity Title Length Indicator ( NETL ) == 0
123  *     n+1             Options, dissected in osi.c
124  *
125  */
126
127 /* ############################ Tool Functions ############################## */
128
129
130 static void
131 esis_dissect_esh_pdu( guint8 len, tvbuff_t *tvb, proto_tree *tree) {
132   proto_tree *esis_area_tree;
133   int         offset  = 0;
134   int         no_sa   = 0;
135   int         sal     = 0;
136
137   proto_item  *ti;
138
139   if (tree) {
140     offset += ESIS_HDR_FIXED_LENGTH;
141
142     no_sa  = tvb_get_guint8(tvb, offset);
143     len   -= 1;
144
145     ti = proto_tree_add_uint( tree, hf_esis_number_of_source_addresses, tvb, offset, 1, no_sa);
146     offset++;
147
148     esis_area_tree = proto_item_add_subtree( ti, ett_esis_area_addr );
149     while ( no_sa-- > 0 ) {
150       sal = (int) tvb_get_guint8(tvb, offset);
151       proto_tree_add_uint_format_value(esis_area_tree, hf_esis_sal, tvb, offset, 1, sal, "%2u Octets", sal);
152       offset++;
153       proto_tree_add_text(esis_area_tree, tvb, offset, sal,
154                           " SA: %s",
155                           print_nsap_net( tvb_get_ptr(tvb, offset, sal), sal ) );
156       offset += sal;
157       len    -= ( sal + 1 );
158     }
159     dissect_osi_options( len, tvb, offset, tree );
160   }
161 } /* esis_dissect_esh_pdu */
162
163 static void
164 esis_dissect_ish_pdu( guint8 len, tvbuff_t *tvb, proto_tree *tree) {
165
166   int   offset  = 0;
167   int   netl    = 0;
168
169   if (tree) {
170     offset += ESIS_HDR_FIXED_LENGTH;
171
172     netl = (int) tvb_get_guint8(tvb, offset);
173     proto_tree_add_text( tree, tvb, offset, netl + 1,
174                          "### Network Entity Title Section ###");
175     proto_tree_add_uint_format_value(tree, hf_esis_netl, tvb, offset++, 1, netl, "%2u Octets", netl);
176     proto_tree_add_string( tree, hf_esis_net, tvb, offset, netl, print_nsap_net( tvb_get_ptr(tvb, offset, netl), netl ) );
177     offset += netl;
178     len    -= ( netl + 1 );
179
180     dissect_osi_options( len, tvb, offset, tree );
181   }
182 }
183
184 static void
185 esis_dissect_redirect_pdu( guint8 len, tvbuff_t *tvb, proto_tree *tree) {
186
187   int   offset  = 0;
188   int   tmpl    = 0;
189
190   if (tree) {
191     offset += ESIS_HDR_FIXED_LENGTH;
192
193     tmpl = (int) tvb_get_guint8(tvb, offset);
194     proto_tree_add_text( tree, tvb, offset, tmpl + 1,
195                          "### Destination Address Section ###" );
196     proto_tree_add_uint_format_value(tree, hf_esis_dal, tvb, offset++, 1, tmpl, "%2u Octets", tmpl);
197     proto_tree_add_string( tree, hf_esis_da, tvb, offset, tmpl,
198                          print_nsap_net( tvb_get_ptr(tvb, offset, tmpl), tmpl ) );
199     offset += tmpl;
200     len    -= ( tmpl + 1 );
201     tmpl    = (int) tvb_get_guint8(tvb, offset);
202
203     proto_tree_add_text( tree, tvb, offset, tmpl + 1,
204                          "###  Subnetwork Address Section ###");
205     proto_tree_add_uint_format_value(tree, hf_esis_bsnpal, tvb, offset++, 1, tmpl, "%2u Octets", tmpl);
206     proto_tree_add_item( tree, hf_esis_bsnpa, tvb, offset, tmpl, ENC_NA);
207     offset += tmpl;
208     len    -= ( tmpl + 1 );
209     tmpl    = (int) tvb_get_guint8(tvb, offset);
210
211     if ( 0 == tmpl ) {
212       proto_tree_add_text( tree, tvb, offset, 1,
213                            "### No Network Entity Title Section ###" );
214       offset++;
215       len--;
216     }
217     else {
218       proto_tree_add_text( tree, tvb, offset, 1,
219                            "### Network Entity Title Section ###" );
220       proto_tree_add_uint_format_value(tree, hf_esis_netl, tvb, offset++, 1, tmpl, "%2u Octets", tmpl );
221       proto_tree_add_string( tree, hf_esis_net, tvb, offset, tmpl,
222                            print_nsap_net( tvb_get_ptr(tvb, offset, tmpl), tmpl ) );
223       offset += tmpl;
224       len    -= ( tmpl + 1 );
225     }
226     dissect_osi_options( len, tvb, offset, tree );
227   }
228 }
229
230
231 /*
232  * Name: dissect_esis()
233  *
234  * Description:
235  *   Main entry area for esis de-mangling.  This will build the
236  *   main esis tree data and call the sub-protocols as needed.
237  *
238  * Input:
239  *   tvbuff *      : tvbuff referring to packet data
240  *   packet_info * : info for current packet
241  *   proto_tree *  : tree of display data.  May be NULL.
242  *
243  * Output:
244  *   void, but we will add to the proto_tree if it is not NULL.
245  */
246 static void
247 dissect_esis(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
248   guint8 version, length;
249   proto_item *ti, *type_item;
250   proto_tree *esis_tree    = NULL;
251   guint8      variable_len, type;
252   guint16     holdtime, checksum;
253   const char *cksum_status;
254
255   col_set_str(pinfo->cinfo, COL_PROTOCOL, "ESIS");
256   col_clear(pinfo->cinfo, COL_INFO);
257
258     ti = proto_tree_add_item(tree, proto_esis, tvb, 0, -1, ENC_NA);
259     esis_tree = proto_item_add_subtree(ti, ett_esis);
260
261     proto_tree_add_item( esis_tree, hf_esis_nlpi, tvb, 0, 1, ENC_BIG_ENDIAN);
262     ti = proto_tree_add_item( esis_tree, hf_esis_length, tvb, 1, 1, ENC_BIG_ENDIAN );
263     length = tvb_get_guint8(tvb, 1);
264     if (length < ESIS_HDR_FIXED_LENGTH) {
265       expert_add_info_format(pinfo, ti, &ei_esis_length,
266                            "Bogus ESIS length (%u, must be >= %u)",
267                            length, ESIS_HDR_FIXED_LENGTH );
268       return;
269     }
270
271     version = tvb_get_guint8(tvb, 2);
272     ti = proto_tree_add_item( esis_tree, hf_esis_version, tvb, 2, 1, ENC_BIG_ENDIAN);
273     if (version != ESIS_REQUIRED_VERSION){
274       expert_add_info_format(pinfo, ti, &ei_esis_version,
275                            "Unknown ESIS version (%u vs %u)",
276                            version, ESIS_REQUIRED_VERSION );
277     }
278
279     proto_tree_add_item( esis_tree, hf_esis_reserved, tvb, 3, 1, ENC_BIG_ENDIAN);
280
281     type_item = proto_tree_add_item( esis_tree, hf_esis_type, tvb, 4, 1, ENC_BIG_ENDIAN);
282     type = tvb_get_guint8(tvb, 4) & OSI_PDU_TYPE_MASK;
283
284     holdtime = tvb_get_ntohs(tvb, 5);
285     proto_tree_add_uint_format_value(esis_tree, hf_esis_holdtime, tvb, 5, 2,
286                                holdtime, "%u seconds", holdtime);
287
288     checksum = tvb_get_ntohs(tvb, 7);
289     switch (calc_checksum( tvb, 0, length, checksum)) {
290
291     case NO_CKSUM:
292       cksum_status = "Not Used";
293       break;
294
295     case DATA_MISSING:
296       cksum_status = "Not checkable - not all of packet was captured";
297       break;
298
299     case CKSUM_OK:
300       cksum_status = "Is good";
301       break;
302
303     case CKSUM_NOT_OK:
304       cksum_status = "Is wrong";
305       break;
306
307     default:
308       cksum_status = NULL;
309       DISSECTOR_ASSERT_NOT_REACHED();
310     }
311     proto_tree_add_uint_format_value( esis_tree, hf_esis_checksum, tvb, 7, 2,
312                                 checksum, "0x%x ( %s )", checksum, cksum_status );
313
314
315   /*
316    * Let us make sure we use the same names for all our decodes
317    * here.  First, dump the name into info column, and THEN
318    * dispatch the sub-type.
319    */
320   col_add_str(pinfo->cinfo, COL_INFO,
321                 val_to_str( type, esis_vals,
322                             "Unknown (0x%x)" ) );
323
324   variable_len = length - ESIS_HDR_FIXED_LENGTH;
325
326   switch (type) {
327   case ESIS_ESH_PDU:
328     esis_dissect_esh_pdu( variable_len, tvb, esis_tree);
329     break;
330   case ESIS_ISH_PDU:
331     esis_dissect_ish_pdu( variable_len, tvb, esis_tree);
332     break;
333   case ESIS_RD_PDU:
334     esis_dissect_redirect_pdu( variable_len, tvb, esis_tree);
335     break;
336   default:
337     expert_add_info(pinfo, type_item, &ei_esis_type);
338   }
339 } /* dissect_esis */
340
341
342 /*
343  * Name: proto_register_esis()
344  *
345  * Description:
346  *      main register for esis protocol set.  We register some display
347  *      formats and the protocol module variables.
348  *
349  *      NOTE: this procedure to autolinked by the makefile process that
350  *      builds register.c
351  *
352  * Input:
353  *      void
354  *
355  * Output:
356  *      void
357  */
358 void
359 proto_register_esis(void) {
360   static hf_register_info hf[] = {
361     { &hf_esis_nlpi,
362       { "Network Layer Protocol Identifier", "esis.nlpi",
363         FT_UINT8, BASE_HEX, VALS(nlpid_vals), 0x0, NULL, HFILL }},
364
365     { &hf_esis_length,
366       { "PDU Length", "esis.length",  FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
367
368     { &hf_esis_version,
369       { "Version", "esis.ver",  FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
370
371     { &hf_esis_reserved,
372       { "Reserved(==0)", "esis.res",  FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
373
374     { &hf_esis_type,
375       { "PDU Type", "esis.type",      FT_UINT8, BASE_DEC, VALS(esis_vals), OSI_PDU_TYPE_MASK, NULL, HFILL }},
376
377     { &hf_esis_holdtime,
378       { "Holding Time", "esis.htime", FT_UINT16, BASE_DEC, NULL, 0x0, "s", HFILL }},
379
380     { &hf_esis_checksum,
381       { "Checksum", "esis.chksum",    FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
382
383       /* Generated from convert_proto_tree_add_text.pl */
384       { &hf_esis_number_of_source_addresses, { "Number of Source Addresses (SA, Format: NSAP)", "esis.number_of_source_addresses", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
385       { &hf_esis_sal, { "SAL", "esis.sal", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
386       { &hf_esis_netl, { "NETL", "esis.netl", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
387       { &hf_esis_dal, { "DAL", "esis.dal", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
388       { &hf_esis_bsnpal, { "BSNPAL", "esis.bsnpal", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
389       { &hf_esis_net, { "NET", "esis.net", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
390       { &hf_esis_da, { "DA", "esis.da", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
391       { &hf_esis_bsnpa, { "BSNPA", "esis.bsnpa", FT_SYSTEM_ID, BASE_NONE, NULL, 0x0, NULL, HFILL }},
392   };
393
394   static gint *ett[] = {
395     &ett_esis,
396     &ett_esis_area_addr,
397   };
398
399   static ei_register_info ei[] = {
400     { &ei_esis_version, { "esis.ver.unknown", PI_PROTOCOL, PI_WARN, "Unknown ESIS version", EXPFILL }},
401     { &ei_esis_length, { "esis.length.invalid", PI_MALFORMED, PI_ERROR, "Bogus ESIS length", EXPFILL }},
402     { &ei_esis_type, { "esis.type.unknown", PI_PROTOCOL, PI_WARN, "Unknown ESIS packet type", EXPFILL }},
403   };
404
405   expert_module_t* expert_esis;
406
407   proto_esis = proto_register_protocol( PROTO_STRING_ESIS, "ESIS", "esis");
408   proto_register_field_array(proto_esis, hf, array_length(hf));
409   proto_register_subtree_array(ett, array_length(ett));
410   expert_esis = expert_register_protocol(proto_esis);
411   expert_register_field_array(expert_esis, ei, array_length(ei));
412   register_dissector("esis", dissect_esis, proto_esis);
413 }
414
415 void
416 proto_reg_handoff_esis(void)
417 {
418   dissector_handle_t esis_handle;
419
420   esis_handle = find_dissector("esis");
421   dissector_add_uint("osinl.incl", NLPID_ISO9542_ESIS, esis_handle);
422 }
423
424 /*
425  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
426  *
427  * Local Variables:
428  * c-basic-offset: 2
429  * tab-width: 8
430  * indent-tabs-mode: nil
431  * End:
432  *
433  * ex: set shiftwidth=2 tabstop=8 expandtab:
434  * :indentSize=2:tabSize=8:noTabs=true:
435  */