From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v5 10/15] util: Fix setting of IPV6_V6ONLY socket option
Date: Tue, 2 Dec 2025 15:02:10 +1100 [thread overview]
Message-ID: <20251202040215.2351792-11-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20251202040215.2351792-1-david@gibson.dropbear.id.au>
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 <david@gibson.dropbear.id.au>
---
util.c | 29 +++++++++++++++++++++++------
1 file changed, 23 insertions(+), 6 deletions(-)
diff --git a/util.c b/util.c
index ee369614..853c35a3 100644
--- a/util.c
+++ b/util.c
@@ -45,13 +45,13 @@
* @type: epoll type
* @sa: Socket address to bind to
* @ifname: Interface for binding, NULL for any
- * @v6only: Set IPV6_V6ONLY socket option
+ * @v6only: If >= 0, set IPV6_V6ONLY socket option to this value
*
* Return: newly created socket, negative error code on failure
*/
static int sock_l4_(const struct ctx *c, enum epoll_type type,
const union sockaddr_inany *sa, const char *ifname,
- bool v6only)
+ int v6only)
{
sa_family_t af = sa->sa_family;
bool freebind = false;
@@ -95,9 +95,13 @@ static int sock_l4_(const struct ctx *c, enum epoll_type type,
return -EBADF;
}
- if (v6only)
- if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &y, sizeof(y)))
- debug("Failed to set IPV6_V6ONLY on socket %i", fd);
+ if (v6only >= 0) {
+ if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
+ &v6only, sizeof(v6only))) {
+ debug("Failed to set IPV6_V6ONLY to %d on socket %i",
+ v6only, fd);
+ }
+ }
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(y)))
debug("Failed to set SO_REUSEADDR on socket %i", fd);
@@ -181,7 +185,16 @@ static int sock_l4_(const struct ctx *c, enum epoll_type type,
int sock_l4(const struct ctx *c, enum epoll_type type,
const union sockaddr_inany *sa, const char *ifname)
{
- return sock_l4_(c, type, sa, ifname, sa->sa_family == AF_INET6);
+ int v6only = -1;
+
+ /* The option doesn't exist for IPv4 sockets, and we don't care about it
+ * for IPv6 sockets with a non-wildcard address.
+ */
+ if (sa->sa_family == AF_INET6 &&
+ IN6_IS_ADDR_UNSPECIFIED(&sa->sa6.sin6_addr))
+ v6only = 1;
+
+ return sock_l4_(c, type, sa, ifname, v6only);
}
/**
@@ -204,6 +217,10 @@ int sock_l4_dualstack(const struct ctx *c, enum epoll_type type,
.sa6.sin6_port = htons(port),
};
+ /* Dual stack sockets require IPV6_V6ONLY == 0. Usually that's the
+ * default, but sysctl net.ipv6.bindv6only can change that, so set the
+ * sockopt explicitly.
+ */
return sock_l4_(c, type, &sa, ifname, 0);
}
--
2.52.0
next prev parent reply other threads:[~2025-12-02 4:02 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-02 4:02 [PATCH v5 00/15] Reduce differences between inbound and outbound socket binding David Gibson
2025-12-02 4:02 ` [PATCH v5 01/15] util: Correct error message on SO_BINDTODEVICE failure David Gibson
2025-12-02 4:02 ` [PATCH v5 02/15] util: Extend sock_probe_mem() to sock_probe_features() David Gibson
2025-12-03 6:34 ` Stefano Brivio
2025-12-02 4:02 ` [PATCH v5 03/15] conf: More useful errors for kernels without SO_BINDTODEVICE David Gibson
2025-12-02 4:02 ` [PATCH v5 04/15] flow: Remove bogus @path field from flowside_sock_args David Gibson
2025-12-02 4:02 ` [PATCH v5 05/15] inany: Let length of sockaddr_inany be implicit from the family David Gibson
2025-12-02 4:02 ` [PATCH v5 06/15] util, flow, pif: Simplify sock_l4_sa() interface David Gibson
2025-12-02 4:02 ` [PATCH v5 07/15] tcp: Merge tcp_ns_sock_init[46]() into tcp_sock_init_one() David Gibson
2025-12-02 4:02 ` [PATCH v5 08/15] udp: Unify some more inbound/outbound parts of udp_sock_init() David Gibson
2025-12-02 4:02 ` [PATCH v5 09/15] udp: Move udp_sock_init() special case to its caller David Gibson
2025-12-02 4:02 ` David Gibson [this message]
2025-12-02 4:02 ` [PATCH v5 11/15] tcp, udp: Remove fallback if creating dual stack socket fails David Gibson
2025-12-02 4:02 ` [PATCH v5 12/15] tcp, udp: Bind outbound listening sockets by interface instead of address David Gibson
2025-12-03 4:41 ` David Gibson
2025-12-03 6:38 ` Stefano Brivio
2025-12-03 13:13 ` Stefano Brivio
2025-12-02 4:02 ` [PATCH v5 13/15] util: Rename sock_l4_dualstack() to sock_l4_dualstack_any() David Gibson
2025-12-02 4:02 ` [PATCH v5 14/15] tcp: Always populate oaddr field for socket initiated flows David Gibson
2025-12-02 4:02 ` [PATCH v5 15/15] fwd: Preserve non-standard loopback address when splice forwarding David Gibson
2025-12-03 6:34 ` [PATCH v5 00/15] Reduce differences between inbound and outbound socket binding Stefano Brivio
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=20251202040215.2351792-11-david@gibson.dropbear.id.au \
--to=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/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).