blob: 2ffbd16b79e00279235244c3412046062a86fec5 [file] [log] [blame]
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -07001/*
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ärvinen172589c2007-08-28 15:50:33 -070011#include <linux/kernel.h>
Vegard Nossum9e337b02008-10-18 17:37:51 +020012#include <linux/kmemcheck.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040014#include <linux/module.h>
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070015#include <net/inet_hashtables.h>
16#include <net/inet_timewait_sock.h>
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -070017#include <net/ip.h>
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070018
Eric Dumazet13475a32009-12-02 22:31:19 +000019
Eric Dumazet2a8875e2009-12-08 20:19:53 -080020/**
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 Dumazet13475a32009-12-02 22:31:19 +000027 */
28int 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 Dumazet2a8875e2009-12-08 20:19:53 -080035 /*
36 * We cannot call inet_twsk_put() ourself under lock,
37 * caller must call it for us.
38 */
Eric Dumazet13475a32009-12-02 22:31:19 +000039 return 1;
40}
41
Eric Dumazet2a8875e2009-12-08 20:19:53 -080042/**
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 Dumazet3cdaeda2009-12-04 03:47:42 +000050 */
51int 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 Dumazet2a8875e2009-12-08 20:19:53 -080062 /*
63 * We cannot call inet_twsk_put() ourself under lock,
64 * caller must call it for us.
65 */
Eric Dumazet3cdaeda2009-12-04 03:47:42 +000066 return 1;
67}
68
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070069/* Must be called with locally disabled BHs. */
Eric Dumazet789f5582015-04-12 18:51:09 -070070static void inet_twsk_kill(struct inet_timewait_sock *tw)
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070071{
Eric Dumazet789f5582015-04-12 18:51:09 -070072 struct inet_hashinfo *hashinfo = tw->tw_dr->hashinfo;
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070073 struct inet_bind_hashbucket *bhead;
Eric Dumazet13475a32009-12-02 22:31:19 +000074 int refcnt;
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070075 /* Unlink from established hashes. */
Eric Dumazet9db66bd2008-11-20 20:39:09 -080076 spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070077
Eric Dumazet9db66bd2008-11-20 20:39:09 -080078 spin_lock(lock);
Eric Dumazet13475a32009-12-02 22:31:19 +000079 refcnt = inet_twsk_unhash(tw);
Eric Dumazet9db66bd2008-11-20 20:39:09 -080080 spin_unlock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070081
82 /* Disassociate with bind bucket. */
Pavel Emelyanov7f635ab2008-06-16 17:12:49 -070083 bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num,
84 hashinfo->bhash_size)];
Eric Dumazet3cdaeda2009-12-04 03:47:42 +000085
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070086 spin_lock(&bhead->lock);
Eric Dumazet3cdaeda2009-12-04 03:47:42 +000087 refcnt += inet_twsk_bind_unhash(tw, hashinfo);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070088 spin_unlock(&bhead->lock);
Eric Dumazet3cdaeda2009-12-04 03:47:42 +000089
Eric Dumazet05dbc7b2013-10-03 00:22:02 -070090 BUG_ON(refcnt >= atomic_read(&tw->tw_refcnt));
91 atomic_sub(refcnt, &tw->tw_refcnt);
Eric Dumazet789f5582015-04-12 18:51:09 -070092 atomic_dec(&tw->tw_dr->tw_count);
93 inet_twsk_put(tw);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070094}
95
Eric Dumazet05dbc7b2013-10-03 00:22:02 -070096void inet_twsk_free(struct inet_timewait_sock *tw)
Arnaldo Carvalho de Melo4dbc8ef2009-05-06 16:50:52 -070097{
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 Melo4dbc8ef2009-05-06 16:50:52 -0700103 kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
104 module_put(owner);
105}
106
Pavel Emelyanov7054fb92007-12-20 15:32:54 -0800107void inet_twsk_put(struct inet_timewait_sock *tw)
108{
Arnaldo Carvalho de Melo4dbc8ef2009-05-06 16:50:52 -0700109 if (atomic_dec_and_test(&tw->tw_refcnt))
110 inet_twsk_free(tw);
Pavel Emelyanov7054fb92007-12-20 15:32:54 -0800111}
112EXPORT_SYMBOL_GPL(inet_twsk_put);
113
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700114static 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
120static 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 Meloe48c4142005-08-09 20:09:46 -0700126/*
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 */
131void __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 Melo463c84b2005-08-09 20:10:42 -0700135 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet81c3d542005-10-03 14:13:38 -0700136 struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
Eric Dumazet9db66bd2008-11-20 20:39:09 -0800137 spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700138 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 Dumazetc720c7e2009-10-15 06:30:45 +0000143 bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->inet_num,
Pavel Emelyanov7f635ab2008-06-16 17:12:49 -0700144 hashinfo->bhash_size)];
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700145 spin_lock(&bhead->lock);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700146 tw->tw_tb = icsk->icsk_bind_hash;
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700147 WARN_ON(!icsk->icsk_bind_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700148 inet_twsk_add_bind_node(tw, &tw->tw_tb->owners);
149 spin_unlock(&bhead->lock);
150
Eric Dumazet9db66bd2008-11-20 20:39:09 -0800151 spin_lock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700152
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800153 /*
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700154 * 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 Dumazet3ab5aee2008-11-16 19:40:17 -0800161 */
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700162 atomic_set(&tw->tw_refcnt, 1 + 1 + 1);
163 inet_twsk_add_node_rcu(tw, &ehead->chain);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800164
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700165 /* Step 3: Remove SK from hash chain */
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800166 if (__sk_nulls_del_node_init_rcu(sk))
167 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700168
Eric Dumazet9db66bd2008-11-20 20:39:09 -0800169 spin_unlock(lock);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700170}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700171EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
172
Eric Dumazet216f8bb2015-05-12 06:22:56 -0700173static void tw_timer_handler(unsigned long data)
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700174{
Eric Dumazet789f5582015-04-12 18:51:09 -0700175 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
184struct 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 Morris00db4122015-04-03 09:17:27 +0100195 if (tw) {
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700196 const struct inet_sock *inet = inet_sk(sk);
197
Vegard Nossum9e337b02008-10-18 17:37:51 +0200198 kmemcheck_annotate_bitfield(tw, flags);
199
Eric Dumazet789f5582015-04-12 18:51:09 -0700200 tw->tw_dr = dr;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700201 /* Give us an identity. */
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000202 tw->tw_daddr = inet->inet_daddr;
203 tw->tw_rcv_saddr = inet->inet_rcv_saddr;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700204 tw->tw_bound_dev_if = sk->sk_bound_dev_if;
Eric Dumazet66b13d92011-10-24 03:06:21 -0400205 tw->tw_tos = inet->tos;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000206 tw->tw_num = inet->inet_num;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700207 tw->tw_state = TCP_TIME_WAIT;
208 tw->tw_substate = state;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000209 tw->tw_sport = inet->inet_sport;
210 tw->tw_dport = inet->inet_dport;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700211 tw->tw_family = sk->sk_family;
212 tw->tw_reuse = sk->sk_reuse;
Eric Dumazet81c3d542005-10-03 14:13:38 -0700213 tw->tw_hash = sk->sk_hash;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700214 tw->tw_ipv6only = 0;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -0700215 tw->tw_transparent = inet->transparent;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700216 tw->tw_prot = sk->sk_prot_creator;
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700217 atomic64_set(&tw->tw_cookie, atomic64_read(&sk->sk_cookie));
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -0500218 twsk_net_set(tw, sock_net(sk));
Eric Dumazet789f5582015-04-12 18:51:09 -0700219 setup_timer(&tw->tw_timer, tw_timer_handler, (unsigned long)tw);
Eric Dumazet47e1c322009-12-03 00:49:01 +0000220 /*
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 Dumazet789f5582015-04-12 18:51:09 -0700226
Arnaldo Carvalho de Meloeeb2b852005-10-10 21:25:23 -0700227 __module_get(tw->tw_prot->owner);
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700228 }
229
230 return tw;
231}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700232EXPORT_SYMBOL_GPL(inet_twsk_alloc);
233
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700234/* 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 Dumazet789f5582015-04-12 18:51:09 -0700239void inet_twsk_deschedule(struct inet_timewait_sock *tw)
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700240{
Eric Dumazet789f5582015-04-12 18:51:09 -0700241 if (del_timer_sync(&tw->tw_timer))
242 inet_twsk_kill(tw);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700243}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700244EXPORT_SYMBOL(inet_twsk_deschedule);
245
Eric Dumazet789f5582015-04-12 18:51:09 -0700246void inet_twsk_schedule(struct inet_timewait_sock *tw, const int timeo)
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700247{
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700248 /* 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 Melo696ab2d2005-08-09 20:45:03 -0700272
Eric Dumazet789f5582015-04-12 18:51:09 -0700273 tw->tw_kill = timeo <= 4*HZ;
274 if (!mod_timer_pinned(&tw->tw_timer, jiffies + timeo)) {
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700275 atomic_inc(&tw->tw_refcnt);
Eric Dumazet789f5582015-04-12 18:51:09 -0700276 atomic_inc(&tw->tw_dr->tw_count);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700277 }
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700278}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700279EXPORT_SYMBOL_GPL(inet_twsk_schedule);
280
Eric W. Biedermanb099ce22009-12-03 02:29:09 +0000281void inet_twsk_purge(struct inet_hashinfo *hashinfo,
Daniel Lezcanod3154922008-09-08 13:17:27 -0700282 struct inet_timewait_death_row *twdr, int family)
283{
284 struct inet_timewait_sock *tw;
285 struct sock *sk;
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800286 struct hlist_nulls_node *node;
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000287 unsigned int slot;
Daniel Lezcanod3154922008-09-08 13:17:27 -0700288
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000289 for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
290 struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
291restart_rcu:
Eric Dumazet738e6d32015-03-18 17:40:51 -0700292 cond_resched();
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000293 rcu_read_lock();
Daniel Lezcanod3154922008-09-08 13:17:27 -0700294restart:
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700295 sk_nulls_for_each_rcu(sk, node, &head->chain) {
296 if (sk->sk_state != TCP_TIME_WAIT)
297 continue;
Daniel Lezcanod3154922008-09-08 13:17:27 -0700298 tw = inet_twsk(sk);
Eric W. Biedermanb099ce22009-12-03 02:29:09 +0000299 if ((tw->tw_family != family) ||
300 atomic_read(&twsk_net(tw)->count))
Daniel Lezcanod3154922008-09-08 13:17:27 -0700301 continue;
302
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000303 if (unlikely(!atomic_inc_not_zero(&tw->tw_refcnt)))
304 continue;
305
Eric W. Biedermanb099ce22009-12-03 02:29:09 +0000306 if (unlikely((tw->tw_family != family) ||
307 atomic_read(&twsk_net(tw)->count))) {
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000308 inet_twsk_put(tw);
309 goto restart;
310 }
311
312 rcu_read_unlock();
Eric Dumazet91035f02011-02-18 22:35:56 +0000313 local_bh_disable();
Eric Dumazet789f5582015-04-12 18:51:09 -0700314 inet_twsk_deschedule(tw);
Eric Dumazet91035f02011-02-18 22:35:56 +0000315 local_bh_enable();
Daniel Lezcanod3154922008-09-08 13:17:27 -0700316 inet_twsk_put(tw);
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000317 goto restart_rcu;
Daniel Lezcanod3154922008-09-08 13:17:27 -0700318 }
Eric W. Biederman575f4cd2009-12-03 02:29:08 +0000319 /* 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 Lezcanod3154922008-09-08 13:17:27 -0700326 }
Daniel Lezcanod3154922008-09-08 13:17:27 -0700327}
328EXPORT_SYMBOL_GPL(inet_twsk_purge);