suricata
flow-bit.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2013 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  * Implements per flow bits. Actually, not a bit,
24  * but called that way because of Snort's flowbits.
25  * It's a binary storage.
26  *
27  * \todo move away from a linked list implementation
28  * \todo use different datatypes, such as string, int, etc.
29  * \todo have more than one instance of the same var, and be able to match on a
30  * specific one, or one all at a time. So if a certain capture matches
31  * multiple times, we can operate on all of them.
32  */
33 
34 #include "suricata-common.h"
35 #include "threads.h"
36 #include "flow-bit.h"
37 #include "flow.h"
38 #include "flow-util.h"
39 #include "flow-private.h"
40 #include "detect.h"
41 #include "util-var.h"
42 #include "util-debug.h"
43 #include "util-unittest.h"
44 
45 /* get the flowbit with idx from the flow */
46 static FlowBit *FlowBitGet(Flow *f, uint32_t idx)
47 {
48  GenericVar *gv = f->flowvar;
49  for ( ; gv != NULL; gv = gv->next) {
50  if (gv->type == DETECT_FLOWBITS && gv->idx == idx) {
51  return (FlowBit *)gv;
52  }
53  }
54 
55  return NULL;
56 }
57 
58 /* add a flowbit to the flow */
59 static void FlowBitAdd(Flow *f, uint32_t idx)
60 {
61  FlowBit *fb = FlowBitGet(f, idx);
62  if (fb == NULL) {
63  fb = SCMalloc(sizeof(FlowBit));
64  if (unlikely(fb == NULL))
65  return;
66 
67  fb->type = DETECT_FLOWBITS;
68  fb->idx = idx;
69  fb->next = NULL;
71  }
72 }
73 
74 static void FlowBitRemove(Flow *f, uint32_t idx)
75 {
76  FlowBit *fb = FlowBitGet(f, idx);
77  if (fb == NULL)
78  return;
79 
81  FlowBitFree(fb);
82 }
83 
84 void FlowBitSet(Flow *f, uint32_t idx)
85 {
86  FlowBitAdd(f, idx);
87 }
88 
89 void FlowBitUnset(Flow *f, uint32_t idx)
90 {
91  FlowBitRemove(f, idx);
92 }
93 
94 void FlowBitToggle(Flow *f, uint32_t idx)
95 {
96  FlowBit *fb = FlowBitGet(f, idx);
97  if (fb != NULL) {
98  FlowBitRemove(f, idx);
99  } else {
100  FlowBitAdd(f, idx);
101  }
102 }
103 
104 int FlowBitIsset(Flow *f, uint32_t idx)
105 {
106  int r = 0;
107 
108  FlowBit *fb = FlowBitGet(f, idx);
109  if (fb != NULL) {
110  r = 1;
111  }
112 
113  return r;
114 }
115 
116 int FlowBitIsnotset(Flow *f, uint32_t idx)
117 {
118  int r = 0;
119 
120  FlowBit *fb = FlowBitGet(f, idx);
121  if (fb == NULL) {
122  r = 1;
123  }
124 
125  return r;
126 }
127 
129 {
130  if (fb == NULL)
131  return;
132 
133  SCFree(fb);
134 }
135 
136 
137 /* TESTS */
138 #ifdef UNITTESTS
139 static int FlowBitTest01 (void)
140 {
141  int ret = 0;
142 
143  Flow f;
144  memset(&f, 0, sizeof(Flow));
145 
146  FlowBitAdd(&f, 0);
147 
148  FlowBit *fb = FlowBitGet(&f,0);
149  if (fb != NULL)
150  ret = 1;
151 
153  return ret;
154 }
155 
156 static int FlowBitTest02 (void)
157 {
158  int ret = 0;
159 
160  Flow f;
161  memset(&f, 0, sizeof(Flow));
162 
163  FlowBit *fb = FlowBitGet(&f,0);
164  if (fb == NULL)
165  ret = 1;
166 
168  return ret;
169 }
170 
171 static int FlowBitTest03 (void)
172 {
173  int ret = 0;
174 
175  Flow f;
176  memset(&f, 0, sizeof(Flow));
177 
178  FlowBitAdd(&f, 0);
179 
180  FlowBit *fb = FlowBitGet(&f,0);
181  if (fb == NULL) {
182  printf("fb == NULL although it was just added: ");
183  goto end;
184  }
185 
186  FlowBitRemove(&f, 0);
187 
188  fb = FlowBitGet(&f,0);
189  if (fb != NULL) {
190  printf("fb != NULL although it was just removed: ");
191  goto end;
192  } else {
193  ret = 1;
194  }
195 end:
197  return ret;
198 }
199 
200 static int FlowBitTest04 (void)
201 {
202  int ret = 0;
203 
204  Flow f;
205  memset(&f, 0, sizeof(Flow));
206 
207  FlowBitAdd(&f, 0);
208  FlowBitAdd(&f, 1);
209  FlowBitAdd(&f, 2);
210  FlowBitAdd(&f, 3);
211 
212  FlowBit *fb = FlowBitGet(&f,0);
213  if (fb != NULL)
214  ret = 1;
215 
217  return ret;
218 }
219 
220 static int FlowBitTest05 (void)
221 {
222  int ret = 0;
223 
224  Flow f;
225  memset(&f, 0, sizeof(Flow));
226 
227  FlowBitAdd(&f, 0);
228  FlowBitAdd(&f, 1);
229  FlowBitAdd(&f, 2);
230  FlowBitAdd(&f, 3);
231 
232  FlowBit *fb = FlowBitGet(&f,1);
233  if (fb != NULL)
234  ret = 1;
235 
237  return ret;
238 }
239 
240 static int FlowBitTest06 (void)
241 {
242  int ret = 0;
243 
244  Flow f;
245  memset(&f, 0, sizeof(Flow));
246 
247  FlowBitAdd(&f, 0);
248  FlowBitAdd(&f, 1);
249  FlowBitAdd(&f, 2);
250  FlowBitAdd(&f, 3);
251 
252  FlowBit *fb = FlowBitGet(&f,2);
253  if (fb != NULL)
254  ret = 1;
255 
257  return ret;
258 }
259 
260 static int FlowBitTest07 (void)
261 {
262  int ret = 0;
263 
264  Flow f;
265  memset(&f, 0, sizeof(Flow));
266 
267  FlowBitAdd(&f, 0);
268  FlowBitAdd(&f, 1);
269  FlowBitAdd(&f, 2);
270  FlowBitAdd(&f, 3);
271 
272  FlowBit *fb = FlowBitGet(&f,3);
273  if (fb != NULL)
274  ret = 1;
275 
277  return ret;
278 }
279 
280 static int FlowBitTest08 (void)
281 {
282  int ret = 0;
283 
284  Flow f;
285  memset(&f, 0, sizeof(Flow));
286 
287  FlowBitAdd(&f, 0);
288  FlowBitAdd(&f, 1);
289  FlowBitAdd(&f, 2);
290  FlowBitAdd(&f, 3);
291 
292  FlowBit *fb = FlowBitGet(&f,0);
293  if (fb == NULL)
294  goto end;
295 
296  FlowBitRemove(&f,0);
297 
298  fb = FlowBitGet(&f,0);
299  if (fb != NULL) {
300  printf("fb != NULL even though it was removed: ");
301  goto end;
302  }
303 
304  ret = 1;
305 end:
307  return ret;
308 }
309 
310 static int FlowBitTest09 (void)
311 {
312  int ret = 0;
313 
314  Flow f;
315  memset(&f, 0, sizeof(Flow));
316 
317  FlowBitAdd(&f, 0);
318  FlowBitAdd(&f, 1);
319  FlowBitAdd(&f, 2);
320  FlowBitAdd(&f, 3);
321 
322  FlowBit *fb = FlowBitGet(&f,1);
323  if (fb == NULL)
324  goto end;
325 
326  FlowBitRemove(&f,1);
327 
328  fb = FlowBitGet(&f,1);
329  if (fb != NULL) {
330  printf("fb != NULL even though it was removed: ");
331  goto end;
332  }
333 
334  ret = 1;
335 end:
337  return ret;
338 }
339 
340 static int FlowBitTest10 (void)
341 {
342  int ret = 0;
343 
344  Flow f;
345  memset(&f, 0, sizeof(Flow));
346 
347  FlowBitAdd(&f, 0);
348  FlowBitAdd(&f, 1);
349  FlowBitAdd(&f, 2);
350  FlowBitAdd(&f, 3);
351 
352  FlowBit *fb = FlowBitGet(&f,2);
353  if (fb == NULL)
354  goto end;
355 
356  FlowBitRemove(&f,2);
357 
358  fb = FlowBitGet(&f,2);
359  if (fb != NULL) {
360  printf("fb != NULL even though it was removed: ");
361  goto end;
362  }
363 
364  ret = 1;
365 end:
367  return ret;
368 }
369 
370 static int FlowBitTest11 (void)
371 {
372  int ret = 0;
373 
374  Flow f;
375  memset(&f, 0, sizeof(Flow));
376 
377  FlowBitAdd(&f, 0);
378  FlowBitAdd(&f, 1);
379  FlowBitAdd(&f, 2);
380  FlowBitAdd(&f, 3);
381 
382  FlowBit *fb = FlowBitGet(&f,3);
383  if (fb == NULL)
384  goto end;
385 
386  FlowBitRemove(&f,3);
387 
388  fb = FlowBitGet(&f,3);
389  if (fb != NULL) {
390  printf("fb != NULL even though it was removed: ");
391  goto end;
392  }
393 
394  ret = 1;
395 end:
397  return ret;
398 }
399 
400 #endif /* UNITTESTS */
401 
403 {
404 #ifdef UNITTESTS
405  UtRegisterTest("FlowBitTest01", FlowBitTest01);
406  UtRegisterTest("FlowBitTest02", FlowBitTest02);
407  UtRegisterTest("FlowBitTest03", FlowBitTest03);
408  UtRegisterTest("FlowBitTest04", FlowBitTest04);
409  UtRegisterTest("FlowBitTest05", FlowBitTest05);
410  UtRegisterTest("FlowBitTest06", FlowBitTest06);
411  UtRegisterTest("FlowBitTest07", FlowBitTest07);
412  UtRegisterTest("FlowBitTest08", FlowBitTest08);
413  UtRegisterTest("FlowBitTest09", FlowBitTest09);
414  UtRegisterTest("FlowBitTest10", FlowBitTest10);
415  UtRegisterTest("FlowBitTest11", FlowBitTest11);
416 #endif /* UNITTESTS */
417 }
418 
GenericVar_::type
uint8_t type
Definition: util-var.h:49
GenericVarAppend
void GenericVarAppend(GenericVar **list, GenericVar *gv)
Definition: util-var.c:92
flow-util.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
threads.h
flow-private.h
Flow_
Flow data structure.
Definition: flow.h:347
flow-bit.h
FlowBit_::next
GenericVar * next
Definition: flow-bit.h:34
util-var.h
util-unittest.h
FlowBitRegisterTests
void FlowBitRegisterTests(void)
Definition: flow-bit.c:402
FlowBitUnset
void FlowBitUnset(Flow *f, uint32_t idx)
Definition: flow-bit.c:89
util-debug.h
GenericVar_::next
struct GenericVar_ * next
Definition: util-var.h:52
FlowBitSet
void FlowBitSet(Flow *f, uint32_t idx)
Definition: flow-bit.c:84
detect.h
GenericVar_::idx
uint32_t idx
Definition: util-var.h:51
GenericVarRemove
void GenericVarRemove(GenericVar **list, GenericVar *gv)
Definition: util-var.c:111
FlowBitToggle
void FlowBitToggle(Flow *f, uint32_t idx)
Definition: flow-bit.c:94
Flow_::flowvar
GenericVar * flowvar
Definition: flow.h:482
suricata-common.h
GenericVar_
Definition: util-var.h:48
FlowBit_::idx
uint32_t idx
Definition: flow-bit.h:33
DETECT_FLOWBITS
@ DETECT_FLOWBITS
Definition: detect-engine-register.h:90
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
FlowBitIsset
int FlowBitIsset(Flow *f, uint32_t idx)
Definition: flow-bit.c:104
FlowBit_
Definition: flow-bit.h:30
FlowBitFree
void FlowBitFree(FlowBit *fb)
Definition: flow-bit.c:128
SCFree
#define SCFree(p)
Definition: util-mem.h:61
FlowBit_::type
uint8_t type
Definition: flow-bit.h:31
flow.h
GenericVarFree
void GenericVarFree(GenericVar *gv)
Definition: util-var.c:47
FlowBitIsnotset
int FlowBitIsnotset(Flow *f, uint32_t idx)
Definition: flow-bit.c:116