suricata
detect-udphdr.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-udphdr.h"
35 
36 /* prototypes */
37 static int DetectUdphdrSetup (DetectEngineCtx *, Signature *, const char *);
38 #ifdef UNITTESTS
39 void DetectUdphdrRegisterTests (void);
40 #endif
41 
42 static int g_udphdr_buffer_id = 0;
43 
44 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
45  const DetectEngineTransforms *transforms, Packet *p, const int list_id);
46 /**
47  * \brief Registration function for udp.hdr: keyword
48  */
50 {
51  sigmatch_table[DETECT_UDPHDR].name = "udp.hdr";
52  sigmatch_table[DETECT_UDPHDR].desc = "sticky buffer to match on the UDP header";
53  sigmatch_table[DETECT_UDPHDR].url = DOC_URL DOC_VERSION "/rules/header-keywords.html#udphdr";
54  sigmatch_table[DETECT_UDPHDR].Setup = DetectUdphdrSetup;
56 #ifdef UNITTESTS
58 #endif
59 
60  g_udphdr_buffer_id = DetectBufferTypeRegister("udp.hdr");
61  BUG_ON(g_udphdr_buffer_id < 0);
62 
64 
66 
67  DetectPktInspectEngineRegister("udp.hdr", GetData,
69  return;
70 }
71 
72 /**
73  * \brief set up the udp.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 DetectUdphdrSetup (DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
83 {
84  if (!(DetectProtoContainsProto(&s->proto, IPPROTO_UDP)))
85  return -1;
86 
88 
89  if (DetectBufferSetActiveList(s, g_udphdr_buffer_id) < 0)
90  return -1;
91 
92  return 0;
93 }
94 
95 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
96  const DetectEngineTransforms *transforms, Packet *p, const int list_id)
97 {
98  SCEnter();
99 
100  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
101  if (buffer->inspect == NULL) {
102  if (((uint8_t *)p->udph + (ptrdiff_t)UDP_HEADER_LEN) >
103  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)))
104  {
105  SCLogDebug("data out of range: %p > %p",
106  ((uint8_t *)p->udph + (ptrdiff_t)UDP_HEADER_LEN),
107  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
108  return NULL;
109  }
110 
111  const uint32_t data_len = UDP_HEADER_LEN;
112  const uint8_t *data = (const uint8_t *)p->udph;
113 
114  InspectionBufferSetup(buffer, data, data_len);
115  InspectionBufferApplyTransforms(buffer, transforms);
116  }
117 
118  return buffer;
119 }
120 
121 #ifdef UNITTESTS
122 #include "tests/detect-udphdr.c"
123 #endif
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1447
DetectProto proto
Definition: detect.h:543
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1187
UDPHdr * udph
Definition: decode.h:524
#define SCLogDebug(...)
Definition: util-debug.h:335
#define BUG_ON(x)
uint32_t flags
Definition: detect.h:527
void DetectPktMpmRegister(const char *name, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id), InspectionBufferGetPktDataPtr GetData)
register a MPM engine
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:228
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
const char * name
Definition: detect.h:1201
Signature container.
Definition: detect.h:526
void DetectBufferTypeSupportsPacket(const char *name)
main detection engine ctx
Definition: detect.h:764
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1394
void DetectPktInspectEngineRegister(const char *name, InspectionBufferGetPktDataPtr GetPktData, InspectionBufferPktInspectFunc Callback)
register inspect engine at start up time
#define SCEnter(...)
Definition: util-debug.h:337
void DetectUdphdrRegister(void)
Registration function for udp.hdr: keyword.
Definition: detect-udphdr.c:49
const char * desc
Definition: detect.h:1203
int DetectBufferTypeRegister(const char *name)
void DetectUdphdrRegisterTests(void)
this function registers unit tests for DetectUdphdr
Definition: detect-udphdr.c:44
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
#define SIGMATCH_NOOPT
Definition: detect.h:1370
const char * url
Definition: detect.h:1204
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
#define GET_PKT_DATA(p)
Definition: decode.h:225
int DetectBufferSetActiveList(Signature *s, const int list)
const uint8_t * inspect
Definition: detect.h:347
#define DOC_URL
Definition: suricata.h:86
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.
#define GET_PKT_LEN(p)
Definition: decode.h:224
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1195
int DetectProtoContainsProto(const DetectProto *dp, int proto)
see if a DetectProto contains a certain proto
void(* RegisterTests)(void)
Definition: detect.h:1193
#define UDP_HEADER_LEN
Definition: decode-udp.h:27