Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next] net: use inet_rcv_saddr_any() in udp_tunnel and ping
@ 2026-11-16 20:08 Antonio Quartulli
  0 siblings, 0 replies; only message in thread
From: Antonio Quartulli @ 2026-11-16 20:08 UTC (permalink / raw)
  To: netdev
  Cc: Antonio Quartulli, Kuniyuki Iwashima, David Ahern, Ido Schimmel,
	David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Neal Cardwell, linux-kernel

udp_tunnel_core.c carries its own copy of inet_rcv_saddr_any(),
sk_saddr_any(), as it can be built as a module while
inet_rcv_saddr_any() is not exported, and ping_bind() open-codes the
same check for its AF_INET and AF_INET6 sockets.

Make inet_rcv_saddr_any() a static inline in <net/addrconf.h>, where it
is already declared, and use it in both places.

Suggested-by: Kuniyuki Iwashima <kuniyu@google.com>
Link: https://lore.kernel.org/netdev/avVVzxL3pVZc6yQ5@google.com/
Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---

@Kuniyuki:
the helper can't go to <net/sock.h> as suggested, since it needs
ipv6_addr_any() and <net/ipv6.h> includes <net/sock.h>.
I hope <net/addrconf.h> is fine too.


 include/net/addrconf.h          | 12 +++++++++++-
 net/ipv4/inet_connection_sock.c |  9 ---------
 net/ipv4/ping.c                 |  8 ++------
 net/ipv4/udp_tunnel_core.c      | 12 ++----------
 4 files changed, 15 insertions(+), 26 deletions(-)

diff --git a/include/net/addrconf.h b/include/net/addrconf.h
index 6818057cf727..0ec4db881404 100644
--- a/include/net/addrconf.h
+++ b/include/net/addrconf.h
@@ -149,7 +149,17 @@ int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
 		    u32 banned_flags);
 bool inet_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2,
 			  bool match_wildcard);
-bool inet_rcv_saddr_any(const struct sock *sk);
+
+/* whether @sk is bound to the wildcard address of its family */
+static inline bool inet_rcv_saddr_any(const struct sock *sk)
+{
+#if IS_ENABLED(CONFIG_IPV6)
+	if (sk->sk_family == AF_INET6)
+		return ipv6_addr_any(&sk->sk_v6_rcv_saddr);
+#endif
+	return !sk->sk_rcv_saddr;
+}
+
 void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr);
 void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr);
 
diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
index 5807067083c9..c6f7c3550013 100644
--- a/net/ipv4/inet_connection_sock.c
+++ b/net/ipv4/inet_connection_sock.c
@@ -109,15 +109,6 @@ bool inet_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2,
 				    match_wildcard);
 }
 
-bool inet_rcv_saddr_any(const struct sock *sk)
-{
-#if IS_ENABLED(CONFIG_IPV6)
-	if (sk->sk_family == AF_INET6)
-		return ipv6_addr_any(&sk->sk_v6_rcv_saddr);
-#endif
-	return !sk->sk_rcv_saddr;
-}
-
 /**
  *	inet_sk_get_local_port_range - fetch ephemeral ports range
  *	@sk: socket
diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
index 223a0108b74c..ceb0af8c70a3 100644
--- a/net/ipv4/ping.c
+++ b/net/ipv4/ping.c
@@ -40,11 +40,11 @@
 #include <net/route.h>
 #include <net/inet_common.h>
 #include <net/checksum.h>
+#include <net/addrconf.h>
 
 #if IS_ENABLED(CONFIG_IPV6)
 #include <linux/in6.h>
 #include <linux/icmpv6.h>
-#include <net/addrconf.h>
 #include <net/ipv6.h>
 #include <net/transp_v6.h>
 #endif
@@ -439,12 +439,8 @@ int ping_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len)
 		 sk->sk_bound_dev_if);
 
 	err = 0;
-	if (sk->sk_family == AF_INET && isk->inet_rcv_saddr)
+	if (!inet_rcv_saddr_any(sk))
 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
-#if IS_ENABLED(CONFIG_IPV6)
-	if (sk->sk_family == AF_INET6 && !ipv6_addr_any(&sk->sk_v6_rcv_saddr))
-		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
-#endif
 
 	if (snum)
 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
diff --git a/net/ipv4/udp_tunnel_core.c b/net/ipv4/udp_tunnel_core.c
index 29581c94fdcf..c2888aa2bbe6 100644
--- a/net/ipv4/udp_tunnel_core.c
+++ b/net/ipv4/udp_tunnel_core.c
@@ -3,6 +3,7 @@
 #include <linux/errno.h>
 #include <linux/socket.h>
 #include <linux/kernel.h>
+#include <net/addrconf.h>
 #include <net/dst_metadata.h>
 #include <net/flow.h>
 #include <net/udp.h>
@@ -59,15 +60,6 @@ int udp_sock_create4(struct net *net, struct udp_port_cfg *cfg,
 }
 EXPORT_SYMBOL(udp_sock_create4);
 
-static bool sk_saddr_any(struct sock *sk)
-{
-#if IS_ENABLED(CONFIG_IPV6)
-	if (sk->sk_family == AF_INET6)
-		return ipv6_addr_any(&sk->sk_v6_rcv_saddr);
-#endif
-	return !sk->sk_rcv_saddr;
-}
-
 void setup_udp_tunnel_sock(struct net *net, struct sock *sk,
 			   struct udp_tunnel_sock_cfg *cfg)
 {
@@ -91,7 +83,7 @@ void setup_udp_tunnel_sock(struct net *net, struct sock *sk,
 
 	udp_tunnel_update_gro_rcv(sk, true);
 
-	if (!sk->sk_dport && !sk->sk_bound_dev_if && sk_saddr_any(sk) &&
+	if (!sk->sk_dport && !sk->sk_bound_dev_if && inet_rcv_saddr_any(sk) &&
 	    sk->sk_kern_sock)
 		udp_tunnel_update_gro_lookup(net, sk, true);
 }

base-commit: 7ccc5e05043b1bb84326b19a2b05938d8f7a2ae6
-- 
2.56.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-16 20:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 20:08 [PATCH net-next] net: use inet_rcv_saddr_any() in udp_tunnel and ping Antonio Quartulli

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox