suricata
detect-sctphdr.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.hdr sticky buffer
22  *
23  * Author: Giuseppe Longo <glongo@oisf.net>
24  */
25 
26 #include "suricata-common.h"
27 
28 #include "detect.h"
29 #include "detect-engine.h"
30 #include "detect-engine-buffer.h"
31 #include "detect-engine-mpm.h"
32 #include "detect-sctphdr.h"
34 
35 static int DetectSCTPHdrSetup(DetectEngineCtx *, Signature *, const char *);
36 
37 static int g_sctphdr_buffer_id = 0;
38 
39 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
40  const DetectEngineTransforms *transforms, Packet *p, const int list_id);
41 
43 {
44  sigmatch_table[DETECT_SCTPHDR].name = "sctp.hdr";
45  sigmatch_table[DETECT_SCTPHDR].desc = "sticky buffer to match on the SCTP header";
46  sigmatch_table[DETECT_SCTPHDR].url = "/rules/sctp-keywords.html#sctp-hdr";
47  sigmatch_table[DETECT_SCTPHDR].Setup = DetectSCTPHdrSetup;
49 
50  g_sctphdr_buffer_id = DetectBufferTypeRegister("sctp.hdr");
51  BUG_ON(g_sctphdr_buffer_id < 0);
52 
54 
56 
58 }
59 
60 /**
61  * \brief setup sctp.hdr sticky buffer
62  *
63  * \param de_ctx pointer to the Detection Engine Context
64  * \param s pointer to the current Signature
65  * \param _unused unused
66  *
67  * \retval 0 on Success
68  * \retval -1 on Failure
69  */
70 static int DetectSCTPHdrSetup(DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
71 {
73  return -1;
74 
76 
77  if (SCDetectBufferSetActiveList(de_ctx, s, g_sctphdr_buffer_id) < 0)
78  return -1;
79 
80  return 0;
81 }
82 
83 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
84  const DetectEngineTransforms *transforms, Packet *p, const int list_id)
85 {
86  SCEnter();
87 
88  if (!PacketIsSCTP(p)) {
89  SCReturnPtr(NULL, "InspectionBuffer");
90  }
91 
92  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
93  if (buffer->inspect == NULL) {
94  const SCTPHdr *sctph = PacketGetSCTP(p);
95  const uint16_t hlen = p->l4.vars.sctp.hlen;
96  if (((uint8_t *)sctph + (ptrdiff_t)hlen) >
97  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p))) {
98  SCLogDebug("data out of range: %p > %p", ((uint8_t *)sctph + (ptrdiff_t)hlen),
99  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
100  SCReturnPtr(NULL, "InspectionBuffer");
101  }
102 
103  const uint32_t data_len = hlen;
104  const uint8_t *data = (const uint8_t *)sctph;
105 
107  det_ctx, list_id, buffer, data, data_len, transforms);
108  }
109 
110  SCReturnPtr(buffer, "InspectionBuffer");
111 }
PrefilterGenericMpmPktRegister
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1751
SigTableElmt_::url
const char * url
Definition: detect.h:1512
detect-engine.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect-engine-register.h:311
SigTableElmt_::desc
const char * desc
Definition: detect.h:1511
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SCInspectionBufferGet
InspectionBuffer * SCInspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:56
SigTableElmt_::name
const char * name
Definition: detect.h:1509
DetectEngineTransforms
Definition: detect.h:391
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1500
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
DetectPktMpmRegister
void DetectPktMpmRegister(const char *name, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id), InspectionBufferGetPktDataPtr GetData)
register a MPM engine
Definition: detect-engine-mpm.c:598
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:973
SCInspectionBufferSetupAndApplyTransforms
void SCInspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
Definition: detect-engine-inspect-buffer.c:197
detect-sctphdr.h
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
p
Packet * p
Definition: fuzz_iprep.c:21
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1491
detect-engine-prefilter.h
DetectBufferTypeSupportsPacket
void DetectBufferTypeSupportsPacket(const char *name)
Definition: detect-engine.c:1354
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1291
DetectPktInspectEngineRegister
void DetectPktInspectEngineRegister(const char *name, InspectionBufferGetPktDataPtr GetPktData, InspectionBufferPktInspectFunc Callback)
register inspect engine at start up time
Definition: detect-engine.c:156
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:210
detect-engine-mpm.h
detect.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:325
Signature_::flags
uint32_t flags
Definition: detect.h:676
Packet_
Definition: decode.h:515
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:209
Packet_::l4
struct PacketL4 l4
Definition: decode.h:615
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:300
DetectSCTPHdrRegister
void DetectSCTPHdrRegister(void)
Definition: detect-sctphdr.c:42
DETECT_SCTPHDR
@ DETECT_SCTPHDR
Definition: detect-engine-register.h:54
PacketL4::L4Vars::sctp
SCTPVars sctp
Definition: decode.h:494
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1320
suricata-common.h
detect-engine-buffer.h
Signature_::proto
DetectProto * proto
Definition: detect.h:694
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect-engine-inspect-buffer.h:35
Signature_
Signature container.
Definition: detect.h:675
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect-engine-register.h:333
SCTPVars_::hlen
uint16_t hlen
Definition: decode-sctp.h:79
IPPROTO_SCTP
#define IPPROTO_SCTP
Definition: decode.h:1272
DetectEngineInspectPktBufferGeneric
int DetectEngineInspectPktBufferGeneric(DetectEngineThreadCtx *det_ctx, const DetectEnginePktInspectionEngine *engine, const Signature *s, Packet *p, uint8_t *_alert_flags)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2303
PacketL4::vars
union PacketL4::L4Vars vars
DetectProtoContainsProto
int DetectProtoContainsProto(const DetectProto *dp, int proto)
see if a DetectProto contains a certain proto
Definition: detect-engine-proto.c:115
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:253