From 942bfdb801732e9076ebc90e9d8206513ddbcd90 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Fri, 31 Oct 2025 15:19:28 +1100 Subject: fwd: Check forwarding mode in fwd_scan_ports_*() rather than caller fwd_scan_ports() needs to check for FWD_AUTO mode before calling each scan function - otherwise it would clobber the forwarding bitmap which should retain the user's fixed configuration. Make this slightly cleaner and safer by moving the mode check into fwd_scan_ports_{tcp,udp}(). Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- fwd.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/fwd.c b/fwd.c index 4045308..7b6c40f 100644 --- a/fwd.c +++ b/fwd.c @@ -363,6 +363,9 @@ static void procfs_scan_listen(int fd, unsigned int lstate, uint8_t *map) static void fwd_scan_ports_tcp(struct fwd_ports *fwd, const struct fwd_ports *rev) { + if (fwd->mode != FWD_AUTO) + return; + memset(fwd->map, 0, PORT_BITMAP_SIZE); procfs_scan_listen(fwd->scan4, TCP_LISTEN, fwd->map); procfs_scan_listen(fwd->scan6, TCP_LISTEN, fwd->map); @@ -381,6 +384,9 @@ static void fwd_scan_ports_udp(struct fwd_ports *fwd, const struct fwd_ports *tcp_fwd, const struct fwd_ports *tcp_rev) { + if (fwd->mode != FWD_AUTO) + return; + memset(fwd->map, 0, PORT_BITMAP_SIZE); procfs_scan_listen(fwd->scan4, UDP_LISTEN, fwd->map); procfs_scan_listen(fwd->scan6, UDP_LISTEN, fwd->map); @@ -408,18 +414,12 @@ static void fwd_scan_ports_udp(struct fwd_ports *fwd, */ static void fwd_scan_ports(struct ctx *c) { - if (c->tcp.fwd_out.mode == FWD_AUTO) - fwd_scan_ports_tcp(&c->tcp.fwd_out, &c->tcp.fwd_in); - if (c->tcp.fwd_in.mode == FWD_AUTO) - fwd_scan_ports_tcp(&c->tcp.fwd_in, &c->tcp.fwd_out); - if (c->udp.fwd_out.mode == FWD_AUTO) { - fwd_scan_ports_udp(&c->udp.fwd_out, &c->udp.fwd_in, - &c->tcp.fwd_out, &c->tcp.fwd_in); - } - if (c->udp.fwd_in.mode == FWD_AUTO) { - fwd_scan_ports_udp(&c->udp.fwd_in, &c->udp.fwd_out, - &c->tcp.fwd_in, &c->tcp.fwd_out); - } + fwd_scan_ports_tcp(&c->tcp.fwd_out, &c->tcp.fwd_in); + fwd_scan_ports_tcp(&c->tcp.fwd_in, &c->tcp.fwd_out); + fwd_scan_ports_udp(&c->udp.fwd_out, &c->udp.fwd_in, + &c->tcp.fwd_out, &c->tcp.fwd_in); + fwd_scan_ports_udp(&c->udp.fwd_in, &c->udp.fwd_out, + &c->tcp.fwd_in, &c->tcp.fwd_out); } /** -- cgit v1.2.3