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 
55 typedef struct LogIKEFileCtx_ {
56  uint32_t flags;
59 
60 typedef struct LogIKELogThread_ {
64 
65 bool EveIKEAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *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 rs_ike_logger_log(state, tx, LOG_IKE_EXTENDED, js);
72  }
73  }
74 
75  return false;
76 }
77 
78 static 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  JsonBuilder *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 (!rs_ike_logger_log(state, tx, ike_ctx->flags, jb)) {
90  goto error;
91  }
92 
93  OutputJsonBuilderBuffer(jb, thread->ctx);
94 
95  jb_free(jb);
96  return TM_ECODE_OK;
97 
98 error:
99  jb_free(jb);
100  return TM_ECODE_FAILED;
101 }
102 
103 static void OutputIKELogDeInitCtxSub(OutputCtx *output_ctx)
104 {
105  LogIKEFileCtx *ikelog_ctx = (LogIKEFileCtx *)output_ctx->data;
106  SCFree(ikelog_ctx);
107  SCFree(output_ctx);
108 }
109 
110 static OutputInitResult OutputIKELogInitSub(ConfNode *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 = ConfNodeLookupChildValue(conf, "extended");
129  if (extended) {
130  if (ConfValIsTrue(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 
145 static 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 
166 error_exit:
167  SCFree(thread);
168  return TM_ECODE_FAILED;
169 }
170 
171 static 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 }
util-byte.h
tm-threads.h
ALPROTO_IKE
@ ALPROTO_IKE
Definition: app-layer-protos.h:49
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
Flow_::proto
uint8_t proto
Definition: flow.h:378
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:356
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:967
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
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:83
util-unittest.h
IKETransaction
struct IKETransaction_ IKETransaction
Definition: app-layer-ike.h:33
ConfValIsTrue
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:536
OutputCtx_::data
void * data
Definition: tm-modules.h:88
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:82
OutputCtx_
Definition: tm-modules.h:85
OutputJsonThreadCtx_
Definition: output-json.h:89
util-debug.h
app-layer-ike.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
output-json.h
EveIKEAddMetadata
bool EveIKEAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
Definition: output-json-ike.c:65
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:816
JsonIKELogRegister
void JsonIKELogRegister(void)
Definition: output-json-ike.c:182
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:52
Packet_
Definition: decode.h:479
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:81
LogIKEFileCtx_::flags
uint32_t flags
Definition: output-json-ike.c:56
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1087
OutputInitResult_
Definition: output.h:46
LogIKELogThread_
Definition: output-json-ike.c:60
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:37
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:91
LOG_IKE_EXTENDED
#define LOG_IKE_EXTENDED
Definition: output-json-ike.c:53
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
LogIKEFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-ike.c:57
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-ike.h
util-buffer.h
LogIKELogThread
struct LogIKELogThread_ LogIKELogThread
LogIKELogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-ike.c:62
LogIKEFileCtx
struct LogIKEFileCtx_ LogIKEFileCtx
IKEState
struct IKEState_ IKEState
Definition: app-layer-ike.h:32
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
LogIKELogThread_::ikelog_ctx
LogIKEFileCtx * ikelog_ctx
Definition: output-json-ike.c:61
output.h
LogIKEFileCtx_
Definition: output-json-ike.c:55
app-layer.h
ConfNodeLookupChildValue
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:809