Skip to contentSkip to conversation

esp: Consolidate esp4 and esp6.

RFC PATCH In review Steffen Klassert 5 participants 6 messages Oct 22, 2025 – Jan 12

Subsystems (3)
lore
Share & tools
lore

Steffen Klassert

author maintainer
#0 lore raw

RFC PATCH 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 prize of three indirect calls on UDP/TCP encapsulation. No functional changes.

---
 include/net/esp.h       |    6 +
 include/net/xfrm.h      |    4 +
 net/ipv4/esp4.c         | 1067 ++------------------------------------
 net/ipv6/esp6.c         | 1093 +++------------------------------------
 net/ipv6/esp6_offload.c |    6 +-
 net/xfrm/Makefile       |    1 +
 net/xfrm/xfrm_esp.c     | 1025 ++++++++++++++++++++++++++++++++++++
 7 files changed, 1156 insertions(+), 2046 deletions(-)
 create mode 100644 net/xfrm/xfrm_esp.c
include/net/esp.h +6 -0
index 322950727dd0..5761c762c69a 100644
@@ -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
index f3014e4f54fc..9acaa1787e8f 100644
@@ -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 +53 -1014

1,119 lines not on this page · raw message

net/ipv6/esp6.c +64 -1029

1,155 lines not on this page · raw message

net/ipv6/esp6_offload.c +3 -3
index 7b41fb4f00b5..75c12ab221e0 100644
@@ -295,7 +295,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)
@@ -338,7 +338,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;
 	}
@@ -382,7 +382,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 +1025 -0

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

#1 lore raw
kernel test robot noticed the following build errors:

Hi Steffen,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:

[auto build test ERROR on klassert-ipsec-next/master]
[also build test ERROR on klassert-ipsec/master linus/master v6.18-rc2 next-20251022] [cannot apply to horms-ipvs/master]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Steffen-Klassert/esp-Consolidate-esp4-and-esp6/20251022-140856
base:   https://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec-next.git master
patch link:    https://lore.kernel.org/r/aPhzm0lzMXGSpf22%40secunet.com
patch subject: [PATCH RFC ipsec-next] esp: Consolidate esp4 and esp6.
config: i386-buildonly-randconfig-004-20251023 (https://download.01.org/0day-ci/archive/20251023/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251023/[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 error/warnings (new ones prefixed by >>):

>> net/ipv6/esp6.c:53:9: error: incompatible integer to pointer conversion returning 'int' from a function with result type 'struct sock *' [-Wint-conversion]
      53 |         return -EOPNOTSUPP;
         |                ^~~~~~~~~~~
>> net/ipv6/esp6.c:78:6: warning: variable 'hdr_len' set but not used [-Wunused-but-set-variable]
      78 |         int hdr_len = skb_network_header_len(skb);
         |             ^

1 warning and 1 error generated.

vim +53 net/ipv6/esp6.c

26333c37fc285e Sabrina Dubroca  2020-04-27   48  
26333c37fc285e Sabrina Dubroca  2020-04-27   49  #else
110277bee31750 Steffen Klassert 2025-10-22   50  static struct sock *esp6_find_tcp_sk(struct xfrm_state *x)
26333c37fc285e Sabrina Dubroca  2020-04-27   51  {
96f887a612e4cd Hagar Hemdan     2024-05-18   52  	WARN_ON(1);
26333c37fc285e Sabrina Dubroca  2020-04-27  @53  	return -EOPNOTSUPP;
26333c37fc285e Sabrina Dubroca  2020-04-27   54  }
26333c37fc285e Sabrina Dubroca  2020-04-27   55  #endif
26333c37fc285e Sabrina Dubroca  2020-04-27   56  
110277bee31750 Steffen Klassert 2025-10-22   57  static void esp6_output_encap_csum(struct sk_buff *skb)
0146dca70b877b Sabrina Dubroca  2020-04-27   58  {
0146dca70b877b Sabrina Dubroca  2020-04-27   59  	/* UDP encap with IPv6 requires a valid checksum */
0146dca70b877b Sabrina Dubroca  2020-04-27   60  	if (*skb_mac_header(skb) == IPPROTO_UDP) {
0146dca70b877b Sabrina Dubroca  2020-04-27   61  		struct udphdr *uh = udp_hdr(skb);
0146dca70b877b Sabrina Dubroca  2020-04-27   62  		struct ipv6hdr *ip6h = ipv6_hdr(skb);
0146dca70b877b Sabrina Dubroca  2020-04-27   63  		int len = ntohs(uh->len);
0146dca70b877b Sabrina Dubroca  2020-04-27   64  		unsigned int offset = skb_transport_offset(skb);
0146dca70b877b Sabrina Dubroca  2020-04-27   65  		__wsum csum = skb_checksum(skb, offset, skb->len - offset, 0);
0146dca70b877b Sabrina Dubroca  2020-04-27   66  
0146dca70b877b Sabrina Dubroca  2020-04-27   67  		uh->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
0146dca70b877b Sabrina Dubroca  2020-04-27   68  					    len, IPPROTO_UDP, csum);
0146dca70b877b Sabrina Dubroca  2020-04-27   69  		if (uh->check == 0)
0146dca70b877b Sabrina Dubroca  2020-04-27   70  			uh->check = CSUM_MANGLED_0;
0146dca70b877b Sabrina Dubroca  2020-04-27   71  	}
0146dca70b877b Sabrina Dubroca  2020-04-27   72  }
0146dca70b877b Sabrina Dubroca  2020-04-27   73  
110277bee31750 Steffen Klassert 2025-10-22   74  static int esp6_input_encap(struct sk_buff *skb, struct xfrm_state *x)
38320c70d282be Herbert Xu       2008-01-28   75  {
0146dca70b877b Sabrina Dubroca  2020-04-27   76  	const struct ipv6hdr *ip6h = ipv6_hdr(skb);
17175d1a27c618 Sabrina Dubroca  2020-07-03   77  	int offset = skb_network_offset(skb) + sizeof(*ip6h);
110277bee31750 Steffen Klassert 2025-10-22  @78  	int hdr_len = skb_network_header_len(skb);
0146dca70b877b Sabrina Dubroca  2020-04-27   79  	struct xfrm_encap_tmpl *encap = x->encap;
17175d1a27c618 Sabrina Dubroca  2020-07-03   80  	u8 nexthdr = ip6h->nexthdr;
17175d1a27c618 Sabrina Dubroca  2020-07-03   81  	__be16 frag_off, source;
17175d1a27c618 Sabrina Dubroca  2020-07-03   82  	struct udphdr *uh;
17175d1a27c618 Sabrina Dubroca  2020-07-03   83  	struct tcphdr *th;
110277bee31750 Steffen Klassert 2025-10-22   84  	int err = 0;
17175d1a27c618 Sabrina Dubroca  2020-07-03   85  
17175d1a27c618 Sabrina Dubroca  2020-07-03   86  	offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
4db4075f92af2b Sabrina Dubroca  2022-03-10   87  	if (offset == -1) {
5f9c55c8066bcd Jordy Zomer      2021-11-17   88  		err = -EINVAL;
5f9c55c8066bcd Jordy Zomer      2021-11-17   89  		goto out;
5f9c55c8066bcd Jordy Zomer      2021-11-17   90  	}
5f9c55c8066bcd Jordy Zomer      2021-11-17   91  
17175d1a27c618 Sabrina Dubroca  2020-07-03   92  	uh = (void *)(skb->data + offset);
17175d1a27c618 Sabrina Dubroca  2020-07-03   93  	th = (void *)(skb->data + offset);
d5dba1376e2baf Sabrina Dubroca  2020-07-27   94  	hdr_len += offset;
0146dca70b877b Sabrina Dubroca  2020-04-27   95  
0146dca70b877b Sabrina Dubroca  2020-04-27   96  	switch (x->encap->encap_type) {
26333c37fc285e Sabrina Dubroca  2020-04-27   97  	case TCP_ENCAP_ESPINTCP:
26333c37fc285e Sabrina Dubroca  2020-04-27   98  		source = th->source;
26333c37fc285e Sabrina Dubroca  2020-04-27   99  		break;
0146dca70b877b Sabrina Dubroca  2020-04-27  100  	case UDP_ENCAP_ESPINUDP:
0146dca70b877b Sabrina Dubroca  2020-04-27  101  		source = uh->source;
0146dca70b877b Sabrina Dubroca  2020-04-27  102  		break;
0146dca70b877b Sabrina Dubroca  2020-04-27  103  	default:
0146dca70b877b Sabrina Dubroca  2020-04-27  104  		WARN_ON_ONCE(1);
0146dca70b877b Sabrina Dubroca  2020-04-27  105  		err = -EINVAL;
0146dca70b877b Sabrina Dubroca  2020-04-27  106  		goto out;
0146dca70b877b Sabrina Dubroca  2020-04-27  107  	}
0146dca70b877b Sabrina Dubroca  2020-04-27  108  
0146dca70b877b Sabrina Dubroca  2020-04-27  109  	/*
0146dca70b877b Sabrina Dubroca  2020-04-27  110  	 * 1) if the NAT-T peer's IP or port changed then
1711435e3e67e0 Deming Wang      2023-10-25  111  	 *    advertise the change to the keying daemon.
0146dca70b877b Sabrina Dubroca  2020-04-27  112  	 *    This is an inbound SA, so just compare
0146dca70b877b Sabrina Dubroca  2020-04-27  113  	 *    SRC ports.
0146dca70b877b Sabrina Dubroca  2020-04-27  114  	 */
0146dca70b877b Sabrina Dubroca  2020-04-27  115  	if (!ipv6_addr_equal(&ip6h->saddr, &x->props.saddr.in6) ||
0146dca70b877b Sabrina Dubroca  2020-04-27  116  	    source != encap->encap_sport) {
0146dca70b877b Sabrina Dubroca  2020-04-27  117  		xfrm_address_t ipaddr;
0146dca70b877b Sabrina Dubroca  2020-04-27  118  
0146dca70b877b Sabrina Dubroca  2020-04-27  119  		memcpy(&ipaddr.a6, &ip6h->saddr.s6_addr, sizeof(ipaddr.a6));
0146dca70b877b Sabrina Dubroca  2020-04-27  120  		km_new_mapping(x, &ipaddr, source);
0146dca70b877b Sabrina Dubroca  2020-04-27  121  
0146dca70b877b Sabrina Dubroca  2020-04-27  122  		/* XXX: perhaps add an extra
0146dca70b877b Sabrina Dubroca  2020-04-27  123  		 * policy check here, to see
0146dca70b877b Sabrina Dubroca  2020-04-27  124  		 * if we should allow or
0146dca70b877b Sabrina Dubroca  2020-04-27  125  		 * reject a packet from a
0146dca70b877b Sabrina Dubroca  2020-04-27  126  		 * different source
0146dca70b877b Sabrina Dubroca  2020-04-27  127  		 * address/port.
0146dca70b877b Sabrina Dubroca  2020-04-27  128  		 */
0146dca70b877b Sabrina Dubroca  2020-04-27  129  	}
0146dca70b877b Sabrina Dubroca  2020-04-27  130  
0146dca70b877b Sabrina Dubroca  2020-04-27  131  	/*
0146dca70b877b Sabrina Dubroca  2020-04-27  132  	 * 2) ignore UDP/TCP checksums in case
0146dca70b877b Sabrina Dubroca  2020-04-27  133  	 *    of NAT-T in Transport Mode, or
0146dca70b877b Sabrina Dubroca  2020-04-27  134  	 *    perform other post-processing fixes
0146dca70b877b Sabrina Dubroca  2020-04-27  135  	 *    as per draft-ietf-ipsec-udp-encaps-06,
0146dca70b877b Sabrina Dubroca  2020-04-27  136  	 *    section 3.1.2
0146dca70b877b Sabrina Dubroca  2020-04-27  137  	 */
0146dca70b877b Sabrina Dubroca  2020-04-27  138  	if (x->props.mode == XFRM_MODE_TRANSPORT)
0146dca70b877b Sabrina Dubroca  2020-04-27  139  		skb->ip_summed = CHECKSUM_UNNECESSARY;
38320c70d282be Herbert Xu       2008-01-28  140  
38320c70d282be Herbert Xu       2008-01-28  141  out:
^1da177e4c3f41 Linus Torvalds   2005-04-16  142  	return err;
^1da177e4c3f41 Linus Torvalds   2005-04-16  143  }
^1da177e4c3f41 Linus Torvalds   2005-04-16  144  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
reviewer
#2 lore raw
In reply to Steffen Klassert
Quoted 3 lines · 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 prize of three indirect calls

nit: prize -> price

Quoted 4 lines

on UDP/TCP encapsulation. No functional changes.

Quoted 9 lines
---
 include/net/esp.h       |    6 +
 include/net/xfrm.h      |    4 +
 net/ipv4/esp4.c         | 1067 ++------------------------------------
 net/ipv6/esp6.c         | 1093 +++------------------------------------
 net/ipv6/esp6_offload.c |    6 +-
 net/xfrm/Makefile       |    1 +
 net/xfrm/xfrm_esp.c     | 1025 ++++++++++++++++++++++++++++++++++++
 7 files changed, 1156 insertions(+), 2046 deletions(-)

Less is more :)

...

Quoted 1 line
net/ipv6/esp6.c +0 -0

...

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

Quoted 5 lines
net/ipv6/esp6.c +4 -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);
+	int hdr_len = skb_network_header_len(skb);

nit: hrd_len is set here and incremented below,
but otherwise unused in this function.

...

#3 lore raw
In reply to Steffen Klassert
Quoted 4 lines · 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 prize of three indirect calls on UDP/TCP encapsulation.

If that turns out to be a problem, maybe we can figure out a way to use the INDIRECT_CALL* helpers here as well (but currently they don't work for modules, I played with that a while back and could look into it again).

Quoted 12 lines
---
 include/net/esp.h       |    6 +
 include/net/xfrm.h      |    4 +
 net/ipv4/esp4.c         | 1067 ++------------------------------------
 net/ipv6/esp6.c         | 1093 +++------------------------------------
 net/ipv6/esp6_offload.c |    6 +-
 net/xfrm/Makefile       |    1 +
 net/xfrm/xfrm_esp.c     | 1025 ++++++++++++++++++++++++++++++++++++
 7 files changed, 1156 insertions(+), 2046 deletions(-)

nice!

Quoted 2 lines
net/ipv4/esp4.c +0 -2
-int esp_input_done2(struct sk_buff *skb, int err)
-{

[...]

Quoted 4 lines
net/ipv4/esp4.c +4 -0
+	if (iph->saddr != x->props.saddr.a4 ||
+	    source != encap->encap_sport) {
+		xfrm_address_t ipaddr;
+

net/ipv4/esp4.c static struct sock *esp_find_tcp_sk(struct xfrm_state *x) · line 90

Quoted 10 lines
net/ipv4/esp4.c +9 -0
+		ipaddr.a4 = iph->saddr;
+		km_new_mapping(x, &ipaddr, source);
+
+		/* XXX: perhaps add an extra
+		 * policy check here, to see
+		 * if we should allow or
+		 * reject a packet from a
+		 * different source
+		 * address/port.
 		 */

Maybe we can get rid of those "XXX" comments? Unless you think the suggestion still makes sense. But the comments (here and in esp6_input_done2) have been here a long time and it doesn't seem to bother users.

[...]

Quoted 1 line

static int esp_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)

Looking at esp_init_state and esp6_init_state, they also have a lot in common (pretty much everything except some x->props.header_len adjustments) that could be extracted into the new file.

[...]

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

Quoted 5 lines
net/ipv6/esp6.c +4 -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);
+	int hdr_len = skb_network_header_len(skb);

As Simon mentioned, it's set/incremented but not used. The current code in esp6_input_done2 would then use that value in skb_set_transport_header, but now the common esp_input_done2 calls ->input_encap and doesn't get that increased value back. I think we'll need to have esp_input_done2 pass &hdr_len when it calls the ->input_encap to get the correct value and take into account ipv6 exthdrs.

[...]

Quoted 10 lines
net/xfrm/xfrm_esp.c new file +10 -0
+static void esp_output_done(void *data, int err)
+{
+	struct sk_buff *skb = data;
+	struct xfrm_offload *xo = xfrm_offload(skb);
+	void *tmp;
+	struct xfrm_state *x;
+
+	if (xo && (xo->flags & XFRM_DEV_RESUME)) {
+		struct sec_path *sp = skb_sec_path(skb);
+

net/xfrm/xfrm_esp.c line 199

Quoted 10 lines
net/xfrm/xfrm_esp.c new file +10 -0
+		x = sp->xvec[sp->len - 1];
+	} else {
+		x = skb_dst(skb)->xfrm;
+	}
+
+	tmp = ESP_SKB_CB(skb)->tmp;
+	esp_ssg_unref(x, tmp, skb);
+	kfree(tmp);
+
+	x->type->output_encap_csum(skb);

Since the ipv4 variant doesn't do anything, maybe

if (x->type->output_encap_csum)

	x->type->output_encap_csum(skb);

and don't set it in esp_type?

OTOH it's nice to have a consistent "all CBs are always defined" and just call them unconditionally.

net/xfrm/xfrm_esp.c line 289

Quoted 12 lines
net/xfrm/xfrm_esp.c new file +12 -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;
+	unsigned int len;
+	struct xfrm_offload *xo = xfrm_offload(skb);
+
+	len = skb->len + esp->tailen - skb_transport_offset(skb);
+	if (len + sizeof(struct iphdr) > IP_MAX_MTU)

This is now used by both IPv4 and IPv6, and esp6_output_udp_encap didn't have any adjustment in this test.

Quoted 4 lines
net/xfrm/xfrm_esp.c new file +4 -0
+		return ERR_PTR(-EMSGSIZE);
+
+	uh = (struct udphdr *)esp->esph;
+	uh->source = sport;

net/xfrm/xfrm_esp.c line 303

Quoted 10 lines
net/xfrm/xfrm_esp.c new file +10 -0
+	uh->dest = dport;
+	uh->len = htons(len);
+	uh->check = 0;
+
+	/* For IPv4 ESP with UDP encapsulation, if xo is not null, the skb is in the crypto offload
+	 * data path, which means that esp_output_udp_encap is called outside of the XFRM stack.
+	 * In this case, the mac header doesn't point to the IPv4 protocol field, so don't set it.
+	 */
+	if (!xo || encap_type != UDP_ENCAP_ESPINUDP)
+		*skb_mac_header(skb) = IPPROTO_UDP;

That was absent in esp6_output_udp_encap, commit 447bc4b1906f ("xfrm:
Support crypto offload for outbound IPv4 UDP-encapsulated ESP packet") only took care of IPv4. I'm not sure adding this to the IPv6 code without adapting the rest of 447bc4b1906f in the esp6_offload code is correct.

Quoted 3 lines
net/xfrm/xfrm_esp.c new file +3 -0
+
+	return (struct ip_esp_hdr *)(uh + 1);
+}

[...]

Quoted 1 line

+ int esp_output(struct xfrm_state *x, struct sk_buff *skb)

nit: ' ' at the start of the line

Signature · 1 line
Sabrina
#4 lore raw
Quoted 4 lines · Sabrina Dubroca via Devel
Quoted 4 lines
net/ipv4/esp4.c +4 -0
+		/* XXX: perhaps add an extra
+		 * policy check here, to see
+		 * if we should allow or
+		 * reject a packet from a
Quoted 8 lines

net/ipv4/esp4.c static struct sock *esp_find_tcp_sk(struct xfrm_state *x) · line 90

Quoted 3 lines
net/ipv4/esp4.c +2 -0
+		 * different source
+		 * address/port.
 		 */

Maybe we can get rid of those "XXX" comments? Unless you think the suggestion still makes sense. But the comments (here and in esp6_input_done2) have been here a long time and it doesn't seem to bother users.

The whole NAT-T mapping assumptions / rewriting are not RFC compliant anyway, and need fixing. Similar to accepting encap/non-encap with a single state. So I guess yes, this one comment on the whole issue might as well get removed here.

Paul

author maintainer
#5 lore raw
In reply to Sabrina Dubroca
Quoted 10 lines · Sabrina Dubroca
Quoted 4 lines · 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 prize of three indirect calls on UDP/TCP encapsulation.

If that turns out to be a problem, maybe we can figure out a way to use the INDIRECT_CALL* helpers here as well (but currently they don't work for modules, I played with that a while back and could look into it again).

I have no indication that it could be a problem so far, but would be nice to have the INDIRECT_CALL* helpers for modules anyway!

Quoted 14 lines
Quoted 2 lines
net/ipv4/esp4.c +0 -2
-int esp_input_done2(struct sk_buff *skb, int err)
-{

[...]

Quoted 11 lines
net/ipv4/esp4.c +11 -0
+	if (iph->saddr != x->props.saddr.a4 ||
+	    source != encap->encap_sport) {
+		xfrm_address_t ipaddr;
+
+		ipaddr.a4 = iph->saddr;
+		km_new_mapping(x, &ipaddr, source);
+
+		/* XXX: perhaps add an extra
+		 * policy check here, to see
+		 * if we should allow or
+		 * reject a packet from a
Quoted 8 lines

net/ipv4/esp4.c static struct sock *esp_find_tcp_sk(struct xfrm_state *x) · line 90

Quoted 3 lines
net/ipv4/esp4.c +2 -0
+		 * different source
+		 * address/port.
 		 */

Maybe we can get rid of those "XXX" comments? Unless you think the suggestion still makes sense. But the comments (here and in esp6_input_done2) have been here a long time and it doesn't seem to bother users.

Good point, I'll remove them in the next version.

Quoted 7 lines

[...]

Quoted 1 line

static int esp_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)

Looking at esp_init_state and esp6_init_state, they also have a lot in common (pretty much everything except some x->props.header_len adjustments) that could be extracted into the new file.

Yes, true. This patch is just a start, there is much more possible.

Quoted 14 lines

[...]

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

Quoted 5 lines
net/ipv6/esp6.c +4 -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);
+	int hdr_len = skb_network_header_len(skb);

As Simon mentioned, it's set/incremented but not used. The current code in esp6_input_done2 would then use that value in skb_set_transport_header, but now the common esp_input_done2 calls ->input_encap and doesn't get that increased value back. I think we'll need to have esp_input_done2 pass &hdr_len when it calls the ->input_encap to get the correct value and take into account ipv6 exthdrs.

Either this, or ->input_encap returns the offset of the ipv6 exthdrs and then add this value to hdr_len in esp_input_done2().

Quoted 24 lines

[...]

Quoted 10 lines
net/xfrm/xfrm_esp.c new file +10 -0
+static void esp_output_done(void *data, int err)
+{
+	struct sk_buff *skb = data;
+	struct xfrm_offload *xo = xfrm_offload(skb);
+	void *tmp;
+	struct xfrm_state *x;
+
+	if (xo && (xo->flags & XFRM_DEV_RESUME)) {
+		struct sec_path *sp = skb_sec_path(skb);
+

net/xfrm/xfrm_esp.c line 199

Quoted 10 lines
net/xfrm/xfrm_esp.c new file +10 -0
+		x = sp->xvec[sp->len - 1];
+	} else {
+		x = skb_dst(skb)->xfrm;
+	}
+
+	tmp = ESP_SKB_CB(skb)->tmp;
+	esp_ssg_unref(x, tmp, skb);
+	kfree(tmp);
+
+	x->type->output_encap_csum(skb);

Since the ipv4 variant doesn't do anything, maybe

Quoted 7 lines

if (x->type->output_encap_csum)

	x->type->output_encap_csum(skb);

and don't set it in esp_type?

OTOH it's nice to have a consistent "all CBs are always defined" and just call them unconditionally.

Let's keeep the CBs always defined.

Quoted 7 lines
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;
Quoted 8 lines

net/xfrm/xfrm_esp.c line 289

Quoted 5 lines
net/xfrm/xfrm_esp.c new file +5 -0
+	unsigned int len;
+	struct xfrm_offload *xo = xfrm_offload(skb);
+
+	len = skb->len + esp->tailen - skb_transport_offset(skb);
+	if (len + sizeof(struct iphdr) > IP_MAX_MTU)

This is now used by both IPv4 and IPv6, and esp6_output_udp_encap didn't have any adjustment in this test.

I wonder whether this check makes sense here at all. We add ESP headers and trailers, and just in case of UDP/TCP encapsulation, we check the size of the resulting packet. If we need such a check, it should be on a more generic place.

Another thing I noticed when looking into this. The code above is from ipv4 udpencap. All other encap functions do:

len = skb->len + esp->tailen - skb_transport_offset(skb); if (len > IP_MAX_MTU)

	return ERR_PTR(-EMSGSIZE);

I think this is 'kind of' correct for ipv6, but not for ipv4 tcpencap:

#define IP_MAX_MTU 0xFFFFU
#define IP6_MAX_MTU (0xFFFF + sizeof(struct ipv6hdr))

Quoted 12 lines
Quoted 12 lines
net/xfrm/xfrm_esp.c new file +12 -0
+		return ERR_PTR(-EMSGSIZE);
+
+	uh = (struct udphdr *)esp->esph;
+	uh->source = sport;
+	uh->dest = dport;
+	uh->len = htons(len);
+	uh->check = 0;
+
+	/* For IPv4 ESP with UDP encapsulation, if xo is not null, the skb is in the crypto offload
+	 * data path, which means that esp_output_udp_encap is called outside of the XFRM stack.
+	 * In this case, the mac header doesn't point to the IPv4 protocol field, so don't set it.
+	 */
Quoted 8 lines

net/xfrm/xfrm_esp.c line 303

Quoted 2 lines
net/xfrm/xfrm_esp.c new file +2 -0
+	if (!xo || encap_type != UDP_ENCAP_ESPINUDP)
+		*skb_mac_header(skb) = IPPROTO_UDP;

That was absent in esp6_output_udp_encap, commit 447bc4b1906f ("xfrm:
Support crypto offload for outbound IPv4 UDP-encapsulated ESP packet") only took care of IPv4. I'm not sure adding this to the IPv6 code without adapting the rest of 447bc4b1906f in the esp6_offload code is correct.

Hm, that would mean ipv6 udpencap offload can be configured, but does not work in the current codebase. Maybe this needs a separate fix.

Quoted 8 lines
Quoted 3 lines
net/xfrm/xfrm_esp.c new file +3 -0
+
+	return (struct ip_esp_hdr *)(uh + 1);
+}

[...]

Quoted 1 line

+ int esp_output(struct xfrm_state *x, struct sk_buff *skb)

nit: ' ' at the start of the line

Fixed.

Thanks!