Re: ipv4: Simplify ARP hash function.
From: David Miller <davem@davemloft.net>
Date: 2011-07-08 17:54:34
From: David Miller <davem@davemloft.net> Date: Fri, 08 Jul 2011 10:47:39 -0700 (PDT)
From: Martin Mares <mj@ucw.cz> Date: Fri, 8 Jul 2011 19:40:55 +0200quoted
The hash function is linear, so it can be reduced to: a = key ^ dev->ifindex return (a >> 8) ^ (a >> 16) ^ (a >> 24) // (1) ^ (hash_rnd >> 8) ^ (hash_rnd >> 16) ^ (hash_rnd >> 24) // (2)Is this really the same? The inclusion of a full 32-bit xor with hash_rnd before folding was intentional, so that the final folding occurs on a completely "random" value.
For example, try out this test program. Run as "./x ${RANDOM_VALUE}",
it shows that the attacker cannot simply just iterate by the number of
hash table slots to create collisions, assuming a hash table size of
256 slots:
--------------------
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argp)
{
int i, rnd;
rnd = atoi(argp[1]);
for (i = 1; i < (64 * 1024); i += 256) {
int x = (i ^ rnd);
x ^= (x >> 8) ^ (x << 16) ^ (x >> 24);
x &= 0xff;
printf("%d\n", x);
}
return 0;
}