- Decode some more annex C fields
[obnox/wireshark/wip.git] / epan / dissectors / packet-isis-clv.c
1 /* packet-isis-clv.c
2  * Common CLV decode routines.
3  *
4  * $Id$
5  * Stuart Stanley <stuarts@mxmail.net>
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <glib.h>
33
34 #include <epan/packet.h>
35 #include <epan/emem.h>
36 #include "packet-osi.h"
37 #include "packet-isis.h"
38 #include "packet-isis-clv.h"
39 #include <epan/nlpid.h>
40
41 /*
42  * Name: isis_dissect_area_address_clv()
43  *
44  * Description:
45  *      Take an area address CLV and display it pieces.  An area address
46  *      CLV is n, x byte hex strings.
47  *
48  * Input:
49  *      tvbuff_t * : tvbuffer for packet data
50  *      proto_tree * : protocol display tree to fill out.  May be NULL
51  *      int : offset into packet data where we are.
52  *      int : length of clv we are decoding
53  *
54  * Output:
55  *      void, but we will add to proto tree if !NULL.
56  */
57 void
58 isis_dissect_area_address_clv(tvbuff_t *tvb, proto_tree *tree, int offset,
59         int length)
60 {
61         int             arealen,area_idx;
62
63         while ( length > 0 ) {
64                 arealen = tvb_get_guint8(tvb, offset);
65                 length--;
66                 if (length<=0) {
67                         isis_dissect_unknown(tvb, tree, offset,
68                                 "short address (no length for payload)");
69                         return;
70                 }
71                 if ( arealen > length) {
72                         isis_dissect_unknown(tvb, tree, offset,
73                                 "short address, packet says %d, we have %d left",
74                                 arealen, length );
75                         return;
76                 }
77
78                 if ( tree ) {
79                         proto_item *ti;
80         
81                         /*
82                          * Throw an exception rather than putting in a
83                          * partial address.
84                          */
85                         tvb_ensure_bytes_exist ( tvb, offset, arealen + 1 );
86
87                         ti = proto_tree_add_text ( tree, tvb, offset, arealen + 1,
88                                 "Area address (%d): ", arealen );
89
90                         /*
91                          * Lets turn the area address into "standard"
92                          * xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx format string.
93                          * this is a private routine as the print_nsap_net in
94                          * epan/osi_utils.c is incomplete and we need only
95                          * a subset - actually some nice placing of dots ....
96                          */
97                         for (area_idx = 0; area_idx < arealen; area_idx++) {
98                                 proto_item_append_text(ti, "%02x",
99                                     tvb_get_guint8(tvb, offset+area_idx+1));
100                                 if (((area_idx & 1) == 0) &&
101                                     (area_idx + 1 < arealen)) {
102                                         proto_item_append_text(ti, ".");
103                                 }
104                         }
105                 }
106                 offset += arealen + 1;
107                 length -= arealen;      /* length already adjusted for len fld*/
108         }
109 }
110
111
112 /*
113  * Name: isis_dissect_authentication_clv()
114  *
115  * Description:
116  *      Take apart the CLV that hold authentication information.  This
117  *      is currently 1 octet auth type.
118  *      the two defined authentication types
119  *        are 1 for a clear text password and
120  *           54 for a HMAC-MD5 digest
121  *
122  * Input:
123  *      tvbuff_t * : tvbuffer for packet data
124  *      proto_tree * : protocol display tree to fill out.  May be NULL
125  *      int : offset into packet data where we are.
126  *      int : length of clv we are decoding
127  *
128  * Output:
129  *      void, but we will add to proto tree if !NULL.
130  */
131 void
132 isis_dissect_authentication_clv(tvbuff_t *tvb, proto_tree *tree, int offset,
133         int length)
134 {
135         guchar pw_type;
136         int auth_unsupported;
137         proto_item *ti;
138
139         if ( length <= 0 ) {
140                 return;
141         }
142
143         pw_type = tvb_get_guint8(tvb, offset);
144         offset += 1;
145         length--;
146         auth_unsupported = FALSE;
147
148         switch (pw_type) {
149         case 1:
150                 ti = proto_tree_add_text ( tree, tvb, offset - 1, length + 1,
151                     "clear text (1), password (length %d) = ", length);
152                 if ( length > 0 ) {
153                   proto_item_append_text(ti, "%s",
154                     tvb_format_text(tvb, offset, length));
155                 } else {
156                   proto_item_append_text(ti, "no clear-text password found!!!");
157                 }
158                 break;
159         case 54:
160                 ti = proto_tree_add_text ( tree, tvb, offset - 1, length + 1,
161                     "hmac-md5 (54), password (length %d) = ", length);
162
163                 if ( length == 16 ) {
164                   proto_item_append_text(ti, "0x%02x", tvb_get_guint8(tvb, offset));
165                   offset += 1;
166                   length--;
167                   while (length > 0) {
168                     proto_item_append_text(ti, "%02x", tvb_get_guint8(tvb, offset));
169                     offset += 1;
170                     length--;
171                   }
172                 } else {
173                   proto_item_append_text(ti,
174                       "illegal hmac-md5 digest format (must be 16 bytes)");
175                 }
176                 break;
177         default:
178                 ti = proto_tree_add_text ( tree, tvb, offset - 1, length + 1,
179                     "type 0x%02x (0x%02x): ", pw_type, length );
180                 auth_unsupported=TRUE;
181                 break;
182         }
183
184         if ( auth_unsupported ) {
185                 isis_dissect_unknown(tvb, tree, offset,
186                         "Unknown authentication type" );
187         }
188 }
189
190 /*
191  * Name: isis_ip_authentication_clv()
192  *
193  * Description:
194  *      dump the IP authentication information found in TLV 133
195  *      the CLV is standardized in rf1195, however all major
196  *      implementations use TLV #10
197  * Input:
198  *      tvbuff_t * : tvbuffer for packet data
199  *      proto_tree * : protocol display tree to fill out.  May be NULL
200  *      int : offset into packet data where we are.
201  *      int : length of clv we are decoding
202  *
203  * Output:
204  *      void, but we will add to proto tree if !NULL.
205  */
206
207
208 void
209 isis_dissect_ip_authentication_clv(tvbuff_t *tvb, proto_tree *tree, int offset,
210         int length)
211 {
212         if ( !tree ) return;            /* nothing to do! */
213
214         if ( length != 0 ) {
215                 proto_tree_add_text ( tree, tvb, offset, length,
216                         "IP Authentication: %.*s", length,
217                         tvb_get_ptr(tvb, offset, length) );
218         }
219 }
220
221
222 /*
223  * Name: isis_dissect_hostname_clv()
224  *
225  * Description:
226  *      dump the hostname information found in TLV 137
227  *      pls note that the hostname is not null terminated
228  *
229  * Input:
230  *      tvbuff_t * : tvbuffer for packet data
231  *      proto_tree * : protocol display tree to fill out.  May be NULL
232  *      int : offset into packet data where we are.
233  *      int : length of clv we are decoding
234  *
235  * Output:
236  *      void, but we will add to proto tree if !NULL.
237  */
238
239
240 void
241 isis_dissect_hostname_clv(tvbuff_t *tvb, proto_tree *tree, int offset,
242         int length)
243 {
244         if ( !tree ) return;            /* nothing to do! */
245
246         if ( length == 0 ) {
247                 proto_tree_add_text ( tree, tvb, offset, length,
248                         "Hostname: --none--" );
249         } else {
250                 proto_tree_add_text ( tree, tvb, offset, length,
251                         "Hostname: %.*s", length,
252                         tvb_get_ptr(tvb, offset, length) );
253         }
254 }
255
256
257
258
259 void
260 isis_dissect_mt_clv(tvbuff_t *tvb, proto_tree *tree, int offset, int length,
261         int tree_id)
262 {
263         guint16 mt_block;
264         char *mt_desc;
265
266         while (length>0) {
267             /* length can only be a multiple of 2, otherwise there is
268                something broken -> so decode down until length is 1 */
269             if (length!=1) {
270                 /* fetch two bytes */
271                 mt_block=tvb_get_ntohs(tvb, offset);
272
273                 /* mask out the lower 12 bits */
274                 switch(mt_block&0x0fff) {
275                 case 0:
276                     mt_desc="IPv4 unicast";
277                     break;
278                 case 1:
279                     mt_desc="In-Band Management";
280                     break;
281                 case 2:
282                     mt_desc="IPv6 unicast";
283                     break;
284                 case 3:
285                     mt_desc="Multicast";
286                     break;
287                 case 4095:
288                     mt_desc="Development, Experimental or Proprietary";
289                     break;
290                 default:
291                     mt_desc="Reserved for IETF Consensus";
292                     break;
293                 }
294                 proto_tree_add_uint_format ( tree, tree_id, tvb, offset, 2,
295                         mt_block,
296                         "%s Topology (0x%03x)%s%s",
297                                       mt_desc,
298                                       mt_block&0xfff,
299                                       (mt_block&0x8000) ? "" : ", no sub-TLVs present",
300                                       (mt_block&0x4000) ? ", ATT bit set" : "" );
301             } else {
302                 proto_tree_add_text ( tree, tvb, offset, 1,
303                         "malformed MT-ID");
304                 break;
305             }
306             length=length-2;
307             offset=offset+2;
308         }
309 }
310
311
312 /*
313  * Name: isis_dissect_ip_int_clv()
314  *
315  * Description:
316  *      Take apart the CLV that lists all the IP interfaces.  The
317  *      meaning of which is slightly different for the different base packet
318  *      types, but the display is not different.  What we have is n ip
319  *      addresses, plain and simple.
320  *
321  * Input:
322  *      tvbuff_t * : tvbuffer for packet data
323  *      proto_tree * : protocol display tree to fill out.  May be NULL
324  *      int : offset into packet data where we are.
325  *      int : length of clv we are decoding
326  *      int : tree id to use for proto tree.
327  *
328  * Output:
329  *      void, but we will add to proto tree if !NULL.
330  */
331 void
332 isis_dissect_ip_int_clv(tvbuff_t *tvb, proto_tree *tree, int offset,
333         int length, int tree_id)
334 {
335         if ( length <= 0 ) {
336                 return;
337         }
338
339         while ( length > 0 ) {
340                 if ( length < 4 ) {
341                         isis_dissect_unknown(tvb, tree, offset,
342                                 "Short IP interface address (%d vs 4)",length );
343                         return;
344                 }
345
346                 if ( tree ) {
347                         proto_tree_add_item(tree, tree_id, tvb, offset, 4, FALSE);
348                 }
349                 offset += 4;
350                 length -= 4;
351         }
352 }
353
354 /*
355  * Name: isis_dissect_ipv6_int_clv()
356  *
357  * Description:
358  *      Take apart the CLV that lists all the IPv6 interfaces.  The
359  *      meaning of which is slightly different for the different base packet
360  *      types, but the display is not different.  What we have is n ip
361  *      addresses, plain and simple.
362  *
363  * Input:
364  *      tvbuff_t * : tvbuffer for packet data
365  *      proto_tree * : protocol display tree to fill out.  May be NULL
366  *      int : offset into packet data where we are.
367  *      int : length of clv we are decoding
368  *      int : tree id to use for proto tree.
369  *
370  * Output:
371  *      void, but we will add to proto tree if !NULL.
372  */
373 void
374 isis_dissect_ipv6_int_clv(tvbuff_t *tvb, proto_tree *tree, int offset,
375         int length, int tree_id)
376 {
377         guint8 addr [16];
378
379         if ( length <= 0 ) {
380                 return;
381         }
382
383         while ( length > 0 ) {
384                 if ( length < 16 ) {
385                         isis_dissect_unknown(tvb, tree, offset,
386                                 "Short IPv6 interface address (%d vs 16)",length );
387                         return;
388                 }
389                 tvb_memcpy(tvb, addr, offset, sizeof(addr));
390                 if ( tree ) {
391                         proto_tree_add_ipv6(tree, tree_id, tvb, offset, 16, addr);
392                 }
393                 offset += 16;
394                 length -= 16;
395         }
396 }
397
398
399 /*
400  * Name: isis_dissect_te_router_id_clv()
401  *
402  * Description:
403  *      Display the Traffic Engineering Router ID TLV #134.
404  *      This TLV is like the IP Interface TLV, except that
405  *      only _one_ IP address is present
406  *
407  * Input:
408  *      tvbuff_t * : tvbuffer for packet data
409  *      proto_tree * : protocol display tree to fill out.  May be NULL
410  *      int : offset into packet data where we are.
411  *      int : length of clv we are decoding
412  *      int : tree id to use for proto tree.
413  *
414  * Output:
415  *      void, but we will add to proto tree if !NULL.
416  */
417 void
418 isis_dissect_te_router_id_clv(tvbuff_t *tvb, proto_tree *tree, int offset,
419         int length, int tree_id)
420 {
421         if ( length <= 0 ) {
422                 return;
423         }
424
425         if ( length != 4 ) {
426                 isis_dissect_unknown(tvb, tree, offset,
427                         "malformed Traffic Engineering Router ID (%d vs 4)",length );
428                 return;
429         }
430         if ( tree ) {
431                 proto_tree_add_item(tree, tree_id, tvb, offset, 4, FALSE);
432         }
433 }
434
435 /*
436  * Name: isis_dissect_nlpid_clv()
437  *
438  * Description:
439  *      Take apart a NLPID packet and display it.  The NLPID (for intergrated
440  *      ISIS, contains n network layer protocol IDs that the box supports.
441  *      We max out at 256 entries.
442  *
443  * Input:
444  *      tvbuff_t * : tvbuffer for packet data
445  *      proto_tree * : protocol display tree to fill out.  May be NULL
446  *      int : offset into packet data where we are.
447  *      int : length of clv we are decoding
448  *
449  * Output:
450  *      void, but we will add to proto tree if !NULL.
451  */
452
453 #define TRUNCATED_TEXT " [truncated]"
454 void
455 isis_dissect_nlpid_clv(tvbuff_t *tvb, proto_tree *tree, int offset, int length)
456 {
457         gboolean first;
458         proto_item *ti;
459
460         if ( !tree ) return;            /* nothing to do! */
461
462         /*
463          * Throw an exception rather than putting in a
464          * partial address.
465          */
466         tvb_ensure_bytes_exist ( tvb, offset, length );
467
468         if (length <= 0) {
469                 proto_tree_add_text (tree, tvb, offset, length,
470                         "NLPID(s): --none--");
471         } else {
472                 first = TRUE;
473                 ti = proto_tree_add_text (tree, tvb, offset, length,
474                         "NLPID(s): ");
475                 while (length-- > 0 ) {
476                         if (!first) {
477                                 proto_item_append_text(ti, ", ");
478                         }
479                         proto_item_append_text(ti, "%s (0x%02x)",
480                                 val_to_str(tvb_get_guint8(tvb, offset), nlpid_vals,
481                                 "Unknown"), tvb_get_guint8(tvb, offset));
482                         offset++;
483                         first = FALSE;
484                 }
485         }
486 }
487
488 /*
489  * Name: isis_dissect_clvs()
490  *
491  * Description:
492  *      Dispatch routine to shred all the CLVs in a packet.  We just
493  *      walk through the clv entries in the packet.  For each one, we
494  *      search the passed in valid clv's for this protocol (opts) for
495  *      a matching code.  If found, we add to the display tree and
496  *      then call the dissector.  If it is not, we just post an
497  *      "unknown" clv entry using the passed in unknown clv tree id.
498  *
499  * Input:
500  *      tvbuff_t * : tvbuffer for packet data
501  *      proto_tree * : protocol display tree to fill out.  May be NULL
502  *      int : offset into packet data where we are.
503  *      isis_clv_handle_t * : NULL dissector terminated array of codes
504  *              and handlers (along with tree text and tree id's).
505  *      int : length of CLV area.
506  *      int : length of IDs in packet.
507  *      int : unknown clv tree id
508  *
509  * Output:
510  *      void, but we will add to proto tree if !NULL.
511  */
512 void
513 isis_dissect_clvs(tvbuff_t *tvb, proto_tree *tree, int offset,
514         const isis_clv_handle_t *opts, int len, int id_length,
515         int unknown_tree_id)
516 {
517         guint8 code;
518         guint8 length;
519         int q;
520         proto_item      *ti;
521         proto_tree      *clv_tree;
522         int             adj;
523
524         while ( len > 0 ) {
525                 code = tvb_get_guint8(tvb, offset);
526                 offset += 1;
527
528                 length = tvb_get_guint8(tvb, offset);
529                 offset += 1;
530
531                 adj = (sizeof(code) + sizeof(length) + length);
532                 len -= adj;
533                 if ( len < 0 ) {
534                         isis_dissect_unknown(tvb, tree, offset,
535                                 "Short CLV header (%d vs %d)",
536                                 adj, len + adj );
537                         return;
538                 }
539                 q = 0;
540                 while ((opts[q].dissect != NULL )&&( opts[q].optcode != code )){
541                         q++;
542                 }
543                 if ( opts[q].dissect ) {
544                         if (tree) {
545                                 /* adjust by 2 for code/len octets */
546                                 ti = proto_tree_add_text(tree, tvb, offset - 2,
547                                         length + 2, "%s (%u)",
548                                         opts[q].tree_text, length );
549                                 clv_tree = proto_item_add_subtree(ti,
550                                         *opts[q].tree_id );
551                         } else {
552                                 clv_tree = NULL;
553                         }
554                         opts[q].dissect(tvb, clv_tree, offset,
555                                 id_length, length);
556                 } else {
557                         if (tree) {
558                                 ti = proto_tree_add_text(tree, tvb, offset - 2,
559                                         length + 2, "Unknown code %u (%u)",
560                                         code, length);
561                                 clv_tree = proto_item_add_subtree(ti,
562                                         unknown_tree_id );
563                         } else {
564                                 clv_tree = NULL;
565                         }
566                 }
567                 offset += length;
568         }
569 }