Home | History | Annotate | Line # | Download | only in dist
print-ospf.c revision 1.2.12.1
      1       1.1  christos /*
      2       1.1  christos  * Copyright (c) 1992, 1993, 1994, 1995, 1996, 1997
      3       1.1  christos  *	The Regents of the University of California.  All rights reserved.
      4       1.1  christos  *
      5       1.1  christos  * Redistribution and use in source and binary forms, with or without
      6       1.1  christos  * modification, are permitted provided that: (1) source code distributions
      7       1.1  christos  * retain the above copyright notice and this paragraph in its entirety, (2)
      8       1.1  christos  * distributions including binary code include the above copyright notice and
      9       1.1  christos  * this paragraph in its entirety in the documentation or other materials
     10       1.1  christos  * provided with the distribution, and (3) all advertising materials mentioning
     11       1.1  christos  * features or use of this software display the following acknowledgement:
     12       1.1  christos  * ``This product includes software developed by the University of California,
     13       1.1  christos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     14       1.1  christos  * the University nor the names of its contributors may be used to endorse
     15       1.1  christos  * or promote products derived from this software without specific prior
     16       1.1  christos  * written permission.
     17       1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     18       1.1  christos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     19       1.1  christos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     20       1.1  christos  *
     21       1.1  christos  * OSPF support contributed by Jeffrey Honig (jch (at) mitchell.cit.cornell.edu)
     22       1.1  christos  */
     23       1.1  christos 
     24       1.2  christos #include <sys/cdefs.h>
     25       1.1  christos #ifndef lint
     26       1.2  christos #if 0
     27       1.1  christos static const char rcsid[] _U_ =
     28  1.2.12.1       tls     "@(#) Header: /tcpdump/master/tcpdump/print-ospf.c,v 1.66 2007-10-08 07:53:21 hannes Exp  (LBL)";
     29       1.2  christos #else
     30       1.2  christos __RCSID("$NetBSD: print-ospf.c,v 1.2.12.1 2013/06/23 06:28:29 tls Exp $");
     31       1.2  christos #endif
     32       1.1  christos #endif
     33       1.1  christos 
     34       1.1  christos #ifdef HAVE_CONFIG_H
     35       1.1  christos #include "config.h"
     36       1.1  christos #endif
     37       1.1  christos 
     38       1.1  christos #include <tcpdump-stdinc.h>
     39       1.1  christos 
     40       1.1  christos #include <stdio.h>
     41       1.1  christos 
     42       1.1  christos #include "interface.h"
     43       1.1  christos #include "addrtoname.h"
     44       1.1  christos #include "extract.h"
     45       1.1  christos #include "gmpls.h"
     46       1.1  christos 
     47       1.1  christos #include "ospf.h"
     48       1.1  christos 
     49       1.1  christos #include "ip.h"
     50       1.1  christos 
     51       1.1  christos static struct tok ospf_option_values[] = {
     52       1.1  christos         { OSPF_OPTION_T,	"MultiTopology" }, /* draft-ietf-ospf-mt-09 */
     53       1.1  christos 	{ OSPF_OPTION_E,	"External" },
     54       1.1  christos 	{ OSPF_OPTION_MC,	"Multicast" },
     55       1.1  christos 	{ OSPF_OPTION_NP,	"NSSA" },
     56       1.1  christos         { OSPF_OPTION_L,        "LLS" },
     57       1.1  christos 	{ OSPF_OPTION_DC,	"Demand Circuit" },
     58       1.1  christos 	{ OSPF_OPTION_O,	"Opaque" },
     59       1.1  christos 	{ OSPF_OPTION_DN,	"Up/Down" },
     60       1.1  christos 	{ 0,			NULL }
     61       1.1  christos };
     62       1.1  christos 
     63       1.1  christos static struct tok ospf_authtype_values[] = {
     64       1.1  christos 	{ OSPF_AUTH_NONE,	"none" },
     65       1.1  christos 	{ OSPF_AUTH_SIMPLE,	"simple" },
     66       1.1  christos 	{ OSPF_AUTH_MD5,	"MD5" },
     67       1.1  christos 	{ 0,			NULL }
     68       1.1  christos };
     69       1.1  christos 
     70       1.1  christos static struct tok ospf_rla_flag_values[] = {
     71       1.1  christos 	{ RLA_FLAG_B,		"ABR" },
     72       1.1  christos 	{ RLA_FLAG_E,		"ASBR" },
     73       1.1  christos 	{ RLA_FLAG_W1,		"Virtual" },
     74       1.1  christos 	{ RLA_FLAG_W2,		"W2" },
     75       1.1  christos 	{ 0,			NULL }
     76       1.1  christos };
     77       1.1  christos 
     78       1.1  christos static struct tok type2str[] = {
     79       1.1  christos 	{ OSPF_TYPE_UMD,	"UMD" },
     80       1.1  christos 	{ OSPF_TYPE_HELLO,	"Hello" },
     81       1.1  christos 	{ OSPF_TYPE_DD,		"Database Description" },
     82       1.1  christos 	{ OSPF_TYPE_LS_REQ,	"LS-Request" },
     83       1.1  christos 	{ OSPF_TYPE_LS_UPDATE,	"LS-Update" },
     84       1.1  christos 	{ OSPF_TYPE_LS_ACK,	"LS-Ack" },
     85       1.1  christos 	{ 0,			NULL }
     86       1.1  christos };
     87       1.1  christos 
     88       1.1  christos static struct tok lsa_values[] = {
     89       1.1  christos 	{ LS_TYPE_ROUTER,       "Router" },
     90       1.1  christos 	{ LS_TYPE_NETWORK,      "Network" },
     91       1.1  christos 	{ LS_TYPE_SUM_IP,       "Summary" },
     92       1.1  christos 	{ LS_TYPE_SUM_ABR,      "ASBR Summary" },
     93       1.1  christos 	{ LS_TYPE_ASE,          "External" },
     94       1.1  christos 	{ LS_TYPE_GROUP,        "Multicast Group" },
     95       1.1  christos 	{ LS_TYPE_NSSA,         "NSSA" },
     96       1.1  christos 	{ LS_TYPE_OPAQUE_LL,    "Link Local Opaque" },
     97       1.1  christos 	{ LS_TYPE_OPAQUE_AL,    "Area Local Opaque" },
     98       1.1  christos 	{ LS_TYPE_OPAQUE_DW,    "Domain Wide Opaque" },
     99       1.1  christos 	{ 0,			NULL }
    100       1.1  christos };
    101       1.1  christos 
    102       1.1  christos static struct tok ospf_dd_flag_values[] = {
    103       1.1  christos 	{ OSPF_DB_INIT,	        "Init" },
    104       1.1  christos 	{ OSPF_DB_MORE,	        "More" },
    105       1.1  christos 	{ OSPF_DB_MASTER,	"Master" },
    106       1.1  christos     { OSPF_DB_RESYNC,	"OOBResync" },
    107       1.1  christos 	{ 0,			NULL }
    108       1.1  christos };
    109       1.1  christos 
    110       1.1  christos static struct tok lsa_opaque_values[] = {
    111       1.1  christos 	{ LS_OPAQUE_TYPE_TE,    "Traffic Engineering" },
    112       1.1  christos 	{ LS_OPAQUE_TYPE_GRACE, "Graceful restart" },
    113       1.1  christos 	{ LS_OPAQUE_TYPE_RI,    "Router Information" },
    114       1.1  christos 	{ 0,			NULL }
    115       1.1  christos };
    116       1.1  christos 
    117       1.1  christos static struct tok lsa_opaque_te_tlv_values[] = {
    118       1.1  christos 	{ LS_OPAQUE_TE_TLV_ROUTER, "Router Address" },
    119       1.1  christos 	{ LS_OPAQUE_TE_TLV_LINK,   "Link" },
    120       1.1  christos 	{ 0,			NULL }
    121       1.1  christos };
    122       1.1  christos 
    123       1.1  christos static struct tok lsa_opaque_te_link_tlv_subtlv_values[] = {
    124       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_LINK_TYPE,            "Link Type" },
    125       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_LINK_ID,              "Link ID" },
    126       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_LOCAL_IP,             "Local Interface IP address" },
    127       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_REMOTE_IP,            "Remote Interface IP address" },
    128       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_TE_METRIC,            "Traffic Engineering Metric" },
    129       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_MAX_BW,               "Maximum Bandwidth" },
    130       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_MAX_RES_BW,           "Maximum Reservable Bandwidth" },
    131       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_UNRES_BW,             "Unreserved Bandwidth" },
    132       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_ADMIN_GROUP,          "Administrative Group" },
    133       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_LINK_LOCAL_REMOTE_ID, "Link Local/Remote Identifier" },
    134       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_LINK_PROTECTION_TYPE, "Link Protection Type" },
    135       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_INTF_SW_CAP_DESCR,    "Interface Switching Capability" },
    136       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_SHARED_RISK_GROUP,    "Shared Risk Link Group" },
    137       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_BW_CONSTRAINTS,       "Bandwidth Constraints" },
    138       1.1  christos 	{ 0,			NULL }
    139       1.1  christos };
    140       1.1  christos 
    141       1.1  christos static struct tok lsa_opaque_grace_tlv_values[] = {
    142       1.1  christos 	{ LS_OPAQUE_GRACE_TLV_PERIOD,             "Grace Period" },
    143       1.1  christos 	{ LS_OPAQUE_GRACE_TLV_REASON,             "Graceful restart Reason" },
    144       1.1  christos 	{ LS_OPAQUE_GRACE_TLV_INT_ADDRESS,        "IPv4 interface address" },
    145       1.1  christos 	{ 0,		        NULL }
    146       1.1  christos };
    147       1.1  christos 
    148       1.1  christos static struct tok lsa_opaque_grace_tlv_reason_values[] = {
    149       1.1  christos 	{ LS_OPAQUE_GRACE_TLV_REASON_UNKNOWN,     "Unknown" },
    150       1.1  christos 	{ LS_OPAQUE_GRACE_TLV_REASON_SW_RESTART,  "Software Restart" },
    151       1.1  christos 	{ LS_OPAQUE_GRACE_TLV_REASON_SW_UPGRADE,  "Software Reload/Upgrade" },
    152       1.1  christos 	{ LS_OPAQUE_GRACE_TLV_REASON_CP_SWITCH,   "Control Processor Switch" },
    153       1.1  christos 	{ 0,		        NULL }
    154       1.1  christos };
    155       1.1  christos 
    156       1.1  christos static struct tok lsa_opaque_te_tlv_link_type_sub_tlv_values[] = {
    157       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_LINK_TYPE_PTP, "Point-to-point" },
    158       1.1  christos 	{ LS_OPAQUE_TE_LINK_SUBTLV_LINK_TYPE_MA,  "Multi-Access" },
    159       1.1  christos 	{ 0,			NULL }
    160       1.1  christos };
    161       1.1  christos 
    162       1.1  christos static struct tok lsa_opaque_ri_tlv_values[] = {
    163       1.1  christos 	{ LS_OPAQUE_RI_TLV_CAP, "Router Capabilities" },
    164       1.1  christos 	{ 0,		        NULL }
    165       1.1  christos };
    166       1.1  christos 
    167       1.1  christos static struct tok lsa_opaque_ri_tlv_cap_values[] = {
    168       1.1  christos 	{ 1, "Reserved" },
    169       1.1  christos 	{ 2, "Reserved" },
    170       1.1  christos 	{ 4, "Reserved" },
    171       1.1  christos 	{ 8, "Reserved" },
    172       1.1  christos 	{ 16, "graceful restart capable" },
    173       1.1  christos 	{ 32, "graceful restart helper" },
    174       1.1  christos 	{ 64, "Stub router support" },
    175       1.1  christos 	{ 128, "Traffic engineering" },
    176       1.1  christos 	{ 256, "p2p over LAN" },
    177       1.1  christos 	{ 512, "path computation server" },
    178       1.1  christos 	{ 0,		        NULL }
    179       1.1  christos };
    180       1.1  christos 
    181       1.1  christos static struct tok ospf_lls_tlv_values[] = {
    182       1.1  christos 	{ OSPF_LLS_EO,	"Extended Options" },
    183       1.1  christos 	{ OSPF_LLS_MD5,	"MD5 Authentication" },
    184       1.1  christos 	{ 0,	NULL }
    185       1.1  christos };
    186       1.1  christos 
    187       1.1  christos static struct tok ospf_lls_eo_options[] = {
    188       1.1  christos 	{ OSPF_LLS_EO_LR,	"LSDB resync" },
    189       1.1  christos 	{ OSPF_LLS_EO_RS,	"Restart" },
    190       1.1  christos 	{ 0,	NULL }
    191       1.1  christos };
    192       1.1  christos 
    193       1.1  christos static char tstr[] = " [|ospf2]";
    194       1.1  christos 
    195       1.1  christos #ifdef WIN32
    196       1.1  christos #define inline __inline
    197       1.1  christos #endif /* WIN32 */
    198       1.1  christos 
    199       1.1  christos static int ospf_print_lshdr(const struct lsa_hdr *);
    200       1.1  christos static const u_char *ospf_print_lsa(const struct lsa *);
    201       1.1  christos static int ospf_decode_v2(const struct ospfhdr *, const u_char *);
    202       1.1  christos static int ospf_decode_lls(const struct ospfhdr *, register u_int);
    203       1.1  christos 
    204       1.1  christos int
    205  1.2.12.1       tls ospf_print_grace_lsa (const u_int8_t *tptr, u_int ls_length) {
    206       1.1  christos 
    207       1.1  christos     u_int tlv_type, tlv_length;
    208       1.1  christos 
    209       1.1  christos 
    210       1.1  christos     while (ls_length > 0) {
    211       1.1  christos         TCHECK2(*tptr, 4);
    212       1.1  christos         if (ls_length < 4) {
    213       1.1  christos             printf("\n\t    Remaining LS length %u < 4", ls_length);
    214       1.1  christos             return -1;
    215       1.1  christos         }
    216       1.1  christos         tlv_type = EXTRACT_16BITS(tptr);
    217       1.1  christos         tlv_length = EXTRACT_16BITS(tptr+2);
    218       1.1  christos         tptr+=4;
    219       1.1  christos         ls_length-=4;
    220       1.1  christos 
    221       1.1  christos         printf("\n\t    %s TLV (%u), length %u, value: ",
    222       1.1  christos                tok2str(lsa_opaque_grace_tlv_values,"unknown",tlv_type),
    223       1.1  christos                tlv_type,
    224       1.1  christos                tlv_length);
    225       1.1  christos 
    226       1.1  christos         if (tlv_length > ls_length) {
    227       1.1  christos             printf("\n\t    Bogus length %u > %u", tlv_length,
    228       1.1  christos                    ls_length);
    229       1.1  christos             return -1;
    230       1.1  christos         }
    231       1.1  christos 
    232       1.1  christos         /* Infinite loop protection. */
    233       1.1  christos         if (tlv_type == 0 || tlv_length ==0) {
    234       1.1  christos             return -1;
    235       1.1  christos         }
    236       1.1  christos 
    237       1.1  christos         TCHECK2(*tptr, tlv_length);
    238       1.1  christos         switch(tlv_type) {
    239       1.1  christos 
    240       1.1  christos         case LS_OPAQUE_GRACE_TLV_PERIOD:
    241       1.1  christos             if (tlv_length != 4) {
    242       1.1  christos                 printf("\n\t    Bogus length %u != 4", tlv_length);
    243       1.1  christos                 return -1;
    244       1.1  christos             }
    245       1.1  christos             printf("%us",EXTRACT_32BITS(tptr));
    246       1.1  christos             break;
    247       1.1  christos 
    248       1.1  christos         case LS_OPAQUE_GRACE_TLV_REASON:
    249       1.1  christos             if (tlv_length != 1) {
    250       1.1  christos                 printf("\n\t    Bogus length %u != 1", tlv_length);
    251       1.1  christos                 return -1;
    252       1.1  christos             }
    253       1.1  christos             printf("%s (%u)",
    254       1.1  christos                    tok2str(lsa_opaque_grace_tlv_reason_values, "Unknown", *tptr),
    255       1.1  christos                    *tptr);
    256       1.1  christos             break;
    257       1.1  christos 
    258       1.1  christos         case LS_OPAQUE_GRACE_TLV_INT_ADDRESS:
    259       1.1  christos             if (tlv_length != 4) {
    260       1.1  christos                 printf("\n\t    Bogus length %u != 4", tlv_length);
    261       1.1  christos                 return -1;
    262       1.1  christos             }
    263       1.1  christos             printf("%s", ipaddr_string(tptr));
    264       1.1  christos             break;
    265       1.1  christos 
    266       1.1  christos         default:
    267       1.1  christos             if (vflag <= 1) {
    268       1.1  christos                 if(!print_unknown_data(tptr,"\n\t      ",tlv_length))
    269       1.1  christos                     return -1;
    270       1.1  christos             }
    271       1.1  christos             break;
    272       1.1  christos 
    273       1.1  christos         }
    274       1.1  christos         /* in OSPF everything has to be 32-bit aligned, including TLVs */
    275       1.1  christos         if (tlv_length%4 != 0)
    276       1.1  christos             tlv_length+=4-(tlv_length%4);
    277       1.1  christos         ls_length-=tlv_length;
    278       1.1  christos         tptr+=tlv_length;
    279       1.1  christos     }
    280       1.1  christos 
    281       1.1  christos     return 0;
    282       1.1  christos trunc:
    283       1.1  christos     return -1;
    284       1.1  christos }
    285       1.1  christos 
    286       1.1  christos int
    287  1.2.12.1       tls ospf_print_te_lsa (const u_int8_t *tptr, u_int ls_length) {
    288       1.1  christos 
    289       1.1  christos     u_int tlv_type, tlv_length, subtlv_type, subtlv_length;
    290       1.1  christos     u_int priority_level, te_class, count_srlg;
    291       1.1  christos     union { /* int to float conversion buffer for several subTLVs */
    292       1.1  christos         float f;
    293       1.1  christos         u_int32_t i;
    294       1.1  christos     } bw;
    295       1.1  christos 
    296       1.1  christos     while (ls_length != 0) {
    297       1.1  christos         TCHECK2(*tptr, 4);
    298       1.1  christos         if (ls_length < 4) {
    299       1.1  christos             printf("\n\t    Remaining LS length %u < 4", ls_length);
    300       1.1  christos             return -1;
    301       1.1  christos         }
    302       1.1  christos         tlv_type = EXTRACT_16BITS(tptr);
    303       1.1  christos         tlv_length = EXTRACT_16BITS(tptr+2);
    304       1.1  christos         tptr+=4;
    305       1.1  christos         ls_length-=4;
    306       1.1  christos 
    307       1.1  christos         printf("\n\t    %s TLV (%u), length: %u",
    308       1.1  christos                tok2str(lsa_opaque_te_tlv_values,"unknown",tlv_type),
    309       1.1  christos                tlv_type,
    310       1.1  christos                tlv_length);
    311       1.1  christos 
    312       1.1  christos         if (tlv_length > ls_length) {
    313       1.1  christos             printf("\n\t    Bogus length %u > %u", tlv_length,
    314       1.1  christos                    ls_length);
    315       1.1  christos             return -1;
    316       1.1  christos         }
    317       1.1  christos 
    318       1.1  christos         /* Infinite loop protection. */
    319       1.1  christos         if (tlv_type == 0 || tlv_length ==0) {
    320       1.1  christos             return -1;
    321       1.1  christos         }
    322       1.1  christos 
    323       1.1  christos         switch(tlv_type) {
    324       1.1  christos         case LS_OPAQUE_TE_TLV_LINK:
    325       1.1  christos             while (tlv_length >= sizeof(subtlv_type) + sizeof(subtlv_length)) {
    326       1.1  christos                 if (tlv_length < 4) {
    327       1.1  christos                     printf("\n\t    Remaining TLV length %u < 4",
    328       1.1  christos                            tlv_length);
    329       1.1  christos                     return -1;
    330       1.1  christos                 }
    331       1.1  christos                 TCHECK2(*tptr, 4);
    332       1.1  christos                 subtlv_type = EXTRACT_16BITS(tptr);
    333       1.1  christos                 subtlv_length = EXTRACT_16BITS(tptr+2);
    334       1.1  christos                 tptr+=4;
    335       1.1  christos                 tlv_length-=4;
    336       1.1  christos 
    337       1.1  christos                 printf("\n\t      %s subTLV (%u), length: %u",
    338       1.1  christos                        tok2str(lsa_opaque_te_link_tlv_subtlv_values,"unknown",subtlv_type),
    339       1.1  christos                        subtlv_type,
    340       1.1  christos                        subtlv_length);
    341       1.1  christos 
    342       1.1  christos                 TCHECK2(*tptr, subtlv_length);
    343       1.1  christos                 switch(subtlv_type) {
    344       1.1  christos                 case LS_OPAQUE_TE_LINK_SUBTLV_ADMIN_GROUP:
    345       1.1  christos                     printf(", 0x%08x", EXTRACT_32BITS(tptr));
    346       1.1  christos                     break;
    347       1.1  christos                 case LS_OPAQUE_TE_LINK_SUBTLV_LINK_ID:
    348       1.1  christos                 case LS_OPAQUE_TE_LINK_SUBTLV_LINK_LOCAL_REMOTE_ID:
    349       1.1  christos                     printf(", %s (0x%08x)",
    350       1.1  christos                            ipaddr_string(tptr),
    351       1.1  christos                            EXTRACT_32BITS(tptr));
    352       1.1  christos                     if (subtlv_length == 8) /* rfc4203 */
    353       1.1  christos                         printf(", %s (0x%08x)",
    354       1.1  christos                                ipaddr_string(tptr+4),
    355       1.1  christos                                EXTRACT_32BITS(tptr+4));
    356       1.1  christos                     break;
    357       1.1  christos                 case LS_OPAQUE_TE_LINK_SUBTLV_LOCAL_IP:
    358       1.1  christos                 case LS_OPAQUE_TE_LINK_SUBTLV_REMOTE_IP:
    359       1.1  christos                     printf(", %s", ipaddr_string(tptr));
    360       1.1  christos                     break;
    361       1.1  christos                 case LS_OPAQUE_TE_LINK_SUBTLV_MAX_BW:
    362       1.1  christos                 case LS_OPAQUE_TE_LINK_SUBTLV_MAX_RES_BW:
    363       1.1  christos                     bw.i = EXTRACT_32BITS(tptr);
    364       1.1  christos                     printf(", %.3f Mbps", bw.f*8/1000000 );
    365       1.1  christos                     break;
    366       1.1  christos                 case LS_OPAQUE_TE_LINK_SUBTLV_UNRES_BW:
    367       1.1  christos                     for (te_class = 0; te_class < 8; te_class++) {
    368       1.1  christos                         bw.i = EXTRACT_32BITS(tptr+te_class*4);
    369       1.1  christos                         printf("\n\t\tTE-Class %u: %.3f Mbps",
    370       1.1  christos                                te_class,
    371       1.1  christos                                bw.f*8/1000000 );
    372       1.1  christos                     }
    373       1.1  christos                     break;
    374       1.1  christos                 case LS_OPAQUE_TE_LINK_SUBTLV_BW_CONSTRAINTS:
    375       1.1  christos                     printf("\n\t\tBandwidth Constraints Model ID: %s (%u)",
    376       1.1  christos                            tok2str(diffserv_te_bc_values, "unknown", *tptr),
    377       1.1  christos                            *tptr);
    378       1.1  christos                     /* decode BCs until the subTLV ends */
    379       1.1  christos                     for (te_class = 0; te_class < (subtlv_length-4)/4; te_class++) {
    380       1.1  christos                         bw.i = EXTRACT_32BITS(tptr+4+te_class*4);
    381       1.1  christos                         printf("\n\t\t  Bandwidth constraint CT%u: %.3f Mbps",
    382       1.1  christos                                te_class,
    383       1.1  christos                                bw.f*8/1000000 );
    384       1.1  christos                     }
    385       1.1  christos                     break;
    386       1.1  christos                 case LS_OPAQUE_TE_LINK_SUBTLV_TE_METRIC:
    387       1.1  christos                     printf(", Metric %u", EXTRACT_32BITS(tptr));
    388       1.1  christos                     break;
    389       1.1  christos                 case LS_OPAQUE_TE_LINK_SUBTLV_LINK_PROTECTION_TYPE:
    390       1.1  christos                     printf(", %s, Priority %u",
    391       1.1  christos                            bittok2str(gmpls_link_prot_values, "none", *tptr),
    392       1.1  christos                            *(tptr+1));
    393       1.1  christos                     break;
    394       1.1  christos                 case LS_OPAQUE_TE_LINK_SUBTLV_INTF_SW_CAP_DESCR:
    395       1.1  christos                     printf("\n\t\tInterface Switching Capability: %s",
    396       1.1  christos                            tok2str(gmpls_switch_cap_values, "Unknown", *(tptr)));
    397       1.1  christos                     printf("\n\t\tLSP Encoding: %s\n\t\tMax LSP Bandwidth:",
    398       1.1  christos                            tok2str(gmpls_encoding_values, "Unknown", *(tptr+1)));
    399       1.1  christos                     for (priority_level = 0; priority_level < 8; priority_level++) {
    400       1.1  christos                         bw.i = EXTRACT_32BITS(tptr+4+(priority_level*4));
    401       1.1  christos                         printf("\n\t\t  priority level %d: %.3f Mbps",
    402       1.1  christos                                priority_level,
    403       1.1  christos                                bw.f*8/1000000 );
    404       1.1  christos                     }
    405       1.1  christos                     break;
    406       1.1  christos                 case LS_OPAQUE_TE_LINK_SUBTLV_LINK_TYPE:
    407       1.1  christos                     printf(", %s (%u)",
    408       1.1  christos                            tok2str(lsa_opaque_te_tlv_link_type_sub_tlv_values,"unknown",*tptr),
    409       1.1  christos                            *tptr);
    410       1.1  christos                     break;
    411       1.1  christos 
    412       1.1  christos                 case LS_OPAQUE_TE_LINK_SUBTLV_SHARED_RISK_GROUP:
    413       1.1  christos                     count_srlg = subtlv_length / 4;
    414       1.1  christos                     if (count_srlg != 0)
    415       1.1  christos                         printf("\n\t\t  Shared risk group: ");
    416       1.1  christos                     while (count_srlg > 0) {
    417       1.1  christos                         bw.i = EXTRACT_32BITS(tptr);
    418       1.1  christos                         printf("%d",bw.i);
    419       1.1  christos                         tptr+=4;
    420       1.1  christos                         count_srlg--;
    421       1.1  christos                         if (count_srlg > 0)
    422       1.1  christos                             printf(", ");
    423       1.1  christos                     }
    424       1.1  christos                     break;
    425       1.1  christos 
    426       1.1  christos                 default:
    427       1.1  christos                     if (vflag <= 1) {
    428       1.1  christos                         if(!print_unknown_data(tptr,"\n\t\t",subtlv_length))
    429       1.1  christos                             return -1;
    430       1.1  christos                     }
    431       1.1  christos                     break;
    432       1.1  christos                 }
    433       1.1  christos                 /* in OSPF everything has to be 32-bit aligned, including subTLVs */
    434       1.1  christos                 if (subtlv_length%4 != 0)
    435       1.1  christos                     subtlv_length+=4-(subtlv_length%4);
    436       1.1  christos 
    437       1.1  christos                 tlv_length-=subtlv_length;
    438       1.1  christos                 tptr+=subtlv_length;
    439       1.1  christos 
    440       1.1  christos             }
    441       1.1  christos             break;
    442       1.1  christos 
    443       1.1  christos         case LS_OPAQUE_TE_TLV_ROUTER:
    444       1.1  christos             if (tlv_length < 4) {
    445       1.1  christos                 printf("\n\t    TLV length %u < 4", tlv_length);
    446       1.1  christos                 return -1;
    447       1.1  christos             }
    448       1.1  christos             TCHECK2(*tptr, 4);
    449       1.1  christos             printf(", %s", ipaddr_string(tptr));
    450       1.1  christos             break;
    451       1.1  christos 
    452       1.1  christos         default:
    453       1.1  christos             if (vflag <= 1) {
    454       1.1  christos                 if(!print_unknown_data(tptr,"\n\t      ",tlv_length))
    455       1.1  christos                     return -1;
    456       1.1  christos             }
    457       1.1  christos             break;
    458       1.1  christos         }
    459       1.1  christos         /* in OSPF everything has to be 32-bit aligned, including TLVs */
    460       1.1  christos         if (tlv_length%4 != 0)
    461       1.1  christos             tlv_length+=4-(tlv_length%4);
    462       1.1  christos         ls_length-=tlv_length;
    463       1.1  christos         tptr+=tlv_length;
    464       1.1  christos     }
    465       1.1  christos     return 0;
    466       1.1  christos trunc:
    467       1.1  christos     return -1;
    468       1.1  christos }
    469       1.1  christos 
    470       1.1  christos 
    471       1.1  christos static int
    472       1.1  christos ospf_print_lshdr(register const struct lsa_hdr *lshp)
    473       1.1  christos {
    474       1.1  christos         u_int ls_length;
    475       1.1  christos 
    476       1.1  christos         TCHECK(lshp->ls_length);
    477       1.1  christos         ls_length = EXTRACT_16BITS(&lshp->ls_length);
    478       1.1  christos         if (ls_length < sizeof(struct lsa_hdr)) {
    479       1.1  christos                 printf("\n\t    Bogus length %u < header (%lu)", ls_length,
    480       1.1  christos                     (unsigned long)sizeof(struct lsa_hdr));
    481       1.1  christos                 return(-1);
    482       1.1  christos         }
    483       1.1  christos 
    484       1.1  christos         TCHECK(lshp->ls_seq);	/* XXX - ls_length check checked this */
    485       1.1  christos 	printf("\n\t  Advertising Router %s, seq 0x%08x, age %us, length %u",
    486       1.1  christos 	       ipaddr_string(&lshp->ls_router),
    487       1.1  christos 	       EXTRACT_32BITS(&lshp->ls_seq),
    488       1.1  christos 	       EXTRACT_16BITS(&lshp->ls_age),
    489       1.1  christos                ls_length-(u_int)sizeof(struct lsa_hdr));
    490       1.1  christos 
    491       1.1  christos 	TCHECK(lshp->ls_type);	/* XXX - ls_length check checked this */
    492       1.1  christos         switch (lshp->ls_type) {
    493       1.1  christos 	/* the LSA header for opaque LSAs was slightly changed */
    494       1.1  christos         case LS_TYPE_OPAQUE_LL:
    495       1.1  christos         case LS_TYPE_OPAQUE_AL:
    496       1.1  christos         case LS_TYPE_OPAQUE_DW:
    497       1.1  christos             printf("\n\t    %s LSA (%d), Opaque-Type %s LSA (%u), Opaque-ID %u",
    498       1.1  christos                    tok2str(lsa_values,"unknown",lshp->ls_type),
    499       1.1  christos                    lshp->ls_type,
    500       1.1  christos 
    501       1.1  christos 		   tok2str(lsa_opaque_values,
    502       1.1  christos 			   "unknown",
    503       1.1  christos 			   *(&lshp->un_lsa_id.opaque_field.opaque_type)),
    504       1.1  christos 		   *(&lshp->un_lsa_id.opaque_field.opaque_type),
    505       1.1  christos 		   EXTRACT_24BITS(&lshp->un_lsa_id.opaque_field.opaque_id)
    506       1.1  christos 
    507       1.1  christos                    );
    508       1.1  christos             break;
    509       1.1  christos 
    510       1.1  christos 	/* all other LSA types use regular style LSA headers */
    511       1.1  christos 	default:
    512       1.1  christos             printf("\n\t    %s LSA (%d), LSA-ID: %s",
    513       1.1  christos                    tok2str(lsa_values,"unknown",lshp->ls_type),
    514       1.1  christos                    lshp->ls_type,
    515       1.1  christos                    ipaddr_string(&lshp->un_lsa_id.lsa_id));
    516       1.1  christos             break;
    517       1.1  christos         }
    518       1.1  christos 
    519       1.1  christos 	TCHECK(lshp->ls_options);	/* XXX - ls_length check checked this */
    520       1.1  christos         printf("\n\t    Options: [%s]", bittok2str(ospf_option_values,"none",lshp->ls_options));
    521       1.1  christos 
    522       1.1  christos         return (ls_length);
    523       1.1  christos trunc:
    524       1.1  christos 	return (-1);
    525       1.1  christos }
    526       1.1  christos 
    527       1.1  christos /* draft-ietf-ospf-mt-09 */
    528       1.1  christos static struct tok ospf_topology_values[] = {
    529       1.1  christos     { 0, "default " },
    530       1.1  christos     { 1, "multicast " },
    531       1.1  christos     { 2, "management " },
    532       1.1  christos     { 0, NULL }
    533       1.1  christos };
    534       1.1  christos 
    535       1.1  christos /*
    536       1.1  christos  * Print all the per-topology metrics.
    537       1.1  christos  */
    538       1.1  christos static void
    539       1.1  christos ospf_print_tos_metrics(const union un_tos *tos)
    540       1.1  christos {
    541       1.1  christos     int metric_count;
    542       1.1  christos     int toscount;
    543       1.1  christos 
    544       1.1  christos     toscount = tos->link.link_tos_count+1;
    545       1.1  christos     metric_count = 0;
    546       1.1  christos 
    547       1.1  christos     /*
    548       1.1  christos      * All but the first metric contain a valid topology id.
    549       1.1  christos      */
    550       1.1  christos     while (toscount) {
    551       1.1  christos         printf("\n\t\ttopology %s(%u), metric %u",
    552       1.1  christos                tok2str(ospf_topology_values, "",
    553       1.1  christos                        metric_count ? tos->metrics.tos_type : 0),
    554       1.1  christos                metric_count ? tos->metrics.tos_type : 0,
    555       1.1  christos                EXTRACT_16BITS(&tos->metrics.tos_metric));
    556       1.1  christos         metric_count++;
    557       1.1  christos         tos++;
    558       1.1  christos         toscount--;
    559       1.1  christos     }
    560       1.1  christos }
    561       1.1  christos 
    562       1.1  christos /*
    563       1.1  christos  * Print a single link state advertisement.  If truncated or if LSA length
    564       1.1  christos  * field is less than the length of the LSA header, return NULl, else
    565       1.1  christos  * return pointer to data past end of LSA.
    566       1.1  christos  */
    567       1.1  christos static const u_int8_t *
    568       1.1  christos ospf_print_lsa(register const struct lsa *lsap)
    569       1.1  christos {
    570       1.1  christos 	register const u_int8_t *ls_end;
    571       1.1  christos 	register const struct rlalink *rlp;
    572       1.1  christos 	register const struct in_addr *ap;
    573       1.1  christos 	register const struct aslametric *almp;
    574       1.1  christos 	register const struct mcla *mcp;
    575       1.1  christos 	register const u_int32_t *lp;
    576       1.1  christos 	register int j, tlv_type, tlv_length, topology;
    577       1.1  christos 	register int ls_length;
    578       1.1  christos 	const u_int8_t *tptr;
    579       1.1  christos 
    580       1.1  christos 	tptr = (u_int8_t *)lsap->lsa_un.un_unknown; /* squelch compiler warnings */
    581       1.1  christos         ls_length = ospf_print_lshdr(&lsap->ls_hdr);
    582       1.1  christos         if (ls_length == -1)
    583       1.1  christos                 return(NULL);
    584       1.1  christos 	ls_end = (u_int8_t *)lsap + ls_length;
    585       1.1  christos 	ls_length -= sizeof(struct lsa_hdr);
    586       1.1  christos 
    587       1.1  christos 	switch (lsap->ls_hdr.ls_type) {
    588       1.1  christos 
    589       1.1  christos 	case LS_TYPE_ROUTER:
    590       1.1  christos 		TCHECK(lsap->lsa_un.un_rla.rla_flags);
    591       1.1  christos                 printf("\n\t    Router LSA Options: [%s]", bittok2str(ospf_rla_flag_values,"none",lsap->lsa_un.un_rla.rla_flags));
    592       1.1  christos 
    593       1.1  christos 		TCHECK(lsap->lsa_un.un_rla.rla_count);
    594       1.1  christos 		j = EXTRACT_16BITS(&lsap->lsa_un.un_rla.rla_count);
    595       1.1  christos 		TCHECK(lsap->lsa_un.un_rla.rla_link);
    596       1.1  christos 		rlp = lsap->lsa_un.un_rla.rla_link;
    597       1.1  christos 		while (j--) {
    598       1.1  christos 			TCHECK(*rlp);
    599       1.1  christos 			switch (rlp->un_tos.link.link_type) {
    600       1.1  christos 
    601       1.1  christos 			case RLA_TYPE_VIRTUAL:
    602       1.1  christos 				printf("\n\t      Virtual Link: Neighbor Router-ID: %s, Interface Address: %s",
    603       1.1  christos 				    ipaddr_string(&rlp->link_id),
    604       1.1  christos 				    ipaddr_string(&rlp->link_data));
    605       1.1  christos                                 break;
    606       1.1  christos 
    607       1.1  christos 			case RLA_TYPE_ROUTER:
    608       1.1  christos 				printf("\n\t      Neighbor Router-ID: %s, Interface Address: %s",
    609       1.1  christos 				    ipaddr_string(&rlp->link_id),
    610       1.1  christos 				    ipaddr_string(&rlp->link_data));
    611       1.1  christos 				break;
    612       1.1  christos 
    613       1.1  christos 			case RLA_TYPE_TRANSIT:
    614       1.1  christos 				printf("\n\t      Neighbor Network-ID: %s, Interface Address: %s",
    615       1.1  christos 				    ipaddr_string(&rlp->link_id),
    616       1.1  christos 				    ipaddr_string(&rlp->link_data));
    617       1.1  christos 				break;
    618       1.1  christos 
    619       1.1  christos 			case RLA_TYPE_STUB:
    620       1.1  christos 				printf("\n\t      Stub Network: %s, Mask: %s",
    621       1.1  christos 				    ipaddr_string(&rlp->link_id),
    622       1.1  christos 				    ipaddr_string(&rlp->link_data));
    623       1.1  christos 				break;
    624       1.1  christos 
    625       1.1  christos 			default:
    626       1.1  christos 				printf("\n\t      Unknown Router Link Type (%u)",
    627       1.1  christos 				    rlp->un_tos.link.link_type);
    628       1.1  christos 				return (ls_end);
    629       1.1  christos 			}
    630       1.1  christos 
    631       1.1  christos                         ospf_print_tos_metrics(&rlp->un_tos);
    632       1.1  christos 
    633       1.1  christos 			rlp = (struct rlalink *)((u_char *)(rlp + 1) +
    634       1.1  christos 			    ((rlp->un_tos.link.link_tos_count) * sizeof(union un_tos)));
    635       1.1  christos 		}
    636       1.1  christos 		break;
    637       1.1  christos 
    638       1.1  christos 	case LS_TYPE_NETWORK:
    639       1.1  christos 		TCHECK(lsap->lsa_un.un_nla.nla_mask);
    640       1.1  christos 		printf("\n\t    Mask %s\n\t    Connected Routers:",
    641       1.1  christos 		    ipaddr_string(&lsap->lsa_un.un_nla.nla_mask));
    642       1.1  christos 		ap = lsap->lsa_un.un_nla.nla_router;
    643       1.1  christos 		while ((u_char *)ap < ls_end) {
    644       1.1  christos 			TCHECK(*ap);
    645       1.1  christos 			printf("\n\t      %s", ipaddr_string(ap));
    646       1.1  christos 			++ap;
    647       1.1  christos 		}
    648       1.1  christos 		break;
    649       1.1  christos 
    650       1.1  christos 	case LS_TYPE_SUM_IP:
    651       1.1  christos 		TCHECK(lsap->lsa_un.un_nla.nla_mask);
    652       1.1  christos 		printf("\n\t    Mask %s",
    653       1.1  christos 		    ipaddr_string(&lsap->lsa_un.un_sla.sla_mask));
    654       1.1  christos 		TCHECK(lsap->lsa_un.un_sla.sla_tosmetric);
    655       1.1  christos 		lp = lsap->lsa_un.un_sla.sla_tosmetric;
    656       1.1  christos 		while ((u_char *)lp < ls_end) {
    657       1.1  christos 			register u_int32_t ul;
    658       1.1  christos 
    659       1.1  christos 			TCHECK(*lp);
    660       1.1  christos 			ul = EXTRACT_32BITS(lp);
    661       1.1  christos                         topology = (ul & SLA_MASK_TOS) >> SLA_SHIFT_TOS;
    662       1.1  christos 			printf("\n\t\ttopology %s(%u) metric %d",
    663       1.1  christos                                tok2str(ospf_topology_values, "", topology),
    664       1.1  christos                                topology,
    665       1.1  christos                                ul & SLA_MASK_METRIC);
    666       1.1  christos 			++lp;
    667       1.1  christos 		}
    668       1.1  christos 		break;
    669       1.1  christos 
    670       1.1  christos 	case LS_TYPE_SUM_ABR:
    671       1.1  christos 		TCHECK(lsap->lsa_un.un_sla.sla_tosmetric);
    672       1.1  christos 		lp = lsap->lsa_un.un_sla.sla_tosmetric;
    673       1.1  christos 		while ((u_char *)lp < ls_end) {
    674       1.1  christos 			register u_int32_t ul;
    675       1.1  christos 
    676       1.1  christos 			TCHECK(*lp);
    677       1.1  christos 			ul = EXTRACT_32BITS(lp);
    678       1.1  christos                         topology = (ul & SLA_MASK_TOS) >> SLA_SHIFT_TOS;
    679       1.1  christos 			printf("\n\t\ttopology %s(%u) metric %d",
    680       1.1  christos                                tok2str(ospf_topology_values, "", topology),
    681       1.1  christos                                topology,
    682       1.1  christos                                ul & SLA_MASK_METRIC);
    683       1.1  christos 			++lp;
    684       1.1  christos 		}
    685       1.1  christos 		break;
    686       1.1  christos 
    687       1.1  christos 	case LS_TYPE_ASE:
    688       1.1  christos         case LS_TYPE_NSSA: /* fall through - those LSAs share the same format */
    689       1.1  christos 		TCHECK(lsap->lsa_un.un_nla.nla_mask);
    690       1.1  christos 		printf("\n\t    Mask %s",
    691       1.1  christos 		    ipaddr_string(&lsap->lsa_un.un_asla.asla_mask));
    692       1.1  christos 
    693       1.1  christos 		TCHECK(lsap->lsa_un.un_sla.sla_tosmetric);
    694       1.1  christos 		almp = lsap->lsa_un.un_asla.asla_metric;
    695       1.1  christos 		while ((u_char *)almp < ls_end) {
    696       1.1  christos 			register u_int32_t ul;
    697       1.1  christos 
    698       1.1  christos 			TCHECK(almp->asla_tosmetric);
    699       1.1  christos 			ul = EXTRACT_32BITS(&almp->asla_tosmetric);
    700       1.1  christos                         topology = ((ul & ASLA_MASK_TOS) >> ASLA_SHIFT_TOS);
    701       1.1  christos 			printf("\n\t\ttopology %s(%u), type %d, metric",
    702       1.1  christos                                tok2str(ospf_topology_values, "", topology),
    703       1.1  christos                                topology,
    704       1.1  christos                                (ul & ASLA_FLAG_EXTERNAL) ? 2 : 1);
    705       1.1  christos                         if ((ul & ASLA_MASK_METRIC)==0xffffff)
    706       1.1  christos                             printf(" infinite");
    707       1.1  christos                         else
    708       1.1  christos                             printf(" %d", (ul & ASLA_MASK_METRIC));
    709       1.1  christos 
    710       1.1  christos 			TCHECK(almp->asla_forward);
    711       1.1  christos 			if (almp->asla_forward.s_addr) {
    712       1.1  christos 				printf(", forward %s",
    713       1.1  christos 				    ipaddr_string(&almp->asla_forward));
    714       1.1  christos 			}
    715       1.1  christos 			TCHECK(almp->asla_tag);
    716       1.1  christos 			if (almp->asla_tag.s_addr) {
    717       1.1  christos 				printf(", tag %s",
    718       1.1  christos 				    ipaddr_string(&almp->asla_tag));
    719       1.1  christos 			}
    720       1.1  christos 			++almp;
    721       1.1  christos 		}
    722       1.1  christos 		break;
    723       1.1  christos 
    724       1.1  christos 	case LS_TYPE_GROUP:
    725       1.1  christos 		/* Multicast extensions as of 23 July 1991 */
    726       1.1  christos 		mcp = lsap->lsa_un.un_mcla;
    727       1.1  christos 		while ((u_char *)mcp < ls_end) {
    728       1.1  christos 			TCHECK(mcp->mcla_vid);
    729       1.1  christos 			switch (EXTRACT_32BITS(&mcp->mcla_vtype)) {
    730       1.1  christos 
    731       1.1  christos 			case MCLA_VERTEX_ROUTER:
    732       1.1  christos 				printf("\n\t    Router Router-ID %s",
    733       1.1  christos 				    ipaddr_string(&mcp->mcla_vid));
    734       1.1  christos 				break;
    735       1.1  christos 
    736       1.1  christos 			case MCLA_VERTEX_NETWORK:
    737       1.1  christos 				printf("\n\t    Network Designated Router %s",
    738       1.1  christos 				    ipaddr_string(&mcp->mcla_vid));
    739       1.1  christos 				break;
    740       1.1  christos 
    741       1.1  christos 			default:
    742       1.1  christos 				printf("\n\t    unknown VertexType (%u)",
    743       1.1  christos 				    EXTRACT_32BITS(&mcp->mcla_vtype));
    744       1.1  christos 				break;
    745       1.1  christos 			}
    746       1.1  christos 		++mcp;
    747       1.1  christos 		}
    748       1.1  christos 		break;
    749       1.1  christos 
    750       1.1  christos 	case LS_TYPE_OPAQUE_LL: /* fall through */
    751       1.1  christos 	case LS_TYPE_OPAQUE_AL:
    752       1.1  christos 	case LS_TYPE_OPAQUE_DW:
    753       1.1  christos 
    754       1.1  christos 	    switch (*(&lsap->ls_hdr.un_lsa_id.opaque_field.opaque_type)) {
    755       1.1  christos             case LS_OPAQUE_TYPE_RI:
    756       1.1  christos 		tptr = (u_int8_t *)(&lsap->lsa_un.un_ri_tlv.type);
    757       1.1  christos 
    758       1.1  christos 		while (ls_length != 0) {
    759       1.1  christos                     TCHECK2(*tptr, 4);
    760       1.1  christos 		    if (ls_length < 4) {
    761       1.1  christos                         printf("\n\t    Remaining LS length %u < 4", ls_length);
    762       1.1  christos                         return(ls_end);
    763       1.1  christos                     }
    764       1.1  christos                     tlv_type = EXTRACT_16BITS(tptr);
    765       1.1  christos                     tlv_length = EXTRACT_16BITS(tptr+2);
    766       1.1  christos                     tptr+=4;
    767       1.1  christos                     ls_length-=4;
    768       1.1  christos 
    769       1.1  christos                     printf("\n\t    %s TLV (%u), length: %u, value: ",
    770       1.1  christos                            tok2str(lsa_opaque_ri_tlv_values,"unknown",tlv_type),
    771       1.1  christos                            tlv_type,
    772       1.1  christos                            tlv_length);
    773       1.1  christos 
    774       1.1  christos                     if (tlv_length > ls_length) {
    775       1.1  christos                         printf("\n\t    Bogus length %u > %u", tlv_length,
    776       1.1  christos                             ls_length);
    777       1.1  christos                         return(ls_end);
    778       1.1  christos                     }
    779       1.1  christos                     TCHECK2(*tptr, tlv_length);
    780       1.1  christos                     switch(tlv_type) {
    781       1.1  christos 
    782       1.1  christos                     case LS_OPAQUE_RI_TLV_CAP:
    783       1.1  christos                         if (tlv_length != 4) {
    784       1.1  christos                             printf("\n\t    Bogus length %u != 4", tlv_length);
    785       1.1  christos                             return(ls_end);
    786       1.1  christos                         }
    787       1.1  christos                         printf("Capabilities: %s",
    788       1.1  christos                                bittok2str(lsa_opaque_ri_tlv_cap_values, "Unknown", EXTRACT_32BITS(tptr)));
    789       1.1  christos                         break;
    790       1.1  christos                     default:
    791       1.1  christos                         if (vflag <= 1) {
    792       1.1  christos                             if(!print_unknown_data(tptr,"\n\t      ",tlv_length))
    793       1.1  christos                                 return(ls_end);
    794       1.1  christos                         }
    795       1.1  christos                         break;
    796       1.1  christos 
    797       1.1  christos                     }
    798       1.1  christos                     tptr+=tlv_length;
    799       1.1  christos                     ls_length-=tlv_length;
    800       1.1  christos                 }
    801       1.1  christos                 break;
    802       1.1  christos 
    803       1.1  christos             case LS_OPAQUE_TYPE_GRACE:
    804       1.1  christos                 if (ospf_print_grace_lsa((u_int8_t *)(&lsap->lsa_un.un_grace_tlv.type),
    805       1.1  christos                                          ls_length) == -1) {
    806       1.1  christos                     return(ls_end);
    807       1.1  christos                 }
    808       1.1  christos                 break;
    809       1.1  christos 
    810       1.1  christos 	    case LS_OPAQUE_TYPE_TE:
    811       1.1  christos                 if (ospf_print_te_lsa((u_int8_t *)(&lsap->lsa_un.un_te_lsa_tlv.type),
    812       1.1  christos                                       ls_length) == -1) {
    813       1.1  christos                     return(ls_end);
    814       1.1  christos                 }
    815       1.1  christos                 break;
    816       1.1  christos 
    817       1.1  christos             default:
    818       1.1  christos                 if (vflag <= 1) {
    819       1.1  christos                     if(!print_unknown_data((u_int8_t *)lsap->lsa_un.un_unknown,
    820       1.1  christos                                            "\n\t    ", ls_length))
    821       1.1  christos                         return(ls_end);
    822       1.1  christos                 }
    823       1.1  christos                 break;
    824       1.1  christos             }
    825       1.1  christos         }
    826       1.1  christos 
    827       1.1  christos         /* do we want to see an additionally hexdump ? */
    828       1.1  christos         if (vflag> 1)
    829       1.1  christos             if(!print_unknown_data((u_int8_t *)lsap->lsa_un.un_unknown,
    830       1.1  christos                                    "\n\t    ", ls_length)) {
    831       1.1  christos                 return(ls_end);
    832       1.1  christos             }
    833       1.1  christos 
    834       1.1  christos 	return (ls_end);
    835       1.1  christos trunc:
    836       1.1  christos 	return (NULL);
    837       1.1  christos }
    838       1.1  christos 
    839       1.1  christos static int
    840       1.1  christos ospf_decode_lls(register const struct ospfhdr *op,
    841       1.1  christos 		register u_int length)
    842       1.1  christos {
    843       1.1  christos     register const u_char *dptr;
    844       1.1  christos     register const u_char *dataend;
    845       1.1  christos     register u_int length2;
    846       1.1  christos     register u_int16_t lls_type, lls_len;
    847       1.1  christos     register u_int32_t lls_flags;
    848       1.1  christos 
    849       1.1  christos     switch (op->ospf_type) {
    850       1.1  christos 
    851       1.1  christos     case OSPF_TYPE_HELLO:
    852       1.1  christos         if (!(op->ospf_hello.hello_options & OSPF_OPTION_L))
    853       1.1  christos             return (0);
    854       1.1  christos         break;
    855       1.1  christos 
    856       1.1  christos     case OSPF_TYPE_DD:
    857       1.1  christos         if (!(op->ospf_db.db_options & OSPF_OPTION_L))
    858       1.1  christos             return (0);
    859       1.1  christos         break;
    860       1.1  christos 
    861       1.1  christos     default:
    862       1.1  christos         return (0);
    863       1.1  christos     }
    864       1.1  christos 
    865       1.1  christos     /* dig deeper if LLS data is available; see RFC4813 */
    866       1.1  christos     length2 = EXTRACT_16BITS(&op->ospf_len);
    867       1.1  christos     dptr = (u_char *)op + length2;
    868       1.1  christos     dataend = (u_char *)op + length;
    869       1.1  christos 
    870       1.1  christos     if (EXTRACT_16BITS(&op->ospf_authtype) == OSPF_AUTH_MD5) {
    871       1.1  christos         dptr = dptr + op->ospf_authdata[3];
    872       1.1  christos         length2 += op->ospf_authdata[3];
    873       1.1  christos     }
    874       1.1  christos     if (length2 >= length) {
    875       1.1  christos         printf("\n\t[LLS truncated]");
    876       1.1  christos         return (1);
    877       1.1  christos     }
    878       1.1  christos     TCHECK2(*dptr, 2);
    879       1.1  christos     printf("\n\t  LLS: checksum: 0x%04x", (u_int)EXTRACT_16BITS(dptr));
    880       1.1  christos 
    881       1.1  christos     dptr += 2;
    882       1.1  christos     TCHECK2(*dptr, 2);
    883       1.1  christos     length2 = EXTRACT_16BITS(dptr);
    884       1.1  christos     printf(", length: %u", length2);
    885       1.1  christos 
    886       1.1  christos     dptr += 2;
    887       1.1  christos     TCHECK(*dptr);
    888       1.1  christos     while (dptr < dataend) {
    889       1.1  christos         TCHECK2(*dptr, 2);
    890       1.1  christos         lls_type = EXTRACT_16BITS(dptr);
    891       1.1  christos         printf("\n\t    %s (%u)",
    892       1.1  christos                tok2str(ospf_lls_tlv_values,"Unknown TLV",lls_type),
    893       1.1  christos                lls_type);
    894       1.1  christos         dptr += 2;
    895       1.1  christos         TCHECK2(*dptr, 2);
    896       1.1  christos         lls_len = EXTRACT_16BITS(dptr);
    897       1.1  christos         printf(", length: %u", lls_len);
    898       1.1  christos         dptr += 2;
    899       1.1  christos         switch (lls_type) {
    900       1.1  christos 
    901       1.1  christos         case OSPF_LLS_EO:
    902       1.1  christos             if (lls_len != 4) {
    903       1.1  christos                 printf(" [should be 4]");
    904       1.1  christos                 lls_len = 4;
    905       1.1  christos             }
    906       1.1  christos             TCHECK2(*dptr, 4);
    907       1.1  christos             lls_flags = EXTRACT_32BITS(dptr);
    908       1.1  christos             printf("\n\t      Options: 0x%08x [%s]", lls_flags,
    909       1.1  christos                    bittok2str(ospf_lls_eo_options,"?",lls_flags));
    910       1.1  christos 
    911       1.1  christos             break;
    912       1.1  christos 
    913       1.1  christos         case OSPF_LLS_MD5:
    914       1.1  christos             if (lls_len != 20) {
    915       1.1  christos                 printf(" [should be 20]");
    916       1.1  christos                 lls_len = 20;
    917       1.1  christos             }
    918       1.1  christos 			TCHECK2(*dptr, 4);
    919       1.1  christos             printf("\n\t      Sequence number: 0x%08x", EXTRACT_32BITS(dptr));
    920       1.1  christos             break;
    921       1.1  christos         }
    922       1.1  christos 
    923       1.1  christos         dptr += lls_len;
    924       1.1  christos     }
    925       1.1  christos 
    926       1.1  christos     return (0);
    927       1.1  christos trunc:
    928       1.1  christos     return (1);
    929       1.1  christos }
    930       1.1  christos 
    931       1.1  christos static int
    932       1.1  christos ospf_decode_v2(register const struct ospfhdr *op,
    933       1.1  christos     register const u_char *dataend)
    934       1.1  christos {
    935       1.1  christos 	register const struct in_addr *ap;
    936       1.1  christos 	register const struct lsr *lsrp;
    937       1.1  christos 	register const struct lsa_hdr *lshp;
    938       1.1  christos 	register const struct lsa *lsap;
    939       1.1  christos 	register u_int32_t lsa_count,lsa_count_max;
    940       1.1  christos 
    941       1.1  christos 	switch (op->ospf_type) {
    942       1.1  christos 
    943       1.1  christos 	case OSPF_TYPE_UMD:
    944       1.1  christos 		/*
    945       1.1  christos 		 * Rob Coltun's special monitoring packets;
    946       1.1  christos 		 * do nothing
    947       1.1  christos 		 */
    948       1.1  christos 		break;
    949       1.1  christos 
    950       1.1  christos 	case OSPF_TYPE_HELLO:
    951       1.1  christos                 printf("\n\tOptions [%s]",
    952       1.1  christos                        bittok2str(ospf_option_values,"none",op->ospf_hello.hello_options));
    953       1.1  christos 
    954       1.1  christos                 TCHECK(op->ospf_hello.hello_deadint);
    955       1.1  christos                 printf("\n\t  Hello Timer %us, Dead Timer %us, Mask %s, Priority %u",
    956       1.1  christos                        EXTRACT_16BITS(&op->ospf_hello.hello_helloint),
    957       1.1  christos                        EXTRACT_32BITS(&op->ospf_hello.hello_deadint),
    958       1.1  christos                        ipaddr_string(&op->ospf_hello.hello_mask),
    959       1.1  christos                        op->ospf_hello.hello_priority);
    960       1.1  christos 
    961       1.1  christos 		TCHECK(op->ospf_hello.hello_dr);
    962       1.1  christos 		if (op->ospf_hello.hello_dr.s_addr != 0)
    963       1.1  christos 			printf("\n\t  Designated Router %s",
    964       1.1  christos 			    ipaddr_string(&op->ospf_hello.hello_dr));
    965       1.1  christos 
    966       1.1  christos 		TCHECK(op->ospf_hello.hello_bdr);
    967       1.1  christos 		if (op->ospf_hello.hello_bdr.s_addr != 0)
    968       1.1  christos 			printf(", Backup Designated Router %s",
    969       1.1  christos 			    ipaddr_string(&op->ospf_hello.hello_bdr));
    970       1.1  christos 
    971       1.1  christos                 ap = op->ospf_hello.hello_neighbor;
    972       1.1  christos                 if ((u_char *)ap < dataend)
    973       1.1  christos                         printf("\n\t  Neighbor List:");
    974       1.1  christos                 while ((u_char *)ap < dataend) {
    975       1.1  christos                         TCHECK(*ap);
    976       1.1  christos                         printf("\n\t    %s", ipaddr_string(ap));
    977       1.1  christos                         ++ap;
    978       1.1  christos                 }
    979       1.1  christos 		break;	/* HELLO */
    980       1.1  christos 
    981       1.1  christos 	case OSPF_TYPE_DD:
    982       1.1  christos 		TCHECK(op->ospf_db.db_options);
    983       1.1  christos                 printf("\n\tOptions [%s]",
    984       1.1  christos                        bittok2str(ospf_option_values,"none",op->ospf_db.db_options));
    985       1.1  christos 		TCHECK(op->ospf_db.db_flags);
    986       1.1  christos                 printf(", DD Flags [%s]",
    987       1.1  christos                        bittok2str(ospf_dd_flag_values,"none",op->ospf_db.db_flags));
    988       1.1  christos                 TCHECK(op->ospf_db.db_ifmtu);
    989       1.1  christos                 if (op->ospf_db.db_ifmtu) {
    990       1.1  christos                         printf(", MTU: %u", EXTRACT_16BITS(&op->ospf_db.db_ifmtu));
    991       1.1  christos                 }
    992       1.1  christos                 TCHECK(op->ospf_db.db_seq);
    993       1.1  christos                 printf(", Sequence: 0x%08x", EXTRACT_32BITS(&op->ospf_db.db_seq));
    994       1.1  christos 
    995       1.1  christos                 /* Print all the LS adv's */
    996       1.1  christos                 lshp = op->ospf_db.db_lshdr;
    997       1.1  christos                 while (((u_char *)lshp < dataend) && ospf_print_lshdr(lshp) != -1) {
    998       1.1  christos                     ++lshp;
    999       1.1  christos                 }
   1000       1.1  christos 		break;
   1001       1.1  christos 
   1002       1.1  christos 	case OSPF_TYPE_LS_REQ:
   1003       1.1  christos                 lsrp = op->ospf_lsr;
   1004       1.1  christos                 while ((u_char *)lsrp < dataend) {
   1005       1.1  christos                     TCHECK(*lsrp);
   1006       1.1  christos 
   1007       1.1  christos                     printf("\n\t  Advertising Router: %s, %s LSA (%u)",
   1008       1.1  christos                            ipaddr_string(&lsrp->ls_router),
   1009       1.1  christos                            tok2str(lsa_values,"unknown",EXTRACT_32BITS(lsrp->ls_type)),
   1010       1.1  christos                            EXTRACT_32BITS(&lsrp->ls_type));
   1011       1.1  christos 
   1012       1.1  christos                     switch (EXTRACT_32BITS(lsrp->ls_type)) {
   1013       1.1  christos                         /* the LSA header for opaque LSAs was slightly changed */
   1014       1.1  christos                     case LS_TYPE_OPAQUE_LL:
   1015       1.1  christos                     case LS_TYPE_OPAQUE_AL:
   1016       1.1  christos                     case LS_TYPE_OPAQUE_DW:
   1017       1.1  christos                         printf(", Opaque-Type: %s LSA (%u), Opaque-ID: %u",
   1018       1.1  christos                                tok2str(lsa_opaque_values, "unknown",lsrp->un_ls_stateid.opaque_field.opaque_type),
   1019       1.1  christos                                lsrp->un_ls_stateid.opaque_field.opaque_type,
   1020       1.1  christos                                EXTRACT_24BITS(&lsrp->un_ls_stateid.opaque_field.opaque_id));
   1021       1.1  christos                         break;
   1022       1.1  christos                     default:
   1023       1.1  christos                         printf(", LSA-ID: %s",
   1024       1.1  christos                                ipaddr_string(&lsrp->un_ls_stateid.ls_stateid));
   1025       1.1  christos                         break;
   1026       1.1  christos                     }
   1027       1.1  christos 
   1028       1.1  christos                     ++lsrp;
   1029       1.1  christos                 }
   1030       1.1  christos 		break;
   1031       1.1  christos 
   1032       1.1  christos 	case OSPF_TYPE_LS_UPDATE:
   1033       1.1  christos                 lsap = op->ospf_lsu.lsu_lsa;
   1034       1.1  christos                 TCHECK(op->ospf_lsu.lsu_count);
   1035       1.1  christos                 lsa_count_max = EXTRACT_32BITS(&op->ospf_lsu.lsu_count);
   1036  1.2.12.1       tls                 printf(", %d LSA%s",lsa_count_max, PLURAL_SUFFIX(lsa_count_max));
   1037       1.1  christos                 for (lsa_count=1;lsa_count <= lsa_count_max;lsa_count++) {
   1038       1.1  christos                     printf("\n\t  LSA #%u",lsa_count);
   1039       1.1  christos                         lsap = (const struct lsa *)ospf_print_lsa(lsap);
   1040       1.1  christos                         if (lsap == NULL)
   1041       1.1  christos                                 goto trunc;
   1042       1.1  christos                 }
   1043       1.1  christos 		break;
   1044       1.1  christos 
   1045       1.1  christos 	case OSPF_TYPE_LS_ACK:
   1046       1.1  christos                 lshp = op->ospf_lsa.lsa_lshdr;
   1047       1.1  christos                 while (ospf_print_lshdr(lshp) != -1) {
   1048       1.1  christos                     ++lshp;
   1049       1.1  christos                 }
   1050       1.1  christos                 break;
   1051       1.1  christos 
   1052       1.1  christos 	default:
   1053       1.1  christos 		break;
   1054       1.1  christos 	}
   1055       1.1  christos 	return (0);
   1056       1.1  christos trunc:
   1057       1.1  christos 	return (1);
   1058       1.1  christos }
   1059       1.1  christos 
   1060       1.1  christos void
   1061       1.1  christos ospf_print(register const u_char *bp, register u_int length,
   1062       1.1  christos     const u_char *bp2 _U_)
   1063       1.1  christos {
   1064       1.1  christos 	register const struct ospfhdr *op;
   1065       1.1  christos 	register const u_char *dataend;
   1066       1.1  christos 	register const char *cp;
   1067       1.1  christos 
   1068       1.1  christos 	op = (struct ospfhdr *)bp;
   1069       1.1  christos 
   1070       1.1  christos         /* XXX Before we do anything else, strip off the MD5 trailer */
   1071       1.1  christos         TCHECK(op->ospf_authtype);
   1072       1.1  christos         if (EXTRACT_16BITS(&op->ospf_authtype) == OSPF_AUTH_MD5) {
   1073       1.1  christos                 length -= OSPF_AUTH_MD5_LEN;
   1074       1.1  christos                 snapend -= OSPF_AUTH_MD5_LEN;
   1075       1.1  christos         }
   1076       1.1  christos 
   1077       1.1  christos 	/* If the type is valid translate it, or just print the type */
   1078       1.1  christos 	/* value.  If it's not valid, say so and return */
   1079       1.1  christos 	TCHECK(op->ospf_type);
   1080       1.1  christos 	cp = tok2str(type2str, "unknown LS-type", op->ospf_type);
   1081       1.1  christos 	printf("OSPFv%u, %s, length %u",
   1082       1.1  christos 	       op->ospf_version,
   1083       1.1  christos 	       cp,
   1084       1.1  christos 	       length);
   1085       1.1  christos 	if (*cp == 'u')
   1086       1.1  christos 		return;
   1087       1.1  christos 
   1088       1.1  christos         if(!vflag) { /* non verbose - so lets bail out here */
   1089       1.1  christos                 return;
   1090       1.1  christos         }
   1091       1.1  christos 
   1092       1.1  christos 	TCHECK(op->ospf_len);
   1093       1.1  christos 	if (length != EXTRACT_16BITS(&op->ospf_len)) {
   1094       1.1  christos 		printf(" [len %d]", EXTRACT_16BITS(&op->ospf_len));
   1095       1.1  christos 	}
   1096       1.1  christos 
   1097       1.1  christos 	if (length > EXTRACT_16BITS(&op->ospf_len)) {
   1098       1.1  christos 		dataend = bp + EXTRACT_16BITS(&op->ospf_len);
   1099       1.1  christos 	} else {
   1100       1.1  christos 		dataend = bp + length;
   1101       1.1  christos 	}
   1102       1.1  christos 
   1103       1.1  christos 	TCHECK(op->ospf_routerid);
   1104       1.1  christos         printf("\n\tRouter-ID %s", ipaddr_string(&op->ospf_routerid));
   1105       1.1  christos 
   1106       1.1  christos 	TCHECK(op->ospf_areaid);
   1107       1.1  christos 	if (op->ospf_areaid.s_addr != 0)
   1108       1.1  christos 		printf(", Area %s", ipaddr_string(&op->ospf_areaid));
   1109       1.1  christos 	else
   1110       1.1  christos 		printf(", Backbone Area");
   1111       1.1  christos 
   1112       1.1  christos 	if (vflag) {
   1113       1.1  christos 		/* Print authentication data (should we really do this?) */
   1114       1.1  christos 		TCHECK2(op->ospf_authdata[0], sizeof(op->ospf_authdata));
   1115       1.1  christos 
   1116       1.1  christos                 printf(", Authentication Type: %s (%u)",
   1117       1.1  christos                        tok2str(ospf_authtype_values,"unknown",EXTRACT_16BITS(&op->ospf_authtype)),
   1118       1.1  christos                        EXTRACT_16BITS(&op->ospf_authtype));
   1119       1.1  christos 
   1120       1.1  christos 		switch (EXTRACT_16BITS(&op->ospf_authtype)) {
   1121       1.1  christos 
   1122       1.1  christos 		case OSPF_AUTH_NONE:
   1123       1.1  christos 			break;
   1124       1.1  christos 
   1125       1.1  christos 		case OSPF_AUTH_SIMPLE:
   1126       1.1  christos                         printf("\n\tSimple text password: ");
   1127       1.1  christos                         safeputs((const char *)op->ospf_authdata, OSPF_AUTH_SIMPLE_LEN);
   1128       1.1  christos 			break;
   1129       1.1  christos 
   1130       1.1  christos 		case OSPF_AUTH_MD5:
   1131       1.1  christos                         printf("\n\tKey-ID: %u, Auth-Length: %u, Crypto Sequence Number: 0x%08x",
   1132       1.1  christos                                *((op->ospf_authdata)+2),
   1133       1.1  christos                                *((op->ospf_authdata)+3),
   1134       1.1  christos                                EXTRACT_32BITS((op->ospf_authdata)+4));
   1135       1.1  christos 			break;
   1136       1.1  christos 
   1137       1.1  christos 		default:
   1138       1.1  christos 			return;
   1139       1.1  christos 		}
   1140       1.1  christos 	}
   1141       1.1  christos 	/* Do rest according to version.	 */
   1142       1.1  christos 	switch (op->ospf_version) {
   1143       1.1  christos 
   1144       1.1  christos 	case 2:
   1145       1.1  christos 		/* ospf version 2 */
   1146       1.1  christos 		if (ospf_decode_v2(op, dataend))
   1147       1.1  christos 			goto trunc;
   1148       1.1  christos 		if (length > EXTRACT_16BITS(&op->ospf_len)) {
   1149       1.1  christos 			if (ospf_decode_lls(op, length))
   1150       1.1  christos 				goto trunc;
   1151       1.1  christos 		}
   1152       1.1  christos 		break;
   1153       1.1  christos 
   1154       1.1  christos 	default:
   1155       1.1  christos 		printf(" ospf [version %d]", op->ospf_version);
   1156       1.1  christos 		break;
   1157       1.1  christos 	}			/* end switch on version */
   1158       1.1  christos 
   1159       1.1  christos 	return;
   1160       1.1  christos trunc:
   1161       1.1  christos 	fputs(tstr, stdout);
   1162       1.1  christos }
   1163