xfrm_hash.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _XFRM_HASH_H
  3. #define _XFRM_HASH_H
  4. #include <linux/xfrm.h>
  5. #include <linux/socket.h>
  6. #include <linux/jhash.h>
  7. static inline unsigned int __xfrm4_addr_hash(const xfrm_address_t *addr)
  8. {
  9. return ntohl(addr->a4);
  10. }
  11. static inline unsigned int __xfrm6_addr_hash(const xfrm_address_t *addr)
  12. {
  13. return ntohl(addr->a6[2] ^ addr->a6[3]);
  14. }
  15. static inline unsigned int __xfrm4_daddr_saddr_hash(const xfrm_address_t *daddr,
  16. const xfrm_address_t *saddr)
  17. {
  18. u32 sum = (__force u32)daddr->a4 + (__force u32)saddr->a4;
  19. return ntohl((__force __be32)sum);
  20. }
  21. static inline unsigned int __xfrm6_daddr_saddr_hash(const xfrm_address_t *daddr,
  22. const xfrm_address_t *saddr)
  23. {
  24. return ntohl(daddr->a6[2] ^ daddr->a6[3] ^
  25. saddr->a6[2] ^ saddr->a6[3]);
  26. }
  27. static inline u32 __bits2mask32(__u8 bits)
  28. {
  29. u32 mask32 = 0xffffffff;
  30. if (bits == 0)
  31. mask32 = 0;
  32. else if (bits < 32)
  33. mask32 <<= (32 - bits);
  34. return mask32;
  35. }
  36. static inline unsigned int __xfrm4_dpref_spref_hash(const xfrm_address_t *daddr,
  37. const xfrm_address_t *saddr,
  38. __u8 dbits,
  39. __u8 sbits)
  40. {
  41. return jhash_2words(ntohl(daddr->a4) & __bits2mask32(dbits),
  42. ntohl(saddr->a4) & __bits2mask32(sbits),
  43. 0);
  44. }
  45. static inline unsigned int __xfrm6_pref_hash(const xfrm_address_t *addr,
  46. __u8 prefixlen)
  47. {
  48. unsigned int pdw;
  49. unsigned int pbi;
  50. u32 initval = 0;
  51. pdw = prefixlen >> 5; /* num of whole u32 in prefix */
  52. pbi = prefixlen & 0x1f; /* num of bits in incomplete u32 in prefix */
  53. if (pbi) {
  54. __be32 mask;
  55. mask = htonl((0xffffffff) << (32 - pbi));
  56. initval = (__force u32)(addr->a6[pdw] & mask);
  57. }
  58. return jhash2((__force u32 *)addr->a6, pdw, initval);
  59. }
  60. static inline unsigned int __xfrm6_dpref_spref_hash(const xfrm_address_t *daddr,
  61. const xfrm_address_t *saddr,
  62. __u8 dbits,
  63. __u8 sbits)
  64. {
  65. return __xfrm6_pref_hash(daddr, dbits) ^
  66. __xfrm6_pref_hash(saddr, sbits);
  67. }
  68. static inline unsigned int __xfrm_dst_hash(const xfrm_address_t *daddr,
  69. const xfrm_address_t *saddr,
  70. u32 reqid, unsigned short family,
  71. unsigned int hmask)
  72. {
  73. unsigned int h = family ^ reqid;
  74. switch (family) {
  75. case AF_INET:
  76. h ^= __xfrm4_daddr_saddr_hash(daddr, saddr);
  77. break;
  78. case AF_INET6:
  79. h ^= __xfrm6_daddr_saddr_hash(daddr, saddr);
  80. break;
  81. }
  82. return (h ^ (h >> 16)) & hmask;
  83. }
  84. static inline unsigned int __xfrm_src_hash(const xfrm_address_t *daddr,
  85. const xfrm_address_t *saddr,
  86. unsigned short family,
  87. unsigned int hmask)
  88. {
  89. unsigned int h = family;
  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. static inline unsigned int
  101. __xfrm_spi_hash(const xfrm_address_t *daddr, __be32 spi, u8 proto,
  102. unsigned short family, unsigned int hmask)
  103. {
  104. unsigned int h = (__force u32)spi ^ proto;
  105. switch (family) {
  106. case AF_INET:
  107. h ^= __xfrm4_addr_hash(daddr);
  108. break;
  109. case AF_INET6:
  110. h ^= __xfrm6_addr_hash(daddr);
  111. break;
  112. }
  113. return (h ^ (h >> 10) ^ (h >> 20)) & hmask;
  114. }
  115. static inline unsigned int __idx_hash(u32 index, unsigned int hmask)
  116. {
  117. return (index ^ (index >> 8)) & hmask;
  118. }
  119. static inline unsigned int __sel_hash(const struct xfrm_selector *sel,
  120. unsigned short family, unsigned int hmask,
  121. u8 dbits, u8 sbits)
  122. {
  123. const xfrm_address_t *daddr = &sel->daddr;
  124. const xfrm_address_t *saddr = &sel->saddr;
  125. unsigned int h = 0;
  126. switch (family) {
  127. case AF_INET:
  128. if (sel->prefixlen_d < dbits ||
  129. sel->prefixlen_s < sbits)
  130. return hmask + 1;
  131. h = __xfrm4_dpref_spref_hash(daddr, saddr, dbits, sbits);
  132. break;
  133. case AF_INET6:
  134. if (sel->prefixlen_d < dbits ||
  135. sel->prefixlen_s < sbits)
  136. return hmask + 1;
  137. h = __xfrm6_dpref_spref_hash(daddr, saddr, dbits, sbits);
  138. break;
  139. }
  140. h ^= (h >> 16);
  141. return h & hmask;
  142. }
  143. static inline unsigned int __addr_hash(const xfrm_address_t *daddr,
  144. const xfrm_address_t *saddr,
  145. unsigned short family,
  146. unsigned int hmask,
  147. u8 dbits, u8 sbits)
  148. {
  149. unsigned int h = 0;
  150. switch (family) {
  151. case AF_INET:
  152. h = __xfrm4_dpref_spref_hash(daddr, saddr, dbits, sbits);
  153. break;
  154. case AF_INET6:
  155. h = __xfrm6_dpref_spref_hash(daddr, saddr, dbits, sbits);
  156. break;
  157. }
  158. h ^= (h >> 16);
  159. return h & hmask;
  160. }
  161. struct hlist_head *xfrm_hash_alloc(unsigned int sz);
  162. void xfrm_hash_free(struct hlist_head *n, unsigned int sz);
  163. #endif /* _XFRM_HASH_H */