suricata
stream-tcp-private.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 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  */
23 
24 #ifndef __STREAM_TCP_PRIVATE_H__
25 #define __STREAM_TCP_PRIVATE_H__
26 
27 #include "tree.h"
28 #include "decode.h"
29 #include "util-pool.h"
30 #include "util-pool-thread.h"
31 #include "util-streaming-buffer.h"
32 
33 #define STREAMTCP_QUEUE_FLAG_TS 0x01
34 #define STREAMTCP_QUEUE_FLAG_WS 0x02
35 #define STREAMTCP_QUEUE_FLAG_SACK 0x04
36 
37 /** currently only SYN/ACK */
38 typedef struct TcpStateQueue_ {
39  uint8_t flags;
40  uint8_t wscale;
41  uint16_t win;
42  uint32_t seq;
43  uint32_t ack;
44  uint32_t ts;
45  uint32_t pkt_ts;
48 
49 typedef struct StreamTcpSackRecord {
50  uint32_t le; /**< left edge, host order */
51  uint32_t re; /**< right edge, host order */
54 
56 
57 /* red-black tree prototype for SACK records */
60 
61 typedef struct TcpSegment {
63  uint16_t payload_len; /**< actual size of the payload */
64  uint32_t seq;
65  RB_ENTRY(TcpSegment) __attribute__((__packed__)) rb;
66  StreamingBufferSegment sbseg;
67 } __attribute__((__packed__)) TcpSegment;
68 
69 /** \brief compare function for the Segment tree
70  *
71  * Main sort point is the sequence number. When sequence numbers
72  * are equal compare payload_len as well. This way the tree is
73  * sorted by seq, and in case of duplicate seqs we are sorted
74  * small to large.
75  */
76 int TcpSegmentCompare(struct TcpSegment *a, struct TcpSegment *b);
77 
78 /* red-black tree prototype for TcpSegment */
81 
82 #define TCP_SEG_LEN(seg) (seg)->payload_len
83 #define TCP_SEG_OFFSET(seg) (seg)->sbseg.stream_offset
84 
85 #define SEG_SEQ_RIGHT_EDGE(seg) ((seg)->seq + TCP_SEG_LEN((seg)))
86 
87 /* get right edge of sequence space of seen segments.
88  * Only use if STREAM_HAS_SEEN_DATA is true. */
89 #define STREAM_SEQ_RIGHT_EDGE(stream) (stream)->segs_right_edge
90 #define STREAM_RIGHT_EDGE(stream) (STREAM_BASE_OFFSET((stream)) + (STREAM_SEQ_RIGHT_EDGE((stream)) - (stream)->base_seq))
91 /* return true if we have seen data segments. */
92 #define STREAM_HAS_SEEN_DATA(stream) (!RB_EMPTY(&(stream)->sb.sbb_tree) || (stream)->sb.stream_offset || (stream)->sb.buf_offset)
93 
94 typedef struct TcpStream_ {
95  uint16_t flags:12; /**< Flag specific to the stream e.g. Timestamp */
96  /* coccinelle: TcpStream:flags:STREAMTCP_STREAM_FLAG_ */
97  uint16_t wscale:4; /**< wscale setting in this direction, 4 bits as max val is 15 */
98  uint8_t os_policy; /**< target based OS policy used for reassembly and handling packets*/
99  uint8_t tcp_flags; /**< TCP flags seen */
100 
101  uint32_t isn; /**< initial sequence number */
102  uint32_t next_seq; /**< next expected sequence number */
103  uint32_t last_ack; /**< last ack'd sequence number in this stream */
104  uint32_t next_win; /**< next max seq within window */
105  uint32_t window; /**< current window setting, after wscale is applied */
106 
107  uint32_t last_ts; /**< Time stamp (TSVAL) of the last seen packet for this stream*/
108  uint32_t last_pkt_ts; /**< Time of last seen packet for this stream (needed for PAWS update)
109  This will be used to validate the last_ts, when connection has been idle for
110  longer time.(RFC 1323)*/
111  /* reassembly */
112  uint32_t base_seq; /**< seq where we are left with reassebly. Matches STREAM_BASE_OFFSET below. */
113 
114  uint32_t app_progress_rel; /**< app-layer progress relative to STREAM_BASE_OFFSET */
115  uint32_t raw_progress_rel; /**< raw reassembly progress relative to STREAM_BASE_OFFSET */
116  uint32_t log_progress_rel; /**< streaming logger progress relative to STREAM_BASE_OFFSET */
117 
118  uint32_t min_inspect_depth; /**< min inspect size set by the app layer, to make sure enough data
119  * remains available for inspection together with app layer buffers */
120  uint32_t data_required; /**< data required from STREAM_APP_PROGRESS before calling app-layer again */
121 
123  struct TCPSEG seg_tree; /**< red black tree of TCP segments. Data is stored in TcpStream::sb */
124  uint32_t segs_right_edge;
125 
126  uint32_t sack_size; /**< combined size of the SACK ranges currently in our tree. Updated
127  * at INSERT/REMOVE time. */
128  struct TCPSACK sack_tree; /**< red back tree of TCP SACK records. */
130 
131 #define STREAM_BASE_OFFSET(stream) ((stream)->sb.stream_offset)
132 #define STREAM_APP_PROGRESS(stream) (STREAM_BASE_OFFSET((stream)) + (stream)->app_progress_rel)
133 #define STREAM_RAW_PROGRESS(stream) (STREAM_BASE_OFFSET((stream)) + (stream)->raw_progress_rel)
134 #define STREAM_LOG_PROGRESS(stream) (STREAM_BASE_OFFSET((stream)) + (stream)->log_progress_rel)
135 
136 /* from /usr/include/netinet/tcp.h */
138 {
151 };
152 
153 /*
154  * Per SESSION flags
155  */
156 
157 /** Flag for mid stream session */
158 #define STREAMTCP_FLAG_MIDSTREAM 0x0001
159 /** Flag for mid stream established session */
160 #define STREAMTCP_FLAG_MIDSTREAM_ESTABLISHED 0x0002
161 /** Flag for mid session when syn/ack is received */
162 #define STREAMTCP_FLAG_MIDSTREAM_SYNACK 0x0004
163 /** Flag for TCP Timestamp option */
164 #define STREAMTCP_FLAG_TIMESTAMP 0x0008
165 /** Server supports wscale (even though it can be 0) */
166 #define STREAMTCP_FLAG_SERVER_WSCALE 0x0010
167 /** Closed by RST */
168 #define STREAMTCP_FLAG_CLOSED_BY_RST 0x0020
169 /** Flag to indicate that the session is handling asynchronous stream.*/
170 #define STREAMTCP_FLAG_ASYNC 0x0040
171 /** Flag to indicate we're dealing with 4WHS: SYN, SYN, SYN/ACK, ACK
172  * (http://www.breakingpointsystems.com/community/blog/tcp-portals-the-three-way-handshake-is-a-lie) */
173 #define STREAMTCP_FLAG_4WHS 0x0080
174 /** Flag to indicate that this session is possible trying to evade the detection
175  * (http://www.packetstan.com/2010/06/recently-ive-been-on-campaign-to-make.html) */
176 #define STREAMTCP_FLAG_DETECTION_EVASION_ATTEMPT 0x0100
177 /** Flag to indicate the client (SYN pkt) permits SACK */
178 #define STREAMTCP_FLAG_CLIENT_SACKOK 0x0200
179 /** Flag to indicate both sides of the session permit SACK (SYN + SYN/ACK) */
180 #define STREAMTCP_FLAG_SACKOK 0x0400
181 // vacancy
182 /** 3WHS confirmed by server -- if suri sees 3whs ACK but server doesn't (pkt
183  * is lost on the way to server), SYN/ACK is retransmitted. If server sends
184  * normal packet we assume 3whs to be completed. Only used for SYN/ACK resend
185  * event. */
186 #define STREAMTCP_FLAG_3WHS_CONFIRMED 0x1000
187 /** App Layer tracking/reassembly is disabled */
188 #define STREAMTCP_FLAG_APP_LAYER_DISABLED 0x2000
189 /** Stream can be bypass */
190 #define STREAMTCP_FLAG_BYPASS 0x4000
191 /** SSN uses TCP Fast Open */
192 #define STREAMTCP_FLAG_TCP_FAST_OPEN 0x8000
193 
194 /*
195  * Per STREAM flags
196  */
197 
198 /** stream is in a gap state */
199 #define STREAMTCP_STREAM_FLAG_GAP BIT_U16(0)
200 /** Flag to avoid stream reassembly/app layer inspection for the stream */
201 #define STREAMTCP_STREAM_FLAG_NOREASSEMBLY BIT_U16(1)
202 /** we received a keep alive */
203 #define STREAMTCP_STREAM_FLAG_KEEPALIVE BIT_U16(2)
204 /** Stream has reached it's reassembly depth, all further packets are ignored */
205 #define STREAMTCP_STREAM_FLAG_DEPTH_REACHED BIT_U16(3)
206 /** Trigger reassembly next time we need 'raw' */
207 #define STREAMTCP_STREAM_FLAG_TRIGGER_RAW BIT_U16(4)
208 /** Stream supports TIMESTAMP -- used to set ssn STREAMTCP_FLAG_TIMESTAMP
209  * flag. */
210 #define STREAMTCP_STREAM_FLAG_TIMESTAMP BIT_U16(5)
211 /** Flag to indicate the zero value of timestamp */
212 #define STREAMTCP_STREAM_FLAG_ZERO_TIMESTAMP BIT_U16(6)
213 /** App proto detection completed */
214 #define STREAMTCP_STREAM_FLAG_APPPROTO_DETECTION_COMPLETED BIT_U16(7)
215 /** App proto detection skipped */
216 #define STREAMTCP_STREAM_FLAG_APPPROTO_DETECTION_SKIPPED BIT_U16(8)
217 /** Raw reassembly disabled for new segments */
218 #define STREAMTCP_STREAM_FLAG_NEW_RAW_DISABLED BIT_U16(9)
219 /** Raw reassembly disabled completely */
220 #define STREAMTCP_STREAM_FLAG_DISABLE_RAW BIT_U16(10)
221 
222 #define STREAMTCP_STREAM_FLAG_RST_RECV BIT_U16(11)
223 
224 /** NOTE: flags field is 12 bits */
225 
226 
227 
228 
229 #define PAWS_24DAYS 2073600 /**< 24 days in seconds */
230 
231 #define PKT_IS_IN_RIGHT_DIR(ssn, p) ((ssn)->flags & STREAMTCP_FLAG_MIDSTREAM_SYNACK ? \
232  PKT_IS_TOSERVER(p) ? (p)->flowflags &= ~FLOW_PKT_TOSERVER \
233  (p)->flowflags |= FLOW_PKT_TOCLIENT : (p)->flowflags &= ~FLOW_PKT_TOCLIENT \
234  (p)->flowflags |= FLOW_PKT_TOSERVER : 0)
235 
236 /* Macro's for comparing Sequence numbers
237  * Page 810 from TCP/IP Illustrated, Volume 2. */
238 #define SEQ_EQ(a,b) ((int32_t)((a) - (b)) == 0)
239 #define SEQ_LT(a,b) ((int32_t)((a) - (b)) < 0)
240 #define SEQ_LEQ(a,b) ((int32_t)((a) - (b)) <= 0)
241 #define SEQ_GT(a,b) ((int32_t)((a) - (b)) > 0)
242 #define SEQ_GEQ(a,b) ((int32_t)((a) - (b)) >= 0)
243 
244 #define STREAMTCP_SET_RA_BASE_SEQ(stream, seq) { \
245  do { \
246  (stream)->base_seq = (seq) + 1; \
247  } while(0); \
248 }
249 
250 #define StreamTcpSetEvent(p, e) { \
251  if ((p)->flags & PKT_STREAM_NO_EVENTS) { \
252  SCLogDebug("not setting event %d on pkt %p (%"PRIu64"), " \
253  "stream in known bad condition", (e), p, (p)->pcap_cnt); \
254  } else { \
255  SCLogDebug("setting event %d on pkt %p (%"PRIu64")", \
256  (e), p, (p)->pcap_cnt); \
257  ENGINE_SET_EVENT((p), (e)); \
258  } \
259 }
260 
261 typedef struct TcpSession_ {
263  uint8_t state:4; /**< tcp state from state enum */
264  uint8_t pstate:4; /**< previous state */
265  uint8_t queue_len; /**< length of queue list below */
267  /** track all the tcp flags we've seen */
269  /* coccinelle: TcpSession:flags:STREAMTCP_FLAG */
270  uint16_t flags;
271  uint32_t reassembly_depth; /**< reassembly depth for the stream */
274  TcpStateQueue *queue; /**< list of SYN/ACK candidates */
276 
277 #define StreamTcpSetStreamFlagAppProtoDetectionCompleted(stream) \
278  ((stream)->flags |= STREAMTCP_STREAM_FLAG_APPPROTO_DETECTION_COMPLETED)
279 #define StreamTcpIsSetStreamFlagAppProtoDetectionCompleted(stream) \
280  ((stream)->flags & STREAMTCP_STREAM_FLAG_APPPROTO_DETECTION_COMPLETED)
281 #define StreamTcpResetStreamFlagAppProtoDetectionCompleted(stream) \
282  ((stream)->flags &= ~STREAMTCP_STREAM_FLAG_APPPROTO_DETECTION_COMPLETED);
283 #define StreamTcpDisableAppLayerReassembly(ssn) do { \
284  SCLogDebug("setting STREAMTCP_FLAG_APP_LAYER_DISABLED on ssn %p", ssn); \
285  ((ssn)->flags |= STREAMTCP_FLAG_APP_LAYER_DISABLED); \
286  } while (0);
287 
288 #endif /* __STREAM_TCP_PRIVATE_H__ */
TcpStateQueue_::next
struct TcpStateQueue_ * next
Definition: stream-tcp-private.h:46
TCP_SYN_RECV
@ TCP_SYN_RECV
Definition: stream-tcp-private.h:142
TcpStream_
Definition: stream-tcp-private.h:94
TcpSession_::pstate
uint8_t pstate
Definition: stream-tcp-private.h:264
TcpStream_::isn
uint32_t isn
Definition: stream-tcp-private.h:101
TcpStateQueue
struct TcpStateQueue_ TcpStateQueue
TCP_SYN_SENT
@ TCP_SYN_SENT
Definition: stream-tcp-private.h:141
RB_PROTOTYPE
RB_PROTOTYPE(TCPSACK, StreamTcpSackRecord, rb, TcpSackCompare)
TcpSegmentCompare
int TcpSegmentCompare(struct TcpSegment *a, struct TcpSegment *b)
compare function for the Segment tree
Definition: stream-tcp-list.c:49
TcpStream_::seg_tree
struct TCPSEG seg_tree
Definition: stream-tcp-private.h:123
TcpStream_::log_progress_rel
uint32_t log_progress_rel
Definition: stream-tcp-private.h:116
util-pool-thread.h
TCP_FIN_WAIT2
@ TCP_FIN_WAIT2
Definition: stream-tcp-private.h:145
TcpStream_::os_policy
uint8_t os_policy
Definition: stream-tcp-private.h:98
TcpSegment::sbseg
StreamingBufferSegment sbseg
Definition: stream-tcp-private.h:66
TCP_FIN_WAIT1
@ TCP_FIN_WAIT1
Definition: stream-tcp-private.h:144
TCP_LAST_ACK
@ TCP_LAST_ACK
Definition: stream-tcp-private.h:147
TcpStateQueue_::win
uint16_t win
Definition: stream-tcp-private.h:41
TCP_ESTABLISHED
@ TCP_ESTABLISHED
Definition: stream-tcp-private.h:143
TcpSegment::seq
uint32_t seq
Definition: stream-tcp-private.h:64
TcpStream_::flags
uint16_t flags
Definition: stream-tcp-private.h:95
TcpStateQueue_::flags
uint8_t flags
Definition: stream-tcp-private.h:39
StreamTcpSackRecord::RB_ENTRY
RB_ENTRY(StreamTcpSackRecord) rb
TcpStream_::data_required
uint32_t data_required
Definition: stream-tcp-private.h:120
TcpStateQueue_
Definition: stream-tcp-private.h:38
RB_HEAD
RB_HEAD(TCPSACK, StreamTcpSackRecord)
StreamTcpSackRecord::le
uint32_t le
Definition: stream-tcp-private.h:50
TcpSession_::flags
uint16_t flags
Definition: stream-tcp-private.h:270
TcpState
TcpState
Definition: stream-tcp-private.h:138
TcpSession_::queue
TcpStateQueue * queue
Definition: stream-tcp-private.h:274
__attribute__
struct TcpSegment __attribute__((__packed__)) TcpSegment
DNP3 link header.
Definition: decode-vlan.c:113
TcpStream_::last_ack
uint32_t last_ack
Definition: stream-tcp-private.h:103
TCP_NONE
@ TCP_NONE
Definition: stream-tcp-private.h:139
TcpSession_::reassembly_depth
uint32_t reassembly_depth
Definition: stream-tcp-private.h:271
TCP_CLOSE_WAIT
@ TCP_CLOSE_WAIT
Definition: stream-tcp-private.h:148
TcpStream_::last_ts
uint32_t last_ts
Definition: stream-tcp-private.h:107
decode.h
TcpStateQueue_::seq
uint32_t seq
Definition: stream-tcp-private.h:42
TcpStream
struct TcpStream_ TcpStream
TcpSession
struct TcpSession_ TcpSession
TcpStream_::min_inspect_depth
uint32_t min_inspect_depth
Definition: stream-tcp-private.h:118
TCP_LISTEN
@ TCP_LISTEN
Definition: stream-tcp-private.h:140
TcpSession_::state
uint8_t state
Definition: stream-tcp-private.h:263
TcpStream_::segs_right_edge
uint32_t segs_right_edge
Definition: stream-tcp-private.h:124
TcpSession_::tcp_packet_flags
uint8_t tcp_packet_flags
Definition: stream-tcp-private.h:268
TcpStream_::sack_tree
struct TCPSACK sack_tree
Definition: stream-tcp-private.h:128
TcpStream_::next_win
uint32_t next_win
Definition: stream-tcp-private.h:104
PoolThreadReserved
uint16_t PoolThreadReserved
Definition: util-pool-thread.h:57
TcpSegment
Definition: stream-tcp-private.h:61
TcpStream_::window
uint32_t window
Definition: stream-tcp-private.h:105
TcpSegment::payload_len
uint16_t payload_len
Definition: stream-tcp-private.h:63
TcpStream_::sack_size
uint32_t sack_size
Definition: stream-tcp-private.h:126
TcpStream_::raw_progress_rel
uint32_t raw_progress_rel
Definition: stream-tcp-private.h:115
tree.h
StreamTcpSackRecord
struct StreamTcpSackRecord StreamTcpSackRecord
TcpSegment::res
PoolThreadReserved res
Definition: stream-tcp-private.h:62
StreamingBuffer_
Definition: util-streaming-buffer.h:95
TCP_CLOSED
@ TCP_CLOSED
Definition: stream-tcp-private.h:150
TcpStateQueue_::wscale
uint8_t wscale
Definition: stream-tcp-private.h:40
util-streaming-buffer.h
TcpStream_::base_seq
uint32_t base_seq
Definition: stream-tcp-private.h:112
TcpStream_::sb
StreamingBuffer sb
Definition: stream-tcp-private.h:122
TcpSession_::client
TcpStream client
Definition: stream-tcp-private.h:273
TCP_CLOSING
@ TCP_CLOSING
Definition: stream-tcp-private.h:149
TcpSackCompare
int TcpSackCompare(struct StreamTcpSackRecord *a, struct StreamTcpSackRecord *b)
Definition: stream-tcp-sack.c:34
TcpStream_::next_seq
uint32_t next_seq
Definition: stream-tcp-private.h:102
TcpSession_::server
TcpStream server
Definition: stream-tcp-private.h:272
TcpStream_::app_progress_rel
uint32_t app_progress_rel
Definition: stream-tcp-private.h:114
TcpStateQueue_::ts
uint32_t ts
Definition: stream-tcp-private.h:44
TcpStateQueue_::pkt_ts
uint32_t pkt_ts
Definition: stream-tcp-private.h:45
TcpStream_::last_pkt_ts
uint32_t last_pkt_ts
Definition: stream-tcp-private.h:108
TCP_TIME_WAIT
@ TCP_TIME_WAIT
Definition: stream-tcp-private.h:146
TcpSegment::RB_ENTRY
RB_ENTRY(TcpSegment) __attribute__((__packed__)) rb
TcpSession_::queue_len
uint8_t queue_len
Definition: stream-tcp-private.h:265
TcpSession_
Definition: stream-tcp-private.h:261
TcpSession_::data_first_seen_dir
int8_t data_first_seen_dir
Definition: stream-tcp-private.h:266
TcpSession_::res
PoolThreadReserved res
Definition: stream-tcp-private.h:262
util-pool.h
StreamTcpSackRecord::re
uint32_t re
Definition: stream-tcp-private.h:51
TcpStream_::tcp_flags
uint8_t tcp_flags
Definition: stream-tcp-private.h:99
StreamTcpSackRecord
Definition: stream-tcp-private.h:49
TcpStream_::wscale
uint16_t wscale
Definition: stream-tcp-private.h:97
TcpStateQueue_::ack
uint32_t ack
Definition: stream-tcp-private.h:43