Skip to content

Commit 2c2aba6

Browse files
committed
ipv6: Use universal hash for NDISC.
In order to perform a proper universal hash on a vector of integers, we have to use different universal hashes on each vector element. Which means we need 4 different hash randoms for ipv6. Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 32288eb commit 2c2aba6

7 files changed

Lines changed: 32 additions & 21 deletions

File tree

‎include/net/arp.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static inline struct neighbour *__ipv4_neigh_lookup(struct neigh_table *tbl, str
2323

2424
rcu_read_lock_bh();
2525
nht = rcu_dereference_bh(tbl->nht);
26-
hash_val = arp_hashfn(key, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
26+
hash_val = arp_hashfn(key, dev, nht->hash_rnd[0]) >> (32 - nht->hash_shift);
2727
for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
2828
n != NULL;
2929
n = rcu_dereference_bh(n->next)) {

‎include/net/ndisc.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ struct nd_opt_hdr {
7979
__u8 nd_opt_len;
8080
} __packed;
8181

82+
static inline u32 ndisc_hashfn(const void *pkey, const struct net_device *dev, __u32 *hash_rnd)
83+
{
84+
const u32 *p32 = pkey;
85+
86+
return (((p32[0] ^ dev->ifindex) * hash_rnd[0]) +
87+
(p32[1] * hash_rnd[1]) +
88+
(p32[2] * hash_rnd[2]) +
89+
(p32[3] * hash_rnd[3]));
90+
}
8291

8392
extern int ndisc_init(void);
8493

‎include/net/neighbour.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,12 @@ struct pneigh_entry {
139139
* neighbour table manipulation
140140
*/
141141

142+
#define NEIGH_NUM_HASH_RND 4
143+
142144
struct neigh_hash_table {
143145
struct neighbour __rcu **hash_buckets;
144146
unsigned int hash_shift;
145-
__u32 hash_rnd;
147+
__u32 hash_rnd[NEIGH_NUM_HASH_RND];
146148
struct rcu_head rcu;
147149
};
148150

@@ -154,7 +156,7 @@ struct neigh_table {
154156
int key_len;
155157
__u32 (*hash)(const void *pkey,
156158
const struct net_device *dev,
157-
__u32 hash_rnd);
159+
__u32 *hash_rnd);
158160
int (*constructor)(struct neighbour *);
159161
int (*pconstructor)(struct pneigh_entry *);
160162
void (*pdestructor)(struct pneigh_entry *);

‎net/core/neighbour.c‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,18 @@ static struct neighbour *neigh_alloc(struct neigh_table *tbl, struct net_device
322322
goto out;
323323
}
324324

325+
static void neigh_get_hash_rnd(u32 *x)
326+
{
327+
get_random_bytes(x, sizeof(*x));
328+
*x |= 1;
329+
}
330+
325331
static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
326332
{
327333
size_t size = (1 << shift) * sizeof(struct neighbour *);
328334
struct neigh_hash_table *ret;
329335
struct neighbour __rcu **buckets;
336+
int i;
330337

331338
ret = kmalloc(sizeof(*ret), GFP_ATOMIC);
332339
if (!ret)
@@ -343,8 +350,8 @@ static struct neigh_hash_table *neigh_hash_alloc(unsigned int shift)
343350
}
344351
ret->hash_buckets = buckets;
345352
ret->hash_shift = shift;
346-
get_random_bytes(&ret->hash_rnd, sizeof(ret->hash_rnd));
347-
ret->hash_rnd |= 1;
353+
for (i = 0; i < NEIGH_NUM_HASH_RND; i++)
354+
neigh_get_hash_rnd(&ret->hash_rnd[i]);
348355
return ret;
349356
}
350357

@@ -1828,7 +1835,7 @@ static int neightbl_fill_info(struct sk_buff *skb, struct neigh_table *tbl,
18281835

18291836
rcu_read_lock_bh();
18301837
nht = rcu_dereference_bh(tbl->nht);
1831-
ndc.ndtc_hash_rnd = nht->hash_rnd;
1838+
ndc.ndtc_hash_rnd = nht->hash_rnd[0];
18321839
ndc.ndtc_hash_mask = ((1 << nht->hash_shift) - 1);
18331840
rcu_read_unlock_bh();
18341841

‎net/decnet/dn_neigh.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ static const struct neigh_ops dn_phase3_ops = {
8888

8989
static u32 dn_neigh_hash(const void *pkey,
9090
const struct net_device *dev,
91-
__u32 hash_rnd)
91+
__u32 *hash_rnd)
9292
{
93-
return jhash_2words(*(__u16 *)pkey, 0, hash_rnd);
93+
return jhash_2words(*(__u16 *)pkey, 0, hash_rnd[0]);
9494
}
9595

9696
struct neigh_table dn_neigh_table = {

‎net/ipv4/arp.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
/*
122122
* Interface to generic neighbour cache.
123123
*/
124-
static u32 arp_hash(const void *pkey, const struct net_device *dev, __u32 rnd);
124+
static u32 arp_hash(const void *pkey, const struct net_device *dev, __u32 *hash_rnd);
125125
static int arp_constructor(struct neighbour *neigh);
126126
static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb);
127127
static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb);
@@ -215,9 +215,9 @@ int arp_mc_map(__be32 addr, u8 *haddr, struct net_device *dev, int dir)
215215

216216
static u32 arp_hash(const void *pkey,
217217
const struct net_device *dev,
218-
__u32 hash_rnd)
218+
__u32 *hash_rnd)
219219
{
220-
return arp_hashfn(*(u32 *)pkey, dev, hash_rnd);
220+
return arp_hashfn(*(u32 *)pkey, dev, *hash_rnd);
221221
}
222222

223223
static int arp_constructor(struct neighbour *neigh)

‎net/ipv6/ndisc.c‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393

9494
static u32 ndisc_hash(const void *pkey,
9595
const struct net_device *dev,
96-
__u32 rnd);
96+
__u32 *hash_rnd);
9797
static int ndisc_constructor(struct neighbour *neigh);
9898
static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb);
9999
static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb);
@@ -349,16 +349,9 @@ EXPORT_SYMBOL(ndisc_mc_map);
349349

350350
static u32 ndisc_hash(const void *pkey,
351351
const struct net_device *dev,
352-
__u32 hash_rnd)
352+
__u32 *hash_rnd)
353353
{
354-
const u32 *p32 = pkey;
355-
u32 addr_hash, i;
356-
357-
addr_hash = 0;
358-
for (i = 0; i < (sizeof(struct in6_addr) / sizeof(u32)); i++)
359-
addr_hash ^= *p32++;
360-
361-
return jhash_2words(addr_hash, dev->ifindex, hash_rnd);
354+
return ndisc_hashfn(pkey, dev, hash_rnd);
362355
}
363356

364357
static int ndisc_constructor(struct neighbour *neigh)

0 commit comments

Comments
 (0)