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 <net/inet_hashtables.h>
14 #include <net/inet_timewait_sock.h>
19 * unhash a timewait socket from established hash
20 * lock must be hold by caller
22 int inet_twsk_unhash(struct inet_timewait_sock *tw)
24 if (hlist_nulls_unhashed(&tw->tw_node))
27 hlist_nulls_del_rcu(&tw->tw_node);
28 sk_nulls_node_init(&tw->tw_node);
32 /* Must be called with locally disabled BHs. */
33 static void __inet_twsk_kill(struct inet_timewait_sock *tw,
34 struct inet_hashinfo *hashinfo)
36 struct inet_bind_hashbucket *bhead;
37 struct inet_bind_bucket *tb;
39 /* Unlink from established hashes. */
40 spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
43 refcnt = inet_twsk_unhash(tw);
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);
52 __hlist_del(&tw->tw_bind_node);
54 inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
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));
70 static noinline void inet_twsk_free(struct inet_timewait_sock *tw)
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);
77 release_net(twsk_net(tw));
78 kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
82 void inet_twsk_put(struct inet_timewait_sock *tw)
84 if (atomic_dec_and_test(&tw->tw_refcnt))
87 EXPORT_SYMBOL_GPL(inet_twsk_put);
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.
94 void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
95 struct inet_hashinfo *hashinfo)
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.
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 atomic_inc(&tw->tw_refcnt);
113 spin_unlock(&bhead->lock);
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.
122 atomic_inc(&tw->tw_refcnt);
123 inet_twsk_add_node_rcu(tw, &ehead->twchain);
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);
132 EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
134 struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state)
136 struct inet_timewait_sock *tw =
137 kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
140 const struct inet_sock *inet = inet_sk(sk);
142 kmemcheck_annotate_bitfield(tw, flags);
144 /* Give us an identity. */
145 tw->tw_daddr = inet->inet_daddr;
146 tw->tw_rcv_saddr = inet->inet_rcv_saddr;
147 tw->tw_bound_dev_if = sk->sk_bound_dev_if;
148 tw->tw_num = inet->inet_num;
149 tw->tw_state = TCP_TIME_WAIT;
150 tw->tw_substate = state;
151 tw->tw_sport = inet->inet_sport;
152 tw->tw_dport = inet->inet_dport;
153 tw->tw_family = sk->sk_family;
154 tw->tw_reuse = sk->sk_reuse;
155 tw->tw_hash = sk->sk_hash;
157 tw->tw_transparent = inet->transparent;
158 tw->tw_prot = sk->sk_prot_creator;
159 twsk_net_set(tw, hold_net(sock_net(sk)));
160 atomic_set(&tw->tw_refcnt, 1);
161 inet_twsk_dead_node_init(tw);
162 __module_get(tw->tw_prot->owner);
168 EXPORT_SYMBOL_GPL(inet_twsk_alloc);
170 /* Returns non-zero if quota exceeded. */
171 static int inet_twdr_do_twkill_work(struct inet_timewait_death_row *twdr,
174 struct inet_timewait_sock *tw;
175 struct hlist_node *node;
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.
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);
193 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITED);
197 spin_lock(&twdr->death_lock);
198 if (killed > INET_TWDR_TWKILL_QUOTA) {
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.
212 twdr->tw_count -= killed;
213 #ifndef CONFIG_NET_NS
214 NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITED, killed);
219 void inet_twdr_hangman(unsigned long data)
221 struct inet_timewait_death_row *twdr;
222 int unsigned need_timer;
224 twdr = (struct inet_timewait_death_row *)data;
225 spin_lock(&twdr->death_lock);
227 if (twdr->tw_count == 0)
231 if (inet_twdr_do_twkill_work(twdr, twdr->slot)) {
232 twdr->thread_slots |= (1 << twdr->slot);
233 schedule_work(&twdr->twkill_work);
236 /* We purged the entire slot, anything left? */
239 twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1));
242 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
244 spin_unlock(&twdr->death_lock);
247 EXPORT_SYMBOL_GPL(inet_twdr_hangman);
249 void inet_twdr_twkill_work(struct work_struct *work)
251 struct inet_timewait_death_row *twdr =
252 container_of(work, struct inet_timewait_death_row, twkill_work);
255 BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) >
256 (sizeof(twdr->thread_slots) * 8));
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)))
264 while (inet_twdr_do_twkill_work(twdr, i) != 0) {
265 if (need_resched()) {
266 spin_unlock_bh(&twdr->death_lock);
268 spin_lock_bh(&twdr->death_lock);
272 twdr->thread_slots &= ~(1 << i);
274 spin_unlock_bh(&twdr->death_lock);
278 EXPORT_SYMBOL_GPL(inet_twdr_twkill_work);
280 /* These are always called from BH context. See callers in
281 * tcp_input.c to verify this.
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)
288 spin_lock(&twdr->death_lock);
289 if (inet_twsk_del_dead_node(tw)) {
291 if (--twdr->tw_count == 0)
292 del_timer(&twdr->tw_timer);
294 spin_unlock(&twdr->death_lock);
295 __inet_twsk_kill(tw, twdr->hashinfo);
298 EXPORT_SYMBOL(inet_twsk_deschedule);
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)
304 struct hlist_head *list;
307 /* timeout := RTO * 3.5
309 * 3.5 = 1+2+0.5 to wait for two retransmits.
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)
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
331 slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK;
333 spin_lock(&twdr->death_lock);
335 /* Unlink it, if it was scheduled */
336 if (inet_twsk_del_dead_node(tw))
339 atomic_inc(&tw->tw_refcnt);
341 if (slot >= INET_TWDR_RECYCLE_SLOTS) {
342 /* Schedule to slow timer */
343 if (timeo >= timewait_len) {
344 slot = INET_TWDR_TWKILL_SLOTS - 1;
346 slot = DIV_ROUND_UP(timeo, twdr->period);
347 if (slot >= INET_TWDR_TWKILL_SLOTS)
348 slot = INET_TWDR_TWKILL_SLOTS - 1;
350 tw->tw_ttd = jiffies + timeo;
351 slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
352 list = &twdr->cells[slot];
354 tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK);
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);
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);
369 list = &twdr->twcal_row[slot];
372 hlist_add_head(&tw->tw_death_node, list);
374 if (twdr->tw_count++ == 0)
375 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
376 spin_unlock(&twdr->death_lock);
379 EXPORT_SYMBOL_GPL(inet_twsk_schedule);
381 void inet_twdr_twcal_tick(unsigned long data)
383 struct inet_timewait_death_row *twdr;
386 unsigned long now = jiffies;
390 twdr = (struct inet_timewait_death_row *)data;
392 spin_lock(&twdr->death_lock);
393 if (twdr->twcal_hand < 0)
396 slot = twdr->twcal_hand;
397 j = twdr->twcal_jiffie;
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;
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);
409 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED);
417 twdr->twcal_jiffie = j;
418 twdr->twcal_hand = slot;
421 if (!hlist_empty(&twdr->twcal_row[slot])) {
422 mod_timer(&twdr->twcal_timer, j);
426 j += 1 << INET_TWDR_RECYCLE_TICK;
427 slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1);
429 twdr->twcal_hand = -1;
432 if ((twdr->tw_count -= killed) == 0)
433 del_timer(&twdr->tw_timer);
434 #ifndef CONFIG_NET_NS
435 NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITKILLED, killed);
437 spin_unlock(&twdr->death_lock);
440 EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);
442 void inet_twsk_purge(struct inet_hashinfo *hashinfo,
443 struct inet_timewait_death_row *twdr, int family)
445 struct inet_timewait_sock *tw;
447 struct hlist_nulls_node *node;
450 for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
451 struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
455 sk_nulls_for_each_rcu(sk, node, &head->twchain) {
457 if ((tw->tw_family != family) ||
458 atomic_read(&twsk_net(tw)->count))
461 if (unlikely(!atomic_inc_not_zero(&tw->tw_refcnt)))
464 if (unlikely((tw->tw_family != family) ||
465 atomic_read(&twsk_net(tw)->count))) {
471 inet_twsk_deschedule(tw, twdr);
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.
479 if (get_nulls_value(node) != slot)
484 EXPORT_SYMBOL_GPL(inet_twsk_purge);