Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BC78EC43218 for ; Fri, 26 Apr 2019 17:10:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8110B212F5 for ; Fri, 26 Apr 2019 17:10:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="kf2qppcl" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726373AbfDZRKL (ORCPT ); Fri, 26 Apr 2019 13:10:11 -0400 Received: from mail-ot1-f73.google.com ([209.85.210.73]:38597 "EHLO mail-ot1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726184AbfDZRKL (ORCPT ); Fri, 26 Apr 2019 13:10:11 -0400 Received: by mail-ot1-f73.google.com with SMTP id b5so1090020otq.5 for ; Fri, 26 Apr 2019 10:10:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc :content-transfer-encoding; bh=c6Y0zBiQLEzh8eI/NXJKhiVocHAl0AGkqUw19W5Sca0=; b=kf2qppcl9I3bSldj88VEjDLcNJp5ylAYfvef3NyaDeL3TIWvDfJJlVWgp18E0Vqvxx YuHhIAIKd7cuTzPY899rh+qTt30eJKiAzbS+uzgsZjXLKNYH++wp6bMCRt6V1rm5HXDR WQX0ly3rvdIcQY/tpBX/uWCQ5qT3iTMdX6hsauinxcpHm/AoWJIVApyK41lUck/0ojev NzZ3ZJ+ER+HqQ1IXS+kPegv4vqe7V7VeFESjDAHoWwNNHZdH/ArsWtTyFSlyFbJ/SGUJ 79loGvzKfzGxR6oZ24vXz6tEc/CpNvt2KNVFVFZSN5wPXtC+x6uIL5A4+o12rvT4QQwG 1r8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc :content-transfer-encoding; bh=c6Y0zBiQLEzh8eI/NXJKhiVocHAl0AGkqUw19W5Sca0=; b=gA0kW0wG00/mVkSjVHjVleY5AMk6AqdAwjs7QKGOwGEPGh5blQWduH0ll6n8lc5GxX UdICZ/kByTLVOPkqO1whfl5x/zfNM670I6We0KiOLzt78oJAd7B3LcLqEZbUa2kDaYM2 P7oEMNl0pqW+M5f5XOjtehI7hqVwDrPQxJY/WN850aSfAubThnKswi3pQ5Wg3VBKqQOn ez2D8JJKGl5M+CrV7SFI9XLomFESLVftZHIXhMMlnDnzJsR0x8aXV9hNGnP6IGIc6SNy Wz9bpls0jBMzd0aTEGbNl8gEVJWwFNcA8BH9pjhdPaLaiSxnl0jwM1NyVbGBcdmhHQXj Y04g== X-Gm-Message-State: APjAAAUkc/Z55YnrmP9YS6ePfZQOv/DX7NoZRf5tR/t6cpew3pmZJ/Or hWp/uDouhJn0PlIozuylvV/8LpC2CeA8GQ== X-Google-Smtp-Source: APXvYqwCj0SwRH1qtILjWyu2PDqIbeVEt3Rd8J2oEUWN0p3JLA+BDhSv/cMBwpA0CdSmNom9KSiNVH5fIPqRTQ== X-Received: by 2002:aca:6289:: with SMTP id w131mr7656348oib.84.1556298609232; Fri, 26 Apr 2019 10:10:09 -0700 (PDT) Date: Fri, 26 Apr 2019 10:10:05 -0700 Message-Id: <20190426171005.172805-1-edumazet@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.21.0.593.g511ec345e18-goog Subject: [PATCH net] tcp: add sanity tests in tcp_add_backlog() From: Eric Dumazet To: "David S . Miller" Cc: netdev , Eric Dumazet , Eric Dumazet , Richard Purdie , "=?UTF-8?q?Bruno=20Pr=C3=A9mont?=" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Richard and Bruno both reported that my commit added a bug, and Bruno was able to determine the problem came when a segment wih a FIN packet was coalesced to a prior one in tcp backlog queue. It turns out the header prediction in tcp_rcv_established() looks back to TCP headers in the packet, not in the metadata (aka TCP_SKB_CB(skb)->tcp_flags) The fast path in tcp_rcv_established() is not supposed to handle a FIN flag (it does not call tcp_fin()) Therefore we need to make sure to propagate the FIN flag, so that the coalesced packet does not go through the fast path, the same than a GRO packet carrying a FIN flag. While we are at it, make sure we do not coalesce packets with RST or SYN, or if they do not have ACK set. Many thanks to Richard and Bruno for pinpointing the bad commit, and to Richard for providing a first version of the fix. Fixes: 4f693b55c3d2 ("tcp: implement coalescing on backlog queue") Signed-off-by: Eric Dumazet Reported-by: Richard Purdie Reported-by: Bruno Pr=C3=A9mont --- net/ipv4/tcp_ipv4.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c index 2f8039a26b08fa2b13b5e4da642c0f4ff8207571..a2896944aa377b7feef64177203= 48c02c3d8eecb 100644 --- a/net/ipv4/tcp_ipv4.c +++ b/net/ipv4/tcp_ipv4.c @@ -1673,7 +1673,9 @@ bool tcp_add_backlog(struct sock *sk, struct sk_buff = *skb) if (TCP_SKB_CB(tail)->end_seq !=3D TCP_SKB_CB(skb)->seq || TCP_SKB_CB(tail)->ip_dsfield !=3D TCP_SKB_CB(skb)->ip_dsfield || ((TCP_SKB_CB(tail)->tcp_flags | - TCP_SKB_CB(skb)->tcp_flags) & TCPHDR_URG) || + TCP_SKB_CB(skb)->tcp_flags) & (TCPHDR_SYN | TCPHDR_RST | TCPHDR_URG= )) || + !((TCP_SKB_CB(tail)->tcp_flags & + TCP_SKB_CB(skb)->tcp_flags) & TCPHDR_ACK) || ((TCP_SKB_CB(tail)->tcp_flags ^ TCP_SKB_CB(skb)->tcp_flags) & (TCPHDR_ECE | TCPHDR_CWR)) || #ifdef CONFIG_TLS_DEVICE @@ -1692,6 +1694,15 @@ bool tcp_add_backlog(struct sock *sk, struct sk_buff= *skb) if (after(TCP_SKB_CB(skb)->ack_seq, TCP_SKB_CB(tail)->ack_seq)) TCP_SKB_CB(tail)->ack_seq =3D TCP_SKB_CB(skb)->ack_seq; =20 + /* We have to update both TCP_SKB_CB(tail)->tcp_flags and + * thtail->fin, so that the fast path in tcp_rcv_established() + * is not entered if we append a packet with a FIN. + * SYN, RST, URG are not present. + * ACK is set on both packets. + * PSH : we do not really care in TCP stack, + * at least for 'GRO' packets. + */ + thtail->fin |=3D th->fin; TCP_SKB_CB(tail)->tcp_flags |=3D TCP_SKB_CB(skb)->tcp_flags; =20 if (TCP_SKB_CB(skb)->has_rxtstamp) { --=20 2.21.0.593.g511ec345e18-goog