suricata
host-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 "host.h"
26 
27 #include "detect-engine-tag.h"
29 
30 #include "host-bit.h"
31 #include "host-timeout.h"
32 
33 #include "reputation.h"
34 
35 /** \internal
36  * \brief See if we can really discard this host. Check use_cnt reference.
37  *
38  * \param h host
39  * \param ts timestamp
40  *
41  * \retval 0 not timed out just yet
42  * \retval 1 fully timed out, lets kill it
43  */
44 static int HostHostTimedOut(Host *h, SCTime_t ts)
45 {
46  int busy = 0;
47 
48  /** never prune a host that is used by a packet
49  * we are currently processing in one of the threads */
50  if (SC_ATOMIC_GET(h->use_cnt) > 0) {
51  return 0;
52  }
53 
54  busy |= (h->iprep && SRepHostTimedOut(h) == 0);
55  busy |= (TagHostHasTag(h) && TagTimeoutCheck(h, ts) == 0);
57  busy |= (HostHasHostBits(h) && HostBitsTimedoutCheck(h, ts) == 0);
58  SCLogDebug("host %p %s", h, busy ? "still active" : "timed out");
59  return !busy;
60 }
61 
62 /**
63  * \internal
64  *
65  * \brief check all hosts in a hash row for timing out
66  *
67  * \param hb host hash row *LOCKED*
68  * \param h last host in the hash row
69  * \param ts timestamp
70  *
71  * \retval cnt timed out hosts
72  */
73 static uint32_t HostHashRowTimeout(HostHashRow *hb, Host *h, SCTime_t ts)
74 {
75  uint32_t cnt = 0;
76 
77  do {
78  if (SCMutexTrylock(&h->m) != 0) {
79  h = h->hprev;
80  continue;
81  }
82 
83  Host *next_host = h->hprev;
84 
85  /* check if the host is fully timed out and
86  * ready to be discarded. */
87  if (HostHostTimedOut(h, ts) == 1) {
88  /* remove from the hash */
89  if (h->hprev != NULL)
90  h->hprev->hnext = h->hnext;
91  if (h->hnext != NULL)
92  h->hnext->hprev = h->hprev;
93  if (hb->head == h)
94  hb->head = h->hnext;
95  if (hb->tail == h)
96  hb->tail = h->hprev;
97 
98  h->hnext = NULL;
99  h->hprev = NULL;
100 
101  HostClearMemory (h);
102 
103  /* no one is referring to this host, use_cnt 0, removed from hash
104  * so we can unlock it and move it back to the spare queue. */
105  SCMutexUnlock(&h->m);
106 
107  /* move to spare list */
108  HostMoveToSpare(h);
109 
110  cnt++;
111  } else {
112  SCMutexUnlock(&h->m);
113  }
114 
115  h = next_host;
116  } while (h != NULL);
117 
118  return cnt;
119 }
120 
121 /**
122  * \brief time out hosts from the hash
123  *
124  * \param ts timestamp
125  *
126  * \retval cnt number of timed out host
127  */
129 {
130  uint32_t idx = 0;
131  uint32_t cnt = 0;
132 
133  for (idx = 0; idx < host_config.hash_size; idx++) {
134  HostHashRow *hb = &host_hash[idx];
135 
136  if (HRLOCK_TRYLOCK(hb) != 0)
137  continue;
138 
139  /* host hash bucket is now locked */
140 
141  if (hb->tail == NULL) {
142  HRLOCK_UNLOCK(hb);
143  continue;
144  }
145 
146  /* we have a host, or more than one */
147  cnt += HostHashRowTimeout(hb, hb->tail, ts);
148  HRLOCK_UNLOCK(hb);
149  }
150 
151  return cnt;
152 }
153 
host.h
ts
uint64_t ts
Definition: source-erf-file.c:55
ThresholdHostTimeoutCheck
int ThresholdHostTimeoutCheck(Host *host, SCTime_t ts)
Definition: detect-engine-threshold.c:192
Host_::hnext
struct Host_ * hnext
Definition: host.h:72
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
Host_::m
SCMutex m
Definition: host.h:60
HostClearMemory
void HostClearMemory(Host *h)
Definition: host.c:150
ThresholdHostHasThreshold
int ThresholdHostHasThreshold(Host *host)
Definition: detect-engine-threshold.c:93
HRLOCK_UNLOCK
#define HRLOCK_UNLOCK(fb)
Definition: host.h:53
host_hash
HostHashRow * host_hash
Definition: host.c:50
Host_::hprev
struct Host_ * hprev
Definition: host.h:73
TagHostHasTag
int TagHostHasTag(Host *host)
Definition: detect-engine-tag.c:79
host_config
HostConfig host_config
Definition: host.c:53
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
HostHasHostBits
int HostHasHostBits(Host *host)
Definition: host-bit.c:58
detect-engine-tag.h
SCTime_t
Definition: util-time.h:40
reputation.h
host-timeout.h
TagTimeoutCheck
int TagTimeoutCheck(Host *host, SCTime_t ts)
Removes the entries exceeding the max timeout value.
Definition: detect-engine-tag.c:558
SRepHostTimedOut
int SRepHostTimedOut(Host *h)
Check if a Host is timed out wrt ip rep, meaning a new version is in place.
Definition: reputation.c:206
Host_::iprep
void * iprep
Definition: host.h:69
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
suricata-common.h
HostMoveToSpare
void HostMoveToSpare(Host *h)
Definition: host.c:100
HostBitsTimedoutCheck
int HostBitsTimedoutCheck(Host *h, SCTime_t ts)
Definition: host-bit.c:67
HostConfig_::hash_size
uint32_t hash_size
Definition: host.h:98
HostTimeoutHash
uint32_t HostTimeoutHash(SCTime_t ts)
time out hosts from the hash
Definition: host-timeout.c:128
HRLOCK_TRYLOCK
#define HRLOCK_TRYLOCK(fb)
Definition: host.h:52
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
Host_
Definition: host.h:58
SCMutexTrylock
#define SCMutexTrylock(mut)
Definition: threads-debug.h:118
host-bit.h
detect-engine-threshold.h