suricata
detect-ipv4hdr.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-mpm.h"
33 #include "detect-fast-pattern.h"
34 #include "detect-ipv4hdr.h"
35 
36 /* prototypes */
37 static int DetectIpv4hdrSetup (DetectEngineCtx *, Signature *, const char *);
38 #ifdef UNITTESTS
39 void DetectIpv4hdrRegisterTests (void);
40 #endif
41 
42 static int g_ipv4hdr_buffer_id = 0;
43 
44 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
45  const DetectEngineTransforms *transforms, Packet *p, const int list_id);
46 
47 /**
48  * \brief Registration function for ipv4.hdr: keyword
49  */
51 {
52  sigmatch_table[DETECT_IPV4HDR].name = "ipv4.hdr";
53  sigmatch_table[DETECT_IPV4HDR].desc = "sticky buffer to match on the IPV4 header";
54  sigmatch_table[DETECT_IPV4HDR].url = "/rules/header-keywords.html#ipv4hdr";
55  sigmatch_table[DETECT_IPV4HDR].Setup = DetectIpv4hdrSetup;
57 #ifdef UNITTESTS
59 #endif
60 
61  g_ipv4hdr_buffer_id = DetectBufferTypeRegister("ipv4.hdr");
62  BUG_ON(g_ipv4hdr_buffer_id < 0);
63 
65 
67 
69 }
70 
71 /**
72  * \brief setup ipv4.hdr sticky buffer
73  *
74  * \param de_ctx pointer to the Detection Engine Context
75  * \param s pointer to the Current Signature
76  * \param _unused unused
77  *
78  * \retval 0 on Success
79  * \retval -1 on Failure
80  */
81 static int DetectIpv4hdrSetup (DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
82 {
83  s->proto.flags |= DETECT_PROTO_IPV4; // TODO
84 
86 
87  if (DetectBufferSetActiveList(de_ctx, s, g_ipv4hdr_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  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
99  if (buffer->inspect == NULL) {
100  if (!PacketIsIPv4(p)) {
101  // DETECT_PROTO_IPV4 does not prefilter
102  return NULL;
103  }
104  const IPV4Hdr *ip4h = PacketGetIPv4(p);
105  uint32_t hlen = IPV4_GET_RAW_HLEN(ip4h);
106  if (((uint8_t *)ip4h + (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 *)ip4h + (ptrdiff_t)hlen),
109  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
110  return NULL;
111  }
112 
113  const uint32_t data_len = hlen;
114  const uint8_t *data = (const uint8_t *)ip4h;
115 
116  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
117  InspectionBufferApplyTransforms(buffer, transforms);
118  }
119 
120  return buffer;
121 }
122 
123 #ifdef UNITTESTS
124 #include "tests/detect-ipv4hdr.c"
125 #endif
PrefilterGenericMpmPktRegister
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:873
SigTableElmt_::url
const char * url
Definition: detect.h:1312
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1517
SigTableElmt_::desc
const char * desc
Definition: detect.h:1311
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:128
SigTableElmt_::name
const char * name
Definition: detect.h:1309
DetectEngineTransforms
Definition: detect.h:408
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
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1357
InspectionBuffer
Definition: detect.h:373
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1303
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:843
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1294
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1501
DetectBufferTypeSupportsPacket
void DetectBufferTypeSupportsPacket(const char *name)
Definition: detect-engine.c:1064
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1098
DetectPktInspectEngineRegister
void DetectPktInspectEngineRegister(const char *name, InspectionBufferGetPktDataPtr GetPktData, InspectionBufferPktInspectFunc Callback)
register inspect engine at start up time
Definition: detect-engine.c:130
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:205
detect-engine-mpm.h
detect.h
IPV4_GET_RAW_HLEN
#define IPV4_GET_RAW_HLEN(ip4h)
Definition: decode-ipv4.h:96
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
Signature_::flags
uint32_t flags
Definition: detect.h:604
Packet_
Definition: decode.h:476
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:204
detect-engine-content-inspection.h
IPV4Hdr_
Definition: decode-ipv4.h:72
detect-fast-pattern.h
DetectProto_::flags
uint8_t flags
Definition: detect-engine-proto.h:37
DETECT_PROTO_IPV4
#define DETECT_PROTO_IPV4
Definition: detect-engine-proto.h:31
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1030
Signature_::proto
DetectProto proto
Definition: detect.h:622
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1712
DETECT_IPV4HDR
@ DETECT_IPV4HDR
Definition: detect-engine-register.h:288
DetectIpv4hdrRegister
void DetectIpv4hdrRegister(void)
Registration function for ipv4.hdr: keyword.
Definition: detect-ipv4hdr.c:50
detect-ipv4hdr.c
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:374
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:1596
DetectIpv4hdrRegisterTests
void DetectIpv4hdrRegisterTests(void)
this function registers unit tests for DetectIpv4hdr
Definition: detect-ipv4hdr.c:44
detect-parse.h
Signature_
Signature container.
Definition: detect.h:603
detect-ipv4hdr.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1493
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:2262
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1301
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:250