suricata
output-json-http2.c
Go to the documentation of this file.
1 /* Copyright (C) 2020-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 Philippe Antoine <p.antoine@catenacyber.fr>
22  *
23  * Implements HTTP2 JSON logging portion of the engine.
24  */
25 
26 #include "suricata-common.h"
27 #include "detect.h"
28 #include "pkt-var.h"
29 #include "conf.h"
30 
31 #include "threads.h"
32 #include "threadvars.h"
33 #include "tm-threads.h"
34 
35 #include "util-print.h"
36 #include "util-unittest.h"
37 
38 #include "util-debug.h"
39 #include "app-layer-parser.h"
40 #include "output.h"
41 #include "app-layer-http2.h"
42 #include "app-layer.h"
43 #include "util-privs.h"
44 #include "util-buffer.h"
45 
46 #include "util-logopenfile.h"
47 
48 #include "output-json.h"
49 #include "output-json-http2.h"
50 #include "rust.h"
51 
52 #define MODULE_NAME "LogHttp2Log"
53 
54 typedef struct OutputHttp2Ctx_ {
57 
58 
59 typedef struct JsonHttp2LogThread_ {
63 
64 static int JsonHttp2Logger(ThreadVars *tv, void *thread_data, const Packet *p,
65  Flow *f, void *state, void *txptr, uint64_t tx_id)
66 {
67  JsonHttp2LogThread *aft = (JsonHttp2LogThread *)thread_data;
68 
69  if (unlikely(state == NULL)) {
70  return 0;
71  }
72 
73  JsonBuilder *js = CreateEveHeaderWithTxId(
74  p, LOG_DIR_FLOW, "http", NULL, tx_id, aft->http2log_ctx->eve_ctx);
75  if (unlikely(js == NULL))
76  return 0;
77 
78  if (!rs_http2_log_json(txptr, js)) {
79  goto end;
80  }
81  OutputJsonBuilderBuffer(js, aft->ctx);
82 end:
83  jb_free(js);
84  return 0;
85 }
86 
87 static TmEcode JsonHttp2LogThreadInit(ThreadVars *t, const void *initdata, void **data)
88 {
90  if (unlikely(aft == NULL))
91  return TM_ECODE_FAILED;
92 
93  if(initdata == NULL)
94  {
95  SCLogDebug("Error getting context for EveLogHTTP2. \"initdata\" argument NULL");
96  goto error_exit;
97  }
98 
99  /* Use the Output Context (file pointer and mutex) */
100  aft->http2log_ctx = ((OutputCtx *)initdata)->data;
101  aft->ctx = CreateEveThreadCtx(t, aft->http2log_ctx->eve_ctx);
102  if (!aft->ctx) {
103  goto error_exit;
104  }
105 
106  *data = (void *)aft;
107  return TM_ECODE_OK;
108 
109 error_exit:
110  SCFree(aft);
111  return TM_ECODE_FAILED;
112 }
113 
114 static TmEcode JsonHttp2LogThreadDeinit(ThreadVars *t, void *data)
115 {
116  JsonHttp2LogThread *aft = (JsonHttp2LogThread *)data;
117  if (aft == NULL) {
118  return TM_ECODE_OK;
119  }
120 
121  FreeEveThreadCtx(aft->ctx);
122  /* clear memory */
123  memset(aft, 0, sizeof(JsonHttp2LogThread));
124 
125  SCFree(aft);
126  return TM_ECODE_OK;
127 }
128 
129 static void OutputHttp2LogDeinitSub(OutputCtx *output_ctx)
130 {
131  OutputHttp2Ctx *http2_ctx = output_ctx->data;
132  SCFree(http2_ctx);
133  SCFree(output_ctx);
134 }
135 
136 static OutputInitResult OutputHttp2LogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
137 {
138  OutputInitResult result = { NULL, false };
139  OutputJsonCtx *ojc = parent_ctx->data;
140 
141  OutputHttp2Ctx *http2_ctx = SCCalloc(1, sizeof(OutputHttp2Ctx));
142  if (unlikely(http2_ctx == NULL))
143  return result;
144 
145  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
146  if (unlikely(output_ctx == NULL)) {
147  SCFree(http2_ctx);
148  return result;
149  }
150 
151  http2_ctx->eve_ctx = ojc;
152 
153  output_ctx->data = http2_ctx;
154  output_ctx->DeInit = OutputHttp2LogDeinitSub;
155 
157 
158  result.ctx = output_ctx;
159  result.ok = true;
160  return result;
161 }
162 
164 {
165  /* also register as child of eve-log */
167  OutputHttp2LogInitSub, ALPROTO_HTTP2, JsonHttp2Logger, HTTP2StateClosed,
168  HTTP2StateClosed, JsonHttp2LogThreadInit, JsonHttp2LogThreadDeinit, NULL);
169 }
tm-threads.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
JsonHttp2LogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-http2.c:61
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:350
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:931
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
rust.h
util-privs.h
app-layer-http2.h
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:87
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
OutputCtx_
Definition: tm-modules.h:84
OutputJsonThreadCtx_
Definition: output-json.h:89
JsonHttp2LogThread
struct JsonHttp2LogThread_ JsonHttp2LogThread
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
output-json.h
OutputRegisterTxSubModuleWithProgress
void OutputRegisterTxSubModuleWithProgress(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, int tc_log_progress, int ts_log_progress, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:382
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:475
util-print.h
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:48
app-layer-parser.h
CreateEveHeaderWithTxId
JsonBuilder * CreateEveHeaderWithTxId(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, uint64_t tx_id, OutputJsonCtx *eve_ctx)
Definition: output-json.c:876
Packet_
Definition: decode.h:430
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:83
MODULE_NAME
#define MODULE_NAME
Definition: output-json-http2.c:52
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:61
OutputHttp2Ctx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-http2.c:55
OutputInitResult_
Definition: output.h:46
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:90
JsonHttp2LogThread_
Definition: output-json-http2.c:59
JsonHttp2LogThread_::http2log_ctx
OutputHttp2Ctx * http2log_ctx
Definition: output-json-http2.c:60
OutputHttp2Ctx_
Definition: output-json-http2.c:54
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:40
output-json-http2.h
JsonHttp2LogRegister
void JsonHttp2LogRegister(void)
Definition: output-json-http2.c:163
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:467
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-logopenfile.h
util-buffer.h
OutputHttp2Ctx
struct OutputHttp2Ctx_ OutputHttp2Ctx
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
output.h
app-layer.h