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