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