suricata
output-json-ikev2.c
Go to the documentation of this file.
1 /* Copyright (C) 2018 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  *
23  * Implement JSON/eve logging app-layer IKEv2.
24  */
25 
26 #include "suricata-common.h"
27 #include "debug.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-ikev2.h"
48 #include "output-json-ikev2.h"
49 
50 #include "rust.h"
51 
52 typedef struct LogIKEv2FileCtx_ {
56 
57 typedef struct LogIKEv2LogThread_ {
61 
62 static int JsonIKEv2Logger(ThreadVars *tv, void *thread_data,
63  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
64 {
65  IKEV2Transaction *ikev2tx = tx;
66  LogIKEv2LogThread *thread = thread_data;
67  json_t *js, *ikev2js;
68 
69  js = CreateJSONHeader((Packet *)p, LOG_DIR_PACKET, "ikev2");
70  if (unlikely(js == NULL)) {
71  return TM_ECODE_FAILED;
72  }
73 
74  JsonAddCommonOptions(&thread->ikev2log_ctx->cfg, p, f, js);
75 
76  ikev2js = rs_ikev2_log_json_response(state, ikev2tx);
77  if (unlikely(ikev2js == NULL)) {
78  goto error;
79  }
80  json_object_set_new(js, "ikev2", ikev2js);
81 
82  MemBufferReset(thread->buffer);
83  OutputJSONBuffer(js, thread->ikev2log_ctx->file_ctx, &thread->buffer);
84 
85  json_decref(js);
86  return TM_ECODE_OK;
87 
88 error:
89  json_decref(js);
90  return TM_ECODE_FAILED;
91 }
92 
93 static void OutputIKEv2LogDeInitCtxSub(OutputCtx *output_ctx)
94 {
95  LogIKEv2FileCtx *ikev2log_ctx = (LogIKEv2FileCtx *)output_ctx->data;
96  SCFree(ikev2log_ctx);
97  SCFree(output_ctx);
98 }
99 
100 static OutputInitResult OutputIKEv2LogInitSub(ConfNode *conf,
101  OutputCtx *parent_ctx)
102 {
103  OutputInitResult result = { NULL, false };
104  OutputJsonCtx *ajt = parent_ctx->data;
105 
106  LogIKEv2FileCtx *ikev2log_ctx = SCCalloc(1, sizeof(*ikev2log_ctx));
107  if (unlikely(ikev2log_ctx == NULL)) {
108  return result;
109  }
110  ikev2log_ctx->file_ctx = ajt->file_ctx;
111  ikev2log_ctx->cfg = ajt->cfg;
112 
113  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
114  if (unlikely(output_ctx == NULL)) {
115  SCFree(ikev2log_ctx);
116  return result;
117  }
118  output_ctx->data = ikev2log_ctx;
119  output_ctx->DeInit = OutputIKEv2LogDeInitCtxSub;
120 
121  SCLogDebug("IKEv2 log sub-module initialized.");
122 
124 
125  result.ctx = output_ctx;
126  result.ok = true;
127  return result;
128 }
129 
130 static TmEcode JsonIKEv2LogThreadInit(ThreadVars *t, const void *initdata, void **data)
131 {
132  LogIKEv2LogThread *thread = SCCalloc(1, sizeof(*thread));
133  if (unlikely(thread == NULL)) {
134  return TM_ECODE_FAILED;
135  }
136 
137  if (initdata == NULL) {
138  SCLogDebug("Error getting context for EveLogIKEv2. \"initdata\" is NULL.");
139  SCFree(thread);
140  return TM_ECODE_FAILED;
141  }
142 
144  if (unlikely(thread->buffer == NULL)) {
145  SCFree(thread);
146  return TM_ECODE_FAILED;
147  }
148 
149  thread->ikev2log_ctx = ((OutputCtx *)initdata)->data;
150  *data = (void *)thread;
151 
152  return TM_ECODE_OK;
153 }
154 
155 static TmEcode JsonIKEv2LogThreadDeinit(ThreadVars *t, void *data)
156 {
157  LogIKEv2LogThread *thread = (LogIKEv2LogThread *)data;
158  if (thread == NULL) {
159  return TM_ECODE_OK;
160  }
161  if (thread->buffer != NULL) {
162  MemBufferFree(thread->buffer);
163  }
164  SCFree(thread);
165  return TM_ECODE_OK;
166 }
167 
169 {
170  /* Register as an eve sub-module. */
171  OutputRegisterTxSubModule(LOGGER_JSON_IKEV2, "eve-log", "JsonIKEv2Log",
172  "eve-log.ikev2", OutputIKEv2LogInitSub, ALPROTO_IKEV2,
173  JsonIKEv2Logger, JsonIKEv2LogThreadInit,
174  JsonIKEv2LogThreadDeinit, NULL);
175 
176  SCLogDebug("IKEv2 JSON logger registered.");
177 }
util-byte.h
ALPROTO_IKEV2
@ ALPROTO_IKEV2
Definition: app-layer-protos.h:49
tm-threads.h
LogIKEv2FileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-ikev2.c:53
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:81
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
app-layer-ikev2.h
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:44
threads.h
OutputJsonCtx_
Definition: output-json.h:78
Flow_
Flow data structure.
Definition: flow.h:343
OutputJsonCommonSettings_
Definition: output-json.h:69
LogFileCtx_
Definition: util-logopenfile.h:52
LogIKEv2FileCtx
struct LogIKEv2FileCtx_ LogIKEv2FileCtx
rust.h
LogIKEv2LogThread_
Definition: output-json-ikev2.c:57
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
OutputCtx_
Definition: tm-modules.h:78
LogIKEv2LogThread_::buffer
MemBuffer * buffer
Definition: output-json-ikev2.c:59
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:42
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:471
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:43
IKEV2Transaction
struct IKEV2Transaction_ IKEV2Transaction
Definition: app-layer-ikev2.h:31
app-layer-parser.h
output-json-ikev2.h
Packet_
Definition: decode.h:411
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:77
MemBuffer_
Definition: util-buffer.h:27
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:41
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:37
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
LogIKEv2LogThread
struct LogIKEv2LogThread_ LogIKEv2LogThread
CreateJSONHeader
json_t * CreateJSONHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type)
Definition: output-json.c:710
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
JsonAddCommonOptions
void JsonAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, json_t *js)
Definition: output-json.c:390
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
threadvars.h
LogIKEv2LogThread_::ikev2log_ctx
LogIKEv2FileCtx * ikev2log_ctx
Definition: output-json-ikev2.c:58
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
LogIKEv2FileCtx_
Definition: output-json-ikev2.c:52
LogIKEv2FileCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json-ikev2.c:54
util-buffer.h
JsonIKEv2LogRegister
void JsonIKEv2LogRegister(void)
Definition: output-json-ikev2.c:168
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:79
OutputJSONBuffer
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:809
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
LOGGER_JSON_IKEV2
@ LOGGER_JSON_IKEV2
Definition: suricata-common.h:460
app-layer.h