inet6_hashtables.h 5.3 KB

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