suricata
flow-private.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2016 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 #ifndef SURICATA_FLOW_PRIVATE_H
25 #define SURICATA_FLOW_PRIVATE_H
26 
27 #include "flow-hash.h"
28 #include "flow-queue.h"
29 
30 #include "util-atomic.h"
31 
32 /* global flow flags */
33 
34 /** Flow engine is in emergency mode. This means it doesn't have enough spare
35  * flows for new flows and/or it's memcap limit it reached. In this state the
36  * flow engine with evaluate flows with lower timeout settings. */
37 #define FLOW_EMERGENCY 0x01
38 
39 /* Flow Time out values */
40 #define FLOW_DEFAULT_NEW_TIMEOUT 30
41 #define FLOW_DEFAULT_EST_TIMEOUT 300
42 #define FLOW_DEFAULT_BYPASSED_TIMEOUT 100
43 #define FLOW_IPPROTO_TCP_NEW_TIMEOUT 30
44 #define FLOW_IPPROTO_TCP_EST_TIMEOUT 300
45 #define FLOW_IPPROTO_TCP_CLOSED_TIMEOUT 10
46 #define FLOW_IPPROTO_TCP_BYPASSED_TIMEOUT 100
47 #define FLOW_IPPROTO_UDP_NEW_TIMEOUT 30
48 #define FLOW_IPPROTO_UDP_EST_TIMEOUT 300
49 #define FLOW_IPPROTO_UDP_BYPASSED_TIMEOUT 100
50 #define FLOW_IPPROTO_ICMP_NEW_TIMEOUT 30
51 #define FLOW_IPPROTO_ICMP_EST_TIMEOUT 300
52 #define FLOW_IPPROTO_ICMP_BYPASSED_TIMEOUT 100
53 
54 #define FLOW_DEFAULT_EMERG_NEW_TIMEOUT 10
55 #define FLOW_DEFAULT_EMERG_EST_TIMEOUT 100
56 #define FLOW_DEFAULT_EMERG_BYPASSED_TIMEOUT 50
57 #define FLOW_IPPROTO_TCP_EMERG_NEW_TIMEOUT 10
58 #define FLOW_IPPROTO_TCP_EMERG_EST_TIMEOUT 100
59 #define FLOW_IPPROTO_TCP_EMERG_CLOSED_TIMEOUT 5
60 #define FLOW_IPPROTO_UDP_EMERG_NEW_TIMEOUT 10
61 #define FLOW_IPPROTO_UDP_EMERG_EST_TIMEOUT 100
62 #define FLOW_IPPROTO_ICMP_EMERG_NEW_TIMEOUT 10
63 #define FLOW_IPPROTO_ICMP_EMERG_EST_TIMEOUT 100
64 
65 enum {
70 
71  /* should be last */
73 };
74 /* max used in app-layer (counters) */
75 #define FLOW_PROTO_APPLAYER_MAX (FLOW_PROTO_UDP + 1)
76 
77 /*
78  * Variables
79  */
80 
81 /** FlowProto specific timeouts and free/state functions */
82 
86 
87 /** spare/unused/prealloced flows live here */
88 //extern FlowQueue flow_spare_q;
89 
90 /** queue to pass flows to cleanup/log thread(s) */
92 
93 extern FlowBucket *flow_hash;
94 extern FlowConfig flow_config;
95 
96 /** flow memuse counter (atomic), for enforcing memcap limit */
97 SC_ATOMIC_EXTERN(uint64_t, flow_memuse);
98 
101 
102 static inline uint32_t FlowGetFlowTimeoutDirect(
103  const FlowProtoTimeoutPtr flow_timeouts,
104  const enum FlowState state, const uint8_t protomap)
105 {
106  uint32_t timeout;
107  switch (state) {
108  default:
109  case FLOW_STATE_NEW:
110  timeout = flow_timeouts[protomap].new_timeout;
111  break;
113  timeout = flow_timeouts[protomap].est_timeout;
114  break;
115  case FLOW_STATE_CLOSED:
116  timeout = flow_timeouts[protomap].closed_timeout;
117  break;
118 #ifdef CAPTURE_OFFLOAD
119  case FLOW_STATE_CAPTURE_BYPASSED:
120  timeout = flow_timeouts[protomap].bypassed_timeout;
121  break;
122 #endif
124  timeout = flow_timeouts[protomap].bypassed_timeout;
125  break;
126  }
127  return timeout;
128 }
129 
130 /** \internal
131  * \brief get timeout for flow
132  *
133  * \param f flow
134  * \param state flow state
135  *
136  * \retval timeout timeout in seconds
137  */
138 static inline uint32_t FlowGetFlowTimeout(const Flow *f, enum FlowState state)
139 {
140  FlowProtoTimeoutPtr flow_timeouts = SC_ATOMIC_GET(flow_timeouts);
141  return FlowGetFlowTimeoutDirect(flow_timeouts, state, f->protomap);
142 }
143 
144 /** \internal
145  * \brief get timeout policy for flow
146  * \note does not take emergency mode into account. Always
147  * returns the 'normal' policy.
148  *
149  * \param f flow
150  *
151  * \retval timeout timeout in seconds
152  */
153 static inline uint32_t FlowGetTimeoutPolicy(const Flow *f)
154 {
155  uint32_t timeout;
157  switch (f->flow_state) {
158  default:
159  case FLOW_STATE_NEW:
160  timeout = flow_timeouts[f->protomap].new_timeout;
161  break;
163  timeout = flow_timeouts[f->protomap].est_timeout;
164  break;
165  case FLOW_STATE_CLOSED:
166  timeout = flow_timeouts[f->protomap].closed_timeout;
167  break;
168 #ifdef CAPTURE_OFFLOAD
169  case FLOW_STATE_CAPTURE_BYPASSED:
170  timeout = flow_timeouts[f->protomap].bypassed_timeout;
171  break;
172 #endif
174  timeout = flow_timeouts[f->protomap].bypassed_timeout;
175  break;
176  }
177  return timeout;
178 }
179 #endif /* SURICATA_FLOW_PRIVATE_H */
flow_hash
FlowBucket * flow_hash
Definition: flow-hash.c:59
FLOW_STATE_ESTABLISHED
@ FLOW_STATE_ESTABLISHED
Definition: flow.h:497
Flow_
Flow data structure.
Definition: flow.h:348
Flow_::protomap
uint8_t protomap
Definition: flow.h:437
FlowProtoTimeout_
Definition: flow.h:510
flow-hash.h
FlowProtoTimeout_::bypassed_timeout
uint32_t bypassed_timeout
Definition: flow.h:514
FLOW_PROTO_UDP
@ FLOW_PROTO_UDP
Definition: flow-private.h:67
Flow_::flow_state
FlowStateType flow_state
Definition: flow.h:404
FLOW_STATE_LOCAL_BYPASSED
@ FLOW_STATE_LOCAL_BYPASSED
Definition: flow.h:499
flow_freefuncs
FlowProtoFreeFunc flow_freefuncs[FLOW_PROTO_MAX]
Definition: flow.c:92
FlowCnf_
Definition: flow.h:284
FlowState
FlowState
Definition: flow.h:495
FlowProtoTimeout_::new_timeout
uint32_t new_timeout
Definition: flow.h:511
util-atomic.h
FlowProtoTimeout_::closed_timeout
uint32_t closed_timeout
Definition: flow.h:513
FlowProtoTimeoutPtr
FlowProtoTimeout * FlowProtoTimeoutPtr
Definition: flow-private.h:99
FLOW_PROTO_DEFAULT
@ FLOW_PROTO_DEFAULT
Definition: flow-private.h:69
FLOW_PROTO_ICMP
@ FLOW_PROTO_ICMP
Definition: flow-private.h:68
flow_timeouts_emerg
FlowProtoTimeout flow_timeouts_emerg[FLOW_PROTO_MAX]
Definition: flow.c:90
flow-queue.h
FlowQueue_
Definition: flow-queue.h:48
flow_recycle_q
FlowQueue flow_recycle_q
Definition: flow-manager.c:66
FLOW_STATE_NEW
@ FLOW_STATE_NEW
Definition: flow.h:496
flow_config
FlowConfig flow_config
Definition: flow.c:94
FLOW_STATE_CLOSED
@ FLOW_STATE_CLOSED
Definition: flow.h:498
SC_ATOMIC_EXTERN
SC_ATOMIC_EXTERN(uint64_t, flow_memuse)
flow_timeouts_normal
FlowProtoTimeout flow_timeouts_normal[FLOW_PROTO_MAX]
Definition: flow.c:89
FlowProtoTimeout_::est_timeout
uint32_t est_timeout
Definition: flow.h:512
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
FLOW_PROTO_TCP
@ FLOW_PROTO_TCP
Definition: flow-private.h:66
FLOW_PROTO_MAX
@ FLOW_PROTO_MAX
Definition: flow-private.h:72
FlowProtoFreeFunc_
Definition: flow.h:517