Merge branch 'master' of /pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / net / ipv4 / inet_timewait_sock.c
1 /*
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
11 #include <linux/kernel.h>
12 #include <net/inet_hashtables.h>
13 #include <net/inet_timewait_sock.h>
14 #include <net/ip.h>
15
16 /* Must be called with locally disabled BHs. */
17 static void __inet_twsk_kill(struct inet_timewait_sock *tw,
18                              struct inet_hashinfo *hashinfo)
19 {
20         struct inet_bind_hashbucket *bhead;
21         struct inet_bind_bucket *tb;
22         /* Unlink from established hashes. */
23         rwlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
24
25         write_lock(lock);
26         if (hlist_unhashed(&tw->tw_node)) {
27                 write_unlock(lock);
28                 return;
29         }
30         __hlist_del(&tw->tw_node);
31         sk_node_init(&tw->tw_node);
32         write_unlock(lock);
33
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
51 void 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 }
64 EXPORT_SYMBOL_GPL(inet_twsk_put);
65
66 /*
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  */
71 void __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);
75         const struct inet_connection_sock *icsk = inet_csk(sk);
76         struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
77         rwlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
78         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);
85         tw->tw_tb = icsk->icsk_bind_hash;
86         BUG_TRAP(icsk->icsk_bind_hash);
87         inet_twsk_add_bind_node(tw, &tw->tw_tb->owners);
88         spin_unlock(&bhead->lock);
89
90         write_lock(lock);
91
92         /* Step 2: Remove SK from established hash. */
93         if (__sk_del_node_init(sk))
94                 sock_prot_inuse_add(sk->sk_prot, -1);
95
96         /* Step 3: Hash TW into TIMEWAIT chain. */
97         inet_twsk_add_node(tw, &ehead->twchain);
98         atomic_inc(&tw->tw_refcnt);
99
100         write_unlock(lock);
101 }
102
103 EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
104
105 struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state)
106 {
107         struct inet_timewait_sock *tw =
108                 kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
109                                  GFP_ATOMIC);
110         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;
124                 tw->tw_hash         = sk->sk_hash;
125                 tw->tw_ipv6only     = 0;
126                 tw->tw_prot         = sk->sk_prot_creator;
127                 tw->tw_net          = sk->sk_net;
128                 atomic_set(&tw->tw_refcnt, 1);
129                 inet_twsk_dead_node_init(tw);
130                 __module_get(tw->tw_prot->owner);
131         }
132
133         return tw;
134 }
135
136 EXPORT_SYMBOL_GPL(inet_twsk_alloc);
137
138 /* Returns non-zero if quota exceeded.  */
139 static 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;
155 rescan:
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
183 void 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);
197                 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);
207 out:
208         spin_unlock(&twdr->death_lock);
209 }
210
211 EXPORT_SYMBOL_GPL(inet_twdr_hangman);
212
213 void inet_twdr_twkill_work(struct work_struct *work)
214 {
215         struct inet_timewait_death_row *twdr =
216                 container_of(work, struct inet_timewait_death_row, twkill_work);
217         int i;
218
219         BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) >
220                         (sizeof(twdr->thread_slots) * 8));
221
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
242 EXPORT_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. */
249 void 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
262 EXPORT_SYMBOL(inet_twsk_deschedule);
263
264 void 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 {
310                         slot = DIV_ROUND_UP(timeo, twdr->period);
311                         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
343 EXPORT_SYMBOL_GPL(inet_twsk_schedule);
344
345 void 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
392 out:
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
399 EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);