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