suricata
flow-util.h
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 #ifndef __FLOW_UTIL_H__
25 #define __FLOW_UTIL_H__
26 
27 #include "detect-engine-state.h"
28 #include "tmqh-flow.h"
29 
30 #define COPY_TIMESTAMP(src,dst) ((dst)->tv_sec = (src)->tv_sec, (dst)->tv_usec = (src)->tv_usec)
31 
32 #define RESET_COUNTERS(f) do { \
33  (f)->todstpktcnt = 0; \
34  (f)->tosrcpktcnt = 0; \
35  (f)->todstbytecnt = 0; \
36  (f)->tosrcbytecnt = 0; \
37  } while (0)
38 
39 #define FLOW_INITIALIZE(f) do { \
40  (f)->sp = 0; \
41  (f)->dp = 0; \
42  (f)->proto = 0; \
43  (f)->livedev = NULL; \
44  (f)->timeout_at = 0; \
45  (f)->timeout_policy = 0; \
46  (f)->vlan_idx = 0; \
47  (f)->next = NULL; \
48  (f)->flow_state = 0; \
49  (f)->use_cnt = 0; \
50  (f)->tenant_id = 0; \
51  (f)->parent_id = 0; \
52  (f)->probing_parser_toserver_alproto_masks = 0; \
53  (f)->probing_parser_toclient_alproto_masks = 0; \
54  (f)->flags = 0; \
55  (f)->file_flags = 0; \
56  (f)->protodetect_dp = 0; \
57  (f)->lastts.tv_sec = 0; \
58  (f)->lastts.tv_usec = 0; \
59  FLOWLOCK_INIT((f)); \
60  (f)->protoctx = NULL; \
61  (f)->flow_end_flags = 0; \
62  (f)->alproto = 0; \
63  (f)->alproto_ts = 0; \
64  (f)->alproto_tc = 0; \
65  (f)->alproto_orig = 0; \
66  (f)->alproto_expect = 0; \
67  (f)->de_ctx_version = 0; \
68  (f)->thread_id[0] = 0; \
69  (f)->thread_id[1] = 0; \
70  (f)->alparser = NULL; \
71  (f)->alstate = NULL; \
72  (f)->sgh_toserver = NULL; \
73  (f)->sgh_toclient = NULL; \
74  (f)->flowvar = NULL; \
75  RESET_COUNTERS((f)); \
76  } while (0)
77 
78 /** \brief macro to recycle a flow before it goes into the spare queue for reuse.
79  *
80  * Note that the lnext, lprev, hnext fields are untouched, those are
81  * managed by the queueing code. Same goes for fb (FlowBucket ptr) field.
82  */
83 #define FLOW_RECYCLE(f) do { \
84  FlowCleanupAppLayer((f)); \
85  (f)->sp = 0; \
86  (f)->dp = 0; \
87  (f)->proto = 0; \
88  (f)->livedev = NULL; \
89  (f)->vlan_idx = 0; \
90  (f)->ffr = 0; \
91  (f)->next = NULL; \
92  (f)->timeout_at = 0; \
93  (f)->timeout_policy = 0; \
94  (f)->flow_state = 0; \
95  (f)->use_cnt = 0; \
96  (f)->tenant_id = 0; \
97  (f)->parent_id = 0; \
98  (f)->probing_parser_toserver_alproto_masks = 0; \
99  (f)->probing_parser_toclient_alproto_masks = 0; \
100  (f)->flags = 0; \
101  (f)->file_flags = 0; \
102  (f)->protodetect_dp = 0; \
103  (f)->lastts.tv_sec = 0; \
104  (f)->lastts.tv_usec = 0; \
105  (f)->protoctx = NULL; \
106  (f)->flow_end_flags = 0; \
107  (f)->alparser = NULL; \
108  (f)->alstate = NULL; \
109  (f)->alproto = 0; \
110  (f)->alproto_ts = 0; \
111  (f)->alproto_tc = 0; \
112  (f)->alproto_orig = 0; \
113  (f)->alproto_expect = 0; \
114  (f)->de_ctx_version = 0; \
115  (f)->thread_id[0] = 0; \
116  (f)->thread_id[1] = 0; \
117  (f)->sgh_toserver = NULL; \
118  (f)->sgh_toclient = NULL; \
119  GenericVarFree((f)->flowvar); \
120  (f)->flowvar = NULL; \
121  if (MacSetFlowStorageEnabled()) { \
122  MacSet *ms = FlowGetStorageById((f), MacSetGetFlowStorageID()); \
123  if (ms != NULL) { \
124  MacSetReset(ms); \
125  } \
126  } \
127  RESET_COUNTERS((f)); \
128  } while(0)
129 
130 #define FLOW_DESTROY(f) do { \
131  FlowCleanupAppLayer((f)); \
132  \
133  FLOWLOCK_DESTROY((f)); \
134  GenericVarFree((f)->flowvar); \
135  } while(0)
136 
137 /** \brief check if a memory alloc would fit in the memcap
138  *
139  * \param size memory allocation size to check
140  *
141  * \retval 1 it fits
142  * \retval 0 no fit
143  */
144 #define FLOW_CHECK_MEMCAP(size) \
145  ((((uint64_t)SC_ATOMIC_GET(flow_memuse) + (uint64_t)(size)) <= SC_ATOMIC_GET(flow_config.memcap)))
146 
147 Flow *FlowAlloc(void);
149 void FlowFree(Flow *);
150 uint8_t FlowGetProtoMapping(uint8_t);
151 void FlowInit(Flow *, const Packet *);
152 uint8_t FlowGetReverseProtoMapping(uint8_t rproto);
153 
154 #endif /* __FLOW_UTIL_H__ */
155 
Flow_
Flow data structure.
Definition: flow.h:347
FlowGetProtoMapping
uint8_t FlowGetProtoMapping(uint8_t)
Function to map the protocol to the defined FLOW_PROTO_* enumeration.
Definition: flow-util.c:95
FlowAlloc
Flow * FlowAlloc(void)
allocate a flow
Definition: flow-util.c:51
FlowAllocDirect
Flow * FlowAllocDirect(void)
FlowFree
void FlowFree(Flow *)
cleanup & free the memory of a flow
Definition: flow-util.c:80
Packet_
Definition: decode.h:414
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
tmqh-flow.h
FlowInit
void FlowInit(Flow *, const Packet *)
Definition: flow-util.c:143
FlowGetReverseProtoMapping
uint8_t FlowGetReverseProtoMapping(uint8_t rproto)
Definition: flow-util.c:109