public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 6/8] icmp: Avoid unnecessary handling of unspecified bind address
Date: Fri,  8 Dec 2023 01:31:38 +1100	[thread overview]
Message-ID: <20231207143140.1851378-7-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20231207143140.1851378-1-david@gibson.dropbear.id.au>

We go to some trouble, if the configured output address is unspecified, to
pass NULL to sock_l4().  But while passing NULL is one way to get sock_l4()
not to specify a bind address, passing the "any" address explicitly works
too.  Use this to simplify icmp_tap_handler().

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 icmp.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

diff --git a/icmp.c b/icmp.c
index 2a15d25..d982fda 100644
--- a/icmp.c
+++ b/icmp.c
@@ -187,16 +187,12 @@ 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 struct in_addr *bind_addr = NULL;
 			const char *bind_if;
 
 			bind_if = *c->ip4.ifname_out ? c->ip4.ifname_out : NULL;
 
-			if (!IN4_IS_ADDR_UNSPECIFIED(&c->ip4.addr_out))
-				bind_addr = &c->ip4.addr_out;
-
-			s = sock_l4(c, AF_INET, IPPROTO_ICMP, bind_addr,
-				    bind_if, id, iref.u32);
+			s = sock_l4(c, AF_INET, IPPROTO_ICMP,
+				    &c->ip4.addr_out, bind_if, id, iref.u32);
 			if (s < 0)
 				goto fail_sock;
 			if (s > FD_REF_MAX) {
@@ -241,16 +237,12 @@ 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 struct in6_addr *bind_addr = NULL;
 			const char *bind_if;
 
 			bind_if = *c->ip6.ifname_out ? c->ip6.ifname_out : NULL;
 
-			if (!IN6_IS_ADDR_UNSPECIFIED(&c->ip6.addr_out))
-				bind_addr = &c->ip6.addr_out;
-
-			s = sock_l4(c, AF_INET6, IPPROTO_ICMPV6, bind_addr,
-				    bind_if, id, iref.u32);
+			s = sock_l4(c, AF_INET6, IPPROTO_ICMPV6,
+				    &c->ip6.addr_out, bind_if, id, iref.u32);
 			if (s < 0)
 				goto fail_sock;
 			if (s > FD_REF_MAX) {
-- 
@@ -187,16 +187,12 @@ 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 struct in_addr *bind_addr = NULL;
 			const char *bind_if;
 
 			bind_if = *c->ip4.ifname_out ? c->ip4.ifname_out : NULL;
 
-			if (!IN4_IS_ADDR_UNSPECIFIED(&c->ip4.addr_out))
-				bind_addr = &c->ip4.addr_out;
-
-			s = sock_l4(c, AF_INET, IPPROTO_ICMP, bind_addr,
-				    bind_if, id, iref.u32);
+			s = sock_l4(c, AF_INET, IPPROTO_ICMP,
+				    &c->ip4.addr_out, bind_if, id, iref.u32);
 			if (s < 0)
 				goto fail_sock;
 			if (s > FD_REF_MAX) {
@@ -241,16 +237,12 @@ 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 struct in6_addr *bind_addr = NULL;
 			const char *bind_if;
 
 			bind_if = *c->ip6.ifname_out ? c->ip6.ifname_out : NULL;
 
-			if (!IN6_IS_ADDR_UNSPECIFIED(&c->ip6.addr_out))
-				bind_addr = &c->ip6.addr_out;
-
-			s = sock_l4(c, AF_INET6, IPPROTO_ICMPV6, bind_addr,
-				    bind_if, id, iref.u32);
+			s = sock_l4(c, AF_INET6, IPPROTO_ICMPV6,
+				    &c->ip6.addr_out, bind_if, id, iref.u32);
 			if (s < 0)
 				goto fail_sock;
 			if (s > FD_REF_MAX) {
-- 
2.43.0


  parent reply	other threads:[~2023-12-07 14:31 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-07 14:31 [PATCH 0/8] Small cleanups related to addresses and binding David Gibson
2023-12-07 14:31 ` [PATCH 1/8] tcp: Fix address type for tcp_sock_init_af() David Gibson
2023-12-27 20:25   ` Stefano Brivio
2023-12-28  2:42     ` David Gibson
2023-12-28 10:11       ` Stefano Brivio
2024-01-07  5:35         ` David Gibson
2024-01-13 22:50           ` Stefano Brivio
2023-12-07 14:31 ` [PATCH 2/8] treewide: Use IN4ADDR_LOOPBACK_INIT more widely David Gibson
2023-12-07 14:31 ` [PATCH 3/8] treewide: Add IN4ADDR_ANY_INIT macro David Gibson
2023-12-07 14:31 ` [PATCH 4/8] util: Use htonl_constant() in more places David Gibson
2023-12-07 14:31 ` [PATCH 5/8] util: Improve sockaddr initialisation in sock_l4() David Gibson
2023-12-27 20:25   ` Stefano Brivio
2024-01-07  5:34     ` David Gibson
2023-12-07 14:31 ` David Gibson [this message]
2023-12-07 14:31 ` [PATCH 7/8] treewide: Avoid in_addr_t David Gibson
2023-12-07 14:31 ` [PATCH 8/8] util: Make sock_l4() treat empty string ifname like NULL David Gibson
2023-12-27 20:25 ` [PATCH 0/8] Small cleanups related to addresses and binding Stefano Brivio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231207143140.1851378-7-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).