xref: /openbmc/linux/net/xfrm/xfrm_hash.h (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
244e36b42SDavid S. Miller #ifndef _XFRM_HASH_H
344e36b42SDavid S. Miller #define _XFRM_HASH_H
444e36b42SDavid S. Miller 
544e36b42SDavid S. Miller #include <linux/xfrm.h>
644e36b42SDavid S. Miller #include <linux/socket.h>
7b58555f1SChristophe Gouault #include <linux/jhash.h>
844e36b42SDavid S. Miller 
__xfrm4_addr_hash(const xfrm_address_t * addr)95f803b58SDavid S. Miller static inline unsigned int __xfrm4_addr_hash(const xfrm_address_t *addr)
1044e36b42SDavid S. Miller {
1144e36b42SDavid S. Miller 	return ntohl(addr->a4);
1244e36b42SDavid S. Miller }
1344e36b42SDavid S. Miller 
__xfrm6_addr_hash(const xfrm_address_t * addr)145f803b58SDavid S. Miller static inline unsigned int __xfrm6_addr_hash(const xfrm_address_t *addr)
1544e36b42SDavid S. Miller {
168d4b6bceSMichal Kubecek 	return jhash2((__force u32 *)addr->a6, 4, 0);
1744e36b42SDavid S. Miller }
1844e36b42SDavid S. Miller 
__xfrm4_daddr_saddr_hash(const xfrm_address_t * daddr,const xfrm_address_t * saddr)195f803b58SDavid S. Miller static inline unsigned int __xfrm4_daddr_saddr_hash(const xfrm_address_t *daddr,
205f803b58SDavid S. Miller 						    const xfrm_address_t *saddr)
2144e36b42SDavid S. Miller {
220eae88f3SEric Dumazet 	u32 sum = (__force u32)daddr->a4 + (__force u32)saddr->a4;
230eae88f3SEric Dumazet 	return ntohl((__force __be32)sum);
2444e36b42SDavid S. Miller }
2544e36b42SDavid S. Miller 
__xfrm6_daddr_saddr_hash(const xfrm_address_t * daddr,const xfrm_address_t * saddr)265f803b58SDavid S. Miller static inline unsigned int __xfrm6_daddr_saddr_hash(const xfrm_address_t *daddr,
275f803b58SDavid S. Miller 						    const xfrm_address_t *saddr)
2844e36b42SDavid S. Miller {
298d4b6bceSMichal Kubecek 	return __xfrm6_addr_hash(daddr) ^ __xfrm6_addr_hash(saddr);
3044e36b42SDavid S. Miller }
3144e36b42SDavid S. Miller 
__bits2mask32(__u8 bits)32b58555f1SChristophe Gouault static inline u32 __bits2mask32(__u8 bits)
33b58555f1SChristophe Gouault {
34b58555f1SChristophe Gouault 	u32 mask32 = 0xffffffff;
35b58555f1SChristophe Gouault 
36b58555f1SChristophe Gouault 	if (bits == 0)
37b58555f1SChristophe Gouault 		mask32 = 0;
38b58555f1SChristophe Gouault 	else if (bits < 32)
39b58555f1SChristophe Gouault 		mask32 <<= (32 - bits);
40b58555f1SChristophe Gouault 
41b58555f1SChristophe Gouault 	return mask32;
42b58555f1SChristophe Gouault }
43b58555f1SChristophe Gouault 
__xfrm4_dpref_spref_hash(const xfrm_address_t * daddr,const xfrm_address_t * saddr,__u8 dbits,__u8 sbits)44b58555f1SChristophe Gouault static inline unsigned int __xfrm4_dpref_spref_hash(const xfrm_address_t *daddr,
45b58555f1SChristophe Gouault 						    const xfrm_address_t *saddr,
46b58555f1SChristophe Gouault 						    __u8 dbits,
47b58555f1SChristophe Gouault 						    __u8 sbits)
48b58555f1SChristophe Gouault {
49b58555f1SChristophe Gouault 	return jhash_2words(ntohl(daddr->a4) & __bits2mask32(dbits),
50b58555f1SChristophe Gouault 			    ntohl(saddr->a4) & __bits2mask32(sbits),
51b58555f1SChristophe Gouault 			    0);
52b58555f1SChristophe Gouault }
53b58555f1SChristophe Gouault 
__xfrm6_pref_hash(const xfrm_address_t * addr,__u8 prefixlen)54b58555f1SChristophe Gouault static inline unsigned int __xfrm6_pref_hash(const xfrm_address_t *addr,
55b58555f1SChristophe Gouault 					     __u8 prefixlen)
56b58555f1SChristophe Gouault {
57d7f69466SAlexey Dobriyan 	unsigned int pdw;
58d7f69466SAlexey Dobriyan 	unsigned int pbi;
59b58555f1SChristophe Gouault 	u32 initval = 0;
60b58555f1SChristophe Gouault 
61b58555f1SChristophe Gouault 	pdw = prefixlen >> 5;     /* num of whole u32 in prefix */
62b58555f1SChristophe Gouault 	pbi = prefixlen &  0x1f;  /* num of bits in incomplete u32 in prefix */
63b58555f1SChristophe Gouault 
64b58555f1SChristophe Gouault 	if (pbi) {
65b58555f1SChristophe Gouault 		__be32 mask;
66b58555f1SChristophe Gouault 
67b58555f1SChristophe Gouault 		mask = htonl((0xffffffff) << (32 - pbi));
68b58555f1SChristophe Gouault 
69b58555f1SChristophe Gouault 		initval = (__force u32)(addr->a6[pdw] & mask);
70b58555f1SChristophe Gouault 	}
71b58555f1SChristophe Gouault 
72b58555f1SChristophe Gouault 	return jhash2((__force u32 *)addr->a6, pdw, initval);
73b58555f1SChristophe Gouault }
74b58555f1SChristophe Gouault 
__xfrm6_dpref_spref_hash(const xfrm_address_t * daddr,const xfrm_address_t * saddr,__u8 dbits,__u8 sbits)75b58555f1SChristophe Gouault static inline unsigned int __xfrm6_dpref_spref_hash(const xfrm_address_t *daddr,
76b58555f1SChristophe Gouault 						    const xfrm_address_t *saddr,
77b58555f1SChristophe Gouault 						    __u8 dbits,
78b58555f1SChristophe Gouault 						    __u8 sbits)
79b58555f1SChristophe Gouault {
80b58555f1SChristophe Gouault 	return __xfrm6_pref_hash(daddr, dbits) ^
81b58555f1SChristophe Gouault 	       __xfrm6_pref_hash(saddr, sbits);
82b58555f1SChristophe Gouault }
83b58555f1SChristophe Gouault 
__xfrm_dst_hash(const xfrm_address_t * daddr,const xfrm_address_t * saddr,u32 reqid,unsigned short family,unsigned int hmask)845f803b58SDavid S. Miller static inline unsigned int __xfrm_dst_hash(const xfrm_address_t *daddr,
855f803b58SDavid S. Miller 					   const xfrm_address_t *saddr,
8644e36b42SDavid S. Miller 					   u32 reqid, unsigned short family,
8744e36b42SDavid S. Miller 					   unsigned int hmask)
8844e36b42SDavid S. Miller {
8944e36b42SDavid S. Miller 	unsigned int h = family ^ reqid;
9044e36b42SDavid S. Miller 	switch (family) {
9144e36b42SDavid S. Miller 	case AF_INET:
9244e36b42SDavid S. Miller 		h ^= __xfrm4_daddr_saddr_hash(daddr, saddr);
9344e36b42SDavid S. Miller 		break;
9444e36b42SDavid S. Miller 	case AF_INET6:
9544e36b42SDavid S. Miller 		h ^= __xfrm6_daddr_saddr_hash(daddr, saddr);
9644e36b42SDavid S. Miller 		break;
9744e36b42SDavid S. Miller 	}
9844e36b42SDavid S. Miller 	return (h ^ (h >> 16)) & hmask;
9944e36b42SDavid S. Miller }
10044e36b42SDavid S. Miller 
__xfrm_src_hash(const xfrm_address_t * daddr,const xfrm_address_t * saddr,unsigned short family,unsigned int hmask)10195c96174SEric Dumazet static inline unsigned int __xfrm_src_hash(const xfrm_address_t *daddr,
1025f803b58SDavid S. Miller 					   const xfrm_address_t *saddr,
10344e36b42SDavid S. Miller 					   unsigned short family,
10444e36b42SDavid S. Miller 					   unsigned int hmask)
10544e36b42SDavid S. Miller {
10644e36b42SDavid S. Miller 	unsigned int h = family;
10744e36b42SDavid S. Miller 	switch (family) {
10844e36b42SDavid S. Miller 	case AF_INET:
109667bbcb6SMasahide NAKAMURA 		h ^= __xfrm4_daddr_saddr_hash(daddr, saddr);
11044e36b42SDavid S. Miller 		break;
11144e36b42SDavid S. Miller 	case AF_INET6:
112667bbcb6SMasahide NAKAMURA 		h ^= __xfrm6_daddr_saddr_hash(daddr, saddr);
11344e36b42SDavid S. Miller 		break;
114ccbd6a5aSJoe Perches 	}
11544e36b42SDavid S. Miller 	return (h ^ (h >> 16)) & hmask;
11644e36b42SDavid S. Miller }
11744e36b42SDavid S. Miller 
11844e36b42SDavid S. Miller static inline unsigned int
__xfrm_spi_hash(const xfrm_address_t * daddr,__be32 spi,u8 proto,unsigned short family,unsigned int hmask)1195f803b58SDavid S. Miller __xfrm_spi_hash(const xfrm_address_t *daddr, __be32 spi, u8 proto,
1205f803b58SDavid S. Miller 		unsigned short family, unsigned int hmask)
12144e36b42SDavid S. Miller {
1228122adf0SAl Viro 	unsigned int h = (__force u32)spi ^ proto;
12344e36b42SDavid S. Miller 	switch (family) {
12444e36b42SDavid S. Miller 	case AF_INET:
12544e36b42SDavid S. Miller 		h ^= __xfrm4_addr_hash(daddr);
12644e36b42SDavid S. Miller 		break;
12744e36b42SDavid S. Miller 	case AF_INET6:
12844e36b42SDavid S. Miller 		h ^= __xfrm6_addr_hash(daddr);
12944e36b42SDavid S. Miller 		break;
13044e36b42SDavid S. Miller 	}
13144e36b42SDavid S. Miller 	return (h ^ (h >> 10) ^ (h >> 20)) & hmask;
13244e36b42SDavid S. Miller }
13344e36b42SDavid S. Miller 
134*fe9f1d87SSabrina Dubroca static inline unsigned int
__xfrm_seq_hash(u32 seq,unsigned int hmask)135*fe9f1d87SSabrina Dubroca __xfrm_seq_hash(u32 seq, unsigned int hmask)
136*fe9f1d87SSabrina Dubroca {
137*fe9f1d87SSabrina Dubroca 	unsigned int h = seq;
138*fe9f1d87SSabrina Dubroca 	return (h ^ (h >> 10) ^ (h >> 20)) & hmask;
139*fe9f1d87SSabrina Dubroca }
140*fe9f1d87SSabrina Dubroca 
__idx_hash(u32 index,unsigned int hmask)14144e36b42SDavid S. Miller static inline unsigned int __idx_hash(u32 index, unsigned int hmask)
14244e36b42SDavid S. Miller {
14344e36b42SDavid S. Miller 	return (index ^ (index >> 8)) & hmask;
14444e36b42SDavid S. Miller }
14544e36b42SDavid S. Miller 
__sel_hash(const struct xfrm_selector * sel,unsigned short family,unsigned int hmask,u8 dbits,u8 sbits)1465f803b58SDavid S. Miller static inline unsigned int __sel_hash(const struct xfrm_selector *sel,
147b58555f1SChristophe Gouault 				      unsigned short family, unsigned int hmask,
148b58555f1SChristophe Gouault 				      u8 dbits, u8 sbits)
14944e36b42SDavid S. Miller {
1505f803b58SDavid S. Miller 	const xfrm_address_t *daddr = &sel->daddr;
1515f803b58SDavid S. Miller 	const xfrm_address_t *saddr = &sel->saddr;
15244e36b42SDavid S. Miller 	unsigned int h = 0;
15344e36b42SDavid S. Miller 
15444e36b42SDavid S. Miller 	switch (family) {
15544e36b42SDavid S. Miller 	case AF_INET:
156b58555f1SChristophe Gouault 		if (sel->prefixlen_d < dbits ||
157b58555f1SChristophe Gouault 		    sel->prefixlen_s < sbits)
15844e36b42SDavid S. Miller 			return hmask + 1;
15944e36b42SDavid S. Miller 
160b58555f1SChristophe Gouault 		h = __xfrm4_dpref_spref_hash(daddr, saddr, dbits, sbits);
16144e36b42SDavid S. Miller 		break;
16244e36b42SDavid S. Miller 
16344e36b42SDavid S. Miller 	case AF_INET6:
164b58555f1SChristophe Gouault 		if (sel->prefixlen_d < dbits ||
165b58555f1SChristophe Gouault 		    sel->prefixlen_s < sbits)
16644e36b42SDavid S. Miller 			return hmask + 1;
16744e36b42SDavid S. Miller 
168b58555f1SChristophe Gouault 		h = __xfrm6_dpref_spref_hash(daddr, saddr, dbits, sbits);
16944e36b42SDavid S. Miller 		break;
170ccbd6a5aSJoe Perches 	}
17144e36b42SDavid S. Miller 	h ^= (h >> 16);
17244e36b42SDavid S. Miller 	return h & hmask;
17344e36b42SDavid S. Miller }
17444e36b42SDavid S. Miller 
__addr_hash(const xfrm_address_t * daddr,const xfrm_address_t * saddr,unsigned short family,unsigned int hmask,u8 dbits,u8 sbits)1755f803b58SDavid S. Miller static inline unsigned int __addr_hash(const xfrm_address_t *daddr,
1765f803b58SDavid S. Miller 				       const xfrm_address_t *saddr,
177b58555f1SChristophe Gouault 				       unsigned short family,
178b58555f1SChristophe Gouault 				       unsigned int hmask,
179b58555f1SChristophe Gouault 				       u8 dbits, u8 sbits)
18044e36b42SDavid S. Miller {
18144e36b42SDavid S. Miller 	unsigned int h = 0;
18244e36b42SDavid S. Miller 
18344e36b42SDavid S. Miller 	switch (family) {
18444e36b42SDavid S. Miller 	case AF_INET:
185b58555f1SChristophe Gouault 		h = __xfrm4_dpref_spref_hash(daddr, saddr, dbits, sbits);
18644e36b42SDavid S. Miller 		break;
18744e36b42SDavid S. Miller 
18844e36b42SDavid S. Miller 	case AF_INET6:
189b58555f1SChristophe Gouault 		h = __xfrm6_dpref_spref_hash(daddr, saddr, dbits, sbits);
19044e36b42SDavid S. Miller 		break;
191ccbd6a5aSJoe Perches 	}
19244e36b42SDavid S. Miller 	h ^= (h >> 16);
19344e36b42SDavid S. Miller 	return h & hmask;
19444e36b42SDavid S. Miller }
19544e36b42SDavid S. Miller 
196c1b1203dSJoe Perches struct hlist_head *xfrm_hash_alloc(unsigned int sz);
197c1b1203dSJoe Perches void xfrm_hash_free(struct hlist_head *n, unsigned int sz);
19844e36b42SDavid S. Miller 
19944e36b42SDavid S. Miller #endif /* _XFRM_HASH_H */
200