suricata
stream-tcp-inline.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2017 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 #include "../suricata-common.h"
19 #include "../stream-tcp-private.h"
20 #include "../stream-tcp.h"
21 #include "../stream-tcp-reassemble.h"
22 #include "../stream-tcp-inline.h"
23 #include "../stream-tcp-list.h"
24 #include "../stream-tcp-util.h"
25 #include "../util-streaming-buffer.h"
26 #include "../util-print.h"
27 #include "../util-unittest.h"
28 
29 static int VALIDATE(TcpStream *stream, uint8_t *data, uint32_t data_len)
30 {
31  if (StreamingBufferCompareRawData(&stream->sb,
32  data, data_len) == 0)
33  {
34  SCReturnInt(0);
35  }
36  SCLogInfo("OK");
37  PrintRawDataFp(stdout, data, data_len);
38  return 1;
39 }
40 
41 #define INLINE_START(isn) \
42  Packet *p; \
43  TcpReassemblyThreadCtx *ra_ctx = NULL; \
44  TcpSession ssn; \
45  ThreadVars tv; \
46  memset(&tv, 0, sizeof(tv)); \
47  \
48  StreamTcpUTInit(&ra_ctx); \
49  StreamTcpUTInitInline(); \
50  \
51  StreamTcpUTSetupSession(&ssn); \
52  StreamTcpUTSetupStream(&ssn.server, (isn)); \
53  StreamTcpUTSetupStream(&ssn.client, (isn)); \
54  \
55  TcpStream *stream = &ssn.client;
56 
57 #define INLINE_END \
58  StreamTcpUTClearSession(&ssn); \
59  StreamTcpUTDeinit(ra_ctx); \
60  PASS
61 
62 #define INLINE_STEP(rseq, seg, seglen, buf, buflen, packet, packetlen) \
63  p = UTHBuildPacketReal((uint8_t *)(seg), (seglen), IPPROTO_TCP, "1.1.1.1", "2.2.2.2", 1024, 80); \
64  FAIL_IF(p == NULL); \
65  p->tcph->th_seq = htonl(stream->isn + (rseq)); \
66  p->tcph->th_ack = htonl(31); \
67  FAIL_IF (StreamTcpReassembleHandleSegmentHandleData(&tv, ra_ctx, &ssn, stream, p) < 0); \
68  FAIL_IF (memcmp(p->payload, packet, MIN((packetlen),p->payload_len)) != 0); \
69  UTHFreePacket(p); \
70  FAIL_IF(!(VALIDATE(stream, (uint8_t *)(buf), (buflen))));
71 
72 /** \test full overlap */
73 static int StreamTcpInlineTest01(void)
74 {
75  INLINE_START(0);
76  INLINE_STEP(1, "AAC", 3, "AAC", 3, "AAC", 3);
77  INLINE_STEP(1, "ABC", 3, "AAC", 3, "AAC", 3);
78  INLINE_END;
79 }
80 
81 /** \test full overlap */
82 static int StreamTcpInlineTest02(void)
83 {
84  INLINE_START(0);
85  INLINE_STEP(1, "ABCDE", 5, "ABCDE", 5, "ABCDE", 5);
86  INLINE_STEP(2, "xxx", 3, "ABCDE", 5, "BCD", 3);
87  INLINE_END;
88 }
89 
90 /** \test partial overlap */
91 static int StreamTcpInlineTest03(void)
92 {
93  INLINE_START(0);
94  INLINE_STEP(1, "ABCDE", 5, "ABCDE", 5, "ABCDE", 5);
95  INLINE_STEP(3, "xxxxx", 5, "ABCDExx", 7, "CDExx", 5);
96  INLINE_END;
97 }
98 
99 /** \test partial overlap */
100 static int StreamTcpInlineTest04(void)
101 {
102  INLINE_START(0);
103  INLINE_STEP(3, "ABCDE", 5, "\0\0ABCDE", 7, "ABCDE", 5);
104  INLINE_STEP(1, "xxxxx", 5, "xxABCDE", 7, "xxABC", 5);
105  INLINE_END;
106 }
107 
108 /** \test no overlap */
109 static int StreamTcpInlineTest05(void)
110 {
111  INLINE_START(0);
112  INLINE_STEP(8, "ABCDE", 5, "\0\0\0\0\0\0\0ABCDE", 12, "ABCDE", 5);
113  INLINE_STEP(1, "xxxxx", 5, "xxxxx\0\0ABCDE", 12, "xxxxx", 5);
114  INLINE_END;
115 }
116 
117 /** \test multiple overlaps */
118 static int StreamTcpInlineTest06(void)
119 {
120  INLINE_START(0);
121  INLINE_STEP(2, "A", 1, "\0A", 2, "A", 1);
122  INLINE_STEP(4, "A", 1, "\0A\0A", 4, "A", 1);
123  INLINE_STEP(6, "A", 1, "\0A\0A\0A", 6, "A", 1);
124  INLINE_STEP(8, "A", 1, "\0A\0A\0A\0A", 8, "A", 1);
125  INLINE_STEP(1, "xxxxxxxxx", 9, "xAxAxAxAx", 9, "xAxAxAxAx", 9);
126  INLINE_END;
127 }
128 
129 /** \test overlap, data not different */
130 static int StreamTcpInlineTest07(void)
131 {
132  INLINE_START(0);
133  INLINE_STEP(3, "ABCDE", 5, "\0\0ABCDE", 7, "ABCDE", 5);
134  INLINE_STEP(1, "XXABC", 5, "XXABCDE", 7, "XXABC", 5);
135  INLINE_END;
136 }
137 
138 static int StreamTcpInlineTest08(void)
139 {
140  INLINE_START(0);
141  INLINE_STEP(1, "AAAAA", 5, "AAAAA", 5, "AAAAA", 5);
142  INLINE_STEP(1, "BBBBB", 5, "AAAAA", 5, "AAAAA", 5);
143  INLINE_STEP(1, "CCCCCCCCCC", 10, "AAAAACCCCC", 10, "AAAAACCCCC", 10);
144  INLINE_STEP(10, "X", 1, "AAAAACCCCC", 10, "C", 1);
145  INLINE_STEP(11, "X", 1, "AAAAACCCCCX", 11, "X", 1);
146  INLINE_END;
147 }
148 
150 {
151  UtRegisterTest("StreamTcpInlineTest01", StreamTcpInlineTest01);
152  UtRegisterTest("StreamTcpInlineTest02", StreamTcpInlineTest02);
153  UtRegisterTest("StreamTcpInlineTest03", StreamTcpInlineTest03);
154  UtRegisterTest("StreamTcpInlineTest04", StreamTcpInlineTest04);
155  UtRegisterTest("StreamTcpInlineTest05", StreamTcpInlineTest05);
156  UtRegisterTest("StreamTcpInlineTest06", StreamTcpInlineTest06);
157  UtRegisterTest("StreamTcpInlineTest07", StreamTcpInlineTest07);
158  UtRegisterTest("StreamTcpInlineTest08", StreamTcpInlineTest08);
159 }
int StreamingBufferCompareRawData(const StreamingBuffer *sb, const uint8_t *rawdata, uint32_t rawdata_len)
void StreamTcpInlineRegisterTests(void)
StreamingBuffer sb
#define INLINE_STEP(rseq, seg, seglen, buf, buflen, packet, packetlen)
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define SCReturnInt(x)
Definition: util-debug.h:341
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:254
void PrintRawDataFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
Definition: util-print.c:141
#define INLINE_END
#define INLINE_START(isn)