From 26d86f1304af402878d143ba20a18ef59c444263 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Fri, 3 Nov 2023 13:22:55 +1100 Subject: [PATCH] conf: Cleaner initialisation of default forwarding modes Initialisation of the forwarding mode variables is complicated a bit by the fact that we have different defaults for passt and pasta modes. This leads to some debateably duplicated code between those two cases. More significantly, however, currently the final setting of the mode variable is interleaved with the code to actually start automatic scanning when that's selected. This essentially mingles "policy" code (setting the default mode), with implementation code (making that happen). That's a bit inflexible if we want to change policies in future. Disentangle these two pieces, and use a slightly different construction to make things briefer as well. Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- conf.c | 58 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/conf.c b/conf.c index a235b31..4d37af1 100644 --- a/conf.c +++ b/conf.c @@ -1238,6 +1238,7 @@ void conf(struct ctx *c, int argc, char **argv) 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; bool v4_only = false, v6_only = false; char *runas = NULL, *logfile = NULL; struct in6_addr *dns6 = c->ip6.dns; @@ -1252,6 +1253,7 @@ void conf(struct ctx *c, int argc, char **argv) if (c->mode == MODE_PASTA) { c->no_dhcp_dns = c->no_dhcp_dns_search = 1; + fwd_default = FWD_AUTO; optstring = "dqfel:hF:I:p:P:m:a:n:M:g:i:o:D:S:46t:u:T:U:"; } else { optstring = "dqfel:hs:F:p:P:m:a:n:M:g:i:o:D:S:461t:u:"; @@ -1803,40 +1805,32 @@ void conf(struct ctx *c, int argc, char **argv) if_indextoname(c->ifi6, c->pasta_ifn); } - if (c->mode == MODE_PASTA) { - 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) + c->tcp.fwd_in.mode = fwd_default; + if (!c->tcp.fwd_out.mode) + c->tcp.fwd_out.mode = fwd_default; + if (!c->udp.fwd_in.f.mode) + c->udp.fwd_in.f.mode = fwd_default; + if (!c->udp.fwd_out.f.mode) + c->udp.fwd_out.f.mode = fwd_default; - if (!c->tcp.fwd_in.mode || c->tcp.fwd_in.mode == FWD_AUTO) { - 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 || c->udp.fwd_in.f.mode == FWD_AUTO) { - 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 || c->tcp.fwd_out.mode == FWD_AUTO) { - c->tcp.fwd_out.mode = FWD_AUTO; - get_bound_ports(c, 0, IPPROTO_TCP); - } - if (!c->udp.fwd_out.f.mode || c->udp.fwd_out.f.mode == FWD_AUTO) { - c->udp.fwd_out.f.mode = FWD_AUTO; - get_bound_ports(c, 0, IPPROTO_UDP); - } - } else { - if (!c->tcp.fwd_in.mode) - c->tcp.fwd_in.mode = FWD_NONE; - if (!c->tcp.fwd_out.mode) - c->tcp.fwd_out.mode = FWD_NONE; - if (!c->udp.fwd_in.f.mode) - c->udp.fwd_in.f.mode = FWD_NONE; - if (!c->udp.fwd_out.f.mode) - c->udp.fwd_out.f.mode = FWD_NONE; + 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); if (!c->quiet) conf_print(c);