From 183b826223fdfc9b47286b3cef6e27be82d2d41a Mon Sep 17 00:00:00 2001 From: Giuseppe Longo Date: Mon, 22 Dec 2025 23:18:31 +0100 Subject: [PATCH] flow: swap MACs when flow direction is swapped When FlowSwap() reverses the direction of a flow, the MAC address sets stored in the flow also need to be swapped to maintain consistency with the new direction. Previously, MAC addresses were not swapped along with other flow properties like packet/byte counters. Ticket #8172 (cherry picked from commit f1b9669ed5c509e63d3185ce4b2d60805aebc2ee) --- src/flow.c | 7 +++++++ src/util-macset.c | 23 +++++++++++++++++++++++ src/util-macset.h | 1 + 3 files changed, 31 insertions(+) diff --git a/src/flow.c b/src/flow.c index 154cda66f6..2c7255c43d 100644 --- a/src/flow.c +++ b/src/flow.c @@ -268,6 +268,13 @@ void FlowSwap(Flow *f) SWAP_VARS(uint32_t, f->todstpktcnt, f->tosrcpktcnt); SWAP_VARS(uint64_t, f->todstbytecnt, f->tosrcbytecnt); + + if (MacSetFlowStorageEnabled()) { + MacSet *ms = FlowGetStorageById(f, MacSetGetFlowStorageID()); + if (ms != NULL) { + MacSetSwap(ms); + } + } } /** diff --git a/src/util-macset.c b/src/util-macset.c index e4fd2b9715..23970af89f 100644 --- a/src/util-macset.c +++ b/src/util-macset.c @@ -291,6 +291,29 @@ void MacSetFree(MacSet *ms) (void)SC_ATOMIC_SUB(flow_memuse, total_free); } +void MacSetSwap(MacSet *ms) +{ + if (ms == NULL) + return; + + MacAddr tmp_single; + memcpy(tmp_single, ms->singles[0], sizeof(MacAddr)); + memcpy(ms->singles[0], ms->singles[1], sizeof(MacAddr)); + memcpy(ms->singles[1], tmp_single, sizeof(MacAddr)); + + MacSetState tmp_state = ms->state[0]; + ms->state[0] = ms->state[1]; + ms->state[1] = tmp_state; + + MacAddr *tmp_buf = ms->buf[0]; + ms->buf[0] = ms->buf[1]; + ms->buf[1] = tmp_buf; + + int tmp_last = ms->last[0]; + ms->last[0] = ms->last[1]; + ms->last[1] = tmp_last; +} + #ifdef UNITTESTS static int CheckTest1Membership(uint8_t *addr, MacSetSide side, void *data) diff --git a/src/util-macset.h b/src/util-macset.h index 49685cdbcc..ef1553bb67 100644 --- a/src/util-macset.h +++ b/src/util-macset.h @@ -41,6 +41,7 @@ void MacSetFree(MacSet *); void MacSetRegisterFlowStorage(void); FlowStorageId MacSetGetFlowStorageID(void); bool MacSetFlowStorageEnabled(void); +void MacSetSwap(MacSet *); void MacSetRegisterTests(void); #endif /* SURICATA_UTIL_MACSET_H */