From 69f57ab2695248b0a46d4ffafcb0551ef7ced02e Mon Sep 17 00:00:00 2001 From: Christian Giese Date: Tue, 6 Feb 2024 08:41:25 +0000 Subject: [PATCH] log LSA HDR on load external error --- code/bngblaster/src/ospf/ospf_def.h | 3 +++ code/bngblaster/src/ospf/ospf_lsa.c | 14 ++++++++------ code/bngblaster/src/ospf/ospf_utils.c | 18 ++++++++++++++++++ code/bngblaster/src/ospf/ospf_utils.h | 3 +++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/code/bngblaster/src/ospf/ospf_def.h b/code/bngblaster/src/ospf/ospf_def.h index 752b58d4..4621edb4 100644 --- a/code/bngblaster/src/ospf/ospf_def.h +++ b/code/bngblaster/src/ospf/ospf_def.h @@ -66,6 +66,7 @@ #define OSPF_LSA_MAX_AGE_DIFF 900 /* 15 minutes */ #define OSPF_LSA_SEQ_INIT 0x80000000 #define OSPF_LSA_SEQ_MAX 0x7fffffff +#define OSPF_LSA_CHECKSUM_OFFSET 16 #define OSPF_LSA_BORDER_ROUTER 0x01 #define OSPF_LSA_EXTERNAL_ROUTER 0x02 @@ -163,6 +164,8 @@ #define OSPFV2_EXT_PREFIX_TLV 1 +#define OSPF_LSA_HDR_STRING_LEN sizeof("TYPE255:255.255.255.255:255.255.255.255:FFFFFFFF") + typedef struct ospf_config_ ospf_config_s; typedef struct ospf_instance_ ospf_instance_s; typedef struct ospf_interface_ ospf_interface_s; diff --git a/code/bngblaster/src/ospf/ospf_lsa.c b/code/bngblaster/src/ospf/ospf_lsa.c index b12c8139..ec6d34e8 100644 --- a/code/bngblaster/src/ospf/ospf_lsa.c +++ b/code/bngblaster/src/ospf/ospf_lsa.c @@ -389,12 +389,14 @@ ospf_lsa_verify_checksum(ospf_lsa_header_s *hdr) if(len < sizeof(ospf_lsa_header_s)) return false; - checksum = bbl_checksum_fletcher16(&hdr->options, len-OSPF_LSA_AGE_LEN, 14); + checksum = bbl_checksum_fletcher16(&hdr->options, len-OSPF_LSA_AGE_LEN, + OSPF_LSA_CHECKSUM_OFFSET-OSPF_LSA_AGE_LEN); hdr->checksum = checksum_orig; if(checksum == checksum_orig) { return true; } else { + LOG(ERROR, "OSPF LSA checksum error (expected %04x received %04x)\n", checksum, checksum_orig); return false; } } @@ -1962,7 +1964,7 @@ ospf_lsa_load_external(ospf_instance_s *ospf_instance, uint16_t lsa_count, uint8 uint16_t lsa_len; uint8_t lsa_type; - struct timespec now; + struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); while(len >= OSPF_LSA_HDR_LEN && lsa_count) { @@ -1971,12 +1973,12 @@ ospf_lsa_load_external(ospf_instance_s *ospf_instance, uint16_t lsa_count, uint8 lsa_type = hdr->type; if(lsa_type < OSPF_LSA_TYPE_1 || lsa_type > OSPF_LSA_TYPE_MAX) { - LOG(ERROR, "Failed to decode external OSPF LSA (invalid LSA type %u)\n", lsa_type); + LOG(ERROR, "Failed to decode external OSPF LSA %s (invalid LSA type %u)\n", ospf_lsa_hdr_string(hdr), lsa_type); return false; } lsa_len = be16toh(hdr->length); if(lsa_len > len) { - LOG(ERROR, "Failed to decode external OSPF LSA (invalid LSA len %u)\n", lsa_len); + LOG(ERROR, "Failed to decode external OSPF LSA %s (invalid LSA len %u)\n", ospf_lsa_hdr_string(hdr), lsa_len); return false; } @@ -1985,7 +1987,7 @@ ospf_lsa_load_external(ospf_instance_s *ospf_instance, uint16_t lsa_count, uint8 lsa_count--; if(!ospf_lsa_verify_checksum(hdr)) { - LOG_NOARG(ERROR, "Failed to decode external OSPF LSA (invalid LSA checksum)\n"); + LOG(ERROR, "Failed to decode external OSPF LSA %s (invalid LSA checksum)\n", ospf_lsa_hdr_string(hdr)); return false; } @@ -2000,7 +2002,7 @@ ospf_lsa_load_external(ospf_instance_s *ospf_instance, uint16_t lsa_count, uint8 if(result.inserted) { *result.datum_ptr = lsa; } else { - LOG_NOARG(OSPF, "Failed to add external OSPF LSA to LSDB\n"); + LOG(OSPF, "Failed to add external OSPF LSA %s to LSDB\n", ospf_lsa_hdr_string(hdr)); return false; } } diff --git a/code/bngblaster/src/ospf/ospf_utils.c b/code/bngblaster/src/ospf/ospf_utils.c index 58d06f4b..4fca99df 100644 --- a/code/bngblaster/src/ospf/ospf_utils.c +++ b/code/bngblaster/src/ospf/ospf_utils.c @@ -108,3 +108,21 @@ ospf_rx_error(bbl_network_interface_s *interface, ospf_pdu_s *pdu, const char *e interface->stats.ospf_rx_error++; } +char * +ospf_lsa_hdr_string(ospf_lsa_header_s *hdr) +{ + static char buffer[8][OSPF_LSA_HDR_STRING_LEN]; + static int idx = 0; + char *ret; + ret = buffer[idx]; + idx = (idx+1) & 7; + + uint32_t id = hdr->id; + uint32_t router = hdr->router; + snprintf(ret, OSPF_LSA_HDR_STRING_LEN, "TYPE%u:%s:%s:%04x", + hdr->type, + format_ipv4_address(&id), + format_ipv4_address(&router), + be32toh(hdr->seq)); + return ret; +} diff --git a/code/bngblaster/src/ospf/ospf_utils.h b/code/bngblaster/src/ospf/ospf_utils.h index 61e4e15a..7983b5d0 100644 --- a/code/bngblaster/src/ospf/ospf_utils.h +++ b/code/bngblaster/src/ospf/ospf_utils.h @@ -33,4 +33,7 @@ ospf_pdu_type_string(uint8_t type); void ospf_rx_error(bbl_network_interface_s *interface, ospf_pdu_s *pdu, const char *error); +char * +ospf_lsa_hdr_string(ospf_lsa_header_s *hdr); + #endif \ No newline at end of file