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 #include "rust-ikev2-log-gen.h"
52 
53 typedef struct LogIKEv2FileCtx_ {
57 
58 typedef struct LogIKEv2LogThread_ {
62 
63 static int JsonIKEv2Logger(ThreadVars *tv, void *thread_data,
64  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
65 {
66  IKEV2Transaction *ikev2tx = tx;
67  LogIKEv2LogThread *thread = thread_data;
68  json_t *js, *ikev2js;
69 
70  js = CreateJSONHeader((Packet *)p, LOG_DIR_PACKET, "ikev2");
71  if (unlikely(js == NULL)) {
72  return TM_ECODE_FAILED;
73  }
74 
75  JsonAddCommonOptions(&thread->ikev2log_ctx->cfg, p, f, js);
76 
77  ikev2js = rs_ikev2_log_json_response(state, ikev2tx);
78  if (unlikely(ikev2js == NULL)) {
79  goto error;
80  }
81  json_object_set_new(js, "ikev2", ikev2js);
82 
83  MemBufferReset(thread->buffer);
84  OutputJSONBuffer(js, thread->ikev2log_ctx->file_ctx, &thread->buffer);
85 
86  json_decref(js);
87  return TM_ECODE_OK;
88 
89 error:
90  json_decref(js);
91  return TM_ECODE_FAILED;
92 }
93 
94 static void OutputIKEv2LogDeInitCtxSub(OutputCtx *output_ctx)
95 {
96  LogIKEv2FileCtx *ikev2log_ctx = (LogIKEv2FileCtx *)output_ctx->data;
97  SCFree(ikev2log_ctx);
98  SCFree(output_ctx);
99 }
100 
101 static OutputInitResult OutputIKEv2LogInitSub(ConfNode *conf,
102  OutputCtx *parent_ctx)
103 {
104  OutputInitResult result = { NULL, false };
105  OutputJsonCtx *ajt = parent_ctx->data;
106 
107  LogIKEv2FileCtx *ikev2log_ctx = SCCalloc(1, sizeof(*ikev2log_ctx));
108  if (unlikely(ikev2log_ctx == NULL)) {
109  return result;
110  }
111  ikev2log_ctx->file_ctx = ajt->file_ctx;
112  ikev2log_ctx->cfg = ajt->cfg;
113 
114  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
115  if (unlikely(output_ctx == NULL)) {
116  SCFree(ikev2log_ctx);
117  return result;
118  }
119  output_ctx->data = ikev2log_ctx;
120  output_ctx->DeInit = OutputIKEv2LogDeInitCtxSub;
121 
122  SCLogDebug("IKEv2 log sub-module initialized.");
123 
125 
126  result.ctx = output_ctx;
127  result.ok = true;
128  return result;
129 }
130 
131 static TmEcode JsonIKEv2LogThreadInit(ThreadVars *t, const void *initdata, void **data)
132 {
133  LogIKEv2LogThread *thread = SCCalloc(1, sizeof(*thread));
134  if (unlikely(thread == NULL)) {
135  return TM_ECODE_FAILED;
136  }
137 
138  if (initdata == NULL) {
139  SCLogDebug("Error getting context for EveLogIKEv2. \"initdata\" is NULL.");
140  SCFree(thread);
141  return TM_ECODE_FAILED;
142  }
143 
145  if (unlikely(thread->buffer == NULL)) {
146  SCFree(thread);
147  return TM_ECODE_FAILED;
148  }
149 
150  thread->ikev2log_ctx = ((OutputCtx *)initdata)->data;
151  *data = (void *)thread;
152 
153  return TM_ECODE_OK;
154 }
155 
156 static TmEcode JsonIKEv2LogThreadDeinit(ThreadVars *t, void *data)
157 {
158  LogIKEv2LogThread *thread = (LogIKEv2LogThread *)data;
159  if (thread == NULL) {
160  return TM_ECODE_OK;
161  }
162  if (thread->buffer != NULL) {
163  MemBufferFree(thread->buffer);
164  }
165  SCFree(thread);
166  return TM_ECODE_OK;
167 }
168 
170 {
171  /* Register as an eve sub-module. */
172  OutputRegisterTxSubModule(LOGGER_JSON_IKEV2, "eve-log", "JsonIKEv2Log",
173  "eve-log.ikev2", OutputIKEv2LogInitSub, ALPROTO_IKEV2,
174  JsonIKEv2Logger, JsonIKEv2LogThreadInit,
175  JsonIKEv2LogThreadDeinit, NULL);
176 
177  SCLogDebug("IKEv2 JSON logger registered.");
178 }
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:44
json_t * CreateJSONHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type)
Definition: output-json.c:710
#define SCLogDebug(...)
Definition: util-debug.h:335
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:809
#define unlikely(expr)
Definition: util-optimize.h:35
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
void JsonIKEv2LogRegister(void)
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
void JsonAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, json_t *js)
Definition: output-json.c:390
struct LogIKEv2FileCtx_ LogIKEv2FileCtx
LogFileCtx * file_ctx
#define SCCalloc(nm, a)
Definition: util-mem.h:253
OutputJsonCommonSettings cfg
Definition: output-json.h:81
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
LogFileCtx * file_ctx
Definition: output-json.h:79
struct IKEV2Transaction_ IKEV2Transaction
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:402
LogIKEv2FileCtx * ikev2log_ctx
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
#define SCFree(a)
Definition: util-mem.h:322
uint16_t tx_id
OutputJsonCommonSettings cfg
void * data
Definition: tm-modules.h:81
struct LogIKEv2LogThread_ LogIKEv2LogThread
Per thread variable structure.
Definition: threadvars.h:57
Flow data structure.
Definition: flow.h:325
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82