suricata
detect-igmp-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 igmp.type keyword support
22  */
23 
24 #include "suricata-common.h"
25 #include "decode.h"
26 
27 #include "detect.h"
28 #include "detect-parse.h"
30 #include "detect-engine-build.h"
31 
32 #include "detect-igmp-type.h"
33 #include "detect-engine-uint.h"
34 
35 #include "util-byte.h"
36 #include "util-unittest-helper.h"
37 #include "util-debug.h"
38 
39 static int DetectIGMPTypeMatch(
40  DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *);
41 static int DetectIGMPTypeSetup(DetectEngineCtx *, Signature *, const char *);
42 void DetectIGMPTypeFree(DetectEngineCtx *, void *);
43 
44 static int PrefilterSetupIGMPType(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
45 static bool PrefilterIGMPTypeIsPrefilterable(const Signature *s);
46 
47 /**
48  * \brief Registration function for igmp.type keyword
49  */
51 {
52  sigmatch_table[DETECT_IGMP_TYPE].name = "igmp.type";
53  sigmatch_table[DETECT_IGMP_TYPE].desc = "match on a specific IGMP type";
54  sigmatch_table[DETECT_IGMP_TYPE].url = "/rules/header-keywords.html#igmp-type";
55  sigmatch_table[DETECT_IGMP_TYPE].Match = DetectIGMPTypeMatch;
56  sigmatch_table[DETECT_IGMP_TYPE].Setup = DetectIGMPTypeSetup;
59  sigmatch_table[DETECT_IGMP_TYPE].SupportsPrefilter = PrefilterIGMPTypeIsPrefilterable;
60  sigmatch_table[DETECT_IGMP_TYPE].SetupPrefilter = PrefilterSetupIGMPType;
61 }
62 
63 /**
64  * \brief This function is used to match igmp.type rule option
65  *
66  * \param t pointer to thread vars
67  * \param det_ctx pointer to the pattern matcher thread
68  * \param p pointer to the current packet
69  * \param m pointer to the sigmatch that we will cast into DetectU8Data
70  *
71  * \retval 0 no match
72  * \retval 1 match
73  */
74 static int DetectIGMPTypeMatch(
75  DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const SigMatchCtx *ctx)
76 {
78 
79  if (!PacketIsIGMP(p)) {
80  /* Packet not IGMP */
81  return 0;
82  }
83 
84  const IGMPHdr *igmph = PacketGetIGMP(p);
85  uint8_t type = igmph->type;
86  const DetectU8Data *itd = (const DetectU8Data *)ctx;
87  return DetectU8Match(type, itd);
88 }
89 
90 /**
91  * \brief this function is used to add the parsed igmp.type data into the current signature
92  *
93  * \param de_ctx pointer to the Detection Engine Context
94  * \param s pointer to the Current Signature
95  * \param str pointer to the user provided igmp.type options
96  *
97  * \retval 0 on Success
98  * \retval -1 on Failure
99  */
100 static int DetectIGMPTypeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
101 {
102  if (!(DetectProtoContainsProto(&s->init_data->proto, IPPROTO_IGMP)))
103  return -1;
104 
106  if (itd == NULL)
107  return -1;
108 
112  return -1;
113  }
116 
117  return 0;
118 }
119 
120 /**
121  * \brief this function will free memory associated with DetectU8Data
122  *
123  * \param ptr pointer to DetectU8Data
124  */
126 {
127  DetectU8Data *itd = (DetectU8Data *)ptr;
128  SCDetectU8Free(itd);
129 }
130 
131 /* prefilter code
132  *
133  * Prefilter uses the U8Hash logic, where we setup a 256 entry array
134  * for each IGMP type. Each array element has the list of signatures
135  * that need to be inspected. */
136 
137 static void PrefilterPacketIGMPTypeMatch(
138  DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
139 {
141 
142  if (PacketIsIGMP(p)) {
143  const IGMPHdr *igmph = PacketGetIGMP(p);
144  uint8_t type = igmph->type;
145  const PrefilterPacketU8HashCtx *h = pectx;
146  const SigsArray *sa = h->array[type];
147  if (sa) {
148  PrefilterAddSids(&det_ctx->pmq, sa->sigs, sa->cnt);
149  }
150  }
151 }
152 
153 static int PrefilterSetupIGMPType(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
154 {
157  PrefilterPacketIGMPTypeMatch);
158 }
159 
160 static bool PrefilterIGMPTypeIsPrefilterable(const Signature *s)
161 {
162  return PrefilterIsPrefilterableById(s, DETECT_IGMP_TYPE);
163 }
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1545
SIG_MASK_REQUIRE_REAL_PKT
#define SIG_MASK_REQUIRE_REAL_PKT
Definition: detect.h:320
SigTableElmt_::desc
const char * desc
Definition: detect.h:1544
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1529
SigTableElmt_::name
const char * name
Definition: detect.h:1542
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1364
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1730
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1533
PrefilterPacketU8HashCtx_::array
SigsArray * array[256]
Definition: detect-engine-prefilter-common.h:53
type
uint8_t type
Definition: decode-sctp.h:0
SIGMATCH_INFO_UINT8
#define SIGMATCH_INFO_UINT8
Definition: detect-engine-register.h:342
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1429
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
detect-igmp-type.h
p
Packet * p
Definition: fuzz_dataset.c:30
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
IGMPHdr_::type
uint8_t type
Definition: decode-igmp.h:26
util-unittest-helper.h
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1527
DetectU8Parse
DetectUintData_u8 * DetectU8Parse(const char *u8str)
This function is used to parse u8 options passed via some u8 keyword.
Definition: detect-engine-uint.c:85
DETECT_IGMP_TYPE
@ DETECT_IGMP_TYPE
Definition: detect-engine-register.h:53
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:1316
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:424
detect.h
SignatureInitData_::proto
DetectProto proto
Definition: detect.h:655
DetectU8Data
DetectUintData_u8 DetectU8Data
Definition: detect-engine-uint.h:43
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:118
PrefilterPacketU8Set
void PrefilterPacketU8Set(PrefilterPacketHeaderValue *v, void *smctx)
Definition: detect-engine-uint.c:90
Signature_::flags
uint32_t flags
Definition: detect.h:693
Packet_
Definition: decode.h:516
detect-engine-build.h
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:775
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1504
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:355
DetectProto_::flags
uint8_t flags
Definition: detect-engine-proto.h:40
DETECT_PROTO_IPV4
#define DETECT_PROTO_IPV4
Definition: detect-engine-proto.h:31
DetectU8Match
int DetectU8Match(const uint8_t parg, const DetectUintData_u8 *du8)
Definition: detect-engine-uint.c:71
SigsArray_::sigs
SigIntId * sigs
Definition: detect-engine-prefilter-common.h:47
suricata-common.h
SigsArray_
Definition: detect-engine-prefilter-common.h:46
IGMPHdr_
Definition: decode-igmp.h:25
SigsArray_::cnt
uint32_t cnt
Definition: detect-engine-prefilter-common.h:48
DetectIGMPTypeRegister
void DetectIGMPTypeRegister(void)
Registration function for igmp.type keyword.
Definition: detect-igmp-type.c:50
str
#define str(s)
Definition: suricata-common.h:313
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1526
DetectIGMPTypeFree
void DetectIGMPTypeFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectU8Data
Definition: detect-igmp-type.c:125
detect-parse.h
Signature_
Signature container.
Definition: detect.h:692
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
DetectProtoContainsProto
int DetectProtoContainsProto(const DetectProto *dp, int proto)
see if a DetectProto contains a certain proto
Definition: detect-engine-proto.c:114
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:257