add tethereal_static
[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.10 2000/08/13 14:08:19 deniel 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, int id_length, frame_data *fd, proto_tree *tree);
78 static void dissect_hello_is_neighbors_clv(const u_char *pd, int offset, 
79                 guint length, int id_length, frame_data *fd, proto_tree *tree);
80 static void dissect_hello_padding_clv(const u_char *pd, int offset, 
81                 guint length, int id_length, frame_data *fd, proto_tree *tree);
82 static void dissect_hello_nlpid_clv(const u_char *pd, int offset, 
83                 guint length, int id_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, int id_length, frame_data *fd, proto_tree *tree);
86 static void dissect_hello_auth_clv(const u_char *pd, int offset, 
87                 guint length, int id_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  *      int : length of IDs in packet.
248  *      frame_data * : frame data
249  *      proto_tree * : proto tree to build on (may be null)
250  *
251  * Output:
252  *      void, will modify proto_tree if not null.
253  */
254 static void 
255 dissect_hello_nlpid_clv(const u_char *pd, int offset, 
256                 guint length, int id_length, frame_data *fd, proto_tree *tree) {
257         isis_dissect_nlpid_clv(pd, offset, length, fd, tree );
258 }
259
260 /*
261  * Name: dissect_hello_ip_int_addr_clv()
262  *
263  * Description:
264  *      Decode for a hello packets ip interface addr clv.  Calls into the
265  *      clv common one.
266  *
267  * Input:
268  *      u_char * : packet data
269  *      int : current offset into packet data
270  *      guint : length of this clv
271  *      int : length of IDs in packet.
272  *      frame_data * : frame data
273  *      proto_tree * : proto tree to build on (may be null)
274  *
275  * Output:
276  *      void, will modify proto_tree if not null.
277  */
278 static void 
279 dissect_hello_ip_int_addr_clv(const u_char *pd, int offset, 
280                 guint length, int id_length, frame_data *fd, proto_tree *tree) {
281         isis_dissect_ip_int_clv(pd, offset, length, fd, tree, 
282                 hf_isis_hello_clv_ipv4_int_addr );
283 }
284
285 /*
286  * Name: dissect_hello_auth_clv()
287  *
288  * Description:
289  *      Decode for a hello packets authenticaion clv.  Calls into the
290  *      clv common one.  An auth inside a hello packet is a perlink
291  *      password.
292  *
293  * Input:
294  *      u_char * : packet data
295  *      int : current offset into packet data
296  *      guint : length of this clv
297  *      int : length of IDs in packet.
298  *      frame_data * : frame data
299  *      proto_tree * : proto tree to build on (may be null)
300  *
301  * Output:
302  *      void, will modify proto_tree if not null.
303  */
304 static void 
305 dissect_hello_auth_clv(const u_char *pd, int offset, 
306                 guint length, int id_length, frame_data *fd, proto_tree *tree) {
307         isis_dissect_authentication_clv(pd, offset, length, fd, tree, 
308                 "Per Link authentication" );
309 }
310
311 /*
312  * Name: dissect_hello_area_address_clv()
313  *
314  * Description:
315  *      Decode for a hello packets area address clv.  Calls into the
316  *      clv common one.
317  *
318  * Input:
319  *      u_char * : packet data
320  *      int : current offset into packet data
321  *      guint : length of this clv
322  *      int : length of IDs in packet.
323  *      frame_data * : frame data
324  *      proto_tree * : proto tree to build on (may be null)
325  *
326  * Output:
327  *      void, will modify proto_tree if not null.
328  */
329 static void 
330 dissect_hello_area_address_clv(const u_char *pd, int offset, 
331                 guint length, int id_length, frame_data *fd, proto_tree *tree) {
332         isis_dissect_area_address_clv(pd, offset, length, fd, tree );
333 }
334
335 /*
336  * Name: isis_dissect_is_neighbors_clv()
337  * 
338  * Description:
339  *      Take apart a IS neighbor packet.  A neighbor is n 6 byte packets.
340  *      (they tend to be an 802.3 MAC address, but its not required).
341  *
342  * Input:
343  *      u_char * : packet data
344  *      int : offset into packet data where we are.
345  *      guint : length of clv we are decoding
346  *      int : length of IDs in packet.
347  *      frame_data * : frame data (complete frame)
348  *      proto_tree * : protocol display tree to fill out.  May be NULL
349  *      gint : tree id to use for proto tree.
350  * 
351  * Output:
352  *      void, but we will add to proto tree if !NULL.
353  */
354 void 
355 dissect_hello_is_neighbors_clv(const u_char *pd, int offset, 
356                 guint length, int id_length, frame_data *fd, proto_tree *tree ) {
357         while ( length > 0 ) {
358                 if (length<6) {
359                         isis_dissect_unknown(offset, length, tree, fd, 
360                                 "short is neighbor (%d vs 6)", length );
361                         return;
362                 }
363                 /* 
364                  * Lets turn the area address into "standard" 0000.0000.etc
365                  * format string.  
366                  */
367                 if ( tree ) {
368                         proto_tree_add_text ( tree, NullTVB, offset, 6, 
369                                 "IS Neighbor: %s", print_system_id( pd + offset, 6 ) ); 
370                 }
371                 offset += 6;
372                 length -= 6;
373         }
374 }
375
376
377 /*
378  * Name: dissect_hello_padding_clv()
379  *
380  * Description:
381  *      Decode for a hello packet's padding clv.  Padding does nothing,
382  *      so we just return.
383  *
384  * Input:
385  *      u_char * : packet data
386  *      int : current offset into packet data
387  *      guint : length of this clv
388  *      int : length of IDs in packet.
389  *      frame_data * : frame data
390  *      proto_tree * : proto tree to build on (may be null)
391  *
392  * Output:
393  *      void
394  */
395 static void 
396 dissect_hello_padding_clv(const u_char *pd, int offset, guint length, 
397                 int id_length, frame_data *fd, proto_tree *tree) {
398         /* nothing to do here! */
399 }
400
401 /*
402  * Name: isis_dissect_isis_hello()
403  * 
404  * Description:
405  *      This procedure rips apart the various types of ISIS hellos.  L1H and
406  *      L2H's are identical for the most part, while the PTP hello has
407  *      a shorter header.
408  *
409  * Input:
410  *      int : hello type, a la packet-isis.h ISIS_TYPE_* values
411  *      int : header length of packet.
412  *      int : length of IDs in packet.
413  *      u_char * : packet data
414  *      int offset : our offset into packet data.
415  *      frame_data * : frame data
416  *      proto_tree * : protocol display tree to add to.  May be NULL.
417  *
418  * Output:
419  *      void, will modify proto_tree if not NULL.
420  */     
421 void 
422 isis_dissect_isis_hello(int hello_type, int header_length, int id_length,
423                 const u_char *pd, int offset, frame_data *fd, proto_tree *tree){
424         proto_item      *ti;
425         proto_tree      *hello_tree = NULL;
426         int             len;
427         int             hlen;
428         guint16         pdu_length;
429
430         OLD_CHECK_DISPLAY_AS_DATA(proto_isis_hello, pd, offset, fd, tree);
431
432         if (hello_type == ISIS_TYPE_PTP_HELLO) {
433                 hlen = 1+id_length+2+2+1;
434         } else {
435                 hlen = 1+id_length+2+2+1+id_length+1;
436         }
437
438         if (!BYTES_ARE_IN_FRAME(offset, hlen)) {
439                 isis_dissect_unknown(offset, hlen, tree, fd,
440                         "not enough capture data for header (%d vs %d)",
441                         hlen, END_OF_FRAME);
442                 return;
443         }
444
445         if (tree) {
446                 ti = proto_tree_add_item(tree, proto_isis_hello, NullTVB,
447                         offset, END_OF_FRAME, FALSE);
448                 hello_tree = proto_item_add_subtree(ti, ett_isis_hello);
449                 proto_tree_add_uint_format(hello_tree,
450                         hf_isis_hello_circuit_reserved,
451                         NullTVB, offset, 1, pd[offset],
452                         "Circuit type              : %s, reserved(0x%02x == 0)",
453                                 val_to_str(pd[offset]&ISIS_HELLO_CTYPE_MASK,
454                                         isis_hello_circuit_type_vals,
455                                         "Unknown (0x%x)"),
456                                 pd[offset]&ISIS_HELLO_CT_RESERVED_MASK
457                         );
458         }
459         offset += 1;
460
461         if (tree) {
462                 proto_tree_add_bytes_format(hello_tree, hf_isis_hello_source_id, NullTVB,
463                                     offset, id_length, &pd[offset],
464                                     "SystemID{ Sender of PDU } : %s", 
465                      print_system_id( pd + offset, id_length ) );
466         }
467         offset += id_length;
468
469         if (tree) {
470                 proto_tree_add_uint(hello_tree, hf_isis_hello_holding_timer, NullTVB,
471                                     offset, 2, pntohs(&pd[offset]));
472         }
473         offset += 2;
474
475         pdu_length = pntohs(&pd[offset]);
476         if (tree) {
477                 proto_tree_add_uint(hello_tree, hf_isis_hello_pdu_length, NullTVB,
478                                     offset, 2, pdu_length);
479         }
480         offset += 2;
481
482         if (tree) {
483                 proto_tree_add_uint_format(hello_tree, hf_isis_hello_priority_reserved, NullTVB,
484                                     offset, 1, pd[offset],
485                                     "Priority                  : %d, reserved(0x%02x == 0)",
486                                         pd[offset]&ISIS_HELLO_PRIORITY_MASK,
487                                         pd[offset]&ISIS_HELLO_P_RESERVED_MASK );
488         }
489         offset += 1;
490
491         if (hello_type == ISIS_TYPE_PTP_HELLO) {
492                 if (tree) {
493                         proto_tree_add_uint(hello_tree, hf_isis_hello_local_circuit_id, NullTVB,
494                                          offset, 1, pd[offset] );
495                 }
496                 offset += 1;
497         } else { 
498                 if (tree) {
499                         proto_tree_add_bytes_format(hello_tree, hf_isis_hello_lan_id, NullTVB, 
500                                      offset, id_length + 1, &pd[offset],
501                                          "SystemID{ Designated IS } : %s",
502                                               print_system_id( pd + offset, id_length + 1 ) );
503                 }
504                 offset += id_length + 1;
505         }
506
507         len = pdu_length;
508         len -= header_length;
509         if (len < 0) {
510                 isis_dissect_unknown(offset, header_length, tree, fd, 
511                         "packet header length %d went beyond packet", 
512                         header_length );
513                 return;
514         }
515         /*
516          * Now, we need to decode our CLVs.  We need to pass in
517          * our list of valid ones!
518          */
519         if (hello_type == ISIS_TYPE_L1_HELLO){
520                 isis_dissect_clvs ( clv_l1_hello_opts, len, id_length, pd,
521                         offset, fd, hello_tree, ett_isis_hello_clv_unknown );
522         } else if (hello_type == ISIS_TYPE_L2_HELLO) {
523                 isis_dissect_clvs ( clv_l2_hello_opts, len, id_length, pd,
524                         offset, fd, hello_tree, ett_isis_hello_clv_unknown );
525         } else {
526                 isis_dissect_clvs ( clv_ptp_hello_opts, len, id_length, pd,
527                         offset, fd, hello_tree, ett_isis_hello_clv_unknown );
528         }
529 }
530
531 /*
532  * Name: proto_register_isis_hello()
533  *
534  * Description:
535  *      Register our protocol sub-sets with protocol manager.
536  *      NOTE: this procedure is autolinked by the makefile process that
537  *              builds register.c
538  *
539  * Input: 
540  *      void
541  *
542  * Output:
543  *      void
544  */
545 void
546 proto_register_isis_hello(void) {
547         static hf_register_info hf[] = {
548                 { &hf_isis_hello_circuit_reserved,
549                 { "Circuit type              ", "isis_hello.circuite_type",
550                         FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
551
552                 { &hf_isis_hello_source_id,
553                 { "SystemID{ Sender of PDU } ", "isis_hello.source_id",
554                         FT_BYTES, BASE_HEX, NULL, 0x0, "" }},
555
556                 { &hf_isis_hello_holding_timer,
557                 { "Holding timer             ", "isis_hello.holding_timer", 
558                         FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
559
560                 { &hf_isis_hello_pdu_length,
561                 { "PDU length                ", "isis_hello.pdu_length",
562                         FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
563
564                 { &hf_isis_hello_priority_reserved,
565                  { "Priority                 ", "isis_hello.priority",
566                         FT_UINT8, BASE_DEC, NULL, ISIS_HELLO_P_RESERVED_MASK, "" }},
567
568                 { &hf_isis_hello_lan_id,
569                 { "SystemID{ Designated IS } ", "isis_hello.lan_id",
570                         FT_BYTES, BASE_DEC, NULL, 0x0, "" }},
571
572                 { &hf_isis_hello_local_circuit_id,
573                 { "Local circuit ID          ", "isis_hello.local_circuit_id",
574                         FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
575
576                 { &hf_isis_hello_clv_ipv4_int_addr,
577                 { "IPv4 interface address    ", "isis_hello.clv_ipv4_int_addr",
578                         FT_IPv4, BASE_NONE, NULL, 0x0, "" }},
579
580         };
581         static gint *ett[] = {
582                 &ett_isis_hello,
583                 &ett_isis_hello_clv_area_addr,
584                 &ett_isis_hello_clv_is_neighbors,
585                 &ett_isis_hello_clv_padding,
586                 &ett_isis_hello_clv_unknown,
587                 &ett_isis_hello_clv_nlpid,
588                 &ett_isis_hello_clv_auth,
589                 &ett_isis_hello_clv_ipv4_int_addr,
590         };
591
592         proto_isis_hello = proto_register_protocol("ISIS HELLO", "isis_hello");
593         proto_register_field_array(proto_isis_hello, hf, array_length(hf));
594         proto_register_subtree_array(ett, array_length(ett));
595 }