From c2c8cdb78a839004e802df565e428a77656bfa39 Mon Sep 17 00:00:00 2001 From: Juliana Fajardini Date: Sun, 11 Feb 2024 18:47:31 -0300 Subject: [PATCH] exceptions: make types and ToStr fns more accessible Decode file needed ExceptionPolicy types and exception-policy file needed Decode types, rendering some works quite difficult to work around. ExceptionPolicyToStr is useful for registering exception policy counters, so make that public. Part of Task #5816 --- src/Makefile.am | 1 + src/util-exception-policy-types.h | 54 +++++++++++++++++++++++++++++++ src/util-exception-policy.c | 20 ++++++------ src/util-exception-policy.h | 15 ++------- 4 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 src/util-exception-policy-types.h diff --git a/src/Makefile.am b/src/Makefile.am index ec592ed5f6b3..6d22b80ff1f0 100755 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -548,6 +548,7 @@ noinst_HEADERS = \ util-enum.h \ util-error.h \ util-exception-policy.h \ + util-exception-policy-types.h \ util-file-decompression.h \ util-file.h \ util-file-swf-decompression.h \ diff --git a/src/util-exception-policy-types.h b/src/util-exception-policy-types.h new file mode 100644 index 000000000000..04e6d3bca4e4 --- /dev/null +++ b/src/util-exception-policy-types.h @@ -0,0 +1,54 @@ +/* Copyright (C) 2024 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 + * Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * version 2 along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +/** + * \file + */ + +#ifndef UTIL_EXCEPTION_POLICY_TYPES_H +#define UTIL_EXCEPTION_POLICY_TYPES_H + +enum ExceptionPolicy { + EXCEPTION_POLICY_NOT_SET = 0, + EXCEPTION_POLICY_AUTO, + EXCEPTION_POLICY_PASS_PACKET, + EXCEPTION_POLICY_PASS_FLOW, + EXCEPTION_POLICY_BYPASS_FLOW, + EXCEPTION_POLICY_DROP_PACKET, + EXCEPTION_POLICY_DROP_FLOW, + EXCEPTION_POLICY_REJECT, +}; + +#define EXCEPTION_POLICY_MAX EXCEPTION_POLICY_REJECT + 1 + +/* Max length = possible exception policy scenarios + counter names + * + exception policy type. E.g.: + * "tcp.reassembly_exception_policy.drop_packet" + 1 */ +#define EXCEPTION_POLICY_COUNTER_MAX_LEN 44 + +typedef struct ExceptionPolicyCounters_ { + /* Follows enum order */ + uint16_t eps_id[EXCEPTION_POLICY_MAX]; +} ExceptionPolicyCounters; + +typedef struct ExceptionPolicyStatsSetts_ { + char eps_name[EXCEPTION_POLICY_MAX][EXCEPTION_POLICY_COUNTER_MAX_LEN]; + bool valid_settings_ids[EXCEPTION_POLICY_MAX]; + bool valid_settings_ips[EXCEPTION_POLICY_MAX]; +} ExceptionPolicyStatsSetts; + +#endif diff --git a/src/util-exception-policy.c b/src/util-exception-policy.c index 05f88f0c9a3b..879f0b67a702 100644 --- a/src/util-exception-policy.c +++ b/src/util-exception-policy.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2022-2023 Open Information Security Foundation +/* Copyright (C) 2022-2024 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 @@ -32,7 +32,7 @@ enum ExceptionPolicy g_eps_master_switch = EXCEPTION_POLICY_NOT_SET; /** true if exception policy was defined in config */ static bool g_eps_have_exception_policy = false; -static const char *ExceptionPolicyEnumToString(enum ExceptionPolicy policy) +const char *ExceptionPolicyEnumToString(enum ExceptionPolicy policy, bool is_json) { switch (policy) { case EXCEPTION_POLICY_NOT_SET: @@ -44,13 +44,13 @@ static const char *ExceptionPolicyEnumToString(enum ExceptionPolicy policy) case EXCEPTION_POLICY_BYPASS_FLOW: return "bypass"; case EXCEPTION_POLICY_DROP_FLOW: - return "drop-flow"; + return is_json ? "drop_flow" : "drop-flow"; case EXCEPTION_POLICY_DROP_PACKET: - return "drop-packet"; + return is_json ? "drop_packet" : "drop-packet"; case EXCEPTION_POLICY_PASS_PACKET: - return "pass-packet"; + return is_json ? "pass_packet" : "pass-packet"; case EXCEPTION_POLICY_PASS_FLOW: - return "pass-flow"; + return is_json ? "pass_flow" : "pass-flow"; } // TODO we shouldn't reach this, but if we do, better not to leave this as simply null... return "not set"; @@ -198,7 +198,7 @@ static enum ExceptionPolicy ExceptionPolicyMasterParse(const char *value) } g_eps_have_exception_policy = true; - SCLogInfo("master exception-policy set to: %s", ExceptionPolicyEnumToString(policy)); + SCLogInfo("master exception-policy set to: %s", ExceptionPolicyEnumToString(policy, false)); return policy; } @@ -218,13 +218,13 @@ static enum ExceptionPolicy ExceptionPolicyGetDefault( p = PickPacketAction(option, p); } SCLogConfig("%s: %s (defined via 'exception-policy' master switch)", option, - ExceptionPolicyEnumToString(p)); + ExceptionPolicyEnumToString(p, false)); return p; } else if (EngineModeIsIPS() && !midstream) { p = EXCEPTION_POLICY_DROP_FLOW; } SCLogConfig("%s: %s (defined via 'built-in default' for %s-mode)", option, - ExceptionPolicyEnumToString(p), EngineModeIsIPS() ? "IPS" : "IDS"); + ExceptionPolicyEnumToString(p, false), EngineModeIsIPS() ? "IPS" : "IDS"); return p; } @@ -245,7 +245,7 @@ enum ExceptionPolicy ExceptionPolicyParse(const char *option, bool support_flow) if (!support_flow) { policy = PickPacketAction(option, policy); } - SCLogConfig("%s: %s", option, ExceptionPolicyEnumToString(policy)); + SCLogConfig("%s: %s", option, ExceptionPolicyEnumToString(policy, false)); } } else { policy = ExceptionPolicyGetDefault(option, support_flow, false); diff --git a/src/util-exception-policy.h b/src/util-exception-policy.h index 012888fce37b..ffd199fe527d 100644 --- a/src/util-exception-policy.h +++ b/src/util-exception-policy.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2022-2023 Open Information Security Foundation +/* Copyright (C) 2022-2024 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 @@ -23,18 +23,9 @@ #define SURICATA_UTIL_EXCEPTION_POLICY_H #include "decode.h" +#include "util-exception-policy-types.h" -enum ExceptionPolicy { - EXCEPTION_POLICY_NOT_SET = 0, - EXCEPTION_POLICY_AUTO, - EXCEPTION_POLICY_PASS_PACKET, - EXCEPTION_POLICY_PASS_FLOW, - EXCEPTION_POLICY_BYPASS_FLOW, - EXCEPTION_POLICY_DROP_PACKET, - EXCEPTION_POLICY_DROP_FLOW, - EXCEPTION_POLICY_REJECT, -}; - +const char *ExceptionPolicyEnumToString(enum ExceptionPolicy policy, bool is_json); void SetMasterExceptionPolicy(void); void ExceptionPolicyApply( Packet *p, enum ExceptionPolicy policy, enum PacketDropReason drop_reason);