Skip to contentSkip to conversation

ovpn: properly deconfigure UDP-tunnel

PATCH In review Antonio Quartulli 3 participants 6 messages May 14, 2025 – May 18, 2025

Subsystems (3)
lore
Share & tools
lore

Antonio Quartulli

author maintainer
#0 lore raw

PATCH 1/1 ovpn: properly deconfigure UDP-tunnel

When deconfiguring a UDP-tunnel from a socket, we cannot call setup_udp_tunnel_sock(), as it is expected to be invoked only during setup.

Implement a new function named cleanup_udp_tunnel_sock(), that reverts was what done during setup, and invoke it.

This new function takes care of reverting everything that was done by setup_udp_tunnel_sock() (i.e. unsetting various sk members and cleaning up the encap state in the kernel).

---

@Jakub: I am sending this directly to net-next as it'd be better reviewed by the broader audience on netdev right away.

@Paolo: I think this is the best way to handle the UDP tunnel cleanup. Doing nothing is not truly an option because we leave all the encap state enabled and this may lead to other issues. cleanup_udp_tunnel_sock() will just undo anything that setup_udp_tunnel_sock() had done during setup.

 drivers/net/ovpn/udp.c     |  5 +----
 include/net/udp_tunnel.h   |  1 +
 net/ipv4/udp_tunnel_core.c | 30 ++++++++++++++++++++++++++++++
 3 files changed, 32 insertions(+), 4 deletions(-)
drivers/net/ovpn/udp.c +1 -4
index aef8c0406ec9..137b58d49569 100644
@@ -442,8 +442,5 @@ int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock,
  */
 void ovpn_udp_socket_detach(struct ovpn_socket *ovpn_sock)
 {
-	struct udp_tunnel_sock_cfg cfg = { };
-
-	setup_udp_tunnel_sock(sock_net(ovpn_sock->sock->sk), ovpn_sock->sock,
-			      &cfg);
+	cleanup_udp_tunnel_sock(ovpn_sock->sock);
 }
include/net/udp_tunnel.h +1 -0
index 2df3b8344eb5..387445133eb7 100644
@@ -92,6 +92,7 @@ struct udp_tunnel_sock_cfg {
 /* Setup the given (UDP) sock to receive UDP encapsulated packets */
 void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
 			   struct udp_tunnel_sock_cfg *sock_cfg);
+void cleanup_udp_tunnel_sock(struct socket *sock);
 
 /* -- List of parsable UDP tunnel types --
  *
net/ipv4/udp_tunnel_core.c +30 -0
index 2326548997d3..83e9fd2d499e 100644
@@ -98,6 +98,36 @@ void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
 }
 EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
 
+void cleanup_udp_tunnel_sock(struct socket *sock)
+{
+	struct sock *sk = sock->sk;
+
+	/* Re-enable multicast loopback */
+	inet_set_bit(MC_LOOP, sk);
+
+	/* Disable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
+	inet_dec_convert_csum(sk);
+
+	rcu_assign_sk_user_data(sk, NULL);
+
+	udp_sk(sk)->encap_type = 0;
+	udp_sk(sk)->encap_rcv = NULL;
+	udp_sk(sk)->encap_err_rcv = NULL;
+	udp_sk(sk)->encap_err_lookup = NULL;
+	udp_sk(sk)->encap_destroy = NULL;
+	udp_sk(sk)->gro_receive = NULL;
+	udp_sk(sk)->gro_complete = NULL;
+
+	udp_clear_bit(ENCAP_ENABLED, sk);
+#if IS_ENABLED(CONFIG_IPV6)
+	if (READ_ONCE(sk->sk_family) == PF_INET6)
+		static_branch_dec(&udpv6_encap_needed_key);
+#endif
+	udp_encap_disable();
+	udp_tunnel_cleanup_gro(sk);
+}
+EXPORT_SYMBOL_GPL(cleanup_udp_tunnel_sock);
+
 void udp_tunnel_push_rx_port(struct net_device *dev, struct socket *sock,
 			     unsigned short type)
 {
Signature · 1 line
2.49.0
maintainer
#1 lore raw

net/ipv4/udp_tunnel_core.c void setup_udp_tunnel_sock(struct net *net, struct socket *sock, · line 124

Quoted 3 lines · On Wed, 14 May 2025 11:58:42 +0200 Antonio Quartulli wrote:
net/ipv4/udp_tunnel_core.c +3 -0
+#if IS_ENABLED(CONFIG_IPV6)
+	if (READ_ONCE(sk->sk_family) == PF_INET6)
+		static_branch_dec(&udpv6_encap_needed_key);

udpv6_encap_needed_key is only exported for ipv6.ko I think you need to correct the export, because with IPV6=y and UDP_TUNNEL=m we get:

ERROR: modpost: "udpv6_encap_needed_key" [net/ipv4/udp_tunnel.ko] undefined!

Signature · 1 line
pw-bot: cr
author maintainer
#2 lore raw
Quoted 10 lines · Jakub Kicinski

net/ipv4/udp_tunnel_core.c void setup_udp_tunnel_sock(struct net *net, struct socket *sock, · line 124

Quoted 3 lines · On Wed, 14 May 2025 11:58:42 +0200 Antonio Quartulli wrote:
net/ipv4/udp_tunnel_core.c +3 -0
+#if IS_ENABLED(CONFIG_IPV6)
+	if (READ_ONCE(sk->sk_family) == PF_INET6)
+		static_branch_dec(&udpv6_encap_needed_key);

udpv6_encap_needed_key is only exported for ipv6.ko I think you need to correct the export, because with IPV6=y and UDP_TUNNEL=m we get:

ERROR: modpost: "udpv6_encap_needed_key" [net/ipv4/udp_tunnel.ko] undefined!

Or define and export udpv6_encap_enable() which calls only the static_branch_dec()? Basically the same as the current udpv6_encap_enable().

This way we don't need to touch the export for udpv6_encap_needed_key.

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
author maintainer
#3 lore raw
Quoted 8 lines · Antonio Quartulli
Quoted 5 lines · Jakub Kicinski
Quoted 3 lines · On Wed, 14 May 2025 11:58:42 +0200 Antonio Quartulli wrote:

+#if IS_ENABLED(CONFIG_IPV6)
+    if (READ_ONCE(sk->sk_family) == PF_INET6) +        static_branch_dec(&udpv6_encap_needed_key);

Quoted 7 lines
Quoted 5 lines

udpv6_encap_needed_key is only exported for ipv6.ko I think you need to correct the export, because with IPV6=y and UDP_TUNNEL=m we get:

ERROR: modpost: "udpv6_encap_needed_key" [net/ipv4/udp_tunnel.ko] undefined!

Or define and export udpv6_encap_enable() which calls only the

EDIT: I meant udpv6_encap_disable() here ^^

Quoted 7 lines

static_branch_dec()?
Basically the same as the current udpv6_encap_enable().

This way we don't need to touch the export for udpv6_encap_needed_key.

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
author maintainer
#4 lore raw
In reply to Antonio Quartulli

drivers/net/ovpn/udp.c int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock, · line 445

Quoted 11 lines · Antonio Quartulli
drivers/net/ovpn/udp.c +1 -4
@@ -442,8 +442,5 @@ int ovpn_udp_socket_attach(struct ovpn_socket *ovpn_sock,
   */
  void ovpn_udp_socket_detach(struct ovpn_socket *ovpn_sock)
  {
-	struct udp_tunnel_sock_cfg cfg = { };
-
-	setup_udp_tunnel_sock(sock_net(ovpn_sock->sock->sk), ovpn_sock->sock,
-			      &cfg);
+	cleanup_udp_tunnel_sock(ovpn_sock->sock);

After looking at this some more, I think this is still racy.

We have no guarantee that sock->sk won't be nullified while executing cleanup_udp_tunnel_sock().

The alternative would be to change cleanup_udp_tunnel_sock() to take a 'struct sock' argument (the 'struct socket' container is not really useful).

I see that also setup_udp_tunnel_sock() takes a 'struct socket' argument although there is no need for that. Is there some kind of abstraction in udp_tunnel which wants the user to always pass 'struct socket' instead of 'struct sock' ?

Regards,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#5 lore raw
In reply to Antonio Quartulli
kernel test robot noticed the following build errors:

Hi Antonio,

kernel test robot noticed the following build errors:

[auto build test ERROR on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Antonio-Quartulli/ovpn-properly-deconfigure-UDP-tunnel/20250514-180029
base:   net-next/main
patch link:    https://lore.kernel.org/r/20250514095842.12067-1-antonio%40openvpn.net
patch subject: [PATCH net-next] ovpn: properly deconfigure UDP-tunnel
config: x86_64-rhel-9.4 (https://download.01.org/0day-ci/archive/20250518/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250518/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "udpv6_encap_needed_key" [net/ipv4/udp_tunnel.ko] undefined!
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki