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"
30 #include "detect-engine-mpm.h"
33 #include "detect-fast-pattern.h"
34 #include "detect-icmpv6hdr.h"
35 #include "util-validate.h"
36 
37 /* prototypes */
38 static int DetectICMPv6hdrSetup (DetectEngineCtx *, Signature *, const char *);
39 #ifdef UNITTESTS
41 #endif
42 
43 static int g_icmpv6hdr_buffer_id = 0;
44 
45 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
46  const DetectEngineTransforms *transforms, Packet *p, const int list_id);
47 
48 /**
49  * \brief Registration function for icmpv6.hdr: keyword
50  */
52 {
53  sigmatch_table[DETECT_ICMPV6HDR].name = "icmpv6.hdr";
54  sigmatch_table[DETECT_ICMPV6HDR].desc = "sticky buffer to match on the ICMP V6 header";
55  sigmatch_table[DETECT_ICMPV6HDR].url = "/rules/header-keywords.html#icmpv6hdr";
56  sigmatch_table[DETECT_ICMPV6HDR].Setup = DetectICMPv6hdrSetup;
58 #ifdef UNITTESTS
60 #endif
61 
62  g_icmpv6hdr_buffer_id = DetectBufferTypeRegister("icmpv6.hdr");
63  BUG_ON(g_icmpv6hdr_buffer_id < 0);
64 
65  DetectBufferTypeSupportsPacket("icmpv6.hdr");
66 
67  DetectPktMpmRegister("icmpv6.hdr", 2, PrefilterGenericMpmPktRegister, GetData);
68 
69  DetectPktInspectEngineRegister("icmpv6.hdr", GetData,
71 
72  return;
73 }
74 
75 /**
76  * \brief setup icmpv6.hdr sticky buffer
77  *
78  * \param de_ctx pointer to the Detection Engine Context
79  * \param s pointer to the Current Signature
80  * \param _unused unused
81  *
82  * \retval 0 on Success
83  * \retval -1 on Failure
84  */
85 static int DetectICMPv6hdrSetup (DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
86 {
87  // ICMPv6 comes only with IPv6
89  if (!(DetectProtoContainsProto(&s->proto, IPPROTO_ICMPV6)))
90  return -1;
91 
93 
94  if (DetectBufferSetActiveList(de_ctx, s, g_icmpv6hdr_buffer_id) < 0)
95  return -1;
96 
97  return 0;
98 }
99 
100 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
101  const DetectEngineTransforms *transforms, Packet *p, const int list_id)
102 {
103  SCEnter();
104 
105  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
106  if (buffer->inspect == NULL) {
107  uint32_t hlen = ICMPV6_HEADER_LEN;
108  if (p->icmpv6h == NULL) {
109  // DETECT_PROTO_IPV6 does not prefilter
110  return NULL;
111  }
112  if (((uint8_t *)p->icmpv6h + (ptrdiff_t)hlen) >
113  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)))
114  {
115  SCLogDebug("data out of range: %p > %p",
116  ((uint8_t *)p->icmpv6h + (ptrdiff_t)hlen),
117  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
118  SCReturnPtr(NULL, "InspectionBuffer");
119  }
120 
121  const uint32_t data_len = hlen;
122  const uint8_t *data = (const uint8_t *)p->icmpv6h;
123 
124  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
125  InspectionBufferApplyTransforms(buffer, transforms);
126  }
127 
128  SCReturnPtr(buffer, "InspectionBuffer");
129 }
130 
131 #ifdef UNITTESTS
132 #include "tests/detect-icmpv6hdr.c"
133 #endif
SigTableElmt_::url
const char * url
Definition: detect.h:1299
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1500
SigTableElmt_::desc
const char * desc
Definition: detect.h:1298
DetectICMPv6hdrRegisterTests
void DetectICMPv6hdrRegisterTests(void)
this function registers unit tests for DetectICMPv6hdr
Definition: detect-icmpv6hdr.c:44
DETECT_PROTO_IPV6
#define DETECT_PROTO_IPV6
Definition: detect-engine-proto.h:34
SigTableElmt_::name
const char * name
Definition: detect.h:1296
DetectEngineTransforms
Definition: detect.h:409
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
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:516
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1335
DetectICMPv6hdrRegister
void DetectICMPv6hdrRegister(void)
Registration function for icmpv6.hdr: keyword.
Definition: detect-icmpv6hdr.c:51
InspectionBuffer
Definition: detect.h:374
detect-icmpv6hdr.c
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1290
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:839
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1281
detect-engine-prefilter.h
detect-icmpv6hdr.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1479
DetectBufferTypeSupportsPacket
void DetectBufferTypeSupportsPacket(const char *name)
Definition: detect-engine.c:1042
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1095
DetectPktInspectEngineRegister
void DetectPktInspectEngineRegister(const char *name, InspectionBufferGetPktDataPtr GetPktData, InspectionBufferPktInspectFunc Callback)
register inspect engine at start up time
Definition: detect-engine.c:128
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:221
detect-engine-mpm.h
detect.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
Signature_::flags
uint32_t flags
Definition: detect.h:597
Packet_::icmpv6h
ICMPV6Hdr * icmpv6h
Definition: decode.h:577
Packet_
Definition: decode.h:437
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:220
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:287
PrefilterGenericMpmPktRegister
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:815
detect-engine-content-inspection.h
detect-fast-pattern.h
DetectProto_::flags
uint8_t flags
Definition: detect-engine-proto.h:38
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1008
Signature_::proto
DetectProto proto
Definition: detect.h:615
DETECT_ICMPV6HDR
@ DETECT_ICMPV6HDR
Definition: detect-engine-register.h:285
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1678
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
ICMPV6_HEADER_LEN
#define ICMPV6_HEADER_LEN
Definition: decode-icmpv6.h:31
util-validate.h
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:375
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:1574
detect-parse.h
Signature_
Signature container.
Definition: detect.h:596
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1476
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:2178
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1288
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:249