From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id E47795A0271 for ; Thu, 7 Dec 2023 15:31:52 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1701959502; bh=Rwj1FTWb8pNejrfw5IvMH/pCufJXJ+2WE/ChCI+Iiu8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iUdbdzeg7ulN7L/o0mLWd/Qvwp6z4IOqZd6lMktvKRyvvYQDHU9QxIUWXIiYYO5OJ A5FfrXE9QBzI/H9MkE82XwsvTqm4GcxPIXWhZqwwhn7jf5zgbbfsuwqcg48BSWL0Zh xZ9KHUy2yJhgXus8C4YhVebUaqiEtRZm+vMMC8ZfAST4FSde8PF1wp+a3yNH9jlbEU jdrhs/uVK/gOpAQQQM6hWsvNNNKZ7ecOc+Umy8CTB8Imk1tOObRQ4IxpE60eZXrVHW XjSwonGFhchfv2XS0ldgK9nbIwhNTHZHQ3xZbSju4ePeN6IfaQyliStI95/9KjuAET c74n12AsWQi9w== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4SmGtQ2Gncz4xPf; Fri, 8 Dec 2023 01:31:42 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 8/8] util: Make sock_l4() treat empty string ifname like NULL Date: Fri, 8 Dec 2023 01:31:40 +1100 Message-ID: <20231207143140.1851378-9-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231207143140.1851378-1-david@gibson.dropbear.id.au> References: <20231207143140.1851378-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: PXCJTN7YQIINIJARPGO46UU5TNFEEYGR X-Message-ID-Hash: PXCJTN7YQIINIJARPGO46UU5TNFEEYGR X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- icmp.c | 15 ++++----------- udp.c | 6 ++---- util.c | 2 +- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/icmp.c b/icmp.c index d982fda..c82efd0 100644 --- a/icmp.c +++ b/icmp.c @@ -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); if ((s = icmp_id_map[V4][id].sock) <= 0) { - const char *bind_if; - - 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); + s = sock_l4(c, AF_INET, IPPROTO_ICMP, &c->ip4.addr_out, + c->ip4.ifname_out, id, iref.u32); if (s < 0) goto fail_sock; 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); 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, - &c->ip6.addr_out, bind_if, id, iref.u32); + &c->ip6.addr_out, + c->ip6.ifname_out, id, iref.u32); if (s < 0) goto fail_sock; if (s > FD_REF_MAX) { diff --git a/udp.c b/udp.c index f6e8b37..7057977 100644 --- a/udp.c +++ b/udp.c @@ -870,8 +870,7 @@ int udp_tap_handler(struct ctx *c, uint8_t pif, union udp_epoll_ref uref = { .port = src }; const char *bind_if = NULL; - if (!IN6_IS_ADDR_LOOPBACK(&s_in.sin_addr) && - *c->ip6.ifname_out) + if (!IN6_IS_ADDR_LOOPBACK(&s_in.sin_addr)) bind_if = c->ip6.ifname_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 }; const char *bind_if = NULL; - if (!IN6_IS_ADDR_LOOPBACK(&s_in6.sin6_addr) && - *c->ip6.ifname_out) + if (!IN6_IS_ADDR_LOOPBACK(&s_in6.sin6_addr)) bind_if = c->ip6.ifname_out; if (!IN6_IS_ADDR_UNSPECIFIED(&c->ip6.addr_out) && diff --git a/util.c b/util.c index 4de7b96..9c7012c 100644 --- a/util.c +++ b/util.c @@ -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))) debug("Failed to set SO_REUSEADDR on socket %i", fd); - if (ifname) { + if (ifname && *ifname) { /* Supported since kernel version 5.7, commit c427bfec18f2 * ("net: core: enable SO_BINDTODEVICE for non-root users"). If * it's unsupported, don't bind the socket at all, because the -- 2.43.0