suricata
detect-tcpmss.c
Go to the documentation of this file.
1/* Copyright (C) 2007-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 Victor Julien <victor@inliniac.net>
22 *
23 */
24
25#include "suricata-common.h"
26
27#include "detect.h"
28#include "detect-parse.h"
30#include "detect-engine-uint.h"
31#include "util-byte.h"
32
33#include "detect-tcpmss.h"
34
35
36/* prototypes */
37static int DetectTcpmssMatch (DetectEngineThreadCtx *, Packet *,
38 const Signature *, const SigMatchCtx *);
39static int DetectTcpmssSetup (DetectEngineCtx *, Signature *, const char *);
40void DetectTcpmssFree (DetectEngineCtx *, void *);
41#ifdef UNITTESTS
43#endif
44static int PrefilterSetupTcpmss(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
45static bool PrefilterTcpmssIsPrefilterable(const Signature *s);
46
47/**
48 * \brief Registration function for tcpmss: keyword
49 */
50
52{
54 sigmatch_table[DETECT_TCPMSS].desc = "match on TCP MSS option field";
55 sigmatch_table[DETECT_TCPMSS].url = "/rules/header-keywords.html#tcpmss";
56 sigmatch_table[DETECT_TCPMSS].Match = DetectTcpmssMatch;
57 sigmatch_table[DETECT_TCPMSS].Setup = DetectTcpmssSetup;
59 sigmatch_table[DETECT_TCPMSS].SupportsPrefilter = PrefilterTcpmssIsPrefilterable;
60 sigmatch_table[DETECT_TCPMSS].SetupPrefilter = PrefilterSetupTcpmss;
61}
62
63/**
64 * \brief This function is used to match TCPMSS rule option on a packet with those passed via
65 * tcpmss:
66 *
67 * \param det_ctx pointer to the pattern matcher thread
68 * \param p pointer to the current packet
69 * \param ctx pointer to the sigmatch that we will cast into DetectU16Data
70 *
71 * \retval 0 no match
72 * \retval 1 match
73 */
74static int DetectTcpmssMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
75 const Signature *s, const SigMatchCtx *ctx)
76{
78
79 if (!(PacketIsTCP(p)))
80 return 0;
81
82 if (!(TCP_HAS_MSS(p)))
83 return 0;
84
85 uint16_t ptcpmss = TCP_GET_MSS(p);
86
87 const DetectU16Data *tcpmssd = (const DetectU16Data *)ctx;
88 return DetectU16Match(ptcpmss, tcpmssd);
89}
90
91/**
92 * \brief this function is used to attach the parsed tcpmss data into the current signature
93 *
94 * \param de_ctx pointer to the Detection Engine Context
95 * \param s pointer to the Current Signature
96 * \param tcpmssstr pointer to the user provided tcpmss options
97 *
98 * \retval 0 on Success
99 * \retval -1 on Failure
100 */
101static int DetectTcpmssSetup (DetectEngineCtx *de_ctx, Signature *s, const char *tcpmssstr)
102{
103 DetectU16Data *tcpmssd = DetectU16Parse(tcpmssstr);
104 if (tcpmssd == NULL)
105 return -1;
106
108 de_ctx, s, DETECT_TCPMSS, (SigMatchCtx *)tcpmssd, DETECT_SM_LIST_MATCH) == NULL) {
109 DetectTcpmssFree(de_ctx, tcpmssd);
110 return -1;
111 }
113
114 return 0;
115}
116
117/**
118 * \brief this function will free memory associated with DetectU16Data
119 *
120 * \param ptr pointer to DetectU16Data
121 */
123{
124 SCDetectU16Free(ptr);
125}
126
127/* prefilter code */
128
129static void
130PrefilterPacketTcpmssMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
131{
133 if (!(PacketIsTCP(p)))
134 return;
135
136 if (!(TCP_HAS_MSS(p)))
137 return;
138
139 uint16_t ptcpmss = TCP_GET_MSS(p);
140
141 /* during setup Suricata will automatically see if there is another
142 * check that can be added: alproto, sport or dport */
143 const PrefilterPacketHeaderCtx *ctx = pectx;
144 if (!PrefilterPacketHeaderExtraMatch(ctx, p))
145 return;
146
147 DetectU16Data du16;
148 du16.mode = ctx->v1.u8[0];
149 du16.arg1 = ctx->v1.u16[1];
150 du16.arg2 = ctx->v1.u16[2];
151 /* if we match, add all the sigs that use this prefilter. This means
152 * that these will be inspected further */
153 if (DetectU16Match(ptcpmss, &du16)) {
154 SCLogDebug("packet matches tcpmss/hl %u", ptcpmss);
155 PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
156 }
157}
158
159static int PrefilterSetupTcpmss(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
160{
162 PrefilterPacketU16Set, PrefilterPacketU16Compare, PrefilterPacketTcpmssMatch);
163}
164
165static bool PrefilterTcpmssIsPrefilterable(const Signature *s)
166{
167 const SigMatch *sm;
168 for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
169 switch (sm->type) {
170 case DETECT_TCPMSS:
171 return true;
172 }
173 }
174 return false;
175}
#define TCP_HAS_MSS(p)
Definition decode-tcp.h:96
#define TCP_GET_MSS(p)
Definition decode-tcp.h:105
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition decode.h:1321
int PrefilterSetupPacketHeader(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type, SignatureMask mask, void(*Set)(PrefilterPacketHeaderValue *v, void *), bool(*Compare)(PrefilterPacketHeaderValue v, void *), void(*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
int DetectU16Match(const uint16_t parg, const DetectUintData_u16 *du16)
void PrefilterPacketU16Set(PrefilterPacketHeaderValue *v, void *smctx)
bool PrefilterPacketU16Compare(PrefilterPacketHeaderValue v, void *smctx)
DetectUintData_u16 * DetectU16Parse(const char *u16str)
This function is used to parse u16 options passed via some u16 keyword.
DetectUintData_u16 DetectU16Data
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
SigTableElmt * sigmatch_table
void DetectTcpmssRegister(void)
Registration function for tcpmss: keyword.
void DetectTcpmssRegisterTests(void)
void DetectTcpmssFree(DetectEngineCtx *, void *)
this function will free memory associated with DetectU16Data
#define SIG_FLAG_REQUIRE_PACKET
Definition detect.h:254
#define SIG_MASK_REQUIRE_REAL_PKT
Definition detect.h:316
@ DETECT_SM_LIST_MATCH
Definition detect.h:117
DetectEngineCtx * de_ctx
struct Thresholds ctx
main detection engine ctx
Definition detect.h:932
PrefilterRuleStore pmq
Definition detect.h:1349
Container for matching data for a signature group.
Definition detect.h:1629
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition detect.h:351
a single match condition for a signature
Definition detect.h:356
uint16_t type
Definition detect.h:357
struct SigMatch_ * next
Definition detect.h:360
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition detect.h:1444
void(* Free)(DetectEngineCtx *, void *)
Definition detect.h:1446
const char * desc
Definition detect.h:1461
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition detect.h:1421
const char * name
Definition detect.h:1459
bool(* SupportsPrefilter)(const Signature *s)
Definition detect.h:1443
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition detect.h:642
Signature container.
Definition detect.h:668
uint32_t flags
Definition detect.h:669
SignatureInitData * init_data
Definition detect.h:747
#define SCLogDebug(...)
Definition util-debug.h:275
#define DEBUG_VALIDATE_BUG_ON(exp)