Add CIDR support in Historical Flows

This commit is contained in:
Alfredo Cardigliano 2023-01-25 14:38:50 +01:00
parent eaf6f9ebbf
commit 3a36fb9ba3
6 changed files with 89 additions and 45 deletions

View file

@ -1119,6 +1119,9 @@ function historical_flow_utils.get_tags()
flow_defined_tags["srv_location"] = tag_utils.defined_tags["srv_location"]
flow_defined_tags["traffic_direction"] = tag_utils.defined_tags["traffic_direction"]
flow_defined_tags["confidence"] = tag_utils.defined_tags["confidence"]
flow_defined_tags["network_cidr"] = tag_utils.defined_tags["network_cidr"]
flow_defined_tags["srv_network_cidr"] = tag_utils.defined_tags["srv_network_cidr"]
flow_defined_tags["cli_network_cidr"] = tag_utils.defined_tags["cli_network_cidr"]
return flow_defined_tags
end

View file

@ -1508,7 +1508,9 @@ local known_parameters = {
["old_member"] = validateMember,
["network"] = validateNumber, -- A network ID/name
["network_name"] = validateFilters(validateNetwork),
["network_cidr"] = validateNetwork, -- A network expressed with the /
["network_cidr"] = validateEmptyOr(validateListOfTypeInline(validateFilters(validateNetwork))), -- An IPv4 or IPv6 subnet in CIDR format
["cli_network_cidr"] = validateEmptyOr(validateListOfTypeInline(validateFilters(validateNetwork))), -- An IPv4 or IPv6 subnet in CIDR format
["srv_network_cidr"] = validateEmptyOr(validateListOfTypeInline(validateFilters(validateNetwork))), -- An IPv4 or IPv6 subnet in CIDR format
["cli_network"] = validateEmptyOr(validateListOfTypeInline(validateFilters(validateNumber))), -- A network ID
["srv_network"] = validateEmptyOr(validateListOfTypeInline(validateFilters(validateNumber))), -- A network ID
["ip"] = validateEmptyOr(validateListOfTypeInline(validateFilters(validateServer))), -- An IPv4 or IPv6 address or an Hostname

View file

@ -500,50 +500,6 @@ end
-- ##############################################
function isIPv4(address)
-- Reuse the for loop to check the address validity
local checkAddress = (function(chunks)
for _, v in pairs(chunks) do
if (tonumber(v) < 0) or (tonumber(v) > 255) then
return false
end
end
return true
end)
local chunks = {address:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$")}
local chunksWithPort = {address:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)%:(%d+)$")}
if #chunks == 4 then
return checkAddress(chunks)
elseif #chunksWithPort == 5 then
table.remove(chunksWithPort, 5)
return checkAddress(chunksWithPort)
end
return false
end
-- ##############################################
function isIPv4Network(address)
local parts = split(address, "/")
if #parts == 2 then
local prefix = tonumber(parts[2])
if (prefix == nil) or (math.floor(prefix) ~= prefix) or (prefix < 0) or (prefix > 32) then
return false
end
elseif #parts ~= 1 then
return false
end
return isIPv4(parts[1])
end
-- ##############################################
--
-- Fix member format (IP address to /32 CIDR and VLAN to default 0)
-- E.g. 192.168.1.10 -> 192.168.1.10/32@0

View file

@ -147,12 +147,74 @@ end
-- ##############################################
function isIPv4(address)
-- Reuse the for loop to check the address validity
local checkAddress = (function(chunks)
for _, v in pairs(chunks) do
if (tonumber(v) < 0) or (tonumber(v) > 255) then
return false
end
end
return true
end)
local chunks = {address:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$")}
local chunksWithPort = {address:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)%:(%d+)$")}
if #chunks == 4 then
return checkAddress(chunks)
elseif #chunksWithPort == 5 then
table.remove(chunksWithPort, 5)
return checkAddress(chunksWithPort)
end
return false
end
-- ##############################################
function isIPv6(ip)
return((not isEmptyString(ip)) and ntop.isIPv6(ip))
end
-- ##############################################
function isIPv4Network(address)
local parts = split(address, "/")
if #parts == 2 then
local prefix = tonumber(parts[2])
if (prefix == nil) or (math.floor(prefix) ~= prefix) or (prefix < 0) or (prefix > 32) then
return false
end
elseif #parts ~= 1 then
return false
end
return isIPv4(parts[1])
end
-- ##############################################
function isIPv6Network(address)
local parts = split(address, "/")
if #parts == 2 then
local prefix = tonumber(parts[2])
if (prefix == nil) or (math.floor(prefix) ~= prefix) or (prefix < 0) or (prefix > 128) then
return false
end
elseif #parts ~= 1 then
return false
end
return isIPv6(parts[1])
end
-- ##############################################
function firstToUpper(str)
str = tostring(str)
return (str:gsub("^%l", string.upper))

View file

@ -91,6 +91,24 @@ tag_utils.defined_tags = {
operators = {'eq', 'neq'},
bpf_key = 'ip host',
},
network_cidr = {
value_type = 'cidr',
i18n_label = i18n('db_search.tags.network_cidr'),
operators = {'eq', 'neq'},
bpf_key = 'net',
},
cli_network_cidr = {
value_type = 'cidr',
i18n_label = i18n('db_search.tags.cli_network_cidr'),
operators = {'eq', 'neq'},
bpf_key = 'net',
},
srv_network_cidr = {
value_type = 'cidr',
i18n_label = i18n('db_search.tags.srv_network_cidr'),
operators = {'eq', 'neq'},
bpf_key = 'net',
},
traffic_direction = {
value_type = 'traffic_direction',
i18n_label = i18n('db_search.tags.traffic_direction'),