suricata
output-json-dhcp.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-2020 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 Jason Ish <jason.ish@oisf.net>
22  */
23 
24 #include "suricata-common.h"
25 #include "debug.h"
26 #include "detect.h"
27 #include "pkt-var.h"
28 #include "conf.h"
29 
30 #include "threads.h"
31 #include "threadvars.h"
32 #include "tm-threads.h"
33 
34 #include "util-unittest.h"
35 #include "util-buffer.h"
36 #include "util-debug.h"
37 #include "util-byte.h"
38 
39 #include "output.h"
40 #include "output-json.h"
41 
42 #include "app-layer.h"
43 #include "app-layer-parser.h"
44 
45 #include "output-json-dhcp.h"
46 #include "rust.h"
47 
48 
49 typedef struct LogDHCPFileCtx_ {
51  uint32_t flags;
52  void *rs_logger;
55 
56 typedef struct LogDHCPLogThread_ {
61 
62 static int JsonDHCPLogger(ThreadVars *tv, void *thread_data,
63  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
64 {
65  LogDHCPLogThread *thread = thread_data;
66  LogDHCPFileCtx *ctx = thread->dhcplog_ctx;
67 
68  if (!rs_dhcp_logger_do_log(ctx->rs_logger, tx)) {
69  return TM_ECODE_OK;
70  }
71 
72  JsonBuilder *js = CreateEveHeader((Packet *)p, 0, "dhcp", NULL);
73  if (unlikely(js == NULL)) {
74  return TM_ECODE_FAILED;
75  }
76 
77  EveAddCommonOptions(&thread->dhcplog_ctx->cfg, p, f, js);
78 
79  rs_dhcp_logger_log(ctx->rs_logger, tx, js);
80 
81  MemBufferReset(thread->buffer);
82  OutputJsonBuilderBuffer(js, thread->file_ctx, &thread->buffer);
83  jb_free(js);
84 
85  return TM_ECODE_OK;
86 }
87 
88 static void OutputDHCPLogDeInitCtxSub(OutputCtx *output_ctx)
89 {
90  LogDHCPFileCtx *dhcplog_ctx = (LogDHCPFileCtx *)output_ctx->data;
91  rs_dhcp_logger_free(dhcplog_ctx->rs_logger);
92  SCFree(dhcplog_ctx);
93  SCFree(output_ctx);
94 }
95 
96 static OutputInitResult OutputDHCPLogInitSub(ConfNode *conf,
97  OutputCtx *parent_ctx)
98 {
99  OutputInitResult result = { NULL, false };
100  OutputJsonCtx *ajt = parent_ctx->data;
101 
102  LogDHCPFileCtx *dhcplog_ctx = SCCalloc(1, sizeof(*dhcplog_ctx));
103  if (unlikely(dhcplog_ctx == NULL)) {
104  return result;
105  }
106  dhcplog_ctx->file_ctx = ajt->file_ctx;
107  dhcplog_ctx->cfg = ajt->cfg;
108 
109  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
110  if (unlikely(output_ctx == NULL)) {
111  SCFree(dhcplog_ctx);
112  return result;
113  }
114  output_ctx->data = dhcplog_ctx;
115  output_ctx->DeInit = OutputDHCPLogDeInitCtxSub;
116 
117  dhcplog_ctx->rs_logger = rs_dhcp_logger_new(conf);
118 
120 
121  result.ctx = output_ctx;
122  result.ok = true;
123  return result;
124 }
125 
126 
127 static TmEcode JsonDHCPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
128 {
129  LogDHCPLogThread *thread = SCCalloc(1, sizeof(*thread));
130  if (unlikely(thread == NULL)) {
131  return TM_ECODE_FAILED;
132  }
133 
134  if (initdata == NULL) {
135  SCLogDebug("Error getting context for EveLogDHCP. \"initdata\" is NULL.");
136  goto error_exit;
137  }
138 
140  if (unlikely(thread->buffer == NULL)) {
141  goto error_exit;
142  }
143 
144  thread->dhcplog_ctx = ((OutputCtx *)initdata)->data;
145  thread->file_ctx = LogFileEnsureExists(thread->dhcplog_ctx->file_ctx, t->id);
146  if (!thread->file_ctx) {
147  goto error_exit;
148  }
149 
150  *data = (void *)thread;
151  return TM_ECODE_OK;
152 
153 error_exit:
154  if (unlikely(thread->buffer != NULL)) {
155  MemBufferFree(thread->buffer);
156  }
157  SCFree(thread);
158  return TM_ECODE_FAILED;
159 }
160 
161 static TmEcode JsonDHCPLogThreadDeinit(ThreadVars *t, void *data)
162 {
163  LogDHCPLogThread *thread = (LogDHCPLogThread *)data;
164  if (thread == NULL) {
165  return TM_ECODE_OK;
166  }
167  if (thread->buffer != NULL) {
168  MemBufferFree(thread->buffer);
169  }
170  SCFree(thread);
171  return TM_ECODE_OK;
172 }
173 
175 {
176  /* Register as an eve sub-module. */
177  OutputRegisterTxSubModule(LOGGER_JSON_DHCP, "eve-log", "JsonDHCPLog",
178  "eve-log.dhcp", OutputDHCPLogInitSub, ALPROTO_DHCP,
179  JsonDHCPLogger, JsonDHCPLogThreadInit,
180  JsonDHCPLogThreadDeinit, NULL);
181 }
util-byte.h
tm-threads.h
LogDHCPLogThread_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-dhcp.c:59
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:104
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
LogDHCPLogThread_::buffer
MemBuffer * buffer
Definition: output-json-dhcp.c:58
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:63
threads.h
OutputJsonCtx_
Definition: output-json.h:101
Flow_
Flow data structure.
Definition: flow.h:347
OutputJsonCommonSettings_
Definition: output-json.h:91
LogFileCtx_
Definition: util-logopenfile.h:60
LogDHCPLogThread_
Definition: output-json-dhcp.c:56
LogDHCPFileCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json-dhcp.c:53
LogDHCPFileCtx_::flags
uint32_t flags
Definition: output-json-dhcp.c:51
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:970
rust.h
JsonDHCPLogRegister
void JsonDHCPLogRegister(void)
Definition: output-json-dhcp.c:174
LogDHCPLogThread
struct LogDHCPLogThread_ LogDHCPLogThread
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, JsonBuilder *js)
Definition: output-json.c:426
ALPROTO_DHCP
@ ALPROTO_DHCP
Definition: app-layer-protos.h:51
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
OutputCtx_
Definition: tm-modules.h:78
LogDHCPFileCtx_::rs_logger
void * rs_logger
Definition: output-json-dhcp.c:52
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:44
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr)
Definition: output-json.c:838
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:457
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
LogFileEnsureExists
LogFileCtx * LogFileEnsureExists(LogFileCtx *parent_ctx, int thread_id)
LogFileEnsureExists() Ensure a log file context for the thread exists.
Definition: util-logopenfile.c:659
OutputInitResult_::ok
bool ok
Definition: output.h:45
app-layer-parser.h
ThreadVars_::id
int id
Definition: threadvars.h:87
LOGGER_JSON_DHCP
@ LOGGER_JSON_DHCP
Definition: suricata-common.h:459
Packet_
Definition: decode.h:415
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
MemBuffer_
Definition: util-buffer.h:27
LogDHCPLogThread_::dhcplog_ctx
LogDHCPFileCtx * dhcplog_ctx
Definition: output-json-dhcp.c:57
LogDHCPFileCtx_
Definition: output-json-dhcp.c:49
LogDHCPFileCtx
struct LogDHCPFileCtx_ LogDHCPFileCtx
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:43
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
OutputRegisterTxSubModule
void OutputRegisterTxSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:409
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
SCFree
#define SCFree(p)
Definition: util-mem.h:61
LogDHCPFileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-dhcp.c:50
ConfNode_
Definition: conf.h:32
output-json-dhcp.h
util-buffer.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:102
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
app-layer.h