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