etc/services - sync with NetBSD-8
[minix.git] / external / bsd / tcpdump / dist / print-ospf.c
blobf8f78e5d11fa71a9f9e554244ac360db6bfb1da0
1 /*
2 * Copyright (c) 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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.
21 * OSPF support contributed by Jeffrey Honig (jch@mitchell.cit.cornell.edu)
24 #include <sys/cdefs.h>
25 #ifndef lint
26 __RCSID("$NetBSD: print-ospf.c,v 1.6 2015/03/31 21:59:35 christos Exp $");
27 #endif
29 #define NETDISSECT_REWORKED
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
34 #include <tcpdump-stdinc.h>
36 #include "interface.h"
37 #include "addrtoname.h"
38 #include "extract.h"
39 #include "gmpls.h"
41 #include "ospf.h"
43 static const char tstr[] = " [|ospf2]";
45 static const struct tok ospf_option_values[] = {
46 { OSPF_OPTION_T, "MultiTopology" }, /* draft-ietf-ospf-mt-09 */
47 { OSPF_OPTION_E, "External" },
48 { OSPF_OPTION_MC, "Multicast" },
49 { OSPF_OPTION_NP, "NSSA" },
50 { OSPF_OPTION_L, "LLS" },
51 { OSPF_OPTION_DC, "Demand Circuit" },
52 { OSPF_OPTION_O, "Opaque" },
53 { OSPF_OPTION_DN, "Up/Down" },
54 { 0, NULL }
57 static const struct tok ospf_authtype_values[] = {
58 { OSPF_AUTH_NONE, "none" },
59 { OSPF_AUTH_SIMPLE, "simple" },
60 { OSPF_AUTH_MD5, "MD5" },
61 { 0, NULL }
64 static const struct tok ospf_rla_flag_values[] = {
65 { RLA_FLAG_B, "ABR" },
66 { RLA_FLAG_E, "ASBR" },
67 { RLA_FLAG_W1, "Virtual" },
68 { RLA_FLAG_W2, "W2" },
69 { 0, NULL }
72 static const struct tok type2str[] = {
73 { OSPF_TYPE_UMD, "UMD" },
74 { OSPF_TYPE_HELLO, "Hello" },
75 { OSPF_TYPE_DD, "Database Description" },
76 { OSPF_TYPE_LS_REQ, "LS-Request" },
77 { OSPF_TYPE_LS_UPDATE, "LS-Update" },
78 { OSPF_TYPE_LS_ACK, "LS-Ack" },
79 { 0, NULL }
82 static const struct tok lsa_values[] = {
83 { LS_TYPE_ROUTER, "Router" },
84 { LS_TYPE_NETWORK, "Network" },
85 { LS_TYPE_SUM_IP, "Summary" },
86 { LS_TYPE_SUM_ABR, "ASBR Summary" },
87 { LS_TYPE_ASE, "External" },
88 { LS_TYPE_GROUP, "Multicast Group" },
89 { LS_TYPE_NSSA, "NSSA" },
90 { LS_TYPE_OPAQUE_LL, "Link Local Opaque" },
91 { LS_TYPE_OPAQUE_AL, "Area Local Opaque" },
92 { LS_TYPE_OPAQUE_DW, "Domain Wide Opaque" },
93 { 0, NULL }
96 static const struct tok ospf_dd_flag_values[] = {
97 { OSPF_DB_INIT, "Init" },
98 { OSPF_DB_MORE, "More" },
99 { OSPF_DB_MASTER, "Master" },
100 { OSPF_DB_RESYNC, "OOBResync" },
101 { 0, NULL }
104 static const struct tok lsa_opaque_values[] = {
105 { LS_OPAQUE_TYPE_TE, "Traffic Engineering" },
106 { LS_OPAQUE_TYPE_GRACE, "Graceful restart" },
107 { LS_OPAQUE_TYPE_RI, "Router Information" },
108 { 0, NULL }
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 }
117 static const struct tok lsa_opaque_te_link_tlv_subtlv_values[] = {
118 { LS_OPAQUE_TE_LINK_SUBTLV_LINK_TYPE, "Link Type" },
119 { LS_OPAQUE_TE_LINK_SUBTLV_LINK_ID, "Link ID" },
120 { LS_OPAQUE_TE_LINK_SUBTLV_LOCAL_IP, "Local Interface IP address" },
121 { LS_OPAQUE_TE_LINK_SUBTLV_REMOTE_IP, "Remote Interface IP address" },
122 { LS_OPAQUE_TE_LINK_SUBTLV_TE_METRIC, "Traffic Engineering Metric" },
123 { LS_OPAQUE_TE_LINK_SUBTLV_MAX_BW, "Maximum Bandwidth" },
124 { LS_OPAQUE_TE_LINK_SUBTLV_MAX_RES_BW, "Maximum Reservable Bandwidth" },
125 { LS_OPAQUE_TE_LINK_SUBTLV_UNRES_BW, "Unreserved Bandwidth" },
126 { LS_OPAQUE_TE_LINK_SUBTLV_ADMIN_GROUP, "Administrative Group" },
127 { LS_OPAQUE_TE_LINK_SUBTLV_LINK_LOCAL_REMOTE_ID, "Link Local/Remote Identifier" },
128 { LS_OPAQUE_TE_LINK_SUBTLV_LINK_PROTECTION_TYPE, "Link Protection Type" },
129 { LS_OPAQUE_TE_LINK_SUBTLV_INTF_SW_CAP_DESCR, "Interface Switching Capability" },
130 { LS_OPAQUE_TE_LINK_SUBTLV_SHARED_RISK_GROUP, "Shared Risk Link Group" },
131 { LS_OPAQUE_TE_LINK_SUBTLV_BW_CONSTRAINTS, "Bandwidth Constraints" },
132 { 0, NULL }
135 static const struct tok lsa_opaque_grace_tlv_values[] = {
136 { LS_OPAQUE_GRACE_TLV_PERIOD, "Grace Period" },
137 { LS_OPAQUE_GRACE_TLV_REASON, "Graceful restart Reason" },
138 { LS_OPAQUE_GRACE_TLV_INT_ADDRESS, "IPv4 interface address" },
139 { 0, NULL }
142 static const struct tok lsa_opaque_grace_tlv_reason_values[] = {
143 { LS_OPAQUE_GRACE_TLV_REASON_UNKNOWN, "Unknown" },
144 { LS_OPAQUE_GRACE_TLV_REASON_SW_RESTART, "Software Restart" },
145 { LS_OPAQUE_GRACE_TLV_REASON_SW_UPGRADE, "Software Reload/Upgrade" },
146 { LS_OPAQUE_GRACE_TLV_REASON_CP_SWITCH, "Control Processor Switch" },
147 { 0, NULL }
150 static const struct tok lsa_opaque_te_tlv_link_type_sub_tlv_values[] = {
151 { LS_OPAQUE_TE_LINK_SUBTLV_LINK_TYPE_PTP, "Point-to-point" },
152 { LS_OPAQUE_TE_LINK_SUBTLV_LINK_TYPE_MA, "Multi-Access" },
153 { 0, NULL }
156 static const struct tok lsa_opaque_ri_tlv_values[] = {
157 { LS_OPAQUE_RI_TLV_CAP, "Router Capabilities" },
158 { 0, NULL }
161 static const struct tok lsa_opaque_ri_tlv_cap_values[] = {
162 { 1, "Reserved" },
163 { 2, "Reserved" },
164 { 4, "Reserved" },
165 { 8, "Reserved" },
166 { 16, "graceful restart capable" },
167 { 32, "graceful restart helper" },
168 { 64, "Stub router support" },
169 { 128, "Traffic engineering" },
170 { 256, "p2p over LAN" },
171 { 512, "path computation server" },
172 { 0, NULL }
175 static const struct tok ospf_lls_tlv_values[] = {
176 { OSPF_LLS_EO, "Extended Options" },
177 { OSPF_LLS_MD5, "MD5 Authentication" },
178 { 0, NULL }
181 static const struct tok ospf_lls_eo_options[] = {
182 { OSPF_LLS_EO_LR, "LSDB resync" },
183 { OSPF_LLS_EO_RS, "Restart" },
184 { 0, NULL }
188 ospf_print_grace_lsa(netdissect_options *ndo,
189 const uint8_t *tptr, u_int ls_length)
191 u_int tlv_type, tlv_length;
194 while (ls_length > 0) {
195 ND_TCHECK2(*tptr, 4);
196 if (ls_length < 4) {
197 ND_PRINT((ndo, "\n\t Remaining LS length %u < 4", ls_length));
198 return -1;
200 tlv_type = EXTRACT_16BITS(tptr);
201 tlv_length = EXTRACT_16BITS(tptr+2);
202 tptr+=4;
203 ls_length-=4;
205 ND_PRINT((ndo, "\n\t %s TLV (%u), length %u, value: ",
206 tok2str(lsa_opaque_grace_tlv_values,"unknown",tlv_type),
207 tlv_type,
208 tlv_length));
210 if (tlv_length > ls_length) {
211 ND_PRINT((ndo, "\n\t Bogus length %u > %u", tlv_length,
212 ls_length));
213 return -1;
216 /* Infinite loop protection. */
217 if (tlv_type == 0 || tlv_length ==0) {
218 return -1;
221 ND_TCHECK2(*tptr, tlv_length);
222 switch(tlv_type) {
224 case LS_OPAQUE_GRACE_TLV_PERIOD:
225 if (tlv_length != 4) {
226 ND_PRINT((ndo, "\n\t Bogus length %u != 4", tlv_length));
227 return -1;
229 ND_PRINT((ndo, "%us", EXTRACT_32BITS(tptr)));
230 break;
232 case LS_OPAQUE_GRACE_TLV_REASON:
233 if (tlv_length != 1) {
234 ND_PRINT((ndo, "\n\t Bogus length %u != 1", tlv_length));
235 return -1;
237 ND_PRINT((ndo, "%s (%u)",
238 tok2str(lsa_opaque_grace_tlv_reason_values, "Unknown", *tptr),
239 *tptr));
240 break;
242 case LS_OPAQUE_GRACE_TLV_INT_ADDRESS:
243 if (tlv_length != 4) {
244 ND_PRINT((ndo, "\n\t Bogus length %u != 4", tlv_length));
245 return -1;
247 ND_PRINT((ndo, "%s", ipaddr_string(ndo, tptr)));
248 break;
250 default:
251 if (ndo->ndo_vflag <= 1) {
252 if (!print_unknown_data(ndo, tptr, "\n\t ", tlv_length))
253 return -1;
255 break;
258 /* in OSPF everything has to be 32-bit aligned, including TLVs */
259 if (tlv_length%4 != 0)
260 tlv_length+=4-(tlv_length%4);
261 ls_length-=tlv_length;
262 tptr+=tlv_length;
265 return 0;
266 trunc:
267 return -1;
271 ospf_print_te_lsa(netdissect_options *ndo,
272 const uint8_t *tptr, u_int ls_length)
274 u_int tlv_type, tlv_length, subtlv_type, subtlv_length;
275 u_int priority_level, te_class, count_srlg;
276 union { /* int to float conversion buffer for several subTLVs */
277 float f;
278 uint32_t i;
279 } bw;
281 while (ls_length != 0) {
282 ND_TCHECK2(*tptr, 4);
283 if (ls_length < 4) {
284 ND_PRINT((ndo, "\n\t Remaining LS length %u < 4", ls_length));
285 return -1;
287 tlv_type = EXTRACT_16BITS(tptr);
288 tlv_length = EXTRACT_16BITS(tptr+2);
289 tptr+=4;
290 ls_length-=4;
292 ND_PRINT((ndo, "\n\t %s TLV (%u), length: %u",
293 tok2str(lsa_opaque_te_tlv_values,"unknown",tlv_type),
294 tlv_type,
295 tlv_length));
297 if (tlv_length > ls_length) {
298 ND_PRINT((ndo, "\n\t Bogus length %u > %u", tlv_length,
299 ls_length));
300 return -1;
303 /* Infinite loop protection. */
304 if (tlv_type == 0 || tlv_length ==0) {
305 return -1;
308 switch(tlv_type) {
309 case LS_OPAQUE_TE_TLV_LINK:
310 while (tlv_length >= sizeof(subtlv_type) + sizeof(subtlv_length)) {
311 if (tlv_length < 4) {
312 ND_PRINT((ndo, "\n\t Remaining TLV length %u < 4",
313 tlv_length));
314 return -1;
316 ND_TCHECK2(*tptr, 4);
317 subtlv_type = EXTRACT_16BITS(tptr);
318 subtlv_length = EXTRACT_16BITS(tptr+2);
319 tptr+=4;
320 tlv_length-=4;
322 ND_PRINT((ndo, "\n\t %s subTLV (%u), length: %u",
323 tok2str(lsa_opaque_te_link_tlv_subtlv_values,"unknown",subtlv_type),
324 subtlv_type,
325 subtlv_length));
327 ND_TCHECK2(*tptr, subtlv_length);
328 switch(subtlv_type) {
329 case LS_OPAQUE_TE_LINK_SUBTLV_ADMIN_GROUP:
330 ND_PRINT((ndo, ", 0x%08x", EXTRACT_32BITS(tptr)));
331 break;
332 case LS_OPAQUE_TE_LINK_SUBTLV_LINK_ID:
333 case LS_OPAQUE_TE_LINK_SUBTLV_LINK_LOCAL_REMOTE_ID:
334 ND_PRINT((ndo, ", %s (0x%08x)",
335 ipaddr_string(ndo, tptr),
336 EXTRACT_32BITS(tptr)));
337 if (subtlv_length == 8) /* rfc4203 */
338 ND_PRINT((ndo, ", %s (0x%08x)",
339 ipaddr_string(ndo, tptr+4),
340 EXTRACT_32BITS(tptr + 4)));
341 break;
342 case LS_OPAQUE_TE_LINK_SUBTLV_LOCAL_IP:
343 case LS_OPAQUE_TE_LINK_SUBTLV_REMOTE_IP:
344 ND_PRINT((ndo, ", %s", ipaddr_string(ndo, tptr)));
345 break;
346 case LS_OPAQUE_TE_LINK_SUBTLV_MAX_BW:
347 case LS_OPAQUE_TE_LINK_SUBTLV_MAX_RES_BW:
348 bw.i = EXTRACT_32BITS(tptr);
349 ND_PRINT((ndo, ", %.3f Mbps", bw.f * 8 / 1000000));
350 break;
351 case LS_OPAQUE_TE_LINK_SUBTLV_UNRES_BW:
352 for (te_class = 0; te_class < 8; te_class++) {
353 bw.i = EXTRACT_32BITS(tptr+te_class*4);
354 ND_PRINT((ndo, "\n\t\tTE-Class %u: %.3f Mbps",
355 te_class,
356 bw.f * 8 / 1000000));
358 break;
359 case LS_OPAQUE_TE_LINK_SUBTLV_BW_CONSTRAINTS:
360 ND_PRINT((ndo, "\n\t\tBandwidth Constraints Model ID: %s (%u)",
361 tok2str(diffserv_te_bc_values, "unknown", *tptr),
362 *tptr));
363 /* decode BCs until the subTLV ends */
364 for (te_class = 0; te_class < (subtlv_length-4)/4; te_class++) {
365 bw.i = EXTRACT_32BITS(tptr+4+te_class*4);
366 ND_PRINT((ndo, "\n\t\t Bandwidth constraint CT%u: %.3f Mbps",
367 te_class,
368 bw.f * 8 / 1000000));
370 break;
371 case LS_OPAQUE_TE_LINK_SUBTLV_TE_METRIC:
372 ND_PRINT((ndo, ", Metric %u", EXTRACT_32BITS(tptr)));
373 break;
374 case LS_OPAQUE_TE_LINK_SUBTLV_LINK_PROTECTION_TYPE:
375 ND_PRINT((ndo, ", %s, Priority %u",
376 bittok2str(gmpls_link_prot_values, "none", *tptr),
377 *(tptr + 1)));
378 break;
379 case LS_OPAQUE_TE_LINK_SUBTLV_INTF_SW_CAP_DESCR:
380 ND_PRINT((ndo, "\n\t\tInterface Switching Capability: %s",
381 tok2str(gmpls_switch_cap_values, "Unknown", *(tptr))));
382 ND_PRINT((ndo, "\n\t\tLSP Encoding: %s\n\t\tMax LSP Bandwidth:",
383 tok2str(gmpls_encoding_values, "Unknown", *(tptr + 1))));
384 for (priority_level = 0; priority_level < 8; priority_level++) {
385 bw.i = EXTRACT_32BITS(tptr+4+(priority_level*4));
386 ND_PRINT((ndo, "\n\t\t priority level %d: %.3f Mbps",
387 priority_level,
388 bw.f * 8 / 1000000));
390 break;
391 case LS_OPAQUE_TE_LINK_SUBTLV_LINK_TYPE:
392 ND_PRINT((ndo, ", %s (%u)",
393 tok2str(lsa_opaque_te_tlv_link_type_sub_tlv_values,"unknown",*tptr),
394 *tptr));
395 break;
397 case LS_OPAQUE_TE_LINK_SUBTLV_SHARED_RISK_GROUP:
398 count_srlg = subtlv_length / 4;
399 if (count_srlg != 0)
400 ND_PRINT((ndo, "\n\t\t Shared risk group: "));
401 while (count_srlg > 0) {
402 bw.i = EXTRACT_32BITS(tptr);
403 ND_PRINT((ndo, "%d", bw.i));
404 tptr+=4;
405 count_srlg--;
406 if (count_srlg > 0)
407 ND_PRINT((ndo, ", "));
409 break;
411 default:
412 if (ndo->ndo_vflag <= 1) {
413 if (!print_unknown_data(ndo, tptr, "\n\t\t", subtlv_length))
414 return -1;
416 break;
418 /* in OSPF everything has to be 32-bit aligned, including subTLVs */
419 if (subtlv_length%4 != 0)
420 subtlv_length+=4-(subtlv_length%4);
422 tlv_length-=subtlv_length;
423 tptr+=subtlv_length;
426 break;
428 case LS_OPAQUE_TE_TLV_ROUTER:
429 if (tlv_length < 4) {
430 ND_PRINT((ndo, "\n\t TLV length %u < 4", tlv_length));
431 return -1;
433 ND_TCHECK2(*tptr, 4);
434 ND_PRINT((ndo, ", %s", ipaddr_string(ndo, tptr)));
435 break;
437 default:
438 if (ndo->ndo_vflag <= 1) {
439 if (!print_unknown_data(ndo, tptr, "\n\t ", tlv_length))
440 return -1;
442 break;
444 /* in OSPF everything has to be 32-bit aligned, including TLVs */
445 if (tlv_length%4 != 0)
446 tlv_length+=4-(tlv_length%4);
447 ls_length-=tlv_length;
448 tptr+=tlv_length;
450 return 0;
451 trunc:
452 return -1;
455 static int
456 ospf_print_lshdr(netdissect_options *ndo,
457 register const struct lsa_hdr *lshp)
459 u_int ls_length;
461 ND_TCHECK(lshp->ls_length);
462 ls_length = EXTRACT_16BITS(&lshp->ls_length);
463 if (ls_length < sizeof(struct lsa_hdr)) {
464 ND_PRINT((ndo, "\n\t Bogus length %u < header (%lu)", ls_length,
465 (unsigned long)sizeof(struct lsa_hdr)));
466 return(-1);
469 ND_TCHECK(lshp->ls_seq); /* XXX - ls_length check checked this */
470 ND_PRINT((ndo, "\n\t Advertising Router %s, seq 0x%08x, age %us, length %u",
471 ipaddr_string(ndo, &lshp->ls_router),
472 EXTRACT_32BITS(&lshp->ls_seq),
473 EXTRACT_16BITS(&lshp->ls_age),
474 ls_length - (u_int)sizeof(struct lsa_hdr)));
476 ND_TCHECK(lshp->ls_type); /* XXX - ls_length check checked this */
477 switch (lshp->ls_type) {
478 /* the LSA header for opaque LSAs was slightly changed */
479 case LS_TYPE_OPAQUE_LL:
480 case LS_TYPE_OPAQUE_AL:
481 case LS_TYPE_OPAQUE_DW:
482 ND_PRINT((ndo, "\n\t %s LSA (%d), Opaque-Type %s LSA (%u), Opaque-ID %u",
483 tok2str(lsa_values,"unknown",lshp->ls_type),
484 lshp->ls_type,
486 tok2str(lsa_opaque_values,
487 "unknown",
488 *(&lshp->un_lsa_id.opaque_field.opaque_type)),
489 *(&lshp->un_lsa_id.opaque_field.opaque_type),
490 EXTRACT_24BITS(&lshp->un_lsa_id.opaque_field.opaque_id)
493 break;
495 /* all other LSA types use regular style LSA headers */
496 default:
497 ND_PRINT((ndo, "\n\t %s LSA (%d), LSA-ID: %s",
498 tok2str(lsa_values,"unknown",lshp->ls_type),
499 lshp->ls_type,
500 ipaddr_string(ndo, &lshp->un_lsa_id.lsa_id)));
501 break;
504 ND_TCHECK(lshp->ls_options); /* XXX - ls_length check checked this */
505 ND_PRINT((ndo, "\n\t Options: [%s]", bittok2str(ospf_option_values, "none", lshp->ls_options)));
507 return (ls_length);
508 trunc:
509 return (-1);
512 /* draft-ietf-ospf-mt-09 */
513 static const struct tok ospf_topology_values[] = {
514 { 0, "default " },
515 { 1, "multicast " },
516 { 2, "management " },
517 { 0, NULL }
521 * Print all the per-topology metrics.
523 static void
524 ospf_print_tos_metrics(netdissect_options *ndo,
525 const union un_tos *tos)
527 int metric_count;
528 int toscount;
530 toscount = tos->link.link_tos_count+1;
531 metric_count = 0;
534 * All but the first metric contain a valid topology id.
536 while (toscount) {
537 ND_PRINT((ndo, "\n\t\ttopology %s(%u), metric %u",
538 tok2str(ospf_topology_values, "",
539 metric_count ? tos->metrics.tos_type : 0),
540 metric_count ? tos->metrics.tos_type : 0,
541 EXTRACT_16BITS(&tos->metrics.tos_metric)));
542 metric_count++;
543 tos++;
544 toscount--;
549 * Print a single link state advertisement. If truncated or if LSA length
550 * field is less than the length of the LSA header, return NULl, else
551 * return pointer to data past end of LSA.
553 static const uint8_t *
554 ospf_print_lsa(netdissect_options *ndo,
555 register const struct lsa *lsap)
557 register const uint8_t *ls_end;
558 register const struct rlalink *rlp;
559 register const struct in_addr *ap;
560 register const struct aslametric *almp;
561 register const struct mcla *mcp;
562 register const uint32_t *lp;
563 register int j, tlv_type, tlv_length, topology;
564 register int ls_length;
565 const uint8_t *tptr;
567 tptr = (uint8_t *)lsap->lsa_un.un_unknown; /* squelch compiler warnings */
568 ls_length = ospf_print_lshdr(ndo, &lsap->ls_hdr);
569 if (ls_length == -1)
570 return(NULL);
571 ls_end = (uint8_t *)lsap + ls_length;
572 ls_length -= sizeof(struct lsa_hdr);
574 switch (lsap->ls_hdr.ls_type) {
576 case LS_TYPE_ROUTER:
577 ND_TCHECK(lsap->lsa_un.un_rla.rla_flags);
578 ND_PRINT((ndo, "\n\t Router LSA Options: [%s]",
579 bittok2str(ospf_rla_flag_values, "none", lsap->lsa_un.un_rla.rla_flags)));
581 ND_TCHECK(lsap->lsa_un.un_rla.rla_count);
582 j = EXTRACT_16BITS(&lsap->lsa_un.un_rla.rla_count);
583 ND_TCHECK(lsap->lsa_un.un_rla.rla_link);
584 rlp = lsap->lsa_un.un_rla.rla_link;
585 while (j--) {
586 ND_TCHECK(*rlp);
587 switch (rlp->un_tos.link.link_type) {
589 case RLA_TYPE_VIRTUAL:
590 ND_PRINT((ndo, "\n\t Virtual Link: Neighbor Router-ID: %s, Interface Address: %s",
591 ipaddr_string(ndo, &rlp->link_id),
592 ipaddr_string(ndo, &rlp->link_data)));
593 break;
595 case RLA_TYPE_ROUTER:
596 ND_PRINT((ndo, "\n\t Neighbor Router-ID: %s, Interface Address: %s",
597 ipaddr_string(ndo, &rlp->link_id),
598 ipaddr_string(ndo, &rlp->link_data)));
599 break;
601 case RLA_TYPE_TRANSIT:
602 ND_PRINT((ndo, "\n\t Neighbor Network-ID: %s, Interface Address: %s",
603 ipaddr_string(ndo, &rlp->link_id),
604 ipaddr_string(ndo, &rlp->link_data)));
605 break;
607 case RLA_TYPE_STUB:
608 ND_PRINT((ndo, "\n\t Stub Network: %s, Mask: %s",
609 ipaddr_string(ndo, &rlp->link_id),
610 ipaddr_string(ndo, &rlp->link_data)));
611 break;
613 default:
614 ND_PRINT((ndo, "\n\t Unknown Router Link Type (%u)",
615 rlp->un_tos.link.link_type));
616 return (ls_end);
619 ospf_print_tos_metrics(ndo, &rlp->un_tos);
621 rlp = (struct rlalink *)((u_char *)(rlp + 1) +
622 ((rlp->un_tos.link.link_tos_count) * sizeof(union un_tos)));
624 break;
626 case LS_TYPE_NETWORK:
627 ND_TCHECK(lsap->lsa_un.un_nla.nla_mask);
628 ND_PRINT((ndo, "\n\t Mask %s\n\t Connected Routers:",
629 ipaddr_string(ndo, &lsap->lsa_un.un_nla.nla_mask)));
630 ap = lsap->lsa_un.un_nla.nla_router;
631 while ((u_char *)ap < ls_end) {
632 ND_TCHECK(*ap);
633 ND_PRINT((ndo, "\n\t %s", ipaddr_string(ndo, ap)));
634 ++ap;
636 break;
638 case LS_TYPE_SUM_IP:
639 ND_TCHECK(lsap->lsa_un.un_nla.nla_mask);
640 ND_PRINT((ndo, "\n\t Mask %s",
641 ipaddr_string(ndo, &lsap->lsa_un.un_sla.sla_mask)));
642 ND_TCHECK(lsap->lsa_un.un_sla.sla_tosmetric);
643 lp = lsap->lsa_un.un_sla.sla_tosmetric;
644 while ((u_char *)lp < ls_end) {
645 register uint32_t ul;
647 ND_TCHECK(*lp);
648 ul = EXTRACT_32BITS(lp);
649 topology = (ul & SLA_MASK_TOS) >> SLA_SHIFT_TOS;
650 ND_PRINT((ndo, "\n\t\ttopology %s(%u) metric %d",
651 tok2str(ospf_topology_values, "", topology),
652 topology,
653 ul & SLA_MASK_METRIC));
654 ++lp;
656 break;
658 case LS_TYPE_SUM_ABR:
659 ND_TCHECK(lsap->lsa_un.un_sla.sla_tosmetric);
660 lp = lsap->lsa_un.un_sla.sla_tosmetric;
661 while ((u_char *)lp < ls_end) {
662 register uint32_t ul;
664 ND_TCHECK(*lp);
665 ul = EXTRACT_32BITS(lp);
666 topology = (ul & SLA_MASK_TOS) >> SLA_SHIFT_TOS;
667 ND_PRINT((ndo, "\n\t\ttopology %s(%u) metric %d",
668 tok2str(ospf_topology_values, "", topology),
669 topology,
670 ul & SLA_MASK_METRIC));
671 ++lp;
673 break;
675 case LS_TYPE_ASE:
676 case LS_TYPE_NSSA: /* fall through - those LSAs share the same format */
677 ND_TCHECK(lsap->lsa_un.un_nla.nla_mask);
678 ND_PRINT((ndo, "\n\t Mask %s",
679 ipaddr_string(ndo, &lsap->lsa_un.un_asla.asla_mask)));
681 ND_TCHECK(lsap->lsa_un.un_sla.sla_tosmetric);
682 almp = lsap->lsa_un.un_asla.asla_metric;
683 while ((u_char *)almp < ls_end) {
684 register uint32_t ul;
686 ND_TCHECK(almp->asla_tosmetric);
687 ul = EXTRACT_32BITS(&almp->asla_tosmetric);
688 topology = ((ul & ASLA_MASK_TOS) >> ASLA_SHIFT_TOS);
689 ND_PRINT((ndo, "\n\t\ttopology %s(%u), type %d, metric",
690 tok2str(ospf_topology_values, "", topology),
691 topology,
692 (ul & ASLA_FLAG_EXTERNAL) ? 2 : 1));
693 if ((ul & ASLA_MASK_METRIC) == 0xffffff)
694 ND_PRINT((ndo, " infinite"));
695 else
696 ND_PRINT((ndo, " %d", (ul & ASLA_MASK_METRIC)));
698 ND_TCHECK(almp->asla_forward);
699 if (almp->asla_forward.s_addr) {
700 ND_PRINT((ndo, ", forward %s", ipaddr_string(ndo, &almp->asla_forward)));
702 ND_TCHECK(almp->asla_tag);
703 if (almp->asla_tag.s_addr) {
704 ND_PRINT((ndo, ", tag %s", ipaddr_string(ndo, &almp->asla_tag)));
706 ++almp;
708 break;
710 case LS_TYPE_GROUP:
711 /* Multicast extensions as of 23 July 1991 */
712 mcp = lsap->lsa_un.un_mcla;
713 while ((u_char *)mcp < ls_end) {
714 ND_TCHECK(mcp->mcla_vid);
715 switch (EXTRACT_32BITS(&mcp->mcla_vtype)) {
717 case MCLA_VERTEX_ROUTER:
718 ND_PRINT((ndo, "\n\t Router Router-ID %s",
719 ipaddr_string(ndo, &mcp->mcla_vid)));
720 break;
722 case MCLA_VERTEX_NETWORK:
723 ND_PRINT((ndo, "\n\t Network Designated Router %s",
724 ipaddr_string(ndo, &mcp->mcla_vid)));
725 break;
727 default:
728 ND_PRINT((ndo, "\n\t unknown VertexType (%u)",
729 EXTRACT_32BITS(&mcp->mcla_vtype)));
730 break;
732 ++mcp;
734 break;
736 case LS_TYPE_OPAQUE_LL: /* fall through */
737 case LS_TYPE_OPAQUE_AL:
738 case LS_TYPE_OPAQUE_DW:
740 switch (*(&lsap->ls_hdr.un_lsa_id.opaque_field.opaque_type)) {
741 case LS_OPAQUE_TYPE_RI:
742 tptr = (uint8_t *)(&lsap->lsa_un.un_ri_tlv.type);
744 while (ls_length != 0) {
745 ND_TCHECK2(*tptr, 4);
746 if (ls_length < 4) {
747 ND_PRINT((ndo, "\n\t Remaining LS length %u < 4", ls_length));
748 return(ls_end);
750 tlv_type = EXTRACT_16BITS(tptr);
751 tlv_length = EXTRACT_16BITS(tptr+2);
752 tptr+=4;
753 ls_length-=4;
755 ND_PRINT((ndo, "\n\t %s TLV (%u), length: %u, value: ",
756 tok2str(lsa_opaque_ri_tlv_values,"unknown",tlv_type),
757 tlv_type,
758 tlv_length));
760 if (tlv_length > ls_length) {
761 ND_PRINT((ndo, "\n\t Bogus length %u > %u", tlv_length,
762 ls_length));
763 return(ls_end);
765 ND_TCHECK2(*tptr, tlv_length);
766 switch(tlv_type) {
768 case LS_OPAQUE_RI_TLV_CAP:
769 if (tlv_length != 4) {
770 ND_PRINT((ndo, "\n\t Bogus length %u != 4", tlv_length));
771 return(ls_end);
773 ND_PRINT((ndo, "Capabilities: %s",
774 bittok2str(lsa_opaque_ri_tlv_cap_values, "Unknown", EXTRACT_32BITS(tptr))));
775 break;
776 default:
777 if (ndo->ndo_vflag <= 1) {
778 if (!print_unknown_data(ndo, tptr, "\n\t ", tlv_length))
779 return(ls_end);
781 break;
784 tptr+=tlv_length;
785 ls_length-=tlv_length;
787 break;
789 case LS_OPAQUE_TYPE_GRACE:
790 if (ospf_print_grace_lsa(ndo, (uint8_t *)(&lsap->lsa_un.un_grace_tlv.type),
791 ls_length) == -1) {
792 return(ls_end);
794 break;
796 case LS_OPAQUE_TYPE_TE:
797 if (ospf_print_te_lsa(ndo, (uint8_t *)(&lsap->lsa_un.un_te_lsa_tlv.type),
798 ls_length) == -1) {
799 return(ls_end);
801 break;
803 default:
804 if (ndo->ndo_vflag <= 1) {
805 if (!print_unknown_data(ndo, (uint8_t *)lsap->lsa_un.un_unknown,
806 "\n\t ", ls_length))
807 return(ls_end);
809 break;
813 /* do we want to see an additionally hexdump ? */
814 if (ndo->ndo_vflag> 1)
815 if (!print_unknown_data(ndo, (uint8_t *)lsap->lsa_un.un_unknown,
816 "\n\t ", ls_length)) {
817 return(ls_end);
820 return (ls_end);
821 trunc:
822 return (NULL);
825 static int
826 ospf_decode_lls(netdissect_options *ndo,
827 register const struct ospfhdr *op, register u_int length)
829 register const u_char *dptr;
830 register const u_char *dataend;
831 register u_int length2;
832 register uint16_t lls_type, lls_len;
833 register uint32_t lls_flags;
835 switch (op->ospf_type) {
837 case OSPF_TYPE_HELLO:
838 if (!(op->ospf_hello.hello_options & OSPF_OPTION_L))
839 return (0);
840 break;
842 case OSPF_TYPE_DD:
843 if (!(op->ospf_db.db_options & OSPF_OPTION_L))
844 return (0);
845 break;
847 default:
848 return (0);
851 /* dig deeper if LLS data is available; see RFC4813 */
852 length2 = EXTRACT_16BITS(&op->ospf_len);
853 dptr = (u_char *)op + length2;
854 dataend = (u_char *)op + length;
856 if (EXTRACT_16BITS(&op->ospf_authtype) == OSPF_AUTH_MD5) {
857 dptr = dptr + op->ospf_authdata[3];
858 length2 += op->ospf_authdata[3];
860 if (length2 >= length) {
861 ND_PRINT((ndo, "\n\t[LLS truncated]"));
862 return (1);
864 ND_TCHECK2(*dptr, 2);
865 ND_PRINT((ndo, "\n\t LLS: checksum: 0x%04x", (u_int)EXTRACT_16BITS(dptr)));
867 dptr += 2;
868 ND_TCHECK2(*dptr, 2);
869 length2 = EXTRACT_16BITS(dptr);
870 ND_PRINT((ndo, ", length: %u", length2));
872 dptr += 2;
873 ND_TCHECK(*dptr);
874 while (dptr < dataend) {
875 ND_TCHECK2(*dptr, 2);
876 lls_type = EXTRACT_16BITS(dptr);
877 ND_PRINT((ndo, "\n\t %s (%u)",
878 tok2str(ospf_lls_tlv_values,"Unknown TLV",lls_type),
879 lls_type));
880 dptr += 2;
881 ND_TCHECK2(*dptr, 2);
882 lls_len = EXTRACT_16BITS(dptr);
883 ND_PRINT((ndo, ", length: %u", lls_len));
884 dptr += 2;
885 switch (lls_type) {
887 case OSPF_LLS_EO:
888 if (lls_len != 4) {
889 ND_PRINT((ndo, " [should be 4]"));
890 lls_len = 4;
892 ND_TCHECK2(*dptr, 4);
893 lls_flags = EXTRACT_32BITS(dptr);
894 ND_PRINT((ndo, "\n\t Options: 0x%08x [%s]", lls_flags,
895 bittok2str(ospf_lls_eo_options, "?", lls_flags)));
897 break;
899 case OSPF_LLS_MD5:
900 if (lls_len != 20) {
901 ND_PRINT((ndo, " [should be 20]"));
902 lls_len = 20;
904 ND_TCHECK2(*dptr, 4);
905 ND_PRINT((ndo, "\n\t Sequence number: 0x%08x", EXTRACT_32BITS(dptr)));
906 break;
909 dptr += lls_len;
912 return (0);
913 trunc:
914 return (1);
917 static int
918 ospf_decode_v2(netdissect_options *ndo,
919 register const struct ospfhdr *op, register const u_char *dataend)
921 register const struct in_addr *ap;
922 register const struct lsr *lsrp;
923 register const struct lsa_hdr *lshp;
924 register const struct lsa *lsap;
925 register uint32_t lsa_count,lsa_count_max;
927 switch (op->ospf_type) {
929 case OSPF_TYPE_UMD:
931 * Rob Coltun's special monitoring packets;
932 * do nothing
934 break;
936 case OSPF_TYPE_HELLO:
937 ND_PRINT((ndo, "\n\tOptions [%s]",
938 bittok2str(ospf_option_values,"none",op->ospf_hello.hello_options)));
940 ND_TCHECK(op->ospf_hello.hello_deadint);
941 ND_PRINT((ndo, "\n\t Hello Timer %us, Dead Timer %us, Mask %s, Priority %u",
942 EXTRACT_16BITS(&op->ospf_hello.hello_helloint),
943 EXTRACT_32BITS(&op->ospf_hello.hello_deadint),
944 ipaddr_string(ndo, &op->ospf_hello.hello_mask),
945 op->ospf_hello.hello_priority));
947 ND_TCHECK(op->ospf_hello.hello_dr);
948 if (op->ospf_hello.hello_dr.s_addr != 0)
949 ND_PRINT((ndo, "\n\t Designated Router %s",
950 ipaddr_string(ndo, &op->ospf_hello.hello_dr)));
952 ND_TCHECK(op->ospf_hello.hello_bdr);
953 if (op->ospf_hello.hello_bdr.s_addr != 0)
954 ND_PRINT((ndo, ", Backup Designated Router %s",
955 ipaddr_string(ndo, &op->ospf_hello.hello_bdr)));
957 ap = op->ospf_hello.hello_neighbor;
958 if ((u_char *)ap < dataend)
959 ND_PRINT((ndo, "\n\t Neighbor List:"));
960 while ((u_char *)ap < dataend) {
961 ND_TCHECK(*ap);
962 ND_PRINT((ndo, "\n\t %s", ipaddr_string(ndo, ap)));
963 ++ap;
965 break; /* HELLO */
967 case OSPF_TYPE_DD:
968 ND_TCHECK(op->ospf_db.db_options);
969 ND_PRINT((ndo, "\n\tOptions [%s]",
970 bittok2str(ospf_option_values, "none", op->ospf_db.db_options)));
971 ND_TCHECK(op->ospf_db.db_flags);
972 ND_PRINT((ndo, ", DD Flags [%s]",
973 bittok2str(ospf_dd_flag_values, "none", op->ospf_db.db_flags)));
974 ND_TCHECK(op->ospf_db.db_ifmtu);
975 if (op->ospf_db.db_ifmtu) {
976 ND_PRINT((ndo, ", MTU: %u", EXTRACT_16BITS(&op->ospf_db.db_ifmtu)));
978 ND_TCHECK(op->ospf_db.db_seq);
979 ND_PRINT((ndo, ", Sequence: 0x%08x", EXTRACT_32BITS(&op->ospf_db.db_seq)));
981 /* Print all the LS adv's */
982 lshp = op->ospf_db.db_lshdr;
983 while (((u_char *)lshp < dataend) && ospf_print_lshdr(ndo, lshp) != -1) {
984 ++lshp;
986 break;
988 case OSPF_TYPE_LS_REQ:
989 lsrp = op->ospf_lsr;
990 while ((u_char *)lsrp < dataend) {
991 ND_TCHECK(*lsrp);
993 ND_PRINT((ndo, "\n\t Advertising Router: %s, %s LSA (%u)",
994 ipaddr_string(ndo, &lsrp->ls_router),
995 tok2str(lsa_values,"unknown",EXTRACT_32BITS(lsrp->ls_type)),
996 EXTRACT_32BITS(&lsrp->ls_type)));
998 switch (EXTRACT_32BITS(lsrp->ls_type)) {
999 /* the LSA header for opaque LSAs was slightly changed */
1000 case LS_TYPE_OPAQUE_LL:
1001 case LS_TYPE_OPAQUE_AL:
1002 case LS_TYPE_OPAQUE_DW:
1003 ND_PRINT((ndo, ", Opaque-Type: %s LSA (%u), Opaque-ID: %u",
1004 tok2str(lsa_opaque_values, "unknown",lsrp->un_ls_stateid.opaque_field.opaque_type),
1005 lsrp->un_ls_stateid.opaque_field.opaque_type,
1006 EXTRACT_24BITS(&lsrp->un_ls_stateid.opaque_field.opaque_id)));
1007 break;
1008 default:
1009 ND_PRINT((ndo, ", LSA-ID: %s",
1010 ipaddr_string(ndo, &lsrp->un_ls_stateid.ls_stateid)));
1011 break;
1014 ++lsrp;
1016 break;
1018 case OSPF_TYPE_LS_UPDATE:
1019 lsap = op->ospf_lsu.lsu_lsa;
1020 ND_TCHECK(op->ospf_lsu.lsu_count);
1021 lsa_count_max = EXTRACT_32BITS(&op->ospf_lsu.lsu_count);
1022 ND_PRINT((ndo, ", %d LSA%s", lsa_count_max, PLURAL_SUFFIX(lsa_count_max)));
1023 for (lsa_count=1;lsa_count <= lsa_count_max;lsa_count++) {
1024 ND_PRINT((ndo, "\n\t LSA #%u", lsa_count));
1025 lsap = (const struct lsa *)ospf_print_lsa(ndo, lsap);
1026 if (lsap == NULL)
1027 goto trunc;
1029 break;
1031 case OSPF_TYPE_LS_ACK:
1032 lshp = op->ospf_lsa.lsa_lshdr;
1033 while (ospf_print_lshdr(ndo, lshp) != -1) {
1034 ++lshp;
1036 break;
1038 default:
1039 break;
1041 return (0);
1042 trunc:
1043 return (1);
1046 void
1047 ospf_print(netdissect_options *ndo,
1048 register const u_char *bp, register u_int length,
1049 const u_char *bp2 _U_)
1051 register const struct ospfhdr *op;
1052 register const u_char *dataend;
1053 register const char *cp;
1055 op = (struct ospfhdr *)bp;
1057 /* XXX Before we do anything else, strip off the MD5 trailer */
1058 ND_TCHECK(op->ospf_authtype);
1059 if (EXTRACT_16BITS(&op->ospf_authtype) == OSPF_AUTH_MD5) {
1060 length -= OSPF_AUTH_MD5_LEN;
1061 ndo->ndo_snapend -= OSPF_AUTH_MD5_LEN;
1064 /* If the type is valid translate it, or just print the type */
1065 /* value. If it's not valid, say so and return */
1066 ND_TCHECK(op->ospf_type);
1067 cp = tok2str(type2str, "unknown LS-type", op->ospf_type);
1068 ND_PRINT((ndo, "OSPFv%u, %s, length %u", op->ospf_version, cp, length));
1069 if (*cp == 'u')
1070 return;
1072 if (!ndo->ndo_vflag) { /* non verbose - so lets bail out here */
1073 return;
1076 ND_TCHECK(op->ospf_len);
1077 if (length != EXTRACT_16BITS(&op->ospf_len)) {
1078 ND_PRINT((ndo, " [len %d]", EXTRACT_16BITS(&op->ospf_len)));
1081 if (length > EXTRACT_16BITS(&op->ospf_len)) {
1082 dataend = bp + EXTRACT_16BITS(&op->ospf_len);
1083 } else {
1084 dataend = bp + length;
1087 ND_TCHECK(op->ospf_routerid);
1088 ND_PRINT((ndo, "\n\tRouter-ID %s", ipaddr_string(ndo, &op->ospf_routerid)));
1090 ND_TCHECK(op->ospf_areaid);
1091 if (op->ospf_areaid.s_addr != 0)
1092 ND_PRINT((ndo, ", Area %s", ipaddr_string(ndo, &op->ospf_areaid)));
1093 else
1094 ND_PRINT((ndo, ", Backbone Area"));
1096 if (ndo->ndo_vflag) {
1097 /* Print authentication data (should we really do this?) */
1098 ND_TCHECK2(op->ospf_authdata[0], sizeof(op->ospf_authdata));
1100 ND_PRINT((ndo, ", Authentication Type: %s (%u)",
1101 tok2str(ospf_authtype_values, "unknown", EXTRACT_16BITS(&op->ospf_authtype)),
1102 EXTRACT_16BITS(&op->ospf_authtype)));
1104 switch (EXTRACT_16BITS(&op->ospf_authtype)) {
1106 case OSPF_AUTH_NONE:
1107 break;
1109 case OSPF_AUTH_SIMPLE:
1110 ND_PRINT((ndo, "\n\tSimple text password: "));
1111 safeputs(ndo, op->ospf_authdata, OSPF_AUTH_SIMPLE_LEN);
1112 break;
1114 case OSPF_AUTH_MD5:
1115 ND_PRINT((ndo, "\n\tKey-ID: %u, Auth-Length: %u, Crypto Sequence Number: 0x%08x",
1116 *((op->ospf_authdata) + 2),
1117 *((op->ospf_authdata) + 3),
1118 EXTRACT_32BITS((op->ospf_authdata) + 4)));
1119 break;
1121 default:
1122 return;
1125 /* Do rest according to version. */
1126 switch (op->ospf_version) {
1128 case 2:
1129 /* ospf version 2 */
1130 if (ospf_decode_v2(ndo, op, dataend))
1131 goto trunc;
1132 if (length > EXTRACT_16BITS(&op->ospf_len)) {
1133 if (ospf_decode_lls(ndo, op, length))
1134 goto trunc;
1136 break;
1138 default:
1139 ND_PRINT((ndo, " ospf [version %d]", op->ospf_version));
1140 break;
1141 } /* end switch on version */
1143 return;
1144 trunc:
1145 ND_PRINT((ndo, "%s", tstr));