162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * INET		An implementation of the TCP/IP protocol suite for the LINUX
462306a36Sopenharmony_ci *		operating system.  INET is implemented using the BSD Socket
562306a36Sopenharmony_ci *		interface as the means of communication with the user level.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci *		Generic INET transport hashtables
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Authors:	Lotsa people, from code originally in tcp
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/module.h>
1362306a36Sopenharmony_ci#include <linux/random.h>
1462306a36Sopenharmony_ci#include <linux/sched.h>
1562306a36Sopenharmony_ci#include <linux/slab.h>
1662306a36Sopenharmony_ci#include <linux/wait.h>
1762306a36Sopenharmony_ci#include <linux/vmalloc.h>
1862306a36Sopenharmony_ci#include <linux/memblock.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#include <net/addrconf.h>
2162306a36Sopenharmony_ci#include <net/inet_connection_sock.h>
2262306a36Sopenharmony_ci#include <net/inet_hashtables.h>
2362306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
2462306a36Sopenharmony_ci#include <net/inet6_hashtables.h>
2562306a36Sopenharmony_ci#endif
2662306a36Sopenharmony_ci#include <net/secure_seq.h>
2762306a36Sopenharmony_ci#include <net/ip.h>
2862306a36Sopenharmony_ci#include <net/tcp.h>
2962306a36Sopenharmony_ci#include <net/sock_reuseport.h>
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ciu32 inet_ehashfn(const struct net *net, const __be32 laddr,
3262306a36Sopenharmony_ci		 const __u16 lport, const __be32 faddr,
3362306a36Sopenharmony_ci		 const __be16 fport)
3462306a36Sopenharmony_ci{
3562306a36Sopenharmony_ci	static u32 inet_ehash_secret __read_mostly;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	net_get_random_once(&inet_ehash_secret, sizeof(inet_ehash_secret));
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	return __inet_ehashfn(laddr, lport, faddr, fport,
4062306a36Sopenharmony_ci			      inet_ehash_secret + net_hash_mix(net));
4162306a36Sopenharmony_ci}
4262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(inet_ehashfn);
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci/* This function handles inet_sock, but also timewait and request sockets
4562306a36Sopenharmony_ci * for IPv4/IPv6.
4662306a36Sopenharmony_ci */
4762306a36Sopenharmony_cistatic u32 sk_ehashfn(const struct sock *sk)
4862306a36Sopenharmony_ci{
4962306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
5062306a36Sopenharmony_ci	if (sk->sk_family == AF_INET6 &&
5162306a36Sopenharmony_ci	    !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
5262306a36Sopenharmony_ci		return inet6_ehashfn(sock_net(sk),
5362306a36Sopenharmony_ci				     &sk->sk_v6_rcv_saddr, sk->sk_num,
5462306a36Sopenharmony_ci				     &sk->sk_v6_daddr, sk->sk_dport);
5562306a36Sopenharmony_ci#endif
5662306a36Sopenharmony_ci	return inet_ehashfn(sock_net(sk),
5762306a36Sopenharmony_ci			    sk->sk_rcv_saddr, sk->sk_num,
5862306a36Sopenharmony_ci			    sk->sk_daddr, sk->sk_dport);
5962306a36Sopenharmony_ci}
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci/*
6262306a36Sopenharmony_ci * Allocate and initialize a new local port bind bucket.
6362306a36Sopenharmony_ci * The bindhash mutex for snum's hash chain must be held here.
6462306a36Sopenharmony_ci */
6562306a36Sopenharmony_cistruct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
6662306a36Sopenharmony_ci						 struct net *net,
6762306a36Sopenharmony_ci						 struct inet_bind_hashbucket *head,
6862306a36Sopenharmony_ci						 const unsigned short snum,
6962306a36Sopenharmony_ci						 int l3mdev)
7062306a36Sopenharmony_ci{
7162306a36Sopenharmony_ci	struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	if (tb) {
7462306a36Sopenharmony_ci		write_pnet(&tb->ib_net, net);
7562306a36Sopenharmony_ci		tb->l3mdev    = l3mdev;
7662306a36Sopenharmony_ci		tb->port      = snum;
7762306a36Sopenharmony_ci		tb->fastreuse = 0;
7862306a36Sopenharmony_ci		tb->fastreuseport = 0;
7962306a36Sopenharmony_ci		INIT_HLIST_HEAD(&tb->owners);
8062306a36Sopenharmony_ci		hlist_add_head(&tb->node, &head->chain);
8162306a36Sopenharmony_ci	}
8262306a36Sopenharmony_ci	return tb;
8362306a36Sopenharmony_ci}
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/*
8662306a36Sopenharmony_ci * Caller must hold hashbucket lock for this tb with local BH disabled
8762306a36Sopenharmony_ci */
8862306a36Sopenharmony_civoid inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb)
8962306a36Sopenharmony_ci{
9062306a36Sopenharmony_ci	if (hlist_empty(&tb->owners)) {
9162306a36Sopenharmony_ci		__hlist_del(&tb->node);
9262306a36Sopenharmony_ci		kmem_cache_free(cachep, tb);
9362306a36Sopenharmony_ci	}
9462306a36Sopenharmony_ci}
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_cibool inet_bind_bucket_match(const struct inet_bind_bucket *tb, const struct net *net,
9762306a36Sopenharmony_ci			    unsigned short port, int l3mdev)
9862306a36Sopenharmony_ci{
9962306a36Sopenharmony_ci	return net_eq(ib_net(tb), net) && tb->port == port &&
10062306a36Sopenharmony_ci		tb->l3mdev == l3mdev;
10162306a36Sopenharmony_ci}
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_cistatic void inet_bind2_bucket_init(struct inet_bind2_bucket *tb,
10462306a36Sopenharmony_ci				   struct net *net,
10562306a36Sopenharmony_ci				   struct inet_bind_hashbucket *head,
10662306a36Sopenharmony_ci				   unsigned short port, int l3mdev,
10762306a36Sopenharmony_ci				   const struct sock *sk)
10862306a36Sopenharmony_ci{
10962306a36Sopenharmony_ci	write_pnet(&tb->ib_net, net);
11062306a36Sopenharmony_ci	tb->l3mdev    = l3mdev;
11162306a36Sopenharmony_ci	tb->port      = port;
11262306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
11362306a36Sopenharmony_ci	tb->family    = sk->sk_family;
11462306a36Sopenharmony_ci	if (sk->sk_family == AF_INET6)
11562306a36Sopenharmony_ci		tb->v6_rcv_saddr = sk->sk_v6_rcv_saddr;
11662306a36Sopenharmony_ci	else
11762306a36Sopenharmony_ci#endif
11862306a36Sopenharmony_ci		tb->rcv_saddr = sk->sk_rcv_saddr;
11962306a36Sopenharmony_ci	INIT_HLIST_HEAD(&tb->owners);
12062306a36Sopenharmony_ci	INIT_HLIST_HEAD(&tb->deathrow);
12162306a36Sopenharmony_ci	hlist_add_head(&tb->node, &head->chain);
12262306a36Sopenharmony_ci}
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_cistruct inet_bind2_bucket *inet_bind2_bucket_create(struct kmem_cache *cachep,
12562306a36Sopenharmony_ci						   struct net *net,
12662306a36Sopenharmony_ci						   struct inet_bind_hashbucket *head,
12762306a36Sopenharmony_ci						   unsigned short port,
12862306a36Sopenharmony_ci						   int l3mdev,
12962306a36Sopenharmony_ci						   const struct sock *sk)
13062306a36Sopenharmony_ci{
13162306a36Sopenharmony_ci	struct inet_bind2_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	if (tb)
13462306a36Sopenharmony_ci		inet_bind2_bucket_init(tb, net, head, port, l3mdev, sk);
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	return tb;
13762306a36Sopenharmony_ci}
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci/* Caller must hold hashbucket lock for this tb with local BH disabled */
14062306a36Sopenharmony_civoid inet_bind2_bucket_destroy(struct kmem_cache *cachep, struct inet_bind2_bucket *tb)
14162306a36Sopenharmony_ci{
14262306a36Sopenharmony_ci	if (hlist_empty(&tb->owners) && hlist_empty(&tb->deathrow)) {
14362306a36Sopenharmony_ci		__hlist_del(&tb->node);
14462306a36Sopenharmony_ci		kmem_cache_free(cachep, tb);
14562306a36Sopenharmony_ci	}
14662306a36Sopenharmony_ci}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_cistatic bool inet_bind2_bucket_addr_match(const struct inet_bind2_bucket *tb2,
14962306a36Sopenharmony_ci					 const struct sock *sk)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
15262306a36Sopenharmony_ci	if (sk->sk_family != tb2->family) {
15362306a36Sopenharmony_ci		if (sk->sk_family == AF_INET)
15462306a36Sopenharmony_ci			return ipv6_addr_v4mapped(&tb2->v6_rcv_saddr) &&
15562306a36Sopenharmony_ci				tb2->v6_rcv_saddr.s6_addr32[3] == sk->sk_rcv_saddr;
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci		return ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr) &&
15862306a36Sopenharmony_ci			sk->sk_v6_rcv_saddr.s6_addr32[3] == tb2->rcv_saddr;
15962306a36Sopenharmony_ci	}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	if (sk->sk_family == AF_INET6)
16262306a36Sopenharmony_ci		return ipv6_addr_equal(&tb2->v6_rcv_saddr,
16362306a36Sopenharmony_ci				       &sk->sk_v6_rcv_saddr);
16462306a36Sopenharmony_ci#endif
16562306a36Sopenharmony_ci	return tb2->rcv_saddr == sk->sk_rcv_saddr;
16662306a36Sopenharmony_ci}
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_civoid inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
16962306a36Sopenharmony_ci		    struct inet_bind2_bucket *tb2, unsigned short port)
17062306a36Sopenharmony_ci{
17162306a36Sopenharmony_ci	inet_sk(sk)->inet_num = port;
17262306a36Sopenharmony_ci	sk_add_bind_node(sk, &tb->owners);
17362306a36Sopenharmony_ci	inet_csk(sk)->icsk_bind_hash = tb;
17462306a36Sopenharmony_ci	sk_add_bind2_node(sk, &tb2->owners);
17562306a36Sopenharmony_ci	inet_csk(sk)->icsk_bind2_hash = tb2;
17662306a36Sopenharmony_ci}
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci/*
17962306a36Sopenharmony_ci * Get rid of any references to a local port held by the given sock.
18062306a36Sopenharmony_ci */
18162306a36Sopenharmony_cistatic void __inet_put_port(struct sock *sk)
18262306a36Sopenharmony_ci{
18362306a36Sopenharmony_ci	struct inet_hashinfo *hashinfo = tcp_or_dccp_get_hashinfo(sk);
18462306a36Sopenharmony_ci	struct inet_bind_hashbucket *head, *head2;
18562306a36Sopenharmony_ci	struct net *net = sock_net(sk);
18662306a36Sopenharmony_ci	struct inet_bind_bucket *tb;
18762306a36Sopenharmony_ci	int bhash;
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	bhash = inet_bhashfn(net, inet_sk(sk)->inet_num, hashinfo->bhash_size);
19062306a36Sopenharmony_ci	head = &hashinfo->bhash[bhash];
19162306a36Sopenharmony_ci	head2 = inet_bhashfn_portaddr(hashinfo, sk, net, inet_sk(sk)->inet_num);
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	spin_lock(&head->lock);
19462306a36Sopenharmony_ci	tb = inet_csk(sk)->icsk_bind_hash;
19562306a36Sopenharmony_ci	__sk_del_bind_node(sk);
19662306a36Sopenharmony_ci	inet_csk(sk)->icsk_bind_hash = NULL;
19762306a36Sopenharmony_ci	inet_sk(sk)->inet_num = 0;
19862306a36Sopenharmony_ci	inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	spin_lock(&head2->lock);
20162306a36Sopenharmony_ci	if (inet_csk(sk)->icsk_bind2_hash) {
20262306a36Sopenharmony_ci		struct inet_bind2_bucket *tb2 = inet_csk(sk)->icsk_bind2_hash;
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci		__sk_del_bind2_node(sk);
20562306a36Sopenharmony_ci		inet_csk(sk)->icsk_bind2_hash = NULL;
20662306a36Sopenharmony_ci		inet_bind2_bucket_destroy(hashinfo->bind2_bucket_cachep, tb2);
20762306a36Sopenharmony_ci	}
20862306a36Sopenharmony_ci	spin_unlock(&head2->lock);
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	spin_unlock(&head->lock);
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_civoid inet_put_port(struct sock *sk)
21462306a36Sopenharmony_ci{
21562306a36Sopenharmony_ci	local_bh_disable();
21662306a36Sopenharmony_ci	__inet_put_port(sk);
21762306a36Sopenharmony_ci	local_bh_enable();
21862306a36Sopenharmony_ci}
21962306a36Sopenharmony_ciEXPORT_SYMBOL(inet_put_port);
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ciint __inet_inherit_port(const struct sock *sk, struct sock *child)
22262306a36Sopenharmony_ci{
22362306a36Sopenharmony_ci	struct inet_hashinfo *table = tcp_or_dccp_get_hashinfo(sk);
22462306a36Sopenharmony_ci	unsigned short port = inet_sk(child)->inet_num;
22562306a36Sopenharmony_ci	struct inet_bind_hashbucket *head, *head2;
22662306a36Sopenharmony_ci	bool created_inet_bind_bucket = false;
22762306a36Sopenharmony_ci	struct net *net = sock_net(sk);
22862306a36Sopenharmony_ci	bool update_fastreuse = false;
22962306a36Sopenharmony_ci	struct inet_bind2_bucket *tb2;
23062306a36Sopenharmony_ci	struct inet_bind_bucket *tb;
23162306a36Sopenharmony_ci	int bhash, l3mdev;
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci	bhash = inet_bhashfn(net, port, table->bhash_size);
23462306a36Sopenharmony_ci	head = &table->bhash[bhash];
23562306a36Sopenharmony_ci	head2 = inet_bhashfn_portaddr(table, child, net, port);
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	spin_lock(&head->lock);
23862306a36Sopenharmony_ci	spin_lock(&head2->lock);
23962306a36Sopenharmony_ci	tb = inet_csk(sk)->icsk_bind_hash;
24062306a36Sopenharmony_ci	tb2 = inet_csk(sk)->icsk_bind2_hash;
24162306a36Sopenharmony_ci	if (unlikely(!tb || !tb2)) {
24262306a36Sopenharmony_ci		spin_unlock(&head2->lock);
24362306a36Sopenharmony_ci		spin_unlock(&head->lock);
24462306a36Sopenharmony_ci		return -ENOENT;
24562306a36Sopenharmony_ci	}
24662306a36Sopenharmony_ci	if (tb->port != port) {
24762306a36Sopenharmony_ci		l3mdev = inet_sk_bound_l3mdev(sk);
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci		/* NOTE: using tproxy and redirecting skbs to a proxy
25062306a36Sopenharmony_ci		 * on a different listener port breaks the assumption
25162306a36Sopenharmony_ci		 * that the listener socket's icsk_bind_hash is the same
25262306a36Sopenharmony_ci		 * as that of the child socket. We have to look up or
25362306a36Sopenharmony_ci		 * create a new bind bucket for the child here. */
25462306a36Sopenharmony_ci		inet_bind_bucket_for_each(tb, &head->chain) {
25562306a36Sopenharmony_ci			if (inet_bind_bucket_match(tb, net, port, l3mdev))
25662306a36Sopenharmony_ci				break;
25762306a36Sopenharmony_ci		}
25862306a36Sopenharmony_ci		if (!tb) {
25962306a36Sopenharmony_ci			tb = inet_bind_bucket_create(table->bind_bucket_cachep,
26062306a36Sopenharmony_ci						     net, head, port, l3mdev);
26162306a36Sopenharmony_ci			if (!tb) {
26262306a36Sopenharmony_ci				spin_unlock(&head2->lock);
26362306a36Sopenharmony_ci				spin_unlock(&head->lock);
26462306a36Sopenharmony_ci				return -ENOMEM;
26562306a36Sopenharmony_ci			}
26662306a36Sopenharmony_ci			created_inet_bind_bucket = true;
26762306a36Sopenharmony_ci		}
26862306a36Sopenharmony_ci		update_fastreuse = true;
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci		goto bhash2_find;
27162306a36Sopenharmony_ci	} else if (!inet_bind2_bucket_addr_match(tb2, child)) {
27262306a36Sopenharmony_ci		l3mdev = inet_sk_bound_l3mdev(sk);
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_cibhash2_find:
27562306a36Sopenharmony_ci		tb2 = inet_bind2_bucket_find(head2, net, port, l3mdev, child);
27662306a36Sopenharmony_ci		if (!tb2) {
27762306a36Sopenharmony_ci			tb2 = inet_bind2_bucket_create(table->bind2_bucket_cachep,
27862306a36Sopenharmony_ci						       net, head2, port,
27962306a36Sopenharmony_ci						       l3mdev, child);
28062306a36Sopenharmony_ci			if (!tb2)
28162306a36Sopenharmony_ci				goto error;
28262306a36Sopenharmony_ci		}
28362306a36Sopenharmony_ci	}
28462306a36Sopenharmony_ci	if (update_fastreuse)
28562306a36Sopenharmony_ci		inet_csk_update_fastreuse(tb, child);
28662306a36Sopenharmony_ci	inet_bind_hash(child, tb, tb2, port);
28762306a36Sopenharmony_ci	spin_unlock(&head2->lock);
28862306a36Sopenharmony_ci	spin_unlock(&head->lock);
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci	return 0;
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_cierror:
29362306a36Sopenharmony_ci	if (created_inet_bind_bucket)
29462306a36Sopenharmony_ci		inet_bind_bucket_destroy(table->bind_bucket_cachep, tb);
29562306a36Sopenharmony_ci	spin_unlock(&head2->lock);
29662306a36Sopenharmony_ci	spin_unlock(&head->lock);
29762306a36Sopenharmony_ci	return -ENOMEM;
29862306a36Sopenharmony_ci}
29962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__inet_inherit_port);
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_cistatic struct inet_listen_hashbucket *
30262306a36Sopenharmony_ciinet_lhash2_bucket_sk(struct inet_hashinfo *h, struct sock *sk)
30362306a36Sopenharmony_ci{
30462306a36Sopenharmony_ci	u32 hash;
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
30762306a36Sopenharmony_ci	if (sk->sk_family == AF_INET6)
30862306a36Sopenharmony_ci		hash = ipv6_portaddr_hash(sock_net(sk),
30962306a36Sopenharmony_ci					  &sk->sk_v6_rcv_saddr,
31062306a36Sopenharmony_ci					  inet_sk(sk)->inet_num);
31162306a36Sopenharmony_ci	else
31262306a36Sopenharmony_ci#endif
31362306a36Sopenharmony_ci		hash = ipv4_portaddr_hash(sock_net(sk),
31462306a36Sopenharmony_ci					  inet_sk(sk)->inet_rcv_saddr,
31562306a36Sopenharmony_ci					  inet_sk(sk)->inet_num);
31662306a36Sopenharmony_ci	return inet_lhash2_bucket(h, hash);
31762306a36Sopenharmony_ci}
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_cistatic inline int compute_score(struct sock *sk, struct net *net,
32062306a36Sopenharmony_ci				const unsigned short hnum, const __be32 daddr,
32162306a36Sopenharmony_ci				const int dif, const int sdif)
32262306a36Sopenharmony_ci{
32362306a36Sopenharmony_ci	int score = -1;
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci	if (net_eq(sock_net(sk), net) && sk->sk_num == hnum &&
32662306a36Sopenharmony_ci			!ipv6_only_sock(sk)) {
32762306a36Sopenharmony_ci		if (sk->sk_rcv_saddr != daddr)
32862306a36Sopenharmony_ci			return -1;
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci		if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
33162306a36Sopenharmony_ci			return -1;
33262306a36Sopenharmony_ci		score =  sk->sk_bound_dev_if ? 2 : 1;
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci		if (sk->sk_family == PF_INET)
33562306a36Sopenharmony_ci			score++;
33662306a36Sopenharmony_ci		if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
33762306a36Sopenharmony_ci			score++;
33862306a36Sopenharmony_ci	}
33962306a36Sopenharmony_ci	return score;
34062306a36Sopenharmony_ci}
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ci/**
34362306a36Sopenharmony_ci * inet_lookup_reuseport() - execute reuseport logic on AF_INET socket if necessary.
34462306a36Sopenharmony_ci * @net: network namespace.
34562306a36Sopenharmony_ci * @sk: AF_INET socket, must be in TCP_LISTEN state for TCP or TCP_CLOSE for UDP.
34662306a36Sopenharmony_ci * @skb: context for a potential SK_REUSEPORT program.
34762306a36Sopenharmony_ci * @doff: header offset.
34862306a36Sopenharmony_ci * @saddr: source address.
34962306a36Sopenharmony_ci * @sport: source port.
35062306a36Sopenharmony_ci * @daddr: destination address.
35162306a36Sopenharmony_ci * @hnum: destination port in host byte order.
35262306a36Sopenharmony_ci * @ehashfn: hash function used to generate the fallback hash.
35362306a36Sopenharmony_ci *
35462306a36Sopenharmony_ci * Return: NULL if sk doesn't have SO_REUSEPORT set, otherwise a pointer to
35562306a36Sopenharmony_ci *         the selected sock or an error.
35662306a36Sopenharmony_ci */
35762306a36Sopenharmony_cistruct sock *inet_lookup_reuseport(struct net *net, struct sock *sk,
35862306a36Sopenharmony_ci				   struct sk_buff *skb, int doff,
35962306a36Sopenharmony_ci				   __be32 saddr, __be16 sport,
36062306a36Sopenharmony_ci				   __be32 daddr, unsigned short hnum,
36162306a36Sopenharmony_ci				   inet_ehashfn_t *ehashfn)
36262306a36Sopenharmony_ci{
36362306a36Sopenharmony_ci	struct sock *reuse_sk = NULL;
36462306a36Sopenharmony_ci	u32 phash;
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci	if (sk->sk_reuseport) {
36762306a36Sopenharmony_ci		phash = INDIRECT_CALL_2(ehashfn, udp_ehashfn, inet_ehashfn,
36862306a36Sopenharmony_ci					net, daddr, hnum, saddr, sport);
36962306a36Sopenharmony_ci		reuse_sk = reuseport_select_sock(sk, phash, skb, doff);
37062306a36Sopenharmony_ci	}
37162306a36Sopenharmony_ci	return reuse_sk;
37262306a36Sopenharmony_ci}
37362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(inet_lookup_reuseport);
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci/*
37662306a36Sopenharmony_ci * Here are some nice properties to exploit here. The BSD API
37762306a36Sopenharmony_ci * does not allow a listening sock to specify the remote port nor the
37862306a36Sopenharmony_ci * remote address for the connection. So always assume those are both
37962306a36Sopenharmony_ci * wildcarded during the search since they can never be otherwise.
38062306a36Sopenharmony_ci */
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci/* called with rcu_read_lock() : No refcount taken on the socket */
38362306a36Sopenharmony_cistatic struct sock *inet_lhash2_lookup(struct net *net,
38462306a36Sopenharmony_ci				struct inet_listen_hashbucket *ilb2,
38562306a36Sopenharmony_ci				struct sk_buff *skb, int doff,
38662306a36Sopenharmony_ci				const __be32 saddr, __be16 sport,
38762306a36Sopenharmony_ci				const __be32 daddr, const unsigned short hnum,
38862306a36Sopenharmony_ci				const int dif, const int sdif)
38962306a36Sopenharmony_ci{
39062306a36Sopenharmony_ci	struct sock *sk, *result = NULL;
39162306a36Sopenharmony_ci	struct hlist_nulls_node *node;
39262306a36Sopenharmony_ci	int score, hiscore = 0;
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) {
39562306a36Sopenharmony_ci		score = compute_score(sk, net, hnum, daddr, dif, sdif);
39662306a36Sopenharmony_ci		if (score > hiscore) {
39762306a36Sopenharmony_ci			result = inet_lookup_reuseport(net, sk, skb, doff,
39862306a36Sopenharmony_ci						       saddr, sport, daddr, hnum, inet_ehashfn);
39962306a36Sopenharmony_ci			if (result)
40062306a36Sopenharmony_ci				return result;
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_ci			result = sk;
40362306a36Sopenharmony_ci			hiscore = score;
40462306a36Sopenharmony_ci		}
40562306a36Sopenharmony_ci	}
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ci	return result;
40862306a36Sopenharmony_ci}
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_cistruct sock *inet_lookup_run_sk_lookup(struct net *net,
41162306a36Sopenharmony_ci				       int protocol,
41262306a36Sopenharmony_ci				       struct sk_buff *skb, int doff,
41362306a36Sopenharmony_ci				       __be32 saddr, __be16 sport,
41462306a36Sopenharmony_ci				       __be32 daddr, u16 hnum, const int dif,
41562306a36Sopenharmony_ci				       inet_ehashfn_t *ehashfn)
41662306a36Sopenharmony_ci{
41762306a36Sopenharmony_ci	struct sock *sk, *reuse_sk;
41862306a36Sopenharmony_ci	bool no_reuseport;
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ci	no_reuseport = bpf_sk_lookup_run_v4(net, protocol, saddr, sport,
42162306a36Sopenharmony_ci					    daddr, hnum, dif, &sk);
42262306a36Sopenharmony_ci	if (no_reuseport || IS_ERR_OR_NULL(sk))
42362306a36Sopenharmony_ci		return sk;
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci	reuse_sk = inet_lookup_reuseport(net, sk, skb, doff, saddr, sport, daddr, hnum,
42662306a36Sopenharmony_ci					 ehashfn);
42762306a36Sopenharmony_ci	if (reuse_sk)
42862306a36Sopenharmony_ci		sk = reuse_sk;
42962306a36Sopenharmony_ci	return sk;
43062306a36Sopenharmony_ci}
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_cistruct sock *__inet_lookup_listener(struct net *net,
43362306a36Sopenharmony_ci				    struct inet_hashinfo *hashinfo,
43462306a36Sopenharmony_ci				    struct sk_buff *skb, int doff,
43562306a36Sopenharmony_ci				    const __be32 saddr, __be16 sport,
43662306a36Sopenharmony_ci				    const __be32 daddr, const unsigned short hnum,
43762306a36Sopenharmony_ci				    const int dif, const int sdif)
43862306a36Sopenharmony_ci{
43962306a36Sopenharmony_ci	struct inet_listen_hashbucket *ilb2;
44062306a36Sopenharmony_ci	struct sock *result = NULL;
44162306a36Sopenharmony_ci	unsigned int hash2;
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci	/* Lookup redirect from BPF */
44462306a36Sopenharmony_ci	if (static_branch_unlikely(&bpf_sk_lookup_enabled) &&
44562306a36Sopenharmony_ci	    hashinfo == net->ipv4.tcp_death_row.hashinfo) {
44662306a36Sopenharmony_ci		result = inet_lookup_run_sk_lookup(net, IPPROTO_TCP, skb, doff,
44762306a36Sopenharmony_ci						   saddr, sport, daddr, hnum, dif,
44862306a36Sopenharmony_ci						   inet_ehashfn);
44962306a36Sopenharmony_ci		if (result)
45062306a36Sopenharmony_ci			goto done;
45162306a36Sopenharmony_ci	}
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci	hash2 = ipv4_portaddr_hash(net, daddr, hnum);
45462306a36Sopenharmony_ci	ilb2 = inet_lhash2_bucket(hashinfo, hash2);
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci	result = inet_lhash2_lookup(net, ilb2, skb, doff,
45762306a36Sopenharmony_ci				    saddr, sport, daddr, hnum,
45862306a36Sopenharmony_ci				    dif, sdif);
45962306a36Sopenharmony_ci	if (result)
46062306a36Sopenharmony_ci		goto done;
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci	/* Lookup lhash2 with INADDR_ANY */
46362306a36Sopenharmony_ci	hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
46462306a36Sopenharmony_ci	ilb2 = inet_lhash2_bucket(hashinfo, hash2);
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ci	result = inet_lhash2_lookup(net, ilb2, skb, doff,
46762306a36Sopenharmony_ci				    saddr, sport, htonl(INADDR_ANY), hnum,
46862306a36Sopenharmony_ci				    dif, sdif);
46962306a36Sopenharmony_cidone:
47062306a36Sopenharmony_ci	if (IS_ERR(result))
47162306a36Sopenharmony_ci		return NULL;
47262306a36Sopenharmony_ci	return result;
47362306a36Sopenharmony_ci}
47462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__inet_lookup_listener);
47562306a36Sopenharmony_ci
47662306a36Sopenharmony_ci/* All sockets share common refcount, but have different destructors */
47762306a36Sopenharmony_civoid sock_gen_put(struct sock *sk)
47862306a36Sopenharmony_ci{
47962306a36Sopenharmony_ci	if (!refcount_dec_and_test(&sk->sk_refcnt))
48062306a36Sopenharmony_ci		return;
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ci	if (sk->sk_state == TCP_TIME_WAIT)
48362306a36Sopenharmony_ci		inet_twsk_free(inet_twsk(sk));
48462306a36Sopenharmony_ci	else if (sk->sk_state == TCP_NEW_SYN_RECV)
48562306a36Sopenharmony_ci		reqsk_free(inet_reqsk(sk));
48662306a36Sopenharmony_ci	else
48762306a36Sopenharmony_ci		sk_free(sk);
48862306a36Sopenharmony_ci}
48962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(sock_gen_put);
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_civoid sock_edemux(struct sk_buff *skb)
49262306a36Sopenharmony_ci{
49362306a36Sopenharmony_ci	sock_gen_put(skb->sk);
49462306a36Sopenharmony_ci}
49562306a36Sopenharmony_ciEXPORT_SYMBOL(sock_edemux);
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_cistruct sock *__inet_lookup_established(struct net *net,
49862306a36Sopenharmony_ci				  struct inet_hashinfo *hashinfo,
49962306a36Sopenharmony_ci				  const __be32 saddr, const __be16 sport,
50062306a36Sopenharmony_ci				  const __be32 daddr, const u16 hnum,
50162306a36Sopenharmony_ci				  const int dif, const int sdif)
50262306a36Sopenharmony_ci{
50362306a36Sopenharmony_ci	INET_ADDR_COOKIE(acookie, saddr, daddr);
50462306a36Sopenharmony_ci	const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
50562306a36Sopenharmony_ci	struct sock *sk;
50662306a36Sopenharmony_ci	const struct hlist_nulls_node *node;
50762306a36Sopenharmony_ci	/* Optimize here for direct hit, only listening connections can
50862306a36Sopenharmony_ci	 * have wildcards anyways.
50962306a36Sopenharmony_ci	 */
51062306a36Sopenharmony_ci	unsigned int hash = inet_ehashfn(net, daddr, hnum, saddr, sport);
51162306a36Sopenharmony_ci	unsigned int slot = hash & hashinfo->ehash_mask;
51262306a36Sopenharmony_ci	struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_cibegin:
51562306a36Sopenharmony_ci	sk_nulls_for_each_rcu(sk, node, &head->chain) {
51662306a36Sopenharmony_ci		if (sk->sk_hash != hash)
51762306a36Sopenharmony_ci			continue;
51862306a36Sopenharmony_ci		if (likely(inet_match(net, sk, acookie, ports, dif, sdif))) {
51962306a36Sopenharmony_ci			if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
52062306a36Sopenharmony_ci				goto out;
52162306a36Sopenharmony_ci			if (unlikely(!inet_match(net, sk, acookie,
52262306a36Sopenharmony_ci						 ports, dif, sdif))) {
52362306a36Sopenharmony_ci				sock_gen_put(sk);
52462306a36Sopenharmony_ci				goto begin;
52562306a36Sopenharmony_ci			}
52662306a36Sopenharmony_ci			goto found;
52762306a36Sopenharmony_ci		}
52862306a36Sopenharmony_ci	}
52962306a36Sopenharmony_ci	/*
53062306a36Sopenharmony_ci	 * if the nulls value we got at the end of this lookup is
53162306a36Sopenharmony_ci	 * not the expected one, we must restart lookup.
53262306a36Sopenharmony_ci	 * We probably met an item that was moved to another chain.
53362306a36Sopenharmony_ci	 */
53462306a36Sopenharmony_ci	if (get_nulls_value(node) != slot)
53562306a36Sopenharmony_ci		goto begin;
53662306a36Sopenharmony_ciout:
53762306a36Sopenharmony_ci	sk = NULL;
53862306a36Sopenharmony_cifound:
53962306a36Sopenharmony_ci	return sk;
54062306a36Sopenharmony_ci}
54162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__inet_lookup_established);
54262306a36Sopenharmony_ci
54362306a36Sopenharmony_ci/* called with local bh disabled */
54462306a36Sopenharmony_cistatic int __inet_check_established(struct inet_timewait_death_row *death_row,
54562306a36Sopenharmony_ci				    struct sock *sk, __u16 lport,
54662306a36Sopenharmony_ci				    struct inet_timewait_sock **twp)
54762306a36Sopenharmony_ci{
54862306a36Sopenharmony_ci	struct inet_hashinfo *hinfo = death_row->hashinfo;
54962306a36Sopenharmony_ci	struct inet_sock *inet = inet_sk(sk);
55062306a36Sopenharmony_ci	__be32 daddr = inet->inet_rcv_saddr;
55162306a36Sopenharmony_ci	__be32 saddr = inet->inet_daddr;
55262306a36Sopenharmony_ci	int dif = sk->sk_bound_dev_if;
55362306a36Sopenharmony_ci	struct net *net = sock_net(sk);
55462306a36Sopenharmony_ci	int sdif = l3mdev_master_ifindex_by_index(net, dif);
55562306a36Sopenharmony_ci	INET_ADDR_COOKIE(acookie, saddr, daddr);
55662306a36Sopenharmony_ci	const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
55762306a36Sopenharmony_ci	unsigned int hash = inet_ehashfn(net, daddr, lport,
55862306a36Sopenharmony_ci					 saddr, inet->inet_dport);
55962306a36Sopenharmony_ci	struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
56062306a36Sopenharmony_ci	spinlock_t *lock = inet_ehash_lockp(hinfo, hash);
56162306a36Sopenharmony_ci	struct sock *sk2;
56262306a36Sopenharmony_ci	const struct hlist_nulls_node *node;
56362306a36Sopenharmony_ci	struct inet_timewait_sock *tw = NULL;
56462306a36Sopenharmony_ci
56562306a36Sopenharmony_ci	spin_lock(lock);
56662306a36Sopenharmony_ci
56762306a36Sopenharmony_ci	sk_nulls_for_each(sk2, node, &head->chain) {
56862306a36Sopenharmony_ci		if (sk2->sk_hash != hash)
56962306a36Sopenharmony_ci			continue;
57062306a36Sopenharmony_ci
57162306a36Sopenharmony_ci		if (likely(inet_match(net, sk2, acookie, ports, dif, sdif))) {
57262306a36Sopenharmony_ci			if (sk2->sk_state == TCP_TIME_WAIT) {
57362306a36Sopenharmony_ci				tw = inet_twsk(sk2);
57462306a36Sopenharmony_ci				if (twsk_unique(sk, sk2, twp))
57562306a36Sopenharmony_ci					break;
57662306a36Sopenharmony_ci			}
57762306a36Sopenharmony_ci			goto not_unique;
57862306a36Sopenharmony_ci		}
57962306a36Sopenharmony_ci	}
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_ci	/* Must record num and sport now. Otherwise we will see
58262306a36Sopenharmony_ci	 * in hash table socket with a funny identity.
58362306a36Sopenharmony_ci	 */
58462306a36Sopenharmony_ci	inet->inet_num = lport;
58562306a36Sopenharmony_ci	inet->inet_sport = htons(lport);
58662306a36Sopenharmony_ci	sk->sk_hash = hash;
58762306a36Sopenharmony_ci	WARN_ON(!sk_unhashed(sk));
58862306a36Sopenharmony_ci	__sk_nulls_add_node_rcu(sk, &head->chain);
58962306a36Sopenharmony_ci	if (tw) {
59062306a36Sopenharmony_ci		sk_nulls_del_node_init_rcu((struct sock *)tw);
59162306a36Sopenharmony_ci		__NET_INC_STATS(net, LINUX_MIB_TIMEWAITRECYCLED);
59262306a36Sopenharmony_ci	}
59362306a36Sopenharmony_ci	spin_unlock(lock);
59462306a36Sopenharmony_ci	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_ci	if (twp) {
59762306a36Sopenharmony_ci		*twp = tw;
59862306a36Sopenharmony_ci	} else if (tw) {
59962306a36Sopenharmony_ci		/* Silly. Should hash-dance instead... */
60062306a36Sopenharmony_ci		inet_twsk_deschedule_put(tw);
60162306a36Sopenharmony_ci	}
60262306a36Sopenharmony_ci	return 0;
60362306a36Sopenharmony_ci
60462306a36Sopenharmony_cinot_unique:
60562306a36Sopenharmony_ci	spin_unlock(lock);
60662306a36Sopenharmony_ci	return -EADDRNOTAVAIL;
60762306a36Sopenharmony_ci}
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_cistatic u64 inet_sk_port_offset(const struct sock *sk)
61062306a36Sopenharmony_ci{
61162306a36Sopenharmony_ci	const struct inet_sock *inet = inet_sk(sk);
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_ci	return secure_ipv4_port_ephemeral(inet->inet_rcv_saddr,
61462306a36Sopenharmony_ci					  inet->inet_daddr,
61562306a36Sopenharmony_ci					  inet->inet_dport);
61662306a36Sopenharmony_ci}
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_ci/* Searches for an exsiting socket in the ehash bucket list.
61962306a36Sopenharmony_ci * Returns true if found, false otherwise.
62062306a36Sopenharmony_ci */
62162306a36Sopenharmony_cistatic bool inet_ehash_lookup_by_sk(struct sock *sk,
62262306a36Sopenharmony_ci				    struct hlist_nulls_head *list)
62362306a36Sopenharmony_ci{
62462306a36Sopenharmony_ci	const __portpair ports = INET_COMBINED_PORTS(sk->sk_dport, sk->sk_num);
62562306a36Sopenharmony_ci	const int sdif = sk->sk_bound_dev_if;
62662306a36Sopenharmony_ci	const int dif = sk->sk_bound_dev_if;
62762306a36Sopenharmony_ci	const struct hlist_nulls_node *node;
62862306a36Sopenharmony_ci	struct net *net = sock_net(sk);
62962306a36Sopenharmony_ci	struct sock *esk;
63062306a36Sopenharmony_ci
63162306a36Sopenharmony_ci	INET_ADDR_COOKIE(acookie, sk->sk_daddr, sk->sk_rcv_saddr);
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ci	sk_nulls_for_each_rcu(esk, node, list) {
63462306a36Sopenharmony_ci		if (esk->sk_hash != sk->sk_hash)
63562306a36Sopenharmony_ci			continue;
63662306a36Sopenharmony_ci		if (sk->sk_family == AF_INET) {
63762306a36Sopenharmony_ci			if (unlikely(inet_match(net, esk, acookie,
63862306a36Sopenharmony_ci						ports, dif, sdif))) {
63962306a36Sopenharmony_ci				return true;
64062306a36Sopenharmony_ci			}
64162306a36Sopenharmony_ci		}
64262306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
64362306a36Sopenharmony_ci		else if (sk->sk_family == AF_INET6) {
64462306a36Sopenharmony_ci			if (unlikely(inet6_match(net, esk,
64562306a36Sopenharmony_ci						 &sk->sk_v6_daddr,
64662306a36Sopenharmony_ci						 &sk->sk_v6_rcv_saddr,
64762306a36Sopenharmony_ci						 ports, dif, sdif))) {
64862306a36Sopenharmony_ci				return true;
64962306a36Sopenharmony_ci			}
65062306a36Sopenharmony_ci		}
65162306a36Sopenharmony_ci#endif
65262306a36Sopenharmony_ci	}
65362306a36Sopenharmony_ci	return false;
65462306a36Sopenharmony_ci}
65562306a36Sopenharmony_ci
65662306a36Sopenharmony_ci/* Insert a socket into ehash, and eventually remove another one
65762306a36Sopenharmony_ci * (The another one can be a SYN_RECV or TIMEWAIT)
65862306a36Sopenharmony_ci * If an existing socket already exists, socket sk is not inserted,
65962306a36Sopenharmony_ci * and sets found_dup_sk parameter to true.
66062306a36Sopenharmony_ci */
66162306a36Sopenharmony_cibool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk)
66262306a36Sopenharmony_ci{
66362306a36Sopenharmony_ci	struct inet_hashinfo *hashinfo = tcp_or_dccp_get_hashinfo(sk);
66462306a36Sopenharmony_ci	struct inet_ehash_bucket *head;
66562306a36Sopenharmony_ci	struct hlist_nulls_head *list;
66662306a36Sopenharmony_ci	spinlock_t *lock;
66762306a36Sopenharmony_ci	bool ret = true;
66862306a36Sopenharmony_ci
66962306a36Sopenharmony_ci	WARN_ON_ONCE(!sk_unhashed(sk));
67062306a36Sopenharmony_ci
67162306a36Sopenharmony_ci	sk->sk_hash = sk_ehashfn(sk);
67262306a36Sopenharmony_ci	head = inet_ehash_bucket(hashinfo, sk->sk_hash);
67362306a36Sopenharmony_ci	list = &head->chain;
67462306a36Sopenharmony_ci	lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
67562306a36Sopenharmony_ci
67662306a36Sopenharmony_ci	spin_lock(lock);
67762306a36Sopenharmony_ci	if (osk) {
67862306a36Sopenharmony_ci		WARN_ON_ONCE(sk->sk_hash != osk->sk_hash);
67962306a36Sopenharmony_ci		ret = sk_nulls_del_node_init_rcu(osk);
68062306a36Sopenharmony_ci	} else if (found_dup_sk) {
68162306a36Sopenharmony_ci		*found_dup_sk = inet_ehash_lookup_by_sk(sk, list);
68262306a36Sopenharmony_ci		if (*found_dup_sk)
68362306a36Sopenharmony_ci			ret = false;
68462306a36Sopenharmony_ci	}
68562306a36Sopenharmony_ci
68662306a36Sopenharmony_ci	if (ret)
68762306a36Sopenharmony_ci		__sk_nulls_add_node_rcu(sk, list);
68862306a36Sopenharmony_ci
68962306a36Sopenharmony_ci	spin_unlock(lock);
69062306a36Sopenharmony_ci
69162306a36Sopenharmony_ci	return ret;
69262306a36Sopenharmony_ci}
69362306a36Sopenharmony_ci
69462306a36Sopenharmony_cibool inet_ehash_nolisten(struct sock *sk, struct sock *osk, bool *found_dup_sk)
69562306a36Sopenharmony_ci{
69662306a36Sopenharmony_ci	bool ok = inet_ehash_insert(sk, osk, found_dup_sk);
69762306a36Sopenharmony_ci
69862306a36Sopenharmony_ci	if (ok) {
69962306a36Sopenharmony_ci		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
70062306a36Sopenharmony_ci	} else {
70162306a36Sopenharmony_ci		this_cpu_inc(*sk->sk_prot->orphan_count);
70262306a36Sopenharmony_ci		inet_sk_set_state(sk, TCP_CLOSE);
70362306a36Sopenharmony_ci		sock_set_flag(sk, SOCK_DEAD);
70462306a36Sopenharmony_ci		inet_csk_destroy_sock(sk);
70562306a36Sopenharmony_ci	}
70662306a36Sopenharmony_ci	return ok;
70762306a36Sopenharmony_ci}
70862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(inet_ehash_nolisten);
70962306a36Sopenharmony_ci
71062306a36Sopenharmony_cistatic int inet_reuseport_add_sock(struct sock *sk,
71162306a36Sopenharmony_ci				   struct inet_listen_hashbucket *ilb)
71262306a36Sopenharmony_ci{
71362306a36Sopenharmony_ci	struct inet_bind_bucket *tb = inet_csk(sk)->icsk_bind_hash;
71462306a36Sopenharmony_ci	const struct hlist_nulls_node *node;
71562306a36Sopenharmony_ci	struct sock *sk2;
71662306a36Sopenharmony_ci	kuid_t uid = sock_i_uid(sk);
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ci	sk_nulls_for_each_rcu(sk2, node, &ilb->nulls_head) {
71962306a36Sopenharmony_ci		if (sk2 != sk &&
72062306a36Sopenharmony_ci		    sk2->sk_family == sk->sk_family &&
72162306a36Sopenharmony_ci		    ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
72262306a36Sopenharmony_ci		    sk2->sk_bound_dev_if == sk->sk_bound_dev_if &&
72362306a36Sopenharmony_ci		    inet_csk(sk2)->icsk_bind_hash == tb &&
72462306a36Sopenharmony_ci		    sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) &&
72562306a36Sopenharmony_ci		    inet_rcv_saddr_equal(sk, sk2, false))
72662306a36Sopenharmony_ci			return reuseport_add_sock(sk, sk2,
72762306a36Sopenharmony_ci						  inet_rcv_saddr_any(sk));
72862306a36Sopenharmony_ci	}
72962306a36Sopenharmony_ci
73062306a36Sopenharmony_ci	return reuseport_alloc(sk, inet_rcv_saddr_any(sk));
73162306a36Sopenharmony_ci}
73262306a36Sopenharmony_ci
73362306a36Sopenharmony_ciint __inet_hash(struct sock *sk, struct sock *osk)
73462306a36Sopenharmony_ci{
73562306a36Sopenharmony_ci	struct inet_hashinfo *hashinfo = tcp_or_dccp_get_hashinfo(sk);
73662306a36Sopenharmony_ci	struct inet_listen_hashbucket *ilb2;
73762306a36Sopenharmony_ci	int err = 0;
73862306a36Sopenharmony_ci
73962306a36Sopenharmony_ci	if (sk->sk_state != TCP_LISTEN) {
74062306a36Sopenharmony_ci		local_bh_disable();
74162306a36Sopenharmony_ci		inet_ehash_nolisten(sk, osk, NULL);
74262306a36Sopenharmony_ci		local_bh_enable();
74362306a36Sopenharmony_ci		return 0;
74462306a36Sopenharmony_ci	}
74562306a36Sopenharmony_ci	WARN_ON(!sk_unhashed(sk));
74662306a36Sopenharmony_ci	ilb2 = inet_lhash2_bucket_sk(hashinfo, sk);
74762306a36Sopenharmony_ci
74862306a36Sopenharmony_ci	spin_lock(&ilb2->lock);
74962306a36Sopenharmony_ci	if (sk->sk_reuseport) {
75062306a36Sopenharmony_ci		err = inet_reuseport_add_sock(sk, ilb2);
75162306a36Sopenharmony_ci		if (err)
75262306a36Sopenharmony_ci			goto unlock;
75362306a36Sopenharmony_ci	}
75462306a36Sopenharmony_ci	sock_set_flag(sk, SOCK_RCU_FREE);
75562306a36Sopenharmony_ci	if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
75662306a36Sopenharmony_ci		sk->sk_family == AF_INET6)
75762306a36Sopenharmony_ci		__sk_nulls_add_node_tail_rcu(sk, &ilb2->nulls_head);
75862306a36Sopenharmony_ci	else
75962306a36Sopenharmony_ci		__sk_nulls_add_node_rcu(sk, &ilb2->nulls_head);
76062306a36Sopenharmony_ci	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
76162306a36Sopenharmony_ciunlock:
76262306a36Sopenharmony_ci	spin_unlock(&ilb2->lock);
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci	return err;
76562306a36Sopenharmony_ci}
76662306a36Sopenharmony_ciEXPORT_SYMBOL(__inet_hash);
76762306a36Sopenharmony_ci
76862306a36Sopenharmony_ciint inet_hash(struct sock *sk)
76962306a36Sopenharmony_ci{
77062306a36Sopenharmony_ci	int err = 0;
77162306a36Sopenharmony_ci
77262306a36Sopenharmony_ci	if (sk->sk_state != TCP_CLOSE)
77362306a36Sopenharmony_ci		err = __inet_hash(sk, NULL);
77462306a36Sopenharmony_ci
77562306a36Sopenharmony_ci	return err;
77662306a36Sopenharmony_ci}
77762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(inet_hash);
77862306a36Sopenharmony_ci
77962306a36Sopenharmony_civoid inet_unhash(struct sock *sk)
78062306a36Sopenharmony_ci{
78162306a36Sopenharmony_ci	struct inet_hashinfo *hashinfo = tcp_or_dccp_get_hashinfo(sk);
78262306a36Sopenharmony_ci
78362306a36Sopenharmony_ci	if (sk_unhashed(sk))
78462306a36Sopenharmony_ci		return;
78562306a36Sopenharmony_ci
78662306a36Sopenharmony_ci	if (sk->sk_state == TCP_LISTEN) {
78762306a36Sopenharmony_ci		struct inet_listen_hashbucket *ilb2;
78862306a36Sopenharmony_ci
78962306a36Sopenharmony_ci		ilb2 = inet_lhash2_bucket_sk(hashinfo, sk);
79062306a36Sopenharmony_ci		/* Don't disable bottom halves while acquiring the lock to
79162306a36Sopenharmony_ci		 * avoid circular locking dependency on PREEMPT_RT.
79262306a36Sopenharmony_ci		 */
79362306a36Sopenharmony_ci		spin_lock(&ilb2->lock);
79462306a36Sopenharmony_ci		if (sk_unhashed(sk)) {
79562306a36Sopenharmony_ci			spin_unlock(&ilb2->lock);
79662306a36Sopenharmony_ci			return;
79762306a36Sopenharmony_ci		}
79862306a36Sopenharmony_ci
79962306a36Sopenharmony_ci		if (rcu_access_pointer(sk->sk_reuseport_cb))
80062306a36Sopenharmony_ci			reuseport_stop_listen_sock(sk);
80162306a36Sopenharmony_ci
80262306a36Sopenharmony_ci		__sk_nulls_del_node_init_rcu(sk);
80362306a36Sopenharmony_ci		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
80462306a36Sopenharmony_ci		spin_unlock(&ilb2->lock);
80562306a36Sopenharmony_ci	} else {
80662306a36Sopenharmony_ci		spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
80762306a36Sopenharmony_ci
80862306a36Sopenharmony_ci		spin_lock_bh(lock);
80962306a36Sopenharmony_ci		if (sk_unhashed(sk)) {
81062306a36Sopenharmony_ci			spin_unlock_bh(lock);
81162306a36Sopenharmony_ci			return;
81262306a36Sopenharmony_ci		}
81362306a36Sopenharmony_ci		__sk_nulls_del_node_init_rcu(sk);
81462306a36Sopenharmony_ci		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
81562306a36Sopenharmony_ci		spin_unlock_bh(lock);
81662306a36Sopenharmony_ci	}
81762306a36Sopenharmony_ci}
81862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(inet_unhash);
81962306a36Sopenharmony_ci
82062306a36Sopenharmony_cistatic bool inet_bind2_bucket_match(const struct inet_bind2_bucket *tb,
82162306a36Sopenharmony_ci				    const struct net *net, unsigned short port,
82262306a36Sopenharmony_ci				    int l3mdev, const struct sock *sk)
82362306a36Sopenharmony_ci{
82462306a36Sopenharmony_ci	if (!net_eq(ib2_net(tb), net) || tb->port != port ||
82562306a36Sopenharmony_ci	    tb->l3mdev != l3mdev)
82662306a36Sopenharmony_ci		return false;
82762306a36Sopenharmony_ci
82862306a36Sopenharmony_ci	return inet_bind2_bucket_addr_match(tb, sk);
82962306a36Sopenharmony_ci}
83062306a36Sopenharmony_ci
83162306a36Sopenharmony_cibool inet_bind2_bucket_match_addr_any(const struct inet_bind2_bucket *tb, const struct net *net,
83262306a36Sopenharmony_ci				      unsigned short port, int l3mdev, const struct sock *sk)
83362306a36Sopenharmony_ci{
83462306a36Sopenharmony_ci	if (!net_eq(ib2_net(tb), net) || tb->port != port ||
83562306a36Sopenharmony_ci	    tb->l3mdev != l3mdev)
83662306a36Sopenharmony_ci		return false;
83762306a36Sopenharmony_ci
83862306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
83962306a36Sopenharmony_ci	if (sk->sk_family != tb->family) {
84062306a36Sopenharmony_ci		if (sk->sk_family == AF_INET)
84162306a36Sopenharmony_ci			return ipv6_addr_any(&tb->v6_rcv_saddr) ||
84262306a36Sopenharmony_ci				ipv6_addr_v4mapped_any(&tb->v6_rcv_saddr);
84362306a36Sopenharmony_ci
84462306a36Sopenharmony_ci		return false;
84562306a36Sopenharmony_ci	}
84662306a36Sopenharmony_ci
84762306a36Sopenharmony_ci	if (sk->sk_family == AF_INET6)
84862306a36Sopenharmony_ci		return ipv6_addr_any(&tb->v6_rcv_saddr);
84962306a36Sopenharmony_ci#endif
85062306a36Sopenharmony_ci	return tb->rcv_saddr == 0;
85162306a36Sopenharmony_ci}
85262306a36Sopenharmony_ci
85362306a36Sopenharmony_ci/* The socket's bhash2 hashbucket spinlock must be held when this is called */
85462306a36Sopenharmony_cistruct inet_bind2_bucket *
85562306a36Sopenharmony_ciinet_bind2_bucket_find(const struct inet_bind_hashbucket *head, const struct net *net,
85662306a36Sopenharmony_ci		       unsigned short port, int l3mdev, const struct sock *sk)
85762306a36Sopenharmony_ci{
85862306a36Sopenharmony_ci	struct inet_bind2_bucket *bhash2 = NULL;
85962306a36Sopenharmony_ci
86062306a36Sopenharmony_ci	inet_bind_bucket_for_each(bhash2, &head->chain)
86162306a36Sopenharmony_ci		if (inet_bind2_bucket_match(bhash2, net, port, l3mdev, sk))
86262306a36Sopenharmony_ci			break;
86362306a36Sopenharmony_ci
86462306a36Sopenharmony_ci	return bhash2;
86562306a36Sopenharmony_ci}
86662306a36Sopenharmony_ci
86762306a36Sopenharmony_cistruct inet_bind_hashbucket *
86862306a36Sopenharmony_ciinet_bhash2_addr_any_hashbucket(const struct sock *sk, const struct net *net, int port)
86962306a36Sopenharmony_ci{
87062306a36Sopenharmony_ci	struct inet_hashinfo *hinfo = tcp_or_dccp_get_hashinfo(sk);
87162306a36Sopenharmony_ci	u32 hash;
87262306a36Sopenharmony_ci
87362306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
87462306a36Sopenharmony_ci	if (sk->sk_family == AF_INET6)
87562306a36Sopenharmony_ci		hash = ipv6_portaddr_hash(net, &in6addr_any, port);
87662306a36Sopenharmony_ci	else
87762306a36Sopenharmony_ci#endif
87862306a36Sopenharmony_ci		hash = ipv4_portaddr_hash(net, 0, port);
87962306a36Sopenharmony_ci
88062306a36Sopenharmony_ci	return &hinfo->bhash2[hash & (hinfo->bhash_size - 1)];
88162306a36Sopenharmony_ci}
88262306a36Sopenharmony_ci
88362306a36Sopenharmony_cistatic void inet_update_saddr(struct sock *sk, void *saddr, int family)
88462306a36Sopenharmony_ci{
88562306a36Sopenharmony_ci	if (family == AF_INET) {
88662306a36Sopenharmony_ci		inet_sk(sk)->inet_saddr = *(__be32 *)saddr;
88762306a36Sopenharmony_ci		sk_rcv_saddr_set(sk, inet_sk(sk)->inet_saddr);
88862306a36Sopenharmony_ci	}
88962306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
89062306a36Sopenharmony_ci	else {
89162306a36Sopenharmony_ci		sk->sk_v6_rcv_saddr = *(struct in6_addr *)saddr;
89262306a36Sopenharmony_ci	}
89362306a36Sopenharmony_ci#endif
89462306a36Sopenharmony_ci}
89562306a36Sopenharmony_ci
89662306a36Sopenharmony_cistatic int __inet_bhash2_update_saddr(struct sock *sk, void *saddr, int family, bool reset)
89762306a36Sopenharmony_ci{
89862306a36Sopenharmony_ci	struct inet_hashinfo *hinfo = tcp_or_dccp_get_hashinfo(sk);
89962306a36Sopenharmony_ci	struct inet_bind_hashbucket *head, *head2;
90062306a36Sopenharmony_ci	struct inet_bind2_bucket *tb2, *new_tb2;
90162306a36Sopenharmony_ci	int l3mdev = inet_sk_bound_l3mdev(sk);
90262306a36Sopenharmony_ci	int port = inet_sk(sk)->inet_num;
90362306a36Sopenharmony_ci	struct net *net = sock_net(sk);
90462306a36Sopenharmony_ci	int bhash;
90562306a36Sopenharmony_ci
90662306a36Sopenharmony_ci	if (!inet_csk(sk)->icsk_bind2_hash) {
90762306a36Sopenharmony_ci		/* Not bind()ed before. */
90862306a36Sopenharmony_ci		if (reset)
90962306a36Sopenharmony_ci			inet_reset_saddr(sk);
91062306a36Sopenharmony_ci		else
91162306a36Sopenharmony_ci			inet_update_saddr(sk, saddr, family);
91262306a36Sopenharmony_ci
91362306a36Sopenharmony_ci		return 0;
91462306a36Sopenharmony_ci	}
91562306a36Sopenharmony_ci
91662306a36Sopenharmony_ci	/* Allocate a bind2 bucket ahead of time to avoid permanently putting
91762306a36Sopenharmony_ci	 * the bhash2 table in an inconsistent state if a new tb2 bucket
91862306a36Sopenharmony_ci	 * allocation fails.
91962306a36Sopenharmony_ci	 */
92062306a36Sopenharmony_ci	new_tb2 = kmem_cache_alloc(hinfo->bind2_bucket_cachep, GFP_ATOMIC);
92162306a36Sopenharmony_ci	if (!new_tb2) {
92262306a36Sopenharmony_ci		if (reset) {
92362306a36Sopenharmony_ci			/* The (INADDR_ANY, port) bucket might have already
92462306a36Sopenharmony_ci			 * been freed, then we cannot fixup icsk_bind2_hash,
92562306a36Sopenharmony_ci			 * so we give up and unlink sk from bhash/bhash2 not
92662306a36Sopenharmony_ci			 * to leave inconsistency in bhash2.
92762306a36Sopenharmony_ci			 */
92862306a36Sopenharmony_ci			inet_put_port(sk);
92962306a36Sopenharmony_ci			inet_reset_saddr(sk);
93062306a36Sopenharmony_ci		}
93162306a36Sopenharmony_ci
93262306a36Sopenharmony_ci		return -ENOMEM;
93362306a36Sopenharmony_ci	}
93462306a36Sopenharmony_ci
93562306a36Sopenharmony_ci	bhash = inet_bhashfn(net, port, hinfo->bhash_size);
93662306a36Sopenharmony_ci	head = &hinfo->bhash[bhash];
93762306a36Sopenharmony_ci	head2 = inet_bhashfn_portaddr(hinfo, sk, net, port);
93862306a36Sopenharmony_ci
93962306a36Sopenharmony_ci	/* If we change saddr locklessly, another thread
94062306a36Sopenharmony_ci	 * iterating over bhash might see corrupted address.
94162306a36Sopenharmony_ci	 */
94262306a36Sopenharmony_ci	spin_lock_bh(&head->lock);
94362306a36Sopenharmony_ci
94462306a36Sopenharmony_ci	spin_lock(&head2->lock);
94562306a36Sopenharmony_ci	__sk_del_bind2_node(sk);
94662306a36Sopenharmony_ci	inet_bind2_bucket_destroy(hinfo->bind2_bucket_cachep, inet_csk(sk)->icsk_bind2_hash);
94762306a36Sopenharmony_ci	spin_unlock(&head2->lock);
94862306a36Sopenharmony_ci
94962306a36Sopenharmony_ci	if (reset)
95062306a36Sopenharmony_ci		inet_reset_saddr(sk);
95162306a36Sopenharmony_ci	else
95262306a36Sopenharmony_ci		inet_update_saddr(sk, saddr, family);
95362306a36Sopenharmony_ci
95462306a36Sopenharmony_ci	head2 = inet_bhashfn_portaddr(hinfo, sk, net, port);
95562306a36Sopenharmony_ci
95662306a36Sopenharmony_ci	spin_lock(&head2->lock);
95762306a36Sopenharmony_ci	tb2 = inet_bind2_bucket_find(head2, net, port, l3mdev, sk);
95862306a36Sopenharmony_ci	if (!tb2) {
95962306a36Sopenharmony_ci		tb2 = new_tb2;
96062306a36Sopenharmony_ci		inet_bind2_bucket_init(tb2, net, head2, port, l3mdev, sk);
96162306a36Sopenharmony_ci	}
96262306a36Sopenharmony_ci	sk_add_bind2_node(sk, &tb2->owners);
96362306a36Sopenharmony_ci	inet_csk(sk)->icsk_bind2_hash = tb2;
96462306a36Sopenharmony_ci	spin_unlock(&head2->lock);
96562306a36Sopenharmony_ci
96662306a36Sopenharmony_ci	spin_unlock_bh(&head->lock);
96762306a36Sopenharmony_ci
96862306a36Sopenharmony_ci	if (tb2 != new_tb2)
96962306a36Sopenharmony_ci		kmem_cache_free(hinfo->bind2_bucket_cachep, new_tb2);
97062306a36Sopenharmony_ci
97162306a36Sopenharmony_ci	return 0;
97262306a36Sopenharmony_ci}
97362306a36Sopenharmony_ci
97462306a36Sopenharmony_ciint inet_bhash2_update_saddr(struct sock *sk, void *saddr, int family)
97562306a36Sopenharmony_ci{
97662306a36Sopenharmony_ci	return __inet_bhash2_update_saddr(sk, saddr, family, false);
97762306a36Sopenharmony_ci}
97862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(inet_bhash2_update_saddr);
97962306a36Sopenharmony_ci
98062306a36Sopenharmony_civoid inet_bhash2_reset_saddr(struct sock *sk)
98162306a36Sopenharmony_ci{
98262306a36Sopenharmony_ci	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
98362306a36Sopenharmony_ci		__inet_bhash2_update_saddr(sk, NULL, 0, true);
98462306a36Sopenharmony_ci}
98562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(inet_bhash2_reset_saddr);
98662306a36Sopenharmony_ci
98762306a36Sopenharmony_ci/* RFC 6056 3.3.4.  Algorithm 4: Double-Hash Port Selection Algorithm
98862306a36Sopenharmony_ci * Note that we use 32bit integers (vs RFC 'short integers')
98962306a36Sopenharmony_ci * because 2^16 is not a multiple of num_ephemeral and this
99062306a36Sopenharmony_ci * property might be used by clever attacker.
99162306a36Sopenharmony_ci *
99262306a36Sopenharmony_ci * RFC claims using TABLE_LENGTH=10 buckets gives an improvement, though
99362306a36Sopenharmony_ci * attacks were since demonstrated, thus we use 65536 by default instead
99462306a36Sopenharmony_ci * to really give more isolation and privacy, at the expense of 256kB
99562306a36Sopenharmony_ci * of kernel memory.
99662306a36Sopenharmony_ci */
99762306a36Sopenharmony_ci#define INET_TABLE_PERTURB_SIZE (1 << CONFIG_INET_TABLE_PERTURB_ORDER)
99862306a36Sopenharmony_cistatic u32 *table_perturb;
99962306a36Sopenharmony_ci
100062306a36Sopenharmony_ciint __inet_hash_connect(struct inet_timewait_death_row *death_row,
100162306a36Sopenharmony_ci		struct sock *sk, u64 port_offset,
100262306a36Sopenharmony_ci		int (*check_established)(struct inet_timewait_death_row *,
100362306a36Sopenharmony_ci			struct sock *, __u16, struct inet_timewait_sock **))
100462306a36Sopenharmony_ci{
100562306a36Sopenharmony_ci	struct inet_hashinfo *hinfo = death_row->hashinfo;
100662306a36Sopenharmony_ci	struct inet_bind_hashbucket *head, *head2;
100762306a36Sopenharmony_ci	struct inet_timewait_sock *tw = NULL;
100862306a36Sopenharmony_ci	int port = inet_sk(sk)->inet_num;
100962306a36Sopenharmony_ci	struct net *net = sock_net(sk);
101062306a36Sopenharmony_ci	struct inet_bind2_bucket *tb2;
101162306a36Sopenharmony_ci	struct inet_bind_bucket *tb;
101262306a36Sopenharmony_ci	bool tb_created = false;
101362306a36Sopenharmony_ci	u32 remaining, offset;
101462306a36Sopenharmony_ci	int ret, i, low, high;
101562306a36Sopenharmony_ci	int l3mdev;
101662306a36Sopenharmony_ci	u32 index;
101762306a36Sopenharmony_ci
101862306a36Sopenharmony_ci	if (port) {
101962306a36Sopenharmony_ci		local_bh_disable();
102062306a36Sopenharmony_ci		ret = check_established(death_row, sk, port, NULL);
102162306a36Sopenharmony_ci		local_bh_enable();
102262306a36Sopenharmony_ci		return ret;
102362306a36Sopenharmony_ci	}
102462306a36Sopenharmony_ci
102562306a36Sopenharmony_ci	l3mdev = inet_sk_bound_l3mdev(sk);
102662306a36Sopenharmony_ci
102762306a36Sopenharmony_ci	inet_sk_get_local_port_range(sk, &low, &high);
102862306a36Sopenharmony_ci	high++; /* [32768, 60999] -> [32768, 61000[ */
102962306a36Sopenharmony_ci	remaining = high - low;
103062306a36Sopenharmony_ci	if (likely(remaining > 1))
103162306a36Sopenharmony_ci		remaining &= ~1U;
103262306a36Sopenharmony_ci
103362306a36Sopenharmony_ci	get_random_sleepable_once(table_perturb,
103462306a36Sopenharmony_ci				  INET_TABLE_PERTURB_SIZE * sizeof(*table_perturb));
103562306a36Sopenharmony_ci	index = port_offset & (INET_TABLE_PERTURB_SIZE - 1);
103662306a36Sopenharmony_ci
103762306a36Sopenharmony_ci	offset = READ_ONCE(table_perturb[index]) + (port_offset >> 32);
103862306a36Sopenharmony_ci	offset %= remaining;
103962306a36Sopenharmony_ci
104062306a36Sopenharmony_ci	/* In first pass we try ports of @low parity.
104162306a36Sopenharmony_ci	 * inet_csk_get_port() does the opposite choice.
104262306a36Sopenharmony_ci	 */
104362306a36Sopenharmony_ci	offset &= ~1U;
104462306a36Sopenharmony_ciother_parity_scan:
104562306a36Sopenharmony_ci	port = low + offset;
104662306a36Sopenharmony_ci	for (i = 0; i < remaining; i += 2, port += 2) {
104762306a36Sopenharmony_ci		if (unlikely(port >= high))
104862306a36Sopenharmony_ci			port -= remaining;
104962306a36Sopenharmony_ci		if (inet_is_local_reserved_port(net, port))
105062306a36Sopenharmony_ci			continue;
105162306a36Sopenharmony_ci		head = &hinfo->bhash[inet_bhashfn(net, port,
105262306a36Sopenharmony_ci						  hinfo->bhash_size)];
105362306a36Sopenharmony_ci		spin_lock_bh(&head->lock);
105462306a36Sopenharmony_ci
105562306a36Sopenharmony_ci		/* Does not bother with rcv_saddr checks, because
105662306a36Sopenharmony_ci		 * the established check is already unique enough.
105762306a36Sopenharmony_ci		 */
105862306a36Sopenharmony_ci		inet_bind_bucket_for_each(tb, &head->chain) {
105962306a36Sopenharmony_ci			if (inet_bind_bucket_match(tb, net, port, l3mdev)) {
106062306a36Sopenharmony_ci				if (tb->fastreuse >= 0 ||
106162306a36Sopenharmony_ci				    tb->fastreuseport >= 0)
106262306a36Sopenharmony_ci					goto next_port;
106362306a36Sopenharmony_ci				WARN_ON(hlist_empty(&tb->owners));
106462306a36Sopenharmony_ci				if (!check_established(death_row, sk,
106562306a36Sopenharmony_ci						       port, &tw))
106662306a36Sopenharmony_ci					goto ok;
106762306a36Sopenharmony_ci				goto next_port;
106862306a36Sopenharmony_ci			}
106962306a36Sopenharmony_ci		}
107062306a36Sopenharmony_ci
107162306a36Sopenharmony_ci		tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
107262306a36Sopenharmony_ci					     net, head, port, l3mdev);
107362306a36Sopenharmony_ci		if (!tb) {
107462306a36Sopenharmony_ci			spin_unlock_bh(&head->lock);
107562306a36Sopenharmony_ci			return -ENOMEM;
107662306a36Sopenharmony_ci		}
107762306a36Sopenharmony_ci		tb_created = true;
107862306a36Sopenharmony_ci		tb->fastreuse = -1;
107962306a36Sopenharmony_ci		tb->fastreuseport = -1;
108062306a36Sopenharmony_ci		goto ok;
108162306a36Sopenharmony_cinext_port:
108262306a36Sopenharmony_ci		spin_unlock_bh(&head->lock);
108362306a36Sopenharmony_ci		cond_resched();
108462306a36Sopenharmony_ci	}
108562306a36Sopenharmony_ci
108662306a36Sopenharmony_ci	offset++;
108762306a36Sopenharmony_ci	if ((offset & 1) && remaining > 1)
108862306a36Sopenharmony_ci		goto other_parity_scan;
108962306a36Sopenharmony_ci
109062306a36Sopenharmony_ci	return -EADDRNOTAVAIL;
109162306a36Sopenharmony_ci
109262306a36Sopenharmony_ciok:
109362306a36Sopenharmony_ci	/* Find the corresponding tb2 bucket since we need to
109462306a36Sopenharmony_ci	 * add the socket to the bhash2 table as well
109562306a36Sopenharmony_ci	 */
109662306a36Sopenharmony_ci	head2 = inet_bhashfn_portaddr(hinfo, sk, net, port);
109762306a36Sopenharmony_ci	spin_lock(&head2->lock);
109862306a36Sopenharmony_ci
109962306a36Sopenharmony_ci	tb2 = inet_bind2_bucket_find(head2, net, port, l3mdev, sk);
110062306a36Sopenharmony_ci	if (!tb2) {
110162306a36Sopenharmony_ci		tb2 = inet_bind2_bucket_create(hinfo->bind2_bucket_cachep, net,
110262306a36Sopenharmony_ci					       head2, port, l3mdev, sk);
110362306a36Sopenharmony_ci		if (!tb2)
110462306a36Sopenharmony_ci			goto error;
110562306a36Sopenharmony_ci	}
110662306a36Sopenharmony_ci
110762306a36Sopenharmony_ci	/* Here we want to add a little bit of randomness to the next source
110862306a36Sopenharmony_ci	 * port that will be chosen. We use a max() with a random here so that
110962306a36Sopenharmony_ci	 * on low contention the randomness is maximal and on high contention
111062306a36Sopenharmony_ci	 * it may be inexistent.
111162306a36Sopenharmony_ci	 */
111262306a36Sopenharmony_ci	i = max_t(int, i, get_random_u32_below(8) * 2);
111362306a36Sopenharmony_ci	WRITE_ONCE(table_perturb[index], READ_ONCE(table_perturb[index]) + i + 2);
111462306a36Sopenharmony_ci
111562306a36Sopenharmony_ci	/* Head lock still held and bh's disabled */
111662306a36Sopenharmony_ci	inet_bind_hash(sk, tb, tb2, port);
111762306a36Sopenharmony_ci
111862306a36Sopenharmony_ci	if (sk_unhashed(sk)) {
111962306a36Sopenharmony_ci		inet_sk(sk)->inet_sport = htons(port);
112062306a36Sopenharmony_ci		inet_ehash_nolisten(sk, (struct sock *)tw, NULL);
112162306a36Sopenharmony_ci	}
112262306a36Sopenharmony_ci	if (tw)
112362306a36Sopenharmony_ci		inet_twsk_bind_unhash(tw, hinfo);
112462306a36Sopenharmony_ci
112562306a36Sopenharmony_ci	spin_unlock(&head2->lock);
112662306a36Sopenharmony_ci	spin_unlock(&head->lock);
112762306a36Sopenharmony_ci
112862306a36Sopenharmony_ci	if (tw)
112962306a36Sopenharmony_ci		inet_twsk_deschedule_put(tw);
113062306a36Sopenharmony_ci	local_bh_enable();
113162306a36Sopenharmony_ci	return 0;
113262306a36Sopenharmony_ci
113362306a36Sopenharmony_cierror:
113462306a36Sopenharmony_ci	if (sk_hashed(sk)) {
113562306a36Sopenharmony_ci		spinlock_t *lock = inet_ehash_lockp(hinfo, sk->sk_hash);
113662306a36Sopenharmony_ci
113762306a36Sopenharmony_ci		sock_prot_inuse_add(net, sk->sk_prot, -1);
113862306a36Sopenharmony_ci
113962306a36Sopenharmony_ci		spin_lock(lock);
114062306a36Sopenharmony_ci		__sk_nulls_del_node_init_rcu(sk);
114162306a36Sopenharmony_ci		spin_unlock(lock);
114262306a36Sopenharmony_ci
114362306a36Sopenharmony_ci		sk->sk_hash = 0;
114462306a36Sopenharmony_ci		inet_sk(sk)->inet_sport = 0;
114562306a36Sopenharmony_ci		inet_sk(sk)->inet_num = 0;
114662306a36Sopenharmony_ci
114762306a36Sopenharmony_ci		if (tw)
114862306a36Sopenharmony_ci			inet_twsk_bind_unhash(tw, hinfo);
114962306a36Sopenharmony_ci	}
115062306a36Sopenharmony_ci
115162306a36Sopenharmony_ci	spin_unlock(&head2->lock);
115262306a36Sopenharmony_ci	if (tb_created)
115362306a36Sopenharmony_ci		inet_bind_bucket_destroy(hinfo->bind_bucket_cachep, tb);
115462306a36Sopenharmony_ci	spin_unlock(&head->lock);
115562306a36Sopenharmony_ci
115662306a36Sopenharmony_ci	if (tw)
115762306a36Sopenharmony_ci		inet_twsk_deschedule_put(tw);
115862306a36Sopenharmony_ci
115962306a36Sopenharmony_ci	local_bh_enable();
116062306a36Sopenharmony_ci
116162306a36Sopenharmony_ci	return -ENOMEM;
116262306a36Sopenharmony_ci}
116362306a36Sopenharmony_ci
116462306a36Sopenharmony_ci/*
116562306a36Sopenharmony_ci * Bind a port for a connect operation and hash it.
116662306a36Sopenharmony_ci */
116762306a36Sopenharmony_ciint inet_hash_connect(struct inet_timewait_death_row *death_row,
116862306a36Sopenharmony_ci		      struct sock *sk)
116962306a36Sopenharmony_ci{
117062306a36Sopenharmony_ci	u64 port_offset = 0;
117162306a36Sopenharmony_ci
117262306a36Sopenharmony_ci	if (!inet_sk(sk)->inet_num)
117362306a36Sopenharmony_ci		port_offset = inet_sk_port_offset(sk);
117462306a36Sopenharmony_ci	return __inet_hash_connect(death_row, sk, port_offset,
117562306a36Sopenharmony_ci				   __inet_check_established);
117662306a36Sopenharmony_ci}
117762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(inet_hash_connect);
117862306a36Sopenharmony_ci
117962306a36Sopenharmony_cistatic void init_hashinfo_lhash2(struct inet_hashinfo *h)
118062306a36Sopenharmony_ci{
118162306a36Sopenharmony_ci	int i;
118262306a36Sopenharmony_ci
118362306a36Sopenharmony_ci	for (i = 0; i <= h->lhash2_mask; i++) {
118462306a36Sopenharmony_ci		spin_lock_init(&h->lhash2[i].lock);
118562306a36Sopenharmony_ci		INIT_HLIST_NULLS_HEAD(&h->lhash2[i].nulls_head,
118662306a36Sopenharmony_ci				      i + LISTENING_NULLS_BASE);
118762306a36Sopenharmony_ci	}
118862306a36Sopenharmony_ci}
118962306a36Sopenharmony_ci
119062306a36Sopenharmony_civoid __init inet_hashinfo2_init(struct inet_hashinfo *h, const char *name,
119162306a36Sopenharmony_ci				unsigned long numentries, int scale,
119262306a36Sopenharmony_ci				unsigned long low_limit,
119362306a36Sopenharmony_ci				unsigned long high_limit)
119462306a36Sopenharmony_ci{
119562306a36Sopenharmony_ci	h->lhash2 = alloc_large_system_hash(name,
119662306a36Sopenharmony_ci					    sizeof(*h->lhash2),
119762306a36Sopenharmony_ci					    numentries,
119862306a36Sopenharmony_ci					    scale,
119962306a36Sopenharmony_ci					    0,
120062306a36Sopenharmony_ci					    NULL,
120162306a36Sopenharmony_ci					    &h->lhash2_mask,
120262306a36Sopenharmony_ci					    low_limit,
120362306a36Sopenharmony_ci					    high_limit);
120462306a36Sopenharmony_ci	init_hashinfo_lhash2(h);
120562306a36Sopenharmony_ci
120662306a36Sopenharmony_ci	/* this one is used for source ports of outgoing connections */
120762306a36Sopenharmony_ci	table_perturb = alloc_large_system_hash("Table-perturb",
120862306a36Sopenharmony_ci						sizeof(*table_perturb),
120962306a36Sopenharmony_ci						INET_TABLE_PERTURB_SIZE,
121062306a36Sopenharmony_ci						0, 0, NULL, NULL,
121162306a36Sopenharmony_ci						INET_TABLE_PERTURB_SIZE,
121262306a36Sopenharmony_ci						INET_TABLE_PERTURB_SIZE);
121362306a36Sopenharmony_ci}
121462306a36Sopenharmony_ci
121562306a36Sopenharmony_ciint inet_hashinfo2_init_mod(struct inet_hashinfo *h)
121662306a36Sopenharmony_ci{
121762306a36Sopenharmony_ci	h->lhash2 = kmalloc_array(INET_LHTABLE_SIZE, sizeof(*h->lhash2), GFP_KERNEL);
121862306a36Sopenharmony_ci	if (!h->lhash2)
121962306a36Sopenharmony_ci		return -ENOMEM;
122062306a36Sopenharmony_ci
122162306a36Sopenharmony_ci	h->lhash2_mask = INET_LHTABLE_SIZE - 1;
122262306a36Sopenharmony_ci	/* INET_LHTABLE_SIZE must be a power of 2 */
122362306a36Sopenharmony_ci	BUG_ON(INET_LHTABLE_SIZE & h->lhash2_mask);
122462306a36Sopenharmony_ci
122562306a36Sopenharmony_ci	init_hashinfo_lhash2(h);
122662306a36Sopenharmony_ci	return 0;
122762306a36Sopenharmony_ci}
122862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(inet_hashinfo2_init_mod);
122962306a36Sopenharmony_ci
123062306a36Sopenharmony_ciint inet_ehash_locks_alloc(struct inet_hashinfo *hashinfo)
123162306a36Sopenharmony_ci{
123262306a36Sopenharmony_ci	unsigned int locksz = sizeof(spinlock_t);
123362306a36Sopenharmony_ci	unsigned int i, nblocks = 1;
123462306a36Sopenharmony_ci
123562306a36Sopenharmony_ci	if (locksz != 0) {
123662306a36Sopenharmony_ci		/* allocate 2 cache lines or at least one spinlock per cpu */
123762306a36Sopenharmony_ci		nblocks = max(2U * L1_CACHE_BYTES / locksz, 1U);
123862306a36Sopenharmony_ci		nblocks = roundup_pow_of_two(nblocks * num_possible_cpus());
123962306a36Sopenharmony_ci
124062306a36Sopenharmony_ci		/* no more locks than number of hash buckets */
124162306a36Sopenharmony_ci		nblocks = min(nblocks, hashinfo->ehash_mask + 1);
124262306a36Sopenharmony_ci
124362306a36Sopenharmony_ci		hashinfo->ehash_locks = kvmalloc_array(nblocks, locksz, GFP_KERNEL);
124462306a36Sopenharmony_ci		if (!hashinfo->ehash_locks)
124562306a36Sopenharmony_ci			return -ENOMEM;
124662306a36Sopenharmony_ci
124762306a36Sopenharmony_ci		for (i = 0; i < nblocks; i++)
124862306a36Sopenharmony_ci			spin_lock_init(&hashinfo->ehash_locks[i]);
124962306a36Sopenharmony_ci	}
125062306a36Sopenharmony_ci	hashinfo->ehash_locks_mask = nblocks - 1;
125162306a36Sopenharmony_ci	return 0;
125262306a36Sopenharmony_ci}
125362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(inet_ehash_locks_alloc);
125462306a36Sopenharmony_ci
125562306a36Sopenharmony_cistruct inet_hashinfo *inet_pernet_hashinfo_alloc(struct inet_hashinfo *hashinfo,
125662306a36Sopenharmony_ci						 unsigned int ehash_entries)
125762306a36Sopenharmony_ci{
125862306a36Sopenharmony_ci	struct inet_hashinfo *new_hashinfo;
125962306a36Sopenharmony_ci	int i;
126062306a36Sopenharmony_ci
126162306a36Sopenharmony_ci	new_hashinfo = kmemdup(hashinfo, sizeof(*hashinfo), GFP_KERNEL);
126262306a36Sopenharmony_ci	if (!new_hashinfo)
126362306a36Sopenharmony_ci		goto err;
126462306a36Sopenharmony_ci
126562306a36Sopenharmony_ci	new_hashinfo->ehash = vmalloc_huge(ehash_entries * sizeof(struct inet_ehash_bucket),
126662306a36Sopenharmony_ci					   GFP_KERNEL_ACCOUNT);
126762306a36Sopenharmony_ci	if (!new_hashinfo->ehash)
126862306a36Sopenharmony_ci		goto free_hashinfo;
126962306a36Sopenharmony_ci
127062306a36Sopenharmony_ci	new_hashinfo->ehash_mask = ehash_entries - 1;
127162306a36Sopenharmony_ci
127262306a36Sopenharmony_ci	if (inet_ehash_locks_alloc(new_hashinfo))
127362306a36Sopenharmony_ci		goto free_ehash;
127462306a36Sopenharmony_ci
127562306a36Sopenharmony_ci	for (i = 0; i < ehash_entries; i++)
127662306a36Sopenharmony_ci		INIT_HLIST_NULLS_HEAD(&new_hashinfo->ehash[i].chain, i);
127762306a36Sopenharmony_ci
127862306a36Sopenharmony_ci	new_hashinfo->pernet = true;
127962306a36Sopenharmony_ci
128062306a36Sopenharmony_ci	return new_hashinfo;
128162306a36Sopenharmony_ci
128262306a36Sopenharmony_cifree_ehash:
128362306a36Sopenharmony_ci	vfree(new_hashinfo->ehash);
128462306a36Sopenharmony_cifree_hashinfo:
128562306a36Sopenharmony_ci	kfree(new_hashinfo);
128662306a36Sopenharmony_cierr:
128762306a36Sopenharmony_ci	return NULL;
128862306a36Sopenharmony_ci}
128962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(inet_pernet_hashinfo_alloc);
129062306a36Sopenharmony_ci
129162306a36Sopenharmony_civoid inet_pernet_hashinfo_free(struct inet_hashinfo *hashinfo)
129262306a36Sopenharmony_ci{
129362306a36Sopenharmony_ci	if (!hashinfo->pernet)
129462306a36Sopenharmony_ci		return;
129562306a36Sopenharmony_ci
129662306a36Sopenharmony_ci	inet_ehash_locks_free(hashinfo);
129762306a36Sopenharmony_ci	vfree(hashinfo->ehash);
129862306a36Sopenharmony_ci	kfree(hashinfo);
129962306a36Sopenharmony_ci}
130062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(inet_pernet_hashinfo_free);
1301