blob: 717c411a5c6be876311c65ddc76dcb8bb24ac873 [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>
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070012#include <net/inet_hashtables.h>
13#include <net/inet_timewait_sock.h>
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -070014#include <net/ip.h>
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070015
16/* Must be called with locally disabled BHs. */
Adrian Bunkacd159b2007-07-14 19:00:59 -070017static void __inet_twsk_kill(struct inet_timewait_sock *tw,
18 struct inet_hashinfo *hashinfo)
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070019{
20 struct inet_bind_hashbucket *bhead;
21 struct inet_bind_bucket *tb;
22 /* Unlink from established hashes. */
Eric Dumazet230140c2007-11-07 02:40:20 -080023 rwlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070024
Eric Dumazet230140c2007-11-07 02:40:20 -080025 write_lock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070026 if (hlist_unhashed(&tw->tw_node)) {
Eric Dumazet230140c2007-11-07 02:40:20 -080027 write_unlock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070028 return;
29 }
30 __hlist_del(&tw->tw_node);
31 sk_node_init(&tw->tw_node);
Eric Dumazet230140c2007-11-07 02:40:20 -080032 write_unlock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070033
34 /* Disassociate with bind bucket. */
35 bhead = &hashinfo->bhash[inet_bhashfn(tw->tw_num, hashinfo->bhash_size)];
36 spin_lock(&bhead->lock);
37 tb = tw->tw_tb;
38 __hlist_del(&tw->tw_bind_node);
39 tw->tw_tb = NULL;
40 inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
41 spin_unlock(&bhead->lock);
42#ifdef SOCK_REFCNT_DEBUG
43 if (atomic_read(&tw->tw_refcnt) != 1) {
44 printk(KERN_DEBUG "%s timewait_sock %p refcnt=%d\n",
45 tw->tw_prot->name, tw, atomic_read(&tw->tw_refcnt));
46 }
47#endif
48 inet_twsk_put(tw);
49}
50
Pavel Emelyanov7054fb92007-12-20 15:32:54 -080051void inet_twsk_put(struct inet_timewait_sock *tw)
52{
53 if (atomic_dec_and_test(&tw->tw_refcnt)) {
54 struct module *owner = tw->tw_prot->owner;
55 twsk_destructor((struct sock *)tw);
56#ifdef SOCK_REFCNT_DEBUG
57 printk(KERN_DEBUG "%s timewait_sock %p released\n",
58 tw->tw_prot->name, tw);
59#endif
60 kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
61 module_put(owner);
62 }
63}
64EXPORT_SYMBOL_GPL(inet_twsk_put);
65
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070066/*
67 * Enter the time wait state. This is called with locally disabled BH.
68 * Essentially we whip up a timewait bucket, copy the relevant info into it
69 * from the SK, and mess with hash chains and list linkage.
70 */
71void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
72 struct inet_hashinfo *hashinfo)
73{
74 const struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070075 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet81c3d542005-10-03 14:13:38 -070076 struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
Eric Dumazet230140c2007-11-07 02:40:20 -080077 rwlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070078 struct inet_bind_hashbucket *bhead;
79 /* Step 1: Put TW into bind hash. Original socket stays there too.
80 Note, that any socket with inet->num != 0 MUST be bound in
81 binding cache, even if it is closed.
82 */
83 bhead = &hashinfo->bhash[inet_bhashfn(inet->num, hashinfo->bhash_size)];
84 spin_lock(&bhead->lock);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070085 tw->tw_tb = icsk->icsk_bind_hash;
86 BUG_TRAP(icsk->icsk_bind_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070087 inet_twsk_add_bind_node(tw, &tw->tw_tb->owners);
88 spin_unlock(&bhead->lock);
89
Eric Dumazet230140c2007-11-07 02:40:20 -080090 write_lock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070091
92 /* Step 2: Remove SK from established hash. */
93 if (__sk_del_node_init(sk))
Eric Dumazet65f76512008-01-03 20:46:48 -080094 sock_prot_inuse_add(sk->sk_prot, -1);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070095
Eric Dumazetdbca9b2752007-02-08 14:16:46 -080096 /* Step 3: Hash TW into TIMEWAIT chain. */
97 inet_twsk_add_node(tw, &ehead->twchain);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070098 atomic_inc(&tw->tw_refcnt);
99
Eric Dumazet230140c2007-11-07 02:40:20 -0800100 write_unlock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700101}
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700102
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700103EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
104
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700105struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state)
106{
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -0800107 struct inet_timewait_sock *tw =
108 kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800109 GFP_ATOMIC);
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700110 if (tw != NULL) {
111 const struct inet_sock *inet = inet_sk(sk);
112
113 /* Give us an identity. */
114 tw->tw_daddr = inet->daddr;
115 tw->tw_rcv_saddr = inet->rcv_saddr;
116 tw->tw_bound_dev_if = sk->sk_bound_dev_if;
117 tw->tw_num = inet->num;
118 tw->tw_state = TCP_TIME_WAIT;
119 tw->tw_substate = state;
120 tw->tw_sport = inet->sport;
121 tw->tw_dport = inet->dport;
122 tw->tw_family = sk->sk_family;
123 tw->tw_reuse = sk->sk_reuse;
Eric Dumazet81c3d542005-10-03 14:13:38 -0700124 tw->tw_hash = sk->sk_hash;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700125 tw->tw_ipv6only = 0;
126 tw->tw_prot = sk->sk_prot_creator;
Daniel Lezcano7951f0b2008-04-10 20:53:10 -0700127 tw->tw_net = sk->sk_net;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700128 atomic_set(&tw->tw_refcnt, 1);
129 inet_twsk_dead_node_init(tw);
Arnaldo Carvalho de Meloeeb2b852005-10-10 21:25:23 -0700130 __module_get(tw->tw_prot->owner);
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700131 }
132
133 return tw;
134}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700135
136EXPORT_SYMBOL_GPL(inet_twsk_alloc);
137
138/* Returns non-zero if quota exceeded. */
139static int inet_twdr_do_twkill_work(struct inet_timewait_death_row *twdr,
140 const int slot)
141{
142 struct inet_timewait_sock *tw;
143 struct hlist_node *node;
144 unsigned int killed;
145 int ret;
146
147 /* NOTE: compare this to previous version where lock
148 * was released after detaching chain. It was racy,
149 * because tw buckets are scheduled in not serialized context
150 * in 2.3 (with netfilter), and with softnet it is common, because
151 * soft irqs are not sequenced.
152 */
153 killed = 0;
154 ret = 0;
155rescan:
156 inet_twsk_for_each_inmate(tw, node, &twdr->cells[slot]) {
157 __inet_twsk_del_dead_node(tw);
158 spin_unlock(&twdr->death_lock);
159 __inet_twsk_kill(tw, twdr->hashinfo);
160 inet_twsk_put(tw);
161 killed++;
162 spin_lock(&twdr->death_lock);
163 if (killed > INET_TWDR_TWKILL_QUOTA) {
164 ret = 1;
165 break;
166 }
167
168 /* While we dropped twdr->death_lock, another cpu may have
169 * killed off the next TW bucket in the list, therefore
170 * do a fresh re-read of the hlist head node with the
171 * lock reacquired. We still use the hlist traversal
172 * macro in order to get the prefetches.
173 */
174 goto rescan;
175 }
176
177 twdr->tw_count -= killed;
178 NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITED, killed);
179
180 return ret;
181}
182
183void inet_twdr_hangman(unsigned long data)
184{
185 struct inet_timewait_death_row *twdr;
186 int unsigned need_timer;
187
188 twdr = (struct inet_timewait_death_row *)data;
189 spin_lock(&twdr->death_lock);
190
191 if (twdr->tw_count == 0)
192 goto out;
193
194 need_timer = 0;
195 if (inet_twdr_do_twkill_work(twdr, twdr->slot)) {
196 twdr->thread_slots |= (1 << twdr->slot);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700197 schedule_work(&twdr->twkill_work);
198 need_timer = 1;
199 } else {
200 /* We purged the entire slot, anything left? */
201 if (twdr->tw_count)
202 need_timer = 1;
203 }
204 twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1));
205 if (need_timer)
206 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
207out:
208 spin_unlock(&twdr->death_lock);
209}
210
211EXPORT_SYMBOL_GPL(inet_twdr_hangman);
212
David Howells65f27f32006-11-22 14:55:48 +0000213void inet_twdr_twkill_work(struct work_struct *work)
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700214{
David Howells65f27f32006-11-22 14:55:48 +0000215 struct inet_timewait_death_row *twdr =
216 container_of(work, struct inet_timewait_death_row, twkill_work);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700217 int i;
218
Pavel Emelyanov95c93822007-12-11 02:12:36 -0800219 BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) >
220 (sizeof(twdr->thread_slots) * 8));
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700221
222 while (twdr->thread_slots) {
223 spin_lock_bh(&twdr->death_lock);
224 for (i = 0; i < INET_TWDR_TWKILL_SLOTS; i++) {
225 if (!(twdr->thread_slots & (1 << i)))
226 continue;
227
228 while (inet_twdr_do_twkill_work(twdr, i) != 0) {
229 if (need_resched()) {
230 spin_unlock_bh(&twdr->death_lock);
231 schedule();
232 spin_lock_bh(&twdr->death_lock);
233 }
234 }
235
236 twdr->thread_slots &= ~(1 << i);
237 }
238 spin_unlock_bh(&twdr->death_lock);
239 }
240}
241
242EXPORT_SYMBOL_GPL(inet_twdr_twkill_work);
243
244/* These are always called from BH context. See callers in
245 * tcp_input.c to verify this.
246 */
247
248/* This is for handling early-kills of TIME_WAIT sockets. */
249void inet_twsk_deschedule(struct inet_timewait_sock *tw,
250 struct inet_timewait_death_row *twdr)
251{
252 spin_lock(&twdr->death_lock);
253 if (inet_twsk_del_dead_node(tw)) {
254 inet_twsk_put(tw);
255 if (--twdr->tw_count == 0)
256 del_timer(&twdr->tw_timer);
257 }
258 spin_unlock(&twdr->death_lock);
259 __inet_twsk_kill(tw, twdr->hashinfo);
260}
261
262EXPORT_SYMBOL(inet_twsk_deschedule);
263
264void inet_twsk_schedule(struct inet_timewait_sock *tw,
265 struct inet_timewait_death_row *twdr,
266 const int timeo, const int timewait_len)
267{
268 struct hlist_head *list;
269 int slot;
270
271 /* timeout := RTO * 3.5
272 *
273 * 3.5 = 1+2+0.5 to wait for two retransmits.
274 *
275 * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
276 * our ACK acking that FIN can be lost. If N subsequent retransmitted
277 * FINs (or previous seqments) are lost (probability of such event
278 * is p^(N+1), where p is probability to lose single packet and
279 * time to detect the loss is about RTO*(2^N - 1) with exponential
280 * backoff). Normal timewait length is calculated so, that we
281 * waited at least for one retransmitted FIN (maximal RTO is 120sec).
282 * [ BTW Linux. following BSD, violates this requirement waiting
283 * only for 60sec, we should wait at least for 240 secs.
284 * Well, 240 consumes too much of resources 8)
285 * ]
286 * This interval is not reduced to catch old duplicate and
287 * responces to our wandering segments living for two MSLs.
288 * However, if we use PAWS to detect
289 * old duplicates, we can reduce the interval to bounds required
290 * by RTO, rather than MSL. So, if peer understands PAWS, we
291 * kill tw bucket after 3.5*RTO (it is important that this number
292 * is greater than TS tick!) and detect old duplicates with help
293 * of PAWS.
294 */
295 slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK;
296
297 spin_lock(&twdr->death_lock);
298
299 /* Unlink it, if it was scheduled */
300 if (inet_twsk_del_dead_node(tw))
301 twdr->tw_count--;
302 else
303 atomic_inc(&tw->tw_refcnt);
304
305 if (slot >= INET_TWDR_RECYCLE_SLOTS) {
306 /* Schedule to slow timer */
307 if (timeo >= timewait_len) {
308 slot = INET_TWDR_TWKILL_SLOTS - 1;
309 } else {
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700310 slot = DIV_ROUND_UP(timeo, twdr->period);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700311 if (slot >= INET_TWDR_TWKILL_SLOTS)
312 slot = INET_TWDR_TWKILL_SLOTS - 1;
313 }
314 tw->tw_ttd = jiffies + timeo;
315 slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
316 list = &twdr->cells[slot];
317 } else {
318 tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK);
319
320 if (twdr->twcal_hand < 0) {
321 twdr->twcal_hand = 0;
322 twdr->twcal_jiffie = jiffies;
323 twdr->twcal_timer.expires = twdr->twcal_jiffie +
324 (slot << INET_TWDR_RECYCLE_TICK);
325 add_timer(&twdr->twcal_timer);
326 } else {
327 if (time_after(twdr->twcal_timer.expires,
328 jiffies + (slot << INET_TWDR_RECYCLE_TICK)))
329 mod_timer(&twdr->twcal_timer,
330 jiffies + (slot << INET_TWDR_RECYCLE_TICK));
331 slot = (twdr->twcal_hand + slot) & (INET_TWDR_RECYCLE_SLOTS - 1);
332 }
333 list = &twdr->twcal_row[slot];
334 }
335
336 hlist_add_head(&tw->tw_death_node, list);
337
338 if (twdr->tw_count++ == 0)
339 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
340 spin_unlock(&twdr->death_lock);
341}
342
343EXPORT_SYMBOL_GPL(inet_twsk_schedule);
344
345void inet_twdr_twcal_tick(unsigned long data)
346{
347 struct inet_timewait_death_row *twdr;
348 int n, slot;
349 unsigned long j;
350 unsigned long now = jiffies;
351 int killed = 0;
352 int adv = 0;
353
354 twdr = (struct inet_timewait_death_row *)data;
355
356 spin_lock(&twdr->death_lock);
357 if (twdr->twcal_hand < 0)
358 goto out;
359
360 slot = twdr->twcal_hand;
361 j = twdr->twcal_jiffie;
362
363 for (n = 0; n < INET_TWDR_RECYCLE_SLOTS; n++) {
364 if (time_before_eq(j, now)) {
365 struct hlist_node *node, *safe;
366 struct inet_timewait_sock *tw;
367
368 inet_twsk_for_each_inmate_safe(tw, node, safe,
369 &twdr->twcal_row[slot]) {
370 __inet_twsk_del_dead_node(tw);
371 __inet_twsk_kill(tw, twdr->hashinfo);
372 inet_twsk_put(tw);
373 killed++;
374 }
375 } else {
376 if (!adv) {
377 adv = 1;
378 twdr->twcal_jiffie = j;
379 twdr->twcal_hand = slot;
380 }
381
382 if (!hlist_empty(&twdr->twcal_row[slot])) {
383 mod_timer(&twdr->twcal_timer, j);
384 goto out;
385 }
386 }
387 j += 1 << INET_TWDR_RECYCLE_TICK;
388 slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1);
389 }
390 twdr->twcal_hand = -1;
391
392out:
393 if ((twdr->tw_count -= killed) == 0)
394 del_timer(&twdr->tw_timer);
395 NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITKILLED, killed);
396 spin_unlock(&twdr->death_lock);
397}
398
399EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);