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