suricata
flow.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2025 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_H
25 #define SURICATA_FLOW_H
26 
27 /* forward declaration for macset include */
28 typedef struct SCFlowStorageId SCFlowStorageId;
29 
30 #include "decode.h"
31 #include "util-time.h"
32 #include "util-exception-policy.h"
34 #include "util-var.h"
35 #include "util-validate.h"
36 #include "app-layer-protos.h"
37 #include "flow-bindgen.h"
38 
39 /* Part of the flow structure, so we declare it here.
40  * The actual declaration is in app-layer-parser.c */
42 
43 #define FLOW_QUIET true
44 
45 #define TOSERVER 0
46 #define TOCLIENT 1
47 
48 /* per flow flags */
49 
50 /** At least one packet from the source address was seen */
51 #define FLOW_TO_SRC_SEEN BIT_U64(0)
52 /** At least one packet from the destination address was seen */
53 #define FLOW_TO_DST_SEEN BIT_U64(1)
54 
55 /** next packet in toclient direction will act on updated app-layer state */
56 #define FLOW_TC_APP_UPDATE_NEXT BIT_U64(2)
57 
58 /** Flow is marked an elephant flow */
59 #define FLOW_IS_ELEPHANT_TOSERVER BIT_U64(3)
60 #define FLOW_IS_ELEPHANT_TOCLIENT BIT_U64(4)
61 
62 /** All packets in this flow should be accepted */
63 #define FLOW_ACTION_ACCEPT BIT_U64(5)
64 
65 /** Packet payloads belonging to this flow should not be inspected */
66 #define FLOW_NOPAYLOAD_INSPECTION BIT_U64(6)
67 
68 /** All packets in this flow should be dropped */
69 #define FLOW_ACTION_DROP BIT_U64(7)
70 
71 /** Sgh for toserver direction set (even if it's NULL) */
72 #define FLOW_SGH_TOSERVER BIT_U64(8)
73 /** Sgh for toclient direction set (even if it's NULL) */
74 #define FLOW_SGH_TOCLIENT BIT_U64(9)
75 
76 /** packet to server direction has been logged in drop file (only in IPS mode) */
77 #define FLOW_TOSERVER_DROP_LOGGED BIT_U64(10)
78 /** packet to client direction has been logged in drop file (only in IPS mode) */
79 #define FLOW_TOCLIENT_DROP_LOGGED BIT_U64(11)
80 
81 /** flow has alerts */
82 #define FLOW_HAS_ALERTS BIT_U64(12)
83 
84 /** Pattern matcher alproto detection done */
85 #define FLOW_TS_PM_ALPROTO_DETECT_DONE BIT_U64(13)
86 /** Probing parser alproto detection done */
87 #define FLOW_TS_PP_ALPROTO_DETECT_DONE BIT_U64(14)
88 /** Expectation alproto detection done */
89 #define FLOW_TS_PE_ALPROTO_DETECT_DONE BIT_U64(15)
90 /** Pattern matcher alproto detection done */
91 #define FLOW_TC_PM_ALPROTO_DETECT_DONE BIT_U64(16)
92 /** Probing parser alproto detection done */
93 #define FLOW_TC_PP_ALPROTO_DETECT_DONE BIT_U64(17)
94 /** Expectation alproto detection done */
95 #define FLOW_TC_PE_ALPROTO_DETECT_DONE BIT_U64(18)
96 #define FLOW_TIMEOUT_REASSEMBLY_DONE BIT_U64(19)
97 
98 /** flow is ipv4 */
99 #define FLOW_IPV4 BIT_U64(20)
100 /** flow is ipv6 */
101 #define FLOW_IPV6 BIT_U64(21)
102 
103 #define FLOW_PROTO_DETECT_TS_DONE BIT_U64(22)
104 #define FLOW_PROTO_DETECT_TC_DONE BIT_U64(23)
105 
106 /** Indicate that alproto detection for flow should be done again */
107 #define FLOW_CHANGE_PROTO BIT_U64(24)
108 
109 #define FLOW_WRONG_THREAD BIT_U64(25)
110 /** Protocol detection told us flow is picked up in wrong direction (midstream) */
111 #define FLOW_DIR_REVERSED BIT_U64(26)
112 /** Indicate that the flow did trigger an expectation creation */
113 #define FLOW_HAS_EXPECTATION BIT_U64(27)
114 
115 /** All packets in this flow should be passed */
116 #define FLOW_ACTION_PASS BIT_U64(28)
117 
118 #define FLOW_TS_APP_UPDATED BIT_U64(29)
119 #define FLOW_TC_APP_UPDATED BIT_U64(30)
120 
121 /** next packet in toserver direction will act on updated app-layer state */
122 #define FLOW_TS_APP_UPDATE_NEXT BIT_U64(31)
123 
124 /** Flow action issued by firewall */
125 #define FLOW_ACTION_BY_FIREWALL BIT_U64(32)
126 /** Flow action issued by exception policy */
127 #define FLOW_ACTION_BY_EXCEPTION_POLICY BIT_U64(33)
128 
129 /** The app layer was disabled mid-flow: run the tx loggers once at the
130  * next output stage, as no further app-layer update will reach them.
131  * Consumed by the tx logger. */
132 #define FLOW_APP_LAYER_FLUSH_PENDING BIT_U64(34)
133 
134 /* File flags */
135 
136 #define FLOWFILE_INIT 0
137 
138 /** no magic on files in this flow */
139 #define FLOWFILE_NO_MAGIC_TS BIT_U16(0)
140 #define FLOWFILE_NO_MAGIC_TC BIT_U16(1)
141 
142 /** even if the flow has files, don't store 'm */
143 // defined in rust
144 // #define FLOWFILE_NO_STORE_TS BIT_U16(2)
145 // #define FLOWFILE_NO_STORE_TC BIT_U16(3)
146 /** no md5 on files in this flow */
147 #define FLOWFILE_NO_MD5_TS BIT_U16(4)
148 #define FLOWFILE_NO_MD5_TC BIT_U16(5)
149 
150 /** no sha1 on files in this flow */
151 #define FLOWFILE_NO_SHA1_TS BIT_U16(6)
152 #define FLOWFILE_NO_SHA1_TC BIT_U16(7)
153 
154 /** no sha256 on files in this flow */
155 #define FLOWFILE_NO_SHA256_TS BIT_U16(8)
156 #define FLOWFILE_NO_SHA256_TC BIT_U16(9)
157 
158 // vacancy(2)
159 
160 /** store files in the flow */
161 // defined in rust
162 // #define FLOWFILE_STORE_TS BIT_U16(12)
163 // #define FLOWFILE_STORE_TC BIT_U16(13)
164 
165 #define FLOWFILE_NONE_TS \
166  (FLOWFILE_NO_MAGIC_TS | FLOWFILE_NO_STORE_TS | FLOWFILE_NO_MD5_TS | FLOWFILE_NO_SHA1_TS | \
167  FLOWFILE_NO_SHA256_TS)
168 #define FLOWFILE_NONE_TC \
169  (FLOWFILE_NO_MAGIC_TC | FLOWFILE_NO_STORE_TC | FLOWFILE_NO_MD5_TC | FLOWFILE_NO_SHA1_TC | \
170  FLOWFILE_NO_SHA256_TC)
171 #define FLOWFILE_NONE (FLOWFILE_NONE_TS|FLOWFILE_NONE_TC)
172 
173 #define FLOW_IS_IPV4(f) \
174  (((f)->flags & FLOW_IPV4) == FLOW_IPV4)
175 #define FLOW_IS_IPV6(f) \
176  (((f)->flags & FLOW_IPV6) == FLOW_IPV6)
177 
178 #define FLOW_GET_SP(f) \
179  ((f)->flags & FLOW_DIR_REVERSED) ? (f)->dp : (f)->sp;
180 #define FLOW_GET_DP(f) \
181  ((f)->flags & FLOW_DIR_REVERSED) ? (f)->sp : (f)->dp;
182 
183 #define FLOW_COPY_IPV4_ADDR_TO_PACKET(fa, pa) do { \
184  (pa)->family = AF_INET; \
185  (pa)->addr_data32[0] = (fa)->addr_data32[0]; \
186  } while (0)
187 
188 #define FLOW_COPY_IPV6_ADDR_TO_PACKET(fa, pa) do { \
189  (pa)->family = AF_INET6; \
190  (pa)->addr_data32[0] = (fa)->addr_data32[0]; \
191  (pa)->addr_data32[1] = (fa)->addr_data32[1]; \
192  (pa)->addr_data32[2] = (fa)->addr_data32[2]; \
193  (pa)->addr_data32[3] = (fa)->addr_data32[3]; \
194  } while (0)
195 
196 /* Set the IPv4 addressesinto the Addrs of the Packet.
197  * Make sure p->ip4h is initialized and validated.
198  *
199  * We set the rest of the struct to 0 so we can
200  * prevent using memset. */
201 #define FLOW_SET_IPV4_SRC_ADDR_FROM_PACKET(ip4h, a) \
202  do { \
203  (a)->addr_data32[0] = (uint32_t)(ip4h)->s_ip_src.s_addr; \
204  (a)->addr_data32[1] = 0; \
205  (a)->addr_data32[2] = 0; \
206  (a)->addr_data32[3] = 0; \
207  } while (0)
208 
209 #define FLOW_SET_IPV4_DST_ADDR_FROM_PACKET(ip4h, a) \
210  do { \
211  (a)->addr_data32[0] = (uint32_t)(ip4h)->s_ip_dst.s_addr; \
212  (a)->addr_data32[1] = 0; \
213  (a)->addr_data32[2] = 0; \
214  (a)->addr_data32[3] = 0; \
215  } while (0)
216 
217 /* Set the IPv6 addressesinto the Addrs of the Packet.
218  * Make sure p->ip6h is initialized and validated. */
219 #define FLOW_SET_IPV6_SRC_ADDR_FROM_PACKET(ip6h, a) \
220  do { \
221  (a)->addr_data32[0] = (ip6h)->s_ip6_src[0]; \
222  (a)->addr_data32[1] = (ip6h)->s_ip6_src[1]; \
223  (a)->addr_data32[2] = (ip6h)->s_ip6_src[2]; \
224  (a)->addr_data32[3] = (ip6h)->s_ip6_src[3]; \
225  } while (0)
226 
227 #define FLOW_SET_IPV6_DST_ADDR_FROM_PACKET(ip6h, a) \
228  do { \
229  (a)->addr_data32[0] = (ip6h)->s_ip6_dst[0]; \
230  (a)->addr_data32[1] = (ip6h)->s_ip6_dst[1]; \
231  (a)->addr_data32[2] = (ip6h)->s_ip6_dst[2]; \
232  (a)->addr_data32[3] = (ip6h)->s_ip6_dst[3]; \
233  } while (0)
234 
235 /* pkt flow flags */
236 #define FLOW_PKT_TOSERVER 0x01
237 #define FLOW_PKT_TOCLIENT 0x02
238 #define FLOW_PKT_ESTABLISHED 0x04
239 #define FLOW_PKT_TOSERVER_FIRST 0x08
240 #define FLOW_PKT_TOCLIENT_FIRST 0x10
241 /** last pseudo packet in the flow. Can be used to trigger final clean,
242  * logging, etc. */
243 #define FLOW_PKT_LAST_PSEUDO 0x20
244 
245 #define FLOW_END_FLAG_EMERGENCY 0x01
246 #define FLOW_END_FLAG_TIMEOUT 0x02
247 #define FLOW_END_FLAG_FORCED 0x04
248 #define FLOW_END_FLAG_SHUTDOWN 0x08
249 #define FLOW_END_FLAG_TCPREUSE 0x10
250 
251 /** Mutex or RWLocks for the flow. */
252 //#define FLOWLOCK_RWLOCK
253 #define FLOWLOCK_MUTEX
254 
255 #ifdef FLOWLOCK_RWLOCK
256  #ifdef FLOWLOCK_MUTEX
257  #error Cannot enable both FLOWLOCK_RWLOCK and FLOWLOCK_MUTEX
258  #endif
259 #endif
260 
261 #ifdef FLOWLOCK_RWLOCK
262  #define FLOWLOCK_INIT(fb) SCRWLockInit(&(fb)->r, NULL)
263  #define FLOWLOCK_DESTROY(fb) SCRWLockDestroy(&(fb)->r)
264  #define FLOWLOCK_RDLOCK(fb) SCRWLockRDLock(&(fb)->r)
265  #define FLOWLOCK_WRLOCK(fb) SCRWLockWRLock(&(fb)->r)
266  #define FLOWLOCK_TRYRDLOCK(fb) SCRWLockTryRDLock(&(fb)->r)
267  #define FLOWLOCK_TRYWRLOCK(fb) SCRWLockTryWRLock(&(fb)->r)
268  #define FLOWLOCK_UNLOCK(fb) SCRWLockUnlock(&(fb)->r)
269 #elif defined FLOWLOCK_MUTEX
270  #define FLOWLOCK_INIT(fb) SCMutexInit(&(fb)->m, NULL)
271  #define FLOWLOCK_DESTROY(fb) SCMutexDestroy(&(fb)->m)
272  #define FLOWLOCK_RDLOCK(fb) SCMutexLock(&(fb)->m)
273  #define FLOWLOCK_WRLOCK(fb) SCMutexLock(&(fb)->m)
274  #define FLOWLOCK_TRYRDLOCK(fb) SCMutexTrylock(&(fb)->m)
275  #define FLOWLOCK_TRYWRLOCK(fb) SCMutexTrylock(&(fb)->m)
276  #define FLOWLOCK_UNLOCK(fb) SCMutexUnlock(&(fb)->m)
277 #else
278  #error Enable FLOWLOCK_RWLOCK or FLOWLOCK_MUTEX
279 #endif
280 
281 #define FLOW_IS_PM_DONE(f, dir) (((dir) & STREAM_TOSERVER) ? ((f)->flags & FLOW_TS_PM_ALPROTO_DETECT_DONE) : ((f)->flags & FLOW_TC_PM_ALPROTO_DETECT_DONE))
282 #define FLOW_IS_PP_DONE(f, dir) (((dir) & STREAM_TOSERVER) ? ((f)->flags & FLOW_TS_PP_ALPROTO_DETECT_DONE) : ((f)->flags & FLOW_TC_PP_ALPROTO_DETECT_DONE))
283 #define FLOW_IS_PE_DONE(f, dir) (((dir) & STREAM_TOSERVER) ? ((f)->flags & FLOW_TS_PE_ALPROTO_DETECT_DONE) : ((f)->flags & FLOW_TC_PE_ALPROTO_DETECT_DONE))
284 
285 #define FLOW_SET_PM_DONE(f, dir) (((dir) & STREAM_TOSERVER) ? ((f)->flags |= FLOW_TS_PM_ALPROTO_DETECT_DONE) : ((f)->flags |= FLOW_TC_PM_ALPROTO_DETECT_DONE))
286 #define FLOW_SET_PP_DONE(f, dir) (((dir) & STREAM_TOSERVER) ? ((f)->flags |= FLOW_TS_PP_ALPROTO_DETECT_DONE) : ((f)->flags |= FLOW_TC_PP_ALPROTO_DETECT_DONE))
287 #define FLOW_SET_PE_DONE(f, dir) (((dir) & STREAM_TOSERVER) ? ((f)->flags |= FLOW_TS_PE_ALPROTO_DETECT_DONE) : ((f)->flags |= FLOW_TC_PE_ALPROTO_DETECT_DONE))
288 
289 #define FLOW_RESET_PM_DONE(f, dir) (((dir) & STREAM_TOSERVER) ? ((f)->flags &= ~FLOW_TS_PM_ALPROTO_DETECT_DONE) : ((f)->flags &= ~FLOW_TC_PM_ALPROTO_DETECT_DONE))
290 #define FLOW_RESET_PP_DONE(f, dir) (((dir) & STREAM_TOSERVER) ? ((f)->flags &= ~FLOW_TS_PP_ALPROTO_DETECT_DONE) : ((f)->flags &= ~FLOW_TC_PP_ALPROTO_DETECT_DONE))
291 #define FLOW_RESET_PE_DONE(f, dir) (((dir) & STREAM_TOSERVER) ? ((f)->flags &= ~FLOW_TS_PE_ALPROTO_DETECT_DONE) : ((f)->flags &= ~FLOW_TC_PE_ALPROTO_DETECT_DONE))
292 
293 /* global flow config */
294 typedef struct FlowCnf_
295 {
296  uint32_t hash_rand;
297  uint32_t hash_size;
298  uint32_t prealloc;
299 
300  uint32_t timeout_new;
301  uint32_t timeout_est;
302 
304 
306 
307  SC_ATOMIC_DECLARE(uint64_t, memcap);
309 
310 /* Hash key for the flow hash */
311 typedef struct FlowKey_
312 {
315  uint8_t proto;
317  uint16_t livedev_id;
320 
321 typedef struct FlowAddress_ {
322  union {
323  uint32_t address_un_data32[4]; /* type-specific field */
324  uint16_t address_un_data16[8]; /* type-specific field */
325  uint8_t address_un_data8[16]; /* type-specific field */
328 
329 #define addr_data32 address.address_un_data32
330 #define addr_data16 address.address_un_data16
331 #define addr_data8 address.address_un_data8
332 
333 typedef unsigned short FlowStateType;
334 
335 /** Local Thread ID */
336 typedef uint16_t FlowThreadId;
337 
338 #include "util-storage.h"
339 
340 /**
341  * \brief Flow data structure.
342  *
343  * The flow is a global data structure that is created for new packets of a
344  * flow and then looked up for the following packets of a flow.
345  *
346  * Locking
347  *
348  * The flow is updated/used by multiple packets at the same time. This is why
349  * there is a flow-mutex. It's a mutex and not a spinlock because some
350  * operations on the flow can be quite expensive, thus spinning would be
351  * too expensive.
352  *
353  * The flow "header" (addresses, ports, proto, recursion level) are static
354  * after the initialization and remain read-only throughout the entire live
355  * of a flow. This is why we can access those without protection of the lock.
356  */
357 
358 typedef struct Flow_
359 {
360  /* flow "header", used for hashing and flow lookup. Static after init,
361  * so safe to look at without lock */
363  union {
364  Port sp; /**< tcp/udp source port */
365  struct {
366  uint8_t type; /**< icmp type */
367  uint8_t code; /**< icmp code */
369 
370  struct {
371  uint32_t spi; /**< esp spi */
372  } esp;
373  };
374  union {
375  Port dp; /**< tcp/udp destination port */
376  struct {
377  uint8_t type; /**< icmp type */
378  uint8_t code; /**< icmp code */
380  };
381  uint8_t proto; /**< ip proto of the flow */
384 
385  uint8_t vlan_idx;
386 
387  /* track toserver/toclient flow timeout needs */
388  union {
389  struct {
390  uint8_t ffr_ts:4;
391  uint8_t ffr_tc:4;
392  };
393  uint8_t ffr;
394  };
395 
396  /** Thread ID for the stream/detect portion of this flow */
398 
399  /** flow hash - the flow hash before hash table size mod. Used for calculating
400  * the flow_id. */
401  uint32_t flow_hash;
402 
403  /** Incoming interface */
404  uint16_t livedev_id;
405 
406  struct Flow_ *next; /* (hash) list next */
407 
408  uint64_t flags; /**< generic flags */
409 
410  uint16_t file_flags; /**< file tracking/extraction flags */
411 
412  /** destination port to be used in protocol detection. This is meant
413  * for use with STARTTLS and HTTP CONNECT detection */
414  uint16_t protodetect_dp; /**< 0 if not used */
415 
416  /** timeout in seconds by policy, add to Flow::lastts to get actual time this times out.
417  * Ignored in emergency mode. */
418  uint32_t timeout_policy;
419 
420  /** time stamp of last update (last packet). Set/updated under the
421  * flow and flow hash row locks, safe to read under either the
422  * flow lock or flow hash row lock. */
424 
426 
427  /** flow tenant id, used to setup flow timeout and stream pseudo
428  * packets with the correct tenant id set */
429  uint32_t tenant_id;
430 
433 
434  /* Parent flow id for protocol like ftp */
435  int64_t parent_id;
436 
437  /** protocol specific data pointer, e.g. for TcpSession */
438  void *protoctx;
439 
440 #ifdef FLOWLOCK_RWLOCK
441  SCRWLock r;
442 #elif defined FLOWLOCK_MUTEX
444 #else
445  #error Enable FLOWLOCK_RWLOCK or FLOWLOCK_MUTEX
446 #endif
447 
448  /** mapping to Flow's protocol specific protocols for timeouts
449  and state and free functions. */
450  uint8_t protomap;
451 
452  uint8_t flow_end_flags;
453  /* coccinelle: Flow:flow_end_flags:FLOW_END_FLAG_ */
454 
455  AppProto alproto; /**< \brief application level protocol */
458 
459  /** original application level protocol. Used to indicate the previous
460  protocol when changing to another protocol , e.g. with STARTTLS. */
462  /** expected app protocol: used in protocol change/upgrade like in
463  * STARTTLS. */
465 
466  /** detection engine ctx version used to inspect this flow. Set at initial
467  * inspection. If it doesn't match the currently in use de_ctx, the
468  * stored sgh ptrs are reset. */
469  uint32_t de_ctx_version;
470 
471  /** ttl tracking */
476 
477  /** which exception policies were applied, if any */
479 
480  /** application level storage ptrs.
481  *
482  */
483  AppLayerParserState *alparser; /**< parser internal state */
484  void *alstate; /**< application layer state */
485 
486  /** toclient sgh for this flow. Only use when FLOW_SGH_TOCLIENT flow flag
487  * has been set. */
489  /** toserver sgh for this flow. Only use when FLOW_SGH_TOSERVER flow flag
490  * has been set. */
492 
493  /* pointer to the var list */
495 
496  struct FlowBucket_ *fb;
497 
499 
500  uint32_t todstpktcnt;
501  uint32_t tosrcpktcnt;
502  uint64_t todstbytecnt;
503  uint64_t tosrcbytecnt;
504 
507 
508 enum FlowState {
513 #ifdef CAPTURE_OFFLOAD
514  FLOW_STATE_CAPTURE_BYPASSED,
515 #endif
516 };
517 #ifdef CAPTURE_OFFLOAD
518 #define FLOW_STATE_SIZE 5
519 #else
520 #define FLOW_STATE_SIZE 4
521 #endif
522 
523 typedef struct FlowProtoTimeout_ {
524  uint32_t new_timeout;
525  uint32_t est_timeout;
526  uint32_t closed_timeout;
529 
530 typedef struct FlowProtoFreeFunc_ {
531  void (*Freefunc)(void *);
533 
534 typedef struct FlowBypassInfo_ {
535  bool (* BypassUpdate)(Flow *f, void *data, time_t tsec);
536  void (* BypassFree)(void *data);
537  void *bypass_data;
538  uint64_t tosrcpktcnt;
539  uint64_t tosrcbytecnt;
540  uint64_t todstpktcnt;
541  uint64_t todstbytecnt;
543 
544 #include "flow-queue.h"
545 
546 typedef struct FlowLookupStruct_ // TODO name
547 {
548  /** thread store of spare queues */
554 
555 /** \brief prepare packet for a life with flow
556  * Set PKT_WANTS_FLOW flag to indicate workers should do a flow lookup
557  * and calc the hash value to be used in the lookup and autofp flow
558  * balancing. */
559 void FlowSetupPacket(Packet *p);
561 void FlowInitConfig(bool);
562 void FlowReset(void);
563 void FlowShutdown(void);
564 void FlowSetHasAlertsFlag(Flow *);
565 int FlowHasAlerts(const Flow *);
568 int FlowChangeProto(Flow *);
569 void FlowSwap(Flow *);
570 
571 void FlowRegisterTests(void);
572 int FlowSetProtoFreeFunc(uint8_t, void (*Free)(void *));
573 
574 static inline void FlowSetNoPayloadInspectionFlag(Flow *);
575 
576 int FlowGetPacketDirection(const Flow *, const Packet *);
577 
578 void FlowCleanupAppLayer(Flow *);
579 
580 void FlowUpdateState(Flow *f, enum FlowState s);
581 
582 int FlowSetMemcap(uint64_t size);
583 uint64_t FlowGetMemcap(void);
584 uint64_t FlowGetMemuse(void);
586 
588 void RegisterFlowBypassInfo(void);
589 
590 /** ----- Inline functions ----- */
591 static inline void *FlowGetAppState(const Flow *f)
592 {
593  return f->alstate;
594 }
595 
596 /** \brief Set the No payload inspection Flag without locking the flow.
597  *
598  * \param f Flow to set the flag in
599  */
600 /** \brief mark the flow dropped and schedule the pending tx loggers.
601  *
602  * A flow scope drop ends the app layer without a further update: the txs
603  * still pending (e.g. the ssh success record) must be logged once. The
604  * output stage of the dropping packet consumes the flag. */
605 static inline void FlowSetDropAction(Flow *f, const uint64_t by)
606 {
607  f->flags |= FLOW_ACTION_DROP | by;
608  if (f->alparser != NULL)
610 }
611 
612 static inline void FlowSetNoPayloadInspectionFlag(Flow *f)
613 {
614  SCEnter();
615 
616  SCLogDebug("flow %p", f);
618 
619  SCReturn;
620 }
621 
622 /** \brief Reference the flow
623  * \note This should only be called once for a destination
624  * pointer */
625 static inline void FlowReference(Flow **d, Flow *f)
626 {
627  if (likely(f != NULL)) {
628  DEBUG_VALIDATE_BUG_ON(*d == f);
629  if (*d == f)
630  return;
631  *d = f;
632  }
633 }
634 
635 static inline void FlowDeReference(Flow **d)
636 {
637  if (likely(*d != NULL)) {
638  *d = NULL;
639  }
640 }
641 
642 /** \brief create a flow id that is as unique as possible
643  * \retval flow_id unsigned 64bit id
644  */
645 static inline uint64_t FlowGetId(const Flow *f)
646 {
647  uint64_t id = (uint64_t)(SCTIME_SECS(f->startts) & 0xFFFF) << 48 |
648  (uint64_t)(SCTIME_USECS(f->startts) & 0xFFFF) << 32 | (uint64_t)f->flow_hash;
649  /* reduce to 51 bits as JavaScript and even JSON often seem to
650  * max out there. */
651  id &= 0x7ffffffffffffLL;
652  return id;
653 }
654 
655 static inline bool FlowIsBypassed(const Flow *f)
656 {
657  if (
658 #ifdef CAPTURE_OFFLOAD
659  f->flow_state == FLOW_STATE_CAPTURE_BYPASSED ||
660 #endif
662  return true;
663  }
664  return false;
665 }
666 
667 int FlowClearMemory(Flow *,uint8_t );
668 
669 void *FlowGetAppState(const Flow *f);
670 uint8_t FlowGetDisruptionFlags(const Flow *f, uint8_t flags);
671 
673 
674 #endif /* SURICATA_FLOW_H */
Flow_::ffr_tc
uint8_t ffr_tc
Definition: flow.h:391
FlowLookupStruct_::work_queue
FlowQueuePrivate work_queue
Definition: flow.h:551
FlowSetMemcap
int FlowSetMemcap(uint64_t size)
Update memcap value.
Definition: flow.c:105
FlowGetDisruptionFlags
uint8_t FlowGetDisruptionFlags(const Flow *f, uint8_t flags)
get 'disruption' flags: GAP/DEPTH/PASS
Definition: flow.c:1168
FlowBucket_
Definition: flow-hash.h:43
Flow_::ffr_ts
uint8_t ffr_ts
Definition: flow.h:390
Flow_::recursion_level
uint8_t recursion_level
Definition: flow.h:382
Flow_::flags
uint64_t flags
Definition: flow.h:408
FlowAddress_
Definition: flow.h:321
FLOW_STATE_ESTABLISHED
@ FLOW_STATE_ESTABLISHED
Definition: flow.h:510
FlowLookupStruct_::dtv
DecodeThreadVars * dtv
Definition: flow.h:550
Flow_::startts
SCTime_t startts
Definition: flow.h:498
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1730
FlowKey_::src
Address src
Definition: flow.h:313
FlowBypassInfo_
Definition: flow.h:534
FlowCnf_::emergency_recovery
uint32_t emergency_recovery
Definition: flow.h:303
FlowCnf_::hash_size
uint32_t hash_size
Definition: flow.h:297
FlowHandlePacket
void FlowHandlePacket(ThreadVars *, FlowLookupStruct *, Packet *)
Entry point for packet flow handling.
Definition: flow.c:560
FlowAddress_::address_un_data32
uint32_t address_un_data32[4]
Definition: flow.h:323
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
FlowLookupStruct
struct FlowLookupStruct_ FlowLookupStruct
Flow_::proto
uint8_t proto
Definition: flow.h:381
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:87
FlowInitConfig
void FlowInitConfig(bool)
initialize the configuration
Definition: flow.c:574
FlowGetPacketDirection
int FlowGetPacketDirection(const Flow *, const Packet *)
determine the direction of the packet compared to the flow
Definition: flow.c:283
FlowClearMemory
int FlowClearMemory(Flow *, uint8_t)
Function clear the flow memory before queueing it to spare flow queue.
Definition: flow.c:1124
Flow_
Flow data structure.
Definition: flow.h:359
Flow_::esp
struct Flow_::@126::@133 esp
Flow_::protomap
uint8_t protomap
Definition: flow.h:450
FlowProtoTimeout_
Definition: flow.h:523
SCFlowStorageId
Definition: flow-storage.h:33
FLOW_NOPAYLOAD_INSPECTION
#define FLOW_NOPAYLOAD_INSPECTION
Definition: flow.h:66
FlowLookupStruct_
Definition: flow.h:547
Flow_::alproto_orig
AppProto alproto_orig
Definition: flow.h:461
Flow
struct Flow_ Flow
Flow data structure.
util-exception-policy-types.h
FlowProtoTimeout_::bypassed_timeout
uint32_t bypassed_timeout
Definition: flow.h:527
FlowBypassInfo_::tosrcbytecnt
uint64_t tosrcbytecnt
Definition: flow.h:539
FLOW_APP_LAYER_FLUSH_PENDING
#define FLOW_APP_LAYER_FLUSH_PENDING
Definition: flow.h:132
Address_
Definition: decode.h:113
FLOW_ACTION_DROP
#define FLOW_ACTION_DROP
Definition: flow.h:69
p
Packet * p
Definition: fuzz_dataset.c:30
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:531
Flow_::max_ttl_toserver
uint8_t max_ttl_toserver
Definition: flow.h:473
Flow_::livedev_id
uint16_t livedev_id
Definition: flow.h:404
Flow_::dp
Port dp
Definition: flow.h:375
Flow_::protoctx
void * protoctx
Definition: flow.h:438
FlowUnsetChangeProtoFlag
void FlowUnsetChangeProtoFlag(Flow *)
Unset flag to indicate to change proto for the flow.
Definition: flow.c:184
util-var.h
Storage
Definition: util-storage.h:40
FlowCnf_::prealloc
uint32_t prealloc
Definition: flow.h:298
Flow_::flow_state
FlowStateType flow_state
Definition: flow.h:425
Flow_::spi
uint32_t spi
Definition: flow.h:371
Flow_::sgh_toserver
const struct SigGroupHead_ * sgh_toserver
Definition: flow.h:491
Flow_::icmp_d
struct Flow_::@128::@134 icmp_d
FlowLookupStruct_::emerg_spare_sync_stamp
uint32_t emerg_spare_sync_stamp
Definition: flow.h:552
Flow_::tosrcbytecnt
uint64_t tosrcbytecnt
Definition: flow.h:503
FlowConfig
struct FlowCnf_ FlowConfig
Flow_::alparser
AppLayerParserState * alparser
Definition: flow.h:483
Flow_::dst
FlowAddress dst
Definition: flow.h:362
FlowProtoFreeFunc
struct FlowProtoFreeFunc_ FlowProtoFreeFunc
Flow_::fb
struct FlowBucket_ * fb
Definition: flow.h:496
FLOW_STATE_LOCAL_BYPASSED
@ FLOW_STATE_LOCAL_BYPASSED
Definition: flow.h:512
Flow_::min_ttl_toserver
uint8_t min_ttl_toserver
Definition: flow.h:472
Flow_::protodetect_dp
uint16_t protodetect_dp
Definition: flow.h:414
decode.h
FlowBypassInfo_::todstbytecnt
uint64_t todstbytecnt
Definition: flow.h:541
AppLayerParserState_
Definition: app-layer-parser.c:160
RegisterFlowBypassInfo
void RegisterFlowBypassInfo(void)
Definition: flow-util.c:241
FlowBypassInfo_::BypassUpdate
bool(* BypassUpdate)(Flow *f, void *data, time_t tsec)
Definition: flow.h:535
FlowBypassInfo_::BypassFree
void(* BypassFree)(void *data)
Definition: flow.h:536
Flow_::todstpktcnt
uint32_t todstpktcnt
Definition: flow.h:500
util-exception-policy.h
Flow_::lastts
SCTime_t lastts
Definition: flow.h:423
FlowSwap
void FlowSwap(Flow *)
swap the flow's direction
Definition: flow.c:243
FlowKey_::recursion_level
uint8_t recursion_level
Definition: flow.h:316
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
FlowShutdown
void FlowShutdown(void)
shutdown the flow engine
Definition: flow.c:718
FlowGetMemuse
uint64_t FlowGetMemuse(void)
Definition: flow.c:126
Flow_::flow_end_flags
uint8_t flow_end_flags
Definition: flow.h:452
FlowCnf_
Definition: flow.h:295
FlowState
FlowState
Definition: flow.h:508
FlowBypassInfo_::todstpktcnt
uint64_t todstpktcnt
Definition: flow.h:540
FlowProtoTimeout_::new_timeout
uint32_t new_timeout
Definition: flow.h:524
FlowProtoTimeout_::closed_timeout
uint32_t closed_timeout
Definition: flow.h:526
util-time.h
FlowBypassInfo_::bypass_data
void * bypass_data
Definition: flow.h:537
Flow_::m
SCMutex m
Definition: flow.h:443
FlowBypassInfo
struct FlowBypassInfo_ FlowBypassInfo
FlowCnf_::timeout_new
uint32_t timeout_new
Definition: flow.h:300
FlowKey_::livedev_id
uint16_t livedev_id
Definition: flow.h:317
Flow_::todstbytecnt
uint64_t todstbytecnt
Definition: flow.h:502
Flow_::sgh_toclient
const struct SigGroupHead_ * sgh_toclient
Definition: flow.h:488
FlowChangeProto
int FlowChangeProto(Flow *)
Check if change proto flag is set for flow.
Definition: flow.c:194
FlowThreadId
uint16_t FlowThreadId
Definition: flow.h:336
FlowKey_::sp
Port sp
Definition: flow.h:314
SCReturn
#define SCReturn
Definition: util-debug.h:286
Packet_
Definition: decode.h:516
Flow_::type
uint8_t type
Definition: flow.h:366
FlowRegisterTests
void FlowRegisterTests(void)
Function to register the Flow Unitests.
Definition: flow.c:1544
Port
uint16_t Port
Definition: decode.h:219
SCTime_t
Definition: util-time.h:40
FlowProtoFreeFunc_::Freefunc
void(* Freefunc)(void *)
Definition: flow.h:531
FlowAddress_::address_un_data16
uint16_t address_un_data16[8]
Definition: flow.h:324
FlowCnf_::hash_rand
uint32_t hash_rand
Definition: flow.h:296
Flow_::vlan_idx
uint8_t vlan_idx
Definition: flow.h:385
FlowHandlePacketUpdate
void FlowHandlePacketUpdate(Flow *f, Packet *p, ThreadVars *tv, DecodeThreadVars *dtv)
Update Packet and Flow.
Definition: flow.c:421
Flow_::min_ttl_toclient
uint8_t min_ttl_toclient
Definition: flow.h:474
flow-queue.h
FlowCleanupAppLayer
void FlowCleanupAppLayer(Flow *)
Definition: flow.c:137
FlowBypassInfo_::tosrcpktcnt
uint64_t tosrcpktcnt
Definition: flow.h:538
Flow_::probing_parser_toclient_alproto_masks
uint32_t probing_parser_toclient_alproto_masks
Definition: flow.h:432
FlowCnf_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, memcap)
Flow_::flow_hash
uint32_t flow_hash
Definition: flow.h:401
Flow_::alproto_expect
AppProto alproto_expect
Definition: flow.h:464
Flow_::src
FlowAddress src
Definition: flow.h:362
Flow_::flowvar
GenericVar * flowvar
Definition: flow.h:494
FlowReset
void FlowReset(void)
Definition: flow.c:700
Flow_::next
struct Flow_ * next
Definition: flow.h:406
Flow_::probing_parser_toserver_alproto_masks
uint32_t probing_parser_toserver_alproto_masks
Definition: flow.h:431
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:35
FlowLookupStruct_::spare_queue
FlowQueuePrivate spare_queue
Definition: flow.h:549
FlowProtoTimeout
struct FlowProtoTimeout_ FlowProtoTimeout
Flow_::ffr
uint8_t ffr
Definition: flow.h:393
Flow_::icmp_s
struct Flow_::@126::@132 icmp_s
FlowStateType
unsigned short FlowStateType
Definition: flow.h:333
flags
uint8_t flags
Definition: decode-gre.h:0
Flow_::applied_exception_policy
uint8_t applied_exception_policy
Definition: flow.h:478
FLOW_STATE_NEW
@ FLOW_STATE_NEW
Definition: flow.h:509
Flow_::code
uint8_t code
Definition: flow.h:367
GenericVar_
Definition: util-var.h:53
FlowAddress
struct FlowAddress_ FlowAddress
FlowKey_::dst
Address dst
Definition: flow.h:313
SCTIME_SECS
#define SCTIME_SECS(t)
Definition: util-time.h:57
FlowGetMemcapExceptionPolicy
enum ExceptionPolicy FlowGetMemcapExceptionPolicy(void)
Definition: flow.c:132
VLAN_MAX_LAYERS
#define VLAN_MAX_LAYERS
Definition: decode-vlan.h:51
Flow_::max_ttl_toclient
uint8_t max_ttl_toclient
Definition: flow.h:475
Flow_::parent_id
int64_t parent_id
Definition: flow.h:435
FlowCnf_::timeout_est
uint32_t timeout_est
Definition: flow.h:301
Flow_::timeout_policy
uint32_t timeout_policy
Definition: flow.h:418
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
util-validate.h
FlowQueuePrivate_
Definition: flow-queue.h:40
FlowCnf_::memcap_policy
enum ExceptionPolicy memcap_policy
Definition: flow.h:305
FlowAddress_::address_un_data8
uint8_t address_un_data8[16]
Definition: flow.h:325
FlowKey_::proto
uint8_t proto
Definition: flow.h:315
FLOW_STATE_CLOSED
@ FLOW_STATE_CLOSED
Definition: flow.h:511
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:995
Flow_::alproto_ts
AppProto alproto_ts
Definition: flow.h:456
Flow_::alstate
void * alstate
Definition: flow.h:484
flow-bindgen.h
FlowAddress_::address
union FlowAddress_::@125 address
SCRWLock
#define SCRWLock
Definition: threads-debug.h:364
FlowGetMemcap
uint64_t FlowGetMemcap(void)
Return memcap value.
Definition: flow.c:120
FlowKey_
Definition: flow.h:312
app-layer-protos.h
Flow_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: flow.h:383
likely
#define likely(expr)
Definition: util-optimize.h:32
FlowProtoTimeout_::est_timeout
uint32_t est_timeout
Definition: flow.h:525
Flow_::sp
Port sp
Definition: flow.h:364
FlowKey_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: flow.h:318
GetFlowBypassInfoID
SCFlowStorageId GetFlowBypassInfoID(void)
Definition: flow-util.c:223
Flow_::alproto_tc
AppProto alproto_tc
Definition: flow.h:457
FlowSetChangeProtoFlag
void FlowSetChangeProtoFlag(Flow *)
Set flag to indicate to change proto for the flow.
Definition: flow.c:175
Flow_::file_flags
uint16_t file_flags
Definition: flow.h:410
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:455
FlowUpdateState
void FlowUpdateState(Flow *f, enum FlowState s)
Definition: flow.c:1194
ExceptionPolicy
ExceptionPolicy
Definition: util-exception-policy-types.h:26
FlowKey_::dp
Port dp
Definition: flow.h:314
Flow_::storage
Storage storage[]
Definition: flow.h:505
FlowKey
struct FlowKey_ FlowKey
SCMutex
#define SCMutex
Definition: threads-debug.h:114
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
FlowSetProtoFreeFunc
int FlowSetProtoFreeFunc(uint8_t, void(*Free)(void *))
Function to set the function to get protocol specific flow state.
Definition: flow.c:1152
Flow_::tenant_id
uint32_t tenant_id
Definition: flow.h:429
Flow_::tosrcpktcnt
uint32_t tosrcpktcnt
Definition: flow.h:501
FlowSetHasAlertsFlag
void FlowSetHasAlertsFlag(Flow *)
Set flag to indicate that flow has alerts.
Definition: flow.c:151
util-storage.h
Flow_::thread_id
FlowThreadId thread_id[2]
Definition: flow.h:397
FlowHasAlerts
int FlowHasAlerts(const Flow *)
Check if flow has alerts.
Definition: flow.c:162
SCTIME_USECS
#define SCTIME_USECS(t)
Definition: util-time.h:56
FlowProtoFreeFunc_
Definition: flow.h:530
f
Flow f
Definition: fuzz_dataset.c:32
Flow_::de_ctx_version
uint32_t de_ctx_version
Definition: flow.h:469