conf, fwd: Don't attempt to forward port 0

When using -t all, -u all or exclude-only ranges, we'll attempt to forward
all non-ephemeral port numbers, including port 0.  However, this won't work
as intended: bind() treats a zero port not as literal port 0, but as
"pick a port for me".  Because of the special meaning of port 0, we mostly
outright exclude it in our handling.

Do the same for setting up forwards, not attempting to forward for port 0.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Reviewed-by: Laurent Vivier <lvivier@redhat.com>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2024-08-29 19:58:46 +10:00 committed by Stefano Brivio
parent 1daf6f4615
commit 4a41dc58d6

10
conf.c
View file

@ -157,7 +157,10 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg,
fwd->mode = FWD_ALL; fwd->mode = FWD_ALL;
for (i = 0; i < NUM_PORTS; i++) { /* Skip port 0. It has special meaning for many socket APIs, so
* trying to bind it is not really safe.
*/
for (i = 1; i < NUM_PORTS; i++) {
if (fwd_port_is_ephemeral(i)) if (fwd_port_is_ephemeral(i))
continue; continue;
@ -262,7 +265,10 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg,
} while ((p = next_chunk(p, ','))); } while ((p = next_chunk(p, ',')));
if (exclude_only) { if (exclude_only) {
for (i = 0; i < NUM_PORTS; i++) { /* Skip port 0. It has special meaning for many socket APIs, so
* trying to bind it is not really safe.
*/
for (i = 1; i < NUM_PORTS; i++) {
if (fwd_port_is_ephemeral(i) || if (fwd_port_is_ephemeral(i) ||
bitmap_isset(exclude, i)) bitmap_isset(exclude, i))
continue; continue;