suricata
stream-tcp.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2022 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 __STREAM_TCP_H__
26 #define __STREAM_TCP_H__
27 
28 #include "stream-tcp-private.h"
29 
30 #include "stream.h"
31 #include "stream-tcp-reassemble.h"
32 
33 #define STREAM_VERBOSE false
34 /* Flag to indicate that the checksum validation for the stream engine
35  has been enabled */
36 #define STREAMTCP_INIT_FLAG_CHECKSUM_VALIDATION BIT_U8(0)
37 #define STREAMTCP_INIT_FLAG_DROP_INVALID BIT_U8(1)
38 #define STREAMTCP_INIT_FLAG_BYPASS BIT_U8(2)
39 #define STREAMTCP_INIT_FLAG_INLINE BIT_U8(3)
40 
41 /*global flow data*/
42 typedef struct TcpStreamCnf_ {
43  /** stream tracking
44  *
45  * max stream mem usage
46  */
47  SC_ATOMIC_DECLARE(uint64_t, memcap);
48  SC_ATOMIC_DECLARE(uint64_t, reassembly_memcap); /**< max memory usage for stream reassembly */
49 
50  uint16_t stream_init_flags; /**< new stream flags will be initialized to this */
51 
52  /* coccinelle: TcpStreamCnf:flags:STREAMTCP_INIT_ */
53  uint8_t flags;
55 
56  uint32_t prealloc_sessions; /**< ssns to prealloc per stream thread */
57  uint32_t prealloc_segments; /**< segments to prealloc per stream thread */
58  bool midstream;
60  uint32_t reassembly_depth; /**< Depth until when we reassemble the stream */
61 
64 
66 
69 
72 
73 typedef struct StreamTcpThread_ {
75 
76  /** queue for pseudo packet(s) that were created in the stream
77  * process and need further handling. Currently only used when
78  * receiving (valid) RST packets */
80 
83  /** sessions not picked up because memcap was reached */
85  /** pseudo packets processed */
87  /** pseudo packets failed to setup */
89  /** packets rejected because their csum is invalid */
91  /** TCP packets with no associated flow */
93  /** sessions reused */
95  /** syn pkts */
96  uint16_t counter_tcp_syn;
97  /** syn/ack pkts */
99  /** rst pkts */
100  uint16_t counter_tcp_rst;
101  /** midstream pickups */
103  /** wrong thread */
105 
106  /** tcp reassembly thread data */
109 
111 void StreamTcpInitConfig(bool);
112 void StreamTcpFreeConfig(bool);
113 void StreamTcpRegisterTests (void);
114 
116 
117 void StreamTcpInitMemuse(void);
118 void StreamTcpIncrMemuse(uint64_t);
119 void StreamTcpDecrMemuse(uint64_t);
120 int StreamTcpSetMemcap(uint64_t);
121 uint64_t StreamTcpGetMemcap(void);
122 int StreamTcpCheckMemcap(uint64_t);
123 uint64_t StreamTcpMemuseCounter(void);
125 
126 Packet *StreamTcpPseudoSetup(Packet *, uint8_t *, uint32_t);
127 
128 int StreamTcpSegmentForEach(const Packet *p, uint8_t flag,
129  StreamSegmentCallback CallbackFunc,
130  void *data);
132  const Packet *p, uint8_t flag, StreamSegmentCallback CallbackFunc, void *data);
134 void TcpSessionSetReassemblyDepth(TcpSession *ssn, uint32_t size);
135 
136 typedef int (*StreamReassembleRawFunc)(
137  void *data, const uint8_t *input, const uint32_t input_len, const uint64_t offset);
138 
140  void *cb_data, const uint64_t offset, const bool eof);
141 int StreamReassembleLog(TcpSession *ssn, TcpStream *stream,
142  StreamReassembleRawFunc Callback, void *cb_data,
143  uint64_t progress_in,
144  uint64_t *progress_out, bool eof);
145 int StreamReassembleRaw(TcpSession *ssn, const Packet *p,
146  StreamReassembleRawFunc Callback, void *cb_data,
147  uint64_t *progress_out, bool respect_inspect_depth);
148 void StreamReassembleRawUpdateProgress(TcpSession *ssn, Packet *p, uint64_t progress);
149 
151 
152 const char *StreamTcpStateAsString(const enum TcpState);
153 const char *StreamTcpSsnStateAsString(const TcpSession *ssn);
154 
155 /** ------- Inline functions: ------ */
156 
157 /**
158  * \brief If we are on IPS mode, and got a drop action triggered from
159  * the IP only module, or from a reassembled msg and/or from an
160  * applayer detection, then drop the rest of the packets of the
161  * same stream and avoid inspecting it any further
162  * \param p pointer to the Packet to check
163  * \retval 1 if we must drop this stream
164  * \retval 0 if the stream still legal
165  */
166 static inline int StreamTcpCheckFlowDrops(Packet *p)
167 {
168  /* 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  if (EngineModeIsIPS() && (p->flow->flags & FLOW_ACTION_DROP))
173  return 1;
174 
175  return 0;
176 }
177 
178 enum {
179  /* stream has no segments for forced reassembly, nor for detection */
181  /* stream has no segments for forced reassembly, but only segments that
182  * have been sent for detection, but are stuck in the detection queues */
184 };
185 
187 uint8_t StreamNeedsReassembly(const TcpSession *ssn, uint8_t direction);
188 TmEcode StreamTcpThreadInit(ThreadVars *, void *, void **);
190 void StreamTcpRegisterTests (void);
191 
193  PacketQueueNoLock *pq);
194 /* clear ssn and return to pool */
195 void StreamTcpSessionClear(void *ssnptr);
196 /* cleanup ssn, but don't free ssn */
198 /* cleanup stream, but don't free the stream */
199 void StreamTcpStreamCleanup(TcpStream *stream);
200 /* check if bypass is enabled */
201 int StreamTcpBypassEnabled(void);
203 int StreamTcpInlineMode(void);
204 
205 int TcpSessionPacketSsnReuse(const Packet *p, const Flow *f, const void *tcp_ssn);
206 
207 void StreamTcpUpdateAppLayerProgress(TcpSession *ssn, char direction,
208  const uint32_t progress);
209 
210 uint64_t StreamTcpGetAcked(const TcpStream *stream);
211 uint64_t StreamTcpGetUsable(const TcpStream *stream, const bool eof);
212 
213 #endif /* __STREAM_TCP_H__ */
214 
StreamReassembleRawFunc
int(* StreamReassembleRawFunc)(void *data, const uint8_t *input, const uint32_t input_len, const uint64_t offset)
Definition: stream-tcp.h:136
StreamTcpThread_::counter_tcp_midstream_pickups
uint16_t counter_tcp_midstream_pickups
Definition: stream-tcp.h:102
StreamSegmentCallback
int(* StreamSegmentCallback)(const Packet *, TcpSegment *, void *, const uint8_t *, uint32_t)
Definition: stream.h:36
TcpStream_
Definition: stream-tcp-private.h:109
TcpStreamCnf_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, memcap)
StreamTcpPacket
int StreamTcpPacket(ThreadVars *tv, Packet *p, StreamTcpThread *stt, PacketQueueNoLock *pq)
Definition: stream-tcp.c:4910
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
TcpStreamCnf
struct TcpStreamCnf_ TcpStreamCnf
StreamTcpSegmentForEach
int StreamTcpSegmentForEach(const Packet *p, uint8_t flag, StreamSegmentCallback CallbackFunc, void *data)
Definition: stream-tcp.c:6377
StreamTcpDecrMemuse
void StreamTcpDecrMemuse(uint64_t)
Definition: stream-tcp.c:129
StreamTcpCheckMemcap
int StreamTcpCheckMemcap(uint64_t)
Check if alloc'ing "size" would mean we're over memcap.
Definition: stream-tcp.c:162
StreamTcpReassembleConfigEnableOverlapCheck
void StreamTcpReassembleConfigEnableOverlapCheck(void)
Definition: stream-tcp-list.c:40
StreamTcpThread_
Definition: stream-tcp.h:73
StreamTcpThread_::counter_tcp_reused_ssn
uint16_t counter_tcp_reused_ssn
Definition: stream-tcp.h:94
TcpStreamCnf_::reassembly_depth
uint32_t reassembly_depth
Definition: stream-tcp.h:60
Flow_
Flow data structure.
Definition: flow.h:356
TcpStreamCnf_::flags
uint8_t flags
Definition: stream-tcp.h:53
StreamTcpThread_::counter_tcp_sessions
uint16_t counter_tcp_sessions
Definition: stream-tcp.h:82
StreamTcpPseudoSetup
Packet * StreamTcpPseudoSetup(Packet *, uint8_t *, uint32_t)
Function to fetch a packet from the packet allocation queue for creation of the pseudo packet from th...
Definition: stream-tcp.c:6115
TcpStreamCnf_::async_oneside
int async_oneside
Definition: stream-tcp.h:59
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:1824
TcpStreamCnf_::sbcnf
StreamingBufferConfig sbcnf
Definition: stream-tcp.h:70
StreamTcpMemuseCounter
uint64_t StreamTcpMemuseCounter(void)
Definition: stream-tcp.c:150
StreamTcpGetAcked
uint64_t StreamTcpGetAcked(const TcpStream *stream)
Definition: stream-tcp-reassemble.c:405
StreamTcpGetMemcap
uint64_t StreamTcpGetMemcap(void)
Return memcap value.
Definition: stream-tcp.c:190
StreamTcpSessionPktFree
void StreamTcpSessionPktFree(Packet *)
Function to return the stream segments back to the pool.
Definition: stream-tcp.c:277
stream-tcp-reassemble.h
FLOW_ACTION_DROP
#define FLOW_ACTION_DROP
Definition: flow.h:69
TcpStreamCnf_
Definition: stream-tcp.h:42
TcpStreamCnf_::streaming_log_api
bool streaming_log_api
Definition: stream-tcp.h:65
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:243
PacketQueueNoLock_
simple fifo queue for packets
Definition: packet-queue.h:32
StreamTcpInlineDropInvalid
int StreamTcpInlineDropInvalid(void)
See if stream engine is dropping invalid packet in inline mode.
Definition: stream-tcp.c:333
StreamReassembleRawUpdateProgress
void StreamReassembleRawUpdateProgress(TcpSession *ssn, Packet *p, uint64_t progress)
update stream engine after detection
Definition: stream-tcp-reassemble.c:1458
TcpState
TcpState
Definition: stream-tcp-private.h:152
StreamTcpThread_::counter_tcp_ssn_memcap
uint16_t counter_tcp_ssn_memcap
Definition: stream-tcp.h:84
TcpStreamCnf_::ssn_memcap_policy
enum ExceptionPolicy ssn_memcap_policy
Definition: stream-tcp.h:67
STREAM_HAS_UNPROCESSED_SEGMENTS_NONE
@ STREAM_HAS_UNPROCESSED_SEGMENTS_NONE
Definition: stream-tcp.h:180
StreamTcpThread_::counter_tcp_synack
uint16_t counter_tcp_synack
Definition: stream-tcp.h:98
StreamTcpBypassEnabled
int StreamTcpBypassEnabled(void)
Definition: stream-tcp.c:6519
StreamTcpIncrMemuse
void StreamTcpIncrMemuse(uint64_t)
Definition: stream-tcp.c:122
StreamTcpThreadInit
TmEcode StreamTcpThreadInit(ThreadVars *, void *, void **)
Definition: stream-tcp.c:5364
StreamTcpThread_::counter_tcp_wrong_thread
uint16_t counter_tcp_wrong_thread
Definition: stream-tcp.h:104
StreamTcpThread_::counter_tcp_invalid_checksum
uint16_t counter_tcp_invalid_checksum
Definition: stream-tcp.h:90
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
StreamTcpUpdateAppLayerProgress
void StreamTcpUpdateAppLayerProgress(TcpSession *ssn, char direction, const uint32_t progress)
update reassembly progress
Definition: stream-tcp.c:6026
StreamTcpThread_::counter_tcp_rst
uint16_t counter_tcp_rst
Definition: stream-tcp.h:100
StreamTcpStreamCleanup
void StreamTcpStreamCleanup(TcpStream *stream)
Definition: stream-tcp.c:196
STREAM_HAS_UNPROCESSED_SEGMENTS_NEED_ONLY_DETECTION
@ STREAM_HAS_UNPROCESSED_SEGMENTS_NEED_ONLY_DETECTION
Definition: stream-tcp.h:183
StreamTcpThread_::counter_tcp_syn
uint16_t counter_tcp_syn
Definition: stream-tcp.h:96
ExceptionPolicy
ExceptionPolicy
Definition: util-exception-policy.h:25
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:6353
TcpStreamCnf_::max_synack_queued
uint8_t max_synack_queued
Definition: stream-tcp.h:54
Packet_
Definition: decode.h:442
StreamTcpReassembleMemuseGlobalCounter
uint64_t StreamTcpReassembleMemuseGlobalCounter(void)
Definition: stream-tcp-reassemble.c:148
stream-tcp-private.h
StreamTcpRegisterTests
void StreamTcpRegisterTests(void)
Definition: stream-tcp.c:4275
TcpSessionSetReassemblyDepth
void TcpSessionSetReassemblyDepth(TcpSession *ssn, uint32_t size)
Definition: stream-tcp.c:6536
TmEcode
TmEcode
Definition: tm-threads-common.h:81
StreamTcpSessionCleanup
void StreamTcpSessionCleanup(TcpSession *ssn)
Session cleanup function. Does not free the ssn.
Definition: stream-tcp.c:209
StreamReassembleForFrame
int StreamReassembleForFrame(TcpSession *ssn, TcpStream *stream, StreamReassembleRawFunc Callback, void *cb_data, const uint64_t offset, const bool eof)
Definition: stream-tcp-reassemble.c:1812
TcpSessionPacketSsnReuse
int TcpSessionPacketSsnReuse(const Packet *p, const Flow *f, const void *tcp_ssn)
Definition: stream-tcp.c:5313
TcpStreamCnf_::reassembly_memcap_policy
enum ExceptionPolicy reassembly_memcap_policy
Definition: stream-tcp.h:68
StreamTcp
TmEcode StreamTcp(ThreadVars *, Packet *, void *, PacketQueueNoLock *)
Definition: stream-tcp.c:5325
StreamTcpInitConfig
void StreamTcpInitConfig(bool)
To initialize the stream global configuration data.
Definition: stream-tcp.c:359
StreamTcpThread_::counter_tcp_pseudo_failed
uint16_t counter_tcp_pseudo_failed
Definition: stream-tcp.h:88
Packet_::flow
struct Flow_ * flow
Definition: decode.h:479
StreamTcpSsnStateAsString
const char * StreamTcpSsnStateAsString(const TcpSession *ssn)
Definition: stream-tcp.c:6586
StreamTcpStateAsString
const char * StreamTcpStateAsString(const enum TcpState)
Definition: stream-tcp.c:6545
StreamReassembleLog
int StreamReassembleLog(TcpSession *ssn, TcpStream *stream, StreamReassembleRawFunc Callback, void *cb_data, uint64_t progress_in, uint64_t *progress_out, bool eof)
Definition: stream-tcp-reassemble.c:1889
StreamTcpThread_::counter_tcp_active_sessions
uint16_t counter_tcp_active_sessions
Definition: stream-tcp.h:81
TcpStreamCnf_::prealloc_sessions
uint32_t prealloc_sessions
Definition: stream-tcp.h:56
stream_config
TcpStreamCnf stream_config
Definition: stream-tcp.c:113
TcpStreamCnf_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, reassembly_memcap)
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
StreamTcpInlineMode
int StreamTcpInlineMode(void)
See if stream engine is operating in inline mode.
Definition: stream-tcp.c:6530
StreamTcpThreadDeinit
TmEcode StreamTcpThreadDeinit(ThreadVars *tv, void *data)
Definition: stream-tcp.c:5430
TcpStreamCnf_::midstream
bool midstream
Definition: stream-tcp.h:58
TcpStreamCnf_::reassembly_toclient_chunk_size
uint16_t reassembly_toclient_chunk_size
Definition: stream-tcp.h:63
StreamingBufferConfig_
Definition: util-streaming-buffer.h:64
StreamTcpInitMemuse
void StreamTcpInitMemuse(void)
Definition: stream-tcp.c:117
StreamTcpThread
struct StreamTcpThread_ StreamTcpThread
StreamTcpThread_::counter_tcp_no_flow
uint16_t counter_tcp_no_flow
Definition: stream-tcp.h:92
StreamTcpGetUsable
uint64_t StreamTcpGetUsable(const TcpStream *stream, const bool eof)
Definition: stream-tcp-reassemble.c:410
StreamTcpSetMemcap
int StreamTcpSetMemcap(uint64_t)
Update memcap value.
Definition: stream-tcp.c:175
Flow_::flags
uint32_t flags
Definition: flow.h:434
StreamTcpThread_::counter_tcp_pseudo
uint16_t counter_tcp_pseudo
Definition: stream-tcp.h:86
StreamTcpThread_::ra_ctx
TcpReassemblyThreadCtx * ra_ctx
Definition: stream-tcp.h:107
TcpStreamCnf_::stream_init_flags
uint16_t stream_init_flags
Definition: stream-tcp.h:50
TcpReassemblyThreadCtx_
Definition: stream-tcp-reassemble.h:60
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:6436
TcpStreamCnf_::prealloc_segments
uint32_t prealloc_segments
Definition: stream-tcp.h:57
EngineModeIsIPS
int EngineModeIsIPS(void)
Definition: suricata.c:252
StreamTcpThread_::pseudo_queue
PacketQueueNoLock pseudo_queue
Definition: stream-tcp.h:79
TcpSession_
Definition: stream-tcp-private.h:274
StreamTcpThread_::ssn_pool_id
int ssn_pool_id
Definition: stream-tcp.h:74
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:878
TcpStreamCnf_::reassembly_toserver_chunk_size
uint16_t reassembly_toserver_chunk_size
Definition: stream-tcp.h:62
StreamTcpFreeConfig
void StreamTcpFreeConfig(bool)
Definition: stream-tcp.c:666