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
11 #include <linux/kernel.h>
12 #include <linux/kmemcheck.h>
13 #include <linux/slab.h>
14 #include <net/inet_hashtables.h>
15 #include <net/inet_timewait_sock.h>
20 * inet_twsk_unhash - unhash a timewait socket from established hash
21 * @tw: timewait socket
23 * unhash a timewait socket from established hash, if hashed.
24 * ehash lock must be held by caller.
25 * Returns 1 if caller should call inet_twsk_put() after lock release.
27 int inet_twsk_unhash(struct inet_timewait_sock *tw)
29 if (hlist_nulls_unhashed(&tw->tw_node))
32 hlist_nulls_del_rcu(&tw->tw_node);
33 sk_nulls_node_init(&tw->tw_node);
35 * We cannot call inet_twsk_put() ourself under lock,
36 * caller must call it for us.
42 * inet_twsk_bind_unhash - unhash a timewait socket from bind hash
43 * @tw: timewait socket
44 * @hashinfo: hashinfo pointer
46 * unhash a timewait socket from bind hash, if hashed.
47 * bind hash lock must be held by caller.
48 * Returns 1 if caller should call inet_twsk_put() after lock release.
50 int inet_twsk_bind_unhash(struct inet_timewait_sock *tw,
51 struct inet_hashinfo *hashinfo)
53 struct inet_bind_bucket *tb = tw->tw_tb;
58 __hlist_del(&tw->tw_bind_node);
60 inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
62 * We cannot call inet_twsk_put() ourself under lock,
63 * caller must call it for us.
68 /* Must be called with locally disabled BHs. */
69 static void __inet_twsk_kill(struct inet_timewait_sock *tw,
70 struct inet_hashinfo *hashinfo)
72 struct inet_bind_hashbucket *bhead;
74 /* Unlink from established hashes. */
75 spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
78 refcnt = inet_twsk_unhash(tw);
81 /* Disassociate with bind bucket. */
82 bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num,
83 hashinfo->bhash_size)];
85 spin_lock(&bhead->lock);
86 refcnt += inet_twsk_bind_unhash(tw, hashinfo);
87 spin_unlock(&bhead->lock);
89 #ifdef SOCK_REFCNT_DEBUG
90 if (atomic_read(&tw->tw_refcnt) != 1) {
91 printk(KERN_DEBUG "%s timewait_sock %p refcnt=%d\n",
92 tw->tw_prot->name, tw, atomic_read(&tw->tw_refcnt));
101 static noinline void inet_twsk_free(struct inet_timewait_sock *tw)
103 struct module *owner = tw->tw_prot->owner;
104 twsk_destructor((struct sock *)tw);
105 #ifdef SOCK_REFCNT_DEBUG
106 pr_debug("%s timewait_sock %p released\n", tw->tw_prot->name, tw);
108 release_net(twsk_net(tw));
109 kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
113 void inet_twsk_put(struct inet_timewait_sock *tw)
115 if (atomic_dec_and_test(&tw->tw_refcnt))
118 EXPORT_SYMBOL_GPL(inet_twsk_put);
121 * Enter the time wait state. This is called with locally disabled BH.
122 * Essentially we whip up a timewait bucket, copy the relevant info into it
123 * from the SK, and mess with hash chains and list linkage.
125 void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
126 struct inet_hashinfo *hashinfo)
128 const struct inet_sock *inet = inet_sk(sk);
129 const struct inet_connection_sock *icsk = inet_csk(sk);
130 struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
131 spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
132 struct inet_bind_hashbucket *bhead;
133 /* Step 1: Put TW into bind hash. Original socket stays there too.
134 Note, that any socket with inet->num != 0 MUST be bound in
135 binding cache, even if it is closed.
137 bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->inet_num,
138 hashinfo->bhash_size)];
139 spin_lock(&bhead->lock);
140 tw->tw_tb = icsk->icsk_bind_hash;
141 WARN_ON(!icsk->icsk_bind_hash);
142 inet_twsk_add_bind_node(tw, &tw->tw_tb->owners);
143 spin_unlock(&bhead->lock);
148 * Step 2: Hash TW into TIMEWAIT chain.
149 * Should be done before removing sk from established chain
150 * because readers are lockless and search established first.
152 inet_twsk_add_node_rcu(tw, &ehead->twchain);
154 /* Step 3: Remove SK from established hash. */
155 if (__sk_nulls_del_node_init_rcu(sk))
156 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
160 * - We initially set tw_refcnt to 0 in inet_twsk_alloc()
161 * - We add one reference for the bhash link
162 * - We add one reference for the ehash link
163 * - We want this refcnt update done before allowing other
164 * threads to find this tw in ehash chain.
166 atomic_add(1 + 1 + 1, &tw->tw_refcnt);
170 EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
172 struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state)
174 struct inet_timewait_sock *tw =
175 kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
178 const struct inet_sock *inet = inet_sk(sk);
180 kmemcheck_annotate_bitfield(tw, flags);
182 /* Give us an identity. */
183 tw->tw_daddr = inet->inet_daddr;
184 tw->tw_rcv_saddr = inet->inet_rcv_saddr;
185 tw->tw_bound_dev_if = sk->sk_bound_dev_if;
186 tw->tw_tos = inet->tos;
187 tw->tw_num = inet->inet_num;
188 tw->tw_state = TCP_TIME_WAIT;
189 tw->tw_substate = state;
190 tw->tw_sport = inet->inet_sport;
191 tw->tw_dport = inet->inet_dport;
192 tw->tw_family = sk->sk_family;
193 tw->tw_reuse = sk->sk_reuse;
194 tw->tw_hash = sk->sk_hash;
196 tw->tw_transparent = inet->transparent;
197 tw->tw_prot = sk->sk_prot_creator;
198 twsk_net_set(tw, hold_net(sock_net(sk)));
200 * Because we use RCU lookups, we should not set tw_refcnt
201 * to a non null value before everything is setup for this
204 atomic_set(&tw->tw_refcnt, 0);
205 inet_twsk_dead_node_init(tw);
206 __module_get(tw->tw_prot->owner);
211 EXPORT_SYMBOL_GPL(inet_twsk_alloc);
213 /* Returns non-zero if quota exceeded. */
214 static int inet_twdr_do_twkill_work(struct inet_timewait_death_row *twdr,
217 struct inet_timewait_sock *tw;
218 struct hlist_node *node;
222 /* NOTE: compare this to previous version where lock
223 * was released after detaching chain. It was racy,
224 * because tw buckets are scheduled in not serialized context
225 * in 2.3 (with netfilter), and with softnet it is common, because
226 * soft irqs are not sequenced.
231 inet_twsk_for_each_inmate(tw, node, &twdr->cells[slot]) {
232 __inet_twsk_del_dead_node(tw);
233 spin_unlock(&twdr->death_lock);
234 __inet_twsk_kill(tw, twdr->hashinfo);
236 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITED);
240 spin_lock(&twdr->death_lock);
241 if (killed > INET_TWDR_TWKILL_QUOTA) {
246 /* While we dropped twdr->death_lock, another cpu may have
247 * killed off the next TW bucket in the list, therefore
248 * do a fresh re-read of the hlist head node with the
249 * lock reacquired. We still use the hlist traversal
250 * macro in order to get the prefetches.
255 twdr->tw_count -= killed;
256 #ifndef CONFIG_NET_NS
257 NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITED, killed);
262 void inet_twdr_hangman(unsigned long data)
264 struct inet_timewait_death_row *twdr;
265 int unsigned need_timer;
267 twdr = (struct inet_timewait_death_row *)data;
268 spin_lock(&twdr->death_lock);
270 if (twdr->tw_count == 0)
274 if (inet_twdr_do_twkill_work(twdr, twdr->slot)) {
275 twdr->thread_slots |= (1 << twdr->slot);
276 schedule_work(&twdr->twkill_work);
279 /* We purged the entire slot, anything left? */
282 twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1));
285 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
287 spin_unlock(&twdr->death_lock);
289 EXPORT_SYMBOL_GPL(inet_twdr_hangman);
291 void inet_twdr_twkill_work(struct work_struct *work)
293 struct inet_timewait_death_row *twdr =
294 container_of(work, struct inet_timewait_death_row, twkill_work);
297 BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) >
298 (sizeof(twdr->thread_slots) * 8));
300 while (twdr->thread_slots) {
301 spin_lock_bh(&twdr->death_lock);
302 for (i = 0; i < INET_TWDR_TWKILL_SLOTS; i++) {
303 if (!(twdr->thread_slots & (1 << i)))
306 while (inet_twdr_do_twkill_work(twdr, i) != 0) {
307 if (need_resched()) {
308 spin_unlock_bh(&twdr->death_lock);
310 spin_lock_bh(&twdr->death_lock);
314 twdr->thread_slots &= ~(1 << i);
316 spin_unlock_bh(&twdr->death_lock);
319 EXPORT_SYMBOL_GPL(inet_twdr_twkill_work);
321 /* These are always called from BH context. See callers in
322 * tcp_input.c to verify this.
325 /* This is for handling early-kills of TIME_WAIT sockets. */
326 void inet_twsk_deschedule(struct inet_timewait_sock *tw,
327 struct inet_timewait_death_row *twdr)
329 spin_lock(&twdr->death_lock);
330 if (inet_twsk_del_dead_node(tw)) {
332 if (--twdr->tw_count == 0)
333 del_timer(&twdr->tw_timer);
335 spin_unlock(&twdr->death_lock);
336 __inet_twsk_kill(tw, twdr->hashinfo);
338 EXPORT_SYMBOL(inet_twsk_deschedule);
340 void inet_twsk_schedule(struct inet_timewait_sock *tw,
341 struct inet_timewait_death_row *twdr,
342 const int timeo, const int timewait_len)
344 struct hlist_head *list;
347 /* timeout := RTO * 3.5
349 * 3.5 = 1+2+0.5 to wait for two retransmits.
351 * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
352 * our ACK acking that FIN can be lost. If N subsequent retransmitted
353 * FINs (or previous seqments) are lost (probability of such event
354 * is p^(N+1), where p is probability to lose single packet and
355 * time to detect the loss is about RTO*(2^N - 1) with exponential
356 * backoff). Normal timewait length is calculated so, that we
357 * waited at least for one retransmitted FIN (maximal RTO is 120sec).
358 * [ BTW Linux. following BSD, violates this requirement waiting
359 * only for 60sec, we should wait at least for 240 secs.
360 * Well, 240 consumes too much of resources 8)
362 * This interval is not reduced to catch old duplicate and
363 * responces to our wandering segments living for two MSLs.
364 * However, if we use PAWS to detect
365 * old duplicates, we can reduce the interval to bounds required
366 * by RTO, rather than MSL. So, if peer understands PAWS, we
367 * kill tw bucket after 3.5*RTO (it is important that this number
368 * is greater than TS tick!) and detect old duplicates with help
371 slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK;
373 spin_lock(&twdr->death_lock);
375 /* Unlink it, if it was scheduled */
376 if (inet_twsk_del_dead_node(tw))
379 atomic_inc(&tw->tw_refcnt);
381 if (slot >= INET_TWDR_RECYCLE_SLOTS) {
382 /* Schedule to slow timer */
383 if (timeo >= timewait_len) {
384 slot = INET_TWDR_TWKILL_SLOTS - 1;
386 slot = DIV_ROUND_UP(timeo, twdr->period);
387 if (slot >= INET_TWDR_TWKILL_SLOTS)
388 slot = INET_TWDR_TWKILL_SLOTS - 1;
390 tw->tw_ttd = jiffies + timeo;
391 slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
392 list = &twdr->cells[slot];
394 tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK);
396 if (twdr->twcal_hand < 0) {
397 twdr->twcal_hand = 0;
398 twdr->twcal_jiffie = jiffies;
399 twdr->twcal_timer.expires = twdr->twcal_jiffie +
400 (slot << INET_TWDR_RECYCLE_TICK);
401 add_timer(&twdr->twcal_timer);
403 if (time_after(twdr->twcal_timer.expires,
404 jiffies + (slot << INET_TWDR_RECYCLE_TICK)))
405 mod_timer(&twdr->twcal_timer,
406 jiffies + (slot << INET_TWDR_RECYCLE_TICK));
407 slot = (twdr->twcal_hand + slot) & (INET_TWDR_RECYCLE_SLOTS - 1);
409 list = &twdr->twcal_row[slot];
412 hlist_add_head(&tw->tw_death_node, list);
414 if (twdr->tw_count++ == 0)
415 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
416 spin_unlock(&twdr->death_lock);
418 EXPORT_SYMBOL_GPL(inet_twsk_schedule);
420 void inet_twdr_twcal_tick(unsigned long data)
422 struct inet_timewait_death_row *twdr;
425 unsigned long now = jiffies;
429 twdr = (struct inet_timewait_death_row *)data;
431 spin_lock(&twdr->death_lock);
432 if (twdr->twcal_hand < 0)
435 slot = twdr->twcal_hand;
436 j = twdr->twcal_jiffie;
438 for (n = 0; n < INET_TWDR_RECYCLE_SLOTS; n++) {
439 if (time_before_eq(j, now)) {
440 struct hlist_node *node, *safe;
441 struct inet_timewait_sock *tw;
443 inet_twsk_for_each_inmate_safe(tw, node, safe,
444 &twdr->twcal_row[slot]) {
445 __inet_twsk_del_dead_node(tw);
446 __inet_twsk_kill(tw, twdr->hashinfo);
448 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED);
456 twdr->twcal_jiffie = j;
457 twdr->twcal_hand = slot;
460 if (!hlist_empty(&twdr->twcal_row[slot])) {
461 mod_timer(&twdr->twcal_timer, j);
465 j += 1 << INET_TWDR_RECYCLE_TICK;
466 slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1);
468 twdr->twcal_hand = -1;
471 if ((twdr->tw_count -= killed) == 0)
472 del_timer(&twdr->tw_timer);
473 #ifndef CONFIG_NET_NS
474 NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITKILLED, killed);
476 spin_unlock(&twdr->death_lock);
478 EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);
480 void inet_twsk_purge(struct inet_hashinfo *hashinfo,
481 struct inet_timewait_death_row *twdr, int family)
483 struct inet_timewait_sock *tw;
485 struct hlist_nulls_node *node;
488 for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
489 struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
493 sk_nulls_for_each_rcu(sk, node, &head->twchain) {
495 if ((tw->tw_family != family) ||
496 atomic_read(&twsk_net(tw)->count))
499 if (unlikely(!atomic_inc_not_zero(&tw->tw_refcnt)))
502 if (unlikely((tw->tw_family != family) ||
503 atomic_read(&twsk_net(tw)->count))) {
510 inet_twsk_deschedule(tw, twdr);
515 /* If the nulls value we got at the end of this lookup is
516 * not the expected one, we must restart lookup.
517 * We probably met an item that was moved to another chain.
519 if (get_nulls_value(node) != slot)
524 EXPORT_SYMBOL_GPL(inet_twsk_purge);