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