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.h"
37 #include "util-unittest-helper.h"
38 #include "util-debug.h"
39 
40 static int DetectIGMPTypeMatch(
41  DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *);
42 static int DetectIGMPTypeSetup(DetectEngineCtx *, Signature *, const char *);
43 void DetectIGMPTypeFree(DetectEngineCtx *, void *);
44 
45 static int PrefilterSetupIGMPType(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
46 static bool PrefilterIGMPTypeIsPrefilterable(const Signature *s);
47 
48 /**
49  * \brief Registration function for igmp.type keyword
50  */
52 {
53  sigmatch_table[DETECT_IGMP_TYPE].name = "igmp.type";
54  sigmatch_table[DETECT_IGMP_TYPE].desc = "match on a specific IGMP type";
55  sigmatch_table[DETECT_IGMP_TYPE].url = "/rules/header-keywords.html#igmp.type";
56  sigmatch_table[DETECT_IGMP_TYPE].Match = DetectIGMPTypeMatch;
57  sigmatch_table[DETECT_IGMP_TYPE].Setup = DetectIGMPTypeSetup;
60  sigmatch_table[DETECT_IGMP_TYPE].SupportsPrefilter = PrefilterIGMPTypeIsPrefilterable;
61  sigmatch_table[DETECT_IGMP_TYPE].SetupPrefilter = PrefilterSetupIGMPType;
62 }
63 
64 /**
65  * \brief This function is used to match igmp.type rule option
66  *
67  * \param t pointer to thread vars
68  * \param det_ctx pointer to the pattern matcher thread
69  * \param p pointer to the current packet
70  * \param m pointer to the sigmatch that we will cast into DetectU8Data
71  *
72  * \retval 0 no match
73  * \retval 1 match
74  */
75 static int DetectIGMPTypeMatch(
76  DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const SigMatchCtx *ctx)
77 {
79 
80  if (!PacketIsIGMP(p)) {
81  /* Packet not IGMP */
82  return 0;
83  }
84 
85  const IGMPHdr *igmph = PacketGetIGMP(p);
86  uint8_t type = igmph->type;
87  const DetectU8Data *itd = (const DetectU8Data *)ctx;
88  return DetectU8Match(type, itd);
89 }
90 
91 /**
92  * \brief this function is used to add the parsed igmp.type data into the current signature
93  *
94  * \param de_ctx pointer to the Detection Engine Context
95  * \param s pointer to the Current Signature
96  * \param str pointer to the user provided igmp.type options
97  *
98  * \retval 0 on Success
99  * \retval -1 on Failure
100  */
101 static int DetectIGMPTypeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
102 {
103  if (!(DetectProtoContainsProto(&s->proto, IPPROTO_IGMP)))
104  return -1;
105 
107  if (itd == NULL)
108  return -1;
109 
113  return -1;
114  }
117 
118  return 0;
119 }
120 
121 /**
122  * \brief this function will free memory associated with DetectU8Data
123  *
124  * \param ptr pointer to DetectU8Data
125  */
127 {
128  DetectU8Data *itd = (DetectU8Data *)ptr;
129  SCDetectU8Free(itd);
130 }
131 
132 /* prefilter code
133  *
134  * Prefilter uses the U8Hash logic, where we setup a 256 entry array
135  * for each IGMP type. Each array element has the list of signatures
136  * that need to be inspected. */
137 
138 static void PrefilterPacketIGMPTypeMatch(
139  DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
140 {
142 
143  if (PacketIsIGMP(p)) {
144  const IGMPHdr *igmph = PacketGetIGMP(p);
145  uint8_t type = igmph->type;
146  const PrefilterPacketU8HashCtx *h = pectx;
147  const SigsArray *sa = h->array[type];
148  if (sa) {
149  PrefilterAddSids(&det_ctx->pmq, sa->sigs, sa->cnt);
150  }
151  }
152 }
153 
154 static int PrefilterSetupIGMPType(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
155 {
158  PrefilterPacketIGMPTypeMatch);
159 }
160 
161 static bool PrefilterIGMPTypeIsPrefilterable(const Signature *s)
162 {
163  return PrefilterIsPrefilterableById(s, DETECT_IGMP_TYPE);
164 }
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1461
SIG_MASK_REQUIRE_REAL_PKT
#define SIG_MASK_REQUIRE_REAL_PKT
Definition: detect.h:315
SigTableElmt_::desc
const char * desc
Definition: detect.h:1460
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1445
SigTableElmt_::name
const char * name
Definition: detect.h:1458
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1348
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1628
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1449
PrefilterPacketU8HashCtx_::array
SigsArray * array[256]
Definition: detect-engine-prefilter-common.h:53
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1348
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
detect-igmp-type.h
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1440
util-unittest.h
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:1443
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:19
DetectEngineThreadCtx_
Definition: detect.h:1245
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:669
Packet_
Definition: decode.h:505
detect-engine-build.h
type
uint16_t type
Definition: decode-vlan.c:106
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1420
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:350
DetectProto_::flags
uint8_t flags
Definition: detect-engine-proto.h:37
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
Signature_::proto
DetectProto proto
Definition: detect.h:687
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:51
str
#define str(s)
Definition: suricata-common.h:308
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1442
DetectIGMPTypeFree
void DetectIGMPTypeFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectU8Data
Definition: detect-igmp-type.c:126
detect-parse.h
Signature_
Signature container.
Definition: detect.h:668
SIGMATCH_INFO_UINT8
#define SIGMATCH_INFO_UINT8
Definition: detect.h:1687
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:102
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