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