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>
17 /* Must be called with locally disabled BHs. */
18 static void __inet_twsk_kill(struct inet_timewait_sock
*tw
,
19 struct inet_hashinfo
*hashinfo
)
21 struct inet_bind_hashbucket
*bhead
;
22 struct inet_bind_bucket
*tb
;
23 /* Unlink from established hashes. */
24 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, tw
->tw_hash
);
27 if (hlist_nulls_unhashed(&tw
->tw_node
)) {
31 hlist_nulls_del_rcu(&tw
->tw_node
);
32 sk_nulls_node_init(&tw
->tw_node
);
35 /* Disassociate with bind bucket. */
36 bhead
= &hashinfo
->bhash
[inet_bhashfn(twsk_net(tw
), tw
->tw_num
,
37 hashinfo
->bhash_size
)];
38 spin_lock(&bhead
->lock
);
40 __hlist_del(&tw
->tw_bind_node
);
42 inet_bind_bucket_destroy(hashinfo
->bind_bucket_cachep
, tb
);
43 spin_unlock(&bhead
->lock
);
44 #ifdef SOCK_REFCNT_DEBUG
45 if (atomic_read(&tw
->tw_refcnt
) != 1) {
46 printk(KERN_DEBUG
"%s timewait_sock %p refcnt=%d\n",
47 tw
->tw_prot
->name
, tw
, atomic_read(&tw
->tw_refcnt
));
53 void inet_twsk_put(struct inet_timewait_sock
*tw
)
55 if (atomic_dec_and_test(&tw
->tw_refcnt
)) {
56 struct module
*owner
= tw
->tw_prot
->owner
;
57 twsk_destructor((struct sock
*)tw
);
58 #ifdef SOCK_REFCNT_DEBUG
59 printk(KERN_DEBUG
"%s timewait_sock %p released\n",
60 tw
->tw_prot
->name
, tw
);
62 release_net(twsk_net(tw
));
63 kmem_cache_free(tw
->tw_prot
->twsk_prot
->twsk_slab
, tw
);
67 EXPORT_SYMBOL_GPL(inet_twsk_put
);
70 * Enter the time wait state. This is called with locally disabled BH.
71 * Essentially we whip up a timewait bucket, copy the relevant info into it
72 * from the SK, and mess with hash chains and list linkage.
74 void __inet_twsk_hashdance(struct inet_timewait_sock
*tw
, struct sock
*sk
,
75 struct inet_hashinfo
*hashinfo
)
77 const struct inet_sock
*inet
= inet_sk(sk
);
78 const struct inet_connection_sock
*icsk
= inet_csk(sk
);
79 struct inet_ehash_bucket
*ehead
= inet_ehash_bucket(hashinfo
, sk
->sk_hash
);
80 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, sk
->sk_hash
);
81 struct inet_bind_hashbucket
*bhead
;
82 /* Step 1: Put TW into bind hash. Original socket stays there too.
83 Note, that any socket with inet->num != 0 MUST be bound in
84 binding cache, even if it is closed.
86 bhead
= &hashinfo
->bhash
[inet_bhashfn(twsk_net(tw
), inet
->num
,
87 hashinfo
->bhash_size
)];
88 spin_lock(&bhead
->lock
);
89 tw
->tw_tb
= icsk
->icsk_bind_hash
;
90 WARN_ON(!icsk
->icsk_bind_hash
);
91 inet_twsk_add_bind_node(tw
, &tw
->tw_tb
->owners
);
92 spin_unlock(&bhead
->lock
);
97 * Step 2: Hash TW into TIMEWAIT chain.
98 * Should be done before removing sk from established chain
99 * because readers are lockless and search established first.
101 atomic_inc(&tw
->tw_refcnt
);
102 inet_twsk_add_node_rcu(tw
, &ehead
->twchain
);
104 /* Step 3: Remove SK from established hash. */
105 if (__sk_nulls_del_node_init_rcu(sk
))
106 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, -1);
111 EXPORT_SYMBOL_GPL(__inet_twsk_hashdance
);
113 struct inet_timewait_sock
*inet_twsk_alloc(const struct sock
*sk
, const int state
)
115 struct inet_timewait_sock
*tw
=
116 kmem_cache_alloc(sk
->sk_prot_creator
->twsk_prot
->twsk_slab
,
119 const struct inet_sock
*inet
= inet_sk(sk
);
121 kmemcheck_annotate_bitfield(tw
->flags
);
123 /* Give us an identity. */
124 tw
->tw_daddr
= inet
->daddr
;
125 tw
->tw_rcv_saddr
= inet
->rcv_saddr
;
126 tw
->tw_bound_dev_if
= sk
->sk_bound_dev_if
;
127 tw
->tw_num
= inet
->num
;
128 tw
->tw_state
= TCP_TIME_WAIT
;
129 tw
->tw_substate
= state
;
130 tw
->tw_sport
= inet
->sport
;
131 tw
->tw_dport
= inet
->dport
;
132 tw
->tw_family
= sk
->sk_family
;
133 tw
->tw_reuse
= sk
->sk_reuse
;
134 tw
->tw_hash
= sk
->sk_hash
;
136 tw
->tw_transparent
= inet
->transparent
;
137 tw
->tw_prot
= sk
->sk_prot_creator
;
138 twsk_net_set(tw
, hold_net(sock_net(sk
)));
139 atomic_set(&tw
->tw_refcnt
, 1);
140 inet_twsk_dead_node_init(tw
);
141 __module_get(tw
->tw_prot
->owner
);
147 EXPORT_SYMBOL_GPL(inet_twsk_alloc
);
149 /* Returns non-zero if quota exceeded. */
150 static int inet_twdr_do_twkill_work(struct inet_timewait_death_row
*twdr
,
153 struct inet_timewait_sock
*tw
;
154 struct hlist_node
*node
;
158 /* NOTE: compare this to previous version where lock
159 * was released after detaching chain. It was racy,
160 * because tw buckets are scheduled in not serialized context
161 * in 2.3 (with netfilter), and with softnet it is common, because
162 * soft irqs are not sequenced.
167 inet_twsk_for_each_inmate(tw
, node
, &twdr
->cells
[slot
]) {
168 __inet_twsk_del_dead_node(tw
);
169 spin_unlock(&twdr
->death_lock
);
170 __inet_twsk_kill(tw
, twdr
->hashinfo
);
172 NET_INC_STATS_BH(twsk_net(tw
), LINUX_MIB_TIMEWAITED
);
176 spin_lock(&twdr
->death_lock
);
177 if (killed
> INET_TWDR_TWKILL_QUOTA
) {
182 /* While we dropped twdr->death_lock, another cpu may have
183 * killed off the next TW bucket in the list, therefore
184 * do a fresh re-read of the hlist head node with the
185 * lock reacquired. We still use the hlist traversal
186 * macro in order to get the prefetches.
191 twdr
->tw_count
-= killed
;
192 #ifndef CONFIG_NET_NS
193 NET_ADD_STATS_BH(&init_net
, LINUX_MIB_TIMEWAITED
, killed
);
198 void inet_twdr_hangman(unsigned long data
)
200 struct inet_timewait_death_row
*twdr
;
201 int unsigned need_timer
;
203 twdr
= (struct inet_timewait_death_row
*)data
;
204 spin_lock(&twdr
->death_lock
);
206 if (twdr
->tw_count
== 0)
210 if (inet_twdr_do_twkill_work(twdr
, twdr
->slot
)) {
211 twdr
->thread_slots
|= (1 << twdr
->slot
);
212 schedule_work(&twdr
->twkill_work
);
215 /* We purged the entire slot, anything left? */
219 twdr
->slot
= ((twdr
->slot
+ 1) & (INET_TWDR_TWKILL_SLOTS
- 1));
221 mod_timer(&twdr
->tw_timer
, jiffies
+ twdr
->period
);
223 spin_unlock(&twdr
->death_lock
);
226 EXPORT_SYMBOL_GPL(inet_twdr_hangman
);
228 void inet_twdr_twkill_work(struct work_struct
*work
)
230 struct inet_timewait_death_row
*twdr
=
231 container_of(work
, struct inet_timewait_death_row
, twkill_work
);
234 BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS
- 1) >
235 (sizeof(twdr
->thread_slots
) * 8));
237 while (twdr
->thread_slots
) {
238 spin_lock_bh(&twdr
->death_lock
);
239 for (i
= 0; i
< INET_TWDR_TWKILL_SLOTS
; i
++) {
240 if (!(twdr
->thread_slots
& (1 << i
)))
243 while (inet_twdr_do_twkill_work(twdr
, i
) != 0) {
244 if (need_resched()) {
245 spin_unlock_bh(&twdr
->death_lock
);
247 spin_lock_bh(&twdr
->death_lock
);
251 twdr
->thread_slots
&= ~(1 << i
);
253 spin_unlock_bh(&twdr
->death_lock
);
257 EXPORT_SYMBOL_GPL(inet_twdr_twkill_work
);
259 /* These are always called from BH context. See callers in
260 * tcp_input.c to verify this.
263 /* This is for handling early-kills of TIME_WAIT sockets. */
264 void inet_twsk_deschedule(struct inet_timewait_sock
*tw
,
265 struct inet_timewait_death_row
*twdr
)
267 spin_lock(&twdr
->death_lock
);
268 if (inet_twsk_del_dead_node(tw
)) {
270 if (--twdr
->tw_count
== 0)
271 del_timer(&twdr
->tw_timer
);
273 spin_unlock(&twdr
->death_lock
);
274 __inet_twsk_kill(tw
, twdr
->hashinfo
);
277 EXPORT_SYMBOL(inet_twsk_deschedule
);
279 void inet_twsk_schedule(struct inet_timewait_sock
*tw
,
280 struct inet_timewait_death_row
*twdr
,
281 const int timeo
, const int timewait_len
)
283 struct hlist_head
*list
;
286 /* timeout := RTO * 3.5
288 * 3.5 = 1+2+0.5 to wait for two retransmits.
290 * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
291 * our ACK acking that FIN can be lost. If N subsequent retransmitted
292 * FINs (or previous seqments) are lost (probability of such event
293 * is p^(N+1), where p is probability to lose single packet and
294 * time to detect the loss is about RTO*(2^N - 1) with exponential
295 * backoff). Normal timewait length is calculated so, that we
296 * waited at least for one retransmitted FIN (maximal RTO is 120sec).
297 * [ BTW Linux. following BSD, violates this requirement waiting
298 * only for 60sec, we should wait at least for 240 secs.
299 * Well, 240 consumes too much of resources 8)
301 * This interval is not reduced to catch old duplicate and
302 * responces to our wandering segments living for two MSLs.
303 * However, if we use PAWS to detect
304 * old duplicates, we can reduce the interval to bounds required
305 * by RTO, rather than MSL. So, if peer understands PAWS, we
306 * kill tw bucket after 3.5*RTO (it is important that this number
307 * is greater than TS tick!) and detect old duplicates with help
310 slot
= (timeo
+ (1 << INET_TWDR_RECYCLE_TICK
) - 1) >> INET_TWDR_RECYCLE_TICK
;
312 spin_lock(&twdr
->death_lock
);
314 /* Unlink it, if it was scheduled */
315 if (inet_twsk_del_dead_node(tw
))
318 atomic_inc(&tw
->tw_refcnt
);
320 if (slot
>= INET_TWDR_RECYCLE_SLOTS
) {
321 /* Schedule to slow timer */
322 if (timeo
>= timewait_len
) {
323 slot
= INET_TWDR_TWKILL_SLOTS
- 1;
325 slot
= DIV_ROUND_UP(timeo
, twdr
->period
);
326 if (slot
>= INET_TWDR_TWKILL_SLOTS
)
327 slot
= INET_TWDR_TWKILL_SLOTS
- 1;
329 tw
->tw_ttd
= jiffies
+ timeo
;
330 slot
= (twdr
->slot
+ slot
) & (INET_TWDR_TWKILL_SLOTS
- 1);
331 list
= &twdr
->cells
[slot
];
333 tw
->tw_ttd
= jiffies
+ (slot
<< INET_TWDR_RECYCLE_TICK
);
335 if (twdr
->twcal_hand
< 0) {
336 twdr
->twcal_hand
= 0;
337 twdr
->twcal_jiffie
= jiffies
;
338 twdr
->twcal_timer
.expires
= twdr
->twcal_jiffie
+
339 (slot
<< INET_TWDR_RECYCLE_TICK
);
340 add_timer(&twdr
->twcal_timer
);
342 if (time_after(twdr
->twcal_timer
.expires
,
343 jiffies
+ (slot
<< INET_TWDR_RECYCLE_TICK
)))
344 mod_timer(&twdr
->twcal_timer
,
345 jiffies
+ (slot
<< INET_TWDR_RECYCLE_TICK
));
346 slot
= (twdr
->twcal_hand
+ slot
) & (INET_TWDR_RECYCLE_SLOTS
- 1);
348 list
= &twdr
->twcal_row
[slot
];
351 hlist_add_head(&tw
->tw_death_node
, list
);
353 if (twdr
->tw_count
++ == 0)
354 mod_timer(&twdr
->tw_timer
, jiffies
+ twdr
->period
);
355 spin_unlock(&twdr
->death_lock
);
358 EXPORT_SYMBOL_GPL(inet_twsk_schedule
);
360 void inet_twdr_twcal_tick(unsigned long data
)
362 struct inet_timewait_death_row
*twdr
;
365 unsigned long now
= jiffies
;
369 twdr
= (struct inet_timewait_death_row
*)data
;
371 spin_lock(&twdr
->death_lock
);
372 if (twdr
->twcal_hand
< 0)
375 slot
= twdr
->twcal_hand
;
376 j
= twdr
->twcal_jiffie
;
378 for (n
= 0; n
< INET_TWDR_RECYCLE_SLOTS
; n
++) {
379 if (time_before_eq(j
, now
)) {
380 struct hlist_node
*node
, *safe
;
381 struct inet_timewait_sock
*tw
;
383 inet_twsk_for_each_inmate_safe(tw
, node
, safe
,
384 &twdr
->twcal_row
[slot
]) {
385 __inet_twsk_del_dead_node(tw
);
386 __inet_twsk_kill(tw
, twdr
->hashinfo
);
388 NET_INC_STATS_BH(twsk_net(tw
), LINUX_MIB_TIMEWAITKILLED
);
396 twdr
->twcal_jiffie
= j
;
397 twdr
->twcal_hand
= slot
;
400 if (!hlist_empty(&twdr
->twcal_row
[slot
])) {
401 mod_timer(&twdr
->twcal_timer
, j
);
405 j
+= 1 << INET_TWDR_RECYCLE_TICK
;
406 slot
= (slot
+ 1) & (INET_TWDR_RECYCLE_SLOTS
- 1);
408 twdr
->twcal_hand
= -1;
411 if ((twdr
->tw_count
-= killed
) == 0)
412 del_timer(&twdr
->tw_timer
);
413 #ifndef CONFIG_NET_NS
414 NET_ADD_STATS_BH(&init_net
, LINUX_MIB_TIMEWAITKILLED
, killed
);
416 spin_unlock(&twdr
->death_lock
);
419 EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick
);
421 void inet_twsk_purge(struct net
*net
, struct inet_hashinfo
*hashinfo
,
422 struct inet_timewait_death_row
*twdr
, int family
)
424 struct inet_timewait_sock
*tw
;
426 struct hlist_nulls_node
*node
;
430 for (h
= 0; h
< (hashinfo
->ehash_size
); h
++) {
431 struct inet_ehash_bucket
*head
=
432 inet_ehash_bucket(hashinfo
, h
);
433 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, h
);
436 sk_nulls_for_each(sk
, node
, &head
->twchain
) {
439 if (!net_eq(twsk_net(tw
), net
) ||
440 tw
->tw_family
!= family
)
443 atomic_inc(&tw
->tw_refcnt
);
445 inet_twsk_deschedule(tw
, twdr
);
454 EXPORT_SYMBOL_GPL(inet_twsk_purge
);