suricata
detect-etherhdr.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 #include "suricata-common.h"
23 
24 #include "detect.h"
25 #include "detect-parse.h"
26 #include "detect-engine.h"
27 #include "detect-engine-buffer.h"
28 #include "detect-engine-mpm.h"
31 #include "detect-fast-pattern.h"
32 #include "detect-etherhdr.h"
33 
34 /* prototypes */
35 static int DetectEtherhdrSetup(DetectEngineCtx *, Signature *, const char *);
36 #ifdef UNITTESTS
38 #endif
39 
40 static int g_etherhdr_buffer_id = 0;
41 
42 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
43  const DetectEngineTransforms *transforms, Packet *p, const int list_id);
44 
45 /**
46  * \brief Registration function for ether.hdr: keyword
47  */
49 {
50  sigmatch_table[DETECT_ETHERHDR].name = "ether.hdr";
51  sigmatch_table[DETECT_ETHERHDR].desc = "sticky buffer to match on the Ethernet header";
52  sigmatch_table[DETECT_ETHERHDR].url = "/rules/header-keywords.html#etherhdr";
53  sigmatch_table[DETECT_ETHERHDR].Setup = DetectEtherhdrSetup;
55 #ifdef UNITTESTS
57 #endif
58 
59  g_etherhdr_buffer_id = DetectBufferTypeRegister("ether.hdr");
60  BUG_ON(g_etherhdr_buffer_id < 0);
61 
62  DetectBufferTypeSupportsPacket("ether.hdr");
63 
64  DetectPktMpmRegister("ether.hdr", 2, PrefilterGenericMpmPktRegister, GetData);
65 
67 }
68 
69 /**
70  * \brief setup ether.hdr sticky buffer
71  *
72  * \param de_ctx pointer to the Detection Engine Context
73  * \param s pointer to the Current Signature
74  * \param _unused unused
75  *
76  * \retval 0 on Success
77  * \retval -1 on Failure
78  */
79 static int DetectEtherhdrSetup(DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
80 {
83 
84  if (SCDetectBufferSetActiveList(de_ctx, s, g_etherhdr_buffer_id) < 0)
85  return -1;
86 
87  return 0;
88 }
89 
90 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
91  const DetectEngineTransforms *transforms, Packet *p, const int list_id)
92 {
93  SCEnter();
94 
95  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
96  if (buffer->inspect == NULL) {
97  if (!PacketIsEthernet(p)) {
98  // DETECT_PROTO_ETHERNET does not prefilter
99  return NULL;
100  }
101  const EthernetHdr *ethh = PacketGetEthernet(p);
102  if (((uint8_t *)ethh + (ptrdiff_t)ETHERNET_HEADER_LEN) >
103  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p))) {
104  SCLogDebug("data out of range: %p > %p",
105  ((uint8_t *)ethh + (ptrdiff_t)ETHERNET_HEADER_LEN),
106  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
107  return NULL;
108  }
109 
110  const uint32_t data_len = (uint32_t)ETHERNET_HEADER_LEN;
111  const uint8_t *data = (const uint8_t *)ethh;
112  SCLogDebug("inspect data %p / %u", data, data_len);
113 
115  det_ctx, list_id, buffer, data, data_len, transforms);
116  }
117 
118  return buffer;
119 }
120 
121 #ifdef UNITTESTS
122 #include "tests/detect-etherhdr.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:1471
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1685
SigTableElmt_::desc
const char * desc
Definition: detect.h:1470
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SigTableElmt_::name
const char * name
Definition: detect.h:1468
DetectEngineTransforms
Definition: detect.h:391
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1459
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:937
DETECT_PROTO_ETHERNET
#define DETECT_PROTO_ETHERNET
Definition: detect-engine-proto.h:33
DETECT_ETHERHDR
@ DETECT_ETHERHDR
Definition: detect-engine-register.h:269
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:1450
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:1252
detect-etherhdr.c
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
SignatureInitData_::proto
DetectProto proto
Definition: detect.h:635
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:317
Signature_::flags
uint32_t flags
Definition: detect.h:673
Packet_
Definition: decode.h:505
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:209
ETHERNET_HEADER_LEN
#define ETHERNET_HEADER_LEN
Definition: decode-ethernet.h:27
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:751
detect-engine-content-inspection.h
DetectEtherhdrRegisterTests
void DetectEtherhdrRegisterTests(void)
this function registers unit tests for ether.hdr
Definition: detect-etherhdr.c:44
detect-fast-pattern.h
DetectProto_::flags
uint8_t flags
Definition: detect-engine-proto.h:40
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1214
suricata-common.h
detect-engine-buffer.h
detect-etherhdr.h
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect-engine-inspect-buffer.h:35
detect-parse.h
Signature_
Signature container.
Definition: detect.h:672
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
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1660
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
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1457
DetectEtherhdrRegister
void DetectEtherhdrRegister(void)
Registration function for ether.hdr: keyword.
Definition: detect-etherhdr.c:48
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:253