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