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 <net/inet_hashtables.h>
14 #include <net/inet_timewait_sock.h>
19 * unhash a timewait socket from established hash
20 * lock must be hold by caller
22 int inet_twsk_unhash(struct inet_timewait_sock
*tw
)
24 if (hlist_nulls_unhashed(&tw
->tw_node
))
27 hlist_nulls_del_rcu(&tw
->tw_node
);
28 sk_nulls_node_init(&tw
->tw_node
);
32 /* Must be called with locally disabled BHs. */
33 static void __inet_twsk_kill(struct inet_timewait_sock
*tw
,
34 struct inet_hashinfo
*hashinfo
)
36 struct inet_bind_hashbucket
*bhead
;
37 struct inet_bind_bucket
*tb
;
39 /* Unlink from established hashes. */
40 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, tw
->tw_hash
);
43 refcnt
= inet_twsk_unhash(tw
);
46 /* Disassociate with bind bucket. */
47 bhead
= &hashinfo
->bhash
[inet_bhashfn(twsk_net(tw
), tw
->tw_num
,
48 hashinfo
->bhash_size
)];
49 spin_lock(&bhead
->lock
);
52 __hlist_del(&tw
->tw_bind_node
);
54 inet_bind_bucket_destroy(hashinfo
->bind_bucket_cachep
, tb
);
57 spin_unlock(&bhead
->lock
);
58 #ifdef SOCK_REFCNT_DEBUG
59 if (atomic_read(&tw
->tw_refcnt
) != 1) {
60 printk(KERN_DEBUG
"%s timewait_sock %p refcnt=%d\n",
61 tw
->tw_prot
->name
, tw
, atomic_read(&tw
->tw_refcnt
));
70 static noinline
void inet_twsk_free(struct inet_timewait_sock
*tw
)
72 struct module
*owner
= tw
->tw_prot
->owner
;
73 twsk_destructor((struct sock
*)tw
);
74 #ifdef SOCK_REFCNT_DEBUG
75 pr_debug("%s timewait_sock %p released\n", tw
->tw_prot
->name
, tw
);
77 release_net(twsk_net(tw
));
78 kmem_cache_free(tw
->tw_prot
->twsk_prot
->twsk_slab
, tw
);
82 void inet_twsk_put(struct inet_timewait_sock
*tw
)
84 if (atomic_dec_and_test(&tw
->tw_refcnt
))
87 EXPORT_SYMBOL_GPL(inet_twsk_put
);
90 * Enter the time wait state. This is called with locally disabled BH.
91 * Essentially we whip up a timewait bucket, copy the relevant info into it
92 * from the SK, and mess with hash chains and list linkage.
94 void __inet_twsk_hashdance(struct inet_timewait_sock
*tw
, struct sock
*sk
,
95 struct inet_hashinfo
*hashinfo
)
97 const struct inet_sock
*inet
= inet_sk(sk
);
98 const struct inet_connection_sock
*icsk
= inet_csk(sk
);
99 struct inet_ehash_bucket
*ehead
= inet_ehash_bucket(hashinfo
, sk
->sk_hash
);
100 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, sk
->sk_hash
);
101 struct inet_bind_hashbucket
*bhead
;
102 /* Step 1: Put TW into bind hash. Original socket stays there too.
103 Note, that any socket with inet->num != 0 MUST be bound in
104 binding cache, even if it is closed.
106 bhead
= &hashinfo
->bhash
[inet_bhashfn(twsk_net(tw
), inet
->inet_num
,
107 hashinfo
->bhash_size
)];
108 spin_lock(&bhead
->lock
);
109 tw
->tw_tb
= icsk
->icsk_bind_hash
;
110 WARN_ON(!icsk
->icsk_bind_hash
);
111 inet_twsk_add_bind_node(tw
, &tw
->tw_tb
->owners
);
112 spin_unlock(&bhead
->lock
);
117 * Step 2: Hash TW into TIMEWAIT chain.
118 * Should be done before removing sk from established chain
119 * because readers are lockless and search established first.
121 inet_twsk_add_node_rcu(tw
, &ehead
->twchain
);
123 /* Step 3: Remove SK from established hash. */
124 if (__sk_nulls_del_node_init_rcu(sk
))
125 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, -1);
129 * - We initially set tw_refcnt to 0 in inet_twsk_alloc()
130 * - We add one reference for the bhash link
131 * - We add one reference for the ehash link
132 * - We want this refcnt update done before allowing other
133 * threads to find this tw in ehash chain.
135 atomic_add(1 + 1 + 1, &tw
->tw_refcnt
);
140 EXPORT_SYMBOL_GPL(__inet_twsk_hashdance
);
142 struct inet_timewait_sock
*inet_twsk_alloc(const struct sock
*sk
, const int state
)
144 struct inet_timewait_sock
*tw
=
145 kmem_cache_alloc(sk
->sk_prot_creator
->twsk_prot
->twsk_slab
,
148 const struct inet_sock
*inet
= inet_sk(sk
);
150 kmemcheck_annotate_bitfield(tw
, flags
);
152 /* Give us an identity. */
153 tw
->tw_daddr
= inet
->inet_daddr
;
154 tw
->tw_rcv_saddr
= inet
->inet_rcv_saddr
;
155 tw
->tw_bound_dev_if
= sk
->sk_bound_dev_if
;
156 tw
->tw_num
= inet
->inet_num
;
157 tw
->tw_state
= TCP_TIME_WAIT
;
158 tw
->tw_substate
= state
;
159 tw
->tw_sport
= inet
->inet_sport
;
160 tw
->tw_dport
= inet
->inet_dport
;
161 tw
->tw_family
= sk
->sk_family
;
162 tw
->tw_reuse
= sk
->sk_reuse
;
163 tw
->tw_hash
= sk
->sk_hash
;
165 tw
->tw_transparent
= inet
->transparent
;
166 tw
->tw_prot
= sk
->sk_prot_creator
;
167 twsk_net_set(tw
, hold_net(sock_net(sk
)));
169 * Because we use RCU lookups, we should not set tw_refcnt
170 * to a non null value before everything is setup for this
173 atomic_set(&tw
->tw_refcnt
, 0);
174 inet_twsk_dead_node_init(tw
);
175 __module_get(tw
->tw_prot
->owner
);
181 EXPORT_SYMBOL_GPL(inet_twsk_alloc
);
183 /* Returns non-zero if quota exceeded. */
184 static int inet_twdr_do_twkill_work(struct inet_timewait_death_row
*twdr
,
187 struct inet_timewait_sock
*tw
;
188 struct hlist_node
*node
;
192 /* NOTE: compare this to previous version where lock
193 * was released after detaching chain. It was racy,
194 * because tw buckets are scheduled in not serialized context
195 * in 2.3 (with netfilter), and with softnet it is common, because
196 * soft irqs are not sequenced.
201 inet_twsk_for_each_inmate(tw
, node
, &twdr
->cells
[slot
]) {
202 __inet_twsk_del_dead_node(tw
);
203 spin_unlock(&twdr
->death_lock
);
204 __inet_twsk_kill(tw
, twdr
->hashinfo
);
206 NET_INC_STATS_BH(twsk_net(tw
), LINUX_MIB_TIMEWAITED
);
210 spin_lock(&twdr
->death_lock
);
211 if (killed
> INET_TWDR_TWKILL_QUOTA
) {
216 /* While we dropped twdr->death_lock, another cpu may have
217 * killed off the next TW bucket in the list, therefore
218 * do a fresh re-read of the hlist head node with the
219 * lock reacquired. We still use the hlist traversal
220 * macro in order to get the prefetches.
225 twdr
->tw_count
-= killed
;
226 #ifndef CONFIG_NET_NS
227 NET_ADD_STATS_BH(&init_net
, LINUX_MIB_TIMEWAITED
, killed
);
232 void inet_twdr_hangman(unsigned long data
)
234 struct inet_timewait_death_row
*twdr
;
235 int unsigned need_timer
;
237 twdr
= (struct inet_timewait_death_row
*)data
;
238 spin_lock(&twdr
->death_lock
);
240 if (twdr
->tw_count
== 0)
244 if (inet_twdr_do_twkill_work(twdr
, twdr
->slot
)) {
245 twdr
->thread_slots
|= (1 << twdr
->slot
);
246 schedule_work(&twdr
->twkill_work
);
249 /* We purged the entire slot, anything left? */
252 twdr
->slot
= ((twdr
->slot
+ 1) & (INET_TWDR_TWKILL_SLOTS
- 1));
255 mod_timer(&twdr
->tw_timer
, jiffies
+ twdr
->period
);
257 spin_unlock(&twdr
->death_lock
);
260 EXPORT_SYMBOL_GPL(inet_twdr_hangman
);
262 void inet_twdr_twkill_work(struct work_struct
*work
)
264 struct inet_timewait_death_row
*twdr
=
265 container_of(work
, struct inet_timewait_death_row
, twkill_work
);
268 BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS
- 1) >
269 (sizeof(twdr
->thread_slots
) * 8));
271 while (twdr
->thread_slots
) {
272 spin_lock_bh(&twdr
->death_lock
);
273 for (i
= 0; i
< INET_TWDR_TWKILL_SLOTS
; i
++) {
274 if (!(twdr
->thread_slots
& (1 << i
)))
277 while (inet_twdr_do_twkill_work(twdr
, i
) != 0) {
278 if (need_resched()) {
279 spin_unlock_bh(&twdr
->death_lock
);
281 spin_lock_bh(&twdr
->death_lock
);
285 twdr
->thread_slots
&= ~(1 << i
);
287 spin_unlock_bh(&twdr
->death_lock
);
291 EXPORT_SYMBOL_GPL(inet_twdr_twkill_work
);
293 /* These are always called from BH context. See callers in
294 * tcp_input.c to verify this.
297 /* This is for handling early-kills of TIME_WAIT sockets. */
298 void inet_twsk_deschedule(struct inet_timewait_sock
*tw
,
299 struct inet_timewait_death_row
*twdr
)
301 spin_lock(&twdr
->death_lock
);
302 if (inet_twsk_del_dead_node(tw
)) {
304 if (--twdr
->tw_count
== 0)
305 del_timer(&twdr
->tw_timer
);
307 spin_unlock(&twdr
->death_lock
);
308 __inet_twsk_kill(tw
, twdr
->hashinfo
);
311 EXPORT_SYMBOL(inet_twsk_deschedule
);
313 void inet_twsk_schedule(struct inet_timewait_sock
*tw
,
314 struct inet_timewait_death_row
*twdr
,
315 const int timeo
, const int timewait_len
)
317 struct hlist_head
*list
;
320 /* timeout := RTO * 3.5
322 * 3.5 = 1+2+0.5 to wait for two retransmits.
324 * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
325 * our ACK acking that FIN can be lost. If N subsequent retransmitted
326 * FINs (or previous seqments) are lost (probability of such event
327 * is p^(N+1), where p is probability to lose single packet and
328 * time to detect the loss is about RTO*(2^N - 1) with exponential
329 * backoff). Normal timewait length is calculated so, that we
330 * waited at least for one retransmitted FIN (maximal RTO is 120sec).
331 * [ BTW Linux. following BSD, violates this requirement waiting
332 * only for 60sec, we should wait at least for 240 secs.
333 * Well, 240 consumes too much of resources 8)
335 * This interval is not reduced to catch old duplicate and
336 * responces to our wandering segments living for two MSLs.
337 * However, if we use PAWS to detect
338 * old duplicates, we can reduce the interval to bounds required
339 * by RTO, rather than MSL. So, if peer understands PAWS, we
340 * kill tw bucket after 3.5*RTO (it is important that this number
341 * is greater than TS tick!) and detect old duplicates with help
344 slot
= (timeo
+ (1 << INET_TWDR_RECYCLE_TICK
) - 1) >> INET_TWDR_RECYCLE_TICK
;
346 spin_lock(&twdr
->death_lock
);
348 /* Unlink it, if it was scheduled */
349 if (inet_twsk_del_dead_node(tw
))
352 atomic_inc(&tw
->tw_refcnt
);
354 if (slot
>= INET_TWDR_RECYCLE_SLOTS
) {
355 /* Schedule to slow timer */
356 if (timeo
>= timewait_len
) {
357 slot
= INET_TWDR_TWKILL_SLOTS
- 1;
359 slot
= DIV_ROUND_UP(timeo
, twdr
->period
);
360 if (slot
>= INET_TWDR_TWKILL_SLOTS
)
361 slot
= INET_TWDR_TWKILL_SLOTS
- 1;
363 tw
->tw_ttd
= jiffies
+ timeo
;
364 slot
= (twdr
->slot
+ slot
) & (INET_TWDR_TWKILL_SLOTS
- 1);
365 list
= &twdr
->cells
[slot
];
367 tw
->tw_ttd
= jiffies
+ (slot
<< INET_TWDR_RECYCLE_TICK
);
369 if (twdr
->twcal_hand
< 0) {
370 twdr
->twcal_hand
= 0;
371 twdr
->twcal_jiffie
= jiffies
;
372 twdr
->twcal_timer
.expires
= twdr
->twcal_jiffie
+
373 (slot
<< INET_TWDR_RECYCLE_TICK
);
374 add_timer(&twdr
->twcal_timer
);
376 if (time_after(twdr
->twcal_timer
.expires
,
377 jiffies
+ (slot
<< INET_TWDR_RECYCLE_TICK
)))
378 mod_timer(&twdr
->twcal_timer
,
379 jiffies
+ (slot
<< INET_TWDR_RECYCLE_TICK
));
380 slot
= (twdr
->twcal_hand
+ slot
) & (INET_TWDR_RECYCLE_SLOTS
- 1);
382 list
= &twdr
->twcal_row
[slot
];
385 hlist_add_head(&tw
->tw_death_node
, list
);
387 if (twdr
->tw_count
++ == 0)
388 mod_timer(&twdr
->tw_timer
, jiffies
+ twdr
->period
);
389 spin_unlock(&twdr
->death_lock
);
392 EXPORT_SYMBOL_GPL(inet_twsk_schedule
);
394 void inet_twdr_twcal_tick(unsigned long data
)
396 struct inet_timewait_death_row
*twdr
;
399 unsigned long now
= jiffies
;
403 twdr
= (struct inet_timewait_death_row
*)data
;
405 spin_lock(&twdr
->death_lock
);
406 if (twdr
->twcal_hand
< 0)
409 slot
= twdr
->twcal_hand
;
410 j
= twdr
->twcal_jiffie
;
412 for (n
= 0; n
< INET_TWDR_RECYCLE_SLOTS
; n
++) {
413 if (time_before_eq(j
, now
)) {
414 struct hlist_node
*node
, *safe
;
415 struct inet_timewait_sock
*tw
;
417 inet_twsk_for_each_inmate_safe(tw
, node
, safe
,
418 &twdr
->twcal_row
[slot
]) {
419 __inet_twsk_del_dead_node(tw
);
420 __inet_twsk_kill(tw
, twdr
->hashinfo
);
422 NET_INC_STATS_BH(twsk_net(tw
), LINUX_MIB_TIMEWAITKILLED
);
430 twdr
->twcal_jiffie
= j
;
431 twdr
->twcal_hand
= slot
;
434 if (!hlist_empty(&twdr
->twcal_row
[slot
])) {
435 mod_timer(&twdr
->twcal_timer
, j
);
439 j
+= 1 << INET_TWDR_RECYCLE_TICK
;
440 slot
= (slot
+ 1) & (INET_TWDR_RECYCLE_SLOTS
- 1);
442 twdr
->twcal_hand
= -1;
445 if ((twdr
->tw_count
-= killed
) == 0)
446 del_timer(&twdr
->tw_timer
);
447 #ifndef CONFIG_NET_NS
448 NET_ADD_STATS_BH(&init_net
, LINUX_MIB_TIMEWAITKILLED
, killed
);
450 spin_unlock(&twdr
->death_lock
);
453 EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick
);
455 void inet_twsk_purge(struct inet_hashinfo
*hashinfo
,
456 struct inet_timewait_death_row
*twdr
, int family
)
458 struct inet_timewait_sock
*tw
;
460 struct hlist_nulls_node
*node
;
463 for (slot
= 0; slot
<= hashinfo
->ehash_mask
; slot
++) {
464 struct inet_ehash_bucket
*head
= &hashinfo
->ehash
[slot
];
468 sk_nulls_for_each_rcu(sk
, node
, &head
->twchain
) {
470 if ((tw
->tw_family
!= family
) ||
471 atomic_read(&twsk_net(tw
)->count
))
474 if (unlikely(!atomic_inc_not_zero(&tw
->tw_refcnt
)))
477 if (unlikely((tw
->tw_family
!= family
) ||
478 atomic_read(&twsk_net(tw
)->count
))) {
484 inet_twsk_deschedule(tw
, twdr
);
488 /* If the nulls value we got at the end of this lookup is
489 * not the expected one, we must restart lookup.
490 * We probably met an item that was moved to another chain.
492 if (get_nulls_value(node
) != slot
)
497 EXPORT_SYMBOL_GPL(inet_twsk_purge
);