suricata
detect-vlan.c
Go to the documentation of this file.
1 /* Copyright (C) 2024 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 #include "detect-vlan.h"
19 #include "detect-engine-uint.h"
20 #include "detect-parse.h"
21 #include "rust.h"
22 
23 static void DetectVlanIdFree(DetectEngineCtx *de_ctx, void *ptr)
24 {
25  SCDetectVlanIdFree(ptr);
26 }
27 
28 static int DetectVlanIdSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
29 {
30  SigMatchCtx *vdata = SCDetectVlanIdParse(rawstr);
31  if (vdata == NULL) {
32  SCLogError("vlan id invalid %s", rawstr);
33  return -1;
34  }
35 
37  DetectVlanIdFree(de_ctx, vdata);
38  return -1;
39  }
41 
42  return 0;
43 }
44 
45 static void PrefilterPacketVlanIdMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
46 {
47  const PrefilterPacketHeaderCtx *ctx = pectx;
48 
49  DetectVlanIdDataPrefilter vdata;
50  vdata.du16.mode = ctx->v1.u8[0];
51  vdata.layer = ctx->v1.u8[1];
52  vdata.du16.arg1 = ctx->v1.u16[2];
53  vdata.du16.arg2 = ctx->v1.u16[3];
54 
55  if (SCDetectVlanIdPrefilterMatch(p->vlan_idx, p->vlan_id, &vdata)) {
56  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
57  }
58 }
59 
60 static void PrefilterPacketVlanIdSet(PrefilterPacketHeaderValue *v, void *smctx)
61 {
62  const DetectVlanIdDataPrefilter a = SCDetectVlanIdPrefilter(smctx);
63  v->u8[0] = a.du16.mode;
64  v->u8[1] = a.layer;
65  v->u16[2] = a.du16.arg1;
66  v->u16[3] = a.du16.arg2;
67 }
68 
69 static bool PrefilterPacketVlanIdCompare(PrefilterPacketHeaderValue v, void *smctx)
70 {
71  const DetectVlanIdDataPrefilter a = SCDetectVlanIdPrefilter(smctx);
72  if (v.u8[0] == a.du16.mode && v.u8[1] == a.layer && v.u16[2] == a.du16.arg1 &&
73  v.u16[3] == a.du16.arg2)
74  return true;
75  return false;
76 }
77 
78 static int PrefilterSetupVlanId(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
79 {
81  PrefilterPacketVlanIdSet, PrefilterPacketVlanIdCompare, PrefilterPacketVlanIdMatch);
82 }
83 
84 static bool PrefilterVlanIdIsPrefilterable(const Signature *s)
85 {
86  const SigMatch *sm;
87  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH]; sm != NULL; sm = sm->next) {
88  if (sm->type == DETECT_VLAN_ID) {
89  return SCDetectVlanIdPrefilterable(sm->ctx);
90  }
91  }
92  return false;
93 }
94 
95 static int DetectVlanIdMatch(
96  DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const SigMatchCtx *ctx)
97 {
98  return SCDetectVlanIdMatch(p->vlan_idx, p->vlan_id, ctx);
99 }
101 {
102  sigmatch_table[DETECT_VLAN_ID].name = "vlan.id";
103  sigmatch_table[DETECT_VLAN_ID].desc = "match vlan id";
104  sigmatch_table[DETECT_VLAN_ID].url = "/rules/vlan-keywords.html#vlan-id";
105  sigmatch_table[DETECT_VLAN_ID].Match = DetectVlanIdMatch;
106  sigmatch_table[DETECT_VLAN_ID].Setup = DetectVlanIdSetup;
107  sigmatch_table[DETECT_VLAN_ID].Free = DetectVlanIdFree;
109  sigmatch_table[DETECT_VLAN_ID].SupportsPrefilter = PrefilterVlanIdIsPrefilterable;
110  sigmatch_table[DETECT_VLAN_ID].SetupPrefilter = PrefilterSetupVlanId;
111 }
112 
113 static int DetectVlanLayersMatch(
114  DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const SigMatchCtx *ctx)
115 {
116  uint8_t nb = p->vlan_idx;
117 
118  const DetectU8Data *du8 = (const DetectU8Data *)ctx;
119  return DetectU8Match(nb, du8);
120 }
121 
122 static void DetectVlanLayersFree(DetectEngineCtx *de_ctx, void *ptr)
123 {
124  SCDetectU8Free(ptr);
125 }
126 
127 static int DetectVlanLayersSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
128 {
129  DetectU8Data *du8 = DetectU8Parse(rawstr);
130 
131  if (du8 == NULL) {
132  SCLogError("vlan layers invalid %s", rawstr);
133  return -1;
134  }
135 
136  if (du8->arg1 > VLAN_MAX_LAYERS || du8->arg2 > VLAN_MAX_LAYERS) {
137  SCLogError("number of layers out of range %s", rawstr);
138  DetectVlanLayersFree(de_ctx, du8);
139  return -1;
140  }
141 
144  DetectVlanLayersFree(de_ctx, du8);
145  return -1;
146  }
148 
149  return 0;
150 }
151 
152 static void PrefilterPacketVlanLayersMatch(
153  DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
154 {
155  const PrefilterPacketHeaderCtx *ctx = pectx;
156 
157  DetectU8Data du8;
158  du8.mode = ctx->v1.u8[0];
159  du8.arg1 = ctx->v1.u8[1];
160  du8.arg2 = ctx->v1.u8[2];
161 
162  if (DetectVlanLayersMatch(det_ctx, p, NULL, (const SigMatchCtx *)&du8)) {
163  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
164  }
165 }
166 
167 static int PrefilterSetupVlanLayers(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
168 {
170  PrefilterPacketU8Set, PrefilterPacketU8Compare, PrefilterPacketVlanLayersMatch);
171 }
172 
173 static bool PrefilterVlanLayersIsPrefilterable(const Signature *s)
174 {
175  return PrefilterIsPrefilterableById(s, DETECT_VLAN_LAYERS);
176 }
177 
179 {
180  sigmatch_table[DETECT_VLAN_LAYERS].name = "vlan.layers";
181  sigmatch_table[DETECT_VLAN_LAYERS].desc = "match number of vlan layers";
182  sigmatch_table[DETECT_VLAN_LAYERS].url = "/rules/vlan-keywords.html#vlan-layers";
183  sigmatch_table[DETECT_VLAN_LAYERS].Match = DetectVlanLayersMatch;
184  sigmatch_table[DETECT_VLAN_LAYERS].Setup = DetectVlanLayersSetup;
185  sigmatch_table[DETECT_VLAN_LAYERS].Free = DetectVlanLayersFree;
186  sigmatch_table[DETECT_VLAN_LAYERS].SupportsPrefilter = PrefilterVlanLayersIsPrefilterable;
187  sigmatch_table[DETECT_VLAN_LAYERS].SetupPrefilter = PrefilterSetupVlanLayers;
189 }
SIGMATCH_INFO_UINT16
#define SIGMATCH_INFO_UINT16
Definition: detect.h:1689
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1461
detect-vlan.h
SIG_MASK_REQUIRE_REAL_PKT
#define SIG_MASK_REQUIRE_REAL_PKT
Definition: detect.h:316
SignatureInitData_::smlists
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition: detect.h:642
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
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1628
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1449
Packet_::vlan_idx
uint8_t vlan_idx
Definition: decode.h:529
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1348
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
DetectVlanLayersRegister
void DetectVlanLayersRegister(void)
Definition: detect-vlan.c:178
PrefilterPacketHeaderValue::u8
uint8_t u8[16]
Definition: detect-engine-prefilter-common.h:24
rust.h
DETECT_VLAN_LAYERS
@ DETECT_VLAN_LAYERS
Definition: detect-engine-register.h:326
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1440
PrefilterPacketHeaderValue::u16
uint16_t u16[8]
Definition: detect-engine-prefilter-common.h:25
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
PrefilterPacketHeaderCtx_
Definition: detect-engine-prefilter-common.h:35
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1245
SIGMATCH_INFO_MULTI_UINT
#define SIGMATCH_INFO_MULTI_UINT
Definition: detect.h:1695
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:388
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:360
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
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:359
Signature_::flags
uint32_t flags
Definition: detect.h:669
Packet_
Definition: decode.h:501
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:747
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
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:351
DetectU8Match
int DetectU8Match(const uint8_t parg, const DetectUintData_u8 *du8)
Definition: detect-engine-uint.c:71
SigMatch_::type
uint16_t type
Definition: detect.h:357
VLAN_MAX_LAYERS
#define VLAN_MAX_LAYERS
Definition: decode-vlan.h:51
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:271
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1442
detect-parse.h
Signature_
Signature container.
Definition: detect.h:668
SigMatch_
a single match condition for a signature
Definition: detect.h:356
SIGMATCH_INFO_UINT8
#define SIGMATCH_INFO_UINT8
Definition: detect.h:1687
PrefilterPacketU8Compare
bool PrefilterPacketU8Compare(PrefilterPacketHeaderValue v, void *smctx)
Definition: detect-engine-uint.c:98
DetectVlanIdRegister
void DetectVlanIdRegister(void)
Definition: detect-vlan.c:100
PrefilterPacketHeaderValue
Definition: detect-engine-prefilter-common.h:23
Packet_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: decode.h:528
DETECT_VLAN_ID
@ DETECT_VLAN_ID
Definition: detect-engine-register.h:325
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:254