Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Ipv6 subnet summarization #618

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions libs/visor_utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ static void split(const std::string &s, char delim, Out result)
}
}

static uint8_t reverse_bits(uint8_t n)
{
static constexpr std::array<uint8_t, 9> bit_reverse_masks{0, 128, 192, 224, 240, 248, 252, 254, 255};
return bit_reverse_masks[n];
}

std::optional<IPv4subnetList::const_iterator> match_subnet(IPv4subnetList &ipv4_list, uint32_t ipv4_val)
{
if (ipv4_val && !ipv4_list.empty()) {
Expand Down Expand Up @@ -84,14 +90,15 @@ std::array<uint8_t, 16> get_subnet(const uint8_t *addr, uint8_t cidr)
{
std::array<uint8_t, 16> mask{};
std::memcpy(mask.data(), addr, mask.size());
uint8_t bits = 128 - cidr;
uint8_t byte_count = bits / 8;
uint8_t bit_count = bits % 8;
for (uint8_t b = 0; b < mask.size(); ++b) {
if (b < byte_count) {
uint8_t byte_count = cidr / 8;
uint8_t bit_count = cidr % 8;
for (uint8_t b = mask.size(); b-- > 0;) {
if (b > byte_count) {
mask[b] = 0;
} else if (b == byte_count && bit_count) {
mask[b] = mask[b] >> bit_count;
mask[b] &= reverse_bits(bit_count);
} else if (b == byte_count) {
mask[b] = 0;
}
}
return mask;
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/flow/tests/test_flows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ TEST_CASE("Parse IPFIX stream with subnet summary wildcard", "[netflow][flow]")
CHECK(j["devices"]["192.168.100.2"]["interfaces"]["0"]["cardinality"]["dst_ports_out"] == 9);
CHECK(j["devices"]["192.168.100.2"]["interfaces"]["0"]["cardinality"]["src_ports_in"] == 16);
CHECK(j["devices"]["192.168.100.2"]["interfaces"]["0"]["top_out_src_ips_bytes"][0]["estimate"] == 120000);
CHECK(j["devices"]["192.168.100.2"]["interfaces"]["0"]["top_out_src_ips_bytes"][0]["name"] == "::1/24");
CHECK(j["devices"]["192.168.100.2"]["interfaces"]["0"]["top_out_src_ips_bytes"][0]["name"] == "::/24");
CHECK(j["devices"]["192.168.100.2"]["interfaces"]["0"]["top_out_src_ips_packets"][0]["estimate"] == 1472);
}

Expand Down