tcp: Fix a connect() race with timewait sockets
[linux-2.6.git] / 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 <linux/kmemcheck.h>
13 #include <net/inet_hashtables.h>
14 #include <net/inet_timewait_sock.h>
15 #include <net/ip.h>
16
17
18 /*
19  * unhash a timewait socket from established hash
20  * lock must be hold by caller
21  */
22 int inet_twsk_unhash(struct inet_timewait_sock *tw)
23 {
24         if (hlist_nulls_unhashed(&tw->tw_node))
25                 return 0;
26
27         hlist_nulls_del_rcu(&tw->tw_node);
28         sk_nulls_node_init(&tw->tw_node);
29         return 1;
30 }
31
32 /*
33  * unhash a timewait socket from bind hash
34  * lock must be hold by caller
35  */
36 int inet_twsk_bind_unhash(struct inet_timewait_sock *tw,
37                           struct inet_hashinfo *hashinfo)
38 {
39         struct inet_bind_bucket *tb = tw->tw_tb;
40
41         if (!tb)
42                 return 0;
43
44         __hlist_del(&tw->tw_bind_node);
45         tw->tw_tb = NULL;
46         inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
47         return 1;
48 }
49
50 /* Must be called with locally disabled BHs. */
51 static void __inet_twsk_kill(struct inet_timewait_sock *tw,
52                              struct inet_hashinfo *hashinfo)
53 {
54         struct inet_bind_hashbucket *bhead;
55         int refcnt;
56         /* Unlink from established hashes. */
57         spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
58
59         spin_lock(lock);
60         refcnt = inet_twsk_unhash(tw);
61         spin_unlock(lock);
62
63         /* Disassociate with bind bucket. */
64         bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num,
65                         hashinfo->bhash_size)];
66
67         spin_lock(&bhead->lock);
68         refcnt += inet_twsk_bind_unhash(tw, hashinfo);
69         spin_unlock(&bhead->lock);
70
71 #ifdef SOCK_REFCNT_DEBUG
72         if (atomic_read(&tw->tw_refcnt) != 1) {
73                 printk(KERN_DEBUG "%s timewait_sock %p refcnt=%d\n",
74                        tw->tw_prot->name, tw, atomic_read(&tw->tw_refcnt));
75         }
76 #endif
77         while (refcnt) {
78                 inet_twsk_put(tw);
79                 refcnt--;
80         }
81 }
82
83 static noinline void inet_twsk_free(struct inet_timewait_sock *tw)
84 {
85         struct module *owner = tw->tw_prot->owner;
86         twsk_destructor((struct sock *)tw);
87 #ifdef SOCK_REFCNT_DEBUG
88         pr_debug("%s timewait_sock %p released\n", tw->tw_prot->name, tw);
89 #endif
90         release_net(twsk_net(tw));
91         kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
92         module_put(owner);
93 }
94
95 void inet_twsk_put(struct inet_timewait_sock *tw)
96 {
97         if (atomic_dec_and_test(&tw->tw_refcnt))
98                 inet_twsk_free(tw);
99 }
100 EXPORT_SYMBOL_GPL(inet_twsk_put);
101
102 /*
103  * Enter the time wait state. This is called with locally disabled BH.
104  * Essentially we whip up a timewait bucket, copy the relevant info into it
105  * from the SK, and mess with hash chains and list linkage.
106  */
107 void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
108                            struct inet_hashinfo *hashinfo)
109 {
110         const struct inet_sock *inet = inet_sk(sk);
111         const struct inet_connection_sock *icsk = inet_csk(sk);
112         struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
113         spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
114         struct inet_bind_hashbucket *bhead;
115         /* Step 1: Put TW into bind hash. Original socket stays there too.
116            Note, that any socket with inet->num != 0 MUST be bound in
117            binding cache, even if it is closed.
118          */
119         bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->inet_num,
120                         hashinfo->bhash_size)];
121         spin_lock(&bhead->lock);
122         tw->tw_tb = icsk->icsk_bind_hash;
123         WARN_ON(!icsk->icsk_bind_hash);
124         inet_twsk_add_bind_node(tw, &tw->tw_tb->owners);
125         spin_unlock(&bhead->lock);
126
127         spin_lock(lock);
128
129         /*
130          * Step 2: Hash TW into TIMEWAIT chain.
131          * Should be done before removing sk from established chain
132          * because readers are lockless and search established first.
133          */
134         inet_twsk_add_node_rcu(tw, &ehead->twchain);
135
136         /* Step 3: Remove SK from established hash. */
137         if (__sk_nulls_del_node_init_rcu(sk))
138                 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
139
140         /*
141          * Notes :
142          * - We initially set tw_refcnt to 0 in inet_twsk_alloc()
143          * - We add one reference for the bhash link
144          * - We add one reference for the ehash link
145          * - We want this refcnt update done before allowing other
146          *   threads to find this tw in ehash chain.
147          */
148         atomic_add(1 + 1 + 1, &tw->tw_refcnt);
149
150         spin_unlock(lock);
151 }
152
153 EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
154
155 struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state)
156 {
157         struct inet_timewait_sock *tw =
158                 kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
159                                  GFP_ATOMIC);
160         if (tw != NULL) {
161                 const struct inet_sock *inet = inet_sk(sk);
162
163                 kmemcheck_annotate_bitfield(tw, flags);
164
165                 /* Give us an identity. */
166                 tw->tw_daddr        = inet->inet_daddr;
167                 tw->tw_rcv_saddr    = inet->inet_rcv_saddr;
168                 tw->tw_bound_dev_if = sk->sk_bound_dev_if;
169                 tw->tw_num          = inet->inet_num;
170                 tw->tw_state        = TCP_TIME_WAIT;
171                 tw->tw_substate     = state;
172                 tw->tw_sport        = inet->inet_sport;
173                 tw->tw_dport        = inet->inet_dport;
174                 tw->tw_family       = sk->sk_family;
175                 tw->tw_reuse        = sk->sk_reuse;
176                 tw->tw_hash         = sk->sk_hash;
177                 tw->tw_ipv6only     = 0;
178                 tw->tw_transparent  = inet->transparent;
179                 tw->tw_prot         = sk->sk_prot_creator;
180                 twsk_net_set(tw, hold_net(sock_net(sk)));
181                 /*
182                  * Because we use RCU lookups, we should not set tw_refcnt
183                  * to a non null value before everything is setup for this
184                  * timewait socket.
185                  */
186                 atomic_set(&tw->tw_refcnt, 0);
187                 inet_twsk_dead_node_init(tw);
188                 __module_get(tw->tw_prot->owner);
189         }
190
191         return tw;
192 }
193
194 EXPORT_SYMBOL_GPL(inet_twsk_alloc);
195
196 /* Returns non-zero if quota exceeded.  */
197 static int inet_twdr_do_twkill_work(struct inet_timewait_death_row *twdr,
198                                     const int slot)
199 {
200         struct inet_timewait_sock *tw;
201         struct hlist_node *node;
202         unsigned int killed;
203         int ret;
204
205         /* NOTE: compare this to previous version where lock
206          * was released after detaching chain. It was racy,
207          * because tw buckets are scheduled in not serialized context
208          * in 2.3 (with netfilter), and with softnet it is common, because
209          * soft irqs are not sequenced.
210          */
211         killed = 0;
212         ret = 0;
213 rescan:
214         inet_twsk_for_each_inmate(tw, node, &twdr->cells[slot]) {
215                 __inet_twsk_del_dead_node(tw);
216                 spin_unlock(&twdr->death_lock);
217                 __inet_twsk_kill(tw, twdr->hashinfo);
218 #ifdef CONFIG_NET_NS
219                 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITED);
220 #endif
221                 inet_twsk_put(tw);
222                 killed++;
223                 spin_lock(&twdr->death_lock);
224                 if (killed > INET_TWDR_TWKILL_QUOTA) {
225                         ret = 1;
226                         break;
227                 }
228
229                 /* While we dropped twdr->death_lock, another cpu may have
230                  * killed off the next TW bucket in the list, therefore
231                  * do a fresh re-read of the hlist head node with the
232                  * lock reacquired.  We still use the hlist traversal
233                  * macro in order to get the prefetches.
234                  */
235                 goto rescan;
236         }
237
238         twdr->tw_count -= killed;
239 #ifndef CONFIG_NET_NS
240         NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITED, killed);
241 #endif
242         return ret;
243 }
244
245 void inet_twdr_hangman(unsigned long data)
246 {
247         struct inet_timewait_death_row *twdr;
248         int unsigned need_timer;
249
250         twdr = (struct inet_timewait_death_row *)data;
251         spin_lock(&twdr->death_lock);
252
253         if (twdr->tw_count == 0)
254                 goto out;
255
256         need_timer = 0;
257         if (inet_twdr_do_twkill_work(twdr, twdr->slot)) {
258                 twdr->thread_slots |= (1 << twdr->slot);
259                 schedule_work(&twdr->twkill_work);
260                 need_timer = 1;
261         } else {
262                 /* We purged the entire slot, anything left?  */
263                 if (twdr->tw_count)
264                         need_timer = 1;
265                 twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1));
266         }
267         if (need_timer)
268                 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
269 out:
270         spin_unlock(&twdr->death_lock);
271 }
272
273 EXPORT_SYMBOL_GPL(inet_twdr_hangman);
274
275 void inet_twdr_twkill_work(struct work_struct *work)
276 {
277         struct inet_timewait_death_row *twdr =
278                 container_of(work, struct inet_timewait_death_row, twkill_work);
279         int i;
280
281         BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) >
282                         (sizeof(twdr->thread_slots) * 8));
283
284         while (twdr->thread_slots) {
285                 spin_lock_bh(&twdr->death_lock);
286                 for (i = 0; i < INET_TWDR_TWKILL_SLOTS; i++) {
287                         if (!(twdr->thread_slots & (1 << i)))
288                                 continue;
289
290                         while (inet_twdr_do_twkill_work(twdr, i) != 0) {
291                                 if (need_resched()) {
292                                         spin_unlock_bh(&twdr->death_lock);
293                                         schedule();
294                                         spin_lock_bh(&twdr->death_lock);
295                                 }
296                         }
297
298                         twdr->thread_slots &= ~(1 << i);
299                 }
300                 spin_unlock_bh(&twdr->death_lock);
301         }
302 }
303
304 EXPORT_SYMBOL_GPL(inet_twdr_twkill_work);
305
306 /* These are always called from BH context.  See callers in
307  * tcp_input.c to verify this.
308  */
309
310 /* This is for handling early-kills of TIME_WAIT sockets. */
311 void inet_twsk_deschedule(struct inet_timewait_sock *tw,
312                           struct inet_timewait_death_row *twdr)
313 {
314         spin_lock(&twdr->death_lock);
315         if (inet_twsk_del_dead_node(tw)) {
316                 inet_twsk_put(tw);
317                 if (--twdr->tw_count == 0)
318                         del_timer(&twdr->tw_timer);
319         }
320         spin_unlock(&twdr->death_lock);
321         __inet_twsk_kill(tw, twdr->hashinfo);
322 }
323
324 EXPORT_SYMBOL(inet_twsk_deschedule);
325
326 void inet_twsk_schedule(struct inet_timewait_sock *tw,
327                        struct inet_timewait_death_row *twdr,
328                        const int timeo, const int timewait_len)
329 {
330         struct hlist_head *list;
331         int slot;
332
333         /* timeout := RTO * 3.5
334          *
335          * 3.5 = 1+2+0.5 to wait for two retransmits.
336          *
337          * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
338          * our ACK acking that FIN can be lost. If N subsequent retransmitted
339          * FINs (or previous seqments) are lost (probability of such event
340          * is p^(N+1), where p is probability to lose single packet and
341          * time to detect the loss is about RTO*(2^N - 1) with exponential
342          * backoff). Normal timewait length is calculated so, that we
343          * waited at least for one retransmitted FIN (maximal RTO is 120sec).
344          * [ BTW Linux. following BSD, violates this requirement waiting
345          *   only for 60sec, we should wait at least for 240 secs.
346          *   Well, 240 consumes too much of resources 8)
347          * ]
348          * This interval is not reduced to catch old duplicate and
349          * responces to our wandering segments living for two MSLs.
350          * However, if we use PAWS to detect
351          * old duplicates, we can reduce the interval to bounds required
352          * by RTO, rather than MSL. So, if peer understands PAWS, we
353          * kill tw bucket after 3.5*RTO (it is important that this number
354          * is greater than TS tick!) and detect old duplicates with help
355          * of PAWS.
356          */
357         slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK;
358
359         spin_lock(&twdr->death_lock);
360
361         /* Unlink it, if it was scheduled */
362         if (inet_twsk_del_dead_node(tw))
363                 twdr->tw_count--;
364         else
365                 atomic_inc(&tw->tw_refcnt);
366
367         if (slot >= INET_TWDR_RECYCLE_SLOTS) {
368                 /* Schedule to slow timer */
369                 if (timeo >= timewait_len) {
370                         slot = INET_TWDR_TWKILL_SLOTS - 1;
371                 } else {
372                         slot = DIV_ROUND_UP(timeo, twdr->period);
373                         if (slot >= INET_TWDR_TWKILL_SLOTS)
374                                 slot = INET_TWDR_TWKILL_SLOTS - 1;
375                 }
376                 tw->tw_ttd = jiffies + timeo;
377                 slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
378                 list = &twdr->cells[slot];
379         } else {
380                 tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK);
381
382                 if (twdr->twcal_hand < 0) {
383                         twdr->twcal_hand = 0;
384                         twdr->twcal_jiffie = jiffies;
385                         twdr->twcal_timer.expires = twdr->twcal_jiffie +
386                                               (slot << INET_TWDR_RECYCLE_TICK);
387                         add_timer(&twdr->twcal_timer);
388                 } else {
389                         if (time_after(twdr->twcal_timer.expires,
390                                        jiffies + (slot << INET_TWDR_RECYCLE_TICK)))
391                                 mod_timer(&twdr->twcal_timer,
392                                           jiffies + (slot << INET_TWDR_RECYCLE_TICK));
393                         slot = (twdr->twcal_hand + slot) & (INET_TWDR_RECYCLE_SLOTS - 1);
394                 }
395                 list = &twdr->twcal_row[slot];
396         }
397
398         hlist_add_head(&tw->tw_death_node, list);
399
400         if (twdr->tw_count++ == 0)
401                 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
402         spin_unlock(&twdr->death_lock);
403 }
404
405 EXPORT_SYMBOL_GPL(inet_twsk_schedule);
406
407 void inet_twdr_twcal_tick(unsigned long data)
408 {
409         struct inet_timewait_death_row *twdr;
410         int n, slot;
411         unsigned long j;
412         unsigned long now = jiffies;
413         int killed = 0;
414         int adv = 0;
415
416         twdr = (struct inet_timewait_death_row *)data;
417
418         spin_lock(&twdr->death_lock);
419         if (twdr->twcal_hand < 0)
420                 goto out;
421
422         slot = twdr->twcal_hand;
423         j = twdr->twcal_jiffie;
424
425         for (n = 0; n < INET_TWDR_RECYCLE_SLOTS; n++) {
426                 if (time_before_eq(j, now)) {
427                         struct hlist_node *node, *safe;
428                         struct inet_timewait_sock *tw;
429
430                         inet_twsk_for_each_inmate_safe(tw, node, safe,
431                                                        &twdr->twcal_row[slot]) {
432                                 __inet_twsk_del_dead_node(tw);
433                                 __inet_twsk_kill(tw, twdr->hashinfo);
434 #ifdef CONFIG_NET_NS
435                                 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED);
436 #endif
437                                 inet_twsk_put(tw);
438                                 killed++;
439                         }
440                 } else {
441                         if (!adv) {
442                                 adv = 1;
443                                 twdr->twcal_jiffie = j;
444                                 twdr->twcal_hand = slot;
445                         }
446
447                         if (!hlist_empty(&twdr->twcal_row[slot])) {
448                                 mod_timer(&twdr->twcal_timer, j);
449                                 goto out;
450                         }
451                 }
452                 j += 1 << INET_TWDR_RECYCLE_TICK;
453                 slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1);
454         }
455         twdr->twcal_hand = -1;
456
457 out:
458         if ((twdr->tw_count -= killed) == 0)
459                 del_timer(&twdr->tw_timer);
460 #ifndef CONFIG_NET_NS
461         NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITKILLED, killed);
462 #endif
463         spin_unlock(&twdr->death_lock);
464 }
465
466 EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);
467
468 void inet_twsk_purge(struct inet_hashinfo *hashinfo,
469                      struct inet_timewait_death_row *twdr, int family)
470 {
471         struct inet_timewait_sock *tw;
472         struct sock *sk;
473         struct hlist_nulls_node *node;
474         unsigned int slot;
475
476         for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
477                 struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
478 restart_rcu:
479                 rcu_read_lock();
480 restart:
481                 sk_nulls_for_each_rcu(sk, node, &head->twchain) {
482                         tw = inet_twsk(sk);
483                         if ((tw->tw_family != family) ||
484                                 atomic_read(&twsk_net(tw)->count))
485                                 continue;
486
487                         if (unlikely(!atomic_inc_not_zero(&tw->tw_refcnt)))
488                                 continue;
489
490                         if (unlikely((tw->tw_family != family) ||
491                                      atomic_read(&twsk_net(tw)->count))) {
492                                 inet_twsk_put(tw);
493                                 goto restart;
494                         }
495
496                         rcu_read_unlock();
497                         inet_twsk_deschedule(tw, twdr);
498                         inet_twsk_put(tw);
499                         goto restart_rcu;
500                 }
501                 /* If the nulls value we got at the end of this lookup is
502                  * not the expected one, we must restart lookup.
503                  * We probably met an item that was moved to another chain.
504                  */
505                 if (get_nulls_value(node) != slot)
506                         goto restart;
507                 rcu_read_unlock();
508         }
509 }
510 EXPORT_SYMBOL_GPL(inet_twsk_purge);