]> The Tcpdump Group git mirrors - tcpdump/blob - print-ospf.c
add better handling for unknown LSAs to OSPF
[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[] =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-ospf.c,v 1.37 2003-10-02 13:35:52 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
41 #include "ospf.h"
42
43 #include "ip.h"
44
45 static struct tok ospf_option_values[] = {
46 { OSPF_OPTION_T, "TOS" },
47 { OSPF_OPTION_E, "External" },
48 { OSPF_OPTION_MC, "Multicast" },
49 { OSPF_OPTION_NP, "NSSA" },
50 { OSPF_OPTION_EA, "Advertise External" },
51 { OSPF_OPTION_DC, "Demand Circuit" },
52 { OSPF_OPTION_O, "Opaque" },
53 { 0, NULL }
54 };
55
56 static struct tok ospf_authtype_values[] = {
57 { OSPF_AUTH_NONE, "none" },
58 { OSPF_AUTH_NONE, "simple" },
59 { OSPF_AUTH_MD5, "MD5" },
60 { 0, NULL }
61 };
62
63 static struct tok ospf_rla_flag_values[] = {
64 { RLA_FLAG_B, "ABR" },
65 { RLA_FLAG_E, "ASBR" },
66 { RLA_FLAG_W1, "Virtual" },
67 { RLA_FLAG_W2, "W2" },
68 { 0, NULL }
69 };
70
71 static struct tok type2str[] = {
72 { OSPF_TYPE_UMD, "UMD" },
73 { OSPF_TYPE_HELLO, "Hello" },
74 { OSPF_TYPE_DD, "Database Description" },
75 { OSPF_TYPE_LS_REQ, "LS-Request" },
76 { OSPF_TYPE_LS_UPDATE, "LS-Update" },
77 { OSPF_TYPE_LS_ACK, "LS-Ack" },
78 { 0, NULL }
79 };
80
81 static struct tok lsa_values[] = {
82 { LS_TYPE_ROUTER, "Router" },
83 { LS_TYPE_NETWORK, "Network" },
84 { LS_TYPE_SUM_IP, "Summary" },
85 { LS_TYPE_SUM_ABR, "ASBR Summary" },
86 { LS_TYPE_ASE, "External" },
87 { LS_TYPE_GROUP, "Multicast Group" },
88 { LS_TYPE_NSSA, "NSSA" },
89 { LS_TYPE_OPAQUE_LL, "Link Local Opaque" },
90 { LS_TYPE_OPAQUE_AL, "Area Local Opaque" },
91 { LS_TYPE_OPAQUE_DW, "Domain Wide Opaque" },
92 { 0, NULL }
93 };
94
95 static struct tok ospf_dd_flag_values[] = {
96 { OSPF_DB_INIT, "Init" },
97 { OSPF_DB_MORE, "More" },
98 { OSPF_DB_MASTER, "Master" },
99 { 0, NULL }
100 };
101
102 static char tstr[] = " [|ospf]";
103
104 #ifdef WIN32
105 #define inline __inline
106 #endif /* WIN32 */
107
108 static int ospf_print_lshdr(const struct lsa_hdr *);
109 static int ospf_print_lsa(const struct lsa *);
110 static int ospf_decode_v2(const struct ospfhdr *, const u_char *);
111
112 static int
113 ospf_print_lshdr(register const struct lsa_hdr *lshp) {
114
115 TCHECK(lshp->ls_type);
116 TCHECK(lshp->ls_options);
117
118 printf("\n\t %s LSA (%d), LSA-ID: %s, Advertising Router: %s, seq 0x%08x, age %us",
119 tok2str(lsa_values,"unknown",lshp->ls_type),
120 lshp->ls_type,
121 ipaddr_string(&lshp->ls_stateid),
122 ipaddr_string(&lshp->ls_router),
123 EXTRACT_32BITS(&lshp->ls_seq),
124 EXTRACT_16BITS(&lshp->ls_age));
125 printf("\n\t Options: %s", bittok2str(ospf_option_values,"none",lshp->ls_options));
126
127 return (0);
128 trunc:
129 return (1);
130 }
131
132 /*
133 * Print a single link state advertisement. If truncated return 1, else 0.
134 */
135 static int
136 ospf_print_lsa(register const struct lsa *lsap)
137 {
138 register const u_char *ls_end;
139 register const struct rlalink *rlp;
140 register const struct tos_metric *tosp;
141 register const struct in_addr *ap;
142 register const struct aslametric *almp;
143 register const struct mcla *mcp;
144 register const u_int32_t *lp;
145 register int j, k;
146
147 printf("\n\t %s LSA (%d), LSA-ID: %s, Advertising Router: %s, seq 0x%08x, age %us",
148 tok2str(lsa_values,"unknown",lsap->ls_hdr.ls_type),
149 lsap->ls_hdr.ls_type,
150 ipaddr_string(&lsap->ls_hdr.ls_stateid),
151 ipaddr_string(&lsap->ls_hdr.ls_router),
152 EXTRACT_32BITS(&lsap->ls_hdr.ls_seq),
153 EXTRACT_16BITS(&lsap->ls_hdr.ls_age));
154 printf("\n\t Options: %s", bittok2str(ospf_option_values,"none",lsap->ls_hdr.ls_options));
155
156 TCHECK(lsap->ls_hdr.ls_length);
157 ls_end = (u_char *)lsap + EXTRACT_16BITS(&lsap->ls_hdr.ls_length);
158 switch (lsap->ls_hdr.ls_type) {
159
160 case LS_TYPE_ROUTER:
161 TCHECK(lsap->lsa_un.un_rla.rla_flags);
162 printf("\n\t Router LSA Options: %s", bittok2str(ospf_rla_flag_values,"unknown (%u)",lsap->lsa_un.un_rla.rla_flags));
163
164 TCHECK(lsap->lsa_un.un_rla.rla_count);
165 j = EXTRACT_16BITS(&lsap->lsa_un.un_rla.rla_count);
166 TCHECK(lsap->lsa_un.un_rla.rla_link);
167 rlp = lsap->lsa_un.un_rla.rla_link;
168 while (j--) {
169 TCHECK(*rlp);
170 switch (rlp->link_type) {
171
172 case RLA_TYPE_VIRTUAL:
173 printf("\n\t Virtual Link: Neighbor-Router-ID: %s, Interface-IP: %s",
174 ipaddr_string(&rlp->link_id),
175 ipaddr_string(&rlp->link_data));
176 break;
177
178 case RLA_TYPE_ROUTER:
179 printf("\n\t Neighbor-Router-ID: %s, Interface-IP: %s",
180 ipaddr_string(&rlp->link_id),
181 ipaddr_string(&rlp->link_data));
182 break;
183
184 case RLA_TYPE_TRANSIT:
185 printf("\n\t Neighbor-Network-ID: %s, Interface-IP: %s",
186 ipaddr_string(&rlp->link_id),
187 ipaddr_string(&rlp->link_data));
188 break;
189
190 case RLA_TYPE_STUB:
191 printf("\n\t Stub-Network: %s, mask: %s",
192 ipaddr_string(&rlp->link_id),
193 ipaddr_string(&rlp->link_data));
194 break;
195
196 default:
197 printf("\n\t unknown Router Links Type (%u)",
198 rlp->link_type);
199 return (0);
200 }
201 printf(", tos 0, metric: %d", EXTRACT_16BITS(&rlp->link_tos0metric));
202 tosp = (struct tos_metric *)
203 ((sizeof rlp->link_tos0metric) + (u_char *) rlp);
204 for (k = 0; k < (int) rlp->link_toscount; ++k, ++tosp) {
205 TCHECK(*tosp);
206 printf(", tos %d, metric: %d",
207 tosp->tos_type,
208 EXTRACT_16BITS(&tosp->tos_metric));
209 }
210 rlp = (struct rlalink *)((u_char *)(rlp + 1) +
211 ((rlp->link_toscount) * sizeof(*tosp)));
212 }
213 break;
214
215 case LS_TYPE_NETWORK:
216 TCHECK(lsap->lsa_un.un_nla.nla_mask);
217 printf("\n\t mask %s rtrs",
218 ipaddr_string(&lsap->lsa_un.un_nla.nla_mask));
219 ap = lsap->lsa_un.un_nla.nla_router;
220 while ((u_char *)ap < ls_end) {
221 TCHECK(*ap);
222 printf(" %s", ipaddr_string(ap));
223 ++ap;
224 }
225 break;
226
227 case LS_TYPE_SUM_IP:
228 TCHECK(lsap->lsa_un.un_nla.nla_mask);
229 printf("\n\t mask %s",
230 ipaddr_string(&lsap->lsa_un.un_sla.sla_mask));
231 TCHECK(lsap->lsa_un.un_sla.sla_tosmetric);
232 lp = lsap->lsa_un.un_sla.sla_tosmetric;
233 /* suppress tos if its not supported */
234 if(!((lsap->ls_hdr.ls_options)&OSPF_OPTION_T)) {
235 printf(", metric: %u", EXTRACT_32BITS(lp)&SLA_MASK_METRIC);
236 break;
237 }
238 while ((u_char *)lp < ls_end) {
239 register u_int32_t ul;
240
241 TCHECK(*lp);
242 ul = EXTRACT_32BITS(lp);
243 printf(", tos %d metric %d",
244 (ul & SLA_MASK_TOS) >> SLA_SHIFT_TOS,
245 ul & SLA_MASK_METRIC);
246 ++lp;
247 }
248 break;
249
250 case LS_TYPE_SUM_ABR:
251 TCHECK(lsap->lsa_un.un_sla.sla_tosmetric);
252 lp = lsap->lsa_un.un_sla.sla_tosmetric;
253 /* suppress tos if its not supported */
254 if(!((lsap->ls_hdr.ls_options)&OSPF_OPTION_T)) {
255 printf(", metric: %u", EXTRACT_32BITS(lp)&SLA_MASK_METRIC);
256 break;
257 }
258 while ((u_char *)lp < ls_end) {
259 register u_int32_t ul;
260
261 TCHECK(*lp);
262 ul = EXTRACT_32BITS(lp);
263 printf(", tos %d metric %d",
264 (ul & SLA_MASK_TOS) >> SLA_SHIFT_TOS,
265 ul & SLA_MASK_METRIC);
266 ++lp;
267 }
268 break;
269
270 case LS_TYPE_ASE:
271 TCHECK(lsap->lsa_un.un_nla.nla_mask);
272 printf("\n\t mask %s",
273 ipaddr_string(&lsap->lsa_un.un_asla.asla_mask));
274
275 TCHECK(lsap->lsa_un.un_sla.sla_tosmetric);
276 almp = lsap->lsa_un.un_asla.asla_metric;
277 while ((u_char *)almp < ls_end) {
278 register u_int32_t ul;
279
280 TCHECK(almp->asla_tosmetric);
281 ul = EXTRACT_32BITS(&almp->asla_tosmetric);
282 printf(", type %d, tos %d metric:",
283 (ul & ASLA_FLAG_EXTERNAL) ? 2 : 1,
284 (ul & ASLA_MASK_TOS) >> ASLA_SHIFT_TOS);
285 if ((ul & ASLA_MASK_METRIC)==0xffffff)
286 printf(" infinite");
287 else
288 printf(" %d", (ul & ASLA_MASK_METRIC));
289
290 TCHECK(almp->asla_forward);
291 if (almp->asla_forward.s_addr) {
292 printf(", forward %s",
293 ipaddr_string(&almp->asla_forward));
294 }
295 TCHECK(almp->asla_tag);
296 if (almp->asla_tag.s_addr) {
297 printf(", tag %s",
298 ipaddr_string(&almp->asla_tag));
299 }
300 ++almp;
301 }
302 break;
303
304 case LS_TYPE_GROUP:
305 /* Multicast extensions as of 23 July 1991 */
306 mcp = lsap->lsa_un.un_mcla;
307 while ((u_char *)mcp < ls_end) {
308 TCHECK(mcp->mcla_vid);
309 switch (EXTRACT_32BITS(&mcp->mcla_vtype)) {
310
311 case MCLA_VERTEX_ROUTER:
312 printf("\n\t Router Router-ID %s",
313 ipaddr_string(&mcp->mcla_vid));
314 break;
315
316 case MCLA_VERTEX_NETWORK:
317 printf("\n\t Network Designated Router %s",
318 ipaddr_string(&mcp->mcla_vid));
319 break;
320
321 default:
322 printf("\n\t unknown VertexType (%u)",
323 EXTRACT_32BITS(&mcp->mcla_vtype));
324 break;
325 }
326 ++mcp;
327 }
328 break;
329
330 /*
331 * FIXME those are the defined LSAs that lack a decoder
332 * you are welcome to contribute code ;-)
333 */
334
335 case LS_TYPE_OPAQUE_LL:
336 case LS_TYPE_OPAQUE_AL:
337 case LS_TYPE_OPAQUE_DW:
338
339 default:
340 if (vflag <= 1) {
341 if(!print_unknown_data((u_char *)lsap->lsa_un.un_unknown,
342 "\n\t ", EXTRACT_16BITS(&lsap->ls_hdr.ls_length)-sizeof(struct lsa_hdr)))
343 return(0);
344 }
345 break;
346 }
347
348 /* do we want to see an additionally hexdump ? */
349 if (vflag> 1) {
350 if(!print_unknown_data((u_char *)lsap->lsa_un.un_unknown,
351 "\n\t ", EXTRACT_16BITS(&lsap->ls_hdr.ls_length)-sizeof(struct lsa_hdr)))
352 return(0);
353 }
354
355 return (0);
356 trunc:
357 return (1);
358 }
359
360 static int
361 ospf_decode_v2(register const struct ospfhdr *op,
362 register const u_char *dataend)
363 {
364 register const struct in_addr *ap;
365 register const struct lsr *lsrp;
366 register const struct lsa_hdr *lshp;
367 register const struct lsa *lsap;
368 register int lsa_count;
369
370 switch (op->ospf_type) {
371
372 case OSPF_TYPE_UMD:
373 /*
374 * Rob Coltun's special monitoring packets;
375 * do nothing
376 */
377 break;
378
379 case OSPF_TYPE_HELLO:
380 TCHECK(op->ospf_hello.hello_deadint);
381 printf("\n\t Hello Timer: %us, Dead Timer %us, mask: %s, Priority: %u",
382 EXTRACT_16BITS(&op->ospf_hello.hello_helloint),
383 EXTRACT_32BITS(&op->ospf_hello.hello_deadint),
384 ipaddr_string(&op->ospf_hello.hello_mask),
385 op->ospf_hello.hello_priority);
386
387 printf("\n\t Options: %s",
388 bittok2str(ospf_option_values,"none",op->ospf_hello.hello_options));
389
390 TCHECK(op->ospf_hello.hello_dr);
391 if (op->ospf_hello.hello_dr.s_addr != 0)
392 printf("\n\t Designated Router %s",
393 ipaddr_string(&op->ospf_hello.hello_dr));
394
395 TCHECK(op->ospf_hello.hello_bdr);
396 if (op->ospf_hello.hello_bdr.s_addr != 0)
397 printf(", Backup Designated Router %s",
398 ipaddr_string(&op->ospf_hello.hello_bdr));
399
400 ap = op->ospf_hello.hello_neighbor;
401 if ((u_char *)ap < dataend)
402 printf("\n\t Neighbor List:");
403 while ((u_char *)ap < dataend) {
404 TCHECK(*ap);
405 printf("\n\t %s", ipaddr_string(ap));
406 ++ap;
407 }
408 break; /* HELLO */
409
410 case OSPF_TYPE_DD:
411 TCHECK(op->ospf_db.db_options);
412 printf("\n\t Options: %s",
413 bittok2str(ospf_option_values,"none",op->ospf_db.db_options));
414 TCHECK(op->ospf_db.db_flags);
415 printf("\n\t DD Flags: %s",
416 bittok2str(ospf_dd_flag_values,"none",op->ospf_db.db_flags));
417
418 if (vflag) {
419 /* Print all the LS adv's */
420 lshp = op->ospf_db.db_lshdr;
421 while (!ospf_print_lshdr(lshp)) {
422 ++lshp;
423 }
424 }
425 break;
426
427 case OSPF_TYPE_LS_REQ:
428 lsrp = op->ospf_lsr;
429 while ((u_char *)lsrp < dataend) {
430 TCHECK(*lsrp);
431
432 printf("\n\t %s LSA (%d), LSA-ID: %s, Advertising Router: %s",
433 tok2str(lsa_values,"unknown",lsrp->ls_type),
434 lsrp->ls_type,
435 ipaddr_string(&lsrp->ls_stateid),
436 ipaddr_string(&lsrp->ls_router));
437 ++lsrp;
438 }
439 break;
440
441 case OSPF_TYPE_LS_UPDATE:
442 lsap = op->ospf_lsu.lsu_lsa;
443 TCHECK(op->ospf_lsu.lsu_count);
444 lsa_count = EXTRACT_32BITS(&op->ospf_lsu.lsu_count);
445 printf(", %d LSA%s",lsa_count, lsa_count > 1 ? "s" : "");
446 while (lsa_count--) {
447 if (ospf_print_lsa(lsap))
448 goto trunc;
449 lsap = (struct lsa *)((u_char *)lsap +
450 EXTRACT_16BITS(&lsap->ls_hdr.ls_length));
451 }
452 break;
453
454 case OSPF_TYPE_LS_ACK:
455 lshp = op->ospf_lsa.lsa_lshdr;
456 while (!ospf_print_lshdr(lshp)) {
457 ++lshp;
458 }
459 break;
460
461 default:
462 printf("v2 type (%d)", op->ospf_type);
463 break;
464 }
465 return (0);
466 trunc:
467 return (1);
468 }
469
470 void
471 ospf_print(register const u_char *bp, register u_int length,
472 register const u_char *bp2)
473 {
474 register const struct ospfhdr *op;
475 register const struct ip *ip;
476 register const u_char *dataend;
477 register const char *cp;
478
479 op = (struct ospfhdr *)bp;
480 ip = (struct ip *)bp2;
481
482 /* XXX Before we do anything else, strip off the MD5 trailer */
483 TCHECK(op->ospf_authtype);
484 if (EXTRACT_16BITS(&op->ospf_authtype) == OSPF_AUTH_MD5) {
485 length -= OSPF_AUTH_MD5_LEN;
486 snapend -= OSPF_AUTH_MD5_LEN;
487 }
488
489 /* If the type is valid translate it, or just print the type */
490 /* value. If it's not valid, say so and return */
491 TCHECK(op->ospf_type);
492 cp = tok2str(type2str, "unknown LS-Type %d", op->ospf_type);
493 printf("OSPFv%d %s length: %d", op->ospf_version, cp, length);
494 if (*cp == 'u')
495 return;
496
497 if(!vflag) /* non verbose - so lets bail out here */
498 return;
499
500 TCHECK(op->ospf_len);
501 if (length != EXTRACT_16BITS(&op->ospf_len)) {
502 printf(" [len %d]", EXTRACT_16BITS(&op->ospf_len));
503 return;
504 }
505 dataend = bp + length;
506
507 TCHECK(op->ospf_routerid);
508 printf("\n\tRouter-ID: %s", ipaddr_string(&op->ospf_routerid));
509
510 TCHECK(op->ospf_areaid);
511 if (op->ospf_areaid.s_addr != 0)
512 printf(", Area %s", ipaddr_string(&op->ospf_areaid));
513 else
514 printf(", Backbone Area");
515
516 if (vflag) {
517 /* Print authentication data (should we really do this?) */
518 TCHECK2(op->ospf_authdata[0], sizeof(op->ospf_authdata));
519
520 printf(", Authentication Type: %s (%u)",
521 tok2str(ospf_authtype_values,"unknown",EXTRACT_16BITS(&op->ospf_authtype)),
522 EXTRACT_16BITS(&op->ospf_authtype));
523
524 switch (EXTRACT_16BITS(&op->ospf_authtype)) {
525
526 case OSPF_AUTH_NONE:
527 break;
528
529 case OSPF_AUTH_SIMPLE:
530 (void)fn_printn(op->ospf_authdata,
531 sizeof(op->ospf_authdata), NULL);
532 printf("\"");
533 break;
534
535 case OSPF_AUTH_MD5:
536 printf("\n\tKey-ID: %u, Auth-Length: %u, Crypto Sequence Number: 0x%08x",
537 *((op->ospf_authdata)+2),
538 *((op->ospf_authdata)+3),
539 EXTRACT_32BITS((op->ospf_authdata)+4));
540 break;
541
542 default:
543 return;
544 }
545 }
546 /* Do rest according to version. */
547 switch (op->ospf_version) {
548
549 case 2:
550 /* ospf version 2 */
551 if (ospf_decode_v2(op, dataend))
552 goto trunc;
553 break;
554
555 default:
556 printf(" ospf [version %d]", op->ospf_version);
557 break;
558 } /* end switch on version */
559
560 return;
561 trunc:
562 fputs(tstr, stdout);
563 }