suricata
detect-icmpv4hdr.c
Go to the documentation of this file.
1 /* Copyright (C) 2020 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  * \author Jeff Lucovsky <jeff@lucovsky.org>
22  *
23  */
24 
25 #include "suricata-common.h"
26 
27 #include "detect.h"
28 #include "detect-engine.h"
29 #include "detect-engine-mpm.h"
30 #include "detect-icmpv4hdr.h"
31 
32 /* prototypes */
33 static int DetectIcmpv4HdrSetup(DetectEngineCtx *, Signature *, const char *);
34 #ifdef UNITTESTS
36 #endif
37 
38 static int g_icmpv4hdr_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 icmpv4.hdr: keyword
45  */
47 {
48  sigmatch_table[DETECT_ICMPV4HDR].name = "icmpv4.hdr";
49  sigmatch_table[DETECT_ICMPV4HDR].desc = "sticky buffer to match on the ICMP v4 header";
50  sigmatch_table[DETECT_ICMPV4HDR].url = "/rules/header-keywords.html#icmpv4-hdr";
51  sigmatch_table[DETECT_ICMPV4HDR].Setup = DetectIcmpv4HdrSetup;
53 #ifdef UNITTESTS
55 #endif
56 
57  g_icmpv4hdr_buffer_id = DetectBufferTypeRegister("icmpv4.hdr");
58  BUG_ON(g_icmpv4hdr_buffer_id < 0);
59 
60  DetectBufferTypeSupportsPacket("icmpv4.hdr");
61 
62  DetectPktMpmRegister("icmpv4.hdr", 2, PrefilterGenericMpmPktRegister, GetData);
63 
65 
66  return;
67 }
68 
69 /**
70  * \brief setup icmpv4.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 DetectIcmpv4HdrSetup(DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
80 {
81  if (!(DetectProtoContainsProto(&s->proto, IPPROTO_ICMP)))
82  return -1;
83 
86 
87  if (DetectBufferSetActiveList(s, g_icmpv4hdr_buffer_id) < 0)
88  return -1;
89 
90  return 0;
91 }
92 
93 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
94  const DetectEngineTransforms *transforms, Packet *p, const int list_id)
95 {
96  SCEnter();
97 
98  if (p->icmpv4h == NULL) {
99  SCReturnPtr(NULL, "InspectionBuffer");
100  }
101 
102  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
103  if (buffer->inspect == NULL) {
104  uint16_t hlen = ICMPV4_GET_HLEN_ICMPV4H(p);
105  if (((uint8_t *)p->icmpv4h + (ptrdiff_t)hlen) >
106  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p))) {
107  SCLogDebug("data out of range: %p > %p", ((uint8_t *)p->icmpv4h + (ptrdiff_t)hlen),
108  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
109  SCReturnPtr(NULL, "InspectionBuffer");
110  }
111 
112  const uint32_t data_len = hlen;
113  const uint8_t *data = (const uint8_t *)p->icmpv4h;
114 
115  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
116  InspectionBufferApplyTransforms(buffer, transforms);
117  }
118 
119  SCReturnPtr(buffer, "InspectionBuffer");
120 }
121 
122 #ifdef UNITTESTS
123 #include "tests/detect-icmpv4hdr.c"
124 #endif
detect-icmpv4hdr.h
SigTableElmt_::url
const char * url
Definition: detect.h:1204
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1397
SigTableElmt_::desc
const char * desc
Definition: detect.h:1203
DetectPktMpmRegister
void DetectPktMpmRegister(const char *name, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id), InspectionBufferGetPktDataPtr GetData)
register a MPM engine
Definition: detect-engine-mpm.c:294
SigTableElmt_::name
const char * name
Definition: detect.h:1201
DetectEngineTransforms
Definition: detect.h:378
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
InspectionBuffer
Definition: detect.h:344
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1195
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:758
DetectIcmpv4HdrRegister
void DetectIcmpv4HdrRegister(void)
Registration function for icmpv4.hdr: keyword.
Definition: detect-icmpv4hdr.c:46
ICMPV4_GET_HLEN_ICMPV4H
#define ICMPV4_GET_HLEN_ICMPV4H(p)
Definition: decode-icmpv4.h:262
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1010
DetectBufferTypeSupportsPacket
void DetectBufferTypeSupportsPacket(const char *name)
Definition: detect-engine.c:799
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1003
DetectPktInspectEngineRegister
void DetectPktInspectEngineRegister(const char *name, InspectionBufferGetPktDataPtr GetPktData, InspectionBufferPktInspectFunc Callback)
register inspect engine at start up time
Definition: detect-engine.c:131
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:233
detect-engine-mpm.h
DETECT_ICMPV4HDR
@ DETECT_ICMPV4HDR
Definition: detect-engine-register.h:49
detect.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:281
Signature_::flags
uint32_t flags
Definition: detect.h:518
Packet_
Definition: decode.h:414
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:232
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:316
DetectProto_::flags
uint8_t flags
Definition: detect-engine-proto.h:38
DETECT_PROTO_IPV4
#define DETECT_PROTO_IPV4
Definition: detect-engine-proto.h:33
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:785
Signature_::proto
DetectProto proto
Definition: detect.h:535
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1193
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
Packet_::icmpv4h
ICMPV4Hdr * icmpv4h
Definition: decode.h:539
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:345
DetectIcmpv4HdrRegisterTests
void DetectIcmpv4HdrRegisterTests(void)
this function registers unit tests for DetectIcmpv4Hdr
Definition: detect-icmpv4hdr.c:42
InspectionBufferSetup
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1091
Signature_
Signature container.
Definition: detect.h:517
PrefilterGenericMpmPktRegister
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:746
detect-icmpv4hdr.c
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1373
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:941
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:1682
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1193
DetectProtoContainsProto
int DetectProtoContainsProto(const DetectProto *dp, int proto)
see if a DetectProto contains a certain proto
Definition: detect-engine-proto.c:135
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:223