Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[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                 release_net(twsk_net(tw));
61                 kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
62                 module_put(owner);
63         }
64 }
65 EXPORT_SYMBOL_GPL(inet_twsk_put);
66
67 /*
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  */
72 void __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);
76         const struct inet_connection_sock *icsk = inet_csk(sk);
77         struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
78         rwlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
79         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);
86         tw->tw_tb = icsk->icsk_bind_hash;
87         BUG_TRAP(icsk->icsk_bind_hash);
88         inet_twsk_add_bind_node(tw, &tw->tw_tb->owners);
89         spin_unlock(&bhead->lock);
90
91         write_lock(lock);
92
93         /* Step 2: Remove SK from established hash. */
94         if (__sk_del_node_init(sk))
95                 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
96
97         /* Step 3: Hash TW into TIMEWAIT chain. */
98         inet_twsk_add_node(tw, &ehead->twchain);
99         atomic_inc(&tw->tw_refcnt);
100
101         write_unlock(lock);
102 }
103
104 EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
105
106 struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state)
107 {
108         struct inet_timewait_sock *tw =
109                 kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
110                                  GFP_ATOMIC);
111         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;
125                 tw->tw_hash         = sk->sk_hash;
126                 tw->tw_ipv6only     = 0;
127                 tw->tw_prot         = sk->sk_prot_creator;
128                 twsk_net_set(tw, hold_net(sock_net(sk)));
129                 atomic_set(&tw->tw_refcnt, 1);
130                 inet_twsk_dead_node_init(tw);
131                 __module_get(tw->tw_prot->owner);
132         }
133
134         return tw;
135 }
136
137 EXPORT_SYMBOL_GPL(inet_twsk_alloc);
138
139 /* Returns non-zero if quota exceeded.  */
140 static 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;
156 rescan:
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
184 void 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);
198                 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);
208 out:
209         spin_unlock(&twdr->death_lock);
210 }
211
212 EXPORT_SYMBOL_GPL(inet_twdr_hangman);
213
214 void inet_twdr_twkill_work(struct work_struct *work)
215 {
216         struct inet_timewait_death_row *twdr =
217                 container_of(work, struct inet_timewait_death_row, twkill_work);
218         int i;
219
220         BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) >
221                         (sizeof(twdr->thread_slots) * 8));
222
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
243 EXPORT_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. */
250 void 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
263 EXPORT_SYMBOL(inet_twsk_deschedule);
264
265 void 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 {
311                         slot = DIV_ROUND_UP(timeo, twdr->period);
312                         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
344 EXPORT_SYMBOL_GPL(inet_twsk_schedule);
345
346 void 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
393 out:
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
400 EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);