suricata
flow.c File Reference
#include "suricata-common.h"
#include "suricata.h"
#include "decode.h"
#include "conf.h"
#include "threadvars.h"
#include "tm-threads.h"
#include "runmodes.h"
#include "util-random.h"
#include "util-time.h"
#include "flow.h"
#include "flow-queue.h"
#include "flow-hash.h"
#include "flow-util.h"
#include "flow-var.h"
#include "flow-private.h"
#include "flow-timeout.h"
#include "flow-manager.h"
#include "flow-storage.h"
#include "flow-bypass.h"
#include "stream-tcp-private.h"
#include "stream-tcp-reassemble.h"
#include "stream-tcp.h"
#include "util-unittest.h"
#include "util-unittest-helper.h"
#include "util-byte.h"
#include "util-misc.h"
#include "util-debug.h"
#include "util-privs.h"
#include "detect.h"
#include "detect-engine-state.h"
#include "stream.h"
#include "app-layer-parser.h"
#include "threads.h"
Include dependency graph for flow.c:

Go to the source code of this file.

Macros

#define FLOW_DEFAULT_EMERGENCY_RECOVERY   30
 
#define FLOW_DEFAULT_HASHSIZE   65536
 
#define FLOW_DEFAULT_MEMCAP   (32 * 1024 * 1024) /* 32 MB */
 
#define FLOW_DEFAULT_PREALLOC   10000
 
#define SET_DEFAULTS(p, n, e, c, b, ne, ee, ce, be)
 

Functions

 SC_ATOMIC_DECLARE (unsigned int, flow_prune_idx)
 
 SC_ATOMIC_DECLARE (unsigned int, flow_flags)
 
void FlowRegisterTests (void)
 Function to register the Flow Unitests. More...
 
void FlowInitFlowProto (void)
 Function to set the default timeout, free function and flow state function for all supported flow_proto. More...
 
int FlowSetProtoFreeFunc (uint8_t proto, void(*Free)(void *))
 Function to set the function to get protocol specific flow state. More...
 
int FlowSetMemcap (uint64_t size)
 Update memcap value. More...
 
uint64_t FlowGetMemcap (void)
 Return memcap value. More...
 
uint64_t FlowGetMemuse (void)
 
void FlowCleanupAppLayer (Flow *f)
 
int FlowUpdateSpareFlows (void)
 Make sure we have enough spare flows. More...
 
void FlowSetIPOnlyFlag (Flow *f, int direction)
 Set the IPOnly scanned flag for 'direction'. More...
 
void FlowSetHasAlertsFlag (Flow *f)
 Set flag to indicate that flow has alerts. More...
 
int FlowHasAlerts (const Flow *f)
 Check if flow has alerts. More...
 
void FlowSetChangeProtoFlag (Flow *f)
 Set flag to indicate to change proto for the flow. More...
 
void FlowUnsetChangeProtoFlag (Flow *f)
 Unset flag to indicate to change proto for the flow. More...
 
int FlowChangeProto (Flow *f)
 Check if change proto flag is set for flow. More...
 
void FlowSwap (Flow *f)
 swap the flow's direction More...
 
int FlowGetPacketDirection (const Flow *f, const Packet *p)
 determine the direction of the packet compared to the flow More...
 
void FlowHandlePacketUpdate (Flow *f, Packet *p)
 Update Packet and Flow. More...
 
void FlowHandlePacket (ThreadVars *tv, DecodeThreadVars *dtv, Packet *p)
 Entry point for packet flow handling. More...
 
void FlowInitConfig (char quiet)
 initialize the configuration More...
 
void FlowShutdown (void)
 shutdown the flow engine More...
 
int FlowClearMemory (Flow *f, uint8_t proto_map)
 Function clear the flow memory before queueing it to spare flow queue. More...
 
AppProto FlowGetAppProtocol (const Flow *f)
 
void * FlowGetAppState (const Flow *f)
 
uint8_t FlowGetDisruptionFlags (const Flow *f, uint8_t flags)
 get 'disruption' flags: GAP/DEPTH/PASS More...
 
void FlowUpdateState (Flow *f, enum FlowState s)
 

Variables

int run_mode
 

Detailed Description

Author
Victor Julien victo.nosp@m.r@in.nosp@m.linia.nosp@m.c.ne.nosp@m.t

Flow implementation.

Definition in file flow.c.

Macro Definition Documentation

#define FLOW_DEFAULT_EMERGENCY_RECOVERY   30

Definition at line 66 of file flow.c.

Referenced by FlowInitConfig().

#define FLOW_DEFAULT_HASHSIZE   65536

Definition at line 69 of file flow.c.

Referenced by FlowInitConfig().

#define FLOW_DEFAULT_MEMCAP   (32 * 1024 * 1024) /* 32 MB */

Definition at line 71 of file flow.c.

Referenced by FlowInitConfig().

#define FLOW_DEFAULT_PREALLOC   10000

Definition at line 73 of file flow.c.

Referenced by FlowInitConfig().

#define SET_DEFAULTS (   p,
  n,
  e,
  c,
  b,
  ne,
  ee,
  ce,
  be 
)
Value:
flow_timeouts_normal[(p)].new_timeout = (n); \
flow_timeouts_normal[(p)].est_timeout = (e); \
flow_timeouts_normal[(p)].closed_timeout = (c); \
flow_timeouts_normal[(p)].bypassed_timeout = (b); \
flow_timeouts_emerg[(p)].new_timeout = (ne); \
flow_timeouts_emerg[(p)].est_timeout = (ee); \
flow_timeouts_emerg[(p)].closed_timeout = (ce); \
flow_timeouts_emerg[(p)].bypassed_timeout = (be); \
FlowProtoTimeout flow_timeouts_normal[FLOW_PROTO_MAX]
Definition: flow-private.h:86
FlowProtoTimeout flow_timeouts_emerg[FLOW_PROTO_MAX]
Definition: flow-private.h:87

Referenced by FlowInitFlowProto().

Function Documentation

void FlowCleanupAppLayer ( Flow f)

Definition at line 125 of file flow.c.

References Flow_::alparser, Flow_::alstate, AppLayerParserStateCleanup(), and Flow_::proto.

Referenced by AppLayerIncTxCounter().

Here is the call graph for this function:

Here is the caller graph for this function:

int FlowClearMemory ( Flow f,
uint8_t  proto_map 
)

Function clear the flow memory before queueing it to spare flow queue.

Parameters
fpointer to the flow needed to be cleared.
proto_mapmapped value of the protocol to FLOW_PROTO's.

Definition at line 1027 of file flow.c.

References flow_freefuncs, FLOW_RECYCLE, FlowFreeStorage(), FlowProtoFreeFunc_::Freefunc, Flow_::protoctx, SCEnter, and SCReturnInt.

Referenced by FlowGetExistingFlowFromHash(), FlowShutdown(), FlowStorageRegister(), and TagTimeoutCheck().

Here is the call graph for this function:

Here is the caller graph for this function:

AppProto FlowGetAppProtocol ( const Flow f)

Definition at line 1060 of file flow.c.

References Flow_::alproto.

Referenced by JsonBuildFileInfoRecord(), SigMatchSignaturesGetSgh(), and Unified2Logger().

Here is the caller graph for this function:

void* FlowGetAppState ( const Flow f)

Definition at line 1065 of file flow.c.

References Flow_::alstate.

Referenced by DetectEngineStateResetTxs(), DetectTlsRegister(), HttpXFFGetIP(), and HttpXFFGetIPFromTx().

Here is the caller graph for this function:

uint8_t FlowGetDisruptionFlags ( const Flow f,
uint8_t  flags 
)

get 'disruption' flags: GAP/DEPTH/PASS

Parameters
flocked flow
flagsexisting flags to be ammended
Return values
flagsoriginal flags + disrupt flags (if any) handle UDP

Definition at line 1077 of file flow.c.

References TcpSession_::client, TcpStream_::flags, flags, Flow_::proto, Flow_::protoctx, TcpSession_::server, STREAM_DEPTH, STREAM_GAP, STREAM_TOSERVER, STREAMTCP_STREAM_FLAG_DEPTH_REACHED, and STREAMTCP_STREAM_FLAG_GAP.

Referenced by DetectSignatureApplyActions(), OutputRegisterTxLogger(), and SigMatchSignaturesGetSgh().

Here is the caller graph for this function:

uint64_t FlowGetMemcap ( void  )

Return memcap value.

Return values
memcapvalue

Definition at line 113 of file flow.c.

References flow_config, and SC_ATOMIC_GET.

Referenced by RunModeUnixSocketGetDefaultMode().

Here is the caller graph for this function:

uint64_t FlowGetMemuse ( void  )

Definition at line 119 of file flow.c.

References SC_ATOMIC_GET.

Referenced by FlowManagerThreadSpawn(), and RunModeUnixSocketGetDefaultMode().

Here is the caller graph for this function:

int FlowGetPacketDirection ( const Flow f,
const Packet p 
)

determine the direction of the packet compared to the flow

Return values
0to_server
1to_client

Definition at line 320 of file flow.c.

References CMP_ADDR, CMP_PORT, Packet_::dp, Flow_::flags, FLOW_DIR_REVERSED, FlowGetPacketDirection(), ICMPV4_IS_ERROR_MSG, MAX, Flow_::max_ttl_toclient, Flow_::max_ttl_toserver, MIN, Flow_::min_ttl_toclient, Flow_::min_ttl_toserver, PKT_IS_ICMPV4, Packet_::proto, Flow_::sp, Packet_::sp, Flow_::src, Packet_::src, TOCLIENT, and TOSERVER.

Referenced by FlowGetPacketDirection(), FlowHandlePacketUpdate(), and StreamTcpPacket().

Here is the call graph for this function:

Here is the caller graph for this function:

void FlowHandlePacket ( ThreadVars tv,
DecodeThreadVars dtv,
Packet p 
)

Entry point for packet flow handling.

This is called for every packet.

Parameters
tvthreadvars
dtvdecode thread vars (for flow output api thread data)
ppacket to handle flow for

Definition at line 496 of file flow.c.

References Packet_::flags, Packet_::flow, FlowGetFlowFromHash(), and PKT_HAS_FLOW.

Referenced by UTHBuildPacketOfFlows().

Here is the call graph for this function:

Here is the caller graph for this function:

int FlowHasAlerts ( const Flow f)

Check if flow has alerts.

Parameters
fflow
Return values
1has alerts
0has not alerts

Definition at line 208 of file flow.c.

References Flow_::flags, and FLOW_HAS_ALERTS.

void FlowInitConfig ( char  quiet)

initialize the configuration

Warning
Not thread safe

set config values for memcap, prealloc and hash_size

Definition at line 512 of file flow.c.

References ByteExtractStringUint32(), CLS, ConfGet(), ConfGetInt(), FlowCnf_::emergency_recovery, FALSE, FBLOCK_INIT, FLOW_CHECK_MEMCAP, flow_config, FLOW_DEFAULT_EMERGENCY_RECOVERY, FLOW_DEFAULT_HASHSIZE, FLOW_DEFAULT_MEMCAP, FLOW_DEFAULT_PREALLOC, flow_hash, flow_recycle_q, flow_spare_q, FlowAlloc(), FlowEnqueue(), FlowInitFlowProto(), FlowQueueInit(), FlowStorageSize(), FlowCnf_::hash_rand, FlowCnf_::hash_size, FlowQueue_::len, ParseSizeStringU64(), FlowCnf_::prealloc, RandomGet(), SC_ATOMIC_ADD, SC_ATOMIC_GET, SC_ATOMIC_INIT, SC_ATOMIC_SET, SC_ERR_FATAL, SC_ERR_FLOW_INIT, SC_ERR_INVALID_VALUE, SC_ERR_INVALID_YAML_CONF_ENTRY, SC_ERR_SIZE_PARSE, SCLogDebug, SCLogError, SCMallocAligned, and unlikely.

Referenced by __attribute__(), DecodeICMPV6(), DecodeIPV4(), DecodeIPV6(), DecodePPP(), DecodePPPOESession(), DecodeRaw(), DecodeTCP(), DecodeVXLAN(), DetectFastPatternRegister(), DetectFragBitsRegister(), DetectFragOffsetFree(), DetectIcmpIdFree(), DetectIPProtoRemoveAllSMs(), DetectPortHashFree(), DetectReplaceFreeInternal(), DetectSetupParseRegexes(), FlowStorageRegister(), FlowUpdateState(), ICMPv4GetCounterpart(), PreRunInit(), SCSigSignatureOrderingModuleCleanup(), SigParseApplyDsizeToContent(), TagTimeoutCheck(), TmModuleFlowRecyclerRegister(), Unified2AlertInitCtx(), and UTHParseSignature().

Here is the call graph for this function:

Here is the caller graph for this function:

void FlowInitFlowProto ( void  )
void FlowRegisterTests ( void  )

Function to register the Flow Unitests.

Definition at line 1327 of file flow.c.

References FlowMgrRegisterTests(), RegisterFlowStorageTests(), and UtRegisterTest().

Here is the call graph for this function:

void FlowSetChangeProtoFlag ( Flow f)

Set flag to indicate to change proto for the flow.

Parameters
fflow

Definition at line 221 of file flow.c.

References Flow_::flags, and FLOW_CHANGE_PROTO.

Referenced by AppLayerRequestProtocolChange().

Here is the caller graph for this function:

void FlowSetHasAlertsFlag ( Flow f)

Set flag to indicate that flow has alerts.

Parameters
fflow

Definition at line 197 of file flow.c.

References Flow_::flags, and FLOW_HAS_ALERTS.

Referenced by PacketAlertFinalize().

Here is the caller graph for this function:

void FlowSetIPOnlyFlag ( Flow f,
int  direction 
)

Set the IPOnly scanned flag for 'direction'.

Parameters
fFlow to set the flag in
directiondirection to set the flag in

Definition at line 186 of file flow.c.

References Flow_::flags, FLOW_TOCLIENT_IPONLY_SET, and FLOW_TOSERVER_IPONLY_SET.

Referenced by PacketAlertFinalize(), and SigMatchSignaturesGetSgh().

Here is the caller graph for this function:

int FlowSetMemcap ( uint64_t  size)

Update memcap value.

Parameters
sizenew memcap value

Definition at line 98 of file flow.c.

References flow_config, SC_ATOMIC_GET, and SC_ATOMIC_SET.

Referenced by RunModeUnixSocketGetDefaultMode().

Here is the caller graph for this function:

int FlowSetProtoFreeFunc ( uint8_t  proto,
void(*)(void *)  Free 
)

Function to set the function to get protocol specific flow state.

Parameters
protoprotocol of which function is needed to be set.
FreeFunction pointer which will be called to free the protocol specific memory.

Definition at line 1051 of file flow.c.

References flow_freefuncs, FlowGetProtoMapping(), and FlowProtoFreeFunc_::Freefunc.

Referenced by FlowUpdateState(), and StreamTcpInitConfig().

Here is the call graph for this function:

Here is the caller graph for this function:

void FlowSwap ( Flow f)

swap the flow's direction

Note
leaves the 'header' untouched. Interpret that based on FLOW_DIR_REVERSED flag.
Warning
: only valid before applayer parsing started. This function doesn't swap anything in Flow::alparser, Flow::alstate

Definition at line 289 of file flow.c.

References Flow_::alproto_tc, Flow_::alproto_ts, Flow_::flags, FLOW_DIR_REVERSED, Flow_::max_ttl_toclient, Flow_::max_ttl_toserver, Flow_::min_ttl_toclient, Flow_::min_ttl_toserver, Flow_::probing_parser_toclient_alproto_masks, Flow_::probing_parser_toserver_alproto_masks, Flow_::proto, Flow_::sgh_toclient, Flow_::sgh_toserver, SWAP_VARS, Flow_::todstbytecnt, Flow_::todstpktcnt, Flow_::tosrcbytecnt, and Flow_::tosrcpktcnt.

Referenced by AppLayerHandleUdp(), and AppLayerIncTxCounter().

Here is the caller graph for this function:

void FlowUnsetChangeProtoFlag ( Flow f)

Unset flag to indicate to change proto for the flow.

Parameters
fflow

Definition at line 230 of file flow.c.

References Flow_::flags, and FLOW_CHANGE_PROTO.

Referenced by AppLayerProtoDetectReset().

Here is the caller graph for this function:

int FlowUpdateSpareFlows ( void  )

Make sure we have enough spare flows.

Enforce the prealloc parameter, so keep at least prealloc flows in the spare queue and free flows going over the limit.

Return values
1if the queue was properly updated (or if it already was in good shape)
0otherwise.

Definition at line 144 of file flow.c.

References flow_config, flow_spare_q, FlowAlloc(), FlowDequeue(), FlowEnqueue(), FlowFree(), FQLOCK_LOCK, FQLOCK_UNLOCK, FlowQueue_::len, len, FlowCnf_::prealloc, and SCEnter.

Here is the call graph for this function:

SC_ATOMIC_DECLARE ( unsigned  int,
flow_prune_idx   
)

atomic int that is used when freeing a flow from the hash. In this case we walk the hash to find a flow to free. This var records where we left off in the hash. Without this only the top rows of the hash are freed. This isn't just about fairness. Under severe presure, the hash rows on top would be all freed and the time to find a flow to free increased with every run.

SC_ATOMIC_DECLARE ( unsigned  int,
flow_flags   
)

atomic flags

Variable Documentation

int run_mode

Run mode selected

Definition at line 204 of file suricata.c.