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