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