suricata
output-json-dhcp.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-2021 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 "detect.h"
26 #include "pkt-var.h"
27 #include "conf.h"
28 
29 #include "threads.h"
30 #include "threadvars.h"
31 #include "tm-threads.h"
32 
33 #include "util-unittest.h"
34 #include "util-buffer.h"
35 #include "util-debug.h"
36 #include "util-byte.h"
37 
38 #include "output.h"
39 #include "output-json.h"
40 
41 #include "app-layer.h"
42 #include "app-layer-parser.h"
43 
44 #include "output-json-dhcp.h"
45 #include "rust.h"
46 
47 
48 typedef struct LogDHCPFileCtx_ {
49  void *rs_logger;
52 
53 typedef struct LogDHCPLogThread_ {
57 
58 static int JsonDHCPLogger(ThreadVars *tv, void *thread_data,
59  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
60 {
61  LogDHCPLogThread *thread = thread_data;
62  LogDHCPFileCtx *ctx = thread->dhcplog_ctx;
63 
64  if (!SCDhcpLoggerDoLog(ctx->rs_logger, tx)) {
65  return TM_ECODE_OK;
66  }
67 
68  SCJsonBuilder *js = CreateEveHeader((Packet *)p, 0, "dhcp", NULL, ctx->eve_ctx);
69  if (unlikely(js == NULL)) {
70  return TM_ECODE_FAILED;
71  }
72 
73  SCDhcpLoggerLog(ctx->rs_logger, tx, js);
74 
75  OutputJsonBuilderBuffer(tv, p, p->flow, js, thread->thread);
76  SCJbFree(js);
77 
78  return TM_ECODE_OK;
79 }
80 
81 static void OutputDHCPLogDeInitCtxSub(OutputCtx *output_ctx)
82 {
83  LogDHCPFileCtx *dhcplog_ctx = (LogDHCPFileCtx *)output_ctx->data;
84  SCDhcpLoggerFree(dhcplog_ctx->rs_logger);
85  SCFree(dhcplog_ctx);
86  SCFree(output_ctx);
87 }
88 
89 static OutputInitResult OutputDHCPLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
90 {
91  OutputInitResult result = { NULL, false };
92 
93  LogDHCPFileCtx *dhcplog_ctx = SCCalloc(1, sizeof(*dhcplog_ctx));
94  if (unlikely(dhcplog_ctx == NULL)) {
95  return result;
96  }
97  dhcplog_ctx->eve_ctx = parent_ctx->data;
98 
99  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
100  if (unlikely(output_ctx == NULL)) {
101  SCFree(dhcplog_ctx);
102  return result;
103  }
104  output_ctx->data = dhcplog_ctx;
105  output_ctx->DeInit = OutputDHCPLogDeInitCtxSub;
106 
107  dhcplog_ctx->rs_logger = SCDhcpLoggerNew(conf);
108 
110 
111  result.ctx = output_ctx;
112  result.ok = true;
113  return result;
114 }
115 
116 static TmEcode JsonDHCPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
117 {
118  LogDHCPLogThread *thread = SCCalloc(1, sizeof(*thread));
119  if (unlikely(thread == NULL)) {
120  return TM_ECODE_FAILED;
121  }
122  LogDHCPFileCtx *ctx = ((OutputCtx *)initdata)->data;
123  thread->dhcplog_ctx = ctx;
124  thread->thread = CreateEveThreadCtx(t, ctx->eve_ctx);
125  if (thread->thread == NULL) {
126  SCFree(thread);
127  return TM_ECODE_FAILED;
128  }
129 
130  *data = (void *)thread;
131  return TM_ECODE_OK;
132 }
133 
134 static TmEcode JsonDHCPLogThreadDeinit(ThreadVars *t, void *data)
135 {
136  LogDHCPLogThread *thread = (LogDHCPLogThread *)data;
137  if (thread == NULL) {
138  return TM_ECODE_OK;
139  }
140  FreeEveThreadCtx(thread->thread);
141  SCFree(thread);
142  return TM_ECODE_OK;
143 }
144 
146 {
147  /* Register as an eve sub-module. */
148  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonDHCPLog", "eve-log.dhcp",
149  OutputDHCPLogInitSub, ALPROTO_DHCP, JsonDHCPLogger, JsonDHCPLogThreadInit,
150  JsonDHCPLogThreadDeinit);
151 }
util-byte.h
tm-threads.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
CreateEveHeader
SCJsonBuilder * CreateEveHeader(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:834
threads.h
OutputJsonCtx_
Definition: output-json.h:75
Flow_
Flow data structure.
Definition: flow.h:356
ctx
struct Thresholds ctx
LogDHCPLogThread_
Definition: output-json-dhcp.c:53
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
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)
Definition: output.c:383
rust.h
JsonDHCPLogRegister
void JsonDHCPLogRegister(void)
Definition: output-json-dhcp.c:145
LogDHCPLogThread
struct LogDHCPLogThread_ LogDHCPLogThread
OutputJsonBuilderBuffer
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:992
ALPROTO_DHCP
@ ALPROTO_DHCP
Definition: app-layer-protos.h:58
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:87
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
OutputCtx_
Definition: tm-modules.h:84
OutputJsonThreadCtx_
Definition: output-json.h:83
LogDHCPFileCtx_::rs_logger
void * rs_logger
Definition: output-json-dhcp.c:49
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:490
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:48
app-layer-parser.h
Packet_
Definition: decode.h:484
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:80
LogDHCPLogThread_::dhcplog_ctx
LogDHCPFileCtx * dhcplog_ctx
Definition: output-json-dhcp.c:54
LogDHCPFileCtx_
Definition: output-json-dhcp.c:48
LogDHCPFileCtx
struct LogDHCPFileCtx_ LogDHCPFileCtx
OutputInitResult_
Definition: output.h:46
Packet_::flow
struct Flow_ * flow
Definition: decode.h:529
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:90
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:477
SCFree
#define SCFree(p)
Definition: util-mem.h:61
output-json-dhcp.h
util-buffer.h
LogDHCPLogThread_::thread
OutputJsonThreadCtx * thread
Definition: output-json-dhcp.c:55
LogDHCPFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-dhcp.c:50
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCConfNode_
Definition: conf.h:32
output.h
app-layer.h