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  uint16_t val = p->l4.vars.sctp.chunk_cnt;
71  const DetectU16Data *data = (const DetectU16Data *)ctx;
72  return DetectU16Match(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  DetectU16Data *data = (DetectU16Data *)ptr;
94  SCDetectU16Free(data);
95 }
96 
97 static void PrefilterPacketSCTPChunkCntMatch(
98  DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
99 {
101 
102  if (PacketIsSCTP(p)) {
103  uint16_t val = p->l4.vars.sctp.chunk_cnt;
104  const PrefilterPacketHeaderCtx *ctx = pectx;
105  DetectU16Data du16;
106  du16.mode = ctx->v1.u8[0];
107  du16.arg1 = ctx->v1.u16[1];
108  du16.arg2 = ctx->v1.u16[2];
109  if (DetectU16Match(val, &du16)) {
110  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
111  }
112  }
113 }
114 
115 static int PrefilterSetupSCTPChunkCnt(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
116 {
118  PrefilterPacketU16Set, PrefilterPacketU16Compare, PrefilterPacketSCTPChunkCntMatch);
119 }
120 
121 static bool PrefilterSCTPChunkCntIsPrefilterable(const Signature *s)
122 {
123  return PrefilterIsPrefilterableById(s, DETECT_SCTP_CHUNK_CNT);
124 }
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
PrefilterPacketU16Set
void PrefilterPacketU16Set(PrefilterPacketHeaderValue *v, void *smctx)
Definition: detect-engine-uint.c:124
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
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1399
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:973
SCTPVars_::chunk_cnt
uint16_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
PrefilterPacketHeaderCtx_
Definition: detect-engine-prefilter-common.h:35
decode.h
util-debug.h
SIGMATCH_INFO_UINT16
#define SIGMATCH_INFO_UINT16
Definition: detect-engine-register.h:342
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
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:117
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
PrefilterSetupPacketHeader
int PrefilterSetupPacketHeader(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:470
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
DetectU16Match
int DetectU16Match(const uint16_t parg, const DetectUintData_u16 *du16)
Definition: detect-engine-uint.c:105
DetectU16Parse
DetectUintData_u16 * DetectU16Parse(const char *u16str)
This function is used to parse u16 options passed via some u16 keyword.
Definition: detect-engine-uint.c:119
PrefilterPacketU16Compare
bool PrefilterPacketU16Compare(PrefilterPacketHeaderValue v, void *smctx)
Definition: detect-engine-uint.c:132
suricata-common.h
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
detect-engine-prefilter-common.h
DetectU16Data
DetectUintData_u16 DetectU16Data
Definition: detect-engine-uint.h:42
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