udp: Consistently use -1 to indicate un-opened sockets in maps

udp uses the udp_tap_map, udp_splice_ns and udp_splice_init tables to keep
track of already opened sockets bound to specific ports.  We need a way to
indicate entries where a socket hasn't been opened, but the code isn't
consistent if this is indicated by a 0 or a -1:
  * udp_splice_sendfrom() and udp_tap_handler() assume that 0 indicates
    an unopened socket
  * udp_sock_init() fills in -1 for a failure to open a socket
  * udp_timer_one() is somewhere in between, treating only strictly
    positive fds as valid

-1 (or, at least, negative) is really the correct choice here, since 0 is
a theoretically valid fd value (if very unlikely in practice).  Change to
use that consistently throughout.

The table does need to be initialised to all -1 values before any calls to
udp_sock_init() which can happen from conf_ports().  Because C doesn't make
it easy to statically initialise non zero values in large tables, this does
require a somewhat awkward call to initialise the table from conf().  This
is the best approach I could see for the short term, with any luck it will
go away at some point when those socket tables are replaced by a unified
flow table.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2023-11-06 13:17:08 +11:00 committed by Stefano Brivio
parent c0426ff10b
commit 480aa4a108
3 changed files with 23 additions and 5 deletions

1
conf.c
View file

@ -1675,6 +1675,7 @@ void conf(struct ctx *c, int argc, char **argv)
c->no_map_gw = 1; c->no_map_gw = 1;
/* Inbound port options can be parsed now (after IPv4/IPv6 settings) */ /* Inbound port options can be parsed now (after IPv4/IPv6 settings) */
udp_portmap_clear();
optind = 1; optind = 1;
do { do {
name = getopt_long(argc, argv, optstring, options, NULL); name = getopt_long(argc, argv, optstring, options, NULL);

26
udp.c
View file

@ -238,6 +238,20 @@ static struct sockaddr_in6 udp6_localname = {
static struct mmsghdr udp4_mh_splice [UDP_MAX_FRAMES]; static struct mmsghdr udp4_mh_splice [UDP_MAX_FRAMES];
static struct mmsghdr udp6_mh_splice [UDP_MAX_FRAMES]; static struct mmsghdr udp6_mh_splice [UDP_MAX_FRAMES];
/**
* udp_portmap_clear() - Clear UDP port map before configuration
*/
void udp_portmap_clear(void)
{
unsigned i;
for (i = 0; i < NUM_PORTS; i++) {
udp_tap_map[V4][i].sock = udp_tap_map[V6][i].sock = -1;
udp_splice_ns[V4][i].sock = udp_splice_ns[V6][i].sock = -1;
udp_splice_init[V4][i].sock = udp_splice_init[V6][i].sock = -1;
}
}
/** /**
* udp_invert_portmap() - Compute reverse port translations for return packets * udp_invert_portmap() - Compute reverse port translations for return packets
* @fwd: Port forwarding configuration to compute reverse map for * @fwd: Port forwarding configuration to compute reverse map for
@ -522,7 +536,7 @@ static void udp_splice_sendfrom(const struct ctx *c, unsigned start, unsigned n,
if (from_pif == PIF_SPLICE) { if (from_pif == PIF_SPLICE) {
src += c->udp.fwd_in.rdelta[src]; src += c->udp.fwd_in.rdelta[src];
s = udp_splice_init[v6][src].sock; s = udp_splice_init[v6][src].sock;
if (!s && allow_new) if (s < 0 && allow_new)
s = udp_splice_new(c, v6, src, false); s = udp_splice_new(c, v6, src, false);
if (s < 0) if (s < 0)
@ -534,7 +548,7 @@ static void udp_splice_sendfrom(const struct ctx *c, unsigned start, unsigned n,
ASSERT(from_pif == PIF_HOST); ASSERT(from_pif == PIF_HOST);
src += c->udp.fwd_out.rdelta[src]; src += c->udp.fwd_out.rdelta[src];
s = udp_splice_ns[v6][src].sock; s = udp_splice_ns[v6][src].sock;
if (!s && allow_new) { if (s < 0 && allow_new) {
struct udp_splice_new_ns_arg arg = { struct udp_splice_new_ns_arg arg = {
c, v6, src, -1, c, v6, src, -1,
}; };
@ -849,7 +863,9 @@ int udp_tap_handler(struct ctx *c, uint8_t pif,
s_in.sin_addr = c->ip4.addr_seen; s_in.sin_addr = c->ip4.addr_seen;
} }
if (!(s = udp_tap_map[V4][src].sock)) { debug("UDP from tap src=%hu dst=%hu, s=%d",
src, dst, udp_tap_map[V4][src].sock);
if ((s = udp_tap_map[V4][src].sock) < 0) {
union udp_epoll_ref uref = { .port = src }; union udp_epoll_ref uref = { .port = src };
in_addr_t bind_addr = { 0 }; in_addr_t bind_addr = { 0 };
const char *bind_if = NULL; const char *bind_if = NULL;
@ -899,7 +915,7 @@ int udp_tap_handler(struct ctx *c, uint8_t pif,
bind_addr = &c->ip6.addr_ll; bind_addr = &c->ip6.addr_ll;
} }
if (!(s = udp_tap_map[V6][src].sock)) { if ((s = udp_tap_map[V6][src].sock) < 0) {
union udp_epoll_ref uref = { .v6 = 1, .port = src }; union udp_epoll_ref uref = { .v6 = 1, .port = src };
const char *bind_if = NULL; const char *bind_if = NULL;
@ -1161,7 +1177,7 @@ static void udp_timer_one(struct ctx *c, int v6, enum udp_act_type type,
return; return;
} }
if (s > 0) { if (s >= 0) {
epoll_ctl(c->epollfd, EPOLL_CTL_DEL, s, NULL); epoll_ctl(c->epollfd, EPOLL_CTL_DEL, s, NULL);
close(s); close(s);
bitmap_clear(udp_act[v6 ? V6 : V4][type], port); bitmap_clear(udp_act[v6 ? V6 : V4][type], port);

1
udp.h
View file

@ -8,6 +8,7 @@
#define UDP_TIMER_INTERVAL 1000 /* ms */ #define UDP_TIMER_INTERVAL 1000 /* ms */
void udp_portmap_clear(void);
void udp_sock_handler(const struct ctx *c, union epoll_ref ref, uint32_t events, void udp_sock_handler(const struct ctx *c, union epoll_ref ref, uint32_t events,
const struct timespec *now); const struct timespec *now);
int udp_tap_handler(struct ctx *c, uint8_t pif, int af, int udp_tap_handler(struct ctx *c, uint8_t pif, int af,