suricata
detect-ipv6hdr.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2019 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 Victor Julien <victor@inliniac.net>
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-buffer.h"
31 #include "detect-engine-mpm.h"
34 #include "detect-fast-pattern.h"
35 #include "detect-ipv6hdr.h"
36 
37 /* prototypes */
38 static int DetectIpv6hdrSetup (DetectEngineCtx *, Signature *, const char *);
39 #ifdef UNITTESTS
40 void DetectIpv6hdrRegisterTests (void);
41 #endif
42 
43 static int g_ipv6hdr_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 ipv6.hdr: keyword
50  */
52 {
53  sigmatch_table[DETECT_IPV6HDR].name = "ipv6.hdr";
54  sigmatch_table[DETECT_IPV6HDR].desc = "sticky buffer to match on the IPV6 header";
55  sigmatch_table[DETECT_IPV6HDR].url = "/rules/header-keywords.html#ipv6hdr";
56  sigmatch_table[DETECT_IPV6HDR].Setup = DetectIpv6hdrSetup;
58 #ifdef UNITTESTS
60 #endif
61 
62  g_ipv6hdr_buffer_id = DetectBufferTypeRegister("ipv6.hdr");
63  BUG_ON(g_ipv6hdr_buffer_id < 0);
64 
66 
68 
70 }
71 
72 /**
73  * \brief setup ipv6.hdr sticky buffer
74  *
75  * \param de_ctx pointer to the Detection Engine Context
76  * \param s pointer to the Current Signature
77  * \param _unused unused
78  *
79  * \retval 0 on Success
80  * \retval -1 on Failure
81  */
82 static int DetectIpv6hdrSetup (DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
83 {
84  s->proto.flags |= DETECT_PROTO_IPV6; // TODO
85 
87 
88  if (SCDetectBufferSetActiveList(de_ctx, s, g_ipv6hdr_buffer_id) < 0)
89  return -1;
90 
91  return 0;
92 }
93 
94 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
95  const DetectEngineTransforms *transforms, Packet *p, const int list_id)
96 {
97  SCEnter();
98 
99  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
100  if (buffer->inspect == NULL) {
101  if (!PacketIsIPv6(p)) {
102  return NULL;
103  }
104  const IPV6Hdr *ip6h = PacketGetIPv6(p);
105  uint32_t hlen = IPV6_HEADER_LEN + IPV6_GET_EXTHDRS_LEN(p);
106  if (((uint8_t *)ip6h + (ptrdiff_t)hlen) >
107  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p))) {
108  SCLogDebug("data out of range: %p > %p (exthdrs_len %u)",
109  ((uint8_t *)ip6h + (ptrdiff_t)hlen),
110  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)),
112  SCReturnPtr(NULL, "InspectionBuffer");
113  }
114 
115  const uint32_t data_len = hlen;
116  const uint8_t *data = (const uint8_t *)ip6h;
117 
119  det_ctx, list_id, buffer, data, data_len, transforms);
120  }
121 
122  SCReturnPtr(buffer, "InspectionBuffer");
123 }
124 
125 #ifdef UNITTESTS
126 #include "tests/detect-ipv6hdr.c"
127 #endif
PrefilterGenericMpmPktRegister
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1670
SigTableElmt_::url
const char * url
Definition: detect.h:1406
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1620
SigTableElmt_::desc
const char * desc
Definition: detect.h:1405
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:155
DETECT_PROTO_IPV6
#define DETECT_PROTO_IPV6
Definition: detect-engine-proto.h:32
SigTableElmt_::name
const char * name
Definition: detect.h:1403
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.c:1570
DetectEngineTransforms
Definition: detect.h:415
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:541
InspectionBuffer
Definition: detect.h:380
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1397
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:921
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1388
detect-engine-prefilter.h
detect-ipv6hdr.c
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1429
DetectBufferTypeSupportsPacket
void DetectBufferTypeSupportsPacket(const char *name)
Definition: detect-engine.c:1127
DetectIpv6hdrRegisterTests
void DetectIpv6hdrRegisterTests(void)
this function registers unit tests for DetectIpv6hdr
Definition: detect-ipv6hdr.c:44
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1198
detect-ipv6hdr.h
DetectPktInspectEngineRegister
void DetectPktInspectEngineRegister(const char *name, InspectionBufferGetPktDataPtr GetPktData, InspectionBufferPktInspectFunc Callback)
register inspect engine at start up time
Definition: detect-engine.c:135
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:209
detect-engine-mpm.h
detect.h
DetectIpv6hdrRegister
void DetectIpv6hdrRegister(void)
Registration function for ipv6.hdr: keyword.
Definition: detect-ipv6hdr.c:51
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:309
Signature_::flags
uint32_t flags
Definition: detect.h:671
IPV6Hdr_
Definition: decode-ipv6.h:32
Packet_
Definition: decode.h:484
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:208
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:287
detect-engine-content-inspection.h
detect-fast-pattern.h
DetectProto_::flags
uint8_t flags
Definition: detect-engine-proto.h:37
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1093
Signature_::proto
DetectProto proto
Definition: detect.h:689
suricata-common.h
detect-engine-buffer.h
DETECT_IPV6HDR
@ DETECT_IPV6HDR
Definition: detect-engine-register.h:286
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:381
detect-parse.h
Signature_
Signature container.
Definition: detect.h:670
IPV6_HEADER_LEN
#define IPV6_HEADER_LEN
Definition: decode-ipv6.h:27
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1595
IPV6_GET_EXTHDRS_LEN
#define IPV6_GET_EXTHDRS_LEN(p)
Definition: decode-ipv6.h:76
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:2234
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1395
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:253