Skip to content

Commit 72cd43b

Browse files
edumazetdavem330
authored andcommitted
tcp: free batches of packets in tcp_prune_ofo_queue()
Juha-Matti Tilli reported that malicious peers could inject tiny packets in out_of_order_queue, forcing very expensive calls to tcp_collapse_ofo_queue() and tcp_prune_ofo_queue() for every incoming packet. out_of_order_queue rb-tree can contain thousands of nodes, iterating over all of them is not nice. Before linux-4.9, we would have pruned all packets in ofo_queue in one go, every XXXX packets. XXXX depends on sk_rcvbuf and skbs truesize, but is about 7000 packets with tcp_rmem[2] default of 6 MB. Since we plan to increase tcp_rmem[2] in the future to cope with modern BDP, can not revert to the old behavior, without great pain. Strategy taken in this patch is to purge ~12.5 % of the queue capacity. Fixes: 36a6503 ("tcp: refine tcp_prune_ofo_queue() to not drop all packets") Signed-off-by: Eric Dumazet <[email protected]> Reported-by: Juha-Matti Tilli <[email protected]> Acked-by: Yuchung Cheng <[email protected]> Acked-by: Soheil Hassas Yeganeh <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 3dd1c9a commit 72cd43b

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

net/ipv4/tcp_input.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4942,27 +4942,34 @@ static void tcp_collapse_ofo_queue(struct sock *sk)
49424942
* 2) not add too big latencies if thousands of packets sit there.
49434943
* (But if application shrinks SO_RCVBUF, we could still end up
49444944
* freeing whole queue here)
4945+
* 3) Drop at least 12.5 % of sk_rcvbuf to avoid malicious attacks.
49454946
*
49464947
* Return true if queue has shrunk.
49474948
*/
49484949
static bool tcp_prune_ofo_queue(struct sock *sk)
49494950
{
49504951
struct tcp_sock *tp = tcp_sk(sk);
49514952
struct rb_node *node, *prev;
4953+
int goal;
49524954

49534955
if (RB_EMPTY_ROOT(&tp->out_of_order_queue))
49544956
return false;
49554957

49564958
NET_INC_STATS(sock_net(sk), LINUX_MIB_OFOPRUNED);
4959+
goal = sk->sk_rcvbuf >> 3;
49574960
node = &tp->ooo_last_skb->rbnode;
49584961
do {
49594962
prev = rb_prev(node);
49604963
rb_erase(node, &tp->out_of_order_queue);
4964+
goal -= rb_to_skb(node)->truesize;
49614965
tcp_drop(sk, rb_to_skb(node));
4962-
sk_mem_reclaim(sk);
4963-
if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
4964-
!tcp_under_memory_pressure(sk))
4965-
break;
4966+
if (!prev || goal <= 0) {
4967+
sk_mem_reclaim(sk);
4968+
if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
4969+
!tcp_under_memory_pressure(sk))
4970+
break;
4971+
goal = sk->sk_rcvbuf >> 3;
4972+
}
49664973
node = prev;
49674974
} while (node);
49684975
tp->ooo_last_skb = rb_to_skb(prev);

0 commit comments

Comments
 (0)