blob: cc94cc2d8b2d8825d909266b501afb1c6ea03cab [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
Eric Dumazet2a8875e2009-12-08 20:19:53 -080018/**
19 * inet_twsk_unhash - unhash a timewait socket from established hash
20 * @tw: timewait socket
21 *
22 * unhash a timewait socket from established hash, if hashed.
23 * ehash lock must be held by caller.
24 * Returns 1 if caller should call inet_twsk_put() after lock release.
Eric Dumazet13475a32009-12-02 22:31:19 +000025 */
26int inet_twsk_unhash(struct inet_timewait_sock *tw)
27{
28 if (hlist_nulls_unhashed(&tw->tw_node))
29 return 0;
30
31 hlist_nulls_del_rcu(&tw->tw_node);
32 sk_nulls_node_init(&tw->tw_node);
Eric Dumazet2a8875e2009-12-08 20:19:53 -080033 /*
34 * We cannot call inet_twsk_put() ourself under lock,
35 * caller must call it for us.
36 */
Eric Dumazet13475a32009-12-02 22:31:19 +000037 return 1;
38}
39
Eric Dumazet2a8875e2009-12-08 20:19:53 -080040/**
41 * inet_twsk_bind_unhash - unhash a timewait socket from bind hash
42 * @tw: timewait socket
43 * @hashinfo: hashinfo pointer
44 *
45 * unhash a timewait socket from bind hash, if hashed.
46 * bind hash lock must be held by caller.
47 * Returns 1 if caller should call inet_twsk_put() after lock release.
Eric Dumazet3cdaeda2009-12-04 03:47:42 +000048 */
49int inet_twsk_bind_unhash(struct inet_timewait_sock *tw,
50 struct inet_hashinfo *hashinfo)
51{
52 struct inet_bind_bucket *tb = tw->tw_tb;
53
54 if (!tb)
55 return 0;
56
57 __hlist_del(&tw->tw_bind_node);
58 tw->tw_tb = NULL;
59 inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
Eric Dumazet2a8875e2009-12-08 20:19:53 -080060 /*
61 * We cannot call inet_twsk_put() ourself under lock,
62 * caller must call it for us.
63 */
Eric Dumazet3cdaeda2009-12-04 03:47:42 +000064 return 1;
65}
66
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070067/* Must be called with locally disabled BHs. */
Adrian Bunkacd159b2007-07-14 19:00:59 -070068static void __inet_twsk_kill(struct inet_timewait_sock *tw,
69 struct inet_hashinfo *hashinfo)
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070070{
71 struct inet_bind_hashbucket *bhead;
Eric Dumazet13475a32009-12-02 22:31:19 +000072 int refcnt;
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070073 /* Unlink from established hashes. */
Eric Dumazet9db66bd2008-11-20 20:39:09 -080074 spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070075
Eric Dumazet9db66bd2008-11-20 20:39:09 -080076 spin_lock(lock);
Eric Dumazet13475a32009-12-02 22:31:19 +000077 refcnt = inet_twsk_unhash(tw);
Eric Dumazet9db66bd2008-11-20 20:39:09 -080078 spin_unlock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070079
80 /* Disassociate with bind bucket. */
Pavel Emelyanov7f635ab2008-06-16 17:12:49 -070081 bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num,
82 hashinfo->bhash_size)];
Eric Dumazet3cdaeda2009-12-04 03:47:42 +000083
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070084 spin_lock(&bhead->lock);
Eric Dumazet3cdaeda2009-12-04 03:47:42 +000085 refcnt += inet_twsk_bind_unhash(tw, hashinfo);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070086 spin_unlock(&bhead->lock);
Eric Dumazet3cdaeda2009-12-04 03:47:42 +000087
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070088#ifdef SOCK_REFCNT_DEBUG
89 if (atomic_read(&tw->tw_refcnt) != 1) {
90 printk(KERN_DEBUG "%s timewait_sock %p refcnt=%d\n",
91 tw->tw_prot->name, tw, atomic_read(&tw->tw_refcnt));
92 }
93#endif
Eric Dumazet13475a32009-12-02 22:31:19 +000094 while (refcnt) {
95 inet_twsk_put(tw);
96 refcnt--;
97 }
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070098}
99
Arnaldo Carvalho de Melo4dbc8ef2009-05-06 16:50:52 -0700100static noinline void inet_twsk_free(struct inet_timewait_sock *tw)
101{
102 struct module *owner = tw->tw_prot->owner;
103 twsk_destructor((struct sock *)tw);
104#ifdef SOCK_REFCNT_DEBUG
105 pr_debug("%s timewait_sock %p released\n", tw->tw_prot->name, tw);
106#endif
107 release_net(twsk_net(tw));
108 kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
109 module_put(owner);
110}
111
Pavel Emelyanov7054fb92007-12-20 15:32:54 -0800112void inet_twsk_put(struct inet_timewait_sock *tw)
113{
Arnaldo Carvalho de Melo4dbc8ef2009-05-06 16:50:52 -0700114 if (atomic_dec_and_test(&tw->tw_refcnt))
115 inet_twsk_free(tw);
Pavel Emelyanov7054fb92007-12-20 15:32:54 -0800116}
117EXPORT_SYMBOL_GPL(inet_twsk_put);
118
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700119/*
120 * Enter the time wait state. This is called with locally disabled BH.
121 * Essentially we whip up a timewait bucket, copy the relevant info into it
122 * from the SK, and mess with hash chains and list linkage.
123 */
124void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
125 struct inet_hashinfo *hashinfo)
126{
127 const struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700128 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet81c3d542005-10-03 14:13:38 -0700129 struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
Eric Dumazet9db66bd2008-11-20 20:39:09 -0800130 spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700131 struct inet_bind_hashbucket *bhead;
132 /* Step 1: Put TW into bind hash. Original socket stays there too.
133 Note, that any socket with inet->num != 0 MUST be bound in
134 binding cache, even if it is closed.
135 */
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000136 bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->inet_num,
Pavel Emelyanov7f635ab2008-06-16 17:12:49 -0700137 hashinfo->bhash_size)];
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700138 spin_lock(&bhead->lock);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700139 tw->tw_tb = icsk->icsk_bind_hash;
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700140 WARN_ON(!icsk->icsk_bind_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700141 inet_twsk_add_bind_node(tw, &tw->tw_tb->owners);
142 spin_unlock(&bhead->lock);
143
Eric Dumazet9db66bd2008-11-20 20:39:09 -0800144 spin_lock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700145
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800146 /*
147 * Step 2: Hash TW into TIMEWAIT chain.
148 * Should be done before removing sk from established chain
149 * because readers are lockless and search established first.
150 */
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800151 inet_twsk_add_node_rcu(tw, &ehead->twchain);
152
153 /* Step 3: Remove SK from established hash. */
154 if (__sk_nulls_del_node_init_rcu(sk))
155 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700156
Eric Dumazet47e1c322009-12-03 00:49:01 +0000157 /*
158 * Notes :
Eric Dumazet2a8875e2009-12-08 20:19:53 -0800159 * - We initially set tw_refcnt to 0 in inet_twsk_alloc()
Eric Dumazet47e1c322009-12-03 00:49:01 +0000160 * - We add one reference for the bhash link
161 * - We add one reference for the ehash link
162 * - We want this refcnt update done before allowing other
163 * threads to find this tw in ehash chain.
164 */
165 atomic_add(1 + 1 + 1, &tw->tw_refcnt);
166
Eric Dumazet9db66bd2008-11-20 20:39:09 -0800167 spin_unlock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700168}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700169EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
170
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700171struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state)
172{
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800173 struct inet_timewait_sock *tw =
174 kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800175 GFP_ATOMIC);
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700176 if (tw != NULL) {
177 const struct inet_sock *inet = inet_sk(sk);
178
Vegard Nossum9e337b02008-10-18 17:37:51 +0200179 kmemcheck_annotate_bitfield(tw, flags);
180
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700181 /* Give us an identity. */
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000182 tw->tw_daddr = inet->inet_daddr;
183 tw->tw_rcv_saddr = inet->inet_rcv_saddr;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700184 tw->tw_bound_dev_if = sk->sk_bound_dev_if;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000185 tw->tw_num = inet->inet_num;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700186 tw->tw_state = TCP_TIME_WAIT;
187 tw->tw_substate = state;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000188 tw->tw_sport = inet->inet_sport;
189 tw->tw_dport = inet->inet_dport;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700190 tw->tw_family = sk->sk_family;
191 tw->tw_reuse = sk->sk_reuse;
Eric Dumazet81c3d542005-10-03 14:13:38 -0700192 tw->tw_hash = sk->sk_hash;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700193 tw->tw_ipv6only = 0;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -0700194 tw->tw_transparent = inet->transparent;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700195 tw->tw_prot = sk->sk_prot_creator;
Denis V. Lunevcd5342d2008-04-16 02:00:28 -0700196 twsk_net_set(tw, hold_net(sock_net(sk)));
Eric Dumazet47e1c322009-12-03 00:49:01 +0000197 /*
198 * Because we use RCU lookups, we should not set tw_refcnt
199 * to a non null value before everything is setup for this
200 * timewait socket.
201 */
202 atomic_set(&tw->tw_refcnt, 0);
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700203 inet_twsk_dead_node_init(tw);
Arnaldo Carvalho de Meloeeb2b852005-10-10 21:25:23 -0700204 __module_get(tw->tw_prot->owner);
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700205 }
206
207 return tw;
208}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700209EXPORT_SYMBOL_GPL(inet_twsk_alloc);
210
211/* Returns non-zero if quota exceeded. */
212static int inet_twdr_do_twkill_work(struct inet_timewait_death_row *twdr,
213 const int slot)
214{
215 struct inet_timewait_sock *tw;
216 struct hlist_node *node;
217 unsigned int killed;
218 int ret;
219
220 /* NOTE: compare this to previous version where lock
221 * was released after detaching chain. It was racy,
222 * because tw buckets are scheduled in not serialized context
223 * in 2.3 (with netfilter), and with softnet it is common, because
224 * soft irqs are not sequenced.
225 */
226 killed = 0;
227 ret = 0;
228rescan:
229 inet_twsk_for_each_inmate(tw, node, &twdr->cells[slot]) {
230 __inet_twsk_del_dead_node(tw);
231 spin_unlock(&twdr->death_lock);
232 __inet_twsk_kill(tw, twdr->hashinfo);
Pavel Emelyanovf2bf4152008-07-16 20:32:25 -0700233#ifdef CONFIG_NET_NS
234 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITED);
235#endif
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700236 inet_twsk_put(tw);
237 killed++;
238 spin_lock(&twdr->death_lock);
239 if (killed > INET_TWDR_TWKILL_QUOTA) {
240 ret = 1;
241 break;
242 }
243
244 /* While we dropped twdr->death_lock, another cpu may have
245 * killed off the next TW bucket in the list, therefore
246 * do a fresh re-read of the hlist head node with the
247 * lock reacquired. We still use the hlist traversal
248 * macro in order to get the prefetches.
249 */
250 goto rescan;
251 }
252
253 twdr->tw_count -= killed;
Pavel Emelyanovf2bf4152008-07-16 20:32:25 -0700254#ifndef CONFIG_NET_NS
255 NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITED, killed);
256#endif
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700257 return ret;
258}
259
260void inet_twdr_hangman(unsigned long data)
261{
262 struct inet_timewait_death_row *twdr;
263 int unsigned need_timer;
264
265 twdr = (struct inet_timewait_death_row *)data;
266 spin_lock(&twdr->death_lock);
267
268 if (twdr->tw_count == 0)
269 goto out;
270
271 need_timer = 0;
272 if (inet_twdr_do_twkill_work(twdr, twdr->slot)) {
273 twdr->thread_slots |= (1 << twdr->slot);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700274 schedule_work(&twdr->twkill_work);
275 need_timer = 1;
276 } else {
277 /* We purged the entire slot, anything left? */
278 if (twdr->tw_count)
279 need_timer = 1;
Octavian Purdila80a10962009-08-29 00:00:35 -0700280 twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1));
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700281 }
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700282 if (need_timer)
283 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
284out:
285 spin_unlock(&twdr->death_lock);
286}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700287EXPORT_SYMBOL_GPL(inet_twdr_hangman);
288
David Howells65f27f32006-11-22 14:55:48 +0000289void inet_twdr_twkill_work(struct work_struct *work)
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700290{
David Howells65f27f32006-11-22 14:55:48 +0000291 struct inet_timewait_death_row *twdr =
292 container_of(work, struct inet_timewait_death_row, twkill_work);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700293 int i;
294
Pavel Emelyanov95c93822007-12-11 02:12:36 -0800295 BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) >
296 (sizeof(twdr->thread_slots) * 8));
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700297
298 while (twdr->thread_slots) {
299 spin_lock_bh(&twdr->death_lock);
300 for (i = 0; i < INET_TWDR_TWKILL_SLOTS; i++) {
301 if (!(twdr->thread_slots & (1 << i)))
302 continue;
303
304 while (inet_twdr_do_twkill_work(twdr, i) != 0) {
305 if (need_resched()) {
306 spin_unlock_bh(&twdr->death_lock);
307 schedule();
308 spin_lock_bh(&twdr->death_lock);
309 }
310 }
311
312 twdr->thread_slots &= ~(1 << i);
313 }
314 spin_unlock_bh(&twdr->death_lock);
315 }
316}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700317EXPORT_SYMBOL_GPL(inet_twdr_twkill_work);
318
319/* These are always called from BH context. See callers in
320 * tcp_input.c to verify this.
321 */
322
323/* This is for handling early-kills of TIME_WAIT sockets. */
324void inet_twsk_deschedule(struct inet_timewait_sock *tw,
325 struct inet_timewait_death_row *twdr)
326{
327 spin_lock(&twdr->death_lock);
328 if (inet_twsk_del_dead_node(tw)) {
329 inet_twsk_put(tw);
330 if (--twdr->tw_count == 0)
331 del_timer(&twdr->tw_timer);
332 }
333 spin_unlock(&twdr->death_lock);
334 __inet_twsk_kill(tw, twdr->hashinfo);
335}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700336EXPORT_SYMBOL(inet_twsk_deschedule);
337
338void inet_twsk_schedule(struct inet_timewait_sock *tw,
339 struct inet_timewait_death_row *twdr,
340 const int timeo, const int timewait_len)
341{
342 struct hlist_head *list;
343 int slot;
344
345 /* timeout := RTO * 3.5
346 *
347 * 3.5 = 1+2+0.5 to wait for two retransmits.
348 *
349 * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
350 * our ACK acking that FIN can be lost. If N subsequent retransmitted
351 * FINs (or previous seqments) are lost (probability of such event
352 * is p^(N+1), where p is probability to lose single packet and
353 * time to detect the loss is about RTO*(2^N - 1) with exponential
354 * backoff). Normal timewait length is calculated so, that we
355 * waited at least for one retransmitted FIN (maximal RTO is 120sec).
356 * [ BTW Linux. following BSD, violates this requirement waiting
357 * only for 60sec, we should wait at least for 240 secs.
358 * Well, 240 consumes too much of resources 8)
359 * ]
360 * This interval is not reduced to catch old duplicate and
361 * responces to our wandering segments living for two MSLs.
362 * However, if we use PAWS to detect
363 * old duplicates, we can reduce the interval to bounds required
364 * by RTO, rather than MSL. So, if peer understands PAWS, we
365 * kill tw bucket after 3.5*RTO (it is important that this number
366 * is greater than TS tick!) and detect old duplicates with help
367 * of PAWS.
368 */
369 slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK;
370
371 spin_lock(&twdr->death_lock);
372
373 /* Unlink it, if it was scheduled */
374 if (inet_twsk_del_dead_node(tw))
375 twdr->tw_count--;
376 else
377 atomic_inc(&tw->tw_refcnt);
378
379 if (slot >= INET_TWDR_RECYCLE_SLOTS) {
380 /* Schedule to slow timer */
381 if (timeo >= timewait_len) {
382 slot = INET_TWDR_TWKILL_SLOTS - 1;
383 } else {
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700384 slot = DIV_ROUND_UP(timeo, twdr->period);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700385 if (slot >= INET_TWDR_TWKILL_SLOTS)
386 slot = INET_TWDR_TWKILL_SLOTS - 1;
387 }
388 tw->tw_ttd = jiffies + timeo;
389 slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
390 list = &twdr->cells[slot];
391 } else {
392 tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK);
393
394 if (twdr->twcal_hand < 0) {
395 twdr->twcal_hand = 0;
396 twdr->twcal_jiffie = jiffies;
397 twdr->twcal_timer.expires = twdr->twcal_jiffie +
398 (slot << INET_TWDR_RECYCLE_TICK);
399 add_timer(&twdr->twcal_timer);
400 } else {
401 if (time_after(twdr->twcal_timer.expires,
402 jiffies + (slot << INET_TWDR_RECYCLE_TICK)))
403 mod_timer(&twdr->twcal_timer,
404 jiffies + (slot << INET_TWDR_RECYCLE_TICK));
405 slot = (twdr->twcal_hand + slot) & (INET_TWDR_RECYCLE_SLOTS - 1);
406 }
407 list = &twdr->twcal_row[slot];
408 }
409
410 hlist_add_head(&tw->tw_death_node, list);
411
412 if (twdr->tw_count++ == 0)
413 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
414 spin_unlock(&twdr->death_lock);
415}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700416EXPORT_SYMBOL_GPL(inet_twsk_schedule);
417
418void inet_twdr_twcal_tick(unsigned long data)
419{
420 struct inet_timewait_death_row *twdr;
421 int n, slot;
422 unsigned long j;
423 unsigned long now = jiffies;
424 int killed = 0;
425 int adv = 0;
426
427 twdr = (struct inet_timewait_death_row *)data;
428
429 spin_lock(&twdr->death_lock);
430 if (twdr->twcal_hand < 0)
431 goto out;
432
433 slot = twdr->twcal_hand;
434 j = twdr->twcal_jiffie;
435
436 for (n = 0; n < INET_TWDR_RECYCLE_SLOTS; n++) {
437 if (time_before_eq(j, now)) {
438 struct hlist_node *node, *safe;
439 struct inet_timewait_sock *tw;
440
441 inet_twsk_for_each_inmate_safe(tw, node, safe,
442 &twdr->twcal_row[slot]) {
443 __inet_twsk_del_dead_node(tw);
444 __inet_twsk_kill(tw, twdr->hashinfo);
Pavel Emelyanovf2bf4152008-07-16 20:32:25 -0700445#ifdef CONFIG_NET_NS
446 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED);
447#endif
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700448 inet_twsk_put(tw);
449 killed++;
450 }
451 } else {
452 if (!adv) {
453 adv = 1;
454 twdr->twcal_jiffie = j;
455 twdr->twcal_hand = slot;
456 }
457
458 if (!hlist_empty(&twdr->twcal_row[slot])) {
459 mod_timer(&twdr->twcal_timer, j);
460 goto out;
461 }
462 }
463 j += 1 << INET_TWDR_RECYCLE_TICK;
464 slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1);
465 }
466 twdr->twcal_hand = -1;
467
468out:
469 if ((twdr->tw_count -= killed) == 0)
470 del_timer(&twdr->tw_timer);
Pavel Emelyanovf2bf4152008-07-16 20:32:25 -0700471#ifndef CONFIG_NET_NS
472 NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITKILLED, killed);
473#endif
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700474 spin_unlock(&twdr->death_lock);
475}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700476EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);
Daniel Lezcanod3154922008-09-08 13:17:27 -0700477
Eric W. Biedermanb099ce22009-12-03 02:29:09 +0000478void inet_twsk_purge(struct inet_hashinfo *hashinfo,
Daniel Lezcanod3154922008-09-08 13:17:27 -0700479 struct inet_timewait_death_row *twdr, int family)
480{
481 struct inet_timewait_sock *tw;
482 struct sock *sk;
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800483 struct hlist_nulls_node *node;
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000484 unsigned int slot;
Daniel Lezcanod3154922008-09-08 13:17:27 -0700485
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000486 for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
487 struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
488restart_rcu:
489 rcu_read_lock();
Daniel Lezcanod3154922008-09-08 13:17:27 -0700490restart:
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000491 sk_nulls_for_each_rcu(sk, node, &head->twchain) {
Daniel Lezcanod3154922008-09-08 13:17:27 -0700492 tw = inet_twsk(sk);
Eric W. Biedermanb099ce22009-12-03 02:29:09 +0000493 if ((tw->tw_family != family) ||
494 atomic_read(&twsk_net(tw)->count))
Daniel Lezcanod3154922008-09-08 13:17:27 -0700495 continue;
496
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000497 if (unlikely(!atomic_inc_not_zero(&tw->tw_refcnt)))
498 continue;
499
Eric W. Biedermanb099ce22009-12-03 02:29:09 +0000500 if (unlikely((tw->tw_family != family) ||
501 atomic_read(&twsk_net(tw)->count))) {
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000502 inet_twsk_put(tw);
503 goto restart;
504 }
505
506 rcu_read_unlock();
Daniel Lezcanod3154922008-09-08 13:17:27 -0700507 inet_twsk_deschedule(tw, twdr);
508 inet_twsk_put(tw);
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000509 goto restart_rcu;
Daniel Lezcanod3154922008-09-08 13:17:27 -0700510 }
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000511 /* If the nulls value we got at the end of this lookup is
512 * not the expected one, we must restart lookup.
513 * We probably met an item that was moved to another chain.
514 */
515 if (get_nulls_value(node) != slot)
516 goto restart;
517 rcu_read_unlock();
Daniel Lezcanod3154922008-09-08 13:17:27 -0700518 }
Daniel Lezcanod3154922008-09-08 13:17:27 -0700519}
520EXPORT_SYMBOL_GPL(inet_twsk_purge);