usbdi.9: Some small fixes.
[dragonfly.git] / contrib / tcpdump / print-isoclns.c
1 /*
2  * Copyright (c) 1992, 1993, 1994, 1995, 1996
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * Original code by Matt Thomas, Digital Equipment Corporation
22  *
23  * Extensively modified by Hannes Gredler (hannes@juniper.net) for more
24  * complete IS-IS & CLNP support.
25  */
26
27 #ifndef lint
28 static const char rcsid[] _U_ =
29     "@(#) $Header: /tcpdump/master/tcpdump/print-isoclns.c,v 1.165 2008-08-16 13:38:15 hannes Exp $ (LBL)";
30 #endif
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <tcpdump-stdinc.h>
37
38 #include <stdio.h>
39 #include <string.h>
40
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "ethertype.h"
44 #include "ether.h"
45 #include "nlpid.h"
46 #include "extract.h"
47 #include "gmpls.h"
48 #include "oui.h"
49 #include "signature.h"
50
51 /*
52  * IS-IS is defined in ISO 10589.  Look there for protocol definitions.
53  */
54
55 #define SYSTEM_ID_LEN   ETHER_ADDR_LEN
56 #define NODE_ID_LEN     SYSTEM_ID_LEN+1
57 #define LSP_ID_LEN      SYSTEM_ID_LEN+2
58
59 #define ISIS_VERSION    1
60 #define ESIS_VERSION    1
61 #define CLNP_VERSION    1
62
63 #define ISIS_PDU_TYPE_MASK      0x1F
64 #define ESIS_PDU_TYPE_MASK      0x1F
65 #define CLNP_PDU_TYPE_MASK      0x1F
66 #define CLNP_FLAG_MASK          0xE0
67 #define ISIS_LAN_PRIORITY_MASK  0x7F
68
69 #define ISIS_PDU_L1_LAN_IIH     15
70 #define ISIS_PDU_L2_LAN_IIH     16
71 #define ISIS_PDU_PTP_IIH        17
72 #define ISIS_PDU_L1_LSP         18
73 #define ISIS_PDU_L2_LSP         20
74 #define ISIS_PDU_L1_CSNP        24
75 #define ISIS_PDU_L2_CSNP        25
76 #define ISIS_PDU_L1_PSNP        26
77 #define ISIS_PDU_L2_PSNP        27
78
79 static struct tok isis_pdu_values[] = {
80     { ISIS_PDU_L1_LAN_IIH,       "L1 Lan IIH"},
81     { ISIS_PDU_L2_LAN_IIH,       "L2 Lan IIH"},
82     { ISIS_PDU_PTP_IIH,          "p2p IIH"},
83     { ISIS_PDU_L1_LSP,           "L1 LSP"},
84     { ISIS_PDU_L2_LSP,           "L2 LSP"},
85     { ISIS_PDU_L1_CSNP,          "L1 CSNP"},
86     { ISIS_PDU_L2_CSNP,          "L2 CSNP"},
87     { ISIS_PDU_L1_PSNP,          "L1 PSNP"},
88     { ISIS_PDU_L2_PSNP,          "L2 PSNP"},
89     { 0, NULL}
90 };
91
92 /*
93  * A TLV is a tuple of a type, length and a value and is normally used for
94  * encoding information in all sorts of places.  This is an enumeration of
95  * the well known types.
96  *
97  * list taken from rfc3359 plus some memory from veterans ;-)
98  */
99
100 #define ISIS_TLV_AREA_ADDR           1   /* iso10589 */
101 #define ISIS_TLV_IS_REACH            2   /* iso10589 */
102 #define ISIS_TLV_ESNEIGH             3   /* iso10589 */
103 #define ISIS_TLV_PART_DIS            4   /* iso10589 */
104 #define ISIS_TLV_PREFIX_NEIGH        5   /* iso10589 */
105 #define ISIS_TLV_ISNEIGH             6   /* iso10589 */
106 #define ISIS_TLV_ISNEIGH_VARLEN      7   /* iso10589 */
107 #define ISIS_TLV_PADDING             8   /* iso10589 */
108 #define ISIS_TLV_LSP                 9   /* iso10589 */
109 #define ISIS_TLV_AUTH                10  /* iso10589, rfc3567 */
110 #define ISIS_TLV_CHECKSUM            12  /* rfc3358 */
111 #define ISIS_TLV_CHECKSUM_MINLEN 2
112 #define ISIS_TLV_LSP_BUFFERSIZE      14  /* iso10589 rev2 */
113 #define ISIS_TLV_LSP_BUFFERSIZE_MINLEN 2
114 #define ISIS_TLV_EXT_IS_REACH        22  /* draft-ietf-isis-traffic-05 */
115 #define ISIS_TLV_IS_ALIAS_ID         24  /* draft-ietf-isis-ext-lsp-frags-02 */
116 #define ISIS_TLV_DECNET_PHASE4       42
117 #define ISIS_TLV_LUCENT_PRIVATE      66
118 #define ISIS_TLV_INT_IP_REACH        128 /* rfc1195, rfc2966 */
119 #define ISIS_TLV_PROTOCOLS           129 /* rfc1195 */
120 #define ISIS_TLV_EXT_IP_REACH        130 /* rfc1195, rfc2966 */
121 #define ISIS_TLV_IDRP_INFO           131 /* rfc1195 */
122 #define ISIS_TLV_IDRP_INFO_MINLEN      1
123 #define ISIS_TLV_IPADDR              132 /* rfc1195 */
124 #define ISIS_TLV_IPAUTH              133 /* rfc1195 */
125 #define ISIS_TLV_TE_ROUTER_ID        134 /* draft-ietf-isis-traffic-05 */
126 #define ISIS_TLV_EXTD_IP_REACH       135 /* draft-ietf-isis-traffic-05 */
127 #define ISIS_TLV_HOSTNAME            137 /* rfc2763 */
128 #define ISIS_TLV_SHARED_RISK_GROUP   138 /* draft-ietf-isis-gmpls-extensions */
129 #define ISIS_TLV_NORTEL_PRIVATE1     176
130 #define ISIS_TLV_NORTEL_PRIVATE2     177
131 #define ISIS_TLV_RESTART_SIGNALING   211 /* rfc3847 */
132 #define ISIS_TLV_RESTART_SIGNALING_FLAGLEN 1
133 #define ISIS_TLV_RESTART_SIGNALING_HOLDTIMELEN 2
134 #define ISIS_TLV_MT_IS_REACH         222 /* draft-ietf-isis-wg-multi-topology-05 */
135 #define ISIS_TLV_MT_SUPPORTED        229 /* draft-ietf-isis-wg-multi-topology-05 */
136 #define ISIS_TLV_MT_SUPPORTED_MINLEN 2
137 #define ISIS_TLV_IP6ADDR             232 /* draft-ietf-isis-ipv6-02 */
138 #define ISIS_TLV_MT_IP_REACH         235 /* draft-ietf-isis-wg-multi-topology-05 */
139 #define ISIS_TLV_IP6_REACH           236 /* draft-ietf-isis-ipv6-02 */
140 #define ISIS_TLV_MT_IP6_REACH        237 /* draft-ietf-isis-wg-multi-topology-05 */
141 #define ISIS_TLV_PTP_ADJ             240 /* rfc3373 */
142 #define ISIS_TLV_IIH_SEQNR           241 /* draft-shen-isis-iih-sequence-00 */
143 #define ISIS_TLV_IIH_SEQNR_MINLEN 4
144 #define ISIS_TLV_VENDOR_PRIVATE      250 /* draft-ietf-isis-experimental-tlv-01 */
145 #define ISIS_TLV_VENDOR_PRIVATE_MINLEN 3
146
147 static struct tok isis_tlv_values[] = {
148     { ISIS_TLV_AREA_ADDR,          "Area address(es)"},
149     { ISIS_TLV_IS_REACH,           "IS Reachability"},
150     { ISIS_TLV_ESNEIGH,            "ES Neighbor(s)"},
151     { ISIS_TLV_PART_DIS,           "Partition DIS"},
152     { ISIS_TLV_PREFIX_NEIGH,       "Prefix Neighbors"},
153     { ISIS_TLV_ISNEIGH,            "IS Neighbor(s)"},
154     { ISIS_TLV_ISNEIGH_VARLEN,     "IS Neighbor(s) (variable length)"},
155     { ISIS_TLV_PADDING,            "Padding"},
156     { ISIS_TLV_LSP,                "LSP entries"},
157     { ISIS_TLV_AUTH,               "Authentication"},
158     { ISIS_TLV_CHECKSUM,           "Checksum"},
159     { ISIS_TLV_LSP_BUFFERSIZE,     "LSP Buffersize"},
160     { ISIS_TLV_EXT_IS_REACH,       "Extended IS Reachability"},
161     { ISIS_TLV_IS_ALIAS_ID,        "IS Alias ID"},
162     { ISIS_TLV_DECNET_PHASE4,      "DECnet Phase IV"},
163     { ISIS_TLV_LUCENT_PRIVATE,     "Lucent Proprietary"},
164     { ISIS_TLV_INT_IP_REACH,       "IPv4 Internal Reachability"},
165     { ISIS_TLV_PROTOCOLS,          "Protocols supported"},
166     { ISIS_TLV_EXT_IP_REACH,       "IPv4 External Reachability"},
167     { ISIS_TLV_IDRP_INFO,          "Inter-Domain Information Type"},
168     { ISIS_TLV_IPADDR,             "IPv4 Interface address(es)"},
169     { ISIS_TLV_IPAUTH,             "IPv4 authentication (deprecated)"},
170     { ISIS_TLV_TE_ROUTER_ID,       "Traffic Engineering Router ID"},
171     { ISIS_TLV_EXTD_IP_REACH,      "Extended IPv4 Reachability"},
172     { ISIS_TLV_SHARED_RISK_GROUP,  "Shared Risk Link Group"},
173     { ISIS_TLV_NORTEL_PRIVATE1,    "Nortel Proprietary"},
174     { ISIS_TLV_NORTEL_PRIVATE2,    "Nortel Proprietary"},
175     { ISIS_TLV_HOSTNAME,           "Hostname"},
176     { ISIS_TLV_RESTART_SIGNALING,  "Restart Signaling"},
177     { ISIS_TLV_MT_IS_REACH,        "Multi Topology IS Reachability"},
178     { ISIS_TLV_MT_SUPPORTED,       "Multi Topology"},
179     { ISIS_TLV_IP6ADDR,            "IPv6 Interface address(es)"},
180     { ISIS_TLV_MT_IP_REACH,        "Multi-Topology IPv4 Reachability"},
181     { ISIS_TLV_IP6_REACH,          "IPv6 reachability"},
182     { ISIS_TLV_MT_IP6_REACH,       "Multi-Topology IP6 Reachability"},
183     { ISIS_TLV_PTP_ADJ,            "Point-to-point Adjacency State"},
184     { ISIS_TLV_IIH_SEQNR,          "Hello PDU Sequence Number"},
185     { ISIS_TLV_VENDOR_PRIVATE,     "Vendor Private"},
186     { 0, NULL }
187 };
188
189 #define ESIS_OPTION_PROTOCOLS        129
190 #define ESIS_OPTION_QOS_MAINTENANCE  195 /* iso9542 */
191 #define ESIS_OPTION_SECURITY         197 /* iso9542 */
192 #define ESIS_OPTION_ES_CONF_TIME     198 /* iso9542 */
193 #define ESIS_OPTION_PRIORITY         205 /* iso9542 */
194 #define ESIS_OPTION_ADDRESS_MASK     225 /* iso9542 */
195 #define ESIS_OPTION_SNPA_MASK        226 /* iso9542 */
196
197 static struct tok esis_option_values[] = {
198     { ESIS_OPTION_PROTOCOLS,       "Protocols supported"},
199     { ESIS_OPTION_QOS_MAINTENANCE, "QoS Maintenance" },
200     { ESIS_OPTION_SECURITY,        "Security" },
201     { ESIS_OPTION_ES_CONF_TIME,    "ES Configuration Time" },
202     { ESIS_OPTION_PRIORITY,        "Priority" },
203     { ESIS_OPTION_ADDRESS_MASK,    "Addressk Mask" },
204     { ESIS_OPTION_SNPA_MASK,       "SNPA Mask" },
205     { 0, NULL }
206 };
207
208 #define CLNP_OPTION_DISCARD_REASON   193
209 #define CLNP_OPTION_QOS_MAINTENANCE  195 /* iso8473 */
210 #define CLNP_OPTION_SECURITY         197 /* iso8473 */
211 #define CLNP_OPTION_SOURCE_ROUTING   200 /* iso8473 */
212 #define CLNP_OPTION_ROUTE_RECORDING  203 /* iso8473 */
213 #define CLNP_OPTION_PADDING          204 /* iso8473 */
214 #define CLNP_OPTION_PRIORITY         205 /* iso8473 */
215
216 static struct tok clnp_option_values[] = {
217     { CLNP_OPTION_DISCARD_REASON,  "Discard Reason"},
218     { CLNP_OPTION_PRIORITY,        "Priority"},
219     { CLNP_OPTION_QOS_MAINTENANCE, "QoS Maintenance"},
220     { CLNP_OPTION_SECURITY, "Security"},
221     { CLNP_OPTION_SOURCE_ROUTING, "Source Routing"},
222     { CLNP_OPTION_ROUTE_RECORDING, "Route Recording"},
223     { CLNP_OPTION_PADDING, "Padding"},
224     { 0, NULL }
225 };
226
227 static struct tok clnp_option_rfd_class_values[] = {
228     { 0x0, "General"},
229     { 0x8, "Address"},
230     { 0x9, "Source Routeing"},
231     { 0xa, "Lifetime"},
232     { 0xb, "PDU Discarded"},
233     { 0xc, "Reassembly"},
234     { 0, NULL }
235 };
236
237 static struct tok clnp_option_rfd_general_values[] = {
238     { 0x0, "Reason not specified"},
239     { 0x1, "Protocol procedure error"},
240     { 0x2, "Incorrect checksum"},
241     { 0x3, "PDU discarded due to congestion"},
242     { 0x4, "Header syntax error (cannot be parsed)"},
243     { 0x5, "Segmentation needed but not permitted"},
244     { 0x6, "Incomplete PDU received"},
245     { 0x7, "Duplicate option"},
246     { 0, NULL }
247 };
248
249 static struct tok clnp_option_rfd_address_values[] = {
250     { 0x0, "Destination address unreachable"},
251     { 0x1, "Destination address unknown"},
252     { 0, NULL }
253 };
254
255 static struct tok clnp_option_rfd_source_routeing_values[] = {
256     { 0x0, "Unspecified source routeing error"},
257     { 0x1, "Syntax error in source routeing field"},
258     { 0x2, "Unknown address in source routeing field"},
259     { 0x3, "Path not acceptable"},
260     { 0, NULL }
261 };
262
263 static struct tok clnp_option_rfd_lifetime_values[] = {
264     { 0x0, "Lifetime expired while data unit in transit"},
265     { 0x1, "Lifetime expired during reassembly"},
266     { 0, NULL }
267 };
268
269 static struct tok clnp_option_rfd_pdu_discard_values[] = {
270     { 0x0, "Unsupported option not specified"},
271     { 0x1, "Unsupported protocol version"},
272     { 0x2, "Unsupported security option"},
273     { 0x3, "Unsupported source routeing option"},
274     { 0x4, "Unsupported recording of route option"},
275     { 0, NULL }
276 };
277
278 static struct tok clnp_option_rfd_reassembly_values[] = {
279     { 0x0, "Reassembly interference"},
280     { 0, NULL }
281 };
282
283 /* array of 16 error-classes */
284 static struct tok *clnp_option_rfd_error_class[] = {
285     clnp_option_rfd_general_values,
286     NULL,
287     NULL,
288     NULL,
289     NULL,
290     NULL,
291     NULL,
292     NULL,
293     clnp_option_rfd_address_values,
294     clnp_option_rfd_source_routeing_values,
295     clnp_option_rfd_lifetime_values,
296     clnp_option_rfd_pdu_discard_values,
297     clnp_option_rfd_reassembly_values,
298     NULL,
299     NULL,
300     NULL
301 };
302
303 #define CLNP_OPTION_OPTION_QOS_MASK 0x3f
304 #define CLNP_OPTION_SCOPE_MASK      0xc0
305 #define CLNP_OPTION_SCOPE_SA_SPEC   0x40
306 #define CLNP_OPTION_SCOPE_DA_SPEC   0x80
307 #define CLNP_OPTION_SCOPE_GLOBAL    0xc0
308
309 static struct tok clnp_option_scope_values[] = {
310     { CLNP_OPTION_SCOPE_SA_SPEC, "Source Address Specific"},
311     { CLNP_OPTION_SCOPE_DA_SPEC, "Destination Address Specific"},
312     { CLNP_OPTION_SCOPE_GLOBAL, "Globally unique"},
313     { 0, NULL }
314 };
315
316 static struct tok clnp_option_sr_rr_values[] = {
317     { 0x0, "partial"},
318     { 0x1, "complete"},
319     { 0, NULL }
320 };
321
322 static struct tok clnp_option_sr_rr_string_values[] = {
323     { CLNP_OPTION_SOURCE_ROUTING, "source routing"},
324     { CLNP_OPTION_ROUTE_RECORDING, "recording of route in progress"},
325     { 0, NULL }
326 };
327
328 static struct tok clnp_option_qos_global_values[] = {
329     { 0x20, "reserved"},
330     { 0x10, "sequencing vs. delay"},
331     { 0x08, "congested"},
332     { 0x04, "delay vs. cost"},
333     { 0x02, "error vs. delay"},
334     { 0x01, "error vs. cost"},
335     { 0, NULL }
336 };
337
338 #define ISIS_SUBTLV_EXT_IS_REACH_ADMIN_GROUP           3 /* draft-ietf-isis-traffic-05 */
339 #define ISIS_SUBTLV_EXT_IS_REACH_LINK_LOCAL_REMOTE_ID  4 /* rfc4205 */
340 #define ISIS_SUBTLV_EXT_IS_REACH_LINK_REMOTE_ID        5 /* draft-ietf-isis-traffic-05 */
341 #define ISIS_SUBTLV_EXT_IS_REACH_IPV4_INTF_ADDR        6 /* draft-ietf-isis-traffic-05 */
342 #define ISIS_SUBTLV_EXT_IS_REACH_IPV4_NEIGHBOR_ADDR    8 /* draft-ietf-isis-traffic-05 */
343 #define ISIS_SUBTLV_EXT_IS_REACH_MAX_LINK_BW           9 /* draft-ietf-isis-traffic-05 */
344 #define ISIS_SUBTLV_EXT_IS_REACH_RESERVABLE_BW        10 /* draft-ietf-isis-traffic-05 */
345 #define ISIS_SUBTLV_EXT_IS_REACH_UNRESERVED_BW        11 /* rfc4124 */
346 #define ISIS_SUBTLV_EXT_IS_REACH_BW_CONSTRAINTS_OLD   12 /* draft-ietf-tewg-diff-te-proto-06 */
347 #define ISIS_SUBTLV_EXT_IS_REACH_TE_METRIC            18 /* draft-ietf-isis-traffic-05 */
348 #define ISIS_SUBTLV_EXT_IS_REACH_LINK_ATTRIBUTE       19 /* draft-ietf-isis-link-attr-01 */
349 #define ISIS_SUBTLV_EXT_IS_REACH_LINK_PROTECTION_TYPE 20 /* rfc4205 */
350 #define ISIS_SUBTLV_EXT_IS_REACH_INTF_SW_CAP_DESCR    21 /* rfc4205 */
351 #define ISIS_SUBTLV_EXT_IS_REACH_BW_CONSTRAINTS       22 /* rfc4124 */
352
353 static struct tok isis_ext_is_reach_subtlv_values[] = {
354     { ISIS_SUBTLV_EXT_IS_REACH_ADMIN_GROUP,            "Administrative groups" },
355     { ISIS_SUBTLV_EXT_IS_REACH_LINK_LOCAL_REMOTE_ID,   "Link Local/Remote Identifier" },
356     { ISIS_SUBTLV_EXT_IS_REACH_LINK_REMOTE_ID,         "Link Remote Identifier" },
357     { ISIS_SUBTLV_EXT_IS_REACH_IPV4_INTF_ADDR,         "IPv4 interface address" },
358     { ISIS_SUBTLV_EXT_IS_REACH_IPV4_NEIGHBOR_ADDR,     "IPv4 neighbor address" },
359     { ISIS_SUBTLV_EXT_IS_REACH_MAX_LINK_BW,            "Maximum link bandwidth" },
360     { ISIS_SUBTLV_EXT_IS_REACH_RESERVABLE_BW,          "Reservable link bandwidth" },
361     { ISIS_SUBTLV_EXT_IS_REACH_UNRESERVED_BW,          "Unreserved bandwidth" },
362     { ISIS_SUBTLV_EXT_IS_REACH_TE_METRIC,              "Traffic Engineering Metric" },
363     { ISIS_SUBTLV_EXT_IS_REACH_LINK_ATTRIBUTE,         "Link Attribute" },
364     { ISIS_SUBTLV_EXT_IS_REACH_LINK_PROTECTION_TYPE,   "Link Protection Type" },
365     { ISIS_SUBTLV_EXT_IS_REACH_INTF_SW_CAP_DESCR,      "Interface Switching Capability" },
366     { ISIS_SUBTLV_EXT_IS_REACH_BW_CONSTRAINTS_OLD,     "Bandwidth Constraints (old)" },
367     { ISIS_SUBTLV_EXT_IS_REACH_BW_CONSTRAINTS,         "Bandwidth Constraints" },
368     { 250,                                             "Reserved for cisco specific extensions" },
369     { 251,                                             "Reserved for cisco specific extensions" },
370     { 252,                                             "Reserved for cisco specific extensions" },
371     { 253,                                             "Reserved for cisco specific extensions" },
372     { 254,                                             "Reserved for cisco specific extensions" },
373     { 255,                                             "Reserved for future expansion" },
374     { 0, NULL }
375 };
376
377 #define ISIS_SUBTLV_EXTD_IP_REACH_ADMIN_TAG32          1 /* draft-ietf-isis-admin-tags-01 */
378 #define ISIS_SUBTLV_EXTD_IP_REACH_ADMIN_TAG64          2 /* draft-ietf-isis-admin-tags-01 */
379 #define ISIS_SUBTLV_EXTD_IP_REACH_MGMT_PREFIX_COLOR  117 /* draft-ietf-isis-wg-multi-topology-05 */
380
381 static struct tok isis_ext_ip_reach_subtlv_values[] = {
382     { ISIS_SUBTLV_EXTD_IP_REACH_ADMIN_TAG32,           "32-Bit Administrative tag" },
383     { ISIS_SUBTLV_EXTD_IP_REACH_ADMIN_TAG64,           "64-Bit Administrative tag" },
384     { ISIS_SUBTLV_EXTD_IP_REACH_MGMT_PREFIX_COLOR,     "Management Prefix Color" },
385     { 0, NULL }
386 };
387
388 static struct tok isis_subtlv_link_attribute_values[] = {
389     { 0x01, "Local Protection Available" },
390     { 0x02, "Link excluded from local protection path" },
391     { 0x04, "Local maintenance required"},
392     { 0, NULL }
393 };
394
395 #define ISIS_SUBTLV_AUTH_SIMPLE        1
396 #define ISIS_SUBTLV_AUTH_GENERIC       3 /* rfc 5310 */
397 #define ISIS_SUBTLV_AUTH_MD5          54
398 #define ISIS_SUBTLV_AUTH_MD5_LEN      16
399 #define ISIS_SUBTLV_AUTH_PRIVATE     255
400
401 static struct tok isis_subtlv_auth_values[] = {
402     { ISIS_SUBTLV_AUTH_SIMPLE,  "simple text password"},
403     { ISIS_SUBTLV_AUTH_GENERIC, "Generic Crypto key-id"},
404     { ISIS_SUBTLV_AUTH_MD5,     "HMAC-MD5 password"},
405     { ISIS_SUBTLV_AUTH_PRIVATE, "Routing Domain private password"},
406     { 0, NULL }
407 };
408
409 #define ISIS_SUBTLV_IDRP_RES           0
410 #define ISIS_SUBTLV_IDRP_LOCAL         1
411 #define ISIS_SUBTLV_IDRP_ASN           2
412
413 static struct tok isis_subtlv_idrp_values[] = {
414     { ISIS_SUBTLV_IDRP_RES,         "Reserved"},
415     { ISIS_SUBTLV_IDRP_LOCAL,       "Routing-Domain Specific"},
416     { ISIS_SUBTLV_IDRP_ASN,         "AS Number Tag"},
417     { 0, NULL}
418 };
419
420 #define CLNP_SEGMENT_PART  0x80
421 #define CLNP_MORE_SEGMENTS 0x40
422 #define CLNP_REQUEST_ER    0x20
423
424 static struct tok clnp_flag_values[] = {
425     { CLNP_SEGMENT_PART, "Segmentation permitted"},
426     { CLNP_MORE_SEGMENTS, "more Segments"},
427     { CLNP_REQUEST_ER, "request Error Report"},
428     { 0, NULL}
429 };
430
431 #define ISIS_MASK_LSP_OL_BIT(x)            ((x)&0x4)
432 #define ISIS_MASK_LSP_ISTYPE_BITS(x)       ((x)&0x3)
433 #define ISIS_MASK_LSP_PARTITION_BIT(x)     ((x)&0x80)
434 #define ISIS_MASK_LSP_ATT_BITS(x)          ((x)&0x78)
435 #define ISIS_MASK_LSP_ATT_ERROR_BIT(x)     ((x)&0x40)
436 #define ISIS_MASK_LSP_ATT_EXPENSE_BIT(x)   ((x)&0x20)
437 #define ISIS_MASK_LSP_ATT_DELAY_BIT(x)     ((x)&0x10)
438 #define ISIS_MASK_LSP_ATT_DEFAULT_BIT(x)   ((x)&0x8)
439
440 #define ISIS_MASK_MTID(x)                  ((x)&0x0fff)
441 #define ISIS_MASK_MTFLAGS(x)               ((x)&0xf000)
442
443 static struct tok isis_mt_flag_values[] = {
444     { 0x4000,                  "ATT bit set"},
445     { 0x8000,                  "Overload bit set"},
446     { 0, NULL}
447 };
448
449 #define ISIS_MASK_TLV_EXTD_IP_UPDOWN(x)     ((x)&0x80)
450 #define ISIS_MASK_TLV_EXTD_IP_SUBTLV(x)     ((x)&0x40)
451
452 #define ISIS_MASK_TLV_EXTD_IP6_IE(x)        ((x)&0x40)
453 #define ISIS_MASK_TLV_EXTD_IP6_SUBTLV(x)    ((x)&0x20)
454
455 #define ISIS_LSP_TLV_METRIC_SUPPORTED(x)   ((x)&0x80)
456 #define ISIS_LSP_TLV_METRIC_IE(x)          ((x)&0x40)
457 #define ISIS_LSP_TLV_METRIC_UPDOWN(x)      ((x)&0x80)
458 #define ISIS_LSP_TLV_METRIC_VALUE(x)       ((x)&0x3f)
459
460 #define ISIS_MASK_TLV_SHARED_RISK_GROUP(x) ((x)&0x1)
461
462 static struct tok isis_mt_values[] = {
463     { 0,    "IPv4 unicast"},
464     { 1,    "In-Band Management"},
465     { 2,    "IPv6 unicast"},
466     { 3,    "Multicast"},
467     { 4095, "Development, Experimental or Proprietary"},
468     { 0, NULL }
469 };
470
471 static struct tok isis_iih_circuit_type_values[] = {
472     { 1,    "Level 1 only"},
473     { 2,    "Level 2 only"},
474     { 3,    "Level 1, Level 2"},
475     { 0, NULL}
476 };
477
478 #define ISIS_LSP_TYPE_UNUSED0   0
479 #define ISIS_LSP_TYPE_LEVEL_1   1
480 #define ISIS_LSP_TYPE_UNUSED2   2
481 #define ISIS_LSP_TYPE_LEVEL_2   3
482
483 static struct tok isis_lsp_istype_values[] = {
484     { ISIS_LSP_TYPE_UNUSED0,    "Unused 0x0 (invalid)"},
485     { ISIS_LSP_TYPE_LEVEL_1,    "L1 IS"},
486     { ISIS_LSP_TYPE_UNUSED2,    "Unused 0x2 (invalid)"},
487     { ISIS_LSP_TYPE_LEVEL_2,    "L2 IS"},
488     { 0, NULL }
489 };
490
491 /*
492  * Katz's point to point adjacency TLV uses codes to tell us the state of
493  * the remote adjacency.  Enumerate them.
494  */
495
496 #define ISIS_PTP_ADJ_UP   0
497 #define ISIS_PTP_ADJ_INIT 1
498 #define ISIS_PTP_ADJ_DOWN 2
499
500 static struct tok isis_ptp_adjancey_values[] = {
501     { ISIS_PTP_ADJ_UP,    "Up" },
502     { ISIS_PTP_ADJ_INIT,  "Initializing" },
503     { ISIS_PTP_ADJ_DOWN,  "Down" },
504     { 0, NULL}
505 };
506
507 struct isis_tlv_ptp_adj {
508     u_int8_t adjacency_state;
509     u_int8_t extd_local_circuit_id[4];
510     u_int8_t neighbor_sysid[SYSTEM_ID_LEN];
511     u_int8_t neighbor_extd_local_circuit_id[4];
512 };
513
514 static void osi_print_cksum(const u_int8_t *pptr, u_int16_t checksum,
515                             u_int checksum_offset, u_int length);
516 static int clnp_print(const u_int8_t *, u_int);
517 static void esis_print(const u_int8_t *, u_int);
518 static int isis_print(const u_int8_t *, u_int);
519
520 struct isis_metric_block {
521     u_int8_t metric_default;
522     u_int8_t metric_delay;
523     u_int8_t metric_expense;
524     u_int8_t metric_error;
525 };
526
527 struct isis_tlv_is_reach {
528     struct isis_metric_block isis_metric_block;
529     u_int8_t neighbor_nodeid[NODE_ID_LEN];
530 };
531
532 struct isis_tlv_es_reach {
533     struct isis_metric_block isis_metric_block;
534     u_int8_t neighbor_sysid[SYSTEM_ID_LEN];
535 };
536
537 struct isis_tlv_ip_reach {
538     struct isis_metric_block isis_metric_block;
539     u_int8_t prefix[4];
540     u_int8_t mask[4];
541 };
542
543 static struct tok isis_is_reach_virtual_values[] = {
544     { 0,    "IsNotVirtual"},
545     { 1,    "IsVirtual"},
546     { 0, NULL }
547 };
548
549 static struct tok isis_restart_flag_values[] = {
550     { 0x1,  "Restart Request"},
551     { 0x2,  "Restart Acknowledgement"},
552     { 0x4,  "Suppress adjacency advertisement"},
553     { 0, NULL }
554 };
555
556 struct isis_common_header {
557     u_int8_t nlpid;
558     u_int8_t fixed_len;
559     u_int8_t version;                   /* Protocol version */
560     u_int8_t id_length;
561     u_int8_t pdu_type;                  /* 3 MSbits are reserved */
562     u_int8_t pdu_version;               /* Packet format version */
563     u_int8_t reserved;
564     u_int8_t max_area;
565 };
566
567 struct isis_iih_lan_header {
568     u_int8_t circuit_type;
569     u_int8_t source_id[SYSTEM_ID_LEN];
570     u_int8_t holding_time[2];
571     u_int8_t pdu_len[2];
572     u_int8_t priority;
573     u_int8_t lan_id[NODE_ID_LEN];
574 };
575
576 struct isis_iih_ptp_header {
577     u_int8_t circuit_type;
578     u_int8_t source_id[SYSTEM_ID_LEN];
579     u_int8_t holding_time[2];
580     u_int8_t pdu_len[2];
581     u_int8_t circuit_id;
582 };
583
584 struct isis_lsp_header {
585     u_int8_t pdu_len[2];
586     u_int8_t remaining_lifetime[2];
587     u_int8_t lsp_id[LSP_ID_LEN];
588     u_int8_t sequence_number[4];
589     u_int8_t checksum[2];
590     u_int8_t typeblock;
591 };
592
593 struct isis_csnp_header {
594     u_int8_t pdu_len[2];
595     u_int8_t source_id[NODE_ID_LEN];
596     u_int8_t start_lsp_id[LSP_ID_LEN];
597     u_int8_t end_lsp_id[LSP_ID_LEN];
598 };
599
600 struct isis_psnp_header {
601     u_int8_t pdu_len[2];
602     u_int8_t source_id[NODE_ID_LEN];
603 };
604
605 struct isis_tlv_lsp {
606     u_int8_t remaining_lifetime[2];
607     u_int8_t lsp_id[LSP_ID_LEN];
608     u_int8_t sequence_number[4];
609     u_int8_t checksum[2];
610 };
611
612 #define ISIS_COMMON_HEADER_SIZE (sizeof(struct isis_common_header))
613 #define ISIS_IIH_LAN_HEADER_SIZE (sizeof(struct isis_iih_lan_header))
614 #define ISIS_IIH_PTP_HEADER_SIZE (sizeof(struct isis_iih_ptp_header))
615 #define ISIS_LSP_HEADER_SIZE (sizeof(struct isis_lsp_header))
616 #define ISIS_CSNP_HEADER_SIZE (sizeof(struct isis_csnp_header))
617 #define ISIS_PSNP_HEADER_SIZE (sizeof(struct isis_psnp_header))
618
619 void isoclns_print(const u_int8_t *p, u_int length, u_int caplen)
620 {
621         if (caplen <= 1) { /* enough bytes on the wire ? */
622             printf("|OSI");
623             return;
624         }
625
626         if (eflag)
627             printf("OSI NLPID %s (0x%02x): ",
628                    tok2str(nlpid_values,"Unknown",*p),
629                    *p);
630         
631         switch (*p) {
632
633         case NLPID_CLNP:
634                 if (!clnp_print(p, length))
635                         print_unknown_data(p,"\n\t",caplen);
636                 break;
637
638         case NLPID_ESIS:
639                 esis_print(p, length);
640                 return;
641
642         case NLPID_ISIS:
643                 if (!isis_print(p, length))
644                         print_unknown_data(p,"\n\t",caplen);
645                 break;
646
647         case NLPID_NULLNS:
648                 (void)printf("%slength: %u",
649                              eflag ? "" : ", ",
650                              length);
651                 break;
652
653         case NLPID_Q933:
654                 q933_print(p+1, length-1);
655                 break;
656
657         case NLPID_IP:
658                 ip_print(gndo, p+1, length-1);
659                 break;
660
661 #ifdef INET6
662         case NLPID_IP6:
663                 ip6_print(gndo, p+1, length-1);
664                 break;
665 #endif
666
667         case NLPID_PPP:
668                 ppp_print(p+1, length-1);
669                 break;
670
671         default:
672                 if (!eflag)
673                     printf("OSI NLPID 0x%02x unknown",*p);
674                 (void)printf("%slength: %u",
675                              eflag ? "" : ", ",
676                              length);
677                 if (caplen > 1)
678                         print_unknown_data(p,"\n\t",caplen);
679                 break;
680         }
681 }
682
683 #define CLNP_PDU_ER      1
684 #define CLNP_PDU_DT     28
685 #define CLNP_PDU_MD     29
686 #define CLNP_PDU_ERQ    30
687 #define CLNP_PDU_ERP    31
688
689 static struct tok clnp_pdu_values[] = {
690     { CLNP_PDU_ER,  "Error Report"},
691     { CLNP_PDU_MD,  "MD"},
692     { CLNP_PDU_DT,  "Data"},
693     { CLNP_PDU_ERQ, "Echo Request"},
694     { CLNP_PDU_ERP, "Echo Response"},
695     { 0, NULL }
696 };
697
698 struct clnp_header_t {
699     u_int8_t nlpid;
700     u_int8_t length_indicator;
701     u_int8_t version;
702     u_int8_t lifetime; /* units of 500ms */
703     u_int8_t type;
704     u_int8_t segment_length[2];
705     u_int8_t cksum[2];
706 };
707
708 struct clnp_segment_header_t {
709     u_int8_t data_unit_id[2];
710     u_int8_t segment_offset[2];
711     u_int8_t total_length[2];
712 };
713
714 /*
715  * clnp_print
716  * Decode CLNP packets.  Return 0 on error.
717  */
718
719 static int clnp_print (const u_int8_t *pptr, u_int length)
720 {
721         const u_int8_t *optr,*source_address,*dest_address;
722         u_int li,tlen,nsap_offset,source_address_length,dest_address_length, clnp_pdu_type, clnp_flags;
723         const struct clnp_header_t *clnp_header;
724         const struct clnp_segment_header_t *clnp_segment_header;
725         u_int8_t rfd_error_major,rfd_error_minor;
726
727         clnp_header = (const struct clnp_header_t *) pptr;
728         TCHECK(*clnp_header);
729
730         li = clnp_header->length_indicator;
731         optr = pptr;
732
733         if (!eflag)
734             printf("CLNP");
735
736         /*
737          * Sanity checking of the header.
738          */
739
740         if (clnp_header->version != CLNP_VERSION) {
741             printf("version %d packet not supported", clnp_header->version);
742             return (0);
743         }
744
745         /* FIXME further header sanity checking */
746
747         clnp_pdu_type = clnp_header->type & CLNP_PDU_TYPE_MASK;
748         clnp_flags = clnp_header->type & CLNP_FLAG_MASK;
749
750         pptr += sizeof(struct clnp_header_t);
751         li -= sizeof(struct clnp_header_t);
752         dest_address_length = *pptr;
753         dest_address = pptr + 1;
754
755         pptr += (1 + dest_address_length);
756         li -= (1 + dest_address_length);
757         source_address_length = *pptr;
758         source_address = pptr +1;
759
760         pptr += (1 + source_address_length);
761         li -= (1 + source_address_length);
762
763         if (vflag < 1) {
764             printf("%s%s > %s, %s, length %u",
765                    eflag ? "" : ", ",
766                    isonsap_string(source_address, source_address_length),
767                    isonsap_string(dest_address, dest_address_length),
768                    tok2str(clnp_pdu_values,"unknown (%u)",clnp_pdu_type),
769                    length);
770             return (1);
771         }
772         printf("%slength %u",eflag ? "" : ", ",length);
773
774         printf("\n\t%s PDU, hlen: %u, v: %u, lifetime: %u.%us, Segment PDU length: %u, checksum: 0x%04x",
775                tok2str(clnp_pdu_values, "unknown (%u)",clnp_pdu_type),
776                clnp_header->length_indicator,
777                clnp_header->version,
778                clnp_header->lifetime/2,
779                (clnp_header->lifetime%2)*5,
780                EXTRACT_16BITS(clnp_header->segment_length),
781                EXTRACT_16BITS(clnp_header->cksum));
782
783         osi_print_cksum(optr, EXTRACT_16BITS(clnp_header->cksum), 7,
784                         clnp_header->length_indicator);
785
786         printf("\n\tFlags [%s]",
787                bittok2str(clnp_flag_values,"none",clnp_flags));
788
789         printf("\n\tsource address (length %u): %s\n\tdest   address (length %u): %s",
790                source_address_length,
791                isonsap_string(source_address, source_address_length),
792                dest_address_length,
793                isonsap_string(dest_address,dest_address_length));
794
795         if (clnp_flags & CLNP_SEGMENT_PART) {
796                 clnp_segment_header = (const struct clnp_segment_header_t *) pptr;
797                 TCHECK(*clnp_segment_header);
798                 printf("\n\tData Unit ID: 0x%04x, Segment Offset: %u, Total PDU Length: %u",
799                        EXTRACT_16BITS(clnp_segment_header->data_unit_id),
800                        EXTRACT_16BITS(clnp_segment_header->segment_offset),
801                        EXTRACT_16BITS(clnp_segment_header->total_length));
802                 pptr+=sizeof(const struct clnp_segment_header_t);
803                 li-=sizeof(const struct clnp_segment_header_t);
804         }
805
806         /* now walk the options */
807         while (li >= 2) {
808             u_int op, opli;
809             const u_int8_t *tptr;
810             
811             TCHECK2(*pptr, 2);
812             if (li < 2) {
813                 printf(", bad opts/li");
814                 return (0);
815             }
816             op = *pptr++;
817             opli = *pptr++;
818             li -= 2;
819             TCHECK2(*pptr, opli);
820             if (opli > li) {
821                 printf(", opt (%d) too long", op);
822                 return (0);
823             }
824             li -= opli;
825             tptr = pptr;
826             tlen = opli;
827             
828             printf("\n\t  %s Option #%u, length %u, value: ",
829                    tok2str(clnp_option_values,"Unknown",op),
830                    op,
831                    opli);
832
833             switch (op) {
834
835
836             case CLNP_OPTION_ROUTE_RECORDING: /* those two options share the format */
837             case CLNP_OPTION_SOURCE_ROUTING:  
838                     printf("%s %s",
839                            tok2str(clnp_option_sr_rr_values,"Unknown",*tptr),
840                            tok2str(clnp_option_sr_rr_string_values,"Unknown Option %u",op));
841                     nsap_offset=*(tptr+1);
842                     if (nsap_offset == 0) {
843                             printf(" Bad NSAP offset (0)");
844                             break;
845                     }
846                     nsap_offset-=1; /* offset to nsap list */
847                     if (nsap_offset > tlen) {
848                             printf(" Bad NSAP offset (past end of option)");
849                             break;
850                     }
851                     tptr+=nsap_offset;
852                     tlen-=nsap_offset;
853                     while (tlen > 0) {
854                             source_address_length=*tptr;
855                             if (tlen < source_address_length+1) {
856                                     printf("\n\t    NSAP address goes past end of option");
857                                     break;
858                             }
859                             if (source_address_length > 0) {
860                                     source_address=(tptr+1);
861                                     TCHECK2(*source_address, source_address_length);
862                                     printf("\n\t    NSAP address (length %u): %s",
863                                            source_address_length,
864                                            isonsap_string(source_address, source_address_length));
865                             }
866                             tlen-=source_address_length+1;
867                     }
868                     break;
869
870             case CLNP_OPTION_PRIORITY:
871                     printf("0x%1x", *tptr&0x0f);
872                     break;
873
874             case CLNP_OPTION_QOS_MAINTENANCE:
875                     printf("\n\t    Format Code: %s",
876                            tok2str(clnp_option_scope_values,"Reserved",*tptr&CLNP_OPTION_SCOPE_MASK));
877
878                     if ((*tptr&CLNP_OPTION_SCOPE_MASK) == CLNP_OPTION_SCOPE_GLOBAL)
879                             printf("\n\t    QoS Flags [%s]",
880                                    bittok2str(clnp_option_qos_global_values,
881                                               "none",
882                                               *tptr&CLNP_OPTION_OPTION_QOS_MASK));
883                     break;
884
885             case CLNP_OPTION_SECURITY:
886                     printf("\n\t    Format Code: %s, Security-Level %u",
887                            tok2str(clnp_option_scope_values,"Reserved",*tptr&CLNP_OPTION_SCOPE_MASK),
888                            *(tptr+1));
889                     break;
890
891             case CLNP_OPTION_DISCARD_REASON:
892                 rfd_error_major = (*tptr&0xf0) >> 4;
893                 rfd_error_minor = *tptr&0x0f;
894                 printf("\n\t    Class: %s Error (0x%01x), %s (0x%01x)",
895                        tok2str(clnp_option_rfd_class_values,"Unknown",rfd_error_major),
896                        rfd_error_major,
897                        tok2str(clnp_option_rfd_error_class[rfd_error_major],"Unknown",rfd_error_minor),
898                        rfd_error_minor);
899                 break;
900
901             case CLNP_OPTION_PADDING:
902                     printf("padding data");
903                 break;
904
905                 /*
906                  * FIXME those are the defined Options that lack a decoder
907                  * you are welcome to contribute code ;-)
908                  */
909
910             default:
911                 print_unknown_data(tptr,"\n\t  ",opli);
912                 break;
913             }
914             if (vflag > 1)
915                 print_unknown_data(pptr,"\n\t  ",opli);
916             pptr += opli;
917         }
918
919         switch (clnp_pdu_type) {
920
921         case    CLNP_PDU_ER: /* fall through */
922         case    CLNP_PDU_ERP:
923             TCHECK(*pptr);
924             if (*(pptr) == NLPID_CLNP) {
925                 printf("\n\t-----original packet-----\n\t");
926                 /* FIXME recursion protection */
927                 clnp_print(pptr, length-clnp_header->length_indicator);
928                 break;
929             } 
930
931         case    CLNP_PDU_DT:
932         case    CLNP_PDU_MD:
933         case    CLNP_PDU_ERQ:
934             
935         default:
936             /* dump the PDU specific data */
937             if (length-(pptr-optr) > 0) {
938                 printf("\n\t  undecoded non-header data, length %u",length-clnp_header->length_indicator);
939                 print_unknown_data(pptr,"\n\t  ",length-(pptr-optr));
940             }
941         }
942
943         return (1);
944
945  trunc:
946     fputs("[|clnp]", stdout);
947     return (1);
948
949 }
950
951
952 #define ESIS_PDU_REDIRECT       6
953 #define ESIS_PDU_ESH            2
954 #define ESIS_PDU_ISH            4
955
956 static struct tok esis_pdu_values[] = {
957     { ESIS_PDU_REDIRECT, "redirect"},
958     { ESIS_PDU_ESH,      "ESH"},
959     { ESIS_PDU_ISH,      "ISH"},
960     { 0, NULL }
961 };
962
963 struct esis_header_t {
964         u_int8_t nlpid;
965         u_int8_t length_indicator;
966         u_int8_t version;
967         u_int8_t reserved;
968         u_int8_t type;
969         u_int8_t holdtime[2];
970         u_int8_t cksum[2];
971 };
972
973 static void
974 esis_print(const u_int8_t *pptr, u_int length)
975 {
976         const u_int8_t *optr;
977         u_int li,esis_pdu_type,source_address_length, source_address_number;
978         const struct esis_header_t *esis_header;
979
980         if (!eflag)
981             printf("ES-IS");
982
983         if (length <= 2) {
984                 if (qflag)
985                         printf("bad pkt!");
986                 else
987                         printf("no header at all!");
988                 return;
989         }
990
991         esis_header = (const struct esis_header_t *) pptr;
992         TCHECK(*esis_header);
993         li = esis_header->length_indicator;
994         optr = pptr;
995
996         /*
997          * Sanity checking of the header.
998          */
999
1000         if (esis_header->nlpid != NLPID_ESIS) {
1001             printf(" nlpid 0x%02x packet not supported", esis_header->nlpid);
1002             return;
1003         }
1004
1005         if (esis_header->version != ESIS_VERSION) {
1006             printf(" version %d packet not supported", esis_header->version);
1007             return;
1008         }
1009                 
1010         if (li > length) {
1011             printf(" length indicator(%d) > PDU size (%d)!", li, length);
1012             return;
1013         }
1014
1015         if (li < sizeof(struct esis_header_t) + 2) {
1016             printf(" length indicator < min PDU size %d:", li);
1017             while (--length != 0)
1018                 printf("%02X", *pptr++);
1019             return;
1020         }
1021
1022         esis_pdu_type = esis_header->type & ESIS_PDU_TYPE_MASK;
1023
1024         if (vflag < 1) {
1025             printf("%s%s, length %u",
1026                    eflag ? "" : ", ",
1027                    tok2str(esis_pdu_values,"unknown type (%u)",esis_pdu_type),
1028                    length);
1029             return;
1030         } else
1031             printf("%slength %u\n\t%s (%u)",
1032                    eflag ? "" : ", ",
1033                    length,
1034                    tok2str(esis_pdu_values,"unknown type: %u", esis_pdu_type),
1035                    esis_pdu_type);
1036
1037         printf(", v: %u%s", esis_header->version, esis_header->version == ESIS_VERSION ? "" : "unsupported" );
1038         printf(", checksum: 0x%04x", EXTRACT_16BITS(esis_header->cksum));
1039
1040         osi_print_cksum(pptr, EXTRACT_16BITS(esis_header->cksum), 7, li);
1041
1042         printf(", holding time: %us, length indicator: %u",EXTRACT_16BITS(esis_header->holdtime),li);
1043
1044         if (vflag > 1)
1045             print_unknown_data(optr,"\n\t",sizeof(struct esis_header_t));
1046
1047         pptr += sizeof(struct esis_header_t);
1048         li -= sizeof(struct esis_header_t);
1049
1050         switch (esis_pdu_type) {
1051         case ESIS_PDU_REDIRECT: {
1052                 const u_int8_t *dst, *snpa, *neta;
1053                 u_int dstl, snpal, netal;
1054
1055                 TCHECK(*pptr);
1056                 if (li < 1) {
1057                         printf(", bad redirect/li");
1058                         return;
1059                 }
1060                 dstl = *pptr;
1061                 pptr++;
1062                 li--;
1063                 TCHECK2(*pptr, dstl);
1064                 if (li < dstl) {
1065                         printf(", bad redirect/li");
1066                         return;
1067                 }
1068                 dst = pptr;
1069                 pptr += dstl;
1070                 li -= dstl;
1071                 printf("\n\t  %s", isonsap_string(dst,dstl));
1072
1073                 TCHECK(*pptr);
1074                 if (li < 1) {
1075                         printf(", bad redirect/li");
1076                         return;
1077                 }
1078                 snpal = *pptr;
1079                 pptr++;
1080                 li--;
1081                 TCHECK2(*pptr, snpal);
1082                 if (li < snpal) {
1083                         printf(", bad redirect/li");
1084                         return;
1085                 }
1086                 snpa = pptr;
1087                 pptr += snpal;
1088                 li -= snpal;
1089                 TCHECK(*pptr);
1090                 if (li < 1) {
1091                         printf(", bad redirect/li");
1092                         return;
1093                 }
1094                 netal = *pptr;
1095                 pptr++;
1096                 TCHECK2(*pptr, netal);
1097                 if (li < netal) {
1098                         printf(", bad redirect/li");
1099                         return;
1100                 }
1101                 neta = pptr;
1102                 pptr += netal;
1103                 li -= netal;
1104
1105                 if (netal == 0)
1106                         printf("\n\t  %s", etheraddr_string(snpa));
1107                 else
1108                         printf("\n\t  %s", isonsap_string(neta,netal));
1109                 break;
1110         }
1111
1112         case ESIS_PDU_ESH:
1113             TCHECK(*pptr);
1114             if (li < 1) {
1115                 printf(", bad esh/li");
1116                 return;
1117             }
1118             source_address_number = *pptr;
1119             pptr++;
1120             li--;
1121
1122             printf("\n\t  Number of Source Addresses: %u", source_address_number);
1123            
1124             while (source_address_number > 0) {
1125                 TCHECK(*pptr);
1126                 if (li < 1) {
1127                     printf(", bad esh/li");
1128                     return;
1129                 }
1130                 source_address_length = *pptr;
1131                 pptr++;
1132                 li--;
1133
1134                 TCHECK2(*pptr, source_address_length);
1135                 if (li < source_address_length) {
1136                     printf(", bad esh/li");
1137                     return;
1138                 }
1139                 printf("\n\t  NET (length: %u): %s",
1140                        source_address_length,
1141                        isonsap_string(pptr,source_address_length));
1142                 pptr += source_address_length;
1143                 li -= source_address_length;
1144                 source_address_number--;
1145             }
1146
1147             break;
1148
1149         case ESIS_PDU_ISH: {
1150             TCHECK(*pptr);
1151             if (li < 1) {
1152                 printf(", bad ish/li");
1153                 return;
1154             }
1155             source_address_length = *pptr;
1156             pptr++;
1157             li--;
1158             TCHECK2(*pptr, source_address_length);
1159             if (li < source_address_length) {
1160                 printf(", bad ish/li");
1161                 return;
1162             }
1163             printf("\n\t  NET (length: %u): %s", source_address_length, isonsap_string(pptr, source_address_length));
1164             pptr += source_address_length;
1165             li -= source_address_length;
1166             break;
1167         }
1168
1169         default:
1170             if (vflag <= 1) {
1171                     if (pptr < snapend) 
1172                             print_unknown_data(pptr,"\n\t  ",snapend-pptr);
1173             }
1174             return;
1175         }
1176
1177         /* now walk the options */
1178         while (li != 0) {
1179             u_int op, opli;
1180             const u_int8_t *tptr;
1181             
1182             if (li < 2) {
1183                 printf(", bad opts/li");
1184                 return;
1185             }
1186             TCHECK2(*pptr, 2);
1187             op = *pptr++;
1188             opli = *pptr++;
1189             li -= 2;
1190             if (opli > li) {
1191                 printf(", opt (%d) too long", op);
1192                 return;
1193             }
1194             li -= opli;
1195             tptr = pptr;
1196             
1197             printf("\n\t  %s Option #%u, length %u, value: ",
1198                    tok2str(esis_option_values,"Unknown",op),
1199                    op,
1200                    opli);
1201
1202             switch (op) {
1203
1204             case ESIS_OPTION_ES_CONF_TIME:
1205                 if (opli == 2) {
1206                     TCHECK2(*pptr, 2);
1207                     printf("%us", EXTRACT_16BITS(tptr));
1208                 } else
1209                     printf("(bad length)");
1210                 break;
1211
1212             case ESIS_OPTION_PROTOCOLS:
1213                 while (opli>0) {
1214                     TCHECK(*pptr);
1215                     printf("%s (0x%02x)",
1216                            tok2str(nlpid_values,
1217                                    "unknown",
1218                                    *tptr),
1219                            *tptr);
1220                     if (opli>1) /* further NPLIDs ? - put comma */
1221                         printf(", ");
1222                     tptr++;
1223                     opli--;
1224                 }
1225                 break;
1226
1227                 /*
1228                  * FIXME those are the defined Options that lack a decoder
1229                  * you are welcome to contribute code ;-)
1230                  */
1231
1232             case ESIS_OPTION_QOS_MAINTENANCE:
1233             case ESIS_OPTION_SECURITY:
1234             case ESIS_OPTION_PRIORITY:
1235             case ESIS_OPTION_ADDRESS_MASK:
1236             case ESIS_OPTION_SNPA_MASK:
1237
1238             default:
1239                 print_unknown_data(tptr,"\n\t  ",opli);
1240                 break;
1241             }
1242             if (vflag > 1)
1243                 print_unknown_data(pptr,"\n\t  ",opli);
1244             pptr += opli;
1245         }
1246 trunc:
1247         return;
1248 }   
1249
1250 /* shared routine for printing system, node and lsp-ids */
1251 static char *
1252 isis_print_id(const u_int8_t *cp, int id_len)
1253 {
1254     int i;
1255     static char id[sizeof("xxxx.xxxx.xxxx.yy-zz")];
1256     char *pos = id;
1257
1258     for (i = 1; i <= SYSTEM_ID_LEN; i++) {
1259         snprintf(pos, sizeof(id) - (pos - id), "%02x", *cp++);
1260         pos += strlen(pos);
1261         if (i == 2 || i == 4)
1262             *pos++ = '.';
1263         }
1264     if (id_len >= NODE_ID_LEN) {
1265         snprintf(pos, sizeof(id) - (pos - id), ".%02x", *cp++);
1266         pos += strlen(pos);
1267     }
1268     if (id_len == LSP_ID_LEN)
1269         snprintf(pos, sizeof(id) - (pos - id), "-%02x", *cp);
1270     return (id);
1271 }
1272
1273 /* print the 4-byte metric block which is common found in the old-style TLVs */
1274 static int
1275 isis_print_metric_block (const struct isis_metric_block *isis_metric_block)
1276 {
1277     printf(", Default Metric: %d, %s",
1278            ISIS_LSP_TLV_METRIC_VALUE(isis_metric_block->metric_default),
1279            ISIS_LSP_TLV_METRIC_IE(isis_metric_block->metric_default) ? "External" : "Internal");
1280     if (!ISIS_LSP_TLV_METRIC_SUPPORTED(isis_metric_block->metric_delay))
1281         printf("\n\t\t  Delay Metric: %d, %s",
1282                ISIS_LSP_TLV_METRIC_VALUE(isis_metric_block->metric_delay),
1283                ISIS_LSP_TLV_METRIC_IE(isis_metric_block->metric_delay) ? "External" : "Internal");
1284     if (!ISIS_LSP_TLV_METRIC_SUPPORTED(isis_metric_block->metric_expense))
1285         printf("\n\t\t  Expense Metric: %d, %s",
1286                ISIS_LSP_TLV_METRIC_VALUE(isis_metric_block->metric_expense),
1287                ISIS_LSP_TLV_METRIC_IE(isis_metric_block->metric_expense) ? "External" : "Internal");
1288     if (!ISIS_LSP_TLV_METRIC_SUPPORTED(isis_metric_block->metric_error))
1289         printf("\n\t\t  Error Metric: %d, %s",
1290                ISIS_LSP_TLV_METRIC_VALUE(isis_metric_block->metric_error),
1291                ISIS_LSP_TLV_METRIC_IE(isis_metric_block->metric_error) ? "External" : "Internal");
1292
1293     return(1); /* everything is ok */
1294 }
1295
1296 static int
1297 isis_print_tlv_ip_reach (const u_int8_t *cp, const char *ident, int length)
1298 {
1299         int prefix_len;
1300         const struct isis_tlv_ip_reach *tlv_ip_reach;
1301
1302         tlv_ip_reach = (const struct isis_tlv_ip_reach *)cp;
1303
1304         while (length > 0) {
1305                 if ((size_t)length < sizeof(*tlv_ip_reach)) {
1306                         printf("short IPv4 Reachability (%d vs %lu)",
1307                                length,
1308                                (unsigned long)sizeof(*tlv_ip_reach));
1309                         return (0);
1310                 }
1311
1312                 if (!TTEST(*tlv_ip_reach))
1313                     return (0);
1314
1315                 prefix_len = mask2plen(EXTRACT_32BITS(tlv_ip_reach->mask));
1316
1317                 if (prefix_len == -1)
1318                         printf("%sIPv4 prefix: %s mask %s",
1319                                ident,
1320                                ipaddr_string((tlv_ip_reach->prefix)),
1321                                ipaddr_string((tlv_ip_reach->mask)));
1322                 else
1323                         printf("%sIPv4 prefix: %15s/%u",
1324                                ident,
1325                                ipaddr_string((tlv_ip_reach->prefix)),
1326                                prefix_len);
1327
1328                 printf(", Distribution: %s, Metric: %u, %s",
1329                        ISIS_LSP_TLV_METRIC_UPDOWN(tlv_ip_reach->isis_metric_block.metric_default) ? "down" : "up",
1330                        ISIS_LSP_TLV_METRIC_VALUE(tlv_ip_reach->isis_metric_block.metric_default),
1331                        ISIS_LSP_TLV_METRIC_IE(tlv_ip_reach->isis_metric_block.metric_default) ? "External" : "Internal");
1332
1333                 if (!ISIS_LSP_TLV_METRIC_SUPPORTED(tlv_ip_reach->isis_metric_block.metric_delay))
1334                     printf("%s  Delay Metric: %u, %s",
1335                            ident,
1336                            ISIS_LSP_TLV_METRIC_VALUE(tlv_ip_reach->isis_metric_block.metric_delay),
1337                            ISIS_LSP_TLV_METRIC_IE(tlv_ip_reach->isis_metric_block.metric_delay) ? "External" : "Internal");
1338                 
1339                 if (!ISIS_LSP_TLV_METRIC_SUPPORTED(tlv_ip_reach->isis_metric_block.metric_expense))
1340                     printf("%s  Expense Metric: %u, %s",
1341                            ident,
1342                            ISIS_LSP_TLV_METRIC_VALUE(tlv_ip_reach->isis_metric_block.metric_expense),
1343                            ISIS_LSP_TLV_METRIC_IE(tlv_ip_reach->isis_metric_block.metric_expense) ? "External" : "Internal");
1344                 
1345                 if (!ISIS_LSP_TLV_METRIC_SUPPORTED(tlv_ip_reach->isis_metric_block.metric_error))
1346                     printf("%s  Error Metric: %u, %s",
1347                            ident,
1348                            ISIS_LSP_TLV_METRIC_VALUE(tlv_ip_reach->isis_metric_block.metric_error),
1349                            ISIS_LSP_TLV_METRIC_IE(tlv_ip_reach->isis_metric_block.metric_error) ? "External" : "Internal");
1350
1351                 length -= sizeof(struct isis_tlv_ip_reach);
1352                 tlv_ip_reach++;
1353         }
1354         return (1);
1355 }
1356
1357 /*
1358  * this is the common IP-REACH subTLV decoder it is called
1359  * from various EXTD-IP REACH TLVs (135,235,236,237)
1360  */
1361
1362 static int
1363 isis_print_ip_reach_subtlv (const u_int8_t *tptr,int subt,int subl,const char *ident) {
1364
1365         /* first lets see if we know the subTLVs name*/
1366         printf("%s%s subTLV #%u, length: %u",
1367                ident,
1368                tok2str(isis_ext_ip_reach_subtlv_values,
1369                        "unknown",
1370                        subt),
1371                subt,
1372                subl);
1373
1374         if (!TTEST2(*tptr,subl))
1375             goto trunctlv;
1376
1377     switch(subt) {
1378     case ISIS_SUBTLV_EXTD_IP_REACH_MGMT_PREFIX_COLOR: /* fall through */
1379     case ISIS_SUBTLV_EXTD_IP_REACH_ADMIN_TAG32:
1380         while (subl >= 4) {
1381             printf(", 0x%08x (=%u)",
1382                    EXTRACT_32BITS(tptr),
1383                    EXTRACT_32BITS(tptr));
1384             tptr+=4;
1385             subl-=4;
1386         }
1387         break;
1388     case ISIS_SUBTLV_EXTD_IP_REACH_ADMIN_TAG64:
1389         while (subl >= 8) {
1390             printf(", 0x%08x%08x",
1391                    EXTRACT_32BITS(tptr),
1392                    EXTRACT_32BITS(tptr+4));
1393             tptr+=8;
1394             subl-=8;
1395         }
1396         break;
1397     default:
1398         if(!print_unknown_data(tptr,"\n\t\t    ",
1399                                subl))
1400           return(0);
1401         break;
1402     }
1403     return(1);
1404         
1405 trunctlv:
1406     printf("%spacket exceeded snapshot",ident);
1407     return(0);
1408 }
1409
1410 /*
1411  * this is the common IS-REACH subTLV decoder it is called
1412  * from isis_print_ext_is_reach()
1413  */
1414
1415 static int
1416 isis_print_is_reach_subtlv (const u_int8_t *tptr,u_int subt,u_int subl,const char *ident) {
1417
1418         u_int te_class,priority_level,gmpls_switch_cap;
1419         union { /* int to float conversion buffer for several subTLVs */
1420             float f; 
1421             u_int32_t i;
1422         } bw;
1423
1424         /* first lets see if we know the subTLVs name*/
1425         printf("%s%s subTLV #%u, length: %u",
1426                ident,
1427                tok2str(isis_ext_is_reach_subtlv_values,
1428                        "unknown",
1429                        subt),
1430                subt,
1431                subl);
1432
1433         if (!TTEST2(*tptr,subl))
1434             goto trunctlv;
1435
1436         switch(subt) {
1437         case ISIS_SUBTLV_EXT_IS_REACH_ADMIN_GROUP:      
1438         case ISIS_SUBTLV_EXT_IS_REACH_LINK_LOCAL_REMOTE_ID:
1439         case ISIS_SUBTLV_EXT_IS_REACH_LINK_REMOTE_ID:
1440             if (subl >= 4) {
1441               printf(", 0x%08x", EXTRACT_32BITS(tptr));
1442               if (subl == 8) /* rfc4205 */
1443                 printf(", 0x%08x", EXTRACT_32BITS(tptr+4));
1444             }
1445             break;
1446         case ISIS_SUBTLV_EXT_IS_REACH_IPV4_INTF_ADDR:
1447         case ISIS_SUBTLV_EXT_IS_REACH_IPV4_NEIGHBOR_ADDR:
1448             if (subl >= sizeof(struct in_addr))
1449               printf(", %s", ipaddr_string(tptr));
1450             break;
1451         case ISIS_SUBTLV_EXT_IS_REACH_MAX_LINK_BW :
1452         case ISIS_SUBTLV_EXT_IS_REACH_RESERVABLE_BW:  
1453             if (subl >= 4) {
1454               bw.i = EXTRACT_32BITS(tptr);
1455               printf(", %.3f Mbps", bw.f*8/1000000 );
1456             }
1457             break;
1458         case ISIS_SUBTLV_EXT_IS_REACH_UNRESERVED_BW :
1459             if (subl >= 32) {
1460               for (te_class = 0; te_class < 8; te_class++) {
1461                 bw.i = EXTRACT_32BITS(tptr);
1462                 printf("%s  TE-Class %u: %.3f Mbps",
1463                        ident,
1464                        te_class,
1465                        bw.f*8/1000000 );
1466                 tptr+=4;
1467               }
1468             }
1469             break;
1470         case ISIS_SUBTLV_EXT_IS_REACH_BW_CONSTRAINTS: /* fall through */
1471         case ISIS_SUBTLV_EXT_IS_REACH_BW_CONSTRAINTS_OLD:
1472             printf("%sBandwidth Constraints Model ID: %s (%u)",
1473                    ident,
1474                    tok2str(diffserv_te_bc_values, "unknown", *tptr),
1475                    *tptr);
1476             tptr++;
1477             /* decode BCs until the subTLV ends */
1478             for (te_class = 0; te_class < (subl-1)/4; te_class++) {
1479                 bw.i = EXTRACT_32BITS(tptr);
1480                 printf("%s  Bandwidth constraint CT%u: %.3f Mbps",
1481                        ident,
1482                        te_class,
1483                        bw.f*8/1000000 );
1484                 tptr+=4;
1485             }
1486             break;
1487         case ISIS_SUBTLV_EXT_IS_REACH_TE_METRIC:
1488             if (subl >= 3)
1489               printf(", %u", EXTRACT_24BITS(tptr));
1490             break;
1491         case ISIS_SUBTLV_EXT_IS_REACH_LINK_ATTRIBUTE:
1492             if (subl == 2) {
1493                printf(", [ %s ] (0x%04x)",
1494                       bittok2str(isis_subtlv_link_attribute_values,
1495                                  "Unknown",
1496                                  EXTRACT_16BITS(tptr)),
1497                       EXTRACT_16BITS(tptr));
1498             }
1499             break;
1500         case ISIS_SUBTLV_EXT_IS_REACH_LINK_PROTECTION_TYPE:
1501             if (subl >= 2) {
1502               printf(", %s, Priority %u",
1503                    bittok2str(gmpls_link_prot_values, "none", *tptr),
1504                    *(tptr+1));
1505             }
1506             break;
1507         case ISIS_SUBTLV_EXT_IS_REACH_INTF_SW_CAP_DESCR:
1508             if (subl >= 36) {
1509               gmpls_switch_cap = *tptr;
1510               printf("%s  Interface Switching Capability:%s",
1511                    ident,
1512                    tok2str(gmpls_switch_cap_values, "Unknown", gmpls_switch_cap));
1513               printf(", LSP Encoding: %s",
1514                    tok2str(gmpls_encoding_values, "Unknown", *(tptr+1)));
1515               tptr+=4;
1516               printf("%s  Max LSP Bandwidth:",ident);
1517               for (priority_level = 0; priority_level < 8; priority_level++) {
1518                 bw.i = EXTRACT_32BITS(tptr);
1519                 printf("%s    priority level %d: %.3f Mbps",
1520                        ident,
1521                        priority_level,
1522                        bw.f*8/1000000 );
1523                 tptr+=4;
1524               }
1525               subl-=36;
1526               switch (gmpls_switch_cap) {
1527               case GMPLS_PSC1:
1528               case GMPLS_PSC2:
1529               case GMPLS_PSC3:
1530               case GMPLS_PSC4:
1531                 bw.i = EXTRACT_32BITS(tptr);
1532                 printf("%s  Min LSP Bandwidth: %.3f Mbps", ident, bw.f*8/1000000);
1533                 printf("%s  Interface MTU: %u", ident, EXTRACT_16BITS(tptr+4));
1534                 break;
1535               case GMPLS_TSC:
1536                 bw.i = EXTRACT_32BITS(tptr);
1537                 printf("%s  Min LSP Bandwidth: %.3f Mbps", ident, bw.f*8/1000000);
1538                 printf("%s  Indication %s", ident,
1539                        tok2str(gmpls_switch_cap_tsc_indication_values, "Unknown (%u)", *(tptr+4)));
1540                 break;
1541               default:
1542                 /* there is some optional stuff left to decode but this is as of yet
1543                    not specified so just lets hexdump what is left */
1544                 if(subl>0){
1545                   if(!print_unknown_data(tptr,"\n\t\t    ",
1546                                          subl))
1547                     return(0);
1548                 }
1549               }
1550             }
1551             break;
1552         default:
1553             if(!print_unknown_data(tptr,"\n\t\t    ",
1554                                    subl))
1555                 return(0);
1556             break;
1557         }
1558         return(1);
1559
1560 trunctlv:
1561     printf("%spacket exceeded snapshot",ident);
1562     return(0);
1563 }
1564
1565
1566 /*
1567  * this is the common IS-REACH decoder it is called
1568  * from various EXTD-IS REACH style TLVs (22,24,222)
1569  */
1570
1571 static int
1572 isis_print_ext_is_reach (const u_int8_t *tptr,const char *ident, int tlv_type) {
1573
1574     char ident_buffer[20];
1575     int subtlv_type,subtlv_len,subtlv_sum_len;
1576     int proc_bytes = 0; /* how many bytes did we process ? */
1577     
1578     if (!TTEST2(*tptr, NODE_ID_LEN))
1579         return(0);
1580
1581     printf("%sIS Neighbor: %s", ident, isis_print_id(tptr, NODE_ID_LEN));
1582     tptr+=(NODE_ID_LEN);
1583
1584     if (tlv_type != ISIS_TLV_IS_ALIAS_ID) { /* the Alias TLV Metric field is implicit 0 */
1585         if (!TTEST2(*tptr, 3))    /* and is therefore skipped */
1586             return(0);
1587         printf(", Metric: %d",EXTRACT_24BITS(tptr));
1588         tptr+=3;
1589     }
1590         
1591     if (!TTEST2(*tptr, 1))
1592         return(0);
1593     subtlv_sum_len=*(tptr++); /* read out subTLV length */
1594     proc_bytes=NODE_ID_LEN+3+1;
1595     printf(", %ssub-TLVs present",subtlv_sum_len ? "" : "no ");
1596     if (subtlv_sum_len) {
1597         printf(" (%u)",subtlv_sum_len);
1598         while (subtlv_sum_len>0) {
1599             if (!TTEST2(*tptr,2))
1600                 return(0);
1601             subtlv_type=*(tptr++);
1602             subtlv_len=*(tptr++);
1603             /* prepend the ident string */
1604             snprintf(ident_buffer, sizeof(ident_buffer), "%s  ",ident);
1605             if(!isis_print_is_reach_subtlv(tptr,subtlv_type,subtlv_len,ident_buffer))
1606                 return(0);
1607             tptr+=subtlv_len;
1608             subtlv_sum_len-=(subtlv_len+2);
1609             proc_bytes+=(subtlv_len+2);
1610         }
1611     }
1612     return(proc_bytes);
1613 }
1614
1615 /*
1616  * this is the common Multi Topology ID decoder
1617  * it is called from various MT-TLVs (222,229,235,237)
1618  */
1619
1620 static int
1621 isis_print_mtid (const u_int8_t *tptr,const char *ident) {
1622     
1623     if (!TTEST2(*tptr, 2))
1624         return(0);
1625
1626     printf("%s%s",
1627            ident,
1628            tok2str(isis_mt_values,
1629                    "Reserved for IETF Consensus",
1630                    ISIS_MASK_MTID(EXTRACT_16BITS(tptr))));
1631
1632     printf(" Topology (0x%03x), Flags: [%s]",
1633            ISIS_MASK_MTID(EXTRACT_16BITS(tptr)),
1634            bittok2str(isis_mt_flag_values, "none",ISIS_MASK_MTFLAGS(EXTRACT_16BITS(tptr))));
1635
1636     return(2);
1637 }
1638
1639 /*
1640  * this is the common extended IP reach decoder
1641  * it is called from TLVs (135,235,236,237)
1642  * we process the TLV and optional subTLVs and return
1643  * the amount of processed bytes
1644  */
1645
1646 static int
1647 isis_print_extd_ip_reach (const u_int8_t *tptr, const char *ident, u_int16_t afi) {
1648
1649     char ident_buffer[20];
1650 #ifdef INET6
1651     u_int8_t prefix[sizeof(struct in6_addr)]; /* shared copy buffer for IPv4 and IPv6 prefixes */
1652 #else
1653     u_int8_t prefix[sizeof(struct in_addr)]; /* shared copy buffer for IPv4 prefixes */
1654 #endif
1655     u_int metric, status_byte, bit_length, byte_length, sublen, processed, subtlvtype, subtlvlen;
1656
1657     if (!TTEST2(*tptr, 4))
1658         return (0);
1659     metric = EXTRACT_32BITS(tptr);
1660     processed=4;
1661     tptr+=4;
1662     
1663     if (afi == AF_INET) {
1664         if (!TTEST2(*tptr, 1)) /* fetch status byte */
1665             return (0);
1666         status_byte=*(tptr++);
1667         bit_length = status_byte&0x3f;
1668         if (bit_length > 32) {
1669             printf("%sIPv4 prefix: bad bit length %u",
1670                    ident,
1671                    bit_length);
1672             return (0);
1673         }
1674         processed++;
1675 #ifdef INET6
1676     } else if (afi == AF_INET6) {
1677         if (!TTEST2(*tptr, 1)) /* fetch status & prefix_len byte */
1678             return (0);
1679         status_byte=*(tptr++);
1680         bit_length=*(tptr++);
1681         if (bit_length > 128) {
1682             printf("%sIPv6 prefix: bad bit length %u",
1683                    ident,
1684                    bit_length);
1685             return (0);
1686         }
1687         processed+=2;
1688 #endif
1689     } else
1690         return (0); /* somebody is fooling us */
1691
1692     byte_length = (bit_length + 7) / 8; /* prefix has variable length encoding */
1693    
1694     if (!TTEST2(*tptr, byte_length))
1695         return (0);
1696     memset(prefix, 0, sizeof prefix);   /* clear the copy buffer */
1697     memcpy(prefix,tptr,byte_length);    /* copy as much as is stored in the TLV */
1698     tptr+=byte_length;
1699     processed+=byte_length;
1700
1701     if (afi == AF_INET)
1702         printf("%sIPv4 prefix: %15s/%u",
1703                ident,
1704                ipaddr_string(prefix),
1705                bit_length);
1706 #ifdef INET6
1707     if (afi == AF_INET6)
1708         printf("%sIPv6 prefix: %s/%u",
1709                ident,
1710                ip6addr_string(prefix),
1711                bit_length);
1712 #endif 
1713    
1714     printf(", Distribution: %s, Metric: %u",
1715            ISIS_MASK_TLV_EXTD_IP_UPDOWN(status_byte) ? "down" : "up",
1716            metric);
1717
1718     if (afi == AF_INET && ISIS_MASK_TLV_EXTD_IP_SUBTLV(status_byte))
1719         printf(", sub-TLVs present");
1720 #ifdef INET6
1721     if (afi == AF_INET6)
1722         printf(", %s%s",
1723                ISIS_MASK_TLV_EXTD_IP6_IE(status_byte) ? "External" : "Internal",
1724                ISIS_MASK_TLV_EXTD_IP6_SUBTLV(status_byte) ? ", sub-TLVs present" : "");
1725 #endif
1726     
1727     if ((afi == AF_INET  && ISIS_MASK_TLV_EXTD_IP_SUBTLV(status_byte))
1728 #ifdef INET6
1729      || (afi == AF_INET6 && ISIS_MASK_TLV_EXTD_IP6_SUBTLV(status_byte))
1730 #endif
1731         ) {
1732         /* assume that one prefix can hold more
1733            than one subTLV - therefore the first byte must reflect
1734            the aggregate bytecount of the subTLVs for this prefix
1735         */
1736         if (!TTEST2(*tptr, 1))
1737             return (0);
1738         sublen=*(tptr++);
1739         processed+=sublen+1;
1740         printf(" (%u)",sublen);   /* print out subTLV length */
1741         
1742         while (sublen>0) {
1743             if (!TTEST2(*tptr,2))
1744                 return (0);
1745             subtlvtype=*(tptr++);
1746             subtlvlen=*(tptr++);
1747             /* prepend the ident string */
1748             snprintf(ident_buffer, sizeof(ident_buffer), "%s  ",ident);
1749             if(!isis_print_ip_reach_subtlv(tptr,subtlvtype,subtlvlen,ident_buffer))
1750                 return(0);
1751             tptr+=subtlvlen;
1752             sublen-=(subtlvlen+2);
1753         }
1754     }
1755     return (processed);
1756 }
1757
1758 /*
1759  * isis_print
1760  * Decode IS-IS packets.  Return 0 on error.
1761  */
1762
1763 static int isis_print (const u_int8_t *p, u_int length)
1764 {
1765     const struct isis_common_header *isis_header;
1766
1767     const struct isis_iih_lan_header *header_iih_lan;
1768     const struct isis_iih_ptp_header *header_iih_ptp;
1769     struct isis_lsp_header *header_lsp;
1770     const struct isis_csnp_header *header_csnp;
1771     const struct isis_psnp_header *header_psnp;
1772
1773     const struct isis_tlv_lsp *tlv_lsp;
1774     const struct isis_tlv_ptp_adj *tlv_ptp_adj;
1775     const struct isis_tlv_is_reach *tlv_is_reach;
1776     const struct isis_tlv_es_reach *tlv_es_reach;
1777
1778     u_int8_t pdu_type, max_area, id_length, tlv_type, tlv_len, tmp, alen, lan_alen, prefix_len;
1779     u_int8_t ext_is_len, ext_ip_len, mt_len;
1780     const u_int8_t *optr, *pptr, *tptr;
1781     u_short packet_len,pdu_len, key_id;
1782     u_int i,vendor_id;
1783     int sigcheck;
1784
1785     packet_len=length;
1786     optr = p; /* initialize the _o_riginal pointer to the packet start -
1787                  need it for parsing the checksum TLV and authentication
1788                  TLV verification */
1789     isis_header = (const struct isis_common_header *)p;
1790     TCHECK(*isis_header);
1791     pptr = p+(ISIS_COMMON_HEADER_SIZE);
1792     header_iih_lan = (const struct isis_iih_lan_header *)pptr;
1793     header_iih_ptp = (const struct isis_iih_ptp_header *)pptr;
1794     header_lsp = (struct isis_lsp_header *)pptr;
1795     header_csnp = (const struct isis_csnp_header *)pptr;
1796     header_psnp = (const struct isis_psnp_header *)pptr;
1797
1798     if (!eflag)
1799         printf("IS-IS");
1800
1801     /*
1802      * Sanity checking of the header.
1803      */
1804
1805     if (isis_header->version != ISIS_VERSION) {
1806         printf("version %d packet not supported", isis_header->version);
1807         return (0);
1808     }
1809
1810     if ((isis_header->id_length != SYSTEM_ID_LEN) && (isis_header->id_length != 0)) {
1811         printf("system ID length of %d is not supported",
1812                isis_header->id_length);
1813         return (0);
1814     }
1815
1816     if (isis_header->pdu_version != ISIS_VERSION) {
1817         printf("version %d packet not supported", isis_header->pdu_version);
1818         return (0);
1819     }
1820
1821     max_area = isis_header->max_area;
1822     switch(max_area) {
1823     case 0:
1824         max_area = 3;    /* silly shit */
1825         break;
1826     case 255:
1827         printf("bad packet -- 255 areas");
1828         return (0);
1829     default:
1830         break;
1831     }
1832
1833     id_length = isis_header->id_length;
1834     switch(id_length) {
1835     case 0:
1836         id_length = 6;   /* silly shit again */
1837         break;
1838     case 1:              /* 1-8 are valid sys-ID lenghts */
1839     case 2:
1840     case 3:
1841     case 4:
1842     case 5:
1843     case 6:
1844     case 7:
1845     case 8:
1846         break;
1847     case 255:
1848         id_length = 0;   /* entirely useless */
1849         break;
1850     default:
1851         break;
1852     }
1853
1854     /* toss any non 6-byte sys-ID len PDUs */
1855     if (id_length != 6 ) { 
1856         printf("bad packet -- illegal sys-ID length (%u)", id_length);
1857         return (0);
1858     }
1859
1860     pdu_type=isis_header->pdu_type;
1861
1862     /* in non-verbose mode print the basic PDU Type plus PDU specific brief information*/
1863     if (vflag < 1) {
1864         printf("%s%s",
1865                eflag ? "" : ", ",
1866                tok2str(isis_pdu_values,"unknown PDU-Type %u",pdu_type));
1867
1868         switch (pdu_type) {
1869
1870         case ISIS_PDU_L1_LAN_IIH:
1871         case ISIS_PDU_L2_LAN_IIH:
1872             printf(", src-id %s",
1873                    isis_print_id(header_iih_lan->source_id,SYSTEM_ID_LEN));
1874             printf(", lan-id %s, prio %u",
1875                    isis_print_id(header_iih_lan->lan_id,NODE_ID_LEN),
1876                    header_iih_lan->priority);
1877             break;
1878         case ISIS_PDU_PTP_IIH:
1879             printf(", src-id %s", isis_print_id(header_iih_ptp->source_id,SYSTEM_ID_LEN));
1880             break;
1881         case ISIS_PDU_L1_LSP:
1882         case ISIS_PDU_L2_LSP:
1883             printf(", lsp-id %s, seq 0x%08x, lifetime %5us",
1884                    isis_print_id(header_lsp->lsp_id, LSP_ID_LEN),
1885                    EXTRACT_32BITS(header_lsp->sequence_number),
1886                    EXTRACT_16BITS(header_lsp->remaining_lifetime));
1887             break;
1888         case ISIS_PDU_L1_CSNP:
1889         case ISIS_PDU_L2_CSNP:
1890             printf(", src-id %s", isis_print_id(header_csnp->source_id,NODE_ID_LEN));
1891             break;
1892         case ISIS_PDU_L1_PSNP:
1893         case ISIS_PDU_L2_PSNP:
1894             printf(", src-id %s", isis_print_id(header_psnp->source_id,NODE_ID_LEN));
1895             break;
1896
1897         }
1898         printf(", length %u", length);
1899
1900         return(1);
1901     }
1902
1903     /* ok they seem to want to know everything - lets fully decode it */
1904     printf("%slength %u", eflag ? "" : ", ",length);
1905
1906     printf("\n\t%s, hlen: %u, v: %u, pdu-v: %u, sys-id-len: %u (%u), max-area: %u (%u)",
1907            tok2str(isis_pdu_values,
1908                    "unknown, type %u",
1909                    pdu_type),
1910            isis_header->fixed_len,
1911            isis_header->version,
1912            isis_header->pdu_version,
1913            id_length,
1914            isis_header->id_length,
1915            max_area,
1916            isis_header->max_area);
1917
1918     if (vflag > 1) {
1919         if(!print_unknown_data(optr,"\n\t",8)) /* provide the _o_riginal pointer */
1920             return(0);                         /* for optionally debugging the common header */
1921     }
1922
1923     switch (pdu_type) {
1924
1925     case ISIS_PDU_L1_LAN_IIH:
1926     case ISIS_PDU_L2_LAN_IIH:
1927         if (isis_header->fixed_len != (ISIS_COMMON_HEADER_SIZE+ISIS_IIH_LAN_HEADER_SIZE)) {
1928             printf(", bogus fixed header length %u should be %lu",
1929                    isis_header->fixed_len, (unsigned long)ISIS_IIH_LAN_HEADER_SIZE);
1930             return (0);
1931         }
1932
1933         pdu_len=EXTRACT_16BITS(header_iih_lan->pdu_len);
1934         if (packet_len>pdu_len) {
1935             packet_len=pdu_len; /* do TLV decoding as long as it makes sense */
1936             length=pdu_len;
1937         }
1938
1939         TCHECK(*header_iih_lan);
1940         printf("\n\t  source-id: %s,  holding time: %us, Flags: [%s]",
1941                isis_print_id(header_iih_lan->source_id,SYSTEM_ID_LEN),
1942                EXTRACT_16BITS(header_iih_lan->holding_time),
1943                tok2str(isis_iih_circuit_type_values,
1944                        "unknown circuit type 0x%02x",
1945                        header_iih_lan->circuit_type));
1946
1947         printf("\n\t  lan-id:    %s, Priority: %u, PDU length: %u",
1948                isis_print_id(header_iih_lan->lan_id, NODE_ID_LEN),
1949                (header_iih_lan->priority) & ISIS_LAN_PRIORITY_MASK,
1950                pdu_len);
1951
1952         if (vflag > 1) {
1953             if(!print_unknown_data(pptr,"\n\t  ",ISIS_IIH_LAN_HEADER_SIZE))
1954                 return(0);
1955         }
1956
1957         packet_len -= (ISIS_COMMON_HEADER_SIZE+ISIS_IIH_LAN_HEADER_SIZE);
1958         pptr = p + (ISIS_COMMON_HEADER_SIZE+ISIS_IIH_LAN_HEADER_SIZE);
1959         break;
1960
1961     case ISIS_PDU_PTP_IIH:
1962         if (isis_header->fixed_len != (ISIS_COMMON_HEADER_SIZE+ISIS_IIH_PTP_HEADER_SIZE)) {
1963             printf(", bogus fixed header length %u should be %lu",
1964                    isis_header->fixed_len, (unsigned long)ISIS_IIH_PTP_HEADER_SIZE);
1965             return (0);
1966         }
1967
1968         pdu_len=EXTRACT_16BITS(header_iih_ptp->pdu_len);
1969         if (packet_len>pdu_len) {
1970             packet_len=pdu_len; /* do TLV decoding as long as it makes sense */
1971             length=pdu_len;
1972         }
1973
1974         TCHECK(*header_iih_ptp);
1975         printf("\n\t  source-id: %s, holding time: %us, Flags: [%s]",
1976                isis_print_id(header_iih_ptp->source_id,SYSTEM_ID_LEN),
1977                EXTRACT_16BITS(header_iih_ptp->holding_time),
1978                tok2str(isis_iih_circuit_type_values,
1979                        "unknown circuit type 0x%02x",
1980                        header_iih_ptp->circuit_type));
1981
1982         printf("\n\t  circuit-id: 0x%02x, PDU length: %u",
1983                header_iih_ptp->circuit_id,
1984                pdu_len);
1985
1986         if (vflag > 1) {
1987             if(!print_unknown_data(pptr,"\n\t  ",ISIS_IIH_PTP_HEADER_SIZE))
1988                 return(0);
1989         }
1990
1991         packet_len -= (ISIS_COMMON_HEADER_SIZE+ISIS_IIH_PTP_HEADER_SIZE);
1992         pptr = p + (ISIS_COMMON_HEADER_SIZE+ISIS_IIH_PTP_HEADER_SIZE);
1993         break;
1994
1995     case ISIS_PDU_L1_LSP:
1996     case ISIS_PDU_L2_LSP:
1997         if (isis_header->fixed_len != (ISIS_COMMON_HEADER_SIZE+ISIS_LSP_HEADER_SIZE)) {
1998             printf(", bogus fixed header length %u should be %lu",
1999                    isis_header->fixed_len, (unsigned long)ISIS_LSP_HEADER_SIZE);
2000             return (0);
2001         }
2002
2003         pdu_len=EXTRACT_16BITS(header_lsp->pdu_len);
2004         if (packet_len>pdu_len) {
2005             packet_len=pdu_len; /* do TLV decoding as long as it makes sense */
2006             length=pdu_len;
2007         }
2008
2009         TCHECK(*header_lsp);
2010         printf("\n\t  lsp-id: %s, seq: 0x%08x, lifetime: %5us\n\t  chksum: 0x%04x",
2011                isis_print_id(header_lsp->lsp_id, LSP_ID_LEN),
2012                EXTRACT_32BITS(header_lsp->sequence_number),
2013                EXTRACT_16BITS(header_lsp->remaining_lifetime),
2014                EXTRACT_16BITS(header_lsp->checksum));
2015
2016
2017         osi_print_cksum((u_int8_t *)header_lsp->lsp_id,
2018                         EXTRACT_16BITS(header_lsp->checksum), 12, length-12);
2019
2020         /*
2021          * Clear checksum and lifetime prior to signature verification.
2022          */
2023         header_lsp->checksum[0] = 0;
2024         header_lsp->checksum[1] = 0;
2025         header_lsp->remaining_lifetime[0] = 0;
2026         header_lsp->remaining_lifetime[1] = 0;
2027         
2028
2029         printf(", PDU length: %u, Flags: [ %s",
2030                pdu_len,
2031                ISIS_MASK_LSP_OL_BIT(header_lsp->typeblock) ? "Overload bit set, " : "");
2032
2033         if (ISIS_MASK_LSP_ATT_BITS(header_lsp->typeblock)) {
2034             printf("%s", ISIS_MASK_LSP_ATT_DEFAULT_BIT(header_lsp->typeblock) ? "default " : "");
2035             printf("%s", ISIS_MASK_LSP_ATT_DELAY_BIT(header_lsp->typeblock) ? "delay " : "");
2036             printf("%s", ISIS_MASK_LSP_ATT_EXPENSE_BIT(header_lsp->typeblock) ? "expense " : "");
2037             printf("%s", ISIS_MASK_LSP_ATT_ERROR_BIT(header_lsp->typeblock) ? "error " : "");
2038             printf("ATT bit set, ");
2039         }
2040         printf("%s", ISIS_MASK_LSP_PARTITION_BIT(header_lsp->typeblock) ? "P bit set, " : "");
2041         printf("%s ]", tok2str(isis_lsp_istype_values,"Unknown(0x%x)",ISIS_MASK_LSP_ISTYPE_BITS(header_lsp->typeblock)));
2042
2043         if (vflag > 1) {
2044             if(!print_unknown_data(pptr,"\n\t  ",ISIS_LSP_HEADER_SIZE))
2045                 return(0);
2046         }
2047
2048         packet_len -= (ISIS_COMMON_HEADER_SIZE+ISIS_LSP_HEADER_SIZE);
2049         pptr = p + (ISIS_COMMON_HEADER_SIZE+ISIS_LSP_HEADER_SIZE);
2050         break;
2051
2052     case ISIS_PDU_L1_CSNP:
2053     case ISIS_PDU_L2_CSNP:
2054         if (isis_header->fixed_len != (ISIS_COMMON_HEADER_SIZE+ISIS_CSNP_HEADER_SIZE)) {
2055             printf(", bogus fixed header length %u should be %lu",
2056                    isis_header->fixed_len, (unsigned long)ISIS_CSNP_HEADER_SIZE);
2057             return (0);
2058         }
2059
2060         pdu_len=EXTRACT_16BITS(header_csnp->pdu_len);
2061         if (packet_len>pdu_len) {
2062             packet_len=pdu_len; /* do TLV decoding as long as it makes sense */
2063             length=pdu_len;
2064         }
2065
2066         TCHECK(*header_csnp);
2067         printf("\n\t  source-id:    %s, PDU length: %u",
2068                isis_print_id(header_csnp->source_id, NODE_ID_LEN),
2069                pdu_len);
2070         printf("\n\t  start lsp-id: %s",
2071                isis_print_id(header_csnp->start_lsp_id, LSP_ID_LEN));
2072         printf("\n\t  end lsp-id:   %s",
2073                isis_print_id(header_csnp->end_lsp_id, LSP_ID_LEN));
2074
2075         if (vflag > 1) {
2076             if(!print_unknown_data(pptr,"\n\t  ",ISIS_CSNP_HEADER_SIZE))
2077                 return(0);
2078         }
2079
2080         packet_len -= (ISIS_COMMON_HEADER_SIZE+ISIS_CSNP_HEADER_SIZE);
2081         pptr = p + (ISIS_COMMON_HEADER_SIZE+ISIS_CSNP_HEADER_SIZE);
2082         break;
2083
2084     case ISIS_PDU_L1_PSNP:
2085     case ISIS_PDU_L2_PSNP:
2086         if (isis_header->fixed_len != (ISIS_COMMON_HEADER_SIZE+ISIS_PSNP_HEADER_SIZE)) {
2087             printf("- bogus fixed header length %u should be %lu",
2088                    isis_header->fixed_len, (unsigned long)ISIS_PSNP_HEADER_SIZE);
2089             return (0);
2090         }
2091
2092         pdu_len=EXTRACT_16BITS(header_psnp->pdu_len);
2093         if (packet_len>pdu_len) {
2094             packet_len=pdu_len; /* do TLV decoding as long as it makes sense */
2095             length=pdu_len;
2096         }
2097
2098         TCHECK(*header_psnp);
2099         printf("\n\t  source-id:    %s, PDU length: %u",
2100                isis_print_id(header_psnp->source_id, NODE_ID_LEN),
2101                pdu_len);
2102
2103         if (vflag > 1) {
2104             if(!print_unknown_data(pptr,"\n\t  ",ISIS_PSNP_HEADER_SIZE))
2105                 return(0);
2106         }
2107
2108         packet_len -= (ISIS_COMMON_HEADER_SIZE+ISIS_PSNP_HEADER_SIZE);
2109         pptr = p + (ISIS_COMMON_HEADER_SIZE+ISIS_PSNP_HEADER_SIZE);
2110         break;
2111
2112     default:
2113         if(!print_unknown_data(pptr,"\n\t  ",length))
2114             return(0);
2115         return (0);
2116     }
2117
2118     /*
2119      * Now print the TLV's.
2120      */
2121
2122     while (packet_len >= 2) {
2123         if (pptr == snapend) {
2124             return (1);
2125         }
2126
2127         if (!TTEST2(*pptr, 2)) {
2128             printf("\n\t\t packet exceeded snapshot (%ld) bytes",
2129                    (long)(pptr-snapend));
2130             return (1);
2131         }
2132         tlv_type = *pptr++;
2133         tlv_len = *pptr++;
2134         tmp =tlv_len; /* copy temporary len & pointer to packet data */
2135         tptr = pptr;
2136         packet_len -= 2;
2137         if (tlv_len > packet_len) {
2138             break;
2139         }
2140
2141         /* first lets see if we know the TLVs name*/
2142         printf("\n\t    %s TLV #%u, length: %u",
2143                tok2str(isis_tlv_values,
2144                        "unknown",
2145                        tlv_type),
2146                tlv_type,
2147                tlv_len);
2148
2149         if (tlv_len == 0) /* something is malformed */
2150             continue;
2151
2152         /* now check if we have a decoder otherwise do a hexdump at the end*/
2153         switch (tlv_type) {
2154         case ISIS_TLV_AREA_ADDR:
2155             if (!TTEST2(*tptr, 1))
2156                 goto trunctlv;
2157             alen = *tptr++;
2158             while (tmp && alen < tmp) {
2159                 printf("\n\t      Area address (length: %u): %s",
2160                        alen,
2161                        isonsap_string(tptr,alen));
2162                 tptr += alen;
2163                 tmp -= alen + 1;
2164                 if (tmp==0) /* if this is the last area address do not attemt a boundary check */
2165                     break;
2166                 if (!TTEST2(*tptr, 1))
2167                     goto trunctlv;
2168                 alen = *tptr++;
2169             }
2170             break;
2171         case ISIS_TLV_ISNEIGH:
2172             while (tmp >= ETHER_ADDR_LEN) {
2173                 if (!TTEST2(*tptr, ETHER_ADDR_LEN))
2174                     goto trunctlv;
2175                 printf("\n\t      SNPA: %s",isis_print_id(tptr,ETHER_ADDR_LEN));
2176                 tmp -= ETHER_ADDR_LEN;
2177                 tptr += ETHER_ADDR_LEN;
2178             }
2179             break;
2180
2181         case ISIS_TLV_ISNEIGH_VARLEN:
2182             if (!TTEST2(*tptr, 1) || tmp < 3) /* min. TLV length */
2183                 goto trunctlv;
2184             lan_alen = *tptr++; /* LAN address length */
2185             if (lan_alen == 0) {
2186                 printf("\n\t      LAN address length 0 bytes (invalid)");
2187                 break;
2188             }
2189             tmp --;
2190             printf("\n\t      LAN address length %u bytes ",lan_alen);
2191             while (tmp >= lan_alen) {
2192                 if (!TTEST2(*tptr, lan_alen))
2193                     goto trunctlv;
2194                 printf("\n\t\tIS Neighbor: %s",isis_print_id(tptr,lan_alen));
2195                 tmp -= lan_alen;
2196                 tptr +=lan_alen;
2197             }
2198             break;
2199
2200         case ISIS_TLV_PADDING:
2201             break;
2202
2203         case ISIS_TLV_MT_IS_REACH:
2204             mt_len = isis_print_mtid(tptr, "\n\t      ");
2205             if (mt_len == 0) /* did something go wrong ? */
2206                 goto trunctlv;
2207             tptr+=mt_len;
2208             tmp-=mt_len;
2209             while (tmp >= 2+NODE_ID_LEN+3+1) {
2210                 ext_is_len = isis_print_ext_is_reach(tptr,"\n\t      ",tlv_type);
2211                 if (ext_is_len == 0) /* did something go wrong ? */
2212                     goto trunctlv;
2213                    
2214                 tmp-=ext_is_len;
2215                 tptr+=ext_is_len;
2216             }
2217             break;
2218
2219         case ISIS_TLV_IS_ALIAS_ID:
2220             while (tmp >= NODE_ID_LEN+1) { /* is it worth attempting a decode ? */
2221                 ext_is_len = isis_print_ext_is_reach(tptr,"\n\t      ",tlv_type);
2222                 if (ext_is_len == 0) /* did something go wrong ? */
2223                     goto trunctlv;
2224                 tmp-=ext_is_len;
2225                 tptr+=ext_is_len;
2226             }
2227             break;
2228
2229         case ISIS_TLV_EXT_IS_REACH:
2230             while (tmp >= NODE_ID_LEN+3+1) { /* is it worth attempting a decode ? */
2231                 ext_is_len = isis_print_ext_is_reach(tptr,"\n\t      ",tlv_type);
2232                 if (ext_is_len == 0) /* did something go wrong ? */
2233                     goto trunctlv;                   
2234                 tmp-=ext_is_len;
2235                 tptr+=ext_is_len;
2236             }
2237             break;
2238         case ISIS_TLV_IS_REACH:
2239             if (!TTEST2(*tptr,1))  /* check if there is one byte left to read out the virtual flag */
2240                 goto trunctlv;
2241             printf("\n\t      %s",
2242                    tok2str(isis_is_reach_virtual_values,
2243                            "bogus virtual flag 0x%02x",
2244                            *tptr++));
2245             tlv_is_reach = (const struct isis_tlv_is_reach *)tptr;
2246             while (tmp >= sizeof(struct isis_tlv_is_reach)) {
2247                 if (!TTEST(*tlv_is_reach))
2248                     goto trunctlv;
2249                 printf("\n\t      IS Neighbor: %s",
2250                        isis_print_id(tlv_is_reach->neighbor_nodeid, NODE_ID_LEN));
2251                 isis_print_metric_block(&tlv_is_reach->isis_metric_block);
2252                 tmp -= sizeof(struct isis_tlv_is_reach);
2253                 tlv_is_reach++;
2254             }
2255             break;
2256
2257         case ISIS_TLV_ESNEIGH:
2258             tlv_es_reach = (const struct isis_tlv_es_reach *)tptr;
2259             while (tmp >= sizeof(struct isis_tlv_es_reach)) {
2260                 if (!TTEST(*tlv_es_reach))
2261                     goto trunctlv;
2262                 printf("\n\t      ES Neighbor: %s",
2263                        isis_print_id(tlv_es_reach->neighbor_sysid,SYSTEM_ID_LEN));
2264                 isis_print_metric_block(&tlv_es_reach->isis_metric_block);
2265                 tmp -= sizeof(struct isis_tlv_es_reach);
2266                 tlv_es_reach++;
2267             }
2268             break;
2269
2270             /* those two TLVs share the same format */
2271         case ISIS_TLV_INT_IP_REACH:
2272         case ISIS_TLV_EXT_IP_REACH:
2273             if (!isis_print_tlv_ip_reach(pptr, "\n\t      ", tlv_len))
2274                 return (1);
2275             break;
2276
2277         case ISIS_TLV_EXTD_IP_REACH:
2278             while (tmp>0) {
2279                 ext_ip_len = isis_print_extd_ip_reach(tptr, "\n\t      ", AF_INET);
2280                 if (ext_ip_len == 0) /* did something go wrong ? */
2281                     goto trunctlv;
2282                 tptr+=ext_ip_len;
2283                 tmp-=ext_ip_len;
2284             }
2285             break;
2286
2287         case ISIS_TLV_MT_IP_REACH:
2288             mt_len = isis_print_mtid(tptr, "\n\t      ");
2289             if (mt_len == 0) { /* did something go wrong ? */
2290                 goto trunctlv;
2291             }
2292             tptr+=mt_len;
2293             tmp-=mt_len;
2294
2295             while (tmp>0) {
2296                 ext_ip_len = isis_print_extd_ip_reach(tptr, "\n\t      ", AF_INET);
2297                 if (ext_ip_len == 0) /* did something go wrong ? */
2298                     goto trunctlv;
2299                 tptr+=ext_ip_len;
2300                 tmp-=ext_ip_len;
2301             }
2302             break;
2303
2304 #ifdef INET6
2305         case ISIS_TLV_IP6_REACH:
2306             while (tmp>0) {
2307                 ext_ip_len = isis_print_extd_ip_reach(tptr, "\n\t      ", AF_INET6);
2308                 if (ext_ip_len == 0) /* did something go wrong ? */
2309                     goto trunctlv;
2310                 tptr+=ext_ip_len;
2311                 tmp-=ext_ip_len;
2312             }
2313             break;
2314
2315         case ISIS_TLV_MT_IP6_REACH:
2316             mt_len = isis_print_mtid(tptr, "\n\t      ");
2317             if (mt_len == 0) { /* did something go wrong ? */
2318                 goto trunctlv;
2319             }
2320             tptr+=mt_len;
2321             tmp-=mt_len;
2322
2323             while (tmp>0) {
2324                 ext_ip_len = isis_print_extd_ip_reach(tptr, "\n\t      ", AF_INET6);
2325                 if (ext_ip_len == 0) /* did something go wrong ? */
2326                     goto trunctlv;
2327                 tptr+=ext_ip_len;
2328                 tmp-=ext_ip_len;
2329             }
2330             break;
2331
2332         case ISIS_TLV_IP6ADDR:
2333             while (tmp>=sizeof(struct in6_addr)) {
2334                 if (!TTEST2(*tptr, sizeof(struct in6_addr)))
2335                     goto trunctlv;
2336
2337                 printf("\n\t      IPv6 interface address: %s",
2338                        ip6addr_string(tptr));
2339
2340                 tptr += sizeof(struct in6_addr);
2341                 tmp -= sizeof(struct in6_addr);
2342             }
2343             break;
2344 #endif
2345         case ISIS_TLV_AUTH:
2346             if (!TTEST2(*tptr, 1))
2347                 goto trunctlv;
2348
2349             printf("\n\t      %s: ",
2350                    tok2str(isis_subtlv_auth_values,
2351                            "unknown Authentication type 0x%02x",
2352                            *tptr));
2353
2354             switch (*tptr) {
2355             case ISIS_SUBTLV_AUTH_SIMPLE:
2356                 for(i=1;i<tlv_len;i++) {
2357                     if (!TTEST2(*(tptr+i), 1))
2358                         goto trunctlv;
2359                     printf("%c",*(tptr+i));
2360                 }
2361                 break;
2362             case ISIS_SUBTLV_AUTH_MD5:
2363                 for(i=1;i<tlv_len;i++) {
2364                     if (!TTEST2(*(tptr+i), 1))
2365                         goto trunctlv;
2366                     printf("%02x",*(tptr+i));
2367                 }
2368                 if (tlv_len != ISIS_SUBTLV_AUTH_MD5_LEN+1)
2369                     printf(", (malformed subTLV) ");
2370
2371 #ifdef HAVE_LIBCRYPTO
2372                 sigcheck = signature_verify(optr, length,
2373                                             (unsigned char *)tptr + 1);
2374 #else
2375                 sigcheck = CANT_CHECK_SIGNATURE;
2376 #endif
2377                 printf(" (%s)", tok2str(signature_check_values, "Unknown", sigcheck));
2378
2379                 break;
2380             case ISIS_SUBTLV_AUTH_GENERIC:
2381                 key_id = EXTRACT_16BITS((tptr+1));
2382                 printf("%u, password: ", key_id); 
2383                 for(i=1 + sizeof(u_int16_t);i<tlv_len;i++) {
2384                     if (!TTEST2(*(tptr+i), 1))
2385                         goto trunctlv;
2386                     printf("%02x",*(tptr+i));
2387                 }
2388                 break;
2389             case ISIS_SUBTLV_AUTH_PRIVATE:
2390             default:
2391                 if(!print_unknown_data(tptr+1,"\n\t\t  ",tlv_len-1))
2392                     return(0);
2393                 break;
2394             }
2395             break;
2396
2397         case ISIS_TLV_PTP_ADJ:
2398             tlv_ptp_adj = (const struct isis_tlv_ptp_adj *)tptr;
2399             if(tmp>=1) {
2400                 if (!TTEST2(*tptr, 1))
2401                     goto trunctlv;
2402                 printf("\n\t      Adjacency State: %s (%u)",
2403                        tok2str(isis_ptp_adjancey_values, "unknown", *tptr),
2404                         *tptr);
2405                 tmp--;
2406             }
2407             if(tmp>sizeof(tlv_ptp_adj->extd_local_circuit_id)) {
2408                 if (!TTEST2(tlv_ptp_adj->extd_local_circuit_id,
2409                             sizeof(tlv_ptp_adj->extd_local_circuit_id)))
2410                     goto trunctlv;
2411                 printf("\n\t      Extended Local circuit-ID: 0x%08x",
2412                        EXTRACT_32BITS(tlv_ptp_adj->extd_local_circuit_id));
2413                 tmp-=sizeof(tlv_ptp_adj->extd_local_circuit_id);
2414             }
2415             if(tmp>=SYSTEM_ID_LEN) {
2416                 if (!TTEST2(tlv_ptp_adj->neighbor_sysid, SYSTEM_ID_LEN))
2417                     goto trunctlv;
2418                 printf("\n\t      Neighbor System-ID: %s",
2419                        isis_print_id(tlv_ptp_adj->neighbor_sysid,SYSTEM_ID_LEN));
2420                 tmp-=SYSTEM_ID_LEN;
2421             }
2422             if(tmp>=sizeof(tlv_ptp_adj->neighbor_extd_local_circuit_id)) {
2423                 if (!TTEST2(tlv_ptp_adj->neighbor_extd_local_circuit_id,
2424                             sizeof(tlv_ptp_adj->neighbor_extd_local_circuit_id)))
2425                     goto trunctlv;
2426                 printf("\n\t      Neighbor Extended Local circuit-ID: 0x%08x",
2427                        EXTRACT_32BITS(tlv_ptp_adj->neighbor_extd_local_circuit_id));
2428             }
2429             break;
2430
2431         case ISIS_TLV_PROTOCOLS:
2432             printf("\n\t      NLPID(s): ");
2433             while (tmp>0) {
2434                 if (!TTEST2(*(tptr), 1))
2435                     goto trunctlv;
2436                 printf("%s (0x%02x)",
2437                        tok2str(nlpid_values,
2438                                "unknown",
2439                                *tptr),
2440                        *tptr);
2441                 if (tmp>1) /* further NPLIDs ? - put comma */
2442                     printf(", ");
2443                 tptr++;
2444                 tmp--;
2445             }
2446             break;
2447
2448         case ISIS_TLV_TE_ROUTER_ID:
2449             if (!TTEST2(*pptr, sizeof(struct in_addr)))
2450                 goto trunctlv;
2451             printf("\n\t      Traffic Engineering Router ID: %s", ipaddr_string(pptr));
2452             break;
2453
2454         case ISIS_TLV_IPADDR:
2455             while (tmp>=sizeof(struct in_addr)) {
2456                 if (!TTEST2(*tptr, sizeof(struct in_addr)))
2457                     goto trunctlv;
2458                 printf("\n\t      IPv4 interface address: %s", ipaddr_string(tptr));
2459                 tptr += sizeof(struct in_addr);
2460                 tmp -= sizeof(struct in_addr);
2461             }
2462             break;
2463
2464         case ISIS_TLV_HOSTNAME:
2465             printf("\n\t      Hostname: ");
2466             while (tmp>0) {
2467                 if (!TTEST2(*tptr, 1))
2468                     goto trunctlv;
2469                 printf("%c",*tptr++);
2470                 tmp--;
2471             }
2472             break;
2473
2474         case ISIS_TLV_SHARED_RISK_GROUP:
2475             if (tmp < NODE_ID_LEN)
2476                 break;
2477             if (!TTEST2(*tptr, NODE_ID_LEN))
2478                 goto trunctlv;
2479             printf("\n\t      IS Neighbor: %s", isis_print_id(tptr, NODE_ID_LEN));
2480             tptr+=(NODE_ID_LEN);
2481             tmp-=(NODE_ID_LEN);
2482
2483             if (tmp < 1)
2484                 break;
2485             if (!TTEST2(*tptr, 1))
2486                 goto trunctlv;
2487             printf(", Flags: [%s]", ISIS_MASK_TLV_SHARED_RISK_GROUP(*tptr++) ? "numbered" : "unnumbered");
2488             tmp--;
2489
2490             if (tmp < sizeof(struct in_addr))
2491                 break;
2492             if (!TTEST2(*tptr,sizeof(struct in_addr)))
2493                 goto trunctlv;
2494             printf("\n\t      IPv4 interface address: %s", ipaddr_string(tptr));
2495             tptr+=sizeof(struct in_addr);
2496             tmp-=sizeof(struct in_addr);
2497
2498             if (tmp < sizeof(struct in_addr))
2499                 break;
2500             if (!TTEST2(*tptr,sizeof(struct in_addr)))
2501                 goto trunctlv;
2502             printf("\n\t      IPv4 neighbor address: %s", ipaddr_string(tptr));
2503             tptr+=sizeof(struct in_addr);
2504             tmp-=sizeof(struct in_addr);
2505
2506             while (tmp>=4) {
2507                 if (!TTEST2(*tptr, 4))
2508                     goto trunctlv;
2509                 printf("\n\t      Link-ID: 0x%08x", EXTRACT_32BITS(tptr));
2510                 tptr+=4;
2511                 tmp-=4;
2512             }
2513             break;
2514
2515         case ISIS_TLV_LSP:
2516             tlv_lsp = (const struct isis_tlv_lsp *)tptr;
2517             while(tmp>=sizeof(struct isis_tlv_lsp)) {
2518                 if (!TTEST((tlv_lsp->lsp_id)[LSP_ID_LEN-1]))
2519                     goto trunctlv;
2520                 printf("\n\t      lsp-id: %s",
2521                        isis_print_id(tlv_lsp->lsp_id, LSP_ID_LEN));
2522                 if (!TTEST2(tlv_lsp->sequence_number, 4))
2523                     goto trunctlv;
2524                 printf(", seq: 0x%08x",EXTRACT_32BITS(tlv_lsp->sequence_number));
2525                 if (!TTEST2(tlv_lsp->remaining_lifetime, 2))
2526                     goto trunctlv;
2527                 printf(", lifetime: %5ds",EXTRACT_16BITS(tlv_lsp->remaining_lifetime));
2528                 if (!TTEST2(tlv_lsp->checksum, 2))
2529                     goto trunctlv;
2530                 printf(", chksum: 0x%04x",EXTRACT_16BITS(tlv_lsp->checksum));
2531                 tmp-=sizeof(struct isis_tlv_lsp);
2532                 tlv_lsp++;
2533             }
2534             break;
2535
2536         case ISIS_TLV_CHECKSUM:
2537             if (tmp < ISIS_TLV_CHECKSUM_MINLEN)
2538                 break;
2539             if (!TTEST2(*tptr, ISIS_TLV_CHECKSUM_MINLEN))
2540                 goto trunctlv;
2541             printf("\n\t      checksum: 0x%04x ", EXTRACT_16BITS(tptr));
2542             /* do not attempt to verify the checksum if it is zero
2543              * most likely a HMAC-MD5 TLV is also present and
2544              * to avoid conflicts the checksum TLV is zeroed.
2545              * see rfc3358 for details
2546              */
2547             osi_print_cksum(optr, EXTRACT_16BITS(tptr), tptr-optr, length);
2548             break;
2549
2550         case ISIS_TLV_MT_SUPPORTED:
2551             if (tmp < ISIS_TLV_MT_SUPPORTED_MINLEN)
2552                 break;
2553             while (tmp>1) {
2554                 /* length can only be a multiple of 2, otherwise there is
2555                    something broken -> so decode down until length is 1 */
2556                 if (tmp!=1) {
2557                     mt_len = isis_print_mtid(tptr, "\n\t      ");
2558                     if (mt_len == 0) /* did something go wrong ? */
2559                         goto trunctlv;
2560                     tptr+=mt_len;
2561                     tmp-=mt_len;
2562                 } else {
2563                     printf("\n\t      malformed MT-ID");
2564                     break;
2565                 }
2566             }
2567             break;
2568
2569         case ISIS_TLV_RESTART_SIGNALING:
2570             /* first attempt to decode the flags */
2571             if (tmp < ISIS_TLV_RESTART_SIGNALING_FLAGLEN)
2572                 break;
2573             if (!TTEST2(*tptr, ISIS_TLV_RESTART_SIGNALING_FLAGLEN))
2574                 goto trunctlv;
2575             printf("\n\t      Flags [%s]",
2576                    bittok2str(isis_restart_flag_values, "none", *tptr));
2577             tptr+=ISIS_TLV_RESTART_SIGNALING_FLAGLEN;
2578             tmp-=ISIS_TLV_RESTART_SIGNALING_FLAGLEN;
2579
2580             /* is there anything other than the flags field? */
2581             if (tmp == 0)
2582                 break;
2583
2584             if (tmp < ISIS_TLV_RESTART_SIGNALING_HOLDTIMELEN)
2585                 break;
2586             if (!TTEST2(*tptr, ISIS_TLV_RESTART_SIGNALING_HOLDTIMELEN))
2587                 goto trunctlv;
2588
2589             printf(", Remaining holding time %us", EXTRACT_16BITS(tptr));
2590             tptr+=ISIS_TLV_RESTART_SIGNALING_HOLDTIMELEN;
2591             tmp-=ISIS_TLV_RESTART_SIGNALING_HOLDTIMELEN;
2592
2593             /* is there an additional sysid field present ?*/
2594             if (tmp == SYSTEM_ID_LEN) {
2595                     if (!TTEST2(*tptr, SYSTEM_ID_LEN))
2596                             goto trunctlv;
2597                     printf(", for %s",isis_print_id(tptr,SYSTEM_ID_LEN));
2598             } 
2599             break;
2600
2601         case ISIS_TLV_IDRP_INFO:
2602             if (tmp < ISIS_TLV_IDRP_INFO_MINLEN)
2603                 break;
2604             if (!TTEST2(*tptr, ISIS_TLV_IDRP_INFO_MINLEN))
2605                 goto trunctlv;
2606             printf("\n\t      Inter-Domain Information Type: %s",
2607                    tok2str(isis_subtlv_idrp_values,
2608                            "Unknown (0x%02x)",
2609                            *tptr));
2610             switch (*tptr++) {
2611             case ISIS_SUBTLV_IDRP_ASN:
2612                 if (!TTEST2(*tptr, 2)) /* fetch AS number */
2613                     goto trunctlv;
2614                 printf("AS Number: %u",EXTRACT_16BITS(tptr));
2615                 break;
2616             case ISIS_SUBTLV_IDRP_LOCAL:
2617             case ISIS_SUBTLV_IDRP_RES:
2618             default:
2619                 if(!print_unknown_data(tptr,"\n\t      ",tlv_len-1))
2620                     return(0);
2621                 break;
2622             }
2623             break;
2624
2625         case ISIS_TLV_LSP_BUFFERSIZE:
2626             if (tmp < ISIS_TLV_LSP_BUFFERSIZE_MINLEN)
2627                 break;
2628             if (!TTEST2(*tptr, ISIS_TLV_LSP_BUFFERSIZE_MINLEN))
2629                 goto trunctlv;
2630             printf("\n\t      LSP Buffersize: %u",EXTRACT_16BITS(tptr));
2631             break;
2632
2633         case ISIS_TLV_PART_DIS:
2634             while (tmp >= SYSTEM_ID_LEN) {
2635                 if (!TTEST2(*tptr, SYSTEM_ID_LEN))
2636                     goto trunctlv;
2637                 printf("\n\t      %s",isis_print_id(tptr,SYSTEM_ID_LEN));
2638                 tptr+=SYSTEM_ID_LEN;
2639                 tmp-=SYSTEM_ID_LEN;
2640             }
2641             break;
2642
2643         case ISIS_TLV_PREFIX_NEIGH:
2644             if (tmp < sizeof(struct isis_metric_block))
2645                 break;
2646             if (!TTEST2(*tptr, sizeof(struct isis_metric_block)))
2647                 goto trunctlv;
2648             printf("\n\t      Metric Block");
2649             isis_print_metric_block((const struct isis_metric_block *)tptr);
2650             tptr+=sizeof(struct isis_metric_block);
2651             tmp-=sizeof(struct isis_metric_block);
2652
2653             while(tmp>0) {
2654                 if (!TTEST2(*tptr, 1))
2655                     goto trunctlv;
2656                 prefix_len=*tptr++; /* read out prefix length in semioctets*/
2657                 if (prefix_len < 2) {
2658                     printf("\n\t\tAddress: prefix length %u < 2", prefix_len);
2659                     break;
2660                 }
2661                 tmp--;
2662                 if (tmp < prefix_len/2)
2663                     break;
2664                 if (!TTEST2(*tptr, prefix_len/2))
2665                     goto trunctlv;
2666                 printf("\n\t\tAddress: %s/%u",
2667                        isonsap_string(tptr,prefix_len/2),
2668                        prefix_len*4);
2669                 tptr+=prefix_len/2;
2670                 tmp-=prefix_len/2;
2671             }
2672             break;
2673
2674         case ISIS_TLV_IIH_SEQNR:
2675             if (tmp < ISIS_TLV_IIH_SEQNR_MINLEN)
2676                 break;
2677             if (!TTEST2(*tptr, ISIS_TLV_IIH_SEQNR_MINLEN)) /* check if four bytes are on the wire */
2678                 goto trunctlv;
2679             printf("\n\t      Sequence number: %u", EXTRACT_32BITS(tptr) );
2680             break;
2681
2682         case ISIS_TLV_VENDOR_PRIVATE:
2683             if (tmp < ISIS_TLV_VENDOR_PRIVATE_MINLEN)
2684                 break;
2685             if (!TTEST2(*tptr, ISIS_TLV_VENDOR_PRIVATE_MINLEN)) /* check if enough byte for a full oui */
2686                 goto trunctlv;
2687             vendor_id = EXTRACT_24BITS(tptr);
2688             printf("\n\t      Vendor: %s (%u)",
2689                    tok2str(oui_values,"Unknown",vendor_id),
2690                    vendor_id);
2691             tptr+=3;
2692             tmp-=3;
2693             if (tmp > 0) /* hexdump the rest */
2694                 if(!print_unknown_data(tptr,"\n\t\t",tmp))
2695                     return(0);
2696             break;
2697             /*
2698              * FIXME those are the defined TLVs that lack a decoder
2699              * you are welcome to contribute code ;-)
2700              */
2701
2702         case ISIS_TLV_DECNET_PHASE4:
2703         case ISIS_TLV_LUCENT_PRIVATE:
2704         case ISIS_TLV_IPAUTH:
2705         case ISIS_TLV_NORTEL_PRIVATE1:
2706         case ISIS_TLV_NORTEL_PRIVATE2:
2707
2708         default:
2709             if (vflag <= 1) {
2710                 if(!print_unknown_data(pptr,"\n\t\t",tlv_len))
2711                     return(0);
2712             }
2713             break;
2714         }
2715         /* do we want to see an additionally hexdump ? */
2716         if (vflag> 1) {
2717             if(!print_unknown_data(pptr,"\n\t      ",tlv_len))
2718                 return(0);
2719         }
2720
2721         pptr += tlv_len;
2722         packet_len -= tlv_len;
2723     }
2724
2725     if (packet_len != 0) {
2726         printf("\n\t      %u straggler bytes", packet_len);
2727     }
2728     return (1);
2729
2730  trunc:
2731     fputs("[|isis]", stdout);
2732     return (1);
2733
2734  trunctlv:
2735     printf("\n\t\t packet exceeded snapshot");
2736     return(1);
2737 }
2738
2739 static void
2740 osi_print_cksum (const u_int8_t *pptr, u_int16_t checksum,
2741                     u_int checksum_offset, u_int length)
2742 {
2743         u_int16_t calculated_checksum;
2744
2745         /* do not attempt to verify the checksum if it is zero */
2746         if (!checksum) {
2747                 printf("(unverified)");
2748         } else {
2749                 calculated_checksum = create_osi_cksum(pptr, checksum_offset, length);
2750                 if (checksum == calculated_checksum) {
2751                         printf(" (correct)");
2752                 } else {
2753                         printf(" (incorrect should be 0x%04x)", calculated_checksum);
2754                 }
2755         }
2756 }
2757
2758 /*
2759  * Local Variables:
2760  * c-style: whitesmith
2761  * c-basic-offset: 8
2762  * End:
2763  */