aboutgitcodebugslistschat
path: root/conf.c
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2023-11-03 13:22:56 +1100
committerStefano Brivio <sbrivio@redhat.com>2023-11-07 09:53:14 +0100
commite90f2770ae44de238a227f884e806637a2b80403 (patch)
tree215078d0b14fb196c2541bdc9f917cae11a505b1 /conf.c
parent26d86f1304af402878d143ba20a18ef59c444263 (diff)
downloadpasst-e90f2770ae44de238a227f884e806637a2b80403.tar
passt-e90f2770ae44de238a227f884e806637a2b80403.tar.gz
passt-e90f2770ae44de238a227f884e806637a2b80403.tar.bz2
passt-e90f2770ae44de238a227f884e806637a2b80403.tar.lz
passt-e90f2770ae44de238a227f884e806637a2b80403.tar.xz
passt-e90f2770ae44de238a227f884e806637a2b80403.tar.zst
passt-e90f2770ae44de238a227f884e806637a2b80403.zip
port_fwd: Move automatic port forwarding code to port_fwd.[ch]
The implementation of scanning /proc files to do automatic port forwarding is a bit awkwardly split between procfs_scan_listen() in util.c, get_bound_ports() and related functions in conf.c and the initial setup code in conf(). Consolidate all of this into port_fwd.h, which already has some related definitions, and a new port_fwd.c. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'conf.c')
-rw-r--r--conf.c85
1 files changed, 1 insertions, 84 deletions
diff --git a/conf.c b/conf.c
index 4d37af1..d3e6eb2 100644
--- a/conf.c
+++ b/conf.c
@@ -45,72 +45,6 @@
#include "log.h"
/**
- * get_bound_ports() - Get maps of ports with bound sockets
- * @c: Execution context
- * @ns: If set, set bitmaps for ports to tap/ns -- to init otherwise
- * @proto: Protocol number (IPPROTO_TCP or IPPROTO_UDP)
- */
-void get_bound_ports(struct ctx *c, int ns, uint8_t proto)
-{
- uint8_t *udp_map, *udp_excl, *tcp_map, *tcp_excl;
-
- if (ns) {
- udp_map = c->udp.fwd_in.f.map;
- udp_excl = c->udp.fwd_out.f.map;
- tcp_map = c->tcp.fwd_in.map;
- tcp_excl = c->tcp.fwd_out.map;
- } else {
- udp_map = c->udp.fwd_out.f.map;
- udp_excl = c->udp.fwd_in.f.map;
- tcp_map = c->tcp.fwd_out.map;
- tcp_excl = c->tcp.fwd_in.map;
- }
-
- if (proto == IPPROTO_UDP) {
- memset(udp_map, 0, PORT_BITMAP_SIZE);
- procfs_scan_listen(c, IPPROTO_UDP, V4, ns, udp_map, udp_excl);
- procfs_scan_listen(c, IPPROTO_UDP, V6, ns, udp_map, udp_excl);
-
- procfs_scan_listen(c, IPPROTO_TCP, V4, ns, udp_map, udp_excl);
- procfs_scan_listen(c, IPPROTO_TCP, V6, ns, udp_map, udp_excl);
- } else if (proto == IPPROTO_TCP) {
- memset(tcp_map, 0, PORT_BITMAP_SIZE);
- procfs_scan_listen(c, IPPROTO_TCP, V4, ns, tcp_map, tcp_excl);
- procfs_scan_listen(c, IPPROTO_TCP, V6, ns, tcp_map, tcp_excl);
- }
-}
-
-/**
- * struct get_bound_ports_ns_arg - Arguments for get_bound_ports_ns()
- * @c: Execution context
- * @proto: Protocol number (IPPROTO_TCP or IPPROTO_UDP)
- */
-struct get_bound_ports_ns_arg {
- struct ctx *c;
- uint8_t proto;
-};
-
-/**
- * get_bound_ports_ns() - Get maps of ports in namespace with bound sockets
- * @arg: See struct get_bound_ports_ns_arg
- *
- * Return: 0
- */
-static int get_bound_ports_ns(void *arg)
-{
- struct get_bound_ports_ns_arg *a = (struct get_bound_ports_ns_arg *)arg;
- struct ctx *c = a->c;
-
- if (!c->pasta_netns_fd)
- return 0;
-
- ns_enter(c);
- get_bound_ports(c, 1, a->proto);
-
- return 0;
-}
-
-/**
* next_chunk - Return the next piece of a string delimited by a character
* @s: String to search
* @c: Delimiter character
@@ -1235,7 +1169,6 @@ void conf(struct ctx *c, int argc, char **argv)
{"no-copy-addrs", no_argument, NULL, 19 },
{ 0 },
};
- struct get_bound_ports_ns_arg ns_ports_arg = { .c = c };
char userns[PATH_MAX] = { 0 }, netns[PATH_MAX] = { 0 };
bool copy_addrs_opt = false, copy_routes_opt = false;
enum port_fwd_mode fwd_default = FWD_NONE;
@@ -1814,23 +1747,7 @@ void conf(struct ctx *c, int argc, char **argv)
if (!c->udp.fwd_out.f.mode)
c->udp.fwd_out.f.mode = fwd_default;
- 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;
-
- if (c->tcp.fwd_in.mode == FWD_AUTO) {
- ns_ports_arg.proto = IPPROTO_TCP;
- NS_CALL(get_bound_ports_ns, &ns_ports_arg);
- }
- if (c->udp.fwd_in.f.mode == FWD_AUTO) {
- ns_ports_arg.proto = IPPROTO_UDP;
- NS_CALL(get_bound_ports_ns, &ns_ports_arg);
- }
- if (c->tcp.fwd_out.mode == FWD_AUTO)
- get_bound_ports(c, 0, IPPROTO_TCP);
- if (c->udp.fwd_out.f.mode == FWD_AUTO)
- get_bound_ports(c, 0, IPPROTO_UDP);
+ port_fwd_init(c);
if (!c->quiet)
conf_print(c);