aboutgitcodebugslistschat
path: root/conf.c
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2023-11-03 13:22:55 +1100
committerStefano Brivio <sbrivio@redhat.com>2023-11-07 09:52:59 +0100
commit26d86f1304af402878d143ba20a18ef59c444263 (patch)
treec89a251ab62c00571a36c6fe3c17cb49398b279e /conf.c
parent3fb3f0f7a59498bdea1d199eecfdbae6c608f78f (diff)
downloadpasst-26d86f1304af402878d143ba20a18ef59c444263.tar
passt-26d86f1304af402878d143ba20a18ef59c444263.tar.gz
passt-26d86f1304af402878d143ba20a18ef59c444263.tar.bz2
passt-26d86f1304af402878d143ba20a18ef59c444263.tar.lz
passt-26d86f1304af402878d143ba20a18ef59c444263.tar.xz
passt-26d86f1304af402878d143ba20a18ef59c444263.tar.zst
passt-26d86f1304af402878d143ba20a18ef59c444263.zip
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 <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'conf.c')
-rw-r--r--conf.c60
1 files changed, 27 insertions, 33 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_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;
+ 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;
+
+ 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);