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"
31 #include "stream-tcp-reassemble.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  TCP_STREAM_URGENT_DROP, /**< drop TCP packet with URG flag */
48  TCP_STREAM_URGENT_OOB, /**< treat 1 byte of URG data as OOB */
49  TCP_STREAM_URGENT_GAP, /**< treat 1 byte of URG data as GAP */
50 };
51 
52 /*global flow data*/
53 typedef struct TcpStreamCnf_ {
54  /** stream tracking
55  *
56  * max stream mem usage
57  */
58  SC_ATOMIC_DECLARE(uint64_t, memcap);
59  SC_ATOMIC_DECLARE(uint64_t, reassembly_memcap); /**< max memory usage for stream reassembly */
60 
61  uint16_t stream_init_flags; /**< new stream flags will be initialized to this */
62 
63  /* coccinelle: TcpStreamCnf:flags:STREAMTCP_INIT_ */
64  uint8_t flags;
66 
67  uint32_t prealloc_sessions; /**< ssns to prealloc per stream thread */
68  uint32_t prealloc_segments; /**< segments to prealloc per stream thread */
69  bool midstream;
72  uint8_t max_syn_queued;
73 
74  uint32_t reassembly_depth; /**< Depth until when we reassemble the stream */
75 
78 
84 
85  /* default to "LINUX" timestamp behavior if true*/
87 
90 
91 typedef struct StreamTcpThread_ {
93 
96  /** sessions not picked up because memcap was reached */
100  /** exception policy */
102  /** pseudo packets processed */
104  /** packets rejected because their csum is invalid */
106  /** midstream pickups */
108  /** exception policy stats */
110  /** wrong thread */
112  /** ack for unseen data */
114 
115  /** tcp reassembly thread data */
118 
120 void StreamTcpInitConfig(bool);
121 void StreamTcpFreeConfig(bool);
122 void StreamTcpRegisterTests (void);
123 
125 
126 void StreamTcpInitMemuse(void);
127 void StreamTcpIncrMemuse(uint64_t);
128 void StreamTcpDecrMemuse(uint64_t);
129 int StreamTcpSetMemcap(uint64_t);
130 uint64_t StreamTcpGetMemcap(void);
131 int StreamTcpCheckMemcap(uint64_t);
132 uint64_t StreamTcpMemuseCounter(void);
133 
134 int StreamTcpSegmentForEach(const Packet *p, uint8_t flag,
135  StreamSegmentCallback CallbackFunc,
136  void *data);
138  const Packet *p, uint8_t flag, StreamSegmentCallback CallbackFunc, void *data);
140 void TcpSessionSetReassemblyDepth(TcpSession *ssn, uint32_t size);
141 
142 typedef int (*StreamReassembleRawFunc)(
143  void *data, const uint8_t *input, const uint32_t input_len, const uint64_t offset);
144 
146  void *cb_data, const uint64_t offset, const bool eof);
147 int StreamReassembleLog(const TcpSession *ssn, const TcpStream *stream,
148  StreamReassembleRawFunc Callback, void *cb_data, const uint64_t progress_in,
149  uint64_t *progress_out, const bool eof);
151  StreamReassembleRawFunc Callback, void *cb_data,
152  uint64_t *progress_out, bool respect_inspect_depth);
153 void StreamReassembleRawUpdateProgress(TcpSession *ssn, Packet *p, const uint64_t progress);
154 
156 
157 const char *StreamTcpStateAsString(const enum TcpState);
158 
162 
163 /** ------- Inline functions: ------ */
164 
165 /**
166  * \brief If we are on IPS mode, and got a drop action triggered from
167  * the IP only module, or from a reassembled msg and/or from an
168  * applayer detection, then drop the rest of the packets of the
169  * same stream and avoid inspecting it any further
170  * \param p pointer to the Packet to check
171  * \retval 1 if we must drop this stream
172  * \retval 0 if the stream still legal
173  */
174 static inline int StreamTcpCheckFlowDrops(Packet *p)
175 {
176  /* If we are on IPS mode, and got a drop action triggered from
177  * the IP only module, or from a reassembled msg and/or from an
178  * applayer detection, then drop the rest of the packets of the
179  * same stream and avoid inspecting it any further */
181  return 1;
182 
183  return 0;
184 }
185 
186 enum {
187  /* stream has no segments for forced reassembly, nor for detection */
189  /* stream has no segments for forced reassembly, but only segments that
190  * have been sent for detection, but are stuck in the detection queues */
192 };
193 
195 uint8_t StreamNeedsReassembly(const TcpSession *ssn, uint8_t direction);
196 TmEcode StreamTcpThreadInit(ThreadVars *, void *, void **);
198 
200  PacketQueueNoLock *pq);
201 /* clear ssn and return to pool */
202 void StreamTcpSessionClear(void *ssnptr);
203 /* cleanup ssn, but don't free ssn */
205 /* cleanup stream, but don't free the stream */
206 void StreamTcpStreamCleanup(TcpStream *stream);
207 /* check if bypass is enabled */
208 int StreamTcpBypassEnabled(void);
209 bool StreamTcpInlineMode(void);
210 
211 bool TcpSessionPacketSsnReuse(const Packet *p, const Flow *f, const void *tcp_ssn);
212 
213 void StreamTcpUpdateAppLayerProgress(TcpSession *ssn, char direction,
214  const uint32_t progress);
215 
216 uint64_t StreamTcpGetUsable(const TcpStream *stream, const bool eof);
217 uint64_t StreamDataRightEdge(const TcpStream *stream, const bool eof);
218 
219 #endif /* SURICATA_STREAM_TCP_H */
StreamReassembleRawFunc
int(* StreamReassembleRawFunc)(void *data, const uint8_t *input, const uint32_t input_len, const uint64_t offset)
Definition: stream-tcp.h:142
StreamMidstreamGetExceptionPolicy
enum ExceptionPolicy StreamMidstreamGetExceptionPolicy(void)
Definition: stream-tcp.c:914
StreamSegmentCallback
int(* StreamSegmentCallback)(const Packet *, TcpSegment *, void *, const uint8_t *, uint32_t)
Definition: stream.h:36
TcpStream_
Definition: stream-tcp-private.h:106
TcpStreamCnf_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, memcap)
StreamTcpReassemblyMemcapGetExceptionPolicy
enum ExceptionPolicy StreamTcpReassemblyMemcapGetExceptionPolicy(void)
Definition: stream-tcp.c:909
StreamTcpPacket
int StreamTcpPacket(ThreadVars *tv, Packet *p, StreamTcpThread *stt, PacketQueueNoLock *pq)
Definition: stream-tcp.c:5703
Flow_::flags
uint64_t flags
Definition: flow.h:410
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
StreamTcpThread_::counter_tcp_ssn_from_pool
StatsCounterId counter_tcp_ssn_from_pool
Definition: stream-tcp.h:99
TcpStreamCnf
struct TcpStreamCnf_ TcpStreamCnf
StreamTcpSegmentForEach
int StreamTcpSegmentForEach(const Packet *p, uint8_t flag, StreamSegmentCallback CallbackFunc, void *data)
Definition: stream-tcp.c:7148
TCP_STREAM_URGENT_OOB
@ TCP_STREAM_URGENT_OOB
Definition: stream-tcp.h:48
StreamTcpDecrMemuse
void StreamTcpDecrMemuse(uint64_t)
Definition: stream-tcp.c:242
StreamTcpCheckMemcap
int StreamTcpCheckMemcap(uint64_t)
Check if alloc'ing "size" would mean we're over memcap.
Definition: stream-tcp.c:274
StreamTcpReassembleConfigEnableOverlapCheck
void StreamTcpReassembleConfigEnableOverlapCheck(void)
Definition: stream-tcp-list.c:40
StreamTcpThread_
Definition: stream-tcp.h:91
ssn
TcpSession ssn
Definition: fuzz_dataset.c:31
TcpStreamCnf_::async_oneside
bool async_oneside
Definition: stream-tcp.h:70
TcpStreamCnf_::reassembly_depth
uint32_t reassembly_depth
Definition: stream-tcp.h:74
Flow_
Flow data structure.
Definition: flow.h:360
TcpStreamCnf_::flags
uint8_t flags
Definition: stream-tcp.h:64
StreamReassembleRaw
int StreamReassembleRaw(TcpSession *ssn, const Packet *p, StreamReassembleRawFunc Callback, void *cb_data, uint64_t *progress_out, bool respect_inspect_depth)
Definition: stream-tcp-reassemble.c:1917
TcpStreamCnf_::sbcnf
StreamingBufferConfig sbcnf
Definition: stream-tcp.h:88
StreamTcpMemuseCounter
uint64_t StreamTcpMemuseCounter(void)
Definition: stream-tcp.c:262
util-exception-policy-types.h
StreamTcpSsnMemcapGetExceptionPolicy
enum ExceptionPolicy StreamTcpSsnMemcapGetExceptionPolicy(void)
Definition: stream-tcp.c:904
StreamTcpGetMemcap
uint64_t StreamTcpGetMemcap(void)
Return memcap value.
Definition: stream-tcp.c:302
StreamTcpSessionPktFree
void StreamTcpSessionPktFree(Packet *)
Function to return the stream segments back to the pool.
Definition: stream-tcp.c:388
TcpStreamCnf_::max_syn_queued
uint8_t max_syn_queued
Definition: stream-tcp.h:72
stream-tcp-reassemble.h
FLOW_ACTION_DROP
#define FLOW_ACTION_DROP
Definition: flow.h:69
TcpStreamCnf_
Definition: stream-tcp.h:53
p
Packet * p
Definition: fuzz_dataset.c:30
StatsCounterId
Definition: counters.h:30
TcpStreamCnf_::streaming_log_api
bool streaming_log_api
Definition: stream-tcp.h:71
TCP_STREAM_URGENT_GAP
@ TCP_STREAM_URGENT_GAP
Definition: stream-tcp.h:49
TcpStreamCnf_::midstream_policy
enum ExceptionPolicy midstream_policy
Definition: stream-tcp.h:81
StreamTcpThread_::counter_tcp_active_sessions
StatsCounterId counter_tcp_active_sessions
Definition: stream-tcp.h:94
StreamTcpSessionClear
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:359
PacketQueueNoLock_
simple fifo queue for packets
Definition: packet-queue.h:34
TcpState
TcpState
Definition: stream-tcp-private.h:150
STREAM_HAS_UNPROCESSED_SEGMENTS_NEED_ONLY_DETECTION
@ STREAM_HAS_UNPROCESSED_SEGMENTS_NEED_ONLY_DETECTION
Definition: stream-tcp.h:191
StreamTcpThread_::counter_tcp_ssn_memcap
StatsCounterId counter_tcp_ssn_memcap
Definition: stream-tcp.h:97
TcpStreamCnf_::ssn_memcap_policy
enum ExceptionPolicy ssn_memcap_policy
Definition: stream-tcp.h:79
ExceptionPolicyCounters_
Definition: util-exception-policy-types.h:54
StreamTcpBypassEnabled
int StreamTcpBypassEnabled(void)
Definition: stream-tcp.c:7290
StreamTcpIncrMemuse
void StreamTcpIncrMemuse(uint64_t)
Definition: stream-tcp.c:236
STREAM_HAS_UNPROCESSED_SEGMENTS_NONE
@ STREAM_HAS_UNPROCESSED_SEGMENTS_NONE
Definition: stream-tcp.h:188
TcpStreamCnf_::urgent_policy
enum TcpStreamUrgentHandling urgent_policy
Definition: stream-tcp.h:82
StreamTcpThreadInit
TmEcode StreamTcpThreadInit(ThreadVars *, void *, void **)
Definition: stream-tcp.c:6178
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
StreamTcpUpdateAppLayerProgress
void StreamTcpUpdateAppLayerProgress(TcpSession *ssn, char direction, const uint32_t progress)
update reassembly progress
Definition: stream-tcp.c:6875
StreamTcpStreamCleanup
void StreamTcpStreamCleanup(TcpStream *stream)
Definition: stream-tcp.c:308
StreamTcpInlineMode
bool StreamTcpInlineMode(void)
See if stream engine is operating in inline mode.
Definition: stream-tcp.c:7301
TcpStreamCnf_::liberal_timestamps
bool liberal_timestamps
Definition: stream-tcp.h:86
stream.h
StreamTcpDetectLogFlush
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....
Definition: stream-tcp.c:7124
TcpStreamCnf_::max_synack_queued
uint8_t max_synack_queued
Definition: stream-tcp.h:65
Packet_
Definition: decode.h:528
stream-tcp-private.h
StreamTcpRegisterTests
void StreamTcpRegisterTests(void)
Definition: stream-tcp.c:3500
TcpSessionSetReassemblyDepth
void TcpSessionSetReassemblyDepth(TcpSession *ssn, uint32_t size)
Definition: stream-tcp.c:7307
StreamTcpThread_::counter_tcp_ack_unseen_data
StatsCounterId counter_tcp_ack_unseen_data
Definition: stream-tcp.h:113
TmEcode
TmEcode
Definition: tm-threads-common.h:80
StreamTcpSessionCleanup
void StreamTcpSessionCleanup(TcpSession *ssn)
Session cleanup function. Does not free the ssn.
Definition: stream-tcp.c:335
StreamTcpThread_::counter_tcp_pseudo
StatsCounterId counter_tcp_pseudo
Definition: stream-tcp.h:103
StreamReassembleForFrame
int StreamReassembleForFrame(TcpSession *ssn, TcpStream *stream, StreamReassembleRawFunc Callback, void *cb_data, const uint64_t offset, const bool eof)
Definition: stream-tcp-reassemble.c:1905
TcpStreamCnf_::reassembly_memcap_policy
enum ExceptionPolicy reassembly_memcap_policy
Definition: stream-tcp.h:80
StreamTcpThread_::counter_tcp_midstream_pickups
StatsCounterId counter_tcp_midstream_pickups
Definition: stream-tcp.h:107
StreamTcp
TmEcode StreamTcp(ThreadVars *, Packet *, void *, PacketQueueNoLock *)
Definition: stream-tcp.c:6137
StreamTcpInitConfig
void StreamTcpInitConfig(bool)
To initialize the stream global configuration data.
Definition: stream-tcp.c:496
Packet_::flow
struct Flow_ * flow
Definition: decode.h:584
StreamTcpThread_::counter_tcp_wrong_thread
StatsCounterId counter_tcp_wrong_thread
Definition: stream-tcp.h:111
StreamTcpStateAsString
const char * StreamTcpStateAsString(const enum TcpState)
Definition: stream-tcp.c:7314
TcpStreamCnf_::urgent_oob_limit_policy
enum TcpStreamUrgentHandling urgent_oob_limit_policy
Definition: stream-tcp.h:83
TcpStreamCnf_::prealloc_sessions
uint32_t prealloc_sessions
Definition: stream-tcp.h:67
stream_config
TcpStreamCnf stream_config
Definition: stream-tcp.c:227
TcpStreamCnf_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, reassembly_memcap)
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
TCP_STREAM_URGENT_DROP
@ TCP_STREAM_URGENT_DROP
Definition: stream-tcp.h:47
StreamTcpThreadDeinit
TmEcode StreamTcpThreadDeinit(ThreadVars *tv, void *data)
Definition: stream-tcp.c:6268
TcpStreamCnf_::midstream
bool midstream
Definition: stream-tcp.h:69
TcpStreamCnf_::reassembly_toclient_chunk_size
uint16_t reassembly_toclient_chunk_size
Definition: stream-tcp.h:77
StreamingBufferConfig_
Definition: util-streaming-buffer.h:65
StreamTcpInitMemuse
void StreamTcpInitMemuse(void)
Definition: stream-tcp.c:231
StreamTcpThread
struct StreamTcpThread_ StreamTcpThread
StreamTcpThread_::counter_tcp_sessions
StatsCounterId counter_tcp_sessions
Definition: stream-tcp.h:95
StreamTcpGetUsable
uint64_t StreamTcpGetUsable(const TcpStream *stream, const bool eof)
Definition: stream-tcp-reassemble.c:428
StreamTcpSetMemcap
int StreamTcpSetMemcap(uint64_t)
Update memcap value.
Definition: stream-tcp.c:287
StreamTcpThread_::counter_tcp_ssn_from_cache
StatsCounterId counter_tcp_ssn_from_cache
Definition: stream-tcp.h:98
StreamDataRightEdge
uint64_t StreamDataRightEdge(const TcpStream *stream, const bool eof)
Definition: stream-tcp-reassemble.c:419
StreamTcpThread_::ra_ctx
TcpReassemblyThreadCtx * ra_ctx
Definition: stream-tcp.h:116
TcpStreamCnf_::stream_init_flags
uint16_t stream_init_flags
Definition: stream-tcp.h:61
TcpReassemblyThreadCtx_
Definition: stream-tcp-reassemble.h:61
StreamTcpSegmentForSession
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.
Definition: stream-tcp.c:7207
TcpStreamCnf_::prealloc_segments
uint32_t prealloc_segments
Definition: stream-tcp.h:68
StreamTcpThread_::counter_tcp_ssn_memcap_eps
ExceptionPolicyCounters counter_tcp_ssn_memcap_eps
Definition: stream-tcp.h:101
EngineModeIsIPS
int EngineModeIsIPS(void)
Definition: suricata.c:247
StreamTcpThread_::counter_tcp_invalid_checksum
StatsCounterId counter_tcp_invalid_checksum
Definition: stream-tcp.h:105
suricata.h
TcpSessionPacketSsnReuse
bool TcpSessionPacketSsnReuse(const Packet *p, const Flow *f, const void *tcp_ssn)
Definition: stream-tcp.c:6125
TCP_STREAM_URGENT_INLINE
@ TCP_STREAM_URGENT_INLINE
Definition: stream-tcp.h:46
TcpStreamUrgentHandling
TcpStreamUrgentHandling
Definition: stream-tcp.h:45
TcpSession_
Definition: stream-tcp-private.h:290
StreamTcpThread_::ssn_pool_id
int ssn_pool_id
Definition: stream-tcp.h:92
StreamNeedsReassembly
uint8_t StreamNeedsReassembly(const TcpSession *ssn, uint8_t direction)
see what if any work the TCP session still needs
Definition: stream-tcp-reassemble.c:985
ExceptionPolicy
ExceptionPolicy
Definition: util-exception-policy-types.h:26
StreamTcpThread_::counter_tcp_midstream_eps
ExceptionPolicyCounters counter_tcp_midstream_eps
Definition: stream-tcp.h:109
TcpStreamCnf_::reassembly_toserver_chunk_size
uint16_t reassembly_toserver_chunk_size
Definition: stream-tcp.h:76
StreamTcpFreeConfig
void StreamTcpFreeConfig(bool)
Definition: stream-tcp.c:864
StreamReassembleRawUpdateProgress
void StreamReassembleRawUpdateProgress(TcpSession *ssn, Packet *p, const uint64_t progress)
update stream engine after detection
Definition: stream-tcp-reassemble.c:1559
f
Flow f
Definition: fuzz_dataset.c:32
StreamReassembleLog
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)
Definition: stream-tcp-reassemble.c:1982