suricata
stream-tcp.h
Go to the documentation of this file.
1/* Copyright (C) 2007-2025 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 Victor Julien <victor@inliniac.net>
22 * \author Gurvinder Singh <gurvindersinghdahiya@gmail.com>
23 */
24
25#ifndef SURICATA_STREAM_TCP_H
26#define SURICATA_STREAM_TCP_H
27
28#include "stream-tcp-private.h"
29
30#include "stream.h"
32#include "suricata.h"
34
35#define STREAM_VERBOSE false
36/* Flag to indicate that the checksum validation for the stream engine
37 has been enabled */
38#define STREAMTCP_INIT_FLAG_CHECKSUM_VALIDATION BIT_U8(0)
39#define STREAMTCP_INIT_FLAG_DROP_INVALID BIT_U8(1)
40#define STREAMTCP_INIT_FLAG_BYPASS BIT_U8(2)
41#define STREAMTCP_INIT_FLAG_INLINE BIT_U8(3)
42/** flag to drop packets with URG flag set */
43#define STREAMTCP_INIT_FLAG_DROP_URG BIT_U8(4)
44
46 TCP_STREAM_URGENT_INLINE, /**< treat as inline data */
47#define TCP_STREAM_URGENT_DEFAULT TCP_STREAM_URGENT_INLINE
48 TCP_STREAM_URGENT_DROP, /**< drop TCP packet with URG flag */
49 TCP_STREAM_URGENT_OOB, /**< treat 1 byte of URG data as OOB */
50 TCP_STREAM_URGENT_GAP, /**< treat 1 byte of URG data as GAP */
51};
52
53/*global flow data*/
54typedef struct TcpStreamCnf_ {
55 /** stream tracking
56 *
57 * max stream mem usage
58 */
59 SC_ATOMIC_DECLARE(uint64_t, memcap);
60 SC_ATOMIC_DECLARE(uint64_t, reassembly_memcap); /**< max memory usage for stream reassembly */
61
62 uint16_t stream_init_flags; /**< new stream flags will be initialized to this */
63
64 /* coccinelle: TcpStreamCnf:flags:STREAMTCP_INIT_ */
65 uint8_t flags;
67
68 uint32_t prealloc_sessions; /**< ssns to prealloc per stream thread */
69 uint32_t prealloc_segments; /**< segments to prealloc per stream thread */
74
75 uint32_t reassembly_depth; /**< Depth until when we reassemble the stream */
76
79
85
86 /* default to "LINUX" timestamp behavior if true*/
88
91
92typedef struct StreamTcpThread_ {
94
97 /** sessions not picked up because memcap was reached */
101 /** exception policy */
103 /** pseudo packets processed */
105 /** packets rejected because their csum is invalid */
107 /** midstream pickups */
109 /** exception policy stats */
111 /** wrong thread */
113 /** ack for unseen data */
115
116 /** tcp reassembly thread data */
119
121void StreamTcpInitConfig(bool);
122void StreamTcpFreeConfig(bool);
123void StreamTcpRegisterTests (void);
124
126
127void StreamTcpInitMemuse(void);
128void StreamTcpIncrMemuse(uint64_t);
129void StreamTcpDecrMemuse(uint64_t);
130int StreamTcpSetMemcap(uint64_t);
131uint64_t StreamTcpGetMemcap(void);
132int StreamTcpCheckMemcap(uint64_t);
133uint64_t StreamTcpMemuseCounter(void);
134
135int StreamTcpSegmentForEach(const Packet *p, uint8_t flag,
136 StreamSegmentCallback CallbackFunc,
137 void *data);
139 const Packet *p, uint8_t flag, StreamSegmentCallback CallbackFunc, void *data);
141void TcpSessionSetReassemblyDepth(TcpSession *ssn, uint32_t size);
142
144 void *data, const uint8_t *input, const uint32_t input_len, const uint64_t offset);
145
147 void *cb_data, const uint64_t offset, const bool eof);
148int StreamReassembleLog(const TcpSession *ssn, const TcpStream *stream,
149 StreamReassembleRawFunc Callback, void *cb_data, const uint64_t progress_in,
150 uint64_t *progress_out, const bool eof);
151int StreamReassembleRaw(TcpSession *ssn, const Packet *p,
152 StreamReassembleRawFunc Callback, void *cb_data,
153 uint64_t *progress_out, bool respect_inspect_depth);
154void StreamReassembleRawUpdateProgress(TcpSession *ssn, Packet *p, const uint64_t progress);
155
157
158const char *StreamTcpStateAsString(const enum TcpState);
159const char *StreamTcpSsnStateAsString(const TcpSession *ssn);
160
164
165/** ------- Inline functions: ------ */
166
167/**
168 * \brief If we are on IPS mode, and got a drop action triggered from
169 * the IP only module, or from a reassembled msg and/or from an
170 * applayer detection, then drop the rest of the packets of the
171 * same stream and avoid inspecting it any further
172 * \param p pointer to the Packet to check
173 * \retval 1 if we must drop this stream
174 * \retval 0 if the stream still legal
175 */
176static inline int StreamTcpCheckFlowDrops(Packet *p)
177{
178 /* If we are on IPS mode, and got a drop action triggered from
179 * the IP only module, or from a reassembled msg and/or from an
180 * applayer detection, then drop the rest of the packets of the
181 * same stream and avoid inspecting it any further */
183 return 1;
184
185 return 0;
186}
187
188enum {
189 /* stream has no segments for forced reassembly, nor for detection */
191 /* stream has no segments for forced reassembly, but only segments that
192 * have been sent for detection, but are stuck in the detection queues */
194};
195
197uint8_t StreamNeedsReassembly(const TcpSession *ssn, uint8_t direction);
198TmEcode StreamTcpThreadInit(ThreadVars *, void *, void **);
200
203/* clear ssn and return to pool */
204void StreamTcpSessionClear(void *ssnptr);
205/* cleanup ssn, but don't free ssn */
207/* cleanup stream, but don't free the stream */
209/* check if bypass is enabled */
210int StreamTcpBypassEnabled(void);
211bool StreamTcpInlineMode(void);
212
213bool TcpSessionPacketSsnReuse(const Packet *p, const Flow *f, const void *tcp_ssn);
214
215void StreamTcpUpdateAppLayerProgress(TcpSession *ssn, char direction,
216 const uint32_t progress);
217
218uint64_t StreamTcpGetUsable(const TcpStream *stream, const bool eof);
219uint64_t StreamDataRightEdge(const TcpStream *stream, const bool eof);
220
221#endif /* SURICATA_STREAM_TCP_H */
#define FLOW_ACTION_DROP
Definition flow.h:70
ThreadVars * tv
TmEcode StreamTcp(ThreadVars *, Packet *, void *, PacketQueueNoLock *)
void StreamTcpInitMemuse(void)
Definition stream-tcp.c:223
int StreamTcpBypassEnabled(void)
TmEcode StreamTcpThreadDeinit(ThreadVars *tv, void *data)
void StreamTcpSessionCleanup(TcpSession *ssn)
Session cleanup function. Does not free the ssn.
Definition stream-tcp.c:327
int StreamTcpSetMemcap(uint64_t)
Update memcap value.
Definition stream-tcp.c:279
int StreamReassembleRaw(TcpSession *ssn, const Packet *p, StreamReassembleRawFunc Callback, void *cb_data, uint64_t *progress_out, bool respect_inspect_depth)
void StreamTcpReassembleConfigEnableOverlapCheck(void)
int StreamTcpSegmentForSession(const Packet *p, uint8_t flag, StreamSegmentCallback CallbackFunc, void *data)
Run callback function on each TCP segment in both directions of a session.
bool StreamTcpInlineMode(void)
See if stream engine is operating in inline mode.
void StreamTcpRegisterTests(void)
struct StreamTcpThread_ StreamTcpThread
void StreamTcpSessionPktFree(Packet *)
Function to return the stream segments back to the pool.
Definition stream-tcp.c:380
void StreamTcpFreeConfig(bool)
Definition stream-tcp.c:859
TcpStreamUrgentHandling
Definition stream-tcp.h:45
@ TCP_STREAM_URGENT_INLINE
Definition stream-tcp.h:46
@ TCP_STREAM_URGENT_DROP
Definition stream-tcp.h:48
@ TCP_STREAM_URGENT_OOB
Definition stream-tcp.h:49
@ TCP_STREAM_URGENT_GAP
Definition stream-tcp.h:50
bool TcpSessionPacketSsnReuse(const Packet *p, const Flow *f, const void *tcp_ssn)
void StreamTcpDecrMemuse(uint64_t)
Definition stream-tcp.c:234
uint64_t StreamTcpGetUsable(const TcpStream *stream, const bool eof)
void StreamTcpStreamCleanup(TcpStream *stream)
Definition stream-tcp.c:300
void StreamReassembleRawUpdateProgress(TcpSession *ssn, Packet *p, const uint64_t progress)
update stream engine after detection
const char * StreamTcpStateAsString(const enum TcpState)
void StreamTcpIncrMemuse(uint64_t)
Definition stream-tcp.c:228
enum ExceptionPolicy StreamTcpSsnMemcapGetExceptionPolicy(void)
Definition stream-tcp.c:899
int StreamTcpSegmentForEach(const Packet *p, uint8_t flag, StreamSegmentCallback CallbackFunc, void *data)
struct TcpStreamCnf_ TcpStreamCnf
enum ExceptionPolicy StreamTcpReassemblyMemcapGetExceptionPolicy(void)
Definition stream-tcp.c:904
TmEcode StreamTcpThreadInit(ThreadVars *, void *, void **)
void TcpSessionSetReassemblyDepth(TcpSession *ssn, uint32_t size)
uint64_t StreamTcpGetMemcap(void)
Return memcap value.
Definition stream-tcp.c:294
@ STREAM_HAS_UNPROCESSED_SEGMENTS_NEED_ONLY_DETECTION
Definition stream-tcp.h:193
@ STREAM_HAS_UNPROCESSED_SEGMENTS_NONE
Definition stream-tcp.h:190
int(* StreamReassembleRawFunc)(void *data, const uint8_t *input, const uint32_t input_len, const uint64_t offset)
Definition stream-tcp.h:143
int StreamReassembleLog(const TcpSession *ssn, const TcpStream *stream, StreamReassembleRawFunc Callback, void *cb_data, const uint64_t progress_in, uint64_t *progress_out, const bool eof)
uint64_t StreamDataRightEdge(const TcpStream *stream, const bool eof)
TcpStreamCnf stream_config
Definition stream-tcp.c:219
int StreamTcpPacket(ThreadVars *tv, Packet *p, StreamTcpThread *stt, PacketQueueNoLock *pq)
void StreamTcpInitConfig(bool)
To initialize the stream global configuration data.
Definition stream-tcp.c:488
void StreamTcpDetectLogFlush(ThreadVars *tv, StreamTcpThread *stt, Flow *f, Packet *p, PacketQueueNoLock *pq)
create packets in both directions to flush out logging and detection before switching protocols....
const char * StreamTcpSsnStateAsString(const TcpSession *ssn)
void StreamTcpSessionClear(void *ssnptr)
Function to return the stream back to the pool. It returns the segments in the stream to the segment ...
Definition stream-tcp.c:351
int StreamTcpCheckMemcap(uint64_t)
Check if alloc'ing "size" would mean we're over memcap.
Definition stream-tcp.c:266
uint64_t StreamTcpMemuseCounter(void)
Definition stream-tcp.c:254
enum ExceptionPolicy StreamMidstreamGetExceptionPolicy(void)
Definition stream-tcp.c:909
uint8_t StreamNeedsReassembly(const TcpSession *ssn, uint8_t direction)
see what if any work the TCP session still needs
void StreamTcpUpdateAppLayerProgress(TcpSession *ssn, char direction, const uint32_t progress)
update reassembly progress
int StreamReassembleForFrame(TcpSession *ssn, TcpStream *stream, StreamReassembleRawFunc Callback, void *cb_data, const uint64_t offset, const bool eof)
int(* StreamSegmentCallback)(const Packet *, TcpSegment *, void *, const uint8_t *, uint32_t)
Definition stream.h:36
Flow data structure.
Definition flow.h:356
uint32_t flags
Definition flow.h:421
simple fifo queue for packets
struct Flow_ * flow
Definition decode.h:546
uint16_t counter_tcp_ack_unseen_data
Definition stream-tcp.h:114
uint16_t counter_tcp_ssn_from_pool
Definition stream-tcp.h:100
uint16_t counter_tcp_invalid_checksum
Definition stream-tcp.h:106
uint16_t counter_tcp_pseudo
Definition stream-tcp.h:104
uint16_t counter_tcp_ssn_memcap
Definition stream-tcp.h:98
uint16_t counter_tcp_active_sessions
Definition stream-tcp.h:95
uint16_t counter_tcp_sessions
Definition stream-tcp.h:96
TcpReassemblyThreadCtx * ra_ctx
Definition stream-tcp.h:117
uint16_t counter_tcp_midstream_pickups
Definition stream-tcp.h:108
ExceptionPolicyCounters counter_tcp_midstream_eps
Definition stream-tcp.h:110
uint16_t counter_tcp_ssn_from_cache
Definition stream-tcp.h:99
ExceptionPolicyCounters counter_tcp_ssn_memcap_eps
Definition stream-tcp.h:102
uint16_t counter_tcp_wrong_thread
Definition stream-tcp.h:112
bool liberal_timestamps
Definition stream-tcp.h:87
bool async_oneside
Definition stream-tcp.h:71
uint32_t prealloc_sessions
Definition stream-tcp.h:68
enum ExceptionPolicy reassembly_memcap_policy
Definition stream-tcp.h:81
SC_ATOMIC_DECLARE(uint64_t, reassembly_memcap)
enum ExceptionPolicy midstream_policy
Definition stream-tcp.h:82
uint16_t reassembly_toserver_chunk_size
Definition stream-tcp.h:77
uint32_t reassembly_depth
Definition stream-tcp.h:75
uint16_t reassembly_toclient_chunk_size
Definition stream-tcp.h:78
uint8_t max_synack_queued
Definition stream-tcp.h:66
bool streaming_log_api
Definition stream-tcp.h:72
enum TcpStreamUrgentHandling urgent_policy
Definition stream-tcp.h:83
SC_ATOMIC_DECLARE(uint64_t, memcap)
uint8_t flags
Definition stream-tcp.h:65
uint8_t max_syn_queued
Definition stream-tcp.h:73
StreamingBufferConfig sbcnf
Definition stream-tcp.h:89
enum TcpStreamUrgentHandling urgent_oob_limit_policy
Definition stream-tcp.h:84
uint32_t prealloc_segments
Definition stream-tcp.h:69
uint16_t stream_init_flags
Definition stream-tcp.h:62
enum ExceptionPolicy ssn_memcap_policy
Definition stream-tcp.h:80
Per thread variable structure.
Definition threadvars.h:58
int EngineModeIsIPS(void)
Definition suricata.c:242
uint64_t offset