42#define DETECT_CSUM_VALID "valid"
43#define DETECT_CSUM_INVALID "invalid"
94static void DetectCsumRegisterTests(
void);
196 if (key[0] ==
'\"' && key[strlen(key) - 1] ==
'\"') {
201 str[strlen(key) - 2] =
'\0';
240 if (!PacketIsIPv4(p))
248 const IPV4Hdr *ip4h = PacketGetIPv4(p);
279 if (DetectCsumParseArg(csum_str, cd) == 0)
290 DetectIPV4CsumFree(
de_ctx, cd);
319 if (!PacketIsIPv4(p) || !PacketIsTCP(p) || p->
proto != IPPROTO_TCP)
327 const IPV4Hdr *ip4h = PacketGetIPv4(p);
328 const TCPHdr *tcph = PacketGetTCP(p);
329 p->
l4.
csum = TCPChecksum(ip4h->s_ip_addrs, (uint16_t *)tcph,
359 if (DetectCsumParseArg(csum_str, cd) == 0)
370 DetectTCPV4CsumFree(
de_ctx, cd);
399 if (!PacketIsIPv6(p) || !PacketIsTCP(p) || p->
proto != IPPROTO_TCP)
407 const IPV6Hdr *ip6h = PacketGetIPv6(p);
408 const TCPHdr *tcph = PacketGetTCP(p);
409 p->
l4.
csum = TCPV6Checksum(ip6h->s_ip6_addrs, (uint16_t *)tcph,
440 if (DetectCsumParseArg(csum_str, cd) == 0)
451 DetectTCPV6CsumFree(
de_ctx, cd);
480 if (!PacketIsIPv4(p) || !PacketIsUDP(p) || p->
proto != IPPROTO_UDP)
483 const UDPHdr *udph = PacketGetUDP(p);
492 const IPV4Hdr *ip4h = PacketGetIPv4(p);
493 p->
l4.
csum = UDPV4Checksum(ip4h->s_ip_addrs, (uint16_t *)udph,
523 if (DetectCsumParseArg(csum_str, cd) == 0)
534 DetectUDPV4CsumFree(
de_ctx, cd);
563 if (!PacketIsIPv6(p) || !PacketIsUDP(p) || p->
proto != IPPROTO_UDP)
571 const IPV6Hdr *ip6h = PacketGetIPv6(p);
572 const UDPHdr *udph = PacketGetUDP(p);
573 p->
l4.
csum = UDPV6Checksum(ip6h->s_ip6_addrs, (uint16_t *)udph,
603 if (DetectCsumParseArg(csum_str, cd) == 0)
614 DetectUDPV6CsumFree(
de_ctx, cd);
646 if (!PacketIsIPv4(p) || !PacketIsICMPv4(p) || p->
proto != IPPROTO_ICMP)
653 const ICMPV4Hdr *icmpv4h = PacketGetICMPv4(p);
655 const IPV4Hdr *ip4h = PacketGetIPv4(p);
656 p->
l4.
csum = ICMPV4CalculateChecksum(
660 if (p->
l4.
csum == icmpv4h->checksum && cd->
valid == 1)
662 else if (p->
l4.
csum != icmpv4h->checksum && cd->
valid == 0)
686 if (DetectCsumParseArg(csum_str, cd) == 0)
697 DetectICMPV4CsumFree(
de_ctx, cd);
726 if (!PacketIsIPv6(p) || !PacketIsICMPv6(p) || p->
proto != IPPROTO_ICMPV6) {
729 const ICMPV6Hdr *icmpv6h = PacketGetICMPv6(p);
739 const IPV6Hdr *ip6h = PacketGetIPv6(p);
742 p->
l4.
csum = ICMPV6CalculateChecksum(ip6h->s_ip6_addrs, (uint16_t *)icmpv6h,
len);
772 if (DetectCsumParseArg(csum_str, cd) == 0)
783 DetectICMPV6CsumFree(
de_ctx, cd);
801#define TEST1(kwstr) {\
802 DetectEngineCtx *de_ctx = DetectEngineCtxInit();\
803 FAIL_IF_NULL(de_ctx);\
804 de_ctx->flags = DE_QUIET;\
806 Signature *s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:valid; sid:1;)");\
808 s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:invalid; sid:2;)");\
810 s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:vaLid; sid:3;)");\
812 s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:VALID; sid:4;)");\
814 s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:iNvaLid; sid:5;)");\
816 DetectEngineCtxFree(de_ctx);\
820static int DetectCsumValidArgsTestParse01(
void)
833#define TEST2(kwstr) \
835 DetectEngineCtx *de_ctx = DetectEngineCtxInit(); \
836 FAIL_IF_NULL(de_ctx); \
837 Signature *s = DetectEngineAppendSig( \
838 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:xxxx; sid:1;)"); \
840 s = DetectEngineAppendSig( \
841 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:xxxxxxxx; sid:2;)"); \
843 s = DetectEngineAppendSig( \
844 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:xxxxxx; sid:3;)"); \
846 s = DetectEngineAppendSig( \
847 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:XXXXXX; sid:4;)"); \
849 s = DetectEngineAppendSig( \
850 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:XxXxXxX; sid:5;)"); \
852 DetectEngineCtxFree(de_ctx); \
855static int DetectCsumInvalidArgsTestParse02(
void)
868#define TEST3(kwstr, kwtype) { \
869 DetectEngineCtx *de_ctx = DetectEngineCtxInit();\
870 FAIL_IF_NULL(de_ctx);\
871 Signature *s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:valid; sid:1;)");\
873 SigMatch *sm = DetectGetLastSMFromLists(s, (kwtype), -1);\
875 FAIL_IF_NULL(sm->ctx);\
876 FAIL_IF_NOT(((DetectCsumData *)sm->ctx)->valid == 1);\
877 s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:INVALID; sid:2;)");\
879 sm = DetectGetLastSMFromLists(s, (kwtype), -1);\
881 FAIL_IF_NULL(sm->ctx);\
882 FAIL_IF_NOT(((DetectCsumData *)sm->ctx)->valid == 0);\
883 DetectEngineCtxFree(de_ctx);\
886static int DetectCsumValidArgsTestParse03(
void)
902static int DetectCsumICMPV6Test01(
void)
914 0x00, 0x30, 0x18, 0xa8, 0x7c, 0x23, 0x2c, 0x41,
915 0x38, 0xa7, 0xea, 0xeb, 0x86, 0xdd, 0x60, 0x00,
916 0x00, 0x00, 0x00, 0x40, 0x3c, 0x40, 0xad, 0xa1,
917 0x09, 0x80, 0x00, 0x01, 0xd6, 0xf3, 0x20, 0x01,
918 0xf4, 0xbe, 0xea, 0x3c, 0x00, 0x01, 0x00, 0x00,
919 0x00, 0x00, 0x32, 0xb2, 0x00, 0x01, 0x32, 0xb2,
920 0x09, 0x80, 0x20, 0x01, 0x00, 0x00, 0x3c, 0x00,
921 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00,
922 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00,
923 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00,
924 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00,
925 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00,
926 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x00,
927 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
928 0x63, 0xc2, 0x00, 0x00, 0x00, 0x00 };
932 memset(&
tv, 0,
sizeof(
tv));
933 memset(&
dtv, 0,
sizeof(
dtv));
944 "(icmpv6-csum:valid; sid:1;)");
966static void DetectCsumRegisterTests(
void)
969 DetectCsumValidArgsTestParse01);
971 DetectCsumInvalidArgsTestParse02);
973 DetectCsumValidArgsTestParse03);
976 DetectCsumICMPV6Test01);
int DecodeEthernet(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
#define IPV4_GET_RAW_IPLEN(ip4h)
#define IPV4_GET_RAW_HLEN(ip4h)
#define IPV6_GET_RAW_PLEN(ip6h)
#define TCP_GET_RAW_HLEN(tcph)
#define PKT_IGNORE_CHECKSUM
void DetectCsumRegister(void)
Registers handlers for all the checksum keywords. The checksum keywords that are registered are ipv4-...
#define DETECT_CSUM_VALID
struct DetectCsumData_ DetectCsumData
#define TEST3(kwstr, kwtype)
#define DETECT_CSUM_INVALID
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
DetectEngineCtx * DetectEngineCtxInit(void)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
SigTableElmt * sigmatch_table
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
void FlowInitConfig(bool quiet)
initialize the configuration
void FlowShutdown(void)
shutdown the flow engine
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define PASS
Pass the test.
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
int PacketCopyData(Packet *p, const uint8_t *pktdata, uint32_t pktlen)
Copy data to Packet payload and set packet length.
void PacketRecycle(Packet *p)
void StreamTcpFreeConfig(bool quiet)
void StreamTcpInitConfig(bool)
To initialize the stream global configuration data.
Structure to hold thread specific data for all decode modules.
main detection engine ctx
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
void(* Free)(DetectEngineCtx *, void *)
void(* RegisterTests)(void)
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Per thread variable structure.
uint8_t mpm_default_matcher