suricata
util-mpm.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2014 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 #ifndef __UTIL_MPM_H__
25 #define __UTIL_MPM_H__
26 
27 #include "util-prefilter.h"
28 
29 #define MPM_INIT_HASH_SIZE 65536
30 
31 enum {
33 
34  /* aho-corasick */
39  /* table size */
41 };
42 
43 /* Internal Pattern Index: 0 to pattern_cnt-1 */
44 typedef uint32_t MpmPatternIndex;
45 
46 typedef struct MpmThreadCtx_ {
47  void *ctx;
48 
49  uint32_t memory_cnt;
50  uint32_t memory_size;
51 
53 
54 typedef struct MpmPattern_ {
55  /* length of the pattern */
56  uint16_t len;
57  /* flags decribing the pattern */
58  uint8_t flags;
59 
60  /* offset into the buffer where match may start */
61  uint16_t offset;
62 
63  /* offset into the buffer before which match much complete */
64  uint16_t depth;
65 
66  /* holds the original pattern that was added */
67  uint8_t *original_pat;
68  /* case sensitive */
69  uint8_t *cs;
70  /* case INsensitive */
71  uint8_t *ci;
72  /* pattern id */
73  uint32_t id;
74 
75  /* sid(s) for this pattern */
76  uint32_t sids_size;
78 
79  struct MpmPattern_ *next;
81 
82 /* Indicates if this a global mpm_ctx. Global mpm_ctx is the one that
83  * is instantiated when we use "single". Non-global is "full", i.e.
84  * one per sgh. */
85 #define MPMCTX_FLAGS_GLOBAL BIT_U8(0)
86 #define MPMCTX_FLAGS_NODEPTH BIT_U8(1)
87 
88 typedef struct MpmCtx_ {
89  void *ctx;
90  uint8_t mpm_type;
91 
92  uint8_t flags;
93 
94  uint16_t maxdepth;
95 
96  /* unique patterns */
97  uint32_t pattern_cnt;
98 
99  uint16_t minlen;
100  uint16_t maxlen;
101 
102  uint32_t memory_cnt;
103  uint32_t memory_size;
104 
105  uint32_t max_pat_id;
106 
107  /* hash used during ctx initialization */
110 
111 /* if we want to retrieve an unique mpm context from the mpm context factory
112  * we should supply this as the key */
113 #define MPM_CTX_FACTORY_UNIQUE_CONTEXT -1
114 
115 typedef struct MpmCtxFactoryItem {
116  const char *name;
119  int32_t id;
120  int32_t sm_list;
123 
124 typedef struct MpmCtxFactoryContainer_ {
126  int32_t no_of_items;
127  int32_t max_id;
129 
130 /** pattern is case insensitive */
131 #define MPM_PATTERN_FLAG_NOCASE 0x01
132 /** pattern is negated */
133 #define MPM_PATTERN_FLAG_NEGATED 0x02
134 /** pattern has a depth setting */
135 #define MPM_PATTERN_FLAG_DEPTH 0x04
136 /** pattern has an offset setting */
137 #define MPM_PATTERN_FLAG_OFFSET 0x08
138 /** one byte pattern (used in b2g) */
139 #define MPM_PATTERN_ONE_BYTE 0x10
140 /** the ctx uses it's own internal id instead of
141  * what is passed through the API */
142 #define MPM_PATTERN_CTX_OWNS_ID 0x20
143 
144 typedef struct MpmTableElmt_ {
145  const char *name;
146  void (*InitCtx)(struct MpmCtx_ *);
147  void (*InitThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *);
148  void (*DestroyCtx)(struct MpmCtx_ *);
149  void (*DestroyThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *);
150 
151  /** function pointers for adding patterns to the mpm ctx.
152  *
153  * \param mpm_ctx Mpm context to add the pattern to
154  * \param pattern pointer to the pattern
155  * \param pattern_len length of the pattern in bytes
156  * \param offset pattern offset setting
157  * \param depth pattern depth setting
158  * \param pid pattern id
159  * \param sid signature _internal_ id
160  * \param flags pattern flags
161  */
162  int (*AddPattern)(struct MpmCtx_ *, uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t);
163  int (*AddPatternNocase)(struct MpmCtx_ *, uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t);
164  int (*Prepare)(struct MpmCtx_ *);
165  uint32_t (*Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t);
166  void (*PrintCtx)(struct MpmCtx_ *);
167  void (*PrintThreadCtx)(struct MpmThreadCtx_ *);
168  void (*RegisterUnittests)(void);
169  uint8_t flags;
171 
173 extern int mpm_default_matcher;
174 
175 struct DetectEngineCtx_;
176 
177 int32_t MpmFactoryRegisterMpmCtxProfile(struct DetectEngineCtx_ *, const char *, const int);
181 int32_t MpmFactoryIsMpmCtxAvailable(const struct DetectEngineCtx_ *, const MpmCtx *);
182 
187 
188 void MpmTableSetup(void);
189 void MpmRegisterTests(void);
190 
191 void MpmInitCtx(MpmCtx *mpm_ctx, uint16_t matcher);
192 void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, uint16_t);
193 
194 int MpmAddPatternCS(struct MpmCtx_ *mpm_ctx, uint8_t *pat, uint16_t patlen,
195  uint16_t offset, uint16_t depth,
196  uint32_t pid, SigIntId sid, uint8_t flags);
197 int MpmAddPatternCI(struct MpmCtx_ *mpm_ctx, uint8_t *pat, uint16_t patlen,
198  uint16_t offset, uint16_t depth,
199  uint32_t pid, SigIntId sid, uint8_t flags);
200 
201 void MpmFreePattern(MpmCtx *mpm_ctx, MpmPattern *p);
202 
203 int MpmAddPattern(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen,
204  uint16_t offset, uint16_t depth, uint32_t pid,
205  SigIntId sid, uint8_t flags);
206 
207 #endif /* __UTIL_MPM_H__ */
MpmTableElmt_::PrintThreadCtx
void(* PrintThreadCtx)(struct MpmThreadCtx_ *)
Definition: util-mpm.h:167
mpm_default_matcher
int mpm_default_matcher
Definition: util-mpm.c:49
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:90
MpmPatternIndex
uint32_t MpmPatternIndex
Definition: util-mpm.h:44
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
MpmTableElmt_::InitThreadCtx
void(* InitThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *)
Definition: util-mpm.h:147
MpmTableElmt_::flags
uint8_t flags
Definition: util-mpm.h:169
MpmThreadCtx_
Definition: util-mpm.h:46
MpmAddPattern
int MpmAddPattern(MpmCtx *mpm_ctx, uint8_t *pat, uint16_t patlen, uint16_t offset, uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
Definition: util-mpm.c:427
MpmTableElmt_::name
const char * name
Definition: util-mpm.h:145
PrefilterRuleStore_
structure for storing potential rule matches
Definition: util-prefilter.h:32
PmqCleanup
void PmqCleanup(PrefilterRuleStore *)
Cleanup a Pmq.
Definition: util-prefilter.c:112
MpmThreadCtx_::memory_cnt
uint32_t memory_cnt
Definition: util-mpm.h:49
MpmAddPatternCI
int MpmAddPatternCI(struct MpmCtx_ *mpm_ctx, uint8_t *pat, uint16_t patlen, uint16_t offset, uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
Definition: util-mpm.c:250
MpmCtxFactoryContainer
struct MpmCtxFactoryContainer_ MpmCtxFactoryContainer
MpmTableElmt
struct MpmTableElmt_ MpmTableElmt
MpmAddPatternCS
int MpmAddPatternCS(struct MpmCtx_ *mpm_ctx, uint8_t *pat, uint16_t patlen, uint16_t offset, uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
Definition: util-mpm.c:241
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:758
MpmCtx_::memory_size
uint32_t memory_size
Definition: util-mpm.h:103
MpmTableElmt_::AddPatternNocase
int(* AddPatternNocase)(struct MpmCtx_ *, uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t)
Definition: util-mpm.h:163
MpmCtxFactoryItem::mpm_ctx_ts
MpmCtx * mpm_ctx_ts
Definition: util-mpm.h:117
MpmCtx_::maxlen
uint16_t maxlen
Definition: util-mpm.h:100
MpmTableElmt_::AddPattern
int(* AddPattern)(struct MpmCtx_ *, uint8_t *, uint16_t, uint16_t, uint16_t, uint32_t, SigIntId, uint8_t)
Definition: util-mpm.h:162
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:48
MpmCtxFactoryItem
struct MpmCtxFactoryItem MpmCtxFactoryItem
MPM_NOTSET
@ MPM_NOTSET
Definition: util-mpm.h:32
MpmPattern_::original_pat
uint8_t * original_pat
Definition: util-mpm.h:67
MpmCtx_::maxdepth
uint16_t maxdepth
Definition: util-mpm.h:94
MPM_AC_KS
@ MPM_AC_KS
Definition: util-mpm.h:37
MpmThreadCtx
struct MpmThreadCtx_ MpmThreadCtx
MpmTableElmt_::InitCtx
void(* InitCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:146
MPM_AC
@ MPM_AC
Definition: util-mpm.h:35
MpmPattern_::flags
uint8_t flags
Definition: util-mpm.h:58
MpmInitThreadCtx
void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, uint16_t)
Definition: util-mpm.c:195
MpmTableElmt_::PrintCtx
void(* PrintCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:166
MpmCtx_::max_pat_id
uint32_t max_pat_id
Definition: util-mpm.h:105
MpmTableSetup
void MpmTableSetup(void)
Definition: util-mpm.c:215
MpmInitCtx
void MpmInitCtx(MpmCtx *mpm_ctx, uint16_t matcher)
Definition: util-mpm.c:200
MpmPattern
struct MpmPattern_ MpmPattern
MpmCtxFactoryItem::name
const char * name
Definition: util-mpm.h:116
MpmPattern_::next
struct MpmPattern_ * next
Definition: util-mpm.h:79
MpmPattern_::id
uint32_t id
Definition: util-mpm.h:73
MpmThreadCtx_::memory_size
uint32_t memory_size
Definition: util-mpm.h:50
MpmFactoryIsMpmCtxAvailable
int32_t MpmFactoryIsMpmCtxAvailable(const struct DetectEngineCtx_ *, const MpmCtx *)
MpmCtxFactoryContainer_::items
MpmCtxFactoryItem * items
Definition: util-mpm.h:125
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
PmqReset
void PmqReset(PrefilterRuleStore *)
Reset a Pmq for reusage. Meant to be called after a single search.
Definition: util-prefilter.c:101
MpmPattern_::sids_size
uint32_t sids_size
Definition: util-mpm.h:76
MpmFreePattern
void MpmFreePattern(MpmCtx *mpm_ctx, MpmPattern *p)
Definition: util-mpm.c:349
MPM_HS
@ MPM_HS
Definition: util-mpm.h:38
MpmTableElmt_::Prepare
int(* Prepare)(struct MpmCtx_ *)
Definition: util-mpm.h:164
MpmPattern_
Definition: util-mpm.h:54
MpmCtxFactoryItem::sm_list
int32_t sm_list
Definition: util-mpm.h:120
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:165
MpmPattern_::ci
uint8_t * ci
Definition: util-mpm.h:71
MpmCtxFactoryItem::next
struct MpmCtxFactoryItem * next
Definition: util-mpm.h:121
util-prefilter.h
flags
uint8_t flags
Definition: decode-gre.h:0
MpmCtx_::pattern_cnt
uint32_t pattern_cnt
Definition: util-mpm.h:97
MpmFactoryReClaimMpmCtx
void MpmFactoryReClaimMpmCtx(const struct DetectEngineCtx_ *, MpmCtx *)
PmqFree
void PmqFree(PrefilterRuleStore *)
Cleanup and free a Pmq.
Definition: util-prefilter.c:125
MpmPattern_::offset
uint16_t offset
Definition: util-mpm.h:61
MpmPattern_::sids
SigIntId * sids
Definition: util-mpm.h:77
MpmRegisterTests
void MpmRegisterTests(void)
Definition: util-mpm.c:563
MpmPattern_::depth
uint16_t depth
Definition: util-mpm.h:64
MpmFactoryDeRegisterAllMpmCtxProfiles
void MpmFactoryDeRegisterAllMpmCtxProfiles(struct DetectEngineCtx_ *)
Definition: util-mpm.c:168
MpmTableElmt_::DestroyCtx
void(* DestroyCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:148
MpmCtxFactoryItem
Definition: util-mpm.h:115
MpmCtxFactoryContainer_::no_of_items
int32_t no_of_items
Definition: util-mpm.h:126
MPM_AC_BS
@ MPM_AC_BS
Definition: util-mpm.h:36
PmqSetup
int PmqSetup(PrefilterRuleStore *)
Setup a pmq.
Definition: util-prefilter.c:36
MpmCtxFactoryItem::id
int32_t id
Definition: util-mpm.h:119
MpmCtx_::memory_cnt
uint32_t memory_cnt
Definition: util-mpm.h:102
MpmCtx_::init_hash
MpmPattern ** init_hash
Definition: util-mpm.h:108
MpmPattern_::len
uint16_t len
Definition: util-mpm.h:56
MpmCtxFactoryItem::mpm_ctx_tc
MpmCtx * mpm_ctx_tc
Definition: util-mpm.h:118
MpmCtx
struct MpmCtx_ MpmCtx
MpmTableElmt_::DestroyThreadCtx
void(* DestroyThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *)
Definition: util-mpm.h:149
MPM_TABLE_SIZE
@ MPM_TABLE_SIZE
Definition: util-mpm.h:40
MpmCtxFactoryContainer_::max_id
int32_t max_id
Definition: util-mpm.h:127
MpmCtx_
Definition: util-mpm.h:88
MpmPattern_::cs
uint8_t * cs
Definition: util-mpm.h:69
SigIntId
#define SigIntId
Definition: suricata-common.h:292
MpmCtx_::flags
uint8_t flags
Definition: util-mpm.h:92
MpmFactoryRegisterMpmCtxProfile
int32_t MpmFactoryRegisterMpmCtxProfile(struct DetectEngineCtx_ *, const char *, const int)
Register a new Mpm Context.
Definition: util-mpm.c:59
MpmTableElmt_
Definition: util-mpm.h:144
MpmCtx_::ctx
void * ctx
Definition: util-mpm.h:89
MpmFactoryGetMpmCtxForProfile
MpmCtx * MpmFactoryGetMpmCtxForProfile(const struct DetectEngineCtx_ *, int32_t, int)
MpmThreadCtx_::ctx
void * ctx
Definition: util-mpm.h:47
MpmTableElmt_::RegisterUnittests
void(* RegisterUnittests)(void)
Definition: util-mpm.h:168
MpmCtxFactoryContainer_
Definition: util-mpm.h:124