Skip to content

Commit ca7af04

Browse files
wtarreaukuba-moo
authored andcommitted
tcp: add small random increments to the source port
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 <moshe.kol@mail.huji.ac.il> Cc: Yossi Gilad <yossi.gilad@mail.huji.ac.il> Cc: Amit Klein <aksecurity@gmail.com> Reviewed-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Willy Tarreau <w@1wt.eu> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 4dfa9b4 commit ca7af04

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

net/ipv4/inet_hashtables.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -833,11 +833,12 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
833833
return -EADDRNOTAVAIL;
834834

835835
ok:
836-
/* If our first attempt found a candidate, skip next candidate
837-
* in 1/16 of cases to add some noise.
836+
/* Here we want to add a little bit of randomness to the next source
837+
* port that will be chosen. We use a max() with a random here so that
838+
* on low contention the randomness is maximal and on high contention
839+
* it may be inexistent.
838840
*/
839-
if (!i && !(prandom_u32() % 16))
840-
i = 2;
841+
i = max_t(int, i, (prandom_u32() & 7) * 2);
841842
WRITE_ONCE(table_perturb[index], READ_ONCE(table_perturb[index]) + i + 2);
842843

843844
/* Head lock still held and bh's disabled */

0 commit comments

Comments
 (0)