suricata
runmode-nflog.c
Go to the documentation of this file.
1/* Copyright (C) 2014-2022 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18/**
19 * \file
20 *
21 * \author Giuseppe Longo <giuseppelng@gmail.com>
22 */
23#include "suricata-common.h"
24#include "tm-threads.h"
25#include "conf.h"
26#include "runmodes.h"
27#include "runmode-nflog.h"
28
29#include "util-debug.h"
30#include "util-device-private.h"
31#include "util-runmodes.h"
32#include "util-misc.h"
33
34#include "source-nflog.h"
35
36#ifdef HAVE_NFLOG
37#include "util-time.h"
38
39static void NflogDerefConfig(void *data)
40{
41 NflogGroupConfig *nflogconf = (NflogGroupConfig *)data;
42 SCFree(nflogconf);
43}
44
45static void *ParseNflogConfig(const char *group)
46{
47 SCConfNode *group_root;
48 SCConfNode *group_default = NULL;
49 SCConfNode *nflog_node;
50 NflogGroupConfig *nflogconf = SCMalloc(sizeof(*nflogconf));
51 intmax_t bufsize;
52 intmax_t bufsize_max;
53 intmax_t qthreshold;
54 intmax_t qtimeout;
55 int boolval;
56
57 if (unlikely(nflogconf == NULL))
58 return NULL;
59
60 if (group == NULL) {
61 SCFree(nflogconf);
62 return NULL;
63 }
64
65 nflogconf->DerefFunc = NflogDerefConfig;
66 nflog_node = SCConfGetNode("nflog");
67
68 if (nflog_node == NULL) {
69 SCLogInfo("Unable to find nflog config using default value");
70 return nflogconf;
71 }
72
73 group_root = SCConfNodeLookupKeyValue(nflog_node, "group", group);
74
75 group_default = SCConfNodeLookupKeyValue(nflog_node, "group", "default");
76
77 if (group_root == NULL && group_default == NULL) {
78 SCLogInfo("Unable to find nflog config for "
79 "group \"%s\" or \"default\", using default value",
80 group);
81 return nflogconf;
82 }
83
84 nflogconf->nful_overrun_warned = 0;
85 strlcpy(nflogconf->numgroup, group, sizeof(nflogconf->numgroup));
86
87 if (ParseSizeStringU16(group, &nflogconf->group) < 0) {
88 FatalError("NFLOG's group number invalid.");
89 }
90
91 boolval = SCConfGetChildValueIntWithDefault(group_root, group_default, "buffer-size", &bufsize);
92
93 if (boolval)
94 nflogconf->nlbufsiz = bufsize;
95 else {
96 SCLogError("Invalid buffer-size value");
97 SCFree(nflogconf);
98 return NULL;
99 }
100
101 boolval =
102 SCConfGetChildValueIntWithDefault(group_root, group_default, "max-size", &bufsize_max);
103
104 if (boolval)
105 nflogconf->nlbufsiz_max = bufsize_max;
106 else {
107 SCLogError("Invalid max-size value");
108 SCFree(nflogconf);
109 return NULL;
110 }
111
112 if (nflogconf->nlbufsiz > nflogconf->nlbufsiz_max) {
113 SCLogWarning("buffer-size value larger "
114 "than max-size value, adjusting buffer-size");
115 nflogconf->nlbufsiz = nflogconf->nlbufsiz_max;
116 }
117
118 boolval =
119 SCConfGetChildValueIntWithDefault(group_root, group_default, "qthreshold", &qthreshold);
120
121 if (boolval)
122 nflogconf->qthreshold = qthreshold;
123 else {
124 SCLogError("Invalid qthreshold value");
125 SCFree(nflogconf);
126 return NULL;
127 }
128
129 boolval = SCConfGetChildValueIntWithDefault(group_root, group_default, "qtimeout", &qtimeout);
130
131 if (boolval)
132 nflogconf->qtimeout = qtimeout;
133 else {
134 SCLogError("Invalid qtimeout value");
135 SCFree(nflogconf);
136 return NULL;
137 }
138
139 return nflogconf;
140}
141
142static int NflogConfigGeThreadsCount(void *conf)
143{
144 /* for each nflog group there is no reason to use more than 1 thread */
145 return 1;
146}
147#endif
148
149static int RunModeIdsNflogAutoFp(void)
150{
151 SCEnter();
152
153#ifdef HAVE_NFLOG
155
156 int ret = RunModeSetLiveCaptureAutoFp(ParseNflogConfig, NflogConfigGeThreadsCount,
157 "ReceiveNFLOG", "DecodeNFLOG", thread_name_autofp, NULL);
158 if (ret != 0) {
159 FatalError("Unable to start runmode");
160 }
161
162 SCLogInfo("RunModeIdsNflogAutoFp initialised");
163#endif /* HAVE_NFLOG */
164
165 SCReturnInt(0);
166}
167
168static int RunModeIdsNflogSingle(void)
169{
170 SCEnter();
171
172#ifdef HAVE_NFLOG
174
175 int ret = RunModeSetLiveCaptureSingle(ParseNflogConfig, NflogConfigGeThreadsCount,
176 "ReceiveNFLOG", "DecodeNFLOG", thread_name_single, NULL);
177 if (ret != 0) {
178 FatalError("Unable to start runmode");
179 }
180
181 SCLogInfo("RunModeIdsNflogSingle initialised");
182#endif /* HAVE_NFLOG */
183
184 SCReturnInt(0);
185}
186
187static int RunModeIdsNflogWorkers(void)
188{
189 SCEnter();
190
191#ifdef HAVE_NFLOG
193
194 int ret = RunModeSetLiveCaptureWorkers(ParseNflogConfig, NflogConfigGeThreadsCount,
195 "ReceiveNFLOG", "DecodeNFLOG", thread_name_workers, NULL);
196 if (ret != 0) {
197 FatalError("Unable to start runmode");
198 }
199
200 SCLogInfo("RunModeIdsNflogWorkers initialised");
201#endif /* HAVE_NFLOG */
202
203 SCReturnInt(0);
204}
205
207{
208 return "autofp";
209}
210
212{
214 RUNMODE_NFLOG, "autofp", "Multi threaded nflog mode", RunModeIdsNflogAutoFp, NULL);
216 RUNMODE_NFLOG, "single", "Single threaded nflog mode", RunModeIdsNflogSingle, NULL);
218 RUNMODE_NFLOG, "workers", "Workers nflog mode", RunModeIdsNflogWorkers, NULL);
219}
uint8_t group
SCConfNode * SCConfGetNode(const char *name)
Get a SCConfNode by name.
Definition conf.c:181
SCConfNode * SCConfNodeLookupKeyValue(const SCConfNode *base, const char *key, const char *value)
Lookup for a key value under a specific node.
Definition conf.c:841
int SCConfGetChildValueIntWithDefault(const SCConfNode *base, const SCConfNode *dflt, const char *name, intmax_t *val)
Definition conf.c:476
void RunModeIdsNflogRegister(void)
const char * RunModeIdsNflogGetDefaultMode(void)
const char * thread_name_single
Definition runmodes.c:67
void RunModeRegisterNewRunMode(enum SCRunModes runmode, const char *name, const char *description, int(*RunModeFunc)(void), int(*RunModeIsIPSEnabled)(void))
Registers a new runmode.
Definition runmodes.c:486
const char * thread_name_autofp
Definition runmodes.c:66
const char * thread_name_workers
Definition runmodes.c:68
@ RUNMODE_NFLOG
Definition runmodes.h:32
void(* DerefFunc)(void *)
uint32_t nlbufsiz_max
char numgroup[NFLOG_GROUP_NAME_LENGTH]
size_t strlcpy(char *dst, const char *src, size_t siz)
#define SCEnter(...)
Definition util-debug.h:277
#define FatalError(...)
Definition util-debug.h:510
#define SCReturnInt(x)
Definition util-debug.h:281
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition util-debug.h:255
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition util-debug.h:225
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267
#define SCMalloc(sz)
Definition util-mem.h:47
#define SCFree(p)
Definition util-mem.h:61
int ParseSizeStringU16(const char *size, uint16_t *res)
Definition util-misc.c:156
#define unlikely(expr)
int RunModeSetLiveCaptureSingle(ConfigIfaceParserFunc ConfigParser, ConfigIfaceThreadsCountFunc ModThreadsCount, const char *recv_mod_name, const char *decode_mod_name, const char *thread_name, const char *live_dev)
int RunModeSetLiveCaptureWorkers(ConfigIfaceParserFunc ConfigParser, ConfigIfaceThreadsCountFunc ModThreadsCount, const char *recv_mod_name, const char *decode_mod_name, const char *thread_name, const char *live_dev)
int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser, ConfigIfaceThreadsCountFunc ModThreadsCount, const char *recv_mod_name, const char *decode_mod_name, const char *thread_name, const char *live_dev)
void TimeModeSetLive(void)
Definition util-time.c:99