Skip to contentSkip to conversation

net: enable udp v6 sockets receiving v4 packets with UDP

PATCH Applied Xin Long 5 participants 9 messages Feb 3, 2021 – Apr 2, 2022

Subsystems (4)
lore
Share & tools
lore

Xin Long

author
#0 lore raw

v5 cover net: enable udp v6 sockets receiving v4 packets with UDP

Currently, udp v6 socket can not process v4 packets with UDP GRO, as udp_encap_needed_key is not increased when udp_tunnel_encap_enable() is called for v6 socket.

This patchset is to increase it and remove the unnecessary code in bareudp in Patch 1/2, and improve rxrpc encap_enable by calling udp_tunnel_encap_enable().

v1->v4:
- See patch 1/2.
v4->v5:
- See patch 2/2.

Xin Long (2):
udp: call udp_encap_enable for v6 sockets when enabling encap
rxrpc: call udp_tunnel_encap_enable in rxrpc_open_socket

 drivers/net/bareudp.c    | 6 ------
 include/net/udp.h        | 1 +
 include/net/udp_tunnel.h | 3 +--
 net/ipv4/udp.c           | 6 ++++++
 net/ipv6/udp.c           | 4 +++-
 net/rxrpc/local_object.c | 7 ++-----
 6 files changed, 13 insertions(+), 14 deletions(-)
Signature · 1 line
2.1.0
author
#1 lore raw

PATCH v5 1/2 udp: call udp_encap_enable for v6 sockets when enabling encap

When enabling encap for a ipv6 socket without udp_encap_needed_key increased, UDP GRO won't work for v4 mapped v6 address packets as sk will be NULL in udp4_gro_receive().

This patch is to enable it by increasing udp_encap_needed_key for v6 sockets in udp_tunnel_encap_enable(), and correspondingly decrease udp_encap_needed_key in udpv6_destroy_sock().

v1->v2:
- add udp_encap_disable() and export it.
v2->v3:
- add the change for rxrpc and bareudp into one patch, as Alex suggested.
v3->v4:
- move rxrpc part to another patch.

---
 drivers/net/bareudp.c    | 6 ------
 include/net/udp.h        | 1 +
 include/net/udp_tunnel.h | 3 +--
 net/ipv4/udp.c           | 6 ++++++
 net/ipv6/udp.c           | 4 +++-
 5 files changed, 11 insertions(+), 9 deletions(-)
drivers/net/bareudp.c +0 -6
index 1b8f597..7511bca 100644
@@ -240,12 +240,6 @@ static int bareudp_socket_create(struct bareudp_dev *bareudp, __be16 port)
 	tunnel_cfg.encap_destroy = NULL;
 	setup_udp_tunnel_sock(bareudp->net, sock, &tunnel_cfg);
 
-	/* As the setup_udp_tunnel_sock does not call udp_encap_enable if the
-	 * socket type is v6 an explicit call to udp_encap_enable is needed.
-	 */
-	if (sock->sk->sk_family == AF_INET6)
-		udp_encap_enable();
-
 	rcu_assign_pointer(bareudp->sock, sock);
 	return 0;
 }
include/net/udp.h +1 -0
index 01351ba..5ddbb42 100644
@@ -467,6 +467,7 @@ void udp_init(void);
 
 DECLARE_STATIC_KEY_FALSE(udp_encap_needed_key);
 void udp_encap_enable(void);
+void udp_encap_disable(void);
 #if IS_ENABLED(CONFIG_IPV6)
 DECLARE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
 void udpv6_encap_enable(void);
include/net/udp_tunnel.h +1 -2
index 282d10e..afc7ce7 100644
@@ -181,9 +181,8 @@ static inline void udp_tunnel_encap_enable(struct socket *sock)
 #if IS_ENABLED(CONFIG_IPV6)
 	if (sock->sk->sk_family == PF_INET6)
 		ipv6_stub->udpv6_encap_enable();
-	else
 #endif
-		udp_encap_enable();
+	udp_encap_enable();
 }
 
 #define UDP_TUNNEL_NIC_MAX_TABLES	4
net/ipv4/udp.c +6 -0
index 69ea765..48208fb 100644
@@ -596,6 +596,12 @@ void udp_encap_enable(void)
 }
 EXPORT_SYMBOL(udp_encap_enable);
 
+void udp_encap_disable(void)
+{
+	static_branch_dec(&udp_encap_needed_key);
+}
+EXPORT_SYMBOL(udp_encap_disable);
+
 /* Handler for tunnels with arbitrary destination ports: no socket lookup, go
  * through error handlers in encapsulations looking for a match.
  */
net/ipv6/udp.c +3 -1
index b9f3dfd..d754292 100644
@@ -1608,8 +1608,10 @@ void udpv6_destroy_sock(struct sock *sk)
 			if (encap_destroy)
 				encap_destroy(sk);
 		}
-		if (up->encap_enabled)
+		if (up->encap_enabled) {
 			static_branch_dec(&udpv6_encap_needed_key);
+			udp_encap_disable();
+		}
 	}
 
 	inet6_destroy_sock(sk);
Signature · 1 line
2.1.0
author
#2 lore raw

PATCH v5 2/2 rxrpc: call udp_tunnel_encap_enable in rxrpc_open_socket

When doing encap_enable/increasing encap_needed_key, up->encap_enabled is not set in rxrpc_open_socket(), and it will cause encap_needed_key not being decreased in udpv6_destroy_sock().

This patch is to improve it by just calling udp_tunnel_encap_enable() where it increases both UDP and UDPv6 encap_needed_key and sets up->encap_enabled.

v4->v5:
- add the missing '#include <net/udp_tunnel.h>', as David Howells noticed.

---
 net/rxrpc/local_object.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
net/rxrpc/local_object.c +2 -5
index 8c28810..33b4936 100644
@@ -16,6 +16,7 @@
 #include <linux/hashtable.h>
 #include <net/sock.h>
 #include <net/udp.h>
+#include <net/udp_tunnel.h>
 #include <net/af_rxrpc.h>
 #include "ar-internal.h"
 
@@ -135,11 +136,7 @@ static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
 	udp_sk(usk)->gro_receive = NULL;
 	udp_sk(usk)->gro_complete = NULL;
 
-	udp_encap_enable();
-#if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
-	if (local->srx.transport.family == AF_INET6)
-		udpv6_encap_enable();
-#endif
+	udp_tunnel_encap_enable(local->socket);
 	usk->sk_error_report = rxrpc_error_report;
 
 	/* if a local address was supplied then bind it */
Signature · 1 line
2.1.0
maintainer
#3 lore raw

Re: [PATCHv5 net-next 2/2] rxrpc: call udp_tunnel_encap_enable in rxrpc_open_socket

Quoted 8 lines · Xin Long

When doing encap_enable/increasing encap_needed_key, up->encap_enabled is not set in rxrpc_open_socket(), and it will cause encap_needed_key not being decreased in udpv6_destroy_sock().

This patch is to improve it by just calling udp_tunnel_encap_enable() where it increases both UDP and UDPv6 encap_needed_key and sets up->encap_enabled.

Quoted 6 lines

v4->v5:
- add the missing '#include <net/udp_tunnel.h>', as David Howells noticed.

Looks good.

David

#4 lore raw

Re: [PATCHv5 net-next 1/2] udp: call udp_encap_enable for v6 sockets when enabling encap

In reply to Xin Long

Hi all,

Quoted 8 lines · Xin Long

When enabling encap for a ipv6 socket without udp_encap_needed_key increased, UDP GRO won't work for v4 mapped v6 address packets as sk will be NULL in udp4_gro_receive().

This patch is to enable it by increasing udp_encap_needed_key for v6 sockets in udp_tunnel_encap_enable(), and correspondingly decrease udp_encap_needed_key in udpv6_destroy_sock().

This is a non-negligible issue that other users (in or out of tree) may hit as well.

At OpenVPN we are developing a kernel device driver that has the same problem as UDP GRO. So far the only workaround is to let users upgrade to v5.12+.

I would like to propose to take this patch in stable releases. Greg, is this an option?

Commit in the linux kernel is:
a4a600dd301ccde6ea239804ec1f19364a39d643

Thanks a lot.

Best Regards,

Quoted 95 lines

v1->v2:
- add udp_encap_disable() and export it.
v2->v3:
- add the change for rxrpc and bareudp into one patch, as Alex suggested.
v3->v4:
- move rxrpc part to another patch.

---
  drivers/net/bareudp.c    | 6 ------
  include/net/udp.h        | 1 +
  include/net/udp_tunnel.h | 3 +--
  net/ipv4/udp.c           | 6 ++++++
  net/ipv6/udp.c           | 4 +++-
  5 files changed, 11 insertions(+), 9 deletions(-)
drivers/net/bareudp.c +0 -6
index 1b8f597..7511bca 100644
@@ -240,12 +240,6 @@ static int bareudp_socket_create(struct bareudp_dev *bareudp, __be16 port)
  	tunnel_cfg.encap_destroy = NULL;
  	setup_udp_tunnel_sock(bareudp->net, sock, &tunnel_cfg);
 
-	/* As the setup_udp_tunnel_sock does not call udp_encap_enable if the
-	 * socket type is v6 an explicit call to udp_encap_enable is needed.
-	 */
-	if (sock->sk->sk_family == AF_INET6)
-		udp_encap_enable();
-
  	rcu_assign_pointer(bareudp->sock, sock);
  	return 0;
  }
include/net/udp.h +1 -0
index 01351ba..5ddbb42 100644
@@ -467,6 +467,7 @@ void udp_init(void);
 
  DECLARE_STATIC_KEY_FALSE(udp_encap_needed_key);
  void udp_encap_enable(void);
+void udp_encap_disable(void);
  #if IS_ENABLED(CONFIG_IPV6)
  DECLARE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
  void udpv6_encap_enable(void);
include/net/udp_tunnel.h +1 -2
index 282d10e..afc7ce7 100644
@@ -181,9 +181,8 @@ static inline void udp_tunnel_encap_enable(struct socket *sock)
  #if IS_ENABLED(CONFIG_IPV6)
  	if (sock->sk->sk_family == PF_INET6)
  		ipv6_stub->udpv6_encap_enable();
-	else
  #endif
-		udp_encap_enable();
+	udp_encap_enable();
  }
 
  #define UDP_TUNNEL_NIC_MAX_TABLES	4
net/ipv4/udp.c +6 -0
index 69ea765..48208fb 100644
@@ -596,6 +596,12 @@ void udp_encap_enable(void)
  }
  EXPORT_SYMBOL(udp_encap_enable);
 
+void udp_encap_disable(void)
+{
+	static_branch_dec(&udp_encap_needed_key);
+}
+EXPORT_SYMBOL(udp_encap_disable);
+
  /* Handler for tunnels with arbitrary destination ports: no socket lookup, go
   * through error handlers in encapsulations looking for a match.
   */
net/ipv6/udp.c +3 -1
index b9f3dfd..d754292 100644
@@ -1608,8 +1608,10 @@ void udpv6_destroy_sock(struct sock *sk)
  			if (encap_destroy)
  				encap_destroy(sk);
  		}
-		if (up->encap_enabled)
+		if (up->encap_enabled) {
  			static_branch_dec(&udpv6_encap_needed_key);
+			udp_encap_disable();
+		}
  	}
 
  	inet6_destroy_sock(sk);
Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#5 lore raw

Re: [PATCHv5 net-next 1/2] udp: call udp_encap_enable for v6 sockets when enabling encap

Quoted 19 lines · Antonio Quartulli

Hi all,

Quoted 8 lines · Xin Long

When enabling encap for a ipv6 socket without udp_encap_needed_key increased, UDP GRO won't work for v4 mapped v6 address packets as sk will be NULL in udp4_gro_receive().

This patch is to enable it by increasing udp_encap_needed_key for v6 sockets in udp_tunnel_encap_enable(), and correspondingly decrease udp_encap_needed_key in udpv6_destroy_sock().

This is a non-negligible issue that other users (in or out of tree) may hit as well.

At OpenVPN we are developing a kernel device driver that has the same problem as UDP GRO. So far the only workaround is to let users upgrade to v5.12+.

Quoted 5 lines

I would like to propose to take this patch in stable releases. Greg, is this an option?

Commit in the linux kernel is:
a4a600dd301ccde6ea239804ec1f19364a39d643

What stable tree(s) should this apply to, and where have you tested it?

thanks,

greg k-h

#6 lore raw

Re: [PATCHv5 net-next 1/2] udp: call udp_encap_enable for v6 sockets when enabling encap

Hi,

Quoted 8 lines · Greg Kroah-Hartman
Quoted 5 lines

I would like to propose to take this patch in stable releases. Greg, is this an option?

Commit in the linux kernel is:
a4a600dd301ccde6ea239804ec1f19364a39d643

What stable tree(s) should this apply to, and where have you tested it?

Sorry for the delay, Greg, but I wanted to run some extra tests on the various longterm kernel releases.

This bug exists since "ever", therefore ideally it could/should be applied to all stable trees.

However, this patch applies as-is only to v5.10 and v5.4 (you need to ignore the hunk for 'drivers/net/bareudp.c' on the latter).

Older trees require a different code change.

My tests on v5.10 and v5.4 show that the patch works as expected.

Therefore, could it be backported to these 2 trees? It can get my

Thanks a lot,

Signature · 2 lines
Antonio Quartulli
OpenVPN Inc.
#7 lore raw

Re: [PATCHv5 net-next 1/2] udp: call udp_encap_enable for v6 sockets when enabling encap

Quoted 21 lines · Antonio Quartulli

Hi,

Quoted 8 lines · Greg Kroah-Hartman
Quoted 5 lines

I would like to propose to take this patch in stable releases. Greg, is this an option?

Commit in the linux kernel is:
a4a600dd301ccde6ea239804ec1f19364a39d643

What stable tree(s) should this apply to, and where have you tested it?

Sorry for the delay, Greg, but I wanted to run some extra tests on the various longterm kernel releases.

This bug exists since "ever", therefore ideally it could/should be applied to all stable trees.

However, this patch applies as-is only to v5.10 and v5.4 (you need to ignore the hunk for 'drivers/net/bareudp.c' on the latter).

Quoted 8 lines

Older trees require a different code change.

My tests on v5.10 and v5.4 show that the patch works as expected.

Therefore, could it be backported to these 2 trees? It can get my

Now queued up, thanks.

greg k-h

#8 lore raw
In reply to Xin Long
This series was applied to netdev/net-next.git (refs/heads/master):

Hello:

This series was applied to netdev/net-next.git (refs/heads/master):

Quoted 9 lines · On Wed, 3 Feb 2021 16:54:21 +0800 you wrote:

Currently, udp v6 socket can not process v4 packets with UDP GRO, as udp_encap_needed_key is not increased when udp_tunnel_encap_enable() is called for v6 socket.

This patchset is to increase it and remove the unnecessary code in bareudp in Patch 1/2, and improve rxrpc encap_enable by calling udp_tunnel_encap_enable().

[...]

Here is the summary with links:
- [PATCHv5,net-next,1/2] udp: call udp_encap_enable for v6 sockets when enabling encap

    https://git.kernel.org/netdev/net-next/c/a4a600dd301c
  - [PATCHv5,net-next,2/2] rxrpc: call udp_tunnel_encap_enable in rxrpc_open_socket
    https://git.kernel.org/netdev/net-next/c/5d30c626b67e

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html