]> The Tcpdump Group git mirrors - tcpdump/blob - print-ospf.c
don't pass on src & dst MAC adresses to the isoclns decoder as MAC adresses
[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.36 2002-12-23 19:57:49 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 }
329
330 return (0);
331 trunc:
332 return (1);
333 }
334
335 static int
336 ospf_decode_v2(register const struct ospfhdr *op,
337 register const u_char *dataend)
338 {
339 register const struct in_addr *ap;
340 register const struct lsr *lsrp;
341 register const struct lsa_hdr *lshp;
342 register const struct lsa *lsap;
343 register int lsa_count;
344
345 switch (op->ospf_type) {
346
347 case OSPF_TYPE_UMD:
348 /*
349 * Rob Coltun's special monitoring packets;
350 * do nothing
351 */
352 break;
353
354 case OSPF_TYPE_HELLO:
355 TCHECK(op->ospf_hello.hello_deadint);
356 printf("\n\t Hello Timer: %us, Dead Timer %us, mask: %s, Priority: %u",
357 EXTRACT_16BITS(&op->ospf_hello.hello_helloint),
358 EXTRACT_32BITS(&op->ospf_hello.hello_deadint),
359 ipaddr_string(&op->ospf_hello.hello_mask),
360 op->ospf_hello.hello_priority);
361
362 printf("\n\t Options: %s",
363 bittok2str(ospf_option_values,"none",op->ospf_hello.hello_options));
364
365 TCHECK(op->ospf_hello.hello_dr);
366 if (op->ospf_hello.hello_dr.s_addr != 0)
367 printf("\n\t Designated Router %s",
368 ipaddr_string(&op->ospf_hello.hello_dr));
369
370 TCHECK(op->ospf_hello.hello_bdr);
371 if (op->ospf_hello.hello_bdr.s_addr != 0)
372 printf(", Backup Designated Router %s",
373 ipaddr_string(&op->ospf_hello.hello_bdr));
374
375 ap = op->ospf_hello.hello_neighbor;
376 if ((u_char *)ap < dataend)
377 printf("\n\t Neighbor List:");
378 while ((u_char *)ap < dataend) {
379 TCHECK(*ap);
380 printf("\n\t %s", ipaddr_string(ap));
381 ++ap;
382 }
383 break; /* HELLO */
384
385 case OSPF_TYPE_DD:
386 TCHECK(op->ospf_db.db_options);
387 printf("\n\t Options: %s",
388 bittok2str(ospf_option_values,"none",op->ospf_db.db_options));
389 TCHECK(op->ospf_db.db_flags);
390 printf("\n\t DD Flags: %s",
391 bittok2str(ospf_dd_flag_values,"none",op->ospf_db.db_flags));
392
393 if (vflag) {
394 /* Print all the LS adv's */
395 lshp = op->ospf_db.db_lshdr;
396 while (!ospf_print_lshdr(lshp)) {
397 ++lshp;
398 }
399 }
400 break;
401
402 case OSPF_TYPE_LS_REQ:
403 lsrp = op->ospf_lsr;
404 while ((u_char *)lsrp < dataend) {
405 TCHECK(*lsrp);
406
407 printf("\n\t %s LSA (%d), LSA-ID: %s, Advertising Router: %s",
408 tok2str(lsa_values,"unknown",lsrp->ls_type),
409 lsrp->ls_type,
410 ipaddr_string(&lsrp->ls_stateid),
411 ipaddr_string(&lsrp->ls_router));
412 ++lsrp;
413 }
414 break;
415
416 case OSPF_TYPE_LS_UPDATE:
417 lsap = op->ospf_lsu.lsu_lsa;
418 TCHECK(op->ospf_lsu.lsu_count);
419 lsa_count = EXTRACT_32BITS(&op->ospf_lsu.lsu_count);
420 printf(", %d LSA%s",lsa_count, lsa_count > 1 ? "s" : "");
421 while (lsa_count--) {
422 if (ospf_print_lsa(lsap))
423 goto trunc;
424 lsap = (struct lsa *)((u_char *)lsap +
425 EXTRACT_16BITS(&lsap->ls_hdr.ls_length));
426 }
427 break;
428
429 case OSPF_TYPE_LS_ACK:
430 lshp = op->ospf_lsa.lsa_lshdr;
431 while (!ospf_print_lshdr(lshp)) {
432 ++lshp;
433 }
434 break;
435
436 default:
437 printf("v2 type (%d)", op->ospf_type);
438 break;
439 }
440 return (0);
441 trunc:
442 return (1);
443 }
444
445 void
446 ospf_print(register const u_char *bp, register u_int length,
447 register const u_char *bp2)
448 {
449 register const struct ospfhdr *op;
450 register const struct ip *ip;
451 register const u_char *dataend;
452 register const char *cp;
453
454 op = (struct ospfhdr *)bp;
455 ip = (struct ip *)bp2;
456
457 /* XXX Before we do anything else, strip off the MD5 trailer */
458 TCHECK(op->ospf_authtype);
459 if (EXTRACT_16BITS(&op->ospf_authtype) == OSPF_AUTH_MD5) {
460 length -= OSPF_AUTH_MD5_LEN;
461 snapend -= OSPF_AUTH_MD5_LEN;
462 }
463
464 /* If the type is valid translate it, or just print the type */
465 /* value. If it's not valid, say so and return */
466 TCHECK(op->ospf_type);
467 cp = tok2str(type2str, "unknown LS-Type %d", op->ospf_type);
468 printf("OSPFv%d %s length: %d", op->ospf_version, cp, length);
469 if (*cp == 'u')
470 return;
471
472 if(!vflag) /* non verbose - so lets bail out here */
473 return;
474
475 TCHECK(op->ospf_len);
476 if (length != EXTRACT_16BITS(&op->ospf_len)) {
477 printf(" [len %d]", EXTRACT_16BITS(&op->ospf_len));
478 return;
479 }
480 dataend = bp + length;
481
482 TCHECK(op->ospf_routerid);
483 printf("\n\tRouter-ID: %s", ipaddr_string(&op->ospf_routerid));
484
485 TCHECK(op->ospf_areaid);
486 if (op->ospf_areaid.s_addr != 0)
487 printf(", Area %s", ipaddr_string(&op->ospf_areaid));
488 else
489 printf(", Backbone Area");
490
491 if (vflag) {
492 /* Print authentication data (should we really do this?) */
493 TCHECK2(op->ospf_authdata[0], sizeof(op->ospf_authdata));
494
495 printf(", Authentication Type: %s (%u)",
496 tok2str(ospf_authtype_values,"unknown",EXTRACT_16BITS(&op->ospf_authtype)),
497 EXTRACT_16BITS(&op->ospf_authtype));
498
499 switch (EXTRACT_16BITS(&op->ospf_authtype)) {
500
501 case OSPF_AUTH_NONE:
502 break;
503
504 case OSPF_AUTH_SIMPLE:
505 (void)fn_printn(op->ospf_authdata,
506 sizeof(op->ospf_authdata), NULL);
507 printf("\"");
508 break;
509
510 case OSPF_AUTH_MD5:
511 printf("\n\tKey-ID: %u, Auth-Length: %u, Crypto Sequence Number: 0x%08x",
512 *((op->ospf_authdata)+2),
513 *((op->ospf_authdata)+3),
514 EXTRACT_32BITS((op->ospf_authdata)+4));
515 break;
516
517 default:
518 return;
519 }
520 }
521 /* Do rest according to version. */
522 switch (op->ospf_version) {
523
524 case 2:
525 /* ospf version 2 */
526 if (ospf_decode_v2(op, dataend))
527 goto trunc;
528 break;
529
530 default:
531 printf(" ospf [version %d]", op->ospf_version);
532 break;
533 } /* end switch on version */
534
535 return;
536 trunc:
537 fputs(tstr, stdout);
538 }