Update Free Software Foundation address.
[metze/wireshark/wip.git] / epan / dissectors / packet-isis.c
1 /* packet-isis.c
2  * Routines for ISO/OSI network and transport protocol packet disassembly, core
3  * bits.
4  *
5  * $Id$
6  * Stuart Stanley <stuarts@mxmail.net>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <glib.h>
32 #include <epan/packet.h>
33 #include <epan/nlpid.h>
34 #include <epan/etypes.h>
35 #include "packet-osi.h"
36 #include "packet-isis.h"
37 #include "packet-isis-lsp.h"
38 #include "packet-isis-hello.h"
39 #include "packet-isis-snp.h"
40
41
42 /* isis base header */
43 static int proto_isis               = -1;
44
45 static int hf_isis_irpd             = -1;
46 static int hf_isis_header_length    = -1;
47 static int hf_isis_version          = -1;
48 static int hf_isis_system_id_length = -1;
49 static int hf_isis_type             = -1;
50 static int hf_isis_version2         = -1;
51 static int hf_isis_reserved         = -1;
52 static int hf_isis_max_area_adr     = -1;
53
54 static gint ett_isis                = -1;
55
56 static const value_string isis_vals[] = {
57   { ISIS_TYPE_L1_HELLO,  "L1 HELLO"},
58   { ISIS_TYPE_L2_HELLO,  "L2 HELLO"},
59   { ISIS_TYPE_PTP_HELLO, "P2P HELLO"},
60   { ISIS_TYPE_L1_LSP,    "L1 LSP"},
61   { ISIS_TYPE_L2_LSP,    "L2 LSP"},
62   { ISIS_TYPE_L1_CSNP,   "L1 CSNP"},
63   { ISIS_TYPE_L2_CSNP,   "L2 CSNP"},
64   { ISIS_TYPE_L1_PSNP,   "L1 PSNP"},
65   { ISIS_TYPE_L2_PSNP,   "L2 PSNP"},
66   { 0,                   NULL}      };
67
68 /*
69  * Name: isis_dissect_unknown()
70  *
71  * Description:
72  *      There was some error in the protocol and we are in unknown space
73  *      here.  Add a tree item to cover the error and go on.  Note
74  *      that we make sure we don't go off the end of the bleedin packet here!
75  *
76  * Input
77  *      tvbuff_t * : tvbuffer for packet data
78  *      proto_tree * : tree of display data.  May be NULL.
79  *      int : current offset into packet data
80  *      char * : format text
81  *      subsequent args : arguments to format
82  *
83  * Output:
84  *      void (may modify proto tree)
85  */
86 void
87 isis_dissect_unknown(tvbuff_t *tvb, proto_tree *tree, int offset,
88         const char *fmat, ...)
89 {
90         va_list ap;
91
92         va_start(ap, fmat);
93         proto_tree_add_text_valist(tree, tvb, offset, -1, fmat, ap);
94         va_end(ap);
95 }
96
97 /*
98  * Name: dissect_isis()
99  *
100  * Description:
101  *      Main entry area for isis de-mangling.  This will build the
102  *      main isis tree data and call the sub-protocols as needed.
103  *
104  * Input:
105  *      tvbuff_t * : tvbuffer for packet data
106  *      packet_info * : info for current packet
107  *      proto_tree * : tree of display data.  May be NULL.
108  *
109  * Output:
110  *      void, but we will add to the proto_tree if it is not NULL.
111  */
112 static void
113 dissect_isis(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
114 {
115         proto_item *ti;
116         proto_tree *isis_tree = NULL;
117         int offset = 0;
118         guint8 isis_version;
119         guint8 isis_header_length;
120         guint8 isis_type_reserved;
121         guint8 isis_type;
122         guint8 isis_system_id_len;
123
124         col_set_str(pinfo->cinfo, COL_PROTOCOL, "ISIS");
125         col_clear(pinfo->cinfo, COL_INFO);
126
127         isis_version = tvb_get_guint8(tvb, 2);
128         if (isis_version != ISIS_REQUIRED_VERSION){
129                 if (check_col(pinfo->cinfo, COL_INFO)) {
130                         col_add_fstr(pinfo->cinfo, COL_INFO,
131                                 "Unknown ISIS version (%u vs %u)",
132                                 isis_version, ISIS_REQUIRED_VERSION );
133                 }
134                 isis_dissect_unknown(tvb, tree, 0,
135                         "Unknown ISIS version (%d vs %d)",
136                         isis_version, ISIS_REQUIRED_VERSION);
137                 return;
138         }
139
140         if (tree) {
141                 ti = proto_tree_add_item(tree, proto_isis, tvb, 0, -1, ENC_NA);
142                 isis_tree = proto_item_add_subtree(ti, ett_isis);
143         }
144
145         if (tree) {
146                 proto_tree_add_item(isis_tree, hf_isis_irpd, tvb, offset, 1,
147                         ENC_BIG_ENDIAN );
148         }
149         offset += 1;
150
151         isis_header_length = tvb_get_guint8(tvb, offset);
152         if (tree) {
153                 proto_tree_add_uint(isis_tree, hf_isis_header_length, tvb,
154                         offset, 1, isis_header_length );
155         }
156         offset += 1;
157
158         if (tree) {
159                 proto_tree_add_uint(isis_tree, hf_isis_version, tvb,
160                         offset, 1, isis_version );
161         }
162         offset += 1;
163
164         isis_system_id_len = tvb_get_guint8(tvb, offset);
165         if (tree) {
166                 proto_tree_add_uint(isis_tree, hf_isis_system_id_length, tvb,
167                         offset, 1, isis_system_id_len );
168         }
169         offset += 1;
170
171         isis_type_reserved = tvb_get_guint8(tvb, offset);
172         isis_type = isis_type_reserved & ISIS_TYPE_MASK;
173         if (check_col(pinfo->cinfo, COL_INFO)) {
174                 col_add_str(pinfo->cinfo, COL_INFO,
175                         val_to_str ( isis_type, isis_vals, "Unknown (0x%x)" ) );
176         }
177         if (tree) {
178                 proto_tree_add_uint_format(isis_tree, hf_isis_type, tvb,
179                         offset, 1, isis_type,
180                         "PDU Type           : %s (R:%s%s%s)",
181                         val_to_str(isis_type, isis_vals, "Unknown (0x%x)"),
182                         (isis_type_reserved & ISIS_R8_MASK) ? "1" : "0",
183                         (isis_type_reserved & ISIS_R7_MASK) ? "1" : "0",
184                         (isis_type_reserved & ISIS_R6_MASK) ? "1" : "0");
185         }
186         offset += 1;
187
188         if (tree) {
189                 proto_tree_add_item(isis_tree, hf_isis_version2, tvb, offset, 1,
190                         ENC_BIG_ENDIAN );
191         }
192         offset += 1;
193
194         if (tree) {
195                 proto_tree_add_item(isis_tree, hf_isis_reserved, tvb, offset, 1,
196                         ENC_BIG_ENDIAN );
197         }
198         offset += 1;
199
200         if (tree) {
201                 proto_tree_add_item(isis_tree, hf_isis_max_area_adr, tvb, offset, 1,
202                         ENC_BIG_ENDIAN );
203         }
204         offset += 1;
205
206         /*
207          * Interpret the system ID length.
208          */
209         if (isis_system_id_len == 0)
210                 isis_system_id_len = 6; /* zero means 6-octet ID field length */
211         else if (isis_system_id_len == 255) {
212                 isis_system_id_len = 0; /* 255 means null ID field */
213                 /* XXX - what about the LAN ID? */
214         }
215         /* XXX - otherwise, must be in the range 1 through 8 */
216
217         switch (isis_type) {
218         case ISIS_TYPE_L1_HELLO:
219         case ISIS_TYPE_L2_HELLO:
220         case ISIS_TYPE_PTP_HELLO:
221                 isis_dissect_isis_hello(tvb, pinfo, isis_tree, offset,
222                         isis_type, isis_header_length, isis_system_id_len);
223                 break;
224         case ISIS_TYPE_L1_LSP:
225         case ISIS_TYPE_L2_LSP:
226                 isis_dissect_isis_lsp(tvb, pinfo, isis_tree, offset,
227                         isis_type, isis_header_length, isis_system_id_len);
228                 break;
229         case ISIS_TYPE_L1_CSNP:
230         case ISIS_TYPE_L2_CSNP:
231                 isis_dissect_isis_csnp(tvb, pinfo, isis_tree, offset,
232                         isis_type, isis_header_length, isis_system_id_len);
233                 break;
234         case ISIS_TYPE_L1_PSNP:
235         case ISIS_TYPE_L2_PSNP:
236                 isis_dissect_isis_psnp(tvb, pinfo, isis_tree, offset,
237                         isis_type, isis_header_length, isis_system_id_len);
238                 break;
239         default:
240                 isis_dissect_unknown(tvb, tree, offset,
241                         "Unknown ISIS packet type");
242         }
243 } /* dissect_isis */
244
245
246 /*
247  * Name: proto_register_isis()
248  *
249  * Description:
250  *      main register for isis protocol set.  We register some display
251  *      formats and the protocol module variables.
252  *
253  *      NOTE: this procedure to autolinked by the makefile process that
254  *      builds register.c
255  *
256  * Input:
257  *      void
258  *
259  * Output:
260  *      void
261  */
262 void
263 proto_register_isis(void) {
264   static hf_register_info hf[] = {
265     { &hf_isis_irpd,
266       { "Intra Domain Routing Protocol Discriminator",  "isis.irpd",
267         FT_UINT8, BASE_HEX, VALS(nlpid_vals), 0x0, NULL, HFILL }},
268
269     { &hf_isis_header_length,
270       { "PDU Header Length", "isis.len", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
271
272     { &hf_isis_version,
273       { "Version (==1)", "isis.version", FT_UINT8,
274          BASE_DEC, NULL, 0x0, NULL, HFILL }},
275
276     { &hf_isis_system_id_length,
277       { "System ID Length", "isis.sysid_len",
278         FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
279
280     { &hf_isis_type,
281       { "PDU Type", "isis.type", FT_UINT8, BASE_DEC,
282         VALS(isis_vals), 0xff, NULL, HFILL }},
283
284     { &hf_isis_version2,
285       { "Version2 (==1)", "isis.version2", FT_UINT8, BASE_DEC, NULL,
286         0x0, NULL, HFILL }},
287
288     { &hf_isis_reserved,
289       { "Reserved (==0)", "isis.reserved", FT_UINT8, BASE_DEC, NULL,
290         0x0, NULL, HFILL }},
291
292     { &hf_isis_max_area_adr,
293       { "Max.AREAs: (0==3)", "isis.max_area_adr", FT_UINT8, BASE_DEC, NULL,
294       0x0, NULL, HFILL }},
295
296     };
297     /*
298      * Note, we pull in the unknown CLV handler here, since it
299      * is used by all ISIS packet types.
300      */
301     static gint *ett[] = {
302       &ett_isis,
303     };
304
305     proto_isis = proto_register_protocol(PROTO_STRING_ISIS, "ISIS", "isis");
306     proto_register_field_array(proto_isis, hf, array_length(hf));
307     proto_register_subtree_array(ett, array_length(ett));
308
309         register_dissector("isis", dissect_isis, proto_isis);
310
311     /*
312      * Call registration routines for other source files in the ISIS
313      * dissector.
314      */
315     isis_register_hello(proto_isis);
316     isis_register_lsp(proto_isis);
317     isis_register_csnp(proto_isis);
318     isis_register_psnp(proto_isis);
319 }
320
321 void
322 proto_reg_handoff_isis(void)
323 {
324     dissector_handle_t isis_handle;
325
326     isis_handle = create_dissector_handle(dissect_isis, proto_isis);
327     dissector_add_uint("osinl", NLPID_ISO10589_ISIS, isis_handle);
328     dissector_add_uint("ethertype", ETHERTYPE_L2ISIS, isis_handle);
329 }