esp: Consolidate esp4 and esp6.
Subsystems (3)
Steffen Klassert
maintainerPATCH 1/1 esp: Consolidate esp4 and esp6.
This patch merges common code of esp4.c and esp6.c into xfrm_esp.c. This almost halves the size of the ESP implementation for the price of three indirect calls on UDP/TCP encapsulation. No functional changes.
Changes from the RFC version:
- Fix a typo in the commit mesage.
- Remove some old comments that don't make sense anymore.
- Let the ->input_encap functions return the needed offsets.
- Remove the IP_MAX_MTU check from UDP/TCP encap. The IPv4/IPv6 local_out function will do that ceck later.
- The comment on IPv4 ESP offload with UDP encapsulation is true for IPv4 and IPv6, so remove the IPv4 from the comment.
--- include/net/esp.h | 6 + include/net/xfrm.h | 4 + net/ipv4/esp4.c | 1065 ++------------------------------------ net/ipv6/esp6.c | 1081 ++------------------------------------- net/ipv6/esp6_offload.c | 6 +- net/xfrm/Makefile | 1 + net/xfrm/xfrm_esp.c | 1017 ++++++++++++++++++++++++++++++++++++ 7 files changed, 1132 insertions(+), 2048 deletions(-) create mode 100644 net/xfrm/xfrm_esp.c
include/net/esp.h +6 -0
@@ -47,4 +47,10 @@ int esp_input_done2(struct sk_buff *skb, int err);int esp6_output_head (struct xfrm_state *x,struct sk_buff *skb,struct esp_info *esp);int esp6_output_tail (struct xfrm_state *x,struct sk_buff *skb,struct esp_info *esp);int esp6_input_done2 (struct sk_buff *skb,int err); +int esp_init_aead (struct xfrm_state *x,struct netlink_ext_ack *extack); +int esp_init_authenc (struct xfrm_state *x,struct netlink_ext_ack *extack); +void esp_destroy (struct xfrm_state *x); +int esp_input (struct xfrm_state *x,struct sk_buff *skb); +int esp_output (struct xfrm_state *x,struct sk_buff *skb); +#endif
include/net/xfrm.h +4 -0
@@ -455,7 +455,11 @@ struct xfrm_type { struct netlink_ext_ack *extack);void (*destructor)(struct xfrm_state *);int (*input)(struct xfrm_state *,struct sk_buff *skb); +int (*input_encap)(struct sk_buff *skb,struct xfrm_state *x);int (*output)(struct xfrm_state *,struct sk_buff *pskb); +struct sock *(*find_tcp_sk)(struct xfrm_state *x); +void (*output_encap_csum)(struct sk_buff *skb); +int (*output_tcp_encap)(struct xfrm_state *x,struct sk_buff *skb);int (*reject)(struct xfrm_state *,struct sk_buff *,const struct flowi *); };
net/ipv4/esp4.c +49 -1016
1,118 lines not on this page · raw message
net/ipv6/esp6.c +52 -1029
1,140 lines not on this page · raw message
net/ipv6/esp6_offload.c +3 -3
@@ -297,7 +297,7 @@ static int esp6_input_tail(struct xfrm_state *x, struct sk_buff *skb)if (!(xo->flags &CRYPTO_DONE )) skb->ip_summed =CHECKSUM_NONE ; -return esp6_input_done2 (skb,0 ); +return esp_input_done2 (skb,0 ); }static int esp6_xmit (struct xfrm_state *x,struct sk_buff *skb,netdev_features_t features)
@@ -340,7 +340,7 @@ static int esp6_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features esp.tailen = esp.tfclen + esp.plen + alen;if (!hw_offload || !skb_is_gso (skb)) { - esp.nfrags =esp6_output_head (x, skb, &esp); + esp.nfrags =esp_output_head (x, skb, &esp);if (esp.nfrags <0 )return esp.nfrags; }
@@ -384,7 +384,7 @@ static int esp6_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_featuresreturn 0 ; } - err =esp6_output_tail (x, skb, &esp); + err =esp_output_tail (x, skb, &esp);if (err)return err;
net/xfrm/Makefile +1 -0
@@ -24,3 +24,4 @@ obj-$(CONFIG_XFRM_INTERFACE) += xfrm_interface.o obj-$(CONFIG_XFRM_IPTFS) += xfrm_iptfs.o obj-$(CONFIG_XFRM_ESPINTCP) += espintcp.o obj-$(CONFIG_DEBUG_INFO_BTF) += xfrm_state_bpf.o +obj-$(CONFIG_XFRM_ESP) += xfrm_esp.o
net/xfrm/xfrm_esp.c new file +1017 -0
693 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.43.0
Part of the diff is not shown here. View the raw message.
Simon Horman
reviewerOn Thu, Feb 26, 2026 at 08:53:37AM +0100, Steffen Klassert wrote:
...
Quoted 14 lines
net/ipv6/esp6.c +0
-4@@ -11,130 +11,14 @@* This file is derived from net/ipv4/esp.c */ -#define pr_fmt (fmt) "IPv6: " fmt -#include <crypto/aead.h> #include <crypto/authenc.h> -#include <linux/err.h> -#include <linux/module.h>
net/ipv6/esp6.c line 29
Quoted 10 lines
net/ipv6/esp6.c +0
-8#include <net/ip.h> -#include <net/xfrm.h> #include <net/esp.h> -#include <linux/scatterlist.h> -#include <linux/kernel.h> -#include <linux/pfkeyv2.h> -#include <linux/random.h> -#include <linux/slab.h> -#include <linux/spinlock.h> -#include <net/ip6_checksum.h>
Hi Steffen,
I think you need to keep including ip6_checksum.h
as the code still calls csum_ipv6_magic()
...
Quoted 8 lines · Steffen Klassert
This patch merges common code of
esp4.candesp6.cintoxfrm_esp.c. This almost halves the size of the ESP implementation for the price of three indirect calls on UDP/TCP encapsulation. No functional changes.Changes from the RFC version:
- Fix a typo in the commit mesage.
the commit "mesage"? :)
Quoted 29 lines
- Remove some old comments that don't make sense anymore.
- Let the ->input_encap functions return the needed offsets.
- Remove the
IP_MAX_MTUcheck from UDP/TCP encap. The IPv4/IPv6local_outfunction will do that ceck later.- The comment on IPv4 ESP offload with UDP encapsulation is true for IPv4 and IPv6, so remove the IPv4 from the comment.
Signed-off-by Steffen Klassert <[email protected]>Tested-by Tobias Brunner <[email protected]>--- include/net/esp.h | 6 + include/net/xfrm.h | 4 + net/ipv4/esp4.c | 1065 ++------------------------------------ net/ipv6/esp6.c | 1081 ++------------------------------------- net/ipv6/esp6_offload.c | 6 +- net/xfrm/Makefile | 1 + net/xfrm/xfrm_esp.c | 1017 ++++++++++++++++++++++++++++++++++++ 7 files changed, 1132 insertions(+), 2048 deletions(-) create mode 100644 net/xfrm/xfrm_esp.cinclude/net/esp.h +0
-0
include/net/esp.h int esp_input_done2(struct sk_buff *skb, int err); · line 49
Quoted 3 lines
include/net/esp.h +0
-0@@ -47,4 +47,10 @@ int esp_input_done2(struct sk_buff *skb, int err);int esp6_output_head (struct xfrm_state *x,struct sk_buff *skb,struct esp_info *esp);int esp6_output_tail (struct xfrm_state *x,struct sk_buff *skb,struct esp_info *esp);int esp6_input_done2 (struct sk_buff *skb,int err);
Those 3 are gone now and can be removed.
Quoted 12 lines
include/net/esp.h +6
-0+int esp_init_aead (struct xfrm_state *x,struct netlink_ext_ack *extack); +int esp_init_authenc (struct xfrm_state *x,struct netlink_ext_ack *extack); +void esp_destroy (struct xfrm_state *x); +int esp_input (struct xfrm_state *x,struct sk_buff *skb); +int esp_output (struct xfrm_state *x,struct sk_buff *skb); +#endif include/net/xfrm.h +0
-0
include/net/xfrm.h struct xfrm_type { · line 462
Quoted 8 lines
include/net/xfrm.h +4
-0@@ -455,7 +455,11 @@ struct xfrm_type { struct netlink_ext_ack *extack);void (*destructor)(struct xfrm_state *);int (*input)(struct xfrm_state *,struct sk_buff *skb); +int (*input_encap)(struct sk_buff *skb,struct xfrm_state *x);int (*output)(struct xfrm_state *,struct sk_buff *pskb); +struct sock *(*find_tcp_sk)(struct xfrm_state *x); +void (*output_encap_csum)(struct sk_buff *skb); +int (*output_tcp_encap)(struct xfrm_state *x,struct sk_buff *skb);
This CB is unused.
Quoted 4 lines
net/ipv4/esp4.c +0
-0
[...]
Quoted 5 lines
net/ipv4/esp4.c +3
-0+static int esp4_input_encap (struct sk_buff *skb,struct xfrm_state *x) { +const struct iphdr *iph =ip_hdr (skb); +int ihl = iph->ihl *4 ;struct xfrm_encap_tmpl *encap = x->encap;
[...]
net/ipv4/esp4.c static struct sock *esp_find_tcp_sk(struct xfrm_state *x) · line 61
Quoted 10 lines
net/ipv4/esp4.c +7
-1+struct tcphdr *th = (void *)(skb_network_header (skb) + ihl); +struct udphdr *uh = (void *)(skb_network_header (skb) + ihl); +int ret =0 ; +__be16 source; +switch (x->encap->encap_type) {case TCP_ENCAP_ESPINTCP : - esph =esp_output_tcp_encap (x, skb, esp); + source = th->source; + ret -=sizeof (struct tcphdr );
I'm trying to check how all those offset combine, isn't that susceptible to the same problem as 17175d1a27c6 ("xfrm: esp6: fix encapsulation header offset computation")?
Also, since those new ->input_encap should return a negative value on success, the "return -1" for error is a bit confusing. Maybe:
- make them return +1 on error, without going through "goto out" (return -1 on error should be fine since that's not an expected offset)
- make the final return (the one that returns the negative offset) have a DEBUG_NET_WARN_ON_ONCE(ret >= 0)
Or if my math is correct, the caller's hdr_len after adding ->input_encap's return value is the full length of the ipv* header including options/extensions, so let ->input_encap return that directly instead of doing the "hdr_len += ret" dance (and keep the return -1 on error)? That would make things a bit clearer than "skb_network_header_len + skb_network_offset + [whatever ipv6_skip_exthdr returns]".
[...]
net/ipv4/esp4.c static int esp4_rcv_cb(struct sk_buff *skb, int err) · line 198
Quoted 13 lines
net/ipv4/esp4.c +2
-7@@ -1164,13 +194,16 @@ static int esp4_rcv_cb(struct sk_buff *skb, int err)static const struct xfrm_type esp_type = { - .owner =THIS_MODULE , - .proto =IPPROTO_ESP , - .flags =XFRM_TYPE_REPLAY_PROT , - .init_state = esp_init_state, - .destructor = esp_destroy, - .input = esp_input, - .output = esp_output, + .owner =THIS_MODULE , + .proto =IPPROTO_ESP ,
nit: since you're reworking the whitespace, this is:
\t.proto\t \t\t= IPPROTO_ESP,
(with a few spaces in the middle)
[...]
Quoted 4 lines
net/ipv6/esp6.c +0
-0
[...]
Quoted 2 lines
net/ipv6/esp6.c +0
-2-int esp6_input_done2 (struct sk_buff *skb,int err) -{
[...]
net/ipv6/esp6.c static void esp_output_encap_csum(struct sk_buff *skb) · line 821
Quoted 13 lines
net/ipv6/esp6.c +0
-11-/* -* 2) ignore UDP/TCP checksums in case -* of NAT-T in Transport Mode, or -* perform other post-processing fixes -* as per draft-ietf-ipsec-udp-encaps-06, -* section 3.1.2 -*/ -if (x->props.mode ==XFRM_MODE_TRANSPORT ) - skb->ip_summed =CHECKSUM_UNNECESSARY ; } -skb_postpull_rcsum (skb,skb_network_header (skb), -skb_network_header_len (skb));
This doesn't get carried over to the new common function esp_input_done2. It originally came from commit a9b28c2bf05d ("esp6:
Fix RX checksum after header pull"), not sure if it's still needed but I guess so?
Quoted 15 lines
net/ipv6/esp6.c +0
-15-skb_pull_rcsum (skb, hlen); -if (x->props.mode ==XFRM_MODE_TUNNEL || - x->props.mode ==XFRM_MODE_IPTFS ) -skb_reset_transport_header (skb); -else -skb_set_transport_header (skb, -hdr_len); - -/* RFC4303: Drop dummy packets without any error */ -if (err ==IPPROTO_NONE ) - err = -EINVAL ; - -out: -return err; -} -EXPORT_SYMBOL_GPL (esp6_input_done2);
[...]
Quoted 5 lines
net/xfrm/xfrm_esp.c new file +0
-0
[...]
net/xfrm/xfrm_esp.c line 311
Quoted 6 lines
net/xfrm/xfrm_esp.c new file +6
-0+ + +#ifdef CONFIG_INET_ESPINTCP +static struct ip_esp_hdr *esp_output_tcp_encap (struct xfrm_state *x, +struct sk_buff *skb, +struct esp_info *esp)
nit: alignment
(checkpatch complains about this and a few other whitespace things in this new file, probably all carried over from the existing code)
[...]
net/xfrm/xfrm_esp.c line 1017
Quoted 5 lines
net/xfrm/xfrm_esp.c new file +5
-0+EXPORT_SYMBOL_GPL (esp_init_authenc); + +MODULE_LICENSE ("GPL" ); +MODULE_DESCRIPTION ("Generic ESP" ); +
nit: git complains about the empty line
Signature · 1 line
Sabrina
Steffen Klassert
maintainerQuoted 11 lines · Sabrina Dubroca
Quoted 8 lines · Steffen Klassert
This patch merges common code of
esp4.candesp6.cintoxfrm_esp.c. This almost halves the size of the ESP implementation for the price of three indirect calls on UDP/TCP encapsulation. No functional changes.Changes from the RFC version:
- Fix a typo in the commit mesage.
the commit "mesage"? :)
It was not AI generated :)
Quoted 24 lines
Quoted 24 lines
- Remove some old comments that don't make sense anymore.
- Let the ->input_encap functions return the needed offsets.
- Remove the
IP_MAX_MTUcheck from UDP/TCP encap. The IPv4/IPv6local_outfunction will do that ceck later.- The comment on IPv4 ESP offload with UDP encapsulation is true for IPv4 and IPv6, so remove the IPv4 from the comment.
Signed-off-by Steffen Klassert <[email protected]>Tested-by Tobias Brunner <[email protected]>--- include/net/esp.h | 6 + include/net/xfrm.h | 4 + net/ipv4/esp4.c | 1065 ++------------------------------------ net/ipv6/esp6.c | 1081 ++------------------------------------- net/ipv6/esp6_offload.c | 6 +- net/xfrm/Makefile | 1 + net/xfrm/xfrm_esp.c | 1017 ++++++++++++++++++++++++++++++++++++ 7 files changed, 1132 insertions(+), 2048 deletions(-) create mode 100644 net/xfrm/xfrm_esp.c
Quoted 10 lines
include/net/esp.h int esp_input_done2(struct sk_buff *skb, int err); · line 49
Quoted 8 lines
include/net/esp.h +0
-0@@ -47,4 +47,10 @@ int esp_input_done2(struct sk_buff *skb, int err);int esp6_output_head (struct xfrm_state *x,struct sk_buff *skb,struct esp_info *esp);int esp6_output_tail (struct xfrm_state *x,struct sk_buff *skb,struct esp_info *esp);int esp6_input_done2 (struct sk_buff *skb,int err);Those 3 are gone now and can be removed.
Done.
Quoted 14 lines
Quoted 14 lines
include/net/esp.h +6
-0+int esp_init_aead (struct xfrm_state *x,struct netlink_ext_ack *extack); +int esp_init_authenc (struct xfrm_state *x,struct netlink_ext_ack *extack); +void esp_destroy (struct xfrm_state *x); +int esp_input (struct xfrm_state *x,struct sk_buff *skb); +int esp_output (struct xfrm_state *x,struct sk_buff *skb); +#endif include/net/xfrm.h +0
-0@@ -455,7 +455,11 @@ struct xfrm_type { struct netlink_ext_ack *extack);void (*destructor)(struct xfrm_state *);
Quoted 8 lines
include/net/xfrm.h struct xfrm_type { · line 462
Quoted 6 lines
include/net/xfrm.h +4
-0int (*input)(struct xfrm_state *,struct sk_buff *skb); +int (*input_encap)(struct sk_buff *skb,struct xfrm_state *x);int (*output)(struct xfrm_state *,struct sk_buff *pskb); +struct sock *(*find_tcp_sk)(struct xfrm_state *x); +void (*output_encap_csum)(struct sk_buff *skb); +int (*output_tcp_encap)(struct xfrm_state *x,struct sk_buff *skb);This CB is unused.
Removed.
Quoted 26 lines
Quoted 4 lines
net/ipv4/esp4.c +0
-0[...]
Quoted 5 lines
net/ipv4/esp4.c +3
-0+static int esp4_input_encap (struct sk_buff *skb,struct xfrm_state *x) { +const struct iphdr *iph =ip_hdr (skb); +int ihl = iph->ihl *4 ;struct xfrm_encap_tmpl *encap = x->encap;[...]
net/ipv4/esp4.c static struct sock *esp_find_tcp_sk(struct xfrm_state *x) · line 61
Quoted 10 lines
net/ipv4/esp4.c +7
-1+struct tcphdr *th = (void *)(skb_network_header (skb) + ihl); +struct udphdr *uh = (void *)(skb_network_header (skb) + ihl); +int ret =0 ; +__be16 source; +switch (x->encap->encap_type) {case TCP_ENCAP_ESPINTCP : - esph =esp_output_tcp_encap (x, skb, esp); + source = th->source; + ret -=sizeof (struct tcphdr );I'm trying to check how all those offset combine, isn't that susceptible to the same problem as
17175d1a27c6("xfrm: esp6: fix encapsulation header offset computation")?
Quoted 2 lines
Also, since those new ->input_encap should return a negative value on success, the "return -1" for error is a bit confusing.
Yes, that was to align with IPv6, ipv6_skip_exthdr() returns a negative value. I did not expect this and got a crash on my first try.
Quoted 7 lines
Maybe:
- make them return +1 on error, without going through "goto out" (return -1 on error should be fine since that's not an expected offset)
- make the final return (the one that returns the negative offset) have aDEBUG_NET_WARN_ON_ONCE(ret >= 0)
Quoted 7 lines
Or if my math is correct, the caller's
hdr_lenafter adding ->input_encap's return value is the full length of the ipv* header including options/extensions, so let ->input_encap return that directly instead of doing the "hdr_len+= ret" dance (and keep the return -1 on error)? That would make things a bit clearer than "skb_network_header_len+skb_network_offset+ [whateveripv6_skip_exthdrreturns]".
I'll try to make this a bit clearer in the next version.
Quoted 17 lines
[...]
net/ipv4/esp4.c static int esp4_rcv_cb(struct sk_buff *skb, int err) · line 198
Quoted 13 lines
net/ipv4/esp4.c +2
-7@@ -1164,13 +194,16 @@ static int esp4_rcv_cb(struct sk_buff *skb, int err)static const struct xfrm_type esp_type = { - .owner =THIS_MODULE , - .proto =IPPROTO_ESP , - .flags =XFRM_TYPE_REPLAY_PROT , - .init_state = esp_init_state, - .destructor = esp_destroy, - .input = esp_input, - .output = esp_output, + .owner =THIS_MODULE , + .proto =IPPROTO_ESP ,nit: since you're reworking the whitespace, this is:
Quoted 3 lines
\t.proto\t \t\t=
IPPROTO_ESP,(with a few spaces in the middle)
Fixed.
Quoted 20 lines
[...]
Quoted 4 lines
net/ipv6/esp6.c +0
-0[...]
Quoted 2 lines
net/ipv6/esp6.c +0
-2-int esp6_input_done2 (struct sk_buff *skb,int err) -{[...]
Quoted 11 lines
net/ipv6/esp6.c +0
-9-/* -* 2) ignore UDP/TCP checksums in case -* of NAT-T in Transport Mode, or -* perform other post-processing fixes -* as per draft-ietf-ipsec-udp-encaps-06, -* section 3.1.2 -*/ -if (x->props.mode ==XFRM_MODE_TRANSPORT ) - skb->ip_summed =CHECKSUM_UNNECESSARY ; }
Quoted 7 lines
net/ipv6/esp6.c static void esp_output_encap_csum(struct sk_buff *skb) · line 821
Quoted 2 lines
net/ipv6/esp6.c +0
-2-skb_postpull_rcsum (skb,skb_network_header (skb), -skb_network_header_len (skb));This doesn't get carried over to the new common function
esp_input_done2. It originally came from commita9b28c2bf05d("esp6:
Fix RX checksum after header pull"), not sure if it's still needed but I guess so?
I think this skb_postpull_rcsum() is misplaced here, and the skb_pull_rcsum() call below as well. esp_input and esp6_input do:
skb->ip_summed = CHECKSUM_NONE;
So both function do nothing because the checksum is recomputed anyway. The only case where we need this is HW offloding because this does not call esp_input/esp6_input. So both function calls skb_postpull_rcsum() and skb_pull_rcsum() should go to esp6_input_tail, and maybe even to esp_input_tail. Looks like esp4 has the same problem, but we do it just for esp6.
I'll move these calls in the next version.
Quoted 32 lines
Quoted 15 lines
net/ipv6/esp6.c +0
-15-skb_pull_rcsum (skb, hlen); -if (x->props.mode ==XFRM_MODE_TUNNEL || - x->props.mode ==XFRM_MODE_IPTFS ) -skb_reset_transport_header (skb); -else -skb_set_transport_header (skb, -hdr_len); - -/* RFC4303: Drop dummy packets without any error */ -if (err ==IPPROTO_NONE ) - err = -EINVAL ; - -out: -return err; -} -EXPORT_SYMBOL_GPL (esp6_input_done2);[...]
Quoted 5 lines
net/xfrm/xfrm_esp.c new file +0
-0[...]
net/xfrm/xfrm_esp.c line 311
Quoted 6 lines
net/xfrm/xfrm_esp.c new file +6
-0+ + +#ifdef CONFIG_INET_ESPINTCP +static struct ip_esp_hdr *esp_output_tcp_encap (struct xfrm_state *x, +struct sk_buff *skb, +struct esp_info *esp)nit: alignment
Quoted 2 lines
(checkpatch complains about this and a few other whitespace things in this new file, probably all carried over from the existing code)
Fixed.
Quoted 8 lines
[...]
net/xfrm/xfrm_esp.c line 1017
Quoted 5 lines
net/xfrm/xfrm_esp.c new file +5
-0+EXPORT_SYMBOL_GPL (esp_init_authenc); + +MODULE_LICENSE ("GPL" ); +MODULE_DESCRIPTION ("Generic ESP" ); +nit: git complains about the empty line
Fixed.
Thank a lot for your review Sabrina!