suricata
detect-ipopts.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 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 Breno Silva <breno.silva@gmail.com>
22  *
23  * Implements the ipopts keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "suricata.h"
28 #include "decode.h"
29 
30 #include "detect.h"
31 #include "detect-parse.h"
32 
33 #include "flow-var.h"
34 #include "decode-events.h"
35 
36 #include "util-debug.h"
37 
38 #include "detect-ipopts.h"
39 #include "util-unittest.h"
40 
41 #define PARSE_REGEX "\\S[A-z]"
42 
43 static pcre *parse_regex;
44 static pcre_extra *parse_regex_study;
45 
46 static int DetectIpOptsMatch (DetectEngineThreadCtx *, Packet *,
47  const Signature *, const SigMatchCtx *);
48 static int DetectIpOptsSetup (DetectEngineCtx *, Signature *, const char *);
49 void IpOptsRegisterTests(void);
50 void DetectIpOptsFree(void *);
51 
52 /**
53  * \brief Registration function for ipopts: keyword
54  */
56 {
57  sigmatch_table[DETECT_IPOPTS].name = "ipopts";
58  sigmatch_table[DETECT_IPOPTS].desc = "check if a specific IP option is set";
59  sigmatch_table[DETECT_IPOPTS].url = DOC_URL DOC_VERSION "/rules/header-keywords.html#ipopts";
60  sigmatch_table[DETECT_IPOPTS].Match = DetectIpOptsMatch;
61  sigmatch_table[DETECT_IPOPTS].Setup = DetectIpOptsSetup;
64 
65  DetectSetupParseRegexes(PARSE_REGEX, &parse_regex, &parse_regex_study);
66 }
67 
68 /**
69  * \struct DetectIpOptss_
70  * DetectIpOptss_ is used to store supported iptops values
71  */
72 
73 struct DetectIpOpts_ {
74  const char *ipopt_name; /**< ip option name */
75  uint16_t code; /**< ip option flag value */
76 } ipopts[] = {
77  { "rr", IPV4_OPT_FLAG_RR, },
78  { "lsrr", IPV4_OPT_FLAG_LSRR, },
79  { "eol", IPV4_OPT_FLAG_EOL, },
80  { "nop", IPV4_OPT_FLAG_NOP, },
81  { "ts", IPV4_OPT_FLAG_TS, },
82  { "sec", IPV4_OPT_FLAG_SEC, },
83  { "ssrr", IPV4_OPT_FLAG_SSRR, },
84  { "satid", IPV4_OPT_FLAG_SID, },
85  { "any", 0xffff, },
86  { NULL, 0 },
87 };
88 
89 /**
90  * \internal
91  * \brief This function is used to match ip option on a packet with those passed via ipopts:
92  *
93  * \param t pointer to thread vars
94  * \param det_ctx pointer to the pattern matcher thread
95  * \param p pointer to the current packet
96  * \param s pointer to the Signature
97  * \param m pointer to the sigmatch
98  *
99  * \retval 0 no match
100  * \retval 1 match
101  */
102 static int DetectIpOptsMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
103  const Signature *s, const SigMatchCtx *ctx)
104 {
105  const DetectIpOptsData *de = (const DetectIpOptsData *)ctx;
106 
107  if (!de || !PKT_IS_IPV4(p) || PKT_IS_PSEUDOPKT(p))
108  return 0;
109 
110  if (p->ip4vars.opts_set & de->ipopt) {
111  return 1;
112  }
113 
114  return 0;
115 }
116 
117 /**
118  * \internal
119  * \brief This function is used to parse ipopts options passed via ipopts: keyword
120  *
121  * \param rawstr Pointer to the user provided ipopts options
122  *
123  * \retval de pointer to DetectIpOptsData on success
124  * \retval NULL on failure
125  */
126 static DetectIpOptsData *DetectIpOptsParse (const char *rawstr)
127 {
128  int i;
129  DetectIpOptsData *de = NULL;
130 #define MAX_SUBSTRINGS 30
131  int ret = 0, found = 0;
132  int ov[MAX_SUBSTRINGS];
133 
134  ret = pcre_exec(parse_regex, parse_regex_study, rawstr, strlen(rawstr), 0, 0, ov, MAX_SUBSTRINGS);
135  if (ret < 1) {
136  SCLogError(SC_ERR_PCRE_MATCH, "pcre_exec parse error, ret %" PRId32 ", string %s", ret, rawstr);
137  goto error;
138  }
139 
140  for(i = 0; ipopts[i].ipopt_name != NULL; i++) {
141  if((strcasecmp(ipopts[i].ipopt_name,rawstr)) == 0) {
142  found = 1;
143  break;
144  }
145  }
146 
147  if(found == 0)
148  goto error;
149 
150  de = SCMalloc(sizeof(DetectIpOptsData));
151  if (unlikely(de == NULL))
152  goto error;
153 
154  de->ipopt = ipopts[i].code;
155 
156  return de;
157 
158 error:
159  if (de) SCFree(de);
160  return NULL;
161 }
162 
163 /**
164  * \internal
165  * \brief this function is used to add the parsed ipopts into the current signature
166  *
167  * \param de_ctx pointer to the Detection Engine Context
168  * \param s pointer to the Current Signature
169  * \param rawstr pointer to the user provided ipopts options
170  *
171  * \retval 0 on Success
172  * \retval -1 on Failure
173  */
174 static int DetectIpOptsSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
175 {
176  DetectIpOptsData *de = NULL;
177  SigMatch *sm = NULL;
178 
179  de = DetectIpOptsParse(rawstr);
180  if (de == NULL)
181  goto error;
182 
183  sm = SigMatchAlloc();
184  if (sm == NULL)
185  goto error;
186 
187  sm->type = DETECT_IPOPTS;
188  sm->ctx = (SigMatchCtx *)de;
189 
192 
193  return 0;
194 
195 error:
196  if (de) SCFree(de);
197  if (sm) SCFree(sm);
198  return -1;
199 }
200 
201 /**
202  * \internal
203  * \brief this function will free memory associated with DetectIpOptsData
204  *
205  * \param de pointer to DetectIpOptsData
206  */
207 void DetectIpOptsFree(void *de_ptr)
208 {
209  DetectIpOptsData *de = (DetectIpOptsData *)de_ptr;
210  if(de) SCFree(de);
211 }
212 
213 /*
214  * ONLY TESTS BELOW THIS COMMENT
215  */
216 
217 #ifdef UNITTESTS
218 /**
219  * \test IpOptsTestParse01 is a test for a valid ipopts value
220  *
221  * \retval 1 on succces
222  * \retval 0 on failure
223  */
224 static int IpOptsTestParse01 (void)
225 {
226  DetectIpOptsData *de = NULL;
227  de = DetectIpOptsParse("lsrr");
228  if (de) {
229  DetectIpOptsFree(de);
230  return 1;
231  }
232 
233  return 0;
234 }
235 
236 /**
237  * \test IpOptsTestParse02 is a test for an invalid ipopts value
238  *
239  * \retval 1 on succces
240  * \retval 0 on failure
241  */
242 static int IpOptsTestParse02 (void)
243 {
244  DetectIpOptsData *de = NULL;
245  de = DetectIpOptsParse("invalidopt");
246  if (de) {
247  DetectIpOptsFree(de);
248  return 0;
249  }
250 
251  return 1;
252 }
253 
254 /**
255  * \test IpOptsTestParse03 test the match function on a packet that needs to match
256  *
257  * \retval 1 on succces
258  * \retval 0 on failure
259  */
260 static int IpOptsTestParse03 (void)
261 {
263  if (unlikely(p == NULL))
264  return 0;
265  ThreadVars tv;
266  int ret = 0;
267  DetectIpOptsData *de = NULL;
268  SigMatch *sm = NULL;
269  IPV4Hdr ip4h;
270 
271  memset(&tv, 0, sizeof(ThreadVars));
272  memset(p, 0, SIZE_OF_PACKET);
273  memset(&ip4h, 0, sizeof(IPV4Hdr));
274 
275  p->ip4h = &ip4h;
277 
278  de = DetectIpOptsParse("rr");
279 
280  if (de == NULL)
281  goto error;
282 
283  sm = SigMatchAlloc();
284  if (sm == NULL)
285  goto error;
286 
287  sm->type = DETECT_IPOPTS;
288  sm->ctx = (SigMatchCtx *)de;
289 
290  ret = DetectIpOptsMatch(NULL, p, NULL, sm->ctx);
291 
292  if(ret) {
293  SCFree(p);
294  return 1;
295  }
296 
297 error:
298  if (de) SCFree(de);
299  if (sm) SCFree(sm);
300  SCFree(p);
301  return 0;
302 }
303 
304 /**
305  * \test IpOptsTestParse04 test the match function on a packet that needs to not match
306  *
307  * \retval 1 on succces
308  * \retval 0 on failure
309  */
310 static int IpOptsTestParse04 (void)
311 {
313  if (unlikely(p == NULL))
314  return 0;
315  ThreadVars tv;
316  int ret = 0;
317  DetectIpOptsData *de = NULL;
318  SigMatch *sm = NULL;
319  IPV4Hdr ip4h;
320 
321  memset(&tv, 0, sizeof(ThreadVars));
322  memset(p, 0, SIZE_OF_PACKET);
323  memset(&ip4h, 0, sizeof(IPV4Hdr));
324 
325  p->ip4h = &ip4h;
327 
328  de = DetectIpOptsParse("lsrr");
329 
330  if (de == NULL)
331  goto error;
332 
333  sm = SigMatchAlloc();
334  if (sm == NULL)
335  goto error;
336 
337  sm->type = DETECT_IPOPTS;
338  sm->ctx = (SigMatchCtx *)de;
339 
340  ret = DetectIpOptsMatch(NULL, p, NULL, sm->ctx);
341 
342  if(ret) {
343  SCFree(p);
344  return 0;
345  }
346 
347  /* Error expected. */
348 error:
349  if (de) SCFree(de);
350  if (sm) SCFree(sm);
351  SCFree(p);
352  return 1;
353 }
354 #endif /* UNITTESTS */
355 
356 /**
357  * \brief this function registers unit tests for IpOpts
358  */
360 {
361 #ifdef UNITTESTS
362  UtRegisterTest("IpOptsTestParse01", IpOptsTestParse01);
363  UtRegisterTest("IpOptsTestParse02", IpOptsTestParse02);
364  UtRegisterTest("IpOptsTestParse03", IpOptsTestParse03);
365  UtRegisterTest("IpOptsTestParse04", IpOptsTestParse04);
366 #endif /* UNITTESTS */
367 }
IPV4Vars ip4vars
Definition: decode.h:507
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1448
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1186
uint32_t flags
Definition: detect.h:523
#define unlikely(expr)
Definition: util-optimize.h:35
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:223
const char * name
Definition: detect.h:1200
Signature container.
Definition: detect.h:522
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:313
#define PKT_IS_IPV4(p)
Definition: decode.h:252
main detection engine ctx
Definition: detect.h:761
#define SIZE_OF_PACKET
Definition: decode.h:619
int de
uint16_t code
Definition: detect-ipopts.c:75
void DetectIpOptsRegister(void)
Registration function for ipopts: keyword.
Definition: detect-ipopts.c:55
void(* Free)(void *)
Definition: detect.h:1191
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
void DetectSetupParseRegexes(const char *parse_str, pcre **parse_regex, pcre_extra **parse_regex_study)
#define PARSE_REGEX
Definition: detect-ipopts.c:41
const char * ipopt_name
Definition: detect-ipopts.c:74
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1170
#define MAX_SUBSTRINGS
uint8_t type
Definition: detect.h:319
void IpOptsRegisterTests(void)
this function registers unit tests for IpOpts
const char * desc
Definition: detect.h:1202
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:346
IPV4Hdr * ip4h
Definition: decode.h:501
SigMatchCtx * ctx
Definition: detect.h:321
#define SCMalloc(a)
Definition: util-mem.h:222
#define SCFree(a)
Definition: util-mem.h:322
struct DetectIpOpts_ ipopts[]
const char * url
Definition: detect.h:1203
void DetectIpOptsFree(void *)
uint16_t opts_set
Definition: decode-ipv4.h:177
#define DOC_URL
Definition: suricata.h:86
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1133
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:232
Per thread variable structure.
Definition: threadvars.h:57
#define DOC_VERSION
Definition: suricata.h:91
void(* RegisterTests)(void)
Definition: detect.h:1192
a single match condition for a signature
Definition: detect.h:318