Skip to content

Commit 7e122d3

Browse files
edumazetgregkh
authored andcommitted
ipv6: use a stronger hash for tcp
[ Upstream commit 08dcdbf ] It looks like its possible to open thousands of TCP IPv6 sessions on a server, all landing in a single slot of TCP hash table. Incoming packets have to lookup sockets in a very long list. We should hash all bits from foreign IPv6 addresses, using a salt and hash mix, not a simple XOR. inet6_ehashfn() can also separately use the ports, instead of xoring them. Reported-by: Neal Cardwell <ncardwell@google.com> Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Yuchung Cheng <ycheng@google.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8044a3d commit 7e122d3

4 files changed

Lines changed: 24 additions & 6 deletions

File tree

‎include/net/inet6_hashtables.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828

2929
struct inet_hashinfo;
3030

31-
/* I have no idea if this is a good hash for v6 or not. -DaveM */
3231
static inline unsigned int inet6_ehashfn(struct net *net,
3332
const struct in6_addr *laddr, const u16 lport,
3433
const struct in6_addr *faddr, const __be16 fport)
3534
{
36-
u32 ports = (lport ^ (__force u16)fport);
35+
u32 ports = (((u32)lport) << 16) | (__force u32)fport;
3736

3837
return jhash_3words((__force u32)laddr->s6_addr32[3],
39-
(__force u32)faddr->s6_addr32[3],
40-
ports, inet_ehash_secret + net_hash_mix(net));
38+
ipv6_addr_jhash(faddr),
39+
ports,
40+
inet_ehash_secret + net_hash_mix(net));
4141
}
4242

4343
static inline int inet6_sk_ehashfn(const struct sock *sk)

‎include/net/inet_sock.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ static inline void inet_sk_copy_descendant(struct sock *sk_to,
203203
extern int inet_sk_rebuild_header(struct sock *sk);
204204

205205
extern u32 inet_ehash_secret;
206+
extern u32 ipv6_hash_secret;
206207
extern void build_ehash_secret(void);
207208

208209
static inline unsigned int inet_ehashfn(struct net *net,

‎include/net/ipv6.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <linux/ipv6.h>
1717
#include <linux/hardirq.h>
18+
#include <linux/jhash.h>
1819
#include <net/if_inet6.h>
1920
#include <net/ndisc.h>
2021
#include <net/flow.h>
@@ -473,6 +474,17 @@ static inline u32 ipv6_addr_hash(const struct in6_addr *a)
473474
#endif
474475
}
475476

477+
/* more secured version of ipv6_addr_hash() */
478+
static inline u32 ipv6_addr_jhash(const struct in6_addr *a)
479+
{
480+
u32 v = (__force u32)a->s6_addr32[0] ^ (__force u32)a->s6_addr32[1];
481+
482+
return jhash_3words(v,
483+
(__force u32)a->s6_addr32[2],
484+
(__force u32)a->s6_addr32[3],
485+
ipv6_hash_secret);
486+
}
487+
476488
static inline bool ipv6_addr_loopback(const struct in6_addr *a)
477489
{
478490
return (a->s6_addr32[0] | a->s6_addr32[1] |

‎net/ipv4/af_inet.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,12 @@ EXPORT_SYMBOL(inet_listen);
248248
u32 inet_ehash_secret __read_mostly;
249249
EXPORT_SYMBOL(inet_ehash_secret);
250250

251+
u32 ipv6_hash_secret __read_mostly;
252+
EXPORT_SYMBOL(ipv6_hash_secret);
253+
251254
/*
252-
* inet_ehash_secret must be set exactly once
255+
* inet_ehash_secret must be set exactly once, and to a non nul value
256+
* ipv6_hash_secret must be set exactly once.
253257
*/
254258
void build_ehash_secret(void)
255259
{
@@ -259,7 +263,8 @@ void build_ehash_secret(void)
259263
get_random_bytes(&rnd, sizeof(rnd));
260264
} while (rnd == 0);
261265

262-
cmpxchg(&inet_ehash_secret, 0, rnd);
266+
if (cmpxchg(&inet_ehash_secret, 0, rnd) == 0)
267+
get_random_bytes(&ipv6_hash_secret, sizeof(ipv6_hash_secret));
263268
}
264269
EXPORT_SYMBOL(build_ehash_secret);
265270

0 commit comments

Comments
 (0)