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