suricata
detect-sctp-chunk-type.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_type keyword support
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-type.h"
35 #include "detect-engine-uint.h"
36 
37 #include "util-byte.h"
38 #include "util-debug.h"
39 
40 static int DetectSCTPChunkTypeMatch(
41  DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *);
42 static int DetectSCTPChunkTypeSetup(DetectEngineCtx *, Signature *, const char *);
44 
45 static int PrefilterSetupSCTPChunkType(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
46 static bool PrefilterSCTPChunkTypeIsPrefilterable(const Signature *s);
47 
49 {
50  sigmatch_table[DETECT_SCTP_CHUNK_TYPE].name = "sctp.chunk_type";
51  sigmatch_table[DETECT_SCTP_CHUNK_TYPE].desc = "match on any SCTP chunk type in the packet";
52  sigmatch_table[DETECT_SCTP_CHUNK_TYPE].url = "/rules/sctp-keywords.html#sctp-chunk-type";
53  sigmatch_table[DETECT_SCTP_CHUNK_TYPE].Match = DetectSCTPChunkTypeMatch;
54  sigmatch_table[DETECT_SCTP_CHUNK_TYPE].Setup = DetectSCTPChunkTypeSetup;
59  PrefilterSCTPChunkTypeIsPrefilterable;
60  sigmatch_table[DETECT_SCTP_CHUNK_TYPE].SetupPrefilter = PrefilterSetupSCTPChunkType;
61 }
62 
63 static int DetectSCTPChunkTypeMatch(
64  DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const SigMatchCtx *ctx)
65 {
67 
68  if (!PacketIsSCTP(p)) {
69  return 0;
70  }
71 
72  const DetectU8Data *data = (const DetectU8Data *)ctx;
73  const uint8_t cnt = p->l4.vars.sctp.tracked_chunk_cnt;
74  for (uint8_t i = 0; i < cnt; i++) {
75  if (DetectU8Match(p->l4.vars.sctp.chunk_types[i], data)) {
76  return 1;
77  }
78  }
79  return 0;
80 }
81 
82 static int DetectSCTPChunkTypeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
83 {
84  DetectU8Data *data = SCSctpDetectChunkTypeParse(str);
85  if (data == NULL)
86  return -1;
87 
89  DETECT_SM_LIST_MATCH) == NULL) {
91  return -1;
92  }
94 
95  return 0;
96 }
97 
99 {
100  DetectU8Data *data = (DetectU8Data *)ptr;
101  SCDetectU8Free(data);
102 }
103 
104 static void PrefilterPacketSCTPChunkTypeMatch(
105  DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
106 {
108 
109  if (!PacketIsSCTP(p)) {
110  return;
111  }
112 
113  const PrefilterPacketU8HashCtx *h = pectx;
114  const uint8_t cnt = p->l4.vars.sctp.tracked_chunk_cnt;
115  /* bitmap to dedup repeated chunk types within a single packet */
116  uint32_t seen[8] = { 0 };
117  for (uint8_t i = 0; i < cnt; i++) {
118  const uint8_t val = p->l4.vars.sctp.chunk_types[i];
119  if (seen[val >> 5] & (1U << (val & 0x1F))) {
120  continue;
121  }
122  seen[val >> 5] |= 1U << (val & 0x1F);
123  const SigsArray *sa = h->array[val];
124  if (sa) {
125  PrefilterAddSids(&det_ctx->pmq, sa->sigs, sa->cnt);
126  }
127  }
128 }
129 
130 static int PrefilterSetupSCTPChunkType(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
131 {
134  PrefilterPacketSCTPChunkTypeMatch);
135 }
136 
137 static bool PrefilterSCTPChunkTypeIsPrefilterable(const Signature *s)
138 {
139  return PrefilterIsPrefilterableById(s, DETECT_SCTP_CHUNK_TYPE);
140 }
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1512
SIGMATCH_INFO_MULTI_UINT
#define SIGMATCH_INFO_MULTI_UINT
Definition: detect-engine-register.h:353
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
SCTPVars_::chunk_types
uint8_t chunk_types[SCTP_MAX_TRACKED_CHUNKS]
Definition: decode-sctp.h:82
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
DetectSCTPChunkTypeRegister
void DetectSCTPChunkTypeRegister(void)
Definition: detect-sctp-chunk-type.c:48
detect-sctp-chunk-type.h
p
Packet * p
Definition: fuzz_iprep.c:21
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1491
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1494
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
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
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
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
DetectU8Match
int DetectU8Match(const uint8_t parg, const DetectUintData_u8 *du8)
Definition: detect-engine-uint.c:71
SCTPVars_::tracked_chunk_cnt
uint8_t tracked_chunk_cnt
Definition: decode-sctp.h:81
suricata-common.h
SigsArray_
Definition: detect-engine-prefilter-common.h:46
DetectSCTPChunkTypeFree
void DetectSCTPChunkTypeFree(DetectEngineCtx *, void *)
Definition: detect-sctp-chunk-type.c:98
SIGMATCH_INFO_ENUM_UINT
#define SIGMATCH_INFO_ENUM_UINT
Definition: detect-engine-register.h:355
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
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
DETECT_SCTP_CHUNK_TYPE
@ DETECT_SCTP_CHUNK_TYPE
Definition: detect-engine-register.h:55
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:253