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