suricata
ippair-timeout.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2012 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  */
23 
24 #include "suricata-common.h"
25 #include "ippair.h"
26 #include "ippair-bit.h"
27 #include "ippair-timeout.h"
29 
30 /** \internal
31  * \brief See if we can really discard this ippair. Check use_cnt reference.
32  *
33  * \param h ippair
34  * \param ts timestamp
35  *
36  * \retval 0 not timed out just yet
37  * \retval 1 fully timed out, lets kill it
38  */
39 static int IPPairTimedOut(IPPair *h, SCTime_t ts)
40 {
41  int vars = 0;
42  int thresholds = 0;
43 
44  /** never prune a ippair that is used by a packet
45  * we are currently processing in one of the threads */
46  if (SC_ATOMIC_GET(h->use_cnt) > 0) {
47  return 0;
48  }
49 
50  if (IPPairHasBits(h) && IPPairBitsTimedoutCheck(h, ts) == 0) {
51  vars = 1;
52  }
53 
55  thresholds = 1;
56  }
57 
58  if (vars || thresholds) {
59  return 0;
60  }
61 
62  SCLogDebug("ippair %p timed out", h);
63  return 1;
64 }
65 
66 /**
67  * \internal
68  *
69  * \brief check all ippairs in a hash row for timing out
70  *
71  * \param hb ippair hash row *LOCKED*
72  * \param h last ippair in the hash row
73  * \param ts timestamp
74  *
75  * \retval cnt timed out ippairs
76  */
77 static uint32_t IPPairHashRowTimeout(IPPairHashRow *hb, IPPair *h, SCTime_t ts)
78 {
79  uint32_t cnt = 0;
80 
81  do {
82  if (SCMutexTrylock(&h->m) != 0) {
83  h = h->hprev;
84  continue;
85  }
86 
87  IPPair *next_ippair = h->hprev;
88 
89  /* check if the ippair is fully timed out and
90  * ready to be discarded. */
91  if (IPPairTimedOut(h, ts) == 1) {
92  /* remove from the hash */
93  if (h->hprev != NULL)
94  h->hprev->hnext = h->hnext;
95  if (h->hnext != NULL)
96  h->hnext->hprev = h->hprev;
97  if (hb->head == h)
98  hb->head = h->hnext;
99  if (hb->tail == h)
100  hb->tail = h->hprev;
101 
102  h->hnext = NULL;
103  h->hprev = NULL;
104 
105  IPPairClearMemory (h);
106 
107  /* no one is referring to this ippair, use_cnt 0, removed from hash
108  * so we can unlock it and move it back to the spare queue. */
109  SCMutexUnlock(&h->m);
110 
111  /* move to spare list */
113 
114  cnt++;
115  } else {
116  SCMutexUnlock(&h->m);
117  }
118 
119  h = next_ippair;
120  } while (h != NULL);
121 
122  return cnt;
123 }
124 
125 /**
126  * \brief time out ippairs from the hash
127  *
128  * \param ts timestamp
129  *
130  * \retval cnt number of timed out ippair
131  */
133 {
134  uint32_t idx = 0;
135  uint32_t cnt = 0;
136 
137  for (idx = 0; idx < ippair_config.hash_size; idx++) {
138  IPPairHashRow *hb = &ippair_hash[idx];
139 
140  if (HRLOCK_TRYLOCK(hb) != 0)
141  continue;
142 
143  /* ippair hash bucket is now locked */
144 
145  if (hb->tail == NULL) {
146  HRLOCK_UNLOCK(hb);
147  continue;
148  }
149 
150  /* we have a ippair, or more than one */
151  cnt += IPPairHashRowTimeout(hb, hb->tail, ts);
152  HRLOCK_UNLOCK(hb);
153  }
154 
155  return cnt;
156 }
ts
uint64_t ts
Definition: source-erf-file.c:55
ippair.h
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
IPPairTimeoutHash
uint32_t IPPairTimeoutHash(SCTime_t ts)
time out ippairs from the hash
Definition: ippair-timeout.c:132
ippair-bit.h
ThresholdIPPairTimeoutCheck
int ThresholdIPPairTimeoutCheck(IPPair *pair, SCTime_t ts)
Definition: detect-engine-threshold.c:202
IPPairMoveToSpare
void IPPairMoveToSpare(IPPair *h)
Definition: ippair.c:98
IPPair_::m
SCMutex m
Definition: ippair.h:60
HRLOCK_UNLOCK
#define HRLOCK_UNLOCK(fb)
Definition: host.h:53
ThresholdIPPairHasThreshold
int ThresholdIPPairHasThreshold(IPPair *pair)
Definition: detect-engine-threshold.c:98
ippair_config
IPPairConfig ippair_config
Definition: ippair.c:52
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
SCTime_t
Definition: util-time.h:40
IPPairHasBits
int IPPairHasBits(IPPair *ippair)
Definition: ippair-bit.c:58
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
suricata-common.h
IPPair_
Definition: ippair.h:58
IPPairConfig_::hash_size
uint32_t hash_size
Definition: ippair.h:94
IPPairClearMemory
void IPPairClearMemory(IPPair *h)
Definition: ippair.c:150
IPPair_::hprev
struct IPPair_ * hprev
Definition: ippair.h:70
IPPair_::hnext
struct IPPair_ * hnext
Definition: ippair.h:69
HRLOCK_TRYLOCK
#define HRLOCK_TRYLOCK(fb)
Definition: host.h:52
ippair-timeout.h
IPPairBitsTimedoutCheck
int IPPairBitsTimedoutCheck(IPPair *h, SCTime_t ts)
Definition: ippair-bit.c:67
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
ippair_hash
IPPairHashRow * ippair_hash
Definition: ippair.c:49
SCMutexTrylock
#define SCMutexTrylock(mut)
Definition: threads-debug.h:118
detect-engine-threshold.h