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 <david@gibson.dropbear.id.au>
This commit is contained in:
David Gibson 2024-09-20 14:12:41 +10:00 committed by Stefano Brivio
parent 8f8c4d27eb
commit 204e77cd11

13
udp.c
View file

@ -803,21 +803,16 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
ASSERT(!c->no_udp); ASSERT(!c->no_udp);
if (af == AF_UNSPEC && c->ifi4 && c->ifi6) { if (af == AF_UNSPEC && c->ifi4 && c->ifi6 && !ns) {
int s; int s;
ASSERT(!addr);
/* Attempt to get a dual stack socket */ /* Attempt to get a dual stack socket */
if (!ns) {
s = sock_l4(c, AF_UNSPEC, EPOLL_TYPE_UDP_LISTEN, s = sock_l4(c, AF_UNSPEC, EPOLL_TYPE_UDP_LISTEN,
addr, ifname, port, uref.u32); NULL, ifname, port, uref.u32);
udp_splice_init[V4][port] = s < 0 ? -1 : s; udp_splice_init[V4][port] = s < 0 ? -1 : s;
udp_splice_init[V6][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;
}
if (IN_INTERVAL(0, FD_REF_MAX, s)) if (IN_INTERVAL(0, FD_REF_MAX, s))
return 0; return 0;
} }