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