42 #define DETECT_CSUM_VALID "valid"
43 #define DETECT_CSUM_INVALID "invalid"
94 static int DetectIGMPCsumMatch(
100 static void DetectCsumRegisterTests(
void);
211 static int DetectCsumParseArg(
const char *key,
DetectCsumData *cd)
215 if (key[0] ==
'\"' && key[strlen(key) - 1] ==
'\"') {
220 str[strlen(key) - 2] =
'\0';
259 if (!PacketIsIPv4(p))
267 const IPV4Hdr *ip4h = PacketGetIPv4(p);
298 if (DetectCsumParseArg(csum_str, cd) == 0)
309 DetectIPV4CsumFree(
de_ctx, cd);
338 if (!PacketIsIPv4(p) || !PacketIsTCP(p) || p->
proto != IPPROTO_TCP)
346 const IPV4Hdr *ip4h = PacketGetIPv4(p);
347 const TCPHdr *tcph = PacketGetTCP(p);
348 p->
l4.
csum = TCPChecksum(ip4h->s_ip_addrs, (uint16_t *)tcph,
378 if (DetectCsumParseArg(csum_str, cd) == 0)
389 DetectTCPV4CsumFree(
de_ctx, cd);
418 if (!PacketIsIPv6(p) || !PacketIsTCP(p) || p->
proto != IPPROTO_TCP)
426 const IPV6Hdr *ip6h = PacketGetIPv6(p);
427 const TCPHdr *tcph = PacketGetTCP(p);
428 p->
l4.
csum = TCPV6Checksum(ip6h->s_ip6_addrs, (uint16_t *)tcph,
459 if (DetectCsumParseArg(csum_str, cd) == 0)
470 DetectTCPV6CsumFree(
de_ctx, cd);
499 if (!PacketIsIPv4(p) || !PacketIsUDP(p) || p->
proto != IPPROTO_UDP)
502 const UDPHdr *udph = PacketGetUDP(p);
511 const IPV4Hdr *ip4h = PacketGetIPv4(p);
512 p->
l4.
csum = UDPV4Checksum(ip4h->s_ip_addrs, (uint16_t *)udph,
542 if (DetectCsumParseArg(csum_str, cd) == 0)
553 DetectUDPV4CsumFree(
de_ctx, cd);
582 if (!PacketIsIPv6(p) || !PacketIsUDP(p) || p->
proto != IPPROTO_UDP)
590 const IPV6Hdr *ip6h = PacketGetIPv6(p);
591 const UDPHdr *udph = PacketGetUDP(p);
592 p->
l4.
csum = UDPV6Checksum(ip6h->s_ip6_addrs, (uint16_t *)udph,
622 if (DetectCsumParseArg(csum_str, cd) == 0)
633 DetectUDPV6CsumFree(
de_ctx, cd);
665 if (!PacketIsIPv4(p) || !PacketIsICMPv4(p) || p->
proto != IPPROTO_ICMP)
672 const ICMPV4Hdr *icmpv4h = PacketGetICMPv4(p);
674 const IPV4Hdr *ip4h = PacketGetIPv4(p);
675 p->
l4.
csum = ICMPV4CalculateChecksum(
679 if (p->
l4.
csum == icmpv4h->checksum && cd->
valid == 1)
681 else if (p->
l4.
csum != icmpv4h->checksum && cd->
valid == 0)
705 if (DetectCsumParseArg(csum_str, cd) == 0)
716 DetectICMPV4CsumFree(
de_ctx, cd);
745 if (!PacketIsIPv6(p) || !PacketIsICMPv6(p) || p->
proto != IPPROTO_ICMPV6) {
748 const ICMPV6Hdr *icmpv6h = PacketGetICMPv6(p);
758 const IPV6Hdr *ip6h = PacketGetIPv6(p);
761 p->
l4.
csum = ICMPV6CalculateChecksum(ip6h->s_ip6_addrs, (uint16_t *)icmpv6h,
len);
791 if (DetectCsumParseArg(csum_str, cd) == 0)
802 DetectICMPV6CsumFree(
de_ctx, cd);
826 static int DetectIGMPCsumMatch(
831 if (!PacketIsIPv4(p) || !PacketIsIGMP(p) || p->
proto != IPPROTO_IGMP)
838 const IGMPHdr *igmph = PacketGetIGMP(p);
840 const IPV4Hdr *ip4h = PacketGetIPv4(p);
841 p->
l4.
csum = ICMPV4CalculateChecksum(
845 if (p->
l4.
csum == igmph->checksum && cd->
valid == 1)
847 else if (p->
l4.
csum != igmph->checksum && cd->
valid == 0)
871 if (DetectCsumParseArg(csum_str, cd) == 0)
882 DetectIGMPCsumFree(
de_ctx, cd);
900 #define TEST1(kwstr) {\
901 DetectEngineCtx *de_ctx = DetectEngineCtxInit();\
902 FAIL_IF_NULL(de_ctx);\
903 de_ctx->flags = DE_QUIET;\
905 Signature *s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:valid; sid:1;)");\
907 s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:invalid; sid:2;)");\
909 s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:vaLid; sid:3;)");\
911 s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:VALID; sid:4;)");\
913 s = DetectEngineAppendSig(de_ctx, "alert ip any any -> any any ("mystr(kwstr)"-csum:iNvaLid; sid:5;)");\
915 DetectEngineCtxFree(de_ctx);\
919 static int DetectCsumValidArgsTestParse01(
void)
932 #define TEST2(kwstr) \
934 DetectEngineCtx *de_ctx = DetectEngineCtxInit(); \
935 FAIL_IF_NULL(de_ctx); \
936 Signature *s = DetectEngineAppendSig( \
937 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:xxxx; sid:1;)"); \
939 s = DetectEngineAppendSig( \
940 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:xxxxxxxx; sid:2;)"); \
942 s = DetectEngineAppendSig( \
943 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:xxxxxx; sid:3;)"); \
945 s = DetectEngineAppendSig( \
946 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:XXXXXX; sid:4;)"); \
948 s = DetectEngineAppendSig( \
949 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:XxXxXxX; sid:5;)"); \
951 DetectEngineCtxFree(de_ctx); \
954 static int DetectCsumInvalidArgsTestParse02(
void)
967 #define TEST3(kwstr, kwtype) \
969 DetectEngineCtx *de_ctx = DetectEngineCtxInit(); \
970 FAIL_IF_NULL(de_ctx); \
971 Signature *s = DetectEngineAppendSig( \
972 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:valid; sid:1;)"); \
974 SigMatch *sm = SCDetectGetLastSMFromLists(s, (kwtype), -1); \
976 FAIL_IF_NULL(sm->ctx); \
977 FAIL_IF_NOT(((DetectCsumData *)sm->ctx)->valid == 1); \
978 s = DetectEngineAppendSig( \
979 de_ctx, "alert ip any any -> any any (" mystr(kwstr) "-csum:INVALID; sid:2;)"); \
981 sm = SCDetectGetLastSMFromLists(s, (kwtype), -1); \
983 FAIL_IF_NULL(sm->ctx); \
984 FAIL_IF_NOT(((DetectCsumData *)sm->ctx)->valid == 0); \
985 DetectEngineCtxFree(de_ctx); \
988 static int DetectCsumValidArgsTestParse03(
void)
1004 static int DetectCsumICMPV6Test01(
void)
1014 0x00, 0x30, 0x18, 0xa8, 0x7c, 0x23, 0x2c, 0x41,
1015 0x38, 0xa7, 0xea, 0xeb, 0x86, 0xdd, 0x60, 0x00,
1016 0x00, 0x00, 0x00, 0x40, 0x3c, 0x40, 0xad, 0xa1,
1017 0x09, 0x80, 0x00, 0x01, 0xd6, 0xf3, 0x20, 0x01,
1018 0xf4, 0xbe, 0xea, 0x3c, 0x00, 0x01, 0x00, 0x00,
1019 0x00, 0x00, 0x32, 0xb2, 0x00, 0x01, 0x32, 0xb2,
1020 0x09, 0x80, 0x20, 0x01, 0x00, 0x00, 0x3c, 0x00,
1021 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00,
1022 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00,
1023 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00,
1024 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00,
1025 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00,
1026 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x00,
1027 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
1028 0x63, 0xc2, 0x00, 0x00, 0x00, 0x00 };
1032 memset(&
tv, 0,
sizeof(
tv));
1034 memset(&
dtv, 0,
sizeof(
dtv));
1045 "(icmpv6-csum:valid; sid:1;)");
1065 static void DetectCsumRegisterTests(
void)
1068 DetectCsumValidArgsTestParse01);
1070 DetectCsumInvalidArgsTestParse02);
1072 DetectCsumValidArgsTestParse03);
1075 DetectCsumICMPV6Test01);