Make the "isis_hello.source_id" an FT_BYTES field rather than an
[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.9 2000/06/19 08:33:48 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, 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         if (hello_type == ISIS_TYPE_PTP_HELLO) {
431                 hlen = 1+id_length+2+2+1;
432         } else {
433                 hlen = 1+id_length+2+2+1+id_length+1;
434         }
435
436         if (!BYTES_ARE_IN_FRAME(offset, hlen)) {
437                 isis_dissect_unknown(offset, hlen, tree, fd,
438                         "not enough capture data for header (%d vs %d)",
439                         hlen, END_OF_FRAME);
440                 return;
441         }
442
443         if (tree) {
444                 ti = proto_tree_add_item(tree, proto_isis_hello, NullTVB,
445                         offset, END_OF_FRAME, FALSE);
446                 hello_tree = proto_item_add_subtree(ti, ett_isis_hello);
447                 proto_tree_add_uint_format(hello_tree,
448                         hf_isis_hello_circuit_reserved,
449                         NullTVB, offset, 1, pd[offset],
450                         "Circuit type              : %s, reserved(0x%02x == 0)",
451                                 val_to_str(pd[offset]&ISIS_HELLO_CTYPE_MASK,
452                                         isis_hello_circuit_type_vals,
453                                         "Unknown (0x%x)"),
454                                 pd[offset]&ISIS_HELLO_CT_RESERVED_MASK
455                         );
456         }
457         offset += 1;
458
459         if (tree) {
460                 proto_tree_add_bytes_format(hello_tree, hf_isis_hello_source_id, NullTVB,
461                                     offset, id_length, &pd[offset],
462                                     "SystemID{ Sender of PDU } : %s", 
463                      print_system_id( pd + offset, id_length ) );
464         }
465         offset += id_length;
466
467         if (tree) {
468                 proto_tree_add_uint(hello_tree, hf_isis_hello_holding_timer, NullTVB,
469                                     offset, 2, pntohs(&pd[offset]));
470         }
471         offset += 2;
472
473         pdu_length = pntohs(&pd[offset]);
474         if (tree) {
475                 proto_tree_add_uint(hello_tree, hf_isis_hello_pdu_length, NullTVB,
476                                     offset, 2, pdu_length);
477         }
478         offset += 2;
479
480         if (tree) {
481                 proto_tree_add_uint_format(hello_tree, hf_isis_hello_priority_reserved, NullTVB,
482                                     offset, 1, pd[offset],
483                                     "Priority                  : %d, reserved(0x%02x == 0)",
484                                         pd[offset]&ISIS_HELLO_PRIORITY_MASK,
485                                         pd[offset]&ISIS_HELLO_P_RESERVED_MASK );
486         }
487         offset += 1;
488
489         if (hello_type == ISIS_TYPE_PTP_HELLO) {
490                 if (tree) {
491                         proto_tree_add_uint(hello_tree, hf_isis_hello_local_circuit_id, NullTVB,
492                                          offset, 1, pd[offset] );
493                 }
494                 offset += 1;
495         } else { 
496                 if (tree) {
497                         proto_tree_add_bytes_format(hello_tree, hf_isis_hello_lan_id, NullTVB, 
498                                      offset, id_length + 1, &pd[offset],
499                                          "SystemID{ Designated IS } : %s",
500                                               print_system_id( pd + offset, id_length + 1 ) );
501                 }
502                 offset += id_length + 1;
503         }
504
505         len = pdu_length;
506         len -= header_length;
507         if (len < 0) {
508                 isis_dissect_unknown(offset, header_length, tree, fd, 
509                         "packet header length %d went beyond packet", 
510                         header_length );
511                 return;
512         }
513         /*
514          * Now, we need to decode our CLVs.  We need to pass in
515          * our list of valid ones!
516          */
517         if (hello_type == ISIS_TYPE_L1_HELLO){
518                 isis_dissect_clvs ( clv_l1_hello_opts, len, id_length, pd,
519                         offset, fd, hello_tree, ett_isis_hello_clv_unknown );
520         } else if (hello_type == ISIS_TYPE_L2_HELLO) {
521                 isis_dissect_clvs ( clv_l2_hello_opts, len, id_length, pd,
522                         offset, fd, hello_tree, ett_isis_hello_clv_unknown );
523         } else {
524                 isis_dissect_clvs ( clv_ptp_hello_opts, len, id_length, pd,
525                         offset, fd, hello_tree, ett_isis_hello_clv_unknown );
526         }
527 }
528
529 /*
530  * Name: proto_register_isis_hello()
531  *
532  * Description:
533  *      Register our protocol sub-sets with protocol manager.
534  *      NOTE: this procedure is autolinked by the makefile process that
535  *              builds register.c
536  *
537  * Input: 
538  *      void
539  *
540  * Output:
541  *      void
542  */
543 void
544 proto_register_isis_hello(void) {
545         static hf_register_info hf[] = {
546                 { &hf_isis_hello_circuit_reserved,
547                 { "Circuit type              ", "isis_hello.circuite_type",
548                         FT_UINT8, BASE_HEX, NULL, 0x0, "" }},
549
550                 { &hf_isis_hello_source_id,
551                 { "SystemID{ Sender of PDU } ", "isis_hello.source_id",
552                         FT_BYTES, BASE_HEX, NULL, 0x0, "" }},
553
554                 { &hf_isis_hello_holding_timer,
555                 { "Holding timer             ", "isis_hello.holding_timer", 
556                         FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
557
558                 { &hf_isis_hello_pdu_length,
559                 { "PDU length                ", "isis_hello.pdu_length",
560                         FT_UINT16, BASE_DEC, NULL, 0x0, "" }},
561
562                 { &hf_isis_hello_priority_reserved,
563                  { "Priority                 ", "isis_hello.priority",
564                         FT_UINT8, BASE_DEC, NULL, ISIS_HELLO_P_RESERVED_MASK, "" }},
565
566                 { &hf_isis_hello_lan_id,
567                 { "SystemID{ Designated IS } ", "isis_hello.lan_id",
568                         FT_BYTES, BASE_DEC, NULL, 0x0, "" }},
569
570                 { &hf_isis_hello_local_circuit_id,
571                 { "Local circuit ID          ", "isis_hello.local_circuit_id",
572                         FT_UINT8, BASE_DEC, NULL, 0x0, "" }},
573
574                 { &hf_isis_hello_clv_ipv4_int_addr,
575                 { "IPv4 interface address    ", "isis_hello.clv_ipv4_int_addr",
576                         FT_IPv4, BASE_NONE, NULL, 0x0, "" }},
577
578         };
579         static gint *ett[] = {
580                 &ett_isis_hello,
581                 &ett_isis_hello_clv_area_addr,
582                 &ett_isis_hello_clv_is_neighbors,
583                 &ett_isis_hello_clv_padding,
584                 &ett_isis_hello_clv_unknown,
585                 &ett_isis_hello_clv_nlpid,
586                 &ett_isis_hello_clv_auth,
587                 &ett_isis_hello_clv_ipv4_int_addr,
588         };
589
590         proto_isis_hello = proto_register_protocol("ISIS HELLO", "isis_hello");
591         proto_register_field_array(proto_isis_hello, hf, array_length(hf));
592         proto_register_subtree_array(ett, array_length(ett));
593 }