@ -134,12 +134,11 @@ static void OnFlowUpdate(ThreadVars *tv, Flow *f, Packet *p, void *_data)
/* Ignore packets that have a different protocol than the
* flow . This can happen with ICMP unreachable packets . */
if ( p - > proto ! = flow_proto ) {
if ( SCPacketGetIPProto ( p ) ! = flow_proto ) {
return ;
}
uint16_t ip_len = 0 ;
void * ip_ptr = NULL ;
uint32_t ip_len = 0 ;
struct NdpiThreadContext * threadctx = NdpiGetThreadContext ( tv ) ;
struct NdpiFlowContext * flowctx = NdpiGetFlowContext ( f ) ;
@ -147,19 +146,15 @@ static void OnFlowUpdate(ThreadVars *tv, Flow *f, Packet *p, void *_data)
return ;
if ( flowctx = = NULL | | flowctx - > ndpi_flow = = NULL )
return ;
if ( ! ( PacketIsIPv4 ( p ) | | PacketIsIPv6 ( p ) ) )
return ;
if ( PacketIsIPv4 ( p ) ) {
const IPV4Hdr * ip4h = PacketGetIPv4 ( p ) ;
ip_len = IPV4_GET_RAW_IPLEN ( ip4h ) ;
ip_ptr = ( void * ) PacketGetIPv4 ( p ) ;
} else if ( PacketIsIPv6 ( p ) ) {
const IPV6Hdr * ip6h = PacketGetIPv6 ( p ) ;
ip_len = IPV6_HEADER_LEN + IPV6_GET_RAW_PLEN ( ip6h ) ;
ip_ptr = ( void * ) PacketGetIPv6 ( p ) ;
}
const uint8_t * ip_ptr = SCPacketGetL3 ( p , & ip_len ) ;
if ( ! flowctx - > detection_completed & & ip_ptr ! = NULL & & ip_len > 0 ) {
uint64_t time_ms = ( ( uint64_t ) p - > ts . secs ) * 1000 + p - > ts . usecs / 1000 ;
uint64_t secs = 0 , usecs = 0 ;
SCPacketGetTimeAsParts ( p , & secs , & usecs ) ;
uint64_t time_ms = secs * 1000 + usecs / 1000 ;
SCLogDebug ( " Performing nDPI detection... " ) ;
@ -225,7 +220,7 @@ static int DetectnDPIProtocolPacketMatch(
{
SCEnter ( ) ;
const Flow * f = p - > flow ;
const Flow * f = SCPacketGetFlow ( p ) ;
if ( f = = NULL ) {
SCLogDebug ( " packet % " PRIu64 " : no flow " , PcapPacketCntGet ( p ) ) ;
SCReturnInt ( 0 ) ;
@ -321,20 +316,19 @@ static bool nDPIProtocolDataHasConflicts(
static int DetectnDPIProtocolSetup ( DetectEngineCtx * de_ctx , Signature * s , const char * arg )
{
DetectnDPIProtocolData * data = DetectnDPIProtocolParse ( arg , s - > init_data - > negated ) ;
DetectnDPIProtocolData * data = DetectnDPIProtocolParse ( arg , SCDetectSignatureIsNegated ( s ) ) ;
if ( data = = NULL )
goto error ;
SigMatch * tsm = s - > init_data - > smlists [ DETECT_SM_LIST_MATCH ] ;
for ( ; tsm ! = NULL ; tsm = tsm - > next ) {
if ( tsm - > type = = ndpi_protocol_keyword_id ) {
const DetectnDPIProtocolData * them = ( const DetectnDPIProtocolData * ) tsm - > ctx ;
const SigMatch * tsm =
SCDetectSignatureGetLastSMByType ( s , DETECT_SM_LIST_MATCH , ndpi_protocol_keyword_id ) ;
for ( ; tsm ! = NULL ; tsm = SCSigMatchGetPrevByType ( tsm , ndpi_protocol_keyword_id ) ) {
const DetectnDPIProtocolData * them = ( const DetectnDPIProtocolData * ) SCSigMatchGetCtx ( tsm ) ;
if ( nDPIProtocolDataHasConflicts ( data , them ) ) {
SCLogError ( " can't mix "
" positive ndpi-protocol match with negated " ) ;
goto error ;
}
if ( nDPIProtocolDataHasConflicts ( data , them ) ) {
SCLogError ( " can't mix "
" positive ndpi-protocol match with negated " ) ;
goto error ;
}
}
@ -360,7 +354,7 @@ static int DetectnDPIRiskPacketMatch(
{
SCEnter ( ) ;
const Flow * f = p - > flow ;
const Flow * f = SCPacketGetFlow ( p ) ;
if ( f = = NULL ) {
SCLogDebug ( " packet % " PRIu64 " : no flow " , PcapPacketCntGet ( p ) ) ;
SCReturnInt ( 0 ) ;
@ -461,20 +455,19 @@ static bool nDPIRiskDataHasConflicts(const DetectnDPIRiskData *us, const Detectn
static int DetectnDPIRiskSetup ( DetectEngineCtx * de_ctx , Signature * s , const char * arg )
{
DetectnDPIRiskData * data = DetectnDPIRiskParse ( arg , s - > init_data - > negated ) ;
DetectnDPIRiskData * data = DetectnDPIRiskParse ( arg , SCDetectSignatureIsNegated ( s ) ) ;
if ( data = = NULL )
goto error ;
SigMatch * tsm = s - > init_data - > smlists [ DETECT_SM_LIST_MATCH ] ;
for ( ; tsm ! = NULL ; tsm = tsm - > next ) {
if ( tsm - > type = = ndpi_risk_keyword_id ) {
const DetectnDPIRiskData * them = ( const DetectnDPIRiskData * ) tsm - > ctx ;
const SigMatch * tsm =
SCDetectSignatureGetLastSMByType ( s , DETECT_SM_LIST_MATCH , ndpi_risk_keyword_id ) ;
for ( ; tsm ! = NULL ; tsm = SCSigMatchGetPrevByType ( tsm , ndpi_risk_keyword_id ) ) {
const DetectnDPIRiskData * them = ( const DetectnDPIRiskData * ) SCSigMatchGetCtx ( tsm ) ;
if ( nDPIRiskDataHasConflicts ( data , them ) ) {
SCLogError ( " can't mix "
" positive ndpi-risk match with negated " ) ;
goto error ;
}
if ( nDPIRiskDataHasConflicts ( data , them ) ) {
SCLogError ( " can't mix "
" positive ndpi-risk match with negated " ) ;
goto error ;
}
}
@ -538,34 +531,36 @@ static void EveCallback(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder
static void NdpInitRiskKeyword ( void )
{
/* SCSigTableAppLiteElmt and SCDetectHelperKeywordRegister don't yet
* support all the fields required to register the nDPI keywords ,
* missing the ( packet ) Match callback ,
* so we ' ll just register with an empty keyword specifier to get
* the ID , then fill in the ID . */
ndpi_protocol_keyword_id = SCDetectHelperNewKeywordId ( ) ;
const SCSigTableAppLiteElmt protocol_kw = {
. name = " ndpi-protocol " ,
. desc = " match on the detected nDPI protocol " ,
. url = " /rules/ndpi-protocol.html " ,
. flags = SIGMATCH_QUOTES_OPTIONAL | SIGMATCH_HANDLE_NEGATION ,
. Setup = DetectnDPIProtocolSetup ,
. Free = DetectnDPIProtocolFree ,
} ;
ndpi_protocol_keyword_id = SCDetectHelperKeywordRegister ( & protocol_kw ) ;
if ( ndpi_protocol_keyword_id = = UINT16_MAX ) {
FatalError ( " Failed to register ndpi-protocol keyword " ) ;
}
SCDetectHelperKeywordPacketMatchRegister (
ndpi_protocol_keyword_id , DetectnDPIProtocolPacketMatch ) ;
SCLogDebug ( " Registered new ndpi-protocol keyword with ID % " PRIu32 , ndpi_protocol_keyword_id ) ;
sigmatch_table [ ndpi_protocol_keyword_id ] . name = " ndpi-protocol " ;
sigmatch_table [ ndpi_protocol_keyword_id ] . desc = " match on the detected nDPI protocol " ;
sigmatch_table [ ndpi_protocol_keyword_id ] . url = " /rules/ndpi-protocol.html " ;
sigmatch_table [ ndpi_protocol_keyword_id ] . Match = DetectnDPIProtocolPacketMatch ;
sigmatch_table [ ndpi_protocol_keyword_id ] . Setup = DetectnDPIProtocolSetup ;
sigmatch_table [ ndpi_protocol_keyword_id ] . Free = DetectnDPIProtocolFree ;
sigmatch_table [ ndpi_protocol_keyword_id ] . flags =
( SIGMATCH_QUOTES_OPTIONAL | SIGMATCH_HANDLE_NEGATION ) ;
ndpi_risk_keyword_id = SCDetectHelperNewKeywordId ( ) ;
const SCSigTableAppLiteElmt risk_kw = {
. name = " ndpi-risk " ,
. desc = " match on the detected nDPI risk " ,
. url = " /rules/ndpi-risk.html " ,
. flags = SIGMATCH_QUOTES_OPTIONAL | SIGMATCH_HANDLE_NEGATION ,
. Setup = DetectnDPIRiskSetup ,
. Free = DetectnDPIRiskFree ,
} ;
ndpi_risk_keyword_id = SCDetectHelperKeywordRegister ( & risk_kw ) ;
if ( ndpi_risk_keyword_id = = UINT16_MAX ) {
FatalError ( " Failed to register ndpi-risk keyword " ) ;
}
SCDetectHelperKeywordPacketMatchRegister ( ndpi_risk_keyword_id , DetectnDPIRiskPacketMatch ) ;
SCLogDebug ( " Registered new ndpi-risk keyword with ID % " PRIu32 , ndpi_risk_keyword_id ) ;
sigmatch_table [ ndpi_risk_keyword_id ] . name = " ndpi-risk " ;
sigmatch_table [ ndpi_risk_keyword_id ] . desc = " match on the detected nDPI risk " ;
sigmatch_table [ ndpi_risk_keyword_id ] . url = " /rules/ndpi-risk.html " ;
sigmatch_table [ ndpi_risk_keyword_id ] . Match = DetectnDPIRiskPacketMatch ;
sigmatch_table [ ndpi_risk_keyword_id ] . Setup = DetectnDPIRiskSetup ;
sigmatch_table [ ndpi_risk_keyword_id ] . Free = DetectnDPIRiskFree ;
sigmatch_table [ ndpi_risk_keyword_id ] . flags =
( SIGMATCH_QUOTES_OPTIONAL | SIGMATCH_HANDLE_NEGATION ) ;
}
static void NdpiInit ( void )