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 <linux/slab.h>
14 #include <linux/module.h>
15 #include <net/inet_hashtables.h>
16 #include <net/inet_timewait_sock.h>
21 * inet_twsk_unhash - unhash a timewait socket from established hash
22 * @tw: timewait socket
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.
28 int inet_twsk_unhash(struct inet_timewait_sock
*tw
)
30 if (hlist_nulls_unhashed(&tw
->tw_node
))
33 hlist_nulls_del_rcu(&tw
->tw_node
);
34 sk_nulls_node_init(&tw
->tw_node
);
36 * We cannot call inet_twsk_put() ourself under lock,
37 * caller must call it for us.
43 * inet_twsk_bind_unhash - unhash a timewait socket from bind hash
44 * @tw: timewait socket
45 * @hashinfo: hashinfo pointer
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.
51 int inet_twsk_bind_unhash(struct inet_timewait_sock
*tw
,
52 struct inet_hashinfo
*hashinfo
)
54 struct inet_bind_bucket
*tb
= tw
->tw_tb
;
59 __hlist_del(&tw
->tw_bind_node
);
61 inet_bind_bucket_destroy(hashinfo
->bind_bucket_cachep
, tb
);
63 * We cannot call inet_twsk_put() ourself under lock,
64 * caller must call it for us.
69 /* Must be called with locally disabled BHs. */
70 static void inet_twsk_kill(struct inet_timewait_sock
*tw
)
72 struct inet_hashinfo
*hashinfo
= tw
->tw_dr
->hashinfo
;
73 struct inet_bind_hashbucket
*bhead
;
75 /* Unlink from established hashes. */
76 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, tw
->tw_hash
);
79 refcnt
= inet_twsk_unhash(tw
);
82 /* Disassociate with bind bucket. */
83 bhead
= &hashinfo
->bhash
[inet_bhashfn(twsk_net(tw
), tw
->tw_num
,
84 hashinfo
->bhash_size
)];
86 spin_lock(&bhead
->lock
);
87 refcnt
+= inet_twsk_bind_unhash(tw
, hashinfo
);
88 spin_unlock(&bhead
->lock
);
90 BUG_ON(refcnt
>= atomic_read(&tw
->tw_refcnt
));
91 atomic_sub(refcnt
, &tw
->tw_refcnt
);
92 atomic_dec(&tw
->tw_dr
->tw_count
);
96 void inet_twsk_free(struct inet_timewait_sock
*tw
)
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
);
103 kmem_cache_free(tw
->tw_prot
->twsk_prot
->twsk_slab
, tw
);
107 void inet_twsk_put(struct inet_timewait_sock
*tw
)
109 if (atomic_dec_and_test(&tw
->tw_refcnt
))
112 EXPORT_SYMBOL_GPL(inet_twsk_put
);
114 static void inet_twsk_add_node_rcu(struct inet_timewait_sock
*tw
,
115 struct hlist_nulls_head
*list
)
117 hlist_nulls_add_head_rcu(&tw
->tw_node
, list
);
120 static void inet_twsk_add_bind_node(struct inet_timewait_sock
*tw
,
121 struct hlist_head
*list
)
123 hlist_add_head(&tw
->tw_bind_node
, list
);
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.
131 void __inet_twsk_hashdance(struct inet_timewait_sock
*tw
, struct sock
*sk
,
132 struct inet_hashinfo
*hashinfo
)
134 const struct inet_sock
*inet
= inet_sk(sk
);
135 const struct inet_connection_sock
*icsk
= inet_csk(sk
);
136 struct inet_ehash_bucket
*ehead
= inet_ehash_bucket(hashinfo
, sk
->sk_hash
);
137 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, sk
->sk_hash
);
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.
143 bhead
= &hashinfo
->bhash
[inet_bhashfn(twsk_net(tw
), inet
->inet_num
,
144 hashinfo
->bhash_size
)];
145 spin_lock(&bhead
->lock
);
146 tw
->tw_tb
= icsk
->icsk_bind_hash
;
147 WARN_ON(!icsk
->icsk_bind_hash
);
148 inet_twsk_add_bind_node(tw
, &tw
->tw_tb
->owners
);
149 spin_unlock(&bhead
->lock
);
154 * Step 2: Hash TW into tcp ehash chain.
156 * - tw_refcnt is set to 4 because :
157 * - We have one reference from bhash chain.
158 * - We have one reference from ehash chain.
159 * - We have one reference from timer.
160 * - One reference for ourself (our caller will release it).
161 * We can use atomic_set() because prior spin_lock()/spin_unlock()
162 * committed into memory all tw fields.
164 atomic_set(&tw
->tw_refcnt
, 4);
165 inet_twsk_add_node_rcu(tw
, &ehead
->chain
);
167 /* Step 3: Remove SK from hash chain */
168 if (__sk_nulls_del_node_init_rcu(sk
))
169 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, -1);
173 EXPORT_SYMBOL_GPL(__inet_twsk_hashdance
);
175 void tw_timer_handler(unsigned long data
)
177 struct inet_timewait_sock
*tw
= (struct inet_timewait_sock
*)data
;
180 NET_INC_STATS_BH(twsk_net(tw
), LINUX_MIB_TIMEWAITKILLED
);
182 NET_INC_STATS_BH(twsk_net(tw
), LINUX_MIB_TIMEWAITED
);
186 struct inet_timewait_sock
*inet_twsk_alloc(const struct sock
*sk
,
187 struct inet_timewait_death_row
*dr
,
190 struct inet_timewait_sock
*tw
;
192 if (atomic_read(&dr
->tw_count
) >= dr
->sysctl_max_tw_buckets
)
195 tw
= kmem_cache_alloc(sk
->sk_prot_creator
->twsk_prot
->twsk_slab
,
198 const struct inet_sock
*inet
= inet_sk(sk
);
200 kmemcheck_annotate_bitfield(tw
, flags
);
203 /* Give us an identity. */
204 tw
->tw_daddr
= inet
->inet_daddr
;
205 tw
->tw_rcv_saddr
= inet
->inet_rcv_saddr
;
206 tw
->tw_bound_dev_if
= sk
->sk_bound_dev_if
;
207 tw
->tw_tos
= inet
->tos
;
208 tw
->tw_num
= inet
->inet_num
;
209 tw
->tw_state
= TCP_TIME_WAIT
;
210 tw
->tw_substate
= state
;
211 tw
->tw_sport
= inet
->inet_sport
;
212 tw
->tw_dport
= inet
->inet_dport
;
213 tw
->tw_family
= sk
->sk_family
;
214 tw
->tw_reuse
= sk
->sk_reuse
;
215 tw
->tw_hash
= sk
->sk_hash
;
217 tw
->tw_transparent
= inet
->transparent
;
218 tw
->tw_prot
= sk
->sk_prot_creator
;
219 atomic64_set(&tw
->tw_cookie
, atomic64_read(&sk
->sk_cookie
));
220 twsk_net_set(tw
, sock_net(sk
));
221 setup_timer(&tw
->tw_timer
, tw_timer_handler
, (unsigned long)tw
);
223 * Because we use RCU lookups, we should not set tw_refcnt
224 * to a non null value before everything is setup for this
227 atomic_set(&tw
->tw_refcnt
, 0);
229 __module_get(tw
->tw_prot
->owner
);
234 EXPORT_SYMBOL_GPL(inet_twsk_alloc
);
236 /* These are always called from BH context. See callers in
237 * tcp_input.c to verify this.
240 /* This is for handling early-kills of TIME_WAIT sockets. */
241 void inet_twsk_deschedule(struct inet_timewait_sock
*tw
)
243 if (del_timer_sync(&tw
->tw_timer
))
246 EXPORT_SYMBOL(inet_twsk_deschedule
);
248 void __inet_twsk_schedule(struct inet_timewait_sock
*tw
, int timeo
, bool rearm
)
250 /* timeout := RTO * 3.5
252 * 3.5 = 1+2+0.5 to wait for two retransmits.
254 * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
255 * our ACK acking that FIN can be lost. If N subsequent retransmitted
256 * FINs (or previous seqments) are lost (probability of such event
257 * is p^(N+1), where p is probability to lose single packet and
258 * time to detect the loss is about RTO*(2^N - 1) with exponential
259 * backoff). Normal timewait length is calculated so, that we
260 * waited at least for one retransmitted FIN (maximal RTO is 120sec).
261 * [ BTW Linux. following BSD, violates this requirement waiting
262 * only for 60sec, we should wait at least for 240 secs.
263 * Well, 240 consumes too much of resources 8)
265 * This interval is not reduced to catch old duplicate and
266 * responces to our wandering segments living for two MSLs.
267 * However, if we use PAWS to detect
268 * old duplicates, we can reduce the interval to bounds required
269 * by RTO, rather than MSL. So, if peer understands PAWS, we
270 * kill tw bucket after 3.5*RTO (it is important that this number
271 * is greater than TS tick!) and detect old duplicates with help
275 tw
->tw_kill
= timeo
<= 4*HZ
;
277 BUG_ON(mod_timer_pinned(&tw
->tw_timer
, jiffies
+ timeo
));
278 atomic_inc(&tw
->tw_dr
->tw_count
);
280 mod_timer_pending(&tw
->tw_timer
, jiffies
+ timeo
);
283 EXPORT_SYMBOL_GPL(__inet_twsk_schedule
);
285 void inet_twsk_purge(struct inet_hashinfo
*hashinfo
,
286 struct inet_timewait_death_row
*twdr
, int family
)
288 struct inet_timewait_sock
*tw
;
290 struct hlist_nulls_node
*node
;
293 for (slot
= 0; slot
<= hashinfo
->ehash_mask
; slot
++) {
294 struct inet_ehash_bucket
*head
= &hashinfo
->ehash
[slot
];
299 sk_nulls_for_each_rcu(sk
, node
, &head
->chain
) {
300 if (sk
->sk_state
!= TCP_TIME_WAIT
)
303 if ((tw
->tw_family
!= family
) ||
304 atomic_read(&twsk_net(tw
)->count
))
307 if (unlikely(!atomic_inc_not_zero(&tw
->tw_refcnt
)))
310 if (unlikely((tw
->tw_family
!= family
) ||
311 atomic_read(&twsk_net(tw
)->count
))) {
318 inet_twsk_deschedule(tw
);
323 /* If the nulls value we got at the end of this lookup is
324 * not the expected one, we must restart lookup.
325 * We probably met an item that was moved to another chain.
327 if (get_nulls_value(node
) != slot
)
332 EXPORT_SYMBOL_GPL(inet_twsk_purge
);