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