suricata
detect-igmphdr.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  */
22 
23 #include "suricata-common.h"
24 
25 #include "detect.h"
26 #include "detect-engine.h"
27 #include "detect-engine-buffer.h"
28 #include "detect-engine-mpm.h"
29 #include "detect-igmphdr.h"
31 
32 /* prototypes */
33 static int DetectIGMPHdrSetup(DetectEngineCtx *, Signature *, const char *);
34 #ifdef UNITTESTS
36 #endif
37 
38 static int g_igmphdr_buffer_id = 0;
39 
40 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
41  const DetectEngineTransforms *transforms, Packet *p, const int list_id);
42 
43 /**
44  * \brief Registration function for igmp.hdr: keyword
45  */
47 {
48  sigmatch_table[DETECT_IGMPHDR].name = "igmp.hdr";
49  sigmatch_table[DETECT_IGMPHDR].desc = "sticky buffer to match on the IGMP header";
50  sigmatch_table[DETECT_IGMPHDR].url = "/rules/header-keywords.html#igmp-hdr";
51  sigmatch_table[DETECT_IGMPHDR].Setup = DetectIGMPHdrSetup;
53 #ifdef UNITTESTS
55 #endif
56 
57  g_igmphdr_buffer_id = DetectBufferTypeRegister("igmp.hdr");
58  BUG_ON(g_igmphdr_buffer_id < 0);
59 
61 
63 
65 }
66 
67 /**
68  * \brief setup igmp.hdr sticky buffer
69  *
70  * \param de_ctx pointer to the Detection Engine Context
71  * \param s pointer to the Current Signature
72  * \param _unused unused
73  *
74  * \retval 0 on Success
75  * \retval -1 on Failure
76  */
77 static int DetectIGMPHdrSetup(DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
78 {
79  if (!(DetectProtoContainsProto(&s->proto, IPPROTO_IGMP)))
80  return -1;
81 
84 
85  if (SCDetectBufferSetActiveList(de_ctx, s, g_igmphdr_buffer_id) < 0)
86  return -1;
87 
88  return 0;
89 }
90 
91 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
92  const DetectEngineTransforms *transforms, Packet *p, const int list_id)
93 {
94  SCEnter();
95 
96  if (!PacketIsIGMP(p)) {
97  SCReturnPtr(NULL, "InspectionBuffer");
98  }
99 
100  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
101  if (buffer->inspect == NULL) {
102  const IGMPHdr *igmph = PacketGetIGMP(p);
103  const uint16_t hlen = p->l4.vars.igmp.hlen;
104  if (((uint8_t *)igmph + (ptrdiff_t)hlen) >
105  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p))) {
106  SCLogDebug("data out of range: %p > %p", ((uint8_t *)igmph + (ptrdiff_t)hlen),
107  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
108  SCReturnPtr(NULL, "InspectionBuffer");
109  }
110 
111  const uint32_t data_len = hlen;
112  const uint8_t *data = (const uint8_t *)igmph;
113 
115  det_ctx, list_id, buffer, data, data_len, transforms);
116  }
117 
118  SCReturnPtr(buffer, "InspectionBuffer");
119 }
120 
121 #ifdef UNITTESTS
122 #include "tests/detect-igmphdr.c"
123 #endif
PrefilterGenericMpmPktRegister
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1720
SigTableElmt_::url
const char * url
Definition: detect.h:1461
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1675
SigTableElmt_::desc
const char * desc
Definition: detect.h:1460
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SigTableElmt_::name
const char * name
Definition: detect.h:1458
DetectEngineTransforms
Definition: detect.h:390
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1449
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:933
DetectIGMPHdrRegisterTests
void DetectIGMPHdrRegisterTests(void)
register tests
Definition: detect-igmphdr.c:42
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:56
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1440
detect-engine-prefilter.h
DetectBufferTypeSupportsPacket
void DetectBufferTypeSupportsPacket(const char *name)
Definition: detect-engine.c:1248
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:19
DetectEngineThreadCtx_
Definition: detect.h:1245
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:317
detect-igmphdr.h
Signature_::flags
uint32_t flags
Definition: detect.h:669
Packet_
Definition: decode.h:505
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:209
Packet_::l4
struct PacketL4 l4
Definition: decode.h:605
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:300
DetectProto_::flags
uint8_t flags
Definition: detect-engine-proto.h:37
DETECT_PROTO_IPV4
#define DETECT_PROTO_IPV4
Definition: detect-engine-proto.h:31
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1214
Signature_::proto
DetectProto proto
Definition: detect.h:687
suricata-common.h
IGMPHdr_
Definition: decode-igmp.h:25
detect-igmphdr.c
detect-engine-buffer.h
DETECT_IGMPHDR
@ DETECT_IGMPHDR
Definition: detect-engine-register.h:52
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect-engine-inspect-buffer.h:35
Signature_
Signature container.
Definition: detect.h:668
InspectionBufferSetupAndApplyTransforms
void InspectionBufferSetupAndApplyTransforms(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
PacketL4::L4Vars::igmp
IGMPVars igmp
Definition: decode.h:484
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1650
DetectIGMPHdrRegister
void DetectIGMPHdrRegister(void)
Registration function for igmp.hdr: keyword.
Definition: detect-igmphdr.c:46
IGMPVars_::hlen
uint16_t hlen
Definition: decode-igmp.h:33
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:2197
PacketL4::vars
union PacketL4::L4Vars vars
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1447
DetectProtoContainsProto
int DetectProtoContainsProto(const DetectProto *dp, int proto)
see if a DetectProto contains a certain proto
Definition: detect-engine-proto.c:110
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:253