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