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-buffer.h"
37 #include "util-debug.h"
38 #include "util-byte.h"
39 
40 #include "output.h"
41 #include "output-json.h"
42 
43 #include "app-layer.h"
44 #include "app-layer-parser.h"
45 
46 #include "app-layer-ike.h"
47 #include "output-json-ike.h"
48 
49 #include "rust.h"
50 
51 #define LOG_IKE_DEFAULT 0
52 #define LOG_IKE_EXTENDED (1 << 0)
53 
54 typedef struct LogIKEFileCtx_ {
55  uint32_t flags;
58 
59 typedef struct LogIKELogThread_ {
63 
64 bool EveIKEAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
65 {
66  IKEState *state = FlowGetAppState(f);
67  if (state) {
68  void *tx = AppLayerParserGetTx(f->proto, ALPROTO_IKE, state, tx_id);
69  if (tx) {
70  return SCIkeLoggerLog(state, tx, LOG_IKE_EXTENDED, js);
71  }
72  }
73 
74  return false;
75 }
76 
77 static int JsonIKELogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state,
78  void *tx, uint64_t tx_id)
79 {
80  LogIKELogThread *thread = thread_data;
81  SCJsonBuilder *jb =
82  CreateEveHeader((Packet *)p, LOG_DIR_PACKET, "ike", NULL, thread->ikelog_ctx->eve_ctx);
83  if (unlikely(jb == NULL)) {
84  return TM_ECODE_FAILED;
85  }
86 
87  LogIKEFileCtx *ike_ctx = thread->ikelog_ctx;
88  if (!SCIkeLoggerLog(state, tx, ike_ctx->flags, jb)) {
89  goto error;
90  }
91 
92  OutputJsonBuilderBuffer(tv, p, p->flow, jb, thread->ctx);
93 
94  SCJbFree(jb);
95  return TM_ECODE_OK;
96 
97 error:
98  SCJbFree(jb);
99  return TM_ECODE_FAILED;
100 }
101 
102 static void OutputIKELogDeInitCtxSub(OutputCtx *output_ctx)
103 {
104  LogIKEFileCtx *ikelog_ctx = (LogIKEFileCtx *)output_ctx->data;
105  SCFree(ikelog_ctx);
106  SCFree(output_ctx);
107 }
108 
109 static OutputInitResult OutputIKELogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
110 {
111  OutputInitResult result = { NULL, false };
112  OutputJsonCtx *ajt = parent_ctx->data;
113 
114  LogIKEFileCtx *ikelog_ctx = SCCalloc(1, sizeof(*ikelog_ctx));
115  if (unlikely(ikelog_ctx == NULL)) {
116  return result;
117  }
118  ikelog_ctx->eve_ctx = ajt;
119 
120  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
121  if (unlikely(output_ctx == NULL)) {
122  SCFree(ikelog_ctx);
123  return result;
124  }
125 
126  ikelog_ctx->flags = LOG_IKE_DEFAULT;
127  const char *extended = SCConfNodeLookupChildValue(conf, "extended");
128  if (extended) {
129  if (SCConfValIsTrue(extended)) {
130  ikelog_ctx->flags = LOG_IKE_EXTENDED;
131  }
132  }
133 
134  output_ctx->data = ikelog_ctx;
135  output_ctx->DeInit = OutputIKELogDeInitCtxSub;
136 
138 
139  result.ctx = output_ctx;
140  result.ok = true;
141  return result;
142 }
143 
144 static TmEcode JsonIKELogThreadInit(ThreadVars *t, const void *initdata, void **data)
145 {
146  LogIKELogThread *thread = SCCalloc(1, sizeof(*thread));
147  if (unlikely(thread == NULL)) {
148  return TM_ECODE_FAILED;
149  }
150 
151  if (initdata == NULL) {
152  SCLogDebug("Error getting context for EveLogIKE. \"initdata\" is NULL.");
153  goto error_exit;
154  }
155 
156  thread->ikelog_ctx = ((OutputCtx *)initdata)->data;
157  thread->ctx = CreateEveThreadCtx(t, thread->ikelog_ctx->eve_ctx);
158  if (!thread->ctx) {
159  goto error_exit;
160  }
161 
162  *data = (void *)thread;
163  return TM_ECODE_OK;
164 
165 error_exit:
166  SCFree(thread);
167  return TM_ECODE_FAILED;
168 }
169 
170 static TmEcode JsonIKELogThreadDeinit(ThreadVars *t, void *data)
171 {
172  LogIKELogThread *thread = (LogIKELogThread *)data;
173  if (thread == NULL) {
174  return TM_ECODE_OK;
175  }
176  FreeEveThreadCtx(thread->ctx);
177  SCFree(thread);
178  return TM_ECODE_OK;
179 }
180 
182 {
183  /* Register as an eve sub-module. */
184  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonIKELog", "eve-log.ike",
185  OutputIKELogInitSub, ALPROTO_IKE, JsonIKELogger, JsonIKELogThreadInit,
186  JsonIKELogThreadDeinit);
187 }
SCJsonBuilder
struct SCJsonBuilder SCJsonBuilder
Definition: detect-engine-helper.h:83
util-byte.h
tm-threads.h
SCConfValIsTrue
int SCConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:577
ALPROTO_IKE
@ ALPROTO_IKE
Definition: app-layer-protos.h:55
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
Flow_::proto
uint8_t proto
Definition: flow.h:381
CreateEveHeader
SCJsonBuilder * CreateEveHeader(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:816
threads.h
OutputJsonCtx_
Definition: output-json.h:77
Flow_
Flow data structure.
Definition: flow.h:359
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:411
rust.h
p
Packet * p
Definition: fuzz_dataset.c:30
OutputJsonBuilderBuffer
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:1018
SCConfNodeLookupChildValue
const char * SCConfNodeLookupChildValue(const SCConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:877
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
OutputCtx_::data
void * data
Definition: tm-modules.h:91
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
OutputCtx_
Definition: tm-modules.h:88
OutputJsonThreadCtx_
Definition: output-json.h:85
EveIKEAddMetadata
bool EveIKEAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
Definition: output-json-ike.c:64
util-debug.h
app-layer-ike.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
SCAppLayerParserRegisterLogger
void SCAppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:535
output-json.h
JsonIKELogRegister
void JsonIKELogRegister(void)
Definition: output-json-ike.c:181
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:48
app-layer-parser.h
LOG_IKE_DEFAULT
#define LOG_IKE_DEFAULT
Definition: output-json-ike.c:51
Packet_
Definition: decode.h:516
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:80
LogIKEFileCtx_::flags
uint32_t flags
Definition: output-json-ike.c:55
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1219
OutputInitResult_
Definition: output.h:46
Packet_::flow
struct Flow_ * flow
Definition: decode.h:564
LogIKELogThread_
Definition: output-json-ike.c:59
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:94
LOG_IKE_EXTENDED
#define LOG_IKE_EXTENDED
Definition: output-json-ike.c:52
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
LogIKEFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-ike.c:56
threadvars.h
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:489
SCFree
#define SCFree(p)
Definition: util-mem.h:61
output-json-ike.h
util-buffer.h
LogIKELogThread
struct LogIKELogThread_ LogIKELogThread
LogIKELogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-ike.c:61
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-eve-bindgen.h:34
LogIKEFileCtx
struct LogIKEFileCtx_ LogIKEFileCtx
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCConfNode_
Definition: conf.h:37
LogIKELogThread_::ikelog_ctx
LogIKEFileCtx * ikelog_ctx
Definition: output-json-ike.c:60
output.h
LogIKEFileCtx_
Definition: output-json-ike.c:54
app-layer.h
f
Flow f
Definition: fuzz_dataset.c:32