Make the stuff to handle SNAP frames (OUI, PID, payload) a routine of
[obnox/wireshark/wip.git] / packet-isis.c
1 /* packet-isis-core.c
2  * Routines for ISO/OSI network and transport protocol packet disassembly, core
3  * bits.
4  *
5  * $Id: packet-isis.c,v 1.18 2001/01/10 09:07:35 guy Exp $
6  * Stuart Stanley <stuarts@mxmail.net>
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  *
27  *
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
36 #endif
37
38 #include <stdio.h>
39 #include <string.h>
40 #include <glib.h>
41 #include "packet.h"
42 #include "nlpid.h"
43 #include "packet-osi.h"
44 #include "packet-isis.h"
45 #include "packet-isis-lsp.h"
46 #include "packet-isis-hello.h"
47 #include "packet-isis-snp.h"
48
49
50 /* isis base header */
51 static int proto_isis               = -1;
52
53 static int hf_isis_irpd             = -1;
54 static int hf_isis_header_length    = -1;
55 static int hf_isis_version          = -1;
56 static int hf_isis_system_id_length = -1;
57 static int hf_isis_type             = -1;
58 static int hf_isis_version2         = -1;
59 static int hf_isis_reserved         = -1;
60 static int hf_isis_max_area_adr     = -1;
61
62 static gint ett_isis                = -1;
63
64 static const value_string isis_vals[] = {
65   { ISIS_TYPE_L1_HELLO,  "L1 HELLO"},
66   { ISIS_TYPE_L2_HELLO,  "L2 HELLO"},
67   { ISIS_TYPE_PTP_HELLO, "P2P HELLO"},
68   { ISIS_TYPE_L1_LSP,    "L1 LSP"},
69   { ISIS_TYPE_L2_LSP,    "L2 LSP"},
70   { ISIS_TYPE_L1_CSNP,   "L1 CSNP"},
71   { ISIS_TYPE_L2_CSNP,   "L2 CSNP"},
72   { ISIS_TYPE_L1_PSNP,   "L1 PSNP"},
73   { ISIS_TYPE_L2_PSNP,   "L2 PSNP"},
74   { 0,                   NULL}      };
75
76 /*
77  * Name: dissect_isis_unknown()
78  *
79  * Description:
80  *      There was some error in the protocol and we are in unknown space
81  *      here.  Add a tree item to cover the error and go on.  Note
82  *      that we make sure we don't go off the end of the bleedin packet here!
83  *
84  * Input
85  *      unt offset : Current offset into packet data.
86  *      int len : length of to dump.
87  *      proto_tree * : tree of display data.  May be NULL.
88  *      frame_data * fd : frame data
89  *      char * : format text
90  *
91  * Output:
92  *      void (may modify proto tree)
93  */
94 void
95 isis_dissect_unknown(int offset,guint length,proto_tree *tree,frame_data *fd,
96                 char *fmat, ...){
97         va_list ap;
98
99         if ( !IS_DATA_IN_FRAME(offset) ) {
100                 /* 
101                  * big oops   They were off the end of the packet already.
102                  * Just ignore this one.
103                  */
104                 return;
105         }
106         if ( !BYTES_ARE_IN_FRAME(offset, length) ) {
107                 /* 
108                  * length will take us past eop.  Truncate length.
109                  */
110                 length = END_OF_FRAME;
111         }
112
113         va_start(ap, fmat);
114         proto_tree_add_text_valist(tree, NullTVB, offset, length, fmat, ap);
115         va_end(ap);
116 }
117 /*
118  * Name: dissect_isis()
119  * 
120  * Description:
121  *      Main entry area for isis de-mangling.  This will build the
122  *      main isis tree data and call the sub-protocols as needed.
123  *
124  * Input:
125  *      u_char * : packet data
126  *      int : offset into packet where we are (packet_data[offset]== start
127  *              of what we care about)
128  *      frame_data * : frame data (whole packet with extra info)
129  *      proto_tree * : tree of display data.  May be NULL.
130  *
131  * Output:
132  *      void, but we will add to the proto_tree if it is not NULL.
133  */
134 static void
135 dissect_isis(const u_char *pd, int offset, frame_data *fd, 
136                 proto_tree *tree) {
137         isis_hdr_t *ihdr;
138         proto_item *ti;
139         proto_tree *isis_tree = NULL;
140         int id_length;
141
142         OLD_CHECK_DISPLAY_AS_DATA(proto_isis, pd, offset, fd, tree);
143
144         if (check_col(fd, COL_PROTOCOL))
145                 col_set_str(fd, COL_PROTOCOL, "ISIS");
146
147         if (!BYTES_ARE_IN_FRAME(offset, sizeof(*ihdr))) {
148                 isis_dissect_unknown(offset, sizeof(*ihdr), tree, fd,
149                         "not enough capture data for header (%d vs %d)",
150                         sizeof(*ihdr), END_OF_FRAME);
151                 return;
152         }
153
154         ihdr = (isis_hdr_t *) &pd[offset];
155
156         if (ihdr->isis_version != ISIS_REQUIRED_VERSION){
157                 isis_dissect_unknown(offset, END_OF_FRAME, tree, fd,
158                         "Unknown ISIS version (%d vs %d)",
159                         ihdr->isis_version, ISIS_REQUIRED_VERSION );
160                 return;
161         }
162         
163         
164         if (tree) {
165                 ti = proto_tree_add_item(tree, proto_isis, NullTVB, offset, 
166                         END_OF_FRAME, FALSE );
167                 isis_tree = proto_item_add_subtree(ti, ett_isis);
168                 proto_tree_add_uint(isis_tree, hf_isis_irpd, NullTVB, offset, 1,
169                         ihdr->isis_irpd );
170                 proto_tree_add_uint(isis_tree, hf_isis_header_length, NullTVB,
171                         offset + 1, 1, ihdr->isis_header_length );
172                 proto_tree_add_uint(isis_tree, hf_isis_version, NullTVB,
173                         offset + 2, 1, ihdr->isis_version );
174                 proto_tree_add_uint(isis_tree, hf_isis_system_id_length, NullTVB,
175                         offset + 3, 1, ihdr->isis_system_id_len );
176                 proto_tree_add_uint_format(isis_tree, hf_isis_type, NullTVB,
177                         offset + 4, 1, ihdr->isis_type,
178                         "Type               : %s (R:%s%s%s)",
179                         val_to_str(ihdr->isis_type & ISIS_TYPE_MASK, isis_vals,
180                                    "Unknown (0x%x)"),
181                         (ihdr->isis_type & ISIS_R8_MASK) ? "1" : "0",
182                         (ihdr->isis_type & ISIS_R7_MASK) ? "1" : "0",
183                         (ihdr->isis_type & ISIS_R6_MASK) ? "1" : "0");
184                 proto_tree_add_uint(isis_tree, hf_isis_version2, NullTVB,
185                         offset + 5, 1, ihdr->isis_version2 );
186                 proto_tree_add_uint(isis_tree, hf_isis_reserved, NullTVB,
187                         offset + 6, 1, ihdr->isis_reserved );
188                 proto_tree_add_uint(isis_tree, hf_isis_max_area_adr, NullTVB,
189                         offset + 7, 1, ihdr->isis_max_area_adr );
190         }
191
192
193         /*
194          * Let us make sure we use the same names for all our decodes
195          * here.  First, dump the name into info column, and THEN
196          * dispatch the sub-type.
197          */
198         if (check_col(fd, COL_INFO)) {
199                 col_add_str(fd, COL_INFO, val_to_str ( 
200                         ihdr->isis_type&ISIS_TYPE_MASK, isis_vals,
201                         "Unknown (0x%x)" ) );
202         }
203
204         /*
205          * Interpret the system ID length.
206          */
207         id_length = ihdr->isis_system_id_len;
208         if (id_length == 0)
209                 id_length = 6;  /* zero means 6-octet ID field length */
210         else if (id_length == 255) {
211                 id_length = 0;  /* 255 means null ID field */
212                 /* XXX - what about the LAN ID? */
213         }
214         /* XXX - otherwise, must be in the range 1 through 8 */
215
216         /*
217          * Advance offset (we are past the header).
218          */
219         offset += sizeof(*ihdr);
220         switch (ihdr->isis_type) {
221         case ISIS_TYPE_L1_HELLO:
222                 isis_dissect_isis_hello(ISIS_TYPE_L1_HELLO, 
223                         ihdr->isis_header_length, id_length,
224                         pd, offset, fd, isis_tree);
225                 break;
226         case ISIS_TYPE_L2_HELLO:
227                 isis_dissect_isis_hello(ISIS_TYPE_L2_HELLO, 
228                         ihdr->isis_header_length, id_length,
229                         pd, offset, fd, isis_tree);
230                 break;
231         case ISIS_TYPE_PTP_HELLO:
232                 isis_dissect_isis_hello(ISIS_TYPE_PTP_HELLO, 
233                         ihdr->isis_header_length, id_length,
234                         pd, offset, fd, isis_tree);
235                 break;
236         case ISIS_TYPE_L1_LSP:
237                 isis_dissect_isis_lsp(ISIS_TYPE_L1_LSP,
238                         ihdr->isis_header_length, id_length,
239                         pd, offset, fd, isis_tree);
240                 break;
241         case ISIS_TYPE_L2_LSP:
242                 isis_dissect_isis_lsp(ISIS_TYPE_L2_LSP,
243                         ihdr->isis_header_length, id_length,
244                         pd, offset, fd, isis_tree);
245                 break;
246         case ISIS_TYPE_L1_CSNP:
247                 isis_dissect_isis_csnp(ISIS_TYPE_L1_CSNP, 
248                         ihdr->isis_header_length, id_length,
249                         pd, offset, fd, isis_tree);
250                 break;
251         case ISIS_TYPE_L2_CSNP:
252                 isis_dissect_isis_csnp(ISIS_TYPE_L2_CSNP,
253                         ihdr->isis_header_length, id_length,
254                         pd, offset, fd, isis_tree);
255                 break;
256         case ISIS_TYPE_L1_PSNP:
257                 isis_dissect_isis_psnp(ISIS_TYPE_L1_PSNP, 
258                         ihdr->isis_header_length, id_length,
259                         pd, offset, fd, isis_tree);
260                 break;
261         case ISIS_TYPE_L2_PSNP:
262                 isis_dissect_isis_psnp(ISIS_TYPE_L2_PSNP,
263                         ihdr->isis_header_length, id_length,
264                         pd, offset, fd, isis_tree);
265                 break;
266         default:
267                 isis_dissect_unknown(offset, END_OF_FRAME, tree, fd,
268                         "unknown ISIS packet type" );
269         }
270 } /* dissect_isis */
271
272
273 /*
274  * Name: proto_register_isis()
275  *
276  * Description:
277  *      main register for isis protocol set.  We register some display
278  *      formats and the protocol module variables.
279  *
280  *      NOTE: this procedure to autolinked by the makefile process that
281  *      builds register.c
282  *
283  * Input: 
284  *      void
285  *
286  * Output:
287  *      void
288  */
289 void 
290 proto_register_isis(void) {
291   static hf_register_info hf[] = {
292     { &hf_isis_irpd,
293       { "Intra Domain Routing Protocol Discriminator",  "isis.irpd",    
294         FT_UINT8, BASE_HEX, VALS(nlpid_vals), 0x0, "" }},
295
296     { &hf_isis_header_length,
297       { "PDU Header Length  ", "isis.len", FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
298
299     { &hf_isis_version,
300       { "Version (==1)      ", "isis.version", FT_UINT8, 
301          BASE_DEC, NULL, 0x0, "" }},
302
303     { &hf_isis_system_id_length,
304       { "System ID Length   ", "isis.sysid_len",        
305         FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
306
307     { &hf_isis_type, 
308       { "PDU Type          :", "isis.type", FT_UINT8, BASE_DEC, 
309         VALS(isis_vals), 0xff, "" }},
310
311     { &hf_isis_version2, 
312       { "Version2 (==1)     ", "isis.version2", FT_UINT8, BASE_DEC, NULL, 
313         0x0, "" }},
314
315     { &hf_isis_reserved,
316       { "Reserved (==0)     ", "isis.reserved", FT_UINT8, BASE_DEC, NULL, 
317         0x0, "" }},
318
319     { &hf_isis_max_area_adr,
320       { "Max.AREAs: (0==3)  ", "isis.max_area_adr", FT_UINT8, BASE_DEC, NULL, 
321       0x0, "" }},
322
323     };
324     /*
325      * Note, we pull in the unknown CLV handler here, since it
326      * is used by all ISIS packet types.
327     */
328     static gint *ett[] = {
329       &ett_isis,
330     };
331
332     proto_isis = proto_register_protocol(PROTO_STRING_ISIS, "ISIS", "isis");
333     proto_register_field_array(proto_isis, hf, array_length(hf));
334     proto_register_subtree_array(ett, array_length(ett));
335 }
336
337 void
338 proto_reg_handoff_isis(void)
339 {
340     old_dissector_add("osinl", NLPID_ISO10589_ISIS, dissect_isis, proto_isis);
341     old_dissector_add("fr.ietf", NLPID_ISO10589_ISIS, dissect_isis, proto_isis);
342 }