ipvs: fix multiplicative hashing in sh/dh/lblc/lblcr algorithms
authorVincent Bernat <vincent@bernat.im>
Sun, 1 Apr 2018 10:27:11 +0000 (12:27 +0200)
committerSimon Horman <horms@verge.net.au>
Mon, 9 Apr 2018 07:15:27 +0000 (10:15 +0300)
The sh/dh/lblc/lblcr algorithms are using Knuth's multiplicative
hashing incorrectly. Replace its use by the hash_32() macro, which
correctly implements this algorithm. It doesn't use the same constant,
but it shouldn't matter.

Signed-off-by: Vincent Bernat <vincent@bernat.im>
Acked-by: Julian Anastasov <ja@ssi.bg>
Signed-off-by: Simon Horman <horms@verge.net.au>
net/netfilter/ipvs/ip_vs_dh.c
net/netfilter/ipvs/ip_vs_lblc.c
net/netfilter/ipvs/ip_vs_lblcr.c
net/netfilter/ipvs/ip_vs_sh.c

index 75f798f8e83b706701787706c6fad2facad35155..07459e71d9072387531d760dbbde7ab23fc5f145 100644 (file)
@@ -43,6 +43,7 @@
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/skbuff.h>
+#include <linux/hash.h>
 
 #include <net/ip_vs.h>
 
@@ -81,7 +82,7 @@ static inline unsigned int ip_vs_dh_hashkey(int af, const union nf_inet_addr *ad
                addr_fold = addr->ip6[0]^addr->ip6[1]^
                            addr->ip6[2]^addr->ip6[3];
 #endif
-       return (ntohl(addr_fold)*2654435761UL) & IP_VS_DH_TAB_MASK;
+       return hash_32(ntohl(addr_fold), IP_VS_DH_TAB_BITS);
 }
 
 
index 3057e453bf317c500cc77865ec4ed6d2a4eb479a..08147fc6400ccf65ca7b8688826aaba1f3fa85c5 100644 (file)
@@ -48,6 +48,7 @@
 #include <linux/kernel.h>
 #include <linux/skbuff.h>
 #include <linux/jiffies.h>
+#include <linux/hash.h>
 
 /* for sysctl */
 #include <linux/fs.h>
@@ -160,7 +161,7 @@ ip_vs_lblc_hashkey(int af, const union nf_inet_addr *addr)
                addr_fold = addr->ip6[0]^addr->ip6[1]^
                            addr->ip6[2]^addr->ip6[3];
 #endif
-       return (ntohl(addr_fold)*2654435761UL) & IP_VS_LBLC_TAB_MASK;
+       return hash_32(ntohl(addr_fold), IP_VS_LBLC_TAB_BITS);
 }
 
 
index 92adc04557ed954c769e309e9e51d945ac474620..9b6a6c9e9cfa143cf8dc3967472dcb65cb722a60 100644 (file)
@@ -47,6 +47,7 @@
 #include <linux/jiffies.h>
 #include <linux/list.h>
 #include <linux/slab.h>
+#include <linux/hash.h>
 
 /* for sysctl */
 #include <linux/fs.h>
@@ -323,7 +324,7 @@ ip_vs_lblcr_hashkey(int af, const union nf_inet_addr *addr)
                addr_fold = addr->ip6[0]^addr->ip6[1]^
                            addr->ip6[2]^addr->ip6[3];
 #endif
-       return (ntohl(addr_fold)*2654435761UL) & IP_VS_LBLCR_TAB_MASK;
+       return hash_32(ntohl(addr_fold), IP_VS_LBLCR_TAB_BITS);
 }
 
 
index 16aaac6eedc963dee56f088c8933dc962bbd27c6..1e01c782583a62efd4258c133c7b2c8f97bc2e85 100644 (file)
@@ -96,7 +96,8 @@ ip_vs_sh_hashkey(int af, const union nf_inet_addr *addr,
                addr_fold = addr->ip6[0]^addr->ip6[1]^
                            addr->ip6[2]^addr->ip6[3];
 #endif
-       return (offset + (ntohs(port) + ntohl(addr_fold))*2654435761UL) &
+       return (offset + hash_32(ntohs(port) + ntohl(addr_fold),
+                                IP_VS_SH_TAB_BITS)) &
                IP_VS_SH_TAB_MASK;
 }