blob: 337f6011528a7d4c3ab7fdcc0623496cfefafc71 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Implementation of the Transmission Control Protocol(TCP).
7 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Mark Evans, <evansmp@uhura.aston.ac.uk>
11 * Corey Minyard <wf-rch!minyard@relay.EU.net>
12 * Florian La Roche, <flla@stud.uni-sb.de>
13 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
14 * Linus Torvalds, <torvalds@cs.helsinki.fi>
15 * Alan Cox, <gw4pts@gw4pts.ampr.org>
16 * Matthew Dillon, <dillon@apollo.west.oic.com>
17 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
18 * Jorge Cwik, <jorge@laser.satlink.net>
19 */
20
21/*
22 * Changes:
23 * Pedro Roque : Fast Retransmit/Recovery.
24 * Two receive queues.
25 * Retransmit queue handled by TCP.
26 * Better retransmit timer handling.
27 * New congestion avoidance.
28 * Header prediction.
29 * Variable renaming.
30 *
31 * Eric : Fast Retransmit.
32 * Randy Scott : MSS option defines.
33 * Eric Schenk : Fixes to slow start algorithm.
34 * Eric Schenk : Yet another double ACK bug.
35 * Eric Schenk : Delayed ACK bug fixes.
36 * Eric Schenk : Floyd style fast retrans war avoidance.
37 * David S. Miller : Don't allow zero congestion window.
38 * Eric Schenk : Fix retransmitter so that it sends
39 * next packet on ack of previous packet.
40 * Andi Kleen : Moved open_request checking here
41 * and process RSTs for open_requests.
42 * Andi Kleen : Better prune_queue, and other fixes.
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -080043 * Andrey Savochkin: Fix RTT measurements in the presence of
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * timestamps.
45 * Andrey Savochkin: Check sequence numbers correctly when
46 * removing SACKs due to in sequence incoming
47 * data segments.
48 * Andi Kleen: Make sure we never ack data there is not
49 * enough room for. Also make this condition
50 * a fatal error if it might still happen.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090051 * Andi Kleen: Add tcp_measure_rcv_mss to make
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * connections with MSS<min(MTU,ann. MSS)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090053 * work without delayed acks.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * Andi Kleen: Process packets with PSH set in the
55 * fast path.
56 * J Hadi Salim: ECN support
57 * Andrei Gurtov,
58 * Pasi Sarolahti,
59 * Panu Kuhlberg: Experimental audit of TCP (re)transmission
60 * engine. Lots of bugs are found.
61 * Pasi Sarolahti: F-RTO for dealing with spurious RTOs
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
63
Joe Perchesafd465032012-03-12 07:03:32 +000064#define pr_fmt(fmt) "TCP: " fmt
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090067#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/module.h>
69#include <linux/sysctl.h>
Ilpo Järvinena0bffff2009-03-21 13:36:17 -070070#include <linux/kernel.h>
Eric Dumazetad971f62014-10-11 15:17:29 -070071#include <linux/prefetch.h>
Satoru SATOH5ffc02a2008-05-04 22:14:42 -070072#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <net/tcp.h>
74#include <net/inet_common.h>
75#include <linux/ipsec.h>
76#include <asm/unaligned.h>
Willem de Bruijne1c8a602014-08-04 22:11:50 -040077#include <linux/errqueue.h>
Song Liu59415212017-10-23 09:20:25 -070078#include <trace/events/tcp.h>
Ursula Braun60e2a772017-10-25 11:01:45 +020079#include <linux/unaligned/access_ok.h>
80#include <linux/static_key.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Yuchung Cheng94bdc972017-01-12 22:11:42 -080082int sysctl_tcp_fack __read_mostly;
Eric Dumazetdca145f2014-10-27 21:45:24 -070083int sysctl_tcp_max_reordering __read_mostly = 300;
Brian Haleyab32ea52006-09-22 14:15:41 -070084int sysctl_tcp_dsack __read_mostly = 1;
85int sysctl_tcp_app_win __read_mostly = 31;
Eric Dumazetb49960a2012-05-02 02:28:41 +000086int sysctl_tcp_adv_win_scale __read_mostly = 1;
Eric Dumazet4bc2f182010-07-09 21:22:10 +000087EXPORT_SYMBOL(sysctl_tcp_adv_win_scale);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Eric Dumazet282f23c2012-07-17 10:13:05 +020089/* rfc5961 challenge ack rate limiting */
Eric Dumazet75ff39c2016-07-10 10:04:02 +020090int sysctl_tcp_challenge_ack_limit = 1000;
Eric Dumazet282f23c2012-07-17 10:13:05 +020091
Brian Haleyab32ea52006-09-22 14:15:41 -070092int sysctl_tcp_stdurg __read_mostly;
93int sysctl_tcp_rfc1337 __read_mostly;
94int sysctl_tcp_max_orphans __read_mostly = NR_FILE;
Ilpo Järvinenc96fd3d2007-09-20 11:36:37 -070095int sysctl_tcp_frto __read_mostly = 2;
Yuchung Chengf6722582015-10-16 21:57:42 -070096int sysctl_tcp_min_rtt_wlen __read_mostly = 300;
Brian Haleyab32ea52006-09-22 14:15:41 -070097int sysctl_tcp_moderate_rcvbuf __read_mostly = 1;
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +000098int sysctl_tcp_early_retrans __read_mostly = 3;
Neal Cardwell032ee422015-02-06 16:04:38 -050099int sysctl_tcp_invalid_ratelimit __read_mostly = HZ/2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define FLAG_DATA 0x01 /* Incoming frame contained data. */
102#define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */
103#define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */
104#define FLAG_RETRANS_DATA_ACKED 0x08 /* "" "" some of which was retransmitted. */
105#define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */
106#define FLAG_DATA_SACKED 0x20 /* New SACK. */
107#define FLAG_ECE 0x40 /* ECE in this ACK */
Yuchung Cheng291a00d2015-07-01 14:11:14 -0700108#define FLAG_LOST_RETRANS 0x80 /* This ACK marks some retransmission lost */
Florian Westphal31770e32017-08-30 19:24:58 +0200109#define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/
Yuchung Chenge33099f2013-03-20 13:33:00 +0000110#define FLAG_ORIG_SACK_ACKED 0x200 /* Never retransmitted data are (s)acked */
Ilpo Järvinen2e605292007-08-02 19:46:58 -0700111#define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */
Ryousei Takano564262c2007-10-25 23:03:52 -0700112#define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */
Neal Cardwelldf92c832017-08-03 09:19:54 -0400113#define FLAG_SET_XMIT_TIMER 0x1000 /* Set TLP or RTO timer */
Ilpo Järvinencadbd032007-12-31 04:49:21 -0800114#define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */
Eric Dumazet12fb3dd2013-04-19 07:19:48 +0000115#define FLAG_UPDATE_TS_RECENT 0x4000 /* tcp_replace_ts_recent() */
Eric Dumazetd0e1a1b2017-05-23 15:24:46 -0700116#define FLAG_NO_CHALLENGE_ACK 0x8000 /* do not call tcp_send_challenge_ack() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118#define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED)
119#define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
120#define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE)
121#define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED)
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700124#define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Yuchung Chenge662ca42016-02-02 10:33:04 -0800126#define REXMIT_NONE 0 /* no loss recovery to do */
127#define REXMIT_LOST 1 /* retransmit packets marked lost */
128#define REXMIT_NEW 2 /* FRTO-style transmit of unsent/new packets */
129
Marcelo Ricardo Leitner0b9aefe2017-04-01 11:00:21 -0300130static void tcp_gro_dev_warn(struct sock *sk, const struct sk_buff *skb,
131 unsigned int len)
Marcelo Ricardo Leitnerdcb17d22016-12-05 18:37:13 -0200132{
133 static bool __once __read_mostly;
134
135 if (!__once) {
136 struct net_device *dev;
137
138 __once = true;
139
140 rcu_read_lock();
141 dev = dev_get_by_index_rcu(sock_net(sk), skb->skb_iif);
Marcelo Ricardo Leitner0b9aefe2017-04-01 11:00:21 -0300142 if (!dev || len >= dev->mtu)
143 pr_warn("%s: Driver has suspect GRO implementation, TCP performance may be compromised.\n",
144 dev ? dev->name : "Unknown driver");
Marcelo Ricardo Leitnerdcb17d22016-12-05 18:37:13 -0200145 rcu_read_unlock();
146 }
147}
148
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900149/* Adapt the MSS value used to make delayed ack decision to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * real world.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900151 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800152static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700154 struct inet_connection_sock *icsk = inet_csk(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900155 const unsigned int lss = icsk->icsk_ack.last_seg_size;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700156 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900158 icsk->icsk_ack.last_seg_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 /* skb->len may jitter because of SACKs, even if peer
161 * sends good full-sized frames.
162 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800163 len = skb_shinfo(skb)->gso_size ? : skb->len;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700164 if (len >= icsk->icsk_ack.rcv_mss) {
Marcelo Ricardo Leitnerdcb17d22016-12-05 18:37:13 -0200165 icsk->icsk_ack.rcv_mss = min_t(unsigned int, len,
166 tcp_sk(sk)->advmss);
Marcelo Ricardo Leitner0b9aefe2017-04-01 11:00:21 -0300167 /* Account for possibly-removed options */
168 if (unlikely(len > icsk->icsk_ack.rcv_mss +
169 MAX_TCP_OPTION_SPACE))
170 tcp_gro_dev_warn(sk, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 } else {
172 /* Otherwise, we make more careful check taking into account,
173 * that SACKs block is variable.
174 *
175 * "len" is invariant segment length, including TCP header.
176 */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700177 len += skb->data - skb_transport_header(skb);
William Allen Simpsonbee7ca92009-11-10 09:51:18 +0000178 if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 /* If PSH is not set, packet should be
180 * full sized, provided peer TCP is not badly broken.
181 * This observation (if it is correct 8)) allows
182 * to handle super-low mtu links fairly.
183 */
184 (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700185 !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* Subtract also invariant (if peer is RFC compliant),
187 * tcp header plus fixed timestamp option length.
188 * Resulting "len" is MSS free of SACK jitter.
189 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700190 len -= tcp_sk(sk)->tcp_header_len;
191 icsk->icsk_ack.last_seg_size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (len == lss) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700193 icsk->icsk_ack.rcv_mss = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return;
195 }
196 }
Alexey Kuznetsov1ef96962006-09-19 12:52:50 -0700197 if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)
198 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700199 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201}
202
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700203static void tcp_incr_quickack(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700205 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000206 unsigned int quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800208 if (quickacks == 0)
209 quickacks = 2;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700210 if (quickacks > icsk->icsk_ack.quick)
211 icsk->icsk_ack.quick = min(quickacks, TCP_MAX_QUICKACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
stephen hemminger1b9f4092010-09-28 19:30:14 +0000214static void tcp_enter_quickack_mode(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700216 struct inet_connection_sock *icsk = inet_csk(sk);
217 tcp_incr_quickack(sk);
218 icsk->icsk_ack.pingpong = 0;
219 icsk->icsk_ack.ato = TCP_ATO_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222/* Send ACKs quickly, if "quick" count is not exhausted
223 * and the session is not interactive.
224 */
225
Jon Maxwell2251ae42015-07-08 10:12:28 +1000226static bool tcp_in_quickack_mode(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700228 const struct inet_connection_sock *icsk = inet_csk(sk);
Jon Maxwell2251ae42015-07-08 10:12:28 +1000229 const struct dst_entry *dst = __sk_dst_get(sk);
Eric Dumazeta2a385d2012-05-16 23:15:34 +0000230
Jon Maxwell2251ae42015-07-08 10:12:28 +1000231 return (dst && dst_metric(dst, RTAX_QUICKACK)) ||
232 (icsk->icsk_ack.quick && !icsk->icsk_ack.pingpong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Florian Westphal735d3832014-09-29 13:08:30 +0200235static void tcp_ecn_queue_cwr(struct tcp_sock *tp)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700236{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800237 if (tp->ecn_flags & TCP_ECN_OK)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700238 tp->ecn_flags |= TCP_ECN_QUEUE_CWR;
239}
240
Florian Westphal735d3832014-09-29 13:08:30 +0200241static void tcp_ecn_accept_cwr(struct tcp_sock *tp, const struct sk_buff *skb)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700242{
243 if (tcp_hdr(skb)->cwr)
244 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
245}
246
Florian Westphal735d3832014-09-29 13:08:30 +0200247static void tcp_ecn_withdraw_cwr(struct tcp_sock *tp)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700248{
249 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
250}
251
Florian Westphal735d3832014-09-29 13:08:30 +0200252static void __tcp_ecn_check_ce(struct tcp_sock *tp, const struct sk_buff *skb)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700253{
Eric Dumazetb82d1bb2011-09-27 02:20:08 -0400254 switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) {
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000255 case INET_ECN_NOT_ECT:
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700256 /* Funny extension: if ECT is not set on a segment,
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000257 * and we already seen ECT on a previous segment,
258 * it is probably a retransmit.
259 */
260 if (tp->ecn_flags & TCP_ECN_SEEN)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700261 tcp_enter_quickack_mode((struct sock *)tp);
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000262 break;
263 case INET_ECN_CE:
Florian Westphal98900922014-09-26 22:37:35 +0200264 if (tcp_ca_needs_ecn((struct sock *)tp))
265 tcp_ca_event((struct sock *)tp, CA_EVENT_ECN_IS_CE);
266
Eric Dumazetaae06bf2012-08-06 11:04:43 +0000267 if (!(tp->ecn_flags & TCP_ECN_DEMAND_CWR)) {
268 /* Better not delay acks, sender can have a very low cwnd */
269 tcp_enter_quickack_mode((struct sock *)tp);
270 tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
271 }
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000272 tp->ecn_flags |= TCP_ECN_SEEN;
Florian Westphal98900922014-09-26 22:37:35 +0200273 break;
274 default:
275 if (tcp_ca_needs_ecn((struct sock *)tp))
276 tcp_ca_event((struct sock *)tp, CA_EVENT_ECN_NO_CE);
277 tp->ecn_flags |= TCP_ECN_SEEN;
278 break;
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700279 }
280}
281
Florian Westphal735d3832014-09-29 13:08:30 +0200282static void tcp_ecn_check_ce(struct tcp_sock *tp, const struct sk_buff *skb)
283{
284 if (tp->ecn_flags & TCP_ECN_OK)
285 __tcp_ecn_check_ce(tp, skb);
286}
287
288static void tcp_ecn_rcv_synack(struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700289{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800290 if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr))
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700291 tp->ecn_flags &= ~TCP_ECN_OK;
292}
293
Florian Westphal735d3832014-09-29 13:08:30 +0200294static void tcp_ecn_rcv_syn(struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700295{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800296 if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr))
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700297 tp->ecn_flags &= ~TCP_ECN_OK;
298}
299
Florian Westphal735d3832014-09-29 13:08:30 +0200300static bool tcp_ecn_rcv_ecn_echo(const struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700301{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800302 if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK))
Eric Dumazeta2a385d2012-05-16 23:15:34 +0000303 return true;
304 return false;
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700305}
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/* Buffer size and advertised window tuning.
308 *
309 * 1. Tuning sk->sk_sndbuf, when connection enters established state.
310 */
311
Eric Dumazet6ae70532013-10-01 10:23:44 -0700312static void tcp_sndbuf_expand(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Eric Dumazet6ae70532013-10-01 10:23:44 -0700314 const struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng77bfc172016-09-19 23:39:20 -0400315 const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
Eric Dumazet6ae70532013-10-01 10:23:44 -0700316 int sndmem, per_mss;
317 u32 nr_segs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Eric Dumazet6ae70532013-10-01 10:23:44 -0700319 /* Worst case is non GSO/TSO : each frame consumes one skb
320 * and skb->head is kmalloced using power of two area of memory
321 */
322 per_mss = max_t(u32, tp->rx_opt.mss_clamp, tp->mss_cache) +
323 MAX_TCP_HEADER +
324 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
325
326 per_mss = roundup_pow_of_two(per_mss) +
327 SKB_DATA_ALIGN(sizeof(struct sk_buff));
328
329 nr_segs = max_t(u32, TCP_INIT_CWND, tp->snd_cwnd);
330 nr_segs = max_t(u32, nr_segs, tp->reordering + 1);
331
332 /* Fast Recovery (RFC 5681 3.2) :
333 * Cubic needs 1.7 factor, rounded to 2 to include
334 * extra cushion (application might react slowly to POLLOUT)
335 */
Yuchung Cheng77bfc172016-09-19 23:39:20 -0400336 sndmem = ca_ops->sndbuf_expand ? ca_ops->sndbuf_expand(sk) : 2;
337 sndmem *= nr_segs * per_mss;
Eric Dumazet6ae70532013-10-01 10:23:44 -0700338
Eric Dumazet06a59ec2011-10-13 18:24:42 +0000339 if (sk->sk_sndbuf < sndmem)
340 sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343/* 2. Tuning advertised window (window_clamp, rcv_ssthresh)
344 *
345 * All tcp_full_space() is split to two parts: "network" buffer, allocated
346 * forward and advertised in receiver window (tp->rcv_wnd) and
347 * "application buffer", required to isolate scheduling/application
348 * latencies from network.
349 * window_clamp is maximal advertised window. It can be less than
350 * tcp_full_space(), in this case tcp_full_space() - window_clamp
351 * is reserved for "application" buffer. The less window_clamp is
352 * the smoother our behaviour from viewpoint of network, but the lower
353 * throughput and the higher sensitivity of the connection to losses. 8)
354 *
355 * rcv_ssthresh is more strict window_clamp used at "slow start"
356 * phase to predict further behaviour of this connection.
357 * It is used for two goals:
358 * - to enforce header prediction at sender, even when application
359 * requires some significant "application buffer". It is check #1.
360 * - to prevent pruning of receive queue because of misprediction
361 * of receiver window. Check #2.
362 *
363 * The scheme does not work when sender sends good segments opening
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800364 * window and then starts to feed us spaghetti. But it should work
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * in common situations. Otherwise, we have to rely on queue collapsing.
366 */
367
368/* Slow part of check#2. */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700369static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700371 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 /* Optimize this! */
Eric Dumazetdfd4f0a2007-12-21 06:07:53 -0800373 int truesize = tcp_win_from_space(skb->truesize) >> 1;
374 int window = tcp_win_from_space(sysctl_tcp_rmem[2]) >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 while (tp->rcv_ssthresh <= window) {
377 if (truesize <= skb->len)
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700378 return 2 * inet_csk(sk)->icsk_ack.rcv_mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 truesize >>= 1;
381 window >>= 1;
382 }
383 return 0;
384}
385
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400386static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700388 struct tcp_sock *tp = tcp_sk(sk);
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* Check #1 */
391 if (tp->rcv_ssthresh < tp->window_clamp &&
392 (int)tp->rcv_ssthresh < tcp_space(sk) &&
Eric Dumazetb8da51e2015-05-15 12:39:27 -0700393 !tcp_under_memory_pressure(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 int incr;
395
396 /* Check #2. Increase window, if skb with such overhead
397 * will fit to rcvbuf in future.
398 */
399 if (tcp_win_from_space(skb->truesize) <= skb->len)
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800400 incr = 2 * tp->advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 else
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700402 incr = __tcp_grow_window(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 if (incr) {
Eric Dumazet4d846f02012-04-16 23:28:07 +0000405 incr = max_t(int, incr, 2 * skb->len);
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800406 tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr,
407 tp->window_clamp);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700408 inet_csk(sk)->icsk_ack.quick |= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410 }
411}
412
413/* 3. Tuning rcvbuf, when connection enters established state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414static void tcp_fixup_rcvbuf(struct sock *sk)
415{
Eric Dumazete9266a02011-10-20 16:53:56 -0400416 u32 mss = tcp_sk(sk)->advmss;
Eric Dumazete9266a02011-10-20 16:53:56 -0400417 int rcvmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Yuchung Cheng85f16522013-06-11 15:35:32 -0700419 rcvmem = 2 * SKB_TRUESIZE(mss + MAX_TCP_HEADER) *
420 tcp_default_init_rwnd(mss);
Eric Dumazete9266a02011-10-20 16:53:56 -0400421
Eric Dumazetb0983d32013-09-20 13:56:58 -0700422 /* Dynamic Right Sizing (DRS) has 2 to 3 RTT latency
423 * Allow enough cushion so that sender is not limited by our window
424 */
425 if (sysctl_tcp_moderate_rcvbuf)
426 rcvmem <<= 2;
427
Eric Dumazete9266a02011-10-20 16:53:56 -0400428 if (sk->sk_rcvbuf < rcvmem)
429 sk->sk_rcvbuf = min(rcvmem, sysctl_tcp_rmem[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800432/* 4. Try to fixup all. It is made immediately after connection enters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * established state.
434 */
Jerry Chu10467162012-08-31 12:29:11 +0000435void tcp_init_buffer_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct tcp_sock *tp = tcp_sk(sk);
438 int maxwin;
439
440 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
441 tcp_fixup_rcvbuf(sk);
442 if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
Eric Dumazet6ae70532013-10-01 10:23:44 -0700443 tcp_sndbuf_expand(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 tp->rcvq_space.space = tp->rcv_wnd;
Eric Dumazet9a568de2017-05-16 14:00:14 -0700446 tcp_mstamp_refresh(tp);
Eric Dumazet645f4c62017-04-25 10:15:41 -0700447 tp->rcvq_space.time = tp->tcp_mstamp;
Eric Dumazetb0983d32013-09-20 13:56:58 -0700448 tp->rcvq_space.seq = tp->copied_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 maxwin = tcp_full_space(sk);
451
452 if (tp->window_clamp >= maxwin) {
453 tp->window_clamp = maxwin;
454
455 if (sysctl_tcp_app_win && maxwin > 4 * tp->advmss)
456 tp->window_clamp = max(maxwin -
457 (maxwin >> sysctl_tcp_app_win),
458 4 * tp->advmss);
459 }
460
461 /* Force reservation of one segment. */
462 if (sysctl_tcp_app_win &&
463 tp->window_clamp > 2 * tp->advmss &&
464 tp->window_clamp + tp->advmss > maxwin)
465 tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss);
466
467 tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
Eric Dumazetc2203cf2017-05-16 14:00:04 -0700468 tp->snd_cwnd_stamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/* 5. Recalculate window clamp after socket hit its memory bounds. */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700472static void tcp_clamp_window(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700474 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300475 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300477 icsk->icsk_ack.quick = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
John Heffner326f36e2005-11-10 17:11:48 -0800479 if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] &&
480 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
Eric Dumazetb8da51e2015-05-15 12:39:27 -0700481 !tcp_under_memory_pressure(sk) &&
Glauber Costa180d8cd2011-12-11 21:47:02 +0000482 sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)) {
John Heffner326f36e2005-11-10 17:11:48 -0800483 sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc),
484 sysctl_tcp_rmem[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
John Heffner326f36e2005-11-10 17:11:48 -0800486 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800487 tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800490/* Initialize RCV_MSS value.
491 * RCV_MSS is an our guess about MSS used by the peer.
492 * We haven't any direct information about the MSS.
493 * It's better to underestimate the RCV_MSS rather than overestimate.
494 * Overestimations make us ACKing less frequently than needed.
495 * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
496 */
497void tcp_initialize_rcv_mss(struct sock *sk)
498{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400499 const struct tcp_sock *tp = tcp_sk(sk);
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800500 unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
501
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800502 hint = min(hint, tp->rcv_wnd / 2);
William Allen Simpsonbee7ca92009-11-10 09:51:18 +0000503 hint = min(hint, TCP_MSS_DEFAULT);
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800504 hint = max(hint, TCP_MIN_MSS);
505
506 inet_csk(sk)->icsk_ack.rcv_mss = hint;
507}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000508EXPORT_SYMBOL(tcp_initialize_rcv_mss);
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510/* Receiver "autotuning" code.
511 *
512 * The algorithm for RTT estimation w/o timestamps is based on
513 * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL.
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200514 * <http://public.lanl.gov/radiant/pubs.html#DRS>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 *
516 * More detail on this code can be found at
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200517 * <http://staff.psc.edu/jheffner/>,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * though this reference is out of date. A new paper
519 * is pending.
520 */
521static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
522{
Eric Dumazet645f4c62017-04-25 10:15:41 -0700523 u32 new_sample = tp->rcv_rtt_est.rtt_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 long m = sample;
525
526 if (m == 0)
527 m = 1;
528
529 if (new_sample != 0) {
530 /* If we sample in larger samples in the non-timestamp
531 * case, we could grossly overestimate the RTT especially
532 * with chatty applications or bulk transfer apps which
533 * are stalled on filesystem I/O.
534 *
535 * Also, since we are only going for a minimum in the
Stephen Hemminger31f34262005-11-15 15:17:10 -0800536 * non-timestamp case, we do not smooth things out
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800537 * else with timestamps disabled convergence takes too
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * long.
539 */
540 if (!win_dep) {
541 m -= (new_sample >> 3);
542 new_sample += m;
Neal Cardwell18a223e2012-04-10 07:59:20 +0000543 } else {
544 m <<= 3;
545 if (m < new_sample)
546 new_sample = m;
547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 } else {
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800549 /* No previous measure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 new_sample = m << 3;
551 }
552
Eric Dumazet645f4c62017-04-25 10:15:41 -0700553 tp->rcv_rtt_est.rtt_us = new_sample;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
556static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
557{
Eric Dumazet645f4c62017-04-25 10:15:41 -0700558 u32 delta_us;
559
Eric Dumazet9a568de2017-05-16 14:00:14 -0700560 if (tp->rcv_rtt_est.time == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 goto new_measure;
562 if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq))
563 return;
Eric Dumazet9a568de2017-05-16 14:00:14 -0700564 delta_us = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcv_rtt_est.time);
Eric Dumazet645f4c62017-04-25 10:15:41 -0700565 tcp_rcv_rtt_update(tp, delta_us, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567new_measure:
568 tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd;
Eric Dumazet645f4c62017-04-25 10:15:41 -0700569 tp->rcv_rtt_est.time = tp->tcp_mstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800572static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
573 const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700575 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet9a568de2017-05-16 14:00:14 -0700576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (tp->rx_opt.rcv_tsecr &&
578 (TCP_SKB_CB(skb)->end_seq -
Eric Dumazet9a568de2017-05-16 14:00:14 -0700579 TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss)) {
580 u32 delta = tcp_time_stamp(tp) - tp->rx_opt.rcv_tsecr;
581 u32 delta_us = delta * (USEC_PER_SEC / TCP_TS_HZ);
582
583 tcp_rcv_rtt_update(tp, delta_us, 0);
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587/*
588 * This function should be called every time data is copied to user space.
589 * It calculates the appropriate TCP receive buffer space.
590 */
591void tcp_rcv_space_adjust(struct sock *sk)
592{
593 struct tcp_sock *tp = tcp_sk(sk);
594 int time;
Eric Dumazetb0983d32013-09-20 13:56:58 -0700595 int copied;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900596
Eric Dumazet9a568de2017-05-16 14:00:14 -0700597 time = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcvq_space.time);
Eric Dumazet645f4c62017-04-25 10:15:41 -0700598 if (time < (tp->rcv_rtt_est.rtt_us >> 3) || tp->rcv_rtt_est.rtt_us == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900600
Eric Dumazetb0983d32013-09-20 13:56:58 -0700601 /* Number of bytes copied to user in last RTT */
602 copied = tp->copied_seq - tp->rcvq_space.seq;
603 if (copied <= tp->rcvq_space.space)
604 goto new_measure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Eric Dumazetb0983d32013-09-20 13:56:58 -0700606 /* A bit of theory :
607 * copied = bytes received in previous RTT, our base window
608 * To cope with packet losses, we need a 2x factor
609 * To cope with slow start, and sender growing its cwin by 100 %
610 * every RTT, we need a 4x factor, because the ACK we are sending
611 * now is for the next RTT, not the current one :
612 * <prev RTT . ><current RTT .. ><next RTT .... >
613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Eric Dumazetb0983d32013-09-20 13:56:58 -0700615 if (sysctl_tcp_moderate_rcvbuf &&
616 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
617 int rcvwin, rcvmem, rcvbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Eric Dumazetb0983d32013-09-20 13:56:58 -0700619 /* minimal window to cope with packet losses, assuming
620 * steady state. Add some cushion because of small variations.
621 */
622 rcvwin = (copied << 1) + 16 * tp->advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Eric Dumazetb0983d32013-09-20 13:56:58 -0700624 /* If rate increased by 25%,
625 * assume slow start, rcvwin = 3 * copied
626 * If rate increased by 50%,
627 * assume sender can use 2x growth, rcvwin = 4 * copied
628 */
629 if (copied >=
630 tp->rcvq_space.space + (tp->rcvq_space.space >> 2)) {
631 if (copied >=
632 tp->rcvq_space.space + (tp->rcvq_space.space >> 1))
633 rcvwin <<= 1;
634 else
635 rcvwin += (rcvwin >> 1);
636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Eric Dumazetb0983d32013-09-20 13:56:58 -0700638 rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER);
639 while (tcp_win_from_space(rcvmem) < tp->advmss)
640 rcvmem += 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Eric Dumazetb0983d32013-09-20 13:56:58 -0700642 rcvbuf = min(rcvwin / tp->advmss * rcvmem, sysctl_tcp_rmem[2]);
643 if (rcvbuf > sk->sk_rcvbuf) {
644 sk->sk_rcvbuf = rcvbuf;
645
646 /* Make the window clamp follow along. */
647 tp->window_clamp = rcvwin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649 }
Eric Dumazetb0983d32013-09-20 13:56:58 -0700650 tp->rcvq_space.space = copied;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652new_measure:
653 tp->rcvq_space.seq = tp->copied_seq;
Eric Dumazet645f4c62017-04-25 10:15:41 -0700654 tp->rcvq_space.time = tp->tcp_mstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657/* There is something which you must keep in mind when you analyze the
658 * behavior of the tp->ato delayed ack timeout interval. When a
659 * connection starts up, we want to ack as quickly as possible. The
660 * problem is that "good" TCP's do slow start at the beginning of data
661 * transmission. The means that until we send the first few ACK's the
662 * sender will sit on his end and only queue most of his data, because
663 * he can only send snd_cwnd unacked packets at any given time. For
664 * each ACK we send, he increments snd_cwnd and transmits more of his
665 * queue. -DaveM
666 */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700667static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700669 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700670 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 u32 now;
672
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700673 inet_csk_schedule_ack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700675 tcp_measure_rcv_mss(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 tcp_rcv_rtt_measure(tp);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900678
Eric Dumazet70eabf02017-05-16 14:00:07 -0700679 now = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700681 if (!icsk->icsk_ack.ato) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* The _first_ data packet received, initialize
683 * delayed ACK engine.
684 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700685 tcp_incr_quickack(sk);
686 icsk->icsk_ack.ato = TCP_ATO_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 } else {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700688 int m = now - icsk->icsk_ack.lrcvtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800690 if (m <= TCP_ATO_MIN / 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /* The fastest case is the first. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700692 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2;
693 } else if (m < icsk->icsk_ack.ato) {
694 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m;
695 if (icsk->icsk_ack.ato > icsk->icsk_rto)
696 icsk->icsk_ack.ato = icsk->icsk_rto;
697 } else if (m > icsk->icsk_rto) {
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800698 /* Too long gap. Apparently sender failed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * restart window, so that we send ACKs quickly.
700 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700701 tcp_incr_quickack(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800702 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700705 icsk->icsk_ack.lrcvtime = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Florian Westphal735d3832014-09-29 13:08:30 +0200707 tcp_ecn_check_ce(tp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 if (skb->len >= 128)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700710 tcp_grow_window(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713/* Called to compute a smoothed rtt estimate. The data fed to this
714 * routine either comes from timestamps, or from segments that were
715 * known _not_ to have been retransmitted [see Karn/Partridge
716 * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88
717 * piece by Van Jacobson.
718 * NOTE: the next three routines used to be one big routine.
719 * To save cycles in the RFC 1323 implementation it was better to break
720 * it up into three procedures. -- erics
721 */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800722static void tcp_rtt_estimator(struct sock *sk, long mrtt_us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300724 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet740b0f12014-02-26 14:02:48 -0800725 long m = mrtt_us; /* RTT */
726 u32 srtt = tp->srtt_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* The following amusing code comes from Jacobson's
729 * article in SIGCOMM '88. Note that rtt and mdev
730 * are scaled versions of rtt and mean deviation.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900731 * This is designed to be as fast as possible
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 * m stands for "measurement".
733 *
734 * On a 1990 paper the rto value is changed to:
735 * RTO = rtt + 4 * mdev
736 *
737 * Funny. This algorithm seems to be very broken.
738 * These formulae increase RTO, when it should be decreased, increase
Stephen Hemminger31f34262005-11-15 15:17:10 -0800739 * too slowly, when it should be increased quickly, decrease too quickly
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * etc. I guess in BSD RTO takes ONE value, so that it is absolutely
741 * does not matter how to _calculate_ it. Seems, it was trap
742 * that VJ failed to avoid. 8)
743 */
Eric Dumazet4a5ab4e2014-02-06 15:57:10 -0800744 if (srtt != 0) {
745 m -= (srtt >> 3); /* m is now error in rtt est */
746 srtt += m; /* rtt = 7/8 rtt + 1/8 new */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (m < 0) {
748 m = -m; /* m is now abs(error) */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800749 m -= (tp->mdev_us >> 2); /* similar update on mdev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* This is similar to one of Eifel findings.
751 * Eifel blocks mdev updates when rtt decreases.
752 * This solution is a bit different: we use finer gain
753 * for mdev in this case (alpha*beta).
754 * Like Eifel it also prevents growth of rto,
755 * but also it limits too fast rto decreases,
756 * happening in pure Eifel.
757 */
758 if (m > 0)
759 m >>= 3;
760 } else {
Eric Dumazet740b0f12014-02-26 14:02:48 -0800761 m -= (tp->mdev_us >> 2); /* similar update on mdev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Eric Dumazet740b0f12014-02-26 14:02:48 -0800763 tp->mdev_us += m; /* mdev = 3/4 mdev + 1/4 new */
764 if (tp->mdev_us > tp->mdev_max_us) {
765 tp->mdev_max_us = tp->mdev_us;
766 if (tp->mdev_max_us > tp->rttvar_us)
767 tp->rttvar_us = tp->mdev_max_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 if (after(tp->snd_una, tp->rtt_seq)) {
Eric Dumazet740b0f12014-02-26 14:02:48 -0800770 if (tp->mdev_max_us < tp->rttvar_us)
771 tp->rttvar_us -= (tp->rttvar_us - tp->mdev_max_us) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 tp->rtt_seq = tp->snd_nxt;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800773 tp->mdev_max_us = tcp_rto_min_us(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 } else {
776 /* no previous measure. */
Eric Dumazet4a5ab4e2014-02-06 15:57:10 -0800777 srtt = m << 3; /* take the measured time to be rtt */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800778 tp->mdev_us = m << 1; /* make sure rto = 3*rtt */
779 tp->rttvar_us = max(tp->mdev_us, tcp_rto_min_us(sk));
780 tp->mdev_max_us = tp->rttvar_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 tp->rtt_seq = tp->snd_nxt;
782 }
Eric Dumazet740b0f12014-02-26 14:02:48 -0800783 tp->srtt_us = max(1U, srtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700786/* Set the sk_pacing_rate to allow proper sizing of TSO packets.
787 * Note: TCP stack does not yet implement pacing.
788 * FQ packet scheduler can be used to implement cheap but effective
789 * TCP pacing, to smooth the burst on large writes when packets
790 * in flight is significantly lower than cwnd (or rwin)
791 */
Eric Dumazet43e122b2015-08-21 17:38:02 -0700792int sysctl_tcp_pacing_ss_ratio __read_mostly = 200;
793int sysctl_tcp_pacing_ca_ratio __read_mostly = 120;
794
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700795static void tcp_update_pacing_rate(struct sock *sk)
796{
797 const struct tcp_sock *tp = tcp_sk(sk);
798 u64 rate;
799
800 /* set sk_pacing_rate to 200 % of current rate (mss * cwnd / srtt) */
Eric Dumazet43e122b2015-08-21 17:38:02 -0700801 rate = (u64)tp->mss_cache * ((USEC_PER_SEC / 100) << 3);
802
803 /* current rate is (cwnd * mss) / srtt
804 * In Slow Start [1], set sk_pacing_rate to 200 % the current rate.
805 * In Congestion Avoidance phase, set it to 120 % the current rate.
806 *
807 * [1] : Normal Slow Start condition is (tp->snd_cwnd < tp->snd_ssthresh)
808 * If snd_cwnd >= (tp->snd_ssthresh / 2), we are approaching
809 * end of slow start and should slow down.
810 */
811 if (tp->snd_cwnd < tp->snd_ssthresh / 2)
812 rate *= sysctl_tcp_pacing_ss_ratio;
813 else
814 rate *= sysctl_tcp_pacing_ca_ratio;
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700815
816 rate *= max(tp->snd_cwnd, tp->packets_out);
817
Eric Dumazet740b0f12014-02-26 14:02:48 -0800818 if (likely(tp->srtt_us))
819 do_div(rate, tp->srtt_us);
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700820
Eric Dumazetba537422013-10-09 17:14:52 -0700821 /* ACCESS_ONCE() is needed because sch_fq fetches sk_pacing_rate
822 * without any lock. We want to make sure compiler wont store
823 * intermediate values in this location.
824 */
825 ACCESS_ONCE(sk->sk_pacing_rate) = min_t(u64, rate,
826 sk->sk_max_pacing_rate);
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700827}
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829/* Calculate rto without backoff. This is the second half of Van Jacobson's
830 * routine referred to above.
831 */
stephen hemmingerf7e56a72013-12-29 11:39:51 -0800832static void tcp_set_rto(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700834 const struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /* Old crap is replaced with new one. 8)
836 *
837 * More seriously:
838 * 1. If rtt variance happened to be less 50msec, it is hallucination.
839 * It cannot be less due to utterly erratic ACK generation made
840 * at least by solaris and freebsd. "Erratic ACKs" has _nothing_
841 * to do with delayed acks, because at cwnd>2 true delack timeout
842 * is invisible. Actually, Linux-2.4 also generates erratic
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800843 * ACKs in some circumstances.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 */
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000845 inet_csk(sk)->icsk_rto = __tcp_set_rto(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 /* 2. Fixups made earlier cannot be right.
848 * If we do not estimate RTO correctly without them,
849 * all the algo is pure shit and should be replaced
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800850 * with correct one. It is exactly, which we pretend to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Ilpo Järvinenee6aac52008-12-05 22:43:08 -0800853 /* NOTE: clamping at TCP_RTO_MIN is not required, current algo
854 * guarantees that rto is higher.
855 */
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000856 tcp_bound_rto(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400859__u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
862
Gerrit Renker22b71c82010-08-29 19:23:12 +0000863 if (!cwnd)
David S. Miller442b9632011-02-02 17:05:11 -0800864 cwnd = TCP_INIT_CWND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
866}
867
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300868/*
869 * Packet counting of FACK is based on in-order assumptions, therefore TCP
870 * disables it when reordering is detected
871 */
David S. Miller4aabd8e2012-07-09 16:07:30 -0700872void tcp_disable_fack(struct tcp_sock *tp)
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300873{
Ilpo Järvinen85cc3912007-11-15 19:39:31 -0800874 /* RFC3517 uses different metric in lost marker => reset on change */
875 if (tcp_is_fack(tp))
876 tp->lost_skb_hint = NULL;
Vijay Subramanianab562222011-12-20 13:23:24 +0000877 tp->rx_opt.sack_ok &= ~TCP_FACK_ENABLED;
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300878}
879
Ryousei Takano564262c2007-10-25 23:03:52 -0700880/* Take a notice that peer is sending D-SACKs */
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300881static void tcp_dsack_seen(struct tcp_sock *tp)
882{
Vijay Subramanianab562222011-12-20 13:23:24 +0000883 tp->rx_opt.sack_ok |= TCP_DSACK_SEEN;
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300884}
885
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300886static void tcp_update_reordering(struct sock *sk, const int metric,
887 const int ts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300889 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng2d2517e2017-04-04 14:15:40 -0700890 int mib_idx;
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700891
Soheil Hassas Yeganeh6f5b24e2017-05-16 17:39:02 -0400892 if (WARN_ON_ONCE(metric < 0))
893 return;
894
Yuchung Cheng2d2517e2017-04-04 14:15:40 -0700895 if (metric > tp->reordering) {
Eric Dumazetdca145f2014-10-27 21:45:24 -0700896 tp->reordering = min(sysctl_tcp_max_reordering, metric);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898#if FASTRETRANS_DEBUG > 1
Joe Perches91df42b2012-05-15 14:11:54 +0000899 pr_debug("Disorder%d %d %u f%u s%u rr%d\n",
900 tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state,
901 tp->reordering,
902 tp->fackets_out,
903 tp->sacked_out,
904 tp->undo_marker ? tp->undo_retrans : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905#endif
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300906 tcp_disable_fack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Yuchung Chengeed530b2012-05-02 13:30:03 +0000908
Yuchung Cheng4f41b1c2015-10-16 21:57:47 -0700909 tp->rack.reord = 1;
Yuchung Cheng2d2517e2017-04-04 14:15:40 -0700910
911 /* This exciting event is worth to be remembered. 8) */
912 if (ts)
913 mib_idx = LINUX_MIB_TCPTSREORDER;
914 else if (tcp_is_reno(tp))
915 mib_idx = LINUX_MIB_TCPRENOREORDER;
916 else if (tcp_is_fack(tp))
917 mib_idx = LINUX_MIB_TCPFACKREORDER;
918 else
919 mib_idx = LINUX_MIB_TCPSACKREORDER;
920
921 NET_INC_STATS(sock_net(sk), mib_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Ilpo Järvinen006f5822008-09-20 21:20:20 -0700924/* This must be called before lost_out is incremented */
Ilpo Järvinenc8c213f2008-09-20 21:18:55 -0700925static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
926{
Ian Morris51456b22015-04-03 09:17:26 +0100927 if (!tp->retransmit_skb_hint ||
Ilpo Järvinenc8c213f2008-09-20 21:18:55 -0700928 before(TCP_SKB_CB(skb)->seq,
929 TCP_SKB_CB(tp->retransmit_skb_hint)->seq))
Ilpo Järvinen006f5822008-09-20 21:20:20 -0700930 tp->retransmit_skb_hint = skb;
Ilpo Järvinenc8c213f2008-09-20 21:18:55 -0700931}
932
Neal Cardwell0682e692016-09-19 23:39:13 -0400933/* Sum the number of packets on the wire we have marked as lost.
934 * There are two cases we care about here:
935 * a) Packet hasn't been marked lost (nor retransmitted),
936 * and this is the first loss.
937 * b) Packet has been marked both lost and retransmitted,
938 * and this means we think it was lost again.
939 */
940static void tcp_sum_lost(struct tcp_sock *tp, struct sk_buff *skb)
941{
942 __u8 sacked = TCP_SKB_CB(skb)->sacked;
943
944 if (!(sacked & TCPCB_LOST) ||
945 ((sacked & TCPCB_LOST) && (sacked & TCPCB_SACKED_RETRANS)))
946 tp->lost += tcp_skb_pcount(skb);
947}
948
Ilpo Järvinen41ea36e2008-09-20 21:19:22 -0700949static void tcp_skb_mark_lost(struct tcp_sock *tp, struct sk_buff *skb)
950{
951 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
952 tcp_verify_retransmit_hint(tp, skb);
953
954 tp->lost_out += tcp_skb_pcount(skb);
Neal Cardwell0682e692016-09-19 23:39:13 -0400955 tcp_sum_lost(tp, skb);
Ilpo Järvinen41ea36e2008-09-20 21:19:22 -0700956 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
957 }
958}
959
Yuchung Cheng4f41b1c2015-10-16 21:57:47 -0700960void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp, struct sk_buff *skb)
Ilpo Järvinen006f5822008-09-20 21:20:20 -0700961{
962 tcp_verify_retransmit_hint(tp, skb);
963
Neal Cardwell0682e692016-09-19 23:39:13 -0400964 tcp_sum_lost(tp, skb);
Ilpo Järvinen006f5822008-09-20 21:20:20 -0700965 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
966 tp->lost_out += tcp_skb_pcount(skb);
967 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
968 }
969}
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/* This procedure tags the retransmission queue when SACKs arrive.
972 *
973 * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L).
974 * Packets in queue with these bits set are counted in variables
975 * sacked_out, retrans_out and lost_out, correspondingly.
976 *
977 * Valid combinations are:
978 * Tag InFlight Description
979 * 0 1 - orig segment is in flight.
980 * S 0 - nothing flies, orig reached receiver.
981 * L 0 - nothing flies, orig lost by net.
982 * R 2 - both orig and retransmit are in flight.
983 * L|R 1 - orig is lost, retransmit is in flight.
984 * S|R 1 - orig reached receiver, retrans is still in flight.
985 * (L|S|R is logically valid, it could occur when L|R is sacked,
986 * but it is equivalent to plain S and code short-curcuits it to S.
987 * L|S is logically invalid, it would mean -1 packet in flight 8))
988 *
989 * These 6 states form finite state machine, controlled by the following events:
990 * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())
991 * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())
Yuchung Cheng974c1232012-01-19 14:42:21 +0000992 * 3. Loss detection event of two flavors:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 * A. Scoreboard estimator decided the packet is lost.
994 * A'. Reno "three dupacks" marks head of queue lost.
Yuchung Cheng974c1232012-01-19 14:42:21 +0000995 * A''. Its FACK modification, head until snd.fack is lost.
996 * B. SACK arrives sacking SND.NXT at the moment, when the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 * segment was retransmitted.
998 * 4. D-SACK added new rule: D-SACK changes any tag to S.
999 *
1000 * It is pleasant to note, that state diagram turns out to be commutative,
1001 * so that we are allowed not to be bothered by order of our actions,
1002 * when multiple events arrive simultaneously. (see the function below).
1003 *
1004 * Reordering detection.
1005 * --------------------
1006 * Reordering metric is maximal distance, which a packet can be displaced
1007 * in packet stream. With SACKs we can estimate it:
1008 *
1009 * 1. SACK fills old hole and the corresponding segment was not
1010 * ever retransmitted -> reordering. Alas, we cannot use it
1011 * when segment was retransmitted.
1012 * 2. The last flaw is solved with D-SACK. D-SACK arrives
1013 * for retransmitted and already SACKed segment -> reordering..
1014 * Both of these heuristics are not used in Loss state, when we cannot
1015 * account for retransmits accurately.
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001016 *
1017 * SACK block validation.
1018 * ----------------------
1019 *
1020 * SACK block range validation checks that the received SACK block fits to
1021 * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT.
1022 * Note that SND.UNA is not included to the range though being valid because
Ilpo Järvinen0e835332007-10-01 15:28:17 -07001023 * it means that the receiver is rather inconsistent with itself reporting
1024 * SACK reneging when it should advance SND.UNA. Such SACK block this is
1025 * perfectly valid, however, in light of RFC2018 which explicitly states
1026 * that "SACK block MUST reflect the newest segment. Even if the newest
1027 * segment is going to be discarded ...", not that it looks very clever
1028 * in case of head skb. Due to potentional receiver driven attacks, we
1029 * choose to avoid immediate execution of a walk in write queue due to
1030 * reneging and defer head skb's loss recovery to standard loss recovery
1031 * procedure that will eventually trigger (nothing forbids us doing this).
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001032 *
1033 * Implements also blockage to start_seq wrap-around. Problem lies in the
1034 * fact that though start_seq (s) is before end_seq (i.e., not reversed),
1035 * there's no guarantee that it will be before snd_nxt (n). The problem
1036 * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt
1037 * wrap (s_w):
1038 *
1039 * <- outs wnd -> <- wrapzone ->
1040 * u e n u_w e_w s n_w
1041 * | | | | | | |
1042 * |<------------+------+----- TCP seqno space --------------+---------->|
1043 * ...-- <2^31 ->| |<--------...
1044 * ...---- >2^31 ------>| |<--------...
1045 *
1046 * Current code wouldn't be vulnerable but it's better still to discard such
1047 * crazy SACK blocks. Doing this check for start_seq alone closes somewhat
1048 * similar case (end_seq after snd_nxt wrap) as earlier reversed check in
1049 * snd_nxt wrap -> snd_una region will then become "well defined", i.e.,
1050 * equal to the ideal case (infinite seqno space without wrap caused issues).
1051 *
1052 * With D-SACK the lower bound is extended to cover sequence space below
1053 * SND.UNA down to undo_marker, which is the last point of interest. Yet
Ryousei Takano564262c2007-10-25 23:03:52 -07001054 * again, D-SACK block must not to go across snd_una (for the same reason as
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001055 * for the normal SACK blocks, explained above). But there all simplicity
1056 * ends, TCP might receive valid D-SACKs below that. As long as they reside
1057 * fully below undo_marker they do not affect behavior in anyway and can
1058 * therefore be safely ignored. In rare cases (which are more or less
1059 * theoretical ones), the D-SACK will nicely cross that boundary due to skb
1060 * fragmentation and packet reordering past skb's retransmission. To consider
1061 * them correctly, the acceptable range must be extended even more though
1062 * the exact amount is rather hard to quantify. However, tp->max_window can
1063 * be used as an exaggerated estimate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001065static bool tcp_is_sackblock_valid(struct tcp_sock *tp, bool is_dsack,
1066 u32 start_seq, u32 end_seq)
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001067{
1068 /* Too far in future, or reversed (interpretation is ambiguous) */
1069 if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001070 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001071
1072 /* Nasty start_seq wrap-around check (see comments above) */
1073 if (!before(start_seq, tp->snd_nxt))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001074 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001075
Ryousei Takano564262c2007-10-25 23:03:52 -07001076 /* In outstanding window? ...This is valid exit for D-SACKs too.
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001077 * start_seq == snd_una is non-sensical (see comments above)
1078 */
1079 if (after(start_seq, tp->snd_una))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001080 return true;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001081
1082 if (!is_dsack || !tp->undo_marker)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001083 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001084
1085 /* ...Then it's D-SACK, and must reside below snd_una completely */
Zheng Yanf779b2d2011-09-18 22:37:34 -04001086 if (after(end_seq, tp->snd_una))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001087 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001088
1089 if (!before(start_seq, tp->undo_marker))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001090 return true;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001091
1092 /* Too old */
1093 if (!after(end_seq, tp->undo_marker))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001094 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001095
1096 /* Undo_marker boundary crossing (overestimates a lot). Known already:
1097 * start_seq < undo_marker and end_seq >= undo_marker.
1098 */
1099 return !before(start_seq, end_seq - tp->max_window);
1100}
1101
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001102static bool tcp_check_dsack(struct sock *sk, const struct sk_buff *ack_skb,
1103 struct tcp_sack_block_wire *sp, int num_sacks,
1104 u32 prior_snd_una)
David S. Millerd06e0212007-06-18 22:43:06 -07001105{
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07001106 struct tcp_sock *tp = tcp_sk(sk);
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001107 u32 start_seq_0 = get_unaligned_be32(&sp[0].start_seq);
1108 u32 end_seq_0 = get_unaligned_be32(&sp[0].end_seq);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001109 bool dup_sack = false;
David S. Millerd06e0212007-06-18 22:43:06 -07001110
1111 if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) {
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001112 dup_sack = true;
Ilpo Järvinene60402d2007-08-09 15:14:46 +03001113 tcp_dsack_seen(tp);
Eric Dumazetc10d9312016-04-29 14:16:47 -07001114 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKRECV);
David S. Millerd06e0212007-06-18 22:43:06 -07001115 } else if (num_sacks > 1) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001116 u32 end_seq_1 = get_unaligned_be32(&sp[1].end_seq);
1117 u32 start_seq_1 = get_unaligned_be32(&sp[1].start_seq);
David S. Millerd06e0212007-06-18 22:43:06 -07001118
1119 if (!after(end_seq_0, end_seq_1) &&
1120 !before(start_seq_0, start_seq_1)) {
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001121 dup_sack = true;
Ilpo Järvinene60402d2007-08-09 15:14:46 +03001122 tcp_dsack_seen(tp);
Eric Dumazetc10d9312016-04-29 14:16:47 -07001123 NET_INC_STATS(sock_net(sk),
Pavel Emelyanovde0744a2008-07-16 20:31:16 -07001124 LINUX_MIB_TCPDSACKOFORECV);
David S. Millerd06e0212007-06-18 22:43:06 -07001125 }
1126 }
1127
1128 /* D-SACK for already forgotten data... Do dumb counting. */
Yuchung Cheng6e08d5e2014-07-02 12:07:16 -07001129 if (dup_sack && tp->undo_marker && tp->undo_retrans > 0 &&
David S. Millerd06e0212007-06-18 22:43:06 -07001130 !after(end_seq_0, prior_snd_una) &&
1131 after(end_seq_0, tp->undo_marker))
1132 tp->undo_retrans--;
1133
1134 return dup_sack;
1135}
1136
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001137struct tcp_sacktag_state {
Eric Dumazet740b0f12014-02-26 14:02:48 -08001138 int reord;
1139 int fack_count;
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02001140 /* Timestamps for earliest and latest never-retransmitted segment
1141 * that was SACKed. RTO needs the earliest RTT to stay conservative,
1142 * but congestion control should still get an accurate delay signal.
1143 */
Eric Dumazet9a568de2017-05-16 14:00:14 -07001144 u64 first_sackt;
1145 u64 last_sackt;
Yuchung Chengb9f64822016-09-19 23:39:14 -04001146 struct rate_sample *rate;
Eric Dumazet740b0f12014-02-26 14:02:48 -08001147 int flag;
Eric Dumazet75c119a2017-10-05 22:21:27 -07001148 unsigned int mss_now;
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001149};
1150
Ilpo Järvinend1935942007-10-11 17:34:25 -07001151/* Check if skb is fully within the SACK block. In presence of GSO skbs,
1152 * the incoming SACK may not exactly match but we can find smaller MSS
1153 * aligned portion of it that matches. Therefore we might need to fragment
1154 * which may fail and creates some hassle (caller must handle error case
1155 * returns).
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001156 *
1157 * FIXME: this could be merged to shift decision code
Ilpo Järvinend1935942007-10-11 17:34:25 -07001158 */
Adrian Bunk0f79efd2007-10-26 03:57:36 -07001159static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001160 u32 start_seq, u32 end_seq)
Ilpo Järvinend1935942007-10-11 17:34:25 -07001161{
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001162 int err;
1163 bool in_sack;
Ilpo Järvinend1935942007-10-11 17:34:25 -07001164 unsigned int pkt_len;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001165 unsigned int mss;
Ilpo Järvinend1935942007-10-11 17:34:25 -07001166
1167 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1168 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1169
1170 if (tcp_skb_pcount(skb) > 1 && !in_sack &&
1171 after(TCP_SKB_CB(skb)->end_seq, start_seq)) {
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001172 mss = tcp_skb_mss(skb);
Ilpo Järvinend1935942007-10-11 17:34:25 -07001173 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1174
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001175 if (!in_sack) {
Ilpo Järvinend1935942007-10-11 17:34:25 -07001176 pkt_len = start_seq - TCP_SKB_CB(skb)->seq;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001177 if (pkt_len < mss)
1178 pkt_len = mss;
1179 } else {
Ilpo Järvinend1935942007-10-11 17:34:25 -07001180 pkt_len = end_seq - TCP_SKB_CB(skb)->seq;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001181 if (pkt_len < mss)
1182 return -EINVAL;
1183 }
1184
1185 /* Round if necessary so that SACKs cover only full MSSes
1186 * and/or the remaining small portion (if present)
1187 */
1188 if (pkt_len > mss) {
1189 unsigned int new_len = (pkt_len / mss) * mss;
Yuchung Chengb451e5d2017-05-10 17:01:27 -07001190 if (!in_sack && new_len < pkt_len)
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001191 new_len += mss;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001192 pkt_len = new_len;
1193 }
Yuchung Chengb451e5d2017-05-10 17:01:27 -07001194
1195 if (pkt_len >= skb->len && !in_sack)
1196 return 0;
1197
Eric Dumazet75c119a2017-10-05 22:21:27 -07001198 err = tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb,
1199 pkt_len, mss, GFP_ATOMIC);
Ilpo Järvinend1935942007-10-11 17:34:25 -07001200 if (err < 0)
1201 return err;
1202 }
1203
1204 return in_sack;
1205}
1206
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001207/* Mark the given newly-SACKed range as such, adjusting counters and hints. */
1208static u8 tcp_sacktag_one(struct sock *sk,
1209 struct tcp_sacktag_state *state, u8 sacked,
1210 u32 start_seq, u32 end_seq,
Eric Dumazet740b0f12014-02-26 14:02:48 -08001211 int dup_sack, int pcount,
Eric Dumazet9a568de2017-05-16 14:00:14 -07001212 u64 xmit_time)
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001213{
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001214 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001215 int fack_count = state->fack_count;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001216
1217 /* Account D-SACK for retransmitted packet. */
1218 if (dup_sack && (sacked & TCPCB_RETRANS)) {
Yuchung Cheng6e08d5e2014-07-02 12:07:16 -07001219 if (tp->undo_marker && tp->undo_retrans > 0 &&
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001220 after(end_seq, tp->undo_marker))
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001221 tp->undo_retrans--;
Ilpo Järvinenede9f3b2007-12-02 00:47:58 +02001222 if (sacked & TCPCB_SACKED_ACKED)
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001223 state->reord = min(fack_count, state->reord);
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001224 }
1225
1226 /* Nothing to do; acked frame is about to be dropped (was ACKed). */
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001227 if (!after(end_seq, tp->snd_una))
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001228 return sacked;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001229
1230 if (!(sacked & TCPCB_SACKED_ACKED)) {
Eric Dumazetd2329f12017-04-25 10:15:38 -07001231 tcp_rack_advance(tp, sacked, end_seq, xmit_time);
Yuchung Cheng659a8ad2015-10-16 21:57:46 -07001232
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001233 if (sacked & TCPCB_SACKED_RETRANS) {
1234 /* If the segment is not tagged as lost,
1235 * we do not clear RETRANS, believing
1236 * that retransmission is still in flight.
1237 */
1238 if (sacked & TCPCB_LOST) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001239 sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS);
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001240 tp->lost_out -= pcount;
1241 tp->retrans_out -= pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001242 }
1243 } else {
1244 if (!(sacked & TCPCB_RETRANS)) {
1245 /* New sack for not retransmitted frame,
1246 * which was in hole. It is reordering.
1247 */
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001248 if (before(start_seq,
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001249 tcp_highest_sack_seq(tp)))
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001250 state->reord = min(fack_count,
1251 state->reord);
Yuchung Chenge33099f2013-03-20 13:33:00 +00001252 if (!after(end_seq, tp->high_seq))
1253 state->flag |= FLAG_ORIG_SACK_ACKED;
Eric Dumazet9a568de2017-05-16 14:00:14 -07001254 if (state->first_sackt == 0)
1255 state->first_sackt = xmit_time;
1256 state->last_sackt = xmit_time;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001257 }
1258
1259 if (sacked & TCPCB_LOST) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001260 sacked &= ~TCPCB_LOST;
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001261 tp->lost_out -= pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001262 }
1263 }
1264
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001265 sacked |= TCPCB_SACKED_ACKED;
1266 state->flag |= FLAG_DATA_SACKED;
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001267 tp->sacked_out += pcount;
Yuchung Chengddf1af62016-02-02 10:33:06 -08001268 tp->delivered += pcount; /* Out-of-order packets delivered */
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001269
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001270 fack_count += pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001271
1272 /* Lost marker hint past SACKed? Tweak RFC3517 cnt */
Ian Morris00db4122015-04-03 09:17:27 +01001273 if (!tcp_is_fack(tp) && tp->lost_skb_hint &&
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001274 before(start_seq, TCP_SKB_CB(tp->lost_skb_hint)->seq))
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001275 tp->lost_cnt_hint += pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001276
1277 if (fack_count > tp->fackets_out)
1278 tp->fackets_out = fack_count;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001279 }
1280
1281 /* D-SACK. We can detect redundant retransmission in S|R and plain R
1282 * frames and clear it. undo_retrans is decreased above, L|R frames
1283 * are accounted above as well.
1284 */
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001285 if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) {
1286 sacked &= ~TCPCB_SACKED_RETRANS;
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001287 tp->retrans_out -= pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001288 }
1289
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001290 return sacked;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001291}
1292
Neal Cardwelldaef52b2012-02-12 18:37:10 +00001293/* Shift newly-SACKed bytes from this skb to the immediately previous
1294 * already-SACKed sk_buff. Mark the newly-SACKed bytes as such.
1295 */
Eric Dumazetf3319812017-10-05 22:21:26 -07001296static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *prev,
1297 struct sk_buff *skb,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001298 struct tcp_sacktag_state *state,
1299 unsigned int pcount, int shifted, int mss,
1300 bool dup_sack)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001301{
1302 struct tcp_sock *tp = tcp_sk(sk);
Neal Cardwelldaef52b2012-02-12 18:37:10 +00001303 u32 start_seq = TCP_SKB_CB(skb)->seq; /* start of newly-SACKed */
1304 u32 end_seq = start_seq + shifted; /* end of newly-SACKed */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001305
1306 BUG_ON(!pcount);
1307
Neal Cardwell4c90d3b2012-02-26 10:06:19 +00001308 /* Adjust counters and hints for the newly sacked sequence
1309 * range but discard the return value since prev is already
1310 * marked. We must tag the range first because the seq
1311 * advancement below implicitly advances
1312 * tcp_highest_sack_seq() when skb is highest_sack.
1313 */
1314 tcp_sacktag_one(sk, state, TCP_SKB_CB(skb)->sacked,
Yuchung Cheng59c9af42013-07-22 16:20:47 -07001315 start_seq, end_seq, dup_sack, pcount,
Eric Dumazet9a568de2017-05-16 14:00:14 -07001316 skb->skb_mstamp);
Yuchung Chengb9f64822016-09-19 23:39:14 -04001317 tcp_rate_skb_delivered(sk, skb, state->rate);
Neal Cardwell4c90d3b2012-02-26 10:06:19 +00001318
1319 if (skb == tp->lost_skb_hint)
Neal Cardwell0af2a0d2012-02-13 20:22:08 +00001320 tp->lost_cnt_hint += pcount;
1321
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001322 TCP_SKB_CB(prev)->end_seq += shifted;
1323 TCP_SKB_CB(skb)->seq += shifted;
1324
Eric Dumazetcd7d8492014-09-24 04:11:22 -07001325 tcp_skb_pcount_add(prev, pcount);
1326 BUG_ON(tcp_skb_pcount(skb) < pcount);
1327 tcp_skb_pcount_add(skb, -pcount);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001328
1329 /* When we're adding to gso_segs == 1, gso_size will be zero,
1330 * in theory this shouldn't be necessary but as long as DSACK
1331 * code can come after this skb later on it's better to keep
1332 * setting gso_size to something.
1333 */
Eric Dumazetf69ad292015-06-11 09:15:18 -07001334 if (!TCP_SKB_CB(prev)->tcp_gso_size)
1335 TCP_SKB_CB(prev)->tcp_gso_size = mss;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001336
1337 /* CHECKME: To clear or not to clear? Mimics normal skb currently */
Eric Dumazet51466a72015-06-11 09:15:16 -07001338 if (tcp_skb_pcount(skb) <= 1)
Eric Dumazetf69ad292015-06-11 09:15:18 -07001339 TCP_SKB_CB(skb)->tcp_gso_size = 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001340
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001341 /* Difference in this won't matter, both ACKed by the same cumul. ACK */
1342 TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS);
1343
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001344 if (skb->len > 0) {
1345 BUG_ON(!tcp_skb_pcount(skb));
Eric Dumazetc10d9312016-04-29 14:16:47 -07001346 NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKSHIFTED);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001347 return false;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001348 }
1349
1350 /* Whole SKB was eaten :-) */
1351
Ilpo Järvinen92ee76b2008-11-24 21:26:56 -08001352 if (skb == tp->retransmit_skb_hint)
1353 tp->retransmit_skb_hint = prev;
Ilpo Järvinen92ee76b2008-11-24 21:26:56 -08001354 if (skb == tp->lost_skb_hint) {
1355 tp->lost_skb_hint = prev;
1356 tp->lost_cnt_hint -= tcp_skb_pcount(prev);
1357 }
1358
Eric Dumazet5e8a402f2013-10-04 10:31:41 -07001359 TCP_SKB_CB(prev)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags;
Martin KaFai Laua643b5d2016-04-25 14:44:49 -07001360 TCP_SKB_CB(prev)->eor = TCP_SKB_CB(skb)->eor;
Eric Dumazet5e8a402f2013-10-04 10:31:41 -07001361 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
1362 TCP_SKB_CB(prev)->end_seq++;
1363
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001364 if (skb == tcp_highest_sack(sk))
1365 tcp_advance_highest_sack(sk, skb);
1366
Martin KaFai Laucfea5a62016-04-19 22:39:29 -07001367 tcp_skb_collapse_tstamp(prev, skb);
Eric Dumazet9a568de2017-05-16 14:00:14 -07001368 if (unlikely(TCP_SKB_CB(prev)->tx.delivered_mstamp))
1369 TCP_SKB_CB(prev)->tx.delivered_mstamp = 0;
Yuchung Chengb9f64822016-09-19 23:39:14 -04001370
Eric Dumazet75c119a2017-10-05 22:21:27 -07001371 tcp_rtx_queue_unlink_and_free(skb, sk);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001372
Eric Dumazetc10d9312016-04-29 14:16:47 -07001373 NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKMERGED);
Ilpo Järvinen111cc8b2008-11-24 21:27:22 -08001374
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001375 return true;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001376}
1377
1378/* I wish gso_size would have a bit more sane initialization than
1379 * something-or-zero which complicates things
1380 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001381static int tcp_skb_seglen(const struct sk_buff *skb)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001382{
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001383 return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001384}
1385
1386/* Shifting pages past head area doesn't work */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001387static int skb_can_shift(const struct sk_buff *skb)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001388{
1389 return !skb_headlen(skb) && skb_is_nonlinear(skb);
1390}
1391
1392/* Try collapsing SACK blocks spanning across multiple skbs to a single
1393 * skb.
1394 */
1395static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001396 struct tcp_sacktag_state *state,
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001397 u32 start_seq, u32 end_seq,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001398 bool dup_sack)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001399{
1400 struct tcp_sock *tp = tcp_sk(sk);
1401 struct sk_buff *prev;
1402 int mss;
1403 int pcount = 0;
1404 int len;
1405 int in_sack;
1406
1407 if (!sk_can_gso(sk))
1408 goto fallback;
1409
1410 /* Normally R but no L won't result in plain S */
1411 if (!dup_sack &&
Ilpo Järvinen9969ca52008-12-05 22:41:06 -08001412 (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001413 goto fallback;
1414 if (!skb_can_shift(skb))
1415 goto fallback;
1416 /* This frame is about to be dropped (was ACKed). */
1417 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1418 goto fallback;
1419
1420 /* Can only happen with delayed DSACK + discard craziness */
Eric Dumazet75c119a2017-10-05 22:21:27 -07001421 prev = skb_rb_prev(skb);
1422 if (!prev)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001423 goto fallback;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001424
1425 if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED)
1426 goto fallback;
1427
Martin KaFai Laua643b5d2016-04-25 14:44:49 -07001428 if (!tcp_skb_can_collapse_to(prev))
1429 goto fallback;
1430
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001431 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1432 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1433
1434 if (in_sack) {
1435 len = skb->len;
1436 pcount = tcp_skb_pcount(skb);
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001437 mss = tcp_skb_seglen(skb);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001438
1439 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1440 * drop this restriction as unnecessary
1441 */
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001442 if (mss != tcp_skb_seglen(prev))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001443 goto fallback;
1444 } else {
1445 if (!after(TCP_SKB_CB(skb)->end_seq, start_seq))
1446 goto noop;
1447 /* CHECKME: This is non-MSS split case only?, this will
1448 * cause skipped skbs due to advancing loop btw, original
1449 * has that feature too
1450 */
1451 if (tcp_skb_pcount(skb) <= 1)
1452 goto noop;
1453
1454 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1455 if (!in_sack) {
1456 /* TODO: head merge to next could be attempted here
1457 * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)),
1458 * though it might not be worth of the additional hassle
1459 *
1460 * ...we can probably just fallback to what was done
1461 * previously. We could try merging non-SACKed ones
1462 * as well but it probably isn't going to buy off
1463 * because later SACKs might again split them, and
1464 * it would make skb timestamp tracking considerably
1465 * harder problem.
1466 */
1467 goto fallback;
1468 }
1469
1470 len = end_seq - TCP_SKB_CB(skb)->seq;
1471 BUG_ON(len < 0);
1472 BUG_ON(len > skb->len);
1473
1474 /* MSS boundaries should be honoured or else pcount will
1475 * severely break even though it makes things bit trickier.
1476 * Optimize common case to avoid most of the divides
1477 */
1478 mss = tcp_skb_mss(skb);
1479
1480 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1481 * drop this restriction as unnecessary
1482 */
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001483 if (mss != tcp_skb_seglen(prev))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001484 goto fallback;
1485
1486 if (len == mss) {
1487 pcount = 1;
1488 } else if (len < mss) {
1489 goto noop;
1490 } else {
1491 pcount = len / mss;
1492 len = pcount * mss;
1493 }
1494 }
1495
Neal Cardwell4648dc92012-03-05 19:35:04 +00001496 /* tcp_sacktag_one() won't SACK-tag ranges below snd_una */
1497 if (!after(TCP_SKB_CB(skb)->seq + len, tp->snd_una))
1498 goto fallback;
1499
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001500 if (!skb_shift(prev, skb, len))
1501 goto fallback;
Eric Dumazetf3319812017-10-05 22:21:26 -07001502 if (!tcp_shifted_skb(sk, prev, skb, state, pcount, len, mss, dup_sack))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001503 goto out;
1504
1505 /* Hole filled allows collapsing with the next as well, this is very
1506 * useful when hole on every nth skb pattern happens
1507 */
Eric Dumazet75c119a2017-10-05 22:21:27 -07001508 skb = skb_rb_next(prev);
1509 if (!skb)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001510 goto out;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001511
Ilpo Järvinenf0bc52f2008-12-05 22:40:47 -08001512 if (!skb_can_shift(skb) ||
Ilpo Järvinenf0bc52f2008-12-05 22:40:47 -08001513 ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) ||
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001514 (mss != tcp_skb_seglen(skb)))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001515 goto out;
1516
1517 len = skb->len;
1518 if (skb_shift(prev, skb, len)) {
1519 pcount += tcp_skb_pcount(skb);
Eric Dumazetf3319812017-10-05 22:21:26 -07001520 tcp_shifted_skb(sk, prev, skb, state, tcp_skb_pcount(skb),
1521 len, mss, 0);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001522 }
1523
1524out:
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001525 state->fack_count += pcount;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001526 return prev;
1527
1528noop:
1529 return skb;
1530
1531fallback:
Eric Dumazetc10d9312016-04-29 14:16:47 -07001532 NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001533 return NULL;
1534}
1535
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001536static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
1537 struct tcp_sack_block *next_dup,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001538 struct tcp_sacktag_state *state,
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001539 u32 start_seq, u32 end_seq,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001540 bool dup_sack_in)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001541{
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001542 struct tcp_sock *tp = tcp_sk(sk);
1543 struct sk_buff *tmp;
1544
Eric Dumazet75c119a2017-10-05 22:21:27 -07001545 skb_rbtree_walk_from(skb) {
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001546 int in_sack = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001547 bool dup_sack = dup_sack_in;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001548
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001549 /* queue is in-order => we can short-circuit the walk early */
1550 if (!before(TCP_SKB_CB(skb)->seq, end_seq))
1551 break;
1552
Ian Morris00db4122015-04-03 09:17:27 +01001553 if (next_dup &&
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001554 before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
1555 in_sack = tcp_match_skb_to_sack(sk, skb,
1556 next_dup->start_seq,
1557 next_dup->end_seq);
1558 if (in_sack > 0)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001559 dup_sack = true;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001560 }
1561
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001562 /* skb reference here is a bit tricky to get right, since
1563 * shifting can eat and free both this skb and the next,
1564 * so not even _safe variant of the loop is enough.
1565 */
1566 if (in_sack <= 0) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001567 tmp = tcp_shift_skb_data(sk, skb, state,
1568 start_seq, end_seq, dup_sack);
Ian Morris00db4122015-04-03 09:17:27 +01001569 if (tmp) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001570 if (tmp != skb) {
1571 skb = tmp;
1572 continue;
1573 }
1574
1575 in_sack = 0;
1576 } else {
1577 in_sack = tcp_match_skb_to_sack(sk, skb,
1578 start_seq,
1579 end_seq);
1580 }
1581 }
1582
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001583 if (unlikely(in_sack < 0))
1584 break;
1585
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001586 if (in_sack) {
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001587 TCP_SKB_CB(skb)->sacked =
1588 tcp_sacktag_one(sk,
1589 state,
1590 TCP_SKB_CB(skb)->sacked,
1591 TCP_SKB_CB(skb)->seq,
1592 TCP_SKB_CB(skb)->end_seq,
1593 dup_sack,
Yuchung Cheng59c9af42013-07-22 16:20:47 -07001594 tcp_skb_pcount(skb),
Eric Dumazet9a568de2017-05-16 14:00:14 -07001595 skb->skb_mstamp);
Yuchung Chengb9f64822016-09-19 23:39:14 -04001596 tcp_rate_skb_delivered(sk, skb, state->rate);
Eric Dumazete2080072017-10-04 12:59:58 -07001597 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
1598 list_del_init(&skb->tcp_tsorted_anchor);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001599
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001600 if (!before(TCP_SKB_CB(skb)->seq,
1601 tcp_highest_sack_seq(tp)))
1602 tcp_advance_highest_sack(sk, skb);
1603 }
1604
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001605 state->fack_count += tcp_skb_pcount(skb);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001606 }
1607 return skb;
1608}
1609
Eric Dumazet75c119a2017-10-05 22:21:27 -07001610static struct sk_buff *tcp_sacktag_bsearch(struct sock *sk,
1611 struct tcp_sacktag_state *state,
1612 u32 seq)
1613{
1614 struct rb_node *parent, **p = &sk->tcp_rtx_queue.rb_node;
1615 struct sk_buff *skb;
1616 int unack_bytes;
1617
1618 while (*p) {
1619 parent = *p;
1620 skb = rb_to_skb(parent);
1621 if (before(seq, TCP_SKB_CB(skb)->seq)) {
1622 p = &parent->rb_left;
1623 continue;
1624 }
1625 if (!before(seq, TCP_SKB_CB(skb)->end_seq)) {
1626 p = &parent->rb_right;
1627 continue;
1628 }
1629
1630 state->fack_count = 0;
1631 unack_bytes = TCP_SKB_CB(skb)->seq - tcp_sk(sk)->snd_una;
1632 if (state->mss_now && unack_bytes > 0)
1633 state->fack_count = unack_bytes / state->mss_now;
1634
1635 return skb;
1636 }
1637 return NULL;
1638}
1639
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001640static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001641 struct tcp_sacktag_state *state,
1642 u32 skip_to_seq)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001643{
Eric Dumazet75c119a2017-10-05 22:21:27 -07001644 if (skb && after(TCP_SKB_CB(skb)->seq, skip_to_seq))
1645 return skb;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001646
Eric Dumazet75c119a2017-10-05 22:21:27 -07001647 return tcp_sacktag_bsearch(sk, state, skip_to_seq);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001648}
1649
1650static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
1651 struct sock *sk,
1652 struct tcp_sack_block *next_dup,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001653 struct tcp_sacktag_state *state,
1654 u32 skip_to_seq)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001655{
Ian Morris51456b22015-04-03 09:17:26 +01001656 if (!next_dup)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001657 return skb;
1658
1659 if (before(next_dup->start_seq, skip_to_seq)) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001660 skb = tcp_sacktag_skip(skb, sk, state, next_dup->start_seq);
1661 skb = tcp_sacktag_walk(skb, sk, NULL, state,
1662 next_dup->start_seq, next_dup->end_seq,
1663 1);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001664 }
1665
1666 return skb;
1667}
1668
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001669static int tcp_sack_cache_ok(const struct tcp_sock *tp, const struct tcp_sack_block *cache)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001670{
1671 return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
1672}
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674static int
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001675tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001676 u32 prior_snd_una, struct tcp_sacktag_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001679 const unsigned char *ptr = (skb_transport_header(ack_skb) +
1680 TCP_SKB_CB(ack_skb)->sacked);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001681 struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2);
Adam Langley4389dde2008-07-19 00:07:02 -07001682 struct tcp_sack_block sp[TCP_NUM_SACKS];
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001683 struct tcp_sack_block *cache;
1684 struct sk_buff *skb;
Adam Langley4389dde2008-07-19 00:07:02 -07001685 int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001686 int used_sacks;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001687 bool found_dup_sack = false;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001688 int i, j;
Baruch Evenfda03fb2007-02-04 23:35:57 -08001689 int first_sack_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001691 state->flag = 0;
1692 state->reord = tp->packets_out;
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001693
Ilpo Järvinend738cd82007-03-24 21:03:23 -07001694 if (!tp->sacked_out) {
Ilpo Järvinende83c052007-10-07 23:37:55 -07001695 if (WARN_ON(tp->fackets_out))
1696 tp->fackets_out = 0;
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001697 tcp_highest_sack_reset(sk);
Ilpo Järvinend738cd82007-03-24 21:03:23 -07001698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07001700 found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire,
David S. Millerd06e0212007-06-18 22:43:06 -07001701 num_sacks, prior_snd_una);
Yuchung Chengb9f64822016-09-19 23:39:14 -04001702 if (found_dup_sack) {
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001703 state->flag |= FLAG_DSACKING_ACK;
Yuchung Chengb9f64822016-09-19 23:39:14 -04001704 tp->delivered++; /* A spurious retransmission is delivered */
1705 }
Baruch Even6f746512007-02-04 23:36:42 -08001706
1707 /* Eliminate too old ACKs, but take into
1708 * account more or less fresh ones, they can
1709 * contain valid SACK info.
1710 */
1711 if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
1712 return 0;
1713
Ilpo Järvinen96a2d412007-11-14 15:47:18 -08001714 if (!tp->packets_out)
1715 goto out;
1716
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001717 used_sacks = 0;
1718 first_sack_index = 0;
1719 for (i = 0; i < num_sacks; i++) {
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001720 bool dup_sack = !i && found_dup_sack;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001721
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001722 sp[used_sacks].start_seq = get_unaligned_be32(&sp_wire[i].start_seq);
1723 sp[used_sacks].end_seq = get_unaligned_be32(&sp_wire[i].end_seq);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001724
1725 if (!tcp_is_sackblock_valid(tp, dup_sack,
1726 sp[used_sacks].start_seq,
1727 sp[used_sacks].end_seq)) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001728 int mib_idx;
1729
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001730 if (dup_sack) {
1731 if (!tp->undo_marker)
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001732 mib_idx = LINUX_MIB_TCPDSACKIGNOREDNOUNDO;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001733 else
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001734 mib_idx = LINUX_MIB_TCPDSACKIGNOREDOLD;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001735 } else {
1736 /* Don't count olds caused by ACK reordering */
1737 if ((TCP_SKB_CB(ack_skb)->ack_seq != tp->snd_una) &&
1738 !after(sp[used_sacks].end_seq, tp->snd_una))
1739 continue;
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001740 mib_idx = LINUX_MIB_TCPSACKDISCARD;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001741 }
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001742
Eric Dumazetc10d9312016-04-29 14:16:47 -07001743 NET_INC_STATS(sock_net(sk), mib_idx);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001744 if (i == 0)
1745 first_sack_index = -1;
1746 continue;
1747 }
1748
1749 /* Ignore very old stuff early */
1750 if (!after(sp[used_sacks].end_seq, prior_snd_una))
1751 continue;
1752
1753 used_sacks++;
1754 }
1755
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001756 /* order SACK blocks to allow in order walk of the retrans queue */
1757 for (i = used_sacks - 1; i > 0; i--) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001758 for (j = 0; j < i; j++) {
1759 if (after(sp[j].start_seq, sp[j + 1].start_seq)) {
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07001760 swap(sp[j], sp[j + 1]);
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08001761
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001762 /* Track where the first SACK block goes to */
1763 if (j == first_sack_index)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001764 first_sack_index = j + 1;
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08001765 }
1766 }
1767 }
1768
Eric Dumazet75c119a2017-10-05 22:21:27 -07001769 state->mss_now = tcp_current_mss(sk);
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001770 state->fack_count = 0;
Eric Dumazet75c119a2017-10-05 22:21:27 -07001771 skb = NULL;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001772 i = 0;
1773
1774 if (!tp->sacked_out) {
1775 /* It's already past, so skip checking against it */
1776 cache = tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
1777 } else {
1778 cache = tp->recv_sack_cache;
1779 /* Skip empty blocks in at head of the cache */
1780 while (tcp_sack_cache_ok(tp, cache) && !cache->start_seq &&
1781 !cache->end_seq)
1782 cache++;
Baruch Evenfda03fb2007-02-04 23:35:57 -08001783 }
1784
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001785 while (i < used_sacks) {
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001786 u32 start_seq = sp[i].start_seq;
1787 u32 end_seq = sp[i].end_seq;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001788 bool dup_sack = (found_dup_sack && (i == first_sack_index));
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001789 struct tcp_sack_block *next_dup = NULL;
Ilpo Järvinene56d6cd2007-11-01 00:09:37 -07001790
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001791 if (found_dup_sack && ((i + 1) == first_sack_index))
1792 next_dup = &sp[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001794 /* Skip too early cached blocks */
1795 while (tcp_sack_cache_ok(tp, cache) &&
1796 !before(start_seq, cache->end_seq))
1797 cache++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001799 /* Can skip some work by looking recv_sack_cache? */
1800 if (tcp_sack_cache_ok(tp, cache) && !dup_sack &&
1801 after(end_seq, cache->start_seq)) {
David S. Millerfe067e82007-03-07 12:12:44 -08001802
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001803 /* Head todo? */
1804 if (before(start_seq, cache->start_seq)) {
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001805 skb = tcp_sacktag_skip(skb, sk, state,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001806 start_seq);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001807 skb = tcp_sacktag_walk(skb, sk, next_dup,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001808 state,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001809 start_seq,
1810 cache->start_seq,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001811 dup_sack);
Baruch Evenfda03fb2007-02-04 23:35:57 -08001812 }
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08001813
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001814 /* Rest of the block already fully processed? */
Ilpo Järvinen20de20b2007-11-16 16:17:05 -08001815 if (!after(end_seq, cache->end_seq))
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001816 goto advance_sp;
Ilpo Järvinen20de20b2007-11-16 16:17:05 -08001817
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001818 skb = tcp_maybe_skipping_dsack(skb, sk, next_dup,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001819 state,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001820 cache->end_seq);
Ilpo Järvinene56d6cd2007-11-01 00:09:37 -07001821
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001822 /* ...tail remains todo... */
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001823 if (tcp_highest_sack_seq(tp) == cache->end_seq) {
Ilpo Järvinen20de20b2007-11-16 16:17:05 -08001824 /* ...but better entrypoint exists! */
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001825 skb = tcp_highest_sack(sk);
Ian Morris51456b22015-04-03 09:17:26 +01001826 if (!skb)
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001827 break;
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001828 state->fack_count = tp->fackets_out;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001829 cache++;
1830 goto walk;
1831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001833 skb = tcp_sacktag_skip(skb, sk, state, cache->end_seq);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001834 /* Check overlap against next cached too (past this one already) */
1835 cache++;
1836 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
Ilpo Järvinenfbd52eb2007-11-10 21:24:19 -08001838
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001839 if (!before(start_seq, tcp_highest_sack_seq(tp))) {
1840 skb = tcp_highest_sack(sk);
Ian Morris51456b22015-04-03 09:17:26 +01001841 if (!skb)
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001842 break;
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001843 state->fack_count = tp->fackets_out;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001844 }
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001845 skb = tcp_sacktag_skip(skb, sk, state, start_seq);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001846
1847walk:
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001848 skb = tcp_sacktag_walk(skb, sk, next_dup, state,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001849 start_seq, end_seq, dup_sack);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001850
1851advance_sp:
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001852 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
1854
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001855 /* Clear the head of the cache sack blocks so we can skip it next time */
1856 for (i = 0; i < ARRAY_SIZE(tp->recv_sack_cache) - used_sacks; i++) {
1857 tp->recv_sack_cache[i].start_seq = 0;
1858 tp->recv_sack_cache[i].end_seq = 0;
1859 }
1860 for (j = 0; j < used_sacks; j++)
1861 tp->recv_sack_cache[i++] = sp[j];
1862
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001863 if ((state->reord < tp->fackets_out) &&
Yuchung Cheng9b441902013-03-20 13:32:58 +00001864 ((inet_csk(sk)->icsk_ca_state != TCP_CA_Loss) || tp->undo_marker))
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001865 tcp_update_reordering(sk, tp->fackets_out - state->reord, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Yuchung Cheng9dac8832015-04-29 11:28:30 -07001867 tcp_verify_left_out(tp);
Ilpo Järvinen96a2d412007-11-14 15:47:18 -08001868out:
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870#if FASTRETRANS_DEBUG > 0
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001871 WARN_ON((int)tp->sacked_out < 0);
1872 WARN_ON((int)tp->lost_out < 0);
1873 WARN_ON((int)tp->retrans_out < 0);
1874 WARN_ON((int)tcp_packets_in_flight(tp) < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875#endif
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001876 return state->flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
Ilpo Järvinen882beba2008-04-07 22:33:07 -07001879/* Limits sacked_out so that sum with lost_out isn't ever larger than
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001880 * packets_out. Returns false if sacked_out adjustement wasn't necessary.
Ilpo Järvinen30935cf2007-02-21 23:01:36 -08001881 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001882static bool tcp_limit_reno_sacked(struct tcp_sock *tp)
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001883{
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001884 u32 holes;
1885
1886 holes = max(tp->lost_out, 1U);
1887 holes = min(holes, tp->packets_out);
1888
1889 if ((tp->sacked_out + holes) > tp->packets_out) {
1890 tp->sacked_out = tp->packets_out - holes;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001891 return true;
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001892 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001893 return false;
Ilpo Järvinen882beba2008-04-07 22:33:07 -07001894}
1895
1896/* If we receive more dupacks than we expected counting segments
1897 * in assumption of absent reordering, interpret this as reordering.
1898 * The only another reason could be bug in receiver TCP.
1899 */
1900static void tcp_check_reno_reordering(struct sock *sk, const int addend)
1901{
1902 struct tcp_sock *tp = tcp_sk(sk);
1903 if (tcp_limit_reno_sacked(tp))
1904 tcp_update_reordering(sk, tp->packets_out + addend, 0);
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001905}
1906
1907/* Emulate SACKs for SACKless connection: account for a new dupack. */
1908
1909static void tcp_add_reno_sack(struct sock *sk)
1910{
1911 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Chengddf1af62016-02-02 10:33:06 -08001912 u32 prior_sacked = tp->sacked_out;
1913
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001914 tp->sacked_out++;
1915 tcp_check_reno_reordering(sk, 0);
Yuchung Chengddf1af62016-02-02 10:33:06 -08001916 if (tp->sacked_out > prior_sacked)
1917 tp->delivered++; /* Some out-of-order packet is delivered */
Ilpo Järvinen005903b2007-08-09 14:44:16 +03001918 tcp_verify_left_out(tp);
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001919}
1920
1921/* Account for ACK, ACKing some data in Reno Recovery phase. */
1922
1923static void tcp_remove_reno_sacks(struct sock *sk, int acked)
1924{
1925 struct tcp_sock *tp = tcp_sk(sk);
1926
1927 if (acked > 0) {
1928 /* One ACK acked hole. The rest eat duplicate ACKs. */
Yuchung Chengddf1af62016-02-02 10:33:06 -08001929 tp->delivered += max_t(int, acked - tp->sacked_out, 1);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001930 if (acked - 1 >= tp->sacked_out)
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001931 tp->sacked_out = 0;
1932 else
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001933 tp->sacked_out -= acked - 1;
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001934 }
1935 tcp_check_reno_reordering(sk, acked);
Ilpo Järvinen005903b2007-08-09 14:44:16 +03001936 tcp_verify_left_out(tp);
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001937}
1938
1939static inline void tcp_reset_reno_sack(struct tcp_sock *tp)
1940{
1941 tp->sacked_out = 0;
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001942}
1943
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001944void tcp_clear_retrans(struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 tp->retrans_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 tp->lost_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 tp->undo_marker = 0;
Yuchung Cheng6e08d5e2014-07-02 12:07:16 -07001949 tp->undo_retrans = -1;
Ilpo Järvinen4cd82992007-10-11 17:34:57 -07001950 tp->fackets_out = 0;
1951 tp->sacked_out = 0;
1952}
1953
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001954static inline void tcp_init_undo(struct tcp_sock *tp)
1955{
1956 tp->undo_marker = tp->snd_una;
1957 /* Retransmission still in flight may cause DSACKs later. */
1958 tp->undo_retrans = tp->retrans_out ? : -1;
1959}
1960
Neal Cardwell5ae344c2014-08-04 19:12:29 -04001961/* Enter Loss state. If we detect SACK reneging, forget all SACK information
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 * and reset tags completely, otherwise preserve SACKs. If receiver
1963 * dropped its ofo queue, we will know this due to reneging detection.
1964 */
Neal Cardwell5ae344c2014-08-04 19:12:29 -04001965void tcp_enter_loss(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001967 const struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisov1043e252016-02-03 09:46:52 +02001969 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 struct sk_buff *skb;
Yuchung Chengcc663f42017-04-07 11:42:05 -07001971 bool new_recovery = icsk->icsk_ca_state < TCP_CA_Recovery;
Neal Cardwell5ae344c2014-08-04 19:12:29 -04001972 bool is_reneg; /* is receiver reneging on SACKs? */
Neal Cardwell0682e692016-09-19 23:39:13 -04001973 bool mark_lost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 /* Reduce ssthresh if it has not yet been made inside this window. */
Yuchung Chenge33099f2013-03-20 13:33:00 +00001976 if (icsk->icsk_ca_state <= TCP_CA_Disorder ||
1977 !after(tp->high_seq, tp->snd_una) ||
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001978 (icsk->icsk_ca_state == TCP_CA_Loss && !icsk->icsk_retransmits)) {
1979 tp->prior_ssthresh = tcp_current_ssthresh(sk);
Yuchung Cheng4faf7832017-08-03 20:38:51 -07001980 tp->prior_cwnd = tp->snd_cwnd;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001981 tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
1982 tcp_ca_event(sk, CA_EVENT_LOSS);
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001983 tcp_init_undo(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
1985 tp->snd_cwnd = 1;
1986 tp->snd_cwnd_cnt = 0;
Eric Dumazetc2203cf2017-05-16 14:00:04 -07001987 tp->snd_cwnd_stamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001989 tp->retrans_out = 0;
1990 tp->lost_out = 0;
Ilpo Järvinen4cd82992007-10-11 17:34:57 -07001991
1992 if (tcp_is_reno(tp))
1993 tcp_reset_reno_sack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Eric Dumazet75c119a2017-10-05 22:21:27 -07001995 skb = tcp_rtx_queue_head(sk);
Neal Cardwell5ae344c2014-08-04 19:12:29 -04001996 is_reneg = skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED);
1997 if (is_reneg) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07001998 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSACKRENEGING);
Ilpo Järvinen4cd82992007-10-11 17:34:57 -07001999 tp->sacked_out = 0;
2000 tp->fackets_out = 0;
Ilpo Järvinenb7689202007-09-20 11:37:19 -07002001 }
Ilpo Järvinen64edc272008-09-20 21:18:32 -07002002 tcp_clear_all_retrans_hints(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Eric Dumazet75c119a2017-10-05 22:21:27 -07002004 skb_rbtree_walk_from(skb) {
Neal Cardwell0682e692016-09-19 23:39:13 -04002005 mark_lost = (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) ||
2006 is_reneg);
2007 if (mark_lost)
2008 tcp_sum_lost(tp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 TCP_SKB_CB(skb)->sacked &= (~TCPCB_TAGBITS)|TCPCB_SACKED_ACKED;
Neal Cardwell0682e692016-09-19 23:39:13 -04002010 if (mark_lost) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_ACKED;
2012 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
2013 tp->lost_out += tcp_skb_pcount(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015 }
Ilpo Järvinen005903b2007-08-09 14:44:16 +03002016 tcp_verify_left_out(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Yuchung Cheng74c181d2013-08-12 16:41:25 -07002018 /* Timeout in disordered state after receiving substantial DUPACKs
2019 * suggests that the degree of reordering is over-estimated.
2020 */
2021 if (icsk->icsk_ca_state <= TCP_CA_Disorder &&
Nikolay Borisov1043e252016-02-03 09:46:52 +02002022 tp->sacked_out >= net->ipv4.sysctl_tcp_reordering)
Yuchung Cheng74c181d2013-08-12 16:41:25 -07002023 tp->reordering = min_t(unsigned int, tp->reordering,
Nikolay Borisov1043e252016-02-03 09:46:52 +02002024 net->ipv4.sysctl_tcp_reordering);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002025 tcp_set_ca_state(sk, TCP_CA_Loss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 tp->high_seq = tp->snd_nxt;
Florian Westphal735d3832014-09-29 13:08:30 +02002027 tcp_ecn_queue_cwr(tp);
Yuchung Chenge33099f2013-03-20 13:33:00 +00002028
Yuchung Chengcc663f42017-04-07 11:42:05 -07002029 /* F-RTO RFC5682 sec 3.1 step 1: retransmit SND.UNA if no previous
2030 * loss recovery is underway except recurring timeout(s) on
2031 * the same SND.UNA (sec 3.2). Disable F-RTO on path MTU probing
2032 *
2033 * In theory F-RTO can be used repeatedly during loss recovery.
2034 * In practice this interacts badly with broken middle-boxes that
2035 * falsely raise the receive window, which results in repeated
2036 * timeouts and stop-and-go behavior.
Yuchung Chenge33099f2013-03-20 13:33:00 +00002037 */
Yuchung Chengcc663f42017-04-07 11:42:05 -07002038 tp->frto = sysctl_tcp_frto &&
2039 (new_recovery || icsk->icsk_retransmits) &&
2040 !inet_csk(sk)->icsk_mtup.probe_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002043/* If ACK arrived pointing to a remembered SACK, it means that our
2044 * remembered SACKs do not reflect real state of receiver i.e.
2045 * receiver _host_ is heavily congested (or buggy).
2046 *
Neal Cardwell5ae344c2014-08-04 19:12:29 -04002047 * To avoid big spurious retransmission bursts due to transient SACK
2048 * scoreboard oddities that look like reneging, we give the receiver a
2049 * little time (max(RTT/2, 10ms)) to send us some more ACKs that will
2050 * restore sanity to the SACK scoreboard. If the apparent reneging
2051 * persists until this RTO then we'll clear the SACK scoreboard.
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002052 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002053static bool tcp_check_sack_reneging(struct sock *sk, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002055 if (flag & FLAG_SACK_RENEGING) {
Neal Cardwell5ae344c2014-08-04 19:12:29 -04002056 struct tcp_sock *tp = tcp_sk(sk);
2057 unsigned long delay = max(usecs_to_jiffies(tp->srtt_us >> 4),
2058 msecs_to_jiffies(10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002060 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
Neal Cardwell5ae344c2014-08-04 19:12:29 -04002061 delay, TCP_RTO_MAX);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002062 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002064 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002067static inline int tcp_fackets_out(const struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002069 return tcp_is_reno(tp) ? tp->sacked_out + 1 : tp->fackets_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002072/* Heurestics to calculate number of duplicate ACKs. There's no dupACKs
2073 * counter when SACK is enabled (without SACK, sacked_out is used for
2074 * that purpose).
2075 *
2076 * Instead, with FACK TCP uses fackets_out that includes both SACKed
2077 * segments up to the highest received SACK block so far and holes in
2078 * between them.
2079 *
2080 * With reordering, holes may still be in flight, so RFC3517 recovery
2081 * uses pure sacked_out (total number of SACKed segments) even though
2082 * it violates the RFC that uses duplicate ACKs, often these are equal
2083 * but when e.g. out-of-window ACKs or packet duplication occurs,
2084 * they differ. Since neither occurs due to loss, TCP should really
2085 * ignore them.
2086 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002087static inline int tcp_dupack_heuristics(const struct tcp_sock *tp)
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002088{
2089 return tcp_is_fack(tp) ? tp->fackets_out : tp->sacked_out + 1;
2090}
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092/* Linux NewReno/SACK/FACK/ECN state machine.
2093 * --------------------------------------
2094 *
2095 * "Open" Normal state, no dubious events, fast path.
2096 * "Disorder" In all the respects it is "Open",
2097 * but requires a bit more attention. It is entered when
2098 * we see some SACKs or dupacks. It is split of "Open"
2099 * mainly to move some processing from fast path to slow one.
2100 * "CWR" CWND was reduced due to some Congestion Notification event.
2101 * It can be ECN, ICMP source quench, local device congestion.
2102 * "Recovery" CWND was reduced, we are fast-retransmitting.
2103 * "Loss" CWND was reduced due to RTO timeout or SACK reneging.
2104 *
2105 * tcp_fastretrans_alert() is entered:
2106 * - each incoming ACK, if state is not "Open"
2107 * - when arrived ACK is unusual, namely:
2108 * * SACK
2109 * * Duplicate ACK.
2110 * * ECN ECE.
2111 *
2112 * Counting packets in flight is pretty simple.
2113 *
2114 * in_flight = packets_out - left_out + retrans_out
2115 *
2116 * packets_out is SND.NXT-SND.UNA counted in packets.
2117 *
2118 * retrans_out is number of retransmitted segments.
2119 *
2120 * left_out is number of segments left network, but not ACKed yet.
2121 *
2122 * left_out = sacked_out + lost_out
2123 *
2124 * sacked_out: Packets, which arrived to receiver out of order
2125 * and hence not ACKed. With SACKs this number is simply
2126 * amount of SACKed data. Even without SACKs
2127 * it is easy to give pretty reliable estimate of this number,
2128 * counting duplicate ACKs.
2129 *
2130 * lost_out: Packets lost by network. TCP has no explicit
2131 * "loss notification" feedback from network (for now).
2132 * It means that this number can be only _guessed_.
2133 * Actually, it is the heuristics to predict lossage that
2134 * distinguishes different algorithms.
2135 *
2136 * F.e. after RTO, when all the queue is considered as lost,
2137 * lost_out = packets_out and in_flight = retrans_out.
2138 *
Yuchung Chenga0370b32017-01-12 22:11:36 -08002139 * Essentially, we have now a few algorithms detecting
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 * lost packets.
2141 *
Yuchung Chenga0370b32017-01-12 22:11:36 -08002142 * If the receiver supports SACK:
2143 *
2144 * RFC6675/3517: It is the conventional algorithm. A packet is
2145 * considered lost if the number of higher sequence packets
2146 * SACKed is greater than or equal the DUPACK thoreshold
2147 * (reordering). This is implemented in tcp_mark_head_lost and
2148 * tcp_update_scoreboard.
2149 *
2150 * RACK (draft-ietf-tcpm-rack-01): it is a newer algorithm
2151 * (2017-) that checks timing instead of counting DUPACKs.
2152 * Essentially a packet is considered lost if it's not S/ACKed
2153 * after RTT + reordering_window, where both metrics are
2154 * dynamically measured and adjusted. This is implemented in
2155 * tcp_rack_mark_lost.
2156 *
Yuchung Cheng94bdc972017-01-12 22:11:42 -08002157 * FACK (Disabled by default. Subsumbed by RACK):
2158 * It is the simplest heuristics. As soon as we decided
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 * that something is lost, we decide that _all_ not SACKed
2160 * packets until the most forward SACK are lost. I.e.
2161 * lost_out = fackets_out - sacked_out and left_out = fackets_out.
2162 * It is absolutely correct estimate, if network does not reorder
2163 * packets. And it loses any connection to reality when reordering
2164 * takes place. We use FACK by default until reordering
2165 * is suspected on the path to this destination.
2166 *
Yuchung Chenga0370b32017-01-12 22:11:36 -08002167 * If the receiver does not support SACK:
2168 *
2169 * NewReno (RFC6582): in Recovery we assume that one segment
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 * is lost (classic Reno). While we are in Recovery and
2171 * a partial ACK arrives, we assume that one more packet
2172 * is lost (NewReno). This heuristics are the same in NewReno
2173 * and SACK.
2174 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 * Really tricky (and requiring careful tuning) part of algorithm
2176 * is hidden in functions tcp_time_to_recover() and tcp_xmit_retransmit_queue().
2177 * The first determines the moment _when_ we should reduce CWND and,
2178 * hence, slow down forward transmission. In fact, it determines the moment
2179 * when we decide that hole is caused by loss, rather than by a reorder.
2180 *
2181 * tcp_xmit_retransmit_queue() decides, _what_ we should retransmit to fill
2182 * holes, caused by lost packets.
2183 *
2184 * And the most logically complicated part of algorithm is undo
2185 * heuristics. We detect false retransmits due to both too early
2186 * fast retransmit (reordering) and underestimated RTO, analyzing
2187 * timestamps and D-SACKs. When we detect that some segments were
2188 * retransmitted by mistake and CWND reduction was wrong, we undo
2189 * window reduction and abort recovery phase. This logic is hidden
2190 * inside several functions named tcp_try_undo_<something>.
2191 */
2192
2193/* This function decides, when we should leave Disordered state
2194 * and enter Recovery phase, reducing congestion window.
2195 *
2196 * Main question: may we further continue forward transmission
2197 * with the same cwnd?
2198 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002199static bool tcp_time_to_recover(struct sock *sk, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002201 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /* Trick#1: The loss is proven. */
2204 if (tp->lost_out)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002205 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 /* Not-A-Trick#2 : Classic rule... */
Andreas Petlundea84e552009-10-27 03:27:21 +00002208 if (tcp_dupack_heuristics(tp) > tp->reordering)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002209 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002211 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
Yuchung Cheng974c1232012-01-19 14:42:21 +00002214/* Detect loss in event "A" above by marking head of queue up as lost.
2215 * For FACK or non-SACK(Reno) senders, the first "packets" number of segments
2216 * are considered lost. For RFC3517 SACK, a segment is considered lost if it
2217 * has at least tp->reordering SACKed seqments above it; "packets" refers to
2218 * the maximum SACKed segments to pass before reaching this limit.
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002219 */
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002220static void tcp_mark_head_lost(struct sock *sk, int packets, int mark_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002222 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 struct sk_buff *skb;
Neal Cardwelld88270e2016-01-25 14:01:53 -08002224 int cnt, oldcnt, lost;
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002225 unsigned int mss;
Yuchung Cheng974c1232012-01-19 14:42:21 +00002226 /* Use SACK to deduce losses of new sequences sent during recovery */
2227 const u32 loss_high = tcp_is_sack(tp) ? tp->snd_nxt : tp->high_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002229 WARN_ON(packets > tp->packets_out);
Eric Dumazet5e76ee42017-10-05 22:21:24 -07002230 skb = tp->lost_skb_hint;
2231 if (skb) {
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002232 /* Head already handled? */
Eric Dumazet5e76ee42017-10-05 22:21:24 -07002233 if (mark_head && after(TCP_SKB_CB(skb)->seq, tp->snd_una))
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002234 return;
Eric Dumazet5e76ee42017-10-05 22:21:24 -07002235 cnt = tp->lost_cnt_hint;
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002236 } else {
Eric Dumazet75c119a2017-10-05 22:21:27 -07002237 skb = tcp_rtx_queue_head(sk);
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002238 cnt = 0;
2239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Eric Dumazet75c119a2017-10-05 22:21:27 -07002241 skb_rbtree_walk_from(skb) {
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002242 /* TODO: do this better */
2243 /* this is not the most efficient way to do this... */
2244 tp->lost_skb_hint = skb;
2245 tp->lost_cnt_hint = cnt;
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002246
Yuchung Cheng974c1232012-01-19 14:42:21 +00002247 if (after(TCP_SKB_CB(skb)->end_seq, loss_high))
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002248 break;
2249
2250 oldcnt = cnt;
Ilpo Järvinenad1984e2008-01-30 20:06:02 -08002251 if (tcp_is_fack(tp) || tcp_is_reno(tp) ||
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002252 (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
2253 cnt += tcp_skb_pcount(skb);
2254
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002255 if (cnt > packets) {
Yuchung Chengb3de7552010-09-24 13:22:06 +00002256 if ((tcp_is_sack(tp) && !tcp_is_fack(tp)) ||
Neal Cardwellc0638c22012-03-02 21:36:51 +00002257 (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) ||
Yuchung Chengb3de7552010-09-24 13:22:06 +00002258 (oldcnt >= packets))
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002259 break;
2260
Eric Dumazetf69ad292015-06-11 09:15:18 -07002261 mss = tcp_skb_mss(skb);
Neal Cardwelld88270e2016-01-25 14:01:53 -08002262 /* If needed, chop off the prefix to mark as lost. */
2263 lost = (packets - oldcnt) * mss;
2264 if (lost < skb->len &&
Eric Dumazet75c119a2017-10-05 22:21:27 -07002265 tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb,
2266 lost, mss, GFP_ATOMIC) < 0)
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002267 break;
2268 cnt = packets;
2269 }
2270
Ilpo Järvinen41ea36e2008-09-20 21:19:22 -07002271 tcp_skb_mark_lost(tp, skb);
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002272
2273 if (mark_head)
2274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
Ilpo Järvinen005903b2007-08-09 14:44:16 +03002276 tcp_verify_left_out(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
2279/* Account newly detected lost packet(s) */
2280
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002281static void tcp_update_scoreboard(struct sock *sk, int fast_rexmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002283 struct tcp_sock *tp = tcp_sk(sk);
2284
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002285 if (tcp_is_reno(tp)) {
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002286 tcp_mark_head_lost(sk, 1, 1);
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002287 } else if (tcp_is_fack(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 int lost = tp->fackets_out - tp->reordering;
2289 if (lost <= 0)
2290 lost = 1;
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002291 tcp_mark_head_lost(sk, lost, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 } else {
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002293 int sacked_upto = tp->sacked_out - tp->reordering;
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002294 if (sacked_upto >= 0)
2295 tcp_mark_head_lost(sk, sacked_upto, 0);
2296 else if (fast_rexmit)
2297 tcp_mark_head_lost(sk, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
Yuchung Cheng77c63122015-10-16 21:57:44 -07002301static bool tcp_tsopt_ecr_before(const struct tcp_sock *tp, u32 when)
2302{
2303 return tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
2304 before(tp->rx_opt.rcv_tsecr, when);
2305}
2306
Yuchung Cheng659a8ad2015-10-16 21:57:46 -07002307/* skb is spurious retransmitted if the returned timestamp echo
2308 * reply is prior to the skb transmission time
2309 */
2310static bool tcp_skb_spurious_retrans(const struct tcp_sock *tp,
2311 const struct sk_buff *skb)
2312{
2313 return (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS) &&
2314 tcp_tsopt_ecr_before(tp, tcp_skb_timestamp(skb));
2315}
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317/* Nothing was retransmitted or returned timestamp is less
2318 * than timestamp of the first retransmission.
2319 */
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00002320static inline bool tcp_packet_delayed(const struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
2322 return !tp->retrans_stamp ||
Yuchung Cheng77c63122015-10-16 21:57:44 -07002323 tcp_tsopt_ecr_before(tp, tp->retrans_stamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
2326/* Undo procedures. */
2327
Marcelo Leitner1f37bf82014-11-04 17:15:08 -02002328/* We can clear retrans_stamp when there are no retransmissions in the
2329 * window. It would seem that it is trivially available for us in
2330 * tp->retrans_out, however, that kind of assumptions doesn't consider
2331 * what will happen if errors occur when sending retransmission for the
2332 * second time. ...It could the that such segment has only
2333 * TCPCB_EVER_RETRANS set at the present time. It seems that checking
2334 * the head skb is enough except for some reneging corner cases that
2335 * are not worth the effort.
2336 *
2337 * Main reason for all this complexity is the fact that connection dying
2338 * time now depends on the validity of the retrans_stamp, in particular,
2339 * that successive retransmissions of a segment must not advance
2340 * retrans_stamp under any conditions.
2341 */
2342static bool tcp_any_retrans_done(const struct sock *sk)
2343{
2344 const struct tcp_sock *tp = tcp_sk(sk);
2345 struct sk_buff *skb;
2346
2347 if (tp->retrans_out)
2348 return true;
2349
Eric Dumazet75c119a2017-10-05 22:21:27 -07002350 skb = tcp_rtx_queue_head(sk);
Marcelo Leitner1f37bf82014-11-04 17:15:08 -02002351 if (unlikely(skb && TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS))
2352 return true;
2353
2354 return false;
2355}
2356
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002357static void DBGUNDO(struct sock *sk, const char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
Joe Perches39347882017-09-10 19:02:25 -07002359#if FASTRETRANS_DEBUG > 1
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002360 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 struct inet_sock *inet = inet_sk(sk);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002362
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002363 if (sk->sk_family == AF_INET) {
Joe Perches91df42b2012-05-15 14:11:54 +00002364 pr_debug("Undo %s %pI4/%u c%u l%u ss%u/%u p%u\n",
2365 msg,
2366 &inet->inet_daddr, ntohs(inet->inet_dport),
2367 tp->snd_cwnd, tcp_left_out(tp),
2368 tp->snd_ssthresh, tp->prior_ssthresh,
2369 tp->packets_out);
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002370 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002371#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002372 else if (sk->sk_family == AF_INET6) {
Joe Perches91df42b2012-05-15 14:11:54 +00002373 pr_debug("Undo %s %pI6/%u c%u l%u ss%u/%u p%u\n",
2374 msg,
Eric Dumazet019b1c92016-09-22 17:54:00 -07002375 &sk->sk_v6_daddr, ntohs(inet->inet_dport),
Joe Perches91df42b2012-05-15 14:11:54 +00002376 tp->snd_cwnd, tcp_left_out(tp),
2377 tp->snd_ssthresh, tp->prior_ssthresh,
2378 tp->packets_out);
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002379 }
2380#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381#endif
Joe Perches39347882017-09-10 19:02:25 -07002382}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Yuchung Cheng7026b912013-05-29 14:20:13 +00002384static void tcp_undo_cwnd_reduction(struct sock *sk, bool unmark_loss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002386 struct tcp_sock *tp = tcp_sk(sk);
2387
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002388 if (unmark_loss) {
2389 struct sk_buff *skb;
2390
Eric Dumazet75c119a2017-10-05 22:21:27 -07002391 skb_rbtree_walk(skb, &sk->tcp_rtx_queue) {
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002392 TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST;
2393 }
2394 tp->lost_out = 0;
2395 tcp_clear_all_retrans_hints(tp);
2396 }
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (tp->prior_ssthresh) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002399 const struct inet_connection_sock *icsk = inet_csk(sk);
2400
Florian Westphale9799182016-11-21 14:18:38 +01002401 tp->snd_cwnd = icsk->icsk_ca_ops->undo_cwnd(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Yuchung Cheng7026b912013-05-29 14:20:13 +00002403 if (tp->prior_ssthresh > tp->snd_ssthresh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 tp->snd_ssthresh = tp->prior_ssthresh;
Florian Westphal735d3832014-09-29 13:08:30 +02002405 tcp_ecn_withdraw_cwr(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 }
Eric Dumazetc2203cf2017-05-16 14:00:04 -07002408 tp->snd_cwnd_stamp = tcp_jiffies32;
Yuchung Cheng7026b912013-05-29 14:20:13 +00002409 tp->undo_marker = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00002412static inline bool tcp_may_undo(const struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002414 return tp->undo_marker && (!tp->undo_retrans || tcp_packet_delayed(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
2417/* People celebrate: "We love our President!" */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002418static bool tcp_try_undo_recovery(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002420 struct tcp_sock *tp = tcp_sk(sk);
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (tcp_may_undo(tp)) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07002423 int mib_idx;
2424
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 /* Happy end! We did not retransmit anything
2426 * or our original transmission succeeded.
2427 */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002428 DBGUNDO(sk, inet_csk(sk)->icsk_ca_state == TCP_CA_Loss ? "loss" : "retrans");
Yuchung Cheng7026b912013-05-29 14:20:13 +00002429 tcp_undo_cwnd_reduction(sk, false);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002430 if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss)
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07002431 mib_idx = LINUX_MIB_TCPLOSSUNDO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 else
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07002433 mib_idx = LINUX_MIB_TCPFULLUNDO;
2434
Eric Dumazetc10d9312016-04-29 14:16:47 -07002435 NET_INC_STATS(sock_net(sk), mib_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002437 if (tp->snd_una == tp->high_seq && tcp_is_reno(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 /* Hold old state until something *above* high_seq
2439 * is ACKed. For Reno it is MUST to prevent false
2440 * fast retransmits (RFC2582). SACK TCP is safe. */
Marcelo Leitner1f37bf82014-11-04 17:15:08 -02002441 if (!tcp_any_retrans_done(sk))
2442 tp->retrans_stamp = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002443 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002445 tcp_set_ca_state(sk, TCP_CA_Open);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002446 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
2449/* Try to undo cwnd reduction, because D-SACKs acked all retransmitted data */
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002450static bool tcp_try_undo_dsack(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002452 struct tcp_sock *tp = tcp_sk(sk);
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (tp->undo_marker && !tp->undo_retrans) {
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002455 DBGUNDO(sk, "D-SACK");
Yuchung Cheng7026b912013-05-29 14:20:13 +00002456 tcp_undo_cwnd_reduction(sk, false);
Eric Dumazetc10d9312016-04-29 14:16:47 -07002457 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKUNDO);
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002458 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002460 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
Yuchung Chenge33099f2013-03-20 13:33:00 +00002463/* Undo during loss recovery after partial ACK or using F-RTO. */
2464static bool tcp_try_undo_loss(struct sock *sk, bool frto_undo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002466 struct tcp_sock *tp = tcp_sk(sk);
2467
Yuchung Chenge33099f2013-03-20 13:33:00 +00002468 if (frto_undo || tcp_may_undo(tp)) {
Yuchung Cheng7026b912013-05-29 14:20:13 +00002469 tcp_undo_cwnd_reduction(sk, true);
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002470
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002471 DBGUNDO(sk, "partial loss");
Eric Dumazetc10d9312016-04-29 14:16:47 -07002472 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPLOSSUNDO);
Yuchung Chenge33099f2013-03-20 13:33:00 +00002473 if (frto_undo)
Eric Dumazetc10d9312016-04-29 14:16:47 -07002474 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07002475 LINUX_MIB_TCPSPURIOUSRTOS);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002476 inet_csk(sk)->icsk_retransmits = 0;
Yuchung Chenge33099f2013-03-20 13:33:00 +00002477 if (frto_undo || tcp_is_sack(tp))
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002478 tcp_set_ca_state(sk, TCP_CA_Open);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002479 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002481 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
Yuchung Cheng37598242015-07-01 14:11:15 -07002484/* The cwnd reduction in CWR and Recovery uses the PRR algorithm in RFC 6937.
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002485 * It computes the number of packets to send (sndcnt) based on packets newly
2486 * delivered:
2487 * 1) If the packets in flight is larger than ssthresh, PRR spreads the
2488 * cwnd reductions across a full RTT.
Yuchung Cheng37598242015-07-01 14:11:15 -07002489 * 2) Otherwise PRR uses packet conservation to send as much as delivered.
2490 * But when the retransmits are acked without further losses, PRR
2491 * slow starts cwnd up to ssthresh to speed up the recovery.
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002492 */
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002493static void tcp_init_cwnd_reduction(struct sock *sk)
Yuchung Cheng684bad12012-09-02 17:38:04 +00002494{
2495 struct tcp_sock *tp = tcp_sk(sk);
2496
2497 tp->high_seq = tp->snd_nxt;
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00002498 tp->tlp_high_seq = 0;
Yuchung Cheng684bad12012-09-02 17:38:04 +00002499 tp->snd_cwnd_cnt = 0;
2500 tp->prior_cwnd = tp->snd_cwnd;
2501 tp->prr_delivered = 0;
2502 tp->prr_out = 0;
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002503 tp->snd_ssthresh = inet_csk(sk)->icsk_ca_ops->ssthresh(sk);
Florian Westphal735d3832014-09-29 13:08:30 +02002504 tcp_ecn_queue_cwr(tp);
Yuchung Cheng684bad12012-09-02 17:38:04 +00002505}
2506
Yuchung Cheng57dde7f2017-01-12 22:11:33 -08002507void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked, int flag)
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002508{
2509 struct tcp_sock *tp = tcp_sk(sk);
2510 int sndcnt = 0;
2511 int delta = tp->snd_ssthresh - tcp_packets_in_flight(tp);
2512
Yuchung Cheng8b8a3212016-01-06 12:42:38 -08002513 if (newly_acked_sacked <= 0 || WARN_ON_ONCE(!tp->prior_cwnd))
2514 return;
2515
Yuchung Cheng684bad12012-09-02 17:38:04 +00002516 tp->prr_delivered += newly_acked_sacked;
Yuchung Cheng37598242015-07-01 14:11:15 -07002517 if (delta < 0) {
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002518 u64 dividend = (u64)tp->snd_ssthresh * tp->prr_delivered +
2519 tp->prior_cwnd - 1;
2520 sndcnt = div_u64(dividend, tp->prior_cwnd) - tp->prr_out;
Yuchung Cheng37598242015-07-01 14:11:15 -07002521 } else if ((flag & FLAG_RETRANS_DATA_ACKED) &&
2522 !(flag & FLAG_LOST_RETRANS)) {
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002523 sndcnt = min_t(int, delta,
2524 max_t(int, tp->prr_delivered - tp->prr_out,
2525 newly_acked_sacked) + 1);
Yuchung Cheng37598242015-07-01 14:11:15 -07002526 } else {
2527 sndcnt = min(delta, newly_acked_sacked);
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002528 }
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002529 /* Force a fast retransmit upon entering fast recovery */
2530 sndcnt = max(sndcnt, (tp->prr_out ? 0 : 1));
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002531 tp->snd_cwnd = tcp_packets_in_flight(tp) + sndcnt;
2532}
2533
Yuchung Cheng684bad12012-09-02 17:38:04 +00002534static inline void tcp_end_cwnd_reduction(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002536 struct tcp_sock *tp = tcp_sk(sk);
Nandita Dukkipatia262f0c2011-08-21 20:21:57 +00002537
Yuchung Chengc0402762016-09-19 23:39:21 -04002538 if (inet_csk(sk)->icsk_ca_ops->cong_control)
2539 return;
2540
Yuchung Cheng684bad12012-09-02 17:38:04 +00002541 /* Reset cwnd to ssthresh in CWR or Recovery (unless it's undone) */
Yuchung Chenged254972017-08-01 13:22:32 -07002542 if (tp->snd_ssthresh < TCP_INFINITE_SSTHRESH &&
2543 (inet_csk(sk)->icsk_ca_state == TCP_CA_CWR || tp->undo_marker)) {
Yuchung Cheng684bad12012-09-02 17:38:04 +00002544 tp->snd_cwnd = tp->snd_ssthresh;
Eric Dumazetc2203cf2017-05-16 14:00:04 -07002545 tp->snd_cwnd_stamp = tcp_jiffies32;
Yuchung Cheng67d41202011-03-14 10:57:03 +00002546 }
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002547 tcp_ca_event(sk, CA_EVENT_COMPLETE_CWR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
2549
Yuchung Cheng684bad12012-09-02 17:38:04 +00002550/* Enter CWR state. Disable cwnd undo since congestion is proven with ECN */
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002551void tcp_enter_cwr(struct sock *sk)
Yuchung Cheng09484d12012-09-02 17:38:02 +00002552{
2553 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng09484d12012-09-02 17:38:02 +00002554
2555 tp->prior_ssthresh = 0;
Yuchung Cheng684bad12012-09-02 17:38:04 +00002556 if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) {
Yuchung Cheng09484d12012-09-02 17:38:02 +00002557 tp->undo_marker = 0;
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002558 tcp_init_cwnd_reduction(sk);
Yuchung Cheng09484d12012-09-02 17:38:02 +00002559 tcp_set_ca_state(sk, TCP_CA_CWR);
2560 }
2561}
Kenneth Klette Jonassen7782ad82015-06-10 19:08:16 +02002562EXPORT_SYMBOL(tcp_enter_cwr);
Yuchung Cheng09484d12012-09-02 17:38:02 +00002563
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07002564static void tcp_try_keep_open(struct sock *sk)
2565{
2566 struct tcp_sock *tp = tcp_sk(sk);
2567 int state = TCP_CA_Open;
2568
Neal Cardwellf6982042011-11-16 08:58:04 +00002569 if (tcp_left_out(tp) || tcp_any_retrans_done(sk))
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07002570 state = TCP_CA_Disorder;
2571
2572 if (inet_csk(sk)->icsk_ca_state != state) {
2573 tcp_set_ca_state(sk, state);
2574 tp->high_seq = tp->snd_nxt;
2575 }
2576}
2577
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002578static void tcp_try_to_open(struct sock *sk, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002580 struct tcp_sock *tp = tcp_sk(sk);
2581
Ilpo Järvinen86426c22007-08-09 14:45:17 +03002582 tcp_verify_left_out(tp);
2583
Yuchung Cheng9b441902013-03-20 13:32:58 +00002584 if (!tcp_any_retrans_done(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 tp->retrans_stamp = 0;
2586
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002587 if (flag & FLAG_ECE)
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002588 tcp_enter_cwr(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002590 if (inet_csk(sk)->icsk_ca_state != TCP_CA_CWR) {
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07002591 tcp_try_keep_open(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
2593}
2594
John Heffner5d424d52006-03-20 17:53:41 -08002595static void tcp_mtup_probe_failed(struct sock *sk)
2596{
2597 struct inet_connection_sock *icsk = inet_csk(sk);
2598
2599 icsk->icsk_mtup.search_high = icsk->icsk_mtup.probe_size - 1;
2600 icsk->icsk_mtup.probe_size = 0;
Eric Dumazetc10d9312016-04-29 14:16:47 -07002601 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMTUPFAIL);
John Heffner5d424d52006-03-20 17:53:41 -08002602}
2603
Ilpo Järvinen72211e92009-03-14 14:23:04 +00002604static void tcp_mtup_probe_success(struct sock *sk)
John Heffner5d424d52006-03-20 17:53:41 -08002605{
2606 struct tcp_sock *tp = tcp_sk(sk);
2607 struct inet_connection_sock *icsk = inet_csk(sk);
2608
2609 /* FIXME: breaks with very large cwnd */
2610 tp->prior_ssthresh = tcp_current_ssthresh(sk);
2611 tp->snd_cwnd = tp->snd_cwnd *
2612 tcp_mss_to_mtu(sk, tp->mss_cache) /
2613 icsk->icsk_mtup.probe_size;
2614 tp->snd_cwnd_cnt = 0;
Eric Dumazetc2203cf2017-05-16 14:00:04 -07002615 tp->snd_cwnd_stamp = tcp_jiffies32;
John Heffner9c6d5e52010-10-06 21:18:02 -07002616 tp->snd_ssthresh = tcp_current_ssthresh(sk);
John Heffner5d424d52006-03-20 17:53:41 -08002617
2618 icsk->icsk_mtup.search_low = icsk->icsk_mtup.probe_size;
2619 icsk->icsk_mtup.probe_size = 0;
2620 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetc10d9312016-04-29 14:16:47 -07002621 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMTUPSUCCESS);
John Heffner5d424d52006-03-20 17:53:41 -08002622}
2623
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002624/* Do a simple retransmit without using the backoff mechanisms in
2625 * tcp_timer. This is used for path mtu discovery.
2626 * The socket is already locked here.
2627 */
2628void tcp_simple_retransmit(struct sock *sk)
2629{
2630 const struct inet_connection_sock *icsk = inet_csk(sk);
2631 struct tcp_sock *tp = tcp_sk(sk);
2632 struct sk_buff *skb;
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00002633 unsigned int mss = tcp_current_mss(sk);
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002634 u32 prior_lost = tp->lost_out;
2635
Eric Dumazet75c119a2017-10-05 22:21:27 -07002636 skb_rbtree_walk(skb, &sk->tcp_rtx_queue) {
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08002637 if (tcp_skb_seglen(skb) > mss &&
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002638 !(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) {
2639 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) {
2640 TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
2641 tp->retrans_out -= tcp_skb_pcount(skb);
2642 }
2643 tcp_skb_mark_lost_uncond_verify(tp, skb);
2644 }
2645 }
2646
2647 tcp_clear_retrans_hints_partial(tp);
2648
2649 if (prior_lost == tp->lost_out)
2650 return;
2651
2652 if (tcp_is_reno(tp))
2653 tcp_limit_reno_sacked(tp);
2654
2655 tcp_verify_left_out(tp);
2656
2657 /* Don't muck with the congestion window here.
2658 * Reason is that we do not increase amount of _data_
2659 * in network, but units changed and effective
2660 * cwnd/ssthresh really reduced now.
2661 */
2662 if (icsk->icsk_ca_state != TCP_CA_Loss) {
2663 tp->high_seq = tp->snd_nxt;
2664 tp->snd_ssthresh = tcp_current_ssthresh(sk);
2665 tp->prior_ssthresh = 0;
2666 tp->undo_marker = 0;
2667 tcp_set_ca_state(sk, TCP_CA_Loss);
2668 }
2669 tcp_xmit_retransmit_queue(sk);
2670}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002671EXPORT_SYMBOL(tcp_simple_retransmit);
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002672
Yuchung Cheng57dde7f2017-01-12 22:11:33 -08002673void tcp_enter_recovery(struct sock *sk, bool ece_ack)
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002674{
2675 struct tcp_sock *tp = tcp_sk(sk);
2676 int mib_idx;
2677
2678 if (tcp_is_reno(tp))
2679 mib_idx = LINUX_MIB_TCPRENORECOVERY;
2680 else
2681 mib_idx = LINUX_MIB_TCPSACKRECOVERY;
2682
Eric Dumazetc10d9312016-04-29 14:16:47 -07002683 NET_INC_STATS(sock_net(sk), mib_idx);
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002684
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002685 tp->prior_ssthresh = 0;
Yuchung Cheng989e04c2014-08-22 14:15:22 -07002686 tcp_init_undo(tp);
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002687
Yuchung Cheng291a00d2015-07-01 14:11:14 -07002688 if (!tcp_in_cwnd_reduction(sk)) {
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002689 if (!ece_ack)
2690 tp->prior_ssthresh = tcp_current_ssthresh(sk);
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002691 tcp_init_cwnd_reduction(sk);
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002692 }
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002693 tcp_set_ca_state(sk, TCP_CA_Recovery);
2694}
2695
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002696/* Process an ACK in CA_Loss state. Move to CA_Open if lost data are
2697 * recovered or spurious. Otherwise retransmits more on partial ACKs.
2698 */
Yuchung Chenge662ca42016-02-02 10:33:04 -08002699static void tcp_process_loss(struct sock *sk, int flag, bool is_dupack,
2700 int *rexmit)
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002701{
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002702 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Chenge33099f2013-03-20 13:33:00 +00002703 bool recovered = !before(tp->snd_una, tp->high_seq);
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002704
Yuchung Chengda34ac72015-05-18 12:31:44 -07002705 if ((flag & FLAG_SND_UNA_ADVANCED) &&
2706 tcp_try_undo_loss(sk, false))
2707 return;
2708
Yuchung Cheng89fe18e2017-01-12 22:11:37 -08002709 /* The ACK (s)acks some never-retransmitted data meaning not all
2710 * the data packets before the timeout were lost. Therefore we
2711 * undo the congestion window and state. This is essentially
2712 * the operation in F-RTO (RFC5682 section 3.1 step 3.b). Since
2713 * a retransmitted skb is permantly marked, we can apply such an
2714 * operation even if F-RTO was not used.
2715 */
2716 if ((flag & FLAG_ORIG_SACK_ACKED) &&
2717 tcp_try_undo_loss(sk, tp->undo_marker))
2718 return;
Yuchung Cheng0cfa5c02014-05-30 15:25:59 -07002719
Yuchung Cheng89fe18e2017-01-12 22:11:37 -08002720 if (tp->frto) { /* F-RTO RFC5682 sec 3.1 (sack enhanced version). */
Yuchung Chengb7b0ed92015-05-18 12:31:45 -07002721 if (after(tp->snd_nxt, tp->high_seq)) {
2722 if (flag & FLAG_DATA_SACKED || is_dupack)
2723 tp->frto = 0; /* Step 3.a. loss was real */
Yuchung Chenge33099f2013-03-20 13:33:00 +00002724 } else if (flag & FLAG_SND_UNA_ADVANCED && !recovered) {
2725 tp->high_seq = tp->snd_nxt;
Yuchung Chenge662ca42016-02-02 10:33:04 -08002726 /* Step 2.b. Try send new data (but deferred until cwnd
2727 * is updated in tcp_ack()). Otherwise fall back to
2728 * the conventional recovery.
2729 */
Eric Dumazet75c119a2017-10-05 22:21:27 -07002730 if (!tcp_write_queue_empty(sk) &&
Yuchung Chenge662ca42016-02-02 10:33:04 -08002731 after(tcp_wnd_end(tp), tp->snd_nxt)) {
2732 *rexmit = REXMIT_NEW;
2733 return;
2734 }
Yuchung Chenge33099f2013-03-20 13:33:00 +00002735 tp->frto = 0;
2736 }
2737 }
2738
2739 if (recovered) {
2740 /* F-RTO RFC5682 sec 3.1 step 2.a and 1st part of step 3.a */
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002741 tcp_try_undo_recovery(sk);
2742 return;
2743 }
Yuchung Chenge33099f2013-03-20 13:33:00 +00002744 if (tcp_is_reno(tp)) {
2745 /* A Reno DUPACK means new data in F-RTO step 2.b above are
2746 * delivered. Lower inflight to clock out (re)tranmissions.
2747 */
2748 if (after(tp->snd_nxt, tp->high_seq) && is_dupack)
2749 tcp_add_reno_sack(sk);
2750 else if (flag & FLAG_SND_UNA_ADVANCED)
2751 tcp_reset_reno_sack(tp);
2752 }
Yuchung Chenge662ca42016-02-02 10:33:04 -08002753 *rexmit = REXMIT_LOST;
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002754}
2755
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002756/* Undo during fast recovery after partial ACK. */
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002757static bool tcp_try_undo_partial(struct sock *sk, const int acked)
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002758{
2759 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002760
Yuchung Cheng7026b912013-05-29 14:20:13 +00002761 if (tp->undo_marker && tcp_packet_delayed(tp)) {
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002762 /* Plain luck! Hole if filled with delayed
2763 * packet, rather than with a retransmit.
2764 */
Yuchung Cheng7026b912013-05-29 14:20:13 +00002765 tcp_update_reordering(sk, tcp_fackets_out(tp) + acked, 1);
2766
2767 /* We are getting evidence that the reordering degree is higher
2768 * than we realized. If there are no retransmits out then we
2769 * can undo. Otherwise we clock out new packets but do not
2770 * mark more packets lost or retransmit more.
2771 */
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002772 if (tp->retrans_out)
Yuchung Cheng7026b912013-05-29 14:20:13 +00002773 return true;
Yuchung Cheng7026b912013-05-29 14:20:13 +00002774
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002775 if (!tcp_any_retrans_done(sk))
2776 tp->retrans_stamp = 0;
2777
Yuchung Cheng7026b912013-05-29 14:20:13 +00002778 DBGUNDO(sk, "partial recovery");
2779 tcp_undo_cwnd_reduction(sk, true);
Eric Dumazetc10d9312016-04-29 14:16:47 -07002780 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPARTIALUNDO);
Yuchung Cheng7026b912013-05-29 14:20:13 +00002781 tcp_try_keep_open(sk);
2782 return true;
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002783 }
Yuchung Cheng7026b912013-05-29 14:20:13 +00002784 return false;
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002785}
2786
Eric Dumazetefab8f82017-04-25 10:15:35 -07002787static void tcp_rack_identify_loss(struct sock *sk, int *ack_flag)
Yuchung Cheng98e36d42017-01-12 22:11:35 -08002788{
2789 struct tcp_sock *tp = tcp_sk(sk);
2790
2791 /* Use RACK to detect loss */
Yuchung Chenga0370b32017-01-12 22:11:36 -08002792 if (sysctl_tcp_recovery & TCP_RACK_LOSS_DETECTION) {
Yuchung Cheng98e36d42017-01-12 22:11:35 -08002793 u32 prior_retrans = tp->retrans_out;
2794
Eric Dumazet128eda82017-04-25 10:15:34 -07002795 tcp_rack_mark_lost(sk);
Yuchung Cheng98e36d42017-01-12 22:11:35 -08002796 if (prior_retrans > tp->retrans_out)
2797 *ack_flag |= FLAG_LOST_RETRANS;
2798 }
2799}
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801/* Process an event, which can update packets-in-flight not trivially.
2802 * Main goal of this function is to calculate new estimate for left_out,
2803 * taking into account both packets sitting in receiver's buffer and
2804 * packets lost by network.
2805 *
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002806 * Besides that it updates the congestion state when packet loss or ECN
2807 * is detected. But it does not reduce the cwnd, it is done by the
2808 * congestion control later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 *
2810 * It does _not_ decide what to send, it is made in function
2811 * tcp_xmit_retransmit_queue().
2812 */
Yuchung Cheng68049732013-05-29 14:20:11 +00002813static void tcp_fastretrans_alert(struct sock *sk, const int acked,
Eric Dumazet1317a9d2017-04-25 10:15:36 -07002814 bool is_dupack, int *ack_flag, int *rexmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002816 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002818 int fast_rexmit = 0, flag = *ack_flag;
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002819 bool do_lost = is_dupack || ((flag & FLAG_DATA_SACKED) &&
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002820 (tcp_fackets_out(tp) > tp->reordering));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Eric Dumazet8ba6dda2017-10-05 22:21:25 -07002822 if (!tp->packets_out && tp->sacked_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 tp->sacked_out = 0;
Eric Dumazet8ba6dda2017-10-05 22:21:25 -07002824 if (!tp->sacked_out && tp->fackets_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 tp->fackets_out = 0;
2826
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002827 /* Now state machine starts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 * A. ECE, hence prohibit cwnd undoing, the reduction is required. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002829 if (flag & FLAG_ECE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 tp->prior_ssthresh = 0;
2831
2832 /* B. In all the states check for reneging SACKs. */
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002833 if (tcp_check_sack_reneging(sk, flag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return;
2835
Yuchung Cheng974c1232012-01-19 14:42:21 +00002836 /* C. Check consistency of the current state. */
Ilpo Järvinen005903b2007-08-09 14:44:16 +03002837 tcp_verify_left_out(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Yuchung Cheng974c1232012-01-19 14:42:21 +00002839 /* D. Check state exit conditions. State can be terminated
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 * when high_seq is ACKed. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002841 if (icsk->icsk_ca_state == TCP_CA_Open) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002842 WARN_ON(tp->retrans_out != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 tp->retrans_stamp = 0;
2844 } else if (!before(tp->snd_una, tp->high_seq)) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002845 switch (icsk->icsk_ca_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 case TCP_CA_CWR:
2847 /* CWR is to be held something *above* high_seq
2848 * is ACKed for CWR bit to reach receiver. */
2849 if (tp->snd_una != tp->high_seq) {
Yuchung Cheng684bad12012-09-02 17:38:04 +00002850 tcp_end_cwnd_reduction(sk);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002851 tcp_set_ca_state(sk, TCP_CA_Open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
2853 break;
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 case TCP_CA_Recovery:
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002856 if (tcp_is_reno(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 tcp_reset_reno_sack(tp);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002858 if (tcp_try_undo_recovery(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return;
Yuchung Cheng684bad12012-09-02 17:38:04 +00002860 tcp_end_cwnd_reduction(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 break;
2862 }
2863 }
2864
Yuchung Cheng974c1232012-01-19 14:42:21 +00002865 /* E. Process state. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002866 switch (icsk->icsk_ca_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 case TCP_CA_Recovery:
Ilpo Järvinen2e605292007-08-02 19:46:58 -07002868 if (!(flag & FLAG_SND_UNA_ADVANCED)) {
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002869 if (tcp_is_reno(tp) && is_dupack)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002870 tcp_add_reno_sack(sk);
Yuchung Cheng7026b912013-05-29 14:20:13 +00002871 } else {
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002872 if (tcp_try_undo_partial(sk, acked))
Yuchung Cheng7026b912013-05-29 14:20:13 +00002873 return;
2874 /* Partial ACK arrived. Force fast retransmit. */
2875 do_lost = tcp_is_reno(tp) ||
2876 tcp_fackets_out(tp) > tp->reordering;
2877 }
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002878 if (tcp_try_undo_dsack(sk)) {
2879 tcp_try_keep_open(sk);
2880 return;
2881 }
Eric Dumazetefab8f82017-04-25 10:15:35 -07002882 tcp_rack_identify_loss(sk, ack_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 break;
2884 case TCP_CA_Loss:
Yuchung Chenge662ca42016-02-02 10:33:04 -08002885 tcp_process_loss(sk, flag, is_dupack, rexmit);
Eric Dumazetefab8f82017-04-25 10:15:35 -07002886 tcp_rack_identify_loss(sk, ack_flag);
Yuchung Cheng98e36d42017-01-12 22:11:35 -08002887 if (!(icsk->icsk_ca_state == TCP_CA_Open ||
2888 (*ack_flag & FLAG_LOST_RETRANS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return;
Yuchung Cheng291a00d2015-07-01 14:11:14 -07002890 /* Change state if cwnd is undone or retransmits are lost */
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05002891 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 default:
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002893 if (tcp_is_reno(tp)) {
Ilpo Järvinen2e605292007-08-02 19:46:58 -07002894 if (flag & FLAG_SND_UNA_ADVANCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 tcp_reset_reno_sack(tp);
2896 if (is_dupack)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002897 tcp_add_reno_sack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 }
2899
Neal Cardwellf6982042011-11-16 08:58:04 +00002900 if (icsk->icsk_ca_state <= TCP_CA_Disorder)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002901 tcp_try_undo_dsack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Eric Dumazetefab8f82017-04-25 10:15:35 -07002903 tcp_rack_identify_loss(sk, ack_flag);
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002904 if (!tcp_time_to_recover(sk, flag)) {
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002905 tcp_try_to_open(sk, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 return;
2907 }
2908
John Heffner5d424d52006-03-20 17:53:41 -08002909 /* MTU probe failure: don't reduce cwnd */
2910 if (icsk->icsk_ca_state < TCP_CA_CWR &&
2911 icsk->icsk_mtup.probe_size &&
John Heffner0e7b1362006-03-20 21:32:58 -08002912 tp->snd_una == tp->mtu_probe.probe_seq_start) {
John Heffner5d424d52006-03-20 17:53:41 -08002913 tcp_mtup_probe_failed(sk);
2914 /* Restores the reduction we did in tcp_mtup_probe() */
2915 tp->snd_cwnd++;
2916 tcp_simple_retransmit(sk);
2917 return;
2918 }
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 /* Otherwise enter Recovery state */
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002921 tcp_enter_recovery(sk, (flag & FLAG_ECE));
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002922 fast_rexmit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 }
2924
Yuchung Cheng3e59cb02013-05-17 13:45:05 +00002925 if (do_lost)
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002926 tcp_update_scoreboard(sk, fast_rexmit);
Yuchung Chenge662ca42016-02-02 10:33:04 -08002927 *rexmit = REXMIT_LOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
Yuchung Chengf6722582015-10-16 21:57:42 -07002930static void tcp_update_rtt_min(struct sock *sk, u32 rtt_us)
2931{
Neal Cardwell64033892016-09-19 23:39:10 -04002932 struct tcp_sock *tp = tcp_sk(sk);
2933 u32 wlen = sysctl_tcp_min_rtt_wlen * HZ;
Yuchung Chengf6722582015-10-16 21:57:42 -07002934
Eric Dumazetac9517f2017-05-16 14:00:13 -07002935 minmax_running_min(&tp->rtt_min, wlen, tcp_jiffies32,
Neal Cardwell64033892016-09-19 23:39:10 -04002936 rtt_us ? : jiffies_to_usecs(1));
Yuchung Chengf6722582015-10-16 21:57:42 -07002937}
2938
Yuchung Cheng775e68a2017-05-31 11:30:53 -07002939static bool tcp_ack_update_rtt(struct sock *sk, const int flag,
2940 long seq_rtt_us, long sack_rtt_us,
2941 long ca_rtt_us, struct rate_sample *rs)
Ilpo Järvinen41834b72008-12-05 22:43:26 -08002942{
Yuchung Cheng5b08e472013-07-22 16:20:46 -07002943 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen41834b72008-12-05 22:43:26 -08002944
Yuchung Cheng5b08e472013-07-22 16:20:46 -07002945 /* Prefer RTT measured from ACK's timing to TS-ECR. This is because
2946 * broken middle-boxes or peers may corrupt TS-ECR fields. But
2947 * Karn's algorithm forbids taking RTT if some retransmitted data
2948 * is acked (RFC6298).
2949 */
Eric Dumazet740b0f12014-02-26 14:02:48 -08002950 if (seq_rtt_us < 0)
2951 seq_rtt_us = sack_rtt_us;
Yuchung Chenged084952013-07-22 16:20:48 -07002952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 /* RTTM Rule: A TSecr value received in a segment is used to
2954 * update the averaged RTT measurement only if the segment
2955 * acknowledges some new data, i.e., only if it advances the
2956 * left edge of the send window.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 * See draft-ietf-tcplw-high-performance-00, section 3.3.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 */
Eric Dumazet740b0f12014-02-26 14:02:48 -08002959 if (seq_rtt_us < 0 && tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
Eric Dumazet9a568de2017-05-16 14:00:14 -07002960 flag & FLAG_ACKED) {
2961 u32 delta = tcp_time_stamp(tp) - tp->rx_opt.rcv_tsecr;
2962 u32 delta_us = delta * (USEC_PER_SEC / TCP_TS_HZ);
2963
2964 seq_rtt_us = ca_rtt_us = delta_us;
2965 }
Yuchung Cheng775e68a2017-05-31 11:30:53 -07002966 rs->rtt_us = ca_rtt_us; /* RTT of last (S)ACKed packet (or -1) */
Eric Dumazet740b0f12014-02-26 14:02:48 -08002967 if (seq_rtt_us < 0)
Yuchung Chenged084952013-07-22 16:20:48 -07002968 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Yuchung Chengf6722582015-10-16 21:57:42 -07002970 /* ca_rtt_us >= 0 is counting on the invariant that ca_rtt_us is
2971 * always taken together with ACK, SACK, or TS-opts. Any negative
2972 * values will be skipped with the seq_rtt_us < 0 check above.
2973 */
2974 tcp_update_rtt_min(sk, ca_rtt_us);
Eric Dumazet740b0f12014-02-26 14:02:48 -08002975 tcp_rtt_estimator(sk, seq_rtt_us);
Yuchung Cheng5b08e472013-07-22 16:20:46 -07002976 tcp_set_rto(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Yuchung Cheng5b08e472013-07-22 16:20:46 -07002978 /* RFC6298: only reset backoff on valid RTT measurement. */
2979 inet_csk(sk)->icsk_backoff = 0;
Yuchung Chenged084952013-07-22 16:20:48 -07002980 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
Yuchung Cheng375fe022013-07-22 16:20:45 -07002983/* Compute time elapsed between (last) SYNACK and the ACK completing 3WHS. */
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07002984void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req)
Yuchung Cheng375fe022013-07-22 16:20:45 -07002985{
Yuchung Cheng775e68a2017-05-31 11:30:53 -07002986 struct rate_sample rs;
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07002987 long rtt_us = -1L;
Yuchung Cheng375fe022013-07-22 16:20:45 -07002988
Eric Dumazet9a568de2017-05-16 14:00:14 -07002989 if (req && !req->num_retrans && tcp_rsk(req)->snt_synack)
2990 rtt_us = tcp_stamp_us_delta(tcp_clock_us(), tcp_rsk(req)->snt_synack);
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07002991
Yuchung Cheng775e68a2017-05-31 11:30:53 -07002992 tcp_ack_update_rtt(sk, FLAG_SYN_ACKED, rtt_us, -1L, rtt_us, &rs);
Yuchung Cheng375fe022013-07-22 16:20:45 -07002993}
2994
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07002995
Eric Dumazet24901552014-05-02 21:18:05 -07002996static void tcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002998 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet24901552014-05-02 21:18:05 -07002999
3000 icsk->icsk_ca_ops->cong_avoid(sk, ack, acked);
Eric Dumazetc2203cf2017-05-16 14:00:04 -07003001 tcp_sk(sk)->snd_cwnd_stamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
3003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004/* Restart timer after forward progress on connection.
3005 * RFC2988 recommends to restart timer to now+rto.
3006 */
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003007void tcp_rearm_rto(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00003009 const struct inet_connection_sock *icsk = inet_csk(sk);
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003010 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07003011
Jerry Chu168a8f52012-08-31 12:29:13 +00003012 /* If the retrans timer is currently being used by Fast Open
3013 * for SYN-ACK retrans purpose, stay put.
3014 */
3015 if (tp->fastopen_rsk)
3016 return;
3017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 if (!tp->packets_out) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003019 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 } else {
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003021 u32 rto = inet_csk(sk)->icsk_rto;
3022 /* Offset the time elapsed after installing regular RTO */
Yuchung Chengbec41a12017-01-12 22:11:39 -08003023 if (icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00003024 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
Neal Cardwelle1a10ef2017-08-03 09:19:52 -04003025 s64 delta_us = tcp_rto_delta_us(sk);
Eric Dumazetb17b8a22017-05-18 09:15:58 -07003026 /* delta_us may not be positive if the socket is locked
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00003027 * when the retrans timer fires and is rescheduled.
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003028 */
Neal Cardwellcdbeb632017-08-16 17:53:36 -04003029 rto = usecs_to_jiffies(max_t(int, delta_us, 1));
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003030 }
3031 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, rto,
3032 TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003034}
3035
Neal Cardwelldf92c832017-08-03 09:19:54 -04003036/* Try to schedule a loss probe; if that doesn't work, then schedule an RTO. */
3037static void tcp_set_xmit_timer(struct sock *sk)
3038{
3039 if (!tcp_schedule_loss_probe(sk))
3040 tcp_rearm_rto(sk);
3041}
3042
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003043/* If we get here, the whole TSO packet has not been acked. */
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003044static u32 tcp_tso_acked(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
3046 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003047 u32 packets_acked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003049 BUG_ON(!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051 packets_acked = tcp_skb_pcount(skb);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003052 if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 return 0;
3054 packets_acked -= tcp_skb_pcount(skb);
3055
3056 if (packets_acked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 BUG_ON(tcp_skb_pcount(skb) == 0);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003058 BUG_ON(!before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
3060
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003061 return packets_acked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
3063
Eric Dumazetad971f62014-10-11 15:17:29 -07003064static void tcp_ack_tstamp(struct sock *sk, struct sk_buff *skb,
3065 u32 prior_snd_una)
3066{
3067 const struct skb_shared_info *shinfo;
3068
3069 /* Avoid cache line misses to get skb_shinfo() and shinfo->tx_flags */
Soheil Hassas Yeganeh6b084922016-04-02 23:08:08 -04003070 if (likely(!TCP_SKB_CB(skb)->txstamp_ack))
Eric Dumazetad971f62014-10-11 15:17:29 -07003071 return;
3072
3073 shinfo = skb_shinfo(skb);
Soheil Hassas Yeganeh0a2cf202016-04-27 23:39:01 -04003074 if (!before(shinfo->tskey, prior_snd_una) &&
Eric Dumazete2080072017-10-04 12:59:58 -07003075 before(shinfo->tskey, tcp_sk(sk)->snd_una)) {
3076 tcp_skb_tsorted_save(skb) {
3077 __skb_tstamp_tx(skb, NULL, sk, SCM_TSTAMP_ACK);
3078 } tcp_skb_tsorted_restore(skb);
3079 }
Eric Dumazetad971f62014-10-11 15:17:29 -07003080}
3081
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003082/* Remove acknowledged frames from the retransmission queue. If our packet
3083 * is before the ack sequence we can discard it as it's confirmed to have
3084 * arrived at the other end.
3085 */
Ilpo Järvinen33f5f572008-10-07 14:43:06 -07003086static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,
Yuchung Cheng3ebd88712016-02-02 10:33:07 -08003087 u32 prior_snd_una, int *acked,
Yuchung Chengdeed7be2017-01-12 22:11:32 -08003088 struct tcp_sacktag_state *sack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089{
Stephen Hemminger2d2abba2005-11-10 16:56:12 -08003090 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet9a568de2017-05-16 14:00:14 -07003091 u64 first_ackt, last_ackt;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003092 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen90638a02008-09-20 21:25:52 -07003093 u32 prior_sacked = tp->sacked_out;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003094 u32 reord = tp->packets_out;
Eric Dumazet75c119a2017-10-05 22:21:27 -07003095 struct sk_buff *skb, *next;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003096 bool fully_acked = true;
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02003097 long sack_rtt_us = -1L;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003098 long seq_rtt_us = -1L;
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02003099 long ca_rtt_us = -1L;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003100 u32 pkts_acked = 0;
Lawrence Brakmo6f094b92016-06-08 21:16:44 -07003101 u32 last_in_flight = 0;
Yuchung Cheng2f715c12013-10-24 08:59:27 -07003102 bool rtt_update;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003103 int flag = 0;
3104
Eric Dumazet9a568de2017-05-16 14:00:14 -07003105 first_ackt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Eric Dumazet75c119a2017-10-05 22:21:27 -07003107 for (skb = skb_rb_first(&sk->tcp_rtx_queue); skb; skb = next) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003108 struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003109 u8 sacked = scb->sacked;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003110 u32 acked_pcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Eric Dumazetad971f62014-10-11 15:17:29 -07003112 tcp_ack_tstamp(sk, skb, prior_snd_una);
Willem de Bruijn712a7222014-08-12 14:53:16 -04003113
Gavin McCullagh2072c222007-12-29 19:11:21 -08003114 /* Determine how many packets and what bytes were acked, tso and else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 if (after(scb->end_seq, tp->snd_una)) {
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003116 if (tcp_skb_pcount(skb) == 1 ||
3117 !after(tp->snd_una, scb->seq))
3118 break;
3119
Ilpo Järvinen72018832007-12-30 04:37:55 -08003120 acked_pcount = tcp_tso_acked(sk, skb);
3121 if (!acked_pcount)
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003122 break;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003123 fully_acked = false;
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003124 } else {
Ilpo Järvinen72018832007-12-30 04:37:55 -08003125 acked_pcount = tcp_skb_pcount(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 }
3127
Eric Dumazetad971f62014-10-11 15:17:29 -07003128 if (unlikely(sacked & TCPCB_RETRANS)) {
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003129 if (sacked & TCPCB_SACKED_RETRANS)
Ilpo Järvinen72018832007-12-30 04:37:55 -08003130 tp->retrans_out -= acked_pcount;
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003131 flag |= FLAG_RETRANS_DATA_ACKED;
Kenneth Klette Jonassen3d0d26c2015-04-11 02:17:49 +02003132 } else if (!(sacked & TCPCB_SACKED_ACKED)) {
Eric Dumazet740b0f12014-02-26 14:02:48 -08003133 last_ackt = skb->skb_mstamp;
Eric Dumazet9a568de2017-05-16 14:00:14 -07003134 WARN_ON_ONCE(last_ackt == 0);
3135 if (!first_ackt)
Eric Dumazet740b0f12014-02-26 14:02:48 -08003136 first_ackt = last_ackt;
3137
Lawrence Brakmo6f094b92016-06-08 21:16:44 -07003138 last_in_flight = TCP_SKB_CB(skb)->tx.in_flight;
Kenneth Klette Jonassen3d0d26c2015-04-11 02:17:49 +02003139 reord = min(pkts_acked, reord);
3140 if (!after(scb->end_seq, tp->high_seq))
3141 flag |= FLAG_ORIG_SACK_ACKED;
Stephen Hemminger2d2abba2005-11-10 16:56:12 -08003142 }
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003143
Yuchung Chengddf1af62016-02-02 10:33:06 -08003144 if (sacked & TCPCB_SACKED_ACKED) {
Ilpo Järvinen72018832007-12-30 04:37:55 -08003145 tp->sacked_out -= acked_pcount;
Yuchung Chengddf1af62016-02-02 10:33:06 -08003146 } else if (tcp_is_sack(tp)) {
3147 tp->delivered += acked_pcount;
3148 if (!tcp_skb_spurious_retrans(tp, skb))
Yuchung Cheng1d0833d2017-01-12 22:11:34 -08003149 tcp_rack_advance(tp, sacked, scb->end_seq,
Eric Dumazet9a568de2017-05-16 14:00:14 -07003150 skb->skb_mstamp);
Yuchung Chengddf1af62016-02-02 10:33:06 -08003151 }
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003152 if (sacked & TCPCB_LOST)
Ilpo Järvinen72018832007-12-30 04:37:55 -08003153 tp->lost_out -= acked_pcount;
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003154
Ilpo Järvinen72018832007-12-30 04:37:55 -08003155 tp->packets_out -= acked_pcount;
3156 pkts_acked += acked_pcount;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003157 tcp_rate_skb_delivered(sk, skb, sack->rate);
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003158
Ilpo Järvinen009a2e32007-09-20 11:34:38 -07003159 /* Initial outgoing SYN's get put onto the write_queue
3160 * just like anything else we transmit. It is not
3161 * true data, and if we misinform our callers that
3162 * this ACK acks real data, we will erroneously exit
3163 * connection startup slow start one packet too
3164 * quickly. This is severely frowned upon behavior.
3165 */
Eric Dumazetad971f62014-10-11 15:17:29 -07003166 if (likely(!(scb->tcp_flags & TCPHDR_SYN))) {
Ilpo Järvinen009a2e32007-09-20 11:34:38 -07003167 flag |= FLAG_DATA_ACKED;
3168 } else {
3169 flag |= FLAG_SYN_ACKED;
3170 tp->retrans_stamp = 0;
3171 }
3172
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003173 if (!fully_acked)
3174 break;
3175
Eric Dumazet75c119a2017-10-05 22:21:27 -07003176 next = skb_rb_next(skb);
Eric Dumazetad971f62014-10-11 15:17:29 -07003177 if (unlikely(skb == tp->retransmit_skb_hint))
Ilpo Järvinenef9da472008-09-20 21:25:15 -07003178 tp->retransmit_skb_hint = NULL;
Eric Dumazetad971f62014-10-11 15:17:29 -07003179 if (unlikely(skb == tp->lost_skb_hint))
Ilpo Järvinen90638a02008-09-20 21:25:52 -07003180 tp->lost_skb_hint = NULL;
Eric Dumazet75c119a2017-10-05 22:21:27 -07003181 tcp_rtx_queue_unlink_and_free(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 }
3183
Francis Yan0f872302016-11-27 23:07:14 -08003184 if (!skb)
3185 tcp_chrono_stop(sk, TCP_CHRONO_BUSY);
3186
Ilpo Järvinen33f5f572008-10-07 14:43:06 -07003187 if (likely(between(tp->snd_up, prior_snd_una, tp->snd_una)))
3188 tp->snd_up = tp->snd_una;
3189
Ilpo Järvinencadbd032007-12-31 04:49:21 -08003190 if (skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
3191 flag |= FLAG_SACK_RENEGING;
3192
Eric Dumazet9a568de2017-05-16 14:00:14 -07003193 if (likely(first_ackt) && !(flag & FLAG_RETRANS_DATA_ACKED)) {
3194 seq_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, first_ackt);
3195 ca_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, last_ackt);
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02003196 }
Eric Dumazet9a568de2017-05-16 14:00:14 -07003197 if (sack->first_sackt) {
3198 sack_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, sack->first_sackt);
3199 ca_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, sack->last_sackt);
Eric Dumazet740b0f12014-02-26 14:02:48 -08003200 }
Yuchung Chengf6722582015-10-16 21:57:42 -07003201 rtt_update = tcp_ack_update_rtt(sk, flag, seq_rtt_us, sack_rtt_us,
Yuchung Cheng775e68a2017-05-31 11:30:53 -07003202 ca_rtt_us, sack->rate);
Yuchung Chenged084952013-07-22 16:20:48 -07003203
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003204 if (flag & FLAG_ACKED) {
Neal Cardwelldf92c832017-08-03 09:19:54 -04003205 flag |= FLAG_SET_XMIT_TIMER; /* set TLP or RTO timer */
Ilpo Järvinen72211e92009-03-14 14:23:04 +00003206 if (unlikely(icsk->icsk_mtup.probe_size &&
3207 !after(tp->mtu_probe.probe_seq_end, tp->snd_una))) {
3208 tcp_mtup_probe_success(sk);
3209 }
3210
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003211 if (tcp_is_reno(tp)) {
3212 tcp_remove_reno_sacks(sk, pkts_acked);
3213 } else {
Ilpo Järvinen59a08cb2009-02-28 04:44:28 +00003214 int delta;
3215
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003216 /* Non-retransmitted hole got filled? That's reordering */
Soheil Hassas Yeganehbafbb9c2017-05-15 17:05:47 -04003217 if (reord < prior_fackets && reord <= tp->fackets_out)
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003218 tcp_update_reordering(sk, tp->fackets_out - reord, 0);
Ilpo Järvinen90638a02008-09-20 21:25:52 -07003219
Ilpo Järvinen59a08cb2009-02-28 04:44:28 +00003220 delta = tcp_is_fack(tp) ? pkts_acked :
3221 prior_sacked - tp->sacked_out;
3222 tp->lost_cnt_hint -= min(tp->lost_cnt_hint, delta);
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003223 }
3224
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -07003225 tp->fackets_out -= min(pkts_acked, tp->fackets_out);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08003226
Eric Dumazet740b0f12014-02-26 14:02:48 -08003227 } else if (skb && rtt_update && sack_rtt_us >= 0 &&
Eric Dumazet9a568de2017-05-16 14:00:14 -07003228 sack_rtt_us > tcp_stamp_us_delta(tp->tcp_mstamp, skb->skb_mstamp)) {
Yuchung Cheng2f715c12013-10-24 08:59:27 -07003229 /* Do not re-arm RTO if the sack RTT is measured from data sent
3230 * after when the head was last (re)transmitted. Otherwise the
3231 * timeout may continue to extend in loss recovery.
3232 */
Neal Cardwelldf92c832017-08-03 09:19:54 -04003233 flag |= FLAG_SET_XMIT_TIMER; /* set TLP or RTO timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 }
3235
Lawrence Brakmo756ee172016-05-11 10:02:13 -07003236 if (icsk->icsk_ca_ops->pkts_acked) {
3237 struct ack_sample sample = { .pkts_acked = pkts_acked,
Yuchung Cheng775e68a2017-05-31 11:30:53 -07003238 .rtt_us = sack->rate->rtt_us,
Lawrence Brakmo6f094b92016-06-08 21:16:44 -07003239 .in_flight = last_in_flight };
Lawrence Brakmo756ee172016-05-11 10:02:13 -07003240
3241 icsk->icsk_ca_ops->pkts_acked(sk, &sample);
3242 }
Kenneth Klette Jonassen138998f2015-05-01 01:10:59 +02003243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244#if FASTRETRANS_DEBUG > 0
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003245 WARN_ON((int)tp->sacked_out < 0);
3246 WARN_ON((int)tp->lost_out < 0);
3247 WARN_ON((int)tp->retrans_out < 0);
Ilpo Järvinene60402d2007-08-09 15:14:46 +03003248 if (!tp->packets_out && tcp_is_sack(tp)) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07003249 icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 if (tp->lost_out) {
Joe Perches91df42b2012-05-15 14:11:54 +00003251 pr_debug("Leak l=%u %d\n",
3252 tp->lost_out, icsk->icsk_ca_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 tp->lost_out = 0;
3254 }
3255 if (tp->sacked_out) {
Joe Perches91df42b2012-05-15 14:11:54 +00003256 pr_debug("Leak s=%u %d\n",
3257 tp->sacked_out, icsk->icsk_ca_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 tp->sacked_out = 0;
3259 }
3260 if (tp->retrans_out) {
Joe Perches91df42b2012-05-15 14:11:54 +00003261 pr_debug("Leak r=%u %d\n",
3262 tp->retrans_out, icsk->icsk_ca_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 tp->retrans_out = 0;
3264 }
3265 }
3266#endif
Yuchung Cheng3ebd88712016-02-02 10:33:07 -08003267 *acked = pkts_acked;
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003268 return flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269}
3270
3271static void tcp_ack_probe(struct sock *sk)
3272{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003273 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet75c119a2017-10-05 22:21:27 -07003274 struct sk_buff *head = tcp_send_head(sk);
3275 const struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 /* Was it a usable window open? */
Eric Dumazet75c119a2017-10-05 22:21:27 -07003278 if (!head)
3279 return;
3280 if (!after(TCP_SKB_CB(head)->end_seq, tcp_wnd_end(tp))) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003281 icsk->icsk_backoff = 0;
3282 inet_csk_clear_xmit_timer(sk, ICSK_TIME_PROBE0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 /* Socket must be waked up by subsequent tcp_data_snd_check().
3284 * This function is not for random using!
3285 */
3286 } else {
Eric Dumazet21c8fe92015-05-06 14:26:24 -07003287 unsigned long when = tcp_probe0_when(sk, TCP_RTO_MAX);
Eric Dumazetfcdd1cf2014-09-22 13:19:44 -07003288
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003289 inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
Eric Dumazetfcdd1cf2014-09-22 13:19:44 -07003290 when, TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 }
3292}
3293
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00003294static inline bool tcp_ack_is_dubious(const struct sock *sk, const int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
Eric Dumazeta02cec22010-09-22 20:43:57 +00003296 return !(flag & FLAG_NOT_DUP) || (flag & FLAG_CA_ALERT) ||
3297 inet_csk(sk)->icsk_ca_state != TCP_CA_Open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003300/* Decide wheather to run the increase function of congestion control. */
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00003301static inline bool tcp_may_raise_cwnd(const struct sock *sk, const int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003303 /* If reordering is high then always grow cwnd whenever data is
3304 * delivered regardless of its ordering. Otherwise stay conservative
Yuchung Cheng16edfe72013-09-05 15:36:09 -07003305 * and only grow cwnd on in-order delivery (RFC5681). A stretched ACK w/
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003306 * new SACK or ECE mark may first advance cwnd here and later reduce
3307 * cwnd in tcp_fastretrans_alert() based on more states.
3308 */
Nikolay Borisov1043e252016-02-03 09:46:52 +02003309 if (tcp_sk(sk)->reordering > sock_net(sk)->ipv4.sysctl_tcp_reordering)
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003310 return flag & FLAG_FORWARD_PROGRESS;
3311
Yuchung Cheng16edfe72013-09-05 15:36:09 -07003312 return flag & FLAG_DATA_ACKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313}
3314
Yuchung Chengd452e6c2016-02-02 10:33:09 -08003315/* The "ultimate" congestion control function that aims to replace the rigid
3316 * cwnd increase and decrease control (tcp_cong_avoid,tcp_*cwnd_reduction).
3317 * It's called toward the end of processing an ACK with precise rate
3318 * information. All transmission or retransmission are delayed afterwards.
3319 */
3320static void tcp_cong_control(struct sock *sk, u32 ack, u32 acked_sacked,
Yuchung Chengc0402762016-09-19 23:39:21 -04003321 int flag, const struct rate_sample *rs)
Yuchung Chengd452e6c2016-02-02 10:33:09 -08003322{
Yuchung Chengc0402762016-09-19 23:39:21 -04003323 const struct inet_connection_sock *icsk = inet_csk(sk);
3324
3325 if (icsk->icsk_ca_ops->cong_control) {
3326 icsk->icsk_ca_ops->cong_control(sk, rs);
3327 return;
3328 }
3329
Yuchung Chengd452e6c2016-02-02 10:33:09 -08003330 if (tcp_in_cwnd_reduction(sk)) {
3331 /* Reduce cwnd if state mandates */
3332 tcp_cwnd_reduction(sk, acked_sacked, flag);
3333 } else if (tcp_may_raise_cwnd(sk, flag)) {
3334 /* Advance cwnd if state allows */
3335 tcp_cong_avoid(sk, ack, acked_sacked);
3336 }
3337 tcp_update_pacing_rate(sk);
3338}
3339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340/* Check that window update is acceptable.
3341 * The function assumes that snd_una<=ack<=snd_next.
3342 */
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00003343static inline bool tcp_may_update_window(const struct tcp_sock *tp,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08003344 const u32 ack, const u32 ack_seq,
3345 const u32 nwin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
Eric Dumazeta02cec22010-09-22 20:43:57 +00003347 return after(ack, tp->snd_una) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 after(ack_seq, tp->snd_wl1) ||
Eric Dumazeta02cec22010-09-22 20:43:57 +00003349 (ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350}
3351
Eric Dumazet0df48c22015-04-28 15:28:17 -07003352/* If we update tp->snd_una, also update tp->bytes_acked */
3353static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack)
3354{
3355 u32 delta = ack - tp->snd_una;
3356
Eric Dumazet46cc6e42016-05-03 16:56:03 -07003357 sock_owned_by_me((struct sock *)tp);
Eric Dumazet0df48c22015-04-28 15:28:17 -07003358 tp->bytes_acked += delta;
3359 tp->snd_una = ack;
3360}
3361
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07003362/* If we update tp->rcv_nxt, also update tp->bytes_received */
3363static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq)
3364{
3365 u32 delta = seq - tp->rcv_nxt;
3366
Eric Dumazet46cc6e42016-05-03 16:56:03 -07003367 sock_owned_by_me((struct sock *)tp);
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07003368 tp->bytes_received += delta;
3369 tp->rcv_nxt = seq;
3370}
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372/* Update our send window.
3373 *
3374 * Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2
3375 * and in FreeBSD. NetBSD's one is even worse.) is wrong.
3376 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003377static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 ack,
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07003378 u32 ack_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07003380 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 int flag = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07003382 u32 nwin = ntohs(tcp_hdr(skb)->window);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07003384 if (likely(!tcp_hdr(skb)->syn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 nwin <<= tp->rx_opt.snd_wscale;
3386
3387 if (tcp_may_update_window(tp, ack, ack_seq, nwin)) {
3388 flag |= FLAG_WIN_UPDATE;
Hantzis Fotisee7537b2009-03-02 22:42:02 -08003389 tcp_update_wl(tp, ack_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
3391 if (tp->snd_wnd != nwin) {
3392 tp->snd_wnd = nwin;
3393
Florian Westphal31770e32017-08-30 19:24:58 +02003394 /* Note, it is the only place, where
3395 * fast path is recovered for sending TCP.
3396 */
3397 tp->pred_flags = 0;
3398 tcp_fast_path_check(sk);
3399
Eric Dumazet75c119a2017-10-05 22:21:27 -07003400 if (!tcp_write_queue_empty(sk))
Eric Dumazet6f021c62015-08-21 12:30:00 -07003401 tcp_slow_start_after_idle_check(sk);
3402
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 if (nwin > tp->max_window) {
3404 tp->max_window = nwin;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08003405 tcp_sync_mss(sk, inet_csk(sk)->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 }
3407 }
3408 }
3409
Eric Dumazet0df48c22015-04-28 15:28:17 -07003410 tcp_snd_una_update(tp, ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
3412 return flag;
3413}
3414
Jason Baron083ae302016-07-14 11:38:40 -04003415static bool __tcp_oow_rate_limited(struct net *net, int mib_idx,
3416 u32 *last_oow_ack_time)
3417{
3418 if (*last_oow_ack_time) {
Eric Dumazet594208a2017-05-16 14:00:10 -07003419 s32 elapsed = (s32)(tcp_jiffies32 - *last_oow_ack_time);
Jason Baron083ae302016-07-14 11:38:40 -04003420
3421 if (0 <= elapsed && elapsed < sysctl_tcp_invalid_ratelimit) {
3422 NET_INC_STATS(net, mib_idx);
3423 return true; /* rate-limited: don't send yet! */
3424 }
3425 }
3426
Eric Dumazet594208a2017-05-16 14:00:10 -07003427 *last_oow_ack_time = tcp_jiffies32;
Jason Baron083ae302016-07-14 11:38:40 -04003428
3429 return false; /* not rate-limited: go ahead, send dupack now! */
3430}
3431
Eric Dumazet7970ddc2015-03-16 21:06:20 -07003432/* Return true if we're currently rate-limiting out-of-window ACKs and
3433 * thus shouldn't send a dupack right now. We rate-limit dupacks in
3434 * response to out-of-window SYNs or ACKs to mitigate ACK loops or DoS
3435 * attacks that send repeated SYNs or ACKs for the same connection. To
3436 * do this, we do not send a duplicate SYNACK or ACK if the remote
3437 * endpoint is sending out-of-window SYNs or pure ACKs at a high rate.
3438 */
3439bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
3440 int mib_idx, u32 *last_oow_ack_time)
3441{
3442 /* Data packets without SYNs are not likely part of an ACK loop. */
3443 if ((TCP_SKB_CB(skb)->seq != TCP_SKB_CB(skb)->end_seq) &&
3444 !tcp_hdr(skb)->syn)
Jason Baron083ae302016-07-14 11:38:40 -04003445 return false;
Eric Dumazet7970ddc2015-03-16 21:06:20 -07003446
Jason Baron083ae302016-07-14 11:38:40 -04003447 return __tcp_oow_rate_limited(net, mib_idx, last_oow_ack_time);
Eric Dumazet7970ddc2015-03-16 21:06:20 -07003448}
3449
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003450/* RFC 5961 7 [ACK Throttling] */
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003451static void tcp_send_challenge_ack(struct sock *sk, const struct sk_buff *skb)
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003452{
3453 /* unprotected vars, we dont care of overwrites */
3454 static u32 challenge_timestamp;
3455 static unsigned int challenge_count;
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003456 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003457 u32 count, now;
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003458
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003459 /* First check our per-socket dupack rate limit. */
Jason Baron083ae302016-07-14 11:38:40 -04003460 if (__tcp_oow_rate_limited(sock_net(sk),
3461 LINUX_MIB_TCPACKSKIPPEDCHALLENGE,
3462 &tp->last_oow_ack_time))
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003463 return;
3464
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003465 /* Then check host-wide RFC 5961 rate limit. */
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003466 now = jiffies / HZ;
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003467 if (now != challenge_timestamp) {
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003468 u32 half = (sysctl_tcp_challenge_ack_limit + 1) >> 1;
3469
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003470 challenge_timestamp = now;
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003471 WRITE_ONCE(challenge_count, half +
3472 prandom_u32_max(sysctl_tcp_challenge_ack_limit));
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003473 }
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003474 count = READ_ONCE(challenge_count);
3475 if (count > 0) {
3476 WRITE_ONCE(challenge_count, count - 1);
Eric Dumazetc10d9312016-04-29 14:16:47 -07003477 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPCHALLENGEACK);
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003478 tcp_send_ack(sk);
3479 }
3480}
3481
Eric Dumazet12fb3dd2013-04-19 07:19:48 +00003482static void tcp_store_ts_recent(struct tcp_sock *tp)
3483{
3484 tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval;
3485 tp->rx_opt.ts_recent_stamp = get_seconds();
3486}
3487
3488static void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq)
3489{
3490 if (tp->rx_opt.saw_tstamp && !after(seq, tp->rcv_wup)) {
3491 /* PAWS bug workaround wrt. ACK frames, the PAWS discard
3492 * extra check below makes sure this can only happen
3493 * for pure ACK frames. -DaveM
3494 *
3495 * Not only, also it occurs for expired timestamps.
3496 */
3497
3498 if (tcp_paws_check(&tp->rx_opt, 0))
3499 tcp_store_ts_recent(tp);
3500 }
3501}
3502
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003503/* This routine deals with acks during a TLP episode.
Sébastien Barré08abdff2015-01-12 10:30:40 +01003504 * We mark the end of a TLP episode on receiving TLP dupack or when
3505 * ack is after tlp_high_seq.
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003506 * Ref: loss detection algorithm in draft-dukkipati-tcpm-tcp-loss-probe.
3507 */
3508static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
3509{
3510 struct tcp_sock *tp = tcp_sk(sk);
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003511
Sébastien Barré08abdff2015-01-12 10:30:40 +01003512 if (before(ack, tp->tlp_high_seq))
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003513 return;
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003514
Sébastien Barré08abdff2015-01-12 10:30:40 +01003515 if (flag & FLAG_DSACKING_ACK) {
3516 /* This DSACK means original and TLP probe arrived; no loss */
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003517 tp->tlp_high_seq = 0;
Sébastien Barré08abdff2015-01-12 10:30:40 +01003518 } else if (after(ack, tp->tlp_high_seq)) {
3519 /* ACK advances: there was a loss, so reduce cwnd. Reset
3520 * tlp_high_seq in tcp_init_cwnd_reduction()
3521 */
3522 tcp_init_cwnd_reduction(sk);
3523 tcp_set_ca_state(sk, TCP_CA_CWR);
3524 tcp_end_cwnd_reduction(sk);
3525 tcp_try_keep_open(sk);
Eric Dumazetc10d9312016-04-29 14:16:47 -07003526 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07003527 LINUX_MIB_TCPLOSSPROBERECOVERY);
Sébastien Barré08abdff2015-01-12 10:30:40 +01003528 } else if (!(flag & (FLAG_SND_UNA_ADVANCED |
3529 FLAG_NOT_DUP | FLAG_DATA_SACKED))) {
3530 /* Pure dupack: original and TLP probe arrived; no loss */
3531 tp->tlp_high_seq = 0;
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003532 }
3533}
3534
Florian Westphal7354c8c2014-09-26 22:37:34 +02003535static inline void tcp_in_ack_event(struct sock *sk, u32 flags)
3536{
3537 const struct inet_connection_sock *icsk = inet_csk(sk);
3538
3539 if (icsk->icsk_ca_ops->in_ack_event)
3540 icsk->icsk_ca_ops->in_ack_event(sk, flags);
3541}
3542
Yuchung Chenge662ca42016-02-02 10:33:04 -08003543/* Congestion control has updated the cwnd already. So if we're in
3544 * loss recovery then now we do any new sends (for FRTO) or
3545 * retransmits (for CA_Loss or CA_recovery) that make sense.
3546 */
3547static void tcp_xmit_recovery(struct sock *sk, int rexmit)
3548{
3549 struct tcp_sock *tp = tcp_sk(sk);
3550
3551 if (rexmit == REXMIT_NONE)
3552 return;
3553
3554 if (unlikely(rexmit == 2)) {
3555 __tcp_push_pending_frames(sk, tcp_current_mss(sk),
3556 TCP_NAGLE_OFF);
3557 if (after(tp->snd_nxt, tp->high_seq))
3558 return;
3559 tp->frto = 0;
3560 }
3561 tcp_xmit_retransmit_queue(sk);
3562}
3563
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564/* This routine deals with incoming acks, but not outgoing ones. */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003565static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03003567 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 struct tcp_sock *tp = tcp_sk(sk);
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02003569 struct tcp_sacktag_state sack_state;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003570 struct rate_sample rs = { .prior_delivered = 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 u32 prior_snd_una = tp->snd_una;
3572 u32 ack_seq = TCP_SKB_CB(skb)->seq;
3573 u32 ack = TCP_SKB_CB(skb)->ack_seq;
Neal Cardwell7d2b55f2011-11-16 08:58:01 +00003574 bool is_dupack = false;
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003575 u32 prior_fackets;
Nandita Dukkipati35f079e2013-05-21 15:12:07 +00003576 int prior_packets = tp->packets_out;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003577 u32 delivered = tp->delivered;
3578 u32 lost = tp->lost;
Yuchung Cheng68049732013-05-29 14:20:11 +00003579 int acked = 0; /* Number of packets newly acked */
Yuchung Chenge662ca42016-02-02 10:33:04 -08003580 int rexmit = REXMIT_NONE; /* Flag to (re)transmit to recover losses */
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02003581
Eric Dumazet9a568de2017-05-16 14:00:14 -07003582 sack_state.first_sackt = 0;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003583 sack_state.rate = &rs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Eric Dumazet75c119a2017-10-05 22:21:27 -07003585 /* We very likely will need to access rtx queue. */
3586 prefetch(sk->tcp_rtx_queue.rb_node);
Eric Dumazetad971f62014-10-11 15:17:29 -07003587
John Dykstra96e0bf42009-03-22 21:49:57 -07003588 /* If the ack is older than previous acks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 * then we can probably ignore it.
3590 */
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003591 if (before(ack, prior_snd_una)) {
3592 /* RFC 5961 5.2 [Blind Data Injection Attack].[Mitigation] */
3593 if (before(ack, prior_snd_una - tp->max_window)) {
Eric Dumazetd0e1a1b2017-05-23 15:24:46 -07003594 if (!(flag & FLAG_NO_CHALLENGE_ACK))
3595 tcp_send_challenge_ack(sk, skb);
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003596 return -1;
3597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 goto old_ack;
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
John Dykstra96e0bf42009-03-22 21:49:57 -07003601 /* If the ack includes data we haven't sent yet, discard
3602 * this segment (RFC793 Section 3.9).
3603 */
3604 if (after(ack, tp->snd_nxt))
3605 goto invalid_ack;
3606
Neal Cardwell0c9ab092014-08-14 16:13:07 -04003607 if (after(ack, prior_snd_una)) {
Ilpo Järvinen2e605292007-08-02 19:46:58 -07003608 flag |= FLAG_SND_UNA_ADVANCED;
Neal Cardwell0c9ab092014-08-14 16:13:07 -04003609 icsk->icsk_retransmits = 0;
3610 }
Ilpo Järvinen2e605292007-08-02 19:46:58 -07003611
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003612 prior_fackets = tp->fackets_out;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003613 rs.prior_in_flight = tcp_packets_in_flight(tp);
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003614
Eric Dumazet12fb3dd2013-04-19 07:19:48 +00003615 /* ts_recent update must be made after we are sure that the packet
3616 * is in window.
3617 */
3618 if (flag & FLAG_UPDATE_TS_RECENT)
3619 tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
3620
Florian Westphal31770e32017-08-30 19:24:58 +02003621 if (!(flag & FLAG_SLOWPATH) && after(ack, prior_snd_una)) {
3622 /* Window is constant, pure forward advance.
3623 * No more checks are required.
3624 * Note, we use the fact that SND.UNA>=SND.WL2.
3625 */
3626 tcp_update_wl(tp, ack_seq);
3627 tcp_snd_una_update(tp, ack);
3628 flag |= FLAG_WIN_UPDATE;
3629
3630 tcp_in_ack_event(sk, CA_ACK_WIN_UPDATE);
3631
3632 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPHPACKS);
3633 } else {
Florian Westphalc1d2b4c2017-08-30 19:24:57 +02003634 u32 ack_ev_flags = CA_ACK_SLOWPATH;
Florian Westphal98900922014-09-26 22:37:35 +02003635
Florian Westphalc1d2b4c2017-08-30 19:24:57 +02003636 if (ack_seq != TCP_SKB_CB(skb)->end_seq)
3637 flag |= FLAG_DATA;
3638 else
3639 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPUREACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Florian Westphalc1d2b4c2017-08-30 19:24:57 +02003641 flag |= tcp_ack_update_window(sk, skb, ack, ack_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
Florian Westphalc1d2b4c2017-08-30 19:24:57 +02003643 if (TCP_SKB_CB(skb)->sacked)
3644 flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una,
3645 &sack_state);
3646
3647 if (tcp_ecn_rcv_ecn_echo(tp, tcp_hdr(skb))) {
3648 flag |= FLAG_ECE;
3649 ack_ev_flags |= CA_ACK_ECE;
3650 }
3651
3652 if (flag & FLAG_WIN_UPDATE)
3653 ack_ev_flags |= CA_ACK_WIN_UPDATE;
3654
3655 tcp_in_ack_event(sk, ack_ev_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 }
3657
3658 /* We passed data and got it acked, remove any soft error
3659 * log. Something worked...
3660 */
3661 sk->sk_err_soft = 0;
David S. Miller4b53fb62008-07-23 16:38:45 -07003662 icsk->icsk_probes_out = 0;
Eric Dumazet70eabf02017-05-16 14:00:07 -07003663 tp->rcv_tstamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 if (!prior_packets)
3665 goto no_queue;
3666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 /* See if we can take anything off of the retransmit queue. */
Yuchung Cheng3ebd88712016-02-02 10:33:07 -08003668 flag |= tcp_clean_rtx_queue(sk, prior_fackets, prior_snd_una, &acked,
Yuchung Chengdeed7be2017-01-12 22:11:32 -08003669 &sack_state);
Nandita Dukkipatia262f0c2011-08-21 20:21:57 +00003670
Neal Cardwelldf92c832017-08-03 09:19:54 -04003671 if (tp->tlp_high_seq)
3672 tcp_process_tlp_ack(sk, ack, flag);
3673 /* If needed, reset TLP/RTO timer; RACK may later override this. */
3674 if (flag & FLAG_SET_XMIT_TIMER)
3675 tcp_set_xmit_timer(sk);
3676
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03003677 if (tcp_ack_is_dubious(sk, flag)) {
Neal Cardwell7d2b55f2011-11-16 08:58:01 +00003678 is_dupack = !(flag & (FLAG_SND_UNA_ADVANCED | FLAG_NOT_DUP));
Eric Dumazet1317a9d2017-04-25 10:15:36 -07003679 tcp_fastretrans_alert(sk, acked, is_dupack, &flag, &rexmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 }
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003681
Julian Anastasovc3a2e832017-02-06 23:14:14 +02003682 if ((flag & FLAG_FORWARD_PROGRESS) || !(flag & FLAG_NOT_DUP))
3683 sk_dst_confirm(sk);
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00003684
Yuchung Chengb9f64822016-09-19 23:39:14 -04003685 delivered = tp->delivered - delivered; /* freshly ACKed or SACKed */
3686 lost = tp->lost - lost; /* freshly marked lost */
Eric Dumazet88d5c652017-04-25 10:15:37 -07003687 tcp_rate_gen(sk, delivered, lost, sack_state.rate);
Yuchung Chengdeed7be2017-01-12 22:11:32 -08003688 tcp_cong_control(sk, ack, delivered, flag, sack_state.rate);
Yuchung Chenge662ca42016-02-02 10:33:04 -08003689 tcp_xmit_recovery(sk, rexmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 return 1;
3691
3692no_queue:
Neal Cardwell5628adf2011-11-16 08:58:02 +00003693 /* If data was DSACKed, see if we can undo a cwnd reduction. */
3694 if (flag & FLAG_DSACKING_ACK)
Eric Dumazet1317a9d2017-04-25 10:15:36 -07003695 tcp_fastretrans_alert(sk, acked, is_dupack, &flag, &rexmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 /* If this ack opens up a zero window, clear backoff. It was
3697 * being used to time the probes, and is probably far higher than
3698 * it needs to be for normal retransmission.
3699 */
Eric Dumazet75c119a2017-10-05 22:21:27 -07003700 tcp_ack_probe(sk);
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003701
3702 if (tp->tlp_high_seq)
3703 tcp_process_tlp_ack(sk, ack, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 return 1;
3705
John Dykstra96e0bf42009-03-22 21:49:57 -07003706invalid_ack:
3707 SOCK_DEBUG(sk, "Ack %u after %u:%u\n", ack, tp->snd_una, tp->snd_nxt);
3708 return -1;
3709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710old_ack:
Neal Cardwelle95ae2f2011-11-16 08:58:03 +00003711 /* If data was SACKed, tag it and see if we should send more data.
3712 * If data was DSACKed, see if we can undo a cwnd reduction.
3713 */
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07003714 if (TCP_SKB_CB(skb)->sacked) {
Yuchung Cheng59c9af42013-07-22 16:20:47 -07003715 flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02003716 &sack_state);
Eric Dumazet1317a9d2017-04-25 10:15:36 -07003717 tcp_fastretrans_alert(sk, acked, is_dupack, &flag, &rexmit);
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
3931 /* If the TCP option is too short, we can short cut */
3932 if (length < TCPOLEN_MD5SIG)
3933 return NULL;
3934
3935 while (length > 0) {
3936 int opcode = *ptr++;
3937 int opsize;
3938
Weilong Chena22318e2013-12-23 14:37:26 +08003939 switch (opcode) {
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003940 case TCPOPT_EOL:
3941 return NULL;
3942 case TCPOPT_NOP:
3943 length--;
3944 continue;
3945 default:
3946 opsize = *ptr++;
3947 if (opsize < 2 || opsize > length)
3948 return NULL;
3949 if (opcode == TCPOPT_MD5SIG)
Dmitry Popovba78e2d2010-08-07 20:24:28 -07003950 return opsize == TCPOLEN_MD5SIG ? ptr : NULL;
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003951 }
3952 ptr += opsize - 2;
3953 length -= opsize;
3954 }
3955 return NULL;
3956}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00003957EXPORT_SYMBOL(tcp_parse_md5sig_option);
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003958#endif
3959
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960/* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM
3961 *
3962 * It is not fatal. If this ACK does _not_ change critical state (seqs, window)
3963 * it can pass through stack. So, the following predicate verifies that
3964 * this segment is not used for anything but congestion avoidance or
3965 * fast retransmit. Moreover, we even are able to eliminate most of such
3966 * second order effects, if we apply some small "replay" window (~RTO)
3967 * to timestamp space.
3968 *
3969 * All these measures still do not guarantee that we reject wrapped ACKs
3970 * on networks with high bandwidth, when sequence space is recycled fastly,
3971 * but it guarantees that such events will be very rare and do not affect
3972 * connection seriously. This doesn't look nice, but alas, PAWS is really
3973 * buggy extension.
3974 *
3975 * [ Later note. Even worse! It is buggy for segments _with_ data. RFC
3976 * states that events when retransmit arrives after original data are rare.
3977 * It is a blatant lie. VJ forgot about fast retransmit! 8)8) It is
3978 * the biggest problem on large power networks even with minor reordering.
3979 * OK, let's give it small replay window. If peer clock is even 1hz, it is safe
3980 * up to bandwidth of 18Gigabit/sec. 8) ]
3981 */
3982
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003983static int tcp_disordered_ack(const struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003985 const struct tcp_sock *tp = tcp_sk(sk);
3986 const struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 u32 seq = TCP_SKB_CB(skb)->seq;
3988 u32 ack = TCP_SKB_CB(skb)->ack_seq;
3989
3990 return (/* 1. Pure ACK with correct sequence number. */
3991 (th->ack && seq == TCP_SKB_CB(skb)->end_seq && seq == tp->rcv_nxt) &&
3992
3993 /* 2. ... and duplicate ACK. */
3994 ack == tp->snd_una &&
3995
3996 /* 3. ... and does not update window. */
3997 !tcp_may_update_window(tp, ack, seq, ntohs(th->window) << tp->rx_opt.snd_wscale) &&
3998
3999 /* 4. ... and sits in replay window. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004000 (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 -07004001}
4002
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00004003static inline bool tcp_paws_discard(const struct sock *sk,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004004 const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004006 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinenc887e6d2009-03-14 14:23:03 +00004007
4008 return !tcp_paws_check(&tp->rx_opt, TCP_PAWS_WINDOW) &&
4009 !tcp_disordered_ack(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010}
4011
4012/* Check segment sequence number for validity.
4013 *
4014 * Segment controls are considered valid, if the segment
4015 * fits to the window after truncation to the window. Acceptability
4016 * of data (and SYN, FIN, of course) is checked separately.
4017 * See tcp_data_queue(), for example.
4018 *
4019 * Also, controls (RST is main one) are accepted using RCV.WUP instead
4020 * of RCV.NXT. Peer still did not advance his SND.UNA when we
4021 * delayed ACK, so that hisSND.UNA<=ourRCV.WUP.
4022 * (borrowed from freebsd)
4023 */
4024
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00004025static inline bool tcp_sequence(const struct tcp_sock *tp, u32 seq, u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
4027 return !before(end_seq, tp->rcv_wup) &&
4028 !after(seq, tp->rcv_nxt + tcp_receive_window(tp));
4029}
4030
4031/* When we get a reset we do this. */
Jerry Chu10467162012-08-31 12:29:11 +00004032void tcp_reset(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
Song Liu59415212017-10-23 09:20:25 -07004034 trace_tcp_receive_reset(sk);
4035
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 /* We want the right error as BSD sees it (and indeed as we do). */
4037 switch (sk->sk_state) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004038 case TCP_SYN_SENT:
4039 sk->sk_err = ECONNREFUSED;
4040 break;
4041 case TCP_CLOSE_WAIT:
4042 sk->sk_err = EPIPE;
4043 break;
4044 case TCP_CLOSE:
4045 return;
4046 default:
4047 sk->sk_err = ECONNRESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 }
Tom Marshalla4d25802010-09-20 15:42:05 -07004049 /* This barrier is coupled with smp_rmb() in tcp_poll() */
4050 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Eric Dumazet3d476262017-04-18 09:45:51 -07004052 tcp_done(sk);
4053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 if (!sock_flag(sk, SOCK_DEAD))
4055 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056}
4057
4058/*
4059 * Process the FIN bit. This now behaves as it is supposed to work
4060 * and the FIN takes effect when it is validly part of sequence
4061 * space. Not before when we get holes.
4062 *
4063 * If we are ESTABLISHED, a received fin moves us to CLOSE-WAIT
4064 * (and thence onto LAST-ACK and finally, CLOSE, we never enter
4065 * TIME-WAIT)
4066 *
4067 * If we are in FINWAIT-1, a received FIN indicates simultaneous
4068 * close and we go into CLOSING (and later onto TIME-WAIT)
4069 *
4070 * If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT.
4071 */
Eric Dumazete3e17b72016-02-06 11:16:28 -08004072void tcp_fin(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073{
4074 struct tcp_sock *tp = tcp_sk(sk);
4075
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004076 inet_csk_schedule_ack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
4078 sk->sk_shutdown |= RCV_SHUTDOWN;
4079 sock_set_flag(sk, SOCK_DONE);
4080
4081 switch (sk->sk_state) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004082 case TCP_SYN_RECV:
4083 case TCP_ESTABLISHED:
4084 /* Move to CLOSE_WAIT */
4085 tcp_set_state(sk, TCP_CLOSE_WAIT);
Jon Maxwell2251ae42015-07-08 10:12:28 +10004086 inet_csk(sk)->icsk_ack.pingpong = 1;
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004087 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004089 case TCP_CLOSE_WAIT:
4090 case TCP_CLOSING:
4091 /* Received a retransmission of the FIN, do
4092 * nothing.
4093 */
4094 break;
4095 case TCP_LAST_ACK:
4096 /* RFC793: Remain in the LAST-ACK state. */
4097 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004099 case TCP_FIN_WAIT1:
4100 /* This case occurs when a simultaneous close
4101 * happens, we must ack the received FIN and
4102 * enter the CLOSING state.
4103 */
4104 tcp_send_ack(sk);
4105 tcp_set_state(sk, TCP_CLOSING);
4106 break;
4107 case TCP_FIN_WAIT2:
4108 /* Received a FIN -- send ACK and enter TIME_WAIT. */
4109 tcp_send_ack(sk);
4110 tcp_time_wait(sk, TCP_TIME_WAIT, 0);
4111 break;
4112 default:
4113 /* Only TCP_LISTEN and TCP_CLOSE are left, in these
4114 * cases we should never reach this piece of code.
4115 */
Joe Perches058bd4d2012-03-11 18:36:11 +00004116 pr_err("%s: Impossible, sk->sk_state=%d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08004117 __func__, sk->sk_state);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004118 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
4121 /* It _is_ possible, that we have something out-of-order _after_ FIN.
4122 * Probably, we should reset in this case. For now drop them.
4123 */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004124 skb_rbtree_purge(&tp->out_of_order_queue);
Ilpo Järvinene60402d2007-08-09 15:14:46 +03004125 if (tcp_is_sack(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 tcp_sack_reset(&tp->rx_opt);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08004127 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
4129 if (!sock_flag(sk, SOCK_DEAD)) {
4130 sk->sk_state_change(sk);
4131
4132 /* Do not send POLL_HUP for half duplex close. */
4133 if (sk->sk_shutdown == SHUTDOWN_MASK ||
4134 sk->sk_state == TCP_CLOSE)
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08004135 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 else
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08004137 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 }
4139}
4140
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004141static inline bool tcp_sack_extend(struct tcp_sack_block *sp, u32 seq,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004142 u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
4144 if (!after(seq, sp->end_seq) && !after(sp->start_seq, end_seq)) {
4145 if (before(seq, sp->start_seq))
4146 sp->start_seq = seq;
4147 if (after(end_seq, sp->end_seq))
4148 sp->end_seq = end_seq;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004149 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004151 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152}
4153
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004154static void tcp_dsack_set(struct sock *sk, u32 seq, u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155{
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004156 struct tcp_sock *tp = tcp_sk(sk);
4157
David S. Millerbb5b7c12009-12-15 20:56:42 -08004158 if (tcp_is_sack(tp) && sysctl_tcp_dsack) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07004159 int mib_idx;
4160
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 if (before(seq, tp->rcv_nxt))
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07004162 mib_idx = LINUX_MIB_TCPDSACKOLDSENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 else
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07004164 mib_idx = LINUX_MIB_TCPDSACKOFOSENT;
4165
Eric Dumazetc10d9312016-04-29 14:16:47 -07004166 NET_INC_STATS(sock_net(sk), mib_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
4168 tp->rx_opt.dsack = 1;
4169 tp->duplicate_sack[0].start_seq = seq;
4170 tp->duplicate_sack[0].end_seq = end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 }
4172}
4173
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004174static void tcp_dsack_extend(struct sock *sk, u32 seq, u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175{
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004176 struct tcp_sock *tp = tcp_sk(sk);
4177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 if (!tp->rx_opt.dsack)
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004179 tcp_dsack_set(sk, seq, end_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 else
4181 tcp_sack_extend(tp->duplicate_sack, seq, end_seq);
4182}
4183
Eric Dumazetcf533ea2011-10-21 05:22:42 -04004184static void tcp_send_dupack(struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185{
4186 struct tcp_sock *tp = tcp_sk(sk);
4187
4188 if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
4189 before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07004190 NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004191 tcp_enter_quickack_mode(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
David S. Millerbb5b7c12009-12-15 20:56:42 -08004193 if (tcp_is_sack(tp) && sysctl_tcp_dsack) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 u32 end_seq = TCP_SKB_CB(skb)->end_seq;
4195
4196 if (after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))
4197 end_seq = tp->rcv_nxt;
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004198 tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, end_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 }
4200 }
4201
4202 tcp_send_ack(sk);
4203}
4204
4205/* These routines update the SACK block as out-of-order packets arrive or
4206 * in-order packets close up the sequence space.
4207 */
4208static void tcp_sack_maybe_coalesce(struct tcp_sock *tp)
4209{
4210 int this_sack;
4211 struct tcp_sack_block *sp = &tp->selective_acks[0];
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004212 struct tcp_sack_block *swalk = sp + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
4214 /* See if the recent change to the first SACK eats into
4215 * or hits the sequence space of other SACK blocks, if so coalesce.
4216 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004217 for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 if (tcp_sack_extend(sp, swalk->start_seq, swalk->end_seq)) {
4219 int i;
4220
4221 /* Zap SWALK, by moving every further SACK up by one slot.
4222 * Decrease num_sacks.
4223 */
4224 tp->rx_opt.num_sacks--;
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004225 for (i = this_sack; i < tp->rx_opt.num_sacks; i++)
4226 sp[i] = sp[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 continue;
4228 }
4229 this_sack++, swalk++;
4230 }
4231}
4232
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233static void tcp_sack_new_ofo_skb(struct sock *sk, u32 seq, u32 end_seq)
4234{
4235 struct tcp_sock *tp = tcp_sk(sk);
4236 struct tcp_sack_block *sp = &tp->selective_acks[0];
4237 int cur_sacks = tp->rx_opt.num_sacks;
4238 int this_sack;
4239
4240 if (!cur_sacks)
4241 goto new_sack;
4242
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004243 for (this_sack = 0; this_sack < cur_sacks; this_sack++, sp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 if (tcp_sack_extend(sp, seq, end_seq)) {
4245 /* Rotate this_sack to the first one. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004246 for (; this_sack > 0; this_sack--, sp--)
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07004247 swap(*sp, *(sp - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 if (cur_sacks > 1)
4249 tcp_sack_maybe_coalesce(tp);
4250 return;
4251 }
4252 }
4253
4254 /* Could not find an adjacent existing SACK, build a new one,
4255 * put it at the front, and shift everyone else down. We
4256 * always know there is at least one SACK present already here.
4257 *
4258 * If the sack array is full, forget about the last one.
4259 */
Adam Langley4389dde2008-07-19 00:07:02 -07004260 if (this_sack >= TCP_NUM_SACKS) {
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
Florian Westphal735d3832014-09-29 13:08:30 +02004442 tcp_ecn_check_ce(tp, 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
4639{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet5357f0b2017-08-01 07:02:44 -07004641 bool fragstolen;
4642 int eaten;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643
Eric Dumazet532182c2016-04-01 08:52:19 -07004644 if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
4645 __kfree_skb(skb);
4646 return;
4647 }
Eric Dumazetf84af322010-04-28 15:31:51 -07004648 skb_dst_drop(skb);
Peter Pan(潘卫平)155c6e12014-09-24 22:17:02 +08004649 __skb_pull(skb, tcp_hdr(skb)->doff * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
Florian Westphal735d3832014-09-29 13:08:30 +02004651 tcp_ecn_accept_cwr(tp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
Ilpo Järvinen5861f8e2009-03-14 14:23:01 +00004653 tp->rx_opt.dsack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
4655 /* Queue data for delivery to the user.
4656 * Packets in sequence go to the receive queue.
4657 * Out of sequence packets to the out_of_order_queue.
4658 */
4659 if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {
4660 if (tcp_receive_window(tp) == 0)
4661 goto out_of_window;
4662
4663 /* Ok. In sequence. In window. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664queue_and_out:
Eric Dumazet5357f0b2017-08-01 07:02:44 -07004665 if (skb_queue_len(&sk->sk_receive_queue) == 0)
4666 sk_forced_mem_schedule(sk, skb->truesize);
4667 else if (tcp_try_rmem_schedule(sk, skb, skb->truesize))
4668 goto drop;
4669
Florian Westphalc13ee2a2017-07-30 03:57:19 +02004670 eaten = tcp_queue_rcv(sk, skb, 0, &fragstolen);
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07004671 tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
Stephen Hemminger2de979b2007-03-08 20:45:19 -08004672 if (skb->len)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07004673 tcp_event_data_recv(sk, skb);
Peter Pan(潘卫平)155c6e12014-09-24 22:17:02 +08004674 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
Eric Dumazet20c4cb72011-10-20 17:44:03 -04004675 tcp_fin(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004677 if (!RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 tcp_ofo_queue(sk);
4679
4680 /* RFC2581. 4.2. SHOULD send immediate ACK, when
4681 * gap in queue is filled.
4682 */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004683 if (RB_EMPTY_ROOT(&tp->out_of_order_queue))
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004684 inet_csk(sk)->icsk_ack.pingpong = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 }
4686
4687 if (tp->rx_opt.num_sacks)
4688 tcp_sack_remove(tp);
4689
Florian Westphal31770e32017-08-30 19:24:58 +02004690 tcp_fast_path_check(sk);
4691
Eric Dumazet923dd342012-05-02 07:55:58 +00004692 if (eaten > 0)
4693 kfree_skb_partial(skb, fragstolen);
Eric Dumazet1d57f192012-09-17 12:51:39 +00004694 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04004695 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 return;
4697 }
4698
4699 if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
4700 /* A retransmit, 2nd most common case. Force an immediate ack. */
Eric Dumazetc10d9312016-04-29 14:16:47 -07004701 NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004702 tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
4704out_of_window:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004705 tcp_enter_quickack_mode(sk);
4706 inet_csk_schedule_ack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707drop:
Eric Dumazet532182c2016-04-01 08:52:19 -07004708 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 return;
4710 }
4711
4712 /* Out of window. F.e. zero window probe. */
4713 if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt + tcp_receive_window(tp)))
4714 goto out_of_window;
4715
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004716 tcp_enter_quickack_mode(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
4718 if (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
4719 /* Partial packet, seq < rcv_next < end_seq */
4720 SOCK_DEBUG(sk, "partial packet: rcv_next %X seq %X - %X\n",
4721 tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
4722 TCP_SKB_CB(skb)->end_seq);
4723
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004724 tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, tp->rcv_nxt);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09004725
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 /* If window is closed, drop tail of packet. But after
4727 * remembering D-SACK for its head made in previous line.
4728 */
4729 if (!tcp_receive_window(tp))
4730 goto out_of_window;
4731 goto queue_and_out;
4732 }
4733
Eric Dumazete86b29192012-03-18 11:06:44 +00004734 tcp_data_queue_ofo(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735}
4736
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004737static struct sk_buff *tcp_skb_next(struct sk_buff *skb, struct sk_buff_head *list)
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004738{
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004739 if (list)
4740 return !skb_queue_is_last(list, skb) ? skb->next : NULL;
David S. Miller91521942009-05-28 21:35:47 -07004741
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004742 return skb_rb_next(skb);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004743}
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004744
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004745static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
4746 struct sk_buff_head *list,
4747 struct rb_root *root)
4748{
4749 struct sk_buff *next = tcp_skb_next(skb, list);
4750
4751 if (list)
4752 __skb_unlink(skb, list);
4753 else
4754 rb_erase(&skb->rbnode, root);
4755
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004756 __kfree_skb(skb);
Eric Dumazetc10d9312016-04-29 14:16:47 -07004757 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVCOLLAPSED);
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004758
4759 return next;
4760}
4761
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004762/* Insert skb into rb tree, ordered by TCP_SKB_CB(skb)->seq */
Eric Dumazet75c119a2017-10-05 22:21:27 -07004763void tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004764{
4765 struct rb_node **p = &root->rb_node;
4766 struct rb_node *parent = NULL;
4767 struct sk_buff *skb1;
4768
4769 while (*p) {
4770 parent = *p;
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004771 skb1 = rb_to_skb(parent);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004772 if (before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb1)->seq))
4773 p = &parent->rb_left;
4774 else
4775 p = &parent->rb_right;
4776 }
4777 rb_link_node(&skb->rbnode, parent, p);
4778 rb_insert_color(&skb->rbnode, root);
4779}
4780
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781/* Collapse contiguous sequence of skbs head..tail with
4782 * sequence numbers start..end.
David S. Miller91521942009-05-28 21:35:47 -07004783 *
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004784 * If tail is NULL, this means until the end of the queue.
David S. Miller91521942009-05-28 21:35:47 -07004785 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 * Segments with FIN/SYN are not collapsed (only because this
4787 * simplifies code)
4788 */
4789static void
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004790tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
4791 struct sk_buff *head, struct sk_buff *tail, u32 start, u32 end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792{
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004793 struct sk_buff *skb = head, *n;
4794 struct sk_buff_head tmp;
David S. Miller91521942009-05-28 21:35:47 -07004795 bool end_of_skbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08004797 /* First, check that queue is collapsible and find
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004798 * the point where collapsing can be useful.
4799 */
David S. Miller91521942009-05-28 21:35:47 -07004800restart:
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004801 for (end_of_skbs = true; skb != NULL && skb != tail; skb = n) {
4802 n = tcp_skb_next(skb, list);
4803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 /* No new bits? It is possible on ofo queue. */
4805 if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004806 skb = tcp_collapse_one(sk, skb, list, root);
David S. Miller91521942009-05-28 21:35:47 -07004807 if (!skb)
4808 break;
4809 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 }
4811
4812 /* The first skb to collapse is:
4813 * - not SYN/FIN and
4814 * - bloated or contains data before "start" or
4815 * overlaps to the next one.
4816 */
Eric Dumazete11ecdd2014-09-15 04:19:51 -07004817 if (!(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 (tcp_win_from_space(skb->truesize) > skb->len ||
David S. Miller91521942009-05-28 21:35:47 -07004819 before(TCP_SKB_CB(skb)->seq, start))) {
4820 end_of_skbs = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 break;
David S. Miller91521942009-05-28 21:35:47 -07004822 }
4823
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004824 if (n && n != tail &&
4825 TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(n)->seq) {
4826 end_of_skbs = false;
4827 break;
David S. Miller91521942009-05-28 21:35:47 -07004828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
4830 /* Decided to skip this, advance start seq. */
4831 start = TCP_SKB_CB(skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 }
Eric Dumazete11ecdd2014-09-15 04:19:51 -07004833 if (end_of_skbs ||
4834 (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 return;
4836
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004837 __skb_queue_head_init(&tmp);
4838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 while (before(start, end)) {
Eric Dumazetb3d6cb92014-09-15 04:19:53 -07004840 int copy = min_t(int, SKB_MAX_ORDER(0, 0), end - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 struct sk_buff *nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
Eric Dumazetb3d6cb92014-09-15 04:19:53 -07004843 nskb = alloc_skb(copy, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 if (!nskb)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004845 break;
Arnaldo Carvalho de Meloc51957d2007-03-10 12:47:22 -03004846
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
4848 TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004849 if (list)
4850 __skb_queue_before(list, skb, nskb);
4851 else
4852 __skb_queue_tail(&tmp, nskb); /* defer rbtree insertion */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08004853 skb_set_owner_r(nskb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
4855 /* Copy data, releasing collapsed skbs. */
4856 while (copy > 0) {
4857 int offset = start - TCP_SKB_CB(skb)->seq;
4858 int size = TCP_SKB_CB(skb)->end_seq - start;
4859
Kris Katterjohn09a62662006-01-08 22:24:28 -08004860 BUG_ON(offset < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 if (size > 0) {
4862 size = min(copy, size);
4863 if (skb_copy_bits(skb, offset, skb_put(nskb, size), size))
4864 BUG();
4865 TCP_SKB_CB(nskb)->end_seq += size;
4866 copy -= size;
4867 start += size;
4868 }
4869 if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004870 skb = tcp_collapse_one(sk, skb, list, root);
David S. Miller91521942009-05-28 21:35:47 -07004871 if (!skb ||
4872 skb == tail ||
Eric Dumazete11ecdd2014-09-15 04:19:51 -07004873 (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)))
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004874 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 }
4876 }
4877 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004878end:
4879 skb_queue_walk_safe(&tmp, skb, n)
4880 tcp_rbtree_insert(root, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881}
4882
4883/* Collapse ofo queue. Algorithm: select contiguous sequence of skbs
4884 * and tcp_collapse() them until all the queue is collapsed.
4885 */
4886static void tcp_collapse_ofo_queue(struct sock *sk)
4887{
4888 struct tcp_sock *tp = tcp_sk(sk);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004889 struct sk_buff *skb, *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 u32 start, end;
4891
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004892 skb = skb_rb_first(&tp->out_of_order_queue);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004893new_range:
4894 if (!skb) {
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004895 tp->ooo_last_skb = skb_rb_last(&tp->out_of_order_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 return;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 start = TCP_SKB_CB(skb)->seq;
4899 end = TCP_SKB_CB(skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004901 for (head = skb;;) {
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004902 skb = skb_rb_next(skb);
David S. Miller91521942009-05-28 21:35:47 -07004903
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004904 /* Range is terminated when we see a gap or when
4905 * we are at the queue end.
4906 */
David S. Miller91521942009-05-28 21:35:47 -07004907 if (!skb ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 after(TCP_SKB_CB(skb)->seq, end) ||
4909 before(TCP_SKB_CB(skb)->end_seq, start)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004910 tcp_collapse(sk, NULL, &tp->out_of_order_queue,
David S. Miller8728b832005-08-09 19:25:21 -07004911 head, skb, start, end);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004912 goto new_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004914
4915 if (unlikely(before(TCP_SKB_CB(skb)->seq, start)))
4916 start = TCP_SKB_CB(skb)->seq;
4917 if (after(TCP_SKB_CB(skb)->end_seq, end))
4918 end = TCP_SKB_CB(skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 }
4920}
4921
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004922/*
Eric Dumazet36a65032016-08-17 14:17:09 -07004923 * Clean the out-of-order queue to make room.
4924 * We drop high sequences packets to :
4925 * 1) Let a chance for holes to be filled.
4926 * 2) not add too big latencies if thousands of packets sit there.
4927 * (But if application shrinks SO_RCVBUF, we could still end up
4928 * freeing whole queue here)
4929 *
4930 * Return true if queue has shrunk.
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004931 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004932static bool tcp_prune_ofo_queue(struct sock *sk)
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004933{
4934 struct tcp_sock *tp = tcp_sk(sk);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004935 struct rb_node *node, *prev;
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004936
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004937 if (RB_EMPTY_ROOT(&tp->out_of_order_queue))
Eric Dumazet36a65032016-08-17 14:17:09 -07004938 return false;
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004939
Eric Dumazet36a65032016-08-17 14:17:09 -07004940 NET_INC_STATS(sock_net(sk), LINUX_MIB_OFOPRUNED);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004941 node = &tp->ooo_last_skb->rbnode;
4942 do {
4943 prev = rb_prev(node);
4944 rb_erase(node, &tp->out_of_order_queue);
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004945 tcp_drop(sk, rb_to_skb(node));
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004946 sk_mem_reclaim(sk);
Eric Dumazet36a65032016-08-17 14:17:09 -07004947 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
4948 !tcp_under_memory_pressure(sk))
4949 break;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004950 node = prev;
4951 } while (node);
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004952 tp->ooo_last_skb = rb_to_skb(prev);
Eric Dumazet36a65032016-08-17 14:17:09 -07004953
4954 /* Reset SACK state. A conforming SACK implementation will
4955 * do the same at a timeout based retransmit. When a connection
4956 * is in a sad state like this, we care only about integrity
4957 * of the connection not performance.
4958 */
4959 if (tp->rx_opt.sack_ok)
4960 tcp_sack_reset(&tp->rx_opt);
4961 return true;
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004962}
4963
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964/* Reduce allocated memory if we can, trying to get
4965 * the socket within its memory limits again.
4966 *
4967 * Return less than zero if we should start dropping frames
4968 * until the socket owning process reads some of the data
4969 * to stabilize the situation.
4970 */
4971static int tcp_prune_queue(struct sock *sk)
4972{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09004973 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
4975 SOCK_DEBUG(sk, "prune_queue: c=%x\n", tp->copied_seq);
4976
Eric Dumazetc10d9312016-04-29 14:16:47 -07004977 NET_INC_STATS(sock_net(sk), LINUX_MIB_PRUNECALLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
4979 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07004980 tcp_clamp_window(sk);
Eric Dumazetb8da51e2015-05-15 12:39:27 -07004981 else if (tcp_under_memory_pressure(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss);
4983
4984 tcp_collapse_ofo_queue(sk);
David S. Miller91521942009-05-28 21:35:47 -07004985 if (!skb_queue_empty(&sk->sk_receive_queue))
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004986 tcp_collapse(sk, &sk->sk_receive_queue, NULL,
David S. Miller91521942009-05-28 21:35:47 -07004987 skb_peek(&sk->sk_receive_queue),
4988 NULL,
4989 tp->copied_seq, tp->rcv_nxt);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08004990 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
4992 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
4993 return 0;
4994
4995 /* Collapsing did not help, destructive actions follow.
4996 * This must not ever occur. */
4997
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004998 tcp_prune_ofo_queue(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999
5000 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
5001 return 0;
5002
5003 /* If we are really being abused, tell the caller to silently
5004 * drop receive data on the floor. It will get retransmitted
5005 * and hopefully then we'll have sufficient space.
5006 */
Eric Dumazetc10d9312016-04-29 14:16:47 -07005007 NET_INC_STATS(sock_net(sk), LINUX_MIB_RCVPRUNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
5009 /* Massive buffer overcommit. */
Florian Westphal31770e32017-08-30 19:24:58 +02005010 tp->pred_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 return -1;
5012}
5013
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005014static bool tcp_should_expand_sndbuf(const struct sock *sk)
David S. Miller0d9901d2005-07-05 15:21:10 -07005015{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04005016 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005017
David S. Miller0d9901d2005-07-05 15:21:10 -07005018 /* If the user specified a specific send buffer setting, do
5019 * not modify it.
5020 */
5021 if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005022 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005023
5024 /* If we are under global TCP memory pressure, do not expand. */
Eric Dumazetb8da51e2015-05-15 12:39:27 -07005025 if (tcp_under_memory_pressure(sk))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005026 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005027
5028 /* If we are under soft global TCP memory pressure, do not expand. */
Glauber Costa180d8cd2011-12-11 21:47:02 +00005029 if (sk_memory_allocated(sk) >= sk_prot_mem_limits(sk, 0))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005030 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005031
5032 /* If we filled the congestion window, do not expand. */
Neal Cardwell65148902015-02-20 13:33:16 -05005033 if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005034 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005035
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005036 return true;
David S. Miller0d9901d2005-07-05 15:21:10 -07005037}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038
5039/* When incoming ACK allowed to free some skb from write_queue,
5040 * we remember this event in flag SOCK_QUEUE_SHRUNK and wake up socket
5041 * on the exit from tcp input handler.
5042 *
5043 * PROBLEM: sndbuf expansion does not work well with largesend.
5044 */
5045static void tcp_new_space(struct sock *sk)
5046{
5047 struct tcp_sock *tp = tcp_sk(sk);
5048
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005049 if (tcp_should_expand_sndbuf(sk)) {
Eric Dumazet6ae70532013-10-01 10:23:44 -07005050 tcp_sndbuf_expand(sk);
Eric Dumazetc2203cf2017-05-16 14:00:04 -07005051 tp->snd_cwnd_stamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 }
5053
5054 sk->sk_write_space(sk);
5055}
5056
Stephen Hemminger40efc6f2006-01-03 16:03:49 -08005057static void tcp_check_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058{
5059 if (sock_flag(sk, SOCK_QUEUE_SHRUNK)) {
5060 sock_reset_flag(sk, SOCK_QUEUE_SHRUNK);
jbaron@akamai.com3c715122015-04-20 20:05:07 +00005061 /* pairs with tcp_poll() */
Jason Baron56d80622017-01-24 21:49:41 -05005062 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 if (sk->sk_socket &&
Francis Yanb0f71bd2016-11-27 23:07:16 -08005064 test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 tcp_new_space(sk);
Francis Yanb0f71bd2016-11-27 23:07:16 -08005066 if (!test_bit(SOCK_NOSPACE, &sk->sk_socket->flags))
5067 tcp_chrono_stop(sk, TCP_CHRONO_SNDBUF_LIMITED);
5068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 }
5070}
5071
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005072static inline void tcp_data_snd_check(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005074 tcp_push_pending_frames(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 tcp_check_space(sk);
5076}
5077
5078/*
5079 * Check if sending an ack is needed.
5080 */
5081static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible)
5082{
5083 struct tcp_sock *tp = tcp_sk(sk);
5084
5085 /* More than one full frame received... */
Joe Perches9d4fb272009-11-23 10:41:23 -08005086 if (((tp->rcv_nxt - tp->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 /* ... and right edge of window advances far enough.
5088 * (tcp_recvmsg() will send ACK otherwise). Or...
5089 */
Joe Perches9d4fb272009-11-23 10:41:23 -08005090 __tcp_select_window(sk) >= tp->rcv_wnd) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 /* We ACK each frame or... */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005092 tcp_in_quickack_mode(sk) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 /* We have out of order data. */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07005094 (ofo_possible && !RB_EMPTY_ROOT(&tp->out_of_order_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 /* Then ack it now */
5096 tcp_send_ack(sk);
5097 } else {
5098 /* Else, send delayed ack. */
5099 tcp_send_delayed_ack(sk);
5100 }
5101}
5102
Stephen Hemminger40efc6f2006-01-03 16:03:49 -08005103static inline void tcp_ack_snd_check(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005105 if (!inet_csk_ack_scheduled(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 /* We sent a data segment already. */
5107 return;
5108 }
5109 __tcp_ack_snd_check(sk, 1);
5110}
5111
5112/*
5113 * This routine is only called when we have urgent data
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08005114 * signaled. Its the 'slow' part of tcp_urg. It could be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 * moved inline now as tcp_urg is only called from one
5116 * place. We handle URGent data wrong. We have to - as
5117 * BSD still doesn't use the correction from RFC961.
5118 * For 1003.1g we should support a new option TCP_STDURG to permit
5119 * either form (or just set the sysctl tcp_stdurg).
5120 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005121
Eric Dumazetcf533ea2011-10-21 05:22:42 -04005122static void tcp_check_urg(struct sock *sk, const struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123{
5124 struct tcp_sock *tp = tcp_sk(sk);
5125 u32 ptr = ntohs(th->urg_ptr);
5126
5127 if (ptr && !sysctl_tcp_stdurg)
5128 ptr--;
5129 ptr += ntohl(th->seq);
5130
5131 /* Ignore urgent data that we've already seen and read. */
5132 if (after(tp->copied_seq, ptr))
5133 return;
5134
5135 /* Do not replay urg ptr.
5136 *
5137 * NOTE: interesting situation not covered by specs.
5138 * Misbehaving sender may send urg ptr, pointing to segment,
5139 * which we already have in ofo queue. We are not able to fetch
5140 * such data and will stay in TCP_URG_NOTYET until will be eaten
5141 * by recvmsg(). Seems, we are not obliged to handle such wicked
5142 * situations. But it is worth to think about possibility of some
5143 * DoSes using some hypothetical application level deadlock.
5144 */
5145 if (before(ptr, tp->rcv_nxt))
5146 return;
5147
5148 /* Do we already have a newer (or duplicate) urgent pointer? */
5149 if (tp->urg_data && !after(ptr, tp->urg_seq))
5150 return;
5151
5152 /* Tell the world about our new urgent pointer. */
5153 sk_send_sigurg(sk);
5154
5155 /* We may be adding urgent data when the last byte read was
5156 * urgent. To do this requires some care. We cannot just ignore
5157 * tp->copied_seq since we would read the last urgent byte again
5158 * as data, nor can we alter copied_seq until this data arrives
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08005159 * or we break the semantics of SIOCATMARK (and thus sockatmark())
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 *
5161 * NOTE. Double Dutch. Rendering to plain English: author of comment
5162 * above did something sort of send("A", MSG_OOB); send("B", MSG_OOB);
5163 * and expect that both A and B disappear from stream. This is _wrong_.
5164 * Though this happens in BSD with high probability, this is occasional.
5165 * Any application relying on this is buggy. Note also, that fix "works"
5166 * only in this artificial test. Insert some normal data between A and B and we will
5167 * decline of BSD again. Verdict: it is better to remove to trap
5168 * buggy users.
5169 */
5170 if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005171 !sock_flag(sk, SOCK_URGINLINE) && tp->copied_seq != tp->rcv_nxt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
5173 tp->copied_seq++;
5174 if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) {
David S. Miller8728b832005-08-09 19:25:21 -07005175 __skb_unlink(skb, &sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 __kfree_skb(skb);
5177 }
5178 }
5179
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005180 tp->urg_data = TCP_URG_NOTYET;
5181 tp->urg_seq = ptr;
Florian Westphal31770e32017-08-30 19:24:58 +02005182
5183 /* Disable header prediction. */
5184 tp->pred_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185}
5186
5187/* This is the 'fast' part of urgent handling. */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04005188static void tcp_urg(struct sock *sk, struct sk_buff *skb, const struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189{
5190 struct tcp_sock *tp = tcp_sk(sk);
5191
5192 /* Check if we get a new urgent pointer - normally not. */
5193 if (th->urg)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005194 tcp_check_urg(sk, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
5196 /* Do we wait for any urgent data? - normally not... */
5197 if (tp->urg_data == TCP_URG_NOTYET) {
5198 u32 ptr = tp->urg_seq - ntohl(th->seq) + (th->doff * 4) -
5199 th->syn;
5200
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005201 /* Is the urgent pointer pointing into this packet? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 if (ptr < skb->len) {
5203 u8 tmp;
5204 if (skb_copy_bits(skb, ptr, &tmp, 1))
5205 BUG();
5206 tp->urg_data = TCP_URG_VALID | tmp;
5207 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04005208 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 }
5210 }
5211}
5212
Jason Baron0e40f4c2017-01-17 13:37:19 -05005213/* Accept RST for rcv_nxt - 1 after a FIN.
5214 * When tcp connections are abruptly terminated from Mac OSX (via ^C), a
5215 * FIN is sent followed by a RST packet. The RST is sent with the same
5216 * sequence number as the FIN, and thus according to RFC 5961 a challenge
5217 * ACK should be sent. However, Mac OSX rate limits replies to challenge
5218 * ACKs on the closed socket. In addition middleboxes can drop either the
5219 * challenge ACK or a subsequent RST.
5220 */
5221static bool tcp_reset_check(const struct sock *sk, const struct sk_buff *skb)
5222{
5223 struct tcp_sock *tp = tcp_sk(sk);
5224
5225 return unlikely(TCP_SKB_CB(skb)->seq == (tp->rcv_nxt - 1) &&
5226 (1 << sk->sk_state) & (TCPF_CLOSE_WAIT | TCPF_LAST_ACK |
5227 TCPF_CLOSING));
5228}
5229
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005230/* Does PAWS and seqno based validation of an incoming segment, flags will
5231 * play significant role here.
5232 */
Eric Dumazet0c246042012-07-17 01:41:30 +00005233static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,
5234 const struct tcphdr *th, int syn_inerr)
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005235{
5236 struct tcp_sock *tp = tcp_sk(sk);
Pau Espin Pedrole00431b2016-06-07 16:30:34 +02005237 bool rst_seq_match = false;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005238
5239 /* RFC1323: H1. Apply PAWS check first. */
Eric Dumazeteed29f12017-06-07 10:34:36 -07005240 if (tcp_fast_parse_options(sock_net(sk), skb, th, tp) &&
5241 tp->rx_opt.saw_tstamp &&
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005242 tcp_paws_discard(sk, skb)) {
5243 if (!th->rst) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07005244 NET_INC_STATS(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005245 if (!tcp_oow_rate_limited(sock_net(sk), skb,
5246 LINUX_MIB_TCPACKSKIPPEDPAWS,
5247 &tp->last_oow_ack_time))
5248 tcp_send_dupack(sk, skb);
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005249 goto discard;
5250 }
5251 /* Reset is accepted even if it did not pass PAWS. */
5252 }
5253
5254 /* Step 1: check sequence number */
5255 if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) {
5256 /* RFC793, page 37: "In all states except SYN-SENT, all reset
5257 * (RST) segments are validated by checking their SEQ-fields."
5258 * And page 69: "If an incoming segment is not acceptable,
5259 * an acknowledgment should be sent in reply (unless the RST
5260 * bit is set, if so drop the segment and return)".
5261 */
Eric Dumazete3715892012-07-17 12:29:30 +00005262 if (!th->rst) {
5263 if (th->syn)
5264 goto syn_challenge;
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005265 if (!tcp_oow_rate_limited(sock_net(sk), skb,
5266 LINUX_MIB_TCPACKSKIPPEDSEQ,
5267 &tp->last_oow_ack_time))
5268 tcp_send_dupack(sk, skb);
Jason Baron0e40f4c2017-01-17 13:37:19 -05005269 } else if (tcp_reset_check(sk, skb)) {
5270 tcp_reset(sk);
Eric Dumazete3715892012-07-17 12:29:30 +00005271 }
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005272 goto discard;
5273 }
5274
5275 /* Step 2: check RST bit */
5276 if (th->rst) {
Jason Baron0e40f4c2017-01-17 13:37:19 -05005277 /* RFC 5961 3.2 (extend to match against (RCV.NXT - 1) after a
5278 * FIN and SACK too if available):
5279 * If seq num matches RCV.NXT or (RCV.NXT - 1) after a FIN, or
5280 * the right-most SACK block,
Pau Espin Pedrole00431b2016-06-07 16:30:34 +02005281 * then
Eric Dumazet282f23c2012-07-17 10:13:05 +02005282 * RESET the connection
5283 * else
5284 * Send a challenge ACK
5285 */
Jason Baron0e40f4c2017-01-17 13:37:19 -05005286 if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt ||
5287 tcp_reset_check(sk, skb)) {
Pau Espin Pedrole00431b2016-06-07 16:30:34 +02005288 rst_seq_match = true;
5289 } else if (tcp_is_sack(tp) && tp->rx_opt.num_sacks > 0) {
5290 struct tcp_sack_block *sp = &tp->selective_acks[0];
5291 int max_sack = sp[0].end_seq;
5292 int this_sack;
5293
5294 for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;
5295 ++this_sack) {
5296 max_sack = after(sp[this_sack].end_seq,
5297 max_sack) ?
5298 sp[this_sack].end_seq : max_sack;
5299 }
5300
5301 if (TCP_SKB_CB(skb)->seq == max_sack)
5302 rst_seq_match = true;
5303 }
5304
5305 if (rst_seq_match)
Eric Dumazet282f23c2012-07-17 10:13:05 +02005306 tcp_reset(sk);
Wei Wangcf1ef3f2017-04-20 14:45:46 -07005307 else {
5308 /* Disable TFO if RST is out-of-order
5309 * and no data has been received
5310 * for current active TFO socket
5311 */
5312 if (tp->syn_fastopen && !tp->data_segs_in &&
5313 sk->sk_state == TCP_ESTABLISHED)
Wei Wang46c2fa32017-04-20 14:45:47 -07005314 tcp_fastopen_active_disable(sk);
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005315 tcp_send_challenge_ack(sk, skb);
Wei Wangcf1ef3f2017-04-20 14:45:46 -07005316 }
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005317 goto discard;
5318 }
5319
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005320 /* step 3: check security and precedence [ignored] */
5321
Eric Dumazet0c246042012-07-17 01:41:30 +00005322 /* step 4: Check for a SYN
Sowmini Varadhancd214532014-10-30 12:48:08 -04005323 * RFC 5961 4.2 : Send a challenge ack
Eric Dumazet0c246042012-07-17 01:41:30 +00005324 */
5325 if (th->syn) {
Eric Dumazete3715892012-07-17 12:29:30 +00005326syn_challenge:
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005327 if (syn_inerr)
Eric Dumazetc10d9312016-04-29 14:16:47 -07005328 TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
5329 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSYNCHALLENGE);
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005330 tcp_send_challenge_ack(sk, skb);
Eric Dumazet0c246042012-07-17 01:41:30 +00005331 goto discard;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005332 }
5333
Eric Dumazet0c246042012-07-17 01:41:30 +00005334 return true;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005335
5336discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07005337 tcp_drop(sk, skb);
Eric Dumazet0c246042012-07-17 01:41:30 +00005338 return false;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005339}
5340
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341/*
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005342 * TCP receive function for the ESTABLISHED state.
Florian Westphal31770e32017-08-30 19:24:58 +02005343 *
5344 * It is split into a fast path and a slow path. The fast path is
5345 * disabled when:
5346 * - A zero window was announced from us - zero window probing
5347 * is only handled properly in the slow path.
5348 * - Out of order segments arrived.
5349 * - Urgent data is expected.
5350 * - There is no buffer space left
5351 * - Unexpected TCP flags/window values/header lengths are received
5352 * (detected by checking the TCP header against pred_flags)
5353 * - Data is sent in both directions. Fast path only supports pure senders
5354 * or pure receivers (this means either the sequence number or the ack
5355 * value must stay constant)
5356 * - Unexpected TCP option.
5357 *
5358 * When these conditions are not satisfied it drops into a standard
5359 * receive procedure patterned after RFC793 to handle all cases.
5360 * The first three cases are guaranteed by proper pred_flags setting,
5361 * the rest is checked inline. Fast processing is turned on in
5362 * tcp_data_queue when everything is OK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 */
Vijay Subramanianc995ae22013-09-03 12:23:22 -07005364void tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
Matvejchikov Ilyae42e24c2017-07-24 16:02:12 +04005365 const struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366{
Matvejchikov Ilyae42e24c2017-07-24 16:02:12 +04005367 unsigned int len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 struct tcp_sock *tp = tcp_sk(sk);
5369
Eric Dumazet9a568de2017-05-16 14:00:14 -07005370 tcp_mstamp_refresh(tp);
Ian Morris51456b22015-04-03 09:17:26 +01005371 if (unlikely(!sk->sk_rx_dst))
Eric Dumazet5d299f32012-08-06 05:09:33 +00005372 inet_csk(sk)->icsk_af_ops->sk_rx_dst_set(sk, skb);
Florian Westphal31770e32017-08-30 19:24:58 +02005373 /*
5374 * Header prediction.
5375 * The code loosely follows the one in the famous
5376 * "30 instruction TCP receive" Van Jacobson mail.
5377 *
5378 * Van's trick is to deposit buffers into socket queue
5379 * on a device interrupt, to call tcp_recv function
5380 * on the receive process context and checksum and copy
5381 * the buffer to user space. smart...
5382 *
5383 * Our current scheme is not silly either but we take the
5384 * extra cost of the net_bh soft interrupt processing...
5385 * We do checksum and copy also but from device to kernel.
5386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
5388 tp->rx_opt.saw_tstamp = 0;
5389
Florian Westphal31770e32017-08-30 19:24:58 +02005390 /* pred_flags is 0xS?10 << 16 + snd_wnd
5391 * if header_prediction is to be made
5392 * 'S' will always be tp->tcp_header_len >> 2
5393 * '?' will be 0 for the fast path, otherwise pred_flags is 0 to
5394 * turn it off (when there are holes in the receive
5395 * space for instance)
5396 * PSH flag is ignored.
5397 */
5398
5399 if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags &&
5400 TCP_SKB_CB(skb)->seq == tp->rcv_nxt &&
5401 !after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) {
5402 int tcp_header_len = tp->tcp_header_len;
5403
5404 /* Timestamp header prediction: tcp_header_len
5405 * is automatically equal to th->doff*4 due to pred_flags
5406 * match.
5407 */
5408
5409 /* Check timestamp */
5410 if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) {
5411 /* No? Slow path! */
5412 if (!tcp_parse_aligned_timestamp(tp, th))
5413 goto slow_path;
5414
5415 /* If PAWS failed, check it more carefully in slow path */
5416 if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) < 0)
5417 goto slow_path;
5418
5419 /* DO NOT update ts_recent here, if checksum fails
5420 * and timestamp was corrupted part, it will result
5421 * in a hung connection since we will drop all
5422 * future packets due to the PAWS test.
5423 */
5424 }
5425
5426 if (len <= tcp_header_len) {
5427 /* Bulk data transfer: sender */
5428 if (len == tcp_header_len) {
5429 /* Predicted packet is in window by definition.
5430 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
5431 * Hence, check seq<=rcv_wup reduces to:
5432 */
5433 if (tcp_header_len ==
5434 (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
5435 tp->rcv_nxt == tp->rcv_wup)
5436 tcp_store_ts_recent(tp);
5437
5438 /* We know that such packets are checksummed
5439 * on entry.
5440 */
5441 tcp_ack(sk, skb, 0);
5442 __kfree_skb(skb);
5443 tcp_data_snd_check(sk);
5444 return;
5445 } else { /* Header too small */
5446 TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
5447 goto discard;
5448 }
5449 } else {
5450 int eaten = 0;
5451 bool fragstolen = false;
5452
5453 if (tcp_checksum_complete(skb))
5454 goto csum_error;
5455
5456 if ((int)skb->truesize > sk->sk_forward_alloc)
5457 goto step5;
5458
5459 /* Predicted packet is in window by definition.
5460 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
5461 * Hence, check seq<=rcv_wup reduces to:
5462 */
5463 if (tcp_header_len ==
5464 (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
5465 tp->rcv_nxt == tp->rcv_wup)
5466 tcp_store_ts_recent(tp);
5467
5468 tcp_rcv_rtt_measure_ts(sk, skb);
5469
5470 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPHPHITS);
5471
5472 /* Bulk data transfer: receiver */
5473 eaten = tcp_queue_rcv(sk, skb, tcp_header_len,
5474 &fragstolen);
5475
5476 tcp_event_data_recv(sk, skb);
5477
5478 if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) {
5479 /* Well, only one small jumplet in fast path... */
5480 tcp_ack(sk, skb, FLAG_DATA);
5481 tcp_data_snd_check(sk);
5482 if (!inet_csk_ack_scheduled(sk))
5483 goto no_ack;
5484 }
5485
5486 __tcp_ack_snd_check(sk, 0);
5487no_ack:
5488 if (eaten)
5489 kfree_skb_partial(skb, fragstolen);
5490 sk->sk_data_ready(sk);
5491 return;
5492 }
5493 }
5494
5495slow_path:
Eric Dumazetfb3477c2016-04-29 14:16:48 -07005496 if (len < (th->doff << 2) || tcp_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 goto csum_error;
5498
Calvin Owens0c228e82014-11-20 15:09:53 -08005499 if (!th->ack && !th->rst && !th->syn)
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00005500 goto discard;
5501
Florian Westphal31770e32017-08-30 19:24:58 +02005502 /*
5503 * Standard slow path.
5504 */
5505
Eric Dumazet0c246042012-07-17 01:41:30 +00005506 if (!tcp_validate_incoming(sk, skb, th, 1))
Vijay Subramanianc995ae22013-09-03 12:23:22 -07005507 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
Florian Westphal31770e32017-08-30 19:24:58 +02005509step5:
5510 if (tcp_ack(sk, skb, FLAG_SLOWPATH | FLAG_UPDATE_TS_RECENT) < 0)
John Dykstra96e0bf42009-03-22 21:49:57 -07005511 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005513 tcp_rcv_rtt_measure_ts(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
5515 /* Process urgent data. */
5516 tcp_urg(sk, skb, th);
5517
5518 /* step 7: process the segment text */
5519 tcp_data_queue(sk, skb);
5520
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005521 tcp_data_snd_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 tcp_ack_snd_check(sk);
Vijay Subramanianc995ae22013-09-03 12:23:22 -07005523 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524
5525csum_error:
Eric Dumazetc10d9312016-04-29 14:16:47 -07005526 TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS);
5527 TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528
5529discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07005530 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00005532EXPORT_SYMBOL(tcp_rcv_established);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005534void tcp_finish_connect(struct sock *sk, struct sk_buff *skb)
5535{
5536 struct tcp_sock *tp = tcp_sk(sk);
5537 struct inet_connection_sock *icsk = inet_csk(sk);
5538
5539 tcp_set_state(sk, TCP_ESTABLISHED);
Eric Dumazet70eabf02017-05-16 14:00:07 -07005540 icsk->icsk_ack.lrcvtime = tcp_jiffies32;
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005541
Ian Morris00db4122015-04-03 09:17:27 +01005542 if (skb) {
Eric Dumazet5d299f32012-08-06 05:09:33 +00005543 icsk->icsk_af_ops->sk_rx_dst_set(sk, skb);
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005544 security_inet_conn_established(sk, skb);
David S. Miller41063e92012-06-19 21:22:05 -07005545 }
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005546
Wei Wang27204aa2017-10-04 10:03:44 -07005547 tcp_init_transfer(sk, BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB);
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005548
5549 /* Prevent spurious tcp_cwnd_restart() on first data
5550 * packet.
5551 */
Eric Dumazetd635fbe2017-05-16 14:00:03 -07005552 tp->lsndtime = tcp_jiffies32;
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005553
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005554 if (sock_flag(sk, SOCK_KEEPOPEN))
5555 inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tp));
Florian Westphal31770e32017-08-30 19:24:58 +02005556
5557 if (!tp->rx_opt.snd_wscale)
5558 __tcp_fast_path_on(tp, tp->snd_wnd);
5559 else
5560 tp->pred_flags = 0;
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005561}
5562
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005563static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,
5564 struct tcp_fastopen_cookie *cookie)
5565{
5566 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet75c119a2017-10-05 22:21:27 -07005567 struct sk_buff *data = tp->syn_data ? tcp_rtx_queue_head(sk) : NULL;
Daniel Lee2646c832015-04-06 14:37:27 -07005568 u16 mss = tp->rx_opt.mss_clamp, try_exp = 0;
5569 bool syn_drop = false;
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005570
5571 if (mss == tp->rx_opt.user_mss) {
5572 struct tcp_options_received opt;
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005573
5574 /* Get original SYNACK MSS value if user MSS sets mss_clamp */
5575 tcp_clear_options(&opt);
5576 opt.user_mss = opt.mss_clamp = 0;
Eric Dumazeteed29f12017-06-07 10:34:36 -07005577 tcp_parse_options(sock_net(sk), synack, &opt, 0, NULL);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005578 mss = opt.mss_clamp;
5579 }
5580
Daniel Lee2646c832015-04-06 14:37:27 -07005581 if (!tp->syn_fastopen) {
5582 /* Ignore an unsolicited cookie */
Yuchung Cheng67da22d2012-07-19 06:43:11 +00005583 cookie->len = -1;
Daniel Lee2646c832015-04-06 14:37:27 -07005584 } else if (tp->total_retrans) {
5585 /* SYN timed out and the SYN-ACK neither has a cookie nor
5586 * acknowledges data. Presumably the remote received only
5587 * the retransmitted (regular) SYNs: either the original
5588 * SYN-data or the corresponding SYN-ACK was dropped.
5589 */
5590 syn_drop = (cookie->len < 0 && data);
5591 } else if (cookie->len < 0 && !tp->syn_data) {
5592 /* We requested a cookie but didn't get it. If we did not use
5593 * the (old) exp opt format then try so next time (try_exp=1).
5594 * Otherwise we go back to use the RFC7413 opt (try_exp=2).
5595 */
5596 try_exp = tp->syn_fastopen_exp ? 2 : 1;
5597 }
Yuchung Cheng67da22d2012-07-19 06:43:11 +00005598
Daniel Lee2646c832015-04-06 14:37:27 -07005599 tcp_fastopen_cache_set(sk, mss, cookie, syn_drop, try_exp);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005600
5601 if (data) { /* Retransmit unacked data in SYN */
Eric Dumazet75c119a2017-10-05 22:21:27 -07005602 skb_rbtree_walk_from(data) {
5603 if (__tcp_retransmit_skb(sk, data, 1))
Yuchung Cheng93b174a2012-12-06 08:45:32 +00005604 break;
5605 }
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005606 tcp_rearm_rto(sk);
Eric Dumazetc10d9312016-04-29 14:16:47 -07005607 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07005608 LINUX_MIB_TCPFASTOPENACTIVEFAIL);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005609 return true;
5610 }
Yuchung Cheng6f736012012-10-19 15:14:44 +00005611 tp->syn_data_acked = tp->syn_data;
Yuchung Chengf19c29e2014-03-03 12:31:36 -08005612 if (tp->syn_data_acked)
Eric Dumazetc10d9312016-04-29 14:16:47 -07005613 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07005614 LINUX_MIB_TCPFASTOPENACTIVE);
Eric Dumazet61d2bca2016-02-01 21:03:07 -08005615
5616 tcp_fastopen_add_skb(sk, synack);
5617
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005618 return false;
5619}
5620
Ursula Braun60e2a772017-10-25 11:01:45 +02005621static void smc_check_reset_syn(struct tcp_sock *tp)
5622{
5623#if IS_ENABLED(CONFIG_SMC)
5624 if (static_branch_unlikely(&tcp_have_smc)) {
5625 if (tp->syn_smc && !tp->rx_opt.smc_ok)
5626 tp->syn_smc = 0;
5627 }
5628#endif
5629}
5630
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
Eric Dumazetbda07a62015-09-29 07:42:40 -07005632 const struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633{
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08005634 struct inet_connection_sock *icsk = inet_csk(sk);
William Allen Simpson4957faade2009-12-02 18:25:27 +00005635 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005636 struct tcp_fastopen_cookie foc = { .len = -1 };
William Allen Simpson4957faade2009-12-02 18:25:27 +00005637 int saved_clamp = tp->rx_opt.mss_clamp;
Eric Dumazet0f9fa832017-04-18 09:45:52 -07005638 bool fastopen_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639
Eric Dumazeteed29f12017-06-07 10:34:36 -07005640 tcp_parse_options(sock_net(sk), skb, &tp->rx_opt, 0, &foc);
Andrew Vagine3e12022013-08-27 12:21:55 +04005641 if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
Andrey Vaginee684b62013-02-11 05:50:19 +00005642 tp->rx_opt.rcv_tsecr -= tp->tsoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
5644 if (th->ack) {
5645 /* rfc793:
5646 * "If the state is SYN-SENT then
5647 * first check the ACK bit
5648 * If the ACK bit is set
5649 * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send
5650 * a reset (unless the RST bit is set, if so drop
5651 * the segment and return)"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 */
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005653 if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) ||
5654 after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 goto reset_and_undo;
5656
5657 if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
5658 !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp,
Eric Dumazet9a568de2017-05-16 14:00:14 -07005659 tcp_time_stamp(tp))) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07005660 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07005661 LINUX_MIB_PAWSACTIVEREJECTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 goto reset_and_undo;
5663 }
5664
5665 /* Now ACK is acceptable.
5666 *
5667 * "If the RST bit is set
5668 * If the ACK was acceptable then signal the user "error:
5669 * connection reset", drop the segment, enter CLOSED state,
5670 * delete TCB, and return."
5671 */
5672
5673 if (th->rst) {
5674 tcp_reset(sk);
5675 goto discard;
5676 }
5677
5678 /* rfc793:
5679 * "fifth, if neither of the SYN or RST bits is set then
5680 * drop the segment and return."
5681 *
5682 * See note below!
5683 * --ANK(990513)
5684 */
5685 if (!th->syn)
5686 goto discard_and_undo;
5687
5688 /* rfc793:
5689 * "If the SYN bit is on ...
5690 * are acceptable then ...
5691 * (our SYN has been ACKed), change the connection
5692 * state to ESTABLISHED..."
5693 */
5694
Florian Westphal735d3832014-09-29 13:08:30 +02005695 tcp_ecn_rcv_synack(tp, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
Razvan Ghitulete1b3a6922012-08-14 16:30:20 +03005697 tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
Florian Westphal31770e32017-08-30 19:24:58 +02005698 tcp_ack(sk, skb, FLAG_SLOWPATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699
5700 /* Ok.. it's good. Set up sequence numbers and
5701 * move to established.
5702 */
5703 tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
5704 tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
5705
5706 /* RFC1323: The window in SYN & SYN/ACK segments is
5707 * never scaled.
5708 */
5709 tp->snd_wnd = ntohs(th->window);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
5711 if (!tp->rx_opt.wscale_ok) {
5712 tp->rx_opt.snd_wscale = tp->rx_opt.rcv_wscale = 0;
5713 tp->window_clamp = min(tp->window_clamp, 65535U);
5714 }
5715
5716 if (tp->rx_opt.saw_tstamp) {
5717 tp->rx_opt.tstamp_ok = 1;
5718 tp->tcp_header_len =
5719 sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
5720 tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
5721 tcp_store_ts_recent(tp);
5722 } else {
5723 tp->tcp_header_len = sizeof(struct tcphdr);
5724 }
5725
Ilpo Järvinene60402d2007-08-09 15:14:46 +03005726 if (tcp_is_sack(tp) && sysctl_tcp_fack)
5727 tcp_enable_fack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08005729 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 tcp_initialize_rcv_mss(sk);
5731
5732 /* Remember, tcp_poll() does not lock socket!
5733 * Change state from SYN-SENT only after copied_seq
5734 * is initialized. */
5735 tp->copied_seq = tp->rcv_nxt;
William Allen Simpson4957faade2009-12-02 18:25:27 +00005736
Ursula Braun60e2a772017-10-25 11:01:45 +02005737 smc_check_reset_syn(tp);
5738
Ralf Baechlee16aa202006-12-07 00:11:33 -08005739 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005741 tcp_finish_connect(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742
Eric Dumazet0f9fa832017-04-18 09:45:52 -07005743 fastopen_fail = (tp->syn_fastopen || tp->syn_data) &&
5744 tcp_rcv_fastopen_synack(sk, skb, &foc);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005745
Eric Dumazet0f9fa832017-04-18 09:45:52 -07005746 if (!sock_flag(sk, SOCK_DEAD)) {
5747 sk->sk_state_change(sk);
5748 sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
5749 }
5750 if (fastopen_fail)
5751 return -1;
Arnaldo Carvalho de Melo295f7322005-08-09 20:11:56 -07005752 if (sk->sk_write_pending ||
5753 icsk->icsk_accept_queue.rskq_defer_accept ||
5754 icsk->icsk_ack.pingpong) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 /* Save one ACK. Data will be ready after
5756 * several ticks, if write_pending is set.
5757 *
5758 * It may be deleted, but with this feature tcpdumps
5759 * look so _wonderfully_ clever, that I was not able
5760 * to stand against the temptation 8) --ANK
5761 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005762 inet_csk_schedule_ack(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005763 tcp_enter_quickack_mode(sk);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -07005764 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
5765 TCP_DELACK_MAX, TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766
5767discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07005768 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 return 0;
5770 } else {
5771 tcp_send_ack(sk);
5772 }
5773 return -1;
5774 }
5775
5776 /* No ACK in the segment */
5777
5778 if (th->rst) {
5779 /* rfc793:
5780 * "If the RST bit is set
5781 *
5782 * Otherwise (no ACK) drop the segment and return."
5783 */
5784
5785 goto discard_and_undo;
5786 }
5787
5788 /* PAWS check. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005789 if (tp->rx_opt.ts_recent_stamp && tp->rx_opt.saw_tstamp &&
Ilpo Järvinenc887e6d2009-03-14 14:23:03 +00005790 tcp_paws_reject(&tp->rx_opt, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 goto discard_and_undo;
5792
5793 if (th->syn) {
5794 /* We see SYN without ACK. It is attempt of
5795 * simultaneous connect with crossed SYNs.
5796 * Particularly, it can be connect to self.
5797 */
5798 tcp_set_state(sk, TCP_SYN_RECV);
5799
5800 if (tp->rx_opt.saw_tstamp) {
5801 tp->rx_opt.tstamp_ok = 1;
5802 tcp_store_ts_recent(tp);
5803 tp->tcp_header_len =
5804 sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
5805 } else {
5806 tp->tcp_header_len = sizeof(struct tcphdr);
5807 }
5808
5809 tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
Eric Dumazet142a2e72015-11-26 08:18:14 -08005810 tp->copied_seq = tp->rcv_nxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
5812
5813 /* RFC1323: The window in SYN & SYN/ACK segments is
5814 * never scaled.
5815 */
5816 tp->snd_wnd = ntohs(th->window);
5817 tp->snd_wl1 = TCP_SKB_CB(skb)->seq;
5818 tp->max_window = tp->snd_wnd;
5819
Florian Westphal735d3832014-09-29 13:08:30 +02005820 tcp_ecn_rcv_syn(tp, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821
John Heffner5d424d52006-03-20 17:53:41 -08005822 tcp_mtup_init(sk);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08005823 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 tcp_initialize_rcv_mss(sk);
5825
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 tcp_send_synack(sk);
5827#if 0
5828 /* Note, we could accept data and URG from this segment.
Jerry Chu168a8f52012-08-31 12:29:13 +00005829 * There are no obstacles to make this (except that we must
5830 * either change tcp_recvmsg() to prevent it from returning data
5831 * before 3WHS completes per RFC793, or employ TCP Fast Open).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 *
5833 * However, if we ignore data in ACKless segments sometimes,
5834 * we have no reasons to accept it sometimes.
5835 * Also, seems the code doing it in step6 of tcp_rcv_state_process
5836 * is not flawless. So, discard packet for sanity.
5837 * Uncomment this return to process the data.
5838 */
5839 return -1;
5840#else
5841 goto discard;
5842#endif
5843 }
5844 /* "fifth, if neither of the SYN or RST bits is set then
5845 * drop the segment and return."
5846 */
5847
5848discard_and_undo:
5849 tcp_clear_options(&tp->rx_opt);
5850 tp->rx_opt.mss_clamp = saved_clamp;
5851 goto discard;
5852
5853reset_and_undo:
5854 tcp_clear_options(&tp->rx_opt);
5855 tp->rx_opt.mss_clamp = saved_clamp;
5856 return 1;
5857}
5858
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859/*
5860 * This function implements the receiving procedure of RFC 793 for
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005861 * all states except ESTABLISHED and TIME_WAIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 * It's called from both tcp_v4_rcv and tcp_v6_rcv and should be
5863 * address independent.
5864 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005865
Eric Dumazet72ab4a82015-09-29 07:42:41 -07005866int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867{
5868 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08005869 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet72ab4a82015-09-29 07:42:41 -07005870 const struct tcphdr *th = tcp_hdr(skb);
Jerry Chu168a8f52012-08-31 12:29:13 +00005871 struct request_sock *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 int queued = 0;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00005873 bool acceptable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 switch (sk->sk_state) {
5876 case TCP_CLOSE:
5877 goto discard;
5878
5879 case TCP_LISTEN:
Stephen Hemminger2de979b2007-03-08 20:45:19 -08005880 if (th->ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 return 1;
5882
Stephen Hemminger2de979b2007-03-08 20:45:19 -08005883 if (th->rst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 goto discard;
5885
Stephen Hemminger2de979b2007-03-08 20:45:19 -08005886 if (th->syn) {
Eric Dumazetfdf5af02011-12-02 23:41:42 +00005887 if (th->fin)
5888 goto discard;
Eric Dumazet449809a2017-03-01 08:39:49 -08005889 /* It is possible that we process SYN packets from backlog,
5890 * so we need to make sure to disable BH right there.
5891 */
5892 local_bh_disable();
5893 acceptable = icsk->icsk_af_ops->conn_request(sk, skb) >= 0;
5894 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
Eric Dumazet449809a2017-03-01 08:39:49 -08005896 if (!acceptable)
5897 return 1;
Eric Dumazet0aea76d2016-04-21 22:13:01 -07005898 consume_skb(skb);
Masayuki Nakagawafb7e2392007-01-23 20:15:06 -08005899 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 }
5901 goto discard;
5902
5903 case TCP_SYN_SENT:
Eric Dumazet8804b272016-04-13 22:05:40 -07005904 tp->rx_opt.saw_tstamp = 0;
Eric Dumazet9a568de2017-05-16 14:00:14 -07005905 tcp_mstamp_refresh(tp);
Eric Dumazetbda07a62015-09-29 07:42:40 -07005906 queued = tcp_rcv_synsent_state_process(sk, skb, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 if (queued >= 0)
5908 return queued;
5909
5910 /* Do step6 onward by hand. */
5911 tcp_urg(sk, skb, th);
5912 __kfree_skb(skb);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005913 tcp_data_snd_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 return 0;
5915 }
5916
Eric Dumazet9a568de2017-05-16 14:00:14 -07005917 tcp_mstamp_refresh(tp);
Eric Dumazet8804b272016-04-13 22:05:40 -07005918 tp->rx_opt.saw_tstamp = 0;
Jerry Chu168a8f52012-08-31 12:29:13 +00005919 req = tp->fastopen_rsk;
Ian Morris00db4122015-04-03 09:17:27 +01005920 if (req) {
Jerry Chu37561f62012-10-22 11:26:36 +00005921 WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
Jerry Chu168a8f52012-08-31 12:29:13 +00005922 sk->sk_state != TCP_FIN_WAIT1);
5923
Ian Morris51456b22015-04-03 09:17:26 +01005924 if (!tcp_check_req(sk, skb, req, true))
Jerry Chu168a8f52012-08-31 12:29:13 +00005925 goto discard;
Neal Cardwelle69bebd2012-09-22 04:18:57 +00005926 }
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00005927
Calvin Owens0c228e82014-11-20 15:09:53 -08005928 if (!th->ack && !th->rst && !th->syn)
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00005929 goto discard;
5930
Neal Cardwelle69bebd2012-09-22 04:18:57 +00005931 if (!tcp_validate_incoming(sk, skb, th, 0))
Eric Dumazet0c246042012-07-17 01:41:30 +00005932 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
5934 /* step 5: check the ACK field */
Florian Westphal31770e32017-08-30 19:24:58 +02005935 acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH |
5936 FLAG_UPDATE_TS_RECENT |
Eric Dumazetd0e1a1b2017-05-23 15:24:46 -07005937 FLAG_NO_CHALLENGE_ACK) > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938
Eric Dumazetd0e1a1b2017-05-23 15:24:46 -07005939 if (!acceptable) {
5940 if (sk->sk_state == TCP_SYN_RECV)
5941 return 1; /* send one RST */
5942 tcp_send_challenge_ack(sk, skb);
5943 goto discard;
5944 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00005945 switch (sk->sk_state) {
5946 case TCP_SYN_RECV:
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07005947 if (!tp->srtt_us)
5948 tcp_synack_rtt_meas(sk, req);
5949
Joe Perches61eb90032013-05-24 18:36:13 +00005950 /* Once we leave TCP_SYN_RECV, we no longer need req
5951 * so release it.
5952 */
5953 if (req) {
Yuchung Cheng7e32b442016-09-21 16:16:15 -07005954 inet_csk(sk)->icsk_retransmits = 0;
Joe Perches61eb90032013-05-24 18:36:13 +00005955 reqsk_fastopen_remove(sk, req, false);
Wei Wang6d050812017-10-04 10:04:04 -07005956 /* Re-arm the timer because data may have been sent out.
5957 * This is similar to the regular data transmission case
5958 * when new data has just been ack'ed.
5959 *
5960 * (TFO) - we could try to be more aggressive and
5961 * retransmitting any data sooner based on when they
5962 * are sent out.
5963 */
5964 tcp_rearm_rto(sk);
Joe Perches61eb90032013-05-24 18:36:13 +00005965 } else {
Wei Wang27204aa2017-10-04 10:03:44 -07005966 tcp_init_transfer(sk, BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB);
Joe Perches61eb90032013-05-24 18:36:13 +00005967 tp->copied_seq = tp->rcv_nxt;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00005968 }
Joe Perches61eb90032013-05-24 18:36:13 +00005969 smp_mb();
5970 tcp_set_state(sk, TCP_ESTABLISHED);
5971 sk->sk_state_change(sk);
5972
5973 /* Note, that this wakeup is only for marginal crossed SYN case.
5974 * Passively open sockets are not waked up, because
5975 * sk->sk_sleep == NULL and sk->sk_socket == NULL.
5976 */
5977 if (sk->sk_socket)
5978 sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
5979
5980 tp->snd_una = TCP_SKB_CB(skb)->ack_seq;
5981 tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale;
5982 tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
5983
5984 if (tp->rx_opt.tstamp_ok)
5985 tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
5986
Yuchung Chengc0402762016-09-19 23:39:21 -04005987 if (!inet_csk(sk)->icsk_ca_ops->cong_control)
5988 tcp_update_pacing_rate(sk);
Neal Cardwell02cf4eb2013-10-21 15:40:19 -04005989
Joe Perches61eb90032013-05-24 18:36:13 +00005990 /* Prevent spurious tcp_cwnd_restart() on first data packet */
Eric Dumazetd635fbe2017-05-16 14:00:03 -07005991 tp->lsndtime = tcp_jiffies32;
Joe Perches61eb90032013-05-24 18:36:13 +00005992
5993 tcp_initialize_rcv_mss(sk);
Florian Westphal31770e32017-08-30 19:24:58 +02005994 tcp_fast_path_on(tp);
Eric Dumazet1f6afc82013-05-24 15:03:54 +00005995 break;
5996
Joe Perchesc48b22d2013-05-24 18:06:58 +00005997 case TCP_FIN_WAIT1: {
Joe Perchesc48b22d2013-05-24 18:06:58 +00005998 int tmo;
5999
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006000 /* If we enter the TCP_FIN_WAIT1 state and we are a
6001 * Fast Open socket and this is the first acceptable
6002 * ACK we have received, this would have acknowledged
6003 * our SYNACK so stop the SYNACK timer.
6004 */
Ian Morris00db4122015-04-03 09:17:27 +01006005 if (req) {
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006006 /* We no longer need the request sock. */
6007 reqsk_fastopen_remove(sk, req, false);
6008 tcp_rearm_rto(sk);
6009 }
Joe Perchesc48b22d2013-05-24 18:06:58 +00006010 if (tp->snd_una != tp->write_seq)
6011 break;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006012
Joe Perchesc48b22d2013-05-24 18:06:58 +00006013 tcp_set_state(sk, TCP_FIN_WAIT2);
6014 sk->sk_shutdown |= SEND_SHUTDOWN;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006015
Julian Anastasovc3a2e832017-02-06 23:14:14 +02006016 sk_dst_confirm(sk);
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006017
Joe Perchesc48b22d2013-05-24 18:06:58 +00006018 if (!sock_flag(sk, SOCK_DEAD)) {
6019 /* Wake up lingering close() */
6020 sk->sk_state_change(sk);
6021 break;
6022 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006023
Wei Wangcf1ef3f2017-04-20 14:45:46 -07006024 if (tp->linger2 < 0) {
6025 tcp_done(sk);
6026 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
6027 return 1;
6028 }
6029 if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
6030 after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
6031 /* Receive out of order FIN after close() */
6032 if (tp->syn_fastopen && th->fin)
Wei Wang46c2fa32017-04-20 14:45:47 -07006033 tcp_fastopen_active_disable(sk);
Joe Perchesc48b22d2013-05-24 18:06:58 +00006034 tcp_done(sk);
Eric Dumazetc10d9312016-04-29 14:16:47 -07006035 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
Joe Perchesc48b22d2013-05-24 18:06:58 +00006036 return 1;
6037 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006038
Joe Perchesc48b22d2013-05-24 18:06:58 +00006039 tmo = tcp_fin_time(sk);
6040 if (tmo > TCP_TIMEWAIT_LEN) {
6041 inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN);
6042 } else if (th->fin || sock_owned_by_user(sk)) {
6043 /* Bad case. We could lose such FIN otherwise.
6044 * It is not a big problem, but it looks confusing
6045 * and not so rare event. We still can lose it now,
6046 * if it spins in bh_lock_sock(), but it is really
6047 * marginal case.
6048 */
6049 inet_csk_reset_keepalive_timer(sk, tmo);
6050 } else {
6051 tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
6052 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006054 break;
Joe Perchesc48b22d2013-05-24 18:06:58 +00006055 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006056
6057 case TCP_CLOSING:
6058 if (tp->snd_una == tp->write_seq) {
6059 tcp_time_wait(sk, TCP_TIME_WAIT, 0);
6060 goto discard;
6061 }
6062 break;
6063
6064 case TCP_LAST_ACK:
6065 if (tp->snd_una == tp->write_seq) {
6066 tcp_update_metrics(sk);
6067 tcp_done(sk);
6068 goto discard;
6069 }
6070 break;
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00006071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
6073 /* step 6: check the URG bit */
6074 tcp_urg(sk, skb, th);
6075
6076 /* step 7: process the segment text */
6077 switch (sk->sk_state) {
6078 case TCP_CLOSE_WAIT:
6079 case TCP_CLOSING:
6080 case TCP_LAST_ACK:
6081 if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
6082 break;
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05006083 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 case TCP_FIN_WAIT1:
6085 case TCP_FIN_WAIT2:
6086 /* RFC 793 says to queue data in these states,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09006087 * RFC 1122 says we MUST send a reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 * BSD 4.4 also does reset.
6089 */
6090 if (sk->sk_shutdown & RCV_SHUTDOWN) {
6091 if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
6092 after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07006093 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 tcp_reset(sk);
6095 return 1;
6096 }
6097 }
6098 /* Fall through */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09006099 case TCP_ESTABLISHED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 tcp_data_queue(sk, skb);
6101 queued = 1;
6102 break;
6103 }
6104
6105 /* tcp_data could move socket to TIME-WAIT */
6106 if (sk->sk_state != TCP_CLOSE) {
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07006107 tcp_data_snd_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 tcp_ack_snd_check(sk);
6109 }
6110
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09006111 if (!queued) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07006113 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 }
6115 return 0;
6116}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117EXPORT_SYMBOL(tcp_rcv_state_process);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006118
6119static inline void pr_drop_req(struct request_sock *req, __u16 port, int family)
6120{
6121 struct inet_request_sock *ireq = inet_rsk(req);
6122
6123 if (family == AF_INET)
Joe Perchesba7a46f2014-11-11 10:59:17 -08006124 net_dbg_ratelimited("drop open request from %pI4/%u\n",
6125 &ireq->ir_rmt_addr, port);
Octavian Purdila4135ab82014-06-28 21:20:54 +03006126#if IS_ENABLED(CONFIG_IPV6)
6127 else if (family == AF_INET6)
Joe Perchesba7a46f2014-11-11 10:59:17 -08006128 net_dbg_ratelimited("drop open request from %pI6/%u\n",
6129 &ireq->ir_v6_rmt_addr, port);
Octavian Purdila4135ab82014-06-28 21:20:54 +03006130#endif
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006131}
6132
Florian Westphald82bd122014-09-29 13:08:29 +02006133/* RFC3168 : 6.1.1 SYN packets must not have ECT/ECN bits set
6134 *
6135 * If we receive a SYN packet with these bits set, it means a
6136 * network is playing bad games with TOS bits. In order to
6137 * avoid possible false congestion notifications, we disable
stephen hemmingerf4e715c2014-10-29 16:05:06 -07006138 * TCP ECN negotiation.
Florian Westphald82bd122014-09-29 13:08:29 +02006139 *
6140 * Exception: tcp_ca wants ECN. This is required for DCTCP
Florian Westphal843c2fd2015-01-30 20:45:20 +01006141 * congestion control: Linux DCTCP asserts ECT on all packets,
6142 * including SYN, which is most optimal solution; however,
6143 * others, such as FreeBSD do not.
Florian Westphald82bd122014-09-29 13:08:29 +02006144 */
6145static void tcp_ecn_create_request(struct request_sock *req,
6146 const struct sk_buff *skb,
Florian Westphalf7b3bec2014-11-03 17:35:03 +01006147 const struct sock *listen_sk,
6148 const struct dst_entry *dst)
Florian Westphald82bd122014-09-29 13:08:29 +02006149{
6150 const struct tcphdr *th = tcp_hdr(skb);
6151 const struct net *net = sock_net(listen_sk);
6152 bool th_ecn = th->ece && th->cwr;
Florian Westphal843c2fd2015-01-30 20:45:20 +01006153 bool ect, ecn_ok;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02006154 u32 ecn_ok_dst;
Florian Westphald82bd122014-09-29 13:08:29 +02006155
6156 if (!th_ecn)
6157 return;
6158
6159 ect = !INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield);
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02006160 ecn_ok_dst = dst_feature(dst, DST_FEATURE_ECN_MASK);
6161 ecn_ok = net->ipv4.sysctl_tcp_ecn || ecn_ok_dst;
Florian Westphald82bd122014-09-29 13:08:29 +02006162
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02006163 if ((!ect && ecn_ok) || tcp_ca_needs_ecn(listen_sk) ||
Lawrence Brakmo91b5b212017-06-30 20:02:49 -07006164 (ecn_ok_dst & DST_FEATURE_ECN_CA) ||
6165 tcp_bpf_ca_needs_ecn((struct sock *)req))
Florian Westphald82bd122014-09-29 13:08:29 +02006166 inet_rsk(req)->ecn_ok = 1;
6167}
6168
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006169static void tcp_openreq_init(struct request_sock *req,
6170 const struct tcp_options_received *rx_opt,
6171 struct sk_buff *skb, const struct sock *sk)
6172{
6173 struct inet_request_sock *ireq = inet_rsk(req);
6174
Eric Dumazeted53d0a2015-10-08 19:33:23 -07006175 req->rsk_rcv_wnd = 0; /* So that tcp_send_synack() knows! */
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006176 req->cookie_ts = 0;
6177 tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
6178 tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
Eric Dumazet9a568de2017-05-16 14:00:14 -07006179 tcp_rsk(req)->snt_synack = tcp_clock_us();
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006180 tcp_rsk(req)->last_oow_ack_time = 0;
6181 req->mss = rx_opt->mss_clamp;
6182 req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
6183 ireq->tstamp_ok = rx_opt->tstamp_ok;
6184 ireq->sack_ok = rx_opt->sack_ok;
6185 ireq->snd_wscale = rx_opt->snd_wscale;
6186 ireq->wscale_ok = rx_opt->wscale_ok;
6187 ireq->acked = 0;
6188 ireq->ecn_ok = 0;
6189 ireq->ir_rmt_port = tcp_hdr(skb)->source;
6190 ireq->ir_num = ntohs(tcp_hdr(skb)->dest);
6191 ireq->ir_mark = inet_request_mark(sk, skb);
Ursula Braun60e2a772017-10-25 11:01:45 +02006192#if IS_ENABLED(CONFIG_SMC)
6193 ireq->smc_ok = rx_opt->smc_ok;
6194#endif
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006195}
6196
Eric Dumazete49bb332015-03-17 18:32:27 -07006197struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops,
Eric Dumazeta1a53442015-10-04 21:08:11 -07006198 struct sock *sk_listener,
6199 bool attach_listener)
Eric Dumazete49bb332015-03-17 18:32:27 -07006200{
Eric Dumazeta1a53442015-10-04 21:08:11 -07006201 struct request_sock *req = reqsk_alloc(ops, sk_listener,
6202 attach_listener);
Eric Dumazete49bb332015-03-17 18:32:27 -07006203
6204 if (req) {
6205 struct inet_request_sock *ireq = inet_rsk(req);
6206
6207 kmemcheck_annotate_bitfield(ireq, flags);
Eric Dumazetc92e8c02017-10-20 09:04:13 -07006208 ireq->ireq_opt = NULL;
Huw Davies56ac42b2016-06-27 15:05:28 -04006209#if IS_ENABLED(CONFIG_IPV6)
6210 ireq->pktopts = NULL;
6211#endif
Eric Dumazete49bb332015-03-17 18:32:27 -07006212 atomic64_set(&ireq->ir_cookie, 0);
6213 ireq->ireq_state = TCP_NEW_SYN_RECV;
6214 write_pnet(&ireq->ireq_net, sock_net(sk_listener));
Eric Dumazet0144a812015-03-24 21:45:56 -07006215 ireq->ireq_family = sk_listener->sk_family;
Eric Dumazete49bb332015-03-17 18:32:27 -07006216 }
6217
6218 return req;
6219}
6220EXPORT_SYMBOL(inet_reqsk_alloc);
6221
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006222/*
6223 * Return true if a syncookie should be sent
6224 */
Eric Dumazet2985aaa2015-09-29 07:42:51 -07006225static bool tcp_syn_flood_action(const struct sock *sk,
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006226 const struct sk_buff *skb,
6227 const char *proto)
6228{
Eric Dumazet8d2675f2015-10-02 11:43:25 -07006229 struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006230 const char *msg = "Dropping request";
6231 bool want_cookie = false;
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006232 struct net *net = sock_net(sk);
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006233
6234#ifdef CONFIG_SYN_COOKIES
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006235 if (net->ipv4.sysctl_tcp_syncookies) {
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006236 msg = "Sending cookies";
6237 want_cookie = true;
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07006238 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPREQQFULLDOCOOKIES);
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006239 } else
6240#endif
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07006241 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPREQQFULLDROP);
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006242
Eric Dumazet8d2675f2015-10-02 11:43:25 -07006243 if (!queue->synflood_warned &&
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006244 net->ipv4.sysctl_tcp_syncookies != 2 &&
Eric Dumazet8d2675f2015-10-02 11:43:25 -07006245 xchg(&queue->synflood_warned, 1) == 0)
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006246 pr_info("%s: Possible SYN flooding on port %d. %s. Check SNMP counters.\n",
6247 proto, ntohs(tcp_hdr(skb)->dest), msg);
Eric Dumazet2985aaa2015-09-29 07:42:51 -07006248
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006249 return want_cookie;
6250}
6251
Eric Dumazetcd8ae852015-05-03 21:34:46 -07006252static void tcp_reqsk_record_syn(const struct sock *sk,
6253 struct request_sock *req,
6254 const struct sk_buff *skb)
6255{
6256 if (tcp_sk(sk)->save_syn) {
6257 u32 len = skb_network_header_len(skb) + tcp_hdrlen(skb);
6258 u32 *copy;
6259
6260 copy = kmalloc(len + sizeof(u32), GFP_ATOMIC);
6261 if (copy) {
6262 copy[0] = len;
6263 memcpy(&copy[1], skb_network_header(skb), len);
6264 req->saved_syn = copy;
6265 }
6266 }
6267}
6268
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006269int tcp_conn_request(struct request_sock_ops *rsk_ops,
6270 const struct tcp_request_sock_ops *af_ops,
6271 struct sock *sk, struct sk_buff *skb)
6272{
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006273 struct tcp_fastopen_cookie foc = { .len = -1 };
Eric Dumazet7c85af82015-09-24 17:16:05 -07006274 __u32 isn = TCP_SKB_CB(skb)->tcp_tw_isn;
6275 struct tcp_options_received tmp_opt;
6276 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006277 struct net *net = sock_net(sk);
Eric Dumazet7c85af82015-09-24 17:16:05 -07006278 struct sock *fastopen_sk = NULL;
Eric Dumazet7c85af82015-09-24 17:16:05 -07006279 struct request_sock *req;
6280 bool want_cookie = false;
Eric Dumazeteaa72dc2017-08-29 15:16:01 -07006281 struct dst_entry *dst;
Eric Dumazet7c85af82015-09-24 17:16:05 -07006282 struct flowi fl;
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006283
6284 /* TW buckets are converted to open requests without
6285 * limitations, they conserve resources and peer is
6286 * evidently real one.
6287 */
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006288 if ((net->ipv4.sysctl_tcp_syncookies == 2 ||
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006289 inet_csk_reqsk_queue_is_full(sk)) && !isn) {
6290 want_cookie = tcp_syn_flood_action(sk, skb, rsk_ops->slab_name);
6291 if (!want_cookie)
6292 goto drop;
6293 }
6294
Eric Dumazet5ea8ea22016-10-26 09:27:57 -07006295 if (sk_acceptq_is_full(sk)) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07006296 NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006297 goto drop;
6298 }
6299
Eric Dumazeta1a53442015-10-04 21:08:11 -07006300 req = inet_reqsk_alloc(rsk_ops, sk, !want_cookie);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006301 if (!req)
6302 goto drop;
6303
6304 tcp_rsk(req)->af_specific = af_ops;
Florian Westphal95a22ca2016-12-01 11:32:06 +01006305 tcp_rsk(req)->ts_off = 0;
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006306
6307 tcp_clear_options(&tmp_opt);
6308 tmp_opt.mss_clamp = af_ops->mss_clamp;
6309 tmp_opt.user_mss = tp->rx_opt.user_mss;
Eric Dumazeteed29f12017-06-07 10:34:36 -07006310 tcp_parse_options(sock_net(sk), skb, &tmp_opt, 0,
6311 want_cookie ? NULL : &foc);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006312
6313 if (want_cookie && !tmp_opt.saw_tstamp)
6314 tcp_clear_options(&tmp_opt);
6315
6316 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
6317 tcp_openreq_init(req, &tmp_opt, skb, sk);
KOVACS Krisztian7a682572016-09-23 11:27:42 +02006318 inet_rsk(req)->no_srccheck = inet_sk(sk)->transparent;
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006319
Eric Dumazet16f86162015-03-13 15:51:10 -07006320 /* Note: tcp_v6_init_req() might override ir_iif for link locals */
David Ahern6dd9a142015-12-16 13:20:44 -08006321 inet_rsk(req)->ir_iif = inet_request_bound_dev_if(sk, skb);
Eric Dumazet16f86162015-03-13 15:51:10 -07006322
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006323 af_ops->init_req(req, sk, skb);
6324
6325 if (security_inet_conn_request(sk, skb, req))
6326 goto drop_and_free;
6327
Eric Dumazet84b114b2017-05-05 06:56:54 -07006328 if (tmp_opt.tstamp_ok)
Eric Dumazet5d2ed052017-06-07 10:34:39 -07006329 tcp_rsk(req)->ts_off = af_ops->init_ts_off(net, skb);
Florian Westphal95a22ca2016-12-01 11:32:06 +01006330
Tonghao Zhang49c71582017-08-21 23:33:48 -07006331 dst = af_ops->route_req(sk, &fl, req);
6332 if (!dst)
6333 goto drop_and_free;
6334
Florian Westphalf7b3bec2014-11-03 17:35:03 +01006335 if (!want_cookie && !isn) {
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006336 /* Kill the following clause, if you dislike this way. */
Soheil Hassas Yeganeh4396e462017-03-15 16:30:46 -04006337 if (!net->ipv4.sysctl_tcp_syncookies &&
6338 (net->ipv4.sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
6339 (net->ipv4.sysctl_max_syn_backlog >> 2)) &&
6340 !tcp_peer_is_proven(req, dst)) {
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006341 /* Without syncookies last quarter of
6342 * backlog is filled with destinations,
6343 * proven to be alive.
6344 * It means that we continue to communicate
6345 * to destinations, already remembered
6346 * to the moment of synflood.
6347 */
6348 pr_drop_req(req, ntohs(tcp_hdr(skb)->source),
6349 rsk_ops->family);
6350 goto drop_and_release;
6351 }
6352
Eric Dumazet84b114b2017-05-05 06:56:54 -07006353 isn = af_ops->init_seq(skb);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006354 }
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006355
Florian Westphalf7b3bec2014-11-03 17:35:03 +01006356 tcp_ecn_create_request(req, skb, sk, dst);
6357
6358 if (want_cookie) {
6359 isn = cookie_init_sequence(af_ops, sk, skb, &req->mss);
6360 req->cookie_ts = tmp_opt.tstamp_ok;
6361 if (!tmp_opt.tstamp_ok)
6362 inet_rsk(req)->ecn_ok = 0;
6363 }
6364
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006365 tcp_rsk(req)->snt_isn = isn;
Eric Dumazet58d607d2015-09-15 15:24:20 -07006366 tcp_rsk(req)->txhash = net_tx_rndhash();
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006367 tcp_openreq_init_rwin(req, sk, dst);
Eric Dumazetca6fb062015-10-02 11:43:35 -07006368 if (!want_cookie) {
Eric Dumazetca6fb062015-10-02 11:43:35 -07006369 tcp_reqsk_record_syn(sk, req, skb);
Christoph Paasch71c02372017-10-23 13:22:23 -07006370 fastopen_sk = tcp_try_fastopen(sk, skb, req, &foc, dst);
Eric Dumazetca6fb062015-10-02 11:43:35 -07006371 }
Eric Dumazet7c85af82015-09-24 17:16:05 -07006372 if (fastopen_sk) {
Eric Dumazetca6fb062015-10-02 11:43:35 -07006373 af_ops->send_synack(fastopen_sk, dst, &fl, req,
Eric Dumazetb3d05142016-04-13 22:05:39 -07006374 &foc, TCP_SYNACK_FASTOPEN);
Eric Dumazet7656d842015-10-04 21:08:07 -07006375 /* Add the child socket directly into the accept queue */
6376 inet_csk_reqsk_queue_add(sk, req, fastopen_sk);
6377 sk->sk_data_ready(sk);
6378 bh_unlock_sock(fastopen_sk);
Eric Dumazet7c85af82015-09-24 17:16:05 -07006379 sock_put(fastopen_sk);
6380 } else {
Eric Dumazet9439ce02015-03-17 18:32:29 -07006381 tcp_rsk(req)->tfo_listener = false;
Eric Dumazetca6fb062015-10-02 11:43:35 -07006382 if (!want_cookie)
Lawrence Brakmo8550f322017-06-30 20:02:42 -07006383 inet_csk_reqsk_queue_hash_add(sk, req,
6384 tcp_timeout_init((struct sock *)req));
Eric Dumazetb3d05142016-04-13 22:05:39 -07006385 af_ops->send_synack(sk, dst, &fl, req, &foc,
6386 !want_cookie ? TCP_SYNACK_NORMAL :
6387 TCP_SYNACK_COOKIE);
Eric Dumazet9caad862016-04-01 08:52:20 -07006388 if (want_cookie) {
6389 reqsk_free(req);
6390 return 0;
6391 }
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006392 }
Eric Dumazetca6fb062015-10-02 11:43:35 -07006393 reqsk_put(req);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006394 return 0;
6395
6396drop_and_release:
6397 dst_release(dst);
6398drop_and_free:
6399 reqsk_free(req);
6400drop:
Eric Dumazet9caad862016-04-01 08:52:20 -07006401 tcp_listendrop(sk);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006402 return 0;
6403}
6404EXPORT_SYMBOL(tcp_conn_request);