suricata
output-json-ike.c
Go to the documentation of this file.
1/* Copyright (C) 2018-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 Pierre Chifflier <chifflier@wzdftpd.net>
22 * \author Frank Honza <frank.honza@dcso.de>
23 *
24 * Implement JSON/eve logging app-layer IKE.
25 */
26
27#include "suricata-common.h"
28#include "detect.h"
29#include "pkt-var.h"
30#include "conf.h"
31
32#include "threads.h"
33#include "threadvars.h"
34#include "tm-threads.h"
35
36#include "util-unittest.h"
37#include "util-buffer.h"
38#include "util-debug.h"
39#include "util-byte.h"
40
41#include "output.h"
42#include "output-json.h"
43
44#include "app-layer.h"
45#include "app-layer-parser.h"
46
47#include "app-layer-ike.h"
48#include "output-json-ike.h"
49
50#include "rust.h"
51
52#define LOG_IKE_DEFAULT 0
53#define LOG_IKE_EXTENDED (1 << 0)
54
59
64
65bool EveIKEAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
66{
67 IKEState *state = FlowGetAppState(f);
68 if (state) {
69 IKETransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_IKE, state, tx_id);
70 if (tx) {
71 return SCIkeLoggerLog(state, tx, LOG_IKE_EXTENDED, js);
72 }
73 }
74
75 return false;
76}
77
78static int JsonIKELogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state,
79 void *tx, uint64_t tx_id)
80{
81 LogIKELogThread *thread = thread_data;
82 SCJsonBuilder *jb =
83 CreateEveHeader((Packet *)p, LOG_DIR_PACKET, "ike", NULL, thread->ikelog_ctx->eve_ctx);
84 if (unlikely(jb == NULL)) {
85 return TM_ECODE_FAILED;
86 }
87
88 LogIKEFileCtx *ike_ctx = thread->ikelog_ctx;
89 if (!SCIkeLoggerLog(state, tx, ike_ctx->flags, jb)) {
90 goto error;
91 }
92
93 OutputJsonBuilderBuffer(tv, p, p->flow, jb, thread->ctx);
94
95 SCJbFree(jb);
96 return TM_ECODE_OK;
97
98error:
99 SCJbFree(jb);
100 return TM_ECODE_FAILED;
101}
102
103static void OutputIKELogDeInitCtxSub(OutputCtx *output_ctx)
104{
105 LogIKEFileCtx *ikelog_ctx = (LogIKEFileCtx *)output_ctx->data;
106 SCFree(ikelog_ctx);
107 SCFree(output_ctx);
108}
109
110static OutputInitResult OutputIKELogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
111{
112 OutputInitResult result = { NULL, false };
113 OutputJsonCtx *ajt = parent_ctx->data;
114
115 LogIKEFileCtx *ikelog_ctx = SCCalloc(1, sizeof(*ikelog_ctx));
116 if (unlikely(ikelog_ctx == NULL)) {
117 return result;
118 }
119 ikelog_ctx->eve_ctx = ajt;
120
121 OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
122 if (unlikely(output_ctx == NULL)) {
123 SCFree(ikelog_ctx);
124 return result;
125 }
126
127 ikelog_ctx->flags = LOG_IKE_DEFAULT;
128 const char *extended = SCConfNodeLookupChildValue(conf, "extended");
129 if (extended) {
130 if (SCConfValIsTrue(extended)) {
131 ikelog_ctx->flags = LOG_IKE_EXTENDED;
132 }
133 }
134
135 output_ctx->data = ikelog_ctx;
136 output_ctx->DeInit = OutputIKELogDeInitCtxSub;
137
139
140 result.ctx = output_ctx;
141 result.ok = true;
142 return result;
143}
144
145static TmEcode JsonIKELogThreadInit(ThreadVars *t, const void *initdata, void **data)
146{
147 LogIKELogThread *thread = SCCalloc(1, sizeof(*thread));
148 if (unlikely(thread == NULL)) {
149 return TM_ECODE_FAILED;
150 }
151
152 if (initdata == NULL) {
153 SCLogDebug("Error getting context for EveLogIKE. \"initdata\" is NULL.");
154 goto error_exit;
155 }
156
157 thread->ikelog_ctx = ((OutputCtx *)initdata)->data;
158 thread->ctx = CreateEveThreadCtx(t, thread->ikelog_ctx->eve_ctx);
159 if (!thread->ctx) {
160 goto error_exit;
161 }
162
163 *data = (void *)thread;
164 return TM_ECODE_OK;
165
166error_exit:
167 SCFree(thread);
168 return TM_ECODE_FAILED;
169}
170
171static TmEcode JsonIKELogThreadDeinit(ThreadVars *t, void *data)
172{
173 LogIKELogThread *thread = (LogIKELogThread *)data;
174 if (thread == NULL) {
175 return TM_ECODE_OK;
176 }
177 FreeEveThreadCtx(thread->ctx);
178 SCFree(thread);
179 return TM_ECODE_OK;
180}
181
183{
184 /* Register as an eve sub-module. */
185 OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonIKELog", "eve-log.ike",
186 OutputIKELogInitSub, ALPROTO_IKE, JsonIKELogger, JsonIKELogThreadInit,
187 JsonIKELogThreadDeinit);
188}
struct IKETransaction_ IKETransaction
struct IKEState_ IKEState
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
void SCAppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
@ ALPROTO_IKE
int SCConfValIsTrue(const char *val)
Check if a value is true.
Definition conf.c:551
const char * SCConfNodeLookupChildValue(const SCConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition conf.c:824
ThreadVars * tv
@ LOG_DIR_PACKET
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
#define LOG_IKE_DEFAULT
struct LogIKELogThread_ LogIKELogThread
#define LOG_IKE_EXTENDED
void JsonIKELogRegister(void)
struct LogIKEFileCtx_ LogIKEFileCtx
bool EveIKEAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
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
uint8_t proto
Definition flow.h:378
OutputJsonCtx * eve_ctx
OutputJsonThreadCtx * ctx
LogIKEFileCtx * ikelog_ctx
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 SCLogDebug(...)
Definition util-debug.h:275
#define SCFree(p)
Definition util-mem.h:61
#define SCCalloc(nm, sz)
Definition util-mem.h:53
#define unlikely(expr)