David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 1 | #ifndef _XFRM_HASH_H |
| 2 | #define _XFRM_HASH_H |
| 3 | |
| 4 | #include <linux/xfrm.h> |
| 5 | #include <linux/socket.h> |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 6 | #include <linux/jhash.h> |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 7 | |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 8 | static inline unsigned int __xfrm4_addr_hash(const xfrm_address_t *addr) |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 9 | { |
| 10 | return ntohl(addr->a4); |
| 11 | } |
| 12 | |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 13 | static inline unsigned int __xfrm6_addr_hash(const xfrm_address_t *addr) |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 14 | { |
| 15 | return ntohl(addr->a6[2] ^ addr->a6[3]); |
| 16 | } |
| 17 | |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 18 | static inline unsigned int __xfrm4_daddr_saddr_hash(const xfrm_address_t *daddr, |
| 19 | const xfrm_address_t *saddr) |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 20 | { |
Eric Dumazet | 0eae88f | 2010-04-20 19:06:52 -0700 | [diff] [blame] | 21 | u32 sum = (__force u32)daddr->a4 + (__force u32)saddr->a4; |
| 22 | return ntohl((__force __be32)sum); |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 23 | } |
| 24 | |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 25 | static inline unsigned int __xfrm6_daddr_saddr_hash(const xfrm_address_t *daddr, |
| 26 | const xfrm_address_t *saddr) |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 27 | { |
| 28 | return ntohl(daddr->a6[2] ^ daddr->a6[3] ^ |
| 29 | saddr->a6[2] ^ saddr->a6[3]); |
| 30 | } |
| 31 | |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 32 | static inline u32 __bits2mask32(__u8 bits) |
| 33 | { |
| 34 | u32 mask32 = 0xffffffff; |
| 35 | |
| 36 | if (bits == 0) |
| 37 | mask32 = 0; |
| 38 | else if (bits < 32) |
| 39 | mask32 <<= (32 - bits); |
| 40 | |
| 41 | return mask32; |
| 42 | } |
| 43 | |
| 44 | static inline unsigned int __xfrm4_dpref_spref_hash(const xfrm_address_t *daddr, |
| 45 | const xfrm_address_t *saddr, |
| 46 | __u8 dbits, |
| 47 | __u8 sbits) |
| 48 | { |
| 49 | return jhash_2words(ntohl(daddr->a4) & __bits2mask32(dbits), |
| 50 | ntohl(saddr->a4) & __bits2mask32(sbits), |
| 51 | 0); |
| 52 | } |
| 53 | |
| 54 | static inline unsigned int __xfrm6_pref_hash(const xfrm_address_t *addr, |
| 55 | __u8 prefixlen) |
| 56 | { |
Alexey Dobriyan | d7f6946 | 2017-03-24 01:53:09 +0300 | [diff] [blame] | 57 | unsigned int pdw; |
| 58 | unsigned int pbi; |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 59 | u32 initval = 0; |
| 60 | |
| 61 | pdw = prefixlen >> 5; /* num of whole u32 in prefix */ |
| 62 | pbi = prefixlen & 0x1f; /* num of bits in incomplete u32 in prefix */ |
| 63 | |
| 64 | if (pbi) { |
| 65 | __be32 mask; |
| 66 | |
| 67 | mask = htonl((0xffffffff) << (32 - pbi)); |
| 68 | |
| 69 | initval = (__force u32)(addr->a6[pdw] & mask); |
| 70 | } |
| 71 | |
| 72 | return jhash2((__force u32 *)addr->a6, pdw, initval); |
| 73 | } |
| 74 | |
| 75 | static inline unsigned int __xfrm6_dpref_spref_hash(const xfrm_address_t *daddr, |
| 76 | const xfrm_address_t *saddr, |
| 77 | __u8 dbits, |
| 78 | __u8 sbits) |
| 79 | { |
| 80 | return __xfrm6_pref_hash(daddr, dbits) ^ |
| 81 | __xfrm6_pref_hash(saddr, sbits); |
| 82 | } |
| 83 | |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 84 | static inline unsigned int __xfrm_dst_hash(const xfrm_address_t *daddr, |
| 85 | const xfrm_address_t *saddr, |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 86 | u32 reqid, unsigned short family, |
| 87 | unsigned int hmask) |
| 88 | { |
| 89 | unsigned int h = family ^ reqid; |
| 90 | switch (family) { |
| 91 | case AF_INET: |
| 92 | h ^= __xfrm4_daddr_saddr_hash(daddr, saddr); |
| 93 | break; |
| 94 | case AF_INET6: |
| 95 | h ^= __xfrm6_daddr_saddr_hash(daddr, saddr); |
| 96 | break; |
| 97 | } |
| 98 | return (h ^ (h >> 16)) & hmask; |
| 99 | } |
| 100 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 101 | static inline unsigned int __xfrm_src_hash(const xfrm_address_t *daddr, |
| 102 | const xfrm_address_t *saddr, |
| 103 | unsigned short family, |
| 104 | unsigned int hmask) |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 105 | { |
| 106 | unsigned int h = family; |
| 107 | switch (family) { |
| 108 | case AF_INET: |
Masahide NAKAMURA | 667bbcb | 2006-10-03 15:56:09 -0700 | [diff] [blame] | 109 | h ^= __xfrm4_daddr_saddr_hash(daddr, saddr); |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 110 | break; |
| 111 | case AF_INET6: |
Masahide NAKAMURA | 667bbcb | 2006-10-03 15:56:09 -0700 | [diff] [blame] | 112 | h ^= __xfrm6_daddr_saddr_hash(daddr, saddr); |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 113 | break; |
Joe Perches | ccbd6a5 | 2010-05-14 10:58:26 +0000 | [diff] [blame] | 114 | } |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 115 | return (h ^ (h >> 16)) & hmask; |
| 116 | } |
| 117 | |
| 118 | static inline unsigned int |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 119 | __xfrm_spi_hash(const xfrm_address_t *daddr, __be32 spi, u8 proto, |
| 120 | unsigned short family, unsigned int hmask) |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 121 | { |
Al Viro | 8122adf | 2006-09-27 18:49:35 -0700 | [diff] [blame] | 122 | unsigned int h = (__force u32)spi ^ proto; |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 123 | switch (family) { |
| 124 | case AF_INET: |
| 125 | h ^= __xfrm4_addr_hash(daddr); |
| 126 | break; |
| 127 | case AF_INET6: |
| 128 | h ^= __xfrm6_addr_hash(daddr); |
| 129 | break; |
| 130 | } |
| 131 | return (h ^ (h >> 10) ^ (h >> 20)) & hmask; |
| 132 | } |
| 133 | |
| 134 | static inline unsigned int __idx_hash(u32 index, unsigned int hmask) |
| 135 | { |
| 136 | return (index ^ (index >> 8)) & hmask; |
| 137 | } |
| 138 | |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 139 | static inline unsigned int __sel_hash(const struct xfrm_selector *sel, |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 140 | unsigned short family, unsigned int hmask, |
| 141 | u8 dbits, u8 sbits) |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 142 | { |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 143 | const xfrm_address_t *daddr = &sel->daddr; |
| 144 | const xfrm_address_t *saddr = &sel->saddr; |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 145 | unsigned int h = 0; |
| 146 | |
| 147 | switch (family) { |
| 148 | case AF_INET: |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 149 | if (sel->prefixlen_d < dbits || |
| 150 | sel->prefixlen_s < sbits) |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 151 | return hmask + 1; |
| 152 | |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 153 | h = __xfrm4_dpref_spref_hash(daddr, saddr, dbits, sbits); |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 154 | break; |
| 155 | |
| 156 | case AF_INET6: |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 157 | if (sel->prefixlen_d < dbits || |
| 158 | sel->prefixlen_s < sbits) |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 159 | return hmask + 1; |
| 160 | |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 161 | h = __xfrm6_dpref_spref_hash(daddr, saddr, dbits, sbits); |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 162 | break; |
Joe Perches | ccbd6a5 | 2010-05-14 10:58:26 +0000 | [diff] [blame] | 163 | } |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 164 | h ^= (h >> 16); |
| 165 | return h & hmask; |
| 166 | } |
| 167 | |
David S. Miller | 5f803b5 | 2011-02-24 00:33:19 -0500 | [diff] [blame] | 168 | static inline unsigned int __addr_hash(const xfrm_address_t *daddr, |
| 169 | const xfrm_address_t *saddr, |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 170 | unsigned short family, |
| 171 | unsigned int hmask, |
| 172 | u8 dbits, u8 sbits) |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 173 | { |
| 174 | unsigned int h = 0; |
| 175 | |
| 176 | switch (family) { |
| 177 | case AF_INET: |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 178 | h = __xfrm4_dpref_spref_hash(daddr, saddr, dbits, sbits); |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 179 | break; |
| 180 | |
| 181 | case AF_INET6: |
Christophe Gouault | b58555f | 2014-08-29 16:16:04 +0200 | [diff] [blame] | 182 | h = __xfrm6_dpref_spref_hash(daddr, saddr, dbits, sbits); |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 183 | break; |
Joe Perches | ccbd6a5 | 2010-05-14 10:58:26 +0000 | [diff] [blame] | 184 | } |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 185 | h ^= (h >> 16); |
| 186 | return h & hmask; |
| 187 | } |
| 188 | |
Joe Perches | c1b1203 | 2013-10-18 13:48:25 -0700 | [diff] [blame] | 189 | struct hlist_head *xfrm_hash_alloc(unsigned int sz); |
| 190 | void xfrm_hash_free(struct hlist_head *n, unsigned int sz); |
David S. Miller | 44e36b4 | 2006-08-24 04:50:50 -0700 | [diff] [blame] | 191 | |
| 192 | #endif /* _XFRM_HASH_H */ |