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