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