suricata
output-json-quic.c
Go to the documentation of this file.
1 /* Copyright (C) 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  * Implements JSON/eve logging for Quic app-layer.
22  */
23 
24 #include "suricata-common.h"
25 #include "detect.h"
26 #include "pkt-var.h"
27 #include "conf.h"
28 #include "threads.h"
29 #include "threadvars.h"
30 #include "tm-threads.h"
31 #include "util-unittest.h"
32 #include "util-buffer.h"
33 #include "util-debug.h"
34 #include "util-byte.h"
35 #include "output.h"
36 #include "output-json.h"
37 #include "app-layer.h"
38 #include "app-layer-parser.h"
39 #include "output-json-quic.h"
40 #include "rust.h"
41 
42 typedef struct LogQuicFileCtx_ {
46 
47 typedef struct JsonQuicLogThread_ {
51 
52 static int JsonQuicLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state,
53  void *tx, uint64_t tx_id)
54 {
55  JsonQuicLogThread *thread = thread_data;
56 
57  JsonBuilder *js =
58  CreateEveHeader(p, LOG_DIR_PACKET, "quic", NULL, thread->quiclog_ctx->eve_ctx);
59  if (unlikely(js == NULL)) {
60  return TM_ECODE_OK;
61  }
62  if (!rs_quic_to_json(tx, js)) {
63  jb_free(js);
64  return TM_ECODE_FAILED;
65  }
66  OutputJsonBuilderBuffer(js, thread->ctx);
67 
68  jb_free(js);
69  return TM_ECODE_OK;
70 }
71 
72 static void OutputQuicLogDeInitCtxSub(OutputCtx *output_ctx)
73 {
74  LogQuicFileCtx *quiclog_ctx = (LogQuicFileCtx *)output_ctx->data;
75  SCFree(quiclog_ctx);
76  SCFree(output_ctx);
77 }
78 
79 static OutputInitResult OutputQuicLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
80 {
81  OutputInitResult result = { NULL, false };
82  OutputJsonCtx *ajt = parent_ctx->data;
83 
84  LogQuicFileCtx *quiclog_ctx = SCCalloc(1, sizeof(*quiclog_ctx));
85  if (unlikely(quiclog_ctx == NULL)) {
86  return result;
87  }
88  quiclog_ctx->file_ctx = ajt->file_ctx;
89  quiclog_ctx->eve_ctx = ajt;
90 
91  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
92  if (unlikely(output_ctx == NULL)) {
93  SCFree(quiclog_ctx);
94  return result;
95  }
96  output_ctx->data = quiclog_ctx;
97  output_ctx->DeInit = OutputQuicLogDeInitCtxSub;
98 
100 
101  result.ctx = output_ctx;
102  result.ok = true;
103  return result;
104 }
105 
106 static TmEcode JsonQuicLogThreadInit(ThreadVars *t, const void *initdata, void **data)
107 {
108  if (initdata == NULL) {
109  SCLogDebug("Error getting context for EveLogQuic. \"initdata\" is NULL.");
110  return TM_ECODE_FAILED;
111  }
112 
113  JsonQuicLogThread *thread = SCCalloc(1, sizeof(*thread));
114  if (unlikely(thread == NULL)) {
115  return TM_ECODE_FAILED;
116  }
117 
118  thread->quiclog_ctx = ((OutputCtx *)initdata)->data;
119  thread->ctx = CreateEveThreadCtx(t, thread->quiclog_ctx->eve_ctx);
120  if (thread->ctx == NULL) {
121  goto error_exit;
122  }
123 
124  *data = (void *)thread;
125  return TM_ECODE_OK;
126 
127 error_exit:
128  SCFree(thread);
129  return TM_ECODE_FAILED;
130 }
131 
132 static TmEcode JsonQuicLogThreadDeinit(ThreadVars *t, void *data)
133 {
134  JsonQuicLogThread *thread = (JsonQuicLogThread *)data;
135  if (thread == NULL) {
136  return TM_ECODE_OK;
137  }
138  FreeEveThreadCtx(thread->ctx);
139  SCFree(thread);
140  return TM_ECODE_OK;
141 }
142 
143 bool JsonQuicAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
144 {
145  void *state = FlowGetAppState(f);
146  if (state) {
147  void *tx = AppLayerParserGetTx(f->proto, ALPROTO_QUIC, state, tx_id);
148  if (tx) {
149  return rs_quic_to_json(tx, js);
150  }
151  }
152 
153  return false;
154 }
155 
157 {
158  /* Register as an eve sub-module. */
159  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonQuicLog", "eve-log.quic",
160  OutputQuicLogInitSub, ALPROTO_QUIC, JsonQuicLogger, JsonQuicLogThreadInit,
161  JsonQuicLogThreadDeinit, NULL);
162 
163  SCLogDebug("quic json logger registered.");
164 }
util-byte.h
tm-threads.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
Flow_::proto
uint8_t proto
Definition: flow.h:378
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
ALPROTO_QUIC
@ ALPROTO_QUIC
Definition: app-layer-protos.h:51
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:356
LogFileCtx_
Definition: util-logopenfile.h:65
JsonQuicAddMetadata
bool JsonQuicAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
Definition: output-json-quic.c:143
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:934
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
rust.h
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:83
util-unittest.h
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1148
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:82
OutputCtx_
Definition: tm-modules.h:78
OutputJsonThreadCtx_
Definition: output-json.h:89
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:479
JsonQuicLogThread
struct JsonQuicLogThread_ JsonQuicLogThread
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:796
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
LogQuicFileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-quic.c:43
JsonQuicLogRegister
void JsonQuicLogRegister(void)
Definition: output-json-quic.c:156
Packet_
Definition: decode.h:425
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:81
LogQuicFileCtx_
Definition: output-json-quic.c:42
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1148
OutputInitResult_
Definition: output.h:46
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:39
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
JsonQuicLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-quic.c:49
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:412
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:456
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-buffer.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:82
JsonQuicLogThread_::quiclog_ctx
LogQuicFileCtx * quiclog_ctx
Definition: output-json-quic.c:48
LogQuicFileCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-quic.c:44
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
output-json-quic.h
LogQuicFileCtx
struct LogQuicFileCtx_ LogQuicFileCtx
output.h
app-layer.h
JsonQuicLogThread_
Definition: output-json-quic.c:47