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 "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_ {
50  void *rs_logger;
53 
54 typedef struct LogDHCPLogThread_ {
58 
59 static int JsonDHCPLogger(ThreadVars *tv, void *thread_data,
60  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
61 {
62  LogDHCPLogThread *thread = thread_data;
63  LogDHCPFileCtx *ctx = thread->dhcplog_ctx;
64 
65  if (!rs_dhcp_logger_do_log(ctx->rs_logger, tx)) {
66  return TM_ECODE_OK;
67  }
68 
69  JsonBuilder *js = CreateEveHeader((Packet *)p, 0, "dhcp", NULL, ctx->eve_ctx);
70  if (unlikely(js == NULL)) {
71  return TM_ECODE_FAILED;
72  }
73 
74  rs_dhcp_logger_log(ctx->rs_logger, tx, js);
75 
76  OutputJsonBuilderBuffer(js, thread->thread);
77  jb_free(js);
78 
79  return TM_ECODE_OK;
80 }
81 
82 static void OutputDHCPLogDeInitCtxSub(OutputCtx *output_ctx)
83 {
84  LogDHCPFileCtx *dhcplog_ctx = (LogDHCPFileCtx *)output_ctx->data;
85  rs_dhcp_logger_free(dhcplog_ctx->rs_logger);
86  SCFree(dhcplog_ctx);
87  SCFree(output_ctx);
88 }
89 
90 static OutputInitResult OutputDHCPLogInitSub(ConfNode *conf,
91  OutputCtx *parent_ctx)
92 {
93  OutputInitResult result = { NULL, false };
94 
95  LogDHCPFileCtx *dhcplog_ctx = SCCalloc(1, sizeof(*dhcplog_ctx));
96  if (unlikely(dhcplog_ctx == NULL)) {
97  return result;
98  }
99  dhcplog_ctx->eve_ctx = parent_ctx->data;
100 
101  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
102  if (unlikely(output_ctx == NULL)) {
103  SCFree(dhcplog_ctx);
104  return result;
105  }
106  output_ctx->data = dhcplog_ctx;
107  output_ctx->DeInit = OutputDHCPLogDeInitCtxSub;
108 
109  dhcplog_ctx->rs_logger = rs_dhcp_logger_new(conf);
110 
112 
113  result.ctx = output_ctx;
114  result.ok = true;
115  return result;
116 }
117 
118 static TmEcode JsonDHCPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
119 {
120  LogDHCPLogThread *thread = SCCalloc(1, sizeof(*thread));
121  if (unlikely(thread == NULL)) {
122  return TM_ECODE_FAILED;
123  }
124  LogDHCPFileCtx *ctx = ((OutputCtx *)initdata)->data;
125  thread->dhcplog_ctx = ctx;
126  thread->thread = CreateEveThreadCtx(t, ctx->eve_ctx);
127  if (thread->thread == NULL) {
128  SCFree(thread);
129  return TM_ECODE_FAILED;
130  }
131 
132  *data = (void *)thread;
133  return TM_ECODE_OK;
134 }
135 
136 static TmEcode JsonDHCPLogThreadDeinit(ThreadVars *t, void *data)
137 {
138  LogDHCPLogThread *thread = (LogDHCPLogThread *)data;
139  if (thread == NULL) {
140  return TM_ECODE_OK;
141  }
142  FreeEveThreadCtx(thread->thread);
143  SCFree(thread);
144  return TM_ECODE_OK;
145 }
146 
148 {
149  /* Register as an eve sub-module. */
150  OutputRegisterTxSubModule(LOGGER_JSON_DHCP, "eve-log", "JsonDHCPLog",
151  "eve-log.dhcp", OutputDHCPLogInitSub, ALPROTO_DHCP,
152  JsonDHCPLogger, JsonDHCPLogThreadInit,
153  JsonDHCPLogThreadDeinit, NULL);
154 }
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:73
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:353
LogDHCPLogThread_
Definition: output-json-dhcp.c:54
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:975
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:44
rust.h
JsonDHCPLogRegister
void JsonDHCPLogRegister(void)
Definition: output-json-dhcp.c:147
LogDHCPLogThread
struct LogDHCPLogThread_ LogDHCPLogThread
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
OutputJsonThreadCtx_
Definition: output-json.h:89
LogDHCPFileCtx_::rs_logger
void * rs_logger
Definition: output-json-dhcp.c:50
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:44
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:455
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:839
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:45
app-layer-parser.h
LOGGER_JSON_DHCP
@ LOGGER_JSON_DHCP
Definition: suricata-common.h:457
Packet_
Definition: decode.h:414
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
LogDHCPLogThread_::dhcplog_ctx
LogDHCPFileCtx * dhcplog_ctx
Definition: output-json-dhcp.c:55
LogDHCPFileCtx_
Definition: output-json-dhcp.c:49
LogDHCPFileCtx
struct LogDHCPFileCtx_ LogDHCPFileCtx
OutputInitResult_
Definition: output.h:43
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
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
ConfNode_
Definition: conf.h:32
output-json-dhcp.h
util-buffer.h
LogDHCPLogThread_::thread
OutputJsonThreadCtx * thread
Definition: output-json-dhcp.c:56
LogDHCPFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-dhcp.c:51
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
debug.h
output.h
app-layer.h