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=202602 header.b=eRQxuenW; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 334D35A0265 for ; Mon, 18 May 2026 08:01:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1779084085; bh=uE/ulpWnqh69KVwzxIor8F8YySdaoE/+PRLCUoD/ZH4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=eRQxuenWWLPWHr/NS+YKlJSJrW38mCVRp+D6S0DaADhRXAz5mzgUEnxnrAUq2MRAf z4f5TWyji20u4CKPVSc9MOvkze6ZkLzJnUT6jBLYbRXYghaCbuIhr5y/wG669Dk7Az c41IxsPC8HCp0Fdc8qEKPrQn+6YnyvqZOV9eyb30hd97qPU7upYdNN1C3F8WPQI1KV StsL8InONxZ++tPsTE3X8KOHapgayKW4/hMGZ/XKAcLZ7ZPwXyLL+eADtzk3hHDD0W KYM/sZOqNl2DNI7ICX1S+SG+F6Huo13pQ3CqJECmErUSmEcBQqVy28LV4+xzZl00mn 2R310w+nnpVuA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4gJnHT6KnQz58ms; Mon, 18 May 2026 16:01:25 +1000 (AEST) Date: Mon, 18 May 2026 16:01:22 +1000 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH HACK] fwd, fwd_rule: Implement configurable destination address mapping Message-ID: References: <20260507055036.2110260-1-sbrivio@redhat.com> <20260515012835.087c0ae2@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="ef8kLf0ajQNPguLh" Content-Disposition: inline In-Reply-To: <20260515012835.087c0ae2@elisabeth> Message-ID-Hash: 656MNRJNEL2PSSRPL2MRP5IVRPWLFYEI X-Message-ID-Hash: 656MNRJNEL2PSSRPL2MRP5IVRPWLFYEI 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: Paul Holzinger , passt-dev@passt.top, 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: --ef8kLf0ajQNPguLh Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, May 15, 2026 at 01:28:36AM +0200, Stefano Brivio wrote: > On Thu, 14 May 2026 14:54:33 +1000 > David Gibson wrote: >=20 > > On Thu, May 07, 2026 at 07:50:36AM +0200, Stefano Brivio wrote: > > > This isn't complete, it's rather a quick hack to enable early > > > integration testing. > > >=20 > > > Add a 'daddr' field to forwarding rules, and some rudimentary parsing. > > >=20 > > > Format (for either command line or pesto): > > >=20 > > > -t 2222:192.0.2.1/2222 > > >=20 > > > This should work along with all the other bits, that is, say: > > >=20 > > > -t 192.0.2.1%eth0/2222-2225:192.0.2.2/22-25 > > >=20 > > > Signed-off-by: Stefano Brivio =20 > >=20 > > Very nice for a quick hack, and it gets surprisingly far with not much > > code at all. > >=20 > >=20 > > > --- > > > fwd.c | 4 +++- > > > fwd_rule.c | 46 +++++++++++++++++++++++++++++++++++----------- > > > fwd_rule.h | 2 ++ > > > 3 files changed, 40 insertions(+), 12 deletions(-) > > >=20 > > > diff --git a/fwd.c b/fwd.c > > > index d224c0a..75db350 100644 > > > --- a/fwd.c > > > +++ b/fwd.c > > > @@ -1095,7 +1095,9 @@ uint8_t fwd_nat_from_host(const struct ctx *c, > > > } > > > tgt->oport =3D ini->eport; > > > =20 > > > - if (inany_v4(&tgt->oaddr)) { > > > + if (!inany_is_unspecified(&rule->daddr)) { > > > + tgt->eaddr =3D rule->daddr; =20 > >=20 > > Longer term, there are at least two options for what we want to do > > if the rule doesn't specify a specific destination address: > > * Use the observed guest address (what we do now) >=20 > I would keep that as default for now. Of course. My point is that we'll need to have space in our forwarding rules for saying which approach we're using: Using address =3D= =3D :: can only encode one of them. > > * Use the host side destination address (potentially useful if we > > have multiple containers each assigned different host addresses) >=20 > This could be implemented on top of Jon's "multiple addresses" series I > think, even in terms of using multiple observed guest addresses, if > useful. Sure. > > So we'll probably want to allow for some new rule flags to cover > > options like this. >=20 > Rather than flags, I've been suggesting address pointers / references. > I'm not sure if flags are generic enough. My point is that there's non-address information as well. If you have a single translated address, fine, but there are multiple cases where you don't, so we need some other way to encode which "auto address" mode we're using. > At some point, we might want a syntax to refer to "the observed address > of container B". Even if we have just container A at the moment, maybe > we could start implementing something going in that direction. So, how we do this relates to two possible models for handling multiple containers. One is that each container has its own pif. In that case "observed address on pif XXX" is well defined, but is more or less equivalent to having a destination pif in the rule, which I think we want in any case. The other model is multiple containers (or guests or whatever) on the same pif, effectively bridged together. This is arguably less elegant than one-pif-per-container, but I think it's important to support for two reasons: * Integrates easily with existing solutions that already bridge a bunch of containers or VMs together. * It allows things routed via the container (e.g. over a VPN) to get access via as pasta as well In this case we can't define "observed address of container B" because we don't have a way to tell what came from container B other than the address they use. >=20 > > > + } else if (inany_v4(&tgt->oaddr)) { > > > tgt->eaddr =3D inany_from_v4(c->ip4.addr_seen); > > > } else { > > > if (inany_is_linklocal6(&tgt->oaddr)) > > > diff --git a/fwd_rule.c b/fwd_rule.c > > > index 5fc04d7..5bce2fb 100644 > > > --- a/fwd_rule.c > > > +++ b/fwd_rule.c > > > @@ -465,6 +465,7 @@ static int parse_keyword(const char *s, const cha= r **endptr, const char *kw) > > > */ > > > static void fwd_rule_range_except(struct fwd_table *fwd, bool del, > > > uint8_t proto, const union inany_addr *addr, > > > + const union inany_addr *daddr, > > > const char *ifname, > > > uint16_t first, uint16_t last, > > > const uint8_t *exclude, uint16_t to, > > > @@ -472,6 +473,7 @@ static void fwd_rule_range_except(struct fwd_tabl= e *fwd, bool del, > > > { > > > struct fwd_rule rule =3D { > > > .addr =3D addr ? *addr : inany_any6, > > > + .daddr =3D daddr ? *daddr : inany_any6, > > > .ifname =3D { 0 }, > > > .proto =3D proto, > > > .flags =3D flags, > > > @@ -544,13 +546,13 @@ fail: > > > */ > > > static void fwd_rule_parse_ports(struct fwd_table *fwd, bool del, ui= nt8_t proto, > > > const union inany_addr *addr, > > > - const char *ifname, > > > - const char *spec) > > > + const char *ifname, char *spec) > > > { > > > + union inany_addr daddr_buf =3D inany_any6, *daddr =3D &daddr_buf; > > > uint8_t exclude[PORT_BITMAP_SIZE] =3D { 0 }; > > > bool exclude_only =3D true; > > > - const char *p, *ep; > > > uint8_t flags =3D 0; > > > + char *p, *ep; > > > unsigned i; > > > =20 > > > if (!strcmp(spec, "all")) { > > > @@ -568,7 +570,7 @@ static void fwd_rule_parse_ports(struct fwd_table= *fwd, bool del, uint8_t proto, > > > continue; > > > } > > > =20 > > > - if (parse_keyword(p, &p, "auto") =3D=3D 0) { > > > + if (parse_keyword(p, (const char **)&p, "auto") =3D=3D 0) { > > > if (p !=3D ep) /* Garbage after the keyword */ > > > goto bad; > > > =20 > > > @@ -586,7 +588,7 @@ static void fwd_rule_parse_ports(struct fwd_table= *fwd, bool del, uint8_t proto, > > > goto bad; > > > p++; > > > =20 > > > - if (parse_port_range(p, &p, &xrange)) > > > + if (parse_port_range(p, (const char **)&p, &xrange)) > > > goto bad; > > > if (p !=3D ep) /* Garbage after the range */ > > > goto bad; > > > @@ -599,7 +601,7 @@ static void fwd_rule_parse_ports(struct fwd_table= *fwd, bool del, uint8_t proto, > > > /* Exclude ephemeral ports */ > > > fwd_port_map_ephemeral(exclude); > > > =20 > > > - fwd_rule_range_except(fwd, del, proto, addr, ifname, > > > + fwd_rule_range_except(fwd, del, proto, addr, NULL, ifname, > > > 1, NUM_PORTS - 1, exclude, > > > 1, flags | FWD_WEAK); > > > return; > > > @@ -613,11 +615,32 @@ static void fwd_rule_parse_ports(struct fwd_tab= le *fwd, bool del, uint8_t proto, > > > /* Already parsed */ > > > continue; > > > =20 > > > - if (parse_port_range(p, &p, &orig_range)) > > > + if (parse_port_range(p, (const char **)&p, &orig_range)) > > > goto bad; > > > =20 > > > - if (*p =3D=3D ':') { /* There's a range to map to as well */ > > > - if (parse_port_range(p + 1, &p, &mapped_range)) > > > + if (*p =3D=3D ':') { > > > + /* There's a range or address to map to as well */ > > > + char *addr_end =3D strchr(p, '/'); > > > + > > > + if (addr_end) { > > > + *addr_end =3D '\0'; > > > + > > > + if (*p =3D=3D '[' && p[strlen(p) - 1] =3D=3D ']') { > > > + p[strlen(p) - 1] =3D '\0'; > > > + p++; > > > + } > > > + > > > + if (!inany_pton(p + 1, daddr)) > > > + die("Bad forwarding address '%s'", p); > > > + > > > + p =3D addr_end; > > > + } else { > > > + daddr =3D NULL; > > > + } =20 > >=20 > > We probably want to factor some of this address parsing out into a > > helper, since we're now doing it twice. >=20 > Yes, definitely, and it should have its own small buffer. The in-place > parsing we have is a remnant of my original implementation (originally > intended for much simpler cases) but that's what forced me to > essentially copy and paste this if I wanted to implement this in a few > minutes. Yes. > > > + > > > + > > > + if (parse_port_range(p + 1, (const char **)&p, > > > + &mapped_range)) > > > goto bad; > > > if ((mapped_range.last - mapped_range.first) !=3D > > > (orig_range.last - orig_range.first)) > > > @@ -629,7 +652,7 @@ static void fwd_rule_parse_ports(struct fwd_table= *fwd, bool del, uint8_t proto, > > > if (p !=3D ep) /* Garbage after the ranges */ > > > goto bad; > > > =20 > > > - fwd_rule_range_except(fwd, del, proto, addr, ifname, > > > + fwd_rule_range_except(fwd, del, proto, addr, daddr, ifname, > > > orig_range.first, orig_range.last, > > > exclude, > > > mapped_range.first, flags); > > > @@ -675,7 +698,8 @@ void fwd_rule_parse(char optname, bool del, const= char *optarg, > > > =20 > > > strncpy(buf, optarg, sizeof(buf) - 1); > > > =20 > > > - if ((spec =3D strchr(buf, '/'))) { > > > + if ((spec =3D strchr(buf, '/')) && > > > + strchr(spec, ':') =3D=3D strchr(buf, ':')) { =20 > >=20 > > strchr() for ':' needs a lot of caution, since it can appear within > > IPv6 addresses. >=20 > Absolutely. This patch actually works only for IPv4. See also: >=20 > https://github.com/containers/container-libs/pull/755#issuecomment-4408= 900718 >=20 > We probably need separate helpers extracting different parts to keep > this sane (the LLM-sourced workaround shown there is probably correct > but pretty hard to audit and hard to reason about). Makes sense. > > > *spec =3D 0; > > > spec++; > > > =20 > > > diff --git a/fwd_rule.h b/fwd_rule.h > > > index ae9a3cb..3a2a809 100644 > > > --- a/fwd_rule.h > > > +++ b/fwd_rule.h > > > @@ -35,6 +35,7 @@ > > > /** > > > * struct fwd_rule - Forwarding rule governing a range of ports > > > * @addr: Address to forward from > > > + * @daddr: Optional address to set as destination when forwarding > > > * @ifname: Interface to forward from > > > * @first: First port number to forward > > > * @last: Last port number to forward > > > @@ -47,6 +48,7 @@ > > > */ > > > struct fwd_rule { > > > union inany_addr addr; > > > + union inany_addr daddr; =20 > >=20 > > We probably want to rethink the names here. Both of these are > > destination addresses, just one is host side the other is guest side > > (or, in general, they're destination addresses for two different > > pifs). "initiating" vs. "target" are probably the terms to use, since > > we already use that in the flow table. >=20 > I guess we'll want four addresses anyway, eventually, but that probably > needs the implementation of a separate address list with pointers that > I've been suggesting in the past to keep memory usage reasonable. I'm not so sure about this. Theoretically, yes, we could allow full four-address, four-port range rules. But it can get very messy: often we have NATs that are essentially independent of port remappings, and port remappings that are essentially independent of address. To handle that we either need n*m rules, which is pretty awful or applying multiple rules per flow which is pretty confusing. Although it's less general in principle, I think it will be more tractable in practice to have several different translation steps with their own rule tables, but none of them allowing the fully general four-address matching. > For the moment, there are two ways to distinguish these two destination > addresses, I think: >=20 > 1. outside / inside >=20 > 2. initiating / target >=20 > I was thinking that 1. is simpler to grasp as it refers to stable > concepts (host, container / guest) rather than how a connection came to > be (and client / server are much less universal than that). True, but it doesn't generalise well to multiple inside pifs (e.g. one per container). That's why I came up with initiating / target for the flow table, and I think we should re-use it here. > Going with that convention means that things will be swapped in terms > of 2., though, for outbound forwarding. I still think it's preferable. >=20 > > At some point I'm pretty sure we'll also want to put a target pif in > > the table. Not sure if we want to introduce that initially, or > > whether just an address makes sense for the first cut. >=20 > I don't think it's really important for the table itself (I would add > it if it doesn't add cachelines, and otherwise skip it or add it > commented out), but we should make sure the command line extensions > we're introducing will allow for that, or backwards compatibility will > be complicated to achieve later. Right. > > > char ifname[IFNAMSIZ]; > > > in_port_t first; > > > in_port_t last; > > > --=20 > > > 2.43.0 >=20 > Note that I'm not working on this right now as it's not immediately > helpful: >=20 > https://github.com/containers/container-libs/pull/755#issuecomment-4421= 500913 >=20 > so I don't plan to post further versions of this, at least not any time > soon. Feel free to pick it up from here, if it makes sense. Understood. --=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 --ef8kLf0ajQNPguLh Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmoKqzEACgkQzQJF27ox 2Ge4Fg/8CZ1h09NeBIbmaRLl5roGwR+Nmd6BLqkKcMrHPHTjagwFeRaPZjBTKL+n YmTVlgOg6h6EdE04EUAJI1Hvbz4HttNf3WKs/uXjPxiCHtBaPAb14grZBUbZ/LFW 3rjqaNAaST9a2U9fROGbaHARw7HV8zFMRwLkVLN8rTm5u8PiGFb/aDgoC862UF4T +uWtX5MB9jMkx8vCfg8NTd93huw+ie+a/uCKcfO+XD8DDku+epWrZ7yzhGIbEjWd Aib4EMzIo0BX66leSjwQMKXtUlkERgO813/RZjMFDxVSHjAhnxeBHMchVIuWBsM5 PtMrqKuZN62sBEBa1VXFnupdxjUT6bs661kIVoT/eos3lojWryLBf1d+0k1BXTFD /2fVe9rQHBged7IZ2YfT5v9nbPJAKLpcWYJZCUStcXIzAdjRHHEvICXSHObKGHFG +1pHQWa/q9I5EbZLxwxdBPbXURVyJQNh3rVg6+wuTpt2iGyc+JNfCwc9xVg7wYfy hlEXVcZ8JZM6R1uhD6qSM6+0GrkEbAn4H5AUvj/A2SVXI7C1yaXH63BhLbnIEoNs vkKJlGtY/O82j+XIqmPeP5OVwbxRlmuWaMup7kZyUao+bPhDUiGoLk6h0JZZIel3 n7bdAYiKyjhyG3CD+J5FzKyKPY3459oFyDa1pK1Doxi6IQF4qKM= =zsBA -----END PGP SIGNATURE----- --ef8kLf0ajQNPguLh--