From dcf5c0eb1e877f1de84c94bb07c30d109cb85b70 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Fri, 3 Nov 2023 13:23:02 +1100 Subject: port_fwd: Move port scanning /proc fds into struct port_fwd Currently we store /proc/net fds used to implement automatic port forwarding in the proc_net_{tcp,udp} fields of the main context structure. However, in fact each of those is associated with a particular direction of forwarding, and we already have struct port_fwd which collects all other information related to a particular direction of port forwarding. We can simplify things a bit by moving the /proc fds into struct port_fwd. Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- passt.h | 5 ----- port_fwd.c | 62 ++++++++++++++++++++++++++++++++------------------------------ port_fwd.h | 4 ++++ 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/passt.h b/passt.h index 282bd1a..53defa4 100644 --- a/passt.h +++ b/passt.h @@ -203,8 +203,6 @@ struct ip6_ctx { * @no_netns_quit: In pasta mode, don't exit if fs-bound namespace is gone * @netns_base: Base name for fs-bound namespace, if any, in pasta mode * @netns_dir: Directory of fs-bound namespace, if any, in pasta mode - * @proc_net_tcp: Stored handles for /proc/net/tcp{,6} in init and ns - * @proc_net_udp: Stored handles for /proc/net/udp{,6} in init and ns * @epollfd: File descriptor for epoll instance * @fd_tap_listen: File descriptor for listening AF_UNIX socket, if any * @fd_tap: AF_UNIX socket, tuntap device, or pre-opened socket @@ -258,9 +256,6 @@ struct ctx { char netns_base[PATH_MAX]; char netns_dir[PATH_MAX]; - int proc_net_tcp[IP_VERSIONS][2]; - int proc_net_udp[IP_VERSIONS][2]; - int epollfd; int fd_tap_listen; int fd_tap; diff --git a/port_fwd.c b/port_fwd.c index 40e633d..9502463 100644 --- a/port_fwd.c +++ b/port_fwd.c @@ -74,19 +74,19 @@ static void procfs_scan_listen(int fd, unsigned int lstate, */ void get_bound_ports_tcp(struct ctx *c, int ns) { - uint8_t *map, *excl; + struct port_fwd *fwd, *rev; if (ns) { - map = c->tcp.fwd_in.map; - excl = c->tcp.fwd_out.map; + fwd = &c->tcp.fwd_in; + rev = &c->tcp.fwd_out; } else { - map = c->tcp.fwd_out.map; - excl = c->tcp.fwd_in.map; + fwd = &c->tcp.fwd_out; + rev = &c->tcp.fwd_in; } - memset(map, 0, PORT_BITMAP_SIZE); - procfs_scan_listen(c->proc_net_tcp[V4][ns], TCP_LISTEN, map, excl); - procfs_scan_listen(c->proc_net_tcp[V6][ns], TCP_LISTEN, map, excl); + memset(fwd->map, 0, PORT_BITMAP_SIZE); + procfs_scan_listen(fwd->scan4, TCP_LISTEN, fwd->map, rev->map); + procfs_scan_listen(fwd->scan6, TCP_LISTEN, fwd->map, rev->map); } /** @@ -96,27 +96,29 @@ void get_bound_ports_tcp(struct ctx *c, int ns) */ void get_bound_ports_udp(struct ctx *c, int ns) { - uint8_t *map, *excl; + struct port_fwd *fwd, *rev, *tcp; if (ns) { - map = c->udp.fwd_in.f.map; - excl = c->udp.fwd_out.f.map; + fwd = &c->udp.fwd_in.f; + rev = &c->udp.fwd_out.f; + tcp = &c->tcp.fwd_in; } else { - map = c->udp.fwd_out.f.map; - excl = c->udp.fwd_in.f.map; + fwd = &c->udp.fwd_out.f; + rev = &c->udp.fwd_in.f; + tcp = &c->tcp.fwd_out; } - memset(map, 0, PORT_BITMAP_SIZE); - procfs_scan_listen(c->proc_net_udp[V4][ns], UDP_LISTEN, map, excl); - procfs_scan_listen(c->proc_net_udp[V6][ns], UDP_LISTEN, map, excl); + memset(fwd->map, 0, PORT_BITMAP_SIZE); + procfs_scan_listen(fwd->scan4, UDP_LISTEN, fwd->map, rev->map); + procfs_scan_listen(fwd->scan6, UDP_LISTEN, fwd->map, rev->map); /* Also forward UDP ports with the same numbers as bound TCP ports. * This is useful for a handful of protocols (e.g. iperf3) where a TCP * control port is used to set up transfers on a corresponding UDP * port. */ - procfs_scan_listen(c->proc_net_tcp[V4][ns], TCP_LISTEN, map, excl); - procfs_scan_listen(c->proc_net_tcp[V6][ns], TCP_LISTEN, map, excl); + procfs_scan_listen(tcp->scan4, TCP_LISTEN, fwd->map, rev->map); + procfs_scan_listen(tcp->scan6, TCP_LISTEN, fwd->map, rev->map); } /** @@ -127,29 +129,29 @@ void port_fwd_init(struct ctx *c) { const int flags = O_RDONLY | O_CLOEXEC; - c->proc_net_tcp[V4][0] = c->proc_net_tcp[V4][1] = -1; - c->proc_net_tcp[V6][0] = c->proc_net_tcp[V6][1] = -1; - c->proc_net_udp[V4][0] = c->proc_net_udp[V4][1] = -1; - c->proc_net_udp[V6][0] = c->proc_net_udp[V6][1] = -1; + c->tcp.fwd_in.scan4 = c->tcp.fwd_in.scan6 = -1; + c->tcp.fwd_out.scan4 = c->tcp.fwd_out.scan6 = -1; + c->udp.fwd_in.f.scan4 = c->udp.fwd_in.f.scan6 = -1; + c->udp.fwd_out.f.scan4 = c->udp.fwd_out.f.scan6 = -1; if (c->tcp.fwd_in.mode == FWD_AUTO) { - c->proc_net_tcp[V4][1] = open_in_ns(c, "/proc/net/tcp", flags); - c->proc_net_tcp[V6][1] = open_in_ns(c, "/proc/net/tcp6", flags); + c->tcp.fwd_in.scan4 = open_in_ns(c, "/proc/net/tcp", flags); + c->tcp.fwd_in.scan6 = open_in_ns(c, "/proc/net/tcp6", flags); get_bound_ports_tcp(c, 1); } if (c->udp.fwd_in.f.mode == FWD_AUTO) { - c->proc_net_udp[V4][1] = open_in_ns(c, "/proc/net/udp", flags); - c->proc_net_udp[V6][1] = open_in_ns(c, "/proc/net/udp6", flags); + c->udp.fwd_in.f.scan4 = open_in_ns(c, "/proc/net/udp", flags); + c->udp.fwd_in.f.scan6 = open_in_ns(c, "/proc/net/udp6", flags); get_bound_ports_udp(c, 1); } if (c->tcp.fwd_out.mode == FWD_AUTO) { - c->proc_net_tcp[V4][0] = open("/proc/net/tcp", flags); - c->proc_net_tcp[V6][0] = open("/proc/net/tcp6", flags); + c->tcp.fwd_out.scan4 = open("/proc/net/tcp", flags); + c->tcp.fwd_out.scan6 = open("/proc/net/tcp6", flags); get_bound_ports_tcp(c, 0); } if (c->udp.fwd_out.f.mode == FWD_AUTO) { - c->proc_net_udp[V4][0] = open("/proc/net/udp", flags); - c->proc_net_udp[V6][0] = open("/proc/net/udp6", flags); + c->udp.fwd_out.f.scan4 = open("/proc/net/udp", flags); + c->udp.fwd_out.f.scan6 = open("/proc/net/udp6", flags); get_bound_ports_udp(c, 0); } } diff --git a/port_fwd.h b/port_fwd.h index 2f8f526..8ab6b48 100644 --- a/port_fwd.h +++ b/port_fwd.h @@ -22,11 +22,15 @@ enum port_fwd_mode { /** * port_fwd - Describes port forwarding for one protocol and direction * @mode: Overall forwarding mode (all, none, auto, specific ports) + * @scan4: /proc/net fd to scan for IPv4 ports when in AUTO mode + * @scan6: /proc/net fd to scan for IPv6 ports when in AUTO mode * @map: Bitmap describing which ports are forwarded * @delta: Offset between the original destination and mapped port number */ struct port_fwd { enum port_fwd_mode mode; + int scan4; + int scan6; uint8_t map[PORT_BITMAP_SIZE]; in_port_t delta[NUM_PORTS]; }; -- cgit v1.2.3