Skip to content

Commit

Permalink
eve: revert ethernet addresses when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
regit committed Oct 15, 2023
1 parent 9c27082 commit d5fe84a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/output-json-flow.c
Expand Up @@ -281,7 +281,7 @@ static void EveFlowLogJSON(OutputJsonThreadCtx *aft, JsonBuilder *jb, Flow *f)
/* Close flow. */
jb_close(jb);

EveAddCommonOptions(&aft->ctx->cfg, NULL, f, jb);
EveAddCommonOptions(&aft->ctx->cfg, NULL, f, jb, LOG_DIR_FLOW);

/* TCP */
if (f->proto == IPPROTO_TCP) {
Expand Down
4 changes: 2 additions & 2 deletions src/output-json-netflow.c
Expand Up @@ -275,7 +275,7 @@ static int JsonNetFlowLogger(ThreadVars *tv, void *thread_data, Flow *f)
if (unlikely(jb == NULL))
return TM_ECODE_OK;
NetFlowLogEveToServer(jb, f);
EveAddCommonOptions(&jhl->ctx->cfg, NULL, f, jb);
EveAddCommonOptions(&jhl->ctx->cfg, NULL, f, jb, LOG_DIR_FLOW_TOSERVER);
OutputJsonBuilderBuffer(jb, jhl);
jb_free(jb);

Expand All @@ -285,7 +285,7 @@ static int JsonNetFlowLogger(ThreadVars *tv, void *thread_data, Flow *f)
if (unlikely(jb == NULL))
return TM_ECODE_OK;
NetFlowLogEveToClient(jb, f);
EveAddCommonOptions(&jhl->ctx->cfg, NULL, f, jb);
EveAddCommonOptions(&jhl->ctx->cfg, NULL, f, jb, LOG_DIR_FLOW_TOCLIENT);
OutputJsonBuilderBuffer(jb, jhl);
jb_free(jb);
}
Expand Down
43 changes: 38 additions & 5 deletions src/output-json.c
Expand Up @@ -79,7 +79,7 @@

static void OutputJsonDeInitCtx(OutputCtx *);
static void CreateEveCommunityFlowId(JsonBuilder *js, const Flow *f, const uint16_t seed);
static int CreateJSONEther(JsonBuilder *parent, const Packet *p, const Flow *f);
static int CreateJSONEther(JsonBuilder *parent, const Packet *p, const Flow *f, enum OutputJsonLogDirection dir);

static const char *TRAFFIC_ID_PREFIX = "traffic/id/";
static const char *TRAFFIC_LABEL_PREFIX = "traffic/label/";
Expand Down Expand Up @@ -413,13 +413,13 @@ void EveAddMetadata(const Packet *p, const Flow *f, JsonBuilder *js)
}

void EveAddCommonOptions(const OutputJsonCommonSettings *cfg,
const Packet *p, const Flow *f, JsonBuilder *js)
const Packet *p, const Flow *f, JsonBuilder *js, enum OutputJsonLogDirection dir)
{
if (cfg->include_metadata) {
EveAddMetadata(p, f, js);
}
if (cfg->include_ethernet) {
CreateJSONEther(js, p, f);
CreateJSONEther(js, p, f, dir);
}
if (cfg->include_community_id && f != NULL) {
CreateEveCommunityFlowId(js, f, cfg->community_id_seed);
Expand Down Expand Up @@ -742,14 +742,47 @@ static int MacSetIterateToJSON(uint8_t *val, MacSetSide side, void *data)
return 0;
}

static int CreateJSONEther(JsonBuilder *js, const Packet *p, const Flow *f)
static int CreateJSONEther(JsonBuilder *js, const Packet *p, const Flow *f, enum OutputJsonLogDirection dir)
{
if (p != NULL) {
/* this is a packet context, so we need to add scalar fields */
if (p->ethh != NULL) {
jb_open_object(js, "ether");
uint8_t *src = p->ethh->eth_src;
uint8_t *dst = p->ethh->eth_dst;
switch (dir) {
case LOG_DIR_FLOW:
if PKT_IS_TOCLIENT(p) {
src = p->ethh->eth_dst;
dst = p->ethh->eth_src;
} else {
src = p->ethh->eth_src;
dst = p->ethh->eth_dst;
}
break;
case LOG_DIR_FLOW_TOCLIENT:
if PKT_IS_TOCLIENT(p) {
src = p->ethh->eth_src;
dst = p->ethh->eth_dst;
} else {
src = p->ethh->eth_dst;
dst = p->ethh->eth_src;
}
break;
case LOG_DIR_FLOW_TOSERVER:
if PKT_IS_TOCLIENT(p) {
src = p->ethh->eth_dst;
dst = p->ethh->eth_src;
} else {
src = p->ethh->eth_src;
dst = p->ethh->eth_dst;
}
break;
case LOG_DIR_PACKET:
default:
src = p->ethh->eth_src;
dst = p->ethh->eth_dst;
}
JSONFormatAndAddMACAddr(js, "src_mac", src, false);
JSONFormatAndAddMACAddr(js, "dest_mac", dst, false);
jb_close(js);
Expand Down Expand Up @@ -863,7 +896,7 @@ JsonBuilder *CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir,
jb_set_string(js, "pkt_src", PktSrcToString(p->pkt_src));

if (eve_ctx != NULL) {
EveAddCommonOptions(&eve_ctx->cfg, p, f, js);
EveAddCommonOptions(&eve_ctx->cfg, p, f, js, dir);
}

return js;
Expand Down
2 changes: 1 addition & 1 deletion src/output-json.h
Expand Up @@ -111,7 +111,7 @@ TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data);
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data);

void EveAddCommonOptions(const OutputJsonCommonSettings *cfg,
const Packet *p, const Flow *f, JsonBuilder *js);
const Packet *p, const Flow *f, JsonBuilder *js, enum OutputJsonLogDirection dir);
void EveAddMetadata(const Packet *p, const Flow *f, JsonBuilder *js);

int OutputJSONMemBufferCallback(const char *str, size_t size, void *data);
Expand Down

0 comments on commit d5fe84a

Please sign in to comment.