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 <net/inet_hashtables.h>
13 #include <net/inet_timewait_sock.h>
16 /* Must be called with locally disabled BHs. */
17 static void __inet_twsk_kill(struct inet_timewait_sock
*tw
,
18 struct inet_hashinfo
*hashinfo
)
20 struct inet_bind_hashbucket
*bhead
;
21 struct inet_bind_bucket
*tb
;
22 /* Unlink from established hashes. */
23 struct inet_ehash_bucket
*ehead
= inet_ehash_bucket(hashinfo
, tw
->tw_hash
);
25 write_lock(&ehead
->lock
);
26 if (hlist_unhashed(&tw
->tw_node
)) {
27 write_unlock(&ehead
->lock
);
30 __hlist_del(&tw
->tw_node
);
31 sk_node_init(&tw
->tw_node
);
32 write_unlock(&ehead
->lock
);
34 /* Disassociate with bind bucket. */
35 bhead
= &hashinfo
->bhash
[inet_bhashfn(tw
->tw_num
, hashinfo
->bhash_size
)];
36 spin_lock(&bhead
->lock
);
38 __hlist_del(&tw
->tw_bind_node
);
40 inet_bind_bucket_destroy(hashinfo
->bind_bucket_cachep
, tb
);
41 spin_unlock(&bhead
->lock
);
42 #ifdef SOCK_REFCNT_DEBUG
43 if (atomic_read(&tw
->tw_refcnt
) != 1) {
44 printk(KERN_DEBUG
"%s timewait_sock %p refcnt=%d\n",
45 tw
->tw_prot
->name
, tw
, atomic_read(&tw
->tw_refcnt
));
52 * Enter the time wait state. This is called with locally disabled BH.
53 * Essentially we whip up a timewait bucket, copy the relevant info into it
54 * from the SK, and mess with hash chains and list linkage.
56 void __inet_twsk_hashdance(struct inet_timewait_sock
*tw
, struct sock
*sk
,
57 struct inet_hashinfo
*hashinfo
)
59 const struct inet_sock
*inet
= inet_sk(sk
);
60 const struct inet_connection_sock
*icsk
= inet_csk(sk
);
61 struct inet_ehash_bucket
*ehead
= inet_ehash_bucket(hashinfo
, sk
->sk_hash
);
62 struct inet_bind_hashbucket
*bhead
;
63 /* Step 1: Put TW into bind hash. Original socket stays there too.
64 Note, that any socket with inet->num != 0 MUST be bound in
65 binding cache, even if it is closed.
67 bhead
= &hashinfo
->bhash
[inet_bhashfn(inet
->num
, hashinfo
->bhash_size
)];
68 spin_lock(&bhead
->lock
);
69 tw
->tw_tb
= icsk
->icsk_bind_hash
;
70 BUG_TRAP(icsk
->icsk_bind_hash
);
71 inet_twsk_add_bind_node(tw
, &tw
->tw_tb
->owners
);
72 spin_unlock(&bhead
->lock
);
74 write_lock(&ehead
->lock
);
76 /* Step 2: Remove SK from established hash. */
77 if (__sk_del_node_init(sk
))
78 sock_prot_dec_use(sk
->sk_prot
);
80 /* Step 3: Hash TW into TIMEWAIT chain. */
81 inet_twsk_add_node(tw
, &ehead
->twchain
);
82 atomic_inc(&tw
->tw_refcnt
);
84 write_unlock(&ehead
->lock
);
87 EXPORT_SYMBOL_GPL(__inet_twsk_hashdance
);
89 struct inet_timewait_sock
*inet_twsk_alloc(const struct sock
*sk
, const int state
)
91 struct inet_timewait_sock
*tw
=
92 kmem_cache_alloc(sk
->sk_prot_creator
->twsk_prot
->twsk_slab
,
95 const struct inet_sock
*inet
= inet_sk(sk
);
97 /* Give us an identity. */
98 tw
->tw_daddr
= inet
->daddr
;
99 tw
->tw_rcv_saddr
= inet
->rcv_saddr
;
100 tw
->tw_bound_dev_if
= sk
->sk_bound_dev_if
;
101 tw
->tw_num
= inet
->num
;
102 tw
->tw_state
= TCP_TIME_WAIT
;
103 tw
->tw_substate
= state
;
104 tw
->tw_sport
= inet
->sport
;
105 tw
->tw_dport
= inet
->dport
;
106 tw
->tw_family
= sk
->sk_family
;
107 tw
->tw_reuse
= sk
->sk_reuse
;
108 tw
->tw_hash
= sk
->sk_hash
;
110 tw
->tw_prot
= sk
->sk_prot_creator
;
111 atomic_set(&tw
->tw_refcnt
, 1);
112 inet_twsk_dead_node_init(tw
);
113 __module_get(tw
->tw_prot
->owner
);
119 EXPORT_SYMBOL_GPL(inet_twsk_alloc
);
121 /* Returns non-zero if quota exceeded. */
122 static int inet_twdr_do_twkill_work(struct inet_timewait_death_row
*twdr
,
125 struct inet_timewait_sock
*tw
;
126 struct hlist_node
*node
;
130 /* NOTE: compare this to previous version where lock
131 * was released after detaching chain. It was racy,
132 * because tw buckets are scheduled in not serialized context
133 * in 2.3 (with netfilter), and with softnet it is common, because
134 * soft irqs are not sequenced.
139 inet_twsk_for_each_inmate(tw
, node
, &twdr
->cells
[slot
]) {
140 __inet_twsk_del_dead_node(tw
);
141 spin_unlock(&twdr
->death_lock
);
142 __inet_twsk_kill(tw
, twdr
->hashinfo
);
145 spin_lock(&twdr
->death_lock
);
146 if (killed
> INET_TWDR_TWKILL_QUOTA
) {
151 /* While we dropped twdr->death_lock, another cpu may have
152 * killed off the next TW bucket in the list, therefore
153 * do a fresh re-read of the hlist head node with the
154 * lock reacquired. We still use the hlist traversal
155 * macro in order to get the prefetches.
160 twdr
->tw_count
-= killed
;
161 NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITED
, killed
);
166 void inet_twdr_hangman(unsigned long data
)
168 struct inet_timewait_death_row
*twdr
;
169 int unsigned need_timer
;
171 twdr
= (struct inet_timewait_death_row
*)data
;
172 spin_lock(&twdr
->death_lock
);
174 if (twdr
->tw_count
== 0)
178 if (inet_twdr_do_twkill_work(twdr
, twdr
->slot
)) {
179 twdr
->thread_slots
|= (1 << twdr
->slot
);
180 schedule_work(&twdr
->twkill_work
);
183 /* We purged the entire slot, anything left? */
187 twdr
->slot
= ((twdr
->slot
+ 1) & (INET_TWDR_TWKILL_SLOTS
- 1));
189 mod_timer(&twdr
->tw_timer
, jiffies
+ twdr
->period
);
191 spin_unlock(&twdr
->death_lock
);
194 EXPORT_SYMBOL_GPL(inet_twdr_hangman
);
196 extern void twkill_slots_invalid(void);
198 void inet_twdr_twkill_work(struct work_struct
*work
)
200 struct inet_timewait_death_row
*twdr
=
201 container_of(work
, struct inet_timewait_death_row
, twkill_work
);
204 if ((INET_TWDR_TWKILL_SLOTS
- 1) > (sizeof(twdr
->thread_slots
) * 8))
205 twkill_slots_invalid();
207 while (twdr
->thread_slots
) {
208 spin_lock_bh(&twdr
->death_lock
);
209 for (i
= 0; i
< INET_TWDR_TWKILL_SLOTS
; i
++) {
210 if (!(twdr
->thread_slots
& (1 << i
)))
213 while (inet_twdr_do_twkill_work(twdr
, i
) != 0) {
214 if (need_resched()) {
215 spin_unlock_bh(&twdr
->death_lock
);
217 spin_lock_bh(&twdr
->death_lock
);
221 twdr
->thread_slots
&= ~(1 << i
);
223 spin_unlock_bh(&twdr
->death_lock
);
227 EXPORT_SYMBOL_GPL(inet_twdr_twkill_work
);
229 /* These are always called from BH context. See callers in
230 * tcp_input.c to verify this.
233 /* This is for handling early-kills of TIME_WAIT sockets. */
234 void inet_twsk_deschedule(struct inet_timewait_sock
*tw
,
235 struct inet_timewait_death_row
*twdr
)
237 spin_lock(&twdr
->death_lock
);
238 if (inet_twsk_del_dead_node(tw
)) {
240 if (--twdr
->tw_count
== 0)
241 del_timer(&twdr
->tw_timer
);
243 spin_unlock(&twdr
->death_lock
);
244 __inet_twsk_kill(tw
, twdr
->hashinfo
);
247 EXPORT_SYMBOL(inet_twsk_deschedule
);
249 void inet_twsk_schedule(struct inet_timewait_sock
*tw
,
250 struct inet_timewait_death_row
*twdr
,
251 const int timeo
, const int timewait_len
)
253 struct hlist_head
*list
;
256 /* timeout := RTO * 3.5
258 * 3.5 = 1+2+0.5 to wait for two retransmits.
260 * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
261 * our ACK acking that FIN can be lost. If N subsequent retransmitted
262 * FINs (or previous seqments) are lost (probability of such event
263 * is p^(N+1), where p is probability to lose single packet and
264 * time to detect the loss is about RTO*(2^N - 1) with exponential
265 * backoff). Normal timewait length is calculated so, that we
266 * waited at least for one retransmitted FIN (maximal RTO is 120sec).
267 * [ BTW Linux. following BSD, violates this requirement waiting
268 * only for 60sec, we should wait at least for 240 secs.
269 * Well, 240 consumes too much of resources 8)
271 * This interval is not reduced to catch old duplicate and
272 * responces to our wandering segments living for two MSLs.
273 * However, if we use PAWS to detect
274 * old duplicates, we can reduce the interval to bounds required
275 * by RTO, rather than MSL. So, if peer understands PAWS, we
276 * kill tw bucket after 3.5*RTO (it is important that this number
277 * is greater than TS tick!) and detect old duplicates with help
280 slot
= (timeo
+ (1 << INET_TWDR_RECYCLE_TICK
) - 1) >> INET_TWDR_RECYCLE_TICK
;
282 spin_lock(&twdr
->death_lock
);
284 /* Unlink it, if it was scheduled */
285 if (inet_twsk_del_dead_node(tw
))
288 atomic_inc(&tw
->tw_refcnt
);
290 if (slot
>= INET_TWDR_RECYCLE_SLOTS
) {
291 /* Schedule to slow timer */
292 if (timeo
>= timewait_len
) {
293 slot
= INET_TWDR_TWKILL_SLOTS
- 1;
295 slot
= DIV_ROUND_UP(timeo
, twdr
->period
);
296 if (slot
>= INET_TWDR_TWKILL_SLOTS
)
297 slot
= INET_TWDR_TWKILL_SLOTS
- 1;
299 tw
->tw_ttd
= jiffies
+ timeo
;
300 slot
= (twdr
->slot
+ slot
) & (INET_TWDR_TWKILL_SLOTS
- 1);
301 list
= &twdr
->cells
[slot
];
303 tw
->tw_ttd
= jiffies
+ (slot
<< INET_TWDR_RECYCLE_TICK
);
305 if (twdr
->twcal_hand
< 0) {
306 twdr
->twcal_hand
= 0;
307 twdr
->twcal_jiffie
= jiffies
;
308 twdr
->twcal_timer
.expires
= twdr
->twcal_jiffie
+
309 (slot
<< INET_TWDR_RECYCLE_TICK
);
310 add_timer(&twdr
->twcal_timer
);
312 if (time_after(twdr
->twcal_timer
.expires
,
313 jiffies
+ (slot
<< INET_TWDR_RECYCLE_TICK
)))
314 mod_timer(&twdr
->twcal_timer
,
315 jiffies
+ (slot
<< INET_TWDR_RECYCLE_TICK
));
316 slot
= (twdr
->twcal_hand
+ slot
) & (INET_TWDR_RECYCLE_SLOTS
- 1);
318 list
= &twdr
->twcal_row
[slot
];
321 hlist_add_head(&tw
->tw_death_node
, list
);
323 if (twdr
->tw_count
++ == 0)
324 mod_timer(&twdr
->tw_timer
, jiffies
+ twdr
->period
);
325 spin_unlock(&twdr
->death_lock
);
328 EXPORT_SYMBOL_GPL(inet_twsk_schedule
);
330 void inet_twdr_twcal_tick(unsigned long data
)
332 struct inet_timewait_death_row
*twdr
;
335 unsigned long now
= jiffies
;
339 twdr
= (struct inet_timewait_death_row
*)data
;
341 spin_lock(&twdr
->death_lock
);
342 if (twdr
->twcal_hand
< 0)
345 slot
= twdr
->twcal_hand
;
346 j
= twdr
->twcal_jiffie
;
348 for (n
= 0; n
< INET_TWDR_RECYCLE_SLOTS
; n
++) {
349 if (time_before_eq(j
, now
)) {
350 struct hlist_node
*node
, *safe
;
351 struct inet_timewait_sock
*tw
;
353 inet_twsk_for_each_inmate_safe(tw
, node
, safe
,
354 &twdr
->twcal_row
[slot
]) {
355 __inet_twsk_del_dead_node(tw
);
356 __inet_twsk_kill(tw
, twdr
->hashinfo
);
363 twdr
->twcal_jiffie
= j
;
364 twdr
->twcal_hand
= slot
;
367 if (!hlist_empty(&twdr
->twcal_row
[slot
])) {
368 mod_timer(&twdr
->twcal_timer
, j
);
372 j
+= 1 << INET_TWDR_RECYCLE_TICK
;
373 slot
= (slot
+ 1) & (INET_TWDR_RECYCLE_SLOTS
- 1);
375 twdr
->twcal_hand
= -1;
378 if ((twdr
->tw_count
-= killed
) == 0)
379 del_timer(&twdr
->tw_timer
);
380 NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITKILLED
, killed
);
381 spin_unlock(&twdr
->death_lock
);
384 EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick
);