Commit b3da2cf3 authored by David S. Miller's avatar David S. Miller Committed by David S. Miller

[INET]: Use jhash + random secret for ehash.

The days are gone when this was not an issue, there are folks out
there with huge bot networks that can be used to attack the
established hash tables on remote systems.

So just like the routing cache and connection tracking
hash, use Jenkins hash with random secret input.
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent d30045a0
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
#include <linux/in6.h> #include <linux/in6.h>
#include <linux/ipv6.h> #include <linux/ipv6.h>
#include <linux/types.h> #include <linux/types.h>
#include <linux/jhash.h>
#include <net/inet_sock.h>
#include <net/ipv6.h> #include <net/ipv6.h>
...@@ -28,12 +31,11 @@ struct inet_hashinfo; ...@@ -28,12 +31,11 @@ struct inet_hashinfo;
static inline unsigned int inet6_ehashfn(const struct in6_addr *laddr, const u16 lport, static inline unsigned int inet6_ehashfn(const struct in6_addr *laddr, const u16 lport,
const struct in6_addr *faddr, const __be16 fport) const struct in6_addr *faddr, const __be16 fport)
{ {
unsigned int hashent = (lport ^ (__force u16)fport); u32 ports = (lport ^ (__force u16)fport);
hashent ^= (__force u32)(laddr->s6_addr32[3] ^ faddr->s6_addr32[3]); return jhash_3words((__force u32)laddr->s6_addr32[3],
hashent ^= hashent >> 16; (__force u32)faddr->s6_addr32[3],
hashent ^= hashent >> 8; ports, inet_ehash_secret);
return hashent;
} }
static inline int inet6_sk_ehashfn(const struct sock *sk) static inline int inet6_sk_ehashfn(const struct sock *sk)
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <linux/string.h> #include <linux/string.h>
#include <linux/types.h> #include <linux/types.h>
#include <linux/jhash.h>
#include <net/flow.h> #include <net/flow.h>
#include <net/sock.h> #include <net/sock.h>
...@@ -167,13 +168,15 @@ static inline void inet_sk_copy_descendant(struct sock *sk_to, ...@@ -167,13 +168,15 @@ static inline void inet_sk_copy_descendant(struct sock *sk_to,
extern int inet_sk_rebuild_header(struct sock *sk); extern int inet_sk_rebuild_header(struct sock *sk);
extern u32 inet_ehash_secret;
extern void build_ehash_secret(void);
static inline unsigned int inet_ehashfn(const __be32 laddr, const __u16 lport, static inline unsigned int inet_ehashfn(const __be32 laddr, const __u16 lport,
const __be32 faddr, const __be16 fport) const __be32 faddr, const __be16 fport)
{ {
unsigned int h = ((__force __u32)laddr ^ lport) ^ ((__force __u32)faddr ^ (__force __u32)fport); return jhash_2words((__force __u32) laddr ^ (__force __u32) faddr,
h ^= h >> 16; ((__u32) lport) << 16 | (__force __u32)fport,
h ^= h >> 8; inet_ehash_secret);
return h;
} }
static inline int inet_sk_ehashfn(const struct sock *sk) static inline int inet_sk_ehashfn(const struct sock *sk)
......
...@@ -87,6 +87,7 @@ ...@@ -87,6 +87,7 @@
#include <linux/init.h> #include <linux/init.h>
#include <linux/poll.h> #include <linux/poll.h>
#include <linux/netfilter_ipv4.h> #include <linux/netfilter_ipv4.h>
#include <linux/random.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
#include <asm/system.h> #include <asm/system.h>
...@@ -217,6 +218,16 @@ out: ...@@ -217,6 +218,16 @@ out:
return err; return err;
} }
u32 inet_ehash_secret;
EXPORT_SYMBOL(inet_ehash_secret);
void build_ehash_secret(void)
{
while (!inet_ehash_secret)
get_random_bytes(&inet_ehash_secret, 4);
}
EXPORT_SYMBOL(build_ehash_secret);
/* /*
* Create an inet socket. * Create an inet socket.
*/ */
...@@ -233,6 +244,11 @@ static int inet_create(struct socket *sock, int protocol) ...@@ -233,6 +244,11 @@ static int inet_create(struct socket *sock, int protocol)
int try_loading_module = 0; int try_loading_module = 0;
int err; int err;
if (sock->type != SOCK_RAW &&
sock->type != SOCK_DGRAM &&
!inet_ehash_secret)
build_ehash_secret();
sock->state = SS_UNCONNECTED; sock->state = SS_UNCONNECTED;
/* Look for the requested type/protocol pair. */ /* Look for the requested type/protocol pair. */
......
...@@ -98,6 +98,11 @@ static int inet6_create(struct socket *sock, int protocol) ...@@ -98,6 +98,11 @@ static int inet6_create(struct socket *sock, int protocol)
int try_loading_module = 0; int try_loading_module = 0;
int err; int err;
if (sock->type != SOCK_RAW &&
sock->type != SOCK_DGRAM &&
!inet_ehash_secret)
build_ehash_secret();
/* Look for the requested type/protocol pair. */ /* Look for the requested type/protocol pair. */
answer = NULL; answer = NULL;
lookup_protocol: lookup_protocol:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment