inet6_hashtables.c 10.0 KB

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