inet6_hashtables.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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(const 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. typedef u32 (inet6_ehashfn_t)(const struct net *net,
  43. const struct in6_addr *laddr, const u16 lport,
  44. const struct in6_addr *faddr, const __be16 fport);
  45. inet6_ehashfn_t inet6_ehashfn;
  46. INDIRECT_CALLABLE_DECLARE(inet6_ehashfn_t udp6_ehashfn);
  47. struct sock *inet6_lookup_reuseport(const struct net *net, struct sock *sk,
  48. struct sk_buff *skb, int doff,
  49. const struct in6_addr *saddr,
  50. __be16 sport,
  51. const struct in6_addr *daddr,
  52. unsigned short hnum,
  53. inet6_ehashfn_t *ehashfn);
  54. struct sock *inet6_lookup_listener(const struct net *net,
  55. struct inet_hashinfo *hashinfo,
  56. struct sk_buff *skb, int doff,
  57. const struct in6_addr *saddr,
  58. const __be16 sport,
  59. const struct in6_addr *daddr,
  60. const unsigned short hnum,
  61. const int dif, const int sdif);
  62. struct sock *inet6_lookup_run_sk_lookup(const struct net *net,
  63. int protocol,
  64. struct sk_buff *skb, int doff,
  65. const struct in6_addr *saddr,
  66. const __be16 sport,
  67. const struct in6_addr *daddr,
  68. const u16 hnum, const int dif,
  69. inet6_ehashfn_t *ehashfn);
  70. static inline struct sock *__inet6_lookup(const struct net *net,
  71. struct inet_hashinfo *hashinfo,
  72. struct sk_buff *skb, int doff,
  73. const struct in6_addr *saddr,
  74. const __be16 sport,
  75. const struct in6_addr *daddr,
  76. const u16 hnum,
  77. const int dif, const int sdif,
  78. bool *refcounted)
  79. {
  80. struct sock *sk = __inet6_lookup_established(net, hashinfo, saddr,
  81. sport, daddr, hnum,
  82. dif, sdif);
  83. *refcounted = true;
  84. if (sk)
  85. return sk;
  86. *refcounted = false;
  87. return inet6_lookup_listener(net, hashinfo, skb, doff, saddr, sport,
  88. daddr, hnum, dif, sdif);
  89. }
  90. static inline
  91. struct sock *inet6_steal_sock(struct net *net, struct sk_buff *skb, int doff,
  92. const struct in6_addr *saddr, const __be16 sport,
  93. const struct in6_addr *daddr, const __be16 dport,
  94. bool *refcounted, inet6_ehashfn_t *ehashfn)
  95. {
  96. struct sock *sk, *reuse_sk;
  97. bool prefetched;
  98. sk = skb_steal_sock(skb, refcounted, &prefetched);
  99. if (!sk)
  100. return NULL;
  101. if (!prefetched || !sk_fullsock(sk))
  102. return sk;
  103. if (sk->sk_protocol == IPPROTO_TCP) {
  104. if (sk->sk_state != TCP_LISTEN)
  105. return sk;
  106. } else if (sk->sk_protocol == IPPROTO_UDP) {
  107. if (sk->sk_state != TCP_CLOSE)
  108. return sk;
  109. } else {
  110. return sk;
  111. }
  112. reuse_sk = inet6_lookup_reuseport(net, sk, skb, doff,
  113. saddr, sport, daddr, ntohs(dport),
  114. ehashfn);
  115. if (!reuse_sk)
  116. return sk;
  117. /* We've chosen a new reuseport sock which is never refcounted. This
  118. * implies that sk also isn't refcounted.
  119. */
  120. WARN_ON_ONCE(*refcounted);
  121. return reuse_sk;
  122. }
  123. static inline struct sock *__inet6_lookup_skb(struct inet_hashinfo *hashinfo,
  124. struct sk_buff *skb, int doff,
  125. const __be16 sport,
  126. const __be16 dport,
  127. int iif, int sdif,
  128. bool *refcounted)
  129. {
  130. struct net *net = dev_net_rcu(skb_dst(skb)->dev);
  131. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  132. struct sock *sk;
  133. sk = inet6_steal_sock(net, skb, doff, &ip6h->saddr, sport, &ip6h->daddr, dport,
  134. refcounted, inet6_ehashfn);
  135. if (IS_ERR(sk))
  136. return NULL;
  137. if (sk)
  138. return sk;
  139. return __inet6_lookup(net, hashinfo, skb,
  140. doff, &ip6h->saddr, sport,
  141. &ip6h->daddr, ntohs(dport),
  142. iif, sdif, refcounted);
  143. }
  144. struct sock *inet6_lookup(const struct net *net, struct inet_hashinfo *hashinfo,
  145. struct sk_buff *skb, int doff,
  146. const struct in6_addr *saddr, const __be16 sport,
  147. const struct in6_addr *daddr, const __be16 dport,
  148. const int dif);
  149. int inet6_hash(struct sock *sk);
  150. static inline bool inet6_match(const struct net *net, const struct sock *sk,
  151. const struct in6_addr *saddr,
  152. const struct in6_addr *daddr,
  153. const __portpair ports,
  154. const int dif, const int sdif)
  155. {
  156. if (!net_eq(sock_net(sk), net) ||
  157. sk->sk_family != AF_INET6 ||
  158. sk->sk_portpair != ports ||
  159. !ipv6_addr_equal(&sk->sk_v6_daddr, saddr) ||
  160. !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
  161. return false;
  162. /* READ_ONCE() paired with WRITE_ONCE() in sock_bindtoindex_locked() */
  163. return inet_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif,
  164. sdif);
  165. }
  166. #endif /* IS_ENABLED(CONFIG_IPV6) */
  167. #endif /* _INET6_HASHTABLES_H */