netfilter: conntrack: move synack init code to helper
Subsystems (2)
Florian Westphal
maintainerPATCH 1/2 netfilter: conntrack: move synack init code to helper
It seems more readable to use a common helper in the followup fix rather than copypaste or goto.
No functional change intended. The function is only called for syn-ack or syn in repy direction in case of simultaneous open.
--- net/netfilter/nf_conntrack_proto_tcp.c | 47 ++++++++++++++++---------- 1 file changed, 29 insertions(+), 18 deletions(-)
net/netfilter/nf_conntrack_proto_tcp.c +29 -18
@@ -446,6 +446,32 @@ static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff, } } +static void tcp_init_sender (struct ip_ct_tcp_state *sender, +struct ip_ct_tcp_state *receiver, +const struct sk_buff *skb, +unsigned int dataoff, +const struct tcphdr *tcph, +u32 end,u32 win) +{ +/* SYN-ACK in reply to a SYN +* or SYN from reply direction in simultaneous open. +*/ + sender->td_end = + sender->td_maxend = end; + sender->td_maxwin = (win ==0 ?1 : win); + +tcp_options (skb, dataoff, tcph, sender); +/* RFC 1323: +* Both sides must send the Window Scale option +* to enable window scaling in either direction. +*/ +if (!(sender->flags &IP_CT_TCP_FLAG_WINDOW_SCALE && + receiver->flags &IP_CT_TCP_FLAG_WINDOW_SCALE )) { + sender->td_scale =0 ; + receiver->td_scale =0 ; + } +} + static bool tcp_in_window(struct nf_conn *ct, enum ip_conntrack_dir dir, unsigned int index,
@@ -499,24 +525,9 @@ static bool tcp_in_window(struct nf_conn *ct,* Initialize sender data. */ if (tcph->syn) { -/* -* SYN-ACK in reply to a SYN -* or SYN from reply direction in simultaneous open. -*/ - sender->td_end = - sender->td_maxend = end; - sender->td_maxwin = (win ==0 ?1 : win); - -tcp_options (skb, dataoff, tcph, sender); -/* -* RFC 1323: -* Both sides must send the Window Scale option -* to enable window scaling in either direction. -*/ -if (!(sender->flags &IP_CT_TCP_FLAG_WINDOW_SCALE - && receiver->flags &IP_CT_TCP_FLAG_WINDOW_SCALE )) - sender->td_scale = - receiver->td_scale =0 ; +tcp_init_sender (sender, receiver, + skb, dataoff, tcph, + end, win);if (!tcph->ack)/* Simultaneous open */ return true;
Signature · 1 line
2.34.1
Florian Westphal
maintainerPATCH 2/2 netfilter: conntrack: re-init state for retransmitted syn-ack
TCP conntrack assumes that a syn-ack retransmit is identical to the previous syn-ack. This isn't correct and causes stuck 3whs in some more esoteric scenarios. tcpdump to illustrate the problem:
client > server: Flags [S] seq 1365731894, win 29200, [mss 1460,sackOK,TS val 2083035583 ecr 0,wscale 7] server > client: Flags [S.] seq 145824453, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215367629 ecr 2082921663]
Note the invalid/outdated synack ack number.
Conntrack marks this syn-ack as out-of-window/invalid, but it did initialize the reply direction parameters based on this packets content.
client > server: Flags [S] seq 1365731894, win 29200, [mss 1460,sackOK,TS val 2083036623 ecr 0,wscale 7]
... retransmit...
server > client: Flags [S.], seq 145824453, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215368644 ecr 2082921663]
and another bogus synack. This repeats, then client re-uses for a new attempt:
client > server: Flags [S], seq 2375731741, win 29200, [mss 1460,sackOK,TS val 2083100223 ecr 0,wscale 7] server > client: Flags [S.], seq 145824453, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215430754 ecr 2082921663]
... but still gets a invalid syn-ack.
This repeats until:
server > client: Flags [S.], seq 145824453, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215437785 ecr 2082921663] server > client: Flags [R.], seq 145824454, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215443451 ecr 2082921663] client > server: Flags [S], seq 2375731741, win 29200, [mss 1460,sackOK,TS val 2083115583 ecr 0,wscale 7] server > client: Flags [S.], seq 162602410, ack 2375731742, win 65535, [mss 8952,wscale 5,TS val 3215445754 ecr 2083115583]
This syn-ack has the correct ack number, but conntrack flags it as
invalid: The internal state was created from the first syn-ack seen so the sequence number of the syn-ack is treated as being outside of the announced window.
Don't assume that retransmitted syn-ack is identical to previous one. Treat it like the first syn-ack and reinit state.
--- net/netfilter/nf_conntrack_proto_tcp.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
net/netfilter/nf_conntrack_proto_tcp.c +12 -0
@@ -571,6 +571,18 @@ static bool tcp_in_window(struct nf_conn *ct, sender->td_maxwin = (win ==0 ?1 : win);tcp_options (skb, dataoff, tcph, sender); + }else if (tcph->syn && dir ==IP_CT_DIR_REPLY && + state->state ==TCP_CONNTRACK_SYN_SENT ) { +/* Retransmitted syn-ack, or syn (simultaneous open). +* +* Re-init state for this direction, just like for the first +* syn(-ack) reply, it might differ in seq, ack or tcp options. +*/ +tcp_init_sender (sender, receiver, + skb, dataoff, tcph, + end, win); +if (!tcph->ack) +return true; }if (!(tcph->ack)) {
Signature · 1 line
2.34.1
Quoted 8 lines · Florian Westphal
It seems more readable to use a common helper in the followup fix rather than copypaste or goto.
No functional change intended. The function is only called for syn-ack or syn in repy direction in case of simultaneous open.
Cc Jozsef Kadlecsik <[email protected]>Signed-off-by Florian Westphal <[email protected]>
Quoted 73 lines
--- net/netfilter/nf_conntrack_proto_tcp.c | 47 ++++++++++++++++---------- 1 file changed, 29 insertions(+), 18 deletions(-)net/netfilter/nf_conntrack_proto_tcp.c +29
-18@@ -446,6 +446,32 @@ static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff, } } +static void tcp_init_sender (struct ip_ct_tcp_state *sender, +struct ip_ct_tcp_state *receiver, +const struct sk_buff *skb, +unsigned int dataoff, +const struct tcphdr *tcph, +u32 end,u32 win) +{ +/* SYN-ACK in reply to a SYN +* or SYN from reply direction in simultaneous open. +*/ + sender->td_end = + sender->td_maxend = end; + sender->td_maxwin = (win ==0 ?1 : win); + +tcp_options (skb, dataoff, tcph, sender); +/* RFC 1323: +* Both sides must send the Window Scale option +* to enable window scaling in either direction. +*/ +if (!(sender->flags &IP_CT_TCP_FLAG_WINDOW_SCALE && + receiver->flags &IP_CT_TCP_FLAG_WINDOW_SCALE )) { + sender->td_scale =0 ; + receiver->td_scale =0 ; + } +} + static bool tcp_in_window(struct nf_conn *ct, enum ip_conntrack_dir dir, unsigned int index,@@ -499,24 +525,9 @@ static bool tcp_in_window(struct nf_conn *ct,* Initialize sender data. */ if (tcph->syn) { -/* -* SYN-ACK in reply to a SYN -* or SYN from reply direction in simultaneous open. -*/ - sender->td_end = - sender->td_maxend = end; - sender->td_maxwin = (win ==0 ?1 : win); - -tcp_options (skb, dataoff, tcph, sender); -/* -* RFC 1323: -* Both sides must send the Window Scale option -* to enable window scaling in either direction. -*/ -if (!(sender->flags &IP_CT_TCP_FLAG_WINDOW_SCALE - && receiver->flags &IP_CT_TCP_FLAG_WINDOW_SCALE )) - sender->td_scale = - receiver->td_scale =0 ; +tcp_init_sender (sender, receiver, + skb, dataoff, tcph, + end, win);if (!tcph->ack)/* Simultaneous open */ return true;Signature · 1 line
2.34.1
-
E-mail : [email protected], [email protected] PGP key : https://wigner.hu/~kadlec/pgp_public_key.txt
Address : Wigner Research Centre for Physics
H-1525 Budapest 114, POB. 49, Hungary
Re: [PATCH nf 2/2] netfilter: conntrack: re-init state for retransmitted syn-ack
In reply to Florian WestphalHi Florian,
Quoted 32 lines · Florian Westphal
TCP conntrack assumes that a syn-ack retransmit is identical to the previous syn-ack. This isn't correct and causes stuck 3whs in some more esoteric scenarios. tcpdump to illustrate the problem:
client > server: Flags [S] seq 1365731894, win 29200, [mss 1460,sackOK,TS val 2083035583 ecr 0,wscale 7] server > client: Flags [S.] seq 145824453, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215367629 ecr 2082921663]
Note the invalid/outdated synack ack number.
Conntrack marks this syn-ack as out-of-window/invalid, but it did initialize the reply direction parameters based on this packets content.client > server: Flags [S] seq 1365731894, win 29200, [mss 1460,sackOK,TS val 2083036623 ecr 0,wscale 7]
... retransmit...
server > client: Flags [S.], seq 145824453, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215368644 ecr 2082921663]
and another bogus synack. This repeats, then client re-uses for a new attempt:
client > server: Flags [S], seq 2375731741, win 29200, [mss 1460,sackOK,TS val 2083100223 ecr 0,wscale 7] server > client: Flags [S.], seq 145824453, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215430754 ecr 2082921663]
... but still gets a invalid syn-ack.
This repeats until:
server > client: Flags [S.], seq 145824453, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215437785 ecr 2082921663] server > client: Flags [R.], seq 145824454, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215443451 ecr 2082921663] client > server: Flags [S], seq 2375731741, win 29200, [mss 1460,sackOK,TS val 2083115583 ecr 0,wscale 7] server > client: Flags [S.], seq 162602410, ack 2375731742, win 65535, [mss 8952,wscale 5,TS val 3215445754 ecr 2083115583]
Quoted 4 lines
This syn-ack has the correct ack number, but conntrack flags it as
invalid: The internal state was created from the first syn-ack seen so the sequence number of the syn-ack is treated as being outside of the announced window.
I can only assume that the client is/are behind like a carrier-grade NAT and the bogus SYN-ACK sent by the server is replying a connection attempt from another client. Yes, the best thing to do is to reinit the state.
Quoted 5 lines
Don't assume that retransmitted syn-ack is identical to previous one. Treat it like the first syn-ack and reinit state.
Cc Jozsef Kadlecsik <[email protected]>Signed-off-by Florian Westphal <[email protected]>
Best regards,
Jozsef
Quoted 31 lines
--- net/netfilter/nf_conntrack_proto_tcp.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)net/netfilter/nf_conntrack_proto_tcp.c +12
-0@@ -571,6 +571,18 @@ static bool tcp_in_window(struct nf_conn *ct, sender->td_maxwin = (win ==0 ?1 : win);tcp_options (skb, dataoff, tcph, sender); + }else if (tcph->syn && dir ==IP_CT_DIR_REPLY && + state->state ==TCP_CONNTRACK_SYN_SENT ) { +/* Retransmitted syn-ack, or syn (simultaneous open). +* +* Re-init state for this direction, just like for the first +* syn(-ack) reply, it might differ in seq, ack or tcp options. +*/ +tcp_init_sender (sender, receiver, + skb, dataoff, tcph, + end, win); +if (!tcph->ack) +return true; }if (!(tcph->ack)) {Signature · 1 line
2.34.1
-
E-mail : [email protected], [email protected] PGP key : https://wigner.hu/~kadlec/pgp_public_key.txt
Address : Wigner Research Centre for Physics
H-1525 Budapest 114, POB. 49, Hungary
Florian Westphal
maintainerRe: [PATCH nf 2/2] netfilter: conntrack: re-init state for retransmitted syn-ack
Quoted 3 lines · Jozsef Kadlecsik
I can only assume that the client is/are behind like a carrier-grade NAT and the bogus SYN-ACK sent by the server is replying a connection attempt from another client. Yes, the best thing to do is to reinit the state.
Yes, thats my guess as well, some sort of CGN or stateless nat hding multiple clients.
Thanks for the quick review!
Quoted 5 lines · Jozsef Kadlecsik
Quoted 3 lines · Florian Westphal
It seems more readable to use a common helper in the followup fix rather than copypaste or goto.
Quoted 7 lines
Quoted 5 lines
No functional change intended. The function is only called for syn-ack or syn in repy direction in case of simultaneous open.
Cc Jozsef Kadlecsik <[email protected]>Signed-off-by Florian Westphal <[email protected]>Acked-by Jozsef Kadlecsik <[email protected]>
Applied, thanks
Re: [PATCH nf 2/2] netfilter: conntrack: re-init state for retransmitted syn-ack
In reply to Jozsef KadlecsikQuoted 45 lines · Jozsef Kadlecsik
Hi Florian,
Quoted 32 lines · Florian Westphal
TCP conntrack assumes that a syn-ack retransmit is identical to the previous syn-ack. This isn't correct and causes stuck 3whs in some more esoteric scenarios. tcpdump to illustrate the problem:
client > server: Flags [S] seq 1365731894, win 29200, [mss 1460,sackOK,TS val 2083035583 ecr 0,wscale 7] server > client: Flags [S.] seq 145824453, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215367629 ecr 2082921663]
Note the invalid/outdated synack ack number.
Conntrack marks this syn-ack as out-of-window/invalid, but it did initialize the reply direction parameters based on this packets content.client > server: Flags [S] seq 1365731894, win 29200, [mss 1460,sackOK,TS val 2083036623 ecr 0,wscale 7]
... retransmit...
server > client: Flags [S.], seq 145824453, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215368644 ecr 2082921663]
and another bogus synack. This repeats, then client re-uses for a new attempt:
client > server: Flags [S], seq 2375731741, win 29200, [mss 1460,sackOK,TS val 2083100223 ecr 0,wscale 7] server > client: Flags [S.], seq 145824453, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215430754 ecr 2082921663]
... but still gets a invalid syn-ack.
This repeats until:
server > client: Flags [S.], seq 145824453, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215437785 ecr 2082921663] server > client: Flags [R.], seq 145824454, ack 643160523, win 65535, [mss 8952,wscale 5,TS val 3215443451 ecr 2082921663] client > server: Flags [S], seq 2375731741, win 29200, [mss 1460,sackOK,TS val 2083115583 ecr 0,wscale 7] server > client: Flags [S.], seq 162602410, ack 2375731742, win 65535, [mss 8952,wscale 5,TS val 3215445754 ecr 2083115583]
Quoted 4 lines
This syn-ack has the correct ack number, but conntrack flags it as
invalid: The internal state was created from the first syn-ack seen so the sequence number of the syn-ack is treated as being outside of the announced window.I can only assume that the client is/are behind like a carrier-grade NAT and the bogus SYN-ACK sent by the server is replying a connection attempt from another client. Yes, the best thing to do is to reinit the state.
Quoted 7 lines
Quoted 5 lines
Don't assume that retransmitted syn-ack is identical to previous one. Treat it like the first syn-ack and reinit state.
Cc Jozsef Kadlecsik <[email protected]>Signed-off-by Florian Westphal <[email protected]>Acked-by Jozsef Kadlecsik <[email protected]>
Applied, thanks