85 #define STREAMTCP_DEFAULT_PREALLOC 2048
86 #define STREAMTCP_DEFAULT_MEMCAP (64 * 1024 * 1024)
87 #define STREAMTCP_DEFAULT_REASSEMBLY_MEMCAP (256 * 1024 * 1024)
88 #define STREAMTCP_DEFAULT_TOSERVER_CHUNK_SIZE 2560
89 #define STREAMTCP_DEFAULT_TOCLIENT_CHUNK_SIZE 2560
90 #define STREAMTCP_DEFAULT_MAX_SYN_QUEUED 10
91 #define STREAMTCP_DEFAULT_MAX_SYNACK_QUEUED 5
106 .valid_settings_ips = {
132 .valid_settings_ips = {
158 .valid_settings_ips = {
184 .valid_settings_ips = {
207 static int StreamTcpStateDispatch(
216 static uint64_t ssn_pool_cnt = 0;
236 #if defined(DEBUG_VALIDATION) && defined(UNITTESTS)
239 BUG_ON(presize > UINT_MAX);
245 #if defined(DEBUG_VALIDATION) && defined(UNITTESTS)
248 BUG_ON(postsize > presize);
269 if (memcapcopy == 0 || size +
SC_ATOMIC_GET(st_memuse) <= memcapcopy)
281 if (size == 0 || (uint64_t)
SC_ATOMIC_GET(st_memuse) < size) {
302 if (stream != NULL) {
309 static void StreamTcp3wsFreeQueue(
TcpSession *ssn)
336 StreamTcp3wsFreeQueue(ssn);
397 static void *StreamTcpSessionPoolAlloc(
void)
411 static int StreamTcpSessionPoolInit(
void *data,
void* initdata)
421 static void StreamTcpSessionPoolCleanup(
void *s)
435 static inline bool StreamTcpInlineDropInvalid(
void)
446 static inline bool StreamTcpInlineDropUrg(
void)
455 static int RandomGetWrap(
void)
461 }
while(r >= ULONG_MAX - (ULONG_MAX % RAND_MAX));
491 uint16_t rdrange = 10;
500 if ((
ConfGetInt(
"stream.max-sessions", &value)) == 1) {
502 "Number of concurrent sessions is now only limited by Flow and "
503 "TCP stream engine memcaps.");
506 if ((
ConfGetInt(
"stream.prealloc-sessions", &value)) == 1) {
513 if (
ConfGetNode(
"stream.prealloc-sessions") != NULL) {
521 SCLogConfig(
"stream \"prealloc-sessions\": %"PRIu32
" (per thread)",
525 const char *temp_stream_memcap_str;
526 if (
ConfGet(
"stream.memcap", &temp_stream_memcap_str) == 1) {
527 uint64_t stream_memcap_copy;
530 "from conf file - %s. Killing engine",
531 temp_stream_memcap_str);
545 (void)
ConfGetBool(
"stream.midstream", &imidstream);
553 (void)
ConfGetBool(
"stream.async-oneside", &async_oneside);
562 if ((
ConfGetBool(
"stream.checksum-validation", &csum)) == 1) {
574 "enabled" :
"disabled");
577 const char *temp_stream_inline_str;
578 if (
ConfGet(
"stream.inline", &temp_stream_inline_str) == 1) {
583 if (strcmp(temp_stream_inline_str,
"auto") == 0) {
587 }
else if (
ConfGetBool(
"stream.inline", &inl) == 1) {
606 ?
"enabled" :
"disabled");
610 if ((
ConfGetBool(
"stream.bypass", &bypass)) == 1) {
619 ?
"enabled" :
"disabled");
622 int drop_invalid = 0;
623 if ((
ConfGetBool(
"stream.drop-invalid", &drop_invalid)) == 1) {
624 if (drop_invalid == 1) {
631 const char *temp_urgpol = NULL;
632 if (
ConfGet(
"stream.reassembly.urgent.policy", &temp_urgpol) == 1 && temp_urgpol != NULL) {
633 if (strcmp(temp_urgpol,
"inline") == 0) {
635 }
else if (strcmp(temp_urgpol,
"drop") == 0) {
637 }
else if (strcmp(temp_urgpol,
"oob") == 0) {
639 }
else if (strcmp(temp_urgpol,
"gap") == 0) {
642 FatalError(
"stream.reassembly.urgent.policy: invalid value '%s'", temp_urgpol);
651 const char *temp_urgoobpol = NULL;
652 if (
ConfGet(
"stream.reassembly.urgent.oob-limit-policy", &temp_urgoobpol) == 1 &&
653 temp_urgoobpol != NULL) {
654 if (strcmp(temp_urgoobpol,
"inline") == 0) {
656 }
else if (strcmp(temp_urgoobpol,
"drop") == 0) {
658 }
else if (strcmp(temp_urgoobpol,
"gap") == 0) {
661 FatalError(
"stream.reassembly.urgent.oob-limit-policy: invalid value '%s'", temp_urgoobpol);
671 if ((
ConfGetInt(
"stream.max-syn-queued", &value)) == 1) {
672 if (value >= 0 && value <= 255) {
684 if ((
ConfGetInt(
"stream.max-synack-queued", &value)) == 1) {
685 if (value >= 0 && value <= 255) {
697 const char *temp_stream_reassembly_memcap_str;
698 if (
ConfGet(
"stream.reassembly.memcap", &temp_stream_reassembly_memcap_str) == 1) {
699 uint64_t stream_reassembly_memcap_copy;
701 &stream_reassembly_memcap_copy) < 0) {
703 "stream.reassembly.memcap "
704 "from conf file - %s. Killing engine",
705 temp_stream_reassembly_memcap_str);
715 SCLogConfig(
"stream.reassembly \"memcap\": %"PRIu64
"",
719 const char *temp_stream_reassembly_depth_str;
720 if (
ConfGet(
"stream.reassembly.depth", &temp_stream_reassembly_depth_str) == 1) {
724 "stream.reassembly.depth "
725 "from conf file - %s. Killing engine",
726 temp_stream_reassembly_depth_str);
738 if ((
ConfGetBool(
"stream.reassembly.randomize-chunk-size", &randomize)) == 0) {
746 const char *temp_rdrange;
747 if (
ConfGet(
"stream.reassembly.randomize-chunk-range", &temp_rdrange) == 1) {
750 "stream.reassembly.randomize-chunk-range "
751 "from conf file - %s. Killing engine",
754 }
else if (rdrange >= 100) {
755 FatalError(
"stream.reassembly.randomize-chunk-range "
756 "must be lower than 100");
761 const char *temp_stream_reassembly_toserver_chunk_size_str;
762 if (
ConfGet(
"stream.reassembly.toserver-chunk-size",
763 &temp_stream_reassembly_toserver_chunk_size_str) == 1) {
767 "stream.reassembly.toserver-chunk-size "
768 "from conf file - %s. Killing engine",
769 temp_stream_reassembly_toserver_chunk_size_str);
778 long int r = RandomGetWrap();
783 const char *temp_stream_reassembly_toclient_chunk_size_str;
784 if (
ConfGet(
"stream.reassembly.toclient-chunk-size",
785 &temp_stream_reassembly_toclient_chunk_size_str) == 1) {
789 "stream.reassembly.toclient-chunk-size "
790 "from conf file - %s. Killing engine",
791 temp_stream_reassembly_toclient_chunk_size_str);
800 long int r = RandomGetWrap();
806 SCLogConfig(
"stream.reassembly \"toserver-chunk-size\": %"PRIu16,
808 SCLogConfig(
"stream.reassembly \"toclient-chunk-size\": %"PRIu16,
813 if (
ConfGetBool(
"stream.reassembly.raw", &enable_raw) == 1) {
821 SCLogConfig(
"stream.reassembly.raw: %s", enable_raw ?
"enabled" :
"disabled");
825 int liberal_timestamps = 0;
826 if (
ConfGetBool(
"stream.liberal-timestamps", &liberal_timestamps) == 1) {
830 SCLogConfig(
"stream.liberal-timestamps: %s", liberal_timestamps ?
"enabled" :
"disabled");
850 StreamTcpSessionPoolAlloc,
851 StreamTcpSessionPoolInit, NULL,
852 StreamTcpSessionPoolCleanup, NULL);
871 SCLogDebug(
"ssn_pool_cnt %"PRIu64
"", ssn_pool_cnt);
874 static bool IsReassemblyMemcapExceptionPolicyStatsValid(
enum ExceptionPolicy exception_policy)
882 static bool IsStreamTcpSessionMemcapExceptionPolicyStatsValid(
enum ExceptionPolicy policy)
890 static void StreamTcpSsnMemcapExceptionPolicyStatsIncr(
934 if (
unlikely((g_eps_stream_ssn_memcap != UINT64_MAX &&
935 g_eps_stream_ssn_memcap ==
t_pcapcnt))) {
950 const TCPHdr *tcph = PacketGetTCP(p);
1009 if (PacketIsIPv4(p)) {
1019 }
else if (PacketIsIPv6(p)) {
1045 #define StreamTcpUpdateLastAck(ssn, stream, ack) { \
1046 if (SEQ_GT((ack), (stream)->last_ack)) \
1048 SCLogDebug("ssn %p: last_ack set to %"PRIu32", moved %u forward", (ssn), (ack), (ack) - (stream)->last_ack); \
1049 if ((SEQ_LEQ((stream)->last_ack, (stream)->next_seq) && SEQ_GT((ack),(stream)->next_seq))) { \
1050 SCLogDebug("last_ack just passed next_seq: %u (was %u) > %u", (ack), (stream)->last_ack, (stream)->next_seq); \
1052 SCLogDebug("next_seq (%u) <> last_ack now %d", (stream)->next_seq, (int)(stream)->next_seq - (ack)); \
1054 (stream)->last_ack = (ack); \
1055 StreamTcpSackPruneList((stream)); \
1057 SCLogDebug("ssn %p: no update: ack %u, last_ack %"PRIu32", next_seq %u (state %u)", \
1058 (ssn), (ack), (stream)->last_ack, (stream)->next_seq, (ssn)->state); \
1062 #define StreamTcpAsyncLastAckUpdate(ssn, stream) { \
1063 if ((ssn)->flags & STREAMTCP_FLAG_ASYNC) { \
1064 if (SEQ_GT((stream)->next_seq, (stream)->last_ack)) { \
1065 uint32_t ack_diff = (stream)->next_seq - (stream)->last_ack; \
1066 (stream)->last_ack += ack_diff; \
1067 SCLogDebug("ssn %p: ASYNC last_ack set to %"PRIu32", moved %u forward", \
1068 (ssn), (stream)->next_seq, ack_diff); \
1073 #define StreamTcpUpdateNextSeq(ssn, stream, seq) { \
1074 (stream)->next_seq = seq; \
1075 SCLogDebug("ssn %p: next_seq %" PRIu32, (ssn), (stream)->next_seq); \
1076 StreamTcpAsyncLastAckUpdate((ssn), (stream)); \
1086 #define StreamTcpUpdateNextWin(ssn, stream, win) { \
1087 uint32_t sacked_size__ = StreamTcpSackedSize((stream)); \
1088 if (SEQ_GT(((win) + sacked_size__), (stream)->next_win)) { \
1089 (stream)->next_win = ((win) + sacked_size__); \
1090 SCLogDebug("ssn %p: next_win set to %"PRIu32, (ssn), (stream)->next_win); \
1098 SCLogDebug(
"ssn %p: (state: %s) Reset received and state changed to "
1102 static bool IsMidstreamExceptionPolicyStatsValid(
enum ExceptionPolicy policy)
1116 static void StreamTcpMidstreamExceptionPolicyStatsIncr(
1125 static int StreamTcpPacketIsRetransmission(
TcpStream *stream,
Packet *p)
1130 const TCPHdr *tcph = PacketGetTCP(p);
1168 static int StreamTcpPacketStateNone(
1171 const TCPHdr *tcph = PacketGetTCP(p);
1174 SCLogDebug(
"RST packet received, no session setup");
1184 SCLogDebug(
"FIN packet received, no session setup");
1190 SCLogDebug(
"FIN packet received, no session setup");
1207 SCLogDebug(
"ssn %p: =~ midstream picked ssn state is now "
1239 SCLogDebug(
"ssn %p: ssn->client.next_win %" PRIu32
", "
1240 "ssn->server.next_win %" PRIu32
"",
1242 SCLogDebug(
"ssn %p: ssn->client.last_ack %" PRIu32
", "
1243 "ssn->server.last_ack %" PRIu32
"",
1251 SCLogDebug(
"ssn %p: ssn->server.last_ts %" PRIu32
" "
1252 "ssn->client.last_ts %" PRIu32
"",
1271 SCLogDebug(
"ssn %p: assuming SACK permitted for both sides", ssn);
1280 SCLogDebug(
"Midstream not enabled, so won't pick up a session");
1285 SCLogDebug(
"Midstream policy not permissive, so won't pick up a session");
1308 SCLogDebug(
"ssn %p: =~ midstream picked ssn state is now "
1309 "TCP_SYN_RECV", ssn);
1340 SCLogDebug(
"ssn %p: wscale enabled. client %u server %u",
1344 SCLogDebug(
"ssn %p: ssn->client.isn %"PRIu32
", ssn->client.next_seq"
1345 " %"PRIu32
", ssn->client.last_ack %"PRIu32
"", ssn,
1348 SCLogDebug(
"ssn %p: ssn->server.isn %"PRIu32
", ssn->server.next_seq"
1349 " %"PRIu32
", ssn->server.last_ack %"PRIu32
"", ssn,
1358 SCLogDebug(
"ssn %p: ssn->server.last_ts %" PRIu32
" "
1359 "ssn->client.last_ts %" PRIu32
"", ssn,
1377 SCLogDebug(
"ssn %p: SYN/ACK with SACK permitted, assuming "
1378 "SACK permitted for both sides", ssn);
1396 SCLogDebug(
"ssn %p: =~ ssn state is now TCP_SYN_SENT", ssn);
1429 SCLogDebug(
"ssn %p: SACK permitted on SYN packet", ssn);
1436 SCLogDebug(
"ssn: %p (TFO) isn %u base_seq %u next_seq %u payload len %u", ssn,
1443 SCLogDebug(
"ssn %p: ssn->client.isn %" PRIu32
", "
1444 "ssn->client.next_seq %" PRIu32
", ssn->client.last_ack "
1454 SCLogDebug(
"Midstream not enabled, so won't pick up a session");
1459 SCLogDebug(
"Midstream policy not permissive, so won't pick up a session");
1476 SCLogDebug(
"ssn %p: =~ midstream picked ssn state is now "
1477 "TCP_ESTABLISHED", ssn);
1498 SCLogDebug(
"ssn %p: ssn->client.isn %u, ssn->client.next_seq %u",
1507 SCLogDebug(
"ssn %p: ssn->client.next_win %"PRIu32
", "
1508 "ssn->server.next_win %"PRIu32
"", ssn,
1510 SCLogDebug(
"ssn %p: ssn->client.last_ack %"PRIu32
", "
1511 "ssn->server.last_ack %"PRIu32
"", ssn,
1519 SCLogDebug(
"ssn %p: ssn->server.last_ts %" PRIu32
" "
1520 "ssn->client.last_ts %" PRIu32
"", ssn,
1539 SCLogDebug(
"ssn %p: assuming SACK permitted for both sides", ssn);
1553 const TCPHdr *tcph = PacketGetTCP(p);
1583 StreamTcp3whsSynAckToStateQueue(p, &search);
1591 search.
ts == q->
ts) {
1604 if (StreamTcp3whsFindSynAckBySynAck(ssn, p) != NULL)
1608 SCLogDebug(
"ssn %p: =~ SYN/ACK queue limit reached", ssn);
1614 SCLogDebug(
"ssn %p: =~ SYN/ACK queue failed: stream memcap reached", ssn);
1620 SCLogDebug(
"ssn %p: =~ SYN/ACK queue failed: alloc failed", ssn);
1625 StreamTcp3whsSynAckToStateQueue(p, q);
1639 const TCPHdr *tcph = PacketGetTCP(p);
1674 StreamTcp3whsSynAckToStateQueue(p, &update);
1681 SCLogDebug(
"ssn %p: =~ ssn state is now TCP_SYN_RECV", ssn);
1697 SCLogDebug(
"ssn %p: ssn->server.last_ts %" PRIu32
" "
1698 "ssn->client.last_ts %" PRIu32
"", ssn,
1726 SCLogDebug(
"ssn %p: SACK permitted for session", ssn);
1733 SCLogDebug(
"ssn %p: ssn->server.next_win %" PRIu32
"", ssn,
1735 SCLogDebug(
"ssn %p: ssn->client.next_win %" PRIu32
"", ssn,
1737 SCLogDebug(
"ssn %p: ssn->server.isn %" PRIu32
", "
1738 "ssn->server.next_seq %" PRIu32
", "
1739 "ssn->server.last_ack %" PRIu32
" "
1740 "(ssn->client.last_ack %" PRIu32
")", ssn,
1747 SCLogDebug(
"ssn %p: STREAMTCP_FLAG_4WHS unset, normal SYN/ACK"
1748 " so considering 3WHS", ssn);
1759 static inline bool StateSynSentValidateTimestamp(
TcpSession *ssn,
Packet *p)
1769 if (receiver_stream->
last_ts != 0 && ts_echo != 0 &&
1770 ts_echo != receiver_stream->
last_ts)
1772 SCLogDebug(
"ssn %p: BAD TSECR echo %u recv %u", ssn,
1773 ts_echo, receiver_stream->
last_ts);
1777 if (receiver_stream->
last_ts == 0 && ts_echo != 0) {
1778 SCLogDebug(
"ssn %p: BAD TSECR echo %u recv %u", ssn,
1779 ts_echo, receiver_stream->
last_ts);
1789 memset(q, 0,
sizeof(*q));
1809 SCLogDebug(
"ssn %p: state:%p, isn:%u/win:%u/has_ts:%s/tsval:%u", ssn, q, q->
seq, q->
win,
1815 #if defined(DEBUG_VALIDATION) || defined(DEBUG)
1819 memset(q, 0,
sizeof(*q));
1820 const TCPHdr *tcph = PacketGetTCP(p);
1838 SCLogDebug(
"ssn %p: state:%p, isn:%u/win:%u/has_ts:%s/tsval:%u", ssn, q, q->
seq, q->
win,
1845 #if defined(DEBUG_VALIDATION) || defined(DEBUG)
1852 memset(q, 0,
sizeof(*q));
1854 const TCPHdr *tcph = PacketGetTCP(p);
1871 SCLogDebug(
"ssn %p: state:%p, isn:%u/win:%u/has_ts:%s/tsval:%u", ssn, q, q->
seq, q->
win,
1881 SCLogDebug(
"ssn %p: search state:%p, isn:%u/win:%u/has_ts:%s/tsval:%u", ssn, s, s->
seq, s->
win,
1885 SCLogDebug(
"ssn %p: queue state:%p, isn:%u/win:%u/has_ts:%s/tsval:%u", ssn, q, q->
seq,
1899 TcpStateQueueInitFromSsnSyn(ssn, &search);
1902 if (ssn->
queue != NULL && StreamTcp3whsFindSyn(ssn, &search) != NULL)
1906 SCLogDebug(
"ssn %p: =~ SYN queue limit reached", ssn);
1912 SCLogDebug(
"ssn %p: =~ SYN queue failed: stream memcap reached", ssn);
1918 SCLogDebug(
"ssn %p: =~ SYN queue failed: alloc failed", ssn);
1965 static int StreamTcpPacketStateSynSent(
1969 const TCPHdr *tcph = PacketGetTCP(p);
1975 SCLogDebug(
"ssn %p: SYN/ACK on SYN_SENT state for packet %" PRIu64, ssn, p->
pcap_cnt);
1982 SCLogDebug(
"ssn %p: ACK mismatch, packet ACK %" PRIu32
" != "
1983 "%" PRIu32
" from stream",
1989 SCLogDebug(
"ssn %p: (TFO) ACK matches next_seq, packet ACK %" PRIu32
" == "
1990 "%" PRIu32
" from stream",
1993 SCLogDebug(
"ssn %p: (TFO) ACK matches ISN+1, packet ACK %" PRIu32
" == "
1994 "%" PRIu32
" from stream",
2002 SCLogDebug(
"ssn %p: (TFO) ACK mismatch, packet ACK %" PRIu32
" != "
2003 "%" PRIu32
" from stream",
2011 const bool ts_mismatch = !StateSynSentValidateTimestamp(ssn, p);
2016 TcpStateQueueInitFromPktSynAck(p, &search);
2018 const TcpStateQueue *q = StreamTcp3whsFindSyn(ssn, &search);
2024 SCLogDebug(
"ssn %p: found queued SYN state:%p, isn:%u/win:%u/has_ts:%s/tsval:%u",
2028 StreamTcp3whsStoreSynApplyToSsn(ssn, q);
2038 StreamTcp3wsFreeQueue(ssn);
2040 StreamTcp3whsSynAckUpdate(ssn, p, NULL);
2047 SCLogDebug(
"ssn %p: SYN/ACK received in the wrong direction", ssn);
2051 SCLogDebug(
"ssn %p: SYN/ACK received on 4WHS session", ssn);
2058 SCLogDebug(
"ssn %p: 4WHS ACK mismatch, packet ACK %" PRIu32
""
2059 " != %" PRIu32
" from stream",
2069 SCLogDebug(
"ssn %p: 4WHS SEQ mismatch, packet SEQ %" PRIu32
""
2070 " != %" PRIu32
" from *first* SYN pkt",
2077 SCLogDebug(
"ssn %p: =~ 4WHS ssn state is now TCP_SYN_RECV", ssn);
2091 SCLogDebug(
"ssn %p: 4WHS ssn->client.last_ts %" PRIu32
" "
2092 "ssn->server.last_ts %" PRIu32
"",
2117 SCLogDebug(
"ssn %p: SACK permitted for 4WHS session", ssn);
2124 SCLogDebug(
"ssn %p: 4WHS ssn->client.isn %" PRIu32
", "
2125 "ssn->client.next_seq %" PRIu32
", "
2126 "ssn->client.last_ack %" PRIu32
" "
2127 "(ssn->server.last_ack %" PRIu32
")",
2136 if (StateSynSentValidateTimestamp(ssn, p) ==
false) {
2144 if (!StreamTcpValidateRst(ssn, p))
2151 SCLogDebug(
"ssn->server.flags |= STREAMTCP_STREAM_FLAG_RST_RECV");
2153 StreamTcpCloseSsnWithReset(p, ssn);
2154 StreamTcp3wsFreeQueue(ssn);
2158 SCLogDebug(
"ssn->client.flags |= STREAMTCP_STREAM_FLAG_RST_RECV");
2159 StreamTcpCloseSsnWithReset(p, ssn);
2160 StreamTcp3wsFreeQueue(ssn);
2168 SCLogDebug(
"ssn %p: SYN packet on state SYN_SENT... resent", ssn);
2170 SCLogDebug(
"ssn %p: SYN packet on state SYN_SENT... resent of "
2183 SCLogDebug(
"ssn %p: STREAMTCP_FLAG_4WHS flag set", ssn);
2220 SCLogDebug(
"ssn %p: 4WHS ssn->server.isn %" PRIu32
", "
2221 "ssn->server.next_seq %" PRIu32
", "
2222 "ssn->server.last_ack %"PRIu32
"", ssn,
2225 SCLogDebug(
"ssn %p: 4WHS ssn->client.isn %" PRIu32
", "
2226 "ssn->client.next_seq %" PRIu32
", "
2227 "ssn->client.last_ack %"PRIu32
"", ssn,
2236 TcpStateQueueInitFromPktSyn(p, &syn_pkt);
2237 TcpStateQueueInitFromSsnSyn(ssn, &syn_ssn);
2239 if (memcmp(&syn_pkt, &syn_ssn,
sizeof(
TcpStateQueue)) != 0) {
2241 StreamTcp3whsStoreSyn(ssn, p);
2242 SCLogDebug(
"ssn %p: Retransmitted SYN. Updating ssn from packet %" PRIu64
2243 ". Stored previous state",
2246 StreamTcp3whsStoreSynApplyToSsn(ssn, &syn_pkt);
2262 SCLogDebug(
"ssn %p: SEQ mismatch, packet SEQ %" PRIu32
" != "
2263 "%" PRIu32
" from stream",
2270 SCLogDebug(
"ssn %p: =~ ssn state is now TCP_ESTABLISHED", ssn);
2271 StreamTcp3wsFreeQueue(ssn);
2284 SCLogDebug(
"ssn %p: synsent => Asynchronous stream, packet SEQ"
2285 " %" PRIu32
", payload size %" PRIu32
" (%" PRIu32
"), "
2286 "ssn->client.next_seq %" PRIu32
"",
2335 static int StreamTcpPacketStateSynRecv(
2339 const TCPHdr *tcph = PacketGetTCP(p);
2342 if (!StreamTcpValidateRst(ssn, p))
2359 SCLogDebug(
"Detection evasion has been attempted, so"
2360 " not resetting the connection !!");
2368 SCLogDebug(
"Detection evasion has been attempted, so"
2369 " not resetting the connection !!");
2375 StreamTcpCloseSsnWithReset(p, ssn);
2378 StreamTcpHandleTimestamp(ssn, p);
2385 if (!StreamTcpValidateTimestamp(ssn, p))
2389 if ((StreamTcpHandleFin(
tv, stt, ssn, p)) == -1)
2394 SCLogDebug(
"ssn %p: SYN/ACK packet on state SYN_RECV. resent", ssn);
2397 SCLogDebug(
"ssn %p: SYN/ACK-pkt to server in SYN_RECV state", ssn);
2406 SCLogDebug(
"ssn %p: ACK mismatch, packet ACK %" PRIu32
" != "
2407 "%" PRIu32
" from stream",
2417 SCLogDebug(
"ssn %p: SEQ mismatch, packet SEQ %" PRIu32
" != "
2418 "%" PRIu32
" from stream",
2421 if (StreamTcp3whsQueueSynAck(ssn, p) == -1)
2423 SCLogDebug(
"ssn %p: queued different SYN/ACK", ssn);
2427 SCLogDebug(
"ssn %p: SYN packet on state SYN_RECV... resent", ssn);
2430 SCLogDebug(
"ssn %p: SYN-pkt to client in SYN_RECV state", ssn);
2437 SCLogDebug(
"ssn %p: SYN with different SEQ on SYN_RECV state", ssn);
2445 SCLogDebug(
"ssn %p: checking ACK against queued SYN/ACKs", ssn);
2448 SCLogDebug(
"ssn %p: here we update state against queued SYN/ACK", ssn);
2449 StreamTcp3whsSynAckUpdate(ssn, p, q);
2451 SCLogDebug(
"ssn %p: none found, now checking ACK against original SYN/ACK (state)", ssn);
2461 if (!(StreamTcpValidateTimestamp(ssn, p))) {
2467 SCLogDebug(
"ssn %p: ACK received on 4WHS session",ssn);
2470 SCLogDebug(
"ssn %p: 4WHS wrong seq nr on packet", ssn);
2475 if (StreamTcpValidateAck(ssn, &ssn->
client, p) == -1) {
2476 SCLogDebug(
"ssn %p: 4WHS invalid ack nr on packet", ssn);
2482 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to client: SEQ "
2483 "%" PRIu32
", ACK %" PRIu32
"",
2487 StreamTcpHandleTimestamp(ssn, p);
2496 SCLogDebug(
"ssn %p: =~ ssn state is now TCP_ESTABLISHED", ssn);
2500 SCLogDebug(
"ssn %p: ssn->client.next_win %" PRIu32
", "
2501 "ssn->client.last_ack %"PRIu32
"", ssn,
2506 bool ack_indicates_missed_3whs_ack_packet =
false;
2516 SCLogDebug(
"ssn %p: ACK received on midstream SYN/ACK "
2517 "pickup session",ssn);
2520 SCLogDebug(
"ssn %p: ACK received on TFO session",ssn);
2537 SCLogDebug(
"ssn %p: possible data injection", ssn);
2542 SCLogDebug(
"ssn %p: ACK received in the wrong direction",
2547 ack_indicates_missed_3whs_ack_packet =
true;
2551 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to server: SEQ %" PRIu32
""
2552 ", ACK %" PRIu32
"",
2564 StreamTcpHandleTimestamp(ssn, p);
2588 SCLogDebug(
"ssn %p: =~ ssn state is now TCP_ESTABLISHED", ssn);
2614 StreamTcpHandleTimestamp(ssn, p);
2627 SCLogDebug(
"ssn %p: synrecv => Asynchronous stream, packet SEQ"
2628 " %" PRIu32
", payload size %" PRIu32
" (%" PRIu32
"), "
2629 "ssn->server.next_seq %" PRIu32,
2634 SCLogDebug(
"ssn %p: =~ ssn state is now TCP_ESTABLISHED", ssn);
2643 SCLogDebug(
"ssn %p: wrong ack nr on packet, possible evasion!!",
2653 SCLogDebug(
"ssn %p: ACK for missing data", ssn);
2656 StreamTcpHandleTimestamp(ssn, p);
2662 SCLogDebug(
"ssn %p: ACK for missing data: ssn->server.next_seq %u", ssn,
2672 SCLogDebug(
"ssn %p: =~ ssn state is now TCP_ESTABLISHED", ssn);
2681 SCLogDebug(
"ssn %p: ACK for missing data", ssn);
2684 StreamTcpHandleTimestamp(ssn, p);
2706 SCLogDebug(
"ssn %p: =~ ssn state is now TCP_ESTABLISHED", ssn);
2711 }
else if ((ack_indicates_missed_3whs_ack_packet ||
2715 if (ack_indicates_missed_3whs_ack_packet) {
2716 SCLogDebug(
"ssn %p: packet fits perfectly after a missed 3whs-ACK", ssn);
2718 SCLogDebug(
"ssn %p: (TFO) expected packet fits perfectly after SYN/ACK", ssn);
2727 SCLogDebug(
"ssn %p: =~ ssn state is now TCP_ESTABLISHED", ssn);
2732 SCLogDebug(
"ssn %p: wrong seq nr on packet", ssn);
2738 SCLogDebug(
"ssn %p: ssn->server.next_win %" PRIu32
", "
2739 "ssn->server.last_ack %"PRIu32
"", ssn,
2761 static int HandleEstablishedPacketToServer(
2764 const TCPHdr *tcph = PacketGetTCP(p);
2769 SCLogDebug(
"ssn %p: =+ pkt (%" PRIu32
") is to server: SEQ %" PRIu32
","
2770 "ACK %" PRIu32
", WIN %" PRIu16
"",
2776 SCLogDebug(
"ssn %p: accepting ACK as it ACKs the one byte from the ZWP", ssn);
2779 }
else if (StreamTcpValidateAck(ssn, &ssn->
server, p) == -1) {
2780 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
2788 SCLogDebug(
"ssn %p: pkt is keep alive", ssn);
2793 SCLogDebug(
"ssn %p: server => Asynchronous stream, packet SEQ"
2794 " %" PRIu32
", payload size %" PRIu32
" (%" PRIu32
"),"
2795 " ssn->client.last_ack %" PRIu32
", ssn->client.next_win"
2796 "%" PRIu32
"(%" PRIu32
")",
2806 SCLogDebug(
"ssn %p: server => Asynchronous stream, packet SEQ."
2807 " %" PRIu32
", payload size %" PRIu32
" (%" PRIu32
"), "
2808 "ssn->client.last_ack %" PRIu32
", ssn->client.next_win "
2809 "%" PRIu32
"(%" PRIu32
")",
2821 SCLogDebug(
"ssn %p: server => Asynchronous stream, packet SEQ"
2822 " %" PRIu32
", payload size %" PRIu32
" (%" PRIu32
"), "
2823 "ssn->client.last_ack %" PRIu32
", ssn->client.next_win "
2824 "%" PRIu32
"(%" PRIu32
")",
2839 SCLogDebug(
"ssn %p: PKT SEQ %" PRIu32
" payload_len %" PRIu16
2840 " before last_ack %" PRIu32
", after next_seq %" PRIu32
":"
2841 " acked data that we haven't seen before",
2844 SCLogDebug(
"ssn %p: server => SEQ before last_ack, packet SEQ"
2845 " %" PRIu32
", payload size %" PRIu32
" (%" PRIu32
"), "
2846 "ssn->client.last_ack %" PRIu32
", ssn->client.next_win "
2847 "%" PRIu32
"(%" PRIu32
")",
2851 SCLogDebug(
"ssn %p: rejecting because pkt before last_ack", ssn);
2857 int zerowindowprobe = 0;
2860 SCLogDebug(
"ssn %p: zero window probe", ssn);
2861 zerowindowprobe = 1;
2871 if (zerowindowprobe) {
2872 SCLogDebug(
"ssn %p: zero window probe, skipping oow check", ssn);
2875 SCLogDebug(
"ssn %p: seq %" PRIu32
" in window, ssn->client.next_win "
2880 SCLogDebug(
"ssn %p: ssn->server.window %"PRIu32
"", ssn,
2897 StreamTcpHandleTimestamp(ssn, p);
2909 SCLogDebug(
"ssn %p: toserver => SEQ out of window, packet SEQ "
2910 "%" PRIu32
", payload size %" PRIu32
" (%" PRIu32
"),"
2911 "ssn->client.last_ack %" PRIu32
", ssn->client.next_win "
2912 "%" PRIu32
"(%" PRIu32
")",
2916 StreamTcpSackedSize(&ssn->
client));
2936 static int HandleEstablishedPacketToClient(
2939 const TCPHdr *tcph = PacketGetTCP(p);
2944 SCLogDebug(
"ssn %p: =+ pkt (%" PRIu32
") is to client: SEQ %" PRIu32
","
2945 " ACK %" PRIu32
", WIN %" PRIu16
"",
2951 SCLogDebug(
"ssn %p: accepting ACK as it ACKs the one byte from the ZWP", ssn);
2954 }
else if (StreamTcpValidateAck(ssn, &ssn->
client, p) == -1) {
2955 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
2969 SCLogDebug(
"ssn %p: adjusted midstream ssn->server.next_win to "
2975 SCLogDebug(
"ssn %p: pkt is keep alive", ssn);
2981 SCLogDebug(
"ssn %p: client => Asynchronous stream, packet SEQ"
2982 " %" PRIu32
", payload size %" PRIu32
" (%" PRIu32
"),"
2983 " ssn->client.last_ack %" PRIu32
", ssn->client.next_win"
2984 " %" PRIu32
"(%" PRIu32
")",
2995 SCLogDebug(
"ssn %p: PKT SEQ %" PRIu32
" payload_len %" PRIu16
2996 " before last_ack %" PRIu32
", after next_seq %" PRIu32
":"
2997 " acked data that we haven't seen before",
3000 SCLogDebug(
"ssn %p: PKT SEQ %" PRIu32
" payload_len %" PRIu16
3001 " before last_ack %" PRIu32
". next_seq %" PRIu32,
3008 int zerowindowprobe = 0;
3011 SCLogDebug(
"ssn %p: zero window probe", ssn);
3012 zerowindowprobe = 1;
3023 if (zerowindowprobe) {
3024 SCLogDebug(
"ssn %p: zero window probe, skipping oow check", ssn);
3027 SCLogDebug(
"ssn %p: seq %" PRIu32
" in window, ssn->server.next_win "
3031 SCLogDebug(
"ssn %p: ssn->client.window %"PRIu32
"", ssn,
3044 StreamTcpHandleTimestamp(ssn, p);
3053 SCLogDebug(
"ssn %p: client => SEQ out of window, packet SEQ"
3054 "%" PRIu32
", payload size %" PRIu32
" (%" PRIu32
"),"
3055 " ssn->server.last_ack %" PRIu32
", ssn->server.next_win "
3056 "%" PRIu32
"(%" PRIu32
")",
3065 static bool StreamTcpPacketIsZeroWindowProbeAck(
const TcpSession *ssn,
const Packet *p)
3067 const TCPHdr *tcph = PacketGetTCP(p);
3091 if (pkt_win != rcv->
window)
3098 SCLogDebug(
"ssn %p: packet %" PRIu64
" is a Zero Window Probe ACK", ssn, p->
pcap_cnt);
3105 static bool StreamTcpPacketIsDupAck(
const TcpSession *ssn,
const Packet *p)
3107 const TCPHdr *tcph = PacketGetTCP(p);
3125 if (pkt_win == 0 || rcv->
window == 0)
3127 if (pkt_win != rcv->
window)
3135 SCLogDebug(
"ssn %p: packet:%" PRIu64
" seq:%u ack:%u win:%u snd %u:%u:%u rcv %u:%u:%u", ssn,
3159 const TCPHdr *tcph = PacketGetTCP(p);
3210 static int StreamTcpPacketIsSpuriousRetransmission(
const TcpSession *ssn,
Packet *p)
3221 const TCPHdr *tcph = PacketGetTCP(p);
3227 "ssn %p: spurious retransmission; packet entirely before base_seq: SEQ %u(%u) "
3228 "last_ack %u base_seq %u",
3237 SCLogDebug(
"ssn %p: spurious retransmission; packet entirely before last_ack: SEQ %u(%u) "
3245 SCLogDebug(
"ssn %p: NOT spurious retransmission; packet NOT entirely before last_ack: SEQ "
3246 "%u(%u) last_ack %u, base_seq %u",
3263 static int StreamTcpPacketStateEstablished(
3267 const TCPHdr *tcph = PacketGetTCP(p);
3273 if (!StreamTcpValidateRst(ssn, p))
3277 StreamTcpCloseSsnWithReset(p, ssn);
3281 SCLogDebug(
"ssn %p: ssn->server.next_seq %" PRIu32
"", ssn,
3291 StreamTcpHandleTimestamp(ssn, p);
3295 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
3303 StreamTcpCloseSsnWithReset(p, ssn);
3308 SCLogDebug(
"ssn %p: ssn->server.next_seq %" PRIu32
"", ssn,
3318 StreamTcpHandleTimestamp(ssn, p);
3322 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
3333 if (!StreamTcpValidateTimestamp(ssn, p))
3338 " %" PRIu32
", last ACK %" PRIu32
", next win %"PRIu32
","
3343 if ((StreamTcpHandleFin(
tv, stt, ssn, p)) == -1)
3348 SCLogDebug(
"ssn %p: SYN/ACK packet on state ESTABLISHED... resent",
3352 SCLogDebug(
"ssn %p: SYN/ACK-pkt to server in ESTABLISHED state", ssn);
3361 SCLogDebug(
"ssn %p: ACK mismatch, packet ACK %" PRIu32
" != "
3362 "%" PRIu32
" from stream",
3372 SCLogDebug(
"ssn %p: SEQ mismatch, packet SEQ %" PRIu32
" != "
3373 "%" PRIu32
" from stream",
3386 SCLogDebug(
"ssn %p: SYN/ACK packet on state ESTABLISHED... resent. "
3387 "Likely due server not receiving final ACK in 3whs", ssn);
3391 SCLogDebug(
"ssn %p: SYN packet on state ESTABLISHED... resent", ssn);
3393 SCLogDebug(
"ssn %p: SYN-pkt to client in EST state", ssn);
3400 SCLogDebug(
"ssn %p: SYN with different SEQ on SYN_RECV state", ssn);
3421 if (!StreamTcpValidateTimestamp(ssn, p))
3427 HandleEstablishedPacketToServer(
tv, ssn, p, stt);
3429 SCLogDebug(
"ssn %p: next SEQ %" PRIu32
", last ACK %" PRIu32
","
3430 " next win %" PRIu32
", win %" PRIu32
"", ssn,
3437 SCLogDebug(
"3whs is now confirmed by server");
3441 HandleEstablishedPacketToClient(
tv, ssn, p, stt);
3443 SCLogDebug(
"ssn %p: next SEQ %" PRIu32
", last ACK %" PRIu32
","
3444 " next win %" PRIu32
", win %" PRIu32
"", ssn,
3469 const TCPHdr *tcph = PacketGetTCP(p);
3475 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to server: SEQ %" PRIu32
","
3479 if (StreamTcpValidateAck(ssn, &ssn->
server, p) == -1) {
3480 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
3486 SCLogDebug(
"ssn %p: -> SEQ %u, re %u. last_ack %u next_win %u", ssn,
seq, pkt_re,
3491 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
" != "
3492 "%" PRIu32
" from stream",
3505 SCLogDebug(
"ssn %p: state changed to TCP_CLOSE_WAIT", ssn);
3510 SCLogDebug(
"ssn %p: ssn->client.next_seq %" PRIu32
"", ssn,
3515 StreamTcpHandleTimestamp(ssn, p);
3528 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK %" PRIu32
"",
3531 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to client: SEQ %" PRIu32
", "
3535 if (StreamTcpValidateAck(ssn, &ssn->
client, p) == -1) {
3536 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
3542 SCLogDebug(
"ssn %p: -> SEQ %u, re %u. last_ack %u next_win %u", ssn,
seq, pkt_re,
3547 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
" != "
3548 "%" PRIu32
" from stream (last_ack %u win %u = %u)",
3557 SCLogDebug(
"ssn %p: state changed to TCP_FIN_WAIT1", ssn);
3566 StreamTcpHandleTimestamp(ssn, p);
3579 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK %" PRIu32
"",
3599 static int StreamTcpPacketStateFinWait1(
3603 const TCPHdr *tcph = PacketGetTCP(p);
3609 if (!StreamTcpValidateRst(ssn, p))
3612 StreamTcpCloseSsnWithReset(p, ssn);
3621 StreamTcpHandleTimestamp(ssn, p);
3632 StreamTcpHandleTimestamp(ssn, p);
3640 if (!StreamTcpValidateTimestamp(ssn, p))
3645 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to server: SEQ "
3646 "%" PRIu32
", ACK %" PRIu32
"",
3648 int retransmission = 0;
3650 if (StreamTcpPacketIsRetransmission(&ssn->
client, p)) {
3651 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
3657 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
3658 " != %" PRIu32
" from stream",
3664 if (StreamTcpValidateAck(ssn, &ssn->
server, p) == -1) {
3665 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
3670 if (!retransmission) {
3672 SCLogDebug(
"ssn %p: state changed to TCP_TIME_WAIT", ssn);
3678 StreamTcpHandleTimestamp(ssn, p);
3694 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
3698 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to client: SEQ "
3699 "%" PRIu32
", ACK %" PRIu32
"",
3701 int retransmission = 0;
3703 if (StreamTcpPacketIsRetransmission(&ssn->
server, p)) {
3704 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
3709 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
3715 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
3716 " != %" PRIu32
" from stream",
3722 if (StreamTcpValidateAck(ssn, &ssn->
client, p) == -1) {
3723 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
3729 StreamTcpHandleTimestamp(ssn, p);
3732 if (!retransmission) {
3734 SCLogDebug(
"ssn %p: state changed to TCP_TIME_WAIT", ssn);
3752 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
3759 if (!StreamTcpValidateTimestamp(ssn, p))
3764 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to server: SEQ "
3765 "%" PRIu32
", ACK %" PRIu32
"",
3767 int retransmission = 0;
3769 if (StreamTcpPacketIsRetransmission(&ssn->
client, p)) {
3770 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
3776 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
3777 " != %" PRIu32
" from stream",
3783 if (StreamTcpValidateAck(ssn, &ssn->
server, p) == -1) {
3784 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
3789 if (!retransmission) {
3791 SCLogDebug(
"ssn %p: state changed to TCP_CLOSING", ssn);
3797 StreamTcpHandleTimestamp(ssn, p);
3814 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
3818 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to client: SEQ "
3819 "%" PRIu32
", ACK %" PRIu32
"",
3822 int retransmission = 0;
3824 if (StreamTcpPacketIsRetransmission(&ssn->
server, p)) {
3825 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
3831 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
3832 " != %" PRIu32
" from stream",
3838 if (StreamTcpValidateAck(ssn, &ssn->
client, p) == -1) {
3839 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
3844 if (!retransmission) {
3846 SCLogDebug(
"ssn %p: state changed to TCP_CLOSING", ssn);
3852 StreamTcpHandleTimestamp(ssn, p);
3869 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
3874 SCLogDebug(
"ssn (%p): SYN pkt on FinWait1", ssn);
3880 if (!StreamTcpValidateTimestamp(ssn, p))
3885 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to server: SEQ "
3886 "%" PRIu32
", ACK %" PRIu32
"",
3888 int retransmission = 0;
3890 if (StreamTcpPacketIsRetransmission(&ssn->
client, p)) {
3891 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
3896 if (StreamTcpValidateAck(ssn, &ssn->
server, p) == -1) {
3897 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
3904 "ssn %p: ACK's older segment as %u < %u", ssn, ack, ssn->
server.
next_seq);
3905 }
else if (!retransmission) {
3909 SCLogDebug(
"ssn %p: seq %" PRIu32
" in window, ssn->client.next_win "
3915 SCLogDebug(
"ssn %p: state changed to TCP_FIN_WAIT2", ssn);
3918 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
3919 " != %" PRIu32
" from stream",
3931 StreamTcpHandleTimestamp(ssn, p);
3952 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
3958 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to client: SEQ "
3959 "%" PRIu32
", ACK %" PRIu32
"",
3962 int retransmission = 0;
3964 if (StreamTcpPacketIsRetransmission(&ssn->
server, p)) {
3965 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
3970 if (StreamTcpValidateAck(ssn, &ssn->
client, p) == -1) {
3971 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
3976 if (!retransmission) {
3979 SCLogDebug(
"ssn %p: seq %" PRIu32
" in window, ssn->server.next_win "
3985 SCLogDebug(
"ssn %p: state changed to TCP_FIN_WAIT2", ssn);
3988 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
3989 " != %" PRIu32
" from stream",
3999 StreamTcpHandleTimestamp(ssn, p);
4020 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
4041 static int StreamTcpPacketStateFinWait2(
4045 const TCPHdr *tcph = PacketGetTCP(p);
4051 if (!StreamTcpValidateRst(ssn, p))
4054 StreamTcpCloseSsnWithReset(p, ssn);
4063 StreamTcpHandleTimestamp(ssn, p);
4074 StreamTcpHandleTimestamp(ssn, p);
4082 if (!StreamTcpValidateTimestamp(ssn, p))
4087 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to server: SEQ "
4088 "%" PRIu32
", ACK %" PRIu32
"",
4090 int retransmission = 0;
4096 }
else if (StreamTcpPacketIsRetransmission(&ssn->
client, p)) {
4097 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
4103 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ "
4104 "%" PRIu32
" != %" PRIu32
" from stream",
4110 if (StreamTcpValidateAck(ssn, &ssn->
server, p) == -1) {
4111 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
4116 if (!retransmission) {
4118 SCLogDebug(
"ssn %p: state changed to TCP_TIME_WAIT", ssn);
4128 StreamTcpHandleTimestamp(ssn, p);
4141 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
4145 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to client: SEQ "
4146 "%" PRIu32
", ACK %" PRIu32
"",
4148 int retransmission = 0;
4154 }
else if (StreamTcpPacketIsRetransmission(&ssn->
server, p)) {
4155 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
4161 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ "
4162 "%" PRIu32
" != %" PRIu32
" from stream",
4168 if (StreamTcpValidateAck(ssn, &ssn->
client, p) == -1) {
4169 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
4174 if (!retransmission) {
4176 SCLogDebug(
"ssn %p: state changed to TCP_TIME_WAIT", ssn);
4182 StreamTcpHandleTimestamp(ssn, p);
4194 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
4200 SCLogDebug(
"ssn (%p): SYN pkt on FinWait2", ssn);
4206 if (!StreamTcpValidateTimestamp(ssn, p))
4211 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to server: SEQ "
4212 "%" PRIu32
", ACK %" PRIu32
"",
4214 int retransmission = 0;
4216 if (StreamTcpPacketIsRetransmission(&ssn->
client, p)) {
4217 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
4222 if (StreamTcpValidateAck(ssn, &ssn->
server, p) == -1) {
4223 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
4228 if (!retransmission) {
4231 SCLogDebug(
"ssn %p: seq %" PRIu32
" in window, ssn->client.next_win "
4236 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
4237 " != %" PRIu32
" from stream",
4247 StreamTcpHandleTimestamp(ssn, p);
4263 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
4267 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to client: SEQ "
4268 "%" PRIu32
", ACK %" PRIu32
"",
4270 int retransmission = 0;
4272 if (StreamTcpPacketIsRetransmission(&ssn->
server, p)) {
4273 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
4278 if (StreamTcpValidateAck(ssn, &ssn->
client, p) == -1) {
4279 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
4284 if (!retransmission) {
4287 SCLogDebug(
"ssn %p: seq %" PRIu32
" in window, ssn->server.next_win "
4291 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
4292 " != %" PRIu32
" from stream",
4302 StreamTcpHandleTimestamp(ssn, p);
4318 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
4339 static int StreamTcpPacketStateClosing(
4343 const TCPHdr *tcph = PacketGetTCP(p);
4349 if (!StreamTcpValidateRst(ssn, p))
4352 StreamTcpCloseSsnWithReset(p, ssn);
4361 StreamTcpHandleTimestamp(ssn, p);
4372 StreamTcpHandleTimestamp(ssn, p);
4379 SCLogDebug(
"ssn (%p): SYN pkt on Closing", ssn);
4385 if (!StreamTcpValidateTimestamp(ssn, p))
4390 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to server: SEQ "
4391 "%" PRIu32
", ACK %" PRIu32
"",
4393 int retransmission = 0;
4394 if (StreamTcpPacketIsRetransmission(&ssn->
client, p)) {
4395 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
4401 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
4402 " != %" PRIu32
" from stream",
4408 if (StreamTcpValidateAck(ssn, &ssn->
server, p) == -1) {
4409 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
4414 if (!retransmission) {
4416 SCLogDebug(
"ssn %p: state changed to TCP_TIME_WAIT", ssn);
4422 StreamTcpHandleTimestamp(ssn, p);
4432 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
4436 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to client: SEQ "
4437 "%" PRIu32
", ACK %" PRIu32
"",
4439 int retransmission = 0;
4440 if (StreamTcpPacketIsRetransmission(&ssn->
server, p)) {
4441 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
4447 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
4448 " != %" PRIu32
" from stream",
4454 if (StreamTcpValidateAck(ssn, &ssn->
client, p) == -1) {
4455 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
4460 if (!retransmission) {
4462 SCLogDebug(
"ssn %p: state changed to TCP_TIME_WAIT", ssn);
4468 StreamTcpHandleTimestamp(ssn, p);
4479 SCLogDebug(
"StreamTcpPacketStateClosing (%p): =+ next SEQ "
4480 "%" PRIu32
", last ACK %" PRIu32
"", ssn,
4500 static int StreamTcpPacketStateCloseWait(
4504 const TCPHdr *tcph = PacketGetTCP(p);
4512 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to client: SEQ "
4513 "%" PRIu32
", ACK %" PRIu32
"",
4516 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to server: SEQ "
4517 "%" PRIu32
", ACK %" PRIu32
"",
4522 if (!StreamTcpValidateRst(ssn, p))
4525 StreamTcpCloseSsnWithReset(p, ssn);
4534 StreamTcpHandleTimestamp(ssn, p);
4545 StreamTcpHandleTimestamp(ssn, p);
4553 if (!StreamTcpValidateTimestamp(ssn, p))
4558 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to server: SEQ "
4559 "%" PRIu32
", ACK %" PRIu32
"",
4562 int retransmission = 0;
4563 if (StreamTcpPacketIsRetransmission(&ssn->
client, p)) {
4564 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
4569 if (!retransmission) {
4572 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
4573 " != %" PRIu32
" from stream",
4580 if (StreamTcpValidateAck(ssn, &ssn->
server, p) == -1) {
4581 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
4588 if (!retransmission)
4592 StreamTcpHandleTimestamp(ssn, p);
4604 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
4608 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to client: SEQ "
4609 "%" PRIu32
", ACK %" PRIu32
"",
4612 int retransmission = 0;
4613 if (StreamTcpPacketIsRetransmission(&ssn->
server, p)) {
4614 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
4619 if (!retransmission) {
4622 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
4623 " != %" PRIu32
" from stream",
4630 if (StreamTcpValidateAck(ssn, &ssn->
client, p) == -1) {
4631 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
4636 if (!retransmission) {
4638 SCLogDebug(
"ssn %p: state changed to TCP_LAST_ACK", ssn);
4644 StreamTcpHandleTimestamp(ssn, p);
4656 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
4662 SCLogDebug(
"ssn (%p): SYN pkt on CloseWait", ssn);
4668 if (!StreamTcpValidateTimestamp(ssn, p))
4673 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to server: SEQ "
4674 "%" PRIu32
", ACK %" PRIu32
"",
4677 int retransmission = 0;
4678 if (StreamTcpPacketIsRetransmission(&ssn->
client, p)) {
4679 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
4685 SCLogDebug(
"ssn %p: -> retransmission", ssn);
4690 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
4691 " != %" PRIu32
" from stream",
4697 if (StreamTcpValidateAck(ssn, &ssn->
server, p) == -1) {
4698 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
4703 if (!retransmission) {
4708 StreamTcpHandleTimestamp(ssn, p);
4722 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
4726 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to client: SEQ "
4727 "%" PRIu32
", ACK %" PRIu32
"",
4729 int retransmission = 0;
4730 if (StreamTcpPacketIsRetransmission(&ssn->
server, p)) {
4731 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
4737 SCLogDebug(
"ssn %p: -> retransmission", ssn);
4742 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
4743 " != %" PRIu32
" from stream",
4749 if (StreamTcpValidateAck(ssn, &ssn->
client, p) == -1) {
4750 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
4755 if (!retransmission) {
4760 StreamTcpHandleTimestamp(ssn, p);
4774 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
4795 static int StreamTcpPacketStateLastAck(
4799 const TCPHdr *tcph = PacketGetTCP(p);
4805 if (!StreamTcpValidateRst(ssn, p))
4808 StreamTcpCloseSsnWithReset(p, ssn);
4817 StreamTcpHandleTimestamp(ssn, p);
4828 StreamTcpHandleTimestamp(ssn, p);
4836 SCLogDebug(
"ssn (%p): FIN pkt on LastAck", ssn);
4839 SCLogDebug(
"ssn (%p): SYN pkt on LastAck", ssn);
4845 if (!StreamTcpValidateTimestamp(ssn, p))
4850 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to server: SEQ "
4851 "%" PRIu32
", ACK %" PRIu32
"",
4854 int retransmission = 0;
4855 if (StreamTcpPacketIsRetransmission(&ssn->
client, p)) {
4856 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
4861 if (StreamTcpValidateAck(ssn, &ssn->
server, p) == -1) {
4862 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
4867 if (!retransmission) {
4869 SCLogDebug(
"ssn %p: not updating state as packet is before next_seq", ssn);
4871 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
4872 " != %" PRIu32
" from stream",
4878 SCLogDebug(
"ssn %p: state changed to TCP_CLOSED", ssn);
4884 StreamTcpHandleTimestamp(ssn, p);
4895 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
4916 static int StreamTcpPacketStateTimeWait(
4920 const TCPHdr *tcph = PacketGetTCP(p);
4926 if (!StreamTcpValidateRst(ssn, p))
4929 StreamTcpCloseSsnWithReset(p, ssn);
4938 StreamTcpHandleTimestamp(ssn, p);
4949 StreamTcpHandleTimestamp(ssn, p);
4959 SCLogDebug(
"ssn (%p): SYN pkt on TimeWait", ssn);
4965 if (!StreamTcpValidateTimestamp(ssn, p))
4970 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to server: SEQ "
4971 "%" PRIu32
", ACK %" PRIu32
"",
4973 int retransmission = 0;
4974 if (StreamTcpPacketIsRetransmission(&ssn->
client, p)) {
4975 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
4980 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
4981 " != %" PRIu32
" from stream",
4987 if (StreamTcpValidateAck(ssn, &ssn->
server, p) == -1) {
4988 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
4993 if (!retransmission) {
4995 SCLogDebug(
"ssn %p: state changed to TCP_CLOSED", ssn);
5001 StreamTcpHandleTimestamp(ssn, p);
5012 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
5016 SCLogDebug(
"ssn %p: pkt (%" PRIu32
") is to client: SEQ "
5017 "%" PRIu32
", ACK %" PRIu32
"",
5019 int retransmission = 0;
5020 if (StreamTcpPacketIsRetransmission(&ssn->
server, p)) {
5021 SCLogDebug(
"ssn %p: packet is retransmission", ssn);
5026 SCLogDebug(
"ssn %p: -> retransmission", ssn);
5029 SCLogDebug(
"ssn %p: -> SEQ mismatch, packet SEQ %" PRIu32
""
5030 " != %" PRIu32
" from stream",
5037 if (StreamTcpValidateAck(ssn, &ssn->
client, p) == -1) {
5038 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
5043 if (!retransmission) {
5045 SCLogDebug(
"ssn %p: state changed to TCP_CLOSED", ssn);
5051 StreamTcpHandleTimestamp(ssn, p);
5062 SCLogDebug(
"ssn %p: =+ next SEQ %" PRIu32
", last ACK "
5074 static int StreamTcpPacketStateClosed(
5079 const TCPHdr *tcph = PacketGetTCP(p);
5085 TcpStream *stream = NULL, *ostream = NULL;
5102 if (StreamTcpStateDispatch(
tv, p, stt, ssn, ssn->
pstate) < 0)
5117 const TCPHdr *tcph = PacketGetTCP(p);
5131 SCLogDebug(
"regular packet %"PRIu64
" from same sender as "
5132 "the previous RST. Looks like it injected!", p->
pcap_cnt);
5158 const TCPHdr *tcph = PacketGetTCP(p);
5163 TcpStream *stream = NULL, *ostream = NULL;
5174 if (ack == ostream->last_ack &&
seq == (stream->
next_seq - 1)) {
5190 TcpStream *stream = NULL, *ostream = NULL;
5201 const TCPHdr *tcph = PacketGetTCP(p);
5220 if (pkt_win != ostream->window)
5229 SCLogDebug(
"seq %u (%u), ack %u (%u) FLAG_KEEPALIVE: %s",
seq, stream->
next_seq, ack, ostream->last_ack,
5259 TcpStream *stream = NULL, *ostream = NULL;
5273 const TCPHdr *tcph = PacketGetTCP(p);
5292 if (pkt_win == ostream->window)
5295 if (ack == ostream->last_ack &&
seq == stream->
next_seq) {
5310 TcpStream *stream = NULL, *ostream = NULL;
5318 const TCPHdr *tcph = PacketGetTCP(p);
5335 SCLogDebug(
"%"PRIu64
", seq %u ack %u stream->next_seq %u ostream->next_seq %u",
5363 TcpStream *stream = NULL, *ostream = NULL;
5374 const TCPHdr *tcph = PacketGetTCP(p);
5390 if (pkt_win < ostream->window) {
5391 uint32_t diff = ostream->window - pkt_win;
5393 SEQ_GT(ack, ostream->next_seq) &&
5396 SCLogDebug(
"%"PRIu64
", pkt_win %u, stream win %u, diff %u, dsize %u",
5398 SCLogDebug(
"%"PRIu64
", pkt_win %u, stream win %u",
5399 p->
pcap_cnt, pkt_win, ostream->window);
5400 SCLogDebug(
"%"PRIu64
", seq %u ack %u ostream->next_seq %u ostream->last_ack %u, ostream->next_win %u, diff %u (%u)",
5401 p->
pcap_cnt,
seq, ack, ostream->next_seq, ostream->last_ack, ostream->next_win,
5402 ostream->next_seq - ostream->last_ack, stream->
next_seq - stream->
last_ack);
5409 uint32_t adiff = ack - ostream->last_ack;
5410 if (((pkt_win > 1024) && (diff > (adiff + 32))) ||
5411 ((pkt_win <= 1024) && (diff > adiff)))
5413 SCLogDebug(
"pkt ACK %u is %u bytes beyond last_ack %u, shrinks window by %u "
5414 "(allowing 32 bytes extra): pkt WIN %u", ack, adiff, ostream->last_ack, diff, pkt_win);
5415 SCLogDebug(
"%u - %u = %u (state %u)", diff, adiff, diff - adiff, ssn->
state);
5430 static inline int StreamTcpStateDispatch(
5438 SCLogDebug(
"packet received on TCP_SYN_SENT state");
5439 if (StreamTcpPacketStateSynSent(
tv, p, stt, ssn)) {
5444 SCLogDebug(
"packet received on TCP_SYN_RECV state");
5445 if (StreamTcpPacketStateSynRecv(
tv, p, stt, ssn)) {
5450 SCLogDebug(
"packet received on TCP_ESTABLISHED state");
5451 if (StreamTcpPacketStateEstablished(
tv, p, stt, ssn)) {
5456 SCLogDebug(
"packet received on TCP_FIN_WAIT1 state");
5457 if (StreamTcpPacketStateFinWait1(
tv, p, stt, ssn)) {
5462 SCLogDebug(
"packet received on TCP_FIN_WAIT2 state");
5463 if (StreamTcpPacketStateFinWait2(
tv, p, stt, ssn)) {
5468 SCLogDebug(
"packet received on TCP_CLOSING state");
5469 if (StreamTcpPacketStateClosing(
tv, p, stt, ssn)) {
5474 SCLogDebug(
"packet received on TCP_CLOSE_WAIT state");
5475 if (StreamTcpPacketStateCloseWait(
tv, p, stt, ssn)) {
5480 SCLogDebug(
"packet received on TCP_LAST_ACK state");
5481 if (StreamTcpPacketStateLastAck(
tv, p, stt, ssn)) {
5486 SCLogDebug(
"packet received on TCP_TIME_WAIT state");
5487 if (StreamTcpPacketStateTimeWait(
tv, p, stt, ssn)) {
5493 SCLogDebug(
"packet received on closed state");
5495 if (StreamTcpPacketStateClosed(
tv, p, stt, ssn)) {
5501 SCLogDebug(
"packet received on default state");
5537 const TCPHdr *tcph = PacketGetTCP(p);
5552 SCLogDebug(
"ssn %p: removing ASYNC flag as we have packets on both sides", ssn);
5572 if (StreamTcpCheckFlowDrops(p) == 1) {
5582 if (StreamTcpPacketStateNone(
tv, p, stt, ssn) == -1) {
5608 if (StreamTcpPacketIsKeepAlive(ssn, p) == 1) {
5611 if (StreamTcpPacketIsKeepAliveACK(ssn, p) == 1) {
5612 StreamTcpClearKeepAliveFlag(ssn, p);
5615 StreamTcpClearKeepAliveFlag(ssn, p);
5617 const bool is_zwp_ack = StreamTcpPacketIsZeroWindowProbeAck(ssn, p);
5628 if (StreamTcpPacketIsDupAck(ssn, p) ==
true) {
5635 if (StreamTcpPacketIsFinShutdownAck(ssn, p) == 0) {
5636 if (StreamTcpPacketIsWindowUpdate(ssn, p) == 0) {
5637 if (StreamTcpPacketIsBadWindowUpdate(ssn,p))
5639 if (StreamTcpPacketIsOutdatedAck(ssn, p))
5644 int ret = StreamTcpPacketIsSpuriousRetransmission(ssn, p);
5653 if (StreamTcpStateDispatch(
tv, p, stt, ssn, ssn->
state) < 0)
5657 StreamTcpPacketCheckPostRst(ssn, p);
5706 SCLogDebug(
"bypass as stream is dead and we have no rules");
5719 if (StreamTcpInlineDropInvalid()) {
5723 DecodeSetNoPayloadInspectionFlag(p);
5737 static inline int StreamTcpValidateChecksum(
Packet *p)
5745 const TCPHdr *tcph = PacketGetTCP(p);
5746 if (PacketIsIPv4(p)) {
5747 const IPV4Hdr *ip4h = PacketGetIPv4(p);
5748 p->
l4.
csum = TCPChecksum(ip4h->s_ip_addrs, (uint16_t *)tcph,
5751 }
else if (PacketIsIPv6(p)) {
5752 const IPV6Hdr *ip6h = PacketGetIPv6(p);
5753 p->
l4.
csum = TCPV6Checksum(ip6h->s_ip6_addrs, (uint16_t *)tcph,
5774 static int TcpSessionPacketIsStreamStarter(
const Packet *p)
5776 const TCPHdr *tcph = PacketGetTCP(p);
5799 static bool TcpSessionReuseDoneEnoughSyn(
const Packet *p,
const Flow *f,
const TcpSession *ssn)
5801 const TCPHdr *tcph = PacketGetTCP(p);
5806 SCLogDebug(
"steam starter packet %" PRIu64
", ssn %p null. Reuse.", p->
pcap_cnt, ssn);
5811 ", ssn %p. STREAMTCP_FLAG_TFO_DATA_IGNORED set. Reuse.",
5816 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p. Packet SEQ == Stream ISN. Retransmission. Don't reuse.", p->
pcap_cnt, ssn);
5820 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p state >= TCP_LAST_ACK (%u). Reuse.", p->
pcap_cnt, ssn, ssn->
state);
5823 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p state == TCP_NONE (%u). Reuse.", p->
pcap_cnt, ssn, ssn->
state);
5826 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p state < TCP_LAST_ACK (%u). Don't reuse.", p->
pcap_cnt, ssn, ssn->
state);
5836 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p state >= TCP_LAST_ACK (%u). Reuse.", p->
pcap_cnt, ssn, ssn->
state);
5839 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p state == TCP_NONE (%u). Reuse.", p->
pcap_cnt, ssn, ssn->
state);
5842 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p state < TCP_LAST_ACK (%u). Don't reuse.", p->
pcap_cnt, ssn, ssn->
state);
5855 static bool TcpSessionReuseDoneEnoughSynAck(
const Packet *p,
const Flow *f,
const TcpSession *ssn)
5857 const TCPHdr *tcph = PacketGetTCP(p);
5860 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p null. No reuse.", p->
pcap_cnt, ssn);
5864 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p. Packet SEQ == Stream ISN. Retransmission. Don't reuse.", p->
pcap_cnt, ssn);
5868 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p state >= TCP_LAST_ACK (%u). Reuse.", p->
pcap_cnt, ssn, ssn->
state);
5871 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p state == TCP_NONE (%u). Reuse.", p->
pcap_cnt, ssn, ssn->
state);
5874 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p state < TCP_LAST_ACK (%u). Don't reuse.", p->
pcap_cnt, ssn, ssn->
state);
5884 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p state >= TCP_LAST_ACK (%u). Reuse.", p->
pcap_cnt, ssn, ssn->
state);
5887 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p state == TCP_NONE (%u). Reuse.", p->
pcap_cnt, ssn, ssn->
state);
5890 SCLogDebug(
"steam starter packet %"PRIu64
", ssn %p state < TCP_LAST_ACK (%u). Don't reuse.", p->
pcap_cnt, ssn, ssn->
state);
5906 const TCPHdr *tcph = PacketGetTCP(p);
5908 return TcpSessionReuseDoneEnoughSyn(p, f, ssn);
5913 return TcpSessionReuseDoneEnoughSynAck(p, f, ssn);
5922 if (p->
proto == IPPROTO_TCP && PacketIsTCP(p)) {
5923 if (TcpSessionPacketIsStreamStarter(p) == 1) {
5924 if (TcpSessionReuseDoneEnough(p, f, tcp_ssn) == 1) {
5947 if (!(PacketIsTCP(p))) {
5951 HandleThreadId(
tv, p, stt);
5957 if (StreamTcpValidateChecksum(p) == 0) {
5983 *data = (
void *)stt;
5992 IsStreamTcpSessionMemcapExceptionPolicyStatsValid);
6000 "tcp.midstream_exception_policy.", IsMidstreamExceptionPolicyStatsValid);
6004 "tcp.midstream_exception_policy.", IsMidstreamExceptionPolicyStatsValid);
6019 "tcp.reassembly_exception_policy.", IsReassemblyMemcapExceptionPolicyStatsValid);
6033 SCLogDebug(
"StreamTcp thread specific ctx online at %p, reassembly ctx %p",
6042 StreamTcpSessionPoolAlloc,
6043 StreamTcpSessionPoolInit, NULL,
6044 StreamTcpSessionPoolCleanup, NULL);
6054 SCLogError(
"failed to setup/expand stream session pool. Expand stream.memcap?");
6099 const TCPHdr *tcph = PacketGetTCP(p);
6107 if (!StreamTcpValidateTimestamp(ssn, p)) {
6136 StreamTcpValidateAck(ssn, &ssn->
server, p) == -1) {
6137 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
6149 StreamTcpValidateAck(ssn, &ssn->
client, p) == -1) {
6150 SCLogDebug(
"ssn %p: rejecting because of invalid ack value", ssn);
6163 receiver_stream = &ssn->
server;
6165 receiver_stream = &ssn->
client;
6167 SCLogDebug(
"ssn %p: setting STREAMTCP_STREAM_FLAG_RST_RECV on receiver stream", ssn);
6187 switch (os_policy) {
6194 SCLogDebug(
"reset is not Valid! Packet SEQ: %" PRIu32
" "
6195 "and server SEQ: %" PRIu32
"",
6204 SCLogDebug(
"reset is not valid! Packet SEQ: %" PRIu32
" "
6205 "and client SEQ: %" PRIu32
"",
6222 SCLogDebug(
"reset is not valid! Packet SEQ: %" PRIu32
" and"
6223 " server SEQ: %" PRIu32
"",
6235 SCLogDebug(
"reset is not valid! Packet SEQ: %" PRIu32
" and"
6236 " client SEQ: %" PRIu32
"",
6257 SCLogDebug(
"reset is not valid! Packet SEQ: %" PRIu32
" "
6258 "and server SEQ: %" PRIu32
"",
6264 SCLogDebug(
"reset is valid! Packet SEQ: %" PRIu32
" Stream %u",
seq,
6268 SCLogDebug(
"reset is not valid! Packet SEQ: %" PRIu32
" and"
6269 " client SEQ: %" PRIu32
"",
6300 uint8_t check_ts = 1;
6301 const TCPHdr *tcph = PacketGetTCP(p);
6305 sender_stream = &ssn->
client;
6306 receiver_stream = &ssn->
server;
6308 sender_stream = &ssn->
server;
6309 receiver_stream = &ssn->
client;
6320 uint32_t last_pkt_ts = sender_stream->
last_pkt_ts;
6321 uint32_t last_ts = sender_stream->
last_ts;
6371 SCLogDebug(
"ts %"PRIu32
", last_ts %"PRIu32
"",
ts, last_ts);
6375 result = (int32_t) ((
ts - last_ts) + 1);
6377 result = (int32_t) (
ts - last_ts);
6380 SCLogDebug(
"result %" PRIi32
", p->ts(secs) %" PRIuMAX
"", result,
6383 if (last_pkt_ts == 0 &&
6391 "%" PRIu32
" p->tcpvars->ts %" PRIu32
" result "
6392 "%" PRId32
"", last_ts,
ts, result);
6395 }
else if ((sender_stream->
last_ts != 0) &&
6397 SCLogDebug(
"packet is not valid last_pkt_ts "
6398 "%" PRIu32
" p->ts(sec) %" PRIu32
"",
6410 SCLogDebug(
"timestamp considered valid anyway");
6442 uint8_t check_ts = 1;
6443 const TCPHdr *tcph = PacketGetTCP(p);
6447 sender_stream = &ssn->
client;
6448 receiver_stream = &ssn->
server;
6450 sender_stream = &ssn->
server;
6451 receiver_stream = &ssn->
client;
6519 result = (int32_t) ((
ts - sender_stream->
last_ts) + 1);
6521 result = (int32_t) (
ts - sender_stream->
last_ts);
6524 SCLogDebug(
"result %" PRIi32
", p->ts(sec) %" PRIuMAX
"", result,
6534 SCLogDebug(
"timestamp is not valid sender_stream->last_ts "
6535 "%" PRIu32
" p->tcpvars->ts %" PRIu32
" result "
6536 "%" PRId32
"", sender_stream->
last_ts,
ts, result);
6539 }
else if ((sender_stream->
last_ts != 0) &&
6542 SCLogDebug(
"packet is not valid sender_stream->last_pkt_ts "
6543 "%" PRIu32
" p->ts(sec) %" PRIu32
"",
6557 }
else if (ret == 0) {
6567 SCLogDebug(
"timestamp considered valid anyway");
6603 const TCPHdr *tcph = PacketGetTCP(p);
6618 SCLogDebug(
"ssn %p: pkt ACK %" PRIu32
" == stream last ACK %" PRIu32, ssn, ack,
6631 SCLogDebug(
"ACK %"PRIu32
" is before last_ack %"PRIu32
" - window "
6632 "%"PRIu32
" = %"PRIu32, ack, stream->
last_ack,
6655 SCLogDebug(
"default path leading to invalid: ACK %"PRIu32
", last_ack %"PRIu32
6668 const uint32_t progress)
6726 static void StreamTcpPseudoPacketCreateDetectLogFlush(
ThreadVars *
tv,
6749 np->
proto = IPPROTO_TCP;
6750 FlowReference(&np->
flow, f);
6760 DecodeSetNoPacketInspectionFlag(np);
6763 DecodeSetNoPayloadInspectionFlag(np);
6803 ip4h->
ip_len = htons(40);
6809 ip4h->s_ip_src.s_addr = f->
src.addr_data32[0];
6810 ip4h->s_ip_dst.s_addr = f->
dst.addr_data32[0];
6812 ip4h->s_ip_src.s_addr = f->
dst.addr_data32[0];
6813 ip4h->s_ip_dst.s_addr = f->
src.addr_data32[0];
6846 ip6h->s_ip6_vfc = 0x60;
6847 ip6h->s_ip6_flow = 0;
6848 ip6h->s_ip6_nxt = IPPROTO_TCP;
6849 ip6h->s_ip6_plen = htons(20);
6850 ip6h->s_ip6_hlim = 64;
6852 ip6h->s_ip6_src[0] = f->
src.addr_data32[0];
6853 ip6h->s_ip6_src[1] = f->
src.addr_data32[1];
6854 ip6h->s_ip6_src[2] = f->
src.addr_data32[2];
6855 ip6h->s_ip6_src[3] = f->
src.addr_data32[3];
6856 ip6h->s_ip6_dst[0] = f->
dst.addr_data32[0];
6857 ip6h->s_ip6_dst[1] = f->
dst.addr_data32[1];
6858 ip6h->s_ip6_dst[2] = f->
dst.addr_data32[2];
6859 ip6h->s_ip6_dst[3] = f->
dst.addr_data32[3];
6861 ip6h->s_ip6_src[0] = f->
dst.addr_data32[0];
6862 ip6h->s_ip6_src[1] = f->
dst.addr_data32[1];
6863 ip6h->s_ip6_src[2] = f->
dst.addr_data32[2];
6864 ip6h->s_ip6_src[3] = f->
dst.addr_data32[3];
6865 ip6h->s_ip6_dst[0] = f->
src.addr_data32[0];
6866 ip6h->s_ip6_dst[1] = f->
src.addr_data32[1];
6867 ip6h->s_ip6_dst[2] = f->
src.addr_data32[2];
6868 ip6h->s_ip6_dst[3] = f->
src.addr_data32[3];
6900 np->
ts = parent->
ts;
6908 FlowDeReference(&np->
flow);
6927 StreamTcpPseudoPacketCreateDetectLogFlush(
tv, stt, p, ssn, pq,
ts^0);
6928 StreamTcpPseudoPacketCreateDetectLogFlush(
tv, stt, p, ssn, pq,
ts^1);
6948 if (p->
flow == NULL)
6976 const uint8_t *seg_data;
6977 uint32_t seg_datalen;
6980 int ret = CallbackFunc(p, seg, data, seg_data, seg_datalen);
7008 if (p->
flow == NULL)
7022 if (server_node == NULL && client_node == NULL) {
7026 while (server_node != NULL || client_node != NULL) {
7027 const uint8_t *seg_data;
7028 uint32_t seg_datalen;
7029 if (server_node == NULL) {
7036 &client_stream->
sb, &client_node->
sbseg, &seg_data, &seg_datalen);
7037 ret = CallbackFunc(p, client_node, data, seg_data, seg_datalen);
7042 client_node = TCPSEG_RB_NEXT(client_node);
7043 }
else if (client_node == NULL) {
7050 &server_stream->
sb, &server_node->
sbseg, &seg_data, &seg_datalen);
7051 ret = CallbackFunc(p, server_node, data, seg_data, seg_datalen);
7056 server_node = TCPSEG_RB_NEXT(server_node);
7061 &client_stream->
sb, &client_node->
sbseg, &seg_data, &seg_datalen);
7062 ret = CallbackFunc(p, client_node, data, seg_data, seg_datalen);
7067 client_node = TCPSEG_RB_NEXT(client_node);
7070 &server_stream->
sb, &server_node->
sbseg, &seg_data, &seg_datalen);
7071 ret = CallbackFunc(p, server_node, data, seg_data, seg_datalen);
7076 server_node = TCPSEG_RB_NEXT(server_node);
7111 const char *tcp_state = NULL;
7117 tcp_state =
"syn_sent";
7120 tcp_state =
"syn_recv";
7123 tcp_state =
"established";
7126 tcp_state =
"fin_wait1";
7129 tcp_state =
"fin_wait2";
7132 tcp_state =
"time_wait";
7135 tcp_state =
"last_ack";
7138 tcp_state =
"close_wait";
7141 tcp_state =
"closing";
7144 tcp_state =
"closed";