From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202512 header.b=LwVpyvuX; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id E61375A0624 for ; Thu, 15 Jan 2026 09:50:57 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202512; t=1768467049; bh=t4Z4o0e00EQPq6M0XkW17flE+GjkS8PIZbwha/zC5Lo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LwVpyvuX9FecJnUNYiea9ejHWXGGmtPFJOYoKCcafyxlNc3Dm80bSPc1fhGHMGcND 8Qli/Zf8e6UGukikAvwYgPRtIEfxJCoYO990FHpxhIzKBRpn9BqDZarQcLq7oIpWlg YmUQa6v04uYDLNpwzuMqcIXB7sMJ6+UwbyOwtrKkSqmCljH8QiQ9zZ63CKA7OpsCD9 vFWj7K0MpMQrGakaFbvXbMsXEGoTGPqfz9xcqXTtiX1dStMfZKYQ3INdfld5UB9dTl kKko91z8cIX2zRPZFNL2USZcu+aYj/cC8lSZqJt69ZuYZMs8s8KYKgIkiDQzXKXXTY ckRx2KNLjGcVw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4dsGsj41kJz4wMQ; Thu, 15 Jan 2026 19:50:49 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v4 10/14] fwd: Generate auto-forward exclusions from socket fd tables Date: Thu, 15 Jan 2026 19:50:41 +1100 Message-ID: <20260115085045.3309818-11-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260115085045.3309818-1-david@gibson.dropbear.id.au> References: <20260115085045.3309818-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: CIBKHNF2EI3DH2JEQMDXOIU53OANIGZX X-Message-ID-Hash: CIBKHNF2EI3DH2JEQMDXOIU53OANIGZX X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: When auto-forwarding based on port scans, we must exclude our own listening ports, to avoid circular forwards. Currently we use the (previous value of the) forwarding bitmaps for the reverse direction to determine that. Instead, generate it from the tables of listening sockets that we now maintain. For now this seems like a lot more work to get to the same place. However, it does mean we're basing our exclusions directly on the relevant information: which of the scanned listens belong to us. More importantly, it's a step towards removing the bitmaps entirely. Signed-off-by: David Gibson --- fwd.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/fwd.c b/fwd.c index 5b580ec7..575ae21a 100644 --- a/fwd.c +++ b/fwd.c @@ -683,6 +683,28 @@ static void fwd_scan_ports_udp(struct fwd_ports *fwd, bitmap_and_not(fwd->map, PORT_BITMAP_SIZE, fwd->map, exclude); } +/** + * current_listen_map() - Get bitmap of which ports we're already listening on + * @map: Bitmap to populate + * @fwd: Forwarding table to consider + */ +static void current_listen_map(uint8_t *map, const struct fwd_ports *fwd) +{ + unsigned i; + + memset(map, 0, PORT_BITMAP_SIZE); + + for (i = 0; i < fwd->count; i++) { + const struct fwd_rule *rule = &fwd->rules[i]; + unsigned port; + + for (port = rule->first; port <= rule->last; port++) { + if (rule->socks[port - rule->first] >= 0) + bitmap_set(map, port); + } + } +} + /** * fwd_scan_ports() - Scan automatic port forwarding information * @c: Execution context @@ -692,10 +714,10 @@ static void fwd_scan_ports(struct ctx *c) uint8_t excl_tcp_out[PORT_BITMAP_SIZE], excl_udp_out[PORT_BITMAP_SIZE]; uint8_t excl_tcp_in[PORT_BITMAP_SIZE], excl_udp_in[PORT_BITMAP_SIZE]; - memcpy(excl_tcp_out, c->tcp.fwd_in.map, sizeof(excl_tcp_out)); - memcpy(excl_tcp_in, c->tcp.fwd_out.map, sizeof(excl_tcp_in)); - memcpy(excl_udp_out, c->udp.fwd_in.map, sizeof(excl_udp_out)); - memcpy(excl_udp_in, c->udp.fwd_out.map, sizeof(excl_udp_in)); + current_listen_map(excl_tcp_out, &c->tcp.fwd_in); + current_listen_map(excl_tcp_in, &c->tcp.fwd_out); + current_listen_map(excl_udp_out, &c->udp.fwd_in); + current_listen_map(excl_udp_in, &c->udp.fwd_out); fwd_scan_ports_tcp(&c->tcp.fwd_out, excl_tcp_out); fwd_scan_ports_tcp(&c->tcp.fwd_in, excl_tcp_in); -- 2.52.0