[PATCH net v4 2/2] xfrm: fix selector matching for IPv6 atomic fragments

From: Jérémy Jean

Date: Mon Nov 09 2026 - 16:39:20 EST


Commit 7e3a42a12c4b ("xfrm6: handling fragment") stopped decoding IPv6
selector fields after a Fragment header. That is correct for real
fragments because later fragments do not carry those fields, yet an
atomic fragment contains the complete datagram. Leaving its ports,
ICMPv6 type and code, and GRE key zero lets a plaintext packet miss a
policy that matches on one of them.

Leave these fields unset for real fragments. For atomic fragments,
recover the ports at the transport offset found by the flow dissector
and use the decoded ICMPv6 and GRE fields. Have ipv6_find_hdr()
report non-atomic Fragment headers and restore selectors only when
every Fragment header in the chain is atomic.

Read the initial IPv6 header with skb_header_pointer() so nonlinear
headers and headers before skb->data can be inspected without pulling
data or reallocating the skb head. Callers of the policy check can
keep header pointers across session decoding.

Fixes: 7e3a42a12c4b ("xfrm6: handling fragment")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Jérémy Jean <Jeremy.Jean@xxxxxxxxxxxxxxxxx>
---
include/net/ipv6.h | 1 +
net/ipv6/exthdrs_core.c | 25 +++++++++++++------------
net/xfrm/xfrm_policy.c | 16 +++++++++++++++-
3 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/include/net/ipv6.h b/include/net/ipv6.h
index 3de07e738538f..10785721b013d 100644
--- a/include/net/ipv6.h
+++ b/include/net/ipv6.h
@@ -1110,6 +1110,7 @@ enum {
IP6_FH_F_FRAG = (1 << 0),
IP6_FH_F_AUTH = (1 << 1),
IP6_FH_F_SKIP_RH = (1 << 2),
+ IP6_FH_F_NON_ATOMIC = (1 << 3),
};

/* find specified header and get offset to it */
diff --git a/net/ipv6/exthdrs_core.c b/net/ipv6/exthdrs_core.c
index 4a9748338cf40..f00c28ce2f1e7 100644
--- a/net/ipv6/exthdrs_core.c
+++ b/net/ipv6/exthdrs_core.c
@@ -183,7 +183,8 @@ EXPORT_SYMBOL_GPL(ipv6_find_tlv);
* isn't NULL.
*
* if flags is not NULL and it's a fragment, then the frag flag
- * IP6_FH_F_FRAG will be set. If it's an AH header, the
+ * IP6_FH_F_FRAG will be set. IP6_FH_F_NON_ATOMIC will be set for a
+ * non-atomic Fragment header. If it's an AH header, the
* IP6_FH_F_AUTH flag is set and target < 0, then this function will
* stop at the AH header. If IP6_FH_F_SKIP_RH flag was passed, then this
* function will skip all those routing headers, where segements_left was 0.
@@ -191,23 +192,20 @@ EXPORT_SYMBOL_GPL(ipv6_find_tlv);
int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
int target, unsigned short *fragoff, int *flags)
{
- unsigned int start = skb_network_offset(skb) + sizeof(struct ipv6hdr);
- u8 nexthdr = ipv6_hdr(skb)->nexthdr;
+ unsigned int start = *offset ?: skb_network_offset(skb);
+ struct ipv6hdr _ip6, *ip6;
int exthdr_cnt = 0;
+ u8 nexthdr;
bool found;

if (fragoff)
*fragoff = 0;

- if (*offset) {
- struct ipv6hdr _ip6, *ip6;
-
- ip6 = skb_header_pointer(skb, *offset, sizeof(_ip6), &_ip6);
- if (!ip6 || (ip6->version != 6))
- return -EBADMSG;
- start = *offset + sizeof(struct ipv6hdr);
- nexthdr = ip6->nexthdr;
- }
+ ip6 = skb_header_pointer(skb, start, sizeof(_ip6), &_ip6);
+ if (!ip6 || (*offset && ip6->version != 6))
+ return -EBADMSG;
+ nexthdr = ip6->nexthdr;
+ start += sizeof(struct ipv6hdr);

do {
struct ipv6_opt_hdr _hdr, *hp;
@@ -254,6 +252,9 @@ int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
if (!fp)
return -EBADMSG;

+ if (flags && (*fp & htons(IP6_OFFSET | IP6_MF)))
+ *flags |= IP6_FH_F_NON_ATOMIC;
+
_frag_off = ntohs(*fp) & ~0x7;
if (_frag_off) {
if (target < 0 &&
diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
index bda5100c9fdbf..bf2c04180b98c 100644
--- a/net/xfrm/xfrm_policy.c
+++ b/net/xfrm/xfrm_policy.c
@@ -3526,9 +3526,23 @@ int __xfrm_decode_session(struct net *net, struct sk_buff *skb, struct flowi *fl
decode_session4(&flkeys, fl, reverse);
break;
#if IS_ENABLED(CONFIG_IPV6)
- case AF_INET6:
+ case AF_INET6: {
+ unsigned int offset = 0;
+ int fh_flags = 0;
+
+ if ((flkeys.control.flags & FLOW_DIS_IS_FRAGMENT) &&
+ ipv6_find_hdr(skb, &offset, -1, NULL, &fh_flags) >= 0 &&
+ (fh_flags & IP6_FH_F_FRAG) &&
+ !(fh_flags & IP6_FH_F_NON_ATOMIC)) {
+ flkeys.control.flags &= ~FLOW_DIS_IS_FRAGMENT;
+ flkeys.ports.ports = skb_flow_get_ports(skb,
+ flkeys.control.thoff,
+ flkeys.basic.ip_proto,
+ NULL, 0);
+ }
decode_session6(&flkeys, fl, reverse);
break;
+ }
#endif
default:
return -EAFNOSUPPORT;
--
2.47.3