Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6145071iog; Thu, 23 Jun 2022 12:16:34 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sAhMSbIyqj5VYPcNH0J7E2l02c9tHnZVMFzv0UPtA/RsMjmOun99nrQLg4x5R26XkHdwYP X-Received: by 2002:a17:902:f34a:b0:16a:e2d:3e9 with SMTP id q10-20020a170902f34a00b0016a0e2d03e9mr31307560ple.95.1656011794061; Thu, 23 Jun 2022 12:16:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011794; cv=none; d=google.com; s=arc-20160816; b=AGX+hPPEowujMHELCL1bYI2i1hFpPl7CQ3rDMlKiweTkijS+nXROsb6qTT5I9fAGm+ g1eD8uimLPfG7zu97g5phU4jhcLwxY3Qqv8rOAlPUpT5wJii9dGKPGEVfCifJHjljWcD 5X+6Bihx9+AgY+xcLaK1Tgu/tyjFEbS7bYVLpXpkbBwzIJYjf21ymyiz0STov86ljxee CV3jVd8/3GmtuJZzq07aWDIzZKWhbnvKMOYL6qDJmCxcCaT5kuxxvtx44PsKJWSRVdZk xe3ol1zrT1DXe8rHkBONfs5VeHXPiWuabzyWem4Yue4aiHQOswZUcsTSjz4f+tqq6NPL f2uQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Iq4wcr3Y+3LwNr59Ivf9Z0GpSCD9IccniPD43sVgqyM=; b=xTCxxzpgQ19YvJsaJNmYN2rIXfzhdO2TQEmQfl8AGChgh0FVWhdw9L9bhXpGlm5jeE C5Q95mLFJCmQXivvFvHBrLDmZ3rEjzwyKGaNEiyERNbjBoDPDUWLnRyHxIpmVLOl1JtR o+qWb++TypBYqyjauPCCV6g4H6A8r71ycV4S4XjR4HvsTEcSTIbA6JvM454I+SiFbgVj +bwUOXE9yMqoh704VZugMeR3XG1gMNcR7dODEZINu3qtromiwobZgtx+J/dzzpwxT0oR IieAT7bYCdDMCDZaws8jHVPbJYasgvMbTFrslg9gpkbIxyL9GQZ7ONYcRJhu+rr/BLhp E9HQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=s66YKltA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f10-20020a056a001aca00b0050e0a0b03afsi30903100pfv.362.2022.06.23.12.16.22; Thu, 23 Jun 2022 12:16:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=s66YKltA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233310AbiFWRRg (ORCPT + 99 others); Thu, 23 Jun 2022 13:17:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233513AbiFWRMw (ORCPT ); Thu, 23 Jun 2022 13:12:52 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F206A4EF7A; Thu, 23 Jun 2022 09:58:50 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 7F893B82497; Thu, 23 Jun 2022 16:58:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D802EC341CB; Thu, 23 Jun 2022 16:58:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003528; bh=mbhjCA8gaFtDwUJ3TL4ixjbT+sq7I8e4Ukm6sJYkP6A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s66YKltAHkq43seq1bCWg5KXeqLeg42QJW9HhFYyI57EFgdUuHlNe0vgkDSIC7TwN LOm0fSVWxPMM16ppvxadClXeg3zt2PH5C3x6SStim3m0Wkb2DGRGn9sPAYgeI+/pgD lDhPd1kQu9yW//4LSVpitQjlEak2OVSlqO8cxxF4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Moshe Kol , Yossi Gilad , Amit Klein , Eric Dumazet , Willy Tarreau , Jakub Kicinski , Ben Hutchings Subject: [PATCH 4.9 261/264] tcp: add small random increments to the source port Date: Thu, 23 Jun 2022 18:44:14 +0200 Message-Id: <20220623164351.449023824@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164344.053938039@linuxfoundation.org> References: <20220623164344.053938039@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Willy Tarreau commit ca7af0402550f9a0b3316d5f1c30904e42ed257d upstream. Here we're randomly adding between 0 and 7 random increments to the selected source port in order to add some noise in the source port selection that will make the next port less predictable. With the default port range of 32768-60999 this means a worst case reuse scenario of 14116/8=1764 connections between two consecutive uses of the same port, with an average of 14116/4.5=3137. This code was stressed at more than 800000 connections per second to a fixed target with all connections closed by the client using RSTs (worst condition) and only 2 connections failed among 13 billion, despite the hash being reseeded every 10 seconds, indicating a perfectly safe situation. Cc: Moshe Kol Cc: Yossi Gilad Cc: Amit Klein Reviewed-by: Eric Dumazet Signed-off-by: Willy Tarreau Signed-off-by: Jakub Kicinski Signed-off-by: Ben Hutchings Signed-off-by: Greg Kroah-Hartman --- net/ipv4/inet_hashtables.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) --- a/net/ipv4/inet_hashtables.c +++ b/net/ipv4/inet_hashtables.c @@ -644,11 +644,12 @@ next_port: return -EADDRNOTAVAIL; ok: - /* If our first attempt found a candidate, skip next candidate - * in 1/16 of cases to add some noise. + /* Here we want to add a little bit of randomness to the next source + * port that will be chosen. We use a max() with a random here so that + * on low contention the randomness is maximal and on high contention + * it may be inexistent. */ - if (!i && !(prandom_u32() % 16)) - i = 2; + i = max_t(int, i, (prandom_u32() & 7) * 2); WRITE_ONCE(table_perturb[index], READ_ONCE(table_perturb[index]) + i + 2); /* Head lock still held and bh's disabled */