xref: /linux/net/ipv4/inet_timewait_sock.c (revision 770041d337a85923810dd3aeebea38037a28d534)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e48c414eSArnaldo Carvalho de Melo /*
3e48c414eSArnaldo Carvalho de Melo  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4e48c414eSArnaldo Carvalho de Melo  *		operating system.  INET is implemented using the  BSD Socket
5e48c414eSArnaldo Carvalho de Melo  *		interface as the means of communication with the user level.
6e48c414eSArnaldo Carvalho de Melo  *
7e48c414eSArnaldo Carvalho de Melo  *		Generic TIME_WAIT sockets functions
8e48c414eSArnaldo Carvalho de Melo  *
9e48c414eSArnaldo Carvalho de Melo  *		From code orinally in TCP
10e48c414eSArnaldo Carvalho de Melo  */
11e48c414eSArnaldo Carvalho de Melo 
12172589ccSIlpo Järvinen #include <linux/kernel.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
143a9a231dSPaul Gortmaker #include <linux/module.h>
15e48c414eSArnaldo Carvalho de Melo #include <net/inet_hashtables.h>
16e48c414eSArnaldo Carvalho de Melo #include <net/inet_timewait_sock.h>
17696ab2d3SArnaldo Carvalho de Melo #include <net/ip.h>
18e48c414eSArnaldo Carvalho de Melo 
1913475a30SEric Dumazet 
202a8875e7SEric Dumazet /**
212a8875e7SEric Dumazet  *	inet_twsk_bind_unhash - unhash a timewait socket from bind hash
222a8875e7SEric Dumazet  *	@tw: timewait socket
232a8875e7SEric Dumazet  *	@hashinfo: hashinfo pointer
242a8875e7SEric Dumazet  *
252a8875e7SEric Dumazet  *	unhash a timewait socket from bind hash, if hashed.
262a8875e7SEric Dumazet  *	bind hash lock must be held by caller.
272a8875e7SEric Dumazet  *	Returns 1 if caller should call inet_twsk_put() after lock release.
283cdaedaeSEric Dumazet  */
29fc01538fSEric Dumazet void inet_twsk_bind_unhash(struct inet_timewait_sock *tw,
303cdaedaeSEric Dumazet 			  struct inet_hashinfo *hashinfo)
313cdaedaeSEric Dumazet {
32936a192fSKuniyuki Iwashima 	struct inet_bind2_bucket *tb2 = tw->tw_tb2;
333cdaedaeSEric Dumazet 	struct inet_bind_bucket *tb = tw->tw_tb;
343cdaedaeSEric Dumazet 
353cdaedaeSEric Dumazet 	if (!tb)
36fc01538fSEric Dumazet 		return;
373cdaedaeSEric Dumazet 
38*770041d3SKuniyuki Iwashima 	__sk_del_bind_node((struct sock *)tw);
393cdaedaeSEric Dumazet 	tw->tw_tb = NULL;
40936a192fSKuniyuki Iwashima 	tw->tw_tb2 = NULL;
41936a192fSKuniyuki Iwashima 	inet_bind2_bucket_destroy(hashinfo->bind2_bucket_cachep, tb2);
428002d44fSKuniyuki Iwashima 	inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
43936a192fSKuniyuki Iwashima 
44fc01538fSEric Dumazet 	__sock_put((struct sock *)tw);
453cdaedaeSEric Dumazet }
463cdaedaeSEric Dumazet 
47e48c414eSArnaldo Carvalho de Melo /* Must be called with locally disabled BHs. */
48789f558cSEric Dumazet static void inet_twsk_kill(struct inet_timewait_sock *tw)
49e48c414eSArnaldo Carvalho de Melo {
50789f558cSEric Dumazet 	struct inet_hashinfo *hashinfo = tw->tw_dr->hashinfo;
519db66bdcSEric Dumazet 	spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
52936a192fSKuniyuki Iwashima 	struct inet_bind_hashbucket *bhead, *bhead2;
53e48c414eSArnaldo Carvalho de Melo 
549db66bdcSEric Dumazet 	spin_lock(lock);
55fc01538fSEric Dumazet 	sk_nulls_del_node_init_rcu((struct sock *)tw);
569db66bdcSEric Dumazet 	spin_unlock(lock);
57e48c414eSArnaldo Carvalho de Melo 
58e48c414eSArnaldo Carvalho de Melo 	/* Disassociate with bind bucket. */
5904c494e6SEric Dumazet 	bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num,
6004c494e6SEric Dumazet 			hashinfo->bhash_size)];
61936a192fSKuniyuki Iwashima 	bhead2 = inet_bhashfn_portaddr(hashinfo, (struct sock *)tw,
62936a192fSKuniyuki Iwashima 				       twsk_net(tw), tw->tw_num);
633cdaedaeSEric Dumazet 
64e48c414eSArnaldo Carvalho de Melo 	spin_lock(&bhead->lock);
65936a192fSKuniyuki Iwashima 	spin_lock(&bhead2->lock);
66fc01538fSEric Dumazet 	inet_twsk_bind_unhash(tw, hashinfo);
67936a192fSKuniyuki Iwashima 	spin_unlock(&bhead2->lock);
68e48c414eSArnaldo Carvalho de Melo 	spin_unlock(&bhead->lock);
693cdaedaeSEric Dumazet 
70e9bd0ccaSKuniyuki Iwashima 	refcount_dec(&tw->tw_dr->tw_refcount);
71789f558cSEric Dumazet 	inet_twsk_put(tw);
72e48c414eSArnaldo Carvalho de Melo }
73e48c414eSArnaldo Carvalho de Melo 
7405dbc7b5SEric Dumazet void inet_twsk_free(struct inet_timewait_sock *tw)
757054fb93SPavel Emelyanov {
767054fb93SPavel Emelyanov 	struct module *owner = tw->tw_prot->owner;
777054fb93SPavel Emelyanov 	twsk_destructor((struct sock *)tw);
787054fb93SPavel Emelyanov 	kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
797054fb93SPavel Emelyanov 	module_put(owner);
807054fb93SPavel Emelyanov }
814dbc8ef7SArnaldo Carvalho de Melo 
824dbc8ef7SArnaldo Carvalho de Melo void inet_twsk_put(struct inet_timewait_sock *tw)
834dbc8ef7SArnaldo Carvalho de Melo {
8441c6d650SReshetova, Elena 	if (refcount_dec_and_test(&tw->tw_refcnt))
854dbc8ef7SArnaldo Carvalho de Melo 		inet_twsk_free(tw);
867054fb93SPavel Emelyanov }
877054fb93SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_twsk_put);
887054fb93SPavel Emelyanov 
8981b3ade5SKuniyuki Iwashima static void inet_twsk_add_node_rcu(struct inet_timewait_sock *tw,
9005dbc7b5SEric Dumazet 				   struct hlist_nulls_head *list)
9105dbc7b5SEric Dumazet {
9281b3ade5SKuniyuki Iwashima 	hlist_nulls_add_head_rcu(&tw->tw_node, list);
9305dbc7b5SEric Dumazet }
9405dbc7b5SEric Dumazet 
95e48c414eSArnaldo Carvalho de Melo /*
96e599ea14SEric Dumazet  * Enter the time wait state. This is called with locally disabled BH.
97e48c414eSArnaldo Carvalho de Melo  * Essentially we whip up a timewait bucket, copy the relevant info into it
98e48c414eSArnaldo Carvalho de Melo  * from the SK, and mess with hash chains and list linkage.
99e48c414eSArnaldo Carvalho de Melo  */
100ec94c269SEric Dumazet void inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
101e48c414eSArnaldo Carvalho de Melo 			   struct inet_hashinfo *hashinfo)
102e48c414eSArnaldo Carvalho de Melo {
103e48c414eSArnaldo Carvalho de Melo 	const struct inet_sock *inet = inet_sk(sk);
104463c84b9SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
10581c3d547SEric Dumazet 	struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
1069db66bdcSEric Dumazet 	spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
107936a192fSKuniyuki Iwashima 	struct inet_bind_hashbucket *bhead, *bhead2;
108936a192fSKuniyuki Iwashima 
109e48c414eSArnaldo Carvalho de Melo 	/* Step 1: Put TW into bind hash. Original socket stays there too.
110e48c414eSArnaldo Carvalho de Melo 	   Note, that any socket with inet->num != 0 MUST be bound in
111e48c414eSArnaldo Carvalho de Melo 	   binding cache, even if it is closed.
112e48c414eSArnaldo Carvalho de Melo 	 */
11304c494e6SEric Dumazet 	bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->inet_num,
11404c494e6SEric Dumazet 			hashinfo->bhash_size)];
115936a192fSKuniyuki Iwashima 	bhead2 = inet_bhashfn_portaddr(hashinfo, sk, twsk_net(tw), inet->inet_num);
116936a192fSKuniyuki Iwashima 
117e599ea14SEric Dumazet 	spin_lock(&bhead->lock);
118936a192fSKuniyuki Iwashima 	spin_lock(&bhead2->lock);
119936a192fSKuniyuki Iwashima 
120463c84b9SArnaldo Carvalho de Melo 	tw->tw_tb = icsk->icsk_bind_hash;
121547b792cSIlpo Järvinen 	WARN_ON(!icsk->icsk_bind_hash);
122936a192fSKuniyuki Iwashima 
123936a192fSKuniyuki Iwashima 	tw->tw_tb2 = icsk->icsk_bind2_hash;
124936a192fSKuniyuki Iwashima 	WARN_ON(!icsk->icsk_bind2_hash);
125*770041d3SKuniyuki Iwashima 	sk_add_bind_node((struct sock *)tw, &tw->tw_tb2->owners);
126936a192fSKuniyuki Iwashima 
127936a192fSKuniyuki Iwashima 	spin_unlock(&bhead2->lock);
128e48c414eSArnaldo Carvalho de Melo 	spin_unlock(&bhead->lock);
129e48c414eSArnaldo Carvalho de Melo 
1309db66bdcSEric Dumazet 	spin_lock(lock);
131e48c414eSArnaldo Carvalho de Melo 
13281b3ade5SKuniyuki Iwashima 	inet_twsk_add_node_rcu(tw, &ehead->chain);
1333ab5aee7SEric Dumazet 
13405dbc7b5SEric Dumazet 	/* Step 3: Remove SK from hash chain */
1353ab5aee7SEric Dumazet 	if (__sk_nulls_del_node_init_rcu(sk))
1363ab5aee7SEric Dumazet 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
137e48c414eSArnaldo Carvalho de Melo 
138e599ea14SEric Dumazet 	spin_unlock(lock);
139ec94c269SEric Dumazet 
140ec94c269SEric Dumazet 	/* tw_refcnt is set to 3 because we have :
141ec94c269SEric Dumazet 	 * - one reference for bhash chain.
142ec94c269SEric Dumazet 	 * - one reference for ehash chain.
143ec94c269SEric Dumazet 	 * - one reference for timer.
144ec94c269SEric Dumazet 	 * We can use atomic_set() because prior spin_lock()/spin_unlock()
145ec94c269SEric Dumazet 	 * committed into memory all tw fields.
146ec94c269SEric Dumazet 	 * Also note that after this point, we lost our implicit reference
147ec94c269SEric Dumazet 	 * so we are not allowed to use tw anymore.
148ec94c269SEric Dumazet 	 */
149ec94c269SEric Dumazet 	refcount_set(&tw->tw_refcnt, 3);
150e48c414eSArnaldo Carvalho de Melo }
151ec94c269SEric Dumazet EXPORT_SYMBOL_GPL(inet_twsk_hashdance);
152696ab2d3SArnaldo Carvalho de Melo 
1531ab791dcSKees Cook static void tw_timer_handler(struct timer_list *t)
154c676270bSArnaldo Carvalho de Melo {
1551ab791dcSKees Cook 	struct inet_timewait_sock *tw = from_timer(tw, t, tw_timer);
156789f558cSEric Dumazet 
157789f558cSEric Dumazet 	inet_twsk_kill(tw);
158789f558cSEric Dumazet }
159789f558cSEric Dumazet 
160789f558cSEric Dumazet struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk,
161789f558cSEric Dumazet 					   struct inet_timewait_death_row *dr,
162789f558cSEric Dumazet 					   const int state)
163789f558cSEric Dumazet {
164789f558cSEric Dumazet 	struct inet_timewait_sock *tw;
165789f558cSEric Dumazet 
1666f605b57SKuniyuki Iwashima 	if (refcount_read(&dr->tw_refcount) - 1 >=
1676f605b57SKuniyuki Iwashima 	    READ_ONCE(dr->sysctl_max_tw_buckets))
168789f558cSEric Dumazet 		return NULL;
169789f558cSEric Dumazet 
170789f558cSEric Dumazet 	tw = kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
17154e6ecb2SChristoph Lameter 			      GFP_ATOMIC);
17200db4124SIan Morris 	if (tw) {
173c676270bSArnaldo Carvalho de Melo 		const struct inet_sock *inet = inet_sk(sk);
174c676270bSArnaldo Carvalho de Melo 
175789f558cSEric Dumazet 		tw->tw_dr	    = dr;
176c676270bSArnaldo Carvalho de Melo 		/* Give us an identity. */
177c720c7e8SEric Dumazet 		tw->tw_daddr	    = inet->inet_daddr;
178c720c7e8SEric Dumazet 		tw->tw_rcv_saddr    = inet->inet_rcv_saddr;
179c676270bSArnaldo Carvalho de Melo 		tw->tw_bound_dev_if = sk->sk_bound_dev_if;
18066b13d99SEric Dumazet 		tw->tw_tos	    = inet->tos;
181c720c7e8SEric Dumazet 		tw->tw_num	    = inet->inet_num;
182c676270bSArnaldo Carvalho de Melo 		tw->tw_state	    = TCP_TIME_WAIT;
183c676270bSArnaldo Carvalho de Melo 		tw->tw_substate	    = state;
184c720c7e8SEric Dumazet 		tw->tw_sport	    = inet->inet_sport;
185c720c7e8SEric Dumazet 		tw->tw_dport	    = inet->inet_dport;
186c676270bSArnaldo Carvalho de Melo 		tw->tw_family	    = sk->sk_family;
187c676270bSArnaldo Carvalho de Melo 		tw->tw_reuse	    = sk->sk_reuse;
1883099a529SEric Dumazet 		tw->tw_reuseport    = sk->sk_reuseport;
18981c3d547SEric Dumazet 		tw->tw_hash	    = sk->sk_hash;
190c676270bSArnaldo Carvalho de Melo 		tw->tw_ipv6only	    = 0;
1914bd0623fSEric Dumazet 		tw->tw_transparent  = inet_test_bit(TRANSPARENT, sk);
192c676270bSArnaldo Carvalho de Melo 		tw->tw_prot	    = sk->sk_prot_creator;
19333cf7c90SEric Dumazet 		atomic64_set(&tw->tw_cookie, atomic64_read(&sk->sk_cookie));
194efd7ef1cSEric W. Biederman 		twsk_net_set(tw, sock_net(sk));
1951ab791dcSKees Cook 		timer_setup(&tw->tw_timer, tw_timer_handler, TIMER_PINNED);
19647e1c323SEric Dumazet 		/*
19747e1c323SEric Dumazet 		 * Because we use RCU lookups, we should not set tw_refcnt
19847e1c323SEric Dumazet 		 * to a non null value before everything is setup for this
19947e1c323SEric Dumazet 		 * timewait socket.
20047e1c323SEric Dumazet 		 */
20141c6d650SReshetova, Elena 		refcount_set(&tw->tw_refcnt, 0);
202789f558cSEric Dumazet 
203eeb2b856SArnaldo Carvalho de Melo 		__module_get(tw->tw_prot->owner);
204c676270bSArnaldo Carvalho de Melo 	}
205c676270bSArnaldo Carvalho de Melo 
206c676270bSArnaldo Carvalho de Melo 	return tw;
207c676270bSArnaldo Carvalho de Melo }
208696ab2d3SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_twsk_alloc);
209696ab2d3SArnaldo Carvalho de Melo 
210696ab2d3SArnaldo Carvalho de Melo /* These are always called from BH context.  See callers in
211696ab2d3SArnaldo Carvalho de Melo  * tcp_input.c to verify this.
212696ab2d3SArnaldo Carvalho de Melo  */
213696ab2d3SArnaldo Carvalho de Melo 
214dbe7faa4SEric Dumazet /* This is for handling early-kills of TIME_WAIT sockets.
215dbe7faa4SEric Dumazet  * Warning : consume reference.
216dbe7faa4SEric Dumazet  * Caller should not access tw anymore.
217dbe7faa4SEric Dumazet  */
218dbe7faa4SEric Dumazet void inet_twsk_deschedule_put(struct inet_timewait_sock *tw)
219696ab2d3SArnaldo Carvalho de Melo {
220789f558cSEric Dumazet 	if (del_timer_sync(&tw->tw_timer))
221789f558cSEric Dumazet 		inet_twsk_kill(tw);
222dbe7faa4SEric Dumazet 	inet_twsk_put(tw);
223696ab2d3SArnaldo Carvalho de Melo }
224dbe7faa4SEric Dumazet EXPORT_SYMBOL(inet_twsk_deschedule_put);
225696ab2d3SArnaldo Carvalho de Melo 
226ed2e9239SEric Dumazet void __inet_twsk_schedule(struct inet_timewait_sock *tw, int timeo, bool rearm)
227696ab2d3SArnaldo Carvalho de Melo {
228696ab2d3SArnaldo Carvalho de Melo 	/* timeout := RTO * 3.5
229696ab2d3SArnaldo Carvalho de Melo 	 *
230696ab2d3SArnaldo Carvalho de Melo 	 * 3.5 = 1+2+0.5 to wait for two retransmits.
231696ab2d3SArnaldo Carvalho de Melo 	 *
232696ab2d3SArnaldo Carvalho de Melo 	 * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
233696ab2d3SArnaldo Carvalho de Melo 	 * our ACK acking that FIN can be lost. If N subsequent retransmitted
234696ab2d3SArnaldo Carvalho de Melo 	 * FINs (or previous seqments) are lost (probability of such event
235696ab2d3SArnaldo Carvalho de Melo 	 * is p^(N+1), where p is probability to lose single packet and
236696ab2d3SArnaldo Carvalho de Melo 	 * time to detect the loss is about RTO*(2^N - 1) with exponential
237696ab2d3SArnaldo Carvalho de Melo 	 * backoff). Normal timewait length is calculated so, that we
238696ab2d3SArnaldo Carvalho de Melo 	 * waited at least for one retransmitted FIN (maximal RTO is 120sec).
239696ab2d3SArnaldo Carvalho de Melo 	 * [ BTW Linux. following BSD, violates this requirement waiting
240696ab2d3SArnaldo Carvalho de Melo 	 *   only for 60sec, we should wait at least for 240 secs.
241696ab2d3SArnaldo Carvalho de Melo 	 *   Well, 240 consumes too much of resources 8)
242696ab2d3SArnaldo Carvalho de Melo 	 * ]
243696ab2d3SArnaldo Carvalho de Melo 	 * This interval is not reduced to catch old duplicate and
244696ab2d3SArnaldo Carvalho de Melo 	 * responces to our wandering segments living for two MSLs.
245696ab2d3SArnaldo Carvalho de Melo 	 * However, if we use PAWS to detect
246696ab2d3SArnaldo Carvalho de Melo 	 * old duplicates, we can reduce the interval to bounds required
247696ab2d3SArnaldo Carvalho de Melo 	 * by RTO, rather than MSL. So, if peer understands PAWS, we
248696ab2d3SArnaldo Carvalho de Melo 	 * kill tw bucket after 3.5*RTO (it is important that this number
249696ab2d3SArnaldo Carvalho de Melo 	 * is greater than TS tick!) and detect old duplicates with help
250696ab2d3SArnaldo Carvalho de Melo 	 * of PAWS.
251696ab2d3SArnaldo Carvalho de Melo 	 */
252696ab2d3SArnaldo Carvalho de Melo 
253ed2e9239SEric Dumazet 	if (!rearm) {
25427dd35e0SEric Dumazet 		bool kill = timeo <= 4*HZ;
25527dd35e0SEric Dumazet 
25627dd35e0SEric Dumazet 		__NET_INC_STATS(twsk_net(tw), kill ? LINUX_MIB_TIMEWAITKILLED :
25727dd35e0SEric Dumazet 						     LINUX_MIB_TIMEWAITED);
258f3438bc7SThomas Gleixner 		BUG_ON(mod_timer(&tw->tw_timer, jiffies + timeo));
259fbb82952SEric Dumazet 		refcount_inc(&tw->tw_dr->tw_refcount);
260ed2e9239SEric Dumazet 	} else {
261ed2e9239SEric Dumazet 		mod_timer_pending(&tw->tw_timer, jiffies + timeo);
262696ab2d3SArnaldo Carvalho de Melo 	}
263696ab2d3SArnaldo Carvalho de Melo }
264ed2e9239SEric Dumazet EXPORT_SYMBOL_GPL(__inet_twsk_schedule);
26504c494e6SEric Dumazet 
26604c494e6SEric Dumazet void inet_twsk_purge(struct inet_hashinfo *hashinfo, int family)
26704c494e6SEric Dumazet {
26804c494e6SEric Dumazet 	struct inet_timewait_sock *tw;
26904c494e6SEric Dumazet 	struct sock *sk;
27004c494e6SEric Dumazet 	struct hlist_nulls_node *node;
27104c494e6SEric Dumazet 	unsigned int slot;
27204c494e6SEric Dumazet 
27304c494e6SEric Dumazet 	for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
27404c494e6SEric Dumazet 		struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
27504c494e6SEric Dumazet restart_rcu:
27604c494e6SEric Dumazet 		cond_resched();
27704c494e6SEric Dumazet 		rcu_read_lock();
27804c494e6SEric Dumazet restart:
27904c494e6SEric Dumazet 		sk_nulls_for_each_rcu(sk, node, &head->chain) {
280740ea3c4SKuniyuki Iwashima 			if (sk->sk_state != TCP_TIME_WAIT) {
281740ea3c4SKuniyuki Iwashima 				/* A kernel listener socket might not hold refcnt for net,
282740ea3c4SKuniyuki Iwashima 				 * so reqsk_timer_handler() could be fired after net is
283740ea3c4SKuniyuki Iwashima 				 * freed.  Userspace listener and reqsk never exist here.
284740ea3c4SKuniyuki Iwashima 				 */
285740ea3c4SKuniyuki Iwashima 				if (unlikely(sk->sk_state == TCP_NEW_SYN_RECV &&
286740ea3c4SKuniyuki Iwashima 					     hashinfo->pernet)) {
287740ea3c4SKuniyuki Iwashima 					struct request_sock *req = inet_reqsk(sk);
288740ea3c4SKuniyuki Iwashima 
289740ea3c4SKuniyuki Iwashima 					inet_csk_reqsk_queue_drop_and_put(req->rsk_listener, req);
290740ea3c4SKuniyuki Iwashima 				}
291740ea3c4SKuniyuki Iwashima 
29204c494e6SEric Dumazet 				continue;
293740ea3c4SKuniyuki Iwashima 			}
294740ea3c4SKuniyuki Iwashima 
29504c494e6SEric Dumazet 			tw = inet_twsk(sk);
29604c494e6SEric Dumazet 			if ((tw->tw_family != family) ||
29704c494e6SEric Dumazet 				refcount_read(&twsk_net(tw)->ns.count))
29804c494e6SEric Dumazet 				continue;
29904c494e6SEric Dumazet 
30004c494e6SEric Dumazet 			if (unlikely(!refcount_inc_not_zero(&tw->tw_refcnt)))
30104c494e6SEric Dumazet 				continue;
30204c494e6SEric Dumazet 
30304c494e6SEric Dumazet 			if (unlikely((tw->tw_family != family) ||
30404c494e6SEric Dumazet 				     refcount_read(&twsk_net(tw)->ns.count))) {
30504c494e6SEric Dumazet 				inet_twsk_put(tw);
30604c494e6SEric Dumazet 				goto restart;
30704c494e6SEric Dumazet 			}
30804c494e6SEric Dumazet 
30904c494e6SEric Dumazet 			rcu_read_unlock();
31004c494e6SEric Dumazet 			local_bh_disable();
31104c494e6SEric Dumazet 			inet_twsk_deschedule_put(tw);
31204c494e6SEric Dumazet 			local_bh_enable();
31304c494e6SEric Dumazet 			goto restart_rcu;
31404c494e6SEric Dumazet 		}
31504c494e6SEric Dumazet 		/* If the nulls value we got at the end of this lookup is
31604c494e6SEric Dumazet 		 * not the expected one, we must restart lookup.
31704c494e6SEric Dumazet 		 * We probably met an item that was moved to another chain.
31804c494e6SEric Dumazet 		 */
31904c494e6SEric Dumazet 		if (get_nulls_value(node) != slot)
32004c494e6SEric Dumazet 			goto restart;
32104c494e6SEric Dumazet 		rcu_read_unlock();
32204c494e6SEric Dumazet 	}
32304c494e6SEric Dumazet }
32404c494e6SEric Dumazet EXPORT_SYMBOL_GPL(inet_twsk_purge);
325