[PATCH v5 00/15] Reduce differences between inbound and outbound socket binding
This series is based on my series fixing bug 176 (regression in auto forwarding). The fact that outbound forwarding sockets are bound to the loopback address, whereas inbound forwarding sockets are (by default) bound to the unspecified address leads to some unexpected differences between the paths setting up each of them. An idea for tackling bug 100 suggested a different approach which will also reduce some of those differences and allow more code to be shared between the two paths. I've since discovered that this approach doesn't help for bug 100, but I think it's still worthwhile for other reasons. v5: - Combine with SO_BINDTODEVICE and bug 113 patch series - Add fallback handling for kernels without SO_BINDTODEVICE - Add missing struct field documentation for no_bindtodevice v4: - Add cleanup patch removing unused structure field - Rebase, fixing conflicts with Laurent's changes - A bunch of spelling and other cosmetic fixes - Clarify relation to bug 100 and bug 113 v3: - A number of additional fixes covering the handling of IPV6_V6ONLY sockopt - Assorted trivial changes v2: - Some rearrangements and rewordings for clarity David Gibson (15): util: Correct error message on SO_BINDTODEVICE failure util: Extend sock_probe_mem() to sock_probe_features() conf: More useful errors for kernels without SO_BINDTODEVICE flow: Remove bogus @path field from flowside_sock_args inany: Let length of sockaddr_inany be implicit from the family util, flow, pif: Simplify sock_l4_sa() interface tcp: Merge tcp_ns_sock_init[46]() into tcp_sock_init_one() udp: Unify some more inbound/outbound parts of udp_sock_init() udp: Move udp_sock_init() special case to its caller util: Fix setting of IPV6_V6ONLY socket option tcp, udp: Remove fallback if creating dual stack socket fails tcp, udp: Bind outbound listening sockets by interface instead of address util: Rename sock_l4_dualstack() to sock_l4_dualstack_any() tcp: Always populate oaddr field for socket initiated flows fwd: Preserve non-standard loopback address when splice forwarding conf.c | 16 ++++- flow.c | 22 +++---- fwd.c | 4 +- icmp.c | 3 +- inany.h | 17 ++++++ passt.c | 2 +- passt.h | 2 + pif.c | 27 ++------- pif.h | 2 +- tcp.c | 162 +++++++++++++++++++++------------------------------ tcp.h | 5 +- tcp_splice.c | 5 +- udp.c | 122 +++++++++++++++++++++----------------- udp.h | 5 +- util.c | 102 +++++++++++++++++++++++++++----- util.h | 10 ++-- 16 files changed, 289 insertions(+), 217 deletions(-) -- 2.52.0
This was there when the structure was created, but never used. Looks like
a copy-pasta error.
Fixes: 781164e25bdf ("flow: Helper to create sockets based on flowside")
Signed-off-by: David Gibson
Before 5.7, the kernel didn't allow SO_BINDTODEVICE to be called
unprivileged. That means for earlier kernels, we can't implement binding
listening sockets to a specific interface (e.g. -t %eth0/80).
Currently we'll generate an error on this at the point we actually attempt
the SO_BINDTODEVICE setsockopt(), at which point the connection to the
command line option might not be entirely clear.
Use the fact we now probe for SO_BINDTODEVICE support to make a clearer
error message at the time we parse the forwarding option.
Signed-off-by: David Gibson
sock_probe_mem() currently checks whether we're able to allocate large
socket buffers. Extend it to also check whether the SO_BINDTODEVICE
socket option is available. Rename to sock_probe_features() to reflect the
new functionality.
Signed-off-by: David Gibson
Inbound sockets are bound to the unspecified address by default, whereas
outbound sockets are bound to the loopback address. This is currently
handled in udp_sock_init() which ignores its addr argument in the outbound
case.
Move the handling of this special case to the caller, udp_port_rebind().
This makes the semantics of the 'addr' argument more consistent, and will
make future changes easier.
Signed-off-by: David Gibson
udp_sock_init() takes an 'ns' parameter determining if it creates a socket
in the guest namespace or host namespace. Alter it to take a pif
parameter instead, like tcp_sock_init(), and use that change to slightly
reduce code duplication between the HOST and SPLICE cases.
Signed-off-by: David Gibson
Surprisingly little logic is shared between the path for creating a
listen()ing socket in the guest namespace versus in the host namespace.
Improve this, by extending tcp_sock_init_one() to take a pif parameter
indicating where it should open the socket. This allows
tcp_ns_sock_init[46]() to be removed entirely.
We generalise tcp_sock_init() in the same way, although we don't fully use
it yet, due to some subtle differences in how we bind for -t versus -T.
Signed-off-by: David Gibson
Stefano correctly noted that the fact a socket is dual-stack doesn't
necessarily imply that it is bound to a wildcard address. While that's the
only case we use for dual-stack sockets, there may be others. Therefore
rename this function to make it clearer that it always uses a wildcard
bind.
Suggested-by: Stefano Brivio
sockaddr_inany can contain either an IPv4 or IPv6 socket address, so the
relevant length for bind() or connect() can vary. In pif_sockaddr() we
return that length, and in sock_l4_sa() we take it as an extra parameter.
However, sockaddr_inany always contains exactly a sockaddr_in or a
sockaddr_in6 each with a fixed size. Therefore we can derive the relevant
length from the family, and don't need to pass it around separately.
Make a tiny helper to get the relevant address length, and update all
interfaces to use that approach instead.
In the process, fix a buglet in tcp_flow_repair_bind(): we passed
sizeof(union sockaddr_inany) to bind() instead of the specific length for
the address family. Since the sizeof() is always longer than the specific
length, this is probably fine, but not theoretically correct.
Signed-off-by: David Gibson
Currently, outbound forwards (-T, -U) are handled by sockets bound to the
loopback address. Typically we create two sockets, one for 127.0.0.1 and
one for ::1.
This has some disadvantages:
* The guest can't connect via 127.0.0.0/8 addresses other than 127.0.0.1
* We can't use dual-stack sockets, we have to have separate sockets for
IPv4 and IPv6.
The restriction exists for a reason though. If the guest has any
interfaces other than pasta (e.g. a VPN tunnel) external hosts could reach
the host via the forwards. Especially combined with -T auto / -U auto this
would make it very easy to make a mistake with nasty security implications.
We can achieve this a different way, however. Don't bind to a specific
address, but _do_ use SO_BINDTODEVICE to restrict the sockets to the "lo"
interface. We fall back to the old behaviour for older kernels where
SO_BINDTODEVICE is not available unprivileged.
Note that although traffic to a local but non-loopback address is passed
over the 'lo' interface (as seen by netfilter and dumpcap), it doesn't
count as attached to that interface for the purposes of SO_BINDTODEVICE
(information from the routing table overrides the "physical" interface).
So, this change doesn't help for bug 100.
It's also not a complete fix for bug 113, it does however:
* Get us a step closer to fixing bug 113
* Slightly simplify the code
* Make things a bit easier to allow more flexible binding on the guest in
in future
Link: https://bugs.passt.top/show_bug.cgi?id=113
Signed-off-by: David Gibson
sock_l4_sa() has a somewhat confusing 'v6only' option controlling whether
to set the IPV6_V6ONLY socket option. Usually it's set when the given
address is IPv6, but not when we want to create a dual stack listening
socket. The latter only makes sense when the address is :: however.
Clarify this by only keeping the v6only option in an internal helper
sock_l4_(). External users will call either sock_l4() which always creates
a socket bound to a specific IP version, or sock_l4_dualstack() which
creates a dual stack socket, but takes only a port not an address.
We drop the '_sa' suffix while we're at it - it exists because this used
to be an internal version with a sock_l4() wrapper. The wrapper no longer
exists so the '_sa' is no longer useful.
Signed-off-by: David Gibson
When we receive a TCP connection, we get the peer address from the accept()
call. In the case of a listening socket with an unspecified address (:: or
0.0.0.0) the local address of the accept()ed socket could vary. We don't
get that from the accept() - we must explicitly call getsockname() to get
it.
Currently we avoid the latency of that extra syscall, and therefore don't
populate the initiating 'oaddr' field of a flow created by an incoming TCP
socket connection. This more or less works, because we rarely need that
local address, but it does cause some oddities:
* For migration we need the local address to recreate the socket on the
destination, so we *do* call getsockname() in vhost-user mode
* It limits our options in terms of forwarding connections flexibly based
on the address to which they're received
* It differs from UDP, where we explicitly use the IP_PKTINFO cmsg to
populate oaddr.
* It means (some) flow debug messages will contain wildcards instead of
real local addresses
In theory we can elide this call when accept()ing from a socket bound to
a specific address instead of a wildcard. However to do that will need
revisions to the data structures we use to keep track of listening sockets.
The lack of this information is making it hard to implement some fixes we
want. So, pay the price of the extra syscall to get this information, with
the hope that we can later optimise it away for some cases.
Signed-off-by: David Gibson
To save kernel memory we try to use "dual stack" sockets which can listen
for both IPv4 and IPv6 connections where possible. To support kernels
which don't allow dual stack sockets, we fall back to creating individual
sockets for IPv4 and IPv6.
This fallback causes some mild ugliness now, and will cause more difficulty
with upcoming improvements to the forwarding logic. I don't think we need
the fallback on the following grounds:
1) The fallback was broken since inception:
The fallback was triggered if pif_sock_l4() failed attempting to create the
dual stack socket. But even if the kernel didn't support them,
pif_sock_l4() would not report a failure.
- Dual stack sockets are distinguished by having the IPV6_V6ONLY sockopt
set to 0. However, until the last patch, we only called setsockopt()
if we wanted to set this to 1, so there was no kernel operation which
could fail for dual stack sockets - we'd silently create a IPv6 only
socket instead.
- Even if we did call the setsockopt(), we only printed a debug() message
for failures, we didn't report it to the caller
2) Dual stack sockets are not just a Linux extension
The dual stack socket interface is described in RFC3493, specifically
section 3.7 and section 5.3. It is supported on BSD:
https://man.freebsd.org/cgi/man.cgi?query=ip6
and on Windows:
https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-ipv6-socket-...
3) Linux has supported dual stack sockets for over 20 years
According to ipv6(7) the IPV6_V6ONLY socket option was introduced in
Linux 2.6 and Linux 2.4.21 both from 2003.
Signed-off-by: David Gibson
Currently we only call setsockopt() on IPV6_V6ONLY when we want to set it
to 1, which we typically do on all IPv6 sockets except those explicitly for
dual stack listening. That's not quite right in two ways:
* Although IPV6_V6ONLY==0 is normally the default on Linux, that can be
changed with the net.ipv6.bindv6only sysctl. It may also have different
defaults on other OSes if we ever support them. We know we need it off
for dual stack sockets, so explicitly set it to 0 in that case.
* At the same time setting IPV6_V6ONLY to 1 for IPv6 sockets bound to a
specific address is harmless but pointless. Don't set the option at all
in this case, saving a syscall.
Signed-off-by: David Gibson
When forwarding "spliced" connections outwards (-T or -U) we listen on the
guest's loopback and always forward to 127.0.0.1 (or ::1) on the host.
However, it's also possible for clients on the guest to attempt connecting
to other addresses in 127.0.0.0/8 (systemd-resolved uses 127.0.0.53 in
practice). If the host side server is only listening on that specific
non-standard loopback address, the forward won't work. Fix this by
preserving the specific (loopback) address when forwarding such
connections.
Link: https://bugs.passt.top/show_bug.cgi?id=113
Signed-off-by: David Gibson
Sorry, I realised the warning I add here isn't correct in a couple of ways. I'll respin shortly, but patches 1..11 should still be fine to review. On Tue, Dec 02, 2025 at 03:02:12PM +1100, David Gibson wrote:
Currently, outbound forwards (-T, -U) are handled by sockets bound to the loopback address. Typically we create two sockets, one for 127.0.0.1 and one for ::1.
This has some disadvantages: * The guest can't connect via 127.0.0.0/8 addresses other than 127.0.0.1 * We can't use dual-stack sockets, we have to have separate sockets for IPv4 and IPv6.
The restriction exists for a reason though. If the guest has any interfaces other than pasta (e.g. a VPN tunnel) external hosts could reach the host via the forwards. Especially combined with -T auto / -U auto this would make it very easy to make a mistake with nasty security implications.
We can achieve this a different way, however. Don't bind to a specific address, but _do_ use SO_BINDTODEVICE to restrict the sockets to the "lo" interface. We fall back to the old behaviour for older kernels where SO_BINDTODEVICE is not available unprivileged.
Note that although traffic to a local but non-loopback address is passed over the 'lo' interface (as seen by netfilter and dumpcap), it doesn't count as attached to that interface for the purposes of SO_BINDTODEVICE (information from the routing table overrides the "physical" interface). So, this change doesn't help for bug 100.
It's also not a complete fix for bug 113, it does however: * Get us a step closer to fixing bug 113 * Slightly simplify the code * Make things a bit easier to allow more flexible binding on the guest in in future
Link: https://bugs.passt.top/show_bug.cgi?id=113
Signed-off-by: David Gibson
--- conf.c | 6 ++++++ pif.c | 6 ------ tcp.c | 5 +++++ udp.c | 30 +++++++++++++++++++++++------- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/conf.c b/conf.c index 6bd9717b..02a4b65a 100644 --- a/conf.c +++ b/conf.c @@ -235,6 +235,12 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg, if (c->mode != MODE_PASTA) die("'auto' port forwarding is only allowed for pasta");
+ if ((optname == 'T' || optname == 'U') && c->no_bindtodevice) { + warn( +"'-%c auto' enabled without unprivileged SO_BINDTODEVICE", optname); + warn( +"Forwarding from addresses other than 127.0.0.1 will not work"); + } fwd->mode = FWD_AUTO; return; } diff --git a/pif.c b/pif.c index 85904f35..db447b4f 100644 --- a/pif.c +++ b/pif.c @@ -81,12 +81,6 @@ int pif_sock_l4(const struct ctx *c, enum epoll_type type, uint8_t pif,
ASSERT(pif_is_socket(pif));
- if (pif == PIF_SPLICE) { - /* Sanity checks */ - ASSERT(!ifname); - ASSERT(addr && inany_is_loopback(addr)); - } - if (!addr) { ref.fd = sock_l4_dualstack(c, type, port, ifname); } else { diff --git a/tcp.c b/tcp.c index 2abb8be4..aacc5b20 100644 --- a/tcp.c +++ b/tcp.c @@ -2627,6 +2627,11 @@ static void tcp_ns_sock_init(const struct ctx *c, in_port_t port) { ASSERT(!c->no_tcp);
+ if (!c->no_bindtodevice) { + tcp_sock_init(c, PIF_SPLICE, NULL, "lo", port); + return; + } + if (c->ifi4) tcp_sock_init_one(c, PIF_SPLICE, &inany_loopback4, NULL, port); if (c->ifi6) diff --git a/udp.c b/udp.c index 3d097fbb..4b625b78 100644 --- a/udp.c +++ b/udp.c @@ -1182,6 +1182,26 @@ static void udp_splice_iov_init(void) } }
+/** + * udp_ns_sock_init() - Init socket to listen for spliced outbound connections + * @c: Execution context + * @port: Port, host order + */ +static void udp_ns_sock_init(const struct ctx *c, in_port_t port) +{ + ASSERT(!c->no_udp); + + if (!c->no_bindtodevice) { + udp_sock_init(c, PIF_SPLICE, NULL, "lo", port); + return; + } + + if (c->ifi4) + udp_sock_init(c, PIF_SPLICE, &inany_loopback4, NULL, port); + if (c->ifi6) + udp_sock_init(c, PIF_SPLICE, &inany_loopback6, NULL, port); +} + /** * udp_port_rebind() - Rebind ports to match forward maps * @c: Execution context @@ -1213,14 +1233,10 @@ static void udp_port_rebind(struct ctx *c, bool outbound)
if ((c->ifi4 && socks[V4][port] == -1) || (c->ifi6 && socks[V6][port] == -1)) { - if (outbound) { - udp_sock_init(c, PIF_SPLICE, - &inany_loopback4, NULL, port); - udp_sock_init(c, PIF_SPLICE, - &inany_loopback6, NULL, port); - } else { + if (outbound) + udp_ns_sock_init(c, port); + else udp_sock_init(c, PIF_HOST, NULL, NULL, port); - } } } } -- 2.52.0
-- 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
On Tue, 2 Dec 2025 15:02:02 +1100
David Gibson
sock_probe_mem() currently checks whether we're able to allocate large socket buffers. Extend it to also check whether the SO_BINDTODEVICE socket option is available. Rename to sock_probe_features() to reflect the new functionality.
Signed-off-by: David Gibson
--- passt.c | 2 +- passt.h | 2 ++ util.c | 19 +++++++++++++++++-- util.h | 2 +- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/passt.c b/passt.c index 4964427d..0b84ac6c 100644 --- a/passt.c +++ b/passt.c @@ -381,7 +381,7 @@ int main(int argc, char **argv) if (setrlimit(RLIMIT_NOFILE, &limit)) die_perror("Failed to set current limit for open files");
- sock_probe_mem(&c); + sock_probe_features(&c);
conf(&c, argc, argv); trace_init(c.trace); diff --git a/passt.h b/passt.h index 15801b44..79d01ddb 100644 --- a/passt.h +++ b/passt.h @@ -204,6 +204,7 @@ struct ip6_ctx { * @freebind: Allow binding of non-local addresses for forwarding * @low_wmem: Low probed net.core.wmem_max * @low_rmem: Low probed net.core.rmem_max + * @no_bindtodevice: Unprivileged SO_BINDTODEVICE not available * @vdev: vhost-user device * @device_state_fd: Device state migration channel * @device_state_result: Device state migration result @@ -281,6 +282,7 @@ struct ctx {
int low_wmem; int low_rmem; + int no_bindtodevice;
struct vu_dev *vdev;
diff --git a/util.c b/util.c index 347f34f5..bad38129 100644 --- a/util.c +++ b/util.c @@ -233,12 +233,13 @@ int sock_unix(char *sock_path) }
/** - * sock_probe_mem() - Check if setting high SO_SNDBUF and SO_RCVBUF is allowed + * sock_probe_features() - Probe for socket features we might use * @c: Execution context */ -void sock_probe_mem(struct ctx *c) +void sock_probe_features(struct ctx *c) { int v = INT_MAX / 2, s; + const char lo[] = "lo"; socklen_t sl;
s = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP); @@ -247,6 +248,7 @@ void sock_probe_mem(struct ctx *c) return; }
+ /* Check if setting high SO_SNDBUF and SO_RCVBUF is allowed */ sl = sizeof(v); if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &v, sizeof(v)) || getsockopt(s, SOL_SOCKET, SO_SNDBUF, &v, &sl) || @@ -259,6 +261,19 @@ void sock_probe_mem(struct ctx *c) (size_t)v < RCVBUF_BIG) c->low_rmem = 1;
+ /* Check if SO_BINDTODEVICE is available + * + * Supported since kernel version 5.7, commit c427bfec18f2 ("net: core: + * enable SO_BINDTODEVICE for non-root users"). Some distro kernels may + * have backports, of course. Record whether we can use it so that we + * can give more useful diagnostics. + */ + if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, lo, sizeof(lo)-1)) {
Nit: our coding style uses spaces around arithmetic operators... I took the liberty of fixing this up on merge, though. -- Stefano
On Tue, 2 Dec 2025 15:02:00 +1100
David Gibson
This series is based on my series fixing bug 176 (regression in auto forwarding).
The fact that outbound forwarding sockets are bound to the loopback address, whereas inbound forwarding sockets are (by default) bound to the unspecified address leads to some unexpected differences between the paths setting up each of them.
An idea for tackling bug 100 suggested a different approach which will also reduce some of those differences and allow more code to be shared between the two paths. I've since discovered that this approach doesn't help for bug 100, but I think it's still worthwhile for other reasons.
v5: - Combine with SO_BINDTODEVICE and bug 113 patch series - Add fallback handling for kernels without SO_BINDTODEVICE - Add missing struct field documentation for no_bindtodevice
Applied (with nit from 2/15 fixed). -- Stefano
On Wed, 3 Dec 2025 15:41:36 +1100
David Gibson
Sorry, I realised the warning I add here isn't correct in a couple of ways. I'll respin shortly, but patches 1..11 should still be fine to review.
...read just in time, I won't push the series (not even up to 11/15 I guess?). -- Stefano
On Wed, 3 Dec 2025 07:38:55 +0100
Stefano Brivio
On Wed, 3 Dec 2025 15:41:36 +1100 David Gibson
wrote: Sorry, I realised the warning I add here isn't correct in a couple of ways. I'll respin shortly, but patches 1..11 should still be fine to review.
...read just in time, I won't push the series (not even up to 11/15 I guess?).
Pushed now, as you noted offline that the series doesn't actually make the situation worse. -- Stefano
participants (2)
-
David Gibson
-
Stefano Brivio