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=ASrjRHwv; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id AE96B5A0652 for ; Thu, 08 Jan 2026 03:29:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202512; t=1767839390; bh=imItYRSmtvkoFpwzQbwsLn1oHgs4SdB26ngGbJ9GT4g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ASrjRHwvEhMiXL7JR4v6U7TRXWkuPuDyCzTr5DY+2Ea3u/12+PtbVHNqTMOB7ySQr 33GeYZKcW/T7M1BGEBbxvgXFDAqawryPHUIB+9ncrGSiY4jDvERi7bEPzHU/gv0HTZ qov2uBLe5OmEfOXcI2IDcoQB4c6KiNKIkpXDeKasfDNN8QLWKy2uCZQMXMSinsla1Q Ajvdi8I05/boRqBcXWk5j5u8/q1UdxVKpHosNQDhUhSNFsVL3iakzCQh91GoMEJHK6 9mcfQXV/+N1vR51oAmADw1doxuwVpuDJNjbAyx7Qk43f+vTRGH8Q938CHZkh8JZ/50 R64daz45rI3FA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4dmplL3gDYz4wRF; Thu, 08 Jan 2026 13:29:50 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v3 05/14] fwd: Make space to store listening sockets in forward table Date: Thu, 8 Jan 2026 13:29:39 +1100 Message-ID: <20260108022948.2657573-6-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260108022948.2657573-1-david@gibson.dropbear.id.au> References: <20260108022948.2657573-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 45FCMCRSQCH54CNXQRTEJKHRR64HKMSS X-Message-ID-Hash: 45FCMCRSQCH54CNXQRTEJKHRR64HKMSS 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: At present, we don't keep track of the fds for listening sockets (except for "auto" ones). Since the fd is stored in the epoll reference, we didn't need an alternative source of it for the various handlers. However, we're intending to allow dynamic changes to forwarding configuration in future. That means we need a way to enumerate sockets so we can close them on removal of a forward. Extend our forwarding table data structure to make space for all the listening sockets. To avoid allocation, this imposes another limit: we could run out of space for socket fds before we run out of slots for forwarding rules. We don't actually do anything with the allocate spaced yet. For "auto" forwards it's redundant with existing arrays. We'll fix both of those in later patches. Signed-off-by: David Gibson --- fwd.c | 10 +++++++++- fwd.h | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/fwd.c b/fwd.c index 69aca441..f27a4220 100644 --- a/fwd.c +++ b/fwd.c @@ -345,6 +345,7 @@ void fwd_rule_add(struct fwd_ports *fwd, uint8_t flags, { /* Flags which can be set from the caller */ const uint8_t allowed_flags = FWD_WEAK | FWD_SCAN; + unsigned num = (unsigned)last - first + 1; struct fwd_rule *new; unsigned port; @@ -352,6 +353,8 @@ void fwd_rule_add(struct fwd_ports *fwd, uint8_t flags, if (fwd->count >= ARRAY_SIZE(fwd->rules)) die("Too many port forwarding ranges"); + if ((fwd->listen_sock_count + num) > ARRAY_SIZE(fwd->listen_socks)) + die("Too many listening sockets"); new = &fwd->rules[fwd->count++]; new->flags = flags; @@ -373,8 +376,13 @@ void fwd_rule_add(struct fwd_ports *fwd, uint8_t flags, new->to = to; + new->socks = &fwd->listen_socks[fwd->listen_sock_count]; + fwd->listen_sock_count += num; + for (port = new->first; port <= new->last; port++) { - /* Fill in the legacy data structures to match the table */ + new->socks[port - new->first] = -1; + + /* Fill in the legacy forwarding data structures to match the table */ if (!(new->flags & FWD_SCAN)) bitmap_set(fwd->map, port); fwd->delta[port] = new->to - new->first; diff --git a/fwd.h b/fwd.h index 94869c2a..3ddcb91d 100644 --- a/fwd.h +++ b/fwd.h @@ -23,6 +23,7 @@ bool fwd_port_is_ephemeral(in_port_t port); * @first: First port number to forward * @last: Last port number to forward * @to: Port number to forward port @first to. + * @socks: Array of listening sockets for this entry * @flags: Flag mask * FWD_DUAL_STACK_ANY - match any IPv4 or IPv6 address (@addr should be ::) * FWD_WEAK - Don't give an error if binds fail for some forwards @@ -34,6 +35,7 @@ struct fwd_rule { union inany_addr addr; char ifname[IFNAMSIZ]; in_port_t first, last, to; + int *socks; #define FWD_DUAL_STACK_ANY BIT(0) #define FWD_WEAK BIT(1) #define FWD_SCAN BIT(2) @@ -65,6 +67,13 @@ enum fwd_ports_mode { #define PORT_BITMAP_SIZE DIV_ROUND_UP(NUM_PORTS, 8) +/* Maximum number of listening sockets (per pif & protocol) + * + * Rationale: This lets us listen on every port for two addresses (which we need + * for -T auto without SO_BINDTODEVICE), plus a comfortable number of extras. + */ +#define MAX_LISTEN_SOCKS (NUM_PORTS * 3) + /** * fwd_ports() - Describes port forwarding for one protocol and direction * @mode: Overall forwarding mode (all, none, auto, specific ports) @@ -74,6 +83,8 @@ enum fwd_ports_mode { * @rules: Array of forwarding rules * @map: Bitmap describing which ports are forwarded * @delta: Offset between the original destination and mapped port number + * @listen_sock_count: Number of entries used in @listen_socks + * @listen_socks: Listening sockets for forwarding */ struct fwd_ports { enum fwd_ports_mode mode; @@ -83,6 +94,8 @@ struct fwd_ports { struct fwd_rule rules[MAX_FWD_RULES]; uint8_t map[PORT_BITMAP_SIZE]; in_port_t delta[NUM_PORTS]; + unsigned listen_sock_count; + int listen_socks[MAX_LISTEN_SOCKS]; }; #define FWD_PORT_SCAN_INTERVAL 1000 /* ms */ -- 2.52.0