util: Make sock_l4() treat empty string ifname like NULL

sock_l4() takes NULL for ifname if you don't want to bind the socket to a
particular interface.  However, for a number of the callers, it's more
natural to use an empty string for that case.  Change sock_l4() to accept
either NULL or an empty string equivalently, and simplify some callers
using that change.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2023-12-08 01:31:40 +11:00 committed by Stefano Brivio
parent 5cada56186
commit 57de44a4bc
3 changed files with 7 additions and 16 deletions

15
icmp.c
View file

@ -187,12 +187,8 @@ int icmp_tap_handler(const struct ctx *c, uint8_t pif, int af,
iref.id = id = ntohs(ih->un.echo.id); iref.id = id = ntohs(ih->un.echo.id);
if ((s = icmp_id_map[V4][id].sock) <= 0) { if ((s = icmp_id_map[V4][id].sock) <= 0) {
const char *bind_if; s = sock_l4(c, AF_INET, IPPROTO_ICMP, &c->ip4.addr_out,
c->ip4.ifname_out, id, iref.u32);
bind_if = *c->ip4.ifname_out ? c->ip4.ifname_out : NULL;
s = sock_l4(c, AF_INET, IPPROTO_ICMP,
&c->ip4.addr_out, bind_if, id, iref.u32);
if (s < 0) if (s < 0)
goto fail_sock; goto fail_sock;
if (s > FD_REF_MAX) { if (s > FD_REF_MAX) {
@ -237,12 +233,9 @@ int icmp_tap_handler(const struct ctx *c, uint8_t pif, int af,
iref.id = id = ntohs(ih->icmp6_identifier); iref.id = id = ntohs(ih->icmp6_identifier);
if ((s = icmp_id_map[V6][id].sock) <= 0) { if ((s = icmp_id_map[V6][id].sock) <= 0) {
const char *bind_if;
bind_if = *c->ip6.ifname_out ? c->ip6.ifname_out : NULL;
s = sock_l4(c, AF_INET6, IPPROTO_ICMPV6, s = sock_l4(c, AF_INET6, IPPROTO_ICMPV6,
&c->ip6.addr_out, bind_if, id, iref.u32); &c->ip6.addr_out,
c->ip6.ifname_out, id, iref.u32);
if (s < 0) if (s < 0)
goto fail_sock; goto fail_sock;
if (s > FD_REF_MAX) { if (s > FD_REF_MAX) {

6
udp.c
View file

@ -870,8 +870,7 @@ int udp_tap_handler(struct ctx *c, uint8_t pif,
union udp_epoll_ref uref = { .port = src }; union udp_epoll_ref uref = { .port = src };
const char *bind_if = NULL; const char *bind_if = NULL;
if (!IN6_IS_ADDR_LOOPBACK(&s_in.sin_addr) && if (!IN6_IS_ADDR_LOOPBACK(&s_in.sin_addr))
*c->ip6.ifname_out)
bind_if = c->ip6.ifname_out; bind_if = c->ip6.ifname_out;
if (!IN4_IS_ADDR_UNSPECIFIED(&c->ip4.addr_out) && if (!IN4_IS_ADDR_UNSPECIFIED(&c->ip4.addr_out) &&
@ -919,8 +918,7 @@ int udp_tap_handler(struct ctx *c, uint8_t pif,
union udp_epoll_ref uref = { .v6 = 1, .port = src }; union udp_epoll_ref uref = { .v6 = 1, .port = src };
const char *bind_if = NULL; const char *bind_if = NULL;
if (!IN6_IS_ADDR_LOOPBACK(&s_in6.sin6_addr) && if (!IN6_IS_ADDR_LOOPBACK(&s_in6.sin6_addr))
*c->ip6.ifname_out)
bind_if = c->ip6.ifname_out; bind_if = c->ip6.ifname_out;
if (!IN6_IS_ADDR_UNSPECIFIED(&c->ip6.addr_out) && if (!IN6_IS_ADDR_UNSPECIFIED(&c->ip6.addr_out) &&

2
util.c
View file

@ -187,7 +187,7 @@ int sock_l4(const struct ctx *c, int af, uint8_t proto,
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(y))) if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(y)))
debug("Failed to set SO_REUSEADDR on socket %i", fd); debug("Failed to set SO_REUSEADDR on socket %i", fd);
if (ifname) { if (ifname && *ifname) {
/* Supported since kernel version 5.7, commit c427bfec18f2 /* Supported since kernel version 5.7, commit c427bfec18f2
* ("net: core: enable SO_BINDTODEVICE for non-root users"). If * ("net: core: enable SO_BINDTODEVICE for non-root users"). If
* it's unsupported, don't bind the socket at all, because the * it's unsupported, don't bind the socket at all, because the