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 SURICATA_FLOW_UTIL_H
25 #define SURICATA_FLOW_UTIL_H
26 
27 #include "flow.h"
28 #include "stream-tcp-private.h"
29 
30 #define RESET_COUNTERS(f) \
31  do { \
32  (f)->todstpktcnt = 0; \
33  (f)->tosrcpktcnt = 0; \
34  (f)->todstbytecnt = 0; \
35  (f)->tosrcbytecnt = 0; \
36  } while (0)
37 
38 #define FLOW_INITIALIZE(f) \
39  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)->tenant_id = 0; \
50  (f)->parent_id = 0; \
51  (f)->probing_parser_toserver_alproto_masks = 0; \
52  (f)->probing_parser_toclient_alproto_masks = 0; \
53  (f)->flags = 0; \
54  (f)->file_flags = 0; \
55  (f)->protodetect_dp = 0; \
56  SCTIME_INIT((f)->lastts); \
57  FLOWLOCK_INIT((f)); \
58  (f)->protoctx = NULL; \
59  (f)->flow_end_flags = 0; \
60  (f)->alproto = 0; \
61  (f)->alproto_ts = 0; \
62  (f)->alproto_tc = 0; \
63  (f)->alproto_orig = 0; \
64  (f)->alproto_expect = 0; \
65  (f)->de_ctx_version = 0; \
66  (f)->thread_id[0] = 0; \
67  (f)->thread_id[1] = 0; \
68  (f)->alparser = NULL; \
69  (f)->alstate = NULL; \
70  (f)->sgh_toserver = NULL; \
71  (f)->sgh_toclient = NULL; \
72  (f)->flowvar = NULL; \
73  RESET_COUNTERS((f)); \
74  } while (0)
75 
76 /** \brief macro to recycle a flow before it goes into the spare queue for reuse.
77  *
78  * Note that the lnext, lprev, hnext fields are untouched, those are
79  * managed by the queueing code. Same goes for fb (FlowBucket ptr) field.
80  */
81 #define FLOW_RECYCLE(f) \
82  do { \
83  FlowCleanupAppLayer((f)); \
84  (f)->sp = 0; \
85  (f)->dp = 0; \
86  (f)->proto = 0; \
87  (f)->livedev = NULL; \
88  (f)->vlan_idx = 0; \
89  (f)->ffr = 0; \
90  (f)->next = NULL; \
91  (f)->timeout_at = 0; \
92  (f)->timeout_policy = 0; \
93  (f)->flow_state = 0; \
94  (f)->tenant_id = 0; \
95  (f)->parent_id = 0; \
96  (f)->probing_parser_toserver_alproto_masks = 0; \
97  (f)->probing_parser_toclient_alproto_masks = 0; \
98  (f)->flags = 0; \
99  (f)->file_flags = 0; \
100  (f)->protodetect_dp = 0; \
101  SCTIME_INIT((f)->lastts); \
102  (f)->protoctx = NULL; \
103  (f)->flow_end_flags = 0; \
104  (f)->alparser = NULL; \
105  (f)->alstate = NULL; \
106  (f)->alproto = 0; \
107  (f)->alproto_ts = 0; \
108  (f)->alproto_tc = 0; \
109  (f)->alproto_orig = 0; \
110  (f)->alproto_expect = 0; \
111  (f)->de_ctx_version = 0; \
112  (f)->thread_id[0] = 0; \
113  (f)->thread_id[1] = 0; \
114  (f)->sgh_toserver = NULL; \
115  (f)->sgh_toclient = NULL; \
116  GenericVarFree((f)->flowvar); \
117  (f)->flowvar = NULL; \
118  RESET_COUNTERS((f)); \
119  } while (0)
120 
121 #define FLOW_DESTROY(f) \
122  do { \
123  FlowCleanupAppLayer((f)); \
124  \
125  FLOWLOCK_DESTROY((f)); \
126  GenericVarFree((f)->flowvar); \
127  } while (0)
128 
129 /** \brief check if a memory alloc would fit in the memcap
130  *
131  * \param size memory allocation size to check
132  *
133  * \retval 1 it fits
134  * \retval 0 no fit
135  */
136 #define FLOW_CHECK_MEMCAP(size) \
137  ((((uint64_t)SC_ATOMIC_GET(flow_memuse) + (uint64_t)(size)) <= \
138  SC_ATOMIC_GET(flow_config.memcap)))
139 
140 Flow *FlowAlloc(void);
141 void FlowFree(Flow *);
142 uint8_t FlowGetProtoMapping(uint8_t);
143 void FlowInit(ThreadVars *, Flow *, const Packet *);
144 uint8_t FlowGetReverseProtoMapping(uint8_t rproto);
145 
146 /* flow end counter logic */
147 
148 typedef struct FlowEndCounters_ {
150  uint16_t flow_tcp_state[TCP_CLOSED + 1];
153 
154 static inline void FlowEndCountersUpdate(ThreadVars *tv, FlowEndCounters *fec, Flow *f)
155 {
156  if (f->proto == IPPROTO_TCP && f->protoctx != NULL) {
157  TcpSession *ssn = f->protoctx;
158  StatsIncr(tv, fec->flow_tcp_state[ssn->state]);
161  }
162  }
163  StatsIncr(tv, fec->flow_state[f->flow_state]);
164 }
165 
167 
168 #endif /* SURICATA_FLOW_UTIL_H */
StatsIncr
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
Definition: counters.c:166
Flow_::proto
uint8_t proto
Definition: flow.h:378
Flow_
Flow data structure.
Definition: flow.h:356
FlowGetProtoMapping
uint8_t FlowGetProtoMapping(uint8_t)
Function to map the protocol to the defined FLOW_PROTO_* enumeration.
Definition: flow-util.c:98
FlowEndCounters_::flow_tcp_liberal
uint16_t flow_tcp_liberal
Definition: flow-util.h:151
FlowEndCounters_::flow_state
uint16_t flow_state[FLOW_STATE_SIZE]
Definition: flow-util.h:149
Flow_::protoctx
void * protoctx
Definition: flow.h:446
FlowAlloc
Flow * FlowAlloc(void)
allocate a flow
Definition: flow-util.c:55
Flow_::flow_state
FlowStateType flow_state
Definition: flow.h:417
STREAMTCP_FLAG_LOSSY_BE_LIBERAL
#define STREAMTCP_FLAG_LOSSY_BE_LIBERAL
Definition: stream-tcp-private.h:194
TcpSession_::flags
uint32_t flags
Definition: stream-tcp-private.h:292
FlowEndCounters_::flow_tcp_state
uint16_t flow_tcp_state[TCP_CLOSED+1]
Definition: flow-util.h:150
FlowInit
void FlowInit(ThreadVars *, Flow *, const Packet *)
Definition: flow-util.c:146
FlowEndCounters
struct FlowEndCounters_ FlowEndCounters
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
FlowFree
void FlowFree(Flow *)
cleanup & free the memory of a flow
Definition: flow-util.c:83
TcpSession_::state
uint8_t state
Definition: stream-tcp-private.h:285
Packet_
Definition: decode.h:473
stream-tcp-private.h
FLOW_STATE_SIZE
#define FLOW_STATE_SIZE
Definition: flow.h:517
TCP_CLOSED
@ TCP_CLOSED
Definition: stream-tcp-private.h:162
FlowEndCountersRegister
void FlowEndCountersRegister(ThreadVars *t, FlowEndCounters *fec)
Definition: flow-util.c:238
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
TcpSession_
Definition: stream-tcp-private.h:283
FlowGetReverseProtoMapping
uint8_t FlowGetReverseProtoMapping(uint8_t rproto)
Definition: flow-util.c:112
flow.h
FlowEndCounters_
Definition: flow-util.h:148