]> git.ipfire.org Git - people/ms/suricata.git/blame - src/app-layer-parser.h
app-layer: include decoder events in app-layer tx data
[people/ms/suricata.git] / src / app-layer-parser.h
CommitLineData
21e6f1f0 1/* Copyright (C) 2007-2020 Open Information Security Foundation
ce019275
WM
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 Victor Julien <victor@inliniac.net>
429c6388 22 * \author Anoop Saldanha <anoopsaldanha@gmail.com>
ce019275
WM
23 */
24
59327e0f
VJ
25#ifndef __APP_LAYER_PARSER_H__
26#define __APP_LAYER_PARSER_H__
8e10844f 27
347c0df9 28#include "app-layer-events.h"
1cf02560 29#include "detect-engine-state.h"
e1022ee5 30#include "util-file.h"
b160c49e 31#include "stream-tcp-private.h"
b573c16d 32#include "rust.h"
5665fc83 33#include "util-config.h"
e1022ee5 34
c862bbdc 35/* Flags for AppLayerParserState. */
4f73943d 36// flag available BIT_U8(0)
26eb49d7
EL
37#define APP_LAYER_PARSER_NO_INSPECTION BIT_U8(1)
38#define APP_LAYER_PARSER_NO_REASSEMBLY BIT_U8(2)
39#define APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD BIT_U8(3)
40#define APP_LAYER_PARSER_BYPASS_READY BIT_U8(4)
4f73943d
VJ
41#define APP_LAYER_PARSER_EOF_TS BIT_U8(5)
42#define APP_LAYER_PARSER_EOF_TC BIT_U8(6)
2c857087 43
c862bbdc 44/* Flags for AppLayerParserProtoCtx. */
c8fb9bcb 45#define APP_LAYER_PARSER_OPT_ACCEPT_GAPS BIT_U32(0)
ac3cf6ff 46#define APP_LAYER_PARSER_OPT_UNIDIR_TXS BIT_U32(1)
c8fb9bcb
VJ
47
48#define APP_LAYER_PARSER_INT_STREAM_DEPTH_SET BIT_U32(0)
c862bbdc 49
daeb8fd3
VJ
50/* applies to DetectFlags uint64_t field */
51
ed877849
VJ
52/** reserved for future use */
53#define APP_LAYER_TX_RESERVED1_FLAG BIT_U64(48)
54#define APP_LAYER_TX_RESERVED2_FLAG BIT_U64(49)
55#define APP_LAYER_TX_RESERVED3_FLAG BIT_U64(50)
56#define APP_LAYER_TX_RESERVED4_FLAG BIT_U64(51)
57#define APP_LAYER_TX_RESERVED5_FLAG BIT_U64(52)
58#define APP_LAYER_TX_RESERVED6_FLAG BIT_U64(53)
59#define APP_LAYER_TX_RESERVED7_FLAG BIT_U64(54)
60#define APP_LAYER_TX_RESERVED8_FLAG BIT_U64(55)
61#define APP_LAYER_TX_RESERVED9_FLAG BIT_U64(56)
62#define APP_LAYER_TX_RESERVED10_FLAG BIT_U64(57)
63#define APP_LAYER_TX_RESERVED11_FLAG BIT_U64(58)
64#define APP_LAYER_TX_RESERVED12_FLAG BIT_U64(59)
65#define APP_LAYER_TX_RESERVED13_FLAG BIT_U64(60)
66#define APP_LAYER_TX_RESERVED14_FLAG BIT_U64(61)
67#define APP_LAYER_TX_RESERVED15_FLAG BIT_U64(62)
68
69#define APP_LAYER_TX_RESERVED_FLAGS \
70 (APP_LAYER_TX_RESERVED1_FLAG | APP_LAYER_TX_RESERVED2_FLAG | APP_LAYER_TX_RESERVED3_FLAG | \
71 APP_LAYER_TX_RESERVED4_FLAG | APP_LAYER_TX_RESERVED5_FLAG | \
72 APP_LAYER_TX_RESERVED6_FLAG | APP_LAYER_TX_RESERVED7_FLAG | \
73 APP_LAYER_TX_RESERVED8_FLAG | APP_LAYER_TX_RESERVED9_FLAG | \
74 APP_LAYER_TX_RESERVED10_FLAG | APP_LAYER_TX_RESERVED11_FLAG | \
75 APP_LAYER_TX_RESERVED12_FLAG | APP_LAYER_TX_RESERVED13_FLAG | \
76 APP_LAYER_TX_RESERVED14_FLAG | APP_LAYER_TX_RESERVED15_FLAG)
77
daeb8fd3
VJ
78/** is tx fully inspected? */
79#define APP_LAYER_TX_INSPECTED_FLAG BIT_U64(63)
80/** other 63 bits are for tracking which prefilter engine is already
81 * completely inspected */
ed877849 82#define APP_LAYER_TX_PREFILTER_MASK ~(APP_LAYER_TX_INSPECTED_FLAG | APP_LAYER_TX_RESERVED_FLAGS)
daeb8fd3 83
21e6f1f0
VJ
84/** parser has successfully processed in the input, and has consumed
85 * all of it. */
44d3f264 86#define APP_LAYER_OK (AppLayerResult) { 0, 0, 0 }
21e6f1f0
VJ
87
88/** parser has hit an unrecoverable error. Returning this to the API
89 * leads to no further calls to the parser. */
44d3f264 90#define APP_LAYER_ERROR (AppLayerResult) { -1, 0, 0 }
21e6f1f0
VJ
91
92/** parser needs more data. Through 'c' it will indicate how many
93 * of the input bytes it has consumed. Through 'n' it will indicate
94 * how many more bytes it needs before getting called again.
95 * \note consumed (c) should never be more than the input len
96 * needed (n) + consumed (c) should be more than the input len
97 */
674b8dc0 98#define APP_LAYER_INCOMPLETE(c,n) (AppLayerResult) { 1, (c), (n) }
3bcf948a 99
5908dd08 100int AppLayerParserProtoIsRegistered(uint8_t ipproto, AppProto alproto);
2c857087
VJ
101
102/***** transaction handling *****/
103
429c6388 104int AppLayerParserSetup(void);
6d562f3b 105void AppLayerParserPostStreamSetup(void);
429c6388
AS
106int AppLayerParserDeSetup(void);
107
9634e60e
VJ
108typedef struct AppLayerParserThreadCtx_ AppLayerParserThreadCtx;
109
429c6388
AS
110/**
111 * \brief Gets a new app layer protocol's parser thread context.
112 *
113 * \retval Non-NULL pointer on success.
114 * NULL pointer on failure.
115 */
9634e60e 116AppLayerParserThreadCtx *AppLayerParserThreadCtxAlloc(void);
429c6388
AS
117
118/**
119 * \brief Destroys the app layer parser thread context obtained
fdefb65b 120 * using AppLayerParserThreadCtxAlloc().
429c6388
AS
121 *
122 * \param tctx Pointer to the thread context to be destroyed.
123 */
9634e60e 124void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx);
429c6388
AS
125
126/**
127 * \brief Given a protocol name, checks if the parser is enabled in
128 * the conf file.
129 *
130 * \param alproto_name Name of the app layer protocol.
131 *
132 * \retval 1 If enabled.
133 * \retval 0 If disabled.
d4d18e31 134 */
429c6388
AS
135int AppLayerParserConfParserEnabled(const char *ipproto,
136 const char *alproto_name);
d4d18e31 137
7c8bdfd3 138/** \brief Prototype for parsing functions */
44d3f264 139typedef AppLayerResult (*AppLayerParserFPtr)(Flow *f, void *protocol_state,
7c8bdfd3 140 AppLayerParserState *pstate,
579cc9f0 141 const uint8_t *buf, uint32_t buf_len,
7bc3c3ac 142 void *local_storage, const uint8_t flags);
7c8bdfd3 143
e96d9c11
VJ
144typedef struct AppLayerGetTxIterState {
145 union {
146 void *ptr;
147 uint64_t u64;
148 } un;
149} AppLayerGetTxIterState;
150
151/** \brief tx iterator prototype */
152typedef AppLayerGetTxIterTuple (*AppLayerGetTxIteratorFunc)
153 (const uint8_t ipproto, const AppProto alproto,
154 void *alstate, uint64_t min_tx_id, uint64_t max_tx_id,
155 AppLayerGetTxIterState *state);
156
429c6388 157/***** Parser related registration *****/
d4d18e31
AS
158
159/**
429c6388 160 * \brief Register app layer parser for the protocol.
d4d18e31 161 *
429c6388
AS
162 * \retval 0 On success.
163 * \retval -1 On failure.
d4d18e31 164 */
5cdeadb3 165int AppLayerParserRegisterParser(uint8_t ipproto, AppProto alproto,
429c6388 166 uint8_t direction,
7c8bdfd3 167 AppLayerParserFPtr Parser);
5cdeadb3 168void AppLayerParserRegisterParserAcceptableDataDirection(uint8_t ipproto,
429c6388
AS
169 AppProto alproto,
170 uint8_t direction);
c862bbdc 171void AppLayerParserRegisterOptionFlags(uint8_t ipproto, AppProto alproto,
c8fb9bcb 172 uint32_t flags);
5cdeadb3 173void AppLayerParserRegisterStateFuncs(uint8_t ipproto, AppProto alproto,
547d6c2d 174 void *(*StateAlloc)(void *, AppProto), void (*StateFree)(void *));
5cdeadb3 175void AppLayerParserRegisterLocalStorageFunc(uint8_t ipproto, AppProto proto,
429c6388
AS
176 void *(*LocalStorageAlloc)(void),
177 void (*LocalStorageFree)(void *));
5cdeadb3 178void AppLayerParserRegisterGetFilesFunc(uint8_t ipproto, AppProto alproto,
429c6388 179 FileContainer *(*StateGetFiles)(void *, uint8_t));
7732efbe
JI
180// void AppLayerParserRegisterGetEventsFunc(uint8_t ipproto, AppProto proto,
181// AppLayerDecoderEvents *(*StateGetEvents)(void *) __attribute__((nonnull)));
f3599323 182void AppLayerParserRegisterLoggerFuncs(uint8_t ipproto, AppProto alproto,
bca0cd71
VJ
183 LoggerId (*StateGetTxLogged)(void *, void *),
184 void (*StateSetTxLogged)(void *, void *, LoggerId));
5cdeadb3 185void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto);
01724f04 186void AppLayerParserRegisterLoggerBits(uint8_t ipproto, AppProto alproto, LoggerId bits);
5cdeadb3 187void AppLayerParserRegisterTruncateFunc(uint8_t ipproto, AppProto alproto,
429c6388 188 void (*Truncate)(void *, uint8_t));
5cdeadb3 189void AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto,
429c6388 190 int (*StateGetStateProgress)(void *alstate, uint8_t direction));
5cdeadb3 191void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto,
429c6388 192 void (*StateTransactionFree)(void *, uint64_t));
5cdeadb3 193void AppLayerParserRegisterGetTxCnt(uint8_t ipproto, AppProto alproto,
429c6388 194 uint64_t (*StateGetTxCnt)(void *alstate));
5cdeadb3 195void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto,
429c6388 196 void *(StateGetTx)(void *alstate, uint64_t tx_id));
e96d9c11
VJ
197void AppLayerParserRegisterGetTxIterator(uint8_t ipproto, AppProto alproto,
198 AppLayerGetTxIteratorFunc Func);
efc9a7a3
VJ
199void AppLayerParserRegisterStateProgressCompletionStatus(
200 AppProto alproto, const int ts, const int tc);
5cdeadb3 201void AppLayerParserRegisterGetEventInfo(uint8_t ipproto, AppProto alproto,
429c6388
AS
202 int (*StateGetEventInfo)(const char *event_name, int *event_id,
203 AppLayerEventType *event_type));
50e23ba9
JL
204void AppLayerParserRegisterGetEventInfoById(uint8_t ipproto, AppProto alproto,
205 int (*StateGetEventInfoById)(int event_id, const char **event_name,
206 AppLayerEventType *event_type));
b160c49e
GL
207void AppLayerParserRegisterGetStreamDepth(uint8_t ipproto,
208 AppProto alproto,
209 uint32_t (*GetStreamDepth)(void));
ed5a439b
GL
210void AppLayerParserRegisterSetStreamDepthFlag(uint8_t ipproto, AppProto alproto,
211 void (*SetStreamDepthFlag)(void *tx, uint8_t flags));
d4d18e31 212
411f428a
VJ
213void AppLayerParserRegisterTxDataFunc(uint8_t ipproto, AppProto alproto,
214 AppLayerTxData *(*GetTxData)(void *tx));
5665fc83
VJ
215void AppLayerParserRegisterApplyTxConfigFunc(uint8_t ipproto, AppProto alproto,
216 bool (*ApplyTxConfig)(void *state, void *tx, int mode, AppLayerTxConfig));
411f428a 217
429c6388 218/***** Get and transaction functions *****/
16cfae2f 219
ac3cf6ff 220uint32_t AppLayerParserGetOptionFlags(uint8_t protomap, AppProto alproto);
e96d9c11
VJ
221AppLayerGetTxIteratorFunc AppLayerGetTxIterator(const uint8_t ipproto,
222 const AppProto alproto);
223
5cdeadb3
VJ
224void *AppLayerParserGetProtocolParserLocalStorage(uint8_t ipproto, AppProto alproto);
225void AppLayerParserDestroyProtocolParserLocalStorage(uint8_t ipproto, AppProto alproto,
429c6388 226 void *local_data);
6cb00142 227
6cb00142 228
9634e60e 229uint64_t AppLayerParserGetTransactionLogId(AppLayerParserState *pstate);
e9fccfa6 230void AppLayerParserSetTransactionLogId(AppLayerParserState *pstate, uint64_t tx_id);
5c01b409 231
9634e60e 232uint64_t AppLayerParserGetTransactionInspectId(AppLayerParserState *pstate, uint8_t direction);
5c01b409 233void AppLayerParserSetTransactionInspectId(const Flow *f, AppLayerParserState *pstate,
af51e0f5 234 void *alstate, const uint8_t flags, bool tag_txs_as_inspected);
5c01b409 235
9634e60e
VJ
236AppLayerDecoderEvents *AppLayerParserGetDecoderEvents(AppLayerParserState *pstate);
237void AppLayerParserSetDecoderEvents(AppLayerParserState *pstate, AppLayerDecoderEvents *devents);
d568e7fa 238AppLayerDecoderEvents *AppLayerParserGetEventsByTx(uint8_t ipproto, AppProto alproto, void *tx);
a4a4d17a 239FileContainer *AppLayerParserGetFiles(const Flow *f, const uint8_t direction);
5cdeadb3 240int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto,
429c6388 241 void *alstate, uint8_t direction);
5c01b409 242uint64_t AppLayerParserGetTxCnt(const Flow *, void *alstate);
5cdeadb3 243void *AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id);
c4b918b6 244int AppLayerParserGetStateProgressCompletionStatus(AppProto alproto, uint8_t direction);
5cdeadb3 245int AppLayerParserGetEventInfo(uint8_t ipproto, AppProto alproto, const char *event_name,
429c6388 246 int *event_id, AppLayerEventType *event_type);
50e23ba9
JL
247int AppLayerParserGetEventInfoById(uint8_t ipproto, AppProto alproto, int event_id,
248 const char **event_name, AppLayerEventType *event_type);
6cb00142 249
3148ff34 250uint64_t AppLayerParserGetTransactionActive(const Flow *f, AppLayerParserState *pstate, uint8_t direction);
6cb00142 251
f5f14880 252uint8_t AppLayerParserGetFirstDataDir(uint8_t ipproto, AppProto alproto);
ddde572f 253
bcfa484b 254int AppLayerParserSupportsFiles(uint8_t ipproto, AppProto alproto);
1cf02560 255
411f428a 256AppLayerTxData *AppLayerParserGetTxData(uint8_t ipproto, AppProto alproto, void *tx);
5665fc83
VJ
257void AppLayerParserApplyTxConfig(uint8_t ipproto, AppProto alproto,
258 void *state, void *tx, enum ConfigAction mode, AppLayerTxConfig);
411f428a 259
429c6388
AS
260/***** General *****/
261
675fa564 262int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *tctx, Flow *f, AppProto alproto,
579cc9f0 263 uint8_t flags, const uint8_t *input, uint32_t input_len);
9634e60e 264void AppLayerParserSetEOF(AppLayerParserState *pstate);
af51e0f5 265bool AppLayerParserHasDecoderEvents(AppLayerParserState *pstate);
078ff0c0 266int AppLayerParserProtocolHasLogger(uint8_t ipproto, AppProto alproto);
bca0cd71 267LoggerId AppLayerParserProtocolGetLoggerBits(uint8_t ipproto, AppProto alproto);
2d223b69 268void AppLayerParserTriggerRawStreamReassembly(Flow *f, int direction);
b160c49e 269void AppLayerParserSetStreamDepth(uint8_t ipproto, AppProto alproto, uint32_t stream_depth);
3148ff34 270uint32_t AppLayerParserGetStreamDepth(const Flow *f);
ed5a439b 271void AppLayerParserSetStreamDepthFlag(uint8_t ipproto, AppProto alproto, void *state, uint64_t tx_id, uint8_t flags);
d369e54f 272int AppLayerParserIsEnabled(AppProto alproto);
429c6388
AS
273
274/***** Cleanup *****/
275
21e74179
PA
276void AppLayerParserStateProtoCleanup(
277 uint8_t protomap, AppProto alproto, void *alstate, AppLayerParserState *pstate);
3148ff34 278void AppLayerParserStateCleanup(const Flow *f, void *alstate, AppLayerParserState *pstate);
429c6388
AS
279
280void AppLayerParserRegisterProtocolParsers(void);
281
282
9634e60e
VJ
283void AppLayerParserStateSetFlag(AppLayerParserState *pstate, uint8_t flag);
284int AppLayerParserStateIssetFlag(AppLayerParserState *pstate, uint8_t flag);
429c6388 285
5cdeadb3 286void AppLayerParserStreamTruncated(uint8_t ipproto, AppProto alproto, void *alstate,
429c6388
AS
287 uint8_t direction);
288
289
290
9634e60e
VJ
291AppLayerParserState *AppLayerParserStateAlloc(void);
292void AppLayerParserStateFree(AppLayerParserState *pstate);
429c6388 293
7a96d18f 294void AppLayerParserTransactionsCleanup(Flow *f);
429c6388
AS
295
296#ifdef DEBUG
9634e60e 297void AppLayerParserStatePrintDetails(AppLayerParserState *pstate);
429c6388 298#endif
6cb00142 299
077ac816 300
6cb00142
AS
301/***** Unittests *****/
302
429c6388 303#ifdef UNITTESTS
5cdeadb3 304void AppLayerParserRegisterProtocolUnittests(uint8_t ipproto, AppProto alproto,
429c6388
AS
305 void (*RegisterUnittests)(void));
306void AppLayerParserRegisterUnittests(void);
307void AppLayerParserBackupParserTable(void);
308void AppLayerParserRestoreParserTable(void);
37203c98 309void UTHAppLayerParserStateGetIds(void *ptr, uint64_t *i1, uint64_t *i2, uint64_t *log, uint64_t *min);
429c6388 310#endif
6cb00142 311
59327e0f 312#endif /* __APP_LAYER_PARSER_H__ */