42 #define DETECT_CSUM_VALID "valid"
43 #define DETECT_CSUM_INVALID "invalid"
94 static void DetectCsumRegisterTests(
void);
192 static int DetectCsumParseArg(
const char *key,
DetectCsumData *cd)
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);\
820 static 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); \
855 static 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);\
886 static int DetectCsumValidArgsTestParse03(
void)
902 static 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;)");
966 static void DetectCsumRegisterTests(
void)
969 DetectCsumValidArgsTestParse01);
971 DetectCsumInvalidArgsTestParse02);
973 DetectCsumValidArgsTestParse03);
976 DetectCsumICMPV6Test01);