Skip to content

Commit

Permalink
flow: add callbacks for flow init and flow updates
Browse files Browse the repository at this point in the history
Adds user registerable callbacks for flow initialization and flow
update.

Some plugins, such as other DPI libraries like nDPI need a way to hook
into these flow lifecycle events.

Ticket: #7319
Ticket: #7320
  • Loading branch information
jasonish committed Oct 10, 2024
1 parent 58556b7 commit 2907164
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ noinst_HEADERS = \
feature.h \
flow-bit.h \
flow-bypass.h \
flow-callbacks.h \
flow.h \
flow-hash.h \
flow-manager.h \
Expand Down Expand Up @@ -902,6 +903,7 @@ libsuricata_c_a_SOURCES = \
feature.c \
flow-bit.c \
flow-bypass.c \
flow-callbacks.c \
flow.c \
flow-hash.c \
flow-manager.c \
Expand Down
88 changes: 88 additions & 0 deletions src/flow-callbacks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* Copyright (C) 2024 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

#include "flow-callbacks.h"

typedef struct FlowInitCallback_ {
SCFlowInitCallbackFn fn;
struct FlowInitCallback_ *next;
} FlowInitCallback;

static FlowInitCallback *init_callbacks = NULL;

typedef struct FlowUpdateCallback_ {
SCFlowUpdateCallbackFn fn;
struct FlowUpdateCallback_ *next;
} FlowUpdateCallback;

static FlowUpdateCallback *update_callbacks = NULL;

bool SCFlowRegisterInitCallback(SCFlowInitCallbackFn fn)
{
FlowInitCallback *cb = SCCalloc(1, sizeof(*cb));
if (cb == NULL) {
return false;
}
cb->fn = fn;
if (init_callbacks == NULL) {
init_callbacks = cb;
} else {
FlowInitCallback *current = init_callbacks;
while (current->next != NULL) {
current = current->next;
}
current->next = cb;
}
return true;
}

void SCFlowRunInitCallbacks(Flow *f, const Packet *p)
{
FlowInitCallback *cb = init_callbacks;
while (cb != NULL) {
cb->fn(f, p);
cb = cb->next;
}
}

bool SCFlowRegisterUpdateCallback(SCFlowUpdateCallbackFn fn)
{
FlowUpdateCallback *cb = SCCalloc(1, sizeof(*cb));
if (cb == NULL) {
return false;
}
cb->fn = fn;
if (update_callbacks == NULL) {
update_callbacks = cb;
} else {
FlowUpdateCallback *current = update_callbacks;
while (current->next != NULL) {
current = current->next;
}
current->next = cb;
}
return true;
}

void SCFlowRunUpdateCallbacks(Flow *f, Packet *p, ThreadVars *tv)
{
FlowUpdateCallback *cb = update_callbacks;
while (cb != NULL) {
cb->fn(f, p, tv);
cb = cb->next;
}
}
60 changes: 60 additions & 0 deletions src/flow-callbacks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Copyright (C) 2024 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

#ifndef SURICATA_FLOW_CALLBACKS_H
#define SURICATA_FLOW_CALLBACKS_H

#include "suricata-common.h"
#include "flow.h"

typedef void (*SCFlowInitCallbackFn)(Flow *f, const Packet *p);

/** \brief Register a flow init callback.
*
* Register a user provided function to be called every time a flow is
* initialized for use.
*
* \returns true if callback was registered, otherwise false if the
* callback could not be registered due to memory allocation error.
*/
bool SCFlowRegisterInitCallback(SCFlowInitCallbackFn fn);

/** \internal
*
* Run all registered flow init callbacks.
*/
void SCFlowRunInitCallbacks(Flow *f, const Packet *p);

typedef void (*SCFlowUpdateCallbackFn)(Flow *f, Packet *p, ThreadVars *tv);

/** \brief Register a flow update callback.
*
* Register a user provided function to be called everytime a flow is
* updated.
*
* \returns true if callback was registered, otherwise false if the
* callback could not be registered due to memory allocation error.
*/
bool SCFlowRegisterUpdateCallback(SCFlowUpdateCallbackFn fn);

/** \internal
*
* Run all registered flow update callbacks.
*/
void SCFlowRunUpdateCallbacks(Flow *f, Packet *p, ThreadVars *tv);

#endif /* SURICATA_FLOW_CALLBACKS_H */
3 changes: 3 additions & 0 deletions src/flow-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "flow.h"
#include "flow-private.h"
#include "flow-util.h"
#include "flow-callbacks.h"
#include "flow-var.h"
#include "app-layer.h"

Expand Down Expand Up @@ -203,6 +204,8 @@ void FlowInit(Flow *f, const Packet *p)
FlowSetStorageById(f, MacSetGetFlowStorageID(), ms);
}

SCFlowRunInitCallbacks(f, p);

SCReturn;
}

Expand Down
3 changes: 3 additions & 0 deletions src/flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "flow-storage.h"
#include "flow-bypass.h"
#include "flow-spare-pool.h"
#include "flow-callbacks.h"

#include "stream-tcp-private.h"

Expand Down Expand Up @@ -503,6 +504,8 @@ void FlowHandlePacketUpdate(Flow *f, Packet *p, ThreadVars *tv, DecodeThreadVars
SCLogDebug("setting FLOW_NOPAYLOAD_INSPECTION flag on flow %p", f);
DecodeSetNoPayloadInspectionFlag(p);
}

SCFlowRunUpdateCallbacks(f, p, tv);
}

/** \brief Entry point for packet flow handling
Expand Down

0 comments on commit 2907164

Please sign in to comment.