74 static SCMutex segment_pool_memuse_mutex;
75 static uint64_t segment_pool_memuse = 0;
76 static uint64_t segment_pool_memcnt = 0;
79 thread_local uint64_t t_pcapcnt = UINT64_MAX;
89 static int g_tcp_session_dump_enabled = 0;
93 return g_tcp_session_dump_enabled == 1;
98 g_tcp_session_dump_enabled = 1;
116 static void StreamTcpReassembleIncrMemuse(uint64_t size)
129 static void StreamTcpReassembleDecrMemuse(uint64_t size)
134 BUG_ON(presize > UINT_MAX);
143 BUG_ON(postsize > presize);
167 if (
unlikely((g_eps_stream_reassembly_memcap != UINT64_MAX &&
168 g_eps_stream_reassembly_memcap == t_pcapcnt))) {
169 SCLogNotice(
"simulating memcap reached condition for packet %" PRIu64, t_pcapcnt);
174 if (memcapcopy == 0 ||
175 (uint64_t)((uint64_t)size +
SC_ATOMIC_GET(ra_memuse)) <= memcapcopy)
187 if (size == 0 || (uint64_t)
SC_ATOMIC_GET(ra_memuse) < size) {
211 static void *ReassembleCalloc(
size_t n,
size_t size)
222 StreamTcpReassembleIncrMemuse(n * size);
231 if (size > orig_size) {
244 if (size > orig_size) {
245 StreamTcpReassembleIncrMemuse(size - orig_size);
247 StreamTcpReassembleDecrMemuse(orig_size - size);
255 static void ReassembleFree(
void *ptr,
size_t size)
258 StreamTcpReassembleDecrMemuse(size);
262 static void *TcpSegmentPoolAlloc(
void)
284 "TcpSegmentPcapHdrStorage");
293 "packet header data within "
294 "TcpSegmentPcapHdrStorage");
306 static int TcpSegmentPoolInit(
void *data)
324 if (pcap_hdr != NULL) {
331 StreamTcpReassembleIncrMemuse(memuse);
341 segment_pool_memcnt++;
342 SCLogDebug(
"segment_pool_memcnt %"PRIu64
"", segment_pool_memcnt);
346 StreamTcpReassembleIncrMemuse((uint32_t)
sizeof(
TcpSegment));
351 static void TcpSegmentPoolCleanup(
void *ptr)
367 StreamTcpReassembleDecrMemuse((uint32_t)
sizeof(
TcpSegment));
372 segment_pool_memcnt--;
373 SCLogDebug(
"segment_pool_memcnt %"PRIu64
"", segment_pool_memcnt);
410 static inline uint64_t GetAbsLastAck(
const TcpStream *stream)
412 if (STREAM_LASTACK_GT_BASESEQ(stream)) {
423 right_edge =
MIN(GetAbsLastAck(stream), right_edge);
430 uint64_t right_edge = StreamingBufferGetConsecutiveDataRightEdge(&stream->
sb);
432 right_edge =
MIN(GetAbsLastAck(stream), right_edge);
459 const uint16_t eof_flags = APP_LAYER_PARSER_EOF_TS | APP_LAYER_PARSER_EOF_TC;
484 static int StreamTcpReassemblyConfig(
bool quiet)
486 uint32_t segment_prealloc = 2048;
489 uint32_t prealloc = 0;
496 segment_prealloc = prealloc;
499 SCLogConfig(
"stream.reassembly \"segment-prealloc\": %u", segment_prealloc);
502 int overlap_diff_data = 0;
503 (void)
SCConfGetBool(
"stream.reassembly.check-overlap-different-data", &overlap_diff_data);
504 if (overlap_diff_data) {
511 uint16_t max_regions = 8;
522 SCLogConfig(
"stream.reassembly \"max-regions\": %u", max_regions);
540 if (StreamTcpReassemblyConfig(quiet) < 0)
562 if (segment_pool_memuse > 0)
563 SCLogDebug(
"segment_pool_memuse %" PRIu64
" segment_pool_memcnt %" PRIu64
"",
564 segment_pool_memuse, segment_pool_memcnt);
583 TcpSegmentPoolInit, TcpSegmentPoolCleanup);
585 SCLogDebug(
"pool size %d, thread segment_thread_pool_id %d",
591 SCLogDebug(
"pool size %d, thread segment_thread_pool_id %d",
597 SCLogError(
"failed to setup/expand stream segment pool. Expand stream.reassembly.memcap?");
617 static void StreamTcpReassembleExceptionPolicyStatsIncr(
663 uint32_t
seq, uint32_t size)
681 SCLogDebug(
"segment entirely before base_seq, weird: base %u, seq %u, re %u",
695 SCLogDebug(
"seq + size %u, base %u, seg_depth %"PRIu64
" limit %u", (
seq + size),
700 SCLogDebug(
"STREAMTCP_STREAM_FLAG_DEPTH_REACHED");
704 SCLogDebug(
"NOT STREAMTCP_STREAM_FLAG_DEPTH_REACHED");
707 SCLogDebug(
"full depth not yet reached: %"PRIu64
" <= %"PRIu32,
708 (stream->base_seq_offset + stream->
base_seq + size),
775 SCLogDebug(
"ssn %p: both app and raw reassembly disabled, not reassembling",
ssn);
779 uint16_t *urg_offset;
786 const TCPHdr *tcph = PacketGetTCP(
p);
790 uint8_t urg_data = 0;
800 if ((*urg_offset) < UINT16_MAX) {
804 if ((*urg_offset) == UINT16_MAX) {
821 (*urg_offset) == UINT16_MAX &&
837 uint32_t size = StreamTcpReassembleCheckDepth(
ssn, stream, seg_seq,
payload_len);
838 SCLogDebug(
"ssn %p: check depth returned %"PRIu32,
ssn, size);
884 SCLogDebug(
"StreamTcpReassembleInsertSegment failed");
896 flag |= STREAM_START;
904 flag |= STREAM_MIDSTREAM;
912 flag |= STREAM_TOSERVER;
914 flag |= STREAM_TOCLIENT;
917 flag |= STREAM_DEPTH;
928 static bool StreamTcpReassembleRawCheckLimit(
934 #define STREAMTCP_STREAM_FLAG_FLUSH_FLAGS \
935 ( STREAMTCP_STREAM_FLAG_DEPTH_REACHED \
936 | STREAMTCP_STREAM_FLAG_TRIGGER_RAW \
937 | STREAMTCP_STREAM_FLAG_NEW_RAW_DISABLED)
941 SCLogDebug(
"reassembling now as STREAMTCP_STREAM_FLAG_DEPTH_REACHED "
942 "is set, so not expecting any new data segments");
945 SCLogDebug(
"reassembling now as STREAMTCP_STREAM_FLAG_TRIGGER_RAW is set");
948 SCLogDebug(
"reassembling now as STREAMTCP_STREAM_FLAG_NEW_RAW_DISABLED is set, "
949 "so no new segments will be considered");
953 #undef STREAMTCP_STREAM_FLAG_FLUSH_FLAGS
962 const uint64_t last_ack_abs = GetAbsLastAck(stream);
968 if (chunk_size <= diff) {
971 SCLogDebug(
"%s min chunk len not yet reached: "
972 "last_ack %" PRIu32
", ra_raw_base_seq %" PRIu32
", %" PRIu32
" < "
989 const char *dirstr = NULL;
991 if (direction == STREAM_TOSERVER) {
1015 const uint64_t right_edge =
1017 SCLogDebug(
"%s: app %" PRIu64
" (use: %s), raw %" PRIu64
1018 " (use: %s). Stream right edge: %" PRIu64,
1022 SCLogDebug(
"%s: STREAM_HAS_UNPROCESSED_SEGMENTS_NEED_ONLY_DETECTION", dirstr);
1027 const uint64_t right_edge = StreamingBufferGetConsecutiveDataRightEdge(&stream->
sb);
1028 SCLogDebug(
"%s: app %" PRIu64
" (use: %s), raw %" PRIu64
1029 " (use: %s). Stream right edge: %" PRIu64,
1033 SCLogDebug(
"%s: STREAM_HAS_UNPROCESSED_SEGMENTS_NEED_ONLY_DETECTION", dirstr);
1038 SCLogDebug(
"%s: STREAM_HAS_UNPROCESSED_SEGMENTS_NONE", dirstr);
1043 static uint64_t GetStreamSize(
TcpStream *stream)
1048 uint64_t last_ack_abs = GetAbsLastAck(stream);
1049 uint64_t last_re = 0;
1055 const uint64_t seg_abs =
1057 if (last_re != 0 && last_re < seg_abs) {
1058 const char *gacked = NULL;
1059 if (last_ack_abs >= seg_abs) {
1060 gacked =
"fully ack'd";
1061 }
else if (last_ack_abs > last_re) {
1062 gacked =
"partly ack'd";
1064 gacked =
"not yet ack'd";
1066 SCLogDebug(
" -> gap of size %" PRIu64
", ack:%s", seg_abs - last_re, gacked);
1069 const char *acked = NULL;
1070 if (last_ack_abs >= seg_abs + (uint64_t)
TCP_SEG_LEN(seg)) {
1071 acked =
"fully ack'd";
1072 }
else if (last_ack_abs > seg_abs) {
1073 acked =
"partly ack'd";
1075 acked =
"not yet ack'd";
1078 SCLogDebug(
"%u -> seg %p seq %u abs %" PRIu64
" size %u abs %" PRIu64
" (%" PRIu64
1111 GetAbsLastAck(stream) > (cur_blk->
offset + cur_blk->
len);
1123 static bool GetAppBuffer(
const TcpStream *stream,
const uint8_t **data, uint32_t *data_len,
1124 uint64_t
offset,
const bool check_for_gap)
1126 const uint8_t *mydata;
1127 uint32_t mydata_len;
1128 bool gap_ahead =
false;
1136 *data_len = mydata_len;
1155 gap_ahead = check_for_gap && GapAhead(stream, blk);
1159 SCLogDebug(
"gap, want data at offset %"PRIu64
", "
1160 "got data at %"PRIu64
". GAP of size %"PRIu64,
1167 SCLogDebug(
"get data from offset %"PRIu64
". SBB %"PRIu64
"/%u",
1170 SCLogDebug(
"data %p, data_len %u", *data, *data_len);
1172 gap_ahead = check_for_gap && GapAhead(stream, blk);
1191 const uint64_t last_ack_abs = GetAbsLastAck(stream) - (uint64_t)ackadded;
1198 if (last_ack_abs > app_progress) {
1205 "next_seq %u < last_ack %u, but no data in list",
1209 const uint64_t next_seq_abs =
1212 if (blk->
offset > next_seq_abs && blk->
offset < last_ack_abs) {
1215 "next_seq %u < last_ack %u, but ACK'd data beyond gap.",
1222 "last_ack_abs %" PRIu64
" > app_progress %" PRIu64
", "
1223 "but we have no data.",
1228 "last_ack_abs %" PRIu64
" <= app_progress %" PRIu64,
1233 static inline uint32_t AdjustToAcked(
const Packet *
p,
1235 const uint64_t app_progress,
const uint32_t data_len)
1237 uint32_t adjusted = data_len;
1248 const uint64_t last_ack_abs = GetAbsLastAck(stream);
1252 if (app_progress <= last_ack_abs && app_progress + data_len > last_ack_abs) {
1253 adjusted = (uint32_t)(last_ack_abs - app_progress);
1255 SCLogDebug(
"data len adjusted to %u to make sure only ACK'd "
1256 "data is considered", adjusted);
1273 SCLogDebug(
"app progress %"PRIu64, app_progress);
1275 uint64_t last_ack_abs = GetAbsLastAck(*stream);
1276 SCLogDebug(
"last_ack %u (abs %" PRIu64
"), base_seq %u", (*stream)->last_ack, last_ack_abs,
1277 (*stream)->base_seq);
1279 const uint8_t *mydata;
1280 uint32_t mydata_len;
1281 bool last_was_gap =
false;
1284 const uint8_t
flags = StreamGetAppLayerFlags(
ssn, *stream,
p);
1285 bool check_for_gap_ahead = ((*stream)->data_required > 0);
1287 GetAppBuffer(*stream, &mydata, &mydata_len, app_progress, check_for_gap_ahead);
1289 if (last_was_gap && mydata_len == 0) {
1292 last_was_gap =
false;
1295 mydata_len = AdjustToAcked(
p,
ssn, *stream, app_progress, mydata_len);
1297 if (mydata == NULL && mydata_len > 0 && CheckGap(
ssn, *stream,
p)) {
1298 SCLogDebug(
"sending GAP to app-layer (size: %u)", mydata_len);
1301 StreamGetAppLayerFlags(
ssn, *stream,
p) | STREAM_GAP, app_update_dir);
1315 if ((*stream)->data_required > 0) {
1316 if ((*stream)->data_required > mydata_len) {
1317 (*stream)->data_required -= mydata_len;
1319 (*stream)->data_required = 0;
1324 if (no_progress_update)
1326 last_was_gap =
true;
1329 }
else if (
flags & STREAM_DEPTH) {
1332 if (mydata == NULL && mydata_len > 0) {
1335 }
else if (mydata == NULL || (mydata_len == 0 && ((
flags & STREAM_EOF) == 0))) {
1348 SCLogDebug(
"stream %p data in buffer %p of len %u and offset %"PRIu64,
1349 *stream, &(*stream)->sb, mydata_len, app_progress);
1353 if (mydata_len < (*stream)->data_required) {
1356 SCLogDebug(
"GAP while expecting more data (expect %u, gap size %u)",
1357 (*stream)->data_required, mydata_len);
1358 (*stream)->app_progress_rel += mydata_len;
1359 (*stream)->data_required -= mydata_len;
1369 (*stream)->data_required = 0;
1374 mydata_len,
flags, app_update_dir);
1380 app_progress = new_app_progress;
1381 if (
flags & STREAM_DEPTH)
1409 SCLogDebug(
"stream no reassembly flag set or app-layer disabled.");
1415 GetSessionSize(
ssn,
p);
1426 uint8_t stream_flags = StreamGetAppLayerFlags(
ssn, stream,
p);
1428 tv, ra_ctx,
p,
p->
flow,
ssn, &stream, NULL, 0, stream_flags, app_update_dir);
1436 return ReassembleUpdateAppLayer(
tv, ra_ctx,
ssn, &stream,
p, app_update_dir);
1442 static int GetRawBuffer(
const TcpStream *stream,
const uint8_t **data, uint32_t *data_len,
1445 const uint8_t *mydata;
1446 uint32_t mydata_len;
1451 uint64_t roffset =
offset;
1459 *data_len = mydata_len;
1460 *data_offset = roffset;
1462 SCLogDebug(
"multiblob %s. Want offset %"PRIu64,
1463 *iter == NULL ?
"starting" :
"continuing",
offset);
1464 if (*iter == NULL) {
1469 if (*iter == NULL) {
1476 SCLogDebug(
"getting multiple blobs. Iter %p, %"PRIu64
"/%u", *iter, (*iter)->offset, (*iter)->len);
1481 if ((*iter)->offset <
offset) {
1482 uint64_t delta =
offset - (*iter)->offset;
1483 if (delta < mydata_len) {
1484 *data = mydata + delta;
1485 *data_len = (uint32_t)(mydata_len - delta);
1496 *data_len = mydata_len;
1497 *data_offset = (*iter)->offset;
1500 *iter = SBB_RB_NEXT(*iter);
1531 const uint64_t segs_re_abs =
1536 if (StreamTcpReassembleRawCheckLimit(
ssn, stream,
p) == 1) {
1574 }
else if (progress == 0) {
1579 target = GetAbsLastAck(stream);
1589 SCLogDebug(
"pcap_cnt %" PRIu64
": progress %" PRIu64
" app %" PRIu64
" raw %" PRIu64
1590 " tcp win %" PRIu32,
1599 SCLogDebug(
"ssn %p: STREAMTCP_STREAM_FLAG_NEW_RAW_DISABLED set, "
1600 "now that detect ran also set STREAMTCP_STREAM_FLAG_DISABLE_RAW",
ssn);
1647 "packet payload len %u, so chunk_size adjusted to %u",
p->
payload_len, chunk_size);
1650 const TCPHdr *tcph = PacketGetTCP(
p);
1655 uint64_t packet_rightedge_abs = packet_leftedge_abs +
p->
payload_len;
1656 SCLogDebug(
"packet_leftedge_abs %"PRIu64
", rightedge %"PRIu64,
1657 packet_leftedge_abs, packet_rightedge_abs);
1659 const uint8_t *mydata = NULL;
1660 uint32_t mydata_len = 0;
1661 uint64_t mydata_offset = 0;
1663 bool return_progress =
false;
1668 return_progress =
true;
1671 SCLogDebug(
"finding our SBB from offset %"PRIu64, packet_leftedge_abs);
1678 mydata_offset = sbb->
offset;
1683 uint64_t mydata_rightedge_abs = mydata_offset + mydata_len;
1684 if ((mydata == NULL || mydata_len == 0) ||
1685 (mydata_offset >= packet_rightedge_abs ||
1686 packet_leftedge_abs >= mydata_rightedge_abs) ||
1687 (packet_leftedge_abs < mydata_offset ||
1688 packet_rightedge_abs > mydata_rightedge_abs))
1693 mydata_offset = packet_leftedge_abs;
1697 SCLogDebug(
"chunk_size %u mydata_len %u", chunk_size, mydata_len);
1698 if (mydata_len > chunk_size) {
1699 uint32_t excess = mydata_len - chunk_size;
1700 SCLogDebug(
"chunk_size %u mydata_len %u excess %u", chunk_size, mydata_len, excess);
1702 if (mydata_rightedge_abs == packet_rightedge_abs) {
1704 mydata_len -= excess;
1705 mydata_offset += excess;
1706 SCLogDebug(
"cutting front of the buffer with %u", excess);
1707 }
else if (mydata_offset == packet_leftedge_abs) {
1708 mydata_len -= excess;
1709 SCLogDebug(
"cutting tail of the buffer with %u", excess);
1712 uint32_t abs_before = (uint32_t)(packet_leftedge_abs - mydata_offset);
1714 uint32_t abs_after = (uint32_t)(mydata_rightedge_abs - packet_rightedge_abs);
1715 uint32_t before = abs_before;
1716 uint32_t after = abs_after;
1717 SCLogDebug(
"before %u after %u", before, after);
1742 uint32_t skip = abs_before - before;
1744 uint32_t cut = abs_after - after;
1749 mydata_len -= (skip + cut);
1750 mydata_offset += skip;
1756 r = Callback(cb_data, mydata, mydata_len, mydata_offset);
1759 if (return_progress) {
1760 *progress_out = (mydata_offset + mydata_len);
1767 const uint64_t last_ack_abs = GetAbsLastAck(stream);
1768 SCLogDebug(
"last_ack_abs %"PRIu64, last_ack_abs);
1771 if (mydata_offset > last_ack_abs) {
1773 *progress_out = last_ack_abs;
1775 *progress_out = (mydata_offset + mydata_len);
1814 const uint64_t re, uint64_t *progress_out,
bool eof)
1820 uint64_t progress = progress_in;
1825 const uint8_t *mydata;
1826 uint32_t mydata_len;
1827 uint64_t mydata_offset = 0;
1829 GetRawBuffer(stream, &mydata, &mydata_len, &iter, progress, &mydata_offset);
1830 if (mydata_len == 0) {
1836 SCLogDebug(
"raw progress %"PRIu64, progress);
1837 SCLogDebug(
"stream %p data in buffer %p of len %u and offset %"PRIu64,
1838 stream, &stream->
sb, mydata_len, progress);
1843 if (re < progress) {
1848 SCLogDebug(
"re %" PRIu64
", raw_progress %" PRIu64, re, progress);
1849 SCLogDebug(
"raw_progress + mydata_len %" PRIu64
", re %" PRIu64, progress + mydata_len,
1853 if (progress + mydata_len > re) {
1854 #ifdef DEBUG_VALIDATION
1855 uint32_t check = mydata_len;
1857 mydata_len = (uint32_t)(re - progress);
1859 SCLogDebug(
"data len adjusted to %u to make sure only ACK'd "
1860 "data is considered", mydata_len);
1863 if (mydata_len == 0)
1866 SCLogDebug(
"data %p len %u", mydata, mydata_len);
1869 r = Callback(cb_data, mydata, mydata_len, mydata_offset);
1872 if (mydata_offset == progress) {
1873 SCLogDebug(
"progress %"PRIu64
" increasing with data len %u to %"PRIu64,
1874 progress, mydata_len, progress_in + mydata_len);
1876 progress += mydata_len;
1877 SCLogDebug(
"raw progress now %"PRIu64, progress);
1880 }
else if (mydata_offset > progress && mydata_offset < re) {
1881 SCLogDebug(
"GAP: data is missing from %"PRIu64
" (%u bytes), setting to first data we have: %"PRIu64, progress, (uint32_t)(mydata_offset - progress), mydata_offset);
1883 progress = mydata_offset;
1884 SCLogDebug(
"raw progress now %"PRIu64, progress);
1888 }
else if (mydata_offset > progress && mydata_offset == re) {
1889 SCLogDebug(
"mydata_offset %" PRIu64
", progress %" PRIu64
", re %" PRIu64,
1890 mydata_offset, progress, re);
1893 SCLogDebug(
"not increasing progress, data gap => mydata_offset "
1894 "%"PRIu64
" != progress %"PRIu64, mydata_offset, progress);
1897 if (iter == NULL || r == 1)
1901 *progress_out = progress;
1906 void *cb_data,
const uint64_t
offset,
const bool eof)
1910 SCLogDebug(
"app_progress %" PRIu64, app_progress);
1912 uint64_t unused = 0;
1913 return StreamReassembleRawDo(
1914 ssn, stream, Callback, cb_data,
offset, app_progress, &unused, eof);
1919 uint64_t *progress_out,
bool respect_inspect_depth)
1923 return StreamReassembleRawInline(
ssn,
p, Callback, cb_data, progress_out);
1934 StreamTcpReassembleRawCheckLimit(
ssn, stream,
p) == 0)
1946 SCLogDebug(
"respect_inspect_depth %s STREAMTCP_STREAM_FLAG_TRIGGER_RAW %s "
1947 "stream->min_inspect_depth %u",
1948 respect_inspect_depth ?
"true" :
"false",
1965 SCLogDebug(
"applied min inspect depth due to STREAMTCP_STREAM_FLAG_TRIGGER_RAW: progress "
1975 const uint64_t last_ack_abs = GetAbsLastAck(stream);
1976 SCLogDebug(
"last_ack_abs %" PRIu64, last_ack_abs);
1978 return StreamReassembleRawDo(
ssn, stream, Callback, cb_data, progress, last_ack_abs,
1984 uint64_t *progress_out,
const bool eof)
1990 const uint64_t last_ack_abs = GetAbsLastAck(stream);
1991 SCLogDebug(
"last_ack_abs %" PRIu64, last_ack_abs);
1993 return StreamReassembleRawDo(
1994 ssn, stream, Callback, cb_data, progress_in, last_ack_abs, progress_out, eof);
2002 static int StreamTcpReassembleHandleSegmentUpdateACK (
ThreadVars *
tv,
2019 const TCPHdr *tcph = PacketGetTCP(
p);
2021 SCLogDebug(
"ssn %p, stream %p, p %p, p->payload_len %"PRIu16
"",
2059 if (StreamTcpReassembleHandleSegmentUpdateACK(
tv, ra_ctx,
ssn, opposing_stream,
p) != 0) {
2060 SCLogDebug(
"StreamTcpReassembleHandleSegmentUpdateACK error");
2068 if (reversed_before_ack_handling != reversed_after_ack_handling) {
2070 stream = opposing_stream;
2076 SCLogDebug(
"calling StreamTcpReassembleHandleSegmentHandleData");
2079 SCLogDebug(
"StreamTcpReassembleHandleSegmentHandleData error");
2083 StreamTcpReassembleExceptionPolicyStatsIncr(
2091 SCLogDebug(
"ssn %p / stream %p: not calling StreamTcpReassembleHandleSegmentHandleData:"
2092 " p->payload_len %u, STREAMTCP_STREAM_FLAG_NOREASSEMBLY %s",
2102 if ((stream->
flags &
2106 SCLogDebug(
"STREAMTCP_STREAM_FLAG_DEPTH_REACHED, truncate applayer");
2108 SCLogDebug(
"override: direction now UPDATE_DIR_PACKET so we "
2109 "can trigger Truncate");
2117 SCLogDebug(
"inline (%s) or PKT_PSEUDO_STREAM_END (%s)",
2175 if (direction == STREAM_TOSERVER) {
2181 SCLogDebug(
"flagged ssn %p for immediate raw reassembly",
ssn);
2192 if (direction == STREAM_TOSERVER) {
2194 SCLogDebug(
"ssn %p: set client.min_inspect_depth to %u",
ssn, depth);
2197 SCLogDebug(
"ssn %p: set server.min_inspect_depth to %u",
ssn, depth);
2205 #define SET_ISN(stream, setseq) \
2206 (stream)->isn = (setseq); \
2207 (stream)->base_seq = (setseq) + 1
2225 for (; i <
len; i++)
2239 data, data_len) == 0)
2248 #define MISSED_START(isn) \
2249 TcpReassemblyThreadCtx *ra_ctx = NULL; \
2252 memset(&tv, 0, sizeof(tv)); \
2254 StreamTcpUTInit(&ra_ctx); \
2256 StreamTcpUTSetupSession(&ssn); \
2257 StreamTcpUTSetupStream(&ssn.server, (isn)); \
2258 StreamTcpUTSetupStream(&ssn.client, (isn)); \
2260 TcpStream *stream = &ssn.client;
2262 #define MISSED_END \
2263 StreamTcpUTClearSession(&ssn); \
2264 StreamTcpUTDeinit(ra_ctx); \
2267 #define MISSED_STEP(seq, seg, seglen, buf, buflen) \
2268 StreamTcpUTAddPayload(&tv, ra_ctx, &ssn, stream, (seq), (uint8_t *)(seg), (seglen)); \
2269 FAIL_IF(!(VALIDATE(stream, (uint8_t *)(buf), (buflen))));
2271 #define MISSED_ADD_PAYLOAD(seq, seg, seglen) \
2272 StreamTcpUTAddPayload(&tv, ra_ctx, &ssn, stream, (seq), (uint8_t *)(seg), (seglen));
2279 uint64_t last_re = 0;
2283 if (sbb->
offset != last_re) {
2303 uint64_t last_re = 0;
2307 if (sbb->
offset != last_re) {
2313 const uint8_t *buf = NULL;
2314 uint32_t buf_len = 0;
2317 if (
len == buf_len) {
2318 return (memcmp(data, buf,
len) == 0);
2335 static int StreamTcpReassembleTest25 (
void)
2356 static int StreamTcpReassembleTest26 (
void)
2375 static int StreamTcpReassembleTest27 (
void)
2392 static int StreamTcpReassembleTest28 (
void)
2416 static int StreamTcpReassembleTest29 (
void)
2428 static int StreamTcpReassembleTest33(
void)
2437 uint8_t packet[1460] =
"";
2442 memset(&
f, 0,
sizeof (
Flow));
2443 memset(&tcph, 0,
sizeof (
TCPHdr));
2490 static int StreamTcpReassembleTest34(
void)
2499 uint8_t packet[1460] =
"";
2503 memset(&
f, 0,
sizeof (
Flow));
2504 memset(&tcph, 0,
sizeof (
TCPHdr));
2521 tcph.
th_seq = htonl(857961230);
2527 tcph.
th_seq = htonl(857961534);
2533 tcph.
th_seq = htonl(857963582);
2539 tcph.
th_seq = htonl(857960946);
2560 static int StreamTcpReassembleTest39 (
void)
2570 memset (&
f, 0,
sizeof(
Flow));
2572 memset(&stt, 0,
sizeof (stt));
2573 memset(&tcph, 0,
sizeof (
TCPHdr));
2584 tcph.
th_win = htons(5480);
2652 uint8_t request1[] = { 0x47, 0x45, };
2699 uint8_t request2[] = {
2700 0x54, 0x20, 0x2f, 0x69, 0x6e, 0x64,
2701 0x65, 0x78, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x20,
2702 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30,
2703 0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20,
2704 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
2705 0x74, 0x0d, 0x0a, 0x55, 0x73, 0x65, 0x72, 0x2d,
2706 0x41, 0x67, 0x65, 0x6e, 0x74, 0x3a, 0x20, 0x41,
2707 0x70, 0x61, 0x63, 0x68, 0x65, 0x42, 0x65, 0x6e,
2708 0x63, 0x68, 0x2f, 0x32, 0x2e, 0x33, 0x0d, 0x0a,
2709 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20,
2710 0x2a, 0x2f, 0x2a, 0x0d, 0x0a, 0x0d, 0x0a };
2735 uint8_t response[] = {
2736 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31,
2737 0x20, 0x32, 0x30, 0x30, 0x20, 0x4f, 0x4b, 0x0d,
2738 0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20, 0x46,
2739 0x72, 0x69, 0x2c, 0x20, 0x32, 0x33, 0x20, 0x53,
2740 0x65, 0x70, 0x20, 0x32, 0x30, 0x31, 0x31, 0x20,
2741 0x30, 0x36, 0x3a, 0x32, 0x39, 0x3a, 0x33, 0x39,
2742 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a, 0x53, 0x65,
2743 0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x41, 0x70,
2744 0x61, 0x63, 0x68, 0x65, 0x2f, 0x32, 0x2e, 0x32,
2745 0x2e, 0x31, 0x35, 0x20, 0x28, 0x55, 0x6e, 0x69,
2746 0x78, 0x29, 0x20, 0x44, 0x41, 0x56, 0x2f, 0x32,
2747 0x0d, 0x0a, 0x4c, 0x61, 0x73, 0x74, 0x2d, 0x4d,
2748 0x6f, 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x3a,
2749 0x20, 0x54, 0x68, 0x75, 0x2c, 0x20, 0x30, 0x34,
2750 0x20, 0x4e, 0x6f, 0x76, 0x20, 0x32, 0x30, 0x31,
2751 0x30, 0x20, 0x31, 0x35, 0x3a, 0x30, 0x34, 0x3a,
2752 0x34, 0x36, 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a,
2753 0x45, 0x54, 0x61, 0x67, 0x3a, 0x20, 0x22, 0x61,
2754 0x62, 0x38, 0x39, 0x36, 0x35, 0x2d, 0x32, 0x63,
2755 0x2d, 0x34, 0x39, 0x34, 0x33, 0x62, 0x37, 0x61,
2756 0x37, 0x66, 0x37, 0x66, 0x38, 0x30, 0x22, 0x0d,
2757 0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d,
2758 0x52, 0x61, 0x6e, 0x67, 0x65, 0x73, 0x3a, 0x20,
2759 0x62, 0x79, 0x74, 0x65, 0x73, 0x0d, 0x0a, 0x43,
2760 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c,
2761 0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20, 0x34,
2762 0x34, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65,
2763 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x63,
2764 0x6c, 0x6f, 0x73, 0x65, 0x0d, 0x0a, 0x43, 0x6f,
2765 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79,
2766 0x70, 0x65, 0x3a, 0x20, 0x74, 0x65, 0x78, 0x74,
2767 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x0d, 0x0a, 0x58,
2768 0x2d, 0x50, 0x61, 0x64, 0x3a, 0x20, 0x61, 0x76,
2769 0x6f, 0x69, 0x64, 0x20, 0x62, 0x72, 0x6f, 0x77,
2770 0x73, 0x65, 0x72, 0x20, 0x62, 0x75, 0x67, 0x0d,
2771 0x0a, 0x0d, 0x0a, 0x3c, 0x68, 0x74, 0x6d, 0x6c,
2772 0x3e, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x3c,
2773 0x68, 0x31, 0x3e, 0x49, 0x74, 0x20, 0x77, 0x6f,
2774 0x72, 0x6b, 0x73, 0x21, 0x3c, 0x2f, 0x68, 0x31,
2775 0x3e, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e,
2776 0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e };
2802 tcph.
th_ack = htonl(328);
2828 tcph.
th_seq = htonl(328);
2852 tcph.
th_ack = htonl(328);
2877 tcph.
th_seq = htonl(328);
2902 tcph.
th_ack = htonl(328);
2928 tcph.
th_seq = htonl(328);
2953 tcph.
th_ack = htonl(328);
2980 tcph.
th_ack = htonl(175);
2981 tcph.
th_seq = htonl(328);
3008 tcph.
th_ack = htonl(175);
3009 tcph.
th_seq = htonl(328);
3025 tcph.
th_ack = htonl(328);
3026 tcph.
th_seq = htonl(175);
3047 static int StreamTcpReassembleTest40 (
void)
3054 memset(&tcph, 0,
sizeof (
TCPHdr));
3064 uint8_t httpbuf1[] =
"P";
3065 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
3066 uint8_t httpbuf3[] =
"O";
3067 uint32_t httplen3 =
sizeof(httpbuf3) - 1;
3068 uint8_t httpbuf4[] =
"S";
3069 uint32_t httplen4 =
sizeof(httpbuf4) - 1;
3070 uint8_t httpbuf5[] =
"T \r\n";
3071 uint32_t httplen5 =
sizeof(httpbuf5) - 1;
3073 uint8_t httpbuf2[] =
"HTTP/1.0 200 OK\r\nServer: VictorServer/1.0\r\n\r\n";
3074 uint32_t httplen2 =
sizeof(httpbuf2) - 1;
3087 tcph.
th_win = htons(5480);
3139 tcph.
th_ack = htonl(100);
3148 tcph.
th_seq = htonl(100);
3159 tcph.
th_ack = htonl(145);
3168 tcph.
th_seq = htonl(145);
3185 static int StreamTcpReassembleTest44(
void)
3189 StreamTcpReassembleIncrMemuse(500);
3191 StreamTcpReassembleDecrMemuse(500);
3205 static int StreamTcpReassembleTestMemusePcapHdrStorage(
void)
3209 const int dump_enabled = g_tcp_session_dump_enabled;
3210 g_tcp_session_dump_enabled = 1;
3221 TcpSegmentPoolCleanup(seg);
3226 g_tcp_session_dump_enabled = dump_enabled;
3235 static int StreamTcpReassembleTestPcapHdrStorageInitFail(
void)
3239 const int dump_enabled = g_tcp_session_dump_enabled;
3240 g_tcp_session_dump_enabled = 1;
3248 bool storage_freed =
false;
3251 init_ret = TcpSegmentPoolInit(seg);
3260 g_tcp_session_dump_enabled = dump_enabled;
3276 static int StreamTcpReassembleTest45 (
void)
3281 memset(&
tv, 0,
sizeof(
tv));
3282 uint8_t payload[100] = {0};
3283 uint16_t payload_size = 100;
3314 static int StreamTcpReassembleTest46 (
void)
3319 memset(&
tv, 0,
sizeof(
tv));
3320 uint8_t payload[100] = {0};
3321 uint16_t payload_size = 100;
3352 static int StreamTcpReassembleTest47 (
void)
3360 memset(&tcph, 0,
sizeof (
TCPHdr));
3367 uint8_t httpbuf1[] =
"GET /EVILSUFF HTTP/1.1\r\n\r\n";
3368 uint32_t httplen1 =
sizeof(httpbuf1) - 1;
3382 tcph.
th_win = htons(5480);
3389 tcph.
th_ack = htonl(572799782UL);
3401 tcph.
th_seq = htonl(572799782UL);
3420 static int StreamTcpReassembleInlineTest01(
void)
3427 memset(&
tv, 0x00,
sizeof(
tv));
3435 uint8_t payload[] = {
'C',
'C',
'C',
'C',
'C' };
3455 static int StreamTcpReassembleInlineTest02(
void)
3462 memset(&
tv, 0x00,
sizeof(
tv));
3470 uint8_t payload[] = {
'C',
'C',
'C',
'C',
'C' };
3493 static int StreamTcpReassembleInlineTest03(
void)
3500 memset(&
tv, 0x00,
sizeof(
tv));
3510 uint8_t payload[] = {
'C',
'C',
'C',
'C',
'C' };
3536 static int StreamTcpReassembleInlineTest04(
void)
3543 memset(&
tv, 0x00,
sizeof(
tv));
3553 uint8_t payload[] = {
'C',
'C',
'C',
'C',
'C' };
3580 static int StreamTcpReassembleInlineTest08(
void)
3584 memset(&
tv, 0x00,
sizeof(
tv));
3596 uint8_t payload[] = {
'C',
'C',
'C',
'C',
'C' };
3628 static int StreamTcpReassembleInlineTest09(
void)
3635 memset(&
tv, 0x00,
sizeof(
tv));
3645 uint8_t payload[] = {
'C',
'C',
'C',
'C',
'C' };
3677 static int StreamTcpReassembleInlineTest10(
void)
3684 memset(&
tv, 0x00,
sizeof(
tv));
3700 uint8_t stream_payload1[] =
"GE";
3701 uint8_t stream_payload2[] =
"T /";
3702 uint8_t stream_payload3[] =
"HTTP/1.0\r\n\r\n";
3738 static int StreamTcpReassembleInsertTest01(
void)
3745 memset(&
tv, 0x00,
sizeof(
tv));
3753 uint8_t payload[] = {
'C',
'C',
'C',
'C',
'C' };
3775 static int StreamTcpReassembleInsertTest02(
void)
3781 memset(&
tv, 0x00,
sizeof(
tv));
3788 for (i = 2; i < 10; i++) {
3809 static int StreamTcpReassembleInsertTest03(
void)
3815 memset(&
tv, 0x00,
sizeof(
tv));
3824 for (i = 2; i < 10; i++) {
3851 UtRegisterTest(
"StreamTcpReassembleTest25 -- Gap at Start Reassembly Test",
3852 StreamTcpReassembleTest25);
3853 UtRegisterTest(
"StreamTcpReassembleTest26 -- Gap at middle Reassembly Test",
3854 StreamTcpReassembleTest26);
3856 "StreamTcpReassembleTest27 -- Gap at after Reassembly Test", StreamTcpReassembleTest27);
3857 UtRegisterTest(
"StreamTcpReassembleTest28 -- Gap at Start IDS missed packet Reassembly Test",
3858 StreamTcpReassembleTest28);
3859 UtRegisterTest(
"StreamTcpReassembleTest29 -- Gap at Middle IDS missed packet Reassembly Test",
3860 StreamTcpReassembleTest29);
3862 StreamTcpReassembleTest33);
3864 StreamTcpReassembleTest34);
3866 StreamTcpReassembleTest39);
3868 StreamTcpReassembleTest40);
3870 StreamTcpReassembleTest44);
3871 UtRegisterTest(
"StreamTcpReassembleTestMemusePcapHdrStorage -- pcap hdr storage memuse",
3872 StreamTcpReassembleTestMemusePcapHdrStorage);
3873 UtRegisterTest(
"StreamTcpReassembleTestPcapHdrStorageInitFail -- pcap hdr storage init fail",
3874 StreamTcpReassembleTestPcapHdrStorageInitFail);
3876 StreamTcpReassembleTest45);
3878 StreamTcpReassembleTest46);
3879 UtRegisterTest(
"StreamTcpReassembleTest47 -- TCP Sequence Wraparound Test",
3880 StreamTcpReassembleTest47);
3882 UtRegisterTest(
"StreamTcpReassembleInlineTest01 -- inline RAW ra",
3883 StreamTcpReassembleInlineTest01);
3884 UtRegisterTest(
"StreamTcpReassembleInlineTest02 -- inline RAW ra 2",
3885 StreamTcpReassembleInlineTest02);
3886 UtRegisterTest(
"StreamTcpReassembleInlineTest03 -- inline RAW ra 3",
3887 StreamTcpReassembleInlineTest03);
3888 UtRegisterTest(
"StreamTcpReassembleInlineTest04 -- inline RAW ra 4",
3889 StreamTcpReassembleInlineTest04);
3890 UtRegisterTest(
"StreamTcpReassembleInlineTest08 -- inline RAW ra 8 cleanup",
3891 StreamTcpReassembleInlineTest08);
3892 UtRegisterTest(
"StreamTcpReassembleInlineTest09 -- inline RAW ra 9 GAP cleanup",
3893 StreamTcpReassembleInlineTest09);
3895 UtRegisterTest(
"StreamTcpReassembleInlineTest10 -- inline APP ra 10",
3896 StreamTcpReassembleInlineTest10);
3898 UtRegisterTest(
"StreamTcpReassembleInsertTest01 -- insert with overlap",
3899 StreamTcpReassembleInsertTest01);
3900 UtRegisterTest(
"StreamTcpReassembleInsertTest02 -- insert with overlap",
3901 StreamTcpReassembleInsertTest02);
3902 UtRegisterTest(
"StreamTcpReassembleInsertTest03 -- insert with overlap",
3903 StreamTcpReassembleInsertTest03);
3908 StreamTcpReassembleRawRegisterTests();