]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
flow: do timeout checks before tuple compare
authorVictor Julien <victor@inliniac.net>
Mon, 13 Jan 2020 19:06:00 +0000 (20:06 +0100)
committerVictor Julien <victor@inliniac.net>
Thu, 6 Aug 2020 09:43:46 +0000 (11:43 +0200)
src/flow-hash.c

index 742333a8ef84d173400daa13d3e59d8cfa5ac5af..29baed2694d075bcf4ff36c22514dad950869e21 100644 (file)
@@ -627,13 +627,13 @@ Flow *FlowGetFlowFromHash(ThreadVars *tv, DecodeThreadVars *dtv, const Packet *p
     Flow *pf = NULL; /* previous flow */
     f = fb->head;
     do {
-        if (FlowCompare(f, p) != 0) {
-            FLOWLOCK_WRLOCK(f);
-            if ((f->flags & (FLOW_TCP_REUSED|FLOW_TIMED_OUT)) == 0) {
-                uint32_t timeout = FlowGetFlowTimeout(f, SC_ATOMIC_GET(f->flow_state));
-                int32_t flow_times_out_at = (int32_t)(f->lastts.tv_sec + timeout);
-                /* do the timeout check */
-                if (flow_times_out_at >= p->ts.tv_sec) {
+        FLOWLOCK_WRLOCK(f);
+        if ((f->flags & (FLOW_TCP_REUSED|FLOW_TIMED_OUT)) == 0) {
+            uint32_t timeout = FlowGetFlowTimeout(f, SC_ATOMIC_GET(f->flow_state));
+            int32_t flow_times_out_at = (int32_t)(f->lastts.tv_sec + timeout);
+            /* do the timeout check */
+            if (flow_times_out_at >= p->ts.tv_sec) {
+                if (FlowCompare(f, p) != 0) {
                     if (unlikely(TcpSessionPacketSsnReuse(p, f, f->protoctx) == 1)) {
                         f = TcpReuseReplace(tv, dtv, fb, f, hash, p);
                         if (f == NULL) {
@@ -649,8 +649,8 @@ Flow *FlowGetFlowFromHash(ThreadVars *tv, DecodeThreadVars *dtv, const Packet *p
                 }
                 f->flags |= FLOW_TIMED_OUT;
             }
-            FLOWLOCK_UNLOCK(f);
         }
+        FLOWLOCK_UNLOCK(f);
         if (f->hnext == NULL) {
             pf = f;
             f = pf->hnext = FlowGetNew(tv, dtv, p);