Skip to contentSkip to conversation

netfilter: flowtable: fix IP6IP6 tunnel offset double-count with vlan/pppoe encap

PATCH In review David Carlier 3 participants 4 messages Jun 4 – Jun 5

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: flowtable: fix IP6IP6 tunnel offset double-count with vlan/pppoe encap

nf_flow_ip6_tunnel_proto() stores the return value of ipv6_skip_exthdr() directly into ctx->tun.hdr_size and then does ctx->offset += ctx->tun.hdr_size.

ipv6_skip_exthdr() returns an offset measured from skb->data, i.e. its result already includes the "sizeof(*ip6h) + ctx->offset" start argument. So hdr_size ends up containing ctx->offset, and the subsequent "ctx->offset += ctx->tun.hdr_size" counts the encap offset twice.

This is harmless for a bare IP6IP6 packet, where ctx->offset is 0 on entry, which is why it has gone unnoticed. But nf_flow_skb_encap_protocol() advances ctx->offset by VLAN_HLEN / PPPOE_SES_HLEN before the tunnel parser runs, so for an IP6IP6 flow carried over vlan or pppoe both ctx->offset and ctx->tun.hdr_size are off by the encap length:

- nf_flow_tuple_ipv6() then reads the inner header at the wrong offset, the computed tuple no longer matches the flowtable entry, and the packet silently falls back to the slow path (IP6IP6 rx acceleration stops working);
- on the forward path nf_flow_ip_tunnel_pop() would skb_pull() past the inner header.

The IPv4 sibling nf_flow_ip4_tunnel_proto() does this correctly: it stores a relative header length (iph->ihl << 2) and adds that to ctx->offset. Make the IPv6 path symmetric by storing the relative size.

Fixes d98103575dcd ("netfilter: flowtable: Add IP6IP6 rx sw acceleration")
---
 net/netfilter/nf_flow_table_ip.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_flow_table_ip.c +1 -1
index 9c05a50d6013..4c6a68765c6b 100644
@@ -366,7 +366,7 @@ static bool nf_flow_ip6_tunnel_proto(struct nf_flowtable_ctx *ctx,
 		return false;
 
 	if (nexthdr == IPPROTO_IPV6) {
-		ctx->tun.hdr_size = hdrlen;
+		ctx->tun.hdr_size = hdrlen - ctx->offset;
 		ctx->tun.proto = IPPROTO_IPV6;
 	}
 	ctx->offset += ctx->tun.hdr_size;
Signature · 1 line
2.53.0
Acked-by on v1 1/1
#1 lore raw
Quoted 22 lines

nf_flow_ip6_tunnel_proto() stores the return value of ipv6_skip_exthdr() directly into ctx->tun.hdr_size and then does ctx->offset += ctx->tun.hdr_size.

ipv6_skip_exthdr() returns an offset measured from skb->data, i.e. its result already includes the "sizeof(*ip6h) + ctx->offset" start argument. So hdr_size ends up containing ctx->offset, and the subsequent "ctx->offset += ctx->tun.hdr_size" counts the encap offset twice.

This is harmless for a bare IP6IP6 packet, where ctx->offset is 0 on entry, which is why it has gone unnoticed. But nf_flow_skb_encap_protocol() advances ctx->offset by VLAN_HLEN / PPPOE_SES_HLEN before the tunnel parser runs, so for an IP6IP6 flow carried over vlan or pppoe both ctx->offset and ctx->tun.hdr_size are off by the encap length:

- nf_flow_tuple_ipv6() then reads the inner header at the wrong offset, the computed tuple no longer matches the flowtable entry, and the packet silently falls back to the slow path (IP6IP6 rx acceleration stops working);
- on the forward path nf_flow_ip_tunnel_pop() would skb_pull() past the inner header.

Quoted 6 lines

The IPv4 sibling nf_flow_ip4_tunnel_proto() does this correctly: it stores a relative header length (iph->ihl << 2) and adds that to ctx->offset. Make the IPv6 path symmetric by storing the relative size.

Fixes d98103575dcd ("netfilter: flowtable: Add IP6IP6 rx sw acceleration")

Hi David,

thx for fixing it. I developed the IP6IP6 vlan support using the veth as underlying device. veth enables vlan rx/tx offload by default so I was not able to spot the issue.

Regards,
Lorenzo

Quoted 20 lines
---
 net/netfilter/nf_flow_table_ip.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_flow_table_ip.c +1 -1
index 9c05a50d6013..4c6a68765c6b 100644
@@ -366,7 +366,7 @@ static bool nf_flow_ip6_tunnel_proto(struct nf_flowtable_ctx *ctx,
 		return false;
 
 	if (nexthdr == IPPROTO_IPV6) {
-		ctx->tun.hdr_size = hdrlen;
+		ctx->tun.hdr_size = hdrlen - ctx->offset;
 		ctx->tun.proto = IPPROTO_IPV6;
 	}
 	ctx->offset += ctx->tun.hdr_size;
Signature · 1 line
2.53.0
#2 lore raw
Quoted 26 lines · Lorenzo Bianconi
Quoted 26 lines

nf_flow_ip6_tunnel_proto() stores the return value of ipv6_skip_exthdr() directly into ctx->tun.hdr_size and then does ctx->offset += ctx->tun.hdr_size.

ipv6_skip_exthdr() returns an offset measured from skb->data, i.e. its result already includes the "sizeof(*ip6h) + ctx->offset" start argument. So hdr_size ends up containing ctx->offset, and the subsequent "ctx->offset += ctx->tun.hdr_size" counts the encap offset twice.

This is harmless for a bare IP6IP6 packet, where ctx->offset is 0 on entry, which is why it has gone unnoticed. But nf_flow_skb_encap_protocol() advances ctx->offset by VLAN_HLEN / PPPOE_SES_HLEN before the tunnel parser runs, so for an IP6IP6 flow carried over vlan or pppoe both ctx->offset and ctx->tun.hdr_size are off by the encap length:

- nf_flow_tuple_ipv6() then reads the inner header at the wrong offset, the computed tuple no longer matches the flowtable entry, and the packet silently falls back to the slow path (IP6IP6 rx acceleration stops working);
- on the forward path nf_flow_ip_tunnel_pop() would skb_pull() past the inner header.

The IPv4 sibling nf_flow_ip4_tunnel_proto() does this correctly: it stores a relative header length (iph->ihl << 2) and adds that to ctx->offset. Make the IPv6 path symmetric by storing the relative size.

Quoted 8 lines
Quoted 2 lines
Fixes d98103575dcd ("netfilter: flowtable: Add IP6IP6 rx sw acceleration")

Hi David,

thx for fixing it. I developed the IP6IP6 vlan support using the veth as underlying device. veth enables vlan rx/tx offload by default so I was not able to spot the issue.

One question when looking at this code:

In nf_flow_ip6_tunnel_proto():

        if (nexthdr == IPPROTO_IPV6) {
                ctx->tun.hdr_size = hdrlen - ctx->offset;
                ctx->tun.proto = IPPROTO_IPV6;
        }
        ctx->offset += ctx->tun.hdr_size;

ctx->offset is bumped out of the branch.

and nf_flow_ip4_tunnel_proto():

        if (iph->protocol == IPPROTO_IPIP) {
                ctx->tun.proto = IPPROTO_IPIP;
                ctx->tun.hdr_size = size;
                ctx->offset += size;
        }

I think these checks are superfluous at this stage:

if (nexthdr == IPPROTO_IPV6) {

if (iph->protocol == IPPROTO_IPIP) {

because only ipip and ip6ip6 is supported.

#3 lore raw
Quoted 56 lines · Lorenzo Bianconi
Quoted 26 lines
Quoted 26 lines

nf_flow_ip6_tunnel_proto() stores the return value of ipv6_skip_exthdr() directly into ctx->tun.hdr_size and then does ctx->offset += ctx->tun.hdr_size.

ipv6_skip_exthdr() returns an offset measured from skb->data, i.e. its result already includes the "sizeof(*ip6h) + ctx->offset" start argument. So hdr_size ends up containing ctx->offset, and the subsequent "ctx->offset += ctx->tun.hdr_size" counts the encap offset twice.

This is harmless for a bare IP6IP6 packet, where ctx->offset is 0 on entry, which is why it has gone unnoticed. But nf_flow_skb_encap_protocol() advances ctx->offset by VLAN_HLEN / PPPOE_SES_HLEN before the tunnel parser runs, so for an IP6IP6 flow carried over vlan or pppoe both ctx->offset and ctx->tun.hdr_size are off by the encap length:

- nf_flow_tuple_ipv6() then reads the inner header at the wrong offset, the computed tuple no longer matches the flowtable entry, and the packet silently falls back to the slow path (IP6IP6 rx acceleration stops working);
- on the forward path nf_flow_ip_tunnel_pop() would skb_pull() past the inner header.

The IPv4 sibling nf_flow_ip4_tunnel_proto() does this correctly: it stores a relative header length (iph->ihl << 2) and adds that to ctx->offset. Make the IPv6 path symmetric by storing the relative size.

Quoted 8 lines
Quoted 2 lines
Fixes d98103575dcd ("netfilter: flowtable: Add IP6IP6 rx sw acceleration")

Hi David,

thx for fixing it. I developed the IP6IP6 vlan support using the veth as underlying device. veth enables vlan rx/tx offload by default so I was not able to spot the issue.

One question when looking at this code:

In nf_flow_ip6_tunnel_proto():

        if (nexthdr == IPPROTO_IPV6) {
                ctx->tun.hdr_size = hdrlen - ctx->offset;
                ctx->tun.proto = IPPROTO_IPV6;
        }
        ctx->offset += ctx->tun.hdr_size;

ctx->offset is bumped out of the branch.

and nf_flow_ip4_tunnel_proto():

        if (iph->protocol == IPPROTO_IPIP) {
                ctx->tun.proto = IPPROTO_IPIP;
                ctx->tun.hdr_size = size;
                ctx->offset += size;
        }
Quoted 7 lines

I think these checks are superfluous at this stage:

if (nexthdr == IPPROTO_IPV6) {

if (iph->protocol == IPPROTO_IPIP) {

because only ipip and ip6ip6 is supported.

In this series [0] I modified a bit this code to support the mixed cases (SIT and IPv4 over IPv6).

Regards,
Lorenzo

[0] https://lore.kernel.org/netfilter-devel/20260531-b4-flowtable-sw-accel-ip6ip-v3-0-56a2826f3279@kernel.org/