suricata
output-json-dhcp.c
Go to the documentation of this file.
1 /* Copyright (C) 2015 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_ {
58  uint32_t count;
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  EveAddCommonOptions(&thread->dhcplog_ctx->cfg, p, f, js);
82  MemBufferReset(thread->buffer);
83  OutputJsonBuilderBuffer(js, thread->dhcplog_ctx->file_ctx, &thread->buffer);
84  jb_free(js);
85 
86  return TM_ECODE_OK;
87 }
88 
89 static void OutputDHCPLogDeInitCtxSub(OutputCtx *output_ctx)
90 {
91  LogDHCPFileCtx *dhcplog_ctx = (LogDHCPFileCtx *)output_ctx->data;
92  rs_dhcp_logger_free(dhcplog_ctx->rs_logger);
93  SCFree(dhcplog_ctx);
94  SCFree(output_ctx);
95 }
96 
97 static OutputInitResult OutputDHCPLogInitSub(ConfNode *conf,
98  OutputCtx *parent_ctx)
99 {
100  OutputInitResult result = { NULL, false };
101  OutputJsonCtx *ajt = parent_ctx->data;
102 
103  LogDHCPFileCtx *dhcplog_ctx = SCCalloc(1, sizeof(*dhcplog_ctx));
104  if (unlikely(dhcplog_ctx == NULL)) {
105  return result;
106  }
107  dhcplog_ctx->file_ctx = ajt->file_ctx;
108  dhcplog_ctx->cfg = ajt->cfg;
109 
110  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
111  if (unlikely(output_ctx == NULL)) {
112  SCFree(dhcplog_ctx);
113  return result;
114  }
115  output_ctx->data = dhcplog_ctx;
116  output_ctx->DeInit = OutputDHCPLogDeInitCtxSub;
117 
118  dhcplog_ctx->rs_logger = rs_dhcp_logger_new(conf);
119 
121 
122  result.ctx = output_ctx;
123  result.ok = true;
124  return result;
125 }
126 
127 
128 static TmEcode JsonDHCPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
129 {
130  LogDHCPLogThread *thread = SCCalloc(1, sizeof(*thread));
131  if (unlikely(thread == NULL)) {
132  return TM_ECODE_FAILED;
133  }
134 
135  if (initdata == NULL) {
136  SCLogDebug("Error getting context for EveLogDHCP. \"initdata\" is NULL.");
137  SCFree(thread);
138  return TM_ECODE_FAILED;
139  }
140 
142  if (unlikely(thread->buffer == NULL)) {
143  SCFree(thread);
144  return TM_ECODE_FAILED;
145  }
146 
147  thread->dhcplog_ctx = ((OutputCtx *)initdata)->data;
148  *data = (void *)thread;
149 
150  return TM_ECODE_OK;
151 }
152 
153 static TmEcode JsonDHCPLogThreadDeinit(ThreadVars *t, void *data)
154 {
155  LogDHCPLogThread *thread = (LogDHCPLogThread *)data;
156  if (thread == NULL) {
157  return TM_ECODE_OK;
158  }
159  if (thread->buffer != NULL) {
160  MemBufferFree(thread->buffer);
161  }
162  SCFree(thread);
163  return TM_ECODE_OK;
164 }
165 
167 {
168  /* Register as an eve sub-module. */
169  OutputRegisterTxSubModule(LOGGER_JSON_DHCP, "eve-log", "JsonDHCPLog",
170  "eve-log.dhcp", OutputDHCPLogInitSub, ALPROTO_DHCP,
171  JsonDHCPLogger, JsonDHCPLogThreadInit,
172  JsonDHCPLogThreadDeinit, NULL);
173 }
util-byte.h
tm-threads.h
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:111
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:59
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:62
threads.h
OutputJsonCtx_
Definition: output-json.h:108
Flow_
Flow data structure.
Definition: flow.h:343
OutputJsonCommonSettings_
Definition: output-json.h:99
LogFileCtx_
Definition: util-logopenfile.h:52
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:1431
rust.h
JsonDHCPLogRegister
void JsonDHCPLogRegister(void)
Definition: output-json-dhcp.c:166
LogDHCPLogThread
struct LogDHCPLogThread_ LogDHCPLogThread
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, JsonBuilder *js)
Definition: output-json.c:673
ALPROTO_DHCP
@ ALPROTO_DHCP
Definition: app-layer-protos.h:51
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
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:42
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr)
Definition: output-json.c:1286
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:471
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:43
app-layer-parser.h
LOGGER_JSON_DHCP
@ LOGGER_JSON_DHCP
Definition: suricata-common.h:462
Packet_
Definition: decode.h:411
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:77
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:41
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:410
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
LogDHCPLogThread_::count
uint32_t count
Definition: output-json-dhcp.c:58
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:109
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