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