defrag/config: switch to radix4/6

Splits the unified tree into a ipv4 specific and ipv6 specific tree.
pull/12174/head
Victor Julien 4 years ago committed by Victor Julien
parent c75b29c74f
commit 46fb7bb162

@ -1,4 +1,4 @@
/* Copyright (C) 2007-2013 Open Information Security Foundation
/* Copyright (C) 2007-2022 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
@ -25,12 +25,9 @@
#include "suricata-common.h"
#include "defrag-config.h"
#include "util-misc.h"
#include "util-radix-tree.h"
#include "conf.h"
static SCRadixTree *defrag_tree = NULL;
static int default_timeout = 0;
#include "util-radix4-tree.h"
#include "util-radix6-tree.h"
static void DefragPolicyFreeUserData(void *data)
{
@ -38,7 +35,14 @@ static void DefragPolicyFreeUserData(void *data)
SCFree(data);
}
static void DefragPolicyAddHostInfo(char *host_ip_range, uint64_t timeout)
static SCRadix4Tree defrag4_tree = SC_RADIX4_TREE_INITIALIZER;
static SCRadix6Tree defrag6_tree = SC_RADIX6_TREE_INITIALIZER;
static SCRadix4Config defrag4_config = { DefragPolicyFreeUserData, NULL };
static SCRadix6Config defrag6_config = { DefragPolicyFreeUserData, NULL };
static int default_timeout = 0;
static void DefragPolicyAddHostInfo(const char *host_ip_range, uint64_t timeout)
{
uint64_t *user_data = NULL;
@ -50,7 +54,8 @@ static void DefragPolicyAddHostInfo(char *host_ip_range, uint64_t timeout)
if (strchr(host_ip_range, ':') != NULL) {
SCLogDebug("adding ipv6 host %s", host_ip_range);
if (!SCRadixAddKeyIPV6String(host_ip_range, defrag_tree, (void *)user_data)) {
if (!SCRadix6AddKeyIPV6String(
&defrag6_tree, &defrag6_config, host_ip_range, (void *)user_data)) {
SCFree(user_data);
if (sc_errno != SC_EEXIST) {
SCLogWarning("failed to add ipv6 host %s", host_ip_range);
@ -58,8 +63,8 @@ static void DefragPolicyAddHostInfo(char *host_ip_range, uint64_t timeout)
}
} else {
SCLogDebug("adding ipv4 host %s", host_ip_range);
if (!SCRadixAddKeyIPV4String(host_ip_range, defrag_tree, (void *)user_data)) {
SCFree(user_data);
if (!SCRadix4AddKeyIPV4String(
&defrag4_tree, &defrag4_config, host_ip_range, (void *)user_data)) {
if (sc_errno != SC_EEXIST) {
SCLogWarning("failed to add ipv4 host %s", host_ip_range);
}
@ -67,20 +72,20 @@ static void DefragPolicyAddHostInfo(char *host_ip_range, uint64_t timeout)
}
}
static int DefragPolicyGetIPv4HostTimeout(uint8_t *ipv4_addr)
static int DefragPolicyGetIPv4HostTimeout(const uint8_t *ipv4_addr)
{
void *user_data = NULL;
(void)SCRadixFindKeyIPV4BestMatch(ipv4_addr, defrag_tree, &user_data);
(void)SCRadix4TreeFindBestMatch(&defrag4_tree, ipv4_addr, &user_data);
if (user_data == NULL)
return -1;
return *((int *)user_data);
}
static int DefragPolicyGetIPv6HostTimeout(uint8_t *ipv6_addr)
static int DefragPolicyGetIPv6HostTimeout(const uint8_t *ipv6_addr)
{
void *user_data = NULL;
(void)SCRadixFindKeyIPV6BestMatch(ipv6_addr, defrag_tree, &user_data);
(void)SCRadix6TreeFindBestMatch(&defrag6_tree, ipv6_addr, &user_data);
if (user_data == NULL)
return -1;
@ -92,9 +97,9 @@ int DefragPolicyGetHostTimeout(Packet *p)
int timeout = 0;
if (PacketIsIPv4(p))
timeout = DefragPolicyGetIPv4HostTimeout((uint8_t *)GET_IPV4_DST_ADDR_PTR(p));
timeout = DefragPolicyGetIPv4HostTimeout((const uint8_t *)GET_IPV4_DST_ADDR_PTR(p));
else if (PacketIsIPv6(p))
timeout = DefragPolicyGetIPv6HostTimeout((uint8_t *)GET_IPV6_DST_ADDR(p));
timeout = DefragPolicyGetIPv6HostTimeout((const uint8_t *)GET_IPV6_DST_ADDR(p));
if (timeout <= 0)
timeout = default_timeout;
@ -134,11 +139,6 @@ void DefragPolicyLoadFromConfig(void)
{
SCEnter();
defrag_tree = SCRadixCreateRadixTree(DefragPolicyFreeUserData, NULL);
if (defrag_tree == NULL) {
FatalError("Can't alloc memory for the defrag config tree.");
}
ConfNode *server_config = ConfGetNode("defrag.host-config");
if (server_config == NULL) {
SCLogDebug("failed to read host config");
@ -160,8 +160,6 @@ void DefragPolicyLoadFromConfig(void)
void DefragTreeDestroy(void)
{
if (defrag_tree != NULL) {
SCRadixReleaseRadixTree(defrag_tree);
}
defrag_tree = NULL;
SCRadix4TreeRelease(&defrag4_tree, &defrag4_config);
SCRadix6TreeRelease(&defrag6_tree, &defrag6_config);
}

@ -1,4 +1,4 @@
/* Copyright (C) 2007-2013 Open Information Security Foundation
/* Copyright (C) 2007-2022 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free

Loading…
Cancel
Save