suricata
output-json-sip.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 Giuseppe Longo <giuseppe@glongo.it>
22  *
23  * Implement JSON/eve logging app-layer SIP.
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-sip.h"
48 #include "output-json-sip.h"
49 
50 #include "rust.h"
51 
52 typedef struct LogSIPFileCtx_ {
56 
57 typedef struct LogSIPLogThread_ {
61 
62 json_t *JsonSIPAddMetadata(const Flow *f, uint64_t tx_id)
63 {
64  SIPState *state = FlowGetAppState(f);
65  if (state) {
66  SIPTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_SIP, state, tx_id);
67  if (tx) {
68  return rs_sip_log_json(state, tx);
69  }
70  }
71 
72  return NULL;
73 }
74 
75 static int JsonSIPLogger(ThreadVars *tv, void *thread_data,
76  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
77 {
78  SIPTransaction *siptx = tx;
79  LogSIPLogThread *thread = thread_data;
80  json_t *js, *sipjs;
81 
82  js = CreateJSONHeader(p, LOG_DIR_PACKET, "sip");
83  if (unlikely(js == NULL)) {
84  return TM_ECODE_FAILED;
85  }
86 
87  JsonAddCommonOptions(&thread->siplog_ctx->cfg, p, f, js);
88 
89  sipjs = rs_sip_log_json(state, siptx);
90  if (unlikely(sipjs == NULL)) {
91  goto error;
92  }
93  json_object_set_new(js, "sip", sipjs);
94 
95  MemBufferReset(thread->buffer);
96  OutputJSONBuffer(js, thread->siplog_ctx->file_ctx, &thread->buffer);
97 
98  json_decref(js);
99  return TM_ECODE_OK;
100 
101 error:
102  json_decref(js);
103  return TM_ECODE_FAILED;
104 }
105 
106 static void OutputSIPLogDeInitCtxSub(OutputCtx *output_ctx)
107 {
108  LogSIPFileCtx *siplog_ctx = (LogSIPFileCtx *)output_ctx->data;
109  SCFree(siplog_ctx);
110  SCFree(output_ctx);
111 }
112 
113 static OutputInitResult OutputSIPLogInitSub(ConfNode *conf,
114  OutputCtx *parent_ctx)
115 {
116  OutputInitResult result = { NULL, false };
117  OutputJsonCtx *ajt = parent_ctx->data;
118 
119  LogSIPFileCtx *siplog_ctx = SCCalloc(1, sizeof(*siplog_ctx));
120  if (unlikely(siplog_ctx == NULL)) {
121  return result;
122  }
123  siplog_ctx->file_ctx = ajt->file_ctx;
124  siplog_ctx->cfg = ajt->cfg;
125 
126  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
127  if (unlikely(output_ctx == NULL)) {
128  SCFree(siplog_ctx);
129  return result;
130  }
131  output_ctx->data = siplog_ctx;
132  output_ctx->DeInit = OutputSIPLogDeInitCtxSub;
133 
134  SCLogDebug("SIP log sub-module initialized.");
135 
137 
138  result.ctx = output_ctx;
139  result.ok = true;
140  return result;
141 }
142 
143 #define OUTPUT_BUFFER_SIZE 65535
144 
145 static TmEcode JsonSIPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
146 {
147  LogSIPLogThread *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 EveLogSIP. \"initdata\" is NULL.");
154  SCFree(thread);
155  return TM_ECODE_FAILED;
156  }
157 
159  if (unlikely(thread->buffer == NULL)) {
160  SCFree(thread);
161  return TM_ECODE_FAILED;
162  }
163 
164  thread->siplog_ctx = ((OutputCtx *)initdata)->data;
165  *data = (void *)thread;
166 
167  return TM_ECODE_OK;
168 }
169 
170 static TmEcode JsonSIPLogThreadDeinit(ThreadVars *t, void *data)
171 {
172  LogSIPLogThread *thread = (LogSIPLogThread *)data;
173  if (thread == NULL) {
174  return TM_ECODE_OK;
175  }
176  if (thread->buffer != NULL) {
177  MemBufferFree(thread->buffer);
178  }
179  SCFree(thread);
180  return TM_ECODE_OK;
181 }
182 
184 {
185  /* Register as an eve sub-module. */
186  OutputRegisterTxSubModule(LOGGER_JSON_SIP, "eve-log", "JsonSIPLog",
187  "eve-log.sip", OutputSIPLogInitSub, ALPROTO_SIP,
188  JsonSIPLogger, JsonSIPLogThreadInit,
189  JsonSIPLogThreadDeinit, NULL);
190 
191  SCLogDebug("SIP JSON logger registered.");
192 }
util-byte.h
tm-threads.h
JsonSIPAddMetadata
json_t * JsonSIPAddMetadata(const Flow *f, uint64_t tx_id)
Definition: output-json-sip.c:62
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:81
SCFree
#define SCFree(a)
Definition: util-mem.h:322
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:335
Flow_::proto
uint8_t proto
Definition: flow.h:359
threads.h
OutputJsonCtx_
Definition: output-json.h:78
Flow_
Flow data structure.
Definition: flow.h:340
OutputJsonCommonSettings_
Definition: output-json.h:69
LogFileCtx_
Definition: util-logopenfile.h:52
ALPROTO_SIP
@ ALPROTO_SIP
Definition: app-layer-protos.h:53
output-json-sip.h
rust.h
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
util-unittest.h
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1082
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
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:42
LOGGER_JSON_SIP
@ LOGGER_JSON_SIP
Definition: suricata-common.h:441
output-json.h
LogSIPLogThread_::siplog_ctx
LogSIPFileCtx * siplog_ctx
Definition: output-json-sip.c:58
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:471
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
pkt-var.h
OUTPUT_BUFFER_SIZE
#define OUTPUT_BUFFER_SIZE
Definition: output-json-sip.c:143
LogSIPLogThread_::buffer
MemBuffer * buffer
Definition: output-json-sip.c:59
LogSIPFileCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json-sip.c:54
OutputInitResult_::ok
bool ok
Definition: output.h:43
app-layer-parser.h
Packet_
Definition: decode.h:408
SCCalloc
#define SCCalloc(nm, a)
Definition: util-mem.h:253
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:77
app-layer-sip.h
MemBuffer_
Definition: util-buffer.h:27
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1047
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
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:411
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LogSIPFileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-sip.c:53
ConfNode_
Definition: conf.h:32
util-buffer.h
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
LogSIPLogThread
struct LogSIPLogThread_ LogSIPLogThread
JsonSIPLogRegister
void JsonSIPLogRegister(void)
Definition: output-json-sip.c:183
LogSIPFileCtx
struct LogSIPFileCtx_ LogSIPFileCtx
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
LogSIPFileCtx_
Definition: output-json-sip.c:52
LogSIPLogThread_
Definition: output-json-sip.c:57
app-layer.h