|
31 | 31 | #include "bgpdump_lib.h" |
32 | 32 | #include "utils.h" |
33 | 33 |
|
| 34 | +#include "bgpstream.h" |
34 | 35 | #include "bgpstream_utils.h" |
35 | 36 |
|
36 | 37 | #include "bgpstream_debug.h" |
37 | 38 | #include "bgpstream_record.h" |
38 | 39 |
|
39 | 40 | #include "bgpstream_elem_int.h" |
| 41 | +#include "utils/bgpstream_utils_rtr.h" |
40 | 42 |
|
41 | 43 | /* ==================== PROTECTED FUNCTIONS ==================== */ |
42 | 44 |
|
@@ -346,6 +348,15 @@ char *bgpstream_elem_custom_snprintf(char *buf, size_t len, |
346 | 348 | if(B_FULL) |
347 | 349 | return NULL; |
348 | 350 |
|
| 351 | +#if defined(FOUND_RTR) |
| 352 | + /* RPKI Validation */ |
| 353 | + char buf_rpki[1024]; |
| 354 | + c = bgpstream_elem_get_rpki_validation_result_snprintf( |
| 355 | + buf_rpki, sizeof(buf_rpki), elem); |
| 356 | + strcat(buf, buf_rpki); |
| 357 | + written += c; |
| 358 | + buf_p += c; |
| 359 | +#endif |
349 | 360 | /* END OF LINE */ |
350 | 361 | break; |
351 | 362 |
|
@@ -422,3 +433,104 @@ char *bgpstream_elem_snprintf(char *buf, size_t len, |
422 | 433 | { |
423 | 434 | return bgpstream_elem_custom_snprintf(buf, len, elem, 1); |
424 | 435 | } |
| 436 | + |
| 437 | +#if defined(FOUND_RTR) |
| 438 | +int bgpstream_elem_get_rpki_validation_result_snprintf( |
| 439 | + char *buf, size_t len, bgpstream_elem_t const *elem) |
| 440 | +{ |
| 441 | + char result_output[1024] = ""; |
| 442 | + if (elem->annotations.rpki_validation_status != |
| 443 | + BGPSTREAM_ELEM_RPKI_VALIDATION_STATUS_NOTFOUND) { |
| 444 | + snprintf(result_output, sizeof(result_output), "%s%s", result_output, |
| 445 | + elem->annotations.rpki_validation_status == |
| 446 | + BGPSTREAM_ELEM_RPKI_VALIDATION_STATUS_INVALID |
| 447 | + ? "invalid;" |
| 448 | + : "valid;"); |
| 449 | + for (int i = 0; i < elem->annotations.rpki_validation_result.asn_used; |
| 450 | + i++) { |
| 451 | + char asn[1024]; |
| 452 | + snprintf(asn, sizeof(asn), "%" PRIu32 ",", |
| 453 | + elem->annotations.rpki_validation_result.asn_pfx[i].asn); |
| 454 | + strcat(result_output, asn); |
| 455 | + for (int j = 0; |
| 456 | + j < elem->annotations.rpki_validation_result.asn_pfx[i].pfx_used; |
| 457 | + j++) { |
| 458 | + char valid_prefix[INET6_ADDRSTRLEN]; |
| 459 | + bgpstream_pfx_snprintf(valid_prefix, INET6_ADDRSTRLEN, |
| 460 | + (bgpstream_pfx_t *)&elem->annotations.rpki_validation_result |
| 461 | + .asn_pfx[i].pfxs[j].pfx); |
| 462 | + strcat(result_output, valid_prefix); |
| 463 | + snprintf(asn, sizeof(asn), "-%" PRIu8, |
| 464 | + elem->annotations.rpki_validation_result.asn_pfx[i] |
| 465 | + .pfxs[j].max_pfx_len); |
| 466 | + strcat(result_output, asn); |
| 467 | + strcat(result_output, |
| 468 | + j != elem->annotations.rpki_validation_result.asn_pfx[i] |
| 469 | + .pfx_used - 1 |
| 470 | + ? " " |
| 471 | + : ""); |
| 472 | + } |
| 473 | + strcat(result_output, |
| 474 | + i != elem->annotations.rpki_validation_result.asn_used - 1 ? ";" |
| 475 | + : ""); |
| 476 | + } |
| 477 | + } else { |
| 478 | + snprintf(result_output, sizeof(result_output), "%s%s", result_output, |
| 479 | + "notfound"); |
| 480 | + } |
| 481 | + |
| 482 | + return snprintf(buf, len, "%s", result_output); |
| 483 | +} |
| 484 | + |
| 485 | +void bgpstream_elem_get_rpki_validation_result(bgpstream_elem_t *elem, |
| 486 | + char *prefix, |
| 487 | + uint32_t origin_asn, |
| 488 | + uint8_t mask_len) |
| 489 | +{ |
| 490 | + if (elem->annotations.rpki_validation_status == |
| 491 | + BGPSTREAM_ELEM_RPKI_VALIDATION_STATUS_NOTVALIDATED) { |
| 492 | + cfg_tr = bgpstream_get_rtr_config(); |
| 493 | + |
| 494 | + struct reasoned_result res_reasoned = |
| 495 | + bgpstream_rtr_validate_reason(cfg_tr, origin_asn, prefix, mask_len); |
| 496 | + |
| 497 | + if (res_reasoned.result == BGP_PFXV_STATE_VALID) { |
| 498 | + elem->annotations.rpki_validation_status = |
| 499 | + BGPSTREAM_ELEM_RPKI_VALIDATION_STATUS_VALID; |
| 500 | + } |
| 501 | + if (res_reasoned.result == BGP_PFXV_STATE_NOT_FOUND) { |
| 502 | + elem->annotations.rpki_validation_status = |
| 503 | + BGPSTREAM_ELEM_RPKI_VALIDATION_STATUS_NOTFOUND; |
| 504 | + } |
| 505 | + if (res_reasoned.result == BGP_PFXV_STATE_INVALID) { |
| 506 | + elem->annotations.rpki_validation_status = |
| 507 | + BGPSTREAM_ELEM_RPKI_VALIDATION_STATUS_INVALID; |
| 508 | + } |
| 509 | + |
| 510 | + if (elem->annotations.rpki_validation_status != |
| 511 | + BGPSTREAM_ELEM_RPKI_VALIDATION_STATUS_NOTFOUND) { |
| 512 | + bgpstream_rpki_validation_result_init( |
| 513 | + &elem->annotations.rpki_validation_result, 2); |
| 514 | + char valid_prefix[INET6_ADDRSTRLEN]; |
| 515 | + char reason_prefix[INET6_ADDRSTRLEN]; |
| 516 | + |
| 517 | + for (int i = 0; i < res_reasoned.reason_len; i++) { |
| 518 | + bgpstream_rpki_validation_result_insert_asn( |
| 519 | + &elem->annotations.rpki_validation_result, |
| 520 | + res_reasoned.reason[i].asn); |
| 521 | + lrtr_ip_addr_to_str(&(res_reasoned.reason[i].prefix), reason_prefix, |
| 522 | + sizeof(reason_prefix)); |
| 523 | + snprintf(valid_prefix, sizeof(valid_prefix), "%s/%" PRIu8, reason_prefix, |
| 524 | + res_reasoned.reason[i].min_len); |
| 525 | + |
| 526 | + bgpstream_pfx_t pfx; |
| 527 | + bgpstream_str2pfx(valid_prefix, (bgpstream_pfx_storage_t *)&pfx); |
| 528 | + bgpstream_rpki_validation_result_insert_pfx( |
| 529 | + &elem->annotations.rpki_validation_result, |
| 530 | + res_reasoned.reason[i].asn, &pfx, res_reasoned.reason[i].max_len); |
| 531 | + } |
| 532 | + } |
| 533 | + free(res_reasoned.reason); |
| 534 | + } |
| 535 | +} |
| 536 | +#endif |
0 commit comments