From 204e77cd11b2df720c9acd35d562e1ed868304b4 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Fri, 20 Sep 2024 14:12:41 +1000 Subject: udp: Don't attempt to get dual-stack sockets in nonsensical cases To save some kernel memory we try to use "dual stack" sockets (that listen to both IPv4 and IPv6 traffic) when possible. However udp_sock_init() attempts to do this in some cases that can't work. Specifically we can only do this when listening on any address. That's never true for the ns (splicing) case, because we always listen on loopback. For the !ns case and AF_UNSPEC case, addr should always be NULL, but add an assert to verify. This is harmless: if addr is non-NULL, sock_l4() will just fail and we'll fall back to the other path. But, it's messy and makes some upcoming changes harder, so avoid attempting this in cases we know can't work. Signed-off-by: David Gibson --- udp.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/udp.c b/udp.c index 7b28313..8cea80c 100644 --- a/udp.c +++ b/udp.c @@ -803,21 +803,16 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af, ASSERT(!c->no_udp); - if (af == AF_UNSPEC && c->ifi4 && c->ifi6) { + if (af == AF_UNSPEC && c->ifi4 && c->ifi6 && !ns) { int s; + ASSERT(!addr); + /* Attempt to get a dual stack socket */ - if (!ns) { - s = sock_l4(c, AF_UNSPEC, EPOLL_TYPE_UDP_LISTEN, - addr, ifname, port, uref.u32); - udp_splice_init[V4][port] = s < 0 ? -1 : s; - udp_splice_init[V6][port] = s < 0 ? -1 : s; - } else { - s = sock_l4(c, AF_UNSPEC, EPOLL_TYPE_UDP_LISTEN, - &in4addr_loopback, ifname, port, uref.u32); - udp_splice_ns[V4][port] = s < 0 ? -1 : s; - udp_splice_ns[V6][port] = s < 0 ? -1 : s; - } + s = sock_l4(c, AF_UNSPEC, EPOLL_TYPE_UDP_LISTEN, + NULL, ifname, port, uref.u32); + udp_splice_init[V4][port] = s < 0 ? -1 : s; + udp_splice_init[V6][port] = s < 0 ? -1 : s; if (IN_INTERVAL(0, FD_REF_MAX, s)) return 0; } -- cgit v1.2.3