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 "stream-tcp-private.h"
28 
29 #define COPY_TIMESTAMP(src,dst) ((dst)->tv_sec = (src)->tv_sec, (dst)->tv_usec = (src)->tv_usec)
30 
31 #define RESET_COUNTERS(f) 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) do { \
39  (f)->sp = 0; \
40  (f)->dp = 0; \
41  (f)->proto = 0; \
42  (f)->livedev = NULL; \
43  (f)->timeout_at = 0; \
44  (f)->timeout_policy = 0; \
45  (f)->vlan_idx = 0; \
46  (f)->next = NULL; \
47  (f)->flow_state = 0; \
48  (f)->use_cnt = 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  (f)->lastts.tv_sec = 0; \
57  (f)->lastts.tv_usec = 0; \
58  FLOWLOCK_INIT((f)); \
59  (f)->protoctx = NULL; \
60  (f)->flow_end_flags = 0; \
61  (f)->alproto = 0; \
62  (f)->alproto_ts = 0; \
63  (f)->alproto_tc = 0; \
64  (f)->alproto_orig = 0; \
65  (f)->alproto_expect = 0; \
66  (f)->de_ctx_version = 0; \
67  (f)->thread_id[0] = 0; \
68  (f)->thread_id[1] = 0; \
69  (f)->alparser = NULL; \
70  (f)->alstate = NULL; \
71  (f)->sgh_toserver = NULL; \
72  (f)->sgh_toclient = NULL; \
73  (f)->flowvar = NULL; \
74  RESET_COUNTERS((f)); \
75  } while (0)
76 
77 /** \brief macro to recycle a flow before it goes into the spare queue for reuse.
78  *
79  * Note that the lnext, lprev, hnext fields are untouched, those are
80  * managed by the queueing code. Same goes for fb (FlowBucket ptr) field.
81  */
82 #define FLOW_RECYCLE(f) 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)->use_cnt = 0; \
95  (f)->tenant_id = 0; \
96  (f)->parent_id = 0; \
97  (f)->probing_parser_toserver_alproto_masks = 0; \
98  (f)->probing_parser_toclient_alproto_masks = 0; \
99  (f)->flags = 0; \
100  (f)->file_flags = 0; \
101  (f)->protodetect_dp = 0; \
102  (f)->lastts.tv_sec = 0; \
103  (f)->lastts.tv_usec = 0; \
104  (f)->protoctx = NULL; \
105  (f)->flow_end_flags = 0; \
106  (f)->alparser = NULL; \
107  (f)->alstate = NULL; \
108  (f)->alproto = 0; \
109  (f)->alproto_ts = 0; \
110  (f)->alproto_tc = 0; \
111  (f)->alproto_orig = 0; \
112  (f)->alproto_expect = 0; \
113  (f)->de_ctx_version = 0; \
114  (f)->thread_id[0] = 0; \
115  (f)->thread_id[1] = 0; \
116  (f)->sgh_toserver = NULL; \
117  (f)->sgh_toclient = NULL; \
118  GenericVarFree((f)->flowvar); \
119  (f)->flowvar = NULL; \
120  if (MacSetFlowStorageEnabled()) { \
121  MacSet *ms = FlowGetStorageById((f), MacSetGetFlowStorageID()); \
122  if (ms != NULL) { \
123  MacSetReset(ms); \
124  } \
125  } \
126  RESET_COUNTERS((f)); \
127  } while(0)
128 
129 #define FLOW_DESTROY(f) do { \
130  FlowCleanupAppLayer((f)); \
131  \
132  FLOWLOCK_DESTROY((f)); \
133  GenericVarFree((f)->flowvar); \
134  } while(0)
135 
136 /** \brief check if a memory alloc would fit in the memcap
137  *
138  * \param size memory allocation size to check
139  *
140  * \retval 1 it fits
141  * \retval 0 no fit
142  */
143 #define FLOW_CHECK_MEMCAP(size) \
144  ((((uint64_t)SC_ATOMIC_GET(flow_memuse) + (uint64_t)(size)) <= SC_ATOMIC_GET(flow_config.memcap)))
145 
146 Flow *FlowAlloc(void);
148 void FlowFree(Flow *);
149 uint8_t FlowGetProtoMapping(uint8_t);
150 void FlowInit(Flow *, const Packet *);
151 uint8_t FlowGetReverseProtoMapping(uint8_t rproto);
152 
153 /* flow end counter logic */
154 
155 typedef struct FlowEndCounters_ {
157  uint16_t flow_tcp_state[TCP_CLOSED + 1];
160 
161 static inline void FlowEndCountersUpdate(ThreadVars *tv, FlowEndCounters *fec, Flow *f)
162 {
163  if (f->proto == IPPROTO_TCP && f->protoctx != NULL) {
164  TcpSession *ssn = f->protoctx;
165  StatsIncr(tv, fec->flow_tcp_state[ssn->state]);
166  if (ssn->lossy_be_liberal) {
168  }
169  }
170  StatsIncr(tv, fec->flow_state[f->flow_state]);
171 }
172 
174 
175 #endif /* __FLOW_UTIL_H__ */
176 
StatsIncr
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
Definition: counters.c:169
Flow_::proto
uint8_t proto
Definition: flow.h:375
Flow_
Flow data structure.
Definition: flow.h:353
FlowGetProtoMapping
uint8_t FlowGetProtoMapping(uint8_t)
Function to map the protocol to the defined FLOW_PROTO_* enumeration.
Definition: flow-util.c:96
FlowEndCounters_::flow_tcp_liberal
uint16_t flow_tcp_liberal
Definition: flow-util.h:158
FlowEndCounters_::flow_state
uint16_t flow_state[FLOW_STATE_SIZE]
Definition: flow-util.h:156
TcpSession_::lossy_be_liberal
bool lossy_be_liberal
Definition: stream-tcp-private.h:282
Flow_::protoctx
void * protoctx
Definition: flow.h:451
FlowAlloc
Flow * FlowAlloc(void)
allocate a flow
Definition: flow-util.c:52
Flow_::flow_state
FlowStateType flow_state
Definition: flow.h:422
FlowEndCounters_::flow_tcp_state
uint16_t flow_tcp_state[TCP_CLOSED+1]
Definition: flow-util.h:157
FlowEndCounters
struct FlowEndCounters_ FlowEndCounters
FlowAllocDirect
Flow * FlowAllocDirect(void)
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:56
FlowFree
void FlowFree(Flow *)
cleanup & free the memory of a flow
Definition: flow-util.c:81
TcpSession_::state
uint8_t state
Definition: stream-tcp-private.h:273
Packet_
Definition: decode.h:434
stream-tcp-private.h
FLOW_STATE_SIZE
#define FLOW_STATE_SIZE
Definition: flow.h:520
TCP_CLOSED
@ TCP_CLOSED
Definition: stream-tcp-private.h:161
FlowEndCountersRegister
void FlowEndCountersRegister(ThreadVars *t, FlowEndCounters *fec)
Definition: flow-util.c:245
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:31
FlowInit
void FlowInit(Flow *, const Packet *)
Definition: flow-util.c:144
TcpSession_
Definition: stream-tcp-private.h:271
FlowGetReverseProtoMapping
uint8_t FlowGetReverseProtoMapping(uint8_t rproto)
Definition: flow-util.c:110
FlowEndCounters_
Definition: flow-util.h:155