Skip to content
38 changes: 29 additions & 9 deletions src/app-layer-detect-proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,15 @@ typedef struct AppLayerProtoDetectCtx_ {

/* Indicates the protocols that have registered themselves
* for protocol detection. This table is independent of the
* ipproto. */
const char *alproto_names[ALPROTO_MAX];
* ipproto. It should be allocated to contain ALPROTO_MAX
* protocols. */
const char **alproto_names;

/* Protocol expectations, like ftp-data on tcp.
* It should be allocated to contain ALPROTO_MAX
* app-layer protocols. For each protocol, an iptype
* is referenced (or 0 if there is no expectation). */
uint8_t *expectation_proto;
} AppLayerProtoDetectCtx;

typedef struct AppLayerProtoDetectAliases_ {
Expand Down Expand Up @@ -1718,6 +1725,15 @@ int AppLayerProtoDetectSetup(void)
}
}

alpd_ctx.alproto_names = SCCalloc(ALPROTO_MAX, sizeof(char *));
if (unlikely(alpd_ctx.alproto_names == NULL)) {
FatalError("Unable to alloc alproto_names.");
}
// to realloc when dynamic protos are added
alpd_ctx.expectation_proto = SCCalloc(ALPROTO_MAX, sizeof(uint8_t));
if (unlikely(alpd_ctx.expectation_proto == NULL)) {
FatalError("Unable to alloc expectation_proto.");
}
AppLayerExpectationSetup();

SCReturnInt(0);
Expand Down Expand Up @@ -1749,6 +1765,11 @@ int AppLayerProtoDetectDeSetup(void)
}
}

SCFree(alpd_ctx.alproto_names);
alpd_ctx.alproto_names = NULL;
SCFree(alpd_ctx.expectation_proto);
alpd_ctx.expectation_proto = NULL;

SpmDestroyGlobalThreadCtx(alpd_ctx.spm_global_thread_ctx);

AppLayerProtoDetectFreeAliases();
Expand All @@ -1762,6 +1783,7 @@ void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_n
{
SCEnter();

// should have just been realloced when dynamic protos is added
if (alpd_ctx.alproto_names[alproto] == NULL)
alpd_ctx.alproto_names[alproto] = alproto_name;

Expand Down Expand Up @@ -2111,27 +2133,25 @@ void AppLayerProtoDetectSupportedAppProtocols(AppProto *alprotos)
SCReturn;
}

uint8_t expectation_proto[ALPROTO_MAX];

static void AppLayerProtoDetectPEGetIpprotos(AppProto alproto,
uint8_t *ipprotos)
{
if (expectation_proto[alproto] == IPPROTO_TCP) {
if (alpd_ctx.expectation_proto[alproto] == IPPROTO_TCP) {
ipprotos[IPPROTO_TCP / 8] |= 1 << (IPPROTO_TCP % 8);
}
if (expectation_proto[alproto] == IPPROTO_UDP) {
if (alpd_ctx.expectation_proto[alproto] == IPPROTO_UDP) {
ipprotos[IPPROTO_UDP / 8] |= 1 << (IPPROTO_UDP % 8);
}
}

void AppLayerRegisterExpectationProto(uint8_t proto, AppProto alproto)
{
if (expectation_proto[alproto]) {
if (proto != expectation_proto[alproto]) {
if (alpd_ctx.expectation_proto[alproto]) {
if (proto != alpd_ctx.expectation_proto[alproto]) {
SCLogError("Expectation on 2 IP protocols are not supported");
}
}
expectation_proto[alproto] = proto;
alpd_ctx.expectation_proto[alproto] = proto;
}

/***** Unittests *****/
Expand Down
12 changes: 11 additions & 1 deletion src/app-layer-frames.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,25 @@
struct FrameConfig {
SC_ATOMIC_DECLARE(uint64_t, types);
};
static struct FrameConfig frame_config[ALPROTO_MAX];
/* This array should be allocated to contain ALPROTO_MAX protocols. */
static struct FrameConfig *frame_config;

void FrameConfigInit(void)
{
frame_config = SCCalloc(ALPROTO_MAX, sizeof(struct FrameConfig));
if (unlikely(frame_config == NULL)) {
FatalError("Unable to alloc frame_config.");
}
for (AppProto p = 0; p < ALPROTO_MAX; p++) {
SC_ATOMIC_INIT(frame_config[p].types);
}
}

void FrameConfigDeInit(void)
{
SCFree(frame_config);
}

void FrameConfigEnableAll(void)
{
const uint64_t bits = UINT64_MAX;
Expand Down
1 change: 1 addition & 0 deletions src/app-layer-frames.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ FramesContainer *AppLayerFramesGetContainer(Flow *f);
FramesContainer *AppLayerFramesSetupContainer(Flow *f);

void FrameConfigInit(void);
void FrameConfigDeInit(void);
void FrameConfigEnableAll(void);
void FrameConfigEnable(const AppProto p, const uint8_t type);

Expand Down
Loading
Loading