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 (!rs_dhcp_logger_do_log(ctx->rs_logger, tx)) {
65  return TM_ECODE_OK;
66  }
67 
68  JsonBuilder *js = CreateEveHeader((Packet *)p, 0, "dhcp", NULL, ctx->eve_ctx);
69  if (unlikely(js == NULL)) {
70  return TM_ECODE_FAILED;
71  }
72 
73  rs_dhcp_logger_log(ctx->rs_logger, tx, js);
74 
75  OutputJsonBuilderBuffer(tv, p, p->flow, js, thread->thread);
76  jb_free(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  rs_dhcp_logger_free(dhcplog_ctx->rs_logger);
85  SCFree(dhcplog_ctx);
86  SCFree(output_ctx);
87 }
88 
89 static OutputInitResult OutputDHCPLogInitSub(ConfNode *conf,
90  OutputCtx *parent_ctx)
91 {
92  OutputInitResult result = { NULL, false };
93 
94  LogDHCPFileCtx *dhcplog_ctx = SCCalloc(1, sizeof(*dhcplog_ctx));
95  if (unlikely(dhcplog_ctx == NULL)) {
96  return result;
97  }
98  dhcplog_ctx->eve_ctx = parent_ctx->data;
99 
100  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
101  if (unlikely(output_ctx == NULL)) {
102  SCFree(dhcplog_ctx);
103  return result;
104  }
105  output_ctx->data = dhcplog_ctx;
106  output_ctx->DeInit = OutputDHCPLogDeInitCtxSub;
107 
108  dhcplog_ctx->rs_logger = rs_dhcp_logger_new(conf);
109 
111 
112  result.ctx = output_ctx;
113  result.ok = true;
114  return result;
115 }
116 
117 static TmEcode JsonDHCPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
118 {
119  LogDHCPLogThread *thread = SCCalloc(1, sizeof(*thread));
120  if (unlikely(thread == NULL)) {
121  return TM_ECODE_FAILED;
122  }
123  LogDHCPFileCtx *ctx = ((OutputCtx *)initdata)->data;
124  thread->dhcplog_ctx = ctx;
125  thread->thread = CreateEveThreadCtx(t, ctx->eve_ctx);
126  if (thread->thread == NULL) {
127  SCFree(thread);
128  return TM_ECODE_FAILED;
129  }
130 
131  *data = (void *)thread;
132  return TM_ECODE_OK;
133 }
134 
135 static TmEcode JsonDHCPLogThreadDeinit(ThreadVars *t, void *data)
136 {
137  LogDHCPLogThread *thread = (LogDHCPLogThread *)data;
138  if (thread == NULL) {
139  return TM_ECODE_OK;
140  }
141  FreeEveThreadCtx(thread->thread);
142  SCFree(thread);
143  return TM_ECODE_OK;
144 }
145 
147 {
148  /* Register as an eve sub-module. */
149  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonDHCPLog", "eve-log.dhcp",
150  OutputDHCPLogInitSub, ALPROTO_DHCP, JsonDHCPLogger, JsonDHCPLogThreadInit,
151  JsonDHCPLogThreadDeinit);
152 }
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
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:356
ctx
struct Thresholds ctx
OutputJsonBuilderBuffer
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:958
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:377
rust.h
JsonDHCPLogRegister
void JsonDHCPLogRegister(void)
Definition: output-json-dhcp.c:146
LogDHCPLogThread
struct LogDHCPLogThread_ LogDHCPLogThread
ALPROTO_DHCP
@ ALPROTO_DHCP
Definition: app-layer-protos.h:52
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:87
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
OutputCtx_
Definition: tm-modules.h:84
OutputJsonThreadCtx_
Definition: output-json.h:89
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:457
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:806
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:476
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
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:515
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:468
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
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
output.h
app-layer.h