suricata
detect-sctp-vtag.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.vtag 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"
31 #include "detect-engine.h"
34 #include "detect-engine-build.h"
35 #include "detect-engine-uint.h"
36 
37 #include "detect-sctp-vtag.h"
38 
39 #include "util-debug.h"
40 
41 static int DetectSCTPVtagSetup(DetectEngineCtx *, Signature *, const char *);
42 static int DetectSCTPVtagMatch(
43  DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *);
44 static void DetectSCTPVtagFree(DetectEngineCtx *, void *);
45 static int PrefilterSetupSCTPVtag(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
46 static bool PrefilterSCTPVtagIsPrefilterable(const Signature *s);
47 
48 #ifdef UNITTESTS
50 #endif
51 
53 {
54  sigmatch_table[DETECT_SCTP_VTAG].name = "sctp.vtag";
55  sigmatch_table[DETECT_SCTP_VTAG].desc = "match on the SCTP verification tag";
56  sigmatch_table[DETECT_SCTP_VTAG].url = "/rules/sctp-keywords.html#sctp-vtag";
57  sigmatch_table[DETECT_SCTP_VTAG].Match = DetectSCTPVtagMatch;
58  sigmatch_table[DETECT_SCTP_VTAG].Setup = DetectSCTPVtagSetup;
59  sigmatch_table[DETECT_SCTP_VTAG].Free = DetectSCTPVtagFree;
61  sigmatch_table[DETECT_SCTP_VTAG].SupportsPrefilter = PrefilterSCTPVtagIsPrefilterable;
62  sigmatch_table[DETECT_SCTP_VTAG].SetupPrefilter = PrefilterSetupSCTPVtag;
63 }
64 
65 static int DetectSCTPVtagMatch(
66  DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const SigMatchCtx *ctx)
67 {
68  const DetectU32Data *data = (const DetectU32Data *)ctx;
69 
71 
72  if (!(PacketIsSCTP(p))) {
73  return 0;
74  }
75 
76  return DetectU32Match(SCTP_GET_RAW_VTAG(PacketGetSCTP(p)), data);
77 }
78 
79 static int DetectSCTPVtagSetup(DetectEngineCtx *de_ctx, Signature *s, const char *optstr)
80 {
81  DetectU32Data *data = SCDetectU32Parse(optstr);
82  if (data == NULL)
83  return -1;
84 
87  DetectSCTPVtagFree(de_ctx, data);
88  return -1;
89  }
91  return 0;
92 }
93 
94 static void DetectSCTPVtagFree(DetectEngineCtx *de_ctx, void *ptr)
95 {
96  SCDetectU32Free(ptr);
97 }
98 
99 static void PrefilterPacketSCTPVtagMatch(
100  DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
101 {
102  const PrefilterPacketHeaderCtx *ctx = pectx;
103 
105  if (!PrefilterPacketHeaderExtraMatch(ctx, p))
106  return;
107 
108  if (p->proto == IPPROTO_SCTP && PacketIsSCTP(p)) {
109  DetectU32Data du32;
110  du32.mode = ctx->v1.u8[0];
111  du32.arg1 = ctx->v1.u32[1];
112  du32.arg2 = ctx->v1.u32[2];
113  if (DetectU32Match(SCTP_GET_RAW_VTAG(PacketGetSCTP(p)), &du32)) {
114  SCLogDebug("packet matches SCTP vtag %u", ctx->v1.u32[0]);
115  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
116  }
117  }
118 }
119 
120 static int PrefilterSetupSCTPVtag(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
121 {
123  PrefilterPacketU32Set, PrefilterPacketU32Compare, PrefilterPacketSCTPVtagMatch);
124 }
125 
126 static bool PrefilterSCTPVtagIsPrefilterable(const Signature *s)
127 {
128  return PrefilterIsPrefilterableById(s, DETECT_SCTP_VTAG);
129 }
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1512
Packet_::proto
uint8_t proto
Definition: decode.h:537
detect-engine.h
SIG_MASK_REQUIRE_REAL_PKT
#define SIG_MASK_REQUIRE_REAL_PKT
Definition: detect.h:316
DetectU32Match
int DetectU32Match(const uint32_t parg, const DetectUintData_u32 *du32)
Definition: detect-engine-uint.c:31
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
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
PrefilterPacketU32Set
void PrefilterPacketU32Set(PrefilterPacketHeaderValue *v, void *smctx)
Definition: detect-engine-uint.c:51
detect-sctp-vtag.h
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1399
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:973
p
Packet * p
Definition: fuzz_iprep.c:21
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1491
detect-engine-prefilter.h
DetectSCTPVtagRegisterTests
void DetectSCTPVtagRegisterTests(void)
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
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
SCTP_GET_RAW_VTAG
#define SCTP_GET_RAW_VTAG(sctph)
Definition: decode-sctp.h:94
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:117
Signature_::flags
uint32_t flags
Definition: detect.h:676
Packet_
Definition: decode.h:515
detect-engine-build.h
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
PrefilterPacketU32Compare
bool PrefilterPacketU32Compare(PrefilterPacketHeaderValue v, void *smctx)
Definition: detect-engine-uint.c:60
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
suricata-common.h
DetectSCTPVtagRegister
void DetectSCTPVtagRegister(void)
Definition: detect-sctp-vtag.c:52
DetectU32Data
DetectUintData_u32 DetectU32Data
Definition: detect-engine-uint.h:41
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1493
SIGMATCH_INFO_UINT32
#define SIGMATCH_INFO_UINT32
Definition: detect-engine-register.h:349
detect-parse.h
Signature_
Signature container.
Definition: detect.h:675
IPPROTO_SCTP
#define IPPROTO_SCTP
Definition: decode.h:1272
detect-engine-prefilter-common.h
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
DETECT_SCTP_VTAG
@ DETECT_SCTP_VTAG
Definition: detect-engine-register.h:57
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:253