suricata
flow-hash.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2023 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  * \author Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
23  *
24  * Flow Hashing functions.
25  */
26 
27 #include "suricata-common.h"
28 #include "threads.h"
29 
30 #include "decode.h"
31 #include "detect-engine-state.h"
32 
33 #include "flow.h"
34 #include "flow-hash.h"
35 #include "flow-util.h"
36 #include "flow-private.h"
37 #include "flow-manager.h"
38 #include "flow-storage.h"
39 #include "flow-timeout.h"
40 #include "flow-spare-pool.h"
41 #include "app-layer-parser.h"
42 
43 #include "util-time.h"
44 #include "util-debug.h"
45 #include "util-device.h"
46 
47 #include "util-hash-lookup3.h"
48 
49 #include "conf.h"
50 #include "output.h"
51 #include "output-flow.h"
52 #include "stream-tcp.h"
53 #include "util-exception-policy.h"
54 
56 
57 
58 FlowBucket *flow_hash;
59 SC_ATOMIC_EXTERN(unsigned int, flow_prune_idx);
60 SC_ATOMIC_EXTERN(unsigned int, flow_flags);
61 
62 static Flow *FlowGetUsedFlow(ThreadVars *tv, DecodeThreadVars *dtv, const SCTime_t ts);
63 
64 /** \brief compare two raw ipv6 addrs
65  *
66  * \note we don't care about the real ipv6 ip's, this is just
67  * to consistently fill the FlowHashKey6 struct, without all
68  * the SCNtohl calls.
69  *
70  * \warning do not use elsewhere unless you know what you're doing.
71  * detect-engine-address-ipv6.c's AddressIPv6GtU32 is likely
72  * what you are looking for.
73  */
74 static inline int FlowHashRawAddressIPv6GtU32(const uint32_t *a, const uint32_t *b)
75 {
76  for (int i = 0; i < 4; i++) {
77  if (a[i] > b[i])
78  return 1;
79  if (a[i] < b[i])
80  break;
81  }
82 
83  return 0;
84 }
85 
86 typedef struct FlowHashKey4_ {
87  union {
88  struct {
89  uint32_t addrs[2];
90  uint16_t ports[2];
91  uint8_t proto; /**< u8 so proto and recur and livedev add up to u32 */
92  uint8_t recur;
93  uint16_t livedev;
95  uint16_t pad[1];
96  };
97  const uint32_t u32[6];
98  };
100 
101 typedef struct FlowHashKey6_ {
102  union {
103  struct {
104  uint32_t src[4], dst[4];
105  uint16_t ports[2];
106  uint8_t proto; /**< u8 so proto and recur and livedev add up to u32 */
107  uint8_t recur;
108  uint16_t livedev;
110  uint16_t pad[1];
111  };
112  const uint32_t u32[12];
113  };
115 
116 uint32_t FlowGetIpPairProtoHash(const Packet *p)
117 {
118  uint32_t hash = 0;
119  if (p->ip4h != NULL) {
120  FlowHashKey4 fhk = {
121  .pad[0] = 0,
122  };
123 
124  int ai = (p->src.addr_data32[0] > p->dst.addr_data32[0]);
125  fhk.addrs[1 - ai] = p->src.addr_data32[0];
126  fhk.addrs[ai] = p->dst.addr_data32[0];
127 
128  fhk.ports[0] = 0xfedc;
129  fhk.ports[1] = 0xba98;
130 
131  fhk.proto = (uint8_t)p->proto;
132  fhk.recur = (uint8_t)p->recursion_level;
133  /* g_vlan_mask sets the vlan_ids to 0 if vlan.use-for-tracking
134  * is disabled. */
135  fhk.vlan_id[0] = p->vlan_id[0] & g_vlan_mask;
136  fhk.vlan_id[1] = p->vlan_id[1] & g_vlan_mask;
137  fhk.vlan_id[2] = p->vlan_id[2] & g_vlan_mask;
138 
139  hash = hashword(fhk.u32, ARRAY_SIZE(fhk.u32), flow_config.hash_rand);
140  } else if (p->ip6h != NULL) {
141  FlowHashKey6 fhk = {
142  .pad[0] = 0,
143  };
144  if (FlowHashRawAddressIPv6GtU32(p->src.addr_data32, p->dst.addr_data32)) {
145  fhk.src[0] = p->src.addr_data32[0];
146  fhk.src[1] = p->src.addr_data32[1];
147  fhk.src[2] = p->src.addr_data32[2];
148  fhk.src[3] = p->src.addr_data32[3];
149  fhk.dst[0] = p->dst.addr_data32[0];
150  fhk.dst[1] = p->dst.addr_data32[1];
151  fhk.dst[2] = p->dst.addr_data32[2];
152  fhk.dst[3] = p->dst.addr_data32[3];
153  } else {
154  fhk.src[0] = p->dst.addr_data32[0];
155  fhk.src[1] = p->dst.addr_data32[1];
156  fhk.src[2] = p->dst.addr_data32[2];
157  fhk.src[3] = p->dst.addr_data32[3];
158  fhk.dst[0] = p->src.addr_data32[0];
159  fhk.dst[1] = p->src.addr_data32[1];
160  fhk.dst[2] = p->src.addr_data32[2];
161  fhk.dst[3] = p->src.addr_data32[3];
162  }
163 
164  fhk.ports[0] = 0xfedc;
165  fhk.ports[1] = 0xba98;
166  fhk.proto = (uint8_t)p->proto;
167  fhk.recur = (uint8_t)p->recursion_level;
168  fhk.vlan_id[0] = p->vlan_id[0] & g_vlan_mask;
169  fhk.vlan_id[1] = p->vlan_id[1] & g_vlan_mask;
170  fhk.vlan_id[2] = p->vlan_id[2] & g_vlan_mask;
171 
172  hash = hashword(fhk.u32, ARRAY_SIZE(fhk.u32), flow_config.hash_rand);
173  }
174  return hash;
175 }
176 
177 /* calculate the hash key for this packet
178  *
179  * we're using:
180  * hash_rand -- set at init time
181  * source port
182  * destination port
183  * source address
184  * destination address
185  * recursion level -- for tunnels, make sure different tunnel layers can
186  * never get mixed up.
187  *
188  * For ICMP we only consider UNREACHABLE errors atm.
189  */
190 static inline uint32_t FlowGetHash(const Packet *p)
191 {
192  uint32_t hash = 0;
193 
194  if (p->ip4h != NULL) {
195  if (p->tcph != NULL || p->udph != NULL) {
196  FlowHashKey4 fhk = { .pad[0] = 0 };
197 
198  int ai = (p->src.addr_data32[0] > p->dst.addr_data32[0]);
199  fhk.addrs[1-ai] = p->src.addr_data32[0];
200  fhk.addrs[ai] = p->dst.addr_data32[0];
201 
202  const int pi = (p->sp > p->dp);
203  fhk.ports[1-pi] = p->sp;
204  fhk.ports[pi] = p->dp;
205 
206  fhk.proto = p->proto;
207  fhk.recur = p->recursion_level;
208  /* g_livedev_mask sets the livedev ids to 0 if livedev.use-for-tracking
209  * is disabled. */
210  uint16_t devid = p->livedev ? p->livedev->id : 0;
211  fhk.livedev = devid & g_livedev_mask;
212  /* g_vlan_mask sets the vlan_ids to 0 if vlan.use-for-tracking
213  * is disabled. */
214  fhk.vlan_id[0] = p->vlan_id[0] & g_vlan_mask;
215  fhk.vlan_id[1] = p->vlan_id[1] & g_vlan_mask;
216  fhk.vlan_id[2] = p->vlan_id[2] & g_vlan_mask;
217 
218  hash = hashword(fhk.u32, ARRAY_SIZE(fhk.u32), flow_config.hash_rand);
219 
220  } else if (ICMPV4_DEST_UNREACH_IS_VALID(p)) {
221  uint32_t psrc = IPV4_GET_RAW_IPSRC_U32(ICMPV4_GET_EMB_IPV4(p));
222  uint32_t pdst = IPV4_GET_RAW_IPDST_U32(ICMPV4_GET_EMB_IPV4(p));
223  FlowHashKey4 fhk = { .pad[0] = 0 };
224 
225  const int ai = (psrc > pdst);
226  fhk.addrs[1-ai] = psrc;
227  fhk.addrs[ai] = pdst;
228 
229  const int pi = (p->icmpv4vars.emb_sport > p->icmpv4vars.emb_dport);
230  fhk.ports[1-pi] = p->icmpv4vars.emb_sport;
231  fhk.ports[pi] = p->icmpv4vars.emb_dport;
232 
233  fhk.proto = ICMPV4_GET_EMB_PROTO(p);
234  fhk.recur = p->recursion_level;
235  uint16_t devid = p->livedev ? p->livedev->id : 0;
236  fhk.livedev = devid & g_livedev_mask;
237  fhk.vlan_id[0] = p->vlan_id[0] & g_vlan_mask;
238  fhk.vlan_id[1] = p->vlan_id[1] & g_vlan_mask;
239  fhk.vlan_id[2] = p->vlan_id[2] & g_vlan_mask;
240 
241  hash = hashword(fhk.u32, ARRAY_SIZE(fhk.u32), flow_config.hash_rand);
242 
243  } else {
244  FlowHashKey4 fhk = { .pad[0] = 0 };
245  const int ai = (p->src.addr_data32[0] > p->dst.addr_data32[0]);
246  fhk.addrs[1-ai] = p->src.addr_data32[0];
247  fhk.addrs[ai] = p->dst.addr_data32[0];
248  fhk.ports[0] = 0xfeed;
249  fhk.ports[1] = 0xbeef;
250  fhk.proto = p->proto;
251  fhk.recur = p->recursion_level;
252  uint16_t devid = p->livedev ? p->livedev->id : 0;
253  fhk.livedev = devid & g_livedev_mask;
254  fhk.vlan_id[0] = p->vlan_id[0] & g_vlan_mask;
255  fhk.vlan_id[1] = p->vlan_id[1] & g_vlan_mask;
256  fhk.vlan_id[2] = p->vlan_id[2] & g_vlan_mask;
257 
258  hash = hashword(fhk.u32, ARRAY_SIZE(fhk.u32), flow_config.hash_rand);
259  }
260  } else if (p->ip6h != NULL) {
261  FlowHashKey6 fhk = { .pad[0] = 0 };
262  if (FlowHashRawAddressIPv6GtU32(p->src.addr_data32, p->dst.addr_data32)) {
263  fhk.src[0] = p->src.addr_data32[0];
264  fhk.src[1] = p->src.addr_data32[1];
265  fhk.src[2] = p->src.addr_data32[2];
266  fhk.src[3] = p->src.addr_data32[3];
267  fhk.dst[0] = p->dst.addr_data32[0];
268  fhk.dst[1] = p->dst.addr_data32[1];
269  fhk.dst[2] = p->dst.addr_data32[2];
270  fhk.dst[3] = p->dst.addr_data32[3];
271  } else {
272  fhk.src[0] = p->dst.addr_data32[0];
273  fhk.src[1] = p->dst.addr_data32[1];
274  fhk.src[2] = p->dst.addr_data32[2];
275  fhk.src[3] = p->dst.addr_data32[3];
276  fhk.dst[0] = p->src.addr_data32[0];
277  fhk.dst[1] = p->src.addr_data32[1];
278  fhk.dst[2] = p->src.addr_data32[2];
279  fhk.dst[3] = p->src.addr_data32[3];
280  }
281 
282  const int pi = (p->sp > p->dp);
283  fhk.ports[1-pi] = p->sp;
284  fhk.ports[pi] = p->dp;
285  fhk.proto = p->proto;
286  fhk.recur = p->recursion_level;
287  uint16_t devid = p->livedev ? p->livedev->id : 0;
288  fhk.livedev = devid & g_livedev_mask;
289  fhk.vlan_id[0] = p->vlan_id[0] & g_vlan_mask;
290  fhk.vlan_id[1] = p->vlan_id[1] & g_vlan_mask;
291  fhk.vlan_id[2] = p->vlan_id[2] & g_vlan_mask;
292 
293  hash = hashword(fhk.u32, ARRAY_SIZE(fhk.u32), flow_config.hash_rand);
294  }
295 
296  return hash;
297 }
298 
299 /**
300  * Basic hashing function for FlowKey
301  *
302  * \note Function only used for bypass and TCP or UDP flows
303  *
304  * \note this is only used at start to create Flow from pinned maps
305  * so fairness is not an issue
306  */
307 uint32_t FlowKeyGetHash(FlowKey *fk)
308 {
309  uint32_t hash = 0;
310 
311  if (fk->src.family == AF_INET) {
312  FlowHashKey4 fhk = {
313  .pad[0] = 0,
314  };
315  int ai = (fk->src.address.address_un_data32[0] > fk->dst.address.address_un_data32[0]);
316  fhk.addrs[1-ai] = fk->src.address.address_un_data32[0];
317  fhk.addrs[ai] = fk->dst.address.address_un_data32[0];
318 
319  const int pi = (fk->sp > fk->dp);
320  fhk.ports[1-pi] = fk->sp;
321  fhk.ports[pi] = fk->dp;
322 
323  fhk.proto = fk->proto;
324  fhk.recur = fk->recursion_level;
325  fhk.livedev = fk->livedev_id & g_livedev_mask;
326  fhk.vlan_id[0] = fk->vlan_id[0] & g_vlan_mask;
327  fhk.vlan_id[1] = fk->vlan_id[1] & g_vlan_mask;
328  fhk.vlan_id[2] = fk->vlan_id[2] & g_vlan_mask;
329 
330  hash = hashword(fhk.u32, ARRAY_SIZE(fhk.u32), flow_config.hash_rand);
331  } else {
332  FlowHashKey6 fhk = {
333  .pad[0] = 0,
334  };
335  if (FlowHashRawAddressIPv6GtU32(fk->src.address.address_un_data32,
337  fhk.src[0] = fk->src.address.address_un_data32[0];
338  fhk.src[1] = fk->src.address.address_un_data32[1];
339  fhk.src[2] = fk->src.address.address_un_data32[2];
340  fhk.src[3] = fk->src.address.address_un_data32[3];
341  fhk.dst[0] = fk->dst.address.address_un_data32[0];
342  fhk.dst[1] = fk->dst.address.address_un_data32[1];
343  fhk.dst[2] = fk->dst.address.address_un_data32[2];
344  fhk.dst[3] = fk->dst.address.address_un_data32[3];
345  } else {
346  fhk.src[0] = fk->dst.address.address_un_data32[0];
347  fhk.src[1] = fk->dst.address.address_un_data32[1];
348  fhk.src[2] = fk->dst.address.address_un_data32[2];
349  fhk.src[3] = fk->dst.address.address_un_data32[3];
350  fhk.dst[0] = fk->src.address.address_un_data32[0];
351  fhk.dst[1] = fk->src.address.address_un_data32[1];
352  fhk.dst[2] = fk->src.address.address_un_data32[2];
353  fhk.dst[3] = fk->src.address.address_un_data32[3];
354  }
355 
356  const int pi = (fk->sp > fk->dp);
357  fhk.ports[1-pi] = fk->sp;
358  fhk.ports[pi] = fk->dp;
359  fhk.proto = fk->proto;
360  fhk.recur = fk->recursion_level;
361  fhk.livedev = fk->livedev_id & g_livedev_mask;
362  fhk.vlan_id[0] = fk->vlan_id[0] & g_vlan_mask;
363  fhk.vlan_id[1] = fk->vlan_id[1] & g_vlan_mask;
364  fhk.vlan_id[2] = fk->vlan_id[2] & g_vlan_mask;
365 
366  hash = hashword(fhk.u32, ARRAY_SIZE(fhk.u32), flow_config.hash_rand);
367  }
368  return hash;
369 }
370 
371 static inline bool CmpAddrs(const uint32_t addr1[4], const uint32_t addr2[4])
372 {
373  return addr1[0] == addr2[0] && addr1[1] == addr2[1] &&
374  addr1[2] == addr2[2] && addr1[3] == addr2[3];
375 }
376 
377 static inline bool CmpAddrsAndPorts(const uint32_t src1[4],
378  const uint32_t dst1[4], Port src_port1, Port dst_port1,
379  const uint32_t src2[4], const uint32_t dst2[4], Port src_port2,
380  Port dst_port2)
381 {
382  /* Compare the source and destination addresses. If they are not equal,
383  * compare the first source address with the second destination address,
384  * and vice versa. Likewise for ports. */
385  return (CmpAddrs(src1, src2) && CmpAddrs(dst1, dst2) &&
386  src_port1 == src_port2 && dst_port1 == dst_port2) ||
387  (CmpAddrs(src1, dst2) && CmpAddrs(dst1, src2) &&
388  src_port1 == dst_port2 && dst_port1 == src_port2);
389 }
390 
391 static inline bool CmpVlanIds(
392  const uint16_t vlan_id1[VLAN_MAX_LAYERS], const uint16_t vlan_id2[VLAN_MAX_LAYERS])
393 {
394  return ((vlan_id1[0] ^ vlan_id2[0]) & g_vlan_mask) == 0 &&
395  ((vlan_id1[1] ^ vlan_id2[1]) & g_vlan_mask) == 0 &&
396  ((vlan_id1[2] ^ vlan_id2[2]) & g_vlan_mask) == 0;
397 }
398 
399 static inline bool CmpLiveDevIds(const LiveDevice *livedev, const uint16_t id)
400 {
401  uint16_t devid = livedev ? livedev->id : 0;
402  return (((devid ^ id) & g_livedev_mask) == 0);
403 }
404 
405 /* Since two or more flows can have the same hash key, we need to compare
406  * the flow with the current packet or flow key. */
407 static inline bool CmpFlowPacket(const Flow *f, const Packet *p)
408 {
409  const uint32_t *f_src = f->src.address.address_un_data32;
410  const uint32_t *f_dst = f->dst.address.address_un_data32;
411  const uint32_t *p_src = p->src.address.address_un_data32;
412  const uint32_t *p_dst = p->dst.address.address_un_data32;
413  return CmpAddrsAndPorts(f_src, f_dst, f->sp, f->dp, p_src, p_dst, p->sp, p->dp) &&
414  f->proto == p->proto && f->recursion_level == p->recursion_level &&
415  CmpVlanIds(f->vlan_id, p->vlan_id) && (f->livedev == p->livedev || g_livedev_mask == 0);
416 }
417 
418 static inline bool CmpFlowKey(const Flow *f, const FlowKey *k)
419 {
420  const uint32_t *f_src = f->src.address.address_un_data32;
421  const uint32_t *f_dst = f->dst.address.address_un_data32;
422  const uint32_t *k_src = k->src.address.address_un_data32;
423  const uint32_t *k_dst = k->dst.address.address_un_data32;
424  return CmpAddrsAndPorts(f_src, f_dst, f->sp, f->dp, k_src, k_dst, k->sp, k->dp) &&
425  f->proto == k->proto && f->recursion_level == k->recursion_level &&
426  CmpVlanIds(f->vlan_id, k->vlan_id) && CmpLiveDevIds(f->livedev, k->livedev_id);
427 }
428 
429 static inline bool CmpAddrsAndICMPTypes(const uint32_t src1[4],
430  const uint32_t dst1[4], uint8_t icmp_s_type1, uint8_t icmp_d_type1,
431  const uint32_t src2[4], const uint32_t dst2[4], uint8_t icmp_s_type2,
432  uint8_t icmp_d_type2)
433 {
434  /* Compare the source and destination addresses. If they are not equal,
435  * compare the first source address with the second destination address,
436  * and vice versa. Likewise for icmp types. */
437  return (CmpAddrs(src1, src2) && CmpAddrs(dst1, dst2) &&
438  icmp_s_type1 == icmp_s_type2 && icmp_d_type1 == icmp_d_type2) ||
439  (CmpAddrs(src1, dst2) && CmpAddrs(dst1, src2) &&
440  icmp_s_type1 == icmp_d_type2 && icmp_d_type1 == icmp_s_type2);
441 }
442 
443 static inline bool CmpFlowICMPPacket(const Flow *f, const Packet *p)
444 {
445  const uint32_t *f_src = f->src.address.address_un_data32;
446  const uint32_t *f_dst = f->dst.address.address_un_data32;
447  const uint32_t *p_src = p->src.address.address_un_data32;
448  const uint32_t *p_dst = p->dst.address.address_un_data32;
449  return CmpAddrsAndICMPTypes(f_src, f_dst, f->icmp_s.type, f->icmp_d.type, p_src, p_dst,
450  p->icmp_s.type, p->icmp_d.type) &&
451  f->proto == p->proto && f->recursion_level == p->recursion_level &&
452  CmpVlanIds(f->vlan_id, p->vlan_id) && (f->livedev == p->livedev || g_livedev_mask == 0);
453 }
454 
455 /**
456  * \brief See if a ICMP packet belongs to a flow by comparing the embedded
457  * packet in the ICMP error packet to the flow.
458  *
459  * \param f flow
460  * \param p ICMP packet
461  *
462  * \retval 1 match
463  * \retval 0 no match
464  */
465 static inline int FlowCompareICMPv4(Flow *f, const Packet *p)
466 {
468  /* first check the direction of the flow, in other words, the client ->
469  * server direction as it's most likely the ICMP error will be a
470  * response to the clients traffic */
471  if ((f->src.addr_data32[0] == IPV4_GET_RAW_IPSRC_U32(ICMPV4_GET_EMB_IPV4(p))) &&
472  (f->dst.addr_data32[0] == IPV4_GET_RAW_IPDST_U32(ICMPV4_GET_EMB_IPV4(p))) &&
473  f->sp == p->icmpv4vars.emb_sport && f->dp == p->icmpv4vars.emb_dport &&
475  CmpVlanIds(f->vlan_id, p->vlan_id) &&
476  (f->livedev == p->livedev || g_livedev_mask == 0)) {
477  return 1;
478 
479  /* check the less likely case where the ICMP error was a response to
480  * a packet from the server. */
481  } else if ((f->dst.addr_data32[0] == IPV4_GET_RAW_IPSRC_U32(ICMPV4_GET_EMB_IPV4(p))) &&
482  (f->src.addr_data32[0] == IPV4_GET_RAW_IPDST_U32(ICMPV4_GET_EMB_IPV4(p))) &&
483  f->dp == p->icmpv4vars.emb_sport && f->sp == p->icmpv4vars.emb_dport &&
484  f->proto == ICMPV4_GET_EMB_PROTO(p) &&
485  f->recursion_level == p->recursion_level && CmpVlanIds(f->vlan_id, p->vlan_id) &&
486  (f->livedev == p->livedev || g_livedev_mask == 0)) {
487  return 1;
488  }
489 
490  /* no match, fall through */
491  } else {
492  /* just treat ICMP as a normal proto for now */
493  return CmpFlowICMPPacket(f, p);
494  }
495 
496  return 0;
497 }
498 
499 /**
500  * \brief See if a IP-ESP packet belongs to a flow by comparing the SPI
501  *
502  * \param f flow
503  * \param p ESP packet
504  *
505  * \retval 1 match
506  * \retval 0 no match
507  */
508 static inline int FlowCompareESP(Flow *f, const Packet *p)
509 {
510  const uint32_t *f_src = f->src.address.address_un_data32;
511  const uint32_t *f_dst = f->dst.address.address_un_data32;
512  const uint32_t *p_src = p->src.address.address_un_data32;
513  const uint32_t *p_dst = p->dst.address.address_un_data32;
514 
515  return CmpAddrs(f_src, p_src) && CmpAddrs(f_dst, p_dst) && f->proto == p->proto &&
516  f->recursion_level == p->recursion_level && CmpVlanIds(f->vlan_id, p->vlan_id) &&
517  f->esp.spi == ESP_GET_SPI(p) && (f->livedev == p->livedev || g_livedev_mask == 0);
518 }
519 
521 {
522  p->flags |= PKT_WANTS_FLOW;
523  p->flow_hash = FlowGetHash(p);
524 }
525 
526 static inline int FlowCompare(Flow *f, const Packet *p)
527 {
528  if (p->proto == IPPROTO_ICMP) {
529  return FlowCompareICMPv4(f, p);
530  } else if (p->proto == IPPROTO_ESP) {
531  return FlowCompareESP(f, p);
532  } else {
533  return CmpFlowPacket(f, p);
534  }
535 }
536 
537 /**
538  * \brief Check if we should create a flow based on a packet
539  *
540  * We use this check to filter out flow creation based on:
541  * - ICMP error messages
542  * - TCP flags (emergency mode only)
543  *
544  * \param p packet
545  * \retval 1 true
546  * \retval 0 false
547  */
548 static inline int FlowCreateCheck(const Packet *p, const bool emerg)
549 {
550  /* if we're in emergency mode, don't try to create a flow for a TCP
551  * that is not a TCP SYN packet. */
552  if (emerg) {
553  if (PKT_IS_TCP(p)) {
554  if (((p->tcph->th_flags & (TH_SYN | TH_ACK | TH_RST | TH_FIN)) == TH_SYN) ||
556  ;
557  } else {
558  return 0;
559  }
560  }
561  }
562 
563  if (PKT_IS_ICMPV4(p)) {
564  if (ICMPV4_IS_ERROR_MSG(p)) {
565  return 0;
566  }
567  }
568 
569  return 1;
570 }
571 
572 static inline void FlowUpdateCounter(ThreadVars *tv, DecodeThreadVars *dtv,
573  uint8_t proto)
574 {
575 #ifdef UNITTESTS
576  if (tv && dtv) {
577 #endif
580  switch (proto){
581  case IPPROTO_UDP:
583  break;
584  case IPPROTO_TCP:
586  break;
587  case IPPROTO_ICMP:
589  break;
590  case IPPROTO_ICMPV6:
592  break;
593  }
594 #ifdef UNITTESTS
595  }
596 #endif
597 }
598 
599 /** \internal
600  * \brief try to fetch a new set of flows from the master flow pool.
601  *
602  * If in emergency mode, do this only once a second at max to avoid trying
603  * to synchronise per packet in the worse case. */
604 static inline Flow *FlowSpareSync(ThreadVars *tv, FlowLookupStruct *fls,
605  const Packet *p, const bool emerg)
606 {
607  Flow *f = NULL;
608  bool spare_sync = false;
609  if (emerg) {
610  if ((uint32_t)SCTIME_SECS(p->ts) > fls->emerg_spare_sync_stamp) {
611  fls->spare_queue = FlowSpareGetFromPool(); /* local empty, (re)populate and try again */
612  spare_sync = true;
614  if (f == NULL) {
615  /* wait till next full sec before retrying */
616  fls->emerg_spare_sync_stamp = (uint32_t)SCTIME_SECS(p->ts);
617  }
618  }
619  } else {
620  fls->spare_queue = FlowSpareGetFromPool(); /* local empty, (re)populate and try again */
622  spare_sync = true;
623  }
624 #ifdef UNITTESTS
625  if (tv && fls->dtv) {
626 #endif
627  if (spare_sync) {
628  if (f != NULL) {
630  if (fls->spare_queue.len < 99) {
632  }
633  } else if (fls->spare_queue.len == 0) {
635  }
637  }
638 #ifdef UNITTESTS
639  }
640 #endif
641  return f;
642 }
643 
644 static inline void NoFlowHandleIPS(Packet *p)
645 {
647 }
648 
649 /**
650  * \brief Get a new flow
651  *
652  * Get a new flow. We're checking memcap first and will try to make room
653  * if the memcap is reached.
654  *
655  * \param tv thread vars
656  * \param fls lookup support vars
657  *
658  * \retval f *LOCKED* flow on success, NULL on error or if we should not create
659  * a new flow.
660  */
661 static Flow *FlowGetNew(ThreadVars *tv, FlowLookupStruct *fls, Packet *p)
662 {
663  const bool emerg = ((SC_ATOMIC_GET(flow_flags) & FLOW_EMERGENCY) != 0);
664 #ifdef DEBUG
665  if (g_eps_flow_memcap != UINT64_MAX && g_eps_flow_memcap == p->pcap_cnt) {
666  NoFlowHandleIPS(p);
668  return NULL;
669  }
670 #endif
671  if (FlowCreateCheck(p, emerg) == 0) {
672  return NULL;
673  }
674 
675  /* get a flow from the spare queue */
677  if (f == NULL) {
678  f = FlowSpareSync(tv, fls, p, emerg);
679  }
680  if (f == NULL) {
681  /* If we reached the max memcap, we get a used flow */
682  if (!(FLOW_CHECK_MEMCAP(sizeof(Flow) + FlowStorageSize()))) {
683  /* declare state of emergency */
684  if (!(SC_ATOMIC_GET(flow_flags) & FLOW_EMERGENCY)) {
685  SC_ATOMIC_OR(flow_flags, FLOW_EMERGENCY);
688  }
689 
690  f = FlowGetUsedFlow(tv, fls->dtv, p->ts);
691  if (f == NULL) {
692  NoFlowHandleIPS(p);
693  return NULL;
694  }
695 #ifdef UNITTESTS
696  if (tv != NULL && fls->dtv != NULL) {
697 #endif
699 #ifdef UNITTESTS
700  }
701 #endif
702  /* flow is still locked from FlowGetUsedFlow() */
703  FlowUpdateCounter(tv, fls->dtv, p->proto);
704  return f;
705  }
706 
707  /* now see if we can alloc a new flow */
708  f = FlowAlloc();
709  if (f == NULL) {
710 #ifdef UNITTESTS
711  if (tv != NULL && fls->dtv != NULL) {
712 #endif
714 #ifdef UNITTESTS
715  }
716 #endif
717  NoFlowHandleIPS(p);
718  return NULL;
719  }
720 
721  /* flow is initialized but *unlocked* */
722  } else {
723  /* flow has been recycled before it went into the spare queue */
724 
725  /* flow is initialized (recycled) but *unlocked* */
726  }
727 
728  FLOWLOCK_WRLOCK(f);
729  FlowUpdateCounter(tv, fls->dtv, p->proto);
730  return f;
731 }
732 
733 static Flow *TcpReuseReplace(ThreadVars *tv, FlowLookupStruct *fls, FlowBucket *fb, Flow *old_f,
734  const uint32_t hash, Packet *p)
735 {
736 #ifdef UNITTESTS
737  if (tv != NULL && fls->dtv != NULL) {
738 #endif
740 #ifdef UNITTESTS
741  }
742 #endif
743  /* time out immediately */
744  old_f->timeout_at = 0;
745  /* get some settings that we move over to the new flow */
746  FlowThreadId thread_id[2] = { old_f->thread_id[0], old_f->thread_id[1] };
747 
748  /* flow is unlocked by caller */
749 
750  /* Get a new flow. It will be either a locked flow or NULL */
751  Flow *f = FlowGetNew(tv, fls, p);
752  if (f == NULL) {
753  return NULL;
754  }
755 
756  /* put at the start of the list */
757  f->next = fb->head;
758  fb->head = f;
759 
760  /* initialize and return */
761  FlowInit(f, p);
762  f->flow_hash = hash;
763  f->fb = fb;
765 
766  f->thread_id[0] = thread_id[0];
767  f->thread_id[1] = thread_id[1];
768 
770  return f;
771 }
772 
773 static inline bool FlowBelongsToUs(const ThreadVars *tv, const Flow *f)
774 {
775 #ifdef UNITTESTS
776  if (RunmodeIsUnittests()) {
777  return true;
778  }
779 #endif
780  return f->thread_id[0] == tv->id;
781 }
782 
783 static inline void MoveToWorkQueue(ThreadVars *tv, FlowLookupStruct *fls,
784  FlowBucket *fb, Flow *f, Flow *prev_f)
785 {
787 
788  /* remove from hash... */
789  if (prev_f) {
790  prev_f->next = f->next;
791  }
792  if (f == fb->head) {
793  fb->head = f->next;
794  }
795 
796  if (f->proto != IPPROTO_TCP || FlowBelongsToUs(tv, f)) { // TODO thread_id[] direction
797  f->fb = NULL;
798  f->next = NULL;
800  } else {
801  /* implied: TCP but our thread does not own it. So set it
802  * aside for the Flow Manager to pick it up. */
803  f->next = fb->evicted;
804  fb->evicted = f;
805  if (SC_ATOMIC_GET(f->fb->next_ts) != 0) {
806  SC_ATOMIC_SET(f->fb->next_ts, 0);
807  }
808  }
809 }
810 
811 static inline bool FlowIsTimedOut(const Flow *f, const uint32_t sec, const bool emerg)
812 {
813  if (unlikely(f->timeout_at < sec)) {
814  return true;
815  } else if (unlikely(emerg)) {
817 
818  int64_t timeout_at = f->timeout_at -
819  FlowGetFlowTimeoutDirect(flow_timeouts_delta, f->flow_state, f->protomap);
820  if ((int64_t)sec >= timeout_at)
821  return true;
822  }
823  return false;
824 }
825 
826 /** \brief Get Flow for packet
827  *
828  * Hash retrieval function for flows. Looks up the hash bucket containing the
829  * flow pointer. Then compares the packet with the found flow to see if it is
830  * the flow we need. If it isn't, walk the list until the right flow is found.
831  *
832  * If the flow is not found or the bucket was empty, a new flow is taken from
833  * the spare pool. The pool will alloc new flows as long as we stay within our
834  * memcap limit.
835  *
836  * The p->flow pointer is updated to point to the flow.
837  *
838  * \param tv thread vars
839  * \param dtv decode thread vars (for flow log api thread data)
840  *
841  * \retval f *LOCKED* flow or NULL
842  */
844 {
845  Flow *f = NULL;
846 
847  /* get our hash bucket and lock it */
848  const uint32_t hash = p->flow_hash;
849  FlowBucket *fb = &flow_hash[hash % flow_config.hash_size];
850  FBLOCK_LOCK(fb);
851 
852  SCLogDebug("fb %p fb->head %p", fb, fb->head);
853 
854  /* see if the bucket already has a flow */
855  if (fb->head == NULL) {
856  f = FlowGetNew(tv, fls, p);
857  if (f == NULL) {
858  FBLOCK_UNLOCK(fb);
859  return NULL;
860  }
861 
862  /* flow is locked */
863  fb->head = f;
864 
865  /* got one, now lock, initialize and return */
866  FlowInit(f, p);
867  f->flow_hash = hash;
868  f->fb = fb;
870 
871  FlowReference(dest, f);
872 
873  FBLOCK_UNLOCK(fb);
874  return f;
875  }
876 
877  const bool emerg = (SC_ATOMIC_GET(flow_flags) & FLOW_EMERGENCY) != 0;
878  const uint32_t fb_nextts = !emerg ? SC_ATOMIC_GET(fb->next_ts) : 0;
879  /* ok, we have a flow in the bucket. Let's find out if it is our flow */
880  Flow *prev_f = NULL; /* previous flow */
881  f = fb->head;
882  do {
883  Flow *next_f = NULL;
884  const bool timedout = (fb_nextts < (uint32_t)SCTIME_SECS(p->ts) &&
885  FlowIsTimedOut(f, (uint32_t)SCTIME_SECS(p->ts), emerg));
886  if (timedout) {
887  FLOWLOCK_WRLOCK(f);
888  next_f = f->next;
889  MoveToWorkQueue(tv, fls, fb, f, prev_f);
890  FLOWLOCK_UNLOCK(f);
891  goto flow_removed;
892  } else if (FlowCompare(f, p) != 0) {
893  FLOWLOCK_WRLOCK(f);
894  /* found a matching flow that is not timed out */
895  if (unlikely(TcpSessionPacketSsnReuse(p, f, f->protoctx) == 1)) {
896  Flow *new_f = TcpReuseReplace(tv, fls, fb, f, hash, p);
897  if (prev_f == NULL) /* if we have no prev it means new_f is now our prev */
898  prev_f = new_f;
899  MoveToWorkQueue(tv, fls, fb, f, prev_f); /* evict old flow */
900  FLOWLOCK_UNLOCK(f); /* unlock old replaced flow */
901 
902  if (new_f == NULL) {
903  FBLOCK_UNLOCK(fb);
904  return NULL;
905  }
906  f = new_f;
907  }
908  FlowReference(dest, f);
909  FBLOCK_UNLOCK(fb);
910  return f; /* return w/o releasing flow lock */
911  }
912  /* unless we removed 'f', prev_f needs to point to
913  * current 'f' when adding a new flow below. */
914  prev_f = f;
915  next_f = f->next;
916 
917 flow_removed:
918  if (next_f == NULL) {
919  f = FlowGetNew(tv, fls, p);
920  if (f == NULL) {
921  FBLOCK_UNLOCK(fb);
922  return NULL;
923  }
924 
925  /* flow is locked */
926 
927  f->next = fb->head;
928  fb->head = f;
929 
930  /* initialize and return */
931  FlowInit(f, p);
932  f->flow_hash = hash;
933  f->fb = fb;
935  FlowReference(dest, f);
936  FBLOCK_UNLOCK(fb);
937  return f;
938  }
939  f = next_f;
940  } while (f != NULL);
941 
942  /* should be unreachable */
943  BUG_ON(1);
944  return NULL;
945 }
946 
947 /** \internal
948  * \retval true if flow matches key
949  * \retval false if flow does not match key, or unsupported protocol
950  * \note only supports TCP & UDP
951  */
952 static inline bool FlowCompareKey(Flow *f, FlowKey *key)
953 {
954  if ((f->proto != IPPROTO_TCP) && (f->proto != IPPROTO_UDP))
955  return false;
956  return CmpFlowKey(f, key);
957 }
958 
959 /** \brief Look for existing Flow using a flow id value
960  *
961  * Hash retrieval function for flows. Looks up the hash bucket containing the
962  * flow pointer. Then compares the flow_id with the found flow's flow_id to see
963  * if it is the flow we need.
964  *
965  * \param flow_id Flow ID of the flow to look for
966  * \retval f *LOCKED* flow or NULL
967  */
969 {
970  uint32_t hash = flow_id & 0x0000FFFF;
971  FlowBucket *fb = &flow_hash[hash % flow_config.hash_size];
972  FBLOCK_LOCK(fb);
973  SCLogDebug("fb %p fb->head %p", fb, fb->head);
974 
975  for (Flow *f = fb->head; f != NULL; f = f->next) {
976  if (FlowGetId(f) == flow_id) {
977  /* found our flow, lock & return */
978  FLOWLOCK_WRLOCK(f);
979  FBLOCK_UNLOCK(fb);
980  return f;
981  }
982  }
983  FBLOCK_UNLOCK(fb);
984  return NULL;
985 }
986 
987 /** \brief Look for existing Flow using a FlowKey
988  *
989  * Hash retrieval function for flows. Looks up the hash bucket containing the
990  * flow pointer. Then compares the key with the found flow to see if it is
991  * the flow we need. If it isn't, walk the list until the right flow is found.
992  *
993  * \param key Pointer to FlowKey build using flow to look for
994  * \param hash Value of the flow hash
995  * \retval f *LOCKED* flow or NULL
996  */
997 static Flow *FlowGetExistingFlowFromHash(FlowKey *key, const uint32_t hash)
998 {
999  /* get our hash bucket and lock it */
1000  FlowBucket *fb = &flow_hash[hash % flow_config.hash_size];
1001  FBLOCK_LOCK(fb);
1002  SCLogDebug("fb %p fb->head %p", fb, fb->head);
1003 
1004  for (Flow *f = fb->head; f != NULL; f = f->next) {
1005  /* see if this is the flow we are looking for */
1006  if (FlowCompareKey(f, key)) {
1007  /* found our flow, lock & return */
1008  FLOWLOCK_WRLOCK(f);
1009  FBLOCK_UNLOCK(fb);
1010  return f;
1011  }
1012  }
1013 
1014  FBLOCK_UNLOCK(fb);
1015  return NULL;
1016 }
1017 
1018 /** \brief Get or create a Flow using a FlowKey
1019  *
1020  * Hash retrieval function for flows. Looks up the hash bucket containing the
1021  * flow pointer. Then compares the packet with the found flow to see if it is
1022  * the flow we need. If it isn't, walk the list until the right flow is found.
1023  * Return a new Flow if ever no Flow was found.
1024  *
1025  *
1026  * \param key Pointer to FlowKey build using flow to look for
1027  * \param ttime time to use for flow creation
1028  * \param hash Value of the flow hash
1029  * \retval f *LOCKED* flow or NULL
1030  */
1031 
1032 Flow *FlowGetFromFlowKey(FlowKey *key, struct timespec *ttime, const uint32_t hash)
1033 {
1034  Flow *f = FlowGetExistingFlowFromHash(key, hash);
1035 
1036  if (f != NULL) {
1037  return f;
1038  }
1039  /* TODO use spare pool */
1040  /* now see if we can alloc a new flow */
1041  f = FlowAlloc();
1042  if (f == NULL) {
1043  SCLogDebug("Can't get a spare flow at start");
1044  return NULL;
1045  }
1046  f->proto = key->proto;
1047  memcpy(&f->vlan_id[0], &key->vlan_id[0], sizeof(f->vlan_id));
1048  ;
1049  f->src.addr_data32[0] = key->src.addr_data32[0];
1050  f->src.addr_data32[1] = key->src.addr_data32[1];
1051  f->src.addr_data32[2] = key->src.addr_data32[2];
1052  f->src.addr_data32[3] = key->src.addr_data32[3];
1053  f->dst.addr_data32[0] = key->dst.addr_data32[0];
1054  f->dst.addr_data32[1] = key->dst.addr_data32[1];
1055  f->dst.addr_data32[2] = key->dst.addr_data32[2];
1056  f->dst.addr_data32[3] = key->dst.addr_data32[3];
1057  f->sp = key->sp;
1058  f->dp = key->dp;
1059  f->recursion_level = 0;
1060  // f->livedev is set by caller EBPFCreateFlowForKey
1061  f->flow_hash = hash;
1062  if (key->src.family == AF_INET) {
1063  f->flags |= FLOW_IPV4;
1064  } else if (key->src.family == AF_INET6) {
1065  f->flags |= FLOW_IPV6;
1066  }
1067 
1069  /* set timestamp to now */
1070  f->startts = SCTIME_FROM_TIMESPEC(ttime);
1071  f->lastts = f->startts;
1072 
1073  FlowBucket *fb = &flow_hash[hash % flow_config.hash_size];
1074  FBLOCK_LOCK(fb);
1075  f->fb = fb;
1076  f->next = fb->head;
1077  fb->head = f;
1078  FLOWLOCK_WRLOCK(f);
1079  FBLOCK_UNLOCK(fb);
1080  return f;
1081 }
1082 
1083 #define FLOW_GET_NEW_TRIES 5
1085 /* inline locking wrappers to make profiling easier */
1086 
1087 static inline int GetUsedTryLockBucket(FlowBucket *fb)
1088 {
1089  int r = FBLOCK_TRYLOCK(fb);
1090  return r;
1091 }
1092 static inline int GetUsedTryLockFlow(Flow *f)
1093 {
1094  int r = FLOWLOCK_TRYWRLOCK(f);
1095  return r;
1096 }
1097 static inline uint32_t GetUsedAtomicUpdate(const uint32_t val)
1098 {
1099  uint32_t r = SC_ATOMIC_ADD(flow_prune_idx, val);
1100  return r;
1101 }
1102 
1103 /** \internal
1104  * \brief check if flow has just seen an update.
1105  */
1106 static inline bool StillAlive(const Flow *f, const SCTime_t ts)
1107 {
1108  switch (f->flow_state) {
1109  case FLOW_STATE_NEW:
1110  if (SCTIME_SECS(ts) - SCTIME_SECS(f->lastts) <= 1) {
1111  return true;
1112  }
1113  break;
1115  if (SCTIME_SECS(ts) - SCTIME_SECS(f->lastts) <= 5) {
1116  return true;
1117  }
1118  break;
1119  case FLOW_STATE_CLOSED:
1120  if (SCTIME_SECS(ts) - SCTIME_SECS(f->lastts) <= 3) {
1121  return true;
1122  }
1123  break;
1124  default:
1125  if (SCTIME_SECS(ts) - SCTIME_SECS(f->lastts) < 30) {
1126  return true;
1127  }
1128  break;
1129  }
1130  return false;
1131 }
1132 
1133 #ifdef UNITTESTS
1134  #define STATSADDUI64(cnt, value) \
1135  if (tv && dtv) { \
1136  StatsAddUI64(tv, dtv->cnt, (value)); \
1137  }
1138 #else
1139  #define STATSADDUI64(cnt, value) \
1140  StatsAddUI64(tv, dtv->cnt, (value));
1141 #endif
1142 
1143 /** \internal
1144  * \brief Get a flow from the hash directly.
1145  *
1146  * Called in conditions where the spare queue is empty and memcap is reached.
1147  *
1148  * Walks the hash until a flow can be freed. Timeouts are disregarded.
1149  * "flow_prune_idx" atomic int makes sure we don't start at the
1150  * top each time since that would clear the top of the hash leading to longer
1151  * and longer search times under high pressure (observed).
1152  *
1153  * \param tv thread vars
1154  * \param dtv decode thread vars (for flow log api thread data)
1155  *
1156  * \retval f flow or NULL
1157  */
1158 static Flow *FlowGetUsedFlow(ThreadVars *tv, DecodeThreadVars *dtv, const SCTime_t ts)
1159 {
1160  uint32_t idx = GetUsedAtomicUpdate(FLOW_GET_NEW_TRIES) % flow_config.hash_size;
1161  uint32_t tried = 0;
1162 
1163  while (1) {
1164  if (tried++ > FLOW_GET_NEW_TRIES) {
1165  STATSADDUI64(counter_flow_get_used_eval, tried);
1166  break;
1167  }
1168  if (++idx >= flow_config.hash_size)
1169  idx = 0;
1170 
1171  FlowBucket *fb = &flow_hash[idx];
1172 
1173  if (SC_ATOMIC_GET(fb->next_ts) == UINT_MAX)
1174  continue;
1175 
1176  if (GetUsedTryLockBucket(fb) != 0) {
1177  STATSADDUI64(counter_flow_get_used_eval_busy, 1);
1178  continue;
1179  }
1180 
1181  Flow *f = fb->head;
1182  if (f == NULL) {
1183  FBLOCK_UNLOCK(fb);
1184  continue;
1185  }
1186 
1187  if (GetUsedTryLockFlow(f) != 0) {
1188  STATSADDUI64(counter_flow_get_used_eval_busy, 1);
1189  FBLOCK_UNLOCK(fb);
1190  continue;
1191  }
1192 
1193  if (StillAlive(f, ts)) {
1194  STATSADDUI64(counter_flow_get_used_eval_reject, 1);
1195  FBLOCK_UNLOCK(fb);
1196  FLOWLOCK_UNLOCK(f);
1197  continue;
1198  }
1199 
1200  /* remove from the hash */
1201  fb->head = f->next;
1202  f->next = NULL;
1203  f->fb = NULL;
1204  FBLOCK_UNLOCK(fb);
1205 
1206  /* rest of the flags is updated on-demand in output */
1208  if (SC_ATOMIC_GET(flow_flags) & FLOW_EMERGENCY)
1210 
1211  /* invoke flow log api */
1212 #ifdef UNITTESTS
1213  if (dtv) {
1214 #endif
1217  }
1218 #ifdef UNITTESTS
1219  }
1220 #endif
1221 
1222  FlowClearMemory(f, f->protomap);
1223 
1224  /* leave locked */
1225 
1226  STATSADDUI64(counter_flow_get_used_eval, tried);
1227  return f;
1228  }
1229 
1230  STATSADDUI64(counter_flow_get_used_failed, 1);
1231  return NULL;
1232 }
FlowHashKey4_::ports
uint16_t ports[2]
Definition: flow-hash.c:90
FlowLookupStruct_::work_queue
FlowQueuePrivate work_queue
Definition: flow.h:542
OutputFlowLog
TmEcode OutputFlowLog(ThreadVars *tv, void *thread_data, Flow *f)
Run flow logger(s)
Definition: output-flow.c:85
Packet_::proto
uint8_t proto
Definition: decode.h:452
DecodeThreadVars_::counter_flow_udp
uint16_t counter_flow_udp
Definition: decode.h:736
ts
uint64_t ts
Definition: source-erf-file.c:55
ExceptionPolicyApply
void ExceptionPolicyApply(Packet *p, enum ExceptionPolicy policy, enum PacketDropReason drop_reason)
Definition: util-exception-policy.c:68
g_livedev_mask
uint16_t g_livedev_mask
Definition: suricata.c:210
Packet_::icmp_s
struct Packet_::@31::@41 icmp_s
DecodeThreadVars_::counter_flow_active
uint16_t counter_flow_active
Definition: decode.h:734
ICMPV4_GET_EMB_PROTO
#define ICMPV4_GET_EMB_PROTO(p)
Definition: decode-icmpv4.h:250
Flow_::recursion_level
uint8_t recursion_level
Definition: flow.h:373
StatsIncr
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
Definition: counters.c:167
IPV4_GET_RAW_IPDST_U32
#define IPV4_GET_RAW_IPDST_U32(ip4h)
Definition: decode-ipv4.h:109
hashword
uint32_t hashword(const uint32_t *k, size_t length, uint32_t initval)
Definition: util-hash-lookup3.c:174
FLOW_STATE_ESTABLISHED
@ FLOW_STATE_ESTABLISHED
Definition: flow.h:501
flow-util.h
FBLOCK_LOCK
#define FBLOCK_LOCK(fb)
Definition: flow-hash.h:73
FlowLookupStruct_::dtv
DecodeThreadVars * dtv
Definition: flow.h:541
DecodeThreadVars_::counter_flow_icmp4
uint16_t counter_flow_icmp4
Definition: decode.h:737
Flow_::icmp_d
struct Flow_::@119::@125 icmp_d
Flow_::startts
SCTime_t startts
Definition: flow.h:489
stream-tcp.h
FlowKey_::src
Address src
Definition: flow.h:304
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SC_ATOMIC_SET
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:387
PKT_DROP_REASON_FLOW_MEMCAP
@ PKT_DROP_REASON_FLOW_MEMCAP
Definition: decode.h:395
FlowCnf_::hash_size
uint32_t hash_size
Definition: flow.h:288
FlowAddress_::address_un_data32
uint32_t address_un_data32[4]
Definition: flow.h:314
FlowSpareGetFromPool
FlowQueuePrivate FlowSpareGetFromPool(void)
Definition: flow-spare-pool.c:175
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
Packet_::pcap_cnt
uint64_t pcap_cnt
Definition: decode.h:598
DecodeThreadVars_::counter_flow_spare_sync_avg
uint16_t counter_flow_spare_sync_avg
Definition: decode.h:749
Flow_::proto
uint8_t proto
Definition: flow.h:372
Packet_::flags
uint32_t flags
Definition: decode.h:467
FlowKeyGetHash
uint32_t FlowKeyGetHash(FlowKey *fk)
Definition: flow-hash.c:307
threads.h
ICMPV4_DEST_UNREACH_IS_VALID
#define ICMPV4_DEST_UNREACH_IS_VALID(p)
Definition: decode-icmpv4.h:267
TH_RST
#define TH_RST
Definition: decode-tcp.h:36
flow-private.h
Flow_
Flow data structure.
Definition: flow.h:350
FlowHashKey4_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: flow-hash.c:94
TH_FIN
#define TH_FIN
Definition: decode-tcp.h:34
Flow_::protomap
uint8_t protomap
Definition: flow.h:444
LiveDevice_
Definition: util-device.h:50
SC_ATOMIC_ADD
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
Definition: util-atomic.h:333
FlowProtoTimeout_
Definition: flow.h:514
LiveDevice_::id
uint16_t id
Definition: util-device.h:56
FLOWLOCK_TRYWRLOCK
#define FLOWLOCK_TRYWRLOCK(fb)
Definition: flow.h:266
PKT_WANTS_FLOW
#define PKT_WANTS_FLOW
Definition: decode.h:1035
flow-hash.h
Packet_::icmp_d
struct Packet_::@33::@42 icmp_d
FlowLookupStruct_
Definition: flow.h:538
FlowHashKey4
struct FlowHashKey4_ FlowHashKey4
FBLOCK_TRYLOCK
#define FBLOCK_TRYLOCK(fb)
Definition: flow-hash.h:74
TcpStreamCnf_
Definition: stream-tcp.h:43
DecodeThreadVars_::counter_flow_tcp
uint16_t counter_flow_tcp
Definition: decode.h:735
Address_::address_un_data32
uint32_t address_un_data32[4]
Definition: decode.h:118
proto
uint8_t proto
Definition: decode-template.h:0
FlowHashKey6_::recur
uint8_t recur
Definition: flow-hash.c:107
Flow_::dp
Port dp
Definition: flow.h:366
stream_config
TcpStreamCnf stream_config
Definition: stream-tcp.c:115
FlowQueuePrivate_::len
uint32_t len
Definition: flow-queue.h:44
Flow_::protoctx
void * protoctx
Definition: flow.h:440
FLOW_IPV4
#define FLOW_IPV4
Definition: flow.h:96
DecodeThreadVars_::counter_flow_get_used
uint16_t counter_flow_get_used
Definition: decode.h:740
FLOWLOCK_UNLOCK
#define FLOWLOCK_UNLOCK(fb)
Definition: flow.h:267
Flow_::flow_state
FlowStateType flow_state
Definition: flow.h:411
DecodeThreadVars_::counter_flow_spare_sync_empty
uint16_t counter_flow_spare_sync_empty
Definition: decode.h:747
DecodeThreadVars_::counter_flow_tcp_reuse
uint16_t counter_flow_tcp_reuse
Definition: decode.h:739
DecodeThreadVars_::counter_flow_total
uint16_t counter_flow_total
Definition: decode.h:733
FlowWakeupFlowManagerThread
void FlowWakeupFlowManagerThread(void)
Definition: flow-manager.c:92
FLOW_CHECK_MEMCAP
#define FLOW_CHECK_MEMCAP(size)
check if a memory alloc would fit in the memcap
Definition: flow-util.h:136
FlowLookupStruct_::emerg_spare_sync_stamp
uint32_t emerg_spare_sync_stamp
Definition: flow.h:543
TcpSessionPacketSsnReuse
int TcpSessionPacketSsnReuse(const Packet *p, const Flow *f, const void *tcp_ssn)
Definition: stream-tcp.c:5696
PKT_IS_TCP
#define PKT_IS_TCP(p)
Definition: decode.h:247
flow-spare-pool.h
DecodeThreadVars_::counter_flow_spare_sync
uint16_t counter_flow_spare_sync
Definition: decode.h:746
Flow_::dst
FlowAddress dst
Definition: flow.h:353
Flow_::fb
struct FlowBucket_ * fb
Definition: flow.h:487
FlowHashKey6_::ports
uint16_t ports[2]
Definition: flow-hash.c:105
Flow_::esp
struct Flow_::@117::@124 esp
FlowHashKey4_::u32
const uint32_t u32[6]
Definition: flow-hash.c:97
decode.h
util-device.h
util-debug.h
STREAM_PKT_FLAG_TCP_PORT_REUSE
#define STREAM_PKT_FLAG_TCP_PORT_REUSE
Definition: stream-tcp-private.h:320
FLOW_GET_NEW_TRIES
#define FLOW_GET_NEW_TRIES
Definition: flow-hash.c:1083
FLOW_PROTO_MAX
@ FLOW_PROTO_MAX
Definition: flow-private.h:74
STATSADDUI64
#define STATSADDUI64(cnt, value)
Definition: flow-hash.c:1134
SCTIME_FROM_TIMESPEC
#define SCTIME_FROM_TIMESPEC(ts)
Definition: util-time.h:91
Packet_::ts
SCTime_t ts
Definition: decode.h:475
ICMPV4_GET_EMB_IPV4
#define ICMPV4_GET_EMB_IPV4(p)
Definition: decode-icmpv4.h:252
FlowHashKey4_::livedev
uint16_t livedev
Definition: flow-hash.c:93
util-exception-policy.h
FlowHashKey6_::proto
uint8_t proto
Definition: flow-hash.c:106
Flow_::lastts
SCTime_t lastts
Definition: flow.h:409
FLOWLOCK_WRLOCK
#define FLOWLOCK_WRLOCK(fb)
Definition: flow.h:264
SC_ATOMIC_EXTERN
SC_ATOMIC_EXTERN(unsigned int, flow_prune_idx)
FlowKey_::recursion_level
uint8_t recursion_level
Definition: flow.h:307
ICMPV4Vars_::emb_dport
uint16_t emb_dport
Definition: decode-icmpv4.h:204
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
Flow_::flow_end_flags
uint8_t flow_end_flags
Definition: flow.h:446
FlowStorageSize
unsigned int FlowStorageSize(void)
Definition: flow-storage.c:35
Packet_::sp
Port sp
Definition: decode.h:437
FlowHashKey6
struct FlowHashKey6_ FlowHashKey6
FlowHashKey4_::pad
uint16_t pad[1]
Definition: flow-hash.c:95
TH_ACK
#define TH_ACK
Definition: decode-tcp.h:38
util-time.h
FlowQueuePrivateGetFromTop
Flow * FlowQueuePrivateGetFromTop(FlowQueuePrivate *fqc)
Definition: flow-queue.c:151
app-layer-parser.h
FlowKey_::livedev_id
uint16_t livedev_id
Definition: flow.h:308
ThreadVars_::id
int id
Definition: threadvars.h:86
FlowHashKey6_::livedev
uint16_t livedev
Definition: flow-hash.c:108
Flow_::icmp_s
struct Flow_::@117::@123 icmp_s
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
Address_::address
union Address_::@30 address
FlowThreadId
uint16_t FlowThreadId
Definition: flow.h:327
FlowKey_::sp
Port sp
Definition: flow.h:305
FlowTimeoutsEmergency
void FlowTimeoutsEmergency(void)
Definition: flow-manager.c:111
FlowGetProtoMapping
uint8_t FlowGetProtoMapping(uint8_t proto)
Function to map the protocol to the defined FLOW_PROTO_* enumeration.
Definition: flow-util.c:97
Packet_
Definition: decode.h:430
FlowHashKey6_::pad
uint16_t pad[1]
Definition: flow-hash.c:110
ESP_GET_SPI
#define ESP_GET_SPI(p)
Get the spi field off a packet.
Definition: decode-esp.h:32
FlowGetExistingFlowFromFlowId
Flow * FlowGetExistingFlowFromFlowId(int64_t flow_id)
Look for existing Flow using a flow id value.
Definition: flow-hash.c:968
conf.h
Packet_::ip4h
IPV4Hdr * ip4h
Definition: decode.h:535
Port
uint16_t Port
Definition: decode.h:229
FLOW_END_FLAG_EMERGENCY
#define FLOW_END_FLAG_EMERGENCY
Definition: flow.h:236
FBLOCK_UNLOCK
#define FBLOCK_UNLOCK(fb)
Definition: flow-hash.h:75
SCTime_t
Definition: util-time.h:40
Packet_::livedev
struct LiveDevice_ * livedev
Definition: decode.h:590
FlowHashKey4_
Definition: flow-hash.c:86
FlowClearMemory
int FlowClearMemory(Flow *f, uint8_t proto_map)
Function clear the flow memory before queueing it to spare flow queue.
Definition: flow.c:1113
flow_timeouts_delta
FlowProtoTimeout flow_timeouts_delta[FLOW_PROTO_MAX]
Definition: flow.c:99
FlowCnf_::hash_rand
uint32_t hash_rand
Definition: flow.h:287
FlowHashKey4_::addrs
uint32_t addrs[2]
Definition: flow-hash.c:89
output-flow.h
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
FlowHashKey6_
Definition: flow-hash.c:101
flow-timeout.h
Flow_::flow_hash
uint32_t flow_hash
Definition: flow.h:400
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:252
FlowUpdateState
void FlowUpdateState(Flow *f, const enum FlowState s)
Definition: flow.c:1178
Flow_::src
FlowAddress src
Definition: flow.h:353
Flow_::next
struct Flow_ * next
Definition: flow.h:395
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:33
FlowHashKey6_::src
uint32_t src[4]
Definition: flow-hash.c:104
FlowLookupStruct_::spare_queue
FlowQueuePrivate spare_queue
Definition: flow.h:540
FlowGetIpPairProtoHash
uint32_t FlowGetIpPairProtoHash(const Packet *p)
Definition: flow-hash.c:116
ARRAY_SIZE
#define ARRAY_SIZE(arr)
Definition: suricata-common.h:547
flow_hash
FlowBucket * flow_hash
Definition: flow-hash.c:58
Packet_::icmpv4vars
ICMPV4Vars icmpv4vars
Definition: decode.h:550
flow-storage.h
TH_SYN
#define TH_SYN
Definition: decode-tcp.h:35
FLOW_STATE_NEW
@ FLOW_STATE_NEW
Definition: flow.h:500
DecodeThreadVars_::counter_flow_spare_sync_incomplete
uint16_t counter_flow_spare_sync_incomplete
Definition: decode.h:748
flow-manager.h
suricata-common.h
IPV4_GET_RAW_IPSRC_U32
#define IPV4_GET_RAW_IPSRC_U32(ip4h)
Definition: decode-ipv4.h:107
FLOW_IPV6
#define FLOW_IPV6
Definition: flow.h:98
DecodeThreadVars_::counter_flow_icmp6
uint16_t counter_flow_icmp6
Definition: decode.h:738
Packet_::tcph
TCPHdr * tcph
Definition: decode.h:557
flow_config
FlowConfig flow_config
Definition: flow.c:102
FlowKey_::dst
Address dst
Definition: flow.h:304
SCTIME_SECS
#define SCTIME_SECS(t)
Definition: util-time.h:57
VLAN_MAX_LAYERS
#define VLAN_MAX_LAYERS
Definition: decode-vlan.h:51
util-hash-lookup3.h
FlowGetFlowFromHash
Flow * FlowGetFlowFromHash(ThreadVars *tv, FlowLookupStruct *fls, Packet *p, Flow **dest)
Get Flow for packet.
Definition: flow-hash.c:843
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
Flow_::livedev
struct LiveDevice_ * livedev
Definition: flow.h:397
PKT_IS_ICMPV4
#define PKT_IS_ICMPV4(p)
Definition: decode.h:249
FlowHashKey6_::dst
uint32_t dst[4]
Definition: flow-hash.c:104
TcpStreamCnf_::midstream
bool midstream
Definition: stream-tcp.h:59
FlowCnf_::memcap_policy
enum ExceptionPolicy memcap_policy
Definition: flow.h:296
StatsAddUI64
void StatsAddUI64(ThreadVars *tv, uint16_t id, uint64_t x)
Adds a value of type uint64_t to the local counter.
Definition: counters.c:146
ICMPV4_IS_ERROR_MSG
#define ICMPV4_IS_ERROR_MSG(p)
Definition: decode-icmpv4.h:284
g_vlan_mask
uint16_t g_vlan_mask
Definition: suricata.c:206
Packet_::flow_hash
uint32_t flow_hash
Definition: decode.h:473
FlowKey_::proto
uint8_t proto
Definition: flow.h:306
FLOW_STATE_CLOSED
@ FLOW_STATE_CLOSED
Definition: flow.h:502
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:673
FlowGetFromFlowKey
Flow * FlowGetFromFlowKey(FlowKey *key, struct timespec *ttime, const uint32_t hash)
Get or create a Flow using a FlowKey.
Definition: flow-hash.c:1032
Flow_::flags
uint32_t flags
Definition: flow.h:420
Packet_::recursion_level
uint8_t recursion_level
Definition: decode.h:455
DecodeThreadVars_::output_flow_thread_data
void * output_flow_thread_data
Definition: decode.h:755
FlowKey_
Definition: flow.h:303
FlowHashKey6_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: flow-hash.c:109
Packet_::udph
UDPHdr * udph
Definition: decode.h:559
FlowHashKey4_::proto
uint8_t proto
Definition: flow-hash.c:91
FLOW_EMERGENCY
#define FLOW_EMERGENCY
Definition: flow-private.h:37
STREAM_PKT_FLAG_SET
#define STREAM_PKT_FLAG_SET(p, f)
Definition: stream-tcp-private.h:324
Address_::family
char family
Definition: decode.h:116
Packet_::dst
Address dst
Definition: decode.h:435
Flow_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: flow.h:374
DecodeThreadVars_::counter_flow_memcap
uint16_t counter_flow_memcap
Definition: decode.h:730
FLOW_END_FLAG_TIMEOUT
#define FLOW_END_FLAG_TIMEOUT
Definition: flow.h:237
FlowHashKey6_::u32
const uint32_t u32[12]
Definition: flow-hash.c:112
Packet_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: decode.h:457
Flow_::sp
Port sp
Definition: flow.h:355
ICMPV4Vars_::emb_sport
uint16_t emb_sport
Definition: decode-icmpv4.h:203
Packet_::ip6h
IPV6Hdr * ip6h
Definition: decode.h:537
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:376
FlowKey_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: flow.h:309
flow.h
FlowQueuePrivateAppendFlow
void FlowQueuePrivateAppendFlow(FlowQueuePrivate *fqc, Flow *f)
Definition: flow-queue.c:65
FlowAlloc
Flow * FlowAlloc(void)
allocate a flow
Definition: flow-util.c:54
FlowAddress_::address
union FlowAddress_::@116 address
Packet_::dp
Port dp
Definition: decode.h:445
FLOW_END_FLAG_FORCED
#define FLOW_END_FLAG_FORCED
Definition: flow.h:238
Flow_::timeout_at
uint32_t timeout_at
Definition: flow.h:390
FlowKey_::dp
Port dp
Definition: flow.h:305
FlowInit
void FlowInit(Flow *f, const Packet *p)
Definition: flow-util.c:145
FlowSetupPacket
void FlowSetupPacket(Packet *p)
prepare packet for a life with flow Set PKT_WANTS_FLOW flag to indicate workers should do a flow look...
Definition: flow-hash.c:520
Packet_::src
Address src
Definition: decode.h:434
FlowHashKey4_::recur
uint8_t recur
Definition: flow-hash.c:92
output.h
Flow_::thread_id
FlowThreadId thread_id[2]
Definition: flow.h:393
SC_ATOMIC_OR
#define SC_ATOMIC_OR(name, val)
Bitwise OR a value to our atomic variable.
Definition: util-atomic.h:351