Add routines for adding items to a protocol tree that take arguments of
[obnox/wireshark/wip.git] / packet-isis-hello.c
1 /* packet-isis-hello.c
2  * Routines for decoding isis hello packets and their CLVs
3  *
4  * $Id: packet-isis-hello.c,v 1.8 2000/05/31 05:07:12 guy Exp $
5  * Stuart Stanley <stuarts@mxmail.net>
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 1998 Gerald Combs
10  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  *
26  *
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
35 #endif
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <glib.h>
40 #include "packet.h"
41 #include "packet-osi.h"
42 #include "packet-isis.h"
43 #include "packet-isis-clv.h"
44 #include "packet-isis-hello.h"
45
46 /* hello packets */
47 static int proto_isis_hello                  = -1;
48 static int hf_isis_hello_circuit_reserved    = -1;
49 static int hf_isis_hello_source_id           = -1;
50 static int hf_isis_hello_holding_timer       = -1;
51 static int hf_isis_hello_pdu_length          = -1;
52 static int hf_isis_hello_priority_reserved   = -1;
53 static int hf_isis_hello_lan_id              = -1;
54 static int hf_isis_hello_local_circuit_id    = -1;
55 static int hf_isis_hello_clv_ipv4_int_addr   = -1;
56
57 static gint ett_isis_hello                   = -1;
58 static gint ett_isis_hello_clv_area_addr     = -1;
59 static gint ett_isis_hello_clv_is_neighbors  = -1;
60 static gint ett_isis_hello_clv_padding       = -1;
61 static gint ett_isis_hello_clv_unknown       = -1;
62 static gint ett_isis_hello_clv_nlpid         = -1;
63 static gint ett_isis_hello_clv_auth          = -1;
64 static gint ett_isis_hello_clv_ipv4_int_addr = -1;
65
66 static const value_string isis_hello_circuit_type_vals[] = {
67         { ISIS_HELLO_TYPE_RESERVED,     "Reserved 0 (discard PDU)"},
68         { ISIS_HELLO_TYPE_LEVEL_1,      "Level 1 only"},
69         { ISIS_HELLO_TYPE_LEVEL_2,      "Level 2 only"},
70         { ISIS_HELLO_TYPE_LEVEL_12,     "Level 1 and 2"},
71         { 0,            NULL} };
72
73 /* 
74  * Predclare dissectors for use in clv dissection.
75  */
76 static void dissect_hello_area_address_clv(const u_char *pd, int offset, 
77                 guint length, frame_data *fd, proto_tree *tree);
78 static void dissect_hello_is_neighbors_clv(const u_char *pd, int offset, 
79                 guint length, frame_data *fd, proto_tree *tree);
80 static void dissect_hello_padding_clv(const u_char *pd, int offset, 
81                 guint length, frame_data *fd, proto_tree *tree);
82 static void dissect_hello_nlpid_clv(const u_char *pd, int offset, 
83                 guint length, frame_data *fd, proto_tree *tree);
84 static void dissect_hello_ip_int_addr_clv(const u_char *pd, int offset, 
85                 guint length, frame_data *fd, proto_tree *tree);
86 static void dissect_hello_auth_clv(const u_char *pd, int offset, 
87                 guint length, frame_data *fd, proto_tree *tree);
88
89 static const isis_clv_handle_t clv_l1_hello_opts[] = {
90         {
91                 ISIS_CLV_L1H_AREA_ADDRESS,
92                 "Area address(es)",
93                 &ett_isis_hello_clv_area_addr,
94                 dissect_hello_area_address_clv
95         },
96         {
97                 ISIS_CLV_L1H_IS_NEIGHBORS,
98                 "IS Neighbor(s)",
99                 &ett_isis_hello_clv_is_neighbors,
100                 dissect_hello_is_neighbors_clv
101         },
102         {
103                 ISIS_CLV_L1H_PADDING,
104                 "Padding",
105                 &ett_isis_hello_clv_padding,
106                 dissect_hello_padding_clv
107         },
108         {
109                 ISIS_CLV_L1H_NLPID,
110                 "NLPID",
111                 &ett_isis_hello_clv_nlpid,
112                 dissect_hello_nlpid_clv
113         },
114         {
115                 ISIS_CLV_L1H_IP_INTERFACE_ADDR,
116                 "IP Interface address(es)",
117                 &ett_isis_hello_clv_ipv4_int_addr,
118                 dissect_hello_ip_int_addr_clv
119         },
120         {
121                 ISIS_CLV_L1H_AUTHENTICATION_NS,
122                 "Authentication(non spec)",
123                 &ett_isis_hello_clv_auth,
124                 dissect_hello_auth_clv
125         },
126         {
127                 ISIS_CLV_L1H_AUTHENTICATION,
128                 "Authentication",
129                 &ett_isis_hello_clv_auth,
130                 dissect_hello_auth_clv
131         },
132         {
133                 0,
134                 "",
135                 NULL,
136                 NULL
137         }
138 };
139
140 static const isis_clv_handle_t clv_l2_hello_opts[] = {
141         {
142                 ISIS_CLV_L2H_AREA_ADDRESS,
143                 "Area address(es)",
144                 &ett_isis_hello_clv_area_addr,
145                 dissect_hello_area_address_clv
146         },
147         {
148                 ISIS_CLV_L2H_IS_NEIGHBORS,
149                 "IS Neighbor(s)",
150                 &ett_isis_hello_clv_is_neighbors,
151                 dissect_hello_is_neighbors_clv
152         },
153         {
154                 ISIS_CLV_L2H_PADDING,
155                 "Padding",
156                 &ett_isis_hello_clv_padding,
157                 dissect_hello_padding_clv
158         },
159         {
160                 ISIS_CLV_L2H_NLPID,
161                 "NLPID",
162                 &ett_isis_hello_clv_nlpid,
163                 dissect_hello_nlpid_clv
164         },
165         {
166                 ISIS_CLV_L2H_IP_INTERFACE_ADDR,
167                 "IP Interface address(es)",
168                 &ett_isis_hello_clv_ipv4_int_addr,
169                 dissect_hello_ip_int_addr_clv
170         },
171         {
172                 ISIS_CLV_L2H_AUTHENTICATION_NS,
173                 "Authentication(non spec)",
174                 &ett_isis_hello_clv_auth,
175                 dissect_hello_auth_clv
176         },
177         {
178                 ISIS_CLV_L2H_AUTHENTICATION,
179                 "Authentication",
180                 &ett_isis_hello_clv_auth,
181                 dissect_hello_auth_clv
182         },
183         {
184                 0,
185                 "",
186                 NULL,
187                 NULL
188         }
189 };
190
191 static const isis_clv_handle_t clv_ptp_hello_opts[] = {
192         {
193                 ISIS_CLV_PTP_AREA_ADDRESS,
194                 "Area address(es)",
195                 &ett_isis_hello_clv_area_addr,
196                 dissect_hello_area_address_clv
197         },
198         {
199                 ISIS_CLV_PTP_PADDING,
200                 "Padding",
201                 &ett_isis_hello_clv_padding,
202                 dissect_hello_padding_clv
203         },
204         {
205                 ISIS_CLV_PTP_NLPID,
206                 "NLPID",
207                 &ett_isis_hello_clv_nlpid,
208                 dissect_hello_nlpid_clv
209         },
210         {
211                 ISIS_CLV_PTP_IP_INTERFACE_ADDR,
212                 "IP Interface address(es)",
213                 &ett_isis_hello_clv_ipv4_int_addr,
214                 dissect_hello_ip_int_addr_clv
215         },
216         {
217                 ISIS_CLV_PTP_AUTHENTICATION_NS,
218                 "Authentication(non spec)",
219                 &ett_isis_hello_clv_auth,
220                 dissect_hello_auth_clv
221         },
222         {
223                 ISIS_CLV_PTP_AUTHENTICATION,
224                 "Authentication",
225                 &ett_isis_hello_clv_auth,
226                 dissect_hello_auth_clv
227         },
228         {
229                 0,
230                 "",
231                 NULL,
232                 NULL
233         }
234 };
235
236 /*
237  * Name: dissect_hello_nlpid_clv()
238  *
239  * Description:
240  *      Decode for a hello packets NLPID clv.  Calls into the
241  *      clv common one.
242  *
243  * Input:
244  *      u_char * : packet data
245  *      int : current offset into packet data
246  *      guint : length of this clv
247  *      frame_data * : frame data
248  *      proto_tree * : proto tree to build on (may be null)
249  *
250  * Output:
251  *      void, will modify proto_tree if not null.
252  */
253 static void 
254 dissect_hello_nlpid_clv(const u_char *pd, int offset, 
255                 guint length, frame_data *fd, proto_tree *tree) {
256         isis_dissect_nlpid_clv(pd, offset, length, fd, tree );
257 }
258
259 /*
260  * Name: dissect_hello_ip_int_addr_clv()
261  *
262  * Description:
263  *      Decode for a hello packets ip interface addr clv.  Calls into the
264  *      clv common one.
265  *
266  * Input:
267  *      u_char * : packet data
268  *      int : current offset into packet data
269  *      guint : length of this clv
270  *      frame_data * : frame data
271  *      proto_tree * : proto tree to build on (may be null)
272  *
273  * Output:
274  *      void, will modify proto_tree if not null.
275  */
276 static void 
277 dissect_hello_ip_int_addr_clv(const u_char *pd, int offset, 
278                 guint length, frame_data *fd, proto_tree *tree) {
279         isis_dissect_ip_int_clv(pd, offset, length, fd, tree, 
280                 hf_isis_hello_clv_ipv4_int_addr );
281 }
282
283 /*
284  * Name: dissect_hello_auth_clv()
285  *
286  * Description:
287  *      Decode for a hello packets authenticaion clv.  Calls into the
288  *      clv common one.  An auth inside a hello packet is a perlink
289  *      password.
290  *
291  * Input:
292  *      u_char * : packet data
293  *      int : current offset into packet data
294  *      guint : length of this clv
295  *      frame_data * : frame data
296  *      proto_tree * : proto tree to build on (may be null)
297  *
298  * Output:
299  *      void, will modify proto_tree if not null.
300  */
301 static void 
302 dissect_hello_auth_clv(const u_char *pd, int offset, 
303                 guint length, frame_data *fd, proto_tree *tree) {
304         isis_dissect_authentication_clv(pd, offset, length, fd, tree, 
305                 "Per Link authentication" );
306 }
307
308 /*
309  * Name: dissect_hello_area_address_clv()
310  *
311  * Description:
312  *      Decode for a hello packets area address clv.  Calls into the
313  *      clv common one.
314  *
315  * Input:
316  *      u_char * : packet data
317  *      int : current offset into packet data
318  *      guint : length of this clv
319  *      frame_data * : frame data
320  *      proto_tree * : proto tree to build on (may be null)
321  *
322  * Output:
323  *      void, will modify proto_tree if not null.
324  */
325 static void 
326 dissect_hello_area_address_clv(const u_char *pd, int offset, 
327                 guint length, frame_data *fd, proto_tree *tree) {
328         isis_dissect_area_address_clv(pd, offset, length, fd, tree );
329 }
330
331 /*
332  * Name: isis_dissect_is_neighbors_clv()
333  * 
334  * Description:
335  *      Take apart a IS neighbor packet.  A neighbor is n 6 byte packets.
336  *      (they tend to be an 802.3 MAC address, but its not required).
337  *
338  * Input:
339  *      u_char * : packet data
340  *      int : offset into packet data where we are.
341  *      guint : length of clv we are decoding
342  *      frame_data * : frame data (complete frame)
343  *      proto_tree * : protocol display tree to fill out.  May be NULL
344  *      gint : tree id to use for proto tree.
345  * 
346  * Output:
347  *      void, but we will add to proto tree if !NULL.
348  */
349 void 
350 dissect_hello_is_neighbors_clv(const u_char *pd, int offset, 
351                 guint length, frame_data *fd, proto_tree *tree ) {
352         while ( length > 0 ) {
353                 if (length<6) {
354                         isis_dissect_unknown(offset, length, tree, fd, 
355                                 "short is neighbor (%d vs 6)", length );
356                         return;
357                 }
358                 /* 
359                  * Lets turn the area address into "standard" 0000.0000.etc
360                  * format string.  
361                  */
362                 if ( tree ) {
363                         proto_tree_add_text ( tree, NullTVB, offset, 6, 
364                                 "IS Neighbor: %s", print_system_id( pd + offset, 6 ) ); 
365                 }
366                 offset += 6;
367                 length -= 6;
368         }
369 }
370
371
372 /*
373  * Name: dissect_hello_padding_clv()
374  *
375  * Description:
376  *      Decode for a hello packet's padding clv.  Padding does nothing,
377  *      so we just return.
378  *
379  * Input:
380  *      u_char * : packet data
381  *      int : current offset into packet data
382  *      guint : length of this clv
383  *      frame_data * : frame data
384  *      proto_tree * : proto tree to build on (may be null)
385  *
386  * Output:
387  *      void
388  */
389 static void 
390 dissect_hello_padding_clv(const u_char *pd, int offset, guint length, 
391                 frame_data *fd, proto_tree *tree) {
392         /* nothing to do here! */
393 }
394
395 /*
396  * Name: isis_dissect_isis_hello()
397  * 
398  * Description:
399  *      This procedure rips apart the various types of ISIS hellos.  L1H and
400  *      L2H's are identicle for the most part, while the PTP hello has
401  *      a shorter header.
402  *
403  * Input:
404  *      int : hello type, alla packet-isis.h ISIS_TYPE_* values
405  *      int : header length of packet.
406  *      u_char * : packet data
407  *      int offset : our offset into packet data.
408  *      frame_data * : frame data
409  *      proto_tree * : protocol display tree to add to.  May be NULL.
410  *
411  * Output:
412  *      void, will modify proto_tree if not NULL.
413  */     
414 void 
415 isis_dissect_isis_hello(int hello_type, int header_length, 
416                 const u_char *pd, int offset, frame_data *fd, proto_tree *tree){
417         isis_hello_t    *ihp;
418         proto_item      *ti;
419         proto_tree      *hello_tree = NULL;
420         int             len;
421         int             hlen;
422
423         if (hello_type == ISIS_TYPE_PTP_HELLO) {
424                 hlen = sizeof(*ihp) - 6;        /* make length correct */
425         } else {
426                 hlen = sizeof(*ihp);
427         }
428
429         if (!BYTES_ARE_IN_FRAME(offset, hlen)) {
430                 isis_dissect_unknown(offset, hlen, tree, fd,
431                         "not enough capture data for header (%d vs %d)",
432                         hlen, END_OF_FRAME);
433                 return;
434         }
435
436         ihp = (isis_hello_t *) &pd[offset];     
437
438         if (tree) {
439                 ti = proto_tree_add_item(tree, proto_isis_hello, NullTVB,
440                         offset, END_OF_FRAME, FALSE);
441                 hello_tree = proto_item_add_subtree(ti, ett_isis_hello);
442                 proto_tree_add_uint_format(hello_tree, 
443                         hf_isis_hello_circuit_reserved,
444                         NullTVB, offset, 1, ihp->isis_hello_circuit_reserved,
445                         "Circuit type              : %s, reserved(0x%02x == 0)",
446                                 val_to_str(ihp->isis_hello_circuit, 
447                                         isis_hello_circuit_type_vals,
448                                         "Unknown (0x%x)"),
449                                 ihp->isis_hello_creserved
450                         );
451
452                 proto_tree_add_string_format(hello_tree, hf_isis_hello_lan_id, NullTVB,
453                                     offset + 1, 6, ihp->isis_hello_source_id,
454                                     "SystemID{ Sender of PDU } : %s", 
455                      print_system_id( pd + offset + 1, 6 ) );
456                 proto_tree_add_uint(hello_tree, hf_isis_hello_holding_timer, NullTVB,
457                                     offset + 7, 2,pntohs(&ihp->isis_hello_holding_timer[0]));
458                 proto_tree_add_uint(hello_tree, hf_isis_hello_pdu_length, NullTVB,
459                                     offset + 9, 2,pntohs(&ihp->isis_hello_pdu_length[0]));
460                 proto_tree_add_uint_format(hello_tree, hf_isis_hello_priority_reserved, NullTVB,
461                                     offset + 11, 1, ihp->isis_hello_priority_reserved,
462                                     "Priority                  : %d, reserved(0x%02x == 0)",
463                                          ihp->isis_hello_priority, ihp->isis_hello_preserved );
464                 if (hello_type == ISIS_TYPE_PTP_HELLO) {
465                         proto_tree_add_uint(hello_tree, hf_isis_hello_local_circuit_id, NullTVB,
466                                          offset + 12, 1, ihp->isis_hello_lan_id[0] );
467                 } else { 
468                         proto_tree_add_string_format(hello_tree, hf_isis_hello_lan_id, NullTVB, 
469                      offset + 12, 7, ihp->isis_hello_lan_id,
470                                          "SystemID{ Designated IS } : %s",
471                                               print_system_id( pd + offset + 12, 7 ) );
472                 }
473         }
474
475         offset += hlen;
476         len = pntohs(&ihp->isis_hello_pdu_length[0]);
477         len -= header_length;
478         if (len < 0) {
479                 isis_dissect_unknown(offset, header_length, tree, fd, 
480                         "packet header length %d went beyond packet", 
481                         header_length );
482                 return;
483         }
484         /*
485          * Now, we need to decode our CLVs.  We need to pass in
486          * our list of valid ones!
487          */
488         if (hello_type == ISIS_TYPE_L1_HELLO){
489                 isis_dissect_clvs ( clv_l1_hello_opts, len, pd, offset, fd, 
490                         hello_tree, ett_isis_hello_clv_unknown );
491         } else if (hello_type == ISIS_TYPE_L2_HELLO) {
492                 isis_dissect_clvs ( clv_l2_hello_opts, len, pd, offset, fd, 
493                         hello_tree, ett_isis_hello_clv_unknown );
494         } else {
495                 isis_dissect_clvs ( clv_ptp_hello_opts, len, pd, offset, fd, 
496                         hello_tree, ett_isis_hello_clv_unknown );
497         }
498 }
499
500 /*
501  * Name: proto_register_isis_hello()
502  *
503  * Description:
504  *      Register our protocol sub-sets with protocol manager.
505  *      NOTE: this procedure is autolinked by the makefile process that
506  *              builds register.c
507  *
508  * Input: 
509  *      void
510  *
511  * Output:
512  *      void
513  */
514 void
515 proto_register_isis_hello(void) {
516         static hf_register_info hf[] = {
517                 { &hf_isis_hello_circuit_reserved,
518                 { "Circuit type              ", "isis_hello.circuite_type",
519                         FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
520
521                 { &hf_isis_hello_source_id,
522                 { "SystemID{ Sender of PDU } ", "isis_hello.source_id",
523                         FT_ETHER, BASE_HEX, NULL, 0x0, "" }},
524
525                 { &hf_isis_hello_holding_timer,
526                 { "Holding timer             ", "isis_hello.holding_timer", 
527                         FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
528
529                 { &hf_isis_hello_pdu_length,
530                 { "PDU length                ", "isis_hello.pdu_length",
531                         FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
532
533                 { &hf_isis_hello_priority_reserved,
534                  { "Priority                 ", "isis_hello.priority",FT_UINT8, BASE_DEC, NULL, 
535                     ISIS_HELLO_P_RESERVED_MASK, "" }},
536
537                 { &hf_isis_hello_lan_id,
538                 { "SystemID{ Designated IS } ", "isis_hello.lan_id", FT_STRING, BASE_DEC, NULL,
539          0x0, "" }},
540
541                 { &hf_isis_hello_local_circuit_id,
542                 { "Local circuit ID          ", "isis_hello.local_circuit_id", FT_UINT8,
543                    BASE_DEC, NULL, 0x0, "" }},
544
545                 { &hf_isis_hello_clv_ipv4_int_addr,
546                 { "IPv4 interface address    ", "isis_hello.clv_ipv4_int_addr", FT_IPv4,
547                    BASE_NONE, NULL, 0x0, "" }},
548
549         };
550         static gint *ett[] = {
551                 &ett_isis_hello,
552                 &ett_isis_hello_clv_area_addr,
553                 &ett_isis_hello_clv_is_neighbors,
554                 &ett_isis_hello_clv_padding,
555                 &ett_isis_hello_clv_unknown,
556                 &ett_isis_hello_clv_nlpid,
557                 &ett_isis_hello_clv_auth,
558                 &ett_isis_hello_clv_ipv4_int_addr,
559         };
560
561         proto_isis_hello = proto_register_protocol("ISIS HELLO", "isis_hello");
562         proto_register_field_array(proto_isis_hello, hf, array_length(hf));
563         proto_register_subtree_array(ett, array_length(ett));
564 }
565