suricata
detect-engine-prefilter-common.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2016 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 #include "suricata-common.h"
21 
24 
25  uint16_t type; /**< PREFILTER_EXTRA_MATCH_* */
26  uint16_t value;
27 
28  uint32_t cnt;
30 
31 static uint32_t PrefilterPacketHeaderHashFunc(HashListTable *ht, void *data, uint16_t datalen)
32 {
34  uint64_t hash = ctx->v1.u64[0] + ctx->v1.u64[1] + ctx->type + ctx->value;
35  hash %= ht->array_size;
36  return hash;
37 }
38 
39 static char PrefilterPacketHeaderCompareFunc(void *data1, uint16_t len1,
40  void *data2, uint16_t len2)
41 {
42  PrefilterPacketHeaderHashCtx *ctx1 = data1;
43  PrefilterPacketHeaderHashCtx *ctx2 = data2;
44  return (ctx1->v1.u64[0] == ctx2->v1.u64[0] &&
45  ctx1->v1.u64[1] == ctx2->v1.u64[1] &&
46  ctx1->type == ctx2->type &&
47  ctx1->value == ctx2->value);
48 }
49 
50 static void PrefilterPacketHeaderFreeFunc(void *ptr)
51 {
52  SCFree(ptr);
53 }
54 
55 static void PrefilterPacketHeaderFree(void *pectx)
56 {
58  SCFree(ctx->sigs_array);
59  SCFree(ctx);
60 }
61 
62 static void PrefilterPacketU8HashCtxFree(void *vctx)
63 {
65  int i;
66  for (i = 0; i < 256; i++) {
67  SigsArray *sa = ctx->array[i];
68  if (sa == NULL)
69  continue;
70  SCFree(sa->sigs);
71  SCFree(sa);
72  }
73  SCFree(ctx);
74 }
75 
76 static void GetExtraMatch(const Signature *s, uint16_t *type, uint16_t *value)
77 {
78  if (s->sp != NULL && s->sp->next == NULL && s->sp->port == s->sp->port2 &&
79  !(s->sp->flags & PORT_FLAG_NOT))
80  {
82  *value = s->sp->port;
83  } else if (s->alproto != ALPROTO_UNKNOWN) {
85  *value = s->alproto;
86  } else if (s->dp != NULL && s->dp->next == NULL && s->dp->port == s->dp->port2 &&
87  !(s->dp->flags & PORT_FLAG_NOT))
88  {
90  *value = s->dp->port;
91  }
92 }
93 
94 /** \internal
95  */
96 static int SetupEngineForPacketHeader(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type,
98  bool (*Compare)(PrefilterPacketHeaderValue v, void *),
99  void (*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
100 {
101  Signature *s = NULL;
102  uint32_t sig = 0;
103  uint32_t sig_offset = 0;
104 
106  if (ctx == NULL)
107  return -1;
108 
109  ctx->v1 = hctx->v1;
110  ctx->type = hctx->type;
111  ctx->value = hctx->value;
112 
113  ctx->sigs_cnt = hctx->cnt;
114  ctx->sigs_array = SCCalloc(ctx->sigs_cnt, sizeof(SigIntId));
115  if (ctx->sigs_array == NULL) {
116  SCFree(ctx);
117  return -1;
118  }
119 
120  for (sig = 0; sig < sgh->init->sig_cnt; sig++) {
121  s = sgh->init->match_array[sig];
122  if (s == NULL)
123  continue;
124  if (s->init_data->prefilter_sm == NULL || s->init_data->prefilter_sm->type != sm_type)
125  continue;
126 
127  uint16_t type = 0;
128  uint16_t value = 0;
129  GetExtraMatch(s, &type, &value);
130 
131  if (Compare(ctx->v1, s->init_data->prefilter_sm->ctx) &&
132  ctx->type == type && ctx->value == value)
133  {
134  SCLogDebug("appending sid %u on %u", s->id, sig_offset);
135  ctx->sigs_array[sig_offset] = s->num;
136  sig_offset++;
137 
139  }
140  }
141 
142  SCLogDebug("%s: ctx %p extra type %u extra value %u, sig cnt %u",
143  sigmatch_table[sm_type].name, ctx, ctx->type, ctx->value,
144  ctx->sigs_cnt);
146  de_ctx, sgh, Match, mask, ctx, PrefilterPacketHeaderFree, sigmatch_table[sm_type].name);
147  return 0;
148 }
149 
150 /** \internal
151  * \brief apply signature to each value */
152 static void ApplyToU8Hash(PrefilterPacketU8HashCtx *ctx, PrefilterPacketHeaderValue v, Signature *s)
153 {
154  switch (v.u8[0]) {
156  {
157  SigsArray *sa = ctx->array[v.u8[1]];
158  sa->sigs[sa->offset++] = s->num;
159  break;
160  }
162  {
163  uint8_t x = v.u8[1] - 1;
164  do {
165  SigsArray *sa = ctx->array[x];
166  sa->sigs[sa->offset++] = s->num;
167  } while (x--);
168 
169  break;
170  }
172  {
173  int x = v.u8[1] + 1;
174  do {
175  SigsArray *sa = ctx->array[x];
176  sa->sigs[sa->offset++] = s->num;
177  } while (++x < 256);
178 
179  break;
180  }
182  {
183  int x = v.u8[1] + 1;
184  do {
185  SigsArray *sa = ctx->array[x];
186  sa->sigs[sa->offset++] = s->num;
187  } while (++x < v.u8[2]);
188 
189  break;
190  }
191  }
192 }
193 
194 /** \internal
195  * \brief turn values into a u8 hash map
196  * \todo improve error handling
197  * \todo deduplicate sigs arrays
198  */
199 static int SetupEngineForPacketHeaderPrefilterPacketU8HashCtx(DetectEngineCtx *de_ctx,
200  SigGroupHead *sgh, int sm_type, SignatureMask mask, uint32_t *counts,
201  void (*Set)(PrefilterPacketHeaderValue *v, void *),
202  bool (*Compare)(PrefilterPacketHeaderValue v, void *),
203  void (*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
204 {
205  Signature *s = NULL;
206  uint32_t sig = 0;
207  uint32_t cnt = 0;
208 
210  if (ctx == NULL)
211  return -1;
212 
213  int set_cnt = 0;
214  for (int i = 0; i < 256; i++) {
215  if (counts[i] == 0)
216  continue;
217  ctx->array[i] = SCCalloc(1, sizeof(SigsArray));
218  BUG_ON(ctx->array[i] == NULL);
219 
220  ctx->array[i]->cnt = counts[i];
221  ctx->array[i]->sigs = SCCalloc(ctx->array[i]->cnt, sizeof(SigIntId));
222  BUG_ON(ctx->array[i]->sigs == NULL);
223  set_cnt++;
224  }
225  if (set_cnt == 0) {
226  /* not an error */
227  PrefilterPacketU8HashCtxFree(ctx);
228  return 0;
229  }
230 
231  for (sig = 0; sig < sgh->init->sig_cnt; sig++) {
232  s = sgh->init->match_array[sig];
233  if (s == NULL)
234  continue;
235  if (s->init_data->prefilter_sm == NULL || s->init_data->prefilter_sm->type != sm_type)
236  continue;
237 
239  memset(&v, 0, sizeof(v));
240  Set(&v, s->init_data->prefilter_sm->ctx);
241 
242  ApplyToU8Hash(ctx, v, s);
244  cnt++;
245  }
246 
247  if (cnt) {
248  PrefilterAppendEngine(de_ctx, sgh, Match, mask, ctx, PrefilterPacketU8HashCtxFree,
249  sigmatch_table[sm_type].name);
250  } else {
251  PrefilterPacketU8HashCtxFree(ctx);
252  }
253  return 0;
254 }
255 
256 /** \internal
257  * \brief setup a engine for each unique value
258  */
259 static void SetupSingle(DetectEngineCtx *de_ctx, HashListTable *hash_table, SigGroupHead *sgh,
260  int sm_type, SignatureMask mask, bool (*Compare)(PrefilterPacketHeaderValue v, void *),
261  void (*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
262 {
264  for ( ; hb != NULL; hb = HashListTableGetListNext(hb)) {
266 
267  SetupEngineForPacketHeader(de_ctx, sgh, sm_type, mask, ctx, Compare, Match);
268  }
269 }
270 
271 /** \internal
272  * \brief setup a single engine with a hash map for u8 values
273  */
274 static void SetupU8Hash(DetectEngineCtx *de_ctx, HashListTable *hash_table, SigGroupHead *sgh,
275  int sm_type, SignatureMask mask, void (*Set)(PrefilterPacketHeaderValue *v, void *),
276  bool (*Compare)(PrefilterPacketHeaderValue v, void *),
277  void (*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
278 {
279  uint32_t counts[256];
280  memset(&counts, 0, sizeof(counts));
281 
283  for ( ; hb != NULL; hb = HashListTableGetListNext(hb)) {
285 
286  switch (ctx->v1.u8[0]) {
288  counts[ctx->v1.u8[1]] += ctx->cnt;
289  break;
291  {
292  uint8_t v = ctx->v1.u8[1];
293  while (v > 0) {
294  v--;
295  counts[v] += ctx->cnt;
296  }
297 
298  break;
299  }
301  {
302  uint8_t v = ctx->v1.u8[1];
303  while (v < UINT8_MAX) {
304  v++;
305  counts[v] += ctx->cnt;
306  }
307 
308  break;
309  }
311  {
312  if (ctx->v1.u8[1] < ctx->v1.u8[2]) {
313  // ctx->v1.u8[1] is not UINT8_MAX
314  uint8_t v = ctx->v1.u8[1] + 1;
315  while (v < ctx->v1.u8[2]) {
316  counts[v] += ctx->cnt;
317  v++;
318  }
319  }
320  break;
321  }
322  }
323  }
324 
325  SetupEngineForPacketHeaderPrefilterPacketU8HashCtx(
326  de_ctx, sgh, sm_type, mask, counts, Set, Compare, Match);
327 }
328 
329 static int PrefilterSetupPacketHeaderCommon(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type,
330  SignatureMask mask, void (*Set)(PrefilterPacketHeaderValue *v, void *),
331  bool (*Compare)(PrefilterPacketHeaderValue v, void *),
332  void (*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx), bool u8hash)
333 {
334  Signature *s = NULL;
335  uint32_t sig = 0;
336 
337  if (sgh == NULL)
338  return 0;
339 
340  /* first count how many engines we will need */
341 
342  HashListTable *hash_table = HashListTableInit(4096,
343  PrefilterPacketHeaderHashFunc,
344  PrefilterPacketHeaderCompareFunc,
345  PrefilterPacketHeaderFreeFunc);
346  if (hash_table == NULL)
347  return -1;
348 
349  for (sig = 0; sig < sgh->init->sig_cnt; sig++) {
350  s = sgh->init->match_array[sig];
351  if (s == NULL)
352  continue;
353  if (s->init_data->prefilter_sm == NULL || s->init_data->prefilter_sm->type != sm_type)
354  continue;
355 
357  memset(&ctx, 0, sizeof(ctx));
358  Set(&ctx.v1, s->init_data->prefilter_sm->ctx);
359 
360  GetExtraMatch(s, &ctx.type, &ctx.value);
361 
362  PrefilterPacketHeaderHashCtx *rctx = HashListTableLookup(hash_table, (void *)&ctx, 0);
363  if (rctx != 0) {
364  rctx->cnt++;
365  } else {
366  PrefilterPacketHeaderHashCtx *actx = SCCalloc(1, sizeof(*actx));
367  if (actx == NULL)
368  goto error;
369 
370  Set(&actx->v1, s->init_data->prefilter_sm->ctx);
371  actx->cnt = 1;
372  actx->type = ctx.type;
373  actx->value = ctx.value;
374 
375  int ret = HashListTableAdd(hash_table, actx, 0);
376  if (ret != 0) {
377  SCFree(actx);
378  goto error;
379  }
380  }
381  }
382 
383  if (!u8hash) {
384  SetupSingle(de_ctx, hash_table, sgh, sm_type, mask, Compare, Match);
385  } else {
386  SetupU8Hash(de_ctx, hash_table, sgh, sm_type, mask, Set, Compare, Match);
387  }
388 
389  HashListTableFree(hash_table);
390  return 0;
391 error:
392  HashListTableFree(hash_table);
393  return -1;
394 }
395 
397  SignatureMask mask, void (*Set)(PrefilterPacketHeaderValue *v, void *),
398  bool (*Compare)(PrefilterPacketHeaderValue v, void *),
399  void (*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
400 {
401  return PrefilterSetupPacketHeaderCommon(de_ctx, sgh, sm_type, mask, Set, Compare, Match, true);
402 }
403 
405  SignatureMask mask, void (*Set)(PrefilterPacketHeaderValue *v, void *),
406  bool (*Compare)(PrefilterPacketHeaderValue v, void *),
407  void (*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
408 {
409  return PrefilterSetupPacketHeaderCommon(de_ctx, sgh, sm_type, mask, Set, Compare, Match, false);
410 }
PrefilterPacketHeaderHashCtx
struct PrefilterPacketHeaderHashCtx_ PrefilterPacketHeaderHashCtx
HashListTableGetListData
#define HashListTableGetListData(hb)
Definition: util-hashlist.h:56
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
Signature_::num
SigIntId num
Definition: detect.h:613
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1460
SignatureInitData_::prefilter_sm
SigMatch * prefilter_sm
Definition: detect.h:567
Signature_::alproto
AppProto alproto
Definition: detect.h:606
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
DetectPort_::port
uint16_t port
Definition: detect.h:218
ctx
struct Thresholds ctx
PREFILTER_U8HASH_MODE_RA
#define PREFILTER_U8HASH_MODE_RA
Definition: detect-engine-prefilter-common.h:59
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
HashListTableGetListHead
HashListTableBucket * HashListTableGetListHead(HashListTable *ht)
Definition: util-hashlist.c:287
PrefilterPacketHeaderValue::u8
uint8_t u8[16]
Definition: detect-engine-prefilter-common.h:24
DetectPort_::next
struct DetectPort_ * next
Definition: detect.h:231
PREFILTER_EXTRA_MATCH_SRCPORT
#define PREFILTER_EXTRA_MATCH_SRCPORT
Definition: detect-engine-prefilter-common.h:32
HashListTableLookup
void * HashListTableLookup(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:245
DetectPort_::port2
uint16_t port2
Definition: detect.h:219
detect-engine-prefilter.h
SigsArray_::offset
uint32_t offset
Definition: detect-engine-prefilter-common.h:49
DetectPort_::flags
uint8_t flags
Definition: detect.h:221
HashListTableAdd
int HashListTableAdd(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:114
HashListTable_::array_size
uint32_t array_size
Definition: util-hashlist.h:41
HashListTableGetListNext
#define HashListTableGetListNext(hb)
Definition: util-hashlist.h:55
PrefilterPacketHeaderHashCtx_
Definition: detect-engine-prefilter-common.c:22
HashListTableInit
HashListTable * HashListTableInit(uint32_t size, uint32_t(*Hash)(struct HashListTable_ *, void *, uint16_t), char(*Compare)(void *, uint16_t, void *, uint16_t), void(*Free)(void *))
Definition: util-hashlist.c:35
PrefilterPacketHeaderCtx_
Definition: detect-engine-prefilter-common.h:35
PREFILTER_EXTRA_MATCH_DSTPORT
#define PREFILTER_EXTRA_MATCH_DSTPORT
Definition: detect-engine-prefilter-common.h:33
SigGroupHeadInitData_::sig_cnt
SigIntId sig_cnt
Definition: detect.h:1453
PrefilterSetupPacketHeaderU8Hash
int PrefilterSetupPacketHeaderU8Hash(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))
Definition: detect-engine-prefilter-common.c:396
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1093
PrefilterPacketHeaderHashCtx_::v1
PrefilterPacketHeaderValue v1
Definition: detect-engine-prefilter-common.c:23
SigGroupHead_::init
SigGroupHeadInitData * init
Definition: detect.h:1483
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:352
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
Signature_::flags
uint32_t flags
Definition: detect.h:602
Packet_
Definition: decode.h:473
type
uint16_t type
Definition: decode-vlan.c:107
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:670
PrefilterSetupPacketHeader
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))
Definition: detect-engine-prefilter-common.c:404
PrefilterAppendEngine
int PrefilterAppendEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, PrefilterPktFn PrefilterFunc, SignatureMask mask, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:208
HashListTable_
Definition: util-hashlist.h:37
Signature_::sp
DetectPort * sp
Definition: detect.h:642
PREFILTER_U8HASH_MODE_EQ
#define PREFILTER_U8HASH_MODE_EQ
Definition: detect-engine-prefilter-common.h:56
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
SigsArray_::sigs
SigIntId * sigs
Definition: detect-engine-prefilter-common.h:47
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:350
HashListTableFree
void HashListTableFree(HashListTable *ht)
Definition: util-hashlist.c:88
SigGroupHeadInitData_::match_array
Signature ** match_array
Definition: detect.h:1456
SigsArray_
Definition: detect-engine-prefilter-common.h:46
PrefilterPacketHeaderValue::u64
uint64_t u64[2]
Definition: detect-engine-prefilter-common.h:27
Signature_::dp
DetectPort * dp
Definition: detect.h:642
SCFree
#define SCFree(p)
Definition: util-mem.h:61
Signature_::id
uint32_t id
Definition: detect.h:636
HashListTableBucket_
Definition: util-hashlist.h:28
Signature_
Signature container.
Definition: detect.h:601
SignatureMask
#define SignatureMask
Definition: detect.h:311
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
PREFILTER_U8HASH_MODE_GT
#define PREFILTER_U8HASH_MODE_GT
Definition: detect-engine-prefilter-common.h:58
PORT_FLAG_NOT
#define PORT_FLAG_NOT
Definition: detect.h:213
PrefilterPacketU8HashCtx_
Definition: detect-engine-prefilter-common.h:52
PrefilterPacketHeaderValue
Definition: detect-engine-prefilter-common.h:23
detect-engine-prefilter-common.h
SigIntId
#define SigIntId
Definition: suricata-common.h:315
PrefilterPacketHeaderHashCtx_::cnt
uint32_t cnt
Definition: detect-engine-prefilter-common.c:28
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SIG_FLAG_PREFILTER
#define SIG_FLAG_PREFILTER
Definition: detect.h:274
PrefilterPacketHeaderHashCtx_::value
uint16_t value
Definition: detect-engine-prefilter-common.c:26
PREFILTER_EXTRA_MATCH_ALPROTO
#define PREFILTER_EXTRA_MATCH_ALPROTO
Definition: detect-engine-prefilter-common.h:31
PrefilterPacketHeaderHashCtx_::type
uint16_t type
Definition: detect-engine-prefilter-common.c:25
PREFILTER_U8HASH_MODE_LT
#define PREFILTER_U8HASH_MODE_LT
Definition: detect-engine-prefilter-common.h:57