Merge git://git.infradead.org/~kmpark/onenand-mtd-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                 atomic_set(&tw->tw_refcnt, 1);
128                 inet_twsk_dead_node_init(tw);
129                 __module_get(tw->tw_prot->owner);
130         }
131
132         return tw;
133 }
134
135 EXPORT_SYMBOL_GPL(inet_twsk_alloc);
136
137 /* Returns non-zero if quota exceeded.  */
138 static int inet_twdr_do_twkill_work(struct inet_timewait_death_row *twdr,
139                                     const int slot)
140 {
141         struct inet_timewait_sock *tw;
142         struct hlist_node *node;
143         unsigned int killed;
144         int ret;
145
146         /* NOTE: compare this to previous version where lock
147          * was released after detaching chain. It was racy,
148          * because tw buckets are scheduled in not serialized context
149          * in 2.3 (with netfilter), and with softnet it is common, because
150          * soft irqs are not sequenced.
151          */
152         killed = 0;
153         ret = 0;
154 rescan:
155         inet_twsk_for_each_inmate(tw, node, &twdr->cells[slot]) {
156                 __inet_twsk_del_dead_node(tw);
157                 spin_unlock(&twdr->death_lock);
158                 __inet_twsk_kill(tw, twdr->hashinfo);
159                 inet_twsk_put(tw);
160                 killed++;
161                 spin_lock(&twdr->death_lock);
162                 if (killed > INET_TWDR_TWKILL_QUOTA) {
163                         ret = 1;
164                         break;
165                 }
166
167                 /* While we dropped twdr->death_lock, another cpu may have
168                  * killed off the next TW bucket in the list, therefore
169                  * do a fresh re-read of the hlist head node with the
170                  * lock reacquired.  We still use the hlist traversal
171                  * macro in order to get the prefetches.
172                  */
173                 goto rescan;
174         }
175
176         twdr->tw_count -= killed;
177         NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITED, killed);
178
179         return ret;
180 }
181
182 void inet_twdr_hangman(unsigned long data)
183 {
184         struct inet_timewait_death_row *twdr;
185         int unsigned need_timer;
186
187         twdr = (struct inet_timewait_death_row *)data;
188         spin_lock(&twdr->death_lock);
189
190         if (twdr->tw_count == 0)
191                 goto out;
192
193         need_timer = 0;
194         if (inet_twdr_do_twkill_work(twdr, twdr->slot)) {
195                 twdr->thread_slots |= (1 << twdr->slot);
196                 schedule_work(&twdr->twkill_work);
197                 need_timer = 1;
198         } else {
199                 /* We purged the entire slot, anything left?  */
200                 if (twdr->tw_count)
201                         need_timer = 1;
202         }
203         twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1));
204         if (need_timer)
205                 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
206 out:
207         spin_unlock(&twdr->death_lock);
208 }
209
210 EXPORT_SYMBOL_GPL(inet_twdr_hangman);
211
212 void inet_twdr_twkill_work(struct work_struct *work)
213 {
214         struct inet_timewait_death_row *twdr =
215                 container_of(work, struct inet_timewait_death_row, twkill_work);
216         int i;
217
218         BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) >
219                         (sizeof(twdr->thread_slots) * 8));
220
221         while (twdr->thread_slots) {
222                 spin_lock_bh(&twdr->death_lock);
223                 for (i = 0; i < INET_TWDR_TWKILL_SLOTS; i++) {
224                         if (!(twdr->thread_slots & (1 << i)))
225                                 continue;
226
227                         while (inet_twdr_do_twkill_work(twdr, i) != 0) {
228                                 if (need_resched()) {
229                                         spin_unlock_bh(&twdr->death_lock);
230                                         schedule();
231                                         spin_lock_bh(&twdr->death_lock);
232                                 }
233                         }
234
235                         twdr->thread_slots &= ~(1 << i);
236                 }
237                 spin_unlock_bh(&twdr->death_lock);
238         }
239 }
240
241 EXPORT_SYMBOL_GPL(inet_twdr_twkill_work);
242
243 /* These are always called from BH context.  See callers in
244  * tcp_input.c to verify this.
245  */
246
247 /* This is for handling early-kills of TIME_WAIT sockets. */
248 void inet_twsk_deschedule(struct inet_timewait_sock *tw,
249                           struct inet_timewait_death_row *twdr)
250 {
251         spin_lock(&twdr->death_lock);
252         if (inet_twsk_del_dead_node(tw)) {
253                 inet_twsk_put(tw);
254                 if (--twdr->tw_count == 0)
255                         del_timer(&twdr->tw_timer);
256         }
257         spin_unlock(&twdr->death_lock);
258         __inet_twsk_kill(tw, twdr->hashinfo);
259 }
260
261 EXPORT_SYMBOL(inet_twsk_deschedule);
262
263 void inet_twsk_schedule(struct inet_timewait_sock *tw,
264                        struct inet_timewait_death_row *twdr,
265                        const int timeo, const int timewait_len)
266 {
267         struct hlist_head *list;
268         int slot;
269
270         /* timeout := RTO * 3.5
271          *
272          * 3.5 = 1+2+0.5 to wait for two retransmits.
273          *
274          * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
275          * our ACK acking that FIN can be lost. If N subsequent retransmitted
276          * FINs (or previous seqments) are lost (probability of such event
277          * is p^(N+1), where p is probability to lose single packet and
278          * time to detect the loss is about RTO*(2^N - 1) with exponential
279          * backoff). Normal timewait length is calculated so, that we
280          * waited at least for one retransmitted FIN (maximal RTO is 120sec).
281          * [ BTW Linux. following BSD, violates this requirement waiting
282          *   only for 60sec, we should wait at least for 240 secs.
283          *   Well, 240 consumes too much of resources 8)
284          * ]
285          * This interval is not reduced to catch old duplicate and
286          * responces to our wandering segments living for two MSLs.
287          * However, if we use PAWS to detect
288          * old duplicates, we can reduce the interval to bounds required
289          * by RTO, rather than MSL. So, if peer understands PAWS, we
290          * kill tw bucket after 3.5*RTO (it is important that this number
291          * is greater than TS tick!) and detect old duplicates with help
292          * of PAWS.
293          */
294         slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK;
295
296         spin_lock(&twdr->death_lock);
297
298         /* Unlink it, if it was scheduled */
299         if (inet_twsk_del_dead_node(tw))
300                 twdr->tw_count--;
301         else
302                 atomic_inc(&tw->tw_refcnt);
303
304         if (slot >= INET_TWDR_RECYCLE_SLOTS) {
305                 /* Schedule to slow timer */
306                 if (timeo >= timewait_len) {
307                         slot = INET_TWDR_TWKILL_SLOTS - 1;
308                 } else {
309                         slot = DIV_ROUND_UP(timeo, twdr->period);
310                         if (slot >= INET_TWDR_TWKILL_SLOTS)
311                                 slot = INET_TWDR_TWKILL_SLOTS - 1;
312                 }
313                 tw->tw_ttd = jiffies + timeo;
314                 slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
315                 list = &twdr->cells[slot];
316         } else {
317                 tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK);
318
319                 if (twdr->twcal_hand < 0) {
320                         twdr->twcal_hand = 0;
321                         twdr->twcal_jiffie = jiffies;
322                         twdr->twcal_timer.expires = twdr->twcal_jiffie +
323                                               (slot << INET_TWDR_RECYCLE_TICK);
324                         add_timer(&twdr->twcal_timer);
325                 } else {
326                         if (time_after(twdr->twcal_timer.expires,
327                                        jiffies + (slot << INET_TWDR_RECYCLE_TICK)))
328                                 mod_timer(&twdr->twcal_timer,
329                                           jiffies + (slot << INET_TWDR_RECYCLE_TICK));
330                         slot = (twdr->twcal_hand + slot) & (INET_TWDR_RECYCLE_SLOTS - 1);
331                 }
332                 list = &twdr->twcal_row[slot];
333         }
334
335         hlist_add_head(&tw->tw_death_node, list);
336
337         if (twdr->tw_count++ == 0)
338                 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
339         spin_unlock(&twdr->death_lock);
340 }
341
342 EXPORT_SYMBOL_GPL(inet_twsk_schedule);
343
344 void inet_twdr_twcal_tick(unsigned long data)
345 {
346         struct inet_timewait_death_row *twdr;
347         int n, slot;
348         unsigned long j;
349         unsigned long now = jiffies;
350         int killed = 0;
351         int adv = 0;
352
353         twdr = (struct inet_timewait_death_row *)data;
354
355         spin_lock(&twdr->death_lock);
356         if (twdr->twcal_hand < 0)
357                 goto out;
358
359         slot = twdr->twcal_hand;
360         j = twdr->twcal_jiffie;
361
362         for (n = 0; n < INET_TWDR_RECYCLE_SLOTS; n++) {
363                 if (time_before_eq(j, now)) {
364                         struct hlist_node *node, *safe;
365                         struct inet_timewait_sock *tw;
366
367                         inet_twsk_for_each_inmate_safe(tw, node, safe,
368                                                        &twdr->twcal_row[slot]) {
369                                 __inet_twsk_del_dead_node(tw);
370                                 __inet_twsk_kill(tw, twdr->hashinfo);
371                                 inet_twsk_put(tw);
372                                 killed++;
373                         }
374                 } else {
375                         if (!adv) {
376                                 adv = 1;
377                                 twdr->twcal_jiffie = j;
378                                 twdr->twcal_hand = slot;
379                         }
380
381                         if (!hlist_empty(&twdr->twcal_row[slot])) {
382                                 mod_timer(&twdr->twcal_timer, j);
383                                 goto out;
384                         }
385                 }
386                 j += 1 << INET_TWDR_RECYCLE_TICK;
387                 slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1);
388         }
389         twdr->twcal_hand = -1;
390
391 out:
392         if ((twdr->tw_count -= killed) == 0)
393                 del_timer(&twdr->tw_timer);
394         NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITKILLED, killed);
395         spin_unlock(&twdr->death_lock);
396 }
397
398 EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);