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