When neither -4 nor -6 is supplied, getaddrinfo() selects the socket family but opt.sock.family remains AF_UNSPEC. The cmsg and socket option helpers then take the IPv6 branch even for an IPv4 socket. For example, an IPv4 loopback packet sent with -l 11 arrived with the default TTL of 64 on the test host. Using -L 11 instead failed with ENOPROTOOPT because the tool tried IPV6_UNICAST_HOPS on an IPv4 socket. Explicit -4 works in both cases. Record the selected ai_family after successful address resolution so the helpers use the same family as the socket. Discovered by Codex-assisted code inspection and tested with real loopback UDP traffic, without kernel instrumentation. Covered IPv4 and IPv6, automatic and explicit family selection, and cmsg and setsockopt hop limits. All eight cases receive the requested value after the fix. Built through the net selftest Makefile and with GCC -Wall -Werror. Fixes: 2e5584e0f913 ("selftests/net: expand cmsg_ipv6.sh with ipv4") Signed-off-by: Jingjing Xu --- tools/testing/selftests/net/cmsg_sender.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/net/cmsg_sender.c b/tools/testing/selftests/net/cmsg_sender.c index 67a72b1a2f3d..01617a021338 100644 --- a/tools/testing/selftests/net/cmsg_sender.c +++ b/tools/testing/selftests/net/cmsg_sender.c @@ -495,6 +495,7 @@ int main(int argc, char *argv[]) goto err_free_buff; } + opt.sock.family = ai->ai_family; if (ai->ai_family == AF_INET6 && opt.sock.proto == IPPROTO_ICMP) opt.sock.proto = IPPROTO_ICMPV6; base-commit: 1d5e2c57e9ffc134e14b3f837c8d7c53b508bd22 -- 2.53.0