suricata
output-eve.h
Go to the documentation of this file.
1 /* Copyright (C) 2024 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  * \brief EVE logging subsystem
22  *
23  * This file will attempt to the main module for EVE logging
24  * sub-system. Currently most of the API resides in output-json.[ch],
25  * but due to some circular dependencies between EVE, and LogFileCtx,
26  * it made it hard to add EVE filetype modules there until some
27  * include issues are figured out.
28  */
29 
30 #ifndef SURICATA_OUTPUT_EVE_H
31 #define SURICATA_OUTPUT_EVE_H
32 
33 #ifndef SURICATA_BINDGEN_H
34 #include "suricata-common.h"
35 #include "rust.h"
36 #include "conf.h"
37 #else
38 typedef struct ThreadVars_ ThreadVars;
39 typedef struct Flow_ Flow;
40 typedef struct SCJsonBuilder SCJsonBuilder;
41 typedef struct Packet_ Packet;
42 #endif
43 
44 #include "app-layer-protos.h"
45 
46 typedef uint32_t ThreadId;
47 
54 
55 typedef bool (*EveJsonSimpleTxLogFunc)(const void *, void *);
56 
57 typedef struct EveJsonSimpleAppLayerLogger {
59  const char *name;
61 
63 
64 typedef struct EveJsonTxLoggerRegistrationData {
65  const char *confname;
66  const char *logname;
68  uint8_t dir;
71 
73 
74 /** \brief Function type for EVE file-type initialization. */
75 typedef int (*SCEveFileTypeInitFunc)(const SCConfNode *conf, const bool threaded, void **init_data);
76 
77 /** \brief Function type for EVE file-type thread initialization. */
79  const void *init_data, const ThreadId thread_id, void **thread_data);
80 
81 /** \brief Function type for EVE file-type writes. */
82 typedef int (*SCEveFileTypeWriteFunc)(
83  const char *buffer, const int buffer_len, const void *init_data, void *thread_data);
84 
85 /** \brief Function type for EVE file-type thread deinitialization. */
86 typedef void (*SCEveFileTypeThreadDeinitFunc)(const void *init_data, void *thread_data);
87 
88 /** \brief Function type for EVE file-type deinitialization. */
89 typedef void (*SCEveFileTypeDeinitFunc)(void *init_data);
90 
91 /** \brief Structure used to define an EVE output file type.
92  *
93  * EVE filetypes implement an object with a file-like interface and
94  * are used to output EVE log records to files, syslog, or
95  * database. They can be built-in such as the syslog (see
96  * SyslogInitialize()) and nullsink (see NullLogInitialize()) outputs,
97  * registered by a library user or dynamically loaded as a plugin.
98  *
99  * The life cycle of an EVE filetype is:
100  * - Init: called once for each EVE instance using this filetype
101  * - ThreadInit: called once for each output thread
102  * - Write: called for each log record
103  * - ThreadDeinit: called once for each output thread on exit
104  * - Deinit: called once for each EVE instance using this filetype on exit
105  *
106  * Examples:
107  * - built-in syslog: \ref src/output-eve-syslog.c
108  * - built-in nullsink: \ref src/output-eve-null.c
109  * - example plugin: \ref examples/plugins/c-json-filetype/filetype.c
110  *
111  * ### Multi-Threaded Note:
112  *
113  * The EVE logging system can be configured by the Suricata user to
114  * run in threaded or non-threaded modes. In the default non-threaded
115  * mode, ThreadInit will only be called once and the filetype does not
116  * need to be concerned with threads.
117  *
118  * However, in **threaded** mode, ThreadInit will be called multiple
119  * times and the filetype needs to be thread aware and thread-safe. If
120  * utilizing a unique resource such as a file for each thread then you
121  * may be naturally thread safe. However, if sharing a single file
122  * handle across all threads then your filetype will have to take care
123  * of locking, etc.
124  */
125 typedef struct SCEveFileType_ {
126  /**
127  * \brief The name of the output, used in the configuration.
128  *
129  * This name is used by the configuration file to specify the EVE
130  * filetype used.
131  *
132  * For example:
133  *
134  * \code{.yaml}
135  * outputs:
136  * - eve-log:
137  * filetype: my-output-name
138  * \endcode
139  */
140  const char *name;
141 
142  /**
143  * \brief Function to initialize this filetype.
144  *
145  * \param conf The ConfNode of the `eve-log` configuration
146  * section this filetype is being initialized for
147  *
148  * \param threaded Flag to specify if the EVE sub-systems is in
149  * threaded mode or not
150  *
151  * \param init_data An output pointer for filetype specific data
152  *
153  * \retval 0 on success, -1 on failure
154  */
156 
157  /**
158  * \brief Initialize thread specific data.
159  *
160  * Initialize any thread specific data. For example, if
161  * implementing a file output you might open the files here, so
162  * you have one output file per thread.
163  *
164  * \param init_data Data setup during Init
165  *
166  * \param thread_id A unique ID to differentiate this thread from
167  * others. If EVE is not in threaded mode this will be called
168  * once with a ThreadId of 0. In threaded mode the ThreadId of
169  * 0 correlates to the main Suricata thread.
170  *
171  * \param thread_data Output pointer for any data required by this
172  * thread.
173  *
174  * \retval 0 on success, -1 on failure
175  */
177 
178  /**
179  * \brief Called for each EVE log record.
180  *
181  * The Write function is called for each log EVE log record. The
182  * provided buffer contains a fully formatted EVE record in JSON
183  * format.
184  *
185  * \param buffer The fully formatted JSON EVE log record
186  *
187  * \param buffer_len The length of the buffer
188  *
189  * \param init_data The data setup in the call to Init
190  *
191  * \param thread_data The data setup in the call to ThreadInit
192  *
193  * \retval 0 on success, -1 on failure
194  */
196 
197  /**
198  * \brief Called to deinitialize each thread.
199  *
200  * This function will be called for each thread. It is where any
201  * resources allocated in ThreadInit should be released.
202  *
203  * \param init_data The data setup in Init
204  *
205  * \param thread_data The data setup in ThreadInit
206  */
208 
209  /**
210  * \brief Final call to deinitialize this filetype.
211  *
212  * Called, usually on exit to deinitialize and free any resources
213  * allocated during Init.
214  *
215  * \param init_data Data setup in the call to Init.
216  */
218 
219  /* Internal list management. */
222 
224 
225 SCEveFileType *SCEveFindFileType(const char *name);
226 
227 /** \brief Function type for EVE callbacks.
228  *
229  * The function type for callbacks registered with
230  * SCEveRegisterCallback. This function will be called with the
231  * SCJsonBuilder just prior to the top-level object being closed. New
232  * fields may be added, however, there is no way to alter existing
233  * objects already added to the SCJsonBuilder.
234  *
235  * \param tv The ThreadVars for the thread performing the logging.
236  * \param p Packet if available.
237  * \param f Flow if available.
238  * \param user User data provided during callback registration.
239  */
240 typedef void (*SCEveUserCallbackFn)(
241  ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *jb, void *user);
242 
243 /** \brief Register a callback for adding extra information to EVE logs.
244  *
245  * Allow users to register a callback for each EVE log. The callback
246  * is called just before the root object on the SCJsonBuilder is to be
247  * closed.
248  *
249  * New objects and fields can be appended, but existing entries cannot be modified.
250  *
251  * Packet and Flow will be provided if available, but will otherwise be
252  * NULL.
253  *
254  * Limitations: At this time the callbacks will only be called for EVE
255  * loggers that use SCJsonBuilder, notably this means it won't be called
256  * for stats records at this time.
257  *
258  * \returns true if callback is registered, false is not due to memory
259  * allocation error.
260  */
262 
263 /** \internal
264  *
265  * Run EVE callbacks.
266  */
267 void SCEveRunCallbacks(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *jb);
268 
269 #endif
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-eve.h:50
SCEveRunCallbacks
void SCEveRunCallbacks(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *jb)
Definition: output-eve.c:53
SCOutputJsonLogDirection
SCOutputJsonLogDirection
Definition: output-eve-bindgen.h:33
SCEveFileTypeDeinitFunc
void(* SCEveFileTypeDeinitFunc)(void *init_data)
Function type for EVE file-type deinitialization.
Definition: output-eve.h:89
SCEveFileType_::Deinit
SCEveFileTypeDeinitFunc Deinit
Final call to deinitialize this filetype.
Definition: output-eve-bindgen.h:202
SCEveRegisterCallback
bool SCEveRegisterCallback(SCEveUserCallbackFn fn, void *user)
Register a callback for adding extra information to EVE logs.
SCEveFindFileType
SCEveFileType * SCEveFindFileType(const char *name)
Definition: output-eve.c:82
EveJsonTxLoggerRegistrationData::LogTx
EveJsonSimpleTxLogFunc LogTx
Definition: output-eve-bindgen.h:54
EveJsonSimpleTxLogFunc
bool(* EveJsonSimpleTxLogFunc)(const void *, void *)
Definition: output-eve.h:55
SCEveFileType_::Write
SCEveFileTypeWriteFunc Write
Called for each EVE log record.
Definition: output-eve-bindgen.h:180
name
const char * name
Definition: detect-engine-proto.c:48
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:86
Flow_
Flow data structure.
Definition: flow.h:347
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-eve.h:49
SCEveFileTypeThreadDeinitFunc
void(* SCEveFileTypeThreadDeinitFunc)(const void *init_data, void *thread_data)
Function type for EVE file-type thread deinitialization.
Definition: output-eve.h:86
rust.h
EveJsonTxLoggerRegistrationData::dir
uint8_t dir
Definition: output-eve-bindgen.h:53
ThreadId
uint32_t ThreadId
Definition: output-eve.h:46
SCEveFileTypeThreadInitFunc
int(* SCEveFileTypeThreadInitFunc)(const void *init_data, const ThreadId thread_id, void **thread_data)
Function type for EVE file-type thread initialization.
Definition: output-eve.h:78
SCEveFileType_::ThreadDeinit
SCEveFileTypeThreadDeinitFunc ThreadDeinit
Called to deinitialize each thread.
Definition: output-eve-bindgen.h:192
EveJsonTxLoggerRegistrationData
struct EveJsonTxLoggerRegistrationData EveJsonTxLoggerRegistrationData
EveJsonSimpleAppLayerLogger
struct EveJsonSimpleAppLayerLogger EveJsonSimpleAppLayerLogger
SCEveFileType_::ThreadInit
SCEveFileTypeThreadInitFunc ThreadInit
Initialize thread specific data.
Definition: output-eve-bindgen.h:161
EveJsonTxLoggerRegistrationData::alproto
AppProto alproto
Definition: output-eve-bindgen.h:52
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
SCEveFileType_::Init
SCEveFileTypeInitFunc Init
Function to initialize this filetype.
Definition: output-eve-bindgen.h:140
SCRegisterEveFileType
bool SCRegisterEveFileType(SCEveFileType *)
Register an Eve file type.
Definition: output-eve.c:100
SCEveFileTypeInitFunc
int(* SCEveFileTypeInitFunc)(const SCConfNode *conf, const bool threaded, void **init_data)
Function type for EVE file-type initialization.
Definition: output-eve.h:75
Packet_
Definition: decode.h:501
SCEveFileTypeWriteFunc
int(* SCEveFileTypeWriteFunc)(const char *buffer, const int buffer_len, const void *init_data, void *thread_data)
Function type for EVE file-type writes.
Definition: output-eve.h:82
EveJsonTxLoggerRegistrationData
Definition: output-eve-bindgen.h:49
conf.h
SCEveFileType_::name
const char * name
The name of the output, used in the configuration.
Definition: output-eve-bindgen.h:125
EveJsonSimpleAppLayerLogger
Definition: output-eve-bindgen.h:42
LOG_DIR_FLOW_TOSERVER
@ LOG_DIR_FLOW_TOSERVER
Definition: output-eve.h:52
EveJsonTxLoggerRegistrationData::logname
const char * logname
Definition: output-eve-bindgen.h:51
SCOutputJsonLogDirection
SCOutputJsonLogDirection
Definition: output-eve.h:48
suricata-common.h
EveJsonSimpleAppLayerLogger::LogTx
EveJsonSimpleTxLogFunc LogTx
Definition: output-eve-bindgen.h:43
EveJsonSimpleAppLayerLogger::name
const char * name
Definition: output-eve-bindgen.h:44
ThreadId
uint32_t ThreadId
Definition: output-eve-bindgen.h:31
SCEveFileType_::TAILQ_ENTRY
TAILQ_ENTRY(SCEveFileType_) entries
SCOutputEvePreRegisterLogger
int SCOutputEvePreRegisterLogger(EveJsonTxLoggerRegistrationData reg_data)
Definition: output.c:1060
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:33
SCEveFileType
struct SCEveFileType_ SCEveFileType
Structure used to define an EVE output file type.
SCEveJsonSimpleGetLogger
EveJsonSimpleAppLayerLogger * SCEveJsonSimpleGetLogger(AppProto alproto)
Definition: output.c:930
EveJsonTxLoggerRegistrationData::confname
const char * confname
Definition: output-eve-bindgen.h:50
app-layer-protos.h
SCEveUserCallbackFn
void(* SCEveUserCallbackFn)(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *jb, void *user)
Function type for EVE callbacks.
Definition: output-eve.h:240
SCConfNode_
Definition: conf.h:37
LOG_DIR_FLOW_TOCLIENT
@ LOG_DIR_FLOW_TOCLIENT
Definition: output-eve.h:51
SCEveFileType_
Structure used to define an EVE output file type.
Definition: output-eve-bindgen.h:110