41 #define DETECT_CSUM_VALID "valid"
42 #define DETECT_CSUM_INVALID "invalid"
93 static int DetectIGMPCsumMatch(
99 static void DetectCsumRegisterTests(
void);
210 static int DetectCsumParseArg(
const char *key,
DetectCsumData *cd)
214 if (key[0] ==
'\"' && key[strlen(key) - 1] ==
'\"') {
219 str[strlen(key) - 2] =
'\0';
258 if (!PacketIsIPv4(
p))
266 const IPV4Hdr *ip4h = PacketGetIPv4(
p);
297 if (DetectCsumParseArg(csum_str, cd) == 0)
308 DetectIPV4CsumFree(
de_ctx, cd);
337 if (!PacketIsIPv4(
p) || !PacketIsTCP(
p) ||
p->
proto != IPPROTO_TCP)
345 const IPV4Hdr *ip4h = PacketGetIPv4(
p);
346 const TCPHdr *tcph = PacketGetTCP(
p);
347 p->
l4.
csum = TCPChecksum(ip4h->s_ip_addrs, (uint16_t *)tcph,
377 if (DetectCsumParseArg(csum_str, cd) == 0)
388 DetectTCPV4CsumFree(
de_ctx, cd);
417 if (!PacketIsIPv6(
p) || !PacketIsTCP(
p) ||
p->
proto != IPPROTO_TCP)
425 const IPV6Hdr *ip6h = PacketGetIPv6(
p);
426 const TCPHdr *tcph = PacketGetTCP(
p);
427 p->
l4.
csum = TCPV6Checksum(ip6h->s_ip6_addrs, (uint16_t *)tcph,
458 if (DetectCsumParseArg(csum_str, cd) == 0)
469 DetectTCPV6CsumFree(
de_ctx, cd);
498 if (!PacketIsIPv4(
p) || !PacketIsUDP(
p) ||
p->
proto != IPPROTO_UDP)
501 const UDPHdr *udph = PacketGetUDP(
p);
510 const IPV4Hdr *ip4h = PacketGetIPv4(
p);
511 p->
l4.
csum = UDPV4Checksum(ip4h->s_ip_addrs, (uint16_t *)udph,
541 if (DetectCsumParseArg(csum_str, cd) == 0)
552 DetectUDPV4CsumFree(
de_ctx, cd);
581 if (!PacketIsIPv6(
p) || !PacketIsUDP(
p) ||
p->
proto != IPPROTO_UDP)
589 const IPV6Hdr *ip6h = PacketGetIPv6(
p);
590 const UDPHdr *udph = PacketGetUDP(
p);
591 p->
l4.
csum = UDPV6Checksum(ip6h->s_ip6_addrs, (uint16_t *)udph,
621 if (DetectCsumParseArg(csum_str, cd) == 0)
632 DetectUDPV6CsumFree(
de_ctx, cd);
664 if (!PacketIsIPv4(
p) || !PacketIsICMPv4(
p) ||
p->
proto != IPPROTO_ICMP)
671 const ICMPV4Hdr *icmpv4h = PacketGetICMPv4(
p);
673 const IPV4Hdr *ip4h = PacketGetIPv4(
p);
674 p->
l4.
csum = ICMPV4CalculateChecksum(
680 else if (
p->
l4.
csum != icmpv4h->checksum && cd->
valid == 0)
704 if (DetectCsumParseArg(csum_str, cd) == 0)
715 DetectICMPV4CsumFree(
de_ctx, cd);
744 if (!PacketIsIPv6(
p) || !PacketIsICMPv6(
p) ||
p->
proto != IPPROTO_ICMPV6) {
747 const ICMPV6Hdr *icmpv6h = PacketGetICMPv6(
p);
757 const IPV6Hdr *ip6h = PacketGetIPv6(
p);
760 p->
l4.
csum = ICMPV6CalculateChecksum(ip6h->s_ip6_addrs, (uint16_t *)icmpv6h,
len);
790 if (DetectCsumParseArg(csum_str, cd) == 0)
801 DetectICMPV6CsumFree(
de_ctx, cd);
825 static int DetectIGMPCsumMatch(
830 if (!PacketIsIPv4(
p) || !PacketIsIGMP(
p) ||
p->
proto != IPPROTO_IGMP)
837 const IGMPHdr *igmph = PacketGetIGMP(
p);
839 const IPV4Hdr *ip4h = PacketGetIPv4(
p);
840 p->
l4.
csum = ICMPV4CalculateChecksum(
846 else if (
p->
l4.
csum != igmph->checksum && cd->
valid == 0)
870 if (DetectCsumParseArg(csum_str, cd) == 0)
881 DetectIGMPCsumFree(
de_ctx, cd);
899 #define TEST1(kwstr) {\
900 DetectEngineCtx *de_ctx = DetectEngineCtxInit();\
901 FAIL_IF_NULL(de_ctx);\
902 de_ctx->flags = DE_QUIET;\
904 Signature *s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:valid; sid:1;)");\
906 s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:invalid; sid:2;)");\
908 s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:vaLid; sid:3;)");\
910 s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:VALID; sid:4;)");\
912 s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:iNvaLid; sid:5;)");\
914 DetectEngineCtxFree(de_ctx);\
918 static int DetectCsumValidArgsTestParse01(
void)
931 #define TEST2(kwstr) \
933 DetectEngineCtx *de_ctx = DetectEngineCtxInit(); \
934 FAIL_IF_NULL(de_ctx); \
935 Signature *s = DetectEngineAppendSig( \
936 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:xxxx; sid:1;)"); \
938 s = DetectEngineAppendSig( \
939 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:xxxxxxxx; sid:2;)"); \
941 s = DetectEngineAppendSig( \
942 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:xxxxxx; sid:3;)"); \
944 s = DetectEngineAppendSig( \
945 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:XXXXXX; sid:4;)"); \
947 s = DetectEngineAppendSig( \
948 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:XxXxXxX; sid:5;)"); \
950 DetectEngineCtxFree(de_ctx); \
953 static int DetectCsumInvalidArgsTestParse02(
void)
966 #define TEST3(kwstr, kwtype) \
968 DetectEngineCtx *de_ctx = DetectEngineCtxInit(); \
969 FAIL_IF_NULL(de_ctx); \
970 Signature *s = DetectEngineAppendSig( \
971 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:valid; sid:1;)"); \
973 SigMatch *sm = SCDetectGetLastSMFromLists(s, (kwtype), -1); \
975 FAIL_IF_NULL(sm->ctx); \
976 FAIL_IF_NOT(((DetectCsumData *)sm->ctx)->valid == 1); \
977 s = DetectEngineAppendSig( \
978 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:INVALID; sid:2;)"); \
980 sm = SCDetectGetLastSMFromLists(s, (kwtype), -1); \
982 FAIL_IF_NULL(sm->ctx); \
983 FAIL_IF_NOT(((DetectCsumData *)sm->ctx)->valid == 0); \
984 DetectEngineCtxFree(de_ctx); \
987 static int DetectCsumValidArgsTestParse03(
void)
1003 static int DetectCsumICMPV6Test01(
void)
1013 0x00, 0x30, 0x18, 0xa8, 0x7c, 0x23, 0x2c, 0x41,
1014 0x38, 0xa7, 0xea, 0xeb, 0x86, 0xdd, 0x60, 0x00,
1015 0x00, 0x00, 0x00, 0x40, 0x3c, 0x40, 0xad, 0xa1,
1016 0x09, 0x80, 0x00, 0x01, 0xd6, 0xf3, 0x20, 0x01,
1017 0xf4, 0xbe, 0xea, 0x3c, 0x00, 0x01, 0x00, 0x00,
1018 0x00, 0x00, 0x32, 0xb2, 0x00, 0x01, 0x32, 0xb2,
1019 0x09, 0x80, 0x20, 0x01, 0x00, 0x00, 0x3c, 0x00,
1020 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00,
1021 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00,
1022 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00,
1023 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00,
1024 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00,
1025 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x00,
1026 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
1027 0x63, 0xc2, 0x00, 0x00, 0x00, 0x00 };
1031 memset(&
tv, 0,
sizeof(
tv));
1033 memset(&
dtv, 0,
sizeof(
dtv));
1044 "(icmpv6-csum:valid; sid:1;)");
1064 static void DetectCsumRegisterTests(
void)
1067 DetectCsumValidArgsTestParse01);
1069 DetectCsumInvalidArgsTestParse02);
1071 DetectCsumValidArgsTestParse03);
1074 DetectCsumICMPV6Test01);