xref: /netbsd-src/external/bsd/tcpdump/dist/print-ospf6.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
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@mitchell.cit.cornell.edu)
22  */
23 
24 #include <sys/cdefs.h>
25 #ifndef lint
26 __RCSID("$NetBSD: print-ospf6.c,v 1.11 2023/08/17 20:19:40 christos 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_Nt	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 #define LSA_PREFIX_OPT_N  0x20
112 
113 /* sla_tosmetric breakdown	*/
114 #define	SLA_MASK_TOS		0x7f000000
115 #define	SLA_MASK_METRIC		0x00ffffff
116 #define SLA_SHIFT_TOS		24
117 
118 /* asla_metric */
119 #define ASLA_FLAG_FWDADDR	0x02000000
120 #define ASLA_FLAG_ROUTETAG	0x01000000
121 #define	ASLA_MASK_METRIC	0x00ffffff
122 
123 /* RFC6506 Section 4.1 */
124 #define OSPF6_AT_HDRLEN             16U
125 #define OSPF6_AUTH_TYPE_HMAC        0x0001
126 
127 typedef nd_uint32_t rtrid_t;
128 
129 /* link state advertisement header */
130 struct lsa6_hdr {
131     nd_uint16_t ls_age;
132     nd_uint16_t ls_type;
133     rtrid_t ls_stateid;
134     rtrid_t ls_router;
135     nd_uint32_t ls_seq;
136     nd_uint16_t ls_chksum;
137     nd_uint16_t ls_length;
138 };
139 
140 /* Length of an IPv6 address, in bytes. */
141 #define IPV6_ADDR_LEN_BYTES (128/8)
142 
143 struct lsa6_prefix {
144     nd_uint8_t lsa_p_len;
145     nd_uint8_t lsa_p_opt;
146     nd_uint16_t lsa_p_metric;
147     nd_byte lsa_p_prefix[IPV6_ADDR_LEN_BYTES]; /* maximum length */
148 };
149 
150 /* link state advertisement */
151 struct lsa6 {
152     struct lsa6_hdr ls_hdr;
153 
154     /* Link state types */
155     union {
156 	/* Router links advertisements */
157 	struct {
158 	    union {
159 		nd_uint8_t flg;
160 		nd_uint32_t opt;
161 	    } rla_flgandopt;
162 #define rla_flags	rla_flgandopt.flg
163 #define rla_options	rla_flgandopt.opt
164 	    struct rlalink6 {
165 		nd_uint8_t link_type;
166 		nd_byte link_zero;
167 		nd_uint16_t link_metric;
168 		nd_uint32_t link_ifid;
169 		nd_uint32_t link_nifid;
170 		rtrid_t link_nrtid;
171 	    } rla_link[1];		/* may repeat	*/
172 	} un_rla;
173 
174 	/* Network links advertisements */
175 	struct {
176 	    nd_uint32_t nla_options;
177 	    rtrid_t nla_router[1];	/* may repeat	*/
178 	} un_nla;
179 
180 	/* Inter Area Prefix LSA */
181 	struct {
182 	    nd_uint32_t inter_ap_metric;
183 	    struct lsa6_prefix inter_ap_prefix[1];
184 	} un_inter_ap;
185 
186 	/* AS external links advertisements */
187 	struct {
188 	    nd_uint32_t asla_metric;
189 	    struct lsa6_prefix asla_prefix[1];
190 	    /* some optional fields follow */
191 	} un_asla;
192 
193 #if 0
194 	/* Summary links advertisements */
195 	struct {
196 	    nd_ipv4     sla_mask;
197 	    nd_uint32_t sla_tosmetric[1];	/* may repeat	*/
198 	} un_sla;
199 
200 	/* Multicast group membership */
201 	struct mcla {
202 	    nd_uint32_t mcla_vtype;
203 	    nd_ipv4     mcla_vid;
204 	} un_mcla[1];
205 #endif
206 
207 	/* Type 7 LSA */
208 
209 	/* Link LSA */
210 	struct llsa {
211 	    union {
212 		nd_uint8_t pri;
213 		nd_uint32_t opt;
214 	    } llsa_priandopt;
215 #define llsa_priority	llsa_priandopt.pri
216 #define llsa_options	llsa_priandopt.opt
217 	    nd_ipv6	llsa_lladdr;
218 	    nd_uint32_t llsa_nprefix;
219 	    struct lsa6_prefix llsa_prefix[1];
220 	} un_llsa;
221 
222 	/* Intra-Area-Prefix */
223 	struct {
224 	    nd_uint16_t intra_ap_nprefix;
225 	    nd_uint16_t intra_ap_lstype;
226 	    rtrid_t intra_ap_lsid;
227 	    rtrid_t intra_ap_rtid;
228 	    struct lsa6_prefix intra_ap_prefix[1];
229 	} un_intra_ap;
230     } lsa_un;
231 };
232 
233 /*
234  * the main header
235  */
236 struct ospf6hdr {
237     nd_uint8_t ospf6_version;
238     nd_uint8_t ospf6_type;
239     nd_uint16_t ospf6_len;
240     rtrid_t ospf6_routerid;
241     rtrid_t ospf6_areaid;
242     nd_uint16_t ospf6_chksum;
243     nd_uint8_t ospf6_instanceid;
244     nd_uint8_t ospf6_rsvd;
245 };
246 
247 /*
248  * The OSPF6 header length is 16 bytes, regardless of how your compiler
249  * might choose to pad the above structure.
250  */
251 #define OSPF6HDR_LEN    16
252 
253 /* Hello packet */
254 struct hello6 {
255     nd_uint32_t hello_ifid;
256     union {
257 	nd_uint8_t pri;
258 	nd_uint32_t opt;
259     } hello_priandopt;
260 #define hello_priority	hello_priandopt.pri
261 #define hello_options	hello_priandopt.opt
262     nd_uint16_t hello_helloint;
263     nd_uint16_t hello_deadint;
264     rtrid_t hello_dr;
265     rtrid_t hello_bdr;
266     rtrid_t hello_neighbor[1]; /* may repeat	*/
267 };
268 
269 /* Database Description packet */
270 struct dd6 {
271     nd_uint32_t db_options;
272     nd_uint16_t db_mtu;
273     nd_uint8_t db_mbz;
274     nd_uint8_t db_flags;
275     nd_uint32_t db_seq;
276     struct lsa6_hdr db_lshdr[1]; /* may repeat	*/
277 };
278 
279 /* Link State Request */
280 struct lsr6 {
281     nd_uint16_t ls_mbz;
282     nd_uint16_t ls_type;
283     rtrid_t ls_stateid;
284     rtrid_t ls_router;
285 };
286 
287 /* Link State Update */
288 struct lsu6 {
289     nd_uint32_t lsu_count;
290     struct lsa6 lsu_lsa[1]; /* may repeat	*/
291 };
292 
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,		"Deprecated" },
312 	{ RLA_FLAG_Nt,		"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         { LSA_PREFIX_OPT_N, "N-bit" },
372 	{ 0, NULL }
373 };
374 
375 static const struct tok ospf6_auth_type_str[] = {
376 	{ OSPF6_AUTH_TYPE_HMAC,        "HMAC" },
377 	{ 0, NULL }
378 };
379 
380 static void
381 ospf6_print_ls_type(netdissect_options *ndo,
382                     u_int ls_type, const rtrid_t *ls_stateid)
383 {
384         ND_PRINT("\n\t    %s LSA (%u), %s Scope%s, LSA-ID %s",
385                tok2str(ospf6_lsa_values, "Unknown", ls_type & LS_TYPE_MASK),
386                ls_type & LS_TYPE_MASK,
387                tok2str(ospf6_ls_scope_values, "Unknown", ls_type & LS_SCOPE_MASK),
388                ls_type &0x8000 ? ", transitive" : "", /* U-bit */
389                GET_IPADDR_STRING(ls_stateid));
390 }
391 
392 UNALIGNED_OK
393 static int
394 ospf6_print_lshdr(netdissect_options *ndo,
395                   const struct lsa6_hdr *lshp, const u_char *dataend)
396 {
397 	if ((const u_char *)(lshp + 1) > dataend)
398 		goto trunc;
399 
400 	ND_PRINT("\n\t  Advertising Router %s, seq 0x%08x, age %us, length %zu",
401 		 GET_IPADDR_STRING(lshp->ls_router),
402 		 GET_BE_U_4(lshp->ls_seq),
403 		 GET_BE_U_2(lshp->ls_age),
404 		 GET_BE_U_2(lshp->ls_length)-sizeof(struct lsa6_hdr));
405 
406 	ospf6_print_ls_type(ndo, GET_BE_U_2(lshp->ls_type),
407 			    &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 	nd_ipv6 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_TCHECK_LEN(lsapp, sizeof(*lsapp) - IPV6_ADDR_LEN_BYTES);
426 	wordlen = (GET_U_1(lsapp->lsa_p_len) + 31) / 32;
427 	if (wordlen * 4 > sizeof(nd_ipv6)) {
428 		ND_PRINT(" bogus prefixlen /%u", GET_U_1(lsapp->lsa_p_len));
429 		goto trunc;
430 	}
431 	if (lsa_length < wordlen * 4)
432 		goto trunc;
433 	lsa_length -= wordlen * 4;
434 	memset(prefix, 0, sizeof(prefix));
435 	GET_CPY_BYTES(prefix, lsapp->lsa_p_prefix, wordlen * 4);
436 	ND_PRINT("\n\t\t%s/%u", ip6addr_string(ndo, prefix), /* local buffer, not packet data; don't use GET_IP6ADDR_STRING() */
437 		 GET_U_1(lsapp->lsa_p_len));
438         if (GET_U_1(lsapp->lsa_p_opt)) {
439             ND_PRINT(", Options [%s]",
440                    bittok2str(ospf6_lsa_prefix_option_values,
441                               "none", GET_U_1(lsapp->lsa_p_opt)));
442         }
443         ND_PRINT(", metric %u", GET_BE_U_2(lsapp->lsa_p_metric));
444 	return sizeof(*lsapp) - IPV6_ADDR_LEN_BYTES + wordlen * 4;
445 
446 trunc:
447 	return -1;
448 }
449 
450 
451 /*
452  * Print a single link state advertisement.  If truncated return 1, else 0.
453  */
454 UNALIGNED_OK
455 static int
456 ospf6_print_lsa(netdissect_options *ndo,
457                 const struct lsa6 *lsap, const u_char *dataend)
458 {
459 	const struct rlalink6 *rlp;
460 #if 0
461 	const struct tos_metric *tosp;
462 #endif
463 	const rtrid_t *ap;
464 #if 0
465 	const struct aslametric *almp;
466 	const struct mcla *mcp;
467 #endif
468 	const struct llsa *llsap;
469 	const struct lsa6_prefix *lsapp;
470 #if 0
471 	const uint32_t *lp;
472 #endif
473 	u_int prefixes;
474 	int bytelen;
475 	u_int length, lsa_length;
476 	uint32_t flags32;
477 	const uint8_t *tptr;
478 
479 	if (ospf6_print_lshdr(ndo, &lsap->ls_hdr, dataend))
480 		return (1);
481         length = GET_BE_U_2(lsap->ls_hdr.ls_length);
482 
483 	/*
484 	 * The LSA length includes the length of the header;
485 	 * it must have a value that's at least that length.
486 	 * If it does, find the length of what follows the
487 	 * header.
488 	 */
489         if (length < sizeof(struct lsa6_hdr) || (const u_char *)lsap + length > dataend)
490 		return (1);
491         lsa_length = length - sizeof(struct lsa6_hdr);
492         tptr = (const uint8_t *)lsap+sizeof(struct lsa6_hdr);
493 
494 	switch (GET_BE_U_2(lsap->ls_hdr.ls_type)) {
495 	case LS_TYPE_ROUTER | LS_SCOPE_AREA:
496 		if (lsa_length < sizeof (lsap->lsa_un.un_rla.rla_options))
497 			return (1);
498 		lsa_length -= sizeof (lsap->lsa_un.un_rla.rla_options);
499 		ND_PRINT("\n\t      Options [%s]",
500 		          bittok2str(ospf6_option_values, "none",
501 		          GET_BE_U_4(lsap->lsa_un.un_rla.rla_options)));
502 		ND_PRINT(", RLA-Flags [%s]",
503 		          bittok2str(ospf6_rla_flag_values, "none",
504 		          GET_U_1(lsap->lsa_un.un_rla.rla_flags)));
505 
506 		rlp = lsap->lsa_un.un_rla.rla_link;
507 		while (lsa_length != 0) {
508 			if (lsa_length < sizeof (*rlp))
509 				return (1);
510 			lsa_length -= sizeof (*rlp);
511 			ND_TCHECK_SIZE(rlp);
512 			switch (GET_U_1(rlp->link_type)) {
513 
514 			case RLA_TYPE_VIRTUAL:
515 				ND_PRINT("\n\t      Virtual Link: Neighbor Router-ID %s"
516                                        "\n\t      Neighbor Interface-ID %s, Interface %s",
517                                        GET_IPADDR_STRING(rlp->link_nrtid),
518                                        GET_IPADDR_STRING(rlp->link_nifid),
519                                        GET_IPADDR_STRING(rlp->link_ifid));
520                                 break;
521 
522 			case RLA_TYPE_ROUTER:
523 				ND_PRINT("\n\t      Neighbor Router-ID %s"
524                                        "\n\t      Neighbor Interface-ID %s, Interface %s",
525                                        GET_IPADDR_STRING(rlp->link_nrtid),
526                                        GET_IPADDR_STRING(rlp->link_nifid),
527                                        GET_IPADDR_STRING(rlp->link_ifid));
528 				break;
529 
530 			case RLA_TYPE_TRANSIT:
531 				ND_PRINT("\n\t      Neighbor Network-ID %s"
532                                        "\n\t      Neighbor Interface-ID %s, Interface %s",
533 				    GET_IPADDR_STRING(rlp->link_nrtid),
534 				    GET_IPADDR_STRING(rlp->link_nifid),
535 				    GET_IPADDR_STRING(rlp->link_ifid));
536 				break;
537 
538 			default:
539 				ND_PRINT("\n\t      Unknown Router Links Type 0x%02x",
540 				    GET_U_1(rlp->link_type));
541 				return (0);
542 			}
543 			ND_PRINT(", metric %u", GET_BE_U_2(rlp->link_metric));
544 			rlp++;
545 		}
546 		break;
547 
548 	case LS_TYPE_NETWORK | LS_SCOPE_AREA:
549 		if (lsa_length < sizeof (lsap->lsa_un.un_nla.nla_options))
550 			return (1);
551 		lsa_length -= sizeof (lsap->lsa_un.un_nla.nla_options);
552 		ND_PRINT("\n\t      Options [%s]",
553 		          bittok2str(ospf6_option_values, "none",
554 		          GET_BE_U_4(lsap->lsa_un.un_nla.nla_options)));
555 
556 		ND_PRINT("\n\t      Connected Routers:");
557 		ap = lsap->lsa_un.un_nla.nla_router;
558 		while (lsa_length != 0) {
559 			if (lsa_length < sizeof (*ap))
560 				return (1);
561 			lsa_length -= sizeof (*ap);
562 			ND_PRINT("\n\t\t%s", GET_IPADDR_STRING(ap));
563 			++ap;
564 		}
565 		break;
566 
567 	case LS_TYPE_INTER_AP | LS_SCOPE_AREA:
568 		if (lsa_length < sizeof (lsap->lsa_un.un_inter_ap.inter_ap_metric))
569 			return (1);
570 		lsa_length -= sizeof (lsap->lsa_un.un_inter_ap.inter_ap_metric);
571 		ND_PRINT(", metric %u",
572 			GET_BE_U_4(lsap->lsa_un.un_inter_ap.inter_ap_metric) & SLA_MASK_METRIC);
573 
574 		tptr = (const uint8_t *)lsap->lsa_un.un_inter_ap.inter_ap_prefix;
575 		while (lsa_length != 0) {
576 			bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
577 			if (bytelen < 0)
578 				goto trunc;
579 			/*
580 			 * ospf6_print_lsaprefix() will return -1 if
581 			 * the length is too high, so this will not
582 			 * underflow.
583 			 */
584 			lsa_length -= bytelen;
585 			tptr += bytelen;
586 		}
587 		break;
588 
589 	case LS_TYPE_ASE | LS_SCOPE_AS:
590 		if (lsa_length < sizeof (lsap->lsa_un.un_asla.asla_metric))
591 			return (1);
592 		lsa_length -= sizeof (lsap->lsa_un.un_asla.asla_metric);
593 		flags32 = GET_BE_U_4(lsap->lsa_un.un_asla.asla_metric);
594 		ND_PRINT("\n\t     Flags [%s]",
595 		          bittok2str(ospf6_asla_flag_values, "none", flags32));
596 		ND_PRINT(" metric %u",
597 		       GET_BE_U_4(lsap->lsa_un.un_asla.asla_metric) &
598 		       ASLA_MASK_METRIC);
599 
600 		tptr = (const uint8_t *)lsap->lsa_un.un_asla.asla_prefix;
601 		lsapp = (const struct lsa6_prefix *)tptr;
602 		bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
603 		if (bytelen < 0)
604 			goto trunc;
605 		/*
606 		 * ospf6_print_lsaprefix() will return -1 if
607 		 * the length is too high, so this will not
608 		 * underflow.
609 		 */
610 		lsa_length -= bytelen;
611 		tptr += bytelen;
612 
613 		if ((flags32 & ASLA_FLAG_FWDADDR) != 0) {
614 			if (lsa_length < sizeof (nd_ipv6))
615 				return (1);
616 			lsa_length -= sizeof (nd_ipv6);
617 			ND_PRINT(" forward %s",
618 				 GET_IP6ADDR_STRING(tptr));
619 			tptr += sizeof(nd_ipv6);
620 		}
621 
622 		if ((flags32 & ASLA_FLAG_ROUTETAG) != 0) {
623 			if (lsa_length < sizeof (uint32_t))
624 				return (1);
625 			lsa_length -= sizeof (uint32_t);
626 			ND_PRINT(" tag %s",
627 			       GET_IPADDR_STRING(tptr));
628 			tptr += sizeof(uint32_t);
629 		}
630 
631 		if (GET_U_1(lsapp->lsa_p_metric)) {
632 			if (lsa_length < sizeof (uint32_t))
633 				return (1);
634 			lsa_length -= sizeof (uint32_t);
635 			ND_PRINT(" RefLSID: %s",
636 			       GET_IPADDR_STRING(tptr));
637 			tptr += sizeof(uint32_t);
638 		}
639 		break;
640 
641 	case LS_TYPE_LINK:
642 		/* Link LSA */
643 		llsap = &lsap->lsa_un.un_llsa;
644 		if (lsa_length < sizeof (llsap->llsa_priandopt))
645 			return (1);
646 		lsa_length -= sizeof (llsap->llsa_priandopt);
647 		ND_TCHECK_SIZE(&llsap->llsa_priandopt);
648 		ND_PRINT("\n\t      Options [%s]",
649 		          bittok2str(ospf6_option_values, "none",
650 		          GET_BE_U_4(llsap->llsa_options)));
651 
652 		if (lsa_length < sizeof (llsap->llsa_lladdr) + sizeof (llsap->llsa_nprefix))
653 			return (1);
654 		lsa_length -= sizeof (llsap->llsa_lladdr) + sizeof (llsap->llsa_nprefix);
655                 prefixes = GET_BE_U_4(llsap->llsa_nprefix);
656 		ND_PRINT("\n\t      Priority %u, Link-local address %s, Prefixes %u:",
657                        GET_U_1(llsap->llsa_priority),
658                        GET_IP6ADDR_STRING(llsap->llsa_lladdr),
659                        prefixes);
660 
661 		tptr = (const uint8_t *)llsap->llsa_prefix;
662 		while (prefixes > 0) {
663 			bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
664 			if (bytelen < 0)
665 				goto trunc;
666 			prefixes--;
667 			/*
668 			 * ospf6_print_lsaprefix() will return -1 if
669 			 * the length is too high, so this will not
670 			 * underflow.
671 			 */
672 			lsa_length -= bytelen;
673 			tptr += bytelen;
674 		}
675 		break;
676 
677 	case LS_TYPE_INTRA_AP | LS_SCOPE_AREA:
678 		/* Intra-Area-Prefix LSA */
679 		if (lsa_length < sizeof (lsap->lsa_un.un_intra_ap.intra_ap_rtid))
680 			return (1);
681 		lsa_length -= sizeof (lsap->lsa_un.un_intra_ap.intra_ap_rtid);
682 		ND_TCHECK_4(lsap->lsa_un.un_intra_ap.intra_ap_rtid);
683 		ospf6_print_ls_type(ndo,
684 			GET_BE_U_2(lsap->lsa_un.un_intra_ap.intra_ap_lstype),
685 			&lsap->lsa_un.un_intra_ap.intra_ap_lsid);
686 
687 		if (lsa_length < sizeof (lsap->lsa_un.un_intra_ap.intra_ap_nprefix))
688 			return (1);
689 		lsa_length -= sizeof (lsap->lsa_un.un_intra_ap.intra_ap_nprefix);
690                 prefixes = GET_BE_U_2(lsap->lsa_un.un_intra_ap.intra_ap_nprefix);
691 		ND_PRINT("\n\t      Prefixes %u:", 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 			/*
700 			 * ospf6_print_lsaprefix() will return -1 if
701 			 * the length is too high, so this will not
702 			 * underflow.
703 			 */
704 			lsa_length -= bytelen;
705 			tptr += bytelen;
706 		}
707 		break;
708 
709         case LS_TYPE_GRACE | LS_SCOPE_LINKLOCAL:
710                 if (ospf_grace_lsa_print(ndo, tptr, lsa_length) == -1) {
711                     return 1;
712                 }
713                 break;
714 
715         case LS_TYPE_INTRA_ATE | LS_SCOPE_LINKLOCAL:
716                 if (ospf_te_lsa_print(ndo, tptr, lsa_length) == -1) {
717                     return 1;
718                 }
719                 break;
720 
721 	default:
722                 if(!print_unknown_data(ndo,tptr,
723                                        "\n\t      ",
724                                        lsa_length)) {
725                     return (1);
726                 }
727                 break;
728 	}
729 
730 	return (0);
731 trunc:
732 	return (1);
733 }
734 
735 UNALIGNED_OK
736 static int
737 ospf6_decode_v3(netdissect_options *ndo,
738                 const struct ospf6hdr *op,
739                 const u_char *dataend)
740 {
741 	const rtrid_t *ap;
742 	const struct lsr6 *lsrp;
743 	const struct lsa6_hdr *lshp;
744 	const struct lsa6 *lsap;
745 	int i;
746 
747 	switch (GET_U_1(op->ospf6_type)) {
748 
749 	case OSPF_TYPE_HELLO: {
750 		const struct hello6 *hellop = (const struct hello6 *)((const uint8_t *)op + OSPF6HDR_LEN);
751 
752 		ND_PRINT("\n\tOptions [%s]",
753 		          bittok2str(ospf6_option_values, "none",
754 		          GET_BE_U_4(hellop->hello_options)));
755 
756 		ND_PRINT("\n\t  Hello Timer %us, Dead Timer %us, Interface-ID %s, Priority %u",
757 		          GET_BE_U_2(hellop->hello_helloint),
758 		          GET_BE_U_2(hellop->hello_deadint),
759 		          GET_IPADDR_STRING(hellop->hello_ifid),
760 		          GET_U_1(hellop->hello_priority));
761 
762 		if (GET_BE_U_4(hellop->hello_dr) != 0)
763 			ND_PRINT("\n\t  Designated Router %s",
764 			    GET_IPADDR_STRING(hellop->hello_dr));
765 		if (GET_BE_U_4(hellop->hello_bdr) != 0)
766 			ND_PRINT(", Backup Designated Router %s",
767 			    GET_IPADDR_STRING(hellop->hello_bdr));
768 		if (ndo->ndo_vflag > 1) {
769 			ND_PRINT("\n\t  Neighbor List:");
770 			ap = hellop->hello_neighbor;
771 			while ((const u_char *)ap < dataend) {
772 				ND_PRINT("\n\t    %s", GET_IPADDR_STRING(ap));
773 				++ap;
774 			}
775 		}
776 		break;	/* HELLO */
777 	}
778 
779 	case OSPF_TYPE_DD: {
780 		const struct dd6 *ddp = (const struct dd6 *)((const uint8_t *)op + OSPF6HDR_LEN);
781 
782 		ND_PRINT("\n\tOptions [%s]",
783 		          bittok2str(ospf6_option_values, "none",
784 		          GET_BE_U_4(ddp->db_options)));
785 		ND_PRINT(", DD Flags [%s]",
786 		          bittok2str(ospf6_dd_flag_values,"none",GET_U_1(ddp->db_flags)));
787 
788 		ND_PRINT(", MTU %u, DD-Sequence 0x%08x",
789                        GET_BE_U_2(ddp->db_mtu),
790                        GET_BE_U_4(ddp->db_seq));
791 		if (ndo->ndo_vflag > 1) {
792 			/* Print all the LS adv's */
793 			lshp = ddp->db_lshdr;
794 			while ((const u_char *)lshp < dataend) {
795 				if (ospf6_print_lshdr(ndo, lshp++, dataend))
796 					goto trunc;
797 			}
798 		}
799 		break;
800 	}
801 
802 	case OSPF_TYPE_LS_REQ:
803 		if (ndo->ndo_vflag > 1) {
804 			lsrp = (const struct lsr6 *)((const uint8_t *)op + OSPF6HDR_LEN);
805 			while ((const u_char *)lsrp < dataend) {
806 				ND_TCHECK_SIZE(lsrp);
807 				ND_PRINT("\n\t  Advertising Router %s",
808 				          GET_IPADDR_STRING(lsrp->ls_router));
809 				ospf6_print_ls_type(ndo,
810                                                     GET_BE_U_2(lsrp->ls_type),
811                                                     &lsrp->ls_stateid);
812 				++lsrp;
813 			}
814 		}
815 		break;
816 
817 	case OSPF_TYPE_LS_UPDATE:
818 		if (ndo->ndo_vflag > 1) {
819 			const struct lsu6 *lsup = (const struct lsu6 *)((const uint8_t *)op + OSPF6HDR_LEN);
820 
821 			i = GET_BE_U_4(lsup->lsu_count);
822 			lsap = lsup->lsu_lsa;
823 			while ((const u_char *)lsap < dataend && i--) {
824 				if (ospf6_print_lsa(ndo, lsap, dataend))
825 					goto trunc;
826 				lsap = (const struct lsa6 *)((const u_char *)lsap +
827 				    GET_BE_U_2(lsap->ls_hdr.ls_length));
828 			}
829 		}
830 		break;
831 
832 	case OSPF_TYPE_LS_ACK:
833 		if (ndo->ndo_vflag > 1) {
834 			lshp = (const struct lsa6_hdr *)((const uint8_t *)op + OSPF6HDR_LEN);
835 			while ((const u_char *)lshp < dataend) {
836 				if (ospf6_print_lshdr(ndo, lshp++, dataend))
837 					goto trunc;
838 			}
839 		}
840 		break;
841 
842 	default:
843 		break;
844 	}
845 	return (0);
846 trunc:
847 	return (1);
848 }
849 
850 /* RFC5613 Section 2.2 (w/o the TLVs) */
851 static int
852 ospf6_print_lls(netdissect_options *ndo,
853                 const u_char *cp, const u_int len)
854 {
855 	uint16_t llsdatalen;
856 
857 	if (len == 0)
858 		return 0;
859 	if (len < OSPF_LLS_HDRLEN)
860 		goto trunc;
861 	/* Checksum */
862 	ND_PRINT("\n\tLLS Checksum 0x%04x", GET_BE_U_2(cp));
863 	cp += 2;
864 	/* LLS Data Length */
865 	llsdatalen = GET_BE_U_2(cp);
866 	ND_PRINT(", Data Length %u", llsdatalen);
867 	if (llsdatalen < OSPF_LLS_HDRLEN || llsdatalen > len)
868 		goto trunc;
869 	cp += 2;
870 	/* LLS TLVs */
871 	ND_TCHECK_LEN(cp, llsdatalen - OSPF_LLS_HDRLEN);
872 	/* FIXME: code in print-ospf.c can be reused to decode the TLVs */
873 
874 	return llsdatalen;
875 trunc:
876 	return -1;
877 }
878 
879 /* RFC6506 Section 4.1 */
880 static int
881 ospf6_decode_at(netdissect_options *ndo,
882                 const u_char *cp, const u_int len)
883 {
884 	uint16_t authdatalen;
885 
886 	if (len == 0)
887 		return 0;
888 	if (len < OSPF6_AT_HDRLEN)
889 		goto trunc;
890 	/* Authentication Type */
891 	ND_PRINT("\n\tAuthentication Type %s",
892 		 tok2str(ospf6_auth_type_str, "unknown (0x%04x)", GET_BE_U_2(cp)));
893 	cp += 2;
894 	/* Auth Data Len */
895 	authdatalen = GET_BE_U_2(cp);
896 	ND_PRINT(", Length %u", authdatalen);
897 	if (authdatalen < OSPF6_AT_HDRLEN || authdatalen > len)
898 		goto trunc;
899 	cp += 2;
900 	/* Reserved */
901 	cp += 2;
902 	/* Security Association ID */
903 	ND_PRINT(", SAID %u", GET_BE_U_2(cp));
904 	cp += 2;
905 	/* Cryptographic Sequence Number (High-Order 32 Bits) */
906 	ND_PRINT(", CSN 0x%08x", GET_BE_U_4(cp));
907 	cp += 4;
908 	/* Cryptographic Sequence Number (Low-Order 32 Bits) */
909 	ND_PRINT(":%08x", GET_BE_U_4(cp));
910 	cp += 4;
911 	/* Authentication Data */
912 	ND_TCHECK_LEN(cp, authdatalen - OSPF6_AT_HDRLEN);
913 	if (ndo->ndo_vflag > 1)
914 		print_unknown_data(ndo,cp, "\n\tAuthentication Data ", authdatalen - OSPF6_AT_HDRLEN);
915 	return 0;
916 
917 trunc:
918 	return 1;
919 }
920 
921 /* The trailing data may include LLS and/or AT data (in this specific order).
922  * LLS data may be present only in Hello and DBDesc packets with the L-bit set.
923  * AT data may be present in Hello and DBDesc packets with the AT-bit set or in
924  * any other packet type, thus decode the AT data regardless of the AT-bit.
925  */
926 UNALIGNED_OK
927 static int
928 ospf6_decode_v3_trailer(netdissect_options *ndo,
929                         const struct ospf6hdr *op, const u_char *cp, const unsigned len)
930 {
931 	uint8_t type;
932 	int llslen = 0;
933 	int lls_hello = 0;
934 	int lls_dd = 0;
935 
936 	type = GET_U_1(op->ospf6_type);
937 	if (type == OSPF_TYPE_HELLO) {
938 		const struct hello6 *hellop = (const struct hello6 *)((const uint8_t *)op + OSPF6HDR_LEN);
939 		if (GET_BE_U_4(hellop->hello_options) & OSPF6_OPTION_L)
940 			lls_hello = 1;
941 	} else if (type == OSPF_TYPE_DD) {
942 		const struct dd6 *ddp = (const struct dd6 *)((const uint8_t *)op + OSPF6HDR_LEN);
943 		if (GET_BE_U_4(ddp->db_options) & OSPF6_OPTION_L)
944 			lls_dd = 1;
945 	}
946 	if ((lls_hello || lls_dd) && (llslen = ospf6_print_lls(ndo, cp, len)) < 0)
947 		goto trunc;
948 	return ospf6_decode_at(ndo, cp + llslen, len - llslen);
949 
950 trunc:
951 	return 1;
952 }
953 
954 UNALIGNED_OK
955 void
956 ospf6_print(netdissect_options *ndo,
957             const u_char *bp, u_int length)
958 {
959 	const struct ospf6hdr *op;
960 	const u_char *dataend;
961 	const char *cp;
962 	uint16_t datalen;
963 
964 	ndo->ndo_protocol = "ospf3";
965 	op = (const struct ospf6hdr *)bp;
966 
967 	/* If the type is valid translate it, or just print the type */
968 	/* value.  If it's not valid, say so and return */
969 	cp = tok2str(ospf6_type_values, "unknown packet type (%u)",
970 		     GET_U_1(op->ospf6_type));
971 	ND_PRINT("OSPFv%u, %s, length %u", GET_U_1(op->ospf6_version), cp,
972 		 length);
973 	if (*cp == 'u') {
974 		return;
975 	}
976 
977 	if(!ndo->ndo_vflag) { /* non verbose - so lets bail out here */
978 		return;
979 	}
980 
981 	/* OSPFv3 data always comes first and optional trailing data may follow. */
982 	datalen = GET_BE_U_2(op->ospf6_len);
983 	if (datalen > length) {
984 		ND_PRINT(" [len %u]", datalen);
985 		return;
986 	}
987 	dataend = bp + datalen;
988 
989 	ND_PRINT("\n\tRouter-ID %s", GET_IPADDR_STRING(op->ospf6_routerid));
990 
991 	if (GET_BE_U_4(op->ospf6_areaid) != 0)
992 		ND_PRINT(", Area %s", GET_IPADDR_STRING(op->ospf6_areaid));
993 	else
994 		ND_PRINT(", Backbone Area");
995 	if (GET_U_1(op->ospf6_instanceid))
996 		ND_PRINT(", Instance %u", GET_U_1(op->ospf6_instanceid));
997 
998 	/* Do rest according to version.	 */
999 	switch (GET_U_1(op->ospf6_version)) {
1000 
1001 	case 3:
1002 		/* ospf version 3 */
1003 		if (ospf6_decode_v3(ndo, op, dataend) ||
1004 		    ospf6_decode_v3_trailer(ndo, op, dataend, length - datalen))
1005 			goto trunc;
1006 		break;
1007 	}			/* end switch on version */
1008 
1009 	return;
1010 trunc:
1011 	nd_print_trunc(ndo);
1012 }
1013