From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id A91995A0275 for ; Fri, 3 Nov 2023 03:23:13 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1698978187; bh=9417+jqjqjQwC663pwiKyqbae38pKkA35nizPaKGreI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xnu882eYgRAO7Aqa3+PNR766W95P97BVptNQjMTKA/rIM3O4mYZwi0XTLEHDx/noR SGWetq4zBltf+UmKFlfwWXMj+fhiIeEVvNcOOYjABznG75GMF76pIce4DPCTIXmU4m ctxuyjwEpWSDdojYEbIg0WXT+rLSy3ICjKmvS2UU= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4SM4KR5Jsgz4xTy; Fri, 3 Nov 2023 13:23:07 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 1/9] conf: Cleaner initialisation of default forwarding modes Date: Fri, 3 Nov 2023 13:22:55 +1100 Message-ID: <20231103022303.968638-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231103022303.968638-1-david@gibson.dropbear.id.au> References: <20231103022303.968638-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: SY46QFBJR5VIP6NZMH5Q5BONS67NXL3R X-Message-ID-Hash: SY46QFBJR5VIP6NZMH5Q5BONS67NXL3R 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