[PATCH ipsec v2 2/2] xfrm: refuse modes that AH and ESP do not support
From: Kyuwon Cho
Date: Tue Nov 24 2026 - 21:43:06 EST
ah6_init_state() refuses any mode but transport, tunnel and BEET, but
ah_init_state(), esp_init_state() and esp6_init_state() accept any mode
and only adjust header_len for the ones they know. For ESP over IPv6,
a state in route optimization mode then reaches the WARN_ON_ONCE()s for
unknown modes in xfrm_state_mtu() and xfrm_inner_mode_input(), as long
as verify_newsa_info() lets the mode through. Over IPv4 there is no
route optimization mode, and __xfrm_init_state() refuses it before the
type is set up.
Make these functions refuse the modes they do not support, as
ah6_init_state() does, so that each type checks its own modes. ESP
keeps IP-TFS.
Fixes: c2d305e51038 ("xfrm: remove input indirection from xfrm_mode")
Fixes: c7b37c769d2a ("xfrm: remove get_mtu indirection from xfrm_type")
Suggested-by: Sabrina Dubroca <sd@xxxxxxxxxxxxxxx>
Signed-off-by: Kyuwon Cho <kyuwoncho18@xxxxxxxxx>
---
net/ipv4/ah4.c | 11 ++++++++++-
net/ipv4/esp4.c | 18 +++++++++++++++---
net/ipv6/esp6.c | 6 +++++-
3 files changed, 30 insertions(+), 5 deletions(-)
diff --git a/net/ipv4/ah4.c b/net/ipv4/ah4.c
index 6fd642d2278d..32094169718c 100644
--- a/net/ipv4/ah4.c
+++ b/net/ipv4/ah4.c
@@ -535,8 +535,17 @@ static int ah_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
else
x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
ahp->icv_trunc_len);
- if (x->props.mode == XFRM_MODE_TUNNEL)
+ switch (x->props.mode) {
+ case XFRM_MODE_BEET:
+ case XFRM_MODE_TRANSPORT:
+ break;
+ case XFRM_MODE_TUNNEL:
x->props.header_len += sizeof(struct iphdr);
+ break;
+ default:
+ NL_SET_ERR_MSG(extack, "Invalid mode requested for AH");
+ goto error;
+ }
x->data = ahp;
return 0;
diff --git a/net/ipv4/esp4.c b/net/ipv4/esp4.c
index 111bea0bd260..1bb7c4b24cc6 100644
--- a/net/ipv4/esp4.c
+++ b/net/ipv4/esp4.c
@@ -1148,10 +1148,22 @@ static int esp_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
x->props.header_len = sizeof(struct ip_esp_hdr) +
crypto_aead_ivsize(aead);
- if (x->props.mode == XFRM_MODE_TUNNEL)
+ switch (x->props.mode) {
+ case XFRM_MODE_BEET:
+ if (x->sel.family != AF_INET6)
+ x->props.header_len += IPV4_BEET_PHMAXLEN;
+ break;
+ case XFRM_MODE_TRANSPORT:
+ case XFRM_MODE_IPTFS:
+ break;
+ case XFRM_MODE_TUNNEL:
x->props.header_len += sizeof(struct iphdr);
- else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
- x->props.header_len += IPV4_BEET_PHMAXLEN;
+ break;
+ default:
+ NL_SET_ERR_MSG(extack, "Invalid mode requested for ESP");
+ err = -EINVAL;
+ goto error;
+ }
if (x->encap) {
struct xfrm_encap_tmpl *encap = x->encap;
diff --git a/net/ipv6/esp6.c b/net/ipv6/esp6.c
index b287c807b018..ced910d44b47 100644
--- a/net/ipv6/esp6.c
+++ b/net/ipv6/esp6.c
@@ -1192,12 +1192,16 @@ static int esp6_init_state(struct xfrm_state *x, struct netlink_ext_ack *extack)
x->props.header_len += IPV4_BEET_PHMAXLEN +
(sizeof(struct ipv6hdr) - sizeof(struct iphdr));
break;
- default:
case XFRM_MODE_TRANSPORT:
+ case XFRM_MODE_IPTFS:
break;
case XFRM_MODE_TUNNEL:
x->props.header_len += sizeof(struct ipv6hdr);
break;
+ default:
+ NL_SET_ERR_MSG(extack, "Invalid mode requested for ESP");
+ err = -EINVAL;
+ goto error;
}
if (x->encap) {
--
2.43.0