xfrm_hash.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #ifndef _XFRM_HASH_H
  2. #define _XFRM_HASH_H
  3. #include <linux/xfrm.h>
  4. #include <linux/socket.h>
  5. static inline unsigned int __xfrm4_addr_hash(const xfrm_address_t *addr)
  6. {
  7. return ntohl(addr->a4);
  8. }
  9. static inline unsigned int __xfrm6_addr_hash(const xfrm_address_t *addr)
  10. {
  11. return ntohl(addr->a6[2] ^ addr->a6[3]);
  12. }
  13. static inline unsigned int __xfrm4_daddr_saddr_hash(const xfrm_address_t *daddr,
  14. const xfrm_address_t *saddr)
  15. {
  16. u32 sum = (__force u32)daddr->a4 + (__force u32)saddr->a4;
  17. return ntohl((__force __be32)sum);
  18. }
  19. static inline unsigned int __xfrm6_daddr_saddr_hash(const xfrm_address_t *daddr,
  20. const xfrm_address_t *saddr)
  21. {
  22. return ntohl(daddr->a6[2] ^ daddr->a6[3] ^
  23. saddr->a6[2] ^ saddr->a6[3]);
  24. }
  25. static inline unsigned int __xfrm_dst_hash(const xfrm_address_t *daddr,
  26. const xfrm_address_t *saddr,
  27. u32 reqid, unsigned short family,
  28. unsigned int hmask)
  29. {
  30. unsigned int h = family ^ reqid;
  31. switch (family) {
  32. case AF_INET:
  33. h ^= __xfrm4_daddr_saddr_hash(daddr, saddr);
  34. break;
  35. case AF_INET6:
  36. h ^= __xfrm6_daddr_saddr_hash(daddr, saddr);
  37. break;
  38. }
  39. return (h ^ (h >> 16)) & hmask;
  40. }
  41. static inline unsigned __xfrm_src_hash(const xfrm_address_t *daddr,
  42. const xfrm_address_t *saddr,
  43. unsigned short family,
  44. unsigned int hmask)
  45. {
  46. unsigned int h = family;
  47. switch (family) {
  48. case AF_INET:
  49. h ^= __xfrm4_daddr_saddr_hash(daddr, saddr);
  50. break;
  51. case AF_INET6:
  52. h ^= __xfrm6_daddr_saddr_hash(daddr, saddr);
  53. break;
  54. }
  55. return (h ^ (h >> 16)) & hmask;
  56. }
  57. static inline unsigned int
  58. __xfrm_spi_hash(const xfrm_address_t *daddr, __be32 spi, u8 proto,
  59. unsigned short family, unsigned int hmask)
  60. {
  61. unsigned int h = (__force u32)spi ^ proto;
  62. switch (family) {
  63. case AF_INET:
  64. h ^= __xfrm4_addr_hash(daddr);
  65. break;
  66. case AF_INET6:
  67. h ^= __xfrm6_addr_hash(daddr);
  68. break;
  69. }
  70. return (h ^ (h >> 10) ^ (h >> 20)) & hmask;
  71. }
  72. static inline unsigned int __idx_hash(u32 index, unsigned int hmask)
  73. {
  74. return (index ^ (index >> 8)) & hmask;
  75. }
  76. static inline unsigned int __sel_hash(const struct xfrm_selector *sel,
  77. unsigned short family, unsigned int hmask)
  78. {
  79. const xfrm_address_t *daddr = &sel->daddr;
  80. const xfrm_address_t *saddr = &sel->saddr;
  81. unsigned int h = 0;
  82. switch (family) {
  83. case AF_INET:
  84. if (sel->prefixlen_d != 32 ||
  85. sel->prefixlen_s != 32)
  86. return hmask + 1;
  87. h = __xfrm4_daddr_saddr_hash(daddr, saddr);
  88. break;
  89. case AF_INET6:
  90. if (sel->prefixlen_d != 128 ||
  91. sel->prefixlen_s != 128)
  92. return hmask + 1;
  93. h = __xfrm6_daddr_saddr_hash(daddr, saddr);
  94. break;
  95. }
  96. h ^= (h >> 16);
  97. return h & hmask;
  98. }
  99. static inline unsigned int __addr_hash(const xfrm_address_t *daddr,
  100. const xfrm_address_t *saddr,
  101. unsigned short family, unsigned int hmask)
  102. {
  103. unsigned int h = 0;
  104. switch (family) {
  105. case AF_INET:
  106. h = __xfrm4_daddr_saddr_hash(daddr, saddr);
  107. break;
  108. case AF_INET6:
  109. h = __xfrm6_daddr_saddr_hash(daddr, saddr);
  110. break;
  111. }
  112. h ^= (h >> 16);
  113. return h & hmask;
  114. }
  115. extern struct hlist_head *xfrm_hash_alloc(unsigned int sz);
  116. extern void xfrm_hash_free(struct hlist_head *n, unsigned int sz);
  117. #endif /* _XFRM_HASH_H */