1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/* Copyright (C) 2007-2013 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \file
*
* \author Victor Julien <victor@inliniac.net>
*
* Flow utility functions
*/
#include "suricata-common.h"
#include "threads.h"
#include "flow.h"
#include "flow-private.h"
#include "flow-util.h"
#include "flow-var.h"
#include "app-layer.h"
#include "util-var.h"
#include "util-debug.h"
#include "util-macset.h"
#include "flow-storage.h"
#include "detect.h"
#include "detect-engine-state.h"
#include "decode-icmpv4.h"
#include "util-validate.h"
/** \brief allocate a flow
*
* We check against the memuse counter. If it passes that check we increment
* the counter first, then we try to alloc.
*
* \retval f the flow or NULL on out of memory
*/
Flow *FlowAlloc(void)
{
Flow *f;
size_t size = sizeof(Flow) + FlowStorageSize();
if (!(FLOW_CHECK_MEMCAP(size))) {
return NULL;
}
(void) SC_ATOMIC_ADD(flow_memuse, size);
f = SCMalloc(size);
if (unlikely(f == NULL)) {
(void)SC_ATOMIC_SUB(flow_memuse, size);
return NULL;
}
memset(f, 0, size);
/* coverity[missing_lock] */
FLOW_INITIALIZE(f);
return f;
}
/**
* \brief cleanup & free the memory of a flow
*
* \param f flow to clear & destroy
*/
void FlowFree(Flow *f)
{
FLOW_DESTROY(f);
SCFree(f);
size_t size = sizeof(Flow) + FlowStorageSize();
(void) SC_ATOMIC_SUB(flow_memuse, size);
}
/**
* \brief Function to map the protocol to the defined FLOW_PROTO_* enumeration.
*
* \param proto protocol which is needed to be mapped
*/
uint8_t FlowGetProtoMapping(uint8_t proto)
{
switch (proto) {
case IPPROTO_TCP:
return FLOW_PROTO_TCP;
case IPPROTO_UDP:
return FLOW_PROTO_UDP;
case IPPROTO_ICMP:
return FLOW_PROTO_ICMP;
default:
return FLOW_PROTO_DEFAULT;
}
}
uint8_t FlowGetReverseProtoMapping(uint8_t rproto)
{
switch (rproto) {
case FLOW_PROTO_TCP:
return IPPROTO_TCP;
case FLOW_PROTO_UDP:
return IPPROTO_UDP;
case FLOW_PROTO_ICMP:
return IPPROTO_ICMP;
default:
exit(EXIT_FAILURE);
}
}
static inline void FlowSetICMPv4CounterPart(Flow *f)
{
int ctype = ICMPv4GetCounterpart(f->icmp_s.type);
if (ctype == -1)
return;
f->icmp_d.type = (uint8_t)ctype;
}
static inline void FlowSetICMPv6CounterPart(Flow *f)
{
int ctype = ICMPv6GetCounterpart(f->icmp_s.type);
if (ctype == -1)
return;
f->icmp_d.type = (uint8_t)ctype;
}
/* initialize the flow from the first packet
* we see from it. */
void FlowInit(Flow *f, const Packet *p)
{
SCEnter();
SCLogDebug("flow %p", f);
f->proto = p->proto;
f->recursion_level = p->recursion_level;
memcpy(&f->vlan_id[0], &p->vlan_id[0], sizeof(f->vlan_id));
f->vlan_idx = p->vlan_idx;
f->livedev = p->livedev;
if (PKT_IS_IPV4(p)) {
FLOW_SET_IPV4_SRC_ADDR_FROM_PACKET(p, &f->src);
FLOW_SET_IPV4_DST_ADDR_FROM_PACKET(p, &f->dst);
f->min_ttl_toserver = f->max_ttl_toserver = IPV4_GET_IPTTL((p));
f->flags |= FLOW_IPV4;
} else if (PKT_IS_IPV6(p)) {
FLOW_SET_IPV6_SRC_ADDR_FROM_PACKET(p, &f->src);
FLOW_SET_IPV6_DST_ADDR_FROM_PACKET(p, &f->dst);
f->min_ttl_toserver = f->max_ttl_toserver = IPV6_GET_HLIM((p));
f->flags |= FLOW_IPV6;
} else {
SCLogDebug("neither IPv4 or IPv6, weird");
DEBUG_VALIDATE_BUG_ON(1);
}
if (p->tcph != NULL) { /* XXX MACRO */
SET_TCP_SRC_PORT(p,&f->sp);
SET_TCP_DST_PORT(p,&f->dp);
} else if (p->udph != NULL) { /* XXX MACRO */
SET_UDP_SRC_PORT(p,&f->sp);
SET_UDP_DST_PORT(p,&f->dp);
} else if (p->icmpv4h != NULL) {
f->icmp_s.type = p->icmp_s.type;
f->icmp_s.code = p->icmp_s.code;
FlowSetICMPv4CounterPart(f);
} else if (p->icmpv6h != NULL) {
f->icmp_s.type = p->icmp_s.type;
f->icmp_s.code = p->icmp_s.code;
FlowSetICMPv6CounterPart(f);
} else if (p->sctph != NULL) { /* XXX MACRO */
SET_SCTP_SRC_PORT(p,&f->sp);
SET_SCTP_DST_PORT(p,&f->dp);
} else if (p->esph != NULL) {
f->esp.spi = ESP_GET_SPI(p);
} else {
/* nothing to do for this IP proto. */
SCLogDebug("no special setup for IP proto %u", p->proto);
}
f->startts = p->ts;
f->protomap = FlowGetProtoMapping(f->proto);
f->timeout_policy = FlowGetTimeoutPolicy(f);
const uint32_t timeout_at = (uint32_t)SCTIME_SECS(f->startts) + f->timeout_policy;
f->timeout_at = timeout_at;
if (MacSetFlowStorageEnabled()) {
DEBUG_VALIDATE_BUG_ON(FlowGetStorageById(f, MacSetGetFlowStorageID()) != NULL);
MacSet *ms = MacSetInit(10);
FlowSetStorageById(f, MacSetGetFlowStorageID(), ms);
}
SCReturn;
}
FlowStorageId g_bypass_info_id = { .id = -1 };
FlowStorageId GetFlowBypassInfoID(void)
{
return g_bypass_info_id;
}
static void FlowBypassFree(void *x)
{
FlowBypassInfo *fb = (FlowBypassInfo *) x;
if (fb == NULL)
return;
if (fb->bypass_data && fb->BypassFree) {
fb->BypassFree(fb->bypass_data);
}
SCFree(fb);
}
void RegisterFlowBypassInfo(void)
{
g_bypass_info_id = FlowStorageRegister("bypass_counters", sizeof(void *),
NULL, FlowBypassFree);
}
void FlowEndCountersRegister(ThreadVars *t, FlowEndCounters *fec)
{
for (int i = 0; i < FLOW_STATE_SIZE; i++) {
const char *name = NULL;
if (i == FLOW_STATE_NEW) {
name = "flow.end.state.new";
} else if (i == FLOW_STATE_ESTABLISHED) {
name = "flow.end.state.established";
} else if (i == FLOW_STATE_CLOSED) {
name = "flow.end.state.closed";
} else if (i == FLOW_STATE_LOCAL_BYPASSED) {
name = "flow.end.state.local_bypassed";
#ifdef CAPTURE_OFFLOAD
} else if (i == FLOW_STATE_CAPTURE_BYPASSED) {
name = "flow.end.state.capture_bypassed";
#endif
}
if (name) {
fec->flow_state[i] = StatsRegisterCounter(name, t);
}
}
for (enum TcpState i = TCP_NONE; i <= TCP_CLOSED; i++) {
const char *name = NULL;
switch (i) {
case TCP_NONE:
name = "flow.end.tcp_state.none";
break;
case TCP_SYN_SENT:
name = "flow.end.tcp_state.syn_sent";
break;
case TCP_SYN_RECV:
name = "flow.end.tcp_state.syn_recv";
break;
case TCP_ESTABLISHED:
name = "flow.end.tcp_state.established";
break;
case TCP_FIN_WAIT1:
name = "flow.end.tcp_state.fin_wait1";
break;
case TCP_FIN_WAIT2:
name = "flow.end.tcp_state.fin_wait2";
break;
case TCP_TIME_WAIT:
name = "flow.end.tcp_state.time_wait";
break;
case TCP_LAST_ACK:
name = "flow.end.tcp_state.last_ack";
break;
case TCP_CLOSE_WAIT:
name = "flow.end.tcp_state.close_wait";
break;
case TCP_CLOSING:
name = "flow.end.tcp_state.closing";
break;
case TCP_CLOSED:
name = "flow.end.tcp_state.closed";
break;
}
fec->flow_tcp_state[i] = StatsRegisterCounter(name, t);
}
fec->flow_tcp_liberal = StatsRegisterCounter("flow.end.tcp_liberal", t);
}
|