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 "debug.h"
29 #include "detect.h"
30 #include "pkt-var.h"
31 #include "conf.h"
32 
33 #include "threads.h"
34 #include "threadvars.h"
35 #include "tm-threads.h"
36 
37 #include "util-unittest.h"
38 #include "util-buffer.h"
39 #include "util-debug.h"
40 #include "util-byte.h"
41 
42 #include "output.h"
43 #include "output-json.h"
44 
45 #include "app-layer.h"
46 #include "app-layer-parser.h"
47 
48 #include "app-layer-ike.h"
49 #include "output-json-ike.h"
50 
51 #include "rust.h"
52 
53 #define LOG_IKE_DEFAULT 0
54 #define LOG_IKE_EXTENDED (1 << 0)
55 
56 typedef struct LogIKEFileCtx_ {
57  uint32_t flags;
60 
61 typedef struct LogIKELogThread_ {
65 
66 bool EveIKEAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
67 {
68  IKEState *state = FlowGetAppState(f);
69  if (state) {
70  IKETransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_IKE, state, tx_id);
71  if (tx) {
72  return rs_ike_logger_log(state, tx, LOG_IKE_EXTENDED, js);
73  }
74  }
75 
76  return false;
77 }
78 
79 static int JsonIKELogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state,
80  void *tx, uint64_t tx_id)
81 {
82  LogIKELogThread *thread = thread_data;
83  JsonBuilder *jb =
84  CreateEveHeader((Packet *)p, LOG_DIR_PACKET, "ike", NULL, thread->ikelog_ctx->eve_ctx);
85  if (unlikely(jb == NULL)) {
86  return TM_ECODE_FAILED;
87  }
88 
89  LogIKEFileCtx *ike_ctx = thread->ikelog_ctx;
90  if (!rs_ike_logger_log(state, tx, ike_ctx->flags, jb)) {
91  goto error;
92  }
93 
94  OutputJsonBuilderBuffer(jb, thread->ctx);
95 
96  jb_free(jb);
97  return TM_ECODE_OK;
98 
99 error:
100  jb_free(jb);
101  return TM_ECODE_FAILED;
102 }
103 
104 static void OutputIKELogDeInitCtxSub(OutputCtx *output_ctx)
105 {
106  LogIKEFileCtx *ikelog_ctx = (LogIKEFileCtx *)output_ctx->data;
107  SCFree(ikelog_ctx);
108  SCFree(output_ctx);
109 }
110 
111 static OutputInitResult OutputIKELogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
112 {
113  OutputInitResult result = { NULL, false };
114  OutputJsonCtx *ajt = parent_ctx->data;
115 
116  LogIKEFileCtx *ikelog_ctx = SCCalloc(1, sizeof(*ikelog_ctx));
117  if (unlikely(ikelog_ctx == NULL)) {
118  return result;
119  }
120  ikelog_ctx->eve_ctx = ajt;
121 
122  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
123  if (unlikely(output_ctx == NULL)) {
124  SCFree(ikelog_ctx);
125  return result;
126  }
127 
128  ikelog_ctx->flags = LOG_IKE_DEFAULT;
129  const char *extended = ConfNodeLookupChildValue(conf, "extended");
130  if (extended) {
131  if (ConfValIsTrue(extended)) {
132  ikelog_ctx->flags = LOG_IKE_EXTENDED;
133  }
134  }
135 
136  output_ctx->data = ikelog_ctx;
137  output_ctx->DeInit = OutputIKELogDeInitCtxSub;
138 
140 
141  result.ctx = output_ctx;
142  result.ok = true;
143  return result;
144 }
145 
146 static TmEcode JsonIKELogThreadInit(ThreadVars *t, const void *initdata, void **data)
147 {
148  LogIKELogThread *thread = SCCalloc(1, sizeof(*thread));
149  if (unlikely(thread == NULL)) {
150  return TM_ECODE_FAILED;
151  }
152 
153  if (initdata == NULL) {
154  SCLogDebug("Error getting context for EveLogIKE. \"initdata\" is NULL.");
155  goto error_exit;
156  }
157 
158  thread->ikelog_ctx = ((OutputCtx *)initdata)->data;
159  thread->ctx = CreateEveThreadCtx(t, thread->ikelog_ctx->eve_ctx);
160  if (!thread->ctx) {
161  goto error_exit;
162  }
163 
164  *data = (void *)thread;
165  return TM_ECODE_OK;
166 
167 error_exit:
168  SCFree(thread);
169  return TM_ECODE_FAILED;
170 }
171 
172 static TmEcode JsonIKELogThreadDeinit(ThreadVars *t, void *data)
173 {
174  LogIKELogThread *thread = (LogIKELogThread *)data;
175  if (thread == NULL) {
176  return TM_ECODE_OK;
177  }
178  FreeEveThreadCtx(thread->ctx);
179  SCFree(thread);
180  return TM_ECODE_OK;
181 }
182 
184 {
185  /* Register as an eve sub-module. */
186  OutputRegisterTxSubModule(LOGGER_JSON_IKE, "eve-log", "JsonIKELog", "eve-log.ike",
187  OutputIKELogInitSub, ALPROTO_IKE, JsonIKELogger, JsonIKELogThreadInit,
188  JsonIKELogThreadDeinit, NULL);
189 }
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:298
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:73
Flow_::proto
uint8_t proto
Definition: flow.h:372
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:350
LOGGER_JSON_IKE
@ LOGGER_JSON_IKE
Definition: suricata-common.h:454
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:974
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:44
rust.h
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
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:565
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1102
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
util-debug.h
app-layer-ike.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:44
output-json.h
EveIKEAddMetadata
bool EveIKEAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
Definition: output-json-ike.c:66
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:838
JsonIKELogRegister
void JsonIKELogRegister(void)
Definition: output-json-ike.c:183
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
LOG_IKE_DEFAULT
#define LOG_IKE_DEFAULT
Definition: output-json-ike.c:53
Packet_
Definition: decode.h:416
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
LogIKEFileCtx_::flags
uint32_t flags
Definition: output-json-ike.c:57
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1074
OutputInitResult_
Definition: output.h:43
LogIKELogThread_
Definition: output-json-ike.c:61
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:39
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
LOG_IKE_EXTENDED
#define LOG_IKE_EXTENDED
Definition: output-json-ike.c:54
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:410
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
LogIKEFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-ike.c:58
threadvars.h
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:63
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:62
debug.h
output.h
LogIKEFileCtx_
Definition: output-json-ike.c:56
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:842