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