Skip to content

Commit

Permalink
exceptions: make types and ToStr fns more accessible
Browse files Browse the repository at this point in the history
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 OISF#5816

(cherry picked from commit c2c8cdb)
  • Loading branch information
jufajardini committed Apr 12, 2024
1 parent d5aa4b6 commit d79a2ac
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,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 \
Expand Down
54 changes: 54 additions & 0 deletions src/util-exception-policy-types.h
Original file line number Diff line number Diff line change
@@ -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
20 changes: 10 additions & 10 deletions src/util-exception-policy.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,7 +31,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:
Expand All @@ -43,13 +43,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";
Expand Down Expand Up @@ -197,7 +197,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;
}
Expand All @@ -217,13 +217,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;
}
Expand All @@ -244,7 +244,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);
Expand Down
15 changes: 3 additions & 12 deletions src/util-exception-policy.h
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,18 +23,9 @@
#define __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);
Expand Down

0 comments on commit d79a2ac

Please sign in to comment.