xfrm: Fix off by one in the replay advance functions
Subsystems (4)
Steffen Klassert
maintainerPATCH 1/3 xfrm: Fix off by one in the replay advance functions
We may write 4 byte too much when we reinitialize the anti replay window in the replay advance functions. This patch fixes this by adjusting the last index of the initialization loop.
--- net/xfrm/xfrm_replay.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
net/xfrm/xfrm_replay.c +2 -2
@@ -265,7 +265,7 @@ static void xfrm_replay_advance_bmp(struct xfrm_state *x, __be32 net_seq) bitnr = bitnr &0x1F ; replay_esn->bmp[nr] |= (1U << bitnr); }else { - nr = replay_esn->replay_window >>5 ; + nr = (replay_esn->replay_window -1 ) >>5 ;for (i =0 ; i <= nr; i++) replay_esn->bmp[i] =0 ;
@@ -471,7 +471,7 @@ static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq) bitnr = bitnr &0x1F ; replay_esn->bmp[nr] |= (1U << bitnr); }else { - nr = replay_esn->replay_window >>5 ; + nr = (replay_esn->replay_window -1 ) >>5 ;for (i =0 ; i <= nr; i++) replay_esn->bmp[i] =0 ;
Signature · 1 line
1.7.0.4
Steffen Klassert
maintainerPATCH 2/3 ipv4: Fix packet size calculation for IPsec packets in __ip_append_data
Git commit 59104f06 (ip: take care of last fragment in ip_append_data) added a check to see if we exceed the mtu when we add trailer_len. However, the mtu is already subtracted by trailer_len when the xfrm transfomation bundles are set up. So IPsec packets with mtu size get fragmented, or if the DF bit is set the packets will not be send even though they match the mtu perfectly fine. This patch actually reverts commit 59104f06.
--- net/ipv4/ip_output.c | 7 ++----- 1 files changed, 2 insertions(+), 5 deletions(-)
net/ipv4/ip_output.c +2 -5
@@ -888,12 +888,9 @@ alloc_new_skb:* because we have no idea what fragment will be * the last. */ -if (datalen == length + fraggap) { +if (datalen == length + fraggap) alloclen += rt->dst.trailer_len; -/* make sure mtu is not reached */ -if (datalen > mtu - fragheaderlen - rt->dst.trailer_len) - datalen -=ALIGN (rt->dst.trailer_len,8 ); - } +if (transhdrlen) { skb = sock_alloc_send_skb(sk, alloclen + hh_len + 15,
Signature · 1 line
1.7.0.4
Re: [PATCH 2/3] ipv4: Fix packet size calculation for IPsec packets in __ip_append_data
Quoted 10 lines · Steffen Klassert
Git commit
59104f06(ip: take care of last fragment inip_append_data) added a check to see if we exceed the mtu when we addtrailer_len. However, the mtu is already subtracted bytrailer_lenwhen the xfrm transfomation bundles are set up. So IPsec packets with mtu size get fragmented, or if the DF bit is set the packets will not be send even though they match the mtu perfectly fine. This patch actually reverts commit59104f06.Signed-off-by Steffen Klassert <[email protected]>---
Woh, I am afraid I wont have time in following days to check your assertion.
What about original problem then, how should we fix it ?
We do have some cases where at least one fragment (the last one) is oversized.
I remember I used Nick Bowler scripts at that time, I might find them again...
[PATCH] ip : take care of last fragment in ip_append_data
While investigating a bit, I found ip_fragment() slow path was taken because ip_append_data() provides following layout for a send(MTU + N*(MTU - 20)) syscall :
- one skb with 1500 (mtu) bytes
- N fragments of 1480 (mtu-20) bytes (before adding IP header) last fragment gets 17 bytes of trail data because of following bit:
if (datalen == length + fraggap)
alloclen += rt->dst.trailer_len;Then esp4 adds 16 bytes of data (while trailer_len is 17... hmm... another bug ?)
In ip_fragment(), we notice last fragment is too big (1496 + 20) > mtu, so we take slow path, building another skb chain.
In order to avoid taking slow path, we should correct ip_append_data() to make sure last fragment has real trail space, under mtu...
Steffen Klassert
maintainerRe: [PATCH 2/3] ipv4: Fix packet size calculation for IPsec packets in __ip_append_data
Quoted 3 lines · Eric Dumazet
Woh, I am afraid I wont have time in following days to check your assertion.
My test setup was the following:
I use an IPsec tunnel with tunnel endpoints 192.168.1.1 and 192.168.1.2
Then I do at 192.168.1.2
ping -c1 -M do -s 1410 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 1410(1438) bytes of data.
Quoted 1 line
From 192.168.1.2 icmp_seq=1 Frag needed and DF set (mtu = 1438)
--- 192.168.1.1 ping statistics ---
0 packets transmitted, 0 received, +1 errors
So the packet matches the mtu but it is not send.
I used a kernel with your patch as head commit.
Reverting your patch (going one commit deeper in the history):
ping -c1 -M do -s 1410 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 1410(1438) bytes of data. 1418 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=3.01 ms
--- 192.168.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 3.014/3.014/3.014/0.000 ms
Quoted 3 lines
What about original problem then, how should we fix it ?
Hm, I don't know. I'll try to reproduce it here.
Quoted 2 lines
We do have some cases where at least one fragment (the last one) is oversized.
trailer_len is used only on IPsec so the poroblem exists only when using IPsec, right?
Quoted 3 lines
I remember I used Nick Bowler scripts at that time, I might find them again...
Would be nice if you could provide these scripts and some informations on how to reproduce the problem.
Re: [PATCH 2/3] ipv4: Fix packet size calculation for IPsec packets in __ip_append_data
Quoted 46 lines · Steffen Klassert
Quoted 3 lines · Eric Dumazet
Woh, I am afraid I wont have time in following days to check your assertion.
My test setup was the following:
I use an IPsec tunnel with tunnel endpoints 192.168.1.1 and 192.168.1.2
Then I do at 192.168.1.2
ping -c1 -M do -s 1410 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 1410(1438) bytes of data. From 192.168.1.2 icmp_seq=1 Frag needed and DF set (mtu = 1438)
--- 192.168.1.1 ping statistics ---
0 packets transmitted, 0 received, +1 errorsSo the packet matches the mtu but it is not send.
I used a kernel with your patch as head commit.Reverting your patch (going one commit deeper in the history):
ping -c1 -M do -s 1410 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 1410(1438) bytes of data. 1418 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=3.01 ms
--- 192.168.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 3.014/3.014/3.014/0.000 msQuoted 3 lines
What about original problem then, how should we fix it ?
Hm, I don't know. I'll try to reproduce it here.
Quoted 2 lines
We do have some cases where at least one fragment (the last one) is oversized.
trailer_lenis used only on IPsec so the poroblem exists only when using IPsec, right?Quoted 1 line
Quoted 6 lines
Quoted 2 lines
I remember I used Nick Bowler scripts at that time, I might find them again...
Would be nice if you could provide these scripts and some informations on how to reproduce the problem.
Nick mail was :
http://www.spinics.net/lists/netdev/msg141308.html
Unfortunatly I could not find on my machines where I put my own scripts...
Not a big deal, I suspect we can revert my commit if you say it added a regression :)
Thanks
Steffen Klassert
maintainerRe: [PATCH 2/3] ipv4: Fix packet size calculation for IPsec packets in __ip_append_data
Quoted 5 lines · Eric Dumazet
Nick mail was :
Thanks for providing these informations.
Quoted 6 lines
Unfortunatly I could not find on my machines where I put my own scripts...
Not a big deal, I suspect we can revert my commit if you say it added a regression :)
In between I can confirm that we get the slowpath problem back with my patch, so we still have a bug somewhere. Reverting your commit would be just a band aid. I think it is better to find the bug and do a real fix instead. Unfortunatly I fear I'm not able to track it down before my vacation that starts tomorrow. I'll continue to work at it once I'm back...
David Miller
maintainerRe: [PATCH 2/3] ipv4: Fix packet size calculation for IPsec packets in __ip_append_data
From: Steffen Klassert <[email protected]>
Date: Wed, 8 Jun 2011 07:30:20 +0200
Quoted 6 lines
In between I can confirm that we get the slowpath problem back with my patch, so we still have a bug somewhere. Reverting your commit would be just a band aid. I think it is better to find the bug and do a real fix instead. Unfortunatly I fear I'm not able to track it down before my vacation that starts tomorrow. I'll continue to work at it once I'm back...
Thanks Steffen, looking forward to this.
Steffen Klassert
maintainerRe: [PATCH 2/3] ipv4: Fix packet size calculation for IPsec packets in __ip_append_data
Quoted 11 lines · David Miller
From: Steffen Klassert <[email protected]>
Date: Wed, 8 Jun 2011 07:30:20 +0200Quoted 6 lines
In between I can confirm that we get the slowpath problem back with my patch, so we still have a bug somewhere. Reverting your commit would be just a band aid. I think it is better to find the bug and do a real fix instead. Unfortunatly I fear I'm not able to track it down before my vacation that starts tomorrow. I'll continue to work at it once I'm back...
Thanks Steffen, looking forward to this.
In between I found the problem. In ip_setup_cork() we take the mtu on the base of dst_mtu(rt->dst.path) and assign it to cork->fragsize which is used as the mtu in __ip_append_data(). The path dst_entry is a routing dst_entry that does not take the IPsec header and trailer overhead into account. So if we build an IPsec packet based on this mtu it might be to big after the IPsec transformations are applied. If we take the actual IPsec mtu from dst_mtu(&rt->dst) and adapt the exthdr handling to this, it works as expected. So I'll send two patches, one that reverts Eric's patch and one that fixes the slowpath issue.
While reading through the code of __ip_append_data() I noticed that we might use ip_ufo_append_data() for packets that will be IPsec transformed later, is this ok? I don't know how ufo handling works, but I would guess that it expects an udp header and not an IPsec header as the packets transport header.
I found another funny thing when I tested my patches:
I use an IPsec tunnel with tunnel endpoints 192.168.1.1 and 192.168.1.2
Then I do at 192.168.1.2
ping -c20 -M do -s 1500 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 1500(1528) bytes of data.
Quoted 12 lines
From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 1438) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 1438) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 1374) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 1310) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 1246) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 1182) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 1118) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 1054) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 990) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 926) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 862) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 798)
Quoted 8 lines
From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 734) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 670) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 606) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 552) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 494) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 494) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 494) From 192.168.1.1 icmp_seq=1 Frag needed and DF set (mtu = 494)
--- 192.168.1.1 ping statistics ---
0 packets transmitted, 0 received, +20 errors
These packets are locally generated and not from 192.168.1.1 as they claim to be. I think the problem is the following:
The IPsec mtu is 1438 here, so the first packet is too big. xfrm4_tunnel_check_size() notices this and sends a ICMP_FRAG_NEEDED packet that announces a mtu of 1438 to the original sender of the ping packet. Unfortunately the sender is a local address, it's the IPsec tunnel entry point. So we update the mtu for this connection to 1438. Now, with the next packet xfrm_bundle_ok() notices that the path mtu has changed, so it subtracts the IPsec overhead from the mtu a second time and we end up with a mtu of 1374. This game goes until we reach a minimal mtu of 494.
Unfortunately I don't know how to fix this. Any ideas?
David Miller
maintainerRe: [PATCH 2/3] ipv4: Fix packet size calculation for IPsec packets in __ip_append_data
From: Steffen Klassert <[email protected]>
Date: Wed, 22 Jun 2011 13:02:19 +0200
Quoted 9 lines
In between I found the problem. In
ip_setup_cork()we take the mtu on the base ofdst_mtu(rt->dst.path)and assign it tocork->fragsizewhich is used as the mtu in__ip_append_data(). The pathdst_entryis a routingdst_entrythat does not take the IPsec header and trailer overhead into account. So if we build an IPsec packet based on this mtu it might be to big after the IPsec transformations are applied. If we take the actual IPsec mtu fromdst_mtu(&rt->dst)and adapt the exthdr handling to this, it works as expected. So I'll send two patches, one that reverts Eric's patch and one that fixes the slowpath issue.
Thanks for doing this work Steffen.
Quoted 5 lines
While reading through the code of
__ip_append_data()I noticed that we might useip_ufo_append_data()for packets that will be IPsec transformed later, is this ok? I don't know how ufo handling works, but I would guess that it expects an udp header and not an IPsec header as the packets transport header.
Indeed, it could be a real problem.
Quoted 11 lines
The IPsec mtu is 1438 here, so the first packet is too big.
xfrm4_tunnel_check_size()notices this and sends aICMP_FRAG_NEEDEDpacket that announces a mtu of 1438 to the original sender of the ping packet. Unfortunately the sender is a local address, it's the IPsec tunnel entry point. So we update the mtu for this connection to 1438. Now, with the next packetxfrm_bundle_ok()notices that the path mtu has changed, so it subtracts the IPsec overhead from the mtu a second time and we end up with a mtu of 1374. This game goes until we reach a minimal mtu of 494.Unfortunately I don't know how to fix this. Any ideas?
If the generic PMTU handling in net/ipv4/route.c is adjusting the MTU for the IPSEC path's route, that would be the problem.
In the case of IPSEC encapsulation, tunnels, and similar, only the tunnel or the XFRM layer should be processing the PMTU notification.
Steffen Klassert
maintainerRe: [PATCH 2/3] ipv4: Fix packet size calculation for IPsec packets in __ip_append_data
Quoted 10 lines · David Miller
From: Steffen Klassert <[email protected]>
Date: Wed, 22 Jun 2011 13:02:19 +0200Quoted 5 lines
While reading through the code of
__ip_append_data()I noticed that we might useip_ufo_append_data()for packets that will be IPsec transformed later, is this ok? I don't know how ufo handling works, but I would guess that it expects an udp header and not an IPsec header as the packets transport header.Indeed, it could be a real problem.
Ok, so I'll send a patch to fix it up.
Quoted 11 lines
Quoted 11 lines
The IPsec mtu is 1438 here, so the first packet is too big.
xfrm4_tunnel_check_size()notices this and sends aICMP_FRAG_NEEDEDpacket that announces a mtu of 1438 to the original sender of the ping packet. Unfortunately the sender is a local address, it's the IPsec tunnel entry point. So we update the mtu for this connection to 1438. Now, with the next packetxfrm_bundle_ok()notices that the path mtu has changed, so it subtracts the IPsec overhead from the mtu a second time and we end up with a mtu of 1374. This game goes until we reach a minimal mtu of 494.
Quoted 5 lines
Quoted 1 line
Unfortunately I don't know how to fix this. Any ideas?
If the generic PMTU handling in
net/ipv4/route.cis adjusting the MTU for the IPSEC path's route, that would be the problem.
Yes, this is exactly what happens. We use icmp_send() to notify about message size errors even for locally generated packets, this leads to an incorrect pmtu update. Changing this to use ip_local_error() if we have socket context fixes the problem. I'll send a patch for this too.
Steffen Klassert
maintainerPATCH 3/3 ipv4: Fix packet size calculation for raw IPsec packets in __ip_append_data
In reply to Steffen KlassertWe assume that transhdrlen is positive on the first fragment which is wrong for raw packets. So we don't add exthdrlen to the packet size for raw packets. This leads to a reallocation on IPsec because we have not enough headroom on the skb to place the IPsec headers. This patch fixes this by adding exthdrlen to the packet size whenever the send queue of the socket is empty. This issue was introduced with git commit 1470ddf7 (inet: Remove explicit write references to sk/inet in ip_append_data)
--- net/ipv4/ip_output.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
net/ipv4/ip_output.c +3 -3
@@ -799,7 +799,9 @@ static int __ip_append_data(struct sock *sk,int csummode =CHECKSUM_NONE ;struct rtable *rt = (struct rtable *)cork->dst; - exthdrlen = transhdrlen ? rt->dst.header_len :0 ; + skb =skb_peek_tail (queue); + + exthdrlen = !skb ? rt->dst.header_len :0 ; length += exthdrlen; transhdrlen += exthdrlen; mtu = cork->fragsize;
@@ -825,8 +827,6 @@ static int __ip_append_data(struct sock *sk, !exthdrlen) csummode =CHECKSUM_PARTIAL ; - skb =skb_peek_tail (queue); - cork->length += length; if (((length > mtu) || (skb && skb_is_gso(skb))) && (sk->sk_protocol == IPPROTO_UDP) &&
Signature · 1 line
1.7.0.4
Re: [PATCH 3/3] ipv4: Fix packet size calculation for raw IPsec packets in __ip_append_data
From: Steffen Klassert <[email protected]>
Date: Mon, 6 Jun 2011 08:48:47 +0200
Quoted 10 lines
We assume that transhdrlen is positive on the first fragment which is wrong for raw packets. So we don't add exthdrlen to the packet size for raw packets. This leads to a reallocation on IPsec because we have not enough headroom on the skb to place the IPsec headers. This patch fixes this by adding exthdrlen to the packet size whenever the send queue of the socket is empty. This issue was introduced with git commit
1470ddf7(inet: Remove explicit write references to sk/inet inip_append_data)Signed-off-by Steffen Klassert <[email protected]>
Applied, thanks.
From: Steffen Klassert <[email protected]>
Date: Mon, 6 Jun 2011 08:46:03 +0200
Quoted 5 lines
We may write 4 byte too much when we reinitialize the anti replay window in the replay advance functions. This patch fixes this by adjusting the last index of the initialization loop.
Signed-off-by Steffen Klassert <[email protected]>
Applied, thanks.