Home | History | Annotate | Line # | Download | only in dist
print-ospf6.c revision 1.8.4.1
      1 /*
      2  * Copyright (c) 1992, 1993, 1994, 1995, 1996, 1997
      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  * OSPF support contributed by Jeffrey Honig (jch (at) mitchell.cit.cornell.edu)
     22  */
     23 
     24 #include <sys/cdefs.h>
     25 #ifndef lint
     26 __RCSID("$NetBSD: print-ospf6.c,v 1.8.4.1 2020/04/08 14:04:14 martin Exp $");
     27 #endif
     28 
     29 /* \summary: IPv6 Open Shortest Path First (OSPFv3) printer */
     30 
     31 #ifdef HAVE_CONFIG_H
     32 #include "config.h"
     33 #endif
     34 
     35 #include <netdissect-stdinc.h>
     36 
     37 #include <string.h>
     38 
     39 #include "netdissect.h"
     40 #include "addrtoname.h"
     41 #include "extract.h"
     42 
     43 #include "ospf.h"
     44 
     45 #define	OSPF_TYPE_HELLO         1	/* Hello */
     46 #define	OSPF_TYPE_DD            2	/* Database Description */
     47 #define	OSPF_TYPE_LS_REQ        3	/* Link State Request */
     48 #define	OSPF_TYPE_LS_UPDATE     4	/* Link State Update */
     49 #define	OSPF_TYPE_LS_ACK        5	/* Link State Ack */
     50 
     51 /* Options *_options	*/
     52 #define OSPF6_OPTION_V6	0x01	/* V6 bit: A bit for peeping tom */
     53 #define OSPF6_OPTION_E	0x02	/* E bit: External routes advertised	*/
     54 #define OSPF6_OPTION_MC	0x04	/* MC bit: Multicast capable */
     55 #define OSPF6_OPTION_N	0x08	/* N bit: For type-7 LSA */
     56 #define OSPF6_OPTION_R	0x10	/* R bit: Router bit */
     57 #define OSPF6_OPTION_DC	0x20	/* DC bit: Demand circuits */
     58 /* The field is actually 24-bit (RFC5340 Section A.2). */
     59 #define OSPF6_OPTION_AF	0x0100	/* AF bit: Multiple address families */
     60 #define OSPF6_OPTION_L	0x0200	/* L bit: Link-local signaling (LLS) */
     61 #define OSPF6_OPTION_AT	0x0400	/* AT bit: Authentication trailer */
     62 
     63 
     64 /* db_flags	*/
     65 #define	OSPF6_DB_INIT		0x04	    /*	*/
     66 #define	OSPF6_DB_MORE		0x02
     67 #define	OSPF6_DB_MASTER		0x01
     68 #define	OSPF6_DB_M6		0x10  /* IPv6 MTU */
     69 
     70 /* ls_type	*/
     71 #define	LS_TYPE_ROUTER		1   /* router link */
     72 #define	LS_TYPE_NETWORK		2   /* network link */
     73 #define	LS_TYPE_INTER_AP	3   /* Inter-Area-Prefix */
     74 #define	LS_TYPE_INTER_AR	4   /* Inter-Area-Router */
     75 #define	LS_TYPE_ASE		5   /* ASE */
     76 #define	LS_TYPE_GROUP		6   /* Group membership */
     77 #define	LS_TYPE_NSSA		7   /* NSSA */
     78 #define	LS_TYPE_LINK		8   /* Link LSA */
     79 #define	LS_TYPE_INTRA_AP	9   /* Intra-Area-Prefix */
     80 #define LS_TYPE_INTRA_ATE       10  /* Intra-Area-TE */
     81 #define LS_TYPE_GRACE           11  /* Grace LSA */
     82 #define LS_TYPE_RI		12  /* Router information */
     83 #define LS_TYPE_INTER_ASTE	13  /* Inter-AS-TE */
     84 #define LS_TYPE_L1VPN		14  /* L1VPN */
     85 #define LS_TYPE_MASK		0x1fff
     86 
     87 #define LS_SCOPE_LINKLOCAL	0x0000
     88 #define LS_SCOPE_AREA		0x2000
     89 #define LS_SCOPE_AS		0x4000
     90 #define LS_SCOPE_MASK		0x6000
     91 #define LS_SCOPE_U              0x8000
     92 
     93 /* rla_link.link_type	*/
     94 #define	RLA_TYPE_ROUTER		1   /* point-to-point to another router	*/
     95 #define	RLA_TYPE_TRANSIT	2   /* connection to transit network	*/
     96 #define RLA_TYPE_VIRTUAL	4   /* virtual link			*/
     97 
     98 /* rla_flags	*/
     99 #define	RLA_FLAG_B	0x01
    100 #define	RLA_FLAG_E	0x02
    101 #define	RLA_FLAG_V	0x04
    102 #define	RLA_FLAG_W	0x08
    103 #define RLA_FLAG_N      0x10
    104 
    105 /* lsa_prefix options */
    106 #define LSA_PREFIX_OPT_NU 0x01
    107 #define LSA_PREFIX_OPT_LA 0x02
    108 #define LSA_PREFIX_OPT_MC 0x04
    109 #define LSA_PREFIX_OPT_P  0x08
    110 #define LSA_PREFIX_OPT_DN 0x10
    111 
    112 /* sla_tosmetric breakdown	*/
    113 #define	SLA_MASK_TOS		0x7f000000
    114 #define	SLA_MASK_METRIC		0x00ffffff
    115 #define SLA_SHIFT_TOS		24
    116 
    117 /* asla_metric */
    118 #define ASLA_FLAG_FWDADDR	0x02000000
    119 #define ASLA_FLAG_ROUTETAG	0x01000000
    120 #define	ASLA_MASK_METRIC	0x00ffffff
    121 
    122 /* RFC6506 Section 4.1 */
    123 #define OSPF6_AT_HDRLEN             16U
    124 #define OSPF6_AUTH_TYPE_HMAC        0x0001
    125 
    126 typedef uint32_t rtrid_t;
    127 
    128 /* link state advertisement header */
    129 struct lsa6_hdr {
    130     uint16_t ls_age;
    131     uint16_t ls_type;
    132     rtrid_t ls_stateid;
    133     rtrid_t ls_router;
    134     uint32_t ls_seq;
    135     uint16_t ls_chksum;
    136     uint16_t ls_length;
    137 };
    138 
    139 /* Length of an IPv6 address, in bytes. */
    140 #define IPV6_ADDR_LEN_BYTES (128/8)
    141 
    142 struct lsa6_prefix {
    143     uint8_t lsa_p_len;
    144     uint8_t lsa_p_opt;
    145     uint16_t lsa_p_metric;
    146     uint8_t lsa_p_prefix[IPV6_ADDR_LEN_BYTES]; /* maximum length */
    147 };
    148 
    149 /* link state advertisement */
    150 struct lsa6 {
    151     struct lsa6_hdr ls_hdr;
    152 
    153     /* Link state types */
    154     union {
    155 	/* Router links advertisements */
    156 	struct {
    157 	    union {
    158 		uint8_t flg;
    159 		uint32_t opt;
    160 	    } rla_flgandopt;
    161 #define rla_flags	rla_flgandopt.flg
    162 #define rla_options	rla_flgandopt.opt
    163 	    struct rlalink6 {
    164 		uint8_t link_type;
    165 		uint8_t link_zero[1];
    166 		uint16_t link_metric;
    167 		uint32_t link_ifid;
    168 		uint32_t link_nifid;
    169 		rtrid_t link_nrtid;
    170 	    } rla_link[1];		/* may repeat	*/
    171 	} un_rla;
    172 
    173 	/* Network links advertisements */
    174 	struct {
    175 	    uint32_t nla_options;
    176 	    rtrid_t nla_router[1];	/* may repeat	*/
    177 	} un_nla;
    178 
    179 	/* Inter Area Prefix LSA */
    180 	struct {
    181 	    uint32_t inter_ap_metric;
    182 	    struct lsa6_prefix inter_ap_prefix[1];
    183 	} un_inter_ap;
    184 
    185 	/* AS external links advertisements */
    186 	struct {
    187 	    uint32_t asla_metric;
    188 	    struct lsa6_prefix asla_prefix[1];
    189 	    /* some optional fields follow */
    190 	} un_asla;
    191 
    192 #if 0
    193 	/* Summary links advertisements */
    194 	struct {
    195 	    struct in_addr sla_mask;
    196 	    uint32_t sla_tosmetric[1];	/* may repeat	*/
    197 	} un_sla;
    198 
    199 	/* Multicast group membership */
    200 	struct mcla {
    201 	    uint32_t mcla_vtype;
    202 	    struct in_addr mcla_vid;
    203 	} un_mcla[1];
    204 #endif
    205 
    206 	/* Type 7 LSA */
    207 
    208 	/* Link LSA */
    209 	struct llsa {
    210 	    union {
    211 		uint8_t pri;
    212 		uint32_t opt;
    213 	    } llsa_priandopt;
    214 #define llsa_priority	llsa_priandopt.pri
    215 #define llsa_options	llsa_priandopt.opt
    216 	    struct in6_addr llsa_lladdr;
    217 	    uint32_t llsa_nprefix;
    218 	    struct lsa6_prefix llsa_prefix[1];
    219 	} un_llsa;
    220 
    221 	/* Intra-Area-Prefix */
    222 	struct {
    223 	    uint16_t intra_ap_nprefix;
    224 	    uint16_t intra_ap_lstype;
    225 	    rtrid_t intra_ap_lsid;
    226 	    rtrid_t intra_ap_rtid;
    227 	    struct lsa6_prefix intra_ap_prefix[1];
    228 	} un_intra_ap;
    229     } lsa_un;
    230 };
    231 
    232 /*
    233  * the main header
    234  */
    235 struct ospf6hdr {
    236     uint8_t ospf6_version;
    237     uint8_t ospf6_type;
    238     uint16_t ospf6_len;
    239     rtrid_t ospf6_routerid;
    240     rtrid_t ospf6_areaid;
    241     uint16_t ospf6_chksum;
    242     uint8_t ospf6_instanceid;
    243     uint8_t ospf6_rsvd;
    244 };
    245 
    246 /*
    247  * The OSPF6 header length is 16 bytes, regardless of how your compiler
    248  * might choose to pad the above structure.
    249  */
    250 #define OSPF6HDR_LEN    16
    251 
    252 /* Hello packet */
    253 struct hello6 {
    254     uint32_t hello_ifid;
    255     union {
    256 	uint8_t pri;
    257 	uint32_t opt;
    258     } hello_priandopt;
    259 #define hello_priority	hello_priandopt.pri
    260 #define hello_options	hello_priandopt.opt
    261     uint16_t hello_helloint;
    262     uint16_t hello_deadint;
    263     rtrid_t hello_dr;
    264     rtrid_t hello_bdr;
    265     rtrid_t hello_neighbor[1]; /* may repeat	*/
    266 };
    267 
    268 /* Database Description packet */
    269 struct dd6 {
    270     uint32_t db_options;
    271     uint16_t db_mtu;
    272     uint8_t db_mbz;
    273     uint8_t db_flags;
    274     uint32_t db_seq;
    275     struct lsa6_hdr db_lshdr[1]; /* may repeat	*/
    276 };
    277 
    278 /* Link State Request */
    279 struct lsr6 {
    280     uint16_t ls_mbz;
    281     uint16_t ls_type;
    282     rtrid_t ls_stateid;
    283     rtrid_t ls_router;
    284 };
    285 
    286 /* Link State Update */
    287 struct lsu6 {
    288     uint32_t lsu_count;
    289     struct lsa6 lsu_lsa[1]; /* may repeat	*/
    290 };
    291 
    292 static const char tstr[] = " [|ospf3]";
    293 
    294 static const struct tok ospf6_option_values[] = {
    295 	{ OSPF6_OPTION_V6,	"V6" },
    296 	{ OSPF6_OPTION_E,	"External" },
    297 	{ OSPF6_OPTION_MC,	"Deprecated" },
    298 	{ OSPF6_OPTION_N,	"NSSA" },
    299 	{ OSPF6_OPTION_R,	"Router" },
    300 	{ OSPF6_OPTION_DC,	"Demand Circuit" },
    301 	{ OSPF6_OPTION_AF,	"AFs Support" },
    302 	{ OSPF6_OPTION_L,	"LLS" },
    303 	{ OSPF6_OPTION_AT,	"Authentication Trailer" },
    304 	{ 0,			NULL }
    305 };
    306 
    307 static const struct tok ospf6_rla_flag_values[] = {
    308 	{ RLA_FLAG_B,		"ABR" },
    309 	{ RLA_FLAG_E,		"External" },
    310 	{ RLA_FLAG_V,		"Virtual-Link Endpoint" },
    311 	{ RLA_FLAG_W,		"Wildcard Receiver" },
    312         { RLA_FLAG_N,           "NSSA Translator" },
    313 	{ 0,			NULL }
    314 };
    315 
    316 static const struct tok ospf6_asla_flag_values[] = {
    317 	{ ASLA_FLAG_EXTERNAL,	"External Type 2" },
    318 	{ ASLA_FLAG_FWDADDR,	"Forwarding" },
    319 	{ ASLA_FLAG_ROUTETAG,	"Tag" },
    320 	{ 0,			NULL }
    321 };
    322 
    323 static const struct tok ospf6_type_values[] = {
    324 	{ OSPF_TYPE_HELLO,	"Hello" },
    325 	{ OSPF_TYPE_DD,		"Database Description" },
    326 	{ OSPF_TYPE_LS_REQ,	"LS-Request" },
    327 	{ OSPF_TYPE_LS_UPDATE,	"LS-Update" },
    328 	{ OSPF_TYPE_LS_ACK,	"LS-Ack" },
    329 	{ 0,			NULL }
    330 };
    331 
    332 static const struct tok ospf6_lsa_values[] = {
    333 	{ LS_TYPE_ROUTER,       "Router" },
    334 	{ LS_TYPE_NETWORK,      "Network" },
    335 	{ LS_TYPE_INTER_AP,     "Inter-Area Prefix" },
    336 	{ LS_TYPE_INTER_AR,     "Inter-Area Router" },
    337 	{ LS_TYPE_ASE,          "External" },
    338 	{ LS_TYPE_GROUP,        "Deprecated" },
    339 	{ LS_TYPE_NSSA,         "NSSA" },
    340 	{ LS_TYPE_LINK,         "Link" },
    341 	{ LS_TYPE_INTRA_AP,     "Intra-Area Prefix" },
    342         { LS_TYPE_INTRA_ATE,    "Intra-Area TE" },
    343         { LS_TYPE_GRACE,        "Grace" },
    344 	{ LS_TYPE_RI,           "Router Information" },
    345 	{ LS_TYPE_INTER_ASTE,   "Inter-AS-TE" },
    346 	{ LS_TYPE_L1VPN,        "Layer 1 VPN" },
    347 	{ 0,			NULL }
    348 };
    349 
    350 static const struct tok ospf6_ls_scope_values[] = {
    351 	{ LS_SCOPE_LINKLOCAL,   "Link Local" },
    352 	{ LS_SCOPE_AREA,        "Area Local" },
    353 	{ LS_SCOPE_AS,          "Domain Wide" },
    354 	{ 0,			NULL }
    355 };
    356 
    357 static const struct tok ospf6_dd_flag_values[] = {
    358 	{ OSPF6_DB_INIT,	"Init" },
    359 	{ OSPF6_DB_MORE,	"More" },
    360 	{ OSPF6_DB_MASTER,	"Master" },
    361 	{ OSPF6_DB_M6,		"IPv6 MTU" },
    362 	{ 0,			NULL }
    363 };
    364 
    365 static const struct tok ospf6_lsa_prefix_option_values[] = {
    366         { LSA_PREFIX_OPT_NU, "No Unicast" },
    367         { LSA_PREFIX_OPT_LA, "Local address" },
    368         { LSA_PREFIX_OPT_MC, "Deprecated" },
    369         { LSA_PREFIX_OPT_P, "Propagate" },
    370         { LSA_PREFIX_OPT_DN, "Down" },
    371 	{ 0, NULL }
    372 };
    373 
    374 static const struct tok ospf6_auth_type_str[] = {
    375 	{ OSPF6_AUTH_TYPE_HMAC,        "HMAC" },
    376 	{ 0, NULL }
    377 };
    378 
    379 static void
    380 ospf6_print_ls_type(netdissect_options *ndo,
    381                     register u_int ls_type, register const rtrid_t *ls_stateid)
    382 {
    383         ND_PRINT((ndo, "\n\t    %s LSA (%d), %s Scope%s, LSA-ID %s",
    384                tok2str(ospf6_lsa_values, "Unknown", ls_type & LS_TYPE_MASK),
    385                ls_type & LS_TYPE_MASK,
    386                tok2str(ospf6_ls_scope_values, "Unknown", ls_type & LS_SCOPE_MASK),
    387                ls_type &0x8000 ? ", transitive" : "", /* U-bit */
    388                ipaddr_string(ndo, ls_stateid)));
    389 }
    390 
    391 UNALIGNED_OK
    392 static int
    393 ospf6_print_lshdr(netdissect_options *ndo,
    394                   register const struct lsa6_hdr *lshp, const u_char *dataend)
    395 {
    396 	if ((const u_char *)(lshp + 1) > dataend)
    397 		goto trunc;
    398 	ND_TCHECK(lshp->ls_type);
    399 	ND_TCHECK(lshp->ls_seq);
    400 
    401 	ND_PRINT((ndo, "\n\t  Advertising Router %s, seq 0x%08x, age %us, length %u",
    402                ipaddr_string(ndo, &lshp->ls_router),
    403                EXTRACT_32BITS(&lshp->ls_seq),
    404                EXTRACT_16BITS(&lshp->ls_age),
    405                EXTRACT_16BITS(&lshp->ls_length)-(u_int)sizeof(struct lsa6_hdr)));
    406 
    407 	ospf6_print_ls_type(ndo, EXTRACT_16BITS(&lshp->ls_type), &lshp->ls_stateid);
    408 
    409 	return (0);
    410 trunc:
    411 	return (1);
    412 }
    413 
    414 static int
    415 ospf6_print_lsaprefix(netdissect_options *ndo,
    416                       const uint8_t *tptr, u_int lsa_length)
    417 {
    418 	const struct lsa6_prefix *lsapp = (const struct lsa6_prefix *)tptr;
    419 	u_int wordlen;
    420 	struct in6_addr prefix;
    421 
    422 	if (lsa_length < sizeof (*lsapp) - IPV6_ADDR_LEN_BYTES)
    423 		goto trunc;
    424 	lsa_length -= sizeof (*lsapp) - IPV6_ADDR_LEN_BYTES;
    425 	ND_TCHECK2(*lsapp, sizeof (*lsapp) - IPV6_ADDR_LEN_BYTES);
    426 	wordlen = (lsapp->lsa_p_len + 31) / 32;
    427 	if (wordlen * 4 > sizeof(struct in6_addr)) {
    428 		ND_PRINT((ndo, " bogus prefixlen /%d", lsapp->lsa_p_len));
    429 		goto trunc;
    430 	}
    431 	if (lsa_length < wordlen * 4)
    432 		goto trunc;
    433 	lsa_length -= wordlen * 4;
    434 	ND_TCHECK2(lsapp->lsa_p_prefix, wordlen * 4);
    435 	memset(&prefix, 0, sizeof(prefix));
    436 	memcpy(&prefix, lsapp->lsa_p_prefix, wordlen * 4);
    437 	ND_PRINT((ndo, "\n\t\t%s/%d", ip6addr_string(ndo, &prefix),
    438 		lsapp->lsa_p_len));
    439         if (lsapp->lsa_p_opt) {
    440             ND_PRINT((ndo, ", Options [%s]",
    441                    bittok2str(ospf6_lsa_prefix_option_values,
    442                               "none", lsapp->lsa_p_opt)));
    443         }
    444         ND_PRINT((ndo, ", metric %u", EXTRACT_16BITS(&lsapp->lsa_p_metric)));
    445 	return sizeof(*lsapp) - IPV6_ADDR_LEN_BYTES + wordlen * 4;
    446 
    447 trunc:
    448 	return -1;
    449 }
    450 
    451 
    452 /*
    453  * Print a single link state advertisement.  If truncated return 1, else 0.
    454  */
    455 UNALIGNED_OK
    456 static int
    457 ospf6_print_lsa(netdissect_options *ndo,
    458                 register const struct lsa6 *lsap, const u_char *dataend)
    459 {
    460 	register const struct rlalink6 *rlp;
    461 #if 0
    462 	register const struct tos_metric *tosp;
    463 #endif
    464 	register const rtrid_t *ap;
    465 #if 0
    466 	register const struct aslametric *almp;
    467 	register const struct mcla *mcp;
    468 #endif
    469 	register const struct llsa *llsap;
    470 	register const struct lsa6_prefix *lsapp;
    471 #if 0
    472 	register const uint32_t *lp;
    473 #endif
    474 	register u_int prefixes;
    475 	register int bytelen;
    476 	register u_int length, lsa_length;
    477 	uint32_t flags32;
    478 	const uint8_t *tptr;
    479 
    480 	if (ospf6_print_lshdr(ndo, &lsap->ls_hdr, dataend))
    481 		return (1);
    482 	ND_TCHECK(lsap->ls_hdr.ls_length);
    483         length = EXTRACT_16BITS(&lsap->ls_hdr.ls_length);
    484 
    485 	/*
    486 	 * The LSA length includes the length of the header;
    487 	 * it must have a value that's at least that length.
    488 	 * If it does, find the length of what follows the
    489 	 * header.
    490 	 */
    491         if (length < sizeof(struct lsa6_hdr) || (const u_char *)lsap + length > dataend)
    492         	return (1);
    493         lsa_length = length - sizeof(struct lsa6_hdr);
    494         tptr = (const uint8_t *)lsap+sizeof(struct lsa6_hdr);
    495 
    496 	switch (EXTRACT_16BITS(&lsap->ls_hdr.ls_type)) {
    497 	case LS_TYPE_ROUTER | LS_SCOPE_AREA:
    498 		if (lsa_length < sizeof (lsap->lsa_un.un_rla.rla_options))
    499 			return (1);
    500 		lsa_length -= sizeof (lsap->lsa_un.un_rla.rla_options);
    501 		ND_TCHECK(lsap->lsa_un.un_rla.rla_options);
    502 		ND_PRINT((ndo, "\n\t      Options [%s]",
    503 		          bittok2str(ospf6_option_values, "none",
    504 		          EXTRACT_32BITS(&lsap->lsa_un.un_rla.rla_options))));
    505 		ND_PRINT((ndo, ", RLA-Flags [%s]",
    506 		          bittok2str(ospf6_rla_flag_values, "none",
    507 		          lsap->lsa_un.un_rla.rla_flags)));
    508 
    509 		rlp = lsap->lsa_un.un_rla.rla_link;
    510 		while (lsa_length != 0) {
    511 			if (lsa_length < sizeof (*rlp))
    512 				return (1);
    513 			lsa_length -= sizeof (*rlp);
    514 			ND_TCHECK(*rlp);
    515 			switch (rlp->link_type) {
    516 
    517 			case RLA_TYPE_VIRTUAL:
    518 				ND_PRINT((ndo, "\n\t      Virtual Link: Neighbor Router-ID %s"
    519                                        "\n\t      Neighbor Interface-ID %s, Interface %s",
    520                                        ipaddr_string(ndo, &rlp->link_nrtid),
    521                                        ipaddr_string(ndo, &rlp->link_nifid),
    522                                        ipaddr_string(ndo, &rlp->link_ifid)));
    523                                 break;
    524 
    525 			case RLA_TYPE_ROUTER:
    526 				ND_PRINT((ndo, "\n\t      Neighbor Router-ID %s"
    527                                        "\n\t      Neighbor Interface-ID %s, Interface %s",
    528                                        ipaddr_string(ndo, &rlp->link_nrtid),
    529                                        ipaddr_string(ndo, &rlp->link_nifid),
    530                                        ipaddr_string(ndo, &rlp->link_ifid)));
    531 				break;
    532 
    533 			case RLA_TYPE_TRANSIT:
    534 				ND_PRINT((ndo, "\n\t      Neighbor Network-ID %s"
    535                                        "\n\t      Neighbor Interface-ID %s, Interface %s",
    536 				    ipaddr_string(ndo, &rlp->link_nrtid),
    537 				    ipaddr_string(ndo, &rlp->link_nifid),
    538 				    ipaddr_string(ndo, &rlp->link_ifid)));
    539 				break;
    540 
    541 			default:
    542 				ND_PRINT((ndo, "\n\t      Unknown Router Links Type 0x%02x",
    543 				    rlp->link_type));
    544 				return (0);
    545 			}
    546 			ND_PRINT((ndo, ", metric %d", EXTRACT_16BITS(&rlp->link_metric)));
    547 			rlp++;
    548 		}
    549 		break;
    550 
    551 	case LS_TYPE_NETWORK | LS_SCOPE_AREA:
    552 		if (lsa_length < sizeof (lsap->lsa_un.un_nla.nla_options))
    553 			return (1);
    554 		lsa_length -= sizeof (lsap->lsa_un.un_nla.nla_options);
    555 		ND_TCHECK(lsap->lsa_un.un_nla.nla_options);
    556 		ND_PRINT((ndo, "\n\t      Options [%s]",
    557 		          bittok2str(ospf6_option_values, "none",
    558 		          EXTRACT_32BITS(&lsap->lsa_un.un_nla.nla_options))));
    559 
    560 		ND_PRINT((ndo, "\n\t      Connected Routers:"));
    561 		ap = lsap->lsa_un.un_nla.nla_router;
    562 		while (lsa_length != 0) {
    563 			if (lsa_length < sizeof (*ap))
    564 				return (1);
    565 			lsa_length -= sizeof (*ap);
    566 			ND_TCHECK(*ap);
    567 			ND_PRINT((ndo, "\n\t\t%s", ipaddr_string(ndo, ap)));
    568 			++ap;
    569 		}
    570 		break;
    571 
    572 	case LS_TYPE_INTER_AP | LS_SCOPE_AREA:
    573 		if (lsa_length < sizeof (lsap->lsa_un.un_inter_ap.inter_ap_metric))
    574 			return (1);
    575 		lsa_length -= sizeof (lsap->lsa_un.un_inter_ap.inter_ap_metric);
    576 		ND_TCHECK(lsap->lsa_un.un_inter_ap.inter_ap_metric);
    577 		ND_PRINT((ndo, ", metric %u",
    578 			EXTRACT_32BITS(&lsap->lsa_un.un_inter_ap.inter_ap_metric) & SLA_MASK_METRIC));
    579 
    580 		tptr = (const uint8_t *)lsap->lsa_un.un_inter_ap.inter_ap_prefix;
    581 		while (lsa_length != 0) {
    582 			bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
    583 			if (bytelen < 0)
    584 				goto trunc;
    585 			lsa_length -= bytelen;
    586 			tptr += bytelen;
    587 		}
    588 		break;
    589 
    590 	case LS_TYPE_ASE | LS_SCOPE_AS:
    591 		if (lsa_length < sizeof (lsap->lsa_un.un_asla.asla_metric))
    592 			return (1);
    593 		lsa_length -= sizeof (lsap->lsa_un.un_asla.asla_metric);
    594 		ND_TCHECK(lsap->lsa_un.un_asla.asla_metric);
    595 		flags32 = EXTRACT_32BITS(&lsap->lsa_un.un_asla.asla_metric);
    596 		ND_PRINT((ndo, "\n\t     Flags [%s]",
    597 		          bittok2str(ospf6_asla_flag_values, "none", flags32)));
    598 		ND_PRINT((ndo, " metric %u",
    599 		       EXTRACT_32BITS(&lsap->lsa_un.un_asla.asla_metric) &
    600 		       ASLA_MASK_METRIC));
    601 
    602 		tptr = (const uint8_t *)lsap->lsa_un.un_asla.asla_prefix;
    603 		lsapp = (const struct lsa6_prefix *)tptr;
    604 		bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
    605 		if (bytelen < 0)
    606 			goto trunc;
    607 		lsa_length -= bytelen;
    608 		tptr += bytelen;
    609 
    610 		if ((flags32 & ASLA_FLAG_FWDADDR) != 0) {
    611 			const struct in6_addr *fwdaddr6;
    612 
    613 			fwdaddr6 = (const struct in6_addr *)tptr;
    614 			if (lsa_length < sizeof (*fwdaddr6))
    615 				return (1);
    616 			lsa_length -= sizeof (*fwdaddr6);
    617 			ND_TCHECK(*fwdaddr6);
    618 			ND_PRINT((ndo, " forward %s",
    619 			       ip6addr_string(ndo, fwdaddr6)));
    620 			tptr += sizeof(*fwdaddr6);
    621 		}
    622 
    623 		if ((flags32 & ASLA_FLAG_ROUTETAG) != 0) {
    624 			if (lsa_length < sizeof (uint32_t))
    625 				return (1);
    626 			lsa_length -= sizeof (uint32_t);
    627 			ND_TCHECK(*(const uint32_t *)tptr);
    628 			ND_PRINT((ndo, " tag %s",
    629 			       ipaddr_string(ndo, (const uint32_t *)tptr)));
    630 			tptr += sizeof(uint32_t);
    631 		}
    632 
    633 		if (lsapp->lsa_p_metric) {
    634 			if (lsa_length < sizeof (uint32_t))
    635 				return (1);
    636 			lsa_length -= sizeof (uint32_t);
    637 			ND_TCHECK(*(const uint32_t *)tptr);
    638 			ND_PRINT((ndo, " RefLSID: %s",
    639 			       ipaddr_string(ndo, (const uint32_t *)tptr)));
    640 			tptr += sizeof(uint32_t);
    641 		}
    642 		break;
    643 
    644 	case LS_TYPE_LINK:
    645 		/* Link LSA */
    646 		llsap = &lsap->lsa_un.un_llsa;
    647 		if (lsa_length < sizeof (llsap->llsa_priandopt))
    648 			return (1);
    649 		lsa_length -= sizeof (llsap->llsa_priandopt);
    650 		ND_TCHECK(llsap->llsa_priandopt);
    651 		ND_PRINT((ndo, "\n\t      Options [%s]",
    652 		          bittok2str(ospf6_option_values, "none",
    653 		          EXTRACT_32BITS(&llsap->llsa_options))));
    654 
    655 		if (lsa_length < sizeof (llsap->llsa_lladdr) + sizeof (llsap->llsa_nprefix))
    656 			return (1);
    657 		lsa_length -= sizeof (llsap->llsa_lladdr) + sizeof (llsap->llsa_nprefix);
    658                 ND_TCHECK(llsap->llsa_nprefix);
    659                 prefixes = EXTRACT_32BITS(&llsap->llsa_nprefix);
    660 		ND_PRINT((ndo, "\n\t      Priority %d, Link-local address %s, Prefixes %d:",
    661                        llsap->llsa_priority,
    662                        ip6addr_string(ndo, &llsap->llsa_lladdr),
    663                        prefixes));
    664 
    665 		tptr = (const uint8_t *)llsap->llsa_prefix;
    666 		while (prefixes > 0) {
    667 			bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
    668 			if (bytelen < 0)
    669 				goto trunc;
    670 			prefixes--;
    671 			lsa_length -= bytelen;
    672 			tptr += bytelen;
    673 		}
    674 		break;
    675 
    676 	case LS_TYPE_INTRA_AP | LS_SCOPE_AREA:
    677 		/* Intra-Area-Prefix LSA */
    678 		if (lsa_length < sizeof (lsap->lsa_un.un_intra_ap.intra_ap_rtid))
    679 			return (1);
    680 		lsa_length -= sizeof (lsap->lsa_un.un_intra_ap.intra_ap_rtid);
    681 		ND_TCHECK(lsap->lsa_un.un_intra_ap.intra_ap_rtid);
    682 		ospf6_print_ls_type(ndo,
    683 			EXTRACT_16BITS(&lsap->lsa_un.un_intra_ap.intra_ap_lstype),
    684 			&lsap->lsa_un.un_intra_ap.intra_ap_lsid);
    685 
    686 		if (lsa_length < sizeof (lsap->lsa_un.un_intra_ap.intra_ap_nprefix))
    687 			return (1);
    688 		lsa_length -= sizeof (lsap->lsa_un.un_intra_ap.intra_ap_nprefix);
    689 		ND_TCHECK(lsap->lsa_un.un_intra_ap.intra_ap_nprefix);
    690                 prefixes = EXTRACT_16BITS(&lsap->lsa_un.un_intra_ap.intra_ap_nprefix);
    691 		ND_PRINT((ndo, "\n\t      Prefixes %d:", prefixes));
    692 
    693 		tptr = (const uint8_t *)lsap->lsa_un.un_intra_ap.intra_ap_prefix;
    694 		while (prefixes > 0) {
    695 			bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
    696 			if (bytelen < 0)
    697 				goto trunc;
    698 			prefixes--;
    699 			lsa_length -= bytelen;
    700 			tptr += bytelen;
    701 		}
    702 		break;
    703 
    704         case LS_TYPE_GRACE | LS_SCOPE_LINKLOCAL:
    705                 if (ospf_print_grace_lsa(ndo, tptr, lsa_length) == -1) {
    706                     return 1;
    707                 }
    708                 break;
    709 
    710         case LS_TYPE_INTRA_ATE | LS_SCOPE_LINKLOCAL:
    711                 if (ospf_print_te_lsa(ndo, tptr, lsa_length) == -1) {
    712                     return 1;
    713                 }
    714                 break;
    715 
    716 	default:
    717                 if(!print_unknown_data(ndo,tptr,
    718                                        "\n\t      ",
    719                                        lsa_length)) {
    720                     return (1);
    721                 }
    722                 break;
    723 	}
    724 
    725 	return (0);
    726 trunc:
    727 	return (1);
    728 }
    729 
    730 UNALIGNED_OK
    731 static int
    732 ospf6_decode_v3(netdissect_options *ndo,
    733                 register const struct ospf6hdr *op,
    734                 register const u_char *dataend)
    735 {
    736 	register const rtrid_t *ap;
    737 	register const struct lsr6 *lsrp;
    738 	register const struct lsa6_hdr *lshp;
    739 	register const struct lsa6 *lsap;
    740 	register int i;
    741 
    742 	switch (op->ospf6_type) {
    743 
    744 	case OSPF_TYPE_HELLO: {
    745 		register const struct hello6 *hellop = (const struct hello6 *)((const uint8_t *)op + OSPF6HDR_LEN);
    746 
    747 		ND_TCHECK_32BITS(&hellop->hello_options);
    748 		ND_PRINT((ndo, "\n\tOptions [%s]",
    749 		          bittok2str(ospf6_option_values, "none",
    750 		          EXTRACT_32BITS(&hellop->hello_options))));
    751 
    752 		ND_TCHECK(hellop->hello_deadint);
    753 		ND_PRINT((ndo, "\n\t  Hello Timer %us, Dead Timer %us, Interface-ID %s, Priority %u",
    754 		          EXTRACT_16BITS(&hellop->hello_helloint),
    755 		          EXTRACT_16BITS(&hellop->hello_deadint),
    756 		          ipaddr_string(ndo, &hellop->hello_ifid),
    757 		          hellop->hello_priority));
    758 
    759 		ND_TCHECK(hellop->hello_dr);
    760 		if (EXTRACT_32BITS(&hellop->hello_dr) != 0)
    761 			ND_PRINT((ndo, "\n\t  Designated Router %s",
    762 			    ipaddr_string(ndo, &hellop->hello_dr)));
    763 		ND_TCHECK(hellop->hello_bdr);
    764 		if (EXTRACT_32BITS(&hellop->hello_bdr) != 0)
    765 			ND_PRINT((ndo, ", Backup Designated Router %s",
    766 			    ipaddr_string(ndo, &hellop->hello_bdr)));
    767 		if (ndo->ndo_vflag > 1) {
    768 			ND_PRINT((ndo, "\n\t  Neighbor List:"));
    769 			ap = hellop->hello_neighbor;
    770 			while ((const u_char *)ap < dataend) {
    771 				ND_TCHECK(*ap);
    772 				ND_PRINT((ndo, "\n\t    %s", ipaddr_string(ndo, ap)));
    773 				++ap;
    774 			}
    775 		}
    776 		break;	/* HELLO */
    777 	}
    778 
    779 	case OSPF_TYPE_DD: {
    780 		register const struct dd6 *ddp = (const struct dd6 *)((const uint8_t *)op + OSPF6HDR_LEN);
    781 
    782 		ND_TCHECK(ddp->db_options);
    783 		ND_PRINT((ndo, "\n\tOptions [%s]",
    784 		          bittok2str(ospf6_option_values, "none",
    785 		          EXTRACT_32BITS(&ddp->db_options))));
    786 		ND_TCHECK(ddp->db_flags);
    787 		ND_PRINT((ndo, ", DD Flags [%s]",
    788 		          bittok2str(ospf6_dd_flag_values,"none",ddp->db_flags)));
    789 
    790 		ND_TCHECK(ddp->db_seq);
    791 		ND_PRINT((ndo, ", MTU %u, DD-Sequence 0x%08x",
    792                        EXTRACT_16BITS(&ddp->db_mtu),
    793                        EXTRACT_32BITS(&ddp->db_seq)));
    794 		if (ndo->ndo_vflag > 1) {
    795 			/* Print all the LS adv's */
    796 			lshp = ddp->db_lshdr;
    797 			while ((const u_char *)lshp < dataend) {
    798 				if (ospf6_print_lshdr(ndo, lshp++, dataend))
    799 					goto trunc;
    800 			}
    801 		}
    802 		break;
    803 	}
    804 
    805 	case OSPF_TYPE_LS_REQ:
    806 		if (ndo->ndo_vflag > 1) {
    807 			lsrp = (const struct lsr6 *)((const uint8_t *)op + OSPF6HDR_LEN);
    808 			while ((const u_char *)lsrp < dataend) {
    809 				ND_TCHECK(*lsrp);
    810 				ND_PRINT((ndo, "\n\t  Advertising Router %s",
    811 				          ipaddr_string(ndo, &lsrp->ls_router)));
    812 				ospf6_print_ls_type(ndo, EXTRACT_16BITS(&lsrp->ls_type),
    813                                                     &lsrp->ls_stateid);
    814 				++lsrp;
    815 			}
    816 		}
    817 		break;
    818 
    819 	case OSPF_TYPE_LS_UPDATE:
    820 		if (ndo->ndo_vflag > 1) {
    821 			register const struct lsu6 *lsup = (const struct lsu6 *)((const uint8_t *)op + OSPF6HDR_LEN);
    822 
    823 			ND_TCHECK(lsup->lsu_count);
    824 			i = EXTRACT_32BITS(&lsup->lsu_count);
    825 			lsap = lsup->lsu_lsa;
    826 			while ((const u_char *)lsap < dataend && i--) {
    827 				if (ospf6_print_lsa(ndo, lsap, dataend))
    828 					goto trunc;
    829 				lsap = (const struct lsa6 *)((const u_char *)lsap +
    830 				    EXTRACT_16BITS(&lsap->ls_hdr.ls_length));
    831 			}
    832 		}
    833 		break;
    834 
    835 	case OSPF_TYPE_LS_ACK:
    836 		if (ndo->ndo_vflag > 1) {
    837 			lshp = (const struct lsa6_hdr *)((const uint8_t *)op + OSPF6HDR_LEN);
    838 			while ((const u_char *)lshp < dataend) {
    839 				if (ospf6_print_lshdr(ndo, lshp++, dataend))
    840 					goto trunc;
    841 			}
    842 		}
    843 		break;
    844 
    845 	default:
    846 		break;
    847 	}
    848 	return (0);
    849 trunc:
    850 	return (1);
    851 }
    852 
    853 /* RFC5613 Section 2.2 (w/o the TLVs) */
    854 static int
    855 ospf6_print_lls(netdissect_options *ndo,
    856                 const u_char *cp, const u_int len)
    857 {
    858 	uint16_t llsdatalen;
    859 
    860 	if (len == 0)
    861 		return 0;
    862 	if (len < OSPF_LLS_HDRLEN)
    863 		goto trunc;
    864 	/* Checksum */
    865 	ND_TCHECK2(*cp, 2);
    866 	ND_PRINT((ndo, "\n\tLLS Checksum 0x%04x", EXTRACT_16BITS(cp)));
    867 	cp += 2;
    868 	/* LLS Data Length */
    869 	ND_TCHECK2(*cp, 2);
    870 	llsdatalen = EXTRACT_16BITS(cp);
    871 	ND_PRINT((ndo, ", Data Length %u", llsdatalen));
    872 	if (llsdatalen < OSPF_LLS_HDRLEN || llsdatalen > len)
    873 		goto trunc;
    874 	cp += 2;
    875 	/* LLS TLVs */
    876 	ND_TCHECK2(*cp, llsdatalen - OSPF_LLS_HDRLEN);
    877 	/* FIXME: code in print-ospf.c can be reused to decode the TLVs */
    878 
    879 	return llsdatalen;
    880 trunc:
    881 	return -1;
    882 }
    883 
    884 /* RFC6506 Section 4.1 */
    885 static int
    886 ospf6_decode_at(netdissect_options *ndo,
    887                 const u_char *cp, const u_int len)
    888 {
    889 	uint16_t authdatalen;
    890 
    891 	if (len == 0)
    892 		return 0;
    893 	if (len < OSPF6_AT_HDRLEN)
    894 		goto trunc;
    895 	/* Authentication Type */
    896 	ND_TCHECK2(*cp, 2);
    897 	ND_PRINT((ndo, "\n\tAuthentication Type %s", tok2str(ospf6_auth_type_str, "unknown (0x%04x)", EXTRACT_16BITS(cp))));
    898 	cp += 2;
    899 	/* Auth Data Len */
    900 	ND_TCHECK2(*cp, 2);
    901 	authdatalen = EXTRACT_16BITS(cp);
    902 	ND_PRINT((ndo, ", Length %u", authdatalen));
    903 	if (authdatalen < OSPF6_AT_HDRLEN || authdatalen > len)
    904 		goto trunc;
    905 	cp += 2;
    906 	/* Reserved */
    907 	ND_TCHECK2(*cp, 2);
    908 	cp += 2;
    909 	/* Security Association ID */
    910 	ND_TCHECK2(*cp, 2);
    911 	ND_PRINT((ndo, ", SAID %u", EXTRACT_16BITS(cp)));
    912 	cp += 2;
    913 	/* Cryptographic Sequence Number (High-Order 32 Bits) */
    914 	ND_TCHECK2(*cp, 4);
    915 	ND_PRINT((ndo, ", CSN 0x%08x", EXTRACT_32BITS(cp)));
    916 	cp += 4;
    917 	/* Cryptographic Sequence Number (Low-Order 32 Bits) */
    918 	ND_TCHECK2(*cp, 4);
    919 	ND_PRINT((ndo, ":%08x", EXTRACT_32BITS(cp)));
    920 	cp += 4;
    921 	/* Authentication Data */
    922 	ND_TCHECK2(*cp, authdatalen - OSPF6_AT_HDRLEN);
    923 	if (ndo->ndo_vflag > 1)
    924 		print_unknown_data(ndo,cp, "\n\tAuthentication Data ", authdatalen - OSPF6_AT_HDRLEN);
    925 	return 0;
    926 
    927 trunc:
    928 	return 1;
    929 }
    930 
    931 /* The trailing data may include LLS and/or AT data (in this specific order).
    932  * LLS data may be present only in Hello and DBDesc packets with the L-bit set.
    933  * AT data may be present in Hello and DBDesc packets with the AT-bit set or in
    934  * any other packet type, thus decode the AT data regardless of the AT-bit.
    935  */
    936 UNALIGNED_OK
    937 static int
    938 ospf6_decode_v3_trailer(netdissect_options *ndo,
    939                         const struct ospf6hdr *op, const u_char *cp, const unsigned len)
    940 {
    941 	int llslen = 0;
    942 	int lls_hello = 0;
    943 	int lls_dd = 0;
    944 
    945 	if (op->ospf6_type == OSPF_TYPE_HELLO) {
    946 		const struct hello6 *hellop = (const struct hello6 *)((const uint8_t *)op + OSPF6HDR_LEN);
    947 		ND_TCHECK(hellop->hello_options);
    948 		if (EXTRACT_32BITS(&hellop->hello_options) & OSPF6_OPTION_L)
    949 			lls_hello = 1;
    950 	} else if (op->ospf6_type == OSPF_TYPE_DD) {
    951 		const struct dd6 *ddp = (const struct dd6 *)((const uint8_t *)op + OSPF6HDR_LEN);
    952 		ND_TCHECK(ddp->db_options);
    953 		if (EXTRACT_32BITS(&ddp->db_options) & OSPF6_OPTION_L)
    954 			lls_dd = 1;
    955 	}
    956 	if ((lls_hello || lls_dd) && (llslen = ospf6_print_lls(ndo, cp, len)) < 0)
    957 		goto trunc;
    958 	return ospf6_decode_at(ndo, cp + llslen, len - llslen);
    959 
    960 trunc:
    961 	return 1;
    962 }
    963 
    964 UNALIGNED_OK
    965 void
    966 ospf6_print(netdissect_options *ndo,
    967             register const u_char *bp, register u_int length)
    968 {
    969 	register const struct ospf6hdr *op;
    970 	register const u_char *dataend;
    971 	register const char *cp;
    972 	uint16_t datalen;
    973 
    974 	op = (const struct ospf6hdr *)bp;
    975 
    976 	/* If the type is valid translate it, or just print the type */
    977 	/* value.  If it's not valid, say so and return */
    978 	ND_TCHECK(op->ospf6_type);
    979 	cp = tok2str(ospf6_type_values, "unknown packet type (%u)", op->ospf6_type);
    980 	ND_PRINT((ndo, "OSPFv%u, %s, length %d", op->ospf6_version, cp, length));
    981 	if (*cp == 'u') {
    982 		return;
    983 	}
    984 
    985 	if(!ndo->ndo_vflag) { /* non verbose - so lets bail out here */
    986 		return;
    987 	}
    988 
    989 	/* OSPFv3 data always comes first and optional trailing data may follow. */
    990 	ND_TCHECK(op->ospf6_len);
    991 	datalen = EXTRACT_16BITS(&op->ospf6_len);
    992 	if (datalen > length) {
    993 		ND_PRINT((ndo, " [len %d]", datalen));
    994 		return;
    995 	}
    996 	dataend = bp + datalen;
    997 
    998 	ND_TCHECK(op->ospf6_routerid);
    999 	ND_PRINT((ndo, "\n\tRouter-ID %s", ipaddr_string(ndo, &op->ospf6_routerid)));
   1000 
   1001 	ND_TCHECK(op->ospf6_areaid);
   1002 	if (EXTRACT_32BITS(&op->ospf6_areaid) != 0)
   1003 		ND_PRINT((ndo, ", Area %s", ipaddr_string(ndo, &op->ospf6_areaid)));
   1004 	else
   1005 		ND_PRINT((ndo, ", Backbone Area"));
   1006 	ND_TCHECK(op->ospf6_instanceid);
   1007 	if (op->ospf6_instanceid)
   1008 		ND_PRINT((ndo, ", Instance %u", op->ospf6_instanceid));
   1009 
   1010 	/* Do rest according to version.	 */
   1011 	switch (op->ospf6_version) {
   1012 
   1013 	case 3:
   1014 		/* ospf version 3 */
   1015 		if (ospf6_decode_v3(ndo, op, dataend) ||
   1016 		    ospf6_decode_v3_trailer(ndo, op, dataend, length - datalen))
   1017 			goto trunc;
   1018 		break;
   1019 	}			/* end switch on version */
   1020 
   1021 	return;
   1022 trunc:
   1023 	ND_PRINT((ndo, "%s", tstr));
   1024 }
   1025