From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202606 header.b=m1ql4jrq; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 52BE95A0626 for ; Wed, 01 Jul 2026 07:32:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202606; t=1782883921; bh=g8Gpz1M4dCGc/wNEZnhry0m/vfOI4++YJJ84ndQio1k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m1ql4jrqHAKh1zh/pODojEx7Ciwdog7NCGRpNLf1mqWrFGYul5Cn5nSzRKSg8WzIU txjOfKofBEu51YmM+2DC+VmDrZ77DqWKg8r0hXKtTp5/lykDT0Ui+MoEeaHfk0LQQU L5VphGs1fYluur8DYQvbmPqDuykJ6GukqL4qxwiQXdn84tHfp/UDiZnV0YljUTmKli GRJ0w+qgf9lhO+cTp/eoRBPDIA2s0Iekt6PDYR0sLx1GYbQwTzdkCDbM8YNEFY6SXX Ceo0zF/llEZB+xg+IgXRG+F9owZeKXdq+iat/f/7fMexYaDh5sy5Hq3CVsuwNCz87Q urYjUOAZ8pCAA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4gqpYF1T1Mz58dg; Wed, 01 Jul 2026 15:32:01 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v2 11/13] conf: Use new parsing tools to handle -a option Date: Wed, 1 Jul 2026 15:31:53 +1000 Message-ID: <20260701053155.1219264-12-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260701053155.1219264-1-david@gibson.dropbear.id.au> References: <20260701053155.1219264-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 52F3W2XQCAEOF7JRGUY5QRYLRRX2WSDD X-Message-ID-Hash: 52F3W2XQCAEOF7JRGUY5QRYLRRX2WSDD 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 , Jon Maloy 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: The -a command line option can take either an address prefix, or a bare address. Current parsing of this is pretty awkward, using the special purpose helper inany_prefix_pton(). With the new incremental parsing helpers this can be done more naturally. Rework it to use them. This does requiring extending parse_inany() to parse_inany_() which also reports the format of the address as parse, as opposed to the family of the resulting address. This is so that ::ffff:192.0.1.1/112 will be correctly interpreted the same as 192.0.1.1/16, rather than the nonsensical 192.0.0.1/112. Cc: Jon Maloy Signed-off-by: David Gibson --- Makefile | 1 - conf.c | 57 +++++++++++++++++++++++++++++++++++++------------------- inany.c | 50 ------------------------------------------------- inany.h | 2 -- parse.c | 17 ++++++++++++++--- parse.h | 5 ++++- 6 files changed, 56 insertions(+), 76 deletions(-) diff --git a/Makefile b/Makefile index e2b22ddf..5757aeff 100644 --- a/Makefile +++ b/Makefile @@ -223,7 +223,6 @@ passt-repair.cppcheck: $(PASST_REPAIR_SRCS) $(PASST_REPAIR_HEADERS) seccomp_repa pesto.cppcheck: BASE_CPPFLAGS += -DPESTO pesto.cppcheck: CPPCHECK_FLAGS += --suppress=unusedFunction:bitmap.c pesto.cppcheck: CPPCHECK_FLAGS += --suppress=unusedFunction:inany.h -pesto.cppcheck: CPPCHECK_FLAGS += --suppress=unusedFunction:inany.c pesto.cppcheck: CPPCHECK_FLAGS += --suppress=unusedFunction:ip.h pesto.cppcheck: CPPCHECK_FLAGS += --suppress=unusedFunction:serialise.c pesto.cppcheck: CPPCHECK_FLAGS += --suppress=staticFunction:fwd_rule.c diff --git a/conf.c b/conf.c index d1889e0c..c4a36dee 100644 --- a/conf.c +++ b/conf.c @@ -1176,31 +1176,45 @@ int conf_tap_fd(const char *arg) */ static bool conf_addr(struct ctx *c, char *arg, uint8_t opt_n) { + unsigned long prefix_len; + const struct in_addr *a4; union inany_addr addr; + sa_family_t parse_af; const char *p = arg; - uint8_t prefix_len; bool is_prefix; - is_prefix = inany_prefix_pton(arg, &addr, &prefix_len); - - if (is_prefix && opt_n) - die("Redundant prefix length specification"); - - if (!is_prefix && - !(parse_inany(&p, &addr) && parse_eoi(p))) - die("Invalid address: %s", arg); - - if (opt_n && inany_v4(&addr)) - prefix_len = opt_n; - else if (!is_prefix) - prefix_len = inany_default_prefix_len(&addr); + if (!parse_inany_(&p, &addr, &parse_af)) + goto bad; + a4 = inany_v4(&addr); + + if ((is_prefix = parse_literal(&p, "/"))) { + /* Prefix length included in -a option */ + if (!parse_unsigned(&p, 10, &prefix_len)) + goto bad; + if (opt_n) + die("Redundant prefix length specification"); + if (parse_af == AF_INET) { + if (prefix_len > 32) + goto bad_prefix; + prefix_len += 96; + } else if (prefix_len > 128) { + goto bad_prefix; + } + } else { + /* Get prefix length from elsewhere */ + if (opt_n && a4) + prefix_len = opt_n; + else + prefix_len = inany_default_prefix_len(&addr); + } - if (inany_is_unspecified(&addr) || inany_is_multicast(&addr) || - inany_is_loopback(&addr) || IN6_IS_ADDR_V4COMPAT(&addr.a6)) - die("Invalid address: %s", arg); + if (!parse_eoi(p) || + !inany_is_unicast(&addr) || + inany_is_loopback(&addr)) + goto bad; - if (inany_v4(&addr)) { - c->ip4.addr = *inany_v4(&addr); + if (a4) { + c->ip4.addr = *a4; c->ip4.prefix_len = prefix_len - 96; c->ip4.addr_fixed = true; c->ip4.no_copy_addrs = true; @@ -1211,6 +1225,11 @@ static bool conf_addr(struct ctx *c, char *arg, uint8_t opt_n) } return is_prefix; + +bad_prefix: + die("Invalid prefix length: %s", arg); +bad: + die("Invalid guest address: %s", arg); } /** diff --git a/inany.c b/inany.c index 154f08b5..120c9387 100644 --- a/inany.c +++ b/inany.c @@ -70,53 +70,3 @@ const char *inany_ntop(const union inany_addr *src, char *dst, socklen_t size) return inet_ntop(AF_INET6, &src->a6, dst, size); } - -/** - * inany_prefix_pton() - Parse an IPv[46] address with prefix length - * @src: IPv[46] address and prefix length string in CIDR format - * @dst: Output buffer, filled with parsed address - * @prefix_len: Prefix length, to be filled in IPv6 format - * - * Return: 1 on success, 0 if no parseable address or prefix is found - */ -int inany_prefix_pton(const char *src, union inany_addr *dst, - uint8_t *prefix_len) -{ - char astr[INANY_ADDRSTRLEN] = { 0 }; - size_t alen = strcspn(src, "/"); - const char *pstr = &src[alen + 1]; - const char *p = astr; - unsigned long plen; - char *end; - - if (alen >= INANY_ADDRSTRLEN) - return 0; - - if (src[alen] != '/') - return 0; - - strncpy(astr, src, alen); - - /* Read prefix length */ - errno = 0; - plen = strtoul(pstr, &end, 10); - if (errno || *end || plen > 128) - return 0; - - /* Read address */ - if (inet_pton(AF_INET6, astr, dst)) { - if (inany_v4(dst) && plen < 96) - return 0; - *prefix_len = plen; - return 1; - } - - if (parse_inany(&p, dst) && parse_eoi(p)) { - if (plen > 32) - return 0; - *prefix_len = plen + 96; - return 1; - } - - return 0; -} diff --git a/inany.h b/inany.h index 93d98368..5b176ccf 100644 --- a/inany.h +++ b/inany.h @@ -303,7 +303,5 @@ static inline int inany_from_sockaddr(union inany_addr *dst, in_port_t *port, bool inany_matches(const union inany_addr *a, const union inany_addr *b); const char *inany_ntop(const union inany_addr *src, char *dst, socklen_t size); -int inany_prefix_pton(const char *src, union inany_addr *dst, - uint8_t *prefix_len); #endif /* INANY_H */ diff --git a/parse.c b/parse.c index c8d3a9b0..24178873 100644 --- a/parse.c +++ b/parse.c @@ -185,18 +185,29 @@ static bool parse_ipv6(const char **cursor, struct in6_addr *abuf) } /** - * parse_inany() - Parse an IPv4 or IPv6 address from a string + * parse_inany_() - Parse an IPv4 or IPv6 address from a string * @addr: On success, updated with parsed address + * @parse_af: On success, updated with the format of the parsed address + * + * @parseaf is updated to reflect the string format, not the final address + * family. So "::ffff:192.0.1.1", will set @parseaf to AF_INET6, despite being + * a IPv4-mapped address. */ -bool parse_inany(const char **cursor, union inany_addr *addr) +bool parse_inany_(const char **cursor, union inany_addr *addr, + sa_family_t *parse_af) { struct in_addr a4; - if (parse_ipv6(cursor, &addr->a6)) + if (parse_ipv6(cursor, &addr->a6)) { + if (parse_af) + *parse_af = AF_INET6; return true; + } if (parse_ipv4(cursor, &a4)) { *addr = inany_from_v4(a4); + if (parse_af) + *parse_af = AF_INET; return true; } diff --git a/parse.h b/parse.h index 2820a065..ab1d5adb 100644 --- a/parse.h +++ b/parse.h @@ -27,6 +27,9 @@ bool parse_eoi(const char *cursor); bool parse_unsigned(const char **cursor, int base, unsigned long *valp); bool parse_port_range(const char **cursor, struct port_range *range); bool parse_ipv4(const char **cursor, struct in_addr *abuf); -bool parse_inany(const char **cursor, union inany_addr *addr); +bool parse_inany_(const char **cursor, union inany_addr *addr, + sa_family_t *parse_af); + +#define parse_inany(cursor, addr) parse_inany_((cursor), (addr), NULL) #endif /* _PARSE_H */ -- 2.54.0