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 #include "rust-sip-log-gen.h"
52 
53 typedef struct LogSIPFileCtx_ {
57 
58 typedef struct LogSIPLogThread_ {
62 
63 json_t *JsonSIPAddMetadata(const Flow *f, uint64_t tx_id)
64 {
65  SIPState *state = FlowGetAppState(f);
66  if (state) {
67  SIPTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_SIP, state, tx_id);
68  if (tx) {
69  return rs_sip_log_json(state, tx);
70  }
71  }
72 
73  return NULL;
74 }
75 
76 static int JsonSIPLogger(ThreadVars *tv, void *thread_data,
77  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
78 {
79  SIPTransaction *siptx = tx;
80  LogSIPLogThread *thread = thread_data;
81  json_t *js, *sipjs;
82 
83  js = CreateJSONHeader(p, LOG_DIR_PACKET, "sip");
84  if (unlikely(js == NULL)) {
85  return TM_ECODE_FAILED;
86  }
87 
88  JsonAddCommonOptions(&thread->siplog_ctx->cfg, p, f, js);
89 
90  sipjs = rs_sip_log_json(state, siptx);
91  if (unlikely(sipjs == NULL)) {
92  goto error;
93  }
94  json_object_set_new(js, "sip", sipjs);
95 
96  MemBufferReset(thread->buffer);
97  OutputJSONBuffer(js, thread->siplog_ctx->file_ctx, &thread->buffer);
98 
99  json_decref(js);
100  return TM_ECODE_OK;
101 
102 error:
103  json_decref(js);
104  return TM_ECODE_FAILED;
105 }
106 
107 static void OutputSIPLogDeInitCtxSub(OutputCtx *output_ctx)
108 {
109  LogSIPFileCtx *siplog_ctx = (LogSIPFileCtx *)output_ctx->data;
110  SCFree(siplog_ctx);
111  SCFree(output_ctx);
112 }
113 
114 static OutputInitResult OutputSIPLogInitSub(ConfNode *conf,
115  OutputCtx *parent_ctx)
116 {
117  OutputInitResult result = { NULL, false };
118  OutputJsonCtx *ajt = parent_ctx->data;
119 
120  LogSIPFileCtx *siplog_ctx = SCCalloc(1, sizeof(*siplog_ctx));
121  if (unlikely(siplog_ctx == NULL)) {
122  return result;
123  }
124  siplog_ctx->file_ctx = ajt->file_ctx;
125  siplog_ctx->cfg = ajt->cfg;
126 
127  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
128  if (unlikely(output_ctx == NULL)) {
129  SCFree(siplog_ctx);
130  return result;
131  }
132  output_ctx->data = siplog_ctx;
133  output_ctx->DeInit = OutputSIPLogDeInitCtxSub;
134 
135  SCLogDebug("SIP log sub-module initialized.");
136 
138 
139  result.ctx = output_ctx;
140  result.ok = true;
141  return result;
142 }
143 
144 #define OUTPUT_BUFFER_SIZE 65535
145 
146 static TmEcode JsonSIPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
147 {
148  LogSIPLogThread *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 EveLogSIP. \"initdata\" is NULL.");
155  SCFree(thread);
156  return TM_ECODE_FAILED;
157  }
158 
160  if (unlikely(thread->buffer == NULL)) {
161  SCFree(thread);
162  return TM_ECODE_FAILED;
163  }
164 
165  thread->siplog_ctx = ((OutputCtx *)initdata)->data;
166  *data = (void *)thread;
167 
168  return TM_ECODE_OK;
169 }
170 
171 static TmEcode JsonSIPLogThreadDeinit(ThreadVars *t, void *data)
172 {
173  LogSIPLogThread *thread = (LogSIPLogThread *)data;
174  if (thread == NULL) {
175  return TM_ECODE_OK;
176  }
177  if (thread->buffer != NULL) {
178  MemBufferFree(thread->buffer);
179  }
180  SCFree(thread);
181  return TM_ECODE_OK;
182 }
183 
185 {
186  /* Register as an eve sub-module. */
187  OutputRegisterTxSubModule(LOGGER_JSON_SIP, "eve-log", "JsonSIPLog",
188  "eve-log.sip", OutputSIPLogInitSub, ALPROTO_SIP,
189  JsonSIPLogger, JsonSIPLogThreadInit,
190  JsonSIPLogThreadDeinit, NULL);
191 
192  SCLogDebug("SIP JSON logger registered.");
193 }
struct LogSIPLogThread_ LogSIPLogThread
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
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
uint8_t proto
Definition: flow.h:344
#define unlikely(expr)
Definition: util-optimize.h:35
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
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
LogFileCtx * file_ctx
LogSIPFileCtx * siplog_ctx
struct LogSIPFileCtx_ LogSIPFileCtx
#define SCCalloc(nm, a)
Definition: util-mem.h:253
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
struct SIPTransaction_ SIPTransaction
Definition: app-layer-sip.h:32
json_t * JsonSIPAddMetadata(const Flow *f, uint64_t tx_id)
OutputJsonCommonSettings cfg
Definition: output-json.h:81
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
LogFileCtx * file_ctx
Definition: output-json.h:79
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
void JsonSIPLogRegister(void)
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
#define SCFree(a)
Definition: util-mem.h:322
uint16_t tx_id
MemBuffer * buffer
void * data
Definition: tm-modules.h:81
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1068
OutputJsonCommonSettings cfg
Per thread variable structure.
Definition: threadvars.h:57
struct SIPState_ SIPState
Definition: app-layer-sip.h:31
Flow data structure.
Definition: flow.h:325
#define OUTPUT_BUFFER_SIZE
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82