tcp: probe for SO_PEEK_OFF both in tcpv4 and tcp6

Based on an original patch by Jon Maloy:

--
The recently added socket option SO_PEEK_OFF is not supported for
TCP/IPv6 sockets. Until we get that support into the kernel we need to
test for support in both protocols to set the global 'peek_offset_cap´
to true.
--

Compared to the original patch:
- only check for SO_PEEK_OFF support for enabled IP versions
- use sa_family_t instead of int to pass the address family around

Fixes: e63d281871 ("tcp: leverage support of SO_PEEK_OFF socket option when available")
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
Jon Maloy 2024-07-20 09:54:53 -04:00 committed by Stefano Brivio
parent 882599e180
commit 9cb6b50815

37
tcp.c
View file

@ -2470,6 +2470,29 @@ static void tcp_sock_refill_init(const struct ctx *c)
} }
} }
/**
* tcp_probe_peek_offset_cap() - Check if SO_PEEK_OFF is supported by kernel
* @af: Address family, IPv4 or IPv6
*
* Return: true if supported, false otherwise
*/
bool tcp_probe_peek_offset_cap(sa_family_t af)
{
bool ret = false;
int s, optv = 0;
s = socket(af, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
if (s < 0) {
warn_perror("Temporary TCP socket creation failed");
} else {
if (!setsockopt(s, SOL_SOCKET, SO_PEEK_OFF, &optv, sizeof(int)))
ret = true;
close(s);
}
return ret;
}
/** /**
* tcp_init() - Get initial sequence, hash secret, initialise per-socket data * tcp_init() - Get initial sequence, hash secret, initialise per-socket data
* @c: Execution context * @c: Execution context
@ -2478,9 +2501,6 @@ static void tcp_sock_refill_init(const struct ctx *c)
*/ */
int tcp_init(struct ctx *c) int tcp_init(struct ctx *c)
{ {
unsigned int optv = 0;
int s;
ASSERT(!c->no_tcp); ASSERT(!c->no_tcp);
if (c->ifi4) if (c->ifi4)
@ -2502,15 +2522,8 @@ int tcp_init(struct ctx *c)
NS_CALL(tcp_ns_socks_init, c); NS_CALL(tcp_ns_socks_init, c);
} }
/* Probe for SO_PEEK_OFF support */ peek_offset_cap = (!c->ifi4 || tcp_probe_peek_offset_cap(AF_INET)) &&
s = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP); (!c->ifi6 || tcp_probe_peek_offset_cap(AF_INET6));
if (s < 0) {
warn_perror("Temporary TCP socket creation failed");
} else {
if (!setsockopt(s, SOL_SOCKET, SO_PEEK_OFF, &optv, sizeof(int)))
peek_offset_cap = true;
close(s);
}
info("SO_PEEK_OFF%ssupported", peek_offset_cap ? " " : " not "); info("SO_PEEK_OFF%ssupported", peek_offset_cap ? " " : " not ");
return 0; return 0;