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"
30#include "detect-engine-mpm.h"
31#include "detect-icmpv4hdr.h"
33
34/* prototypes */
35static int DetectIcmpv4HdrSetup(DetectEngineCtx *, Signature *, const char *);
36#ifdef UNITTESTS
38#endif
39
40static int g_icmpv4hdr_buffer_id = 0;
41
42static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
43 const DetectEngineTransforms *transforms, Packet *p, const int list_id);
44
45/**
46 * \brief Registration function for icmpv4.hdr: keyword
47 */
49{
50 sigmatch_table[DETECT_ICMPV4HDR].name = "icmpv4.hdr";
51 sigmatch_table[DETECT_ICMPV4HDR].desc = "sticky buffer to match on the ICMP v4 header";
52 sigmatch_table[DETECT_ICMPV4HDR].url = "/rules/header-keywords.html#icmpv4-hdr";
53 sigmatch_table[DETECT_ICMPV4HDR].Setup = DetectIcmpv4HdrSetup;
55#ifdef UNITTESTS
57#endif
58
59 g_icmpv4hdr_buffer_id = DetectBufferTypeRegister("icmpv4.hdr");
60 BUG_ON(g_icmpv4hdr_buffer_id < 0);
61
63
65
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 */
79static 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 (SCDetectBufferSetActiveList(de_ctx, s, g_icmpv4hdr_buffer_id) < 0)
88 return -1;
89
90 return 0;
91}
92
93static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
94 const DetectEngineTransforms *transforms, Packet *p, const int list_id)
95{
96 SCEnter();
97
98 if (!PacketIsICMPv4(p)) {
99 SCReturnPtr(NULL, "InspectionBuffer");
100 }
101
102 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
103 if (buffer->inspect == NULL) {
104 const ICMPV4Hdr *icmpv4h = PacketGetICMPv4(p);
105 uint16_t hlen = ICMPV4_GET_HLEN_ICMPV4H(p);
106 if (((uint8_t *)icmpv4h + (ptrdiff_t)hlen) >
107 ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p))) {
108 SCLogDebug("data out of range: %p > %p", ((uint8_t *)icmpv4h + (ptrdiff_t)hlen),
109 ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
110 SCReturnPtr(NULL, "InspectionBuffer");
111 }
112
113 const uint32_t data_len = hlen;
114 const uint8_t *data = (const uint8_t *)icmpv4h;
115
117 det_ctx, list_id, buffer, data, data_len, transforms);
118 }
119
120 SCReturnPtr(buffer, "InspectionBuffer");
121}
122
123#ifdef UNITTESTS
125#endif
#define ICMPV4_GET_HLEN_ICMPV4H(p)
#define GET_PKT_DATA(p)
Definition decode.h:209
#define GET_PKT_LEN(p)
Definition decode.h:208
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
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
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
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
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
int DetectProtoContainsProto(const DetectProto *dp, int proto)
see if a DetectProto contains a certain proto
#define DETECT_PROTO_IPV4
@ DETECT_ICMPV4HDR
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.
int DetectBufferTypeRegister(const char *name)
void DetectPktInspectEngineRegister(const char *name, InspectionBufferGetPktDataPtr GetPktData, InspectionBufferPktInspectFunc Callback)
register inspect engine at start up time
void DetectBufferTypeSupportsPacket(const char *name)
void DetectIcmpv4HdrRegister(void)
Registration function for icmpv4.hdr: keyword.
void DetectIcmpv4HdrRegisterTests(void)
this function registers unit tests for DetectIcmpv4Hdr
SigTableElmt * sigmatch_table
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define SIG_FLAG_REQUIRE_PACKET
Definition detect.h:254
#define SIGMATCH_INFO_STICKY_BUFFER
Definition detect.h:1676
DetectEngineCtx * de_ctx
main detection engine ctx
Definition detect.h:932
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
void(* RegisterTests)(void)
Definition detect.h:1448
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
uint32_t flags
Definition detect.h:669
DetectProto proto
Definition detect.h:687
#define BUG_ON(x)
#define SCEnter(...)
Definition util-debug.h:277
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCReturnPtr(x, type)
Definition util-debug.h:293