inet6_hashtables.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. * Generic INET6 transport hashtables
  8. *
  9. * Authors: Lotsa people, from code originally in tcp, generalised here
  10. * by Arnaldo Carvalho de Melo <[email protected]>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/random.h>
  14. #include <net/addrconf.h>
  15. #include <net/inet_connection_sock.h>
  16. #include <net/inet_hashtables.h>
  17. #include <net/inet6_hashtables.h>
  18. #include <net/secure_seq.h>
  19. #include <net/ip.h>
  20. #include <net/sock_reuseport.h>
  21. u32 inet6_ehashfn(const struct net *net,
  22. const struct in6_addr *laddr, const u16 lport,
  23. const struct in6_addr *faddr, const __be16 fport)
  24. {
  25. static u32 inet6_ehash_secret __read_mostly;
  26. static u32 ipv6_hash_secret __read_mostly;
  27. u32 lhash, fhash;
  28. net_get_random_once(&inet6_ehash_secret, sizeof(inet6_ehash_secret));
  29. net_get_random_once(&ipv6_hash_secret, sizeof(ipv6_hash_secret));
  30. lhash = (__force u32)laddr->s6_addr32[3];
  31. fhash = __ipv6_addr_jhash(faddr, ipv6_hash_secret);
  32. return __inet6_ehashfn(lhash, lport, fhash, fport,
  33. inet6_ehash_secret + net_hash_mix(net));
  34. }
  35. /*
  36. * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
  37. * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
  38. *
  39. * The sockhash lock must be held as a reader here.
  40. */
  41. struct sock *__inet6_lookup_established(struct net *net,
  42. struct inet_hashinfo *hashinfo,
  43. const struct in6_addr *saddr,
  44. const __be16 sport,
  45. const struct in6_addr *daddr,
  46. const u16 hnum,
  47. const int dif, const int sdif)
  48. {
  49. struct sock *sk;
  50. const struct hlist_nulls_node *node;
  51. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  52. /* Optimize here for direct hit, only listening connections can
  53. * have wildcards anyways.
  54. */
  55. unsigned int hash = inet6_ehashfn(net, daddr, hnum, saddr, sport);
  56. unsigned int slot = hash & hashinfo->ehash_mask;
  57. struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
  58. begin:
  59. sk_nulls_for_each_rcu(sk, node, &head->chain) {
  60. if (sk->sk_hash != hash)
  61. continue;
  62. if (!inet6_match(net, sk, saddr, daddr, ports, dif, sdif))
  63. continue;
  64. if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
  65. goto out;
  66. if (unlikely(!inet6_match(net, sk, saddr, daddr, ports, dif, sdif))) {
  67. sock_gen_put(sk);
  68. goto begin;
  69. }
  70. goto found;
  71. }
  72. if (get_nulls_value(node) != slot)
  73. goto begin;
  74. out:
  75. sk = NULL;
  76. found:
  77. return sk;
  78. }
  79. EXPORT_SYMBOL(__inet6_lookup_established);
  80. static inline int compute_score(struct sock *sk, struct net *net,
  81. const unsigned short hnum,
  82. const struct in6_addr *daddr,
  83. const int dif, const int sdif)
  84. {
  85. int score = -1;
  86. if (net_eq(sock_net(sk), net) && inet_sk(sk)->inet_num == hnum &&
  87. sk->sk_family == PF_INET6) {
  88. if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
  89. return -1;
  90. if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
  91. return -1;
  92. score = sk->sk_bound_dev_if ? 2 : 1;
  93. if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
  94. score++;
  95. }
  96. return score;
  97. }
  98. static inline struct sock *lookup_reuseport(struct net *net, struct sock *sk,
  99. struct sk_buff *skb, int doff,
  100. const struct in6_addr *saddr,
  101. __be16 sport,
  102. const struct in6_addr *daddr,
  103. unsigned short hnum)
  104. {
  105. struct sock *reuse_sk = NULL;
  106. u32 phash;
  107. if (sk->sk_reuseport) {
  108. phash = inet6_ehashfn(net, daddr, hnum, saddr, sport);
  109. reuse_sk = reuseport_select_sock(sk, phash, skb, doff);
  110. }
  111. return reuse_sk;
  112. }
  113. /* called with rcu_read_lock() */
  114. static struct sock *inet6_lhash2_lookup(struct net *net,
  115. struct inet_listen_hashbucket *ilb2,
  116. struct sk_buff *skb, int doff,
  117. const struct in6_addr *saddr,
  118. const __be16 sport, const struct in6_addr *daddr,
  119. const unsigned short hnum, const int dif, const int sdif)
  120. {
  121. struct sock *sk, *result = NULL;
  122. struct hlist_nulls_node *node;
  123. int score, hiscore = 0;
  124. sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) {
  125. score = compute_score(sk, net, hnum, daddr, dif, sdif);
  126. if (score > hiscore) {
  127. result = lookup_reuseport(net, sk, skb, doff,
  128. saddr, sport, daddr, hnum);
  129. if (result)
  130. return result;
  131. result = sk;
  132. hiscore = score;
  133. }
  134. }
  135. return result;
  136. }
  137. static inline struct sock *inet6_lookup_run_bpf(struct net *net,
  138. struct inet_hashinfo *hashinfo,
  139. struct sk_buff *skb, int doff,
  140. const struct in6_addr *saddr,
  141. const __be16 sport,
  142. const struct in6_addr *daddr,
  143. const u16 hnum, const int dif)
  144. {
  145. struct sock *sk, *reuse_sk;
  146. bool no_reuseport;
  147. if (hashinfo != net->ipv4.tcp_death_row.hashinfo)
  148. return NULL; /* only TCP is supported */
  149. no_reuseport = bpf_sk_lookup_run_v6(net, IPPROTO_TCP, saddr, sport,
  150. daddr, hnum, dif, &sk);
  151. if (no_reuseport || IS_ERR_OR_NULL(sk))
  152. return sk;
  153. reuse_sk = lookup_reuseport(net, sk, skb, doff, saddr, sport, daddr, hnum);
  154. if (reuse_sk)
  155. sk = reuse_sk;
  156. return sk;
  157. }
  158. struct sock *inet6_lookup_listener(struct net *net,
  159. struct inet_hashinfo *hashinfo,
  160. struct sk_buff *skb, int doff,
  161. const struct in6_addr *saddr,
  162. const __be16 sport, const struct in6_addr *daddr,
  163. const unsigned short hnum, const int dif, const int sdif)
  164. {
  165. struct inet_listen_hashbucket *ilb2;
  166. struct sock *result = NULL;
  167. unsigned int hash2;
  168. /* Lookup redirect from BPF */
  169. if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
  170. result = inet6_lookup_run_bpf(net, hashinfo, skb, doff,
  171. saddr, sport, daddr, hnum, dif);
  172. if (result)
  173. goto done;
  174. }
  175. hash2 = ipv6_portaddr_hash(net, daddr, hnum);
  176. ilb2 = inet_lhash2_bucket(hashinfo, hash2);
  177. result = inet6_lhash2_lookup(net, ilb2, skb, doff,
  178. saddr, sport, daddr, hnum,
  179. dif, sdif);
  180. if (result)
  181. goto done;
  182. /* Lookup lhash2 with in6addr_any */
  183. hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
  184. ilb2 = inet_lhash2_bucket(hashinfo, hash2);
  185. result = inet6_lhash2_lookup(net, ilb2, skb, doff,
  186. saddr, sport, &in6addr_any, hnum,
  187. dif, sdif);
  188. done:
  189. if (IS_ERR(result))
  190. return NULL;
  191. return result;
  192. }
  193. EXPORT_SYMBOL_GPL(inet6_lookup_listener);
  194. struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo,
  195. struct sk_buff *skb, int doff,
  196. const struct in6_addr *saddr, const __be16 sport,
  197. const struct in6_addr *daddr, const __be16 dport,
  198. const int dif)
  199. {
  200. struct sock *sk;
  201. bool refcounted;
  202. sk = __inet6_lookup(net, hashinfo, skb, doff, saddr, sport, daddr,
  203. ntohs(dport), dif, 0, &refcounted);
  204. if (sk && !refcounted && !refcount_inc_not_zero(&sk->sk_refcnt))
  205. sk = NULL;
  206. return sk;
  207. }
  208. EXPORT_SYMBOL_GPL(inet6_lookup);
  209. static int __inet6_check_established(struct inet_timewait_death_row *death_row,
  210. struct sock *sk, const __u16 lport,
  211. struct inet_timewait_sock **twp)
  212. {
  213. struct inet_hashinfo *hinfo = death_row->hashinfo;
  214. struct inet_sock *inet = inet_sk(sk);
  215. const struct in6_addr *daddr = &sk->sk_v6_rcv_saddr;
  216. const struct in6_addr *saddr = &sk->sk_v6_daddr;
  217. const int dif = sk->sk_bound_dev_if;
  218. struct net *net = sock_net(sk);
  219. const int sdif = l3mdev_master_ifindex_by_index(net, dif);
  220. const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
  221. const unsigned int hash = inet6_ehashfn(net, daddr, lport, saddr,
  222. inet->inet_dport);
  223. struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
  224. spinlock_t *lock = inet_ehash_lockp(hinfo, hash);
  225. struct sock *sk2;
  226. const struct hlist_nulls_node *node;
  227. struct inet_timewait_sock *tw = NULL;
  228. spin_lock(lock);
  229. sk_nulls_for_each(sk2, node, &head->chain) {
  230. if (sk2->sk_hash != hash)
  231. continue;
  232. if (likely(inet6_match(net, sk2, saddr, daddr, ports,
  233. dif, sdif))) {
  234. if (sk2->sk_state == TCP_TIME_WAIT) {
  235. tw = inet_twsk(sk2);
  236. if (twsk_unique(sk, sk2, twp))
  237. break;
  238. }
  239. goto not_unique;
  240. }
  241. }
  242. /* Must record num and sport now. Otherwise we will see
  243. * in hash table socket with a funny identity.
  244. */
  245. inet->inet_num = lport;
  246. inet->inet_sport = htons(lport);
  247. sk->sk_hash = hash;
  248. WARN_ON(!sk_unhashed(sk));
  249. __sk_nulls_add_node_rcu(sk, &head->chain);
  250. if (tw) {
  251. sk_nulls_del_node_init_rcu((struct sock *)tw);
  252. __NET_INC_STATS(net, LINUX_MIB_TIMEWAITRECYCLED);
  253. }
  254. spin_unlock(lock);
  255. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  256. if (twp) {
  257. *twp = tw;
  258. } else if (tw) {
  259. /* Silly. Should hash-dance instead... */
  260. inet_twsk_deschedule_put(tw);
  261. }
  262. return 0;
  263. not_unique:
  264. spin_unlock(lock);
  265. return -EADDRNOTAVAIL;
  266. }
  267. static u64 inet6_sk_port_offset(const struct sock *sk)
  268. {
  269. const struct inet_sock *inet = inet_sk(sk);
  270. return secure_ipv6_port_ephemeral(sk->sk_v6_rcv_saddr.s6_addr32,
  271. sk->sk_v6_daddr.s6_addr32,
  272. inet->inet_dport);
  273. }
  274. int inet6_hash_connect(struct inet_timewait_death_row *death_row,
  275. struct sock *sk)
  276. {
  277. u64 port_offset = 0;
  278. if (!inet_sk(sk)->inet_num)
  279. port_offset = inet6_sk_port_offset(sk);
  280. return __inet_hash_connect(death_row, sk, port_offset,
  281. __inet6_check_established);
  282. }
  283. EXPORT_SYMBOL_GPL(inet6_hash_connect);
  284. int inet6_hash(struct sock *sk)
  285. {
  286. int err = 0;
  287. if (sk->sk_state != TCP_CLOSE)
  288. err = __inet_hash(sk, NULL);
  289. return err;
  290. }
  291. EXPORT_SYMBOL_GPL(inet6_hash);