Skip to contentSkip to conversation

FYI 6.4 xfrm_prepare_input/xfrm_inner_mode_encap_remove WARN_ON hit - related to ESPinUDP

PATCH In review Maciej Żenczykowski 4 participants 10 messages Jun 30, 2023 – Jul 11, 2023

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

PATCH 1/1 FYI 6.4 xfrm_prepare_input/xfrm_inner_mode_encap_remove WARN_ON hit - related to ESPinUDP

Steffan, this isn't of course a patch meant for inclusion, instead just a WARN_ON hit report. The patch is simply what prints the following extra info:

xfrm_prepare_input: XFRM_MODE_SKB_CB(skb)->protocol: 17 xfrm_inner_mode_encap_remove: x->props.mode: 1 XFRM_MODE_SKB_SB(skb)->protocol:17

(note: XFRM_MODE_TUNNEL=1 IPPROTO_UDP=17)

Hit on Linux 6.4 by:
https://cs.android.com/android/platform/superproject/+/master:kernel/tests/net/test/xfrm_test.py

likely related to line 230:
encap_sock.setsockopt(IPPROTO_UDP, xfrm.UDP_ENCAP, xfrm.UDP_ENCAP_ESPINUDP)

I'm not the author of these tests, and I know very little about XFRM. As such, I'm not sure if there isn't a bug in the tests themselves... maybe we're generating invalid packets that aren't meant to be decapsulated???

Or are we missing some sort of assignment inside of the ESP in UDP decap codepath?

Somewhere in the vicinity of xfrm4_udp_encap_rcv / xfrm4_rcv_encap (and the v6 equivalents)

$ git log --decorate --oneline --graph -n 3
* c1ae64591391 (HEAD) FYI 6.4 xfrm_prepare_input/xfrm_inner_mode_encap_remove WARN_ON hit - related to ESPinUDP
* da2779e6377a ANDROID: net: xfrm: make PF_KEY SHA256 use RFC-compliant truncation.  [trivial: .icv_truncbits = 96 -> 128 -- just ignore it]
* 6995e2de6891 (tag: remotes/linux/v6.4) Linux 6.4

Full call stack from running the Android net test suite on a 6.4 UML via:
ARCH=um SUBARCH=x86_64 /aosp-tests/net/test/run_net_test.sh --builder all_tests.sh

*#### ./xfrm_test.py (11/24)

.........xfrm_prepare_input: XFRM_MODE_SKB_CB(skb)->protocol: 17

------------[ cut here ]------------
WARNING: CPU: 0 PID: 326 at net/xfrm/xfrm_input.c:380 xfrm_input+0x7fc/0x115d
Modules linked in:
CPU: 0 PID: 326 Comm: xfrm_test.py Not tainted 6.4.0-gc1ae64591391 #8
Stack:
 604bc85a 605bc4ee 818ef5b0 604a3aea
 818ef650 605bc4ee 60037b59 604bc85a
 00000001 605ae79b 818ef5e0 604c3327
Call Trace:
 [<604bc85a>] ? _printk+0x0/0x94
 [<60025a79>] show_stack+0x113/0x18b
 [<604bc85a>] ? _printk+0x0/0x94
 [<604a3aea>] ? dump_stack_print_info+0xe1/0xf0
 [<60037b59>] ? um_set_signals+0x0/0x3e
 [<604bc85a>] ? _printk+0x0/0x94
 [<604c3327>] dump_stack_lvl+0x4a/0x58
 [<604c32dd>] ? dump_stack_lvl+0x0/0x58
 [<60041747>] ? check_panic_on_warn+0x0/0x6f
 [<604c334f>] dump_stack+0x1a/0x1c
 [<600419b4>] __warn+0xcd/0x118
 [<604bbc1e>] warn_slowpath_fmt+0xe4/0xf2
 [<604bbb3a>] ? warn_slowpath_fmt+0x0/0xf2
 [<604bc85a>] ? _printk+0x0/0x94
 [<603e2958>] ? esp_input+0x28f/0x2ac
 [<603fbae9>] ? xfrm_aevent_is_on+0x21/0x26
 [<603fc087>] ? xfrm_replay_advance+0xf2/0x26a
 [<603f99cf>] xfrm_input+0x7fc/0x115d
 [<6040334e>] xfrmi_input+0xa3/0xb2
 [<60403373>] xfrmi4_input+0x16/0x18
 [<603eb9f0>] xfrm4_rcv_encap+0xb5/0xea
 [<603eb102>] ? xfrm4_dst_destroy+0x67/0xeb
 [<603eb39c>] xfrm4_udp_encap_rcv+0x1c6/0x1e6
 [<603eb1d6>] ? xfrm4_udp_encap_rcv+0x0/0x1e6
 [<603b60f8>] udp_queue_rcv_one_skb+0x77/0x339
 [<603b7957>] udp_queue_rcv_skb+0x5c/0x298
 [<603b7c01>] udp_unicast_rcv_skb+0x6e/0x7c
 [<603b962a>] __udp4_lib_rcv+0x613/0x6fe
 [<603b3641>] ? raw_local_deliver+0x0/0x1c7
 [<603b9a0c>] udp_rcv+0x27/0x29
 [<603809eb>] ip_protocol_deliver_rcu+0x77/0x104
 [<60380b4c>] ip_local_deliver_finish+0xd4/0xdb
 [<60380a78>] ? ip_local_deliver_finish+0x0/0xdb
 [<6037fe91>] NF_HOOK.constprop.0+0x76/0x81
 [<60380a78>] ? ip_local_deliver_finish+0x0/0xdb
 [<6037feed>] ip_local_deliver+0x51/0x72
 [<60380371>] ? ip_rcv_finish+0x0/0x3d
 [<603803a9>] ip_rcv_finish+0x38/0x3d
 [<6037fe91>] NF_HOOK.constprop.0+0x76/0x81
 [<60380371>] ? ip_rcv_finish+0x0/0x3d
 [<60380b9e>] ip_rcv+0x4b/0x50
 [<60318d32>] __netif_receive_skb_one_core+0x46/0x4d
 [<60318da2>] __netif_receive_skb+0x55/0x5c
 [<60318df4>] netif_receive_skb+0x4b/0x4f
 [<602c9838>] tun_rx_batched+0x199/0x1b4
 [<602cf02e>] tun_get_user+0xba5/0xcd8
 [<6003a100>] ? userspace_tramp+0x1a6/0x242
 [<60039b1c>] ? do_syscall_stub+0xfa/0x24a
 [<602cf1d7>] tun_chr_write_iter+0x76/0x9a
 [<601410aa>] new_sync_write+0x8f/0x101
 [<601423d2>] vfs_write+0xe7/0x12a
 [<6016239e>] ? __fdget_pos+0x12/0x4c
 [<60142555>] ksys_write+0x79/0xb5
 [<601425a1>] sys_write+0x10/0x12
 [<60027b34>] handle_syscall+0x79/0xa7
 [<6003a9af>] userspace+0x4cf/0x60f
 [<600248bf>] fork_handler+0x92/0x94
---[ end trace 0000000000000000 ]---
xfrm_inner_mode_encap_remove: x->props.mode: 1 XFRM_MODE_SKB_SB(skb)->protocol:17
------------[ cut here ]------------
WARNING: CPU: 0 PID: 326 at net/xfrm/xfrm_input.c:352 xfrm_input+0xf2f/0x115d

...

Cc Herbert Xu <[email protected]>
Cc Steffen Klassert <[email protected]>
Cc Benedict Wong <[email protected]>
Cc Lorenzo Colitti <[email protected]>
Cc Yan Yan <[email protected]>
---
 net/xfrm/xfrm_input.c | 2 ++
 1 file changed, 2 insertions(+)
net/xfrm/xfrm_input.c +2 -0
index 815b38080401..bd10605b211d 100644
@@ -348,6 +348,7 @@ xfrm_inner_mode_encap_remove(struct xfrm_state *x,
 		}
 	}
 
+	printk(KERN_WARNING "xfrm_inner_mode_encap_remove: x->props.mode: %d XFRM_MODE_SKB_SB(skb)->protocol:%d\n", x->props.mode, XFRM_MODE_SKB_CB(skb)->protocol);
 	WARN_ON_ONCE(1);
 	return -EOPNOTSUPP;
 }
@@ -375,6 +376,7 @@ static int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
 		skb->protocol = htons(ETH_P_IPV6);
 		break;
 	default:
+		printk(KERN_WARNING "xfrm_prepare_input: XFRM_MODE_SKB_CB(skb)->protocol: %d\n", XFRM_MODE_SKB_CB(skb)->protocol);
 		WARN_ON_ONCE(1);
 		break;
 	}
Signature · 1 line
2.41.0.255.g8b1d071c50-goog
#1 lore raw
Quoted 1 line · Maciej Żenczykowski

Steffan, this isn't of course a patch meant for inclusion, instead just a WARN_ON hit report.

Sorry for the name typo (it's Stefan in Polish).

Quoted 13 lines

The patch is simply what prints the following extra info:

xfrm_prepare_input: XFRM_MODE_SKB_CB(skb)->protocol: 17 xfrm_inner_mode_encap_remove: x->props.mode: 1 XFRM_MODE_SKB_SB(skb)->protocol:17

(note: XFRM_MODE_TUNNEL=1 IPPROTO_UDP=17)

Hit on Linux 6.4 by:
https://cs.android.com/android/platform/superproject/+/master:kernel/tests/net/test/xfrm_test.py

likely related to line 230:
encap_sock.setsockopt(IPPROTO_UDP, xfrm.UDP_ENCAP, xfrm.UDP_ENCAP_ESPINUDP)

Quoted 8 lines

I'm not the author of these tests, and I know very little about XFRM. As such, I'm not sure if there isn't a bug in the tests themselves... maybe we're generating invalid packets that aren't meant to be decapsulated???

Or are we missing some sort of assignment inside of the ESP in UDP decap codepath?

Somewhere in the vicinity of xfrm4_udp_encap_rcv / xfrm4_rcv_encap (and the v6 equivalents)

I've done some bisection (well more like educated guesswork) and the regression (if one should call it that?) is caused by 6.4

commit 5f24f41e8ea62a6a9095f9bbafb8b3aebe265c68
Author: Herbert Xu <[email protected]>
xfrm: Remove inner/outer modes from input path

The xfrm tests pass either way, but with the above reverted it no longer triggers the WARN_ON.

$ git log --decorate --oneline --graph -n 3
* da7dc0870b19 (HEAD) Revert "xfrm: Remove inner/outer modes from
input path"  <-- passes, doesn't warn
* 51d5381c5809 ANDROID: net: xfrm: make PF_KEY SHA256 use
RFC-compliant truncation.  <-- passes, does warn
* 5f24f41e8ea6 xfrm: Remove inner/outer modes from input path  <--
passes xfrm, fails pf_key, warns
maintainer
#2 lore raw
In reply to Maciej Żenczykowski
Quoted 5 lines · Maciej Żenczykowski

Steffan, this isn't of course a patch meant for inclusion, instead just a WARN_ON hit report. The patch is simply what prints the following extra info:

xfrm_prepare_input: XFRM_MODE_SKB_CB(skb)->protocol: 17 xfrm_inner_mode_encap_remove: x->props.mode: 1 XFRM_MODE_SKB_SB(skb)->protocol:17

This seems to make no sense. UDP encapsulation is supposed to sit on the outside of ESP. So by the time we hit xfrm_input it should be lone gone. On the inside of the packet, as it's tunnel mode we should have either IPIP or IPV6, definitely not UDP.

Are you able to reduce this to a set of "ip xfrm" commands that I can use to reproduce this?

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw
Quoted 7 lines · Herbert Xu
Quoted 2 lines

xfrm_prepare_input: XFRM_MODE_SKB_CB(skb)->protocol: 17 xfrm_inner_mode_encap_remove: x->props.mode: 1 XFRM_MODE_SKB_SB(skb)->protocol:17

This seems to make no sense. UDP encapsulation is supposed to sit on the outside of ESP. So by the time we hit xfrm_input it should be lone gone. On the inside of the packet, as it's tunnel mode we should have either IPIP or IPV6, definitely not UDP.

It's triggering in testIPv4UDPEncapRecvTunnel() in xfrm_test.py. Specifically, it's the self.ReceivePacketOn(netid, input_pkt) a dozen lines higher.

The packet we end up writing into the tap fd is
02 00 00 00 C8 01 02 00 00 00 C8 00 08 00
45 00 00 44 00 01 00 00 40 11 98 96 08 08 08 08 0A 00 C8 02 11 94 BF 12 00 30 1C D0
00 00 12 34 00 00 00 01
45 00 00 20 00 01 00 00 40 11 98 BA 08 08 08 08 0A 00 C8 02 01 BB 7D 7B 00 0C 9B 7A
01 02 02 11

You can decode this with https://hpd.gasmi.net/ or https://packetor.com/

You can decode the inner packet (this is null esp crypto) by passing in 00 00 00 00 00 00 00 00 00 00 00 00 08 00
45 00 00 20 00 01 00 00 40 11 98 BA 08 08 08 08 0A 00 C8 02 01 BB 7D 7B 00 0C 9B 7A
01 02 02 11
instead.

Note that the protocol the kernel's printk I added prints is the *outer* encap UDP protocol, not the inner UDP.
ie. you can change the scapy.UDP to scapy.TCP in the 'inner_pkt =' assignment, and the warning still triggers. The resulting packet is:
02 00 00 00 FA 01 02 00 00 00 FA 00 08 00
45 00 00 50 00 01 00 00 40 11 66 8A 08 08 08 08 0A 00 FA 02
11 94 A7 EB 00 3C 33 E0
00 00 12 34 00 00 00 01
45 00 00 2C 00 01 00 00 40 06 66 B9 08 08 08 08 0A 00 FA 02
01 BB 7D 7B 00 00 00 00 00 00 00 00 50 02 20 00 F9 82 00 00
01 02 02 11

ie. the inner packet is IPv4/TCP:
00 00 00 00 00 00 00 00 00 00 00 00 08 00
45 00 00 2C 00 01 00 00 40 06 66 B9 08 08 08 08 0A 00 FA 02 01 BB 7D 7B 00 00 00 00 00 00 00 00 50 02 20 00 F9 82 00 00 01 02 02 11

Quoted 2 lines

Are you able to reduce this to a set of "ip xfrm" commands that I can use to reproduce this?

#4 lore raw
Quoted 43 lines · Maciej Żenczykowski
Quoted 7 lines · Herbert Xu
Quoted 2 lines

xfrm_prepare_input: XFRM_MODE_SKB_CB(skb)->protocol: 17 xfrm_inner_mode_encap_remove: x->props.mode: 1 XFRM_MODE_SKB_SB(skb)->protocol:17

This seems to make no sense. UDP encapsulation is supposed to sit on the outside of ESP. So by the time we hit xfrm_input it should be lone gone. On the inside of the packet, as it's tunnel mode we should have either IPIP or IPV6, definitely not UDP.

It's triggering in testIPv4UDPEncapRecvTunnel() in xfrm_test.py. Specifically, it's the self.ReceivePacketOn(netid, input_pkt) a dozen lines higher.

The packet we end up writing into the tap fd is
02 00 00 00 C8 01 02 00 00 00 C8 00 08 00
45 00 00 44 00 01 00 00 40 11 98 96 08 08 08 08 0A 00 C8 02 11 94 BF 12 00 30 1C D0
00 00 12 34 00 00 00 01
45 00 00 20 00 01 00 00 40 11 98 BA 08 08 08 08 0A 00 C8 02 01 BB 7D 7B 00 0C 9B 7A
01 02 02 11

You can decode this with https://hpd.gasmi.net/ or https://packetor.com/

You can decode the inner packet (this is null esp crypto) by passing in 00 00 00 00 00 00 00 00 00 00 00 00 08 00
45 00 00 20 00 01 00 00 40 11 98 BA 08 08 08 08 0A 00 C8 02 01 BB 7D 7B 00 0C 9B 7A
01 02 02 11
instead.

Note that the protocol the kernel's printk I added prints is the *outer* encap UDP protocol, not the inner UDP.
ie. you can change the scapy.UDP to scapy.TCP in the 'inner_pkt =' assignment, and the warning still triggers. The resulting packet is:
02 00 00 00 FA 01 02 00 00 00 FA 00 08 00
45 00 00 50 00 01 00 00 40 11 66 8A 08 08 08 08 0A 00 FA 02
11 94 A7 EB 00 3C 33 E0
00 00 12 34 00 00 00 01
45 00 00 2C 00 01 00 00 40 06 66 B9 08 08 08 08 0A 00 FA 02
01 BB 7D 7B 00 00 00 00 00 00 00 00 50 02 20 00 F9 82 00 00
01 02 02 11

Quoted 5 lines

ie. the inner packet is IPv4/TCP:
00 00 00 00 00 00 00 00 00 00 00 00 08 00
45 00 00 2C 00 01 00 00 40 06 66 B9 08 08 08 08 0A 00 FA 02 01 BB 7D 7B 00 00 00 00 00 00 00 00 50 02 20 00 F9 82 00 00 01 02 02 11

It looks like the problem is that final '11', and thus the fix is in the python test itself:

https://android-review.googlesource.com/c/kernel/tests/+/2647762

- data += xfrm_base.GetEspTrailer(len(data), IPPROTO_UDP)
+ data += xfrm_base.GetEspTrailer(len(data), {4: IPPROTO_IPIP, 6:
IPPROTO_IPV6}[version])

I guess it's OK that this WARN_ON is remotely triggerable?

maintainer
#5 lore raw
Quoted 2 lines · Maciej Żenczykowski

I guess it's OK that this WARN_ON is remotely triggerable?

That's a good point.

We should audit all the WARN_ONs in net/xfrm and get rid of the ones that can be triggered by a remote peer.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#6 lore raw
Quoted 4 lines

That's a good point.

We should audit all the WARN_ONs in net/xfrm and get rid of the ones that can be triggered by a remote peer.

While I'm looking at this code... I noticed the comment:

if mode == xfrm.XFRM_MODE_TRANSPORT:
# Due to a bug in the IPv6 UDP encap code, there must be at least 32 # bytes after the ESP header or the packet will be dropped. # 8 (UDP header) + 18 (payload) + 2 (ESP trailer) = 28, dropped # 8 (UDP header) + 19 (payload) + 4 (ESP trailer) = 32, received # There is a similar bug in IPv4 encap, but the minimum is only 12 bytes, # which is much less likely to occur. This doesn't affect tunnel mode # because IP headers are always at least 20 bytes long.
data = 19 * b"a"
datalen = len(data)
data += xfrm_base.GetEspTrailer(len(data), IPPROTO_UDP)

and indeed reducing the 19 to 18 results in the test failing. (I'm guessing 'encap' in the comment really should be 'decap'...)

I guess this means short IPv6/ESP transport/UDP fail?

I'll dig a little deeper later...

maintainer
#7 lore raw

PATCH 1/1 xfrm: Silence warnings triggerable by bad packets

In reply to Maciej Żenczykowski
Quoted 7 lines · Maciej Żenczykowski

Steffan, this isn't of course a patch meant for inclusion, instead just a WARN_ON hit report. The patch is simply what prints the following extra info:

xfrm_prepare_input: XFRM_MODE_SKB_CB(skb)->protocol: 17 xfrm_inner_mode_encap_remove: x->props.mode: 1 XFRM_MODE_SKB_SB(skb)->protocol:17

(note: XFRM_MODE_TUNNEL=1 IPPROTO_UDP=17)

Thanks for the report. This patch should fix the warnings:

---8<---
After the elimination of inner modes, a couple of warnings that were previously unreachable can now be triggered by malformed inbound packets.

Fix this by:

1. Moving the setting of skb->protocol into the decap functions.
2. Returning -EINVAL when unexpected protocol is seen.

Fixes 5f24f41e8ea6 ("xfrm: Remove inner/outer modes from input path")
net/xfrm/xfrm_input.c +9 -13
index 815b38080401..d5ee96789d4b 100644
@@ -180,6 +180,8 @@ static int xfrm4_remove_beet_encap(struct xfrm_state *x, struct sk_buff *skb)
 	int optlen = 0;
 	int err = -EINVAL;
 
+	skb->protocol = htons(ETH_P_IP);
+
 	if (unlikely(XFRM_MODE_SKB_CB(skb)->protocol == IPPROTO_BEETPH)) {
 		struct ip_beet_phdr *ph;
 		int phlen;
@@ -232,6 +234,8 @@ static int xfrm4_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb)
 {
 	int err = -EINVAL;
 
+	skb->protocol = htons(ETH_P_IP);
+
 	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
 		goto out;
 
@@ -267,6 +271,8 @@ static int xfrm6_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb)
 {
 	int err = -EINVAL;
 
+	skb->protocol = htons(ETH_P_IPV6);
+
 	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
 		goto out;
 
@@ -296,6 +302,8 @@ static int xfrm6_remove_beet_encap(struct xfrm_state *x, struct sk_buff *skb)
 	int size = sizeof(struct ipv6hdr);
 	int err;
 
+	skb->protocol = htons(ETH_P_IPV6);
+
 	err = skb_cow_head(skb, size + skb->mac_len);
 	if (err)
 		goto out;
@@ -346,6 +354,7 @@ xfrm_inner_mode_encap_remove(struct xfrm_state *x,
 			return xfrm6_remove_tunnel_encap(x, skb);
 		break;
 		}
+		return -EINVAL;
 	}
 
 	WARN_ON_ONCE(1);
@@ -366,19 +375,6 @@ static int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
 		return -EAFNOSUPPORT;
 	}
 
-	switch (XFRM_MODE_SKB_CB(skb)->protocol) {
-	case IPPROTO_IPIP:
-	case IPPROTO_BEETPH:
-		skb->protocol = htons(ETH_P_IP);
-		break;
-	case IPPROTO_IPV6:
-		skb->protocol = htons(ETH_P_IPV6);
-		break;
-	default:
-		WARN_ON_ONCE(1);
-		break;
-	}
-
 	return xfrm_inner_mode_encap_remove(x, skb);
 }
 
Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
author Reviewed-by on v1 1/1
#8 lore raw

Re: [PATCH] xfrm: Silence warnings triggerable by bad packets

Quoted 83 lines · Herbert Xu
Quoted 7 lines · Maciej Żenczykowski

Steffan, this isn't of course a patch meant for inclusion, instead just a WARN_ON hit report. The patch is simply what prints the following extra info:

xfrm_prepare_input: XFRM_MODE_SKB_CB(skb)->protocol: 17 xfrm_inner_mode_encap_remove: x->props.mode: 1 XFRM_MODE_SKB_SB(skb)->protocol:17

(note: XFRM_MODE_TUNNEL=1 IPPROTO_UDP=17)

Thanks for the report. This patch should fix the warnings:

---8<---
After the elimination of inner modes, a couple of warnings that were previously unreachable can now be triggered by malformed inbound packets.

Fix this by:

1. Moving the setting of skb->protocol into the decap functions.
2. Returning -EINVAL when unexpected protocol is seen.

Fixes 5f24f41e8ea6 ("xfrm: Remove inner/outer modes from input path")
net/xfrm/xfrm_input.c +9 -5
index 815b38080401..d5ee96789d4b 100644
@@ -180,6 +180,8 @@ static int xfrm4_remove_beet_encap(struct xfrm_state *x, struct sk_buff *skb)
        int optlen = 0;
        int err = -EINVAL;
 
+       skb->protocol = htons(ETH_P_IP);
+
        if (unlikely(XFRM_MODE_SKB_CB(skb)->protocol == IPPROTO_BEETPH)) {
                struct ip_beet_phdr *ph;
                int phlen;
@@ -232,6 +234,8 @@ static int xfrm4_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb)
 {
        int err = -EINVAL;
 
+       skb->protocol = htons(ETH_P_IP);
+
        if (!pskb_may_pull(skb, sizeof(struct iphdr)))
                goto out;
 
@@ -267,6 +271,8 @@ static int xfrm6_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb)
 {
        int err = -EINVAL;
 
+       skb->protocol = htons(ETH_P_IPV6);
+
        if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
                goto out;
 
@@ -296,6 +302,8 @@ static int xfrm6_remove_beet_encap(struct xfrm_state *x, struct sk_buff *skb)
        int size = sizeof(struct ipv6hdr);
        int err;
 
+       skb->protocol = htons(ETH_P_IPV6);
+
        err = skb_cow_head(skb, size + skb->mac_len);
        if (err)
                goto out;
@@ -346,6 +354,7 @@ xfrm_inner_mode_encap_remove(struct xfrm_state *x,
                        return xfrm6_remove_tunnel_encap(x, skb);
                break;
                }
+               return -EINVAL;
        }
 
        WARN_ON_ONCE(1);
@@ -366,19 +375,6 @@ static int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb)
                return -EAFNOSUPPORT;
        }
 
-       switch (XFRM_MODE_SKB_CB(skb)->protocol) {
-       case IPPROTO_IPIP:
-       case IPPROTO_BEETPH:
-               skb->protocol = htons(ETH_P_IP);
-               break;

net/xfrm/xfrm_input.c static int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb) · line 379

Quoted 10 lines
net/xfrm/xfrm_input.c +0 -8
-       case IPPROTO_IPV6:
-               skb->protocol = htons(ETH_P_IPV6);
-               break;
-       default:
-               WARN_ON_ONCE(1);
-               break;
-       }
-
        return xfrm_inner_mode_encap_remove(x, skb);
 }
maintainer Applied
#9 lore raw

Re: [PATCH] xfrm: Silence warnings triggerable by bad packets

In reply to Herbert Xu
Quoted 16 lines · Herbert Xu
Quoted 7 lines · Maciej Żenczykowski

Steffan, this isn't of course a patch meant for inclusion, instead just a WARN_ON hit report. The patch is simply what prints the following extra info:

xfrm_prepare_input: XFRM_MODE_SKB_CB(skb)->protocol: 17 xfrm_inner_mode_encap_remove: x->props.mode: 1 XFRM_MODE_SKB_SB(skb)->protocol:17

(note: XFRM_MODE_TUNNEL=1 IPPROTO_UDP=17)

Thanks for the report. This patch should fix the warnings:

---8<---
After the elimination of inner modes, a couple of warnings that were previously unreachable can now be triggered by malformed inbound packets.

Quoted 8 lines

Fix this by:

1. Moving the setting of skb->protocol into the decap functions.
2. Returning -EINVAL when unexpected protocol is seen.

Fixes 5f24f41e8ea6 ("xfrm: Remove inner/outer modes from input path")

Applied, thanks a lot Herbert!