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