]> The Tcpdump Group git mirrors - tcpdump/blob - print-ospf.c
TESTrun: Use more 'newdir' and 'diffdir' variables
[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_V, "Virtual" },
63 { RLA_FLAG_W, "Wildcard" },
64 { RLA_FLAG_NT, "Nt" },
65 { RLA_FLAG_H, "Host" },
66 { 0, NULL }
67 };
68
69 static const struct tok type2str[] = {
70 { OSPF_TYPE_HELLO, "Hello" },
71 { OSPF_TYPE_DD, "Database Description" },
72 { OSPF_TYPE_LS_REQ, "LS-Request" },
73 { OSPF_TYPE_LS_UPDATE, "LS-Update" },
74 { OSPF_TYPE_LS_ACK, "LS-Ack" },
75 { 0, NULL }
76 };
77
78 static const struct tok lsa_values[] = {
79 { LS_TYPE_ROUTER, "Router" },
80 { LS_TYPE_NETWORK, "Network" },
81 { LS_TYPE_SUM_IP, "Summary" },
82 { LS_TYPE_SUM_ABR, "ASBR Summary" },
83 { LS_TYPE_ASE, "External" },
84 { LS_TYPE_GROUP, "Multicast Group" },
85 { LS_TYPE_NSSA, "NSSA" },
86 { LS_TYPE_OPAQUE_LL, "Link Local Opaque" },
87 { LS_TYPE_OPAQUE_AL, "Area Local Opaque" },
88 { LS_TYPE_OPAQUE_DW, "Domain Wide Opaque" },
89 { 0, NULL }
90 };
91
92 static const struct tok ospf_dd_flag_values[] = {
93 { OSPF_DB_INIT, "Init" },
94 { OSPF_DB_MORE, "More" },
95 { OSPF_DB_MASTER, "Master" },
96 { OSPF_DB_RESYNC, "OOBResync" },
97 { 0, NULL }
98 };
99
100 static const struct tok lsa_opaque_values[] = {
101 { LS_OPAQUE_TYPE_TE, "Traffic Engineering" },
102 { LS_OPAQUE_TYPE_GRACE, "Graceful restart" },
103 { LS_OPAQUE_TYPE_RI, "Router Information" },
104 { LS_OPAQUE_TYPE_EP, "Extended Prefix" },
105 { LS_OPAQUE_TYPE_EL, "Extended Link" },
106 { 0, NULL }
107 };
108
109 static const struct tok lsa_opaque_ri_sid_subtlv_values[] = {
110 { LS_OPAQUE_RI_SUBTLV_SID_LABEL, "SID/Label" },
111 { 0, NULL }
112 };
113
114 static const struct tok lsa_opaque_te_tlv_values[] = {
115 { LS_OPAQUE_TE_TLV_ROUTER, "Router Address" },
116 { LS_OPAQUE_TE_TLV_LINK, "Link" },
117 { 0, NULL }
118 };
119
120 static const struct tok lsa_opaque_ep_extd_prefix_subtlv_values[] = {
121 { LS_OPAQUE_EP_SUBTLV_PREFIX_SID, "Prefix-SID" },
122 { 0, NULL }
123 };
124
125 static const struct tok ep_range_tlv_prefix_sid_subtlv_flag_values[] = {
126 { 0x40, "No-PHP"},
127 { 0x20, "Mapping-Server"},
128 { 0x10, "Explicit-NULL"},
129 { 0x08, "Value"},
130 { 0x04, "Local"},
131 { 0, NULL}
132 };
133
134
135 static const struct tok lsa_opaque_ep_route_type_values[] = {
136 { 0, "Unspecified" },
137 { 1, "Intra-Area" },
138 { 3, "Inter-Area" },
139 { 5, "AS External" },
140 { 7, "NSSA External" },
141 { 0, NULL }
142 };
143
144 static const struct tok lsa_opaque_ep_tlv_values[] = {
145 { LS_OPAQUE_EP_EXTD_PREFIX_TLV, "Extended Prefix" },
146 { LS_OPAQUE_EP_EXTD_PREFIX_RANGE_TLV, "Extended Prefix Range" },
147 { 0, NULL }
148 };
149
150 static const struct tok ep_tlv_flag_values[] = {
151 { 0x80, "Inter-Area"},
152 { 0, NULL}
153 };
154
155 static const struct tok lsa_opaque_te_link_tlv_subtlv_values[] = {
156 { LS_OPAQUE_TE_LINK_SUBTLV_LINK_TYPE, "Link Type" },
157 { LS_OPAQUE_TE_LINK_SUBTLV_LINK_ID, "Link ID" },
158 { LS_OPAQUE_TE_LINK_SUBTLV_LOCAL_IP, "Local Interface IP address" },
159 { LS_OPAQUE_TE_LINK_SUBTLV_REMOTE_IP, "Remote Interface IP address" },
160 { LS_OPAQUE_TE_LINK_SUBTLV_TE_METRIC, "Traffic Engineering Metric" },
161 { LS_OPAQUE_TE_LINK_SUBTLV_MAX_BW, "Maximum Bandwidth" },
162 { LS_OPAQUE_TE_LINK_SUBTLV_MAX_RES_BW, "Maximum Reservable Bandwidth" },
163 { LS_OPAQUE_TE_LINK_SUBTLV_UNRES_BW, "Unreserved Bandwidth" },
164 { LS_OPAQUE_TE_LINK_SUBTLV_ADMIN_GROUP, "Administrative Group" },
165 { LS_OPAQUE_TE_LINK_SUBTLV_LINK_LOCAL_REMOTE_ID, "Link Local/Remote Identifier" },
166 { LS_OPAQUE_TE_LINK_SUBTLV_LINK_PROTECTION_TYPE, "Link Protection Type" },
167 { LS_OPAQUE_TE_LINK_SUBTLV_INTF_SW_CAP_DESCR, "Interface Switching Capability" },
168 { LS_OPAQUE_TE_LINK_SUBTLV_SHARED_RISK_GROUP, "Shared Risk Link Group" },
169 { LS_OPAQUE_TE_LINK_SUBTLV_BW_CONSTRAINTS, "Bandwidth Constraints" },
170 { 0, NULL }
171 };
172
173 static const struct tok lsa_opaque_grace_tlv_values[] = {
174 { LS_OPAQUE_GRACE_TLV_PERIOD, "Grace Period" },
175 { LS_OPAQUE_GRACE_TLV_REASON, "Graceful restart Reason" },
176 { LS_OPAQUE_GRACE_TLV_INT_ADDRESS, "IPv4 interface address" },
177 { 0, NULL }
178 };
179
180 static const struct tok lsa_opaque_grace_tlv_reason_values[] = {
181 { LS_OPAQUE_GRACE_TLV_REASON_UNKNOWN, "Unknown" },
182 { LS_OPAQUE_GRACE_TLV_REASON_SW_RESTART, "Software Restart" },
183 { LS_OPAQUE_GRACE_TLV_REASON_SW_UPGRADE, "Software Reload/Upgrade" },
184 { LS_OPAQUE_GRACE_TLV_REASON_CP_SWITCH, "Control Processor Switch" },
185 { 0, NULL }
186 };
187
188 static const struct tok lsa_opaque_te_tlv_link_type_sub_tlv_values[] = {
189 { LS_OPAQUE_TE_LINK_SUBTLV_LINK_TYPE_PTP, "Point-to-point" },
190 { LS_OPAQUE_TE_LINK_SUBTLV_LINK_TYPE_MA, "Multi-Access" },
191 { 0, NULL }
192 };
193
194 static const struct tok lsa_opaque_ri_tlv_values[] = {
195 { LS_OPAQUE_RI_TLV_CAP, "Router Capabilities" },
196 { LS_OPAQUE_RI_TLV_SR_ALGO, "SR-Algorithm" },
197 { LS_OPAQUE_RI_TLV_HOSTNAME, "Hostname" },
198 { LS_OPAQUE_RI_TLV_SID_LABEL_RANGE, "SID/Label Range" },
199 { LS_OPAQUE_RI_TLV_SR_LOCAL_BLOCK, "SR Local Block" },
200 { LS_OPAQUE_RI_TLV_SRMS_PREFERENCE, "SRMS Preference" },
201 { 0, NULL }
202 };
203
204 static const struct tok lsa_opaque_ri_tlv_cap_values[] = {
205 { 1, "Reserved" },
206 { 2, "Reserved" },
207 { 4, "Reserved" },
208 { 8, "Reserved" },
209 { 16, "graceful restart capable" },
210 { 32, "graceful restart helper" },
211 { 64, "Stub router support" },
212 { 128, "Traffic engineering" },
213 { 256, "p2p over LAN" },
214 { 512, "path computation server" },
215 { 0, NULL }
216 };
217
218 static const struct tok lsa_opaque_ri_tlv_sr_algos[] = {
219 { 0, "Shortest Path First" },
220 { 1, "Strict Shortest Path First" },
221 { 0, NULL }
222 };
223
224 static const struct tok ospf_lls_tlv_values[] = {
225 { OSPF_LLS_EO, "Extended Options" },
226 { OSPF_LLS_MD5, "MD5 Authentication" },
227 { 0, NULL }
228 };
229
230 static const struct tok ospf_lls_eo_options[] = {
231 { OSPF_LLS_EO_LR, "LSDB resync" },
232 { OSPF_LLS_EO_RS, "Restart" },
233 { 0, NULL }
234 };
235
236 int
237 ospf_grace_lsa_print(netdissect_options *ndo,
238 const u_char *tptr, u_int ls_length)
239 {
240 u_int tlv_type, tlv_length;
241
242
243 while (ls_length > 0) {
244 ND_TCHECK_4(tptr);
245 if (ls_length < 4) {
246 ND_PRINT("\n\t Remaining LS length %u < 4", ls_length);
247 return -1;
248 }
249 tlv_type = GET_BE_U_2(tptr);
250 tlv_length = GET_BE_U_2(tptr + 2);
251 tptr+=4;
252 ls_length-=4;
253
254 ND_PRINT("\n\t %s TLV (%u), length %u, value: ",
255 tok2str(lsa_opaque_grace_tlv_values,"unknown",tlv_type),
256 tlv_type,
257 tlv_length);
258
259 if (tlv_length > ls_length) {
260 ND_PRINT("\n\t Bogus length %u > %u", tlv_length,
261 ls_length);
262 return -1;
263 }
264
265 /* Infinite loop protection. */
266 if (tlv_type == 0 || tlv_length ==0) {
267 nd_print_invalid(ndo);
268 return -1;
269 }
270
271 ND_TCHECK_LEN(tptr, tlv_length);
272 switch(tlv_type) {
273
274 case LS_OPAQUE_GRACE_TLV_PERIOD:
275 if (tlv_length != 4) {
276 ND_PRINT("\n\t Bogus length %u != 4", tlv_length);
277 return -1;
278 }
279 ND_PRINT("%us", GET_BE_U_4(tptr));
280 break;
281
282 case LS_OPAQUE_GRACE_TLV_REASON:
283 if (tlv_length != 1) {
284 ND_PRINT("\n\t Bogus length %u != 1", tlv_length);
285 return -1;
286 }
287 ND_PRINT("%s (%u)",
288 tok2str(lsa_opaque_grace_tlv_reason_values, "Unknown", GET_U_1(tptr)),
289 GET_U_1(tptr));
290 break;
291
292 case LS_OPAQUE_GRACE_TLV_INT_ADDRESS:
293 if (tlv_length != 4) {
294 ND_PRINT("\n\t Bogus length %u != 4", tlv_length);
295 return -1;
296 }
297 ND_PRINT("%s", GET_IPADDR_STRING(tptr));
298 break;
299
300 default:
301 if (ndo->ndo_vflag <= 1) {
302 if (!print_unknown_data(ndo, tptr, "\n\t ", tlv_length))
303 return -1;
304 }
305 break;
306
307 }
308 /* in OSPF everything has to be 32-bit aligned, including TLVs */
309 if (tlv_length%4 != 0)
310 tlv_length+=4-(tlv_length%4);
311 ls_length-=tlv_length;
312 tptr+=tlv_length;
313 }
314
315 return 0;
316 trunc:
317 return -1;
318 }
319
320 int
321 ospf_te_lsa_print(netdissect_options *ndo,
322 const u_char *tptr, u_int ls_length)
323 {
324 u_int tlv_type, tlv_length, subtlv_type, subtlv_length;
325 u_int priority_level, te_class, count_srlg;
326 union { /* int to float conversion buffer for several subTLVs */
327 float f;
328 uint32_t i;
329 } bw;
330
331 while (ls_length != 0) {
332 ND_TCHECK_4(tptr);
333 if (ls_length < 4) {
334 ND_PRINT("\n\t Remaining LS length %u < 4", ls_length);
335 return -1;
336 }
337 tlv_type = GET_BE_U_2(tptr);
338 tlv_length = GET_BE_U_2(tptr + 2);
339 tptr+=4;
340 ls_length-=4;
341
342 ND_PRINT("\n\t %s TLV (%u), length: %u",
343 tok2str(lsa_opaque_te_tlv_values,"unknown",tlv_type),
344 tlv_type,
345 tlv_length);
346
347 if (tlv_length > ls_length) {
348 ND_PRINT("\n\t Bogus length %u > %u", tlv_length,
349 ls_length);
350 return -1;
351 }
352
353 /* Infinite loop protection. */
354 if (tlv_type == 0 || tlv_length ==0) {
355 nd_print_invalid(ndo);
356 return -1;
357 }
358
359 switch(tlv_type) {
360 case LS_OPAQUE_TE_TLV_LINK:
361 while (tlv_length != 0) {
362 if (tlv_length < 4) {
363 ND_PRINT("\n\t Remaining TLV length %u < 4",
364 tlv_length);
365 return -1;
366 }
367 subtlv_type = GET_BE_U_2(tptr);
368 subtlv_length = GET_BE_U_2(tptr + 2);
369 tptr+=4;
370 tlv_length-=4;
371
372 /* Infinite loop protection */
373 if (subtlv_type == 0 || subtlv_length == 0)
374 goto invalid;
375
376 ND_PRINT("\n\t %s subTLV (%u), length: %u",
377 tok2str(lsa_opaque_te_link_tlv_subtlv_values,"unknown",subtlv_type),
378 subtlv_type,
379 subtlv_length);
380
381 if (tlv_length < subtlv_length) {
382 ND_PRINT("\n\t Remaining TLV length %u < %u",
383 tlv_length + 4, subtlv_length + 4);
384 return -1;
385 }
386 ND_TCHECK_LEN(tptr, subtlv_length);
387 switch(subtlv_type) {
388 case LS_OPAQUE_TE_LINK_SUBTLV_ADMIN_GROUP:
389 if (subtlv_length != 4) {
390 ND_PRINT(" != 4");
391 goto invalid;
392 }
393 ND_PRINT(", 0x%08x", GET_BE_U_4(tptr));
394 break;
395 case LS_OPAQUE_TE_LINK_SUBTLV_LINK_ID:
396 case LS_OPAQUE_TE_LINK_SUBTLV_LINK_LOCAL_REMOTE_ID:
397 if (subtlv_length != 4 && subtlv_length != 8) {
398 ND_PRINT(" != 4 && != 8");
399 goto invalid;
400 }
401 ND_PRINT(", %s (0x%08x)",
402 GET_IPADDR_STRING(tptr),
403 GET_BE_U_4(tptr));
404 if (subtlv_length == 8) /* rfc4203 */
405 ND_PRINT(", %s (0x%08x)",
406 GET_IPADDR_STRING(tptr+4),
407 GET_BE_U_4(tptr + 4));
408 break;
409 case LS_OPAQUE_TE_LINK_SUBTLV_LOCAL_IP:
410 case LS_OPAQUE_TE_LINK_SUBTLV_REMOTE_IP:
411 if (subtlv_length != 4) {
412 ND_PRINT(" != 4");
413 goto invalid;
414 }
415 ND_PRINT(", %s", GET_IPADDR_STRING(tptr));
416 break;
417 case LS_OPAQUE_TE_LINK_SUBTLV_MAX_BW:
418 case LS_OPAQUE_TE_LINK_SUBTLV_MAX_RES_BW:
419 if (subtlv_length != 4) {
420 ND_PRINT(" != 4");
421 goto invalid;
422 }
423 bw.i = GET_BE_U_4(tptr);
424 ND_PRINT(", %.3f Mbps", bw.f * 8 / 1000000);
425 break;
426 case LS_OPAQUE_TE_LINK_SUBTLV_UNRES_BW:
427 if (subtlv_length != 32) {
428 ND_PRINT(" != 32");
429 goto invalid;
430 }
431 for (te_class = 0; te_class < 8; te_class++) {
432 bw.i = GET_BE_U_4(tptr + te_class * 4);
433 ND_PRINT("\n\t\tTE-Class %u: %.3f Mbps",
434 te_class,
435 bw.f * 8 / 1000000);
436 }
437 break;
438 case LS_OPAQUE_TE_LINK_SUBTLV_BW_CONSTRAINTS:
439 if (subtlv_length < 4) {
440 ND_PRINT(" < 4");
441 goto invalid;
442 }
443 /* BC Model Id (1 octet) + Reserved (3 octets) */
444 ND_PRINT("\n\t\tBandwidth Constraints Model ID: %s (%u)",
445 tok2str(diffserv_te_bc_values, "unknown", GET_U_1(tptr)),
446 GET_U_1(tptr));
447 if (subtlv_length % 4 != 0) {
448 ND_PRINT("\n\t\tlength %u != N x 4", subtlv_length);
449 goto invalid;
450 }
451 if (subtlv_length > 36) {
452 ND_PRINT("\n\t\tlength %u > 36", subtlv_length);
453 goto invalid;
454 }
455 /* decode BCs until the subTLV ends */
456 for (te_class = 0; te_class < (subtlv_length-4)/4; te_class++) {
457 bw.i = GET_BE_U_4(tptr + 4 + te_class * 4);
458 ND_PRINT("\n\t\t Bandwidth constraint CT%u: %.3f Mbps",
459 te_class,
460 bw.f * 8 / 1000000);
461 }
462 break;
463 case LS_OPAQUE_TE_LINK_SUBTLV_TE_METRIC:
464 if (subtlv_length != 4) {
465 ND_PRINT(" != 4");
466 goto invalid;
467 }
468 ND_PRINT(", Metric %u", GET_BE_U_4(tptr));
469 break;
470 case LS_OPAQUE_TE_LINK_SUBTLV_LINK_PROTECTION_TYPE:
471 /* Protection Cap (1 octet) + Reserved ((3 octets) */
472 if (subtlv_length != 4) {
473 ND_PRINT(" != 4");
474 goto invalid;
475 }
476 ND_PRINT(", %s",
477 bittok2str(gmpls_link_prot_values, "none", GET_U_1(tptr)));
478 break;
479 case LS_OPAQUE_TE_LINK_SUBTLV_INTF_SW_CAP_DESCR:
480 if (subtlv_length < 36) {
481 ND_PRINT(" < 36");
482 goto invalid;
483 }
484 /* Switching Cap (1 octet) + Encoding (1) + Reserved (2) */
485 ND_PRINT("\n\t\tInterface Switching Capability: %s",
486 tok2str(gmpls_switch_cap_values, "Unknown", GET_U_1((tptr))));
487 ND_PRINT("\n\t\tLSP Encoding: %s\n\t\tMax LSP Bandwidth:",
488 tok2str(gmpls_encoding_values, "Unknown", GET_U_1((tptr + 1))));
489 for (priority_level = 0; priority_level < 8; priority_level++) {
490 bw.i = GET_BE_U_4(tptr + 4 + (priority_level * 4));
491 ND_PRINT("\n\t\t priority level %u: %.3f Mbps",
492 priority_level,
493 bw.f * 8 / 1000000);
494 }
495 break;
496 case LS_OPAQUE_TE_LINK_SUBTLV_LINK_TYPE:
497 if (subtlv_length != 1) {
498 ND_PRINT(" != 1");
499 goto invalid;
500 }
501 ND_PRINT(", %s (%u)",
502 tok2str(lsa_opaque_te_tlv_link_type_sub_tlv_values,"unknown",GET_U_1(tptr)),
503 GET_U_1(tptr));
504 break;
505
506 case LS_OPAQUE_TE_LINK_SUBTLV_SHARED_RISK_GROUP:
507 if (subtlv_length % 4 != 0) {
508 ND_PRINT(" != N x 4");
509 goto invalid;
510 }
511 count_srlg = subtlv_length / 4;
512 if (count_srlg != 0)
513 ND_PRINT("\n\t\t Shared risk group: ");
514 while (count_srlg > 0) {
515 bw.i = GET_BE_U_4(tptr);
516 ND_PRINT("%u", bw.i);
517 tptr+=4;
518 count_srlg--;
519 if (count_srlg > 0)
520 ND_PRINT(", ");
521 }
522 break;
523
524 default:
525 if (ndo->ndo_vflag <= 1) {
526 if (!print_unknown_data(ndo, tptr, "\n\t\t", subtlv_length))
527 return -1;
528 }
529 break;
530 }
531 /* in OSPF everything has to be 32-bit aligned, including subTLVs */
532 if (subtlv_length%4 != 0)
533 subtlv_length+=4-(subtlv_length%4);
534
535 if (tlv_length < subtlv_length) {
536 ND_PRINT("\n\t Remaining TLV length %u < %u",
537 tlv_length + 4, subtlv_length + 4);
538 return -1;
539 }
540 tlv_length-=subtlv_length;
541 tptr+=subtlv_length;
542
543 }
544 break;
545
546 case LS_OPAQUE_TE_TLV_ROUTER:
547 if (tlv_length < 4) {
548 ND_PRINT("\n\t TLV length %u < 4", tlv_length);
549 return -1;
550 }
551 ND_PRINT(", %s", GET_IPADDR_STRING(tptr));
552 break;
553
554 default:
555 if (ndo->ndo_vflag <= 1) {
556 if (!print_unknown_data(ndo, tptr, "\n\t ", tlv_length))
557 return -1;
558 }
559 break;
560 }
561 /* in OSPF everything has to be 32-bit aligned, including TLVs */
562 if (tlv_length%4 != 0)
563 tlv_length+=4-(tlv_length%4);
564 if (tlv_length > ls_length) {
565 ND_PRINT("\n\t Bogus padded length %u > %u", tlv_length,
566 ls_length);
567 return -1;
568 }
569 ls_length-=tlv_length;
570 tptr+=tlv_length;
571 }
572 return 0;
573 trunc:
574 return -1;
575 invalid:
576 nd_print_invalid(ndo);
577 return -1;
578 }
579
580 static int
581 ospf_print_lshdr(netdissect_options *ndo,
582 const struct lsa_hdr *lshp)
583 {
584 u_int ls_type;
585 u_int ls_length;
586
587 ls_length = GET_BE_U_2(lshp->ls_length);
588 if (ls_length < sizeof(struct lsa_hdr)) {
589 ND_PRINT("\n\t Bogus length %u < header (%zu)", ls_length,
590 sizeof(struct lsa_hdr));
591 return(-1);
592 }
593 ND_PRINT("\n\t Advertising Router %s, seq 0x%08x, age %us, length %zu",
594 GET_IPADDR_STRING(lshp->ls_router),
595 GET_BE_U_4(lshp->ls_seq),
596 GET_BE_U_2(lshp->ls_age),
597 ls_length - sizeof(struct lsa_hdr));
598 ls_type = GET_U_1(lshp->ls_type);
599 switch (ls_type) {
600 /* the LSA header for opaque LSAs was slightly changed */
601 case LS_TYPE_OPAQUE_LL:
602 case LS_TYPE_OPAQUE_AL:
603 case LS_TYPE_OPAQUE_DW:
604 ND_PRINT("\n\t %s LSA (%u), Opaque-Type %s LSA (%u), Opaque-ID %u",
605 tok2str(lsa_values,"unknown",ls_type),
606 ls_type,
607
608 tok2str(lsa_opaque_values,
609 "unknown",
610 GET_U_1(lshp->un_lsa_id.opaque_field.opaque_type)),
611 GET_U_1(lshp->un_lsa_id.opaque_field.opaque_type),
612 GET_BE_U_3(lshp->un_lsa_id.opaque_field.opaque_id)
613
614 );
615 break;
616
617 /* all other LSA types use regular style LSA headers */
618 default:
619 ND_PRINT("\n\t %s LSA (%u), LSA-ID: %s",
620 tok2str(lsa_values,"unknown",ls_type),
621 ls_type,
622 GET_IPADDR_STRING(lshp->un_lsa_id.lsa_id));
623 break;
624 }
625 ND_PRINT("\n\t Options: [%s]",
626 bittok2str(ospf_option_values, "none", GET_U_1(lshp->ls_options)));
627
628 return (ls_length);
629 }
630
631 /* draft-ietf-ospf-mt-09 */
632 static const struct tok ospf_topology_values[] = {
633 { 0, "default" },
634 { 1, "multicast" },
635 { 2, "management" },
636 { 0, NULL }
637 };
638
639 /*
640 * Print all the per-topology metrics.
641 */
642 static void
643 ospf_print_tos_metrics(netdissect_options *ndo,
644 const union un_tos *tos)
645 {
646 u_int metric_count;
647 u_int toscount;
648 u_int tos_type;
649
650 toscount = GET_U_1(tos->link.link_tos_count)+1;
651 metric_count = 0;
652
653 /*
654 * All but the first metric contain a valid topology id.
655 */
656 while (toscount != 0) {
657 tos_type = GET_U_1(tos->metrics.tos_type);
658 ND_PRINT("\n\t\ttopology %s (%u), metric %u",
659 tok2str(ospf_topology_values, "Unknown",
660 metric_count ? tos_type : 0),
661 metric_count ? tos_type : 0,
662 GET_BE_U_2(tos->metrics.tos_metric));
663 metric_count++;
664 tos++;
665 toscount--;
666 }
667 }
668
669 /*
670 * The SID/Label Range TLV
671 * https://datatracker.ietf.org/doc/html/rfc8665#section-3.2
672 * and the SR Local Block TLV
673 * https://datatracker.ietf.org/doc/html/rfc8665#section-3.3
674 * have the same contents, so this function is used to
675 * print both.
676 */
677 static int
678 ospf_print_ri_lsa_sid_label_range_tlv(netdissect_options *ndo, const uint8_t *tptr,
679 u_int tlv_length)
680 {
681 u_int subtlv_type, subtlv_length;
682
683 while (tlv_length >= 4) {
684
685 subtlv_type = GET_BE_U_2(tptr);
686 subtlv_length = GET_BE_U_2(tptr+2);
687 tptr+=4;
688 tlv_length-=4;
689
690 /* Infinite loop protection. */
691 if (subtlv_type == 0 || subtlv_length == 0) {
692 nd_print_invalid(ndo);
693 return -1;
694 }
695
696 ND_PRINT("\n\t %s subTLV (%u), length: %u, value: ",
697 tok2str(lsa_opaque_ri_sid_subtlv_values,"unknown",subtlv_type),
698 subtlv_type,
699 subtlv_length);
700
701 switch (subtlv_type) {
702 case LS_OPAQUE_RI_SUBTLV_SID_LABEL:
703 if (subtlv_length == 3) {
704 ND_PRINT("\n\t\tLabel: %u", GET_BE_U_3(tptr));
705 } else if (subtlv_length == 4) {
706 ND_PRINT("\n\t\tSID: %u", GET_BE_U_4(tptr));
707 } else {
708 ND_PRINT("\n\t\tBogus subTLV length %u", subtlv_length);
709 }
710 break;
711
712 default:
713 if (ndo->ndo_vflag <= 1) {
714 if (!print_unknown_data(ndo, tptr, "\n\t\t", subtlv_length))
715 return -1;
716 }
717 }
718
719 /* in OSPF everything has to be 32-bit aligned, including subTLVs */
720 if (subtlv_length % 4) {
721 subtlv_length += (4 - (subtlv_length % 4));
722 }
723 tptr+=subtlv_length;
724 tlv_length-=subtlv_length;
725 }
726 return 0;
727 }
728
729 static int
730 ospf_print_ep_lsa_extd_prefix_tlv(netdissect_options *ndo, const uint8_t *tptr,
731 u_int tlv_length)
732 {
733 u_int subtlv_type, subtlv_length;
734 uint8_t flags, mt_id, algo;
735
736 while (tlv_length >= 4) {
737 subtlv_type = GET_BE_U_2(tptr);
738 subtlv_length = GET_BE_U_2(tptr+2);
739 tptr+=4;
740 tlv_length-=4;
741
742 /* Infinite loop protection. */
743 if (subtlv_type == 0 || subtlv_length == 0) {
744 nd_print_invalid(ndo);
745 return -1;
746 }
747
748 ND_PRINT("\n\t\t%s subTLV (%u), length: %u, value: ",
749 tok2str(lsa_opaque_ep_extd_prefix_subtlv_values,"unknown",subtlv_type),
750 subtlv_type,
751 subtlv_length);
752
753 switch (subtlv_type) {
754 case LS_OPAQUE_EP_SUBTLV_PREFIX_SID:
755 flags = GET_U_1(tptr);
756 mt_id = GET_U_1(tptr+2);
757 algo = GET_U_1(tptr+3);
758
759 if (subtlv_length == 7) {
760 ND_PRINT("\n\t\t Label: %u", GET_BE_U_3(tptr+4));
761 } else if (subtlv_length == 8) {
762 ND_PRINT("\n\t\t Index: %u", GET_BE_U_4(tptr+4));
763 } else {
764 ND_PRINT("\n\t\tBogus subTLV length %u", subtlv_length);
765 break;
766 }
767 ND_PRINT( ", MT-ID: %u, Algorithm: %s (%u), Flags [%s]",
768 mt_id, tok2str(lsa_opaque_ri_tlv_sr_algos, "Unknown", algo), algo,
769 bittok2str(ep_range_tlv_prefix_sid_subtlv_flag_values, "none", flags));
770 break;
771
772 default:
773 if (ndo->ndo_vflag <= 1) {
774 if (!print_unknown_data(ndo, tptr, "\n\t\t", subtlv_length))
775 return -1;
776 }
777 }
778
779 /* in OSPF everything has to be 32-bit aligned, including subTLVs */
780 if (subtlv_length % 4) {
781 subtlv_length += (4 - (subtlv_length % 4));
782 }
783 tptr+=subtlv_length;
784 tlv_length-=subtlv_length;
785 }
786 return 0;
787 }
788
789 static int
790 ospf_ep_lsa_print(netdissect_options *ndo, const uint8_t *tptr, u_int lsa_length)
791 {
792 u_int tlv_type, tlv_length;
793 uint16_t range_size;
794 uint8_t af, prefix_length, route_type, flags;
795
796 while (lsa_length >= 4) {
797
798 tlv_type = GET_BE_U_2(tptr);
799 tlv_length = GET_BE_U_2(tptr+2);
800 tptr+=4;
801 lsa_length-=4;
802
803 /* Infinite loop protection. */
804 if (tlv_type == 0 || tlv_length == 0) {
805 nd_print_invalid(ndo);
806 return -1;
807 }
808
809 ND_PRINT("\n\t %s TLV (%u), length: %u, value: ",
810 tok2str(lsa_opaque_ep_tlv_values,"unknown",tlv_type),
811 tlv_type,
812 tlv_length);
813
814 switch (tlv_type) {
815 case LS_OPAQUE_EP_EXTD_PREFIX_TLV:
816 prefix_length = GET_U_1(tptr+1);
817 af = GET_U_1(tptr+2);
818 route_type = GET_U_1(tptr);
819 flags = GET_U_1(tptr+3);
820
821 if (af != 0) {
822 ND_PRINT("\n\t Bogus AF %u", af);
823 return -1;
824 }
825
826 if (prefix_length > 32) {
827 ND_PRINT("\n\t IPv4 prefix: bad bit length %u", prefix_length);
828 return -1;
829 }
830
831 ND_PRINT("\n\t IPv4 prefix: %15s/%u, Route Type: %s, Flags [%s]",
832 GET_IPADDR_STRING(tptr+4), prefix_length,
833 tok2str(lsa_opaque_ep_route_type_values, "Unknown", route_type),
834 bittok2str(ep_tlv_flag_values, "none", flags));
835
836 /* subTLVs present ? */
837 if (tlv_length > 12) {
838 if (ospf_print_ep_lsa_extd_prefix_tlv(ndo, tptr+8, tlv_length-8) == -1) {
839 return -1;
840 }
841 }
842 break;
843
844 case LS_OPAQUE_EP_EXTD_PREFIX_RANGE_TLV:
845 prefix_length = GET_U_1(tptr);
846 af = GET_U_1(tptr+1);
847 range_size = GET_BE_U_2(tptr+2);
848 flags = GET_U_1(tptr+4);
849
850 if (af != 0) {
851 ND_PRINT("\n\t Bogus AF %u", af);
852 return -1;
853 }
854
855 if (prefix_length > 32) {
856 ND_PRINT("\n\t IPv4 prefix: bad bit length %u", prefix_length);
857 return -1;
858 }
859
860 ND_PRINT("\n\t IPv4 prefix: %15s/%u, Range size: %u, Flags [%s]",
861 GET_IPADDR_STRING(tptr+8), prefix_length,
862 range_size,
863 bittok2str(ep_tlv_flag_values, "none", flags));
864
865 /* subTLVs present ? */
866 if (tlv_length > 12) {
867 if (ospf_print_ep_lsa_extd_prefix_tlv(ndo, tptr+12, tlv_length-12) == -1) {
868 return -1;
869 }
870 }
871 break;
872
873 default:
874 if (ndo->ndo_vflag <= 1) {
875 if (!print_unknown_data(ndo, tptr, "\n\t\t", tlv_length))
876 return -1;
877 }
878 }
879
880 /* in OSPF everything has to be 32-bit aligned, including TLVs */
881 if (tlv_length % 4) {
882 tlv_length += (4 - (tlv_length % 4));
883 }
884 tptr+=tlv_length;
885 lsa_length-=tlv_length;
886 }
887 return 0;
888 }
889
890 /*
891 * Print a single link state advertisement. If truncated or if LSA length
892 * field is less than the length of the LSA header, return NULl, else
893 * return pointer to data past end of LSA.
894 */
895 static const uint8_t *
896 ospf_print_lsa(netdissect_options *ndo,
897 const struct lsa *lsap)
898 {
899 const uint8_t *ls_end;
900 const struct rlalink *rlp;
901 const nd_ipv4 *ap;
902 const struct aslametric *almp;
903 const struct mcla *mcp;
904 const uint8_t *lp;
905 u_int tlv_type, tlv_length, rla_count, topology, num_tlv;
906 int ospf_print_lshdr_ret;
907 u_int ls_length;
908 const uint8_t *tptr;
909
910 tptr = (const uint8_t *)lsap->lsa_un.un_unknown; /* squelch compiler warnings */
911 ospf_print_lshdr_ret = ospf_print_lshdr(ndo, &lsap->ls_hdr);
912 if (ospf_print_lshdr_ret < 0)
913 return(NULL);
914 ls_length = (u_int)ospf_print_lshdr_ret;
915 ls_end = (const uint8_t *)lsap + ls_length;
916 /*
917 * ospf_print_lshdr() returns -1 if the length is too short,
918 * so we know ls_length is >= sizeof(struct lsa_hdr).
919 */
920 ls_length -= sizeof(struct lsa_hdr);
921
922 switch (GET_U_1(lsap->ls_hdr.ls_type)) {
923
924 case LS_TYPE_ROUTER:
925 ND_PRINT("\n\t Router LSA Options: [%s]",
926 bittok2str(ospf_rla_flag_values, "none", GET_U_1(lsap->lsa_un.un_rla.rla_flags)));
927
928 rla_count = GET_BE_U_2(lsap->lsa_un.un_rla.rla_count);
929 ND_TCHECK_SIZE(lsap->lsa_un.un_rla.rla_link);
930 rlp = lsap->lsa_un.un_rla.rla_link;
931 for (u_int i = rla_count; i != 0; i--) {
932 ND_TCHECK_SIZE(rlp);
933 switch (GET_U_1(rlp->un_tos.link.link_type)) {
934
935 case RLA_TYPE_VIRTUAL:
936 ND_PRINT("\n\t Virtual Link: Neighbor Router-ID: %s, Interface Address: %s",
937 GET_IPADDR_STRING(rlp->link_id),
938 GET_IPADDR_STRING(rlp->link_data));
939 break;
940
941 case RLA_TYPE_ROUTER:
942 ND_PRINT("\n\t Neighbor Router-ID: %s, Interface Address: %s",
943 GET_IPADDR_STRING(rlp->link_id),
944 GET_IPADDR_STRING(rlp->link_data));
945 break;
946
947 case RLA_TYPE_TRANSIT:
948 ND_PRINT("\n\t Neighbor Network-ID: %s, Interface Address: %s",
949 GET_IPADDR_STRING(rlp->link_id),
950 GET_IPADDR_STRING(rlp->link_data));
951 break;
952
953 case RLA_TYPE_STUB:
954 ND_PRINT("\n\t Stub Network: %s, Mask: %s",
955 GET_IPADDR_STRING(rlp->link_id),
956 GET_IPADDR_STRING(rlp->link_data));
957 break;
958
959 default:
960 ND_PRINT("\n\t Unknown Router Link Type (%u)",
961 GET_U_1(rlp->un_tos.link.link_type));
962 return (ls_end);
963 }
964
965 ospf_print_tos_metrics(ndo, &rlp->un_tos);
966
967 rlp = (const struct rlalink *)((const u_char *)(rlp + 1) +
968 (GET_U_1(rlp->un_tos.link.link_tos_count) * sizeof(union un_tos)));
969 }
970 break;
971
972 case LS_TYPE_NETWORK:
973 ND_PRINT("\n\t Mask %s\n\t Connected Routers:",
974 GET_IPADDR_STRING(lsap->lsa_un.un_nla.nla_mask));
975 ap = lsap->lsa_un.un_nla.nla_router;
976 while ((const u_char *)ap < ls_end) {
977 ND_PRINT("\n\t %s", GET_IPADDR_STRING(ap));
978 ++ap;
979 }
980 break;
981
982 case LS_TYPE_SUM_IP:
983 ND_TCHECK_4(lsap->lsa_un.un_nla.nla_mask);
984 ND_PRINT("\n\t Mask %s",
985 GET_IPADDR_STRING(lsap->lsa_un.un_sla.sla_mask));
986 ND_TCHECK_SIZE(lsap->lsa_un.un_sla.sla_tosmetric);
987 lp = (const uint8_t *)lsap->lsa_un.un_sla.sla_tosmetric;
988 while (lp < ls_end) {
989 uint32_t ul;
990
991 ul = GET_BE_U_4(lp);
992 topology = (ul & SLA_MASK_TOS) >> SLA_SHIFT_TOS;
993 ND_PRINT("\n\t\ttopology %s (%u) metric %u",
994 tok2str(ospf_topology_values, "Unknown", topology),
995 topology,
996 ul & SLA_MASK_METRIC);
997 lp += 4;
998 }
999 break;
1000
1001 case LS_TYPE_SUM_ABR:
1002 ND_TCHECK_SIZE(lsap->lsa_un.un_sla.sla_tosmetric);
1003 lp = (const uint8_t *)lsap->lsa_un.un_sla.sla_tosmetric;
1004 while (lp < ls_end) {
1005 uint32_t ul;
1006
1007 ul = GET_BE_U_4(lp);
1008 topology = (ul & SLA_MASK_TOS) >> SLA_SHIFT_TOS;
1009 ND_PRINT("\n\t\ttopology %s (%u) metric %u",
1010 tok2str(ospf_topology_values, "Unknown", topology),
1011 topology,
1012 ul & SLA_MASK_METRIC);
1013 lp += 4;
1014 }
1015 break;
1016
1017 case LS_TYPE_ASE:
1018 case LS_TYPE_NSSA: /* fall through - those LSAs share the same format */
1019 ND_TCHECK_4(lsap->lsa_un.un_nla.nla_mask);
1020 ND_PRINT("\n\t Mask %s",
1021 GET_IPADDR_STRING(lsap->lsa_un.un_asla.asla_mask));
1022
1023 ND_TCHECK_SIZE(lsap->lsa_un.un_sla.sla_tosmetric);
1024 almp = lsap->lsa_un.un_asla.asla_metric;
1025 while ((const u_char *)almp < ls_end) {
1026 uint32_t ul;
1027
1028 ul = GET_BE_U_4(almp->asla_tosmetric);
1029 topology = ((ul & ASLA_MASK_TOS) >> ASLA_SHIFT_TOS);
1030 ND_PRINT("\n\t\ttopology %s (%u), type %u, metric",
1031 tok2str(ospf_topology_values, "Unknown", topology),
1032 topology,
1033 (ul & ASLA_FLAG_EXTERNAL) ? 2 : 1);
1034 if ((ul & ASLA_MASK_METRIC) == 0xffffff)
1035 ND_PRINT(" infinite");
1036 else
1037 ND_PRINT(" %u", (ul & ASLA_MASK_METRIC));
1038
1039 if (GET_IPV4_TO_NETWORK_ORDER(almp->asla_forward) != 0) {
1040 ND_PRINT(", forward %s", GET_IPADDR_STRING(almp->asla_forward));
1041 }
1042 if (GET_IPV4_TO_NETWORK_ORDER(almp->asla_tag) != 0) {
1043 ND_PRINT(", tag %s", GET_IPADDR_STRING(almp->asla_tag));
1044 }
1045 ++almp;
1046 }
1047 break;
1048
1049 case LS_TYPE_GROUP:
1050 /* Multicast extensions as of 23 July 1991 */
1051 mcp = lsap->lsa_un.un_mcla;
1052 while ((const u_char *)mcp < ls_end) {
1053 switch (GET_BE_U_4(mcp->mcla_vtype)) {
1054
1055 case MCLA_VERTEX_ROUTER:
1056 ND_PRINT("\n\t Router Router-ID %s",
1057 GET_IPADDR_STRING(mcp->mcla_vid));
1058 break;
1059
1060 case MCLA_VERTEX_NETWORK:
1061 ND_PRINT("\n\t Network Designated Router %s",
1062 GET_IPADDR_STRING(mcp->mcla_vid));
1063 break;
1064
1065 default:
1066 ND_PRINT("\n\t unknown VertexType (%u)",
1067 GET_BE_U_4(mcp->mcla_vtype));
1068 break;
1069 }
1070 ++mcp;
1071 }
1072 break;
1073
1074 case LS_TYPE_OPAQUE_LL: /* fall through */
1075 case LS_TYPE_OPAQUE_AL:
1076 case LS_TYPE_OPAQUE_DW:
1077
1078 switch (GET_U_1(lsap->ls_hdr.un_lsa_id.opaque_field.opaque_type)) {
1079 case LS_OPAQUE_TYPE_RI:
1080 tptr = (const uint8_t *)(lsap->lsa_un.un_ri_tlv);
1081
1082 u_int ls_length_remaining = ls_length;
1083 while (ls_length_remaining != 0) {
1084 ND_TCHECK_4(tptr);
1085 if (ls_length_remaining < 4) {
1086 ND_PRINT("\n\t Remaining LS length %u < 4", ls_length_remaining);
1087 return(ls_end);
1088 }
1089 tlv_type = GET_BE_U_2(tptr);
1090 tlv_length = GET_BE_U_2(tptr + 2);
1091 tptr+=4;
1092 ls_length_remaining-=4;
1093
1094 ND_PRINT("\n\t %s TLV (%u), length: %u, value: ",
1095 tok2str(lsa_opaque_ri_tlv_values,"unknown",tlv_type),
1096 tlv_type,
1097 tlv_length);
1098
1099 if (tlv_length > ls_length_remaining) {
1100 ND_PRINT("\n\t Bogus length %u > remaining LS length %u", tlv_length,
1101 ls_length_remaining);
1102 return(ls_end);
1103 }
1104 ND_TCHECK_LEN(tptr, tlv_length);
1105 switch(tlv_type) {
1106
1107 case LS_OPAQUE_RI_TLV_CAP:
1108 if (tlv_length != 4) {
1109 ND_PRINT("\n\t Bogus length %u != 4", tlv_length);
1110 return(ls_end);
1111 }
1112 ND_PRINT("Capabilities: %s",
1113 bittok2str(lsa_opaque_ri_tlv_cap_values, "Unknown", GET_BE_U_4(tptr)));
1114 break;
1115
1116 case LS_OPAQUE_RI_TLV_HOSTNAME:
1117 ND_PRINT("\n\t Hostname: ");
1118 nd_printjnp(ndo, tptr, tlv_length);
1119 break;
1120
1121 case LS_OPAQUE_RI_TLV_SR_ALGO:
1122 num_tlv = tlv_length;
1123 while (num_tlv >= 1) {
1124 ND_PRINT("\n\t %s (%u)",
1125 tok2str(lsa_opaque_ri_tlv_sr_algos, "Unknown", GET_U_1(tptr+tlv_length-num_tlv)), GET_U_1(tptr+tlv_length-num_tlv));
1126 num_tlv--;
1127 }
1128 break;
1129
1130 case LS_OPAQUE_RI_TLV_SID_LABEL_RANGE:
1131 case LS_OPAQUE_RI_TLV_SR_LOCAL_BLOCK:
1132 ND_TCHECK_4(tptr);
1133 ND_PRINT("\n\t Range size: %u", GET_BE_U_3(tptr));
1134 if (ospf_print_ri_lsa_sid_label_range_tlv(ndo, tptr+4, tlv_length-4) == -1) {
1135 return(ls_end);
1136 }
1137 break;
1138
1139 case LS_OPAQUE_RI_TLV_SRMS_PREFERENCE:
1140 if (tlv_length != 4) {
1141 ND_PRINT("\n\t Bogus SRMS Preference TLV length %u != 4", tlv_length);
1142 return(ls_end);
1143 }
1144 ND_PRINT("\n\t SRMS Preference: %u", GET_U_1(tptr));
1145 break;
1146
1147 default:
1148 if (ndo->ndo_vflag <= 1) {
1149 if (!print_unknown_data(ndo, tptr, "\n\t ", tlv_length))
1150 return(ls_end);
1151 }
1152 break;
1153
1154 }
1155
1156 /* in OSPF everything has to be 32-bit aligned, including TLVs */
1157 if (tlv_length % 4) {
1158 tlv_length += (4 - (tlv_length % 4));
1159 }
1160 tptr+=tlv_length;
1161 ls_length_remaining-=tlv_length;
1162 }
1163 break;
1164
1165 case LS_OPAQUE_TYPE_GRACE:
1166 if (ospf_grace_lsa_print(ndo, (const u_char *)(lsap->lsa_un.un_grace_tlv),
1167 ls_length) == -1) {
1168 return(ls_end);
1169 }
1170 break;
1171
1172 case LS_OPAQUE_TYPE_TE:
1173 if (ospf_te_lsa_print(ndo, (const u_char *)(lsap->lsa_un.un_te_lsa_tlv),
1174 ls_length) == -1) {
1175 return(ls_end);
1176 }
1177 break;
1178
1179 case LS_OPAQUE_TYPE_EP:
1180 if (ospf_ep_lsa_print(ndo, (const u_char *)(lsap->lsa_un.un_ep_tlv),
1181 ls_length) == -1) {
1182 return(ls_end);
1183 }
1184 break;
1185
1186 default:
1187 if (ndo->ndo_vflag <= 1) {
1188 if (!print_unknown_data(ndo, (const uint8_t *)lsap->lsa_un.un_unknown,
1189 "\n\t ", ls_length))
1190 return(ls_end);
1191 }
1192 break;
1193 }
1194 }
1195
1196 /* do we want to see an additionally hexdump ? */
1197 if (ndo->ndo_vflag> 1)
1198 if (!print_unknown_data(ndo, (const uint8_t *)lsap->lsa_un.un_unknown,
1199 "\n\t ", ls_length)) {
1200 return(ls_end);
1201 }
1202
1203 return (ls_end);
1204 trunc:
1205 return (NULL);
1206 }
1207
1208 static void
1209 ospf_decode_lls(netdissect_options *ndo,
1210 const struct ospfhdr *op, u_int length)
1211 {
1212 const u_char *dptr;
1213 const u_char *dataend;
1214 u_int length2;
1215 uint16_t lls_type, lls_len;
1216 uint32_t lls_flags;
1217
1218 switch (GET_U_1(op->ospf_type)) {
1219
1220 case OSPF_TYPE_HELLO:
1221 if (!(GET_U_1(op->ospf_hello.hello_options) & OSPF_OPTION_L))
1222 return;
1223 break;
1224
1225 case OSPF_TYPE_DD:
1226 if (!(GET_U_1(op->ospf_db.db_options) & OSPF_OPTION_L))
1227 return;
1228 break;
1229
1230 default:
1231 return;
1232 }
1233
1234 /* dig deeper if LLS data is available; see RFC4813 */
1235 length2 = GET_BE_U_2(op->ospf_len);
1236 dptr = (const u_char *)op + length2;
1237 dataend = (const u_char *)op + length;
1238
1239 if (GET_BE_U_2(op->ospf_authtype) == OSPF_AUTH_MD5) {
1240 dptr = dptr + GET_U_1(op->ospf_authdata + 3);
1241 length2 += GET_U_1(op->ospf_authdata + 3);
1242 }
1243 if (length2 >= length) {
1244 ND_PRINT("\n\t[LLS truncated]");
1245 return;
1246 }
1247 ND_PRINT("\n\t LLS: checksum: 0x%04x", (u_int) GET_BE_U_2(dptr));
1248
1249 dptr += 2;
1250 length2 = GET_BE_U_2(dptr);
1251 ND_PRINT(", length: %u", length2);
1252
1253 dptr += 2;
1254 while (dptr < dataend) {
1255 lls_type = GET_BE_U_2(dptr);
1256 ND_PRINT("\n\t %s (%u)",
1257 tok2str(ospf_lls_tlv_values,"Unknown TLV",lls_type),
1258 lls_type);
1259 dptr += 2;
1260 lls_len = GET_BE_U_2(dptr);
1261 ND_PRINT(", length: %u", lls_len);
1262 dptr += 2;
1263 switch (lls_type) {
1264
1265 case OSPF_LLS_EO:
1266 if (lls_len != 4) {
1267 ND_PRINT(" [should be 4]");
1268 lls_len = 4;
1269 }
1270 lls_flags = GET_BE_U_4(dptr);
1271 ND_PRINT("\n\t Options: 0x%08x [%s]", lls_flags,
1272 bittok2str(ospf_lls_eo_options, "?", lls_flags));
1273
1274 break;
1275
1276 case OSPF_LLS_MD5:
1277 if (lls_len != 20) {
1278 ND_PRINT(" [should be 20]");
1279 lls_len = 20;
1280 }
1281 ND_PRINT("\n\t Sequence number: 0x%08x", GET_BE_U_4(dptr));
1282 break;
1283 }
1284
1285 dptr += lls_len;
1286 }
1287 }
1288
1289 static int
1290 ospf_decode_v2(netdissect_options *ndo,
1291 const struct ospfhdr *op, const u_char *dataend)
1292 {
1293 const nd_ipv4 *ap;
1294 const struct lsr *lsrp;
1295 const struct lsa_hdr *lshp;
1296 const struct lsa *lsap;
1297 uint32_t lsa_count,lsa_count_max;
1298
1299 switch (GET_U_1(op->ospf_type)) {
1300
1301 case OSPF_TYPE_HELLO:
1302 ND_PRINT("\n\tOptions [%s]",
1303 bittok2str(ospf_option_values,"none",GET_U_1(op->ospf_hello.hello_options)));
1304
1305 ND_PRINT("\n\t Hello Timer %us, Dead Timer %us, Mask %s, Priority %u",
1306 GET_BE_U_2(op->ospf_hello.hello_helloint),
1307 GET_BE_U_4(op->ospf_hello.hello_deadint),
1308 GET_IPADDR_STRING(op->ospf_hello.hello_mask),
1309 GET_U_1(op->ospf_hello.hello_priority));
1310
1311 if (GET_IPV4_TO_NETWORK_ORDER(op->ospf_hello.hello_dr) != 0)
1312 ND_PRINT("\n\t Designated Router %s",
1313 GET_IPADDR_STRING(op->ospf_hello.hello_dr));
1314
1315 if (GET_IPV4_TO_NETWORK_ORDER(op->ospf_hello.hello_bdr) != 0)
1316 ND_PRINT(", Backup Designated Router %s",
1317 GET_IPADDR_STRING(op->ospf_hello.hello_bdr));
1318
1319 ap = op->ospf_hello.hello_neighbor;
1320 if ((const u_char *)ap < dataend)
1321 ND_PRINT("\n\t Neighbor List:");
1322 while ((const u_char *)ap < dataend) {
1323 ND_PRINT("\n\t %s", GET_IPADDR_STRING(ap));
1324 ++ap;
1325 }
1326 break; /* HELLO */
1327
1328 case OSPF_TYPE_DD:
1329 ND_PRINT("\n\tOptions [%s]",
1330 bittok2str(ospf_option_values, "none", GET_U_1(op->ospf_db.db_options)));
1331 ND_PRINT(", DD Flags [%s]",
1332 bittok2str(ospf_dd_flag_values, "none", GET_U_1(op->ospf_db.db_flags)));
1333 if (GET_BE_U_2(op->ospf_db.db_ifmtu)) {
1334 ND_PRINT(", MTU: %u",
1335 GET_BE_U_2(op->ospf_db.db_ifmtu));
1336 }
1337 ND_PRINT(", Sequence: 0x%08x", GET_BE_U_4(op->ospf_db.db_seq));
1338
1339 /* Print all the LS adv's */
1340 lshp = op->ospf_db.db_lshdr;
1341 while (((const u_char *)lshp < dataend) && ospf_print_lshdr(ndo, lshp) != -1) {
1342 ++lshp;
1343 }
1344 break;
1345
1346 case OSPF_TYPE_LS_REQ:
1347 lsrp = op->ospf_lsr;
1348 while ((const u_char *)lsrp < dataend) {
1349 ND_TCHECK_SIZE(lsrp);
1350
1351 ND_PRINT("\n\t Advertising Router: %s, %s LSA (%u)",
1352 GET_IPADDR_STRING(lsrp->ls_router),
1353 tok2str(lsa_values,"unknown",GET_BE_U_4(lsrp->ls_type)),
1354 GET_BE_U_4(lsrp->ls_type));
1355
1356 switch (GET_BE_U_4(lsrp->ls_type)) {
1357 /* the LSA header for opaque LSAs was slightly changed */
1358 case LS_TYPE_OPAQUE_LL:
1359 case LS_TYPE_OPAQUE_AL:
1360 case LS_TYPE_OPAQUE_DW:
1361 ND_PRINT(", Opaque-Type: %s LSA (%u), Opaque-ID: %u",
1362 tok2str(lsa_opaque_values, "unknown",GET_U_1(lsrp->un_ls_stateid.opaque_field.opaque_type)),
1363 GET_U_1(lsrp->un_ls_stateid.opaque_field.opaque_type),
1364 GET_BE_U_3(lsrp->un_ls_stateid.opaque_field.opaque_id));
1365 break;
1366 default:
1367 ND_PRINT(", LSA-ID: %s",
1368 GET_IPADDR_STRING(lsrp->un_ls_stateid.ls_stateid));
1369 break;
1370 }
1371
1372 ++lsrp;
1373 }
1374 break;
1375
1376 case OSPF_TYPE_LS_UPDATE:
1377 lsap = op->ospf_lsu.lsu_lsa;
1378 lsa_count_max = GET_BE_U_4(op->ospf_lsu.lsu_count);
1379 ND_PRINT(", %u LSA%s", lsa_count_max, PLURAL_SUFFIX(lsa_count_max));
1380 for (lsa_count=1;lsa_count <= lsa_count_max;lsa_count++) {
1381 ND_PRINT("\n\t LSA #%u", lsa_count);
1382 lsap = (const struct lsa *)ospf_print_lsa(ndo, lsap);
1383 if (lsap == NULL)
1384 goto trunc;
1385 }
1386 break;
1387
1388 case OSPF_TYPE_LS_ACK:
1389 lshp = op->ospf_lsa.lsa_lshdr;
1390 while ((const u_char *)lshp < dataend) {
1391 ospf_print_lshdr(ndo, lshp);
1392 ++lshp;
1393 }
1394 break;
1395
1396 default:
1397 break;
1398 }
1399 return (0);
1400 trunc:
1401 return (1);
1402 }
1403
1404 void
1405 ospf_print(netdissect_options *ndo,
1406 const u_char *bp, u_int length,
1407 const u_char *bp2 _U_)
1408 {
1409 const struct ospfhdr *op;
1410 const u_char *dataend;
1411 const char *cp;
1412
1413 ndo->ndo_protocol = "ospf2";
1414 op = (const struct ospfhdr *)bp;
1415
1416 /* XXX Before we do anything else, strip off the MD5 trailer */
1417 if (GET_BE_U_2(op->ospf_authtype) == OSPF_AUTH_MD5) {
1418 length -= OSPF_AUTH_MD5_LEN;
1419 ndo->ndo_snapend -= OSPF_AUTH_MD5_LEN;
1420 }
1421
1422 /* If the type is valid translate it, or just print the type */
1423 /* value. If it's not valid, say so and return */
1424 cp = tok2str(type2str, "unknown LS-type %u", GET_U_1(op->ospf_type));
1425 ND_PRINT("OSPFv%u, %s, length %u", GET_U_1(op->ospf_version), cp,
1426 length);
1427 if (*cp == 'u')
1428 return;
1429
1430 if (!ndo->ndo_vflag) { /* non verbose - so lets bail out here */
1431 return;
1432 }
1433
1434 if (length != GET_BE_U_2(op->ospf_len)) {
1435 ND_PRINT(" [len %u]", GET_BE_U_2(op->ospf_len));
1436 }
1437
1438 if (length > GET_BE_U_2(op->ospf_len)) {
1439 dataend = bp + GET_BE_U_2(op->ospf_len);
1440 } else {
1441 dataend = bp + length;
1442 }
1443
1444 ND_PRINT("\n\tRouter-ID %s", GET_IPADDR_STRING(op->ospf_routerid));
1445
1446 if (GET_IPV4_TO_NETWORK_ORDER(op->ospf_areaid) != 0)
1447 ND_PRINT(", Area %s", GET_IPADDR_STRING(op->ospf_areaid));
1448 else
1449 ND_PRINT(", Backbone Area");
1450
1451 if (ndo->ndo_vflag) {
1452 /* Print authentication data (should we really do this?) */
1453 ND_TCHECK_LEN(op->ospf_authdata, sizeof(op->ospf_authdata));
1454
1455 ND_PRINT(", Authentication Type: %s (%u)",
1456 tok2str(ospf_authtype_values, "unknown", GET_BE_U_2(op->ospf_authtype)),
1457 GET_BE_U_2(op->ospf_authtype));
1458
1459 switch (GET_BE_U_2(op->ospf_authtype)) {
1460
1461 case OSPF_AUTH_NONE:
1462 break;
1463
1464 case OSPF_AUTH_SIMPLE:
1465 ND_PRINT("\n\tSimple text password: ");
1466 nd_printjnp(ndo, op->ospf_authdata, OSPF_AUTH_SIMPLE_LEN);
1467 break;
1468
1469 case OSPF_AUTH_MD5:
1470 ND_PRINT("\n\tKey-ID: %u, Auth-Length: %u, Crypto Sequence Number: 0x%08x",
1471 GET_U_1(op->ospf_authdata + 2),
1472 GET_U_1(op->ospf_authdata + 3),
1473 GET_BE_U_4((op->ospf_authdata) + 4));
1474 break;
1475
1476 default:
1477 return;
1478 }
1479 }
1480 /* Do rest according to version. */
1481 switch (GET_U_1(op->ospf_version)) {
1482
1483 case 2:
1484 /* ospf version 2 */
1485 if (ospf_decode_v2(ndo, op, dataend))
1486 goto trunc;
1487 if (length > GET_BE_U_2(op->ospf_len))
1488 ospf_decode_lls(ndo, op, length);
1489 break;
1490
1491 default:
1492 ND_PRINT(" ospf [version %u]", GET_U_1(op->ospf_version));
1493 break;
1494 } /* end switch on version */
1495
1496 return;
1497 trunc:
1498 nd_trunc_longjmp(ndo);
1499 }