blob: 11a107a5af4f64d7a0e7159079941dc80359b2d4 [file] [log] [blame]
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Generic TIME_WAIT sockets functions
7 *
8 * From code orinally in TCP
9 */
10
Ilpo Järvinen172589c2007-08-28 15:50:33 -070011#include <linux/kernel.h>
Vegard Nossum9e337b02008-10-18 17:37:51 +020012#include <linux/kmemcheck.h>
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070013#include <net/inet_hashtables.h>
14#include <net/inet_timewait_sock.h>
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -070015#include <net/ip.h>
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070016
Eric Dumazet13475a32009-12-02 22:31:19 +000017
18/*
19 * unhash a timewait socket from established hash
20 * lock must be hold by caller
21 */
22int inet_twsk_unhash(struct inet_timewait_sock *tw)
23{
24 if (hlist_nulls_unhashed(&tw->tw_node))
25 return 0;
26
27 hlist_nulls_del_rcu(&tw->tw_node);
28 sk_nulls_node_init(&tw->tw_node);
29 return 1;
30}
31
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070032/* Must be called with locally disabled BHs. */
Adrian Bunkacd159b2007-07-14 19:00:59 -070033static void __inet_twsk_kill(struct inet_timewait_sock *tw,
34 struct inet_hashinfo *hashinfo)
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070035{
36 struct inet_bind_hashbucket *bhead;
37 struct inet_bind_bucket *tb;
Eric Dumazet13475a32009-12-02 22:31:19 +000038 int refcnt;
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070039 /* Unlink from established hashes. */
Eric Dumazet9db66bd2008-11-20 20:39:09 -080040 spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070041
Eric Dumazet9db66bd2008-11-20 20:39:09 -080042 spin_lock(lock);
Eric Dumazet13475a32009-12-02 22:31:19 +000043 refcnt = inet_twsk_unhash(tw);
Eric Dumazet9db66bd2008-11-20 20:39:09 -080044 spin_unlock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070045
46 /* Disassociate with bind bucket. */
Pavel Emelyanov7f635ab2008-06-16 17:12:49 -070047 bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num,
48 hashinfo->bhash_size)];
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070049 spin_lock(&bhead->lock);
50 tb = tw->tw_tb;
Eric Dumazet13475a32009-12-02 22:31:19 +000051 if (tb) {
52 __hlist_del(&tw->tw_bind_node);
53 tw->tw_tb = NULL;
54 inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
55 refcnt++;
56 }
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070057 spin_unlock(&bhead->lock);
58#ifdef SOCK_REFCNT_DEBUG
59 if (atomic_read(&tw->tw_refcnt) != 1) {
60 printk(KERN_DEBUG "%s timewait_sock %p refcnt=%d\n",
61 tw->tw_prot->name, tw, atomic_read(&tw->tw_refcnt));
62 }
63#endif
Eric Dumazet13475a32009-12-02 22:31:19 +000064 while (refcnt) {
65 inet_twsk_put(tw);
66 refcnt--;
67 }
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070068}
69
Arnaldo Carvalho de Melo4dbc8ef2009-05-06 16:50:52 -070070static noinline void inet_twsk_free(struct inet_timewait_sock *tw)
71{
72 struct module *owner = tw->tw_prot->owner;
73 twsk_destructor((struct sock *)tw);
74#ifdef SOCK_REFCNT_DEBUG
75 pr_debug("%s timewait_sock %p released\n", tw->tw_prot->name, tw);
76#endif
77 release_net(twsk_net(tw));
78 kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
79 module_put(owner);
80}
81
Pavel Emelyanov7054fb92007-12-20 15:32:54 -080082void inet_twsk_put(struct inet_timewait_sock *tw)
83{
Arnaldo Carvalho de Melo4dbc8ef2009-05-06 16:50:52 -070084 if (atomic_dec_and_test(&tw->tw_refcnt))
85 inet_twsk_free(tw);
Pavel Emelyanov7054fb92007-12-20 15:32:54 -080086}
87EXPORT_SYMBOL_GPL(inet_twsk_put);
88
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070089/*
90 * Enter the time wait state. This is called with locally disabled BH.
91 * Essentially we whip up a timewait bucket, copy the relevant info into it
92 * from the SK, and mess with hash chains and list linkage.
93 */
94void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
95 struct inet_hashinfo *hashinfo)
96{
97 const struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070098 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet81c3d542005-10-03 14:13:38 -070099 struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
Eric Dumazet9db66bd2008-11-20 20:39:09 -0800100 spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700101 struct inet_bind_hashbucket *bhead;
102 /* Step 1: Put TW into bind hash. Original socket stays there too.
103 Note, that any socket with inet->num != 0 MUST be bound in
104 binding cache, even if it is closed.
105 */
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000106 bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->inet_num,
Pavel Emelyanov7f635ab2008-06-16 17:12:49 -0700107 hashinfo->bhash_size)];
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700108 spin_lock(&bhead->lock);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700109 tw->tw_tb = icsk->icsk_bind_hash;
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700110 WARN_ON(!icsk->icsk_bind_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700111 inet_twsk_add_bind_node(tw, &tw->tw_tb->owners);
Eric Dumazet13475a32009-12-02 22:31:19 +0000112 atomic_inc(&tw->tw_refcnt);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700113 spin_unlock(&bhead->lock);
114
Eric Dumazet9db66bd2008-11-20 20:39:09 -0800115 spin_lock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700116
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800117 /*
118 * Step 2: Hash TW into TIMEWAIT chain.
119 * Should be done before removing sk from established chain
120 * because readers are lockless and search established first.
121 */
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700122 atomic_inc(&tw->tw_refcnt);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800123 inet_twsk_add_node_rcu(tw, &ehead->twchain);
124
125 /* Step 3: Remove SK from established hash. */
126 if (__sk_nulls_del_node_init_rcu(sk))
127 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700128
Eric Dumazet9db66bd2008-11-20 20:39:09 -0800129 spin_unlock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700130}
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700131
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700132EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
133
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700134struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state)
135{
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800136 struct inet_timewait_sock *tw =
137 kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800138 GFP_ATOMIC);
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700139 if (tw != NULL) {
140 const struct inet_sock *inet = inet_sk(sk);
141
Vegard Nossum9e337b02008-10-18 17:37:51 +0200142 kmemcheck_annotate_bitfield(tw, flags);
143
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700144 /* Give us an identity. */
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000145 tw->tw_daddr = inet->inet_daddr;
146 tw->tw_rcv_saddr = inet->inet_rcv_saddr;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700147 tw->tw_bound_dev_if = sk->sk_bound_dev_if;
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000148 tw->tw_num = inet->inet_num;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700149 tw->tw_state = TCP_TIME_WAIT;
150 tw->tw_substate = state;
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000151 tw->tw_sport = inet->inet_sport;
152 tw->tw_dport = inet->inet_dport;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700153 tw->tw_family = sk->sk_family;
154 tw->tw_reuse = sk->sk_reuse;
Eric Dumazet81c3d542005-10-03 14:13:38 -0700155 tw->tw_hash = sk->sk_hash;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700156 tw->tw_ipv6only = 0;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -0700157 tw->tw_transparent = inet->transparent;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700158 tw->tw_prot = sk->sk_prot_creator;
Denis V. Lunevcd5342d2008-04-16 02:00:28 -0700159 twsk_net_set(tw, hold_net(sock_net(sk)));
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700160 atomic_set(&tw->tw_refcnt, 1);
161 inet_twsk_dead_node_init(tw);
Arnaldo Carvalho de Meloeeb2b852005-10-10 21:25:23 -0700162 __module_get(tw->tw_prot->owner);
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700163 }
164
165 return tw;
166}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700167
168EXPORT_SYMBOL_GPL(inet_twsk_alloc);
169
170/* Returns non-zero if quota exceeded. */
171static int inet_twdr_do_twkill_work(struct inet_timewait_death_row *twdr,
172 const int slot)
173{
174 struct inet_timewait_sock *tw;
175 struct hlist_node *node;
176 unsigned int killed;
177 int ret;
178
179 /* NOTE: compare this to previous version where lock
180 * was released after detaching chain. It was racy,
181 * because tw buckets are scheduled in not serialized context
182 * in 2.3 (with netfilter), and with softnet it is common, because
183 * soft irqs are not sequenced.
184 */
185 killed = 0;
186 ret = 0;
187rescan:
188 inet_twsk_for_each_inmate(tw, node, &twdr->cells[slot]) {
189 __inet_twsk_del_dead_node(tw);
190 spin_unlock(&twdr->death_lock);
191 __inet_twsk_kill(tw, twdr->hashinfo);
Pavel Emelyanovf2bf4152008-07-16 20:32:25 -0700192#ifdef CONFIG_NET_NS
193 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITED);
194#endif
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700195 inet_twsk_put(tw);
196 killed++;
197 spin_lock(&twdr->death_lock);
198 if (killed > INET_TWDR_TWKILL_QUOTA) {
199 ret = 1;
200 break;
201 }
202
203 /* While we dropped twdr->death_lock, another cpu may have
204 * killed off the next TW bucket in the list, therefore
205 * do a fresh re-read of the hlist head node with the
206 * lock reacquired. We still use the hlist traversal
207 * macro in order to get the prefetches.
208 */
209 goto rescan;
210 }
211
212 twdr->tw_count -= killed;
Pavel Emelyanovf2bf4152008-07-16 20:32:25 -0700213#ifndef CONFIG_NET_NS
214 NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITED, killed);
215#endif
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700216 return ret;
217}
218
219void inet_twdr_hangman(unsigned long data)
220{
221 struct inet_timewait_death_row *twdr;
222 int unsigned need_timer;
223
224 twdr = (struct inet_timewait_death_row *)data;
225 spin_lock(&twdr->death_lock);
226
227 if (twdr->tw_count == 0)
228 goto out;
229
230 need_timer = 0;
231 if (inet_twdr_do_twkill_work(twdr, twdr->slot)) {
232 twdr->thread_slots |= (1 << twdr->slot);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700233 schedule_work(&twdr->twkill_work);
234 need_timer = 1;
235 } else {
236 /* We purged the entire slot, anything left? */
237 if (twdr->tw_count)
238 need_timer = 1;
Octavian Purdila80a10962009-08-29 00:00:35 -0700239 twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1));
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700240 }
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700241 if (need_timer)
242 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
243out:
244 spin_unlock(&twdr->death_lock);
245}
246
247EXPORT_SYMBOL_GPL(inet_twdr_hangman);
248
David Howells65f27f32006-11-22 14:55:48 +0000249void inet_twdr_twkill_work(struct work_struct *work)
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700250{
David Howells65f27f32006-11-22 14:55:48 +0000251 struct inet_timewait_death_row *twdr =
252 container_of(work, struct inet_timewait_death_row, twkill_work);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700253 int i;
254
Pavel Emelyanov95c93822007-12-11 02:12:36 -0800255 BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) >
256 (sizeof(twdr->thread_slots) * 8));
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700257
258 while (twdr->thread_slots) {
259 spin_lock_bh(&twdr->death_lock);
260 for (i = 0; i < INET_TWDR_TWKILL_SLOTS; i++) {
261 if (!(twdr->thread_slots & (1 << i)))
262 continue;
263
264 while (inet_twdr_do_twkill_work(twdr, i) != 0) {
265 if (need_resched()) {
266 spin_unlock_bh(&twdr->death_lock);
267 schedule();
268 spin_lock_bh(&twdr->death_lock);
269 }
270 }
271
272 twdr->thread_slots &= ~(1 << i);
273 }
274 spin_unlock_bh(&twdr->death_lock);
275 }
276}
277
278EXPORT_SYMBOL_GPL(inet_twdr_twkill_work);
279
280/* These are always called from BH context. See callers in
281 * tcp_input.c to verify this.
282 */
283
284/* This is for handling early-kills of TIME_WAIT sockets. */
285void inet_twsk_deschedule(struct inet_timewait_sock *tw,
286 struct inet_timewait_death_row *twdr)
287{
288 spin_lock(&twdr->death_lock);
289 if (inet_twsk_del_dead_node(tw)) {
290 inet_twsk_put(tw);
291 if (--twdr->tw_count == 0)
292 del_timer(&twdr->tw_timer);
293 }
294 spin_unlock(&twdr->death_lock);
295 __inet_twsk_kill(tw, twdr->hashinfo);
296}
297
298EXPORT_SYMBOL(inet_twsk_deschedule);
299
300void inet_twsk_schedule(struct inet_timewait_sock *tw,
301 struct inet_timewait_death_row *twdr,
302 const int timeo, const int timewait_len)
303{
304 struct hlist_head *list;
305 int slot;
306
307 /* timeout := RTO * 3.5
308 *
309 * 3.5 = 1+2+0.5 to wait for two retransmits.
310 *
311 * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
312 * our ACK acking that FIN can be lost. If N subsequent retransmitted
313 * FINs (or previous seqments) are lost (probability of such event
314 * is p^(N+1), where p is probability to lose single packet and
315 * time to detect the loss is about RTO*(2^N - 1) with exponential
316 * backoff). Normal timewait length is calculated so, that we
317 * waited at least for one retransmitted FIN (maximal RTO is 120sec).
318 * [ BTW Linux. following BSD, violates this requirement waiting
319 * only for 60sec, we should wait at least for 240 secs.
320 * Well, 240 consumes too much of resources 8)
321 * ]
322 * This interval is not reduced to catch old duplicate and
323 * responces to our wandering segments living for two MSLs.
324 * However, if we use PAWS to detect
325 * old duplicates, we can reduce the interval to bounds required
326 * by RTO, rather than MSL. So, if peer understands PAWS, we
327 * kill tw bucket after 3.5*RTO (it is important that this number
328 * is greater than TS tick!) and detect old duplicates with help
329 * of PAWS.
330 */
331 slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK;
332
333 spin_lock(&twdr->death_lock);
334
335 /* Unlink it, if it was scheduled */
336 if (inet_twsk_del_dead_node(tw))
337 twdr->tw_count--;
338 else
339 atomic_inc(&tw->tw_refcnt);
340
341 if (slot >= INET_TWDR_RECYCLE_SLOTS) {
342 /* Schedule to slow timer */
343 if (timeo >= timewait_len) {
344 slot = INET_TWDR_TWKILL_SLOTS - 1;
345 } else {
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700346 slot = DIV_ROUND_UP(timeo, twdr->period);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700347 if (slot >= INET_TWDR_TWKILL_SLOTS)
348 slot = INET_TWDR_TWKILL_SLOTS - 1;
349 }
350 tw->tw_ttd = jiffies + timeo;
351 slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
352 list = &twdr->cells[slot];
353 } else {
354 tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK);
355
356 if (twdr->twcal_hand < 0) {
357 twdr->twcal_hand = 0;
358 twdr->twcal_jiffie = jiffies;
359 twdr->twcal_timer.expires = twdr->twcal_jiffie +
360 (slot << INET_TWDR_RECYCLE_TICK);
361 add_timer(&twdr->twcal_timer);
362 } else {
363 if (time_after(twdr->twcal_timer.expires,
364 jiffies + (slot << INET_TWDR_RECYCLE_TICK)))
365 mod_timer(&twdr->twcal_timer,
366 jiffies + (slot << INET_TWDR_RECYCLE_TICK));
367 slot = (twdr->twcal_hand + slot) & (INET_TWDR_RECYCLE_SLOTS - 1);
368 }
369 list = &twdr->twcal_row[slot];
370 }
371
372 hlist_add_head(&tw->tw_death_node, list);
373
374 if (twdr->tw_count++ == 0)
375 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
376 spin_unlock(&twdr->death_lock);
377}
378
379EXPORT_SYMBOL_GPL(inet_twsk_schedule);
380
381void inet_twdr_twcal_tick(unsigned long data)
382{
383 struct inet_timewait_death_row *twdr;
384 int n, slot;
385 unsigned long j;
386 unsigned long now = jiffies;
387 int killed = 0;
388 int adv = 0;
389
390 twdr = (struct inet_timewait_death_row *)data;
391
392 spin_lock(&twdr->death_lock);
393 if (twdr->twcal_hand < 0)
394 goto out;
395
396 slot = twdr->twcal_hand;
397 j = twdr->twcal_jiffie;
398
399 for (n = 0; n < INET_TWDR_RECYCLE_SLOTS; n++) {
400 if (time_before_eq(j, now)) {
401 struct hlist_node *node, *safe;
402 struct inet_timewait_sock *tw;
403
404 inet_twsk_for_each_inmate_safe(tw, node, safe,
405 &twdr->twcal_row[slot]) {
406 __inet_twsk_del_dead_node(tw);
407 __inet_twsk_kill(tw, twdr->hashinfo);
Pavel Emelyanovf2bf4152008-07-16 20:32:25 -0700408#ifdef CONFIG_NET_NS
409 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED);
410#endif
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700411 inet_twsk_put(tw);
412 killed++;
413 }
414 } else {
415 if (!adv) {
416 adv = 1;
417 twdr->twcal_jiffie = j;
418 twdr->twcal_hand = slot;
419 }
420
421 if (!hlist_empty(&twdr->twcal_row[slot])) {
422 mod_timer(&twdr->twcal_timer, j);
423 goto out;
424 }
425 }
426 j += 1 << INET_TWDR_RECYCLE_TICK;
427 slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1);
428 }
429 twdr->twcal_hand = -1;
430
431out:
432 if ((twdr->tw_count -= killed) == 0)
433 del_timer(&twdr->tw_timer);
Pavel Emelyanovf2bf4152008-07-16 20:32:25 -0700434#ifndef CONFIG_NET_NS
435 NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITKILLED, killed);
436#endif
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700437 spin_unlock(&twdr->death_lock);
438}
439
440EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);
Daniel Lezcanod3154922008-09-08 13:17:27 -0700441
Eric W. Biedermanb099ce22009-12-03 02:29:09 +0000442void inet_twsk_purge(struct inet_hashinfo *hashinfo,
Daniel Lezcanod3154922008-09-08 13:17:27 -0700443 struct inet_timewait_death_row *twdr, int family)
444{
445 struct inet_timewait_sock *tw;
446 struct sock *sk;
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800447 struct hlist_nulls_node *node;
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000448 unsigned int slot;
Daniel Lezcanod3154922008-09-08 13:17:27 -0700449
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000450 for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
451 struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
452restart_rcu:
453 rcu_read_lock();
Daniel Lezcanod3154922008-09-08 13:17:27 -0700454restart:
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000455 sk_nulls_for_each_rcu(sk, node, &head->twchain) {
Daniel Lezcanod3154922008-09-08 13:17:27 -0700456 tw = inet_twsk(sk);
Eric W. Biedermanb099ce22009-12-03 02:29:09 +0000457 if ((tw->tw_family != family) ||
458 atomic_read(&twsk_net(tw)->count))
Daniel Lezcanod3154922008-09-08 13:17:27 -0700459 continue;
460
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000461 if (unlikely(!atomic_inc_not_zero(&tw->tw_refcnt)))
462 continue;
463
Eric W. Biedermanb099ce22009-12-03 02:29:09 +0000464 if (unlikely((tw->tw_family != family) ||
465 atomic_read(&twsk_net(tw)->count))) {
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000466 inet_twsk_put(tw);
467 goto restart;
468 }
469
470 rcu_read_unlock();
Daniel Lezcanod3154922008-09-08 13:17:27 -0700471 inet_twsk_deschedule(tw, twdr);
472 inet_twsk_put(tw);
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000473 goto restart_rcu;
Daniel Lezcanod3154922008-09-08 13:17:27 -0700474 }
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000475 /* If the nulls value we got at the end of this lookup is
476 * not the expected one, we must restart lookup.
477 * We probably met an item that was moved to another chain.
478 */
479 if (get_nulls_value(node) != slot)
480 goto restart;
481 rcu_read_unlock();
Daniel Lezcanod3154922008-09-08 13:17:27 -0700482 }
Daniel Lezcanod3154922008-09-08 13:17:27 -0700483}
484EXPORT_SYMBOL_GPL(inet_twsk_purge);