1e48c414eSArnaldo Carvalho de Melo /* 2e48c414eSArnaldo Carvalho de Melo * INET An implementation of the TCP/IP protocol suite for the LINUX 3e48c414eSArnaldo Carvalho de Melo * operating system. INET is implemented using the BSD Socket 4e48c414eSArnaldo Carvalho de Melo * interface as the means of communication with the user level. 5e48c414eSArnaldo Carvalho de Melo * 6e48c414eSArnaldo Carvalho de Melo * Generic TIME_WAIT sockets functions 7e48c414eSArnaldo Carvalho de Melo * 8e48c414eSArnaldo Carvalho de Melo * From code orinally in TCP 9e48c414eSArnaldo Carvalho de Melo */ 10e48c414eSArnaldo Carvalho de Melo 11172589ccSIlpo Järvinen #include <linux/kernel.h> 125a0e3ad6STejun Heo #include <linux/slab.h> 133a9a231dSPaul Gortmaker #include <linux/module.h> 14e48c414eSArnaldo Carvalho de Melo #include <net/inet_hashtables.h> 15e48c414eSArnaldo Carvalho de Melo #include <net/inet_timewait_sock.h> 16696ab2d3SArnaldo Carvalho de Melo #include <net/ip.h> 17e48c414eSArnaldo Carvalho de Melo 1813475a30SEric Dumazet 192a8875e7SEric Dumazet /** 202a8875e7SEric Dumazet * inet_twsk_bind_unhash - unhash a timewait socket from bind hash 212a8875e7SEric Dumazet * @tw: timewait socket 222a8875e7SEric Dumazet * @hashinfo: hashinfo pointer 232a8875e7SEric Dumazet * 242a8875e7SEric Dumazet * unhash a timewait socket from bind hash, if hashed. 252a8875e7SEric Dumazet * bind hash lock must be held by caller. 262a8875e7SEric Dumazet * Returns 1 if caller should call inet_twsk_put() after lock release. 273cdaedaeSEric Dumazet */ 28fc01538fSEric Dumazet void inet_twsk_bind_unhash(struct inet_timewait_sock *tw, 293cdaedaeSEric Dumazet struct inet_hashinfo *hashinfo) 303cdaedaeSEric Dumazet { 313cdaedaeSEric Dumazet struct inet_bind_bucket *tb = tw->tw_tb; 323cdaedaeSEric Dumazet 333cdaedaeSEric Dumazet if (!tb) 34fc01538fSEric Dumazet return; 353cdaedaeSEric Dumazet 363cdaedaeSEric Dumazet __hlist_del(&tw->tw_bind_node); 373cdaedaeSEric Dumazet tw->tw_tb = NULL; 383cdaedaeSEric Dumazet inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb); 39fc01538fSEric Dumazet __sock_put((struct sock *)tw); 403cdaedaeSEric Dumazet } 413cdaedaeSEric Dumazet 42e48c414eSArnaldo Carvalho de Melo /* Must be called with locally disabled BHs. */ 43789f558cSEric Dumazet static void inet_twsk_kill(struct inet_timewait_sock *tw) 44e48c414eSArnaldo Carvalho de Melo { 45789f558cSEric Dumazet struct inet_hashinfo *hashinfo = tw->tw_dr->hashinfo; 469db66bdcSEric Dumazet spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash); 47fc01538fSEric Dumazet struct inet_bind_hashbucket *bhead; 48e48c414eSArnaldo Carvalho de Melo 499db66bdcSEric Dumazet spin_lock(lock); 50fc01538fSEric Dumazet sk_nulls_del_node_init_rcu((struct sock *)tw); 519db66bdcSEric Dumazet spin_unlock(lock); 52e48c414eSArnaldo Carvalho de Melo 53e48c414eSArnaldo Carvalho de Melo /* Disassociate with bind bucket. */ 547f635ab7SPavel Emelyanov bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num, 557f635ab7SPavel Emelyanov hashinfo->bhash_size)]; 563cdaedaeSEric Dumazet 57e48c414eSArnaldo Carvalho de Melo spin_lock(&bhead->lock); 58fc01538fSEric Dumazet inet_twsk_bind_unhash(tw, hashinfo); 59e48c414eSArnaldo Carvalho de Melo spin_unlock(&bhead->lock); 603cdaedaeSEric Dumazet 61789f558cSEric Dumazet atomic_dec(&tw->tw_dr->tw_count); 62789f558cSEric Dumazet inet_twsk_put(tw); 63e48c414eSArnaldo Carvalho de Melo } 64e48c414eSArnaldo Carvalho de Melo 6505dbc7b5SEric Dumazet void inet_twsk_free(struct inet_timewait_sock *tw) 667054fb93SPavel Emelyanov { 677054fb93SPavel Emelyanov struct module *owner = tw->tw_prot->owner; 687054fb93SPavel Emelyanov twsk_destructor((struct sock *)tw); 697054fb93SPavel Emelyanov #ifdef SOCK_REFCNT_DEBUG 704dbc8ef7SArnaldo Carvalho de Melo pr_debug("%s timewait_sock %p released\n", tw->tw_prot->name, tw); 717054fb93SPavel Emelyanov #endif 727054fb93SPavel Emelyanov kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw); 737054fb93SPavel Emelyanov module_put(owner); 747054fb93SPavel Emelyanov } 754dbc8ef7SArnaldo Carvalho de Melo 764dbc8ef7SArnaldo Carvalho de Melo void inet_twsk_put(struct inet_timewait_sock *tw) 774dbc8ef7SArnaldo Carvalho de Melo { 7841c6d650SReshetova, Elena if (refcount_dec_and_test(&tw->tw_refcnt)) 794dbc8ef7SArnaldo Carvalho de Melo inet_twsk_free(tw); 807054fb93SPavel Emelyanov } 817054fb93SPavel Emelyanov EXPORT_SYMBOL_GPL(inet_twsk_put); 827054fb93SPavel Emelyanov 8305dbc7b5SEric Dumazet static void inet_twsk_add_node_rcu(struct inet_timewait_sock *tw, 8405dbc7b5SEric Dumazet struct hlist_nulls_head *list) 8505dbc7b5SEric Dumazet { 8605dbc7b5SEric Dumazet hlist_nulls_add_head_rcu(&tw->tw_node, list); 8705dbc7b5SEric Dumazet } 8805dbc7b5SEric Dumazet 8905dbc7b5SEric Dumazet static void inet_twsk_add_bind_node(struct inet_timewait_sock *tw, 9005dbc7b5SEric Dumazet struct hlist_head *list) 9105dbc7b5SEric Dumazet { 9205dbc7b5SEric Dumazet hlist_add_head(&tw->tw_bind_node, list); 9305dbc7b5SEric Dumazet } 9405dbc7b5SEric Dumazet 95e48c414eSArnaldo Carvalho de Melo /* 96e599ea14SEric Dumazet * Enter the time wait state. This is called with locally disabled BH. 97e48c414eSArnaldo Carvalho de Melo * Essentially we whip up a timewait bucket, copy the relevant info into it 98e48c414eSArnaldo Carvalho de Melo * from the SK, and mess with hash chains and list linkage. 99e48c414eSArnaldo Carvalho de Melo */ 100*ec94c269SEric Dumazet void inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk, 101e48c414eSArnaldo Carvalho de Melo struct inet_hashinfo *hashinfo) 102e48c414eSArnaldo Carvalho de Melo { 103e48c414eSArnaldo Carvalho de Melo const struct inet_sock *inet = inet_sk(sk); 104463c84b9SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 10581c3d547SEric Dumazet struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash); 1069db66bdcSEric Dumazet spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash); 107e48c414eSArnaldo Carvalho de Melo struct inet_bind_hashbucket *bhead; 108e48c414eSArnaldo Carvalho de Melo /* Step 1: Put TW into bind hash. Original socket stays there too. 109e48c414eSArnaldo Carvalho de Melo Note, that any socket with inet->num != 0 MUST be bound in 110e48c414eSArnaldo Carvalho de Melo binding cache, even if it is closed. 111e48c414eSArnaldo Carvalho de Melo */ 112c720c7e8SEric Dumazet bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->inet_num, 1137f635ab7SPavel Emelyanov hashinfo->bhash_size)]; 114e599ea14SEric Dumazet spin_lock(&bhead->lock); 115463c84b9SArnaldo Carvalho de Melo tw->tw_tb = icsk->icsk_bind_hash; 116547b792cSIlpo Järvinen WARN_ON(!icsk->icsk_bind_hash); 117e48c414eSArnaldo Carvalho de Melo inet_twsk_add_bind_node(tw, &tw->tw_tb->owners); 118e48c414eSArnaldo Carvalho de Melo spin_unlock(&bhead->lock); 119e48c414eSArnaldo Carvalho de Melo 1209db66bdcSEric Dumazet spin_lock(lock); 121e48c414eSArnaldo Carvalho de Melo 12205dbc7b5SEric Dumazet inet_twsk_add_node_rcu(tw, &ehead->chain); 1233ab5aee7SEric Dumazet 12405dbc7b5SEric Dumazet /* Step 3: Remove SK from hash chain */ 1253ab5aee7SEric Dumazet if (__sk_nulls_del_node_init_rcu(sk)) 1263ab5aee7SEric Dumazet sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 127e48c414eSArnaldo Carvalho de Melo 128e599ea14SEric Dumazet spin_unlock(lock); 129*ec94c269SEric Dumazet 130*ec94c269SEric Dumazet /* tw_refcnt is set to 3 because we have : 131*ec94c269SEric Dumazet * - one reference for bhash chain. 132*ec94c269SEric Dumazet * - one reference for ehash chain. 133*ec94c269SEric Dumazet * - one reference for timer. 134*ec94c269SEric Dumazet * We can use atomic_set() because prior spin_lock()/spin_unlock() 135*ec94c269SEric Dumazet * committed into memory all tw fields. 136*ec94c269SEric Dumazet * Also note that after this point, we lost our implicit reference 137*ec94c269SEric Dumazet * so we are not allowed to use tw anymore. 138*ec94c269SEric Dumazet */ 139*ec94c269SEric Dumazet refcount_set(&tw->tw_refcnt, 3); 140e48c414eSArnaldo Carvalho de Melo } 141*ec94c269SEric Dumazet EXPORT_SYMBOL_GPL(inet_twsk_hashdance); 142696ab2d3SArnaldo Carvalho de Melo 1431ab791dcSKees Cook static void tw_timer_handler(struct timer_list *t) 144c676270bSArnaldo Carvalho de Melo { 1451ab791dcSKees Cook struct inet_timewait_sock *tw = from_timer(tw, t, tw_timer); 146789f558cSEric Dumazet 147789f558cSEric Dumazet if (tw->tw_kill) 14802a1d6e7SEric Dumazet __NET_INC_STATS(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED); 149789f558cSEric Dumazet else 15002a1d6e7SEric Dumazet __NET_INC_STATS(twsk_net(tw), LINUX_MIB_TIMEWAITED); 151789f558cSEric Dumazet inet_twsk_kill(tw); 152789f558cSEric Dumazet } 153789f558cSEric Dumazet 154789f558cSEric Dumazet struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, 155789f558cSEric Dumazet struct inet_timewait_death_row *dr, 156789f558cSEric Dumazet const int state) 157789f558cSEric Dumazet { 158789f558cSEric Dumazet struct inet_timewait_sock *tw; 159789f558cSEric Dumazet 160789f558cSEric Dumazet if (atomic_read(&dr->tw_count) >= dr->sysctl_max_tw_buckets) 161789f558cSEric Dumazet return NULL; 162789f558cSEric Dumazet 163789f558cSEric Dumazet tw = kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab, 16454e6ecb2SChristoph Lameter GFP_ATOMIC); 16500db4124SIan Morris if (tw) { 166c676270bSArnaldo Carvalho de Melo const struct inet_sock *inet = inet_sk(sk); 167c676270bSArnaldo Carvalho de Melo 168789f558cSEric Dumazet tw->tw_dr = dr; 169c676270bSArnaldo Carvalho de Melo /* Give us an identity. */ 170c720c7e8SEric Dumazet tw->tw_daddr = inet->inet_daddr; 171c720c7e8SEric Dumazet tw->tw_rcv_saddr = inet->inet_rcv_saddr; 172c676270bSArnaldo Carvalho de Melo tw->tw_bound_dev_if = sk->sk_bound_dev_if; 17366b13d99SEric Dumazet tw->tw_tos = inet->tos; 174c720c7e8SEric Dumazet tw->tw_num = inet->inet_num; 175c676270bSArnaldo Carvalho de Melo tw->tw_state = TCP_TIME_WAIT; 176c676270bSArnaldo Carvalho de Melo tw->tw_substate = state; 177c720c7e8SEric Dumazet tw->tw_sport = inet->inet_sport; 178c720c7e8SEric Dumazet tw->tw_dport = inet->inet_dport; 179c676270bSArnaldo Carvalho de Melo tw->tw_family = sk->sk_family; 180c676270bSArnaldo Carvalho de Melo tw->tw_reuse = sk->sk_reuse; 18181c3d547SEric Dumazet tw->tw_hash = sk->sk_hash; 182c676270bSArnaldo Carvalho de Melo tw->tw_ipv6only = 0; 183f5715aeaSKOVACS Krisztian tw->tw_transparent = inet->transparent; 184c676270bSArnaldo Carvalho de Melo tw->tw_prot = sk->sk_prot_creator; 18533cf7c90SEric Dumazet atomic64_set(&tw->tw_cookie, atomic64_read(&sk->sk_cookie)); 186efd7ef1cSEric W. Biederman twsk_net_set(tw, sock_net(sk)); 1871ab791dcSKees Cook timer_setup(&tw->tw_timer, tw_timer_handler, TIMER_PINNED); 18847e1c323SEric Dumazet /* 18947e1c323SEric Dumazet * Because we use RCU lookups, we should not set tw_refcnt 19047e1c323SEric Dumazet * to a non null value before everything is setup for this 19147e1c323SEric Dumazet * timewait socket. 19247e1c323SEric Dumazet */ 19341c6d650SReshetova, Elena refcount_set(&tw->tw_refcnt, 0); 194789f558cSEric Dumazet 195eeb2b856SArnaldo Carvalho de Melo __module_get(tw->tw_prot->owner); 196c676270bSArnaldo Carvalho de Melo } 197c676270bSArnaldo Carvalho de Melo 198c676270bSArnaldo Carvalho de Melo return tw; 199c676270bSArnaldo Carvalho de Melo } 200696ab2d3SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet_twsk_alloc); 201696ab2d3SArnaldo Carvalho de Melo 202696ab2d3SArnaldo Carvalho de Melo /* These are always called from BH context. See callers in 203696ab2d3SArnaldo Carvalho de Melo * tcp_input.c to verify this. 204696ab2d3SArnaldo Carvalho de Melo */ 205696ab2d3SArnaldo Carvalho de Melo 206dbe7faa4SEric Dumazet /* This is for handling early-kills of TIME_WAIT sockets. 207dbe7faa4SEric Dumazet * Warning : consume reference. 208dbe7faa4SEric Dumazet * Caller should not access tw anymore. 209dbe7faa4SEric Dumazet */ 210dbe7faa4SEric Dumazet void inet_twsk_deschedule_put(struct inet_timewait_sock *tw) 211696ab2d3SArnaldo Carvalho de Melo { 212789f558cSEric Dumazet if (del_timer_sync(&tw->tw_timer)) 213789f558cSEric Dumazet inet_twsk_kill(tw); 214dbe7faa4SEric Dumazet inet_twsk_put(tw); 215696ab2d3SArnaldo Carvalho de Melo } 216dbe7faa4SEric Dumazet EXPORT_SYMBOL(inet_twsk_deschedule_put); 217696ab2d3SArnaldo Carvalho de Melo 218ed2e9239SEric Dumazet void __inet_twsk_schedule(struct inet_timewait_sock *tw, int timeo, bool rearm) 219696ab2d3SArnaldo Carvalho de Melo { 220696ab2d3SArnaldo Carvalho de Melo /* timeout := RTO * 3.5 221696ab2d3SArnaldo Carvalho de Melo * 222696ab2d3SArnaldo Carvalho de Melo * 3.5 = 1+2+0.5 to wait for two retransmits. 223696ab2d3SArnaldo Carvalho de Melo * 224696ab2d3SArnaldo Carvalho de Melo * RATIONALE: if FIN arrived and we entered TIME-WAIT state, 225696ab2d3SArnaldo Carvalho de Melo * our ACK acking that FIN can be lost. If N subsequent retransmitted 226696ab2d3SArnaldo Carvalho de Melo * FINs (or previous seqments) are lost (probability of such event 227696ab2d3SArnaldo Carvalho de Melo * is p^(N+1), where p is probability to lose single packet and 228696ab2d3SArnaldo Carvalho de Melo * time to detect the loss is about RTO*(2^N - 1) with exponential 229696ab2d3SArnaldo Carvalho de Melo * backoff). Normal timewait length is calculated so, that we 230696ab2d3SArnaldo Carvalho de Melo * waited at least for one retransmitted FIN (maximal RTO is 120sec). 231696ab2d3SArnaldo Carvalho de Melo * [ BTW Linux. following BSD, violates this requirement waiting 232696ab2d3SArnaldo Carvalho de Melo * only for 60sec, we should wait at least for 240 secs. 233696ab2d3SArnaldo Carvalho de Melo * Well, 240 consumes too much of resources 8) 234696ab2d3SArnaldo Carvalho de Melo * ] 235696ab2d3SArnaldo Carvalho de Melo * This interval is not reduced to catch old duplicate and 236696ab2d3SArnaldo Carvalho de Melo * responces to our wandering segments living for two MSLs. 237696ab2d3SArnaldo Carvalho de Melo * However, if we use PAWS to detect 238696ab2d3SArnaldo Carvalho de Melo * old duplicates, we can reduce the interval to bounds required 239696ab2d3SArnaldo Carvalho de Melo * by RTO, rather than MSL. So, if peer understands PAWS, we 240696ab2d3SArnaldo Carvalho de Melo * kill tw bucket after 3.5*RTO (it is important that this number 241696ab2d3SArnaldo Carvalho de Melo * is greater than TS tick!) and detect old duplicates with help 242696ab2d3SArnaldo Carvalho de Melo * of PAWS. 243696ab2d3SArnaldo Carvalho de Melo */ 244696ab2d3SArnaldo Carvalho de Melo 245789f558cSEric Dumazet tw->tw_kill = timeo <= 4*HZ; 246ed2e9239SEric Dumazet if (!rearm) { 247f3438bc7SThomas Gleixner BUG_ON(mod_timer(&tw->tw_timer, jiffies + timeo)); 248789f558cSEric Dumazet atomic_inc(&tw->tw_dr->tw_count); 249ed2e9239SEric Dumazet } else { 250ed2e9239SEric Dumazet mod_timer_pending(&tw->tw_timer, jiffies + timeo); 251696ab2d3SArnaldo Carvalho de Melo } 252696ab2d3SArnaldo Carvalho de Melo } 253ed2e9239SEric Dumazet EXPORT_SYMBOL_GPL(__inet_twsk_schedule); 254696ab2d3SArnaldo Carvalho de Melo 2551946e672SHaishuang Yan void inet_twsk_purge(struct inet_hashinfo *hashinfo, int family) 256d315492bSDaniel Lezcano { 257d315492bSDaniel Lezcano struct inet_timewait_sock *tw; 258d315492bSDaniel Lezcano struct sock *sk; 2593ab5aee7SEric Dumazet struct hlist_nulls_node *node; 260575f4cd5SEric W. Biederman unsigned int slot; 261d315492bSDaniel Lezcano 262575f4cd5SEric W. Biederman for (slot = 0; slot <= hashinfo->ehash_mask; slot++) { 263575f4cd5SEric W. Biederman struct inet_ehash_bucket *head = &hashinfo->ehash[slot]; 264575f4cd5SEric W. Biederman restart_rcu: 265738e6d30SEric Dumazet cond_resched(); 266575f4cd5SEric W. Biederman rcu_read_lock(); 267d315492bSDaniel Lezcano restart: 26805dbc7b5SEric Dumazet sk_nulls_for_each_rcu(sk, node, &head->chain) { 26905dbc7b5SEric Dumazet if (sk->sk_state != TCP_TIME_WAIT) 27005dbc7b5SEric Dumazet continue; 271d315492bSDaniel Lezcano tw = inet_twsk(sk); 272b099ce26SEric W. Biederman if ((tw->tw_family != family) || 273b099ce26SEric W. Biederman atomic_read(&twsk_net(tw)->count)) 274d315492bSDaniel Lezcano continue; 275d315492bSDaniel Lezcano 27641c6d650SReshetova, Elena if (unlikely(!refcount_inc_not_zero(&tw->tw_refcnt))) 277575f4cd5SEric W. Biederman continue; 278d315492bSDaniel Lezcano 279b099ce26SEric W. Biederman if (unlikely((tw->tw_family != family) || 280b099ce26SEric W. Biederman atomic_read(&twsk_net(tw)->count))) { 281575f4cd5SEric W. Biederman inet_twsk_put(tw); 282d315492bSDaniel Lezcano goto restart; 283d315492bSDaniel Lezcano } 284575f4cd5SEric W. Biederman 285575f4cd5SEric W. Biederman rcu_read_unlock(); 28691035f0bSEric Dumazet local_bh_disable(); 287dbe7faa4SEric Dumazet inet_twsk_deschedule_put(tw); 28891035f0bSEric Dumazet local_bh_enable(); 289575f4cd5SEric W. Biederman goto restart_rcu; 290d315492bSDaniel Lezcano } 291575f4cd5SEric W. Biederman /* If the nulls value we got at the end of this lookup is 292575f4cd5SEric W. Biederman * not the expected one, we must restart lookup. 293575f4cd5SEric W. Biederman * We probably met an item that was moved to another chain. 294575f4cd5SEric W. Biederman */ 295575f4cd5SEric W. Biederman if (get_nulls_value(node) != slot) 296575f4cd5SEric W. Biederman goto restart; 297575f4cd5SEric W. Biederman rcu_read_unlock(); 298575f4cd5SEric W. Biederman } 299d315492bSDaniel Lezcano } 300d315492bSDaniel Lezcano EXPORT_SYMBOL_GPL(inet_twsk_purge); 301