blob: a010e9a68811cf325f75b6b894849cd2a58303cc [file] [log] [blame]
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -07001/*
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.
5 *
6 * Generic TIME_WAIT sockets functions
7 *
8 * From code orinally in TCP
9 */
10
11#include <linux/config.h>
12
13#include <net/inet_hashtables.h>
14#include <net/inet_timewait_sock.h>
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -070015#include <net/ip.h>
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070016
17/* Must be called with locally disabled BHs. */
18void __inet_twsk_kill(struct inet_timewait_sock *tw, struct inet_hashinfo *hashinfo)
19{
20 struct inet_bind_hashbucket *bhead;
21 struct inet_bind_bucket *tb;
22 /* Unlink from established hashes. */
Eric Dumazet81c3d542005-10-03 14:13:38 -070023 struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, tw->tw_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070024
25 write_lock(&ehead->lock);
26 if (hlist_unhashed(&tw->tw_node)) {
27 write_unlock(&ehead->lock);
28 return;
29 }
30 __hlist_del(&tw->tw_node);
31 sk_node_init(&tw->tw_node);
32 write_unlock(&ehead->lock);
33
34 /* Disassociate with bind bucket. */
35 bhead = &hashinfo->bhash[inet_bhashfn(tw->tw_num, hashinfo->bhash_size)];
36 spin_lock(&bhead->lock);
37 tb = tw->tw_tb;
38 __hlist_del(&tw->tw_bind_node);
39 tw->tw_tb = NULL;
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));
46 }
47#endif
48 inet_twsk_put(tw);
49}
50
Arnaldo Carvalho de Melod8c97a92005-08-09 20:12:12 -070051EXPORT_SYMBOL_GPL(__inet_twsk_kill);
52
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070053/*
54 * Enter the time wait state. This is called with locally disabled BH.
55 * Essentially we whip up a timewait bucket, copy the relevant info into it
56 * from the SK, and mess with hash chains and list linkage.
57 */
58void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
59 struct inet_hashinfo *hashinfo)
60{
61 const struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070062 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet81c3d542005-10-03 14:13:38 -070063 struct inet_ehash_bucket *ehead = inet_ehash_bucket(hashinfo, sk->sk_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070064 struct inet_bind_hashbucket *bhead;
65 /* Step 1: Put TW into bind hash. Original socket stays there too.
66 Note, that any socket with inet->num != 0 MUST be bound in
67 binding cache, even if it is closed.
68 */
69 bhead = &hashinfo->bhash[inet_bhashfn(inet->num, hashinfo->bhash_size)];
70 spin_lock(&bhead->lock);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070071 tw->tw_tb = icsk->icsk_bind_hash;
72 BUG_TRAP(icsk->icsk_bind_hash);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -070073 inet_twsk_add_bind_node(tw, &tw->tw_tb->owners);
74 spin_unlock(&bhead->lock);
75
76 write_lock(&ehead->lock);
77
78 /* Step 2: Remove SK from established hash. */
79 if (__sk_del_node_init(sk))
80 sock_prot_dec_use(sk->sk_prot);
81
82 /* Step 3: Hash TW into TIMEWAIT half of established hash table. */
83 inet_twsk_add_node(tw, &(ehead + hashinfo->ehash_size)->chain);
84 atomic_inc(&tw->tw_refcnt);
85
86 write_unlock(&ehead->lock);
87}
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -070088
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -070089EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
90
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -070091struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state)
92{
93 struct inet_timewait_sock *tw = kmem_cache_alloc(sk->sk_prot_creator->twsk_slab,
94 SLAB_ATOMIC);
95 if (tw != NULL) {
96 const struct inet_sock *inet = inet_sk(sk);
97
98 /* Give us an identity. */
99 tw->tw_daddr = inet->daddr;
100 tw->tw_rcv_saddr = inet->rcv_saddr;
101 tw->tw_bound_dev_if = sk->sk_bound_dev_if;
102 tw->tw_num = inet->num;
103 tw->tw_state = TCP_TIME_WAIT;
104 tw->tw_substate = state;
105 tw->tw_sport = inet->sport;
106 tw->tw_dport = inet->dport;
107 tw->tw_family = sk->sk_family;
108 tw->tw_reuse = sk->sk_reuse;
Eric Dumazet81c3d542005-10-03 14:13:38 -0700109 tw->tw_hash = sk->sk_hash;
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700110 tw->tw_ipv6only = 0;
111 tw->tw_prot = sk->sk_prot_creator;
112 atomic_set(&tw->tw_refcnt, 1);
113 inet_twsk_dead_node_init(tw);
Arnaldo Carvalho de Meloeeb2b852005-10-10 21:25:23 -0700114 __module_get(tw->tw_prot->owner);
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700115 }
116
117 return tw;
118}
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700119
120EXPORT_SYMBOL_GPL(inet_twsk_alloc);
121
122/* Returns non-zero if quota exceeded. */
123static int inet_twdr_do_twkill_work(struct inet_timewait_death_row *twdr,
124 const int slot)
125{
126 struct inet_timewait_sock *tw;
127 struct hlist_node *node;
128 unsigned int killed;
129 int ret;
130
131 /* NOTE: compare this to previous version where lock
132 * was released after detaching chain. It was racy,
133 * because tw buckets are scheduled in not serialized context
134 * in 2.3 (with netfilter), and with softnet it is common, because
135 * soft irqs are not sequenced.
136 */
137 killed = 0;
138 ret = 0;
139rescan:
140 inet_twsk_for_each_inmate(tw, node, &twdr->cells[slot]) {
141 __inet_twsk_del_dead_node(tw);
142 spin_unlock(&twdr->death_lock);
143 __inet_twsk_kill(tw, twdr->hashinfo);
144 inet_twsk_put(tw);
145 killed++;
146 spin_lock(&twdr->death_lock);
147 if (killed > INET_TWDR_TWKILL_QUOTA) {
148 ret = 1;
149 break;
150 }
151
152 /* While we dropped twdr->death_lock, another cpu may have
153 * killed off the next TW bucket in the list, therefore
154 * do a fresh re-read of the hlist head node with the
155 * lock reacquired. We still use the hlist traversal
156 * macro in order to get the prefetches.
157 */
158 goto rescan;
159 }
160
161 twdr->tw_count -= killed;
162 NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITED, killed);
163
164 return ret;
165}
166
167void inet_twdr_hangman(unsigned long data)
168{
169 struct inet_timewait_death_row *twdr;
170 int unsigned need_timer;
171
172 twdr = (struct inet_timewait_death_row *)data;
173 spin_lock(&twdr->death_lock);
174
175 if (twdr->tw_count == 0)
176 goto out;
177
178 need_timer = 0;
179 if (inet_twdr_do_twkill_work(twdr, twdr->slot)) {
180 twdr->thread_slots |= (1 << twdr->slot);
181 mb();
182 schedule_work(&twdr->twkill_work);
183 need_timer = 1;
184 } else {
185 /* We purged the entire slot, anything left? */
186 if (twdr->tw_count)
187 need_timer = 1;
188 }
189 twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1));
190 if (need_timer)
191 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
192out:
193 spin_unlock(&twdr->death_lock);
194}
195
196EXPORT_SYMBOL_GPL(inet_twdr_hangman);
197
198extern void twkill_slots_invalid(void);
199
200void inet_twdr_twkill_work(void *data)
201{
202 struct inet_timewait_death_row *twdr = data;
203 int i;
204
205 if ((INET_TWDR_TWKILL_SLOTS - 1) > (sizeof(twdr->thread_slots) * 8))
206 twkill_slots_invalid();
207
208 while (twdr->thread_slots) {
209 spin_lock_bh(&twdr->death_lock);
210 for (i = 0; i < INET_TWDR_TWKILL_SLOTS; i++) {
211 if (!(twdr->thread_slots & (1 << i)))
212 continue;
213
214 while (inet_twdr_do_twkill_work(twdr, i) != 0) {
215 if (need_resched()) {
216 spin_unlock_bh(&twdr->death_lock);
217 schedule();
218 spin_lock_bh(&twdr->death_lock);
219 }
220 }
221
222 twdr->thread_slots &= ~(1 << i);
223 }
224 spin_unlock_bh(&twdr->death_lock);
225 }
226}
227
228EXPORT_SYMBOL_GPL(inet_twdr_twkill_work);
229
230/* These are always called from BH context. See callers in
231 * tcp_input.c to verify this.
232 */
233
234/* This is for handling early-kills of TIME_WAIT sockets. */
235void inet_twsk_deschedule(struct inet_timewait_sock *tw,
236 struct inet_timewait_death_row *twdr)
237{
238 spin_lock(&twdr->death_lock);
239 if (inet_twsk_del_dead_node(tw)) {
240 inet_twsk_put(tw);
241 if (--twdr->tw_count == 0)
242 del_timer(&twdr->tw_timer);
243 }
244 spin_unlock(&twdr->death_lock);
245 __inet_twsk_kill(tw, twdr->hashinfo);
246}
247
248EXPORT_SYMBOL(inet_twsk_deschedule);
249
250void inet_twsk_schedule(struct inet_timewait_sock *tw,
251 struct inet_timewait_death_row *twdr,
252 const int timeo, const int timewait_len)
253{
254 struct hlist_head *list;
255 int slot;
256
257 /* timeout := RTO * 3.5
258 *
259 * 3.5 = 1+2+0.5 to wait for two retransmits.
260 *
261 * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
262 * our ACK acking that FIN can be lost. If N subsequent retransmitted
263 * FINs (or previous seqments) are lost (probability of such event
264 * is p^(N+1), where p is probability to lose single packet and
265 * time to detect the loss is about RTO*(2^N - 1) with exponential
266 * backoff). Normal timewait length is calculated so, that we
267 * waited at least for one retransmitted FIN (maximal RTO is 120sec).
268 * [ BTW Linux. following BSD, violates this requirement waiting
269 * only for 60sec, we should wait at least for 240 secs.
270 * Well, 240 consumes too much of resources 8)
271 * ]
272 * This interval is not reduced to catch old duplicate and
273 * responces to our wandering segments living for two MSLs.
274 * However, if we use PAWS to detect
275 * old duplicates, we can reduce the interval to bounds required
276 * by RTO, rather than MSL. So, if peer understands PAWS, we
277 * kill tw bucket after 3.5*RTO (it is important that this number
278 * is greater than TS tick!) and detect old duplicates with help
279 * of PAWS.
280 */
281 slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK;
282
283 spin_lock(&twdr->death_lock);
284
285 /* Unlink it, if it was scheduled */
286 if (inet_twsk_del_dead_node(tw))
287 twdr->tw_count--;
288 else
289 atomic_inc(&tw->tw_refcnt);
290
291 if (slot >= INET_TWDR_RECYCLE_SLOTS) {
292 /* Schedule to slow timer */
293 if (timeo >= timewait_len) {
294 slot = INET_TWDR_TWKILL_SLOTS - 1;
295 } else {
296 slot = (timeo + twdr->period - 1) / twdr->period;
297 if (slot >= INET_TWDR_TWKILL_SLOTS)
298 slot = INET_TWDR_TWKILL_SLOTS - 1;
299 }
300 tw->tw_ttd = jiffies + timeo;
301 slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
302 list = &twdr->cells[slot];
303 } else {
304 tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK);
305
306 if (twdr->twcal_hand < 0) {
307 twdr->twcal_hand = 0;
308 twdr->twcal_jiffie = jiffies;
309 twdr->twcal_timer.expires = twdr->twcal_jiffie +
310 (slot << INET_TWDR_RECYCLE_TICK);
311 add_timer(&twdr->twcal_timer);
312 } else {
313 if (time_after(twdr->twcal_timer.expires,
314 jiffies + (slot << INET_TWDR_RECYCLE_TICK)))
315 mod_timer(&twdr->twcal_timer,
316 jiffies + (slot << INET_TWDR_RECYCLE_TICK));
317 slot = (twdr->twcal_hand + slot) & (INET_TWDR_RECYCLE_SLOTS - 1);
318 }
319 list = &twdr->twcal_row[slot];
320 }
321
322 hlist_add_head(&tw->tw_death_node, list);
323
324 if (twdr->tw_count++ == 0)
325 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
326 spin_unlock(&twdr->death_lock);
327}
328
329EXPORT_SYMBOL_GPL(inet_twsk_schedule);
330
331void inet_twdr_twcal_tick(unsigned long data)
332{
333 struct inet_timewait_death_row *twdr;
334 int n, slot;
335 unsigned long j;
336 unsigned long now = jiffies;
337 int killed = 0;
338 int adv = 0;
339
340 twdr = (struct inet_timewait_death_row *)data;
341
342 spin_lock(&twdr->death_lock);
343 if (twdr->twcal_hand < 0)
344 goto out;
345
346 slot = twdr->twcal_hand;
347 j = twdr->twcal_jiffie;
348
349 for (n = 0; n < INET_TWDR_RECYCLE_SLOTS; n++) {
350 if (time_before_eq(j, now)) {
351 struct hlist_node *node, *safe;
352 struct inet_timewait_sock *tw;
353
354 inet_twsk_for_each_inmate_safe(tw, node, safe,
355 &twdr->twcal_row[slot]) {
356 __inet_twsk_del_dead_node(tw);
357 __inet_twsk_kill(tw, twdr->hashinfo);
358 inet_twsk_put(tw);
359 killed++;
360 }
361 } else {
362 if (!adv) {
363 adv = 1;
364 twdr->twcal_jiffie = j;
365 twdr->twcal_hand = slot;
366 }
367
368 if (!hlist_empty(&twdr->twcal_row[slot])) {
369 mod_timer(&twdr->twcal_timer, j);
370 goto out;
371 }
372 }
373 j += 1 << INET_TWDR_RECYCLE_TICK;
374 slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1);
375 }
376 twdr->twcal_hand = -1;
377
378out:
379 if ((twdr->tw_count -= killed) == 0)
380 del_timer(&twdr->tw_timer);
381 NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITKILLED, killed);
382 spin_unlock(&twdr->death_lock);
383}
384
385EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);