blob: 03169fc848779fc0ee53bf3f76b05c5357c7f1bb [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
17/* Must be called with locally disabled BHs. */
Adrian Bunkacd159b2007-07-14 19:00:59 -070018static void __inet_twsk_kill(struct inet_timewait_sock *tw,
19 struct inet_hashinfo *hashinfo)
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070020{
21 struct inet_bind_hashbucket *bhead;
22 struct inet_bind_bucket *tb;
23 /* Unlink from established hashes. */
Eric Dumazet9db66bd2008-11-20 20:39:09 -080024 spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070025
Eric Dumazet9db66bd2008-11-20 20:39:09 -080026 spin_lock(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -080027 if (hlist_nulls_unhashed(&tw->tw_node)) {
Eric Dumazet9db66bd2008-11-20 20:39:09 -080028 spin_unlock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070029 return;
30 }
Eric Dumazet3ab5aee2008-11-16 19:40:17 -080031 hlist_nulls_del_rcu(&tw->tw_node);
32 sk_nulls_node_init(&tw->tw_node);
Eric Dumazet9db66bd2008-11-20 20:39:09 -080033 spin_unlock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070034
35 /* Disassociate with bind bucket. */
Pavel Emelyanov7f635ab2008-06-16 17:12:49 -070036 bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num,
37 hashinfo->bhash_size)];
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070038 spin_lock(&bhead->lock);
39 tb = tw->tw_tb;
40 __hlist_del(&tw->tw_bind_node);
41 tw->tw_tb = NULL;
42 inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
43 spin_unlock(&bhead->lock);
44#ifdef SOCK_REFCNT_DEBUG
45 if (atomic_read(&tw->tw_refcnt) != 1) {
46 printk(KERN_DEBUG "%s timewait_sock %p refcnt=%d\n",
47 tw->tw_prot->name, tw, atomic_read(&tw->tw_refcnt));
48 }
49#endif
50 inet_twsk_put(tw);
51}
52
Pavel Emelyanov7054fb92007-12-20 15:32:54 -080053void inet_twsk_put(struct inet_timewait_sock *tw)
54{
55 if (atomic_dec_and_test(&tw->tw_refcnt)) {
56 struct module *owner = tw->tw_prot->owner;
57 twsk_destructor((struct sock *)tw);
58#ifdef SOCK_REFCNT_DEBUG
59 printk(KERN_DEBUG "%s timewait_sock %p released\n",
60 tw->tw_prot->name, tw);
61#endif
Denis V. Lunevcd5342d2008-04-16 02:00:28 -070062 release_net(twsk_net(tw));
Pavel Emelyanov7054fb92007-12-20 15:32:54 -080063 kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
64 module_put(owner);
65 }
66}
67EXPORT_SYMBOL_GPL(inet_twsk_put);
68
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070069/*
70 * Enter the time wait state. This is called with locally disabled BH.
71 * Essentially we whip up a timewait bucket, copy the relevant info into it
72 * from the SK, and mess with hash chains and list linkage.
73 */
74void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
75 struct inet_hashinfo *hashinfo)
76{
77 const struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070078 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet81c3d542005-10-03 14:13:38 -070079 struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
Eric Dumazet9db66bd2008-11-20 20:39:09 -080080 spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070081 struct inet_bind_hashbucket *bhead;
82 /* Step 1: Put TW into bind hash. Original socket stays there too.
83 Note, that any socket with inet->num != 0 MUST be bound in
84 binding cache, even if it is closed.
85 */
Pavel Emelyanov7f635ab2008-06-16 17:12:49 -070086 bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->num,
87 hashinfo->bhash_size)];
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070088 spin_lock(&bhead->lock);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070089 tw->tw_tb = icsk->icsk_bind_hash;
Ilpo Järvinen547b7922008-07-25 21:43:18 -070090 WARN_ON(!icsk->icsk_bind_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070091 inet_twsk_add_bind_node(tw, &tw->tw_tb->owners);
92 spin_unlock(&bhead->lock);
93
Eric Dumazet9db66bd2008-11-20 20:39:09 -080094 spin_lock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070095
Eric Dumazet3ab5aee2008-11-16 19:40:17 -080096 /*
97 * Step 2: Hash TW into TIMEWAIT chain.
98 * Should be done before removing sk from established chain
99 * because readers are lockless and search established first.
100 */
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700101 atomic_inc(&tw->tw_refcnt);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800102 inet_twsk_add_node_rcu(tw, &ehead->twchain);
103
104 /* Step 3: Remove SK from established hash. */
105 if (__sk_nulls_del_node_init_rcu(sk))
106 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700107
Eric Dumazet9db66bd2008-11-20 20:39:09 -0800108 spin_unlock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700109}
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700110
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700111EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
112
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700113struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state)
114{
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800115 struct inet_timewait_sock *tw =
116 kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800117 GFP_ATOMIC);
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700118 if (tw != NULL) {
119 const struct inet_sock *inet = inet_sk(sk);
120
Vegard Nossum9e337b02008-10-18 17:37:51 +0200121 kmemcheck_annotate_bitfield(tw, flags);
122
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700123 /* Give us an identity. */
124 tw->tw_daddr = inet->daddr;
125 tw->tw_rcv_saddr = inet->rcv_saddr;
126 tw->tw_bound_dev_if = sk->sk_bound_dev_if;
127 tw->tw_num = inet->num;
128 tw->tw_state = TCP_TIME_WAIT;
129 tw->tw_substate = state;
130 tw->tw_sport = inet->sport;
131 tw->tw_dport = inet->dport;
132 tw->tw_family = sk->sk_family;
133 tw->tw_reuse = sk->sk_reuse;
Eric Dumazet81c3d542005-10-03 14:13:38 -0700134 tw->tw_hash = sk->sk_hash;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700135 tw->tw_ipv6only = 0;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -0700136 tw->tw_transparent = inet->transparent;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700137 tw->tw_prot = sk->sk_prot_creator;
Denis V. Lunevcd5342d2008-04-16 02:00:28 -0700138 twsk_net_set(tw, hold_net(sock_net(sk)));
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700139 atomic_set(&tw->tw_refcnt, 1);
140 inet_twsk_dead_node_init(tw);
Arnaldo Carvalho de Meloeeb2b852005-10-10 21:25:23 -0700141 __module_get(tw->tw_prot->owner);
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700142 }
143
144 return tw;
145}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700146
147EXPORT_SYMBOL_GPL(inet_twsk_alloc);
148
149/* Returns non-zero if quota exceeded. */
150static int inet_twdr_do_twkill_work(struct inet_timewait_death_row *twdr,
151 const int slot)
152{
153 struct inet_timewait_sock *tw;
154 struct hlist_node *node;
155 unsigned int killed;
156 int ret;
157
158 /* NOTE: compare this to previous version where lock
159 * was released after detaching chain. It was racy,
160 * because tw buckets are scheduled in not serialized context
161 * in 2.3 (with netfilter), and with softnet it is common, because
162 * soft irqs are not sequenced.
163 */
164 killed = 0;
165 ret = 0;
166rescan:
167 inet_twsk_for_each_inmate(tw, node, &twdr->cells[slot]) {
168 __inet_twsk_del_dead_node(tw);
169 spin_unlock(&twdr->death_lock);
170 __inet_twsk_kill(tw, twdr->hashinfo);
Pavel Emelyanovf2bf4152008-07-16 20:32:25 -0700171#ifdef CONFIG_NET_NS
172 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITED);
173#endif
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700174 inet_twsk_put(tw);
175 killed++;
176 spin_lock(&twdr->death_lock);
177 if (killed > INET_TWDR_TWKILL_QUOTA) {
178 ret = 1;
179 break;
180 }
181
182 /* While we dropped twdr->death_lock, another cpu may have
183 * killed off the next TW bucket in the list, therefore
184 * do a fresh re-read of the hlist head node with the
185 * lock reacquired. We still use the hlist traversal
186 * macro in order to get the prefetches.
187 */
188 goto rescan;
189 }
190
191 twdr->tw_count -= killed;
Pavel Emelyanovf2bf4152008-07-16 20:32:25 -0700192#ifndef CONFIG_NET_NS
193 NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITED, killed);
194#endif
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700195 return ret;
196}
197
198void inet_twdr_hangman(unsigned long data)
199{
200 struct inet_timewait_death_row *twdr;
201 int unsigned need_timer;
202
203 twdr = (struct inet_timewait_death_row *)data;
204 spin_lock(&twdr->death_lock);
205
206 if (twdr->tw_count == 0)
207 goto out;
208
209 need_timer = 0;
210 if (inet_twdr_do_twkill_work(twdr, twdr->slot)) {
211 twdr->thread_slots |= (1 << twdr->slot);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700212 schedule_work(&twdr->twkill_work);
213 need_timer = 1;
214 } else {
215 /* We purged the entire slot, anything left? */
216 if (twdr->tw_count)
217 need_timer = 1;
218 }
219 twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1));
220 if (need_timer)
221 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
222out:
223 spin_unlock(&twdr->death_lock);
224}
225
226EXPORT_SYMBOL_GPL(inet_twdr_hangman);
227
David Howells65f27f32006-11-22 14:55:48 +0000228void inet_twdr_twkill_work(struct work_struct *work)
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700229{
David Howells65f27f32006-11-22 14:55:48 +0000230 struct inet_timewait_death_row *twdr =
231 container_of(work, struct inet_timewait_death_row, twkill_work);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700232 int i;
233
Pavel Emelyanov95c93822007-12-11 02:12:36 -0800234 BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) >
235 (sizeof(twdr->thread_slots) * 8));
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700236
237 while (twdr->thread_slots) {
238 spin_lock_bh(&twdr->death_lock);
239 for (i = 0; i < INET_TWDR_TWKILL_SLOTS; i++) {
240 if (!(twdr->thread_slots & (1 << i)))
241 continue;
242
243 while (inet_twdr_do_twkill_work(twdr, i) != 0) {
244 if (need_resched()) {
245 spin_unlock_bh(&twdr->death_lock);
246 schedule();
247 spin_lock_bh(&twdr->death_lock);
248 }
249 }
250
251 twdr->thread_slots &= ~(1 << i);
252 }
253 spin_unlock_bh(&twdr->death_lock);
254 }
255}
256
257EXPORT_SYMBOL_GPL(inet_twdr_twkill_work);
258
259/* These are always called from BH context. See callers in
260 * tcp_input.c to verify this.
261 */
262
263/* This is for handling early-kills of TIME_WAIT sockets. */
264void inet_twsk_deschedule(struct inet_timewait_sock *tw,
265 struct inet_timewait_death_row *twdr)
266{
267 spin_lock(&twdr->death_lock);
268 if (inet_twsk_del_dead_node(tw)) {
269 inet_twsk_put(tw);
270 if (--twdr->tw_count == 0)
271 del_timer(&twdr->tw_timer);
272 }
273 spin_unlock(&twdr->death_lock);
274 __inet_twsk_kill(tw, twdr->hashinfo);
275}
276
277EXPORT_SYMBOL(inet_twsk_deschedule);
278
279void inet_twsk_schedule(struct inet_timewait_sock *tw,
280 struct inet_timewait_death_row *twdr,
281 const int timeo, const int timewait_len)
282{
283 struct hlist_head *list;
284 int slot;
285
286 /* timeout := RTO * 3.5
287 *
288 * 3.5 = 1+2+0.5 to wait for two retransmits.
289 *
290 * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
291 * our ACK acking that FIN can be lost. If N subsequent retransmitted
292 * FINs (or previous seqments) are lost (probability of such event
293 * is p^(N+1), where p is probability to lose single packet and
294 * time to detect the loss is about RTO*(2^N - 1) with exponential
295 * backoff). Normal timewait length is calculated so, that we
296 * waited at least for one retransmitted FIN (maximal RTO is 120sec).
297 * [ BTW Linux. following BSD, violates this requirement waiting
298 * only for 60sec, we should wait at least for 240 secs.
299 * Well, 240 consumes too much of resources 8)
300 * ]
301 * This interval is not reduced to catch old duplicate and
302 * responces to our wandering segments living for two MSLs.
303 * However, if we use PAWS to detect
304 * old duplicates, we can reduce the interval to bounds required
305 * by RTO, rather than MSL. So, if peer understands PAWS, we
306 * kill tw bucket after 3.5*RTO (it is important that this number
307 * is greater than TS tick!) and detect old duplicates with help
308 * of PAWS.
309 */
310 slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK;
311
312 spin_lock(&twdr->death_lock);
313
314 /* Unlink it, if it was scheduled */
315 if (inet_twsk_del_dead_node(tw))
316 twdr->tw_count--;
317 else
318 atomic_inc(&tw->tw_refcnt);
319
320 if (slot >= INET_TWDR_RECYCLE_SLOTS) {
321 /* Schedule to slow timer */
322 if (timeo >= timewait_len) {
323 slot = INET_TWDR_TWKILL_SLOTS - 1;
324 } else {
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700325 slot = DIV_ROUND_UP(timeo, twdr->period);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700326 if (slot >= INET_TWDR_TWKILL_SLOTS)
327 slot = INET_TWDR_TWKILL_SLOTS - 1;
328 }
329 tw->tw_ttd = jiffies + timeo;
330 slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
331 list = &twdr->cells[slot];
332 } else {
333 tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK);
334
335 if (twdr->twcal_hand < 0) {
336 twdr->twcal_hand = 0;
337 twdr->twcal_jiffie = jiffies;
338 twdr->twcal_timer.expires = twdr->twcal_jiffie +
339 (slot << INET_TWDR_RECYCLE_TICK);
340 add_timer(&twdr->twcal_timer);
341 } else {
342 if (time_after(twdr->twcal_timer.expires,
343 jiffies + (slot << INET_TWDR_RECYCLE_TICK)))
344 mod_timer(&twdr->twcal_timer,
345 jiffies + (slot << INET_TWDR_RECYCLE_TICK));
346 slot = (twdr->twcal_hand + slot) & (INET_TWDR_RECYCLE_SLOTS - 1);
347 }
348 list = &twdr->twcal_row[slot];
349 }
350
351 hlist_add_head(&tw->tw_death_node, list);
352
353 if (twdr->tw_count++ == 0)
354 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
355 spin_unlock(&twdr->death_lock);
356}
357
358EXPORT_SYMBOL_GPL(inet_twsk_schedule);
359
360void inet_twdr_twcal_tick(unsigned long data)
361{
362 struct inet_timewait_death_row *twdr;
363 int n, slot;
364 unsigned long j;
365 unsigned long now = jiffies;
366 int killed = 0;
367 int adv = 0;
368
369 twdr = (struct inet_timewait_death_row *)data;
370
371 spin_lock(&twdr->death_lock);
372 if (twdr->twcal_hand < 0)
373 goto out;
374
375 slot = twdr->twcal_hand;
376 j = twdr->twcal_jiffie;
377
378 for (n = 0; n < INET_TWDR_RECYCLE_SLOTS; n++) {
379 if (time_before_eq(j, now)) {
380 struct hlist_node *node, *safe;
381 struct inet_timewait_sock *tw;
382
383 inet_twsk_for_each_inmate_safe(tw, node, safe,
384 &twdr->twcal_row[slot]) {
385 __inet_twsk_del_dead_node(tw);
386 __inet_twsk_kill(tw, twdr->hashinfo);
Pavel Emelyanovf2bf4152008-07-16 20:32:25 -0700387#ifdef CONFIG_NET_NS
388 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED);
389#endif
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700390 inet_twsk_put(tw);
391 killed++;
392 }
393 } else {
394 if (!adv) {
395 adv = 1;
396 twdr->twcal_jiffie = j;
397 twdr->twcal_hand = slot;
398 }
399
400 if (!hlist_empty(&twdr->twcal_row[slot])) {
401 mod_timer(&twdr->twcal_timer, j);
402 goto out;
403 }
404 }
405 j += 1 << INET_TWDR_RECYCLE_TICK;
406 slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1);
407 }
408 twdr->twcal_hand = -1;
409
410out:
411 if ((twdr->tw_count -= killed) == 0)
412 del_timer(&twdr->tw_timer);
Pavel Emelyanovf2bf4152008-07-16 20:32:25 -0700413#ifndef CONFIG_NET_NS
414 NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITKILLED, killed);
415#endif
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700416 spin_unlock(&twdr->death_lock);
417}
418
419EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);
Daniel Lezcanod3154922008-09-08 13:17:27 -0700420
421void inet_twsk_purge(struct net *net, struct inet_hashinfo *hashinfo,
422 struct inet_timewait_death_row *twdr, int family)
423{
424 struct inet_timewait_sock *tw;
425 struct sock *sk;
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800426 struct hlist_nulls_node *node;
Daniel Lezcanod3154922008-09-08 13:17:27 -0700427 int h;
428
429 local_bh_disable();
430 for (h = 0; h < (hashinfo->ehash_size); h++) {
431 struct inet_ehash_bucket *head =
432 inet_ehash_bucket(hashinfo, h);
Eric Dumazet9db66bd2008-11-20 20:39:09 -0800433 spinlock_t *lock = inet_ehash_lockp(hashinfo, h);
Daniel Lezcanod3154922008-09-08 13:17:27 -0700434restart:
Eric Dumazet9db66bd2008-11-20 20:39:09 -0800435 spin_lock(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800436 sk_nulls_for_each(sk, node, &head->twchain) {
Daniel Lezcanod3154922008-09-08 13:17:27 -0700437
438 tw = inet_twsk(sk);
439 if (!net_eq(twsk_net(tw), net) ||
440 tw->tw_family != family)
441 continue;
442
443 atomic_inc(&tw->tw_refcnt);
Eric Dumazet9db66bd2008-11-20 20:39:09 -0800444 spin_unlock(lock);
Daniel Lezcanod3154922008-09-08 13:17:27 -0700445 inet_twsk_deschedule(tw, twdr);
446 inet_twsk_put(tw);
447
448 goto restart;
449 }
Eric Dumazet9db66bd2008-11-20 20:39:09 -0800450 spin_unlock(lock);
Daniel Lezcanod3154922008-09-08 13:17:27 -0700451 }
452 local_bh_enable();
453}
454EXPORT_SYMBOL_GPL(inet_twsk_purge);