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