6403822d4ab6493107b7cf6a032a431e5ddcc6cb
[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 static int  hf_esis_checksum_status  = -1;
59 /* Generated from convert_proto_tree_add_text.pl */
60 static int hf_esis_dal = -1;
61 static int hf_esis_number_of_source_addresses = -1;
62 static int hf_esis_netl = -1;
63 static int hf_esis_sal = -1;
64 static int hf_esis_sa = -1;
65 static int hf_esis_bsnpal = -1;
66 static int hf_esis_net = -1;
67 static int hf_esis_da = -1;
68 static int hf_esis_bsnpa = -1;
69
70 static gint ett_esis              = -1;
71 static gint ett_esis_area_addr    = -1;
72 static gint ett_esis_network      = -1;
73 static gint ett_esis_dest_addr    = -1;
74 static gint ett_esis_subnetwork   = -1;
75
76
77 static expert_field ei_esis_version = EI_INIT;
78 static expert_field ei_esis_length = EI_INIT;
79 static expert_field ei_esis_type = EI_INIT;
80 static expert_field ei_esis_checksum = EI_INIT;
81
82 static const value_string esis_vals[] = {
83   { ESIS_ESH_PDU, "ES HELLO"},
84   { ESIS_ISH_PDU, "IS HELLO"},
85   { ESIS_RD_PDU,  "RD REQUEST"},
86   { 0,             NULL} };
87
88 /* ################## Descriptions ###########################################*/
89 /* Parameters for the ESH PDU
90  * Source Address Parameter:
91  *
92  * Octet:    Length:   Parameter Type:
93  *     10          1   Number of Source Addresses ( NSAPs served by this Network
94  *     11          1   Source Address Length Indicator ( SAL )     #    Entity )
95  * 12-m-1   variable   Source Address ( NSAP )
96  *      m              Options, dissected in osi.c
97  *
98  *
99  * Parameter for the ISH PDU:
100  * Network Entity Title Parameter:
101  *
102  * Octet:    Length:   Parameter Type:
103  *     10          1   Network Entity Title Length Indicator ( NETL )
104  * 11-m-1   variable   Network Entity Title ( NET )
105  *      m              Options, dissected in osi.c
106  *
107  *
108  * Parameter for the RD PDU:
109  * When re-directed to an IS:
110  *
111  *  Octet:   Length:   Parameter Type:
112  *      10         1   Destination Address Length Indicator ( DAL )
113  *  11>m-1  variable   Destination Address ( DA )
114  *       m         1   Subnetwork Address Length Indicator ( BSNPAL )
115  * m+1>n-1  variable   Subnetwork Address ( BSNPA )
116  *       n         1   Network Entity Title Length Indicator ( NETL )
117  * n+1>p-1  variable   Network Entity Title ( NET )
118  *       p             Options, dissected in osi.c
119  *
120  *
121  * Parameter for the RD PDU:
122  * When re-directed to an ES:
123  *
124  *  Octet:   Length:   Parameter Type:
125  *      10         1   Destination Address Length Indicator ( DAL )
126  *  11>m-1  variable   Destination Address ( DA )
127  *       m         1   Subnetwork Address Length Indicator ( BSNPAL )
128  * m+1>n-1  variable   Subnetwork Address ( BSNPA )
129  *       n         1   Network Entity Title Length Indicator ( NETL ) == 0
130  *     n+1             Options, dissected in osi.c
131  *
132  */
133
134 /* ############################ Tool Functions ############################## */
135
136
137 static void
138 esis_dissect_esh_pdu( guint8 len, tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo) {
139     proto_tree *esis_area_tree;
140     int         offset  = 0;
141     int         no_sa   = 0;
142     int         sal     = 0;
143
144     proto_item  *ti;
145
146     offset += ESIS_HDR_FIXED_LENGTH;
147
148     no_sa  = tvb_get_guint8(tvb, offset);
149     len   -= 1;
150
151     ti = proto_tree_add_uint( tree, hf_esis_number_of_source_addresses, tvb, offset, 1, no_sa);
152     offset++;
153
154     esis_area_tree = proto_item_add_subtree( ti, ett_esis_area_addr );
155     while ( no_sa-- > 0 ) {
156       sal = (int) tvb_get_guint8(tvb, offset);
157       proto_tree_add_uint_format_value(esis_area_tree, hf_esis_sal, tvb, offset, 1, sal, "%2u Octets", sal);
158       offset++;
159       proto_tree_add_string(esis_area_tree, hf_esis_sa, tvb, offset, sal, print_nsap_net(tvb, offset, sal ) );
160       offset += sal;
161       len    -= ( sal + 1 );
162     }
163     dissect_osi_options( len, tvb, offset, tree, pinfo );
164
165 } /* esis_dissect_esh_pdu */
166
167 static void
168 esis_dissect_ish_pdu( guint8 len, tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo) {
169
170     int   offset  = 0;
171     int   netl    = 0;
172     proto_tree* network_tree;
173
174     offset += ESIS_HDR_FIXED_LENGTH;
175
176     netl = (int) tvb_get_guint8(tvb, offset);
177     network_tree = proto_tree_add_subtree( tree, tvb, offset, netl + 1, ett_esis_network, NULL,
178                          "### Network Entity Title Section ###");
179     proto_tree_add_uint_format_value(network_tree, hf_esis_netl, tvb, offset++, 1, netl, "%2u Octets", netl);
180     proto_tree_add_string(network_tree, hf_esis_net, tvb, offset, netl, print_nsap_net( tvb, offset, netl ) );
181     offset += netl;
182     len    -= ( netl + 1 );
183
184     dissect_osi_options( len, tvb, offset, network_tree, pinfo );
185 }
186
187 static void
188 esis_dissect_redirect_pdu( guint8 len, tvbuff_t *tvb, proto_tree *tree, packet_info *pinfo) {
189
190     int   offset  = 0;
191     int   tmpl    = 0;
192     proto_tree *dest_tree, *subnet_tree, *network_tree;
193
194     offset += ESIS_HDR_FIXED_LENGTH;
195
196     tmpl = (int) tvb_get_guint8(tvb, offset);
197     dest_tree = proto_tree_add_subtree( tree, tvb, offset, tmpl + 1, ett_esis_dest_addr, NULL,
198                          "### Destination Address Section ###" );
199     proto_tree_add_uint_format_value(dest_tree, hf_esis_dal, tvb, offset++, 1, tmpl, "%2u Octets", tmpl);
200     proto_tree_add_string( dest_tree, hf_esis_da, tvb, offset, tmpl,
201                          print_nsap_net( tvb, offset, tmpl ) );
202     offset += tmpl;
203     len    -= ( tmpl + 1 );
204     tmpl    = (int) tvb_get_guint8(tvb, offset);
205
206     subnet_tree = proto_tree_add_subtree( tree, tvb, offset, tmpl + 1, ett_esis_subnetwork, NULL,
207                          "###  Subnetwork Address Section ###");
208     proto_tree_add_uint_format_value(subnet_tree, hf_esis_bsnpal, tvb, offset++, 1, tmpl, "%2u Octets", tmpl);
209     proto_tree_add_item(subnet_tree, hf_esis_bsnpa, tvb, offset, tmpl, ENC_NA);
210     offset += tmpl;
211     len    -= ( tmpl + 1 );
212     tmpl    = (int) tvb_get_guint8(tvb, offset);
213
214     if ( 0 == tmpl ) {
215       network_tree = proto_tree_add_subtree( tree, tvb, offset, 1, ett_esis_network, NULL,
216                            "### No Network Entity Title Section ###" );
217       offset++;
218       len--;
219     }
220     else {
221       network_tree = proto_tree_add_subtree( tree, tvb, offset, 1, ett_esis_network, NULL,
222                            "### Network Entity Title Section ###" );
223       proto_tree_add_uint_format_value(network_tree, hf_esis_netl, tvb, offset++, 1, tmpl, "%2u Octets", tmpl );
224       proto_tree_add_string( network_tree, hf_esis_net, tvb, offset, tmpl,
225                            print_nsap_net( tvb, offset, tmpl ) );
226       offset += tmpl;
227       len    -= ( tmpl + 1 );
228     }
229     dissect_osi_options( len, tvb, offset, network_tree, pinfo );
230 }
231
232
233 /*
234  * Name: dissect_esis()
235  *
236  * Description:
237  *   Main entry area for esis de-mangling.  This will build the
238  *   main esis tree data and call the sub-protocols as needed.
239  *
240  * Input:
241  *   tvbuff *      : tvbuff referring to packet data
242  *   packet_info * : info for current packet
243  *   proto_tree *  : tree of display data.  May be NULL.
244  *
245  * Output:
246  *   void, but we will add to the proto_tree if it is not NULL.
247  */
248 static int
249 dissect_esis(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_) {
250   guint8 version, length;
251   proto_item *ti, *type_item;
252   proto_tree *esis_tree    = NULL;
253   guint8      variable_len, type;
254   guint16     holdtime, checksum;
255
256   col_set_str(pinfo->cinfo, COL_PROTOCOL, "ESIS");
257   col_clear(pinfo->cinfo, COL_INFO);
258
259     ti = proto_tree_add_item(tree, proto_esis, tvb, 0, -1, ENC_NA);
260     esis_tree = proto_item_add_subtree(ti, ett_esis);
261
262     proto_tree_add_item( esis_tree, hf_esis_nlpi, tvb, 0, 1, ENC_BIG_ENDIAN);
263     ti = proto_tree_add_item( esis_tree, hf_esis_length, tvb, 1, 1, ENC_BIG_ENDIAN );
264     length = tvb_get_guint8(tvb, 1);
265     if (length < ESIS_HDR_FIXED_LENGTH) {
266       expert_add_info_format(pinfo, ti, &ei_esis_length,
267                            "Bogus ESIS length (%u, must be >= %u)",
268                            length, ESIS_HDR_FIXED_LENGTH );
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     if (checksum == 0) {
290         /* No checksum present */
291         proto_tree_add_checksum(esis_tree, tvb, 7, hf_esis_checksum, -1, NULL, pinfo, 0, ENC_BIG_ENDIAN, PROTO_CHECKSUM_NOT_PRESENT);
292     } else {
293         guint32 c0 = 0, c1 = 0;
294
295         if (osi_calc_checksum(tvb, 0, length, &c0, &c1)) {
296             /* Successfully processed checksum, verify it */
297             proto_tree_add_checksum(esis_tree, tvb, 7, hf_esis_checksum, hf_esis_checksum_status, &ei_esis_checksum, pinfo, c0 | c1, ENC_BIG_ENDIAN, PROTO_CHECKSUM_VERIFY|PROTO_CHECKSUM_ZERO);
298         } else {
299             proto_tree_add_checksum(esis_tree, tvb, 7, hf_esis_checksum, hf_esis_checksum_status, &ei_esis_checksum, pinfo, 0, ENC_BIG_ENDIAN, PROTO_CHECKSUM_NO_FLAGS);
300         }
301     }
302
303   /*
304    * Let us make sure we use the same names for all our decodes
305    * here.  First, dump the name into info column, and THEN
306    * dispatch the sub-type.
307    */
308   col_add_str(pinfo->cinfo, COL_INFO,
309                 val_to_str( type, esis_vals,
310                             "Unknown (0x%x)" ) );
311
312   variable_len = length - ESIS_HDR_FIXED_LENGTH;
313
314   switch (type) {
315   case ESIS_ESH_PDU:
316     esis_dissect_esh_pdu( variable_len, tvb, esis_tree, pinfo);
317     break;
318   case ESIS_ISH_PDU:
319     esis_dissect_ish_pdu( variable_len, tvb, esis_tree, pinfo);
320     break;
321   case ESIS_RD_PDU:
322     esis_dissect_redirect_pdu( variable_len, tvb, esis_tree, pinfo);
323     break;
324   default:
325     expert_add_info(pinfo, type_item, &ei_esis_type);
326   }
327   return tvb_captured_length(tvb);
328 } /* dissect_esis */
329
330
331 /*
332  * Name: proto_register_esis()
333  *
334  * Description:
335  *      main register for esis protocol set.  We register some display
336  *      formats and the protocol module variables.
337  *
338  *      NOTE: this procedure to autolinked by the makefile process that
339  *      builds register.c
340  *
341  * Input:
342  *      void
343  *
344  * Output:
345  *      void
346  */
347 void
348 proto_register_esis(void) {
349   static hf_register_info hf[] = {
350     { &hf_esis_nlpi,
351       { "Network Layer Protocol Identifier", "esis.nlpi",
352         FT_UINT8, BASE_HEX, VALS(nlpid_vals), 0x0, NULL, HFILL }},
353
354     { &hf_esis_length,
355       { "PDU Length", "esis.length",  FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
356
357     { &hf_esis_version,
358       { "Version", "esis.ver",  FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
359
360     { &hf_esis_reserved,
361       { "Reserved(==0)", "esis.res",  FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
362
363     { &hf_esis_type,
364       { "PDU Type", "esis.type",      FT_UINT8, BASE_DEC, VALS(esis_vals), OSI_PDU_TYPE_MASK, NULL, HFILL }},
365
366     { &hf_esis_holdtime,
367       { "Holding Time", "esis.htime", FT_UINT16, BASE_DEC, NULL, 0x0, "s", HFILL }},
368
369     { &hf_esis_checksum,
370       { "Checksum", "esis.chksum",    FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
371
372     { &hf_esis_checksum_status,
373       { "Checksum Status", "esis.chksum.status",    FT_UINT8, BASE_NONE, VALS(proto_checksum_vals), 0x0, NULL, HFILL }},
374
375       /* Generated from convert_proto_tree_add_text.pl */
376       { &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 }},
377       { &hf_esis_sal, { "SAL", "esis.sal", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
378       { &hf_esis_sa, { "SA", "esis.sa", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
379       { &hf_esis_netl, { "NETL", "esis.netl", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
380       { &hf_esis_dal, { "DAL", "esis.dal", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
381       { &hf_esis_bsnpal, { "BSNPAL", "esis.bsnpal", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
382       { &hf_esis_net, { "NET", "esis.net", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
383       { &hf_esis_da, { "DA", "esis.da", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
384       { &hf_esis_bsnpa, { "BSNPA", "esis.bsnpa", FT_SYSTEM_ID, BASE_NONE, NULL, 0x0, NULL, HFILL }},
385   };
386
387   static gint *ett[] = {
388     &ett_esis,
389     &ett_esis_area_addr,
390     &ett_esis_network,
391     &ett_esis_dest_addr,
392     &ett_esis_subnetwork
393   };
394
395   static ei_register_info ei[] = {
396     { &ei_esis_version, { "esis.ver.unknown", PI_PROTOCOL, PI_WARN, "Unknown ESIS version", EXPFILL }},
397     { &ei_esis_length, { "esis.length.invalid", PI_MALFORMED, PI_ERROR, "Bogus ESIS length", EXPFILL }},
398     { &ei_esis_type, { "esis.type.unknown", PI_PROTOCOL, PI_WARN, "Unknown ESIS packet type", EXPFILL }},
399     { &ei_esis_checksum, { "esis.bad_checksum", PI_CHECKSUM, PI_ERROR, "Bad checksum", EXPFILL }},
400   };
401
402   expert_module_t* expert_esis;
403
404   proto_esis = proto_register_protocol( PROTO_STRING_ESIS, "ESIS", "esis");
405   proto_register_field_array(proto_esis, hf, array_length(hf));
406   proto_register_subtree_array(ett, array_length(ett));
407   expert_esis = expert_register_protocol(proto_esis);
408   expert_register_field_array(expert_esis, ei, array_length(ei));
409   register_dissector("esis", dissect_esis, proto_esis);
410 }
411
412 void
413 proto_reg_handoff_esis(void)
414 {
415   dissector_handle_t esis_handle;
416
417   esis_handle = find_dissector("esis");
418   dissector_add_uint("osinl.incl", NLPID_ISO9542_ESIS, esis_handle);
419 }
420
421 /*
422  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
423  *
424  * Local Variables:
425  * c-basic-offset: 2
426  * tab-width: 8
427  * indent-tabs-mode: nil
428  * End:
429  *
430  * ex: set shiftwidth=2 tabstop=8 expandtab:
431  * :indentSize=2:tabSize=8:noTabs=true:
432  */