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 
38 static void NflogDerefConfig(void *data)
39 {
40  NflogGroupConfig *nflogconf = (NflogGroupConfig *)data;
41  SCFree(nflogconf);
42 }
43 
44 static void *ParseNflogConfig(const char *group)
45 {
46  SCConfNode *group_root;
47  SCConfNode *group_default = NULL;
48  SCConfNode *nflog_node;
49  NflogGroupConfig *nflogconf = SCMalloc(sizeof(*nflogconf));
50  intmax_t bufsize;
51  intmax_t bufsize_max;
52  intmax_t qthreshold;
53  intmax_t qtimeout;
54  int boolval;
55 
56  if (unlikely(nflogconf == NULL))
57  return NULL;
58 
59  if (group == NULL) {
60  SCFree(nflogconf);
61  return NULL;
62  }
63 
64  nflogconf->DerefFunc = NflogDerefConfig;
65  nflog_node = SCConfGetNode("nflog");
66 
67  if (nflog_node == NULL) {
68  SCLogInfo("Unable to find nflog config using default value");
69  return nflogconf;
70  }
71 
72  group_root = SCConfNodeLookupKeyValue(nflog_node, "group", group);
73 
74  group_default = SCConfNodeLookupKeyValue(nflog_node, "group", "default");
75 
76  if (group_root == NULL && group_default == NULL) {
77  SCLogInfo("Unable to find nflog config for "
78  "group \"%s\" or \"default\", using default value",
79  group);
80  return nflogconf;
81  }
82 
83  nflogconf->nful_overrun_warned = 0;
84  strlcpy(nflogconf->numgroup, group, sizeof(nflogconf->numgroup));
85 
86  if (ParseSizeStringU16(group, &nflogconf->group) < 0) {
87  FatalError("NFLOG's group number invalid.");
88  }
89 
90  boolval = SCConfGetChildValueIntWithDefault(group_root, group_default, "buffer-size", &bufsize);
91 
92  if (boolval)
93  nflogconf->nlbufsiz = bufsize;
94  else {
95  SCLogError("Invalid buffer-size value");
96  SCFree(nflogconf);
97  return NULL;
98  }
99 
100  boolval =
101  SCConfGetChildValueIntWithDefault(group_root, group_default, "max-size", &bufsize_max);
102 
103  if (boolval)
104  nflogconf->nlbufsiz_max = bufsize_max;
105  else {
106  SCLogError("Invalid max-size value");
107  SCFree(nflogconf);
108  return NULL;
109  }
110 
111  if (nflogconf->nlbufsiz > nflogconf->nlbufsiz_max) {
112  SCLogWarning("buffer-size value larger "
113  "than max-size value, adjusting buffer-size");
114  nflogconf->nlbufsiz = nflogconf->nlbufsiz_max;
115  }
116 
117  boolval =
118  SCConfGetChildValueIntWithDefault(group_root, group_default, "qthreshold", &qthreshold);
119 
120  if (boolval)
121  nflogconf->qthreshold = qthreshold;
122  else {
123  SCLogError("Invalid qthreshold value");
124  SCFree(nflogconf);
125  return NULL;
126  }
127 
128  boolval = SCConfGetChildValueIntWithDefault(group_root, group_default, "qtimeout", &qtimeout);
129 
130  if (boolval)
131  nflogconf->qtimeout = qtimeout;
132  else {
133  SCLogError("Invalid qtimeout value");
134  SCFree(nflogconf);
135  return NULL;
136  }
137 
138  return nflogconf;
139 }
140 
141 static uint16_t NflogConfigGeThreadsCount(void *conf)
142 {
143  /* for each nflog group there is no reason to use more than 1 thread */
144  return 1;
145 }
146 #endif
147 
148 static int RunModeIdsNflogAutoFp(void)
149 {
150  SCEnter();
151 
152 #ifdef HAVE_NFLOG
153  TimeModeSetLive();
154 
155  int ret = RunModeSetLiveCaptureAutoFp(ParseNflogConfig, NflogConfigGeThreadsCount,
156  "ReceiveNFLOG", "DecodeNFLOG", thread_name_autofp, NULL);
157  if (ret != 0) {
158  FatalError("Unable to start runmode");
159  }
160 
161  SCLogInfo("RunModeIdsNflogAutoFp initialised");
162 #endif /* HAVE_NFLOG */
163 
164  SCReturnInt(0);
165 }
166 
167 static int RunModeIdsNflogSingle(void)
168 {
169  SCEnter();
170 
171 #ifdef HAVE_NFLOG
172  TimeModeSetLive();
173 
174  int ret = RunModeSetLiveCaptureSingle(ParseNflogConfig, NflogConfigGeThreadsCount,
175  "ReceiveNFLOG", "DecodeNFLOG", thread_name_single, NULL);
176  if (ret != 0) {
177  FatalError("Unable to start runmode");
178  }
179 
180  SCLogInfo("RunModeIdsNflogSingle initialised");
181 #endif /* HAVE_NFLOG */
182 
183  SCReturnInt(0);
184 }
185 
186 static int RunModeIdsNflogWorkers(void)
187 {
188  SCEnter();
189 
190 #ifdef HAVE_NFLOG
191  TimeModeSetLive();
192 
193  int ret = RunModeSetLiveCaptureWorkers(ParseNflogConfig, NflogConfigGeThreadsCount,
194  "ReceiveNFLOG", "DecodeNFLOG", thread_name_workers, NULL);
195  if (ret != 0) {
196  FatalError("Unable to start runmode");
197  }
198 
199  SCLogInfo("RunModeIdsNflogWorkers initialised");
200 #endif /* HAVE_NFLOG */
201 
202  SCReturnInt(0);
203 }
204 
206 {
207  return "autofp";
208 }
209 
211 {
213  RUNMODE_NFLOG, "autofp", "Multi threaded nflog mode", RunModeIdsNflogAutoFp, NULL);
215  RUNMODE_NFLOG, "single", "Single threaded nflog mode", RunModeIdsNflogSingle, NULL);
217  RUNMODE_NFLOG, "workers", "Workers nflog mode", RunModeIdsNflogWorkers, NULL);
218 }
thread_name_workers
const char * thread_name_workers
Definition: runmodes.c:67
util-device-private.h
tm-threads.h
source-nflog.h
RunModeSetLiveCaptureWorkers
int RunModeSetLiveCaptureWorkers(ConfigIfaceParserFunc ConfigParser, ConfigIfaceThreadsCountFunc ModThreadsCount, const char *recv_mod_name, const char *decode_mod_name, const char *thread_name, const char *live_dev)
Definition: util-runmodes.c:329
RUNMODE_NFLOG
@ RUNMODE_NFLOG
Definition: runmodes.h:32
ParseSizeStringU16
int ParseSizeStringU16(const char *size, uint16_t *res)
Definition: util-misc.c:156
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
util-runmodes.h
thread_name_autofp
const char * thread_name_autofp
Definition: runmodes.c:65
RunModeIdsNflogGetDefaultMode
const char * RunModeIdsNflogGetDefaultMode(void)
Definition: runmode-nflog.c:205
NflogGroupConfig_::numgroup
char numgroup[NFLOG_GROUP_NAME_LENGTH]
Definition: source-nflog.h:47
thread_name_single
const char * thread_name_single
Definition: runmodes.c:66
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
RunModeSetLiveCaptureAutoFp
int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser, ConfigIfaceThreadsCountFunc ModThreadsCount, const char *recv_mod_name, const char *decode_mod_name, const char *thread_name, const char *live_dev)
Definition: util-runmodes.c:85
util-debug.h
SCConfGetChildValueIntWithDefault
int SCConfGetChildValueIntWithDefault(const SCConfNode *base, const SCConfNode *dflt, const char *name, intmax_t *val)
Definition: conf.c:502
RunModeIdsNflogRegister
void RunModeIdsNflogRegister(void)
Definition: runmode-nflog.c:210
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:262
NflogGroupConfig_::qtimeout
uint32_t qtimeout
Definition: source-nflog.h:44
conf.h
RunModeRegisterNewRunMode
void RunModeRegisterNewRunMode(enum SCRunModes runmode, const char *name, const char *description, int(*RunModeFunc)(void), int(*RunModeIsIPSEnabled)(void))
Registers a new runmode.
Definition: runmodes.c:481
runmodes.h
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:232
TimeModeSetLive
void TimeModeSetLive(void)
Definition: util-time.c:99
SCConfNodeLookupKeyValue
SCConfNode * SCConfNodeLookupKeyValue(const SCConfNode *base, const char *key, const char *value)
Lookup for a key value under a specific node.
Definition: conf.c:894
group
uint8_t group
Definition: app-layer-dnp3.h:0
suricata-common.h
NflogGroupConfig_::DerefFunc
void(* DerefFunc)(void *)
Definition: source-nflog.h:51
FatalError
#define FatalError(...)
Definition: util-debug.h:517
NflogGroupConfig_::group
uint16_t group
Definition: source-nflog.h:36
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCConfGetNode
SCConfNode * SCConfGetNode(const char *name)
Get a SCConfNode by name.
Definition: conf.c:183
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
NflogGroupConfig_::qthreshold
uint32_t qthreshold
Definition: source-nflog.h:42
SCFree
#define SCFree(p)
Definition: util-mem.h:61
NflogGroupConfig_::nful_overrun_warned
int nful_overrun_warned
Definition: source-nflog.h:49
RunModeSetLiveCaptureSingle
int RunModeSetLiveCaptureSingle(ConfigIfaceParserFunc ConfigParser, ConfigIfaceThreadsCountFunc ModThreadsCount, const char *recv_mod_name, const char *decode_mod_name, const char *thread_name, const char *live_dev)
Definition: util-runmodes.c:353
runmode-nflog.h
NflogGroupConfig_
Definition: source-nflog.h:34
util-misc.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
SCConfNode_
Definition: conf.h:37
NflogGroupConfig_::nlbufsiz_max
uint32_t nlbufsiz_max
Definition: source-nflog.h:40
NflogGroupConfig_::nlbufsiz
uint32_t nlbufsiz
Definition: source-nflog.h:38