xref: /linux/net/ipv4/inet_hashtables.c (revision ab1e0a13d70299e792fd0527cefd070c1405fa5b)
177d8bf9cSArnaldo Carvalho de Melo /*
277d8bf9cSArnaldo Carvalho de Melo  * INET		An implementation of the TCP/IP protocol suite for the LINUX
377d8bf9cSArnaldo Carvalho de Melo  *		operating system.  INET is implemented using the BSD Socket
477d8bf9cSArnaldo Carvalho de Melo  *		interface as the means of communication with the user level.
577d8bf9cSArnaldo Carvalho de Melo  *
677d8bf9cSArnaldo Carvalho de Melo  *		Generic INET transport hashtables
777d8bf9cSArnaldo Carvalho de Melo  *
877d8bf9cSArnaldo Carvalho de Melo  * Authors:	Lotsa people, from code originally in tcp
977d8bf9cSArnaldo Carvalho de Melo  *
1077d8bf9cSArnaldo Carvalho de Melo  *	This program is free software; you can redistribute it and/or
1177d8bf9cSArnaldo Carvalho de Melo  *      modify it under the terms of the GNU General Public License
1277d8bf9cSArnaldo Carvalho de Melo  *      as published by the Free Software Foundation; either version
1377d8bf9cSArnaldo Carvalho de Melo  *      2 of the License, or (at your option) any later version.
1477d8bf9cSArnaldo Carvalho de Melo  */
1577d8bf9cSArnaldo Carvalho de Melo 
162d8c4ce5SArnaldo Carvalho de Melo #include <linux/module.h>
17a7f5e7f1SArnaldo Carvalho de Melo #include <linux/random.h>
18f3f05f70SArnaldo Carvalho de Melo #include <linux/sched.h>
1977d8bf9cSArnaldo Carvalho de Melo #include <linux/slab.h>
20f3f05f70SArnaldo Carvalho de Melo #include <linux/wait.h>
2177d8bf9cSArnaldo Carvalho de Melo 
22463c84b9SArnaldo Carvalho de Melo #include <net/inet_connection_sock.h>
2377d8bf9cSArnaldo Carvalho de Melo #include <net/inet_hashtables.h>
24a7f5e7f1SArnaldo Carvalho de Melo #include <net/ip.h>
2577d8bf9cSArnaldo Carvalho de Melo 
2677d8bf9cSArnaldo Carvalho de Melo /*
2777d8bf9cSArnaldo Carvalho de Melo  * Allocate and initialize a new local port bind bucket.
2877d8bf9cSArnaldo Carvalho de Melo  * The bindhash mutex for snum's hash chain must be held here.
2977d8bf9cSArnaldo Carvalho de Melo  */
30e18b890bSChristoph Lameter struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
31941b1d22SPavel Emelyanov 						 struct net *net,
3277d8bf9cSArnaldo Carvalho de Melo 						 struct inet_bind_hashbucket *head,
3377d8bf9cSArnaldo Carvalho de Melo 						 const unsigned short snum)
3477d8bf9cSArnaldo Carvalho de Melo {
3554e6ecb2SChristoph Lameter 	struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
3677d8bf9cSArnaldo Carvalho de Melo 
3777d8bf9cSArnaldo Carvalho de Melo 	if (tb != NULL) {
38941b1d22SPavel Emelyanov 		tb->ib_net       = net;
3977d8bf9cSArnaldo Carvalho de Melo 		tb->port      = snum;
4077d8bf9cSArnaldo Carvalho de Melo 		tb->fastreuse = 0;
4177d8bf9cSArnaldo Carvalho de Melo 		INIT_HLIST_HEAD(&tb->owners);
4277d8bf9cSArnaldo Carvalho de Melo 		hlist_add_head(&tb->node, &head->chain);
4377d8bf9cSArnaldo Carvalho de Melo 	}
4477d8bf9cSArnaldo Carvalho de Melo 	return tb;
4577d8bf9cSArnaldo Carvalho de Melo }
4677d8bf9cSArnaldo Carvalho de Melo 
4777d8bf9cSArnaldo Carvalho de Melo /*
4877d8bf9cSArnaldo Carvalho de Melo  * Caller must hold hashbucket lock for this tb with local BH disabled
4977d8bf9cSArnaldo Carvalho de Melo  */
50e18b890bSChristoph Lameter void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket *tb)
5177d8bf9cSArnaldo Carvalho de Melo {
5277d8bf9cSArnaldo Carvalho de Melo 	if (hlist_empty(&tb->owners)) {
5377d8bf9cSArnaldo Carvalho de Melo 		__hlist_del(&tb->node);
5477d8bf9cSArnaldo Carvalho de Melo 		kmem_cache_free(cachep, tb);
5577d8bf9cSArnaldo Carvalho de Melo 	}
5677d8bf9cSArnaldo Carvalho de Melo }
572d8c4ce5SArnaldo Carvalho de Melo 
582d8c4ce5SArnaldo Carvalho de Melo void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
592d8c4ce5SArnaldo Carvalho de Melo 		    const unsigned short snum)
602d8c4ce5SArnaldo Carvalho de Melo {
61463c84b9SArnaldo Carvalho de Melo 	inet_sk(sk)->num = snum;
622d8c4ce5SArnaldo Carvalho de Melo 	sk_add_bind_node(sk, &tb->owners);
63463c84b9SArnaldo Carvalho de Melo 	inet_csk(sk)->icsk_bind_hash = tb;
642d8c4ce5SArnaldo Carvalho de Melo }
652d8c4ce5SArnaldo Carvalho de Melo 
662d8c4ce5SArnaldo Carvalho de Melo /*
672d8c4ce5SArnaldo Carvalho de Melo  * Get rid of any references to a local port held by the given sock.
682d8c4ce5SArnaldo Carvalho de Melo  */
69*ab1e0a13SArnaldo Carvalho de Melo static void __inet_put_port(struct sock *sk)
702d8c4ce5SArnaldo Carvalho de Melo {
71*ab1e0a13SArnaldo Carvalho de Melo 	struct inet_hashinfo *hashinfo = sk->sk_prot->hashinfo;
72463c84b9SArnaldo Carvalho de Melo 	const int bhash = inet_bhashfn(inet_sk(sk)->num, hashinfo->bhash_size);
732d8c4ce5SArnaldo Carvalho de Melo 	struct inet_bind_hashbucket *head = &hashinfo->bhash[bhash];
742d8c4ce5SArnaldo Carvalho de Melo 	struct inet_bind_bucket *tb;
752d8c4ce5SArnaldo Carvalho de Melo 
762d8c4ce5SArnaldo Carvalho de Melo 	spin_lock(&head->lock);
77463c84b9SArnaldo Carvalho de Melo 	tb = inet_csk(sk)->icsk_bind_hash;
782d8c4ce5SArnaldo Carvalho de Melo 	__sk_del_bind_node(sk);
79463c84b9SArnaldo Carvalho de Melo 	inet_csk(sk)->icsk_bind_hash = NULL;
80463c84b9SArnaldo Carvalho de Melo 	inet_sk(sk)->num = 0;
812d8c4ce5SArnaldo Carvalho de Melo 	inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
822d8c4ce5SArnaldo Carvalho de Melo 	spin_unlock(&head->lock);
832d8c4ce5SArnaldo Carvalho de Melo }
842d8c4ce5SArnaldo Carvalho de Melo 
85*ab1e0a13SArnaldo Carvalho de Melo void inet_put_port(struct sock *sk)
862d8c4ce5SArnaldo Carvalho de Melo {
872d8c4ce5SArnaldo Carvalho de Melo 	local_bh_disable();
88*ab1e0a13SArnaldo Carvalho de Melo 	__inet_put_port(sk);
892d8c4ce5SArnaldo Carvalho de Melo 	local_bh_enable();
902d8c4ce5SArnaldo Carvalho de Melo }
912d8c4ce5SArnaldo Carvalho de Melo 
922d8c4ce5SArnaldo Carvalho de Melo EXPORT_SYMBOL(inet_put_port);
93f3f05f70SArnaldo Carvalho de Melo 
94f3f05f70SArnaldo Carvalho de Melo /*
95f3f05f70SArnaldo Carvalho de Melo  * This lock without WQ_FLAG_EXCLUSIVE is good on UP and it can be very bad on SMP.
96f3f05f70SArnaldo Carvalho de Melo  * Look, when several writers sleep and reader wakes them up, all but one
97f3f05f70SArnaldo Carvalho de Melo  * immediately hit write lock and grab all the cpus. Exclusive sleep solves
98f3f05f70SArnaldo Carvalho de Melo  * this, _but_ remember, it adds useless work on UP machines (wake up each
99f3f05f70SArnaldo Carvalho de Melo  * exclusive lock release). It should be ifdefed really.
100f3f05f70SArnaldo Carvalho de Melo  */
101f3f05f70SArnaldo Carvalho de Melo void inet_listen_wlock(struct inet_hashinfo *hashinfo)
1029a429c49SEric Dumazet 	__acquires(hashinfo->lhash_lock)
103f3f05f70SArnaldo Carvalho de Melo {
104f3f05f70SArnaldo Carvalho de Melo 	write_lock(&hashinfo->lhash_lock);
105f3f05f70SArnaldo Carvalho de Melo 
106f3f05f70SArnaldo Carvalho de Melo 	if (atomic_read(&hashinfo->lhash_users)) {
107f3f05f70SArnaldo Carvalho de Melo 		DEFINE_WAIT(wait);
108f3f05f70SArnaldo Carvalho de Melo 
109f3f05f70SArnaldo Carvalho de Melo 		for (;;) {
110f3f05f70SArnaldo Carvalho de Melo 			prepare_to_wait_exclusive(&hashinfo->lhash_wait,
111f3f05f70SArnaldo Carvalho de Melo 						  &wait, TASK_UNINTERRUPTIBLE);
112f3f05f70SArnaldo Carvalho de Melo 			if (!atomic_read(&hashinfo->lhash_users))
113f3f05f70SArnaldo Carvalho de Melo 				break;
114f3f05f70SArnaldo Carvalho de Melo 			write_unlock_bh(&hashinfo->lhash_lock);
115f3f05f70SArnaldo Carvalho de Melo 			schedule();
116f3f05f70SArnaldo Carvalho de Melo 			write_lock_bh(&hashinfo->lhash_lock);
117f3f05f70SArnaldo Carvalho de Melo 		}
118f3f05f70SArnaldo Carvalho de Melo 
119f3f05f70SArnaldo Carvalho de Melo 		finish_wait(&hashinfo->lhash_wait, &wait);
120f3f05f70SArnaldo Carvalho de Melo 	}
121f3f05f70SArnaldo Carvalho de Melo }
122f3f05f70SArnaldo Carvalho de Melo 
123f3f05f70SArnaldo Carvalho de Melo EXPORT_SYMBOL(inet_listen_wlock);
12433b62231SArnaldo Carvalho de Melo 
12533b62231SArnaldo Carvalho de Melo /*
12633b62231SArnaldo Carvalho de Melo  * Don't inline this cruft. Here are some nice properties to exploit here. The
12733b62231SArnaldo Carvalho de Melo  * BSD API does not allow a listening sock to specify the remote port nor the
12833b62231SArnaldo Carvalho de Melo  * remote address for the connection. So always assume those are both
12933b62231SArnaldo Carvalho de Melo  * wildcarded during the search since they can never be otherwise.
13033b62231SArnaldo Carvalho de Melo  */
131c67499c0SPavel Emelyanov static struct sock *inet_lookup_listener_slow(struct net *net,
132c67499c0SPavel Emelyanov 					      const struct hlist_head *head,
133fb99c848SAl Viro 					      const __be32 daddr,
13499a92ff5SHerbert Xu 					      const unsigned short hnum,
13599a92ff5SHerbert Xu 					      const int dif)
13633b62231SArnaldo Carvalho de Melo {
13733b62231SArnaldo Carvalho de Melo 	struct sock *result = NULL, *sk;
13833b62231SArnaldo Carvalho de Melo 	const struct hlist_node *node;
13933b62231SArnaldo Carvalho de Melo 	int hiscore = -1;
14033b62231SArnaldo Carvalho de Melo 
14133b62231SArnaldo Carvalho de Melo 	sk_for_each(sk, node, head) {
14233b62231SArnaldo Carvalho de Melo 		const struct inet_sock *inet = inet_sk(sk);
14333b62231SArnaldo Carvalho de Melo 
144c67499c0SPavel Emelyanov 		if (sk->sk_net == net && inet->num == hnum &&
145c67499c0SPavel Emelyanov 				!ipv6_only_sock(sk)) {
146fb99c848SAl Viro 			const __be32 rcv_saddr = inet->rcv_saddr;
14733b62231SArnaldo Carvalho de Melo 			int score = sk->sk_family == PF_INET ? 1 : 0;
14833b62231SArnaldo Carvalho de Melo 
14933b62231SArnaldo Carvalho de Melo 			if (rcv_saddr) {
15033b62231SArnaldo Carvalho de Melo 				if (rcv_saddr != daddr)
15133b62231SArnaldo Carvalho de Melo 					continue;
15233b62231SArnaldo Carvalho de Melo 				score += 2;
15333b62231SArnaldo Carvalho de Melo 			}
15433b62231SArnaldo Carvalho de Melo 			if (sk->sk_bound_dev_if) {
15533b62231SArnaldo Carvalho de Melo 				if (sk->sk_bound_dev_if != dif)
15633b62231SArnaldo Carvalho de Melo 					continue;
15733b62231SArnaldo Carvalho de Melo 				score += 2;
15833b62231SArnaldo Carvalho de Melo 			}
15933b62231SArnaldo Carvalho de Melo 			if (score == 5)
16033b62231SArnaldo Carvalho de Melo 				return sk;
16133b62231SArnaldo Carvalho de Melo 			if (score > hiscore) {
16233b62231SArnaldo Carvalho de Melo 				hiscore	= score;
16333b62231SArnaldo Carvalho de Melo 				result	= sk;
16433b62231SArnaldo Carvalho de Melo 			}
16533b62231SArnaldo Carvalho de Melo 		}
16633b62231SArnaldo Carvalho de Melo 	}
16733b62231SArnaldo Carvalho de Melo 	return result;
16833b62231SArnaldo Carvalho de Melo }
169e48c414eSArnaldo Carvalho de Melo 
17099a92ff5SHerbert Xu /* Optimize the common listener case. */
171c67499c0SPavel Emelyanov struct sock *__inet_lookup_listener(struct net *net,
172c67499c0SPavel Emelyanov 				    struct inet_hashinfo *hashinfo,
173fb99c848SAl Viro 				    const __be32 daddr, const unsigned short hnum,
17499a92ff5SHerbert Xu 				    const int dif)
17599a92ff5SHerbert Xu {
17699a92ff5SHerbert Xu 	struct sock *sk = NULL;
17799a92ff5SHerbert Xu 	const struct hlist_head *head;
17899a92ff5SHerbert Xu 
17999a92ff5SHerbert Xu 	read_lock(&hashinfo->lhash_lock);
18099a92ff5SHerbert Xu 	head = &hashinfo->listening_hash[inet_lhashfn(hnum)];
18199a92ff5SHerbert Xu 	if (!hlist_empty(head)) {
18299a92ff5SHerbert Xu 		const struct inet_sock *inet = inet_sk((sk = __sk_head(head)));
18399a92ff5SHerbert Xu 
18499a92ff5SHerbert Xu 		if (inet->num == hnum && !sk->sk_node.next &&
18599a92ff5SHerbert Xu 		    (!inet->rcv_saddr || inet->rcv_saddr == daddr) &&
18699a92ff5SHerbert Xu 		    (sk->sk_family == PF_INET || !ipv6_only_sock(sk)) &&
187c67499c0SPavel Emelyanov 		    !sk->sk_bound_dev_if && sk->sk_net == net)
18899a92ff5SHerbert Xu 			goto sherry_cache;
189c67499c0SPavel Emelyanov 		sk = inet_lookup_listener_slow(net, head, daddr, hnum, dif);
19099a92ff5SHerbert Xu 	}
19199a92ff5SHerbert Xu 	if (sk) {
19299a92ff5SHerbert Xu sherry_cache:
19399a92ff5SHerbert Xu 		sock_hold(sk);
19499a92ff5SHerbert Xu 	}
19599a92ff5SHerbert Xu 	read_unlock(&hashinfo->lhash_lock);
19699a92ff5SHerbert Xu 	return sk;
19799a92ff5SHerbert Xu }
1988f491069SHerbert Xu EXPORT_SYMBOL_GPL(__inet_lookup_listener);
199a7f5e7f1SArnaldo Carvalho de Melo 
200c67499c0SPavel Emelyanov struct sock * __inet_lookup_established(struct net *net,
201c67499c0SPavel Emelyanov 				  struct inet_hashinfo *hashinfo,
20277a5ba55SPavel Emelyanov 				  const __be32 saddr, const __be16 sport,
20377a5ba55SPavel Emelyanov 				  const __be32 daddr, const u16 hnum,
20477a5ba55SPavel Emelyanov 				  const int dif)
20577a5ba55SPavel Emelyanov {
20677a5ba55SPavel Emelyanov 	INET_ADDR_COOKIE(acookie, saddr, daddr)
20777a5ba55SPavel Emelyanov 	const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
20877a5ba55SPavel Emelyanov 	struct sock *sk;
20977a5ba55SPavel Emelyanov 	const struct hlist_node *node;
21077a5ba55SPavel Emelyanov 	/* Optimize here for direct hit, only listening connections can
21177a5ba55SPavel Emelyanov 	 * have wildcards anyways.
21277a5ba55SPavel Emelyanov 	 */
21377a5ba55SPavel Emelyanov 	unsigned int hash = inet_ehashfn(daddr, hnum, saddr, sport);
21477a5ba55SPavel Emelyanov 	struct inet_ehash_bucket *head = inet_ehash_bucket(hashinfo, hash);
21577a5ba55SPavel Emelyanov 	rwlock_t *lock = inet_ehash_lockp(hashinfo, hash);
21677a5ba55SPavel Emelyanov 
21777a5ba55SPavel Emelyanov 	prefetch(head->chain.first);
21877a5ba55SPavel Emelyanov 	read_lock(lock);
21977a5ba55SPavel Emelyanov 	sk_for_each(sk, node, &head->chain) {
220c67499c0SPavel Emelyanov 		if (INET_MATCH(sk, net, hash, acookie,
221c67499c0SPavel Emelyanov 					saddr, daddr, ports, dif))
22277a5ba55SPavel Emelyanov 			goto hit; /* You sunk my battleship! */
22377a5ba55SPavel Emelyanov 	}
22477a5ba55SPavel Emelyanov 
22577a5ba55SPavel Emelyanov 	/* Must check for a TIME_WAIT'er before going to listener hash. */
22677a5ba55SPavel Emelyanov 	sk_for_each(sk, node, &head->twchain) {
227c67499c0SPavel Emelyanov 		if (INET_TW_MATCH(sk, net, hash, acookie,
228c67499c0SPavel Emelyanov 					saddr, daddr, ports, dif))
22977a5ba55SPavel Emelyanov 			goto hit;
23077a5ba55SPavel Emelyanov 	}
23177a5ba55SPavel Emelyanov 	sk = NULL;
23277a5ba55SPavel Emelyanov out:
23377a5ba55SPavel Emelyanov 	read_unlock(lock);
23477a5ba55SPavel Emelyanov 	return sk;
23577a5ba55SPavel Emelyanov hit:
23677a5ba55SPavel Emelyanov 	sock_hold(sk);
23777a5ba55SPavel Emelyanov 	goto out;
23877a5ba55SPavel Emelyanov }
23977a5ba55SPavel Emelyanov EXPORT_SYMBOL_GPL(__inet_lookup_established);
24077a5ba55SPavel Emelyanov 
241a7f5e7f1SArnaldo Carvalho de Melo /* called with local bh disabled */
242a7f5e7f1SArnaldo Carvalho de Melo static int __inet_check_established(struct inet_timewait_death_row *death_row,
243a7f5e7f1SArnaldo Carvalho de Melo 				    struct sock *sk, __u16 lport,
244a7f5e7f1SArnaldo Carvalho de Melo 				    struct inet_timewait_sock **twp)
245a7f5e7f1SArnaldo Carvalho de Melo {
246a7f5e7f1SArnaldo Carvalho de Melo 	struct inet_hashinfo *hinfo = death_row->hashinfo;
247a7f5e7f1SArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
248fb99c848SAl Viro 	__be32 daddr = inet->rcv_saddr;
249fb99c848SAl Viro 	__be32 saddr = inet->daddr;
250a7f5e7f1SArnaldo Carvalho de Melo 	int dif = sk->sk_bound_dev_if;
251a7f5e7f1SArnaldo Carvalho de Melo 	INET_ADDR_COOKIE(acookie, saddr, daddr)
2524f765d84SAl Viro 	const __portpair ports = INET_COMBINED_PORTS(inet->dport, lport);
253a7f5e7f1SArnaldo Carvalho de Melo 	unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport);
254a7f5e7f1SArnaldo Carvalho de Melo 	struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
255230140cfSEric Dumazet 	rwlock_t *lock = inet_ehash_lockp(hinfo, hash);
256a7f5e7f1SArnaldo Carvalho de Melo 	struct sock *sk2;
257a7f5e7f1SArnaldo Carvalho de Melo 	const struct hlist_node *node;
258a7f5e7f1SArnaldo Carvalho de Melo 	struct inet_timewait_sock *tw;
259c67499c0SPavel Emelyanov 	struct net *net = sk->sk_net;
260a7f5e7f1SArnaldo Carvalho de Melo 
261a7f5e7f1SArnaldo Carvalho de Melo 	prefetch(head->chain.first);
262230140cfSEric Dumazet 	write_lock(lock);
263a7f5e7f1SArnaldo Carvalho de Melo 
264a7f5e7f1SArnaldo Carvalho de Melo 	/* Check TIME-WAIT sockets first. */
265dbca9b27SEric Dumazet 	sk_for_each(sk2, node, &head->twchain) {
266a7f5e7f1SArnaldo Carvalho de Melo 		tw = inet_twsk(sk2);
267a7f5e7f1SArnaldo Carvalho de Melo 
268c67499c0SPavel Emelyanov 		if (INET_TW_MATCH(sk2, net, hash, acookie,
269c67499c0SPavel Emelyanov 					saddr, daddr, ports, dif)) {
270a7f5e7f1SArnaldo Carvalho de Melo 			if (twsk_unique(sk, sk2, twp))
271a7f5e7f1SArnaldo Carvalho de Melo 				goto unique;
272a7f5e7f1SArnaldo Carvalho de Melo 			else
273a7f5e7f1SArnaldo Carvalho de Melo 				goto not_unique;
274a7f5e7f1SArnaldo Carvalho de Melo 		}
275a7f5e7f1SArnaldo Carvalho de Melo 	}
276a7f5e7f1SArnaldo Carvalho de Melo 	tw = NULL;
277a7f5e7f1SArnaldo Carvalho de Melo 
278a7f5e7f1SArnaldo Carvalho de Melo 	/* And established part... */
279a7f5e7f1SArnaldo Carvalho de Melo 	sk_for_each(sk2, node, &head->chain) {
280c67499c0SPavel Emelyanov 		if (INET_MATCH(sk2, net, hash, acookie,
281c67499c0SPavel Emelyanov 					saddr, daddr, ports, dif))
282a7f5e7f1SArnaldo Carvalho de Melo 			goto not_unique;
283a7f5e7f1SArnaldo Carvalho de Melo 	}
284a7f5e7f1SArnaldo Carvalho de Melo 
285a7f5e7f1SArnaldo Carvalho de Melo unique:
286a7f5e7f1SArnaldo Carvalho de Melo 	/* Must record num and sport now. Otherwise we will see
287a7f5e7f1SArnaldo Carvalho de Melo 	 * in hash table socket with a funny identity. */
288a7f5e7f1SArnaldo Carvalho de Melo 	inet->num = lport;
289a7f5e7f1SArnaldo Carvalho de Melo 	inet->sport = htons(lport);
290a7f5e7f1SArnaldo Carvalho de Melo 	sk->sk_hash = hash;
291a7f5e7f1SArnaldo Carvalho de Melo 	BUG_TRAP(sk_unhashed(sk));
292a7f5e7f1SArnaldo Carvalho de Melo 	__sk_add_node(sk, &head->chain);
29365f76517SEric Dumazet 	sock_prot_inuse_add(sk->sk_prot, 1);
294230140cfSEric Dumazet 	write_unlock(lock);
295a7f5e7f1SArnaldo Carvalho de Melo 
296a7f5e7f1SArnaldo Carvalho de Melo 	if (twp) {
297a7f5e7f1SArnaldo Carvalho de Melo 		*twp = tw;
298a7f5e7f1SArnaldo Carvalho de Melo 		NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
299a7f5e7f1SArnaldo Carvalho de Melo 	} else if (tw) {
300a7f5e7f1SArnaldo Carvalho de Melo 		/* Silly. Should hash-dance instead... */
301a7f5e7f1SArnaldo Carvalho de Melo 		inet_twsk_deschedule(tw, death_row);
302a7f5e7f1SArnaldo Carvalho de Melo 		NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
303a7f5e7f1SArnaldo Carvalho de Melo 
304a7f5e7f1SArnaldo Carvalho de Melo 		inet_twsk_put(tw);
305a7f5e7f1SArnaldo Carvalho de Melo 	}
306a7f5e7f1SArnaldo Carvalho de Melo 
307a7f5e7f1SArnaldo Carvalho de Melo 	return 0;
308a7f5e7f1SArnaldo Carvalho de Melo 
309a7f5e7f1SArnaldo Carvalho de Melo not_unique:
310230140cfSEric Dumazet 	write_unlock(lock);
311a7f5e7f1SArnaldo Carvalho de Melo 	return -EADDRNOTAVAIL;
312a7f5e7f1SArnaldo Carvalho de Melo }
313a7f5e7f1SArnaldo Carvalho de Melo 
314a7f5e7f1SArnaldo Carvalho de Melo static inline u32 inet_sk_port_offset(const struct sock *sk)
315a7f5e7f1SArnaldo Carvalho de Melo {
316a7f5e7f1SArnaldo Carvalho de Melo 	const struct inet_sock *inet = inet_sk(sk);
317a7f5e7f1SArnaldo Carvalho de Melo 	return secure_ipv4_port_ephemeral(inet->rcv_saddr, inet->daddr,
318a7f5e7f1SArnaldo Carvalho de Melo 					  inet->dport);
319a7f5e7f1SArnaldo Carvalho de Melo }
320a7f5e7f1SArnaldo Carvalho de Melo 
321*ab1e0a13SArnaldo Carvalho de Melo void __inet_hash_nolisten(struct sock *sk)
322152da81dSPavel Emelyanov {
323*ab1e0a13SArnaldo Carvalho de Melo 	struct inet_hashinfo *hashinfo = sk->sk_prot->hashinfo;
324152da81dSPavel Emelyanov 	struct hlist_head *list;
325152da81dSPavel Emelyanov 	rwlock_t *lock;
326152da81dSPavel Emelyanov 	struct inet_ehash_bucket *head;
327152da81dSPavel Emelyanov 
328152da81dSPavel Emelyanov 	BUG_TRAP(sk_unhashed(sk));
329152da81dSPavel Emelyanov 
330152da81dSPavel Emelyanov 	sk->sk_hash = inet_sk_ehashfn(sk);
331152da81dSPavel Emelyanov 	head = inet_ehash_bucket(hashinfo, sk->sk_hash);
332152da81dSPavel Emelyanov 	list = &head->chain;
333152da81dSPavel Emelyanov 	lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
334152da81dSPavel Emelyanov 
335152da81dSPavel Emelyanov 	write_lock(lock);
336152da81dSPavel Emelyanov 	__sk_add_node(sk, list);
33765f76517SEric Dumazet 	sock_prot_inuse_add(sk->sk_prot, 1);
338152da81dSPavel Emelyanov 	write_unlock(lock);
339152da81dSPavel Emelyanov }
340152da81dSPavel Emelyanov EXPORT_SYMBOL_GPL(__inet_hash_nolisten);
341152da81dSPavel Emelyanov 
342*ab1e0a13SArnaldo Carvalho de Melo static void __inet_hash(struct sock *sk)
343152da81dSPavel Emelyanov {
344*ab1e0a13SArnaldo Carvalho de Melo 	struct inet_hashinfo *hashinfo = sk->sk_prot->hashinfo;
345152da81dSPavel Emelyanov 	struct hlist_head *list;
346152da81dSPavel Emelyanov 	rwlock_t *lock;
347152da81dSPavel Emelyanov 
348152da81dSPavel Emelyanov 	if (sk->sk_state != TCP_LISTEN) {
349*ab1e0a13SArnaldo Carvalho de Melo 		__inet_hash_nolisten(sk);
350152da81dSPavel Emelyanov 		return;
351152da81dSPavel Emelyanov 	}
352152da81dSPavel Emelyanov 
353152da81dSPavel Emelyanov 	BUG_TRAP(sk_unhashed(sk));
354152da81dSPavel Emelyanov 	list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
355152da81dSPavel Emelyanov 	lock = &hashinfo->lhash_lock;
356152da81dSPavel Emelyanov 
357152da81dSPavel Emelyanov 	inet_listen_wlock(hashinfo);
358152da81dSPavel Emelyanov 	__sk_add_node(sk, list);
35965f76517SEric Dumazet 	sock_prot_inuse_add(sk->sk_prot, 1);
360152da81dSPavel Emelyanov 	write_unlock(lock);
361152da81dSPavel Emelyanov 	wake_up(&hashinfo->lhash_wait);
362152da81dSPavel Emelyanov }
363*ab1e0a13SArnaldo Carvalho de Melo 
364*ab1e0a13SArnaldo Carvalho de Melo void inet_hash(struct sock *sk)
365*ab1e0a13SArnaldo Carvalho de Melo {
366*ab1e0a13SArnaldo Carvalho de Melo 	if (sk->sk_state != TCP_CLOSE) {
367*ab1e0a13SArnaldo Carvalho de Melo 		local_bh_disable();
368*ab1e0a13SArnaldo Carvalho de Melo 		__inet_hash(sk);
369*ab1e0a13SArnaldo Carvalho de Melo 		local_bh_enable();
370*ab1e0a13SArnaldo Carvalho de Melo 	}
371*ab1e0a13SArnaldo Carvalho de Melo }
372*ab1e0a13SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_hash);
373*ab1e0a13SArnaldo Carvalho de Melo 
374*ab1e0a13SArnaldo Carvalho de Melo void inet_unhash(struct sock *sk)
375*ab1e0a13SArnaldo Carvalho de Melo {
376*ab1e0a13SArnaldo Carvalho de Melo 	rwlock_t *lock;
377*ab1e0a13SArnaldo Carvalho de Melo 	struct inet_hashinfo *hashinfo = sk->sk_prot->hashinfo;
378*ab1e0a13SArnaldo Carvalho de Melo 
379*ab1e0a13SArnaldo Carvalho de Melo 	if (sk_unhashed(sk))
380*ab1e0a13SArnaldo Carvalho de Melo 		goto out;
381*ab1e0a13SArnaldo Carvalho de Melo 
382*ab1e0a13SArnaldo Carvalho de Melo 	if (sk->sk_state == TCP_LISTEN) {
383*ab1e0a13SArnaldo Carvalho de Melo 		local_bh_disable();
384*ab1e0a13SArnaldo Carvalho de Melo 		inet_listen_wlock(hashinfo);
385*ab1e0a13SArnaldo Carvalho de Melo 		lock = &hashinfo->lhash_lock;
386*ab1e0a13SArnaldo Carvalho de Melo 	} else {
387*ab1e0a13SArnaldo Carvalho de Melo 		lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
388*ab1e0a13SArnaldo Carvalho de Melo 		write_lock_bh(lock);
389*ab1e0a13SArnaldo Carvalho de Melo 	}
390*ab1e0a13SArnaldo Carvalho de Melo 
391*ab1e0a13SArnaldo Carvalho de Melo 	if (__sk_del_node_init(sk))
392*ab1e0a13SArnaldo Carvalho de Melo 		sock_prot_inuse_add(sk->sk_prot, -1);
393*ab1e0a13SArnaldo Carvalho de Melo 	write_unlock_bh(lock);
394*ab1e0a13SArnaldo Carvalho de Melo out:
395*ab1e0a13SArnaldo Carvalho de Melo 	if (sk->sk_state == TCP_LISTEN)
396*ab1e0a13SArnaldo Carvalho de Melo 		wake_up(&hashinfo->lhash_wait);
397*ab1e0a13SArnaldo Carvalho de Melo }
398*ab1e0a13SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_unhash);
399152da81dSPavel Emelyanov 
4005ee31fc1SPavel Emelyanov int __inet_hash_connect(struct inet_timewait_death_row *death_row,
4015ee31fc1SPavel Emelyanov 		struct sock *sk,
4025ee31fc1SPavel Emelyanov 		int (*check_established)(struct inet_timewait_death_row *,
4035ee31fc1SPavel Emelyanov 			struct sock *, __u16, struct inet_timewait_sock **),
404*ab1e0a13SArnaldo Carvalho de Melo 		void (*hash)(struct sock *sk))
405a7f5e7f1SArnaldo Carvalho de Melo {
406a7f5e7f1SArnaldo Carvalho de Melo 	struct inet_hashinfo *hinfo = death_row->hashinfo;
407a7f5e7f1SArnaldo Carvalho de Melo 	const unsigned short snum = inet_sk(sk)->num;
408a7f5e7f1SArnaldo Carvalho de Melo 	struct inet_bind_hashbucket *head;
409a7f5e7f1SArnaldo Carvalho de Melo 	struct inet_bind_bucket *tb;
410a7f5e7f1SArnaldo Carvalho de Melo 	int ret;
411941b1d22SPavel Emelyanov 	struct net *net = sk->sk_net;
412a7f5e7f1SArnaldo Carvalho de Melo 
413a7f5e7f1SArnaldo Carvalho de Melo 	if (!snum) {
414227b60f5SStephen Hemminger 		int i, remaining, low, high, port;
415a7f5e7f1SArnaldo Carvalho de Melo 		static u32 hint;
416a7f5e7f1SArnaldo Carvalho de Melo 		u32 offset = hint + inet_sk_port_offset(sk);
417a7f5e7f1SArnaldo Carvalho de Melo 		struct hlist_node *node;
418a7f5e7f1SArnaldo Carvalho de Melo 		struct inet_timewait_sock *tw = NULL;
419a7f5e7f1SArnaldo Carvalho de Melo 
420227b60f5SStephen Hemminger 		inet_get_local_port_range(&low, &high);
421a25de534SAnton Arapov 		remaining = (high - low) + 1;
422227b60f5SStephen Hemminger 
423a7f5e7f1SArnaldo Carvalho de Melo 		local_bh_disable();
424227b60f5SStephen Hemminger 		for (i = 1; i <= remaining; i++) {
425227b60f5SStephen Hemminger 			port = low + (i + offset) % remaining;
426a7f5e7f1SArnaldo Carvalho de Melo 			head = &hinfo->bhash[inet_bhashfn(port, hinfo->bhash_size)];
427a7f5e7f1SArnaldo Carvalho de Melo 			spin_lock(&head->lock);
428a7f5e7f1SArnaldo Carvalho de Melo 
429a7f5e7f1SArnaldo Carvalho de Melo 			/* Does not bother with rcv_saddr checks,
430a7f5e7f1SArnaldo Carvalho de Melo 			 * because the established check is already
431a7f5e7f1SArnaldo Carvalho de Melo 			 * unique enough.
432a7f5e7f1SArnaldo Carvalho de Melo 			 */
433a7f5e7f1SArnaldo Carvalho de Melo 			inet_bind_bucket_for_each(tb, node, &head->chain) {
434941b1d22SPavel Emelyanov 				if (tb->ib_net == net && tb->port == port) {
435a7f5e7f1SArnaldo Carvalho de Melo 					BUG_TRAP(!hlist_empty(&tb->owners));
436a7f5e7f1SArnaldo Carvalho de Melo 					if (tb->fastreuse >= 0)
437a7f5e7f1SArnaldo Carvalho de Melo 						goto next_port;
4385ee31fc1SPavel Emelyanov 					if (!check_established(death_row, sk,
4395ee31fc1SPavel Emelyanov 								port, &tw))
440a7f5e7f1SArnaldo Carvalho de Melo 						goto ok;
441a7f5e7f1SArnaldo Carvalho de Melo 					goto next_port;
442a7f5e7f1SArnaldo Carvalho de Melo 				}
443a7f5e7f1SArnaldo Carvalho de Melo 			}
444a7f5e7f1SArnaldo Carvalho de Melo 
445941b1d22SPavel Emelyanov 			tb = inet_bind_bucket_create(hinfo->bind_bucket_cachep,
446941b1d22SPavel Emelyanov 					net, head, port);
447a7f5e7f1SArnaldo Carvalho de Melo 			if (!tb) {
448a7f5e7f1SArnaldo Carvalho de Melo 				spin_unlock(&head->lock);
449a7f5e7f1SArnaldo Carvalho de Melo 				break;
450a7f5e7f1SArnaldo Carvalho de Melo 			}
451a7f5e7f1SArnaldo Carvalho de Melo 			tb->fastreuse = -1;
452a7f5e7f1SArnaldo Carvalho de Melo 			goto ok;
453a7f5e7f1SArnaldo Carvalho de Melo 
454a7f5e7f1SArnaldo Carvalho de Melo 		next_port:
455a7f5e7f1SArnaldo Carvalho de Melo 			spin_unlock(&head->lock);
456a7f5e7f1SArnaldo Carvalho de Melo 		}
457a7f5e7f1SArnaldo Carvalho de Melo 		local_bh_enable();
458a7f5e7f1SArnaldo Carvalho de Melo 
459a7f5e7f1SArnaldo Carvalho de Melo 		return -EADDRNOTAVAIL;
460a7f5e7f1SArnaldo Carvalho de Melo 
461a7f5e7f1SArnaldo Carvalho de Melo ok:
462a7f5e7f1SArnaldo Carvalho de Melo 		hint += i;
463a7f5e7f1SArnaldo Carvalho de Melo 
464a7f5e7f1SArnaldo Carvalho de Melo 		/* Head lock still held and bh's disabled */
465a7f5e7f1SArnaldo Carvalho de Melo 		inet_bind_hash(sk, tb, port);
466a7f5e7f1SArnaldo Carvalho de Melo 		if (sk_unhashed(sk)) {
467a7f5e7f1SArnaldo Carvalho de Melo 			inet_sk(sk)->sport = htons(port);
468*ab1e0a13SArnaldo Carvalho de Melo 			hash(sk);
469a7f5e7f1SArnaldo Carvalho de Melo 		}
470a7f5e7f1SArnaldo Carvalho de Melo 		spin_unlock(&head->lock);
471a7f5e7f1SArnaldo Carvalho de Melo 
472a7f5e7f1SArnaldo Carvalho de Melo 		if (tw) {
47353b3531bSAlexey Dobriyan 			inet_twsk_deschedule(tw, death_row);
474a7f5e7f1SArnaldo Carvalho de Melo 			inet_twsk_put(tw);
475a7f5e7f1SArnaldo Carvalho de Melo 		}
476a7f5e7f1SArnaldo Carvalho de Melo 
477a7f5e7f1SArnaldo Carvalho de Melo 		ret = 0;
478a7f5e7f1SArnaldo Carvalho de Melo 		goto out;
479a7f5e7f1SArnaldo Carvalho de Melo 	}
480a7f5e7f1SArnaldo Carvalho de Melo 
481a7f5e7f1SArnaldo Carvalho de Melo 	head = &hinfo->bhash[inet_bhashfn(snum, hinfo->bhash_size)];
482a7f5e7f1SArnaldo Carvalho de Melo 	tb  = inet_csk(sk)->icsk_bind_hash;
483a7f5e7f1SArnaldo Carvalho de Melo 	spin_lock_bh(&head->lock);
484a7f5e7f1SArnaldo Carvalho de Melo 	if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
485*ab1e0a13SArnaldo Carvalho de Melo 		hash(sk);
486a7f5e7f1SArnaldo Carvalho de Melo 		spin_unlock_bh(&head->lock);
487a7f5e7f1SArnaldo Carvalho de Melo 		return 0;
488a7f5e7f1SArnaldo Carvalho de Melo 	} else {
489a7f5e7f1SArnaldo Carvalho de Melo 		spin_unlock(&head->lock);
490a7f5e7f1SArnaldo Carvalho de Melo 		/* No definite answer... Walk to established hash table */
4915ee31fc1SPavel Emelyanov 		ret = check_established(death_row, sk, snum, NULL);
492a7f5e7f1SArnaldo Carvalho de Melo out:
493a7f5e7f1SArnaldo Carvalho de Melo 		local_bh_enable();
494a7f5e7f1SArnaldo Carvalho de Melo 		return ret;
495a7f5e7f1SArnaldo Carvalho de Melo 	}
496a7f5e7f1SArnaldo Carvalho de Melo }
4975ee31fc1SPavel Emelyanov EXPORT_SYMBOL_GPL(__inet_hash_connect);
4985ee31fc1SPavel Emelyanov 
4995ee31fc1SPavel Emelyanov /*
5005ee31fc1SPavel Emelyanov  * Bind a port for a connect operation and hash it.
5015ee31fc1SPavel Emelyanov  */
5025ee31fc1SPavel Emelyanov int inet_hash_connect(struct inet_timewait_death_row *death_row,
5035ee31fc1SPavel Emelyanov 		      struct sock *sk)
5045ee31fc1SPavel Emelyanov {
5055ee31fc1SPavel Emelyanov 	return __inet_hash_connect(death_row, sk,
5065ee31fc1SPavel Emelyanov 			__inet_check_established, __inet_hash_nolisten);
5075ee31fc1SPavel Emelyanov }
508a7f5e7f1SArnaldo Carvalho de Melo 
509a7f5e7f1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_hash_connect);
510