]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/tcp_minisocks.c
[TIMEWAIT]: Introduce inet_timewait_death_row
[net-next-2.6.git] / net / ipv4 / tcp_minisocks.c
CommitLineData
1da177e4
LT
1/*
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 * Implementation of the Transmission Control Protocol(TCP).
7 *
8 * Version: $Id: tcp_minisocks.c,v 1.15 2002/02/01 22:01:04 davem Exp $
9 *
02c30a84 10 * Authors: Ross Biro
1da177e4
LT
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
13 * Corey Minyard <wf-rch!minyard@relay.EU.net>
14 * Florian La Roche, <flla@stud.uni-sb.de>
15 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
16 * Linus Torvalds, <torvalds@cs.helsinki.fi>
17 * Alan Cox, <gw4pts@gw4pts.ampr.org>
18 * Matthew Dillon, <dillon@apollo.west.oic.com>
19 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
20 * Jorge Cwik, <jorge@laser.satlink.net>
21 */
22
23#include <linux/config.h>
24#include <linux/mm.h>
25#include <linux/module.h>
26#include <linux/sysctl.h>
27#include <linux/workqueue.h>
28#include <net/tcp.h>
29#include <net/inet_common.h>
30#include <net/xfrm.h>
31
32#ifdef CONFIG_SYSCTL
33#define SYNC_INIT 0 /* let the user enable it */
34#else
35#define SYNC_INIT 1
36#endif
37
295ff7ed
ACM
38/* New-style handling of TIME_WAIT sockets. */
39
40static void inet_twdr_hangman(unsigned long data);
41static void inet_twdr_twkill_work(void *data);
42static void inet_twdr_twcal_tick(unsigned long data);
1da177e4
LT
43
44int sysctl_tcp_syncookies = SYNC_INIT;
45int sysctl_tcp_abort_on_overflow;
46
295ff7ed
ACM
47struct inet_timewait_death_row tcp_death_row = {
48 .sysctl_max_tw_buckets = NR_FILE * 2,
49 .period = TCP_TIMEWAIT_LEN / INET_TWDR_TWKILL_SLOTS,
50 .death_lock = SPIN_LOCK_UNLOCKED,
51 .hashinfo = &tcp_hashinfo,
52 .tw_timer = TIMER_INITIALIZER(inet_twdr_hangman, 0,
53 (unsigned long)&tcp_death_row),
54 .twkill_work = __WORK_INITIALIZER(tcp_death_row.twkill_work,
55 inet_twdr_twkill_work,
56 &tcp_death_row),
57/* Short-time timewait calendar */
58
59 .twcal_hand = -1,
60 .twcal_timer = TIMER_INITIALIZER(inet_twdr_twcal_tick, 0,
61 (unsigned long)&tcp_death_row),
62};
63
64EXPORT_SYMBOL_GPL(tcp_death_row);
65
66static void inet_twsk_schedule(struct inet_timewait_sock *tw,
67 struct inet_timewait_death_row *twdr,
68 const int timeo);
1da177e4
LT
69
70static __inline__ int tcp_in_window(u32 seq, u32 end_seq, u32 s_win, u32 e_win)
71{
72 if (seq == s_win)
73 return 1;
74 if (after(end_seq, s_win) && before(seq, e_win))
75 return 1;
76 return (seq == e_win && seq == end_seq);
77}
78
1da177e4
LT
79/*
80 * * Main purpose of TIME-WAIT state is to close connection gracefully,
81 * when one of ends sits in LAST-ACK or CLOSING retransmitting FIN
82 * (and, probably, tail of data) and one or more our ACKs are lost.
83 * * What is TIME-WAIT timeout? It is associated with maximal packet
84 * lifetime in the internet, which results in wrong conclusion, that
85 * it is set to catch "old duplicate segments" wandering out of their path.
86 * It is not quite correct. This timeout is calculated so that it exceeds
87 * maximal retransmission timeout enough to allow to lose one (or more)
88 * segments sent by peer and our ACKs. This time may be calculated from RTO.
89 * * When TIME-WAIT socket receives RST, it means that another end
90 * finally closed and we are allowed to kill TIME-WAIT too.
91 * * Second purpose of TIME-WAIT is catching old duplicate segments.
92 * Well, certainly it is pure paranoia, but if we load TIME-WAIT
93 * with this semantics, we MUST NOT kill TIME-WAIT state with RSTs.
94 * * If we invented some more clever way to catch duplicates
95 * (f.e. based on PAWS), we could truncate TIME-WAIT to several RTOs.
96 *
97 * The algorithm below is based on FORMAL INTERPRETATION of RFCs.
98 * When you compare it to RFCs, please, read section SEGMENT ARRIVES
99 * from the very beginning.
100 *
101 * NOTE. With recycling (and later with fin-wait-2) TW bucket
102 * is _not_ stateless. It means, that strictly speaking we must
103 * spinlock it. I do not want! Well, probability of misbehaviour
104 * is ridiculously low and, seems, we could use some mb() tricks
105 * to avoid misread sequence numbers, states etc. --ANK
106 */
107enum tcp_tw_status
8feaf0c0
ACM
108tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
109 const struct tcphdr *th)
1da177e4 110{
8feaf0c0 111 struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
1da177e4
LT
112 struct tcp_options_received tmp_opt;
113 int paws_reject = 0;
114
115 tmp_opt.saw_tstamp = 0;
8feaf0c0 116 if (th->doff > (sizeof(*th) >> 2) && tcptw->tw_ts_recent_stamp) {
1da177e4
LT
117 tcp_parse_options(skb, &tmp_opt, 0);
118
119 if (tmp_opt.saw_tstamp) {
8feaf0c0
ACM
120 tmp_opt.ts_recent = tcptw->tw_ts_recent;
121 tmp_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
1da177e4
LT
122 paws_reject = tcp_paws_check(&tmp_opt, th->rst);
123 }
124 }
125
126 if (tw->tw_substate == TCP_FIN_WAIT2) {
127 /* Just repeat all the checks of tcp_rcv_state_process() */
128
129 /* Out of window, send ACK */
130 if (paws_reject ||
131 !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
8feaf0c0
ACM
132 tcptw->tw_rcv_nxt,
133 tcptw->tw_rcv_nxt + tcptw->tw_rcv_wnd))
1da177e4
LT
134 return TCP_TW_ACK;
135
136 if (th->rst)
137 goto kill;
138
8feaf0c0 139 if (th->syn && !before(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt))
1da177e4
LT
140 goto kill_with_rst;
141
142 /* Dup ACK? */
8feaf0c0 143 if (!after(TCP_SKB_CB(skb)->end_seq, tcptw->tw_rcv_nxt) ||
1da177e4 144 TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) {
8feaf0c0 145 inet_twsk_put(tw);
1da177e4
LT
146 return TCP_TW_SUCCESS;
147 }
148
149 /* New data or FIN. If new data arrive after half-duplex close,
150 * reset.
151 */
152 if (!th->fin ||
8feaf0c0 153 TCP_SKB_CB(skb)->end_seq != tcptw->tw_rcv_nxt + 1) {
1da177e4 154kill_with_rst:
295ff7ed 155 inet_twsk_deschedule(tw, &tcp_death_row);
8feaf0c0 156 inet_twsk_put(tw);
1da177e4
LT
157 return TCP_TW_RST;
158 }
159
160 /* FIN arrived, enter true time-wait state. */
8feaf0c0
ACM
161 tw->tw_substate = TCP_TIME_WAIT;
162 tcptw->tw_rcv_nxt = TCP_SKB_CB(skb)->end_seq;
1da177e4 163 if (tmp_opt.saw_tstamp) {
8feaf0c0
ACM
164 tcptw->tw_ts_recent_stamp = xtime.tv_sec;
165 tcptw->tw_ts_recent = tmp_opt.rcv_tsval;
1da177e4
LT
166 }
167
168 /* I am shamed, but failed to make it more elegant.
169 * Yes, it is direct reference to IP, which is impossible
170 * to generalize to IPv6. Taking into account that IPv6
171 * do not undertsnad recycling in any case, it not
172 * a big problem in practice. --ANK */
173 if (tw->tw_family == AF_INET &&
295ff7ed 174 tcp_death_row.sysctl_tw_recycle && tcptw->tw_ts_recent_stamp &&
1da177e4 175 tcp_v4_tw_remember_stamp(tw))
295ff7ed 176 inet_twsk_schedule(tw, &tcp_death_row, tw->tw_timeout);
1da177e4 177 else
295ff7ed 178 inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN);
1da177e4
LT
179 return TCP_TW_ACK;
180 }
181
182 /*
183 * Now real TIME-WAIT state.
184 *
185 * RFC 1122:
186 * "When a connection is [...] on TIME-WAIT state [...]
187 * [a TCP] MAY accept a new SYN from the remote TCP to
188 * reopen the connection directly, if it:
189 *
190 * (1) assigns its initial sequence number for the new
191 * connection to be larger than the largest sequence
192 * number it used on the previous connection incarnation,
193 * and
194 *
195 * (2) returns to TIME-WAIT state if the SYN turns out
196 * to be an old duplicate".
197 */
198
199 if (!paws_reject &&
8feaf0c0 200 (TCP_SKB_CB(skb)->seq == tcptw->tw_rcv_nxt &&
1da177e4
LT
201 (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq || th->rst))) {
202 /* In window segment, it may be only reset or bare ack. */
203
204 if (th->rst) {
205 /* This is TIME_WAIT assasination, in two flavors.
206 * Oh well... nobody has a sufficient solution to this
207 * protocol bug yet.
208 */
209 if (sysctl_tcp_rfc1337 == 0) {
210kill:
295ff7ed 211 inet_twsk_deschedule(tw, &tcp_death_row);
8feaf0c0 212 inet_twsk_put(tw);
1da177e4
LT
213 return TCP_TW_SUCCESS;
214 }
215 }
295ff7ed 216 inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN);
1da177e4
LT
217
218 if (tmp_opt.saw_tstamp) {
8feaf0c0
ACM
219 tcptw->tw_ts_recent = tmp_opt.rcv_tsval;
220 tcptw->tw_ts_recent_stamp = xtime.tv_sec;
1da177e4
LT
221 }
222
8feaf0c0 223 inet_twsk_put(tw);
1da177e4
LT
224 return TCP_TW_SUCCESS;
225 }
226
227 /* Out of window segment.
228
229 All the segments are ACKed immediately.
230
231 The only exception is new SYN. We accept it, if it is
232 not old duplicate and we are not in danger to be killed
233 by delayed old duplicates. RFC check is that it has
234 newer sequence number works at rates <40Mbit/sec.
235 However, if paws works, it is reliable AND even more,
236 we even may relax silly seq space cutoff.
237
238 RED-PEN: we violate main RFC requirement, if this SYN will appear
239 old duplicate (i.e. we receive RST in reply to SYN-ACK),
240 we must return socket to time-wait state. It is not good,
241 but not fatal yet.
242 */
243
244 if (th->syn && !th->rst && !th->ack && !paws_reject &&
8feaf0c0
ACM
245 (after(TCP_SKB_CB(skb)->seq, tcptw->tw_rcv_nxt) ||
246 (tmp_opt.saw_tstamp &&
247 (s32)(tcptw->tw_ts_recent - tmp_opt.rcv_tsval) < 0))) {
248 u32 isn = tcptw->tw_snd_nxt + 65535 + 2;
1da177e4
LT
249 if (isn == 0)
250 isn++;
251 TCP_SKB_CB(skb)->when = isn;
252 return TCP_TW_SYN;
253 }
254
255 if (paws_reject)
256 NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED);
257
258 if(!th->rst) {
259 /* In this case we must reset the TIMEWAIT timer.
260 *
261 * If it is ACKless SYN it may be both old duplicate
262 * and new good SYN with random sequence number <rcv_nxt.
263 * Do not reschedule in the last case.
264 */
265 if (paws_reject || th->ack)
295ff7ed 266 inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN);
1da177e4
LT
267
268 /* Send ACK. Note, we do not put the bucket,
269 * it will be released by caller.
270 */
271 return TCP_TW_ACK;
272 }
8feaf0c0 273 inet_twsk_put(tw);
1da177e4
LT
274 return TCP_TW_SUCCESS;
275}
276
1da177e4
LT
277/*
278 * Move a socket to time-wait or dead fin-wait-2 state.
279 */
280void tcp_time_wait(struct sock *sk, int state, int timeo)
281{
8feaf0c0
ACM
282 struct inet_timewait_sock *tw = NULL;
283 const struct tcp_sock *tp = tcp_sk(sk);
1da177e4
LT
284 int recycle_ok = 0;
285
295ff7ed 286 if (tcp_death_row.sysctl_tw_recycle && tp->rx_opt.ts_recent_stamp)
1da177e4
LT
287 recycle_ok = tp->af_specific->remember_stamp(sk);
288
295ff7ed 289 if (tcp_death_row.tw_count < tcp_death_row.sysctl_max_tw_buckets)
c676270b 290 tw = inet_twsk_alloc(sk, state);
1da177e4 291
8feaf0c0
ACM
292 if (tw != NULL) {
293 struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
463c84b9
ACM
294 const struct inet_connection_sock *icsk = inet_csk(sk);
295 const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
8feaf0c0 296
1da177e4 297 tw->tw_rcv_wscale = tp->rx_opt.rcv_wscale;
8feaf0c0
ACM
298 tcptw->tw_rcv_nxt = tp->rcv_nxt;
299 tcptw->tw_snd_nxt = tp->snd_nxt;
300 tcptw->tw_rcv_wnd = tcp_receive_window(tp);
301 tcptw->tw_ts_recent = tp->rx_opt.ts_recent;
302 tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp;
1da177e4
LT
303
304#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
305 if (tw->tw_family == PF_INET6) {
306 struct ipv6_pinfo *np = inet6_sk(sk);
8feaf0c0 307 struct tcp6_timewait_sock *tcp6tw = tcp6_twsk((struct sock *)tw);
1da177e4 308
8feaf0c0
ACM
309 ipv6_addr_copy(&tcp6tw->tw_v6_daddr, &np->daddr);
310 ipv6_addr_copy(&tcp6tw->tw_v6_rcv_saddr, &np->rcv_saddr);
311 tw->tw_ipv6only = np->ipv6only;
c676270b 312 }
1da177e4
LT
313#endif
314 /* Linkage updates. */
e48c414e 315 __inet_twsk_hashdance(tw, sk, &tcp_hashinfo);
1da177e4
LT
316
317 /* Get the TIME_WAIT timeout firing. */
318 if (timeo < rto)
319 timeo = rto;
320
321 if (recycle_ok) {
322 tw->tw_timeout = rto;
323 } else {
324 tw->tw_timeout = TCP_TIMEWAIT_LEN;
325 if (state == TCP_TIME_WAIT)
326 timeo = TCP_TIMEWAIT_LEN;
327 }
328
295ff7ed 329 inet_twsk_schedule(tw, &tcp_death_row, timeo);
8feaf0c0 330 inet_twsk_put(tw);
1da177e4
LT
331 } else {
332 /* Sorry, if we're out of memory, just CLOSE this
333 * socket up. We've got bigger problems than
334 * non-graceful socket closings.
335 */
336 if (net_ratelimit())
337 printk(KERN_INFO "TCP: time wait bucket table overflow\n");
338 }
339
340 tcp_update_metrics(sk);
341 tcp_done(sk);
342}
343
1da177e4 344/* Returns non-zero if quota exceeded. */
295ff7ed
ACM
345static int inet_twdr_do_twkill_work(struct inet_timewait_death_row *twdr,
346 const int slot)
1da177e4 347{
8feaf0c0 348 struct inet_timewait_sock *tw;
1da177e4
LT
349 struct hlist_node *node;
350 unsigned int killed;
351 int ret;
352
353 /* NOTE: compare this to previous version where lock
354 * was released after detaching chain. It was racy,
355 * because tw buckets are scheduled in not serialized context
356 * in 2.3 (with netfilter), and with softnet it is common, because
357 * soft irqs are not sequenced.
358 */
359 killed = 0;
360 ret = 0;
361rescan:
295ff7ed 362 inet_twsk_for_each_inmate(tw, node, &twdr->cells[slot]) {
8feaf0c0 363 __inet_twsk_del_dead_node(tw);
295ff7ed
ACM
364 spin_unlock(&twdr->death_lock);
365 __inet_twsk_kill(tw, twdr->hashinfo);
8feaf0c0 366 inet_twsk_put(tw);
1da177e4 367 killed++;
295ff7ed
ACM
368 spin_lock(&twdr->death_lock);
369 if (killed > INET_TWDR_TWKILL_QUOTA) {
1da177e4
LT
370 ret = 1;
371 break;
372 }
373
295ff7ed 374 /* While we dropped twdr->death_lock, another cpu may have
1da177e4
LT
375 * killed off the next TW bucket in the list, therefore
376 * do a fresh re-read of the hlist head node with the
377 * lock reacquired. We still use the hlist traversal
378 * macro in order to get the prefetches.
379 */
380 goto rescan;
381 }
382
295ff7ed 383 twdr->tw_count -= killed;
1da177e4
LT
384 NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITED, killed);
385
386 return ret;
387}
388
295ff7ed 389static void inet_twdr_hangman(unsigned long data)
1da177e4 390{
295ff7ed
ACM
391 struct inet_timewait_death_row *twdr;
392 int unsigned need_timer;
1da177e4 393
295ff7ed
ACM
394 twdr = (struct inet_timewait_death_row *)data;
395 spin_lock(&twdr->death_lock);
1da177e4 396
295ff7ed 397 if (twdr->tw_count == 0)
1da177e4
LT
398 goto out;
399
400 need_timer = 0;
295ff7ed
ACM
401 if (inet_twdr_do_twkill_work(twdr, twdr->slot)) {
402 twdr->thread_slots |= (1 << twdr->slot);
1da177e4 403 mb();
295ff7ed 404 schedule_work(&twdr->twkill_work);
1da177e4
LT
405 need_timer = 1;
406 } else {
407 /* We purged the entire slot, anything left? */
295ff7ed 408 if (twdr->tw_count)
1da177e4
LT
409 need_timer = 1;
410 }
295ff7ed 411 twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1));
1da177e4 412 if (need_timer)
295ff7ed 413 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
1da177e4 414out:
295ff7ed 415 spin_unlock(&twdr->death_lock);
1da177e4
LT
416}
417
418extern void twkill_slots_invalid(void);
419
295ff7ed 420static void inet_twdr_twkill_work(void *data)
1da177e4 421{
295ff7ed 422 struct inet_timewait_death_row *twdr = data;
1da177e4
LT
423 int i;
424
295ff7ed 425 if ((INET_TWDR_TWKILL_SLOTS - 1) > (sizeof(twdr->thread_slots) * 8))
1da177e4
LT
426 twkill_slots_invalid();
427
295ff7ed
ACM
428 while (twdr->thread_slots) {
429 spin_lock_bh(&twdr->death_lock);
430 for (i = 0; i < INET_TWDR_TWKILL_SLOTS; i++) {
431 if (!(twdr->thread_slots & (1 << i)))
1da177e4
LT
432 continue;
433
295ff7ed 434 while (inet_twdr_do_twkill_work(twdr, i) != 0) {
1da177e4 435 if (need_resched()) {
295ff7ed 436 spin_unlock_bh(&twdr->death_lock);
1da177e4 437 schedule();
295ff7ed 438 spin_lock_bh(&twdr->death_lock);
1da177e4
LT
439 }
440 }
441
295ff7ed 442 twdr->thread_slots &= ~(1 << i);
1da177e4 443 }
295ff7ed 444 spin_unlock_bh(&twdr->death_lock);
1da177e4
LT
445 }
446}
447
448/* These are always called from BH context. See callers in
449 * tcp_input.c to verify this.
450 */
451
452/* This is for handling early-kills of TIME_WAIT sockets. */
295ff7ed
ACM
453void inet_twsk_deschedule(struct inet_timewait_sock *tw,
454 struct inet_timewait_death_row *twdr)
1da177e4 455{
295ff7ed 456 spin_lock(&twdr->death_lock);
8feaf0c0
ACM
457 if (inet_twsk_del_dead_node(tw)) {
458 inet_twsk_put(tw);
295ff7ed
ACM
459 if (--twdr->tw_count == 0)
460 del_timer(&twdr->tw_timer);
1da177e4 461 }
295ff7ed
ACM
462 spin_unlock(&twdr->death_lock);
463 __inet_twsk_kill(tw, twdr->hashinfo);
1da177e4
LT
464}
465
295ff7ed
ACM
466static void inet_twsk_schedule(struct inet_timewait_sock *tw,
467 struct inet_timewait_death_row *twdr,
468 const int timeo)
1da177e4
LT
469{
470 struct hlist_head *list;
471 int slot;
472
473 /* timeout := RTO * 3.5
474 *
475 * 3.5 = 1+2+0.5 to wait for two retransmits.
476 *
477 * RATIONALE: if FIN arrived and we entered TIME-WAIT state,
478 * our ACK acking that FIN can be lost. If N subsequent retransmitted
479 * FINs (or previous seqments) are lost (probability of such event
480 * is p^(N+1), where p is probability to lose single packet and
481 * time to detect the loss is about RTO*(2^N - 1) with exponential
482 * backoff). Normal timewait length is calculated so, that we
483 * waited at least for one retransmitted FIN (maximal RTO is 120sec).
484 * [ BTW Linux. following BSD, violates this requirement waiting
485 * only for 60sec, we should wait at least for 240 secs.
486 * Well, 240 consumes too much of resources 8)
487 * ]
488 * This interval is not reduced to catch old duplicate and
489 * responces to our wandering segments living for two MSLs.
490 * However, if we use PAWS to detect
491 * old duplicates, we can reduce the interval to bounds required
492 * by RTO, rather than MSL. So, if peer understands PAWS, we
493 * kill tw bucket after 3.5*RTO (it is important that this number
494 * is greater than TS tick!) and detect old duplicates with help
495 * of PAWS.
496 */
295ff7ed 497 slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK;
1da177e4 498
295ff7ed 499 spin_lock(&twdr->death_lock);
1da177e4
LT
500
501 /* Unlink it, if it was scheduled */
8feaf0c0 502 if (inet_twsk_del_dead_node(tw))
295ff7ed 503 twdr->tw_count--;
1da177e4
LT
504 else
505 atomic_inc(&tw->tw_refcnt);
506
295ff7ed 507 if (slot >= INET_TWDR_RECYCLE_SLOTS) {
1da177e4
LT
508 /* Schedule to slow timer */
509 if (timeo >= TCP_TIMEWAIT_LEN) {
295ff7ed 510 slot = INET_TWDR_TWKILL_SLOTS - 1;
1da177e4 511 } else {
295ff7ed
ACM
512 slot = (timeo + twdr->period - 1) / twdr->period;
513 if (slot >= INET_TWDR_TWKILL_SLOTS)
514 slot = INET_TWDR_TWKILL_SLOTS - 1;
1da177e4
LT
515 }
516 tw->tw_ttd = jiffies + timeo;
295ff7ed
ACM
517 slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
518 list = &twdr->cells[slot];
1da177e4 519 } else {
295ff7ed
ACM
520 tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK);
521
522 if (twdr->twcal_hand < 0) {
523 twdr->twcal_hand = 0;
524 twdr->twcal_jiffie = jiffies;
525 twdr->twcal_timer.expires = twdr->twcal_jiffie +
526 (slot << INET_TWDR_RECYCLE_TICK);
527 add_timer(&twdr->twcal_timer);
1da177e4 528 } else {
295ff7ed
ACM
529 if (time_after(twdr->twcal_timer.expires,
530 jiffies + (slot << INET_TWDR_RECYCLE_TICK)))
531 mod_timer(&twdr->twcal_timer,
532 jiffies + (slot << INET_TWDR_RECYCLE_TICK));
533 slot = (twdr->twcal_hand + slot) & (INET_TWDR_RECYCLE_SLOTS - 1);
1da177e4 534 }
295ff7ed 535 list = &twdr->twcal_row[slot];
1da177e4
LT
536 }
537
538 hlist_add_head(&tw->tw_death_node, list);
539
295ff7ed
ACM
540 if (twdr->tw_count++ == 0)
541 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
542 spin_unlock(&twdr->death_lock);
1da177e4
LT
543}
544
295ff7ed 545void inet_twdr_twcal_tick(unsigned long data)
1da177e4 546{
295ff7ed 547 struct inet_timewait_death_row *twdr;
1da177e4
LT
548 int n, slot;
549 unsigned long j;
550 unsigned long now = jiffies;
551 int killed = 0;
552 int adv = 0;
553
295ff7ed
ACM
554 twdr = (struct inet_timewait_death_row *)data;
555
556 spin_lock(&twdr->death_lock);
557 if (twdr->twcal_hand < 0)
1da177e4
LT
558 goto out;
559
295ff7ed
ACM
560 slot = twdr->twcal_hand;
561 j = twdr->twcal_jiffie;
1da177e4 562
295ff7ed 563 for (n = 0; n < INET_TWDR_RECYCLE_SLOTS; n++) {
1da177e4
LT
564 if (time_before_eq(j, now)) {
565 struct hlist_node *node, *safe;
8feaf0c0 566 struct inet_timewait_sock *tw;
1da177e4 567
8feaf0c0 568 inet_twsk_for_each_inmate_safe(tw, node, safe,
295ff7ed 569 &twdr->twcal_row[slot]) {
8feaf0c0 570 __inet_twsk_del_dead_node(tw);
295ff7ed 571 __inet_twsk_kill(tw, twdr->hashinfo);
8feaf0c0 572 inet_twsk_put(tw);
1da177e4
LT
573 killed++;
574 }
575 } else {
576 if (!adv) {
577 adv = 1;
295ff7ed
ACM
578 twdr->twcal_jiffie = j;
579 twdr->twcal_hand = slot;
1da177e4
LT
580 }
581
295ff7ed
ACM
582 if (!hlist_empty(&twdr->twcal_row[slot])) {
583 mod_timer(&twdr->twcal_timer, j);
1da177e4
LT
584 goto out;
585 }
586 }
295ff7ed
ACM
587 j += 1 << INET_TWDR_RECYCLE_TICK;
588 slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1);
1da177e4 589 }
295ff7ed 590 twdr->twcal_hand = -1;
1da177e4
LT
591
592out:
295ff7ed
ACM
593 if ((twdr->tw_count -= killed) == 0)
594 del_timer(&twdr->tw_timer);
1da177e4 595 NET_ADD_STATS_BH(LINUX_MIB_TIMEWAITKILLED, killed);
295ff7ed 596 spin_unlock(&twdr->death_lock);
1da177e4
LT
597}
598
599/* This is not only more efficient than what we used to do, it eliminates
600 * a lot of code duplication between IPv4/IPv6 SYN recv processing. -DaveM
601 *
602 * Actually, we could lots of memory writes here. tp of listening
603 * socket contains all necessary default parameters.
604 */
60236fdd 605struct sock *tcp_create_openreq_child(struct sock *sk, struct request_sock *req, struct sk_buff *skb)
1da177e4 606{
9f1d2604 607 struct sock *newsk = inet_csk_clone(sk, req, GFP_ATOMIC);
1da177e4 608
87d11ceb 609 if (newsk != NULL) {
9f1d2604 610 const struct inet_request_sock *ireq = inet_rsk(req);
2e6599cb 611 struct tcp_request_sock *treq = tcp_rsk(req);
9f1d2604 612 struct inet_connection_sock *newicsk = inet_csk(sk);
1da177e4 613 struct tcp_sock *newtp;
1da177e4 614
1da177e4
LT
615 /* Now setup tcp_sock */
616 newtp = tcp_sk(newsk);
617 newtp->pred_flags = 0;
2e6599cb 618 newtp->rcv_nxt = treq->rcv_isn + 1;
87d11ceb 619 newtp->snd_nxt = newtp->snd_una = newtp->snd_sml = treq->snt_isn + 1;
1da177e4
LT
620
621 tcp_prequeue_init(newtp);
622
2e6599cb 623 tcp_init_wl(newtp, treq->snt_isn, treq->rcv_isn);
1da177e4 624
1da177e4
LT
625 newtp->srtt = 0;
626 newtp->mdev = TCP_TIMEOUT_INIT;
463c84b9 627 newicsk->icsk_rto = TCP_TIMEOUT_INIT;
1da177e4
LT
628
629 newtp->packets_out = 0;
630 newtp->left_out = 0;
631 newtp->retrans_out = 0;
632 newtp->sacked_out = 0;
633 newtp->fackets_out = 0;
634 newtp->snd_ssthresh = 0x7fffffff;
635
636 /* So many TCP implementations out there (incorrectly) count the
637 * initial SYN frame in their delayed-ACK and congestion control
638 * algorithms that we must have the following bandaid to talk
639 * efficiently to them. -DaveM
640 */
641 newtp->snd_cwnd = 2;
642 newtp->snd_cwnd_cnt = 0;
643
644 newtp->frto_counter = 0;
645 newtp->frto_highmark = 0;
646
317a76f9
SH
647 newtp->ca_ops = &tcp_reno;
648
1da177e4
LT
649 tcp_set_ca_state(newtp, TCP_CA_Open);
650 tcp_init_xmit_timers(newsk);
651 skb_queue_head_init(&newtp->out_of_order_queue);
2e6599cb
ACM
652 newtp->rcv_wup = treq->rcv_isn + 1;
653 newtp->write_seq = treq->snt_isn + 1;
1da177e4 654 newtp->pushed_seq = newtp->write_seq;
2e6599cb 655 newtp->copied_seq = treq->rcv_isn + 1;
1da177e4
LT
656
657 newtp->rx_opt.saw_tstamp = 0;
658
659 newtp->rx_opt.dsack = 0;
660 newtp->rx_opt.eff_sacks = 0;
661
662 newtp->probes_out = 0;
663 newtp->rx_opt.num_sacks = 0;
664 newtp->urg_data = 0;
1da177e4 665
1da177e4 666 if (sock_flag(newsk, SOCK_KEEPOPEN))
463c84b9
ACM
667 inet_csk_reset_keepalive_timer(newsk,
668 keepalive_time_when(newtp));
1da177e4 669
2e6599cb
ACM
670 newtp->rx_opt.tstamp_ok = ireq->tstamp_ok;
671 if((newtp->rx_opt.sack_ok = ireq->sack_ok) != 0) {
1da177e4
LT
672 if (sysctl_tcp_fack)
673 newtp->rx_opt.sack_ok |= 2;
674 }
675 newtp->window_clamp = req->window_clamp;
676 newtp->rcv_ssthresh = req->rcv_wnd;
677 newtp->rcv_wnd = req->rcv_wnd;
2e6599cb 678 newtp->rx_opt.wscale_ok = ireq->wscale_ok;
1da177e4 679 if (newtp->rx_opt.wscale_ok) {
2e6599cb
ACM
680 newtp->rx_opt.snd_wscale = ireq->snd_wscale;
681 newtp->rx_opt.rcv_wscale = ireq->rcv_wscale;
1da177e4
LT
682 } else {
683 newtp->rx_opt.snd_wscale = newtp->rx_opt.rcv_wscale = 0;
684 newtp->window_clamp = min(newtp->window_clamp, 65535U);
685 }
686 newtp->snd_wnd = ntohs(skb->h.th->window) << newtp->rx_opt.snd_wscale;
687 newtp->max_window = newtp->snd_wnd;
688
689 if (newtp->rx_opt.tstamp_ok) {
690 newtp->rx_opt.ts_recent = req->ts_recent;
691 newtp->rx_opt.ts_recent_stamp = xtime.tv_sec;
692 newtp->tcp_header_len = sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
693 } else {
694 newtp->rx_opt.ts_recent_stamp = 0;
695 newtp->tcp_header_len = sizeof(struct tcphdr);
696 }
697 if (skb->len >= TCP_MIN_RCVMSS+newtp->tcp_header_len)
463c84b9 698 newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len;
1da177e4
LT
699 newtp->rx_opt.mss_clamp = req->mss;
700 TCP_ECN_openreq_child(newtp, req);
701 if (newtp->ecn_flags&TCP_ECN_OK)
702 sock_set_flag(newsk, SOCK_NO_LARGESEND);
703
1da177e4
LT
704 TCP_INC_STATS_BH(TCP_MIB_PASSIVEOPENS);
705 }
706 return newsk;
707}
708
709/*
710 * Process an incoming packet for SYN_RECV sockets represented
60236fdd 711 * as a request_sock.
1da177e4
LT
712 */
713
714struct sock *tcp_check_req(struct sock *sk,struct sk_buff *skb,
60236fdd
ACM
715 struct request_sock *req,
716 struct request_sock **prev)
1da177e4
LT
717{
718 struct tcphdr *th = skb->h.th;
719 struct tcp_sock *tp = tcp_sk(sk);
720 u32 flg = tcp_flag_word(th) & (TCP_FLAG_RST|TCP_FLAG_SYN|TCP_FLAG_ACK);
721 int paws_reject = 0;
722 struct tcp_options_received tmp_opt;
723 struct sock *child;
724
725 tmp_opt.saw_tstamp = 0;
726 if (th->doff > (sizeof(struct tcphdr)>>2)) {
727 tcp_parse_options(skb, &tmp_opt, 0);
728
729 if (tmp_opt.saw_tstamp) {
730 tmp_opt.ts_recent = req->ts_recent;
731 /* We do not store true stamp, but it is not required,
732 * it can be estimated (approximately)
733 * from another data.
734 */
735 tmp_opt.ts_recent_stamp = xtime.tv_sec - ((TCP_TIMEOUT_INIT/HZ)<<req->retrans);
736 paws_reject = tcp_paws_check(&tmp_opt, th->rst);
737 }
738 }
739
740 /* Check for pure retransmitted SYN. */
2e6599cb 741 if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn &&
1da177e4
LT
742 flg == TCP_FLAG_SYN &&
743 !paws_reject) {
744 /*
745 * RFC793 draws (Incorrectly! It was fixed in RFC1122)
746 * this case on figure 6 and figure 8, but formal
747 * protocol description says NOTHING.
748 * To be more exact, it says that we should send ACK,
749 * because this segment (at least, if it has no data)
750 * is out of window.
751 *
752 * CONCLUSION: RFC793 (even with RFC1122) DOES NOT
753 * describe SYN-RECV state. All the description
754 * is wrong, we cannot believe to it and should
755 * rely only on common sense and implementation
756 * experience.
757 *
758 * Enforce "SYN-ACK" according to figure 8, figure 6
759 * of RFC793, fixed by RFC1122.
760 */
60236fdd 761 req->rsk_ops->rtx_syn_ack(sk, req, NULL);
1da177e4
LT
762 return NULL;
763 }
764
765 /* Further reproduces section "SEGMENT ARRIVES"
766 for state SYN-RECEIVED of RFC793.
767 It is broken, however, it does not work only
768 when SYNs are crossed.
769
770 You would think that SYN crossing is impossible here, since
771 we should have a SYN_SENT socket (from connect()) on our end,
772 but this is not true if the crossed SYNs were sent to both
773 ends by a malicious third party. We must defend against this,
774 and to do that we first verify the ACK (as per RFC793, page
775 36) and reset if it is invalid. Is this a true full defense?
776 To convince ourselves, let us consider a way in which the ACK
777 test can still pass in this 'malicious crossed SYNs' case.
778 Malicious sender sends identical SYNs (and thus identical sequence
779 numbers) to both A and B:
780
781 A: gets SYN, seq=7
782 B: gets SYN, seq=7
783
784 By our good fortune, both A and B select the same initial
785 send sequence number of seven :-)
786
787 A: sends SYN|ACK, seq=7, ack_seq=8
788 B: sends SYN|ACK, seq=7, ack_seq=8
789
790 So we are now A eating this SYN|ACK, ACK test passes. So
791 does sequence test, SYN is truncated, and thus we consider
792 it a bare ACK.
793
295f7324
ACM
794 If icsk->icsk_accept_queue.rskq_defer_accept, we silently drop this
795 bare ACK. Otherwise, we create an established connection. Both
796 ends (listening sockets) accept the new incoming connection and try
797 to talk to each other. 8-)
1da177e4
LT
798
799 Note: This case is both harmless, and rare. Possibility is about the
800 same as us discovering intelligent life on another plant tomorrow.
801
802 But generally, we should (RFC lies!) to accept ACK
803 from SYNACK both here and in tcp_rcv_state_process().
804 tcp_rcv_state_process() does not, hence, we do not too.
805
806 Note that the case is absolutely generic:
807 we cannot optimize anything here without
808 violating protocol. All the checks must be made
809 before attempt to create socket.
810 */
811
812 /* RFC793 page 36: "If the connection is in any non-synchronized state ...
813 * and the incoming segment acknowledges something not yet
814 * sent (the segment carries an unaccaptable ACK) ...
815 * a reset is sent."
816 *
817 * Invalid ACK: reset will be sent by listening socket
818 */
819 if ((flg & TCP_FLAG_ACK) &&
2e6599cb 820 (TCP_SKB_CB(skb)->ack_seq != tcp_rsk(req)->snt_isn + 1))
1da177e4
LT
821 return sk;
822
823 /* Also, it would be not so bad idea to check rcv_tsecr, which
824 * is essentially ACK extension and too early or too late values
825 * should cause reset in unsynchronized states.
826 */
827
828 /* RFC793: "first check sequence number". */
829
830 if (paws_reject || !tcp_in_window(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq,
2e6599cb 831 tcp_rsk(req)->rcv_isn + 1, tcp_rsk(req)->rcv_isn + 1 + req->rcv_wnd)) {
1da177e4
LT
832 /* Out of window: send ACK and drop. */
833 if (!(flg & TCP_FLAG_RST))
60236fdd 834 req->rsk_ops->send_ack(skb, req);
1da177e4
LT
835 if (paws_reject)
836 NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED);
837 return NULL;
838 }
839
840 /* In sequence, PAWS is OK. */
841
2e6599cb 842 if (tmp_opt.saw_tstamp && !after(TCP_SKB_CB(skb)->seq, tcp_rsk(req)->rcv_isn + 1))
1da177e4
LT
843 req->ts_recent = tmp_opt.rcv_tsval;
844
2e6599cb 845 if (TCP_SKB_CB(skb)->seq == tcp_rsk(req)->rcv_isn) {
1da177e4 846 /* Truncate SYN, it is out of window starting
2e6599cb 847 at tcp_rsk(req)->rcv_isn + 1. */
1da177e4
LT
848 flg &= ~TCP_FLAG_SYN;
849 }
850
851 /* RFC793: "second check the RST bit" and
852 * "fourth, check the SYN bit"
853 */
854 if (flg & (TCP_FLAG_RST|TCP_FLAG_SYN))
855 goto embryonic_reset;
856
857 /* ACK sequence verified above, just make sure ACK is
858 * set. If ACK not set, just silently drop the packet.
859 */
860 if (!(flg & TCP_FLAG_ACK))
861 return NULL;
862
863 /* If TCP_DEFER_ACCEPT is set, drop bare ACK. */
295f7324
ACM
864 if (inet_csk(sk)->icsk_accept_queue.rskq_defer_accept &&
865 TCP_SKB_CB(skb)->end_seq == tcp_rsk(req)->rcv_isn + 1) {
2e6599cb 866 inet_rsk(req)->acked = 1;
1da177e4
LT
867 return NULL;
868 }
869
870 /* OK, ACK is valid, create big socket and
871 * feed this segment to it. It will repeat all
872 * the tests. THIS SEGMENT MUST MOVE SOCKET TO
873 * ESTABLISHED STATE. If it will be dropped after
874 * socket is created, wait for troubles.
875 */
876 child = tp->af_specific->syn_recv_sock(sk, skb, req, NULL);
877 if (child == NULL)
878 goto listen_overflow;
879
463c84b9
ACM
880 inet_csk_reqsk_queue_unlink(sk, req, prev);
881 inet_csk_reqsk_queue_removed(sk, req);
1da177e4 882
463c84b9 883 inet_csk_reqsk_queue_add(sk, req, child);
1da177e4
LT
884 return child;
885
886 listen_overflow:
887 if (!sysctl_tcp_abort_on_overflow) {
2e6599cb 888 inet_rsk(req)->acked = 1;
1da177e4
LT
889 return NULL;
890 }
891
892 embryonic_reset:
893 NET_INC_STATS_BH(LINUX_MIB_EMBRYONICRSTS);
894 if (!(flg & TCP_FLAG_RST))
60236fdd 895 req->rsk_ops->send_reset(skb);
1da177e4 896
463c84b9 897 inet_csk_reqsk_queue_drop(sk, req, prev);
1da177e4
LT
898 return NULL;
899}
900
901/*
902 * Queue segment on the new socket if the new socket is active,
903 * otherwise we just shortcircuit this and continue with
904 * the new socket.
905 */
906
907int tcp_child_process(struct sock *parent, struct sock *child,
908 struct sk_buff *skb)
909{
910 int ret = 0;
911 int state = child->sk_state;
912
913 if (!sock_owned_by_user(child)) {
914 ret = tcp_rcv_state_process(child, skb, skb->h.th, skb->len);
915
916 /* Wakeup parent, send SIGIO */
917 if (state == TCP_SYN_RECV && child->sk_state != state)
918 parent->sk_data_ready(parent, 0);
919 } else {
920 /* Alas, it is possible again, because we do lookup
921 * in main socket hash table and lock on listening
922 * socket does not protect us more.
923 */
924 sk_add_backlog(child, skb);
925 }
926
927 bh_unlock_sock(child);
928 sock_put(child);
929 return ret;
930}
931
932EXPORT_SYMBOL(tcp_check_req);
933EXPORT_SYMBOL(tcp_child_process);
934EXPORT_SYMBOL(tcp_create_openreq_child);
935EXPORT_SYMBOL(tcp_timewait_state_process);
295ff7ed 936EXPORT_SYMBOL(inet_twsk_deschedule);