45
45
int DetectFlowvarMatch (ThreadVars *, DetectEngineThreadCtx *, Packet *, Signature *, SigMatch *);
46
46
static int DetectFlowvarSetup (DetectEngineCtx *, Signature *, char *);
47
static int DetectFlowvarPostMatch(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, Packet *p, Signature *s, SigMatch *sm);
48
static void DetectFlowvarDataFree(void *ptr);
48
50
void DetectFlowvarRegister (void) {
49
51
sigmatch_table[DETECT_FLOWVAR].name = "flowvar";
50
52
sigmatch_table[DETECT_FLOWVAR].Match = DetectFlowvarMatch;
51
53
sigmatch_table[DETECT_FLOWVAR].Setup = DetectFlowvarSetup;
52
sigmatch_table[DETECT_FLOWVAR].Free = NULL;
54
sigmatch_table[DETECT_FLOWVAR].Free = DetectFlowvarDataFree;
53
55
sigmatch_table[DETECT_FLOWVAR].RegisterTests = NULL;
57
/* post-match for flowvar storage */
58
sigmatch_table[DETECT_FLOWVAR_POSTMATCH].name = "__flowvar__postmatch__";
59
sigmatch_table[DETECT_FLOWVAR_POSTMATCH].Match = DetectFlowvarPostMatch;
60
sigmatch_table[DETECT_FLOWVAR_POSTMATCH].Setup = NULL;
61
sigmatch_table[DETECT_FLOWVAR_POSTMATCH].Free = DetectFlowvarDataFree;
62
sigmatch_table[DETECT_FLOWVAR_POSTMATCH].RegisterTests = NULL;
280
/** \brief Store flowvar in det_ctx so we can exec it post-match */
281
int DetectFlowvarStoreMatch(DetectEngineThreadCtx *det_ctx, uint16_t idx, uint8_t *buffer, uint16_t len) {
282
DetectFlowvarList *fs = det_ctx->flowvarlist;
284
/* first check if we have had a previous match for this idx */
285
for ( ; fs != NULL; fs = fs->next) {
286
if (fs->idx == idx) {
287
/* we're replacing the older store */
295
fs = SCMalloc(sizeof(*fs));
296
if (unlikely(fs == NULL))
301
fs->next = det_ctx->flowvarlist;
302
det_ctx->flowvarlist = fs;
310
/** \brief Setup a post-match for flowvar storage
311
* We're piggyback riding the DetectFlowvarData struct
313
int DetectFlowvarPostMatchSetup(Signature *s, uint16_t idx) {
315
DetectFlowvarData *fv = NULL;
317
fv = SCMalloc(sizeof(DetectFlowvarData));
318
if (unlikely(fv == NULL))
320
memset(fv, 0x00, sizeof(*fv));
322
/* we only need the idx */
325
sm = SigMatchAlloc();
329
sm->type = DETECT_FLOWVAR_POSTMATCH;
330
sm->ctx = (void *)fv;
332
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_POSTMATCH);
339
* \brief post-match func to store flowvars in the flow
340
* \param sm sigmatch containing the idx to store
341
* \retval 1 or -1 in case of error
343
static int DetectFlowvarPostMatch(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, Packet *p, Signature *s, SigMatch *sm) {
344
DetectFlowvarList *fs, *prev;
345
DetectFlowvarData *fd;
347
if (det_ctx->flowvarlist == NULL || p->flow == NULL)
350
fd = (DetectFlowvarData *)sm->ctx;
353
fs = det_ctx->flowvarlist;
355
if (fd->idx == fs->idx) {
356
FlowVarAddStr(p->flow, fs->idx, fs->buffer, fs->len);
357
/* memory at fs->buffer is now the responsibility of
358
* the flowvar code. */
360
if (fs == det_ctx->flowvarlist) {
361
det_ctx->flowvarlist = fs->next;
363
fs = det_ctx->flowvarlist;
365
prev->next = fs->next;
377
/** \brief Clean flowvar candidate list in det_ctx */
378
void DetectFlowvarCleanupList(DetectEngineThreadCtx *det_ctx) {
379
DetectFlowvarList *fs, *next;
380
if (det_ctx->flowvarlist != NULL) {
381
fs = det_ctx->flowvarlist;
389
det_ctx->flowvarlist = NULL;