Skip to contentSkip to conversation

esp: Consolidate esp4 and esp6

PATCH In review Steffen Klassert 3 participants 5 messages May 6 – May 10

Subsystems (3)
lore
Share & tools
lore

Steffen Klassert

author maintainer
#0 lore raw

PATCH v2 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 message.

- 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.

Changes since v1:

- Remove some now unused code.

- Whitespace fixes.

- Cleanup the header length calculation in the input path for UDP/TCP encapsulation.

- Move the skb_pull_rcsum() call to the generic esp_input_done2() function.

- Move the skb_postpull_rcsum() call to the offloading codepath, it is needed only there.

---
 include/net/esp.h       |    9 +-
 include/net/xfrm.h      |    3 +
 net/ipv4/esp4.c         | 1068 ++------------------------------------
 net/ipv4/esp4_offload.c |    3 +-
 net/ipv6/esp6.c         | 1086 ++-------------------------------------
 net/ipv6/esp6_offload.c |   12 +-
 net/xfrm/Makefile       |    1 +
 net/xfrm/xfrm_esp.c     | 1012 ++++++++++++++++++++++++++++++++++++
 8 files changed, 1135 insertions(+), 2059 deletions(-)
 create mode 100644 net/xfrm/xfrm_esp.c
include/net/esp.h +6 -3
index 322950727dd0..e1ae485ac1c7 100644
@@ -44,7 +44,10 @@ struct esp_info {
 int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp);
 int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp);
 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 +3 -0
index 10d3edde6b2f..d99b6f57ecda 100644
@@ -455,7 +455,10 @@ 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			(*reject)(struct xfrm_state *, struct sk_buff *,
 					  const struct flowi *);
 };
net/ipv4/esp4.c +49 -1019

1,121 lines not on this page · raw message

net/ipv4/esp4_offload.c +2 -1
index abd77162f5e7..293623dacdd4 100644
@@ -252,8 +252,9 @@ static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb)
 {
 	struct crypto_aead *aead = x->data;
 	struct xfrm_offload *xo = xfrm_offload(skb);
+	int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
 
-	if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
+	if (!pskb_may_pull(skb, hlen))
 		return -EINVAL;
 
 	if (!(xo->flags & CRYPTO_DONE))
net/ipv6/esp6.c +54 -1032

1,147 lines not on this page · raw message

net/ipv6/esp6_offload.c +8 -4
index 22895521a57d..b1c38d7d286e 100644
@@ -290,14 +290,18 @@ static int esp6_input_tail(struct xfrm_state *x, struct sk_buff *skb)
 {
 	struct crypto_aead *aead = x->data;
 	struct xfrm_offload *xo = xfrm_offload(skb);
+	int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
 
-	if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
+	if (!pskb_may_pull(skb, hlen))
 		return -EINVAL;
 
 	if (!(xo->flags & CRYPTO_DONE))
 		skb->ip_summed = CHECKSUM_NONE;
+	else
+		skb_postpull_rcsum(skb, skb_network_header(skb),
+				   skb_network_header_len(skb));
 
-	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 +344,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 +388,7 @@ static int esp6_xmit(struct xfrm_state *x, struct sk_buff *skb,  netdev_features
 		return 0;
 	}
 
-	err = esp6_output_tail(x, skb, &esp);
+	err = esp_output_tail(x, skb, &esp);
 	if (err)
 		return err;
 
net/xfrm/Makefile +1 -0
index 5a1787587cb3..2a8995a34bdd 100644
@@ -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 +1012 -0

659 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.

reviewer
#1 lore raw

From: 'Simon Horman' <[email protected]>

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev
---
Now I have enough to produce the report.

commit 64848fd4fb36bc803f3ebb41951e5c02a19f0280
Author: Steffen Klassert <[email protected]>

esp: Consolidate esp4 and esp6

This patch merges common code of esp4.c and esp6.c into a new xfrm_esp.c. ESP output/input, AEAD setup and the TCP/UDP encapsulation helpers are shared between IPv4 and IPv6, with three family-specific hooks (input_encap, find_tcp_sk, output_encap_csum) added to struct xfrm_type.

Quoted 13 lines
net/ipv6/esp6_offload.c +2 -1
index 22895521a57d..b1c38d7d286e 100644
@@ -290,14 +290,18 @@ static int esp6_input_tail(struct xfrm_state *x, struct sk_buff *skb)
 {
 	struct crypto_aead *aead = x->data;
 	struct xfrm_offload *xo = xfrm_offload(skb);
+	int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
 
-	if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
+	if (!pskb_may_pull(skb, hlen))
 		return -EINVAL;

net/ipv6/esp6_offload.c static int esp6_input_tail(struct xfrm_state *x, struct sk_buff *skb) · line 305

Quoted 10 lines
net/ipv6/esp6_offload.c +4 -1
 
 	if (!(xo->flags & CRYPTO_DONE))
 		skb->ip_summed = CHECKSUM_NONE;
+	else
+		skb_postpull_rcsum(skb, skb_network_header(skb),
+				   skb_network_header_len(skb));
 
-	return esp6_input_done2(skb, 0);
+	return esp_input_done2(skb, 0);
 }

Has the non-offload IPv6 ESP input path lost its skb_postpull_rcsum() for the IPv6 header?

Before this patch, esp6_input_done2() did:

    skb_postpull_rcsum(skb, skb_network_header(skb),
                       skb_network_header_len(skb));
    skb_pull_rcsum(skb, hlen);

and that call was introduced by commit a9b28c2bf05d ("esp6: Fix RX checksum after header pull") to compensate for ip6_input_finish() stripping the IPv6 header without adjusting skb->csum, fixing "hw csum failure" warnings on devices delivering CHECKSUM_COMPLETE.

The new esp_input_done2() in net/xfrm/xfrm_esp.c only does:

    hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
    skb_pull_rcsum(skb, hlen);

and the postpull is reinstated only in esp6_input_tail() under the "xo->flags & CRYPTO_DONE" branch. Non-offload software-decrypt IPv6 ESP RX therefore no longer compensates skb->csum for the outer IPv6 header bytes. Is this an intentional change, and if so what replaces the fix from a9b28c2bf05d?

The commit message says "Move the skb_postpull_rcsum() call to the offloading codepath, it is needed only there", which seems to contradict the stated reason for a9b28c2bf05d. Is there a later commit or code change that makes it unnecessary in the non-offload path?

Quoted 5 lines
net/xfrm/xfrm_esp.c new file +0 -0
new file mode 100644
index 000000000000..4f75e1ace3bb

[ ... ]

Quoted 9 lines
net/xfrm/xfrm_esp.c new file +9 -0
+#ifdef CONFIG_INET_ESPINTCP
+static int esp_output_tcp_finish(struct xfrm_state *x, struct sk_buff *skb)
+{
+	struct sock *sk;
+	int err;
+
+	rcu_read_lock();
+
+	sk = x->type->find_tcp_sk(x);

[ ... ]

Quoted 7 lines
net/xfrm/xfrm_esp.c new file +7 -0
+static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb)
+{
+	int err;
+
+	local_bh_disable();
+	err = xfrm_trans_queue_net(xs_net(x), skb, esp_output_tcp_encap_cb);
+	local_bh_enable();

[ ... ]

net/xfrm/xfrm_esp.c line 178

Quoted 8 lines
net/xfrm/xfrm_esp.c new file +8 -0
+}
+#else
+static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb)
+{
+	WARN_ON(1);
+	return -EOPNOTSUPP;
+}
+#endif

Is CONFIG_INET_ESPINTCP the right guard for this shared file?

This new xfrm_esp.c is compiled for both IPv4 and IPv6 ESP, but the TCP-encap helpers are wrapped with #ifdef CONFIG_INET_ESPINTCP, which is the IPv4-only Kconfig symbol. The IPv6 counterpart is CONFIG_INET6_ESPINTCP, and both symbols "select XFRM_ESPINTCP" as the common hidden symbol (see net/xfrm/Kconfig and net/xfrm/Makefile, which uses obj-$(CONFIG_XFRM_ESPINTCP) += espintcp.o).

For a kernel configured with CONFIG_INET_ESPINTCP=n and CONFIG_INET6_ESPINTCP=y, the full esp_output_tcp_encap() / esp_output_tail_tcp() are not compiled in, so the stub returning -EOPNOTSUPP with WARN_ON(1) is selected, and any IPv6 ESP-in-TCP output call ending up in esp_output_head() -> esp_output_encap() -> esp_output_tcp_encap() will fail. The same #ifdef also appears further down around esp_output_tcp_encap().

Should these two blocks be guarded by CONFIG_XFRM_ESPINTCP instead?

Quoted 7 lines
net/xfrm/xfrm_esp.c new file +7 -0
+static struct ip_esp_hdr *esp_output_udp_encap(struct sk_buff *skb,
+					       int encap_type,
+					       struct esp_info *esp,
+					       __be16 sport,
+					       __be16 dport)
+{
+	struct udphdr *uh;

net/xfrm/xfrm_esp.c line 297

Quoted 10 lines
net/xfrm/xfrm_esp.c new file +10 -0
+	unsigned int len;
+	struct xfrm_offload *xo = xfrm_offload(skb);
+
+	len = skb->len + esp->tailen - skb_transport_offset(skb);
+
+	uh = (struct udphdr *)esp->esph;
+	uh->source = sport;
+	uh->dest = dport;
+	uh->len = htons(len);
+	uh->check = 0;

Can this silently truncate uh->len?

The old esp6_output_udp_encap() explicitly rejected len > U16_MAX with -EMSGSIZE before writing htons(len) into the 16-bit UDP length field, and the old IPv4 esp_output_udp_encap() rejected len + sizeof(iphdr)

Quoted 1 line

IP_MAX_MTU. Both bounds are gone from this consolidated helper.

The commit message says:

"Remove the IP_MAX_MTU check from UDP/TCP encap.
The IPv4/IPv6 local_out function will do that ceck later."

local_out checks the outer IP packet length, not the UDP 16-bit length field. In the software-fallback branch of esp6_xmit() (the !hw_offload path calling esp_output_head()), skb->len of a GSO skb can exceed 65535 pre-segmentation, so the htons() can truncate uh->len without any earlier check rejecting the packet.

Should the >U16_MAX check be retained in esp_output_udp_encap(), at least for IPv6?

Quoted 12 lines
net/xfrm/xfrm_esp.c new file +12 -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)
+{
+	__be16 *lenp = (void *)esp->esph;
+	struct ip_esp_hdr *esph;
+	unsigned int len;
+	struct sock *sk;
+
+	len = skb->len + esp->tailen - skb_transport_offset(skb);
+

net/xfrm/xfrm_esp.c line 330

Quoted 10 lines
net/xfrm/xfrm_esp.c new file +10 -0
+	rcu_read_lock();
+	sk = x->type->find_tcp_sk(x);
+	rcu_read_unlock();
+
+	if (IS_ERR(sk))
+		return ERR_CAST(sk);
+
+	sock_put(sk);
+
+	*lenp = htons(len);

Can *lenp be truncated here?

The previous IPv4 and IPv6 versions both had "if (len > IP_MAX_MTU) return ERR_PTR(-EMSGSIZE)" before writing the 2-byte espintcp length prefix. espintcp frames ride over TCP and are not bounded by any IP MTU that local_out will later enforce on the outer header, so the local_out justification in the commit message does not seem to cover this case. Should the len > 65535 check be preserved here?

author maintainer
#2 lore raw
Quoted 5 lines · Simon Horman

From: 'Simon Horman' <[email protected]>

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev

I've seen it already, a v3 is already under test.

Thanks!

reviewer
#3 lore raw
In reply to Steffen Klassert

From: 'Simon Horman' <[email protected]>

This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible. Full review at: https://sashiko.dev
---
esp: Consolidate esp4 and esp6

This commit merges the common code from esp4.c and esp6.c into xfrm_esp.c to reduce duplication. It adapts the input and output encapsulation paths to use indirect function calls.

Quoted 4 lines
net/ipv6/esp6.c +0 -0
index 9f75313734f8c..9d6e030510860 100644

[ ... ]

Quoted 10 lines
net/ipv6/esp6.c +10 -0
+static int esp6_input_encap(struct sk_buff *skb, struct xfrm_state *x)
+{
+	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
+	int offset = skb_network_offset(skb) + sizeof(*ip6h);
+	struct xfrm_encap_tmpl *encap = x->encap;
+	int hdr_len = skb_network_header_len(skb);
+	u8 nexthdr = ip6h->nexthdr;
+	__be16 frag_off, source;
+	struct udphdr *uh;
+	struct tcphdr *th;

net/ipv6/esp6.c static void esp_output_encap_csum(struct sk_buff *skb) · line 94

Quoted 10 lines
net/ipv6/esp6.c +7 -0
+	int ret;
 
+	ret = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
+	if (ret == -1)
+		return ret;
 
+	ret += hdr_len;
 
+	uh = (void *)(skb->data + ret);
+	th = (void *)(skb->data + ret);

If hdr_len is added to ret before computing the uh and th pointers, does this cause the pointers to overshoot the transport header by hdr_len bytes?

If uh->source reads from inside the decrypted ESP payload instead of the actual UDP header, it seems this would almost always mismatch encap->encap_sport. Could this lead to the kernel unconditionally calling km_new_mapping() on every received packet, breaking IPv6 NAT-T?

Quoted 5 lines
net/xfrm/xfrm_esp.c new file +0 -0
new file mode 100644
index 0000000000000..4f75e1ace3bbc

[ ... ]

Quoted 3 lines
net/xfrm/xfrm_esp.c new file +3 -0
+#ifdef CONFIG_INET_ESPINTCP
+static int esp_output_tcp_finish(struct xfrm_state *x, struct sk_buff *skb)
+{

[ ... ]

Quoted 7 lines
net/xfrm/xfrm_esp.c new file +7 -0
+#else
+static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb)
+{
+	WARN_ON(1);
+	return -EOPNOTSUPP;
+}
+#endif

[ ... ]

Quoted 4 lines
net/xfrm/xfrm_esp.c new file +4 -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)

[ ... ]

net/xfrm/xfrm_esp.c line 342

Quoted 8 lines
net/xfrm/xfrm_esp.c new file +8 -0
+#else
+static struct ip_esp_hdr *esp_output_tcp_encap(struct xfrm_state *x,
+					       struct sk_buff *skb,
+					       struct esp_info *esp)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+#endif

Since the TCP encapsulation methods are guarded exclusively by CONFIG_INET_ESPINTCP, does this break IPv6 ESP-in-TCP if CONFIG_INET6_ESPINTCP is enabled but the IPv4 equivalent is disabled?

If CONFIG_INET_ESPINTCP is not enabled, these functions fall back to returning -EOPNOTSUPP. Should these preprocessor guards check for both CONFIG_INET_ESPINTCP and CONFIG_INET6_ESPINTCP?

#4 lore raw
In reply to Steffen Klassert
Quoted 4 lines · On Wed, 6 May 2026 10:15:06 +0200 Steffen Klassert…

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.

Merging into a new file make it very difficult to see the changes (both now and in the future).

It might be better to merge the changes into either esp4.c or esp6.c and then rename the file afterwards.

Since there are only two possibilities (ipv4 and ipv6) conditional calls would be better than indirect ones.
But it is pretty impossible to see where that happens in the changes.

-- David