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