Skip to content

Commit e926147

Browse files
wtarreaukuba-moo
authored andcommitted
tcp: dynamically allocate the perturb table used by source ports
We'll need to further increase the size of this table and it's likely that at some point its size will not be suitable anymore for a static table. Let's allocate it on boot from inet_hashinfo2_init(), which is called from tcp_init(). 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 ca7af04 commit e926147

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

net/ipv4/inet_hashtables.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,8 @@ EXPORT_SYMBOL_GPL(inet_unhash);
731731
* privacy, this only consumes 1 KB of kernel memory.
732732
*/
733733
#define INET_TABLE_PERTURB_SHIFT 8
734-
static u32 table_perturb[1 << INET_TABLE_PERTURB_SHIFT];
734+
#define INET_TABLE_PERTURB_SIZE (1 << INET_TABLE_PERTURB_SHIFT)
735+
static u32 *table_perturb;
735736

736737
int __inet_hash_connect(struct inet_timewait_death_row *death_row,
737738
struct sock *sk, u64 port_offset,
@@ -774,7 +775,8 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
774775
if (likely(remaining > 1))
775776
remaining &= ~1U;
776777

777-
net_get_random_once(table_perturb, sizeof(table_perturb));
778+
net_get_random_once(table_perturb,
779+
INET_TABLE_PERTURB_SIZE * sizeof(*table_perturb));
778780
index = hash_32(port_offset, INET_TABLE_PERTURB_SHIFT);
779781

780782
offset = READ_ONCE(table_perturb[index]) + (port_offset >> 32);
@@ -912,6 +914,12 @@ void __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
912914
low_limit,
913915
high_limit);
914916
init_hashinfo_lhash2(h);
917+
918+
/* this one is used for source ports of outgoing connections */
919+
table_perturb = kmalloc_array(INET_TABLE_PERTURB_SIZE,
920+
sizeof(*table_perturb), GFP_KERNEL);
921+
if (!table_perturb)
922+
panic("TCP: failed to alloc table_perturb");
915923
}
916924

917925
int inet_hashinfo2_init_mod(struct inet_hashinfo *h)

0 commit comments

Comments
 (0)