From 01e64d80dae179d445d17431a390e84098287b90 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Sat, 5 Feb 2022 08:53:58 +0100 Subject: [PATCH] app-layer: trunc parser per direction --- rust/src/applayer.rs | 2 ++ src/app-layer-parser.c | 22 +++++++++++++++------- src/app-layer-parser.h | 5 ++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/rust/src/applayer.rs b/rust/src/applayer.rs index f44b5b63b9..fa275f9682 100644 --- a/rust/src/applayer.rs +++ b/rust/src/applayer.rs @@ -430,6 +430,8 @@ pub const APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD : u16 = BIT_U16!(3); pub const APP_LAYER_PARSER_BYPASS_READY : u16 = BIT_U16!(4); pub const APP_LAYER_PARSER_EOF_TS : u16 = BIT_U16!(5); pub const APP_LAYER_PARSER_EOF_TC : u16 = BIT_U16!(6); +pub const APP_LAYER_PARSER_TRUNC_TS : u16 = BIT_U16!(7); +pub const APP_LAYER_PARSER_TRUNC_TC : u16 = BIT_U16!(8); pub const APP_LAYER_PARSER_OPT_ACCEPT_GAPS: u32 = BIT_U32!(0); pub const APP_LAYER_PARSER_OPT_UNIDIR_TXS: u32 = BIT_U32!(1); diff --git a/src/app-layer-parser.c b/src/app-layer-parser.c index 3bc32d1e06..550a658a27 100644 --- a/src/app-layer-parser.c +++ b/src/app-layer-parser.c @@ -226,6 +226,9 @@ FramesContainer *AppLayerFramesSetupContainer(Flow *f) return f->alparser->frames; } +static inline void AppLayerParserStreamTruncated(AppLayerParserState *pstate, const uint8_t ipproto, + const AppProto alproto, void *alstate, const uint8_t direction); + #ifdef UNITTESTS void UTHAppLayerParserStateGetIds(void *ptr, uint64_t *i1, uint64_t *i2, uint64_t *log, uint64_t *min) { @@ -1290,8 +1293,7 @@ int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow if (!(p->option_flags & APP_LAYER_PARSER_OPT_ACCEPT_GAPS)) { SCLogDebug("app-layer parser does not accept gaps"); if (f->alstate != NULL && !FlowChangeProto(f)) { - AppLayerParserStreamTruncated(f->proto, alproto, f->alstate, - flags); + AppLayerParserStreamTruncated(pstate, f->proto, alproto, f->alstate, flags); } AppLayerIncGapErrorCounter(tv, f); goto error; @@ -1437,7 +1439,7 @@ int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow /* stream truncated, inform app layer */ if (flags & STREAM_DEPTH) - AppLayerParserStreamTruncated(f->proto, alproto, alstate, flags); + AppLayerParserStreamTruncated(pstate, f->proto, alproto, f->alstate, flags); end: /* update app progress */ @@ -1755,14 +1757,20 @@ uint16_t AppLayerParserStateIssetFlag(AppLayerParserState *pstate, uint16_t flag SCReturnUInt(pstate->flags & flag); } - -void AppLayerParserStreamTruncated(uint8_t ipproto, AppProto alproto, void *alstate, - uint8_t direction) +static inline void AppLayerParserStreamTruncated(AppLayerParserState *pstate, const uint8_t ipproto, + const AppProto alproto, void *alstate, const uint8_t direction) { SCEnter(); - if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].Truncate != NULL) + if (direction & STREAM_TOSERVER) { + AppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_TRUNC_TS); + } else { + AppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_TRUNC_TC); + } + + if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].Truncate != NULL) { alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].Truncate(alstate, direction); + } SCReturn; } diff --git a/src/app-layer-parser.h b/src/app-layer-parser.h index 21565af316..c1ef033940 100644 --- a/src/app-layer-parser.h +++ b/src/app-layer-parser.h @@ -38,6 +38,8 @@ #define APP_LAYER_PARSER_BYPASS_READY BIT_U16(4) #define APP_LAYER_PARSER_EOF_TS BIT_U16(5) #define APP_LAYER_PARSER_EOF_TC BIT_U16(6) +#define APP_LAYER_PARSER_TRUNC_TS BIT_U16(7) +#define APP_LAYER_PARSER_TRUNC_TC BIT_U16(8) /* Flags for AppLayerParserProtoCtx. */ #define APP_LAYER_PARSER_OPT_ACCEPT_GAPS BIT_U32(0) @@ -289,9 +291,6 @@ void AppLayerParserRegisterProtocolParsers(void); void AppLayerParserStateSetFlag(AppLayerParserState *pstate, uint16_t flag); uint16_t AppLayerParserStateIssetFlag(AppLayerParserState *pstate, uint16_t flag); -void AppLayerParserStreamTruncated(uint8_t ipproto, AppProto alproto, void *alstate, - uint8_t direction); - AppLayerParserState *AppLayerParserStateAlloc(void); void AppLayerParserStateFree(AppLayerParserState *pstate);