Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 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 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Vegard Nossum | 9e337b0 | 2008-10-18 17:37:51 +0200 | [diff] [blame] | 12 | #include <linux/kmemcheck.h> |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 13 | #include <net/inet_hashtables.h> |
| 14 | #include <net/inet_timewait_sock.h> |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 15 | #include <net/ip.h> |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 16 | |
Eric Dumazet | 13475a3 | 2009-12-02 22:31:19 +0000 | [diff] [blame^] | 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 | |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 32 | /* Must be called with locally disabled BHs. */ |
Adrian Bunk | acd159b | 2007-07-14 19:00:59 -0700 | [diff] [blame] | 33 | static void __inet_twsk_kill(struct inet_timewait_sock *tw, |
| 34 | struct inet_hashinfo *hashinfo) |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 35 | { |
| 36 | struct inet_bind_hashbucket *bhead; |
| 37 | struct inet_bind_bucket *tb; |
Eric Dumazet | 13475a3 | 2009-12-02 22:31:19 +0000 | [diff] [blame^] | 38 | int refcnt; |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 39 | /* Unlink from established hashes. */ |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 40 | spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 41 | |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 42 | spin_lock(lock); |
Eric Dumazet | 13475a3 | 2009-12-02 22:31:19 +0000 | [diff] [blame^] | 43 | refcnt = inet_twsk_unhash(tw); |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 44 | spin_unlock(lock); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 45 | |
| 46 | /* Disassociate with bind bucket. */ |
Pavel Emelyanov | 7f635ab | 2008-06-16 17:12:49 -0700 | [diff] [blame] | 47 | bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num, |
| 48 | hashinfo->bhash_size)]; |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 49 | spin_lock(&bhead->lock); |
| 50 | tb = tw->tw_tb; |
Eric Dumazet | 13475a3 | 2009-12-02 22:31:19 +0000 | [diff] [blame^] | 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 | } |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 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 |
Eric Dumazet | 13475a3 | 2009-12-02 22:31:19 +0000 | [diff] [blame^] | 64 | while (refcnt) { |
| 65 | inet_twsk_put(tw); |
| 66 | refcnt--; |
| 67 | } |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Arnaldo Carvalho de Melo | 4dbc8ef | 2009-05-06 16:50:52 -0700 | [diff] [blame] | 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 | |
Pavel Emelyanov | 7054fb9 | 2007-12-20 15:32:54 -0800 | [diff] [blame] | 82 | void inet_twsk_put(struct inet_timewait_sock *tw) |
| 83 | { |
Arnaldo Carvalho de Melo | 4dbc8ef | 2009-05-06 16:50:52 -0700 | [diff] [blame] | 84 | if (atomic_dec_and_test(&tw->tw_refcnt)) |
| 85 | inet_twsk_free(tw); |
Pavel Emelyanov | 7054fb9 | 2007-12-20 15:32:54 -0800 | [diff] [blame] | 86 | } |
| 87 | EXPORT_SYMBOL_GPL(inet_twsk_put); |
| 88 | |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 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); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 98 | const struct inet_connection_sock *icsk = inet_csk(sk); |
Eric Dumazet | 81c3d54 | 2005-10-03 14:13:38 -0700 | [diff] [blame] | 99 | struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash); |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 100 | spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 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 | */ |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 106 | bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->inet_num, |
Pavel Emelyanov | 7f635ab | 2008-06-16 17:12:49 -0700 | [diff] [blame] | 107 | hashinfo->bhash_size)]; |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 108 | spin_lock(&bhead->lock); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 109 | tw->tw_tb = icsk->icsk_bind_hash; |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 110 | WARN_ON(!icsk->icsk_bind_hash); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 111 | inet_twsk_add_bind_node(tw, &tw->tw_tb->owners); |
Eric Dumazet | 13475a3 | 2009-12-02 22:31:19 +0000 | [diff] [blame^] | 112 | atomic_inc(&tw->tw_refcnt); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 113 | spin_unlock(&bhead->lock); |
| 114 | |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 115 | spin_lock(lock); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 116 | |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 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 | */ |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 122 | atomic_inc(&tw->tw_refcnt); |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 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); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 128 | |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 129 | spin_unlock(lock); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 130 | } |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 131 | |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 132 | EXPORT_SYMBOL_GPL(__inet_twsk_hashdance); |
| 133 | |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 134 | struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state) |
| 135 | { |
Arnaldo Carvalho de Melo | 6d6ee43 | 2005-12-13 23:25:19 -0800 | [diff] [blame] | 136 | struct inet_timewait_sock *tw = |
| 137 | kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab, |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 138 | GFP_ATOMIC); |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 139 | if (tw != NULL) { |
| 140 | const struct inet_sock *inet = inet_sk(sk); |
| 141 | |
Vegard Nossum | 9e337b0 | 2008-10-18 17:37:51 +0200 | [diff] [blame] | 142 | kmemcheck_annotate_bitfield(tw, flags); |
| 143 | |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 144 | /* Give us an identity. */ |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 145 | tw->tw_daddr = inet->inet_daddr; |
| 146 | tw->tw_rcv_saddr = inet->inet_rcv_saddr; |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 147 | tw->tw_bound_dev_if = sk->sk_bound_dev_if; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 148 | tw->tw_num = inet->inet_num; |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 149 | tw->tw_state = TCP_TIME_WAIT; |
| 150 | tw->tw_substate = state; |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 151 | tw->tw_sport = inet->inet_sport; |
| 152 | tw->tw_dport = inet->inet_dport; |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 153 | tw->tw_family = sk->sk_family; |
| 154 | tw->tw_reuse = sk->sk_reuse; |
Eric Dumazet | 81c3d54 | 2005-10-03 14:13:38 -0700 | [diff] [blame] | 155 | tw->tw_hash = sk->sk_hash; |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 156 | tw->tw_ipv6only = 0; |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 157 | tw->tw_transparent = inet->transparent; |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 158 | tw->tw_prot = sk->sk_prot_creator; |
Denis V. Lunev | cd5342d | 2008-04-16 02:00:28 -0700 | [diff] [blame] | 159 | twsk_net_set(tw, hold_net(sock_net(sk))); |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 160 | atomic_set(&tw->tw_refcnt, 1); |
| 161 | inet_twsk_dead_node_init(tw); |
Arnaldo Carvalho de Melo | eeb2b85 | 2005-10-10 21:25:23 -0700 | [diff] [blame] | 162 | __module_get(tw->tw_prot->owner); |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | return tw; |
| 166 | } |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 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); |
Pavel Emelyanov | f2bf415 | 2008-07-16 20:32:25 -0700 | [diff] [blame] | 192 | #ifdef CONFIG_NET_NS |
| 193 | NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITED); |
| 194 | #endif |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 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; |
Pavel Emelyanov | f2bf415 | 2008-07-16 20:32:25 -0700 | [diff] [blame] | 213 | #ifndef CONFIG_NET_NS |
| 214 | NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITED, killed); |
| 215 | #endif |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 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); |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 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; |
Octavian Purdila | 80a1096 | 2009-08-29 00:00:35 -0700 | [diff] [blame] | 239 | twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1)); |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 240 | } |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 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 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 249 | void inet_twdr_twkill_work(struct work_struct *work) |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 250 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 251 | struct inet_timewait_death_row *twdr = |
| 252 | container_of(work, struct inet_timewait_death_row, twkill_work); |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 253 | int i; |
| 254 | |
Pavel Emelyanov | 95c9382 | 2007-12-11 02:12:36 -0800 | [diff] [blame] | 255 | BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) > |
| 256 | (sizeof(twdr->thread_slots) * 8)); |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 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 { |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 346 | slot = DIV_ROUND_UP(timeo, twdr->period); |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 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); |
Pavel Emelyanov | f2bf415 | 2008-07-16 20:32:25 -0700 | [diff] [blame] | 408 | #ifdef CONFIG_NET_NS |
| 409 | NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED); |
| 410 | #endif |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 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); |
Pavel Emelyanov | f2bf415 | 2008-07-16 20:32:25 -0700 | [diff] [blame] | 434 | #ifndef CONFIG_NET_NS |
| 435 | NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITKILLED, killed); |
| 436 | #endif |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 437 | spin_unlock(&twdr->death_lock); |
| 438 | } |
| 439 | |
| 440 | EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick); |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 441 | |
Eric W. Biederman | b099ce2 | 2009-12-03 02:29:09 +0000 | [diff] [blame] | 442 | void inet_twsk_purge(struct inet_hashinfo *hashinfo, |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 443 | struct inet_timewait_death_row *twdr, int family) |
| 444 | { |
| 445 | struct inet_timewait_sock *tw; |
| 446 | struct sock *sk; |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 447 | struct hlist_nulls_node *node; |
Eric W. Biederman | 575f4cd | 2009-12-03 02:29:08 +0000 | [diff] [blame] | 448 | unsigned int slot; |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 449 | |
Eric W. Biederman | 575f4cd | 2009-12-03 02:29:08 +0000 | [diff] [blame] | 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(); |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 454 | restart: |
Eric W. Biederman | 575f4cd | 2009-12-03 02:29:08 +0000 | [diff] [blame] | 455 | sk_nulls_for_each_rcu(sk, node, &head->twchain) { |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 456 | tw = inet_twsk(sk); |
Eric W. Biederman | b099ce2 | 2009-12-03 02:29:09 +0000 | [diff] [blame] | 457 | if ((tw->tw_family != family) || |
| 458 | atomic_read(&twsk_net(tw)->count)) |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 459 | continue; |
| 460 | |
Eric W. Biederman | 575f4cd | 2009-12-03 02:29:08 +0000 | [diff] [blame] | 461 | if (unlikely(!atomic_inc_not_zero(&tw->tw_refcnt))) |
| 462 | continue; |
| 463 | |
Eric W. Biederman | b099ce2 | 2009-12-03 02:29:09 +0000 | [diff] [blame] | 464 | if (unlikely((tw->tw_family != family) || |
| 465 | atomic_read(&twsk_net(tw)->count))) { |
Eric W. Biederman | 575f4cd | 2009-12-03 02:29:08 +0000 | [diff] [blame] | 466 | inet_twsk_put(tw); |
| 467 | goto restart; |
| 468 | } |
| 469 | |
| 470 | rcu_read_unlock(); |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 471 | inet_twsk_deschedule(tw, twdr); |
| 472 | inet_twsk_put(tw); |
Eric W. Biederman | 575f4cd | 2009-12-03 02:29:08 +0000 | [diff] [blame] | 473 | goto restart_rcu; |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 474 | } |
Eric W. Biederman | 575f4cd | 2009-12-03 02:29:08 +0000 | [diff] [blame] | 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(); |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 482 | } |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 483 | } |
| 484 | EXPORT_SYMBOL_GPL(inet_twsk_purge); |