From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top
Subject: [PATCH 5/7] Clarify semantics of c->v4 and c->v6 variables
Date: Fri, 22 Jul 2022 15:31:16 +1000 [thread overview]
Message-ID: <20220722053118.1067459-6-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20220722053118.1067459-1-david@gibson.dropbear.id.au>
[-- Attachment #1: Type: text/plain, Size: 5239 bytes --]
The v4 and v6 fields of the context structure can be confusing, because
they change meaning part way through the code: Before conf_ip(), they are
booleans which indicate whether the -4 or -6 options have been given.
After conf_ip() they are DISABLED|ENABLED|PROBE enums which indicate
whether the IP version is available (which means both that it was allowed
on the command line and we were able to configure it). The PROBE variant
of the enum is only used locally within conf_ip() and since recent changes
there it no longer has a real purpose different from ENABLED.
Simplify this all by making the context fields always just a boolean
indicating the availability of the IP version. They both default to 1, but
can be set to 0 by either command line options or configuration failures.
We use some local variables in conf() for tracking the state of the command
line options on their own.
Signed-off-by: David Gibson <david(a)gibson.dropbear.id.au>
---
conf.c | 59 ++++++++++++++++++++--------------------------------------
util.h | 6 ------
2 files changed, 20 insertions(+), 45 deletions(-)
diff --git a/conf.c b/conf.c
index f5b761f..63f0f3d 100644
--- a/conf.c
+++ b/conf.c
@@ -26,6 +26,7 @@
#include <pwd.h>
#include <stdlib.h>
#include <stdint.h>
+#include <stdbool.h>
#include <unistd.h>
#include <syslog.h>
#include <time.h>
@@ -615,40 +616,25 @@ static int conf_ns_opt(struct ctx *c,
*/
static void conf_ip(struct ctx *c)
{
- int v4, v6;
-
if (c->v4) {
- c->v4 = IP_VERSION_ENABLED;
- v4 = IP_VERSION_PROBE;
- v6 = c->v6 = IP_VERSION_DISABLED;
- } else if (c->v6) {
- c->v6 = IP_VERSION_ENABLED;
- v6 = IP_VERSION_PROBE;
- v4 = c->v4 = IP_VERSION_DISABLED;
- } else {
- c->v4 = c->v6 = IP_VERSION_ENABLED;
- v4 = v6 = IP_VERSION_PROBE;
- }
-
- if (v4 != IP_VERSION_DISABLED) {
if (!c->ifi4)
c->ifi4 = nl_get_ext_if(AF_INET);
if (!c->ifi4) {
warn("No external routable interface for IPv4");
- v4 = IP_VERSION_DISABLED;
+ c->v4 = 0;
}
}
- if (v6 != IP_VERSION_DISABLED) {
+ if (c->v6) {
if (!c->ifi6)
c->ifi6 = nl_get_ext_if(AF_INET6);
if (!c->ifi6) {
warn("No external routable interface for IPv6");
- v6 = IP_VERSION_DISABLED;
+ c->v6 = 0;
}
}
- if (v4 != IP_VERSION_DISABLED) {
+ if (c->v4) {
if (!c->gw4)
nl_route(0, c->ifi4, AF_INET, &c->gw4);
@@ -676,7 +662,7 @@ static void conf_ip(struct ctx *c)
nl_link(0, c->ifi4, c->mac, 0, 0);
}
- if (v6 != IP_VERSION_DISABLED) {
+ if (c->v6) {
int prefix_len = 0;
if (IN6_IS_ADDR_UNSPECIFIED(&c->gw6))
@@ -694,25 +680,18 @@ static void conf_ip(struct ctx *c)
}
if (!c->gw4 || !c->addr4 || MAC_IS_ZERO(c->mac))
- v4 = IP_VERSION_DISABLED;
- else
- v4 = IP_VERSION_ENABLED;
+ c->v4 = 0;
if (IN6_IS_ADDR_UNSPECIFIED(&c->gw6) ||
IN6_IS_ADDR_UNSPECIFIED(&c->addr6) ||
IN6_IS_ADDR_UNSPECIFIED(&c->addr6_ll) ||
MAC_IS_ZERO(c->mac))
- v6 = IP_VERSION_DISABLED;
- else
- v6 = IP_VERSION_ENABLED;
+ c->v6 = 0;
- if ((v4 == IP_VERSION_DISABLED) && (v6 == IP_VERSION_DISABLED)) {
+ if (!c->v4 && !c->v6) {
err("External interface not usable");
exit(EXIT_FAILURE);
}
-
- c->v4 = v4;
- c->v6 = v6;
}
/**
@@ -1054,8 +1033,8 @@ void conf(struct ctx *c, int argc, char **argv)
{"no-ndp", no_argument, &c->no_ndp, 1 },
{"no-ra", no_argument, &c->no_ra, 1 },
{"no-map-gw", no_argument, &c->no_map_gw, 1 },
- {"ipv4-only", no_argument, &c->v4, '4' },
- {"ipv6-only", no_argument, &c->v6, '6' },
+ {"ipv4-only", no_argument, NULL, '4' },
+ {"ipv6-only", no_argument, NULL, '6' },
{"tcp-ports", required_argument, NULL, 't' },
{"udp-ports", required_argument, NULL, 'u' },
{"tcp-ns", required_argument, NULL, 'T' },
@@ -1083,6 +1062,7 @@ void conf(struct ctx *c, int argc, char **argv)
struct in6_addr *dns6 = c->dns6;
int name, ret, mask, b, i;
uint32_t *dns4 = c->dns4;
+ bool v4_only = false, v6_only = false;
if (c->mode == MODE_PASTA)
c->no_dhcp_dns = c->no_dhcp_dns_search = 1;
@@ -1474,10 +1454,10 @@ void conf(struct ctx *c, int argc, char **argv)
usage(argv[0]);
break;
case '4':
- c->v4 = 1;
+ v4_only = true;
break;
case '6':
- c->v6 = 1;
+ v6_only = true;
break;
case 't':
case 'u':
@@ -1508,11 +1488,6 @@ void conf(struct ctx *c, int argc, char **argv)
usage(argv[0]);
}
- if (c->v4 && c->v6) {
- err("Options ipv4-only and ipv6-only are mutually exclusive");
- usage(argv[0]);
- }
-
if (c->pasta_conf_ns)
c->no_ra = 1;
@@ -1524,6 +1499,12 @@ void conf(struct ctx *c, int argc, char **argv)
exit(EXIT_FAILURE);
}
+ if (v4_only && v6_only) {
+ err("Options ipv4-only and ipv6-only are mutually exclusive");
+ usage(argv[0]);
+ }
+ c->v4 = !v6_only;
+ c->v6 = !v4_only;
conf_ip(c);
/* Now we can process port configuration options */
diff --git a/util.h b/util.h
index ae4cc54..8297bec 100644
--- a/util.h
+++ b/util.h
@@ -74,12 +74,6 @@ void trace_init(int enable);
#define V6 1
#define IP_VERSIONS 2
-enum {
- IP_VERSION_DISABLED = 0,
- IP_VERSION_ENABLED,
- IP_VERSION_PROBE,
-};
-
#define ARRAY_SIZE(a) ((int)(sizeof(a) / sizeof((a)[0])))
#define IN_INTERVAL(a, b, x) ((x) >= (a) && (x) <= (b))
--
@@ -74,12 +74,6 @@ void trace_init(int enable);
#define V6 1
#define IP_VERSIONS 2
-enum {
- IP_VERSION_DISABLED = 0,
- IP_VERSION_ENABLED,
- IP_VERSION_PROBE,
-};
-
#define ARRAY_SIZE(a) ((int)(sizeof(a) / sizeof((a)[0])))
#define IN_INTERVAL(a, b, x) ((x) >= (a) && (x) <= (b))
--
2.37.1
next prev parent reply other threads:[~2022-07-22 5:31 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-22 5:31 [PATCH 0/7] Improved selection of external interface and IP configuration David Gibson
2022-07-22 5:31 ` [PATCH 1/7] Allow different external interfaces for IPv4 and IPv6 connectivity David Gibson
2022-07-22 5:31 ` [PATCH 2/7] Separately locate external interfaces for IPv4 and IPv6 David Gibson
2022-08-01 10:23 ` Stefano Brivio
2022-07-22 5:31 ` [PATCH 3/7] Initialize host side MAC when in IPv6 only mode David Gibson
2022-07-22 5:31 ` [PATCH 4/7] Move passt mac_guest init to be more symmetric with pasta David Gibson
2022-07-22 5:31 ` David Gibson [this message]
2022-08-01 10:24 ` [PATCH 5/7] Clarify semantics of c->v4 and c->v6 variables Stefano Brivio
2022-07-22 5:31 ` [PATCH 6/7] Separate IPv4 and IPv6 configuration David Gibson
2022-08-01 10:24 ` Stefano Brivio
2022-07-22 5:31 ` [PATCH 7/7] Make substructures for IPv4 and IPv6 specific context information David Gibson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220722053118.1067459-6-david@gibson.dropbear.id.au \
--to=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).