blob: d1b479220543db39e905d13006cb85215d3bb9bb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -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 * Implementation of the Transmission Control Protocol(TCP).
7 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Mark Evans, <evansmp@uhura.aston.ac.uk>
11 * Corey Minyard <wf-rch!minyard@relay.EU.net>
12 * Florian La Roche, <flla@stud.uni-sb.de>
13 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
14 * Linus Torvalds, <torvalds@cs.helsinki.fi>
15 * Alan Cox, <gw4pts@gw4pts.ampr.org>
16 * Matthew Dillon, <dillon@apollo.west.oic.com>
17 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
18 * Jorge Cwik, <jorge@laser.satlink.net>
19 */
20
21#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/tcp.h>
24
Brian Haleyab32ea52006-09-22 14:15:41 -070025int sysctl_tcp_syn_retries __read_mostly = TCP_SYN_RETRIES;
26int sysctl_tcp_synack_retries __read_mostly = TCP_SYNACK_RETRIES;
27int sysctl_tcp_keepalive_time __read_mostly = TCP_KEEPALIVE_TIME;
28int sysctl_tcp_keepalive_probes __read_mostly = TCP_KEEPALIVE_PROBES;
29int sysctl_tcp_keepalive_intvl __read_mostly = TCP_KEEPALIVE_INTVL;
30int sysctl_tcp_retries1 __read_mostly = TCP_RETR1;
31int sysctl_tcp_retries2 __read_mostly = TCP_RETR2;
32int sysctl_tcp_orphan_retries __read_mostly;
Andreas Petlund36e31b02010-02-18 02:47:01 +000033int sysctl_tcp_thin_linear_timeouts __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35static void tcp_write_timer(unsigned long);
36static void tcp_delack_timer(unsigned long);
Ravi Joshiee51cf02013-07-25 19:04:17 -070037static void tcp_keepalive_timer(unsigned long data);
38
39/*Function to reset tcp_ack related sysctl on resetting master control */
40void set_tcp_default(void)
41{
42 sysctl_tcp_delack_seg = TCP_DELACK_SEG;
43}
44
45/*sysctl handler for tcp_ack realted master control */
46int tcp_proc_delayed_ack_control(ctl_table *table, int write,
47 void __user *buffer, size_t *length, loff_t *ppos)
48{
49 int ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
50
51 /* The ret value will be 0 if the input validation is successful
52 * and the values are written to sysctl table. If not, the stack
53 * will continue to work with currently configured values
54 */
55 return ret;
56}
57
58/*sysctl handler for tcp_ack realted master control */
59int tcp_use_userconfig_sysctl_handler(ctl_table *table, int write,
60 void __user *buffer, size_t *length, loff_t *ppos)
61{
62 int ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
63
64 if (write && ret == 0) {
65 if (!sysctl_tcp_use_userconfig)
66 set_tcp_default();
67 }
68 return ret;
69}
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071void tcp_init_xmit_timers(struct sock *sk)
72{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070073 inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer,
74 &tcp_keepalive_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -070076EXPORT_SYMBOL(tcp_init_xmit_timers);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static void tcp_write_err(struct sock *sk)
79{
80 sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
81 sk->sk_error_report(sk);
82
83 tcp_done(sk);
Pavel Emelyanovde0744a2008-07-16 20:31:16 -070084 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONTIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87/* Do not allow orphaned sockets to eat all our resources.
88 * This is direct violation of TCP specs, but it is required
89 * to prevent DoS attacks. It is called when a retransmission timeout
90 * or zero probe timeout occurs on orphaned socket.
91 *
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -080092 * Criteria is still not confirmed experimentally and may change.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * We kill the socket, if:
94 * 1. If number of orphaned sockets exceeds an administratively configured
95 * limit.
96 * 2. If we have strong memory pressure.
97 */
98static int tcp_out_of_resources(struct sock *sk, int do_reset)
99{
100 struct tcp_sock *tp = tcp_sk(sk);
David S. Millerad1af0f2010-08-25 02:27:49 -0700101 int shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900103 /* If peer does not open window for long time, or did not transmit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * anything for long time, penalize it. */
105 if ((s32)(tcp_time_stamp - tp->lsndtime) > 2*TCP_RTO_MAX || !do_reset)
David S. Millerad1af0f2010-08-25 02:27:49 -0700106 shift++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /* If some dubious ICMP arrived, penalize even more. */
109 if (sk->sk_err_soft)
David S. Millerad1af0f2010-08-25 02:27:49 -0700110 shift++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Arun Sharmaefcdbf22012-01-30 14:16:06 -0800112 if (tcp_check_oom(sk, shift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* Catch exceptional cases, when connection requires reset.
114 * 1. Last segment was sent recently. */
115 if ((s32)(tcp_time_stamp - tp->lsndtime) <= TCP_TIMEWAIT_LEN ||
116 /* 2. Window is closed. */
117 (!tp->snd_wnd && !tp->packets_out))
118 do_reset = 1;
119 if (do_reset)
120 tcp_send_active_reset(sk, GFP_ATOMIC);
121 tcp_done(sk);
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700122 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONMEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 1;
124 }
125 return 0;
126}
127
128/* Calculate maximal number or retries on an orphaned socket. */
129static int tcp_orphan_retries(struct sock *sk, int alive)
130{
131 int retries = sysctl_tcp_orphan_retries; /* May be zero. */
132
133 /* We know from an ICMP that something is wrong. */
134 if (sk->sk_err_soft && !alive)
135 retries = 0;
136
137 /* However, if socket sent something recently, select some safe
138 * number of retries. 8 corresponds to >100 seconds with minimal
139 * RTO of 200msec. */
140 if (retries == 0 && alive)
141 retries = 8;
142 return retries;
143}
144
Eric Dumazetce55dd32007-12-21 01:50:43 -0800145static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk)
146{
Eric Dumazetce55dd32007-12-21 01:50:43 -0800147 /* Black hole detection */
148 if (sysctl_tcp_mtu_probing) {
149 if (!icsk->icsk_mtup.enabled) {
150 icsk->icsk_mtup.enabled = 1;
151 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
152 } else {
153 struct tcp_sock *tp = tcp_sk(sk);
David S. Miller829942c2007-12-21 04:29:16 -0800154 int mss;
155
Eric Dumazet8beb5c52007-12-21 05:58:29 -0800156 mss = tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low) >> 1;
Eric Dumazetce55dd32007-12-21 01:50:43 -0800157 mss = min(sysctl_tcp_base_mss, mss);
158 mss = max(mss, 68 - tp->tcp_header_len);
159 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss);
160 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
161 }
162 }
163}
164
Damian Lukowski2f7de572009-12-07 06:06:16 +0000165/* This function calculates a "timeout" which is equivalent to the timeout of a
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800166 * TCP connection after "boundary" unsuccessful, exponentially backed-off
Damian Lukowski4d22f7d2010-09-28 13:08:32 -0700167 * retransmissions with an initial RTO of TCP_RTO_MIN or TCP_TIMEOUT_INIT if
168 * syn_set flag is set.
Damian Lukowski2f7de572009-12-07 06:06:16 +0000169 */
170static bool retransmits_timed_out(struct sock *sk,
Jerry Chudca43c72010-08-27 19:13:28 +0000171 unsigned int boundary,
David S. Miller21a180c2010-10-04 11:56:38 -0700172 unsigned int timeout,
Damian Lukowski4d22f7d2010-09-28 13:08:32 -0700173 bool syn_set)
Damian Lukowski2f7de572009-12-07 06:06:16 +0000174{
Jerry Chudca43c72010-08-27 19:13:28 +0000175 unsigned int linear_backoff_thresh, start_ts;
Damian Lukowski4d22f7d2010-09-28 13:08:32 -0700176 unsigned int rto_base = syn_set ? TCP_TIMEOUT_INIT : TCP_RTO_MIN;
Damian Lukowski2f7de572009-12-07 06:06:16 +0000177
178 if (!inet_csk(sk)->icsk_retransmits)
179 return false;
180
181 if (unlikely(!tcp_sk(sk)->retrans_stamp))
182 start_ts = TCP_SKB_CB(tcp_write_queue_head(sk))->when;
183 else
184 start_ts = tcp_sk(sk)->retrans_stamp;
185
Jerry Chudca43c72010-08-27 19:13:28 +0000186 if (likely(timeout == 0)) {
David S. Miller21a180c2010-10-04 11:56:38 -0700187 linear_backoff_thresh = ilog2(TCP_RTO_MAX/rto_base);
Damian Lukowski2f7de572009-12-07 06:06:16 +0000188
Jerry Chudca43c72010-08-27 19:13:28 +0000189 if (boundary <= linear_backoff_thresh)
David S. Miller21a180c2010-10-04 11:56:38 -0700190 timeout = ((2 << boundary) - 1) * rto_base;
Jerry Chudca43c72010-08-27 19:13:28 +0000191 else
David S. Miller21a180c2010-10-04 11:56:38 -0700192 timeout = ((2 << linear_backoff_thresh) - 1) * rto_base +
Jerry Chudca43c72010-08-27 19:13:28 +0000193 (boundary - linear_backoff_thresh) * TCP_RTO_MAX;
194 }
Damian Lukowski2f7de572009-12-07 06:06:16 +0000195 return (tcp_time_stamp - start_ts) >= timeout;
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/* A write timeout has occurred. Process the after effects. */
199static int tcp_write_timeout(struct sock *sk)
200{
John Heffner5d424d52006-03-20 17:53:41 -0800201 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int retry_until;
Rusty Russell3db1cd52011-12-19 13:56:45 +0000203 bool do_reset, syn_set = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700206 if (icsk->icsk_retransmits)
Eric Dumazetb6c67122010-04-08 23:03:29 +0000207 dst_negative_advice(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700208 retry_until = icsk->icsk_syn_retries ? : sysctl_tcp_syn_retries;
Rusty Russell3db1cd52011-12-19 13:56:45 +0000209 syn_set = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 } else {
David S. Miller21a180c2010-10-04 11:56:38 -0700211 if (retransmits_timed_out(sk, sysctl_tcp_retries1, 0, 0)) {
John Heffner5d424d52006-03-20 17:53:41 -0800212 /* Black hole detection */
Eric Dumazetce55dd32007-12-21 01:50:43 -0800213 tcp_mtu_probing(icsk, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Eric Dumazetb6c67122010-04-08 23:03:29 +0000215 dst_negative_advice(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
218 retry_until = sysctl_tcp_retries2;
219 if (sock_flag(sk, SOCK_DEAD)) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700220 const int alive = (icsk->icsk_rto < TCP_RTO_MAX);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 retry_until = tcp_orphan_retries(sk, alive);
Damian Lukowski6fa12c82009-08-26 00:16:34 +0000223 do_reset = alive ||
David S. Miller21a180c2010-10-04 11:56:38 -0700224 !retransmits_timed_out(sk, retry_until, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Damian Lukowski6fa12c82009-08-26 00:16:34 +0000226 if (tcp_out_of_resources(sk, do_reset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return 1;
228 }
229 }
230
Jerry Chudca43c72010-08-27 19:13:28 +0000231 if (retransmits_timed_out(sk, retry_until,
David S. Miller21a180c2010-10-04 11:56:38 -0700232 syn_set ? 0 : icsk->icsk_user_timeout, syn_set)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* Has it gone just too far? */
234 tcp_write_err(sk);
235 return 1;
236 }
237 return 0;
238}
239
240static void tcp_delack_timer(unsigned long data)
241{
Jianjun Kongfd3f8c42008-11-03 02:47:38 -0800242 struct sock *sk = (struct sock *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700244 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 bh_lock_sock(sk);
247 if (sock_owned_by_user(sk)) {
248 /* Try again later. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700249 icsk->icsk_ack.blocked = 1;
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700250 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700251 sk_reset_timer(sk, &icsk->icsk_delack_timer, jiffies + TCP_DELACK_MIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 goto out_unlock;
253 }
254
David S. Miller9993e7d2008-01-10 21:56:38 -0800255 sk_mem_reclaim_partial(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700257 if (sk->sk_state == TCP_CLOSE || !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto out;
259
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700260 if (time_after(icsk->icsk_ack.timeout, jiffies)) {
261 sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto out;
263 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700264 icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
David S. Millerb03efcf2005-07-08 14:57:23 -0700266 if (!skb_queue_empty(&tp->ucopy.prequeue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct sk_buff *skb;
268
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700269 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSCHEDULERFAILED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 while ((skb = __skb_dequeue(&tp->ucopy.prequeue)) != NULL)
Peter Zijlstrac57943a2008-10-07 14:18:42 -0700272 sk_backlog_rcv(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 tp->ucopy.memory = 0;
275 }
276
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700277 if (inet_csk_ack_scheduled(sk)) {
278 if (!icsk->icsk_ack.pingpong) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* Delayed ACK missed: inflate ATO. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700280 icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, icsk->icsk_rto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 } else {
282 /* Delayed ACK missed: leave pingpong mode and
283 * deflate ATO.
284 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700285 icsk->icsk_ack.pingpong = 0;
286 icsk->icsk_ack.ato = TCP_ATO_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 tcp_send_ack(sk);
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700289 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292out:
Glauber Costa180d8cd2011-12-11 21:47:02 +0000293 if (sk_under_memory_pressure(sk))
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800294 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295out_unlock:
296 bh_unlock_sock(sk);
297 sock_put(sk);
298}
299
300static void tcp_probe_timer(struct sock *sk)
301{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300302 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 struct tcp_sock *tp = tcp_sk(sk);
304 int max_probes;
305
David S. Millerfe067e82007-03-07 12:12:44 -0800306 if (tp->packets_out || !tcp_send_head(sk)) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300307 icsk->icsk_probes_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return;
309 }
310
311 /* *WARNING* RFC 1122 forbids this
312 *
313 * It doesn't AFAIK, because we kill the retransmit timer -AK
314 *
315 * FIXME: We ought not to do it, Solaris 2.5 actually has fixing
316 * this behaviour in Solaris down as a bug fix. [AC]
317 *
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300318 * Let me to explain. icsk_probes_out is zeroed by incoming ACKs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * even if they advertise zero window. Hence, connection is killed only
320 * if we received no ACKs for normal connection timeout. It is not killed
321 * only because window stays zero for some time, window may be zero
322 * until armageddon and even later. We are in full accordance
323 * with RFCs, only probe timer combines both retransmission timeout
324 * and probe timeout in one bottle. --ANK
325 */
326 max_probes = sysctl_tcp_retries2;
327
328 if (sock_flag(sk, SOCK_DEAD)) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700329 const int alive = ((icsk->icsk_rto << icsk->icsk_backoff) < TCP_RTO_MAX);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 max_probes = tcp_orphan_retries(sk, alive);
332
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300333 if (tcp_out_of_resources(sk, alive || icsk->icsk_probes_out <= max_probes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return;
335 }
336
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300337 if (icsk->icsk_probes_out > max_probes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 tcp_write_err(sk);
339 } else {
340 /* Only send another probe if we didn't close things up. */
341 tcp_send_probe0(sk);
342 }
343}
344
345/*
346 * The TCP retransmit timer.
347 */
348
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000349void tcp_retransmit_timer(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700352 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (!tp->packets_out)
355 goto out;
356
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700357 WARN_ON(tcp_write_queue_empty(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 if (!tp->snd_wnd && !sock_flag(sk, SOCK_DEAD) &&
360 !((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV))) {
361 /* Receiver dastardly shrinks window. Our retransmits
362 * become zero probes, but we should not timeout this
363 * connection. If the socket is an orphan, time it out,
364 * we cannot allow such beasts to hang infinitely.
365 */
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -0700366 struct inet_sock *inet = inet_sk(sk);
367 if (sk->sk_family == AF_INET) {
Joe Perchesafd465032012-03-12 07:03:32 +0000368 LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("Peer %pI4:%u/%u unexpectedly shrunk window %u:%u (repaired)\n"),
369 &inet->inet_daddr,
370 ntohs(inet->inet_dport), inet->inet_num,
371 tp->snd_una, tp->snd_nxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000373#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -0700374 else if (sk->sk_family == AF_INET6) {
375 struct ipv6_pinfo *np = inet6_sk(sk);
Joe Perchesafd465032012-03-12 07:03:32 +0000376 LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n"),
377 &np->daddr,
378 ntohs(inet->inet_dport), inet->inet_num,
379 tp->snd_una, tp->snd_nxt);
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -0700380 }
381#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (tcp_time_stamp - tp->rcv_tstamp > TCP_RTO_MAX) {
383 tcp_write_err(sk);
384 goto out;
385 }
386 tcp_enter_loss(sk, 0);
David S. Millerfe067e82007-03-07 12:12:44 -0800387 tcp_retransmit_skb(sk, tcp_write_queue_head(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 __sk_dst_reset(sk);
389 goto out_reset_timer;
390 }
391
392 if (tcp_write_timeout(sk))
393 goto out;
394
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700395 if (icsk->icsk_retransmits == 0) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700396 int mib_idx;
397
Ilpo Järvinenc60ce4e2010-10-14 01:52:09 +0000398 if (icsk->icsk_ca_state == TCP_CA_Recovery) {
Ilpo Järvinenbc079e92009-02-28 04:44:34 +0000399 if (tcp_is_sack(tp))
400 mib_idx = LINUX_MIB_TCPSACKRECOVERYFAIL;
401 else
402 mib_idx = LINUX_MIB_TCPRENORECOVERYFAIL;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300403 } else if (icsk->icsk_ca_state == TCP_CA_Loss) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700404 mib_idx = LINUX_MIB_TCPLOSSFAILURES;
Ilpo Järvinenc60ce4e2010-10-14 01:52:09 +0000405 } else if ((icsk->icsk_ca_state == TCP_CA_Disorder) ||
406 tp->sacked_out) {
407 if (tcp_is_sack(tp))
408 mib_idx = LINUX_MIB_TCPSACKFAILURES;
409 else
410 mib_idx = LINUX_MIB_TCPRENOFAILURES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 } else {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700412 mib_idx = LINUX_MIB_TCPTIMEOUTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700414 NET_INC_STATS_BH(sock_net(sk), mib_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
417 if (tcp_use_frto(sk)) {
418 tcp_enter_frto(sk);
419 } else {
420 tcp_enter_loss(sk, 0);
421 }
422
David S. Millerfe067e82007-03-07 12:12:44 -0800423 if (tcp_retransmit_skb(sk, tcp_write_queue_head(sk)) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* Retransmission failed because of local congestion,
425 * do not backoff.
426 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700427 if (!icsk->icsk_retransmits)
428 icsk->icsk_retransmits = 1;
429 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700430 min(icsk->icsk_rto, TCP_RESOURCE_PROBE_INTERVAL),
431 TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 goto out;
433 }
434
435 /* Increase the timeout each time we retransmit. Note that
436 * we do not increase the rtt estimate. rto is initialized
437 * from rtt, but increases here. Jacobson (SIGCOMM 88) suggests
438 * that doubling rto each time is the least we can get away with.
439 * In KA9Q, Karn uses this for the first few times, and then
440 * goes to quadratic. netBSD doubles, but only goes up to *64,
441 * and clamps at 1 to 64 sec afterwards. Note that 120 sec is
442 * defined in the protocol as the maximum possible RTT. I guess
443 * we'll have to use something other than TCP to talk to the
444 * University of Mars.
445 *
446 * PAWS allows us longer timeouts and large windows, so once
447 * implemented ftp to mars will work nicely. We will have to fix
448 * the 120 second clamps though!
449 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700450 icsk->icsk_backoff++;
451 icsk->icsk_retransmits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453out_reset_timer:
Andreas Petlund36e31b02010-02-18 02:47:01 +0000454 /* If stream is thin, use linear timeouts. Since 'icsk_backoff' is
455 * used to reset timer, set to 0. Recalculate 'icsk_rto' as this
456 * might be increased if the stream oscillates between thin and thick,
457 * thus the old value might already be too high compared to the value
458 * set by 'tcp_set_rto' in tcp_input.c which resets the rto without
459 * backoff. Limit to TCP_THIN_LINEAR_RETRIES before initiating
460 * exponential backoff behaviour to avoid continue hammering
461 * linear-timeout retransmissions into a black hole
462 */
463 if (sk->sk_state == TCP_ESTABLISHED &&
464 (tp->thin_lto || sysctl_tcp_thin_linear_timeouts) &&
465 tcp_stream_is_thin(tp) &&
466 icsk->icsk_retransmits <= TCP_THIN_LINEAR_RETRIES) {
467 icsk->icsk_backoff = 0;
468 icsk->icsk_rto = min(__tcp_set_rto(tp), TCP_RTO_MAX);
469 } else {
470 /* Use normal (exponential) backoff */
471 icsk->icsk_rto = min(icsk->icsk_rto << 1, TCP_RTO_MAX);
472 }
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -0700473 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, TCP_RTO_MAX);
David S. Miller21a180c2010-10-04 11:56:38 -0700474 if (retransmits_timed_out(sk, sysctl_tcp_retries1 + 1, 0, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 __sk_dst_reset(sk);
476
477out:;
478}
479
480static void tcp_write_timer(unsigned long data)
481{
Jianjun Kongfd3f8c42008-11-03 02:47:38 -0800482 struct sock *sk = (struct sock *)data;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700483 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int event;
485
486 bh_lock_sock(sk);
487 if (sock_owned_by_user(sk)) {
488 /* Try again later */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700489 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + (HZ / 20));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 goto out_unlock;
491 }
492
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700493 if (sk->sk_state == TCP_CLOSE || !icsk->icsk_pending)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto out;
495
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700496 if (time_after(icsk->icsk_timeout, jiffies)) {
497 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 goto out;
499 }
500
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700501 event = icsk->icsk_pending;
502 icsk->icsk_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 switch (event) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700505 case ICSK_TIME_RETRANS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 tcp_retransmit_timer(sk);
507 break;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700508 case ICSK_TIME_PROBE0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 tcp_probe_timer(sk);
510 break;
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513out:
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800514 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515out_unlock:
516 bh_unlock_sock(sk);
517 sock_put(sk);
518}
519
Arnaldo Carvalho de Melo295f7322005-08-09 20:11:56 -0700520/*
521 * Timer for listening sockets
522 */
523
524static void tcp_synack_timer(struct sock *sk)
525{
Arnaldo Carvalho de Meloa019d6f2005-08-09 20:15:09 -0700526 inet_csk_reqsk_queue_prune(sk, TCP_SYNQ_INTERVAL,
527 TCP_TIMEOUT_INIT, TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Octavian Purdila72659ec2010-01-17 19:09:39 -0800530void tcp_syn_ack_timeout(struct sock *sk, struct request_sock *req)
531{
532 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPTIMEOUTS);
533}
534EXPORT_SYMBOL(tcp_syn_ack_timeout);
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536void tcp_set_keepalive(struct sock *sk, int val)
537{
538 if ((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))
539 return;
540
541 if (val && !sock_flag(sk, SOCK_KEEPOPEN))
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700542 inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tcp_sk(sk)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 else if (!val)
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700544 inet_csk_delete_keepalive_timer(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547
548static void tcp_keepalive_timer (unsigned long data)
549{
550 struct sock *sk = (struct sock *) data;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300551 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 struct tcp_sock *tp = tcp_sk(sk);
Flavio Leitner6c37e5d2010-04-26 18:33:27 +0000553 u32 elapsed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 /* Only process if socket is not in use. */
556 bh_lock_sock(sk);
557 if (sock_owned_by_user(sk)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900558 /* Try again later. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700559 inet_csk_reset_keepalive_timer (sk, HZ/20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto out;
561 }
562
563 if (sk->sk_state == TCP_LISTEN) {
564 tcp_synack_timer(sk);
565 goto out;
566 }
567
568 if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) {
569 if (tp->linger2 >= 0) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700570 const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (tmo > 0) {
573 tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
574 goto out;
575 }
576 }
577 tcp_send_active_reset(sk, GFP_ATOMIC);
578 goto death;
579 }
580
581 if (!sock_flag(sk, SOCK_KEEPOPEN) || sk->sk_state == TCP_CLOSE)
582 goto out;
583
584 elapsed = keepalive_time_when(tp);
585
586 /* It is alive without keepalive 8) */
David S. Millerfe067e82007-03-07 12:12:44 -0800587 if (tp->packets_out || tcp_send_head(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 goto resched;
589
Flavio Leitner6c37e5d2010-04-26 18:33:27 +0000590 elapsed = keepalive_time_elapsed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 if (elapsed >= keepalive_time_when(tp)) {
Jerry Chudca43c72010-08-27 19:13:28 +0000593 /* If the TCP_USER_TIMEOUT option is enabled, use that
594 * to determine when to timeout instead.
595 */
596 if ((icsk->icsk_user_timeout != 0 &&
597 elapsed >= icsk->icsk_user_timeout &&
598 icsk->icsk_probes_out > 0) ||
599 (icsk->icsk_user_timeout == 0 &&
600 icsk->icsk_probes_out >= keepalive_probes(tp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 tcp_send_active_reset(sk, GFP_ATOMIC);
602 tcp_write_err(sk);
603 goto out;
604 }
605 if (tcp_write_wakeup(sk) <= 0) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300606 icsk->icsk_probes_out++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 elapsed = keepalive_intvl_when(tp);
608 } else {
609 /* If keepalive was lost due to local congestion,
610 * try harder.
611 */
612 elapsed = TCP_RESOURCE_PROBE_INTERVAL;
613 }
614 } else {
615 /* It is tp->rcv_tstamp + keepalive_time_when(tp) */
616 elapsed = keepalive_time_when(tp) - elapsed;
617 }
618
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800619 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621resched:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700622 inet_csk_reset_keepalive_timer (sk, elapsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto out;
624
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900625death:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 tcp_done(sk);
627
628out:
629 bh_unlock_sock(sk);
630 sock_put(sk);
631}