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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 14 | #include <linux/module.h> |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 15 | #include <net/inet_hashtables.h> |
| 16 | #include <net/inet_timewait_sock.h> |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 17 | #include <net/ip.h> |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 18 | |
Eric Dumazet | 13475a3 | 2009-12-02 22:31:19 +0000 | [diff] [blame] | 19 | |
Eric Dumazet | 2a8875e | 2009-12-08 20:19:53 -0800 | [diff] [blame] | 20 | /** |
| 21 | * inet_twsk_unhash - unhash a timewait socket from established hash |
| 22 | * @tw: timewait socket |
| 23 | * |
| 24 | * unhash a timewait socket from established hash, if hashed. |
| 25 | * ehash lock must be held by caller. |
| 26 | * Returns 1 if caller should call inet_twsk_put() after lock release. |
Eric Dumazet | 13475a3 | 2009-12-02 22:31:19 +0000 | [diff] [blame] | 27 | */ |
| 28 | int inet_twsk_unhash(struct inet_timewait_sock *tw) |
| 29 | { |
| 30 | if (hlist_nulls_unhashed(&tw->tw_node)) |
| 31 | return 0; |
| 32 | |
| 33 | hlist_nulls_del_rcu(&tw->tw_node); |
| 34 | sk_nulls_node_init(&tw->tw_node); |
Eric Dumazet | 2a8875e | 2009-12-08 20:19:53 -0800 | [diff] [blame] | 35 | /* |
| 36 | * We cannot call inet_twsk_put() ourself under lock, |
| 37 | * caller must call it for us. |
| 38 | */ |
Eric Dumazet | 13475a3 | 2009-12-02 22:31:19 +0000 | [diff] [blame] | 39 | return 1; |
| 40 | } |
| 41 | |
Eric Dumazet | 2a8875e | 2009-12-08 20:19:53 -0800 | [diff] [blame] | 42 | /** |
| 43 | * inet_twsk_bind_unhash - unhash a timewait socket from bind hash |
| 44 | * @tw: timewait socket |
| 45 | * @hashinfo: hashinfo pointer |
| 46 | * |
| 47 | * unhash a timewait socket from bind hash, if hashed. |
| 48 | * bind hash lock must be held by caller. |
| 49 | * Returns 1 if caller should call inet_twsk_put() after lock release. |
Eric Dumazet | 3cdaeda | 2009-12-04 03:47:42 +0000 | [diff] [blame] | 50 | */ |
| 51 | int inet_twsk_bind_unhash(struct inet_timewait_sock *tw, |
| 52 | struct inet_hashinfo *hashinfo) |
| 53 | { |
| 54 | struct inet_bind_bucket *tb = tw->tw_tb; |
| 55 | |
| 56 | if (!tb) |
| 57 | return 0; |
| 58 | |
| 59 | __hlist_del(&tw->tw_bind_node); |
| 60 | tw->tw_tb = NULL; |
| 61 | inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb); |
Eric Dumazet | 2a8875e | 2009-12-08 20:19:53 -0800 | [diff] [blame] | 62 | /* |
| 63 | * We cannot call inet_twsk_put() ourself under lock, |
| 64 | * caller must call it for us. |
| 65 | */ |
Eric Dumazet | 3cdaeda | 2009-12-04 03:47:42 +0000 | [diff] [blame] | 66 | return 1; |
| 67 | } |
| 68 | |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 69 | /* Must be called with locally disabled BHs. */ |
Eric Dumazet | 789f558 | 2015-04-12 18:51:09 -0700 | [diff] [blame] | 70 | static void inet_twsk_kill(struct inet_timewait_sock *tw) |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 71 | { |
Eric Dumazet | 789f558 | 2015-04-12 18:51:09 -0700 | [diff] [blame] | 72 | struct inet_hashinfo *hashinfo = tw->tw_dr->hashinfo; |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 73 | struct inet_bind_hashbucket *bhead; |
Eric Dumazet | 13475a3 | 2009-12-02 22:31:19 +0000 | [diff] [blame] | 74 | int refcnt; |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 75 | /* Unlink from established hashes. */ |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 76 | spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 77 | |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 78 | spin_lock(lock); |
Eric Dumazet | 13475a3 | 2009-12-02 22:31:19 +0000 | [diff] [blame] | 79 | refcnt = inet_twsk_unhash(tw); |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 80 | spin_unlock(lock); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 81 | |
| 82 | /* Disassociate with bind bucket. */ |
Pavel Emelyanov | 7f635ab | 2008-06-16 17:12:49 -0700 | [diff] [blame] | 83 | bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num, |
| 84 | hashinfo->bhash_size)]; |
Eric Dumazet | 3cdaeda | 2009-12-04 03:47:42 +0000 | [diff] [blame] | 85 | |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 86 | spin_lock(&bhead->lock); |
Eric Dumazet | 3cdaeda | 2009-12-04 03:47:42 +0000 | [diff] [blame] | 87 | refcnt += inet_twsk_bind_unhash(tw, hashinfo); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 88 | spin_unlock(&bhead->lock); |
Eric Dumazet | 3cdaeda | 2009-12-04 03:47:42 +0000 | [diff] [blame] | 89 | |
Eric Dumazet | 05dbc7b | 2013-10-03 00:22:02 -0700 | [diff] [blame] | 90 | BUG_ON(refcnt >= atomic_read(&tw->tw_refcnt)); |
| 91 | atomic_sub(refcnt, &tw->tw_refcnt); |
Eric Dumazet | 789f558 | 2015-04-12 18:51:09 -0700 | [diff] [blame] | 92 | atomic_dec(&tw->tw_dr->tw_count); |
| 93 | inet_twsk_put(tw); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Eric Dumazet | 05dbc7b | 2013-10-03 00:22:02 -0700 | [diff] [blame] | 96 | void inet_twsk_free(struct inet_timewait_sock *tw) |
Arnaldo Carvalho de Melo | 4dbc8ef | 2009-05-06 16:50:52 -0700 | [diff] [blame] | 97 | { |
| 98 | struct module *owner = tw->tw_prot->owner; |
| 99 | twsk_destructor((struct sock *)tw); |
| 100 | #ifdef SOCK_REFCNT_DEBUG |
| 101 | pr_debug("%s timewait_sock %p released\n", tw->tw_prot->name, tw); |
| 102 | #endif |
Arnaldo Carvalho de Melo | 4dbc8ef | 2009-05-06 16:50:52 -0700 | [diff] [blame] | 103 | kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw); |
| 104 | module_put(owner); |
| 105 | } |
| 106 | |
Pavel Emelyanov | 7054fb9 | 2007-12-20 15:32:54 -0800 | [diff] [blame] | 107 | void inet_twsk_put(struct inet_timewait_sock *tw) |
| 108 | { |
Arnaldo Carvalho de Melo | 4dbc8ef | 2009-05-06 16:50:52 -0700 | [diff] [blame] | 109 | if (atomic_dec_and_test(&tw->tw_refcnt)) |
| 110 | inet_twsk_free(tw); |
Pavel Emelyanov | 7054fb9 | 2007-12-20 15:32:54 -0800 | [diff] [blame] | 111 | } |
| 112 | EXPORT_SYMBOL_GPL(inet_twsk_put); |
| 113 | |
Eric Dumazet | 05dbc7b | 2013-10-03 00:22:02 -0700 | [diff] [blame] | 114 | static void inet_twsk_add_node_rcu(struct inet_timewait_sock *tw, |
| 115 | struct hlist_nulls_head *list) |
| 116 | { |
| 117 | hlist_nulls_add_head_rcu(&tw->tw_node, list); |
| 118 | } |
| 119 | |
| 120 | static void inet_twsk_add_bind_node(struct inet_timewait_sock *tw, |
| 121 | struct hlist_head *list) |
| 122 | { |
| 123 | hlist_add_head(&tw->tw_bind_node, list); |
| 124 | } |
| 125 | |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 126 | /* |
| 127 | * Enter the time wait state. This is called with locally disabled BH. |
| 128 | * Essentially we whip up a timewait bucket, copy the relevant info into it |
| 129 | * from the SK, and mess with hash chains and list linkage. |
| 130 | */ |
| 131 | void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk, |
| 132 | struct inet_hashinfo *hashinfo) |
| 133 | { |
| 134 | const struct inet_sock *inet = inet_sk(sk); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 135 | const struct inet_connection_sock *icsk = inet_csk(sk); |
Eric Dumazet | 81c3d54 | 2005-10-03 14:13:38 -0700 | [diff] [blame] | 136 | struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash); |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 137 | spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 138 | struct inet_bind_hashbucket *bhead; |
| 139 | /* Step 1: Put TW into bind hash. Original socket stays there too. |
| 140 | Note, that any socket with inet->num != 0 MUST be bound in |
| 141 | binding cache, even if it is closed. |
| 142 | */ |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 143 | bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->inet_num, |
Pavel Emelyanov | 7f635ab | 2008-06-16 17:12:49 -0700 | [diff] [blame] | 144 | hashinfo->bhash_size)]; |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 145 | spin_lock(&bhead->lock); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 146 | tw->tw_tb = icsk->icsk_bind_hash; |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 147 | WARN_ON(!icsk->icsk_bind_hash); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 148 | inet_twsk_add_bind_node(tw, &tw->tw_tb->owners); |
| 149 | spin_unlock(&bhead->lock); |
| 150 | |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 151 | spin_lock(lock); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 152 | |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 153 | /* |
Eric Dumazet | 05dbc7b | 2013-10-03 00:22:02 -0700 | [diff] [blame] | 154 | * Step 2: Hash TW into tcp ehash chain. |
| 155 | * Notes : |
| 156 | * - tw_refcnt is set to 3 because : |
| 157 | * - We have one reference from bhash chain. |
| 158 | * - We have one reference from ehash chain. |
| 159 | * We can use atomic_set() because prior spin_lock()/spin_unlock() |
| 160 | * committed into memory all tw fields. |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 161 | */ |
Eric Dumazet | 05dbc7b | 2013-10-03 00:22:02 -0700 | [diff] [blame] | 162 | atomic_set(&tw->tw_refcnt, 1 + 1 + 1); |
| 163 | inet_twsk_add_node_rcu(tw, &ehead->chain); |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 164 | |
Eric Dumazet | 05dbc7b | 2013-10-03 00:22:02 -0700 | [diff] [blame] | 165 | /* Step 3: Remove SK from hash chain */ |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 166 | if (__sk_nulls_del_node_init_rcu(sk)) |
| 167 | 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] | 168 | |
Eric Dumazet | 9db66bd | 2008-11-20 20:39:09 -0800 | [diff] [blame] | 169 | spin_unlock(lock); |
Arnaldo Carvalho de Melo | e48c414 | 2005-08-09 20:09:46 -0700 | [diff] [blame] | 170 | } |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 171 | EXPORT_SYMBOL_GPL(__inet_twsk_hashdance); |
| 172 | |
Eric Dumazet | 216f8bb | 2015-05-12 06:22:56 -0700 | [diff] [blame] | 173 | static void tw_timer_handler(unsigned long data) |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 174 | { |
Eric Dumazet | 789f558 | 2015-04-12 18:51:09 -0700 | [diff] [blame] | 175 | struct inet_timewait_sock *tw = (struct inet_timewait_sock *)data; |
| 176 | |
| 177 | if (tw->tw_kill) |
| 178 | NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED); |
| 179 | else |
| 180 | NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITED); |
| 181 | inet_twsk_kill(tw); |
| 182 | } |
| 183 | |
| 184 | struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, |
| 185 | struct inet_timewait_death_row *dr, |
| 186 | const int state) |
| 187 | { |
| 188 | struct inet_timewait_sock *tw; |
| 189 | |
| 190 | if (atomic_read(&dr->tw_count) >= dr->sysctl_max_tw_buckets) |
| 191 | return NULL; |
| 192 | |
| 193 | tw = kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab, |
| 194 | GFP_ATOMIC); |
Ian Morris | 00db412 | 2015-04-03 09:17:27 +0100 | [diff] [blame] | 195 | if (tw) { |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 196 | const struct inet_sock *inet = inet_sk(sk); |
| 197 | |
Vegard Nossum | 9e337b0 | 2008-10-18 17:37:51 +0200 | [diff] [blame] | 198 | kmemcheck_annotate_bitfield(tw, flags); |
| 199 | |
Eric Dumazet | 789f558 | 2015-04-12 18:51:09 -0700 | [diff] [blame] | 200 | tw->tw_dr = dr; |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 201 | /* Give us an identity. */ |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 202 | tw->tw_daddr = inet->inet_daddr; |
| 203 | tw->tw_rcv_saddr = inet->inet_rcv_saddr; |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 204 | tw->tw_bound_dev_if = sk->sk_bound_dev_if; |
Eric Dumazet | 66b13d9 | 2011-10-24 03:06:21 -0400 | [diff] [blame] | 205 | tw->tw_tos = inet->tos; |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 206 | tw->tw_num = inet->inet_num; |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 207 | tw->tw_state = TCP_TIME_WAIT; |
| 208 | tw->tw_substate = state; |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 209 | tw->tw_sport = inet->inet_sport; |
| 210 | tw->tw_dport = inet->inet_dport; |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 211 | tw->tw_family = sk->sk_family; |
| 212 | tw->tw_reuse = sk->sk_reuse; |
Eric Dumazet | 81c3d54 | 2005-10-03 14:13:38 -0700 | [diff] [blame] | 213 | tw->tw_hash = sk->sk_hash; |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 214 | tw->tw_ipv6only = 0; |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 215 | tw->tw_transparent = inet->transparent; |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 216 | tw->tw_prot = sk->sk_prot_creator; |
Eric Dumazet | 33cf7c9 | 2015-03-11 18:53:14 -0700 | [diff] [blame] | 217 | atomic64_set(&tw->tw_cookie, atomic64_read(&sk->sk_cookie)); |
Eric W. Biederman | efd7ef1 | 2015-03-11 23:04:08 -0500 | [diff] [blame] | 218 | twsk_net_set(tw, sock_net(sk)); |
Eric Dumazet | 789f558 | 2015-04-12 18:51:09 -0700 | [diff] [blame] | 219 | setup_timer(&tw->tw_timer, tw_timer_handler, (unsigned long)tw); |
Eric Dumazet | 47e1c32 | 2009-12-03 00:49:01 +0000 | [diff] [blame] | 220 | /* |
| 221 | * Because we use RCU lookups, we should not set tw_refcnt |
| 222 | * to a non null value before everything is setup for this |
| 223 | * timewait socket. |
| 224 | */ |
| 225 | atomic_set(&tw->tw_refcnt, 0); |
Eric Dumazet | 789f558 | 2015-04-12 18:51:09 -0700 | [diff] [blame] | 226 | |
Arnaldo Carvalho de Melo | eeb2b85 | 2005-10-10 21:25:23 -0700 | [diff] [blame] | 227 | __module_get(tw->tw_prot->owner); |
Arnaldo Carvalho de Melo | c676270 | 2005-08-09 20:09:59 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return tw; |
| 231 | } |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 232 | EXPORT_SYMBOL_GPL(inet_twsk_alloc); |
| 233 | |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 234 | /* These are always called from BH context. See callers in |
| 235 | * tcp_input.c to verify this. |
| 236 | */ |
| 237 | |
| 238 | /* This is for handling early-kills of TIME_WAIT sockets. */ |
Eric Dumazet | 789f558 | 2015-04-12 18:51:09 -0700 | [diff] [blame] | 239 | void inet_twsk_deschedule(struct inet_timewait_sock *tw) |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 240 | { |
Eric Dumazet | 789f558 | 2015-04-12 18:51:09 -0700 | [diff] [blame] | 241 | if (del_timer_sync(&tw->tw_timer)) |
| 242 | inet_twsk_kill(tw); |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 243 | } |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 244 | EXPORT_SYMBOL(inet_twsk_deschedule); |
| 245 | |
Eric Dumazet | 789f558 | 2015-04-12 18:51:09 -0700 | [diff] [blame] | 246 | void inet_twsk_schedule(struct inet_timewait_sock *tw, const int timeo) |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 247 | { |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 248 | /* timeout := RTO * 3.5 |
| 249 | * |
| 250 | * 3.5 = 1+2+0.5 to wait for two retransmits. |
| 251 | * |
| 252 | * RATIONALE: if FIN arrived and we entered TIME-WAIT state, |
| 253 | * our ACK acking that FIN can be lost. If N subsequent retransmitted |
| 254 | * FINs (or previous seqments) are lost (probability of such event |
| 255 | * is p^(N+1), where p is probability to lose single packet and |
| 256 | * time to detect the loss is about RTO*(2^N - 1) with exponential |
| 257 | * backoff). Normal timewait length is calculated so, that we |
| 258 | * waited at least for one retransmitted FIN (maximal RTO is 120sec). |
| 259 | * [ BTW Linux. following BSD, violates this requirement waiting |
| 260 | * only for 60sec, we should wait at least for 240 secs. |
| 261 | * Well, 240 consumes too much of resources 8) |
| 262 | * ] |
| 263 | * This interval is not reduced to catch old duplicate and |
| 264 | * responces to our wandering segments living for two MSLs. |
| 265 | * However, if we use PAWS to detect |
| 266 | * old duplicates, we can reduce the interval to bounds required |
| 267 | * by RTO, rather than MSL. So, if peer understands PAWS, we |
| 268 | * kill tw bucket after 3.5*RTO (it is important that this number |
| 269 | * is greater than TS tick!) and detect old duplicates with help |
| 270 | * of PAWS. |
| 271 | */ |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 272 | |
Eric Dumazet | 789f558 | 2015-04-12 18:51:09 -0700 | [diff] [blame] | 273 | tw->tw_kill = timeo <= 4*HZ; |
| 274 | if (!mod_timer_pinned(&tw->tw_timer, jiffies + timeo)) { |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 275 | atomic_inc(&tw->tw_refcnt); |
Eric Dumazet | 789f558 | 2015-04-12 18:51:09 -0700 | [diff] [blame] | 276 | atomic_inc(&tw->tw_dr->tw_count); |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 277 | } |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 278 | } |
Arnaldo Carvalho de Melo | 696ab2d | 2005-08-09 20:45:03 -0700 | [diff] [blame] | 279 | EXPORT_SYMBOL_GPL(inet_twsk_schedule); |
| 280 | |
Eric W. Biederman | b099ce2 | 2009-12-03 02:29:09 +0000 | [diff] [blame] | 281 | void inet_twsk_purge(struct inet_hashinfo *hashinfo, |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 282 | struct inet_timewait_death_row *twdr, int family) |
| 283 | { |
| 284 | struct inet_timewait_sock *tw; |
| 285 | struct sock *sk; |
Eric Dumazet | 3ab5aee | 2008-11-16 19:40:17 -0800 | [diff] [blame] | 286 | struct hlist_nulls_node *node; |
Eric W. Biederman | 575f4cd | 2009-12-03 02:29:08 +0000 | [diff] [blame] | 287 | unsigned int slot; |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 288 | |
Eric W. Biederman | 575f4cd | 2009-12-03 02:29:08 +0000 | [diff] [blame] | 289 | for (slot = 0; slot <= hashinfo->ehash_mask; slot++) { |
| 290 | struct inet_ehash_bucket *head = &hashinfo->ehash[slot]; |
| 291 | restart_rcu: |
Eric Dumazet | 738e6d3 | 2015-03-18 17:40:51 -0700 | [diff] [blame] | 292 | cond_resched(); |
Eric W. Biederman | 575f4cd | 2009-12-03 02:29:08 +0000 | [diff] [blame] | 293 | rcu_read_lock(); |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 294 | restart: |
Eric Dumazet | 05dbc7b | 2013-10-03 00:22:02 -0700 | [diff] [blame] | 295 | sk_nulls_for_each_rcu(sk, node, &head->chain) { |
| 296 | if (sk->sk_state != TCP_TIME_WAIT) |
| 297 | continue; |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 298 | tw = inet_twsk(sk); |
Eric W. Biederman | b099ce2 | 2009-12-03 02:29:09 +0000 | [diff] [blame] | 299 | if ((tw->tw_family != family) || |
| 300 | atomic_read(&twsk_net(tw)->count)) |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 301 | continue; |
| 302 | |
Eric W. Biederman | 575f4cd | 2009-12-03 02:29:08 +0000 | [diff] [blame] | 303 | if (unlikely(!atomic_inc_not_zero(&tw->tw_refcnt))) |
| 304 | continue; |
| 305 | |
Eric W. Biederman | b099ce2 | 2009-12-03 02:29:09 +0000 | [diff] [blame] | 306 | if (unlikely((tw->tw_family != family) || |
| 307 | atomic_read(&twsk_net(tw)->count))) { |
Eric W. Biederman | 575f4cd | 2009-12-03 02:29:08 +0000 | [diff] [blame] | 308 | inet_twsk_put(tw); |
| 309 | goto restart; |
| 310 | } |
| 311 | |
| 312 | rcu_read_unlock(); |
Eric Dumazet | 91035f0 | 2011-02-18 22:35:56 +0000 | [diff] [blame] | 313 | local_bh_disable(); |
Eric Dumazet | 789f558 | 2015-04-12 18:51:09 -0700 | [diff] [blame] | 314 | inet_twsk_deschedule(tw); |
Eric Dumazet | 91035f0 | 2011-02-18 22:35:56 +0000 | [diff] [blame] | 315 | local_bh_enable(); |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 316 | inet_twsk_put(tw); |
Eric W. Biederman | 575f4cd | 2009-12-03 02:29:08 +0000 | [diff] [blame] | 317 | goto restart_rcu; |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 318 | } |
Eric W. Biederman | 575f4cd | 2009-12-03 02:29:08 +0000 | [diff] [blame] | 319 | /* If the nulls value we got at the end of this lookup is |
| 320 | * not the expected one, we must restart lookup. |
| 321 | * We probably met an item that was moved to another chain. |
| 322 | */ |
| 323 | if (get_nulls_value(node) != slot) |
| 324 | goto restart; |
| 325 | rcu_read_unlock(); |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 326 | } |
Daniel Lezcano | d315492 | 2008-09-08 13:17:27 -0700 | [diff] [blame] | 327 | } |
| 328 | EXPORT_SYMBOL_GPL(inet_twsk_purge); |