From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id EE9265A0271 for ; Thu, 5 Oct 2023 05:44:54 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1696477489; bh=9417+jqjqjQwC663pwiKyqbae38pKkA35nizPaKGreI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B3IIhP2lYy5aL8dOh8oAH8wGx6zNOChHPnE42oGGgN1T+W8yTKCsmlyFnWCOgEWOz C0FXfRHVcOAQbHc6BU42ym2DJSeE3foutUgV10gThWWt3yTd+OZnerYpqiF2SBQq7p kpkd/z+x9Zg9LJ5vNGQ6Eop3n7F/MU310ikaGBuQ= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4S1HW510q5z4xT0; Thu, 5 Oct 2023 14:44:49 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 1/9] conf: Cleaner initialisation of default forwarding modes Date: Thu, 5 Oct 2023 14:44:37 +1100 Message-ID: <20231005034445.2015303-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231005034445.2015303-1-david@gibson.dropbear.id.au> References: <20231005034445.2015303-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: T2GJ5DAK4TZKEVYAGLFFMPLIKIWP33LW X-Message-ID-Hash: T2GJ5DAK4TZKEVYAGLFFMPLIKIWP33LW X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- conf.c | 60 ++++++++++++++++++++++++++-------------------------------- 1 file 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); -- 2.41.0