inet6_hashtables.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * Authors: Lotsa people, from code originally in tcp
  8. */
  9. #ifndef _INET6_HASHTABLES_H
  10. #define _INET6_HASHTABLES_H
  11. #if IS_ENABLED(CONFIG_IPV6)
  12. #include <linux/in6.h>
  13. #include <linux/ipv6.h>
  14. #include <linux/types.h>
  15. #include <linux/jhash.h>
  16. #include <net/inet_sock.h>
  17. #include <net/ipv6.h>
  18. #include <net/netns/hash.h>
  19. struct inet_hashinfo;
  20. static inline unsigned int __inet6_ehashfn(const u32 lhash,
  21. const u16 lport,
  22. const u32 fhash,
  23. const __be16 fport,
  24. const u32 initval)
  25. {
  26. const u32 ports = (((u32)lport) << 16) | (__force u32)fport;
  27. return jhash_3words(lhash, fhash, ports, initval);
  28. }
  29. /*
  30. * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
  31. * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
  32. *
  33. * The sockhash lock must be held as a reader here.
  34. */
  35. struct sock *__inet6_lookup_established(struct net *net,
  36. struct inet_hashinfo *hashinfo,
  37. const struct in6_addr *saddr,
  38. const __be16 sport,
  39. const struct in6_addr *daddr,
  40. const u16 hnum, const int dif,
  41. const int sdif);
  42. struct sock *inet6_lookup_listener(struct net *net,
  43. struct inet_hashinfo *hashinfo,
  44. struct sk_buff *skb, int doff,
  45. const struct in6_addr *saddr,
  46. const __be16 sport,
  47. const struct in6_addr *daddr,
  48. const unsigned short hnum,
  49. const int dif, const int sdif);
  50. static inline struct sock *__inet6_lookup(struct net *net,
  51. struct inet_hashinfo *hashinfo,
  52. struct sk_buff *skb, int doff,
  53. const struct in6_addr *saddr,
  54. const __be16 sport,
  55. const struct in6_addr *daddr,
  56. const u16 hnum,
  57. const int dif, const int sdif,
  58. bool *refcounted)
  59. {
  60. struct sock *sk = __inet6_lookup_established(net, hashinfo, saddr,
  61. sport, daddr, hnum,
  62. dif, sdif);
  63. *refcounted = true;
  64. if (sk)
  65. return sk;
  66. *refcounted = false;
  67. return inet6_lookup_listener(net, hashinfo, skb, doff, saddr, sport,
  68. daddr, hnum, dif, sdif);
  69. }
  70. static inline struct sock *__inet6_lookup_skb(struct inet_hashinfo *hashinfo,
  71. struct sk_buff *skb, int doff,
  72. const __be16 sport,
  73. const __be16 dport,
  74. int iif, int sdif,
  75. bool *refcounted)
  76. {
  77. struct sock *sk = skb_steal_sock(skb, refcounted);
  78. if (sk)
  79. return sk;
  80. return __inet6_lookup(dev_net(skb_dst(skb)->dev), hashinfo, skb,
  81. doff, &ipv6_hdr(skb)->saddr, sport,
  82. &ipv6_hdr(skb)->daddr, ntohs(dport),
  83. iif, sdif, refcounted);
  84. }
  85. struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo,
  86. struct sk_buff *skb, int doff,
  87. const struct in6_addr *saddr, const __be16 sport,
  88. const struct in6_addr *daddr, const __be16 dport,
  89. const int dif);
  90. int inet6_hash(struct sock *sk);
  91. static inline bool inet6_match(struct net *net, const struct sock *sk,
  92. const struct in6_addr *saddr,
  93. const struct in6_addr *daddr,
  94. const __portpair ports,
  95. const int dif, const int sdif)
  96. {
  97. if (!net_eq(sock_net(sk), net) ||
  98. sk->sk_family != AF_INET6 ||
  99. sk->sk_portpair != ports ||
  100. !ipv6_addr_equal(&sk->sk_v6_daddr, saddr) ||
  101. !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
  102. return false;
  103. /* READ_ONCE() paired with WRITE_ONCE() in sock_bindtoindex_locked() */
  104. return inet_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif,
  105. sdif);
  106. }
  107. #endif /* IS_ENABLED(CONFIG_IPV6) */
  108. #endif /* _INET6_HASHTABLES_H */