Coverage Report

Created: 2026-05-16 07:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/src/stream-tcp.h
Line
Count
Source
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 __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
#include "suricata.h"
33
#include "util-exception-policy-types.h"
34
35
71
#define STREAM_VERBOSE false
36
/* Flag to indicate that the checksum validation for the stream engine
37
   has been enabled */
38
6.66M
#define STREAMTCP_INIT_FLAG_CHECKSUM_VALIDATION    BIT_U8(0)
39
71
#define STREAMTCP_INIT_FLAG_DROP_INVALID           BIT_U8(1)
40
104k
#define STREAMTCP_INIT_FLAG_BYPASS                 BIT_U8(2)
41
25.3M
#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
45
enum TcpStreamUrgentHandling {
46
    TCP_STREAM_URGENT_INLINE, /**< treat as inline data */
47
71
#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*/
54
typedef 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;
66
    uint8_t max_synack_queued;
67
68
    uint32_t prealloc_sessions; /**< ssns to prealloc per stream thread */
69
    uint32_t prealloc_segments; /**< segments to prealloc per stream thread */
70
    bool midstream;
71
    bool async_oneside;
72
    bool streaming_log_api;
73
    uint8_t max_syn_queued;
74
75
    uint32_t reassembly_depth;  /**< Depth until when we reassemble the stream */
76
77
    uint16_t reassembly_toserver_chunk_size;
78
    uint16_t reassembly_toclient_chunk_size;
79
80
    enum ExceptionPolicy ssn_memcap_policy;
81
    enum ExceptionPolicy reassembly_memcap_policy;
82
    enum ExceptionPolicy midstream_policy;
83
    enum TcpStreamUrgentHandling urgent_policy;
84
    enum TcpStreamUrgentHandling urgent_oob_limit_policy;
85
86
    /* default to "LINUX" timestamp behavior if true*/
87
    bool liberal_timestamps;
88
89
    StreamingBufferConfig sbcnf;
90
} TcpStreamCnf;
91
92
typedef struct StreamTcpThread_ {
93
    int ssn_pool_id;
94
95
    uint16_t counter_tcp_active_sessions;
96
    uint16_t counter_tcp_sessions;
97
    /** sessions not picked up because memcap was reached */
98
    uint16_t counter_tcp_ssn_memcap;
99
    uint16_t counter_tcp_ssn_from_cache;
100
    uint16_t counter_tcp_ssn_from_pool;
101
    /** exception policy */
102
    ExceptionPolicyCounters counter_tcp_ssn_memcap_eps;
103
    /** pseudo packets processed */
104
    uint16_t counter_tcp_pseudo;
105
    /** pseudo packets failed to setup */
106
    uint16_t counter_tcp_pseudo_failed;
107
    /** packets rejected because their csum is invalid */
108
    uint16_t counter_tcp_invalid_checksum;
109
    /** midstream pickups */
110
    uint16_t counter_tcp_midstream_pickups;
111
    /** exception policy stats */
112
    ExceptionPolicyCounters counter_tcp_midstream_eps;
113
    /** wrong thread */
114
    uint16_t counter_tcp_wrong_thread;
115
    /** ack for unseen data */
116
    uint16_t counter_tcp_ack_unseen_data;
117
118
    /** tcp reassembly thread data */
119
    TcpReassemblyThreadCtx *ra_ctx;
120
} StreamTcpThread;
121
122
extern TcpStreamCnf stream_config;
123
void StreamTcpInitConfig(bool);
124
void StreamTcpFreeConfig(bool);
125
void StreamTcpRegisterTests (void);
126
127
void StreamTcpSessionPktFree (Packet *);
128
129
void StreamTcpInitMemuse(void);
130
void StreamTcpIncrMemuse(uint64_t);
131
void StreamTcpDecrMemuse(uint64_t);
132
int StreamTcpSetMemcap(uint64_t);
133
uint64_t StreamTcpGetMemcap(void);
134
int StreamTcpCheckMemcap(uint64_t);
135
uint64_t StreamTcpMemuseCounter(void);
136
uint64_t StreamTcpReassembleMemuseGlobalCounter(void);
137
138
int StreamTcpSegmentForEach(const Packet *p, uint8_t flag,
139
                        StreamSegmentCallback CallbackFunc,
140
                        void *data);
141
int StreamTcpSegmentForSession(
142
        const Packet *p, uint8_t flag, StreamSegmentCallback CallbackFunc, void *data);
143
void StreamTcpReassembleConfigEnableOverlapCheck(void);
144
void TcpSessionSetReassemblyDepth(TcpSession *ssn, uint32_t size);
145
146
typedef int (*StreamReassembleRawFunc)(
147
        void *data, const uint8_t *input, const uint32_t input_len, const uint64_t offset);
148
149
int StreamReassembleForFrame(TcpSession *ssn, TcpStream *stream, StreamReassembleRawFunc Callback,
150
        void *cb_data, const uint64_t offset, const bool eof);
151
int StreamReassembleLog(const TcpSession *ssn, const TcpStream *stream,
152
        StreamReassembleRawFunc Callback, void *cb_data, const uint64_t progress_in,
153
        uint64_t *progress_out, const bool eof);
154
int StreamReassembleRaw(TcpSession *ssn, const Packet *p,
155
        StreamReassembleRawFunc Callback, void *cb_data,
156
        uint64_t *progress_out, bool respect_inspect_depth);
157
void StreamReassembleRawUpdateProgress(TcpSession *ssn, Packet *p, const uint64_t progress);
158
159
void StreamTcpDetectLogFlush(ThreadVars *tv, StreamTcpThread *stt, Flow *f, Packet *p, PacketQueueNoLock *pq);
160
161
const char *StreamTcpStateAsString(const enum TcpState);
162
const char *StreamTcpSsnStateAsString(const TcpSession *ssn);
163
164
enum ExceptionPolicy StreamTcpSsnMemcapGetExceptionPolicy(void);
165
enum ExceptionPolicy StreamTcpReassemblyMemcapGetExceptionPolicy(void);
166
enum ExceptionPolicy StreamMidstreamGetExceptionPolicy(void);
167
168
/** ------- Inline functions: ------ */
169
170
/**
171
  * \brief If we are on IPS mode, and got a drop action triggered from
172
  * the IP only module, or from a reassembled msg and/or from an
173
  * applayer detection, then drop the rest of the packets of the
174
  * same stream and avoid inspecting it any further
175
  * \param p pointer to the Packet to check
176
  * \retval 1 if we must drop this stream
177
  * \retval 0 if the stream still legal
178
  */
179
static inline int StreamTcpCheckFlowDrops(Packet *p)
180
13.1M
{
181
    /* If we are on IPS mode, and got a drop action triggered from
182
     * the IP only module, or from a reassembled msg and/or from an
183
     * applayer detection, then drop the rest of the packets of the
184
     * same stream and avoid inspecting it any further */
185
13.1M
    if (EngineModeIsIPS() && (p->flow->flags & FLOW_ACTION_DROP))
186
0
        return 1;
187
188
13.1M
    return 0;
189
13.1M
}
Unexecuted instantiation: app-layer-parser.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: app-layer-smtp.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: app-layer-ssh.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: app-layer-ssl.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-engine-state.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: flow.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: flow-hash.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: flow-manager.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: flow-timeout.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: runmode-unix-socket.c:StreamTcpCheckFlowDrops
stream-tcp.c:StreamTcpCheckFlowDrops
Line
Count
Source
180
13.1M
{
181
    /* If we are on IPS mode, and got a drop action triggered from
182
     * the IP only module, or from a reassembled msg and/or from an
183
     * applayer detection, then drop the rest of the packets of the
184
     * same stream and avoid inspecting it any further */
185
13.1M
    if (EngineModeIsIPS() && (p->flow->flags & FLOW_ACTION_DROP))
186
0
        return 1;
187
188
13.1M
    return 0;
189
13.1M
}
Unexecuted instantiation: stream-tcp-list.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: stream-tcp-reassemble.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: stream-tcp-sack.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: suricata.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: util-exception-policy.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: util-file.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: app-layer.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: app-layer-detect-proto.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: app-layer-frames.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: app-layer-htp.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-engine-frame.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-engine-payload.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-engine-register.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-filemagic.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-filename.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-filesize.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-filestore.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-ftpbounce.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-client-body.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-cookie.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-header-common.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-header-names.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-host.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-method.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-protocol.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-request-line.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-response-line.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-server-body.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-start.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-stat-code.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-stat-msg.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-ua.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-http-uri.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-ike-spi.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-ike-nonce-payload.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-ike-key-exchange-payload.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-lua.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-modbus.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-pcre.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-reference.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-rfb-name.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-sip-method.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-sip-protocol.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-sip-request-line.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-sip-response-line.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-sip-stat-code.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-sip-stat-msg.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-sip-uri.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-ssh-hassh.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-ssh-hassh-server.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-ssh-hassh-server-string.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-ssh-hassh-string.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-ssh-proto-version.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-ssh-software-version.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-ssl-state.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-ssl-version.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-stream_size.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-threshold.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-tls.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-tls-cert-fingerprint.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-tls-cert-issuer.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-tls-certs.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-tls-cert-serial.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-tls-cert-subject.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-tls-cert-validity.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-tls-ja3-hash.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-tls-ja3s-hash.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-tls-ja3s-string.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-tls-ja3-string.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-tls-sni.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-tls-version.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-tls-random.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-ttl.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-uricontent.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-urilen.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: flow-worker.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: output.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: output-eve-stream.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: output-filestore.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: output-json-alert.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: output-json-flow.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: output-json-frame.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: output-streaming.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-bypass.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-config.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-dce-iface.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-dce-opnum.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-dce-stub-data.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-dns-query.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-engine-event.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: detect-engine-file.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: stream.c:StreamTcpCheckFlowDrops
Unexecuted instantiation: util-unittest-helper.c:StreamTcpCheckFlowDrops
190
191
enum {
192
    /* stream has no segments for forced reassembly, nor for detection */
193
    STREAM_HAS_UNPROCESSED_SEGMENTS_NONE = 0,
194
    /* stream has no segments for forced reassembly, but only segments that
195
     * have been sent for detection, but are stuck in the detection queues */
196
    STREAM_HAS_UNPROCESSED_SEGMENTS_NEED_ONLY_DETECTION = 1,
197
};
198
199
TmEcode StreamTcp (ThreadVars *, Packet *, void *, PacketQueueNoLock *);
200
uint8_t StreamNeedsReassembly(const TcpSession *ssn, uint8_t direction);
201
TmEcode StreamTcpThreadInit(ThreadVars *, void *, void **);
202
TmEcode StreamTcpThreadDeinit(ThreadVars *tv, void *data);
203
204
int StreamTcpPacket (ThreadVars *tv, Packet *p, StreamTcpThread *stt,
205
                     PacketQueueNoLock *pq);
206
/* clear ssn and return to pool */
207
void StreamTcpSessionClear(void *ssnptr);
208
/* cleanup ssn, but don't free ssn */
209
void StreamTcpSessionCleanup(TcpSession *ssn);
210
/* cleanup stream, but don't free the stream */
211
void StreamTcpStreamCleanup(TcpStream *stream);
212
/* check if bypass is enabled */
213
int StreamTcpBypassEnabled(void);
214
int StreamTcpInlineMode(void);
215
216
int TcpSessionPacketSsnReuse(const Packet *p, const Flow *f, const void *tcp_ssn);
217
218
void StreamTcpUpdateAppLayerProgress(TcpSession *ssn, char direction,
219
        const uint32_t progress);
220
221
uint64_t StreamTcpGetAcked(const TcpStream *stream);
222
uint64_t StreamTcpGetUsable(const TcpStream *stream, const bool eof);
223
uint64_t StreamDataRightEdge(const TcpStream *stream, const bool eof);
224
225
void StreamTcpThreadCacheEnable(void);
226
void StreamTcpThreadCacheCleanup(void);
227
228
#endif /* __STREAM_TCP_H__ */
229