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=202512 header.b=QZvtcBa7; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 2376D5A0625 for ; Thu, 18 Dec 2025 05:22:25 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202512; t=1766031742; bh=hsMIieJubbRuRfCq6R9+D3Ki/SNfrv6fBK0WsKYDUTg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=QZvtcBa7Q/n/kICHvv0jhte5Q6JNwAkI2HBPolYVdMMTK44uWqk7ybEM+vPS6iTVQ ms0FnjrmtaUcgSnazXyscSZL35MTn+UgiGxjT8zxjtbdV/mOOBE/w8xM2Xnzy4cOg4 h9DUasDt/93dqsLUaM4zi8Ap3TWNCYfRd4ulPVgvH8G2mov2Ad4egGwb/iwXkBIoHB R8qJltOPVGEUS5fuBZA0gpOBgHY6eavAfmyqEx+ndo0LdYia5BOi8xzk5VnVDyD7/r eIKuOePuWjnJn22PszFqlGEkdKSh3UTW213VagQ5apH9kAKB+UoPKO4er6nOoZZsJJ X7LwgWrbh9n5g== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4dWyDt5xQJz4wD7; Thu, 18 Dec 2025 15:22:22 +1100 (AEDT) Date: Thu, 18 Dec 2025 15:22:16 +1100 From: David Gibson To: Jon Maloy Subject: Re: [PATCH v2] conf: Support CIDR notation for -a/--address option Message-ID: References: <20251217213142.358407-1-jmaloy@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="XJXNA+3bczUv5Com" Content-Disposition: inline In-Reply-To: <20251217213142.358407-1-jmaloy@redhat.com> Message-ID-Hash: I7IIBZXOJYC2ZMJBTC7YCAQ4LVSZI62T X-Message-ID-Hash: I7IIBZXOJYC2ZMJBTC7YCAQ4LVSZI62T 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: sbrivio@redhat.com, dgibson@redhat.com, passt-dev@passt.top 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: --XJXNA+3bczUv5Com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Dec 17, 2025 at 04:31:42PM -0500, Jon Maloy wrote: > Extend the -a/--address option to accept addresses in CIDR notation > (e.g., 192.168.1.1/24 or 2001:db8::1/64) as an alternative to using > separate -a and -n options. >=20 > Add conf_addr_prefix() helper function that: > - Parses address strings with optional /prefix suffix > - Validates prefix length based on address family (0-32 for IPv4, > 0-128 for IPv6) > - Returns address family and fills address/prefix output parameters >=20 > For IPv4, the prefix is stored in ip4.prefix_len when provided. > Multiple CIDR addresses use last-wins semantics for the prefix, > consistent with how addresses are handled currently. However, > mixing -n and CIDR notation results in an error to catch likely > user mistakes. >=20 > Also fix a bug in conf_ip4_prefix() that was incorrectly using the > global 'optarg' instead of its 'arg' parameter. Ouch. >=20 > Signed-off-by: Jon Maloy Missing man page update as Stefano mentioned, and a handful of nits below. >=20 > --- > v2: Fixed incorrect error printout, as noted by Laurent Vivier. > We now keep the old semantics, i.e., allowing multiple -a > options for each protocol. This semantics looks wrong, > but will anyway be fixed in my upcoming series. >=20 > Signed-off-by: Jon Maloy > --- > conf.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++--------- > 1 file changed, 84 insertions(+), 15 deletions(-) >=20 > diff --git a/conf.c b/conf.c > index 2942c8c..764b910 100644 > --- a/conf.c > +++ b/conf.c > @@ -682,7 +682,7 @@ static int conf_ip4_prefix(const char *arg) > return -1; > } else { > errno =3D 0; > - len =3D strtoul(optarg, NULL, 0); > + len =3D strtoul(arg, NULL, 0); > if (len > 32 || errno) > return -1; > } > @@ -690,6 +690,56 @@ static int conf_ip4_prefix(const char *arg) > return len; > } > =20 > +/** > + * conf_addr_prefix() - Parse address with optional /prefix notation > + * @arg: Address string, optionally with /prefix > + * @addr4: Output for IPv4 address > + * @addr6: Output for IPv6 address > + * @prefix: Output for prefix length (0 if not specified) > + * > + * Return: AF_INET for IPv4, AF_INET6 for IPv6, -1 on error > + */ > +static int conf_addr_prefix(const char *arg, struct in_addr *addr4, > + struct in6_addr *addr6, int *prefix) Would it make sense to use union inany_addr for the output? That would also let you reuse inany_pton(). > +{ > + char buf[INET6_ADDRSTRLEN + sizeof("/128")]; > + char *slash; > + > + *prefix =3D 0; > + > + if (snprintf(buf, sizeof(buf), "%s", arg) >=3D (int)sizeof(buf)) > + return -1; Complicated way of doing a strncpy. But more to the point, I don't think we need to copy the argument - we already add \0s in place in conf_ports(), so I think it's fine to do it here too. > + /* Check for /prefix suffix */ prefix length, as Stefano mentioned. > + slash =3D strchr(buf, '/'); > + if (slash) { > + unsigned long len; > + char *end; > + > + *slash =3D '\0'; > + errno =3D 0; > + len =3D strtoul(slash + 1, &end, 10); > + if (errno || *end) > + return -1; > + > + *prefix =3D len; > + } > + > + if (inet_pton(AF_INET6, buf, addr6) =3D=3D 1) { > + if (*prefix > 128) > + return -1; > + return AF_INET6; > + } > + > + if (inet_pton(AF_INET, buf, addr4) =3D=3D 1) { > + if (*prefix > 32) > + return -1; > + return AF_INET; > + } > + > + return -1; > +} > + > /** > * conf_ip4() - Verify or detect IPv4 support, get relevant addresses > * @ifi: Host interface to attempt (0 to determine one) > @@ -896,7 +946,7 @@ static void usage(const char *name, FILE *f, int stat= us) > " a zero value disables assignment\n" > " default: 65520: maximum 802.3 MTU minus 802.3 header\n" > " length, rounded to 32 bits (IPv4 words)\n" > - " -a, --address ADDR Assign IPv4 or IPv6 address ADDR\n" > + " -a, --address ADDR Assign IPv4 or IPv6 address ADDR[/PREFIX]\n" prefix length > " can be specified zero to two times (for IPv4 and IPv6)\n" > " default: use addresses from interface with default route\n" > " -n, --netmask MASK Assign IPv4 MASK, dot-decimal or bits\n" > @@ -1499,6 +1549,7 @@ void conf(struct ctx *c, int argc, char **argv) > const char *logname =3D (c->mode =3D=3D MODE_PASTA) ? "pasta" : "passt"; > char userns[PATH_MAX] =3D { 0 }, netns[PATH_MAX] =3D { 0 }; > bool copy_addrs_opt =3D false, copy_routes_opt =3D false; > + bool prefix_from_cidr =3D false, prefix_from_opt =3D false; > enum fwd_ports_mode fwd_default =3D FWD_NONE; > bool v4_only =3D false, v6_only =3D false; > unsigned dns4_idx =3D 0, dns6_idx =3D 0; > @@ -1808,23 +1859,38 @@ void conf(struct ctx *c, int argc, char **argv) > c->mtu =3D mtu; > break; > } > - case 'a': > - if (inet_pton(AF_INET6, optarg, &c->ip6.addr) && > - !IN6_IS_ADDR_UNSPECIFIED(&c->ip6.addr) && > - !IN6_IS_ADDR_LOOPBACK(&c->ip6.addr) && > - !IN6_IS_ADDR_V4MAPPED(&c->ip6.addr) && > - !IN6_IS_ADDR_V4COMPAT(&c->ip6.addr) && > - !IN6_IS_ADDR_MULTICAST(&c->ip6.addr)) { > + case 'a': { > + struct in6_addr addr6; > + struct in_addr addr4; > + int prefix =3D 0; > + int af; > + > + af =3D conf_addr_prefix(optarg, &addr4, &addr6, &prefix); > + > + if (af =3D=3D AF_INET6 && > + !IN6_IS_ADDR_UNSPECIFIED(&addr6) && > + !IN6_IS_ADDR_LOOPBACK(&addr6) && > + !IN6_IS_ADDR_V4MAPPED(&addr6) && > + !IN6_IS_ADDR_V4COMPAT(&addr6) && > + !IN6_IS_ADDR_MULTICAST(&addr6)) { > + c->ip6.addr =3D addr6; > if (c->mode =3D=3D MODE_PASTA) > c->ip6.no_copy_addrs =3D true; > break; > } > =20 > - if (inet_pton(AF_INET, optarg, &c->ip4.addr) && > - !IN4_IS_ADDR_UNSPECIFIED(&c->ip4.addr) && > - !IN4_IS_ADDR_BROADCAST(&c->ip4.addr) && > - !IN4_IS_ADDR_LOOPBACK(&c->ip4.addr) && > - !IN4_IS_ADDR_MULTICAST(&c->ip4.addr)) { > + if (af =3D=3D AF_INET && > + !IN4_IS_ADDR_UNSPECIFIED(&addr4) && > + !IN4_IS_ADDR_BROADCAST(&addr4) && > + !IN4_IS_ADDR_LOOPBACK(&addr4) && > + !IN4_IS_ADDR_MULTICAST(&addr4)) { > + c->ip4.addr =3D addr4; > + if (prefix) { > + if (prefix_from_opt) > + die("Can't use both -n and CIDR prefix"); > + c->ip4.prefix_len =3D prefix; > + prefix_from_cidr =3D true; > + } > if (c->mode =3D=3D MODE_PASTA) > c->ip4.no_copy_addrs =3D true; > break; > @@ -1832,11 +1898,14 @@ void conf(struct ctx *c, int argc, char **argv) > =20 > die("Invalid address: %s", optarg); > break; > + } > case 'n': > + if (prefix_from_cidr) > + die("Can't use both -n and CIDR prefix"); > c->ip4.prefix_len =3D conf_ip4_prefix(optarg); > if (c->ip4.prefix_len < 0) > die("Invalid netmask: %s", optarg); > - > + prefix_from_opt =3D true; > break; > case 'M': > parse_mac(c->our_tap_mac, optarg); > --=20 > 2.52.0 >=20 --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --XJXNA+3bczUv5Com Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmlDgXcACgkQzQJF27ox 2GerAxAAhZ+w64pNWAgZLvSsDbKnFj3UQcNDMuPD4Ia9YWdg7anszpIRqme8qQU6 bE9mHkPqP8oRb0RA898jUAiclboC5Qx2WOn7bMOfs/YpMDHhI5pj0YZIvhQTyGIz o80/k9NiNsUSV1bnja2l6UhusMZVskFLNDBtQA7J8E/LpjIQ48pGtdL2vCSP4bqf mbfQ9NimVEn419nhnkdO2TR7lmGQfbFulYcg9sr3sVeMEuOajzkhr77D5d9p50MB QARcpvBM4SHw69UiFyX82Pv0gMi0UtvVraP/k217wAbPCxClvpaltmQ0Sf2YaqPU IF0HJ1nxb58onLCi0wbYLSXJaCmNx2v/wOxDl2gfQo6++FM/B25EuKj+A7ZGR75w h/FhSZi/BXNZ5LIK+mUMV7Nm7cj5o0qSZpvD5HKKj5h+C5/CdtiHPwJCzEBpS4Kg 6mN7cdadgXh5bd4RMyS2tzauH9nYhmwDa7V0yt4EfRJEuqaIBepHO9AeLFj9A1pB lrvCYAPtw3FWhdGcEmszaYoc43KI3P/Fki+aUGOZNI4a+GsS4KYrwNVk19AAUMwr rEVM0B133szWt40sp5fqRFxouXlTY51vlEpsDlAMFUgfdTIgLoO1n+U90n9ljslF DkvMDNyuUM4ERZmhrOAmPhyu69tC0oAKUx4t9r+QAEyQaAsUnlw= =El0r -----END PGP SIGNATURE----- --XJXNA+3bczUv5Com--