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
52
57
58static 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;
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
81static 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
89static 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
116static 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
134static 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}
void SCAppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
@ ALPROTO_DHCP
ThreadVars * tv
struct Thresholds ctx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
struct LogDHCPLogThread_ LogDHCPLogThread
struct LogDHCPFileCtx_ LogDHCPFileCtx
void JsonDHCPLogRegister(void)
SCJsonBuilder * CreateEveHeader(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *js, OutputJsonThreadCtx *ctx)
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:406
Flow data structure.
Definition flow.h:356
OutputJsonCtx * eve_ctx
LogDHCPFileCtx * dhcplog_ctx
OutputJsonThreadCtx * thread
void * data
Definition tm-modules.h:91
void(* DeInit)(struct OutputCtx_ *)
Definition tm-modules.h:94
OutputCtx * ctx
Definition output.h:47
struct Flow_ * flow
Definition decode.h:546
Per thread variable structure.
Definition threadvars.h:58
@ LOGGER_JSON_TX
@ TM_ECODE_FAILED
@ TM_ECODE_OK
#define SCFree(p)
Definition util-mem.h:61
#define SCCalloc(nm, sz)
Definition util-mem.h:53
#define unlikely(expr)