1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * INET An implementation of the TCP/IP protocol suite for the LINUX
4*4882a593Smuzhiyun * operating system. INET is implemented using the BSD Socket
5*4882a593Smuzhiyun * interface as the means of communication with the user level.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Generic TIME_WAIT sockets functions
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * From code orinally in TCP
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <net/inet_hashtables.h>
16*4882a593Smuzhiyun #include <net/inet_timewait_sock.h>
17*4882a593Smuzhiyun #include <net/ip.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /**
21*4882a593Smuzhiyun * inet_twsk_bind_unhash - unhash a timewait socket from bind hash
22*4882a593Smuzhiyun * @tw: timewait socket
23*4882a593Smuzhiyun * @hashinfo: hashinfo pointer
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * unhash a timewait socket from bind hash, if hashed.
26*4882a593Smuzhiyun * bind hash lock must be held by caller.
27*4882a593Smuzhiyun * Returns 1 if caller should call inet_twsk_put() after lock release.
28*4882a593Smuzhiyun */
inet_twsk_bind_unhash(struct inet_timewait_sock * tw,struct inet_hashinfo * hashinfo)29*4882a593Smuzhiyun void inet_twsk_bind_unhash(struct inet_timewait_sock *tw,
30*4882a593Smuzhiyun struct inet_hashinfo *hashinfo)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun struct inet_bind_bucket *tb = tw->tw_tb;
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun if (!tb)
35*4882a593Smuzhiyun return;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun __hlist_del(&tw->tw_bind_node);
38*4882a593Smuzhiyun tw->tw_tb = NULL;
39*4882a593Smuzhiyun inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);
40*4882a593Smuzhiyun __sock_put((struct sock *)tw);
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* Must be called with locally disabled BHs. */
inet_twsk_kill(struct inet_timewait_sock * tw)44*4882a593Smuzhiyun static void inet_twsk_kill(struct inet_timewait_sock *tw)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun struct inet_hashinfo *hashinfo = tw->tw_dr->hashinfo;
47*4882a593Smuzhiyun spinlock_t *lock = inet_ehash_lockp(hashinfo, tw->tw_hash);
48*4882a593Smuzhiyun struct inet_bind_hashbucket *bhead;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun spin_lock(lock);
51*4882a593Smuzhiyun sk_nulls_del_node_init_rcu((struct sock *)tw);
52*4882a593Smuzhiyun spin_unlock(lock);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /* Disassociate with bind bucket. */
55*4882a593Smuzhiyun bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), tw->tw_num,
56*4882a593Smuzhiyun hashinfo->bhash_size)];
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun spin_lock(&bhead->lock);
59*4882a593Smuzhiyun inet_twsk_bind_unhash(tw, hashinfo);
60*4882a593Smuzhiyun spin_unlock(&bhead->lock);
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun atomic_dec(&tw->tw_dr->tw_count);
63*4882a593Smuzhiyun inet_twsk_put(tw);
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
inet_twsk_free(struct inet_timewait_sock * tw)66*4882a593Smuzhiyun void inet_twsk_free(struct inet_timewait_sock *tw)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun struct module *owner = tw->tw_prot->owner;
69*4882a593Smuzhiyun twsk_destructor((struct sock *)tw);
70*4882a593Smuzhiyun #ifdef SOCK_REFCNT_DEBUG
71*4882a593Smuzhiyun pr_debug("%s timewait_sock %p released\n", tw->tw_prot->name, tw);
72*4882a593Smuzhiyun #endif
73*4882a593Smuzhiyun kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
74*4882a593Smuzhiyun module_put(owner);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
inet_twsk_put(struct inet_timewait_sock * tw)77*4882a593Smuzhiyun void inet_twsk_put(struct inet_timewait_sock *tw)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun if (refcount_dec_and_test(&tw->tw_refcnt))
80*4882a593Smuzhiyun inet_twsk_free(tw);
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(inet_twsk_put);
83*4882a593Smuzhiyun
inet_twsk_add_node_rcu(struct inet_timewait_sock * tw,struct hlist_nulls_head * list)84*4882a593Smuzhiyun static void inet_twsk_add_node_rcu(struct inet_timewait_sock *tw,
85*4882a593Smuzhiyun struct hlist_nulls_head *list)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun hlist_nulls_add_head_rcu(&tw->tw_node, list);
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
inet_twsk_add_bind_node(struct inet_timewait_sock * tw,struct hlist_head * list)90*4882a593Smuzhiyun static void inet_twsk_add_bind_node(struct inet_timewait_sock *tw,
91*4882a593Smuzhiyun struct hlist_head *list)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun hlist_add_head(&tw->tw_bind_node, list);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun * Enter the time wait state. This is called with locally disabled BH.
98*4882a593Smuzhiyun * Essentially we whip up a timewait bucket, copy the relevant info into it
99*4882a593Smuzhiyun * from the SK, and mess with hash chains and list linkage.
100*4882a593Smuzhiyun */
inet_twsk_hashdance(struct inet_timewait_sock * tw,struct sock * sk,struct inet_hashinfo * hashinfo)101*4882a593Smuzhiyun void inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
102*4882a593Smuzhiyun struct inet_hashinfo *hashinfo)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun const struct inet_sock *inet = inet_sk(sk);
105*4882a593Smuzhiyun const struct inet_connection_sock *icsk = inet_csk(sk);
106*4882a593Smuzhiyun struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
107*4882a593Smuzhiyun spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
108*4882a593Smuzhiyun struct inet_bind_hashbucket *bhead;
109*4882a593Smuzhiyun /* Step 1: Put TW into bind hash. Original socket stays there too.
110*4882a593Smuzhiyun Note, that any socket with inet->num != 0 MUST be bound in
111*4882a593Smuzhiyun binding cache, even if it is closed.
112*4882a593Smuzhiyun */
113*4882a593Smuzhiyun bhead = &hashinfo->bhash[inet_bhashfn(twsk_net(tw), inet->inet_num,
114*4882a593Smuzhiyun hashinfo->bhash_size)];
115*4882a593Smuzhiyun spin_lock(&bhead->lock);
116*4882a593Smuzhiyun tw->tw_tb = icsk->icsk_bind_hash;
117*4882a593Smuzhiyun WARN_ON(!icsk->icsk_bind_hash);
118*4882a593Smuzhiyun inet_twsk_add_bind_node(tw, &tw->tw_tb->owners);
119*4882a593Smuzhiyun spin_unlock(&bhead->lock);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun spin_lock(lock);
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun inet_twsk_add_node_rcu(tw, &ehead->chain);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun /* Step 3: Remove SK from hash chain */
126*4882a593Smuzhiyun if (__sk_nulls_del_node_init_rcu(sk))
127*4882a593Smuzhiyun sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun spin_unlock(lock);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /* tw_refcnt is set to 3 because we have :
132*4882a593Smuzhiyun * - one reference for bhash chain.
133*4882a593Smuzhiyun * - one reference for ehash chain.
134*4882a593Smuzhiyun * - one reference for timer.
135*4882a593Smuzhiyun * We can use atomic_set() because prior spin_lock()/spin_unlock()
136*4882a593Smuzhiyun * committed into memory all tw fields.
137*4882a593Smuzhiyun * Also note that after this point, we lost our implicit reference
138*4882a593Smuzhiyun * so we are not allowed to use tw anymore.
139*4882a593Smuzhiyun */
140*4882a593Smuzhiyun refcount_set(&tw->tw_refcnt, 3);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(inet_twsk_hashdance);
143*4882a593Smuzhiyun
tw_timer_handler(struct timer_list * t)144*4882a593Smuzhiyun static void tw_timer_handler(struct timer_list *t)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun struct inet_timewait_sock *tw = from_timer(tw, t, tw_timer);
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun if (tw->tw_kill)
149*4882a593Smuzhiyun __NET_INC_STATS(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED);
150*4882a593Smuzhiyun else
151*4882a593Smuzhiyun __NET_INC_STATS(twsk_net(tw), LINUX_MIB_TIMEWAITED);
152*4882a593Smuzhiyun inet_twsk_kill(tw);
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
inet_twsk_alloc(const struct sock * sk,struct inet_timewait_death_row * dr,const int state)155*4882a593Smuzhiyun struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk,
156*4882a593Smuzhiyun struct inet_timewait_death_row *dr,
157*4882a593Smuzhiyun const int state)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun struct inet_timewait_sock *tw;
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun if (atomic_read(&dr->tw_count) >= dr->sysctl_max_tw_buckets)
162*4882a593Smuzhiyun return NULL;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun tw = kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
165*4882a593Smuzhiyun GFP_ATOMIC);
166*4882a593Smuzhiyun if (tw) {
167*4882a593Smuzhiyun const struct inet_sock *inet = inet_sk(sk);
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun tw->tw_dr = dr;
170*4882a593Smuzhiyun /* Give us an identity. */
171*4882a593Smuzhiyun tw->tw_daddr = inet->inet_daddr;
172*4882a593Smuzhiyun tw->tw_rcv_saddr = inet->inet_rcv_saddr;
173*4882a593Smuzhiyun tw->tw_bound_dev_if = sk->sk_bound_dev_if;
174*4882a593Smuzhiyun tw->tw_tos = inet->tos;
175*4882a593Smuzhiyun tw->tw_num = inet->inet_num;
176*4882a593Smuzhiyun tw->tw_state = TCP_TIME_WAIT;
177*4882a593Smuzhiyun tw->tw_substate = state;
178*4882a593Smuzhiyun tw->tw_sport = inet->inet_sport;
179*4882a593Smuzhiyun tw->tw_dport = inet->inet_dport;
180*4882a593Smuzhiyun tw->tw_family = sk->sk_family;
181*4882a593Smuzhiyun tw->tw_reuse = sk->sk_reuse;
182*4882a593Smuzhiyun tw->tw_reuseport = sk->sk_reuseport;
183*4882a593Smuzhiyun tw->tw_hash = sk->sk_hash;
184*4882a593Smuzhiyun tw->tw_ipv6only = 0;
185*4882a593Smuzhiyun tw->tw_transparent = inet->transparent;
186*4882a593Smuzhiyun tw->tw_prot = sk->sk_prot_creator;
187*4882a593Smuzhiyun atomic64_set(&tw->tw_cookie, atomic64_read(&sk->sk_cookie));
188*4882a593Smuzhiyun twsk_net_set(tw, sock_net(sk));
189*4882a593Smuzhiyun timer_setup(&tw->tw_timer, tw_timer_handler, TIMER_PINNED);
190*4882a593Smuzhiyun /*
191*4882a593Smuzhiyun * Because we use RCU lookups, we should not set tw_refcnt
192*4882a593Smuzhiyun * to a non null value before everything is setup for this
193*4882a593Smuzhiyun * timewait socket.
194*4882a593Smuzhiyun */
195*4882a593Smuzhiyun refcount_set(&tw->tw_refcnt, 0);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun __module_get(tw->tw_prot->owner);
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun return tw;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(inet_twsk_alloc);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun /* These are always called from BH context. See callers in
205*4882a593Smuzhiyun * tcp_input.c to verify this.
206*4882a593Smuzhiyun */
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun /* This is for handling early-kills of TIME_WAIT sockets.
209*4882a593Smuzhiyun * Warning : consume reference.
210*4882a593Smuzhiyun * Caller should not access tw anymore.
211*4882a593Smuzhiyun */
inet_twsk_deschedule_put(struct inet_timewait_sock * tw)212*4882a593Smuzhiyun void inet_twsk_deschedule_put(struct inet_timewait_sock *tw)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun if (del_timer_sync(&tw->tw_timer))
215*4882a593Smuzhiyun inet_twsk_kill(tw);
216*4882a593Smuzhiyun inet_twsk_put(tw);
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun EXPORT_SYMBOL(inet_twsk_deschedule_put);
219*4882a593Smuzhiyun
__inet_twsk_schedule(struct inet_timewait_sock * tw,int timeo,bool rearm)220*4882a593Smuzhiyun void __inet_twsk_schedule(struct inet_timewait_sock *tw, int timeo, bool rearm)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun /* timeout := RTO * 3.5
223*4882a593Smuzhiyun *
224*4882a593Smuzhiyun * 3.5 = 1+2+0.5 to wait for two retransmits.
225*4882a593Smuzhiyun *
226*4882a593Smuzhiyun * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
227*4882a593Smuzhiyun * our ACK acking that FIN can be lost. If N subsequent retransmitted
228*4882a593Smuzhiyun * FINs (or previous seqments) are lost (probability of such event
229*4882a593Smuzhiyun * is p^(N+1), where p is probability to lose single packet and
230*4882a593Smuzhiyun * time to detect the loss is about RTO*(2^N - 1) with exponential
231*4882a593Smuzhiyun * backoff). Normal timewait length is calculated so, that we
232*4882a593Smuzhiyun * waited at least for one retransmitted FIN (maximal RTO is 120sec).
233*4882a593Smuzhiyun * [ BTW Linux. following BSD, violates this requirement waiting
234*4882a593Smuzhiyun * only for 60sec, we should wait at least for 240 secs.
235*4882a593Smuzhiyun * Well, 240 consumes too much of resources 8)
236*4882a593Smuzhiyun * ]
237*4882a593Smuzhiyun * This interval is not reduced to catch old duplicate and
238*4882a593Smuzhiyun * responces to our wandering segments living for two MSLs.
239*4882a593Smuzhiyun * However, if we use PAWS to detect
240*4882a593Smuzhiyun * old duplicates, we can reduce the interval to bounds required
241*4882a593Smuzhiyun * by RTO, rather than MSL. So, if peer understands PAWS, we
242*4882a593Smuzhiyun * kill tw bucket after 3.5*RTO (it is important that this number
243*4882a593Smuzhiyun * is greater than TS tick!) and detect old duplicates with help
244*4882a593Smuzhiyun * of PAWS.
245*4882a593Smuzhiyun */
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun tw->tw_kill = timeo <= 4*HZ;
248*4882a593Smuzhiyun if (!rearm) {
249*4882a593Smuzhiyun BUG_ON(mod_timer(&tw->tw_timer, jiffies + timeo));
250*4882a593Smuzhiyun atomic_inc(&tw->tw_dr->tw_count);
251*4882a593Smuzhiyun } else {
252*4882a593Smuzhiyun mod_timer_pending(&tw->tw_timer, jiffies + timeo);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__inet_twsk_schedule);
256*4882a593Smuzhiyun
inet_twsk_purge(struct inet_hashinfo * hashinfo,int family)257*4882a593Smuzhiyun void inet_twsk_purge(struct inet_hashinfo *hashinfo, int family)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun struct inet_timewait_sock *tw;
260*4882a593Smuzhiyun struct sock *sk;
261*4882a593Smuzhiyun struct hlist_nulls_node *node;
262*4882a593Smuzhiyun unsigned int slot;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
265*4882a593Smuzhiyun struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
266*4882a593Smuzhiyun restart_rcu:
267*4882a593Smuzhiyun cond_resched();
268*4882a593Smuzhiyun rcu_read_lock();
269*4882a593Smuzhiyun restart:
270*4882a593Smuzhiyun sk_nulls_for_each_rcu(sk, node, &head->chain) {
271*4882a593Smuzhiyun if (sk->sk_state != TCP_TIME_WAIT)
272*4882a593Smuzhiyun continue;
273*4882a593Smuzhiyun tw = inet_twsk(sk);
274*4882a593Smuzhiyun if ((tw->tw_family != family) ||
275*4882a593Smuzhiyun refcount_read(&twsk_net(tw)->count))
276*4882a593Smuzhiyun continue;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun if (unlikely(!refcount_inc_not_zero(&tw->tw_refcnt)))
279*4882a593Smuzhiyun continue;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun if (unlikely((tw->tw_family != family) ||
282*4882a593Smuzhiyun refcount_read(&twsk_net(tw)->count))) {
283*4882a593Smuzhiyun inet_twsk_put(tw);
284*4882a593Smuzhiyun goto restart;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun rcu_read_unlock();
288*4882a593Smuzhiyun local_bh_disable();
289*4882a593Smuzhiyun inet_twsk_deschedule_put(tw);
290*4882a593Smuzhiyun local_bh_enable();
291*4882a593Smuzhiyun goto restart_rcu;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun /* If the nulls value we got at the end of this lookup is
294*4882a593Smuzhiyun * not the expected one, we must restart lookup.
295*4882a593Smuzhiyun * We probably met an item that was moved to another chain.
296*4882a593Smuzhiyun */
297*4882a593Smuzhiyun if (get_nulls_value(node) != slot)
298*4882a593Smuzhiyun goto restart;
299*4882a593Smuzhiyun rcu_read_unlock();
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(inet_twsk_purge);
303