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