inet6_hashtables.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 lport + __inet6_ehashfn(lhash, 0, 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. 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. const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
  50. const struct hlist_nulls_node *node;
  51. struct inet_ehash_bucket *head;
  52. struct inet_hashinfo *hashinfo;
  53. unsigned int hash, slot;
  54. struct sock *sk;
  55. hashinfo = net->ipv4.tcp_death_row.hashinfo;
  56. hash = inet6_ehashfn(net, daddr, hnum, saddr, sport);
  57. slot = hash & hashinfo->ehash_mask;
  58. 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) &&
  88. READ_ONCE(inet_sk(sk)->inet_num) == hnum &&
  89. sk->sk_family == PF_INET6) {
  90. if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
  91. return -1;
  92. if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
  93. return -1;
  94. score = sk->sk_bound_dev_if ? 2 : 1;
  95. if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
  96. score++;
  97. }
  98. return score;
  99. }
  100. /**
  101. * inet6_lookup_reuseport() - execute reuseport logic on AF_INET6 socket if necessary.
  102. * @net: network namespace.
  103. * @sk: AF_INET6 socket, must be in TCP_LISTEN state for TCP or TCP_CLOSE for UDP.
  104. * @skb: context for a potential SK_REUSEPORT program.
  105. * @doff: header offset.
  106. * @saddr: source address.
  107. * @sport: source port.
  108. * @daddr: destination address.
  109. * @hnum: destination port in host byte order.
  110. * @ehashfn: hash function used to generate the fallback hash.
  111. *
  112. * Return: NULL if sk doesn't have SO_REUSEPORT set, otherwise a pointer to
  113. * the selected sock or an error.
  114. */
  115. struct sock *inet6_lookup_reuseport(const struct net *net, struct sock *sk,
  116. struct sk_buff *skb, int doff,
  117. const struct in6_addr *saddr,
  118. __be16 sport,
  119. const struct in6_addr *daddr,
  120. unsigned short hnum,
  121. inet6_ehashfn_t *ehashfn)
  122. {
  123. struct sock *reuse_sk = NULL;
  124. u32 phash;
  125. if (sk->sk_reuseport) {
  126. phash = INDIRECT_CALL_INET(ehashfn, udp6_ehashfn, inet6_ehashfn,
  127. net, daddr, hnum, saddr, sport);
  128. reuse_sk = reuseport_select_sock(sk, phash, skb, doff);
  129. }
  130. return reuse_sk;
  131. }
  132. EXPORT_SYMBOL_GPL(inet6_lookup_reuseport);
  133. /* called with rcu_read_lock() */
  134. static struct sock *inet6_lhash2_lookup(const struct net *net,
  135. struct inet_listen_hashbucket *ilb2,
  136. struct sk_buff *skb, int doff,
  137. const struct in6_addr *saddr,
  138. const __be16 sport, const struct in6_addr *daddr,
  139. const unsigned short hnum, const int dif, const int sdif)
  140. {
  141. struct sock *sk, *result = NULL;
  142. struct hlist_nulls_node *node;
  143. int score, hiscore = 0;
  144. sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) {
  145. score = compute_score(sk, net, hnum, daddr, dif, sdif);
  146. if (score > hiscore) {
  147. result = inet6_lookup_reuseport(net, sk, skb, doff,
  148. saddr, sport, daddr, hnum, inet6_ehashfn);
  149. if (result)
  150. return result;
  151. result = sk;
  152. hiscore = score;
  153. }
  154. }
  155. return result;
  156. }
  157. struct sock *inet6_lookup_run_sk_lookup(const struct net *net,
  158. int protocol,
  159. struct sk_buff *skb, int doff,
  160. const struct in6_addr *saddr,
  161. const __be16 sport,
  162. const struct in6_addr *daddr,
  163. const u16 hnum, const int dif,
  164. inet6_ehashfn_t *ehashfn)
  165. {
  166. struct sock *sk, *reuse_sk;
  167. bool no_reuseport;
  168. no_reuseport = bpf_sk_lookup_run_v6(net, protocol, saddr, sport,
  169. daddr, hnum, dif, &sk);
  170. if (no_reuseport || IS_ERR_OR_NULL(sk))
  171. return sk;
  172. reuse_sk = inet6_lookup_reuseport(net, sk, skb, doff,
  173. saddr, sport, daddr, hnum, ehashfn);
  174. if (reuse_sk)
  175. sk = reuse_sk;
  176. return sk;
  177. }
  178. EXPORT_SYMBOL_GPL(inet6_lookup_run_sk_lookup);
  179. struct sock *inet6_lookup_listener(const struct net *net,
  180. struct sk_buff *skb, int doff,
  181. const struct in6_addr *saddr,
  182. const __be16 sport,
  183. const struct in6_addr *daddr,
  184. const unsigned short hnum,
  185. const int dif, const int sdif)
  186. {
  187. struct inet_listen_hashbucket *ilb2;
  188. struct inet_hashinfo *hashinfo;
  189. struct sock *result = NULL;
  190. unsigned int hash2;
  191. /* Lookup redirect from BPF */
  192. if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
  193. result = inet6_lookup_run_sk_lookup(net, IPPROTO_TCP, skb, doff,
  194. saddr, sport, daddr, hnum, dif,
  195. inet6_ehashfn);
  196. if (result)
  197. goto done;
  198. }
  199. hashinfo = net->ipv4.tcp_death_row.hashinfo;
  200. hash2 = ipv6_portaddr_hash(net, daddr, hnum);
  201. ilb2 = inet_lhash2_bucket(hashinfo, hash2);
  202. result = inet6_lhash2_lookup(net, ilb2, skb, doff,
  203. saddr, sport, daddr, hnum,
  204. dif, sdif);
  205. if (result)
  206. goto done;
  207. /* Lookup lhash2 with in6addr_any */
  208. hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
  209. ilb2 = inet_lhash2_bucket(hashinfo, hash2);
  210. result = inet6_lhash2_lookup(net, ilb2, skb, doff,
  211. saddr, sport, &in6addr_any, hnum,
  212. dif, sdif);
  213. done:
  214. if (IS_ERR(result))
  215. return NULL;
  216. return result;
  217. }
  218. EXPORT_SYMBOL_GPL(inet6_lookup_listener);
  219. struct sock *inet6_lookup(const struct net *net,
  220. struct sk_buff *skb, int doff,
  221. const struct in6_addr *saddr, const __be16 sport,
  222. const struct in6_addr *daddr, const __be16 dport,
  223. const int dif)
  224. {
  225. struct sock *sk;
  226. bool refcounted;
  227. sk = __inet6_lookup(net, skb, doff, saddr, sport, daddr,
  228. ntohs(dport), dif, 0, &refcounted);
  229. if (sk && !refcounted && !refcount_inc_not_zero(&sk->sk_refcnt))
  230. sk = NULL;
  231. return sk;
  232. }
  233. EXPORT_SYMBOL_GPL(inet6_lookup);
  234. static int __inet6_check_established(struct inet_timewait_death_row *death_row,
  235. struct sock *sk, const __u16 lport,
  236. struct inet_timewait_sock **twp,
  237. bool rcu_lookup,
  238. u32 hash)
  239. {
  240. struct inet_hashinfo *hinfo = death_row->hashinfo;
  241. struct inet_sock *inet = inet_sk(sk);
  242. const struct in6_addr *daddr = &sk->sk_v6_rcv_saddr;
  243. const struct in6_addr *saddr = &sk->sk_v6_daddr;
  244. const int dif = sk->sk_bound_dev_if;
  245. struct net *net = sock_net(sk);
  246. const int sdif = l3mdev_master_ifindex_by_index(net, dif);
  247. const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
  248. struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
  249. struct inet_timewait_sock *tw = NULL;
  250. const struct hlist_nulls_node *node;
  251. struct sock *sk2;
  252. spinlock_t *lock;
  253. if (rcu_lookup) {
  254. sk_nulls_for_each(sk2, node, &head->chain) {
  255. if (sk2->sk_hash != hash ||
  256. !inet6_match(net, sk2, saddr, daddr,
  257. ports, dif, sdif))
  258. continue;
  259. if (sk2->sk_state == TCP_TIME_WAIT)
  260. break;
  261. return -EADDRNOTAVAIL;
  262. }
  263. return 0;
  264. }
  265. lock = inet_ehash_lockp(hinfo, hash);
  266. spin_lock(lock);
  267. sk_nulls_for_each(sk2, node, &head->chain) {
  268. if (sk2->sk_hash != hash)
  269. continue;
  270. if (likely(inet6_match(net, sk2, saddr, daddr, ports,
  271. dif, sdif))) {
  272. if (sk2->sk_state == TCP_TIME_WAIT) {
  273. tw = inet_twsk(sk2);
  274. if (tcp_twsk_unique(sk, sk2, twp))
  275. break;
  276. }
  277. goto not_unique;
  278. }
  279. }
  280. /* Must record num and sport now. Otherwise we will see
  281. * in hash table socket with a funny identity.
  282. */
  283. inet->inet_num = lport;
  284. inet->inet_sport = htons(lport);
  285. sk->sk_hash = hash;
  286. WARN_ON(!sk_unhashed(sk));
  287. __sk_nulls_add_node_rcu(sk, &head->chain);
  288. if (tw) {
  289. sk_nulls_del_node_init_rcu((struct sock *)tw);
  290. __NET_INC_STATS(net, LINUX_MIB_TIMEWAITRECYCLED);
  291. }
  292. spin_unlock(lock);
  293. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  294. if (twp) {
  295. *twp = tw;
  296. } else if (tw) {
  297. /* Silly. Should hash-dance instead... */
  298. inet_twsk_deschedule_put(tw);
  299. }
  300. return 0;
  301. not_unique:
  302. spin_unlock(lock);
  303. return -EADDRNOTAVAIL;
  304. }
  305. static u64 inet6_sk_port_offset(const struct sock *sk)
  306. {
  307. const struct inet_sock *inet = inet_sk(sk);
  308. return secure_ipv6_port_ephemeral(sk->sk_v6_rcv_saddr.s6_addr32,
  309. sk->sk_v6_daddr.s6_addr32,
  310. inet->inet_dport);
  311. }
  312. int inet6_hash_connect(struct inet_timewait_death_row *death_row,
  313. struct sock *sk)
  314. {
  315. const struct in6_addr *daddr = &sk->sk_v6_rcv_saddr;
  316. const struct in6_addr *saddr = &sk->sk_v6_daddr;
  317. const struct inet_sock *inet = inet_sk(sk);
  318. const struct net *net = sock_net(sk);
  319. u64 port_offset = 0;
  320. u32 hash_port0;
  321. if (!inet_sk(sk)->inet_num)
  322. port_offset = inet6_sk_port_offset(sk);
  323. hash_port0 = inet6_ehashfn(net, daddr, 0, saddr, inet->inet_dport);
  324. return __inet_hash_connect(death_row, sk, port_offset, hash_port0,
  325. __inet6_check_established);
  326. }
  327. EXPORT_SYMBOL_GPL(inet6_hash_connect);