suricata
detect-icmpv6hdr.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 Philippe Antoine <p.antoine@catenacyber.fr>
22 *
23 */
24
25#include "suricata-common.h"
26
27#include "detect.h"
28#include "detect-parse.h"
29#include "detect-engine.h"
31#include "detect-engine-mpm.h"
34#include "detect-fast-pattern.h"
35#include "detect-icmpv6hdr.h"
36#include "util-validate.h"
37
38/* prototypes */
39static int DetectICMPv6hdrSetup (DetectEngineCtx *, Signature *, const char *);
40#ifdef UNITTESTS
42#endif
43
44static int g_icmpv6hdr_buffer_id = 0;
45
46static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
47 const DetectEngineTransforms *transforms, Packet *p, const int list_id);
48
49/**
50 * \brief Registration function for icmpv6.hdr: keyword
51 */
53{
54 sigmatch_table[DETECT_ICMPV6HDR].name = "icmpv6.hdr";
55 sigmatch_table[DETECT_ICMPV6HDR].desc = "sticky buffer to match on the ICMP V6 header";
56 sigmatch_table[DETECT_ICMPV6HDR].url = "/rules/header-keywords.html#icmpv6hdr";
57 sigmatch_table[DETECT_ICMPV6HDR].Setup = DetectICMPv6hdrSetup;
59#ifdef UNITTESTS
61#endif
62
63 g_icmpv6hdr_buffer_id = DetectBufferTypeRegister("icmpv6.hdr");
64 BUG_ON(g_icmpv6hdr_buffer_id < 0);
65
67
69
71}
72
73/**
74 * \brief setup icmpv6.hdr sticky buffer
75 *
76 * \param de_ctx pointer to the Detection Engine Context
77 * \param s pointer to the Current Signature
78 * \param _unused unused
79 *
80 * \retval 0 on Success
81 * \retval -1 on Failure
82 */
83static int DetectICMPv6hdrSetup (DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
84{
85 // ICMPv6 comes only with IPv6
87 if (!(DetectProtoContainsProto(&s->proto, IPPROTO_ICMPV6)))
88 return -1;
89
91
92 if (SCDetectBufferSetActiveList(de_ctx, s, g_icmpv6hdr_buffer_id) < 0)
93 return -1;
94
95 return 0;
96}
97
98static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
99 const DetectEngineTransforms *transforms, Packet *p, const int list_id)
100{
101 SCEnter();
102
103 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
104 if (buffer->inspect == NULL) {
105 uint32_t hlen = ICMPV6_HEADER_LEN;
106 if (!PacketIsICMPv6(p)) {
107 // DETECT_PROTO_IPV6 does not prefilter
108 return NULL;
109 }
110 const ICMPV6Hdr *icmpv6h = PacketGetICMPv6(p);
111 if (((uint8_t *)icmpv6h + (ptrdiff_t)hlen) >
112 ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p))) {
113 SCLogDebug("data out of range: %p > %p", ((uint8_t *)icmpv6h + (ptrdiff_t)hlen),
114 ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
115 SCReturnPtr(NULL, "InspectionBuffer");
116 }
117
118 const uint32_t data_len = hlen;
119 const uint8_t *data = (const uint8_t *)icmpv6h;
120
122 det_ctx, list_id, buffer, data, data_len, transforms);
123 }
124
125 SCReturnPtr(buffer, "InspectionBuffer");
126}
127
128#ifdef UNITTESTS
130#endif
#define ICMPV6_HEADER_LEN
#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_IPV6
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 DetectICMPv6hdrRegister(void)
Registration function for icmpv6.hdr: keyword.
void DetectICMPv6hdrRegisterTests(void)
this function registers unit tests for DetectICMPv6hdr
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