blob: 76ca88f63b70b21bd2ec5c783471528b9599439b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * Implementation of the Transmission Control Protocol(TCP).
8 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07009 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
11 * Mark Evans, <evansmp@uhura.aston.ac.uk>
12 * Corey Minyard <wf-rch!minyard@relay.EU.net>
13 * Florian La Roche, <flla@stud.uni-sb.de>
14 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
15 * Linus Torvalds, <torvalds@cs.helsinki.fi>
16 * Alan Cox, <gw4pts@gw4pts.ampr.org>
17 * Matthew Dillon, <dillon@apollo.west.oic.com>
18 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
19 * Jorge Cwik, <jorge@laser.satlink.net>
20 */
21
22/*
23 * Changes:
24 * Pedro Roque : Fast Retransmit/Recovery.
25 * Two receive queues.
26 * Retransmit queue handled by TCP.
27 * Better retransmit timer handling.
28 * New congestion avoidance.
29 * Header prediction.
30 * Variable renaming.
31 *
32 * Eric : Fast Retransmit.
33 * Randy Scott : MSS option defines.
34 * Eric Schenk : Fixes to slow start algorithm.
35 * Eric Schenk : Yet another double ACK bug.
36 * Eric Schenk : Delayed ACK bug fixes.
37 * Eric Schenk : Floyd style fast retrans war avoidance.
38 * David S. Miller : Don't allow zero congestion window.
39 * Eric Schenk : Fix retransmitter so that it sends
40 * next packet on ack of previous packet.
41 * Andi Kleen : Moved open_request checking here
42 * and process RSTs for open_requests.
43 * Andi Kleen : Better prune_queue, and other fixes.
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -080044 * Andrey Savochkin: Fix RTT measurements in the presence of
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * timestamps.
46 * Andrey Savochkin: Check sequence numbers correctly when
47 * removing SACKs due to in sequence incoming
48 * data segments.
49 * Andi Kleen: Make sure we never ack data there is not
50 * enough room for. Also make this condition
51 * a fatal error if it might still happen.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090052 * Andi Kleen: Add tcp_measure_rcv_mss to make
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * connections with MSS<min(MTU,ann. MSS)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090054 * work without delayed acks.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * Andi Kleen: Process packets with PSH set in the
56 * fast path.
57 * J Hadi Salim: ECN support
58 * Andrei Gurtov,
59 * Pasi Sarolahti,
60 * Panu Kuhlberg: Experimental audit of TCP (re)transmission
61 * engine. Lots of bugs are found.
62 * Pasi Sarolahti: F-RTO for dealing with spurious RTOs
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 */
64
Joe Perchesafd465032012-03-12 07:03:32 +000065#define pr_fmt(fmt) "TCP: " fmt
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090068#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/module.h>
70#include <linux/sysctl.h>
Ilpo Järvinena0bffff2009-03-21 13:36:17 -070071#include <linux/kernel.h>
Eric Dumazetad971f62014-10-11 15:17:29 -070072#include <linux/prefetch.h>
Satoru SATOH5ffc02a2008-05-04 22:14:42 -070073#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <net/tcp.h>
75#include <net/inet_common.h>
76#include <linux/ipsec.h>
77#include <asm/unaligned.h>
Willem de Bruijne1c8a602014-08-04 22:11:50 -040078#include <linux/errqueue.h>
Song Liu59415212017-10-23 09:20:25 -070079#include <trace/events/tcp.h>
Ursula Braun60e2a772017-10-25 11:01:45 +020080#include <linux/static_key.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Brian Haleyab32ea52006-09-22 14:15:41 -070082int sysctl_tcp_max_orphans __read_mostly = NR_FILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define FLAG_DATA 0x01 /* Incoming frame contained data. */
85#define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */
86#define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */
87#define FLAG_RETRANS_DATA_ACKED 0x08 /* "" "" some of which was retransmitted. */
88#define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */
89#define FLAG_DATA_SACKED 0x20 /* New SACK. */
90#define FLAG_ECE 0x40 /* ECE in this ACK */
Yuchung Cheng291a00d2015-07-01 14:11:14 -070091#define FLAG_LOST_RETRANS 0x80 /* This ACK marks some retransmission lost */
Florian Westphal31770e32017-08-30 19:24:58 +020092#define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/
Yuchung Chenge33099f2013-03-20 13:33:00 +000093#define FLAG_ORIG_SACK_ACKED 0x200 /* Never retransmitted data are (s)acked */
Ilpo Järvinen2e605292007-08-02 19:46:58 -070094#define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */
Ryousei Takano564262c2007-10-25 23:03:52 -070095#define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */
Neal Cardwelldf92c832017-08-03 09:19:54 -040096#define FLAG_SET_XMIT_TIMER 0x1000 /* Set TLP or RTO timer */
Ilpo Järvinencadbd032007-12-31 04:49:21 -080097#define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */
Eric Dumazet12fb3dd2013-04-19 07:19:48 +000098#define FLAG_UPDATE_TS_RECENT 0x4000 /* tcp_replace_ts_recent() */
Eric Dumazetd0e1a1b2017-05-23 15:24:46 -070099#define FLAG_NO_CHALLENGE_ACK 0x8000 /* do not call tcp_send_challenge_ack() */
Yuchung Chengeb36be02018-01-17 12:11:00 -0800100#define FLAG_ACK_MAYBE_DELAYED 0x10000 /* Likely a delayed ACK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102#define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED)
103#define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
Priyaranjan Jhad09b9e62017-11-03 17:46:55 -0700104#define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE|FLAG_DSACKING_ACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED)
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700108#define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Yuchung Chenge662ca42016-02-02 10:33:04 -0800110#define REXMIT_NONE 0 /* no loss recovery to do */
111#define REXMIT_LOST 1 /* retransmit packets marked lost */
112#define REXMIT_NEW 2 /* FRTO-style transmit of unsent/new packets */
113
Ilya Lesokhin6dac1522018-04-30 10:16:10 +0300114#if IS_ENABLED(CONFIG_TLS_DEVICE)
115static DEFINE_STATIC_KEY_FALSE(clean_acked_data_enabled);
116
117void clean_acked_data_enable(struct inet_connection_sock *icsk,
118 void (*cad)(struct sock *sk, u32 ack_seq))
119{
120 icsk->icsk_clean_acked = cad;
121 static_branch_inc(&clean_acked_data_enabled);
122}
123EXPORT_SYMBOL_GPL(clean_acked_data_enable);
124
125void clean_acked_data_disable(struct inet_connection_sock *icsk)
126{
127 static_branch_dec(&clean_acked_data_enabled);
128 icsk->icsk_clean_acked = NULL;
129}
130EXPORT_SYMBOL_GPL(clean_acked_data_disable);
131#endif
132
Marcelo Ricardo Leitner0b9aefe2017-04-01 11:00:21 -0300133static void tcp_gro_dev_warn(struct sock *sk, const struct sk_buff *skb,
134 unsigned int len)
Marcelo Ricardo Leitnerdcb17d22016-12-05 18:37:13 -0200135{
136 static bool __once __read_mostly;
137
138 if (!__once) {
139 struct net_device *dev;
140
141 __once = true;
142
143 rcu_read_lock();
144 dev = dev_get_by_index_rcu(sock_net(sk), skb->skb_iif);
Marcelo Ricardo Leitner0b9aefe2017-04-01 11:00:21 -0300145 if (!dev || len >= dev->mtu)
146 pr_warn("%s: Driver has suspect GRO implementation, TCP performance may be compromised.\n",
147 dev ? dev->name : "Unknown driver");
Marcelo Ricardo Leitnerdcb17d22016-12-05 18:37:13 -0200148 rcu_read_unlock();
149 }
150}
151
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900152/* Adapt the MSS value used to make delayed ack decision to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * real world.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900154 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800155static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700157 struct inet_connection_sock *icsk = inet_csk(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900158 const unsigned int lss = icsk->icsk_ack.last_seg_size;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700159 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900161 icsk->icsk_ack.last_seg_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 /* skb->len may jitter because of SACKs, even if peer
164 * sends good full-sized frames.
165 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800166 len = skb_shinfo(skb)->gso_size ? : skb->len;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700167 if (len >= icsk->icsk_ack.rcv_mss) {
Marcelo Ricardo Leitnerdcb17d22016-12-05 18:37:13 -0200168 icsk->icsk_ack.rcv_mss = min_t(unsigned int, len,
169 tcp_sk(sk)->advmss);
Marcelo Ricardo Leitner0b9aefe2017-04-01 11:00:21 -0300170 /* Account for possibly-removed options */
171 if (unlikely(len > icsk->icsk_ack.rcv_mss +
172 MAX_TCP_OPTION_SPACE))
173 tcp_gro_dev_warn(sk, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 } else {
175 /* Otherwise, we make more careful check taking into account,
176 * that SACKs block is variable.
177 *
178 * "len" is invariant segment length, including TCP header.
179 */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700180 len += skb->data - skb_transport_header(skb);
William Allen Simpsonbee7ca92009-11-10 09:51:18 +0000181 if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 /* If PSH is not set, packet should be
183 * full sized, provided peer TCP is not badly broken.
184 * This observation (if it is correct 8)) allows
185 * to handle super-low mtu links fairly.
186 */
187 (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700188 !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* Subtract also invariant (if peer is RFC compliant),
190 * tcp header plus fixed timestamp option length.
191 * Resulting "len" is MSS free of SACK jitter.
192 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700193 len -= tcp_sk(sk)->tcp_header_len;
194 icsk->icsk_ack.last_seg_size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (len == lss) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700196 icsk->icsk_ack.rcv_mss = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return;
198 }
199 }
Alexey Kuznetsov1ef96962006-09-19 12:52:50 -0700200 if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)
201 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700202 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204}
205
Eric Dumazet9a9c9b52018-05-21 15:08:56 -0700206static void tcp_incr_quickack(struct sock *sk, unsigned int max_quickacks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700208 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000209 unsigned int quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800211 if (quickacks == 0)
212 quickacks = 2;
Eric Dumazet9a9c9b52018-05-21 15:08:56 -0700213 quickacks = min(quickacks, max_quickacks);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700214 if (quickacks > icsk->icsk_ack.quick)
Eric Dumazet9a9c9b52018-05-21 15:08:56 -0700215 icsk->icsk_ack.quick = quickacks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Eric Dumazet9a9c9b52018-05-21 15:08:56 -0700218static void tcp_enter_quickack_mode(struct sock *sk, unsigned int max_quickacks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700220 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet9a9c9b52018-05-21 15:08:56 -0700221
222 tcp_incr_quickack(sk, max_quickacks);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700223 icsk->icsk_ack.pingpong = 0;
224 icsk->icsk_ack.ato = TCP_ATO_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227/* Send ACKs quickly, if "quick" count is not exhausted
228 * and the session is not interactive.
229 */
230
Jon Maxwell2251ae42015-07-08 10:12:28 +1000231static bool tcp_in_quickack_mode(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700233 const struct inet_connection_sock *icsk = inet_csk(sk);
Jon Maxwell2251ae42015-07-08 10:12:28 +1000234 const struct dst_entry *dst = __sk_dst_get(sk);
Eric Dumazeta2a385d2012-05-16 23:15:34 +0000235
Jon Maxwell2251ae42015-07-08 10:12:28 +1000236 return (dst && dst_metric(dst, RTAX_QUICKACK)) ||
237 (icsk->icsk_ack.quick && !icsk->icsk_ack.pingpong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Florian Westphal735d3832014-09-29 13:08:30 +0200240static void tcp_ecn_queue_cwr(struct tcp_sock *tp)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700241{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800242 if (tp->ecn_flags & TCP_ECN_OK)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700243 tp->ecn_flags |= TCP_ECN_QUEUE_CWR;
244}
245
Florian Westphal735d3832014-09-29 13:08:30 +0200246static void tcp_ecn_accept_cwr(struct tcp_sock *tp, const struct sk_buff *skb)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700247{
248 if (tcp_hdr(skb)->cwr)
249 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
250}
251
Florian Westphal735d3832014-09-29 13:08:30 +0200252static void tcp_ecn_withdraw_cwr(struct tcp_sock *tp)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700253{
254 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
255}
256
Yousuk Seungf4c9f852018-06-04 15:29:51 -0700257static void __tcp_ecn_check_ce(struct sock *sk, const struct sk_buff *skb)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700258{
Yousuk Seungf4c9f852018-06-04 15:29:51 -0700259 struct tcp_sock *tp = tcp_sk(sk);
260
Eric Dumazetb82d1bb2011-09-27 02:20:08 -0400261 switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) {
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000262 case INET_ECN_NOT_ECT:
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700263 /* Funny extension: if ECT is not set on a segment,
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000264 * and we already seen ECT on a previous segment,
265 * it is probably a retransmit.
266 */
267 if (tp->ecn_flags & TCP_ECN_SEEN)
Yousuk Seungf4c9f852018-06-04 15:29:51 -0700268 tcp_enter_quickack_mode(sk, 1);
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000269 break;
270 case INET_ECN_CE:
Yousuk Seungf4c9f852018-06-04 15:29:51 -0700271 if (tcp_ca_needs_ecn(sk))
272 tcp_ca_event(sk, CA_EVENT_ECN_IS_CE);
Florian Westphal98900922014-09-26 22:37:35 +0200273
Eric Dumazetaae06bf2012-08-06 11:04:43 +0000274 if (!(tp->ecn_flags & TCP_ECN_DEMAND_CWR)) {
275 /* Better not delay acks, sender can have a very low cwnd */
Yousuk Seungf4c9f852018-06-04 15:29:51 -0700276 tcp_enter_quickack_mode(sk, 1);
Eric Dumazetaae06bf2012-08-06 11:04:43 +0000277 tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
278 }
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000279 tp->ecn_flags |= TCP_ECN_SEEN;
Florian Westphal98900922014-09-26 22:37:35 +0200280 break;
281 default:
Yousuk Seungf4c9f852018-06-04 15:29:51 -0700282 if (tcp_ca_needs_ecn(sk))
283 tcp_ca_event(sk, CA_EVENT_ECN_NO_CE);
Florian Westphal98900922014-09-26 22:37:35 +0200284 tp->ecn_flags |= TCP_ECN_SEEN;
285 break;
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700286 }
287}
288
Yousuk Seungf4c9f852018-06-04 15:29:51 -0700289static void tcp_ecn_check_ce(struct sock *sk, const struct sk_buff *skb)
Florian Westphal735d3832014-09-29 13:08:30 +0200290{
Yousuk Seungf4c9f852018-06-04 15:29:51 -0700291 if (tcp_sk(sk)->ecn_flags & TCP_ECN_OK)
292 __tcp_ecn_check_ce(sk, skb);
Florian Westphal735d3832014-09-29 13:08:30 +0200293}
294
295static void tcp_ecn_rcv_synack(struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700296{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800297 if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr))
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700298 tp->ecn_flags &= ~TCP_ECN_OK;
299}
300
Florian Westphal735d3832014-09-29 13:08:30 +0200301static void tcp_ecn_rcv_syn(struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700302{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800303 if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr))
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700304 tp->ecn_flags &= ~TCP_ECN_OK;
305}
306
Florian Westphal735d3832014-09-29 13:08:30 +0200307static bool tcp_ecn_rcv_ecn_echo(const struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700308{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800309 if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK))
Eric Dumazeta2a385d2012-05-16 23:15:34 +0000310 return true;
311 return false;
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/* Buffer size and advertised window tuning.
315 *
316 * 1. Tuning sk->sk_sndbuf, when connection enters established state.
317 */
318
Eric Dumazet6ae70532013-10-01 10:23:44 -0700319static void tcp_sndbuf_expand(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Eric Dumazet6ae70532013-10-01 10:23:44 -0700321 const struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng77bfc172016-09-19 23:39:20 -0400322 const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
Eric Dumazet6ae70532013-10-01 10:23:44 -0700323 int sndmem, per_mss;
324 u32 nr_segs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Eric Dumazet6ae70532013-10-01 10:23:44 -0700326 /* Worst case is non GSO/TSO : each frame consumes one skb
327 * and skb->head is kmalloced using power of two area of memory
328 */
329 per_mss = max_t(u32, tp->rx_opt.mss_clamp, tp->mss_cache) +
330 MAX_TCP_HEADER +
331 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
332
333 per_mss = roundup_pow_of_two(per_mss) +
334 SKB_DATA_ALIGN(sizeof(struct sk_buff));
335
336 nr_segs = max_t(u32, TCP_INIT_CWND, tp->snd_cwnd);
337 nr_segs = max_t(u32, nr_segs, tp->reordering + 1);
338
339 /* Fast Recovery (RFC 5681 3.2) :
340 * Cubic needs 1.7 factor, rounded to 2 to include
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800341 * extra cushion (application might react slowly to EPOLLOUT)
Eric Dumazet6ae70532013-10-01 10:23:44 -0700342 */
Yuchung Cheng77bfc172016-09-19 23:39:20 -0400343 sndmem = ca_ops->sndbuf_expand ? ca_ops->sndbuf_expand(sk) : 2;
344 sndmem *= nr_segs * per_mss;
Eric Dumazet6ae70532013-10-01 10:23:44 -0700345
Eric Dumazet06a59ec2011-10-13 18:24:42 +0000346 if (sk->sk_sndbuf < sndmem)
Eric Dumazet356d1832017-11-07 00:29:28 -0800347 sk->sk_sndbuf = min(sndmem, sock_net(sk)->ipv4.sysctl_tcp_wmem[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350/* 2. Tuning advertised window (window_clamp, rcv_ssthresh)
351 *
352 * All tcp_full_space() is split to two parts: "network" buffer, allocated
353 * forward and advertised in receiver window (tp->rcv_wnd) and
354 * "application buffer", required to isolate scheduling/application
355 * latencies from network.
356 * window_clamp is maximal advertised window. It can be less than
357 * tcp_full_space(), in this case tcp_full_space() - window_clamp
358 * is reserved for "application" buffer. The less window_clamp is
359 * the smoother our behaviour from viewpoint of network, but the lower
360 * throughput and the higher sensitivity of the connection to losses. 8)
361 *
362 * rcv_ssthresh is more strict window_clamp used at "slow start"
363 * phase to predict further behaviour of this connection.
364 * It is used for two goals:
365 * - to enforce header prediction at sender, even when application
366 * requires some significant "application buffer". It is check #1.
367 * - to prevent pruning of receive queue because of misprediction
368 * of receiver window. Check #2.
369 *
370 * The scheme does not work when sender sends good segments opening
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800371 * window and then starts to feed us spaghetti. But it should work
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * in common situations. Otherwise, we have to rely on queue collapsing.
373 */
374
375/* Slow part of check#2. */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700376static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700378 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* Optimize this! */
Eric Dumazet94f08932017-10-26 21:55:09 -0700380 int truesize = tcp_win_from_space(sk, skb->truesize) >> 1;
Eric Dumazet356d1832017-11-07 00:29:28 -0800381 int window = tcp_win_from_space(sk, sock_net(sk)->ipv4.sysctl_tcp_rmem[2]) >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 while (tp->rcv_ssthresh <= window) {
384 if (truesize <= skb->len)
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700385 return 2 * inet_csk(sk)->icsk_ack.rcv_mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 truesize >>= 1;
388 window >>= 1;
389 }
390 return 0;
391}
392
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400393static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700395 struct tcp_sock *tp = tcp_sk(sk);
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* Check #1 */
398 if (tp->rcv_ssthresh < tp->window_clamp &&
399 (int)tp->rcv_ssthresh < tcp_space(sk) &&
Eric Dumazetb8da51e2015-05-15 12:39:27 -0700400 !tcp_under_memory_pressure(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 int incr;
402
403 /* Check #2. Increase window, if skb with such overhead
404 * will fit to rcvbuf in future.
405 */
Eric Dumazet94f08932017-10-26 21:55:09 -0700406 if (tcp_win_from_space(sk, skb->truesize) <= skb->len)
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800407 incr = 2 * tp->advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 else
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700409 incr = __tcp_grow_window(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 if (incr) {
Eric Dumazet4d846f02012-04-16 23:28:07 +0000412 incr = max_t(int, incr, 2 * skb->len);
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800413 tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr,
414 tp->window_clamp);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700415 inet_csk(sk)->icsk_ack.quick |= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 }
418}
419
420/* 3. Tuning rcvbuf, when connection enters established state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static void tcp_fixup_rcvbuf(struct sock *sk)
422{
Eric Dumazete9266a02011-10-20 16:53:56 -0400423 u32 mss = tcp_sk(sk)->advmss;
Eric Dumazete9266a02011-10-20 16:53:56 -0400424 int rcvmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Yuchung Cheng85f16522013-06-11 15:35:32 -0700426 rcvmem = 2 * SKB_TRUESIZE(mss + MAX_TCP_HEADER) *
427 tcp_default_init_rwnd(mss);
Eric Dumazete9266a02011-10-20 16:53:56 -0400428
Eric Dumazetb0983d32013-09-20 13:56:58 -0700429 /* Dynamic Right Sizing (DRS) has 2 to 3 RTT latency
430 * Allow enough cushion so that sender is not limited by our window
431 */
Eric Dumazet4540c0c2017-10-27 07:47:22 -0700432 if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf)
Eric Dumazetb0983d32013-09-20 13:56:58 -0700433 rcvmem <<= 2;
434
Eric Dumazete9266a02011-10-20 16:53:56 -0400435 if (sk->sk_rcvbuf < rcvmem)
Eric Dumazet356d1832017-11-07 00:29:28 -0800436 sk->sk_rcvbuf = min(rcvmem, sock_net(sk)->ipv4.sysctl_tcp_rmem[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800439/* 4. Try to fixup all. It is made immediately after connection enters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 * established state.
441 */
Jerry Chu10467162012-08-31 12:29:11 +0000442void tcp_init_buffer_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Eric Dumazet0c126542017-10-26 21:55:08 -0700444 int tcp_app_win = sock_net(sk)->ipv4.sysctl_tcp_app_win;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct tcp_sock *tp = tcp_sk(sk);
446 int maxwin;
447
448 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
449 tcp_fixup_rcvbuf(sk);
450 if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
Eric Dumazet6ae70532013-10-01 10:23:44 -0700451 tcp_sndbuf_expand(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 tp->rcvq_space.space = tp->rcv_wnd;
Eric Dumazet9a568de2017-05-16 14:00:14 -0700454 tcp_mstamp_refresh(tp);
Eric Dumazet645f4c62017-04-25 10:15:41 -0700455 tp->rcvq_space.time = tp->tcp_mstamp;
Eric Dumazetb0983d32013-09-20 13:56:58 -0700456 tp->rcvq_space.seq = tp->copied_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 maxwin = tcp_full_space(sk);
459
460 if (tp->window_clamp >= maxwin) {
461 tp->window_clamp = maxwin;
462
Eric Dumazet0c126542017-10-26 21:55:08 -0700463 if (tcp_app_win && maxwin > 4 * tp->advmss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 tp->window_clamp = max(maxwin -
Eric Dumazet0c126542017-10-26 21:55:08 -0700465 (maxwin >> tcp_app_win),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 4 * tp->advmss);
467 }
468
469 /* Force reservation of one segment. */
Eric Dumazet0c126542017-10-26 21:55:08 -0700470 if (tcp_app_win &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 tp->window_clamp > 2 * tp->advmss &&
472 tp->window_clamp + tp->advmss > maxwin)
473 tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss);
474
475 tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
Eric Dumazetc2203cf2017-05-16 14:00:04 -0700476 tp->snd_cwnd_stamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/* 5. Recalculate window clamp after socket hit its memory bounds. */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700480static void tcp_clamp_window(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700482 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300483 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet356d1832017-11-07 00:29:28 -0800484 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300486 icsk->icsk_ack.quick = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Eric Dumazet356d1832017-11-07 00:29:28 -0800488 if (sk->sk_rcvbuf < net->ipv4.sysctl_tcp_rmem[2] &&
John Heffner326f36e2005-11-10 17:11:48 -0800489 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
Eric Dumazetb8da51e2015-05-15 12:39:27 -0700490 !tcp_under_memory_pressure(sk) &&
Glauber Costa180d8cd2011-12-11 21:47:02 +0000491 sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)) {
John Heffner326f36e2005-11-10 17:11:48 -0800492 sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc),
Eric Dumazet356d1832017-11-07 00:29:28 -0800493 net->ipv4.sysctl_tcp_rmem[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
John Heffner326f36e2005-11-10 17:11:48 -0800495 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800496 tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800499/* Initialize RCV_MSS value.
500 * RCV_MSS is an our guess about MSS used by the peer.
501 * We haven't any direct information about the MSS.
502 * It's better to underestimate the RCV_MSS rather than overestimate.
503 * Overestimations make us ACKing less frequently than needed.
504 * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
505 */
506void tcp_initialize_rcv_mss(struct sock *sk)
507{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400508 const struct tcp_sock *tp = tcp_sk(sk);
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800509 unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
510
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800511 hint = min(hint, tp->rcv_wnd / 2);
William Allen Simpsonbee7ca92009-11-10 09:51:18 +0000512 hint = min(hint, TCP_MSS_DEFAULT);
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800513 hint = max(hint, TCP_MIN_MSS);
514
515 inet_csk(sk)->icsk_ack.rcv_mss = hint;
516}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000517EXPORT_SYMBOL(tcp_initialize_rcv_mss);
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/* Receiver "autotuning" code.
520 *
521 * The algorithm for RTT estimation w/o timestamps is based on
522 * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL.
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200523 * <http://public.lanl.gov/radiant/pubs.html#DRS>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 *
525 * More detail on this code can be found at
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200526 * <http://staff.psc.edu/jheffner/>,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * though this reference is out of date. A new paper
528 * is pending.
529 */
530static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
531{
Eric Dumazet645f4c62017-04-25 10:15:41 -0700532 u32 new_sample = tp->rcv_rtt_est.rtt_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 long m = sample;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (new_sample != 0) {
536 /* If we sample in larger samples in the non-timestamp
537 * case, we could grossly overestimate the RTT especially
538 * with chatty applications or bulk transfer apps which
539 * are stalled on filesystem I/O.
540 *
541 * Also, since we are only going for a minimum in the
Stephen Hemminger31f34262005-11-15 15:17:10 -0800542 * non-timestamp case, we do not smooth things out
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800543 * else with timestamps disabled convergence takes too
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 * long.
545 */
546 if (!win_dep) {
547 m -= (new_sample >> 3);
548 new_sample += m;
Neal Cardwell18a223e2012-04-10 07:59:20 +0000549 } else {
550 m <<= 3;
551 if (m < new_sample)
552 new_sample = m;
553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 } else {
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800555 /* No previous measure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 new_sample = m << 3;
557 }
558
Eric Dumazet645f4c62017-04-25 10:15:41 -0700559 tp->rcv_rtt_est.rtt_us = new_sample;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
563{
Eric Dumazet645f4c62017-04-25 10:15:41 -0700564 u32 delta_us;
565
Eric Dumazet9a568de2017-05-16 14:00:14 -0700566 if (tp->rcv_rtt_est.time == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto new_measure;
568 if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq))
569 return;
Eric Dumazet9a568de2017-05-16 14:00:14 -0700570 delta_us = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcv_rtt_est.time);
Wei Wang9ee11bd2017-12-12 16:28:58 -0800571 if (!delta_us)
572 delta_us = 1;
Eric Dumazet645f4c62017-04-25 10:15:41 -0700573 tcp_rcv_rtt_update(tp, delta_us, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575new_measure:
576 tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd;
Eric Dumazet645f4c62017-04-25 10:15:41 -0700577 tp->rcv_rtt_est.time = tp->tcp_mstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800580static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
581 const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700583 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet9a568de2017-05-16 14:00:14 -0700584
Wei Wang3f6c65d2018-06-19 21:42:50 -0700585 if (tp->rx_opt.rcv_tsecr == tp->rcv_rtt_last_tsecr)
586 return;
587 tp->rcv_rtt_last_tsecr = tp->rx_opt.rcv_tsecr;
588
589 if (TCP_SKB_CB(skb)->end_seq -
590 TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss) {
Eric Dumazet9a568de2017-05-16 14:00:14 -0700591 u32 delta = tcp_time_stamp(tp) - tp->rx_opt.rcv_tsecr;
Wei Wang9ee11bd2017-12-12 16:28:58 -0800592 u32 delta_us;
Eric Dumazet9a568de2017-05-16 14:00:14 -0700593
Wei Wang9ee11bd2017-12-12 16:28:58 -0800594 if (!delta)
595 delta = 1;
596 delta_us = delta * (USEC_PER_SEC / TCP_TS_HZ);
Eric Dumazet9a568de2017-05-16 14:00:14 -0700597 tcp_rcv_rtt_update(tp, delta_us, 0);
598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601/*
602 * This function should be called every time data is copied to user space.
603 * It calculates the appropriate TCP receive buffer space.
604 */
605void tcp_rcv_space_adjust(struct sock *sk)
606{
607 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet607065b2017-12-10 17:55:03 -0800608 u32 copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 int time;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900610
Yafang Shao61638492018-04-20 23:18:26 +0800611 trace_tcp_rcv_space_adjust(sk);
612
Eric Dumazet86323852017-12-06 11:08:19 -0800613 tcp_mstamp_refresh(tp);
Eric Dumazet9a568de2017-05-16 14:00:14 -0700614 time = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcvq_space.time);
Eric Dumazet645f4c62017-04-25 10:15:41 -0700615 if (time < (tp->rcv_rtt_est.rtt_us >> 3) || tp->rcv_rtt_est.rtt_us == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900617
Eric Dumazetb0983d32013-09-20 13:56:58 -0700618 /* Number of bytes copied to user in last RTT */
619 copied = tp->copied_seq - tp->rcvq_space.seq;
620 if (copied <= tp->rcvq_space.space)
621 goto new_measure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Eric Dumazetb0983d32013-09-20 13:56:58 -0700623 /* A bit of theory :
624 * copied = bytes received in previous RTT, our base window
625 * To cope with packet losses, we need a 2x factor
626 * To cope with slow start, and sender growing its cwin by 100 %
627 * every RTT, we need a 4x factor, because the ACK we are sending
628 * now is for the next RTT, not the current one :
629 * <prev RTT . ><current RTT .. ><next RTT .... >
630 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Eric Dumazet4540c0c2017-10-27 07:47:22 -0700632 if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf &&
Eric Dumazetb0983d32013-09-20 13:56:58 -0700633 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
Eric Dumazet607065b2017-12-10 17:55:03 -0800634 int rcvmem, rcvbuf;
Eric Dumazetc3916ad2017-12-10 17:55:04 -0800635 u64 rcvwin, grow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Eric Dumazetb0983d32013-09-20 13:56:58 -0700637 /* minimal window to cope with packet losses, assuming
638 * steady state. Add some cushion because of small variations.
639 */
Eric Dumazet607065b2017-12-10 17:55:03 -0800640 rcvwin = ((u64)copied << 1) + 16 * tp->advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Eric Dumazetc3916ad2017-12-10 17:55:04 -0800642 /* Accommodate for sender rate increase (eg. slow start) */
643 grow = rcvwin * (copied - tp->rcvq_space.space);
644 do_div(grow, tp->rcvq_space.space);
645 rcvwin += (grow << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Eric Dumazetb0983d32013-09-20 13:56:58 -0700647 rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER);
Eric Dumazet94f08932017-10-26 21:55:09 -0700648 while (tcp_win_from_space(sk, rcvmem) < tp->advmss)
Eric Dumazetb0983d32013-09-20 13:56:58 -0700649 rcvmem += 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Eric Dumazet607065b2017-12-10 17:55:03 -0800651 do_div(rcvwin, tp->advmss);
652 rcvbuf = min_t(u64, rcvwin * rcvmem,
653 sock_net(sk)->ipv4.sysctl_tcp_rmem[2]);
Eric Dumazetb0983d32013-09-20 13:56:58 -0700654 if (rcvbuf > sk->sk_rcvbuf) {
655 sk->sk_rcvbuf = rcvbuf;
656
657 /* Make the window clamp follow along. */
Eric Dumazet02db5572017-12-10 17:55:02 -0800658 tp->window_clamp = tcp_win_from_space(sk, rcvbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660 }
Eric Dumazetb0983d32013-09-20 13:56:58 -0700661 tp->rcvq_space.space = copied;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663new_measure:
664 tp->rcvq_space.seq = tp->copied_seq;
Eric Dumazet645f4c62017-04-25 10:15:41 -0700665 tp->rcvq_space.time = tp->tcp_mstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668/* There is something which you must keep in mind when you analyze the
669 * behavior of the tp->ato delayed ack timeout interval. When a
670 * connection starts up, we want to ack as quickly as possible. The
671 * problem is that "good" TCP's do slow start at the beginning of data
672 * transmission. The means that until we send the first few ACK's the
673 * sender will sit on his end and only queue most of his data, because
674 * he can only send snd_cwnd unacked packets at any given time. For
675 * each ACK we send, he increments snd_cwnd and transmits more of his
676 * queue. -DaveM
677 */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700678static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700680 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700681 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 u32 now;
683
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700684 inet_csk_schedule_ack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700686 tcp_measure_rcv_mss(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 tcp_rcv_rtt_measure(tp);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900689
Eric Dumazet70eabf02017-05-16 14:00:07 -0700690 now = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700692 if (!icsk->icsk_ack.ato) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* The _first_ data packet received, initialize
694 * delayed ACK engine.
695 */
Eric Dumazet9a9c9b52018-05-21 15:08:56 -0700696 tcp_incr_quickack(sk, TCP_MAX_QUICKACKS);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700697 icsk->icsk_ack.ato = TCP_ATO_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 } else {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700699 int m = now - icsk->icsk_ack.lrcvtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800701 if (m <= TCP_ATO_MIN / 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* The fastest case is the first. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700703 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2;
704 } else if (m < icsk->icsk_ack.ato) {
705 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m;
706 if (icsk->icsk_ack.ato > icsk->icsk_rto)
707 icsk->icsk_ack.ato = icsk->icsk_rto;
708 } else if (m > icsk->icsk_rto) {
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800709 /* Too long gap. Apparently sender failed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 * restart window, so that we send ACKs quickly.
711 */
Eric Dumazet9a9c9b52018-05-21 15:08:56 -0700712 tcp_incr_quickack(sk, TCP_MAX_QUICKACKS);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800713 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700716 icsk->icsk_ack.lrcvtime = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Yousuk Seungf4c9f852018-06-04 15:29:51 -0700718 tcp_ecn_check_ce(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 if (skb->len >= 128)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700721 tcp_grow_window(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724/* Called to compute a smoothed rtt estimate. The data fed to this
725 * routine either comes from timestamps, or from segments that were
726 * known _not_ to have been retransmitted [see Karn/Partridge
727 * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88
728 * piece by Van Jacobson.
729 * NOTE: the next three routines used to be one big routine.
730 * To save cycles in the RFC 1323 implementation it was better to break
731 * it up into three procedures. -- erics
732 */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800733static void tcp_rtt_estimator(struct sock *sk, long mrtt_us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300735 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet740b0f12014-02-26 14:02:48 -0800736 long m = mrtt_us; /* RTT */
737 u32 srtt = tp->srtt_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* The following amusing code comes from Jacobson's
740 * article in SIGCOMM '88. Note that rtt and mdev
741 * are scaled versions of rtt and mean deviation.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900742 * This is designed to be as fast as possible
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 * m stands for "measurement".
744 *
745 * On a 1990 paper the rto value is changed to:
746 * RTO = rtt + 4 * mdev
747 *
748 * Funny. This algorithm seems to be very broken.
749 * These formulae increase RTO, when it should be decreased, increase
Stephen Hemminger31f34262005-11-15 15:17:10 -0800750 * too slowly, when it should be increased quickly, decrease too quickly
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * etc. I guess in BSD RTO takes ONE value, so that it is absolutely
752 * does not matter how to _calculate_ it. Seems, it was trap
753 * that VJ failed to avoid. 8)
754 */
Eric Dumazet4a5ab4e2014-02-06 15:57:10 -0800755 if (srtt != 0) {
756 m -= (srtt >> 3); /* m is now error in rtt est */
757 srtt += m; /* rtt = 7/8 rtt + 1/8 new */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (m < 0) {
759 m = -m; /* m is now abs(error) */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800760 m -= (tp->mdev_us >> 2); /* similar update on mdev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* This is similar to one of Eifel findings.
762 * Eifel blocks mdev updates when rtt decreases.
763 * This solution is a bit different: we use finer gain
764 * for mdev in this case (alpha*beta).
765 * Like Eifel it also prevents growth of rto,
766 * but also it limits too fast rto decreases,
767 * happening in pure Eifel.
768 */
769 if (m > 0)
770 m >>= 3;
771 } else {
Eric Dumazet740b0f12014-02-26 14:02:48 -0800772 m -= (tp->mdev_us >> 2); /* similar update on mdev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Eric Dumazet740b0f12014-02-26 14:02:48 -0800774 tp->mdev_us += m; /* mdev = 3/4 mdev + 1/4 new */
775 if (tp->mdev_us > tp->mdev_max_us) {
776 tp->mdev_max_us = tp->mdev_us;
777 if (tp->mdev_max_us > tp->rttvar_us)
778 tp->rttvar_us = tp->mdev_max_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 if (after(tp->snd_una, tp->rtt_seq)) {
Eric Dumazet740b0f12014-02-26 14:02:48 -0800781 if (tp->mdev_max_us < tp->rttvar_us)
782 tp->rttvar_us -= (tp->rttvar_us - tp->mdev_max_us) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 tp->rtt_seq = tp->snd_nxt;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800784 tp->mdev_max_us = tcp_rto_min_us(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786 } else {
787 /* no previous measure. */
Eric Dumazet4a5ab4e2014-02-06 15:57:10 -0800788 srtt = m << 3; /* take the measured time to be rtt */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800789 tp->mdev_us = m << 1; /* make sure rto = 3*rtt */
790 tp->rttvar_us = max(tp->mdev_us, tcp_rto_min_us(sk));
791 tp->mdev_max_us = tp->rttvar_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 tp->rtt_seq = tp->snd_nxt;
793 }
Eric Dumazet740b0f12014-02-26 14:02:48 -0800794 tp->srtt_us = max(1U, srtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700797static void tcp_update_pacing_rate(struct sock *sk)
798{
799 const struct tcp_sock *tp = tcp_sk(sk);
800 u64 rate;
801
802 /* set sk_pacing_rate to 200 % of current rate (mss * cwnd / srtt) */
Eric Dumazet43e122b2015-08-21 17:38:02 -0700803 rate = (u64)tp->mss_cache * ((USEC_PER_SEC / 100) << 3);
804
805 /* current rate is (cwnd * mss) / srtt
806 * In Slow Start [1], set sk_pacing_rate to 200 % the current rate.
807 * In Congestion Avoidance phase, set it to 120 % the current rate.
808 *
809 * [1] : Normal Slow Start condition is (tp->snd_cwnd < tp->snd_ssthresh)
810 * If snd_cwnd >= (tp->snd_ssthresh / 2), we are approaching
811 * end of slow start and should slow down.
812 */
813 if (tp->snd_cwnd < tp->snd_ssthresh / 2)
Eric Dumazet23a7102a2017-10-27 07:47:31 -0700814 rate *= sock_net(sk)->ipv4.sysctl_tcp_pacing_ss_ratio;
Eric Dumazet43e122b2015-08-21 17:38:02 -0700815 else
Eric Dumazetc26e91f2017-10-27 07:47:32 -0700816 rate *= sock_net(sk)->ipv4.sysctl_tcp_pacing_ca_ratio;
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700817
818 rate *= max(tp->snd_cwnd, tp->packets_out);
819
Eric Dumazet740b0f12014-02-26 14:02:48 -0800820 if (likely(tp->srtt_us))
821 do_div(rate, tp->srtt_us);
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700822
Mark Rutlanda9da6f22017-10-23 14:07:18 -0700823 /* WRITE_ONCE() is needed because sch_fq fetches sk_pacing_rate
Eric Dumazetba537422013-10-09 17:14:52 -0700824 * without any lock. We want to make sure compiler wont store
825 * intermediate values in this location.
826 */
Mark Rutlanda9da6f22017-10-23 14:07:18 -0700827 WRITE_ONCE(sk->sk_pacing_rate, min_t(u64, rate,
828 sk->sk_max_pacing_rate));
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700829}
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831/* Calculate rto without backoff. This is the second half of Van Jacobson's
832 * routine referred to above.
833 */
stephen hemmingerf7e56a72013-12-29 11:39:51 -0800834static void tcp_set_rto(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700836 const struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* Old crap is replaced with new one. 8)
838 *
839 * More seriously:
840 * 1. If rtt variance happened to be less 50msec, it is hallucination.
841 * It cannot be less due to utterly erratic ACK generation made
842 * at least by solaris and freebsd. "Erratic ACKs" has _nothing_
843 * to do with delayed acks, because at cwnd>2 true delack timeout
844 * is invisible. Actually, Linux-2.4 also generates erratic
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800845 * ACKs in some circumstances.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 */
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000847 inet_csk(sk)->icsk_rto = __tcp_set_rto(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* 2. Fixups made earlier cannot be right.
850 * If we do not estimate RTO correctly without them,
851 * all the algo is pure shit and should be replaced
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800852 * with correct one. It is exactly, which we pretend to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Ilpo Järvinenee6aac52008-12-05 22:43:08 -0800855 /* NOTE: clamping at TCP_RTO_MIN is not required, current algo
856 * guarantees that rto is higher.
857 */
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000858 tcp_bound_rto(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400861__u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
864
Gerrit Renker22b71c82010-08-29 19:23:12 +0000865 if (!cwnd)
David S. Miller442b9632011-02-02 17:05:11 -0800866 cwnd = TCP_INIT_CWND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
868}
869
Ryousei Takano564262c2007-10-25 23:03:52 -0700870/* Take a notice that peer is sending D-SACKs */
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300871static void tcp_dsack_seen(struct tcp_sock *tp)
872{
Vijay Subramanianab562222011-12-20 13:23:24 +0000873 tp->rx_opt.sack_ok |= TCP_DSACK_SEEN;
Priyaranjan Jha1f255692017-11-03 16:38:48 -0700874 tp->rack.dsack_seen = 1;
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300875}
876
Yuchung Cheng737ff312017-11-08 13:01:27 -0800877/* It's reordering when higher sequence was delivered (i.e. sacked) before
878 * some lower never-retransmitted sequence ("low_seq"). The maximum reordering
879 * distance is approximated in full-mss packet distance ("reordering").
880 */
881static void tcp_check_sack_reordering(struct sock *sk, const u32 low_seq,
882 const int ts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300884 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng737ff312017-11-08 13:01:27 -0800885 const u32 mss = tp->mss_cache;
886 u32 fack, metric;
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700887
Yuchung Cheng737ff312017-11-08 13:01:27 -0800888 fack = tcp_highest_sack_seq(tp);
889 if (!before(low_seq, fack))
Soheil Hassas Yeganeh6f5b24e2017-05-16 17:39:02 -0400890 return;
891
Yuchung Cheng737ff312017-11-08 13:01:27 -0800892 metric = fack - low_seq;
893 if ((metric > tp->reordering * mss) && mss) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894#if FASTRETRANS_DEBUG > 1
Joe Perches91df42b2012-05-15 14:11:54 +0000895 pr_debug("Disorder%d %d %u f%u s%u rr%d\n",
896 tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state,
897 tp->reordering,
Yuchung Cheng737ff312017-11-08 13:01:27 -0800898 0,
Joe Perches91df42b2012-05-15 14:11:54 +0000899 tp->sacked_out,
900 tp->undo_marker ? tp->undo_retrans : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901#endif
Yuchung Cheng737ff312017-11-08 13:01:27 -0800902 tp->reordering = min_t(u32, (metric + mss - 1) / mss,
903 sock_net(sk)->ipv4.sysctl_tcp_max_reordering);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Yuchung Chengeed530b2012-05-02 13:30:03 +0000905
Yuchung Cheng4f41b1c2015-10-16 21:57:47 -0700906 tp->rack.reord = 1;
Yuchung Cheng2d2517e2017-04-04 14:15:40 -0700907 /* This exciting event is worth to be remembered. 8) */
Yuchung Cheng737ff312017-11-08 13:01:27 -0800908 NET_INC_STATS(sock_net(sk),
909 ts ? LINUX_MIB_TCPTSREORDER : LINUX_MIB_TCPSACKREORDER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Ilpo Järvinen006f5822008-09-20 21:20:20 -0700912/* This must be called before lost_out is incremented */
Ilpo Järvinenc8c213f2008-09-20 21:18:55 -0700913static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
914{
Ian Morris51456b22015-04-03 09:17:26 +0100915 if (!tp->retransmit_skb_hint ||
Ilpo Järvinenc8c213f2008-09-20 21:18:55 -0700916 before(TCP_SKB_CB(skb)->seq,
917 TCP_SKB_CB(tp->retransmit_skb_hint)->seq))
Ilpo Järvinen006f5822008-09-20 21:20:20 -0700918 tp->retransmit_skb_hint = skb;
Ilpo Järvinenc8c213f2008-09-20 21:18:55 -0700919}
920
Neal Cardwell0682e692016-09-19 23:39:13 -0400921/* Sum the number of packets on the wire we have marked as lost.
922 * There are two cases we care about here:
923 * a) Packet hasn't been marked lost (nor retransmitted),
924 * and this is the first loss.
925 * b) Packet has been marked both lost and retransmitted,
926 * and this means we think it was lost again.
927 */
928static void tcp_sum_lost(struct tcp_sock *tp, struct sk_buff *skb)
929{
930 __u8 sacked = TCP_SKB_CB(skb)->sacked;
931
932 if (!(sacked & TCPCB_LOST) ||
933 ((sacked & TCPCB_LOST) && (sacked & TCPCB_SACKED_RETRANS)))
934 tp->lost += tcp_skb_pcount(skb);
935}
936
Ilpo Järvinen41ea36e2008-09-20 21:19:22 -0700937static void tcp_skb_mark_lost(struct tcp_sock *tp, struct sk_buff *skb)
938{
939 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
940 tcp_verify_retransmit_hint(tp, skb);
941
942 tp->lost_out += tcp_skb_pcount(skb);
Neal Cardwell0682e692016-09-19 23:39:13 -0400943 tcp_sum_lost(tp, skb);
Ilpo Järvinen41ea36e2008-09-20 21:19:22 -0700944 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
945 }
946}
947
Yuchung Cheng4f41b1c2015-10-16 21:57:47 -0700948void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp, struct sk_buff *skb)
Ilpo Järvinen006f5822008-09-20 21:20:20 -0700949{
950 tcp_verify_retransmit_hint(tp, skb);
951
Neal Cardwell0682e692016-09-19 23:39:13 -0400952 tcp_sum_lost(tp, skb);
Ilpo Järvinen006f5822008-09-20 21:20:20 -0700953 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
954 tp->lost_out += tcp_skb_pcount(skb);
955 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
956 }
957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959/* This procedure tags the retransmission queue when SACKs arrive.
960 *
961 * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L).
962 * Packets in queue with these bits set are counted in variables
963 * sacked_out, retrans_out and lost_out, correspondingly.
964 *
965 * Valid combinations are:
966 * Tag InFlight Description
967 * 0 1 - orig segment is in flight.
968 * S 0 - nothing flies, orig reached receiver.
969 * L 0 - nothing flies, orig lost by net.
970 * R 2 - both orig and retransmit are in flight.
971 * L|R 1 - orig is lost, retransmit is in flight.
972 * S|R 1 - orig reached receiver, retrans is still in flight.
973 * (L|S|R is logically valid, it could occur when L|R is sacked,
974 * but it is equivalent to plain S and code short-curcuits it to S.
975 * L|S is logically invalid, it would mean -1 packet in flight 8))
976 *
977 * These 6 states form finite state machine, controlled by the following events:
978 * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())
979 * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())
Yuchung Cheng974c1232012-01-19 14:42:21 +0000980 * 3. Loss detection event of two flavors:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * A. Scoreboard estimator decided the packet is lost.
982 * A'. Reno "three dupacks" marks head of queue lost.
Yuchung Cheng974c1232012-01-19 14:42:21 +0000983 * B. SACK arrives sacking SND.NXT at the moment, when the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * segment was retransmitted.
985 * 4. D-SACK added new rule: D-SACK changes any tag to S.
986 *
987 * It is pleasant to note, that state diagram turns out to be commutative,
988 * so that we are allowed not to be bothered by order of our actions,
989 * when multiple events arrive simultaneously. (see the function below).
990 *
991 * Reordering detection.
992 * --------------------
993 * Reordering metric is maximal distance, which a packet can be displaced
994 * in packet stream. With SACKs we can estimate it:
995 *
996 * 1. SACK fills old hole and the corresponding segment was not
997 * ever retransmitted -> reordering. Alas, we cannot use it
998 * when segment was retransmitted.
999 * 2. The last flaw is solved with D-SACK. D-SACK arrives
1000 * for retransmitted and already SACKed segment -> reordering..
1001 * Both of these heuristics are not used in Loss state, when we cannot
1002 * account for retransmits accurately.
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001003 *
1004 * SACK block validation.
1005 * ----------------------
1006 *
1007 * SACK block range validation checks that the received SACK block fits to
1008 * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT.
1009 * Note that SND.UNA is not included to the range though being valid because
Ilpo Järvinen0e835332007-10-01 15:28:17 -07001010 * it means that the receiver is rather inconsistent with itself reporting
1011 * SACK reneging when it should advance SND.UNA. Such SACK block this is
1012 * perfectly valid, however, in light of RFC2018 which explicitly states
1013 * that "SACK block MUST reflect the newest segment. Even if the newest
1014 * segment is going to be discarded ...", not that it looks very clever
1015 * in case of head skb. Due to potentional receiver driven attacks, we
1016 * choose to avoid immediate execution of a walk in write queue due to
1017 * reneging and defer head skb's loss recovery to standard loss recovery
1018 * procedure that will eventually trigger (nothing forbids us doing this).
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001019 *
1020 * Implements also blockage to start_seq wrap-around. Problem lies in the
1021 * fact that though start_seq (s) is before end_seq (i.e., not reversed),
1022 * there's no guarantee that it will be before snd_nxt (n). The problem
1023 * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt
1024 * wrap (s_w):
1025 *
1026 * <- outs wnd -> <- wrapzone ->
1027 * u e n u_w e_w s n_w
1028 * | | | | | | |
1029 * |<------------+------+----- TCP seqno space --------------+---------->|
1030 * ...-- <2^31 ->| |<--------...
1031 * ...---- >2^31 ------>| |<--------...
1032 *
1033 * Current code wouldn't be vulnerable but it's better still to discard such
1034 * crazy SACK blocks. Doing this check for start_seq alone closes somewhat
1035 * similar case (end_seq after snd_nxt wrap) as earlier reversed check in
1036 * snd_nxt wrap -> snd_una region will then become "well defined", i.e.,
1037 * equal to the ideal case (infinite seqno space without wrap caused issues).
1038 *
1039 * With D-SACK the lower bound is extended to cover sequence space below
1040 * SND.UNA down to undo_marker, which is the last point of interest. Yet
Ryousei Takano564262c2007-10-25 23:03:52 -07001041 * again, D-SACK block must not to go across snd_una (for the same reason as
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001042 * for the normal SACK blocks, explained above). But there all simplicity
1043 * ends, TCP might receive valid D-SACKs below that. As long as they reside
1044 * fully below undo_marker they do not affect behavior in anyway and can
1045 * therefore be safely ignored. In rare cases (which are more or less
1046 * theoretical ones), the D-SACK will nicely cross that boundary due to skb
1047 * fragmentation and packet reordering past skb's retransmission. To consider
1048 * them correctly, the acceptable range must be extended even more though
1049 * the exact amount is rather hard to quantify. However, tp->max_window can
1050 * be used as an exaggerated estimate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001052static bool tcp_is_sackblock_valid(struct tcp_sock *tp, bool is_dsack,
1053 u32 start_seq, u32 end_seq)
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001054{
1055 /* Too far in future, or reversed (interpretation is ambiguous) */
1056 if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001057 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001058
1059 /* Nasty start_seq wrap-around check (see comments above) */
1060 if (!before(start_seq, tp->snd_nxt))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001061 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001062
Ryousei Takano564262c2007-10-25 23:03:52 -07001063 /* In outstanding window? ...This is valid exit for D-SACKs too.
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001064 * start_seq == snd_una is non-sensical (see comments above)
1065 */
1066 if (after(start_seq, tp->snd_una))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001067 return true;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001068
1069 if (!is_dsack || !tp->undo_marker)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001070 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001071
1072 /* ...Then it's D-SACK, and must reside below snd_una completely */
Zheng Yanf779b2d2011-09-18 22:37:34 -04001073 if (after(end_seq, tp->snd_una))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001074 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001075
1076 if (!before(start_seq, tp->undo_marker))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001077 return true;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001078
1079 /* Too old */
1080 if (!after(end_seq, tp->undo_marker))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001081 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001082
1083 /* Undo_marker boundary crossing (overestimates a lot). Known already:
1084 * start_seq < undo_marker and end_seq >= undo_marker.
1085 */
1086 return !before(start_seq, end_seq - tp->max_window);
1087}
1088
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001089static bool tcp_check_dsack(struct sock *sk, const struct sk_buff *ack_skb,
1090 struct tcp_sack_block_wire *sp, int num_sacks,
1091 u32 prior_snd_una)
David S. Millerd06e0212007-06-18 22:43:06 -07001092{
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07001093 struct tcp_sock *tp = tcp_sk(sk);
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001094 u32 start_seq_0 = get_unaligned_be32(&sp[0].start_seq);
1095 u32 end_seq_0 = get_unaligned_be32(&sp[0].end_seq);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001096 bool dup_sack = false;
David S. Millerd06e0212007-06-18 22:43:06 -07001097
1098 if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) {
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001099 dup_sack = true;
Ilpo Järvinene60402d2007-08-09 15:14:46 +03001100 tcp_dsack_seen(tp);
Eric Dumazetc10d9312016-04-29 14:16:47 -07001101 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKRECV);
David S. Millerd06e0212007-06-18 22:43:06 -07001102 } else if (num_sacks > 1) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001103 u32 end_seq_1 = get_unaligned_be32(&sp[1].end_seq);
1104 u32 start_seq_1 = get_unaligned_be32(&sp[1].start_seq);
David S. Millerd06e0212007-06-18 22:43:06 -07001105
1106 if (!after(end_seq_0, end_seq_1) &&
1107 !before(start_seq_0, start_seq_1)) {
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001108 dup_sack = true;
Ilpo Järvinene60402d2007-08-09 15:14:46 +03001109 tcp_dsack_seen(tp);
Eric Dumazetc10d9312016-04-29 14:16:47 -07001110 NET_INC_STATS(sock_net(sk),
Pavel Emelyanovde0744a2008-07-16 20:31:16 -07001111 LINUX_MIB_TCPDSACKOFORECV);
David S. Millerd06e0212007-06-18 22:43:06 -07001112 }
1113 }
1114
1115 /* D-SACK for already forgotten data... Do dumb counting. */
Yuchung Cheng6e08d5e2014-07-02 12:07:16 -07001116 if (dup_sack && tp->undo_marker && tp->undo_retrans > 0 &&
David S. Millerd06e0212007-06-18 22:43:06 -07001117 !after(end_seq_0, prior_snd_una) &&
1118 after(end_seq_0, tp->undo_marker))
1119 tp->undo_retrans--;
1120
1121 return dup_sack;
1122}
1123
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001124struct tcp_sacktag_state {
Yuchung Cheng737ff312017-11-08 13:01:27 -08001125 u32 reord;
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02001126 /* Timestamps for earliest and latest never-retransmitted segment
1127 * that was SACKed. RTO needs the earliest RTT to stay conservative,
1128 * but congestion control should still get an accurate delay signal.
1129 */
Eric Dumazet9a568de2017-05-16 14:00:14 -07001130 u64 first_sackt;
1131 u64 last_sackt;
Yuchung Chengb9f64822016-09-19 23:39:14 -04001132 struct rate_sample *rate;
Eric Dumazet740b0f12014-02-26 14:02:48 -08001133 int flag;
Eric Dumazet75c119a2017-10-05 22:21:27 -07001134 unsigned int mss_now;
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001135};
1136
Ilpo Järvinend1935942007-10-11 17:34:25 -07001137/* Check if skb is fully within the SACK block. In presence of GSO skbs,
1138 * the incoming SACK may not exactly match but we can find smaller MSS
1139 * aligned portion of it that matches. Therefore we might need to fragment
1140 * which may fail and creates some hassle (caller must handle error case
1141 * returns).
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001142 *
1143 * FIXME: this could be merged to shift decision code
Ilpo Järvinend1935942007-10-11 17:34:25 -07001144 */
Adrian Bunk0f79efd2007-10-26 03:57:36 -07001145static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001146 u32 start_seq, u32 end_seq)
Ilpo Järvinend1935942007-10-11 17:34:25 -07001147{
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001148 int err;
1149 bool in_sack;
Ilpo Järvinend1935942007-10-11 17:34:25 -07001150 unsigned int pkt_len;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001151 unsigned int mss;
Ilpo Järvinend1935942007-10-11 17:34:25 -07001152
1153 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1154 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1155
1156 if (tcp_skb_pcount(skb) > 1 && !in_sack &&
1157 after(TCP_SKB_CB(skb)->end_seq, start_seq)) {
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001158 mss = tcp_skb_mss(skb);
Ilpo Järvinend1935942007-10-11 17:34:25 -07001159 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1160
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001161 if (!in_sack) {
Ilpo Järvinend1935942007-10-11 17:34:25 -07001162 pkt_len = start_seq - TCP_SKB_CB(skb)->seq;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001163 if (pkt_len < mss)
1164 pkt_len = mss;
1165 } else {
Ilpo Järvinend1935942007-10-11 17:34:25 -07001166 pkt_len = end_seq - TCP_SKB_CB(skb)->seq;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001167 if (pkt_len < mss)
1168 return -EINVAL;
1169 }
1170
1171 /* Round if necessary so that SACKs cover only full MSSes
1172 * and/or the remaining small portion (if present)
1173 */
1174 if (pkt_len > mss) {
1175 unsigned int new_len = (pkt_len / mss) * mss;
Yuchung Chengb451e5d2017-05-10 17:01:27 -07001176 if (!in_sack && new_len < pkt_len)
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001177 new_len += mss;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001178 pkt_len = new_len;
1179 }
Yuchung Chengb451e5d2017-05-10 17:01:27 -07001180
1181 if (pkt_len >= skb->len && !in_sack)
1182 return 0;
1183
Eric Dumazet75c119a2017-10-05 22:21:27 -07001184 err = tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb,
1185 pkt_len, mss, GFP_ATOMIC);
Ilpo Järvinend1935942007-10-11 17:34:25 -07001186 if (err < 0)
1187 return err;
1188 }
1189
1190 return in_sack;
1191}
1192
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001193/* Mark the given newly-SACKed range as such, adjusting counters and hints. */
1194static u8 tcp_sacktag_one(struct sock *sk,
1195 struct tcp_sacktag_state *state, u8 sacked,
1196 u32 start_seq, u32 end_seq,
Eric Dumazet740b0f12014-02-26 14:02:48 -08001197 int dup_sack, int pcount,
Eric Dumazet9a568de2017-05-16 14:00:14 -07001198 u64 xmit_time)
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001199{
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001200 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001201
1202 /* Account D-SACK for retransmitted packet. */
1203 if (dup_sack && (sacked & TCPCB_RETRANS)) {
Yuchung Cheng6e08d5e2014-07-02 12:07:16 -07001204 if (tp->undo_marker && tp->undo_retrans > 0 &&
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001205 after(end_seq, tp->undo_marker))
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001206 tp->undo_retrans--;
Yuchung Cheng737ff312017-11-08 13:01:27 -08001207 if ((sacked & TCPCB_SACKED_ACKED) &&
1208 before(start_seq, state->reord))
1209 state->reord = start_seq;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001210 }
1211
1212 /* Nothing to do; acked frame is about to be dropped (was ACKed). */
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001213 if (!after(end_seq, tp->snd_una))
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001214 return sacked;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001215
1216 if (!(sacked & TCPCB_SACKED_ACKED)) {
Eric Dumazetd2329f12017-04-25 10:15:38 -07001217 tcp_rack_advance(tp, sacked, end_seq, xmit_time);
Yuchung Cheng659a8ad2015-10-16 21:57:46 -07001218
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001219 if (sacked & TCPCB_SACKED_RETRANS) {
1220 /* If the segment is not tagged as lost,
1221 * we do not clear RETRANS, believing
1222 * that retransmission is still in flight.
1223 */
1224 if (sacked & TCPCB_LOST) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001225 sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS);
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001226 tp->lost_out -= pcount;
1227 tp->retrans_out -= pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001228 }
1229 } else {
1230 if (!(sacked & TCPCB_RETRANS)) {
1231 /* New sack for not retransmitted frame,
1232 * which was in hole. It is reordering.
1233 */
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001234 if (before(start_seq,
Yuchung Cheng737ff312017-11-08 13:01:27 -08001235 tcp_highest_sack_seq(tp)) &&
1236 before(start_seq, state->reord))
1237 state->reord = start_seq;
1238
Yuchung Chenge33099f2013-03-20 13:33:00 +00001239 if (!after(end_seq, tp->high_seq))
1240 state->flag |= FLAG_ORIG_SACK_ACKED;
Eric Dumazet9a568de2017-05-16 14:00:14 -07001241 if (state->first_sackt == 0)
1242 state->first_sackt = xmit_time;
1243 state->last_sackt = xmit_time;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001244 }
1245
1246 if (sacked & TCPCB_LOST) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001247 sacked &= ~TCPCB_LOST;
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001248 tp->lost_out -= pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001249 }
1250 }
1251
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001252 sacked |= TCPCB_SACKED_ACKED;
1253 state->flag |= FLAG_DATA_SACKED;
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001254 tp->sacked_out += pcount;
Yuchung Chengddf1af62016-02-02 10:33:06 -08001255 tp->delivered += pcount; /* Out-of-order packets delivered */
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001256
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001257 /* Lost marker hint past SACKed? Tweak RFC3517 cnt */
Yuchung Cheng713bafe2017-11-08 13:01:26 -08001258 if (tp->lost_skb_hint &&
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001259 before(start_seq, TCP_SKB_CB(tp->lost_skb_hint)->seq))
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001260 tp->lost_cnt_hint += pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001261 }
1262
1263 /* D-SACK. We can detect redundant retransmission in S|R and plain R
1264 * frames and clear it. undo_retrans is decreased above, L|R frames
1265 * are accounted above as well.
1266 */
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001267 if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) {
1268 sacked &= ~TCPCB_SACKED_RETRANS;
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001269 tp->retrans_out -= pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001270 }
1271
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001272 return sacked;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001273}
1274
Neal Cardwelldaef52b2012-02-12 18:37:10 +00001275/* Shift newly-SACKed bytes from this skb to the immediately previous
1276 * already-SACKed sk_buff. Mark the newly-SACKed bytes as such.
1277 */
Eric Dumazetf3319812017-10-05 22:21:26 -07001278static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *prev,
1279 struct sk_buff *skb,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001280 struct tcp_sacktag_state *state,
1281 unsigned int pcount, int shifted, int mss,
1282 bool dup_sack)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001283{
1284 struct tcp_sock *tp = tcp_sk(sk);
Neal Cardwelldaef52b2012-02-12 18:37:10 +00001285 u32 start_seq = TCP_SKB_CB(skb)->seq; /* start of newly-SACKed */
1286 u32 end_seq = start_seq + shifted; /* end of newly-SACKed */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001287
1288 BUG_ON(!pcount);
1289
Neal Cardwell4c90d3b2012-02-26 10:06:19 +00001290 /* Adjust counters and hints for the newly sacked sequence
1291 * range but discard the return value since prev is already
1292 * marked. We must tag the range first because the seq
1293 * advancement below implicitly advances
1294 * tcp_highest_sack_seq() when skb is highest_sack.
1295 */
1296 tcp_sacktag_one(sk, state, TCP_SKB_CB(skb)->sacked,
Yuchung Cheng59c9af42013-07-22 16:20:47 -07001297 start_seq, end_seq, dup_sack, pcount,
Eric Dumazet9a568de2017-05-16 14:00:14 -07001298 skb->skb_mstamp);
Yuchung Chengb9f64822016-09-19 23:39:14 -04001299 tcp_rate_skb_delivered(sk, skb, state->rate);
Neal Cardwell4c90d3b2012-02-26 10:06:19 +00001300
1301 if (skb == tp->lost_skb_hint)
Neal Cardwell0af2a0d2012-02-13 20:22:08 +00001302 tp->lost_cnt_hint += pcount;
1303
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001304 TCP_SKB_CB(prev)->end_seq += shifted;
1305 TCP_SKB_CB(skb)->seq += shifted;
1306
Eric Dumazetcd7d8492014-09-24 04:11:22 -07001307 tcp_skb_pcount_add(prev, pcount);
1308 BUG_ON(tcp_skb_pcount(skb) < pcount);
1309 tcp_skb_pcount_add(skb, -pcount);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001310
1311 /* When we're adding to gso_segs == 1, gso_size will be zero,
1312 * in theory this shouldn't be necessary but as long as DSACK
1313 * code can come after this skb later on it's better to keep
1314 * setting gso_size to something.
1315 */
Eric Dumazetf69ad292015-06-11 09:15:18 -07001316 if (!TCP_SKB_CB(prev)->tcp_gso_size)
1317 TCP_SKB_CB(prev)->tcp_gso_size = mss;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001318
1319 /* CHECKME: To clear or not to clear? Mimics normal skb currently */
Eric Dumazet51466a72015-06-11 09:15:16 -07001320 if (tcp_skb_pcount(skb) <= 1)
Eric Dumazetf69ad292015-06-11 09:15:18 -07001321 TCP_SKB_CB(skb)->tcp_gso_size = 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001322
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001323 /* Difference in this won't matter, both ACKed by the same cumul. ACK */
1324 TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS);
1325
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001326 if (skb->len > 0) {
1327 BUG_ON(!tcp_skb_pcount(skb));
Eric Dumazetc10d9312016-04-29 14:16:47 -07001328 NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKSHIFTED);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001329 return false;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001330 }
1331
1332 /* Whole SKB was eaten :-) */
1333
Ilpo Järvinen92ee76b2008-11-24 21:26:56 -08001334 if (skb == tp->retransmit_skb_hint)
1335 tp->retransmit_skb_hint = prev;
Ilpo Järvinen92ee76b2008-11-24 21:26:56 -08001336 if (skb == tp->lost_skb_hint) {
1337 tp->lost_skb_hint = prev;
1338 tp->lost_cnt_hint -= tcp_skb_pcount(prev);
1339 }
1340
Eric Dumazet5e8a402f2013-10-04 10:31:41 -07001341 TCP_SKB_CB(prev)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags;
Martin KaFai Laua643b5d2016-04-25 14:44:49 -07001342 TCP_SKB_CB(prev)->eor = TCP_SKB_CB(skb)->eor;
Eric Dumazet5e8a402f2013-10-04 10:31:41 -07001343 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
1344 TCP_SKB_CB(prev)->end_seq++;
1345
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001346 if (skb == tcp_highest_sack(sk))
1347 tcp_advance_highest_sack(sk, skb);
1348
Martin KaFai Laucfea5a62016-04-19 22:39:29 -07001349 tcp_skb_collapse_tstamp(prev, skb);
Eric Dumazet9a568de2017-05-16 14:00:14 -07001350 if (unlikely(TCP_SKB_CB(prev)->tx.delivered_mstamp))
1351 TCP_SKB_CB(prev)->tx.delivered_mstamp = 0;
Yuchung Chengb9f64822016-09-19 23:39:14 -04001352
Eric Dumazet75c119a2017-10-05 22:21:27 -07001353 tcp_rtx_queue_unlink_and_free(skb, sk);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001354
Eric Dumazetc10d9312016-04-29 14:16:47 -07001355 NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKMERGED);
Ilpo Järvinen111cc8b2008-11-24 21:27:22 -08001356
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001357 return true;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001358}
1359
1360/* I wish gso_size would have a bit more sane initialization than
1361 * something-or-zero which complicates things
1362 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001363static int tcp_skb_seglen(const struct sk_buff *skb)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001364{
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001365 return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001366}
1367
1368/* Shifting pages past head area doesn't work */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001369static int skb_can_shift(const struct sk_buff *skb)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001370{
1371 return !skb_headlen(skb) && skb_is_nonlinear(skb);
1372}
1373
1374/* Try collapsing SACK blocks spanning across multiple skbs to a single
1375 * skb.
1376 */
1377static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001378 struct tcp_sacktag_state *state,
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001379 u32 start_seq, u32 end_seq,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001380 bool dup_sack)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001381{
1382 struct tcp_sock *tp = tcp_sk(sk);
1383 struct sk_buff *prev;
1384 int mss;
1385 int pcount = 0;
1386 int len;
1387 int in_sack;
1388
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001389 /* Normally R but no L won't result in plain S */
1390 if (!dup_sack &&
Ilpo Järvinen9969ca52008-12-05 22:41:06 -08001391 (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001392 goto fallback;
1393 if (!skb_can_shift(skb))
1394 goto fallback;
1395 /* This frame is about to be dropped (was ACKed). */
1396 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1397 goto fallback;
1398
1399 /* Can only happen with delayed DSACK + discard craziness */
Eric Dumazet75c119a2017-10-05 22:21:27 -07001400 prev = skb_rb_prev(skb);
1401 if (!prev)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001402 goto fallback;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001403
1404 if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED)
1405 goto fallback;
1406
Martin KaFai Laua643b5d2016-04-25 14:44:49 -07001407 if (!tcp_skb_can_collapse_to(prev))
1408 goto fallback;
1409
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001410 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1411 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1412
1413 if (in_sack) {
1414 len = skb->len;
1415 pcount = tcp_skb_pcount(skb);
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001416 mss = tcp_skb_seglen(skb);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001417
1418 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1419 * drop this restriction as unnecessary
1420 */
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001421 if (mss != tcp_skb_seglen(prev))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001422 goto fallback;
1423 } else {
1424 if (!after(TCP_SKB_CB(skb)->end_seq, start_seq))
1425 goto noop;
1426 /* CHECKME: This is non-MSS split case only?, this will
1427 * cause skipped skbs due to advancing loop btw, original
1428 * has that feature too
1429 */
1430 if (tcp_skb_pcount(skb) <= 1)
1431 goto noop;
1432
1433 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1434 if (!in_sack) {
1435 /* TODO: head merge to next could be attempted here
1436 * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)),
1437 * though it might not be worth of the additional hassle
1438 *
1439 * ...we can probably just fallback to what was done
1440 * previously. We could try merging non-SACKed ones
1441 * as well but it probably isn't going to buy off
1442 * because later SACKs might again split them, and
1443 * it would make skb timestamp tracking considerably
1444 * harder problem.
1445 */
1446 goto fallback;
1447 }
1448
1449 len = end_seq - TCP_SKB_CB(skb)->seq;
1450 BUG_ON(len < 0);
1451 BUG_ON(len > skb->len);
1452
1453 /* MSS boundaries should be honoured or else pcount will
1454 * severely break even though it makes things bit trickier.
1455 * Optimize common case to avoid most of the divides
1456 */
1457 mss = tcp_skb_mss(skb);
1458
1459 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1460 * drop this restriction as unnecessary
1461 */
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001462 if (mss != tcp_skb_seglen(prev))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001463 goto fallback;
1464
1465 if (len == mss) {
1466 pcount = 1;
1467 } else if (len < mss) {
1468 goto noop;
1469 } else {
1470 pcount = len / mss;
1471 len = pcount * mss;
1472 }
1473 }
1474
Neal Cardwell4648dc92012-03-05 19:35:04 +00001475 /* tcp_sacktag_one() won't SACK-tag ranges below snd_una */
1476 if (!after(TCP_SKB_CB(skb)->seq + len, tp->snd_una))
1477 goto fallback;
1478
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001479 if (!skb_shift(prev, skb, len))
1480 goto fallback;
Eric Dumazetf3319812017-10-05 22:21:26 -07001481 if (!tcp_shifted_skb(sk, prev, skb, state, pcount, len, mss, dup_sack))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001482 goto out;
1483
1484 /* Hole filled allows collapsing with the next as well, this is very
1485 * useful when hole on every nth skb pattern happens
1486 */
Eric Dumazet75c119a2017-10-05 22:21:27 -07001487 skb = skb_rb_next(prev);
1488 if (!skb)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001489 goto out;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001490
Ilpo Järvinenf0bc52f2008-12-05 22:40:47 -08001491 if (!skb_can_shift(skb) ||
Ilpo Järvinenf0bc52f2008-12-05 22:40:47 -08001492 ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) ||
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001493 (mss != tcp_skb_seglen(skb)))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001494 goto out;
1495
1496 len = skb->len;
1497 if (skb_shift(prev, skb, len)) {
1498 pcount += tcp_skb_pcount(skb);
Eric Dumazetf3319812017-10-05 22:21:26 -07001499 tcp_shifted_skb(sk, prev, skb, state, tcp_skb_pcount(skb),
1500 len, mss, 0);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001501 }
1502
1503out:
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001504 return prev;
1505
1506noop:
1507 return skb;
1508
1509fallback:
Eric Dumazetc10d9312016-04-29 14:16:47 -07001510 NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001511 return NULL;
1512}
1513
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001514static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
1515 struct tcp_sack_block *next_dup,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001516 struct tcp_sacktag_state *state,
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001517 u32 start_seq, u32 end_seq,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001518 bool dup_sack_in)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001519{
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001520 struct tcp_sock *tp = tcp_sk(sk);
1521 struct sk_buff *tmp;
1522
Eric Dumazet75c119a2017-10-05 22:21:27 -07001523 skb_rbtree_walk_from(skb) {
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001524 int in_sack = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001525 bool dup_sack = dup_sack_in;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001526
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001527 /* queue is in-order => we can short-circuit the walk early */
1528 if (!before(TCP_SKB_CB(skb)->seq, end_seq))
1529 break;
1530
Ian Morris00db4122015-04-03 09:17:27 +01001531 if (next_dup &&
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001532 before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
1533 in_sack = tcp_match_skb_to_sack(sk, skb,
1534 next_dup->start_seq,
1535 next_dup->end_seq);
1536 if (in_sack > 0)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001537 dup_sack = true;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001538 }
1539
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001540 /* skb reference here is a bit tricky to get right, since
1541 * shifting can eat and free both this skb and the next,
1542 * so not even _safe variant of the loop is enough.
1543 */
1544 if (in_sack <= 0) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001545 tmp = tcp_shift_skb_data(sk, skb, state,
1546 start_seq, end_seq, dup_sack);
Ian Morris00db4122015-04-03 09:17:27 +01001547 if (tmp) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001548 if (tmp != skb) {
1549 skb = tmp;
1550 continue;
1551 }
1552
1553 in_sack = 0;
1554 } else {
1555 in_sack = tcp_match_skb_to_sack(sk, skb,
1556 start_seq,
1557 end_seq);
1558 }
1559 }
1560
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001561 if (unlikely(in_sack < 0))
1562 break;
1563
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001564 if (in_sack) {
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001565 TCP_SKB_CB(skb)->sacked =
1566 tcp_sacktag_one(sk,
1567 state,
1568 TCP_SKB_CB(skb)->sacked,
1569 TCP_SKB_CB(skb)->seq,
1570 TCP_SKB_CB(skb)->end_seq,
1571 dup_sack,
Yuchung Cheng59c9af42013-07-22 16:20:47 -07001572 tcp_skb_pcount(skb),
Eric Dumazet9a568de2017-05-16 14:00:14 -07001573 skb->skb_mstamp);
Yuchung Chengb9f64822016-09-19 23:39:14 -04001574 tcp_rate_skb_delivered(sk, skb, state->rate);
Eric Dumazete2080072017-10-04 12:59:58 -07001575 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
1576 list_del_init(&skb->tcp_tsorted_anchor);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001577
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001578 if (!before(TCP_SKB_CB(skb)->seq,
1579 tcp_highest_sack_seq(tp)))
1580 tcp_advance_highest_sack(sk, skb);
1581 }
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001582 }
1583 return skb;
1584}
1585
Eric Dumazet75c119a2017-10-05 22:21:27 -07001586static struct sk_buff *tcp_sacktag_bsearch(struct sock *sk,
1587 struct tcp_sacktag_state *state,
1588 u32 seq)
1589{
1590 struct rb_node *parent, **p = &sk->tcp_rtx_queue.rb_node;
1591 struct sk_buff *skb;
Eric Dumazet75c119a2017-10-05 22:21:27 -07001592
1593 while (*p) {
1594 parent = *p;
1595 skb = rb_to_skb(parent);
1596 if (before(seq, TCP_SKB_CB(skb)->seq)) {
1597 p = &parent->rb_left;
1598 continue;
1599 }
1600 if (!before(seq, TCP_SKB_CB(skb)->end_seq)) {
1601 p = &parent->rb_right;
1602 continue;
1603 }
Eric Dumazet75c119a2017-10-05 22:21:27 -07001604 return skb;
1605 }
1606 return NULL;
1607}
1608
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001609static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001610 struct tcp_sacktag_state *state,
1611 u32 skip_to_seq)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001612{
Eric Dumazet75c119a2017-10-05 22:21:27 -07001613 if (skb && after(TCP_SKB_CB(skb)->seq, skip_to_seq))
1614 return skb;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001615
Eric Dumazet75c119a2017-10-05 22:21:27 -07001616 return tcp_sacktag_bsearch(sk, state, skip_to_seq);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001617}
1618
1619static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
1620 struct sock *sk,
1621 struct tcp_sack_block *next_dup,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001622 struct tcp_sacktag_state *state,
1623 u32 skip_to_seq)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001624{
Ian Morris51456b22015-04-03 09:17:26 +01001625 if (!next_dup)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001626 return skb;
1627
1628 if (before(next_dup->start_seq, skip_to_seq)) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001629 skb = tcp_sacktag_skip(skb, sk, state, next_dup->start_seq);
1630 skb = tcp_sacktag_walk(skb, sk, NULL, state,
1631 next_dup->start_seq, next_dup->end_seq,
1632 1);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001633 }
1634
1635 return skb;
1636}
1637
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001638static int tcp_sack_cache_ok(const struct tcp_sock *tp, const struct tcp_sack_block *cache)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001639{
1640 return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
1641}
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643static int
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001644tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001645 u32 prior_snd_una, struct tcp_sacktag_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001648 const unsigned char *ptr = (skb_transport_header(ack_skb) +
1649 TCP_SKB_CB(ack_skb)->sacked);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001650 struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2);
Adam Langley4389dde2008-07-19 00:07:02 -07001651 struct tcp_sack_block sp[TCP_NUM_SACKS];
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001652 struct tcp_sack_block *cache;
1653 struct sk_buff *skb;
Adam Langley4389dde2008-07-19 00:07:02 -07001654 int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001655 int used_sacks;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001656 bool found_dup_sack = false;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001657 int i, j;
Baruch Evenfda03fb2007-02-04 23:35:57 -08001658 int first_sack_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001660 state->flag = 0;
Yuchung Cheng737ff312017-11-08 13:01:27 -08001661 state->reord = tp->snd_nxt;
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001662
Yuchung Cheng737ff312017-11-08 13:01:27 -08001663 if (!tp->sacked_out)
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001664 tcp_highest_sack_reset(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07001666 found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire,
David S. Millerd06e0212007-06-18 22:43:06 -07001667 num_sacks, prior_snd_una);
Yuchung Chengb9f64822016-09-19 23:39:14 -04001668 if (found_dup_sack) {
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001669 state->flag |= FLAG_DSACKING_ACK;
Yuchung Chengb9f64822016-09-19 23:39:14 -04001670 tp->delivered++; /* A spurious retransmission is delivered */
1671 }
Baruch Even6f746512007-02-04 23:36:42 -08001672
1673 /* Eliminate too old ACKs, but take into
1674 * account more or less fresh ones, they can
1675 * contain valid SACK info.
1676 */
1677 if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
1678 return 0;
1679
Ilpo Järvinen96a2d412007-11-14 15:47:18 -08001680 if (!tp->packets_out)
1681 goto out;
1682
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001683 used_sacks = 0;
1684 first_sack_index = 0;
1685 for (i = 0; i < num_sacks; i++) {
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001686 bool dup_sack = !i && found_dup_sack;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001687
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001688 sp[used_sacks].start_seq = get_unaligned_be32(&sp_wire[i].start_seq);
1689 sp[used_sacks].end_seq = get_unaligned_be32(&sp_wire[i].end_seq);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001690
1691 if (!tcp_is_sackblock_valid(tp, dup_sack,
1692 sp[used_sacks].start_seq,
1693 sp[used_sacks].end_seq)) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001694 int mib_idx;
1695
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001696 if (dup_sack) {
1697 if (!tp->undo_marker)
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001698 mib_idx = LINUX_MIB_TCPDSACKIGNOREDNOUNDO;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001699 else
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001700 mib_idx = LINUX_MIB_TCPDSACKIGNOREDOLD;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001701 } else {
1702 /* Don't count olds caused by ACK reordering */
1703 if ((TCP_SKB_CB(ack_skb)->ack_seq != tp->snd_una) &&
1704 !after(sp[used_sacks].end_seq, tp->snd_una))
1705 continue;
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001706 mib_idx = LINUX_MIB_TCPSACKDISCARD;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001707 }
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001708
Eric Dumazetc10d9312016-04-29 14:16:47 -07001709 NET_INC_STATS(sock_net(sk), mib_idx);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001710 if (i == 0)
1711 first_sack_index = -1;
1712 continue;
1713 }
1714
1715 /* Ignore very old stuff early */
1716 if (!after(sp[used_sacks].end_seq, prior_snd_una))
1717 continue;
1718
1719 used_sacks++;
1720 }
1721
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001722 /* order SACK blocks to allow in order walk of the retrans queue */
1723 for (i = used_sacks - 1; i > 0; i--) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001724 for (j = 0; j < i; j++) {
1725 if (after(sp[j].start_seq, sp[j + 1].start_seq)) {
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07001726 swap(sp[j], sp[j + 1]);
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08001727
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001728 /* Track where the first SACK block goes to */
1729 if (j == first_sack_index)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001730 first_sack_index = j + 1;
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08001731 }
1732 }
1733 }
1734
Eric Dumazet75c119a2017-10-05 22:21:27 -07001735 state->mss_now = tcp_current_mss(sk);
Eric Dumazet75c119a2017-10-05 22:21:27 -07001736 skb = NULL;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001737 i = 0;
1738
1739 if (!tp->sacked_out) {
1740 /* It's already past, so skip checking against it */
1741 cache = tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
1742 } else {
1743 cache = tp->recv_sack_cache;
1744 /* Skip empty blocks in at head of the cache */
1745 while (tcp_sack_cache_ok(tp, cache) && !cache->start_seq &&
1746 !cache->end_seq)
1747 cache++;
Baruch Evenfda03fb2007-02-04 23:35:57 -08001748 }
1749
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001750 while (i < used_sacks) {
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001751 u32 start_seq = sp[i].start_seq;
1752 u32 end_seq = sp[i].end_seq;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001753 bool dup_sack = (found_dup_sack && (i == first_sack_index));
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001754 struct tcp_sack_block *next_dup = NULL;
Ilpo Järvinene56d6cd2007-11-01 00:09:37 -07001755
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001756 if (found_dup_sack && ((i + 1) == first_sack_index))
1757 next_dup = &sp[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001759 /* Skip too early cached blocks */
1760 while (tcp_sack_cache_ok(tp, cache) &&
1761 !before(start_seq, cache->end_seq))
1762 cache++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001764 /* Can skip some work by looking recv_sack_cache? */
1765 if (tcp_sack_cache_ok(tp, cache) && !dup_sack &&
1766 after(end_seq, cache->start_seq)) {
David S. Millerfe067e82007-03-07 12:12:44 -08001767
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001768 /* Head todo? */
1769 if (before(start_seq, cache->start_seq)) {
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001770 skb = tcp_sacktag_skip(skb, sk, state,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001771 start_seq);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001772 skb = tcp_sacktag_walk(skb, sk, next_dup,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001773 state,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001774 start_seq,
1775 cache->start_seq,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001776 dup_sack);
Baruch Evenfda03fb2007-02-04 23:35:57 -08001777 }
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08001778
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001779 /* Rest of the block already fully processed? */
Ilpo Järvinen20de20b2007-11-16 16:17:05 -08001780 if (!after(end_seq, cache->end_seq))
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001781 goto advance_sp;
Ilpo Järvinen20de20b2007-11-16 16:17:05 -08001782
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001783 skb = tcp_maybe_skipping_dsack(skb, sk, next_dup,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001784 state,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001785 cache->end_seq);
Ilpo Järvinene56d6cd2007-11-01 00:09:37 -07001786
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001787 /* ...tail remains todo... */
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001788 if (tcp_highest_sack_seq(tp) == cache->end_seq) {
Ilpo Järvinen20de20b2007-11-16 16:17:05 -08001789 /* ...but better entrypoint exists! */
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001790 skb = tcp_highest_sack(sk);
Ian Morris51456b22015-04-03 09:17:26 +01001791 if (!skb)
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001792 break;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001793 cache++;
1794 goto walk;
1795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001797 skb = tcp_sacktag_skip(skb, sk, state, cache->end_seq);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001798 /* Check overlap against next cached too (past this one already) */
1799 cache++;
1800 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
Ilpo Järvinenfbd52eb2007-11-10 21:24:19 -08001802
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001803 if (!before(start_seq, tcp_highest_sack_seq(tp))) {
1804 skb = tcp_highest_sack(sk);
Ian Morris51456b22015-04-03 09:17:26 +01001805 if (!skb)
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001806 break;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001807 }
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001808 skb = tcp_sacktag_skip(skb, sk, state, start_seq);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001809
1810walk:
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001811 skb = tcp_sacktag_walk(skb, sk, next_dup, state,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001812 start_seq, end_seq, dup_sack);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001813
1814advance_sp:
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001815 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001818 /* Clear the head of the cache sack blocks so we can skip it next time */
1819 for (i = 0; i < ARRAY_SIZE(tp->recv_sack_cache) - used_sacks; i++) {
1820 tp->recv_sack_cache[i].start_seq = 0;
1821 tp->recv_sack_cache[i].end_seq = 0;
1822 }
1823 for (j = 0; j < used_sacks; j++)
1824 tp->recv_sack_cache[i++] = sp[j];
1825
Yuchung Cheng737ff312017-11-08 13:01:27 -08001826 if (inet_csk(sk)->icsk_ca_state != TCP_CA_Loss || tp->undo_marker)
1827 tcp_check_sack_reordering(sk, state->reord, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Yuchung Cheng9dac8832015-04-29 11:28:30 -07001829 tcp_verify_left_out(tp);
Ilpo Järvinen96a2d412007-11-14 15:47:18 -08001830out:
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832#if FASTRETRANS_DEBUG > 0
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001833 WARN_ON((int)tp->sacked_out < 0);
1834 WARN_ON((int)tp->lost_out < 0);
1835 WARN_ON((int)tp->retrans_out < 0);
1836 WARN_ON((int)tcp_packets_in_flight(tp) < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837#endif
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001838 return state->flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
Ilpo Järvinen882beba2008-04-07 22:33:07 -07001841/* Limits sacked_out so that sum with lost_out isn't ever larger than
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001842 * packets_out. Returns false if sacked_out adjustement wasn't necessary.
Ilpo Järvinen30935cf2007-02-21 23:01:36 -08001843 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001844static bool tcp_limit_reno_sacked(struct tcp_sock *tp)
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001845{
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001846 u32 holes;
1847
1848 holes = max(tp->lost_out, 1U);
1849 holes = min(holes, tp->packets_out);
1850
1851 if ((tp->sacked_out + holes) > tp->packets_out) {
1852 tp->sacked_out = tp->packets_out - holes;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001853 return true;
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001854 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001855 return false;
Ilpo Järvinen882beba2008-04-07 22:33:07 -07001856}
1857
1858/* If we receive more dupacks than we expected counting segments
1859 * in assumption of absent reordering, interpret this as reordering.
1860 * The only another reason could be bug in receiver TCP.
1861 */
1862static void tcp_check_reno_reordering(struct sock *sk, const int addend)
1863{
1864 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng737ff312017-11-08 13:01:27 -08001865
1866 if (!tcp_limit_reno_sacked(tp))
1867 return;
1868
1869 tp->reordering = min_t(u32, tp->packets_out + addend,
1870 sock_net(sk)->ipv4.sysctl_tcp_max_reordering);
1871 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRENOREORDER);
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001872}
1873
1874/* Emulate SACKs for SACKless connection: account for a new dupack. */
1875
1876static void tcp_add_reno_sack(struct sock *sk)
1877{
1878 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Chengddf1af62016-02-02 10:33:06 -08001879 u32 prior_sacked = tp->sacked_out;
1880
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001881 tp->sacked_out++;
1882 tcp_check_reno_reordering(sk, 0);
Yuchung Chengddf1af62016-02-02 10:33:06 -08001883 if (tp->sacked_out > prior_sacked)
1884 tp->delivered++; /* Some out-of-order packet is delivered */
Ilpo Järvinen005903b2007-08-09 14:44:16 +03001885 tcp_verify_left_out(tp);
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001886}
1887
1888/* Account for ACK, ACKing some data in Reno Recovery phase. */
1889
1890static void tcp_remove_reno_sacks(struct sock *sk, int acked)
1891{
1892 struct tcp_sock *tp = tcp_sk(sk);
1893
1894 if (acked > 0) {
1895 /* One ACK acked hole. The rest eat duplicate ACKs. */
Yuchung Chengddf1af62016-02-02 10:33:06 -08001896 tp->delivered += max_t(int, acked - tp->sacked_out, 1);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001897 if (acked - 1 >= tp->sacked_out)
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001898 tp->sacked_out = 0;
1899 else
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001900 tp->sacked_out -= acked - 1;
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001901 }
1902 tcp_check_reno_reordering(sk, acked);
Ilpo Järvinen005903b2007-08-09 14:44:16 +03001903 tcp_verify_left_out(tp);
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001904}
1905
1906static inline void tcp_reset_reno_sack(struct tcp_sock *tp)
1907{
1908 tp->sacked_out = 0;
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001909}
1910
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001911void tcp_clear_retrans(struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 tp->retrans_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 tp->lost_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 tp->undo_marker = 0;
Yuchung Cheng6e08d5e2014-07-02 12:07:16 -07001916 tp->undo_retrans = -1;
Ilpo Järvinen4cd82992007-10-11 17:34:57 -07001917 tp->sacked_out = 0;
1918}
1919
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001920static inline void tcp_init_undo(struct tcp_sock *tp)
1921{
1922 tp->undo_marker = tp->snd_una;
1923 /* Retransmission still in flight may cause DSACKs later. */
1924 tp->undo_retrans = tp->retrans_out ? : -1;
1925}
1926
Yuchung Chengb8fef652018-05-16 16:40:16 -07001927static bool tcp_is_rack(const struct sock *sk)
1928{
1929 return sock_net(sk)->ipv4.sysctl_tcp_recovery & TCP_RACK_LOSS_DETECTION;
1930}
1931
Yuchung Cheng2ad55f52018-05-16 16:40:14 -07001932/* If we detect SACK reneging, forget all SACK information
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 * and reset tags completely, otherwise preserve SACKs. If receiver
1934 * dropped its ofo queue, we will know this due to reneging detection.
1935 */
Yuchung Cheng2ad55f52018-05-16 16:40:14 -07001936static void tcp_timeout_mark_lost(struct sock *sk)
1937{
1938 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng56f8c5d2018-05-16 16:40:17 -07001939 struct sk_buff *skb, *head;
Yuchung Cheng2ad55f52018-05-16 16:40:14 -07001940 bool is_reneg; /* is receiver reneging on SACKs? */
1941
Yuchung Cheng56f8c5d2018-05-16 16:40:17 -07001942 head = tcp_rtx_queue_head(sk);
1943 is_reneg = head && (TCP_SKB_CB(head)->sacked & TCPCB_SACKED_ACKED);
Yuchung Cheng2ad55f52018-05-16 16:40:14 -07001944 if (is_reneg) {
1945 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSACKRENEGING);
1946 tp->sacked_out = 0;
1947 /* Mark SACK reneging until we recover from this loss event. */
1948 tp->is_sack_reneg = 1;
1949 } else if (tcp_is_reno(tp)) {
1950 tcp_reset_reno_sack(tp);
1951 }
1952
Yuchung Cheng56f8c5d2018-05-16 16:40:17 -07001953 skb = head;
Yuchung Cheng2ad55f52018-05-16 16:40:14 -07001954 skb_rbtree_walk_from(skb) {
1955 if (is_reneg)
1956 TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_ACKED;
Yuchung Cheng56f8c5d2018-05-16 16:40:17 -07001957 else if (tcp_is_rack(sk) && skb != head &&
1958 tcp_rack_skb_timeout(tp, skb, 0) > 0)
1959 continue; /* Don't mark recently sent ones lost yet */
Yuchung Cheng2ad55f52018-05-16 16:40:14 -07001960 tcp_mark_skb_lost(sk, skb);
1961 }
1962 tcp_verify_left_out(tp);
1963 tcp_clear_all_retrans_hints(tp);
1964}
1965
1966/* Enter Loss state. */
Neal Cardwell5ae344c2014-08-04 19:12:29 -04001967void tcp_enter_loss(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001969 const struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisov1043e252016-02-03 09:46:52 +02001971 struct net *net = sock_net(sk);
Yuchung Chengcc663f42017-04-07 11:42:05 -07001972 bool new_recovery = icsk->icsk_ca_state < TCP_CA_Recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Yuchung Chengc77d62f2018-05-16 16:40:15 -07001974 tcp_timeout_mark_lost(sk);
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /* Reduce ssthresh if it has not yet been made inside this window. */
Yuchung Chenge33099f2013-03-20 13:33:00 +00001977 if (icsk->icsk_ca_state <= TCP_CA_Disorder ||
1978 !after(tp->high_seq, tp->snd_una) ||
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001979 (icsk->icsk_ca_state == TCP_CA_Loss && !icsk->icsk_retransmits)) {
1980 tp->prior_ssthresh = tcp_current_ssthresh(sk);
Yuchung Cheng4faf7832017-08-03 20:38:51 -07001981 tp->prior_cwnd = tp->snd_cwnd;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001982 tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
1983 tcp_ca_event(sk, CA_EVENT_LOSS);
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001984 tcp_init_undo(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
Yuchung Cheng56f8c5d2018-05-16 16:40:17 -07001986 tp->snd_cwnd = tcp_packets_in_flight(tp) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 tp->snd_cwnd_cnt = 0;
Eric Dumazetc2203cf2017-05-16 14:00:04 -07001988 tp->snd_cwnd_stamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Yuchung Cheng74c181d2013-08-12 16:41:25 -07001990 /* Timeout in disordered state after receiving substantial DUPACKs
1991 * suggests that the degree of reordering is over-estimated.
1992 */
1993 if (icsk->icsk_ca_state <= TCP_CA_Disorder &&
Nikolay Borisov1043e252016-02-03 09:46:52 +02001994 tp->sacked_out >= net->ipv4.sysctl_tcp_reordering)
Yuchung Cheng74c181d2013-08-12 16:41:25 -07001995 tp->reordering = min_t(unsigned int, tp->reordering,
Nikolay Borisov1043e252016-02-03 09:46:52 +02001996 net->ipv4.sysctl_tcp_reordering);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001997 tcp_set_ca_state(sk, TCP_CA_Loss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 tp->high_seq = tp->snd_nxt;
Florian Westphal735d3832014-09-29 13:08:30 +02001999 tcp_ecn_queue_cwr(tp);
Yuchung Chenge33099f2013-03-20 13:33:00 +00002000
Yuchung Chengcc663f42017-04-07 11:42:05 -07002001 /* F-RTO RFC5682 sec 3.1 step 1: retransmit SND.UNA if no previous
2002 * loss recovery is underway except recurring timeout(s) on
2003 * the same SND.UNA (sec 3.2). Disable F-RTO on path MTU probing
Yuchung Chenge33099f2013-03-20 13:33:00 +00002004 */
Eric Dumazetaf9b69a2017-10-26 21:55:10 -07002005 tp->frto = net->ipv4.sysctl_tcp_frto &&
Yuchung Chengcc663f42017-04-07 11:42:05 -07002006 (new_recovery || icsk->icsk_retransmits) &&
2007 !inet_csk(sk)->icsk_mtup.probe_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002010/* If ACK arrived pointing to a remembered SACK, it means that our
2011 * remembered SACKs do not reflect real state of receiver i.e.
2012 * receiver _host_ is heavily congested (or buggy).
2013 *
Neal Cardwell5ae344c2014-08-04 19:12:29 -04002014 * To avoid big spurious retransmission bursts due to transient SACK
2015 * scoreboard oddities that look like reneging, we give the receiver a
2016 * little time (max(RTT/2, 10ms)) to send us some more ACKs that will
2017 * restore sanity to the SACK scoreboard. If the apparent reneging
2018 * persists until this RTO then we'll clear the SACK scoreboard.
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002019 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002020static bool tcp_check_sack_reneging(struct sock *sk, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002022 if (flag & FLAG_SACK_RENEGING) {
Neal Cardwell5ae344c2014-08-04 19:12:29 -04002023 struct tcp_sock *tp = tcp_sk(sk);
2024 unsigned long delay = max(usecs_to_jiffies(tp->srtt_us >> 4),
2025 msecs_to_jiffies(10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002027 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
Neal Cardwell5ae344c2014-08-04 19:12:29 -04002028 delay, TCP_RTO_MAX);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002029 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002031 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002034/* Heurestics to calculate number of duplicate ACKs. There's no dupACKs
2035 * counter when SACK is enabled (without SACK, sacked_out is used for
2036 * that purpose).
2037 *
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002038 * With reordering, holes may still be in flight, so RFC3517 recovery
2039 * uses pure sacked_out (total number of SACKed segments) even though
2040 * it violates the RFC that uses duplicate ACKs, often these are equal
2041 * but when e.g. out-of-window ACKs or packet duplication occurs,
2042 * they differ. Since neither occurs due to loss, TCP should really
2043 * ignore them.
2044 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002045static inline int tcp_dupack_heuristics(const struct tcp_sock *tp)
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002046{
Yuchung Cheng713bafe2017-11-08 13:01:26 -08002047 return tp->sacked_out + 1;
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002048}
2049
Yuchung Cheng713bafe2017-11-08 13:01:26 -08002050/* Linux NewReno/SACK/ECN state machine.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 * --------------------------------------
2052 *
2053 * "Open" Normal state, no dubious events, fast path.
2054 * "Disorder" In all the respects it is "Open",
2055 * but requires a bit more attention. It is entered when
2056 * we see some SACKs or dupacks. It is split of "Open"
2057 * mainly to move some processing from fast path to slow one.
2058 * "CWR" CWND was reduced due to some Congestion Notification event.
2059 * It can be ECN, ICMP source quench, local device congestion.
2060 * "Recovery" CWND was reduced, we are fast-retransmitting.
2061 * "Loss" CWND was reduced due to RTO timeout or SACK reneging.
2062 *
2063 * tcp_fastretrans_alert() is entered:
2064 * - each incoming ACK, if state is not "Open"
2065 * - when arrived ACK is unusual, namely:
2066 * * SACK
2067 * * Duplicate ACK.
2068 * * ECN ECE.
2069 *
2070 * Counting packets in flight is pretty simple.
2071 *
2072 * in_flight = packets_out - left_out + retrans_out
2073 *
2074 * packets_out is SND.NXT-SND.UNA counted in packets.
2075 *
2076 * retrans_out is number of retransmitted segments.
2077 *
2078 * left_out is number of segments left network, but not ACKed yet.
2079 *
2080 * left_out = sacked_out + lost_out
2081 *
2082 * sacked_out: Packets, which arrived to receiver out of order
2083 * and hence not ACKed. With SACKs this number is simply
2084 * amount of SACKed data. Even without SACKs
2085 * it is easy to give pretty reliable estimate of this number,
2086 * counting duplicate ACKs.
2087 *
2088 * lost_out: Packets lost by network. TCP has no explicit
2089 * "loss notification" feedback from network (for now).
2090 * It means that this number can be only _guessed_.
2091 * Actually, it is the heuristics to predict lossage that
2092 * distinguishes different algorithms.
2093 *
2094 * F.e. after RTO, when all the queue is considered as lost,
2095 * lost_out = packets_out and in_flight = retrans_out.
2096 *
Yuchung Chenga0370b32017-01-12 22:11:36 -08002097 * Essentially, we have now a few algorithms detecting
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 * lost packets.
2099 *
Yuchung Chenga0370b32017-01-12 22:11:36 -08002100 * If the receiver supports SACK:
2101 *
2102 * RFC6675/3517: It is the conventional algorithm. A packet is
2103 * considered lost if the number of higher sequence packets
2104 * SACKed is greater than or equal the DUPACK thoreshold
2105 * (reordering). This is implemented in tcp_mark_head_lost and
2106 * tcp_update_scoreboard.
2107 *
2108 * RACK (draft-ietf-tcpm-rack-01): it is a newer algorithm
2109 * (2017-) that checks timing instead of counting DUPACKs.
2110 * Essentially a packet is considered lost if it's not S/ACKed
2111 * after RTT + reordering_window, where both metrics are
2112 * dynamically measured and adjusted. This is implemented in
2113 * tcp_rack_mark_lost.
2114 *
Yuchung Chenga0370b32017-01-12 22:11:36 -08002115 * If the receiver does not support SACK:
2116 *
2117 * NewReno (RFC6582): in Recovery we assume that one segment
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 * is lost (classic Reno). While we are in Recovery and
2119 * a partial ACK arrives, we assume that one more packet
2120 * is lost (NewReno). This heuristics are the same in NewReno
2121 * and SACK.
2122 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 * Really tricky (and requiring careful tuning) part of algorithm
2124 * is hidden in functions tcp_time_to_recover() and tcp_xmit_retransmit_queue().
2125 * The first determines the moment _when_ we should reduce CWND and,
2126 * hence, slow down forward transmission. In fact, it determines the moment
2127 * when we decide that hole is caused by loss, rather than by a reorder.
2128 *
2129 * tcp_xmit_retransmit_queue() decides, _what_ we should retransmit to fill
2130 * holes, caused by lost packets.
2131 *
2132 * And the most logically complicated part of algorithm is undo
2133 * heuristics. We detect false retransmits due to both too early
2134 * fast retransmit (reordering) and underestimated RTO, analyzing
2135 * timestamps and D-SACKs. When we detect that some segments were
2136 * retransmitted by mistake and CWND reduction was wrong, we undo
2137 * window reduction and abort recovery phase. This logic is hidden
2138 * inside several functions named tcp_try_undo_<something>.
2139 */
2140
2141/* This function decides, when we should leave Disordered state
2142 * and enter Recovery phase, reducing congestion window.
2143 *
2144 * Main question: may we further continue forward transmission
2145 * with the same cwnd?
2146 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002147static bool tcp_time_to_recover(struct sock *sk, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002149 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 /* Trick#1: The loss is proven. */
2152 if (tp->lost_out)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002153 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 /* Not-A-Trick#2 : Classic rule... */
Yuchung Chengb38a51f2018-05-16 16:40:11 -07002156 if (!tcp_is_rack(sk) && tcp_dupack_heuristics(tp) > tp->reordering)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002157 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002159 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
Yuchung Cheng974c1232012-01-19 14:42:21 +00002162/* Detect loss in event "A" above by marking head of queue up as lost.
Yuchung Cheng713bafe2017-11-08 13:01:26 -08002163 * For non-SACK(Reno) senders, the first "packets" number of segments
Yuchung Cheng974c1232012-01-19 14:42:21 +00002164 * are considered lost. For RFC3517 SACK, a segment is considered lost if it
2165 * has at least tp->reordering SACKed seqments above it; "packets" refers to
2166 * the maximum SACKed segments to pass before reaching this limit.
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002167 */
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002168static void tcp_mark_head_lost(struct sock *sk, int packets, int mark_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002170 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 struct sk_buff *skb;
Neal Cardwelld88270e2016-01-25 14:01:53 -08002172 int cnt, oldcnt, lost;
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002173 unsigned int mss;
Yuchung Cheng974c1232012-01-19 14:42:21 +00002174 /* Use SACK to deduce losses of new sequences sent during recovery */
2175 const u32 loss_high = tcp_is_sack(tp) ? tp->snd_nxt : tp->high_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002177 WARN_ON(packets > tp->packets_out);
Eric Dumazet5e76ee42017-10-05 22:21:24 -07002178 skb = tp->lost_skb_hint;
2179 if (skb) {
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002180 /* Head already handled? */
Eric Dumazet5e76ee42017-10-05 22:21:24 -07002181 if (mark_head && after(TCP_SKB_CB(skb)->seq, tp->snd_una))
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002182 return;
Eric Dumazet5e76ee42017-10-05 22:21:24 -07002183 cnt = tp->lost_cnt_hint;
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002184 } else {
Eric Dumazet75c119a2017-10-05 22:21:27 -07002185 skb = tcp_rtx_queue_head(sk);
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002186 cnt = 0;
2187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Eric Dumazet75c119a2017-10-05 22:21:27 -07002189 skb_rbtree_walk_from(skb) {
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002190 /* TODO: do this better */
2191 /* this is not the most efficient way to do this... */
2192 tp->lost_skb_hint = skb;
2193 tp->lost_cnt_hint = cnt;
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002194
Yuchung Cheng974c1232012-01-19 14:42:21 +00002195 if (after(TCP_SKB_CB(skb)->end_seq, loss_high))
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002196 break;
2197
2198 oldcnt = cnt;
Yuchung Cheng713bafe2017-11-08 13:01:26 -08002199 if (tcp_is_reno(tp) ||
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002200 (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
2201 cnt += tcp_skb_pcount(skb);
2202
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002203 if (cnt > packets) {
Yuchung Cheng713bafe2017-11-08 13:01:26 -08002204 if (tcp_is_sack(tp) ||
Neal Cardwellc0638c22012-03-02 21:36:51 +00002205 (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) ||
Yuchung Chengb3de7552010-09-24 13:22:06 +00002206 (oldcnt >= packets))
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002207 break;
2208
Eric Dumazetf69ad292015-06-11 09:15:18 -07002209 mss = tcp_skb_mss(skb);
Neal Cardwelld88270e2016-01-25 14:01:53 -08002210 /* If needed, chop off the prefix to mark as lost. */
2211 lost = (packets - oldcnt) * mss;
2212 if (lost < skb->len &&
Eric Dumazet75c119a2017-10-05 22:21:27 -07002213 tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb,
2214 lost, mss, GFP_ATOMIC) < 0)
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002215 break;
2216 cnt = packets;
2217 }
2218
Ilpo Järvinen41ea36e2008-09-20 21:19:22 -07002219 tcp_skb_mark_lost(tp, skb);
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002220
2221 if (mark_head)
2222 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
Ilpo Järvinen005903b2007-08-09 14:44:16 +03002224 tcp_verify_left_out(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
2227/* Account newly detected lost packet(s) */
2228
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002229static void tcp_update_scoreboard(struct sock *sk, int fast_rexmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002231 struct tcp_sock *tp = tcp_sk(sk);
2232
Yuchung Cheng6ac06ec2018-05-16 16:40:12 -07002233 if (tcp_is_sack(tp)) {
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002234 int sacked_upto = tp->sacked_out - tp->reordering;
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002235 if (sacked_upto >= 0)
2236 tcp_mark_head_lost(sk, sacked_upto, 0);
2237 else if (fast_rexmit)
2238 tcp_mark_head_lost(sk, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
Yuchung Cheng77c63122015-10-16 21:57:44 -07002242static bool tcp_tsopt_ecr_before(const struct tcp_sock *tp, u32 when)
2243{
2244 return tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
2245 before(tp->rx_opt.rcv_tsecr, when);
2246}
2247
Yuchung Cheng659a8ad2015-10-16 21:57:46 -07002248/* skb is spurious retransmitted if the returned timestamp echo
2249 * reply is prior to the skb transmission time
2250 */
2251static bool tcp_skb_spurious_retrans(const struct tcp_sock *tp,
2252 const struct sk_buff *skb)
2253{
2254 return (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS) &&
2255 tcp_tsopt_ecr_before(tp, tcp_skb_timestamp(skb));
2256}
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258/* Nothing was retransmitted or returned timestamp is less
2259 * than timestamp of the first retransmission.
2260 */
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00002261static inline bool tcp_packet_delayed(const struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 return !tp->retrans_stamp ||
Yuchung Cheng77c63122015-10-16 21:57:44 -07002264 tcp_tsopt_ecr_before(tp, tp->retrans_stamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
2267/* Undo procedures. */
2268
Marcelo Leitner1f37bf82014-11-04 17:15:08 -02002269/* We can clear retrans_stamp when there are no retransmissions in the
2270 * window. It would seem that it is trivially available for us in
2271 * tp->retrans_out, however, that kind of assumptions doesn't consider
2272 * what will happen if errors occur when sending retransmission for the
2273 * second time. ...It could the that such segment has only
2274 * TCPCB_EVER_RETRANS set at the present time. It seems that checking
2275 * the head skb is enough except for some reneging corner cases that
2276 * are not worth the effort.
2277 *
2278 * Main reason for all this complexity is the fact that connection dying
2279 * time now depends on the validity of the retrans_stamp, in particular,
2280 * that successive retransmissions of a segment must not advance
2281 * retrans_stamp under any conditions.
2282 */
2283static bool tcp_any_retrans_done(const struct sock *sk)
2284{
2285 const struct tcp_sock *tp = tcp_sk(sk);
2286 struct sk_buff *skb;
2287
2288 if (tp->retrans_out)
2289 return true;
2290
Eric Dumazet75c119a2017-10-05 22:21:27 -07002291 skb = tcp_rtx_queue_head(sk);
Marcelo Leitner1f37bf82014-11-04 17:15:08 -02002292 if (unlikely(skb && TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS))
2293 return true;
2294
2295 return false;
2296}
2297
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002298static void DBGUNDO(struct sock *sk, const char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
Joe Perches39347882017-09-10 19:02:25 -07002300#if FASTRETRANS_DEBUG > 1
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002301 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 struct inet_sock *inet = inet_sk(sk);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002303
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002304 if (sk->sk_family == AF_INET) {
Joe Perches91df42b2012-05-15 14:11:54 +00002305 pr_debug("Undo %s %pI4/%u c%u l%u ss%u/%u p%u\n",
2306 msg,
2307 &inet->inet_daddr, ntohs(inet->inet_dport),
2308 tp->snd_cwnd, tcp_left_out(tp),
2309 tp->snd_ssthresh, tp->prior_ssthresh,
2310 tp->packets_out);
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002311 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002312#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002313 else if (sk->sk_family == AF_INET6) {
Joe Perches91df42b2012-05-15 14:11:54 +00002314 pr_debug("Undo %s %pI6/%u c%u l%u ss%u/%u p%u\n",
2315 msg,
Eric Dumazet019b1c92016-09-22 17:54:00 -07002316 &sk->sk_v6_daddr, ntohs(inet->inet_dport),
Joe Perches91df42b2012-05-15 14:11:54 +00002317 tp->snd_cwnd, tcp_left_out(tp),
2318 tp->snd_ssthresh, tp->prior_ssthresh,
2319 tp->packets_out);
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002320 }
2321#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322#endif
Joe Perches39347882017-09-10 19:02:25 -07002323}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Yuchung Cheng7026b912013-05-29 14:20:13 +00002325static void tcp_undo_cwnd_reduction(struct sock *sk, bool unmark_loss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002327 struct tcp_sock *tp = tcp_sk(sk);
2328
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002329 if (unmark_loss) {
2330 struct sk_buff *skb;
2331
Eric Dumazet75c119a2017-10-05 22:21:27 -07002332 skb_rbtree_walk(skb, &sk->tcp_rtx_queue) {
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002333 TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST;
2334 }
2335 tp->lost_out = 0;
2336 tcp_clear_all_retrans_hints(tp);
2337 }
2338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (tp->prior_ssthresh) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002340 const struct inet_connection_sock *icsk = inet_csk(sk);
2341
Florian Westphale9799182016-11-21 14:18:38 +01002342 tp->snd_cwnd = icsk->icsk_ca_ops->undo_cwnd(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Yuchung Cheng7026b912013-05-29 14:20:13 +00002344 if (tp->prior_ssthresh > tp->snd_ssthresh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 tp->snd_ssthresh = tp->prior_ssthresh;
Florian Westphal735d3832014-09-29 13:08:30 +02002346 tcp_ecn_withdraw_cwr(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
Eric Dumazetc2203cf2017-05-16 14:00:04 -07002349 tp->snd_cwnd_stamp = tcp_jiffies32;
Yuchung Cheng7026b912013-05-29 14:20:13 +00002350 tp->undo_marker = 0;
Yuchung Chengcd1fc852017-12-07 11:33:31 -08002351 tp->rack.advanced = 1; /* Force RACK to re-exam losses */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00002354static inline bool tcp_may_undo(const struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002356 return tp->undo_marker && (!tp->undo_retrans || tcp_packet_delayed(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357}
2358
2359/* People celebrate: "We love our President!" */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002360static bool tcp_try_undo_recovery(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002362 struct tcp_sock *tp = tcp_sk(sk);
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (tcp_may_undo(tp)) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07002365 int mib_idx;
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 /* Happy end! We did not retransmit anything
2368 * or our original transmission succeeded.
2369 */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002370 DBGUNDO(sk, inet_csk(sk)->icsk_ca_state == TCP_CA_Loss ? "loss" : "retrans");
Yuchung Cheng7026b912013-05-29 14:20:13 +00002371 tcp_undo_cwnd_reduction(sk, false);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002372 if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss)
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07002373 mib_idx = LINUX_MIB_TCPLOSSUNDO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 else
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07002375 mib_idx = LINUX_MIB_TCPFULLUNDO;
2376
Eric Dumazetc10d9312016-04-29 14:16:47 -07002377 NET_INC_STATS(sock_net(sk), mib_idx);
Priyaranjan Jha1f255692017-11-03 16:38:48 -07002378 } else if (tp->rack.reo_wnd_persist) {
2379 tp->rack.reo_wnd_persist--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002381 if (tp->snd_una == tp->high_seq && tcp_is_reno(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 /* Hold old state until something *above* high_seq
2383 * is ACKed. For Reno it is MUST to prevent false
2384 * fast retransmits (RFC2582). SACK TCP is safe. */
Marcelo Leitner1f37bf82014-11-04 17:15:08 -02002385 if (!tcp_any_retrans_done(sk))
2386 tp->retrans_stamp = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002387 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002389 tcp_set_ca_state(sk, TCP_CA_Open);
Yousuk Seungd4761752017-12-07 13:41:34 -08002390 tp->is_sack_reneg = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002391 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
2394/* Try to undo cwnd reduction, because D-SACKs acked all retransmitted data */
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002395static bool tcp_try_undo_dsack(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002397 struct tcp_sock *tp = tcp_sk(sk);
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (tp->undo_marker && !tp->undo_retrans) {
Priyaranjan Jha1f255692017-11-03 16:38:48 -07002400 tp->rack.reo_wnd_persist = min(TCP_RACK_RECOVERY_THRESH,
2401 tp->rack.reo_wnd_persist + 1);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002402 DBGUNDO(sk, "D-SACK");
Yuchung Cheng7026b912013-05-29 14:20:13 +00002403 tcp_undo_cwnd_reduction(sk, false);
Eric Dumazetc10d9312016-04-29 14:16:47 -07002404 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKUNDO);
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002405 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002407 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
Yuchung Chenge33099f2013-03-20 13:33:00 +00002410/* Undo during loss recovery after partial ACK or using F-RTO. */
2411static bool tcp_try_undo_loss(struct sock *sk, bool frto_undo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002413 struct tcp_sock *tp = tcp_sk(sk);
2414
Yuchung Chenge33099f2013-03-20 13:33:00 +00002415 if (frto_undo || tcp_may_undo(tp)) {
Yuchung Cheng7026b912013-05-29 14:20:13 +00002416 tcp_undo_cwnd_reduction(sk, true);
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002417
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002418 DBGUNDO(sk, "partial loss");
Eric Dumazetc10d9312016-04-29 14:16:47 -07002419 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPLOSSUNDO);
Yuchung Chenge33099f2013-03-20 13:33:00 +00002420 if (frto_undo)
Eric Dumazetc10d9312016-04-29 14:16:47 -07002421 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07002422 LINUX_MIB_TCPSPURIOUSRTOS);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002423 inet_csk(sk)->icsk_retransmits = 0;
Yousuk Seungd4761752017-12-07 13:41:34 -08002424 if (frto_undo || tcp_is_sack(tp)) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002425 tcp_set_ca_state(sk, TCP_CA_Open);
Yousuk Seungd4761752017-12-07 13:41:34 -08002426 tp->is_sack_reneg = 0;
2427 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002428 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002430 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
Yuchung Cheng37598242015-07-01 14:11:15 -07002433/* The cwnd reduction in CWR and Recovery uses the PRR algorithm in RFC 6937.
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002434 * It computes the number of packets to send (sndcnt) based on packets newly
2435 * delivered:
2436 * 1) If the packets in flight is larger than ssthresh, PRR spreads the
2437 * cwnd reductions across a full RTT.
Yuchung Cheng37598242015-07-01 14:11:15 -07002438 * 2) Otherwise PRR uses packet conservation to send as much as delivered.
2439 * But when the retransmits are acked without further losses, PRR
2440 * slow starts cwnd up to ssthresh to speed up the recovery.
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002441 */
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002442static void tcp_init_cwnd_reduction(struct sock *sk)
Yuchung Cheng684bad12012-09-02 17:38:04 +00002443{
2444 struct tcp_sock *tp = tcp_sk(sk);
2445
2446 tp->high_seq = tp->snd_nxt;
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00002447 tp->tlp_high_seq = 0;
Yuchung Cheng684bad12012-09-02 17:38:04 +00002448 tp->snd_cwnd_cnt = 0;
2449 tp->prior_cwnd = tp->snd_cwnd;
2450 tp->prr_delivered = 0;
2451 tp->prr_out = 0;
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002452 tp->snd_ssthresh = inet_csk(sk)->icsk_ca_ops->ssthresh(sk);
Florian Westphal735d3832014-09-29 13:08:30 +02002453 tcp_ecn_queue_cwr(tp);
Yuchung Cheng684bad12012-09-02 17:38:04 +00002454}
2455
Yuchung Cheng57dde7f2017-01-12 22:11:33 -08002456void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked, int flag)
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002457{
2458 struct tcp_sock *tp = tcp_sk(sk);
2459 int sndcnt = 0;
2460 int delta = tp->snd_ssthresh - tcp_packets_in_flight(tp);
2461
Yuchung Cheng8b8a3212016-01-06 12:42:38 -08002462 if (newly_acked_sacked <= 0 || WARN_ON_ONCE(!tp->prior_cwnd))
2463 return;
2464
Yuchung Cheng684bad12012-09-02 17:38:04 +00002465 tp->prr_delivered += newly_acked_sacked;
Yuchung Cheng37598242015-07-01 14:11:15 -07002466 if (delta < 0) {
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002467 u64 dividend = (u64)tp->snd_ssthresh * tp->prr_delivered +
2468 tp->prior_cwnd - 1;
2469 sndcnt = div_u64(dividend, tp->prior_cwnd) - tp->prr_out;
Yuchung Cheng37598242015-07-01 14:11:15 -07002470 } else if ((flag & FLAG_RETRANS_DATA_ACKED) &&
2471 !(flag & FLAG_LOST_RETRANS)) {
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002472 sndcnt = min_t(int, delta,
2473 max_t(int, tp->prr_delivered - tp->prr_out,
2474 newly_acked_sacked) + 1);
Yuchung Cheng37598242015-07-01 14:11:15 -07002475 } else {
2476 sndcnt = min(delta, newly_acked_sacked);
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002477 }
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002478 /* Force a fast retransmit upon entering fast recovery */
2479 sndcnt = max(sndcnt, (tp->prr_out ? 0 : 1));
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002480 tp->snd_cwnd = tcp_packets_in_flight(tp) + sndcnt;
2481}
2482
Yuchung Cheng684bad12012-09-02 17:38:04 +00002483static inline void tcp_end_cwnd_reduction(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002485 struct tcp_sock *tp = tcp_sk(sk);
Nandita Dukkipatia262f0c2011-08-21 20:21:57 +00002486
Yuchung Chengc0402762016-09-19 23:39:21 -04002487 if (inet_csk(sk)->icsk_ca_ops->cong_control)
2488 return;
2489
Yuchung Cheng684bad12012-09-02 17:38:04 +00002490 /* Reset cwnd to ssthresh in CWR or Recovery (unless it's undone) */
Yuchung Chenged254972017-08-01 13:22:32 -07002491 if (tp->snd_ssthresh < TCP_INFINITE_SSTHRESH &&
2492 (inet_csk(sk)->icsk_ca_state == TCP_CA_CWR || tp->undo_marker)) {
Yuchung Cheng684bad12012-09-02 17:38:04 +00002493 tp->snd_cwnd = tp->snd_ssthresh;
Eric Dumazetc2203cf2017-05-16 14:00:04 -07002494 tp->snd_cwnd_stamp = tcp_jiffies32;
Yuchung Cheng67d41202011-03-14 10:57:03 +00002495 }
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002496 tcp_ca_event(sk, CA_EVENT_COMPLETE_CWR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
Yuchung Cheng684bad12012-09-02 17:38:04 +00002499/* Enter CWR state. Disable cwnd undo since congestion is proven with ECN */
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002500void tcp_enter_cwr(struct sock *sk)
Yuchung Cheng09484d12012-09-02 17:38:02 +00002501{
2502 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng09484d12012-09-02 17:38:02 +00002503
2504 tp->prior_ssthresh = 0;
Yuchung Cheng684bad12012-09-02 17:38:04 +00002505 if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) {
Yuchung Cheng09484d12012-09-02 17:38:02 +00002506 tp->undo_marker = 0;
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002507 tcp_init_cwnd_reduction(sk);
Yuchung Cheng09484d12012-09-02 17:38:02 +00002508 tcp_set_ca_state(sk, TCP_CA_CWR);
2509 }
2510}
Kenneth Klette Jonassen7782ad82015-06-10 19:08:16 +02002511EXPORT_SYMBOL(tcp_enter_cwr);
Yuchung Cheng09484d12012-09-02 17:38:02 +00002512
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07002513static void tcp_try_keep_open(struct sock *sk)
2514{
2515 struct tcp_sock *tp = tcp_sk(sk);
2516 int state = TCP_CA_Open;
2517
Neal Cardwellf6982042011-11-16 08:58:04 +00002518 if (tcp_left_out(tp) || tcp_any_retrans_done(sk))
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07002519 state = TCP_CA_Disorder;
2520
2521 if (inet_csk(sk)->icsk_ca_state != state) {
2522 tcp_set_ca_state(sk, state);
2523 tp->high_seq = tp->snd_nxt;
2524 }
2525}
2526
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002527static void tcp_try_to_open(struct sock *sk, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002529 struct tcp_sock *tp = tcp_sk(sk);
2530
Ilpo Järvinen86426c22007-08-09 14:45:17 +03002531 tcp_verify_left_out(tp);
2532
Yuchung Cheng9b441902013-03-20 13:32:58 +00002533 if (!tcp_any_retrans_done(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 tp->retrans_stamp = 0;
2535
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002536 if (flag & FLAG_ECE)
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002537 tcp_enter_cwr(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002539 if (inet_csk(sk)->icsk_ca_state != TCP_CA_CWR) {
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07002540 tcp_try_keep_open(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 }
2542}
2543
John Heffner5d424d52006-03-20 17:53:41 -08002544static void tcp_mtup_probe_failed(struct sock *sk)
2545{
2546 struct inet_connection_sock *icsk = inet_csk(sk);
2547
2548 icsk->icsk_mtup.search_high = icsk->icsk_mtup.probe_size - 1;
2549 icsk->icsk_mtup.probe_size = 0;
Eric Dumazetc10d9312016-04-29 14:16:47 -07002550 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMTUPFAIL);
John Heffner5d424d52006-03-20 17:53:41 -08002551}
2552
Ilpo Järvinen72211e92009-03-14 14:23:04 +00002553static void tcp_mtup_probe_success(struct sock *sk)
John Heffner5d424d52006-03-20 17:53:41 -08002554{
2555 struct tcp_sock *tp = tcp_sk(sk);
2556 struct inet_connection_sock *icsk = inet_csk(sk);
2557
2558 /* FIXME: breaks with very large cwnd */
2559 tp->prior_ssthresh = tcp_current_ssthresh(sk);
2560 tp->snd_cwnd = tp->snd_cwnd *
2561 tcp_mss_to_mtu(sk, tp->mss_cache) /
2562 icsk->icsk_mtup.probe_size;
2563 tp->snd_cwnd_cnt = 0;
Eric Dumazetc2203cf2017-05-16 14:00:04 -07002564 tp->snd_cwnd_stamp = tcp_jiffies32;
John Heffner9c6d5e52010-10-06 21:18:02 -07002565 tp->snd_ssthresh = tcp_current_ssthresh(sk);
John Heffner5d424d52006-03-20 17:53:41 -08002566
2567 icsk->icsk_mtup.search_low = icsk->icsk_mtup.probe_size;
2568 icsk->icsk_mtup.probe_size = 0;
2569 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetc10d9312016-04-29 14:16:47 -07002570 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMTUPSUCCESS);
John Heffner5d424d52006-03-20 17:53:41 -08002571}
2572
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002573/* Do a simple retransmit without using the backoff mechanisms in
2574 * tcp_timer. This is used for path mtu discovery.
2575 * The socket is already locked here.
2576 */
2577void tcp_simple_retransmit(struct sock *sk)
2578{
2579 const struct inet_connection_sock *icsk = inet_csk(sk);
2580 struct tcp_sock *tp = tcp_sk(sk);
2581 struct sk_buff *skb;
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00002582 unsigned int mss = tcp_current_mss(sk);
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002583
Eric Dumazet75c119a2017-10-05 22:21:27 -07002584 skb_rbtree_walk(skb, &sk->tcp_rtx_queue) {
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08002585 if (tcp_skb_seglen(skb) > mss &&
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002586 !(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) {
2587 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) {
2588 TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
2589 tp->retrans_out -= tcp_skb_pcount(skb);
2590 }
2591 tcp_skb_mark_lost_uncond_verify(tp, skb);
2592 }
2593 }
2594
2595 tcp_clear_retrans_hints_partial(tp);
2596
Yuchung Cheng0eb96bf2017-11-07 15:33:43 -08002597 if (!tp->lost_out)
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002598 return;
2599
2600 if (tcp_is_reno(tp))
2601 tcp_limit_reno_sacked(tp);
2602
2603 tcp_verify_left_out(tp);
2604
2605 /* Don't muck with the congestion window here.
2606 * Reason is that we do not increase amount of _data_
2607 * in network, but units changed and effective
2608 * cwnd/ssthresh really reduced now.
2609 */
2610 if (icsk->icsk_ca_state != TCP_CA_Loss) {
2611 tp->high_seq = tp->snd_nxt;
2612 tp->snd_ssthresh = tcp_current_ssthresh(sk);
2613 tp->prior_ssthresh = 0;
2614 tp->undo_marker = 0;
2615 tcp_set_ca_state(sk, TCP_CA_Loss);
2616 }
2617 tcp_xmit_retransmit_queue(sk);
2618}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002619EXPORT_SYMBOL(tcp_simple_retransmit);
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002620
Yuchung Cheng57dde7f2017-01-12 22:11:33 -08002621void tcp_enter_recovery(struct sock *sk, bool ece_ack)
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002622{
2623 struct tcp_sock *tp = tcp_sk(sk);
2624 int mib_idx;
2625
2626 if (tcp_is_reno(tp))
2627 mib_idx = LINUX_MIB_TCPRENORECOVERY;
2628 else
2629 mib_idx = LINUX_MIB_TCPSACKRECOVERY;
2630
Eric Dumazetc10d9312016-04-29 14:16:47 -07002631 NET_INC_STATS(sock_net(sk), mib_idx);
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002632
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002633 tp->prior_ssthresh = 0;
Yuchung Cheng989e04c2014-08-22 14:15:22 -07002634 tcp_init_undo(tp);
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002635
Yuchung Cheng291a00d2015-07-01 14:11:14 -07002636 if (!tcp_in_cwnd_reduction(sk)) {
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002637 if (!ece_ack)
2638 tp->prior_ssthresh = tcp_current_ssthresh(sk);
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002639 tcp_init_cwnd_reduction(sk);
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002640 }
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002641 tcp_set_ca_state(sk, TCP_CA_Recovery);
2642}
2643
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002644/* Process an ACK in CA_Loss state. Move to CA_Open if lost data are
2645 * recovered or spurious. Otherwise retransmits more on partial ACKs.
2646 */
Yuchung Chenge662ca42016-02-02 10:33:04 -08002647static void tcp_process_loss(struct sock *sk, int flag, bool is_dupack,
2648 int *rexmit)
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002649{
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002650 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Chenge33099f2013-03-20 13:33:00 +00002651 bool recovered = !before(tp->snd_una, tp->high_seq);
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002652
Yuchung Chengda34ac72015-05-18 12:31:44 -07002653 if ((flag & FLAG_SND_UNA_ADVANCED) &&
2654 tcp_try_undo_loss(sk, false))
2655 return;
2656
Yuchung Cheng89fe18e2017-01-12 22:11:37 -08002657 if (tp->frto) { /* F-RTO RFC5682 sec 3.1 (sack enhanced version). */
Yuchung Chengfc68e172018-02-27 14:15:02 -08002658 /* Step 3.b. A timeout is spurious if not all data are
2659 * lost, i.e., never-retransmitted data are (s)acked.
2660 */
2661 if ((flag & FLAG_ORIG_SACK_ACKED) &&
2662 tcp_try_undo_loss(sk, true))
2663 return;
2664
Yuchung Chengb7b0ed92015-05-18 12:31:45 -07002665 if (after(tp->snd_nxt, tp->high_seq)) {
2666 if (flag & FLAG_DATA_SACKED || is_dupack)
2667 tp->frto = 0; /* Step 3.a. loss was real */
Yuchung Chenge33099f2013-03-20 13:33:00 +00002668 } else if (flag & FLAG_SND_UNA_ADVANCED && !recovered) {
2669 tp->high_seq = tp->snd_nxt;
Yuchung Chenge662ca42016-02-02 10:33:04 -08002670 /* Step 2.b. Try send new data (but deferred until cwnd
2671 * is updated in tcp_ack()). Otherwise fall back to
2672 * the conventional recovery.
2673 */
Eric Dumazet75c119a2017-10-05 22:21:27 -07002674 if (!tcp_write_queue_empty(sk) &&
Yuchung Chenge662ca42016-02-02 10:33:04 -08002675 after(tcp_wnd_end(tp), tp->snd_nxt)) {
2676 *rexmit = REXMIT_NEW;
2677 return;
2678 }
Yuchung Chenge33099f2013-03-20 13:33:00 +00002679 tp->frto = 0;
2680 }
2681 }
2682
2683 if (recovered) {
2684 /* F-RTO RFC5682 sec 3.1 step 2.a and 1st part of step 3.a */
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002685 tcp_try_undo_recovery(sk);
2686 return;
2687 }
Yuchung Chenge33099f2013-03-20 13:33:00 +00002688 if (tcp_is_reno(tp)) {
2689 /* A Reno DUPACK means new data in F-RTO step 2.b above are
2690 * delivered. Lower inflight to clock out (re)tranmissions.
2691 */
2692 if (after(tp->snd_nxt, tp->high_seq) && is_dupack)
2693 tcp_add_reno_sack(sk);
2694 else if (flag & FLAG_SND_UNA_ADVANCED)
2695 tcp_reset_reno_sack(tp);
2696 }
Yuchung Chenge662ca42016-02-02 10:33:04 -08002697 *rexmit = REXMIT_LOST;
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002698}
2699
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002700/* Undo during fast recovery after partial ACK. */
Yuchung Cheng737ff312017-11-08 13:01:27 -08002701static bool tcp_try_undo_partial(struct sock *sk, u32 prior_snd_una)
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002702{
2703 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002704
Yuchung Cheng7026b912013-05-29 14:20:13 +00002705 if (tp->undo_marker && tcp_packet_delayed(tp)) {
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002706 /* Plain luck! Hole if filled with delayed
Yuchung Cheng737ff312017-11-08 13:01:27 -08002707 * packet, rather than with a retransmit. Check reordering.
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002708 */
Yuchung Cheng737ff312017-11-08 13:01:27 -08002709 tcp_check_sack_reordering(sk, prior_snd_una, 1);
Yuchung Cheng7026b912013-05-29 14:20:13 +00002710
2711 /* We are getting evidence that the reordering degree is higher
2712 * than we realized. If there are no retransmits out then we
2713 * can undo. Otherwise we clock out new packets but do not
2714 * mark more packets lost or retransmit more.
2715 */
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002716 if (tp->retrans_out)
Yuchung Cheng7026b912013-05-29 14:20:13 +00002717 return true;
Yuchung Cheng7026b912013-05-29 14:20:13 +00002718
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002719 if (!tcp_any_retrans_done(sk))
2720 tp->retrans_stamp = 0;
2721
Yuchung Cheng7026b912013-05-29 14:20:13 +00002722 DBGUNDO(sk, "partial recovery");
2723 tcp_undo_cwnd_reduction(sk, true);
Eric Dumazetc10d9312016-04-29 14:16:47 -07002724 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPARTIALUNDO);
Yuchung Cheng7026b912013-05-29 14:20:13 +00002725 tcp_try_keep_open(sk);
2726 return true;
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002727 }
Yuchung Cheng7026b912013-05-29 14:20:13 +00002728 return false;
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002729}
2730
Yuchung Cheng6ac06ec2018-05-16 16:40:12 -07002731static void tcp_identify_packet_loss(struct sock *sk, int *ack_flag)
Yuchung Cheng98e36d42017-01-12 22:11:35 -08002732{
2733 struct tcp_sock *tp = tcp_sk(sk);
2734
Yuchung Cheng6ac06ec2018-05-16 16:40:12 -07002735 if (tcp_rtx_queue_empty(sk))
2736 return;
2737
2738 if (unlikely(tcp_is_reno(tp))) {
2739 tcp_newreno_mark_lost(sk, *ack_flag & FLAG_SND_UNA_ADVANCED);
2740 } else if (tcp_is_rack(sk)) {
Yuchung Cheng98e36d42017-01-12 22:11:35 -08002741 u32 prior_retrans = tp->retrans_out;
2742
Eric Dumazet128eda82017-04-25 10:15:34 -07002743 tcp_rack_mark_lost(sk);
Yuchung Cheng98e36d42017-01-12 22:11:35 -08002744 if (prior_retrans > tp->retrans_out)
2745 *ack_flag |= FLAG_LOST_RETRANS;
2746 }
2747}
2748
Yuchung Cheng737ff312017-11-08 13:01:27 -08002749static bool tcp_force_fast_retransmit(struct sock *sk)
2750{
2751 struct tcp_sock *tp = tcp_sk(sk);
2752
2753 return after(tcp_highest_sack_seq(tp),
2754 tp->snd_una + tp->reordering * tp->mss_cache);
2755}
2756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757/* Process an event, which can update packets-in-flight not trivially.
2758 * Main goal of this function is to calculate new estimate for left_out,
2759 * taking into account both packets sitting in receiver's buffer and
2760 * packets lost by network.
2761 *
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002762 * Besides that it updates the congestion state when packet loss or ECN
2763 * is detected. But it does not reduce the cwnd, it is done by the
2764 * congestion control later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 *
2766 * It does _not_ decide what to send, it is made in function
2767 * tcp_xmit_retransmit_queue().
2768 */
Yuchung Cheng737ff312017-11-08 13:01:27 -08002769static void tcp_fastretrans_alert(struct sock *sk, const u32 prior_snd_una,
Eric Dumazet1317a9d2017-04-25 10:15:36 -07002770 bool is_dupack, int *ack_flag, int *rexmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002772 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002774 int fast_rexmit = 0, flag = *ack_flag;
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002775 bool do_lost = is_dupack || ((flag & FLAG_DATA_SACKED) &&
Yuchung Cheng737ff312017-11-08 13:01:27 -08002776 tcp_force_fast_retransmit(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Eric Dumazet8ba6dda2017-10-05 22:21:25 -07002778 if (!tp->packets_out && tp->sacked_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 tp->sacked_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002781 /* Now state machine starts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 * A. ECE, hence prohibit cwnd undoing, the reduction is required. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002783 if (flag & FLAG_ECE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 tp->prior_ssthresh = 0;
2785
2786 /* B. In all the states check for reneging SACKs. */
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002787 if (tcp_check_sack_reneging(sk, flag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return;
2789
Yuchung Cheng974c1232012-01-19 14:42:21 +00002790 /* C. Check consistency of the current state. */
Ilpo Järvinen005903b2007-08-09 14:44:16 +03002791 tcp_verify_left_out(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Yuchung Cheng974c1232012-01-19 14:42:21 +00002793 /* D. Check state exit conditions. State can be terminated
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 * when high_seq is ACKed. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002795 if (icsk->icsk_ca_state == TCP_CA_Open) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002796 WARN_ON(tp->retrans_out != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 tp->retrans_stamp = 0;
2798 } else if (!before(tp->snd_una, tp->high_seq)) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002799 switch (icsk->icsk_ca_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 case TCP_CA_CWR:
2801 /* CWR is to be held something *above* high_seq
2802 * is ACKed for CWR bit to reach receiver. */
2803 if (tp->snd_una != tp->high_seq) {
Yuchung Cheng684bad12012-09-02 17:38:04 +00002804 tcp_end_cwnd_reduction(sk);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002805 tcp_set_ca_state(sk, TCP_CA_Open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
2807 break;
2808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 case TCP_CA_Recovery:
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002810 if (tcp_is_reno(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 tcp_reset_reno_sack(tp);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002812 if (tcp_try_undo_recovery(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return;
Yuchung Cheng684bad12012-09-02 17:38:04 +00002814 tcp_end_cwnd_reduction(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 break;
2816 }
2817 }
2818
Yuchung Cheng974c1232012-01-19 14:42:21 +00002819 /* E. Process state. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002820 switch (icsk->icsk_ca_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 case TCP_CA_Recovery:
Ilpo Järvinen2e605292007-08-02 19:46:58 -07002822 if (!(flag & FLAG_SND_UNA_ADVANCED)) {
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002823 if (tcp_is_reno(tp) && is_dupack)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002824 tcp_add_reno_sack(sk);
Yuchung Cheng7026b912013-05-29 14:20:13 +00002825 } else {
Yuchung Cheng737ff312017-11-08 13:01:27 -08002826 if (tcp_try_undo_partial(sk, prior_snd_una))
Yuchung Cheng7026b912013-05-29 14:20:13 +00002827 return;
2828 /* Partial ACK arrived. Force fast retransmit. */
2829 do_lost = tcp_is_reno(tp) ||
Yuchung Cheng737ff312017-11-08 13:01:27 -08002830 tcp_force_fast_retransmit(sk);
Yuchung Cheng7026b912013-05-29 14:20:13 +00002831 }
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002832 if (tcp_try_undo_dsack(sk)) {
2833 tcp_try_keep_open(sk);
2834 return;
2835 }
Yuchung Cheng6ac06ec2018-05-16 16:40:12 -07002836 tcp_identify_packet_loss(sk, ack_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 break;
2838 case TCP_CA_Loss:
Yuchung Chenge662ca42016-02-02 10:33:04 -08002839 tcp_process_loss(sk, flag, is_dupack, rexmit);
Yuchung Cheng6ac06ec2018-05-16 16:40:12 -07002840 tcp_identify_packet_loss(sk, ack_flag);
Yuchung Cheng98e36d42017-01-12 22:11:35 -08002841 if (!(icsk->icsk_ca_state == TCP_CA_Open ||
2842 (*ack_flag & FLAG_LOST_RETRANS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return;
Yuchung Cheng291a00d2015-07-01 14:11:14 -07002844 /* Change state if cwnd is undone or retransmits are lost */
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05002845 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 default:
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002847 if (tcp_is_reno(tp)) {
Ilpo Järvinen2e605292007-08-02 19:46:58 -07002848 if (flag & FLAG_SND_UNA_ADVANCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 tcp_reset_reno_sack(tp);
2850 if (is_dupack)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002851 tcp_add_reno_sack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
2853
Neal Cardwellf6982042011-11-16 08:58:04 +00002854 if (icsk->icsk_ca_state <= TCP_CA_Disorder)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002855 tcp_try_undo_dsack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Yuchung Cheng6ac06ec2018-05-16 16:40:12 -07002857 tcp_identify_packet_loss(sk, ack_flag);
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002858 if (!tcp_time_to_recover(sk, flag)) {
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002859 tcp_try_to_open(sk, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return;
2861 }
2862
John Heffner5d424d52006-03-20 17:53:41 -08002863 /* MTU probe failure: don't reduce cwnd */
2864 if (icsk->icsk_ca_state < TCP_CA_CWR &&
2865 icsk->icsk_mtup.probe_size &&
John Heffner0e7b13682006-03-20 21:32:58 -08002866 tp->snd_una == tp->mtu_probe.probe_seq_start) {
John Heffner5d424d52006-03-20 17:53:41 -08002867 tcp_mtup_probe_failed(sk);
2868 /* Restores the reduction we did in tcp_mtup_probe() */
2869 tp->snd_cwnd++;
2870 tcp_simple_retransmit(sk);
2871 return;
2872 }
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 /* Otherwise enter Recovery state */
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002875 tcp_enter_recovery(sk, (flag & FLAG_ECE));
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002876 fast_rexmit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
2878
Yuchung Chengb38a51f2018-05-16 16:40:11 -07002879 if (!tcp_is_rack(sk) && do_lost)
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002880 tcp_update_scoreboard(sk, fast_rexmit);
Yuchung Chenge662ca42016-02-02 10:33:04 -08002881 *rexmit = REXMIT_LOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882}
2883
Yuchung Chengeb36be02018-01-17 12:11:00 -08002884static void tcp_update_rtt_min(struct sock *sk, u32 rtt_us, const int flag)
Yuchung Chengf6722582015-10-16 21:57:42 -07002885{
Eric Dumazetbd239702017-10-27 07:47:28 -07002886 u32 wlen = sock_net(sk)->ipv4.sysctl_tcp_min_rtt_wlen * HZ;
Neal Cardwell64033892016-09-19 23:39:10 -04002887 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Chengf6722582015-10-16 21:57:42 -07002888
Yuchung Chengeb36be02018-01-17 12:11:00 -08002889 if ((flag & FLAG_ACK_MAYBE_DELAYED) && rtt_us > tcp_min_rtt(tp)) {
2890 /* If the remote keeps returning delayed ACKs, eventually
2891 * the min filter would pick it up and overestimate the
2892 * prop. delay when it expires. Skip suspected delayed ACKs.
2893 */
2894 return;
2895 }
Eric Dumazetac9517f2017-05-16 14:00:13 -07002896 minmax_running_min(&tp->rtt_min, wlen, tcp_jiffies32,
Neal Cardwell64033892016-09-19 23:39:10 -04002897 rtt_us ? : jiffies_to_usecs(1));
Yuchung Chengf6722582015-10-16 21:57:42 -07002898}
2899
Yuchung Cheng775e68a2017-05-31 11:30:53 -07002900static bool tcp_ack_update_rtt(struct sock *sk, const int flag,
2901 long seq_rtt_us, long sack_rtt_us,
2902 long ca_rtt_us, struct rate_sample *rs)
Ilpo Järvinen41834b72008-12-05 22:43:26 -08002903{
Yuchung Cheng5b08e472013-07-22 16:20:46 -07002904 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen41834b72008-12-05 22:43:26 -08002905
Yuchung Cheng5b08e472013-07-22 16:20:46 -07002906 /* Prefer RTT measured from ACK's timing to TS-ECR. This is because
2907 * broken middle-boxes or peers may corrupt TS-ECR fields. But
2908 * Karn's algorithm forbids taking RTT if some retransmitted data
2909 * is acked (RFC6298).
2910 */
Eric Dumazet740b0f12014-02-26 14:02:48 -08002911 if (seq_rtt_us < 0)
2912 seq_rtt_us = sack_rtt_us;
Yuchung Chenged084952013-07-22 16:20:48 -07002913
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 /* RTTM Rule: A TSecr value received in a segment is used to
2915 * update the averaged RTT measurement only if the segment
2916 * acknowledges some new data, i.e., only if it advances the
2917 * left edge of the send window.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 * See draft-ietf-tcplw-high-performance-00, section 3.3.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 */
Eric Dumazet740b0f12014-02-26 14:02:48 -08002920 if (seq_rtt_us < 0 && tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
Eric Dumazet9a568de2017-05-16 14:00:14 -07002921 flag & FLAG_ACKED) {
2922 u32 delta = tcp_time_stamp(tp) - tp->rx_opt.rcv_tsecr;
2923 u32 delta_us = delta * (USEC_PER_SEC / TCP_TS_HZ);
2924
2925 seq_rtt_us = ca_rtt_us = delta_us;
2926 }
Yuchung Cheng775e68a2017-05-31 11:30:53 -07002927 rs->rtt_us = ca_rtt_us; /* RTT of last (S)ACKed packet (or -1) */
Eric Dumazet740b0f12014-02-26 14:02:48 -08002928 if (seq_rtt_us < 0)
Yuchung Chenged084952013-07-22 16:20:48 -07002929 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Yuchung Chengf6722582015-10-16 21:57:42 -07002931 /* ca_rtt_us >= 0 is counting on the invariant that ca_rtt_us is
2932 * always taken together with ACK, SACK, or TS-opts. Any negative
2933 * values will be skipped with the seq_rtt_us < 0 check above.
2934 */
Yuchung Chengeb36be02018-01-17 12:11:00 -08002935 tcp_update_rtt_min(sk, ca_rtt_us, flag);
Eric Dumazet740b0f12014-02-26 14:02:48 -08002936 tcp_rtt_estimator(sk, seq_rtt_us);
Yuchung Cheng5b08e472013-07-22 16:20:46 -07002937 tcp_set_rto(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Yuchung Cheng5b08e472013-07-22 16:20:46 -07002939 /* RFC6298: only reset backoff on valid RTT measurement. */
2940 inet_csk(sk)->icsk_backoff = 0;
Yuchung Chenged084952013-07-22 16:20:48 -07002941 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
2943
Yuchung Cheng375fe022013-07-22 16:20:45 -07002944/* Compute time elapsed between (last) SYNACK and the ACK completing 3WHS. */
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07002945void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req)
Yuchung Cheng375fe022013-07-22 16:20:45 -07002946{
Yuchung Cheng775e68a2017-05-31 11:30:53 -07002947 struct rate_sample rs;
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07002948 long rtt_us = -1L;
Yuchung Cheng375fe022013-07-22 16:20:45 -07002949
Eric Dumazet9a568de2017-05-16 14:00:14 -07002950 if (req && !req->num_retrans && tcp_rsk(req)->snt_synack)
2951 rtt_us = tcp_stamp_us_delta(tcp_clock_us(), tcp_rsk(req)->snt_synack);
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07002952
Yuchung Cheng775e68a2017-05-31 11:30:53 -07002953 tcp_ack_update_rtt(sk, FLAG_SYN_ACKED, rtt_us, -1L, rtt_us, &rs);
Yuchung Cheng375fe022013-07-22 16:20:45 -07002954}
2955
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07002956
Eric Dumazet24901552014-05-02 21:18:05 -07002957static void tcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002959 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet24901552014-05-02 21:18:05 -07002960
2961 icsk->icsk_ca_ops->cong_avoid(sk, ack, acked);
Eric Dumazetc2203cf2017-05-16 14:00:04 -07002962 tcp_sk(sk)->snd_cwnd_stamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963}
2964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965/* Restart timer after forward progress on connection.
2966 * RFC2988 recommends to restart timer to now+rto.
2967 */
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002968void tcp_rearm_rto(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00002970 const struct inet_connection_sock *icsk = inet_csk(sk);
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002971 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002972
Jerry Chu168a8f52012-08-31 12:29:13 +00002973 /* If the retrans timer is currently being used by Fast Open
2974 * for SYN-ACK retrans purpose, stay put.
2975 */
2976 if (tp->fastopen_rsk)
2977 return;
2978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 if (!tp->packets_out) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002980 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 } else {
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002982 u32 rto = inet_csk(sk)->icsk_rto;
2983 /* Offset the time elapsed after installing regular RTO */
Yuchung Chengbec41a12017-01-12 22:11:39 -08002984 if (icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00002985 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
Neal Cardwelle1a10ef2017-08-03 09:19:52 -04002986 s64 delta_us = tcp_rto_delta_us(sk);
Eric Dumazetb17b8a22017-05-18 09:15:58 -07002987 /* delta_us may not be positive if the socket is locked
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00002988 * when the retrans timer fires and is rescheduled.
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002989 */
Neal Cardwellcdbeb632017-08-16 17:53:36 -04002990 rto = usecs_to_jiffies(max_t(int, delta_us, 1));
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002991 }
2992 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, rto,
2993 TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002995}
2996
Neal Cardwelldf92c832017-08-03 09:19:54 -04002997/* Try to schedule a loss probe; if that doesn't work, then schedule an RTO. */
2998static void tcp_set_xmit_timer(struct sock *sk)
2999{
Neal Cardwelled66dfa2017-11-17 21:06:14 -05003000 if (!tcp_schedule_loss_probe(sk, true))
Neal Cardwelldf92c832017-08-03 09:19:54 -04003001 tcp_rearm_rto(sk);
3002}
3003
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003004/* If we get here, the whole TSO packet has not been acked. */
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003005static u32 tcp_tso_acked(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
3007 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003008 u32 packets_acked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003010 BUG_ON(!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
3012 packets_acked = tcp_skb_pcount(skb);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003013 if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 return 0;
3015 packets_acked -= tcp_skb_pcount(skb);
3016
3017 if (packets_acked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 BUG_ON(tcp_skb_pcount(skb) == 0);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003019 BUG_ON(!before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
3021
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003022 return packets_acked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
3024
Eric Dumazetad971f62014-10-11 15:17:29 -07003025static void tcp_ack_tstamp(struct sock *sk, struct sk_buff *skb,
3026 u32 prior_snd_una)
3027{
3028 const struct skb_shared_info *shinfo;
3029
3030 /* Avoid cache line misses to get skb_shinfo() and shinfo->tx_flags */
Soheil Hassas Yeganeh6b084922016-04-02 23:08:08 -04003031 if (likely(!TCP_SKB_CB(skb)->txstamp_ack))
Eric Dumazetad971f62014-10-11 15:17:29 -07003032 return;
3033
3034 shinfo = skb_shinfo(skb);
Soheil Hassas Yeganeh0a2cf202016-04-27 23:39:01 -04003035 if (!before(shinfo->tskey, prior_snd_una) &&
Eric Dumazete2080072017-10-04 12:59:58 -07003036 before(shinfo->tskey, tcp_sk(sk)->snd_una)) {
3037 tcp_skb_tsorted_save(skb) {
3038 __skb_tstamp_tx(skb, NULL, sk, SCM_TSTAMP_ACK);
3039 } tcp_skb_tsorted_restore(skb);
3040 }
Eric Dumazetad971f62014-10-11 15:17:29 -07003041}
3042
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003043/* Remove acknowledged frames from the retransmission queue. If our packet
3044 * is before the ack sequence we can discard it as it's confirmed to have
3045 * arrived at the other end.
3046 */
Yuchung Cheng737ff312017-11-08 13:01:27 -08003047static int tcp_clean_rtx_queue(struct sock *sk, u32 prior_fack,
3048 u32 prior_snd_una,
Yuchung Chengdeed7be2017-01-12 22:11:32 -08003049 struct tcp_sacktag_state *sack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Stephen Hemminger2d2abba2005-11-10 16:56:12 -08003051 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet9a568de2017-05-16 14:00:14 -07003052 u64 first_ackt, last_ackt;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003053 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen90638a02008-09-20 21:25:52 -07003054 u32 prior_sacked = tp->sacked_out;
Yuchung Cheng737ff312017-11-08 13:01:27 -08003055 u32 reord = tp->snd_nxt; /* lowest acked un-retx un-sacked seq */
Eric Dumazet75c119a2017-10-05 22:21:27 -07003056 struct sk_buff *skb, *next;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003057 bool fully_acked = true;
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02003058 long sack_rtt_us = -1L;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003059 long seq_rtt_us = -1L;
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02003060 long ca_rtt_us = -1L;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003061 u32 pkts_acked = 0;
Lawrence Brakmo6f094b92016-06-08 21:16:44 -07003062 u32 last_in_flight = 0;
Yuchung Cheng2f715c12013-10-24 08:59:27 -07003063 bool rtt_update;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003064 int flag = 0;
3065
Eric Dumazet9a568de2017-05-16 14:00:14 -07003066 first_ackt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
Eric Dumazet75c119a2017-10-05 22:21:27 -07003068 for (skb = skb_rb_first(&sk->tcp_rtx_queue); skb; skb = next) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003069 struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
Yuchung Cheng737ff312017-11-08 13:01:27 -08003070 const u32 start_seq = scb->seq;
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003071 u8 sacked = scb->sacked;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003072 u32 acked_pcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Eric Dumazetad971f62014-10-11 15:17:29 -07003074 tcp_ack_tstamp(sk, skb, prior_snd_una);
Willem de Bruijn712a7222014-08-12 14:53:16 -04003075
Gavin McCullagh2072c222007-12-29 19:11:21 -08003076 /* Determine how many packets and what bytes were acked, tso and else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 if (after(scb->end_seq, tp->snd_una)) {
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003078 if (tcp_skb_pcount(skb) == 1 ||
3079 !after(tp->snd_una, scb->seq))
3080 break;
3081
Ilpo Järvinen72018832007-12-30 04:37:55 -08003082 acked_pcount = tcp_tso_acked(sk, skb);
3083 if (!acked_pcount)
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003084 break;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003085 fully_acked = false;
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003086 } else {
Ilpo Järvinen72018832007-12-30 04:37:55 -08003087 acked_pcount = tcp_skb_pcount(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 }
3089
Eric Dumazetad971f62014-10-11 15:17:29 -07003090 if (unlikely(sacked & TCPCB_RETRANS)) {
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003091 if (sacked & TCPCB_SACKED_RETRANS)
Ilpo Järvinen72018832007-12-30 04:37:55 -08003092 tp->retrans_out -= acked_pcount;
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003093 flag |= FLAG_RETRANS_DATA_ACKED;
Kenneth Klette Jonassen3d0d26c2015-04-11 02:17:49 +02003094 } else if (!(sacked & TCPCB_SACKED_ACKED)) {
Eric Dumazet740b0f12014-02-26 14:02:48 -08003095 last_ackt = skb->skb_mstamp;
Eric Dumazet9a568de2017-05-16 14:00:14 -07003096 WARN_ON_ONCE(last_ackt == 0);
3097 if (!first_ackt)
Eric Dumazet740b0f12014-02-26 14:02:48 -08003098 first_ackt = last_ackt;
3099
Lawrence Brakmo6f094b92016-06-08 21:16:44 -07003100 last_in_flight = TCP_SKB_CB(skb)->tx.in_flight;
Yuchung Cheng737ff312017-11-08 13:01:27 -08003101 if (before(start_seq, reord))
3102 reord = start_seq;
Kenneth Klette Jonassen3d0d26c2015-04-11 02:17:49 +02003103 if (!after(scb->end_seq, tp->high_seq))
3104 flag |= FLAG_ORIG_SACK_ACKED;
Stephen Hemminger2d2abba2005-11-10 16:56:12 -08003105 }
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003106
Yuchung Chengddf1af62016-02-02 10:33:06 -08003107 if (sacked & TCPCB_SACKED_ACKED) {
Ilpo Järvinen72018832007-12-30 04:37:55 -08003108 tp->sacked_out -= acked_pcount;
Yuchung Chengddf1af62016-02-02 10:33:06 -08003109 } else if (tcp_is_sack(tp)) {
3110 tp->delivered += acked_pcount;
3111 if (!tcp_skb_spurious_retrans(tp, skb))
Yuchung Cheng1d0833d2017-01-12 22:11:34 -08003112 tcp_rack_advance(tp, sacked, scb->end_seq,
Eric Dumazet9a568de2017-05-16 14:00:14 -07003113 skb->skb_mstamp);
Yuchung Chengddf1af62016-02-02 10:33:06 -08003114 }
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003115 if (sacked & TCPCB_LOST)
Ilpo Järvinen72018832007-12-30 04:37:55 -08003116 tp->lost_out -= acked_pcount;
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003117
Ilpo Järvinen72018832007-12-30 04:37:55 -08003118 tp->packets_out -= acked_pcount;
3119 pkts_acked += acked_pcount;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003120 tcp_rate_skb_delivered(sk, skb, sack->rate);
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003121
Ilpo Järvinen009a2e32007-09-20 11:34:38 -07003122 /* Initial outgoing SYN's get put onto the write_queue
3123 * just like anything else we transmit. It is not
3124 * true data, and if we misinform our callers that
3125 * this ACK acks real data, we will erroneously exit
3126 * connection startup slow start one packet too
3127 * quickly. This is severely frowned upon behavior.
3128 */
Eric Dumazetad971f62014-10-11 15:17:29 -07003129 if (likely(!(scb->tcp_flags & TCPHDR_SYN))) {
Ilpo Järvinen009a2e32007-09-20 11:34:38 -07003130 flag |= FLAG_DATA_ACKED;
3131 } else {
3132 flag |= FLAG_SYN_ACKED;
3133 tp->retrans_stamp = 0;
3134 }
3135
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003136 if (!fully_acked)
3137 break;
3138
Eric Dumazet75c119a2017-10-05 22:21:27 -07003139 next = skb_rb_next(skb);
Eric Dumazetad971f62014-10-11 15:17:29 -07003140 if (unlikely(skb == tp->retransmit_skb_hint))
Ilpo Järvinenef9da472008-09-20 21:25:15 -07003141 tp->retransmit_skb_hint = NULL;
Eric Dumazetad971f62014-10-11 15:17:29 -07003142 if (unlikely(skb == tp->lost_skb_hint))
Ilpo Järvinen90638a02008-09-20 21:25:52 -07003143 tp->lost_skb_hint = NULL;
Eric Dumazet75c119a2017-10-05 22:21:27 -07003144 tcp_rtx_queue_unlink_and_free(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 }
3146
Francis Yan0f872302016-11-27 23:07:14 -08003147 if (!skb)
3148 tcp_chrono_stop(sk, TCP_CHRONO_BUSY);
3149
Ilpo Järvinen33f5f572008-10-07 14:43:06 -07003150 if (likely(between(tp->snd_up, prior_snd_una, tp->snd_una)))
3151 tp->snd_up = tp->snd_una;
3152
Ilpo Järvinencadbd032007-12-31 04:49:21 -08003153 if (skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
3154 flag |= FLAG_SACK_RENEGING;
3155
Eric Dumazet9a568de2017-05-16 14:00:14 -07003156 if (likely(first_ackt) && !(flag & FLAG_RETRANS_DATA_ACKED)) {
3157 seq_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, first_ackt);
3158 ca_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, last_ackt);
Yuchung Chengeb36be02018-01-17 12:11:00 -08003159
3160 if (pkts_acked == 1 && last_in_flight < tp->mss_cache &&
3161 last_in_flight && !prior_sacked && fully_acked &&
3162 sack->rate->prior_delivered + 1 == tp->delivered &&
3163 !(flag & (FLAG_CA_ALERT | FLAG_SYN_ACKED))) {
3164 /* Conservatively mark a delayed ACK. It's typically
3165 * from a lone runt packet over the round trip to
3166 * a receiver w/o out-of-order or CE events.
3167 */
3168 flag |= FLAG_ACK_MAYBE_DELAYED;
3169 }
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02003170 }
Eric Dumazet9a568de2017-05-16 14:00:14 -07003171 if (sack->first_sackt) {
3172 sack_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, sack->first_sackt);
3173 ca_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, sack->last_sackt);
Eric Dumazet740b0f12014-02-26 14:02:48 -08003174 }
Yuchung Chengf6722582015-10-16 21:57:42 -07003175 rtt_update = tcp_ack_update_rtt(sk, flag, seq_rtt_us, sack_rtt_us,
Yuchung Cheng775e68a2017-05-31 11:30:53 -07003176 ca_rtt_us, sack->rate);
Yuchung Chenged084952013-07-22 16:20:48 -07003177
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003178 if (flag & FLAG_ACKED) {
Neal Cardwelldf92c832017-08-03 09:19:54 -04003179 flag |= FLAG_SET_XMIT_TIMER; /* set TLP or RTO timer */
Ilpo Järvinen72211e92009-03-14 14:23:04 +00003180 if (unlikely(icsk->icsk_mtup.probe_size &&
3181 !after(tp->mtu_probe.probe_seq_end, tp->snd_una))) {
3182 tcp_mtup_probe_success(sk);
3183 }
3184
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003185 if (tcp_is_reno(tp)) {
3186 tcp_remove_reno_sacks(sk, pkts_acked);
3187 } else {
Ilpo Järvinen59a08cb2009-02-28 04:44:28 +00003188 int delta;
3189
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003190 /* Non-retransmitted hole got filled? That's reordering */
Yuchung Cheng737ff312017-11-08 13:01:27 -08003191 if (before(reord, prior_fack))
3192 tcp_check_sack_reordering(sk, reord, 0);
Ilpo Järvinen90638a02008-09-20 21:25:52 -07003193
Yuchung Cheng713bafe2017-11-08 13:01:26 -08003194 delta = prior_sacked - tp->sacked_out;
Ilpo Järvinen59a08cb2009-02-28 04:44:28 +00003195 tp->lost_cnt_hint -= min(tp->lost_cnt_hint, delta);
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003196 }
Eric Dumazet740b0f12014-02-26 14:02:48 -08003197 } else if (skb && rtt_update && sack_rtt_us >= 0 &&
Eric Dumazet9a568de2017-05-16 14:00:14 -07003198 sack_rtt_us > tcp_stamp_us_delta(tp->tcp_mstamp, skb->skb_mstamp)) {
Yuchung Cheng2f715c12013-10-24 08:59:27 -07003199 /* Do not re-arm RTO if the sack RTT is measured from data sent
3200 * after when the head was last (re)transmitted. Otherwise the
3201 * timeout may continue to extend in loss recovery.
3202 */
Neal Cardwelldf92c832017-08-03 09:19:54 -04003203 flag |= FLAG_SET_XMIT_TIMER; /* set TLP or RTO timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 }
3205
Lawrence Brakmo756ee172016-05-11 10:02:13 -07003206 if (icsk->icsk_ca_ops->pkts_acked) {
3207 struct ack_sample sample = { .pkts_acked = pkts_acked,
Yuchung Cheng775e68a2017-05-31 11:30:53 -07003208 .rtt_us = sack->rate->rtt_us,
Lawrence Brakmo6f094b92016-06-08 21:16:44 -07003209 .in_flight = last_in_flight };
Lawrence Brakmo756ee172016-05-11 10:02:13 -07003210
3211 icsk->icsk_ca_ops->pkts_acked(sk, &sample);
3212 }
Kenneth Klette Jonassen138998f2015-05-01 01:10:59 +02003213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214#if FASTRETRANS_DEBUG > 0
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003215 WARN_ON((int)tp->sacked_out < 0);
3216 WARN_ON((int)tp->lost_out < 0);
3217 WARN_ON((int)tp->retrans_out < 0);
Ilpo Järvinene60402d2007-08-09 15:14:46 +03003218 if (!tp->packets_out && tcp_is_sack(tp)) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07003219 icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (tp->lost_out) {
Joe Perches91df42b2012-05-15 14:11:54 +00003221 pr_debug("Leak l=%u %d\n",
3222 tp->lost_out, icsk->icsk_ca_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 tp->lost_out = 0;
3224 }
3225 if (tp->sacked_out) {
Joe Perches91df42b2012-05-15 14:11:54 +00003226 pr_debug("Leak s=%u %d\n",
3227 tp->sacked_out, icsk->icsk_ca_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 tp->sacked_out = 0;
3229 }
3230 if (tp->retrans_out) {
Joe Perches91df42b2012-05-15 14:11:54 +00003231 pr_debug("Leak r=%u %d\n",
3232 tp->retrans_out, icsk->icsk_ca_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 tp->retrans_out = 0;
3234 }
3235 }
3236#endif
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003237 return flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
3240static void tcp_ack_probe(struct sock *sk)
3241{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003242 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet75c119a2017-10-05 22:21:27 -07003243 struct sk_buff *head = tcp_send_head(sk);
3244 const struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
3246 /* Was it a usable window open? */
Eric Dumazet75c119a2017-10-05 22:21:27 -07003247 if (!head)
3248 return;
3249 if (!after(TCP_SKB_CB(head)->end_seq, tcp_wnd_end(tp))) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003250 icsk->icsk_backoff = 0;
3251 inet_csk_clear_xmit_timer(sk, ICSK_TIME_PROBE0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 /* Socket must be waked up by subsequent tcp_data_snd_check().
3253 * This function is not for random using!
3254 */
3255 } else {
Eric Dumazet21c8fe92015-05-06 14:26:24 -07003256 unsigned long when = tcp_probe0_when(sk, TCP_RTO_MAX);
Eric Dumazetfcdd1cf2014-09-22 13:19:44 -07003257
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003258 inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
Eric Dumazetfcdd1cf2014-09-22 13:19:44 -07003259 when, TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 }
3261}
3262
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00003263static inline bool tcp_ack_is_dubious(const struct sock *sk, const int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
Eric Dumazeta02cec22010-09-22 20:43:57 +00003265 return !(flag & FLAG_NOT_DUP) || (flag & FLAG_CA_ALERT) ||
3266 inet_csk(sk)->icsk_ca_state != TCP_CA_Open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267}
3268
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003269/* Decide wheather to run the increase function of congestion control. */
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00003270static inline bool tcp_may_raise_cwnd(const struct sock *sk, const int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003272 /* If reordering is high then always grow cwnd whenever data is
3273 * delivered regardless of its ordering. Otherwise stay conservative
Yuchung Cheng16edfe72013-09-05 15:36:09 -07003274 * and only grow cwnd on in-order delivery (RFC5681). A stretched ACK w/
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003275 * new SACK or ECE mark may first advance cwnd here and later reduce
3276 * cwnd in tcp_fastretrans_alert() based on more states.
3277 */
Nikolay Borisov1043e252016-02-03 09:46:52 +02003278 if (tcp_sk(sk)->reordering > sock_net(sk)->ipv4.sysctl_tcp_reordering)
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003279 return flag & FLAG_FORWARD_PROGRESS;
3280
Yuchung Cheng16edfe72013-09-05 15:36:09 -07003281 return flag & FLAG_DATA_ACKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282}
3283
Yuchung Chengd452e6c2016-02-02 10:33:09 -08003284/* The "ultimate" congestion control function that aims to replace the rigid
3285 * cwnd increase and decrease control (tcp_cong_avoid,tcp_*cwnd_reduction).
3286 * It's called toward the end of processing an ACK with precise rate
3287 * information. All transmission or retransmission are delayed afterwards.
3288 */
3289static void tcp_cong_control(struct sock *sk, u32 ack, u32 acked_sacked,
Yuchung Chengc0402762016-09-19 23:39:21 -04003290 int flag, const struct rate_sample *rs)
Yuchung Chengd452e6c2016-02-02 10:33:09 -08003291{
Yuchung Chengc0402762016-09-19 23:39:21 -04003292 const struct inet_connection_sock *icsk = inet_csk(sk);
3293
3294 if (icsk->icsk_ca_ops->cong_control) {
3295 icsk->icsk_ca_ops->cong_control(sk, rs);
3296 return;
3297 }
3298
Yuchung Chengd452e6c2016-02-02 10:33:09 -08003299 if (tcp_in_cwnd_reduction(sk)) {
3300 /* Reduce cwnd if state mandates */
3301 tcp_cwnd_reduction(sk, acked_sacked, flag);
3302 } else if (tcp_may_raise_cwnd(sk, flag)) {
3303 /* Advance cwnd if state allows */
3304 tcp_cong_avoid(sk, ack, acked_sacked);
3305 }
3306 tcp_update_pacing_rate(sk);
3307}
3308
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309/* Check that window update is acceptable.
3310 * The function assumes that snd_una<=ack<=snd_next.
3311 */
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00003312static inline bool tcp_may_update_window(const struct tcp_sock *tp,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08003313 const u32 ack, const u32 ack_seq,
3314 const u32 nwin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315{
Eric Dumazeta02cec22010-09-22 20:43:57 +00003316 return after(ack, tp->snd_una) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 after(ack_seq, tp->snd_wl1) ||
Eric Dumazeta02cec22010-09-22 20:43:57 +00003318 (ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319}
3320
Eric Dumazet0df48c22015-04-28 15:28:17 -07003321/* If we update tp->snd_una, also update tp->bytes_acked */
3322static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack)
3323{
3324 u32 delta = ack - tp->snd_una;
3325
Eric Dumazet46cc6e42016-05-03 16:56:03 -07003326 sock_owned_by_me((struct sock *)tp);
Eric Dumazet0df48c22015-04-28 15:28:17 -07003327 tp->bytes_acked += delta;
3328 tp->snd_una = ack;
3329}
3330
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07003331/* If we update tp->rcv_nxt, also update tp->bytes_received */
3332static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq)
3333{
3334 u32 delta = seq - tp->rcv_nxt;
3335
Eric Dumazet46cc6e42016-05-03 16:56:03 -07003336 sock_owned_by_me((struct sock *)tp);
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07003337 tp->bytes_received += delta;
3338 tp->rcv_nxt = seq;
3339}
3340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341/* Update our send window.
3342 *
3343 * Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2
3344 * and in FreeBSD. NetBSD's one is even worse.) is wrong.
3345 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003346static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 ack,
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07003347 u32 ack_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07003349 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 int flag = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07003351 u32 nwin = ntohs(tcp_hdr(skb)->window);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07003353 if (likely(!tcp_hdr(skb)->syn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 nwin <<= tp->rx_opt.snd_wscale;
3355
3356 if (tcp_may_update_window(tp, ack, ack_seq, nwin)) {
3357 flag |= FLAG_WIN_UPDATE;
Hantzis Fotisee7537b2009-03-02 22:42:02 -08003358 tcp_update_wl(tp, ack_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
3360 if (tp->snd_wnd != nwin) {
3361 tp->snd_wnd = nwin;
3362
Florian Westphal31770e32017-08-30 19:24:58 +02003363 /* Note, it is the only place, where
3364 * fast path is recovered for sending TCP.
3365 */
3366 tp->pred_flags = 0;
3367 tcp_fast_path_check(sk);
3368
Eric Dumazet75c119a2017-10-05 22:21:27 -07003369 if (!tcp_write_queue_empty(sk))
Eric Dumazet6f021c62015-08-21 12:30:00 -07003370 tcp_slow_start_after_idle_check(sk);
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 if (nwin > tp->max_window) {
3373 tp->max_window = nwin;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08003374 tcp_sync_mss(sk, inet_csk(sk)->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 }
3376 }
3377 }
3378
Eric Dumazet0df48c22015-04-28 15:28:17 -07003379 tcp_snd_una_update(tp, ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
3381 return flag;
3382}
3383
Jason Baron083ae302016-07-14 11:38:40 -04003384static bool __tcp_oow_rate_limited(struct net *net, int mib_idx,
3385 u32 *last_oow_ack_time)
3386{
3387 if (*last_oow_ack_time) {
Eric Dumazet594208a2017-05-16 14:00:10 -07003388 s32 elapsed = (s32)(tcp_jiffies32 - *last_oow_ack_time);
Jason Baron083ae302016-07-14 11:38:40 -04003389
Eric Dumazet4170ba62017-10-27 07:47:30 -07003390 if (0 <= elapsed && elapsed < net->ipv4.sysctl_tcp_invalid_ratelimit) {
Jason Baron083ae302016-07-14 11:38:40 -04003391 NET_INC_STATS(net, mib_idx);
3392 return true; /* rate-limited: don't send yet! */
3393 }
3394 }
3395
Eric Dumazet594208a2017-05-16 14:00:10 -07003396 *last_oow_ack_time = tcp_jiffies32;
Jason Baron083ae302016-07-14 11:38:40 -04003397
3398 return false; /* not rate-limited: go ahead, send dupack now! */
3399}
3400
Eric Dumazet7970ddc2015-03-16 21:06:20 -07003401/* Return true if we're currently rate-limiting out-of-window ACKs and
3402 * thus shouldn't send a dupack right now. We rate-limit dupacks in
3403 * response to out-of-window SYNs or ACKs to mitigate ACK loops or DoS
3404 * attacks that send repeated SYNs or ACKs for the same connection. To
3405 * do this, we do not send a duplicate SYNACK or ACK if the remote
3406 * endpoint is sending out-of-window SYNs or pure ACKs at a high rate.
3407 */
3408bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
3409 int mib_idx, u32 *last_oow_ack_time)
3410{
3411 /* Data packets without SYNs are not likely part of an ACK loop. */
3412 if ((TCP_SKB_CB(skb)->seq != TCP_SKB_CB(skb)->end_seq) &&
3413 !tcp_hdr(skb)->syn)
Jason Baron083ae302016-07-14 11:38:40 -04003414 return false;
Eric Dumazet7970ddc2015-03-16 21:06:20 -07003415
Jason Baron083ae302016-07-14 11:38:40 -04003416 return __tcp_oow_rate_limited(net, mib_idx, last_oow_ack_time);
Eric Dumazet7970ddc2015-03-16 21:06:20 -07003417}
3418
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003419/* RFC 5961 7 [ACK Throttling] */
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003420static void tcp_send_challenge_ack(struct sock *sk, const struct sk_buff *skb)
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003421{
3422 /* unprotected vars, we dont care of overwrites */
3423 static u32 challenge_timestamp;
3424 static unsigned int challenge_count;
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003425 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazetb530b682017-10-27 07:47:26 -07003426 struct net *net = sock_net(sk);
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003427 u32 count, now;
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003428
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003429 /* First check our per-socket dupack rate limit. */
Eric Dumazetb530b682017-10-27 07:47:26 -07003430 if (__tcp_oow_rate_limited(net,
Jason Baron083ae302016-07-14 11:38:40 -04003431 LINUX_MIB_TCPACKSKIPPEDCHALLENGE,
3432 &tp->last_oow_ack_time))
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003433 return;
3434
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003435 /* Then check host-wide RFC 5961 rate limit. */
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003436 now = jiffies / HZ;
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003437 if (now != challenge_timestamp) {
Eric Dumazetb530b682017-10-27 07:47:26 -07003438 u32 ack_limit = net->ipv4.sysctl_tcp_challenge_ack_limit;
3439 u32 half = (ack_limit + 1) >> 1;
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003440
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003441 challenge_timestamp = now;
Eric Dumazetb530b682017-10-27 07:47:26 -07003442 WRITE_ONCE(challenge_count, half + prandom_u32_max(ack_limit));
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003443 }
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003444 count = READ_ONCE(challenge_count);
3445 if (count > 0) {
3446 WRITE_ONCE(challenge_count, count - 1);
Eric Dumazetb530b682017-10-27 07:47:26 -07003447 NET_INC_STATS(net, LINUX_MIB_TCPCHALLENGEACK);
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003448 tcp_send_ack(sk);
3449 }
3450}
3451
Eric Dumazet12fb3dd2013-04-19 07:19:48 +00003452static void tcp_store_ts_recent(struct tcp_sock *tp)
3453{
3454 tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval;
3455 tp->rx_opt.ts_recent_stamp = get_seconds();
3456}
3457
3458static void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq)
3459{
3460 if (tp->rx_opt.saw_tstamp && !after(seq, tp->rcv_wup)) {
3461 /* PAWS bug workaround wrt. ACK frames, the PAWS discard
3462 * extra check below makes sure this can only happen
3463 * for pure ACK frames. -DaveM
3464 *
3465 * Not only, also it occurs for expired timestamps.
3466 */
3467
3468 if (tcp_paws_check(&tp->rx_opt, 0))
3469 tcp_store_ts_recent(tp);
3470 }
3471}
3472
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003473/* This routine deals with acks during a TLP episode.
Sébastien Barré08abdff2015-01-12 10:30:40 +01003474 * We mark the end of a TLP episode on receiving TLP dupack or when
3475 * ack is after tlp_high_seq.
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003476 * Ref: loss detection algorithm in draft-dukkipati-tcpm-tcp-loss-probe.
3477 */
3478static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
3479{
3480 struct tcp_sock *tp = tcp_sk(sk);
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003481
Sébastien Barré08abdff2015-01-12 10:30:40 +01003482 if (before(ack, tp->tlp_high_seq))
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003483 return;
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003484
Sébastien Barré08abdff2015-01-12 10:30:40 +01003485 if (flag & FLAG_DSACKING_ACK) {
3486 /* This DSACK means original and TLP probe arrived; no loss */
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003487 tp->tlp_high_seq = 0;
Sébastien Barré08abdff2015-01-12 10:30:40 +01003488 } else if (after(ack, tp->tlp_high_seq)) {
3489 /* ACK advances: there was a loss, so reduce cwnd. Reset
3490 * tlp_high_seq in tcp_init_cwnd_reduction()
3491 */
3492 tcp_init_cwnd_reduction(sk);
3493 tcp_set_ca_state(sk, TCP_CA_CWR);
3494 tcp_end_cwnd_reduction(sk);
3495 tcp_try_keep_open(sk);
Eric Dumazetc10d9312016-04-29 14:16:47 -07003496 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07003497 LINUX_MIB_TCPLOSSPROBERECOVERY);
Sébastien Barré08abdff2015-01-12 10:30:40 +01003498 } else if (!(flag & (FLAG_SND_UNA_ADVANCED |
3499 FLAG_NOT_DUP | FLAG_DATA_SACKED))) {
3500 /* Pure dupack: original and TLP probe arrived; no loss */
3501 tp->tlp_high_seq = 0;
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003502 }
3503}
3504
Florian Westphal7354c8c2014-09-26 22:37:34 +02003505static inline void tcp_in_ack_event(struct sock *sk, u32 flags)
3506{
3507 const struct inet_connection_sock *icsk = inet_csk(sk);
3508
3509 if (icsk->icsk_ca_ops->in_ack_event)
3510 icsk->icsk_ca_ops->in_ack_event(sk, flags);
3511}
3512
Yuchung Chenge662ca42016-02-02 10:33:04 -08003513/* Congestion control has updated the cwnd already. So if we're in
3514 * loss recovery then now we do any new sends (for FRTO) or
3515 * retransmits (for CA_Loss or CA_recovery) that make sense.
3516 */
3517static void tcp_xmit_recovery(struct sock *sk, int rexmit)
3518{
3519 struct tcp_sock *tp = tcp_sk(sk);
3520
3521 if (rexmit == REXMIT_NONE)
3522 return;
3523
3524 if (unlikely(rexmit == 2)) {
3525 __tcp_push_pending_frames(sk, tcp_current_mss(sk),
3526 TCP_NAGLE_OFF);
3527 if (after(tp->snd_nxt, tp->high_seq))
3528 return;
3529 tp->frto = 0;
3530 }
3531 tcp_xmit_retransmit_queue(sk);
3532}
3533
Yuchung Chenga77fa012018-04-17 23:18:47 -07003534/* Returns the number of packets newly acked or sacked by the current ACK */
3535static u32 tcp_newly_delivered(struct sock *sk, u32 prior_delivered, int flag)
3536{
Yuchung Chengfeb5f2e2018-04-17 23:18:49 -07003537 const struct net *net = sock_net(sk);
Yuchung Chenga77fa012018-04-17 23:18:47 -07003538 struct tcp_sock *tp = tcp_sk(sk);
3539 u32 delivered;
3540
3541 delivered = tp->delivered - prior_delivered;
Yuchung Chengfeb5f2e2018-04-17 23:18:49 -07003542 NET_ADD_STATS(net, LINUX_MIB_TCPDELIVERED, delivered);
3543 if (flag & FLAG_ECE) {
Yuchung Chenge21db6f2018-04-17 23:18:48 -07003544 tp->delivered_ce += delivered;
Yuchung Chengfeb5f2e2018-04-17 23:18:49 -07003545 NET_ADD_STATS(net, LINUX_MIB_TCPDELIVEREDCE, delivered);
3546 }
Yuchung Chenga77fa012018-04-17 23:18:47 -07003547 return delivered;
3548}
3549
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550/* This routine deals with incoming acks, but not outgoing ones. */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003551static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03003553 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 struct tcp_sock *tp = tcp_sk(sk);
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02003555 struct tcp_sacktag_state sack_state;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003556 struct rate_sample rs = { .prior_delivered = 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 u32 prior_snd_una = tp->snd_una;
Yousuk Seungd4761752017-12-07 13:41:34 -08003558 bool is_sack_reneg = tp->is_sack_reneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 u32 ack_seq = TCP_SKB_CB(skb)->seq;
3560 u32 ack = TCP_SKB_CB(skb)->ack_seq;
Neal Cardwell7d2b55f2011-11-16 08:58:01 +00003561 bool is_dupack = false;
Nandita Dukkipati35f079e2013-05-21 15:12:07 +00003562 int prior_packets = tp->packets_out;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003563 u32 delivered = tp->delivered;
3564 u32 lost = tp->lost;
Yuchung Chenge662ca42016-02-02 10:33:04 -08003565 int rexmit = REXMIT_NONE; /* Flag to (re)transmit to recover losses */
Yuchung Cheng737ff312017-11-08 13:01:27 -08003566 u32 prior_fack;
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02003567
Eric Dumazet9a568de2017-05-16 14:00:14 -07003568 sack_state.first_sackt = 0;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003569 sack_state.rate = &rs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Eric Dumazet75c119a2017-10-05 22:21:27 -07003571 /* We very likely will need to access rtx queue. */
3572 prefetch(sk->tcp_rtx_queue.rb_node);
Eric Dumazetad971f62014-10-11 15:17:29 -07003573
John Dykstra96e0bf42009-03-22 21:49:57 -07003574 /* If the ack is older than previous acks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 * then we can probably ignore it.
3576 */
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003577 if (before(ack, prior_snd_una)) {
3578 /* RFC 5961 5.2 [Blind Data Injection Attack].[Mitigation] */
3579 if (before(ack, prior_snd_una - tp->max_window)) {
Eric Dumazetd0e1a1b2017-05-23 15:24:46 -07003580 if (!(flag & FLAG_NO_CHALLENGE_ACK))
3581 tcp_send_challenge_ack(sk, skb);
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003582 return -1;
3583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 goto old_ack;
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
John Dykstra96e0bf42009-03-22 21:49:57 -07003587 /* If the ack includes data we haven't sent yet, discard
3588 * this segment (RFC793 Section 3.9).
3589 */
3590 if (after(ack, tp->snd_nxt))
3591 goto invalid_ack;
3592
Neal Cardwell0c9ab092014-08-14 16:13:07 -04003593 if (after(ack, prior_snd_una)) {
Ilpo Järvinen2e605292007-08-02 19:46:58 -07003594 flag |= FLAG_SND_UNA_ADVANCED;
Neal Cardwell0c9ab092014-08-14 16:13:07 -04003595 icsk->icsk_retransmits = 0;
Ilya Lesokhin6dac1522018-04-30 10:16:10 +03003596
3597#if IS_ENABLED(CONFIG_TLS_DEVICE)
3598 if (static_branch_unlikely(&clean_acked_data_enabled))
3599 if (icsk->icsk_clean_acked)
3600 icsk->icsk_clean_acked(sk, ack);
3601#endif
Neal Cardwell0c9ab092014-08-14 16:13:07 -04003602 }
Ilpo Järvinen2e605292007-08-02 19:46:58 -07003603
Eric Dumazet50895b92017-11-14 21:02:19 -08003604 prior_fack = tcp_is_sack(tp) ? tcp_highest_sack_seq(tp) : tp->snd_una;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003605 rs.prior_in_flight = tcp_packets_in_flight(tp);
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003606
Eric Dumazet12fb3dd2013-04-19 07:19:48 +00003607 /* ts_recent update must be made after we are sure that the packet
3608 * is in window.
3609 */
3610 if (flag & FLAG_UPDATE_TS_RECENT)
3611 tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
3612
Florian Westphal31770e32017-08-30 19:24:58 +02003613 if (!(flag & FLAG_SLOWPATH) && after(ack, prior_snd_una)) {
3614 /* Window is constant, pure forward advance.
3615 * No more checks are required.
3616 * Note, we use the fact that SND.UNA>=SND.WL2.
3617 */
3618 tcp_update_wl(tp, ack_seq);
3619 tcp_snd_una_update(tp, ack);
3620 flag |= FLAG_WIN_UPDATE;
3621
3622 tcp_in_ack_event(sk, CA_ACK_WIN_UPDATE);
3623
3624 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPHPACKS);
3625 } else {
Florian Westphalc1d2b4c2017-08-30 19:24:57 +02003626 u32 ack_ev_flags = CA_ACK_SLOWPATH;
Florian Westphal98900922014-09-26 22:37:35 +02003627
Florian Westphalc1d2b4c2017-08-30 19:24:57 +02003628 if (ack_seq != TCP_SKB_CB(skb)->end_seq)
3629 flag |= FLAG_DATA;
3630 else
3631 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPUREACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Florian Westphalc1d2b4c2017-08-30 19:24:57 +02003633 flag |= tcp_ack_update_window(sk, skb, ack, ack_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
Florian Westphalc1d2b4c2017-08-30 19:24:57 +02003635 if (TCP_SKB_CB(skb)->sacked)
3636 flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una,
3637 &sack_state);
3638
3639 if (tcp_ecn_rcv_ecn_echo(tp, tcp_hdr(skb))) {
3640 flag |= FLAG_ECE;
3641 ack_ev_flags |= CA_ACK_ECE;
3642 }
3643
3644 if (flag & FLAG_WIN_UPDATE)
3645 ack_ev_flags |= CA_ACK_WIN_UPDATE;
3646
3647 tcp_in_ack_event(sk, ack_ev_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 }
3649
3650 /* We passed data and got it acked, remove any soft error
3651 * log. Something worked...
3652 */
3653 sk->sk_err_soft = 0;
David S. Miller4b53fb62008-07-23 16:38:45 -07003654 icsk->icsk_probes_out = 0;
Eric Dumazet70eabf02017-05-16 14:00:07 -07003655 tp->rcv_tstamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 if (!prior_packets)
3657 goto no_queue;
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 /* See if we can take anything off of the retransmit queue. */
Yuchung Cheng737ff312017-11-08 13:01:27 -08003660 flag |= tcp_clean_rtx_queue(sk, prior_fack, prior_snd_una, &sack_state);
Nandita Dukkipatia262f0c2011-08-21 20:21:57 +00003661
Priyaranjan Jha1f255692017-11-03 16:38:48 -07003662 tcp_rack_update_reo_wnd(sk, &rs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
Neal Cardwelldf92c832017-08-03 09:19:54 -04003664 if (tp->tlp_high_seq)
3665 tcp_process_tlp_ack(sk, ack, flag);
3666 /* If needed, reset TLP/RTO timer; RACK may later override this. */
3667 if (flag & FLAG_SET_XMIT_TIMER)
3668 tcp_set_xmit_timer(sk);
3669
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03003670 if (tcp_ack_is_dubious(sk, flag)) {
Neal Cardwell7d2b55f2011-11-16 08:58:01 +00003671 is_dupack = !(flag & (FLAG_SND_UNA_ADVANCED | FLAG_NOT_DUP));
Yuchung Cheng737ff312017-11-08 13:01:27 -08003672 tcp_fastretrans_alert(sk, prior_snd_una, is_dupack, &flag,
3673 &rexmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 }
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003675
Julian Anastasovc3a2e832017-02-06 23:14:14 +02003676 if ((flag & FLAG_FORWARD_PROGRESS) || !(flag & FLAG_NOT_DUP))
3677 sk_dst_confirm(sk);
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00003678
Yuchung Chenga77fa012018-04-17 23:18:47 -07003679 delivered = tcp_newly_delivered(sk, delivered, flag);
Yuchung Chengb9f64822016-09-19 23:39:14 -04003680 lost = tp->lost - lost; /* freshly marked lost */
Yuchung Chenge4286602018-01-17 12:11:01 -08003681 rs.is_ack_delayed = !!(flag & FLAG_ACK_MAYBE_DELAYED);
Yousuk Seungd4761752017-12-07 13:41:34 -08003682 tcp_rate_gen(sk, delivered, lost, is_sack_reneg, sack_state.rate);
Yuchung Chengdeed7be2017-01-12 22:11:32 -08003683 tcp_cong_control(sk, ack, delivered, flag, sack_state.rate);
Yuchung Chenge662ca42016-02-02 10:33:04 -08003684 tcp_xmit_recovery(sk, rexmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 return 1;
3686
3687no_queue:
Neal Cardwell5628adf2011-11-16 08:58:02 +00003688 /* If data was DSACKed, see if we can undo a cwnd reduction. */
Yuchung Chenga77fa012018-04-17 23:18:47 -07003689 if (flag & FLAG_DSACKING_ACK) {
Yuchung Cheng737ff312017-11-08 13:01:27 -08003690 tcp_fastretrans_alert(sk, prior_snd_una, is_dupack, &flag,
3691 &rexmit);
Yuchung Chenga77fa012018-04-17 23:18:47 -07003692 tcp_newly_delivered(sk, delivered, flag);
3693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 /* If this ack opens up a zero window, clear backoff. It was
3695 * being used to time the probes, and is probably far higher than
3696 * it needs to be for normal retransmission.
3697 */
Eric Dumazet75c119a2017-10-05 22:21:27 -07003698 tcp_ack_probe(sk);
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003699
3700 if (tp->tlp_high_seq)
3701 tcp_process_tlp_ack(sk, ack, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 return 1;
3703
John Dykstra96e0bf42009-03-22 21:49:57 -07003704invalid_ack:
3705 SOCK_DEBUG(sk, "Ack %u after %u:%u\n", ack, tp->snd_una, tp->snd_nxt);
3706 return -1;
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708old_ack:
Neal Cardwelle95ae2f2011-11-16 08:58:03 +00003709 /* If data was SACKed, tag it and see if we should send more data.
3710 * If data was DSACKed, see if we can undo a cwnd reduction.
3711 */
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07003712 if (TCP_SKB_CB(skb)->sacked) {
Yuchung Cheng59c9af42013-07-22 16:20:47 -07003713 flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02003714 &sack_state);
Yuchung Cheng737ff312017-11-08 13:01:27 -08003715 tcp_fastretrans_alert(sk, prior_snd_una, is_dupack, &flag,
3716 &rexmit);
Yuchung Chenga77fa012018-04-17 23:18:47 -07003717 tcp_newly_delivered(sk, delivered, flag);
Yuchung Chenge662ca42016-02-02 10:33:04 -08003718 tcp_xmit_recovery(sk, rexmit);
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07003719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
John Dykstra96e0bf42009-03-22 21:49:57 -07003721 SOCK_DEBUG(sk, "Ack %u before %u:%u\n", ack, tp->snd_una, tp->snd_nxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 return 0;
3723}
3724
Daniel Lee7f9b8382015-04-06 14:37:26 -07003725static void tcp_parse_fastopen_option(int len, const unsigned char *cookie,
3726 bool syn, struct tcp_fastopen_cookie *foc,
3727 bool exp_opt)
3728{
3729 /* Valid only in SYN or SYN-ACK with an even length. */
3730 if (!foc || !syn || len < 0 || (len & 1))
3731 return;
3732
3733 if (len >= TCP_FASTOPEN_COOKIE_MIN &&
3734 len <= TCP_FASTOPEN_COOKIE_MAX)
3735 memcpy(foc->val, cookie, len);
3736 else if (len != 0)
3737 len = -1;
3738 foc->len = len;
3739 foc->exp = exp_opt;
3740}
3741
Ursula Braun60e2a772017-10-25 11:01:45 +02003742static void smc_parse_options(const struct tcphdr *th,
3743 struct tcp_options_received *opt_rx,
3744 const unsigned char *ptr,
3745 int opsize)
3746{
3747#if IS_ENABLED(CONFIG_SMC)
3748 if (static_branch_unlikely(&tcp_have_smc)) {
3749 if (th->syn && !(opsize & 1) &&
3750 opsize >= TCPOLEN_EXP_SMC_BASE &&
3751 get_unaligned_be32(ptr) == TCPOPT_SMC_MAGIC)
3752 opt_rx->smc_ok = 1;
3753 }
3754#endif
3755}
3756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757/* Look for tcp options. Normally only called on SYN and SYNACK packets.
3758 * But, this can also be called on packets in the established flow when
3759 * the fast version below fails.
3760 */
Eric Dumazeteed29f12017-06-07 10:34:36 -07003761void tcp_parse_options(const struct net *net,
3762 const struct sk_buff *skb,
Christoph Paasch1a2c6182013-03-17 08:23:34 +00003763 struct tcp_options_received *opt_rx, int estab,
Yuchung Cheng2100c8d2012-07-19 06:43:05 +00003764 struct tcp_fastopen_cookie *foc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003766 const unsigned char *ptr;
3767 const struct tcphdr *th = tcp_hdr(skb);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08003768 int length = (th->doff * 4) - sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003770 ptr = (const unsigned char *)(th + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 opt_rx->saw_tstamp = 0;
3772
Stephen Hemminger2de979b2007-03-08 20:45:19 -08003773 while (length > 0) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08003774 int opcode = *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 int opsize;
3776
3777 switch (opcode) {
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003778 case TCPOPT_EOL:
3779 return;
3780 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
3781 length--;
3782 continue;
3783 default:
3784 opsize = *ptr++;
3785 if (opsize < 2) /* "silly options" */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 return;
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003787 if (opsize > length)
3788 return; /* don't parse partial options */
3789 switch (opcode) {
3790 case TCPOPT_MSS:
3791 if (opsize == TCPOLEN_MSS && th->syn && !estab) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07003792 u16 in_mss = get_unaligned_be16(ptr);
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003793 if (in_mss) {
3794 if (opt_rx->user_mss &&
3795 opt_rx->user_mss < in_mss)
3796 in_mss = opt_rx->user_mss;
3797 opt_rx->mss_clamp = in_mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 }
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003799 }
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003800 break;
3801 case TCPOPT_WINDOW:
3802 if (opsize == TCPOLEN_WINDOW && th->syn &&
Eric Dumazet9bb37ef2017-06-07 10:34:38 -07003803 !estab && net->ipv4.sysctl_tcp_window_scaling) {
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003804 __u8 snd_wscale = *(__u8 *)ptr;
3805 opt_rx->wscale_ok = 1;
Gao Feng589c49c2017-04-04 21:09:48 +08003806 if (snd_wscale > TCP_MAX_WSCALE) {
3807 net_info_ratelimited("%s: Illegal window scaling value %d > %u received\n",
Joe Perchese87cc472012-05-13 21:56:26 +00003808 __func__,
Gao Feng589c49c2017-04-04 21:09:48 +08003809 snd_wscale,
3810 TCP_MAX_WSCALE);
3811 snd_wscale = TCP_MAX_WSCALE;
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003812 }
3813 opt_rx->snd_wscale = snd_wscale;
3814 }
3815 break;
3816 case TCPOPT_TIMESTAMP:
3817 if ((opsize == TCPOLEN_TIMESTAMP) &&
3818 ((estab && opt_rx->tstamp_ok) ||
Eric Dumazet5d2ed052017-06-07 10:34:39 -07003819 (!estab && net->ipv4.sysctl_tcp_timestamps))) {
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003820 opt_rx->saw_tstamp = 1;
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07003821 opt_rx->rcv_tsval = get_unaligned_be32(ptr);
3822 opt_rx->rcv_tsecr = get_unaligned_be32(ptr + 4);
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003823 }
3824 break;
3825 case TCPOPT_SACK_PERM:
3826 if (opsize == TCPOLEN_SACK_PERM && th->syn &&
Eric Dumazetf9301032017-06-07 10:34:37 -07003827 !estab && net->ipv4.sysctl_tcp_sack) {
Vijay Subramanianab562222011-12-20 13:23:24 +00003828 opt_rx->sack_ok = TCP_SACK_SEEN;
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003829 tcp_sack_reset(opt_rx);
3830 }
3831 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003832
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003833 case TCPOPT_SACK:
3834 if ((opsize >= (TCPOLEN_SACK_BASE + TCPOLEN_SACK_PERBLOCK)) &&
3835 !((opsize - TCPOLEN_SACK_BASE) % TCPOLEN_SACK_PERBLOCK) &&
3836 opt_rx->sack_ok) {
3837 TCP_SKB_CB(skb)->sacked = (ptr - 2) - (unsigned char *)th;
3838 }
3839 break;
3840#ifdef CONFIG_TCP_MD5SIG
3841 case TCPOPT_MD5SIG:
3842 /*
3843 * The MD5 Hash has already been
3844 * checked (see tcp_v{4,6}_do_rcv()).
3845 */
3846 break;
3847#endif
Daniel Lee7f9b8382015-04-06 14:37:26 -07003848 case TCPOPT_FASTOPEN:
3849 tcp_parse_fastopen_option(
3850 opsize - TCPOLEN_FASTOPEN_BASE,
3851 ptr, th->syn, foc, false);
3852 break;
3853
Yuchung Cheng2100c8d2012-07-19 06:43:05 +00003854 case TCPOPT_EXP:
3855 /* Fast Open option shares code 254 using a
Daniel Lee7f9b8382015-04-06 14:37:26 -07003856 * 16 bits magic number.
Yuchung Cheng2100c8d2012-07-19 06:43:05 +00003857 */
Daniel Lee7f9b8382015-04-06 14:37:26 -07003858 if (opsize >= TCPOLEN_EXP_FASTOPEN_BASE &&
3859 get_unaligned_be16(ptr) ==
3860 TCPOPT_FASTOPEN_MAGIC)
3861 tcp_parse_fastopen_option(opsize -
3862 TCPOLEN_EXP_FASTOPEN_BASE,
3863 ptr + 2, th->syn, foc, true);
Ursula Braun60e2a772017-10-25 11:01:45 +02003864 else
3865 smc_parse_options(th, opt_rx, ptr,
3866 opsize);
Yuchung Cheng2100c8d2012-07-19 06:43:05 +00003867 break;
3868
3869 }
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003870 ptr += opsize-2;
3871 length -= opsize;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 }
3874}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00003875EXPORT_SYMBOL(tcp_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003877static bool tcp_parse_aligned_timestamp(struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003878{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003879 const __be32 *ptr = (const __be32 *)(th + 1);
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003880
3881 if (*ptr == htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16)
3882 | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) {
3883 tp->rx_opt.saw_tstamp = 1;
3884 ++ptr;
3885 tp->rx_opt.rcv_tsval = ntohl(*ptr);
3886 ++ptr;
Andrew Vagine3e12022013-08-27 12:21:55 +04003887 if (*ptr)
3888 tp->rx_opt.rcv_tsecr = ntohl(*ptr) - tp->tsoffset;
3889 else
3890 tp->rx_opt.rcv_tsecr = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003891 return true;
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003892 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003893 return false;
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003894}
3895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896/* Fast parse options. This hopes to only see timestamps.
3897 * If it is wrong it falls back on tcp_parse_options().
3898 */
Eric Dumazeteed29f12017-06-07 10:34:36 -07003899static bool tcp_fast_parse_options(const struct net *net,
3900 const struct sk_buff *skb,
Christoph Paasch1a2c6182013-03-17 08:23:34 +00003901 const struct tcphdr *th, struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902{
William Allen Simpson4957faade2009-12-02 18:25:27 +00003903 /* In the spirit of fast parsing, compare doff directly to constant
3904 * values. Because equality is used, short doff can be ignored here.
3905 */
3906 if (th->doff == (sizeof(*th) / 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 tp->rx_opt.saw_tstamp = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003908 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 } else if (tp->rx_opt.tstamp_ok &&
William Allen Simpson4957faade2009-12-02 18:25:27 +00003910 th->doff == ((sizeof(*th) + TCPOLEN_TSTAMP_ALIGNED) / 4)) {
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003911 if (tcp_parse_aligned_timestamp(tp, th))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003912 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 }
Andrey Vaginee684b62013-02-11 05:50:19 +00003914
Eric Dumazeteed29f12017-06-07 10:34:36 -07003915 tcp_parse_options(net, skb, &tp->rx_opt, 1, NULL);
Andrew Vagine3e12022013-08-27 12:21:55 +04003916 if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
Andrey Vaginee684b62013-02-11 05:50:19 +00003917 tp->rx_opt.rcv_tsecr -= tp->tsoffset;
3918
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003919 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920}
3921
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003922#ifdef CONFIG_TCP_MD5SIG
3923/*
3924 * Parse MD5 Signature option
3925 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003926const u8 *tcp_parse_md5sig_option(const struct tcphdr *th)
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003927{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003928 int length = (th->doff << 2) - sizeof(*th);
3929 const u8 *ptr = (const u8 *)(th + 1);
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003930
Jann Horn7e5a2062018-04-20 15:57:30 +02003931 /* If not enough data remaining, we can short cut */
3932 while (length >= TCPOLEN_MD5SIG) {
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003933 int opcode = *ptr++;
3934 int opsize;
3935
Weilong Chena22318e2013-12-23 14:37:26 +08003936 switch (opcode) {
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003937 case TCPOPT_EOL:
3938 return NULL;
3939 case TCPOPT_NOP:
3940 length--;
3941 continue;
3942 default:
3943 opsize = *ptr++;
3944 if (opsize < 2 || opsize > length)
3945 return NULL;
3946 if (opcode == TCPOPT_MD5SIG)
Dmitry Popovba78e2d2010-08-07 20:24:28 -07003947 return opsize == TCPOLEN_MD5SIG ? ptr : NULL;
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003948 }
3949 ptr += opsize - 2;
3950 length -= opsize;
3951 }
3952 return NULL;
3953}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00003954EXPORT_SYMBOL(tcp_parse_md5sig_option);
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003955#endif
3956
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957/* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM
3958 *
3959 * It is not fatal. If this ACK does _not_ change critical state (seqs, window)
3960 * it can pass through stack. So, the following predicate verifies that
3961 * this segment is not used for anything but congestion avoidance or
3962 * fast retransmit. Moreover, we even are able to eliminate most of such
3963 * second order effects, if we apply some small "replay" window (~RTO)
3964 * to timestamp space.
3965 *
3966 * All these measures still do not guarantee that we reject wrapped ACKs
3967 * on networks with high bandwidth, when sequence space is recycled fastly,
3968 * but it guarantees that such events will be very rare and do not affect
3969 * connection seriously. This doesn't look nice, but alas, PAWS is really
3970 * buggy extension.
3971 *
3972 * [ Later note. Even worse! It is buggy for segments _with_ data. RFC
3973 * states that events when retransmit arrives after original data are rare.
3974 * It is a blatant lie. VJ forgot about fast retransmit! 8)8) It is
3975 * the biggest problem on large power networks even with minor reordering.
3976 * OK, let's give it small replay window. If peer clock is even 1hz, it is safe
3977 * up to bandwidth of 18Gigabit/sec. 8) ]
3978 */
3979
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003980static int tcp_disordered_ack(const struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003982 const struct tcp_sock *tp = tcp_sk(sk);
3983 const struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 u32 seq = TCP_SKB_CB(skb)->seq;
3985 u32 ack = TCP_SKB_CB(skb)->ack_seq;
3986
3987 return (/* 1. Pure ACK with correct sequence number. */
3988 (th->ack && seq == TCP_SKB_CB(skb)->end_seq && seq == tp->rcv_nxt) &&
3989
3990 /* 2. ... and duplicate ACK. */
3991 ack == tp->snd_una &&
3992
3993 /* 3. ... and does not update window. */
3994 !tcp_may_update_window(tp, ack, seq, ntohs(th->window) << tp->rx_opt.snd_wscale) &&
3995
3996 /* 4. ... and sits in replay window. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003997 (s32)(tp->rx_opt.ts_recent - tp->rx_opt.rcv_tsval) <= (inet_csk(sk)->icsk_rto * 1024) / HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998}
3999
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00004000static inline bool tcp_paws_discard(const struct sock *sk,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004001 const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004003 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinenc887e6d2009-03-14 14:23:03 +00004004
4005 return !tcp_paws_check(&tp->rx_opt, TCP_PAWS_WINDOW) &&
4006 !tcp_disordered_ack(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007}
4008
4009/* Check segment sequence number for validity.
4010 *
4011 * Segment controls are considered valid, if the segment
4012 * fits to the window after truncation to the window. Acceptability
4013 * of data (and SYN, FIN, of course) is checked separately.
4014 * See tcp_data_queue(), for example.
4015 *
4016 * Also, controls (RST is main one) are accepted using RCV.WUP instead
4017 * of RCV.NXT. Peer still did not advance his SND.UNA when we
4018 * delayed ACK, so that hisSND.UNA<=ourRCV.WUP.
4019 * (borrowed from freebsd)
4020 */
4021
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00004022static inline bool tcp_sequence(const struct tcp_sock *tp, u32 seq, u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
4024 return !before(end_seq, tp->rcv_wup) &&
4025 !after(seq, tp->rcv_nxt + tcp_receive_window(tp));
4026}
4027
4028/* When we get a reset we do this. */
Jerry Chu10467162012-08-31 12:29:11 +00004029void tcp_reset(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030{
Song Liu59415212017-10-23 09:20:25 -07004031 trace_tcp_receive_reset(sk);
4032
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 /* We want the right error as BSD sees it (and indeed as we do). */
4034 switch (sk->sk_state) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004035 case TCP_SYN_SENT:
4036 sk->sk_err = ECONNREFUSED;
4037 break;
4038 case TCP_CLOSE_WAIT:
4039 sk->sk_err = EPIPE;
4040 break;
4041 case TCP_CLOSE:
4042 return;
4043 default:
4044 sk->sk_err = ECONNRESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 }
Tom Marshalla4d25802010-09-20 15:42:05 -07004046 /* This barrier is coupled with smp_rmb() in tcp_poll() */
4047 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
Soheil Hassas Yeganeha27fd7a2018-02-27 18:32:18 -05004049 tcp_write_queue_purge(sk);
Eric Dumazet3d476262017-04-18 09:45:51 -07004050 tcp_done(sk);
4051
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 if (!sock_flag(sk, SOCK_DEAD))
4053 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054}
4055
4056/*
4057 * Process the FIN bit. This now behaves as it is supposed to work
4058 * and the FIN takes effect when it is validly part of sequence
4059 * space. Not before when we get holes.
4060 *
4061 * If we are ESTABLISHED, a received fin moves us to CLOSE-WAIT
4062 * (and thence onto LAST-ACK and finally, CLOSE, we never enter
4063 * TIME-WAIT)
4064 *
4065 * If we are in FINWAIT-1, a received FIN indicates simultaneous
4066 * close and we go into CLOSING (and later onto TIME-WAIT)
4067 *
4068 * If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT.
4069 */
Eric Dumazete3e17b72016-02-06 11:16:28 -08004070void tcp_fin(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
4072 struct tcp_sock *tp = tcp_sk(sk);
4073
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004074 inet_csk_schedule_ack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
4076 sk->sk_shutdown |= RCV_SHUTDOWN;
4077 sock_set_flag(sk, SOCK_DONE);
4078
4079 switch (sk->sk_state) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004080 case TCP_SYN_RECV:
4081 case TCP_ESTABLISHED:
4082 /* Move to CLOSE_WAIT */
4083 tcp_set_state(sk, TCP_CLOSE_WAIT);
Jon Maxwell2251ae42015-07-08 10:12:28 +10004084 inet_csk(sk)->icsk_ack.pingpong = 1;
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004085 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004087 case TCP_CLOSE_WAIT:
4088 case TCP_CLOSING:
4089 /* Received a retransmission of the FIN, do
4090 * nothing.
4091 */
4092 break;
4093 case TCP_LAST_ACK:
4094 /* RFC793: Remain in the LAST-ACK state. */
4095 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004097 case TCP_FIN_WAIT1:
4098 /* This case occurs when a simultaneous close
4099 * happens, we must ack the received FIN and
4100 * enter the CLOSING state.
4101 */
4102 tcp_send_ack(sk);
4103 tcp_set_state(sk, TCP_CLOSING);
4104 break;
4105 case TCP_FIN_WAIT2:
4106 /* Received a FIN -- send ACK and enter TIME_WAIT. */
4107 tcp_send_ack(sk);
4108 tcp_time_wait(sk, TCP_TIME_WAIT, 0);
4109 break;
4110 default:
4111 /* Only TCP_LISTEN and TCP_CLOSE are left, in these
4112 * cases we should never reach this piece of code.
4113 */
Joe Perches058bd4d2012-03-11 18:36:11 +00004114 pr_err("%s: Impossible, sk->sk_state=%d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08004115 __func__, sk->sk_state);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004116 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
4119 /* It _is_ possible, that we have something out-of-order _after_ FIN.
4120 * Probably, we should reset in this case. For now drop them.
4121 */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004122 skb_rbtree_purge(&tp->out_of_order_queue);
Ilpo Järvinene60402d2007-08-09 15:14:46 +03004123 if (tcp_is_sack(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 tcp_sack_reset(&tp->rx_opt);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08004125 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
4127 if (!sock_flag(sk, SOCK_DEAD)) {
4128 sk->sk_state_change(sk);
4129
4130 /* Do not send POLL_HUP for half duplex close. */
4131 if (sk->sk_shutdown == SHUTDOWN_MASK ||
4132 sk->sk_state == TCP_CLOSE)
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08004133 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 else
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08004135 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 }
4137}
4138
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004139static inline bool tcp_sack_extend(struct tcp_sack_block *sp, u32 seq,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004140 u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141{
4142 if (!after(seq, sp->end_seq) && !after(sp->start_seq, end_seq)) {
4143 if (before(seq, sp->start_seq))
4144 sp->start_seq = seq;
4145 if (after(end_seq, sp->end_seq))
4146 sp->end_seq = end_seq;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004147 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004149 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150}
4151
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004152static void tcp_dsack_set(struct sock *sk, u32 seq, u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004154 struct tcp_sock *tp = tcp_sk(sk);
4155
Eric Dumazet6496f6b2017-10-26 21:55:07 -07004156 if (tcp_is_sack(tp) && sock_net(sk)->ipv4.sysctl_tcp_dsack) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07004157 int mib_idx;
4158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 if (before(seq, tp->rcv_nxt))
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07004160 mib_idx = LINUX_MIB_TCPDSACKOLDSENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 else
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07004162 mib_idx = LINUX_MIB_TCPDSACKOFOSENT;
4163
Eric Dumazetc10d9312016-04-29 14:16:47 -07004164 NET_INC_STATS(sock_net(sk), mib_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
4166 tp->rx_opt.dsack = 1;
4167 tp->duplicate_sack[0].start_seq = seq;
4168 tp->duplicate_sack[0].end_seq = end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 }
4170}
4171
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004172static void tcp_dsack_extend(struct sock *sk, u32 seq, u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173{
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004174 struct tcp_sock *tp = tcp_sk(sk);
4175
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 if (!tp->rx_opt.dsack)
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004177 tcp_dsack_set(sk, seq, end_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 else
4179 tcp_sack_extend(tp->duplicate_sack, seq, end_seq);
4180}
4181
Eric Dumazetcf533ea2011-10-21 05:22:42 -04004182static void tcp_send_dupack(struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183{
4184 struct tcp_sock *tp = tcp_sk(sk);
4185
4186 if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
4187 before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07004188 NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
Eric Dumazet9a9c9b52018-05-21 15:08:56 -07004189 tcp_enter_quickack_mode(sk, TCP_MAX_QUICKACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Eric Dumazet6496f6b2017-10-26 21:55:07 -07004191 if (tcp_is_sack(tp) && sock_net(sk)->ipv4.sysctl_tcp_dsack) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 u32 end_seq = TCP_SKB_CB(skb)->end_seq;
4193
4194 if (after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))
4195 end_seq = tp->rcv_nxt;
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004196 tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, end_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 }
4198 }
4199
4200 tcp_send_ack(sk);
4201}
4202
4203/* These routines update the SACK block as out-of-order packets arrive or
4204 * in-order packets close up the sequence space.
4205 */
4206static void tcp_sack_maybe_coalesce(struct tcp_sock *tp)
4207{
4208 int this_sack;
4209 struct tcp_sack_block *sp = &tp->selective_acks[0];
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004210 struct tcp_sack_block *swalk = sp + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
4212 /* See if the recent change to the first SACK eats into
4213 * or hits the sequence space of other SACK blocks, if so coalesce.
4214 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004215 for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 if (tcp_sack_extend(sp, swalk->start_seq, swalk->end_seq)) {
4217 int i;
4218
4219 /* Zap SWALK, by moving every further SACK up by one slot.
4220 * Decrease num_sacks.
4221 */
4222 tp->rx_opt.num_sacks--;
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004223 for (i = this_sack; i < tp->rx_opt.num_sacks; i++)
4224 sp[i] = sp[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 continue;
4226 }
4227 this_sack++, swalk++;
4228 }
4229}
4230
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231static void tcp_sack_new_ofo_skb(struct sock *sk, u32 seq, u32 end_seq)
4232{
4233 struct tcp_sock *tp = tcp_sk(sk);
4234 struct tcp_sack_block *sp = &tp->selective_acks[0];
4235 int cur_sacks = tp->rx_opt.num_sacks;
4236 int this_sack;
4237
4238 if (!cur_sacks)
4239 goto new_sack;
4240
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004241 for (this_sack = 0; this_sack < cur_sacks; this_sack++, sp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 if (tcp_sack_extend(sp, seq, end_seq)) {
4243 /* Rotate this_sack to the first one. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004244 for (; this_sack > 0; this_sack--, sp--)
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07004245 swap(*sp, *(sp - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 if (cur_sacks > 1)
4247 tcp_sack_maybe_coalesce(tp);
4248 return;
4249 }
4250 }
4251
4252 /* Could not find an adjacent existing SACK, build a new one,
4253 * put it at the front, and shift everyone else down. We
4254 * always know there is at least one SACK present already here.
4255 *
4256 * If the sack array is full, forget about the last one.
4257 */
Adam Langley4389dde2008-07-19 00:07:02 -07004258 if (this_sack >= TCP_NUM_SACKS) {
Eric Dumazet5d9f4262018-05-17 14:47:26 -07004259 if (tp->compressed_ack)
4260 tcp_send_ack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 this_sack--;
4262 tp->rx_opt.num_sacks--;
4263 sp--;
4264 }
Stephen Hemminger2de979b2007-03-08 20:45:19 -08004265 for (; this_sack > 0; this_sack--, sp--)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004266 *sp = *(sp - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
4268new_sack:
4269 /* Build the new head SACK, and we're done. */
4270 sp->start_seq = seq;
4271 sp->end_seq = end_seq;
4272 tp->rx_opt.num_sacks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273}
4274
4275/* RCV.NXT advances, some SACKs should be eaten. */
4276
4277static void tcp_sack_remove(struct tcp_sock *tp)
4278{
4279 struct tcp_sack_block *sp = &tp->selective_acks[0];
4280 int num_sacks = tp->rx_opt.num_sacks;
4281 int this_sack;
4282
4283 /* Empty ofo queue, hence, all the SACKs are eaten. Clear. */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004284 if (RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 tp->rx_opt.num_sacks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 return;
4287 }
4288
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004289 for (this_sack = 0; this_sack < num_sacks;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 /* Check if the start of the sack is covered by RCV.NXT. */
4291 if (!before(tp->rcv_nxt, sp->start_seq)) {
4292 int i;
4293
4294 /* RCV.NXT must cover all the block! */
Ilpo Järvinen547b7922008-07-25 21:43:18 -07004295 WARN_ON(before(tp->rcv_nxt, sp->end_seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
4297 /* Zap this SACK, by moving forward any other SACKS. */
Weilong Chena22318e2013-12-23 14:37:26 +08004298 for (i = this_sack+1; i < num_sacks; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 tp->selective_acks[i-1] = tp->selective_acks[i];
4300 num_sacks--;
4301 continue;
4302 }
4303 this_sack++;
4304 sp++;
4305 }
Ilpo Järvinen5861f8e2009-03-14 14:23:01 +00004306 tp->rx_opt.num_sacks = num_sacks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307}
4308
Eric Dumazet1402d362012-04-23 07:11:42 +00004309/**
4310 * tcp_try_coalesce - try to merge skb to prior one
4311 * @sk: socket
Mike Maloney98aaa912017-08-22 17:08:48 -04004312 * @dest: destination queue
Eric Dumazet1402d362012-04-23 07:11:42 +00004313 * @to: prior buffer
4314 * @from: buffer to add in queue
Eric Dumazet923dd342012-05-02 07:55:58 +00004315 * @fragstolen: pointer to boolean
Eric Dumazet1402d362012-04-23 07:11:42 +00004316 *
4317 * Before queueing skb @from after @to, try to merge them
4318 * to reduce overall memory use and queue lengths, if cost is small.
4319 * Packets in ofo or receive queues can stay a long time.
4320 * Better try to coalesce them right now to avoid future collapses.
Eric Dumazet783c1752012-04-23 17:34:36 +00004321 * Returns true if caller should free @from instead of queueing it
Eric Dumazet1402d362012-04-23 07:11:42 +00004322 */
Eric Dumazet783c1752012-04-23 17:34:36 +00004323static bool tcp_try_coalesce(struct sock *sk,
4324 struct sk_buff *to,
Eric Dumazet329033f2012-04-27 00:38:33 +00004325 struct sk_buff *from,
4326 bool *fragstolen)
Eric Dumazet1402d362012-04-23 07:11:42 +00004327{
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004328 int delta;
Eric Dumazet1402d362012-04-23 07:11:42 +00004329
Eric Dumazet329033f2012-04-27 00:38:33 +00004330 *fragstolen = false;
Alexander Duyck34a802a2012-05-02 21:19:09 +00004331
Eric Dumazet1ca7ee32012-05-23 17:51:37 +00004332 /* Its possible this segment overlaps with prior segment in queue */
4333 if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq)
4334 return false;
4335
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004336 if (!skb_try_coalesce(to, from, fragstolen, &delta))
Eric Dumazet783c1752012-04-23 17:34:36 +00004337 return false;
Alexander Duyck34a802a2012-05-02 21:19:09 +00004338
Alexander Duyck34a802a2012-05-02 21:19:09 +00004339 atomic_add(delta, &sk->sk_rmem_alloc);
4340 sk_mem_charge(sk, delta);
Eric Dumazetc10d9312016-04-29 14:16:47 -07004341 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVCOALESCE);
Alexander Duyck34a802a2012-05-02 21:19:09 +00004342 TCP_SKB_CB(to)->end_seq = TCP_SKB_CB(from)->end_seq;
4343 TCP_SKB_CB(to)->ack_seq = TCP_SKB_CB(from)->ack_seq;
Eric Dumazete93a0432014-09-15 04:19:52 -07004344 TCP_SKB_CB(to)->tcp_flags |= TCP_SKB_CB(from)->tcp_flags;
Mike Maloney98aaa912017-08-22 17:08:48 -04004345
4346 if (TCP_SKB_CB(from)->has_rxtstamp) {
4347 TCP_SKB_CB(to)->has_rxtstamp = true;
Eric Dumazetbffa72c2017-09-19 05:14:24 -07004348 to->tstamp = from->tstamp;
Mike Maloney98aaa912017-08-22 17:08:48 -04004349 }
4350
Alexander Duyck34a802a2012-05-02 21:19:09 +00004351 return true;
Eric Dumazet1402d362012-04-23 07:11:42 +00004352}
4353
Eric Dumazet532182c2016-04-01 08:52:19 -07004354static void tcp_drop(struct sock *sk, struct sk_buff *skb)
4355{
4356 sk_drops_add(sk, skb);
4357 __kfree_skb(skb);
4358}
4359
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360/* This one checks to see if we can put data from the
4361 * out_of_order queue into the receive_queue.
4362 */
4363static void tcp_ofo_queue(struct sock *sk)
4364{
4365 struct tcp_sock *tp = tcp_sk(sk);
4366 __u32 dsack_high = tp->rcv_nxt;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004367 bool fin, fragstolen, eaten;
Eric Dumazetbd1e75a2014-09-19 08:26:20 -07004368 struct sk_buff *skb, *tail;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004369 struct rb_node *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004371 p = rb_first(&tp->out_of_order_queue);
4372 while (p) {
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004373 skb = rb_to_skb(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
4375 break;
4376
4377 if (before(TCP_SKB_CB(skb)->seq, dsack_high)) {
4378 __u32 dsack = dsack_high;
4379 if (before(TCP_SKB_CB(skb)->end_seq, dsack_high))
4380 dsack_high = TCP_SKB_CB(skb)->end_seq;
4381 tcp_dsack_extend(sk, TCP_SKB_CB(skb)->seq, dsack);
4382 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004383 p = rb_next(p);
4384 rb_erase(&skb->rbnode, &tp->out_of_order_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004386 if (unlikely(!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 SOCK_DEBUG(sk, "ofo packet was already received\n");
Eric Dumazet532182c2016-04-01 08:52:19 -07004388 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 continue;
4390 }
4391 SOCK_DEBUG(sk, "ofo requeuing : rcv_next %X seq %X - %X\n",
4392 tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
4393 TCP_SKB_CB(skb)->end_seq);
4394
Eric Dumazetbd1e75a2014-09-19 08:26:20 -07004395 tail = skb_peek_tail(&sk->sk_receive_queue);
Eric Dumazetbffa72c2017-09-19 05:14:24 -07004396 eaten = tail && tcp_try_coalesce(sk, tail, skb, &fragstolen);
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07004397 tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004398 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
Eric Dumazetbd1e75a2014-09-19 08:26:20 -07004399 if (!eaten)
4400 __skb_queue_tail(&sk->sk_receive_queue, skb);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004401 else
Eric Dumazetbd1e75a2014-09-19 08:26:20 -07004402 kfree_skb_partial(skb, fragstolen);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004403
4404 if (unlikely(fin)) {
4405 tcp_fin(sk);
4406 /* tcp_fin() purges tp->out_of_order_queue,
4407 * so we must end this loop right now.
4408 */
4409 break;
4410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 }
4412}
4413
4414static bool tcp_prune_ofo_queue(struct sock *sk);
4415static int tcp_prune_queue(struct sock *sk);
4416
4417static int tcp_try_rmem_schedule(struct sock *sk, struct sk_buff *skb,
4418 unsigned int size)
4419{
4420 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
4421 !sk_rmem_schedule(sk, skb, size)) {
4422
4423 if (tcp_prune_queue(sk) < 0)
4424 return -1;
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004425
Eric Dumazet36a65032016-08-17 14:17:09 -07004426 while (!sk_rmem_schedule(sk, skb, size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 if (!tcp_prune_ofo_queue(sk))
4428 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 }
4430 }
4431 return 0;
4432}
4433
Eric Dumazete86b29192012-03-18 11:06:44 +00004434static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
4435{
4436 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004437 struct rb_node **p, *parent;
Eric Dumazete86b29192012-03-18 11:06:44 +00004438 struct sk_buff *skb1;
4439 u32 seq, end_seq;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004440 bool fragstolen;
Eric Dumazete86b29192012-03-18 11:06:44 +00004441
Yousuk Seungf4c9f852018-06-04 15:29:51 -07004442 tcp_ecn_check_ce(sk, skb);
Eric Dumazete86b29192012-03-18 11:06:44 +00004443
Mel Gormanc76562b2012-07-31 16:44:41 -07004444 if (unlikely(tcp_try_rmem_schedule(sk, skb, skb->truesize))) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07004445 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFODROP);
Eric Dumazet532182c2016-04-01 08:52:19 -07004446 tcp_drop(sk, skb);
Eric Dumazete86b29192012-03-18 11:06:44 +00004447 return;
4448 }
4449
Florian Westphal31770e32017-08-30 19:24:58 +02004450 /* Disable header prediction. */
4451 tp->pred_flags = 0;
Eric Dumazete86b29192012-03-18 11:06:44 +00004452 inet_csk_schedule_ack(sk);
4453
Eric Dumazetc10d9312016-04-29 14:16:47 -07004454 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOQUEUE);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004455 seq = TCP_SKB_CB(skb)->seq;
4456 end_seq = TCP_SKB_CB(skb)->end_seq;
Eric Dumazete86b29192012-03-18 11:06:44 +00004457 SOCK_DEBUG(sk, "out of order segment: rcv_next %X seq %X - %X\n",
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004458 tp->rcv_nxt, seq, end_seq);
Eric Dumazete86b29192012-03-18 11:06:44 +00004459
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004460 p = &tp->out_of_order_queue.rb_node;
4461 if (RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
Eric Dumazete86b29192012-03-18 11:06:44 +00004462 /* Initial out of order segment, build 1 SACK. */
4463 if (tcp_is_sack(tp)) {
4464 tp->rx_opt.num_sacks = 1;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004465 tp->selective_acks[0].start_seq = seq;
4466 tp->selective_acks[0].end_seq = end_seq;
Eric Dumazete86b29192012-03-18 11:06:44 +00004467 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004468 rb_link_node(&skb->rbnode, NULL, p);
4469 rb_insert_color(&skb->rbnode, &tp->out_of_order_queue);
4470 tp->ooo_last_skb = skb;
Eric Dumazete86b29192012-03-18 11:06:44 +00004471 goto end;
4472 }
4473
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004474 /* In the typical case, we are adding an skb to the end of the list.
4475 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
4476 */
Eric Dumazetbffa72c2017-09-19 05:14:24 -07004477 if (tcp_try_coalesce(sk, tp->ooo_last_skb,
Mike Maloney98aaa912017-08-22 17:08:48 -04004478 skb, &fragstolen)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004479coalesce_done:
4480 tcp_grow_window(sk, skb);
4481 kfree_skb_partial(skb, fragstolen);
4482 skb = NULL;
4483 goto add_sack;
Eric Dumazete86b29192012-03-18 11:06:44 +00004484 }
Eric Dumazet2594a2a2016-09-09 14:22:45 -07004485 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
4486 if (!before(seq, TCP_SKB_CB(tp->ooo_last_skb)->end_seq)) {
4487 parent = &tp->ooo_last_skb->rbnode;
4488 p = &parent->rb_right;
4489 goto insert;
Eric Dumazete86b29192012-03-18 11:06:44 +00004490 }
4491
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004492 /* Find place to insert this segment. Handle overlaps on the way. */
4493 parent = NULL;
4494 while (*p) {
4495 parent = *p;
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004496 skb1 = rb_to_skb(parent);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004497 if (before(seq, TCP_SKB_CB(skb1)->seq)) {
4498 p = &parent->rb_left;
4499 continue;
Eric Dumazete86b29192012-03-18 11:06:44 +00004500 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004501 if (before(seq, TCP_SKB_CB(skb1)->end_seq)) {
4502 if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
4503 /* All the bits are present. Drop. */
4504 NET_INC_STATS(sock_net(sk),
4505 LINUX_MIB_TCPOFOMERGE);
4506 __kfree_skb(skb);
4507 skb = NULL;
4508 tcp_dsack_set(sk, seq, end_seq);
4509 goto add_sack;
4510 }
4511 if (after(seq, TCP_SKB_CB(skb1)->seq)) {
4512 /* Partial overlap. */
4513 tcp_dsack_set(sk, seq, TCP_SKB_CB(skb1)->end_seq);
4514 } else {
4515 /* skb's seq == skb1's seq and skb covers skb1.
4516 * Replace skb1 with skb.
4517 */
4518 rb_replace_node(&skb1->rbnode, &skb->rbnode,
4519 &tp->out_of_order_queue);
4520 tcp_dsack_extend(sk,
4521 TCP_SKB_CB(skb1)->seq,
4522 TCP_SKB_CB(skb1)->end_seq);
4523 NET_INC_STATS(sock_net(sk),
4524 LINUX_MIB_TCPOFOMERGE);
4525 __kfree_skb(skb1);
Eric Dumazet76f0dcb2016-09-13 22:55:05 -07004526 goto merge_right;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004527 }
Eric Dumazetbffa72c2017-09-19 05:14:24 -07004528 } else if (tcp_try_coalesce(sk, skb1,
Mike Maloney98aaa912017-08-22 17:08:48 -04004529 skb, &fragstolen)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004530 goto coalesce_done;
4531 }
4532 p = &parent->rb_right;
Eric Dumazete86b29192012-03-18 11:06:44 +00004533 }
Eric Dumazet2594a2a2016-09-09 14:22:45 -07004534insert:
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004535 /* Insert segment into RB tree. */
4536 rb_link_node(&skb->rbnode, parent, p);
4537 rb_insert_color(&skb->rbnode, &tp->out_of_order_queue);
Eric Dumazete86b29192012-03-18 11:06:44 +00004538
Eric Dumazet76f0dcb2016-09-13 22:55:05 -07004539merge_right:
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004540 /* Remove other segments covered by skb. */
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004541 while ((skb1 = skb_rb_next(skb)) != NULL) {
Eric Dumazete86b29192012-03-18 11:06:44 +00004542 if (!after(end_seq, TCP_SKB_CB(skb1)->seq))
4543 break;
4544 if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
4545 tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
4546 end_seq);
4547 break;
4548 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004549 rb_erase(&skb1->rbnode, &tp->out_of_order_queue);
Eric Dumazete86b29192012-03-18 11:06:44 +00004550 tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
4551 TCP_SKB_CB(skb1)->end_seq);
Eric Dumazetc10d9312016-04-29 14:16:47 -07004552 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
Eric Dumazet532182c2016-04-01 08:52:19 -07004553 tcp_drop(sk, skb1);
Eric Dumazete86b29192012-03-18 11:06:44 +00004554 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004555 /* If there is no skb after us, we are the last_skb ! */
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004556 if (!skb1)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004557 tp->ooo_last_skb = skb;
Eric Dumazete86b29192012-03-18 11:06:44 +00004558
4559add_sack:
4560 if (tcp_is_sack(tp))
4561 tcp_sack_new_ofo_skb(sk, seq, end_seq);
4562end:
Eric Dumazet4e4f1fc2013-09-06 10:35:58 -07004563 if (skb) {
4564 tcp_grow_window(sk, skb);
Eric Dumazet60b1af32017-01-24 14:57:36 -08004565 skb_condense(skb);
Eric Dumazete86b29192012-03-18 11:06:44 +00004566 skb_set_owner_r(skb, sk);
Eric Dumazet4e4f1fc2013-09-06 10:35:58 -07004567 }
Eric Dumazete86b29192012-03-18 11:06:44 +00004568}
4569
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004570static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb, int hdrlen,
Eric Dumazetb081f852012-05-02 09:58:29 +00004571 bool *fragstolen)
4572{
4573 int eaten;
4574 struct sk_buff *tail = skb_peek_tail(&sk->sk_receive_queue);
4575
4576 __skb_pull(skb, hdrlen);
4577 eaten = (tail &&
Eric Dumazetbffa72c2017-09-19 05:14:24 -07004578 tcp_try_coalesce(sk, tail,
Mike Maloney98aaa912017-08-22 17:08:48 -04004579 skb, fragstolen)) ? 1 : 0;
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07004580 tcp_rcv_nxt_update(tcp_sk(sk), TCP_SKB_CB(skb)->end_seq);
Eric Dumazetb081f852012-05-02 09:58:29 +00004581 if (!eaten) {
4582 __skb_queue_tail(&sk->sk_receive_queue, skb);
4583 skb_set_owner_r(skb, sk);
4584 }
4585 return eaten;
4586}
Eric Dumazete86b29192012-03-18 11:06:44 +00004587
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004588int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size)
4589{
Eric Dumazetcb934712014-09-17 03:14:42 -07004590 struct sk_buff *skb;
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004591 int err = -ENOMEM;
4592 int data_len = 0;
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004593 bool fragstolen;
4594
Pavel Emelyanovc454e612012-10-29 05:05:33 +00004595 if (size == 0)
4596 return 0;
4597
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004598 if (size > PAGE_SIZE) {
4599 int npages = min_t(size_t, size >> PAGE_SHIFT, MAX_SKB_FRAGS);
4600
4601 data_len = npages << PAGE_SHIFT;
4602 size = data_len + (size & ~PAGE_MASK);
4603 }
4604 skb = alloc_skb_with_frags(size - data_len, data_len,
4605 PAGE_ALLOC_COSTLY_ORDER,
4606 &err, sk->sk_allocation);
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004607 if (!skb)
4608 goto err;
4609
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004610 skb_put(skb, size - data_len);
4611 skb->data_len = data_len;
4612 skb->len = size;
4613
Eric Dumazetcb934712014-09-17 03:14:42 -07004614 if (tcp_try_rmem_schedule(sk, skb, skb->truesize))
Mel Gormanc76562b2012-07-31 16:44:41 -07004615 goto err_free;
4616
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004617 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size);
4618 if (err)
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004619 goto err_free;
4620
4621 TCP_SKB_CB(skb)->seq = tcp_sk(sk)->rcv_nxt;
4622 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + size;
4623 TCP_SKB_CB(skb)->ack_seq = tcp_sk(sk)->snd_una - 1;
4624
Eric Dumazetcb934712014-09-17 03:14:42 -07004625 if (tcp_queue_rcv(sk, skb, 0, &fragstolen)) {
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004626 WARN_ON_ONCE(fragstolen); /* should not happen */
4627 __kfree_skb(skb);
4628 }
4629 return size;
4630
4631err_free:
4632 kfree_skb(skb);
4633err:
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004634 return err;
4635
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004636}
4637
Eric Dumazet03f45c82018-04-16 10:33:37 -07004638void tcp_data_ready(struct sock *sk)
4639{
4640 const struct tcp_sock *tp = tcp_sk(sk);
4641 int avail = tp->rcv_nxt - tp->copied_seq;
4642
4643 if (avail < sk->sk_rcvlowat && !sock_flag(sk, SOCK_DONE))
4644 return;
4645
4646 sk->sk_data_ready(sk);
4647}
4648
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
4650{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet5357f0b2017-08-01 07:02:44 -07004652 bool fragstolen;
4653 int eaten;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
Eric Dumazet532182c2016-04-01 08:52:19 -07004655 if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
4656 __kfree_skb(skb);
4657 return;
4658 }
Eric Dumazetf84af322010-04-28 15:31:51 -07004659 skb_dst_drop(skb);
Peter Pan(潘卫平)155c6e12014-09-24 22:17:02 +08004660 __skb_pull(skb, tcp_hdr(skb)->doff * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Florian Westphal735d3832014-09-29 13:08:30 +02004662 tcp_ecn_accept_cwr(tp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Ilpo Järvinen5861f8e2009-03-14 14:23:01 +00004664 tp->rx_opt.dsack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
4666 /* Queue data for delivery to the user.
4667 * Packets in sequence go to the receive queue.
4668 * Out of sequence packets to the out_of_order_queue.
4669 */
4670 if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {
4671 if (tcp_receive_window(tp) == 0)
4672 goto out_of_window;
4673
4674 /* Ok. In sequence. In window. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675queue_and_out:
Eric Dumazet5357f0b2017-08-01 07:02:44 -07004676 if (skb_queue_len(&sk->sk_receive_queue) == 0)
4677 sk_forced_mem_schedule(sk, skb->truesize);
4678 else if (tcp_try_rmem_schedule(sk, skb, skb->truesize))
4679 goto drop;
4680
Florian Westphalc13ee2a2017-07-30 03:57:19 +02004681 eaten = tcp_queue_rcv(sk, skb, 0, &fragstolen);
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07004682 tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
Stephen Hemminger2de979b2007-03-08 20:45:19 -08004683 if (skb->len)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07004684 tcp_event_data_recv(sk, skb);
Peter Pan(潘卫平)155c6e12014-09-24 22:17:02 +08004685 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
Eric Dumazet20c4cb72011-10-20 17:44:03 -04004686 tcp_fin(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004688 if (!RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 tcp_ofo_queue(sk);
4690
4691 /* RFC2581. 4.2. SHOULD send immediate ACK, when
4692 * gap in queue is filled.
4693 */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004694 if (RB_EMPTY_ROOT(&tp->out_of_order_queue))
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004695 inet_csk(sk)->icsk_ack.pingpong = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 }
4697
4698 if (tp->rx_opt.num_sacks)
4699 tcp_sack_remove(tp);
4700
Florian Westphal31770e32017-08-30 19:24:58 +02004701 tcp_fast_path_check(sk);
4702
Eric Dumazet923dd342012-05-02 07:55:58 +00004703 if (eaten > 0)
4704 kfree_skb_partial(skb, fragstolen);
Eric Dumazet1d57f192012-09-17 12:51:39 +00004705 if (!sock_flag(sk, SOCK_DEAD))
Eric Dumazet03f45c82018-04-16 10:33:37 -07004706 tcp_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 return;
4708 }
4709
4710 if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
4711 /* A retransmit, 2nd most common case. Force an immediate ack. */
Eric Dumazetc10d9312016-04-29 14:16:47 -07004712 NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004713 tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
4715out_of_window:
Eric Dumazet9a9c9b52018-05-21 15:08:56 -07004716 tcp_enter_quickack_mode(sk, TCP_MAX_QUICKACKS);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004717 inet_csk_schedule_ack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718drop:
Eric Dumazet532182c2016-04-01 08:52:19 -07004719 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 return;
4721 }
4722
4723 /* Out of window. F.e. zero window probe. */
4724 if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt + tcp_receive_window(tp)))
4725 goto out_of_window;
4726
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 if (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
4728 /* Partial packet, seq < rcv_next < end_seq */
4729 SOCK_DEBUG(sk, "partial packet: rcv_next %X seq %X - %X\n",
4730 tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
4731 TCP_SKB_CB(skb)->end_seq);
4732
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004733 tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, tp->rcv_nxt);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09004734
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 /* If window is closed, drop tail of packet. But after
4736 * remembering D-SACK for its head made in previous line.
4737 */
4738 if (!tcp_receive_window(tp))
4739 goto out_of_window;
4740 goto queue_and_out;
4741 }
4742
Eric Dumazete86b29192012-03-18 11:06:44 +00004743 tcp_data_queue_ofo(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744}
4745
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004746static struct sk_buff *tcp_skb_next(struct sk_buff *skb, struct sk_buff_head *list)
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004747{
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004748 if (list)
4749 return !skb_queue_is_last(list, skb) ? skb->next : NULL;
David S. Miller91521942009-05-28 21:35:47 -07004750
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004751 return skb_rb_next(skb);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004752}
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004753
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004754static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
4755 struct sk_buff_head *list,
4756 struct rb_root *root)
4757{
4758 struct sk_buff *next = tcp_skb_next(skb, list);
4759
4760 if (list)
4761 __skb_unlink(skb, list);
4762 else
4763 rb_erase(&skb->rbnode, root);
4764
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004765 __kfree_skb(skb);
Eric Dumazetc10d9312016-04-29 14:16:47 -07004766 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVCOLLAPSED);
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004767
4768 return next;
4769}
4770
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004771/* Insert skb into rb tree, ordered by TCP_SKB_CB(skb)->seq */
Eric Dumazet75c119a2017-10-05 22:21:27 -07004772void tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004773{
4774 struct rb_node **p = &root->rb_node;
4775 struct rb_node *parent = NULL;
4776 struct sk_buff *skb1;
4777
4778 while (*p) {
4779 parent = *p;
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004780 skb1 = rb_to_skb(parent);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004781 if (before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb1)->seq))
4782 p = &parent->rb_left;
4783 else
4784 p = &parent->rb_right;
4785 }
4786 rb_link_node(&skb->rbnode, parent, p);
4787 rb_insert_color(&skb->rbnode, root);
4788}
4789
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790/* Collapse contiguous sequence of skbs head..tail with
4791 * sequence numbers start..end.
David S. Miller91521942009-05-28 21:35:47 -07004792 *
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004793 * If tail is NULL, this means until the end of the queue.
David S. Miller91521942009-05-28 21:35:47 -07004794 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 * Segments with FIN/SYN are not collapsed (only because this
4796 * simplifies code)
4797 */
4798static void
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004799tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
4800 struct sk_buff *head, struct sk_buff *tail, u32 start, u32 end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004802 struct sk_buff *skb = head, *n;
4803 struct sk_buff_head tmp;
David S. Miller91521942009-05-28 21:35:47 -07004804 bool end_of_skbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08004806 /* First, check that queue is collapsible and find
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004807 * the point where collapsing can be useful.
4808 */
David S. Miller91521942009-05-28 21:35:47 -07004809restart:
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004810 for (end_of_skbs = true; skb != NULL && skb != tail; skb = n) {
4811 n = tcp_skb_next(skb, list);
4812
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 /* No new bits? It is possible on ofo queue. */
4814 if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004815 skb = tcp_collapse_one(sk, skb, list, root);
David S. Miller91521942009-05-28 21:35:47 -07004816 if (!skb)
4817 break;
4818 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 }
4820
4821 /* The first skb to collapse is:
4822 * - not SYN/FIN and
4823 * - bloated or contains data before "start" or
4824 * overlaps to the next one.
4825 */
Eric Dumazete11ecdd2014-09-15 04:19:51 -07004826 if (!(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) &&
Eric Dumazet94f08932017-10-26 21:55:09 -07004827 (tcp_win_from_space(sk, skb->truesize) > skb->len ||
David S. Miller91521942009-05-28 21:35:47 -07004828 before(TCP_SKB_CB(skb)->seq, start))) {
4829 end_of_skbs = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 break;
David S. Miller91521942009-05-28 21:35:47 -07004831 }
4832
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004833 if (n && n != tail &&
4834 TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(n)->seq) {
4835 end_of_skbs = false;
4836 break;
David S. Miller91521942009-05-28 21:35:47 -07004837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
4839 /* Decided to skip this, advance start seq. */
4840 start = TCP_SKB_CB(skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 }
Eric Dumazete11ecdd2014-09-15 04:19:51 -07004842 if (end_of_skbs ||
4843 (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 return;
4845
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004846 __skb_queue_head_init(&tmp);
4847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 while (before(start, end)) {
Eric Dumazetb3d6cb92014-09-15 04:19:53 -07004849 int copy = min_t(int, SKB_MAX_ORDER(0, 0), end - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 struct sk_buff *nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
Eric Dumazetb3d6cb92014-09-15 04:19:53 -07004852 nskb = alloc_skb(copy, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 if (!nskb)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004854 break;
Arnaldo Carvalho de Meloc51957d2007-03-10 12:47:22 -03004855
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
4857 TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004858 if (list)
4859 __skb_queue_before(list, skb, nskb);
4860 else
4861 __skb_queue_tail(&tmp, nskb); /* defer rbtree insertion */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08004862 skb_set_owner_r(nskb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
4864 /* Copy data, releasing collapsed skbs. */
4865 while (copy > 0) {
4866 int offset = start - TCP_SKB_CB(skb)->seq;
4867 int size = TCP_SKB_CB(skb)->end_seq - start;
4868
Kris Katterjohn09a62662006-01-08 22:24:28 -08004869 BUG_ON(offset < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 if (size > 0) {
4871 size = min(copy, size);
4872 if (skb_copy_bits(skb, offset, skb_put(nskb, size), size))
4873 BUG();
4874 TCP_SKB_CB(nskb)->end_seq += size;
4875 copy -= size;
4876 start += size;
4877 }
4878 if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004879 skb = tcp_collapse_one(sk, skb, list, root);
David S. Miller91521942009-05-28 21:35:47 -07004880 if (!skb ||
4881 skb == tail ||
Eric Dumazete11ecdd2014-09-15 04:19:51 -07004882 (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)))
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004883 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 }
4885 }
4886 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004887end:
4888 skb_queue_walk_safe(&tmp, skb, n)
4889 tcp_rbtree_insert(root, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890}
4891
4892/* Collapse ofo queue. Algorithm: select contiguous sequence of skbs
4893 * and tcp_collapse() them until all the queue is collapsed.
4894 */
4895static void tcp_collapse_ofo_queue(struct sock *sk)
4896{
4897 struct tcp_sock *tp = tcp_sk(sk);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004898 struct sk_buff *skb, *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 u32 start, end;
4900
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004901 skb = skb_rb_first(&tp->out_of_order_queue);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004902new_range:
4903 if (!skb) {
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004904 tp->ooo_last_skb = skb_rb_last(&tp->out_of_order_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 return;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 start = TCP_SKB_CB(skb)->seq;
4908 end = TCP_SKB_CB(skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004910 for (head = skb;;) {
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004911 skb = skb_rb_next(skb);
David S. Miller91521942009-05-28 21:35:47 -07004912
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004913 /* Range is terminated when we see a gap or when
4914 * we are at the queue end.
4915 */
David S. Miller91521942009-05-28 21:35:47 -07004916 if (!skb ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 after(TCP_SKB_CB(skb)->seq, end) ||
4918 before(TCP_SKB_CB(skb)->end_seq, start)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004919 tcp_collapse(sk, NULL, &tp->out_of_order_queue,
David S. Miller8728b832005-08-09 19:25:21 -07004920 head, skb, start, end);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004921 goto new_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004923
4924 if (unlikely(before(TCP_SKB_CB(skb)->seq, start)))
4925 start = TCP_SKB_CB(skb)->seq;
4926 if (after(TCP_SKB_CB(skb)->end_seq, end))
4927 end = TCP_SKB_CB(skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 }
4929}
4930
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004931/*
Eric Dumazet36a65032016-08-17 14:17:09 -07004932 * Clean the out-of-order queue to make room.
4933 * We drop high sequences packets to :
4934 * 1) Let a chance for holes to be filled.
4935 * 2) not add too big latencies if thousands of packets sit there.
4936 * (But if application shrinks SO_RCVBUF, we could still end up
4937 * freeing whole queue here)
4938 *
4939 * Return true if queue has shrunk.
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004940 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004941static bool tcp_prune_ofo_queue(struct sock *sk)
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004942{
4943 struct tcp_sock *tp = tcp_sk(sk);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004944 struct rb_node *node, *prev;
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004945
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004946 if (RB_EMPTY_ROOT(&tp->out_of_order_queue))
Eric Dumazet36a65032016-08-17 14:17:09 -07004947 return false;
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004948
Eric Dumazet36a65032016-08-17 14:17:09 -07004949 NET_INC_STATS(sock_net(sk), LINUX_MIB_OFOPRUNED);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004950 node = &tp->ooo_last_skb->rbnode;
4951 do {
4952 prev = rb_prev(node);
4953 rb_erase(node, &tp->out_of_order_queue);
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004954 tcp_drop(sk, rb_to_skb(node));
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004955 sk_mem_reclaim(sk);
Eric Dumazet36a65032016-08-17 14:17:09 -07004956 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
4957 !tcp_under_memory_pressure(sk))
4958 break;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004959 node = prev;
4960 } while (node);
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004961 tp->ooo_last_skb = rb_to_skb(prev);
Eric Dumazet36a65032016-08-17 14:17:09 -07004962
4963 /* Reset SACK state. A conforming SACK implementation will
4964 * do the same at a timeout based retransmit. When a connection
4965 * is in a sad state like this, we care only about integrity
4966 * of the connection not performance.
4967 */
4968 if (tp->rx_opt.sack_ok)
4969 tcp_sack_reset(&tp->rx_opt);
4970 return true;
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004971}
4972
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973/* Reduce allocated memory if we can, trying to get
4974 * the socket within its memory limits again.
4975 *
4976 * Return less than zero if we should start dropping frames
4977 * until the socket owning process reads some of the data
4978 * to stabilize the situation.
4979 */
4980static int tcp_prune_queue(struct sock *sk)
4981{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09004982 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
4984 SOCK_DEBUG(sk, "prune_queue: c=%x\n", tp->copied_seq);
4985
Eric Dumazetc10d9312016-04-29 14:16:47 -07004986 NET_INC_STATS(sock_net(sk), LINUX_MIB_PRUNECALLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
4988 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07004989 tcp_clamp_window(sk);
Eric Dumazetb8da51e2015-05-15 12:39:27 -07004990 else if (tcp_under_memory_pressure(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss);
4992
4993 tcp_collapse_ofo_queue(sk);
David S. Miller91521942009-05-28 21:35:47 -07004994 if (!skb_queue_empty(&sk->sk_receive_queue))
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004995 tcp_collapse(sk, &sk->sk_receive_queue, NULL,
David S. Miller91521942009-05-28 21:35:47 -07004996 skb_peek(&sk->sk_receive_queue),
4997 NULL,
4998 tp->copied_seq, tp->rcv_nxt);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08004999 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000
5001 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
5002 return 0;
5003
5004 /* Collapsing did not help, destructive actions follow.
5005 * This must not ever occur. */
5006
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07005007 tcp_prune_ofo_queue(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
5009 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
5010 return 0;
5011
5012 /* If we are really being abused, tell the caller to silently
5013 * drop receive data on the floor. It will get retransmitted
5014 * and hopefully then we'll have sufficient space.
5015 */
Eric Dumazetc10d9312016-04-29 14:16:47 -07005016 NET_INC_STATS(sock_net(sk), LINUX_MIB_RCVPRUNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
5018 /* Massive buffer overcommit. */
Florian Westphal31770e32017-08-30 19:24:58 +02005019 tp->pred_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 return -1;
5021}
5022
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005023static bool tcp_should_expand_sndbuf(const struct sock *sk)
David S. Miller0d9901d2005-07-05 15:21:10 -07005024{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04005025 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005026
David S. Miller0d9901d2005-07-05 15:21:10 -07005027 /* If the user specified a specific send buffer setting, do
5028 * not modify it.
5029 */
5030 if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005031 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005032
5033 /* If we are under global TCP memory pressure, do not expand. */
Eric Dumazetb8da51e2015-05-15 12:39:27 -07005034 if (tcp_under_memory_pressure(sk))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005035 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005036
5037 /* If we are under soft global TCP memory pressure, do not expand. */
Glauber Costa180d8cd2011-12-11 21:47:02 +00005038 if (sk_memory_allocated(sk) >= sk_prot_mem_limits(sk, 0))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005039 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005040
5041 /* If we filled the congestion window, do not expand. */
Neal Cardwell65148902015-02-20 13:33:16 -05005042 if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005043 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005044
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005045 return true;
David S. Miller0d9901d2005-07-05 15:21:10 -07005046}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
5048/* When incoming ACK allowed to free some skb from write_queue,
5049 * we remember this event in flag SOCK_QUEUE_SHRUNK and wake up socket
5050 * on the exit from tcp input handler.
5051 *
5052 * PROBLEM: sndbuf expansion does not work well with largesend.
5053 */
5054static void tcp_new_space(struct sock *sk)
5055{
5056 struct tcp_sock *tp = tcp_sk(sk);
5057
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005058 if (tcp_should_expand_sndbuf(sk)) {
Eric Dumazet6ae70532013-10-01 10:23:44 -07005059 tcp_sndbuf_expand(sk);
Eric Dumazetc2203cf2017-05-16 14:00:04 -07005060 tp->snd_cwnd_stamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 }
5062
5063 sk->sk_write_space(sk);
5064}
5065
Stephen Hemminger40efc6f2006-01-03 16:03:49 -08005066static void tcp_check_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067{
5068 if (sock_flag(sk, SOCK_QUEUE_SHRUNK)) {
5069 sock_reset_flag(sk, SOCK_QUEUE_SHRUNK);
jbaron@akamai.com3c715122015-04-20 20:05:07 +00005070 /* pairs with tcp_poll() */
Jason Baron56d80622017-01-24 21:49:41 -05005071 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 if (sk->sk_socket &&
Francis Yanb0f71bd2016-11-27 23:07:16 -08005073 test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 tcp_new_space(sk);
Francis Yanb0f71bd2016-11-27 23:07:16 -08005075 if (!test_bit(SOCK_NOSPACE, &sk->sk_socket->flags))
5076 tcp_chrono_stop(sk, TCP_CHRONO_SNDBUF_LIMITED);
5077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 }
5079}
5080
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005081static inline void tcp_data_snd_check(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005083 tcp_push_pending_frames(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 tcp_check_space(sk);
5085}
5086
5087/*
5088 * Check if sending an ack is needed.
5089 */
5090static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible)
5091{
5092 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet5d9f4262018-05-17 14:47:26 -07005093 unsigned long rtt, delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094
5095 /* More than one full frame received... */
Joe Perches9d4fb272009-11-23 10:41:23 -08005096 if (((tp->rcv_nxt - tp->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 /* ... and right edge of window advances far enough.
Eric Dumazet796f82e2018-04-16 10:33:36 -07005098 * (tcp_recvmsg() will send ACK otherwise).
5099 * If application uses SO_RCVLOWAT, we want send ack now if
5100 * we have not received enough bytes to satisfy the condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 */
Eric Dumazet796f82e2018-04-16 10:33:36 -07005102 (tp->rcv_nxt - tp->copied_seq < sk->sk_rcvlowat ||
5103 __tcp_select_window(sk) >= tp->rcv_wnd)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 /* We ACK each frame or... */
Eric Dumazet5d9f4262018-05-17 14:47:26 -07005105 tcp_in_quickack_mode(sk)) {
5106send_now:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 tcp_send_ack(sk);
Eric Dumazet5d9f4262018-05-17 14:47:26 -07005108 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 }
Eric Dumazet5d9f4262018-05-17 14:47:26 -07005110
5111 if (!ofo_possible || RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
5112 tcp_send_delayed_ack(sk);
5113 return;
5114 }
5115
Eric Dumazet9c21d2f2018-05-17 14:47:29 -07005116 if (!tcp_is_sack(tp) ||
5117 tp->compressed_ack >= sock_net(sk)->ipv4.sysctl_tcp_comp_sack_nr)
Eric Dumazet5d9f4262018-05-17 14:47:26 -07005118 goto send_now;
5119 tp->compressed_ack++;
5120
5121 if (hrtimer_is_queued(&tp->compressed_ack_timer))
5122 return;
5123
Eric Dumazet6d82aa22018-05-17 14:47:28 -07005124 /* compress ack timer : 5 % of rtt, but no more than tcp_comp_sack_delay_ns */
Eric Dumazet5d9f4262018-05-17 14:47:26 -07005125
5126 rtt = tp->rcv_rtt_est.rtt_us;
5127 if (tp->srtt_us && tp->srtt_us < rtt)
5128 rtt = tp->srtt_us;
5129
Eric Dumazet6d82aa22018-05-17 14:47:28 -07005130 delay = min_t(unsigned long, sock_net(sk)->ipv4.sysctl_tcp_comp_sack_delay_ns,
Eric Dumazet5d9f4262018-05-17 14:47:26 -07005131 rtt * (NSEC_PER_USEC >> 3)/20);
5132 sock_hold(sk);
5133 hrtimer_start(&tp->compressed_ack_timer, ns_to_ktime(delay),
5134 HRTIMER_MODE_REL_PINNED_SOFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135}
5136
Stephen Hemminger40efc6f2006-01-03 16:03:49 -08005137static inline void tcp_ack_snd_check(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005139 if (!inet_csk_ack_scheduled(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 /* We sent a data segment already. */
5141 return;
5142 }
5143 __tcp_ack_snd_check(sk, 1);
5144}
5145
5146/*
5147 * This routine is only called when we have urgent data
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08005148 * signaled. Its the 'slow' part of tcp_urg. It could be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 * moved inline now as tcp_urg is only called from one
5150 * place. We handle URGent data wrong. We have to - as
5151 * BSD still doesn't use the correction from RFC961.
5152 * For 1003.1g we should support a new option TCP_STDURG to permit
5153 * either form (or just set the sysctl tcp_stdurg).
5154 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005155
Eric Dumazetcf533ea2011-10-21 05:22:42 -04005156static void tcp_check_urg(struct sock *sk, const struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157{
5158 struct tcp_sock *tp = tcp_sk(sk);
5159 u32 ptr = ntohs(th->urg_ptr);
5160
Eric Dumazet3f4c7c62017-10-26 21:55:01 -07005161 if (ptr && !sock_net(sk)->ipv4.sysctl_tcp_stdurg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 ptr--;
5163 ptr += ntohl(th->seq);
5164
5165 /* Ignore urgent data that we've already seen and read. */
5166 if (after(tp->copied_seq, ptr))
5167 return;
5168
5169 /* Do not replay urg ptr.
5170 *
5171 * NOTE: interesting situation not covered by specs.
5172 * Misbehaving sender may send urg ptr, pointing to segment,
5173 * which we already have in ofo queue. We are not able to fetch
5174 * such data and will stay in TCP_URG_NOTYET until will be eaten
5175 * by recvmsg(). Seems, we are not obliged to handle such wicked
5176 * situations. But it is worth to think about possibility of some
5177 * DoSes using some hypothetical application level deadlock.
5178 */
5179 if (before(ptr, tp->rcv_nxt))
5180 return;
5181
5182 /* Do we already have a newer (or duplicate) urgent pointer? */
5183 if (tp->urg_data && !after(ptr, tp->urg_seq))
5184 return;
5185
5186 /* Tell the world about our new urgent pointer. */
5187 sk_send_sigurg(sk);
5188
5189 /* We may be adding urgent data when the last byte read was
5190 * urgent. To do this requires some care. We cannot just ignore
5191 * tp->copied_seq since we would read the last urgent byte again
5192 * as data, nor can we alter copied_seq until this data arrives
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08005193 * or we break the semantics of SIOCATMARK (and thus sockatmark())
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 *
5195 * NOTE. Double Dutch. Rendering to plain English: author of comment
5196 * above did something sort of send("A", MSG_OOB); send("B", MSG_OOB);
5197 * and expect that both A and B disappear from stream. This is _wrong_.
5198 * Though this happens in BSD with high probability, this is occasional.
5199 * Any application relying on this is buggy. Note also, that fix "works"
5200 * only in this artificial test. Insert some normal data between A and B and we will
5201 * decline of BSD again. Verdict: it is better to remove to trap
5202 * buggy users.
5203 */
5204 if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005205 !sock_flag(sk, SOCK_URGINLINE) && tp->copied_seq != tp->rcv_nxt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
5207 tp->copied_seq++;
5208 if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) {
David S. Miller8728b832005-08-09 19:25:21 -07005209 __skb_unlink(skb, &sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 __kfree_skb(skb);
5211 }
5212 }
5213
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005214 tp->urg_data = TCP_URG_NOTYET;
5215 tp->urg_seq = ptr;
Florian Westphal31770e32017-08-30 19:24:58 +02005216
5217 /* Disable header prediction. */
5218 tp->pred_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219}
5220
5221/* This is the 'fast' part of urgent handling. */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04005222static void tcp_urg(struct sock *sk, struct sk_buff *skb, const struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223{
5224 struct tcp_sock *tp = tcp_sk(sk);
5225
5226 /* Check if we get a new urgent pointer - normally not. */
5227 if (th->urg)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005228 tcp_check_urg(sk, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229
5230 /* Do we wait for any urgent data? - normally not... */
5231 if (tp->urg_data == TCP_URG_NOTYET) {
5232 u32 ptr = tp->urg_seq - ntohl(th->seq) + (th->doff * 4) -
5233 th->syn;
5234
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005235 /* Is the urgent pointer pointing into this packet? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 if (ptr < skb->len) {
5237 u8 tmp;
5238 if (skb_copy_bits(skb, ptr, &tmp, 1))
5239 BUG();
5240 tp->urg_data = TCP_URG_VALID | tmp;
5241 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04005242 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 }
5244 }
5245}
5246
Jason Baron0e40f4c2017-01-17 13:37:19 -05005247/* Accept RST for rcv_nxt - 1 after a FIN.
5248 * When tcp connections are abruptly terminated from Mac OSX (via ^C), a
5249 * FIN is sent followed by a RST packet. The RST is sent with the same
5250 * sequence number as the FIN, and thus according to RFC 5961 a challenge
5251 * ACK should be sent. However, Mac OSX rate limits replies to challenge
5252 * ACKs on the closed socket. In addition middleboxes can drop either the
5253 * challenge ACK or a subsequent RST.
5254 */
5255static bool tcp_reset_check(const struct sock *sk, const struct sk_buff *skb)
5256{
5257 struct tcp_sock *tp = tcp_sk(sk);
5258
5259 return unlikely(TCP_SKB_CB(skb)->seq == (tp->rcv_nxt - 1) &&
5260 (1 << sk->sk_state) & (TCPF_CLOSE_WAIT | TCPF_LAST_ACK |
5261 TCPF_CLOSING));
5262}
5263
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005264/* Does PAWS and seqno based validation of an incoming segment, flags will
5265 * play significant role here.
5266 */
Eric Dumazet0c246042012-07-17 01:41:30 +00005267static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,
5268 const struct tcphdr *th, int syn_inerr)
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005269{
5270 struct tcp_sock *tp = tcp_sk(sk);
Pau Espin Pedrole00431b2016-06-07 16:30:34 +02005271 bool rst_seq_match = false;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005272
5273 /* RFC1323: H1. Apply PAWS check first. */
Eric Dumazeteed29f12017-06-07 10:34:36 -07005274 if (tcp_fast_parse_options(sock_net(sk), skb, th, tp) &&
5275 tp->rx_opt.saw_tstamp &&
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005276 tcp_paws_discard(sk, skb)) {
5277 if (!th->rst) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07005278 NET_INC_STATS(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005279 if (!tcp_oow_rate_limited(sock_net(sk), skb,
5280 LINUX_MIB_TCPACKSKIPPEDPAWS,
5281 &tp->last_oow_ack_time))
5282 tcp_send_dupack(sk, skb);
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005283 goto discard;
5284 }
5285 /* Reset is accepted even if it did not pass PAWS. */
5286 }
5287
5288 /* Step 1: check sequence number */
5289 if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) {
5290 /* RFC793, page 37: "In all states except SYN-SENT, all reset
5291 * (RST) segments are validated by checking their SEQ-fields."
5292 * And page 69: "If an incoming segment is not acceptable,
5293 * an acknowledgment should be sent in reply (unless the RST
5294 * bit is set, if so drop the segment and return)".
5295 */
Eric Dumazete3715892012-07-17 12:29:30 +00005296 if (!th->rst) {
5297 if (th->syn)
5298 goto syn_challenge;
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005299 if (!tcp_oow_rate_limited(sock_net(sk), skb,
5300 LINUX_MIB_TCPACKSKIPPEDSEQ,
5301 &tp->last_oow_ack_time))
5302 tcp_send_dupack(sk, skb);
Jason Baron0e40f4c2017-01-17 13:37:19 -05005303 } else if (tcp_reset_check(sk, skb)) {
5304 tcp_reset(sk);
Eric Dumazete3715892012-07-17 12:29:30 +00005305 }
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005306 goto discard;
5307 }
5308
5309 /* Step 2: check RST bit */
5310 if (th->rst) {
Jason Baron0e40f4c2017-01-17 13:37:19 -05005311 /* RFC 5961 3.2 (extend to match against (RCV.NXT - 1) after a
5312 * FIN and SACK too if available):
5313 * If seq num matches RCV.NXT or (RCV.NXT - 1) after a FIN, or
5314 * the right-most SACK block,
Pau Espin Pedrole00431b2016-06-07 16:30:34 +02005315 * then
Eric Dumazet282f23c2012-07-17 10:13:05 +02005316 * RESET the connection
5317 * else
5318 * Send a challenge ACK
5319 */
Jason Baron0e40f4c2017-01-17 13:37:19 -05005320 if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt ||
5321 tcp_reset_check(sk, skb)) {
Pau Espin Pedrole00431b2016-06-07 16:30:34 +02005322 rst_seq_match = true;
5323 } else if (tcp_is_sack(tp) && tp->rx_opt.num_sacks > 0) {
5324 struct tcp_sack_block *sp = &tp->selective_acks[0];
5325 int max_sack = sp[0].end_seq;
5326 int this_sack;
5327
5328 for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;
5329 ++this_sack) {
5330 max_sack = after(sp[this_sack].end_seq,
5331 max_sack) ?
5332 sp[this_sack].end_seq : max_sack;
5333 }
5334
5335 if (TCP_SKB_CB(skb)->seq == max_sack)
5336 rst_seq_match = true;
5337 }
5338
5339 if (rst_seq_match)
Eric Dumazet282f23c2012-07-17 10:13:05 +02005340 tcp_reset(sk);
Wei Wangcf1ef3f2017-04-20 14:45:46 -07005341 else {
5342 /* Disable TFO if RST is out-of-order
5343 * and no data has been received
5344 * for current active TFO socket
5345 */
5346 if (tp->syn_fastopen && !tp->data_segs_in &&
5347 sk->sk_state == TCP_ESTABLISHED)
Wei Wang46c2fa32017-04-20 14:45:47 -07005348 tcp_fastopen_active_disable(sk);
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005349 tcp_send_challenge_ack(sk, skb);
Wei Wangcf1ef3f2017-04-20 14:45:46 -07005350 }
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005351 goto discard;
5352 }
5353
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005354 /* step 3: check security and precedence [ignored] */
5355
Eric Dumazet0c246042012-07-17 01:41:30 +00005356 /* step 4: Check for a SYN
Sowmini Varadhancd214532014-10-30 12:48:08 -04005357 * RFC 5961 4.2 : Send a challenge ack
Eric Dumazet0c246042012-07-17 01:41:30 +00005358 */
5359 if (th->syn) {
Eric Dumazete3715892012-07-17 12:29:30 +00005360syn_challenge:
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005361 if (syn_inerr)
Eric Dumazetc10d9312016-04-29 14:16:47 -07005362 TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
5363 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSYNCHALLENGE);
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005364 tcp_send_challenge_ack(sk, skb);
Eric Dumazet0c246042012-07-17 01:41:30 +00005365 goto discard;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005366 }
5367
Eric Dumazet0c246042012-07-17 01:41:30 +00005368 return true;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005369
5370discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07005371 tcp_drop(sk, skb);
Eric Dumazet0c246042012-07-17 01:41:30 +00005372 return false;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005373}
5374
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375/*
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005376 * TCP receive function for the ESTABLISHED state.
Florian Westphal31770e32017-08-30 19:24:58 +02005377 *
5378 * It is split into a fast path and a slow path. The fast path is
5379 * disabled when:
5380 * - A zero window was announced from us - zero window probing
5381 * is only handled properly in the slow path.
5382 * - Out of order segments arrived.
5383 * - Urgent data is expected.
5384 * - There is no buffer space left
5385 * - Unexpected TCP flags/window values/header lengths are received
5386 * (detected by checking the TCP header against pred_flags)
5387 * - Data is sent in both directions. Fast path only supports pure senders
5388 * or pure receivers (this means either the sequence number or the ack
5389 * value must stay constant)
5390 * - Unexpected TCP option.
5391 *
5392 * When these conditions are not satisfied it drops into a standard
5393 * receive procedure patterned after RFC793 to handle all cases.
5394 * The first three cases are guaranteed by proper pred_flags setting,
5395 * the rest is checked inline. Fast processing is turned on in
5396 * tcp_data_queue when everything is OK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 */
Yafang Shao3d97d882018-05-29 23:27:31 +08005398void tcp_rcv_established(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399{
Yafang Shao3d97d882018-05-29 23:27:31 +08005400 const struct tcphdr *th = (const struct tcphdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 struct tcp_sock *tp = tcp_sk(sk);
Yafang Shao3d97d882018-05-29 23:27:31 +08005402 unsigned int len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
Masami Hiramatsuc3fde1b2017-12-29 11:45:51 +09005404 /* TCP congestion window tracking */
5405 trace_tcp_probe(sk, skb);
5406
Eric Dumazet9a568de2017-05-16 14:00:14 -07005407 tcp_mstamp_refresh(tp);
Ian Morris51456b22015-04-03 09:17:26 +01005408 if (unlikely(!sk->sk_rx_dst))
Eric Dumazet5d299f32012-08-06 05:09:33 +00005409 inet_csk(sk)->icsk_af_ops->sk_rx_dst_set(sk, skb);
Florian Westphal31770e32017-08-30 19:24:58 +02005410 /*
5411 * Header prediction.
5412 * The code loosely follows the one in the famous
5413 * "30 instruction TCP receive" Van Jacobson mail.
5414 *
5415 * Van's trick is to deposit buffers into socket queue
5416 * on a device interrupt, to call tcp_recv function
5417 * on the receive process context and checksum and copy
5418 * the buffer to user space. smart...
5419 *
5420 * Our current scheme is not silly either but we take the
5421 * extra cost of the net_bh soft interrupt processing...
5422 * We do checksum and copy also but from device to kernel.
5423 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424
5425 tp->rx_opt.saw_tstamp = 0;
5426
Florian Westphal31770e32017-08-30 19:24:58 +02005427 /* pred_flags is 0xS?10 << 16 + snd_wnd
5428 * if header_prediction is to be made
5429 * 'S' will always be tp->tcp_header_len >> 2
5430 * '?' will be 0 for the fast path, otherwise pred_flags is 0 to
5431 * turn it off (when there are holes in the receive
5432 * space for instance)
5433 * PSH flag is ignored.
5434 */
5435
5436 if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags &&
5437 TCP_SKB_CB(skb)->seq == tp->rcv_nxt &&
5438 !after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) {
5439 int tcp_header_len = tp->tcp_header_len;
5440
5441 /* Timestamp header prediction: tcp_header_len
5442 * is automatically equal to th->doff*4 due to pred_flags
5443 * match.
5444 */
5445
5446 /* Check timestamp */
5447 if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) {
5448 /* No? Slow path! */
5449 if (!tcp_parse_aligned_timestamp(tp, th))
5450 goto slow_path;
5451
5452 /* If PAWS failed, check it more carefully in slow path */
5453 if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) < 0)
5454 goto slow_path;
5455
5456 /* DO NOT update ts_recent here, if checksum fails
5457 * and timestamp was corrupted part, it will result
5458 * in a hung connection since we will drop all
5459 * future packets due to the PAWS test.
5460 */
5461 }
5462
5463 if (len <= tcp_header_len) {
5464 /* Bulk data transfer: sender */
5465 if (len == tcp_header_len) {
5466 /* Predicted packet is in window by definition.
5467 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
5468 * Hence, check seq<=rcv_wup reduces to:
5469 */
5470 if (tcp_header_len ==
5471 (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
5472 tp->rcv_nxt == tp->rcv_wup)
5473 tcp_store_ts_recent(tp);
5474
5475 /* We know that such packets are checksummed
5476 * on entry.
5477 */
5478 tcp_ack(sk, skb, 0);
5479 __kfree_skb(skb);
5480 tcp_data_snd_check(sk);
Wei Wang3f6c65d2018-06-19 21:42:50 -07005481 /* When receiving pure ack in fast path, update
5482 * last ts ecr directly instead of calling
5483 * tcp_rcv_rtt_measure_ts()
5484 */
5485 tp->rcv_rtt_last_tsecr = tp->rx_opt.rcv_tsecr;
Florian Westphal31770e32017-08-30 19:24:58 +02005486 return;
5487 } else { /* Header too small */
5488 TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
5489 goto discard;
5490 }
5491 } else {
5492 int eaten = 0;
5493 bool fragstolen = false;
5494
5495 if (tcp_checksum_complete(skb))
5496 goto csum_error;
5497
5498 if ((int)skb->truesize > sk->sk_forward_alloc)
5499 goto step5;
5500
5501 /* Predicted packet is in window by definition.
5502 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
5503 * Hence, check seq<=rcv_wup reduces to:
5504 */
5505 if (tcp_header_len ==
5506 (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
5507 tp->rcv_nxt == tp->rcv_wup)
5508 tcp_store_ts_recent(tp);
5509
5510 tcp_rcv_rtt_measure_ts(sk, skb);
5511
5512 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPHPHITS);
5513
5514 /* Bulk data transfer: receiver */
5515 eaten = tcp_queue_rcv(sk, skb, tcp_header_len,
5516 &fragstolen);
5517
5518 tcp_event_data_recv(sk, skb);
5519
5520 if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) {
5521 /* Well, only one small jumplet in fast path... */
5522 tcp_ack(sk, skb, FLAG_DATA);
5523 tcp_data_snd_check(sk);
5524 if (!inet_csk_ack_scheduled(sk))
5525 goto no_ack;
5526 }
5527
5528 __tcp_ack_snd_check(sk, 0);
5529no_ack:
5530 if (eaten)
5531 kfree_skb_partial(skb, fragstolen);
Eric Dumazet03f45c82018-04-16 10:33:37 -07005532 tcp_data_ready(sk);
Florian Westphal31770e32017-08-30 19:24:58 +02005533 return;
5534 }
5535 }
5536
5537slow_path:
Eric Dumazetfb3477c2016-04-29 14:16:48 -07005538 if (len < (th->doff << 2) || tcp_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 goto csum_error;
5540
Calvin Owens0c228e82014-11-20 15:09:53 -08005541 if (!th->ack && !th->rst && !th->syn)
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00005542 goto discard;
5543
Florian Westphal31770e32017-08-30 19:24:58 +02005544 /*
5545 * Standard slow path.
5546 */
5547
Eric Dumazet0c246042012-07-17 01:41:30 +00005548 if (!tcp_validate_incoming(sk, skb, th, 1))
Vijay Subramanianc995ae22013-09-03 12:23:22 -07005549 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
Florian Westphal31770e32017-08-30 19:24:58 +02005551step5:
5552 if (tcp_ack(sk, skb, FLAG_SLOWPATH | FLAG_UPDATE_TS_RECENT) < 0)
John Dykstra96e0bf42009-03-22 21:49:57 -07005553 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005555 tcp_rcv_rtt_measure_ts(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
5557 /* Process urgent data. */
5558 tcp_urg(sk, skb, th);
5559
5560 /* step 7: process the segment text */
5561 tcp_data_queue(sk, skb);
5562
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005563 tcp_data_snd_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 tcp_ack_snd_check(sk);
Vijay Subramanianc995ae22013-09-03 12:23:22 -07005565 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
5567csum_error:
Eric Dumazetc10d9312016-04-29 14:16:47 -07005568 TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS);
5569 TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570
5571discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07005572 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00005574EXPORT_SYMBOL(tcp_rcv_established);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005576void tcp_finish_connect(struct sock *sk, struct sk_buff *skb)
5577{
5578 struct tcp_sock *tp = tcp_sk(sk);
5579 struct inet_connection_sock *icsk = inet_csk(sk);
5580
5581 tcp_set_state(sk, TCP_ESTABLISHED);
Eric Dumazet70eabf02017-05-16 14:00:07 -07005582 icsk->icsk_ack.lrcvtime = tcp_jiffies32;
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005583
Ian Morris00db4122015-04-03 09:17:27 +01005584 if (skb) {
Eric Dumazet5d299f32012-08-06 05:09:33 +00005585 icsk->icsk_af_ops->sk_rx_dst_set(sk, skb);
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005586 security_inet_conn_established(sk, skb);
David S. Miller41063e92012-06-19 21:22:05 -07005587 }
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005588
Wei Wang27204aa2017-10-04 10:03:44 -07005589 tcp_init_transfer(sk, BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB);
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005590
5591 /* Prevent spurious tcp_cwnd_restart() on first data
5592 * packet.
5593 */
Eric Dumazetd635fbe2017-05-16 14:00:03 -07005594 tp->lsndtime = tcp_jiffies32;
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005595
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005596 if (sock_flag(sk, SOCK_KEEPOPEN))
5597 inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tp));
Florian Westphal31770e32017-08-30 19:24:58 +02005598
5599 if (!tp->rx_opt.snd_wscale)
5600 __tcp_fast_path_on(tp, tp->snd_wnd);
5601 else
5602 tp->pred_flags = 0;
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005603}
5604
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005605static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,
5606 struct tcp_fastopen_cookie *cookie)
5607{
5608 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet75c119a2017-10-05 22:21:27 -07005609 struct sk_buff *data = tp->syn_data ? tcp_rtx_queue_head(sk) : NULL;
Daniel Lee2646c832015-04-06 14:37:27 -07005610 u16 mss = tp->rx_opt.mss_clamp, try_exp = 0;
5611 bool syn_drop = false;
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005612
5613 if (mss == tp->rx_opt.user_mss) {
5614 struct tcp_options_received opt;
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005615
5616 /* Get original SYNACK MSS value if user MSS sets mss_clamp */
5617 tcp_clear_options(&opt);
5618 opt.user_mss = opt.mss_clamp = 0;
Eric Dumazeteed29f12017-06-07 10:34:36 -07005619 tcp_parse_options(sock_net(sk), synack, &opt, 0, NULL);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005620 mss = opt.mss_clamp;
5621 }
5622
Daniel Lee2646c832015-04-06 14:37:27 -07005623 if (!tp->syn_fastopen) {
5624 /* Ignore an unsolicited cookie */
Yuchung Cheng67da22d2012-07-19 06:43:11 +00005625 cookie->len = -1;
Daniel Lee2646c832015-04-06 14:37:27 -07005626 } else if (tp->total_retrans) {
5627 /* SYN timed out and the SYN-ACK neither has a cookie nor
5628 * acknowledges data. Presumably the remote received only
5629 * the retransmitted (regular) SYNs: either the original
5630 * SYN-data or the corresponding SYN-ACK was dropped.
5631 */
5632 syn_drop = (cookie->len < 0 && data);
5633 } else if (cookie->len < 0 && !tp->syn_data) {
5634 /* We requested a cookie but didn't get it. If we did not use
5635 * the (old) exp opt format then try so next time (try_exp=1).
5636 * Otherwise we go back to use the RFC7413 opt (try_exp=2).
5637 */
5638 try_exp = tp->syn_fastopen_exp ? 2 : 1;
5639 }
Yuchung Cheng67da22d2012-07-19 06:43:11 +00005640
Daniel Lee2646c832015-04-06 14:37:27 -07005641 tcp_fastopen_cache_set(sk, mss, cookie, syn_drop, try_exp);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005642
5643 if (data) { /* Retransmit unacked data in SYN */
Eric Dumazet75c119a2017-10-05 22:21:27 -07005644 skb_rbtree_walk_from(data) {
5645 if (__tcp_retransmit_skb(sk, data, 1))
Yuchung Cheng93b174a2012-12-06 08:45:32 +00005646 break;
5647 }
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005648 tcp_rearm_rto(sk);
Eric Dumazetc10d9312016-04-29 14:16:47 -07005649 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07005650 LINUX_MIB_TCPFASTOPENACTIVEFAIL);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005651 return true;
5652 }
Yuchung Cheng6f736012012-10-19 15:14:44 +00005653 tp->syn_data_acked = tp->syn_data;
Yuchung Chengbef57672018-04-17 23:18:46 -07005654 if (tp->syn_data_acked) {
5655 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVE);
5656 /* SYN-data is counted as two separate packets in tcp_ack() */
5657 if (tp->delivered > 1)
5658 --tp->delivered;
5659 }
Eric Dumazet61d2bca2016-02-01 21:03:07 -08005660
5661 tcp_fastopen_add_skb(sk, synack);
5662
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005663 return false;
5664}
5665
Ursula Braun60e2a772017-10-25 11:01:45 +02005666static void smc_check_reset_syn(struct tcp_sock *tp)
5667{
5668#if IS_ENABLED(CONFIG_SMC)
5669 if (static_branch_unlikely(&tcp_have_smc)) {
5670 if (tp->syn_smc && !tp->rx_opt.smc_ok)
5671 tp->syn_smc = 0;
5672 }
5673#endif
5674}
5675
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
Eric Dumazetbda07a62015-09-29 07:42:40 -07005677 const struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678{
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08005679 struct inet_connection_sock *icsk = inet_csk(sk);
William Allen Simpson4957faade2009-12-02 18:25:27 +00005680 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005681 struct tcp_fastopen_cookie foc = { .len = -1 };
William Allen Simpson4957faade2009-12-02 18:25:27 +00005682 int saved_clamp = tp->rx_opt.mss_clamp;
Eric Dumazet0f9fa832017-04-18 09:45:52 -07005683 bool fastopen_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684
Eric Dumazeteed29f12017-06-07 10:34:36 -07005685 tcp_parse_options(sock_net(sk), skb, &tp->rx_opt, 0, &foc);
Andrew Vagine3e12022013-08-27 12:21:55 +04005686 if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
Andrey Vaginee684b62013-02-11 05:50:19 +00005687 tp->rx_opt.rcv_tsecr -= tp->tsoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
5689 if (th->ack) {
5690 /* rfc793:
5691 * "If the state is SYN-SENT then
5692 * first check the ACK bit
5693 * If the ACK bit is set
5694 * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send
5695 * a reset (unless the RST bit is set, if so drop
5696 * the segment and return)"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 */
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005698 if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) ||
5699 after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 goto reset_and_undo;
5701
5702 if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
5703 !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp,
Eric Dumazet9a568de2017-05-16 14:00:14 -07005704 tcp_time_stamp(tp))) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07005705 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07005706 LINUX_MIB_PAWSACTIVEREJECTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 goto reset_and_undo;
5708 }
5709
5710 /* Now ACK is acceptable.
5711 *
5712 * "If the RST bit is set
5713 * If the ACK was acceptable then signal the user "error:
5714 * connection reset", drop the segment, enter CLOSED state,
5715 * delete TCB, and return."
5716 */
5717
5718 if (th->rst) {
5719 tcp_reset(sk);
5720 goto discard;
5721 }
5722
5723 /* rfc793:
5724 * "fifth, if neither of the SYN or RST bits is set then
5725 * drop the segment and return."
5726 *
5727 * See note below!
5728 * --ANK(990513)
5729 */
5730 if (!th->syn)
5731 goto discard_and_undo;
5732
5733 /* rfc793:
5734 * "If the SYN bit is on ...
5735 * are acceptable then ...
5736 * (our SYN has been ACKed), change the connection
5737 * state to ESTABLISHED..."
5738 */
5739
Florian Westphal735d3832014-09-29 13:08:30 +02005740 tcp_ecn_rcv_synack(tp, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741
Razvan Ghitulete1b3a6922012-08-14 16:30:20 +03005742 tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
Florian Westphal31770e32017-08-30 19:24:58 +02005743 tcp_ack(sk, skb, FLAG_SLOWPATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744
5745 /* Ok.. it's good. Set up sequence numbers and
5746 * move to established.
5747 */
5748 tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
5749 tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
5750
5751 /* RFC1323: The window in SYN & SYN/ACK segments is
5752 * never scaled.
5753 */
5754 tp->snd_wnd = ntohs(th->window);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755
5756 if (!tp->rx_opt.wscale_ok) {
5757 tp->rx_opt.snd_wscale = tp->rx_opt.rcv_wscale = 0;
5758 tp->window_clamp = min(tp->window_clamp, 65535U);
5759 }
5760
5761 if (tp->rx_opt.saw_tstamp) {
5762 tp->rx_opt.tstamp_ok = 1;
5763 tp->tcp_header_len =
5764 sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
5765 tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
5766 tcp_store_ts_recent(tp);
5767 } else {
5768 tp->tcp_header_len = sizeof(struct tcphdr);
5769 }
5770
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08005771 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 tcp_initialize_rcv_mss(sk);
5773
5774 /* Remember, tcp_poll() does not lock socket!
5775 * Change state from SYN-SENT only after copied_seq
5776 * is initialized. */
5777 tp->copied_seq = tp->rcv_nxt;
William Allen Simpson4957faade2009-12-02 18:25:27 +00005778
Ursula Braun60e2a772017-10-25 11:01:45 +02005779 smc_check_reset_syn(tp);
5780
Ralf Baechlee16aa202006-12-07 00:11:33 -08005781 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005783 tcp_finish_connect(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
Eric Dumazet0f9fa832017-04-18 09:45:52 -07005785 fastopen_fail = (tp->syn_fastopen || tp->syn_data) &&
5786 tcp_rcv_fastopen_synack(sk, skb, &foc);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005787
Eric Dumazet0f9fa832017-04-18 09:45:52 -07005788 if (!sock_flag(sk, SOCK_DEAD)) {
5789 sk->sk_state_change(sk);
5790 sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
5791 }
5792 if (fastopen_fail)
5793 return -1;
Arnaldo Carvalho de Melo295f7322005-08-09 20:11:56 -07005794 if (sk->sk_write_pending ||
5795 icsk->icsk_accept_queue.rskq_defer_accept ||
5796 icsk->icsk_ack.pingpong) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 /* Save one ACK. Data will be ready after
5798 * several ticks, if write_pending is set.
5799 *
5800 * It may be deleted, but with this feature tcpdumps
5801 * look so _wonderfully_ clever, that I was not able
5802 * to stand against the temptation 8) --ANK
5803 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005804 inet_csk_schedule_ack(sk);
Eric Dumazet9a9c9b52018-05-21 15:08:56 -07005805 tcp_enter_quickack_mode(sk, TCP_MAX_QUICKACKS);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -07005806 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
5807 TCP_DELACK_MAX, TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808
5809discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07005810 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 return 0;
5812 } else {
5813 tcp_send_ack(sk);
5814 }
5815 return -1;
5816 }
5817
5818 /* No ACK in the segment */
5819
5820 if (th->rst) {
5821 /* rfc793:
5822 * "If the RST bit is set
5823 *
5824 * Otherwise (no ACK) drop the segment and return."
5825 */
5826
5827 goto discard_and_undo;
5828 }
5829
5830 /* PAWS check. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005831 if (tp->rx_opt.ts_recent_stamp && tp->rx_opt.saw_tstamp &&
Ilpo Järvinenc887e6d2009-03-14 14:23:03 +00005832 tcp_paws_reject(&tp->rx_opt, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 goto discard_and_undo;
5834
5835 if (th->syn) {
5836 /* We see SYN without ACK. It is attempt of
5837 * simultaneous connect with crossed SYNs.
5838 * Particularly, it can be connect to self.
5839 */
5840 tcp_set_state(sk, TCP_SYN_RECV);
5841
5842 if (tp->rx_opt.saw_tstamp) {
5843 tp->rx_opt.tstamp_ok = 1;
5844 tcp_store_ts_recent(tp);
5845 tp->tcp_header_len =
5846 sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
5847 } else {
5848 tp->tcp_header_len = sizeof(struct tcphdr);
5849 }
5850
5851 tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
Eric Dumazet142a2e72015-11-26 08:18:14 -08005852 tp->copied_seq = tp->rcv_nxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
5854
5855 /* RFC1323: The window in SYN & SYN/ACK segments is
5856 * never scaled.
5857 */
5858 tp->snd_wnd = ntohs(th->window);
5859 tp->snd_wl1 = TCP_SKB_CB(skb)->seq;
5860 tp->max_window = tp->snd_wnd;
5861
Florian Westphal735d3832014-09-29 13:08:30 +02005862 tcp_ecn_rcv_syn(tp, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863
John Heffner5d424d52006-03-20 17:53:41 -08005864 tcp_mtup_init(sk);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08005865 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 tcp_initialize_rcv_mss(sk);
5867
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 tcp_send_synack(sk);
5869#if 0
5870 /* Note, we could accept data and URG from this segment.
Jerry Chu168a8f52012-08-31 12:29:13 +00005871 * There are no obstacles to make this (except that we must
5872 * either change tcp_recvmsg() to prevent it from returning data
5873 * before 3WHS completes per RFC793, or employ TCP Fast Open).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 *
5875 * However, if we ignore data in ACKless segments sometimes,
5876 * we have no reasons to accept it sometimes.
5877 * Also, seems the code doing it in step6 of tcp_rcv_state_process
5878 * is not flawless. So, discard packet for sanity.
5879 * Uncomment this return to process the data.
5880 */
5881 return -1;
5882#else
5883 goto discard;
5884#endif
5885 }
5886 /* "fifth, if neither of the SYN or RST bits is set then
5887 * drop the segment and return."
5888 */
5889
5890discard_and_undo:
5891 tcp_clear_options(&tp->rx_opt);
5892 tp->rx_opt.mss_clamp = saved_clamp;
5893 goto discard;
5894
5895reset_and_undo:
5896 tcp_clear_options(&tp->rx_opt);
5897 tp->rx_opt.mss_clamp = saved_clamp;
5898 return 1;
5899}
5900
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901/*
5902 * This function implements the receiving procedure of RFC 793 for
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005903 * all states except ESTABLISHED and TIME_WAIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 * It's called from both tcp_v4_rcv and tcp_v6_rcv and should be
5905 * address independent.
5906 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005907
Eric Dumazet72ab4a82015-09-29 07:42:41 -07005908int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909{
5910 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08005911 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet72ab4a82015-09-29 07:42:41 -07005912 const struct tcphdr *th = tcp_hdr(skb);
Jerry Chu168a8f52012-08-31 12:29:13 +00005913 struct request_sock *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 int queued = 0;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00005915 bool acceptable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 switch (sk->sk_state) {
5918 case TCP_CLOSE:
5919 goto discard;
5920
5921 case TCP_LISTEN:
Stephen Hemminger2de979b2007-03-08 20:45:19 -08005922 if (th->ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 return 1;
5924
Stephen Hemminger2de979b2007-03-08 20:45:19 -08005925 if (th->rst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 goto discard;
5927
Stephen Hemminger2de979b2007-03-08 20:45:19 -08005928 if (th->syn) {
Eric Dumazetfdf5af02011-12-02 23:41:42 +00005929 if (th->fin)
5930 goto discard;
Eric Dumazet449809a2017-03-01 08:39:49 -08005931 /* It is possible that we process SYN packets from backlog,
5932 * so we need to make sure to disable BH right there.
5933 */
5934 local_bh_disable();
5935 acceptable = icsk->icsk_af_ops->conn_request(sk, skb) >= 0;
5936 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937
Eric Dumazet449809a2017-03-01 08:39:49 -08005938 if (!acceptable)
5939 return 1;
Eric Dumazet0aea76d2016-04-21 22:13:01 -07005940 consume_skb(skb);
Masayuki Nakagawafb7e2392007-01-23 20:15:06 -08005941 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 }
5943 goto discard;
5944
5945 case TCP_SYN_SENT:
Eric Dumazet8804b272016-04-13 22:05:40 -07005946 tp->rx_opt.saw_tstamp = 0;
Eric Dumazet9a568de2017-05-16 14:00:14 -07005947 tcp_mstamp_refresh(tp);
Eric Dumazetbda07a62015-09-29 07:42:40 -07005948 queued = tcp_rcv_synsent_state_process(sk, skb, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 if (queued >= 0)
5950 return queued;
5951
5952 /* Do step6 onward by hand. */
5953 tcp_urg(sk, skb, th);
5954 __kfree_skb(skb);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005955 tcp_data_snd_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 return 0;
5957 }
5958
Eric Dumazet9a568de2017-05-16 14:00:14 -07005959 tcp_mstamp_refresh(tp);
Eric Dumazet8804b272016-04-13 22:05:40 -07005960 tp->rx_opt.saw_tstamp = 0;
Jerry Chu168a8f52012-08-31 12:29:13 +00005961 req = tp->fastopen_rsk;
Ian Morris00db4122015-04-03 09:17:27 +01005962 if (req) {
Eric Dumazete0f97592018-02-13 06:14:12 -08005963 bool req_stolen;
5964
Jerry Chu37561f62012-10-22 11:26:36 +00005965 WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
Jerry Chu168a8f52012-08-31 12:29:13 +00005966 sk->sk_state != TCP_FIN_WAIT1);
5967
Eric Dumazete0f97592018-02-13 06:14:12 -08005968 if (!tcp_check_req(sk, skb, req, true, &req_stolen))
Jerry Chu168a8f52012-08-31 12:29:13 +00005969 goto discard;
Neal Cardwelle69bebd2012-09-22 04:18:57 +00005970 }
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00005971
Calvin Owens0c228e82014-11-20 15:09:53 -08005972 if (!th->ack && !th->rst && !th->syn)
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00005973 goto discard;
5974
Neal Cardwelle69bebd2012-09-22 04:18:57 +00005975 if (!tcp_validate_incoming(sk, skb, th, 0))
Eric Dumazet0c246042012-07-17 01:41:30 +00005976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
5978 /* step 5: check the ACK field */
Florian Westphal31770e32017-08-30 19:24:58 +02005979 acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH |
5980 FLAG_UPDATE_TS_RECENT |
Eric Dumazetd0e1a1b2017-05-23 15:24:46 -07005981 FLAG_NO_CHALLENGE_ACK) > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
Eric Dumazetd0e1a1b2017-05-23 15:24:46 -07005983 if (!acceptable) {
5984 if (sk->sk_state == TCP_SYN_RECV)
5985 return 1; /* send one RST */
5986 tcp_send_challenge_ack(sk, skb);
5987 goto discard;
5988 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00005989 switch (sk->sk_state) {
5990 case TCP_SYN_RECV:
Yuchung Chengbef57672018-04-17 23:18:46 -07005991 tp->delivered++; /* SYN-ACK delivery isn't tracked in tcp_ack */
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07005992 if (!tp->srtt_us)
5993 tcp_synack_rtt_meas(sk, req);
5994
Joe Perches61eb90032013-05-24 18:36:13 +00005995 /* Once we leave TCP_SYN_RECV, we no longer need req
5996 * so release it.
5997 */
5998 if (req) {
Yuchung Cheng7e32b442016-09-21 16:16:15 -07005999 inet_csk(sk)->icsk_retransmits = 0;
Joe Perches61eb90032013-05-24 18:36:13 +00006000 reqsk_fastopen_remove(sk, req, false);
Wei Wang6d050812017-10-04 10:04:04 -07006001 /* Re-arm the timer because data may have been sent out.
6002 * This is similar to the regular data transmission case
6003 * when new data has just been ack'ed.
6004 *
6005 * (TFO) - we could try to be more aggressive and
6006 * retransmitting any data sooner based on when they
6007 * are sent out.
6008 */
6009 tcp_rearm_rto(sk);
Joe Perches61eb90032013-05-24 18:36:13 +00006010 } else {
Wei Wang27204aa2017-10-04 10:03:44 -07006011 tcp_init_transfer(sk, BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB);
Joe Perches61eb90032013-05-24 18:36:13 +00006012 tp->copied_seq = tp->rcv_nxt;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006013 }
Joe Perches61eb90032013-05-24 18:36:13 +00006014 smp_mb();
6015 tcp_set_state(sk, TCP_ESTABLISHED);
6016 sk->sk_state_change(sk);
6017
6018 /* Note, that this wakeup is only for marginal crossed SYN case.
6019 * Passively open sockets are not waked up, because
6020 * sk->sk_sleep == NULL and sk->sk_socket == NULL.
6021 */
6022 if (sk->sk_socket)
6023 sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
6024
6025 tp->snd_una = TCP_SKB_CB(skb)->ack_seq;
6026 tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale;
6027 tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
6028
6029 if (tp->rx_opt.tstamp_ok)
6030 tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
6031
Yuchung Chengc0402762016-09-19 23:39:21 -04006032 if (!inet_csk(sk)->icsk_ca_ops->cong_control)
6033 tcp_update_pacing_rate(sk);
Neal Cardwell02cf4eb2013-10-21 15:40:19 -04006034
Joe Perches61eb90032013-05-24 18:36:13 +00006035 /* Prevent spurious tcp_cwnd_restart() on first data packet */
Eric Dumazetd635fbe2017-05-16 14:00:03 -07006036 tp->lsndtime = tcp_jiffies32;
Joe Perches61eb90032013-05-24 18:36:13 +00006037
6038 tcp_initialize_rcv_mss(sk);
Florian Westphal31770e32017-08-30 19:24:58 +02006039 tcp_fast_path_on(tp);
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006040 break;
6041
Joe Perchesc48b22d2013-05-24 18:06:58 +00006042 case TCP_FIN_WAIT1: {
Joe Perchesc48b22d2013-05-24 18:06:58 +00006043 int tmo;
6044
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006045 /* If we enter the TCP_FIN_WAIT1 state and we are a
6046 * Fast Open socket and this is the first acceptable
6047 * ACK we have received, this would have acknowledged
6048 * our SYNACK so stop the SYNACK timer.
6049 */
Ian Morris00db4122015-04-03 09:17:27 +01006050 if (req) {
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006051 /* We no longer need the request sock. */
6052 reqsk_fastopen_remove(sk, req, false);
6053 tcp_rearm_rto(sk);
6054 }
Joe Perchesc48b22d2013-05-24 18:06:58 +00006055 if (tp->snd_una != tp->write_seq)
6056 break;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006057
Joe Perchesc48b22d2013-05-24 18:06:58 +00006058 tcp_set_state(sk, TCP_FIN_WAIT2);
6059 sk->sk_shutdown |= SEND_SHUTDOWN;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006060
Julian Anastasovc3a2e832017-02-06 23:14:14 +02006061 sk_dst_confirm(sk);
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006062
Joe Perchesc48b22d2013-05-24 18:06:58 +00006063 if (!sock_flag(sk, SOCK_DEAD)) {
6064 /* Wake up lingering close() */
6065 sk->sk_state_change(sk);
6066 break;
6067 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006068
Wei Wangcf1ef3f2017-04-20 14:45:46 -07006069 if (tp->linger2 < 0) {
6070 tcp_done(sk);
6071 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
6072 return 1;
6073 }
6074 if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
6075 after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
6076 /* Receive out of order FIN after close() */
6077 if (tp->syn_fastopen && th->fin)
Wei Wang46c2fa32017-04-20 14:45:47 -07006078 tcp_fastopen_active_disable(sk);
Joe Perchesc48b22d2013-05-24 18:06:58 +00006079 tcp_done(sk);
Eric Dumazetc10d9312016-04-29 14:16:47 -07006080 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
Joe Perchesc48b22d2013-05-24 18:06:58 +00006081 return 1;
6082 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006083
Joe Perchesc48b22d2013-05-24 18:06:58 +00006084 tmo = tcp_fin_time(sk);
6085 if (tmo > TCP_TIMEWAIT_LEN) {
6086 inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN);
6087 } else if (th->fin || sock_owned_by_user(sk)) {
6088 /* Bad case. We could lose such FIN otherwise.
6089 * It is not a big problem, but it looks confusing
6090 * and not so rare event. We still can lose it now,
6091 * if it spins in bh_lock_sock(), but it is really
6092 * marginal case.
6093 */
6094 inet_csk_reset_keepalive_timer(sk, tmo);
6095 } else {
6096 tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
6097 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006099 break;
Joe Perchesc48b22d2013-05-24 18:06:58 +00006100 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006101
6102 case TCP_CLOSING:
6103 if (tp->snd_una == tp->write_seq) {
6104 tcp_time_wait(sk, TCP_TIME_WAIT, 0);
6105 goto discard;
6106 }
6107 break;
6108
6109 case TCP_LAST_ACK:
6110 if (tp->snd_una == tp->write_seq) {
6111 tcp_update_metrics(sk);
6112 tcp_done(sk);
6113 goto discard;
6114 }
6115 break;
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00006116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117
6118 /* step 6: check the URG bit */
6119 tcp_urg(sk, skb, th);
6120
6121 /* step 7: process the segment text */
6122 switch (sk->sk_state) {
6123 case TCP_CLOSE_WAIT:
6124 case TCP_CLOSING:
6125 case TCP_LAST_ACK:
6126 if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
6127 break;
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05006128 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 case TCP_FIN_WAIT1:
6130 case TCP_FIN_WAIT2:
6131 /* RFC 793 says to queue data in these states,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09006132 * RFC 1122 says we MUST send a reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 * BSD 4.4 also does reset.
6134 */
6135 if (sk->sk_shutdown & RCV_SHUTDOWN) {
6136 if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
6137 after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07006138 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 tcp_reset(sk);
6140 return 1;
6141 }
6142 }
6143 /* Fall through */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09006144 case TCP_ESTABLISHED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 tcp_data_queue(sk, skb);
6146 queued = 1;
6147 break;
6148 }
6149
6150 /* tcp_data could move socket to TIME-WAIT */
6151 if (sk->sk_state != TCP_CLOSE) {
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07006152 tcp_data_snd_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 tcp_ack_snd_check(sk);
6154 }
6155
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09006156 if (!queued) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07006158 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 }
6160 return 0;
6161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162EXPORT_SYMBOL(tcp_rcv_state_process);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006163
6164static inline void pr_drop_req(struct request_sock *req, __u16 port, int family)
6165{
6166 struct inet_request_sock *ireq = inet_rsk(req);
6167
6168 if (family == AF_INET)
Joe Perchesba7a46f2014-11-11 10:59:17 -08006169 net_dbg_ratelimited("drop open request from %pI4/%u\n",
6170 &ireq->ir_rmt_addr, port);
Octavian Purdila4135ab82014-06-28 21:20:54 +03006171#if IS_ENABLED(CONFIG_IPV6)
6172 else if (family == AF_INET6)
Joe Perchesba7a46f2014-11-11 10:59:17 -08006173 net_dbg_ratelimited("drop open request from %pI6/%u\n",
6174 &ireq->ir_v6_rmt_addr, port);
Octavian Purdila4135ab82014-06-28 21:20:54 +03006175#endif
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006176}
6177
Florian Westphald82bd122014-09-29 13:08:29 +02006178/* RFC3168 : 6.1.1 SYN packets must not have ECT/ECN bits set
6179 *
6180 * If we receive a SYN packet with these bits set, it means a
6181 * network is playing bad games with TOS bits. In order to
6182 * avoid possible false congestion notifications, we disable
stephen hemmingerf4e715c2014-10-29 16:05:06 -07006183 * TCP ECN negotiation.
Florian Westphald82bd122014-09-29 13:08:29 +02006184 *
6185 * Exception: tcp_ca wants ECN. This is required for DCTCP
Florian Westphal843c2fd2015-01-30 20:45:20 +01006186 * congestion control: Linux DCTCP asserts ECT on all packets,
6187 * including SYN, which is most optimal solution; however,
6188 * others, such as FreeBSD do not.
Florian Westphald82bd122014-09-29 13:08:29 +02006189 */
6190static void tcp_ecn_create_request(struct request_sock *req,
6191 const struct sk_buff *skb,
Florian Westphalf7b3bec2014-11-03 17:35:03 +01006192 const struct sock *listen_sk,
6193 const struct dst_entry *dst)
Florian Westphald82bd122014-09-29 13:08:29 +02006194{
6195 const struct tcphdr *th = tcp_hdr(skb);
6196 const struct net *net = sock_net(listen_sk);
6197 bool th_ecn = th->ece && th->cwr;
Florian Westphal843c2fd2015-01-30 20:45:20 +01006198 bool ect, ecn_ok;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02006199 u32 ecn_ok_dst;
Florian Westphald82bd122014-09-29 13:08:29 +02006200
6201 if (!th_ecn)
6202 return;
6203
6204 ect = !INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield);
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02006205 ecn_ok_dst = dst_feature(dst, DST_FEATURE_ECN_MASK);
6206 ecn_ok = net->ipv4.sysctl_tcp_ecn || ecn_ok_dst;
Florian Westphald82bd122014-09-29 13:08:29 +02006207
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02006208 if ((!ect && ecn_ok) || tcp_ca_needs_ecn(listen_sk) ||
Lawrence Brakmo91b5b212017-06-30 20:02:49 -07006209 (ecn_ok_dst & DST_FEATURE_ECN_CA) ||
6210 tcp_bpf_ca_needs_ecn((struct sock *)req))
Florian Westphald82bd122014-09-29 13:08:29 +02006211 inet_rsk(req)->ecn_ok = 1;
6212}
6213
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006214static void tcp_openreq_init(struct request_sock *req,
6215 const struct tcp_options_received *rx_opt,
6216 struct sk_buff *skb, const struct sock *sk)
6217{
6218 struct inet_request_sock *ireq = inet_rsk(req);
6219
Eric Dumazeted53d0a2015-10-08 19:33:23 -07006220 req->rsk_rcv_wnd = 0; /* So that tcp_send_synack() knows! */
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006221 req->cookie_ts = 0;
6222 tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
6223 tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
Eric Dumazet9a568de2017-05-16 14:00:14 -07006224 tcp_rsk(req)->snt_synack = tcp_clock_us();
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006225 tcp_rsk(req)->last_oow_ack_time = 0;
6226 req->mss = rx_opt->mss_clamp;
6227 req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
6228 ireq->tstamp_ok = rx_opt->tstamp_ok;
6229 ireq->sack_ok = rx_opt->sack_ok;
6230 ireq->snd_wscale = rx_opt->snd_wscale;
6231 ireq->wscale_ok = rx_opt->wscale_ok;
6232 ireq->acked = 0;
6233 ireq->ecn_ok = 0;
6234 ireq->ir_rmt_port = tcp_hdr(skb)->source;
6235 ireq->ir_num = ntohs(tcp_hdr(skb)->dest);
6236 ireq->ir_mark = inet_request_mark(sk, skb);
Ursula Braun60e2a772017-10-25 11:01:45 +02006237#if IS_ENABLED(CONFIG_SMC)
6238 ireq->smc_ok = rx_opt->smc_ok;
6239#endif
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006240}
6241
Eric Dumazete49bb332015-03-17 18:32:27 -07006242struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops,
Eric Dumazeta1a53442015-10-04 21:08:11 -07006243 struct sock *sk_listener,
6244 bool attach_listener)
Eric Dumazete49bb332015-03-17 18:32:27 -07006245{
Eric Dumazeta1a53442015-10-04 21:08:11 -07006246 struct request_sock *req = reqsk_alloc(ops, sk_listener,
6247 attach_listener);
Eric Dumazete49bb332015-03-17 18:32:27 -07006248
6249 if (req) {
6250 struct inet_request_sock *ireq = inet_rsk(req);
6251
Eric Dumazetc92e8c02017-10-20 09:04:13 -07006252 ireq->ireq_opt = NULL;
Huw Davies56ac42b2016-06-27 15:05:28 -04006253#if IS_ENABLED(CONFIG_IPV6)
6254 ireq->pktopts = NULL;
6255#endif
Yafang Shaoa06ac0d2018-04-24 23:07:45 +08006256 atomic64_set(&ireq->ir_cookie, 0);
Eric Dumazete49bb332015-03-17 18:32:27 -07006257 ireq->ireq_state = TCP_NEW_SYN_RECV;
6258 write_pnet(&ireq->ireq_net, sock_net(sk_listener));
Eric Dumazet0144a812015-03-24 21:45:56 -07006259 ireq->ireq_family = sk_listener->sk_family;
Eric Dumazete49bb332015-03-17 18:32:27 -07006260 }
6261
6262 return req;
6263}
6264EXPORT_SYMBOL(inet_reqsk_alloc);
6265
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006266/*
6267 * Return true if a syncookie should be sent
6268 */
Eric Dumazet2985aaa2015-09-29 07:42:51 -07006269static bool tcp_syn_flood_action(const struct sock *sk,
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006270 const struct sk_buff *skb,
6271 const char *proto)
6272{
Eric Dumazet8d2675f2015-10-02 11:43:25 -07006273 struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006274 const char *msg = "Dropping request";
6275 bool want_cookie = false;
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006276 struct net *net = sock_net(sk);
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006277
6278#ifdef CONFIG_SYN_COOKIES
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006279 if (net->ipv4.sysctl_tcp_syncookies) {
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006280 msg = "Sending cookies";
6281 want_cookie = true;
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07006282 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPREQQFULLDOCOOKIES);
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006283 } else
6284#endif
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07006285 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPREQQFULLDROP);
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006286
Eric Dumazet8d2675f2015-10-02 11:43:25 -07006287 if (!queue->synflood_warned &&
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006288 net->ipv4.sysctl_tcp_syncookies != 2 &&
Eric Dumazet8d2675f2015-10-02 11:43:25 -07006289 xchg(&queue->synflood_warned, 1) == 0)
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006290 pr_info("%s: Possible SYN flooding on port %d. %s. Check SNMP counters.\n",
6291 proto, ntohs(tcp_hdr(skb)->dest), msg);
Eric Dumazet2985aaa2015-09-29 07:42:51 -07006292
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006293 return want_cookie;
6294}
6295
Eric Dumazetcd8ae852015-05-03 21:34:46 -07006296static void tcp_reqsk_record_syn(const struct sock *sk,
6297 struct request_sock *req,
6298 const struct sk_buff *skb)
6299{
6300 if (tcp_sk(sk)->save_syn) {
6301 u32 len = skb_network_header_len(skb) + tcp_hdrlen(skb);
6302 u32 *copy;
6303
6304 copy = kmalloc(len + sizeof(u32), GFP_ATOMIC);
6305 if (copy) {
6306 copy[0] = len;
6307 memcpy(&copy[1], skb_network_header(skb), len);
6308 req->saved_syn = copy;
6309 }
6310 }
6311}
6312
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006313int tcp_conn_request(struct request_sock_ops *rsk_ops,
6314 const struct tcp_request_sock_ops *af_ops,
6315 struct sock *sk, struct sk_buff *skb)
6316{
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006317 struct tcp_fastopen_cookie foc = { .len = -1 };
Eric Dumazet7c85af82015-09-24 17:16:05 -07006318 __u32 isn = TCP_SKB_CB(skb)->tcp_tw_isn;
6319 struct tcp_options_received tmp_opt;
6320 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006321 struct net *net = sock_net(sk);
Eric Dumazet7c85af82015-09-24 17:16:05 -07006322 struct sock *fastopen_sk = NULL;
Eric Dumazet7c85af82015-09-24 17:16:05 -07006323 struct request_sock *req;
6324 bool want_cookie = false;
Eric Dumazeteaa72dc2017-08-29 15:16:01 -07006325 struct dst_entry *dst;
Eric Dumazet7c85af82015-09-24 17:16:05 -07006326 struct flowi fl;
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006327
6328 /* TW buckets are converted to open requests without
6329 * limitations, they conserve resources and peer is
6330 * evidently real one.
6331 */
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006332 if ((net->ipv4.sysctl_tcp_syncookies == 2 ||
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006333 inet_csk_reqsk_queue_is_full(sk)) && !isn) {
6334 want_cookie = tcp_syn_flood_action(sk, skb, rsk_ops->slab_name);
6335 if (!want_cookie)
6336 goto drop;
6337 }
6338
Eric Dumazet5ea8ea22016-10-26 09:27:57 -07006339 if (sk_acceptq_is_full(sk)) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07006340 NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006341 goto drop;
6342 }
6343
Eric Dumazeta1a53442015-10-04 21:08:11 -07006344 req = inet_reqsk_alloc(rsk_ops, sk, !want_cookie);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006345 if (!req)
6346 goto drop;
6347
6348 tcp_rsk(req)->af_specific = af_ops;
Florian Westphal95a22ca2016-12-01 11:32:06 +01006349 tcp_rsk(req)->ts_off = 0;
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006350
6351 tcp_clear_options(&tmp_opt);
6352 tmp_opt.mss_clamp = af_ops->mss_clamp;
6353 tmp_opt.user_mss = tp->rx_opt.user_mss;
Eric Dumazeteed29f12017-06-07 10:34:36 -07006354 tcp_parse_options(sock_net(sk), skb, &tmp_opt, 0,
6355 want_cookie ? NULL : &foc);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006356
6357 if (want_cookie && !tmp_opt.saw_tstamp)
6358 tcp_clear_options(&tmp_opt);
6359
Hans Wippelbc58a1b2018-03-23 11:05:45 +01006360 if (IS_ENABLED(CONFIG_SMC) && want_cookie)
6361 tmp_opt.smc_ok = 0;
6362
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006363 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
6364 tcp_openreq_init(req, &tmp_opt, skb, sk);
KOVACS Krisztian7a682572016-09-23 11:27:42 +02006365 inet_rsk(req)->no_srccheck = inet_sk(sk)->transparent;
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006366
Eric Dumazet16f86162015-03-13 15:51:10 -07006367 /* Note: tcp_v6_init_req() might override ir_iif for link locals */
David Ahern6dd9a142015-12-16 13:20:44 -08006368 inet_rsk(req)->ir_iif = inet_request_bound_dev_if(sk, skb);
Eric Dumazet16f86162015-03-13 15:51:10 -07006369
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006370 af_ops->init_req(req, sk, skb);
6371
6372 if (security_inet_conn_request(sk, skb, req))
6373 goto drop_and_free;
6374
Eric Dumazet84b114b2017-05-05 06:56:54 -07006375 if (tmp_opt.tstamp_ok)
Eric Dumazet5d2ed052017-06-07 10:34:39 -07006376 tcp_rsk(req)->ts_off = af_ops->init_ts_off(net, skb);
Florian Westphal95a22ca2016-12-01 11:32:06 +01006377
Tonghao Zhang49c71582017-08-21 23:33:48 -07006378 dst = af_ops->route_req(sk, &fl, req);
6379 if (!dst)
6380 goto drop_and_free;
6381
Florian Westphalf7b3bec2014-11-03 17:35:03 +01006382 if (!want_cookie && !isn) {
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006383 /* Kill the following clause, if you dislike this way. */
Soheil Hassas Yeganeh4396e462017-03-15 16:30:46 -04006384 if (!net->ipv4.sysctl_tcp_syncookies &&
6385 (net->ipv4.sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
6386 (net->ipv4.sysctl_max_syn_backlog >> 2)) &&
6387 !tcp_peer_is_proven(req, dst)) {
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006388 /* Without syncookies last quarter of
6389 * backlog is filled with destinations,
6390 * proven to be alive.
6391 * It means that we continue to communicate
6392 * to destinations, already remembered
6393 * to the moment of synflood.
6394 */
6395 pr_drop_req(req, ntohs(tcp_hdr(skb)->source),
6396 rsk_ops->family);
6397 goto drop_and_release;
6398 }
6399
Eric Dumazet84b114b2017-05-05 06:56:54 -07006400 isn = af_ops->init_seq(skb);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006401 }
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006402
Florian Westphalf7b3bec2014-11-03 17:35:03 +01006403 tcp_ecn_create_request(req, skb, sk, dst);
6404
6405 if (want_cookie) {
6406 isn = cookie_init_sequence(af_ops, sk, skb, &req->mss);
6407 req->cookie_ts = tmp_opt.tstamp_ok;
6408 if (!tmp_opt.tstamp_ok)
6409 inet_rsk(req)->ecn_ok = 0;
6410 }
6411
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006412 tcp_rsk(req)->snt_isn = isn;
Eric Dumazet58d607d2015-09-15 15:24:20 -07006413 tcp_rsk(req)->txhash = net_tx_rndhash();
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006414 tcp_openreq_init_rwin(req, sk, dst);
Eric Dumazetca6fb062015-10-02 11:43:35 -07006415 if (!want_cookie) {
Eric Dumazetca6fb062015-10-02 11:43:35 -07006416 tcp_reqsk_record_syn(sk, req, skb);
Christoph Paasch71c02372017-10-23 13:22:23 -07006417 fastopen_sk = tcp_try_fastopen(sk, skb, req, &foc, dst);
Eric Dumazetca6fb062015-10-02 11:43:35 -07006418 }
Eric Dumazet7c85af82015-09-24 17:16:05 -07006419 if (fastopen_sk) {
Eric Dumazetca6fb062015-10-02 11:43:35 -07006420 af_ops->send_synack(fastopen_sk, dst, &fl, req,
Eric Dumazetb3d05142016-04-13 22:05:39 -07006421 &foc, TCP_SYNACK_FASTOPEN);
Eric Dumazet7656d842015-10-04 21:08:07 -07006422 /* Add the child socket directly into the accept queue */
6423 inet_csk_reqsk_queue_add(sk, req, fastopen_sk);
6424 sk->sk_data_ready(sk);
6425 bh_unlock_sock(fastopen_sk);
Eric Dumazet7c85af82015-09-24 17:16:05 -07006426 sock_put(fastopen_sk);
6427 } else {
Eric Dumazet9439ce02015-03-17 18:32:29 -07006428 tcp_rsk(req)->tfo_listener = false;
Eric Dumazetca6fb062015-10-02 11:43:35 -07006429 if (!want_cookie)
Lawrence Brakmo8550f322017-06-30 20:02:42 -07006430 inet_csk_reqsk_queue_hash_add(sk, req,
6431 tcp_timeout_init((struct sock *)req));
Eric Dumazetb3d05142016-04-13 22:05:39 -07006432 af_ops->send_synack(sk, dst, &fl, req, &foc,
6433 !want_cookie ? TCP_SYNACK_NORMAL :
6434 TCP_SYNACK_COOKIE);
Eric Dumazet9caad862016-04-01 08:52:20 -07006435 if (want_cookie) {
6436 reqsk_free(req);
6437 return 0;
6438 }
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006439 }
Eric Dumazetca6fb062015-10-02 11:43:35 -07006440 reqsk_put(req);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006441 return 0;
6442
6443drop_and_release:
6444 dst_release(dst);
6445drop_and_free:
6446 reqsk_free(req);
6447drop:
Eric Dumazet9caad862016-04-01 08:52:20 -07006448 tcp_listendrop(sk);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006449 return 0;
6450}
6451EXPORT_SYMBOL(tcp_conn_request);