]> The Tcpdump Group git mirrors - tcpdump/blob - print-ospf.c
remove some LSA header code redundancy
[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.39 2003-10-03 13:20:46 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 Advertising Router: %s, seq 0x%08x, age %us",
148 ipaddr_string(&lsap->ls_hdr.ls_router),
149 EXTRACT_32BITS(&lsap->ls_hdr.ls_seq),
150 EXTRACT_16BITS(&lsap->ls_hdr.ls_age));
151
152 switch (lsap->ls_hdr.ls_type) {
153 /* the LSA header for opaque LSAs was slightly changed */
154 case LS_TYPE_OPAQUE_LL:
155 case LS_TYPE_OPAQUE_AL:
156 case LS_TYPE_OPAQUE_DW:
157
158 printf("\n\t %s LSA (%d), Opaque-Type: %u, Opaque-ID: %u",
159 tok2str(lsa_values,"unknown",lsap->ls_hdr.ls_type),
160 (lsap->ls_hdr.ls_type),
161 *(&lsap->ls_hdr.opaque_type),
162 EXTRACT_24BITS(&lsap->ls_hdr.opaque_id));
163 break;
164
165 /* all other LSA types use regular style LSA headers */
166 default:
167 printf("\n\t %s LSA (%d), LSA-ID: %s",
168 tok2str(lsa_values,"unknown",lsap->ls_hdr.ls_type),
169 lsap->ls_hdr.ls_type,
170 ipaddr_string(&lsap->ls_hdr.ls_stateid));
171 break;
172 }
173
174 printf("\n\t Options: %s", bittok2str(ospf_option_values,"none",lsap->ls_hdr.ls_options));
175
176 TCHECK(lsap->ls_hdr.ls_length);
177 ls_end = (u_char *)lsap + EXTRACT_16BITS(&lsap->ls_hdr.ls_length);
178 switch (lsap->ls_hdr.ls_type) {
179
180 case LS_TYPE_ROUTER:
181 TCHECK(lsap->lsa_un.un_rla.rla_flags);
182 printf("\n\t Router LSA Options: %s", bittok2str(ospf_rla_flag_values,"unknown (%u)",lsap->lsa_un.un_rla.rla_flags));
183
184 TCHECK(lsap->lsa_un.un_rla.rla_count);
185 j = EXTRACT_16BITS(&lsap->lsa_un.un_rla.rla_count);
186 TCHECK(lsap->lsa_un.un_rla.rla_link);
187 rlp = lsap->lsa_un.un_rla.rla_link;
188 while (j--) {
189 TCHECK(*rlp);
190 switch (rlp->link_type) {
191
192 case RLA_TYPE_VIRTUAL:
193 printf("\n\t Virtual Link: Neighbor-Router-ID: %s, Interface-IP: %s",
194 ipaddr_string(&rlp->link_id),
195 ipaddr_string(&rlp->link_data));
196 break;
197
198 case RLA_TYPE_ROUTER:
199 printf("\n\t Neighbor-Router-ID: %s, Interface-IP: %s",
200 ipaddr_string(&rlp->link_id),
201 ipaddr_string(&rlp->link_data));
202 break;
203
204 case RLA_TYPE_TRANSIT:
205 printf("\n\t Neighbor-Network-ID: %s, Interface-IP: %s",
206 ipaddr_string(&rlp->link_id),
207 ipaddr_string(&rlp->link_data));
208 break;
209
210 case RLA_TYPE_STUB:
211 printf("\n\t Stub-Network: %s, mask: %s",
212 ipaddr_string(&rlp->link_id),
213 ipaddr_string(&rlp->link_data));
214 break;
215
216 default:
217 printf("\n\t unknown Router Links Type (%u)",
218 rlp->link_type);
219 return (0);
220 }
221 printf(", tos 0, metric: %d", EXTRACT_16BITS(&rlp->link_tos0metric));
222 tosp = (struct tos_metric *)
223 ((sizeof rlp->link_tos0metric) + (u_char *) rlp);
224 for (k = 0; k < (int) rlp->link_toscount; ++k, ++tosp) {
225 TCHECK(*tosp);
226 printf(", tos %d, metric: %d",
227 tosp->tos_type,
228 EXTRACT_16BITS(&tosp->tos_metric));
229 }
230 rlp = (struct rlalink *)((u_char *)(rlp + 1) +
231 ((rlp->link_toscount) * sizeof(*tosp)));
232 }
233 break;
234
235 case LS_TYPE_NETWORK:
236 TCHECK(lsap->lsa_un.un_nla.nla_mask);
237 printf("\n\t mask %s rtrs",
238 ipaddr_string(&lsap->lsa_un.un_nla.nla_mask));
239 ap = lsap->lsa_un.un_nla.nla_router;
240 while ((u_char *)ap < ls_end) {
241 TCHECK(*ap);
242 printf(" %s", ipaddr_string(ap));
243 ++ap;
244 }
245 break;
246
247 case LS_TYPE_SUM_IP:
248 TCHECK(lsap->lsa_un.un_nla.nla_mask);
249 printf("\n\t mask %s",
250 ipaddr_string(&lsap->lsa_un.un_sla.sla_mask));
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_SUM_ABR:
271 TCHECK(lsap->lsa_un.un_sla.sla_tosmetric);
272 lp = lsap->lsa_un.un_sla.sla_tosmetric;
273 /* suppress tos if its not supported */
274 if(!((lsap->ls_hdr.ls_options)&OSPF_OPTION_T)) {
275 printf(", metric: %u", EXTRACT_32BITS(lp)&SLA_MASK_METRIC);
276 break;
277 }
278 while ((u_char *)lp < ls_end) {
279 register u_int32_t ul;
280
281 TCHECK(*lp);
282 ul = EXTRACT_32BITS(lp);
283 printf(", tos %d metric %d",
284 (ul & SLA_MASK_TOS) >> SLA_SHIFT_TOS,
285 ul & SLA_MASK_METRIC);
286 ++lp;
287 }
288 break;
289
290 case LS_TYPE_ASE:
291 TCHECK(lsap->lsa_un.un_nla.nla_mask);
292 printf("\n\t mask %s",
293 ipaddr_string(&lsap->lsa_un.un_asla.asla_mask));
294
295 TCHECK(lsap->lsa_un.un_sla.sla_tosmetric);
296 almp = lsap->lsa_un.un_asla.asla_metric;
297 while ((u_char *)almp < ls_end) {
298 register u_int32_t ul;
299
300 TCHECK(almp->asla_tosmetric);
301 ul = EXTRACT_32BITS(&almp->asla_tosmetric);
302 printf(", type %d, tos %d metric:",
303 (ul & ASLA_FLAG_EXTERNAL) ? 2 : 1,
304 (ul & ASLA_MASK_TOS) >> ASLA_SHIFT_TOS);
305 if ((ul & ASLA_MASK_METRIC)==0xffffff)
306 printf(" infinite");
307 else
308 printf(" %d", (ul & ASLA_MASK_METRIC));
309
310 TCHECK(almp->asla_forward);
311 if (almp->asla_forward.s_addr) {
312 printf(", forward %s",
313 ipaddr_string(&almp->asla_forward));
314 }
315 TCHECK(almp->asla_tag);
316 if (almp->asla_tag.s_addr) {
317 printf(", tag %s",
318 ipaddr_string(&almp->asla_tag));
319 }
320 ++almp;
321 }
322 break;
323
324 case LS_TYPE_GROUP:
325 /* Multicast extensions as of 23 July 1991 */
326 mcp = lsap->lsa_un.un_mcla;
327 while ((u_char *)mcp < ls_end) {
328 TCHECK(mcp->mcla_vid);
329 switch (EXTRACT_32BITS(&mcp->mcla_vtype)) {
330
331 case MCLA_VERTEX_ROUTER:
332 printf("\n\t Router Router-ID %s",
333 ipaddr_string(&mcp->mcla_vid));
334 break;
335
336 case MCLA_VERTEX_NETWORK:
337 printf("\n\t Network Designated Router %s",
338 ipaddr_string(&mcp->mcla_vid));
339 break;
340
341 default:
342 printf("\n\t unknown VertexType (%u)",
343 EXTRACT_32BITS(&mcp->mcla_vtype));
344 break;
345 }
346 ++mcp;
347 }
348 break;
349
350 /*
351 * FIXME those are the defined LSAs that lack a decoder
352 * you are welcome to contribute code ;-)
353 */
354
355 case LS_TYPE_OPAQUE_LL:
356 case LS_TYPE_OPAQUE_AL:
357 case LS_TYPE_OPAQUE_DW:
358
359 default:
360 if (vflag <= 1) {
361 if(!print_unknown_data((u_char *)lsap->lsa_un.un_unknown,
362 "\n\t ", EXTRACT_16BITS(&lsap->ls_hdr.ls_length)-sizeof(struct lsa_hdr)))
363 return(0);
364 }
365 break;
366 }
367
368 /* do we want to see an additionally hexdump ? */
369 if (vflag> 1) {
370 if(!print_unknown_data((u_char *)lsap->lsa_un.un_unknown,
371 "\n\t ", EXTRACT_16BITS(&lsap->ls_hdr.ls_length)-sizeof(struct lsa_hdr)))
372 return(0);
373 }
374
375 return (0);
376 trunc:
377 return (1);
378 }
379
380 static int
381 ospf_decode_v2(register const struct ospfhdr *op,
382 register const u_char *dataend)
383 {
384 register const struct in_addr *ap;
385 register const struct lsr *lsrp;
386 register const struct lsa_hdr *lshp;
387 register const struct lsa *lsap;
388 register int lsa_count;
389
390 switch (op->ospf_type) {
391
392 case OSPF_TYPE_UMD:
393 /*
394 * Rob Coltun's special monitoring packets;
395 * do nothing
396 */
397 break;
398
399 case OSPF_TYPE_HELLO:
400 TCHECK(op->ospf_hello.hello_deadint);
401 printf("\n\t Hello Timer: %us, Dead Timer %us, mask: %s, Priority: %u",
402 EXTRACT_16BITS(&op->ospf_hello.hello_helloint),
403 EXTRACT_32BITS(&op->ospf_hello.hello_deadint),
404 ipaddr_string(&op->ospf_hello.hello_mask),
405 op->ospf_hello.hello_priority);
406
407 printf("\n\t Options: %s",
408 bittok2str(ospf_option_values,"none",op->ospf_hello.hello_options));
409
410 TCHECK(op->ospf_hello.hello_dr);
411 if (op->ospf_hello.hello_dr.s_addr != 0)
412 printf("\n\t Designated Router %s",
413 ipaddr_string(&op->ospf_hello.hello_dr));
414
415 TCHECK(op->ospf_hello.hello_bdr);
416 if (op->ospf_hello.hello_bdr.s_addr != 0)
417 printf(", Backup Designated Router %s",
418 ipaddr_string(&op->ospf_hello.hello_bdr));
419
420 ap = op->ospf_hello.hello_neighbor;
421 if ((u_char *)ap < dataend)
422 printf("\n\t Neighbor List:");
423 while ((u_char *)ap < dataend) {
424 TCHECK(*ap);
425 printf("\n\t %s", ipaddr_string(ap));
426 ++ap;
427 }
428 break; /* HELLO */
429
430 case OSPF_TYPE_DD:
431 TCHECK(op->ospf_db.db_options);
432 printf("\n\t Options: %s",
433 bittok2str(ospf_option_values,"none",op->ospf_db.db_options));
434 TCHECK(op->ospf_db.db_flags);
435 printf("\n\t DD Flags: %s",
436 bittok2str(ospf_dd_flag_values,"none",op->ospf_db.db_flags));
437
438 if (vflag) {
439 /* Print all the LS adv's */
440 lshp = op->ospf_db.db_lshdr;
441 while (!ospf_print_lshdr(lshp)) {
442 ++lshp;
443 }
444 }
445 break;
446
447 case OSPF_TYPE_LS_REQ:
448 lsrp = op->ospf_lsr;
449 while ((u_char *)lsrp < dataend) {
450 TCHECK(*lsrp);
451
452 printf("\n\t %s LSA (%d), LSA-ID: %s, Advertising Router: %s",
453 tok2str(lsa_values,"unknown",lsrp->ls_type),
454 lsrp->ls_type,
455 ipaddr_string(&lsrp->ls_stateid),
456 ipaddr_string(&lsrp->ls_router));
457 ++lsrp;
458 }
459 break;
460
461 case OSPF_TYPE_LS_UPDATE:
462 lsap = op->ospf_lsu.lsu_lsa;
463 TCHECK(op->ospf_lsu.lsu_count);
464 lsa_count = EXTRACT_32BITS(&op->ospf_lsu.lsu_count);
465 printf(", %d LSA%s",lsa_count, lsa_count > 1 ? "s" : "");
466 while (lsa_count--) {
467 if (ospf_print_lsa(lsap))
468 goto trunc;
469 lsap = (struct lsa *)((u_char *)lsap +
470 EXTRACT_16BITS(&lsap->ls_hdr.ls_length));
471 }
472 break;
473
474 case OSPF_TYPE_LS_ACK:
475 lshp = op->ospf_lsa.lsa_lshdr;
476 while (!ospf_print_lshdr(lshp)) {
477 ++lshp;
478 }
479 break;
480
481 default:
482 printf("v2 type (%d)", op->ospf_type);
483 break;
484 }
485 return (0);
486 trunc:
487 return (1);
488 }
489
490 void
491 ospf_print(register const u_char *bp, register u_int length,
492 register const u_char *bp2)
493 {
494 register const struct ospfhdr *op;
495 register const struct ip *ip;
496 register const u_char *dataend;
497 register const char *cp;
498
499 op = (struct ospfhdr *)bp;
500 ip = (struct ip *)bp2;
501
502 /* XXX Before we do anything else, strip off the MD5 trailer */
503 TCHECK(op->ospf_authtype);
504 if (EXTRACT_16BITS(&op->ospf_authtype) == OSPF_AUTH_MD5) {
505 length -= OSPF_AUTH_MD5_LEN;
506 snapend -= OSPF_AUTH_MD5_LEN;
507 }
508
509 /* If the type is valid translate it, or just print the type */
510 /* value. If it's not valid, say so and return */
511 TCHECK(op->ospf_type);
512 cp = tok2str(type2str, "unknown LS-Type %d", op->ospf_type);
513 printf("OSPFv%d %s length: %d", op->ospf_version, cp, length);
514 if (*cp == 'u')
515 return;
516
517 if(!vflag) /* non verbose - so lets bail out here */
518 return;
519
520 TCHECK(op->ospf_len);
521 if (length != EXTRACT_16BITS(&op->ospf_len)) {
522 printf(" [len %d]", EXTRACT_16BITS(&op->ospf_len));
523 return;
524 }
525 dataend = bp + length;
526
527 TCHECK(op->ospf_routerid);
528 printf("\n\tRouter-ID: %s", ipaddr_string(&op->ospf_routerid));
529
530 TCHECK(op->ospf_areaid);
531 if (op->ospf_areaid.s_addr != 0)
532 printf(", Area %s", ipaddr_string(&op->ospf_areaid));
533 else
534 printf(", Backbone Area");
535
536 if (vflag) {
537 /* Print authentication data (should we really do this?) */
538 TCHECK2(op->ospf_authdata[0], sizeof(op->ospf_authdata));
539
540 printf(", Authentication Type: %s (%u)",
541 tok2str(ospf_authtype_values,"unknown",EXTRACT_16BITS(&op->ospf_authtype)),
542 EXTRACT_16BITS(&op->ospf_authtype));
543
544 switch (EXTRACT_16BITS(&op->ospf_authtype)) {
545
546 case OSPF_AUTH_NONE:
547 break;
548
549 case OSPF_AUTH_SIMPLE:
550 (void)fn_printn(op->ospf_authdata,
551 sizeof(op->ospf_authdata), NULL);
552 printf("\"");
553 break;
554
555 case OSPF_AUTH_MD5:
556 printf("\n\tKey-ID: %u, Auth-Length: %u, Crypto Sequence Number: 0x%08x",
557 *((op->ospf_authdata)+2),
558 *((op->ospf_authdata)+3),
559 EXTRACT_32BITS((op->ospf_authdata)+4));
560 break;
561
562 default:
563 return;
564 }
565 }
566 /* Do rest according to version. */
567 switch (op->ospf_version) {
568
569 case 2:
570 /* ospf version 2 */
571 if (ospf_decode_v2(op, dataend))
572 goto trunc;
573 break;
574
575 default:
576 printf(" ospf [version %d]", op->ospf_version);
577 break;
578 } /* end switch on version */
579
580 return;
581 trunc:
582 fputs(tstr, stdout);
583 }