suricata
detect-sctp-chunk-cnt.c
Go to the documentation of this file.
1 /* Copyright (C) 2026 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * Implements sctp.chunk_cnt keyword
22  *
23  * Author: Giuseppe Longo <glongo@oisf.net>
24  */
25 
26 #include "suricata-common.h"
27 #include "decode.h"
28 
29 #include "detect.h"
30 #include "detect-parse.h"
32 #include "detect-engine-build.h"
33 
34 #include "detect-sctp-chunk-cnt.h"
35 #include "detect-engine-uint.h"
36 
37 #include "util-byte.h"
38 #include "util-debug.h"
39 
40 static int DetectSCTPChunkCntMatch(
41  DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *);
42 static int DetectSCTPChunkCntSetup(DetectEngineCtx *, Signature *, const char *);
44 
45 static int PrefilterSetupSCTPChunkCnt(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
46 static bool PrefilterSCTPChunkCntIsPrefilterable(const Signature *s);
47 
49 {
50  sigmatch_table[DETECT_SCTP_CHUNK_CNT].name = "sctp.chunk_cnt";
51  sigmatch_table[DETECT_SCTP_CHUNK_CNT].desc = "match on the SCTP chunk count";
52  sigmatch_table[DETECT_SCTP_CHUNK_CNT].url = "/rules/sctp-keywords.html#sctp-chunk-cnt";
53  sigmatch_table[DETECT_SCTP_CHUNK_CNT].Match = DetectSCTPChunkCntMatch;
54  sigmatch_table[DETECT_SCTP_CHUNK_CNT].Setup = DetectSCTPChunkCntSetup;
57  sigmatch_table[DETECT_SCTP_CHUNK_CNT].SupportsPrefilter = PrefilterSCTPChunkCntIsPrefilterable;
58  sigmatch_table[DETECT_SCTP_CHUNK_CNT].SetupPrefilter = PrefilterSetupSCTPChunkCnt;
59 }
60 
61 static int DetectSCTPChunkCntMatch(
62  DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const SigMatchCtx *ctx)
63 {
65 
66  if (!PacketIsSCTP(p)) {
67  return 0;
68  }
69 
70  uint8_t val = p->l4.vars.sctp.chunk_cnt;
71  const DetectU8Data *data = (const DetectU8Data *)ctx;
72  return DetectU8Match(val, data);
73 }
74 
75 static int DetectSCTPChunkCntSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
76 {
78  if (data == NULL)
79  return -1;
80 
82  DETECT_SM_LIST_MATCH) == NULL) {
84  return -1;
85  }
87 
88  return 0;
89 }
90 
92 {
93  DetectU8Data *data = (DetectU8Data *)ptr;
94  SCDetectU8Free(data);
95 }
96 
97 static void PrefilterPacketSCTPChunkCntMatch(
98  DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
99 {
101 
102  if (PacketIsSCTP(p)) {
103  uint8_t val = p->l4.vars.sctp.chunk_cnt;
104  const PrefilterPacketU8HashCtx *h = pectx;
105  const SigsArray *sa = h->array[val];
106  if (sa) {
107  PrefilterAddSids(&det_ctx->pmq, sa->sigs, sa->cnt);
108  }
109  }
110 }
111 
112 static int PrefilterSetupSCTPChunkCnt(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
113 {
116  PrefilterPacketSCTPChunkCntMatch);
117 }
118 
119 static bool PrefilterSCTPChunkCntIsPrefilterable(const Signature *s)
120 {
121  return PrefilterIsPrefilterableById(s, DETECT_SCTP_CHUNK_CNT);
122 }
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1512
SIG_MASK_REQUIRE_REAL_PKT
#define SIG_MASK_REQUIRE_REAL_PKT
Definition: detect.h:316
SigTableElmt_::desc
const char * desc
Definition: detect.h:1511
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1496
SigTableElmt_::name
const char * name
Definition: detect.h:1509
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1363
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1679
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1500
PrefilterPacketU8HashCtx_::array
SigsArray * array[256]
Definition: detect-engine-prefilter-common.h:53
SIGMATCH_INFO_UINT8
#define SIGMATCH_INFO_UINT8
Definition: detect-engine-register.h:345
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1399
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:973
SCTPVars_::chunk_cnt
uint8_t chunk_cnt
Definition: decode-sctp.h:80
p
Packet * p
Definition: fuzz_iprep.c:21
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1491
DetectSCTPChunkCntFree
void DetectSCTPChunkCntFree(DetectEngineCtx *, void *)
Definition: detect-sctp-chunk-cnt.c:91
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1494
DetectU8Parse
DetectUintData_u8 * DetectU8Parse(const char *u8str)
This function is used to parse u8 options passed via some u8 keyword.
Definition: detect-engine-uint.c:85
decode.h
util-debug.h
PrefilterSetupPacketHeaderU8Hash
int PrefilterSetupPacketHeaderU8Hash(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type, SignatureMask mask, void(*Set)(PrefilterPacketHeaderValue *v, void *), bool(*Compare)(PrefilterPacketHeaderValue v, void *), void(*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
Definition: detect-engine-prefilter-common.c:462
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1291
SCSigMatchAppendSMToList
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:387
detect.h
DetectU8Data
DetectUintData_u8 DetectU8Data
Definition: detect-engine-uint.h:43
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:117
PrefilterPacketU8Set
void PrefilterPacketU8Set(PrefilterPacketHeaderValue *v, void *smctx)
Definition: detect-engine-uint.c:90
detect-sctp-chunk-cnt.h
Signature_::flags
uint32_t flags
Definition: detect.h:676
Packet_
Definition: decode.h:515
detect-engine-build.h
Packet_::l4
struct PacketL4 l4
Definition: decode.h:615
DETECT_SCTP_CHUNK_CNT
@ DETECT_SCTP_CHUNK_CNT
Definition: detect-engine-register.h:56
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1471
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:351
PacketL4::L4Vars::sctp
SCTPVars sctp
Definition: decode.h:494
DetectU8Match
int DetectU8Match(const uint8_t parg, const DetectUintData_u8 *du8)
Definition: detect-engine-uint.c:71
SigsArray_::sigs
SigIntId * sigs
Definition: detect-engine-prefilter-common.h:47
suricata-common.h
SigsArray_
Definition: detect-engine-prefilter-common.h:46
SigsArray_::cnt
uint32_t cnt
Definition: detect-engine-prefilter-common.h:48
str
#define str(s)
Definition: suricata-common.h:316
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1493
detect-parse.h
Signature_
Signature container.
Definition: detect.h:675
DetectSCTPChunkCntRegister
void DetectSCTPChunkCntRegister(void)
Definition: detect-sctp-chunk-cnt.c:48
PrefilterPacketU8Compare
bool PrefilterPacketU8Compare(PrefilterPacketHeaderValue v, void *smctx)
Definition: detect-engine-uint.c:98
PrefilterPacketU8HashCtx_
Definition: detect-engine-prefilter-common.h:52
detect-engine-prefilter-common.h
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
PacketL4::vars
union PacketL4::L4Vars vars
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:253